if_sf.c revision 148887
1/*- 2 * Copyright (c) 1997, 1998, 1999 3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by Bill Paul. 16 * 4. Neither the name of the author nor the names of any co-contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 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 30 * THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33#include <sys/cdefs.h> 34__FBSDID("$FreeBSD: head/sys/dev/sf/if_sf.c 148887 2005-08-09 10:20:02Z rwatson $"); 35 36/* 37 * Adaptec AIC-6915 "Starfire" PCI fast ethernet driver for FreeBSD. 38 * Programming manual is available from: 39 * http://download.adaptec.com/pdfs/user_guides/aic6915_pg.pdf. 40 * 41 * Written by Bill Paul <wpaul@ctr.columbia.edu> 42 * Department of Electical Engineering 43 * Columbia University, New York City 44 */ 45/* 46 * The Adaptec AIC-6915 "Starfire" is a 64-bit 10/100 PCI ethernet 47 * controller designed with flexibility and reducing CPU load in mind. 48 * The Starfire offers high and low priority buffer queues, a 49 * producer/consumer index mechanism and several different buffer 50 * queue and completion queue descriptor types. Any one of a number 51 * of different driver designs can be used, depending on system and 52 * OS requirements. This driver makes use of type0 transmit frame 53 * descriptors (since BSD fragments packets across an mbuf chain) 54 * and two RX buffer queues prioritized on size (one queue for small 55 * frames that will fit into a single mbuf, another with full size 56 * mbuf clusters for everything else). The producer/consumer indexes 57 * and completion queues are also used. 58 * 59 * One downside to the Starfire has to do with alignment: buffer 60 * queues must be aligned on 256-byte boundaries, and receive buffers 61 * must be aligned on longword boundaries. The receive buffer alignment 62 * causes problems on the Alpha platform, where the packet payload 63 * should be longword aligned. There is no simple way around this. 64 * 65 * For receive filtering, the Starfire offers 16 perfect filter slots 66 * and a 512-bit hash table. 67 * 68 * The Starfire has no internal transceiver, relying instead on an 69 * external MII-based transceiver. Accessing registers on external 70 * PHYs is done through a special register map rather than with the 71 * usual bitbang MDIO method. 72 * 73 * Acesssing the registers on the Starfire is a little tricky. The 74 * Starfire has a 512K internal register space. When programmed for 75 * PCI memory mapped mode, the entire register space can be accessed 76 * directly. However in I/O space mode, only 256 bytes are directly 77 * mapped into PCI I/O space. The other registers can be accessed 78 * indirectly using the SF_INDIRECTIO_ADDR and SF_INDIRECTIO_DATA 79 * registers inside the 256-byte I/O window. 80 */ 81 82#include <sys/param.h> 83#include <sys/systm.h> 84#include <sys/sockio.h> 85#include <sys/mbuf.h> 86#include <sys/malloc.h> 87#include <sys/kernel.h> 88#include <sys/module.h> 89#include <sys/socket.h> 90 91#include <net/if.h> 92#include <net/if_arp.h> 93#include <net/ethernet.h> 94#include <net/if_dl.h> 95#include <net/if_media.h> 96#include <net/if_types.h> 97 98#include <net/bpf.h> 99 100#include <vm/vm.h> /* for vtophys */ 101#include <vm/pmap.h> /* for vtophys */ 102#include <machine/bus.h> 103#include <machine/resource.h> 104#include <sys/bus.h> 105#include <sys/rman.h> 106 107#include <dev/mii/mii.h> 108#include <dev/mii/miivar.h> 109 110/* "controller miibus0" required. See GENERIC if you get errors here. */ 111#include "miibus_if.h" 112 113#include <dev/pci/pcireg.h> 114#include <dev/pci/pcivar.h> 115 116#define SF_USEIOSPACE 117 118#include <pci/if_sfreg.h> 119 120MODULE_DEPEND(sf, pci, 1, 1, 1); 121MODULE_DEPEND(sf, ether, 1, 1, 1); 122MODULE_DEPEND(sf, miibus, 1, 1, 1); 123 124static struct sf_type sf_devs[] = { 125 { AD_VENDORID, AD_DEVICEID_STARFIRE, 126 "Adaptec AIC-6915 10/100BaseTX" }, 127 { 0, 0, NULL } 128}; 129 130static int sf_probe(device_t); 131static int sf_attach(device_t); 132static int sf_detach(device_t); 133static void sf_intr(void *); 134static void sf_stats_update(void *); 135static void sf_rxeof(struct sf_softc *); 136static void sf_txeof(struct sf_softc *); 137static int sf_encap(struct sf_softc *, struct sf_tx_bufdesc_type0 *, 138 struct mbuf *); 139static void sf_start(struct ifnet *); 140static int sf_ioctl(struct ifnet *, u_long, caddr_t); 141static void sf_init(void *); 142static void sf_stop(struct sf_softc *); 143static void sf_watchdog(struct ifnet *); 144static void sf_shutdown(device_t); 145static int sf_ifmedia_upd(struct ifnet *); 146static void sf_ifmedia_sts(struct ifnet *, struct ifmediareq *); 147static void sf_reset(struct sf_softc *); 148static int sf_init_rx_ring(struct sf_softc *); 149static void sf_init_tx_ring(struct sf_softc *); 150static int sf_newbuf(struct sf_softc *, struct sf_rx_bufdesc_type0 *, 151 struct mbuf *); 152static void sf_setmulti(struct sf_softc *); 153static int sf_setperf(struct sf_softc *, int, caddr_t); 154static int sf_sethash(struct sf_softc *, caddr_t, int); 155#ifdef notdef 156static int sf_setvlan(struct sf_softc *, int, u_int32_t); 157#endif 158 159static u_int8_t sf_read_eeprom(struct sf_softc *, int); 160 161static int sf_miibus_readreg(device_t, int, int); 162static int sf_miibus_writereg(device_t, int, int, int); 163static void sf_miibus_statchg(device_t); 164#ifdef DEVICE_POLLING 165static void sf_poll(struct ifnet *ifp, enum poll_cmd cmd, 166 int count); 167static void sf_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, 168 int count); 169#endif /* DEVICE_POLLING */ 170 171static u_int32_t csr_read_4(struct sf_softc *, int); 172static void csr_write_4(struct sf_softc *, int, u_int32_t); 173static void sf_txthresh_adjust(struct sf_softc *); 174 175#ifdef SF_USEIOSPACE 176#define SF_RES SYS_RES_IOPORT 177#define SF_RID SF_PCI_LOIO 178#else 179#define SF_RES SYS_RES_MEMORY 180#define SF_RID SF_PCI_LOMEM 181#endif 182 183static device_method_t sf_methods[] = { 184 /* Device interface */ 185 DEVMETHOD(device_probe, sf_probe), 186 DEVMETHOD(device_attach, sf_attach), 187 DEVMETHOD(device_detach, sf_detach), 188 DEVMETHOD(device_shutdown, sf_shutdown), 189 190 /* bus interface */ 191 DEVMETHOD(bus_print_child, bus_generic_print_child), 192 DEVMETHOD(bus_driver_added, bus_generic_driver_added), 193 194 /* MII interface */ 195 DEVMETHOD(miibus_readreg, sf_miibus_readreg), 196 DEVMETHOD(miibus_writereg, sf_miibus_writereg), 197 DEVMETHOD(miibus_statchg, sf_miibus_statchg), 198 199 { 0, 0 } 200}; 201 202static driver_t sf_driver = { 203 "sf", 204 sf_methods, 205 sizeof(struct sf_softc), 206}; 207 208static devclass_t sf_devclass; 209 210DRIVER_MODULE(sf, pci, sf_driver, sf_devclass, 0, 0); 211DRIVER_MODULE(miibus, sf, miibus_driver, miibus_devclass, 0, 0); 212 213#define SF_SETBIT(sc, reg, x) \ 214 csr_write_4(sc, reg, csr_read_4(sc, reg) | (x)) 215 216#define SF_CLRBIT(sc, reg, x) \ 217 csr_write_4(sc, reg, csr_read_4(sc, reg) & ~(x)) 218 219static u_int32_t 220csr_read_4(sc, reg) 221 struct sf_softc *sc; 222 int reg; 223{ 224 u_int32_t val; 225 226#ifdef SF_USEIOSPACE 227 CSR_WRITE_4(sc, SF_INDIRECTIO_ADDR, reg + SF_RMAP_INTREG_BASE); 228 val = CSR_READ_4(sc, SF_INDIRECTIO_DATA); 229#else 230 val = CSR_READ_4(sc, (reg + SF_RMAP_INTREG_BASE)); 231#endif 232 233 return(val); 234} 235 236static u_int8_t 237sf_read_eeprom(sc, reg) 238 struct sf_softc *sc; 239 int reg; 240{ 241 u_int8_t val; 242 243 val = (csr_read_4(sc, SF_EEADDR_BASE + 244 (reg & 0xFFFFFFFC)) >> (8 * (reg & 3))) & 0xFF; 245 246 return(val); 247} 248 249static void 250csr_write_4(sc, reg, val) 251 struct sf_softc *sc; 252 int reg; 253 u_int32_t val; 254{ 255#ifdef SF_USEIOSPACE 256 CSR_WRITE_4(sc, SF_INDIRECTIO_ADDR, reg + SF_RMAP_INTREG_BASE); 257 CSR_WRITE_4(sc, SF_INDIRECTIO_DATA, val); 258#else 259 CSR_WRITE_4(sc, (reg + SF_RMAP_INTREG_BASE), val); 260#endif 261} 262 263/* 264 * Copy the address 'mac' into the perfect RX filter entry at 265 * offset 'idx.' The perfect filter only has 16 entries so do 266 * some sanity tests. 267 */ 268static int 269sf_setperf(sc, idx, mac) 270 struct sf_softc *sc; 271 int idx; 272 caddr_t mac; 273{ 274 u_int16_t *p; 275 276 if (idx < 0 || idx > SF_RXFILT_PERFECT_CNT) 277 return(EINVAL); 278 279 if (mac == NULL) 280 return(EINVAL); 281 282 p = (u_int16_t *)mac; 283 284 csr_write_4(sc, SF_RXFILT_PERFECT_BASE + 285 (idx * SF_RXFILT_PERFECT_SKIP), htons(p[2])); 286 csr_write_4(sc, SF_RXFILT_PERFECT_BASE + 287 (idx * SF_RXFILT_PERFECT_SKIP) + 4, htons(p[1])); 288 csr_write_4(sc, SF_RXFILT_PERFECT_BASE + 289 (idx * SF_RXFILT_PERFECT_SKIP) + 8, htons(p[0])); 290 291 return(0); 292} 293 294/* 295 * Set the bit in the 512-bit hash table that corresponds to the 296 * specified mac address 'mac.' If 'prio' is nonzero, update the 297 * priority hash table instead of the filter hash table. 298 */ 299static int 300sf_sethash(sc, mac, prio) 301 struct sf_softc *sc; 302 caddr_t mac; 303 int prio; 304{ 305 u_int32_t h; 306 307 if (mac == NULL) 308 return(EINVAL); 309 310 h = ether_crc32_be(mac, ETHER_ADDR_LEN) >> 23; 311 312 if (prio) { 313 SF_SETBIT(sc, SF_RXFILT_HASH_BASE + SF_RXFILT_HASH_PRIOOFF + 314 (SF_RXFILT_HASH_SKIP * (h >> 4)), (1 << (h & 0xF))); 315 } else { 316 SF_SETBIT(sc, SF_RXFILT_HASH_BASE + SF_RXFILT_HASH_ADDROFF + 317 (SF_RXFILT_HASH_SKIP * (h >> 4)), (1 << (h & 0xF))); 318 } 319 320 return(0); 321} 322 323#ifdef notdef 324/* 325 * Set a VLAN tag in the receive filter. 326 */ 327static int 328sf_setvlan(sc, idx, vlan) 329 struct sf_softc *sc; 330 int idx; 331 u_int32_t vlan; 332{ 333 if (idx < 0 || idx >> SF_RXFILT_HASH_CNT) 334 return(EINVAL); 335 336 csr_write_4(sc, SF_RXFILT_HASH_BASE + 337 (idx * SF_RXFILT_HASH_SKIP) + SF_RXFILT_HASH_VLANOFF, vlan); 338 339 return(0); 340} 341#endif 342 343static int 344sf_miibus_readreg(dev, phy, reg) 345 device_t dev; 346 int phy, reg; 347{ 348 struct sf_softc *sc; 349 int i; 350 u_int32_t val = 0; 351 352 sc = device_get_softc(dev); 353 354 for (i = 0; i < SF_TIMEOUT; i++) { 355 val = csr_read_4(sc, SF_PHY_REG(phy, reg)); 356 if (val & SF_MII_DATAVALID) 357 break; 358 } 359 360 if (i == SF_TIMEOUT) 361 return(0); 362 363 if ((val & 0x0000FFFF) == 0xFFFF) 364 return(0); 365 366 return(val & 0x0000FFFF); 367} 368 369static int 370sf_miibus_writereg(dev, phy, reg, val) 371 device_t dev; 372 int phy, reg, val; 373{ 374 struct sf_softc *sc; 375 int i; 376 int busy; 377 378 sc = device_get_softc(dev); 379 380 csr_write_4(sc, SF_PHY_REG(phy, reg), val); 381 382 for (i = 0; i < SF_TIMEOUT; i++) { 383 busy = csr_read_4(sc, SF_PHY_REG(phy, reg)); 384 if (!(busy & SF_MII_BUSY)) 385 break; 386 } 387 388 return(0); 389} 390 391static void 392sf_miibus_statchg(dev) 393 device_t dev; 394{ 395 struct sf_softc *sc; 396 struct mii_data *mii; 397 398 sc = device_get_softc(dev); 399 mii = device_get_softc(sc->sf_miibus); 400 401 if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) { 402 SF_SETBIT(sc, SF_MACCFG_1, SF_MACCFG1_FULLDUPLEX); 403 csr_write_4(sc, SF_BKTOBKIPG, SF_IPGT_FDX); 404 } else { 405 SF_CLRBIT(sc, SF_MACCFG_1, SF_MACCFG1_FULLDUPLEX); 406 csr_write_4(sc, SF_BKTOBKIPG, SF_IPGT_HDX); 407 } 408} 409 410static void 411sf_setmulti(sc) 412 struct sf_softc *sc; 413{ 414 struct ifnet *ifp; 415 int i; 416 struct ifmultiaddr *ifma; 417 u_int8_t dummy[] = { 0, 0, 0, 0, 0, 0 }; 418 419 ifp = sc->sf_ifp; 420 421 /* First zot all the existing filters. */ 422 for (i = 1; i < SF_RXFILT_PERFECT_CNT; i++) 423 sf_setperf(sc, i, (char *)&dummy); 424 for (i = SF_RXFILT_HASH_BASE; 425 i < (SF_RXFILT_HASH_MAX + 1); i += 4) 426 csr_write_4(sc, i, 0); 427 SF_CLRBIT(sc, SF_RXFILT, SF_RXFILT_ALLMULTI); 428 429 /* Now program new ones. */ 430 if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) { 431 SF_SETBIT(sc, SF_RXFILT, SF_RXFILT_ALLMULTI); 432 } else { 433 i = 1; 434 IF_ADDR_LOCK(ifp); 435 TAILQ_FOREACH_REVERSE(ifma, &ifp->if_multiaddrs, ifmultihead, ifma_link) { 436 if (ifma->ifma_addr->sa_family != AF_LINK) 437 continue; 438 /* 439 * Program the first 15 multicast groups 440 * into the perfect filter. For all others, 441 * use the hash table. 442 */ 443 if (i < SF_RXFILT_PERFECT_CNT) { 444 sf_setperf(sc, i, 445 LLADDR((struct sockaddr_dl *)ifma->ifma_addr)); 446 i++; 447 continue; 448 } 449 450 sf_sethash(sc, 451 LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 0); 452 } 453 IF_ADDR_UNLOCK(ifp); 454 } 455} 456 457/* 458 * Set media options. 459 */ 460static int 461sf_ifmedia_upd(ifp) 462 struct ifnet *ifp; 463{ 464 struct sf_softc *sc; 465 struct mii_data *mii; 466 467 sc = ifp->if_softc; 468 mii = device_get_softc(sc->sf_miibus); 469 sc->sf_link = 0; 470 if (mii->mii_instance) { 471 struct mii_softc *miisc; 472 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 473 mii_phy_reset(miisc); 474 } 475 mii_mediachg(mii); 476 477 return(0); 478} 479 480/* 481 * Report current media status. 482 */ 483static void 484sf_ifmedia_sts(ifp, ifmr) 485 struct ifnet *ifp; 486 struct ifmediareq *ifmr; 487{ 488 struct sf_softc *sc; 489 struct mii_data *mii; 490 491 sc = ifp->if_softc; 492 mii = device_get_softc(sc->sf_miibus); 493 494 mii_pollstat(mii); 495 ifmr->ifm_active = mii->mii_media_active; 496 ifmr->ifm_status = mii->mii_media_status; 497} 498 499static int 500sf_ioctl(ifp, command, data) 501 struct ifnet *ifp; 502 u_long command; 503 caddr_t data; 504{ 505 struct sf_softc *sc = ifp->if_softc; 506 struct ifreq *ifr = (struct ifreq *) data; 507 struct mii_data *mii; 508 int error = 0; 509 510 SF_LOCK(sc); 511 512 switch(command) { 513 case SIOCSIFFLAGS: 514 if (ifp->if_flags & IFF_UP) { 515 if (ifp->if_drv_flags & IFF_DRV_RUNNING && 516 ifp->if_flags & IFF_PROMISC && 517 !(sc->sf_if_flags & IFF_PROMISC)) { 518 SF_SETBIT(sc, SF_RXFILT, SF_RXFILT_PROMISC); 519 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING && 520 !(ifp->if_flags & IFF_PROMISC) && 521 sc->sf_if_flags & IFF_PROMISC) { 522 SF_CLRBIT(sc, SF_RXFILT, SF_RXFILT_PROMISC); 523 } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) 524 sf_init(sc); 525 } else { 526 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 527 sf_stop(sc); 528 } 529 sc->sf_if_flags = ifp->if_flags; 530 error = 0; 531 break; 532 case SIOCADDMULTI: 533 case SIOCDELMULTI: 534 sf_setmulti(sc); 535 error = 0; 536 break; 537 case SIOCGIFMEDIA: 538 case SIOCSIFMEDIA: 539 mii = device_get_softc(sc->sf_miibus); 540 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 541 break; 542 case SIOCSIFCAP: 543 ifp->if_capenable &= ~IFCAP_POLLING; 544 ifp->if_capenable |= ifr->ifr_reqcap & IFCAP_POLLING; 545 break; 546 default: 547 error = ether_ioctl(ifp, command, data); 548 break; 549 } 550 551 SF_UNLOCK(sc); 552 553 return(error); 554} 555 556static void 557sf_reset(sc) 558 struct sf_softc *sc; 559{ 560 register int i; 561 562 csr_write_4(sc, SF_GEN_ETH_CTL, 0); 563 SF_SETBIT(sc, SF_MACCFG_1, SF_MACCFG1_SOFTRESET); 564 DELAY(1000); 565 SF_CLRBIT(sc, SF_MACCFG_1, SF_MACCFG1_SOFTRESET); 566 567 SF_SETBIT(sc, SF_PCI_DEVCFG, SF_PCIDEVCFG_RESET); 568 569 for (i = 0; i < SF_TIMEOUT; i++) { 570 DELAY(10); 571 if (!(csr_read_4(sc, SF_PCI_DEVCFG) & SF_PCIDEVCFG_RESET)) 572 break; 573 } 574 575 if (i == SF_TIMEOUT) 576 printf("sf%d: reset never completed!\n", sc->sf_unit); 577 578 /* Wait a little while for the chip to get its brains in order. */ 579 DELAY(1000); 580} 581 582/* 583 * Probe for an Adaptec AIC-6915 chip. Check the PCI vendor and device 584 * IDs against our list and return a device name if we find a match. 585 * We also check the subsystem ID so that we can identify exactly which 586 * NIC has been found, if possible. 587 */ 588static int 589sf_probe(dev) 590 device_t dev; 591{ 592 struct sf_type *t; 593 594 t = sf_devs; 595 596 while(t->sf_name != NULL) { 597 if ((pci_get_vendor(dev) == t->sf_vid) && 598 (pci_get_device(dev) == t->sf_did)) { 599 switch((pci_read_config(dev, 600 SF_PCI_SUBVEN_ID, 4) >> 16) & 0xFFFF) { 601 case AD_SUBSYSID_62011_REV0: 602 case AD_SUBSYSID_62011_REV1: 603 device_set_desc(dev, 604 "Adaptec ANA-62011 10/100BaseTX"); 605 return (BUS_PROBE_DEFAULT); 606 case AD_SUBSYSID_62022: 607 device_set_desc(dev, 608 "Adaptec ANA-62022 10/100BaseTX"); 609 return (BUS_PROBE_DEFAULT); 610 case AD_SUBSYSID_62044_REV0: 611 case AD_SUBSYSID_62044_REV1: 612 device_set_desc(dev, 613 "Adaptec ANA-62044 10/100BaseTX"); 614 return (BUS_PROBE_DEFAULT); 615 case AD_SUBSYSID_62020: 616 device_set_desc(dev, 617 "Adaptec ANA-62020 10/100BaseFX"); 618 return (BUS_PROBE_DEFAULT); 619 case AD_SUBSYSID_69011: 620 device_set_desc(dev, 621 "Adaptec ANA-69011 10/100BaseTX"); 622 return (BUS_PROBE_DEFAULT); 623 default: 624 device_set_desc(dev, t->sf_name); 625 return (BUS_PROBE_DEFAULT); 626 break; 627 } 628 } 629 t++; 630 } 631 632 return(ENXIO); 633} 634 635/* 636 * Attach the interface. Allocate softc structures, do ifmedia 637 * setup and ethernet/BPF attach. 638 */ 639static int 640sf_attach(dev) 641 device_t dev; 642{ 643 int i; 644 struct sf_softc *sc; 645 struct ifnet *ifp; 646 int unit, rid, error = 0; 647 u_char eaddr[6]; 648 649 sc = device_get_softc(dev); 650 unit = device_get_unit(dev); 651 652 mtx_init(&sc->sf_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 653 MTX_DEF | MTX_RECURSE); 654 /* 655 * Map control/status registers. 656 */ 657 pci_enable_busmaster(dev); 658 659 rid = SF_RID; 660 sc->sf_res = bus_alloc_resource_any(dev, SF_RES, &rid, RF_ACTIVE); 661 662 if (sc->sf_res == NULL) { 663 printf ("sf%d: couldn't map ports\n", unit); 664 error = ENXIO; 665 goto fail; 666 } 667 668 sc->sf_btag = rman_get_bustag(sc->sf_res); 669 sc->sf_bhandle = rman_get_bushandle(sc->sf_res); 670 671 /* Allocate interrupt */ 672 rid = 0; 673 sc->sf_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 674 RF_SHAREABLE | RF_ACTIVE); 675 676 if (sc->sf_irq == NULL) { 677 printf("sf%d: couldn't map interrupt\n", unit); 678 error = ENXIO; 679 goto fail; 680 } 681 682 callout_handle_init(&sc->sf_stat_ch); 683 /* Reset the adapter. */ 684 sf_reset(sc); 685 686 /* 687 * Get station address from the EEPROM. 688 */ 689 for (i = 0; i < ETHER_ADDR_LEN; i++) 690 eaddr[i] = 691 sf_read_eeprom(sc, SF_EE_NODEADDR + ETHER_ADDR_LEN - i); 692 693 sc->sf_unit = unit; 694 695 /* Allocate the descriptor queues. */ 696 sc->sf_ldata = contigmalloc(sizeof(struct sf_list_data), M_DEVBUF, 697 M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0); 698 699 if (sc->sf_ldata == NULL) { 700 printf("sf%d: no memory for list buffers!\n", unit); 701 error = ENXIO; 702 goto fail; 703 } 704 705 bzero(sc->sf_ldata, sizeof(struct sf_list_data)); 706 707 ifp = sc->sf_ifp = if_alloc(IFT_ETHER); 708 if (ifp == NULL) { 709 printf("sf%d: can not if_alloc()\n", sc->sf_unit); 710 error = ENOSPC; 711 goto fail; 712 } 713 714 /* Do MII setup. */ 715 if (mii_phy_probe(dev, &sc->sf_miibus, 716 sf_ifmedia_upd, sf_ifmedia_sts)) { 717 printf("sf%d: MII without any phy!\n", sc->sf_unit); 718 error = ENXIO; 719 goto fail; 720 } 721 722 ifp->if_softc = sc; 723 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 724 ifp->if_mtu = ETHERMTU; 725 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST | 726 IFF_NEEDSGIANT; 727 ifp->if_ioctl = sf_ioctl; 728 ifp->if_start = sf_start; 729 ifp->if_watchdog = sf_watchdog; 730 ifp->if_init = sf_init; 731 ifp->if_baudrate = 10000000; 732 IFQ_SET_MAXLEN(&ifp->if_snd, SF_TX_DLIST_CNT - 1); 733 ifp->if_snd.ifq_drv_maxlen = SF_TX_DLIST_CNT - 1; 734 IFQ_SET_READY(&ifp->if_snd); 735#ifdef DEVICE_POLLING 736 ifp->if_capabilities |= IFCAP_POLLING; 737#endif /* DEVICE_POLLING */ 738 ifp->if_capenable = ifp->if_capabilities; 739 740 /* 741 * Call MI attach routine. 742 */ 743 ether_ifattach(ifp, eaddr); 744 745 /* Hook interrupt last to avoid having to lock softc */ 746 error = bus_setup_intr(dev, sc->sf_irq, INTR_TYPE_NET, 747 sf_intr, sc, &sc->sf_intrhand); 748 749 if (error) { 750 printf("sf%d: couldn't set up irq\n", unit); 751 ether_ifdetach(ifp); 752 if_free(ifp); 753 goto fail; 754 } 755 756fail: 757 if (error) 758 sf_detach(dev); 759 760 return(error); 761} 762 763/* 764 * Shutdown hardware and free up resources. This can be called any 765 * time after the mutex has been initialized. It is called in both 766 * the error case in attach and the normal detach case so it needs 767 * to be careful about only freeing resources that have actually been 768 * allocated. 769 */ 770static int 771sf_detach(dev) 772 device_t dev; 773{ 774 struct sf_softc *sc; 775 struct ifnet *ifp; 776 777 sc = device_get_softc(dev); 778 KASSERT(mtx_initialized(&sc->sf_mtx), ("sf mutex not initialized")); 779 SF_LOCK(sc); 780 ifp = sc->sf_ifp; 781 782 /* These should only be active if attach succeeded */ 783 if (device_is_attached(dev)) { 784 sf_stop(sc); 785 ether_ifdetach(ifp); 786 if_free(ifp); 787 } 788 if (sc->sf_miibus) 789 device_delete_child(dev, sc->sf_miibus); 790 bus_generic_detach(dev); 791 792 if (sc->sf_intrhand) 793 bus_teardown_intr(dev, sc->sf_irq, sc->sf_intrhand); 794 if (sc->sf_irq) 795 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sf_irq); 796 if (sc->sf_res) 797 bus_release_resource(dev, SF_RES, SF_RID, sc->sf_res); 798 799 if (sc->sf_ldata) 800 contigfree(sc->sf_ldata, sizeof(struct sf_list_data), M_DEVBUF); 801 802 SF_UNLOCK(sc); 803 mtx_destroy(&sc->sf_mtx); 804 805 return(0); 806} 807 808static int 809sf_init_rx_ring(sc) 810 struct sf_softc *sc; 811{ 812 struct sf_list_data *ld; 813 int i; 814 815 ld = sc->sf_ldata; 816 817 bzero((char *)ld->sf_rx_dlist_big, 818 sizeof(struct sf_rx_bufdesc_type0) * SF_RX_DLIST_CNT); 819 bzero((char *)ld->sf_rx_clist, 820 sizeof(struct sf_rx_cmpdesc_type3) * SF_RX_CLIST_CNT); 821 822 for (i = 0; i < SF_RX_DLIST_CNT; i++) { 823 if (sf_newbuf(sc, &ld->sf_rx_dlist_big[i], NULL) == ENOBUFS) 824 return(ENOBUFS); 825 } 826 827 return(0); 828} 829 830static void 831sf_init_tx_ring(sc) 832 struct sf_softc *sc; 833{ 834 struct sf_list_data *ld; 835 int i; 836 837 ld = sc->sf_ldata; 838 839 bzero((char *)ld->sf_tx_dlist, 840 sizeof(struct sf_tx_bufdesc_type0) * SF_TX_DLIST_CNT); 841 bzero((char *)ld->sf_tx_clist, 842 sizeof(struct sf_tx_cmpdesc_type0) * SF_TX_CLIST_CNT); 843 844 for (i = 0; i < SF_TX_DLIST_CNT; i++) 845 ld->sf_tx_dlist[i].sf_id = SF_TX_BUFDESC_ID; 846 for (i = 0; i < SF_TX_CLIST_CNT; i++) 847 ld->sf_tx_clist[i].sf_type = SF_TXCMPTYPE_TX; 848 849 ld->sf_tx_dlist[SF_TX_DLIST_CNT - 1].sf_end = 1; 850 sc->sf_tx_cnt = 0; 851} 852 853static int 854sf_newbuf(sc, c, m) 855 struct sf_softc *sc; 856 struct sf_rx_bufdesc_type0 *c; 857 struct mbuf *m; 858{ 859 struct mbuf *m_new = NULL; 860 861 if (m == NULL) { 862 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 863 if (m_new == NULL) 864 return(ENOBUFS); 865 866 MCLGET(m_new, M_DONTWAIT); 867 if (!(m_new->m_flags & M_EXT)) { 868 m_freem(m_new); 869 return(ENOBUFS); 870 } 871 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 872 } else { 873 m_new = m; 874 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 875 m_new->m_data = m_new->m_ext.ext_buf; 876 } 877 878 m_adj(m_new, sizeof(u_int64_t)); 879 880 c->sf_mbuf = m_new; 881 c->sf_addrlo = SF_RX_HOSTADDR(vtophys(mtod(m_new, caddr_t))); 882 c->sf_valid = 1; 883 884 return(0); 885} 886 887/* 888 * The starfire is programmed to use 'normal' mode for packet reception, 889 * which means we use the consumer/producer model for both the buffer 890 * descriptor queue and the completion descriptor queue. The only problem 891 * with this is that it involves a lot of register accesses: we have to 892 * read the RX completion consumer and producer indexes and the RX buffer 893 * producer index, plus the RX completion consumer and RX buffer producer 894 * indexes have to be updated. It would have been easier if Adaptec had 895 * put each index in a separate register, especially given that the damn 896 * NIC has a 512K register space. 897 * 898 * In spite of all the lovely features that Adaptec crammed into the 6915, 899 * it is marred by one truly stupid design flaw, which is that receive 900 * buffer addresses must be aligned on a longword boundary. This forces 901 * the packet payload to be unaligned, which is suboptimal on the x86 and 902 * completely unuseable on the Alpha. Our only recourse is to copy received 903 * packets into properly aligned buffers before handing them off. 904 */ 905 906static void 907sf_rxeof(sc) 908 struct sf_softc *sc; 909{ 910 struct mbuf *m; 911 struct ifnet *ifp; 912 struct sf_rx_bufdesc_type0 *desc; 913 struct sf_rx_cmpdesc_type3 *cur_rx; 914 u_int32_t rxcons, rxprod; 915 int cmpprodidx, cmpconsidx, bufprodidx; 916 917 SF_LOCK_ASSERT(sc); 918 919 ifp = sc->sf_ifp; 920 921 rxcons = csr_read_4(sc, SF_CQ_CONSIDX); 922 rxprod = csr_read_4(sc, SF_RXDQ_PTR_Q1); 923 cmpprodidx = SF_IDX_LO(csr_read_4(sc, SF_CQ_PRODIDX)); 924 cmpconsidx = SF_IDX_LO(rxcons); 925 bufprodidx = SF_IDX_LO(rxprod); 926 927 while (cmpconsidx != cmpprodidx) { 928 struct mbuf *m0; 929 930#ifdef DEVICE_POLLING 931 if (ifp->if_flags & IFF_POLLING) { 932 if (sc->rxcycles <= 0) 933 break; 934 sc->rxcycles--; 935 } 936#endif /* DEVICE_POLLING */ 937 938 cur_rx = &sc->sf_ldata->sf_rx_clist[cmpconsidx]; 939 desc = &sc->sf_ldata->sf_rx_dlist_big[cur_rx->sf_endidx]; 940 m = desc->sf_mbuf; 941 SF_INC(cmpconsidx, SF_RX_CLIST_CNT); 942 SF_INC(bufprodidx, SF_RX_DLIST_CNT); 943 944 if (!(cur_rx->sf_status1 & SF_RXSTAT1_OK)) { 945 ifp->if_ierrors++; 946 sf_newbuf(sc, desc, m); 947 continue; 948 } 949 950 m0 = m_devget(mtod(m, char *), cur_rx->sf_len, ETHER_ALIGN, 951 ifp, NULL); 952 sf_newbuf(sc, desc, m); 953 if (m0 == NULL) { 954 ifp->if_ierrors++; 955 continue; 956 } 957 m = m0; 958 959 ifp->if_ipackets++; 960 SF_UNLOCK(sc); 961 (*ifp->if_input)(ifp, m); 962 SF_LOCK(sc); 963 } 964 965 csr_write_4(sc, SF_CQ_CONSIDX, 966 (rxcons & ~SF_CQ_CONSIDX_RXQ1) | cmpconsidx); 967 csr_write_4(sc, SF_RXDQ_PTR_Q1, 968 (rxprod & ~SF_RXDQ_PRODIDX) | bufprodidx); 969} 970 971/* 972 * Read the transmit status from the completion queue and release 973 * mbufs. Note that the buffer descriptor index in the completion 974 * descriptor is an offset from the start of the transmit buffer 975 * descriptor list in bytes. This is important because the manual 976 * gives the impression that it should match the producer/consumer 977 * index, which is the offset in 8 byte blocks. 978 */ 979static void 980sf_txeof(sc) 981 struct sf_softc *sc; 982{ 983 int txcons, cmpprodidx, cmpconsidx; 984 struct sf_tx_cmpdesc_type1 *cur_cmp; 985 struct sf_tx_bufdesc_type0 *cur_tx; 986 struct ifnet *ifp; 987 988 ifp = sc->sf_ifp; 989 990 txcons = csr_read_4(sc, SF_CQ_CONSIDX); 991 cmpprodidx = SF_IDX_HI(csr_read_4(sc, SF_CQ_PRODIDX)); 992 cmpconsidx = SF_IDX_HI(txcons); 993 994 while (cmpconsidx != cmpprodidx) { 995 cur_cmp = &sc->sf_ldata->sf_tx_clist[cmpconsidx]; 996 cur_tx = &sc->sf_ldata->sf_tx_dlist[cur_cmp->sf_index >> 7]; 997 998 if (cur_cmp->sf_txstat & SF_TXSTAT_TX_OK) 999 ifp->if_opackets++; 1000 else { 1001 if (cur_cmp->sf_txstat & SF_TXSTAT_TX_UNDERRUN) 1002 sf_txthresh_adjust(sc); 1003 ifp->if_oerrors++; 1004 } 1005 1006 sc->sf_tx_cnt--; 1007 if (cur_tx->sf_mbuf != NULL) { 1008 m_freem(cur_tx->sf_mbuf); 1009 cur_tx->sf_mbuf = NULL; 1010 } else 1011 break; 1012 SF_INC(cmpconsidx, SF_TX_CLIST_CNT); 1013 } 1014 1015 ifp->if_timer = 0; 1016 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1017 1018 csr_write_4(sc, SF_CQ_CONSIDX, 1019 (txcons & ~SF_CQ_CONSIDX_TXQ) | 1020 ((cmpconsidx << 16) & 0xFFFF0000)); 1021} 1022 1023static void 1024sf_txthresh_adjust(sc) 1025 struct sf_softc *sc; 1026{ 1027 u_int32_t txfctl; 1028 u_int8_t txthresh; 1029 1030 txfctl = csr_read_4(sc, SF_TX_FRAMCTL); 1031 txthresh = txfctl & SF_TXFRMCTL_TXTHRESH; 1032 if (txthresh < 0xFF) { 1033 txthresh++; 1034 txfctl &= ~SF_TXFRMCTL_TXTHRESH; 1035 txfctl |= txthresh; 1036#ifdef DIAGNOSTIC 1037 printf("sf%d: tx underrun, increasing " 1038 "tx threshold to %d bytes\n", 1039 sc->sf_unit, txthresh * 4); 1040#endif 1041 csr_write_4(sc, SF_TX_FRAMCTL, txfctl); 1042 } 1043} 1044 1045#ifdef DEVICE_POLLING 1046static void 1047sf_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) 1048{ 1049 struct sf_softc *sc = ifp->if_softc; 1050 1051 SF_LOCK(sc); 1052 sf_poll_locked(ifp, cmd, count); 1053 SF_UNLOCK(sc); 1054} 1055 1056static void 1057sf_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count) 1058{ 1059 struct sf_softc *sc = ifp->if_softc; 1060 1061 SF_LOCK_ASSERT(sc); 1062 1063 if (!(ifp->if_capenable & IFCAP_POLLING)) { 1064 ether_poll_deregister(ifp); 1065 cmd = POLL_DEREGISTER; 1066 } 1067 1068 if (cmd == POLL_DEREGISTER) { 1069 /* Final call, enable interrupts. */ 1070 csr_write_4(sc, SF_IMR, SF_INTRS); 1071 return; 1072 } 1073 1074 sc->rxcycles = count; 1075 sf_rxeof(sc); 1076 sf_txeof(sc); 1077 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 1078 sf_start(ifp); 1079 1080 if (cmd == POLL_AND_CHECK_STATUS) { 1081 u_int32_t status; 1082 1083 status = csr_read_4(sc, SF_ISR); 1084 if (status) 1085 csr_write_4(sc, SF_ISR, status); 1086 1087 if (status & SF_ISR_TX_LOFIFO) 1088 sf_txthresh_adjust(sc); 1089 1090 if (status & SF_ISR_ABNORMALINTR) { 1091 if (status & SF_ISR_STATSOFLOW) { 1092 untimeout(sf_stats_update, sc, 1093 sc->sf_stat_ch); 1094 sf_stats_update(sc); 1095 } else 1096 sf_init(sc); 1097 } 1098 } 1099} 1100#endif /* DEVICE_POLLING */ 1101 1102static void 1103sf_intr(arg) 1104 void *arg; 1105{ 1106 struct sf_softc *sc; 1107 struct ifnet *ifp; 1108 u_int32_t status; 1109 1110 sc = arg; 1111 SF_LOCK(sc); 1112 1113 ifp = sc->sf_ifp; 1114 1115#ifdef DEVICE_POLLING 1116 if (ifp->if_flags & IFF_POLLING) 1117 goto done_locked; 1118 1119 if ((ifp->if_capenable & IFCAP_POLLING) && 1120 ether_poll_register(sf_poll, ifp)) { 1121 /* OK, disable interrupts. */ 1122 csr_write_4(sc, SF_IMR, 0x00000000); 1123 sf_poll_locked(ifp, 0, 1); 1124 goto done_locked; 1125 } 1126#endif /* DEVICE_POLLING */ 1127 1128 if (!(csr_read_4(sc, SF_ISR_SHADOW) & SF_ISR_PCIINT_ASSERTED)) { 1129 SF_UNLOCK(sc); 1130 return; 1131 } 1132 1133 /* Disable interrupts. */ 1134 csr_write_4(sc, SF_IMR, 0x00000000); 1135 1136 for (;;) { 1137 status = csr_read_4(sc, SF_ISR); 1138 if (status) 1139 csr_write_4(sc, SF_ISR, status); 1140 1141 if (!(status & SF_INTRS)) 1142 break; 1143 1144 if (status & SF_ISR_RXDQ1_DMADONE) 1145 sf_rxeof(sc); 1146 1147 if (status & SF_ISR_TX_TXDONE || 1148 status & SF_ISR_TX_DMADONE || 1149 status & SF_ISR_TX_QUEUEDONE) 1150 sf_txeof(sc); 1151 1152 if (status & SF_ISR_TX_LOFIFO) 1153 sf_txthresh_adjust(sc); 1154 1155 if (status & SF_ISR_ABNORMALINTR) { 1156 if (status & SF_ISR_STATSOFLOW) { 1157 untimeout(sf_stats_update, sc, 1158 sc->sf_stat_ch); 1159 sf_stats_update(sc); 1160 } else 1161 sf_init(sc); 1162 } 1163 } 1164 1165 /* Re-enable interrupts. */ 1166 csr_write_4(sc, SF_IMR, SF_INTRS); 1167 1168 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 1169 sf_start(ifp); 1170 1171#ifdef DEVICE_POLLING 1172done_locked: 1173#endif /* DEVICE_POLLING */ 1174 SF_UNLOCK(sc); 1175} 1176 1177static void 1178sf_init(xsc) 1179 void *xsc; 1180{ 1181 struct sf_softc *sc; 1182 struct ifnet *ifp; 1183 struct mii_data *mii; 1184 int i; 1185 1186 sc = xsc; 1187 SF_LOCK(sc); 1188 ifp = sc->sf_ifp; 1189 mii = device_get_softc(sc->sf_miibus); 1190 1191 sf_stop(sc); 1192 sf_reset(sc); 1193 1194 /* Init all the receive filter registers */ 1195 for (i = SF_RXFILT_PERFECT_BASE; 1196 i < (SF_RXFILT_HASH_MAX + 1); i += 4) 1197 csr_write_4(sc, i, 0); 1198 1199 /* Empty stats counter registers. */ 1200 for (i = 0; i < sizeof(struct sf_stats)/sizeof(u_int32_t); i++) 1201 csr_write_4(sc, SF_STATS_BASE + 1202 (i + sizeof(u_int32_t)), 0); 1203 1204 /* Init our MAC address */ 1205 csr_write_4(sc, SF_PAR0, *(u_int32_t *)(&IFP2ENADDR(sc->sf_ifp)[0])); 1206 csr_write_4(sc, SF_PAR1, *(u_int32_t *)(&IFP2ENADDR(sc->sf_ifp)[4])); 1207 sf_setperf(sc, 0, (caddr_t)&IFP2ENADDR(sc->sf_ifp)); 1208 1209 if (sf_init_rx_ring(sc) == ENOBUFS) { 1210 printf("sf%d: initialization failed: no " 1211 "memory for rx buffers\n", sc->sf_unit); 1212 SF_UNLOCK(sc); 1213 return; 1214 } 1215 1216 sf_init_tx_ring(sc); 1217 1218 csr_write_4(sc, SF_RXFILT, SF_PERFMODE_NORMAL|SF_HASHMODE_WITHVLAN); 1219 1220 /* If we want promiscuous mode, set the allframes bit. */ 1221 if (ifp->if_flags & IFF_PROMISC) { 1222 SF_SETBIT(sc, SF_RXFILT, SF_RXFILT_PROMISC); 1223 } else { 1224 SF_CLRBIT(sc, SF_RXFILT, SF_RXFILT_PROMISC); 1225 } 1226 1227 if (ifp->if_flags & IFF_BROADCAST) { 1228 SF_SETBIT(sc, SF_RXFILT, SF_RXFILT_BROAD); 1229 } else { 1230 SF_CLRBIT(sc, SF_RXFILT, SF_RXFILT_BROAD); 1231 } 1232 1233 /* 1234 * Load the multicast filter. 1235 */ 1236 sf_setmulti(sc); 1237 1238 /* Init the completion queue indexes */ 1239 csr_write_4(sc, SF_CQ_CONSIDX, 0); 1240 csr_write_4(sc, SF_CQ_PRODIDX, 0); 1241 1242 /* Init the RX completion queue */ 1243 csr_write_4(sc, SF_RXCQ_CTL_1, 1244 vtophys(sc->sf_ldata->sf_rx_clist) & SF_RXCQ_ADDR); 1245 SF_SETBIT(sc, SF_RXCQ_CTL_1, SF_RXCQTYPE_3); 1246 1247 /* Init RX DMA control. */ 1248 SF_SETBIT(sc, SF_RXDMA_CTL, SF_RXDMA_REPORTBADPKTS); 1249 1250 /* Init the RX buffer descriptor queue. */ 1251 csr_write_4(sc, SF_RXDQ_ADDR_Q1, 1252 vtophys(sc->sf_ldata->sf_rx_dlist_big)); 1253 csr_write_4(sc, SF_RXDQ_CTL_1, (MCLBYTES << 16) | SF_DESCSPACE_16BYTES); 1254 csr_write_4(sc, SF_RXDQ_PTR_Q1, SF_RX_DLIST_CNT - 1); 1255 1256 /* Init the TX completion queue */ 1257 csr_write_4(sc, SF_TXCQ_CTL, 1258 vtophys(sc->sf_ldata->sf_tx_clist) & SF_RXCQ_ADDR); 1259 1260 /* Init the TX buffer descriptor queue. */ 1261 csr_write_4(sc, SF_TXDQ_ADDR_HIPRIO, 1262 vtophys(sc->sf_ldata->sf_tx_dlist)); 1263 SF_SETBIT(sc, SF_TX_FRAMCTL, SF_TXFRMCTL_CPLAFTERTX); 1264 csr_write_4(sc, SF_TXDQ_CTL, 1265 SF_TXBUFDESC_TYPE0|SF_TXMINSPACE_128BYTES|SF_TXSKIPLEN_8BYTES); 1266 SF_SETBIT(sc, SF_TXDQ_CTL, SF_TXDQCTL_NODMACMP); 1267 1268 /* Enable autopadding of short TX frames. */ 1269 SF_SETBIT(sc, SF_MACCFG_1, SF_MACCFG1_AUTOPAD); 1270 1271#ifdef DEVICE_POLLING 1272 /* Disable interrupts if we are polling. */ 1273 if (ifp->if_flags & IFF_POLLING) 1274 csr_write_4(sc, SF_IMR, 0x00000000); 1275 else 1276#endif /* DEVICE_POLLING */ 1277 1278 /* Enable interrupts. */ 1279 csr_write_4(sc, SF_IMR, SF_INTRS); 1280 SF_SETBIT(sc, SF_PCI_DEVCFG, SF_PCIDEVCFG_INTR_ENB); 1281 1282 /* Enable the RX and TX engines. */ 1283 SF_SETBIT(sc, SF_GEN_ETH_CTL, SF_ETHCTL_RX_ENB|SF_ETHCTL_RXDMA_ENB); 1284 SF_SETBIT(sc, SF_GEN_ETH_CTL, SF_ETHCTL_TX_ENB|SF_ETHCTL_TXDMA_ENB); 1285 1286 /*mii_mediachg(mii);*/ 1287 sf_ifmedia_upd(ifp); 1288 1289 ifp->if_drv_flags |= IFF_DRV_RUNNING; 1290 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1291 1292 sc->sf_stat_ch = timeout(sf_stats_update, sc, hz); 1293 1294 SF_UNLOCK(sc); 1295} 1296 1297static int 1298sf_encap(sc, c, m_head) 1299 struct sf_softc *sc; 1300 struct sf_tx_bufdesc_type0 *c; 1301 struct mbuf *m_head; 1302{ 1303 int frag = 0; 1304 struct sf_frag *f = NULL; 1305 struct mbuf *m; 1306 1307 m = m_head; 1308 1309 for (m = m_head, frag = 0; m != NULL; m = m->m_next) { 1310 if (m->m_len != 0) { 1311 if (frag == SF_MAXFRAGS) 1312 break; 1313 f = &c->sf_frags[frag]; 1314 if (frag == 0) 1315 f->sf_pktlen = m_head->m_pkthdr.len; 1316 f->sf_fraglen = m->m_len; 1317 f->sf_addr = vtophys(mtod(m, vm_offset_t)); 1318 frag++; 1319 } 1320 } 1321 1322 if (m != NULL) { 1323 struct mbuf *m_new = NULL; 1324 1325 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 1326 if (m_new == NULL) { 1327 printf("sf%d: no memory for tx list\n", sc->sf_unit); 1328 return(1); 1329 } 1330 1331 if (m_head->m_pkthdr.len > MHLEN) { 1332 MCLGET(m_new, M_DONTWAIT); 1333 if (!(m_new->m_flags & M_EXT)) { 1334 m_freem(m_new); 1335 printf("sf%d: no memory for tx list\n", 1336 sc->sf_unit); 1337 return(1); 1338 } 1339 } 1340 m_copydata(m_head, 0, m_head->m_pkthdr.len, 1341 mtod(m_new, caddr_t)); 1342 m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len; 1343 m_freem(m_head); 1344 m_head = m_new; 1345 f = &c->sf_frags[0]; 1346 f->sf_fraglen = f->sf_pktlen = m_head->m_pkthdr.len; 1347 f->sf_addr = vtophys(mtod(m_head, caddr_t)); 1348 frag = 1; 1349 } 1350 1351 c->sf_mbuf = m_head; 1352 c->sf_id = SF_TX_BUFDESC_ID; 1353 c->sf_fragcnt = frag; 1354 c->sf_intr = 1; 1355 c->sf_caltcp = 0; 1356 c->sf_crcen = 1; 1357 1358 return(0); 1359} 1360 1361static void 1362sf_start(ifp) 1363 struct ifnet *ifp; 1364{ 1365 struct sf_softc *sc; 1366 struct sf_tx_bufdesc_type0 *cur_tx = NULL; 1367 struct mbuf *m_head = NULL; 1368 int i, txprod; 1369 1370 sc = ifp->if_softc; 1371 SF_LOCK(sc); 1372 1373 if (!sc->sf_link && ifp->if_snd.ifq_len < 10) { 1374 SF_UNLOCK(sc); 1375 return; 1376 } 1377 1378 if (ifp->if_drv_flags & IFF_DRV_OACTIVE) { 1379 SF_UNLOCK(sc); 1380 return; 1381 } 1382 1383 txprod = csr_read_4(sc, SF_TXDQ_PRODIDX); 1384 i = SF_IDX_HI(txprod) >> 4; 1385 1386 if (sc->sf_ldata->sf_tx_dlist[i].sf_mbuf != NULL) { 1387 printf("sf%d: TX ring full, resetting\n", sc->sf_unit); 1388 sf_init(sc); 1389 txprod = csr_read_4(sc, SF_TXDQ_PRODIDX); 1390 i = SF_IDX_HI(txprod) >> 4; 1391 } 1392 1393 while(sc->sf_ldata->sf_tx_dlist[i].sf_mbuf == NULL) { 1394 if (sc->sf_tx_cnt >= (SF_TX_DLIST_CNT - 5)) { 1395 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1396 cur_tx = NULL; 1397 break; 1398 } 1399 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head); 1400 if (m_head == NULL) 1401 break; 1402 1403 cur_tx = &sc->sf_ldata->sf_tx_dlist[i]; 1404 if (sf_encap(sc, cur_tx, m_head)) { 1405 IFQ_DRV_PREPEND(&ifp->if_snd, m_head); 1406 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1407 cur_tx = NULL; 1408 break; 1409 } 1410 1411 /* 1412 * If there's a BPF listener, bounce a copy of this frame 1413 * to him. 1414 */ 1415 BPF_MTAP(ifp, m_head); 1416 1417 SF_INC(i, SF_TX_DLIST_CNT); 1418 sc->sf_tx_cnt++; 1419 /* 1420 * Don't get the TX DMA queue get too full. 1421 */ 1422 if (sc->sf_tx_cnt > 64) 1423 break; 1424 } 1425 1426 if (cur_tx == NULL) { 1427 SF_UNLOCK(sc); 1428 return; 1429 } 1430 1431 /* Transmit */ 1432 csr_write_4(sc, SF_TXDQ_PRODIDX, 1433 (txprod & ~SF_TXDQ_PRODIDX_HIPRIO) | 1434 ((i << 20) & 0xFFFF0000)); 1435 1436 ifp->if_timer = 5; 1437 1438 SF_UNLOCK(sc); 1439} 1440 1441static void 1442sf_stop(sc) 1443 struct sf_softc *sc; 1444{ 1445 int i; 1446 struct ifnet *ifp; 1447 1448 SF_LOCK(sc); 1449 1450 ifp = sc->sf_ifp; 1451 1452 untimeout(sf_stats_update, sc, sc->sf_stat_ch); 1453 1454#ifdef DEVICE_POLLING 1455 ether_poll_deregister(ifp); 1456#endif /* DEVICE_POLLING */ 1457 1458 csr_write_4(sc, SF_GEN_ETH_CTL, 0); 1459 csr_write_4(sc, SF_CQ_CONSIDX, 0); 1460 csr_write_4(sc, SF_CQ_PRODIDX, 0); 1461 csr_write_4(sc, SF_RXDQ_ADDR_Q1, 0); 1462 csr_write_4(sc, SF_RXDQ_CTL_1, 0); 1463 csr_write_4(sc, SF_RXDQ_PTR_Q1, 0); 1464 csr_write_4(sc, SF_TXCQ_CTL, 0); 1465 csr_write_4(sc, SF_TXDQ_ADDR_HIPRIO, 0); 1466 csr_write_4(sc, SF_TXDQ_CTL, 0); 1467 sf_reset(sc); 1468 1469 sc->sf_link = 0; 1470 1471 for (i = 0; i < SF_RX_DLIST_CNT; i++) { 1472 if (sc->sf_ldata->sf_rx_dlist_big[i].sf_mbuf != NULL) { 1473 m_freem(sc->sf_ldata->sf_rx_dlist_big[i].sf_mbuf); 1474 sc->sf_ldata->sf_rx_dlist_big[i].sf_mbuf = NULL; 1475 } 1476 } 1477 1478 for (i = 0; i < SF_TX_DLIST_CNT; i++) { 1479 if (sc->sf_ldata->sf_tx_dlist[i].sf_mbuf != NULL) { 1480 m_freem(sc->sf_ldata->sf_tx_dlist[i].sf_mbuf); 1481 sc->sf_ldata->sf_tx_dlist[i].sf_mbuf = NULL; 1482 } 1483 } 1484 1485 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING|IFF_DRV_OACTIVE); 1486 SF_UNLOCK(sc); 1487} 1488 1489/* 1490 * Note: it is important that this function not be interrupted. We 1491 * use a two-stage register access scheme: if we are interrupted in 1492 * between setting the indirect address register and reading from the 1493 * indirect data register, the contents of the address register could 1494 * be changed out from under us. 1495 */ 1496static void 1497sf_stats_update(xsc) 1498 void *xsc; 1499{ 1500 struct sf_softc *sc; 1501 struct ifnet *ifp; 1502 struct mii_data *mii; 1503 struct sf_stats stats; 1504 u_int32_t *ptr; 1505 int i; 1506 1507 sc = xsc; 1508 SF_LOCK(sc); 1509 ifp = sc->sf_ifp; 1510 mii = device_get_softc(sc->sf_miibus); 1511 1512 ptr = (u_int32_t *)&stats; 1513 for (i = 0; i < sizeof(stats)/sizeof(u_int32_t); i++) 1514 ptr[i] = csr_read_4(sc, SF_STATS_BASE + 1515 (i + sizeof(u_int32_t))); 1516 1517 for (i = 0; i < sizeof(stats)/sizeof(u_int32_t); i++) 1518 csr_write_4(sc, SF_STATS_BASE + 1519 (i + sizeof(u_int32_t)), 0); 1520 1521 ifp->if_collisions += stats.sf_tx_single_colls + 1522 stats.sf_tx_multi_colls + stats.sf_tx_excess_colls; 1523 1524 mii_tick(mii); 1525 1526 if (!sc->sf_link && mii->mii_media_status & IFM_ACTIVE && 1527 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 1528 sc->sf_link++; 1529 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 1530 sf_start(ifp); 1531 } 1532 1533 sc->sf_stat_ch = timeout(sf_stats_update, sc, hz); 1534 1535 SF_UNLOCK(sc); 1536} 1537 1538static void 1539sf_watchdog(ifp) 1540 struct ifnet *ifp; 1541{ 1542 struct sf_softc *sc; 1543 1544 sc = ifp->if_softc; 1545 1546 SF_LOCK(sc); 1547 1548 ifp->if_oerrors++; 1549 printf("sf%d: watchdog timeout\n", sc->sf_unit); 1550 1551 sf_stop(sc); 1552 sf_reset(sc); 1553 sf_init(sc); 1554 1555 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 1556 sf_start(ifp); 1557 1558 SF_UNLOCK(sc); 1559} 1560 1561static void 1562sf_shutdown(dev) 1563 device_t dev; 1564{ 1565 struct sf_softc *sc; 1566 1567 sc = device_get_softc(dev); 1568 1569 sf_stop(sc); 1570} 1571