if_my.c revision 268226
1/*- 2 * Written by: yen_cw@myson.com.tw 3 * Copyright (c) 2002 Myson Technology Inc. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions, and the following disclaimer, 11 * without modification, immediately at the beginning of the file. 12 * 2. The name of the author may not be used to endorse or promote products 13 * derived from this software without specific prior written permission. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * Myson fast ethernet PCI NIC driver, available at: http://www.myson.com.tw/ 28 */ 29 30#include <sys/cdefs.h> 31__FBSDID("$FreeBSD: releng/9.3/sys/dev/my/if_my.c 268226 2014-07-03 17:42:26Z hselasky $"); 32 33#include <sys/param.h> 34#include <sys/systm.h> 35#include <sys/sockio.h> 36#include <sys/mbuf.h> 37#include <sys/malloc.h> 38#include <sys/kernel.h> 39#include <sys/socket.h> 40#include <sys/queue.h> 41#include <sys/types.h> 42#include <sys/module.h> 43#include <sys/lock.h> 44#include <sys/mutex.h> 45 46#define NBPFILTER 1 47 48#include <net/if.h> 49#include <net/if_arp.h> 50#include <net/ethernet.h> 51#include <net/if_media.h> 52#include <net/if_types.h> 53#include <net/if_dl.h> 54#include <net/bpf.h> 55 56#include <vm/vm.h> /* for vtophys */ 57#include <vm/pmap.h> /* for vtophys */ 58#include <machine/bus.h> 59#include <machine/resource.h> 60#include <sys/bus.h> 61#include <sys/rman.h> 62 63#include <dev/pci/pcireg.h> 64#include <dev/pci/pcivar.h> 65 66/* 67 * #define MY_USEIOSPACE 68 */ 69 70static int MY_USEIOSPACE = 1; 71 72#ifdef MY_USEIOSPACE 73#define MY_RES SYS_RES_IOPORT 74#define MY_RID MY_PCI_LOIO 75#else 76#define MY_RES SYS_RES_MEMORY 77#define MY_RID MY_PCI_LOMEM 78#endif 79 80 81#include <dev/my/if_myreg.h> 82 83/* 84 * Various supported device vendors/types and their names. 85 */ 86struct my_type *my_info_tmp; 87static struct my_type my_devs[] = { 88 {MYSONVENDORID, MTD800ID, "Myson MTD80X Based Fast Ethernet Card"}, 89 {MYSONVENDORID, MTD803ID, "Myson MTD80X Based Fast Ethernet Card"}, 90 {MYSONVENDORID, MTD891ID, "Myson MTD89X Based Giga Ethernet Card"}, 91 {0, 0, NULL} 92}; 93 94/* 95 * Various supported PHY vendors/types and their names. Note that this driver 96 * will work with pretty much any MII-compliant PHY, so failure to positively 97 * identify the chip is not a fatal error. 98 */ 99static struct my_type my_phys[] = { 100 {MysonPHYID0, MysonPHYID0, "<MYSON MTD981>"}, 101 {SeeqPHYID0, SeeqPHYID0, "<SEEQ 80225>"}, 102 {AhdocPHYID0, AhdocPHYID0, "<AHDOC 101>"}, 103 {MarvellPHYID0, MarvellPHYID0, "<MARVELL 88E1000>"}, 104 {LevelOnePHYID0, LevelOnePHYID0, "<LevelOne LXT1000>"}, 105 {0, 0, "<MII-compliant physical interface>"} 106}; 107 108static int my_probe(device_t); 109static int my_attach(device_t); 110static int my_detach(device_t); 111static int my_newbuf(struct my_softc *, struct my_chain_onefrag *); 112static int my_encap(struct my_softc *, struct my_chain *, struct mbuf *); 113static void my_rxeof(struct my_softc *); 114static void my_txeof(struct my_softc *); 115static void my_txeoc(struct my_softc *); 116static void my_intr(void *); 117static void my_start(struct ifnet *); 118static void my_start_locked(struct ifnet *); 119static int my_ioctl(struct ifnet *, u_long, caddr_t); 120static void my_init(void *); 121static void my_init_locked(struct my_softc *); 122static void my_stop(struct my_softc *); 123static void my_autoneg_timeout(void *); 124static void my_watchdog(void *); 125static int my_shutdown(device_t); 126static int my_ifmedia_upd(struct ifnet *); 127static void my_ifmedia_sts(struct ifnet *, struct ifmediareq *); 128static u_int16_t my_phy_readreg(struct my_softc *, int); 129static void my_phy_writereg(struct my_softc *, int, int); 130static void my_autoneg_xmit(struct my_softc *); 131static void my_autoneg_mii(struct my_softc *, int, int); 132static void my_setmode_mii(struct my_softc *, int); 133static void my_getmode_mii(struct my_softc *); 134static void my_setcfg(struct my_softc *, int); 135static void my_setmulti(struct my_softc *); 136static void my_reset(struct my_softc *); 137static int my_list_rx_init(struct my_softc *); 138static int my_list_tx_init(struct my_softc *); 139static long my_send_cmd_to_phy(struct my_softc *, int, int); 140 141#define MY_SETBIT(sc, reg, x) CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x)) 142#define MY_CLRBIT(sc, reg, x) CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x)) 143 144static device_method_t my_methods[] = { 145 /* Device interface */ 146 DEVMETHOD(device_probe, my_probe), 147 DEVMETHOD(device_attach, my_attach), 148 DEVMETHOD(device_detach, my_detach), 149 DEVMETHOD(device_shutdown, my_shutdown), 150 151 {0, 0} 152}; 153 154static driver_t my_driver = { 155 "my", 156 my_methods, 157 sizeof(struct my_softc) 158}; 159 160static devclass_t my_devclass; 161 162DRIVER_MODULE(my, pci, my_driver, my_devclass, 0, 0); 163MODULE_DEPEND(my, pci, 1, 1, 1); 164MODULE_DEPEND(my, ether, 1, 1, 1); 165 166static long 167my_send_cmd_to_phy(struct my_softc * sc, int opcode, int regad) 168{ 169 long miir; 170 int i; 171 int mask, data; 172 173 MY_LOCK_ASSERT(sc); 174 175 /* enable MII output */ 176 miir = CSR_READ_4(sc, MY_MANAGEMENT); 177 miir &= 0xfffffff0; 178 179 miir |= MY_MASK_MIIR_MII_WRITE + MY_MASK_MIIR_MII_MDO; 180 181 /* send 32 1's preamble */ 182 for (i = 0; i < 32; i++) { 183 /* low MDC; MDO is already high (miir) */ 184 miir &= ~MY_MASK_MIIR_MII_MDC; 185 CSR_WRITE_4(sc, MY_MANAGEMENT, miir); 186 187 /* high MDC */ 188 miir |= MY_MASK_MIIR_MII_MDC; 189 CSR_WRITE_4(sc, MY_MANAGEMENT, miir); 190 } 191 192 /* calculate ST+OP+PHYAD+REGAD+TA */ 193 data = opcode | (sc->my_phy_addr << 7) | (regad << 2); 194 195 /* sent out */ 196 mask = 0x8000; 197 while (mask) { 198 /* low MDC, prepare MDO */ 199 miir &= ~(MY_MASK_MIIR_MII_MDC + MY_MASK_MIIR_MII_MDO); 200 if (mask & data) 201 miir |= MY_MASK_MIIR_MII_MDO; 202 203 CSR_WRITE_4(sc, MY_MANAGEMENT, miir); 204 /* high MDC */ 205 miir |= MY_MASK_MIIR_MII_MDC; 206 CSR_WRITE_4(sc, MY_MANAGEMENT, miir); 207 DELAY(30); 208 209 /* next */ 210 mask >>= 1; 211 if (mask == 0x2 && opcode == MY_OP_READ) 212 miir &= ~MY_MASK_MIIR_MII_WRITE; 213 } 214 215 return miir; 216} 217 218 219static u_int16_t 220my_phy_readreg(struct my_softc * sc, int reg) 221{ 222 long miir; 223 int mask, data; 224 225 MY_LOCK_ASSERT(sc); 226 227 if (sc->my_info->my_did == MTD803ID) 228 data = CSR_READ_2(sc, MY_PHYBASE + reg * 2); 229 else { 230 miir = my_send_cmd_to_phy(sc, MY_OP_READ, reg); 231 232 /* read data */ 233 mask = 0x8000; 234 data = 0; 235 while (mask) { 236 /* low MDC */ 237 miir &= ~MY_MASK_MIIR_MII_MDC; 238 CSR_WRITE_4(sc, MY_MANAGEMENT, miir); 239 240 /* read MDI */ 241 miir = CSR_READ_4(sc, MY_MANAGEMENT); 242 if (miir & MY_MASK_MIIR_MII_MDI) 243 data |= mask; 244 245 /* high MDC, and wait */ 246 miir |= MY_MASK_MIIR_MII_MDC; 247 CSR_WRITE_4(sc, MY_MANAGEMENT, miir); 248 DELAY(30); 249 250 /* next */ 251 mask >>= 1; 252 } 253 254 /* low MDC */ 255 miir &= ~MY_MASK_MIIR_MII_MDC; 256 CSR_WRITE_4(sc, MY_MANAGEMENT, miir); 257 } 258 259 return (u_int16_t) data; 260} 261 262 263static void 264my_phy_writereg(struct my_softc * sc, int reg, int data) 265{ 266 long miir; 267 int mask; 268 269 MY_LOCK_ASSERT(sc); 270 271 if (sc->my_info->my_did == MTD803ID) 272 CSR_WRITE_2(sc, MY_PHYBASE + reg * 2, data); 273 else { 274 miir = my_send_cmd_to_phy(sc, MY_OP_WRITE, reg); 275 276 /* write data */ 277 mask = 0x8000; 278 while (mask) { 279 /* low MDC, prepare MDO */ 280 miir &= ~(MY_MASK_MIIR_MII_MDC + MY_MASK_MIIR_MII_MDO); 281 if (mask & data) 282 miir |= MY_MASK_MIIR_MII_MDO; 283 CSR_WRITE_4(sc, MY_MANAGEMENT, miir); 284 DELAY(1); 285 286 /* high MDC */ 287 miir |= MY_MASK_MIIR_MII_MDC; 288 CSR_WRITE_4(sc, MY_MANAGEMENT, miir); 289 DELAY(1); 290 291 /* next */ 292 mask >>= 1; 293 } 294 295 /* low MDC */ 296 miir &= ~MY_MASK_MIIR_MII_MDC; 297 CSR_WRITE_4(sc, MY_MANAGEMENT, miir); 298 } 299 return; 300} 301 302 303/* 304 * Program the 64-bit multicast hash filter. 305 */ 306static void 307my_setmulti(struct my_softc * sc) 308{ 309 struct ifnet *ifp; 310 int h = 0; 311 u_int32_t hashes[2] = {0, 0}; 312 struct ifmultiaddr *ifma; 313 u_int32_t rxfilt; 314 int mcnt = 0; 315 316 MY_LOCK_ASSERT(sc); 317 318 ifp = sc->my_ifp; 319 320 rxfilt = CSR_READ_4(sc, MY_TCRRCR); 321 322 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 323 rxfilt |= MY_AM; 324 CSR_WRITE_4(sc, MY_TCRRCR, rxfilt); 325 CSR_WRITE_4(sc, MY_MAR0, 0xFFFFFFFF); 326 CSR_WRITE_4(sc, MY_MAR1, 0xFFFFFFFF); 327 328 return; 329 } 330 /* first, zot all the existing hash bits */ 331 CSR_WRITE_4(sc, MY_MAR0, 0); 332 CSR_WRITE_4(sc, MY_MAR1, 0); 333 334 /* now program new ones */ 335 if_maddr_rlock(ifp); 336 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 337 if (ifma->ifma_addr->sa_family != AF_LINK) 338 continue; 339 h = ~ether_crc32_be(LLADDR((struct sockaddr_dl *) 340 ifma->ifma_addr), ETHER_ADDR_LEN) >> 26; 341 if (h < 32) 342 hashes[0] |= (1 << h); 343 else 344 hashes[1] |= (1 << (h - 32)); 345 mcnt++; 346 } 347 if_maddr_runlock(ifp); 348 349 if (mcnt) 350 rxfilt |= MY_AM; 351 else 352 rxfilt &= ~MY_AM; 353 CSR_WRITE_4(sc, MY_MAR0, hashes[0]); 354 CSR_WRITE_4(sc, MY_MAR1, hashes[1]); 355 CSR_WRITE_4(sc, MY_TCRRCR, rxfilt); 356 return; 357} 358 359/* 360 * Initiate an autonegotiation session. 361 */ 362static void 363my_autoneg_xmit(struct my_softc * sc) 364{ 365 u_int16_t phy_sts = 0; 366 367 MY_LOCK_ASSERT(sc); 368 369 my_phy_writereg(sc, PHY_BMCR, PHY_BMCR_RESET); 370 DELAY(500); 371 while (my_phy_readreg(sc, PHY_BMCR) & PHY_BMCR_RESET); 372 373 phy_sts = my_phy_readreg(sc, PHY_BMCR); 374 phy_sts |= PHY_BMCR_AUTONEGENBL | PHY_BMCR_AUTONEGRSTR; 375 my_phy_writereg(sc, PHY_BMCR, phy_sts); 376 377 return; 378} 379 380static void 381my_autoneg_timeout(void *arg) 382{ 383 struct my_softc *sc; 384 385 sc = arg; 386 MY_LOCK_ASSERT(sc); 387 my_autoneg_mii(sc, MY_FLAG_DELAYTIMEO, 1); 388} 389 390/* 391 * Invoke autonegotiation on a PHY. 392 */ 393static void 394my_autoneg_mii(struct my_softc * sc, int flag, int verbose) 395{ 396 u_int16_t phy_sts = 0, media, advert, ability; 397 u_int16_t ability2 = 0; 398 struct ifnet *ifp; 399 struct ifmedia *ifm; 400 401 MY_LOCK_ASSERT(sc); 402 403 ifm = &sc->ifmedia; 404 ifp = sc->my_ifp; 405 406 ifm->ifm_media = IFM_ETHER | IFM_AUTO; 407 408#ifndef FORCE_AUTONEG_TFOUR 409 /* 410 * First, see if autoneg is supported. If not, there's no point in 411 * continuing. 412 */ 413 phy_sts = my_phy_readreg(sc, PHY_BMSR); 414 if (!(phy_sts & PHY_BMSR_CANAUTONEG)) { 415 if (verbose) 416 device_printf(sc->my_dev, 417 "autonegotiation not supported\n"); 418 ifm->ifm_media = IFM_ETHER | IFM_10_T | IFM_HDX; 419 return; 420 } 421#endif 422 switch (flag) { 423 case MY_FLAG_FORCEDELAY: 424 /* 425 * XXX Never use this option anywhere but in the probe 426 * routine: making the kernel stop dead in its tracks for 427 * three whole seconds after we've gone multi-user is really 428 * bad manners. 429 */ 430 my_autoneg_xmit(sc); 431 DELAY(5000000); 432 break; 433 case MY_FLAG_SCHEDDELAY: 434 /* 435 * Wait for the transmitter to go idle before starting an 436 * autoneg session, otherwise my_start() may clobber our 437 * timeout, and we don't want to allow transmission during an 438 * autoneg session since that can screw it up. 439 */ 440 if (sc->my_cdata.my_tx_head != NULL) { 441 sc->my_want_auto = 1; 442 MY_UNLOCK(sc); 443 return; 444 } 445 my_autoneg_xmit(sc); 446 callout_reset(&sc->my_autoneg_timer, hz * 5, my_autoneg_timeout, 447 sc); 448 sc->my_autoneg = 1; 449 sc->my_want_auto = 0; 450 return; 451 case MY_FLAG_DELAYTIMEO: 452 callout_stop(&sc->my_autoneg_timer); 453 sc->my_autoneg = 0; 454 break; 455 default: 456 device_printf(sc->my_dev, "invalid autoneg flag: %d\n", flag); 457 return; 458 } 459 460 if (my_phy_readreg(sc, PHY_BMSR) & PHY_BMSR_AUTONEGCOMP) { 461 if (verbose) 462 device_printf(sc->my_dev, "autoneg complete, "); 463 phy_sts = my_phy_readreg(sc, PHY_BMSR); 464 } else { 465 if (verbose) 466 device_printf(sc->my_dev, "autoneg not complete, "); 467 } 468 469 media = my_phy_readreg(sc, PHY_BMCR); 470 471 /* Link is good. Report modes and set duplex mode. */ 472 if (my_phy_readreg(sc, PHY_BMSR) & PHY_BMSR_LINKSTAT) { 473 if (verbose) 474 device_printf(sc->my_dev, "link status good. "); 475 advert = my_phy_readreg(sc, PHY_ANAR); 476 ability = my_phy_readreg(sc, PHY_LPAR); 477 if ((sc->my_pinfo->my_vid == MarvellPHYID0) || 478 (sc->my_pinfo->my_vid == LevelOnePHYID0)) { 479 ability2 = my_phy_readreg(sc, PHY_1000SR); 480 if (ability2 & PHY_1000SR_1000BTXFULL) { 481 advert = 0; 482 ability = 0; 483 /* 484 * this version did not support 1000M, 485 * ifm->ifm_media = 486 * IFM_ETHER|IFM_1000_T|IFM_FDX; 487 */ 488 ifm->ifm_media = 489 IFM_ETHER | IFM_100_TX | IFM_FDX; 490 media &= ~PHY_BMCR_SPEEDSEL; 491 media |= PHY_BMCR_1000; 492 media |= PHY_BMCR_DUPLEX; 493 printf("(full-duplex, 1000Mbps)\n"); 494 } else if (ability2 & PHY_1000SR_1000BTXHALF) { 495 advert = 0; 496 ability = 0; 497 /* 498 * this version did not support 1000M, 499 * ifm->ifm_media = IFM_ETHER|IFM_1000_T; 500 */ 501 ifm->ifm_media = IFM_ETHER | IFM_100_TX; 502 media &= ~PHY_BMCR_SPEEDSEL; 503 media &= ~PHY_BMCR_DUPLEX; 504 media |= PHY_BMCR_1000; 505 printf("(half-duplex, 1000Mbps)\n"); 506 } 507 } 508 if (advert & PHY_ANAR_100BT4 && ability & PHY_ANAR_100BT4) { 509 ifm->ifm_media = IFM_ETHER | IFM_100_T4; 510 media |= PHY_BMCR_SPEEDSEL; 511 media &= ~PHY_BMCR_DUPLEX; 512 printf("(100baseT4)\n"); 513 } else if (advert & PHY_ANAR_100BTXFULL && 514 ability & PHY_ANAR_100BTXFULL) { 515 ifm->ifm_media = IFM_ETHER | IFM_100_TX | IFM_FDX; 516 media |= PHY_BMCR_SPEEDSEL; 517 media |= PHY_BMCR_DUPLEX; 518 printf("(full-duplex, 100Mbps)\n"); 519 } else if (advert & PHY_ANAR_100BTXHALF && 520 ability & PHY_ANAR_100BTXHALF) { 521 ifm->ifm_media = IFM_ETHER | IFM_100_TX | IFM_HDX; 522 media |= PHY_BMCR_SPEEDSEL; 523 media &= ~PHY_BMCR_DUPLEX; 524 printf("(half-duplex, 100Mbps)\n"); 525 } else if (advert & PHY_ANAR_10BTFULL && 526 ability & PHY_ANAR_10BTFULL) { 527 ifm->ifm_media = IFM_ETHER | IFM_10_T | IFM_FDX; 528 media &= ~PHY_BMCR_SPEEDSEL; 529 media |= PHY_BMCR_DUPLEX; 530 printf("(full-duplex, 10Mbps)\n"); 531 } else if (advert) { 532 ifm->ifm_media = IFM_ETHER | IFM_10_T | IFM_HDX; 533 media &= ~PHY_BMCR_SPEEDSEL; 534 media &= ~PHY_BMCR_DUPLEX; 535 printf("(half-duplex, 10Mbps)\n"); 536 } 537 media &= ~PHY_BMCR_AUTONEGENBL; 538 539 /* Set ASIC's duplex mode to match the PHY. */ 540 my_phy_writereg(sc, PHY_BMCR, media); 541 my_setcfg(sc, media); 542 } else { 543 if (verbose) 544 device_printf(sc->my_dev, "no carrier\n"); 545 } 546 547 my_init_locked(sc); 548 if (sc->my_tx_pend) { 549 sc->my_autoneg = 0; 550 sc->my_tx_pend = 0; 551 my_start_locked(ifp); 552 } 553 return; 554} 555 556/* 557 * To get PHY ability. 558 */ 559static void 560my_getmode_mii(struct my_softc * sc) 561{ 562 u_int16_t bmsr; 563 struct ifnet *ifp; 564 565 MY_LOCK_ASSERT(sc); 566 ifp = sc->my_ifp; 567 bmsr = my_phy_readreg(sc, PHY_BMSR); 568 if (bootverbose) 569 device_printf(sc->my_dev, "PHY status word: %x\n", bmsr); 570 571 /* fallback */ 572 sc->ifmedia.ifm_media = IFM_ETHER | IFM_10_T | IFM_HDX; 573 574 if (bmsr & PHY_BMSR_10BTHALF) { 575 if (bootverbose) 576 device_printf(sc->my_dev, 577 "10Mbps half-duplex mode supported\n"); 578 ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_10_T | IFM_HDX, 579 0, NULL); 580 ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_10_T, 0, NULL); 581 } 582 if (bmsr & PHY_BMSR_10BTFULL) { 583 if (bootverbose) 584 device_printf(sc->my_dev, 585 "10Mbps full-duplex mode supported\n"); 586 587 ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_10_T | IFM_FDX, 588 0, NULL); 589 sc->ifmedia.ifm_media = IFM_ETHER | IFM_10_T | IFM_FDX; 590 } 591 if (bmsr & PHY_BMSR_100BTXHALF) { 592 if (bootverbose) 593 device_printf(sc->my_dev, 594 "100Mbps half-duplex mode supported\n"); 595 ifp->if_baudrate = 100000000; 596 ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_100_TX, 0, NULL); 597 ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_100_TX | IFM_HDX, 598 0, NULL); 599 sc->ifmedia.ifm_media = IFM_ETHER | IFM_100_TX | IFM_HDX; 600 } 601 if (bmsr & PHY_BMSR_100BTXFULL) { 602 if (bootverbose) 603 device_printf(sc->my_dev, 604 "100Mbps full-duplex mode supported\n"); 605 ifp->if_baudrate = 100000000; 606 ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_100_TX | IFM_FDX, 607 0, NULL); 608 sc->ifmedia.ifm_media = IFM_ETHER | IFM_100_TX | IFM_FDX; 609 } 610 /* Some also support 100BaseT4. */ 611 if (bmsr & PHY_BMSR_100BT4) { 612 if (bootverbose) 613 device_printf(sc->my_dev, "100baseT4 mode supported\n"); 614 ifp->if_baudrate = 100000000; 615 ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_100_T4, 0, NULL); 616 sc->ifmedia.ifm_media = IFM_ETHER | IFM_100_T4; 617#ifdef FORCE_AUTONEG_TFOUR 618 if (bootverbose) 619 device_printf(sc->my_dev, 620 "forcing on autoneg support for BT4\n"); 621 ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_AUTO, 0 NULL): 622 sc->ifmedia.ifm_media = IFM_ETHER | IFM_AUTO; 623#endif 624 } 625#if 0 /* this version did not support 1000M, */ 626 if (sc->my_pinfo->my_vid == MarvellPHYID0) { 627 if (bootverbose) 628 device_printf(sc->my_dev, 629 "1000Mbps half-duplex mode supported\n"); 630 631 ifp->if_baudrate = 1000000000; 632 ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_1000_T, 0, NULL); 633 ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_1000_T | IFM_HDX, 634 0, NULL); 635 if (bootverbose) 636 device_printf(sc->my_dev, 637 "1000Mbps full-duplex mode supported\n"); 638 ifp->if_baudrate = 1000000000; 639 ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_1000_T | IFM_FDX, 640 0, NULL); 641 sc->ifmedia.ifm_media = IFM_ETHER | IFM_1000_T | IFM_FDX; 642 } 643#endif 644 if (bmsr & PHY_BMSR_CANAUTONEG) { 645 if (bootverbose) 646 device_printf(sc->my_dev, "autoneg supported\n"); 647 ifmedia_add(&sc->ifmedia, IFM_ETHER | IFM_AUTO, 0, NULL); 648 sc->ifmedia.ifm_media = IFM_ETHER | IFM_AUTO; 649 } 650 return; 651} 652 653/* 654 * Set speed and duplex mode. 655 */ 656static void 657my_setmode_mii(struct my_softc * sc, int media) 658{ 659 u_int16_t bmcr; 660 661 MY_LOCK_ASSERT(sc); 662 /* 663 * If an autoneg session is in progress, stop it. 664 */ 665 if (sc->my_autoneg) { 666 device_printf(sc->my_dev, "canceling autoneg session\n"); 667 callout_stop(&sc->my_autoneg_timer); 668 sc->my_autoneg = sc->my_want_auto = 0; 669 bmcr = my_phy_readreg(sc, PHY_BMCR); 670 bmcr &= ~PHY_BMCR_AUTONEGENBL; 671 my_phy_writereg(sc, PHY_BMCR, bmcr); 672 } 673 device_printf(sc->my_dev, "selecting MII, "); 674 bmcr = my_phy_readreg(sc, PHY_BMCR); 675 bmcr &= ~(PHY_BMCR_AUTONEGENBL | PHY_BMCR_SPEEDSEL | PHY_BMCR_1000 | 676 PHY_BMCR_DUPLEX | PHY_BMCR_LOOPBK); 677 678#if 0 /* this version did not support 1000M, */ 679 if (IFM_SUBTYPE(media) == IFM_1000_T) { 680 printf("1000Mbps/T4, half-duplex\n"); 681 bmcr &= ~PHY_BMCR_SPEEDSEL; 682 bmcr &= ~PHY_BMCR_DUPLEX; 683 bmcr |= PHY_BMCR_1000; 684 } 685#endif 686 if (IFM_SUBTYPE(media) == IFM_100_T4) { 687 printf("100Mbps/T4, half-duplex\n"); 688 bmcr |= PHY_BMCR_SPEEDSEL; 689 bmcr &= ~PHY_BMCR_DUPLEX; 690 } 691 if (IFM_SUBTYPE(media) == IFM_100_TX) { 692 printf("100Mbps, "); 693 bmcr |= PHY_BMCR_SPEEDSEL; 694 } 695 if (IFM_SUBTYPE(media) == IFM_10_T) { 696 printf("10Mbps, "); 697 bmcr &= ~PHY_BMCR_SPEEDSEL; 698 } 699 if ((media & IFM_GMASK) == IFM_FDX) { 700 printf("full duplex\n"); 701 bmcr |= PHY_BMCR_DUPLEX; 702 } else { 703 printf("half duplex\n"); 704 bmcr &= ~PHY_BMCR_DUPLEX; 705 } 706 my_phy_writereg(sc, PHY_BMCR, bmcr); 707 my_setcfg(sc, bmcr); 708 return; 709} 710 711/* 712 * The Myson manual states that in order to fiddle with the 'full-duplex' and 713 * '100Mbps' bits in the netconfig register, we first have to put the 714 * transmit and/or receive logic in the idle state. 715 */ 716static void 717my_setcfg(struct my_softc * sc, int bmcr) 718{ 719 int i, restart = 0; 720 721 MY_LOCK_ASSERT(sc); 722 if (CSR_READ_4(sc, MY_TCRRCR) & (MY_TE | MY_RE)) { 723 restart = 1; 724 MY_CLRBIT(sc, MY_TCRRCR, (MY_TE | MY_RE)); 725 for (i = 0; i < MY_TIMEOUT; i++) { 726 DELAY(10); 727 if (!(CSR_READ_4(sc, MY_TCRRCR) & 728 (MY_TXRUN | MY_RXRUN))) 729 break; 730 } 731 if (i == MY_TIMEOUT) 732 device_printf(sc->my_dev, 733 "failed to force tx and rx to idle \n"); 734 } 735 MY_CLRBIT(sc, MY_TCRRCR, MY_PS1000); 736 MY_CLRBIT(sc, MY_TCRRCR, MY_PS10); 737 if (bmcr & PHY_BMCR_1000) 738 MY_SETBIT(sc, MY_TCRRCR, MY_PS1000); 739 else if (!(bmcr & PHY_BMCR_SPEEDSEL)) 740 MY_SETBIT(sc, MY_TCRRCR, MY_PS10); 741 if (bmcr & PHY_BMCR_DUPLEX) 742 MY_SETBIT(sc, MY_TCRRCR, MY_FD); 743 else 744 MY_CLRBIT(sc, MY_TCRRCR, MY_FD); 745 if (restart) 746 MY_SETBIT(sc, MY_TCRRCR, MY_TE | MY_RE); 747 return; 748} 749 750static void 751my_reset(struct my_softc * sc) 752{ 753 register int i; 754 755 MY_LOCK_ASSERT(sc); 756 MY_SETBIT(sc, MY_BCR, MY_SWR); 757 for (i = 0; i < MY_TIMEOUT; i++) { 758 DELAY(10); 759 if (!(CSR_READ_4(sc, MY_BCR) & MY_SWR)) 760 break; 761 } 762 if (i == MY_TIMEOUT) 763 device_printf(sc->my_dev, "reset never completed!\n"); 764 765 /* Wait a little while for the chip to get its brains in order. */ 766 DELAY(1000); 767 return; 768} 769 770/* 771 * Probe for a Myson chip. Check the PCI vendor and device IDs against our 772 * list and return a device name if we find a match. 773 */ 774static int 775my_probe(device_t dev) 776{ 777 struct my_type *t; 778 779 t = my_devs; 780 while (t->my_name != NULL) { 781 if ((pci_get_vendor(dev) == t->my_vid) && 782 (pci_get_device(dev) == t->my_did)) { 783 device_set_desc(dev, t->my_name); 784 my_info_tmp = t; 785 return (BUS_PROBE_DEFAULT); 786 } 787 t++; 788 } 789 return (ENXIO); 790} 791 792/* 793 * Attach the interface. Allocate softc structures, do ifmedia setup and 794 * ethernet/BPF attach. 795 */ 796static int 797my_attach(device_t dev) 798{ 799 int i; 800 u_char eaddr[ETHER_ADDR_LEN]; 801 u_int32_t iobase; 802 struct my_softc *sc; 803 struct ifnet *ifp; 804 int media = IFM_ETHER | IFM_100_TX | IFM_FDX; 805 unsigned int round; 806 caddr_t roundptr; 807 struct my_type *p; 808 u_int16_t phy_vid, phy_did, phy_sts = 0; 809 int rid, error = 0; 810 811 sc = device_get_softc(dev); 812 sc->my_dev = dev; 813 mtx_init(&sc->my_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 814 MTX_DEF); 815 callout_init_mtx(&sc->my_autoneg_timer, &sc->my_mtx, 0); 816 callout_init_mtx(&sc->my_watchdog, &sc->my_mtx, 0); 817 818 /* 819 * Map control/status registers. 820 */ 821 pci_enable_busmaster(dev); 822 823 if (my_info_tmp->my_did == MTD800ID) { 824 iobase = pci_read_config(dev, MY_PCI_LOIO, 4); 825 if (iobase & 0x300) 826 MY_USEIOSPACE = 0; 827 } 828 829 rid = MY_RID; 830 sc->my_res = bus_alloc_resource_any(dev, MY_RES, &rid, RF_ACTIVE); 831 832 if (sc->my_res == NULL) { 833 device_printf(dev, "couldn't map ports/memory\n"); 834 error = ENXIO; 835 goto destroy_mutex; 836 } 837 sc->my_btag = rman_get_bustag(sc->my_res); 838 sc->my_bhandle = rman_get_bushandle(sc->my_res); 839 840 rid = 0; 841 sc->my_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 842 RF_SHAREABLE | RF_ACTIVE); 843 844 if (sc->my_irq == NULL) { 845 device_printf(dev, "couldn't map interrupt\n"); 846 error = ENXIO; 847 goto release_io; 848 } 849 850 sc->my_info = my_info_tmp; 851 852 /* Reset the adapter. */ 853 MY_LOCK(sc); 854 my_reset(sc); 855 MY_UNLOCK(sc); 856 857 /* 858 * Get station address 859 */ 860 for (i = 0; i < ETHER_ADDR_LEN; ++i) 861 eaddr[i] = CSR_READ_1(sc, MY_PAR0 + i); 862 863 sc->my_ldata_ptr = malloc(sizeof(struct my_list_data) + 8, 864 M_DEVBUF, M_NOWAIT); 865 if (sc->my_ldata_ptr == NULL) { 866 device_printf(dev, "no memory for list buffers!\n"); 867 error = ENXIO; 868 goto release_irq; 869 } 870 sc->my_ldata = (struct my_list_data *) sc->my_ldata_ptr; 871 round = (uintptr_t)sc->my_ldata_ptr & 0xF; 872 roundptr = sc->my_ldata_ptr; 873 for (i = 0; i < 8; i++) { 874 if (round % 8) { 875 round++; 876 roundptr++; 877 } else 878 break; 879 } 880 sc->my_ldata = (struct my_list_data *) roundptr; 881 bzero(sc->my_ldata, sizeof(struct my_list_data)); 882 883 ifp = sc->my_ifp = if_alloc(IFT_ETHER); 884 if (ifp == NULL) { 885 device_printf(dev, "can not if_alloc()\n"); 886 error = ENOSPC; 887 goto free_ldata; 888 } 889 ifp->if_softc = sc; 890 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 891 ifp->if_mtu = ETHERMTU; 892 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 893 ifp->if_ioctl = my_ioctl; 894 ifp->if_start = my_start; 895 ifp->if_init = my_init; 896 ifp->if_baudrate = 10000000; 897 IFQ_SET_MAXLEN(&ifp->if_snd, ifqmaxlen); 898 ifp->if_snd.ifq_drv_maxlen = ifqmaxlen; 899 IFQ_SET_READY(&ifp->if_snd); 900 901 if (sc->my_info->my_did == MTD803ID) 902 sc->my_pinfo = my_phys; 903 else { 904 if (bootverbose) 905 device_printf(dev, "probing for a PHY\n"); 906 MY_LOCK(sc); 907 for (i = MY_PHYADDR_MIN; i < MY_PHYADDR_MAX + 1; i++) { 908 if (bootverbose) 909 device_printf(dev, "checking address: %d\n", i); 910 sc->my_phy_addr = i; 911 phy_sts = my_phy_readreg(sc, PHY_BMSR); 912 if ((phy_sts != 0) && (phy_sts != 0xffff)) 913 break; 914 else 915 phy_sts = 0; 916 } 917 if (phy_sts) { 918 phy_vid = my_phy_readreg(sc, PHY_VENID); 919 phy_did = my_phy_readreg(sc, PHY_DEVID); 920 if (bootverbose) { 921 device_printf(dev, "found PHY at address %d, ", 922 sc->my_phy_addr); 923 printf("vendor id: %x device id: %x\n", 924 phy_vid, phy_did); 925 } 926 p = my_phys; 927 while (p->my_vid) { 928 if (phy_vid == p->my_vid) { 929 sc->my_pinfo = p; 930 break; 931 } 932 p++; 933 } 934 if (sc->my_pinfo == NULL) 935 sc->my_pinfo = &my_phys[PHY_UNKNOWN]; 936 if (bootverbose) 937 device_printf(dev, "PHY type: %s\n", 938 sc->my_pinfo->my_name); 939 } else { 940 MY_UNLOCK(sc); 941 device_printf(dev, "MII without any phy!\n"); 942 error = ENXIO; 943 goto free_if; 944 } 945 MY_UNLOCK(sc); 946 } 947 948 /* Do ifmedia setup. */ 949 ifmedia_init(&sc->ifmedia, 0, my_ifmedia_upd, my_ifmedia_sts); 950 MY_LOCK(sc); 951 my_getmode_mii(sc); 952 my_autoneg_mii(sc, MY_FLAG_FORCEDELAY, 1); 953 media = sc->ifmedia.ifm_media; 954 my_stop(sc); 955 MY_UNLOCK(sc); 956 ifmedia_set(&sc->ifmedia, media); 957 958 ether_ifattach(ifp, eaddr); 959 960 error = bus_setup_intr(dev, sc->my_irq, INTR_TYPE_NET | INTR_MPSAFE, 961 NULL, my_intr, sc, &sc->my_intrhand); 962 963 if (error) { 964 device_printf(dev, "couldn't set up irq\n"); 965 goto detach_if; 966 } 967 968 return (0); 969 970detach_if: 971 ether_ifdetach(ifp); 972free_if: 973 if_free(ifp); 974free_ldata: 975 free(sc->my_ldata_ptr, M_DEVBUF); 976release_irq: 977 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->my_irq); 978release_io: 979 bus_release_resource(dev, MY_RES, MY_RID, sc->my_res); 980destroy_mutex: 981 mtx_destroy(&sc->my_mtx); 982 return (error); 983} 984 985static int 986my_detach(device_t dev) 987{ 988 struct my_softc *sc; 989 struct ifnet *ifp; 990 991 sc = device_get_softc(dev); 992 ifp = sc->my_ifp; 993 ether_ifdetach(ifp); 994 MY_LOCK(sc); 995 my_stop(sc); 996 MY_UNLOCK(sc); 997 bus_teardown_intr(dev, sc->my_irq, sc->my_intrhand); 998 callout_drain(&sc->my_watchdog); 999 callout_drain(&sc->my_autoneg_timer); 1000 1001 if_free(ifp); 1002 free(sc->my_ldata_ptr, M_DEVBUF); 1003 1004 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->my_irq); 1005 bus_release_resource(dev, MY_RES, MY_RID, sc->my_res); 1006 mtx_destroy(&sc->my_mtx); 1007 return (0); 1008} 1009 1010 1011/* 1012 * Initialize the transmit descriptors. 1013 */ 1014static int 1015my_list_tx_init(struct my_softc * sc) 1016{ 1017 struct my_chain_data *cd; 1018 struct my_list_data *ld; 1019 int i; 1020 1021 MY_LOCK_ASSERT(sc); 1022 cd = &sc->my_cdata; 1023 ld = sc->my_ldata; 1024 for (i = 0; i < MY_TX_LIST_CNT; i++) { 1025 cd->my_tx_chain[i].my_ptr = &ld->my_tx_list[i]; 1026 if (i == (MY_TX_LIST_CNT - 1)) 1027 cd->my_tx_chain[i].my_nextdesc = &cd->my_tx_chain[0]; 1028 else 1029 cd->my_tx_chain[i].my_nextdesc = 1030 &cd->my_tx_chain[i + 1]; 1031 } 1032 cd->my_tx_free = &cd->my_tx_chain[0]; 1033 cd->my_tx_tail = cd->my_tx_head = NULL; 1034 return (0); 1035} 1036 1037/* 1038 * Initialize the RX descriptors and allocate mbufs for them. Note that we 1039 * arrange the descriptors in a closed ring, so that the last descriptor 1040 * points back to the first. 1041 */ 1042static int 1043my_list_rx_init(struct my_softc * sc) 1044{ 1045 struct my_chain_data *cd; 1046 struct my_list_data *ld; 1047 int i; 1048 1049 MY_LOCK_ASSERT(sc); 1050 cd = &sc->my_cdata; 1051 ld = sc->my_ldata; 1052 for (i = 0; i < MY_RX_LIST_CNT; i++) { 1053 cd->my_rx_chain[i].my_ptr = 1054 (struct my_desc *) & ld->my_rx_list[i]; 1055 if (my_newbuf(sc, &cd->my_rx_chain[i]) == ENOBUFS) { 1056 MY_UNLOCK(sc); 1057 return (ENOBUFS); 1058 } 1059 if (i == (MY_RX_LIST_CNT - 1)) { 1060 cd->my_rx_chain[i].my_nextdesc = &cd->my_rx_chain[0]; 1061 ld->my_rx_list[i].my_next = vtophys(&ld->my_rx_list[0]); 1062 } else { 1063 cd->my_rx_chain[i].my_nextdesc = 1064 &cd->my_rx_chain[i + 1]; 1065 ld->my_rx_list[i].my_next = 1066 vtophys(&ld->my_rx_list[i + 1]); 1067 } 1068 } 1069 cd->my_rx_head = &cd->my_rx_chain[0]; 1070 return (0); 1071} 1072 1073/* 1074 * Initialize an RX descriptor and attach an MBUF cluster. 1075 */ 1076static int 1077my_newbuf(struct my_softc * sc, struct my_chain_onefrag * c) 1078{ 1079 struct mbuf *m_new = NULL; 1080 1081 MY_LOCK_ASSERT(sc); 1082 MGETHDR(m_new, M_NOWAIT, MT_DATA); 1083 if (m_new == NULL) { 1084 device_printf(sc->my_dev, 1085 "no memory for rx list -- packet dropped!\n"); 1086 return (ENOBUFS); 1087 } 1088 MCLGET(m_new, M_NOWAIT); 1089 if (!(m_new->m_flags & M_EXT)) { 1090 device_printf(sc->my_dev, 1091 "no memory for rx list -- packet dropped!\n"); 1092 m_freem(m_new); 1093 return (ENOBUFS); 1094 } 1095 c->my_mbuf = m_new; 1096 c->my_ptr->my_data = vtophys(mtod(m_new, caddr_t)); 1097 c->my_ptr->my_ctl = (MCLBYTES - 1) << MY_RBSShift; 1098 c->my_ptr->my_status = MY_OWNByNIC; 1099 return (0); 1100} 1101 1102/* 1103 * A frame has been uploaded: pass the resulting mbuf chain up to the higher 1104 * level protocols. 1105 */ 1106static void 1107my_rxeof(struct my_softc * sc) 1108{ 1109 struct ether_header *eh; 1110 struct mbuf *m; 1111 struct ifnet *ifp; 1112 struct my_chain_onefrag *cur_rx; 1113 int total_len = 0; 1114 u_int32_t rxstat; 1115 1116 MY_LOCK_ASSERT(sc); 1117 ifp = sc->my_ifp; 1118 while (!((rxstat = sc->my_cdata.my_rx_head->my_ptr->my_status) 1119 & MY_OWNByNIC)) { 1120 cur_rx = sc->my_cdata.my_rx_head; 1121 sc->my_cdata.my_rx_head = cur_rx->my_nextdesc; 1122 1123 if (rxstat & MY_ES) { /* error summary: give up this rx pkt */ 1124 ifp->if_ierrors++; 1125 cur_rx->my_ptr->my_status = MY_OWNByNIC; 1126 continue; 1127 } 1128 /* No errors; receive the packet. */ 1129 total_len = (rxstat & MY_FLNGMASK) >> MY_FLNGShift; 1130 total_len -= ETHER_CRC_LEN; 1131 1132 if (total_len < MINCLSIZE) { 1133 m = m_devget(mtod(cur_rx->my_mbuf, char *), 1134 total_len, 0, ifp, NULL); 1135 cur_rx->my_ptr->my_status = MY_OWNByNIC; 1136 if (m == NULL) { 1137 ifp->if_ierrors++; 1138 continue; 1139 } 1140 } else { 1141 m = cur_rx->my_mbuf; 1142 /* 1143 * Try to conjure up a new mbuf cluster. If that 1144 * fails, it means we have an out of memory condition 1145 * and should leave the buffer in place and continue. 1146 * This will result in a lost packet, but there's 1147 * little else we can do in this situation. 1148 */ 1149 if (my_newbuf(sc, cur_rx) == ENOBUFS) { 1150 ifp->if_ierrors++; 1151 cur_rx->my_ptr->my_status = MY_OWNByNIC; 1152 continue; 1153 } 1154 m->m_pkthdr.rcvif = ifp; 1155 m->m_pkthdr.len = m->m_len = total_len; 1156 } 1157 ifp->if_ipackets++; 1158 eh = mtod(m, struct ether_header *); 1159#if NBPFILTER > 0 1160 /* 1161 * Handle BPF listeners. Let the BPF user see the packet, but 1162 * don't pass it up to the ether_input() layer unless it's a 1163 * broadcast packet, multicast packet, matches our ethernet 1164 * address or the interface is in promiscuous mode. 1165 */ 1166 if (bpf_peers_present(ifp->if_bpf)) { 1167 bpf_mtap(ifp->if_bpf, m); 1168 if (ifp->if_flags & IFF_PROMISC && 1169 (bcmp(eh->ether_dhost, IF_LLADDR(sc->my_ifp), 1170 ETHER_ADDR_LEN) && 1171 (eh->ether_dhost[0] & 1) == 0)) { 1172 m_freem(m); 1173 continue; 1174 } 1175 } 1176#endif 1177 MY_UNLOCK(sc); 1178 (*ifp->if_input)(ifp, m); 1179 MY_LOCK(sc); 1180 } 1181 return; 1182} 1183 1184 1185/* 1186 * A frame was downloaded to the chip. It's safe for us to clean up the list 1187 * buffers. 1188 */ 1189static void 1190my_txeof(struct my_softc * sc) 1191{ 1192 struct my_chain *cur_tx; 1193 struct ifnet *ifp; 1194 1195 MY_LOCK_ASSERT(sc); 1196 ifp = sc->my_ifp; 1197 /* Clear the timeout timer. */ 1198 sc->my_timer = 0; 1199 if (sc->my_cdata.my_tx_head == NULL) { 1200 return; 1201 } 1202 /* 1203 * Go through our tx list and free mbufs for those frames that have 1204 * been transmitted. 1205 */ 1206 while (sc->my_cdata.my_tx_head->my_mbuf != NULL) { 1207 u_int32_t txstat; 1208 1209 cur_tx = sc->my_cdata.my_tx_head; 1210 txstat = MY_TXSTATUS(cur_tx); 1211 if ((txstat & MY_OWNByNIC) || txstat == MY_UNSENT) 1212 break; 1213 if (!(CSR_READ_4(sc, MY_TCRRCR) & MY_Enhanced)) { 1214 if (txstat & MY_TXERR) { 1215 ifp->if_oerrors++; 1216 if (txstat & MY_EC) /* excessive collision */ 1217 ifp->if_collisions++; 1218 if (txstat & MY_LC) /* late collision */ 1219 ifp->if_collisions++; 1220 } 1221 ifp->if_collisions += (txstat & MY_NCRMASK) >> 1222 MY_NCRShift; 1223 } 1224 ifp->if_opackets++; 1225 m_freem(cur_tx->my_mbuf); 1226 cur_tx->my_mbuf = NULL; 1227 if (sc->my_cdata.my_tx_head == sc->my_cdata.my_tx_tail) { 1228 sc->my_cdata.my_tx_head = NULL; 1229 sc->my_cdata.my_tx_tail = NULL; 1230 break; 1231 } 1232 sc->my_cdata.my_tx_head = cur_tx->my_nextdesc; 1233 } 1234 if (CSR_READ_4(sc, MY_TCRRCR) & MY_Enhanced) { 1235 ifp->if_collisions += (CSR_READ_4(sc, MY_TSR) & MY_NCRMask); 1236 } 1237 return; 1238} 1239 1240/* 1241 * TX 'end of channel' interrupt handler. 1242 */ 1243static void 1244my_txeoc(struct my_softc * sc) 1245{ 1246 struct ifnet *ifp; 1247 1248 MY_LOCK_ASSERT(sc); 1249 ifp = sc->my_ifp; 1250 sc->my_timer = 0; 1251 if (sc->my_cdata.my_tx_head == NULL) { 1252 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1253 sc->my_cdata.my_tx_tail = NULL; 1254 if (sc->my_want_auto) 1255 my_autoneg_mii(sc, MY_FLAG_SCHEDDELAY, 1); 1256 } else { 1257 if (MY_TXOWN(sc->my_cdata.my_tx_head) == MY_UNSENT) { 1258 MY_TXOWN(sc->my_cdata.my_tx_head) = MY_OWNByNIC; 1259 sc->my_timer = 5; 1260 CSR_WRITE_4(sc, MY_TXPDR, 0xFFFFFFFF); 1261 } 1262 } 1263 return; 1264} 1265 1266static void 1267my_intr(void *arg) 1268{ 1269 struct my_softc *sc; 1270 struct ifnet *ifp; 1271 u_int32_t status; 1272 1273 sc = arg; 1274 MY_LOCK(sc); 1275 ifp = sc->my_ifp; 1276 if (!(ifp->if_flags & IFF_UP)) { 1277 MY_UNLOCK(sc); 1278 return; 1279 } 1280 /* Disable interrupts. */ 1281 CSR_WRITE_4(sc, MY_IMR, 0x00000000); 1282 1283 for (;;) { 1284 status = CSR_READ_4(sc, MY_ISR); 1285 status &= MY_INTRS; 1286 if (status) 1287 CSR_WRITE_4(sc, MY_ISR, status); 1288 else 1289 break; 1290 1291 if (status & MY_RI) /* receive interrupt */ 1292 my_rxeof(sc); 1293 1294 if ((status & MY_RBU) || (status & MY_RxErr)) { 1295 /* rx buffer unavailable or rx error */ 1296 ifp->if_ierrors++; 1297#ifdef foo 1298 my_stop(sc); 1299 my_reset(sc); 1300 my_init_locked(sc); 1301#endif 1302 } 1303 if (status & MY_TI) /* tx interrupt */ 1304 my_txeof(sc); 1305 if (status & MY_ETI) /* tx early interrupt */ 1306 my_txeof(sc); 1307 if (status & MY_TBU) /* tx buffer unavailable */ 1308 my_txeoc(sc); 1309 1310#if 0 /* 90/1/18 delete */ 1311 if (status & MY_FBE) { 1312 my_reset(sc); 1313 my_init_locked(sc); 1314 } 1315#endif 1316 1317 } 1318 1319 /* Re-enable interrupts. */ 1320 CSR_WRITE_4(sc, MY_IMR, MY_INTRS); 1321 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 1322 my_start_locked(ifp); 1323 MY_UNLOCK(sc); 1324 return; 1325} 1326 1327/* 1328 * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data 1329 * pointers to the fragment pointers. 1330 */ 1331static int 1332my_encap(struct my_softc * sc, struct my_chain * c, struct mbuf * m_head) 1333{ 1334 struct my_desc *f = NULL; 1335 int total_len; 1336 struct mbuf *m, *m_new = NULL; 1337 1338 MY_LOCK_ASSERT(sc); 1339 /* calculate the total tx pkt length */ 1340 total_len = 0; 1341 for (m = m_head; m != NULL; m = m->m_next) 1342 total_len += m->m_len; 1343 /* 1344 * Start packing the mbufs in this chain into the fragment pointers. 1345 * Stop when we run out of fragments or hit the end of the mbuf 1346 * chain. 1347 */ 1348 m = m_head; 1349 MGETHDR(m_new, M_NOWAIT, MT_DATA); 1350 if (m_new == NULL) { 1351 device_printf(sc->my_dev, "no memory for tx list"); 1352 return (1); 1353 } 1354 if (m_head->m_pkthdr.len > MHLEN) { 1355 MCLGET(m_new, M_NOWAIT); 1356 if (!(m_new->m_flags & M_EXT)) { 1357 m_freem(m_new); 1358 device_printf(sc->my_dev, "no memory for tx list"); 1359 return (1); 1360 } 1361 } 1362 m_copydata(m_head, 0, m_head->m_pkthdr.len, mtod(m_new, caddr_t)); 1363 m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len; 1364 m_freem(m_head); 1365 m_head = m_new; 1366 f = &c->my_ptr->my_frag[0]; 1367 f->my_status = 0; 1368 f->my_data = vtophys(mtod(m_new, caddr_t)); 1369 total_len = m_new->m_len; 1370 f->my_ctl = MY_TXFD | MY_TXLD | MY_CRCEnable | MY_PADEnable; 1371 f->my_ctl |= total_len << MY_PKTShift; /* pkt size */ 1372 f->my_ctl |= total_len; /* buffer size */ 1373 /* 89/12/29 add, for mtd891 *//* [ 89? ] */ 1374 if (sc->my_info->my_did == MTD891ID) 1375 f->my_ctl |= MY_ETIControl | MY_RetryTxLC; 1376 c->my_mbuf = m_head; 1377 c->my_lastdesc = 0; 1378 MY_TXNEXT(c) = vtophys(&c->my_nextdesc->my_ptr->my_frag[0]); 1379 return (0); 1380} 1381 1382/* 1383 * Main transmit routine. To avoid having to do mbuf copies, we put pointers 1384 * to the mbuf data regions directly in the transmit lists. We also save a 1385 * copy of the pointers since the transmit list fragment pointers are 1386 * physical addresses. 1387 */ 1388static void 1389my_start(struct ifnet * ifp) 1390{ 1391 struct my_softc *sc; 1392 1393 sc = ifp->if_softc; 1394 MY_LOCK(sc); 1395 my_start_locked(ifp); 1396 MY_UNLOCK(sc); 1397} 1398 1399static void 1400my_start_locked(struct ifnet * ifp) 1401{ 1402 struct my_softc *sc; 1403 struct mbuf *m_head = NULL; 1404 struct my_chain *cur_tx = NULL, *start_tx; 1405 1406 sc = ifp->if_softc; 1407 MY_LOCK_ASSERT(sc); 1408 if (sc->my_autoneg) { 1409 sc->my_tx_pend = 1; 1410 return; 1411 } 1412 /* 1413 * Check for an available queue slot. If there are none, punt. 1414 */ 1415 if (sc->my_cdata.my_tx_free->my_mbuf != NULL) { 1416 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1417 return; 1418 } 1419 start_tx = sc->my_cdata.my_tx_free; 1420 while (sc->my_cdata.my_tx_free->my_mbuf == NULL) { 1421 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head); 1422 if (m_head == NULL) 1423 break; 1424 1425 /* Pick a descriptor off the free list. */ 1426 cur_tx = sc->my_cdata.my_tx_free; 1427 sc->my_cdata.my_tx_free = cur_tx->my_nextdesc; 1428 1429 /* Pack the data into the descriptor. */ 1430 my_encap(sc, cur_tx, m_head); 1431 1432 if (cur_tx != start_tx) 1433 MY_TXOWN(cur_tx) = MY_OWNByNIC; 1434#if NBPFILTER > 0 1435 /* 1436 * If there's a BPF listener, bounce a copy of this frame to 1437 * him. 1438 */ 1439 BPF_MTAP(ifp, cur_tx->my_mbuf); 1440#endif 1441 } 1442 /* 1443 * If there are no packets queued, bail. 1444 */ 1445 if (cur_tx == NULL) { 1446 return; 1447 } 1448 /* 1449 * Place the request for the upload interrupt in the last descriptor 1450 * in the chain. This way, if we're chaining several packets at once, 1451 * we'll only get an interrupt once for the whole chain rather than 1452 * once for each packet. 1453 */ 1454 MY_TXCTL(cur_tx) |= MY_TXIC; 1455 cur_tx->my_ptr->my_frag[0].my_ctl |= MY_TXIC; 1456 sc->my_cdata.my_tx_tail = cur_tx; 1457 if (sc->my_cdata.my_tx_head == NULL) 1458 sc->my_cdata.my_tx_head = start_tx; 1459 MY_TXOWN(start_tx) = MY_OWNByNIC; 1460 CSR_WRITE_4(sc, MY_TXPDR, 0xFFFFFFFF); /* tx polling demand */ 1461 1462 /* 1463 * Set a timeout in case the chip goes out to lunch. 1464 */ 1465 sc->my_timer = 5; 1466 return; 1467} 1468 1469static void 1470my_init(void *xsc) 1471{ 1472 struct my_softc *sc = xsc; 1473 1474 MY_LOCK(sc); 1475 my_init_locked(sc); 1476 MY_UNLOCK(sc); 1477} 1478 1479static void 1480my_init_locked(struct my_softc *sc) 1481{ 1482 struct ifnet *ifp = sc->my_ifp; 1483 u_int16_t phy_bmcr = 0; 1484 1485 MY_LOCK_ASSERT(sc); 1486 if (sc->my_autoneg) { 1487 return; 1488 } 1489 if (sc->my_pinfo != NULL) 1490 phy_bmcr = my_phy_readreg(sc, PHY_BMCR); 1491 /* 1492 * Cancel pending I/O and free all RX/TX buffers. 1493 */ 1494 my_stop(sc); 1495 my_reset(sc); 1496 1497 /* 1498 * Set cache alignment and burst length. 1499 */ 1500#if 0 /* 89/9/1 modify, */ 1501 CSR_WRITE_4(sc, MY_BCR, MY_RPBLE512); 1502 CSR_WRITE_4(sc, MY_TCRRCR, MY_TFTSF); 1503#endif 1504 CSR_WRITE_4(sc, MY_BCR, MY_PBL8); 1505 CSR_WRITE_4(sc, MY_TCRRCR, MY_TFTSF | MY_RBLEN | MY_RPBLE512); 1506 /* 1507 * 89/12/29 add, for mtd891, 1508 */ 1509 if (sc->my_info->my_did == MTD891ID) { 1510 MY_SETBIT(sc, MY_BCR, MY_PROG); 1511 MY_SETBIT(sc, MY_TCRRCR, MY_Enhanced); 1512 } 1513 my_setcfg(sc, phy_bmcr); 1514 /* Init circular RX list. */ 1515 if (my_list_rx_init(sc) == ENOBUFS) { 1516 device_printf(sc->my_dev, "init failed: no memory for rx buffers\n"); 1517 my_stop(sc); 1518 return; 1519 } 1520 /* Init TX descriptors. */ 1521 my_list_tx_init(sc); 1522 1523 /* If we want promiscuous mode, set the allframes bit. */ 1524 if (ifp->if_flags & IFF_PROMISC) 1525 MY_SETBIT(sc, MY_TCRRCR, MY_PROM); 1526 else 1527 MY_CLRBIT(sc, MY_TCRRCR, MY_PROM); 1528 1529 /* 1530 * Set capture broadcast bit to capture broadcast frames. 1531 */ 1532 if (ifp->if_flags & IFF_BROADCAST) 1533 MY_SETBIT(sc, MY_TCRRCR, MY_AB); 1534 else 1535 MY_CLRBIT(sc, MY_TCRRCR, MY_AB); 1536 1537 /* 1538 * Program the multicast filter, if necessary. 1539 */ 1540 my_setmulti(sc); 1541 1542 /* 1543 * Load the address of the RX list. 1544 */ 1545 MY_CLRBIT(sc, MY_TCRRCR, MY_RE); 1546 CSR_WRITE_4(sc, MY_RXLBA, vtophys(&sc->my_ldata->my_rx_list[0])); 1547 1548 /* 1549 * Enable interrupts. 1550 */ 1551 CSR_WRITE_4(sc, MY_IMR, MY_INTRS); 1552 CSR_WRITE_4(sc, MY_ISR, 0xFFFFFFFF); 1553 1554 /* Enable receiver and transmitter. */ 1555 MY_SETBIT(sc, MY_TCRRCR, MY_RE); 1556 MY_CLRBIT(sc, MY_TCRRCR, MY_TE); 1557 CSR_WRITE_4(sc, MY_TXLBA, vtophys(&sc->my_ldata->my_tx_list[0])); 1558 MY_SETBIT(sc, MY_TCRRCR, MY_TE); 1559 1560 /* Restore state of BMCR */ 1561 if (sc->my_pinfo != NULL) 1562 my_phy_writereg(sc, PHY_BMCR, phy_bmcr); 1563 ifp->if_drv_flags |= IFF_DRV_RUNNING; 1564 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1565 1566 callout_reset(&sc->my_watchdog, hz, my_watchdog, sc); 1567 return; 1568} 1569 1570/* 1571 * Set media options. 1572 */ 1573 1574static int 1575my_ifmedia_upd(struct ifnet * ifp) 1576{ 1577 struct my_softc *sc; 1578 struct ifmedia *ifm; 1579 1580 sc = ifp->if_softc; 1581 MY_LOCK(sc); 1582 ifm = &sc->ifmedia; 1583 if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER) { 1584 MY_UNLOCK(sc); 1585 return (EINVAL); 1586 } 1587 if (IFM_SUBTYPE(ifm->ifm_media) == IFM_AUTO) 1588 my_autoneg_mii(sc, MY_FLAG_SCHEDDELAY, 1); 1589 else 1590 my_setmode_mii(sc, ifm->ifm_media); 1591 MY_UNLOCK(sc); 1592 return (0); 1593} 1594 1595/* 1596 * Report current media status. 1597 */ 1598 1599static void 1600my_ifmedia_sts(struct ifnet * ifp, struct ifmediareq * ifmr) 1601{ 1602 struct my_softc *sc; 1603 u_int16_t advert = 0, ability = 0; 1604 1605 sc = ifp->if_softc; 1606 MY_LOCK(sc); 1607 ifmr->ifm_active = IFM_ETHER; 1608 if (!(my_phy_readreg(sc, PHY_BMCR) & PHY_BMCR_AUTONEGENBL)) { 1609#if 0 /* this version did not support 1000M, */ 1610 if (my_phy_readreg(sc, PHY_BMCR) & PHY_BMCR_1000) 1611 ifmr->ifm_active = IFM_ETHER | IFM_1000TX; 1612#endif 1613 if (my_phy_readreg(sc, PHY_BMCR) & PHY_BMCR_SPEEDSEL) 1614 ifmr->ifm_active = IFM_ETHER | IFM_100_TX; 1615 else 1616 ifmr->ifm_active = IFM_ETHER | IFM_10_T; 1617 if (my_phy_readreg(sc, PHY_BMCR) & PHY_BMCR_DUPLEX) 1618 ifmr->ifm_active |= IFM_FDX; 1619 else 1620 ifmr->ifm_active |= IFM_HDX; 1621 1622 MY_UNLOCK(sc); 1623 return; 1624 } 1625 ability = my_phy_readreg(sc, PHY_LPAR); 1626 advert = my_phy_readreg(sc, PHY_ANAR); 1627 1628#if 0 /* this version did not support 1000M, */ 1629 if (sc->my_pinfo->my_vid = MarvellPHYID0) { 1630 ability2 = my_phy_readreg(sc, PHY_1000SR); 1631 if (ability2 & PHY_1000SR_1000BTXFULL) { 1632 advert = 0; 1633 ability = 0; 1634 ifmr->ifm_active = IFM_ETHER|IFM_1000_T|IFM_FDX; 1635 } else if (ability & PHY_1000SR_1000BTXHALF) { 1636 advert = 0; 1637 ability = 0; 1638 ifmr->ifm_active = IFM_ETHER|IFM_1000_T|IFM_HDX; 1639 } 1640 } 1641#endif 1642 if (advert & PHY_ANAR_100BT4 && ability & PHY_ANAR_100BT4) 1643 ifmr->ifm_active = IFM_ETHER | IFM_100_T4; 1644 else if (advert & PHY_ANAR_100BTXFULL && ability & PHY_ANAR_100BTXFULL) 1645 ifmr->ifm_active = IFM_ETHER | IFM_100_TX | IFM_FDX; 1646 else if (advert & PHY_ANAR_100BTXHALF && ability & PHY_ANAR_100BTXHALF) 1647 ifmr->ifm_active = IFM_ETHER | IFM_100_TX | IFM_HDX; 1648 else if (advert & PHY_ANAR_10BTFULL && ability & PHY_ANAR_10BTFULL) 1649 ifmr->ifm_active = IFM_ETHER | IFM_10_T | IFM_FDX; 1650 else if (advert & PHY_ANAR_10BTHALF && ability & PHY_ANAR_10BTHALF) 1651 ifmr->ifm_active = IFM_ETHER | IFM_10_T | IFM_HDX; 1652 MY_UNLOCK(sc); 1653 return; 1654} 1655 1656static int 1657my_ioctl(struct ifnet * ifp, u_long command, caddr_t data) 1658{ 1659 struct my_softc *sc = ifp->if_softc; 1660 struct ifreq *ifr = (struct ifreq *) data; 1661 int error; 1662 1663 switch (command) { 1664 case SIOCSIFFLAGS: 1665 MY_LOCK(sc); 1666 if (ifp->if_flags & IFF_UP) 1667 my_init_locked(sc); 1668 else if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1669 my_stop(sc); 1670 MY_UNLOCK(sc); 1671 error = 0; 1672 break; 1673 case SIOCADDMULTI: 1674 case SIOCDELMULTI: 1675 MY_LOCK(sc); 1676 my_setmulti(sc); 1677 MY_UNLOCK(sc); 1678 error = 0; 1679 break; 1680 case SIOCGIFMEDIA: 1681 case SIOCSIFMEDIA: 1682 error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command); 1683 break; 1684 default: 1685 error = ether_ioctl(ifp, command, data); 1686 break; 1687 } 1688 return (error); 1689} 1690 1691static void 1692my_watchdog(void *arg) 1693{ 1694 struct my_softc *sc; 1695 struct ifnet *ifp; 1696 1697 sc = arg; 1698 MY_LOCK_ASSERT(sc); 1699 callout_reset(&sc->my_watchdog, hz, my_watchdog, sc); 1700 if (sc->my_timer == 0 || --sc->my_timer > 0) 1701 return; 1702 1703 ifp = sc->my_ifp; 1704 ifp->if_oerrors++; 1705 if_printf(ifp, "watchdog timeout\n"); 1706 if (!(my_phy_readreg(sc, PHY_BMSR) & PHY_BMSR_LINKSTAT)) 1707 if_printf(ifp, "no carrier - transceiver cable problem?\n"); 1708 my_stop(sc); 1709 my_reset(sc); 1710 my_init_locked(sc); 1711 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 1712 my_start_locked(ifp); 1713} 1714 1715 1716/* 1717 * Stop the adapter and free any mbufs allocated to the RX and TX lists. 1718 */ 1719static void 1720my_stop(struct my_softc * sc) 1721{ 1722 register int i; 1723 struct ifnet *ifp; 1724 1725 MY_LOCK_ASSERT(sc); 1726 ifp = sc->my_ifp; 1727 1728 callout_stop(&sc->my_autoneg_timer); 1729 callout_stop(&sc->my_watchdog); 1730 1731 MY_CLRBIT(sc, MY_TCRRCR, (MY_RE | MY_TE)); 1732 CSR_WRITE_4(sc, MY_IMR, 0x00000000); 1733 CSR_WRITE_4(sc, MY_TXLBA, 0x00000000); 1734 CSR_WRITE_4(sc, MY_RXLBA, 0x00000000); 1735 1736 /* 1737 * Free data in the RX lists. 1738 */ 1739 for (i = 0; i < MY_RX_LIST_CNT; i++) { 1740 if (sc->my_cdata.my_rx_chain[i].my_mbuf != NULL) { 1741 m_freem(sc->my_cdata.my_rx_chain[i].my_mbuf); 1742 sc->my_cdata.my_rx_chain[i].my_mbuf = NULL; 1743 } 1744 } 1745 bzero((char *)&sc->my_ldata->my_rx_list, 1746 sizeof(sc->my_ldata->my_rx_list)); 1747 /* 1748 * Free the TX list buffers. 1749 */ 1750 for (i = 0; i < MY_TX_LIST_CNT; i++) { 1751 if (sc->my_cdata.my_tx_chain[i].my_mbuf != NULL) { 1752 m_freem(sc->my_cdata.my_tx_chain[i].my_mbuf); 1753 sc->my_cdata.my_tx_chain[i].my_mbuf = NULL; 1754 } 1755 } 1756 bzero((char *)&sc->my_ldata->my_tx_list, 1757 sizeof(sc->my_ldata->my_tx_list)); 1758 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 1759 return; 1760} 1761 1762/* 1763 * Stop all chip I/O so that the kernel's probe routines don't get confused 1764 * by errant DMAs when rebooting. 1765 */ 1766static int 1767my_shutdown(device_t dev) 1768{ 1769 struct my_softc *sc; 1770 1771 sc = device_get_softc(dev); 1772 MY_LOCK(sc); 1773 my_stop(sc); 1774 MY_UNLOCK(sc); 1775 return 0; 1776} 1777