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