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