xref: /csrg-svn/sys/tahoe/if/if_ace.c (revision 25937)
1*25937Ssam /*	if_ace.c	1.6	86/01/21	*/
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];
5525855Ssam long	acestd[] = { 0x0ff0000, 0xff0100, 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;
47724007Ssam 	int type, s, error;
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;
49025694Ssam 		if (!arpresolve(&is->is_ac, m, &idst, edst))
49124007Ssam 			return (0);	/* if not yet resolved */
49225694Ssam 		if (!bcmp((caddr_t)edst, (caddr_t)etherbroadcastaddr,
493*25937Ssam 		    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 */
49724007Ssam 		if ((ifp->if_flags & IFF_NOTRAILERS) == 0 && off > 0 &&
49824007Ssam 		    (off & 0x1ff) == 0 &&
49924007Ssam 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
50025694Ssam 			type = ETHERTYPE_TRAIL + (off>>9);
50124007Ssam 			m->m_off -= 2 * sizeof (u_short);
50224007Ssam 			m->m_len += 2 * sizeof (u_short);
50325694Ssam 			*mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP);
50424007Ssam 			*(mtod(m, u_short *) + 1) = htons((u_short)m->m_len);
50524007Ssam 			goto gottrailertype;
50624007Ssam 		}
50725694Ssam 		type = ETHERTYPE_IP;
50824007Ssam 		off = 0;
50924007Ssam 		goto gottype;
51024007Ssam #endif
51125694Ssam #ifdef NS
51225694Ssam 	case AF_NS:
51325694Ssam  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
51425694Ssam 		    (caddr_t)edst, sizeof (edst));
51525694Ssam 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost,sizeof(edst)))
51625694Ssam 			mcopy = m_copy(m, 0, (int)M_COPYALL);
51725694Ssam 		else if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost,
51825694Ssam 		    sizeof(edst)))
51925694Ssam 			return(looutput(&loif, m, dst));
52025694Ssam 		type = ETHERTYPE_NS;
52125694Ssam 		off = 0;
52225694Ssam 		goto gottype;
52325694Ssam #endif
52424007Ssam 	case AF_UNSPEC:
52524007Ssam 		ace = (struct ether_header *)dst->sa_data;
52625694Ssam 		bcopy((caddr_t)ace->ether_dhost, (caddr_t)edst, sizeof (edst));
52724007Ssam 		type = ace->ether_type;
52824007Ssam 		goto gottype;
52924007Ssam 
53024007Ssam 	default:
53125855Ssam 		log(LOG_ERR, "ace%d: can't handle af%d\n",
53224007Ssam 		    ifp->if_unit, dst->sa_family);
53324007Ssam 		error = EAFNOSUPPORT;
53424007Ssam 		goto bad;
53524007Ssam 	}
53624007Ssam 
53724007Ssam gottrailertype:
53824007Ssam 	/*
53924007Ssam 	 * Packet to be sent as trailer: move first packet
54024007Ssam 	 * (control information) to end of chain.
54124007Ssam 	 */
54224007Ssam 	while (m->m_next)
54324007Ssam 		m = m->m_next;
54424007Ssam 	m->m_next = m0;
54524007Ssam 	m = m0->m_next;
54624007Ssam 	m0->m_next = 0;
54724007Ssam 	m0 = m;
54824007Ssam 
54924007Ssam gottype:
55024007Ssam 	/*
55124007Ssam 	 * Add local net header.  If no space in first mbuf,
55224007Ssam 	 * allocate another.
55324007Ssam 	 */
55424007Ssam 	if (m->m_off > MMAXOFF ||
55524007Ssam 	    MMINOFF + sizeof (struct ether_header) > m->m_off) {
55624007Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
55724007Ssam 		if (m == 0) {
55824007Ssam 			error = ENOBUFS;
55924007Ssam 			goto bad;
56024007Ssam 		}
56124007Ssam 		m->m_next = m0;
56224007Ssam 		m->m_off = MMINOFF;
56324007Ssam 		m->m_len = sizeof (struct ether_header);
56424007Ssam 	} else {
56524007Ssam 		m->m_off -= sizeof (struct ether_header);
56624007Ssam 		m->m_len += sizeof (struct ether_header);
56724007Ssam 	}
56824007Ssam 	ace = mtod(m, struct ether_header *);
56925694Ssam 	bcopy((caddr_t)edst, (caddr_t)ace->ether_dhost, sizeof (edst));
57025694Ssam 	bcopy((caddr_t)is->is_addr, (caddr_t)ace->ether_shost,
57125694Ssam 	    sizeof (is->is_addr));
57224007Ssam 	ace->ether_type = htons((u_short)type);
57324007Ssam 
57424007Ssam 	/*
57524007Ssam 	 * Queue message on interface, and start output if interface
57624007Ssam 	 * not yet active.
57724007Ssam 	 */
57824007Ssam 	s = splimp();
57924007Ssam 	if (IF_QFULL(&ifp->if_snd)) {
58024007Ssam 		IF_DROP(&ifp->if_snd);
58124007Ssam 		error = ENOBUFS;
58224007Ssam 		goto qfull;
58324007Ssam 	}
58424007Ssam 	IF_ENQUEUE(&ifp->if_snd, m);
58524007Ssam 	splx(s);
58625927Ssam 	acestart(ifp->if_unit);
58724007Ssam 	return (mcopy ? looutput(&loif, mcopy, dst) : 0);
58824007Ssam qfull:
58924007Ssam 	m0 = m;
59024007Ssam 	splx(s);
59124007Ssam bad:
59224007Ssam 	m_freem(m0);
59324007Ssam 	if (mcopy)
59424007Ssam 		m_freem(mcopy);
59524007Ssam 	return (error);
59624007Ssam }
59724007Ssam 
59824007Ssam /*
59924007Ssam  * Routine to copy from mbuf chain to transmit buffer on the VERSAbus
60024007Ssam  * If packet size is less than the minimum legal size,
60124007Ssam  * the buffer is expanded.  We probably should zero out the extra
60224007Ssam  * bytes for security, but that would slow things down.
60324007Ssam  */
60425694Ssam /*ARGSUSED*/
60524007Ssam aceput(unit, txbuf, m)
60625855Ssam 	int unit;
60725694Ssam 	char *txbuf;
60824007Ssam 	struct mbuf *m;
60924007Ssam {
61025855Ssam 	register u_char *bp, *mcp;
61125855Ssam 	register short *s1, *s2;
61225694Ssam 	register u_int len;
61324007Ssam 	register struct mbuf *mp;
61425694Ssam 	int total;
61524007Ssam 
61624007Ssam 	total = 0;
61725694Ssam 	bp = (u_char *)txbuf;
61825694Ssam 	for (mp = m; (mp); mp = mp->m_next) {
61924007Ssam 		len = mp->m_len;
62024007Ssam 		if (len == 0)
62124007Ssam 			continue;
62224007Ssam 		total += len;
62324007Ssam 		mcp = mtod(mp, u_char *);
62424007Ssam 		if (((int)mcp & 01) && ((int)bp & 01)) {
62524007Ssam 			/* source & destination at odd addresses */
62625694Ssam 			movob(bp++, *mcp++);
62724007Ssam 			--len;
62824007Ssam 		}
62924007Ssam 		if (len > 1 && (((int)mcp & 01)==0) && (((int)bp & 01)==0)) {
63025694Ssam 			register u_int l;
63124007Ssam 
63224007Ssam 			s1 = (short *)bp;
63324007Ssam 			s2 = (short *)mcp;
63424007Ssam 			l = len >> 1;		/* count # of shorts */
63525694Ssam 			while (l-- != 0)
63625694Ssam 				movow(s1++, *s2++);
63724007Ssam 			len &= 1;		/* # remaining bytes */
63824007Ssam 			bp = (u_char *)s1;
63924007Ssam 			mcp = (u_char *)s2;
64024007Ssam 		}
64125694Ssam 		while (len-- != 0)
64225694Ssam 			movob(bp++, *mcp++);
64324007Ssam 	}
64424007Ssam 	m_freem(m);
64524007Ssam 	return (total);
64624007Ssam }
64724007Ssam 
64824007Ssam /*
64924007Ssam  * Routine to copy from VERSAbus memory into mbufs.
65024007Ssam  *
65124007Ssam  * Warning: This makes the fairly safe assumption that
65224007Ssam  * mbufs have even lengths.
65324007Ssam  */
65425694Ssam /*ARGSUSED*/
65524007Ssam struct mbuf *
65625855Ssam aceget(rxbuf, totlen, off0, ifp)
65724007Ssam 	u_char *rxbuf;
65824007Ssam 	int totlen, off0;
65925855Ssam 	struct ifnet *ifp;
66024007Ssam {
66125855Ssam 	register u_char *cp, *mcp;
66224007Ssam 	register int tlen;
66324007Ssam 	register struct mbuf *m;
66424007Ssam 	struct mbuf *top = 0, **mp = &top;
66524007Ssam 	int len, off = off0;
66624007Ssam 
66724007Ssam 	cp = rxbuf + sizeof (struct ether_header);
66824007Ssam 	while (totlen > 0) {
66924007Ssam 		MGET(m, M_DONTWAIT, MT_DATA);
67024007Ssam 		if (m == 0)
67124007Ssam 			goto bad;
67224007Ssam 		if (off) {
67324007Ssam 			len = totlen - off;
67424007Ssam 			cp = rxbuf + sizeof (struct ether_header) + off;
67524007Ssam 		} else
67624007Ssam 			len = totlen;
67725855Ssam 		if (ifp)
67825855Ssam 			len += sizeof(ifp);
67924007Ssam 		if (len >= CLBYTES) {
68024007Ssam 			struct mbuf *p;
68124007Ssam 
68224007Ssam 			MCLGET(p, 1);
68324007Ssam 			if (p != 0) {
68424007Ssam 				m->m_len = len = CLBYTES;
68524007Ssam 				m->m_off = (int)p - (int)m;
68624007Ssam 			} else {
68724007Ssam 				m->m_len = len = MIN(MLEN, len);
68824007Ssam 				m->m_off = MMINOFF;
68924007Ssam 			}
69024007Ssam 		} else {
69124007Ssam 			m->m_len = len = MIN(MLEN, len);
69224007Ssam 			m->m_off = MMINOFF;
69324007Ssam 		}
69424007Ssam 		mcp = mtod(m, u_char *);
69525855Ssam 		if (ifp) {
69625855Ssam 			/*
69725855Ssam 			 * Prepend interface pointer to first mbuf.
69825855Ssam 			 */
69925855Ssam 			*(mtod(m, struct ifnet **)) = ifp;
70025855Ssam 			mcp += sizeof(ifp);
70125855Ssam 			len -= sizeof(ifp);
70225855Ssam 			ifp = (struct ifnet *)0;
70325855Ssam 		}
70424007Ssam 		/*bcopy((caddr_t)cp, (caddr_t)mcp, len);*/
70524007Ssam 		/*cp += len; mcp += len;*/
70624007Ssam 		tlen = len;
70724007Ssam 		if (((int)mcp & 01) && ((int)cp & 01)) {
70824007Ssam 			/* source & destination at odd addresses */
70924007Ssam 			*mcp++ = *cp++;
71024007Ssam 			--tlen;
71124007Ssam 		}
71224007Ssam 		if (tlen > 1 && (((int)mcp&01) == 0) && (((int)cp&01) == 0)) {
71324007Ssam 			register short *s1, *s2;
71424007Ssam 			register int l;
71524007Ssam 
71624007Ssam 			s1 = (short *)mcp;
71724007Ssam 			s2 = (short *)cp;
71824007Ssam 			l = tlen >> 1;		/* count # of shorts */
71924007Ssam 			while (l-- > 0)		/* copy shorts */
72024007Ssam 				*s1++ = *s2++;
72124007Ssam 			tlen &= 1;		/* # remaining bytes */
72224007Ssam 			mcp = (u_char *)s1;
72324007Ssam 			cp = (u_char *)s2;
72424007Ssam 		}
72524007Ssam 		while (tlen-- > 0)
72624007Ssam 			*mcp++ = *cp++;
72724007Ssam 		*mp = m;
72824007Ssam 		mp = &m->m_next;
72924007Ssam 		if (off == 0) {
73024007Ssam 			totlen -= len;
73124007Ssam 			continue;
73224007Ssam 		}
73324007Ssam 		off += len;
73424007Ssam 		if (off == totlen) {
73524007Ssam 			cp = rxbuf + sizeof (struct ether_header);
73624007Ssam 			off = 0;
73724007Ssam 			totlen = off0;
73824007Ssam 		}
73924007Ssam 	}
74024007Ssam 	return (top);
74124007Ssam bad:
74224007Ssam 	m_freem(top);
74324007Ssam 	return (0);
74424007Ssam }
74524007Ssam 
74624007Ssam acebakoff(is, txseg, retries)
74724007Ssam 	struct ace_softc *is;
74824007Ssam 	struct tx_segment *txseg;
74924007Ssam 	register int retries;
75024007Ssam {
75124007Ssam 	register short *pBakNum, random_num;
75224007Ssam 	short *pMask;
75324007Ssam 
75424007Ssam 	pMask = &random_mask_tbl[0];
75524007Ssam 	pBakNum = &txseg->tx_backoff[0];
75624007Ssam 	while (--retries >= 0) {
75724007Ssam 		random_num = (is->is_currnd = (is->is_currnd * 18741)-13849);
75824007Ssam 		random_num &= *pMask++;
75924007Ssam 		*pBakNum++ = random_num ^ (short)(0xFF00 | 0x00FC);
76024007Ssam 	}
76124007Ssam }
76224007Ssam 
76324007Ssam /*
76424007Ssam  * Process an ioctl request.
76524007Ssam  */
76624007Ssam aceioctl(ifp, cmd, data)
76724007Ssam 	register struct ifnet *ifp;
76824007Ssam 	int cmd;
76924007Ssam 	caddr_t data;
77024007Ssam {
77125694Ssam 	register struct ifaddr *ifa = (struct ifaddr *)data;
77225855Ssam 	struct acedevice *addr;
77325694Ssam 	int s = splimp(), error = 0;
77424007Ssam 
77524007Ssam 	switch (cmd) {
77624007Ssam 
77724007Ssam 	case SIOCSIFADDR:
77825694Ssam 		ifp->if_flags |= IFF_UP;
77925855Ssam 		switch (ifa->ifa_addr.sa_family) {
78025855Ssam #ifdef INET
78125855Ssam 		case AF_INET:
78225855Ssam 			aceinit(ifp->if_unit);	/* before arpwhohas */
78325855Ssam 			((struct arpcom *)ifp)->ac_ipaddr =
78425855Ssam 				IA_SIN(ifa)->sin_addr;
78525855Ssam 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
78625855Ssam 			break;
78725855Ssam #endif
78825855Ssam #ifdef NS
78925855Ssam 		case AF_NS: {
790*25937Ssam 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
791*25937Ssam 			struct ace_softc *is = &ace_softc[ifp->if_unit];
79225855Ssam 
79325855Ssam 			if (!ns_nullhost(*ina)) {
79425855Ssam 				ifp->if_flags &= ~IFF_RUNNING;
79525855Ssam 				addr = (struct acedevice *)
796*25937Ssam 				    aceinfo[ifp->if_unit]->ui_addr;
79725855Ssam 				movow(&addr->csr, CSR_RESET);
79825855Ssam 				DELAY(10000);
79925855Ssam 				/* set station address & copy addr to arp */
80025855Ssam 				acesetetaddr(ifp->if_unit, addr,
80125855Ssam 				    ina->x_host.c_host);
80225855Ssam 			} else
803*25937Ssam 				ina->x_host = *(union ns_host *)is->is_addr;
80425855Ssam 			aceinit(ifp->if_unit);
80525855Ssam 			break;
80625855Ssam 		}
80725855Ssam #endif
80825855Ssam 		default:
80925855Ssam 			aceinit(ifp->if_unit);
81025855Ssam 			break;
81125855Ssam 		}
81224007Ssam 		break;
81324007Ssam 
81425855Ssam 	case SIOCSIFFLAGS:
81525855Ssam 		if ((ifp->if_flags&IFF_UP) == 0 && ifp->if_flags&IFF_RUNNING) {
81625855Ssam 			addr = (struct acedevice *)
81725855Ssam 			    (aceinfo[ifp->if_unit]->ui_addr);
81825855Ssam 			movow(&addr->csr, CSR_RESET);
81925855Ssam 			ifp->if_flags &= ~IFF_RUNNING;
82025855Ssam 		} else if (ifp->if_flags&IFF_UP &&
82125855Ssam 		    (ifp->if_flags&IFF_RUNNING) == 0)
82225855Ssam 			aceinit(ifp->if_unit);
82324007Ssam 		break;
82424007Ssam 
82524007Ssam 	default:
82624007Ssam 		error = EINVAL;
82724007Ssam 	}
82824007Ssam 	splx(s);
82924007Ssam 	return (error);
83024007Ssam }
83124007Ssam 
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