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