2 * Copyright (c) 2010-2015 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_STATS_UPDATE_PERIOD SFXGE_PARAM(stats_update_period)
98 static int sfxge_stats_update_period = SFXGE_CALLOUT_TICKS;
99 TUNABLE_INT(SFXGE_PARAM_STATS_UPDATE_PERIOD,
100 &sfxge_stats_update_period);
101 SYSCTL_INT(_hw_sfxge, OID_AUTO, stats_update_period, CTLFLAG_RDTUN,
102 &sfxge_stats_update_period, 0,
103 "netstat interface statistics update period in ticks");
106 sfxge_reset(void *arg, int npending);
109 sfxge_estimate_rsrc_limits(struct sfxge_softc *sc)
111 efx_drv_limits_t limits;
113 unsigned int evq_max;
114 uint32_t evq_allocated;
115 uint32_t rxq_allocated;
116 uint32_t txq_allocated;
119 * Limit the number of event queues to:
121 * - hardwire maximum RSS channels
122 * - administratively specified maximum RSS channels
124 evq_max = MIN(mp_ncpus, EFX_MAXRSS);
125 if (sc->max_rss_channels > 0)
126 evq_max = MIN(evq_max, sc->max_rss_channels);
128 memset(&limits, 0, sizeof(limits));
130 limits.edl_min_evq_count = 1;
131 limits.edl_max_evq_count = evq_max;
132 limits.edl_min_txq_count = SFXGE_TXQ_NTYPES;
133 limits.edl_max_txq_count = evq_max + SFXGE_TXQ_NTYPES - 1;
134 limits.edl_min_rxq_count = 1;
135 limits.edl_max_rxq_count = evq_max;
137 efx_nic_set_drv_limits(sc->enp, &limits);
139 if ((rc = efx_nic_init(sc->enp)) != 0)
142 rc = efx_nic_get_vi_pool(sc->enp, &evq_allocated, &rxq_allocated,
145 efx_nic_fini(sc->enp);
149 KASSERT(txq_allocated >= SFXGE_TXQ_NTYPES,
150 ("txq_allocated < SFXGE_TXQ_NTYPES"));
152 sc->evq_max = MIN(evq_allocated, evq_max);
153 sc->evq_max = MIN(rxq_allocated, sc->evq_max);
154 sc->evq_max = MIN(txq_allocated - (SFXGE_TXQ_NTYPES - 1),
157 KASSERT(sc->evq_max <= evq_max,
158 ("allocated more than maximum requested"));
161 * NIC is kept initialized in the case of success to be able to
162 * initialize port to find out media types.
168 sfxge_set_drv_limits(struct sfxge_softc *sc)
170 efx_drv_limits_t limits;
172 memset(&limits, 0, sizeof(limits));
174 /* Limits are strict since take into account initial estimation */
175 limits.edl_min_evq_count = limits.edl_max_evq_count =
177 limits.edl_min_txq_count = limits.edl_max_txq_count =
178 sc->intr.n_alloc + SFXGE_TXQ_NTYPES - 1;
179 limits.edl_min_rxq_count = limits.edl_max_rxq_count =
182 return (efx_nic_set_drv_limits(sc->enp, &limits));
186 sfxge_start(struct sfxge_softc *sc)
190 SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
192 if (sc->init_state == SFXGE_STARTED)
195 if (sc->init_state != SFXGE_REGISTERED) {
200 /* Set required resource limits */
201 if ((rc = sfxge_set_drv_limits(sc)) != 0)
204 if ((rc = efx_nic_init(sc->enp)) != 0)
207 /* Start processing interrupts. */
208 if ((rc = sfxge_intr_start(sc)) != 0)
211 /* Start processing events. */
212 if ((rc = sfxge_ev_start(sc)) != 0)
215 /* Fire up the port. */
216 if ((rc = sfxge_port_start(sc)) != 0)
219 /* Start the receiver side. */
220 if ((rc = sfxge_rx_start(sc)) != 0)
223 /* Start the transmitter side. */
224 if ((rc = sfxge_tx_start(sc)) != 0)
227 sc->init_state = SFXGE_STARTED;
229 /* Tell the stack we're running. */
230 sc->ifnet->if_drv_flags |= IFF_DRV_RUNNING;
231 sc->ifnet->if_drv_flags &= ~IFF_DRV_OACTIVE;
248 efx_nic_fini(sc->enp);
251 device_printf(sc->dev, "sfxge_start: %d\n", rc);
257 sfxge_if_init(void *arg)
259 struct sfxge_softc *sc;
261 sc = (struct sfxge_softc *)arg;
263 SFXGE_ADAPTER_LOCK(sc);
264 (void)sfxge_start(sc);
265 SFXGE_ADAPTER_UNLOCK(sc);
269 sfxge_stop(struct sfxge_softc *sc)
271 SFXGE_ADAPTER_LOCK_ASSERT_OWNED(sc);
273 if (sc->init_state != SFXGE_STARTED)
276 sc->init_state = SFXGE_REGISTERED;
278 /* Stop the transmitter. */
281 /* Stop the receiver. */
287 /* Stop processing events. */
290 /* Stop processing interrupts. */
293 efx_nic_fini(sc->enp);
295 sc->ifnet->if_drv_flags &= ~IFF_DRV_RUNNING;
300 sfxge_vpd_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ioc)
302 efx_vpd_value_t value;
305 switch (ioc->u.vpd.op) {
306 case SFXGE_VPD_OP_GET_KEYWORD:
307 value.evv_tag = ioc->u.vpd.tag;
308 value.evv_keyword = ioc->u.vpd.keyword;
309 rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value);
312 ioc->u.vpd.len = MIN(ioc->u.vpd.len, value.evv_length);
313 if (ioc->u.vpd.payload != 0) {
314 rc = copyout(value.evv_value, ioc->u.vpd.payload,
318 case SFXGE_VPD_OP_SET_KEYWORD:
319 if (ioc->u.vpd.len > sizeof(value.evv_value))
321 value.evv_tag = ioc->u.vpd.tag;
322 value.evv_keyword = ioc->u.vpd.keyword;
323 value.evv_length = ioc->u.vpd.len;
324 rc = copyin(ioc->u.vpd.payload, value.evv_value, value.evv_length);
327 rc = efx_vpd_set(sc->enp, sc->vpd_data, sc->vpd_size, &value);
330 rc = efx_vpd_verify(sc->enp, sc->vpd_data, sc->vpd_size);
333 rc = efx_vpd_write(sc->enp, sc->vpd_data, sc->vpd_size);
344 sfxge_private_ioctl(struct sfxge_softc *sc, sfxge_ioc_t *ioc)
348 return (sfxge_mcdi_ioctl(sc, ioc));
349 case SFXGE_NVRAM_IOC:
350 return (sfxge_nvram_ioctl(sc, ioc));
352 return (sfxge_vpd_ioctl(sc, ioc));
360 sfxge_if_ioctl(struct ifnet *ifp, unsigned long command, caddr_t data)
362 struct sfxge_softc *sc;
367 ifr = (struct ifreq *)data;
373 SFXGE_ADAPTER_LOCK(sc);
374 if (ifp->if_flags & IFF_UP) {
375 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
376 if ((ifp->if_flags ^ sc->if_flags) &
377 (IFF_PROMISC | IFF_ALLMULTI)) {
378 sfxge_mac_filter_set(sc);
383 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
385 sc->if_flags = ifp->if_flags;
386 SFXGE_ADAPTER_UNLOCK(sc);
389 if (ifr->ifr_mtu == ifp->if_mtu) {
392 } else if (ifr->ifr_mtu > SFXGE_MAX_MTU) {
394 } else if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
395 ifp->if_mtu = ifr->ifr_mtu;
398 /* Restart required */
399 SFXGE_ADAPTER_LOCK(sc);
401 ifp->if_mtu = ifr->ifr_mtu;
402 error = sfxge_start(sc);
403 SFXGE_ADAPTER_UNLOCK(sc);
405 ifp->if_flags &= ~IFF_UP;
406 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
413 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
414 sfxge_mac_filter_set(sc);
418 int reqcap = ifr->ifr_reqcap;
421 SFXGE_ADAPTER_LOCK(sc);
423 /* Capabilities to be changed in accordance with request */
424 capchg_mask = ifp->if_capenable ^ reqcap;
427 * The networking core already rejects attempts to
428 * enable capabilities we don't have. We still have
429 * to reject attempts to disable capabilities that we
430 * can't (yet) disable.
432 KASSERT((reqcap & ~ifp->if_capabilities) == 0,
433 ("Unsupported capabilities 0x%x requested 0x%x vs "
435 reqcap & ~ifp->if_capabilities,
436 reqcap , ifp->if_capabilities));
437 if (capchg_mask & SFXGE_CAP_FIXED) {
439 SFXGE_ADAPTER_UNLOCK(sc);
443 /* Check request before any changes */
444 if ((capchg_mask & IFCAP_TSO4) &&
445 (reqcap & (IFCAP_TSO4 | IFCAP_TXCSUM)) == IFCAP_TSO4) {
447 SFXGE_ADAPTER_UNLOCK(sc);
448 if_printf(ifp, "enable txcsum before tso4\n");
451 if ((capchg_mask & IFCAP_TSO6) &&
452 (reqcap & (IFCAP_TSO6 | IFCAP_TXCSUM_IPV6)) == IFCAP_TSO6) {
454 SFXGE_ADAPTER_UNLOCK(sc);
455 if_printf(ifp, "enable txcsum6 before tso6\n");
459 if (reqcap & IFCAP_TXCSUM) {
460 ifp->if_hwassist |= (CSUM_IP | CSUM_TCP | CSUM_UDP);
462 ifp->if_hwassist &= ~(CSUM_IP | CSUM_TCP | CSUM_UDP);
463 if (reqcap & IFCAP_TSO4) {
464 reqcap &= ~IFCAP_TSO4;
466 "tso4 disabled due to -txcsum\n");
469 if (reqcap & IFCAP_TXCSUM_IPV6) {
470 ifp->if_hwassist |= (CSUM_TCP_IPV6 | CSUM_UDP_IPV6);
472 ifp->if_hwassist &= ~(CSUM_TCP_IPV6 | CSUM_UDP_IPV6);
473 if (reqcap & IFCAP_TSO6) {
474 reqcap &= ~IFCAP_TSO6;
476 "tso6 disabled due to -txcsum6\n");
481 * The kernel takes both IFCAP_TSOx and CSUM_TSO into
482 * account before using TSO. So, we do not touch
483 * checksum flags when IFCAP_TSOx is modified.
484 * Note that CSUM_TSO is (CSUM_IP_TSO|CSUM_IP6_TSO),
485 * but both bits are set in IPv4 and IPv6 mbufs.
488 ifp->if_capenable = reqcap;
490 SFXGE_ADAPTER_UNLOCK(sc);
495 error = ifmedia_ioctl(ifp, ifr, &sc->media, command);
498 error = priv_check(curthread, PRIV_DRIVER);
501 error = copyin(ifr->ifr_data, &ioc, sizeof(ioc));
504 error = sfxge_private_ioctl(sc, &ioc);
506 error = copyout(&ioc, ifr->ifr_data, sizeof(ioc));
510 error = ether_ioctl(ifp, command, data);
517 sfxge_tick(void *arg)
519 struct sfxge_softc *sc = arg;
521 sfxge_port_update_stats(sc);
522 sfxge_tx_update_stats(sc);
524 callout_reset(&sc->tick_callout, sfxge_stats_update_period,
529 sfxge_ifnet_fini(struct ifnet *ifp)
531 struct sfxge_softc *sc = ifp->if_softc;
533 callout_drain(&sc->tick_callout);
535 SFXGE_ADAPTER_LOCK(sc);
537 SFXGE_ADAPTER_UNLOCK(sc);
539 ifmedia_removeall(&sc->media);
545 sfxge_ifnet_init(struct ifnet *ifp, struct sfxge_softc *sc)
547 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sc->enp);
554 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
555 ifp->if_init = sfxge_if_init;
557 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
558 ifp->if_ioctl = sfxge_if_ioctl;
560 ifp->if_capabilities = SFXGE_CAP;
561 ifp->if_capenable = SFXGE_CAP_ENABLE;
564 ifp->if_capabilities |= IFCAP_LRO;
565 ifp->if_capenable |= IFCAP_LRO;
568 if (encp->enc_hw_tx_insert_vlan_enabled) {
569 ifp->if_capabilities |= IFCAP_VLAN_HWTAGGING;
570 ifp->if_capenable |= IFCAP_VLAN_HWTAGGING;
572 ifp->if_hwassist = CSUM_TCP | CSUM_UDP | CSUM_IP | CSUM_TSO |
573 CSUM_TCP_IPV6 | CSUM_UDP_IPV6;
575 ether_ifattach(ifp, encp->enc_mac_addr);
577 ifp->if_transmit = sfxge_if_transmit;
578 ifp->if_qflush = sfxge_if_qflush;
580 callout_init(&sc->tick_callout, B_TRUE);
582 DBGPRINT(sc->dev, "ifmedia_init");
583 if ((rc = sfxge_port_ifmedia_init(sc)) != 0)
586 callout_reset(&sc->tick_callout, sfxge_stats_update_period,
592 ether_ifdetach(sc->ifnet);
597 sfxge_sram_buf_tbl_alloc(struct sfxge_softc *sc, size_t n, uint32_t *idp)
599 KASSERT(sc->buffer_table_next + n <=
600 efx_nic_cfg_get(sc->enp)->enc_buftbl_limit,
601 ("buffer table full"));
603 *idp = sc->buffer_table_next;
604 sc->buffer_table_next += n;
608 sfxge_bar_init(struct sfxge_softc *sc)
610 efsys_bar_t *esbp = &sc->bar;
612 esbp->esb_rid = PCIR_BAR(EFX_MEM_BAR);
613 if ((esbp->esb_res = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY,
614 &esbp->esb_rid, RF_ACTIVE)) == NULL) {
615 device_printf(sc->dev, "Cannot allocate BAR region %d\n",
619 esbp->esb_tag = rman_get_bustag(esbp->esb_res);
620 esbp->esb_handle = rman_get_bushandle(esbp->esb_res);
622 SFXGE_BAR_LOCK_INIT(esbp, device_get_nameunit(sc->dev));
628 sfxge_bar_fini(struct sfxge_softc *sc)
630 efsys_bar_t *esbp = &sc->bar;
632 bus_release_resource(sc->dev, SYS_RES_MEMORY, esbp->esb_rid,
634 SFXGE_BAR_LOCK_DESTROY(esbp);
638 sfxge_create(struct sfxge_softc *sc)
643 char rss_param_name[sizeof(SFXGE_PARAM(%d.max_rss_channels))];
647 SFXGE_ADAPTER_LOCK_INIT(sc, device_get_nameunit(sc->dev));
649 sc->max_rss_channels = 0;
650 snprintf(rss_param_name, sizeof(rss_param_name),
651 SFXGE_PARAM(%d.max_rss_channels),
652 (int)device_get_unit(dev));
653 TUNABLE_INT_FETCH(rss_param_name, &sc->max_rss_channels);
655 sc->stats_node = SYSCTL_ADD_NODE(
656 device_get_sysctl_ctx(dev),
657 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
658 OID_AUTO, "stats", CTLFLAG_RD, NULL, "Statistics");
659 if (sc->stats_node == NULL) {
664 TASK_INIT(&sc->task_reset, 0, sfxge_reset, sc);
666 (void) pci_enable_busmaster(dev);
668 /* Initialize DMA mappings. */
669 DBGPRINT(sc->dev, "dma_init...");
670 if ((error = sfxge_dma_init(sc)) != 0)
673 /* Map the device registers. */
674 DBGPRINT(sc->dev, "bar_init...");
675 if ((error = sfxge_bar_init(sc)) != 0)
678 error = efx_family(pci_get_vendor(dev), pci_get_device(dev),
680 KASSERT(error == 0, ("Family should be filtered by sfxge_probe()"));
682 DBGPRINT(sc->dev, "nic_create...");
684 /* Create the common code nic object. */
685 SFXGE_EFSYS_LOCK_INIT(&sc->enp_lock,
686 device_get_nameunit(sc->dev), "nic");
687 if ((error = efx_nic_create(sc->family, (efsys_identifier_t *)sc,
688 &sc->bar, &sc->enp_lock, &enp)) != 0)
692 if (!ISP2(sfxge_rx_ring_entries) ||
693 (sfxge_rx_ring_entries < EFX_RXQ_MINNDESCS) ||
694 (sfxge_rx_ring_entries > EFX_RXQ_MAXNDESCS)) {
695 log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
696 SFXGE_PARAM_RX_RING, sfxge_rx_ring_entries,
697 EFX_RXQ_MINNDESCS, EFX_RXQ_MAXNDESCS);
699 goto fail_rx_ring_entries;
701 sc->rxq_entries = sfxge_rx_ring_entries;
703 if (!ISP2(sfxge_tx_ring_entries) ||
704 (sfxge_tx_ring_entries < EFX_TXQ_MINNDESCS) ||
705 (sfxge_tx_ring_entries > EFX_TXQ_MAXNDESCS(efx_nic_cfg_get(enp)))) {
706 log(LOG_ERR, "%s=%d must be power of 2 from %u to %u",
707 SFXGE_PARAM_TX_RING, sfxge_tx_ring_entries,
708 EFX_TXQ_MINNDESCS, EFX_TXQ_MAXNDESCS(efx_nic_cfg_get(enp)));
710 goto fail_tx_ring_entries;
712 sc->txq_entries = sfxge_tx_ring_entries;
714 /* Initialize MCDI to talk to the microcontroller. */
715 DBGPRINT(sc->dev, "mcdi_init...");
716 if ((error = sfxge_mcdi_init(sc)) != 0)
719 /* Probe the NIC and build the configuration data area. */
720 DBGPRINT(sc->dev, "nic_probe...");
721 if ((error = efx_nic_probe(enp)) != 0)
724 SYSCTL_ADD_STRING(device_get_sysctl_ctx(dev),
725 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
726 OID_AUTO, "version", CTLFLAG_RD,
727 SFXGE_VERSION_STRING, 0,
730 SYSCTL_ADD_UINT(device_get_sysctl_ctx(dev),
731 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
732 OID_AUTO, "phy_type", CTLFLAG_RD,
733 NULL, efx_nic_cfg_get(enp)->enc_phy_type,
736 /* Initialize the NVRAM. */
737 DBGPRINT(sc->dev, "nvram_init...");
738 if ((error = efx_nvram_init(enp)) != 0)
741 /* Initialize the VPD. */
742 DBGPRINT(sc->dev, "vpd_init...");
743 if ((error = efx_vpd_init(enp)) != 0)
746 efx_mcdi_new_epoch(enp);
749 DBGPRINT(sc->dev, "nic_reset...");
750 if ((error = efx_nic_reset(enp)) != 0)
753 /* Initialize buffer table allocation. */
754 sc->buffer_table_next = 0;
757 * Guarantee minimum and estimate maximum number of event queues
758 * to take it into account when MSI-X interrupts are allocated.
759 * It initializes NIC and keeps it initialized on success.
761 if ((error = sfxge_estimate_rsrc_limits(sc)) != 0)
764 /* Set up interrupts. */
765 DBGPRINT(sc->dev, "intr_init...");
766 if ((error = sfxge_intr_init(sc)) != 0)
769 /* Initialize event processing state. */
770 DBGPRINT(sc->dev, "ev_init...");
771 if ((error = sfxge_ev_init(sc)) != 0)
774 /* Initialize port state. */
775 DBGPRINT(sc->dev, "port_init...");
776 if ((error = sfxge_port_init(sc)) != 0)
779 /* Initialize receive state. */
780 DBGPRINT(sc->dev, "rx_init...");
781 if ((error = sfxge_rx_init(sc)) != 0)
784 /* Initialize transmit state. */
785 DBGPRINT(sc->dev, "tx_init...");
786 if ((error = sfxge_tx_init(sc)) != 0)
789 sc->init_state = SFXGE_INITIALIZED;
791 DBGPRINT(sc->dev, "success");
807 efx_nic_fini(sc->enp);
816 efx_nic_unprobe(enp);
822 fail_tx_ring_entries:
823 fail_rx_ring_entries:
825 efx_nic_destroy(enp);
826 SFXGE_EFSYS_LOCK_DESTROY(&sc->enp_lock);
830 (void) pci_disable_busmaster(sc->dev);
833 DBGPRINT(sc->dev, "failed %d", error);
835 SFXGE_ADAPTER_LOCK_DESTROY(sc);
840 sfxge_destroy(struct sfxge_softc *sc)
844 /* Clean up transmit state. */
847 /* Clean up receive state. */
850 /* Clean up port state. */
853 /* Clean up event processing state. */
856 /* Clean up interrupts. */
859 /* Tear down common code subsystems. */
860 efx_nic_reset(sc->enp);
861 efx_vpd_fini(sc->enp);
862 efx_nvram_fini(sc->enp);
863 efx_nic_unprobe(sc->enp);
865 /* Tear down MCDI. */
868 /* Destroy common code context. */
871 efx_nic_destroy(enp);
873 /* Free DMA memory. */
876 /* Free mapped BARs. */
879 (void) pci_disable_busmaster(sc->dev);
881 taskqueue_drain(taskqueue_thread, &sc->task_reset);
883 /* Destroy the softc lock. */
884 SFXGE_ADAPTER_LOCK_DESTROY(sc);
888 sfxge_vpd_handler(SYSCTL_HANDLER_ARGS)
890 struct sfxge_softc *sc = arg1;
891 efx_vpd_value_t value;
894 value.evv_tag = arg2 >> 16;
895 value.evv_keyword = arg2 & 0xffff;
896 if ((rc = efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value))
900 return (SYSCTL_OUT(req, value.evv_value, value.evv_length));
904 sfxge_vpd_try_add(struct sfxge_softc *sc, struct sysctl_oid_list *list,
905 efx_vpd_tag_t tag, const char *keyword)
907 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
908 efx_vpd_value_t value;
910 /* Check whether VPD tag/keyword is present */
912 value.evv_keyword = EFX_VPD_KEYWORD(keyword[0], keyword[1]);
913 if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) != 0)
917 ctx, list, OID_AUTO, keyword, CTLTYPE_STRING|CTLFLAG_RD,
918 sc, tag << 16 | EFX_VPD_KEYWORD(keyword[0], keyword[1]),
919 sfxge_vpd_handler, "A", "");
923 sfxge_vpd_init(struct sfxge_softc *sc)
925 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->dev);
926 struct sysctl_oid *vpd_node;
927 struct sysctl_oid_list *vpd_list;
929 efx_vpd_value_t value;
932 if ((rc = efx_vpd_size(sc->enp, &sc->vpd_size)) != 0) {
934 * Unpriviledged functions deny VPD access.
935 * Simply skip VPD in this case.
941 sc->vpd_data = malloc(sc->vpd_size, M_SFXGE, M_WAITOK);
942 if ((rc = efx_vpd_read(sc->enp, sc->vpd_data, sc->vpd_size)) != 0)
945 /* Copy ID (product name) into device description, and log it. */
946 value.evv_tag = EFX_VPD_ID;
947 if (efx_vpd_get(sc->enp, sc->vpd_data, sc->vpd_size, &value) == 0) {
948 value.evv_value[value.evv_length] = 0;
949 device_set_desc_copy(sc->dev, value.evv_value);
950 device_printf(sc->dev, "%s\n", value.evv_value);
953 vpd_node = SYSCTL_ADD_NODE(
954 ctx, SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)),
955 OID_AUTO, "vpd", CTLFLAG_RD, NULL, "Vital Product Data");
956 vpd_list = SYSCTL_CHILDREN(vpd_node);
958 /* Add sysctls for all expected and any vendor-defined keywords. */
959 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "PN");
960 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "EC");
961 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, "SN");
964 for (keyword[1] = '0'; keyword[1] <= '9'; keyword[1]++)
965 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
966 for (keyword[1] = 'A'; keyword[1] <= 'Z'; keyword[1]++)
967 sfxge_vpd_try_add(sc, vpd_list, EFX_VPD_RO, keyword);
973 free(sc->vpd_data, M_SFXGE);
979 sfxge_vpd_fini(struct sfxge_softc *sc)
981 free(sc->vpd_data, M_SFXGE);
985 sfxge_reset(void *arg, int npending)
987 struct sfxge_softc *sc;
993 sc = (struct sfxge_softc *)arg;
995 SFXGE_ADAPTER_LOCK(sc);
997 if (sc->init_state != SFXGE_STARTED)
1001 efx_nic_reset(sc->enp);
1002 for (attempt = 0; attempt < 3; ++attempt) {
1003 if ((rc = sfxge_start(sc)) == 0)
1006 device_printf(sc->dev, "start on reset failed (%d)\n", rc);
1010 device_printf(sc->dev, "reset failed; interface is now stopped\n");
1013 SFXGE_ADAPTER_UNLOCK(sc);
1017 sfxge_schedule_reset(struct sfxge_softc *sc)
1019 taskqueue_enqueue(taskqueue_thread, &sc->task_reset);
1023 sfxge_attach(device_t dev)
1025 struct sfxge_softc *sc;
1029 sc = device_get_softc(dev);
1032 /* Allocate ifnet. */
1033 ifp = if_alloc(IFT_ETHER);
1035 device_printf(dev, "Couldn't allocate ifnet\n");
1041 /* Initialize hardware. */
1042 DBGPRINT(sc->dev, "create nic");
1043 if ((error = sfxge_create(sc)) != 0)
1046 /* Create the ifnet for the port. */
1047 DBGPRINT(sc->dev, "init ifnet");
1048 if ((error = sfxge_ifnet_init(ifp, sc)) != 0)
1051 DBGPRINT(sc->dev, "init vpd");
1052 if ((error = sfxge_vpd_init(sc)) != 0)
1056 * NIC is initialized inside sfxge_create() and kept inialized
1057 * to be able to initialize port to discover media types in
1058 * sfxge_ifnet_init().
1060 efx_nic_fini(sc->enp);
1062 sc->init_state = SFXGE_REGISTERED;
1064 DBGPRINT(sc->dev, "success");
1068 sfxge_ifnet_fini(ifp);
1070 efx_nic_fini(sc->enp);
1077 DBGPRINT(sc->dev, "failed %d", error);
1082 sfxge_detach(device_t dev)
1084 struct sfxge_softc *sc;
1086 sc = device_get_softc(dev);
1090 /* Destroy the ifnet. */
1091 sfxge_ifnet_fini(sc->ifnet);
1093 /* Tear down hardware. */
1100 sfxge_probe(device_t dev)
1102 uint16_t pci_vendor_id;
1103 uint16_t pci_device_id;
1104 efx_family_t family;
1107 pci_vendor_id = pci_get_vendor(dev);
1108 pci_device_id = pci_get_device(dev);
1110 DBGPRINT(dev, "PCI ID %04x:%04x", pci_vendor_id, pci_device_id);
1111 rc = efx_family(pci_vendor_id, pci_device_id, &family);
1113 DBGPRINT(dev, "efx_family fail %d", rc);
1117 if (family == EFX_FAMILY_SIENA) {
1118 device_set_desc(dev, "Solarflare SFC9000 family");
1122 if (family == EFX_FAMILY_HUNTINGTON) {
1123 device_set_desc(dev, "Solarflare SFC9100 family");
1127 DBGPRINT(dev, "impossible controller family %d", family);
1131 static device_method_t sfxge_methods[] = {
1132 DEVMETHOD(device_probe, sfxge_probe),
1133 DEVMETHOD(device_attach, sfxge_attach),
1134 DEVMETHOD(device_detach, sfxge_detach),
1139 static devclass_t sfxge_devclass;
1141 static driver_t sfxge_driver = {
1144 sizeof(struct sfxge_softc)
1147 DRIVER_MODULE(sfxge, pci, sfxge_driver, sfxge_devclass, 0, 0);