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