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