qe.c revision 1.5 1 /* $NetBSD: qe.c,v 1.5 1999/03/23 00:27:09 pk 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 ether_header *eh;
407 struct mbuf *m;
408
409 if (len <= sizeof(struct ether_header) ||
410 len > ETHERMTU + sizeof(struct ether_header)) {
411
412 printf("%s: invalid packet size %d; dropping\n",
413 ifp->if_xname, len);
414
415 ifp->if_ierrors++;
416 return;
417 }
418
419 /*
420 * Pull packet off interface.
421 */
422 m = qe_get(sc, idx, len);
423 if (m == NULL) {
424 ifp->if_ierrors++;
425 return;
426 }
427 ifp->if_ipackets++;
428
429 /* We assume that the header fit entirely in one mbuf. */
430 eh = mtod(m, struct ether_header *);
431
432 #if NBPFILTER > 0
433 /*
434 * Check if there's a BPF listener on this interface.
435 * If so, hand off the raw packet to BPF.
436 */
437 if (ifp->if_bpf)
438 bpf_mtap(ifp->if_bpf, m);
439 #endif
440 /* Pass the packet up, with the ether header sort-of removed. */
441 m_adj(m, sizeof(struct ether_header));
442 ether_input(ifp, eh, m);
443 }
444
445 /*
446 * Start output on interface.
447 * We make two assumptions here:
448 * 1) that the current priority is set to splnet _before_ this code
449 * is called *and* is returned to the appropriate priority after
450 * return
451 * 2) that the IFF_OACTIVE flag is checked before this code is called
452 * (i.e. that the output part of the interface is idle)
453 */
454 void
455 qestart(ifp)
456 struct ifnet *ifp;
457 {
458 struct qe_softc *sc = (struct qe_softc *)ifp->if_softc;
459 struct qec_xd *txd = sc->sc_rb.rb_txd;
460 struct mbuf *m;
461 unsigned int bix, len;
462 unsigned int ntbuf = sc->sc_rb.rb_ntbuf;
463
464 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
465 return;
466
467 bix = sc->sc_rb.rb_tdhead;
468
469 for (;;) {
470 IF_DEQUEUE(&ifp->if_snd, m);
471 if (m == 0)
472 break;
473
474 #if NBPFILTER > 0
475 /*
476 * If BPF is listening on this interface, let it see the
477 * packet before we commit it to the wire.
478 */
479 if (ifp->if_bpf)
480 bpf_mtap(ifp->if_bpf, m);
481 #endif
482
483 /*
484 * Copy the mbuf chain into the transmit buffer.
485 */
486 len = qe_put(sc, bix, m);
487
488 /*
489 * Initialize transmit registers and start transmission
490 */
491 txd[bix].xd_flags = QEC_XD_OWN | QEC_XD_SOP | QEC_XD_EOP |
492 (len & QEC_XD_LENGTH);
493 bus_space_write_4(sc->sc_bustag, sc->sc_cr, QE_CRI_CTRL,
494 QE_CR_CTRL_TWAKEUP);
495
496 if (++bix == QEC_XD_RING_MAXSIZE)
497 bix = 0;
498
499 if (++sc->sc_rb.rb_td_nbusy == ntbuf) {
500 ifp->if_flags |= IFF_OACTIVE;
501 break;
502 }
503 }
504
505 sc->sc_rb.rb_tdhead = bix;
506 }
507
508 void
509 qestop(sc)
510 struct qe_softc *sc;
511 {
512 bus_space_tag_t t = sc->sc_bustag;
513 bus_space_handle_t mr = sc->sc_mr;
514 bus_space_handle_t cr = sc->sc_cr;
515 int n;
516
517 #if defined(SUN4U) || defined(__GNUC__)
518 (void)&t;
519 #endif
520 /* Stop the schwurst */
521 bus_space_write_1(t, mr, QE_MRI_BIUCC, QE_MR_BIUCC_SWRST);
522 for (n = 200; n > 0; n--) {
523 if ((bus_space_read_1(t, mr, QE_MRI_BIUCC) &
524 QE_MR_BIUCC_SWRST) == 0)
525 break;
526 DELAY(20);
527 }
528
529 /* then reset */
530 bus_space_write_4(t, cr, QE_CRI_CTRL, QE_CR_CTRL_RESET);
531 for (n = 200; n > 0; n--) {
532 if ((bus_space_read_4(t, cr, QE_CRI_CTRL) &
533 QE_CR_CTRL_RESET) == 0)
534 break;
535 DELAY(20);
536 }
537 }
538
539 /*
540 * Reset interface.
541 */
542 void
543 qereset(sc)
544 struct qe_softc *sc;
545 {
546 int s;
547
548 s = splnet();
549 qestop(sc);
550 qeinit(sc);
551 splx(s);
552 }
553
554 void
555 qewatchdog(ifp)
556 struct ifnet *ifp;
557 {
558 struct qe_softc *sc = ifp->if_softc;
559
560 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
561 ++sc->sc_ethercom.ec_if.if_oerrors;
562
563 qereset(sc);
564 }
565
566 /*
567 * Interrupt dispatch.
568 */
569 int
570 qeintr(arg)
571 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
584 /* Filter out status for this channel */
585 qecstat = qecstat >> (4 * sc->sc_channel);
586 if ((qecstat & 0xf) == 0)
587 return (r);
588
589 qestat = bus_space_read_4(t, sc->sc_cr, QE_CRI_STAT);
590
591 if (qestat & QE_CR_STAT_ALLERRORS) {
592 r |= qe_eint(sc, qestat);
593 if (r == -1)
594 return (1);
595 }
596
597 if (qestat & QE_CR_STAT_TXIRQ)
598 r |= qe_tint(sc);
599
600 if (qestat & QE_CR_STAT_RXIRQ)
601 r |= qe_rint(sc);
602
603 return (r);
604 }
605
606 /*
607 * Transmit interrupt.
608 */
609 int
610 qe_tint(sc)
611 struct qe_softc *sc;
612 {
613 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
614 unsigned int bix, txflags;
615
616 bix = sc->sc_rb.rb_tdtail;
617
618 for (;;) {
619 if (sc->sc_rb.rb_td_nbusy <= 0)
620 break;
621
622 txflags = sc->sc_rb.rb_txd[bix].xd_flags;
623
624 if (txflags & QEC_XD_OWN)
625 break;
626
627 ifp->if_flags &= ~IFF_OACTIVE;
628 ifp->if_opackets++;
629
630 if (++bix == QEC_XD_RING_MAXSIZE)
631 bix = 0;
632
633 --sc->sc_rb.rb_td_nbusy;
634 }
635
636 sc->sc_rb.rb_tdtail = bix;
637
638 qestart(ifp);
639
640 if (sc->sc_rb.rb_td_nbusy == 0)
641 ifp->if_timer = 0;
642
643 return (1);
644 }
645
646 /*
647 * Receive interrupt.
648 */
649 int
650 qe_rint(sc)
651 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
657 bix = sc->sc_rb.rb_rdtail;
658
659 /*
660 * Process all buffers with valid data.
661 */
662 for (;;) {
663 len = xd[bix].xd_flags;
664 if (len & QEC_XD_OWN)
665 break;
666
667 len &= QEC_XD_LENGTH;
668 len -= 4;
669 qe_read(sc, bix, len);
670
671 /* ... */
672 xd[(bix+nrbuf) % QEC_XD_RING_MAXSIZE].xd_flags =
673 QEC_XD_OWN | (QE_PKT_BUF_SZ & QEC_XD_LENGTH);
674
675 if (++bix == QEC_XD_RING_MAXSIZE)
676 bix = 0;
677 }
678
679 sc->sc_rb.rb_rdtail = bix;
680
681 return (1);
682 }
683
684 /*
685 * Error interrupt.
686 */
687 int
688 qe_eint(sc, why)
689 struct qe_softc *sc;
690 u_int32_t why;
691 {
692 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
693 int r = 0, rst = 0;
694
695 if (why & QE_CR_STAT_EDEFER) {
696 printf("%s: excessive tx defers.\n", sc->sc_dev.dv_xname);
697 r |= 1;
698 ifp->if_oerrors++;
699 }
700
701 if (why & QE_CR_STAT_CLOSS) {
702 printf("%s: no carrier, link down?\n", sc->sc_dev.dv_xname);
703 ifp->if_oerrors++;
704 r |= 1;
705 }
706
707 if (why & QE_CR_STAT_ERETRIES) {
708 printf("%s: excessive tx retries\n", sc->sc_dev.dv_xname);
709 ifp->if_oerrors++;
710 r |= 1;
711 rst = 1;
712 }
713
714
715 if (why & QE_CR_STAT_LCOLL) {
716 printf("%s: late tx transmission\n", sc->sc_dev.dv_xname);
717 ifp->if_oerrors++;
718 r |= 1;
719 rst = 1;
720 }
721
722 if (why & QE_CR_STAT_FUFLOW) {
723 printf("%s: tx fifo underflow\n", sc->sc_dev.dv_xname);
724 ifp->if_oerrors++;
725 r |= 1;
726 rst = 1;
727 }
728
729 if (why & QE_CR_STAT_JERROR) {
730 printf("%s: jabber seen\n", sc->sc_dev.dv_xname);
731 r |= 1;
732 }
733
734 if (why & QE_CR_STAT_BERROR) {
735 printf("%s: babble seen\n", sc->sc_dev.dv_xname);
736 r |= 1;
737 }
738
739 if (why & QE_CR_STAT_TCCOFLOW) {
740 ifp->if_collisions += 256;
741 ifp->if_oerrors += 256;
742 r |= 1;
743 }
744
745 if (why & QE_CR_STAT_TXDERROR) {
746 printf("%s: tx descriptor is bad\n", sc->sc_dev.dv_xname);
747 rst = 1;
748 r |= 1;
749 }
750
751 if (why & QE_CR_STAT_TXLERR) {
752 printf("%s: tx late error\n", sc->sc_dev.dv_xname);
753 ifp->if_oerrors++;
754 rst = 1;
755 r |= 1;
756 }
757
758 if (why & QE_CR_STAT_TXPERR) {
759 printf("%s: tx dma parity error\n", sc->sc_dev.dv_xname);
760 ifp->if_oerrors++;
761 rst = 1;
762 r |= 1;
763 }
764
765 if (why & QE_CR_STAT_TXSERR) {
766 printf("%s: tx dma sbus error ack\n", sc->sc_dev.dv_xname);
767 ifp->if_oerrors++;
768 rst = 1;
769 r |= 1;
770 }
771
772 if (why & QE_CR_STAT_RCCOFLOW) {
773 ifp->if_collisions += 256;
774 ifp->if_ierrors += 256;
775 r |= 1;
776 }
777
778 if (why & QE_CR_STAT_RUOFLOW) {
779 ifp->if_ierrors += 256;
780 r |= 1;
781 }
782
783 if (why & QE_CR_STAT_MCOFLOW) {
784 ifp->if_ierrors += 256;
785 r |= 1;
786 }
787
788 if (why & QE_CR_STAT_RXFOFLOW) {
789 printf("%s: rx fifo overflow\n", sc->sc_dev.dv_xname);
790 ifp->if_ierrors++;
791 r |= 1;
792 }
793
794 if (why & QE_CR_STAT_RLCOLL) {
795 printf("%s: rx late collision\n", sc->sc_dev.dv_xname);
796 ifp->if_ierrors++;
797 ifp->if_collisions++;
798 r |= 1;
799 }
800
801 if (why & QE_CR_STAT_FCOFLOW) {
802 ifp->if_ierrors += 256;
803 r |= 1;
804 }
805
806 if (why & QE_CR_STAT_CECOFLOW) {
807 ifp->if_ierrors += 256;
808 r |= 1;
809 }
810
811 if (why & QE_CR_STAT_RXDROP) {
812 printf("%s: rx packet dropped\n", sc->sc_dev.dv_xname);
813 ifp->if_ierrors++;
814 r |= 1;
815 }
816
817 if (why & QE_CR_STAT_RXSMALL) {
818 printf("%s: rx buffer too small\n", sc->sc_dev.dv_xname);
819 ifp->if_ierrors++;
820 r |= 1;
821 rst = 1;
822 }
823
824 if (why & QE_CR_STAT_RXLERR) {
825 printf("%s: rx late error\n", sc->sc_dev.dv_xname);
826 ifp->if_ierrors++;
827 r |= 1;
828 rst = 1;
829 }
830
831 if (why & QE_CR_STAT_RXPERR) {
832 printf("%s: rx dma parity error\n", sc->sc_dev.dv_xname);
833 ifp->if_ierrors++;
834 r |= 1;
835 rst = 1;
836 }
837
838 if (why & QE_CR_STAT_RXSERR) {
839 printf("%s: rx dma sbus error ack\n", sc->sc_dev.dv_xname);
840 ifp->if_ierrors++;
841 r |= 1;
842 rst = 1;
843 }
844
845 if (r == 0)
846 printf("%s: unexpected interrupt error: %08x\n",
847 sc->sc_dev.dv_xname, why);
848
849 if (rst) {
850 printf("%s: resetting...\n", sc->sc_dev.dv_xname);
851 qereset(sc);
852 return (-1);
853 }
854
855 return (r);
856 }
857
858 int
859 qeioctl(ifp, cmd, data)
860 struct ifnet *ifp;
861 u_long cmd;
862 caddr_t data;
863 {
864 struct qe_softc *sc = ifp->if_softc;
865 struct ifaddr *ifa = (struct ifaddr *)data;
866 struct ifreq *ifr = (struct ifreq *)data;
867 int s, error = 0;
868
869 s = splnet();
870
871 switch (cmd) {
872 case SIOCSIFADDR:
873 ifp->if_flags |= IFF_UP;
874 switch (ifa->ifa_addr->sa_family) {
875 #ifdef INET
876 case AF_INET:
877 qeinit(sc);
878 arp_ifinit(ifp, ifa);
879 break;
880 #endif /* INET */
881 #ifdef NS
882 case AF_NS:
883 {
884 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
885
886 if (ns_nullhost(*ina))
887 ina->x_host =
888 *(union ns_host *)LLADDR(ifp->if_sadl);
889 else
890 bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
891 sizeof(sc->sc_enaddr));
892 /* Set new address. */
893 qeinit(sc);
894 break;
895 }
896 #endif /* NS */
897 default:
898 qeinit(sc);
899 break;
900 }
901 break;
902
903 case SIOCSIFFLAGS:
904 if ((ifp->if_flags & IFF_UP) == 0 &&
905 (ifp->if_flags & IFF_RUNNING) != 0) {
906 /*
907 * If interface is marked down and it is running, then
908 * stop it.
909 */
910 qestop(sc);
911 ifp->if_flags &= ~IFF_RUNNING;
912
913 } else if ((ifp->if_flags & IFF_UP) != 0 &&
914 (ifp->if_flags & IFF_RUNNING) == 0) {
915 /*
916 * If interface is marked up and it is stopped, then
917 * start it.
918 */
919 qeinit(sc);
920
921 } else {
922 /*
923 * Reset the interface to pick up changes in any other
924 * flags that affect hardware registers.
925 */
926 qestop(sc);
927 qeinit(sc);
928 }
929 #ifdef QEDEBUG
930 if (ifp->if_flags & IFF_DEBUG)
931 sc->sc_debug = 1;
932 else
933 sc->sc_debug = 0;
934 #endif
935 break;
936
937 case SIOCADDMULTI:
938 case SIOCDELMULTI:
939 error = (cmd == SIOCADDMULTI) ?
940 ether_addmulti(ifr, &sc->sc_ethercom):
941 ether_delmulti(ifr, &sc->sc_ethercom);
942
943 if (error == ENETRESET) {
944 /*
945 * Multicast list has changed; set the hardware filter
946 * accordingly.
947 */
948 qe_mcreset(sc);
949 error = 0;
950 }
951 break;
952
953 case SIOCGIFMEDIA:
954 case SIOCSIFMEDIA:
955 error = ifmedia_ioctl(ifp, ifr, &sc->sc_ifmedia, cmd);
956 break;
957
958 default:
959 error = EINVAL;
960 break;
961 }
962
963 splx(s);
964 return (error);
965 }
966
967
968 void
969 qeinit(sc)
970 struct qe_softc *sc;
971 {
972 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
973 bus_space_tag_t t = sc->sc_bustag;
974 bus_space_handle_t cr = sc->sc_cr;
975 bus_space_handle_t mr = sc->sc_mr;
976 struct qec_softc *qec = sc->sc_qec;
977 u_int32_t qecaddr;
978 u_int8_t *ea;
979 int i, s;
980
981 #if defined(SUN4U) || defined(__GNUC__)
982 (void)&t;
983 #endif
984 s = splimp();
985 qestop(sc);
986
987 /*
988 * Allocate descriptor ring and buffers
989 */
990 qec_meminit(&sc->sc_rb, QE_PKT_BUF_SZ);
991
992 /* Channel registers: */
993 bus_space_write_4(t, cr, QE_CRI_RXDS, (u_int32_t)sc->sc_rb.rb_rxddma);
994 bus_space_write_4(t, cr, QE_CRI_TXDS, (u_int32_t)sc->sc_rb.rb_txddma);
995
996 bus_space_write_4(t, cr, QE_CRI_RIMASK, 0);
997 bus_space_write_4(t, cr, QE_CRI_TIMASK, 0);
998 bus_space_write_4(t, cr, QE_CRI_QMASK, 0);
999 bus_space_write_4(t, cr, QE_CRI_MMASK, QE_CR_MMASK_RXCOLL);
1000 bus_space_write_4(t, cr, QE_CRI_CCNT, 0);
1001 bus_space_write_4(t, cr, QE_CRI_PIPG, 0);
1002
1003 qecaddr = sc->sc_channel * qec->sc_msize;
1004 bus_space_write_4(t, cr, QE_CRI_RXWBUF, qecaddr);
1005 bus_space_write_4(t, cr, QE_CRI_RXRBUF, qecaddr);
1006 bus_space_write_4(t, cr, QE_CRI_TXWBUF, qecaddr + qec->sc_rsize);
1007 bus_space_write_4(t, cr, QE_CRI_TXRBUF, qecaddr + qec->sc_rsize);
1008
1009 /* MACE registers: */
1010 bus_space_write_1(t, mr, QE_MRI_PHYCC, QE_MR_PHYCC_ASEL);
1011 bus_space_write_1(t, mr, QE_MRI_XMTFC, QE_MR_XMTFC_APADXMT);
1012 bus_space_write_1(t, mr, QE_MRI_RCVFC, 0);
1013 bus_space_write_1(t, mr, QE_MRI_IMR,
1014 QE_MR_IMR_CERRM | QE_MR_IMR_RCVINTM);
1015 bus_space_write_1(t, mr, QE_MRI_BIUCC,
1016 QE_MR_BIUCC_BSWAP | QE_MR_BIUCC_64TS);
1017
1018 bus_space_write_1(t, mr, QE_MRI_FIFOFC,
1019 QE_MR_FIFOCC_TXF16 | QE_MR_FIFOCC_RXF32 |
1020 QE_MR_FIFOCC_RFWU | QE_MR_FIFOCC_TFWU);
1021
1022 bus_space_write_1(t, mr, QE_MRI_PLSCC, QE_MR_PLSCC_TP);
1023
1024 /*
1025 * Station address
1026 */
1027 ea = sc->sc_enaddr;
1028 bus_space_write_1(t, mr, QE_MRI_IAC,
1029 QE_MR_IAC_ADDRCHG | QE_MR_IAC_PHYADDR);
1030 bus_space_write_1(t, mr, QE_MRI_PADR, ea[0]);
1031 bus_space_write_1(t, mr, QE_MRI_PADR, ea[1]);
1032 bus_space_write_1(t, mr, QE_MRI_PADR, ea[2]);
1033 bus_space_write_1(t, mr, QE_MRI_PADR, ea[3]);
1034 bus_space_write_1(t, mr, QE_MRI_PADR, ea[4]);
1035 bus_space_write_1(t, mr, QE_MRI_PADR, ea[5]);
1036
1037 /* Apply media settings */
1038 qe_ifmedia_upd(ifp);
1039
1040 /*
1041 * Logical address filter
1042 */
1043 bus_space_write_1(t, mr, QE_MRI_IAC,
1044 QE_MR_IAC_ADDRCHG | QE_MR_IAC_LOGADDR);
1045 for (i = 0; i < 8; i++)
1046 bus_space_write_1(t, mr, QE_MRI_LADRF, 0);
1047 bus_space_write_1(t, mr, QE_MRI_IAC, 0);
1048
1049 /* Clear missed packet count (register cleared on read) */
1050 (void)bus_space_read_1(t, mr, QE_MRI_MPC);
1051
1052 /* Enable transmitter & receiver */
1053 bus_space_write_1(t, mr, QE_MRI_MACCC,
1054 QE_MR_MACCC_ENXMT | QE_MR_MACCC_ENRCV |
1055 ((ifp->if_flags&IFF_PROMISC) ? QE_MR_MACCC_PROM : 0));
1056
1057 /* Reset multicast filter */
1058 qe_mcreset(sc);
1059
1060 ifp->if_flags |= IFF_RUNNING;
1061 ifp->if_flags &= ~IFF_OACTIVE;
1062 splx(s);
1063 }
1064
1065 /*
1066 * Reset multicast filter.
1067 */
1068 void
1069 qe_mcreset(sc)
1070 struct qe_softc *sc;
1071 {
1072 struct ethercom *ec = &sc->sc_ethercom;
1073 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1074 bus_space_tag_t t = sc->sc_bustag;
1075 bus_space_handle_t mr = sc->sc_mr;
1076 struct ether_multi *enm;
1077 struct ether_multistep step;
1078 u_int32_t crc;
1079 u_int16_t hash[4];
1080 u_int8_t octet, maccc, *ladrp = (u_int8_t *)&hash[0];
1081 int i, j;
1082
1083 #if defined(SUN4U) || defined(__GNUC__)
1084 (void)&t;
1085 #endif
1086
1087 maccc = QE_MR_MACCC_ENXMT | QE_MR_MACCC_ENRCV;
1088
1089 if (ifp->if_flags & IFF_PROMISC) {
1090 maccc |= QE_MR_MACCC_PROM;
1091 bus_space_write_1(t, mr, QE_MRI_MACCC, maccc);
1092 return;
1093 }
1094
1095 if (ifp->if_flags & IFF_ALLMULTI) {
1096 bus_space_write_1(t, mr, QE_MRI_IAC,
1097 QE_MR_IAC_ADDRCHG | QE_MR_IAC_LOGADDR);
1098 for (i = 0; i < 8; i++)
1099 bus_space_write_1(t, mr, QE_MRI_LADRF, 0xff);
1100 bus_space_write_1(t, mr, QE_MRI_IAC, 0);
1101 bus_space_write_1(t, mr, QE_MRI_MACCC, maccc);
1102 return;
1103 }
1104
1105 hash[3] = hash[2] = hash[1] = hash[0] = 0;
1106
1107 ETHER_FIRST_MULTI(step, ec, enm);
1108 while (enm != NULL) {
1109 if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
1110 ETHER_ADDR_LEN) != 0) {
1111 /*
1112 * We must listen to a range of multicast
1113 * addresses. For now, just accept all
1114 * multicasts, rather than trying to set only
1115 * those filter bits needed to match the range.
1116 * (At this time, the only use of address
1117 * ranges is for IP multicast routing, for
1118 * which the range is big enough to require
1119 * all bits set.)
1120 */
1121 bus_space_write_1(t, mr, QE_MRI_IAC,
1122 QE_MR_IAC_ADDRCHG | QE_MR_IAC_LOGADDR);
1123 for (i = 0; i < 8; i++)
1124 bus_space_write_1(t, mr, QE_MRI_LADRF,
1125 0xff);
1126 bus_space_write_1(t, mr, QE_MRI_IAC, 0);
1127 ifp->if_flags |= IFF_ALLMULTI;
1128 break;
1129 }
1130
1131 crc = 0xffffffff;
1132
1133 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1134 octet = enm->enm_addrlo[i];
1135
1136 for (j = 0; j < 8; j++) {
1137 if ((crc & 1) ^ (octet & 1)) {
1138 crc >>= 1;
1139 crc ^= MC_POLY_LE;
1140 }
1141 else
1142 crc >>= 1;
1143 octet >>= 1;
1144 }
1145 }
1146
1147 crc >>= 26;
1148 hash[crc >> 4] |= 1 << (crc & 0xf);
1149 ETHER_NEXT_MULTI(step, enm);
1150 }
1151
1152 bus_space_write_1(t, mr, QE_MRI_IAC,
1153 QE_MR_IAC_ADDRCHG | QE_MR_IAC_LOGADDR);
1154 for (i = 0; i < 8; i++)
1155 bus_space_write_1(t, mr, QE_MRI_LADRF, ladrp[i]);
1156 bus_space_write_1(t, mr, QE_MRI_IAC, 0);
1157
1158 bus_space_write_1(t, mr, QE_MRI_MACCC, maccc);
1159 }
1160
1161 /*
1162 * Get current media settings.
1163 */
1164 void
1165 qe_ifmedia_sts(ifp, ifmr)
1166 struct ifnet *ifp;
1167 struct ifmediareq *ifmr;
1168 {
1169 struct qe_softc *sc = ifp->if_softc;
1170 bus_space_tag_t t = sc->sc_bustag;
1171 bus_space_handle_t mr = sc->sc_mr;
1172 u_int8_t v;
1173
1174 #if defined(SUN4U) || defined(__GNUC__)
1175 (void)&t;
1176 #endif
1177 v = bus_space_read_1(t, mr, QE_MRI_PLSCC);
1178
1179 switch (bus_space_read_1(t, mr, QE_MRI_PLSCC) & QE_MR_PLSCC_PORTMASK) {
1180 case QE_MR_PLSCC_TP:
1181 ifmr->ifm_active = IFM_ETHER | IFM_10_T;
1182 break;
1183 case QE_MR_PLSCC_AUI:
1184 ifmr->ifm_active = IFM_ETHER | IFM_10_5;
1185 break;
1186 case QE_MR_PLSCC_GPSI:
1187 case QE_MR_PLSCC_DAI:
1188 /* ... */
1189 break;
1190 }
1191
1192 v = bus_space_read_1(t, mr, QE_MRI_PHYCC);
1193 ifmr->ifm_status |= IFM_AVALID;
1194 if ((v & QE_MR_PHYCC_LNKFL) != 0)
1195 ifmr->ifm_status &= ~IFM_ACTIVE;
1196 else
1197 ifmr->ifm_status |= IFM_ACTIVE;
1198
1199 }
1200
1201 /*
1202 * Set media options.
1203 */
1204 int
1205 qe_ifmedia_upd(ifp)
1206 struct ifnet *ifp;
1207 {
1208 struct qe_softc *sc = ifp->if_softc;
1209 struct ifmedia *ifm = &sc->sc_ifmedia;
1210 bus_space_tag_t t = sc->sc_bustag;
1211 bus_space_handle_t mr = sc->sc_mr;
1212 int newmedia = ifm->ifm_media;
1213 u_int8_t plscc, phycc;
1214
1215 #if defined(SUN4U) || defined(__GNUC__)
1216 (void)&t;
1217 #endif
1218 if (IFM_TYPE(newmedia) != IFM_ETHER)
1219 return (EINVAL);
1220
1221 plscc = bus_space_read_1(t, mr, QE_MRI_PLSCC) & ~QE_MR_PLSCC_PORTMASK;
1222 phycc = bus_space_read_1(t, mr, QE_MRI_PHYCC) & ~QE_MR_PHYCC_ASEL;
1223
1224 if (IFM_SUBTYPE(newmedia) == IFM_AUTO)
1225 phycc |= QE_MR_PHYCC_ASEL;
1226 else if (IFM_SUBTYPE(newmedia) == IFM_10_T)
1227 plscc |= QE_MR_PLSCC_TP;
1228 else if (IFM_SUBTYPE(newmedia) == IFM_10_5)
1229 plscc |= QE_MR_PLSCC_AUI;
1230
1231 bus_space_write_1(t, mr, QE_MRI_PLSCC, plscc);
1232 bus_space_write_1(t, mr, QE_MRI_PHYCC, phycc);
1233
1234 return (0);
1235 }
1236