xref: /csrg-svn/sys/tahoe/if/if_ace.c (revision 35411)
134405Skarels /*
234405Skarels  * Copyright (c) 1988 Regents of the University of California.
334405Skarels  * All rights reserved.
434405Skarels  *
5*35411Skarels  * This code is derived from software contributed to Berkeley by
6*35411Skarels  * Computer Consoles Inc.
7*35411Skarels  *
834405Skarels  * Redistribution and use in source and binary forms are permitted
934864Sbostic  * provided that the above copyright notice and this paragraph are
1034864Sbostic  * duplicated in all such forms and that any documentation,
1134864Sbostic  * advertising materials, and other materials related to such
1234864Sbostic  * distribution and use acknowledge that the software was developed
1334864Sbostic  * by the University of California, Berkeley.  The name of the
1434864Sbostic  * University may not be used to endorse or promote products derived
1534864Sbostic  * from this software without specific prior written permission.
1634864Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1734864Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1834864Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1934405Skarels  *
20*35411Skarels  *	@(#)if_ace.c	7.3 (Berkeley) 08/27/88
2134405Skarels  */
2224007Ssam 
2324007Ssam /*
2424007Ssam  * ACC VERSAbus Ethernet controller
2524007Ssam  */
2624007Ssam #include "ace.h"
2724007Ssam #if NACE > 0
2824007Ssam 
2925694Ssam #include "param.h"
3025694Ssam #include "systm.h"
31*35411Skarels #include "malloc.h"
3225694Ssam #include "mbuf.h"
3325694Ssam #include "buf.h"
3425694Ssam #include "protosw.h"
3525694Ssam #include "socket.h"
3625694Ssam #include "vmmac.h"
3725694Ssam #include "ioctl.h"
3825694Ssam #include "errno.h"
3925694Ssam #include "vmparam.h"
4025855Ssam #include "syslog.h"
4124007Ssam 
4224007Ssam #include "../net/if.h"
4324007Ssam #include "../net/netisr.h"
4424007Ssam #include "../net/route.h"
4525855Ssam #ifdef INET
4624007Ssam #include "../netinet/in.h"
4724007Ssam #include "../netinet/in_systm.h"
4825694Ssam #include "../netinet/in_var.h"
4924007Ssam #include "../netinet/ip.h"
5024007Ssam #include "../netinet/ip_var.h"
5124007Ssam #include "../netinet/if_ether.h"
5225855Ssam #endif
5325855Ssam #ifdef NS
5425855Ssam #include "../netns/ns.h"
5525855Ssam #include "../netns/ns_if.h"
5625855Ssam #endif
5724007Ssam 
5830229Ssam #include "../machine/cpu.h"
5930229Ssam #include "../machine/pte.h"
6030229Ssam 
6124007Ssam #include "../tahoe/mtpr.h"
6224007Ssam #include "../tahoeif/if_acereg.h"
6325694Ssam #include "../tahoevba/vbavar.h"
6424007Ssam 
65*35411Skarels int	aceprobe(), aceattach(), acerint(), acecint(), acestart();
6624007Ssam struct	vba_device *aceinfo[NACE];
6725983Ssam long	acestd[] = { 0 };
6824007Ssam struct	vba_driver acedriver =
6925927Ssam     { aceprobe, 0, aceattach, 0, acestd, "ace", aceinfo, "v/eiu", 0 };
7024007Ssam 
7124007Ssam int	aceinit(), aceoutput(), aceioctl(), acereset();
7224007Ssam struct	mbuf *aceget();
7324007Ssam 
7424007Ssam /*
7524007Ssam  * Ethernet software status per interface.
7624007Ssam  *
7724007Ssam  * Each interface is referenced by a network interface structure,
7824007Ssam  * is_if, which the routing code uses to locate the interface.
7924007Ssam  * This structure contains the output queue for the interface, its address, ...
8024007Ssam  */
8124007Ssam struct	ace_softc {
8224007Ssam 	struct	arpcom is_ac;		/* Ethernet common part	*/
8324007Ssam #define	is_if	is_ac.ac_if		/* network-visible interface */
8424007Ssam #define	is_addr	is_ac.ac_enaddr		/* hardware Ethernet address */
8524007Ssam 	short	is_flags;
8624007Ssam #define	ACEF_OACTIVE	0x1		/* output is active */
8724007Ssam #define	ACEF_RCVPENDING	0x2		/* start rcv in acecint	*/
8824007Ssam 	short	is_promiscuous;		/* true is enabled */
8924007Ssam 	short	is_segboundry;		/* first TX Seg in dpm */
9024007Ssam 	short	is_eictr;		/* Rx segment tracking ctr */
9124007Ssam 	short	is_eoctr;		/* Tx segment tracking ctr */
9224007Ssam 	short	is_txnext;		/* Next available Tx segment */
9324007Ssam 	short	is_currnd;		/* current random backoff */
9424007Ssam 	struct	ace_stats is_stats;	/* holds board statistics */
9524007Ssam 	short	is_xcnt;		/* count xmitted segments to be acked
9624007Ssam 					   by the controller */
9725855Ssam 	long	is_ivec;		/* autoconfig interrupt vector base */
9825927Ssam 	struct	pte *is_map;		/* pte map for dual ported memory */
9925927Ssam 	caddr_t	is_dpm;			/* address of mapped memory */
10024007Ssam } ace_softc[NACE];
10124007Ssam extern	struct ifnet loif;
10224007Ssam 
10325855Ssam aceprobe(reg, vi)
10424007Ssam 	caddr_t reg;
10525855Ssam 	struct vba_device *vi;
10624007Ssam {
10725855Ssam 	register br, cvec;		/* must be r12, r11 */
10825855Ssam 	struct acedevice *ap = (struct acedevice *)reg;
10925855Ssam 	struct ace_softc *is = &ace_softc[vi->ui_unit];
11024007Ssam 
11124007Ssam #ifdef lint
11230295Ssam 	br = 0; cvec = br; br = cvec;
11324007Ssam 	acerint(0); acecint(0);
11424007Ssam #endif
11524007Ssam 	if (badaddr(reg, 2))
11625855Ssam 		return (0);
11725855Ssam 	movow(&ap->csr, CSR_RESET);
11824007Ssam 	DELAY(10000);
11925855Ssam #ifdef notdef
12025855Ssam 	/*
12125855Ssam 	 * Select two spaces for the interrupts aligned to an
12225855Ssam 	 * eight vector boundary and fitting in 8 bits (as
12325855Ssam 	 * required by the controller) -- YECH.  The controller
12425855Ssam 	 * will be notified later at initialization time.
12525855Ssam 	 */
12625855Ssam 	if ((vi->ui_hd->vh_lastiv -= 2) > 0xff)
12725855Ssam 		vi->ui_hd->vh_lastiv  = 0x200;
12825855Ssam 	is->is_ivec = vi->ui_hd->vh_lastiv = vi->ui_hd->vh_lastiv &~ 0x7;
12925855Ssam #else
13025855Ssam 	is->is_ivec = 0x90+vi->ui_unit*8;
13125855Ssam #endif
13225855Ssam 	br = 0x14, cvec = is->is_ivec;		/* XXX */
13325855Ssam 	return (sizeof (*ap));
13424007Ssam }
13524007Ssam 
13624007Ssam /*
13724007Ssam  * Interface exists: make available by filling in network interface
13824007Ssam  * record.  System will initialize the interface when it is ready
13924007Ssam  * to accept packets.
14024007Ssam  */
14124007Ssam aceattach(ui)
14224007Ssam 	struct vba_device *ui;
14324007Ssam {
14424007Ssam 	register short unit = ui->ui_unit;
14524007Ssam 	register struct ace_softc *is = &ace_softc[unit];
14624007Ssam 	register struct ifnet *ifp = &is->is_if;
14724007Ssam 	register struct acedevice *addr = (struct acedevice *)ui->ui_addr;
14824007Ssam 	register short *wp, i;
149*35411Skarels 	extern enoutput();
15024007Ssam 
15124007Ssam 	ifp->if_unit = unit;
15224007Ssam 	ifp->if_name = "ace";
15324007Ssam 	ifp->if_mtu = ETHERMTU;
15424007Ssam 	/*
15529408Ssam 	 * Get station's addresses and set multicast hash table.
15624007Ssam 	 */
15729408Ssam 	for (wp = (short *)addr->station, i = 0; i < 6; i++)
15829408Ssam 		is->is_addr[i] = ~*wp++;
15929408Ssam 	printf("ace%d: hardware address %s\n", unit,
16029408Ssam 	    ether_sprintf(is->is_addr));
16124007Ssam 	is->is_promiscuous = 0;
16229408Ssam 	for (wp = (short *)addr->hash, i =  0; i < 8; i++)
16329408Ssam 		movow(wp++, ~0xf);
16425694Ssam 	movow(&addr->bcastena[0], ~0xffff);
16525694Ssam 	movow(&addr->bcastena[1], ~0xffff);
16625927Ssam 	/*
16725927Ssam 	 * Allocate and map dual ported VERSAbus memory.
16825927Ssam 	 */
16931735Skarels 	if (vbmemalloc(32, (caddr_t)ui->ui_flags,
17031735Skarels 	    &is->is_map, &is->is_dpm) == 0) {
17131735Skarels 		printf("ace%d: can't allocate VERSAbus memory map\n", unit);
17231735Skarels 		return;
17331735Skarels 	}
17425927Ssam 
17524007Ssam 	ifp->if_init = aceinit;
176*35411Skarels 	ifp->if_output = enoutput;
177*35411Skarels 	ifp->if_start = acestart;
17824007Ssam 	ifp->if_ioctl = aceioctl;
17924007Ssam 	ifp->if_reset = acereset;
180*35411Skarels 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
18124007Ssam 	if_attach(ifp);
18224007Ssam }
18324007Ssam 
18424007Ssam /*
18524007Ssam  * Reset of interface after "system" reset.
18624007Ssam  */
18724007Ssam acereset(unit, vban)
18824007Ssam 	int unit, vban;
18924007Ssam {
19024007Ssam 	register struct vba_device *ui;
19124007Ssam 
19224007Ssam 	if (unit >= NACE || (ui = aceinfo[unit]) == 0 || ui->ui_alive == 0 ||
19324007Ssam 	    ui->ui_vbanum != vban)
19424007Ssam 		return;
19524007Ssam 	printf(" ace%d", unit);
19624007Ssam 	aceinit(unit);
19724007Ssam }
19824007Ssam 
19924007Ssam /*
20024007Ssam  * Initialization of interface; clear recorded pending operations
20124007Ssam  */
20224007Ssam aceinit(unit)
20324007Ssam 	int unit;
20424007Ssam {
20524007Ssam 	register struct ace_softc *is = &ace_softc[unit];
20624007Ssam 	register struct vba_device *ui = aceinfo[unit];
20724007Ssam 	register struct acedevice *addr;
20824007Ssam 	register short Csr;
20925694Ssam 	register int s;
21024007Ssam 
211*35411Skarels 	if (is->is_if.if_addrlist == (struct ifaddr *)0)
21224007Ssam 		return;
213*35411Skarels 	if ((is->is_if.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)
241*35411Skarels 		acestart(&is->is_if);
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  */
249*35411Skarels acestart(ifp)
250*35411Skarels 	register struct ifnet *ifp;
25124007Ssam {
25224007Ssam 	register struct tx_segment *txs;
25325694Ssam 	register long len;
25425694Ssam 	register int s;
25524007Ssam 	struct mbuf *m;
25625694Ssam 	short retries;
257*35411Skarels #define	is ((struct ace_softc *)ifp)
25824007Ssam 
25924007Ssam again:
26024007Ssam 	txs = (struct tx_segment*)(is->is_dpm + (is->is_txnext << 11));
26124007Ssam 	if (txs->tx_csr & TCS_TBFULL) {
26224007Ssam 		is->is_stats.tx_busy++;
263*35411Skarels 		ifp->if_flags |= IFF_OACTIVE;
264*35411Skarels 		return (0);
26524007Ssam 	}
26625694Ssam 	s = splimp();
267*35411Skarels 	IF_DEQUEUE(&ifp->if_snd, m);
26825694Ssam 	splx(s);
26925927Ssam 	if (m == 0) {
270*35411Skarels 		ifp->if_flags &= ~IFF_OACTIVE;
271*35411Skarels 		return (0);
27225927Ssam 	}
273*35411Skarels 	len = aceput(txs->tx_data, m);
27424007Ssam 	retries = txs->tx_csr & TCS_RTC;
27524007Ssam 	if (retries > 0)
27624007Ssam 		acebakoff(is, txs, retries);
27724007Ssam 
27824007Ssam 	/*
27924007Ssam 	 * Ensure minimum packet length.
28024007Ssam 	 * This makes the safe assumtion that there are no virtual holes
28124007Ssam 	 * after the data.
28224007Ssam 	 * For security, it might be wise to zero out the added bytes,
28324007Ssam 	 * but we're mainly interested in speed at the moment.
28424007Ssam 	 */
28524007Ssam 	if (len - sizeof (struct ether_header) < ETHERMIN)
28624007Ssam 		len = ETHERMIN + sizeof (struct ether_header);
28724007Ssam 	if (++is->is_txnext > SEG_MAX)
28824007Ssam 		is->is_txnext = is->is_segboundry;
289*35411Skarels 	ifp->if_opackets++;
29024007Ssam 	is->is_xcnt++;
29124007Ssam 	len = (len & 0x7fff) | TCS_TBFULL;
29225694Ssam 	movow(txs, len);
29324007Ssam 	goto again;
294*35411Skarels #undef is
29524007Ssam }
29624007Ssam 
29724007Ssam /*
29824007Ssam  * Transmit done interrupt.
29924007Ssam  */
30024007Ssam acecint(unit)
30124007Ssam 	int unit;
30224007Ssam {
30324007Ssam 	register struct ace_softc *is = &ace_softc[unit];
30424007Ssam 	register struct tx_segment *txseg;
30525694Ssam 	short eostat;
30624007Ssam 
30724007Ssam 	if (is->is_xcnt <= 0)  {
30825855Ssam 		log(LOG_ERR, "ace%d: stray xmit interrupt, xcnt %d\n",
30924007Ssam 		    unit, is->is_xcnt);
31024007Ssam 		is->is_xcnt = 0;
31125694Ssam 		if (is->is_if.if_snd.ifq_head)
312*35411Skarels 			acestart(&is->is_if);
31324007Ssam 		return;
31424007Ssam 	}
31524007Ssam 	is->is_xcnt--;
31624007Ssam 	txseg = (struct tx_segment *)((is->is_eoctr << 11) + is->is_dpm);
31724007Ssam 	eostat = txseg->tx_csr;
31824007Ssam 	if ((eostat & TCS_TBFULL) == 0) {
31924007Ssam 		is->is_stats.tx_retries += eostat & TCS_RTC;
32024007Ssam 		if (eostat & TCS_RTFAIL)  {
32124007Ssam 			is->is_stats.tx_discarded++;
32224007Ssam 			is->is_if.if_oerrors++;
32324007Ssam 		} else
32424007Ssam 			is->is_stats.tx_datagrams++;
32524007Ssam 		if (++is->is_eoctr >= 16)
32624007Ssam 			is->is_eoctr = is->is_segboundry;
32724007Ssam 	}
32825694Ssam 	if (is->is_if.if_snd.ifq_head)
329*35411Skarels 		acestart(&is->is_if);
33024007Ssam }
33124007Ssam 
33224007Ssam /*
33324007Ssam  * Ethernet interface receiver interrupt.
33424007Ssam  * If input error just drop packet.
33524007Ssam  * Otherwise purge input buffered data path and examine
33624007Ssam  * packet to determine type.  If can't determine length
33724007Ssam  * from type, then have to drop packet.  Othewise decapsulate
33824007Ssam  * packet based on type and pass to type specific higher-level
33924007Ssam  * input routine.
34024007Ssam  */
34124007Ssam acerint(unit)
34224007Ssam 	int unit;
34324007Ssam {
34424007Ssam 	register struct ace_softc *is = &ace_softc[unit];
34524007Ssam 	register struct ifqueue *inq;
34624007Ssam 	register struct ether_header *ace;
34724007Ssam 	register struct rx_segment *rxseg;
34824007Ssam 	int len, s, off, resid;
34924007Ssam 	struct mbuf *m;
35024007Ssam 	short eistat;
35124007Ssam 
35225694Ssam 	if ((is->is_if.if_flags&IFF_RUNNING) == 0)
35325694Ssam 		return;
35424007Ssam again:
35524007Ssam 	rxseg = (struct rx_segment *)((is->is_eictr << 11) + is->is_dpm);
35624007Ssam 	eistat = rxseg->rx_csr;
35724007Ssam 	if ((eistat & RCS_RBFULL) == 0)
35824007Ssam 		return;
35924007Ssam 	is->is_if.if_ipackets++;
36024007Ssam 	if (++is->is_eictr >= is->is_segboundry)
36124007Ssam 		is->is_eictr = 0;
36224007Ssam 	len = eistat & RCS_RBC;
36324007Ssam 	if ((eistat & (RCS_ROVRN | RCS_RCRC | RCS_RODD)) ||
36424007Ssam 	    len < ET_MINLEN || len > ET_MAXLEN+CRC_SIZE) {
36524007Ssam 		if (eistat & RCS_ROVRN)
36624007Ssam 			is->is_stats.rx_overruns++;
36724007Ssam 		if (eistat & RCS_RCRC)
36824007Ssam 			is->is_stats.rx_crc_errors++;
36924007Ssam 		if (eistat & RCS_RODD)
37024007Ssam 			is->is_stats.rx_align_errors++;
37124007Ssam 		if (len < ET_MINLEN)
37224007Ssam 			is->is_stats.rx_underruns++;
37324007Ssam 		if (len > ET_MAXLEN+CRC_SIZE)
37424007Ssam 			is->is_stats.rx_overruns++;
37524007Ssam 		is->is_if.if_ierrors++;
37624007Ssam 		rxseg->rx_csr = 0;
37724007Ssam 		return;
37824007Ssam 	} else
37924007Ssam 		is->is_stats.rx_datagrams++;
38024007Ssam 	ace = (struct ether_header *)rxseg->rx_data;
38124007Ssam 	len -= sizeof (struct ether_header);
38224007Ssam 	/*
38325694Ssam 	 * Deal with trailer protocol: if type is trailer
38424007Ssam 	 * get true type from first 16-bit word past data.
38524007Ssam 	 * Remember that type was trailer by setting off.
38624007Ssam 	 */
38724007Ssam 	ace->ether_type = ntohs((u_short)ace->ether_type);
38824007Ssam #define	acedataaddr(ace, off, type) \
38924007Ssam     ((type)(((caddr_t)(((char *)ace)+sizeof (struct ether_header))+(off))))
39025694Ssam 	if (ace->ether_type >= ETHERTYPE_TRAIL &&
39125694Ssam 	    ace->ether_type < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
39225694Ssam 		off = (ace->ether_type - ETHERTYPE_TRAIL) * 512;
39324007Ssam 		if (off >= ETHERMTU)
39424007Ssam 			goto setup;		/* sanity */
39524007Ssam 		ace->ether_type = ntohs(*acedataaddr(ace, off, u_short *));
39624007Ssam 		resid = ntohs(*(acedataaddr(ace, off+2, u_short *)));
39724007Ssam 		if (off + resid > len)
39824007Ssam 			goto setup;		/* sanity */
39924007Ssam 		len = off + resid;
40024007Ssam 	} else
40124007Ssam 		off = 0;
40224007Ssam 	if (len == 0)
40324007Ssam 		goto setup;
40424007Ssam 
40524007Ssam 	/*
40624007Ssam 	 * Pull packet off interface.  Off is nonzero if packet
40724007Ssam 	 * has trailing header; aceget will then force this header
408*35411Skarels 	 * information to be at the front.
40924007Ssam 	 */
41025855Ssam 	m = aceget((u_char *)rxseg->rx_data, len, off, &is->is_if);
41124007Ssam 	if (m == 0)
41224007Ssam 		goto setup;
41324007Ssam 	switch (ace->ether_type) {
41424007Ssam 
41524007Ssam #ifdef INET
41625694Ssam 	case ETHERTYPE_IP:
41724007Ssam 		schednetisr(NETISR_IP);
41824007Ssam 		inq = &ipintrq;
41924007Ssam 		break;
42025855Ssam #endif
42124007Ssam 
42225694Ssam 	case ETHERTYPE_ARP:
42324007Ssam 		arpinput(&is->is_ac, m);
42424007Ssam 		goto setup;
42525694Ssam #ifdef NS
42625694Ssam 	case ETHERTYPE_NS:
42725694Ssam 		schednetisr(NETISR_NS);
42825694Ssam 		inq = &nsintrq;
42925694Ssam 		break;
43025694Ssam 
43125694Ssam #endif
43224007Ssam 	default:
43324007Ssam 		m_freem(m);
43424007Ssam 		goto setup;
43524007Ssam 	}
43624007Ssam 	if (IF_QFULL(inq)) {
43724007Ssam 		IF_DROP(inq);
43824007Ssam 		m_freem(m);
43924007Ssam 		goto setup;
44024007Ssam 	}
44124007Ssam 	s = splimp();
44224007Ssam 	IF_ENQUEUE(inq, m);
44324007Ssam 	splx(s);
44424007Ssam setup:
44524007Ssam 	rxseg->rx_csr = 0;
44624007Ssam 	goto again;
44724007Ssam }
44824007Ssam 
44924007Ssam /*
45024007Ssam  * Routine to copy from mbuf chain to transmit buffer on the VERSAbus
45124007Ssam  * If packet size is less than the minimum legal size,
45224007Ssam  * the buffer is expanded.  We probably should zero out the extra
45324007Ssam  * bytes for security, but that would slow things down.
45424007Ssam  */
455*35411Skarels aceput(txbuf, m)
45625694Ssam 	char *txbuf;
45724007Ssam 	struct mbuf *m;
45824007Ssam {
45925855Ssam 	register u_char *bp, *mcp;
46025855Ssam 	register short *s1, *s2;
46125694Ssam 	register u_int len;
46224007Ssam 	register struct mbuf *mp;
46325694Ssam 	int total;
46424007Ssam 
465*35411Skarels 	total = mp->m_pkthdr.len;
46625694Ssam 	bp = (u_char *)txbuf;
46725694Ssam 	for (mp = m; (mp); mp = mp->m_next) {
46824007Ssam 		len = mp->m_len;
46924007Ssam 		if (len == 0)
47024007Ssam 			continue;
47124007Ssam 		mcp = mtod(mp, u_char *);
47224007Ssam 		if (((int)mcp & 01) && ((int)bp & 01)) {
47324007Ssam 			/* source & destination at odd addresses */
47425694Ssam 			movob(bp++, *mcp++);
47524007Ssam 			--len;
47624007Ssam 		}
47724007Ssam 		if (len > 1 && (((int)mcp & 01)==0) && (((int)bp & 01)==0)) {
478*35411Skarels 			int l = len;
47924007Ssam 
48024007Ssam 			s1 = (short *)bp;
48124007Ssam 			s2 = (short *)mcp;
482*35411Skarels 			len >>= 1;		/* count # of shorts */
483*35411Skarels 			while (len-- != 0)
48425694Ssam 				movow(s1++, *s2++);
485*35411Skarels 			len = l & 1;		/* # remaining bytes */
48624007Ssam 			bp = (u_char *)s1;
48724007Ssam 			mcp = (u_char *)s2;
48824007Ssam 		}
48925694Ssam 		while (len-- != 0)
49025694Ssam 			movob(bp++, *mcp++);
49124007Ssam 	}
49224007Ssam 	m_freem(m);
49324007Ssam 	return (total);
49424007Ssam }
49524007Ssam 
49624007Ssam /*
49724007Ssam  * Routine to copy from VERSAbus memory into mbufs.
49824007Ssam  *
49924007Ssam  * Warning: This makes the fairly safe assumption that
50024007Ssam  * mbufs have even lengths.
50124007Ssam  */
50225694Ssam /*ARGSUSED*/
50324007Ssam struct mbuf *
504*35411Skarels aceget(rxbuf, totlen, off, ifp)
50524007Ssam 	u_char *rxbuf;
506*35411Skarels 	int totlen, off;
50725855Ssam 	struct ifnet *ifp;
50824007Ssam {
50925855Ssam 	register u_char *cp, *mcp;
510*35411Skarels 	register struct mbuf *m;
51124007Ssam 	register int tlen;
51224007Ssam 	struct mbuf *top = 0, **mp = &top;
513*35411Skarels 	int len;
514*35411Skarels 	u_char *packet_end;
51524007Ssam 
516*35411Skarels 	rxbuf += sizeof (struct ether_header);
517*35411Skarels 	cp = rxbuf;
518*35411Skarels 	packet_end = cp + totlen;
519*35411Skarels 	if (off) {
520*35411Skarels 		off += 2 * sizeof(u_short);
521*35411Skarels 		totlen -= 2 *sizeof(u_short);
522*35411Skarels 		cp = rxbuf + off;
523*35411Skarels 	}
524*35411Skarels 
525*35411Skarels 	MGETHDR(m, M_DONTWAIT, MT_DATA);
526*35411Skarels 	if (m == 0)
527*35411Skarels 		return (0);
528*35411Skarels 	m->m_pkthdr.rcvif = ifp;
529*35411Skarels 	m->m_pkthdr.len = totlen;
530*35411Skarels 	m->m_len = MHLEN;
531*35411Skarels 
53224007Ssam 	while (totlen > 0) {
533*35411Skarels 		if (top) {
534*35411Skarels 			MGET(m, M_DONTWAIT, MT_DATA);
535*35411Skarels 			if (m == 0) {
536*35411Skarels 				m_freem(top);
537*35411Skarels 				return (0);
538*35411Skarels 			}
539*35411Skarels 			m->m_len = MLEN;
540*35411Skarels 		}
541*35411Skarels 		len = min(totlen, (packet_end - cp));
542*35411Skarels 		if (len >= MINCLSIZE) {
543*35411Skarels 			MCLGET(m, M_DONTWAIT);
544*35411Skarels 			if (m->m_flags & M_EXT)
545*35411Skarels 				m->m_len = len = min(len, MCLBYTES);
54629563Ssam 			else
547*35411Skarels 				len = m->m_len;
54824007Ssam 		} else {
54925855Ssam 			/*
550*35411Skarels 			 * Place initial small packet/header at end of mbuf.
55125855Ssam 			 */
552*35411Skarels 			if (len < m->m_len) {
553*35411Skarels 				if (top == 0 && len + max_linkhdr <= m->m_len)
554*35411Skarels 					m->m_data += max_linkhdr;
555*35411Skarels 				m->m_len = len;
556*35411Skarels 			} else
557*35411Skarels 				len = m->m_len;
55825855Ssam 		}
559*35411Skarels 		mcp = mtod(m, u_char *);
56024007Ssam 		/*bcopy((caddr_t)cp, (caddr_t)mcp, len);*/
56124007Ssam 		/*cp += len; mcp += len;*/
56224007Ssam 		tlen = len;
56324007Ssam 		if (((int)mcp & 01) && ((int)cp & 01)) {
56424007Ssam 			/* source & destination at odd addresses */
56524007Ssam 			*mcp++ = *cp++;
56624007Ssam 			--tlen;
56724007Ssam 		}
56824007Ssam 		if (tlen > 1 && (((int)mcp&01) == 0) && (((int)cp&01) == 0)) {
56924007Ssam 			register short *s1, *s2;
57024007Ssam 			register int l;
57124007Ssam 
57224007Ssam 			s1 = (short *)mcp;
57324007Ssam 			s2 = (short *)cp;
57424007Ssam 			l = tlen >> 1;		/* count # of shorts */
57524007Ssam 			while (l-- > 0)		/* copy shorts */
57624007Ssam 				*s1++ = *s2++;
57724007Ssam 			tlen &= 1;		/* # remaining bytes */
57824007Ssam 			mcp = (u_char *)s1;
57924007Ssam 			cp = (u_char *)s2;
58024007Ssam 		}
58124007Ssam 		while (tlen-- > 0)
58224007Ssam 			*mcp++ = *cp++;
58324007Ssam 		*mp = m;
58424007Ssam 		mp = &m->m_next;
585*35411Skarels 		totlen -= len;
586*35411Skarels 		if (cp == packet_end)
587*35411Skarels 			cp = rxbuf;
58824007Ssam 	}
58924007Ssam 	return (top);
59024007Ssam }
59124007Ssam 
59229408Ssam /* backoff table masks */
59329408Ssam short	random_mask_tbl[16] = {
59429563Ssam 	0x0040, 0x00c0, 0x01c0, 0x03c0, 0x07c0, 0x0fc0, 0x1fc0, 0x3fc0,
59529563Ssam 	0x7fc0, 0xffc0, 0xffc0, 0xffc0, 0xffc0, 0xffc0, 0xffc0, 0xffc0
59629408Ssam };
59729408Ssam 
59824007Ssam acebakoff(is, txseg, retries)
59924007Ssam 	struct ace_softc *is;
60024007Ssam 	struct tx_segment *txseg;
60124007Ssam 	register int retries;
60224007Ssam {
60324007Ssam 	register short *pBakNum, random_num;
60424007Ssam 	short *pMask;
60524007Ssam 
60624007Ssam 	pMask = &random_mask_tbl[0];
60724007Ssam 	pBakNum = &txseg->tx_backoff[0];
60824007Ssam 	while (--retries >= 0) {
60924007Ssam 		random_num = (is->is_currnd = (is->is_currnd * 18741)-13849);
61024007Ssam 		random_num &= *pMask++;
61129563Ssam 		*pBakNum++ = random_num ^ (short)(0xff00 | 0x00fc);
61224007Ssam 	}
61324007Ssam }
61424007Ssam 
61524007Ssam /*
61624007Ssam  * Process an ioctl request.
61724007Ssam  */
61824007Ssam aceioctl(ifp, cmd, data)
61924007Ssam 	register struct ifnet *ifp;
62024007Ssam 	int cmd;
62124007Ssam 	caddr_t data;
62224007Ssam {
62325694Ssam 	register struct ifaddr *ifa = (struct ifaddr *)data;
62425855Ssam 	struct acedevice *addr;
62525694Ssam 	int s = splimp(), error = 0;
62624007Ssam 
62724007Ssam 	switch (cmd) {
62824007Ssam 
62924007Ssam 	case SIOCSIFADDR:
63025694Ssam 		ifp->if_flags |= IFF_UP;
63125855Ssam 		switch (ifa->ifa_addr.sa_family) {
63225855Ssam #ifdef INET
63325855Ssam 		case AF_INET:
63425855Ssam 			aceinit(ifp->if_unit);	/* before arpwhohas */
63525855Ssam 			((struct arpcom *)ifp)->ac_ipaddr =
63625855Ssam 				IA_SIN(ifa)->sin_addr;
63725855Ssam 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
63825855Ssam 			break;
63925855Ssam #endif
64025855Ssam #ifdef NS
64125855Ssam 		case AF_NS: {
64225937Ssam 			struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
64325937Ssam 			struct ace_softc *is = &ace_softc[ifp->if_unit];
64425855Ssam 
64525855Ssam 			if (!ns_nullhost(*ina)) {
64625855Ssam 				ifp->if_flags &= ~IFF_RUNNING;
64725855Ssam 				addr = (struct acedevice *)
64825937Ssam 				    aceinfo[ifp->if_unit]->ui_addr;
64925855Ssam 				movow(&addr->csr, CSR_RESET);
65025855Ssam 				DELAY(10000);
65125855Ssam 				/* set station address & copy addr to arp */
65229408Ssam 				acesetaddr(ifp->if_unit, addr,
65325855Ssam 				    ina->x_host.c_host);
65425855Ssam 			} else
65525937Ssam 				ina->x_host = *(union ns_host *)is->is_addr;
65625855Ssam 			aceinit(ifp->if_unit);
65725855Ssam 			break;
65825855Ssam 		}
65925855Ssam #endif
66025855Ssam 		default:
66125855Ssam 			aceinit(ifp->if_unit);
66225855Ssam 			break;
66325855Ssam 		}
66424007Ssam 		break;
66524007Ssam 
66625855Ssam 	case SIOCSIFFLAGS:
66725855Ssam 		if ((ifp->if_flags&IFF_UP) == 0 && ifp->if_flags&IFF_RUNNING) {
66825855Ssam 			addr = (struct acedevice *)
66925855Ssam 			    (aceinfo[ifp->if_unit]->ui_addr);
67025855Ssam 			movow(&addr->csr, CSR_RESET);
67125855Ssam 			ifp->if_flags &= ~IFF_RUNNING;
67225855Ssam 		} else if (ifp->if_flags&IFF_UP &&
67325855Ssam 		    (ifp->if_flags&IFF_RUNNING) == 0)
67425855Ssam 			aceinit(ifp->if_unit);
67524007Ssam 		break;
67624007Ssam 
67724007Ssam 	default:
67824007Ssam 		error = EINVAL;
67924007Ssam 	}
68024007Ssam 	splx(s);
68124007Ssam 	return (error);
68224007Ssam }
68324007Ssam 
68429408Ssam /*
68529408Ssam  * Set the on-board station address, then read it back
68629408Ssam  * to initialize the address used by ARP (among others).
68729408Ssam  */
68829408Ssam acesetaddr(unit, addr, station)
68929408Ssam 	short unit;
69029408Ssam 	struct acedevice *addr;
69130295Ssam 	u_char *station;
69229408Ssam {
69329408Ssam 	struct ace_softc *is = &ace_softc[unit];
69429408Ssam 	register short *wp, i;
69529408Ssam 
69629408Ssam 	for (wp = (short *)addr->station, i = 0; i < 6; i++)
69729408Ssam 		movow(wp++, ~*station++);
69829408Ssam 	for (wp = (short *)addr->station, i = 0; i < 6; i++)
69929408Ssam 		is->is_addr[i] = ~*wp++;
70029408Ssam 	printf("ace%d: hardware address %s\n", unit,
70129408Ssam 	    ether_sprintf(is->is_addr));
70229408Ssam }
70329408Ssam 
70429408Ssam /*
70529408Ssam  * Setup the device for use.  Initialize dual-ported memory,
70629408Ssam  * backoff parameters, and various other software state.
70729408Ssam  */
70825927Ssam acesetup(unit)
70924007Ssam 	int unit;
71024007Ssam {
71124007Ssam 	register struct ace_softc *is = &ace_softc[unit];
71225927Ssam 	register char *pData1;
71325694Ssam 	register short i;
71425927Ssam 	struct acedevice *addr;
71524007Ssam 
71625927Ssam 	bzero(is->is_dpm, 16384*2);
71724007Ssam 	is->is_currnd = 49123;
71825927Ssam 	addr = (struct acedevice *)aceinfo[unit]->ui_addr;
71924007Ssam 	is->is_segboundry = (addr->segb >> 11) & 0xf;
72025927Ssam 	pData1 = is->is_dpm + (is->is_segboundry << 11);
72124007Ssam 	for (i = SEG_MAX + 1 - is->is_segboundry; --i >= 0;) {
72224007Ssam 		acebakoff(is, (struct tx_segment *)pData1, 15);
72324007Ssam 		pData1 += sizeof (struct tx_segment);
72424007Ssam 	}
72524007Ssam 	is->is_eictr = 0;
72624007Ssam 	is->is_eoctr = is->is_txnext = is->is_segboundry;
72724007Ssam 	bzero((char *)&is->is_stats, sizeof (is->is_stats));
72824007Ssam }
72924007Ssam #endif
730