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