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_var.h>
58 #include <net/if_media.h>
59 #include <net/if_types.h>
61 #include "common/efx.h"
65 #include "sfxge_ioc.h"
66 #include "sfxge_version.h"
68 #define SFXGE_CAP (IFCAP_VLAN_MTU | IFCAP_VLAN_HWCSUM | \
69 IFCAP_RXCSUM | IFCAP_TXCSUM | \
70 IFCAP_RXCSUM_IPV6 | IFCAP_TXCSUM_IPV6 | \
71 IFCAP_TSO4 | IFCAP_TSO6 | \
73 IFCAP_VLAN_HWTSO | IFCAP_LINKSTATE | IFCAP_HWSTATS)
74 #define SFXGE_CAP_ENABLE SFXGE_CAP
75 #define SFXGE_CAP_FIXED (IFCAP_VLAN_MTU | \
76 IFCAP_JUMBO_MTU | IFCAP_LINKSTATE | IFCAP_HWSTATS)
78 MALLOC_DEFINE(M_SFXGE, "sfxge", "Solarflare 10GigE driver");
81 SYSCTL_NODE(_hw, OID_AUTO, sfxge, CTLFLAG_RD, 0,
82 "SFXGE driver parameters");
84 #define SFXGE_PARAM_RX_RING SFXGE_PARAM(rx_ring)
85 static int sfxge_rx_ring_entries = SFXGE_NDESCS;
86 TUNABLE_INT(SFXGE_PARAM_RX_RING, &sfxge_rx_ring_entries);
87 SYSCTL_INT(_hw_sfxge, OID_AUTO, rx_ring, CTLFLAG_RDTUN,
88 &sfxge_rx_ring_entries, 0,
89 "Maximum number of descriptors in a receive ring");
91 #define SFXGE_PARAM_TX_RING SFXGE_PARAM(tx_ring)
92 static int sfxge_tx_ring_entries = SFXGE_NDESCS;
93 TUNABLE_INT(SFXGE_PARAM_TX_RING, &sfxge_tx_ring_entries);
94 SYSCTL_INT(_hw_sfxge, OID_AUTO, tx_ring, CTLFLAG_RDTUN,
95 &sfxge_tx_ring_entries, 0,
96 "Maximum number of descriptors in a transmit ring");
98 #define SFXGE_PARAM_RESTART_ATTEMPTS SFXGE_PARAM(restart_attempts)
99 static int sfxge_restart_attempts = 3;
100 TUNABLE_INT(SFXGE_PARAM_RESTART_ATTEMPTS, &sfxge_restart_attempts);
101 SYSCTL_INT(_hw_sfxge, OID_AUTO, restart_attempts, CTLFLAG_RDTUN,
102 &sfxge_restart_attempts, 0,
103 "Maximum number of attempts to bring interface up after reset");
105 #if EFSYS_OPT_MCDI_LOGGING
106 #define SFXGE_PARAM_MCDI_LOGGING SFXGE_PARAM(mcdi_logging)
107 static int sfxge_mcdi_logging = 0;
108 TUNABLE_INT(SFXGE_PARAM_MCDI_LOGGING, &sfxge_mcdi_logging);
112 sfxge_reset(void *arg, int npending);
115 sfxge_estimate_rsrc_limits(struct sfxge_softc *sc)
117 efx_drv_limits_t limits;
119 unsigned int evq_max;
120 uint32_t evq_allocated;
121 uint32_t rxq_allocated;
122 uint32_t txq_allocated;
125 * Limit the number of event queues to:
127 * - hardwire maximum RSS channels
128 * - administratively specified maximum RSS channels
130 evq_max = MIN(mp_ncpus, EFX_MAXRSS);
131 if (sc->max_rss_channels > 0)
132 evq_max = MIN(evq_max, sc->max_rss_channels);
134 memset(&limits, 0, sizeof(limits));
136 limits.edl_min_evq_count = 1;
137 limits.edl_max_evq_count = evq_max;
138 limits.edl_min_txq_count = SFXGE_TXQ_NTYPES;
139 limits.edl_max_txq_count = evq_max + SFXGE_TXQ_NTYPES - 1;
140 limits.edl_min_rxq_count = 1;
141 limits.edl_max_rxq_count = evq_max;
143 efx_nic_set_drv_limits(sc->enp, &limits);
145 if ((rc = efx_nic_init(sc->enp)) != 0)
148 rc = efx_nic_get_vi_pool(sc->enp, &evq_allocated, &rxq_allocated,
151 efx_nic_fini(sc->enp);
155 KASSERT(txq_allocated >= SFXGE_TXQ_NTYPES,
156 ("txq_allocated < SFXGE_TXQ_NTYPES"));
158 sc->evq_max = MIN(evq_allocated, evq_max);
159 sc->evq_max = MIN(rxq_allocated, sc->evq_max);
160 sc->evq_max = MIN(txq_allocated - (SFXGE_TXQ_NTYPES - 1),
163 KASSERT(sc->evq_max <= evq_max,
164 ("allocated more than maximum requested"));
167 * NIC is kept initialized in the case of success to be able to
168 * initialize port to find out media types.
174 sfxge_set_drv_limits(struct sfxge_softc *sc)
176 efx_drv_limits_t limits;
178 memset(&limits, 0, sizeof(limits));
180 /* Limits are strict since take into account initial estimation */
181 limits.edl_min_evq_count = limits.edl_max_evq_count =
183 limits.edl_min_txq_count = limits.edl_max_txq_count =
184 sc->intr.n_alloc + SFXGE_TXQ_NTYPES - 1;
185 limits.edl_min_rxq_count = limits.edl_max_rxq_count =
188 return (efx_nic_set_drv_limits(sc->enp, &limits));
192 sfxge_start(struct sfxge_softc *sc)
196 SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
198 if (sc->init_state == SFXGE_STARTED)
201 if (sc->init_state != SFXGE_REGISTERED) {
206 /* Set required resource limits */
207 if ((rc = sfxge_set_drv_limits(sc)) != 0)
210 if ((rc = efx_nic_init(sc->enp)) != 0)
213 /* Start processing interrupts. */
214 if ((rc = sfxge_intr_start(sc)) != 0)
217 /* Start processing events. */
218 if ((rc = sfxge_ev_start(sc)) != 0)
221 /* Fire up the port. */
222 if ((rc = sfxge_port_start(sc)) != 0)
225 /* Start the receiver side. */
226 if ((rc = sfxge_rx_start(sc)) != 0)
229 /* Start the transmitter side. */
230 if ((rc = sfxge_tx_start(sc)) != 0)
233 sc->init_state = SFXGE_STARTED;
235 /* Tell the stack we're running. */
236 sc->ifnet->if_drv_flags |= IFF_DRV_RUNNING;
237 sc->ifnet->if_drv_flags &= ~IFF_DRV_OACTIVE;
254 efx_nic_fini(sc->enp);
257 device_printf(sc->dev, "sfxge_start: %d\n", rc);
263 sfxge_if_init(void *arg)
265 struct sfxge_softc *sc;
267 sc = (struct sfxge_softc *)arg;
269 SFXGE_ADAPTER_LOCK(sc);
270 (void)sfxge_start(sc);
271 SFXGE_ADAPTER_UNLOCK(sc);
275 sfxge_stop(struct sfxge_softc *sc)
277 SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
279 if (sc->init_state != SFXGE_STARTED)
282 sc->init_state = SFXGE_REGISTERED;
284 /* Stop the transmitter. */
287 /* Stop the receiver. */
293 /* Stop processing events. */
296 /* Stop processing interrupts. */
299 efx_nic_fini(sc->enp);
301 sc->ifnet->if_drv_flags &= ~IFF_DRV_RUNNING;
306 sfxge_vpd_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ioc)
308 efx_vpd_value_t value;
311 switch (ioc->u.vpd.op) {
312 case SFXGE_VPD_OP_GET_KEYWORD:
313 value.evv_tag = ioc->u.vpd.tag;
314 value.evv_keyword = ioc->u.vpd.keyword;
315 rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value);
318 ioc->u.vpd.len = MIN(ioc->u.vpd.len, value.evv_length);
319 if (ioc->u.vpd.payload != 0) {
320 rc = copyout(value.evv_value, ioc->u.vpd.payload,
324 case SFXGE_VPD_OP_SET_KEYWORD:
325 if (ioc->u.vpd.len > sizeof(value.evv_value))
327 value.evv_tag = ioc->u.vpd.tag;
328 value.evv_keyword = ioc->u.vpd.keyword;
329 value.evv_length = ioc->u.vpd.len;
330 rc = copyin(ioc->u.vpd.payload, value.evv_value, value.evv_length);
333 rc = efx_vpd_set(sc->enp, sc->vpd_data, sc->vpd_size, &value);
336 rc = efx_vpd_verify(sc->enp, sc->vpd_data, sc->vpd_size);
339 rc = efx_vpd_write(sc->enp, sc->vpd_data, sc->vpd_size);
350 sfxge_private_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ioc)
354 return (sfxge_mcdi_ioctl(sc, ioc));
355 case SFXGE_NVRAM_IOC:
356 return (sfxge_nvram_ioctl(sc, ioc));
358 return (sfxge_vpd_ioctl(sc, ioc));
366 sfxge_if_ioctl(struct ifnet *ifp, unsigned long command, caddr_t data)
368 struct sfxge_softc *sc;
373 ifr = (struct ifreq *)data;
379 SFXGE_ADAPTER_LOCK(sc);
380 if (ifp->if_flags & IFF_UP) {
381 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
382 if ((ifp->if_flags ^ sc->if_flags) &
383 (IFF_PROMISC | IFF_ALLMULTI)) {
384 sfxge_mac_filter_set(sc);
389 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
391 sc->if_flags = ifp->if_flags;
392 SFXGE_ADAPTER_UNLOCK(sc);
395 if (ifr->ifr_mtu == ifp->if_mtu) {
398 } else if (ifr->ifr_mtu > SFXGE_MAX_MTU) {
400 } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
401 ifp->if_mtu = ifr->ifr_mtu;
404 /* Restart required */
405 SFXGE_ADAPTER_LOCK(sc);
407 ifp->if_mtu = ifr->ifr_mtu;
408 error = sfxge_start(sc);
409 SFXGE_ADAPTER_UNLOCK(sc);
411 ifp->if_flags &= ~IFF_UP;
412 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
419 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
420 sfxge_mac_filter_set(sc);
424 int reqcap = ifr->ifr_reqcap;
427 SFXGE_ADAPTER_LOCK(sc);
429 /* Capabilities to be changed in accordance with request */
430 capchg_mask = ifp->if_capenable ^ reqcap;
433 * The networking core already rejects attempts to
434 * enable capabilities we don't have. We still have
435 * to reject attempts to disable capabilities that we
436 * can't (yet) disable.
438 KASSERT((reqcap & ~ifp->if_capabilities) == 0,
439 ("Unsupported capabilities 0x%x requested 0x%x vs "
441 reqcap & ~ifp->if_capabilities,
442 reqcap , ifp->if_capabilities));
443 if (capchg_mask & SFXGE_CAP_FIXED) {
445 SFXGE_ADAPTER_UNLOCK(sc);
449 /* Check request before any changes */
450 if ((capchg_mask & IFCAP_TSO4) &&
451 (reqcap & (IFCAP_TSO4 | IFCAP_TXCSUM)) == IFCAP_TSO4) {
453 SFXGE_ADAPTER_UNLOCK(sc);
454 if_printf(ifp, "enable txcsum before tso4\n");
457 if ((capchg_mask & IFCAP_TSO6) &&
458 (reqcap & (IFCAP_TSO6 | IFCAP_TXCSUM_IPV6)) == IFCAP_TSO6) {
460 SFXGE_ADAPTER_UNLOCK(sc);
461 if_printf(ifp, "enable txcsum6 before tso6\n");
465 if (reqcap & IFCAP_TXCSUM) {
466 ifp->if_hwassist |= (CSUM_IP | CSUM_TCP | CSUM_UDP);
468 ifp->if_hwassist &= ~(CSUM_IP | CSUM_TCP | CSUM_UDP);
469 if (reqcap & IFCAP_TSO4) {
470 reqcap &= ~IFCAP_TSO4;
472 "tso4 disabled due to -txcsum\n");
475 if (reqcap & IFCAP_TXCSUM_IPV6) {
476 ifp->if_hwassist |= (CSUM_TCP_IPV6 | CSUM_UDP_IPV6);
478 ifp->if_hwassist &= ~(CSUM_TCP_IPV6 | CSUM_UDP_IPV6);
479 if (reqcap & IFCAP_TSO6) {
480 reqcap &= ~IFCAP_TSO6;
482 "tso6 disabled due to -txcsum6\n");
487 * The kernel takes both IFCAP_TSOx and CSUM_TSO into
488 * account before using TSO. So, we do not touch
489 * checksum flags when IFCAP_TSOx is modified.
490 * Note that CSUM_TSO is (CSUM_IP_TSO|CSUM_IP6_TSO),
491 * but both bits are set in IPv4 and IPv6 mbufs.
494 ifp->if_capenable = reqcap;
496 SFXGE_ADAPTER_UNLOCK(sc);
501 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
508 error = copyin(ifr->ifr_data, &i2c, sizeof(i2c));
512 if (i2c.len > sizeof(i2c.data)) {
517 SFXGE_ADAPTER_LOCK(sc);
518 error = efx_phy_module_get_info(sc->enp, i2c.dev_addr,
521 SFXGE_ADAPTER_UNLOCK(sc);
523 error = copyout(&i2c, ifr->ifr_data, sizeof(i2c));
528 error = priv_check(curthread, PRIV_DRIVER);
531 error = copyin(ifr->ifr_data, &ioc, sizeof(ioc));
534 error = sfxge_private_ioctl(sc, &ioc);
536 error = copyout(&ioc, ifr->ifr_data, sizeof(ioc));
540 error = ether_ioctl(ifp, command, data);
547 sfxge_ifnet_fini(struct ifnet *ifp)
549 struct sfxge_softc *sc = ifp->if_softc;
551 SFXGE_ADAPTER_LOCK(sc);
553 SFXGE_ADAPTER_UNLOCK(sc);
555 ifmedia_removeall(&sc->media);
561 sfxge_ifnet_init(struct ifnet *ifp, struct sfxge_softc *sc)
563 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
570 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
571 ifp->if_init = sfxge_if_init;
573 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
574 ifp->if_ioctl = sfxge_if_ioctl;
576 ifp->if_capabilities = SFXGE_CAP;
577 ifp->if_capenable = SFXGE_CAP_ENABLE;
578 ifp->if_hw_tsomax = SFXGE_TSO_MAX_SIZE;
579 ifp->if_hw_tsomaxsegcount = SFXGE_TX_MAPPING_MAX_SEG;
580 ifp->if_hw_tsomaxsegsize = PAGE_SIZE;
583 ifp->if_capabilities |= IFCAP_LRO;
584 ifp->if_capenable |= IFCAP_LRO;
587 if (encp->enc_hw_tx_insert_vlan_enabled) {
588 ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING;
589 ifp->if_capenable |= IFCAP_VLAN_HWTAGGING;
591 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO |
592 CSUM_TCP_IPV6 | CSUM_UDP_IPV6;
594 ether_ifattach(ifp, encp->enc_mac_addr);
596 ifp->if_transmit = sfxge_if_transmit;
597 ifp->if_qflush = sfxge_if_qflush;
599 ifp->if_get_counter = sfxge_get_counter;
601 DBGPRINT(sc->dev, "ifmedia_init");
602 if ((rc = sfxge_port_ifmedia_init(sc)) != 0)
608 ether_ifdetach(sc->ifnet);
613 sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n, uint32_t *idp)
615 KASSERT(sc->buffer_table_next + n <=
616 efx_nic_cfg_get(sc->enp)->enc_buftbl_limit,
617 ("buffer table full"));
619 *idp = sc->buffer_table_next;
620 sc->buffer_table_next += n;
624 sfxge_bar_init(struct sfxge_softc *sc)
626 efsys_bar_t *esbp = &sc->bar;
628 esbp->esb_rid = PCIR_BAR(EFX_MEM_BAR);
629 if ((esbp->esb_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
630 &esbp->esb_rid, RF_ACTIVE)) == NULL) {
631 device_printf(sc->dev, "Cannot allocate BAR region %d\n",
635 esbp->esb_tag = rman_get_bustag(esbp->esb_res);
636 esbp->esb_handle = rman_get_bushandle(esbp->esb_res);
638 SFXGE_BAR_LOCK_INIT(esbp, device_get_nameunit(sc->dev));
644 sfxge_bar_fini(struct sfxge_softc *sc)
646 efsys_bar_t *esbp = &sc->bar;
648 bus_release_resource(sc->dev, SYS_RES_MEMORY, esbp->esb_rid,
650 SFXGE_BAR_LOCK_DESTROY(esbp);
654 sfxge_create(struct sfxge_softc *sc)
659 char rss_param_name[sizeof(SFXGE_PARAM(%d.max_rss_channels))];
660 #if EFSYS_OPT_MCDI_LOGGING
661 char mcdi_log_param_name[sizeof(SFXGE_PARAM(%d.mcdi_logging))];
666 SFXGE_ADAPTER_LOCK_INIT(sc, device_get_nameunit(sc->dev));
668 sc->max_rss_channels = 0;
669 snprintf(rss_param_name, sizeof(rss_param_name),
670 SFXGE_PARAM(%d.max_rss_channels),
671 (int)device_get_unit(dev));
672 TUNABLE_INT_FETCH(rss_param_name, &sc->max_rss_channels);
673 #if EFSYS_OPT_MCDI_LOGGING
674 sc->mcdi_logging = sfxge_mcdi_logging;
675 snprintf(mcdi_log_param_name, sizeof(mcdi_log_param_name),
676 SFXGE_PARAM(%d.mcdi_logging),
677 (int)device_get_unit(dev));
678 TUNABLE_INT_FETCH(mcdi_log_param_name, &sc->mcdi_logging);
681 sc->stats_node = SYSCTL_ADD_NODE(
682 device_get_sysctl_ctx(dev),
683 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
684 OID_AUTO, "stats", CTLFLAG_RD, NULL, "Statistics");
685 if (sc->stats_node == NULL) {
690 TASK_INIT(&sc->task_reset, 0, sfxge_reset, sc);
692 (void) pci_enable_busmaster(dev);
694 /* Initialize DMA mappings. */
695 DBGPRINT(sc->dev, "dma_init...");
696 if ((error = sfxge_dma_init(sc)) != 0)
699 /* Map the device registers. */
700 DBGPRINT(sc->dev, "bar_init...");
701 if ((error = sfxge_bar_init(sc)) != 0)
704 error = efx_family(pci_get_vendor(dev), pci_get_device(dev),
706 KASSERT(error == 0, ("Family should be filtered by sfxge_probe()"));
708 DBGPRINT(sc->dev, "nic_create...");
710 /* Create the common code nic object. */
711 SFXGE_EFSYS_LOCK_INIT(&sc->enp_lock,
712 device_get_nameunit(sc->dev), "nic");
713 if ((error = efx_nic_create(sc->family, (efsys_identifier_t *)sc,
714 &sc->bar, &sc->enp_lock, &enp)) != 0)
718 if (!ISP2(sfxge_rx_ring_entries) ||
719 (sfxge_rx_ring_entries < EFX_RXQ_MINNDESCS) ||
720 (sfxge_rx_ring_entries > EFX_RXQ_MAXNDESCS)) {
721 log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
722 SFXGE_PARAM_RX_RING, sfxge_rx_ring_entries,
723 EFX_RXQ_MINNDESCS, EFX_RXQ_MAXNDESCS);
725 goto fail_rx_ring_entries;
727 sc->rxq_entries = sfxge_rx_ring_entries;
729 if (!ISP2(sfxge_tx_ring_entries) ||
730 (sfxge_tx_ring_entries < EFX_TXQ_MINNDESCS) ||
731 (sfxge_tx_ring_entries > EFX_TXQ_MAXNDESCS(efx_nic_cfg_get(enp)))) {
732 log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
733 SFXGE_PARAM_TX_RING, sfxge_tx_ring_entries,
734 EFX_TXQ_MINNDESCS, EFX_TXQ_MAXNDESCS(efx_nic_cfg_get(enp)));
736 goto fail_tx_ring_entries;
738 sc->txq_entries = sfxge_tx_ring_entries;
740 /* Initialize MCDI to talk to the microcontroller. */
741 DBGPRINT(sc->dev, "mcdi_init...");
742 if ((error = sfxge_mcdi_init(sc)) != 0)
745 /* Probe the NIC and build the configuration data area. */
746 DBGPRINT(sc->dev, "nic_probe...");
747 if ((error = efx_nic_probe(enp)) != 0)
750 SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
751 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
752 OID_AUTO, "version", CTLFLAG_RD,
753 SFXGE_VERSION_STRING, 0,
756 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
757 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
758 OID_AUTO, "phy_type", CTLFLAG_RD,
759 NULL, efx_nic_cfg_get(enp)->enc_phy_type,
762 /* Initialize the NVRAM. */
763 DBGPRINT(sc->dev, "nvram_init...");
764 if ((error = efx_nvram_init(enp)) != 0)
767 /* Initialize the VPD. */
768 DBGPRINT(sc->dev, "vpd_init...");
769 if ((error = efx_vpd_init(enp)) != 0)
772 efx_mcdi_new_epoch(enp);
775 DBGPRINT(sc->dev, "nic_reset...");
776 if ((error = efx_nic_reset(enp)) != 0)
779 /* Initialize buffer table allocation. */
780 sc->buffer_table_next = 0;
783 * Guarantee minimum and estimate maximum number of event queues
784 * to take it into account when MSI-X interrupts are allocated.
785 * It initializes NIC and keeps it initialized on success.
787 if ((error = sfxge_estimate_rsrc_limits(sc)) != 0)
790 /* Set up interrupts. */
791 DBGPRINT(sc->dev, "intr_init...");
792 if ((error = sfxge_intr_init(sc)) != 0)
795 /* Initialize event processing state. */
796 DBGPRINT(sc->dev, "ev_init...");
797 if ((error = sfxge_ev_init(sc)) != 0)
800 /* Initialize port state. */
801 DBGPRINT(sc->dev, "port_init...");
802 if ((error = sfxge_port_init(sc)) != 0)
805 /* Initialize receive state. */
806 DBGPRINT(sc->dev, "rx_init...");
807 if ((error = sfxge_rx_init(sc)) != 0)
810 /* Initialize transmit state. */
811 DBGPRINT(sc->dev, "tx_init...");
812 if ((error = sfxge_tx_init(sc)) != 0)
815 sc->init_state = SFXGE_INITIALIZED;
817 DBGPRINT(sc->dev, "success");
833 efx_nic_fini(sc->enp);
842 efx_nic_unprobe(enp);
848 fail_tx_ring_entries:
849 fail_rx_ring_entries:
851 efx_nic_destroy(enp);
852 SFXGE_EFSYS_LOCK_DESTROY(&sc->enp_lock);
856 (void) pci_disable_busmaster(sc->dev);
859 DBGPRINT(sc->dev, "failed %d", error);
861 SFXGE_ADAPTER_LOCK_DESTROY(sc);
866 sfxge_destroy(struct sfxge_softc *sc)
870 /* Clean up transmit state. */
873 /* Clean up receive state. */
876 /* Clean up port state. */
879 /* Clean up event processing state. */
882 /* Clean up interrupts. */
885 /* Tear down common code subsystems. */
886 efx_nic_reset(sc->enp);
887 efx_vpd_fini(sc->enp);
888 efx_nvram_fini(sc->enp);
889 efx_nic_unprobe(sc->enp);
891 /* Tear down MCDI. */
894 /* Destroy common code context. */
897 efx_nic_destroy(enp);
899 /* Free DMA memory. */
902 /* Free mapped BARs. */
905 (void) pci_disable_busmaster(sc->dev);
907 taskqueue_drain(taskqueue_thread, &sc->task_reset);
909 /* Destroy the softc lock. */
910 SFXGE_ADAPTER_LOCK_DESTROY(sc);
914 sfxge_vpd_handler(SYSCTL_HANDLER_ARGS)
916 struct sfxge_softc *sc = arg1;
917 efx_vpd_value_t value;
920 value.evv_tag = arg2 >> 16;
921 value.evv_keyword = arg2 & 0xffff;
922 if ((rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value))
926 return (SYSCTL_OUT(req, value.evv_value, value.evv_length));
930 sfxge_vpd_try_add(struct sfxge_softc *sc, struct sysctl_oid_list *list,
931 efx_vpd_tag_t tag, const char *keyword)
933 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
934 efx_vpd_value_t value;
936 /* Check whether VPD tag/keyword is present */
938 value.evv_keyword = EFX_VPD_KEYWORD(keyword[0], keyword[1]);
939 if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) != 0)
943 ctx, list, OID_AUTO, keyword, CTLTYPE_STRING|CTLFLAG_RD,
944 sc, tag << 16 | EFX_VPD_KEYWORD(keyword[0], keyword[1]),
945 sfxge_vpd_handler, "A", "");
949 sfxge_vpd_init(struct sfxge_softc *sc)
951 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
952 struct sysctl_oid *vpd_node;
953 struct sysctl_oid_list *vpd_list;
955 efx_vpd_value_t value;
958 if ((rc = efx_vpd_size(sc->enp, &sc->vpd_size)) != 0) {
960 * Unpriviledged functions deny VPD access.
961 * Simply skip VPD in this case.
967 sc->vpd_data = malloc(sc->vpd_size, M_SFXGE, M_WAITOK);
968 if ((rc = efx_vpd_read(sc->enp, sc->vpd_data, sc->vpd_size)) != 0)
971 /* Copy ID (product name) into device description, and log it. */
972 value.evv_tag = EFX_VPD_ID;
973 if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) == 0) {
974 value.evv_value[value.evv_length] = 0;
975 device_set_desc_copy(sc->dev, value.evv_value);
976 device_printf(sc->dev, "%s\n", value.evv_value);
979 vpd_node = SYSCTL_ADD_NODE(
980 ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
981 OID_AUTO, "vpd", CTLFLAG_RD, NULL, "Vital Product Data");
982 vpd_list = SYSCTL_CHILDREN(vpd_node);
984 /* Add sysctls for all expected and any vendor-defined keywords. */
985 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "PN");
986 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "EC");
987 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "SN");
990 for (keyword[1] = '0'; keyword[1] <= '9'; keyword[1]++)
991 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
992 for (keyword[1] = 'A'; keyword[1] <= 'Z'; keyword[1]++)
993 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
999 free(sc->vpd_data, M_SFXGE);
1005 sfxge_vpd_fini(struct sfxge_softc *sc)
1007 free(sc->vpd_data, M_SFXGE);
1011 sfxge_reset(void *arg, int npending)
1013 struct sfxge_softc *sc;
1019 sc = (struct sfxge_softc *)arg;
1021 SFXGE_ADAPTER_LOCK(sc);
1023 if (sc->init_state != SFXGE_STARTED)
1027 efx_nic_reset(sc->enp);
1028 for (attempt = 0; attempt < sfxge_restart_attempts; ++attempt) {
1029 if ((rc = sfxge_start(sc)) == 0)
1032 device_printf(sc->dev, "start on reset failed (%d)\n", rc);
1036 device_printf(sc->dev, "reset failed; interface is now stopped\n");
1039 SFXGE_ADAPTER_UNLOCK(sc);
1043 sfxge_schedule_reset(struct sfxge_softc *sc)
1045 taskqueue_enqueue(taskqueue_thread, &sc->task_reset);
1049 sfxge_attach(device_t dev)
1051 struct sfxge_softc *sc;
1055 sc = device_get_softc(dev);
1058 /* Allocate ifnet. */
1059 ifp = if_alloc(IFT_ETHER);
1061 device_printf(dev, "Couldn't allocate ifnet\n");
1067 /* Initialize hardware. */
1068 DBGPRINT(sc->dev, "create nic");
1069 if ((error = sfxge_create(sc)) != 0)
1072 /* Create the ifnet for the port. */
1073 DBGPRINT(sc->dev, "init ifnet");
1074 if ((error = sfxge_ifnet_init(ifp, sc)) != 0)
1077 DBGPRINT(sc->dev, "init vpd");
1078 if ((error = sfxge_vpd_init(sc)) != 0)
1082 * NIC is initialized inside sfxge_create() and kept inialized
1083 * to be able to initialize port to discover media types in
1084 * sfxge_ifnet_init().
1086 efx_nic_fini(sc->enp);
1088 sc->init_state = SFXGE_REGISTERED;
1090 DBGPRINT(sc->dev, "success");
1094 sfxge_ifnet_fini(ifp);
1096 efx_nic_fini(sc->enp);
1103 DBGPRINT(sc->dev, "failed %d", error);
1108 sfxge_detach(device_t dev)
1110 struct sfxge_softc *sc;
1112 sc = device_get_softc(dev);
1116 /* Destroy the ifnet. */
1117 sfxge_ifnet_fini(sc->ifnet);
1119 /* Tear down hardware. */
1126 sfxge_probe(device_t dev)
1128 uint16_t pci_vendor_id;
1129 uint16_t pci_device_id;
1130 efx_family_t family;
1133 pci_vendor_id = pci_get_vendor(dev);
1134 pci_device_id = pci_get_device(dev);
1136 DBGPRINT(dev, "PCI ID %04x:%04x", pci_vendor_id, pci_device_id);
1137 rc = efx_family(pci_vendor_id, pci_device_id, &family);
1139 DBGPRINT(dev, "efx_family fail %d", rc);
1143 if (family == EFX_FAMILY_SIENA) {
1144 device_set_desc(dev, "Solarflare SFC9000 family");
1148 if (family == EFX_FAMILY_HUNTINGTON) {
1149 device_set_desc(dev, "Solarflare SFC9100 family");
1153 if (family == EFX_FAMILY_MEDFORD) {
1154 device_set_desc(dev, "Solarflare SFC9200 family");
1158 DBGPRINT(dev, "impossible controller family %d", family);
1162 static device_method_t sfxge_methods[] = {
1163 DEVMETHOD(device_probe, sfxge_probe),
1164 DEVMETHOD(device_attach, sfxge_attach),
1165 DEVMETHOD(device_detach, sfxge_detach),
1170 static devclass_t sfxge_devclass;
1172 static driver_t sfxge_driver = {
1175 sizeof(struct sfxge_softc)
1178 DRIVER_MODULE(sfxge, pci, sfxge_driver, sfxge_devclass, 0, 0);