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