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