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