xref: /netbsd-src/sys/arch/mac68k/dev/if_ae.c (revision d0fed6c87ddc40a8bffa6f99e7433ddfc864dd83)
1 /*	$NetBSD: if_ae.c,v 1.62 1997/04/24 16:52:05 scottr Exp $	*/
2 
3 /*
4  * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
5  * adapters.
6  *
7  * Copyright (c) 1994, 1995 Charles M. Hannum.  All rights reserved.
8  *
9  * Copyright (C) 1993, David Greenman.  This software may be used, modified,
10  * copied, distributed, and sold, in both source and binary form provided that
11  * the above copyright and these terms are retained.  Under no circumstances is
12  * the author responsible for the proper functioning of this software, nor does
13  * the author assume any responsibility for damages incurred with its use.
14  */
15 
16 #include "bpfilter.h"
17 
18 #include <sys/param.h>
19 #include <sys/systm.h>
20 #include <sys/errno.h>
21 #include <sys/ioctl.h>
22 #include <sys/mbuf.h>
23 #include <sys/socket.h>
24 #include <sys/syslog.h>
25 #include <sys/device.h>
26 
27 #include <net/if.h>
28 #include <net/if_dl.h>
29 #include <net/if_types.h>
30 #include <net/if_ether.h>
31 
32 #ifdef INET
33 #include <netinet/in.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/in_var.h>
36 #include <netinet/ip.h>
37 #include <netinet/if_inarp.h>
38 #endif
39 
40 #ifdef NS
41 #include <netns/ns.h>
42 #include <netns/ns_if.h>
43 #endif
44 
45 #if NBPFILTER > 0
46 #include <net/bpf.h>
47 #include <net/bpfdesc.h>
48 #endif
49 
50 #include <machine/bus.h>
51 #include <machine/viareg.h>
52 
53 #include <dev/ic/dp8390reg.h>
54 #include "if_aereg.h"
55 #include "if_aevar.h"
56 
57 #define inline	/* XXX for debugging porpoises */
58 
59 static inline void ae_rint __P((struct ae_softc *));
60 static inline void ae_xmit __P((struct ae_softc *));
61 static inline int ae_ring_copy __P((struct ae_softc *, int, caddr_t, int));
62 
63 #define	ETHER_MIN_LEN	64
64 #define ETHER_MAX_LEN	1518
65 #define	ETHER_ADDR_LEN	6
66 
67 #define NIC_GET(sc, reg)	(bus_space_read_1((sc)->sc_regt,	\
68 				    (sc)->sc_regh,			\
69 				    ((sc)->sc_reg_map[reg])))
70 #define NIC_PUT(sc, reg, val)	(bus_space_write_1((sc)->sc_regt,	\
71 				    (sc)->sc_regh,			\
72 				    ((sc)->sc_reg_map[reg]), (val)))
73 
74 struct cfdriver ae_cd = {
75 	NULL, "ae", DV_IFNET
76 };
77 
78 int
79 ae_size_card_memory(bst, bsh, ofs)
80 	bus_space_tag_t bst;
81 	bus_space_handle_t bsh;
82 	int ofs;
83 {
84 	int i1, i2, i3, i4;
85 
86 	/*
87 	 * banks; also assume it will generally mirror in upper banks
88 	 * if not installed.
89 	 */
90 	i1 = (8192 * 0);
91 	i2 = (8192 * 1);
92 	i3 = (8192 * 2);
93 	i4 = (8192 * 3);
94 
95 	bus_space_write_2(bst, bsh, ofs + i1, 0x1111);
96 	bus_space_write_2(bst, bsh, ofs + i2, 0x2222);
97 	bus_space_write_2(bst, bsh, ofs + i3, 0x3333);
98 	bus_space_write_2(bst, bsh, ofs + i4, 0x4444);
99 
100 	if (bus_space_read_2(bst, bsh, ofs + i1) == 0x1111 &&
101 	    bus_space_read_2(bst, bsh, ofs + i2) == 0x2222 &&
102 	    bus_space_read_2(bst, bsh, ofs + i3) == 0x3333 &&
103 	    bus_space_read_2(bst, bsh, ofs + i4) == 0x4444)
104 		return 8192 * 4;
105 
106 	if ((bus_space_read_2(bst, bsh, ofs + i1) == 0x1111 &&
107 	    bus_space_read_2(bst, bsh, ofs + i2) == 0x2222) ||
108 	    (bus_space_read_2(bst, bsh, ofs + i1) == 0x3333 &&
109 	    bus_space_read_2(bst, bsh, ofs + i2) == 0x4444))
110 		return 8192 * 2;
111 
112 	if (bus_space_read_2(bst, bsh, ofs + i1) == 0x1111 ||
113 	    bus_space_read_2(bst, bsh, ofs + i1) == 0x4444)
114 		return 8192;
115 
116 	return 0;
117 }
118 
119 /*
120  * Do bus-independent setup.
121  */
122 int
123 aesetup(sc, lladdr)
124 	struct ae_softc *sc;
125 	u_int8_t *lladdr;
126 {
127 	struct ifnet *ifp = &sc->sc_ec.ec_if;
128 	int i;
129 
130 	sc->cr_proto = ED_CR_RD2;
131 
132 	/* Allocate one xmit buffer if < 16k, two buffers otherwise. */
133 	if ((sc->mem_size < 16384) ||
134 	    (sc->sc_flags & AE_FLAGS_NO_DOUBLE_BUFFERING))
135 		sc->txb_cnt = 1;
136 	else
137 		sc->txb_cnt = 2;
138 
139 	sc->tx_page_start = 0;
140 	sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
141 	sc->rec_page_stop = sc->tx_page_start + (sc->mem_size >> ED_PAGE_SHIFT);
142 	sc->mem_ring = sc->rec_page_start << ED_PAGE_SHIFT;
143 
144 	/* Now zero memory and verify that it is clear. */
145 	bus_space_set_region_2(sc->sc_buft, sc->sc_bufh,
146 	    0, 0, sc->mem_size / 2);
147 
148 	for (i = 0; i < sc->mem_size; ++i) {
149 		if (bus_space_read_1(sc->sc_buft, sc->sc_bufh, i)) {
150 printf(": failed to clear shared memory - check configuration\n");
151 			return 1;
152 		}
153 	}
154 
155 	/* Set interface to stopped condition (reset). */
156 	aestop(sc);
157 
158 	/* Initialize ifnet structure. */
159 	bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
160 	ifp->if_softc = sc;
161 	ifp->if_start = aestart;
162 	ifp->if_ioctl = aeioctl;
163 	if (!ifp->if_watchdog)
164 		ifp->if_watchdog = aewatchdog;
165 	ifp->if_flags =
166 	    IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
167 
168 	/* Attach the interface. */
169 	if_attach(ifp);
170 	ether_ifattach(ifp, lladdr);
171 
172 	/* Print additional info when attached. */
173 	printf(": address %s, ", ether_sprintf(lladdr));
174 
175 	printf("type %s, %dKB memory\n", sc->type_str, sc->mem_size / 1024);
176 
177 #if NBPFILTER > 0
178 	bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
179 #endif
180 
181 	return 0;
182 }
183 
184 /*
185  * Reset interface.
186  */
187 void
188 aereset(sc)
189 	struct ae_softc *sc;
190 {
191 	int     s;
192 
193 	s = splnet();
194 	aestop(sc);
195 	aeinit(sc);
196 	splx(s);
197 }
198 
199 /*
200  * Take interface offline.
201  */
202 void
203 aestop(sc)
204 	struct ae_softc *sc;
205 {
206 	int     n = 5000;
207 
208 	/* Stop everything on the interface, and select page 0 registers. */
209 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
210 
211 	/*
212 	 * Wait for interface to enter stopped state, but limit # of checks to
213 	 * 'n' (about 5ms).  It shouldn't even take 5us on modern DS8390's, but
214 	 * just in case it's an old one.
215 	 */
216 	while (((NIC_GET(sc, ED_P0_ISR) & ED_ISR_RST) == 0) && --n);
217 }
218 
219 /*
220  * Device timeout/watchdog routine.  Entered if the device neglects to generate
221  * an interrupt after a transmit has been started on it.
222  */
223 
224 void
225 aewatchdog(ifp)
226 	struct ifnet *ifp;
227 {
228 	struct ae_softc *sc = ifp->if_softc;
229 
230 	log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
231 	++sc->sc_ec.ec_if.if_oerrors;
232 
233 	aereset(sc);
234 }
235 
236 /*
237  * Initialize device.
238  */
239 void
240 aeinit(sc)
241 	struct ae_softc *sc;
242 {
243 	struct ifnet *ifp = &sc->sc_ec.ec_if;
244 	u_int8_t mcaf[8];
245 	int i;
246 
247 	/*
248 	 * Initialize the NIC in the exact order outlined in the NS manual.
249 	 * This init procedure is "mandatory"...don't change what or when
250 	 * things happen.
251 	 */
252 
253 	/* Reset transmitter flags. */
254 	ifp->if_timer = 0;
255 
256 	sc->txb_inuse = 0;
257 	sc->txb_new = 0;
258 	sc->txb_next_tx = 0;
259 
260 	/* Set interface for page 0, remote DMA complete, stopped. */
261 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
262 
263 	if (sc->use16bit) {
264 		/*
265 		 * Set FIFO threshold to 8, No auto-init Remote DMA, byte
266 		 * order=80x86, word-wide DMA xfers,
267 		 */
268 		NIC_PUT(sc, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_WTS | ED_DCR_LS);
269 	} else {
270 		/* Same as above, but byte-wide DMA xfers. */
271 		NIC_PUT(sc, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
272 	}
273 
274 	/* Clear remote byte count registers. */
275 	NIC_PUT(sc, ED_P0_RBCR0, 0);
276 	NIC_PUT(sc, ED_P0_RBCR1, 0);
277 
278 	/* Tell RCR to do nothing for now. */
279 	NIC_PUT(sc, ED_P0_RCR, ED_RCR_MON);
280 
281 	/* Place NIC in internal loopback mode. */
282 	NIC_PUT(sc, ED_P0_TCR, ED_TCR_LB0);
283 
284 	/* Initialize receive buffer ring. */
285 	NIC_PUT(sc, ED_P0_TPSR, sc->rec_page_start);
286 	NIC_PUT(sc, ED_P0_PSTART, sc->rec_page_start);
287 
288 	NIC_PUT(sc, ED_P0_PSTOP, sc->rec_page_stop);
289 	NIC_PUT(sc, ED_P0_BNRY, sc->rec_page_start);
290 
291 	/*
292 	 * Clear all interrupts.  A '1' in each bit position clears the
293 	 * corresponding flag.
294 	 */
295 	NIC_PUT(sc, ED_P0_ISR, 0xff);
296 
297 	/*
298 	 * Enable the following interrupts: receive/transmit complete,
299 	 * receive/transmit error, and Receiver OverWrite.
300 	 *
301 	 * Counter overflow and Remote DMA complete are *not* enabled.
302 	 */
303 	NIC_PUT(sc, ED_P0_IMR,
304 	    ED_IMR_PRXE | ED_IMR_PTXE | ED_IMR_RXEE | ED_IMR_TXEE |
305 	    ED_IMR_OVWE);
306 
307 	/* Program command register for page 1. */
308 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
309 
310 	/* Copy out our station address. */
311 	for (i = 0; i < ETHER_ADDR_LEN; ++i)
312 		NIC_PUT(sc, ED_P1_PAR0 + i, LLADDR(ifp->if_sadl)[i]);
313 
314 	/* Set multicast filter on chip. */
315 	ae_getmcaf(&sc->sc_ec, mcaf);
316 	for (i = 0; i < 8; i++)
317 		NIC_PUT(sc, ED_P1_MAR0 + i, mcaf[i]);
318 
319 	/*
320 	 * Set current page pointer to one page after the boundary pointer, as
321 	 * recommended in the National manual.
322 	 */
323 	sc->next_packet = sc->rec_page_start + 1;
324 	NIC_PUT(sc, ED_P1_CURR, sc->next_packet);
325 
326 	/* Program command register for page 0. */
327 	NIC_PUT(sc, ED_P1_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
328 
329 	i = ED_RCR_AB | ED_RCR_AM;
330 	if (ifp->if_flags & IFF_PROMISC) {
331 		/*
332 		 * Set promiscuous mode.  Multicast filter was set earlier so
333 		 * that we should receive all multicast packets.
334 		 */
335 		i |= ED_RCR_PRO | ED_RCR_AR | ED_RCR_SEP;
336 	}
337 	NIC_PUT(sc, ED_P0_RCR, i);
338 
339 	/* Take interface out of loopback. */
340 	NIC_PUT(sc, ED_P0_TCR, 0);
341 
342 	/* Fire up the interface. */
343 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
344 
345 	/* Set 'running' flag, and clear output active flag. */
346 	ifp->if_flags |= IFF_RUNNING;
347 	ifp->if_flags &= ~IFF_OACTIVE;
348 
349 	/* ...and attempt to start output. */
350 	aestart(ifp);
351 }
352 
353 /*
354  * This routine actually starts the transmission on the interface.
355  */
356 static inline void
357 ae_xmit(sc)
358 	struct ae_softc *sc;
359 {
360 	struct ifnet *ifp = &sc->sc_ec.ec_if;
361 	u_short len;
362 
363 	len = sc->txb_len[sc->txb_next_tx];
364 
365 	/* Set NIC for page 0 register access. */
366 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
367 
368 	/* Set TX buffer start page. */
369 	NIC_PUT(sc, ED_P0_TPSR, sc->tx_page_start +
370 	    sc->txb_next_tx * ED_TXBUF_SIZE);
371 
372 	/* Set TX length. */
373 	NIC_PUT(sc, ED_P0_TBCR0, len);
374 	NIC_PUT(sc, ED_P0_TBCR1, len >> 8);
375 
376 	/* Set page 0, remote DMA complete, transmit packet, and *start*. */
377 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_TXP | ED_CR_STA);
378 
379 	/* Point to next transmit buffer slot and wrap if necessary. */
380 	sc->txb_next_tx++;
381 	if (sc->txb_next_tx == sc->txb_cnt)
382 		sc->txb_next_tx = 0;
383 
384 	/* Set a timer just in case we never hear from the board again. */
385 	ifp->if_timer = 2;
386 }
387 
388 /*
389  * Start output on interface.
390  * We make two assumptions here:
391  *  1) that the current priority is set to splnet _before_ this code
392  *     is called *and* is returned to the appropriate priority after
393  *     return
394  *  2) that the IFF_OACTIVE flag is checked before this code is called
395  *     (i.e. that the output part of the interface is idle)
396  */
397 void
398 aestart(ifp)
399 	struct ifnet *ifp;
400 {
401 	struct ae_softc *sc = ifp->if_softc;
402 	struct mbuf *m0;
403 	int buffer;
404 	int len;
405 
406 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
407 		return;
408 
409 outloop:
410 	/* See if there is room to put another packet in the buffer. */
411 	if (sc->txb_inuse == sc->txb_cnt) {
412 		/* No room.  Indicate this to the outside world and exit. */
413 		ifp->if_flags |= IFF_OACTIVE;
414 		return;
415 	}
416 	IF_DEQUEUE(&ifp->if_snd, m0);
417 	if (m0 == 0)
418 		return;
419 
420 	/* We need to use m->m_pkthdr.len, so require the header */
421 	if ((m0->m_flags & M_PKTHDR) == 0)
422 		panic("aestart: no header mbuf");
423 
424 #if NBPFILTER > 0
425 	/* Tap off here if there is a BPF listener. */
426 	if (ifp->if_bpf)
427 		bpf_mtap(ifp->if_bpf, m0);
428 #endif
429 
430 	/* txb_new points to next open buffer slot. */
431 	buffer = (sc->txb_new * ED_TXBUF_SIZE) << ED_PAGE_SHIFT;
432 
433 	len = ae_put(sc, m0, buffer);
434 #if DIAGNOSTIC
435 	if (len != m0->m_pkthdr.len)
436 		printf("aestart: len %d != m0->m_pkthdr.len %d.\n",
437 			len, m0->m_pkthdr.len);
438 #endif
439 	len = m0->m_pkthdr.len;
440 
441 	m_freem(m0);
442 	sc->txb_len[sc->txb_new] = max(len, ETHER_MIN_LEN);
443 
444 	/* Start the first packet transmitting. */
445 	if (sc->txb_inuse == 0)
446 		ae_xmit(sc);
447 
448 	/* Point to next buffer slot and wrap if necessary. */
449 	if (++sc->txb_new == sc->txb_cnt)
450 		sc->txb_new = 0;
451 
452 	sc->txb_inuse++;
453 
454 	/* Loop back to the top to possibly buffer more packets. */
455 	goto outloop;
456 }
457 
458 /*
459  * Ethernet interface receiver interrupt.
460  */
461 static inline void
462 ae_rint(sc)
463 	struct ae_softc *sc;
464 {
465 	u_char boundary, current;
466 	u_short len;
467 	u_char nlen;
468 	u_int8_t *lenp;
469 	struct ae_ring packet_hdr;
470 	int packet_ptr;
471 
472 loop:
473 	/* Set NIC to page 1 registers to get 'current' pointer. */
474 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
475 
476 	/*
477 	 * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
478 	 * it points to where new data has been buffered.  The 'CURR' (current)
479 	 * register points to the logical end of the ring-buffer - i.e. it
480 	 * points to where additional new data will be added.  We loop here
481 	 * until the logical beginning equals the logical end (or in other
482 	 * words, until the ring-buffer is empty).
483 	 */
484 	current = NIC_GET(sc, ED_P1_CURR);
485 	if (sc->next_packet == current)
486 		return;
487 
488 	/* Set NIC to page 0 registers to update boundary register. */
489 	NIC_PUT(sc, ED_P1_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
490 
491 	do {
492 		/* Get pointer to this buffer's header structure. */
493 		packet_ptr = sc->mem_ring +
494 		    ((sc->next_packet - sc->rec_page_start) << ED_PAGE_SHIFT);
495 
496 		/*
497 		 * The byte count includes a 4 byte header that was added by
498 		 * the NIC.
499 		 */
500 		bus_space_read_region_1(sc->sc_buft, sc->sc_bufh,
501 		    packet_ptr, &packet_hdr, sizeof(struct ae_ring));
502 		lenp = (u_int8_t *)&packet_hdr.count; /* sigh. */
503 		len = lenp[0] | (lenp[1] << 8);
504 		packet_hdr.count = len;
505 
506 		/*
507 		 * Try do deal with old, buggy chips that sometimes duplicate
508 		 * the low byte of the length into the high byte.  We do this
509 		 * by simply ignoring the high byte of the length and always
510 		 * recalculating it.
511 		 *
512 		 * NOTE: sc->next_packet is pointing at the current packet.
513 		 */
514 		if (packet_hdr.next_packet >= sc->next_packet)
515 			nlen = (packet_hdr.next_packet - sc->next_packet);
516 		else
517 			nlen = ((packet_hdr.next_packet - sc->rec_page_start) +
518 			    (sc->rec_page_stop - sc->next_packet));
519 		--nlen;
520 		if ((len & ED_PAGE_MASK) + sizeof(packet_hdr) > ED_PAGE_SIZE)
521 			--nlen;
522 		len = (len & ED_PAGE_MASK) | (nlen << ED_PAGE_SHIFT);
523 #ifdef DIAGNOSTIC
524 		if (len != packet_hdr.count) {
525 			printf("%s: length does not match next packet pointer\n",
526 			    sc->sc_dev.dv_xname);
527 			printf("%s: len %04x nlen %04x start %02x first %02x curr %02x next %02x stop %02x\n",
528 			    sc->sc_dev.dv_xname, packet_hdr.count, len,
529 			    sc->rec_page_start, sc->next_packet, current,
530 			    packet_hdr.next_packet, sc->rec_page_stop);
531 		}
532 #endif
533 
534 		/*
535 		 * Be fairly liberal about what we allow as a "reasonable"
536 		 * length so that a [crufty] packet will make it to BPF (and
537 		 * can thus be analyzed).  Note that all that is really
538 		 * important is that we have a length that will fit into one
539 		 * mbuf cluster or less; the upper layer protocols can then
540 		 * figure out the length from their own length field(s).
541 		 */
542 		if (len <= MCLBYTES &&
543 		    packet_hdr.next_packet >= sc->rec_page_start &&
544 		    packet_hdr.next_packet < sc->rec_page_stop) {
545 			/* Go get packet. */
546 			aeread(sc, packet_ptr + sizeof(struct ae_ring),
547 			    len - sizeof(struct ae_ring));
548 			++sc->sc_ec.ec_if.if_ipackets;
549 		} else {
550 			/* Really BAD.  The ring pointers are corrupted. */
551 			log(LOG_ERR,
552 			    "%s: NIC memory corrupt - invalid packet length %d\n",
553 			    sc->sc_dev.dv_xname, len);
554 			++sc->sc_ec.ec_if.if_ierrors;
555 			aereset(sc);
556 			return;
557 		}
558 
559 		/* Update next packet pointer. */
560 		sc->next_packet = packet_hdr.next_packet;
561 
562 		/*
563 		 * Update NIC boundary pointer - being careful to keep it one
564 		 * buffer behind (as recommended by NS databook).
565 		 */
566 		boundary = sc->next_packet - 1;
567 		if (boundary < sc->rec_page_start)
568 			boundary = sc->rec_page_stop - 1;
569 		NIC_PUT(sc, ED_P0_BNRY, boundary);
570 	} while (sc->next_packet != current);
571 
572 	goto loop;
573 }
574 
575 /* Ethernet interface interrupt processor. */
576 void
577 aeintr(arg, slot)
578 	void *arg;
579 	int slot;
580 {
581 	struct ae_softc *sc = (struct ae_softc *)arg;
582 	struct ifnet *ifp = &sc->sc_ec.ec_if;
583 	u_char isr;
584 
585 	/* Set NIC to page 0 registers. */
586 	NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
587 
588 	isr = NIC_GET(sc, ED_P0_ISR);
589 	if (!isr)
590 		return;
591 
592 	/* Loop until there are no more new interrupts. */
593 	for (;;) {
594 		/*
595 		 * Reset all the bits that we are 'acknowledging' by writing a
596 		 * '1' to each bit position that was set.
597 		 * (Writing a '1' *clears* the bit.)
598 		 */
599 		NIC_PUT(sc, ED_P0_ISR, isr);
600 
601 		/*
602 		 * Handle transmitter interrupts.  Handle these first because
603 		 * the receiver will reset the board under some conditions.
604 		 */
605 		if (isr & (ED_ISR_PTX | ED_ISR_TXE)) {
606 			u_char  collisions = NIC_GET(sc, ED_P0_NCR) & 0x0f;
607 
608 			/*
609 			 * Check for transmit error.  If a TX completed with an
610 			 * error, we end up throwing the packet away.  Really
611 			 * the only error that is possible is excessive
612 			 * collisions, and in this case it is best to allow the
613 			 * automatic mechanisms of TCP to backoff the flow.  Of
614 			 * course, with UDP we're screwed, but this is expected
615 			 * when a network is heavily loaded.
616 			 */
617 			(void) NIC_GET(sc, ED_P0_TSR);
618 			if (isr & ED_ISR_TXE) {
619 				/*
620 				 * Excessive collisions (16).
621 				 */
622 				if ((NIC_GET(sc, ED_P0_TSR) & ED_TSR_ABT)
623 				    && (collisions == 0)) {
624 					/*
625 					 * When collisions total 16, the P0_NCR
626 					 * will indicate 0, and the TSR_ABT is
627 					 * set.
628 					 */
629 					collisions = 16;
630 				}
631 
632 				/* Update output errors counter. */
633 				++ifp->if_oerrors;
634 			} else {
635 				/*
636 				 * Update total number of successfully
637 				 * transmitted packets.
638 				 */
639 				++ifp->if_opackets;
640 			}
641 
642 			/* Done with the buffer. */
643 			sc->txb_inuse--;
644 
645 			/* Clear watchdog timer. */
646 			ifp->if_timer = 0;
647 			ifp->if_flags &= ~IFF_OACTIVE;
648 
649 			/*
650 			 * Add in total number of collisions on last
651 			 * transmission.
652 			 */
653 			ifp->if_collisions += collisions;
654 
655 			/*
656 			 * Decrement buffer in-use count if not zero (can only
657 			 * be zero if a transmitter interrupt occured while not
658 			 * actually transmitting).
659 			 * If data is ready to transmit, start it transmitting,
660 			 * otherwise defer until after handling receiver.
661 			 */
662 			if (sc->txb_inuse > 0)
663 				ae_xmit(sc);
664 		}
665 
666 		/* Handle receiver interrupts. */
667 		if (isr & (ED_ISR_PRX | ED_ISR_RXE | ED_ISR_OVW)) {
668 			/*
669 			 * Overwrite warning.  In order to make sure that a
670 			 * lockup of the local DMA hasn't occurred, we reset
671 			 * and re-init the NIC.  The NSC manual suggests only a
672 			 * partial reset/re-init is necessary - but some chips
673 			 * seem to want more.  The DMA lockup has been seen
674 			 * only with early rev chips - Methinks this bug was
675 			 * fixed in later revs.  -DG
676 			 */
677 			if (isr & ED_ISR_OVW) {
678 				++ifp->if_ierrors;
679 #ifdef DIAGNOSTIC
680 				log(LOG_WARNING,
681 				    "%s: warning - receiver ring buffer overrun\n",
682 				    sc->sc_dev.dv_xname);
683 #endif
684 				/* Stop/reset/re-init NIC. */
685 				aereset(sc);
686 			} else {
687 				/*
688 				 * Receiver Error.  One or more of: CRC error,
689 				 * frame alignment error FIFO overrun, or
690 				 * missed packet.
691 				 */
692 				if (isr & ED_ISR_RXE) {
693 					++ifp->if_ierrors;
694 #ifdef AE_DEBUG
695 					printf("%s: receive error %x\n",
696 					    sc->sc_dev.dv_xname,
697 					    NIC_GET(sc, ED_P0_RSR));
698 #endif
699 				}
700 
701 				/*
702 				 * Go get the packet(s)
703 				 * XXX - Doing this on an error is dubious
704 				 * because there shouldn't be any data to get
705 				 * (we've configured the interface to not
706 				 * accept packets with errors).
707 				 */
708 				ae_rint(sc);
709 			}
710 		}
711 
712 		/*
713 		 * If it looks like the transmitter can take more data, attempt
714 		 * to start output on the interface.  This is done after
715 		 * handling the receiver to give the receiver priority.
716 		 */
717 		aestart(ifp);
718 
719 		/*
720 		 * Return NIC CR to standard state: page 0, remote DMA
721 		 * complete, start (toggling the TXP bit off, even if was just
722 		 * set in the transmit routine, is *okay* - it is 'edge'
723 		 * triggered from low to high).
724 		 */
725 		NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
726 
727 		/*
728 		 * If the Network Talley Counters overflow, read them to reset
729 		 * them.  It appears that old 8390's won't clear the ISR flag
730 		 * otherwise - resulting in an infinite loop.
731 		 */
732 		if (isr & ED_ISR_CNT) {
733 			(void)NIC_GET(sc, ED_P0_CNTR0);
734 			(void)NIC_GET(sc, ED_P0_CNTR1);
735 			(void)NIC_GET(sc, ED_P0_CNTR2);
736 		}
737 
738 		isr = NIC_GET(sc, ED_P0_ISR);
739 		if (!isr)
740 			return;
741 	}
742 }
743 
744 /*
745  * Process an ioctl request.  This code needs some work - it looks pretty ugly.
746  */
747 int
748 aeioctl(ifp, cmd, data)
749 	struct ifnet *ifp;
750 	u_long cmd;
751 	caddr_t data;
752 {
753 	struct ae_softc *sc = ifp->if_softc;
754 	struct ifaddr *ifa = (struct ifaddr *) data;
755 	struct ifreq *ifr = (struct ifreq *) data;
756 	int     s, error = 0;
757 
758 	s = splnet();
759 
760 	switch (cmd) {
761 
762 	case SIOCSIFADDR:
763 		ifp->if_flags |= IFF_UP;
764 
765 		switch (ifa->ifa_addr->sa_family) {
766 #ifdef INET
767 		case AF_INET:
768 			aeinit(sc);
769 			arp_ifinit(ifp, ifa);
770 			break;
771 #endif
772 #ifdef NS
773 			/* XXX - This code is probably wrong. */
774 		case AF_NS:
775 			{
776 				struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
777 
778 				if (ns_nullhost(*ina))
779 					ina->x_host =
780 					    *(union ns_host *)LLADDR(ifp->if_sadl);
781 				else
782 					bcopy(ina->x_host.c_host,
783 					    LLADDR(ifp->if_sadl),
784 					    ETHER_ADDR_LEN);
785 				/* Set new address. */
786 				aeinit(sc);
787 				break;
788 			}
789 #endif
790 		default:
791 			aeinit(sc);
792 			break;
793 		}
794 		break;
795 
796 	case SIOCSIFFLAGS:
797 		if ((ifp->if_flags & IFF_UP) == 0 &&
798 		    (ifp->if_flags & IFF_RUNNING) != 0) {
799 			/*
800 			 * If interface is marked down and it is running, then
801 			 * stop it.
802 			 */
803 			aestop(sc);
804 			ifp->if_flags &= ~IFF_RUNNING;
805 		} else
806 			if ((ifp->if_flags & IFF_UP) != 0 &&
807 			    (ifp->if_flags & IFF_RUNNING) == 0) {
808 				/*
809 				 * If interface is marked up and it is stopped, then
810 				 * start it.
811 				 */
812 				aeinit(sc);
813 			} else {
814 				/*
815 				 * Reset the interface to pick up changes in any other
816 				 * flags that affect hardware registers.
817 				 */
818 				aestop(sc);
819 				aeinit(sc);
820 			}
821 		break;
822 
823 	case SIOCADDMULTI:
824 	case SIOCDELMULTI:
825 		/* Update our multicast list. */
826 		error = (cmd == SIOCADDMULTI) ?
827 		    ether_addmulti(ifr, &sc->sc_ec) :
828 		    ether_delmulti(ifr, &sc->sc_ec);
829 
830 		if (error == ENETRESET) {
831 			/*
832 			 * Multicast list has changed; set the hardware filter
833 			 * accordingly.
834 			 */
835 			aestop(sc);	/* XXX for ds_setmcaf? */
836 			aeinit(sc);
837 			error = 0;
838 		}
839 		break;
840 
841 	default:
842 		error = EINVAL;
843 		break;
844 	}
845 
846 	splx(s);
847 	return (error);
848 }
849 
850 /*
851  * Retreive packet from shared memory and send to the next level up via
852  * ether_input().  If there is a BPF listener, give a copy to BPF, too.
853  */
854 void
855 aeread(sc, buf, len)
856 	struct ae_softc *sc;
857 	int buf;
858 	int len;
859 {
860 	struct ifnet *ifp = &sc->sc_ec.ec_if;
861 	struct mbuf *m;
862 	struct ether_header *eh;
863 
864 	/* Pull packet off interface. */
865 	m = aeget(sc, buf, len);
866 	if (m == 0) {
867 		ifp->if_ierrors++;
868 		return;
869 	}
870 
871 	ifp->if_ipackets++;
872 
873 	/* We assume that the header fits entirely in one mbuf. */
874 	eh = mtod(m, struct ether_header *);
875 
876 #if NBPFILTER > 0
877 	/*
878 	 * Check if there's a BPF listener on this interface.
879 	 * If so, hand off the raw packet to bpf.
880 	 */
881 	if (ifp->if_bpf) {
882 		bpf_mtap(ifp->if_bpf, m);
883 
884 		/*
885 		 * Note that the interface cannot be in promiscuous mode if
886 		 * there are no BPF listeners.  And if we are in promiscuous
887 		 * mode, we have to check if this packet is really ours.
888 		 */
889 		if ((ifp->if_flags & IFF_PROMISC) &&
890 		    (eh->ether_dhost[0] & 1) == 0 &&	/* !mcast and !bcast */
891 		    bcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
892 			sizeof(eh->ether_dhost)) != 0) {
893 			m_freem(m);
894 			return;
895 		}
896 	}
897 #endif
898 
899 	/* Fix up data start offset in mbuf to point past ether header. */
900 	m_adj(m, sizeof(struct ether_header));
901 	ether_input(ifp, eh, m);
902 }
903 
904 /*
905  * Supporting routines.
906  */
907 /*
908  * Given a source and destination address, copy 'amount' of a packet from the
909  * ring buffer into a linear destination buffer.  Takes into account ring-wrap.
910  */
911 static inline int
912 ae_ring_copy(sc, src, dst, amount)
913 	struct ae_softc *sc;
914 	int src;
915 	caddr_t dst;
916 	int amount;
917 {
918 	bus_space_tag_t bst = sc->sc_buft;
919 	bus_space_handle_t bsh = sc->sc_bufh;
920 	int tmp_amount;
921 
922 	/* Does copy wrap to lower addr in ring buffer? */
923 	if (src + amount > sc->mem_size) {
924 		tmp_amount = sc->mem_size - src;
925 
926 		/* Copy amount up to end of NIC memory. */
927 		bus_space_read_region_1(bst, bsh, src, dst, tmp_amount);
928 
929 		amount -= tmp_amount;
930 		src = sc->mem_ring;
931 		dst += tmp_amount;
932 	}
933 	bus_space_read_region_1(bst, bsh, src, dst, amount);
934 
935 	return (src + amount);
936 }
937 
938 /*
939  * Copy data from receive buffer to end of mbuf chain allocate additional mbufs
940  * as needed.  Return pointer to last mbuf in chain.
941  * sc = ae info (softc)
942  * src = pointer in ae ring buffer
943  * dst = pointer to last mbuf in mbuf chain to copy to
944  * amount = amount of data to copy
945  */
946 struct mbuf *
947 aeget(sc, src, total_len)
948 	struct ae_softc *sc;
949 	int src;
950 	u_short total_len;
951 {
952 	struct ifnet *ifp = &sc->sc_ec.ec_if;
953 	struct mbuf *top, **mp, *m;
954 	int len;
955 
956 	MGETHDR(m, M_DONTWAIT, MT_DATA);
957 	if (m == 0)
958 		return 0;
959 	m->m_pkthdr.rcvif = ifp;
960 	m->m_pkthdr.len = total_len;
961 	len = MHLEN;
962 	top = 0;
963 	mp = &top;
964 
965 	while (total_len > 0) {
966 		if (top) {
967 			MGET(m, M_DONTWAIT, MT_DATA);
968 			if (m == 0) {
969 				m_freem(top);
970 				return 0;
971 			}
972 			len = MLEN;
973 		}
974 		if (total_len >= MINCLSIZE) {
975 			MCLGET(m, M_DONTWAIT);
976 			if ((m->m_flags & M_EXT) == 0) {
977 				m_freem(top);
978 				return 0;
979 			}
980 			len = MCLBYTES;
981 		}
982 		m->m_len = len = min(total_len, len);
983 		src = ae_ring_copy(sc, src, mtod(m, caddr_t), len);
984 		total_len -= len;
985 		*mp = m;
986 		mp = &m->m_next;
987 	}
988 
989 	return top;
990 }
991 
992 /*
993  * Compute the multicast address filter from the list of multicast addresses we
994  * need to listen to.
995  */
996 void
997 ae_getmcaf(ec, af)
998 	struct ethercom *ec;
999 	u_char *af;
1000 {
1001 	struct ifnet *ifp = &ec->ec_if;
1002 	struct ether_multi *enm;
1003 	u_char *cp, c;
1004 	u_int32_t crc;
1005 	int i, len;
1006 	struct ether_multistep step;
1007 
1008 	/*
1009 	 * Set up multicast address filter by passing all multicast addresses
1010 	 * through a crc generator, and then using the high order 6 bits as an
1011 	 * index into the 64 bit logical address filter.  The high order bit
1012 	 * selects the word, while the rest of the bits select the bit within
1013 	 * the word.
1014 	 */
1015 
1016 	if (ifp->if_flags & IFF_PROMISC) {
1017 		ifp->if_flags |= IFF_ALLMULTI;
1018 		for (i = 0; i < 8; i++)
1019 			af[i] = 0xff;
1020 		return;
1021 	}
1022 	for (i = 0; i < 8; i++)
1023 		af[i] = 0;
1024 	ETHER_FIRST_MULTI(step, ec, enm);
1025 	while (enm != NULL) {
1026 		if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
1027 			sizeof(enm->enm_addrlo)) != 0) {
1028 			/*
1029 			 * We must listen to a range of multicast addresses.
1030 			 * For now, just accept all multicasts, rather than
1031 			 * trying to set only those filter bits needed to match
1032 			 * the range.  (At this time, the only use of address
1033 			 * ranges is for IP multicast routing, for which the
1034 			 * range is big enough to require all bits set.)
1035 			 */
1036 			ifp->if_flags |= IFF_ALLMULTI;
1037 			for (i = 0; i < 8; i++)
1038 				af[i] = 0xff;
1039 			return;
1040 		}
1041 		cp = enm->enm_addrlo;
1042 		crc = 0xffffffff;
1043 		for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
1044 			c = *cp++;
1045 			for (i = 8; --i >= 0;) {
1046 				if (((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01)) {
1047 					crc <<= 1;
1048 					crc ^= 0x04c11db6 | 1;
1049 				} else
1050 					crc <<= 1;
1051 				c >>= 1;
1052 			}
1053 		}
1054 		/* Just want the 6 most significant bits. */
1055 		crc >>= 26;
1056 
1057 		/* Turn on the corresponding bit in the filter. */
1058 		af[crc >> 3] |= 1 << (crc & 0x7);
1059 
1060 		ETHER_NEXT_MULTI(step, enm);
1061 	}
1062 	ifp->if_flags &= ~IFF_ALLMULTI;
1063 }
1064 
1065 /*
1066  * Copy packet from mbuf to the board memory
1067  *
1068  * Currently uses an extra buffer/extra memory copy,
1069  * unless the whole packet fits in one mbuf.
1070  *
1071  */
1072 int
1073 ae_put(sc, m, buf)
1074 	struct ae_softc *sc;
1075 	struct mbuf *m;
1076 	int buf;
1077 {
1078 	u_char *data, savebyte[2];
1079 	int len, wantbyte;
1080 	u_short totlen = 0;
1081 
1082 	wantbyte = 0;
1083 
1084 	for (; m ; m = m->m_next) {
1085 		data = mtod(m, u_char *);
1086 		len = m->m_len;
1087 		totlen += len;
1088 		if (len > 0) {
1089 			/* Finish the last word. */
1090 			if (wantbyte) {
1091 				savebyte[1] = *data;
1092 				bus_space_write_region_2(sc->sc_buft,
1093 				    sc->sc_bufh, buf, savebyte, 1);
1094 				buf += 2;
1095 				data++;
1096 				len--;
1097 				wantbyte = 0;
1098 			}
1099 			/* Output contiguous words. */
1100 			if (len > 1) {
1101 				bus_space_write_region_2(sc->sc_buft,
1102 				    sc->sc_bufh, buf, data, len >> 1);
1103 				buf += len & ~1;
1104 				data += len & ~1;
1105 				len &= 1;
1106 			}
1107 			/* Save last byte, if necessary. */
1108 			if (len == 1) {
1109 				savebyte[0] = *data;
1110 				wantbyte = 1;
1111 			}
1112 		}
1113 	}
1114 
1115 	if (wantbyte) {
1116 		savebyte[1] = 0;
1117 		bus_space_write_region_2(sc->sc_buft, sc->sc_bufh,
1118 		    buf, savebyte, 1);
1119 	}
1120 	return (totlen);
1121 }
1122