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