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