dp8390.c revision 1.79 1 /* $NetBSD: dp8390.c,v 1.79 2010/04/11 09:58:36 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.79 2010/04/11 09:58:36 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 bpf_mtap(ifp, m0);
482
483 /* txb_new points to next open buffer slot. */
484 buffer = sc->mem_start +
485 ((sc->txb_new * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
486
487 len = (*sc->write_mbuf)(sc, m0, buffer);
488
489 m_freem(m0);
490 sc->txb_len[sc->txb_new] = len;
491
492 /* Point to next buffer slot and wrap if necessary. */
493 if (++sc->txb_new == sc->txb_cnt)
494 sc->txb_new = 0;
495
496 /* Start the first packet transmitting. */
497 if (sc->txb_inuse++ == 0)
498 dp8390_xmit(sc);
499
500 /* Loop back to the top to possibly buffer more packets. */
501 goto outloop;
502 }
503
504 /*
505 * Ethernet interface receiver interrupt.
506 */
507 void
508 dp8390_rint(struct dp8390_softc *sc)
509 {
510 bus_space_tag_t regt = sc->sc_regt;
511 bus_space_handle_t regh = sc->sc_regh;
512 struct dp8390_ring packet_hdr;
513 int packet_ptr;
514 uint16_t len;
515 uint8_t boundary, current;
516 uint8_t nlen;
517
518 loop:
519 /* Set NIC to page 1 registers to get 'current' pointer. */
520 NIC_BARRIER(regt, regh);
521 NIC_PUT(regt, regh, ED_P0_CR,
522 sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
523 NIC_BARRIER(regt, regh);
524
525 /*
526 * 'sc->next_packet' is the logical beginning of the ring-buffer - i.e.
527 * it points to where new data has been buffered. The 'CURR' (current)
528 * register points to the logical end of the ring-buffer - i.e. it
529 * points to where additional new data will be added. We loop here
530 * until the logical beginning equals the logical end (or in other
531 * words, until the ring-buffer is empty).
532 */
533 current = NIC_GET(regt, regh, ED_P1_CURR);
534 if (sc->next_packet == current)
535 return;
536
537 /* Set NIC to page 0 registers to update boundary register. */
538 NIC_BARRIER(regt, regh);
539 NIC_PUT(regt, regh, ED_P1_CR,
540 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
541 NIC_BARRIER(regt, regh);
542
543 do {
544 /* Get pointer to this buffer's header structure. */
545 packet_ptr = sc->mem_ring +
546 ((sc->next_packet - sc->rec_page_start) << ED_PAGE_SHIFT);
547
548 (*sc->read_hdr)(sc, packet_ptr, &packet_hdr);
549 len = packet_hdr.count;
550
551 /*
552 * Try do deal with old, buggy chips that sometimes duplicate
553 * the low byte of the length into the high byte. We do this
554 * by simply ignoring the high byte of the length and always
555 * recalculating it.
556 *
557 * NOTE: sc->next_packet is pointing at the current packet.
558 */
559 if (packet_hdr.next_packet >= sc->next_packet)
560 nlen = (packet_hdr.next_packet - sc->next_packet);
561 else
562 nlen = ((packet_hdr.next_packet - sc->rec_page_start) +
563 (sc->rec_page_stop - sc->next_packet));
564 --nlen;
565 if ((len & ED_PAGE_MASK) + sizeof(packet_hdr) > ED_PAGE_SIZE)
566 --nlen;
567 len = (len & ED_PAGE_MASK) | (nlen << ED_PAGE_SHIFT);
568 #ifdef DIAGNOSTIC
569 if (len != packet_hdr.count) {
570 aprint_verbose_dev(sc->sc_dev, "length does not match "
571 "next packet pointer\n");
572 aprint_verbose_dev(sc->sc_dev, "len %04x nlen %04x "
573 "start %02x first %02x curr %02x next %02x "
574 "stop %02x\n", packet_hdr.count, len,
575 sc->rec_page_start, sc->next_packet, current,
576 packet_hdr.next_packet, sc->rec_page_stop);
577 }
578 #endif
579
580 /*
581 * Be fairly liberal about what we allow as a "reasonable"
582 * length so that a [crufty] packet will make it to BPF (and
583 * can thus be analyzed). Note that all that is really
584 * important is that we have a length that will fit into one
585 * mbuf cluster or less; the upper layer protocols can then
586 * figure out the length from their own length field(s).
587 */
588 if (len <= MCLBYTES &&
589 packet_hdr.next_packet >= sc->rec_page_start &&
590 packet_hdr.next_packet < sc->rec_page_stop) {
591 /* Go get packet. */
592 dp8390_read(sc,
593 packet_ptr + sizeof(struct dp8390_ring),
594 len - sizeof(struct dp8390_ring));
595 } else {
596 /* Really BAD. The ring pointers are corrupted. */
597 log(LOG_ERR, "%s: NIC memory corrupt - "
598 "invalid packet length %d\n",
599 device_xname(sc->sc_dev), len);
600 ++sc->sc_ec.ec_if.if_ierrors;
601 dp8390_reset(sc);
602 return;
603 }
604
605 /* Update next packet pointer. */
606 sc->next_packet = packet_hdr.next_packet;
607
608 /*
609 * Update NIC boundary pointer - being careful to keep it one
610 * buffer behind (as recommended by NS databook).
611 */
612 boundary = sc->next_packet - 1;
613 if (boundary < sc->rec_page_start)
614 boundary = sc->rec_page_stop - 1;
615 NIC_PUT(regt, regh, ED_P0_BNRY, boundary);
616 } while (sc->next_packet != current);
617
618 goto loop;
619 }
620
621 /* Ethernet interface interrupt processor. */
622 int
623 dp8390_intr(void *arg)
624 {
625 struct dp8390_softc *sc = arg;
626 bus_space_tag_t regt = sc->sc_regt;
627 bus_space_handle_t regh = sc->sc_regh;
628 struct ifnet *ifp = &sc->sc_ec.ec_if;
629 uint8_t isr;
630 #if NRND > 0
631 uint8_t rndisr;
632 #endif
633
634 if (sc->sc_enabled == 0 ||
635 !device_is_active(sc->sc_dev))
636 return 0;
637
638 /* Set NIC to page 0 registers. */
639 NIC_BARRIER(regt, regh);
640 NIC_PUT(regt, regh, ED_P0_CR,
641 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
642 NIC_BARRIER(regt, regh);
643
644 isr = NIC_GET(regt, regh, ED_P0_ISR);
645 if (isr == 0)
646 return 0;
647
648 #if NRND > 0
649 rndisr = isr;
650 #endif
651
652 /* Loop until there are no more new interrupts. */
653 for (;;) {
654 /*
655 * Reset all the bits that we are 'acknowledging' by writing a
656 * '1' to each bit position that was set.
657 * (Writing a '1' *clears* the bit.)
658 */
659 NIC_PUT(regt, regh, ED_P0_ISR, isr);
660
661 /* Work around for AX88190 bug */
662 if ((sc->sc_flags & DP8390_DO_AX88190_WORKAROUND) != 0)
663 while ((NIC_GET(regt, regh, ED_P0_ISR) & isr) != 0) {
664 NIC_PUT(regt, regh, ED_P0_ISR, 0);
665 NIC_PUT(regt, regh, ED_P0_ISR, isr);
666 }
667
668 /*
669 * Handle transmitter interrupts. Handle these first because
670 * the receiver will reset the board under some conditions.
671 *
672 * If the chip was reset while a packet was transmitting, it
673 * may still deliver a TX interrupt. In this case, just ignore
674 * the interrupt.
675 */
676 if ((isr & (ED_ISR_PTX | ED_ISR_TXE)) != 0 &&
677 sc->txb_inuse != 0) {
678 uint8_t collisions =
679 NIC_GET(regt, regh, ED_P0_NCR) & 0x0f;
680
681 /*
682 * Check for transmit error. If a TX completed with an
683 * error, we end up throwing the packet away. Really
684 * the only error that is possible is excessive
685 * collisions, and in this case it is best to allow the
686 * automatic mechanisms of TCP to backoff the flow. Of
687 * course, with UDP we're screwed, but this is expected
688 * when a network is heavily loaded.
689 */
690 if ((isr & ED_ISR_TXE) != 0) {
691 /*
692 * Excessive collisions (16).
693 */
694 if ((NIC_GET(regt, regh, ED_P0_TSR)
695 & ED_TSR_ABT) && (collisions == 0)) {
696 /*
697 * When collisions total 16, the P0_NCR
698 * will indicate 0, and the TSR_ABT is
699 * set.
700 */
701 collisions = 16;
702 }
703
704 /* Update output errors counter. */
705 ++ifp->if_oerrors;
706 } else {
707 /*
708 * Throw away the non-error status bits.
709 *
710 * XXX
711 * It may be useful to detect loss of carrier
712 * and late collisions here.
713 */
714 (void)NIC_GET(regt, regh, ED_P0_TSR);
715
716 /*
717 * Update total number of successfully
718 * transmitted packets.
719 */
720 ++ifp->if_opackets;
721 }
722
723 /* Clear watchdog timer. */
724 ifp->if_timer = 0;
725 ifp->if_flags &= ~IFF_OACTIVE;
726
727 /*
728 * Add in total number of collisions on last
729 * transmission.
730 */
731 ifp->if_collisions += collisions;
732
733 /*
734 * Decrement buffer in-use count if not zero (can only
735 * be zero if a transmitter interrupt occurred while not
736 * actually transmitting).
737 * If data is ready to transmit, start it transmitting,
738 * otherwise defer until after handling receiver.
739 */
740 if (--sc->txb_inuse != 0)
741 dp8390_xmit(sc);
742 }
743
744 /* Handle receiver interrupts. */
745 if ((isr & (ED_ISR_PRX | ED_ISR_RXE | ED_ISR_OVW)) != 0) {
746 /*
747 * Overwrite warning. In order to make sure that a
748 * lockup of the local DMA hasn't occurred, we reset
749 * and re-init the NIC. The NSC manual suggests only a
750 * partial reset/re-init is necessary - but some chips
751 * seem to want more. The DMA lockup has been seen
752 * only with early rev chips - Methinks this bug was
753 * fixed in later revs. -DG
754 */
755 if ((isr & ED_ISR_OVW) != 0) {
756 ++ifp->if_ierrors;
757 #ifdef DIAGNOSTIC
758 log(LOG_WARNING, "%s: warning - receiver "
759 "ring buffer overrun\n",
760 device_xname(sc->sc_dev));
761 #endif
762 /* Stop/reset/re-init NIC. */
763 dp8390_reset(sc);
764 } else {
765 /*
766 * Receiver Error. One or more of: CRC error,
767 * frame alignment error FIFO overrun, or
768 * missed packet.
769 */
770 if ((isr & ED_ISR_RXE) != 0) {
771 ++ifp->if_ierrors;
772 #ifdef DEBUG
773 if (dp8390_debug) {
774 printf("%s: receive error %x\n",
775 device_xname(sc->sc_dev),
776 NIC_GET(regt, regh,
777 ED_P0_RSR));
778 }
779 #endif
780 }
781
782 /*
783 * Go get the packet(s)
784 * XXX - Doing this on an error is dubious
785 * because there shouldn't be any data to get
786 * (we've configured the interface to not
787 * accept packets with errors).
788 */
789 (*sc->recv_int)(sc);
790 }
791 }
792
793 /*
794 * If it looks like the transmitter can take more data, attempt
795 * to start output on the interface. This is done after
796 * handling the receiver to give the receiver priority.
797 */
798 dp8390_start(ifp);
799
800 /*
801 * Return NIC CR to standard state: page 0, remote DMA
802 * complete, start (toggling the TXP bit off, even if was just
803 * set in the transmit routine, is *okay* - it is 'edge'
804 * triggered from low to high).
805 */
806 NIC_BARRIER(regt, regh);
807 NIC_PUT(regt, regh, ED_P0_CR,
808 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
809 NIC_BARRIER(regt, regh);
810
811 /*
812 * If the Network Talley Counters overflow, read them to reset
813 * them. It appears that old 8390's won't clear the ISR flag
814 * otherwise - resulting in an infinite loop.
815 */
816 if ((isr & ED_ISR_CNT) != 0) {
817 (void)NIC_GET(regt, regh, ED_P0_CNTR0);
818 (void)NIC_GET(regt, regh, ED_P0_CNTR1);
819 (void)NIC_GET(regt, regh, ED_P0_CNTR2);
820 }
821
822 isr = NIC_GET(regt, regh, ED_P0_ISR);
823 if (isr == 0)
824 goto out;
825 }
826
827 out:
828 #if NRND > 0
829 rnd_add_uint32(&sc->rnd_source, rndisr);
830 #endif
831 return 1;
832 }
833
834 /*
835 * Process an ioctl request. This code needs some work - it looks pretty ugly.
836 */
837 int
838 dp8390_ioctl(struct ifnet *ifp, u_long cmd, void *data)
839 {
840 struct dp8390_softc *sc = ifp->if_softc;
841 struct ifaddr *ifa = data;
842 struct ifreq *ifr = data;
843 int s, error = 0;
844
845 s = splnet();
846
847 switch (cmd) {
848
849 case SIOCINITIFADDR:
850 if ((error = dp8390_enable(sc)) != 0)
851 break;
852 ifp->if_flags |= IFF_UP;
853
854 dp8390_init(sc);
855 switch (ifa->ifa_addr->sa_family) {
856 #ifdef INET
857 case AF_INET:
858 arp_ifinit(ifp, ifa);
859 break;
860 #endif
861 default:
862 break;
863 }
864 break;
865
866 case SIOCSIFFLAGS:
867 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
868 break;
869 switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
870 case IFF_RUNNING:
871 /*
872 * If interface is marked down and it is running, then
873 * stop it.
874 */
875 dp8390_stop(sc);
876 ifp->if_flags &= ~IFF_RUNNING;
877 dp8390_disable(sc);
878 break;
879 case IFF_UP:
880 /*
881 * If interface is marked up and it is stopped, then
882 * start it.
883 */
884 if ((error = dp8390_enable(sc)) != 0)
885 break;
886 dp8390_init(sc);
887 break;
888 case IFF_UP|IFF_RUNNING:
889 /*
890 * Reset the interface to pick up changes in any other
891 * flags that affect hardware registers.
892 */
893 dp8390_stop(sc);
894 dp8390_init(sc);
895 break;
896 default:
897 break;
898 }
899 break;
900
901 case SIOCADDMULTI:
902 case SIOCDELMULTI:
903 if (sc->sc_enabled == 0) {
904 error = EIO;
905 break;
906 }
907
908 /* Update our multicast list. */
909 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
910 /*
911 * Multicast list has changed; set the hardware filter
912 * accordingly.
913 */
914 if (ifp->if_flags & IFF_RUNNING) {
915 dp8390_stop(sc); /* XXX for ds_setmcaf? */
916 dp8390_init(sc);
917 }
918 error = 0;
919 }
920 break;
921
922 case SIOCGIFMEDIA:
923 case SIOCSIFMEDIA:
924 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
925 break;
926
927 default:
928 error = ether_ioctl(ifp, cmd, data);
929 break;
930 }
931
932 splx(s);
933 return error;
934 }
935
936 /*
937 * Retrieve packet from buffer memory and send to the next level up via
938 * ether_input(). If there is a BPF listener, give a copy to BPF, too.
939 */
940 void
941 dp8390_read(struct dp8390_softc *sc, int buf, u_short len)
942 {
943 struct ifnet *ifp = &sc->sc_ec.ec_if;
944 struct mbuf *m;
945
946 /* Pull packet off interface. */
947 m = dp8390_get(sc, buf, len);
948 if (m == NULL) {
949 ifp->if_ierrors++;
950 return;
951 }
952
953 ifp->if_ipackets++;
954
955 /*
956 * Check if there's a BPF listener on this interface.
957 * If so, hand off the raw packet to bpf.
958 */
959 bpf_mtap(ifp, m);
960
961 (*ifp->if_input)(ifp, m);
962 }
963
964
965 /*
966 * Supporting routines.
967 */
968
969 /*
970 * Compute the multicast address filter from the list of multicast addresses we
971 * need to listen to.
972 */
973 void
974 dp8390_getmcaf(struct ethercom *ec, uint8_t *af)
975 {
976 struct ifnet *ifp = &ec->ec_if;
977 struct ether_multi *enm;
978 uint32_t crc;
979 int i;
980 struct ether_multistep step;
981
982 /*
983 * Set up multicast address filter by passing all multicast addresses
984 * through a crc generator, and then using the high order 6 bits as an
985 * index into the 64 bit logical address filter. The high order bit
986 * selects the word, while the rest of the bits select the bit within
987 * the word.
988 */
989
990 if (ifp->if_flags & IFF_PROMISC) {
991 ifp->if_flags |= IFF_ALLMULTI;
992 for (i = 0; i < 8; i++)
993 af[i] = 0xff;
994 return;
995 }
996 for (i = 0; i < 8; i++)
997 af[i] = 0;
998 ETHER_FIRST_MULTI(step, ec, enm);
999 while (enm != NULL) {
1000 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
1001 sizeof(enm->enm_addrlo)) != 0) {
1002 /*
1003 * We must listen to a range of multicast addresses.
1004 * For now, just accept all multicasts, rather than
1005 * trying to set only those filter bits needed to match
1006 * the range. (At this time, the only use of address
1007 * ranges is for IP multicast routing, for which the
1008 * range is big enough to require all bits set.)
1009 */
1010 ifp->if_flags |= IFF_ALLMULTI;
1011 for (i = 0; i < 8; i++)
1012 af[i] = 0xff;
1013 return;
1014 }
1015
1016 crc = ether_crc32_be(enm->enm_addrlo, ETHER_ADDR_LEN);
1017
1018 /* Just want the 6 most significant bits. */
1019 crc >>= 26;
1020
1021 /* Turn on the corresponding bit in the filter. */
1022 af[crc >> 3] |= 1 << (crc & 0x7);
1023
1024 ETHER_NEXT_MULTI(step, enm);
1025 }
1026 ifp->if_flags &= ~IFF_ALLMULTI;
1027 }
1028
1029 /*
1030 * Copy data from receive buffer to a new mbuf chain allocating mbufs
1031 * as needed. Return pointer to first mbuf in chain.
1032 * sc = dp8390 info (softc)
1033 * src = pointer in dp8390 ring buffer
1034 * total_len = amount of data to copy
1035 */
1036 struct mbuf *
1037 dp8390_get(struct dp8390_softc *sc, int src, u_short total_len)
1038 {
1039 struct ifnet *ifp = &sc->sc_ec.ec_if;
1040 struct mbuf *m, *m0, *newm;
1041 u_short len;
1042
1043 MGETHDR(m0, M_DONTWAIT, MT_DATA);
1044 if (m0 == NULL)
1045 return NULL;
1046 m0->m_pkthdr.rcvif = ifp;
1047 m0->m_pkthdr.len = total_len;
1048 len = MHLEN;
1049 m = m0;
1050
1051 while (total_len > 0) {
1052 if (total_len >= MINCLSIZE) {
1053 MCLGET(m, M_DONTWAIT);
1054 if ((m->m_flags & M_EXT) == 0)
1055 goto bad;
1056 len = MCLBYTES;
1057 }
1058
1059 /*
1060 * Make sure the data after the Ethernet header is aligned.
1061 */
1062 if (m == m0) {
1063 char *newdata = (char *)
1064 ALIGN(m->m_data + sizeof(struct ether_header)) -
1065 sizeof(struct ether_header);
1066 len -= newdata - m->m_data;
1067 m->m_data = newdata;
1068 }
1069
1070 m->m_len = len = min(total_len, len);
1071 src = (*sc->ring_copy)(sc, src, mtod(m, void *), len);
1072
1073 total_len -= len;
1074 if (total_len > 0) {
1075 MGET(newm, M_DONTWAIT, MT_DATA);
1076 if (newm == NULL)
1077 goto bad;
1078 len = MLEN;
1079 m = m->m_next = newm;
1080 }
1081 }
1082
1083 return m0;
1084
1085 bad:
1086 m_freem(m0);
1087 return NULL;
1088 }
1089
1090
1091 /*
1092 * Default driver support functions.
1093 *
1094 * NOTE: all support functions assume 8-bit shared memory.
1095 */
1096 /*
1097 * Zero NIC buffer memory and verify that it is clear.
1098 */
1099 static int
1100 dp8390_test_mem(struct dp8390_softc *sc)
1101 {
1102 bus_space_tag_t buft = sc->sc_buft;
1103 bus_space_handle_t bufh = sc->sc_bufh;
1104 int i;
1105
1106 bus_space_set_region_1(buft, bufh, sc->mem_start, 0, sc->mem_size);
1107
1108 for (i = 0; i < sc->mem_size; ++i) {
1109 if (bus_space_read_1(buft, bufh, sc->mem_start + i)) {
1110 printf(": failed to clear NIC buffer at offset %x - "
1111 "check configuration\n", (sc->mem_start + i));
1112 return 1;
1113 }
1114 }
1115
1116 return 0;
1117 }
1118
1119 /*
1120 * Read a packet header from the ring, given the source offset.
1121 */
1122 static void
1123 dp8390_read_hdr(struct dp8390_softc *sc, int src, struct dp8390_ring *hdrp)
1124 {
1125 bus_space_tag_t buft = sc->sc_buft;
1126 bus_space_handle_t bufh = sc->sc_bufh;
1127
1128 /*
1129 * The byte count includes a 4 byte header that was added by
1130 * the NIC.
1131 */
1132 hdrp->rsr = bus_space_read_1(buft, bufh, src);
1133 hdrp->next_packet = bus_space_read_1(buft, bufh, src + 1);
1134 hdrp->count = bus_space_read_1(buft, bufh, src + 2) |
1135 (bus_space_read_1(buft, bufh, src + 3) << 8);
1136 }
1137
1138 /*
1139 * Copy `amount' bytes from a packet in the ring buffer to a linear
1140 * destination buffer, given a source offset and destination address.
1141 * Takes into account ring-wrap.
1142 */
1143 static int
1144 dp8390_ring_copy(struct dp8390_softc *sc, int src, void *dst, u_short amount)
1145 {
1146 bus_space_tag_t buft = sc->sc_buft;
1147 bus_space_handle_t bufh = sc->sc_bufh;
1148 u_short tmp_amount;
1149
1150 /* Does copy wrap to lower addr in ring buffer? */
1151 if (src + amount > sc->mem_end) {
1152 tmp_amount = sc->mem_end - src;
1153
1154 /* Copy amount up to end of NIC memory. */
1155 bus_space_read_region_1(buft, bufh, src, dst, tmp_amount);
1156
1157 amount -= tmp_amount;
1158 src = sc->mem_ring;
1159 dst = (char *)dst + tmp_amount;
1160 }
1161 bus_space_read_region_1(buft, bufh, src, dst, amount);
1162
1163 return src + amount;
1164 }
1165
1166 /*
1167 * Copy a packet from an mbuf to the transmit buffer on the card.
1168 *
1169 * Currently uses an extra buffer/extra memory copy, unless the whole
1170 * packet fits in one mbuf.
1171 */
1172 static int
1173 dp8390_write_mbuf(struct dp8390_softc *sc, struct mbuf *m, int buf)
1174 {
1175 bus_space_tag_t buft = sc->sc_buft;
1176 bus_space_handle_t bufh = sc->sc_bufh;
1177 uint8_t *data;
1178 int len, totlen = 0;
1179
1180 for (; m ; m = m->m_next) {
1181 data = mtod(m, uint8_t *);
1182 len = m->m_len;
1183 if (len > 0) {
1184 bus_space_write_region_1(buft, bufh, buf, data, len);
1185 totlen += len;
1186 buf += len;
1187 }
1188 }
1189 if (totlen < ETHER_MIN_LEN - ETHER_CRC_LEN) {
1190 bus_space_set_region_1(buft, bufh, buf, 0,
1191 ETHER_MIN_LEN - ETHER_CRC_LEN - totlen);
1192 totlen = ETHER_MIN_LEN - ETHER_CRC_LEN;
1193 }
1194 return totlen;
1195 }
1196
1197 /*
1198 * Enable power on the interface.
1199 */
1200 int
1201 dp8390_enable(struct dp8390_softc *sc)
1202 {
1203
1204 if (sc->sc_enabled == 0 && sc->sc_enable != NULL) {
1205 if ((*sc->sc_enable)(sc) != 0) {
1206 aprint_error_dev(sc->sc_dev,
1207 "device enable failed\n");
1208 return EIO;
1209 }
1210 }
1211
1212 sc->sc_enabled = 1;
1213 return 0;
1214 }
1215
1216 /*
1217 * Disable power on the interface.
1218 */
1219 void
1220 dp8390_disable(struct dp8390_softc *sc)
1221 {
1222
1223 if (sc->sc_enabled != 0 && sc->sc_disable != NULL) {
1224 (*sc->sc_disable)(sc);
1225 sc->sc_enabled = 0;
1226 }
1227 }
1228
1229 int
1230 dp8390_activate(device_t self, enum devact act)
1231 {
1232 struct dp8390_softc *sc = device_private(self);
1233
1234 switch (act) {
1235 case DVACT_DEACTIVATE:
1236 if_deactivate(&sc->sc_ec.ec_if);
1237 return 0;
1238 default:
1239 return EOPNOTSUPP;
1240 }
1241 }
1242
1243 int
1244 dp8390_detach(struct dp8390_softc *sc, int flags)
1245 {
1246 struct ifnet *ifp = &sc->sc_ec.ec_if;
1247
1248 /* Succeed now if there's no work to do. */
1249 if ((sc->sc_flags & DP8390_ATTACHED) == 0)
1250 return 0;
1251
1252 /* dp8390_disable() checks sc->sc_enabled */
1253 dp8390_disable(sc);
1254
1255 if (sc->sc_media_fini != NULL)
1256 (*sc->sc_media_fini)(sc);
1257
1258 /* Delete all remaining media. */
1259 ifmedia_delete_instance(&sc->sc_media, IFM_INST_ANY);
1260
1261 #if NRND > 0
1262 rnd_detach_source(&sc->rnd_source);
1263 #endif
1264 ether_ifdetach(ifp);
1265 if_detach(ifp);
1266
1267 return 0;
1268 }
1269
1270 #ifdef IPKDB_DP8390
1271 static void dp8390_ipkdb_hwinit(struct ipkdb_if *);
1272 static void dp8390_ipkdb_init(struct ipkdb_if *);
1273 static void dp8390_ipkdb_leave(struct ipkdb_if *);
1274 static int dp8390_ipkdb_rcv(struct ipkdb_if *, uint8_t *, int);
1275 static void dp8390_ipkdb_send(struct ipkdb_if *, uint8_t *, int);
1276
1277 /*
1278 * This is essentially similar to dp8390_config above.
1279 */
1280 int
1281 dp8390_ipkdb_attach(struct ipkdb_if *kip)
1282 {
1283 struct dp8390_softc *sc = kip->port;
1284
1285 if (sc->mem_size < 8192 * 2)
1286 sc->txb_cnt = 1;
1287 else if (sc->mem_size < 8192 * 3)
1288 sc->txb_cnt = 2;
1289 else
1290 sc->txb_cnt = 3;
1291
1292 sc->tx_page_start = sc->mem_start >> ED_PAGE_SHIFT;
1293 sc->rec_page_start = sc->tx_page_start + sc->txb_cnt * ED_TXBUF_SIZE;
1294 sc->rec_page_stop = sc->tx_page_start + (sc->mem_size >> ED_PAGE_SHIFT);
1295 sc->mem_ring = sc->mem_start +
1296 ((sc->txb_cnt * ED_TXBUF_SIZE) << ED_PAGE_SHIFT);
1297 sc->mem_end = sc->mem_start + sc->mem_size;
1298
1299 dp8390_stop(sc);
1300
1301 kip->start = dp8390_ipkdb_init;
1302 kip->leave = dp8390_ipkdb_leave;
1303 kip->receive = dp8390_ipkdb_rcv;
1304 kip->send = dp8390_ipkdb_send;
1305
1306 return 0;
1307 }
1308
1309 /*
1310 * Similar to dp8390_init above.
1311 */
1312 static void
1313 dp8390_ipkdb_hwinit(struct ipkdb_if *kip)
1314 {
1315 struct dp8390_softc *sc = kip->port;
1316 struct ifnet *ifp = &sc->sc_ec.ec_if;
1317 bus_space_tag_t regt = sc->sc_regt;
1318 bus_space_handle_t regh = sc->sc_regh;
1319 int i;
1320
1321 sc->txb_inuse = 0;
1322 sc->txb_new = 0;
1323 sc->txb_next_tx = 0;
1324 dp8390_stop(sc);
1325
1326 if (sc->dcr_reg & ED_DCR_LS)
1327 NIC_PUT(regt, regh, ED_P0_DCR, sc->dcr_reg);
1328 else
1329 NIC_PUT(regt, regh, ED_P0_DCR, ED_DCR_FT1 | ED_DCR_LS);
1330 NIC_PUT(regt, regh, ED_P0_RBCR0, 0);
1331 NIC_PUT(regt, regh, ED_P0_RBCR1, 0);
1332 NIC_PUT(regt, regh, ED_P0_RCR, ED_RCR_MON | sc->rcr_proto);
1333 NIC_PUT(regt, regh, ED_P0_TCR, ED_TCR_LB0);
1334 if (sc->is790)
1335 NIC_PUT(regt, regh, 0x09, 0);
1336 NIC_PUT(regt, regh, ED_P0_BNRY, sc->rec_page_start);
1337 NIC_PUT(regt, regh, ED_P0_PSTART, sc->rec_page_start);
1338 NIC_PUT(regt, regh, ED_P0_PSTOP, sc->rec_page_stop);
1339 NIC_PUT(regt, regh, ED_P0_IMR, 0);
1340 NIC_BARRIER(regt, regh);
1341 NIC_PUT(regt, regh, ED_P0_ISR, 0xff);
1342
1343 NIC_BARRIER(regt, regh);
1344 NIC_PUT(regt, regh, ED_P0_CR,
1345 sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STP);
1346 NIC_BARRIER(regt, regh);
1347
1348 for (i = 0; i < sizeof kip->myenetaddr; i++)
1349 NIC_PUT(regt, regh, ED_P1_PAR0 + i, kip->myenetaddr[i]);
1350 /* multicast filter? */
1351
1352 sc->next_packet = sc->rec_page_start + 1;
1353 NIC_PUT(regt, regh, ED_P1_CURR, sc->next_packet);
1354
1355 NIC_BARRIER(regt, regh);
1356 NIC_PUT(regt, regh, ED_P1_CR,
1357 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STP);
1358 NIC_BARRIER(regt, regh);
1359
1360 /* promiscuous mode? */
1361 NIC_PUT(regt, regh, ED_P0_RCR, ED_RCR_AB | ED_RCR_AM | sc->rcr_proto);
1362 NIC_PUT(regt, regh, ED_P0_TCR, 0);
1363
1364 /* card-specific initialization? */
1365
1366 NIC_BARRIER(regt, regh);
1367 NIC_PUT(regt, regh, ED_P0_CR,
1368 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
1369
1370 ifp->if_flags &= ~IFF_OACTIVE;
1371 }
1372
1373 static void
1374 dp8390_ipkdb_init(struct ipkdb_if *kip)
1375 {
1376 struct dp8390_softc *sc = kip->port;
1377 bus_space_tag_t regt = sc->sc_regt;
1378 bus_space_handle_t regh = sc->sc_regh;
1379 uint8_t cmd;
1380
1381 cmd = NIC_GET(regt, regh, ED_P0_CR) & ~(ED_CR_PAGE_3 | ED_CR_STA);
1382
1383 /* Select page 0 */
1384 NIC_BARRIER(regt, regh);
1385 NIC_PUT(regt, regh, ED_P0_CR, cmd | ED_CR_PAGE_0 | ED_CR_STP);
1386 NIC_BARRIER(regt, regh);
1387
1388 /* If not started, init chip */
1389 if ((cmd & ED_CR_STP) != 0)
1390 dp8390_ipkdb_hwinit(kip);
1391
1392 /* If output active, wait for packets to drain */
1393 while (sc->txb_inuse) {
1394 while ((cmd = (NIC_GET(regt, regh, ED_P0_ISR) &
1395 (ED_ISR_PTX | ED_ISR_TXE))) == 0)
1396 DELAY(1);
1397 NIC_PUT(regt, regh, ED_P0_ISR, cmd);
1398 if (--sc->txb_inuse)
1399 dp8390_xmit(sc);
1400 }
1401 }
1402
1403 static void
1404 dp8390_ipkdb_leave(struct ipkdb_if *kip)
1405 {
1406 struct dp8390_softc *sc = kip->port;
1407 struct ifnet *ifp = &sc->sc_ec.ec_if;
1408
1409 ifp->if_timer = 0;
1410 }
1411
1412 /*
1413 * Similar to dp8390_intr above.
1414 */
1415 static int
1416 dp8390_ipkdb_rcv(struct ipkdb_if *kip, uint8_t *buf, int poll)
1417 {
1418 struct dp8390_softc *sc = kip->port;
1419 bus_space_tag_t regt = sc->sc_regt;
1420 bus_space_handle_t regh = sc->sc_regh;
1421 uint8_t bnry, current, isr;
1422 int len, nlen, packet_ptr;
1423 struct dp8390_ring packet_hdr;
1424
1425 /* Switch to page 0. */
1426 NIC_BARRIER(regt, regh);
1427 NIC_PUT(regt, regh, ED_P0_CR,
1428 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
1429 NIC_BARRIER(regt, regh);
1430
1431 for (;;) {
1432 isr = NIC_GET(regt, regh, ED_P0_ISR);
1433 NIC_PUT(regt, regh, ED_P0_ISR, isr);
1434
1435 if (isr & (ED_ISR_PRX | ED_ISR_TXE)) {
1436 NIC_GET(regt, regh, ED_P0_NCR);
1437 NIC_GET(regt, regh, ED_P0_TSR);
1438 }
1439
1440 if (isr & ED_ISR_OVW) {
1441 dp8390_ipkdb_hwinit(kip);
1442 continue;
1443 }
1444
1445 if (isr & ED_ISR_CNT) {
1446 NIC_GET(regt, regh, ED_P0_CNTR0);
1447 NIC_GET(regt, regh, ED_P0_CNTR1);
1448 NIC_GET(regt, regh, ED_P0_CNTR2);
1449 }
1450
1451 /* Similar to dp8390_rint above. */
1452 NIC_BARRIER(regt, regh);
1453 NIC_PUT(regt, regh, ED_P0_CR,
1454 sc->cr_proto | ED_CR_PAGE_1 | ED_CR_STA);
1455 NIC_BARRIER(regt, regh);
1456
1457 current = NIC_GET(regt, regh, ED_P1_CURR);
1458
1459 NIC_BARRIER(regt, regh);
1460 NIC_PUT(regt, regh, ED_P1_CR,
1461 sc->cr_proto | ED_CR_PAGE_0 | ED_CR_STA);
1462 NIC_BARRIER(regt, regh);
1463
1464 if (sc->next_packet == current) {
1465 if (poll)
1466 return 0;
1467 continue;
1468 }
1469
1470 packet_ptr = sc->mem_ring +
1471 ((sc->next_packet - sc->rec_page_start) << ED_PAGE_SHIFT);
1472 sc->read_hdr(sc, packet_ptr, &packet_hdr);
1473 len = packet_hdr.count;
1474 nlen = packet_hdr.next_packet - sc->next_packet;
1475 if (nlen < 0)
1476 nlen += sc->rec_page_stop - sc->rec_page_start;
1477 nlen--;
1478 if ((len & ED_PAGE_MASK) + sizeof(packet_hdr) > ED_PAGE_SIZE)
1479 nlen--;
1480 len = (len & ED_PAGE_MASK) | (nlen << ED_PAGE_SHIFT);
1481 len -= sizeof(packet_hdr);
1482
1483 if (len <= ETHERMTU &&
1484 packet_hdr.next_packet >= sc->rec_page_start &&
1485 packet_hdr.next_packet < sc->rec_page_stop) {
1486 sc->ring_copy(sc, packet_ptr + sizeof(packet_hdr),
1487 buf, len);
1488 sc->next_packet = packet_hdr.next_packet;
1489 bnry = sc->next_packet - 1;
1490 if (bnry < sc->rec_page_start)
1491 bnry = sc->rec_page_stop - 1;
1492 NIC_PUT(regt, regh, ED_P0_BNRY, bnry);
1493 return len;
1494 }
1495
1496 dp8390_ipkdb_hwinit(kip);
1497 }
1498 }
1499
1500 static void
1501 dp8390_ipkdb_send(struct ipkdb_if *kip, uint8_t *buf, int l)
1502 {
1503 struct dp8390_softc *sc = kip->port;
1504 bus_space_tag_t regt = sc->sc_regt;
1505 bus_space_handle_t regh = sc->sc_regh;
1506 struct mbuf mb;
1507
1508 mb.m_next = NULL;
1509 mb.m_pkthdr.len = mb.m_len = l;
1510 mb.m_data = buf;
1511 mb.m_flags = M_EXT | M_PKTHDR;
1512 mb.m_type = MT_DATA;
1513
1514 l = sc->write_mbuf(sc, &mb,
1515 sc->mem_start + ((sc->txb_new * ED_TXBUF_SIZE) << ED_PAGE_SHIFT));
1516 sc->txb_len[sc->txb_new] = max(l, ETHER_MIN_LEN - ETHER_CRC_LEN);
1517
1518 if (++sc->txb_new == sc->txb_cnt)
1519 sc->txb_new = 0;
1520
1521 sc->txb_inuse++;
1522 dp8390_xmit(sc);
1523
1524 while ((NIC_GET(regt, regh, ED_P0_ISR) &
1525 (ED_ISR_PTX | ED_ISR_TXE)) == 0)
1526 DELAY(1);
1527
1528 sc->txb_inuse--;
1529 }
1530 #endif
1531