2 * Copyright (c) 2010-2016 Solarflare Communications Inc.
5 * This software was developed in part by Philip Paeps under contract for
6 * Solarflare Communications, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation are
30 * those of the authors and should not be interpreted as representing official
31 * policies, either expressed or implied, of the FreeBSD Project.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
45 #include <sys/socket.h>
46 #include <sys/taskqueue.h>
47 #include <sys/sockio.h>
48 #include <sys/sysctl.h>
50 #include <sys/syslog.h>
52 #include <dev/pci/pcireg.h>
53 #include <dev/pci/pcivar.h>
55 #include <net/ethernet.h>
57 #include <net/if_media.h>
58 #include <net/if_types.h>
60 #include "common/efx.h"
64 #include "sfxge_ioc.h"
65 #include "sfxge_version.h"
67 #define SFXGE_CAP (IFCAP_VLAN_MTU | IFCAP_VLAN_HWCSUM | \
68 IFCAP_RXCSUM | IFCAP_TXCSUM | \
69 IFCAP_RXCSUM_IPV6 | IFCAP_TXCSUM_IPV6 | \
70 IFCAP_TSO4 | IFCAP_TSO6 | \
72 IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE | IFCAP_HWSTATS)
73 #define SFXGE_CAP_ENABLE SFXGE_CAP
74 #define SFXGE_CAP_FIXED (IFCAP_VLAN_MTU | \
75 IFCAP_JUMBO_MTU | IFCAP_LINKSTATE | IFCAP_HWSTATS)
77 MALLOC_DEFINE(M_SFXGE, "sfxge", "Solarflare 10GigE driver");
80 SYSCTL_NODE(_hw, OID_AUTO, sfxge, CTLFLAG_RD, 0,
81 "SFXGE driver parameters");
83 #define SFXGE_PARAM_RX_RING SFXGE_PARAM(rx_ring)
84 static int sfxge_rx_ring_entries = SFXGE_NDESCS;
85 TUNABLE_INT(SFXGE_PARAM_RX_RING, &sfxge_rx_ring_entries);
86 SYSCTL_INT(_hw_sfxge, OID_AUTO, rx_ring, CTLFLAG_RDTUN,
87 &sfxge_rx_ring_entries, 0,
88 "Maximum number of descriptors in a receive ring");
90 #define SFXGE_PARAM_TX_RING SFXGE_PARAM(tx_ring)
91 static int sfxge_tx_ring_entries = SFXGE_NDESCS;
92 TUNABLE_INT(SFXGE_PARAM_TX_RING, &sfxge_tx_ring_entries);
93 SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_ring, CTLFLAG_RDTUN,
94 &sfxge_tx_ring_entries, 0,
95 "Maximum number of descriptors in a transmit ring");
97 #define SFXGE_PARAM_RESTART_ATTEMPTS SFXGE_PARAM(restart_attempts)
98 static int sfxge_restart_attempts = 3;
99 TUNABLE_INT(SFXGE_PARAM_RESTART_ATTEMPTS, &sfxge_restart_attempts);
100 SYSCTL_INT(_hw_sfxge, OID_AUTO, restart_attempts, CTLFLAG_RDTUN,
101 &sfxge_restart_attempts, 0,
102 "Maximum number of attempts to bring interface up after reset");
104 #if EFSYS_OPT_MCDI_LOGGING
105 #define SFXGE_PARAM_MCDI_LOGGING SFXGE_PARAM(mcdi_logging)
106 static int sfxge_mcdi_logging = 0;
107 TUNABLE_INT(SFXGE_PARAM_MCDI_LOGGING, &sfxge_mcdi_logging);
111 sfxge_reset(void *arg, int npending);
114 sfxge_estimate_rsrc_limits(struct sfxge_softc *sc)
116 efx_drv_limits_t limits;
118 unsigned int evq_max;
119 uint32_t evq_allocated;
120 uint32_t rxq_allocated;
121 uint32_t txq_allocated;
124 * Limit the number of event queues to:
126 * - hardwire maximum RSS channels
127 * - administratively specified maximum RSS channels
129 evq_max = MIN(mp_ncpus, EFX_MAXRSS);
130 if (sc->max_rss_channels > 0)
131 evq_max = MIN(evq_max, sc->max_rss_channels);
133 memset(&limits, 0, sizeof(limits));
135 limits.edl_min_evq_count = 1;
136 limits.edl_max_evq_count = evq_max;
137 limits.edl_min_txq_count = SFXGE_EVQ0_N_TXQ(sc);
138 limits.edl_max_txq_count = evq_max + SFXGE_EVQ0_N_TXQ(sc) - 1;
139 limits.edl_min_rxq_count = 1;
140 limits.edl_max_rxq_count = evq_max;
142 efx_nic_set_drv_limits(sc->enp, &limits);
144 if ((rc = efx_nic_init(sc->enp)) != 0)
147 rc = efx_nic_get_vi_pool(sc->enp, &evq_allocated, &rxq_allocated,
150 efx_nic_fini(sc->enp);
154 KASSERT(txq_allocated >= SFXGE_EVQ0_N_TXQ(sc),
155 ("txq_allocated < %u", SFXGE_EVQ0_N_TXQ(sc)));
157 sc->evq_max = MIN(evq_allocated, evq_max);
158 sc->evq_max = MIN(rxq_allocated, sc->evq_max);
159 sc->evq_max = MIN(txq_allocated - (SFXGE_EVQ0_N_TXQ(sc) - 1),
162 KASSERT(sc->evq_max <= evq_max,
163 ("allocated more than maximum requested"));
166 * NIC is kept initialized in the case of success to be able to
167 * initialize port to find out media types.
173 sfxge_set_drv_limits(struct sfxge_softc *sc)
175 efx_drv_limits_t limits;
177 memset(&limits, 0, sizeof(limits));
179 /* Limits are strict since take into account initial estimation */
180 limits.edl_min_evq_count = limits.edl_max_evq_count =
182 limits.edl_min_txq_count = limits.edl_max_txq_count =
183 sc->intr.n_alloc + SFXGE_EVQ0_N_TXQ(sc) - 1;
184 limits.edl_min_rxq_count = limits.edl_max_rxq_count =
187 return (efx_nic_set_drv_limits(sc->enp, &limits));
191 sfxge_start(struct sfxge_softc *sc)
195 SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
197 if (sc->init_state == SFXGE_STARTED)
200 if (sc->init_state != SFXGE_REGISTERED) {
205 /* Set required resource limits */
206 if ((rc = sfxge_set_drv_limits(sc)) != 0)
209 if ((rc = efx_nic_init(sc->enp)) != 0)
212 /* Start processing interrupts. */
213 if ((rc = sfxge_intr_start(sc)) != 0)
216 /* Start processing events. */
217 if ((rc = sfxge_ev_start(sc)) != 0)
220 /* Fire up the port. */
221 if ((rc = sfxge_port_start(sc)) != 0)
224 /* Start the receiver side. */
225 if ((rc = sfxge_rx_start(sc)) != 0)
228 /* Start the transmitter side. */
229 if ((rc = sfxge_tx_start(sc)) != 0)
232 sc->init_state = SFXGE_STARTED;
234 /* Tell the stack we're running. */
235 sc->ifnet->if_drv_flags |= IFF_DRV_RUNNING;
236 sc->ifnet->if_drv_flags &= ~IFF_DRV_OACTIVE;
253 efx_nic_fini(sc->enp);
256 device_printf(sc->dev, "sfxge_start: %d\n", rc);
262 sfxge_if_init(void *arg)
264 struct sfxge_softc *sc;
266 sc = (struct sfxge_softc *)arg;
268 SFXGE_ADAPTER_LOCK(sc);
269 (void)sfxge_start(sc);
270 SFXGE_ADAPTER_UNLOCK(sc);
274 sfxge_stop(struct sfxge_softc *sc)
276 SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
278 if (sc->init_state != SFXGE_STARTED)
281 sc->init_state = SFXGE_REGISTERED;
283 /* Stop the transmitter. */
286 /* Stop the receiver. */
292 /* Stop processing events. */
295 /* Stop processing interrupts. */
298 efx_nic_fini(sc->enp);
300 sc->ifnet->if_drv_flags &= ~IFF_DRV_RUNNING;
305 sfxge_vpd_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ioc)
307 efx_vpd_value_t value;
310 switch (ioc->u.vpd.op) {
311 case SFXGE_VPD_OP_GET_KEYWORD:
312 value.evv_tag = ioc->u.vpd.tag;
313 value.evv_keyword = ioc->u.vpd.keyword;
314 rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value);
317 ioc->u.vpd.len = MIN(ioc->u.vpd.len, value.evv_length);
318 if (ioc->u.vpd.payload != 0) {
319 rc = copyout(value.evv_value, ioc->u.vpd.payload,
323 case SFXGE_VPD_OP_SET_KEYWORD:
324 if (ioc->u.vpd.len > sizeof(value.evv_value))
326 value.evv_tag = ioc->u.vpd.tag;
327 value.evv_keyword = ioc->u.vpd.keyword;
328 value.evv_length = ioc->u.vpd.len;
329 rc = copyin(ioc->u.vpd.payload, value.evv_value, value.evv_length);
332 rc = efx_vpd_set(sc->enp, sc->vpd_data, sc->vpd_size, &value);
335 rc = efx_vpd_verify(sc->enp, sc->vpd_data, sc->vpd_size);
338 rc = efx_vpd_write(sc->enp, sc->vpd_data, sc->vpd_size);
349 sfxge_private_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ioc)
353 return (sfxge_mcdi_ioctl(sc, ioc));
354 case SFXGE_NVRAM_IOC:
355 return (sfxge_nvram_ioctl(sc, ioc));
357 return (sfxge_vpd_ioctl(sc, ioc));
365 sfxge_if_ioctl(struct ifnet *ifp, unsigned long command, caddr_t data)
367 struct sfxge_softc *sc;
372 ifr = (struct ifreq *)data;
378 SFXGE_ADAPTER_LOCK(sc);
379 if (ifp->if_flags & IFF_UP) {
380 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
381 if ((ifp->if_flags ^ sc->if_flags) &
382 (IFF_PROMISC | IFF_ALLMULTI)) {
383 sfxge_mac_filter_set(sc);
388 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
390 sc->if_flags = ifp->if_flags;
391 SFXGE_ADAPTER_UNLOCK(sc);
394 if (ifr->ifr_mtu == ifp->if_mtu) {
397 } else if (ifr->ifr_mtu > SFXGE_MAX_MTU) {
399 } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
400 ifp->if_mtu = ifr->ifr_mtu;
403 /* Restart required */
404 SFXGE_ADAPTER_LOCK(sc);
406 ifp->if_mtu = ifr->ifr_mtu;
407 error = sfxge_start(sc);
408 SFXGE_ADAPTER_UNLOCK(sc);
410 ifp->if_flags &= ~IFF_UP;
411 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
418 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
419 sfxge_mac_filter_set(sc);
423 int reqcap = ifr->ifr_reqcap;
426 SFXGE_ADAPTER_LOCK(sc);
428 /* Capabilities to be changed in accordance with request */
429 capchg_mask = ifp->if_capenable ^ reqcap;
432 * The networking core already rejects attempts to
433 * enable capabilities we don't have. We still have
434 * to reject attempts to disable capabilities that we
435 * can't (yet) disable.
437 KASSERT((reqcap & ~ifp->if_capabilities) == 0,
438 ("Unsupported capabilities 0x%x requested 0x%x vs "
440 reqcap & ~ifp->if_capabilities,
441 reqcap , ifp->if_capabilities));
442 if (capchg_mask & SFXGE_CAP_FIXED) {
444 SFXGE_ADAPTER_UNLOCK(sc);
448 /* Check request before any changes */
449 if ((capchg_mask & IFCAP_TSO4) &&
450 (reqcap & (IFCAP_TSO4 | IFCAP_TXCSUM)) == IFCAP_TSO4) {
452 SFXGE_ADAPTER_UNLOCK(sc);
453 if_printf(ifp, "enable txcsum before tso4\n");
456 if ((capchg_mask & IFCAP_TSO6) &&
457 (reqcap & (IFCAP_TSO6 | IFCAP_TXCSUM_IPV6)) == IFCAP_TSO6) {
459 SFXGE_ADAPTER_UNLOCK(sc);
460 if_printf(ifp, "enable txcsum6 before tso6\n");
464 if (reqcap & IFCAP_TXCSUM) {
465 ifp->if_hwassist |= (CSUM_IP | CSUM_TCP | CSUM_UDP);
467 ifp->if_hwassist &= ~(CSUM_IP | CSUM_TCP | CSUM_UDP);
468 if (reqcap & IFCAP_TSO4) {
469 reqcap &= ~IFCAP_TSO4;
471 "tso4 disabled due to -txcsum\n");
474 if (reqcap & IFCAP_TXCSUM_IPV6) {
475 ifp->if_hwassist |= (CSUM_TCP_IPV6 | CSUM_UDP_IPV6);
477 ifp->if_hwassist &= ~(CSUM_TCP_IPV6 | CSUM_UDP_IPV6);
478 if (reqcap & IFCAP_TSO6) {
479 reqcap &= ~IFCAP_TSO6;
481 "tso6 disabled due to -txcsum6\n");
486 * The kernel takes both IFCAP_TSOx and CSUM_TSO into
487 * account before using TSO. So, we do not touch
488 * checksum flags when IFCAP_TSOx is modified.
489 * Note that CSUM_TSO is (CSUM_IP_TSO|CSUM_IP6_TSO),
490 * but both bits are set in IPv4 and IPv6 mbufs.
493 ifp->if_capenable = reqcap;
495 SFXGE_ADAPTER_UNLOCK(sc);
500 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
507 error = copyin(ifr->ifr_data, &i2c, sizeof(i2c));
511 if (i2c.len > sizeof(i2c.data)) {
516 SFXGE_ADAPTER_LOCK(sc);
517 error = efx_phy_module_get_info(sc->enp, i2c.dev_addr,
520 SFXGE_ADAPTER_UNLOCK(sc);
522 error = copyout(&i2c, ifr->ifr_data, sizeof(i2c));
527 error = priv_check(curthread, PRIV_DRIVER);
530 error = copyin(ifr->ifr_data, &ioc, sizeof(ioc));
533 error = sfxge_private_ioctl(sc, &ioc);
535 error = copyout(&ioc, ifr->ifr_data, sizeof(ioc));
539 error = ether_ioctl(ifp, command, data);
546 sfxge_tick(void *arg)
548 struct sfxge_softc *sc = arg;
550 sfxge_port_update_stats(sc);
551 sfxge_tx_update_stats(sc);
553 callout_reset(&sc->tick_callout,
554 hz * sc->port.stats_update_period_ms / 1000,
559 sfxge_ifnet_fini(struct ifnet *ifp)
561 struct sfxge_softc *sc = ifp->if_softc;
563 callout_drain(&sc->tick_callout);
565 SFXGE_ADAPTER_LOCK(sc);
567 SFXGE_ADAPTER_UNLOCK(sc);
569 ifmedia_removeall(&sc->media);
575 sfxge_ifnet_init(struct ifnet *ifp, struct sfxge_softc *sc)
577 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
584 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
585 ifp->if_init = sfxge_if_init;
587 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
588 ifp->if_ioctl = sfxge_if_ioctl;
590 ifp->if_capabilities = SFXGE_CAP;
591 ifp->if_capenable = SFXGE_CAP_ENABLE;
592 ifp->if_hw_tsomax = SFXGE_TSO_MAX_SIZE;
593 ifp->if_hw_tsomaxsegcount = SFXGE_TX_MAPPING_MAX_SEG;
594 ifp->if_hw_tsomaxsegsize = PAGE_SIZE;
597 ifp->if_capabilities |= IFCAP_LRO;
598 ifp->if_capenable |= IFCAP_LRO;
601 if (encp->enc_hw_tx_insert_vlan_enabled) {
602 ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING;
603 ifp->if_capenable |= IFCAP_VLAN_HWTAGGING;
605 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO |
606 CSUM_TCP_IPV6 | CSUM_UDP_IPV6;
608 ether_ifattach(ifp, encp->enc_mac_addr);
610 ifp->if_transmit = sfxge_if_transmit;
611 ifp->if_qflush = sfxge_if_qflush;
613 callout_init(&sc->tick_callout, B_TRUE);
615 DBGPRINT(sc->dev, "ifmedia_init");
616 if ((rc = sfxge_port_ifmedia_init(sc)) != 0)
619 callout_reset(&sc->tick_callout,
620 hz * sc->port.stats_update_period_ms / 1000,
626 ether_ifdetach(sc->ifnet);
631 sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n, uint32_t *idp)
633 KASSERT(sc->buffer_table_next + n <=
634 efx_nic_cfg_get(sc->enp)->enc_buftbl_limit,
635 ("buffer table full"));
637 *idp = sc->buffer_table_next;
638 sc->buffer_table_next += n;
642 sfxge_bar_init(struct sfxge_softc *sc)
644 efsys_bar_t *esbp = &sc->bar;
646 esbp->esb_rid = PCIR_BAR(EFX_MEM_BAR);
647 if ((esbp->esb_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
648 &esbp->esb_rid, RF_ACTIVE)) == NULL) {
649 device_printf(sc->dev, "Cannot allocate BAR region %d\n",
653 esbp->esb_tag = rman_get_bustag(esbp->esb_res);
654 esbp->esb_handle = rman_get_bushandle(esbp->esb_res);
656 SFXGE_BAR_LOCK_INIT(esbp, device_get_nameunit(sc->dev));
662 sfxge_bar_fini(struct sfxge_softc *sc)
664 efsys_bar_t *esbp = &sc->bar;
666 bus_release_resource(sc->dev, SYS_RES_MEMORY, esbp->esb_rid,
668 SFXGE_BAR_LOCK_DESTROY(esbp);
672 sfxge_create(struct sfxge_softc *sc)
677 char rss_param_name[sizeof(SFXGE_PARAM(%d.max_rss_channels))];
678 #if EFSYS_OPT_MCDI_LOGGING
679 char mcdi_log_param_name[sizeof(SFXGE_PARAM(%d.mcdi_logging))];
684 SFXGE_ADAPTER_LOCK_INIT(sc, device_get_nameunit(sc->dev));
686 sc->max_rss_channels = 0;
687 snprintf(rss_param_name, sizeof(rss_param_name),
688 SFXGE_PARAM(%d.max_rss_channels),
689 (int)device_get_unit(dev));
690 TUNABLE_INT_FETCH(rss_param_name, &sc->max_rss_channels);
691 #if EFSYS_OPT_MCDI_LOGGING
692 sc->mcdi_logging = sfxge_mcdi_logging;
693 snprintf(mcdi_log_param_name, sizeof(mcdi_log_param_name),
694 SFXGE_PARAM(%d.mcdi_logging),
695 (int)device_get_unit(dev));
696 TUNABLE_INT_FETCH(mcdi_log_param_name, &sc->mcdi_logging);
699 sc->stats_node = SYSCTL_ADD_NODE(
700 device_get_sysctl_ctx(dev),
701 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
702 OID_AUTO, "stats", CTLFLAG_RD, NULL, "Statistics");
703 if (sc->stats_node == NULL) {
708 TASK_INIT(&sc->task_reset, 0, sfxge_reset, sc);
710 (void) pci_enable_busmaster(dev);
712 /* Initialize DMA mappings. */
713 DBGPRINT(sc->dev, "dma_init...");
714 if ((error = sfxge_dma_init(sc)) != 0)
717 /* Map the device registers. */
718 DBGPRINT(sc->dev, "bar_init...");
719 if ((error = sfxge_bar_init(sc)) != 0)
722 error = efx_family(pci_get_vendor(dev), pci_get_device(dev),
724 KASSERT(error == 0, ("Family should be filtered by sfxge_probe()"));
726 DBGPRINT(sc->dev, "nic_create...");
728 /* Create the common code nic object. */
729 SFXGE_EFSYS_LOCK_INIT(&sc->enp_lock,
730 device_get_nameunit(sc->dev), "nic");
731 if ((error = efx_nic_create(sc->family, (efsys_identifier_t *)sc,
732 &sc->bar, &sc->enp_lock, &enp)) != 0)
736 /* Initialize MCDI to talk to the microcontroller. */
737 DBGPRINT(sc->dev, "mcdi_init...");
738 if ((error = sfxge_mcdi_init(sc)) != 0)
741 /* Probe the NIC and build the configuration data area. */
742 DBGPRINT(sc->dev, "nic_probe...");
743 if ((error = efx_nic_probe(enp)) != 0)
746 if (!ISP2(sfxge_rx_ring_entries) ||
747 (sfxge_rx_ring_entries < EFX_RXQ_MINNDESCS) ||
748 (sfxge_rx_ring_entries > EFX_RXQ_MAXNDESCS)) {
749 log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
750 SFXGE_PARAM_RX_RING, sfxge_rx_ring_entries,
751 EFX_RXQ_MINNDESCS, EFX_RXQ_MAXNDESCS);
753 goto fail_rx_ring_entries;
755 sc->rxq_entries = sfxge_rx_ring_entries;
757 if (efx_nic_cfg_get(enp)->enc_features & EFX_FEATURE_TXQ_CKSUM_OP_DESC)
758 sc->txq_dynamic_cksum_toggle_supported = B_TRUE;
760 sc->txq_dynamic_cksum_toggle_supported = B_FALSE;
762 if (!ISP2(sfxge_tx_ring_entries) ||
763 (sfxge_tx_ring_entries < EFX_TXQ_MINNDESCS) ||
764 (sfxge_tx_ring_entries > efx_nic_cfg_get(enp)->enc_txq_max_ndescs)) {
765 log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
766 SFXGE_PARAM_TX_RING, sfxge_tx_ring_entries,
767 EFX_TXQ_MINNDESCS, efx_nic_cfg_get(enp)->enc_txq_max_ndescs);
769 goto fail_tx_ring_entries;
771 sc->txq_entries = sfxge_tx_ring_entries;
773 SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
774 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
775 OID_AUTO, "version", CTLFLAG_RD,
776 SFXGE_VERSION_STRING, 0,
779 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
780 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
781 OID_AUTO, "phy_type", CTLFLAG_RD,
782 NULL, efx_nic_cfg_get(enp)->enc_phy_type,
785 /* Initialize the NVRAM. */
786 DBGPRINT(sc->dev, "nvram_init...");
787 if ((error = efx_nvram_init(enp)) != 0)
790 /* Initialize the VPD. */
791 DBGPRINT(sc->dev, "vpd_init...");
792 if ((error = efx_vpd_init(enp)) != 0)
795 efx_mcdi_new_epoch(enp);
798 DBGPRINT(sc->dev, "nic_reset...");
799 if ((error = efx_nic_reset(enp)) != 0)
802 /* Initialize buffer table allocation. */
803 sc->buffer_table_next = 0;
806 * Guarantee minimum and estimate maximum number of event queues
807 * to take it into account when MSI-X interrupts are allocated.
808 * It initializes NIC and keeps it initialized on success.
810 if ((error = sfxge_estimate_rsrc_limits(sc)) != 0)
813 /* Set up interrupts. */
814 DBGPRINT(sc->dev, "intr_init...");
815 if ((error = sfxge_intr_init(sc)) != 0)
818 /* Initialize event processing state. */
819 DBGPRINT(sc->dev, "ev_init...");
820 if ((error = sfxge_ev_init(sc)) != 0)
823 /* Initialize port state. */
824 DBGPRINT(sc->dev, "port_init...");
825 if ((error = sfxge_port_init(sc)) != 0)
828 /* Initialize receive state. */
829 DBGPRINT(sc->dev, "rx_init...");
830 if ((error = sfxge_rx_init(sc)) != 0)
833 /* Initialize transmit state. */
834 DBGPRINT(sc->dev, "tx_init...");
835 if ((error = sfxge_tx_init(sc)) != 0)
838 sc->init_state = SFXGE_INITIALIZED;
840 DBGPRINT(sc->dev, "success");
856 efx_nic_fini(sc->enp);
865 fail_tx_ring_entries:
866 fail_rx_ring_entries:
867 efx_nic_unprobe(enp);
874 efx_nic_destroy(enp);
875 SFXGE_EFSYS_LOCK_DESTROY(&sc->enp_lock);
879 (void) pci_disable_busmaster(sc->dev);
882 DBGPRINT(sc->dev, "failed %d", error);
884 SFXGE_ADAPTER_LOCK_DESTROY(sc);
889 sfxge_destroy(struct sfxge_softc *sc)
893 /* Clean up transmit state. */
896 /* Clean up receive state. */
899 /* Clean up port state. */
902 /* Clean up event processing state. */
905 /* Clean up interrupts. */
908 /* Tear down common code subsystems. */
909 efx_nic_reset(sc->enp);
910 efx_vpd_fini(sc->enp);
911 efx_nvram_fini(sc->enp);
912 efx_nic_unprobe(sc->enp);
914 /* Tear down MCDI. */
917 /* Destroy common code context. */
920 efx_nic_destroy(enp);
922 /* Free DMA memory. */
925 /* Free mapped BARs. */
928 (void) pci_disable_busmaster(sc->dev);
930 taskqueue_drain(taskqueue_thread, &sc->task_reset);
932 /* Destroy the softc lock. */
933 SFXGE_ADAPTER_LOCK_DESTROY(sc);
937 sfxge_vpd_handler(SYSCTL_HANDLER_ARGS)
939 struct sfxge_softc *sc = arg1;
940 efx_vpd_value_t value;
943 value.evv_tag = arg2 >> 16;
944 value.evv_keyword = arg2 & 0xffff;
945 if ((rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value))
949 return (SYSCTL_OUT(req, value.evv_value, value.evv_length));
953 sfxge_vpd_try_add(struct sfxge_softc *sc, struct sysctl_oid_list *list,
954 efx_vpd_tag_t tag, const char *keyword)
956 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
957 efx_vpd_value_t value;
959 /* Check whether VPD tag/keyword is present */
961 value.evv_keyword = EFX_VPD_KEYWORD(keyword[0], keyword[1]);
962 if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) != 0)
966 ctx, list, OID_AUTO, keyword, CTLTYPE_STRING|CTLFLAG_RD,
967 sc, tag << 16 | EFX_VPD_KEYWORD(keyword[0], keyword[1]),
968 sfxge_vpd_handler, "A", "");
972 sfxge_vpd_init(struct sfxge_softc *sc)
974 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
975 struct sysctl_oid *vpd_node;
976 struct sysctl_oid_list *vpd_list;
978 efx_vpd_value_t value;
981 if ((rc = efx_vpd_size(sc->enp, &sc->vpd_size)) != 0) {
983 * Unpriviledged functions deny VPD access.
984 * Simply skip VPD in this case.
990 sc->vpd_data = malloc(sc->vpd_size, M_SFXGE, M_WAITOK);
991 if ((rc = efx_vpd_read(sc->enp, sc->vpd_data, sc->vpd_size)) != 0)
994 /* Copy ID (product name) into device description, and log it. */
995 value.evv_tag = EFX_VPD_ID;
996 if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) == 0) {
997 value.evv_value[value.evv_length] = 0;
998 device_set_desc_copy(sc->dev, value.evv_value);
999 device_printf(sc->dev, "%s\n", value.evv_value);
1002 vpd_node = SYSCTL_ADD_NODE(
1003 ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
1004 OID_AUTO, "vpd", CTLFLAG_RD, NULL, "Vital Product Data");
1005 vpd_list = SYSCTL_CHILDREN(vpd_node);
1007 /* Add sysctls for all expected and any vendor-defined keywords. */
1008 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "PN");
1009 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "EC");
1010 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "SN");
1013 for (keyword[1] = '0'; keyword[1] <= '9'; keyword[1]++)
1014 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
1015 for (keyword[1] = 'A'; keyword[1] <= 'Z'; keyword[1]++)
1016 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
1022 free(sc->vpd_data, M_SFXGE);
1028 sfxge_vpd_fini(struct sfxge_softc *sc)
1030 free(sc->vpd_data, M_SFXGE);
1034 sfxge_reset(void *arg, int npending)
1036 struct sfxge_softc *sc;
1042 sc = (struct sfxge_softc *)arg;
1044 SFXGE_ADAPTER_LOCK(sc);
1046 if (sc->init_state != SFXGE_STARTED)
1050 efx_nic_reset(sc->enp);
1051 for (attempt = 0; attempt < sfxge_restart_attempts; ++attempt) {
1052 if ((rc = sfxge_start(sc)) == 0)
1055 device_printf(sc->dev, "start on reset failed (%d)\n", rc);
1059 device_printf(sc->dev, "reset failed; interface is now stopped\n");
1062 SFXGE_ADAPTER_UNLOCK(sc);
1066 sfxge_schedule_reset(struct sfxge_softc *sc)
1068 taskqueue_enqueue(taskqueue_thread, &sc->task_reset);
1072 sfxge_attach(device_t dev)
1074 struct sfxge_softc *sc;
1078 sc = device_get_softc(dev);
1081 /* Allocate ifnet. */
1082 ifp = if_alloc(IFT_ETHER);
1084 device_printf(dev, "Couldn't allocate ifnet\n");
1090 /* Initialize hardware. */
1091 DBGPRINT(sc->dev, "create nic");
1092 if ((error = sfxge_create(sc)) != 0)
1095 /* Create the ifnet for the port. */
1096 DBGPRINT(sc->dev, "init ifnet");
1097 if ((error = sfxge_ifnet_init(ifp, sc)) != 0)
1100 DBGPRINT(sc->dev, "init vpd");
1101 if ((error = sfxge_vpd_init(sc)) != 0)
1105 * NIC is initialized inside sfxge_create() and kept inialized
1106 * to be able to initialize port to discover media types in
1107 * sfxge_ifnet_init().
1109 efx_nic_fini(sc->enp);
1111 sc->init_state = SFXGE_REGISTERED;
1113 DBGPRINT(sc->dev, "success");
1117 sfxge_ifnet_fini(ifp);
1119 efx_nic_fini(sc->enp);
1126 DBGPRINT(sc->dev, "failed %d", error);
1131 sfxge_detach(device_t dev)
1133 struct sfxge_softc *sc;
1135 sc = device_get_softc(dev);
1139 /* Destroy the ifnet. */
1140 sfxge_ifnet_fini(sc->ifnet);
1142 /* Tear down hardware. */
1149 sfxge_probe(device_t dev)
1151 uint16_t pci_vendor_id;
1152 uint16_t pci_device_id;
1153 efx_family_t family;
1156 pci_vendor_id = pci_get_vendor(dev);
1157 pci_device_id = pci_get_device(dev);
1159 DBGPRINT(dev, "PCI ID %04x:%04x", pci_vendor_id, pci_device_id);
1160 rc = efx_family(pci_vendor_id, pci_device_id, &family);
1162 DBGPRINT(dev, "efx_family fail %d", rc);
1166 if (family == EFX_FAMILY_SIENA) {
1167 device_set_desc(dev, "Solarflare SFC9000 family");
1171 if (family == EFX_FAMILY_HUNTINGTON) {
1172 device_set_desc(dev, "Solarflare SFC9100 family");
1176 if (family == EFX_FAMILY_MEDFORD) {
1177 device_set_desc(dev, "Solarflare SFC9200 family");
1181 DBGPRINT(dev, "impossible controller family %d", family);
1185 static device_method_t sfxge_methods[] = {
1186 DEVMETHOD(device_probe, sfxge_probe),
1187 DEVMETHOD(device_attach, sfxge_attach),
1188 DEVMETHOD(device_detach, sfxge_detach),
1193 static devclass_t sfxge_devclass;
1195 static driver_t sfxge_driver = {
1198 sizeof(struct sfxge_softc)
1201 DRIVER_MODULE(sfxge, pci, sfxge_driver, sfxge_devclass, 0, 0);