be.c revision 1.8 1 /* $NetBSD: be.c,v 1.8 1999/11/12 18:14:20 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 if (sc->sc_conf & BE_CONF_MII) {
616 /* Down the MII. */
617 mii_down(&sc->sc_mii);
618 }
619
620 /* Stop the transmitter */
621 bus_space_write_4(t, br, BE_BRI_TXCFG, 0);
622 for (n = 32; n > 0; n--) {
623 if (bus_space_read_4(t, br, BE_BRI_TXCFG) == 0)
624 break;
625 DELAY(20);
626 }
627
628 /* Stop the receiver */
629 bus_space_write_4(t, br, BE_BRI_RXCFG, 0);
630 for (n = 32; n > 0; n--) {
631 if (bus_space_read_4(t, br, BE_BRI_RXCFG) == 0)
632 break;
633 DELAY(20);
634 }
635 }
636
637 /*
638 * Reset interface.
639 */
640 void
641 bereset(sc)
642 struct be_softc *sc;
643 {
644 int s;
645
646 s = splnet();
647 bestop(sc);
648 beinit(sc);
649 splx(s);
650 }
651
652 void
653 bewatchdog(ifp)
654 struct ifnet *ifp;
655 {
656 struct be_softc *sc = ifp->if_softc;
657
658 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
659 ++sc->sc_ethercom.ec_if.if_oerrors;
660
661 bereset(sc);
662 }
663
664 int
665 beintr(v)
666 void *v;
667 {
668 struct be_softc *sc = (struct be_softc *)v;
669 bus_space_tag_t t = sc->sc_bustag;
670 u_int32_t whyq, whyb, whyc;
671 int r = 0;
672
673 /* Read QEC status, channel status and BE status */
674 whyq = bus_space_read_4(t, sc->sc_qr, QEC_QRI_STAT);
675 whyc = bus_space_read_4(t, sc->sc_cr, BE_CRI_STAT);
676 whyb = bus_space_read_4(t, sc->sc_br, BE_BRI_STAT);
677
678 if (whyq & QEC_STAT_BM)
679 r |= beeint(sc, whyb);
680
681 if (whyq & QEC_STAT_ER)
682 r |= beqint(sc, whyc);
683
684 if (whyq & QEC_STAT_TX && whyc & BE_CR_STAT_TXIRQ)
685 r |= betint(sc);
686
687 if (whyq & QEC_STAT_RX && whyc & BE_CR_STAT_RXIRQ)
688 r |= berint(sc);
689
690 return (r);
691 }
692
693 /*
694 * QEC Interrupt.
695 */
696 int
697 beqint(sc, why)
698 struct be_softc *sc;
699 u_int32_t why;
700 {
701 int r = 0, rst = 0;
702
703 if (why & BE_CR_STAT_TXIRQ)
704 r |= 1;
705 if (why & BE_CR_STAT_RXIRQ)
706 r |= 1;
707
708 if (why & BE_CR_STAT_BERROR) {
709 r |= 1;
710 rst = 1;
711 printf("%s: bigmac error\n", sc->sc_dev.dv_xname);
712 }
713
714 if (why & BE_CR_STAT_TXDERR) {
715 r |= 1;
716 rst = 1;
717 printf("%s: bogus tx descriptor\n", sc->sc_dev.dv_xname);
718 }
719
720 if (why & (BE_CR_STAT_TXLERR | BE_CR_STAT_TXPERR | BE_CR_STAT_TXSERR)) {
721 r |= 1;
722 rst = 1;
723 printf("%s: tx dma error ( ", sc->sc_dev.dv_xname);
724 if (why & BE_CR_STAT_TXLERR)
725 printf("Late ");
726 if (why & BE_CR_STAT_TXPERR)
727 printf("Parity ");
728 if (why & BE_CR_STAT_TXSERR)
729 printf("Generic ");
730 printf(")\n");
731 }
732
733 if (why & BE_CR_STAT_RXDROP) {
734 r |= 1;
735 rst = 1;
736 printf("%s: out of rx descriptors\n", sc->sc_dev.dv_xname);
737 }
738
739 if (why & BE_CR_STAT_RXSMALL) {
740 r |= 1;
741 rst = 1;
742 printf("%s: rx descriptor too small\n", sc->sc_dev.dv_xname);
743 }
744
745 if (why & (BE_CR_STAT_RXLERR | BE_CR_STAT_RXPERR | BE_CR_STAT_RXSERR)) {
746 r |= 1;
747 rst = 1;
748 printf("%s: rx dma error ( ", sc->sc_dev.dv_xname);
749 if (why & BE_CR_STAT_RXLERR)
750 printf("Late ");
751 if (why & BE_CR_STAT_RXPERR)
752 printf("Parity ");
753 if (why & BE_CR_STAT_RXSERR)
754 printf("Generic ");
755 printf(")\n");
756 }
757
758 if (!r) {
759 rst = 1;
760 printf("%s: unexpected error interrupt %08x\n",
761 sc->sc_dev.dv_xname, why);
762 }
763
764 if (rst) {
765 printf("%s: resetting\n", sc->sc_dev.dv_xname);
766 bereset(sc);
767 }
768
769 return (r);
770 }
771
772 /*
773 * Error interrupt.
774 */
775 int
776 beeint(sc, why)
777 struct be_softc *sc;
778 u_int32_t why;
779 {
780 int r = 0, rst = 0;
781
782 if (why & BE_BR_STAT_RFIFOVF) {
783 r |= 1;
784 rst = 1;
785 printf("%s: receive fifo overrun\n", sc->sc_dev.dv_xname);
786 }
787 if (why & BE_BR_STAT_TFIFO_UND) {
788 r |= 1;
789 rst = 1;
790 printf("%s: transmit fifo underrun\n", sc->sc_dev.dv_xname);
791 }
792 if (why & BE_BR_STAT_MAXPKTERR) {
793 r |= 1;
794 rst = 1;
795 printf("%s: max packet size error\n", sc->sc_dev.dv_xname);
796 }
797
798 if (!r) {
799 rst = 1;
800 printf("%s: unexpected error interrupt %08x\n",
801 sc->sc_dev.dv_xname, why);
802 }
803
804 if (rst) {
805 printf("%s: resetting\n", sc->sc_dev.dv_xname);
806 bereset(sc);
807 }
808
809 return (r);
810 }
811
812 /*
813 * Transmit interrupt.
814 */
815 int
816 betint(sc)
817 struct be_softc *sc;
818 {
819 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
820 bus_space_tag_t t = sc->sc_bustag;
821 bus_space_handle_t br = sc->sc_br;
822 unsigned int bix, txflags;
823
824 /*
825 * Unload collision counters
826 */
827 ifp->if_collisions +=
828 bus_space_read_4(t, br, BE_BRI_NCCNT) +
829 bus_space_read_4(t, br, BE_BRI_FCCNT) +
830 bus_space_read_4(t, br, BE_BRI_EXCNT) +
831 bus_space_read_4(t, br, BE_BRI_LTCNT);
832
833 /*
834 * the clear the hardware counters
835 */
836 bus_space_write_4(t, br, BE_BRI_NCCNT, 0);
837 bus_space_write_4(t, br, BE_BRI_FCCNT, 0);
838 bus_space_write_4(t, br, BE_BRI_EXCNT, 0);
839 bus_space_write_4(t, br, BE_BRI_LTCNT, 0);
840
841 bix = sc->sc_rb.rb_tdtail;
842
843 for (;;) {
844 if (sc->sc_rb.rb_td_nbusy <= 0)
845 break;
846
847 txflags = sc->sc_rb.rb_txd[bix].xd_flags;
848
849 if (txflags & QEC_XD_OWN)
850 break;
851
852 ifp->if_flags &= ~IFF_OACTIVE;
853 ifp->if_opackets++;
854
855 if (++bix == QEC_XD_RING_MAXSIZE)
856 bix = 0;
857
858 --sc->sc_rb.rb_td_nbusy;
859 }
860
861 sc->sc_rb.rb_tdtail = bix;
862
863 bestart(ifp);
864
865 if (sc->sc_rb.rb_td_nbusy == 0)
866 ifp->if_timer = 0;
867
868 return (1);
869 }
870
871 /*
872 * Receive interrupt.
873 */
874 int
875 berint(sc)
876 struct be_softc *sc;
877 {
878 struct qec_xd *xd = sc->sc_rb.rb_rxd;
879 unsigned int bix, len;
880 unsigned int nrbuf = sc->sc_rb.rb_nrbuf;
881
882 bix = sc->sc_rb.rb_rdtail;
883
884 /*
885 * Process all buffers with valid data.
886 */
887 for (;;) {
888 len = xd[bix].xd_flags;
889 if (len & QEC_XD_OWN)
890 break;
891
892 len &= QEC_XD_LENGTH;
893 be_read(sc, bix, len);
894
895 /* ... */
896 xd[(bix+nrbuf) % QEC_XD_RING_MAXSIZE].xd_flags =
897 QEC_XD_OWN | (BE_PKT_BUF_SZ & QEC_XD_LENGTH);
898
899 if (++bix == QEC_XD_RING_MAXSIZE)
900 bix = 0;
901 }
902
903 sc->sc_rb.rb_rdtail = bix;
904
905 return (1);
906 }
907
908 int
909 beioctl(ifp, cmd, data)
910 struct ifnet *ifp;
911 u_long cmd;
912 caddr_t data;
913 {
914 struct be_softc *sc = ifp->if_softc;
915 struct ifaddr *ifa = (struct ifaddr *)data;
916 struct ifreq *ifr = (struct ifreq *)data;
917 int s, error = 0;
918
919 s = splnet();
920
921 switch (cmd) {
922 case SIOCSIFADDR:
923 ifp->if_flags |= IFF_UP;
924 switch (ifa->ifa_addr->sa_family) {
925 #ifdef INET
926 case AF_INET:
927 beinit(sc);
928 arp_ifinit(ifp, ifa);
929 break;
930 #endif /* INET */
931 #ifdef NS
932 case AF_NS:
933 {
934 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
935
936 if (ns_nullhost(*ina))
937 ina->x_host =
938 *(union ns_host *)LLADDR(ifp->if_sadl);
939 else
940 bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
941 sizeof(sc->sc_enaddr));
942 /* Set new address. */
943 beinit(sc);
944 break;
945 }
946 #endif /* NS */
947 default:
948 beinit(sc);
949 break;
950 }
951 break;
952
953 case SIOCSIFFLAGS:
954 if ((ifp->if_flags & IFF_UP) == 0 &&
955 (ifp->if_flags & IFF_RUNNING) != 0) {
956 /*
957 * If interface is marked down and it is running, then
958 * stop it.
959 */
960 bestop(sc);
961 ifp->if_flags &= ~IFF_RUNNING;
962 } else if ((ifp->if_flags & IFF_UP) != 0 &&
963 (ifp->if_flags & IFF_RUNNING) == 0) {
964 /*
965 * If interface is marked up and it is stopped, then
966 * start it.
967 */
968 beinit(sc);
969 } else {
970 /*
971 * Reset the interface to pick up changes in any other
972 * flags that affect hardware registers.
973 */
974 bestop(sc);
975 beinit(sc);
976 }
977 #ifdef BEDEBUG
978 if (ifp->if_flags & IFF_DEBUG)
979 sc->sc_debug = 1;
980 else
981 sc->sc_debug = 0;
982 #endif
983 break;
984
985 case SIOCADDMULTI:
986 case SIOCDELMULTI:
987 error = (cmd == SIOCADDMULTI) ?
988 ether_addmulti(ifr, &sc->sc_ethercom):
989 ether_delmulti(ifr, &sc->sc_ethercom);
990
991 if (error == ENETRESET) {
992 /*
993 * Multicast list has changed; set the hardware filter
994 * accordingly.
995 */
996 be_mcreset(sc);
997 error = 0;
998 }
999 break;
1000 case SIOCGIFMEDIA:
1001 case SIOCSIFMEDIA:
1002 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1003 break;
1004 default:
1005 error = EINVAL;
1006 break;
1007 }
1008 splx(s);
1009 return (error);
1010 }
1011
1012
1013 void
1014 beinit(sc)
1015 struct be_softc *sc;
1016 {
1017 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1018 bus_space_tag_t t = sc->sc_bustag;
1019 bus_space_handle_t br = sc->sc_br;
1020 bus_space_handle_t cr = sc->sc_cr;
1021 struct qec_softc *qec = sc->sc_qec;
1022 u_int32_t qecaddr;
1023 u_int8_t *ea;
1024 int s;
1025
1026 s = splimp();
1027
1028 qec_meminit(&sc->sc_rb, BE_PKT_BUF_SZ);
1029 be_tcvr_init(sc);
1030
1031 be_ifmedia_upd(ifp);
1032
1033 bestop(sc);
1034
1035 ea = sc->sc_enaddr;
1036 bus_space_write_4(t, br, BE_BRI_MACADDR0, (ea[0] << 8) | ea[1]);
1037 bus_space_write_4(t, br, BE_BRI_MACADDR1, (ea[2] << 8) | ea[3]);
1038 bus_space_write_4(t, br, BE_BRI_MACADDR2, (ea[4] << 8) | ea[5]);
1039
1040 bus_space_write_4(t, br, BE_BRI_HASHTAB0, 0);
1041 bus_space_write_4(t, br, BE_BRI_HASHTAB1, 0);
1042 bus_space_write_4(t, br, BE_BRI_HASHTAB2, 0);
1043 bus_space_write_4(t, br, BE_BRI_HASHTAB3, 0);
1044
1045 be_mcreset(sc);
1046
1047 bus_space_write_4(t, br, BE_BRI_RANDSEED, 0xbd);
1048
1049 bus_space_write_4(t, br, BE_BRI_XIFCFG,
1050 BE_BR_XCFG_ODENABLE | BE_BR_XCFG_RESV);
1051
1052 bus_space_write_4(t, br, BE_BRI_JSIZE, 4);
1053
1054 /*
1055 * Turn off counter expiration interrupts as well as
1056 * 'gotframe' and 'sentframe'
1057 */
1058 bus_space_write_4(t, br, BE_BRI_IMASK,
1059 BE_BR_IMASK_GOTFRAME |
1060 BE_BR_IMASK_RCNTEXP |
1061 BE_BR_IMASK_ACNTEXP |
1062 BE_BR_IMASK_CCNTEXP |
1063 BE_BR_IMASK_LCNTEXP |
1064 BE_BR_IMASK_CVCNTEXP |
1065 BE_BR_IMASK_SENTFRAME |
1066 BE_BR_IMASK_NCNTEXP |
1067 BE_BR_IMASK_ECNTEXP |
1068 BE_BR_IMASK_LCCNTEXP |
1069 BE_BR_IMASK_FCNTEXP |
1070 BE_BR_IMASK_DTIMEXP);
1071
1072 /* Channel registers: */
1073 bus_space_write_4(t, cr, BE_CRI_RXDS, (u_int32_t)sc->sc_rb.rb_rxddma);
1074 bus_space_write_4(t, cr, BE_CRI_TXDS, (u_int32_t)sc->sc_rb.rb_txddma);
1075
1076 qecaddr = sc->sc_channel * qec->sc_msize;
1077 bus_space_write_4(t, cr, BE_CRI_RXWBUF, qecaddr);
1078 bus_space_write_4(t, cr, BE_CRI_RXRBUF, qecaddr);
1079 bus_space_write_4(t, cr, BE_CRI_TXWBUF, qecaddr + qec->sc_rsize);
1080 bus_space_write_4(t, cr, BE_CRI_TXRBUF, qecaddr + qec->sc_rsize);
1081
1082 bus_space_write_4(t, cr, BE_CRI_RIMASK, 0);
1083 bus_space_write_4(t, cr, BE_CRI_TIMASK, 0);
1084 bus_space_write_4(t, cr, BE_CRI_QMASK, 0);
1085 bus_space_write_4(t, cr, BE_CRI_BMASK, 0);
1086 bus_space_write_4(t, cr, BE_CRI_CCNT, 0);
1087
1088 /* Enable transmitter */
1089 bus_space_write_4(t, br, BE_BRI_TXCFG,
1090 BE_BR_TXCFG_FIFO | BE_BR_TXCFG_ENABLE);
1091
1092 /* Enable receiver */
1093 bus_space_write_4(t, br, BE_BRI_RXCFG,
1094 BE_BR_RXCFG_HENABLE | BE_BR_RXCFG_FIFO |
1095 BE_BR_RXCFG_ENABLE);
1096
1097 ifp->if_flags |= IFF_RUNNING;
1098 ifp->if_flags &= ~IFF_OACTIVE;
1099
1100 timeout(be_tick, sc, hz);
1101 splx(s);
1102 }
1103
1104 void
1105 be_mcreset(sc)
1106 struct be_softc *sc;
1107 {
1108 struct ethercom *ec = &sc->sc_ethercom;
1109 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1110 bus_space_tag_t t = sc->sc_bustag;
1111 bus_space_handle_t br = sc->sc_br;
1112 u_int32_t crc;
1113 u_int16_t hash[4];
1114 u_int8_t octet;
1115 u_int32_t v;
1116 int i, j;
1117 struct ether_multi *enm;
1118 struct ether_multistep step;
1119
1120 if (ifp->if_flags & IFF_PROMISC) {
1121 v = bus_space_read_4(t, br, BE_BRI_RXCFG);
1122 v |= BE_BR_RXCFG_PMISC;
1123 bus_space_write_4(t, br, BE_BRI_RXCFG, v);
1124 return;
1125 }
1126
1127 v = bus_space_read_4(t, br, BE_BRI_RXCFG);
1128 v &= ~BE_BR_RXCFG_PMISC;
1129 bus_space_write_4(t, br, BE_BRI_RXCFG, v);
1130
1131 if (ifp->if_flags & IFF_ALLMULTI) {
1132 bus_space_write_4(t, br, BE_BRI_HASHTAB0, 0xffff);
1133 bus_space_write_4(t, br, BE_BRI_HASHTAB1, 0xffff);
1134 bus_space_write_4(t, br, BE_BRI_HASHTAB2, 0xffff);
1135 bus_space_write_4(t, br, BE_BRI_HASHTAB3, 0xffff);
1136 return;
1137 }
1138
1139 hash[3] = hash[2] = hash[1] = hash[0] = 0;
1140
1141 ETHER_FIRST_MULTI(step, ec, enm);
1142 while (enm != NULL) {
1143 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1144 /*
1145 * We must listen to a range of multicast
1146 * addresses. For now, just accept all
1147 * multicasts, rather than trying to set only
1148 * those filter bits needed to match the range.
1149 * (At this time, the only use of address
1150 * ranges is for IP multicast routing, for
1151 * which the range is big enough to require
1152 * all bits set.)
1153 */
1154 bus_space_write_4(t, br, BE_BRI_HASHTAB0, 0xffff);
1155 bus_space_write_4(t, br, BE_BRI_HASHTAB1, 0xffff);
1156 bus_space_write_4(t, br, BE_BRI_HASHTAB2, 0xffff);
1157 bus_space_write_4(t, br, BE_BRI_HASHTAB3, 0xffff);
1158 ifp->if_flags |= IFF_ALLMULTI;
1159 return;
1160 }
1161
1162 crc = 0xffffffff;
1163
1164 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1165 octet = enm->enm_addrlo[i];
1166
1167 for (j = 0; j < 8; j++) {
1168 if ((crc & 1) ^ (octet & 1)) {
1169 crc >>= 1;
1170 crc ^= MC_POLY_LE;
1171 }
1172 else
1173 crc >>= 1;
1174 octet >>= 1;
1175 }
1176 }
1177
1178 crc >>= 26;
1179 hash[crc >> 4] |= 1 << (crc & 0xf);
1180 ETHER_NEXT_MULTI(step, enm);
1181 }
1182
1183 bus_space_write_4(t, br, BE_BRI_HASHTAB0, hash[0]);
1184 bus_space_write_4(t, br, BE_BRI_HASHTAB1, hash[1]);
1185 bus_space_write_4(t, br, BE_BRI_HASHTAB2, hash[2]);
1186 bus_space_write_4(t, br, BE_BRI_HASHTAB3, hash[3]);
1187 ifp->if_flags &= ~IFF_ALLMULTI;
1188 }
1189
1190 /*
1191 * Set the tcvr to an idle state
1192 */
1193 void
1194 be_mii_sync(sc)
1195 struct be_softc *sc;
1196 {
1197 bus_space_tag_t t = sc->sc_bustag;
1198 bus_space_handle_t tr = sc->sc_tr;
1199 int n = 20;
1200
1201 while (n--) {
1202 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1203 MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO |
1204 MGMT_PAL_OENAB);
1205 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1206 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1207 MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO |
1208 MGMT_PAL_OENAB | MGMT_PAL_DCLOCK);
1209 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1210 }
1211 }
1212
1213 /*
1214 * Initialize the transceiver and figure out whether we're using the
1215 * external or internal one.
1216 */
1217 void
1218 be_tcvr_init(sc)
1219 struct be_softc *sc;
1220 {
1221 bus_space_tag_t t = sc->sc_bustag;
1222 bus_space_handle_t tr = sc->sc_tr;
1223 u_int32_t v;
1224
1225 be_mii_sync(sc);
1226
1227 if (sc->sc_rev != 1) {
1228 printf("%s: rev %d PAL not supported.\n",
1229 sc->sc_dev.dv_xname,
1230 sc->sc_rev);
1231 return;
1232 }
1233
1234 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1235 MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO |
1236 MGMT_PAL_DCLOCK);
1237 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1238
1239 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1240 MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO);
1241 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1242 DELAY(200);
1243
1244 v = bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1245 #ifdef BEDEBUG
1246 if (sc->sc_debug != 0) {
1247 char bits[64];
1248 printf("be_tcvr_init: MGMTPAL=%s\n",
1249 bitmask_snprintf(v, MGMT_PAL_BITS, bits, sizeof(bits)));
1250 }
1251 #endif
1252 {
1253 char bits[64];
1254 printf("be_tcvr_init: MGMTPAL=%s\n",
1255 bitmask_snprintf(v, MGMT_PAL_BITS, bits, sizeof(bits)));
1256 if ((v & MGMT_PAL_EXT_MDIO) != 0) {
1257 printf("EXTERNAL\n");
1258 }
1259 }
1260 if ((v & MGMT_PAL_EXT_MDIO) != 0) {
1261 sc->sc_conf |= BE_CONF_MII;
1262 /*sc->sc_tcvr_type = BE_TCVR_EXTERNAL;*/
1263 bus_space_write_4(t, tr, BE_TRI_TCVRPAL,
1264 ~(TCVR_PAL_EXTLBACK | TCVR_PAL_MSENSE |
1265 TCVR_PAL_LTENABLE));
1266
1267 (void)bus_space_read_4(t, tr, BE_TRI_TCVRPAL);
1268 } else if ((v & MGMT_PAL_INT_MDIO) != 0) {
1269 /*sc->sc_tcvr_type = BE_TCVR_INTERNAL;*/
1270 bus_space_write_4(t, tr, BE_TRI_TCVRPAL,
1271 ~(TCVR_PAL_EXTLBACK | TCVR_PAL_MSENSE |
1272 TCVR_PAL_LTENABLE | TCVR_PAL_SERIAL));
1273 (void)bus_space_read_4(t, tr, BE_TRI_TCVRPAL);
1274 } else {
1275 printf("%s: no internal or external transceiver found.\n",
1276 sc->sc_dev.dv_xname);
1277 }
1278 }
1279
1280
1281 static __inline__ int
1282 be_tcvr_read_bit(sc, phy)
1283 struct be_softc *sc;
1284 int phy;
1285 {
1286 bus_space_tag_t t = sc->sc_bustag;
1287 bus_space_handle_t tr = sc->sc_tr;
1288 int ret;
1289
1290 if (phy == BE_PHY_INTERNAL) {
1291 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, MGMT_PAL_EXT_MDIO);
1292 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1293 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1294 MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK);
1295 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1296 DELAY(20);
1297 ret = (bus_space_read_4(t, tr, BE_TRI_MGMTPAL) &
1298 MGMT_PAL_INT_MDIO) >> 3;
1299 } else {
1300 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, MGMT_PAL_INT_MDIO);
1301 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1302 DELAY(20);
1303 ret = (bus_space_read_4(t, tr, BE_TRI_MGMTPAL) &
1304 MGMT_PAL_EXT_MDIO) >> 2;
1305 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1306 MGMT_PAL_INT_MDIO | MGMT_PAL_DCLOCK);
1307 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1308 }
1309
1310 return (ret);
1311 }
1312
1313 static __inline__ void
1314 be_tcvr_write_bit(sc, phy, bit)
1315 struct be_softc *sc;
1316 int phy;
1317 int bit;
1318 {
1319 bus_space_tag_t t = sc->sc_bustag;
1320 bus_space_handle_t tr = sc->sc_tr;
1321
1322 if (phy == BE_PHY_INTERNAL) {
1323 bit = ((bit & 1) << 3) | MGMT_PAL_OENAB | MGMT_PAL_EXT_MDIO;
1324 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, bit);
1325 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1326
1327 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, bit | MGMT_PAL_DCLOCK);
1328 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1329 } else {
1330 bit = ((bit & 1) << 2) | MGMT_PAL_OENAB | MGMT_PAL_INT_MDIO;
1331 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, bit);
1332 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1333 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, bit | MGMT_PAL_DCLOCK);
1334 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1335 }
1336 }
1337
1338 static __inline__ void
1339 be_mii_sendbits(sc, phy, data, nbits)
1340 struct be_softc *sc;
1341 int phy;
1342 u_int32_t data;
1343 int nbits;
1344 {
1345 int i;
1346
1347 for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
1348 be_tcvr_write_bit(sc, phy, (data & i) != 0);
1349 }
1350 }
1351
1352 static int
1353 be_mii_readreg(self, phy, reg)
1354 struct device *self;
1355 int phy, reg;
1356 {
1357 struct be_softc *sc = (struct be_softc *)self;
1358 int val = 0, i;
1359
1360 /* The `be' internal PHY is not treated as an MII device */
1361 if (phy == BE_PHY_INTERNAL)
1362 return (0);
1363
1364 /*
1365 * Read the PHY register by manually driving the MII control lines.
1366 */
1367 be_mii_sync(sc);
1368 be_mii_sendbits(sc, phy, MII_COMMAND_START, 2);
1369 be_mii_sendbits(sc, phy, MII_COMMAND_READ, 2);
1370 be_mii_sendbits(sc, phy, phy, 5);
1371 be_mii_sendbits(sc, phy, reg, 5);
1372
1373 (void) be_tcvr_read_bit(sc, phy);
1374 (void) be_tcvr_read_bit(sc, phy);
1375
1376 for (i = 15; i >= 0; i--)
1377 val |= (be_tcvr_read_bit(sc, phy) << i);
1378
1379 (void) be_tcvr_read_bit(sc, phy);
1380 (void) be_tcvr_read_bit(sc, phy);
1381 (void) be_tcvr_read_bit(sc, phy);
1382
1383 return (val);
1384 }
1385
1386 void
1387 be_mii_writereg(self, phy, reg, val)
1388 struct device *self;
1389 int phy, reg, val;
1390 {
1391 struct be_softc *sc = (struct be_softc *)self;
1392 int i;
1393
1394 /*
1395 * Write the PHY register by manually driving the MII control lines.
1396 */
1397 be_mii_sync(sc);
1398 be_mii_sendbits(sc, phy, MII_COMMAND_START, 2);
1399 be_mii_sendbits(sc, phy, MII_COMMAND_WRITE, 2);
1400 be_mii_sendbits(sc, phy, phy, 5);
1401 be_mii_sendbits(sc, phy, reg, 5);
1402
1403 be_tcvr_write_bit(sc, phy, 1);
1404 be_tcvr_write_bit(sc, phy, 0);
1405
1406 for (i = 15; i >= 0; i--)
1407 be_tcvr_write_bit(sc, phy, (val >> i) & 1);
1408 }
1409
1410 int
1411 be_mii_reset(sc, phy)
1412 struct be_softc *sc;
1413 int phy;
1414 {
1415 int n;
1416
1417 be_mii_writereg((struct device *)sc, phy, MII_BMCR,
1418 BMCR_LOOP | BMCR_PDOWN | BMCR_ISO);
1419 be_mii_writereg((struct device *)sc, phy, MII_BMCR, BMCR_RESET);
1420
1421 for (n = 16; n >= 0; n--) {
1422 int bmcr = be_mii_readreg((struct device *)sc, phy, MII_BMCR);
1423 if ((bmcr & BMCR_RESET) == 0)
1424 break;
1425 DELAY(20);
1426 }
1427 if (n == 0) {
1428 printf("%s: bmcr reset failed\n", sc->sc_dev.dv_xname);
1429 return (EIO);
1430 }
1431 return (0);
1432 }
1433
1434 void
1435 be_statchg(self)
1436 struct device *self;
1437 {
1438 struct be_softc *sc = (struct be_softc *)self;
1439
1440 printf("be_statchg: media_active=%x\n", sc->sc_mii.mii_media_active);
1441 }
1442
1443 void
1444 be_tick(arg)
1445 void *arg;
1446 {
1447 struct be_softc *sc = arg;
1448 int s = splnet();
1449
1450 if ((sc->sc_conf & BE_CONF_MII) != 0)
1451 mii_tick(&sc->sc_mii);
1452 else
1453 be_internal_phy_auto(sc);
1454
1455 splx(s);
1456 timeout(be_tick, sc, hz);
1457 }
1458
1459 void
1460 be_internal_phy_auto(sc)
1461 struct be_softc *sc;
1462 {
1463 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1464 int bmcr, bmsr;
1465
1466 /*
1467 * Check link status; if we don't have a link, try another
1468 * speed. We can't detect duplex mode, so half-duplex is
1469 * what we have to settle for.
1470 */
1471
1472 /* Only used for automatic media selection */
1473 if (IFM_SUBTYPE(sc->sc_media.ifm_cur->ifm_media) != IFM_AUTO)
1474 return;
1475
1476 /* Don't bother if interface isn't up */
1477 if ((ifp->if_flags & IFF_UP) == 0)
1478 return;
1479
1480 /* Read twice in case the register is latched */
1481 bmsr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMSR)|
1482 be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMSR);
1483
1484 if ((bmsr & BMSR_LINK) != 0) {
1485 /* We have a carrier */
1486 return;
1487 }
1488
1489 bmcr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMCR);
1490 /* Just flip the fast speed bit */
1491 bmcr ^= BMCR_S100;
1492 be_mii_writereg((struct device *)sc, BE_PHY_INTERNAL, MII_BMCR, bmcr);
1493 }
1494
1495 /*
1496 * Get current media settings.
1497 */
1498 void
1499 be_ifmedia_sts(ifp, ifmr)
1500 struct ifnet *ifp;
1501 struct ifmediareq *ifmr;
1502 {
1503 struct be_softc *sc = ifp->if_softc;
1504 int bmcr, bmsr;
1505
1506 if ((sc->sc_conf & BE_CONF_MII) != 0) {
1507 mii_pollstat(&sc->sc_mii);
1508 ifmr->ifm_status = sc->sc_mii.mii_media_status;
1509 ifmr->ifm_active = sc->sc_mii.mii_media_active;
1510 return;
1511 }
1512
1513 /*
1514 * Internal transceiver; do the work here.
1515 */
1516 bmcr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMCR);
1517
1518 switch (bmcr & (BMCR_S100 | BMCR_FDX)) {
1519 case (BMCR_S100 | BMCR_FDX):
1520 ifmr->ifm_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
1521 break;
1522 case BMCR_S100:
1523 ifmr->ifm_active = IFM_ETHER | IFM_100_TX | IFM_HDX;
1524 break;
1525 case BMCR_FDX:
1526 ifmr->ifm_active = IFM_ETHER | IFM_10_T | IFM_FDX;
1527 break;
1528 case 0:
1529 ifmr->ifm_active = IFM_ETHER | IFM_10_T | IFM_HDX;
1530 break;
1531 }
1532
1533 /* Read twice in case the register is latched */
1534 bmsr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMSR)|
1535 be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMSR);
1536 if (bmsr & BMSR_LINK)
1537 ifmr->ifm_status |= IFM_AVALID | IFM_ACTIVE;
1538 else {
1539 ifmr->ifm_status |= IFM_AVALID;
1540 ifmr->ifm_status &= ~IFM_ACTIVE;
1541 }
1542 }
1543
1544 /*
1545 * Set media options.
1546 */
1547 int
1548 be_ifmedia_upd(ifp)
1549 struct ifnet *ifp;
1550 {
1551 struct be_softc *sc = ifp->if_softc;
1552 struct ifmedia *ifm = &sc->sc_media;
1553 int newmedia = ifm->ifm_media;
1554 int n, error, phy, bmcr;
1555 char *speed, *mode;
1556 u_int32_t v;
1557 bus_space_tag_t t = sc->sc_bustag;
1558 bus_space_handle_t br = sc->sc_br;
1559
1560 if (IFM_TYPE(newmedia) != IFM_ETHER)
1561 return (EINVAL);
1562
1563 if ((sc->sc_conf & BE_CONF_MII) != 0) {
1564 int error;
1565
1566 if ((error = mii_mediachg(&sc->sc_mii)) != 0)
1567 return (error);
1568
1569 v = bus_space_read_4(t, br, BE_BRI_TXCFG);
1570 if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
1571 v |= BE_BR_TXCFG_FULLDPLX;
1572 else
1573 v &= ~BE_BR_TXCFG_FULLDPLX;
1574 bus_space_write_4(t, br, BE_BRI_TXCFG, v);
1575
1576 return (0);
1577 }
1578
1579 /*
1580 * The rest of this routine is devoted to the
1581 * not-quite-a-phy internal transceiver case.
1582 */
1583 phy = BE_PHY_INTERNAL;
1584
1585 /* Why must we reset the device? */
1586 if ((error = be_mii_reset(sc, phy)) != 0)
1587 return (error);
1588
1589 bmcr = be_mii_readreg((struct device *)sc, phy, MII_BMCR);
1590
1591 if (IFM_SUBTYPE(newmedia) == IFM_100_TX) {
1592 bmcr |= BMCR_S100;
1593 speed = "100baseTX";
1594 } else if (IFM_SUBTYPE(newmedia) == IFM_10_T) {
1595 bmcr &= ~BMCR_S100;
1596 speed = "10baseT";
1597 } else {
1598 speed = "auto sense";
1599 }
1600
1601 printf("%s: selecting %s", sc->sc_dev.dv_xname, speed);
1602
1603 v = bus_space_read_4(t, br, BE_BRI_TXCFG);
1604 if ((IFM_OPTIONS(newmedia) & IFM_FDX) != 0) {
1605 bmcr |= BMCR_FDX;
1606 v |= BE_BR_TXCFG_FULLDPLX;
1607 mode = "full";
1608 } else {
1609 bmcr &= ~BMCR_FDX;
1610 v &= ~BE_BR_TXCFG_FULLDPLX;
1611 mode = "half";
1612 }
1613 bus_space_write_4(t, br, BE_BRI_TXCFG, v);
1614 printf(" %s-duplex\n", mode);
1615
1616 /* Select the new mode and take out of isolation */
1617 be_mii_writereg((struct device *)sc, phy, MII_BMCR, bmcr & ~BMCR_ISO);
1618
1619 for (n = 32; n >= 0; n--) {
1620 bmcr = be_mii_readreg((struct device *)sc, phy, MII_BMCR);
1621 if ((bmcr & BMCR_ISO) == 0)
1622 break;
1623 DELAY(20);
1624 }
1625 if (n == 0) {
1626 printf("%s: bmcr unisolate failed\n", sc->sc_dev.dv_xname);
1627 return (EIO);
1628 }
1629
1630 return (0);
1631 }
1632