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