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