1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2010-2016 Solarflare Communications Inc. 5 * All rights reserved. 6 * 7 * This software was developed in part by Philip Paeps under contract for 8 * Solarflare Communications, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright notice, 14 * this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 29 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 * The views and conclusions contained in the software and documentation are 32 * those of the authors and should not be interpreted as representing official 33 * policies, either expressed or implied, of the FreeBSD Project. 34 */ 35 36 #include <sys/cdefs.h> 37 __FBSDID("$FreeBSD$"); 38 39 #include "opt_rss.h" 40 41 #include <sys/param.h> 42 #include <sys/kernel.h> 43 #include <sys/bus.h> 44 #include <sys/rman.h> 45 #include <sys/lock.h> 46 #include <sys/module.h> 47 #include <sys/mutex.h> 48 #include <sys/smp.h> 49 #include <sys/socket.h> 50 #include <sys/taskqueue.h> 51 #include <sys/sockio.h> 52 #include <sys/sysctl.h> 53 #include <sys/priv.h> 54 #include <sys/syslog.h> 55 56 #include <dev/pci/pcireg.h> 57 #include <dev/pci/pcivar.h> 58 59 #include <net/ethernet.h> 60 #include <net/if.h> 61 #include <net/if_var.h> 62 #include <net/if_media.h> 63 #include <net/if_types.h> 64 65 #ifdef RSS 66 #include <net/rss_config.h> 67 #endif 68 69 #include "common/efx.h" 70 71 #include "sfxge.h" 72 #include "sfxge_rx.h" 73 #include "sfxge_ioc.h" 74 #include "sfxge_version.h" 75 76 #define SFXGE_CAP (IFCAP_VLAN_MTU | IFCAP_VLAN_HWCSUM | \ 77 IFCAP_RXCSUM | IFCAP_TXCSUM | \ 78 IFCAP_RXCSUM_IPV6 | IFCAP_TXCSUM_IPV6 | \ 79 IFCAP_TSO4 | IFCAP_TSO6 | \ 80 IFCAP_JUMBO_MTU | \ 81 IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE | IFCAP_HWSTATS) 82 #define SFXGE_CAP_ENABLE SFXGE_CAP 83 #define SFXGE_CAP_FIXED (IFCAP_VLAN_MTU | \ 84 IFCAP_JUMBO_MTU | IFCAP_LINKSTATE | IFCAP_HWSTATS) 85 86 MALLOC_DEFINE(M_SFXGE, "sfxge", "Solarflare 10GigE driver"); 87 88 SYSCTL_NODE(_hw, OID_AUTO, sfxge, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 89 "SFXGE driver parameters"); 90 91 #define SFXGE_PARAM_RX_RING SFXGE_PARAM(rx_ring) 92 static int sfxge_rx_ring_entries = SFXGE_NDESCS; 93 TUNABLE_INT(SFXGE_PARAM_RX_RING, &sfxge_rx_ring_entries); 94 SYSCTL_INT(_hw_sfxge, OID_AUTO, rx_ring, CTLFLAG_RDTUN, 95 &sfxge_rx_ring_entries, 0, 96 "Maximum number of descriptors in a receive ring"); 97 98 #define SFXGE_PARAM_TX_RING SFXGE_PARAM(tx_ring) 99 static int sfxge_tx_ring_entries = SFXGE_NDESCS; 100 TUNABLE_INT(SFXGE_PARAM_TX_RING, &sfxge_tx_ring_entries); 101 SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_ring, CTLFLAG_RDTUN, 102 &sfxge_tx_ring_entries, 0, 103 "Maximum number of descriptors in a transmit ring"); 104 105 #define SFXGE_PARAM_RESTART_ATTEMPTS SFXGE_PARAM(restart_attempts) 106 static int sfxge_restart_attempts = 3; 107 TUNABLE_INT(SFXGE_PARAM_RESTART_ATTEMPTS, &sfxge_restart_attempts); 108 SYSCTL_INT(_hw_sfxge, OID_AUTO, restart_attempts, CTLFLAG_RDTUN, 109 &sfxge_restart_attempts, 0, 110 "Maximum number of attempts to bring interface up after reset"); 111 112 #if EFSYS_OPT_MCDI_LOGGING 113 #define SFXGE_PARAM_MCDI_LOGGING SFXGE_PARAM(mcdi_logging) 114 static int sfxge_mcdi_logging = 0; 115 TUNABLE_INT(SFXGE_PARAM_MCDI_LOGGING, &sfxge_mcdi_logging); 116 #endif 117 118 static void 119 sfxge_reset(void *arg, int npending); 120 121 static int 122 sfxge_estimate_rsrc_limits(struct sfxge_softc *sc) 123 { 124 efx_drv_limits_t limits; 125 int rc; 126 unsigned int evq_max; 127 uint32_t evq_allocated; 128 uint32_t rxq_allocated; 129 uint32_t txq_allocated; 130 131 /* 132 * Limit the number of event queues to: 133 * - number of CPUs 134 * - hardwire maximum RSS channels 135 * - administratively specified maximum RSS channels 136 */ 137 #ifdef RSS 138 /* 139 * Avoid extra limitations so that the number of queues 140 * may be configured at administrator's will 141 */ 142 evq_max = MIN(MAX(rss_getnumbuckets(), 1), EFX_MAXRSS); 143 #else 144 evq_max = MIN(mp_ncpus, EFX_MAXRSS); 145 #endif 146 if (sc->max_rss_channels > 0) 147 evq_max = MIN(evq_max, sc->max_rss_channels); 148 149 memset(&limits, 0, sizeof(limits)); 150 151 limits.edl_min_evq_count = 1; 152 limits.edl_max_evq_count = evq_max; 153 limits.edl_min_txq_count = SFXGE_EVQ0_N_TXQ(sc); 154 limits.edl_max_txq_count = evq_max + SFXGE_EVQ0_N_TXQ(sc) - 1; 155 limits.edl_min_rxq_count = 1; 156 limits.edl_max_rxq_count = evq_max; 157 158 efx_nic_set_drv_limits(sc->enp, &limits); 159 160 if ((rc = efx_nic_init(sc->enp)) != 0) 161 return (rc); 162 163 rc = efx_nic_get_vi_pool(sc->enp, &evq_allocated, &rxq_allocated, 164 &txq_allocated); 165 if (rc != 0) { 166 efx_nic_fini(sc->enp); 167 return (rc); 168 } 169 170 KASSERT(txq_allocated >= SFXGE_EVQ0_N_TXQ(sc), 171 ("txq_allocated < %u", SFXGE_EVQ0_N_TXQ(sc))); 172 173 sc->evq_max = MIN(evq_allocated, evq_max); 174 sc->evq_max = MIN(rxq_allocated, sc->evq_max); 175 sc->evq_max = MIN(txq_allocated - (SFXGE_EVQ0_N_TXQ(sc) - 1), 176 sc->evq_max); 177 178 KASSERT(sc->evq_max <= evq_max, 179 ("allocated more than maximum requested")); 180 181 #ifdef RSS 182 if (sc->evq_max < rss_getnumbuckets()) 183 device_printf(sc->dev, "The number of allocated queues (%u) " 184 "is less than the number of RSS buckets (%u); " 185 "performance degradation might be observed", 186 sc->evq_max, rss_getnumbuckets()); 187 #endif 188 189 /* 190 * NIC is kept initialized in the case of success to be able to 191 * initialize port to find out media types. 192 */ 193 return (0); 194 } 195 196 static int 197 sfxge_set_drv_limits(struct sfxge_softc *sc) 198 { 199 efx_drv_limits_t limits; 200 201 memset(&limits, 0, sizeof(limits)); 202 203 /* Limits are strict since take into account initial estimation */ 204 limits.edl_min_evq_count = limits.edl_max_evq_count = 205 sc->intr.n_alloc; 206 limits.edl_min_txq_count = limits.edl_max_txq_count = 207 sc->intr.n_alloc + SFXGE_EVQ0_N_TXQ(sc) - 1; 208 limits.edl_min_rxq_count = limits.edl_max_rxq_count = 209 sc->intr.n_alloc; 210 211 return (efx_nic_set_drv_limits(sc->enp, &limits)); 212 } 213 214 static int 215 sfxge_start(struct sfxge_softc *sc) 216 { 217 int rc; 218 219 SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc); 220 221 if (sc->init_state == SFXGE_STARTED) 222 return (0); 223 224 if (sc->init_state != SFXGE_REGISTERED) { 225 rc = EINVAL; 226 goto fail; 227 } 228 229 /* Set required resource limits */ 230 if ((rc = sfxge_set_drv_limits(sc)) != 0) 231 goto fail; 232 233 if ((rc = efx_nic_init(sc->enp)) != 0) 234 goto fail; 235 236 /* Start processing interrupts. */ 237 if ((rc = sfxge_intr_start(sc)) != 0) 238 goto fail2; 239 240 /* Start processing events. */ 241 if ((rc = sfxge_ev_start(sc)) != 0) 242 goto fail3; 243 244 /* Fire up the port. */ 245 if ((rc = sfxge_port_start(sc)) != 0) 246 goto fail4; 247 248 /* Start the receiver side. */ 249 if ((rc = sfxge_rx_start(sc)) != 0) 250 goto fail5; 251 252 /* Start the transmitter side. */ 253 if ((rc = sfxge_tx_start(sc)) != 0) 254 goto fail6; 255 256 sc->init_state = SFXGE_STARTED; 257 258 /* Tell the stack we're running. */ 259 if_setdrvflagbits(sc->ifnet, IFF_DRV_RUNNING, IFF_DRV_OACTIVE); 260 261 return (0); 262 263 fail6: 264 sfxge_rx_stop(sc); 265 266 fail5: 267 sfxge_port_stop(sc); 268 269 fail4: 270 sfxge_ev_stop(sc); 271 272 fail3: 273 sfxge_intr_stop(sc); 274 275 fail2: 276 efx_nic_fini(sc->enp); 277 278 fail: 279 device_printf(sc->dev, "sfxge_start: %d\n", rc); 280 281 return (rc); 282 } 283 284 static void 285 sfxge_if_init(void *arg) 286 { 287 struct sfxge_softc *sc; 288 289 sc = (struct sfxge_softc *)arg; 290 291 SFXGE_ADAPTER_LOCK(sc); 292 (void)sfxge_start(sc); 293 SFXGE_ADAPTER_UNLOCK(sc); 294 } 295 296 static void 297 sfxge_stop(struct sfxge_softc *sc) 298 { 299 SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc); 300 301 if (sc->init_state != SFXGE_STARTED) 302 return; 303 304 sc->init_state = SFXGE_REGISTERED; 305 306 /* Stop the transmitter. */ 307 sfxge_tx_stop(sc); 308 309 /* Stop the receiver. */ 310 sfxge_rx_stop(sc); 311 312 /* Stop the port. */ 313 sfxge_port_stop(sc); 314 315 /* Stop processing events. */ 316 sfxge_ev_stop(sc); 317 318 /* Stop processing interrupts. */ 319 sfxge_intr_stop(sc); 320 321 efx_nic_fini(sc->enp); 322 323 if_setdrvflagbits(sc->ifnet, 0, IFF_DRV_RUNNING); 324 } 325 326 static int 327 sfxge_vpd_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ioc) 328 { 329 efx_vpd_value_t value; 330 int rc = 0; 331 332 switch (ioc->u.vpd.op) { 333 case SFXGE_VPD_OP_GET_KEYWORD: 334 value.evv_tag = ioc->u.vpd.tag; 335 value.evv_keyword = ioc->u.vpd.keyword; 336 rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value); 337 if (rc != 0) 338 break; 339 ioc->u.vpd.len = MIN(ioc->u.vpd.len, value.evv_length); 340 if (ioc->u.vpd.payload != 0) { 341 rc = copyout(value.evv_value, ioc->u.vpd.payload, 342 ioc->u.vpd.len); 343 } 344 break; 345 case SFXGE_VPD_OP_SET_KEYWORD: 346 if (ioc->u.vpd.len > sizeof(value.evv_value)) 347 return (EINVAL); 348 value.evv_tag = ioc->u.vpd.tag; 349 value.evv_keyword = ioc->u.vpd.keyword; 350 value.evv_length = ioc->u.vpd.len; 351 rc = copyin(ioc->u.vpd.payload, value.evv_value, value.evv_length); 352 if (rc != 0) 353 break; 354 rc = efx_vpd_set(sc->enp, sc->vpd_data, sc->vpd_size, &value); 355 if (rc != 0) 356 break; 357 rc = efx_vpd_verify(sc->enp, sc->vpd_data, sc->vpd_size); 358 if (rc != 0) 359 break; 360 rc = efx_vpd_write(sc->enp, sc->vpd_data, sc->vpd_size); 361 break; 362 default: 363 rc = EOPNOTSUPP; 364 break; 365 } 366 367 return (rc); 368 } 369 370 static int 371 sfxge_private_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ioc) 372 { 373 switch (ioc->op) { 374 case SFXGE_MCDI_IOC: 375 return (sfxge_mcdi_ioctl(sc, ioc)); 376 case SFXGE_NVRAM_IOC: 377 return (sfxge_nvram_ioctl(sc, ioc)); 378 case SFXGE_VPD_IOC: 379 return (sfxge_vpd_ioctl(sc, ioc)); 380 default: 381 return (EOPNOTSUPP); 382 } 383 } 384 385 static int 386 sfxge_if_ioctl(if_t ifp, unsigned long command, caddr_t data) 387 { 388 struct sfxge_softc *sc; 389 struct ifreq *ifr; 390 sfxge_ioc_t ioc; 391 int error; 392 393 ifr = (struct ifreq *)data; 394 sc = if_getsoftc(ifp); 395 error = 0; 396 397 switch (command) { 398 case SIOCSIFFLAGS: 399 SFXGE_ADAPTER_LOCK(sc); 400 if (if_getflags(ifp) & IFF_UP) { 401 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) { 402 if ((if_getflags(ifp) ^ sc->if_flags) & 403 (IFF_PROMISC | IFF_ALLMULTI)) { 404 sfxge_mac_filter_set(sc); 405 } 406 } else 407 sfxge_start(sc); 408 } else 409 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) 410 sfxge_stop(sc); 411 sc->if_flags = if_getflags(ifp); 412 SFXGE_ADAPTER_UNLOCK(sc); 413 break; 414 case SIOCSIFMTU: 415 if (ifr->ifr_mtu == if_getmtu(ifp)) { 416 /* Nothing to do */ 417 error = 0; 418 } else if (ifr->ifr_mtu > SFXGE_MAX_MTU) { 419 error = EINVAL; 420 } else if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) { 421 if_setmtu(ifp, ifr->ifr_mtu); 422 error = 0; 423 } else { 424 /* Restart required */ 425 SFXGE_ADAPTER_LOCK(sc); 426 sfxge_stop(sc); 427 if_setmtu(ifp, ifr->ifr_mtu); 428 error = sfxge_start(sc); 429 SFXGE_ADAPTER_UNLOCK(sc); 430 if (error != 0) { 431 if_setflagbits(ifp, 0, IFF_UP); 432 if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING); 433 if_down(ifp); 434 } 435 } 436 break; 437 case SIOCADDMULTI: 438 case SIOCDELMULTI: 439 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) 440 sfxge_mac_filter_set(sc); 441 break; 442 case SIOCSIFCAP: 443 { 444 int reqcap = ifr->ifr_reqcap; 445 int capchg_mask; 446 447 SFXGE_ADAPTER_LOCK(sc); 448 449 /* Capabilities to be changed in accordance with request */ 450 capchg_mask = if_getcapenable(ifp) ^ reqcap; 451 452 /* 453 * The networking core already rejects attempts to 454 * enable capabilities we don't have. We still have 455 * to reject attempts to disable capabilities that we 456 * can't (yet) disable. 457 */ 458 KASSERT((reqcap & ~if_getcapabilities(ifp)) == 0, 459 ("Unsupported capabilities 0x%x requested 0x%x vs " 460 "supported 0x%x", 461 reqcap & ~if_getcapabilities(ifp), 462 reqcap , if_getcapabilities(ifp))); 463 if (capchg_mask & SFXGE_CAP_FIXED) { 464 error = EINVAL; 465 SFXGE_ADAPTER_UNLOCK(sc); 466 break; 467 } 468 469 /* Check request before any changes */ 470 if ((capchg_mask & IFCAP_TSO4) && 471 (reqcap & (IFCAP_TSO4 | IFCAP_TXCSUM)) == IFCAP_TSO4) { 472 error = EAGAIN; 473 SFXGE_ADAPTER_UNLOCK(sc); 474 if_printf(ifp, "enable txcsum before tso4\n"); 475 break; 476 } 477 if ((capchg_mask & IFCAP_TSO6) && 478 (reqcap & (IFCAP_TSO6 | IFCAP_TXCSUM_IPV6)) == IFCAP_TSO6) { 479 error = EAGAIN; 480 SFXGE_ADAPTER_UNLOCK(sc); 481 if_printf(ifp, "enable txcsum6 before tso6\n"); 482 break; 483 } 484 485 if (reqcap & IFCAP_TXCSUM) { 486 if_sethwassistbits(ifp, (CSUM_IP | CSUM_TCP | CSUM_UDP), 0); 487 } else { 488 if_sethwassistbits(ifp, 0, (CSUM_IP | CSUM_TCP | CSUM_UDP)); 489 if (reqcap & IFCAP_TSO4) { 490 reqcap &= ~IFCAP_TSO4; 491 if_printf(ifp, 492 "tso4 disabled due to -txcsum\n"); 493 } 494 } 495 if (reqcap & IFCAP_TXCSUM_IPV6) { 496 if_sethwassistbits(ifp, (CSUM_TCP_IPV6 | CSUM_UDP_IPV6), 0); 497 } else { 498 if_sethwassistbits(ifp, 0, (CSUM_TCP_IPV6 | CSUM_UDP_IPV6)); 499 if (reqcap & IFCAP_TSO6) { 500 reqcap &= ~IFCAP_TSO6; 501 if_printf(ifp, 502 "tso6 disabled due to -txcsum6\n"); 503 } 504 } 505 506 /* 507 * The kernel takes both IFCAP_TSOx and CSUM_TSO into 508 * account before using TSO. So, we do not touch 509 * checksum flags when IFCAP_TSOx is modified. 510 * Note that CSUM_TSO is (CSUM_IP_TSO|CSUM_IP6_TSO), 511 * but both bits are set in IPv4 and IPv6 mbufs. 512 */ 513 514 if_setcapenable(ifp, reqcap); 515 516 SFXGE_ADAPTER_UNLOCK(sc); 517 break; 518 } 519 case SIOCSIFMEDIA: 520 case SIOCGIFMEDIA: 521 error = ifmedia_ioctl(ifp, ifr, &sc->media, command); 522 break; 523 #ifdef SIOCGI2C 524 case SIOCGI2C: 525 { 526 struct ifi2creq i2c; 527 528 error = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c)); 529 if (error != 0) 530 break; 531 532 if (i2c.len > sizeof(i2c.data)) { 533 error = EINVAL; 534 break; 535 } 536 537 SFXGE_ADAPTER_LOCK(sc); 538 error = efx_phy_module_get_info(sc->enp, i2c.dev_addr, 539 i2c.offset, i2c.len, 540 &i2c.data[0]); 541 SFXGE_ADAPTER_UNLOCK(sc); 542 if (error == 0) 543 error = copyout(&i2c, ifr_data_get_ptr(ifr), 544 sizeof(i2c)); 545 break; 546 } 547 #endif 548 case SIOCGPRIVATE_0: 549 error = priv_check(curthread, PRIV_DRIVER); 550 if (error != 0) 551 break; 552 error = copyin(ifr_data_get_ptr(ifr), &ioc, sizeof(ioc)); 553 if (error != 0) 554 return (error); 555 error = sfxge_private_ioctl(sc, &ioc); 556 if (error == 0) { 557 error = copyout(&ioc, ifr_data_get_ptr(ifr), 558 sizeof(ioc)); 559 } 560 break; 561 default: 562 error = ether_ioctl(ifp, command, data); 563 } 564 565 return (error); 566 } 567 568 static void 569 sfxge_ifnet_fini(if_t ifp) 570 { 571 struct sfxge_softc *sc = if_getsoftc(ifp); 572 573 SFXGE_ADAPTER_LOCK(sc); 574 sfxge_stop(sc); 575 SFXGE_ADAPTER_UNLOCK(sc); 576 577 ifmedia_removeall(&sc->media); 578 ether_ifdetach(ifp); 579 if_free(ifp); 580 } 581 582 static int 583 sfxge_ifnet_init(if_t ifp, struct sfxge_softc *sc) 584 { 585 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp); 586 device_t dev; 587 int rc; 588 589 dev = sc->dev; 590 sc->ifnet = ifp; 591 592 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 593 if_setinitfn(ifp, sfxge_if_init); 594 if_setsoftc(ifp, sc); 595 if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST); 596 if_setioctlfn(ifp, sfxge_if_ioctl); 597 598 if_setcapabilities(ifp, SFXGE_CAP); 599 if_setcapenable(ifp, SFXGE_CAP_ENABLE); 600 if_sethwtsomax(ifp, SFXGE_TSO_MAX_SIZE); 601 if_sethwtsomaxsegcount(ifp, SFXGE_TX_MAPPING_MAX_SEG); 602 if_sethwtsomaxsegsize(ifp, PAGE_SIZE); 603 604 #ifdef SFXGE_LRO 605 if_setcapabilitiesbit(ifp, IFCAP_LRO, 0); 606 if_setcapenablebit(ifp, IFCAP_LRO, 0); 607 #endif 608 609 if (encp->enc_hw_tx_insert_vlan_enabled) { 610 if_setcapabilitiesbit(ifp, IFCAP_VLAN_HWTAGGING, 0); 611 if_setcapenablebit(ifp, IFCAP_VLAN_HWTAGGING, 0); 612 } 613 if_sethwassistbits(ifp, CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO | 614 CSUM_TCP_IPV6 | CSUM_UDP_IPV6, 0); 615 616 ether_ifattach(ifp, encp->enc_mac_addr); 617 618 if_settransmitfn(ifp, sfxge_if_transmit); 619 if_setqflushfn(ifp, sfxge_if_qflush); 620 621 if_setgetcounterfn(ifp, sfxge_get_counter); 622 623 DBGPRINT(sc->dev, "ifmedia_init"); 624 if ((rc = sfxge_port_ifmedia_init(sc)) != 0) 625 goto fail; 626 627 return (0); 628 629 fail: 630 ether_ifdetach(sc->ifnet); 631 return (rc); 632 } 633 634 void 635 sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n, uint32_t *idp) 636 { 637 KASSERT(sc->buffer_table_next + n <= 638 efx_nic_cfg_get(sc->enp)->enc_buftbl_limit, 639 ("buffer table full")); 640 641 *idp = sc->buffer_table_next; 642 sc->buffer_table_next += n; 643 } 644 645 static int 646 sfxge_bar_init(struct sfxge_softc *sc) 647 { 648 efsys_bar_t *esbp = &sc->bar; 649 650 esbp->esb_rid = PCIR_BAR(sc->mem_bar); 651 if ((esbp->esb_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY, 652 &esbp->esb_rid, RF_ACTIVE)) == NULL) { 653 device_printf(sc->dev, "Cannot allocate BAR region %d\n", 654 sc->mem_bar); 655 return (ENXIO); 656 } 657 esbp->esb_tag = rman_get_bustag(esbp->esb_res); 658 esbp->esb_handle = rman_get_bushandle(esbp->esb_res); 659 660 SFXGE_BAR_LOCK_INIT(esbp, device_get_nameunit(sc->dev)); 661 662 return (0); 663 } 664 665 static void 666 sfxge_bar_fini(struct sfxge_softc *sc) 667 { 668 efsys_bar_t *esbp = &sc->bar; 669 670 bus_release_resource(sc->dev, SYS_RES_MEMORY, esbp->esb_rid, 671 esbp->esb_res); 672 SFXGE_BAR_LOCK_DESTROY(esbp); 673 } 674 675 static int 676 sfxge_create(struct sfxge_softc *sc) 677 { 678 device_t dev; 679 efx_nic_t *enp; 680 int error; 681 char rss_param_name[sizeof(SFXGE_PARAM(%d.max_rss_channels))]; 682 #if EFSYS_OPT_MCDI_LOGGING 683 char mcdi_log_param_name[sizeof(SFXGE_PARAM(%d.mcdi_logging))]; 684 #endif 685 686 dev = sc->dev; 687 688 SFXGE_ADAPTER_LOCK_INIT(sc, device_get_nameunit(sc->dev)); 689 690 sc->max_rss_channels = 0; 691 snprintf(rss_param_name, sizeof(rss_param_name), 692 SFXGE_PARAM(%d.max_rss_channels), 693 (int)device_get_unit(dev)); 694 TUNABLE_INT_FETCH(rss_param_name, &sc->max_rss_channels); 695 #if EFSYS_OPT_MCDI_LOGGING 696 sc->mcdi_logging = sfxge_mcdi_logging; 697 snprintf(mcdi_log_param_name, sizeof(mcdi_log_param_name), 698 SFXGE_PARAM(%d.mcdi_logging), 699 (int)device_get_unit(dev)); 700 TUNABLE_INT_FETCH(mcdi_log_param_name, &sc->mcdi_logging); 701 #endif 702 703 sc->stats_node = SYSCTL_ADD_NODE(device_get_sysctl_ctx(dev), 704 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "stats", 705 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Statistics"); 706 if (sc->stats_node == NULL) { 707 error = ENOMEM; 708 goto fail; 709 } 710 711 TASK_INIT(&sc->task_reset, 0, sfxge_reset, sc); 712 713 (void) pci_enable_busmaster(dev); 714 715 /* Initialize DMA mappings. */ 716 DBGPRINT(sc->dev, "dma_init..."); 717 if ((error = sfxge_dma_init(sc)) != 0) 718 goto fail; 719 720 error = efx_family(pci_get_vendor(dev), pci_get_device(dev), 721 &sc->family, &sc->mem_bar); 722 KASSERT(error == 0, ("Family should be filtered by sfxge_probe()")); 723 724 /* Map the device registers. */ 725 DBGPRINT(sc->dev, "bar_init..."); 726 if ((error = sfxge_bar_init(sc)) != 0) 727 goto fail; 728 729 DBGPRINT(sc->dev, "nic_create..."); 730 731 /* Create the common code nic object. */ 732 SFXGE_EFSYS_LOCK_INIT(&sc->enp_lock, 733 device_get_nameunit(sc->dev), "nic"); 734 if ((error = efx_nic_create(sc->family, (efsys_identifier_t *)sc, 735 &sc->bar, &sc->enp_lock, &enp)) != 0) 736 goto fail3; 737 sc->enp = enp; 738 739 /* Initialize MCDI to talk to the microcontroller. */ 740 DBGPRINT(sc->dev, "mcdi_init..."); 741 if ((error = sfxge_mcdi_init(sc)) != 0) 742 goto fail4; 743 744 /* Probe the NIC and build the configuration data area. */ 745 DBGPRINT(sc->dev, "nic_probe..."); 746 if ((error = efx_nic_probe(enp, EFX_FW_VARIANT_DONT_CARE)) != 0) 747 goto fail5; 748 749 if (!ISP2(sfxge_rx_ring_entries) || 750 (sfxge_rx_ring_entries < EFX_RXQ_MINNDESCS) || 751 (sfxge_rx_ring_entries > EFX_RXQ_MAXNDESCS)) { 752 log(LOG_ERR, "%s=%d must be power of 2 from %u to %u", 753 SFXGE_PARAM_RX_RING, sfxge_rx_ring_entries, 754 EFX_RXQ_MINNDESCS, EFX_RXQ_MAXNDESCS); 755 error = EINVAL; 756 goto fail_rx_ring_entries; 757 } 758 sc->rxq_entries = sfxge_rx_ring_entries; 759 760 if (efx_nic_cfg_get(enp)->enc_features & EFX_FEATURE_TXQ_CKSUM_OP_DESC) 761 sc->txq_dynamic_cksum_toggle_supported = B_TRUE; 762 else 763 sc->txq_dynamic_cksum_toggle_supported = B_FALSE; 764 765 if (!ISP2(sfxge_tx_ring_entries) || 766 (sfxge_tx_ring_entries < EFX_TXQ_MINNDESCS) || 767 (sfxge_tx_ring_entries > efx_nic_cfg_get(enp)->enc_txq_max_ndescs)) { 768 log(LOG_ERR, "%s=%d must be power of 2 from %u to %u", 769 SFXGE_PARAM_TX_RING, sfxge_tx_ring_entries, 770 EFX_TXQ_MINNDESCS, efx_nic_cfg_get(enp)->enc_txq_max_ndescs); 771 error = EINVAL; 772 goto fail_tx_ring_entries; 773 } 774 sc->txq_entries = sfxge_tx_ring_entries; 775 776 SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev), 777 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 778 OID_AUTO, "version", CTLFLAG_RD, 779 SFXGE_VERSION_STRING, 0, 780 "Driver version"); 781 782 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev), 783 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 784 OID_AUTO, "phy_type", CTLFLAG_RD, 785 NULL, efx_nic_cfg_get(enp)->enc_phy_type, 786 "PHY type"); 787 788 /* Initialize the NVRAM. */ 789 DBGPRINT(sc->dev, "nvram_init..."); 790 if ((error = efx_nvram_init(enp)) != 0) 791 goto fail6; 792 793 /* Initialize the VPD. */ 794 DBGPRINT(sc->dev, "vpd_init..."); 795 if ((error = efx_vpd_init(enp)) != 0) 796 goto fail7; 797 798 efx_mcdi_new_epoch(enp); 799 800 /* Reset the NIC. */ 801 DBGPRINT(sc->dev, "nic_reset..."); 802 if ((error = efx_nic_reset(enp)) != 0) 803 goto fail8; 804 805 /* Initialize buffer table allocation. */ 806 sc->buffer_table_next = 0; 807 808 /* 809 * Guarantee minimum and estimate maximum number of event queues 810 * to take it into account when MSI-X interrupts are allocated. 811 * It initializes NIC and keeps it initialized on success. 812 */ 813 if ((error = sfxge_estimate_rsrc_limits(sc)) != 0) 814 goto fail8; 815 816 /* Set up interrupts. */ 817 DBGPRINT(sc->dev, "intr_init..."); 818 if ((error = sfxge_intr_init(sc)) != 0) 819 goto fail9; 820 821 /* Initialize event processing state. */ 822 DBGPRINT(sc->dev, "ev_init..."); 823 if ((error = sfxge_ev_init(sc)) != 0) 824 goto fail11; 825 826 /* Initialize port state. */ 827 DBGPRINT(sc->dev, "port_init..."); 828 if ((error = sfxge_port_init(sc)) != 0) 829 goto fail12; 830 831 /* Initialize receive state. */ 832 DBGPRINT(sc->dev, "rx_init..."); 833 if ((error = sfxge_rx_init(sc)) != 0) 834 goto fail13; 835 836 /* Initialize transmit state. */ 837 DBGPRINT(sc->dev, "tx_init..."); 838 if ((error = sfxge_tx_init(sc)) != 0) 839 goto fail14; 840 841 sc->init_state = SFXGE_INITIALIZED; 842 843 DBGPRINT(sc->dev, "success"); 844 return (0); 845 846 fail14: 847 sfxge_rx_fini(sc); 848 849 fail13: 850 sfxge_port_fini(sc); 851 852 fail12: 853 sfxge_ev_fini(sc); 854 855 fail11: 856 sfxge_intr_fini(sc); 857 858 fail9: 859 efx_nic_fini(sc->enp); 860 861 fail8: 862 efx_vpd_fini(enp); 863 864 fail7: 865 efx_nvram_fini(enp); 866 867 fail6: 868 fail_tx_ring_entries: 869 fail_rx_ring_entries: 870 efx_nic_unprobe(enp); 871 872 fail5: 873 sfxge_mcdi_fini(sc); 874 875 fail4: 876 sc->enp = NULL; 877 efx_nic_destroy(enp); 878 SFXGE_EFSYS_LOCK_DESTROY(&sc->enp_lock); 879 880 fail3: 881 sfxge_bar_fini(sc); 882 (void) pci_disable_busmaster(sc->dev); 883 884 fail: 885 DBGPRINT(sc->dev, "failed %d", error); 886 sc->dev = NULL; 887 SFXGE_ADAPTER_LOCK_DESTROY(sc); 888 return (error); 889 } 890 891 static void 892 sfxge_destroy(struct sfxge_softc *sc) 893 { 894 efx_nic_t *enp; 895 896 /* Clean up transmit state. */ 897 sfxge_tx_fini(sc); 898 899 /* Clean up receive state. */ 900 sfxge_rx_fini(sc); 901 902 /* Clean up port state. */ 903 sfxge_port_fini(sc); 904 905 /* Clean up event processing state. */ 906 sfxge_ev_fini(sc); 907 908 /* Clean up interrupts. */ 909 sfxge_intr_fini(sc); 910 911 /* Tear down common code subsystems. */ 912 efx_nic_reset(sc->enp); 913 efx_vpd_fini(sc->enp); 914 efx_nvram_fini(sc->enp); 915 efx_nic_unprobe(sc->enp); 916 917 /* Tear down MCDI. */ 918 sfxge_mcdi_fini(sc); 919 920 /* Destroy common code context. */ 921 enp = sc->enp; 922 sc->enp = NULL; 923 efx_nic_destroy(enp); 924 925 /* Free DMA memory. */ 926 sfxge_dma_fini(sc); 927 928 /* Free mapped BARs. */ 929 sfxge_bar_fini(sc); 930 931 (void) pci_disable_busmaster(sc->dev); 932 933 taskqueue_drain(taskqueue_thread, &sc->task_reset); 934 935 /* Destroy the softc lock. */ 936 SFXGE_ADAPTER_LOCK_DESTROY(sc); 937 } 938 939 static int 940 sfxge_vpd_handler(SYSCTL_HANDLER_ARGS) 941 { 942 struct sfxge_softc *sc = arg1; 943 efx_vpd_value_t value; 944 int rc; 945 946 value.evv_tag = arg2 >> 16; 947 value.evv_keyword = arg2 & 0xffff; 948 if ((rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value)) 949 != 0) 950 return (rc); 951 952 return (SYSCTL_OUT(req, value.evv_value, value.evv_length)); 953 } 954 955 static void 956 sfxge_vpd_try_add(struct sfxge_softc *sc, struct sysctl_oid_list *list, 957 efx_vpd_tag_t tag, const char *keyword) 958 { 959 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev); 960 efx_vpd_value_t value; 961 962 /* Check whether VPD tag/keyword is present */ 963 value.evv_tag = tag; 964 value.evv_keyword = EFX_VPD_KEYWORD(keyword[0], keyword[1]); 965 if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) != 0) 966 return; 967 968 SYSCTL_ADD_PROC(ctx, list, OID_AUTO, keyword, 969 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, 970 sc, tag << 16 | EFX_VPD_KEYWORD(keyword[0], keyword[1]), 971 sfxge_vpd_handler, "A", ""); 972 } 973 974 static int 975 sfxge_vpd_init(struct sfxge_softc *sc) 976 { 977 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev); 978 struct sysctl_oid *vpd_node; 979 struct sysctl_oid_list *vpd_list; 980 char keyword[3]; 981 efx_vpd_value_t value; 982 int rc; 983 984 if ((rc = efx_vpd_size(sc->enp, &sc->vpd_size)) != 0) { 985 /* 986 * Unprivileged functions deny VPD access. 987 * Simply skip VPD in this case. 988 */ 989 if (rc == EACCES) 990 goto done; 991 goto fail; 992 } 993 sc->vpd_data = malloc(sc->vpd_size, M_SFXGE, M_WAITOK); 994 if ((rc = efx_vpd_read(sc->enp, sc->vpd_data, sc->vpd_size)) != 0) 995 goto fail2; 996 997 /* Copy ID (product name) into device description, and log it. */ 998 value.evv_tag = EFX_VPD_ID; 999 if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) == 0) { 1000 value.evv_value[value.evv_length] = 0; 1001 device_set_desc_copy(sc->dev, value.evv_value); 1002 device_printf(sc->dev, "%s\n", value.evv_value); 1003 } 1004 1005 vpd_node = SYSCTL_ADD_NODE(ctx, 1006 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), OID_AUTO, "vpd", 1007 CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Vital Product Data"); 1008 vpd_list = SYSCTL_CHILDREN(vpd_node); 1009 1010 /* Add sysctls for all expected and any vendor-defined keywords. */ 1011 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "PN"); 1012 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "EC"); 1013 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "SN"); 1014 keyword[0] = 'V'; 1015 keyword[2] = 0; 1016 for (keyword[1] = '0'; keyword[1] <= '9'; keyword[1]++) 1017 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword); 1018 for (keyword[1] = 'A'; keyword[1] <= 'Z'; keyword[1]++) 1019 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword); 1020 1021 done: 1022 return (0); 1023 1024 fail2: 1025 free(sc->vpd_data, M_SFXGE); 1026 fail: 1027 return (rc); 1028 } 1029 1030 static void 1031 sfxge_vpd_fini(struct sfxge_softc *sc) 1032 { 1033 free(sc->vpd_data, M_SFXGE); 1034 } 1035 1036 static void 1037 sfxge_reset(void *arg, int npending) 1038 { 1039 struct sfxge_softc *sc; 1040 int rc; 1041 unsigned attempt; 1042 1043 (void)npending; 1044 1045 sc = (struct sfxge_softc *)arg; 1046 1047 SFXGE_ADAPTER_LOCK(sc); 1048 1049 if (sc->init_state != SFXGE_STARTED) 1050 goto done; 1051 1052 sfxge_stop(sc); 1053 efx_nic_reset(sc->enp); 1054 for (attempt = 0; attempt < sfxge_restart_attempts; ++attempt) { 1055 if ((rc = sfxge_start(sc)) == 0) 1056 goto done; 1057 1058 device_printf(sc->dev, "start on reset failed (%d)\n", rc); 1059 DELAY(100000); 1060 } 1061 1062 device_printf(sc->dev, "reset failed; interface is now stopped\n"); 1063 1064 done: 1065 SFXGE_ADAPTER_UNLOCK(sc); 1066 } 1067 1068 void 1069 sfxge_schedule_reset(struct sfxge_softc *sc) 1070 { 1071 taskqueue_enqueue(taskqueue_thread, &sc->task_reset); 1072 } 1073 1074 static int 1075 sfxge_attach(device_t dev) 1076 { 1077 struct sfxge_softc *sc; 1078 if_t ifp; 1079 int error; 1080 1081 sc = device_get_softc(dev); 1082 sc->dev = dev; 1083 1084 /* Allocate ifnet. */ 1085 ifp = if_alloc(IFT_ETHER); 1086 if (ifp == NULL) { 1087 device_printf(dev, "Couldn't allocate ifnet\n"); 1088 error = ENOMEM; 1089 goto fail; 1090 } 1091 sc->ifnet = ifp; 1092 1093 /* Initialize hardware. */ 1094 DBGPRINT(sc->dev, "create nic"); 1095 if ((error = sfxge_create(sc)) != 0) 1096 goto fail2; 1097 1098 /* Create the ifnet for the port. */ 1099 DBGPRINT(sc->dev, "init ifnet"); 1100 if ((error = sfxge_ifnet_init(ifp, sc)) != 0) 1101 goto fail3; 1102 1103 DBGPRINT(sc->dev, "init vpd"); 1104 if ((error = sfxge_vpd_init(sc)) != 0) 1105 goto fail4; 1106 1107 /* 1108 * NIC is initialized inside sfxge_create() and kept inialized 1109 * to be able to initialize port to discover media types in 1110 * sfxge_ifnet_init(). 1111 */ 1112 efx_nic_fini(sc->enp); 1113 1114 sc->init_state = SFXGE_REGISTERED; 1115 1116 DBGPRINT(sc->dev, "success"); 1117 return (0); 1118 1119 fail4: 1120 sfxge_ifnet_fini(ifp); 1121 fail3: 1122 efx_nic_fini(sc->enp); 1123 sfxge_destroy(sc); 1124 1125 fail2: 1126 if_free(sc->ifnet); 1127 1128 fail: 1129 DBGPRINT(sc->dev, "failed %d", error); 1130 return (error); 1131 } 1132 1133 static int 1134 sfxge_detach(device_t dev) 1135 { 1136 struct sfxge_softc *sc; 1137 1138 sc = device_get_softc(dev); 1139 1140 sfxge_vpd_fini(sc); 1141 1142 /* Destroy the ifnet. */ 1143 sfxge_ifnet_fini(sc->ifnet); 1144 1145 /* Tear down hardware. */ 1146 sfxge_destroy(sc); 1147 1148 return (0); 1149 } 1150 1151 static int 1152 sfxge_probe(device_t dev) 1153 { 1154 uint16_t pci_vendor_id; 1155 uint16_t pci_device_id; 1156 efx_family_t family; 1157 unsigned int mem_bar; 1158 int rc; 1159 1160 pci_vendor_id = pci_get_vendor(dev); 1161 pci_device_id = pci_get_device(dev); 1162 1163 DBGPRINT(dev, "PCI ID %04x:%04x", pci_vendor_id, pci_device_id); 1164 rc = efx_family(pci_vendor_id, pci_device_id, &family, &mem_bar); 1165 if (rc != 0) { 1166 DBGPRINT(dev, "efx_family fail %d", rc); 1167 return (ENXIO); 1168 } 1169 1170 if (family == EFX_FAMILY_SIENA) { 1171 device_set_desc(dev, "Solarflare SFC9000 family"); 1172 return (0); 1173 } 1174 1175 if (family == EFX_FAMILY_HUNTINGTON) { 1176 device_set_desc(dev, "Solarflare SFC9100 family"); 1177 return (0); 1178 } 1179 1180 if (family == EFX_FAMILY_MEDFORD) { 1181 device_set_desc(dev, "Solarflare SFC9200 family"); 1182 return (0); 1183 } 1184 1185 if (family == EFX_FAMILY_MEDFORD2) { 1186 device_set_desc(dev, "Solarflare SFC9250 family"); 1187 return (0); 1188 } 1189 1190 DBGPRINT(dev, "impossible controller family %d", family); 1191 return (ENXIO); 1192 } 1193 1194 static device_method_t sfxge_methods[] = { 1195 DEVMETHOD(device_probe, sfxge_probe), 1196 DEVMETHOD(device_attach, sfxge_attach), 1197 DEVMETHOD(device_detach, sfxge_detach), 1198 1199 DEVMETHOD_END 1200 }; 1201 1202 static driver_t sfxge_driver = { 1203 "sfxge", 1204 sfxge_methods, 1205 sizeof(struct sfxge_softc) 1206 }; 1207 1208 DRIVER_MODULE(sfxge, pci, sfxge_driver, 0, 0); 1209