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