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