xref: /csrg-svn/sys/tahoe/if/if_ace.c (revision 30229)
1*30229Ssam /*	if_ace.c	1.11	86/11/29	*/
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 
37*30229Ssam #include "../machine/cpu.h"
38*30229Ssam #include "../machine/pte.h"
39*30229Ssam 
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
9124007Ssam 	acerint(0); acecint(0);
9224007Ssam #endif
9324007Ssam 	if (badaddr(reg, 2))
9425855Ssam 		return (0);
9525855Ssam 	movow(&ap->csr, CSR_RESET);
9624007Ssam 	DELAY(10000);
9725855Ssam #ifdef notdef
9825855Ssam 	/*
9925855Ssam 	 * Select two spaces for the interrupts aligned to an
10025855Ssam 	 * eight vector boundary and fitting in 8 bits (as
10125855Ssam 	 * required by the controller) -- YECH.  The controller
10225855Ssam 	 * will be notified later at initialization time.
10325855Ssam 	 */
10425855Ssam 	if ((vi->ui_hd->vh_lastiv -= 2) > 0xff)
10525855Ssam 		vi->ui_hd->vh_lastiv  = 0x200;
10625855Ssam 	is->is_ivec = vi->ui_hd->vh_lastiv = vi->ui_hd->vh_lastiv &~ 0x7;
10725855Ssam #else
10825855Ssam 	is->is_ivec = 0x90+vi->ui_unit*8;
10925855Ssam #endif
11025855Ssam 	br = 0x14, cvec = is->is_ivec;		/* XXX */
11125855Ssam 	return (sizeof (*ap));
11224007Ssam }
11324007Ssam 
11424007Ssam /*
11524007Ssam  * Interface exists: make available by filling in network interface
11624007Ssam  * record.  System will initialize the interface when it is ready
11724007Ssam  * to accept packets.
11824007Ssam  */
11924007Ssam aceattach(ui)
12024007Ssam 	struct vba_device *ui;
12124007Ssam {
12224007Ssam 	register short unit = ui->ui_unit;
12324007Ssam 	register struct ace_softc *is = &ace_softc[unit];
12424007Ssam 	register struct ifnet *ifp = &is->is_if;
12524007Ssam 	register struct acedevice *addr = (struct acedevice *)ui->ui_addr;
12624007Ssam 	register short *wp, i;
12729408Ssam 	char *cp;
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 	 */
14725927Ssam 	vbmemalloc(32, (caddr_t)ui->ui_flags, &is->is_map, &is->is_dpm);
14825927Ssam 
14924007Ssam 	ifp->if_init = aceinit;
15024007Ssam 	ifp->if_output = aceoutput;
15124007Ssam 	ifp->if_ioctl = aceioctl;
15224007Ssam 	ifp->if_reset = acereset;
15325694Ssam 	ifp->if_flags = IFF_BROADCAST;
15424007Ssam 	if_attach(ifp);
15524007Ssam }
15624007Ssam 
15724007Ssam /*
15824007Ssam  * Reset of interface after "system" reset.
15924007Ssam  */
16024007Ssam acereset(unit, vban)
16124007Ssam 	int unit, vban;
16224007Ssam {
16324007Ssam 	register struct vba_device *ui;
16424007Ssam 
16524007Ssam 	if (unit >= NACE || (ui = aceinfo[unit]) == 0 || ui->ui_alive == 0 ||
16624007Ssam 	    ui->ui_vbanum != vban)
16724007Ssam 		return;
16824007Ssam 	printf(" ace%d", unit);
16924007Ssam 	aceinit(unit);
17024007Ssam }
17124007Ssam 
17224007Ssam /*
17324007Ssam  * Initialization of interface; clear recorded pending operations
17424007Ssam  */
17524007Ssam aceinit(unit)
17624007Ssam 	int unit;
17724007Ssam {
17824007Ssam 	register struct ace_softc *is = &ace_softc[unit];
17924007Ssam 	register struct vba_device *ui = aceinfo[unit];
18024007Ssam 	register struct acedevice *addr;
18124007Ssam 	register struct ifnet *ifp = &is->is_if;
18224007Ssam 	register short Csr;
18325694Ssam 	register int s;
18424007Ssam 
18525694Ssam 	if (ifp->if_addrlist == (struct ifaddr *)0)
18624007Ssam 		return;
18724007Ssam 	if ((ifp->if_flags & IFF_RUNNING) == 0) {
18824007Ssam 		/*
18924007Ssam 		 * Reset the controller, initialize the recieve buffers,
19024007Ssam 		 * and turn the controller on again and set board online.
19124007Ssam 		 */
19224007Ssam 		addr = (struct acedevice *)ui->ui_addr;
19324007Ssam 		s = splimp();
19425694Ssam 		movow(&addr->csr, CSR_RESET);
19524007Ssam 		DELAY(10000);
19624007Ssam 
19724007Ssam 		/*
19825927Ssam 		 * Clean up dpm since the controller might
19925927Ssam 		 * jumble dpm after reset.
20024007Ssam 		 */
20125927Ssam 		acesetup(unit);
20225694Ssam 		movow(&addr->csr, CSR_GO);
20324007Ssam 		Csr = addr->csr;
20424007Ssam 		if (Csr & CSR_ACTIVE) {
20525855Ssam 			movow(&addr->ivct, is->is_ivec);
20624007Ssam 			Csr |= CSR_IENA | is->is_promiscuous;
20725694Ssam 			movow(&addr->csr, Csr);
20824007Ssam 			is->is_flags = 0;
20924007Ssam 			is->is_xcnt = 0;
21025694Ssam 			is->is_if.if_flags |= IFF_RUNNING;
21124007Ssam 		}
21224007Ssam 		splx(s);
21324007Ssam 	}
21425694Ssam 	if (is->is_if.if_snd.ifq_head)
21525927Ssam 		acestart(unit);
21624007Ssam }
21724007Ssam 
21824007Ssam /*
21924007Ssam  * Start output on interface.
22024007Ssam  * Get another datagram to send off of the interface queue,
22124007Ssam  * and map it to the interface before starting the output.
22224007Ssam  */
22325927Ssam acestart(unit)
22425927Ssam 	int unit;
22524007Ssam {
22624007Ssam 	register struct tx_segment *txs;
22725694Ssam 	register long len;
22825694Ssam 	register int s;
22924007Ssam 	register struct ace_softc *is = &ace_softc[unit];
23024007Ssam 	struct mbuf *m;
23125694Ssam 	short retries;
23224007Ssam 
23325927Ssam 	if (is->is_flags & ACEF_OACTIVE)
23425927Ssam 		return;
23525927Ssam 	is->is_flags |= ACEF_OACTIVE;
23624007Ssam again:
23724007Ssam 	txs = (struct tx_segment*)(is->is_dpm + (is->is_txnext << 11));
23824007Ssam 	if (txs->tx_csr & TCS_TBFULL) {
23924007Ssam 		is->is_stats.tx_busy++;
24025927Ssam 		is->is_flags &= ~ACEF_OACTIVE;
24124007Ssam 		return;
24224007Ssam 	}
24325694Ssam 	s = splimp();
24424007Ssam 	IF_DEQUEUE(&is->is_if.if_snd, m);
24525694Ssam 	splx(s);
24625927Ssam 	if (m == 0) {
24725927Ssam 		is->is_flags &= ~ACEF_OACTIVE;
24824007Ssam 		return;
24925927Ssam 	}
25024007Ssam 	len = aceput(unit, txs->tx_data, m);
25124007Ssam 	retries = txs->tx_csr & TCS_RTC;
25224007Ssam 	if (retries > 0)
25324007Ssam 		acebakoff(is, txs, retries);
25424007Ssam 
25524007Ssam 	/*
25624007Ssam 	 * Ensure minimum packet length.
25724007Ssam 	 * This makes the safe assumtion that there are no virtual holes
25824007Ssam 	 * after the data.
25924007Ssam 	 * For security, it might be wise to zero out the added bytes,
26024007Ssam 	 * but we're mainly interested in speed at the moment.
26124007Ssam 	 */
26224007Ssam 	if (len - sizeof (struct ether_header) < ETHERMIN)
26324007Ssam 		len = ETHERMIN + sizeof (struct ether_header);
26424007Ssam 	if (++is->is_txnext > SEG_MAX)
26524007Ssam 		is->is_txnext = is->is_segboundry;
26624007Ssam 	is->is_if.if_opackets++;
26724007Ssam 	is->is_xcnt++;
26824007Ssam 	len = (len & 0x7fff) | TCS_TBFULL;
26925694Ssam 	movow(txs, len);
27024007Ssam 	goto again;
27124007Ssam }
27224007Ssam 
27324007Ssam /*
27424007Ssam  * Transmit done interrupt.
27524007Ssam  */
27624007Ssam acecint(unit)
27724007Ssam 	int unit;
27824007Ssam {
27924007Ssam 	register struct ace_softc *is = &ace_softc[unit];
28024007Ssam 	register struct tx_segment *txseg;
28125694Ssam 	short eostat;
28224007Ssam 
28324007Ssam 	if (is->is_xcnt <= 0)  {
28425855Ssam 		log(LOG_ERR, "ace%d: stray xmit interrupt, xcnt %d\n",
28524007Ssam 		    unit, is->is_xcnt);
28624007Ssam 		is->is_xcnt = 0;
28725694Ssam 		if (is->is_if.if_snd.ifq_head)
28825927Ssam 			acestart(unit);
28924007Ssam 		return;
29024007Ssam 	}
29124007Ssam 	is->is_xcnt--;
29224007Ssam 	txseg = (struct tx_segment *)((is->is_eoctr << 11) + is->is_dpm);
29324007Ssam 	eostat = txseg->tx_csr;
29424007Ssam 	if ((eostat & TCS_TBFULL) == 0) {
29524007Ssam 		is->is_stats.tx_retries += eostat & TCS_RTC;
29624007Ssam 		if (eostat & TCS_RTFAIL)  {
29724007Ssam 			is->is_stats.tx_discarded++;
29824007Ssam 			is->is_if.if_oerrors++;
29924007Ssam 		} else
30024007Ssam 			is->is_stats.tx_datagrams++;
30124007Ssam 		if (++is->is_eoctr >= 16)
30224007Ssam 			is->is_eoctr = is->is_segboundry;
30324007Ssam 	}
30425694Ssam 	if (is->is_if.if_snd.ifq_head)
30525927Ssam 		acestart(unit);
30624007Ssam }
30724007Ssam 
30824007Ssam /*
30924007Ssam  * Ethernet interface receiver interrupt.
31024007Ssam  * If input error just drop packet.
31124007Ssam  * Otherwise purge input buffered data path and examine
31224007Ssam  * packet to determine type.  If can't determine length
31324007Ssam  * from type, then have to drop packet.  Othewise decapsulate
31424007Ssam  * packet based on type and pass to type specific higher-level
31524007Ssam  * input routine.
31624007Ssam  */
31724007Ssam acerint(unit)
31824007Ssam 	int unit;
31924007Ssam {
32024007Ssam 	register struct ace_softc *is = &ace_softc[unit];
32124007Ssam 	register struct ifqueue *inq;
32224007Ssam 	register struct ether_header *ace;
32324007Ssam 	register struct rx_segment *rxseg;
32424007Ssam 	int len, s, off, resid;
32524007Ssam 	struct mbuf *m;
32624007Ssam 	short eistat;
32724007Ssam 
32825694Ssam 	if ((is->is_if.if_flags&IFF_RUNNING) == 0)
32925694Ssam 		return;
33024007Ssam again:
33124007Ssam 	rxseg = (struct rx_segment *)((is->is_eictr << 11) + is->is_dpm);
33224007Ssam 	eistat = rxseg->rx_csr;
33324007Ssam 	if ((eistat & RCS_RBFULL) == 0)
33424007Ssam 		return;
33524007Ssam 	is->is_if.if_ipackets++;
33624007Ssam 	if (++is->is_eictr >= is->is_segboundry)
33724007Ssam 		is->is_eictr = 0;
33824007Ssam 	len = eistat & RCS_RBC;
33924007Ssam 	if ((eistat & (RCS_ROVRN | RCS_RCRC | RCS_RODD)) ||
34024007Ssam 	    len < ET_MINLEN || len > ET_MAXLEN+CRC_SIZE) {
34124007Ssam 		if (eistat & RCS_ROVRN)
34224007Ssam 			is->is_stats.rx_overruns++;
34324007Ssam 		if (eistat & RCS_RCRC)
34424007Ssam 			is->is_stats.rx_crc_errors++;
34524007Ssam 		if (eistat & RCS_RODD)
34624007Ssam 			is->is_stats.rx_align_errors++;
34724007Ssam 		if (len < ET_MINLEN)
34824007Ssam 			is->is_stats.rx_underruns++;
34924007Ssam 		if (len > ET_MAXLEN+CRC_SIZE)
35024007Ssam 			is->is_stats.rx_overruns++;
35124007Ssam 		is->is_if.if_ierrors++;
35224007Ssam 		rxseg->rx_csr = 0;
35324007Ssam 		return;
35424007Ssam 	} else
35524007Ssam 		is->is_stats.rx_datagrams++;
35624007Ssam 	ace = (struct ether_header *)rxseg->rx_data;
35724007Ssam 	len -= sizeof (struct ether_header);
35824007Ssam 	/*
35925694Ssam 	 * Deal with trailer protocol: if type is trailer
36024007Ssam 	 * get true type from first 16-bit word past data.
36124007Ssam 	 * Remember that type was trailer by setting off.
36224007Ssam 	 */
36324007Ssam 	ace->ether_type = ntohs((u_short)ace->ether_type);
36424007Ssam #define	acedataaddr(ace, off, type) \
36524007Ssam     ((type)(((caddr_t)(((char *)ace)+sizeof (struct ether_header))+(off))))
36625694Ssam 	if (ace->ether_type >= ETHERTYPE_TRAIL &&
36725694Ssam 	    ace->ether_type < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
36825694Ssam 		off = (ace->ether_type - ETHERTYPE_TRAIL) * 512;
36924007Ssam 		if (off >= ETHERMTU)
37024007Ssam 			goto setup;		/* sanity */
37124007Ssam 		ace->ether_type = ntohs(*acedataaddr(ace, off, u_short *));
37224007Ssam 		resid = ntohs(*(acedataaddr(ace, off+2, u_short *)));
37324007Ssam 		if (off + resid > len)
37424007Ssam 			goto setup;		/* sanity */
37524007Ssam 		len = off + resid;
37624007Ssam 	} else
37724007Ssam 		off = 0;
37824007Ssam 	if (len == 0)
37924007Ssam 		goto setup;
38024007Ssam 
38124007Ssam 	/*
38224007Ssam 	 * Pull packet off interface.  Off is nonzero if packet
38324007Ssam 	 * has trailing header; aceget will then force this header
38424007Ssam 	 * information to be at the front, but we still have to drop
38524007Ssam 	 * the type and length which are at the front of any trailer data.
38624007Ssam 	 */
38725855Ssam 	m = aceget((u_char *)rxseg->rx_data, len, off, &is->is_if);
38824007Ssam 	if (m == 0)
38924007Ssam 		goto setup;
39024007Ssam 	if (off) {
39125855Ssam 		struct ifnet *ifp;
39225855Ssam 
39325855Ssam 		ifp = *(mtod(m, struct ifnet **));
39424007Ssam 		m->m_off += 2 * sizeof (u_short);
39524007Ssam 		m->m_len -= 2 * sizeof (u_short);
39625855Ssam 		*(mtod(m, struct ifnet **)) = ifp;
39724007Ssam 	}
39824007Ssam 	switch (ace->ether_type) {
39924007Ssam 
40024007Ssam #ifdef INET
40125694Ssam 	case ETHERTYPE_IP:
40224007Ssam 		schednetisr(NETISR_IP);
40324007Ssam 		inq = &ipintrq;
40424007Ssam 		break;
40525855Ssam #endif
40624007Ssam 
40725694Ssam 	case ETHERTYPE_ARP:
40824007Ssam 		arpinput(&is->is_ac, m);
40924007Ssam 		goto setup;
41025694Ssam #ifdef NS
41125694Ssam 	case ETHERTYPE_NS:
41225694Ssam 		schednetisr(NETISR_NS);
41325694Ssam 		inq = &nsintrq;
41425694Ssam 		break;
41525694Ssam 
41625694Ssam #endif
41724007Ssam 	default:
41824007Ssam 		m_freem(m);
41924007Ssam 		goto setup;
42024007Ssam 	}
42124007Ssam 	if (IF_QFULL(inq)) {
42224007Ssam 		IF_DROP(inq);
42324007Ssam 		m_freem(m);
42424007Ssam 		goto setup;
42524007Ssam 	}
42624007Ssam 	s = splimp();
42724007Ssam 	IF_ENQUEUE(inq, m);
42824007Ssam 	splx(s);
42924007Ssam setup:
43024007Ssam 	rxseg->rx_csr = 0;
43124007Ssam 	goto again;
43224007Ssam }
43324007Ssam 
43424007Ssam /*
43524007Ssam  * Ethernet output routine.
43624007Ssam  * Encapsulate a packet of type family for the local net.
43724007Ssam  * Use trailer local net encapsulation if enough data in first
43824007Ssam  * packet leaves a multiple of 512 bytes of data in remainder.
43924007Ssam  */
44024007Ssam aceoutput(ifp, m0, dst)
44124007Ssam 	struct ifnet *ifp;
44224007Ssam 	struct mbuf *m0;
44324007Ssam 	struct sockaddr *dst;
44424007Ssam {
44524007Ssam 	register struct ace_softc *is = &ace_softc[ifp->if_unit];
44624007Ssam 	register struct mbuf *m = m0;
44724007Ssam 	register struct ether_header *ace;
44824007Ssam 	register int off;
44924007Ssam 	struct mbuf *mcopy = (struct mbuf *)0;
45025957Ssam 	int type, s, error, usetrailers;
45125694Ssam 	u_char edst[6];
45224007Ssam 	struct in_addr idst;
45324007Ssam 
45425855Ssam 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
45525855Ssam 		error = ENETDOWN;
45625855Ssam 		goto bad;
45725855Ssam 	}
45824007Ssam 	switch (dst->sa_family) {
45924007Ssam 
46024007Ssam #ifdef INET
46124007Ssam 	case AF_INET:
46224007Ssam 		idst = ((struct sockaddr_in *)dst)->sin_addr;
46325957Ssam 		if (!arpresolve(&is->is_ac, m, &idst, edst, &usetrailers))
46424007Ssam 			return (0);	/* if not yet resolved */
46525694Ssam 		if (!bcmp((caddr_t)edst, (caddr_t)etherbroadcastaddr,
46625937Ssam 		    sizeof (edst)))
46724007Ssam 			mcopy = m_copy(m, 0, (int)M_COPYALL);
46824007Ssam 		off = ntohs((u_short)mtod(m, struct ip *)->ip_len) - m->m_len;
46925957Ssam 		if (usetrailers && off > 0 && (off & 0x1ff) == 0 &&
47024007Ssam 		    m->m_off >= MMINOFF + 2 * sizeof (u_short)) {
47125694Ssam 			type = ETHERTYPE_TRAIL + (off>>9);
47224007Ssam 			m->m_off -= 2 * sizeof (u_short);
47324007Ssam 			m->m_len += 2 * sizeof (u_short);
47425694Ssam 			*mtod(m, u_short *) = htons((u_short)ETHERTYPE_IP);
47524007Ssam 			*(mtod(m, u_short *) + 1) = htons((u_short)m->m_len);
47624007Ssam 			goto gottrailertype;
47724007Ssam 		}
47825694Ssam 		type = ETHERTYPE_IP;
47924007Ssam 		off = 0;
48024007Ssam 		goto gottype;
48124007Ssam #endif
48225694Ssam #ifdef NS
48325694Ssam 	case AF_NS:
48425694Ssam  		bcopy((caddr_t)&(((struct sockaddr_ns *)dst)->sns_addr.x_host),
48525694Ssam 		    (caddr_t)edst, sizeof (edst));
48625694Ssam 		if (!bcmp((caddr_t)edst, (caddr_t)&ns_broadhost,sizeof(edst)))
48725694Ssam 			mcopy = m_copy(m, 0, (int)M_COPYALL);
48825694Ssam 		else if (!bcmp((caddr_t)edst, (caddr_t)&ns_thishost,
48925694Ssam 		    sizeof(edst)))
49025694Ssam 			return(looutput(&loif, m, dst));
49125694Ssam 		type = ETHERTYPE_NS;
49225694Ssam 		off = 0;
49325694Ssam 		goto gottype;
49425694Ssam #endif
49524007Ssam 	case AF_UNSPEC:
49624007Ssam 		ace = (struct ether_header *)dst->sa_data;
49725694Ssam 		bcopy((caddr_t)ace->ether_dhost, (caddr_t)edst, sizeof (edst));
49824007Ssam 		type = ace->ether_type;
49924007Ssam 		goto gottype;
50024007Ssam 
50124007Ssam 	default:
50225855Ssam 		log(LOG_ERR, "ace%d: can't handle af%d\n",
50324007Ssam 		    ifp->if_unit, dst->sa_family);
50424007Ssam 		error = EAFNOSUPPORT;
50524007Ssam 		goto bad;
50624007Ssam 	}
50724007Ssam 
50824007Ssam gottrailertype:
50924007Ssam 	/*
51024007Ssam 	 * Packet to be sent as trailer: move first packet
51124007Ssam 	 * (control information) to end of chain.
51224007Ssam 	 */
51324007Ssam 	while (m->m_next)
51424007Ssam 		m = m->m_next;
51524007Ssam 	m->m_next = m0;
51624007Ssam 	m = m0->m_next;
51724007Ssam 	m0->m_next = 0;
51824007Ssam 	m0 = m;
51924007Ssam 
52024007Ssam gottype:
52124007Ssam 	/*
52224007Ssam 	 * Add local net header.  If no space in first mbuf,
52324007Ssam 	 * allocate another.
52424007Ssam 	 */
52524007Ssam 	if (m->m_off > MMAXOFF ||
52624007Ssam 	    MMINOFF + sizeof (struct ether_header) > m->m_off) {
52724007Ssam 		m = m_get(M_DONTWAIT, MT_HEADER);
52824007Ssam 		if (m == 0) {
52924007Ssam 			error = ENOBUFS;
53024007Ssam 			goto bad;
53124007Ssam 		}
53224007Ssam 		m->m_next = m0;
53324007Ssam 		m->m_off = MMINOFF;
53424007Ssam 		m->m_len = sizeof (struct ether_header);
53524007Ssam 	} else {
53624007Ssam 		m->m_off -= sizeof (struct ether_header);
53724007Ssam 		m->m_len += sizeof (struct ether_header);
53824007Ssam 	}
53924007Ssam 	ace = mtod(m, struct ether_header *);
54025694Ssam 	bcopy((caddr_t)edst, (caddr_t)ace->ether_dhost, sizeof (edst));
54125694Ssam 	bcopy((caddr_t)is->is_addr, (caddr_t)ace->ether_shost,
54225694Ssam 	    sizeof (is->is_addr));
54324007Ssam 	ace->ether_type = htons((u_short)type);
54424007Ssam 
54524007Ssam 	/*
54624007Ssam 	 * Queue message on interface, and start output if interface
54724007Ssam 	 * not yet active.
54824007Ssam 	 */
54924007Ssam 	s = splimp();
55024007Ssam 	if (IF_QFULL(&ifp->if_snd)) {
55124007Ssam 		IF_DROP(&ifp->if_snd);
55224007Ssam 		error = ENOBUFS;
55324007Ssam 		goto qfull;
55424007Ssam 	}
55524007Ssam 	IF_ENQUEUE(&ifp->if_snd, m);
55624007Ssam 	splx(s);
55725927Ssam 	acestart(ifp->if_unit);
55824007Ssam 	return (mcopy ? looutput(&loif, mcopy, dst) : 0);
55924007Ssam qfull:
56024007Ssam 	m0 = m;
56124007Ssam 	splx(s);
56224007Ssam bad:
56324007Ssam 	m_freem(m0);
56424007Ssam 	if (mcopy)
56524007Ssam 		m_freem(mcopy);
56624007Ssam 	return (error);
56724007Ssam }
56824007Ssam 
56924007Ssam /*
57024007Ssam  * Routine to copy from mbuf chain to transmit buffer on the VERSAbus
57124007Ssam  * If packet size is less than the minimum legal size,
57224007Ssam  * the buffer is expanded.  We probably should zero out the extra
57324007Ssam  * bytes for security, but that would slow things down.
57424007Ssam  */
57525694Ssam /*ARGSUSED*/
57624007Ssam aceput(unit, txbuf, m)
57725855Ssam 	int unit;
57825694Ssam 	char *txbuf;
57924007Ssam 	struct mbuf *m;
58024007Ssam {
58125855Ssam 	register u_char *bp, *mcp;
58225855Ssam 	register short *s1, *s2;
58325694Ssam 	register u_int len;
58424007Ssam 	register struct mbuf *mp;
58525694Ssam 	int total;
58624007Ssam 
58724007Ssam 	total = 0;
58825694Ssam 	bp = (u_char *)txbuf;
58925694Ssam 	for (mp = m; (mp); mp = mp->m_next) {
59024007Ssam 		len = mp->m_len;
59124007Ssam 		if (len == 0)
59224007Ssam 			continue;
59324007Ssam 		total += len;
59424007Ssam 		mcp = mtod(mp, u_char *);
59524007Ssam 		if (((int)mcp & 01) && ((int)bp & 01)) {
59624007Ssam 			/* source & destination at odd addresses */
59725694Ssam 			movob(bp++, *mcp++);
59824007Ssam 			--len;
59924007Ssam 		}
60024007Ssam 		if (len > 1 && (((int)mcp & 01)==0) && (((int)bp & 01)==0)) {
60125694Ssam 			register u_int l;
60224007Ssam 
60324007Ssam 			s1 = (short *)bp;
60424007Ssam 			s2 = (short *)mcp;
60524007Ssam 			l = len >> 1;		/* count # of shorts */
60625694Ssam 			while (l-- != 0)
60725694Ssam 				movow(s1++, *s2++);
60824007Ssam 			len &= 1;		/* # remaining bytes */
60924007Ssam 			bp = (u_char *)s1;
61024007Ssam 			mcp = (u_char *)s2;
61124007Ssam 		}
61225694Ssam 		while (len-- != 0)
61325694Ssam 			movob(bp++, *mcp++);
61424007Ssam 	}
61524007Ssam 	m_freem(m);
61624007Ssam 	return (total);
61724007Ssam }
61824007Ssam 
61924007Ssam /*
62024007Ssam  * Routine to copy from VERSAbus memory into mbufs.
62124007Ssam  *
62224007Ssam  * Warning: This makes the fairly safe assumption that
62324007Ssam  * mbufs have even lengths.
62424007Ssam  */
62525694Ssam /*ARGSUSED*/
62624007Ssam struct mbuf *
62725855Ssam aceget(rxbuf, totlen, off0, ifp)
62824007Ssam 	u_char *rxbuf;
62924007Ssam 	int totlen, off0;
63025855Ssam 	struct ifnet *ifp;
63124007Ssam {
63225855Ssam 	register u_char *cp, *mcp;
63324007Ssam 	register int tlen;
63424007Ssam 	register struct mbuf *m;
63524007Ssam 	struct mbuf *top = 0, **mp = &top;
63624007Ssam 	int len, off = off0;
63724007Ssam 
63824007Ssam 	cp = rxbuf + sizeof (struct ether_header);
63924007Ssam 	while (totlen > 0) {
64024007Ssam 		MGET(m, M_DONTWAIT, MT_DATA);
64124007Ssam 		if (m == 0)
64224007Ssam 			goto bad;
64324007Ssam 		if (off) {
64424007Ssam 			len = totlen - off;
64524007Ssam 			cp = rxbuf + sizeof (struct ether_header) + off;
64624007Ssam 		} else
64724007Ssam 			len = totlen;
64825855Ssam 		if (ifp)
64925855Ssam 			len += sizeof(ifp);
65029563Ssam 		if (len >= NBPG) {
65129563Ssam 			MCLGET(m);
65229563Ssam 			if (m->m_len == CLBYTES)
65329563Ssam 				m->m_len = len = MIN(len, CLBYTES);
65429563Ssam 			else
65524007Ssam 				m->m_len = len = MIN(MLEN, len);
65624007Ssam 		} else {
65724007Ssam 			m->m_len = len = MIN(MLEN, len);
65824007Ssam 			m->m_off = MMINOFF;
65924007Ssam 		}
66024007Ssam 		mcp = mtod(m, u_char *);
66125855Ssam 		if (ifp) {
66225855Ssam 			/*
66325855Ssam 			 * Prepend interface pointer to first mbuf.
66425855Ssam 			 */
66525855Ssam 			*(mtod(m, struct ifnet **)) = ifp;
66625855Ssam 			mcp += sizeof(ifp);
66725855Ssam 			len -= sizeof(ifp);
66825855Ssam 			ifp = (struct ifnet *)0;
66925855Ssam 		}
67024007Ssam 		/*bcopy((caddr_t)cp, (caddr_t)mcp, len);*/
67124007Ssam 		/*cp += len; mcp += len;*/
67224007Ssam 		tlen = len;
67324007Ssam 		if (((int)mcp & 01) && ((int)cp & 01)) {
67424007Ssam 			/* source & destination at odd addresses */
67524007Ssam 			*mcp++ = *cp++;
67624007Ssam 			--tlen;
67724007Ssam 		}
67824007Ssam 		if (tlen > 1 && (((int)mcp&01) == 0) && (((int)cp&01) == 0)) {
67924007Ssam 			register short *s1, *s2;
68024007Ssam 			register int l;
68124007Ssam 
68224007Ssam 			s1 = (short *)mcp;
68324007Ssam 			s2 = (short *)cp;
68424007Ssam 			l = tlen >> 1;		/* count # of shorts */
68524007Ssam 			while (l-- > 0)		/* copy shorts */
68624007Ssam 				*s1++ = *s2++;
68724007Ssam 			tlen &= 1;		/* # remaining bytes */
68824007Ssam 			mcp = (u_char *)s1;
68924007Ssam 			cp = (u_char *)s2;
69024007Ssam 		}
69124007Ssam 		while (tlen-- > 0)
69224007Ssam 			*mcp++ = *cp++;
69324007Ssam 		*mp = m;
69424007Ssam 		mp = &m->m_next;
69524007Ssam 		if (off == 0) {
69624007Ssam 			totlen -= len;
69724007Ssam 			continue;
69824007Ssam 		}
69924007Ssam 		off += len;
70024007Ssam 		if (off == totlen) {
70124007Ssam 			cp = rxbuf + sizeof (struct ether_header);
70224007Ssam 			off = 0;
70324007Ssam 			totlen = off0;
70424007Ssam 		}
70524007Ssam 	}
70624007Ssam 	return (top);
70724007Ssam bad:
70824007Ssam 	m_freem(top);
70924007Ssam 	return (0);
71024007Ssam }
71124007Ssam 
71229408Ssam /* backoff table masks */
71329408Ssam short	random_mask_tbl[16] = {
71429563Ssam 	0x0040, 0x00c0, 0x01c0, 0x03c0, 0x07c0, 0x0fc0, 0x1fc0, 0x3fc0,
71529563Ssam 	0x7fc0, 0xffc0, 0xffc0, 0xffc0, 0xffc0, 0xffc0, 0xffc0, 0xffc0
71629408Ssam };
71729408Ssam 
71824007Ssam acebakoff(is, txseg, retries)
71924007Ssam 	struct ace_softc *is;
72024007Ssam 	struct tx_segment *txseg;
72124007Ssam 	register int retries;
72224007Ssam {
72324007Ssam 	register short *pBakNum, random_num;
72424007Ssam 	short *pMask;
72524007Ssam 
72624007Ssam 	pMask = &random_mask_tbl[0];
72724007Ssam 	pBakNum = &txseg->tx_backoff[0];
72824007Ssam 	while (--retries >= 0) {
72924007Ssam 		random_num = (is->is_currnd = (is->is_currnd * 18741)-13849);
73024007Ssam 		random_num &= *pMask++;
73129563Ssam 		*pBakNum++ = random_num ^ (short)(0xff00 | 0x00fc);
73224007Ssam 	}
73324007Ssam }
73424007Ssam 
73524007Ssam /*
73624007Ssam  * Process an ioctl request.
73724007Ssam  */
73824007Ssam aceioctl(ifp, cmd, data)
73924007Ssam 	register struct ifnet *ifp;
74024007Ssam 	int cmd;
74124007Ssam 	caddr_t data;
74224007Ssam {
74325694Ssam 	register struct ifaddr *ifa = (struct ifaddr *)data;
74425855Ssam 	struct acedevice *addr;
74525694Ssam 	int s = splimp(), error = 0;
74624007Ssam 
74724007Ssam 	switch (cmd) {
74824007Ssam 
74924007Ssam 	case SIOCSIFADDR:
75025694Ssam 		ifp->if_flags |= IFF_UP;
75125855Ssam 		switch (ifa->ifa_addr.sa_family) {
75225855Ssam #ifdef INET
75325855Ssam 		case AF_INET:
75425855Ssam 			aceinit(ifp->if_unit);	/* before arpwhohas */
75525855Ssam 			((struct arpcom *)ifp)->ac_ipaddr =
75625855Ssam 				IA_SIN(ifa)->sin_addr;
75725855Ssam 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
75825855Ssam 			break;
75925855Ssam #endif
76025855Ssam #ifdef NS
76125855Ssam 		case AF_NS: {
76225937Ssam 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
76325937Ssam 			struct ace_softc *is = &ace_softc[ifp->if_unit];
76425855Ssam 
76525855Ssam 			if (!ns_nullhost(*ina)) {
76625855Ssam 				ifp->if_flags &= ~IFF_RUNNING;
76725855Ssam 				addr = (struct acedevice *)
76825937Ssam 				    aceinfo[ifp->if_unit]->ui_addr;
76925855Ssam 				movow(&addr->csr, CSR_RESET);
77025855Ssam 				DELAY(10000);
77125855Ssam 				/* set station address & copy addr to arp */
77229408Ssam 				acesetaddr(ifp->if_unit, addr,
77325855Ssam 				    ina->x_host.c_host);
77425855Ssam 			} else
77525937Ssam 				ina->x_host = *(union ns_host *)is->is_addr;
77625855Ssam 			aceinit(ifp->if_unit);
77725855Ssam 			break;
77825855Ssam 		}
77925855Ssam #endif
78025855Ssam 		default:
78125855Ssam 			aceinit(ifp->if_unit);
78225855Ssam 			break;
78325855Ssam 		}
78424007Ssam 		break;
78524007Ssam 
78625855Ssam 	case SIOCSIFFLAGS:
78725855Ssam 		if ((ifp->if_flags&IFF_UP) == 0 && ifp->if_flags&IFF_RUNNING) {
78825855Ssam 			addr = (struct acedevice *)
78925855Ssam 			    (aceinfo[ifp->if_unit]->ui_addr);
79025855Ssam 			movow(&addr->csr, CSR_RESET);
79125855Ssam 			ifp->if_flags &= ~IFF_RUNNING;
79225855Ssam 		} else if (ifp->if_flags&IFF_UP &&
79325855Ssam 		    (ifp->if_flags&IFF_RUNNING) == 0)
79425855Ssam 			aceinit(ifp->if_unit);
79524007Ssam 		break;
79624007Ssam 
79724007Ssam 	default:
79824007Ssam 		error = EINVAL;
79924007Ssam 	}
80024007Ssam 	splx(s);
80124007Ssam 	return (error);
80224007Ssam }
80324007Ssam 
80429408Ssam /*
80529408Ssam  * Set the on-board station address, then read it back
80629408Ssam  * to initialize the address used by ARP (among others).
80729408Ssam  */
80829408Ssam acesetaddr(unit, addr, station)
80929408Ssam 	short unit;
81029408Ssam 	struct acedevice *addr;
81129408Ssam 	char *station;
81229408Ssam {
81329408Ssam 	struct ace_softc *is = &ace_softc[unit];
81429408Ssam 	register short *wp, i;
81529408Ssam 
81629408Ssam 	for (wp = (short *)addr->station, i = 0; i < 6; i++)
81729408Ssam 		movow(wp++, ~*station++);
81829408Ssam 	for (wp = (short *)addr->station, i = 0; i < 6; i++)
81929408Ssam 		is->is_addr[i] = ~*wp++;
82029408Ssam 	printf("ace%d: hardware address %s\n", unit,
82129408Ssam 	    ether_sprintf(is->is_addr));
82229408Ssam }
82329408Ssam 
82429408Ssam /*
82529408Ssam  * Setup the device for use.  Initialize dual-ported memory,
82629408Ssam  * backoff parameters, and various other software state.
82729408Ssam  */
82825927Ssam acesetup(unit)
82924007Ssam 	int unit;
83024007Ssam {
83124007Ssam 	register struct ace_softc *is = &ace_softc[unit];
83225927Ssam 	register char *pData1;
83325694Ssam 	register short i;
83425927Ssam 	struct acedevice *addr;
83524007Ssam 
83625927Ssam 	bzero(is->is_dpm, 16384*2);
83724007Ssam 	is->is_currnd = 49123;
83825927Ssam 	addr = (struct acedevice *)aceinfo[unit]->ui_addr;
83924007Ssam 	is->is_segboundry = (addr->segb >> 11) & 0xf;
84025927Ssam 	pData1 = is->is_dpm + (is->is_segboundry << 11);
84124007Ssam 	for (i = SEG_MAX + 1 - is->is_segboundry; --i >= 0;) {
84224007Ssam 		acebakoff(is, (struct tx_segment *)pData1, 15);
84324007Ssam 		pData1 += sizeof (struct tx_segment);
84424007Ssam 	}
84524007Ssam 	is->is_eictr = 0;
84624007Ssam 	is->is_eoctr = is->is_txnext = is->is_segboundry;
84724007Ssam 	bzero((char *)&is->is_stats, sizeof (is->is_stats));
84824007Ssam }
84924007Ssam #endif
850