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