xref: /csrg-svn/sys/tahoe/if/if_ace.c (revision 31735)
1*31735Skarels /*	if_ace.c	1.13	87/06/30	*/
224007Ssam 
324007Ssam /*
424007Ssam  * ACC VERSAbus Ethernet controller
524007Ssam  */
624007Ssam #include "ace.h"
724007Ssam #if NACE > 0
824007Ssam 
925694Ssam #include "param.h"
1025694Ssam #include "systm.h"
1125694Ssam #include "mbuf.h"
1225694Ssam #include "buf.h"
1325694Ssam #include "protosw.h"
1425694Ssam #include "socket.h"
1525694Ssam #include "vmmac.h"
1625694Ssam #include "ioctl.h"
1725694Ssam #include "errno.h"
1825694Ssam #include "vmparam.h"
1925855Ssam #include "syslog.h"
2024007Ssam 
2124007Ssam #include "../net/if.h"
2224007Ssam #include "../net/netisr.h"
2324007Ssam #include "../net/route.h"
2425855Ssam #ifdef INET
2524007Ssam #include "../netinet/in.h"
2624007Ssam #include "../netinet/in_systm.h"
2725694Ssam #include "../netinet/in_var.h"
2824007Ssam #include "../netinet/ip.h"
2924007Ssam #include "../netinet/ip_var.h"
3024007Ssam #include "../netinet/if_ether.h"
3125855Ssam #endif
3225855Ssam #ifdef NS
3325855Ssam #include "../netns/ns.h"
3425855Ssam #include "../netns/ns_if.h"
3525855Ssam #endif
3624007Ssam 
3730229Ssam #include "../machine/cpu.h"
3830229Ssam #include "../machine/pte.h"
3930229Ssam 
4024007Ssam #include "../tahoe/mtpr.h"
4124007Ssam #include "../tahoeif/if_acereg.h"
4225694Ssam #include "../tahoevba/vbavar.h"
4324007Ssam 
4424007Ssam int	aceprobe(), aceattach(), acerint(), acecint();
4524007Ssam struct	vba_device *aceinfo[NACE];
4625983Ssam long	acestd[] = { 0 };
4724007Ssam struct	vba_driver acedriver =
4825927Ssam     { aceprobe, 0, aceattach, 0, acestd, "ace", aceinfo, "v/eiu", 0 };
4924007Ssam 
5024007Ssam int	aceinit(), aceoutput(), aceioctl(), acereset();
5124007Ssam struct	mbuf *aceget();
5224007Ssam 
5324007Ssam /*
5424007Ssam  * Ethernet software status per interface.
5524007Ssam  *
5624007Ssam  * Each interface is referenced by a network interface structure,
5724007Ssam  * is_if, which the routing code uses to locate the interface.
5824007Ssam  * This structure contains the output queue for the interface, its address, ...
5924007Ssam  */
6024007Ssam struct	ace_softc {
6124007Ssam 	struct	arpcom is_ac;		/* Ethernet common part	*/
6224007Ssam #define	is_if	is_ac.ac_if		/* network-visible interface */
6324007Ssam #define	is_addr	is_ac.ac_enaddr		/* hardware Ethernet address */
6424007Ssam 	short	is_flags;
6524007Ssam #define	ACEF_OACTIVE	0x1		/* output is active */
6624007Ssam #define	ACEF_RCVPENDING	0x2		/* start rcv in acecint	*/
6724007Ssam 	short	is_promiscuous;		/* true is enabled */
6824007Ssam 	short	is_segboundry;		/* first TX Seg in dpm */
6924007Ssam 	short	is_eictr;		/* Rx segment tracking ctr */
7024007Ssam 	short	is_eoctr;		/* Tx segment tracking ctr */
7124007Ssam 	short	is_txnext;		/* Next available Tx segment */
7224007Ssam 	short	is_currnd;		/* current random backoff */
7324007Ssam 	struct	ace_stats is_stats;	/* holds board statistics */
7424007Ssam 	short	is_xcnt;		/* count xmitted segments to be acked
7524007Ssam 					   by the controller */
7625855Ssam 	long	is_ivec;		/* autoconfig interrupt vector base */
7725927Ssam 	struct	pte *is_map;		/* pte map for dual ported memory */
7825927Ssam 	caddr_t	is_dpm;			/* address of mapped memory */
7924007Ssam } ace_softc[NACE];
8024007Ssam extern	struct ifnet loif;
8124007Ssam 
8225855Ssam aceprobe(reg, vi)
8324007Ssam 	caddr_t reg;
8425855Ssam 	struct vba_device *vi;
8524007Ssam {
8625855Ssam 	register br, cvec;		/* must be r12, r11 */
8725855Ssam 	struct acedevice *ap = (struct acedevice *)reg;
8825855Ssam 	struct ace_softc *is = &ace_softc[vi->ui_unit];
8924007Ssam 
9024007Ssam #ifdef lint
9130295Ssam 	br = 0; cvec = br; br = cvec;
9224007Ssam 	acerint(0); acecint(0);
9324007Ssam #endif
9424007Ssam 	if (badaddr(reg, 2))
9525855Ssam 		return (0);
9625855Ssam 	movow(&ap->csr, CSR_RESET);
9724007Ssam 	DELAY(10000);
9825855Ssam #ifdef notdef
9925855Ssam 	/*
10025855Ssam 	 * Select two spaces for the interrupts aligned to an
10125855Ssam 	 * eight vector boundary and fitting in 8 bits (as
10225855Ssam 	 * required by the controller) -- YECH.  The controller
10325855Ssam 	 * will be notified later at initialization time.
10425855Ssam 	 */
10525855Ssam 	if ((vi->ui_hd->vh_lastiv -= 2) > 0xff)
10625855Ssam 		vi->ui_hd->vh_lastiv  = 0x200;
10725855Ssam 	is->is_ivec = vi->ui_hd->vh_lastiv = vi->ui_hd->vh_lastiv &~ 0x7;
10825855Ssam #else
10925855Ssam 	is->is_ivec = 0x90+vi->ui_unit*8;
11025855Ssam #endif
11125855Ssam 	br = 0x14, cvec = is->is_ivec;		/* XXX */
11225855Ssam 	return (sizeof (*ap));
11324007Ssam }
11424007Ssam 
11524007Ssam /*
11624007Ssam  * Interface exists: make available by filling in network interface
11724007Ssam  * record.  System will initialize the interface when it is ready
11824007Ssam  * to accept packets.
11924007Ssam  */
12024007Ssam aceattach(ui)
12124007Ssam 	struct vba_device *ui;
12224007Ssam {
12324007Ssam 	register short unit = ui->ui_unit;
12424007Ssam 	register struct ace_softc *is = &ace_softc[unit];
12524007Ssam 	register struct ifnet *ifp = &is->is_if;
12624007Ssam 	register struct acedevice *addr = (struct acedevice *)ui->ui_addr;
12724007Ssam 	register short *wp, i;
12824007Ssam 
12924007Ssam 	ifp->if_unit = unit;
13024007Ssam 	ifp->if_name = "ace";
13124007Ssam 	ifp->if_mtu = ETHERMTU;
13224007Ssam 	/*
13329408Ssam 	 * Get station's addresses and set multicast hash table.
13424007Ssam 	 */
13529408Ssam 	for (wp = (short *)addr->station, i = 0; i < 6; i++)
13629408Ssam 		is->is_addr[i] = ~*wp++;
13729408Ssam 	printf("ace%d: hardware address %s\n", unit,
13829408Ssam 	    ether_sprintf(is->is_addr));
13924007Ssam 	is->is_promiscuous = 0;
14029408Ssam 	for (wp = (short *)addr->hash, i =  0; i < 8; i++)
14129408Ssam 		movow(wp++, ~0xf);
14225694Ssam 	movow(&addr->bcastena[0], ~0xffff);
14325694Ssam 	movow(&addr->bcastena[1], ~0xffff);
14425927Ssam 	/*
14525927Ssam 	 * Allocate and map dual ported VERSAbus memory.
14625927Ssam 	 */
147*31735Skarels 	if (vbmemalloc(32, (caddr_t)ui->ui_flags,
148*31735Skarels 	    &is->is_map, &is->is_dpm) == 0) {
149*31735Skarels 		printf("ace%d: can't allocate VERSAbus memory map\n", unit);
150*31735Skarels 		return;
151*31735Skarels 	}
15225927Ssam 
15324007Ssam 	ifp->if_init = aceinit;
15424007Ssam 	ifp->if_output = aceoutput;
15524007Ssam 	ifp->if_ioctl = aceioctl;
15624007Ssam 	ifp->if_reset = acereset;
15725694Ssam 	ifp->if_flags = IFF_BROADCAST;
15824007Ssam 	if_attach(ifp);
15924007Ssam }
16024007Ssam 
16124007Ssam /*
16224007Ssam  * Reset of interface after "system" reset.
16324007Ssam  */
16424007Ssam acereset(unit, vban)
16524007Ssam 	int unit, vban;
16624007Ssam {
16724007Ssam 	register struct vba_device *ui;
16824007Ssam 
16924007Ssam 	if (unit >= NACE || (ui = aceinfo[unit]) == 0 || ui->ui_alive == 0 ||
17024007Ssam 	    ui->ui_vbanum != vban)
17124007Ssam 		return;
17224007Ssam 	printf(" ace%d", unit);
17324007Ssam 	aceinit(unit);
17424007Ssam }
17524007Ssam 
17624007Ssam /*
17724007Ssam  * Initialization of interface; clear recorded pending operations
17824007Ssam  */
17924007Ssam aceinit(unit)
18024007Ssam 	int unit;
18124007Ssam {
18224007Ssam 	register struct ace_softc *is = &ace_softc[unit];
18324007Ssam 	register struct vba_device *ui = aceinfo[unit];
18424007Ssam 	register struct acedevice *addr;
18524007Ssam 	register struct ifnet *ifp = &is->is_if;
18624007Ssam 	register short Csr;
18725694Ssam 	register int s;
18824007Ssam 
18925694Ssam 	if (ifp->if_addrlist == (struct ifaddr *)0)
19024007Ssam 		return;
19124007Ssam 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
19224007Ssam 		/*
19324007Ssam 		 * Reset the controller, initialize the recieve buffers,
19424007Ssam 		 * and turn the controller on again and set board online.
19524007Ssam 		 */
19624007Ssam 		addr = (struct acedevice *)ui->ui_addr;
19724007Ssam 		s = splimp();
19825694Ssam 		movow(&addr->csr, CSR_RESET);
19924007Ssam 		DELAY(10000);
20024007Ssam 
20124007Ssam 		/*
20225927Ssam 		 * Clean up dpm since the controller might
20325927Ssam 		 * jumble dpm after reset.
20424007Ssam 		 */
20525927Ssam 		acesetup(unit);
20625694Ssam 		movow(&addr->csr, CSR_GO);
20724007Ssam 		Csr = addr->csr;
20824007Ssam 		if (Csr & CSR_ACTIVE) {
20925855Ssam 			movow(&addr->ivct, is->is_ivec);
21024007Ssam 			Csr |= CSR_IENA | is->is_promiscuous;
21125694Ssam 			movow(&addr->csr, Csr);
21224007Ssam 			is->is_flags = 0;
21324007Ssam 			is->is_xcnt = 0;
21425694Ssam 			is->is_if.if_flags |= IFF_RUNNING;
21524007Ssam 		}
21624007Ssam 		splx(s);
21724007Ssam 	}
21825694Ssam 	if (is->is_if.if_snd.ifq_head)
21925927Ssam 		acestart(unit);
22024007Ssam }
22124007Ssam 
22224007Ssam /*
22324007Ssam  * Start output on interface.
22424007Ssam  * Get another datagram to send off of the interface queue,
22524007Ssam  * and map it to the interface before starting the output.
22624007Ssam  */
22725927Ssam acestart(unit)
22825927Ssam 	int unit;
22924007Ssam {
23024007Ssam 	register struct tx_segment *txs;
23125694Ssam 	register long len;
23225694Ssam 	register int s;
23324007Ssam 	register struct ace_softc *is = &ace_softc[unit];
23424007Ssam 	struct mbuf *m;
23525694Ssam 	short retries;
23624007Ssam 
23725927Ssam 	if (is->is_flags & ACEF_OACTIVE)
23825927Ssam 		return;
23925927Ssam 	is->is_flags |= ACEF_OACTIVE;
24024007Ssam again:
24124007Ssam 	txs = (struct tx_segment*)(is->is_dpm + (is->is_txnext << 11));
24224007Ssam 	if (txs->tx_csr & TCS_TBFULL) {
24324007Ssam 		is->is_stats.tx_busy++;
24425927Ssam 		is->is_flags &= ~ACEF_OACTIVE;
24524007Ssam 		return;
24624007Ssam 	}
24725694Ssam 	s = splimp();
24824007Ssam 	IF_DEQUEUE(&is->is_if.if_snd, m);
24925694Ssam 	splx(s);
25025927Ssam 	if (m == 0) {
25125927Ssam 		is->is_flags &= ~ACEF_OACTIVE;
25224007Ssam 		return;
25325927Ssam 	}
25424007Ssam 	len = aceput(unit, txs->tx_data, m);
25524007Ssam 	retries = txs->tx_csr & TCS_RTC;
25624007Ssam 	if (retries > 0)
25724007Ssam 		acebakoff(is, txs, retries);
25824007Ssam 
25924007Ssam 	/*
26024007Ssam 	 * Ensure minimum packet length.
26124007Ssam 	 * This makes the safe assumtion that there are no virtual holes
26224007Ssam 	 * after the data.
26324007Ssam 	 * For security, it might be wise to zero out the added bytes,
26424007Ssam 	 * but we're mainly interested in speed at the moment.
26524007Ssam 	 */
26624007Ssam 	if (len - sizeof (struct ether_header) < ETHERMIN)
26724007Ssam 		len = ETHERMIN + sizeof (struct ether_header);
26824007Ssam 	if (++is->is_txnext > SEG_MAX)
26924007Ssam 		is->is_txnext = is->is_segboundry;
27024007Ssam 	is->is_if.if_opackets++;
27124007Ssam 	is->is_xcnt++;
27224007Ssam 	len = (len & 0x7fff) | TCS_TBFULL;
27325694Ssam 	movow(txs, len);
27424007Ssam 	goto again;
27524007Ssam }
27624007Ssam 
27724007Ssam /*
27824007Ssam  * Transmit done interrupt.
27924007Ssam  */
28024007Ssam acecint(unit)
28124007Ssam 	int unit;
28224007Ssam {
28324007Ssam 	register struct ace_softc *is = &ace_softc[unit];
28424007Ssam 	register struct tx_segment *txseg;
28525694Ssam 	short eostat;
28624007Ssam 
28724007Ssam 	if (is->is_xcnt <= 0)  {
28825855Ssam 		log(LOG_ERR, "ace%d: stray xmit interrupt, xcnt %d\n",
28924007Ssam 		    unit, is->is_xcnt);
29024007Ssam 		is->is_xcnt = 0;
29125694Ssam 		if (is->is_if.if_snd.ifq_head)
29225927Ssam 			acestart(unit);
29324007Ssam 		return;
29424007Ssam 	}
29524007Ssam 	is->is_xcnt--;
29624007Ssam 	txseg = (struct tx_segment *)((is->is_eoctr << 11) + is->is_dpm);
29724007Ssam 	eostat = txseg->tx_csr;
29824007Ssam 	if ((eostat & TCS_TBFULL) == 0) {
29924007Ssam 		is->is_stats.tx_retries += eostat & TCS_RTC;
30024007Ssam 		if (eostat & TCS_RTFAIL)  {
30124007Ssam 			is->is_stats.tx_discarded++;
30224007Ssam 			is->is_if.if_oerrors++;
30324007Ssam 		} else
30424007Ssam 			is->is_stats.tx_datagrams++;
30524007Ssam 		if (++is->is_eoctr >= 16)
30624007Ssam 			is->is_eoctr = is->is_segboundry;
30724007Ssam 	}
30825694Ssam 	if (is->is_if.if_snd.ifq_head)
30925927Ssam 		acestart(unit);
31024007Ssam }
31124007Ssam 
31224007Ssam /*
31324007Ssam  * Ethernet interface receiver interrupt.
31424007Ssam  * If input error just drop packet.
31524007Ssam  * Otherwise purge input buffered data path and examine
31624007Ssam  * packet to determine type.  If can't determine length
31724007Ssam  * from type, then have to drop packet.  Othewise decapsulate
31824007Ssam  * packet based on type and pass to type specific higher-level
31924007Ssam  * input routine.
32024007Ssam  */
32124007Ssam acerint(unit)
32224007Ssam 	int unit;
32324007Ssam {
32424007Ssam 	register struct ace_softc *is = &ace_softc[unit];
32524007Ssam 	register struct ifqueue *inq;
32624007Ssam 	register struct ether_header *ace;
32724007Ssam 	register struct rx_segment *rxseg;
32824007Ssam 	int len, s, off, resid;
32924007Ssam 	struct mbuf *m;
33024007Ssam 	short eistat;
33124007Ssam 
33225694Ssam 	if ((is->is_if.if_flags&IFF_RUNNING) == 0)
33325694Ssam 		return;
33424007Ssam again:
33524007Ssam 	rxseg = (struct rx_segment *)((is->is_eictr << 11) + is->is_dpm);
33624007Ssam 	eistat = rxseg->rx_csr;
33724007Ssam 	if ((eistat & RCS_RBFULL) == 0)
33824007Ssam 		return;
33924007Ssam 	is->is_if.if_ipackets++;
34024007Ssam 	if (++is->is_eictr >= is->is_segboundry)
34124007Ssam 		is->is_eictr = 0;
34224007Ssam 	len = eistat & RCS_RBC;
34324007Ssam 	if ((eistat & (RCS_ROVRN | RCS_RCRC | RCS_RODD)) ||
34424007Ssam 	    len < ET_MINLEN || len > ET_MAXLEN+CRC_SIZE) {
34524007Ssam 		if (eistat & RCS_ROVRN)
34624007Ssam 			is->is_stats.rx_overruns++;
34724007Ssam 		if (eistat & RCS_RCRC)
34824007Ssam 			is->is_stats.rx_crc_errors++;
34924007Ssam 		if (eistat & RCS_RODD)
35024007Ssam 			is->is_stats.rx_align_errors++;
35124007Ssam 		if (len < ET_MINLEN)
35224007Ssam 			is->is_stats.rx_underruns++;
35324007Ssam 		if (len > ET_MAXLEN+CRC_SIZE)
35424007Ssam 			is->is_stats.rx_overruns++;
35524007Ssam 		is->is_if.if_ierrors++;
35624007Ssam 		rxseg->rx_csr = 0;
35724007Ssam 		return;
35824007Ssam 	} else
35924007Ssam 		is->is_stats.rx_datagrams++;
36024007Ssam 	ace = (struct ether_header *)rxseg->rx_data;
36124007Ssam 	len -= sizeof (struct ether_header);
36224007Ssam 	/*
36325694Ssam 	 * Deal with trailer protocol: if type is trailer
36424007Ssam 	 * get true type from first 16-bit word past data.
36524007Ssam 	 * Remember that type was trailer by setting off.
36624007Ssam 	 */
36724007Ssam 	ace->ether_type = ntohs((u_short)ace->ether_type);
36824007Ssam #define	acedataaddr(ace, off, type) \
36924007Ssam     ((type)(((caddr_t)(((char *)ace)+sizeof (struct ether_header))+(off))))
37025694Ssam 	if (ace->ether_type >= ETHERTYPE_TRAIL &&
37125694Ssam 	    ace->ether_type < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
37225694Ssam 		off = (ace->ether_type - ETHERTYPE_TRAIL) * 512;
37324007Ssam 		if (off >= ETHERMTU)
37424007Ssam 			goto setup;		/* sanity */
37524007Ssam 		ace->ether_type = ntohs(*acedataaddr(ace, off, u_short *));
37624007Ssam 		resid = ntohs(*(acedataaddr(ace, off+2, u_short *)));
37724007Ssam 		if (off + resid > len)
37824007Ssam 			goto setup;		/* sanity */
37924007Ssam 		len = off + resid;
38024007Ssam 	} else
38124007Ssam 		off = 0;
38224007Ssam 	if (len == 0)
38324007Ssam 		goto setup;
38424007Ssam 
38524007Ssam 	/*
38624007Ssam 	 * Pull packet off interface.  Off is nonzero if packet
38724007Ssam 	 * has trailing header; aceget will then force this header
38824007Ssam 	 * information to be at the front, but we still have to drop
38924007Ssam 	 * the type and length which are at the front of any trailer data.
39024007Ssam 	 */
39125855Ssam 	m = aceget((u_char *)rxseg->rx_data, len, off, &is->is_if);
39224007Ssam 	if (m == 0)
39324007Ssam 		goto setup;
39424007Ssam 	if (off) {
39525855Ssam 		struct ifnet *ifp;
39625855Ssam 
39725855Ssam 		ifp = *(mtod(m, struct ifnet **));
39824007Ssam 		m->m_off += 2 * sizeof (u_short);
39924007Ssam 		m->m_len -= 2 * sizeof (u_short);
40025855Ssam 		*(mtod(m, struct ifnet **)) = ifp;
40124007Ssam 	}
40224007Ssam 	switch (ace->ether_type) {
40324007Ssam 
40424007Ssam #ifdef INET
40525694Ssam 	case ETHERTYPE_IP:
40624007Ssam 		schednetisr(NETISR_IP);
40724007Ssam 		inq = &ipintrq;
40824007Ssam 		break;
40925855Ssam #endif
41024007Ssam 
41125694Ssam 	case ETHERTYPE_ARP:
41224007Ssam 		arpinput(&is->is_ac, m);
41324007Ssam 		goto setup;
41425694Ssam #ifdef NS
41525694Ssam 	case ETHERTYPE_NS:
41625694Ssam 		schednetisr(NETISR_NS);
41725694Ssam 		inq = &nsintrq;
41825694Ssam 		break;
41925694Ssam 
42025694Ssam #endif
42124007Ssam 	default:
42224007Ssam 		m_freem(m);
42324007Ssam 		goto setup;
42424007Ssam 	}
42524007Ssam 	if (IF_QFULL(inq)) {
42624007Ssam 		IF_DROP(inq);
42724007Ssam 		m_freem(m);
42824007Ssam 		goto setup;
42924007Ssam 	}
43024007Ssam 	s = splimp();
43124007Ssam 	IF_ENQUEUE(inq, m);
43224007Ssam 	splx(s);
43324007Ssam setup:
43424007Ssam 	rxseg->rx_csr = 0;
43524007Ssam 	goto again;
43624007Ssam }
43724007Ssam 
43824007Ssam /*
43924007Ssam  * Ethernet output routine.
44024007Ssam  * Encapsulate a packet of type family for the local net.
44124007Ssam  * Use trailer local net encapsulation if enough data in first
44224007Ssam  * packet leaves a multiple of 512 bytes of data in remainder.
44324007Ssam  */
44424007Ssam aceoutput(ifp, m0, dst)
44524007Ssam 	struct ifnet *ifp;
44624007Ssam 	struct mbuf *m0;
44724007Ssam 	struct sockaddr *dst;
44824007Ssam {
44924007Ssam 	register struct ace_softc *is = &ace_softc[ifp->if_unit];
45024007Ssam 	register struct mbuf *m = m0;
45124007Ssam 	register struct ether_header *ace;
45224007Ssam 	register int off;
45324007Ssam 	struct mbuf *mcopy = (struct mbuf *)0;
45425957Ssam 	int type, s, error, usetrailers;
45525694Ssam 	u_char edst[6];
45624007Ssam 	struct in_addr idst;
45724007Ssam 
45825855Ssam 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
45925855Ssam 		error = ENETDOWN;
46025855Ssam 		goto bad;
46125855Ssam 	}
46224007Ssam 	switch (dst->sa_family) {
46324007Ssam 
46424007Ssam #ifdef INET
46524007Ssam 	case AF_INET:
46624007Ssam 		idst = ((struct sockaddr_in *)dst)->sin_addr;
46725957Ssam 		if (!arpresolve(&is->is_ac, m, &idst, edst, &usetrailers))
46824007Ssam 			return (0);	/* if not yet resolved */
46925694Ssam 		if (!bcmp((caddr_t)edst, (caddr_t)etherbroadcastaddr,
47025937Ssam 		    sizeof (edst)))
47124007Ssam 			mcopy = m_copy(m, 0, (int)M_COPYALL);
47224007Ssam 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
47325957Ssam 		if (usetrailers && off > 0 && (off & 0x1ff) == 0 &&
47424007Ssam 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
47525694Ssam 			type = ETHERTYPE_TRAIL + (off>>9);
47624007Ssam 			m->m_off -= 2 * sizeof (u_short);
47724007Ssam 			m->m_len += 2 * sizeof (u_short);
47825694Ssam 			*mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP);
47924007Ssam 			*(mtod(m, u_short *) + 1) = htons((u_short)m->m_len);
48024007Ssam 			goto gottrailertype;
48124007Ssam 		}
48225694Ssam 		type = ETHERTYPE_IP;
48324007Ssam 		off = 0;
48424007Ssam 		goto gottype;
48524007Ssam #endif
48625694Ssam #ifdef NS
48725694Ssam 	case AF_NS:
48825694Ssam  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
48925694Ssam 		    (caddr_t)edst, sizeof (edst));
49025694Ssam 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost,sizeof(edst)))
49125694Ssam 			mcopy = m_copy(m, 0, (int)M_COPYALL);
49225694Ssam 		else if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost,
49325694Ssam 		    sizeof(edst)))
49425694Ssam 			return(looutput(&loif, m, dst));
49525694Ssam 		type = ETHERTYPE_NS;
49625694Ssam 		off = 0;
49725694Ssam 		goto gottype;
49825694Ssam #endif
49924007Ssam 	case AF_UNSPEC:
50024007Ssam 		ace = (struct ether_header *)dst->sa_data;
50125694Ssam 		bcopy((caddr_t)ace->ether_dhost, (caddr_t)edst, sizeof (edst));
50224007Ssam 		type = ace->ether_type;
50324007Ssam 		goto gottype;
50424007Ssam 
50524007Ssam 	default:
50625855Ssam 		log(LOG_ERR, "ace%d: can't handle af%d\n",
50724007Ssam 		    ifp->if_unit, dst->sa_family);
50824007Ssam 		error = EAFNOSUPPORT;
50924007Ssam 		goto bad;
51024007Ssam 	}
51124007Ssam 
51224007Ssam gottrailertype:
51324007Ssam 	/*
51424007Ssam 	 * Packet to be sent as trailer: move first packet
51524007Ssam 	 * (control information) to end of chain.
51624007Ssam 	 */
51724007Ssam 	while (m->m_next)
51824007Ssam 		m = m->m_next;
51924007Ssam 	m->m_next = m0;
52024007Ssam 	m = m0->m_next;
52124007Ssam 	m0->m_next = 0;
52224007Ssam 	m0 = m;
52324007Ssam 
52424007Ssam gottype:
52524007Ssam 	/*
52624007Ssam 	 * Add local net header.  If no space in first mbuf,
52724007Ssam 	 * allocate another.
52824007Ssam 	 */
52924007Ssam 	if (m->m_off > MMAXOFF ||
53024007Ssam 	    MMINOFF + sizeof (struct ether_header) > m->m_off) {
53124007Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
53224007Ssam 		if (m == 0) {
53324007Ssam 			error = ENOBUFS;
53424007Ssam 			goto bad;
53524007Ssam 		}
53624007Ssam 		m->m_next = m0;
53724007Ssam 		m->m_off = MMINOFF;
53824007Ssam 		m->m_len = sizeof (struct ether_header);
53924007Ssam 	} else {
54024007Ssam 		m->m_off -= sizeof (struct ether_header);
54124007Ssam 		m->m_len += sizeof (struct ether_header);
54224007Ssam 	}
54324007Ssam 	ace = mtod(m, struct ether_header *);
54425694Ssam 	bcopy((caddr_t)edst, (caddr_t)ace->ether_dhost, sizeof (edst));
54525694Ssam 	bcopy((caddr_t)is->is_addr, (caddr_t)ace->ether_shost,
54625694Ssam 	    sizeof (is->is_addr));
54724007Ssam 	ace->ether_type = htons((u_short)type);
54824007Ssam 
54924007Ssam 	/*
55024007Ssam 	 * Queue message on interface, and start output if interface
55124007Ssam 	 * not yet active.
55224007Ssam 	 */
55324007Ssam 	s = splimp();
55424007Ssam 	if (IF_QFULL(&ifp->if_snd)) {
55524007Ssam 		IF_DROP(&ifp->if_snd);
55624007Ssam 		error = ENOBUFS;
55724007Ssam 		goto qfull;
55824007Ssam 	}
55924007Ssam 	IF_ENQUEUE(&ifp->if_snd, m);
56024007Ssam 	splx(s);
56125927Ssam 	acestart(ifp->if_unit);
56224007Ssam 	return (mcopy ? looutput(&loif, mcopy, dst) : 0);
56324007Ssam qfull:
56424007Ssam 	m0 = m;
56524007Ssam 	splx(s);
56624007Ssam bad:
56724007Ssam 	m_freem(m0);
56824007Ssam 	if (mcopy)
56924007Ssam 		m_freem(mcopy);
57024007Ssam 	return (error);
57124007Ssam }
57224007Ssam 
57324007Ssam /*
57424007Ssam  * Routine to copy from mbuf chain to transmit buffer on the VERSAbus
57524007Ssam  * If packet size is less than the minimum legal size,
57624007Ssam  * the buffer is expanded.  We probably should zero out the extra
57724007Ssam  * bytes for security, but that would slow things down.
57824007Ssam  */
57925694Ssam /*ARGSUSED*/
58024007Ssam aceput(unit, txbuf, m)
58125855Ssam 	int unit;
58225694Ssam 	char *txbuf;
58324007Ssam 	struct mbuf *m;
58424007Ssam {
58525855Ssam 	register u_char *bp, *mcp;
58625855Ssam 	register short *s1, *s2;
58725694Ssam 	register u_int len;
58824007Ssam 	register struct mbuf *mp;
58925694Ssam 	int total;
59024007Ssam 
59124007Ssam 	total = 0;
59225694Ssam 	bp = (u_char *)txbuf;
59325694Ssam 	for (mp = m; (mp); mp = mp->m_next) {
59424007Ssam 		len = mp->m_len;
59524007Ssam 		if (len == 0)
59624007Ssam 			continue;
59724007Ssam 		total += len;
59824007Ssam 		mcp = mtod(mp, u_char *);
59924007Ssam 		if (((int)mcp & 01) && ((int)bp & 01)) {
60024007Ssam 			/* source & destination at odd addresses */
60125694Ssam 			movob(bp++, *mcp++);
60224007Ssam 			--len;
60324007Ssam 		}
60424007Ssam 		if (len > 1 && (((int)mcp & 01)==0) && (((int)bp & 01)==0)) {
60525694Ssam 			register u_int l;
60624007Ssam 
60724007Ssam 			s1 = (short *)bp;
60824007Ssam 			s2 = (short *)mcp;
60924007Ssam 			l = len >> 1;		/* count # of shorts */
61025694Ssam 			while (l-- != 0)
61125694Ssam 				movow(s1++, *s2++);
61224007Ssam 			len &= 1;		/* # remaining bytes */
61324007Ssam 			bp = (u_char *)s1;
61424007Ssam 			mcp = (u_char *)s2;
61524007Ssam 		}
61625694Ssam 		while (len-- != 0)
61725694Ssam 			movob(bp++, *mcp++);
61824007Ssam 	}
61924007Ssam 	m_freem(m);
62024007Ssam 	return (total);
62124007Ssam }
62224007Ssam 
62324007Ssam /*
62424007Ssam  * Routine to copy from VERSAbus memory into mbufs.
62524007Ssam  *
62624007Ssam  * Warning: This makes the fairly safe assumption that
62724007Ssam  * mbufs have even lengths.
62824007Ssam  */
62925694Ssam /*ARGSUSED*/
63024007Ssam struct mbuf *
63125855Ssam aceget(rxbuf, totlen, off0, ifp)
63224007Ssam 	u_char *rxbuf;
63324007Ssam 	int totlen, off0;
63425855Ssam 	struct ifnet *ifp;
63524007Ssam {
63625855Ssam 	register u_char *cp, *mcp;
63724007Ssam 	register int tlen;
63824007Ssam 	register struct mbuf *m;
63924007Ssam 	struct mbuf *top = 0, **mp = &top;
64024007Ssam 	int len, off = off0;
64124007Ssam 
64224007Ssam 	cp = rxbuf + sizeof (struct ether_header);
64324007Ssam 	while (totlen > 0) {
64424007Ssam 		MGET(m, M_DONTWAIT, MT_DATA);
64524007Ssam 		if (m == 0)
64624007Ssam 			goto bad;
64724007Ssam 		if (off) {
64824007Ssam 			len = totlen - off;
64924007Ssam 			cp = rxbuf + sizeof (struct ether_header) + off;
65024007Ssam 		} else
65124007Ssam 			len = totlen;
65225855Ssam 		if (ifp)
65325855Ssam 			len += sizeof(ifp);
65429563Ssam 		if (len >= NBPG) {
65529563Ssam 			MCLGET(m);
65629563Ssam 			if (m->m_len == CLBYTES)
65729563Ssam 				m->m_len = len = MIN(len, CLBYTES);
65829563Ssam 			else
65924007Ssam 				m->m_len = len = MIN(MLEN, len);
66024007Ssam 		} else {
66124007Ssam 			m->m_len = len = MIN(MLEN, len);
66224007Ssam 			m->m_off = MMINOFF;
66324007Ssam 		}
66424007Ssam 		mcp = mtod(m, u_char *);
66525855Ssam 		if (ifp) {
66625855Ssam 			/*
66725855Ssam 			 * Prepend interface pointer to first mbuf.
66825855Ssam 			 */
66925855Ssam 			*(mtod(m, struct ifnet **)) = ifp;
67025855Ssam 			mcp += sizeof(ifp);
67125855Ssam 			len -= sizeof(ifp);
67225855Ssam 			ifp = (struct ifnet *)0;
67325855Ssam 		}
67424007Ssam 		/*bcopy((caddr_t)cp, (caddr_t)mcp, len);*/
67524007Ssam 		/*cp += len; mcp += len;*/
67624007Ssam 		tlen = len;
67724007Ssam 		if (((int)mcp & 01) && ((int)cp & 01)) {
67824007Ssam 			/* source & destination at odd addresses */
67924007Ssam 			*mcp++ = *cp++;
68024007Ssam 			--tlen;
68124007Ssam 		}
68224007Ssam 		if (tlen > 1 && (((int)mcp&01) == 0) && (((int)cp&01) == 0)) {
68324007Ssam 			register short *s1, *s2;
68424007Ssam 			register int l;
68524007Ssam 
68624007Ssam 			s1 = (short *)mcp;
68724007Ssam 			s2 = (short *)cp;
68824007Ssam 			l = tlen >> 1;		/* count # of shorts */
68924007Ssam 			while (l-- > 0)		/* copy shorts */
69024007Ssam 				*s1++ = *s2++;
69124007Ssam 			tlen &= 1;		/* # remaining bytes */
69224007Ssam 			mcp = (u_char *)s1;
69324007Ssam 			cp = (u_char *)s2;
69424007Ssam 		}
69524007Ssam 		while (tlen-- > 0)
69624007Ssam 			*mcp++ = *cp++;
69724007Ssam 		*mp = m;
69824007Ssam 		mp = &m->m_next;
69924007Ssam 		if (off == 0) {
70024007Ssam 			totlen -= len;
70124007Ssam 			continue;
70224007Ssam 		}
70324007Ssam 		off += len;
70424007Ssam 		if (off == totlen) {
70524007Ssam 			cp = rxbuf + sizeof (struct ether_header);
70624007Ssam 			off = 0;
70724007Ssam 			totlen = off0;
70824007Ssam 		}
70924007Ssam 	}
71024007Ssam 	return (top);
71124007Ssam bad:
71224007Ssam 	m_freem(top);
71324007Ssam 	return (0);
71424007Ssam }
71524007Ssam 
71629408Ssam /* backoff table masks */
71729408Ssam short	random_mask_tbl[16] = {
71829563Ssam 	0x0040, 0x00c0, 0x01c0, 0x03c0, 0x07c0, 0x0fc0, 0x1fc0, 0x3fc0,
71929563Ssam 	0x7fc0, 0xffc0, 0xffc0, 0xffc0, 0xffc0, 0xffc0, 0xffc0, 0xffc0
72029408Ssam };
72129408Ssam 
72224007Ssam acebakoff(is, txseg, retries)
72324007Ssam 	struct ace_softc *is;
72424007Ssam 	struct tx_segment *txseg;
72524007Ssam 	register int retries;
72624007Ssam {
72724007Ssam 	register short *pBakNum, random_num;
72824007Ssam 	short *pMask;
72924007Ssam 
73024007Ssam 	pMask = &random_mask_tbl[0];
73124007Ssam 	pBakNum = &txseg->tx_backoff[0];
73224007Ssam 	while (--retries >= 0) {
73324007Ssam 		random_num = (is->is_currnd = (is->is_currnd * 18741)-13849);
73424007Ssam 		random_num &= *pMask++;
73529563Ssam 		*pBakNum++ = random_num ^ (short)(0xff00 | 0x00fc);
73624007Ssam 	}
73724007Ssam }
73824007Ssam 
73924007Ssam /*
74024007Ssam  * Process an ioctl request.
74124007Ssam  */
74224007Ssam aceioctl(ifp, cmd, data)
74324007Ssam 	register struct ifnet *ifp;
74424007Ssam 	int cmd;
74524007Ssam 	caddr_t data;
74624007Ssam {
74725694Ssam 	register struct ifaddr *ifa = (struct ifaddr *)data;
74825855Ssam 	struct acedevice *addr;
74925694Ssam 	int s = splimp(), error = 0;
75024007Ssam 
75124007Ssam 	switch (cmd) {
75224007Ssam 
75324007Ssam 	case SIOCSIFADDR:
75425694Ssam 		ifp->if_flags |= IFF_UP;
75525855Ssam 		switch (ifa->ifa_addr.sa_family) {
75625855Ssam #ifdef INET
75725855Ssam 		case AF_INET:
75825855Ssam 			aceinit(ifp->if_unit);	/* before arpwhohas */
75925855Ssam 			((struct arpcom *)ifp)->ac_ipaddr =
76025855Ssam 				IA_SIN(ifa)->sin_addr;
76125855Ssam 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
76225855Ssam 			break;
76325855Ssam #endif
76425855Ssam #ifdef NS
76525855Ssam 		case AF_NS: {
76625937Ssam 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
76725937Ssam 			struct ace_softc *is = &ace_softc[ifp->if_unit];
76825855Ssam 
76925855Ssam 			if (!ns_nullhost(*ina)) {
77025855Ssam 				ifp->if_flags &= ~IFF_RUNNING;
77125855Ssam 				addr = (struct acedevice *)
77225937Ssam 				    aceinfo[ifp->if_unit]->ui_addr;
77325855Ssam 				movow(&addr->csr, CSR_RESET);
77425855Ssam 				DELAY(10000);
77525855Ssam 				/* set station address & copy addr to arp */
77629408Ssam 				acesetaddr(ifp->if_unit, addr,
77725855Ssam 				    ina->x_host.c_host);
77825855Ssam 			} else
77925937Ssam 				ina->x_host = *(union ns_host *)is->is_addr;
78025855Ssam 			aceinit(ifp->if_unit);
78125855Ssam 			break;
78225855Ssam 		}
78325855Ssam #endif
78425855Ssam 		default:
78525855Ssam 			aceinit(ifp->if_unit);
78625855Ssam 			break;
78725855Ssam 		}
78824007Ssam 		break;
78924007Ssam 
79025855Ssam 	case SIOCSIFFLAGS:
79125855Ssam 		if ((ifp->if_flags&IFF_UP) == 0 && ifp->if_flags&IFF_RUNNING) {
79225855Ssam 			addr = (struct acedevice *)
79325855Ssam 			    (aceinfo[ifp->if_unit]->ui_addr);
79425855Ssam 			movow(&addr->csr, CSR_RESET);
79525855Ssam 			ifp->if_flags &= ~IFF_RUNNING;
79625855Ssam 		} else if (ifp->if_flags&IFF_UP &&
79725855Ssam 		    (ifp->if_flags&IFF_RUNNING) == 0)
79825855Ssam 			aceinit(ifp->if_unit);
79924007Ssam 		break;
80024007Ssam 
80124007Ssam 	default:
80224007Ssam 		error = EINVAL;
80324007Ssam 	}
80424007Ssam 	splx(s);
80524007Ssam 	return (error);
80624007Ssam }
80724007Ssam 
80829408Ssam /*
80929408Ssam  * Set the on-board station address, then read it back
81029408Ssam  * to initialize the address used by ARP (among others).
81129408Ssam  */
81229408Ssam acesetaddr(unit, addr, station)
81329408Ssam 	short unit;
81429408Ssam 	struct acedevice *addr;
81530295Ssam 	u_char *station;
81629408Ssam {
81729408Ssam 	struct ace_softc *is = &ace_softc[unit];
81829408Ssam 	register short *wp, i;
81929408Ssam 
82029408Ssam 	for (wp = (short *)addr->station, i = 0; i < 6; i++)
82129408Ssam 		movow(wp++, ~*station++);
82229408Ssam 	for (wp = (short *)addr->station, i = 0; i < 6; i++)
82329408Ssam 		is->is_addr[i] = ~*wp++;
82429408Ssam 	printf("ace%d: hardware address %s\n", unit,
82529408Ssam 	    ether_sprintf(is->is_addr));
82629408Ssam }
82729408Ssam 
82829408Ssam /*
82929408Ssam  * Setup the device for use.  Initialize dual-ported memory,
83029408Ssam  * backoff parameters, and various other software state.
83129408Ssam  */
83225927Ssam acesetup(unit)
83324007Ssam 	int unit;
83424007Ssam {
83524007Ssam 	register struct ace_softc *is = &ace_softc[unit];
83625927Ssam 	register char *pData1;
83725694Ssam 	register short i;
83825927Ssam 	struct acedevice *addr;
83924007Ssam 
84025927Ssam 	bzero(is->is_dpm, 16384*2);
84124007Ssam 	is->is_currnd = 49123;
84225927Ssam 	addr = (struct acedevice *)aceinfo[unit]->ui_addr;
84324007Ssam 	is->is_segboundry = (addr->segb >> 11) & 0xf;
84425927Ssam 	pData1 = is->is_dpm + (is->is_segboundry << 11);
84524007Ssam 	for (i = SEG_MAX + 1 - is->is_segboundry; --i >= 0;) {
84624007Ssam 		acebakoff(is, (struct tx_segment *)pData1, 15);
84724007Ssam 		pData1 += sizeof (struct tx_segment);
84824007Ssam 	}
84924007Ssam 	is->is_eictr = 0;
85024007Ssam 	is->is_eoctr = is->is_txnext = is->is_segboundry;
85124007Ssam 	bzero((char *)&is->is_stats, sizeof (is->is_stats));
85224007Ssam }
85324007Ssam #endif
854