be.c revision 1.4 1 /* $NetBSD: be.c,v 1.4 1999/02/19 14:57:00 pk Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Paul Kranenburg.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1998 Theo de Raadt and Jason L. Wright.
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. The name of the authors may not be used to endorse or promote products
52 * derived from this software without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
55 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
56 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
57 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
58 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
59 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
60 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
61 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
63 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 */
65
66 #include "opt_ddb.h"
67 #include "opt_inet.h"
68 #include "opt_ccitt.h"
69 #include "opt_llc.h"
70 #include "opt_ns.h"
71 #include "bpfilter.h"
72 #include "rnd.h"
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/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_promisc;
146 int sc_burst;
147 int sc_conf;
148 #define BE_CONF_MII 1
149
150 struct qec_ring sc_rb; /* Packet Ring Buffer */
151
152 /* MAC address */
153 u_int8_t sc_enaddr[6];
154 };
155
156 int bematch __P((struct device *, struct cfdata *, void *));
157 void beattach __P((struct device *, struct device *, void *));
158
159 void beinit __P((struct be_softc *));
160 void bestart __P((struct ifnet *));
161 void bestop __P((struct be_softc *));
162 void bewatchdog __P((struct ifnet *));
163 int beioctl __P((struct ifnet *, u_long, caddr_t));
164 void bereset __P((struct be_softc *));
165
166 int beintr __P((void *));
167 int berint __P((struct be_softc *));
168 int betint __P((struct be_softc *));
169 int beqint __P((struct be_softc *, u_int32_t));
170 int beeint __P((struct be_softc *, u_int32_t));
171
172 static void be_read __P((struct be_softc *, int, int));
173 static int be_put __P((struct be_softc *, int, struct mbuf *));
174 static struct mbuf *be_get __P((struct be_softc *, int, int));
175
176 void be_tcvr_init __P((struct be_softc *));
177
178 /* ifmedia callbacks */
179 void be_ifmedia_sts __P((struct ifnet *, struct ifmediareq *));
180 int be_ifmedia_upd __P((struct ifnet *));
181
182 void be_mcreset __P((struct be_softc *));
183
184 /* MII methods & callbacks */
185 static int be_mii_readreg __P((struct device *, int, int));
186 static void be_mii_writereg __P((struct device *, int, int, int));
187 static void be_statchg __P((struct device *));
188
189 /* MII helpers */
190 static void be_mii_sync __P((struct be_softc *));
191 static void be_mii_sendbits __P((struct be_softc *, int, u_int32_t, int));
192 static int be_mii_reset __P((struct be_softc *, int));
193 static int be_tcvr_read_bit __P((struct be_softc *, int));
194 static void be_tcvr_write_bit __P((struct be_softc *, int, int));
195
196 void be_tick __P((void *));
197 void be_internal_phy_auto __P((struct be_softc *));
198
199
200 struct cfattach be_ca = {
201 sizeof(struct be_softc), bematch, beattach
202 };
203
204 int
205 bematch(parent, cf, aux)
206 struct device *parent;
207 struct cfdata *cf;
208 void *aux;
209 {
210 struct sbus_attach_args *sa = aux;
211
212 return (strcmp(cf->cf_driver->cd_name, sa->sa_name) == 0);
213 }
214
215 void
216 beattach(parent, self, aux)
217 struct device *parent, *self;
218 void *aux;
219 {
220 struct sbus_attach_args *sa = aux;
221 struct qec_softc *qec = (struct qec_softc *)parent;
222 struct be_softc *sc = (struct be_softc *)self;
223 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
224 struct mii_data *mii = &sc->sc_mii;
225 int node = sa->sa_node;
226 bus_dma_segment_t seg;
227 bus_size_t size;
228 int rseg, error;
229 extern void myetheraddr __P((u_char *));
230
231 if (sa->sa_nreg < 3) {
232 printf("%s: only %d register sets\n",
233 self->dv_xname, sa->sa_nreg);
234 return;
235 }
236
237 if (bus_space_map2(sa->sa_bustag,
238 (bus_type_t)sa->sa_reg[0].sbr_slot,
239 (bus_addr_t)sa->sa_reg[0].sbr_offset,
240 (bus_size_t)sa->sa_reg[0].sbr_size,
241 BUS_SPACE_MAP_LINEAR, 0, &sc->sc_cr) != 0) {
242 printf("beattach: cannot map registers\n");
243 return;
244 }
245
246 if (bus_space_map2(sa->sa_bustag,
247 (bus_type_t)sa->sa_reg[1].sbr_slot,
248 (bus_addr_t)sa->sa_reg[1].sbr_offset,
249 (bus_size_t)sa->sa_reg[1].sbr_size,
250 BUS_SPACE_MAP_LINEAR, 0, &sc->sc_br) != 0) {
251 printf("beattach: cannot map registers\n");
252 return;
253 }
254
255 if (bus_space_map2(sa->sa_bustag,
256 (bus_type_t)sa->sa_reg[2].sbr_slot,
257 (bus_addr_t)sa->sa_reg[2].sbr_offset,
258 (bus_size_t)sa->sa_reg[2].sbr_size,
259 BUS_SPACE_MAP_LINEAR, 0, &sc->sc_tr) != 0) {
260 printf("beattach: cannot map registers\n");
261 return;
262 }
263
264 sc->sc_qec = qec;
265 sc->sc_qr = qec->sc_regs;
266
267 sc->sc_rev = getpropint(node, "board-version", -1);
268 printf(" rev %x", sc->sc_rev);
269
270 bestop(sc);
271
272 sc->sc_channel = getpropint(node, "channel#", -1);
273 if (sc->sc_channel == -1)
274 sc->sc_channel = 0;
275
276 sc->sc_burst = getpropint(node, "burst-sizes", -1);
277 if (sc->sc_burst == -1)
278 sc->sc_burst = qec->sc_burst;
279
280 /* Clamp at parent's burst sizes */
281 sc->sc_burst &= qec->sc_burst;
282
283 (void)bus_intr_establish(sa->sa_bustag, sa->sa_pri, 0, beintr, sc);
284
285 myetheraddr(sc->sc_enaddr);
286 printf(" address %s\n", ether_sprintf(sc->sc_enaddr));
287
288 /*
289 * Allocate descriptor ring and buffers.
290 */
291
292 /* for now, allocate as many bufs as there are ring descriptors */
293 sc->sc_rb.rb_ntbuf = QEC_XD_RING_MAXSIZE;
294 sc->sc_rb.rb_nrbuf = QEC_XD_RING_MAXSIZE;
295
296 size = QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd) +
297 QEC_XD_RING_MAXSIZE * sizeof(struct qec_xd) +
298 sc->sc_rb.rb_ntbuf * BE_PKT_BUF_SZ +
299 sc->sc_rb.rb_nrbuf * BE_PKT_BUF_SZ;
300 if ((error = bus_dmamem_alloc(sa->sa_dmatag, size,
301 NBPG, 0,
302 &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
303 printf("%s: DMA buffer alloc error %d\n",
304 self->dv_xname, error);
305 return;
306 }
307 sc->sc_rb.rb_dmabase = seg.ds_addr;
308
309 if ((error = bus_dmamem_map(sa->sa_dmatag, &seg, rseg, size,
310 &sc->sc_rb.rb_membase,
311 BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
312 printf("%s: DMA buffer map error %d\n",
313 self->dv_xname, error);
314 bus_dmamem_free(sa->sa_dmatag, &seg, rseg);
315 return;
316 }
317
318 /*
319 * Initialize transceiver and determine which PHY connection to use.
320 */
321 be_tcvr_init(sc);
322
323 /*
324 * Initialize our media structures and MII info.
325 */
326 mii->mii_ifp = ifp;
327 mii->mii_readreg = be_mii_readreg;
328 mii->mii_writereg = be_mii_writereg;
329 mii->mii_statchg = be_statchg;
330
331 ifmedia_init(&mii->mii_media, 0, be_ifmedia_upd, be_ifmedia_sts);
332
333 if ((sc->sc_conf & BE_CONF_MII) != 0) {
334 #if 1
335 mii_phy_probe(&sc->sc_dev, mii, 0xffffffff);
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 ether_header *eh;
509 struct mbuf *m;
510
511 if (len <= sizeof(struct ether_header) ||
512 len > ETHERMTU + sizeof(struct ether_header)) {
513
514 printf("%s: invalid packet size %d; dropping\n",
515 ifp->if_xname, len);
516
517 ifp->if_ierrors++;
518 return;
519 }
520
521 /*
522 * Pull packet off interface.
523 */
524 m = be_get(sc, idx, len);
525 if (m == NULL) {
526 ifp->if_ierrors++;
527 return;
528 }
529 ifp->if_ipackets++;
530
531 /* We assume that the header fits entirely in one mbuf. */
532 eh = mtod(m, struct ether_header *);
533
534 #if NBPFILTER > 0
535 /*
536 * Check if there's a BPF listener on this interface.
537 * If so, hand off the raw packet to BPF.
538 */
539 if (ifp->if_bpf)
540 bpf_mtap(ifp->if_bpf, m);
541 #endif
542 /* Pass the packet up, with the ether header sort-of removed. */
543 m_adj(m, sizeof(struct ether_header));
544 ether_input(ifp, eh, m);
545 }
546
547 /*
548 * Start output on interface.
549 * We make two assumptions here:
550 * 1) that the current priority is set to splnet _before_ this code
551 * is called *and* is returned to the appropriate priority after
552 * return
553 * 2) that the IFF_OACTIVE flag is checked before this code is called
554 * (i.e. that the output part of the interface is idle)
555 */
556 void
557 bestart(ifp)
558 struct ifnet *ifp;
559 {
560 struct be_softc *sc = (struct be_softc *)ifp->if_softc;
561 struct qec_xd *txd = sc->sc_rb.rb_txd;
562 struct mbuf *m;
563 unsigned int bix, len;
564 unsigned int ntbuf = sc->sc_rb.rb_ntbuf;
565
566 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING)
567 return;
568
569 bix = sc->sc_rb.rb_tdhead;
570
571 for (;;) {
572 IF_DEQUEUE(&ifp->if_snd, m);
573 if (m == 0)
574 break;
575
576 #if NBPFILTER > 0
577 /*
578 * If BPF is listening on this interface, let it see the
579 * packet before we commit it to the wire.
580 */
581 if (ifp->if_bpf)
582 bpf_mtap(ifp->if_bpf, m);
583 #endif
584
585 /*
586 * Copy the mbuf chain into the transmit buffer.
587 */
588 len = be_put(sc, bix, m);
589
590 /*
591 * Initialize transmit registers and start transmission
592 */
593 txd[bix].xd_flags = QEC_XD_OWN | QEC_XD_SOP | QEC_XD_EOP |
594 (len & QEC_XD_LENGTH);
595 bus_space_write_4(sc->sc_bustag, sc->sc_cr, BE_CRI_CTRL,
596 BE_CR_CTRL_TWAKEUP);
597
598 if (++bix == QEC_XD_RING_MAXSIZE)
599 bix = 0;
600
601 if (++sc->sc_rb.rb_td_nbusy == ntbuf) {
602 ifp->if_flags |= IFF_OACTIVE;
603 break;
604 }
605 }
606
607 sc->sc_rb.rb_tdhead = bix;
608 }
609
610 void
611 bestop(sc)
612 struct be_softc *sc;
613 {
614 int n;
615 bus_space_tag_t t = sc->sc_bustag;
616 bus_space_handle_t br = sc->sc_br;
617
618 untimeout(be_tick, sc);
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 sc->sc_promisc = ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI);
955 if ((ifp->if_flags & IFF_UP) == 0 &&
956 (ifp->if_flags & IFF_RUNNING) != 0) {
957 /*
958 * If interface is marked down and it is running, then
959 * stop it.
960 */
961 bestop(sc);
962 ifp->if_flags &= ~IFF_RUNNING;
963 } else if ((ifp->if_flags & IFF_UP) != 0 &&
964 (ifp->if_flags & IFF_RUNNING) == 0) {
965 /*
966 * If interface is marked up and it is stopped, then
967 * start it.
968 */
969 beinit(sc);
970 } else {
971 /*
972 * Reset the interface to pick up changes in any other
973 * flags that affect hardware registers.
974 */
975 bestop(sc);
976 beinit(sc);
977 }
978 #ifdef BEDEBUG
979 if (ifp->if_flags & IFF_DEBUG)
980 sc->sc_debug = 1;
981 else
982 sc->sc_debug = 0;
983 #endif
984 break;
985
986 case SIOCADDMULTI:
987 case SIOCDELMULTI:
988 error = (cmd == SIOCADDMULTI) ?
989 ether_addmulti(ifr, &sc->sc_ethercom):
990 ether_delmulti(ifr, &sc->sc_ethercom);
991
992 if (error == ENETRESET) {
993 /*
994 * Multicast list has changed; set the hardware filter
995 * accordingly.
996 */
997 be_mcreset(sc);
998 error = 0;
999 }
1000 break;
1001 case SIOCGIFMEDIA:
1002 case SIOCSIFMEDIA:
1003 error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, cmd);
1004 break;
1005 default:
1006 error = EINVAL;
1007 break;
1008 }
1009 splx(s);
1010 return (error);
1011 }
1012
1013
1014 void
1015 beinit(sc)
1016 struct be_softc *sc;
1017 {
1018 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1019 bus_space_tag_t t = sc->sc_bustag;
1020 bus_space_handle_t br = sc->sc_br;
1021 bus_space_handle_t cr = sc->sc_cr;
1022 struct qec_softc *qec = sc->sc_qec;
1023 u_int32_t qecaddr;
1024 u_int8_t *ea;
1025 int s;
1026
1027 s = splimp();
1028
1029 qec_meminit(&sc->sc_rb, BE_PKT_BUF_SZ);
1030 be_tcvr_init(sc);
1031
1032 be_ifmedia_upd(ifp);
1033
1034 bestop(sc);
1035
1036 ea = sc->sc_enaddr;
1037 bus_space_write_4(t, br, BE_BRI_MACADDR0, (ea[0] << 8) | ea[1]);
1038 bus_space_write_4(t, br, BE_BRI_MACADDR1, (ea[2] << 8) | ea[3]);
1039 bus_space_write_4(t, br, BE_BRI_MACADDR2, (ea[4] << 8) | ea[5]);
1040
1041 bus_space_write_4(t, br, BE_BRI_HASHTAB0, 0);
1042 bus_space_write_4(t, br, BE_BRI_HASHTAB1, 0);
1043 bus_space_write_4(t, br, BE_BRI_HASHTAB2, 0);
1044 bus_space_write_4(t, br, BE_BRI_HASHTAB3, 0);
1045
1046 DELAY(20);
1047
1048 bus_space_write_4(t, br, BE_BRI_RANDSEED, 0xbd);
1049
1050 bus_space_write_4(t, br, BE_BRI_XIFCFG,
1051 BE_BR_XCFG_ODENABLE | BE_BR_XCFG_RESV);
1052
1053 bus_space_write_4(t, br, BE_BRI_JSIZE, 4);
1054
1055 /*
1056 * Turn off counter expiration interrupts as well as
1057 * 'gotframe' and 'sentframe'
1058 */
1059 bus_space_write_4(t, br, BE_BRI_IMASK,
1060 BE_BR_IMASK_GOTFRAME |
1061 BE_BR_IMASK_RCNTEXP |
1062 BE_BR_IMASK_ACNTEXP |
1063 BE_BR_IMASK_CCNTEXP |
1064 BE_BR_IMASK_LCNTEXP |
1065 BE_BR_IMASK_CVCNTEXP |
1066 BE_BR_IMASK_SENTFRAME |
1067 BE_BR_IMASK_NCNTEXP |
1068 BE_BR_IMASK_ECNTEXP |
1069 BE_BR_IMASK_LCCNTEXP |
1070 BE_BR_IMASK_FCNTEXP |
1071 BE_BR_IMASK_DTIMEXP);
1072
1073 /* Channel registers: */
1074 bus_space_write_4(t, cr, BE_CRI_RXDS, (u_int32_t)sc->sc_rb.rb_rxddma);
1075 bus_space_write_4(t, cr, BE_CRI_TXDS, (u_int32_t)sc->sc_rb.rb_txddma);
1076
1077 qecaddr = sc->sc_channel * qec->sc_msize;
1078 bus_space_write_4(t, cr, BE_CRI_RXWBUF, qecaddr);
1079 bus_space_write_4(t, cr, BE_CRI_RXRBUF, qecaddr);
1080 bus_space_write_4(t, cr, BE_CRI_TXWBUF, qecaddr + qec->sc_rsize);
1081 bus_space_write_4(t, cr, BE_CRI_TXRBUF, qecaddr + qec->sc_rsize);
1082
1083 bus_space_write_4(t, cr, BE_CRI_RIMASK, 0);
1084 bus_space_write_4(t, cr, BE_CRI_TIMASK, 0);
1085 bus_space_write_4(t, cr, BE_CRI_QMASK, 0);
1086 bus_space_write_4(t, cr, BE_CRI_BMASK, 0);
1087 bus_space_write_4(t, cr, BE_CRI_CCNT, 0);
1088
1089 /* Enable transmitter */
1090 bus_space_write_4(t, br, BE_BRI_TXCFG,
1091 BE_BR_TXCFG_FIFO | BE_BR_TXCFG_ENABLE);
1092
1093 /* Enable receiver */
1094 bus_space_write_4(t, br, BE_BRI_RXCFG,
1095 BE_BR_RXCFG_HENABLE | BE_BR_RXCFG_FIFO |
1096 BE_BR_RXCFG_ENABLE);
1097
1098 ifp->if_flags |= IFF_RUNNING;
1099 ifp->if_flags &= ~IFF_OACTIVE;
1100
1101 timeout(be_tick, sc, hz);
1102 splx(s);
1103 }
1104
1105 void
1106 be_mcreset(sc)
1107 struct be_softc *sc;
1108 {
1109 struct ethercom *ec = &sc->sc_ethercom;
1110 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1111 bus_space_tag_t t = sc->sc_bustag;
1112 bus_space_handle_t br = sc->sc_br;
1113 u_int32_t crc;
1114 u_int16_t hash[4];
1115 u_int8_t octet;
1116 int i, j;
1117 struct ether_multi *enm;
1118 struct ether_multistep step;
1119
1120 if (ifp->if_flags & IFF_ALLMULTI) {
1121 bus_space_write_4(t, br, BE_BRI_HASHTAB0, 0xffff);
1122 bus_space_write_4(t, br, BE_BRI_HASHTAB1, 0xffff);
1123 bus_space_write_4(t, br, BE_BRI_HASHTAB2, 0xffff);
1124 bus_space_write_4(t, br, BE_BRI_HASHTAB3, 0xffff);
1125 return;
1126 }
1127
1128 if (ifp->if_flags & IFF_PROMISC) {
1129 u_int32_t v;
1130
1131 v = bus_space_read_4(t, br, BE_BRI_RXCFG);
1132 v |= BE_BR_RXCFG_PMISC;
1133 bus_space_write_4(t, br, BE_BRI_RXCFG, v);
1134 return;
1135 }
1136
1137 hash[3] = hash[2] = hash[1] = hash[0] = 0;
1138
1139 ETHER_FIRST_MULTI(step, ec, enm);
1140 while (enm != NULL) {
1141 if (bcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1142 /*
1143 * We must listen to a range of multicast
1144 * addresses. For now, just accept all
1145 * multicasts, rather than trying to set only
1146 * those filter bits needed to match the range.
1147 * (At this time, the only use of address
1148 * ranges is for IP multicast routing, for
1149 * which the range is big enough to require
1150 * all bits set.)
1151 */
1152 bus_space_write_4(t, br, BE_BRI_HASHTAB0, 0xffff);
1153 bus_space_write_4(t, br, BE_BRI_HASHTAB1, 0xffff);
1154 bus_space_write_4(t, br, BE_BRI_HASHTAB2, 0xffff);
1155 bus_space_write_4(t, br, BE_BRI_HASHTAB3, 0xffff);
1156 ifp->if_flags |= IFF_ALLMULTI;
1157 return;
1158 }
1159
1160 crc = 0xffffffff;
1161
1162 for (i = 0; i < ETHER_ADDR_LEN; i++) {
1163 octet = enm->enm_addrlo[i];
1164
1165 for (j = 0; j < 8; j++) {
1166 if ((crc & 1) ^ (octet & 1)) {
1167 crc >>= 1;
1168 crc ^= MC_POLY_LE;
1169 }
1170 else
1171 crc >>= 1;
1172 octet >>= 1;
1173 }
1174 }
1175
1176 crc >>= 26;
1177 hash[crc >> 4] |= 1 << (crc & 0xf);
1178 ETHER_NEXT_MULTI(step, enm);
1179 }
1180
1181 bus_space_write_4(t, br, BE_BRI_HASHTAB0, hash[0]);
1182 bus_space_write_4(t, br, BE_BRI_HASHTAB1, hash[1]);
1183 bus_space_write_4(t, br, BE_BRI_HASHTAB2, hash[2]);
1184 bus_space_write_4(t, br, BE_BRI_HASHTAB3, hash[3]);
1185 ifp->if_flags &= ~IFF_ALLMULTI;
1186 }
1187
1188 /*
1189 * Set the tcvr to an idle state
1190 */
1191 void
1192 be_mii_sync(sc)
1193 struct be_softc *sc;
1194 {
1195 bus_space_tag_t t = sc->sc_bustag;
1196 bus_space_handle_t tr = sc->sc_tr;
1197 int n = 20;
1198
1199 while (n--) {
1200 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1201 MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO |
1202 MGMT_PAL_OENAB);
1203 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1204 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1205 MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO |
1206 MGMT_PAL_OENAB | MGMT_PAL_DCLOCK);
1207 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1208 }
1209 }
1210
1211 /*
1212 * Initialize the transceiver and figure out whether we're using the
1213 * external or internal one.
1214 */
1215 void
1216 be_tcvr_init(sc)
1217 struct be_softc *sc;
1218 {
1219 bus_space_tag_t t = sc->sc_bustag;
1220 bus_space_handle_t tr = sc->sc_tr;
1221 u_int32_t v;
1222
1223 be_mii_sync(sc);
1224
1225 if (sc->sc_rev != 1) {
1226 printf("%s: rev %d PAL not supported.\n",
1227 sc->sc_dev.dv_xname,
1228 sc->sc_rev);
1229 return;
1230 }
1231
1232 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1233 MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO |
1234 MGMT_PAL_DCLOCK);
1235 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1236
1237 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1238 MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO);
1239 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1240 DELAY(200);
1241
1242 v = bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1243 #ifdef BEDEBUG
1244 if (sc->sc_debug != 0) {
1245 char bits[64];
1246 printf("be_tcvr_init: MGMTPAL=%s\n",
1247 bitmask_snprintf(v, MGMT_PAL_BITS, bits, sizeof(bits)));
1248 }
1249 #endif
1250 {
1251 char bits[64];
1252 printf("be_tcvr_init: MGMTPAL=%s\n",
1253 bitmask_snprintf(v, MGMT_PAL_BITS, bits, sizeof(bits)));
1254 if ((v & MGMT_PAL_EXT_MDIO) != 0) {
1255 printf("EXTERNAL\n");
1256 }
1257 }
1258 if ((v & MGMT_PAL_EXT_MDIO) != 0) {
1259 sc->sc_conf |= BE_CONF_MII;
1260 /*sc->sc_tcvr_type = BE_TCVR_EXTERNAL;*/
1261 bus_space_write_4(t, tr, BE_TRI_TCVRPAL,
1262 ~(TCVR_PAL_EXTLBACK | TCVR_PAL_MSENSE |
1263 TCVR_PAL_LTENABLE));
1264
1265 (void)bus_space_read_4(t, tr, BE_TRI_TCVRPAL);
1266 } else if ((v & MGMT_PAL_INT_MDIO) != 0) {
1267 /*sc->sc_tcvr_type = BE_TCVR_INTERNAL;*/
1268 bus_space_write_4(t, tr, BE_TRI_TCVRPAL,
1269 ~(TCVR_PAL_EXTLBACK | TCVR_PAL_MSENSE |
1270 TCVR_PAL_LTENABLE | TCVR_PAL_SERIAL));
1271 (void)bus_space_read_4(t, tr, BE_TRI_TCVRPAL);
1272 } else {
1273 printf("%s: no internal or external transceiver found.\n",
1274 sc->sc_dev.dv_xname);
1275 }
1276 }
1277
1278
1279 static __inline__ int
1280 be_tcvr_read_bit(sc, phy)
1281 struct be_softc *sc;
1282 int phy;
1283 {
1284 bus_space_tag_t t = sc->sc_bustag;
1285 bus_space_handle_t tr = sc->sc_tr;
1286 int ret;
1287
1288 if (phy == BE_PHY_INTERNAL) {
1289 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, MGMT_PAL_EXT_MDIO);
1290 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1291 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1292 MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK);
1293 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1294 DELAY(20);
1295 ret = (bus_space_read_4(t, tr, BE_TRI_MGMTPAL) &
1296 MGMT_PAL_INT_MDIO) >> 3;
1297 } else {
1298 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, MGMT_PAL_INT_MDIO);
1299 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1300 DELAY(20);
1301 ret = (bus_space_read_4(t, tr, BE_TRI_MGMTPAL) &
1302 MGMT_PAL_EXT_MDIO) >> 2;
1303 bus_space_write_4(t, tr, BE_TRI_MGMTPAL,
1304 MGMT_PAL_INT_MDIO | MGMT_PAL_DCLOCK);
1305 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1306 }
1307
1308 return (ret);
1309 }
1310
1311 static __inline__ void
1312 be_tcvr_write_bit(sc, phy, bit)
1313 struct be_softc *sc;
1314 int phy;
1315 int bit;
1316 {
1317 bus_space_tag_t t = sc->sc_bustag;
1318 bus_space_handle_t tr = sc->sc_tr;
1319
1320 if (phy == BE_PHY_INTERNAL) {
1321 bit = ((bit & 1) << 3) | MGMT_PAL_OENAB | MGMT_PAL_EXT_MDIO;
1322 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, bit);
1323 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1324
1325 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, bit | MGMT_PAL_DCLOCK);
1326 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1327 } else {
1328 bit = ((bit & 1) << 2) | MGMT_PAL_OENAB | MGMT_PAL_INT_MDIO;
1329 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, bit);
1330 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1331 bus_space_write_4(t, tr, BE_TRI_MGMTPAL, bit | MGMT_PAL_DCLOCK);
1332 (void)bus_space_read_4(t, tr, BE_TRI_MGMTPAL);
1333 }
1334 }
1335
1336 static __inline__ void
1337 be_mii_sendbits(sc, phy, data, nbits)
1338 struct be_softc *sc;
1339 int phy;
1340 u_int32_t data;
1341 int nbits;
1342 {
1343 int i;
1344
1345 for (i = 1 << (nbits - 1); i != 0; i >>= 1) {
1346 be_tcvr_write_bit(sc, phy, (data & i) != 0);
1347 }
1348 }
1349
1350 static int
1351 be_mii_readreg(self, phy, reg)
1352 struct device *self;
1353 int phy, reg;
1354 {
1355 struct be_softc *sc = (struct be_softc *)self;
1356 int val = 0, i;
1357
1358 /* The `be' internal PHY is not treated as an MII device */
1359 if (phy == BE_PHY_INTERNAL)
1360 return (0);
1361
1362 /*
1363 * Read the PHY register by manually driving the MII control lines.
1364 */
1365 be_mii_sync(sc);
1366 be_mii_sendbits(sc, phy, MII_COMMAND_START, 2);
1367 be_mii_sendbits(sc, phy, MII_COMMAND_READ, 2);
1368 be_mii_sendbits(sc, phy, phy, 5);
1369 be_mii_sendbits(sc, phy, reg, 5);
1370
1371 (void) be_tcvr_read_bit(sc, phy);
1372 (void) be_tcvr_read_bit(sc, phy);
1373
1374 for (i = 15; i >= 0; i--)
1375 val |= (be_tcvr_read_bit(sc, phy) << i);
1376
1377 (void) be_tcvr_read_bit(sc, phy);
1378 (void) be_tcvr_read_bit(sc, phy);
1379 (void) be_tcvr_read_bit(sc, phy);
1380
1381 return (val);
1382 }
1383
1384 void
1385 be_mii_writereg(self, phy, reg, val)
1386 struct device *self;
1387 int phy, reg, val;
1388 {
1389 struct be_softc *sc = (struct be_softc *)self;
1390 int i;
1391
1392 /*
1393 * Write the PHY register by manually driving the MII control lines.
1394 */
1395 be_mii_sync(sc);
1396 be_mii_sendbits(sc, phy, MII_COMMAND_START, 2);
1397 be_mii_sendbits(sc, phy, MII_COMMAND_WRITE, 2);
1398 be_mii_sendbits(sc, phy, phy, 5);
1399 be_mii_sendbits(sc, phy, reg, 5);
1400
1401 be_tcvr_write_bit(sc, phy, 1);
1402 be_tcvr_write_bit(sc, phy, 0);
1403
1404 for (i = 15; i >= 0; i--)
1405 be_tcvr_write_bit(sc, phy, (val >> i) & 1);
1406 }
1407
1408 int
1409 be_mii_reset(sc, phy)
1410 struct be_softc *sc;
1411 int phy;
1412 {
1413 int n;
1414
1415 be_mii_writereg((struct device *)sc, phy, MII_BMCR,
1416 BMCR_LOOP | BMCR_PDOWN | BMCR_ISO);
1417 be_mii_writereg((struct device *)sc, phy, MII_BMCR, BMCR_RESET);
1418
1419 for (n = 16; n >= 0; n--) {
1420 int bmcr = be_mii_readreg((struct device *)sc, phy, MII_BMCR);
1421 if ((bmcr & BMCR_RESET) == 0)
1422 break;
1423 DELAY(20);
1424 }
1425 if (n == 0) {
1426 printf("%s: bmcr reset failed\n", sc->sc_dev.dv_xname);
1427 return (EIO);
1428 }
1429 return (0);
1430 }
1431
1432 void
1433 be_statchg(self)
1434 struct device *self;
1435 {
1436 struct be_softc *sc = (struct be_softc *)self;
1437
1438 printf("be_statchg: media_active=%x\n", sc->sc_mii.mii_media_active);
1439 }
1440
1441 void
1442 be_tick(arg)
1443 void *arg;
1444 {
1445 struct be_softc *sc = arg;
1446 int s = splnet();
1447
1448 if ((sc->sc_conf & BE_CONF_MII) != 0)
1449 mii_tick(&sc->sc_mii);
1450 else
1451 be_internal_phy_auto(sc);
1452
1453 splx(s);
1454 timeout(be_tick, sc, hz);
1455 }
1456
1457 void
1458 be_internal_phy_auto(sc)
1459 struct be_softc *sc;
1460 {
1461 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1462 int bmcr, bmsr;
1463
1464 /*
1465 * Check link status; if we don't have a link, try another
1466 * speed. We can't detect duplex mode, so half-duplex is
1467 * what we have to settle for.
1468 */
1469
1470 /* Only used for automatic media selection */
1471 if (IFM_SUBTYPE(sc->sc_media.ifm_cur->ifm_media) != IFM_AUTO)
1472 return;
1473
1474 /* Don't bother if interface isn't up */
1475 if ((ifp->if_flags & IFF_UP) == 0)
1476 return;
1477
1478 /* Read twice in case the register is latched */
1479 bmsr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMSR)|
1480 be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMSR);
1481
1482 if ((bmsr & BMSR_LINK) != 0) {
1483 /* We have a carrier */
1484 return;
1485 }
1486
1487 bmcr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMCR);
1488 /* Just flip the fast speed bit */
1489 bmcr ^= BMCR_S100;
1490 be_mii_writereg((struct device *)sc, BE_PHY_INTERNAL, MII_BMCR, bmcr);
1491 }
1492
1493 /*
1494 * Get current media settings.
1495 */
1496 void
1497 be_ifmedia_sts(ifp, ifmr)
1498 struct ifnet *ifp;
1499 struct ifmediareq *ifmr;
1500 {
1501 struct be_softc *sc = ifp->if_softc;
1502 int bmcr, bmsr;
1503
1504 if ((sc->sc_conf & BE_CONF_MII) != 0) {
1505 mii_pollstat(&sc->sc_mii);
1506 ifmr->ifm_status = sc->sc_mii.mii_media_status;
1507 ifmr->ifm_active = sc->sc_mii.mii_media_active;
1508 return;
1509 }
1510
1511 /*
1512 * Internal transceiver; do the work here.
1513 */
1514 bmcr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMCR);
1515
1516 switch (bmcr & (BMCR_S100 | BMCR_FDX)) {
1517 case (BMCR_S100 | BMCR_FDX):
1518 ifmr->ifm_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
1519 break;
1520 case BMCR_S100:
1521 ifmr->ifm_active = IFM_ETHER | IFM_100_TX | IFM_HDX;
1522 break;
1523 case BMCR_FDX:
1524 ifmr->ifm_active = IFM_ETHER | IFM_10_T | IFM_FDX;
1525 break;
1526 case 0:
1527 ifmr->ifm_active = IFM_ETHER | IFM_10_T | IFM_HDX;
1528 break;
1529 }
1530
1531 /* Read twice in case the register is latched */
1532 bmsr = be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMSR)|
1533 be_mii_readreg((struct device *)sc, BE_PHY_INTERNAL, MII_BMSR);
1534 if (bmsr & BMSR_LINK)
1535 ifmr->ifm_status |= IFM_AVALID | IFM_ACTIVE;
1536 else {
1537 ifmr->ifm_status |= IFM_AVALID;
1538 ifmr->ifm_status &= ~IFM_ACTIVE;
1539 }
1540 }
1541
1542 /*
1543 * Set media options.
1544 */
1545 int
1546 be_ifmedia_upd(ifp)
1547 struct ifnet *ifp;
1548 {
1549 struct be_softc *sc = ifp->if_softc;
1550 struct ifmedia *ifm = &sc->sc_media;
1551 int newmedia = ifm->ifm_media;
1552 int n, error, phy, bmcr;
1553 char *speed, *mode;
1554 u_int32_t v;
1555 bus_space_tag_t t = sc->sc_bustag;
1556 bus_space_handle_t br = sc->sc_br;
1557
1558 if (IFM_TYPE(newmedia) != IFM_ETHER)
1559 return (EINVAL);
1560
1561 if ((sc->sc_conf & BE_CONF_MII) != 0) {
1562 int error;
1563
1564 if ((error = mii_mediachg(&sc->sc_mii)) != 0)
1565 return (error);
1566
1567 v = bus_space_read_4(t, br, BE_BRI_TXCFG);
1568 if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0)
1569 v |= BE_BR_TXCFG_FULLDPLX;
1570 else
1571 v &= ~BE_BR_TXCFG_FULLDPLX;
1572 bus_space_write_4(t, br, BE_BRI_TXCFG, v);
1573
1574 return (0);
1575 }
1576
1577 /*
1578 * The rest of this routine is devoted to the
1579 * not-quite-a-phy internal transceiver case.
1580 */
1581 phy = BE_PHY_INTERNAL;
1582
1583 /* Why must we reset the device? */
1584 if ((error = be_mii_reset(sc, phy)) != 0)
1585 return (error);
1586
1587 bmcr = be_mii_readreg((struct device *)sc, phy, MII_BMCR);
1588
1589 if (IFM_SUBTYPE(newmedia) == IFM_100_TX) {
1590 bmcr |= BMCR_S100;
1591 speed = "100baseTX";
1592 } else if (IFM_SUBTYPE(newmedia) == IFM_10_T) {
1593 bmcr &= ~BMCR_S100;
1594 speed = "10baseT";
1595 } else {
1596 speed = "auto sense";
1597 }
1598
1599 printf("%s: selecting %s", sc->sc_dev.dv_xname, speed);
1600
1601 v = bus_space_read_4(t, br, BE_BRI_TXCFG);
1602 if ((IFM_OPTIONS(newmedia) & IFM_FDX) != 0) {
1603 bmcr |= BMCR_FDX;
1604 v |= BE_BR_TXCFG_FULLDPLX;
1605 mode = "full";
1606 } else {
1607 bmcr &= ~BMCR_FDX;
1608 v &= ~BE_BR_TXCFG_FULLDPLX;
1609 mode = "half";
1610 }
1611 bus_space_write_4(t, br, BE_BRI_TXCFG, v);
1612 printf(" %s-duplex\n", mode);
1613
1614 /* Select the new mode and take out of isolation */
1615 be_mii_writereg((struct device *)sc, phy, MII_BMCR, bmcr & ~BMCR_ISO);
1616
1617 for (n = 32; n >= 0; n--) {
1618 bmcr = be_mii_readreg((struct device *)sc, phy, MII_BMCR);
1619 if ((bmcr & BMCR_ISO) == 0)
1620 break;
1621 DELAY(20);
1622 }
1623 if (n == 0) {
1624 printf("%s: bmcr unisolate failed\n", sc->sc_dev.dv_xname);
1625 return (EIO);
1626 }
1627
1628 return (0);
1629 }
1630