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