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