xref: /netbsd-src/sys/dev/sbus/qe.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: qe.c,v 1.58 2010/04/05 07:21:47 joerg Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Paul Kranenburg.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1998 Jason L. Wright.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. The name of the authors may not be used to endorse or promote products
45  *    derived from this software without specific prior written permission.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
48  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
49  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
50  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
51  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
52  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
53  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
54  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
55  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
56  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57  */
58 
59 /*
60  * Driver for the SBus qec+qe QuadEthernet board.
61  *
62  * This driver was written using the AMD MACE Am79C940 documentation, some
63  * ideas gleaned from the S/Linux driver for this card, Solaris header files,
64  * and a loan of a card from Paul Southworth of the Internet Engineering
65  * Group (www.ieng.com).
66  */
67 
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: qe.c,v 1.58 2010/04/05 07:21:47 joerg Exp $");
70 
71 #define QEDEBUG
72 
73 #include "opt_ddb.h"
74 #include "opt_inet.h"
75 #include "rnd.h"
76 
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
80 #include <sys/errno.h>
81 #include <sys/ioctl.h>
82 #include <sys/mbuf.h>
83 #include <sys/socket.h>
84 #include <sys/syslog.h>
85 #include <sys/device.h>
86 #include <sys/malloc.h>
87 #if NRND > 0
88 #include <sys/rnd.h>
89 #endif
90 
91 #include <net/if.h>
92 #include <net/if_dl.h>
93 #include <net/if_types.h>
94 #include <net/netisr.h>
95 #include <net/if_media.h>
96 #include <net/if_ether.h>
97 
98 #ifdef INET
99 #include <netinet/in.h>
100 #include <netinet/if_inarp.h>
101 #include <netinet/in_systm.h>
102 #include <netinet/in_var.h>
103 #include <netinet/ip.h>
104 #endif
105 
106 
107 #include <net/bpf.h>
108 #include <net/bpfdesc.h>
109 
110 #include <sys/bus.h>
111 #include <sys/intr.h>
112 #include <machine/autoconf.h>
113 
114 #include <dev/sbus/sbusvar.h>
115 #include <dev/sbus/qecreg.h>
116 #include <dev/sbus/qecvar.h>
117 #include <dev/sbus/qereg.h>
118 
119 struct qe_softc {
120 	struct	device	sc_dev;		/* base device */
121 	bus_space_tag_t	sc_bustag;	/* bus & DMA tags */
122 	bus_dma_tag_t	sc_dmatag;
123 	bus_dmamap_t	sc_dmamap;
124 	struct	ethercom sc_ethercom;
125 	struct	ifmedia sc_ifmedia;	/* interface media */
126 
127 	struct	qec_softc *sc_qec;	/* QEC parent */
128 
129 	bus_space_handle_t	sc_qr;	/* QEC registers */
130 	bus_space_handle_t	sc_mr;	/* MACE registers */
131 	bus_space_handle_t	sc_cr;	/* channel registers */
132 
133 	int	sc_channel;		/* channel number */
134 	u_int	sc_rev;			/* board revision */
135 
136 	int	sc_burst;
137 
138 	struct  qec_ring	sc_rb;	/* Packet Ring Buffer */
139 
140 	/* MAC address */
141 	uint8_t sc_enaddr[6];
142 
143 #ifdef QEDEBUG
144 	int	sc_debug;
145 #endif
146 };
147 
148 int	qematch(device_t, cfdata_t, void *);
149 void	qeattach(device_t, device_t, void *);
150 
151 void	qeinit(struct qe_softc *);
152 void	qestart(struct ifnet *);
153 void	qestop(struct qe_softc *);
154 void	qewatchdog(struct ifnet *);
155 int	qeioctl(struct ifnet *, u_long, void *);
156 void	qereset(struct qe_softc *);
157 
158 int	qeintr(void *);
159 int	qe_eint(struct qe_softc *, uint32_t);
160 int	qe_rint(struct qe_softc *);
161 int	qe_tint(struct qe_softc *);
162 void	qe_mcreset(struct qe_softc *);
163 
164 static int	qe_put(struct qe_softc *, int, struct mbuf *);
165 static void	qe_read(struct qe_softc *, int, int);
166 static struct mbuf	*qe_get(struct qe_softc *, int, int);
167 
168 /* ifmedia callbacks */
169 void	qe_ifmedia_sts(struct ifnet *, struct ifmediareq *);
170 int	qe_ifmedia_upd(struct ifnet *);
171 
172 CFATTACH_DECL(qe, sizeof(struct qe_softc),
173     qematch, qeattach, NULL, NULL);
174 
175 int
176 qematch(device_t parent, cfdata_t cf, void *aux)
177 {
178 	struct sbus_attach_args *sa = aux;
179 
180 	return (strcmp(cf->cf_name, sa->sa_name) == 0);
181 }
182 
183 void
184 qeattach(device_t parent, device_t self, void *aux)
185 {
186 	struct sbus_attach_args *sa = aux;
187 	struct qec_softc *qec = device_private(parent);
188 	struct qe_softc *sc = device_private(self);
189 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
190 	int node = sa->sa_node;
191 	bus_dma_tag_t dmatag = sa->sa_dmatag;
192 	bus_dma_segment_t seg;
193 	bus_size_t size;
194 	int rseg, error;
195 
196 	if (sa->sa_nreg < 2) {
197 		printf("%s: only %d register sets\n",
198 			device_xname(self), sa->sa_nreg);
199 		return;
200 	}
201 
202 	if (bus_space_map(sa->sa_bustag,
203 			  (bus_addr_t)BUS_ADDR(
204 				sa->sa_reg[0].oa_space,
205 				sa->sa_reg[0].oa_base),
206 			  (bus_size_t)sa->sa_reg[0].oa_size,
207 			  0, &sc->sc_cr) != 0) {
208 		aprint_error_dev(self, "cannot map registers\n");
209 		return;
210 	}
211 
212 	if (bus_space_map(sa->sa_bustag,
213 			  (bus_addr_t)BUS_ADDR(
214 				sa->sa_reg[1].oa_space,
215 				sa->sa_reg[1].oa_base),
216 			  (bus_size_t)sa->sa_reg[1].oa_size,
217 			  0, &sc->sc_mr) != 0) {
218 		aprint_error_dev(self, "cannot map registers\n");
219 		return;
220 	}
221 
222 	sc->sc_rev = prom_getpropint(node, "mace-version", -1);
223 	printf(" rev %x", sc->sc_rev);
224 
225 	sc->sc_bustag = sa->sa_bustag;
226 	sc->sc_dmatag = sa->sa_dmatag;
227 	sc->sc_qec = qec;
228 	sc->sc_qr = qec->sc_regs;
229 
230 	sc->sc_channel = prom_getpropint(node, "channel#", -1);
231 	sc->sc_burst = qec->sc_burst;
232 
233 	qestop(sc);
234 
235 	/* Note: no interrupt level passed */
236 	(void)bus_intr_establish(sa->sa_bustag, 0, IPL_NET, qeintr, sc);
237 	prom_getether(node, sc->sc_enaddr);
238 
239 	/*
240 	 * Allocate descriptor ring and buffers.
241 	 */
242 
243 	/* for now, allocate as many bufs as there are ring descriptors */
244 	sc->sc_rb.rb_ntbuf = QEC_XD_RING_MAXSIZE;
245 	sc->sc_rb.rb_nrbuf = QEC_XD_RING_MAXSIZE;
246 
247 	size =	QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd) +
248 		QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd) +
249 		sc->sc_rb.rb_ntbuf * QE_PKT_BUF_SZ +
250 		sc->sc_rb.rb_nrbuf * QE_PKT_BUF_SZ;
251 
252 	/* Get a DMA handle */
253 	if ((error = bus_dmamap_create(dmatag, size, 1, size, 0,
254 				    BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
255 		aprint_error_dev(self, "DMA map create error %d\n",
256 			error);
257 		return;
258 	}
259 
260 	/* Allocate DMA buffer */
261 	if ((error = bus_dmamem_alloc(dmatag, size, 0, 0,
262 				      &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
263 		aprint_error_dev(self, "DMA buffer alloc error %d\n",
264 			error);
265 		return;
266 	}
267 
268 	/* Map DMA buffer in CPU addressable space */
269 	if ((error = bus_dmamem_map(dmatag, &seg, rseg, size,
270 			            &sc->sc_rb.rb_membase,
271 			            BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
272 		aprint_error_dev(self, "DMA buffer map error %d\n",
273 			error);
274 		bus_dmamem_free(dmatag, &seg, rseg);
275 		return;
276 	}
277 
278 	/* Load the buffer */
279 	if ((error = bus_dmamap_load(dmatag, sc->sc_dmamap,
280 				     sc->sc_rb.rb_membase, size, NULL,
281 				     BUS_DMA_NOWAIT)) != 0) {
282 		aprint_error_dev(self, "DMA buffer map load error %d\n",
283 			error);
284 		bus_dmamem_unmap(dmatag, sc->sc_rb.rb_membase, size);
285 		bus_dmamem_free(dmatag, &seg, rseg);
286 		return;
287 	}
288 	sc->sc_rb.rb_dmabase = sc->sc_dmamap->dm_segs[0].ds_addr;
289 
290 	/* Initialize media properties */
291 	ifmedia_init(&sc->sc_ifmedia, 0, qe_ifmedia_upd, qe_ifmedia_sts);
292 	ifmedia_add(&sc->sc_ifmedia,
293 		    IFM_MAKEWORD(IFM_ETHER,IFM_10_T,0,0),
294 		    0, NULL);
295 	ifmedia_add(&sc->sc_ifmedia,
296 		    IFM_MAKEWORD(IFM_ETHER,IFM_10_5,0,0),
297 		    0, NULL);
298 	ifmedia_add(&sc->sc_ifmedia,
299 		    IFM_MAKEWORD(IFM_ETHER,IFM_AUTO,0,0),
300 		    0, NULL);
301 	ifmedia_set(&sc->sc_ifmedia, IFM_ETHER|IFM_AUTO);
302 
303 	memcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
304 	ifp->if_softc = sc;
305 	ifp->if_start = qestart;
306 	ifp->if_ioctl = qeioctl;
307 	ifp->if_watchdog = qewatchdog;
308 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS |
309 	    IFF_MULTICAST;
310 	IFQ_SET_READY(&ifp->if_snd);
311 
312 	/* Attach the interface. */
313 	if_attach(ifp);
314 	ether_ifattach(ifp, sc->sc_enaddr);
315 
316 	printf(" address %s\n", ether_sprintf(sc->sc_enaddr));
317 }
318 
319 /*
320  * Pull data off an interface.
321  * Len is the length of data, with local net header stripped.
322  * We copy the data into mbufs.  When full cluster sized units are present,
323  * we copy into clusters.
324  */
325 static inline struct mbuf *
326 qe_get(struct qe_softc *sc, int idx, int totlen)
327 {
328 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
329 	struct mbuf *m;
330 	struct mbuf *top, **mp;
331 	int len, pad, boff = 0;
332 	uint8_t *bp;
333 
334 	bp = sc->sc_rb.rb_rxbuf + (idx % sc->sc_rb.rb_nrbuf) * QE_PKT_BUF_SZ;
335 
336 	MGETHDR(m, M_DONTWAIT, MT_DATA);
337 	if (m == NULL)
338 		return (NULL);
339 	m->m_pkthdr.rcvif = ifp;
340 	m->m_pkthdr.len = totlen;
341 	pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
342 	m->m_data += pad;
343 	len = MHLEN - pad;
344 	top = NULL;
345 	mp = &top;
346 
347 	while (totlen > 0) {
348 		if (top) {
349 			MGET(m, M_DONTWAIT, MT_DATA);
350 			if (m == NULL) {
351 				m_freem(top);
352 				return (NULL);
353 			}
354 			len = MLEN;
355 		}
356 		if (top && totlen >= MINCLSIZE) {
357 			MCLGET(m, M_DONTWAIT);
358 			if (m->m_flags & M_EXT)
359 				len = MCLBYTES;
360 		}
361 		m->m_len = len = min(totlen, len);
362 		memcpy(mtod(m, void *), bp + boff, len);
363 		boff += len;
364 		totlen -= len;
365 		*mp = m;
366 		mp = &m->m_next;
367 	}
368 
369 	return (top);
370 }
371 
372 /*
373  * Routine to copy from mbuf chain to transmit buffer in
374  * network buffer memory.
375  */
376 inline int
377 qe_put(struct qe_softc *sc, int idx, struct mbuf *m)
378 {
379 	struct mbuf *n;
380 	int len, tlen = 0, boff = 0;
381 	uint8_t *bp;
382 
383 	bp = sc->sc_rb.rb_txbuf + (idx % sc->sc_rb.rb_ntbuf) * QE_PKT_BUF_SZ;
384 
385 	for (; m; m = n) {
386 		len = m->m_len;
387 		if (len == 0) {
388 			MFREE(m, n);
389 			continue;
390 		}
391 		memcpy(bp + boff, mtod(m, void *), len);
392 		boff += len;
393 		tlen += len;
394 		MFREE(m, n);
395 	}
396 	return (tlen);
397 }
398 
399 /*
400  * Pass a packet to the higher levels.
401  */
402 inline void
403 qe_read(struct qe_softc *sc, int idx, int len)
404 {
405 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
406 	struct mbuf *m;
407 
408 	if (len <= sizeof(struct ether_header) ||
409 	    len > ETHERMTU + sizeof(struct ether_header)) {
410 
411 		printf("%s: invalid packet size %d; dropping\n",
412 			ifp->if_xname, len);
413 
414 		ifp->if_ierrors++;
415 		return;
416 	}
417 
418 	/*
419 	 * Pull packet off interface.
420 	 */
421 	m = qe_get(sc, idx, len);
422 	if (m == NULL) {
423 		ifp->if_ierrors++;
424 		return;
425 	}
426 	ifp->if_ipackets++;
427 
428 	/*
429 	 * Check if there's a BPF listener on this interface.
430 	 * If so, hand off the raw packet to BPF.
431 	 */
432 	bpf_mtap(ifp, m);
433 	/* Pass the packet up. */
434 	(*ifp->if_input)(ifp, m);
435 }
436 
437 /*
438  * Start output on interface.
439  * We make two assumptions here:
440  *  1) that the current priority is set to splnet _before_ this code
441  *     is called *and* is returned to the appropriate priority after
442  *     return
443  *  2) that the IFF_OACTIVE flag is checked before this code is called
444  *     (i.e. that the output part of the interface is idle)
445  */
446 void
447 qestart(struct ifnet *ifp)
448 {
449 	struct qe_softc *sc = ifp->if_softc;
450 	struct qec_xd *txd = sc->sc_rb.rb_txd;
451 	struct mbuf *m;
452 	unsigned int bix, len;
453 	unsigned int ntbuf = sc->sc_rb.rb_ntbuf;
454 
455 	if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
456 		return;
457 
458 	bix = sc->sc_rb.rb_tdhead;
459 
460 	for (;;) {
461 		IFQ_DEQUEUE(&ifp->if_snd, m);
462 		if (m == 0)
463 			break;
464 
465 		/*
466 		 * If BPF is listening on this interface, let it see the
467 		 * packet before we commit it to the wire.
468 		 */
469 		bpf_mtap(ifp, m);
470 
471 		/*
472 		 * Copy the mbuf chain into the transmit buffer.
473 		 */
474 		len = qe_put(sc, bix, m);
475 
476 		/*
477 		 * Initialize transmit registers and start transmission
478 		 */
479 		txd[bix].xd_flags = QEC_XD_OWN | QEC_XD_SOP | QEC_XD_EOP |
480 				    (len & QEC_XD_LENGTH);
481 		bus_space_write_4(sc->sc_bustag, sc->sc_cr, QE_CRI_CTRL,
482 				  QE_CR_CTRL_TWAKEUP);
483 
484 		if (++bix == QEC_XD_RING_MAXSIZE)
485 			bix = 0;
486 
487 		if (++sc->sc_rb.rb_td_nbusy == ntbuf) {
488 			ifp->if_flags |= IFF_OACTIVE;
489 			break;
490 		}
491 	}
492 
493 	sc->sc_rb.rb_tdhead = bix;
494 }
495 
496 void
497 qestop(struct qe_softc *sc)
498 {
499 	bus_space_tag_t t = sc->sc_bustag;
500 	bus_space_handle_t mr = sc->sc_mr;
501 	bus_space_handle_t cr = sc->sc_cr;
502 	int n;
503 
504 #if defined(SUN4U) || defined(__GNUC__)
505 	(void)&t;
506 #endif
507 	/* Stop the schwurst */
508 	bus_space_write_1(t, mr, QE_MRI_BIUCC, QE_MR_BIUCC_SWRST);
509 	for (n = 200; n > 0; n--) {
510 		if ((bus_space_read_1(t, mr, QE_MRI_BIUCC) &
511 			QE_MR_BIUCC_SWRST) == 0)
512 			break;
513 		DELAY(20);
514 	}
515 
516 	/* then reset */
517 	bus_space_write_4(t, cr, QE_CRI_CTRL, QE_CR_CTRL_RESET);
518 	for (n = 200; n > 0; n--) {
519 		if ((bus_space_read_4(t, cr, QE_CRI_CTRL) &
520 			QE_CR_CTRL_RESET) == 0)
521 			break;
522 		DELAY(20);
523 	}
524 }
525 
526 /*
527  * Reset interface.
528  */
529 void
530 qereset(struct qe_softc *sc)
531 {
532 	int s;
533 
534 	s = splnet();
535 	qestop(sc);
536 	qeinit(sc);
537 	splx(s);
538 }
539 
540 void
541 qewatchdog(struct ifnet *ifp)
542 {
543 	struct qe_softc *sc = ifp->if_softc;
544 
545 	log(LOG_ERR, "%s: device timeout\n", device_xname(&sc->sc_dev));
546 	ifp->if_oerrors++;
547 
548 	qereset(sc);
549 }
550 
551 /*
552  * Interrupt dispatch.
553  */
554 int
555 qeintr(void *arg)
556 {
557 	struct qe_softc *sc = arg;
558 	bus_space_tag_t t = sc->sc_bustag;
559 	uint32_t qecstat, qestat;
560 	int r = 0;
561 
562 #if defined(SUN4U) || defined(__GNUC__)
563 	(void)&t;
564 #endif
565 	/* Read QEC status and channel status */
566 	qecstat = bus_space_read_4(t, sc->sc_qr, QEC_QRI_STAT);
567 #ifdef QEDEBUG
568 	if (sc->sc_debug) {
569 		printf("qe%d: intr: qecstat=%x\n", sc->sc_channel, qecstat);
570 	}
571 #endif
572 
573 	/* Filter out status for this channel */
574 	qecstat = qecstat >> (4 * sc->sc_channel);
575 	if ((qecstat & 0xf) == 0)
576 		return (r);
577 
578 	qestat = bus_space_read_4(t, sc->sc_cr, QE_CRI_STAT);
579 
580 #ifdef QEDEBUG
581 	if (sc->sc_debug) {
582 		char bits[64]; int i;
583 		bus_space_tag_t t1 = sc->sc_bustag;
584 		bus_space_handle_t mr = sc->sc_mr;
585 
586 		snprintb(bits, sizeof(bits), QE_CR_STAT_BITS, qestat);
587 		printf("qe%d: intr: qestat=%s\n", sc->sc_channel, bits);
588 
589 		printf("MACE registers:\n");
590 		for (i = 0 ; i < 32; i++) {
591 			printf("  m[%d]=%x,", i, bus_space_read_1(t1, mr, i));
592 			if (((i+1) & 7) == 0)
593 				printf("\n");
594 		}
595 	}
596 #endif
597 
598 	if (qestat & QE_CR_STAT_ALLERRORS) {
599 #ifdef QEDEBUG
600 		if (sc->sc_debug) {
601 			char bits[64];
602 			snprintb(bits, sizeof(bits), QE_CR_STAT_BITS, qestat);
603 			printf("qe%d: eint: qestat=%s\n", sc->sc_channel, bits);
604 		}
605 #endif
606 		r |= qe_eint(sc, qestat);
607 		if (r == -1)
608 			return (1);
609 	}
610 
611 	if (qestat & QE_CR_STAT_TXIRQ)
612 		r |= qe_tint(sc);
613 
614 	if (qestat & QE_CR_STAT_RXIRQ)
615 		r |= qe_rint(sc);
616 
617 	return (r);
618 }
619 
620 /*
621  * Transmit interrupt.
622  */
623 int
624 qe_tint(struct qe_softc *sc)
625 {
626 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
627 	unsigned int bix, txflags;
628 
629 	bix = sc->sc_rb.rb_tdtail;
630 
631 	for (;;) {
632 		if (sc->sc_rb.rb_td_nbusy <= 0)
633 			break;
634 
635 		txflags = sc->sc_rb.rb_txd[bix].xd_flags;
636 
637 		if (txflags & QEC_XD_OWN)
638 			break;
639 
640 		ifp->if_flags &= ~IFF_OACTIVE;
641 		ifp->if_opackets++;
642 
643 		if (++bix == QEC_XD_RING_MAXSIZE)
644 			bix = 0;
645 
646 		--sc->sc_rb.rb_td_nbusy;
647 	}
648 
649 	sc->sc_rb.rb_tdtail = bix;
650 
651 	qestart(ifp);
652 
653 	if (sc->sc_rb.rb_td_nbusy == 0)
654 		ifp->if_timer = 0;
655 
656 	return (1);
657 }
658 
659 /*
660  * Receive interrupt.
661  */
662 int
663 qe_rint(struct qe_softc *sc)
664 {
665 	struct qec_xd *xd = sc->sc_rb.rb_rxd;
666 	unsigned int bix, len;
667 	unsigned int nrbuf = sc->sc_rb.rb_nrbuf;
668 #ifdef QEDEBUG
669 	int npackets = 0;
670 #endif
671 
672 	bix = sc->sc_rb.rb_rdtail;
673 
674 	/*
675 	 * Process all buffers with valid data.
676 	 */
677 	for (;;) {
678 		len = xd[bix].xd_flags;
679 		if (len & QEC_XD_OWN)
680 			break;
681 
682 #ifdef QEDEBUG
683 		npackets++;
684 #endif
685 
686 		len &= QEC_XD_LENGTH;
687 		len -= 4;
688 		qe_read(sc, bix, len);
689 
690 		/* ... */
691 		xd[(bix+nrbuf) % QEC_XD_RING_MAXSIZE].xd_flags =
692 			QEC_XD_OWN | (QE_PKT_BUF_SZ & QEC_XD_LENGTH);
693 
694 		if (++bix == QEC_XD_RING_MAXSIZE)
695 			bix = 0;
696 	}
697 #ifdef QEDEBUG
698 	if (npackets == 0 && sc->sc_debug)
699 		printf("%s: rint: no packets; rb index %d; status 0x%x\n",
700 			device_xname(&sc->sc_dev), bix, len);
701 #endif
702 
703 	sc->sc_rb.rb_rdtail = bix;
704 
705 	return (1);
706 }
707 
708 /*
709  * Error interrupt.
710  */
711 int
712 qe_eint(struct qe_softc *sc, uint32_t why)
713 {
714 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
715 	device_t self = &sc->sc_dev;
716 	int r = 0, rst = 0;
717 
718 	if (why & QE_CR_STAT_EDEFER) {
719 		printf("%s: excessive tx defers.\n", device_xname(self));
720 		r |= 1;
721 		ifp->if_oerrors++;
722 	}
723 
724 	if (why & QE_CR_STAT_CLOSS) {
725 		printf("%s: no carrier, link down?\n", device_xname(self));
726 		ifp->if_oerrors++;
727 		r |= 1;
728 	}
729 
730 	if (why & QE_CR_STAT_ERETRIES) {
731 		printf("%s: excessive tx retries\n", device_xname(self));
732 		ifp->if_oerrors++;
733 		r |= 1;
734 		rst = 1;
735 	}
736 
737 
738 	if (why & QE_CR_STAT_LCOLL) {
739 		printf("%s: late tx transmission\n", device_xname(self));
740 		ifp->if_oerrors++;
741 		r |= 1;
742 		rst = 1;
743 	}
744 
745 	if (why & QE_CR_STAT_FUFLOW) {
746 		printf("%s: tx fifo underflow\n", device_xname(self));
747 		ifp->if_oerrors++;
748 		r |= 1;
749 		rst = 1;
750 	}
751 
752 	if (why & QE_CR_STAT_JERROR) {
753 		printf("%s: jabber seen\n", device_xname(self));
754 		r |= 1;
755 	}
756 
757 	if (why & QE_CR_STAT_BERROR) {
758 		printf("%s: babble seen\n", device_xname(self));
759 		r |= 1;
760 	}
761 
762 	if (why & QE_CR_STAT_TCCOFLOW) {
763 		ifp->if_collisions += 256;
764 		ifp->if_oerrors += 256;
765 		r |= 1;
766 	}
767 
768 	if (why & QE_CR_STAT_TXDERROR) {
769 		printf("%s: tx descriptor is bad\n", device_xname(self));
770 		rst = 1;
771 		r |= 1;
772 	}
773 
774 	if (why & QE_CR_STAT_TXLERR) {
775 		printf("%s: tx late error\n", device_xname(self));
776 		ifp->if_oerrors++;
777 		rst = 1;
778 		r |= 1;
779 	}
780 
781 	if (why & QE_CR_STAT_TXPERR) {
782 		printf("%s: tx DMA parity error\n", device_xname(self));
783 		ifp->if_oerrors++;
784 		rst = 1;
785 		r |= 1;
786 	}
787 
788 	if (why & QE_CR_STAT_TXSERR) {
789 		printf("%s: tx DMA sbus error ack\n", device_xname(self));
790 		ifp->if_oerrors++;
791 		rst = 1;
792 		r |= 1;
793 	}
794 
795 	if (why & QE_CR_STAT_RCCOFLOW) {
796 		ifp->if_collisions += 256;
797 		ifp->if_ierrors += 256;
798 		r |= 1;
799 	}
800 
801 	if (why & QE_CR_STAT_RUOFLOW) {
802 		ifp->if_ierrors += 256;
803 		r |= 1;
804 	}
805 
806 	if (why & QE_CR_STAT_MCOFLOW) {
807 		ifp->if_ierrors += 256;
808 		r |= 1;
809 	}
810 
811 	if (why & QE_CR_STAT_RXFOFLOW) {
812 		printf("%s: rx fifo overflow\n", device_xname(self));
813 		ifp->if_ierrors++;
814 		r |= 1;
815 	}
816 
817 	if (why & QE_CR_STAT_RLCOLL) {
818 		printf("%s: rx late collision\n", device_xname(self));
819 		ifp->if_ierrors++;
820 		ifp->if_collisions++;
821 		r |= 1;
822 	}
823 
824 	if (why & QE_CR_STAT_FCOFLOW) {
825 		ifp->if_ierrors += 256;
826 		r |= 1;
827 	}
828 
829 	if (why & QE_CR_STAT_CECOFLOW) {
830 		ifp->if_ierrors += 256;
831 		r |= 1;
832 	}
833 
834 	if (why & QE_CR_STAT_RXDROP) {
835 		printf("%s: rx packet dropped\n", device_xname(self));
836 		ifp->if_ierrors++;
837 		r |= 1;
838 	}
839 
840 	if (why & QE_CR_STAT_RXSMALL) {
841 		printf("%s: rx buffer too small\n", device_xname(self));
842 		ifp->if_ierrors++;
843 		r |= 1;
844 		rst = 1;
845 	}
846 
847 	if (why & QE_CR_STAT_RXLERR) {
848 		printf("%s: rx late error\n", device_xname(self));
849 		ifp->if_ierrors++;
850 		r |= 1;
851 		rst = 1;
852 	}
853 
854 	if (why & QE_CR_STAT_RXPERR) {
855 		printf("%s: rx DMA parity error\n", device_xname(self));
856 		ifp->if_ierrors++;
857 		r |= 1;
858 		rst = 1;
859 	}
860 
861 	if (why & QE_CR_STAT_RXSERR) {
862 		printf("%s: rx DMA sbus error ack\n", device_xname(self));
863 		ifp->if_ierrors++;
864 		r |= 1;
865 		rst = 1;
866 	}
867 
868 	if (r == 0)
869 		aprint_error_dev(self, "unexpected interrupt error: %08x\n",
870 			why);
871 
872 	if (rst) {
873 		printf("%s: resetting...\n", device_xname(self));
874 		qereset(sc);
875 		return (-1);
876 	}
877 
878 	return (r);
879 }
880 
881 int
882 qeioctl(struct ifnet *ifp, u_long cmd, void *data)
883 {
884 	struct qe_softc *sc = ifp->if_softc;
885 	struct ifaddr *ifa = data;
886 	struct ifreq *ifr = data;
887 	int s, error = 0;
888 
889 	s = splnet();
890 
891 	switch (cmd) {
892 	case SIOCINITIFADDR:
893 		ifp->if_flags |= IFF_UP;
894 		qeinit(sc);
895 		switch (ifa->ifa_addr->sa_family) {
896 #ifdef INET
897 		case AF_INET:
898 			arp_ifinit(ifp, ifa);
899 			break;
900 #endif /* INET */
901 		default:
902 			break;
903 		}
904 		break;
905 
906 	case SIOCSIFFLAGS:
907 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
908 			break;
909 		/* XXX re-use ether_ioctl() */
910 		switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
911 		case IFF_RUNNING:
912 			/*
913 			 * If interface is marked down and it is running, then
914 			 * stop it.
915 			 */
916 			qestop(sc);
917 			ifp->if_flags &= ~IFF_RUNNING;
918 			break;
919 		case IFF_UP:
920 			/*
921 			 * If interface is marked up and it is stopped, then
922 			 * start it.
923 			 */
924 			qeinit(sc);
925 			break;
926 		default:
927 			/*
928 			 * Reset the interface to pick up changes in any other
929 			 * flags that affect hardware registers.
930 			 */
931 			qestop(sc);
932 			qeinit(sc);
933 			break;
934 		}
935 #ifdef QEDEBUG
936 		sc->sc_debug = (ifp->if_flags & IFF_DEBUG) != 0 ? 1 : 0;
937 #endif
938 		break;
939 
940 	case SIOCADDMULTI:
941 	case SIOCDELMULTI:
942 		if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) {
943 			/*
944 			 * Multicast list has changed; set the hardware filter
945 			 * accordingly.
946 			 */
947 			if (ifp->if_flags & IFF_RUNNING)
948 				qe_mcreset(sc);
949 			error = 0;
950 		}
951 		break;
952 
953 	case SIOCGIFMEDIA:
954 	case SIOCSIFMEDIA:
955 		error = ifmedia_ioctl(ifp, ifr, &sc->sc_ifmedia, cmd);
956 		break;
957 
958 	default:
959 		error = EINVAL;
960 		break;
961 	}
962 
963 	splx(s);
964 	return (error);
965 }
966 
967 
968 void
969 qeinit(struct qe_softc *sc)
970 {
971 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
972 	bus_space_tag_t t = sc->sc_bustag;
973 	bus_space_handle_t cr = sc->sc_cr;
974 	bus_space_handle_t mr = sc->sc_mr;
975 	struct qec_softc *qec = sc->sc_qec;
976 	uint32_t qecaddr;
977 	uint8_t *ea;
978 	int s;
979 
980 #if defined(SUN4U) || defined(__GNUC__)
981 	(void)&t;
982 #endif
983 	s = splnet();
984 
985 	qestop(sc);
986 
987 	/*
988 	 * Allocate descriptor ring and buffers
989 	 */
990 	qec_meminit(&sc->sc_rb, QE_PKT_BUF_SZ);
991 
992 	/* Channel registers: */
993 	bus_space_write_4(t, cr, QE_CRI_RXDS, (uint32_t)sc->sc_rb.rb_rxddma);
994 	bus_space_write_4(t, cr, QE_CRI_TXDS, (uint32_t)sc->sc_rb.rb_txddma);
995 
996 	bus_space_write_4(t, cr, QE_CRI_RIMASK, 0);
997 	bus_space_write_4(t, cr, QE_CRI_TIMASK, 0);
998 	bus_space_write_4(t, cr, QE_CRI_QMASK, 0);
999 	bus_space_write_4(t, cr, QE_CRI_MMASK, QE_CR_MMASK_RXCOLL);
1000 	bus_space_write_4(t, cr, QE_CRI_CCNT, 0);
1001 	bus_space_write_4(t, cr, QE_CRI_PIPG, 0);
1002 
1003 	qecaddr = sc->sc_channel * qec->sc_msize;
1004 	bus_space_write_4(t, cr, QE_CRI_RXWBUF, qecaddr);
1005 	bus_space_write_4(t, cr, QE_CRI_RXRBUF, qecaddr);
1006 	bus_space_write_4(t, cr, QE_CRI_TXWBUF, qecaddr + qec->sc_rsize);
1007 	bus_space_write_4(t, cr, QE_CRI_TXRBUF, qecaddr + qec->sc_rsize);
1008 
1009 	/* MACE registers: */
1010 	bus_space_write_1(t, mr, QE_MRI_PHYCC, QE_MR_PHYCC_ASEL);
1011 	bus_space_write_1(t, mr, QE_MRI_XMTFC, QE_MR_XMTFC_APADXMT);
1012 	bus_space_write_1(t, mr, QE_MRI_RCVFC, 0);
1013 
1014 	/*
1015 	 * Mask MACE's receive interrupt, since we're being notified
1016 	 * by the QEC after DMA completes.
1017 	 */
1018 	bus_space_write_1(t, mr, QE_MRI_IMR,
1019 			  QE_MR_IMR_CERRM | QE_MR_IMR_RCVINTM);
1020 
1021 	bus_space_write_1(t, mr, QE_MRI_BIUCC,
1022 			  QE_MR_BIUCC_BSWAP | QE_MR_BIUCC_64TS);
1023 
1024 	bus_space_write_1(t, mr, QE_MRI_FIFOFC,
1025 			  QE_MR_FIFOCC_TXF16 | QE_MR_FIFOCC_RXF32 |
1026 			  QE_MR_FIFOCC_RFWU | QE_MR_FIFOCC_TFWU);
1027 
1028 	bus_space_write_1(t, mr, QE_MRI_PLSCC, QE_MR_PLSCC_TP);
1029 
1030 	/*
1031 	 * Station address
1032 	 */
1033 	ea = sc->sc_enaddr;
1034 	bus_space_write_1(t, mr, QE_MRI_IAC,
1035 			  QE_MR_IAC_ADDRCHG | QE_MR_IAC_PHYADDR);
1036 	bus_space_write_multi_1(t, mr, QE_MRI_PADR, ea, 6);
1037 
1038 	/* Apply media settings */
1039 	qe_ifmedia_upd(ifp);
1040 
1041 	/*
1042 	 * Clear Logical address filter
1043 	 */
1044 	bus_space_write_1(t, mr, QE_MRI_IAC,
1045 			  QE_MR_IAC_ADDRCHG | QE_MR_IAC_LOGADDR);
1046 	bus_space_set_multi_1(t, mr, QE_MRI_LADRF, 0, 8);
1047 	bus_space_write_1(t, mr, QE_MRI_IAC, 0);
1048 
1049 	/* Clear missed packet count (register cleared on read) */
1050 	(void)bus_space_read_1(t, mr, QE_MRI_MPC);
1051 
1052 #if 0
1053 	/* test register: */
1054 	bus_space_write_1(t, mr, QE_MRI_UTR, 0);
1055 #endif
1056 
1057 	/* Reset multicast filter */
1058 	qe_mcreset(sc);
1059 
1060 	ifp->if_flags |= IFF_RUNNING;
1061 	ifp->if_flags &= ~IFF_OACTIVE;
1062 	splx(s);
1063 }
1064 
1065 /*
1066  * Reset multicast filter.
1067  */
1068 void
1069 qe_mcreset(struct qe_softc *sc)
1070 {
1071 	struct ethercom *ec = &sc->sc_ethercom;
1072 	struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1073 	bus_space_tag_t t = sc->sc_bustag;
1074 	bus_space_handle_t mr = sc->sc_mr;
1075 	struct ether_multi *enm;
1076 	struct ether_multistep step;
1077 	uint32_t crc;
1078 	uint16_t hash[4];
1079 	uint8_t octet, maccc, *ladrp = (uint8_t *)&hash[0];
1080 	int i, j;
1081 
1082 #if defined(SUN4U) || defined(__GNUC__)
1083 	(void)&t;
1084 #endif
1085 
1086 	/* We also enable transmitter & receiver here */
1087 	maccc = QE_MR_MACCC_ENXMT | QE_MR_MACCC_ENRCV;
1088 
1089 	if (ifp->if_flags & IFF_PROMISC) {
1090 		maccc |= QE_MR_MACCC_PROM;
1091 		bus_space_write_1(t, mr, QE_MRI_MACCC, maccc);
1092 		return;
1093 	}
1094 
1095 	if (ifp->if_flags & IFF_ALLMULTI) {
1096 		bus_space_write_1(t, mr, QE_MRI_IAC,
1097 				  QE_MR_IAC_ADDRCHG | QE_MR_IAC_LOGADDR);
1098 		bus_space_set_multi_1(t, mr, QE_MRI_LADRF, 0xff, 8);
1099 		bus_space_write_1(t, mr, QE_MRI_IAC, 0);
1100 		bus_space_write_1(t, mr, QE_MRI_MACCC, maccc);
1101 		return;
1102 	}
1103 
1104 	hash[3] = hash[2] = hash[1] = hash[0] = 0;
1105 
1106 	ETHER_FIRST_MULTI(step, ec, enm);
1107 	while (enm != NULL) {
1108 		if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
1109 			 ETHER_ADDR_LEN) != 0) {
1110 			/*
1111 			 * We must listen to a range of multicast
1112 			 * addresses. For now, just accept all
1113 			 * multicasts, rather than trying to set only
1114 			 * those filter bits needed to match the range.
1115 			 * (At this time, the only use of address
1116 			 * ranges is for IP multicast routing, for
1117 			 * which the range is big enough to require
1118 			 * all bits set.)
1119 			 */
1120 			bus_space_write_1(t, mr, QE_MRI_IAC,
1121 				 QE_MR_IAC_ADDRCHG | QE_MR_IAC_LOGADDR);
1122 			bus_space_set_multi_1(t, mr, QE_MRI_LADRF, 0xff, 8);
1123 			bus_space_write_1(t, mr, QE_MRI_IAC, 0);
1124 			ifp->if_flags |= IFF_ALLMULTI;
1125 			break;
1126 		}
1127 
1128 		crc = 0xffffffff;
1129 
1130 		for (i = 0; i < ETHER_ADDR_LEN; i++) {
1131 			octet = enm->enm_addrlo[i];
1132 
1133 			for (j = 0; j < 8; j++) {
1134 				if ((crc & 1) ^ (octet & 1)) {
1135 					crc >>= 1;
1136 					crc ^= MC_POLY_LE;
1137 				}
1138 				else
1139 					crc >>= 1;
1140 				octet >>= 1;
1141 			}
1142 		}
1143 
1144 		crc >>= 26;
1145 		hash[crc >> 4] |= 1 << (crc & 0xf);
1146 		ETHER_NEXT_MULTI(step, enm);
1147 	}
1148 
1149 	bus_space_write_1(t, mr, QE_MRI_IAC,
1150 			  QE_MR_IAC_ADDRCHG | QE_MR_IAC_LOGADDR);
1151 	bus_space_write_multi_1(t, mr, QE_MRI_LADRF, ladrp, 8);
1152 	bus_space_write_1(t, mr, QE_MRI_IAC, 0);
1153 	bus_space_write_1(t, mr, QE_MRI_MACCC, maccc);
1154 }
1155 
1156 /*
1157  * Get current media settings.
1158  */
1159 void
1160 qe_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1161 {
1162 	struct qe_softc *sc = ifp->if_softc;
1163 	bus_space_tag_t t = sc->sc_bustag;
1164 	bus_space_handle_t mr = sc->sc_mr;
1165 	uint8_t v;
1166 
1167 #if defined(SUN4U) || defined(__GNUC__)
1168 	(void)&t;
1169 #endif
1170 	v = bus_space_read_1(t, mr, QE_MRI_PLSCC);
1171 
1172 	switch (bus_space_read_1(t, mr, QE_MRI_PLSCC) & QE_MR_PLSCC_PORTMASK) {
1173 	case QE_MR_PLSCC_TP:
1174 		ifmr->ifm_active = IFM_ETHER | IFM_10_T;
1175 		break;
1176 	case QE_MR_PLSCC_AUI:
1177 		ifmr->ifm_active = IFM_ETHER | IFM_10_5;
1178 		break;
1179 	case QE_MR_PLSCC_GPSI:
1180 	case QE_MR_PLSCC_DAI:
1181 		/* ... */
1182 		break;
1183 	}
1184 
1185 	v = bus_space_read_1(t, mr, QE_MRI_PHYCC);
1186 	ifmr->ifm_status |=  IFM_AVALID;
1187 	if ((v & QE_MR_PHYCC_LNKFL) != 0)
1188 		ifmr->ifm_status &= ~IFM_ACTIVE;
1189 	else
1190 		ifmr->ifm_status |=  IFM_ACTIVE;
1191 
1192 }
1193 
1194 /*
1195  * Set media options.
1196  */
1197 int
1198 qe_ifmedia_upd(struct ifnet *ifp)
1199 {
1200 	struct qe_softc *sc = ifp->if_softc;
1201 	struct ifmedia *ifm = &sc->sc_ifmedia;
1202 	bus_space_tag_t t = sc->sc_bustag;
1203 	bus_space_handle_t mr = sc->sc_mr;
1204 	int newmedia = ifm->ifm_media;
1205 	uint8_t plscc, phycc;
1206 
1207 #if defined(SUN4U) || defined(__GNUC__)
1208 	(void)&t;
1209 #endif
1210 	if (IFM_TYPE(newmedia) != IFM_ETHER)
1211 		return (EINVAL);
1212 
1213 	plscc = bus_space_read_1(t, mr, QE_MRI_PLSCC) & ~QE_MR_PLSCC_PORTMASK;
1214 	phycc = bus_space_read_1(t, mr, QE_MRI_PHYCC) & ~QE_MR_PHYCC_ASEL;
1215 
1216 	if (IFM_SUBTYPE(newmedia) == IFM_AUTO)
1217 		phycc |= QE_MR_PHYCC_ASEL;
1218 	else if (IFM_SUBTYPE(newmedia) == IFM_10_T)
1219 		plscc |= QE_MR_PLSCC_TP;
1220 	else if (IFM_SUBTYPE(newmedia) == IFM_10_5)
1221 		plscc |= QE_MR_PLSCC_AUI;
1222 
1223 	bus_space_write_1(t, mr, QE_MRI_PLSCC, plscc);
1224 	bus_space_write_1(t, mr, QE_MRI_PHYCC, phycc);
1225 
1226 	return (0);
1227 }
1228