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