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