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