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