qe.c revision 1.4 1 /* $NetBSD: qe.c,v 1.4 1999/03/09 00:42:20 mrg 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_promisc;
149 int sc_burst;
150
151 struct qec_ring sc_rb; /* Packet Ring Buffer */
152
153 /* MAC address */
154 u_int8_t sc_enaddr[6];
155 };
156
157 int qematch __P((struct device *, struct cfdata *, void *));
158 void qeattach __P((struct device *, struct device *, void *));
159
160 void qeinit __P((struct qe_softc *));
161 void qestart __P((struct ifnet *));
162 void qestop __P((struct qe_softc *));
163 void qewatchdog __P((struct ifnet *));
164 int qeioctl __P((struct ifnet *, u_long, caddr_t));
165 void qereset __P((struct qe_softc *));
166
167 int qeintr __P((void *));
168 int qe_eint __P((struct qe_softc *, u_int32_t));
169 int qe_rint __P((struct qe_softc *));
170 int qe_tint __P((struct qe_softc *));
171 void qe_mcreset __P((struct qe_softc *));
172
173 static int qe_put __P((struct qe_softc *, int, struct mbuf *));
174 static void qe_read __P((struct qe_softc *, int, int));
175 static struct mbuf *qe_get __P((struct qe_softc *, int, int));
176
177 /* ifmedia callbacks */
178 void qe_ifmedia_sts __P((struct ifnet *, struct ifmediareq *));
179 int qe_ifmedia_upd __P((struct ifnet *));
180
181 struct cfattach qe_ca = {
182 sizeof(struct qe_softc), qematch, qeattach
183 };
184
185 int
186 qematch(parent, cf, aux)
187 struct device *parent;
188 struct cfdata *cf;
189 void *aux;
190 {
191 struct sbus_attach_args *sa = aux;
192
193 return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
194 }
195
196 void
197 qeattach(parent, self, aux)
198 struct device *parent, *self;
199 void *aux;
200 {
201 struct sbus_attach_args *sa = aux;
202 struct qec_softc *qec = (struct qec_softc *)parent;
203 struct qe_softc *sc = (struct qe_softc *)self;
204 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
205 int node = sa->sa_node;
206 bus_dma_segment_t seg;
207 bus_size_t size;
208 int rseg, error;
209 extern void myetheraddr __P((u_char *));
210
211 if (sa->sa_nreg < 2) {
212 printf("%s: only %d register sets\n",
213 self->dv_xname, sa->sa_nreg);
214 return;
215 }
216
217 if (bus_space_map2(sa->sa_bustag,
218 (bus_type_t)sa->sa_reg[0].sbr_slot,
219 (bus_addr_t)sa->sa_reg[0].sbr_offset,
220 (bus_size_t)sa->sa_reg[0].sbr_size,
221 BUS_SPACE_MAP_LINEAR, 0, &sc->sc_cr) != 0) {
222 printf("%s: cannot map registers\n", self->dv_xname);
223 return;
224 }
225
226 if (bus_space_map2(sa->sa_bustag,
227 (bus_type_t)sa->sa_reg[1].sbr_slot,
228 (bus_addr_t)sa->sa_reg[1].sbr_offset,
229 (bus_size_t)sa->sa_reg[1].sbr_size,
230 BUS_SPACE_MAP_LINEAR, 0, &sc->sc_mr) != 0) {
231 printf("%s: cannot map registers\n", self->dv_xname);
232 return;
233 }
234
235 sc->sc_rev = getpropint(node, "mace-version", -1);
236 printf(" rev %x", sc->sc_rev);
237
238 sc->sc_qec = qec;
239 sc->sc_qr = qec->sc_regs;
240
241 sc->sc_channel = getpropint(node, "channel#", -1);
242 sc->sc_burst = qec->sc_burst;
243
244 qestop(sc);
245
246 /* Note: no interrupt level passed */
247 (void)bus_intr_establish(sa->sa_bustag, 0, 0, qeintr, sc);
248 myetheraddr(sc->sc_enaddr);
249
250 /*
251 * Allocate descriptor ring and buffers.
252 */
253
254 /* for now, allocate as many bufs as there are ring descriptors */
255 sc->sc_rb.rb_ntbuf = QEC_XD_RING_MAXSIZE;
256 sc->sc_rb.rb_nrbuf = QEC_XD_RING_MAXSIZE;
257
258 size = QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd) +
259 QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd) +
260 sc->sc_rb.rb_ntbuf * QE_PKT_BUF_SZ +
261 sc->sc_rb.rb_nrbuf * QE_PKT_BUF_SZ;
262 if ((error = bus_dmamem_alloc(sa->sa_dmatag, size,
263 NBPG, 0,
264 &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
265 printf("%s: DMA buffer alloc error %d\n",
266 self->dv_xname, error);
267 return;
268 }
269 sc->sc_rb.rb_dmabase = seg.ds_addr;
270
271 if ((error = bus_dmamem_map(sa->sa_dmatag, &seg, rseg, size,
272 &sc->sc_rb.rb_membase,
273 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
274 printf("%s: DMA buffer map error %d\n",
275 self->dv_xname, error);
276 bus_dmamem_free(sa->sa_dmatag, &seg, rseg);
277 return;
278 }
279
280 /* Initialize media properties */
281 ifmedia_init(&sc->sc_ifmedia, 0, qe_ifmedia_upd, qe_ifmedia_sts);
282 ifmedia_add(&sc->sc_ifmedia,
283 IFM_MAKEWORD(IFM_ETHER,IFM_10_T,0,0),
284 0, NULL);
285 ifmedia_add(&sc->sc_ifmedia,
286 IFM_MAKEWORD(IFM_ETHER,IFM_10_5,0,0),
287 0, NULL);
288 ifmedia_add(&sc->sc_ifmedia,
289 IFM_MAKEWORD(IFM_ETHER,IFM_AUTO,0,0),
290 0, NULL);
291 ifmedia_set(&sc->sc_ifmedia, IFM_ETHER|IFM_AUTO);
292
293 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
294 ifp->if_softc = sc;
295 ifp->if_start = qestart;
296 ifp->if_ioctl = qeioctl;
297 ifp->if_watchdog = qewatchdog;
298 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS |
299 IFF_MULTICAST;
300
301 /* Attach the interface. */
302 if_attach(ifp);
303 ether_ifattach(ifp, sc->sc_enaddr);
304
305 printf(" address %s\n", ether_sprintf(sc->sc_enaddr));
306
307 #if NBPFILTER > 0
308 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
309 sizeof(struct ether_header));
310 #endif
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(sc, idx, totlen)
321 struct qe_softc *sc;
322 int idx, totlen;
323 {
324 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
325 struct mbuf *m;
326 struct mbuf *top, **mp;
327 int len, pad, boff = 0;
328 caddr_t bp;
329
330 bp = sc->sc_rb.rb_rxbuf + (idx % sc->sc_rb.rb_nrbuf) * QE_PKT_BUF_SZ;
331
332 MGETHDR(m, M_DONTWAIT, MT_DATA);
333 if (m == NULL)
334 return (NULL);
335 m->m_pkthdr.rcvif = ifp;
336 m->m_pkthdr.len = totlen;
337 pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
338 m->m_data += pad;
339 len = MHLEN - pad;
340 top = NULL;
341 mp = ⊤
342
343 while (totlen > 0) {
344 if (top) {
345 MGET(m, M_DONTWAIT, MT_DATA);
346 if (m == NULL) {
347 m_freem(top);
348 return (NULL);
349 }
350 len = MLEN;
351 }
352 if (top && totlen >= MINCLSIZE) {
353 MCLGET(m, M_DONTWAIT);
354 if (m->m_flags & M_EXT)
355 len = MCLBYTES;
356 }
357 m->m_len = len = min(totlen, len);
358 bcopy(bp + boff, mtod(m, caddr_t), len);
359 boff += len;
360 totlen -= len;
361 *mp = m;
362 mp = &m->m_next;
363 }
364
365 return (top);
366 }
367
368 /*
369 * Routine to copy from mbuf chain to transmit buffer in
370 * network buffer memory.
371 */
372 __inline__ int
373 qe_put(sc, idx, m)
374 struct qe_softc *sc;
375 int idx;
376 struct mbuf *m;
377 {
378 struct mbuf *n;
379 int len, tlen = 0, boff = 0;
380 caddr_t bp;
381
382 bp = sc->sc_rb.rb_txbuf + (idx % sc->sc_rb.rb_ntbuf) * QE_PKT_BUF_SZ;
383
384 for (; m; m = n) {
385 len = m->m_len;
386 if (len == 0) {
387 MFREE(m, n);
388 continue;
389 }
390 bcopy(mtod(m, caddr_t), bp+boff, len);
391 boff += len;
392 tlen += len;
393 MFREE(m, n);
394 }
395 return (tlen);
396 }
397
398 /*
399 * Pass a packet to the higher levels.
400 */
401 __inline__ void
402 qe_read(sc, idx, len)
403 struct qe_softc *sc;
404 int idx, len;
405 {
406 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
407 struct ether_header *eh;
408 struct mbuf *m;
409
410 if (len <= sizeof(struct ether_header) ||
411 len > ETHERMTU + sizeof(struct ether_header)) {
412
413 printf("%s: invalid packet size %d; dropping\n",
414 ifp->if_xname, len);
415
416 ifp->if_ierrors++;
417 return;
418 }
419
420 /*
421 * Pull packet off interface.
422 */
423 m = qe_get(sc, idx, len);
424 if (m == NULL) {
425 ifp->if_ierrors++;
426 return;
427 }
428 ifp->if_ipackets++;
429
430 /* We assume that the header fit entirely in one mbuf. */
431 eh = mtod(m, struct ether_header *);
432
433 #if NBPFILTER > 0
434 /*
435 * Check if there's a BPF listener on this interface.
436 * If so, hand off the raw packet to BPF.
437 */
438 if (ifp->if_bpf)
439 bpf_mtap(ifp->if_bpf, m);
440 #endif
441 /* Pass the packet up, with the ether header sort-of removed. */
442 m_adj(m, sizeof(struct ether_header));
443 ether_input(ifp, eh, m);
444 }
445
446 /*
447 * Start output on interface.
448 * We make two assumptions here:
449 * 1) that the current priority is set to splnet _before_ this code
450 * is called *and* is returned to the appropriate priority after
451 * return
452 * 2) that the IFF_OACTIVE flag is checked before this code is called
453 * (i.e. that the output part of the interface is idle)
454 */
455 void
456 qestart(ifp)
457 struct ifnet *ifp;
458 {
459 struct qe_softc *sc = (struct qe_softc *)ifp->if_softc;
460 struct qec_xd *txd = sc->sc_rb.rb_txd;
461 struct mbuf *m;
462 unsigned int bix, len;
463 unsigned int ntbuf = sc->sc_rb.rb_ntbuf;
464
465 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
466 return;
467
468 bix = sc->sc_rb.rb_tdhead;
469
470 for (;;) {
471 IF_DEQUEUE(&ifp->if_snd, m);
472 if (m == 0)
473 break;
474
475 #if NBPFILTER > 0
476 /*
477 * If BPF is listening on this interface, let it see the
478 * packet before we commit it to the wire.
479 */
480 if (ifp->if_bpf)
481 bpf_mtap(ifp->if_bpf, m);
482 #endif
483
484 /*
485 * Copy the mbuf chain into the transmit buffer.
486 */
487 len = qe_put(sc, bix, m);
488
489 /*
490 * Initialize transmit registers and start transmission
491 */
492 txd[bix].xd_flags = QEC_XD_OWN | QEC_XD_SOP | QEC_XD_EOP |
493 (len & QEC_XD_LENGTH);
494 bus_space_write_4(sc->sc_bustag, sc->sc_cr, QE_CRI_CTRL,
495 QE_CR_CTRL_TWAKEUP);
496
497 if (++bix == QEC_XD_RING_MAXSIZE)
498 bix = 0;
499
500 if (++sc->sc_rb.rb_td_nbusy == ntbuf) {
501 ifp->if_flags |= IFF_OACTIVE;
502 break;
503 }
504 }
505
506 sc->sc_rb.rb_tdhead = bix;
507 }
508
509 void
510 qestop(sc)
511 struct qe_softc *sc;
512 {
513 bus_space_tag_t t = sc->sc_bustag;
514 bus_space_handle_t mr = sc->sc_mr;
515 bus_space_handle_t cr = sc->sc_cr;
516 int n;
517
518 #if defined(SUN4U) || defined(__GNUC__)
519 (void)&t;
520 #endif
521 /* Stop the schwurst */
522 bus_space_write_1(t, mr, QE_MRI_BIUCC, QE_MR_BIUCC_SWRST);
523 for (n = 200; n > 0; n--) {
524 if ((bus_space_read_1(t, mr, QE_MRI_BIUCC) &
525 QE_MR_BIUCC_SWRST) == 0)
526 break;
527 DELAY(20);
528 }
529
530 /* then reset */
531 bus_space_write_4(t, cr, QE_CRI_CTRL, QE_CR_CTRL_RESET);
532 for (n = 200; n > 0; n--) {
533 if ((bus_space_read_4(t, cr, QE_CRI_CTRL) &
534 QE_CR_CTRL_RESET) == 0)
535 break;
536 DELAY(20);
537 }
538 }
539
540 /*
541 * Reset interface.
542 */
543 void
544 qereset(sc)
545 struct qe_softc *sc;
546 {
547 int s;
548
549 s = splnet();
550 qestop(sc);
551 qeinit(sc);
552 splx(s);
553 }
554
555 void
556 qewatchdog(ifp)
557 struct ifnet *ifp;
558 {
559 struct qe_softc *sc = ifp->if_softc;
560
561 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
562 ++sc->sc_ethercom.ec_if.if_oerrors;
563
564 qereset(sc);
565 }
566
567 /*
568 * Interrupt dispatch.
569 */
570 int
571 qeintr(arg)
572 void *arg;
573 {
574 struct qe_softc *sc = (struct qe_softc *)arg;
575 bus_space_tag_t t = sc->sc_bustag;
576 u_int32_t qecstat, qestat;
577 int r = 0;
578
579 #if defined(SUN4U) || defined(__GNUC__)
580 (void)&t;
581 #endif
582 /* Read QEC status and channel status */
583 qecstat = bus_space_read_4(t, sc->sc_qr, QEC_QRI_STAT);
584
585 /* Filter out status for this channel */
586 qecstat = qecstat >> (4 * sc->sc_channel);
587 if ((qecstat & 0xf) == 0)
588 return (r);
589
590 qestat = bus_space_read_4(t, sc->sc_cr, QE_CRI_STAT);
591
592 if (qestat & QE_CR_STAT_ALLERRORS) {
593 r |= qe_eint(sc, qestat);
594 if (r == -1)
595 return (1);
596 }
597
598 if (qestat & QE_CR_STAT_TXIRQ)
599 r |= qe_tint(sc);
600
601 if (qestat & QE_CR_STAT_RXIRQ)
602 r |= qe_rint(sc);
603
604 return (r);
605 }
606
607 /*
608 * Transmit interrupt.
609 */
610 int
611 qe_tint(sc)
612 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(sc)
652 struct qe_softc *sc;
653 {
654 struct qec_xd *xd = sc->sc_rb.rb_rxd;
655 unsigned int bix, len;
656 unsigned int nrbuf = sc->sc_rb.rb_nrbuf;
657
658 bix = sc->sc_rb.rb_rdtail;
659
660 /*
661 * Process all buffers with valid data.
662 */
663 for (;;) {
664 len = xd[bix].xd_flags;
665 if (len & QEC_XD_OWN)
666 break;
667
668 len &= QEC_XD_LENGTH;
669 len -= 4;
670 qe_read(sc, bix, len);
671
672 /* ... */
673 xd[(bix+nrbuf) % QEC_XD_RING_MAXSIZE].xd_flags =
674 QEC_XD_OWN | (QE_PKT_BUF_SZ & QEC_XD_LENGTH);
675
676 if (++bix == QEC_XD_RING_MAXSIZE)
677 bix = 0;
678 }
679
680 sc->sc_rb.rb_rdtail = bix;
681
682 return (1);
683 }
684
685 /*
686 * Error interrupt.
687 */
688 int
689 qe_eint(sc, why)
690 struct qe_softc *sc;
691 u_int32_t why;
692 {
693 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
694 int r = 0, rst = 0;
695
696 if (why & QE_CR_STAT_EDEFER) {
697 printf("%s: excessive tx defers.\n", sc->sc_dev.dv_xname);
698 r |= 1;
699 ifp->if_oerrors++;
700 }
701
702 if (why & QE_CR_STAT_CLOSS) {
703 printf("%s: no carrier, link down?\n", sc->sc_dev.dv_xname);
704 ifp->if_oerrors++;
705 r |= 1;
706 }
707
708 if (why & QE_CR_STAT_ERETRIES) {
709 printf("%s: excessive tx retries\n", sc->sc_dev.dv_xname);
710 ifp->if_oerrors++;
711 r |= 1;
712 rst = 1;
713 }
714
715
716 if (why & QE_CR_STAT_LCOLL) {
717 printf("%s: late tx transmission\n", sc->sc_dev.dv_xname);
718 ifp->if_oerrors++;
719 r |= 1;
720 rst = 1;
721 }
722
723 if (why & QE_CR_STAT_FUFLOW) {
724 printf("%s: tx fifo underflow\n", sc->sc_dev.dv_xname);
725 ifp->if_oerrors++;
726 r |= 1;
727 rst = 1;
728 }
729
730 if (why & QE_CR_STAT_JERROR) {
731 printf("%s: jabber seen\n", sc->sc_dev.dv_xname);
732 r |= 1;
733 }
734
735 if (why & QE_CR_STAT_BERROR) {
736 printf("%s: babble seen\n", sc->sc_dev.dv_xname);
737 r |= 1;
738 }
739
740 if (why & QE_CR_STAT_TCCOFLOW) {
741 ifp->if_collisions += 256;
742 ifp->if_oerrors += 256;
743 r |= 1;
744 }
745
746 if (why & QE_CR_STAT_TXDERROR) {
747 printf("%s: tx descriptor is bad\n", sc->sc_dev.dv_xname);
748 rst = 1;
749 r |= 1;
750 }
751
752 if (why & QE_CR_STAT_TXLERR) {
753 printf("%s: tx late error\n", sc->sc_dev.dv_xname);
754 ifp->if_oerrors++;
755 rst = 1;
756 r |= 1;
757 }
758
759 if (why & QE_CR_STAT_TXPERR) {
760 printf("%s: tx dma parity error\n", sc->sc_dev.dv_xname);
761 ifp->if_oerrors++;
762 rst = 1;
763 r |= 1;
764 }
765
766 if (why & QE_CR_STAT_TXSERR) {
767 printf("%s: tx dma sbus error ack\n", sc->sc_dev.dv_xname);
768 ifp->if_oerrors++;
769 rst = 1;
770 r |= 1;
771 }
772
773 if (why & QE_CR_STAT_RCCOFLOW) {
774 ifp->if_collisions += 256;
775 ifp->if_ierrors += 256;
776 r |= 1;
777 }
778
779 if (why & QE_CR_STAT_RUOFLOW) {
780 ifp->if_ierrors += 256;
781 r |= 1;
782 }
783
784 if (why & QE_CR_STAT_MCOFLOW) {
785 ifp->if_ierrors += 256;
786 r |= 1;
787 }
788
789 if (why & QE_CR_STAT_RXFOFLOW) {
790 printf("%s: rx fifo overflow\n", sc->sc_dev.dv_xname);
791 ifp->if_ierrors++;
792 r |= 1;
793 }
794
795 if (why & QE_CR_STAT_RLCOLL) {
796 printf("%s: rx late collision\n", sc->sc_dev.dv_xname);
797 ifp->if_ierrors++;
798 ifp->if_collisions++;
799 r |= 1;
800 }
801
802 if (why & QE_CR_STAT_FCOFLOW) {
803 ifp->if_ierrors += 256;
804 r |= 1;
805 }
806
807 if (why & QE_CR_STAT_CECOFLOW) {
808 ifp->if_ierrors += 256;
809 r |= 1;
810 }
811
812 if (why & QE_CR_STAT_RXDROP) {
813 printf("%s: rx packet dropped\n", sc->sc_dev.dv_xname);
814 ifp->if_ierrors++;
815 r |= 1;
816 }
817
818 if (why & QE_CR_STAT_RXSMALL) {
819 printf("%s: rx buffer too small\n", sc->sc_dev.dv_xname);
820 ifp->if_ierrors++;
821 r |= 1;
822 rst = 1;
823 }
824
825 if (why & QE_CR_STAT_RXLERR) {
826 printf("%s: rx late error\n", sc->sc_dev.dv_xname);
827 ifp->if_ierrors++;
828 r |= 1;
829 rst = 1;
830 }
831
832 if (why & QE_CR_STAT_RXPERR) {
833 printf("%s: rx dma parity error\n", sc->sc_dev.dv_xname);
834 ifp->if_ierrors++;
835 r |= 1;
836 rst = 1;
837 }
838
839 if (why & QE_CR_STAT_RXSERR) {
840 printf("%s: rx dma sbus error ack\n", sc->sc_dev.dv_xname);
841 ifp->if_ierrors++;
842 r |= 1;
843 rst = 1;
844 }
845
846 if (r == 0)
847 printf("%s: unexpected interrupt error: %08x\n",
848 sc->sc_dev.dv_xname, why);
849
850 if (rst) {
851 printf("%s: resetting...\n", sc->sc_dev.dv_xname);
852 qereset(sc);
853 return (-1);
854 }
855
856 return (r);
857 }
858
859 int
860 qeioctl(ifp, cmd, data)
861 struct ifnet *ifp;
862 u_long cmd;
863 caddr_t data;
864 {
865 struct qe_softc *sc = ifp->if_softc;
866 struct ifaddr *ifa = (struct ifaddr *)data;
867 struct ifreq *ifr = (struct ifreq *)data;
868 int s, error = 0;
869
870 s = splnet();
871
872 switch (cmd) {
873 case SIOCSIFADDR:
874 ifp->if_flags |= IFF_UP;
875 switch (ifa->ifa_addr->sa_family) {
876 #ifdef INET
877 case AF_INET:
878 qeinit(sc);
879 arp_ifinit(ifp, ifa);
880 break;
881 #endif /* INET */
882 #ifdef NS
883 case AF_NS:
884 {
885 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
886
887 if (ns_nullhost(*ina))
888 ina->x_host =
889 *(union ns_host *)LLADDR(ifp->if_sadl);
890 else
891 bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
892 sizeof(sc->sc_enaddr));
893 /* Set new address. */
894 qeinit(sc);
895 break;
896 }
897 #endif /* NS */
898 default:
899 qeinit(sc);
900 break;
901 }
902 break;
903
904 case SIOCSIFFLAGS:
905 sc->sc_promisc = ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI);
906 if ((ifp->if_flags & IFF_UP) == 0 &&
907 (ifp->if_flags & IFF_RUNNING) != 0) {
908 /*
909 * If interface is marked down and it is running, then
910 * stop it.
911 */
912 qestop(sc);
913 ifp->if_flags &= ~IFF_RUNNING;
914 } else if ((ifp->if_flags & IFF_UP) != 0 &&
915 (ifp->if_flags & IFF_RUNNING) == 0) {
916 /*
917 * If interface is marked up and it is stopped, then
918 * start it.
919 */
920 qeinit(sc);
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 ifp->if_flags |= IFF_RUNNING;
1058 ifp->if_flags &= ~IFF_OACTIVE;
1059 splx(s);
1060 }
1061
1062 /*
1063 * Reset multicast filter.
1064 */
1065 void
1066 qe_mcreset(sc)
1067 struct qe_softc *sc;
1068 {
1069 struct ethercom *ec = &sc->sc_ethercom;
1070 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1071 bus_space_tag_t t = sc->sc_bustag;
1072 bus_space_handle_t mr = sc->sc_mr;
1073 struct ether_multi *enm;
1074 struct ether_multistep step;
1075 u_int32_t crc;
1076 u_int16_t hash[4];
1077 u_int8_t octet, maccc = 0, *ladrp = (u_int8_t *)&hash[0];
1078 int i, j;
1079
1080 #if defined(SUN4U) || defined(__GNUC__)
1081 (void)&t;
1082 #endif
1083 if (ifp->if_flags & IFF_ALLMULTI) {
1084 bus_space_write_1(t, mr, QE_MRI_IAC,
1085 QE_MR_IAC_ADDRCHG | QE_MR_IAC_LOGADDR);
1086 for (i = 0; i < 8; i++)
1087 bus_space_write_1(t, mr, QE_MRI_LADRF, 0xff);
1088 bus_space_write_1(t, mr, QE_MRI_IAC, 0);
1089 } else if (ifp->if_flags & IFF_PROMISC) {
1090 maccc |= QE_MR_MACCC_PROM;
1091 } else {
1092 hash[3] = hash[2] = hash[1] = hash[0] = 0;
1093
1094 ETHER_FIRST_MULTI(step, ec, enm);
1095 while (enm != NULL) {
1096 if (bcmp(enm->enm_addrlo, enm->enm_addrhi,
1097 ETHER_ADDR_LEN) != 0) {
1098 /*
1099 * We must listen to a range of multicast
1100 * addresses. For now, just accept all
1101 * multicasts, rather than trying to set only
1102 * those filter bits needed to match the range.
1103 * (At this time, the only use of address
1104 * ranges is for IP multicast routing, for
1105 * which the range is big enough to require
1106 * all bits set.)
1107 */
1108 bus_space_write_1(t, mr, QE_MRI_IAC,
1109 QE_MR_IAC_ADDRCHG | QE_MR_IAC_LOGADDR);
1110 for (i = 0; i < 8; i++)
1111 bus_space_write_1(t, mr, QE_MRI_LADRF,
1112 0xff);
1113 bus_space_write_1(t, mr, QE_MRI_IAC, 0);
1114 ifp->if_flags |= IFF_ALLMULTI;
1115 break;
1116 }
1117
1118 crc = 0xffffffff;
1119
1120 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1121 octet = enm->enm_addrlo[i];
1122
1123 for (j = 0; j < 8; j++) {
1124 if ((crc & 1) ^ (octet & 1)) {
1125 crc >>= 1;
1126 crc ^= MC_POLY_LE;
1127 }
1128 else
1129 crc >>= 1;
1130 octet >>= 1;
1131 }
1132 }
1133
1134 crc >>= 26;
1135 hash[crc >> 4] |= 1 << (crc & 0xf);
1136 ETHER_NEXT_MULTI(step, enm);
1137 }
1138
1139 bus_space_write_1(t, mr, QE_MRI_IAC,
1140 QE_MR_IAC_ADDRCHG | QE_MR_IAC_LOGADDR);
1141 for (i = 0; i < 8; i++)
1142 bus_space_write_1(t, mr, QE_MRI_LADRF, ladrp[i]);
1143 bus_space_write_1(t, mr, QE_MRI_IAC, 0);
1144 }
1145
1146 bus_space_write_1(t, mr, QE_MRI_MACCC,
1147 maccc | QE_MR_MACCC_ENXMT | QE_MR_MACCC_ENRCV);
1148 }
1149
1150 /*
1151 * Get current media settings.
1152 */
1153 void
1154 qe_ifmedia_sts(ifp, ifmr)
1155 struct ifnet *ifp;
1156 struct ifmediareq *ifmr;
1157 {
1158 struct qe_softc *sc = ifp->if_softc;
1159 bus_space_tag_t t = sc->sc_bustag;
1160 bus_space_handle_t mr = sc->sc_mr;
1161 u_int8_t v;
1162
1163 #if defined(SUN4U) || defined(__GNUC__)
1164 (void)&t;
1165 #endif
1166 v = bus_space_read_1(t, mr, QE_MRI_PLSCC);
1167
1168 switch (bus_space_read_1(t, mr, QE_MRI_PLSCC) & QE_MR_PLSCC_PORTMASK) {
1169 case QE_MR_PLSCC_TP:
1170 ifmr->ifm_active = IFM_ETHER | IFM_10_T;
1171 break;
1172 case QE_MR_PLSCC_AUI:
1173 ifmr->ifm_active = IFM_ETHER | IFM_10_5;
1174 break;
1175 case QE_MR_PLSCC_GPSI:
1176 case QE_MR_PLSCC_DAI:
1177 /* ... */
1178 break;
1179 }
1180
1181 v = bus_space_read_1(t, mr, QE_MRI_PHYCC);
1182 ifmr->ifm_status |= IFM_AVALID;
1183 if ((v & QE_MR_PHYCC_LNKFL) != 0)
1184 ifmr->ifm_status &= ~IFM_ACTIVE;
1185 else
1186 ifmr->ifm_status |= IFM_ACTIVE;
1187
1188 }
1189
1190 /*
1191 * Set media options.
1192 */
1193 int
1194 qe_ifmedia_upd(ifp)
1195 struct ifnet *ifp;
1196 {
1197 struct qe_softc *sc = ifp->if_softc;
1198 struct ifmedia *ifm = &sc->sc_ifmedia;
1199 bus_space_tag_t t = sc->sc_bustag;
1200 bus_space_handle_t mr = sc->sc_mr;
1201 int newmedia = ifm->ifm_media;
1202 u_int8_t plscc, phycc;
1203
1204 #if defined(SUN4U) || defined(__GNUC__)
1205 (void)&t;
1206 #endif
1207 if (IFM_TYPE(newmedia) != IFM_ETHER)
1208 return (EINVAL);
1209
1210 plscc = bus_space_read_1(t, mr, QE_MRI_PLSCC) & ~QE_MR_PLSCC_PORTMASK;
1211 phycc = bus_space_read_1(t, mr, QE_MRI_PHYCC) & ~QE_MR_PHYCC_ASEL;
1212
1213 if (IFM_SUBTYPE(newmedia) == IFM_AUTO)
1214 phycc |= QE_MR_PHYCC_ASEL;
1215 else if (IFM_SUBTYPE(newmedia) == IFM_10_T)
1216 plscc |= QE_MR_PLSCC_TP;
1217 else if (IFM_SUBTYPE(newmedia) == IFM_10_5)
1218 plscc |= QE_MR_PLSCC_AUI;
1219
1220 bus_space_write_1(t, mr, QE_MRI_PLSCC, plscc);
1221 bus_space_write_1(t, mr, QE_MRI_PHYCC, phycc);
1222
1223 return (0);
1224 }
1225