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