Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / arch / amiga / dev / if_ed.c
blob546549624f2035ac2df750e96735c710dbca5840
1 /* $NetBSD: if_ed.c,v 1.59 2009/05/19 18:39:26 phx Exp $ */
3 /*
4 * Device driver for National Semiconductor DS8390/WD83C690 based ethernet
5 * adapters.
7 * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved.
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.
15 * Currently supports the Hydra Systems ethernet card.
18 #include "opt_inet.h"
19 #include "opt_ns.h"
21 #include <sys/cdefs.h>
22 __KERNEL_RCSID(0, "$NetBSD: if_ed.c,v 1.59 2009/05/19 18:39:26 phx Exp $");
24 #include "bpfilter.h"
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>
35 #include <net/if.h>
36 #include <net/if_dl.h>
37 #include <net/if_types.h>
38 #include <net/if_ether.h>
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_inarp.h>
46 #endif
48 #ifdef NS
49 #include <netns/ns.h>
50 #include <netns/ns_if.h>
51 #endif
53 #if NBPFILTER > 0
54 #include <net/bpf.h>
55 #include <net/bpfdesc.h>
56 #endif
58 #include <machine/cpu.h>
60 #include <amiga/amiga/device.h>
61 #include <amiga/amiga/isr.h>
62 #include <amiga/dev/zbusvar.h>
63 #include <dev/ic/dp8390reg.h>
64 #include <amiga/dev/if_edreg.h>
66 #define HYDRA_MANID 2121
67 #define HYDRA_PRODID 1
69 #define ASDG_MANID 1023
70 #define ASDG_PRODID 254
73 * ed_softc: per line info and status
75 struct ed_softc {
76 struct device sc_dev;
77 struct isr sc_isr;
79 struct ethercom sc_ethercom; /* ethernet common */
81 u_char volatile *nic_addr; /* NIC (DS8390) I/O address */
83 u_char cr_proto; /* values always set in CR */
85 void * mem_start; /* NIC memory start address */
86 void * mem_end; /* NIC memory end address */
87 u_long mem_size; /* total NIC memory size */
88 void * mem_ring; /* start of RX ring-buffer (in NIC mem) */
90 u_char xmit_busy; /* transmitter is busy */
91 u_char txb_cnt; /* number of transmit buffers */
92 u_char txb_inuse; /* number of TX buffers currently in-use*/
94 u_char txb_new; /* pointer to where new buffer will be added */
95 u_char txb_next_tx; /* pointer to next buffer ready to xmit */
96 u_short txb_len[8]; /* buffered xmit buffer lengths */
97 u_char tx_page_start; /* first page of TX buffer area */
98 u_char rec_page_start; /* first page of RX ring-buffer */
99 u_char rec_page_stop; /* last page of RX ring-buffer */
100 u_char next_packet; /* pointer to next unread RX packet */
103 int ed_zbus_match(struct device *, struct cfdata *, void *);
104 void ed_zbus_attach(struct device *, struct device *, void *);
105 int edintr(void *);
106 int ed_ioctl(struct ifnet *, u_long, void *);
107 void ed_start(struct ifnet *);
108 void ed_watchdog(struct ifnet *);
109 void ed_reset(struct ed_softc *);
110 void ed_init(struct ed_softc *);
111 void ed_stop(struct ed_softc *);
112 void ed_getmcaf(struct ethercom *, u_long *);
113 u_short ed_put(struct ed_softc *, struct mbuf *, void *);
115 #define inline /* XXX for debugging porpoises */
117 void ed_get_packet(struct ed_softc *, void *, u_short);
118 static inline void ed_rint(struct ed_softc *);
119 static inline void ed_xmit(struct ed_softc *);
120 static inline void *ed_ring_copy(struct ed_softc *, void *, void *,
121 u_short);
123 static inline void NIC_PUT(struct ed_softc *, int, u_char);
124 static inline u_char NIC_GET(struct ed_softc *, int);
125 static inline void word_copy(void *, void *, int);
126 static inline void word_zero(void *, int);
127 struct mbuf *ed_ring_to_mbuf(struct ed_softc *, void *, struct mbuf *,
128 u_short);
130 CFATTACH_DECL(ed_zbus, sizeof(struct ed_softc),
131 ed_zbus_match, ed_zbus_attach, NULL, NULL);
133 static inline void
134 NIC_PUT(struct ed_softc *sc, int off, u_char val)
136 sc->nic_addr[off * 2] = val;
137 #ifdef not_def
139 * This was being used to *slow* access to the bus. I don't
140 * believe it is needed but I'll leave it around incase probelms
141 * pop-up
143 (void)ciaa.pra;
144 #endif
147 static inline u_char
148 NIC_GET(struct ed_softc *sc, int off)
150 register u_char val;
152 val = sc->nic_addr[off * 2];
153 #ifdef not_def
155 * This was being used to *slow* access to the bus. I don't
156 * believe it is needed but I'll leave it around incase probelms
157 * pop-up
159 (void)ciaa.pra;
160 #endif
161 return (val);
165 * Memory copy, copies word at time.
167 static inline void
168 word_copy(void *a, void *b, int len)
170 u_short *x = (u_short *)a,
171 *y = (u_short *)b;
173 len >>= 1;
174 while (len--)
175 *y++ = *x++;
179 * zero memory, one word at time.
181 static inline void
182 word_zero(void *a, int len)
184 u_short *x = (u_short *)a;
186 len >>= 1;
187 while (len--)
188 *x++ = 0;
192 ed_zbus_match(struct device *parent, struct cfdata *cfp, void *aux)
194 struct zbus_args *zap = aux;
196 if (zap->manid == HYDRA_MANID && zap->prodid == HYDRA_PRODID)
197 return (1);
198 else if (zap->manid == ASDG_MANID && zap->prodid == ASDG_PRODID)
199 return (1);
200 return (0);
203 void
204 ed_zbus_attach(struct device *parent, struct device *self, void *aux)
206 struct ed_softc *sc = (void *)self;
207 struct zbus_args *zap = aux;
208 struct cfdata *cf = device_cfdata(&sc->sc_dev);
209 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
210 volatile u_char *prom;
211 int i;
212 u_int8_t myaddr[ETHER_ADDR_LEN];
214 if (zap->manid == HYDRA_MANID) {
215 sc->mem_start = zap->va;
216 sc->mem_size = 16384;
217 sc->nic_addr = (u_char *)sc->mem_start + HYDRA_NIC_BASE;
218 prom = (u_char *)sc->mem_start + HYDRA_ADDRPROM;
219 } else {
220 sc->mem_start = (u_char *)zap->va + 0x8000;
221 sc->mem_size = 16384;
222 sc->nic_addr = (volatile u_char *)zap->va + ASDG_NIC_BASE;
223 prom = (volatile u_char *)sc->nic_addr + ASDG_ADDRPROM;
225 sc->cr_proto = ED_CR_RD2;
226 sc->tx_page_start = 0;
228 sc->mem_end = (u_char *)sc->mem_start + sc->mem_size;
231 * Use one xmit buffer if < 16k, two buffers otherwise (if not told
232 * otherwise).
234 if ((sc->mem_size < 16384) || zap->manid == ASDG_MANID
235 || (cf->cf_flags & ED_FLAGS_NO_MULTI_BUFFERING))
236 sc->txb_cnt = 1;
237 else
238 sc->txb_cnt = 2;
240 sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
241 sc->rec_page_stop = sc->tx_page_start + (sc->mem_size >> ED_PAGE_SHIFT);
243 sc->mem_ring =
244 (u_char *)sc->mem_start +
245 ((sc->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
248 * Interrupts must be inactive when reading the prom, as the interrupt
249 * line is shared with one of its address lines.
252 NIC_PUT(sc, ED_P0_IMR, 0x00); /* disable ints */
253 NIC_PUT(sc, ED_P0_ISR, 0xff); /* clear ints */
256 * read the ethernet address from the board
258 for (i = 0; i < ETHER_ADDR_LEN; i++)
259 myaddr[i] = *(prom + 2 * i);
261 /* Set interface to stopped condition (reset). */
262 ed_stop(sc);
264 /* Initialize ifnet structure. */
265 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ);
266 ifp->if_softc = sc;
267 ifp->if_start = ed_start;
268 ifp->if_ioctl = ed_ioctl;
269 ifp->if_watchdog = ed_watchdog;
270 ifp->if_flags =
271 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
273 /* Attach the interface. */
274 if_attach(ifp);
275 ether_ifattach(ifp, myaddr);
277 /* Print additional info when attached. */
278 printf(": address %s\n", ether_sprintf(myaddr));
280 sc->sc_isr.isr_intr = edintr;
281 sc->sc_isr.isr_arg = sc;
282 sc->sc_isr.isr_ipl = 2;
283 add_isr(&sc->sc_isr);
287 * Reset interface.
289 void
290 ed_reset(struct ed_softc *sc)
292 int s;
294 s = splnet();
295 ed_stop(sc);
296 ed_init(sc);
297 splx(s);
298 log(LOG_ERR, "%s: reset\n", sc->sc_dev.dv_xname);
302 * Take interface offline.
304 void
305 ed_stop(struct ed_softc *sc)
307 int n = 5000;
309 /* Stop everything on the interface, and select page 0 registers. */
310 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
313 * Wait for interface to enter stopped state, but limit # of checks to
314 * 'n' (about 5ms). It shouldn't even take 5us on modern DS8390's, but
315 * just in case it's an old one.
317 while (((NIC_GET(sc, ED_P0_ISR) & ED_ISR_RST) == 0) && --n);
321 * Device timeout/watchdog routine. Entered if the device neglects to generate
322 * an interrupt after a transmit has been started on it.
324 void
325 ed_watchdog(struct ifnet *ifp)
327 struct ed_softc *sc = ifp->if_softc;
329 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
330 ++ifp->if_oerrors;
332 ed_reset(sc);
336 * Initialize device.
338 void
339 ed_init(struct ed_softc *sc)
341 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
342 int i, s;
343 u_long mcaf[2];
346 * Initialize the NIC in the exact order outlined in the NS manual.
347 * This init procedure is "mandatory"...don't change what or when
348 * things happen.
350 s = splnet();
352 /* Reset transmitter flags. */
353 sc->xmit_busy = 0;
354 ifp->if_timer = 0;
356 sc->txb_inuse = 0;
357 sc->txb_new = 0;
358 sc->txb_next_tx = 0;
360 /* Set interface for page 0, remote DMA complete, stopped. */
361 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
364 * Set FIFO threshold to 8, No auto-init Remote DMA, byte
365 * order=68k, word-wide DMA xfers,
366 * XXX changed to use 2 word threshhold
368 NIC_PUT(sc, ED_P0_DCR,
369 ED_DCR_FT0 | ED_DCR_WTS | ED_DCR_LS | ED_DCR_BOS);
371 /* Clear remote byte count registers. */
372 NIC_PUT(sc, ED_P0_RBCR0, 0);
373 NIC_PUT(sc, ED_P0_RBCR1, 0);
375 /* Tell RCR to do nothing for now. */
376 NIC_PUT(sc, ED_P0_RCR, ED_RCR_MON);
378 /* Place NIC in internal loopback mode. */
379 NIC_PUT(sc, ED_P0_TCR, ED_TCR_LB0);
381 /* Initialize receive buffer ring. */
382 NIC_PUT(sc, ED_P0_BNRY, sc->rec_page_start);
383 NIC_PUT(sc, ED_P0_PSTART, sc->rec_page_start);
384 NIC_PUT(sc, ED_P0_PSTOP, sc->rec_page_stop);
387 * Clear all interrupts. A '1' in each bit position clears the
388 * corresponding flag.
390 NIC_PUT(sc, ED_P0_ISR, 0xff);
393 * Enable the following interrupts: receive/transmit complete,
394 * receive/transmit error, and Receiver OverWrite.
396 * Counter overflow and Remote DMA complete are *not* enabled.
398 NIC_PUT(sc, ED_P0_IMR,
399 ED_IMR_PRXE | ED_IMR_PTXE | ED_IMR_RXEE | ED_IMR_TXEE |
400 ED_IMR_OVWE);
402 /* Program command register for page 1. */
403 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
405 /* Copy out our station address. */
406 for (i = 0; i < ETHER_ADDR_LEN; ++i)
407 NIC_PUT(sc, ED_P1_PAR0 + i, CLLADDR(ifp->if_sadl)[i]);
409 /* Set multicast filter on chip. */
410 ed_getmcaf(&sc->sc_ethercom, mcaf);
411 for (i = 0; i < 8; i++)
412 NIC_PUT(sc, ED_P1_MAR0 + i, ((u_char *)mcaf)[i]);
415 * Set current page pointer to one page after the boundary pointer, as
416 * recommended in the National manual.
418 sc->next_packet = sc->rec_page_start + 1;
419 NIC_PUT(sc, ED_P1_CURR, sc->next_packet);
421 /* Program command register for page 0. */
422 NIC_PUT(sc, ED_P1_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
424 i = ED_RCR_AB | ED_RCR_AM;
425 if (ifp->if_flags & IFF_PROMISC) {
427 * Set promiscuous mode. Multicast filter was set earlier so
428 * that we should receive all multicast packets.
430 i |= ED_RCR_PRO | ED_RCR_AR | ED_RCR_SEP;
432 NIC_PUT(sc, ED_P0_RCR, i);
434 /* Take interface out of loopback. */
435 NIC_PUT(sc, ED_P0_TCR, 0);
437 /* Fire up the interface. */
438 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
440 /* Set 'running' flag, and clear output active flag. */
441 ifp->if_flags |= IFF_RUNNING;
442 ifp->if_flags &= ~IFF_OACTIVE;
444 /* ...and attempt to start output. */
445 ed_start(ifp);
447 splx(s);
451 * This routine actually starts the transmission on the interface.
453 static inline void
454 ed_xmit(struct ed_softc *sc)
456 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
457 u_short len;
459 len = sc->txb_len[sc->txb_next_tx];
461 /* Set NIC for page 0 register access. */
462 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
464 /* Set TX buffer start page. */
465 NIC_PUT(sc, ED_P0_TPSR, sc->tx_page_start +
466 sc->txb_next_tx * ED_TXBUF_SIZE);
468 /* Set TX length. */
469 NIC_PUT(sc, ED_P0_TBCR0, len);
470 NIC_PUT(sc, ED_P0_TBCR1, len >> 8);
472 /* Set page 0, remote DMA complete, transmit packet, and *start*. */
473 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_TXP | ED_CR_STA);
474 sc->xmit_busy = 1;
476 /* Point to next transmit buffer slot and wrap if necessary. */
477 sc->txb_next_tx++;
478 if (sc->txb_next_tx == sc->txb_cnt)
479 sc->txb_next_tx = 0;
481 /* Set a timer just in case we never hear from the board again. */
482 ifp->if_timer = 2;
486 * Start output on interface.
487 * We make two assumptions here:
488 * 1) that the current priority is set to splnet _before_ this code
489 * is called *and* is returned to the appropriate priority after
490 * return
491 * 2) that the IFF_OACTIVE flag is checked before this code is called
492 * (i.e. that the output part of the interface is idle)
494 void
495 ed_start(struct ifnet *ifp)
497 struct ed_softc *sc = ifp->if_softc;
498 struct mbuf *m0, *m;
499 void *buffer;
500 int len;
502 outloop:
504 * First, see if there are buffered packets and an idle transmitter -
505 * should never happen at this point.
507 if (sc->txb_inuse && (sc->xmit_busy == 0)) {
508 printf("%s: packets buffered, but transmitter idle\n",
509 sc->sc_dev.dv_xname);
510 ed_xmit(sc);
513 /* See if there is room to put another packet in the buffer. */
514 if (sc->txb_inuse == sc->txb_cnt) {
515 /* No room. Indicate this to the outside world and exit. */
516 ifp->if_flags |= IFF_OACTIVE;
517 return;
520 IF_DEQUEUE(&ifp->if_snd, m);
521 if (m == 0) {
523 * We are using the !OACTIVE flag to indicate to the outside
524 * world that we can accept an additional packet rather than
525 * that the transmitter is _actually_ active. Indeed, the
526 * transmitter may be active, but if we haven't filled all the
527 * buffers with data then we still want to accept more.
529 ifp->if_flags &= ~IFF_OACTIVE;
530 return;
533 /* Copy the mbuf chain into the transmit buffer. */
534 m0 = m;
536 /* txb_new points to next open buffer slot. */
537 buffer = (char*)sc->mem_start +
538 ((sc->txb_new * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
540 len = ed_put(sc, m, buffer);
542 sc->txb_len[sc->txb_new] = len;
543 sc->txb_inuse++;
545 /* Point to next buffer slot and wrap if necessary. */
546 if (++sc->txb_new == sc->txb_cnt)
547 sc->txb_new = 0;
549 if (sc->xmit_busy == 0)
550 ed_xmit(sc);
552 #if NBPFILTER > 0
553 /* Tap off here if there is a BPF listener. */
554 if (ifp->if_bpf)
555 bpf_mtap(ifp->if_bpf, m0);
556 #endif
558 m_freem(m0);
560 /* Loop back to the top to possibly buffer more packets. */
561 goto outloop;
565 * Ethernet interface receiver interrupt.
567 static inline void
568 ed_rint(struct ed_softc *sc)
570 struct ifnet *ifp;
571 void *packet_ptr;
572 u_short len;
573 u_char nlen;
574 u_char boundary, current;
575 struct ed_ring packet_hdr;
577 ifp = &sc->sc_ethercom.ec_if;
578 loop:
579 /* Set NIC to page 1 registers to get 'current' pointer. */
580 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
583 * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
584 * it points to where new data has been buffered. The 'CURR' (current)
585 * register points to the logical end of the ring-buffer - i.e. it
586 * points to where additional new data will be added. We loop here
587 * until the logical beginning equals the logical end (or in other
588 * words, until the ring-buffer is empty).
590 current = NIC_GET(sc, ED_P1_CURR);
591 if (sc->next_packet == current)
592 return;
594 /* Set NIC to page 0 registers to update boundary register. */
595 NIC_PUT(sc, ED_P1_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
597 do {
598 /* Get pointer to this buffer's header structure. */
599 packet_ptr = (char*)sc->mem_ring +
600 ((sc->next_packet - sc->rec_page_start) << ED_PAGE_SHIFT);
603 * The byte count includes a 4 byte header that was added by
604 * the NIC.
606 packet_hdr = *(struct ed_ring *)packet_ptr;
607 packet_hdr.count = ((packet_hdr.count >> 8) & 0xff)
608 | ((packet_hdr.count & 0xff) << 8);
609 len = packet_hdr.count;
611 * Try do deal with old, buggy chips that sometimes duplicate
612 * the low byte of the length into the high byte. We do this
613 * by simply ignoring the high byte of the length and always
614 * recalculating it.
616 * NOTE: sc->next_packet is pointing at the current packet.
618 if (packet_hdr.next_packet >= sc->next_packet)
619 nlen = (packet_hdr.next_packet - sc->next_packet);
620 else
621 nlen = ((packet_hdr.next_packet - sc->rec_page_start) +
622 (sc->rec_page_stop - sc->next_packet));
623 --nlen;
624 if ((len & ED_PAGE_MASK) + sizeof(packet_hdr) > ED_PAGE_SIZE)
625 --nlen;
626 len = (len & ED_PAGE_MASK) | (nlen << ED_PAGE_SHIFT);
627 #ifdef DIAGNOSTIC
628 if (len != packet_hdr.count) {
629 printf("%s: length does not match next packet pointer\n",
630 sc->sc_dev.dv_xname);
631 printf("%s: len %04x nlen %04x start %02x first %02x curr %02x next %02x stop %02x\n",
632 sc->sc_dev.dv_xname, packet_hdr.count, len,
633 sc->rec_page_start, sc->next_packet, current,
634 packet_hdr.next_packet, sc->rec_page_stop);
636 #endif
639 * Be fairly liberal about what we allow as a "reasonable"
640 * length so that a [crufty] packet will make it to BPF (and
641 * can thus be analyzed). Note that all that is really
642 * important is that we have a length that will fit into one
643 * mbuf cluster or less; the upper layer protocols can then
644 * figure out the length from their own length field(s).
646 * MCLBYTES may be less than a valid packet len. Thus
647 * we use a constant that is large enough.
649 if (len <= 2048 &&
650 packet_hdr.next_packet >= sc->rec_page_start &&
651 packet_hdr.next_packet < sc->rec_page_stop) {
652 /* Go get packet. */
653 ed_get_packet(sc, (char*)packet_ptr +
654 sizeof(struct ed_ring),
655 len - sizeof(struct ed_ring));
656 ++ifp->if_ipackets;
657 } else {
658 /* Really BAD. The ring pointers are corrupted. */
659 #ifdef DEBUG_AMIGA_IF_ED
660 log(LOG_ERR,
661 "%s: NIC memory corrupt - invalid packet length %d\n",
662 sc->sc_dev.dv_xname, len);
663 #endif
664 ++ifp->if_ierrors;
665 ed_reset(sc);
666 return;
669 /* Update next packet pointer. */
670 sc->next_packet = packet_hdr.next_packet;
673 * Update NIC boundary pointer - being careful to keep it one
674 * buffer behind (as recommended by NS databook).
676 boundary = sc->next_packet - 1;
677 if (boundary < sc->rec_page_start)
678 boundary = sc->rec_page_stop - 1;
679 NIC_PUT(sc, ED_P0_BNRY, boundary);
680 } while (sc->next_packet != current);
682 goto loop;
685 /* Ethernet interface interrupt processor. */
687 edintr(void *arg)
689 struct ed_softc *sc = arg;
690 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
691 u_char isr;
693 /* Set NIC to page 0 registers. */
694 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
696 isr = NIC_GET(sc, ED_P0_ISR);
697 if (!isr)
698 return (0);
700 /* Loop until there are no more new interrupts. */
701 for (;;) {
703 * Reset all the bits that we are 'acknowledging' by writing a
704 * '1' to each bit position that was set.
705 * (Writing a '1' *clears* the bit.)
707 NIC_PUT(sc, ED_P0_ISR, isr);
710 * Handle transmitter interrupts. Handle these first because
711 * the receiver will reset the board under some conditions.
713 if (isr & (ED_ISR_PTX | ED_ISR_TXE)) {
714 u_char collisions = NIC_GET(sc, ED_P0_NCR) & 0x0f;
717 * Check for transmit error. If a TX completed with an
718 * error, we end up throwing the packet away. Really
719 * the only error that is possible is excessive
720 * collisions, and in this case it is best to allow the
721 * automatic mechanisms of TCP to backoff the flow. Of
722 * course, with UDP we're screwed, but this is expected
723 * when a network is heavily loaded.
725 (void) NIC_GET(sc, ED_P0_TSR);
726 if (isr & ED_ISR_TXE) {
728 * Excessive collisions (16).
730 if ((NIC_GET(sc, ED_P0_TSR) & ED_TSR_ABT)
731 && (collisions == 0)) {
733 * When collisions total 16, the P0_NCR
734 * will indicate 0, and the TSR_ABT is
735 * set.
737 collisions = 16;
740 /* Update output errors counter. */
741 ++ifp->if_oerrors;
742 } else {
744 * Update total number of successfully
745 * transmitted packets.
747 ++ifp->if_opackets;
750 /* Reset TX busy and output active flags. */
751 sc->xmit_busy = 0;
752 ifp->if_flags &= ~IFF_OACTIVE;
754 /* Clear watchdog timer. */
755 ifp->if_timer = 0;
758 * Add in total number of collisions on last
759 * transmission.
761 ifp->if_collisions += collisions;
764 * Decrement buffer in-use count if not zero (can only
765 * be zero if a transmitter interrupt occurred while not
766 * actually transmitting).
767 * If data is ready to transmit, start it transmitting,
768 * otherwise defer until after handling receiver.
770 if (sc->txb_inuse && --sc->txb_inuse)
771 ed_xmit(sc);
774 /* Handle receiver interrupts. */
775 if (isr & (ED_ISR_PRX | ED_ISR_RXE | ED_ISR_OVW)) {
777 * Overwrite warning. In order to make sure that a
778 * lockup of the local DMA hasn't occurred, we reset
779 * and re-init the NIC. The NSC manual suggests only a
780 * partial reset/re-init is necessary - but some chips
781 * seem to want more. The DMA lockup has been seen
782 * only with early rev chips - Methinks this bug was
783 * fixed in later revs. -DG
785 if (isr & ED_ISR_OVW) {
786 ++ifp->if_ierrors;
787 #ifdef DIAGNOSTIC
788 log(LOG_WARNING,
789 "%s: warning - receiver ring buffer overrun\n",
790 sc->sc_dev.dv_xname);
791 #endif
792 /* Stop/reset/re-init NIC. */
793 ed_reset(sc);
794 } else {
796 * Receiver Error. One or more of: CRC error,
797 * frame alignment error FIFO overrun, or
798 * missed packet.
800 if (isr & ED_ISR_RXE) {
801 ++ifp->if_ierrors;
802 #ifdef ED_DEBUG
803 printf("%s: receive error %x\n",
804 sc->sc_dev.dv_xname,
805 NIC_GET(sc, ED_P0_RSR));
806 #endif
810 * Go get the packet(s).
811 * XXX - Doing this on an error is dubious
812 * because there shouldn't be any data to get
813 * (we've configured the interface to not
814 * accept packets with errors).
816 ed_rint(sc);
821 * If it looks like the transmitter can take more data, attempt
822 * to start output on the interface. This is done after
823 * handling the receiver to give the receiver priority.
825 if ((ifp->if_flags & IFF_OACTIVE) == 0)
826 ed_start(ifp);
829 * Return NIC CR to standard state: page 0, remote DMA
830 * complete, start (toggling the TXP bit off, even if was just
831 * set in the transmit routine, is *okay* - it is 'edge'
832 * triggered from low to high).
834 NIC_PUT(sc, ED_P0_CR, sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
837 * If the Network Talley Counters overflow, read them to reset
838 * them. It appears that old 8390's won't clear the ISR flag
839 * otherwise - resulting in an infinite loop.
841 if (isr & ED_ISR_CNT) {
842 (void) NIC_GET(sc, ED_P0_CNTR0);
843 (void) NIC_GET(sc, ED_P0_CNTR1);
844 (void) NIC_GET(sc, ED_P0_CNTR2);
847 isr = NIC_GET(sc, ED_P0_ISR);
848 if (!isr)
849 return (1);
854 * Process an ioctl request. This code needs some work - it looks pretty ugly.
857 ed_ioctl(register struct ifnet *ifp, u_long cmd, void *data)
859 struct ed_softc *sc = ifp->if_softc;
860 register struct ifaddr *ifa = (struct ifaddr *)data;
861 int s, error = 0;
863 s = splnet();
865 switch (cmd) {
867 case SIOCINITIFADDR:
868 ifp->if_flags |= IFF_UP;
870 switch (ifa->ifa_addr->sa_family) {
871 #ifdef INET
872 case AF_INET:
873 ed_init(sc);
874 arp_ifinit(ifp, ifa);
875 break;
876 #endif
877 #ifdef NS
878 /* XXX - This code is probably wrong. */
879 case AF_NS:
881 register struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
883 if (ns_nullhost(*ina))
884 ina->x_host =
885 *(union ns_host *)LLADDR(ifp->if_sadl);
886 else
887 bcopy(ina->x_host.c_host,
888 LLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
889 /* Set new address. */
890 ed_init(sc);
891 break;
893 #endif
894 default:
895 ed_init(sc);
896 break;
898 break;
900 case SIOCSIFFLAGS:
901 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
902 break;
903 /* XXX if ed_init() would ed_stop(, 0), first, perhaps we
904 * can re-use the code in ether_ioctl().
906 if ((ifp->if_flags & IFF_UP) == 0 &&
907 (ifp->if_flags & IFF_RUNNING) != 0) {
909 * If interface is marked down and it is running, then
910 * stop it.
912 ed_stop(sc);
913 ifp->if_flags &= ~IFF_RUNNING;
914 } else if ((ifp->if_flags & IFF_UP) != 0 &&
915 (ifp->if_flags & IFF_RUNNING) == 0) {
917 * If interface is marked up and it is stopped, then
918 * start it.
920 ed_init(sc);
921 } else {
923 * Reset the interface to pick up changes in any other
924 * flags that affect hardware registers.
926 ed_stop(sc);
927 ed_init(sc);
929 break;
931 case SIOCADDMULTI:
932 case SIOCDELMULTI:
933 /* Update our multicast list. */
934 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
936 * Multicast list has changed; set the hardware filter
937 * accordingly.
939 if (ifp->if_flags & IFF_RUNNING) {
940 ed_stop(sc); /* XXX for ds_setmcaf? */
941 ed_init(sc);
943 error = 0;
945 break;
947 default:
948 error = ether_ioctl(ifp, cmd, data);
949 break;
952 splx(s);
953 return (error);
957 * Retrieve packet from shared memory and send to the next level up via
958 * ether_input(). If there is a BPF listener, give a copy to BPF, too.
960 void
961 ed_get_packet(struct ed_softc *sc, void *buf, u_short len)
963 struct mbuf *m;
964 struct ifnet *ifp;
966 ifp = &sc->sc_ethercom.ec_if;
968 /* round length to word boundry */
969 len = (len + 1) & ~1;
971 /* Allocate a header mbuf. */
972 MGETHDR(m, M_DONTWAIT, MT_DATA);
973 if (m == 0)
974 return;
975 m->m_pkthdr.rcvif = ifp;
976 m->m_pkthdr.len = len;
977 m->m_len = 0;
979 /* The following silliness is to make NFS happy. */
980 #define EROUND ((sizeof(struct ether_header) + 3) & ~3)
981 #define EOFF (EROUND - sizeof(struct ether_header))
984 * The following assumes there is room for the ether header in the
985 * header mbuf.
987 m->m_data += EOFF;
989 word_copy(buf, mtod(m, void *), sizeof(struct ether_header));
990 buf = (char*)buf + sizeof(struct ether_header);
991 m->m_len += sizeof(struct ether_header);
992 len -= sizeof(struct ether_header);
994 /* Pull packet off interface. */
995 if (ed_ring_to_mbuf(sc, buf, m, len) == 0) {
996 m_freem(m);
997 return;
1000 #if NBPFILTER > 0
1002 * Check if there's a BPF listener on this interface. If so, hand off
1003 * the raw packet to bpf.
1005 if (ifp->if_bpf)
1006 bpf_mtap(ifp->if_bpf, m);
1007 #endif
1009 (*ifp->if_input)(ifp, m);
1013 * Supporting routines.
1017 * Given a source and destination address, copy 'amount' of a packet from the
1018 * ring buffer into a linear destination buffer. Takes into account ring-wrap.
1020 static inline void *
1021 ed_ring_copy(struct ed_softc *sc, void *src, void *dst, u_short amount)
1023 u_short tmp_amount;
1025 /* Does copy wrap to lower addr in ring buffer? */
1026 if ((char*)src + amount > (char*)sc->mem_end) {
1027 tmp_amount = (char*)sc->mem_end - (char*)src;
1029 /* Copy amount up to end of NIC memory. */
1030 word_copy(src, dst, tmp_amount);
1032 amount -= tmp_amount;
1033 src = sc->mem_ring;
1034 dst = (char*)dst + tmp_amount;
1037 word_copy(src, dst, amount);
1039 return ((char*)src + amount);
1043 * Copy data from receive buffer to end of mbuf chain allocate additional mbufs
1044 * as needed. Return pointer to last mbuf in chain.
1045 * sc = ed info (softc)
1046 * src = pointer in ed ring buffer
1047 * dst = pointer to last mbuf in mbuf chain to copy to
1048 * amount = amount of data to copy
1050 struct mbuf *
1051 ed_ring_to_mbuf(struct ed_softc *sc, void *src, struct mbuf *dst,
1052 u_short total_len)
1054 register struct mbuf *m = dst;
1056 /* Round the length to a word boundary. */
1057 /* total_len = (total_len + 1) & ~1; */
1059 while (total_len) {
1060 register u_short amount = min(total_len, M_TRAILINGSPACE(m));
1062 if (amount == 0) {
1064 * No more data in this mbuf; alloc another.
1066 * If there is enough data for an mbuf cluster, attempt
1067 * to allocate one of those, otherwise, a regular mbuf
1068 * will do.
1069 * Note that a regular mbuf is always required, even if
1070 * we get a cluster - getting a cluster does not
1071 * allocate any mbufs, and one is needed to assign the
1072 * cluster to. The mbuf that has a cluster extension
1073 * can not be used to contain data - only the cluster
1074 * can contain data.
1076 dst = m;
1077 MGET(m, M_DONTWAIT, MT_DATA);
1078 if (m == 0)
1079 return (0);
1081 if (total_len >= MINCLSIZE)
1082 MCLGET(m, M_DONTWAIT);
1084 m->m_len = 0;
1085 dst->m_next = m;
1086 amount = min(total_len, M_TRAILINGSPACE(m));
1089 src = ed_ring_copy(sc, src, mtod(m, char *) + m->m_len,
1090 amount);
1092 m->m_len += amount;
1093 total_len -= amount;
1095 return (m);
1099 * Compute the multicast address filter from the list of multicast addresses we
1100 * need to listen to.
1102 void
1103 ed_getmcaf(struct ethercom *ac, u_long *af)
1105 struct ifnet *ifp = &ac->ec_if;
1106 struct ether_multi *enm;
1107 register u_char *cp, c;
1108 register u_long crc;
1109 register int i, len;
1110 struct ether_multistep step;
1113 * Set up multicast address filter by passing all multicast addresses
1114 * through a crc generator, and then using the high order 6 bits as an
1115 * index into the 64 bit logical address filter. The high order bit
1116 * selects the word, while the rest of the bits select the bit within
1117 * the word.
1120 if (ifp->if_flags & IFF_PROMISC) {
1121 ifp->if_flags |= IFF_ALLMULTI;
1122 af[0] = af[1] = 0xffffffff;
1123 return;
1126 af[0] = af[1] = 0;
1127 ETHER_FIRST_MULTI(step, ac, enm);
1128 while (enm != NULL) {
1129 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
1130 sizeof(enm->enm_addrlo)) != 0) {
1132 * We must listen to a range of multicast addresses.
1133 * For now, just accept all multicasts, rather than
1134 * trying to set only those filter bits needed to match
1135 * the range. (At this time, the only use of address
1136 * ranges is for IP multicast routing, for which the
1137 * range is big enough to require all bits set.)
1139 ifp->if_flags |= IFF_ALLMULTI;
1140 af[0] = af[1] = 0xffffffff;
1141 return;
1144 cp = enm->enm_addrlo;
1145 crc = 0xffffffff;
1146 for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
1147 c = *cp++;
1148 for (i = 8; --i >= 0;) {
1149 if (((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01)) {
1150 crc <<= 1;
1151 crc ^= 0x04c11db6 | 1;
1152 } else
1153 crc <<= 1;
1154 c >>= 1;
1157 /* Just want the 6 most significant bits. */
1158 crc >>= 26;
1160 /* Turn on the corresponding bit in the filter. */
1161 af[crc >> 5] |= 1 << ((crc & 0x1f) ^ 0);
1163 ETHER_NEXT_MULTI(step, enm);
1165 ifp->if_flags &= ~IFF_ALLMULTI;
1169 * Copy packet from mbuf to the board memory
1171 * Currently uses an extra buffer/extra memory copy,
1172 * unless the whole packet fits in one mbuf.
1175 u_short
1176 ed_put(struct ed_softc *sc, struct mbuf *m, void *buf)
1178 u_char *data, savebyte[2];
1179 int len, wantbyte;
1180 u_short totlen;
1182 totlen = wantbyte = 0;
1184 for (; m != 0; m = m->m_next) {
1185 data = mtod(m, u_char *);
1186 len = m->m_len;
1187 totlen += len;
1188 if (len > 0) {
1189 /* Finish the last word. */
1190 if (wantbyte) {
1191 savebyte[1] = *data;
1192 word_copy(savebyte, buf, 2);
1193 buf = (char*)buf + 2;
1194 data++;
1195 len--;
1196 wantbyte = 0;
1198 /* Output contiguous words. */
1199 if (len > 1) {
1200 word_copy(data, buf, len);
1201 buf = (char*)buf + (len & ~1);
1202 data += len & ~1;
1203 len &= 1;
1205 /* Save last byte, if necessary. */
1206 if (len == 1) {
1207 savebyte[0] = *data;
1208 wantbyte = 1;
1213 if (wantbyte) {
1214 savebyte[1] = 0;
1215 word_copy(savebyte, buf, 2);
1216 buf = (char*)buf + 2;
1217 totlen++;
1219 if (totlen < ETHER_MIN_LEN - ETHER_CRC_LEN) {
1220 word_zero(buf, ETHER_MIN_LEN - ETHER_CRC_LEN - totlen);
1221 totlen = ETHER_MIN_LEN - ETHER_CRC_LEN;
1224 return (totlen);