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