if_ae.c revision 1.6 1 /*
2 * Device driver for National Semiconductor DS8390 based ethernet adapters.
3 *
4 * Based on original ISA bus driver by David Greenman, 29-April-1993
5 *
6 * Copyright (C) 1993, David Greenman. This software may be used, modified,
7 * copied, distributed, and sold, in both source and binary form provided
8 * that the above copyright and these terms are retained. Under no
9 * circumstances is the author responsible for the proper functioning
10 * of this software, nor does the author assume any responsibility
11 * for damages incurred with its use.
12 *
13 * Adapted for MacBSD by Brad Parker <brad (at) fcr.com>
14 *
15 * Currently supports:
16 * Apples NB Ethernet card
17 * Interlan A310 Nubus Ethernet card
18 * Cayman Systems GatorCard
19 */
20
21 /*
22 * $Id: if_ae.c,v 1.6 1994/02/22 01:15:00 briggs Exp $
23 */
24
25 /*
26 * Modification history
27 *
28 * $Log: if_ae.c,v $
29 * Revision 1.6 1994/02/22 01:15:00 briggs
30 * Get rid of if_init assignment.
31 *
32 * Revision 1.5 1994/01/30 01:14:49 briggs
33 * Include-cop strikes again.
34 *
35 * Revision 1.3 1993/12/21 03:18:04 briggs
36 * Update ethernet driver to use config.new. At least, it's a first stab
37 * working from mycroft's magnum changes to if_ed.c.
38 *
39 * Revision 1.2 1993/12/15 03:38:20 briggs
40 * Get rid of IFF_ALTPHYS and hence IFF_LLC0 reference. It doesn't appear
41 * to have been used in this driver ;-)
42 *
43 * Revision 1.1 1993/11/29 00:32:43 briggs
44 * Update to current work in progress. This includes an update to
45 * use config.new.
46 * Numerous updates to console so it works better on the SE/30 screen.
47 * Some nice changes from Brad Parker for handling NuBUS and an ethernet
48 * driver that I haven't worked on, yet.
49 *
50 *
51 */
52
53 #include "ae.h"
54 #if NAE > 0
55 /* bpfilter included here in case it is needed in future net includes */
56 #include "bpfilter.h"
57
58 #include "param.h"
59 #include "systm.h"
60 #include "errno.h"
61 #include "ioctl.h"
62 #include "mbuf.h"
63 #include "socket.h"
64 #include "syslog.h"
65
66 #include <net/if.h>
67 #include <net/if_dl.h>
68 #include <net/if_types.h>
69 #include <net/netisr.h>
70
71 #ifdef INET
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/in_var.h>
75 #include <netinet/ip.h>
76 #include <netinet/if_ether.h>
77 #endif
78
79 #ifdef NS
80 #include <netns/ns.h>
81 #include <netns/ns_if.h>
82 #endif
83
84 #if NBPFILTER > 0
85 #include <net/bpf.h>
86 #include <net/bpfdesc.h>
87 #endif
88
89 #include <sys/device.h>
90 #include "nubus.h"
91 #include "if_aereg.h"
92
93 struct ae_device {
94 struct device ae_dev;
95 /* struct nubusdev ae_nu;
96 struct intrhand ae_ih; */
97 };
98
99 /*
100 * ae_softc: per line info and status
101 */
102 struct ae_softc {
103 struct ae_device *sc_ae;
104
105 struct arpcom arpcom; /* ethernet common */
106
107 char *type_str; /* pointer to type string */
108 u_char vendor; /* interface vendor */
109 u_char type; /* interface type code */
110 #define APPLE_CARD(sc) ((sc)->vendor == AE_VENDOR_APPLE)
111 #define REG_MAP(sc, reg) (APPLE_CARD(sc) ? (0x0f-(reg))<<2 : (reg)<<2)
112 #define NIC_GET(sc, reg) ((sc)->nic_addr[REG_MAP(sc, reg)])
113 #define NIC_PUT(sc, reg, val) ((sc)->nic_addr[REG_MAP(sc, reg)] = (val))
114 volatile caddr_t nic_addr; /* NIC (DS8390) I/O bus address */
115 caddr_t rom_addr; /* on board prom address */
116 caddr_t smem_start; /* shared memory start address */
117 caddr_t smem_end; /* shared memory end address */
118 u_long smem_size; /* total shared memory size */
119 caddr_t smem_ring; /* start of RX ring-buffer (in smem) */
120
121 caddr_t bpf; /* BPF "magic cookie" */
122
123 u_char xmit_busy; /* transmitter is busy */
124 u_char txb_cnt; /* Number of transmit buffers */
125 u_char txb_next; /* Pointer to next buffer ready to xmit */
126 u_short txb_next_len; /* next xmit buffer length */
127 u_char data_buffered; /* data has been buffered in interface memory */
128 u_char tx_page_start; /* first page of TX buffer area */
129
130 u_char rec_page_start; /* first page of RX ring-buffer */
131 u_char rec_page_stop; /* last page of RX ring-buffer */
132 u_char next_packet; /* pointer to next unread RX packet */
133 } ae_softc[NAE];
134
135 void ae_find();
136 int ae_attach(), ae_init(), aeintr(), ae_ioctl(), ae_probe(),
137 ae_start(), ae_reset(), ae_watchdog();
138
139 struct cfdriver aecd =
140 { NULL, "ae", ae_probe, ae_attach, DV_IFNET, sizeof(struct ae_device), NULL, 0 };
141
142 static void ae_stop();
143 static inline void ae_rint();
144 static inline void ae_xmit();
145 static inline char *ae_ring_copy();
146
147 extern int ether_output();
148
149 #define ETHER_MIN_LEN 64
150 #define ETHER_MAX_LEN 1518
151 #define ETHER_ADDR_LEN 6
152 #define ETHER_HDR_SIZE 14
153
154 char ae_name[] = "8390 Nubus Ethernet card";
155 static char zero = 0;
156 static u_char ones = 0xff;
157
158 int
159 ae_probe(parent, cf, aux)
160 struct cfdriver *parent;
161 struct cfdata *cf;
162 void *aux;
163 {
164 register struct nubus_hw *nu = (struct nubus_hw *) aux;
165 struct ae_softc *sc = &ae_softc[cf->cf_unit];
166 int i, memsize;
167 int flags = 0;
168
169 if (nu->Slot.type != NUBUS_NETWORK)
170 return 0;
171
172 /*
173 * Try to determine what type of card this is...
174 sc->vendor == AE_VENDOR_APPLE;
175 */
176
177 /* see if it's an Interlan/GatorCard */
178 sc->rom_addr = nu->addr + GC_ROM_OFFSET;
179 if (sc->rom_addr[0x18] == 0x0 &&
180 sc->rom_addr[0x1c] == 0x55) {
181 sc->vendor = AE_VENDOR_INTERLAN;
182 }
183
184 sc->type = 0;
185
186 switch (sc->vendor) {
187 case AE_VENDOR_INTERLAN:
188 sc->nic_addr = nu->addr + GC_NIC_OFFSET;
189 sc->rom_addr = nu->addr + GC_ROM_OFFSET;
190 sc->smem_start = nu->addr + GC_DATA_OFFSET;
191 sc->type_str = "Interlan";
192 memsize = 8192;
193
194 /* reset the NIC chip */
195 *((caddr_t)nu->addr + GC_RESET_OFFSET) = (char)zero;
196
197 /* Get station address from on-board ROM */
198 for (i = 0; i < ETHER_ADDR_LEN; ++i)
199 sc->arpcom.ac_enaddr[i] = *(sc->rom_addr + i*4);
200 break;
201
202 case AE_VENDOR_APPLE:
203 default:
204 sc->nic_addr = nu->addr + AE_NIC_OFFSET;
205 sc->rom_addr = nu->addr + AE_ROM_OFFSET;
206 sc->smem_start = nu->addr + AE_DATA_OFFSET;
207 sc->type_str = "Apple";
208 memsize = 8192;
209
210 /* Get station address from on-board ROM */
211 for (i = 0; i < ETHER_ADDR_LEN; ++i)
212 sc->arpcom.ac_enaddr[i] = *(sc->rom_addr + i*2);
213 break;
214 }
215
216 /*
217 * allocate one xmit buffer if < 16k, two buffers otherwise
218 */
219 if ((memsize < 16384) || (flags & AE_FLAGS_NO_DOUBLE_BUFFERING)) {
220 sc->smem_ring = sc->smem_start + (AE_PAGE_SIZE * AE_TXBUF_SIZE);
221 sc->txb_cnt = 1;
222 sc->rec_page_start = AE_TXBUF_SIZE;
223 } else {
224 sc->smem_ring = sc->smem_start + (AE_PAGE_SIZE * AE_TXBUF_SIZE * 2);
225 sc->txb_cnt = 2;
226 sc->rec_page_start = AE_TXBUF_SIZE * 2;
227 }
228
229 sc->smem_size = memsize;
230 sc->smem_end = sc->smem_start + memsize;
231 sc->rec_page_stop = memsize / AE_PAGE_SIZE;
232 sc->tx_page_start = 0;
233
234 /*
235 * Now zero memory and verify that it is clear
236 */
237 bzero(sc->smem_start, memsize);
238
239 for (i = 0; i < memsize; ++i)
240 if (sc->smem_start[i]) {
241 printf(": failed to clear shared memory at %x\n",
242 sc->smem_start + i);
243
244 return(0);
245 }
246
247 #ifdef DEBUG_PRINT
248 printf("nic_addr %x, rom_addr %x\n",
249 sc->nic_addr, sc->rom_addr);
250 printf("smem_size %d\n", sc->smem_size);
251 printf("smem_start %x, smem_ring %x, smem_end %x\n",
252 sc->smem_start, sc->smem_ring, sc->smem_end);
253 printf("phys address %02x:%02x:%02x:%02x:%02x:%02x\n",
254 sc->arpcom.ac_enaddr[0],
255 sc->arpcom.ac_enaddr[1],
256 sc->arpcom.ac_enaddr[2],
257 sc->arpcom.ac_enaddr[3],
258 sc->arpcom.ac_enaddr[4],
259 sc->arpcom.ac_enaddr[5]);
260 #endif
261
262 return(1);
263 }
264
265 /*
266 * Install interface into kernel networking data structures
267 */
268 int
269 ae_attach(parent, self, aux)
270 struct cfdriver *parent, *self;
271 void *aux;
272 {
273 struct nubus_hw *nu = aux;
274 struct ae_device *ae = (struct ae_device *) self;
275 struct ae_softc *sc = &ae_softc[ae->ae_dev.dv_unit];
276 struct cfdata *cf = ae->ae_dev.dv_cfdata;
277 struct ifnet *ifp = &sc->arpcom.ac_if;
278 struct ifaddr *ifa;
279 struct sockaddr_dl *sdl;
280
281 sc->sc_ae = ae;
282
283 /*
284 * Set interface to stopped condition (reset)
285 */
286 ae_stop(sc);
287
288 /*
289 * Initialize ifnet structure
290 */
291 ifp->if_unit = ae->ae_dev.dv_unit;
292 ifp->if_name = aecd.cd_name;
293 ifp->if_mtu = ETHERMTU;
294 ifp->if_output = ether_output;
295 ifp->if_start = ae_start;
296 ifp->if_ioctl = ae_ioctl;
297 ifp->if_reset = ae_reset;
298 ifp->if_watchdog = ae_watchdog;
299 ifp->if_flags = (IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS);
300
301 #if 0
302 /*
303 * Set default state for ALTPHYS flag (used to disable the transceiver
304 * for AUI operation), based on compile-time config option.
305 */
306 if (cf->cf_flags & AE_FLAGS_DISABLE_TRANSCEIVER)
307 ifp->if_flags |= IFF_ALTPHYS;
308 #endif
309
310 /*
311 * Attach the interface
312 */
313 if_attach(ifp);
314
315 /*
316 * Search down the ifa address list looking for the AF_LINK type entry
317 */
318 ifa = ifp->if_addrlist;
319 while ((ifa != 0) && (ifa->ifa_addr != 0) &&
320 (ifa->ifa_addr->sa_family != AF_LINK))
321 ifa = ifa->ifa_next;
322 /*
323 * If we find an AF_LINK type entry we fill in the hardware address.
324 * This is useful for netstat(1) to keep track of which interface
325 * is which.
326 */
327 if ((ifa != 0) && (ifa->ifa_addr != 0)) {
328 /*
329 * Fill in the link-level address for this interface
330 */
331 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
332 sdl->sdl_type = IFT_ETHER;
333 sdl->sdl_alen = ETHER_ADDR_LEN;
334 sdl->sdl_slen = 0;
335 bcopy(sc->arpcom.ac_enaddr, LLADDR(sdl), ETHER_ADDR_LEN);
336 }
337
338 /*
339 * Print additional info when attached
340 */
341 printf(": address %s, ", ether_sprintf(sc->arpcom.ac_enaddr));
342
343 if (sc->type_str && (*sc->type_str != 0))
344 printf("type %s ", sc->type_str);
345 else
346 printf("type unknown (0x%x) ", sc->type);
347
348 printf("\n");
349
350 /*
351 * If BPF is in the kernel, call the attach for it
352 */
353 #if NBPFILTER > 0
354 bpfattach(&sc->bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
355 #endif
356 }
357
358 /*
359 * Reset interface.
360 */
361 int
362 ae_reset(sc)
363 struct ae_softc *sc;
364 {
365 int s;
366
367 s = splnet();
368
369 /*
370 * Stop interface and re-initialize.
371 */
372 ae_stop(sc);
373 ae_init(sc);
374
375 (void) splx(s);
376 }
377
378 /*
379 * Take interface offline.
380 */
381 void
382 ae_stop(sc)
383 struct ae_softc *sc;
384 {
385 int n = 5000;
386
387 /*
388 * Stop everything on the interface, and select page 0 registers.
389 */
390 NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STP);
391
392 /*
393 * Wait for interface to enter stopped state, but limit # of checks
394 * to 'n' (about 5ms). It shouldn't even take 5us on modern
395 * DS8390's, but just in case it's an old one.
396 */
397 while (((NIC_GET(sc, AE_P0_ISR) & AE_ISR_RST) == 0) && --n);
398 }
399
400 /*
401 * Device timeout/watchdog routine. Entered if the device neglects to
402 * generate an interrupt after a transmit has been started on it.
403 */
404 int
405 ae_watchdog(unit)
406 short unit;
407 {
408 log(LOG_ERR, "ae%d: device timeout\n", unit);
409 {
410 struct ae_softc *sc = &ae_softc[unit];
411 printf("cr %x, isr %x\n", NIC_GET(sc, AE_P0_CR), NIC_GET(sc, AE_P0_ISR));
412 /* via_dump(); */
413 if (NIC_GET(sc, AE_P0_ISR)) {
414 aeintr(0);
415 return;
416 }
417 }
418 ae_reset(unit);
419 }
420
421 /*
422 * Initialize device.
423 */
424 ae_init(sc)
425 struct ae_softc *sc;
426 {
427 struct ifnet *ifp = &sc->arpcom.ac_if;
428 int i, s;
429 u_char command;
430
431
432 /* address not known */
433 if (ifp->if_addrlist == (struct ifaddr *)0) return;
434
435 /*
436 * Initialize the NIC in the exact order outlined in the NS manual.
437 * This init procedure is "mandatory"...don't change what or when
438 * things happen.
439 */
440 s = splnet();
441
442 /* reset transmitter flags */
443 sc->data_buffered = 0;
444 sc->xmit_busy = 0;
445 sc->arpcom.ac_if.if_timer = 0;
446
447 sc->txb_next = 0;
448
449 /* This variable is used below - don't move this assignment */
450 sc->next_packet = sc->rec_page_start + 1;
451
452 #ifdef DEBUG_PRINT
453 printf("page_start %d, page_stop %d, next %d\n",
454 sc->rec_page_start, sc->rec_page_stop, sc->next_packet);
455 #endif
456
457 /*
458 * Set interface for page 0, Remote DMA complete, Stopped
459 */
460 NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STP);
461
462 /*
463 * Set FIFO threshold to 4, No auto-init Remote DMA, Burst mode,
464 * byte order=80x86, word-wide DMA xfers,
465 */
466 NIC_PUT(sc, AE_P0_DCR, AE_DCR_FT1|AE_DCR_BMS|AE_DCR_WTS);
467
468 /*
469 * Clear Remote Byte Count Registers
470 */
471 NIC_PUT(sc, AE_P0_RBCR0, zero);
472 NIC_PUT(sc, AE_P0_RBCR1, zero);
473
474 /*
475 * Enable reception of broadcast packets
476 */
477 NIC_PUT(sc, AE_P0_RCR, AE_RCR_AB);
478
479 /*
480 * Place NIC in internal loopback mode
481 */
482 NIC_PUT(sc, AE_P0_TCR, AE_TCR_LB0);
483
484 /*
485 * Initialize transmit/receive (ring-buffer) Page Start
486 */
487 NIC_PUT(sc, AE_P0_TPSR, sc->tx_page_start);
488 NIC_PUT(sc, AE_P0_PSTART, sc->rec_page_start);
489
490 /*
491 * Initialize Receiver (ring-buffer) Page Stop and Boundry
492 */
493 NIC_PUT(sc, AE_P0_PSTOP, sc->rec_page_stop);
494 NIC_PUT(sc, AE_P0_BNRY, sc->rec_page_start);
495
496 /*
497 * Clear all interrupts. A '1' in each bit position clears the
498 * corresponding flag.
499 */
500 NIC_PUT(sc, AE_P0_ISR, ones);
501
502 /*
503 * Enable the following interrupts: receive/transmit complete,
504 * receive/transmit error, and Receiver OverWrite.
505 *
506 * Counter overflow and Remote DMA complete are *not* enabled.
507 */
508 NIC_PUT(sc, AE_P0_IMR,
509 AE_IMR_PRXE|AE_IMR_PTXE|AE_IMR_RXEE|AE_IMR_TXEE|AE_IMR_OVWE);
510
511 /*
512 * Program Command Register for page 1
513 */
514 NIC_PUT(sc, AE_P0_CR, AE_CR_PAGE_1|AE_CR_RD2|AE_CR_STP);
515
516 /*
517 * Copy out our station address
518 */
519 for (i = 0; i < ETHER_ADDR_LEN; ++i)
520 NIC_PUT(sc, AE_P1_PAR0 + i, sc->arpcom.ac_enaddr[i]);
521
522 #if NBPFILTER > 0
523 /*
524 * Initialize multicast address hashing registers to accept
525 * all multicasts (only used when in promiscuous mode)
526 */
527 for (i = 0; i < 8; ++i)
528 NIC_PUT(sc, AE_P1_MAR0 + i, 0xff);
529 #endif
530
531 /*
532 * Set Current Page pointer to next_packet (initialized above)
533 */
534 NIC_PUT(sc, AE_P1_CURR, sc->next_packet);
535
536 /*
537 * Set Command Register for page 0, Remote DMA complete,
538 * and interface Start.
539 */
540 NIC_PUT(sc, AE_P1_CR, AE_CR_RD2|AE_CR_STA);
541
542 /*
543 * Take interface out of loopback
544 */
545 NIC_PUT(sc, AE_P0_TCR, zero);
546
547 /*
548 * Set 'running' flag, and clear output active flag.
549 */
550 ifp->if_flags |= IFF_RUNNING;
551 ifp->if_flags &= ~IFF_OACTIVE;
552
553 /* XXXXXX */
554 add_nubus_intr(sc->rom_addr - GC_ROM_OFFSET, aeintr, sc - ae_softc);
555
556 /*
557 * ...and attempt to start output
558 */
559 ae_start(ifp);
560
561 (void) splx(s);
562 }
563
564 /*
565 * This routine actually starts the transmission on the interface
566 */
567 static inline void ae_xmit(ifp)
568 struct ifnet *ifp;
569 {
570 struct ae_softc *sc = &ae_softc[ifp->if_unit];
571 u_short len = sc->txb_next_len;
572
573 /*
574 * Set NIC for page 0 register access
575 */
576 NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STA);
577
578 /*
579 * Set TX buffer start page
580 */
581 NIC_PUT(sc, AE_P0_TPSR, sc->tx_page_start +
582 sc->txb_next * AE_TXBUF_SIZE);
583
584 /*
585 * Set TX length
586 */
587 NIC_PUT(sc, AE_P0_TBCR0, len & 0xff);
588 NIC_PUT(sc, AE_P0_TBCR1, len >> 8);
589
590 /*
591 * Set page 0, Remote DMA complete, Transmit Packet, and *Start*
592 */
593 NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_TXP|AE_CR_STA);
594
595 sc->xmit_busy = 1;
596 sc->data_buffered = 0;
597
598 /*
599 * Switch buffers if we are doing double-buffered transmits
600 */
601 if ((sc->txb_next == 0) && (sc->txb_cnt > 1))
602 sc->txb_next = 1;
603 else
604 sc->txb_next = 0;
605
606 /*
607 * Set a timer just in case we never hear from the board again
608 */
609 ifp->if_timer = 2;
610 }
611
612 /*
613 * Start output on interface.
614 * We make two assumptions here:
615 * 1) that the current priority is set to splnet _before_ this code
616 * is called *and* is returned to the appropriate priority after
617 * return
618 * 2) that the IFF_OACTIVE flag is checked before this code is called
619 * (i.e. that the output part of the interface is idle)
620 */
621 int
622 ae_start(ifp)
623 struct ifnet *ifp;
624 {
625 struct ae_softc *sc = &ae_softc[ifp->if_unit];
626 struct mbuf *m0, *m;
627 caddr_t buffer;
628 int len;
629
630 outloop:
631 /*
632 * See if there is room to send more data (i.e. one or both of the
633 * buffers is empty).
634 */
635 if (sc->data_buffered)
636 if (sc->xmit_busy) {
637 /*
638 * No room. Indicate this to the outside world
639 * and exit.
640 */
641 ifp->if_flags |= IFF_OACTIVE;
642 return;
643 } else {
644 /*
645 * Data is buffered, but we're not transmitting, so
646 * start the xmit on the buffered data.
647 * Note that ae_xmit() resets the data_buffered flag
648 * before returning.
649 */
650 ae_xmit(ifp);
651 }
652
653 IF_DEQUEUE(&sc->arpcom.ac_if.if_snd, m);
654 if (m == 0) {
655 /*
656 * The following isn't pretty; we are using the !OACTIVE flag to
657 * indicate to the outside world that we can accept an additional
658 * packet rather than that the transmitter is _actually_
659 * active. Indeed, the transmitter may be active, but if we haven't
660 * filled the secondary buffer with data then we still want to
661 * accept more.
662 * Note that it isn't necessary to test the data_buffered flag -
663 * we wouldn't have tried to de-queue the packet in the first place
664 * if it was set.
665 */
666 ifp->if_flags &= ~IFF_OACTIVE;
667 return;
668 }
669
670 /*
671 * Copy the mbuf chain into the transmit buffer
672 */
673 buffer = sc->smem_start + (sc->txb_next * AE_TXBUF_SIZE * AE_PAGE_SIZE);
674 len = 0;
675 for (m0 = m; m != 0; m = m->m_next) {
676 /*printf("ae: copy %d bytes @ %x\n", m->m_len, buffer);*/
677 bcopy(mtod(m, caddr_t), buffer, m->m_len);
678 buffer += m->m_len;
679 len += m->m_len;
680 }
681 if (len & 1) len++;
682
683 sc->txb_next_len = MAX(len, ETHER_MIN_LEN);
684
685 if (sc->txb_cnt > 1)
686 /*
687 * only set 'buffered' flag if doing multiple buffers
688 */
689 sc->data_buffered = 1;
690
691 if (sc->xmit_busy == 0)
692 ae_xmit(ifp);
693 /*
694 * If there is BPF support in the configuration, tap off here.
695 * The following has support for converting trailer packets
696 * back to normal.
697 */
698 #if NBPFILTER > 0
699 if (sc->bpf) {
700 u_short etype;
701 int off, datasize, resid;
702 struct ether_header *eh;
703 struct trailer_header {
704 u_short ether_type;
705 u_short ether_residual;
706 } trailer_header;
707 char ether_packet[ETHER_MAX_LEN];
708 char *ep;
709
710 ep = ether_packet;
711
712 /*
713 * We handle trailers below:
714 * Copy ether header first, then residual data,
715 * then data. Put all this in a temporary buffer
716 * 'ether_packet' and send off to bpf. Since the
717 * system has generated this packet, we assume
718 * that all of the offsets in the packet are
719 * correct; if they're not, the system will almost
720 * certainly crash in m_copydata.
721 * We make no assumptions about how the data is
722 * arranged in the mbuf chain (i.e. how much
723 * data is in each mbuf, if mbuf clusters are
724 * used, etc.), which is why we use m_copydata
725 * to get the ether header rather than assume
726 * that this is located in the first mbuf.
727 */
728 /* copy ether header */
729 m_copydata(m0, 0, sizeof(struct ether_header), ep);
730 eh = (struct ether_header *) ep;
731 ep += sizeof(struct ether_header);
732 etype = ntohs(eh->ether_type);
733 if (etype >= ETHERTYPE_TRAIL &&
734 etype < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
735 datasize = ((etype - ETHERTYPE_TRAIL) << 9);
736 off = datasize + sizeof(struct ether_header);
737
738 /* copy trailer_header into a data structure */
739 m_copydata(m0, off, sizeof(struct trailer_header),
740 &trailer_header.ether_type);
741
742 /* copy residual data */
743 m_copydata(m0, off+sizeof(struct trailer_header),
744 resid = ntohs(trailer_header.ether_residual) -
745 sizeof(struct trailer_header), ep);
746 ep += resid;
747
748 /* copy data */
749 m_copydata(m0, sizeof(struct ether_header),
750 datasize, ep);
751 ep += datasize;
752
753 /* restore original ether packet type */
754 eh->ether_type = trailer_header.ether_type;
755
756 bpf_tap(sc->bpf, ether_packet, ep - ether_packet);
757 } else
758 bpf_mtap(sc->bpf, m0);
759 }
760 #endif
761
762 m_freem(m0);
763
764 /*
765 * If we are doing double-buffering, a buffer might be free to
766 * fill with another packet, so loop back to the top.
767 */
768 if (sc->txb_cnt > 1)
769 goto outloop;
770 else {
771 ifp->if_flags |= IFF_OACTIVE;
772 return;
773 }
774 }
775
776 /*
777 * Ethernet interface receiver interrupt.
778 */
779 static inline void
780 ae_rint(unit)
781 int unit;
782 {
783 register struct ae_softc *sc = &ae_softc[unit];
784 u_char boundry, current;
785 u_short len;
786 struct ae_ring *packet_ptr;
787
788 /*
789 * Set NIC to page 1 registers to get 'current' pointer
790 */
791 NIC_PUT(sc, AE_P0_CR, AE_CR_PAGE_1|AE_CR_RD2|AE_CR_STA);
792
793 /*
794 * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
795 * it points to where new data has been buffered. The 'CURR'
796 * (current) register points to the logical end of the ring-buffer
797 * - i.e. it points to where additional new data will be added.
798 * We loop here until the logical beginning equals the logical
799 * end (or in other words, until the ring-buffer is empty).
800 */
801 while (sc->next_packet != NIC_GET(sc, AE_P1_CURR)) {
802
803 /* get pointer to this buffer header structure */
804 packet_ptr = (struct ae_ring *)(sc->smem_ring +
805 (sc->next_packet - sc->rec_page_start) * AE_PAGE_SIZE);
806
807 /*
808 * The byte count includes the FCS - Frame Check Sequence (a
809 * 32 bit CRC).
810 */
811 len = packet_ptr->count[0] | (packet_ptr->count[1] << 8);
812 if ((len >= ETHER_MIN_LEN) && (len <= ETHER_MAX_LEN)) {
813 /*
814 * Go get packet. len - 4 removes CRC from length.
815 * (packet_ptr + 1) points to data just after the packet ring
816 * header (+4 bytes)
817 */
818 ae_get_packet(sc, (caddr_t)(packet_ptr + 1), len - 4);
819 ++sc->arpcom.ac_if.if_ipackets;
820 } else {
821 /*
822 * Really BAD...probably indicates that the ring pointers
823 * are corrupted. Also seen on early rev chips under
824 * high load - the byte order of the length gets switched.
825 */
826 log(LOG_ERR,
827 "ae%d: shared memory corrupt - invalid packet length %d\n",
828 unit, len);
829 ae_reset(unit);
830 return;
831 }
832
833 /*
834 * Update next packet pointer
835 */
836 sc->next_packet = packet_ptr->next_packet;
837
838 /*
839 * Update NIC boundry pointer - being careful to keep it
840 * one buffer behind. (as recommended by NS databook)
841 */
842 boundry = sc->next_packet - 1;
843 if (boundry < sc->rec_page_start)
844 boundry = sc->rec_page_stop - 1;
845
846 /*
847 * Set NIC to page 0 registers to update boundry register
848 */
849 NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STA);
850
851 NIC_PUT(sc, AE_P0_BNRY, boundry);
852
853 /*
854 * Set NIC to page 1 registers before looping to top (prepare to
855 * get 'CURR' current pointer)
856 */
857 NIC_PUT(sc, AE_P0_CR, AE_CR_PAGE_1|AE_CR_RD2|AE_CR_STA);
858 }
859 }
860
861 /*
862 * Ethernet interface interrupt processor
863 */
864 int
865 aeintr(unit)
866 int unit;
867 {
868 struct ae_softc *sc = &ae_softc[unit];
869 u_char isr;
870
871 /*
872 * Set NIC to page 0 registers
873 */
874 NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STA);
875
876 /*
877 * loop until there are no more new interrupts
878 */
879 while (isr = NIC_GET(sc, AE_P0_ISR)) {
880
881 /*
882 * reset all the bits that we are 'acknowledging'
883 * by writing a '1' to each bit position that was set
884 * (writing a '1' *clears* the bit)
885 */
886 NIC_PUT(sc, AE_P0_ISR, isr);
887
888 /*
889 * Handle transmitter interrupts. Handle these first
890 * because the receiver will reset the board under
891 * some conditions.
892 */
893 if (isr & (AE_ISR_PTX|AE_ISR_TXE)) {
894 u_char collisions = NIC_GET(sc, AE_P0_NCR);
895
896 /*
897 * Check for transmit error. If a TX completed with an
898 * error, we end up throwing the packet away. Really
899 * the only error that is possible is excessive
900 * collisions, and in this case it is best to allow the
901 * automatic mechanisms of TCP to backoff the flow. Of
902 * course, with UDP we're screwed, but this is expected
903 * when a network is heavily loaded.
904 */
905 if (isr & AE_ISR_TXE) {
906
907 /*
908 * Excessive collisions (16)
909 */
910 if ((NIC_GET(sc, AE_P0_TSR) & AE_TSR_ABT)
911 && (collisions == 0)) {
912 /*
913 * When collisions total 16, the
914 * P0_NCR will indicate 0, and the
915 * TSR_ABT is set.
916 */
917 collisions = 16;
918 }
919
920 /*
921 * update output errors counter
922 */
923 ++sc->arpcom.ac_if.if_oerrors;
924 } else {
925 /*
926 * Update total number of successfully
927 * transmitted packets.
928 */
929 ++sc->arpcom.ac_if.if_opackets;
930 }
931
932 /*
933 * reset tx busy and output active flags
934 */
935 sc->xmit_busy = 0;
936 sc->arpcom.ac_if.if_flags &= ~IFF_OACTIVE;
937
938 /*
939 * clear watchdog timer
940 */
941 sc->arpcom.ac_if.if_timer = 0;
942
943 /*
944 * Add in total number of collisions on last
945 * transmission.
946 */
947 sc->arpcom.ac_if.if_collisions += collisions;
948
949 /*
950 * If data is ready to transmit, start it transmitting,
951 * otherwise defer until after handling receiver
952 */
953 if (sc->data_buffered)
954 ae_xmit(&sc->arpcom.ac_if);
955 }
956
957 /*
958 * Handle receiver interrupts
959 */
960 if (isr & (AE_ISR_PRX|AE_ISR_RXE|AE_ISR_OVW)) {
961 /*
962 * Overwrite warning. In order to make sure that a lockup
963 * of the local DMA hasn't occurred, we reset and
964 * re-init the NIC. The NSC manual suggests only a
965 * partial reset/re-init is necessary - but some
966 * chips seem to want more. The DMA lockup has been
967 * seen only with early rev chips - Methinks this
968 * bug was fixed in later revs. -DG
969 */
970 if (isr & AE_ISR_OVW) {
971 ++sc->arpcom.ac_if.if_ierrors;
972 log(LOG_WARNING,
973 "ae%d: warning - receiver ring buffer overrun\n",
974 unit);
975 /*
976 * Stop/reset/re-init NIC
977 */
978 ae_reset(unit);
979 } else {
980
981 /*
982 * Receiver Error. One or more of: CRC error, frame
983 * alignment error FIFO overrun, or missed packet.
984 */
985 if (isr & AE_ISR_RXE) {
986 ++sc->arpcom.ac_if.if_ierrors;
987 #ifdef AE_DEBUG
988 printf("ae%d: receive error %x\n", unit,
989 NIC_GET(sc, AE_P0_RSR));
990 #endif
991 }
992
993 /*
994 * Go get the packet(s)
995 * XXX - Doing this on an error is dubious
996 * because there shouldn't be any data to
997 * get (we've configured the interface to
998 * not accept packets with errors).
999 */
1000 ae_rint (unit);
1001 }
1002 }
1003
1004 /*
1005 * If it looks like the transmitter can take more data,
1006 * attempt to start output on the interface.
1007 * This is done after handling the receiver to
1008 * give the receiver priority.
1009 */
1010 if ((sc->arpcom.ac_if.if_flags & IFF_OACTIVE) == 0)
1011 ae_start(&sc->arpcom.ac_if);
1012
1013 /*
1014 * return NIC CR to standard state: page 0, remote DMA complete,
1015 * start (toggling the TXP bit off, even if was just set
1016 * in the transmit routine, is *okay* - it is 'edge'
1017 * triggered from low to high)
1018 */
1019 NIC_PUT(sc, AE_P0_CR, AE_CR_RD2|AE_CR_STA);
1020
1021 /*
1022 * If the Network Talley Counters overflow, read them to
1023 * reset them. It appears that old 8390's won't
1024 * clear the ISR flag otherwise - resulting in an
1025 * infinite loop.
1026 */
1027 if (isr & AE_ISR_CNT) {
1028 (void) NIC_GET(sc, AE_P0_CNTR0);
1029 (void) NIC_GET(sc, AE_P0_CNTR1);
1030 (void) NIC_GET(sc, AE_P0_CNTR2);
1031 }
1032 }
1033 }
1034
1035 /*
1036 * Process an ioctl request. This code needs some work - it looks
1037 * pretty ugly.
1038 */
1039 int
1040 ae_ioctl(ifp, command, data)
1041 register struct ifnet *ifp;
1042 int command;
1043 caddr_t data;
1044 {
1045 register struct ifaddr *ifa = (struct ifaddr *)data;
1046 struct ae_softc *sc = &ae_softc[ifp->if_unit];
1047 struct ifreq *ifr = (struct ifreq *)data;
1048 int s, error = 0;
1049
1050 s = splnet();
1051
1052 switch (command) {
1053
1054 case SIOCSIFADDR:
1055 ifp->if_flags |= IFF_UP;
1056
1057 switch (ifa->ifa_addr->sa_family) {
1058 #ifdef INET
1059 case AF_INET:
1060 ae_init(ifp->if_unit); /* before arpwhohas */
1061 /*
1062 * See if another station has *our* IP address.
1063 * i.e.: There is an address conflict! If a
1064 * conflict exists, a message is sent to the
1065 * console.
1066 */
1067 ((struct arpcom *)ifp)->ac_ipaddr =
1068 IA_SIN(ifa)->sin_addr;
1069 arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
1070 break;
1071 #endif
1072 #ifdef NS
1073 /*
1074 * XXX - This code is probably wrong
1075 */
1076 case AF_NS:
1077 {
1078 register struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
1079
1080 if (ns_nullhost(*ina))
1081 ina->x_host =
1082 *(union ns_host *)(sc->arpcom.ac_enaddr);
1083 else {
1084 /*
1085 *
1086 */
1087 bcopy((caddr_t)ina->x_host.c_host,
1088 (caddr_t)sc->arpcom.ac_enaddr,
1089 sizeof(sc->arpcom.ac_enaddr));
1090 }
1091 /*
1092 * Set new address
1093 */
1094 ae_init(ifp->if_unit);
1095 break;
1096 }
1097 #endif
1098 default:
1099 ae_init(ifp->if_unit);
1100 break;
1101 }
1102 break;
1103
1104 case SIOCSIFFLAGS:
1105 /*
1106 * If interface is marked down and it is running, then stop it
1107 */
1108 if (((ifp->if_flags & IFF_UP) == 0) &&
1109 (ifp->if_flags & IFF_RUNNING)) {
1110 ae_stop(ifp->if_unit);
1111 ifp->if_flags &= ~IFF_RUNNING;
1112 } else {
1113 /*
1114 * If interface is marked up and it is stopped, then start it
1115 */
1116 if ((ifp->if_flags & IFF_UP) &&
1117 ((ifp->if_flags & IFF_RUNNING) == 0))
1118 ae_init(ifp->if_unit);
1119 }
1120 #if NBPFILTER > 0
1121 if (ifp->if_flags & IFF_PROMISC) {
1122 /*
1123 * Set promiscuous mode on interface.
1124 * XXX - for multicasts to work, we would need to
1125 * write 1's in all bits of multicast
1126 * hashing array. For now we assume that
1127 * this was done in ae_init().
1128 */
1129 NIC_PUT(sc, AE_P0_RCR,
1130 AE_RCR_PRO|AE_RCR_AM|AE_RCR_AB);
1131 } else {
1132 /*
1133 * XXX - for multicasts to work, we would need to
1134 * rewrite the multicast hashing array with the
1135 * proper hash (would have been destroyed above).
1136 */
1137 NIC_PUT(sc, AE_P0_RCR, AE_RCR_AB);
1138 }
1139 #endif
1140 break;
1141
1142 default:
1143 error = EINVAL;
1144 }
1145 (void) splx(s);
1146 return (error);
1147 }
1148
1149 /*
1150 * Macro to calculate a new address within shared memory when given an offset
1151 * from an address, taking into account ring-wrap.
1152 */
1153 #define ringoffset(sc, start, off, type) \
1154 ((type)( ((caddr_t)(start)+(off) >= (sc)->smem_end) ? \
1155 (((caddr_t)(start)+(off))) - (sc)->smem_end \
1156 + (sc)->smem_ring: \
1157 ((caddr_t)(start)+(off)) ))
1158
1159 /*
1160 * Retreive packet from shared memory and send to the next level up via
1161 * ether_input(). If there is a BPF listener, give a copy to BPF, too.
1162 */
1163 ae_get_packet(sc, buf, len)
1164 struct ae_softc *sc;
1165 char *buf;
1166 u_short len;
1167 {
1168 struct ether_header *eh;
1169 struct mbuf *m, *head, *ae_ring_to_mbuf();
1170 u_short off;
1171 int resid;
1172 u_short etype;
1173 struct trailer_header {
1174 u_short trail_type;
1175 u_short trail_residual;
1176 } trailer_header;
1177
1178 /* Allocate a header mbuf */
1179 MGETHDR(m, M_DONTWAIT, MT_DATA);
1180 if (m == 0)
1181 goto bad;
1182 m->m_pkthdr.rcvif = &sc->arpcom.ac_if;
1183 m->m_pkthdr.len = len;
1184 m->m_len = 0;
1185 head = m;
1186
1187 eh = (struct ether_header *)buf;
1188
1189 /* The following sillines is to make NFS happy */
1190 #define EROUND ((sizeof(struct ether_header) + 3) & ~3)
1191 #define EOFF (EROUND - sizeof(struct ether_header))
1192
1193 /*
1194 * The following assumes there is room for
1195 * the ether header in the header mbuf
1196 */
1197 head->m_data += EOFF;
1198 bcopy(buf, mtod(head, caddr_t), sizeof(struct ether_header));
1199 buf += sizeof(struct ether_header);
1200 head->m_len += sizeof(struct ether_header);
1201 len -= sizeof(struct ether_header);
1202
1203 etype = ntohs((u_short)eh->ether_type);
1204
1205 /*
1206 * Deal with trailer protocol:
1207 * If trailer protocol, calculate the datasize as 'off',
1208 * which is also the offset to the trailer header.
1209 * Set resid to the amount of packet data following the
1210 * trailer header.
1211 * Finally, copy residual data into mbuf chain.
1212 */
1213 if (etype >= ETHERTYPE_TRAIL &&
1214 etype < ETHERTYPE_TRAIL+ETHERTYPE_NTRAILER) {
1215
1216 off = (etype - ETHERTYPE_TRAIL) << 9;
1217 if ((off + sizeof(struct trailer_header)) > len)
1218 goto bad; /* insanity */
1219
1220 eh->ether_type = *ringoffset(sc, buf, off, u_short *);
1221 resid = ntohs(*ringoffset(sc, buf, off+2, u_short *));
1222
1223 if ((off + resid) > len) goto bad; /* insanity */
1224
1225 resid -= sizeof(struct trailer_header);
1226 if (resid < 0) goto bad; /* insanity */
1227
1228 m = ae_ring_to_mbuf(sc, ringoffset(sc, buf, off+4, char *), head, resid);
1229 if (m == 0) goto bad;
1230
1231 len = off;
1232 head->m_pkthdr.len -= 4; /* subtract trailer header */
1233 }
1234
1235 /*
1236 * Pull packet off interface. Or if this was a trailer packet,
1237 * the data portion is appended.
1238 */
1239 m = ae_ring_to_mbuf(sc, buf, m, len);
1240 if (m == 0) goto bad;
1241
1242 #if NBPFILTER > 0
1243 /*
1244 * Check if there's a BPF listener on this interface.
1245 * If so, hand off the raw packet to bpf.
1246 */
1247 if (sc->bpf) {
1248 bpf_mtap(sc->bpf, head);
1249
1250 /*
1251 * Note that the interface cannot be in promiscuous mode if
1252 * there are no BPF listeners. And if we are in promiscuous
1253 * mode, we have to check if this packet is really ours.
1254 *
1255 * XXX This test does not support multicasts.
1256 */
1257 if ((sc->arpcom.ac_if.if_flags & IFF_PROMISC) &&
1258 bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr,
1259 sizeof(eh->ether_dhost)) != 0 &&
1260 bcmp(eh->ether_dhost, etherbroadcastaddr,
1261 sizeof(eh->ether_dhost)) != 0) {
1262
1263 m_freem(head);
1264 return;
1265 }
1266 }
1267 #endif
1268
1269 /*
1270 * Fix up data start offset in mbuf to point past ether header
1271 */
1272 m_adj(head, sizeof(struct ether_header));
1273
1274 ether_input(&sc->arpcom.ac_if, eh, head);
1275 return;
1276
1277 bad: if (head)
1278 m_freem(head);
1279 return;
1280 }
1281
1282 /*
1283 * Supporting routines
1284 */
1285
1286 /*
1287 * Given a source and destination address, copy 'amount' of a packet from
1288 * the ring buffer into a linear destination buffer. Takes into account
1289 * ring-wrap.
1290 */
1291 static inline char *
1292 ae_ring_copy(sc,src,dst,amount)
1293 struct ae_softc *sc;
1294 char *src;
1295 char *dst;
1296 u_short amount;
1297 {
1298 u_short tmp_amount;
1299
1300 /* does copy wrap to lower addr in ring buffer? */
1301 if (src + amount > sc->smem_end) {
1302 tmp_amount = sc->smem_end - src;
1303 bcopy(src, dst, tmp_amount); /* copy amount up to end of smem */
1304 amount -= tmp_amount;
1305 src = sc->smem_ring;
1306 dst += tmp_amount;
1307 }
1308
1309 bcopy(src, dst, amount);
1310
1311 return(src + amount);
1312 }
1313
1314 /*
1315 * Copy data from receive buffer to end of mbuf chain
1316 * allocate additional mbufs as needed. return pointer
1317 * to last mbuf in chain.
1318 * sc = ed info (softc)
1319 * src = pointer in ed ring buffer
1320 * dst = pointer to last mbuf in mbuf chain to copy to
1321 * amount = amount of data to copy
1322 */
1323 struct mbuf *
1324 ae_ring_to_mbuf(sc,src,dst,total_len)
1325 struct ae_softc *sc;
1326 char *src;
1327 struct mbuf *dst;
1328 u_short total_len;
1329 {
1330 register struct mbuf *m = dst;
1331
1332 while (total_len) {
1333 register u_short amount = min(total_len, M_TRAILINGSPACE(m));
1334
1335 if (amount == 0) { /* no more data in this mbuf, alloc another */
1336 /*
1337 * If there is enough data for an mbuf cluster, attempt
1338 * to allocate one of those, otherwise, a regular
1339 * mbuf will do.
1340 * Note that a regular mbuf is always required, even if
1341 * we get a cluster - getting a cluster does not
1342 * allocate any mbufs, and one is needed to assign
1343 * the cluster to. The mbuf that has a cluster
1344 * extension can not be used to contain data - only
1345 * the cluster can contain data.
1346 */
1347 dst = m;
1348 MGET(m, M_DONTWAIT, MT_DATA);
1349 if (m == 0)
1350 return (0);
1351
1352 if (total_len >= MINCLSIZE)
1353 MCLGET(m, M_DONTWAIT);
1354
1355 m->m_len = 0;
1356 dst->m_next = m;
1357 amount = min(total_len, M_TRAILINGSPACE(m));
1358 }
1359
1360 src = ae_ring_copy(sc, src, mtod(m, caddr_t) + m->m_len, amount);
1361
1362 m->m_len += amount;
1363 total_len -= amount;
1364
1365 }
1366 return (m);
1367 }
1368 #endif
1369
1370