xref: /csrg-svn/sys/tahoe/if/if_ace.c (revision 25983)
1*25983Ssam /*	if_ace.c	1.8	86/01/26	*/
224007Ssam 
324007Ssam /*
424007Ssam  * ACC VERSAbus Ethernet controller
524007Ssam  */
624007Ssam #include "ace.h"
724007Ssam #if NACE > 0
824007Ssam 
924007Ssam #include "../machine/pte.h"
1024007Ssam 
1125694Ssam #include "param.h"
1225694Ssam #include "systm.h"
1325694Ssam #include "mbuf.h"
1425694Ssam #include "buf.h"
1525694Ssam #include "protosw.h"
1625694Ssam #include "socket.h"
1725694Ssam #include "vmmac.h"
1825694Ssam #include "ioctl.h"
1925694Ssam #include "errno.h"
2025694Ssam #include "vmparam.h"
2125855Ssam #include "syslog.h"
2224007Ssam 
2324007Ssam #include "../net/if.h"
2424007Ssam #include "../net/netisr.h"
2524007Ssam #include "../net/route.h"
2625855Ssam #ifdef INET
2724007Ssam #include "../netinet/in.h"
2824007Ssam #include "../netinet/in_systm.h"
2925694Ssam #include "../netinet/in_var.h"
3024007Ssam #include "../netinet/ip.h"
3124007Ssam #include "../netinet/ip_var.h"
3224007Ssam #include "../netinet/if_ether.h"
3325855Ssam #endif
3425855Ssam #ifdef NS
3525855Ssam #include "../netns/ns.h"
3625855Ssam #include "../netns/ns_if.h"
3725855Ssam #endif
3824007Ssam 
3924007Ssam #include "../tahoe/mtpr.h"
4024007Ssam #include "../tahoeif/if_acereg.h"
4125694Ssam #include "../tahoevba/vbavar.h"
4224007Ssam 
4324007Ssam /* station address */
4424007Ssam char	ace_station[6] = { ~0x8, ~0x0, ~0x3, ~0x0, ~0x0, ~0x1 };
4524007Ssam /* multicast hash table initializer */
4624007Ssam char	ace_hash[8] = { ~0xF,~0xF,~0xF,~0xF,~0xF,~0xF,~0xF,~0xF };
4724007Ssam /* backoff table masks */
4824007Ssam short random_mask_tbl[16] = {
4924007Ssam 	0x0040, 0x00C0, 0x01C0, 0x03C0, 0x07C0, 0x0FC0, 0x1FC0, 0x3FC0,
5024007Ssam 	0x7FC0, 0xFFC0, 0xFFC0, 0xFFC0, 0xFFC0, 0xFFC0, 0xFFC0, 0xFFC0
5124007Ssam };
5224007Ssam 
5324007Ssam int	aceprobe(), aceattach(), acerint(), acecint();
5424007Ssam struct	vba_device *aceinfo[NACE];
55*25983Ssam long	acestd[] = { 0 };
5624007Ssam struct	vba_driver acedriver =
5725927Ssam     { aceprobe, 0, aceattach, 0, acestd, "ace", aceinfo, "v/eiu", 0 };
5824007Ssam 
5924007Ssam int	aceinit(), aceoutput(), aceioctl(), acereset();
6024007Ssam struct	mbuf *aceget();
6124007Ssam 
6224007Ssam /*
6324007Ssam  * Ethernet software status per interface.
6424007Ssam  *
6524007Ssam  * Each interface is referenced by a network interface structure,
6624007Ssam  * is_if, which the routing code uses to locate the interface.
6724007Ssam  * This structure contains the output queue for the interface, its address, ...
6824007Ssam  */
6924007Ssam struct	ace_softc {
7024007Ssam 	struct	arpcom is_ac;		/* Ethernet common part	*/
7124007Ssam #define	is_if	is_ac.ac_if		/* network-visible interface */
7224007Ssam #define	is_addr	is_ac.ac_enaddr		/* hardware Ethernet address */
7324007Ssam 	short	is_flags;
7424007Ssam #define	ACEF_OACTIVE	0x1		/* output is active */
7524007Ssam #define	ACEF_RCVPENDING	0x2		/* start rcv in acecint	*/
7624007Ssam 	short	is_promiscuous;		/* true is enabled */
7724007Ssam 	short	is_segboundry;		/* first TX Seg in dpm */
7824007Ssam 	short	is_eictr;		/* Rx segment tracking ctr */
7924007Ssam 	short	is_eoctr;		/* Tx segment tracking ctr */
8024007Ssam 	short	is_txnext;		/* Next available Tx segment */
8124007Ssam 	short	is_currnd;		/* current random backoff */
8224007Ssam 	struct	ace_stats is_stats;	/* holds board statistics */
8324007Ssam 	short	is_xcnt;		/* count xmitted segments to be acked
8424007Ssam 					   by the controller */
8525855Ssam 	long	is_ivec;		/* autoconfig interrupt vector base */
8625927Ssam 	struct	pte *is_map;		/* pte map for dual ported memory */
8725927Ssam 	caddr_t	is_dpm;			/* address of mapped memory */
8824007Ssam } ace_softc[NACE];
8924007Ssam extern	struct ifnet loif;
9024007Ssam 
9125855Ssam aceprobe(reg, vi)
9224007Ssam 	caddr_t reg;
9325855Ssam 	struct vba_device *vi;
9424007Ssam {
9525855Ssam 	register br, cvec;		/* must be r12, r11 */
9625855Ssam 	struct acedevice *ap = (struct acedevice *)reg;
9725855Ssam 	struct ace_softc *is = &ace_softc[vi->ui_unit];
9824007Ssam 
9924007Ssam #ifdef lint
10024007Ssam 	acerint(0); acecint(0);
10124007Ssam #endif
10224007Ssam 	if (badaddr(reg, 2))
10325855Ssam 		return (0);
10425855Ssam 	movow(&ap->csr, CSR_RESET);
10524007Ssam 	DELAY(10000);
10625855Ssam #ifdef notdef
10725855Ssam 	/*
10825855Ssam 	 * Select two spaces for the interrupts aligned to an
10925855Ssam 	 * eight vector boundary and fitting in 8 bits (as
11025855Ssam 	 * required by the controller) -- YECH.  The controller
11125855Ssam 	 * will be notified later at initialization time.
11225855Ssam 	 */
11325855Ssam 	if ((vi->ui_hd->vh_lastiv -= 2) > 0xff)
11425855Ssam 		vi->ui_hd->vh_lastiv  = 0x200;
11525855Ssam 	is->is_ivec = vi->ui_hd->vh_lastiv = vi->ui_hd->vh_lastiv &~ 0x7;
11625855Ssam #else
11725855Ssam 	is->is_ivec = 0x90+vi->ui_unit*8;
11825855Ssam #endif
11925855Ssam 	br = 0x14, cvec = is->is_ivec;		/* XXX */
12025855Ssam 	return (sizeof (*ap));
12124007Ssam }
12224007Ssam 
12324007Ssam /*
12424007Ssam  * Interface exists: make available by filling in network interface
12524007Ssam  * record.  System will initialize the interface when it is ready
12624007Ssam  * to accept packets.
12724007Ssam  */
12824007Ssam aceattach(ui)
12924007Ssam 	struct vba_device *ui;
13024007Ssam {
13124007Ssam 	register short unit = ui->ui_unit;
13224007Ssam 	register struct ace_softc *is = &ace_softc[unit];
13324007Ssam 	register struct ifnet *ifp = &is->is_if;
13424007Ssam 	register struct acedevice *addr = (struct acedevice *)ui->ui_addr;
13524007Ssam 	register short *wp, i;
13624007Ssam 
13724007Ssam 	ifp->if_unit = unit;
13824007Ssam 	ifp->if_name = "ace";
13924007Ssam 	ifp->if_mtu = ETHERMTU;
14024007Ssam 	/*
14125927Ssam 	 * Set station's addresses and multicast hash table.
14224007Ssam 	 */
14324007Ssam 	ace_station[5] = ~(unit + 1);
14424007Ssam 	acesetetaddr(unit, addr, ace_station);
14524007Ssam 	is->is_promiscuous = 0;
14624007Ssam 	wp = (short *)addr->hash;
14724007Ssam 	for (i =  0; i < 8; i++)
14825694Ssam 		movow(wp++, ace_hash[i]);
14925694Ssam 	movow(&addr->bcastena[0], ~0xffff);
15025694Ssam 	movow(&addr->bcastena[1], ~0xffff);
15125927Ssam 	/*
15225927Ssam 	 * Allocate and map dual ported VERSAbus memory.
15325927Ssam 	 */
15425927Ssam 	vbmemalloc(32, (caddr_t)ui->ui_flags, &is->is_map, &is->is_dpm);
15525927Ssam 
15624007Ssam 	ifp->if_init = aceinit;
15724007Ssam 	ifp->if_output = aceoutput;
15824007Ssam 	ifp->if_ioctl = aceioctl;
15924007Ssam 	ifp->if_reset = acereset;
16025694Ssam 	ifp->if_flags = IFF_BROADCAST;
16124007Ssam 	if_attach(ifp);
16224007Ssam }
16324007Ssam 
16424007Ssam acesetetaddr(unit, addr, station_addr)
16524007Ssam 	short unit;
16624007Ssam 	struct acedevice *addr;
16724007Ssam 	char *station_addr;
16824007Ssam {
16924007Ssam 	register short *wp, i;
17024007Ssam 	register char *cp;
17124007Ssam 	struct ace_softc *is = &ace_softc[unit];
17224007Ssam 
17324007Ssam 	wp = (short *)addr->station;
17424007Ssam 	cp = station_addr;
17524007Ssam 	for (i = 0; i < 6; i++)
17625694Ssam 		movow(wp++, *cp++);
17724007Ssam 	wp = (short *)addr->station;
17825694Ssam 	cp = (char *)is->is_addr;
17924007Ssam 	for (i = 0; i < 6; i++)
18024007Ssam 		*cp++ = ~(*wp++);
18124007Ssam }
18224007Ssam 
18324007Ssam /*
18424007Ssam  * Reset of interface after "system" reset.
18524007Ssam  */
18624007Ssam acereset(unit, vban)
18724007Ssam 	int unit, vban;
18824007Ssam {
18924007Ssam 	register struct vba_device *ui;
19024007Ssam 
19124007Ssam 	if (unit >= NACE || (ui = aceinfo[unit]) == 0 || ui->ui_alive == 0 ||
19224007Ssam 	    ui->ui_vbanum != vban)
19324007Ssam 		return;
19424007Ssam 	printf(" ace%d", unit);
19524007Ssam 	aceinit(unit);
19624007Ssam }
19724007Ssam 
19824007Ssam /*
19924007Ssam  * Initialization of interface; clear recorded pending operations
20024007Ssam  */
20124007Ssam aceinit(unit)
20224007Ssam 	int unit;
20324007Ssam {
20424007Ssam 	register struct ace_softc *is = &ace_softc[unit];
20524007Ssam 	register struct vba_device *ui = aceinfo[unit];
20624007Ssam 	register struct acedevice *addr;
20724007Ssam 	register struct ifnet *ifp = &is->is_if;
20824007Ssam 	register short Csr;
20925694Ssam 	register int s;
21024007Ssam 
21125694Ssam 	if (ifp->if_addrlist == (struct ifaddr *)0)
21224007Ssam 		return;
21324007Ssam 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
21424007Ssam 		/*
21524007Ssam 		 * Reset the controller, initialize the recieve buffers,
21624007Ssam 		 * and turn the controller on again and set board online.
21724007Ssam 		 */
21824007Ssam 		addr = (struct acedevice *)ui->ui_addr;
21924007Ssam 		s = splimp();
22025694Ssam 		movow(&addr->csr, CSR_RESET);
22124007Ssam 		DELAY(10000);
22224007Ssam 
22324007Ssam 		/*
22425927Ssam 		 * Clean up dpm since the controller might
22525927Ssam 		 * jumble dpm after reset.
22624007Ssam 		 */
22725927Ssam 		acesetup(unit);
22825694Ssam 		movow(&addr->csr, CSR_GO);
22924007Ssam 		Csr = addr->csr;
23024007Ssam 		if (Csr & CSR_ACTIVE) {
23125855Ssam 			movow(&addr->ivct, is->is_ivec);
23224007Ssam 			Csr |= CSR_IENA | is->is_promiscuous;
23325694Ssam 			movow(&addr->csr, Csr);
23424007Ssam 			is->is_flags = 0;
23524007Ssam 			is->is_xcnt = 0;
23625694Ssam 			is->is_if.if_flags |= IFF_RUNNING;
23724007Ssam 		}
23824007Ssam 		splx(s);
23924007Ssam 	}
24025694Ssam 	if (is->is_if.if_snd.ifq_head)
24125927Ssam 		acestart(unit);
24224007Ssam }
24324007Ssam 
24424007Ssam /*
24524007Ssam  * Start output on interface.
24624007Ssam  * Get another datagram to send off of the interface queue,
24724007Ssam  * and map it to the interface before starting the output.
24824007Ssam  *
24924007Ssam  */
25025927Ssam acestart(unit)
25125927Ssam 	int unit;
25224007Ssam {
25324007Ssam 	register struct tx_segment *txs;
25425694Ssam 	register long len;
25525694Ssam 	register int s;
25624007Ssam 	register struct ace_softc *is = &ace_softc[unit];
25724007Ssam 	struct mbuf *m;
25825694Ssam 	short retries;
25924007Ssam 
26025927Ssam 	if (is->is_flags & ACEF_OACTIVE)
26125927Ssam 		return;
26225927Ssam 	is->is_flags |= ACEF_OACTIVE;
26324007Ssam again:
26424007Ssam 	txs = (struct tx_segment*)(is->is_dpm + (is->is_txnext << 11));
26524007Ssam 	if (txs->tx_csr & TCS_TBFULL) {
26624007Ssam 		is->is_stats.tx_busy++;
26725927Ssam 		is->is_flags &= ~ACEF_OACTIVE;
26824007Ssam 		return;
26924007Ssam 	}
27025694Ssam 	s = splimp();
27124007Ssam 	IF_DEQUEUE(&is->is_if.if_snd, m);
27225694Ssam 	splx(s);
27325927Ssam 	if (m == 0) {
27425927Ssam 		is->is_flags &= ~ACEF_OACTIVE;
27524007Ssam 		return;
27625927Ssam 	}
27724007Ssam 	len = aceput(unit, txs->tx_data, m);
27824007Ssam 	retries = txs->tx_csr & TCS_RTC;
27924007Ssam 	if (retries > 0)
28024007Ssam 		acebakoff(is, txs, retries);
28124007Ssam 
28224007Ssam 	/*
28324007Ssam 	 * Ensure minimum packet length.
28424007Ssam 	 * This makes the safe assumtion that there are no virtual holes
28524007Ssam 	 * after the data.
28624007Ssam 	 * For security, it might be wise to zero out the added bytes,
28724007Ssam 	 * but we're mainly interested in speed at the moment.
28824007Ssam 	 */
28924007Ssam 	if (len - sizeof (struct ether_header) < ETHERMIN)
29024007Ssam 		len = ETHERMIN + sizeof (struct ether_header);
29124007Ssam 	if (++is->is_txnext > SEG_MAX)
29224007Ssam 		is->is_txnext = is->is_segboundry;
29324007Ssam 	is->is_if.if_opackets++;
29424007Ssam 	is->is_xcnt++;
29524007Ssam 	len = (len & 0x7fff) | TCS_TBFULL;
29625694Ssam 	movow(txs, len);
29724007Ssam 	goto again;
29824007Ssam }
29924007Ssam 
30024007Ssam /*
30124007Ssam  * Transmit done interrupt.
30224007Ssam  */
30324007Ssam acecint(unit)
30424007Ssam 	int unit;
30524007Ssam {
30624007Ssam 	register struct ace_softc *is = &ace_softc[unit];
30724007Ssam 	register struct tx_segment *txseg;
30825694Ssam 	short eostat;
30924007Ssam 
31024007Ssam 	if (is->is_xcnt <= 0)  {
31125855Ssam 		log(LOG_ERR, "ace%d: stray xmit interrupt, xcnt %d\n",
31224007Ssam 		    unit, is->is_xcnt);
31324007Ssam 		is->is_xcnt = 0;
31425694Ssam 		if (is->is_if.if_snd.ifq_head)
31525927Ssam 			acestart(unit);
31624007Ssam 		return;
31724007Ssam 	}
31824007Ssam 	is->is_xcnt--;
31924007Ssam 	txseg = (struct tx_segment *)((is->is_eoctr << 11) + is->is_dpm);
32024007Ssam 	eostat = txseg->tx_csr;
32124007Ssam 	if ((eostat & TCS_TBFULL) == 0) {
32224007Ssam 		is->is_stats.tx_retries += eostat & TCS_RTC;
32324007Ssam 		if (eostat & TCS_RTFAIL)  {
32424007Ssam 			is->is_stats.tx_discarded++;
32524007Ssam 			is->is_if.if_oerrors++;
32624007Ssam 		} else
32724007Ssam 			is->is_stats.tx_datagrams++;
32824007Ssam 		if (++is->is_eoctr >= 16)
32924007Ssam 			is->is_eoctr = is->is_segboundry;
33024007Ssam 	}
33125694Ssam 	if (is->is_if.if_snd.ifq_head)
33225927Ssam 		acestart(unit);
33324007Ssam }
33424007Ssam 
33524007Ssam /*
33624007Ssam  * Ethernet interface receiver interrupt.
33724007Ssam  * If input error just drop packet.
33824007Ssam  * Otherwise purge input buffered data path and examine
33924007Ssam  * packet to determine type.  If can't determine length
34024007Ssam  * from type, then have to drop packet.  Othewise decapsulate
34124007Ssam  * packet based on type and pass to type specific higher-level
34224007Ssam  * input routine.
34324007Ssam  */
34424007Ssam acerint(unit)
34524007Ssam 	int unit;
34624007Ssam {
34724007Ssam 	register struct ace_softc *is = &ace_softc[unit];
34824007Ssam 	register struct ifqueue *inq;
34924007Ssam 	register struct ether_header *ace;
35024007Ssam 	register struct rx_segment *rxseg;
35124007Ssam 	int len, s, off, resid;
35224007Ssam 	struct mbuf *m;
35324007Ssam 	short eistat;
35424007Ssam 
35525694Ssam 	if ((is->is_if.if_flags&IFF_RUNNING) == 0)
35625694Ssam 		return;
35724007Ssam again:
35824007Ssam 	rxseg = (struct rx_segment *)((is->is_eictr << 11) + is->is_dpm);
35924007Ssam 	eistat = rxseg->rx_csr;
36024007Ssam 	if ((eistat & RCS_RBFULL) == 0)
36124007Ssam 		return;
36224007Ssam 	is->is_if.if_ipackets++;
36324007Ssam 	if (++is->is_eictr >= is->is_segboundry)
36424007Ssam 		is->is_eictr = 0;
36524007Ssam 	len = eistat & RCS_RBC;
36624007Ssam 	if ((eistat & (RCS_ROVRN | RCS_RCRC | RCS_RODD)) ||
36724007Ssam 	    len < ET_MINLEN || len > ET_MAXLEN+CRC_SIZE) {
36824007Ssam 		if (eistat & RCS_ROVRN)
36924007Ssam 			is->is_stats.rx_overruns++;
37024007Ssam 		if (eistat & RCS_RCRC)
37124007Ssam 			is->is_stats.rx_crc_errors++;
37224007Ssam 		if (eistat & RCS_RODD)
37324007Ssam 			is->is_stats.rx_align_errors++;
37424007Ssam 		if (len < ET_MINLEN)
37524007Ssam 			is->is_stats.rx_underruns++;
37624007Ssam 		if (len > ET_MAXLEN+CRC_SIZE)
37724007Ssam 			is->is_stats.rx_overruns++;
37824007Ssam 		is->is_if.if_ierrors++;
37924007Ssam 		rxseg->rx_csr = 0;
38024007Ssam 		return;
38124007Ssam 	} else
38224007Ssam 		is->is_stats.rx_datagrams++;
38324007Ssam 	ace = (struct ether_header *)rxseg->rx_data;
38424007Ssam 	len -= sizeof (struct ether_header);
38524007Ssam 	/*
38625694Ssam 	 * Deal with trailer protocol: if type is trailer
38724007Ssam 	 * get true type from first 16-bit word past data.
38824007Ssam 	 * Remember that type was trailer by setting off.
38924007Ssam 	 */
39024007Ssam 	ace->ether_type = ntohs((u_short)ace->ether_type);
39124007Ssam #define	acedataaddr(ace, off, type) \
39224007Ssam     ((type)(((caddr_t)(((char *)ace)+sizeof (struct ether_header))+(off))))
39325694Ssam 	if (ace->ether_type >= ETHERTYPE_TRAIL &&
39425694Ssam 	    ace->ether_type < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
39525694Ssam 		off = (ace->ether_type - ETHERTYPE_TRAIL) * 512;
39624007Ssam 		if (off >= ETHERMTU)
39724007Ssam 			goto setup;		/* sanity */
39824007Ssam 		ace->ether_type = ntohs(*acedataaddr(ace, off, u_short *));
39924007Ssam 		resid = ntohs(*(acedataaddr(ace, off+2, u_short *)));
40024007Ssam 		if (off + resid > len)
40124007Ssam 			goto setup;		/* sanity */
40224007Ssam 		len = off + resid;
40324007Ssam 	} else
40424007Ssam 		off = 0;
40524007Ssam 	if (len == 0)
40624007Ssam 		goto setup;
40724007Ssam 
40824007Ssam 	/*
40924007Ssam 	 * Pull packet off interface.  Off is nonzero if packet
41024007Ssam 	 * has trailing header; aceget will then force this header
41124007Ssam 	 * information to be at the front, but we still have to drop
41224007Ssam 	 * the type and length which are at the front of any trailer data.
41324007Ssam 	 */
41425855Ssam 	m = aceget((u_char *)rxseg->rx_data, len, off, &is->is_if);
41524007Ssam 	if (m == 0)
41624007Ssam 		goto setup;
41724007Ssam 	if (off) {
41825855Ssam 		struct ifnet *ifp;
41925855Ssam 
42025855Ssam 		ifp = *(mtod(m, struct ifnet **));
42124007Ssam 		m->m_off += 2 * sizeof (u_short);
42224007Ssam 		m->m_len -= 2 * sizeof (u_short);
42325855Ssam 		*(mtod(m, struct ifnet **)) = ifp;
42424007Ssam 	}
42524007Ssam 	switch (ace->ether_type) {
42624007Ssam 
42724007Ssam #ifdef INET
42825694Ssam 	case ETHERTYPE_IP:
42924007Ssam 		schednetisr(NETISR_IP);
43024007Ssam 		inq = &ipintrq;
43124007Ssam 		break;
43225855Ssam #endif
43324007Ssam 
43425694Ssam 	case ETHERTYPE_ARP:
43524007Ssam 		arpinput(&is->is_ac, m);
43624007Ssam 		goto setup;
43725694Ssam #ifdef NS
43825694Ssam 	case ETHERTYPE_NS:
43925694Ssam 		schednetisr(NETISR_NS);
44025694Ssam 		inq = &nsintrq;
44125694Ssam 		break;
44225694Ssam 
44325694Ssam #endif
44424007Ssam 	default:
44524007Ssam 		m_freem(m);
44624007Ssam 		goto setup;
44724007Ssam 	}
44824007Ssam 	if (IF_QFULL(inq)) {
44924007Ssam 		IF_DROP(inq);
45024007Ssam 		m_freem(m);
45124007Ssam 		goto setup;
45224007Ssam 	}
45324007Ssam 	s = splimp();
45424007Ssam 	IF_ENQUEUE(inq, m);
45524007Ssam 	splx(s);
45624007Ssam setup:
45724007Ssam 	rxseg->rx_csr = 0;
45824007Ssam 	goto again;
45924007Ssam }
46024007Ssam 
46124007Ssam /*
46224007Ssam  * Ethernet output routine.
46324007Ssam  * Encapsulate a packet of type family for the local net.
46424007Ssam  * Use trailer local net encapsulation if enough data in first
46524007Ssam  * packet leaves a multiple of 512 bytes of data in remainder.
46624007Ssam  */
46724007Ssam aceoutput(ifp, m0, dst)
46824007Ssam 	struct ifnet *ifp;
46924007Ssam 	struct mbuf *m0;
47024007Ssam 	struct sockaddr *dst;
47124007Ssam {
47224007Ssam 	register struct ace_softc *is = &ace_softc[ifp->if_unit];
47324007Ssam 	register struct mbuf *m = m0;
47424007Ssam 	register struct ether_header *ace;
47524007Ssam 	register int off;
47624007Ssam 	struct mbuf *mcopy = (struct mbuf *)0;
47725957Ssam 	int type, s, error, usetrailers;
47825694Ssam 	u_char edst[6];
47924007Ssam 	struct in_addr idst;
48024007Ssam 
48125855Ssam 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
48225855Ssam 		error = ENETDOWN;
48325855Ssam 		goto bad;
48425855Ssam 	}
48524007Ssam 	switch (dst->sa_family) {
48624007Ssam 
48724007Ssam #ifdef INET
48824007Ssam 	case AF_INET:
48924007Ssam 		idst = ((struct sockaddr_in *)dst)->sin_addr;
49025957Ssam 		if (!arpresolve(&is->is_ac, m, &idst, edst, &usetrailers))
49124007Ssam 			return (0);	/* if not yet resolved */
49225694Ssam 		if (!bcmp((caddr_t)edst, (caddr_t)etherbroadcastaddr,
49325937Ssam 		    sizeof (edst)))
49424007Ssam 			mcopy = m_copy(m, 0, (int)M_COPYALL);
49524007Ssam 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
49624007Ssam 		/* need per host negotiation */
49725957Ssam 		if (usetrailers && off > 0 && (off & 0x1ff) == 0 &&
49824007Ssam 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
49925694Ssam 			type = ETHERTYPE_TRAIL + (off>>9);
50024007Ssam 			m->m_off -= 2 * sizeof (u_short);
50124007Ssam 			m->m_len += 2 * sizeof (u_short);
50225694Ssam 			*mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP);
50324007Ssam 			*(mtod(m, u_short *) + 1) = htons((u_short)m->m_len);
50424007Ssam 			goto gottrailertype;
50524007Ssam 		}
50625694Ssam 		type = ETHERTYPE_IP;
50724007Ssam 		off = 0;
50824007Ssam 		goto gottype;
50924007Ssam #endif
51025694Ssam #ifdef NS
51125694Ssam 	case AF_NS:
51225694Ssam  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
51325694Ssam 		    (caddr_t)edst, sizeof (edst));
51425694Ssam 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost,sizeof(edst)))
51525694Ssam 			mcopy = m_copy(m, 0, (int)M_COPYALL);
51625694Ssam 		else if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost,
51725694Ssam 		    sizeof(edst)))
51825694Ssam 			return(looutput(&loif, m, dst));
51925694Ssam 		type = ETHERTYPE_NS;
52025694Ssam 		off = 0;
52125694Ssam 		goto gottype;
52225694Ssam #endif
52324007Ssam 	case AF_UNSPEC:
52424007Ssam 		ace = (struct ether_header *)dst->sa_data;
52525694Ssam 		bcopy((caddr_t)ace->ether_dhost, (caddr_t)edst, sizeof (edst));
52624007Ssam 		type = ace->ether_type;
52724007Ssam 		goto gottype;
52824007Ssam 
52924007Ssam 	default:
53025855Ssam 		log(LOG_ERR, "ace%d: can't handle af%d\n",
53124007Ssam 		    ifp->if_unit, dst->sa_family);
53224007Ssam 		error = EAFNOSUPPORT;
53324007Ssam 		goto bad;
53424007Ssam 	}
53524007Ssam 
53624007Ssam gottrailertype:
53724007Ssam 	/*
53824007Ssam 	 * Packet to be sent as trailer: move first packet
53924007Ssam 	 * (control information) to end of chain.
54024007Ssam 	 */
54124007Ssam 	while (m->m_next)
54224007Ssam 		m = m->m_next;
54324007Ssam 	m->m_next = m0;
54424007Ssam 	m = m0->m_next;
54524007Ssam 	m0->m_next = 0;
54624007Ssam 	m0 = m;
54724007Ssam 
54824007Ssam gottype:
54924007Ssam 	/*
55024007Ssam 	 * Add local net header.  If no space in first mbuf,
55124007Ssam 	 * allocate another.
55224007Ssam 	 */
55324007Ssam 	if (m->m_off > MMAXOFF ||
55424007Ssam 	    MMINOFF + sizeof (struct ether_header) > m->m_off) {
55524007Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
55624007Ssam 		if (m == 0) {
55724007Ssam 			error = ENOBUFS;
55824007Ssam 			goto bad;
55924007Ssam 		}
56024007Ssam 		m->m_next = m0;
56124007Ssam 		m->m_off = MMINOFF;
56224007Ssam 		m->m_len = sizeof (struct ether_header);
56324007Ssam 	} else {
56424007Ssam 		m->m_off -= sizeof (struct ether_header);
56524007Ssam 		m->m_len += sizeof (struct ether_header);
56624007Ssam 	}
56724007Ssam 	ace = mtod(m, struct ether_header *);
56825694Ssam 	bcopy((caddr_t)edst, (caddr_t)ace->ether_dhost, sizeof (edst));
56925694Ssam 	bcopy((caddr_t)is->is_addr, (caddr_t)ace->ether_shost,
57025694Ssam 	    sizeof (is->is_addr));
57124007Ssam 	ace->ether_type = htons((u_short)type);
57224007Ssam 
57324007Ssam 	/*
57424007Ssam 	 * Queue message on interface, and start output if interface
57524007Ssam 	 * not yet active.
57624007Ssam 	 */
57724007Ssam 	s = splimp();
57824007Ssam 	if (IF_QFULL(&ifp->if_snd)) {
57924007Ssam 		IF_DROP(&ifp->if_snd);
58024007Ssam 		error = ENOBUFS;
58124007Ssam 		goto qfull;
58224007Ssam 	}
58324007Ssam 	IF_ENQUEUE(&ifp->if_snd, m);
58424007Ssam 	splx(s);
58525927Ssam 	acestart(ifp->if_unit);
58624007Ssam 	return (mcopy ? looutput(&loif, mcopy, dst) : 0);
58724007Ssam qfull:
58824007Ssam 	m0 = m;
58924007Ssam 	splx(s);
59024007Ssam bad:
59124007Ssam 	m_freem(m0);
59224007Ssam 	if (mcopy)
59324007Ssam 		m_freem(mcopy);
59424007Ssam 	return (error);
59524007Ssam }
59624007Ssam 
59724007Ssam /*
59824007Ssam  * Routine to copy from mbuf chain to transmit buffer on the VERSAbus
59924007Ssam  * If packet size is less than the minimum legal size,
60024007Ssam  * the buffer is expanded.  We probably should zero out the extra
60124007Ssam  * bytes for security, but that would slow things down.
60224007Ssam  */
60325694Ssam /*ARGSUSED*/
60424007Ssam aceput(unit, txbuf, m)
60525855Ssam 	int unit;
60625694Ssam 	char *txbuf;
60724007Ssam 	struct mbuf *m;
60824007Ssam {
60925855Ssam 	register u_char *bp, *mcp;
61025855Ssam 	register short *s1, *s2;
61125694Ssam 	register u_int len;
61224007Ssam 	register struct mbuf *mp;
61325694Ssam 	int total;
61424007Ssam 
61524007Ssam 	total = 0;
61625694Ssam 	bp = (u_char *)txbuf;
61725694Ssam 	for (mp = m; (mp); mp = mp->m_next) {
61824007Ssam 		len = mp->m_len;
61924007Ssam 		if (len == 0)
62024007Ssam 			continue;
62124007Ssam 		total += len;
62224007Ssam 		mcp = mtod(mp, u_char *);
62324007Ssam 		if (((int)mcp & 01) && ((int)bp & 01)) {
62424007Ssam 			/* source & destination at odd addresses */
62525694Ssam 			movob(bp++, *mcp++);
62624007Ssam 			--len;
62724007Ssam 		}
62824007Ssam 		if (len > 1 && (((int)mcp & 01)==0) && (((int)bp & 01)==0)) {
62925694Ssam 			register u_int l;
63024007Ssam 
63124007Ssam 			s1 = (short *)bp;
63224007Ssam 			s2 = (short *)mcp;
63324007Ssam 			l = len >> 1;		/* count # of shorts */
63425694Ssam 			while (l-- != 0)
63525694Ssam 				movow(s1++, *s2++);
63624007Ssam 			len &= 1;		/* # remaining bytes */
63724007Ssam 			bp = (u_char *)s1;
63824007Ssam 			mcp = (u_char *)s2;
63924007Ssam 		}
64025694Ssam 		while (len-- != 0)
64125694Ssam 			movob(bp++, *mcp++);
64224007Ssam 	}
64324007Ssam 	m_freem(m);
64424007Ssam 	return (total);
64524007Ssam }
64624007Ssam 
64724007Ssam /*
64824007Ssam  * Routine to copy from VERSAbus memory into mbufs.
64924007Ssam  *
65024007Ssam  * Warning: This makes the fairly safe assumption that
65124007Ssam  * mbufs have even lengths.
65224007Ssam  */
65325694Ssam /*ARGSUSED*/
65424007Ssam struct mbuf *
65525855Ssam aceget(rxbuf, totlen, off0, ifp)
65624007Ssam 	u_char *rxbuf;
65724007Ssam 	int totlen, off0;
65825855Ssam 	struct ifnet *ifp;
65924007Ssam {
66025855Ssam 	register u_char *cp, *mcp;
66124007Ssam 	register int tlen;
66224007Ssam 	register struct mbuf *m;
66324007Ssam 	struct mbuf *top = 0, **mp = &top;
66424007Ssam 	int len, off = off0;
66524007Ssam 
66624007Ssam 	cp = rxbuf + sizeof (struct ether_header);
66724007Ssam 	while (totlen > 0) {
66824007Ssam 		MGET(m, M_DONTWAIT, MT_DATA);
66924007Ssam 		if (m == 0)
67024007Ssam 			goto bad;
67124007Ssam 		if (off) {
67224007Ssam 			len = totlen - off;
67324007Ssam 			cp = rxbuf + sizeof (struct ether_header) + off;
67424007Ssam 		} else
67524007Ssam 			len = totlen;
67625855Ssam 		if (ifp)
67725855Ssam 			len += sizeof(ifp);
67824007Ssam 		if (len >= CLBYTES) {
67924007Ssam 			struct mbuf *p;
68024007Ssam 
68124007Ssam 			MCLGET(p, 1);
68224007Ssam 			if (p != 0) {
68324007Ssam 				m->m_len = len = CLBYTES;
68424007Ssam 				m->m_off = (int)p - (int)m;
68524007Ssam 			} else {
68624007Ssam 				m->m_len = len = MIN(MLEN, len);
68724007Ssam 				m->m_off = MMINOFF;
68824007Ssam 			}
68924007Ssam 		} else {
69024007Ssam 			m->m_len = len = MIN(MLEN, len);
69124007Ssam 			m->m_off = MMINOFF;
69224007Ssam 		}
69324007Ssam 		mcp = mtod(m, u_char *);
69425855Ssam 		if (ifp) {
69525855Ssam 			/*
69625855Ssam 			 * Prepend interface pointer to first mbuf.
69725855Ssam 			 */
69825855Ssam 			*(mtod(m, struct ifnet **)) = ifp;
69925855Ssam 			mcp += sizeof(ifp);
70025855Ssam 			len -= sizeof(ifp);
70125855Ssam 			ifp = (struct ifnet *)0;
70225855Ssam 		}
70324007Ssam 		/*bcopy((caddr_t)cp, (caddr_t)mcp, len);*/
70424007Ssam 		/*cp += len; mcp += len;*/
70524007Ssam 		tlen = len;
70624007Ssam 		if (((int)mcp & 01) && ((int)cp & 01)) {
70724007Ssam 			/* source & destination at odd addresses */
70824007Ssam 			*mcp++ = *cp++;
70924007Ssam 			--tlen;
71024007Ssam 		}
71124007Ssam 		if (tlen > 1 && (((int)mcp&01) == 0) && (((int)cp&01) == 0)) {
71224007Ssam 			register short *s1, *s2;
71324007Ssam 			register int l;
71424007Ssam 
71524007Ssam 			s1 = (short *)mcp;
71624007Ssam 			s2 = (short *)cp;
71724007Ssam 			l = tlen >> 1;		/* count # of shorts */
71824007Ssam 			while (l-- > 0)		/* copy shorts */
71924007Ssam 				*s1++ = *s2++;
72024007Ssam 			tlen &= 1;		/* # remaining bytes */
72124007Ssam 			mcp = (u_char *)s1;
72224007Ssam 			cp = (u_char *)s2;
72324007Ssam 		}
72424007Ssam 		while (tlen-- > 0)
72524007Ssam 			*mcp++ = *cp++;
72624007Ssam 		*mp = m;
72724007Ssam 		mp = &m->m_next;
72824007Ssam 		if (off == 0) {
72924007Ssam 			totlen -= len;
73024007Ssam 			continue;
73124007Ssam 		}
73224007Ssam 		off += len;
73324007Ssam 		if (off == totlen) {
73424007Ssam 			cp = rxbuf + sizeof (struct ether_header);
73524007Ssam 			off = 0;
73624007Ssam 			totlen = off0;
73724007Ssam 		}
73824007Ssam 	}
73924007Ssam 	return (top);
74024007Ssam bad:
74124007Ssam 	m_freem(top);
74224007Ssam 	return (0);
74324007Ssam }
74424007Ssam 
74524007Ssam acebakoff(is, txseg, retries)
74624007Ssam 	struct ace_softc *is;
74724007Ssam 	struct tx_segment *txseg;
74824007Ssam 	register int retries;
74924007Ssam {
75024007Ssam 	register short *pBakNum, random_num;
75124007Ssam 	short *pMask;
75224007Ssam 
75324007Ssam 	pMask = &random_mask_tbl[0];
75424007Ssam 	pBakNum = &txseg->tx_backoff[0];
75524007Ssam 	while (--retries >= 0) {
75624007Ssam 		random_num = (is->is_currnd = (is->is_currnd * 18741)-13849);
75724007Ssam 		random_num &= *pMask++;
75824007Ssam 		*pBakNum++ = random_num ^ (short)(0xFF00 | 0x00FC);
75924007Ssam 	}
76024007Ssam }
76124007Ssam 
76224007Ssam /*
76324007Ssam  * Process an ioctl request.
76424007Ssam  */
76524007Ssam aceioctl(ifp, cmd, data)
76624007Ssam 	register struct ifnet *ifp;
76724007Ssam 	int cmd;
76824007Ssam 	caddr_t data;
76924007Ssam {
77025694Ssam 	register struct ifaddr *ifa = (struct ifaddr *)data;
77125855Ssam 	struct acedevice *addr;
77225694Ssam 	int s = splimp(), error = 0;
77324007Ssam 
77424007Ssam 	switch (cmd) {
77524007Ssam 
77624007Ssam 	case SIOCSIFADDR:
77725694Ssam 		ifp->if_flags |= IFF_UP;
77825855Ssam 		switch (ifa->ifa_addr.sa_family) {
77925855Ssam #ifdef INET
78025855Ssam 		case AF_INET:
78125855Ssam 			aceinit(ifp->if_unit);	/* before arpwhohas */
78225855Ssam 			((struct arpcom *)ifp)->ac_ipaddr =
78325855Ssam 				IA_SIN(ifa)->sin_addr;
78425855Ssam 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
78525855Ssam 			break;
78625855Ssam #endif
78725855Ssam #ifdef NS
78825855Ssam 		case AF_NS: {
78925937Ssam 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
79025937Ssam 			struct ace_softc *is = &ace_softc[ifp->if_unit];
79125855Ssam 
79225855Ssam 			if (!ns_nullhost(*ina)) {
79325855Ssam 				ifp->if_flags &= ~IFF_RUNNING;
79425855Ssam 				addr = (struct acedevice *)
79525937Ssam 				    aceinfo[ifp->if_unit]->ui_addr;
79625855Ssam 				movow(&addr->csr, CSR_RESET);
79725855Ssam 				DELAY(10000);
79825855Ssam 				/* set station address & copy addr to arp */
79925855Ssam 				acesetetaddr(ifp->if_unit, addr,
80025855Ssam 				    ina->x_host.c_host);
80125855Ssam 			} else
80225937Ssam 				ina->x_host = *(union ns_host *)is->is_addr;
80325855Ssam 			aceinit(ifp->if_unit);
80425855Ssam 			break;
80525855Ssam 		}
80625855Ssam #endif
80725855Ssam 		default:
80825855Ssam 			aceinit(ifp->if_unit);
80925855Ssam 			break;
81025855Ssam 		}
81124007Ssam 		break;
81224007Ssam 
81325855Ssam 	case SIOCSIFFLAGS:
81425855Ssam 		if ((ifp->if_flags&IFF_UP) == 0 && ifp->if_flags&IFF_RUNNING) {
81525855Ssam 			addr = (struct acedevice *)
81625855Ssam 			    (aceinfo[ifp->if_unit]->ui_addr);
81725855Ssam 			movow(&addr->csr, CSR_RESET);
81825855Ssam 			ifp->if_flags &= ~IFF_RUNNING;
81925855Ssam 		} else if (ifp->if_flags&IFF_UP &&
82025855Ssam 		    (ifp->if_flags&IFF_RUNNING) == 0)
82125855Ssam 			aceinit(ifp->if_unit);
82224007Ssam 		break;
82324007Ssam 
82424007Ssam 	default:
82524007Ssam 		error = EINVAL;
82624007Ssam 	}
82724007Ssam 	splx(s);
82824007Ssam 	return (error);
82924007Ssam }
83024007Ssam 
83125927Ssam acesetup(unit)
83224007Ssam 	int unit;
83324007Ssam {
83424007Ssam 	register struct ace_softc *is = &ace_softc[unit];
83525927Ssam 	register char *pData1;
83625694Ssam 	register short i;
83725927Ssam 	struct acedevice *addr;
83824007Ssam 
83925927Ssam 	bzero(is->is_dpm, 16384*2);
84024007Ssam 	is->is_currnd = 49123;
84125927Ssam 	addr = (struct acedevice *)aceinfo[unit]->ui_addr;
84224007Ssam 	is->is_segboundry = (addr->segb >> 11) & 0xf;
84325927Ssam 	pData1 = is->is_dpm + (is->is_segboundry << 11);
84424007Ssam 	for (i = SEG_MAX + 1 - is->is_segboundry; --i >= 0;) {
84524007Ssam 		acebakoff(is, (struct tx_segment *)pData1, 15);
84624007Ssam 		pData1 += sizeof (struct tx_segment);
84724007Ssam 	}
84824007Ssam 	is->is_eictr = 0;
84924007Ssam 	is->is_eoctr = is->is_txnext = is->is_segboundry;
85024007Ssam 	bzero((char *)&is->is_stats, sizeof (is->is_stats));
85124007Ssam }
85224007Ssam #endif
853