dp8390.c revision 1.30 1 /* $NetBSD: dp8390.c,v 1.30 2000/02/02 11:41:56 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 (sc->sc_dev.dv_flags & DVF_ACTIVE) == 0)
631 return (0);
632
633 /* Set NIC to page 0 registers. */
634 NIC_PUT(regt, regh, ED_P0_CR,
635 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
636
637 isr = NIC_GET(regt, regh, ED_P0_ISR);
638 if (!isr)
639 return (0);
640
641 #if NRND > 0
642 rndisr = isr;
643 #endif
644
645 /* Loop until there are no more new interrupts. */
646 for (;;) {
647 /*
648 * Reset all the bits that we are 'acknowledging' by writing a
649 * '1' to each bit position that was set.
650 * (Writing a '1' *clears* the bit.)
651 */
652 NIC_PUT(regt, regh, ED_P0_ISR, isr);
653
654 /*
655 * Handle transmitter interrupts. Handle these first because
656 * the receiver will reset the board under some conditions.
657 *
658 * If the chip was reset while a packet was transmitting, it
659 * may still deliver a TX interrupt. In this case, just ignore
660 * the interrupt.
661 */
662 if (isr & (ED_ISR_PTX | ED_ISR_TXE) &&
663 sc->txb_inuse != 0) {
664 u_char collisions =
665 NIC_GET(regt, regh, ED_P0_NCR) & 0x0f;
666
667 /*
668 * Check for transmit error. If a TX completed with an
669 * error, we end up throwing the packet away. Really
670 * the only error that is possible is excessive
671 * collisions, and in this case it is best to allow the
672 * automatic mechanisms of TCP to backoff the flow. Of
673 * course, with UDP we're screwed, but this is expected
674 * when a network is heavily loaded.
675 */
676 if (isr & ED_ISR_TXE) {
677 /*
678 * Excessive collisions (16).
679 */
680 if ((NIC_GET(regt, regh, ED_P0_TSR)
681 & ED_TSR_ABT) && (collisions == 0)) {
682 /*
683 * When collisions total 16, the P0_NCR
684 * will indicate 0, and the TSR_ABT is
685 * set.
686 */
687 collisions = 16;
688 }
689
690 /* Update output errors counter. */
691 ++ifp->if_oerrors;
692 } else {
693 /*
694 * Throw away the non-error status bits.
695 *
696 * XXX
697 * It may be useful to detect loss of carrier
698 * and late collisions here.
699 */
700 (void)NIC_GET(regt, regh, ED_P0_TSR);
701
702 /*
703 * Update total number of successfully
704 * transmitted packets.
705 */
706 ++ifp->if_opackets;
707 }
708
709 /* Clear watchdog timer. */
710 ifp->if_timer = 0;
711 ifp->if_flags &= ~IFF_OACTIVE;
712
713 /*
714 * Add in total number of collisions on last
715 * transmission.
716 */
717 ifp->if_collisions += collisions;
718
719 /*
720 * Decrement buffer in-use count if not zero (can only
721 * be zero if a transmitter interrupt occured while not
722 * actually transmitting).
723 * If data is ready to transmit, start it transmitting,
724 * otherwise defer until after handling receiver.
725 */
726 if (--sc->txb_inuse != 0)
727 dp8390_xmit(sc);
728 }
729
730 /* Handle receiver interrupts. */
731 if (isr & (ED_ISR_PRX | ED_ISR_RXE | ED_ISR_OVW)) {
732 /*
733 * Overwrite warning. In order to make sure that a
734 * lockup of the local DMA hasn't occurred, we reset
735 * and re-init the NIC. The NSC manual suggests only a
736 * partial reset/re-init is necessary - but some chips
737 * seem to want more. The DMA lockup has been seen
738 * only with early rev chips - Methinks this bug was
739 * fixed in later revs. -DG
740 */
741 if (isr & ED_ISR_OVW) {
742 ++ifp->if_ierrors;
743 #ifdef DIAGNOSTIC
744 log(LOG_WARNING, "%s: warning - receiver "
745 "ring buffer overrun\n",
746 sc->sc_dev.dv_xname);
747 #endif
748 /* Stop/reset/re-init NIC. */
749 dp8390_reset(sc);
750 } else {
751 /*
752 * Receiver Error. One or more of: CRC error,
753 * frame alignment error FIFO overrun, or
754 * missed packet.
755 */
756 if (isr & ED_ISR_RXE) {
757 ++ifp->if_ierrors;
758 #ifdef DEBUG
759 if (dp8390_debug) {
760 printf("%s: receive error %x\n",
761 sc->sc_dev.dv_xname,
762 NIC_GET(regt, regh,
763 ED_P0_RSR));
764 }
765 #endif
766 }
767
768 /*
769 * Go get the packet(s)
770 * XXX - Doing this on an error is dubious
771 * because there shouldn't be any data to get
772 * (we've configured the interface to not
773 * accept packets with errors).
774 */
775 if (sc->recv_int)
776 (*sc->recv_int)(sc);
777 else
778 dp8390_rint(sc);
779 }
780 }
781
782 /*
783 * If it looks like the transmitter can take more data, attempt
784 * to start output on the interface. This is done after
785 * handling the receiver to give the receiver priority.
786 */
787 dp8390_start(ifp);
788
789 /*
790 * Return NIC CR to standard state: page 0, remote DMA
791 * complete, start (toggling the TXP bit off, even if was just
792 * set in the transmit routine, is *okay* - it is 'edge'
793 * triggered from low to high).
794 */
795 NIC_PUT(regt, regh, ED_P0_CR,
796 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
797
798 /*
799 * If the Network Talley Counters overflow, read them to reset
800 * them. It appears that old 8390's won't clear the ISR flag
801 * otherwise - resulting in an infinite loop.
802 */
803 if (isr & ED_ISR_CNT) {
804 (void)NIC_GET(regt, regh, ED_P0_CNTR0);
805 (void)NIC_GET(regt, regh, ED_P0_CNTR1);
806 (void)NIC_GET(regt, regh, ED_P0_CNTR2);
807 }
808
809 isr = NIC_GET(regt, regh, ED_P0_ISR);
810 if (!isr)
811 goto out;
812 }
813
814 out:
815 #if NRND > 0
816 rnd_add_uint32(&sc->rnd_source, rndisr);
817 #endif
818 return (1);
819 }
820
821 /*
822 * Process an ioctl request. This code needs some work - it looks pretty ugly.
823 */
824 int
825 dp8390_ioctl(ifp, cmd, data)
826 struct ifnet *ifp;
827 u_long cmd;
828 caddr_t data;
829 {
830 struct dp8390_softc *sc = ifp->if_softc;
831 struct ifaddr *ifa = (struct ifaddr *) data;
832 struct ifreq *ifr = (struct ifreq *) data;
833 int s, error = 0;
834
835 s = splnet();
836
837 switch (cmd) {
838
839 case SIOCSIFADDR:
840 if ((error = dp8390_enable(sc)) != 0)
841 break;
842 ifp->if_flags |= IFF_UP;
843
844 switch (ifa->ifa_addr->sa_family) {
845 #ifdef INET
846 case AF_INET:
847 dp8390_init(sc);
848 arp_ifinit(ifp, ifa);
849 break;
850 #endif
851 #ifdef NS
852 /* XXX - This code is probably wrong. */
853 case AF_NS:
854 {
855 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
856
857 if (ns_nullhost(*ina))
858 ina->x_host =
859 *(union ns_host *)LLADDR(ifp->if_sadl);
860 else
861 bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
862 ETHER_ADDR_LEN);
863 /* Set new address. */
864 dp8390_init(sc);
865 break;
866 }
867 #endif
868 default:
869 dp8390_init(sc);
870 break;
871 }
872 break;
873
874 case SIOCSIFFLAGS:
875 if ((ifp->if_flags & IFF_UP) == 0 &&
876 (ifp->if_flags & IFF_RUNNING) != 0) {
877 /*
878 * If interface is marked down and it is running, then
879 * stop it.
880 */
881 dp8390_stop(sc);
882 ifp->if_flags &= ~IFF_RUNNING;
883 dp8390_disable(sc);
884 } else if ((ifp->if_flags & IFF_UP) != 0 &&
885 (ifp->if_flags & IFF_RUNNING) == 0) {
886 /*
887 * If interface is marked up and it is stopped, then
888 * start it.
889 */
890 if ((error = dp8390_enable(sc)) != 0)
891 break;
892 dp8390_init(sc);
893 } else if ((ifp->if_flags & IFF_UP) != 0) {
894 /*
895 * Reset the interface to pick up changes in any other
896 * flags that affect hardware registers.
897 */
898 dp8390_stop(sc);
899 dp8390_init(sc);
900 }
901 break;
902
903 case SIOCADDMULTI:
904 case SIOCDELMULTI:
905 if (sc->sc_enabled == 0) {
906 error = EIO;
907 break;
908 }
909
910 /* Update our multicast list. */
911 error = (cmd == SIOCADDMULTI) ?
912 ether_addmulti(ifr, &sc->sc_ec) :
913 ether_delmulti(ifr, &sc->sc_ec);
914
915 if (error == ENETRESET) {
916 /*
917 * Multicast list has changed; set the hardware filter
918 * accordingly.
919 */
920 dp8390_stop(sc); /* XXX for ds_setmcaf? */
921 dp8390_init(sc);
922 error = 0;
923 }
924 break;
925
926 case SIOCGIFMEDIA:
927 case SIOCSIFMEDIA:
928 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
929 break;
930
931 default:
932 error = EINVAL;
933 break;
934 }
935
936 splx(s);
937 return (error);
938 }
939
940 /*
941 * Retrieve packet from buffer memory and send to the next level up via
942 * ether_input(). If there is a BPF listener, give a copy to BPF, too.
943 */
944 void
945 dp8390_read(sc, buf, len)
946 struct dp8390_softc *sc;
947 int buf;
948 u_short len;
949 {
950 struct ifnet *ifp = &sc->sc_ec.ec_if;
951 struct mbuf *m;
952 struct ether_header *eh;
953
954 /* Pull packet off interface. */
955 m = dp8390_get(sc, buf, len);
956 if (m == 0) {
957 ifp->if_ierrors++;
958 return;
959 }
960
961 ifp->if_ipackets++;
962
963 /* We assume that the header fits entirely in one mbuf. */
964 eh = mtod(m, struct ether_header *);
965
966 #if NBPFILTER > 0
967 /*
968 * Check if there's a BPF listener on this interface.
969 * If so, hand off the raw packet to bpf.
970 */
971 if (ifp->if_bpf) {
972 bpf_mtap(ifp->if_bpf, m);
973
974 /*
975 * Note that the interface cannot be in promiscuous mode if
976 * there are no BPF listeners. And if we are in promiscuous
977 * mode, we have to check if this packet is really ours.
978 */
979 if ((ifp->if_flags & IFF_PROMISC) &&
980 (eh->ether_dhost[0] & 1) == 0 && /* !mcast and !bcast */
981 bcmp(eh->ether_dhost, LLADDR(ifp->if_sadl),
982 sizeof(eh->ether_dhost)) != 0) {
983 m_freem(m);
984 return;
985 }
986 }
987 #endif
988
989 (*ifp->if_input)(ifp, m);
990 }
991
992
993 /*
994 * Supporting routines.
995 */
996
997 /*
998 * Compute the multicast address filter from the list of multicast addresses we
999 * need to listen to.
1000 */
1001 void
1002 dp8390_getmcaf(ec, af)
1003 struct ethercom *ec;
1004 u_int8_t *af;
1005 {
1006 struct ifnet *ifp = &ec->ec_if;
1007 struct ether_multi *enm;
1008 u_int8_t *cp, c;
1009 u_int32_t crc;
1010 int i, len;
1011 struct ether_multistep step;
1012
1013 /*
1014 * Set up multicast address filter by passing all multicast addresses
1015 * through a crc generator, and then using the high order 6 bits as an
1016 * index into the 64 bit logical address filter. The high order bit
1017 * selects the word, while the rest of the bits select the bit within
1018 * the word.
1019 */
1020
1021 if (ifp->if_flags & IFF_PROMISC) {
1022 ifp->if_flags |= IFF_ALLMULTI;
1023 for (i = 0; i < 8; i++)
1024 af[i] = 0xff;
1025 return;
1026 }
1027 for (i = 0; i < 8; i++)
1028 af[i] = 0;
1029 ETHER_FIRST_MULTI(step, ec, enm);
1030 while (enm != NULL) {
1031 if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
1032 sizeof(enm->enm_addrlo)) != 0) {
1033 /*
1034 * We must listen to a range of multicast addresses.
1035 * For now, just accept all multicasts, rather than
1036 * trying to set only those filter bits needed to match
1037 * the range. (At this time, the only use of address
1038 * ranges is for IP multicast routing, for which the
1039 * range is big enough to require all bits set.)
1040 */
1041 ifp->if_flags |= IFF_ALLMULTI;
1042 for (i = 0; i < 8; i++)
1043 af[i] = 0xff;
1044 return;
1045 }
1046 cp = enm->enm_addrlo;
1047 crc = 0xffffffff;
1048 for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
1049 c = *cp++;
1050 for (i = 8; --i >= 0;) {
1051 if (((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01)) {
1052 crc <<= 1;
1053 crc ^= 0x04c11db6 | 1;
1054 } else
1055 crc <<= 1;
1056 c >>= 1;
1057 }
1058 }
1059 /* Just want the 6 most significant bits. */
1060 crc >>= 26;
1061
1062 /* Turn on the corresponding bit in the filter. */
1063 af[crc >> 3] |= 1 << (crc & 0x7);
1064
1065 ETHER_NEXT_MULTI(step, enm);
1066 }
1067 ifp->if_flags &= ~IFF_ALLMULTI;
1068 }
1069
1070 /*
1071 * Copy data from receive buffer to a new mbuf chain allocating mbufs
1072 * as needed. Return pointer to first mbuf in chain.
1073 * sc = dp8390 info (softc)
1074 * src = pointer in dp8390 ring buffer
1075 * total_len = amount of data to copy
1076 */
1077 struct mbuf *
1078 dp8390_get(sc, src, total_len)
1079 struct dp8390_softc *sc;
1080 int src;
1081 u_short total_len;
1082 {
1083 struct ifnet *ifp = &sc->sc_ec.ec_if;
1084 struct mbuf *m, *m0, *newm;
1085 u_short len;
1086
1087 MGETHDR(m0, M_DONTWAIT, MT_DATA);
1088 if (m0 == 0)
1089 return (0);
1090 m0->m_pkthdr.rcvif = ifp;
1091 m0->m_pkthdr.len = total_len;
1092 len = MHLEN;
1093 m = m0;
1094
1095 while (total_len > 0) {
1096 if (total_len >= MINCLSIZE) {
1097 MCLGET(m, M_DONTWAIT);
1098 if ((m->m_flags & M_EXT) == 0)
1099 goto bad;
1100 len = MCLBYTES;
1101 }
1102
1103 /*
1104 * Make sure the data after the Ethernet header is aligned.
1105 */
1106 if (m == m0) {
1107 caddr_t newdata = (caddr_t)
1108 ALIGN(m->m_data + sizeof(struct ether_header)) -
1109 sizeof(struct ether_header);
1110 len -= newdata - m->m_data;
1111 m->m_data = newdata;
1112 }
1113
1114 m->m_len = len = min(total_len, len);
1115 if (sc->ring_copy)
1116 src = (*sc->ring_copy)(sc, src, mtod(m, caddr_t), len);
1117 else
1118 src = dp8390_ring_copy(sc, src, mtod(m, caddr_t), len);
1119
1120 total_len -= len;
1121 if (total_len > 0) {
1122 MGET(newm, M_DONTWAIT, MT_DATA);
1123 if (newm == 0)
1124 goto bad;
1125 len = MLEN;
1126 m = m->m_next = newm;
1127 }
1128 }
1129
1130 return (m0);
1131
1132 bad:
1133 m_freem(m0);
1134 return (0);
1135 }
1136
1137
1138 /*
1139 * Default driver support functions.
1140 *
1141 * NOTE: all support functions assume 8-bit shared memory.
1142 */
1143 /*
1144 * Zero NIC buffer memory and verify that it is clear.
1145 */
1146 static int
1147 dp8390_test_mem(sc)
1148 struct dp8390_softc *sc;
1149 {
1150 bus_space_tag_t buft = sc->sc_buft;
1151 bus_space_handle_t bufh = sc->sc_bufh;
1152 int i;
1153
1154 bus_space_set_region_1(buft, bufh, sc->mem_start, 0, sc->mem_size);
1155
1156 for (i = 0; i < sc->mem_size; ++i) {
1157 if (bus_space_read_1(buft, bufh, sc->mem_start + i)) {
1158 printf(": failed to clear NIC buffer at offset %x - "
1159 "check configuration\n", (sc->mem_start + i));
1160 return 1;
1161 }
1162 }
1163
1164 return 0;
1165 }
1166
1167 /*
1168 * Read a packet header from the ring, given the source offset.
1169 */
1170 static __inline__ void
1171 dp8390_read_hdr(sc, src, hdrp)
1172 struct dp8390_softc *sc;
1173 int src;
1174 struct dp8390_ring *hdrp;
1175 {
1176 bus_space_tag_t buft = sc->sc_buft;
1177 bus_space_handle_t bufh = sc->sc_bufh;
1178
1179 /*
1180 * The byte count includes a 4 byte header that was added by
1181 * the NIC.
1182 */
1183 hdrp->rsr = bus_space_read_1(buft, bufh, src);
1184 hdrp->next_packet = bus_space_read_1(buft, bufh, src + 1);
1185 hdrp->count = bus_space_read_1(buft, bufh, src + 2) |
1186 (bus_space_read_1(buft, bufh, src + 3) << 8);
1187 }
1188
1189 /*
1190 * Copy `amount' bytes from a packet in the ring buffer to a linear
1191 * destination buffer, given a source offset and destination address.
1192 * Takes into account ring-wrap.
1193 */
1194 static __inline__ int
1195 dp8390_ring_copy(sc, src, dst, amount)
1196 struct dp8390_softc *sc;
1197 int src;
1198 caddr_t dst;
1199 u_short amount;
1200 {
1201 bus_space_tag_t buft = sc->sc_buft;
1202 bus_space_handle_t bufh = sc->sc_bufh;
1203 u_short tmp_amount;
1204
1205 /* Does copy wrap to lower addr in ring buffer? */
1206 if (src + amount > sc->mem_end) {
1207 tmp_amount = sc->mem_end - src;
1208
1209 /* Copy amount up to end of NIC memory. */
1210 bus_space_read_region_1(buft, bufh, src, dst, tmp_amount);
1211
1212 amount -= tmp_amount;
1213 src = sc->mem_ring;
1214 dst += tmp_amount;
1215 }
1216 bus_space_read_region_1(buft, bufh, src, dst, amount);
1217
1218 return (src + amount);
1219 }
1220
1221 /*
1222 * Copy a packet from an mbuf to the transmit buffer on the card.
1223 *
1224 * Currently uses an extra buffer/extra memory copy, unless the whole
1225 * packet fits in one mbuf.
1226 */
1227 static __inline__ int
1228 dp8390_write_mbuf(sc, m, buf)
1229 struct dp8390_softc *sc;
1230 struct mbuf *m;
1231 int buf;
1232 {
1233 bus_space_tag_t buft = sc->sc_buft;
1234 bus_space_handle_t bufh = sc->sc_bufh;
1235 u_char *data;
1236 int len, totlen = 0;
1237
1238 for (; m ; m = m->m_next) {
1239 data = mtod(m, u_char *);
1240 len = m->m_len;
1241 if (len > 0) {
1242 bus_space_write_region_1(buft, bufh, buf, data, len);
1243 totlen += len;
1244 buf += len;
1245 }
1246 }
1247
1248 return (totlen);
1249 }
1250
1251 /*
1252 * Enable power on the interface.
1253 */
1254 int
1255 dp8390_enable(sc)
1256 struct dp8390_softc *sc;
1257 {
1258
1259 if (sc->sc_enabled == 0 && sc->sc_enable != NULL) {
1260 if ((*sc->sc_enable)(sc) != 0) {
1261 printf("%s: device enable failed\n",
1262 sc->sc_dev.dv_xname);
1263 return (EIO);
1264 }
1265 }
1266
1267 sc->sc_enabled = 1;
1268 return (0);
1269 }
1270
1271 /*
1272 * Disable power on the interface.
1273 */
1274 void
1275 dp8390_disable(sc)
1276 struct dp8390_softc *sc;
1277 {
1278
1279 if (sc->sc_enabled != 0 && sc->sc_disable != NULL) {
1280 (*sc->sc_disable)(sc);
1281 sc->sc_enabled = 0;
1282 }
1283 }
1284
1285 int
1286 dp8390_activate(self, act)
1287 struct device *self;
1288 enum devact act;
1289 {
1290 struct dp8390_softc *sc = (struct dp8390_softc *)self;
1291 int rv = 0, s;
1292
1293 s = splnet();
1294 switch (act) {
1295 case DVACT_ACTIVATE:
1296 rv = EOPNOTSUPP;
1297 break;
1298
1299 case DVACT_DEACTIVATE:
1300 if_deactivate(&sc->sc_ec.ec_if);
1301 #if 0
1302 dp8390_disable(sc);
1303 #endif
1304 break;
1305 }
1306 splx(s);
1307 return (rv);
1308 }
1309
1310 int
1311 dp8390_detach(sc, flags)
1312 struct dp8390_softc *sc;
1313 int flags;
1314 {
1315 struct ifnet *ifp = &sc->sc_ec.ec_if;
1316
1317 dp8390_disable(sc);
1318
1319 /* Delete all media. */
1320 ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY);
1321
1322 #if NRND > 0
1323 rnd_detach_source(&sc->rnd_source);
1324 #endif
1325 #if NBPFILTER > 0
1326 bpfdetach(ifp);
1327 #endif
1328 ether_ifdetach(ifp);
1329 if_detach(ifp);
1330
1331 return (0);
1332 }
1333