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