if_sf.c revision 147256
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 147256 2005-06-10 16:49:24Z brooks $"); 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 TAILQ_FOREACH_REVERSE(ifma, &ifp->if_multiaddrs, ifmultihead, ifma_link) { 435 if (ifma->ifma_addr->sa_family != AF_LINK) 436 continue; 437 /* 438 * Program the first 15 multicast groups 439 * into the perfect filter. For all others, 440 * use the hash table. 441 */ 442 if (i < SF_RXFILT_PERFECT_CNT) { 443 sf_setperf(sc, i, 444 LLADDR((struct sockaddr_dl *)ifma->ifma_addr)); 445 i++; 446 continue; 447 } 448 449 sf_sethash(sc, 450 LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 0); 451 } 452 } 453} 454 455/* 456 * Set media options. 457 */ 458static int 459sf_ifmedia_upd(ifp) 460 struct ifnet *ifp; 461{ 462 struct sf_softc *sc; 463 struct mii_data *mii; 464 465 sc = ifp->if_softc; 466 mii = device_get_softc(sc->sf_miibus); 467 sc->sf_link = 0; 468 if (mii->mii_instance) { 469 struct mii_softc *miisc; 470 LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 471 mii_phy_reset(miisc); 472 } 473 mii_mediachg(mii); 474 475 return(0); 476} 477 478/* 479 * Report current media status. 480 */ 481static void 482sf_ifmedia_sts(ifp, ifmr) 483 struct ifnet *ifp; 484 struct ifmediareq *ifmr; 485{ 486 struct sf_softc *sc; 487 struct mii_data *mii; 488 489 sc = ifp->if_softc; 490 mii = device_get_softc(sc->sf_miibus); 491 492 mii_pollstat(mii); 493 ifmr->ifm_active = mii->mii_media_active; 494 ifmr->ifm_status = mii->mii_media_status; 495} 496 497static int 498sf_ioctl(ifp, command, data) 499 struct ifnet *ifp; 500 u_long command; 501 caddr_t data; 502{ 503 struct sf_softc *sc = ifp->if_softc; 504 struct ifreq *ifr = (struct ifreq *) data; 505 struct mii_data *mii; 506 int error = 0; 507 508 SF_LOCK(sc); 509 510 switch(command) { 511 case SIOCSIFFLAGS: 512 if (ifp->if_flags & IFF_UP) { 513 if (ifp->if_flags & IFF_RUNNING && 514 ifp->if_flags & IFF_PROMISC && 515 !(sc->sf_if_flags & IFF_PROMISC)) { 516 SF_SETBIT(sc, SF_RXFILT, SF_RXFILT_PROMISC); 517 } else if (ifp->if_flags & IFF_RUNNING && 518 !(ifp->if_flags & IFF_PROMISC) && 519 sc->sf_if_flags & IFF_PROMISC) { 520 SF_CLRBIT(sc, SF_RXFILT, SF_RXFILT_PROMISC); 521 } else if (!(ifp->if_flags & IFF_RUNNING)) 522 sf_init(sc); 523 } else { 524 if (ifp->if_flags & IFF_RUNNING) 525 sf_stop(sc); 526 } 527 sc->sf_if_flags = ifp->if_flags; 528 error = 0; 529 break; 530 case SIOCADDMULTI: 531 case SIOCDELMULTI: 532 sf_setmulti(sc); 533 error = 0; 534 break; 535 case SIOCGIFMEDIA: 536 case SIOCSIFMEDIA: 537 mii = device_get_softc(sc->sf_miibus); 538 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 539 break; 540 case SIOCSIFCAP: 541 ifp->if_capenable &= ~IFCAP_POLLING; 542 ifp->if_capenable |= ifr->ifr_reqcap & IFCAP_POLLING; 543 break; 544 default: 545 error = ether_ioctl(ifp, command, data); 546 break; 547 } 548 549 SF_UNLOCK(sc); 550 551 return(error); 552} 553 554static void 555sf_reset(sc) 556 struct sf_softc *sc; 557{ 558 register int i; 559 560 csr_write_4(sc, SF_GEN_ETH_CTL, 0); 561 SF_SETBIT(sc, SF_MACCFG_1, SF_MACCFG1_SOFTRESET); 562 DELAY(1000); 563 SF_CLRBIT(sc, SF_MACCFG_1, SF_MACCFG1_SOFTRESET); 564 565 SF_SETBIT(sc, SF_PCI_DEVCFG, SF_PCIDEVCFG_RESET); 566 567 for (i = 0; i < SF_TIMEOUT; i++) { 568 DELAY(10); 569 if (!(csr_read_4(sc, SF_PCI_DEVCFG) & SF_PCIDEVCFG_RESET)) 570 break; 571 } 572 573 if (i == SF_TIMEOUT) 574 printf("sf%d: reset never completed!\n", sc->sf_unit); 575 576 /* Wait a little while for the chip to get its brains in order. */ 577 DELAY(1000); 578} 579 580/* 581 * Probe for an Adaptec AIC-6915 chip. Check the PCI vendor and device 582 * IDs against our list and return a device name if we find a match. 583 * We also check the subsystem ID so that we can identify exactly which 584 * NIC has been found, if possible. 585 */ 586static int 587sf_probe(dev) 588 device_t dev; 589{ 590 struct sf_type *t; 591 592 t = sf_devs; 593 594 while(t->sf_name != NULL) { 595 if ((pci_get_vendor(dev) == t->sf_vid) && 596 (pci_get_device(dev) == t->sf_did)) { 597 switch((pci_read_config(dev, 598 SF_PCI_SUBVEN_ID, 4) >> 16) & 0xFFFF) { 599 case AD_SUBSYSID_62011_REV0: 600 case AD_SUBSYSID_62011_REV1: 601 device_set_desc(dev, 602 "Adaptec ANA-62011 10/100BaseTX"); 603 return (BUS_PROBE_DEFAULT); 604 case AD_SUBSYSID_62022: 605 device_set_desc(dev, 606 "Adaptec ANA-62022 10/100BaseTX"); 607 return (BUS_PROBE_DEFAULT); 608 case AD_SUBSYSID_62044_REV0: 609 case AD_SUBSYSID_62044_REV1: 610 device_set_desc(dev, 611 "Adaptec ANA-62044 10/100BaseTX"); 612 return (BUS_PROBE_DEFAULT); 613 case AD_SUBSYSID_62020: 614 device_set_desc(dev, 615 "Adaptec ANA-62020 10/100BaseFX"); 616 return (BUS_PROBE_DEFAULT); 617 case AD_SUBSYSID_69011: 618 device_set_desc(dev, 619 "Adaptec ANA-69011 10/100BaseTX"); 620 return (BUS_PROBE_DEFAULT); 621 default: 622 device_set_desc(dev, t->sf_name); 623 return (BUS_PROBE_DEFAULT); 624 break; 625 } 626 } 627 t++; 628 } 629 630 return(ENXIO); 631} 632 633/* 634 * Attach the interface. Allocate softc structures, do ifmedia 635 * setup and ethernet/BPF attach. 636 */ 637static int 638sf_attach(dev) 639 device_t dev; 640{ 641 int i; 642 struct sf_softc *sc; 643 struct ifnet *ifp; 644 int unit, rid, error = 0; 645 u_char eaddr[6]; 646 647 sc = device_get_softc(dev); 648 unit = device_get_unit(dev); 649 650 mtx_init(&sc->sf_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 651 MTX_DEF | MTX_RECURSE); 652 /* 653 * Map control/status registers. 654 */ 655 pci_enable_busmaster(dev); 656 657 rid = SF_RID; 658 sc->sf_res = bus_alloc_resource_any(dev, SF_RES, &rid, RF_ACTIVE); 659 660 if (sc->sf_res == NULL) { 661 printf ("sf%d: couldn't map ports\n", unit); 662 error = ENXIO; 663 goto fail; 664 } 665 666 sc->sf_btag = rman_get_bustag(sc->sf_res); 667 sc->sf_bhandle = rman_get_bushandle(sc->sf_res); 668 669 /* Allocate interrupt */ 670 rid = 0; 671 sc->sf_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 672 RF_SHAREABLE | RF_ACTIVE); 673 674 if (sc->sf_irq == NULL) { 675 printf("sf%d: couldn't map interrupt\n", unit); 676 error = ENXIO; 677 goto fail; 678 } 679 680 callout_handle_init(&sc->sf_stat_ch); 681 /* Reset the adapter. */ 682 sf_reset(sc); 683 684 /* 685 * Get station address from the EEPROM. 686 */ 687 for (i = 0; i < ETHER_ADDR_LEN; i++) 688 eaddr[i] = 689 sf_read_eeprom(sc, SF_EE_NODEADDR + ETHER_ADDR_LEN - i); 690 691 sc->sf_unit = unit; 692 693 /* Allocate the descriptor queues. */ 694 sc->sf_ldata = contigmalloc(sizeof(struct sf_list_data), M_DEVBUF, 695 M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0); 696 697 if (sc->sf_ldata == NULL) { 698 printf("sf%d: no memory for list buffers!\n", unit); 699 error = ENXIO; 700 goto fail; 701 } 702 703 bzero(sc->sf_ldata, sizeof(struct sf_list_data)); 704 705 /* Do MII setup. */ 706 if (mii_phy_probe(dev, &sc->sf_miibus, 707 sf_ifmedia_upd, sf_ifmedia_sts)) { 708 printf("sf%d: MII without any phy!\n", sc->sf_unit); 709 error = ENXIO; 710 goto fail; 711 } 712 713 ifp = sc->sf_ifp = if_alloc(IFT_ETHER); 714 if (ifp == NULL) { 715 printf("sf%d: can not if_alloc()\n", sc->sf_unit); 716 error = ENOSPC; 717 goto fail; 718 } 719 ifp->if_softc = sc; 720 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 721 ifp->if_mtu = ETHERMTU; 722 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST | 723 IFF_NEEDSGIANT; 724 ifp->if_ioctl = sf_ioctl; 725 ifp->if_start = sf_start; 726 ifp->if_watchdog = sf_watchdog; 727 ifp->if_init = sf_init; 728 ifp->if_baudrate = 10000000; 729 IFQ_SET_MAXLEN(&ifp->if_snd, SF_TX_DLIST_CNT - 1); 730 ifp->if_snd.ifq_drv_maxlen = SF_TX_DLIST_CNT - 1; 731 IFQ_SET_READY(&ifp->if_snd); 732#ifdef DEVICE_POLLING 733 ifp->if_capabilities |= IFCAP_POLLING; 734#endif /* DEVICE_POLLING */ 735 ifp->if_capenable = ifp->if_capabilities; 736 737 /* 738 * Call MI attach routine. 739 */ 740 ether_ifattach(ifp, eaddr); 741 742 /* Hook interrupt last to avoid having to lock softc */ 743 error = bus_setup_intr(dev, sc->sf_irq, INTR_TYPE_NET, 744 sf_intr, sc, &sc->sf_intrhand); 745 746 if (error) { 747 printf("sf%d: couldn't set up irq\n", unit); 748 ether_ifdetach(ifp); 749 if_free(ifp); 750 goto fail; 751 } 752 753fail: 754 if (error) 755 sf_detach(dev); 756 757 return(error); 758} 759 760/* 761 * Shutdown hardware and free up resources. This can be called any 762 * time after the mutex has been initialized. It is called in both 763 * the error case in attach and the normal detach case so it needs 764 * to be careful about only freeing resources that have actually been 765 * allocated. 766 */ 767static int 768sf_detach(dev) 769 device_t dev; 770{ 771 struct sf_softc *sc; 772 struct ifnet *ifp; 773 774 sc = device_get_softc(dev); 775 KASSERT(mtx_initialized(&sc->sf_mtx), ("sf mutex not initialized")); 776 SF_LOCK(sc); 777 ifp = sc->sf_ifp; 778 779 /* These should only be active if attach succeeded */ 780 if (device_is_attached(dev)) { 781 sf_stop(sc); 782 ether_ifdetach(ifp); 783 if_free(ifp); 784 } 785 if (sc->sf_miibus) 786 device_delete_child(dev, sc->sf_miibus); 787 bus_generic_detach(dev); 788 789 if (sc->sf_intrhand) 790 bus_teardown_intr(dev, sc->sf_irq, sc->sf_intrhand); 791 if (sc->sf_irq) 792 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sf_irq); 793 if (sc->sf_res) 794 bus_release_resource(dev, SF_RES, SF_RID, sc->sf_res); 795 796 if (sc->sf_ldata) 797 contigfree(sc->sf_ldata, sizeof(struct sf_list_data), M_DEVBUF); 798 799 SF_UNLOCK(sc); 800 mtx_destroy(&sc->sf_mtx); 801 802 return(0); 803} 804 805static int 806sf_init_rx_ring(sc) 807 struct sf_softc *sc; 808{ 809 struct sf_list_data *ld; 810 int i; 811 812 ld = sc->sf_ldata; 813 814 bzero((char *)ld->sf_rx_dlist_big, 815 sizeof(struct sf_rx_bufdesc_type0) * SF_RX_DLIST_CNT); 816 bzero((char *)ld->sf_rx_clist, 817 sizeof(struct sf_rx_cmpdesc_type3) * SF_RX_CLIST_CNT); 818 819 for (i = 0; i < SF_RX_DLIST_CNT; i++) { 820 if (sf_newbuf(sc, &ld->sf_rx_dlist_big[i], NULL) == ENOBUFS) 821 return(ENOBUFS); 822 } 823 824 return(0); 825} 826 827static void 828sf_init_tx_ring(sc) 829 struct sf_softc *sc; 830{ 831 struct sf_list_data *ld; 832 int i; 833 834 ld = sc->sf_ldata; 835 836 bzero((char *)ld->sf_tx_dlist, 837 sizeof(struct sf_tx_bufdesc_type0) * SF_TX_DLIST_CNT); 838 bzero((char *)ld->sf_tx_clist, 839 sizeof(struct sf_tx_cmpdesc_type0) * SF_TX_CLIST_CNT); 840 841 for (i = 0; i < SF_TX_DLIST_CNT; i++) 842 ld->sf_tx_dlist[i].sf_id = SF_TX_BUFDESC_ID; 843 for (i = 0; i < SF_TX_CLIST_CNT; i++) 844 ld->sf_tx_clist[i].sf_type = SF_TXCMPTYPE_TX; 845 846 ld->sf_tx_dlist[SF_TX_DLIST_CNT - 1].sf_end = 1; 847 sc->sf_tx_cnt = 0; 848} 849 850static int 851sf_newbuf(sc, c, m) 852 struct sf_softc *sc; 853 struct sf_rx_bufdesc_type0 *c; 854 struct mbuf *m; 855{ 856 struct mbuf *m_new = NULL; 857 858 if (m == NULL) { 859 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 860 if (m_new == NULL) 861 return(ENOBUFS); 862 863 MCLGET(m_new, M_DONTWAIT); 864 if (!(m_new->m_flags & M_EXT)) { 865 m_freem(m_new); 866 return(ENOBUFS); 867 } 868 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 869 } else { 870 m_new = m; 871 m_new->m_len = m_new->m_pkthdr.len = MCLBYTES; 872 m_new->m_data = m_new->m_ext.ext_buf; 873 } 874 875 m_adj(m_new, sizeof(u_int64_t)); 876 877 c->sf_mbuf = m_new; 878 c->sf_addrlo = SF_RX_HOSTADDR(vtophys(mtod(m_new, caddr_t))); 879 c->sf_valid = 1; 880 881 return(0); 882} 883 884/* 885 * The starfire is programmed to use 'normal' mode for packet reception, 886 * which means we use the consumer/producer model for both the buffer 887 * descriptor queue and the completion descriptor queue. The only problem 888 * with this is that it involves a lot of register accesses: we have to 889 * read the RX completion consumer and producer indexes and the RX buffer 890 * producer index, plus the RX completion consumer and RX buffer producer 891 * indexes have to be updated. It would have been easier if Adaptec had 892 * put each index in a separate register, especially given that the damn 893 * NIC has a 512K register space. 894 * 895 * In spite of all the lovely features that Adaptec crammed into the 6915, 896 * it is marred by one truly stupid design flaw, which is that receive 897 * buffer addresses must be aligned on a longword boundary. This forces 898 * the packet payload to be unaligned, which is suboptimal on the x86 and 899 * completely unuseable on the Alpha. Our only recourse is to copy received 900 * packets into properly aligned buffers before handing them off. 901 */ 902 903static void 904sf_rxeof(sc) 905 struct sf_softc *sc; 906{ 907 struct mbuf *m; 908 struct ifnet *ifp; 909 struct sf_rx_bufdesc_type0 *desc; 910 struct sf_rx_cmpdesc_type3 *cur_rx; 911 u_int32_t rxcons, rxprod; 912 int cmpprodidx, cmpconsidx, bufprodidx; 913 914 SF_LOCK_ASSERT(sc); 915 916 ifp = sc->sf_ifp; 917 918 rxcons = csr_read_4(sc, SF_CQ_CONSIDX); 919 rxprod = csr_read_4(sc, SF_RXDQ_PTR_Q1); 920 cmpprodidx = SF_IDX_LO(csr_read_4(sc, SF_CQ_PRODIDX)); 921 cmpconsidx = SF_IDX_LO(rxcons); 922 bufprodidx = SF_IDX_LO(rxprod); 923 924 while (cmpconsidx != cmpprodidx) { 925 struct mbuf *m0; 926 927#ifdef DEVICE_POLLING 928 if (ifp->if_flags & IFF_POLLING) { 929 if (sc->rxcycles <= 0) 930 break; 931 sc->rxcycles--; 932 } 933#endif /* DEVICE_POLLING */ 934 935 cur_rx = &sc->sf_ldata->sf_rx_clist[cmpconsidx]; 936 desc = &sc->sf_ldata->sf_rx_dlist_big[cur_rx->sf_endidx]; 937 m = desc->sf_mbuf; 938 SF_INC(cmpconsidx, SF_RX_CLIST_CNT); 939 SF_INC(bufprodidx, SF_RX_DLIST_CNT); 940 941 if (!(cur_rx->sf_status1 & SF_RXSTAT1_OK)) { 942 ifp->if_ierrors++; 943 sf_newbuf(sc, desc, m); 944 continue; 945 } 946 947 m0 = m_devget(mtod(m, char *), cur_rx->sf_len, ETHER_ALIGN, 948 ifp, NULL); 949 sf_newbuf(sc, desc, m); 950 if (m0 == NULL) { 951 ifp->if_ierrors++; 952 continue; 953 } 954 m = m0; 955 956 ifp->if_ipackets++; 957 SF_UNLOCK(sc); 958 (*ifp->if_input)(ifp, m); 959 SF_LOCK(sc); 960 } 961 962 csr_write_4(sc, SF_CQ_CONSIDX, 963 (rxcons & ~SF_CQ_CONSIDX_RXQ1) | cmpconsidx); 964 csr_write_4(sc, SF_RXDQ_PTR_Q1, 965 (rxprod & ~SF_RXDQ_PRODIDX) | bufprodidx); 966} 967 968/* 969 * Read the transmit status from the completion queue and release 970 * mbufs. Note that the buffer descriptor index in the completion 971 * descriptor is an offset from the start of the transmit buffer 972 * descriptor list in bytes. This is important because the manual 973 * gives the impression that it should match the producer/consumer 974 * index, which is the offset in 8 byte blocks. 975 */ 976static void 977sf_txeof(sc) 978 struct sf_softc *sc; 979{ 980 int txcons, cmpprodidx, cmpconsidx; 981 struct sf_tx_cmpdesc_type1 *cur_cmp; 982 struct sf_tx_bufdesc_type0 *cur_tx; 983 struct ifnet *ifp; 984 985 ifp = sc->sf_ifp; 986 987 txcons = csr_read_4(sc, SF_CQ_CONSIDX); 988 cmpprodidx = SF_IDX_HI(csr_read_4(sc, SF_CQ_PRODIDX)); 989 cmpconsidx = SF_IDX_HI(txcons); 990 991 while (cmpconsidx != cmpprodidx) { 992 cur_cmp = &sc->sf_ldata->sf_tx_clist[cmpconsidx]; 993 cur_tx = &sc->sf_ldata->sf_tx_dlist[cur_cmp->sf_index >> 7]; 994 995 if (cur_cmp->sf_txstat & SF_TXSTAT_TX_OK) 996 ifp->if_opackets++; 997 else { 998 if (cur_cmp->sf_txstat & SF_TXSTAT_TX_UNDERRUN) 999 sf_txthresh_adjust(sc); 1000 ifp->if_oerrors++; 1001 } 1002 1003 sc->sf_tx_cnt--; 1004 if (cur_tx->sf_mbuf != NULL) { 1005 m_freem(cur_tx->sf_mbuf); 1006 cur_tx->sf_mbuf = NULL; 1007 } else 1008 break; 1009 SF_INC(cmpconsidx, SF_TX_CLIST_CNT); 1010 } 1011 1012 ifp->if_timer = 0; 1013 ifp->if_flags &= ~IFF_OACTIVE; 1014 1015 csr_write_4(sc, SF_CQ_CONSIDX, 1016 (txcons & ~SF_CQ_CONSIDX_TXQ) | 1017 ((cmpconsidx << 16) & 0xFFFF0000)); 1018} 1019 1020static void 1021sf_txthresh_adjust(sc) 1022 struct sf_softc *sc; 1023{ 1024 u_int32_t txfctl; 1025 u_int8_t txthresh; 1026 1027 txfctl = csr_read_4(sc, SF_TX_FRAMCTL); 1028 txthresh = txfctl & SF_TXFRMCTL_TXTHRESH; 1029 if (txthresh < 0xFF) { 1030 txthresh++; 1031 txfctl &= ~SF_TXFRMCTL_TXTHRESH; 1032 txfctl |= txthresh; 1033#ifdef DIAGNOSTIC 1034 printf("sf%d: tx underrun, increasing " 1035 "tx threshold to %d bytes\n", 1036 sc->sf_unit, txthresh * 4); 1037#endif 1038 csr_write_4(sc, SF_TX_FRAMCTL, txfctl); 1039 } 1040} 1041 1042#ifdef DEVICE_POLLING 1043static void 1044sf_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) 1045{ 1046 struct sf_softc *sc = ifp->if_softc; 1047 1048 SF_LOCK(sc); 1049 sf_poll_locked(ifp, cmd, count); 1050 SF_UNLOCK(sc); 1051} 1052 1053static void 1054sf_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count) 1055{ 1056 struct sf_softc *sc = ifp->if_softc; 1057 1058 SF_LOCK_ASSERT(sc); 1059 1060 if (!(ifp->if_capenable & IFCAP_POLLING)) { 1061 ether_poll_deregister(ifp); 1062 cmd = POLL_DEREGISTER; 1063 } 1064 1065 if (cmd == POLL_DEREGISTER) { 1066 /* Final call, enable interrupts. */ 1067 csr_write_4(sc, SF_IMR, SF_INTRS); 1068 return; 1069 } 1070 1071 sc->rxcycles = count; 1072 sf_rxeof(sc); 1073 sf_txeof(sc); 1074 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 1075 sf_start(ifp); 1076 1077 if (cmd == POLL_AND_CHECK_STATUS) { 1078 u_int32_t status; 1079 1080 status = csr_read_4(sc, SF_ISR); 1081 if (status) 1082 csr_write_4(sc, SF_ISR, status); 1083 1084 if (status & SF_ISR_TX_LOFIFO) 1085 sf_txthresh_adjust(sc); 1086 1087 if (status & SF_ISR_ABNORMALINTR) { 1088 if (status & SF_ISR_STATSOFLOW) { 1089 untimeout(sf_stats_update, sc, 1090 sc->sf_stat_ch); 1091 sf_stats_update(sc); 1092 } else 1093 sf_init(sc); 1094 } 1095 } 1096} 1097#endif /* DEVICE_POLLING */ 1098 1099static void 1100sf_intr(arg) 1101 void *arg; 1102{ 1103 struct sf_softc *sc; 1104 struct ifnet *ifp; 1105 u_int32_t status; 1106 1107 sc = arg; 1108 SF_LOCK(sc); 1109 1110 ifp = sc->sf_ifp; 1111 1112#ifdef DEVICE_POLLING 1113 if (ifp->if_flags & IFF_POLLING) 1114 goto done_locked; 1115 1116 if ((ifp->if_capenable & IFCAP_POLLING) && 1117 ether_poll_register(sf_poll, ifp)) { 1118 /* OK, disable interrupts. */ 1119 csr_write_4(sc, SF_IMR, 0x00000000); 1120 sf_poll_locked(ifp, 0, 1); 1121 goto done_locked; 1122 } 1123#endif /* DEVICE_POLLING */ 1124 1125 if (!(csr_read_4(sc, SF_ISR_SHADOW) & SF_ISR_PCIINT_ASSERTED)) { 1126 SF_UNLOCK(sc); 1127 return; 1128 } 1129 1130 /* Disable interrupts. */ 1131 csr_write_4(sc, SF_IMR, 0x00000000); 1132 1133 for (;;) { 1134 status = csr_read_4(sc, SF_ISR); 1135 if (status) 1136 csr_write_4(sc, SF_ISR, status); 1137 1138 if (!(status & SF_INTRS)) 1139 break; 1140 1141 if (status & SF_ISR_RXDQ1_DMADONE) 1142 sf_rxeof(sc); 1143 1144 if (status & SF_ISR_TX_TXDONE || 1145 status & SF_ISR_TX_DMADONE || 1146 status & SF_ISR_TX_QUEUEDONE) 1147 sf_txeof(sc); 1148 1149 if (status & SF_ISR_TX_LOFIFO) 1150 sf_txthresh_adjust(sc); 1151 1152 if (status & SF_ISR_ABNORMALINTR) { 1153 if (status & SF_ISR_STATSOFLOW) { 1154 untimeout(sf_stats_update, sc, 1155 sc->sf_stat_ch); 1156 sf_stats_update(sc); 1157 } else 1158 sf_init(sc); 1159 } 1160 } 1161 1162 /* Re-enable interrupts. */ 1163 csr_write_4(sc, SF_IMR, SF_INTRS); 1164 1165 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 1166 sf_start(ifp); 1167 1168#ifdef DEVICE_POLLING 1169done_locked: 1170#endif /* DEVICE_POLLING */ 1171 SF_UNLOCK(sc); 1172} 1173 1174static void 1175sf_init(xsc) 1176 void *xsc; 1177{ 1178 struct sf_softc *sc; 1179 struct ifnet *ifp; 1180 struct mii_data *mii; 1181 int i; 1182 1183 sc = xsc; 1184 SF_LOCK(sc); 1185 ifp = sc->sf_ifp; 1186 mii = device_get_softc(sc->sf_miibus); 1187 1188 sf_stop(sc); 1189 sf_reset(sc); 1190 1191 /* Init all the receive filter registers */ 1192 for (i = SF_RXFILT_PERFECT_BASE; 1193 i < (SF_RXFILT_HASH_MAX + 1); i += 4) 1194 csr_write_4(sc, i, 0); 1195 1196 /* Empty stats counter registers. */ 1197 for (i = 0; i < sizeof(struct sf_stats)/sizeof(u_int32_t); i++) 1198 csr_write_4(sc, SF_STATS_BASE + 1199 (i + sizeof(u_int32_t)), 0); 1200 1201 /* Init our MAC address */ 1202 csr_write_4(sc, SF_PAR0, *(u_int32_t *)(&IFP2ENADDR(sc->sf_ifp)[0])); 1203 csr_write_4(sc, SF_PAR1, *(u_int32_t *)(&IFP2ENADDR(sc->sf_ifp)[4])); 1204 sf_setperf(sc, 0, (caddr_t)&IFP2ENADDR(sc->sf_ifp)); 1205 1206 if (sf_init_rx_ring(sc) == ENOBUFS) { 1207 printf("sf%d: initialization failed: no " 1208 "memory for rx buffers\n", sc->sf_unit); 1209 SF_UNLOCK(sc); 1210 return; 1211 } 1212 1213 sf_init_tx_ring(sc); 1214 1215 csr_write_4(sc, SF_RXFILT, SF_PERFMODE_NORMAL|SF_HASHMODE_WITHVLAN); 1216 1217 /* If we want promiscuous mode, set the allframes bit. */ 1218 if (ifp->if_flags & IFF_PROMISC) { 1219 SF_SETBIT(sc, SF_RXFILT, SF_RXFILT_PROMISC); 1220 } else { 1221 SF_CLRBIT(sc, SF_RXFILT, SF_RXFILT_PROMISC); 1222 } 1223 1224 if (ifp->if_flags & IFF_BROADCAST) { 1225 SF_SETBIT(sc, SF_RXFILT, SF_RXFILT_BROAD); 1226 } else { 1227 SF_CLRBIT(sc, SF_RXFILT, SF_RXFILT_BROAD); 1228 } 1229 1230 /* 1231 * Load the multicast filter. 1232 */ 1233 sf_setmulti(sc); 1234 1235 /* Init the completion queue indexes */ 1236 csr_write_4(sc, SF_CQ_CONSIDX, 0); 1237 csr_write_4(sc, SF_CQ_PRODIDX, 0); 1238 1239 /* Init the RX completion queue */ 1240 csr_write_4(sc, SF_RXCQ_CTL_1, 1241 vtophys(sc->sf_ldata->sf_rx_clist) & SF_RXCQ_ADDR); 1242 SF_SETBIT(sc, SF_RXCQ_CTL_1, SF_RXCQTYPE_3); 1243 1244 /* Init RX DMA control. */ 1245 SF_SETBIT(sc, SF_RXDMA_CTL, SF_RXDMA_REPORTBADPKTS); 1246 1247 /* Init the RX buffer descriptor queue. */ 1248 csr_write_4(sc, SF_RXDQ_ADDR_Q1, 1249 vtophys(sc->sf_ldata->sf_rx_dlist_big)); 1250 csr_write_4(sc, SF_RXDQ_CTL_1, (MCLBYTES << 16) | SF_DESCSPACE_16BYTES); 1251 csr_write_4(sc, SF_RXDQ_PTR_Q1, SF_RX_DLIST_CNT - 1); 1252 1253 /* Init the TX completion queue */ 1254 csr_write_4(sc, SF_TXCQ_CTL, 1255 vtophys(sc->sf_ldata->sf_tx_clist) & SF_RXCQ_ADDR); 1256 1257 /* Init the TX buffer descriptor queue. */ 1258 csr_write_4(sc, SF_TXDQ_ADDR_HIPRIO, 1259 vtophys(sc->sf_ldata->sf_tx_dlist)); 1260 SF_SETBIT(sc, SF_TX_FRAMCTL, SF_TXFRMCTL_CPLAFTERTX); 1261 csr_write_4(sc, SF_TXDQ_CTL, 1262 SF_TXBUFDESC_TYPE0|SF_TXMINSPACE_128BYTES|SF_TXSKIPLEN_8BYTES); 1263 SF_SETBIT(sc, SF_TXDQ_CTL, SF_TXDQCTL_NODMACMP); 1264 1265 /* Enable autopadding of short TX frames. */ 1266 SF_SETBIT(sc, SF_MACCFG_1, SF_MACCFG1_AUTOPAD); 1267 1268#ifdef DEVICE_POLLING 1269 /* Disable interrupts if we are polling. */ 1270 if (ifp->if_flags & IFF_POLLING) 1271 csr_write_4(sc, SF_IMR, 0x00000000); 1272 else 1273#endif /* DEVICE_POLLING */ 1274 1275 /* Enable interrupts. */ 1276 csr_write_4(sc, SF_IMR, SF_INTRS); 1277 SF_SETBIT(sc, SF_PCI_DEVCFG, SF_PCIDEVCFG_INTR_ENB); 1278 1279 /* Enable the RX and TX engines. */ 1280 SF_SETBIT(sc, SF_GEN_ETH_CTL, SF_ETHCTL_RX_ENB|SF_ETHCTL_RXDMA_ENB); 1281 SF_SETBIT(sc, SF_GEN_ETH_CTL, SF_ETHCTL_TX_ENB|SF_ETHCTL_TXDMA_ENB); 1282 1283 /*mii_mediachg(mii);*/ 1284 sf_ifmedia_upd(ifp); 1285 1286 ifp->if_flags |= IFF_RUNNING; 1287 ifp->if_flags &= ~IFF_OACTIVE; 1288 1289 sc->sf_stat_ch = timeout(sf_stats_update, sc, hz); 1290 1291 SF_UNLOCK(sc); 1292} 1293 1294static int 1295sf_encap(sc, c, m_head) 1296 struct sf_softc *sc; 1297 struct sf_tx_bufdesc_type0 *c; 1298 struct mbuf *m_head; 1299{ 1300 int frag = 0; 1301 struct sf_frag *f = NULL; 1302 struct mbuf *m; 1303 1304 m = m_head; 1305 1306 for (m = m_head, frag = 0; m != NULL; m = m->m_next) { 1307 if (m->m_len != 0) { 1308 if (frag == SF_MAXFRAGS) 1309 break; 1310 f = &c->sf_frags[frag]; 1311 if (frag == 0) 1312 f->sf_pktlen = m_head->m_pkthdr.len; 1313 f->sf_fraglen = m->m_len; 1314 f->sf_addr = vtophys(mtod(m, vm_offset_t)); 1315 frag++; 1316 } 1317 } 1318 1319 if (m != NULL) { 1320 struct mbuf *m_new = NULL; 1321 1322 MGETHDR(m_new, M_DONTWAIT, MT_DATA); 1323 if (m_new == NULL) { 1324 printf("sf%d: no memory for tx list\n", sc->sf_unit); 1325 return(1); 1326 } 1327 1328 if (m_head->m_pkthdr.len > MHLEN) { 1329 MCLGET(m_new, M_DONTWAIT); 1330 if (!(m_new->m_flags & M_EXT)) { 1331 m_freem(m_new); 1332 printf("sf%d: no memory for tx list\n", 1333 sc->sf_unit); 1334 return(1); 1335 } 1336 } 1337 m_copydata(m_head, 0, m_head->m_pkthdr.len, 1338 mtod(m_new, caddr_t)); 1339 m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len; 1340 m_freem(m_head); 1341 m_head = m_new; 1342 f = &c->sf_frags[0]; 1343 f->sf_fraglen = f->sf_pktlen = m_head->m_pkthdr.len; 1344 f->sf_addr = vtophys(mtod(m_head, caddr_t)); 1345 frag = 1; 1346 } 1347 1348 c->sf_mbuf = m_head; 1349 c->sf_id = SF_TX_BUFDESC_ID; 1350 c->sf_fragcnt = frag; 1351 c->sf_intr = 1; 1352 c->sf_caltcp = 0; 1353 c->sf_crcen = 1; 1354 1355 return(0); 1356} 1357 1358static void 1359sf_start(ifp) 1360 struct ifnet *ifp; 1361{ 1362 struct sf_softc *sc; 1363 struct sf_tx_bufdesc_type0 *cur_tx = NULL; 1364 struct mbuf *m_head = NULL; 1365 int i, txprod; 1366 1367 sc = ifp->if_softc; 1368 SF_LOCK(sc); 1369 1370 if (!sc->sf_link && ifp->if_snd.ifq_len < 10) { 1371 SF_UNLOCK(sc); 1372 return; 1373 } 1374 1375 if (ifp->if_flags & IFF_OACTIVE) { 1376 SF_UNLOCK(sc); 1377 return; 1378 } 1379 1380 txprod = csr_read_4(sc, SF_TXDQ_PRODIDX); 1381 i = SF_IDX_HI(txprod) >> 4; 1382 1383 if (sc->sf_ldata->sf_tx_dlist[i].sf_mbuf != NULL) { 1384 printf("sf%d: TX ring full, resetting\n", sc->sf_unit); 1385 sf_init(sc); 1386 txprod = csr_read_4(sc, SF_TXDQ_PRODIDX); 1387 i = SF_IDX_HI(txprod) >> 4; 1388 } 1389 1390 while(sc->sf_ldata->sf_tx_dlist[i].sf_mbuf == NULL) { 1391 if (sc->sf_tx_cnt >= (SF_TX_DLIST_CNT - 5)) { 1392 ifp->if_flags |= IFF_OACTIVE; 1393 cur_tx = NULL; 1394 break; 1395 } 1396 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head); 1397 if (m_head == NULL) 1398 break; 1399 1400 cur_tx = &sc->sf_ldata->sf_tx_dlist[i]; 1401 if (sf_encap(sc, cur_tx, m_head)) { 1402 IFQ_DRV_PREPEND(&ifp->if_snd, m_head); 1403 ifp->if_flags |= IFF_OACTIVE; 1404 cur_tx = NULL; 1405 break; 1406 } 1407 1408 /* 1409 * If there's a BPF listener, bounce a copy of this frame 1410 * to him. 1411 */ 1412 BPF_MTAP(ifp, m_head); 1413 1414 SF_INC(i, SF_TX_DLIST_CNT); 1415 sc->sf_tx_cnt++; 1416 /* 1417 * Don't get the TX DMA queue get too full. 1418 */ 1419 if (sc->sf_tx_cnt > 64) 1420 break; 1421 } 1422 1423 if (cur_tx == NULL) { 1424 SF_UNLOCK(sc); 1425 return; 1426 } 1427 1428 /* Transmit */ 1429 csr_write_4(sc, SF_TXDQ_PRODIDX, 1430 (txprod & ~SF_TXDQ_PRODIDX_HIPRIO) | 1431 ((i << 20) & 0xFFFF0000)); 1432 1433 ifp->if_timer = 5; 1434 1435 SF_UNLOCK(sc); 1436} 1437 1438static void 1439sf_stop(sc) 1440 struct sf_softc *sc; 1441{ 1442 int i; 1443 struct ifnet *ifp; 1444 1445 SF_LOCK(sc); 1446 1447 ifp = sc->sf_ifp; 1448 1449 untimeout(sf_stats_update, sc, sc->sf_stat_ch); 1450 1451#ifdef DEVICE_POLLING 1452 ether_poll_deregister(ifp); 1453#endif /* DEVICE_POLLING */ 1454 1455 csr_write_4(sc, SF_GEN_ETH_CTL, 0); 1456 csr_write_4(sc, SF_CQ_CONSIDX, 0); 1457 csr_write_4(sc, SF_CQ_PRODIDX, 0); 1458 csr_write_4(sc, SF_RXDQ_ADDR_Q1, 0); 1459 csr_write_4(sc, SF_RXDQ_CTL_1, 0); 1460 csr_write_4(sc, SF_RXDQ_PTR_Q1, 0); 1461 csr_write_4(sc, SF_TXCQ_CTL, 0); 1462 csr_write_4(sc, SF_TXDQ_ADDR_HIPRIO, 0); 1463 csr_write_4(sc, SF_TXDQ_CTL, 0); 1464 sf_reset(sc); 1465 1466 sc->sf_link = 0; 1467 1468 for (i = 0; i < SF_RX_DLIST_CNT; i++) { 1469 if (sc->sf_ldata->sf_rx_dlist_big[i].sf_mbuf != NULL) { 1470 m_freem(sc->sf_ldata->sf_rx_dlist_big[i].sf_mbuf); 1471 sc->sf_ldata->sf_rx_dlist_big[i].sf_mbuf = NULL; 1472 } 1473 } 1474 1475 for (i = 0; i < SF_TX_DLIST_CNT; i++) { 1476 if (sc->sf_ldata->sf_tx_dlist[i].sf_mbuf != NULL) { 1477 m_freem(sc->sf_ldata->sf_tx_dlist[i].sf_mbuf); 1478 sc->sf_ldata->sf_tx_dlist[i].sf_mbuf = NULL; 1479 } 1480 } 1481 1482 ifp->if_flags &= ~(IFF_RUNNING|IFF_OACTIVE); 1483 SF_UNLOCK(sc); 1484} 1485 1486/* 1487 * Note: it is important that this function not be interrupted. We 1488 * use a two-stage register access scheme: if we are interrupted in 1489 * between setting the indirect address register and reading from the 1490 * indirect data register, the contents of the address register could 1491 * be changed out from under us. 1492 */ 1493static void 1494sf_stats_update(xsc) 1495 void *xsc; 1496{ 1497 struct sf_softc *sc; 1498 struct ifnet *ifp; 1499 struct mii_data *mii; 1500 struct sf_stats stats; 1501 u_int32_t *ptr; 1502 int i; 1503 1504 sc = xsc; 1505 SF_LOCK(sc); 1506 ifp = sc->sf_ifp; 1507 mii = device_get_softc(sc->sf_miibus); 1508 1509 ptr = (u_int32_t *)&stats; 1510 for (i = 0; i < sizeof(stats)/sizeof(u_int32_t); i++) 1511 ptr[i] = csr_read_4(sc, SF_STATS_BASE + 1512 (i + sizeof(u_int32_t))); 1513 1514 for (i = 0; i < sizeof(stats)/sizeof(u_int32_t); i++) 1515 csr_write_4(sc, SF_STATS_BASE + 1516 (i + sizeof(u_int32_t)), 0); 1517 1518 ifp->if_collisions += stats.sf_tx_single_colls + 1519 stats.sf_tx_multi_colls + stats.sf_tx_excess_colls; 1520 1521 mii_tick(mii); 1522 1523 if (!sc->sf_link && mii->mii_media_status & IFM_ACTIVE && 1524 IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) { 1525 sc->sf_link++; 1526 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 1527 sf_start(ifp); 1528 } 1529 1530 sc->sf_stat_ch = timeout(sf_stats_update, sc, hz); 1531 1532 SF_UNLOCK(sc); 1533} 1534 1535static void 1536sf_watchdog(ifp) 1537 struct ifnet *ifp; 1538{ 1539 struct sf_softc *sc; 1540 1541 sc = ifp->if_softc; 1542 1543 SF_LOCK(sc); 1544 1545 ifp->if_oerrors++; 1546 printf("sf%d: watchdog timeout\n", sc->sf_unit); 1547 1548 sf_stop(sc); 1549 sf_reset(sc); 1550 sf_init(sc); 1551 1552 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 1553 sf_start(ifp); 1554 1555 SF_UNLOCK(sc); 1556} 1557 1558static void 1559sf_shutdown(dev) 1560 device_t dev; 1561{ 1562 struct sf_softc *sc; 1563 1564 sc = device_get_softc(dev); 1565 1566 sf_stop(sc); 1567} 1568