qe.c revision 1.41 1 /* $NetBSD: qe.c,v 1.41 2007/03/04 07:54:11 christos 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.41 2007/03/04 07:54:11 christos 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 <machine/bus.h>
121 #include <machine/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 error = (cmd == SIOCADDMULTI) ?
981 ether_addmulti(ifr, &sc->sc_ethercom):
982 ether_delmulti(ifr, &sc->sc_ethercom);
983
984 if (error == ENETRESET) {
985 /*
986 * Multicast list has changed; set the hardware filter
987 * accordingly.
988 */
989 if (ifp->if_flags & IFF_RUNNING)
990 qe_mcreset(sc);
991 error = 0;
992 }
993 break;
994
995 case SIOCGIFMEDIA:
996 case SIOCSIFMEDIA:
997 error = ifmedia_ioctl(ifp, ifr, &sc->sc_ifmedia, cmd);
998 break;
999
1000 default:
1001 error = EINVAL;
1002 break;
1003 }
1004
1005 splx(s);
1006 return (error);
1007 }
1008
1009
1010 void
1011 qeinit(sc)
1012 struct qe_softc *sc;
1013 {
1014 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1015 bus_space_tag_t t = sc->sc_bustag;
1016 bus_space_handle_t cr = sc->sc_cr;
1017 bus_space_handle_t mr = sc->sc_mr;
1018 struct qec_softc *qec = sc->sc_qec;
1019 u_int32_t qecaddr;
1020 u_int8_t *ea;
1021 int s;
1022
1023 #if defined(SUN4U) || defined(__GNUC__)
1024 (void)&t;
1025 #endif
1026 s = splnet();
1027
1028 qestop(sc);
1029
1030 /*
1031 * Allocate descriptor ring and buffers
1032 */
1033 qec_meminit(&sc->sc_rb, QE_PKT_BUF_SZ);
1034
1035 /* Channel registers: */
1036 bus_space_write_4(t, cr, QE_CRI_RXDS, (u_int32_t)sc->sc_rb.rb_rxddma);
1037 bus_space_write_4(t, cr, QE_CRI_TXDS, (u_int32_t)sc->sc_rb.rb_txddma);
1038
1039 bus_space_write_4(t, cr, QE_CRI_RIMASK, 0);
1040 bus_space_write_4(t, cr, QE_CRI_TIMASK, 0);
1041 bus_space_write_4(t, cr, QE_CRI_QMASK, 0);
1042 bus_space_write_4(t, cr, QE_CRI_MMASK, QE_CR_MMASK_RXCOLL);
1043 bus_space_write_4(t, cr, QE_CRI_CCNT, 0);
1044 bus_space_write_4(t, cr, QE_CRI_PIPG, 0);
1045
1046 qecaddr = sc->sc_channel * qec->sc_msize;
1047 bus_space_write_4(t, cr, QE_CRI_RXWBUF, qecaddr);
1048 bus_space_write_4(t, cr, QE_CRI_RXRBUF, qecaddr);
1049 bus_space_write_4(t, cr, QE_CRI_TXWBUF, qecaddr + qec->sc_rsize);
1050 bus_space_write_4(t, cr, QE_CRI_TXRBUF, qecaddr + qec->sc_rsize);
1051
1052 /* MACE registers: */
1053 bus_space_write_1(t, mr, QE_MRI_PHYCC, QE_MR_PHYCC_ASEL);
1054 bus_space_write_1(t, mr, QE_MRI_XMTFC, QE_MR_XMTFC_APADXMT);
1055 bus_space_write_1(t, mr, QE_MRI_RCVFC, 0);
1056
1057 /*
1058 * Mask MACE's receive interrupt, since we're being notified
1059 * by the QEC after DMA completes.
1060 */
1061 bus_space_write_1(t, mr, QE_MRI_IMR,
1062 QE_MR_IMR_CERRM | QE_MR_IMR_RCVINTM);
1063
1064 bus_space_write_1(t, mr, QE_MRI_BIUCC,
1065 QE_MR_BIUCC_BSWAP | QE_MR_BIUCC_64TS);
1066
1067 bus_space_write_1(t, mr, QE_MRI_FIFOFC,
1068 QE_MR_FIFOCC_TXF16 | QE_MR_FIFOCC_RXF32 |
1069 QE_MR_FIFOCC_RFWU | QE_MR_FIFOCC_TFWU);
1070
1071 bus_space_write_1(t, mr, QE_MRI_PLSCC, QE_MR_PLSCC_TP);
1072
1073 /*
1074 * Station address
1075 */
1076 ea = sc->sc_enaddr;
1077 bus_space_write_1(t, mr, QE_MRI_IAC,
1078 QE_MR_IAC_ADDRCHG | QE_MR_IAC_PHYADDR);
1079 bus_space_write_multi_1(t, mr, QE_MRI_PADR, ea, 6);
1080
1081 /* Apply media settings */
1082 qe_ifmedia_upd(ifp);
1083
1084 /*
1085 * Clear Logical address filter
1086 */
1087 bus_space_write_1(t, mr, QE_MRI_IAC,
1088 QE_MR_IAC_ADDRCHG | QE_MR_IAC_LOGADDR);
1089 bus_space_set_multi_1(t, mr, QE_MRI_LADRF, 0, 8);
1090 bus_space_write_1(t, mr, QE_MRI_IAC, 0);
1091
1092 /* Clear missed packet count (register cleared on read) */
1093 (void)bus_space_read_1(t, mr, QE_MRI_MPC);
1094
1095 #if 0
1096 /* test register: */
1097 bus_space_write_1(t, mr, QE_MRI_UTR, 0);
1098 #endif
1099
1100 /* Reset multicast filter */
1101 qe_mcreset(sc);
1102
1103 ifp->if_flags |= IFF_RUNNING;
1104 ifp->if_flags &= ~IFF_OACTIVE;
1105 splx(s);
1106 }
1107
1108 /*
1109 * Reset multicast filter.
1110 */
1111 void
1112 qe_mcreset(sc)
1113 struct qe_softc *sc;
1114 {
1115 struct ethercom *ec = &sc->sc_ethercom;
1116 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1117 bus_space_tag_t t = sc->sc_bustag;
1118 bus_space_handle_t mr = sc->sc_mr;
1119 struct ether_multi *enm;
1120 struct ether_multistep step;
1121 u_int32_t crc;
1122 u_int16_t hash[4];
1123 u_int8_t octet, maccc, *ladrp = (u_int8_t *)&hash[0];
1124 int i, j;
1125
1126 #if defined(SUN4U) || defined(__GNUC__)
1127 (void)&t;
1128 #endif
1129
1130 /* We also enable transmitter & receiver here */
1131 maccc = QE_MR_MACCC_ENXMT | QE_MR_MACCC_ENRCV;
1132
1133 if (ifp->if_flags & IFF_PROMISC) {
1134 maccc |= QE_MR_MACCC_PROM;
1135 bus_space_write_1(t, mr, QE_MRI_MACCC, maccc);
1136 return;
1137 }
1138
1139 if (ifp->if_flags & IFF_ALLMULTI) {
1140 bus_space_write_1(t, mr, QE_MRI_IAC,
1141 QE_MR_IAC_ADDRCHG | QE_MR_IAC_LOGADDR);
1142 bus_space_set_multi_1(t, mr, QE_MRI_LADRF, 0xff, 8);
1143 bus_space_write_1(t, mr, QE_MRI_IAC, 0);
1144 bus_space_write_1(t, mr, QE_MRI_MACCC, maccc);
1145 return;
1146 }
1147
1148 hash[3] = hash[2] = hash[1] = hash[0] = 0;
1149
1150 ETHER_FIRST_MULTI(step, ec, enm);
1151 while (enm != NULL) {
1152 if (memcmp(enm->enm_addrlo, enm->enm_addrhi,
1153 ETHER_ADDR_LEN) != 0) {
1154 /*
1155 * We must listen to a range of multicast
1156 * addresses. For now, just accept all
1157 * multicasts, rather than trying to set only
1158 * those filter bits needed to match the range.
1159 * (At this time, the only use of address
1160 * ranges is for IP multicast routing, for
1161 * which the range is big enough to require
1162 * all bits set.)
1163 */
1164 bus_space_write_1(t, mr, QE_MRI_IAC,
1165 QE_MR_IAC_ADDRCHG | QE_MR_IAC_LOGADDR);
1166 bus_space_set_multi_1(t, mr, QE_MRI_LADRF, 0xff, 8);
1167 bus_space_write_1(t, mr, QE_MRI_IAC, 0);
1168 ifp->if_flags |= IFF_ALLMULTI;
1169 break;
1170 }
1171
1172 crc = 0xffffffff;
1173
1174 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1175 octet = enm->enm_addrlo[i];
1176
1177 for (j = 0; j < 8; j++) {
1178 if ((crc & 1) ^ (octet & 1)) {
1179 crc >>= 1;
1180 crc ^= MC_POLY_LE;
1181 }
1182 else
1183 crc >>= 1;
1184 octet >>= 1;
1185 }
1186 }
1187
1188 crc >>= 26;
1189 hash[crc >> 4] |= 1 << (crc & 0xf);
1190 ETHER_NEXT_MULTI(step, enm);
1191 }
1192
1193 bus_space_write_1(t, mr, QE_MRI_IAC,
1194 QE_MR_IAC_ADDRCHG | QE_MR_IAC_LOGADDR);
1195 bus_space_write_multi_1(t, mr, QE_MRI_LADRF, ladrp, 8);
1196 bus_space_write_1(t, mr, QE_MRI_IAC, 0);
1197 bus_space_write_1(t, mr, QE_MRI_MACCC, maccc);
1198 }
1199
1200 /*
1201 * Get current media settings.
1202 */
1203 void
1204 qe_ifmedia_sts(ifp, ifmr)
1205 struct ifnet *ifp;
1206 struct ifmediareq *ifmr;
1207 {
1208 struct qe_softc *sc = ifp->if_softc;
1209 bus_space_tag_t t = sc->sc_bustag;
1210 bus_space_handle_t mr = sc->sc_mr;
1211 u_int8_t v;
1212
1213 #if defined(SUN4U) || defined(__GNUC__)
1214 (void)&t;
1215 #endif
1216 v = bus_space_read_1(t, mr, QE_MRI_PLSCC);
1217
1218 switch (bus_space_read_1(t, mr, QE_MRI_PLSCC) & QE_MR_PLSCC_PORTMASK) {
1219 case QE_MR_PLSCC_TP:
1220 ifmr->ifm_active = IFM_ETHER | IFM_10_T;
1221 break;
1222 case QE_MR_PLSCC_AUI:
1223 ifmr->ifm_active = IFM_ETHER | IFM_10_5;
1224 break;
1225 case QE_MR_PLSCC_GPSI:
1226 case QE_MR_PLSCC_DAI:
1227 /* ... */
1228 break;
1229 }
1230
1231 v = bus_space_read_1(t, mr, QE_MRI_PHYCC);
1232 ifmr->ifm_status |= IFM_AVALID;
1233 if ((v & QE_MR_PHYCC_LNKFL) != 0)
1234 ifmr->ifm_status &= ~IFM_ACTIVE;
1235 else
1236 ifmr->ifm_status |= IFM_ACTIVE;
1237
1238 }
1239
1240 /*
1241 * Set media options.
1242 */
1243 int
1244 qe_ifmedia_upd(ifp)
1245 struct ifnet *ifp;
1246 {
1247 struct qe_softc *sc = ifp->if_softc;
1248 struct ifmedia *ifm = &sc->sc_ifmedia;
1249 bus_space_tag_t t = sc->sc_bustag;
1250 bus_space_handle_t mr = sc->sc_mr;
1251 int newmedia = ifm->ifm_media;
1252 u_int8_t plscc, phycc;
1253
1254 #if defined(SUN4U) || defined(__GNUC__)
1255 (void)&t;
1256 #endif
1257 if (IFM_TYPE(newmedia) != IFM_ETHER)
1258 return (EINVAL);
1259
1260 plscc = bus_space_read_1(t, mr, QE_MRI_PLSCC) & ~QE_MR_PLSCC_PORTMASK;
1261 phycc = bus_space_read_1(t, mr, QE_MRI_PHYCC) & ~QE_MR_PHYCC_ASEL;
1262
1263 if (IFM_SUBTYPE(newmedia) == IFM_AUTO)
1264 phycc |= QE_MR_PHYCC_ASEL;
1265 else if (IFM_SUBTYPE(newmedia) == IFM_10_T)
1266 plscc |= QE_MR_PLSCC_TP;
1267 else if (IFM_SUBTYPE(newmedia) == IFM_10_5)
1268 plscc |= QE_MR_PLSCC_AUI;
1269
1270 bus_space_write_1(t, mr, QE_MRI_PLSCC, plscc);
1271 bus_space_write_1(t, mr, QE_MRI_PHYCC, phycc);
1272
1273 return (0);
1274 }
1275