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