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