be.c revision 1.6 1 /* $NetBSD: be.c,v 1.6 1999/05/18 23:52:58 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);
335 #else
336 /* TEST */
337 extern int mii_print __P((void *, const char *));
338 struct mii_attach_args ma;
339 struct mii_softc *child;
340
341 bzero(&ma, sizeof(ma));
342 ma.mii_phyno = BE_PHY_INTERNAL;
343 ma.mii_data = mii;
344 ma.mii_capmask = 0xffffffff;
345 if ((child = (struct mii_softc *)
346 config_found(&sc->sc_dev, &ma, mii_print)) != NULL) {
347 /*
348 * Link it up in the parent's MII data.
349 */
350 LIST_INSERT_HEAD(&mii->mii_phys, child, mii_list);
351 mii->mii_instance++;
352 }
353 #endif
354
355 if (LIST_FIRST(&sc->sc_mii.mii_phys) == NULL) {
356 /* No PHY attached */
357 ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_NONE, 0, NULL);
358 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_NONE);
359 } else {
360 /*
361 * XXX - we can really do the following ONLY if the
362 * phy indeed has the auto negotiation capability!!
363 */
364 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_AUTO);
365 }
366 } else {
367 /*
368 * The be internal phy looks vaguely like MII hardware,
369 * but not enough to be able to use the MII device
370 * layer. Hence, we have to take care of media selection
371 * ourselves.
372 */
373
374 /* Use `ifm_data' to store BMCR bits */
375 ifmedia_add(&sc->sc_media,
376 IFM_MAKEWORD(IFM_ETHER,IFM_10_T,0,0),
377 0, NULL);
378 ifmedia_add(&sc->sc_media,
379 IFM_MAKEWORD(IFM_ETHER,IFM_10_T,IFM_FDX,0),
380 BMCR_FDX, NULL);
381 ifmedia_add(&sc->sc_media,
382 IFM_MAKEWORD(IFM_ETHER,IFM_100_TX,0,0),
383 BMCR_S100, NULL);
384 ifmedia_add(&sc->sc_media,
385 IFM_MAKEWORD(IFM_ETHER,IFM_100_TX,IFM_FDX,0),
386 BMCR_S100|BMCR_FDX, NULL);
387 ifmedia_add(&sc->sc_media,
388 IFM_MAKEWORD(IFM_ETHER,IFM_AUTO,0,0),
389 0, NULL);
390 ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_AUTO);
391 }
392
393 bcopy(sc->sc_dev.dv_xname, ifp->if_xname, IFNAMSIZ);
394 ifp->if_softc = sc;
395 ifp->if_start = bestart;
396 ifp->if_ioctl = beioctl;
397 ifp->if_watchdog = bewatchdog;
398 ifp->if_flags =
399 IFF_BROADCAST | IFF_SIMPLEX | IFF_NOTRAILERS | IFF_MULTICAST;
400
401 /* Attach the interface. */
402 if_attach(ifp);
403 ether_ifattach(ifp, sc->sc_enaddr);
404
405 #if NBPFILTER > 0
406 bpfattach(&ifp->if_bpf, ifp, DLT_EN10MB,
407 sizeof(struct ether_header));
408 #endif
409 }
410
411
412 /*
413 * Routine to copy from mbuf chain to transmit buffer in
414 * network buffer memory.
415 */
416 static __inline__ int
417 be_put(sc, idx, m)
418 struct be_softc *sc;
419 int idx;
420 struct mbuf *m;
421 {
422 struct mbuf *n;
423 int len, tlen = 0, boff = 0;
424 caddr_t bp;
425
426 bp = sc->sc_rb.rb_txbuf + (idx % sc->sc_rb.rb_ntbuf) * BE_PKT_BUF_SZ;
427
428 for (; m; m = n) {
429 len = m->m_len;
430 if (len == 0) {
431 MFREE(m, n);
432 continue;
433 }
434 bcopy(mtod(m, caddr_t), bp+boff, len);
435 boff += len;
436 tlen += len;
437 MFREE(m, n);
438 }
439 return (tlen);
440 }
441
442 /*
443 * Pull data off an interface.
444 * Len is the length of data, with local net header stripped.
445 * We copy the data into mbufs. When full cluster sized units are present,
446 * we copy into clusters.
447 */
448 static __inline__ struct mbuf *
449 be_get(sc, idx, totlen)
450 struct be_softc *sc;
451 int idx, totlen;
452 {
453 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
454 struct mbuf *m;
455 struct mbuf *top, **mp;
456 int len, pad, boff = 0;
457 caddr_t bp;
458
459 bp = sc->sc_rb.rb_rxbuf + (idx % sc->sc_rb.rb_nrbuf) * BE_PKT_BUF_SZ;
460
461 MGETHDR(m, M_DONTWAIT, MT_DATA);
462 if (m == NULL)
463 return (NULL);
464 m->m_pkthdr.rcvif = ifp;
465 m->m_pkthdr.len = totlen;
466
467 pad = ALIGN(sizeof(struct ether_header)) - sizeof(struct ether_header);
468 m->m_data += pad;
469 len = MHLEN - pad;
470 top = NULL;
471 mp = ⊤
472
473 while (totlen > 0) {
474 if (top) {
475 MGET(m, M_DONTWAIT, MT_DATA);
476 if (m == NULL) {
477 m_freem(top);
478 return (NULL);
479 }
480 len = MLEN;
481 }
482 if (top && totlen >= MINCLSIZE) {
483 MCLGET(m, M_DONTWAIT);
484 if (m->m_flags & M_EXT)
485 len = MCLBYTES;
486 }
487 m->m_len = len = min(totlen, len);
488 bcopy(bp + boff, mtod(m, caddr_t), len);
489 boff += len;
490 totlen -= len;
491 *mp = m;
492 mp = &m->m_next;
493 }
494
495 return (top);
496 }
497
498 /*
499 * Pass a packet to the higher levels.
500 */
501 static __inline__ void
502 be_read(sc, idx, len)
503 struct be_softc *sc;
504 int idx, len;
505 {
506 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
507 struct mbuf *m;
508
509 if (len <= sizeof(struct ether_header) ||
510 len > ETHERMTU + sizeof(struct ether_header)) {
511
512 printf("%s: invalid packet size %d; dropping\n",
513 ifp->if_xname, len);
514
515 ifp->if_ierrors++;
516 return;
517 }
518
519 /*
520 * Pull packet off interface.
521 */
522 m = be_get(sc, idx, len);
523 if (m == NULL) {
524 ifp->if_ierrors++;
525 return;
526 }
527 ifp->if_ipackets++;
528
529 #if NBPFILTER > 0
530 /*
531 * Check if there's a BPF listener on this interface.
532 * If so, hand off the raw packet to BPF.
533 */
534 if (ifp->if_bpf)
535 bpf_mtap(ifp->if_bpf, m);
536 #endif
537 /* Pass the packet up. */
538 (*ifp->if_input)(ifp, m);
539 }
540
541 /*
542 * Start output on interface.
543 * We make two assumptions here:
544 * 1) that the current priority is set to splnet _before_ this code
545 * is called *and* is returned to the appropriate priority after
546 * return
547 * 2) that the IFF_OACTIVE flag is checked before this code is called
548 * (i.e. that the output part of the interface is idle)
549 */
550 void
551 bestart(ifp)
552 struct ifnet *ifp;
553 {
554 struct be_softc *sc = (struct be_softc *)ifp->if_softc;
555 struct qec_xd *txd = sc->sc_rb.rb_txd;
556 struct mbuf *m;
557 unsigned int bix, len;
558 unsigned int ntbuf = sc->sc_rb.rb_ntbuf;
559
560 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
561 return;
562
563 bix = sc->sc_rb.rb_tdhead;
564
565 for (;;) {
566 IF_DEQUEUE(&ifp->if_snd, m);
567 if (m == 0)
568 break;
569
570 #if NBPFILTER > 0
571 /*
572 * If BPF is listening on this interface, let it see the
573 * packet before we commit it to the wire.
574 */
575 if (ifp->if_bpf)
576 bpf_mtap(ifp->if_bpf, m);
577 #endif
578
579 /*
580 * Copy the mbuf chain into the transmit buffer.
581 */
582 len = be_put(sc, bix, m);
583
584 /*
585 * Initialize transmit registers and start transmission
586 */
587 txd[bix].xd_flags = QEC_XD_OWN | QEC_XD_SOP | QEC_XD_EOP |
588 (len & QEC_XD_LENGTH);
589 bus_space_write_4(sc->sc_bustag, sc->sc_cr, BE_CRI_CTRL,
590 BE_CR_CTRL_TWAKEUP);
591
592 if (++bix == QEC_XD_RING_MAXSIZE)
593 bix = 0;
594
595 if (++sc->sc_rb.rb_td_nbusy == ntbuf) {
596 ifp->if_flags |= IFF_OACTIVE;
597 break;
598 }
599 }
600
601 sc->sc_rb.rb_tdhead = bix;
602 }
603
604 void
605 bestop(sc)
606 struct be_softc *sc;
607 {
608 int n;
609 bus_space_tag_t t = sc->sc_bustag;
610 bus_space_handle_t br = sc->sc_br;
611
612 untimeout(be_tick, sc);
613
614 /* Stop the transmitter */
615 bus_space_write_4(t, br, BE_BRI_TXCFG, 0);
616 for (n = 32; n > 0; n--) {
617 if (bus_space_read_4(t, br, BE_BRI_TXCFG) == 0)
618 break;
619 DELAY(20);
620 }
621
622 /* Stop the receiver */
623 bus_space_write_4(t, br, BE_BRI_RXCFG, 0);
624 for (n = 32; n > 0; n--) {
625 if (bus_space_read_4(t, br, BE_BRI_RXCFG) == 0)
626 break;
627 DELAY(20);
628 }
629 }
630
631 /*
632 * Reset interface.
633 */
634 void
635 bereset(sc)
636 struct be_softc *sc;
637 {
638 int s;
639
640 s = splnet();
641 bestop(sc);
642 beinit(sc);
643 splx(s);
644 }
645
646 void
647 bewatchdog(ifp)
648 struct ifnet *ifp;
649 {
650 struct be_softc *sc = ifp->if_softc;
651
652 log(LOG_ERR, "%s: device timeout\n", sc->sc_dev.dv_xname);
653 ++sc->sc_ethercom.ec_if.if_oerrors;
654
655 bereset(sc);
656 }
657
658 int
659 beintr(v)
660 void *v;
661 {
662 struct be_softc *sc = (struct be_softc *)v;
663 bus_space_tag_t t = sc->sc_bustag;
664 u_int32_t whyq, whyb, whyc;
665 int r = 0;
666
667 /* Read QEC status, channel status and BE status */
668 whyq = bus_space_read_4(t, sc->sc_qr, QEC_QRI_STAT);
669 whyc = bus_space_read_4(t, sc->sc_cr, BE_CRI_STAT);
670 whyb = bus_space_read_4(t, sc->sc_br, BE_BRI_STAT);
671
672 if (whyq & QEC_STAT_BM)
673 r |= beeint(sc, whyb);
674
675 if (whyq & QEC_STAT_ER)
676 r |= beqint(sc, whyc);
677
678 if (whyq & QEC_STAT_TX && whyc & BE_CR_STAT_TXIRQ)
679 r |= betint(sc);
680
681 if (whyq & QEC_STAT_RX && whyc & BE_CR_STAT_RXIRQ)
682 r |= berint(sc);
683
684 return (r);
685 }
686
687 /*
688 * QEC Interrupt.
689 */
690 int
691 beqint(sc, why)
692 struct be_softc *sc;
693 u_int32_t why;
694 {
695 int r = 0, rst = 0;
696
697 if (why & BE_CR_STAT_TXIRQ)
698 r |= 1;
699 if (why & BE_CR_STAT_RXIRQ)
700 r |= 1;
701
702 if (why & BE_CR_STAT_BERROR) {
703 r |= 1;
704 rst = 1;
705 printf("%s: bigmac error\n", sc->sc_dev.dv_xname);
706 }
707
708 if (why & BE_CR_STAT_TXDERR) {
709 r |= 1;
710 rst = 1;
711 printf("%s: bogus tx descriptor\n", sc->sc_dev.dv_xname);
712 }
713
714 if (why & (BE_CR_STAT_TXLERR | BE_CR_STAT_TXPERR | BE_CR_STAT_TXSERR)) {
715 r |= 1;
716 rst = 1;
717 printf("%s: tx dma error ( ", sc->sc_dev.dv_xname);
718 if (why & BE_CR_STAT_TXLERR)
719 printf("Late ");
720 if (why & BE_CR_STAT_TXPERR)
721 printf("Parity ");
722 if (why & BE_CR_STAT_TXSERR)
723 printf("Generic ");
724 printf(")\n");
725 }
726
727 if (why & BE_CR_STAT_RXDROP) {
728 r |= 1;
729 rst = 1;
730 printf("%s: out of rx descriptors\n", sc->sc_dev.dv_xname);
731 }
732
733 if (why & BE_CR_STAT_RXSMALL) {
734 r |= 1;
735 rst = 1;
736 printf("%s: rx descriptor too small\n", sc->sc_dev.dv_xname);
737 }
738
739 if (why & (BE_CR_STAT_RXLERR | BE_CR_STAT_RXPERR | BE_CR_STAT_RXSERR)) {
740 r |= 1;
741 rst = 1;
742 printf("%s: rx dma error ( ", sc->sc_dev.dv_xname);
743 if (why & BE_CR_STAT_RXLERR)
744 printf("Late ");
745 if (why & BE_CR_STAT_RXPERR)
746 printf("Parity ");
747 if (why & BE_CR_STAT_RXSERR)
748 printf("Generic ");
749 printf(")\n");
750 }
751
752 if (!r) {
753 rst = 1;
754 printf("%s: unexpected error interrupt %08x\n",
755 sc->sc_dev.dv_xname, why);
756 }
757
758 if (rst) {
759 printf("%s: resetting\n", sc->sc_dev.dv_xname);
760 bereset(sc);
761 }
762
763 return (r);
764 }
765
766 /*
767 * Error interrupt.
768 */
769 int
770 beeint(sc, why)
771 struct be_softc *sc;
772 u_int32_t why;
773 {
774 int r = 0, rst = 0;
775
776 if (why & BE_BR_STAT_RFIFOVF) {
777 r |= 1;
778 rst = 1;
779 printf("%s: receive fifo overrun\n", sc->sc_dev.dv_xname);
780 }
781 if (why & BE_BR_STAT_TFIFO_UND) {
782 r |= 1;
783 rst = 1;
784 printf("%s: transmit fifo underrun\n", sc->sc_dev.dv_xname);
785 }
786 if (why & BE_BR_STAT_MAXPKTERR) {
787 r |= 1;
788 rst = 1;
789 printf("%s: max packet size error\n", sc->sc_dev.dv_xname);
790 }
791
792 if (!r) {
793 rst = 1;
794 printf("%s: unexpected error interrupt %08x\n",
795 sc->sc_dev.dv_xname, why);
796 }
797
798 if (rst) {
799 printf("%s: resetting\n", sc->sc_dev.dv_xname);
800 bereset(sc);
801 }
802
803 return (r);
804 }
805
806 /*
807 * Transmit interrupt.
808 */
809 int
810 betint(sc)
811 struct be_softc *sc;
812 {
813 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
814 bus_space_tag_t t = sc->sc_bustag;
815 bus_space_handle_t br = sc->sc_br;
816 unsigned int bix, txflags;
817
818 /*
819 * Unload collision counters
820 */
821 ifp->if_collisions +=
822 bus_space_read_4(t, br, BE_BRI_NCCNT) +
823 bus_space_read_4(t, br, BE_BRI_FCCNT) +
824 bus_space_read_4(t, br, BE_BRI_EXCNT) +
825 bus_space_read_4(t, br, BE_BRI_LTCNT);
826
827 /*
828 * the clear the hardware counters
829 */
830 bus_space_write_4(t, br, BE_BRI_NCCNT, 0);
831 bus_space_write_4(t, br, BE_BRI_FCCNT, 0);
832 bus_space_write_4(t, br, BE_BRI_EXCNT, 0);
833 bus_space_write_4(t, br, BE_BRI_LTCNT, 0);
834
835 bix = sc->sc_rb.rb_tdtail;
836
837 for (;;) {
838 if (sc->sc_rb.rb_td_nbusy <= 0)
839 break;
840
841 txflags = sc->sc_rb.rb_txd[bix].xd_flags;
842
843 if (txflags & QEC_XD_OWN)
844 break;
845
846 ifp->if_flags &= ~IFF_OACTIVE;
847 ifp->if_opackets++;
848
849 if (++bix == QEC_XD_RING_MAXSIZE)
850 bix = 0;
851
852 --sc->sc_rb.rb_td_nbusy;
853 }
854
855 sc->sc_rb.rb_tdtail = bix;
856
857 bestart(ifp);
858
859 if (sc->sc_rb.rb_td_nbusy == 0)
860 ifp->if_timer = 0;
861
862 return (1);
863 }
864
865 /*
866 * Receive interrupt.
867 */
868 int
869 berint(sc)
870 struct be_softc *sc;
871 {
872 struct qec_xd *xd = sc->sc_rb.rb_rxd;
873 unsigned int bix, len;
874 unsigned int nrbuf = sc->sc_rb.rb_nrbuf;
875
876 bix = sc->sc_rb.rb_rdtail;
877
878 /*
879 * Process all buffers with valid data.
880 */
881 for (;;) {
882 len = xd[bix].xd_flags;
883 if (len & QEC_XD_OWN)
884 break;
885
886 len &= QEC_XD_LENGTH;
887 be_read(sc, bix, len);
888
889 /* ... */
890 xd[(bix+nrbuf) % QEC_XD_RING_MAXSIZE].xd_flags =
891 QEC_XD_OWN | (BE_PKT_BUF_SZ & QEC_XD_LENGTH);
892
893 if (++bix == QEC_XD_RING_MAXSIZE)
894 bix = 0;
895 }
896
897 sc->sc_rb.rb_rdtail = bix;
898
899 return (1);
900 }
901
902 int
903 beioctl(ifp, cmd, data)
904 struct ifnet *ifp;
905 u_long cmd;
906 caddr_t data;
907 {
908 struct be_softc *sc = ifp->if_softc;
909 struct ifaddr *ifa = (struct ifaddr *)data;
910 struct ifreq *ifr = (struct ifreq *)data;
911 int s, error = 0;
912
913 s = splnet();
914
915 switch (cmd) {
916 case SIOCSIFADDR:
917 ifp->if_flags |= IFF_UP;
918 switch (ifa->ifa_addr->sa_family) {
919 #ifdef INET
920 case AF_INET:
921 beinit(sc);
922 arp_ifinit(ifp, ifa);
923 break;
924 #endif /* INET */
925 #ifdef NS
926 case AF_NS:
927 {
928 struct ns_addr *ina = &IA_SNS(ifa)->sns_addr;
929
930 if (ns_nullhost(*ina))
931 ina->x_host =
932 *(union ns_host *)LLADDR(ifp->if_sadl);
933 else
934 bcopy(ina->x_host.c_host, LLADDR(ifp->if_sadl),
935 sizeof(sc->sc_enaddr));
936 /* Set new address. */
937 beinit(sc);
938 break;
939 }
940 #endif /* NS */
941 default:
942 beinit(sc);
943 break;
944 }
945 break;
946
947 case SIOCSIFFLAGS:
948 if ((ifp->if_flags & IFF_UP) == 0 &&
949 (ifp->if_flags & IFF_RUNNING) != 0) {
950 /*
951 * If interface is marked down and it is running, then
952 * stop it.
953 */
954 bestop(sc);
955 ifp->if_flags &= ~IFF_RUNNING;
956 } else if ((ifp->if_flags & IFF_UP) != 0 &&
957 (ifp->if_flags & IFF_RUNNING) == 0) {
958 /*
959 * If interface is marked up and it is stopped, then
960 * start it.
961 */
962 beinit(sc);
963 } else {
964 /*
965 * Reset the interface to pick up changes in any other
966 * flags that affect hardware registers.
967 */
968 bestop(sc);
969 beinit(sc);
970 }
971 #ifdef BEDEBUG
972 if (ifp->if_flags & IFF_DEBUG)
973 sc->sc_debug = 1;
974 else
975 sc->sc_debug = 0;
976 #endif
977 break;
978
979 case SIOCADDMULTI:
980 case SIOCDELMULTI:
981 error = (cmd == SIOCADDMULTI) ?
982 ether_addmulti(ifr, &sc->sc_ethercom):
983 ether_delmulti(ifr, &sc->sc_ethercom);
984
985 if (error == ENETRESET) {
986 /*
987 * Multicast list has changed; set the hardware filter
988 * accordingly.
989 */
990 be_mcreset(sc);
991 error = 0;
992 }
993 break;
994 case SIOCGIFMEDIA:
995 case SIOCSIFMEDIA:
996 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
997 break;
998 default:
999 error = EINVAL;
1000 break;
1001 }
1002 splx(s);
1003 return (error);
1004 }
1005
1006
1007 void
1008 beinit(sc)
1009 struct be_softc *sc;
1010 {
1011 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1012 bus_space_tag_t t = sc->sc_bustag;
1013 bus_space_handle_t br = sc->sc_br;
1014 bus_space_handle_t cr = sc->sc_cr;
1015 struct qec_softc *qec = sc->sc_qec;
1016 u_int32_t qecaddr;
1017 u_int8_t *ea;
1018 int s;
1019
1020 s = splimp();
1021
1022 qec_meminit(&sc->sc_rb, BE_PKT_BUF_SZ);
1023 be_tcvr_init(sc);
1024
1025 be_ifmedia_upd(ifp);
1026
1027 bestop(sc);
1028
1029 ea = sc->sc_enaddr;
1030 bus_space_write_4(t, br, BE_BRI_MACADDR0, (ea[0] << 8) | ea[1]);
1031 bus_space_write_4(t, br, BE_BRI_MACADDR1, (ea[2] << 8) | ea[3]);
1032 bus_space_write_4(t, br, BE_BRI_MACADDR2, (ea[4] << 8) | ea[5]);
1033
1034 bus_space_write_4(t, br, BE_BRI_HASHTAB0, 0);
1035 bus_space_write_4(t, br, BE_BRI_HASHTAB1, 0);
1036 bus_space_write_4(t, br, BE_BRI_HASHTAB2, 0);
1037 bus_space_write_4(t, br, BE_BRI_HASHTAB3, 0);
1038
1039 be_mcreset(sc);
1040
1041 bus_space_write_4(t, br, BE_BRI_RANDSEED, 0xbd);
1042
1043 bus_space_write_4(t, br, BE_BRI_XIFCFG,
1044 BE_BR_XCFG_ODENABLE | BE_BR_XCFG_RESV);
1045
1046 bus_space_write_4(t, br, BE_BRI_JSIZE, 4);
1047
1048 /*
1049 * Turn off counter expiration interrupts as well as
1050 * 'gotframe' and 'sentframe'
1051 */
1052 bus_space_write_4(t, br, BE_BRI_IMASK,
1053 BE_BR_IMASK_GOTFRAME |
1054 BE_BR_IMASK_RCNTEXP |
1055 BE_BR_IMASK_ACNTEXP |
1056 BE_BR_IMASK_CCNTEXP |
1057 BE_BR_IMASK_LCNTEXP |
1058 BE_BR_IMASK_CVCNTEXP |
1059 BE_BR_IMASK_SENTFRAME |
1060 BE_BR_IMASK_NCNTEXP |
1061 BE_BR_IMASK_ECNTEXP |
1062 BE_BR_IMASK_LCCNTEXP |
1063 BE_BR_IMASK_FCNTEXP |
1064 BE_BR_IMASK_DTIMEXP);
1065
1066 /* Channel registers: */
1067 bus_space_write_4(t, cr, BE_CRI_RXDS, (u_int32_t)sc->sc_rb.rb_rxddma);
1068 bus_space_write_4(t, cr, BE_CRI_TXDS, (u_int32_t)sc->sc_rb.rb_txddma);
1069
1070 qecaddr = sc->sc_channel * qec->sc_msize;
1071 bus_space_write_4(t, cr, BE_CRI_RXWBUF, qecaddr);
1072 bus_space_write_4(t, cr, BE_CRI_RXRBUF, qecaddr);
1073 bus_space_write_4(t, cr, BE_CRI_TXWBUF, qecaddr + qec->sc_rsize);
1074 bus_space_write_4(t, cr, BE_CRI_TXRBUF, qecaddr + qec->sc_rsize);
1075
1076 bus_space_write_4(t, cr, BE_CRI_RIMASK, 0);
1077 bus_space_write_4(t, cr, BE_CRI_TIMASK, 0);
1078 bus_space_write_4(t, cr, BE_CRI_QMASK, 0);
1079 bus_space_write_4(t, cr, BE_CRI_BMASK, 0);
1080 bus_space_write_4(t, cr, BE_CRI_CCNT, 0);
1081
1082 /* Enable transmitter */
1083 bus_space_write_4(t, br, BE_BRI_TXCFG,
1084 BE_BR_TXCFG_FIFO | BE_BR_TXCFG_ENABLE);
1085
1086 /* Enable receiver */
1087 bus_space_write_4(t, br, BE_BRI_RXCFG,
1088 BE_BR_RXCFG_HENABLE | BE_BR_RXCFG_FIFO |
1089 BE_BR_RXCFG_ENABLE);
1090
1091 ifp->if_flags |= IFF_RUNNING;
1092 ifp->if_flags &= ~IFF_OACTIVE;
1093
1094 timeout(be_tick, sc, hz);
1095 splx(s);
1096 }
1097
1098 void
1099 be_mcreset(sc)
1100 struct be_softc *sc;
1101 {
1102 struct ethercom *ec = &sc->sc_ethercom;
1103 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1104 bus_space_tag_t t = sc->sc_bustag;
1105 bus_space_handle_t br = sc->sc_br;
1106 u_int32_t crc;
1107 u_int16_t hash[4];
1108 u_int8_t octet;
1109 u_int32_t v;
1110 int i, j;
1111 struct ether_multi *enm;
1112 struct ether_multistep step;
1113
1114 if (ifp->if_flags & IFF_PROMISC) {
1115 v = bus_space_read_4(t, br, BE_BRI_RXCFG);
1116 v |= BE_BR_RXCFG_PMISC;
1117 bus_space_write_4(t, br, BE_BRI_RXCFG, v);
1118 return;
1119 }
1120
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
1125 if (ifp->if_flags & IFF_ALLMULTI) {
1126 bus_space_write_4(t, br, BE_BRI_HASHTAB0, 0xffff);
1127 bus_space_write_4(t, br, BE_BRI_HASHTAB1, 0xffff);
1128 bus_space_write_4(t, br, BE_BRI_HASHTAB2, 0xffff);
1129 bus_space_write_4(t, br, BE_BRI_HASHTAB3, 0xffff);
1130 return;
1131 }
1132
1133 hash[3] = hash[2] = hash[1] = hash[0] = 0;
1134
1135 ETHER_FIRST_MULTI(step, ec, enm);
1136 while (enm != NULL) {
1137 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1138 /*
1139 * We must listen to a range of multicast
1140 * addresses. For now, just accept all
1141 * multicasts, rather than trying to set only
1142 * those filter bits needed to match the range.
1143 * (At this time, the only use of address
1144 * ranges is for IP multicast routing, for
1145 * which the range is big enough to require
1146 * all bits set.)
1147 */
1148 bus_space_write_4(t, br, BE_BRI_HASHTAB0, 0xffff);
1149 bus_space_write_4(t, br, BE_BRI_HASHTAB1, 0xffff);
1150 bus_space_write_4(t, br, BE_BRI_HASHTAB2, 0xffff);
1151 bus_space_write_4(t, br, BE_BRI_HASHTAB3, 0xffff);
1152 ifp->if_flags |= IFF_ALLMULTI;
1153 return;
1154 }
1155
1156 crc = 0xffffffff;
1157
1158 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1159 octet = enm->enm_addrlo[i];
1160
1161 for (j = 0; j < 8; j++) {
1162 if ((crc & 1) ^ (octet & 1)) {
1163 crc >>= 1;
1164 crc ^= MC_POLY_LE;
1165 }
1166 else
1167 crc >>= 1;
1168 octet >>= 1;
1169 }
1170 }
1171
1172 crc >>= 26;
1173 hash[crc >> 4] |= 1 << (crc & 0xf);
1174 ETHER_NEXT_MULTI(step, enm);
1175 }
1176
1177 bus_space_write_4(t, br, BE_BRI_HASHTAB0, hash[0]);
1178 bus_space_write_4(t, br, BE_BRI_HASHTAB1, hash[1]);
1179 bus_space_write_4(t, br, BE_BRI_HASHTAB2, hash[2]);
1180 bus_space_write_4(t, br, BE_BRI_HASHTAB3, hash[3]);
1181 ifp->if_flags &= ~IFF_ALLMULTI;
1182 }
1183
1184 /*
1185 * Set the tcvr to an idle state
1186 */
1187 void
1188 be_mii_sync(sc)
1189 struct be_softc *sc;
1190 {
1191 bus_space_tag_t t = sc->sc_bustag;
1192 bus_space_handle_t tr = sc->sc_tr;
1193 int n = 20;
1194
1195 while (n--) {
1196 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1197 MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO |
1198 MGMT_PAL_OENAB);
1199 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1200 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1201 MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO |
1202 MGMT_PAL_OENAB | MGMT_PAL_DCLOCK);
1203 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1204 }
1205 }
1206
1207 /*
1208 * Initialize the transceiver and figure out whether we're using the
1209 * external or internal one.
1210 */
1211 void
1212 be_tcvr_init(sc)
1213 struct be_softc *sc;
1214 {
1215 bus_space_tag_t t = sc->sc_bustag;
1216 bus_space_handle_t tr = sc->sc_tr;
1217 u_int32_t v;
1218
1219 be_mii_sync(sc);
1220
1221 if (sc->sc_rev != 1) {
1222 printf("%s: rev %d PAL not supported.\n",
1223 sc->sc_dev.dv_xname,
1224 sc->sc_rev);
1225 return;
1226 }
1227
1228 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1229 MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO |
1230 MGMT_PAL_DCLOCK);
1231 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1232
1233 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1234 MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO);
1235 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1236 DELAY(200);
1237
1238 v = bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1239 #ifdef BEDEBUG
1240 if (sc->sc_debug != 0) {
1241 char bits[64];
1242 printf("be_tcvr_init: MGMTPAL=%s\n",
1243 bitmask_snprintf(v, MGMT_PAL_BITS, bits, sizeof(bits)));
1244 }
1245 #endif
1246 {
1247 char bits[64];
1248 printf("be_tcvr_init: MGMTPAL=%s\n",
1249 bitmask_snprintf(v, MGMT_PAL_BITS, bits, sizeof(bits)));
1250 if ((v & MGMT_PAL_EXT_MDIO) != 0) {
1251 printf("EXTERNAL\n");
1252 }
1253 }
1254 if ((v & MGMT_PAL_EXT_MDIO) != 0) {
1255 sc->sc_conf |= BE_CONF_MII;
1256 /*sc->sc_tcvr_type = BE_TCVR_EXTERNAL;*/
1257 bus_space_write_4(t, tr, BE_TRI_TCVRPAL,
1258 ~(TCVR_PAL_EXTLBACK | TCVR_PAL_MSENSE |
1259 TCVR_PAL_LTENABLE));
1260
1261 (void)bus_space_read_4(t, tr, BE_TRI_TCVRPAL);
1262 } else if ((v & MGMT_PAL_INT_MDIO) != 0) {
1263 /*sc->sc_tcvr_type = BE_TCVR_INTERNAL;*/
1264 bus_space_write_4(t, tr, BE_TRI_TCVRPAL,
1265 ~(TCVR_PAL_EXTLBACK | TCVR_PAL_MSENSE |
1266 TCVR_PAL_LTENABLE | TCVR_PAL_SERIAL));
1267 (void)bus_space_read_4(t, tr, BE_TRI_TCVRPAL);
1268 } else {
1269 printf("%s: no internal or external transceiver found.\n",
1270 sc->sc_dev.dv_xname);
1271 }
1272 }
1273
1274
1275 static __inline__ int
1276 be_tcvr_read_bit(sc, phy)
1277 struct be_softc *sc;
1278 int phy;
1279 {
1280 bus_space_tag_t t = sc->sc_bustag;
1281 bus_space_handle_t tr = sc->sc_tr;
1282 int ret;
1283
1284 if (phy == BE_PHY_INTERNAL) {
1285 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, MGMT_PAL_EXT_MDIO);
1286 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1287 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1288 MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK);
1289 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1290 DELAY(20);
1291 ret = (bus_space_read_4(t, tr, BE_TRI_MGMTPAL) &
1292 MGMT_PAL_INT_MDIO) >> 3;
1293 } else {
1294 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, MGMT_PAL_INT_MDIO);
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_EXT_MDIO) >> 2;
1299 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1300 MGMT_PAL_INT_MDIO | MGMT_PAL_DCLOCK);
1301 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1302 }
1303
1304 return (ret);
1305 }
1306
1307 static __inline__ void
1308 be_tcvr_write_bit(sc, phy, bit)
1309 struct be_softc *sc;
1310 int phy;
1311 int bit;
1312 {
1313 bus_space_tag_t t = sc->sc_bustag;
1314 bus_space_handle_t tr = sc->sc_tr;
1315
1316 if (phy == BE_PHY_INTERNAL) {
1317 bit = ((bit & 1) << 3) | MGMT_PAL_OENAB | MGMT_PAL_EXT_MDIO;
1318 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, bit);
1319 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1320
1321 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, bit | MGMT_PAL_DCLOCK);
1322 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1323 } else {
1324 bit = ((bit & 1) << 2) | MGMT_PAL_OENAB | MGMT_PAL_INT_MDIO;
1325 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, bit);
1326 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
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 }
1330 }
1331
1332 static __inline__ void
1333 be_mii_sendbits(sc, phy, data, nbits)
1334 struct be_softc *sc;
1335 int phy;
1336 u_int32_t data;
1337 int nbits;
1338 {
1339 int i;
1340
1341 for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
1342 be_tcvr_write_bit(sc, phy, (data & i) != 0);
1343 }
1344 }
1345
1346 static int
1347 be_mii_readreg(self, phy, reg)
1348 struct device *self;
1349 int phy, reg;
1350 {
1351 struct be_softc *sc = (struct be_softc *)self;
1352 int val = 0, i;
1353
1354 /* The `be' internal PHY is not treated as an MII device */
1355 if (phy == BE_PHY_INTERNAL)
1356 return (0);
1357
1358 /*
1359 * Read the PHY register by manually driving the MII control lines.
1360 */
1361 be_mii_sync(sc);
1362 be_mii_sendbits(sc, phy, MII_COMMAND_START, 2);
1363 be_mii_sendbits(sc, phy, MII_COMMAND_READ, 2);
1364 be_mii_sendbits(sc, phy, phy, 5);
1365 be_mii_sendbits(sc, phy, reg, 5);
1366
1367 (void) be_tcvr_read_bit(sc, phy);
1368 (void) be_tcvr_read_bit(sc, phy);
1369
1370 for (i = 15; i >= 0; i--)
1371 val |= (be_tcvr_read_bit(sc, phy) << i);
1372
1373 (void) be_tcvr_read_bit(sc, phy);
1374 (void) be_tcvr_read_bit(sc, phy);
1375 (void) be_tcvr_read_bit(sc, phy);
1376
1377 return (val);
1378 }
1379
1380 void
1381 be_mii_writereg(self, phy, reg, val)
1382 struct device *self;
1383 int phy, reg, val;
1384 {
1385 struct be_softc *sc = (struct be_softc *)self;
1386 int i;
1387
1388 /*
1389 * Write the PHY register by manually driving the MII control lines.
1390 */
1391 be_mii_sync(sc);
1392 be_mii_sendbits(sc, phy, MII_COMMAND_START, 2);
1393 be_mii_sendbits(sc, phy, MII_COMMAND_WRITE, 2);
1394 be_mii_sendbits(sc, phy, phy, 5);
1395 be_mii_sendbits(sc, phy, reg, 5);
1396
1397 be_tcvr_write_bit(sc, phy, 1);
1398 be_tcvr_write_bit(sc, phy, 0);
1399
1400 for (i = 15; i >= 0; i--)
1401 be_tcvr_write_bit(sc, phy, (val >> i) & 1);
1402 }
1403
1404 int
1405 be_mii_reset(sc, phy)
1406 struct be_softc *sc;
1407 int phy;
1408 {
1409 int n;
1410
1411 be_mii_writereg((struct device *)sc, phy, MII_BMCR,
1412 BMCR_LOOP | BMCR_PDOWN | BMCR_ISO);
1413 be_mii_writereg((struct device *)sc, phy, MII_BMCR, BMCR_RESET);
1414
1415 for (n = 16; n >= 0; n--) {
1416 int bmcr = be_mii_readreg((struct device *)sc, phy, MII_BMCR);
1417 if ((bmcr & BMCR_RESET) == 0)
1418 break;
1419 DELAY(20);
1420 }
1421 if (n == 0) {
1422 printf("%s: bmcr reset failed\n", sc->sc_dev.dv_xname);
1423 return (EIO);
1424 }
1425 return (0);
1426 }
1427
1428 void
1429 be_statchg(self)
1430 struct device *self;
1431 {
1432 struct be_softc *sc = (struct be_softc *)self;
1433
1434 printf("be_statchg: media_active=%x\n", sc->sc_mii.mii_media_active);
1435 }
1436
1437 void
1438 be_tick(arg)
1439 void *arg;
1440 {
1441 struct be_softc *sc = arg;
1442 int s = splnet();
1443
1444 if ((sc->sc_conf & BE_CONF_MII) != 0)
1445 mii_tick(&sc->sc_mii);
1446 else
1447 be_internal_phy_auto(sc);
1448
1449 splx(s);
1450 timeout(be_tick, sc, hz);
1451 }
1452
1453 void
1454 be_internal_phy_auto(sc)
1455 struct be_softc *sc;
1456 {
1457 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1458 int bmcr, bmsr;
1459
1460 /*
1461 * Check link status; if we don't have a link, try another
1462 * speed. We can't detect duplex mode, so half-duplex is
1463 * what we have to settle for.
1464 */
1465
1466 /* Only used for automatic media selection */
1467 if (IFM_SUBTYPE(sc->sc_media.ifm_cur->ifm_media) != IFM_AUTO)
1468 return;
1469
1470 /* Don't bother if interface isn't up */
1471 if ((ifp->if_flags & IFF_UP) == 0)
1472 return;
1473
1474 /* Read twice in case the register is latched */
1475 bmsr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMSR)|
1476 be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMSR);
1477
1478 if ((bmsr & BMSR_LINK) != 0) {
1479 /* We have a carrier */
1480 return;
1481 }
1482
1483 bmcr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMCR);
1484 /* Just flip the fast speed bit */
1485 bmcr ^= BMCR_S100;
1486 be_mii_writereg((struct device *)sc, BE_PHY_INTERNAL, MII_BMCR, bmcr);
1487 }
1488
1489 /*
1490 * Get current media settings.
1491 */
1492 void
1493 be_ifmedia_sts(ifp, ifmr)
1494 struct ifnet *ifp;
1495 struct ifmediareq *ifmr;
1496 {
1497 struct be_softc *sc = ifp->if_softc;
1498 int bmcr, bmsr;
1499
1500 if ((sc->sc_conf & BE_CONF_MII) != 0) {
1501 mii_pollstat(&sc->sc_mii);
1502 ifmr->ifm_status = sc->sc_mii.mii_media_status;
1503 ifmr->ifm_active = sc->sc_mii.mii_media_active;
1504 return;
1505 }
1506
1507 /*
1508 * Internal transceiver; do the work here.
1509 */
1510 bmcr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMCR);
1511
1512 switch (bmcr & (BMCR_S100 | BMCR_FDX)) {
1513 case (BMCR_S100 | BMCR_FDX):
1514 ifmr->ifm_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
1515 break;
1516 case BMCR_S100:
1517 ifmr->ifm_active = IFM_ETHER | IFM_100_TX | IFM_HDX;
1518 break;
1519 case BMCR_FDX:
1520 ifmr->ifm_active = IFM_ETHER | IFM_10_T | IFM_FDX;
1521 break;
1522 case 0:
1523 ifmr->ifm_active = IFM_ETHER | IFM_10_T | IFM_HDX;
1524 break;
1525 }
1526
1527 /* Read twice in case the register is latched */
1528 bmsr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMSR)|
1529 be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMSR);
1530 if (bmsr & BMSR_LINK)
1531 ifmr->ifm_status |= IFM_AVALID | IFM_ACTIVE;
1532 else {
1533 ifmr->ifm_status |= IFM_AVALID;
1534 ifmr->ifm_status &= ~IFM_ACTIVE;
1535 }
1536 }
1537
1538 /*
1539 * Set media options.
1540 */
1541 int
1542 be_ifmedia_upd(ifp)
1543 struct ifnet *ifp;
1544 {
1545 struct be_softc *sc = ifp->if_softc;
1546 struct ifmedia *ifm = &sc->sc_media;
1547 int newmedia = ifm->ifm_media;
1548 int n, error, phy, bmcr;
1549 char *speed, *mode;
1550 u_int32_t v;
1551 bus_space_tag_t t = sc->sc_bustag;
1552 bus_space_handle_t br = sc->sc_br;
1553
1554 if (IFM_TYPE(newmedia) != IFM_ETHER)
1555 return (EINVAL);
1556
1557 if ((sc->sc_conf & BE_CONF_MII) != 0) {
1558 int error;
1559
1560 if ((error = mii_mediachg(&sc->sc_mii)) != 0)
1561 return (error);
1562
1563 v = bus_space_read_4(t, br, BE_BRI_TXCFG);
1564 if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
1565 v |= BE_BR_TXCFG_FULLDPLX;
1566 else
1567 v &= ~BE_BR_TXCFG_FULLDPLX;
1568 bus_space_write_4(t, br, BE_BRI_TXCFG, v);
1569
1570 return (0);
1571 }
1572
1573 /*
1574 * The rest of this routine is devoted to the
1575 * not-quite-a-phy internal transceiver case.
1576 */
1577 phy = BE_PHY_INTERNAL;
1578
1579 /* Why must we reset the device? */
1580 if ((error = be_mii_reset(sc, phy)) != 0)
1581 return (error);
1582
1583 bmcr = be_mii_readreg((struct device *)sc, phy, MII_BMCR);
1584
1585 if (IFM_SUBTYPE(newmedia) == IFM_100_TX) {
1586 bmcr |= BMCR_S100;
1587 speed = "100baseTX";
1588 } else if (IFM_SUBTYPE(newmedia) == IFM_10_T) {
1589 bmcr &= ~BMCR_S100;
1590 speed = "10baseT";
1591 } else {
1592 speed = "auto sense";
1593 }
1594
1595 printf("%s: selecting %s", sc->sc_dev.dv_xname, speed);
1596
1597 v = bus_space_read_4(t, br, BE_BRI_TXCFG);
1598 if ((IFM_OPTIONS(newmedia) & IFM_FDX) != 0) {
1599 bmcr |= BMCR_FDX;
1600 v |= BE_BR_TXCFG_FULLDPLX;
1601 mode = "full";
1602 } else {
1603 bmcr &= ~BMCR_FDX;
1604 v &= ~BE_BR_TXCFG_FULLDPLX;
1605 mode = "half";
1606 }
1607 bus_space_write_4(t, br, BE_BRI_TXCFG, v);
1608 printf(" %s-duplex\n", mode);
1609
1610 /* Select the new mode and take out of isolation */
1611 be_mii_writereg((struct device *)sc, phy, MII_BMCR, bmcr & ~BMCR_ISO);
1612
1613 for (n = 32; n >= 0; n--) {
1614 bmcr = be_mii_readreg((struct device *)sc, phy, MII_BMCR);
1615 if ((bmcr & BMCR_ISO) == 0)
1616 break;
1617 DELAY(20);
1618 }
1619 if (n == 0) {
1620 printf("%s: bmcr unisolate failed\n", sc->sc_dev.dv_xname);
1621 return (EIO);
1622 }
1623
1624 return (0);
1625 }
1626