dp8390.c revision 1.21 1 /* $NetBSD: dp8390.c,v 1.21 1999/02/17 03:40:59 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 #define ETHER_MIN_LEN 64
82 #define ETHER_MAX_LEN 1518
83 #define ETHER_ADDR_LEN 6
84
85 int dp8390_debug = 0;
86
87 /*
88 * Do bus-independent setup.
89 */
90 int
91 dp8390_config(sc, media, nmedia, defmedia)
92 struct dp8390_softc *sc;
93 int *media, nmedia, defmedia;
94 {
95 struct ifnet *ifp = &sc->sc_ec.ec_if;
96 int i, rv;
97
98 rv = 1;
99
100 if (!sc->test_mem)
101 sc->test_mem = dp8390_test_mem;
102
103 /* Allocate one xmit buffer if < 16k, two buffers otherwise. */
104 if ((sc->mem_size < 16384) ||
105 (sc->sc_flags & DP8390_NO_MULTI_BUFFERING))
106 sc->txb_cnt = 1;
107 else
108 sc->txb_cnt = 2;
109
110 sc->tx_page_start = sc->mem_start >> ED_PAGE_SHIFT;
111 sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
112 sc->rec_page_stop = sc->tx_page_start + (sc->mem_size >> ED_PAGE_SHIFT);
113 sc->mem_ring = sc->mem_start + (sc->rec_page_start << ED_PAGE_SHIFT);
114 sc->mem_end = sc->mem_start + sc->mem_size;
115
116 /* Now zero memory and verify that it is clear. */
117 if ((*sc->test_mem)(sc))
118 goto out;
119
120 /* Set interface to stopped condition (reset). */
121 dp8390_stop(sc);
122
123 /* Initialize ifnet structure. */
124 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
125 ifp->if_softc = sc;
126 ifp->if_start = dp8390_start;
127 ifp->if_ioctl = dp8390_ioctl;
128 if (!ifp->if_watchdog)
129 ifp->if_watchdog = dp8390_watchdog;
130 ifp->if_flags =
131 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
132
133 /* Initialize media goo. */
134 ifmedia_init(&sc->sc_media, 0, dp8390_mediachange, dp8390_mediastatus);
135 if (media != NULL) {
136 for (i = 0; i < nmedia; i++)
137 ifmedia_add(&sc->sc_media, media[i], 0, NULL);
138 ifmedia_set(&sc->sc_media, defmedia);
139 } else {
140 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
141 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
142 }
143
144 /* Attach the interface. */
145 if_attach(ifp);
146 ether_ifattach(ifp, sc->sc_enaddr);
147 #if NBPFILTER > 0
148 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
149 #endif
150
151 #if NRND > 0
152 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname, RND_TYPE_NET);
153 #endif
154
155 /* Print additional info when attached. */
156 printf("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
157 ether_sprintf(sc->sc_enaddr));
158
159 rv = 0;
160 out:
161 return (rv);
162 }
163
164 /*
165 * Media change callback.
166 */
167 int
168 dp8390_mediachange(ifp)
169 struct ifnet *ifp;
170 {
171 struct dp8390_softc *sc = ifp->if_softc;
172
173 if (sc->sc_mediachange)
174 return ((*sc->sc_mediachange)(sc));
175 return (EINVAL);
176 }
177
178 /*
179 * Media status callback.
180 */
181 void
182 dp8390_mediastatus(ifp, ifmr)
183 struct ifnet *ifp;
184 struct ifmediareq *ifmr;
185 {
186 struct dp8390_softc *sc = ifp->if_softc;
187
188 if (sc->sc_enabled == 0) {
189 ifmr->ifm_active = IFM_ETHER | IFM_NONE;
190 ifmr->ifm_status = 0;
191 return;
192 }
193
194 if (sc->sc_mediastatus)
195 (*sc->sc_mediastatus)(sc, ifmr);
196 }
197
198 /*
199 * Reset interface.
200 */
201 void
202 dp8390_reset(sc)
203 struct dp8390_softc *sc;
204 {
205 int s;
206
207 s = splnet();
208 dp8390_stop(sc);
209 dp8390_init(sc);
210 splx(s);
211 }
212
213 /*
214 * Take interface offline.
215 */
216 void
217 dp8390_stop(sc)
218 struct dp8390_softc *sc;
219 {
220 bus_space_tag_t regt = sc->sc_regt;
221 bus_space_handle_t regh = sc->sc_regh;
222 int n = 5000;
223
224 /* Stop everything on the interface, and select page 0 registers. */
225 NIC_PUT(regt, regh, ED_P0_CR,
226 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
227
228 /*
229 * Wait for interface to enter stopped state, but limit # of checks to
230 * 'n' (about 5ms). It shouldn't even take 5us on modern DS8390's, but
231 * just in case it's an old one.
232 */
233 while (((NIC_GET(regt, regh,
234 ED_P0_ISR) & ED_ISR_RST) == 0) && --n)
235 ;
236 }
237
238 /*
239 * Device timeout/watchdog routine. Entered if the device neglects to generate
240 * an interrupt after a transmit has been started on it.
241 */
242
243 void
244 dp8390_watchdog(ifp)
245 struct ifnet *ifp;
246 {
247 struct dp8390_softc *sc = ifp->if_softc;
248
249 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
250 ++sc->sc_ec.ec_if.if_oerrors;
251
252 dp8390_reset(sc);
253 }
254
255 /*
256 * Initialize device.
257 */
258 void
259 dp8390_init(sc)
260 struct dp8390_softc *sc;
261 {
262 bus_space_tag_t regt = sc->sc_regt;
263 bus_space_handle_t regh = sc->sc_regh;
264 struct ifnet *ifp = &sc->sc_ec.ec_if;
265 u_int8_t mcaf[8];
266 int i;
267
268 /*
269 * Initialize the NIC in the exact order outlined in the NS manual.
270 * This init procedure is "mandatory"...don't change what or when
271 * things happen.
272 */
273
274 /* Reset transmitter flags. */
275 ifp->if_timer = 0;
276
277 sc->txb_inuse = 0;
278 sc->txb_new = 0;
279 sc->txb_next_tx = 0;
280
281 /* Set interface for page 0, remote DMA complete, stopped. */
282 NIC_PUT(regt, regh, ED_P0_CR,
283 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
284
285 if (sc->dcr_reg & ED_DCR_LS) {
286 NIC_PUT(regt, regh, ED_P0_DCR, sc->dcr_reg);
287 } else {
288 /*
289 * Set FIFO threshold to 8, No auto-init Remote DMA, byte
290 * order=80x86, byte-wide DMA xfers,
291 */
292 NIC_PUT(regt, regh, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
293 }
294
295 /* Clear remote byte count registers. */
296 NIC_PUT(regt, regh, ED_P0_RBCR0, 0);
297 NIC_PUT(regt, regh, ED_P0_RBCR1, 0);
298
299 /* Tell RCR to do nothing for now. */
300 NIC_PUT(regt, regh, ED_P0_RCR, ED_RCR_MON);
301
302 /* Place NIC in internal loopback mode. */
303 NIC_PUT(regt, regh, ED_P0_TCR, ED_TCR_LB0);
304
305 /* Set lower bits of byte addressable framing to 0. */
306 if (sc->is790)
307 NIC_PUT(regt, regh, 0x09, 0);
308
309 /* Initialize receive buffer ring. */
310 NIC_PUT(regt, regh, ED_P0_BNRY, sc->rec_page_start);
311 NIC_PUT(regt, regh, ED_P0_PSTART, sc->rec_page_start);
312 NIC_PUT(regt, regh, ED_P0_PSTOP, sc->rec_page_stop);
313
314 /*
315 * Enable the following interrupts: receive/transmit complete,
316 * receive/transmit error, and Receiver OverWrite.
317 *
318 * Counter overflow and Remote DMA complete are *not* enabled.
319 */
320 NIC_PUT(regt, regh, ED_P0_IMR,
321 ED_IMR_PRXE | ED_IMR_PTXE | ED_IMR_RXEE | ED_IMR_TXEE |
322 ED_IMR_OVWE);
323
324 /*
325 * Clear all interrupts. A '1' in each bit position clears the
326 * corresponding flag.
327 */
328 NIC_PUT(regt, regh, ED_P0_ISR, 0xff);
329
330 /* Program command register for page 1. */
331 NIC_PUT(regt, regh, ED_P0_CR,
332 sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
333
334 /* Copy out our station address. */
335 for (i = 0; i < ETHER_ADDR_LEN; ++i)
336 NIC_PUT(regt, regh, ED_P1_PAR0 + i,
337 LLADDR(ifp->if_sadl)[i]);
338
339 /* Set multicast filter on chip. */
340 dp8390_getmcaf(&sc->sc_ec, mcaf);
341 for (i = 0; i < 8; i++)
342 NIC_PUT(regt, regh, ED_P1_MAR0 + i, mcaf[i]);
343
344 /*
345 * Set current page pointer to one page after the boundary pointer, as
346 * recommended in the National manual.
347 */
348 sc->next_packet = sc->rec_page_start + 1;
349 NIC_PUT(regt, regh, ED_P1_CURR, sc->next_packet);
350
351 /* Program command register for page 0. */
352 NIC_PUT(regt, regh, ED_P1_CR,
353 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
354
355 /* Accept broadcast and multicast packets by default. */
356 i = ED_RCR_AB | ED_RCR_AM;
357 if (ifp->if_flags & IFF_PROMISC) {
358 /*
359 * Set promiscuous mode. Multicast filter was set earlier so
360 * that we should receive all multicast packets.
361 */
362 i |= ED_RCR_PRO | ED_RCR_AR | ED_RCR_SEP;
363 }
364 NIC_PUT(regt, regh, ED_P0_RCR, i);
365
366 /* Take interface out of loopback. */
367 NIC_PUT(regt, regh, ED_P0_TCR, 0);
368
369 /* Do any card-specific initialization, if applicable. */
370 if (sc->init_card)
371 (*sc->init_card)(sc);
372
373 /* Fire up the interface. */
374 NIC_PUT(regt, regh, ED_P0_CR,
375 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
376
377 /* Set 'running' flag, and clear output active flag. */
378 ifp->if_flags |= IFF_RUNNING;
379 ifp->if_flags &= ~IFF_OACTIVE;
380
381 /* ...and attempt to start output. */
382 dp8390_start(ifp);
383 }
384
385 /*
386 * This routine actually starts the transmission on the interface.
387 */
388 static __inline__ void
389 dp8390_xmit(sc)
390 struct dp8390_softc *sc;
391 {
392 bus_space_tag_t regt = sc->sc_regt;
393 bus_space_handle_t regh = sc->sc_regh;
394 struct ifnet *ifp = &sc->sc_ec.ec_if;
395 u_short len;
396
397 #ifdef DIAGNOSTIC
398 if ((sc->txb_next_tx + sc->txb_inuse) % sc->txb_cnt != sc->txb_new)
399 panic("dp8390_xmit: desync, next_tx=%d inuse=%d cnt=%d new=%d",
400 sc->txb_next_tx, sc->txb_inuse, sc->txb_cnt, sc->txb_new);
401
402 if (sc->txb_inuse == 0)
403 panic("dp8390_xmit: no packets to xmit\n");
404 #endif
405
406 len = sc->txb_len[sc->txb_next_tx];
407
408 /* Set NIC for page 0 register access. */
409 NIC_PUT(regt, regh, ED_P0_CR,
410 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
411
412 /* Set TX buffer start page. */
413 NIC_PUT(regt, regh, ED_P0_TPSR, sc->tx_page_start +
414 sc->txb_next_tx * ED_TXBUF_SIZE);
415
416 /* Set TX length. */
417 NIC_PUT(regt, regh, ED_P0_TBCR0, len);
418 NIC_PUT(regt, regh, ED_P0_TBCR1, len >> 8);
419
420 /* Set page 0, remote DMA complete, transmit packet, and *start*. */
421 NIC_PUT(regt, regh, ED_P0_CR,
422 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_TXP | ED_CR_STA);
423
424 /* Point to next transmit buffer slot and wrap if necessary. */
425 if (++sc->txb_next_tx == sc->txb_cnt)
426 sc->txb_next_tx = 0;
427
428 /* Set a timer just in case we never hear from the board again. */
429 ifp->if_timer = 2;
430 }
431
432 /*
433 * Start output on interface.
434 * We make two assumptions here:
435 * 1) that the current priority is set to splnet _before_ this code
436 * is called *and* is returned to the appropriate priority after
437 * return
438 * 2) that the IFF_OACTIVE flag is checked before this code is called
439 * (i.e. that the output part of the interface is idle)
440 */
441 void
442 dp8390_start(ifp)
443 struct ifnet *ifp;
444 {
445 struct dp8390_softc *sc = ifp->if_softc;
446 struct mbuf *m0;
447 int buffer;
448 int len;
449
450 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
451 return;
452
453 outloop:
454 /* See if there is room to put another packet in the buffer. */
455 if (sc->txb_inuse == sc->txb_cnt) {
456 /* No room. Indicate this to the outside world and exit. */
457 ifp->if_flags |= IFF_OACTIVE;
458 return;
459 }
460 IF_DEQUEUE(&ifp->if_snd, m0);
461 if (m0 == 0)
462 return;
463
464 /* We need to use m->m_pkthdr.len, so require the header */
465 if ((m0->m_flags & M_PKTHDR) == 0)
466 panic("dp8390_start: no header mbuf");
467
468 #if NBPFILTER > 0
469 /* Tap off here if there is a BPF listener. */
470 if (ifp->if_bpf)
471 bpf_mtap(ifp->if_bpf, m0);
472 #endif
473
474 /* txb_new points to next open buffer slot. */
475 buffer = sc->mem_start +
476 ((sc->txb_new * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
477
478 if (sc->write_mbuf)
479 len = (*sc->write_mbuf)(sc, m0, buffer);
480 else
481 len = dp8390_write_mbuf(sc, m0, buffer);
482
483 m_freem(m0);
484 sc->txb_len[sc->txb_new] = max(len, ETHER_MIN_LEN);
485
486 /* Point to next buffer slot and wrap if necessary. */
487 if (++sc->txb_new == sc->txb_cnt)
488 sc->txb_new = 0;
489
490 /* Start the first packet transmitting. */
491 if (sc->txb_inuse++ == 0)
492 dp8390_xmit(sc);
493
494 /* Loop back to the top to possibly buffer more packets. */
495 goto outloop;
496 }
497
498 /*
499 * Ethernet interface receiver interrupt.
500 */
501 void
502 dp8390_rint(sc)
503 struct dp8390_softc *sc;
504 {
505 bus_space_tag_t regt = sc->sc_regt;
506 bus_space_handle_t regh = sc->sc_regh;
507 struct dp8390_ring packet_hdr;
508 int packet_ptr;
509 u_short len;
510 u_char boundary, current;
511 u_char nlen;
512
513 loop:
514 /* Set NIC to page 1 registers to get 'current' pointer. */
515 NIC_PUT(regt, regh, ED_P0_CR,
516 sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
517
518 /*
519 * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
520 * it points to where new data has been buffered. The 'CURR' (current)
521 * register points to the logical end of the ring-buffer - i.e. it
522 * points to where additional new data will be added. We loop here
523 * until the logical beginning equals the logical end (or in other
524 * words, until the ring-buffer is empty).
525 */
526 current = NIC_GET(regt, regh, ED_P1_CURR);
527 if (sc->next_packet == current)
528 return;
529
530 /* Set NIC to page 0 registers to update boundary register. */
531 NIC_PUT(regt, regh, ED_P1_CR,
532 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
533
534 do {
535 /* Get pointer to this buffer's header structure. */
536 packet_ptr = sc->mem_ring +
537 ((sc->next_packet - sc->rec_page_start) << ED_PAGE_SHIFT);
538
539 if (sc->read_hdr)
540 (*sc->read_hdr)(sc, packet_ptr, &packet_hdr);
541 else
542 dp8390_read_hdr(sc, packet_ptr, &packet_hdr);
543 len = packet_hdr.count;
544
545 /*
546 * Try do deal with old, buggy chips that sometimes duplicate
547 * the low byte of the length into the high byte. We do this
548 * by simply ignoring the high byte of the length and always
549 * recalculating it.
550 *
551 * NOTE: sc->next_packet is pointing at the current packet.
552 */
553 if (packet_hdr.next_packet >= sc->next_packet)
554 nlen = (packet_hdr.next_packet - sc->next_packet);
555 else
556 nlen = ((packet_hdr.next_packet - sc->rec_page_start) +
557 (sc->rec_page_stop - sc->next_packet));
558 --nlen;
559 if ((len & ED_PAGE_MASK) + sizeof(packet_hdr) > ED_PAGE_SIZE)
560 --nlen;
561 len = (len & ED_PAGE_MASK) | (nlen << ED_PAGE_SHIFT);
562 #ifdef DIAGNOSTIC
563 if (len != packet_hdr.count) {
564 printf("%s: length does not match "
565 "next packet pointer\n", sc->sc_dev.dv_xname);
566 printf("%s: len %04x nlen %04x start %02x "
567 "first %02x curr %02x next %02x stop %02x\n",
568 sc->sc_dev.dv_xname, packet_hdr.count, len,
569 sc->rec_page_start, sc->next_packet, current,
570 packet_hdr.next_packet, sc->rec_page_stop);
571 }
572 #endif
573
574 /*
575 * Be fairly liberal about what we allow as a "reasonable"
576 * length so that a [crufty] packet will make it to BPF (and
577 * can thus be analyzed). Note that all that is really
578 * important is that we have a length that will fit into one
579 * mbuf cluster or less; the upper layer protocols can then
580 * figure out the length from their own length field(s).
581 */
582 if (len <= MCLBYTES &&
583 packet_hdr.next_packet >= sc->rec_page_start &&
584 packet_hdr.next_packet < sc->rec_page_stop) {
585 /* Go get packet. */
586 dp8390_read(sc,
587 packet_ptr + sizeof(struct dp8390_ring),
588 len - sizeof(struct dp8390_ring));
589 ++sc->sc_ec.ec_if.if_ipackets;
590 } else {
591 /* Really BAD. The ring pointers are corrupted. */
592 log(LOG_ERR, "%s: NIC memory corrupt - "
593 "invalid packet length %d\n",
594 sc->sc_dev.dv_xname, len);
595 ++sc->sc_ec.ec_if.if_ierrors;
596 dp8390_reset(sc);
597 return;
598 }
599
600 /* Update next packet pointer. */
601 sc->next_packet = packet_hdr.next_packet;
602
603 /*
604 * Update NIC boundary pointer - being careful to keep it one
605 * buffer behind (as recommended by NS databook).
606 */
607 boundary = sc->next_packet - 1;
608 if (boundary < sc->rec_page_start)
609 boundary = sc->rec_page_stop - 1;
610 NIC_PUT(regt, regh, ED_P0_BNRY, boundary);
611 } while (sc->next_packet != current);
612
613 goto loop;
614 }
615
616 /* Ethernet interface interrupt processor. */
617 int
618 dp8390_intr(arg)
619 void *arg;
620 {
621 struct dp8390_softc *sc = (struct dp8390_softc *)arg;
622 bus_space_tag_t regt = sc->sc_regt;
623 bus_space_handle_t regh = sc->sc_regh;
624 struct ifnet *ifp = &sc->sc_ec.ec_if;
625 u_char isr;
626 #if NRND > 0
627 u_char rndisr;
628 #endif
629
630 if (sc->sc_enabled == 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 /* Fix up data start offset in mbuf to point past ether header. */
990 m_adj(m, sizeof(struct ether_header));
991 ether_input(ifp, eh, m);
992 }
993
994
995 /*
996 * Supporting routines.
997 */
998
999 /*
1000 * Compute the multicast address filter from the list of multicast addresses we
1001 * need to listen to.
1002 */
1003 void
1004 dp8390_getmcaf(ec, af)
1005 struct ethercom *ec;
1006 u_int8_t *af;
1007 {
1008 struct ifnet *ifp = &ec->ec_if;
1009 struct ether_multi *enm;
1010 u_int8_t *cp, c;
1011 u_int32_t crc;
1012 int i, len;
1013 struct ether_multistep step;
1014
1015 /*
1016 * Set up multicast address filter by passing all multicast addresses
1017 * through a crc generator, and then using the high order 6 bits as an
1018 * index into the 64 bit logical address filter. The high order bit
1019 * selects the word, while the rest of the bits select the bit within
1020 * the word.
1021 */
1022
1023 if (ifp->if_flags & IFF_PROMISC) {
1024 ifp->if_flags |= IFF_ALLMULTI;
1025 for (i = 0; i < 8; i++)
1026 af[i] = 0xff;
1027 return;
1028 }
1029 for (i = 0; i < 8; i++)
1030 af[i] = 0;
1031 ETHER_FIRST_MULTI(step, ec, enm);
1032 while (enm != NULL) {
1033 if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
1034 sizeof(enm->enm_addrlo)) != 0) {
1035 /*
1036 * We must listen to a range of multicast addresses.
1037 * For now, just accept all multicasts, rather than
1038 * trying to set only those filter bits needed to match
1039 * the range. (At this time, the only use of address
1040 * ranges is for IP multicast routing, for which the
1041 * range is big enough to require all bits set.)
1042 */
1043 ifp->if_flags |= IFF_ALLMULTI;
1044 for (i = 0; i < 8; i++)
1045 af[i] = 0xff;
1046 return;
1047 }
1048 cp = enm->enm_addrlo;
1049 crc = 0xffffffff;
1050 for (len = sizeof(enm->enm_addrlo); --len >= 0;) {
1051 c = *cp++;
1052 for (i = 8; --i >= 0;) {
1053 if (((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01)) {
1054 crc <<= 1;
1055 crc ^= 0x04c11db6 | 1;
1056 } else
1057 crc <<= 1;
1058 c >>= 1;
1059 }
1060 }
1061 /* Just want the 6 most significant bits. */
1062 crc >>= 26;
1063
1064 /* Turn on the corresponding bit in the filter. */
1065 af[crc >> 3] |= 1 << (crc & 0x7);
1066
1067 ETHER_NEXT_MULTI(step, enm);
1068 }
1069 ifp->if_flags &= ~IFF_ALLMULTI;
1070 }
1071
1072 /*
1073 * Copy data from receive buffer to a new mbuf chain allocating mbufs
1074 * as needed. Return pointer to first mbuf in chain.
1075 * sc = dp8390 info (softc)
1076 * src = pointer in dp8390 ring buffer
1077 * total_len = amount of data to copy
1078 */
1079 struct mbuf *
1080 dp8390_get(sc, src, total_len)
1081 struct dp8390_softc *sc;
1082 int src;
1083 u_short total_len;
1084 {
1085 struct ifnet *ifp = &sc->sc_ec.ec_if;
1086 struct mbuf *m, *m0, *newm;
1087 u_short len;
1088
1089 MGETHDR(m0, M_DONTWAIT, MT_DATA);
1090 if (m0 == 0)
1091 return (0);
1092 m0->m_pkthdr.rcvif = ifp;
1093 m0->m_pkthdr.len = total_len;
1094 len = MHLEN;
1095 m = m0;
1096
1097 while (total_len > 0) {
1098 if (total_len >= MINCLSIZE) {
1099 MCLGET(m, M_DONTWAIT);
1100 if ((m->m_flags & M_EXT) == 0)
1101 goto bad;
1102 len = MCLBYTES;
1103 }
1104
1105 /*
1106 * Make sure the data after the Ethernet header is aligned.
1107 */
1108 if (m == m0) {
1109 caddr_t newdata = (caddr_t)
1110 ALIGN(m->m_data + sizeof(struct ether_header)) -
1111 sizeof(struct ether_header);
1112 len -= newdata - m->m_data;
1113 m->m_data = newdata;
1114 }
1115
1116 m->m_len = len = min(total_len, len);
1117 if (sc->ring_copy)
1118 src = (*sc->ring_copy)(sc, src, mtod(m, caddr_t), len);
1119 else
1120 src = dp8390_ring_copy(sc, src, mtod(m, caddr_t), len);
1121
1122 total_len -= len;
1123 if (total_len > 0) {
1124 MGET(newm, M_DONTWAIT, MT_DATA);
1125 if (newm == 0)
1126 goto bad;
1127 len = MLEN;
1128 m = m->m_next = newm;
1129 }
1130 }
1131
1132 return (m0);
1133
1134 bad:
1135 m_freem(m0);
1136 return (0);
1137 }
1138
1139
1140 /*
1141 * Default driver support functions.
1142 *
1143 * NOTE: all support functions assume 8-bit shared memory.
1144 */
1145 /*
1146 * Zero NIC buffer memory and verify that it is clear.
1147 */
1148 static int
1149 dp8390_test_mem(sc)
1150 struct dp8390_softc *sc;
1151 {
1152 bus_space_tag_t buft = sc->sc_buft;
1153 bus_space_handle_t bufh = sc->sc_bufh;
1154 int i;
1155
1156 bus_space_set_region_1(buft, bufh, sc->mem_start, 0, sc->mem_size);
1157
1158 for (i = 0; i < sc->mem_size; ++i) {
1159 if (bus_space_read_1(buft, bufh, sc->mem_start + i)) {
1160 printf(": failed to clear NIC buffer at offset %x - "
1161 "check configuration\n", (sc->mem_start + i));
1162 return 1;
1163 }
1164 }
1165
1166 return 0;
1167 }
1168
1169 /*
1170 * Read a packet header from the ring, given the source offset.
1171 */
1172 static __inline__ void
1173 dp8390_read_hdr(sc, src, hdrp)
1174 struct dp8390_softc *sc;
1175 int src;
1176 struct dp8390_ring *hdrp;
1177 {
1178 bus_space_tag_t buft = sc->sc_buft;
1179 bus_space_handle_t bufh = sc->sc_bufh;
1180
1181 /*
1182 * The byte count includes a 4 byte header that was added by
1183 * the NIC.
1184 */
1185 hdrp->rsr = bus_space_read_1(buft, bufh, src);
1186 hdrp->next_packet = bus_space_read_1(buft, bufh, src + 1);
1187 hdrp->count = bus_space_read_1(buft, bufh, src + 2) |
1188 (bus_space_read_1(buft, bufh, src + 3) << 8);
1189 }
1190
1191 /*
1192 * Copy `amount' bytes from a packet in the ring buffer to a linear
1193 * destination buffer, given a source offset and destination address.
1194 * Takes into account ring-wrap.
1195 */
1196 static __inline__ int
1197 dp8390_ring_copy(sc, src, dst, amount)
1198 struct dp8390_softc *sc;
1199 int src;
1200 caddr_t dst;
1201 u_short amount;
1202 {
1203 bus_space_tag_t buft = sc->sc_buft;
1204 bus_space_handle_t bufh = sc->sc_bufh;
1205 u_short tmp_amount;
1206
1207 /* Does copy wrap to lower addr in ring buffer? */
1208 if (src + amount > sc->mem_end) {
1209 tmp_amount = sc->mem_end - src;
1210
1211 /* Copy amount up to end of NIC memory. */
1212 bus_space_read_region_1(buft, bufh, src, dst, tmp_amount);
1213
1214 amount -= tmp_amount;
1215 src = sc->mem_ring;
1216 dst += tmp_amount;
1217 }
1218 bus_space_read_region_1(buft, bufh, src, dst, amount);
1219
1220 return (src + amount);
1221 }
1222
1223 /*
1224 * Copy a packet from an mbuf to the transmit buffer on the card.
1225 *
1226 * Currently uses an extra buffer/extra memory copy, unless the whole
1227 * packet fits in one mbuf.
1228 */
1229 static __inline__ int
1230 dp8390_write_mbuf(sc, m, buf)
1231 struct dp8390_softc *sc;
1232 struct mbuf *m;
1233 int buf;
1234 {
1235 bus_space_tag_t buft = sc->sc_buft;
1236 bus_space_handle_t bufh = sc->sc_bufh;
1237 u_char *data;
1238 int len, totlen = 0;
1239
1240 for (; m ; m = m->m_next) {
1241 data = mtod(m, u_char *);
1242 len = m->m_len;
1243 if (len > 0) {
1244 bus_space_write_region_1(buft, bufh, buf, data, len);
1245 totlen += len;
1246 buf += len;
1247 }
1248 }
1249
1250 return (totlen);
1251 }
1252
1253 /*
1254 * Enable power on the interface.
1255 */
1256 int
1257 dp8390_enable(sc)
1258 struct dp8390_softc *sc;
1259 {
1260
1261 if (sc->sc_enabled == 0 && sc->sc_enable != NULL) {
1262 if ((*sc->sc_enable)(sc) != 0) {
1263 printf("%s: device enable failed\n",
1264 sc->sc_dev.dv_xname);
1265 return (EIO);
1266 }
1267 }
1268
1269 sc->sc_enabled = 1;
1270 return (0);
1271 }
1272
1273 /*
1274 * Disable power on the interface.
1275 */
1276 void
1277 dp8390_disable(sc)
1278 struct dp8390_softc *sc;
1279 {
1280
1281 if (sc->sc_enabled != 0 && sc->sc_disable != NULL) {
1282 (*sc->sc_disable)(sc);
1283 sc->sc_enabled = 0;
1284 }
1285 }
1286
1287 int
1288 dp8390_activate(self, act)
1289 struct device *self;
1290 enum devact act;
1291 {
1292 struct dp8390_softc *sc = (struct dp8390_softc *)self;
1293 int rv = 0, s;
1294
1295 s = splnet();
1296 switch (act) {
1297 case DVACT_ACTIVATE:
1298 rv = EOPNOTSUPP;
1299 break;
1300
1301 case DVACT_DEACTIVATE:
1302 #ifdef notyet
1303 /* First, kill off the interface. */
1304 if_detach(sc->sc_ec.ec_if);
1305 #endif
1306
1307 /* Now disable the interface. */
1308 dp8390_disable(sc);
1309 break;
1310 }
1311 splx(s);
1312 return (rv);
1313 }
1314