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