sfxge.c revision 280518
1/*- 2 * Copyright (c) 2010-2011 Solarflare Communications, Inc. 3 * All rights reserved. 4 * 5 * This software was developed in part by Philip Paeps under contract for 6 * Solarflare Communications, Inc. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#include <sys/cdefs.h> 31__FBSDID("$FreeBSD: stable/10/sys/dev/sfxge/sfxge.c 280518 2015-03-25 10:23:00Z arybchik $"); 32 33#include <sys/param.h> 34#include <sys/kernel.h> 35#include <sys/bus.h> 36#include <sys/rman.h> 37#include <sys/lock.h> 38#include <sys/module.h> 39#include <sys/mutex.h> 40#include <sys/smp.h> 41#include <sys/socket.h> 42#include <sys/taskqueue.h> 43#include <sys/sockio.h> 44#include <sys/sysctl.h> 45#include <sys/syslog.h> 46 47#include <dev/pci/pcireg.h> 48#include <dev/pci/pcivar.h> 49 50#include <net/ethernet.h> 51#include <net/if.h> 52#include <net/if_media.h> 53#include <net/if_types.h> 54 55#include "common/efx.h" 56 57#include "sfxge.h" 58#include "sfxge_rx.h" 59 60#define SFXGE_CAP (IFCAP_VLAN_MTU | \ 61 IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM | IFCAP_TSO | \ 62 IFCAP_JUMBO_MTU | IFCAP_LRO | \ 63 IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE) 64#define SFXGE_CAP_ENABLE SFXGE_CAP 65#define SFXGE_CAP_FIXED (IFCAP_VLAN_MTU | IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM | \ 66 IFCAP_JUMBO_MTU | IFCAP_LINKSTATE) 67 68MALLOC_DEFINE(M_SFXGE, "sfxge", "Solarflare 10GigE driver"); 69 70 71SYSCTL_NODE(_hw, OID_AUTO, sfxge, CTLFLAG_RD, 0, 72 "SFXGE driver parameters"); 73 74#define SFXGE_PARAM_RX_RING SFXGE_PARAM(rx_ring) 75static int sfxge_rx_ring_entries = SFXGE_NDESCS; 76TUNABLE_INT(SFXGE_PARAM_RX_RING, &sfxge_rx_ring_entries); 77SYSCTL_INT(_hw_sfxge, OID_AUTO, rx_ring, CTLFLAG_RDTUN, 78 &sfxge_rx_ring_entries, 0, 79 "Maximum number of descriptors in a receive ring"); 80 81#define SFXGE_PARAM_TX_RING SFXGE_PARAM(tx_ring) 82static int sfxge_tx_ring_entries = SFXGE_NDESCS; 83TUNABLE_INT(SFXGE_PARAM_TX_RING, &sfxge_tx_ring_entries); 84SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_ring, CTLFLAG_RDTUN, 85 &sfxge_tx_ring_entries, 0, 86 "Maximum number of descriptors in a transmit ring"); 87 88 89static void 90sfxge_reset(void *arg, int npending); 91 92static int 93sfxge_start(struct sfxge_softc *sc) 94{ 95 int rc; 96 97 sx_assert(&sc->softc_lock, LA_XLOCKED); 98 99 if (sc->init_state == SFXGE_STARTED) 100 return (0); 101 102 if (sc->init_state != SFXGE_REGISTERED) { 103 rc = EINVAL; 104 goto fail; 105 } 106 107 if ((rc = efx_nic_init(sc->enp)) != 0) 108 goto fail; 109 110 /* Start processing interrupts. */ 111 if ((rc = sfxge_intr_start(sc)) != 0) 112 goto fail2; 113 114 /* Start processing events. */ 115 if ((rc = sfxge_ev_start(sc)) != 0) 116 goto fail3; 117 118 /* Start the receiver side. */ 119 if ((rc = sfxge_rx_start(sc)) != 0) 120 goto fail4; 121 122 /* Start the transmitter side. */ 123 if ((rc = sfxge_tx_start(sc)) != 0) 124 goto fail5; 125 126 /* Fire up the port. */ 127 if ((rc = sfxge_port_start(sc)) != 0) 128 goto fail6; 129 130 sc->init_state = SFXGE_STARTED; 131 132 /* Tell the stack we're running. */ 133 sc->ifnet->if_drv_flags |= IFF_DRV_RUNNING; 134 sc->ifnet->if_drv_flags &= ~IFF_DRV_OACTIVE; 135 136 return (0); 137 138fail6: 139 sfxge_tx_stop(sc); 140 141fail5: 142 sfxge_rx_stop(sc); 143 144fail4: 145 sfxge_ev_stop(sc); 146 147fail3: 148 sfxge_intr_stop(sc); 149 150fail2: 151 efx_nic_fini(sc->enp); 152 153fail: 154 device_printf(sc->dev, "sfxge_start: %d\n", rc); 155 156 return (rc); 157} 158 159static void 160sfxge_if_init(void *arg) 161{ 162 struct sfxge_softc *sc; 163 164 sc = (struct sfxge_softc *)arg; 165 166 sx_xlock(&sc->softc_lock); 167 (void)sfxge_start(sc); 168 sx_xunlock(&sc->softc_lock); 169} 170 171static void 172sfxge_stop(struct sfxge_softc *sc) 173{ 174 sx_assert(&sc->softc_lock, LA_XLOCKED); 175 176 if (sc->init_state != SFXGE_STARTED) 177 return; 178 179 sc->init_state = SFXGE_REGISTERED; 180 181 /* Stop the port. */ 182 sfxge_port_stop(sc); 183 184 /* Stop the transmitter. */ 185 sfxge_tx_stop(sc); 186 187 /* Stop the receiver. */ 188 sfxge_rx_stop(sc); 189 190 /* Stop processing events. */ 191 sfxge_ev_stop(sc); 192 193 /* Stop processing interrupts. */ 194 sfxge_intr_stop(sc); 195 196 efx_nic_fini(sc->enp); 197 198 sc->ifnet->if_drv_flags &= ~IFF_DRV_RUNNING; 199} 200 201static int 202sfxge_if_ioctl(struct ifnet *ifp, unsigned long command, caddr_t data) 203{ 204 struct sfxge_softc *sc; 205 struct ifreq *ifr; 206 int error; 207 208 ifr = (struct ifreq *)data; 209 sc = ifp->if_softc; 210 error = 0; 211 212 switch (command) { 213 case SIOCSIFFLAGS: 214 sx_xlock(&sc->softc_lock); 215 if (ifp->if_flags & IFF_UP) { 216 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 217 if ((ifp->if_flags ^ sc->if_flags) & 218 (IFF_PROMISC | IFF_ALLMULTI)) { 219 sfxge_mac_filter_set(sc); 220 } 221 } else 222 sfxge_start(sc); 223 } else 224 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 225 sfxge_stop(sc); 226 sc->if_flags = ifp->if_flags; 227 sx_xunlock(&sc->softc_lock); 228 break; 229 case SIOCSIFMTU: 230 if (ifr->ifr_mtu == ifp->if_mtu) { 231 /* Nothing to do */ 232 error = 0; 233 } else if (ifr->ifr_mtu > SFXGE_MAX_MTU) { 234 error = EINVAL; 235 } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 236 ifp->if_mtu = ifr->ifr_mtu; 237 error = 0; 238 } else { 239 /* Restart required */ 240 sx_xlock(&sc->softc_lock); 241 sfxge_stop(sc); 242 ifp->if_mtu = ifr->ifr_mtu; 243 error = sfxge_start(sc); 244 sx_xunlock(&sc->softc_lock); 245 if (error != 0) { 246 ifp->if_flags &= ~IFF_UP; 247 ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 248 if_down(ifp); 249 } 250 } 251 break; 252 case SIOCADDMULTI: 253 case SIOCDELMULTI: 254 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 255 sfxge_mac_filter_set(sc); 256 break; 257 case SIOCSIFCAP: 258 sx_xlock(&sc->softc_lock); 259 260 /* 261 * The networking core already rejects attempts to 262 * enable capabilities we don't have. We still have 263 * to reject attempts to disable capabilities that we 264 * can't (yet) disable. 265 */ 266 if (~ifr->ifr_reqcap & SFXGE_CAP_FIXED) { 267 error = EINVAL; 268 sx_xunlock(&sc->softc_lock); 269 break; 270 } 271 272 ifp->if_capenable = ifr->ifr_reqcap; 273 if (ifp->if_capenable & IFCAP_TXCSUM) 274 ifp->if_hwassist |= (CSUM_IP | CSUM_TCP | CSUM_UDP); 275 else 276 ifp->if_hwassist &= ~(CSUM_IP | CSUM_TCP | CSUM_UDP); 277 if (ifp->if_capenable & IFCAP_TSO) 278 ifp->if_hwassist |= CSUM_TSO; 279 else 280 ifp->if_hwassist &= ~CSUM_TSO; 281 282 sx_xunlock(&sc->softc_lock); 283 break; 284 case SIOCSIFMEDIA: 285 case SIOCGIFMEDIA: 286 error = ifmedia_ioctl(ifp, ifr, &sc->media, command); 287 break; 288 default: 289 error = ether_ioctl(ifp, command, data); 290 } 291 292 return (error); 293} 294 295static void 296sfxge_ifnet_fini(struct ifnet *ifp) 297{ 298 struct sfxge_softc *sc = ifp->if_softc; 299 300 sx_xlock(&sc->softc_lock); 301 sfxge_stop(sc); 302 sx_xunlock(&sc->softc_lock); 303 304 ifmedia_removeall(&sc->media); 305 ether_ifdetach(ifp); 306 if_free(ifp); 307} 308 309static int 310sfxge_ifnet_init(struct ifnet *ifp, struct sfxge_softc *sc) 311{ 312 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp); 313 device_t dev; 314 int rc; 315 316 dev = sc->dev; 317 sc->ifnet = ifp; 318 319 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 320 ifp->if_init = sfxge_if_init; 321 ifp->if_softc = sc; 322 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 323 ifp->if_ioctl = sfxge_if_ioctl; 324 325 ifp->if_capabilities = SFXGE_CAP; 326 ifp->if_capenable = SFXGE_CAP_ENABLE; 327 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO; 328 329 ether_ifattach(ifp, encp->enc_mac_addr); 330 331#ifdef SFXGE_HAVE_MQ 332 ifp->if_transmit = sfxge_if_transmit; 333 ifp->if_qflush = sfxge_if_qflush; 334#else 335 ifp->if_start = sfxge_if_start; 336 IFQ_SET_MAXLEN(&ifp->if_snd, sc->txq_entries - 1); 337 ifp->if_snd.ifq_drv_maxlen = sc->txq_entries - 1; 338 IFQ_SET_READY(&ifp->if_snd); 339 340 mtx_init(&sc->tx_lock, "txq", NULL, MTX_DEF); 341#endif 342 343 if ((rc = sfxge_port_ifmedia_init(sc)) != 0) 344 goto fail; 345 346 return (0); 347 348fail: 349 ether_ifdetach(sc->ifnet); 350 return (rc); 351} 352 353void 354sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n, uint32_t *idp) 355{ 356 KASSERT(sc->buffer_table_next + n <= 357 efx_nic_cfg_get(sc->enp)->enc_buftbl_limit, 358 ("buffer table full")); 359 360 *idp = sc->buffer_table_next; 361 sc->buffer_table_next += n; 362} 363 364static int 365sfxge_bar_init(struct sfxge_softc *sc) 366{ 367 efsys_bar_t *esbp = &sc->bar; 368 369 esbp->esb_rid = PCIR_BAR(EFX_MEM_BAR); 370 if ((esbp->esb_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY, 371 &esbp->esb_rid, RF_ACTIVE)) == NULL) { 372 device_printf(sc->dev, "Cannot allocate BAR region %d\n", 373 EFX_MEM_BAR); 374 return (ENXIO); 375 } 376 esbp->esb_tag = rman_get_bustag(esbp->esb_res); 377 esbp->esb_handle = rman_get_bushandle(esbp->esb_res); 378 mtx_init(&esbp->esb_lock, "sfxge_efsys_bar", NULL, MTX_DEF); 379 380 return (0); 381} 382 383static void 384sfxge_bar_fini(struct sfxge_softc *sc) 385{ 386 efsys_bar_t *esbp = &sc->bar; 387 388 bus_release_resource(sc->dev, SYS_RES_MEMORY, esbp->esb_rid, 389 esbp->esb_res); 390 mtx_destroy(&esbp->esb_lock); 391} 392 393static int 394sfxge_create(struct sfxge_softc *sc) 395{ 396 device_t dev; 397 efx_nic_t *enp; 398 int error; 399 char rss_param_name[sizeof(SFXGE_PARAM(%d.max_rss_channels))]; 400 401 dev = sc->dev; 402 403 sx_init(&sc->softc_lock, "sfxge_softc"); 404 405 sc->max_rss_channels = 0; 406 snprintf(rss_param_name, sizeof(rss_param_name), 407 SFXGE_PARAM(%d.max_rss_channels), 408 (int)device_get_unit(dev)); 409 TUNABLE_INT_FETCH(rss_param_name, &sc->max_rss_channels); 410 411 sc->stats_node = SYSCTL_ADD_NODE( 412 device_get_sysctl_ctx(dev), 413 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 414 OID_AUTO, "stats", CTLFLAG_RD, NULL, "Statistics"); 415 if (sc->stats_node == NULL) { 416 error = ENOMEM; 417 goto fail; 418 } 419 420 TASK_INIT(&sc->task_reset, 0, sfxge_reset, sc); 421 422 (void) pci_enable_busmaster(dev); 423 424 /* Initialize DMA mappings. */ 425 if ((error = sfxge_dma_init(sc)) != 0) 426 goto fail; 427 428 /* Map the device registers. */ 429 if ((error = sfxge_bar_init(sc)) != 0) 430 goto fail; 431 432 error = efx_family(pci_get_vendor(dev), pci_get_device(dev), 433 &sc->family); 434 KASSERT(error == 0, ("Family should be filtered by sfxge_probe()")); 435 436 /* Create the common code nic object. */ 437 mtx_init(&sc->enp_lock, "sfxge_nic", NULL, MTX_DEF); 438 if ((error = efx_nic_create(sc->family, (efsys_identifier_t *)sc, 439 &sc->bar, &sc->enp_lock, &enp)) != 0) 440 goto fail3; 441 sc->enp = enp; 442 443 if (!ISP2(sfxge_rx_ring_entries) || 444 !(sfxge_rx_ring_entries & EFX_RXQ_NDESCS_MASK)) { 445 log(LOG_ERR, "%s=%d must be power of 2 from %u to %u", 446 SFXGE_PARAM_RX_RING, sfxge_rx_ring_entries, 447 EFX_RXQ_MINNDESCS, EFX_RXQ_MAXNDESCS); 448 error = EINVAL; 449 goto fail_rx_ring_entries; 450 } 451 sc->rxq_entries = sfxge_rx_ring_entries; 452 453 if (!ISP2(sfxge_tx_ring_entries) || 454 !(sfxge_tx_ring_entries & EFX_TXQ_NDESCS_MASK)) { 455 log(LOG_ERR, "%s=%d must be power of 2 from %u to %u", 456 SFXGE_PARAM_TX_RING, sfxge_tx_ring_entries, 457 EFX_TXQ_MINNDESCS, EFX_TXQ_MAXNDESCS); 458 error = EINVAL; 459 goto fail_tx_ring_entries; 460 } 461 sc->txq_entries = sfxge_tx_ring_entries; 462 463 /* Initialize MCDI to talk to the microcontroller. */ 464 if ((error = sfxge_mcdi_init(sc)) != 0) 465 goto fail4; 466 467 /* Probe the NIC and build the configuration data area. */ 468 if ((error = efx_nic_probe(enp)) != 0) 469 goto fail5; 470 471 /* Initialize the NVRAM. */ 472 if ((error = efx_nvram_init(enp)) != 0) 473 goto fail6; 474 475 /* Initialize the VPD. */ 476 if ((error = efx_vpd_init(enp)) != 0) 477 goto fail7; 478 479 /* Reset the NIC. */ 480 if ((error = efx_nic_reset(enp)) != 0) 481 goto fail8; 482 483 /* Initialize buffer table allocation. */ 484 sc->buffer_table_next = 0; 485 486 /* Set up interrupts. */ 487 if ((error = sfxge_intr_init(sc)) != 0) 488 goto fail8; 489 490 /* Initialize event processing state. */ 491 if ((error = sfxge_ev_init(sc)) != 0) 492 goto fail11; 493 494 /* Initialize receive state. */ 495 if ((error = sfxge_rx_init(sc)) != 0) 496 goto fail12; 497 498 /* Initialize transmit state. */ 499 if ((error = sfxge_tx_init(sc)) != 0) 500 goto fail13; 501 502 /* Initialize port state. */ 503 if ((error = sfxge_port_init(sc)) != 0) 504 goto fail14; 505 506 sc->init_state = SFXGE_INITIALIZED; 507 508 return (0); 509 510fail14: 511 sfxge_tx_fini(sc); 512 513fail13: 514 sfxge_rx_fini(sc); 515 516fail12: 517 sfxge_ev_fini(sc); 518 519fail11: 520 sfxge_intr_fini(sc); 521 522fail8: 523 efx_vpd_fini(enp); 524 525fail7: 526 efx_nvram_fini(enp); 527 528fail6: 529 efx_nic_unprobe(enp); 530 531fail5: 532 sfxge_mcdi_fini(sc); 533 534fail4: 535fail_tx_ring_entries: 536fail_rx_ring_entries: 537 sc->enp = NULL; 538 efx_nic_destroy(enp); 539 mtx_destroy(&sc->enp_lock); 540 541fail3: 542 sfxge_bar_fini(sc); 543 (void) pci_disable_busmaster(sc->dev); 544 545fail: 546 sc->dev = NULL; 547 sx_destroy(&sc->softc_lock); 548 return (error); 549} 550 551static void 552sfxge_destroy(struct sfxge_softc *sc) 553{ 554 efx_nic_t *enp; 555 556 /* Clean up port state. */ 557 sfxge_port_fini(sc); 558 559 /* Clean up transmit state. */ 560 sfxge_tx_fini(sc); 561 562 /* Clean up receive state. */ 563 sfxge_rx_fini(sc); 564 565 /* Clean up event processing state. */ 566 sfxge_ev_fini(sc); 567 568 /* Clean up interrupts. */ 569 sfxge_intr_fini(sc); 570 571 /* Tear down common code subsystems. */ 572 efx_nic_reset(sc->enp); 573 efx_vpd_fini(sc->enp); 574 efx_nvram_fini(sc->enp); 575 efx_nic_unprobe(sc->enp); 576 577 /* Tear down MCDI. */ 578 sfxge_mcdi_fini(sc); 579 580 /* Destroy common code context. */ 581 enp = sc->enp; 582 sc->enp = NULL; 583 efx_nic_destroy(enp); 584 585 /* Free DMA memory. */ 586 sfxge_dma_fini(sc); 587 588 /* Free mapped BARs. */ 589 sfxge_bar_fini(sc); 590 591 (void) pci_disable_busmaster(sc->dev); 592 593 taskqueue_drain(taskqueue_thread, &sc->task_reset); 594 595 /* Destroy the softc lock. */ 596 sx_destroy(&sc->softc_lock); 597} 598 599static int 600sfxge_vpd_handler(SYSCTL_HANDLER_ARGS) 601{ 602 struct sfxge_softc *sc = arg1; 603 efx_vpd_value_t value; 604 int rc; 605 606 value.evv_tag = arg2 >> 16; 607 value.evv_keyword = arg2 & 0xffff; 608 if ((rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value)) 609 != 0) 610 return (rc); 611 612 return (SYSCTL_OUT(req, value.evv_value, value.evv_length)); 613} 614 615static void 616sfxge_vpd_try_add(struct sfxge_softc *sc, struct sysctl_oid_list *list, 617 efx_vpd_tag_t tag, const char *keyword) 618{ 619 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev); 620 efx_vpd_value_t value; 621 622 /* Check whether VPD tag/keyword is present */ 623 value.evv_tag = tag; 624 value.evv_keyword = EFX_VPD_KEYWORD(keyword[0], keyword[1]); 625 if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) != 0) 626 return; 627 628 SYSCTL_ADD_PROC( 629 ctx, list, OID_AUTO, keyword, CTLTYPE_STRING|CTLFLAG_RD, 630 sc, tag << 16 | EFX_VPD_KEYWORD(keyword[0], keyword[1]), 631 sfxge_vpd_handler, "A", ""); 632} 633 634static int 635sfxge_vpd_init(struct sfxge_softc *sc) 636{ 637 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev); 638 struct sysctl_oid *vpd_node; 639 struct sysctl_oid_list *vpd_list; 640 char keyword[3]; 641 efx_vpd_value_t value; 642 int rc; 643 644 if ((rc = efx_vpd_size(sc->enp, &sc->vpd_size)) != 0) 645 goto fail; 646 sc->vpd_data = malloc(sc->vpd_size, M_SFXGE, M_WAITOK); 647 if ((rc = efx_vpd_read(sc->enp, sc->vpd_data, sc->vpd_size)) != 0) 648 goto fail2; 649 650 /* Copy ID (product name) into device description, and log it. */ 651 value.evv_tag = EFX_VPD_ID; 652 if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) == 0) { 653 value.evv_value[value.evv_length] = 0; 654 device_set_desc_copy(sc->dev, value.evv_value); 655 device_printf(sc->dev, "%s\n", value.evv_value); 656 } 657 658 vpd_node = SYSCTL_ADD_NODE( 659 ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), 660 OID_AUTO, "vpd", CTLFLAG_RD, NULL, "Vital Product Data"); 661 vpd_list = SYSCTL_CHILDREN(vpd_node); 662 663 /* Add sysctls for all expected and any vendor-defined keywords. */ 664 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "PN"); 665 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "EC"); 666 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "SN"); 667 keyword[0] = 'V'; 668 keyword[2] = 0; 669 for (keyword[1] = '0'; keyword[1] <= '9'; keyword[1]++) 670 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword); 671 for (keyword[1] = 'A'; keyword[1] <= 'Z'; keyword[1]++) 672 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword); 673 674 return (0); 675 676fail2: 677 free(sc->vpd_data, M_SFXGE); 678fail: 679 return (rc); 680} 681 682static void 683sfxge_vpd_fini(struct sfxge_softc *sc) 684{ 685 free(sc->vpd_data, M_SFXGE); 686} 687 688static void 689sfxge_reset(void *arg, int npending) 690{ 691 struct sfxge_softc *sc; 692 int rc; 693 694 (void)npending; 695 696 sc = (struct sfxge_softc *)arg; 697 698 sx_xlock(&sc->softc_lock); 699 700 if (sc->init_state != SFXGE_STARTED) 701 goto done; 702 703 sfxge_stop(sc); 704 efx_nic_reset(sc->enp); 705 if ((rc = sfxge_start(sc)) != 0) 706 device_printf(sc->dev, 707 "reset failed (%d); interface is now stopped\n", 708 rc); 709 710done: 711 sx_xunlock(&sc->softc_lock); 712} 713 714void 715sfxge_schedule_reset(struct sfxge_softc *sc) 716{ 717 taskqueue_enqueue(taskqueue_thread, &sc->task_reset); 718} 719 720static int 721sfxge_attach(device_t dev) 722{ 723 struct sfxge_softc *sc; 724 struct ifnet *ifp; 725 int error; 726 727 sc = device_get_softc(dev); 728 sc->dev = dev; 729 730 /* Allocate ifnet. */ 731 ifp = if_alloc(IFT_ETHER); 732 if (ifp == NULL) { 733 device_printf(dev, "Couldn't allocate ifnet\n"); 734 error = ENOMEM; 735 goto fail; 736 } 737 sc->ifnet = ifp; 738 739 /* Initialize hardware. */ 740 if ((error = sfxge_create(sc)) != 0) 741 goto fail2; 742 743 /* Create the ifnet for the port. */ 744 if ((error = sfxge_ifnet_init(ifp, sc)) != 0) 745 goto fail3; 746 747 if ((error = sfxge_vpd_init(sc)) != 0) 748 goto fail4; 749 750 sc->init_state = SFXGE_REGISTERED; 751 752 return (0); 753 754fail4: 755 sfxge_ifnet_fini(ifp); 756fail3: 757 sfxge_destroy(sc); 758 759fail2: 760 if_free(sc->ifnet); 761 762fail: 763 return (error); 764} 765 766static int 767sfxge_detach(device_t dev) 768{ 769 struct sfxge_softc *sc; 770 771 sc = device_get_softc(dev); 772 773 sfxge_vpd_fini(sc); 774 775 /* Destroy the ifnet. */ 776 sfxge_ifnet_fini(sc->ifnet); 777 778 /* Tear down hardware. */ 779 sfxge_destroy(sc); 780 781 return (0); 782} 783 784static int 785sfxge_probe(device_t dev) 786{ 787 uint16_t pci_vendor_id; 788 uint16_t pci_device_id; 789 efx_family_t family; 790 int rc; 791 792 pci_vendor_id = pci_get_vendor(dev); 793 pci_device_id = pci_get_device(dev); 794 795 rc = efx_family(pci_vendor_id, pci_device_id, &family); 796 if (rc != 0) 797 return (ENXIO); 798 799 KASSERT(family == EFX_FAMILY_SIENA, ("impossible controller family")); 800 device_set_desc(dev, "Solarflare SFC9000 family"); 801 return (0); 802} 803 804static device_method_t sfxge_methods[] = { 805 DEVMETHOD(device_probe, sfxge_probe), 806 DEVMETHOD(device_attach, sfxge_attach), 807 DEVMETHOD(device_detach, sfxge_detach), 808 809 DEVMETHOD_END 810}; 811 812static devclass_t sfxge_devclass; 813 814static driver_t sfxge_driver = { 815 "sfxge", 816 sfxge_methods, 817 sizeof(struct sfxge_softc) 818}; 819 820DRIVER_MODULE(sfxge, pci, sfxge_driver, sfxge_devclass, 0, 0); 821