be.c revision 1.30 1 /* $NetBSD: be.c,v 1.30 2002/03/11 16:00:55 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 Theo de Raadt and 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 #include <sys/cdefs.h>
67 __KERNEL_RCSID(0, "$NetBSD: be.c,v 1.30 2002/03/11 16:00:55 pk Exp $");
68
69 #include "opt_ddb.h"
70 #include "opt_inet.h"
71 #include "opt_ccitt.h"
72 #include "opt_llc.h"
73 #include "opt_ns.h"
74 #include "bpfilter.h"
75 #include "rnd.h"
76
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/callout.h>
80 #include <sys/kernel.h>
81 #include <sys/errno.h>
82 #include <sys/ioctl.h>
83 #include <sys/mbuf.h>
84 #include <sys/socket.h>
85 #include <sys/syslog.h>
86 #include <sys/device.h>
87 #include <sys/malloc.h>
88 #if NRND > 0
89 #include <sys/rnd.h>
90 #endif
91
92 #include <net/if.h>
93 #include <net/if_dl.h>
94 #include <net/if_types.h>
95 #include <net/netisr.h>
96 #include <net/if_media.h>
97 #include <net/if_ether.h>
98
99 #ifdef INET
100 #include <netinet/in.h>
101 #include <netinet/if_inarp.h>
102 #include <netinet/in_systm.h>
103 #include <netinet/in_var.h>
104 #include <netinet/ip.h>
105 #endif
106
107 #ifdef NS
108 #include <netns/ns.h>
109 #include <netns/ns_if.h>
110 #endif
111
112 #if NBPFILTER > 0
113 #include <net/bpf.h>
114 #include <net/bpfdesc.h>
115 #endif
116
117 #include <machine/bus.h>
118 #include <machine/intr.h>
119 #include <machine/autoconf.h>
120
121 #include <dev/sbus/sbusvar.h>
122
123 #include <dev/mii/mii.h>
124 #include <dev/mii/miivar.h>
125
126 #include <dev/sbus/qecreg.h>
127 #include <dev/sbus/qecvar.h>
128 #include <dev/sbus/bereg.h>
129
130 struct be_softc {
131 struct device sc_dev;
132 struct sbusdev sc_sd; /* sbus device */
133 bus_space_tag_t sc_bustag; /* bus & dma tags */
134 bus_dma_tag_t sc_dmatag;
135 bus_dmamap_t sc_dmamap;
136 struct ethercom sc_ethercom;
137 /*struct ifmedia sc_ifmedia; -* interface media */
138 struct mii_data sc_mii; /* MII media control */
139 #define sc_media sc_mii.mii_media/* shorthand */
140 int sc_phys[2]; /* MII instance -> phy */
141
142 struct callout sc_tick_ch;
143
144 /*
145 * Some `mii_softc' items we need to emulate MII operation
146 * for our internal transceiver.
147 */
148 int sc_mii_inst; /* instance of internal phy */
149 int sc_mii_active; /* currently active medium */
150 int sc_mii_ticks; /* tick counter */
151 int sc_mii_flags; /* phy status flags */
152 #define MIIF_HAVELINK 0x04000000
153 int sc_intphy_curspeed; /* Established link speed */
154
155 struct qec_softc *sc_qec; /* QEC parent */
156
157 bus_space_handle_t sc_qr; /* QEC registers */
158 bus_space_handle_t sc_br; /* BE registers */
159 bus_space_handle_t sc_cr; /* channel registers */
160 bus_space_handle_t sc_tr; /* transceiver registers */
161
162 u_int sc_rev;
163
164 int sc_channel; /* channel number */
165 int sc_burst;
166
167 struct qec_ring sc_rb; /* Packet Ring Buffer */
168
169 /* MAC address */
170 u_int8_t sc_enaddr[6];
171 };
172
173 int bematch __P((struct device *, struct cfdata *, void *));
174 void beattach __P((struct device *, struct device *, void *));
175
176 void beinit __P((struct be_softc *));
177 void bestart __P((struct ifnet *));
178 void bestop __P((struct be_softc *));
179 void bewatchdog __P((struct ifnet *));
180 int beioctl __P((struct ifnet *, u_long, caddr_t));
181 void bereset __P((struct be_softc *));
182
183 int beintr __P((void *));
184 int berint __P((struct be_softc *));
185 int betint __P((struct be_softc *));
186 int beqint __P((struct be_softc *, u_int32_t));
187 int beeint __P((struct be_softc *, u_int32_t));
188
189 static void be_read __P((struct be_softc *, int, int));
190 static int be_put __P((struct be_softc *, int, struct mbuf *));
191 static struct mbuf *be_get __P((struct be_softc *, int, int));
192
193 void be_pal_gate __P((struct be_softc *, int));
194
195 /* ifmedia callbacks */
196 void be_ifmedia_sts __P((struct ifnet *, struct ifmediareq *));
197 int be_ifmedia_upd __P((struct ifnet *));
198
199 void be_mcreset __P((struct be_softc *));
200
201 /* MII methods & callbacks */
202 static int be_mii_readreg __P((struct device *, int, int));
203 static void be_mii_writereg __P((struct device *, int, int, int));
204 static void be_mii_statchg __P((struct device *));
205
206 /* MII helpers */
207 static void be_mii_sync __P((struct be_softc *));
208 static void be_mii_sendbits __P((struct be_softc *, int, u_int32_t, int));
209 static int be_mii_reset __P((struct be_softc *, int));
210 static int be_tcvr_read_bit __P((struct be_softc *, int));
211 static void be_tcvr_write_bit __P((struct be_softc *, int, int));
212
213 void be_tick __P((void *));
214 void be_intphy_auto __P((struct be_softc *));
215 void be_intphy_status __P((struct be_softc *));
216 int be_intphy_service __P((struct be_softc *, struct mii_data *, int));
217
218
219 struct cfattach be_ca = {
220 sizeof(struct be_softc), bematch, beattach
221 };
222
223 int
224 bematch(parent, cf, aux)
225 struct device *parent;
226 struct cfdata *cf;
227 void *aux;
228 {
229 struct sbus_attach_args *sa = aux;
230
231 return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
232 }
233
234 void
235 beattach(parent, self, aux)
236 struct device *parent, *self;
237 void *aux;
238 {
239 struct sbus_attach_args *sa = aux;
240 struct qec_softc *qec = (struct qec_softc *)parent;
241 struct be_softc *sc = (struct be_softc *)self;
242 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
243 struct mii_data *mii = &sc->sc_mii;
244 struct mii_softc *child;
245 int node = sa->sa_node;
246 bus_dma_tag_t dmatag = sa->sa_dmatag;
247 bus_dma_segment_t seg;
248 bus_size_t size;
249 int instance;
250 int rseg, error;
251 u_int32_t v;
252 extern void myetheraddr __P((u_char *));
253
254 if (sa->sa_nreg < 3) {
255 printf("%s: only %d register sets\n",
256 self->dv_xname, sa->sa_nreg);
257 return;
258 }
259
260 if (bus_space_map(sa->sa_bustag,
261 (bus_addr_t)BUS_ADDR(
262 sa->sa_reg[0].sbr_slot,
263 sa->sa_reg[0].sbr_offset),
264 (bus_size_t)sa->sa_reg[0].sbr_size,
265 BUS_SPACE_MAP_LINEAR, &sc->sc_cr) != 0) {
266 printf("beattach: cannot map registers\n");
267 return;
268 }
269
270 if (bus_space_map(sa->sa_bustag,
271 (bus_addr_t)BUS_ADDR(
272 sa->sa_reg[1].sbr_slot,
273 sa->sa_reg[1].sbr_offset),
274 (bus_size_t)sa->sa_reg[1].sbr_size,
275 BUS_SPACE_MAP_LINEAR, &sc->sc_br) != 0) {
276 printf("beattach: cannot map registers\n");
277 return;
278 }
279
280 if (bus_space_map(sa->sa_bustag,
281 (bus_addr_t)BUS_ADDR(
282 sa->sa_reg[2].sbr_slot,
283 sa->sa_reg[2].sbr_offset),
284 (bus_size_t)sa->sa_reg[2].sbr_size,
285 BUS_SPACE_MAP_LINEAR, &sc->sc_tr) != 0) {
286 printf("beattach: cannot map registers\n");
287 return;
288 }
289
290 sc->sc_bustag = sa->sa_bustag;
291 sc->sc_qec = qec;
292 sc->sc_qr = qec->sc_regs;
293
294 sc->sc_rev = PROM_getpropint(node, "board-version", -1);
295 printf(" rev %x", sc->sc_rev);
296
297 bestop(sc);
298
299 sc->sc_channel = PROM_getpropint(node, "channel#", -1);
300 if (sc->sc_channel == -1)
301 sc->sc_channel = 0;
302
303 sc->sc_burst = PROM_getpropint(node, "burst-sizes", -1);
304 if (sc->sc_burst == -1)
305 sc->sc_burst = qec->sc_burst;
306
307 /* Clamp at parent's burst sizes */
308 sc->sc_burst &= qec->sc_burst;
309
310 /* Establish interrupt handler */
311 if (sa->sa_nintr)
312 (void)bus_intr_establish(sa->sa_bustag, sa->sa_pri, IPL_NET,
313 0, beintr, sc);
314
315 myetheraddr(sc->sc_enaddr);
316 printf(" address %s\n", ether_sprintf(sc->sc_enaddr));
317
318 /*
319 * Allocate descriptor ring and buffers.
320 */
321
322 /* for now, allocate as many bufs as there are ring descriptors */
323 sc->sc_rb.rb_ntbuf = QEC_XD_RING_MAXSIZE;
324 sc->sc_rb.rb_nrbuf = QEC_XD_RING_MAXSIZE;
325
326 size = QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd) +
327 QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd) +
328 sc->sc_rb.rb_ntbuf * BE_PKT_BUF_SZ +
329 sc->sc_rb.rb_nrbuf * BE_PKT_BUF_SZ;
330
331 /* Get a DMA handle */
332 if ((error = bus_dmamap_create(dmatag, size, 1, size, 0,
333 BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
334 printf("%s: DMA map create error %d\n", self->dv_xname, error);
335 return;
336 }
337
338 /* Allocate DMA buffer */
339 if ((error = bus_dmamem_alloc(sa->sa_dmatag, size, 0, 0,
340 &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
341 printf("%s: DMA buffer alloc error %d\n",
342 self->dv_xname, error);
343 return;
344 }
345
346 /* Map DMA memory in CPU addressable space */
347 if ((error = bus_dmamem_map(sa->sa_dmatag, &seg, rseg, size,
348 &sc->sc_rb.rb_membase,
349 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
350 printf("%s: DMA buffer map error %d\n",
351 self->dv_xname, error);
352 bus_dmamem_free(sa->sa_dmatag, &seg, rseg);
353 return;
354 }
355
356 /* Load the buffer */
357 if ((error = bus_dmamap_load(dmatag, sc->sc_dmamap,
358 sc->sc_rb.rb_membase, size, NULL,
359 BUS_DMA_NOWAIT)) != 0) {
360 printf("%s: DMA buffer map load error %d\n",
361 self->dv_xname, error);
362 bus_dmamem_unmap(dmatag, sc->sc_rb.rb_membase, size);
363 bus_dmamem_free(dmatag, &seg, rseg);
364 return;
365 }
366 sc->sc_rb.rb_dmabase = sc->sc_dmamap->dm_segs[0].ds_addr;
367
368 /*
369 * Initialize our media structures and MII info.
370 */
371 mii->mii_ifp = ifp;
372 mii->mii_readreg = be_mii_readreg;
373 mii->mii_writereg = be_mii_writereg;
374 mii->mii_statchg = be_mii_statchg;
375
376 ifmedia_init(&mii->mii_media, 0, be_ifmedia_upd, be_ifmedia_sts);
377
378 callout_init(&sc->sc_tick_ch);
379
380 /*
381 * Initialize transceiver and determine which PHY connection to use.
382 */
383 be_mii_sync(sc);
384 v = bus_space_read_4(sc->sc_bustag, sc->sc_tr, BE_TRI_MGMTPAL);
385
386 instance = 0;
387
388 if ((v & MGMT_PAL_EXT_MDIO) != 0) {
389
390 mii_attach(&sc->sc_dev, mii, 0xffffffff, BE_PHY_EXTERNAL,
391 MII_OFFSET_ANY, 0);
392
393 child = LIST_FIRST(&mii->mii_phys);
394 if (child == NULL) {
395 /* No PHY attached */
396 ifmedia_add(&sc->sc_media,
397 IFM_MAKEWORD(IFM_ETHER,IFM_NONE,0,instance),
398 0, NULL);
399 ifmedia_set(&sc->sc_media,
400 IFM_MAKEWORD(IFM_ETHER,IFM_NONE,0,instance));
401 } else {
402 /*
403 * Note: we support just one PHY on the external
404 * MII connector.
405 */
406 #ifdef DIAGNOSTIC
407 if (LIST_NEXT(child, mii_list) != NULL) {
408 printf("%s: spurious MII device %s attached\n",
409 sc->sc_dev.dv_xname,
410 child->mii_dev.dv_xname);
411 }
412 #endif
413 if (child->mii_phy != BE_PHY_EXTERNAL ||
414 child->mii_inst > 0) {
415 printf("%s: cannot accomodate MII device %s"
416 " at phy %d, instance %d\n",
417 sc->sc_dev.dv_xname,
418 child->mii_dev.dv_xname,
419 child->mii_phy, child->mii_inst);
420 } else {
421 sc->sc_phys[instance] = child->mii_phy;
422 }
423
424 /*
425 * XXX - we can really do the following ONLY if the
426 * phy indeed has the auto negotiation capability!!
427 */
428 ifmedia_set(&sc->sc_media,
429 IFM_MAKEWORD(IFM_ETHER,IFM_AUTO,0,instance));
430
431 /* Mark our current media setting */
432 be_pal_gate(sc, BE_PHY_EXTERNAL);
433 instance++;
434 }
435
436 }
437
438 if ((v & MGMT_PAL_INT_MDIO) != 0) {
439 /*
440 * The be internal phy looks vaguely like MII hardware,
441 * but not enough to be able to use the MII device
442 * layer. Hence, we have to take care of media selection
443 * ourselves.
444 */
445
446 sc->sc_mii_inst = instance;
447 sc->sc_phys[instance] = BE_PHY_INTERNAL;
448
449 /* Use `ifm_data' to store BMCR bits */
450 ifmedia_add(&sc->sc_media,
451 IFM_MAKEWORD(IFM_ETHER,IFM_10_T,0,instance),
452 0, NULL);
453 ifmedia_add(&sc->sc_media,
454 IFM_MAKEWORD(IFM_ETHER,IFM_100_TX,0,instance),
455 BMCR_S100, NULL);
456 ifmedia_add(&sc->sc_media,
457 IFM_MAKEWORD(IFM_ETHER,IFM_AUTO,0,instance),
458 0, NULL);
459
460 printf("on-board transceiver at %s: 10baseT, 100baseTX, auto\n",
461 self->dv_xname);
462
463 be_mii_reset(sc, BE_PHY_INTERNAL);
464 /* Only set default medium here if there's no external PHY */
465 if (instance == 0) {
466 be_pal_gate(sc, BE_PHY_INTERNAL);
467 ifmedia_set(&sc->sc_media,
468 IFM_MAKEWORD(IFM_ETHER,IFM_AUTO,0,instance));
469 } else
470 be_mii_writereg((void *)sc,
471 BE_PHY_INTERNAL, MII_BMCR, BMCR_ISO);
472 }
473
474 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
475 ifp->if_softc = sc;
476 ifp->if_start = bestart;
477 ifp->if_ioctl = beioctl;
478 ifp->if_watchdog = bewatchdog;
479 ifp->if_flags =
480 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
481 IFQ_SET_READY(&ifp->if_snd);
482
483 /* Attach the interface. */
484 if_attach(ifp);
485 ether_ifattach(ifp, sc->sc_enaddr);
486 }
487
488
489 /*
490 * Routine to copy from mbuf chain to transmit buffer in
491 * network buffer memory.
492 */
493 static __inline__ int
494 be_put(sc, idx, m)
495 struct be_softc *sc;
496 int idx;
497 struct mbuf *m;
498 {
499 struct mbuf *n;
500 int len, tlen = 0, boff = 0;
501 caddr_t bp;
502
503 bp = sc->sc_rb.rb_txbuf + (idx % sc->sc_rb.rb_ntbuf) * BE_PKT_BUF_SZ;
504
505 for (; m; m = n) {
506 len = m->m_len;
507 if (len == 0) {
508 MFREE(m, n);
509 continue;
510 }
511 bcopy(mtod(m, caddr_t), bp+boff, len);
512 boff += len;
513 tlen += len;
514 MFREE(m, n);
515 }
516 return (tlen);
517 }
518
519 /*
520 * Pull data off an interface.
521 * Len is the length of data, with local net header stripped.
522 * We copy the data into mbufs. When full cluster sized units are present,
523 * we copy into clusters.
524 */
525 static __inline__ struct mbuf *
526 be_get(sc, idx, totlen)
527 struct be_softc *sc;
528 int idx, totlen;
529 {
530 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
531 struct mbuf *m;
532 struct mbuf *top, **mp;
533 int len, pad, boff = 0;
534 caddr_t bp;
535
536 bp = sc->sc_rb.rb_rxbuf + (idx % sc->sc_rb.rb_nrbuf) * BE_PKT_BUF_SZ;
537
538 MGETHDR(m, M_DONTWAIT, MT_DATA);
539 if (m == NULL)
540 return (NULL);
541 m->m_pkthdr.rcvif = ifp;
542 m->m_pkthdr.len = totlen;
543
544 pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
545 m->m_data += pad;
546 len = MHLEN - pad;
547 top = NULL;
548 mp = ⊤
549
550 while (totlen > 0) {
551 if (top) {
552 MGET(m, M_DONTWAIT, MT_DATA);
553 if (m == NULL) {
554 m_freem(top);
555 return (NULL);
556 }
557 len = MLEN;
558 }
559 if (top && totlen >= MINCLSIZE) {
560 MCLGET(m, M_DONTWAIT);
561 if (m->m_flags & M_EXT)
562 len = MCLBYTES;
563 }
564 m->m_len = len = min(totlen, len);
565 bcopy(bp + boff, mtod(m, caddr_t), len);
566 boff += len;
567 totlen -= len;
568 *mp = m;
569 mp = &m->m_next;
570 }
571
572 return (top);
573 }
574
575 /*
576 * Pass a packet to the higher levels.
577 */
578 static __inline__ void
579 be_read(sc, idx, len)
580 struct be_softc *sc;
581 int idx, len;
582 {
583 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
584 struct mbuf *m;
585
586 if (len <= sizeof(struct ether_header) ||
587 len > ETHERMTU + sizeof(struct ether_header)) {
588
589 printf("%s: invalid packet size %d; dropping\n",
590 ifp->if_xname, len);
591
592 ifp->if_ierrors++;
593 return;
594 }
595
596 /*
597 * Pull packet off interface.
598 */
599 m = be_get(sc, idx, len);
600 if (m == NULL) {
601 ifp->if_ierrors++;
602 return;
603 }
604 ifp->if_ipackets++;
605
606 #if NBPFILTER > 0
607 /*
608 * Check if there's a BPF listener on this interface.
609 * If so, hand off the raw packet to BPF.
610 */
611 if (ifp->if_bpf)
612 bpf_mtap(ifp->if_bpf, m);
613 #endif
614 /* Pass the packet up. */
615 (*ifp->if_input)(ifp, m);
616 }
617
618 /*
619 * Start output on interface.
620 * We make two assumptions here:
621 * 1) that the current priority is set to splnet _before_ this code
622 * is called *and* is returned to the appropriate priority after
623 * return
624 * 2) that the IFF_OACTIVE flag is checked before this code is called
625 * (i.e. that the output part of the interface is idle)
626 */
627 void
628 bestart(ifp)
629 struct ifnet *ifp;
630 {
631 struct be_softc *sc = (struct be_softc *)ifp->if_softc;
632 struct qec_xd *txd = sc->sc_rb.rb_txd;
633 struct mbuf *m;
634 unsigned int bix, len;
635 unsigned int ntbuf = sc->sc_rb.rb_ntbuf;
636
637 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
638 return;
639
640 bix = sc->sc_rb.rb_tdhead;
641
642 for (;;) {
643 IFQ_DEQUEUE(&ifp->if_snd, m);
644 if (m == 0)
645 break;
646
647 #if NBPFILTER > 0
648 /*
649 * If BPF is listening on this interface, let it see the
650 * packet before we commit it to the wire.
651 */
652 if (ifp->if_bpf)
653 bpf_mtap(ifp->if_bpf, m);
654 #endif
655
656 /*
657 * Copy the mbuf chain into the transmit buffer.
658 */
659 len = be_put(sc, bix, m);
660
661 /*
662 * Initialize transmit registers and start transmission
663 */
664 txd[bix].xd_flags = QEC_XD_OWN | QEC_XD_SOP | QEC_XD_EOP |
665 (len & QEC_XD_LENGTH);
666 bus_space_write_4(sc->sc_bustag, sc->sc_cr, BE_CRI_CTRL,
667 BE_CR_CTRL_TWAKEUP);
668
669 if (++bix == QEC_XD_RING_MAXSIZE)
670 bix = 0;
671
672 if (++sc->sc_rb.rb_td_nbusy == ntbuf) {
673 ifp->if_flags |= IFF_OACTIVE;
674 break;
675 }
676 }
677
678 sc->sc_rb.rb_tdhead = bix;
679 }
680
681 void
682 bestop(sc)
683 struct be_softc *sc;
684 {
685 int n;
686 bus_space_tag_t t = sc->sc_bustag;
687 bus_space_handle_t br = sc->sc_br;
688
689 callout_stop(&sc->sc_tick_ch);
690
691 /* Down the MII. */
692 mii_down(&sc->sc_mii);
693 (void)be_intphy_service(sc, &sc->sc_mii, MII_DOWN);
694
695 /* Stop the transmitter */
696 bus_space_write_4(t, br, BE_BRI_TXCFG, 0);
697 for (n = 32; n > 0; n--) {
698 if (bus_space_read_4(t, br, BE_BRI_TXCFG) == 0)
699 break;
700 DELAY(20);
701 }
702
703 /* Stop the receiver */
704 bus_space_write_4(t, br, BE_BRI_RXCFG, 0);
705 for (n = 32; n > 0; n--) {
706 if (bus_space_read_4(t, br, BE_BRI_RXCFG) == 0)
707 break;
708 DELAY(20);
709 }
710 }
711
712 /*
713 * Reset interface.
714 */
715 void
716 bereset(sc)
717 struct be_softc *sc;
718 {
719 int s;
720
721 s = splnet();
722 bestop(sc);
723 if ((sc->sc_ethercom.ec_if.if_flags & IFF_UP) != 0)
724 beinit(sc);
725 splx(s);
726 }
727
728 void
729 bewatchdog(ifp)
730 struct ifnet *ifp;
731 {
732 struct be_softc *sc = ifp->if_softc;
733
734 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
735 ++sc->sc_ethercom.ec_if.if_oerrors;
736
737 bereset(sc);
738 }
739
740 int
741 beintr(v)
742 void *v;
743 {
744 struct be_softc *sc = (struct be_softc *)v;
745 bus_space_tag_t t = sc->sc_bustag;
746 u_int32_t whyq, whyb, whyc;
747 int r = 0;
748
749 /* Read QEC status, channel status and BE status */
750 whyq = bus_space_read_4(t, sc->sc_qr, QEC_QRI_STAT);
751 whyc = bus_space_read_4(t, sc->sc_cr, BE_CRI_STAT);
752 whyb = bus_space_read_4(t, sc->sc_br, BE_BRI_STAT);
753
754 if (whyq & QEC_STAT_BM)
755 r |= beeint(sc, whyb);
756
757 if (whyq & QEC_STAT_ER)
758 r |= beqint(sc, whyc);
759
760 if (whyq & QEC_STAT_TX && whyc & BE_CR_STAT_TXIRQ)
761 r |= betint(sc);
762
763 if (whyq & QEC_STAT_RX && whyc & BE_CR_STAT_RXIRQ)
764 r |= berint(sc);
765
766 return (r);
767 }
768
769 /*
770 * QEC Interrupt.
771 */
772 int
773 beqint(sc, why)
774 struct be_softc *sc;
775 u_int32_t why;
776 {
777 int r = 0, rst = 0;
778
779 if (why & BE_CR_STAT_TXIRQ)
780 r |= 1;
781 if (why & BE_CR_STAT_RXIRQ)
782 r |= 1;
783
784 if (why & BE_CR_STAT_BERROR) {
785 r |= 1;
786 rst = 1;
787 printf("%s: bigmac error\n", sc->sc_dev.dv_xname);
788 }
789
790 if (why & BE_CR_STAT_TXDERR) {
791 r |= 1;
792 rst = 1;
793 printf("%s: bogus tx descriptor\n", sc->sc_dev.dv_xname);
794 }
795
796 if (why & (BE_CR_STAT_TXLERR | BE_CR_STAT_TXPERR | BE_CR_STAT_TXSERR)) {
797 r |= 1;
798 rst = 1;
799 printf("%s: tx dma error ( ", sc->sc_dev.dv_xname);
800 if (why & BE_CR_STAT_TXLERR)
801 printf("Late ");
802 if (why & BE_CR_STAT_TXPERR)
803 printf("Parity ");
804 if (why & BE_CR_STAT_TXSERR)
805 printf("Generic ");
806 printf(")\n");
807 }
808
809 if (why & BE_CR_STAT_RXDROP) {
810 r |= 1;
811 rst = 1;
812 printf("%s: out of rx descriptors\n", sc->sc_dev.dv_xname);
813 }
814
815 if (why & BE_CR_STAT_RXSMALL) {
816 r |= 1;
817 rst = 1;
818 printf("%s: rx descriptor too small\n", sc->sc_dev.dv_xname);
819 }
820
821 if (why & (BE_CR_STAT_RXLERR | BE_CR_STAT_RXPERR | BE_CR_STAT_RXSERR)) {
822 r |= 1;
823 rst = 1;
824 printf("%s: rx dma error ( ", sc->sc_dev.dv_xname);
825 if (why & BE_CR_STAT_RXLERR)
826 printf("Late ");
827 if (why & BE_CR_STAT_RXPERR)
828 printf("Parity ");
829 if (why & BE_CR_STAT_RXSERR)
830 printf("Generic ");
831 printf(")\n");
832 }
833
834 if (!r) {
835 rst = 1;
836 printf("%s: unexpected error interrupt %08x\n",
837 sc->sc_dev.dv_xname, why);
838 }
839
840 if (rst) {
841 printf("%s: resetting\n", sc->sc_dev.dv_xname);
842 bereset(sc);
843 }
844
845 return (r);
846 }
847
848 /*
849 * Error interrupt.
850 */
851 int
852 beeint(sc, why)
853 struct be_softc *sc;
854 u_int32_t why;
855 {
856 int r = 0, rst = 0;
857
858 if (why & BE_BR_STAT_RFIFOVF) {
859 r |= 1;
860 rst = 1;
861 printf("%s: receive fifo overrun\n", sc->sc_dev.dv_xname);
862 }
863 if (why & BE_BR_STAT_TFIFO_UND) {
864 r |= 1;
865 rst = 1;
866 printf("%s: transmit fifo underrun\n", sc->sc_dev.dv_xname);
867 }
868 if (why & BE_BR_STAT_MAXPKTERR) {
869 r |= 1;
870 rst = 1;
871 printf("%s: max packet size error\n", sc->sc_dev.dv_xname);
872 }
873
874 if (!r) {
875 rst = 1;
876 printf("%s: unexpected error interrupt %08x\n",
877 sc->sc_dev.dv_xname, why);
878 }
879
880 if (rst) {
881 printf("%s: resetting\n", sc->sc_dev.dv_xname);
882 bereset(sc);
883 }
884
885 return (r);
886 }
887
888 /*
889 * Transmit interrupt.
890 */
891 int
892 betint(sc)
893 struct be_softc *sc;
894 {
895 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
896 bus_space_tag_t t = sc->sc_bustag;
897 bus_space_handle_t br = sc->sc_br;
898 unsigned int bix, txflags;
899
900 /*
901 * Unload collision counters
902 */
903 ifp->if_collisions +=
904 bus_space_read_4(t, br, BE_BRI_NCCNT) +
905 bus_space_read_4(t, br, BE_BRI_FCCNT) +
906 bus_space_read_4(t, br, BE_BRI_EXCNT) +
907 bus_space_read_4(t, br, BE_BRI_LTCNT);
908
909 /*
910 * the clear the hardware counters
911 */
912 bus_space_write_4(t, br, BE_BRI_NCCNT, 0);
913 bus_space_write_4(t, br, BE_BRI_FCCNT, 0);
914 bus_space_write_4(t, br, BE_BRI_EXCNT, 0);
915 bus_space_write_4(t, br, BE_BRI_LTCNT, 0);
916
917 bix = sc->sc_rb.rb_tdtail;
918
919 for (;;) {
920 if (sc->sc_rb.rb_td_nbusy <= 0)
921 break;
922
923 txflags = sc->sc_rb.rb_txd[bix].xd_flags;
924
925 if (txflags & QEC_XD_OWN)
926 break;
927
928 ifp->if_flags &= ~IFF_OACTIVE;
929 ifp->if_opackets++;
930
931 if (++bix == QEC_XD_RING_MAXSIZE)
932 bix = 0;
933
934 --sc->sc_rb.rb_td_nbusy;
935 }
936
937 sc->sc_rb.rb_tdtail = bix;
938
939 bestart(ifp);
940
941 if (sc->sc_rb.rb_td_nbusy == 0)
942 ifp->if_timer = 0;
943
944 return (1);
945 }
946
947 /*
948 * Receive interrupt.
949 */
950 int
951 berint(sc)
952 struct be_softc *sc;
953 {
954 struct qec_xd *xd = sc->sc_rb.rb_rxd;
955 unsigned int bix, len;
956 unsigned int nrbuf = sc->sc_rb.rb_nrbuf;
957
958 bix = sc->sc_rb.rb_rdtail;
959
960 /*
961 * Process all buffers with valid data.
962 */
963 for (;;) {
964 len = xd[bix].xd_flags;
965 if (len & QEC_XD_OWN)
966 break;
967
968 len &= QEC_XD_LENGTH;
969 be_read(sc, bix, len);
970
971 /* ... */
972 xd[(bix+nrbuf) % QEC_XD_RING_MAXSIZE].xd_flags =
973 QEC_XD_OWN | (BE_PKT_BUF_SZ & QEC_XD_LENGTH);
974
975 if (++bix == QEC_XD_RING_MAXSIZE)
976 bix = 0;
977 }
978
979 sc->sc_rb.rb_rdtail = bix;
980
981 return (1);
982 }
983
984 int
985 beioctl(ifp, cmd, data)
986 struct ifnet *ifp;
987 u_long cmd;
988 caddr_t data;
989 {
990 struct be_softc *sc = ifp->if_softc;
991 struct ifaddr *ifa = (struct ifaddr *)data;
992 struct ifreq *ifr = (struct ifreq *)data;
993 int s, error = 0;
994
995 s = splnet();
996
997 switch (cmd) {
998 case SIOCSIFADDR:
999 ifp->if_flags |= IFF_UP;
1000 switch (ifa->ifa_addr->sa_family) {
1001 #ifdef INET
1002 case AF_INET:
1003 beinit(sc);
1004 arp_ifinit(ifp, ifa);
1005 break;
1006 #endif /* INET */
1007 #ifdef NS
1008 case AF_NS:
1009 {
1010 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
1011
1012 if (ns_nullhost(*ina))
1013 ina->x_host =
1014 *(union ns_host *)LLADDR(ifp->if_sadl);
1015 else
1016 bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
1017 sizeof(sc->sc_enaddr));
1018 /* Set new address. */
1019 beinit(sc);
1020 break;
1021 }
1022 #endif /* NS */
1023 default:
1024 beinit(sc);
1025 break;
1026 }
1027 break;
1028
1029 case SIOCSIFFLAGS:
1030 if ((ifp->if_flags & IFF_UP) == 0 &&
1031 (ifp->if_flags & IFF_RUNNING) != 0) {
1032 /*
1033 * If interface is marked down and it is running, then
1034 * stop it.
1035 */
1036 bestop(sc);
1037 ifp->if_flags &= ~IFF_RUNNING;
1038 } else if ((ifp->if_flags & IFF_UP) != 0 &&
1039 (ifp->if_flags & IFF_RUNNING) == 0) {
1040 /*
1041 * If interface is marked up and it is stopped, then
1042 * start it.
1043 */
1044 beinit(sc);
1045 } else {
1046 /*
1047 * Reset the interface to pick up changes in any other
1048 * flags that affect hardware registers.
1049 */
1050 bestop(sc);
1051 beinit(sc);
1052 }
1053 #ifdef BEDEBUG
1054 if (ifp->if_flags & IFF_DEBUG)
1055 sc->sc_debug = 1;
1056 else
1057 sc->sc_debug = 0;
1058 #endif
1059 break;
1060
1061 case SIOCADDMULTI:
1062 case SIOCDELMULTI:
1063 error = (cmd == SIOCADDMULTI) ?
1064 ether_addmulti(ifr, &sc->sc_ethercom):
1065 ether_delmulti(ifr, &sc->sc_ethercom);
1066
1067 if (error == ENETRESET) {
1068 /*
1069 * Multicast list has changed; set the hardware filter
1070 * accordingly.
1071 */
1072 be_mcreset(sc);
1073 error = 0;
1074 }
1075 break;
1076 case SIOCGIFMEDIA:
1077 case SIOCSIFMEDIA:
1078 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1079 break;
1080 default:
1081 error = EINVAL;
1082 break;
1083 }
1084 splx(s);
1085 return (error);
1086 }
1087
1088
1089 void
1090 beinit(sc)
1091 struct be_softc *sc;
1092 {
1093 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1094 bus_space_tag_t t = sc->sc_bustag;
1095 bus_space_handle_t br = sc->sc_br;
1096 bus_space_handle_t cr = sc->sc_cr;
1097 struct qec_softc *qec = sc->sc_qec;
1098 u_int32_t v;
1099 u_int32_t qecaddr;
1100 u_int8_t *ea;
1101 int s;
1102
1103 s = splnet();
1104
1105 qec_meminit(&sc->sc_rb, BE_PKT_BUF_SZ);
1106
1107 bestop(sc);
1108
1109 ea = sc->sc_enaddr;
1110 bus_space_write_4(t, br, BE_BRI_MACADDR0, (ea[0] << 8) | ea[1]);
1111 bus_space_write_4(t, br, BE_BRI_MACADDR1, (ea[2] << 8) | ea[3]);
1112 bus_space_write_4(t, br, BE_BRI_MACADDR2, (ea[4] << 8) | ea[5]);
1113
1114 /* Clear hash table */
1115 bus_space_write_4(t, br, BE_BRI_HASHTAB0, 0);
1116 bus_space_write_4(t, br, BE_BRI_HASHTAB1, 0);
1117 bus_space_write_4(t, br, BE_BRI_HASHTAB2, 0);
1118 bus_space_write_4(t, br, BE_BRI_HASHTAB3, 0);
1119
1120 /* Re-initialize RX configuration */
1121 v = BE_BR_RXCFG_FIFO;
1122 bus_space_write_4(t, br, BE_BRI_RXCFG, v);
1123
1124 be_mcreset(sc);
1125
1126 bus_space_write_4(t, br, BE_BRI_RANDSEED, 0xbd);
1127
1128 bus_space_write_4(t, br, BE_BRI_XIFCFG,
1129 BE_BR_XCFG_ODENABLE | BE_BR_XCFG_RESV);
1130
1131 bus_space_write_4(t, br, BE_BRI_JSIZE, 4);
1132
1133 /*
1134 * Turn off counter expiration interrupts as well as
1135 * 'gotframe' and 'sentframe'
1136 */
1137 bus_space_write_4(t, br, BE_BRI_IMASK,
1138 BE_BR_IMASK_GOTFRAME |
1139 BE_BR_IMASK_RCNTEXP |
1140 BE_BR_IMASK_ACNTEXP |
1141 BE_BR_IMASK_CCNTEXP |
1142 BE_BR_IMASK_LCNTEXP |
1143 BE_BR_IMASK_CVCNTEXP |
1144 BE_BR_IMASK_SENTFRAME |
1145 BE_BR_IMASK_NCNTEXP |
1146 BE_BR_IMASK_ECNTEXP |
1147 BE_BR_IMASK_LCCNTEXP |
1148 BE_BR_IMASK_FCNTEXP |
1149 BE_BR_IMASK_DTIMEXP);
1150
1151 /* Channel registers: */
1152 bus_space_write_4(t, cr, BE_CRI_RXDS, (u_int32_t)sc->sc_rb.rb_rxddma);
1153 bus_space_write_4(t, cr, BE_CRI_TXDS, (u_int32_t)sc->sc_rb.rb_txddma);
1154
1155 qecaddr = sc->sc_channel * qec->sc_msize;
1156 bus_space_write_4(t, cr, BE_CRI_RXWBUF, qecaddr);
1157 bus_space_write_4(t, cr, BE_CRI_RXRBUF, qecaddr);
1158 bus_space_write_4(t, cr, BE_CRI_TXWBUF, qecaddr + qec->sc_rsize);
1159 bus_space_write_4(t, cr, BE_CRI_TXRBUF, qecaddr + qec->sc_rsize);
1160
1161 bus_space_write_4(t, cr, BE_CRI_RIMASK, 0);
1162 bus_space_write_4(t, cr, BE_CRI_TIMASK, 0);
1163 bus_space_write_4(t, cr, BE_CRI_QMASK, 0);
1164 bus_space_write_4(t, cr, BE_CRI_BMASK, 0);
1165 bus_space_write_4(t, cr, BE_CRI_CCNT, 0);
1166
1167 /* Enable transmitter */
1168 bus_space_write_4(t, br, BE_BRI_TXCFG,
1169 BE_BR_TXCFG_FIFO | BE_BR_TXCFG_ENABLE);
1170
1171 /* Enable receiver */
1172 v = bus_space_read_4(t, br, BE_BRI_RXCFG);
1173 v |= BE_BR_RXCFG_FIFO | BE_BR_RXCFG_ENABLE;
1174 bus_space_write_4(t, br, BE_BRI_RXCFG, v);
1175
1176 ifp->if_flags |= IFF_RUNNING;
1177 ifp->if_flags &= ~IFF_OACTIVE;
1178
1179 be_ifmedia_upd(ifp);
1180 callout_reset(&sc->sc_tick_ch, hz, be_tick, sc);
1181 splx(s);
1182 }
1183
1184 void
1185 be_mcreset(sc)
1186 struct be_softc *sc;
1187 {
1188 struct ethercom *ec = &sc->sc_ethercom;
1189 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1190 bus_space_tag_t t = sc->sc_bustag;
1191 bus_space_handle_t br = sc->sc_br;
1192 u_int32_t crc;
1193 u_int16_t hash[4];
1194 u_int8_t octet;
1195 u_int32_t v;
1196 int i, j;
1197 struct ether_multi *enm;
1198 struct ether_multistep step;
1199
1200 if (ifp->if_flags & IFF_PROMISC) {
1201 v = bus_space_read_4(t, br, BE_BRI_RXCFG);
1202 v |= BE_BR_RXCFG_PMISC;
1203 bus_space_write_4(t, br, BE_BRI_RXCFG, v);
1204 return;
1205 }
1206
1207 if (ifp->if_flags & IFF_ALLMULTI) {
1208 hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
1209 goto chipit;
1210 }
1211
1212 hash[3] = hash[2] = hash[1] = hash[0] = 0;
1213
1214 ETHER_FIRST_MULTI(step, ec, enm);
1215 while (enm != NULL) {
1216 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1217 /*
1218 * We must listen to a range of multicast
1219 * addresses. For now, just accept all
1220 * multicasts, rather than trying to set only
1221 * those filter bits needed to match the range.
1222 * (At this time, the only use of address
1223 * ranges is for IP multicast routing, for
1224 * which the range is big enough to require
1225 * all bits set.)
1226 */
1227 hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
1228 ifp->if_flags |= IFF_ALLMULTI;
1229 goto chipit;
1230 }
1231
1232 crc = 0xffffffff;
1233
1234 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1235 octet = enm->enm_addrlo[i];
1236
1237 for (j = 0; j < 8; j++) {
1238 if ((crc & 1) ^ (octet & 1)) {
1239 crc >>= 1;
1240 crc ^= MC_POLY_LE;
1241 }
1242 else
1243 crc >>= 1;
1244 octet >>= 1;
1245 }
1246 }
1247
1248 crc >>= 26;
1249 hash[crc >> 4] |= 1 << (crc & 0xf);
1250 ETHER_NEXT_MULTI(step, enm);
1251 }
1252
1253 ifp->if_flags &= ~IFF_ALLMULTI;
1254
1255 chipit:
1256 /* Enable the hash filter */
1257 bus_space_write_4(t, br, BE_BRI_HASHTAB0, hash[0]);
1258 bus_space_write_4(t, br, BE_BRI_HASHTAB1, hash[1]);
1259 bus_space_write_4(t, br, BE_BRI_HASHTAB2, hash[2]);
1260 bus_space_write_4(t, br, BE_BRI_HASHTAB3, hash[3]);
1261
1262 v = bus_space_read_4(t, br, BE_BRI_RXCFG);
1263 v &= ~BE_BR_RXCFG_PMISC;
1264 v |= BE_BR_RXCFG_HENABLE;
1265 bus_space_write_4(t, br, BE_BRI_RXCFG, v);
1266 }
1267
1268 /*
1269 * Set the tcvr to an idle state
1270 */
1271 void
1272 be_mii_sync(sc)
1273 struct be_softc *sc;
1274 {
1275 bus_space_tag_t t = sc->sc_bustag;
1276 bus_space_handle_t tr = sc->sc_tr;
1277 int n = 32;
1278
1279 while (n--) {
1280 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1281 MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO |
1282 MGMT_PAL_OENAB);
1283 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1284 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1285 MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO |
1286 MGMT_PAL_OENAB | MGMT_PAL_DCLOCK);
1287 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1288 }
1289 }
1290
1291 void
1292 be_pal_gate(sc, phy)
1293 struct be_softc *sc;
1294 int phy;
1295 {
1296 bus_space_tag_t t = sc->sc_bustag;
1297 bus_space_handle_t tr = sc->sc_tr;
1298 u_int32_t v;
1299
1300 be_mii_sync(sc);
1301
1302 v = ~(TCVR_PAL_EXTLBACK | TCVR_PAL_MSENSE | TCVR_PAL_LTENABLE);
1303 if (phy == BE_PHY_INTERNAL)
1304 v &= ~TCVR_PAL_SERIAL;
1305
1306 bus_space_write_4(t, tr, BE_TRI_TCVRPAL, v);
1307 (void)bus_space_read_4(t, tr, BE_TRI_TCVRPAL);
1308 }
1309
1310 static int
1311 be_tcvr_read_bit(sc, phy)
1312 struct be_softc *sc;
1313 int phy;
1314 {
1315 bus_space_tag_t t = sc->sc_bustag;
1316 bus_space_handle_t tr = sc->sc_tr;
1317 int ret;
1318
1319 if (phy == BE_PHY_INTERNAL) {
1320 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, MGMT_PAL_EXT_MDIO);
1321 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1322 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1323 MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK);
1324 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1325 ret = (bus_space_read_4(t, tr, BE_TRI_MGMTPAL) &
1326 MGMT_PAL_INT_MDIO) >> MGMT_PAL_INT_MDIO_SHIFT;
1327 } else {
1328 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, MGMT_PAL_INT_MDIO);
1329 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1330 ret = (bus_space_read_4(t, tr, BE_TRI_MGMTPAL) &
1331 MGMT_PAL_EXT_MDIO) >> MGMT_PAL_EXT_MDIO_SHIFT;
1332 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1333 MGMT_PAL_INT_MDIO | MGMT_PAL_DCLOCK);
1334 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1335 }
1336
1337 return (ret);
1338 }
1339
1340 static void
1341 be_tcvr_write_bit(sc, phy, bit)
1342 struct be_softc *sc;
1343 int phy;
1344 int bit;
1345 {
1346 bus_space_tag_t t = sc->sc_bustag;
1347 bus_space_handle_t tr = sc->sc_tr;
1348 u_int32_t v;
1349
1350 if (phy == BE_PHY_INTERNAL) {
1351 v = ((bit & 1) << MGMT_PAL_INT_MDIO_SHIFT) |
1352 MGMT_PAL_OENAB | MGMT_PAL_EXT_MDIO;
1353 } else {
1354 v = ((bit & 1) << MGMT_PAL_EXT_MDIO_SHIFT)
1355 | MGMT_PAL_OENAB | MGMT_PAL_INT_MDIO;
1356 }
1357 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, v);
1358 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1359 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, v | MGMT_PAL_DCLOCK);
1360 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1361 }
1362
1363 static void
1364 be_mii_sendbits(sc, phy, data, nbits)
1365 struct be_softc *sc;
1366 int phy;
1367 u_int32_t data;
1368 int nbits;
1369 {
1370 int i;
1371
1372 for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
1373 be_tcvr_write_bit(sc, phy, (data & i) != 0);
1374 }
1375 }
1376
1377 static int
1378 be_mii_readreg(self, phy, reg)
1379 struct device *self;
1380 int phy, reg;
1381 {
1382 struct be_softc *sc = (struct be_softc *)self;
1383 int val = 0, i;
1384
1385 /*
1386 * Read the PHY register by manually driving the MII control lines.
1387 */
1388 be_mii_sync(sc);
1389 be_mii_sendbits(sc, phy, MII_COMMAND_START, 2);
1390 be_mii_sendbits(sc, phy, MII_COMMAND_READ, 2);
1391 be_mii_sendbits(sc, phy, phy, 5);
1392 be_mii_sendbits(sc, phy, reg, 5);
1393
1394 (void) be_tcvr_read_bit(sc, phy);
1395 (void) be_tcvr_read_bit(sc, phy);
1396
1397 for (i = 15; i >= 0; i--)
1398 val |= (be_tcvr_read_bit(sc, phy) << i);
1399
1400 (void) be_tcvr_read_bit(sc, phy);
1401 (void) be_tcvr_read_bit(sc, phy);
1402 (void) be_tcvr_read_bit(sc, phy);
1403
1404 return (val);
1405 }
1406
1407 void
1408 be_mii_writereg(self, phy, reg, val)
1409 struct device *self;
1410 int phy, reg, val;
1411 {
1412 struct be_softc *sc = (struct be_softc *)self;
1413 int i;
1414
1415 /*
1416 * Write the PHY register by manually driving the MII control lines.
1417 */
1418 be_mii_sync(sc);
1419 be_mii_sendbits(sc, phy, MII_COMMAND_START, 2);
1420 be_mii_sendbits(sc, phy, MII_COMMAND_WRITE, 2);
1421 be_mii_sendbits(sc, phy, phy, 5);
1422 be_mii_sendbits(sc, phy, reg, 5);
1423
1424 be_tcvr_write_bit(sc, phy, 1);
1425 be_tcvr_write_bit(sc, phy, 0);
1426
1427 for (i = 15; i >= 0; i--)
1428 be_tcvr_write_bit(sc, phy, (val >> i) & 1);
1429 }
1430
1431 int
1432 be_mii_reset(sc, phy)
1433 struct be_softc *sc;
1434 int phy;
1435 {
1436 int n;
1437
1438 be_mii_writereg((struct device *)sc, phy, MII_BMCR,
1439 BMCR_LOOP | BMCR_PDOWN | BMCR_ISO);
1440 be_mii_writereg((struct device *)sc, phy, MII_BMCR, BMCR_RESET);
1441
1442 for (n = 16; n >= 0; n--) {
1443 int bmcr = be_mii_readreg((struct device *)sc, phy, MII_BMCR);
1444 if ((bmcr & BMCR_RESET) == 0)
1445 break;
1446 DELAY(20);
1447 }
1448 if (n == 0) {
1449 printf("%s: bmcr reset failed\n", sc->sc_dev.dv_xname);
1450 return (EIO);
1451 }
1452
1453 return (0);
1454 }
1455
1456 void
1457 be_tick(arg)
1458 void *arg;
1459 {
1460 struct be_softc *sc = arg;
1461 int s = splnet();
1462
1463 mii_tick(&sc->sc_mii);
1464 (void)be_intphy_service(sc, &sc->sc_mii, MII_TICK);
1465
1466 splx(s);
1467 callout_reset(&sc->sc_tick_ch, hz, be_tick, sc);
1468 }
1469
1470 void
1471 be_mii_statchg(self)
1472 struct device *self;
1473 {
1474 struct be_softc *sc = (struct be_softc *)self;
1475 bus_space_tag_t t = sc->sc_bustag;
1476 bus_space_handle_t br = sc->sc_br;
1477 u_int instance;
1478 u_int32_t v;
1479
1480 instance = IFM_INST(sc->sc_mii.mii_media.ifm_cur->ifm_media);
1481 #ifdef DIAGNOSTIC
1482 if (instance > 1)
1483 panic("be_mii_statchg: instance %d out of range", instance);
1484 #endif
1485
1486 /* Update duplex mode in TX configuration */
1487 v = bus_space_read_4(t, br, BE_BRI_TXCFG);
1488 if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
1489 v |= BE_BR_TXCFG_FULLDPLX;
1490 else
1491 v &= ~BE_BR_TXCFG_FULLDPLX;
1492 bus_space_write_4(t, br, BE_BRI_TXCFG, v);
1493
1494 /* Change to appropriate gate in transceiver PAL */
1495 be_pal_gate(sc, sc->sc_phys[instance]);
1496 }
1497
1498 /*
1499 * Get current media settings.
1500 */
1501 void
1502 be_ifmedia_sts(ifp, ifmr)
1503 struct ifnet *ifp;
1504 struct ifmediareq *ifmr;
1505 {
1506 struct be_softc *sc = ifp->if_softc;
1507
1508 mii_pollstat(&sc->sc_mii);
1509 (void)be_intphy_service(sc, &sc->sc_mii, MII_POLLSTAT);
1510
1511 ifmr->ifm_status = sc->sc_mii.mii_media_status;
1512 ifmr->ifm_active = sc->sc_mii.mii_media_active;
1513 return;
1514 }
1515
1516 /*
1517 * Set media options.
1518 */
1519 int
1520 be_ifmedia_upd(ifp)
1521 struct ifnet *ifp;
1522 {
1523 struct be_softc *sc = ifp->if_softc;
1524 int error;
1525
1526 if ((error = mii_mediachg(&sc->sc_mii)) != 0)
1527 return (error);
1528
1529 return (be_intphy_service(sc, &sc->sc_mii, MII_MEDIACHG));
1530 }
1531
1532 /*
1533 * Service routine for our pseudo-MII internal transceiver.
1534 */
1535 int
1536 be_intphy_service(sc, mii, cmd)
1537 struct be_softc *sc;
1538 struct mii_data *mii;
1539 int cmd;
1540 {
1541 struct ifmedia_entry *ife = mii->mii_media.ifm_cur;
1542 int bmcr, bmsr;
1543 int error;
1544
1545 switch (cmd) {
1546 case MII_POLLSTAT:
1547 /*
1548 * If we're not polling our PHY instance, just return.
1549 */
1550 if (IFM_INST(ife->ifm_media) != sc->sc_mii_inst)
1551 return (0);
1552
1553 break;
1554
1555 case MII_MEDIACHG:
1556
1557 /*
1558 * If the media indicates a different PHY instance,
1559 * isolate ourselves.
1560 */
1561 if (IFM_INST(ife->ifm_media) != sc->sc_mii_inst) {
1562 bmcr = be_mii_readreg((void *)sc,
1563 BE_PHY_INTERNAL, MII_BMCR);
1564 be_mii_writereg((void *)sc,
1565 BE_PHY_INTERNAL, MII_BMCR, bmcr | BMCR_ISO);
1566 sc->sc_mii_flags &= ~MIIF_HAVELINK;
1567 sc->sc_intphy_curspeed = 0;
1568 return (0);
1569 }
1570
1571
1572 if ((error = be_mii_reset(sc, BE_PHY_INTERNAL)) != 0)
1573 return (error);
1574
1575 bmcr = be_mii_readreg((void *)sc, BE_PHY_INTERNAL, MII_BMCR);
1576
1577 /*
1578 * Select the new mode and take out of isolation
1579 */
1580 if (IFM_SUBTYPE(ife->ifm_media) == IFM_100_TX)
1581 bmcr |= BMCR_S100;
1582 else if (IFM_SUBTYPE(ife->ifm_media) == IFM_10_T)
1583 bmcr &= ~BMCR_S100;
1584 else if (IFM_SUBTYPE(ife->ifm_media) == IFM_AUTO) {
1585 if ((sc->sc_mii_flags & MIIF_HAVELINK) != 0) {
1586 bmcr &= ~BMCR_S100;
1587 bmcr |= sc->sc_intphy_curspeed;
1588 } else {
1589 /* Keep isolated until link is up */
1590 bmcr |= BMCR_ISO;
1591 sc->sc_mii_flags |= MIIF_DOINGAUTO;
1592 }
1593 }
1594
1595 if ((IFM_OPTIONS(ife->ifm_media) & IFM_FDX) != 0)
1596 bmcr |= BMCR_FDX;
1597 else
1598 bmcr &= ~BMCR_FDX;
1599
1600 be_mii_writereg((void *)sc, BE_PHY_INTERNAL, MII_BMCR, bmcr);
1601 break;
1602
1603 case MII_TICK:
1604 /*
1605 * If we're not currently selected, just return.
1606 */
1607 if (IFM_INST(ife->ifm_media) != sc->sc_mii_inst)
1608 return (0);
1609
1610 /* Only used for automatic media selection */
1611 if (IFM_SUBTYPE(ife->ifm_media) != IFM_AUTO)
1612 return (0);
1613
1614 /* Is the interface even up? */
1615 if ((mii->mii_ifp->if_flags & IFF_UP) == 0)
1616 return (0);
1617
1618 /*
1619 * Check link status; if we don't have a link, try another
1620 * speed. We can't detect duplex mode, so half-duplex is
1621 * what we have to settle for.
1622 */
1623
1624 /* Read twice in case the register is latched */
1625 bmsr = be_mii_readreg((void *)sc, BE_PHY_INTERNAL, MII_BMSR) |
1626 be_mii_readreg((void *)sc, BE_PHY_INTERNAL, MII_BMSR);
1627
1628 if ((bmsr & BMSR_LINK) != 0) {
1629 /* We have a carrier */
1630 bmcr = be_mii_readreg((void *)sc,
1631 BE_PHY_INTERNAL, MII_BMCR);
1632
1633 if ((sc->sc_mii_flags & MIIF_DOINGAUTO) != 0) {
1634 bmcr = be_mii_readreg((void *)sc,
1635 BE_PHY_INTERNAL, MII_BMCR);
1636
1637 sc->sc_mii_flags |= MIIF_HAVELINK;
1638 sc->sc_intphy_curspeed = (bmcr & BMCR_S100);
1639 sc->sc_mii_flags &= ~MIIF_DOINGAUTO;
1640
1641 bmcr &= ~BMCR_ISO;
1642 be_mii_writereg((void *)sc,
1643 BE_PHY_INTERNAL, MII_BMCR, bmcr);
1644
1645 printf("%s: link up at %s Mbps\n",
1646 sc->sc_dev.dv_xname,
1647 (bmcr & BMCR_S100) ? "100" : "10");
1648 }
1649 return (0);
1650 }
1651
1652 if ((sc->sc_mii_flags & MIIF_DOINGAUTO) == 0) {
1653 sc->sc_mii_flags |= MIIF_DOINGAUTO;
1654 sc->sc_mii_flags &= ~MIIF_HAVELINK;
1655 sc->sc_intphy_curspeed = 0;
1656 printf("%s: link down\n", sc->sc_dev.dv_xname);
1657 }
1658
1659 /* Only retry autonegotiation every 5 seconds. */
1660 if (++sc->sc_mii_ticks < 5)
1661 return(0);
1662
1663 sc->sc_mii_ticks = 0;
1664 bmcr = be_mii_readreg((void *)sc, BE_PHY_INTERNAL, MII_BMCR);
1665 /* Just flip the fast speed bit */
1666 bmcr ^= BMCR_S100;
1667 be_mii_writereg((void *)sc, BE_PHY_INTERNAL, MII_BMCR, bmcr);
1668
1669 break;
1670
1671 case MII_DOWN:
1672 /* Isolate this phy */
1673 bmcr = be_mii_readreg((void *)sc, BE_PHY_INTERNAL, MII_BMCR);
1674 be_mii_writereg((void *)sc,
1675 BE_PHY_INTERNAL, MII_BMCR, bmcr | BMCR_ISO);
1676 return (0);
1677 }
1678
1679 /* Update the media status. */
1680 be_intphy_status(sc);
1681
1682 /* Callback if something changed. */
1683 if (sc->sc_mii_active != mii->mii_media_active || cmd == MII_MEDIACHG) {
1684 (*mii->mii_statchg)((struct device *)sc);
1685 sc->sc_mii_active = mii->mii_media_active;
1686 }
1687 return (0);
1688 }
1689
1690 /*
1691 * Determine status of internal transceiver
1692 */
1693 void
1694 be_intphy_status(sc)
1695 struct be_softc *sc;
1696 {
1697 struct mii_data *mii = &sc->sc_mii;
1698 int media_active, media_status;
1699 int bmcr, bmsr;
1700
1701 media_status = IFM_AVALID;
1702 media_active = 0;
1703
1704 /*
1705 * Internal transceiver; do the work here.
1706 */
1707 bmcr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMCR);
1708
1709 switch (bmcr & (BMCR_S100 | BMCR_FDX)) {
1710 case (BMCR_S100 | BMCR_FDX):
1711 media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
1712 break;
1713 case BMCR_S100:
1714 media_active = IFM_ETHER | IFM_100_TX | IFM_HDX;
1715 break;
1716 case BMCR_FDX:
1717 media_active = IFM_ETHER | IFM_10_T | IFM_FDX;
1718 break;
1719 case 0:
1720 media_active = IFM_ETHER | IFM_10_T | IFM_HDX;
1721 break;
1722 }
1723
1724 /* Read twice in case the register is latched */
1725 bmsr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMSR)|
1726 be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMSR);
1727 if (bmsr & BMSR_LINK)
1728 media_status |= IFM_ACTIVE;
1729
1730 mii->mii_media_status = media_status;
1731 mii->mii_media_active = media_active;
1732 }
1733