xref: /netbsd-src/sys/arch/mac68k/dev/if_ae.c (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
1 /*	$NetBSD: if_ae.c,v 1.16 1994/12/04 19:40:14 briggs Exp $	*/
2 
3 /*
4  * Device driver for National Semiconductor DS8390 based ethernet adapters.
5  *
6  * Based on original ISA bus driver by David Greenman, 29-April-1993
7  *
8  * Copyright (C) 1993, David Greenman. This software may be used, modified,
9  *   copied, distributed, and sold, in both source and binary form provided
10  *   that the above copyright and these terms are retained. Under no
11  *   circumstances is the author responsible for the proper functioning
12  *   of this software, nor does the author assume any responsibility
13  *   for damages incurred with its use.
14  *
15  * Adapted for MacBSD by Brad Parker <brad@fcr.com>
16  *
17  * Currently supports:
18  *	Apples NB Ethernet card
19  *	Interlan A310 Nubus Ethernet card
20  *	Cayman Systems GatorCard
21  *	Asante MacCon II/E
22  */
23 
24 /*
25  * $Id: if_ae.c,v 1.16 1994/12/04 19:40:14 briggs Exp $
26  */
27 
28 #include "ae.h"
29 /* bpfilter included here in case it is needed in future net includes */
30 #include "bpfilter.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/errno.h>
35 #include <sys/ioctl.h>
36 #include <sys/mbuf.h>
37 #include <sys/socket.h>
38 #include <sys/syslog.h>
39 
40 #include <net/if.h>
41 #include <net/if_dl.h>
42 #include <net/if_types.h>
43 #include <net/netisr.h>
44 
45 #ifdef INET
46 #include <netinet/in.h>
47 #include <netinet/in_systm.h>
48 #include <netinet/in_var.h>
49 #include <netinet/ip.h>
50 #include <netinet/if_ether.h>
51 #endif
52 
53 #ifdef NS
54 #include <netns/ns.h>
55 #include <netns/ns_if.h>
56 #endif
57 
58 #if NBPFILTER > 0
59 #include <net/bpf.h>
60 #include <net/bpfdesc.h>
61 #endif
62 
63 #include <sys/device.h>
64 #include "nubus.h"
65 #include "if_aereg.h"
66 
67 struct ae_device {
68 	struct device	ae_dev;
69 /*	struct nubusdev	ae_nu;
70 	struct intrhand	ae_ih;	*/
71 };
72 
73 /*
74  * ae_softc: per line info and status
75  */
76 struct	ae_softc {
77 	struct ae_device	*sc_ae;
78 
79 	struct	arpcom arpcom;	/* ethernet common */
80 
81 	char	*type_str;	/* pointer to type string */
82 	u_char	vendor;		/* interface vendor */
83 	u_char	type;		/* interface type code */
84 	u_char	regs_rev;	/* registers are reversed */
85 
86 #define	REG_MAP(sc, reg)	((sc)->regs_rev ? (0x0f-(reg))<<2 : (reg)<<2)
87 #define NIC_GET(sc, reg)	((sc)->nic_addr[REG_MAP(sc, reg)])
88 #define NIC_PUT(sc, reg, val)	((sc)->nic_addr[REG_MAP(sc, reg)] = (val))
89 	volatile caddr_t nic_addr; /* NIC (DS8390) I/O bus address */
90 	caddr_t	rom_addr;	/* on board prom address */
91 	caddr_t	smem_start;	/* shared memory start address */
92 	caddr_t	smem_end;	/* shared memory end address */
93 	u_long	smem_size;	/* total shared memory size */
94 	u_char	smem_wr_short;	/* card memory requires int16 writes */
95 	caddr_t	smem_ring;	/* start of RX ring-buffer (in smem) */
96 
97 	caddr_t	bpf;		/* BPF "magic cookie" */
98 
99 	u_char	xmit_busy;	/* transmitter is busy */
100 	u_char	txb_cnt;	/* Number of transmit buffers */
101 	u_char	txb_next;	/* Pointer to next buffer ready to xmit */
102 	u_short	txb_next_len;	/* next xmit buffer length */
103 	u_char	data_buffered;	/* data has been buffered in interface mem */
104 	u_char	tx_page_start;	/* first page of TX buffer area */
105 
106 	u_char	rec_page_start;	/* first page of RX ring-buffer */
107 	u_char	rec_page_stop;	/* last page of RX ring-buffer */
108 	u_char	next_packet;	/* pointer to next unread RX packet */
109 } ae_softc[NAE];
110 
111 void	ae_find(), ae_attach();
112 int	ae_init(), aeintr(), ae_ioctl(), ae_probe(),
113 	ae_start(), ae_reset(), ae_watchdog();
114 
115 struct cfdriver aecd =
116 { NULL, "ae", ae_probe, ae_attach, DV_IFNET, sizeof(struct ae_device), NULL, 0 };
117 
118 static void ae_stop();
119 static inline void ae_rint();
120 static inline void ae_xmit();
121 static inline char *ae_ring_copy();
122 
123 extern int ether_output();
124 
125 #define	ETHER_MIN_LEN	64
126 #define ETHER_MAX_LEN	1518
127 #define	ETHER_ADDR_LEN	6
128 #define	ETHER_HDR_SIZE	14
129 
130 char ae_name[] = "8390 Nubus Ethernet card";
131 static char zero = 0;
132 static u_char ones = 0xff;
133 
134 struct vendor_S {
135 	char	*manu;
136 	int	len;
137 	int	vendor;
138 } vend[] = {
139 	{ "Apple", 5, AE_VENDOR_APPLE },
140 	{ "3Com",  4, AE_VENDOR_APPLE },
141 	{ "Dayna", 5, AE_VENDOR_DAYNA },
142 	{ "Inter", 5, AE_VENDOR_INTERLAN },
143 	{ "Asant", 5, AE_VENDOR_ASANTE },
144 };
145 
146 static int numvend = sizeof(vend)/sizeof(vend[0]);
147 
148 /*
149  * XXX These two should be moved to locore, and maybe changed to use shorts
150  * instead of bytes.  The reason for these is that bcopy and bzero use longs,
151  * which the ethernet cards can't handle.
152  */
153 
154 void
155 bszero (u_short *addr, int len)
156 {
157 	while (len--) {
158 		*addr++ = 0;
159 	}
160 }
161 
162 void
163 bbcopy (char *src, char *dest, int len)
164 {
165 	while (len--) {
166 		*dest++ = *src++;
167 	}
168 }
169 
170 /*
171 	short copy; assume destination is always aligned
172 	and last byte of odd length copy is not important
173 */
174 
175 void
176 bscopy (char *src, char *dest, int len)
177 {
178 	u_short *d = (u_short *)dest;
179 	u_short *s = (u_short *)src;
180 	char b1, b2;
181 
182 	/* odd case, src addr is unaligned */
183 	if ( ((u_long)src) & 1 ) {
184 		while (len > 0) {
185 			b1 = *src++;
186 			b2 = len > 1 ? *src++ : (*d & 0xff);
187 			*d++ = (b1 << 8) | b2;
188 			len -= 2;
189 		}
190 		return;
191 	}
192 
193 	/* normal case, aligned src & dst */
194 	while (len > 0) {
195 		*d++ = *s++;
196 		len -= 2;
197 	}
198 }
199 
200 void
201 ae_id_card(nu, sc)
202 	struct nubus_hw	*nu;
203 	struct ae_softc	*sc;
204 {
205 	int	i;
206 
207 	/*
208 	 * Try to determine what type of card this is...
209 	 */
210 	sc->vendor = AE_VENDOR_UNKNOWN;
211 	for (i=0 ; i<numvend ; i++) {
212 		if (!strncmp(nu->Slot.manufacturer, vend[i].manu, vend[i].len))
213 		{
214 			sc->vendor = vend[i].vendor;
215 			break;
216 		}
217 	}
218 	sc->type_str = (char *) (nu->Slot.manufacturer);
219 
220 }
221 
222 int
223 ae_size_card_memory(sc)
224 	struct ae_softc	*sc;
225 {
226 	u_short *p;
227 	u_short i1, i2, i3, i4;
228 	int size;
229 
230 	p = (u_short *)sc->smem_start;
231 
232 	/*
233 	 * very simple size memory, assuming it's installed in 8k
234 	 * banks; also assume it will generally mirror in upper banks
235 	 * if not installed.
236 	 */
237 	i1 = (8192*0)/2;
238 	i2 = (8192*1)/2;
239 	i3 = (8192*2)/2;
240 	i4 = (8192*3)/2;
241 
242 	p[i1] = 0x1111;
243 	p[i2] = 0x2222;
244 	p[i3] = 0x3333;
245 	p[i4] = 0x4444;
246 
247 	size = 0;
248 	if (p[i1] == 0x1111 && p[i2] == 0x2222 &&
249 	    p[i3] == 0x3333 && p[i4] == 0x4444)
250 		size = 8192*4;
251 	else
252 		if ((p[i1] == 0x1111 && p[i2] == 0x2222) ||
253 		    (p[i1] == 0x3333 && p[i2] == 0x4444))
254 			size = 8192*2;
255 		else
256 			if (p[i1] == 0x1111 || p[i1] == 0x4444)
257 				size = 8192;
258 
259 	if (size == 0)
260 	  return 0;
261 
262 	return size;
263 }
264 
265 int
266 ae_probe(parent, cf, aux)
267 	struct cfdriver	*parent;
268 	struct cfdata	*cf;
269 	void		*aux;
270 {
271 	register struct nubus_hw *nu = (struct nubus_hw *) aux;
272 	struct ae_softc *sc = &ae_softc[cf->cf_unit];
273 	int i, memsize;
274 	int flags = 0;
275 
276 	if (nu->Slot.type != NUBUS_NETWORK)
277 		return 0;
278 
279 	ae_id_card(nu, sc);
280 
281 	sc->regs_rev = 0;
282 	sc->smem_wr_short = 0;
283 
284 	switch (sc->vendor) {
285 	      case AE_VENDOR_INTERLAN:
286 		sc->nic_addr = nu->addr + GC_NIC_OFFSET;
287 		sc->rom_addr = nu->addr + GC_ROM_OFFSET;
288 		sc->smem_start = nu->addr + GC_DATA_OFFSET;
289 		if ((memsize = ae_size_card_memory(sc)) == 0)
290 			return 0;
291 
292 		/* reset the NIC chip */
293 		*((caddr_t)nu->addr + GC_RESET_OFFSET) = (char)zero;
294 
295 		/* Get station address from on-board ROM */
296 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
297 			sc->arpcom.ac_enaddr[i] = *(sc->rom_addr + i*4);
298 		break;
299 
300 	      case AE_VENDOR_ASANTE:
301 		/* memory writes require *(u_short *) */
302 		sc->smem_wr_short = 1;
303 		/* otherwise, pretend to be an apple card (fall through) */
304 
305 	      case AE_VENDOR_APPLE:
306 		sc->regs_rev = 1;
307 		sc->nic_addr = nu->addr + AE_NIC_OFFSET;
308 		sc->rom_addr = nu->addr + AE_ROM_OFFSET;
309 		sc->smem_start = nu->addr + AE_DATA_OFFSET;
310 		if ((memsize = ae_size_card_memory(sc)) == 0)
311 			return 0;
312 
313 		/* Get station address from on-board ROM */
314 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
315 			sc->arpcom.ac_enaddr[i] = *(sc->rom_addr + i*2);
316 		break;
317 
318 	      case AE_VENDOR_DAYNA:
319 		printf("We think we are a Dayna card, but ");
320 		sc->nic_addr = nu->addr + DP_NIC_OFFSET;
321 		sc->rom_addr = nu->addr + DP_ROM_OFFSET;
322 		sc->smem_start = nu->addr + DP_DATA_OFFSET;
323 		memsize = 8192;
324 
325 		/* Get station address from on-board ROM */
326 		for (i = 0; i < ETHER_ADDR_LEN; ++i)
327 			sc->arpcom.ac_enaddr[i] = *(sc->rom_addr + i*2);
328 		printf("it is dangerous to continue.\n");
329 		return 0; /* Since we don't work yet... */
330 		break;
331 
332 	      default:
333 		return 0;
334 		break;
335 	}
336 
337 	/*
338 	 * allocate one xmit buffer if < 16k, two buffers otherwise
339 	 */
340 	if ((memsize < 16384) || (flags & AE_FLAGS_NO_DOUBLE_BUFFERING)) {
341 		sc->smem_ring =
342 		  sc->smem_start + (AE_PAGE_SIZE * AE_TXBUF_SIZE);
343 		sc->txb_cnt = 1;
344 		sc->rec_page_start = AE_TXBUF_SIZE;
345 	} else {
346 		sc->smem_ring =
347 		  sc->smem_start + (AE_PAGE_SIZE * AE_TXBUF_SIZE * 2);
348 		sc->txb_cnt = 2;
349 		sc->rec_page_start = AE_TXBUF_SIZE * 2;
350 	}
351 
352 	sc->smem_size = memsize;
353 	sc->smem_end = sc->smem_start + memsize;
354 	sc->rec_page_stop = memsize / AE_PAGE_SIZE;
355 	sc->tx_page_start = 0;
356 
357 	/*
358 	 * Now zero memory and verify that it is clear
359 	 */
360 	bszero((u_short *)sc->smem_start, memsize / 2);
361 
362 	for (i = 0; i < memsize; ++i)
363 		if (sc->smem_start[i]) {
364 	        	printf("ae: failed to clear shared memory at %x\n",
365 			       sc->smem_start + i);
366 
367 			return(0);
368 		}
369 
370 #ifdef DEBUG_PRINT
371 	printf("nic_addr %x, rom_addr %x\n",
372 		sc->nic_addr, sc->rom_addr);
373 	printf("smem_size %d\n", sc->smem_size);
374 	printf("smem_start %x, smem_ring %x, smem_end %x\n",
375 		sc->smem_start, sc->smem_ring, sc->smem_end);
376 	printf("phys address %02x:%02x:%02x:%02x:%02x:%02x\n",
377 		sc->arpcom.ac_enaddr[0],
378 		sc->arpcom.ac_enaddr[1],
379 		sc->arpcom.ac_enaddr[2],
380 		sc->arpcom.ac_enaddr[3],
381 		sc->arpcom.ac_enaddr[4],
382 		sc->arpcom.ac_enaddr[5]);
383 #endif
384 
385 	return(1);
386 }
387 
388 /*
389  * Install interface into kernel networking data structures
390  */
391 void
392 ae_attach(parent, self, aux)
393 	struct cfdriver	*parent, *self;
394 	void		*aux;
395 {
396 	struct nubus_hw	*nu = aux;
397 	struct ae_device *ae = (struct ae_device *) self;
398 	struct ae_softc *sc = &ae_softc[ae->ae_dev.dv_unit];
399 	struct cfdata *cf = ae->ae_dev.dv_cfdata;
400 	struct ifnet *ifp = &sc->arpcom.ac_if;
401 	struct ifaddr *ifa;
402 	struct sockaddr_dl *sdl;
403 
404 	sc->sc_ae = ae;
405 
406 	/*
407 	 * Set interface to stopped condition (reset)
408 	 */
409 	ae_stop(sc);
410 
411 	/*
412 	 * Initialize ifnet structure
413 	 */
414 	ifp->if_unit = ae->ae_dev.dv_unit;
415 	ifp->if_name = aecd.cd_name;
416 	ifp->if_mtu = ETHERMTU;
417 	ifp->if_output = ether_output;
418 	ifp->if_start = ae_start;
419 	ifp->if_ioctl = ae_ioctl;
420 	ifp->if_reset = ae_reset;
421 	ifp->if_watchdog = ae_watchdog;
422 	ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS);
423 
424 #if 0
425 	/*
426 	 * Set default state for ALTPHYS flag (used to disable the transceiver
427 	 * for AUI operation), based on compile-time config option.
428 	 */
429 	if (cf->cf_flags & AE_FLAGS_DISABLE_TRANSCEIVER)
430 		ifp->if_flags |= IFF_ALTPHYS;
431 #endif
432 
433 	/*
434 	 * Attach the interface
435 	 */
436 	if_attach(ifp);
437 
438 	/*
439 	 * Search down the ifa address list looking for the AF_LINK type entry
440 	 */
441  	ifa = ifp->if_addrlist;
442 	while ((ifa != 0) && (ifa->ifa_addr != 0) &&
443 	    (ifa->ifa_addr->sa_family != AF_LINK))
444 		ifa = ifa->ifa_next;
445 	/*
446 	 * If we find an AF_LINK type entry we fill in the hardware address.
447 	 *	This is useful for netstat(1) to keep track of which interface
448 	 *	is which.
449 	 */
450 	if ((ifa != 0) && (ifa->ifa_addr != 0)) {
451 		/*
452 		 * Fill in the link-level address for this interface
453 		 */
454 		sdl = (struct sockaddr_dl *)ifa->ifa_addr;
455 		sdl->sdl_type = IFT_ETHER;
456 		sdl->sdl_alen = ETHER_ADDR_LEN;
457 		sdl->sdl_slen = 0;
458 		bbcopy(sc->arpcom.ac_enaddr, LLADDR(sdl), ETHER_ADDR_LEN);
459 	}
460 
461 	/*
462 	 * Print additional info when attached
463 	 */
464 	printf(": address %s, ", ether_sprintf(sc->arpcom.ac_enaddr));
465 
466 	if (sc->type_str && (*sc->type_str != 0))
467 		printf("type %s", sc->type_str);
468 	else
469 		printf("type unknown (0x%x)", sc->type);
470 
471 	printf(", %dk mem", sc->smem_size / 1024);
472 
473 	printf("\n");
474 
475 	/*
476 	 * If BPF is in the kernel, call the attach for it
477 	 */
478 #if NBPFILTER > 0
479 	bpfattach(&sc->bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
480 #endif
481 }
482 
483 /*
484  * Reset interface.
485  */
486 int
487 ae_reset(sc)
488 	struct ae_softc *sc;
489 {
490 	int s;
491 
492 	s = splnet();
493 
494 	/*
495 	 * Stop interface and re-initialize.
496 	 */
497 	ae_stop(sc);
498 	ae_init(sc);
499 
500 	(void) splx(s);
501 }
502 
503 /*
504  * Take interface offline.
505  */
506 void
507 ae_stop(sc)
508 	struct ae_softc *sc;
509 {
510 	int n = 5000;
511 
512 	/*
513 	 * Stop everything on the interface, and select page 0 registers.
514 	 */
515 	NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STP);
516 
517 	/*
518 	 * Wait for interface to enter stopped state, but limit # of checks
519 	 *	to 'n' (about 5ms). It shouldn't even take 5us on modern
520 	 *	DS8390's, but just in case it's an old one.
521 	 */
522 	while (((NIC_GET(sc, AE_P0_ISR) & AE_ISR_RST) == 0) && --n);
523 }
524 
525 /*
526  * Device timeout/watchdog routine. Entered if the device neglects to
527  *	generate an interrupt after a transmit has been started on it.
528  */
529 int
530 ae_watchdog(unit)
531 	short unit;
532 {
533 	struct ae_softc *sc = &ae_softc[unit];
534 
535 	log(LOG_ERR, "ae%d: device timeout\n", unit);
536 	ae_reset(sc);
537 }
538 
539 /*
540  * Initialize device.
541  */
542 ae_init(sc)
543 	struct ae_softc *sc;
544 {
545 	struct ifnet *ifp = &sc->arpcom.ac_if;
546 	int i, s;
547 	u_char	command;
548 
549 	/* address not known */
550 	if (ifp->if_addrlist == (struct ifaddr *)0) return;
551 
552 	/*
553 	 * Initialize the NIC in the exact order outlined in the NS manual.
554 	 *	This init procedure is "mandatory"...don't change what or when
555 	 *	things happen.
556 	 */
557 	s = splnet();
558 
559 	/* reset transmitter flags */
560 	sc->data_buffered = 0;
561 	sc->xmit_busy = 0;
562 	sc->arpcom.ac_if.if_timer = 0;
563 
564 	sc->txb_next = 0;
565 
566 	/* This variable is used below - don't move this assignment */
567 	sc->next_packet = sc->rec_page_start + 1;
568 
569 #ifdef DEBUG_PRINT
570 	printf("page_start %d, page_stop %d, next %d\n",
571 		sc->rec_page_start, sc->rec_page_stop, sc->next_packet);
572 #endif
573 
574 	/*
575 	 * Set interface for page 0, Remote DMA complete, Stopped
576 	 */
577 	NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STP);
578 
579 	/*
580 	 * Set FIFO threshold to 4, No auto-init Remote DMA, Burst mode,
581 	 *	byte order=80x86, word-wide DMA xfers,
582 	 */
583 	NIC_PUT(sc, AE_P0_DCR, AE_DCR_FT1|AE_DCR_BMS|AE_DCR_WTS);
584 
585 	/*
586 	 * Clear Remote Byte Count Registers
587 	 */
588 	NIC_PUT(sc, AE_P0_RBCR0, zero);
589 	NIC_PUT(sc, AE_P0_RBCR1, zero);
590 
591 	/*
592 	 * Enable reception of broadcast packets
593 	 */
594 	NIC_PUT(sc, AE_P0_RCR, AE_RCR_AB);
595 
596 	/*
597 	 * Place NIC in internal loopback mode
598 	 */
599 	NIC_PUT(sc, AE_P0_TCR, AE_TCR_LB0);
600 
601 	/*
602 	 * Initialize transmit/receive (ring-buffer) Page Start
603 	 */
604 	NIC_PUT(sc, AE_P0_TPSR, sc->tx_page_start);
605 	NIC_PUT(sc, AE_P0_PSTART, sc->rec_page_start);
606 
607 	/*
608 	 * Initialize Receiver (ring-buffer) Page Stop and Boundry
609 	 */
610 	NIC_PUT(sc, AE_P0_PSTOP, sc->rec_page_stop);
611 	NIC_PUT(sc, AE_P0_BNRY, sc->rec_page_start);
612 
613 	/*
614 	 * Clear all interrupts. A '1' in each bit position clears the
615 	 *	corresponding flag.
616 	 */
617 	NIC_PUT(sc, AE_P0_ISR, ones);
618 
619 	/* make sure interrupts are vectored to us */
620 	add_nubus_intr((int)sc->rom_addr & 0xFF000000, aeintr, sc - ae_softc);
621 
622 	/*
623 	 * Enable the following interrupts: receive/transmit complete,
624 	 *	receive/transmit error, and Receiver OverWrite.
625 	 *
626 	 * Counter overflow and Remote DMA complete are *not* enabled.
627 	 */
628 	NIC_PUT(sc, AE_P0_IMR,
629 		AE_IMR_PRXE|AE_IMR_PTXE|AE_IMR_RXEE|AE_IMR_TXEE|AE_IMR_OVWE);
630 
631 	/*
632 	 * Program Command Register for page 1
633 	 */
634 	NIC_PUT(sc, AE_P0_CR, AE_CR_PAGE_1|AE_CR_RD2|AE_CR_STP);
635 
636 	/*
637 	 * Copy out our station address
638 	 */
639 	for (i = 0; i < ETHER_ADDR_LEN; ++i)
640 		NIC_PUT(sc, AE_P1_PAR0 + i, sc->arpcom.ac_enaddr[i]);
641 
642 #if NBPFILTER > 0
643 	/*
644 	 * Initialize multicast address hashing registers to accept
645 	 *	 all multicasts (only used when in promiscuous mode)
646 	 */
647 	for (i = 0; i < 8; ++i)
648 		NIC_PUT(sc, AE_P1_MAR0 + i, 0xff);
649 #endif
650 
651 	/*
652 	 * Set Current Page pointer to next_packet (initialized above)
653 	 */
654 	NIC_PUT(sc, AE_P1_CURR, sc->next_packet);
655 
656 	/*
657 	 * Set Command Register for page 0, Remote DMA complete,
658 	 * 	and interface Start.
659 	 */
660 	NIC_PUT(sc, AE_P1_CR, AE_CR_RD2|AE_CR_STA);
661 
662 	/*
663 	 * Take interface out of loopback
664 	 */
665 	NIC_PUT(sc, AE_P0_TCR, zero);
666 
667 	/*
668 	 * Set 'running' flag, and clear output active flag.
669 	 */
670 	ifp->if_flags |= IFF_RUNNING;
671 	ifp->if_flags &= ~IFF_OACTIVE;
672 
673 	/*
674 	 * ...and attempt to start output
675 	 */
676 	ae_start(ifp);
677 
678 	(void) splx(s);
679 }
680 
681 /*
682  * This routine actually starts the transmission on the interface
683  */
684 static inline void ae_xmit(ifp)
685 	struct ifnet *ifp;
686 {
687 	struct ae_softc *sc = &ae_softc[ifp->if_unit];
688 	u_short len = sc->txb_next_len;
689 
690 	/*
691 	 * Set NIC for page 0 register access
692 	 */
693 	NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STA);
694 
695 	/*
696 	 * Set TX buffer start page
697 	 */
698 	NIC_PUT(sc, AE_P0_TPSR, sc->tx_page_start +
699 		sc->txb_next * AE_TXBUF_SIZE);
700 
701 	/*
702 	 * Set TX length
703 	 */
704 	NIC_PUT(sc, AE_P0_TBCR0, len & 0xff);
705 	NIC_PUT(sc, AE_P0_TBCR1, len >> 8);
706 
707 	/*
708 	 * Set page 0, Remote DMA complete, Transmit Packet, and *Start*
709 	 */
710 	NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_TXP|AE_CR_STA);
711 
712 	sc->xmit_busy = 1;
713 	sc->data_buffered = 0;
714 
715 	/*
716 	 * Switch buffers if we are doing double-buffered transmits
717 	 */
718 	if ((sc->txb_next == 0) && (sc->txb_cnt > 1))
719 		sc->txb_next = 1;
720 	else
721 		sc->txb_next = 0;
722 
723 	/*
724 	 * Set a timer just in case we never hear from the board again
725 	 */
726 	ifp->if_timer = 2;
727 }
728 
729 /*
730  * Start output on interface.
731  * We make two assumptions here:
732  *  1) that the current priority is set to splnet _before_ this code
733  *     is called *and* is returned to the appropriate priority after
734  *     return
735  *  2) that the IFF_OACTIVE flag is checked before this code is called
736  *     (i.e. that the output part of the interface is idle)
737  */
738 int
739 ae_start(ifp)
740 	struct ifnet *ifp;
741 {
742 	struct ae_softc *sc = &ae_softc[ifp->if_unit];
743 	struct mbuf *m0, *m;
744 	caddr_t buffer;
745 	int len;
746 
747 outloop:
748 	/*
749 	 * See if there is room to send more data (i.e. one or both of the
750 	 *	buffers is empty).
751 	 */
752 	if (sc->data_buffered)
753 		if (sc->xmit_busy) {
754 			/*
755 			 * No room. Indicate this to the outside world
756 			 *	and exit.
757 			 */
758 			ifp->if_flags |= IFF_OACTIVE;
759 			return;
760 		} else {
761 			/*
762 			 * Data is buffered, but we're not transmitting, so
763 			 *	start the xmit on the buffered data.
764 			 * Note that ae_xmit() resets the data_buffered flag
765 			 *	before returning.
766 			 */
767 			ae_xmit(ifp);
768 		}
769 
770 	IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m);
771 	if (m == 0) {
772 	/*
773 	 * The following isn't pretty; we are using the !OACTIVE flag to
774 	 * indicate to the outside world that we can accept an additional
775 	 * packet rather than that the transmitter is _actually_
776 	 * active. Indeed, the transmitter may be active, but if we haven't
777 	 * filled the secondary buffer with data then we still want to
778 	 * accept more.
779 	 * Note that it isn't necessary to test the data_buffered flag -
780 	 * we wouldn't have tried to de-queue the packet in the first place
781 	 * if it was set.
782 	 */
783 		ifp->if_flags &= ~IFF_OACTIVE;
784 		return;
785 	}
786 
787 	/*
788 	 * Copy the mbuf chain into the transmit buffer
789 	 */
790 	buffer = sc->smem_start + (sc->txb_next * AE_TXBUF_SIZE * AE_PAGE_SIZE);
791 	len = 0;
792 	for (m0 = m; m != 0; m = m->m_next) {
793 		/*printf("ae: copy %d bytes @ %x\n", m->m_len, buffer);*/
794 		bscopy(mtod(m, caddr_t), buffer, m->m_len);
795 		buffer += m->m_len;
796        		len += m->m_len;
797 	}
798 	if (len & 1) len++;
799 
800 	sc->txb_next_len = max(len, ETHER_MIN_LEN);
801 
802 	if (sc->txb_cnt > 1)
803 		/*
804 		 * only set 'buffered' flag if doing multiple buffers
805 		 */
806 		sc->data_buffered = 1;
807 
808 	if (sc->xmit_busy == 0)
809 		ae_xmit(ifp);
810 	/*
811 	 * If there is BPF support in the configuration, tap off here.
812 	 *   The following has support for converting trailer packets
813 	 *   back to normal.
814 	 */
815 #if NBPFILTER > 0
816 	if (sc->bpf) {
817 		u_short etype;
818 		int off, datasize, resid;
819 		struct ether_header *eh;
820 		struct trailer_header {
821 			u_short ether_type;
822 			u_short ether_residual;
823 		} trailer_header;
824 		char ether_packet[ETHER_MAX_LEN];
825 		char *ep;
826 
827 		ep = ether_packet;
828 
829 		/*
830 		 * We handle trailers below:
831 		 * Copy ether header first, then residual data,
832 		 * then data. Put all this in a temporary buffer
833 		 * 'ether_packet' and send off to bpf. Since the
834 		 * system has generated this packet, we assume
835 		 * that all of the offsets in the packet are
836 		 * correct; if they're not, the system will almost
837 		 * certainly crash in m_copydata.
838 		 * We make no assumptions about how the data is
839 		 * arranged in the mbuf chain (i.e. how much
840 		 * data is in each mbuf, if mbuf clusters are
841 		 * used, etc.), which is why we use m_copydata
842 		 * to get the ether header rather than assume
843 		 * that this is located in the first mbuf.
844 		 */
845 		/* copy ether header */
846 		m_copydata(m0, 0, sizeof(struct ether_header), ep);
847 		eh = (struct ether_header *) ep;
848 		ep += sizeof(struct ether_header);
849 		etype = ntohs(eh->ether_type);
850 		if (etype >= ETHERTYPE_TRAIL &&
851 		    etype < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
852 			datasize = ((etype - ETHERTYPE_TRAIL) << 9);
853 			off = datasize + sizeof(struct ether_header);
854 
855 			/* copy trailer_header into a data structure */
856 			m_copydata(m0, off, sizeof(struct trailer_header),
857 				&trailer_header.ether_type);
858 
859 			/* copy residual data */
860 			m_copydata(m0, off+sizeof(struct trailer_header),
861 				resid = ntohs(trailer_header.ether_residual) -
862 				sizeof(struct trailer_header), ep);
863 			ep += resid;
864 
865 			/* copy data */
866 			m_copydata(m0, sizeof(struct ether_header),
867 				datasize, ep);
868 			ep += datasize;
869 
870 			/* restore original ether packet type */
871 			eh->ether_type = trailer_header.ether_type;
872 
873 			bpf_tap(sc->bpf, ether_packet, ep - ether_packet);
874 		} else
875 			bpf_mtap(sc->bpf, m0);
876 	}
877 #endif
878 
879 	m_freem(m0);
880 
881 	/*
882 	 * If we are doing double-buffering, a buffer might be free to
883 	 *	fill with another packet, so loop back to the top.
884 	 */
885 	if (sc->txb_cnt > 1)
886 		goto outloop;
887 	else {
888 		ifp->if_flags |= IFF_OACTIVE;
889 		return;
890 	}
891 }
892 
893 /*
894  * Ethernet interface receiver interrupt.
895  */
896 static inline void
897 ae_rint(unit)
898 	int unit;
899 {
900 	register struct ae_softc *sc = &ae_softc[unit];
901 	u_char boundry, current;
902 	u_short len;
903 	struct ae_ring *packet_ptr;
904 
905 	/*
906 	 * Set NIC to page 1 registers to get 'current' pointer
907 	 */
908 	NIC_PUT(sc, AE_P0_CR, AE_CR_PAGE_1|AE_CR_RD2|AE_CR_STA);
909 
910 	/*
911 	 * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
912 	 *	it points to where new data has been buffered. The 'CURR'
913 	 *	(current) register points to the logical end of the ring-buffer
914 	 *	- i.e. it points to where additional new data will be added.
915 	 *	We loop here until the logical beginning equals the logical
916 	 *	end (or in other words, until the ring-buffer is empty).
917 	 */
918 	while (sc->next_packet != NIC_GET(sc, AE_P1_CURR)) {
919 
920 		/* get pointer to this buffer header structure */
921 		packet_ptr = (struct ae_ring *)(sc->smem_ring +
922 			 (sc->next_packet - sc->rec_page_start) * AE_PAGE_SIZE);
923 
924 		/*
925 		 * The byte count includes the FCS - Frame Check Sequence (a
926 		 *	32 bit CRC).
927 		 */
928 		len = packet_ptr->count[0] | (packet_ptr->count[1] << 8);
929 		if ((len >= ETHER_MIN_LEN) && (len <= ETHER_MAX_LEN)) {
930 			/*
931 			 * Go get packet. len - 4 removes CRC from length.
932 			 * (packet_ptr + 1) points to data just after the
933 			 * packet ring header (+4 bytes)
934 			 */
935 			ae_get_packet(sc, (caddr_t)(packet_ptr + 1), len - 4);
936 			++sc->arpcom.ac_if.if_ipackets;
937 		} else {
938 			/*
939 			 * Really BAD...probably indicates that the ring
940 			 * pointers are corrupted. Also seen on early rev
941 			 * chips under high load - the byte order of the
942 			 * length gets switched.
943 			 */
944 			log(LOG_ERR,
945 				"ae%d: shared memory corrupt - invalid packet length %d\n",
946 				unit, len);
947 			ae_reset(sc);
948 			return;
949 		}
950 
951 		/*
952 		 * Update next packet pointer
953 		 */
954 		sc->next_packet = packet_ptr->next_packet;
955 
956 		/*
957 		 * Update NIC boundry pointer - being careful to keep it
958 		 *	one buffer behind. (as recommended by NS databook)
959 		 */
960 		boundry = sc->next_packet - 1;
961 		if (boundry < sc->rec_page_start)
962 			boundry = sc->rec_page_stop - 1;
963 
964 		/*
965 		 * Set NIC to page 0 registers to update boundry register
966 		 */
967 		NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STA);
968 
969 		NIC_PUT(sc, AE_P0_BNRY, boundry);
970 
971 		/*
972 		 * Set NIC to page 1 registers before looping to top
973 		 * (prepare to get 'CURR' current pointer)
974 		 */
975 		NIC_PUT(sc, AE_P0_CR, AE_CR_PAGE_1|AE_CR_RD2|AE_CR_STA);
976 	}
977 }
978 
979 /*
980  * Ethernet interface interrupt processor
981  */
982 int
983 aeintr(unit)
984 	int unit;
985 {
986 	struct ae_softc *sc = &ae_softc[unit];
987 	u_char isr;
988 
989 	/*
990 	 * Set NIC to page 0 registers
991 	 */
992 	NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STA);
993 
994 	/*
995 	 * loop until there are no more new interrupts
996 	 */
997 	while (isr = NIC_GET(sc, AE_P0_ISR)) {
998 
999 		/*
1000 		 * reset all the bits that we are 'acknowledging'
1001 		 *	by writing a '1' to each bit position that was set
1002 		 * (writing a '1' *clears* the bit)
1003 		 */
1004 		NIC_PUT(sc, AE_P0_ISR, isr);
1005 
1006 		/*
1007 		 * Handle transmitter interrupts. Handle these first
1008 		 *	because the receiver will reset the board under
1009 		 *	some conditions.
1010 		 */
1011 		if (isr & (AE_ISR_PTX|AE_ISR_TXE)) {
1012 			u_char collisions = NIC_GET(sc, AE_P0_NCR);
1013 
1014 			/*
1015 			 * Check for transmit error. If a TX completed with an
1016 			 * error, we end up throwing the packet away. Really
1017 			 * the only error that is possible is excessive
1018 			 * collisions, and in this case it is best to allow the
1019 			 * automatic mechanisms of TCP to backoff the flow. Of
1020 			 * course, with UDP we're screwed, but this is expected
1021 			 * when a network is heavily loaded.
1022 			 */
1023 			if (isr & AE_ISR_TXE) {
1024 
1025 				/*
1026 				 * Excessive collisions (16)
1027 				 */
1028 				if ((NIC_GET(sc, AE_P0_TSR) & AE_TSR_ABT)
1029 					&& (collisions == 0)) {
1030 					/*
1031 					 *    When collisions total 16, the
1032 					 * P0_NCR will indicate 0, and the
1033 					 * TSR_ABT is set.
1034 					 */
1035 					collisions = 16;
1036 				}
1037 
1038 				/*
1039 				 * update output errors counter
1040 				 */
1041 				++sc->arpcom.ac_if.if_oerrors;
1042 			} else {
1043 				/*
1044 				 * Update total number of successfully
1045 				 * 	transmitted packets.
1046 				 */
1047 				++sc->arpcom.ac_if.if_opackets;
1048 			}
1049 
1050 			/*
1051 			 * reset tx busy and output active flags
1052 			 */
1053 			sc->xmit_busy = 0;
1054 			sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
1055 
1056 			/*
1057 			 * clear watchdog timer
1058 			 */
1059 			sc->arpcom.ac_if.if_timer = 0;
1060 
1061 			/*
1062 			 * Add in total number of collisions on last
1063 			 *	transmission.
1064 			 */
1065 			sc->arpcom.ac_if.if_collisions += collisions;
1066 
1067 			/*
1068 			 * If data is ready to transmit, start it transmitting,
1069 			 *	otherwise defer until after handling receiver
1070 			 */
1071 			if (sc->data_buffered)
1072 				ae_xmit(&sc->arpcom.ac_if);
1073 		}
1074 
1075 		/*
1076 		 * Handle receiver interrupts
1077 		 */
1078 		if (isr & (AE_ISR_PRX|AE_ISR_RXE|AE_ISR_OVW)) {
1079 		    /*
1080 		     * Overwrite warning. In order to make sure that a lockup
1081 		     *	of the local DMA hasn't occurred, we reset and
1082 		     *	re-init the NIC. The NSC manual suggests only a
1083 		     *	partial reset/re-init is necessary - but some
1084 		     *	chips seem to want more. The DMA lockup has been
1085 		     *	seen only with early rev chips - Methinks this
1086 		     *	bug was fixed in later revs. -DG
1087 		     */
1088 			if (isr & AE_ISR_OVW) {
1089 				++sc->arpcom.ac_if.if_ierrors;
1090 				log(LOG_WARNING,
1091 					"ae%d: warning - receiver ring buffer overrun\n",
1092 					unit);
1093 				/*
1094 				 * Stop/reset/re-init NIC
1095 				 */
1096 				ae_reset(sc);
1097 			} else {
1098 
1099 			    /*
1100 			     * Receiver Error. One or more of: CRC error, frame
1101 			     *	alignment error FIFO overrun, or missed packet.
1102 			     */
1103 				if (isr & AE_ISR_RXE) {
1104 					++sc->arpcom.ac_if.if_ierrors;
1105 #ifdef AE_DEBUG
1106 					printf("ae%d: receive error %x\n", unit,
1107 						NIC_GET(sc, AE_P0_RSR));
1108 #endif
1109 				}
1110 
1111 				/*
1112 				 * Go get the packet(s)
1113 				 * XXX - Doing this on an error is dubious
1114 				 *    because there shouldn't be any data to
1115 				 *    get (we've configured the interface to
1116 				 *    not accept packets with errors).
1117 				 */
1118 				ae_rint (unit);
1119 			}
1120 		}
1121 
1122 		/*
1123 		 * If it looks like the transmitter can take more data,
1124 		 * 	attempt to start output on the interface.
1125 		 *	This is done after handling the receiver to
1126 		 *	give the receiver priority.
1127 		 */
1128 		if ((sc->arpcom.ac_if.if_flags & IFF_OACTIVE) == 0)
1129 			ae_start(&sc->arpcom.ac_if);
1130 
1131 		/*
1132 		 * return NIC CR to standard state: page 0, remote DMA complete,
1133 		 * 	start (toggling the TXP bit off, even if was just set
1134 		 *	in the transmit routine, is *okay* - it is 'edge'
1135 		 *	triggered from low to high)
1136 		 */
1137 		NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STA);
1138 
1139 		/*
1140 		 * If the Network Talley Counters overflow, read them to
1141 		 *	reset them. It appears that old 8390's won't
1142 		 *	clear the ISR flag otherwise - resulting in an
1143 		 *	infinite loop.
1144 		 */
1145 		if (isr & AE_ISR_CNT) {
1146 			(void) NIC_GET(sc, AE_P0_CNTR0);
1147 			(void) NIC_GET(sc, AE_P0_CNTR1);
1148 			(void) NIC_GET(sc, AE_P0_CNTR2);
1149 		}
1150 	}
1151 }
1152 
1153 /*
1154  * Process an ioctl request. This code needs some work - it looks
1155  *	pretty ugly.
1156  */
1157 int
1158 ae_ioctl(ifp, command, data)
1159 	register struct ifnet *ifp;
1160 	int command;
1161 	caddr_t data;
1162 {
1163 	register struct ifaddr *ifa = (struct ifaddr *)data;
1164 	struct ae_softc *sc = &ae_softc[ifp->if_unit];
1165 	struct ifreq *ifr = (struct ifreq *)data;
1166 	int s, error = 0;
1167 
1168 	s = splnet();
1169 
1170 	switch (command) {
1171 
1172 	case SIOCSIFADDR:
1173 		ifp->if_flags |= IFF_UP;
1174 
1175 		switch (ifa->ifa_addr->sa_family) {
1176 #ifdef INET
1177 		case AF_INET:
1178 			ae_init(sc);	/* before arpwhohas */
1179 			/*
1180 			 * See if another station has *our* IP address.
1181 			 * i.e.: There is an address conflict! If a
1182 			 * conflict exists, a message is sent to the
1183 			 * console.
1184 			 */
1185 			((struct arpcom *)ifp)->ac_ipaddr =
1186 				IA_SIN(ifa)->sin_addr;
1187 			arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
1188 			break;
1189 #endif
1190 #ifdef NS
1191 		/*
1192 		 * XXX - This code is probably wrong
1193 		 */
1194 		case AF_NS:
1195 		    {
1196 			register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
1197 
1198 			if (ns_nullhost(*ina))
1199 				ina->x_host =
1200 					*(union ns_host *)(sc->arpcom.ac_enaddr);
1201 			else {
1202 				/*
1203 				 *
1204 				 */
1205 				bbcopy((caddr_t)ina->x_host.c_host,
1206 				    (caddr_t)sc->arpcom.ac_enaddr,
1207 					sizeof(sc->arpcom.ac_enaddr));
1208 			}
1209 			/*
1210 			 * Set new address
1211 			 */
1212 			ae_init(sc);
1213 			break;
1214 		    }
1215 #endif
1216 		default:
1217 			ae_init(sc);
1218 			break;
1219 		}
1220 		break;
1221 
1222 	case SIOCSIFFLAGS:
1223 		/*
1224 		 * If interface is marked down and it is running, then stop it
1225 		 */
1226 		if (((ifp->if_flags & IFF_UP) == 0) &&
1227 		    (ifp->if_flags & IFF_RUNNING)) {
1228 			ae_stop(sc);
1229 			ifp->if_flags &= ~IFF_RUNNING;
1230 		} else {
1231 		/*
1232 		 * If interface is marked up and it is stopped, then start it
1233 		 */
1234 			if ((ifp->if_flags & IFF_UP) &&
1235 		    	    ((ifp->if_flags & IFF_RUNNING) == 0))
1236 				ae_init(sc);
1237 		}
1238 #if NBPFILTER > 0
1239 		if (ifp->if_flags & IFF_PROMISC) {
1240 			/*
1241 			 * Set promiscuous mode on interface.
1242 			 *	XXX - for multicasts to work, we would need to
1243 			 *		write 1's in all bits of multicast
1244 			 *		hashing array. For now we assume that
1245 			 *		this was done in ae_init().
1246 			 */
1247 			NIC_PUT(sc, AE_P0_RCR,
1248 				AE_RCR_PRO|AE_RCR_AM|AE_RCR_AB);
1249 		} else {
1250 			/*
1251 			 * XXX - for multicasts to work, we would need to
1252 			 *	rewrite the multicast hashing array with the
1253 			 *	proper hash (would have been destroyed above).
1254 			 */
1255 			NIC_PUT(sc, AE_P0_RCR, AE_RCR_AB);
1256 		}
1257 #endif
1258 		break;
1259 
1260 	default:
1261 		error = EINVAL;
1262 	}
1263 	(void) splx(s);
1264 	return (error);
1265 }
1266 
1267 /*
1268  * Macro to calculate a new address within shared memory when given an offset
1269  *	from an address, taking into account ring-wrap.
1270  */
1271 #define	ringoffset(sc, start, off, type) \
1272 	((type)( ((caddr_t)(start)+(off) >= (sc)->smem_end) ? \
1273 		(((caddr_t)(start)+(off))) - (sc)->smem_end \
1274 		+ (sc)->smem_ring: \
1275 		((caddr_t)(start)+(off)) ))
1276 
1277 /*
1278  * Retreive packet from shared memory and send to the next level up via
1279  *	ether_input(). If there is a BPF listener, give a copy to BPF, too.
1280  */
1281 ae_get_packet(sc, buf, len)
1282 	struct ae_softc *sc;
1283 	char *buf;
1284 	u_short len;
1285 {
1286 	struct ether_header *eh;
1287     	struct mbuf *m, *head, *ae_ring_to_mbuf();
1288 	u_short off;
1289 	int resid;
1290 	u_short etype;
1291 	struct trailer_header {
1292 		u_short	trail_type;
1293 		u_short trail_residual;
1294 	} trailer_header;
1295 
1296 	/* Allocate a header mbuf */
1297 	MGETHDR(m, M_DONTWAIT, MT_DATA);
1298 	if (m == 0)
1299 		goto bad;
1300 	m->m_pkthdr.rcvif = &sc->arpcom.ac_if;
1301 	m->m_pkthdr.len = len;
1302 	m->m_len = 0;
1303 	head = m;
1304 
1305 	eh = (struct ether_header *)buf;
1306 
1307 	/* The following sillines is to make NFS happy */
1308 #define EROUND	((sizeof(struct ether_header) + 3) & ~3)
1309 #define EOFF	(EROUND - sizeof(struct ether_header))
1310 
1311 	/*
1312 	 * The following assumes there is room for
1313 	 * the ether header in the header mbuf
1314 	 */
1315 	head->m_data += EOFF;
1316 	bbcopy(buf, mtod(head, caddr_t), sizeof(struct ether_header));
1317 	buf += sizeof(struct ether_header);
1318 	head->m_len += sizeof(struct ether_header);
1319 	len -= sizeof(struct ether_header);
1320 
1321 	etype = ntohs((u_short)eh->ether_type);
1322 
1323 	/*
1324 	 * Deal with trailer protocol:
1325 	 * If trailer protocol, calculate the datasize as 'off',
1326 	 * which is also the offset to the trailer header.
1327 	 * Set resid to the amount of packet data following the
1328 	 * trailer header.
1329 	 * Finally, copy residual data into mbuf chain.
1330 	 */
1331 	if (etype >= ETHERTYPE_TRAIL &&
1332 	    etype < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
1333 
1334 		off = (etype - ETHERTYPE_TRAIL) << 9;
1335 		if ((off + sizeof(struct trailer_header)) > len)
1336 			goto bad;	/* insanity */
1337 
1338 		eh->ether_type = *ringoffset(sc, buf, off, u_short *);
1339 		resid = ntohs(*ringoffset(sc, buf, off+2, u_short *));
1340 
1341 		if ((off + resid) > len) goto bad;	/* insanity */
1342 
1343 		resid -= sizeof(struct trailer_header);
1344 		if (resid < 0) goto bad;	/* insanity */
1345 
1346 		m = ae_ring_to_mbuf(sc, ringoffset(sc, buf, off+4, char *),
1347 				    head, resid);
1348 		if (m == 0) goto bad;
1349 
1350 		len = off;
1351 		head->m_pkthdr.len -= 4; /* subtract trailer header */
1352 	}
1353 
1354 	/*
1355 	 * Pull packet off interface. Or if this was a trailer packet,
1356 	 * the data portion is appended.
1357 	 */
1358 	m = ae_ring_to_mbuf(sc, buf, m, len);
1359 	if (m == 0) goto bad;
1360 
1361 #if NBPFILTER > 0
1362 	/*
1363 	 * Check if there's a BPF listener on this interface.
1364 	 * If so, hand off the raw packet to bpf.
1365 	 */
1366 	if (sc->bpf) {
1367 		bpf_mtap(sc->bpf, head);
1368 
1369 		/*
1370 		 * Note that the interface cannot be in promiscuous mode if
1371 		 * there are no BPF listeners.  And if we are in promiscuous
1372 		 * mode, we have to check if this packet is really ours.
1373 		 *
1374 		 * XXX This test does not support multicasts.
1375 		 */
1376 		if ((sc->arpcom.ac_if.if_flags & IFF_PROMISC) &&
1377 			bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr,
1378 				sizeof(eh->ether_dhost)) != 0 &&
1379 			bcmp(eh->ether_dhost, etherbroadcastaddr,
1380 				sizeof(eh->ether_dhost)) != 0) {
1381 
1382 			m_freem(head);
1383 			return;
1384 		}
1385 	}
1386 #endif
1387 
1388 	/*
1389 	 * Fix up data start offset in mbuf to point past ether header
1390 	 */
1391 	m_adj(head, sizeof(struct ether_header));
1392 
1393 	ether_input(&sc->arpcom.ac_if, eh, head);
1394 	return;
1395 
1396 bad:	if (head)
1397 		m_freem(head);
1398 	return;
1399 }
1400 
1401 /*
1402  * Supporting routines
1403  */
1404 
1405 /*
1406  * Given a source and destination address, copy 'amount' of a packet from
1407  *	the ring buffer into a linear destination buffer. Takes into account
1408  *	ring-wrap.
1409  */
1410 static inline char *
1411 ae_ring_copy(sc,src,dst,amount)
1412 	struct ae_softc *sc;
1413 	char	*src;
1414 	char	*dst;
1415 	u_short	amount;
1416 {
1417 	u_short	tmp_amount;
1418 
1419 	/* does copy wrap to lower addr in ring buffer? */
1420 	if (src + amount > sc->smem_end) {
1421 		tmp_amount = sc->smem_end - src;
1422 		/* copy amount up to end of smem */
1423 		bbcopy(src, dst, tmp_amount);
1424 		amount -= tmp_amount;
1425 		src = sc->smem_ring;
1426 		dst += tmp_amount;
1427 	}
1428 
1429 	bbcopy(src, dst, amount);
1430 
1431 	return(src + amount);
1432 }
1433 
1434 /*
1435  * Copy data from receive buffer to end of mbuf chain
1436  * allocate additional mbufs as needed. return pointer
1437  * to last mbuf in chain.
1438  * sc = ed info (softc)
1439  * src = pointer in ed ring buffer
1440  * dst = pointer to last mbuf in mbuf chain to copy to
1441  * amount = amount of data to copy
1442  */
1443 struct mbuf *
1444 ae_ring_to_mbuf(sc,src,dst,total_len)
1445 	struct ae_softc *sc;
1446 	char *src;
1447 	struct mbuf *dst;
1448 	u_short total_len;
1449 {
1450 	register struct mbuf *m = dst;
1451 
1452 	while (total_len) {
1453 		register u_short amount = min(total_len, M_TRAILINGSPACE(m));
1454 
1455 		if (amount == 0) {
1456 		  /* no more data in this mbuf, alloc another */
1457 			/*
1458 			 * If there is enough data for an mbuf cluster, attempt
1459 			 * 	to allocate one of those, otherwise, a regular
1460 			 *	mbuf will do.
1461 			 * Note that a regular mbuf is always required, even if
1462 			 *	we get a cluster - getting a cluster does not
1463 			 *	allocate any mbufs, and one is needed to assign
1464 			 *	the cluster to. The mbuf that has a cluster
1465 			 *	extension can not be used to contain data -
1466 			 *	only the cluster can contain data.
1467 			 */
1468 			dst = m;
1469 			MGET(m, M_DONTWAIT, MT_DATA);
1470 			if (m == 0)
1471 				return (0);
1472 
1473 			if (total_len >= MINCLSIZE)
1474 				MCLGET(m, M_DONTWAIT);
1475 
1476 			m->m_len = 0;
1477 			dst->m_next = m;
1478 			amount = min(total_len, M_TRAILINGSPACE(m));
1479 		}
1480 
1481 		src = ae_ring_copy(sc, src, mtod(m, caddr_t) + m->m_len,
1482 				   amount);
1483 
1484 		m->m_len += amount;
1485 		total_len -= amount;
1486 
1487 	}
1488 	return (m);
1489 }
1490