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