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