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