1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright (c) 2016-2018 Solarflare Communications Inc.
6 * This software was jointly developed between OKTET Labs (under contract
7 * for Solarflare) and Solarflare Communications, Inc.
13 #include <rte_errno.h>
14 #include <rte_alarm.h>
26 sfc_dma_alloc(const struct sfc_adapter *sa, const char *name, uint16_t id,
27 size_t len, int socket_id, efsys_mem_t *esmp)
29 const struct rte_memzone *mz;
31 sfc_log_init(sa, "name=%s id=%u len=%lu socket_id=%d",
32 name, id, len, socket_id);
34 mz = rte_eth_dma_zone_reserve(sa->eth_dev, name, id, len,
35 sysconf(_SC_PAGESIZE), socket_id);
37 sfc_err(sa, "cannot reserve DMA zone for %s:%u %#x@%d: %s",
38 name, (unsigned int)id, (unsigned int)len, socket_id,
39 rte_strerror(rte_errno));
43 esmp->esm_addr = mz->iova;
44 if (esmp->esm_addr == RTE_BAD_IOVA) {
45 (void)rte_memzone_free(mz);
50 esmp->esm_base = mz->addr;
56 sfc_dma_free(const struct sfc_adapter *sa, efsys_mem_t *esmp)
60 sfc_log_init(sa, "name=%s", esmp->esm_mz->name);
62 rc = rte_memzone_free(esmp->esm_mz);
64 sfc_err(sa, "rte_memzone_free(() failed: %d", rc);
66 memset(esmp, 0, sizeof(*esmp));
70 sfc_phy_cap_from_link_speeds(uint32_t speeds)
72 uint32_t phy_caps = 0;
74 if (~speeds & ETH_LINK_SPEED_FIXED) {
75 phy_caps |= (1 << EFX_PHY_CAP_AN);
77 * If no speeds are specified in the mask, any supported
80 if (speeds == ETH_LINK_SPEED_AUTONEG)
82 (1 << EFX_PHY_CAP_1000FDX) |
83 (1 << EFX_PHY_CAP_10000FDX) |
84 (1 << EFX_PHY_CAP_40000FDX);
86 if (speeds & ETH_LINK_SPEED_1G)
87 phy_caps |= (1 << EFX_PHY_CAP_1000FDX);
88 if (speeds & ETH_LINK_SPEED_10G)
89 phy_caps |= (1 << EFX_PHY_CAP_10000FDX);
90 if (speeds & ETH_LINK_SPEED_40G)
91 phy_caps |= (1 << EFX_PHY_CAP_40000FDX);
97 * Check requested device level configuration.
98 * Receive and transmit configuration is checked in corresponding
102 sfc_check_conf(struct sfc_adapter *sa)
104 const struct rte_eth_conf *conf = &sa->eth_dev->data->dev_conf;
107 sa->port.phy_adv_cap =
108 sfc_phy_cap_from_link_speeds(conf->link_speeds) &
109 sa->port.phy_adv_cap_mask;
110 if ((sa->port.phy_adv_cap & ~(1 << EFX_PHY_CAP_AN)) == 0) {
111 sfc_err(sa, "No link speeds from mask %#x are supported",
116 if (conf->lpbk_mode != 0) {
117 sfc_err(sa, "Loopback not supported");
121 if (conf->dcb_capability_en != 0) {
122 sfc_err(sa, "Priority-based flow control not supported");
126 if (conf->fdir_conf.mode != RTE_FDIR_MODE_NONE) {
127 sfc_err(sa, "Flow Director not supported");
131 if ((conf->intr_conf.lsc != 0) &&
132 (sa->intr.type != EFX_INTR_LINE) &&
133 (sa->intr.type != EFX_INTR_MESSAGE)) {
134 sfc_err(sa, "Link status change interrupt not supported");
138 if (conf->intr_conf.rxq != 0) {
139 sfc_err(sa, "Receive queue interrupt not supported");
147 * Find out maximum number of receive and transmit queues which could be
150 * NIC is kept initialized on success to allow other modules acquire
151 * defaults and capabilities.
154 sfc_estimate_resource_limits(struct sfc_adapter *sa)
156 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
157 efx_drv_limits_t limits;
159 uint32_t evq_allocated;
160 uint32_t rxq_allocated;
161 uint32_t txq_allocated;
163 memset(&limits, 0, sizeof(limits));
165 /* Request at least one Rx and Tx queue */
166 limits.edl_min_rxq_count = 1;
167 limits.edl_min_txq_count = 1;
168 /* Management event queue plus event queue for each Tx and Rx queue */
169 limits.edl_min_evq_count =
170 1 + limits.edl_min_rxq_count + limits.edl_min_txq_count;
172 /* Divide by number of functions to guarantee that all functions
173 * will get promised resources
175 /* FIXME Divide by number of functions (not 2) below */
176 limits.edl_max_evq_count = encp->enc_evq_limit / 2;
177 SFC_ASSERT(limits.edl_max_evq_count >= limits.edl_min_rxq_count);
179 /* Split equally between receive and transmit */
180 limits.edl_max_rxq_count =
181 MIN(encp->enc_rxq_limit, (limits.edl_max_evq_count - 1) / 2);
182 SFC_ASSERT(limits.edl_max_rxq_count >= limits.edl_min_rxq_count);
184 limits.edl_max_txq_count =
185 MIN(encp->enc_txq_limit,
186 limits.edl_max_evq_count - 1 - limits.edl_max_rxq_count);
189 limits.edl_max_txq_count =
190 MIN(limits.edl_max_txq_count,
191 encp->enc_fw_assisted_tso_v2_n_contexts /
192 encp->enc_hw_pf_count);
194 SFC_ASSERT(limits.edl_max_txq_count >= limits.edl_min_rxq_count);
196 /* Configure the minimum required resources needed for the
197 * driver to operate, and the maximum desired resources that the
198 * driver is capable of using.
200 efx_nic_set_drv_limits(sa->nic, &limits);
202 sfc_log_init(sa, "init nic");
203 rc = efx_nic_init(sa->nic);
207 /* Find resource dimensions assigned by firmware to this function */
208 rc = efx_nic_get_vi_pool(sa->nic, &evq_allocated, &rxq_allocated,
211 goto fail_get_vi_pool;
213 /* It still may allocate more than maximum, ensure limit */
214 evq_allocated = MIN(evq_allocated, limits.edl_max_evq_count);
215 rxq_allocated = MIN(rxq_allocated, limits.edl_max_rxq_count);
216 txq_allocated = MIN(txq_allocated, limits.edl_max_txq_count);
218 /* Subtract management EVQ not used for traffic */
219 SFC_ASSERT(evq_allocated > 0);
222 /* Right now we use separate EVQ for Rx and Tx */
223 sa->rxq_max = MIN(rxq_allocated, evq_allocated / 2);
224 sa->txq_max = MIN(txq_allocated, evq_allocated - sa->rxq_max);
226 /* Keep NIC initialized */
231 efx_nic_fini(sa->nic);
236 sfc_set_drv_limits(struct sfc_adapter *sa)
238 const struct rte_eth_dev_data *data = sa->eth_dev->data;
239 efx_drv_limits_t lim;
241 memset(&lim, 0, sizeof(lim));
243 /* Limits are strict since take into account initial estimation */
244 lim.edl_min_evq_count = lim.edl_max_evq_count =
245 1 + data->nb_rx_queues + data->nb_tx_queues;
246 lim.edl_min_rxq_count = lim.edl_max_rxq_count = data->nb_rx_queues;
247 lim.edl_min_txq_count = lim.edl_max_txq_count = data->nb_tx_queues;
249 return efx_nic_set_drv_limits(sa->nic, &lim);
253 sfc_try_start(struct sfc_adapter *sa)
255 const efx_nic_cfg_t *encp;
258 sfc_log_init(sa, "entry");
260 SFC_ASSERT(sfc_adapter_is_locked(sa));
261 SFC_ASSERT(sa->state == SFC_ADAPTER_STARTING);
263 sfc_log_init(sa, "set resource limits");
264 rc = sfc_set_drv_limits(sa);
266 goto fail_set_drv_limits;
268 sfc_log_init(sa, "init nic");
269 rc = efx_nic_init(sa->nic);
273 encp = efx_nic_cfg_get(sa->nic);
274 if (encp->enc_tunnel_encapsulations_supported != 0) {
275 sfc_log_init(sa, "apply tunnel config");
276 rc = efx_tunnel_reconfigure(sa->nic);
278 goto fail_tunnel_reconfigure;
281 rc = sfc_intr_start(sa);
283 goto fail_intr_start;
285 rc = sfc_ev_start(sa);
289 rc = sfc_port_start(sa);
291 goto fail_port_start;
293 rc = sfc_rx_start(sa);
297 rc = sfc_tx_start(sa);
301 rc = sfc_flow_start(sa);
303 goto fail_flows_insert;
305 sfc_log_init(sa, "done");
324 fail_tunnel_reconfigure:
325 efx_nic_fini(sa->nic);
329 sfc_log_init(sa, "failed %d", rc);
334 sfc_start(struct sfc_adapter *sa)
336 unsigned int start_tries = 3;
339 sfc_log_init(sa, "entry");
341 SFC_ASSERT(sfc_adapter_is_locked(sa));
344 case SFC_ADAPTER_CONFIGURED:
346 case SFC_ADAPTER_STARTED:
347 sfc_info(sa, "already started");
354 sa->state = SFC_ADAPTER_STARTING;
357 rc = sfc_try_start(sa);
358 } while ((--start_tries > 0) &&
359 (rc == EIO || rc == EAGAIN || rc == ENOENT || rc == EINVAL));
364 sa->state = SFC_ADAPTER_STARTED;
365 sfc_log_init(sa, "done");
369 sa->state = SFC_ADAPTER_CONFIGURED;
371 sfc_log_init(sa, "failed %d", rc);
376 sfc_stop(struct sfc_adapter *sa)
378 sfc_log_init(sa, "entry");
380 SFC_ASSERT(sfc_adapter_is_locked(sa));
383 case SFC_ADAPTER_STARTED:
385 case SFC_ADAPTER_CONFIGURED:
386 sfc_info(sa, "already stopped");
389 sfc_err(sa, "stop in unexpected state %u", sa->state);
394 sa->state = SFC_ADAPTER_STOPPING;
402 efx_nic_fini(sa->nic);
404 sa->state = SFC_ADAPTER_CONFIGURED;
405 sfc_log_init(sa, "done");
409 sfc_restart(struct sfc_adapter *sa)
413 SFC_ASSERT(sfc_adapter_is_locked(sa));
415 if (sa->state != SFC_ADAPTER_STARTED)
422 sfc_err(sa, "restart failed");
428 sfc_restart_if_required(void *arg)
430 struct sfc_adapter *sa = arg;
432 /* If restart is scheduled, clear the flag and do it */
433 if (rte_atomic32_cmpset((volatile uint32_t *)&sa->restart_required,
435 sfc_adapter_lock(sa);
436 if (sa->state == SFC_ADAPTER_STARTED)
437 (void)sfc_restart(sa);
438 sfc_adapter_unlock(sa);
443 sfc_schedule_restart(struct sfc_adapter *sa)
447 /* Schedule restart alarm if it is not scheduled yet */
448 if (!rte_atomic32_test_and_set(&sa->restart_required))
451 rc = rte_eal_alarm_set(1, sfc_restart_if_required, sa);
453 sfc_warn(sa, "alarms are not supported, restart is pending");
455 sfc_err(sa, "cannot arm restart alarm (rc=%d)", rc);
457 sfc_info(sa, "restart scheduled");
461 sfc_configure(struct sfc_adapter *sa)
465 sfc_log_init(sa, "entry");
467 SFC_ASSERT(sfc_adapter_is_locked(sa));
469 SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED ||
470 sa->state == SFC_ADAPTER_CONFIGURED);
471 sa->state = SFC_ADAPTER_CONFIGURING;
473 rc = sfc_check_conf(sa);
475 goto fail_check_conf;
477 rc = sfc_intr_configure(sa);
479 goto fail_intr_configure;
481 rc = sfc_port_configure(sa);
483 goto fail_port_configure;
485 rc = sfc_rx_configure(sa);
487 goto fail_rx_configure;
489 rc = sfc_tx_configure(sa);
491 goto fail_tx_configure;
493 sa->state = SFC_ADAPTER_CONFIGURED;
494 sfc_log_init(sa, "done");
508 sa->state = SFC_ADAPTER_INITIALIZED;
509 sfc_log_init(sa, "failed %d", rc);
514 sfc_close(struct sfc_adapter *sa)
516 sfc_log_init(sa, "entry");
518 SFC_ASSERT(sfc_adapter_is_locked(sa));
520 SFC_ASSERT(sa->state == SFC_ADAPTER_CONFIGURED);
521 sa->state = SFC_ADAPTER_CLOSING;
528 sa->state = SFC_ADAPTER_INITIALIZED;
529 sfc_log_init(sa, "done");
533 sfc_mem_bar_init(struct sfc_adapter *sa, unsigned int membar)
535 struct rte_eth_dev *eth_dev = sa->eth_dev;
536 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
537 efsys_bar_t *ebp = &sa->mem_bar;
538 struct rte_mem_resource *res = &pci_dev->mem_resource[membar];
540 SFC_BAR_LOCK_INIT(ebp, eth_dev->data->name);
541 ebp->esb_rid = membar;
542 ebp->esb_dev = pci_dev;
543 ebp->esb_base = res->addr;
548 sfc_mem_bar_fini(struct sfc_adapter *sa)
550 efsys_bar_t *ebp = &sa->mem_bar;
552 SFC_BAR_LOCK_DESTROY(ebp);
553 memset(ebp, 0, sizeof(*ebp));
556 #if EFSYS_OPT_RX_SCALE
558 * A fixed RSS key which has a property of being symmetric
559 * (symmetrical flows are distributed to the same CPU)
560 * and also known to give a uniform distribution
561 * (a good distribution of traffic between different CPUs)
563 static const uint8_t default_rss_key[EFX_RSS_KEY_SIZE] = {
564 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
565 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
566 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
567 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
568 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
572 #if EFSYS_OPT_RX_SCALE
574 sfc_set_rss_defaults(struct sfc_adapter *sa)
578 rc = efx_intr_init(sa->nic, sa->intr.type, NULL);
582 rc = efx_ev_init(sa->nic);
586 rc = efx_rx_init(sa->nic);
590 rc = efx_rx_scale_default_support_get(sa->nic, &sa->rss_support);
592 goto fail_scale_support_get;
594 rc = efx_rx_hash_default_support_get(sa->nic, &sa->hash_support);
596 goto fail_hash_support_get;
598 efx_rx_fini(sa->nic);
599 efx_ev_fini(sa->nic);
600 efx_intr_fini(sa->nic);
602 sa->rss_hash_types = sfc_rte_to_efx_hash_type(SFC_RSS_OFFLOADS);
604 rte_memcpy(sa->rss_key, default_rss_key, sizeof(sa->rss_key));
608 fail_hash_support_get:
609 fail_scale_support_get:
611 efx_ev_fini(sa->nic);
614 efx_intr_fini(sa->nic);
621 sfc_set_rss_defaults(__rte_unused struct sfc_adapter *sa)
628 sfc_attach(struct sfc_adapter *sa)
630 const efx_nic_cfg_t *encp;
631 efx_nic_t *enp = sa->nic;
634 sfc_log_init(sa, "entry");
636 SFC_ASSERT(sfc_adapter_is_locked(sa));
638 efx_mcdi_new_epoch(enp);
640 sfc_log_init(sa, "reset nic");
641 rc = efx_nic_reset(enp);
646 * Probed NIC is sufficient for tunnel init.
647 * Initialize tunnel support to be able to use libefx
648 * efx_tunnel_config_udp_{add,remove}() in any state and
649 * efx_tunnel_reconfigure() on start up.
651 rc = efx_tunnel_init(enp);
653 goto fail_tunnel_init;
655 encp = efx_nic_cfg_get(sa->nic);
657 if (sa->dp_tx->features & SFC_DP_TX_FEAT_TSO) {
658 sa->tso = encp->enc_fw_assisted_tso_v2_enabled;
661 "TSO support isn't available on this adapter");
664 sfc_log_init(sa, "estimate resource limits");
665 rc = sfc_estimate_resource_limits(sa);
667 goto fail_estimate_rsrc_limits;
669 sa->txq_max_entries = encp->enc_txq_max_ndescs;
670 SFC_ASSERT(rte_is_power_of_2(sa->txq_max_entries));
672 rc = sfc_intr_attach(sa);
674 goto fail_intr_attach;
676 rc = sfc_ev_attach(sa);
680 rc = sfc_port_attach(sa);
682 goto fail_port_attach;
684 rc = sfc_set_rss_defaults(sa);
686 goto fail_set_rss_defaults;
688 rc = sfc_filter_attach(sa);
690 goto fail_filter_attach;
692 sfc_log_init(sa, "fini nic");
697 sa->state = SFC_ADAPTER_INITIALIZED;
699 sfc_log_init(sa, "done");
703 fail_set_rss_defaults:
713 efx_nic_fini(sa->nic);
715 fail_estimate_rsrc_limits:
717 efx_tunnel_fini(sa->nic);
721 sfc_log_init(sa, "failed %d", rc);
726 sfc_detach(struct sfc_adapter *sa)
728 sfc_log_init(sa, "entry");
730 SFC_ASSERT(sfc_adapter_is_locked(sa));
734 sfc_filter_detach(sa);
738 efx_tunnel_fini(sa->nic);
740 sa->state = SFC_ADAPTER_UNINITIALIZED;
744 sfc_probe(struct sfc_adapter *sa)
746 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(sa->eth_dev);
751 sfc_log_init(sa, "entry");
753 SFC_ASSERT(sfc_adapter_is_locked(sa));
755 sa->socket_id = rte_socket_id();
756 rte_atomic32_init(&sa->restart_required);
758 sfc_log_init(sa, "get family");
759 rc = efx_family(pci_dev->id.vendor_id, pci_dev->id.device_id,
760 &sa->family, &membar);
763 sfc_log_init(sa, "family is %u, membar is %u", sa->family, membar);
765 sfc_log_init(sa, "init mem bar");
766 rc = sfc_mem_bar_init(sa, membar);
768 goto fail_mem_bar_init;
770 sfc_log_init(sa, "create nic");
771 rte_spinlock_init(&sa->nic_lock);
772 rc = efx_nic_create(sa->family, (efsys_identifier_t *)sa,
773 &sa->mem_bar, &sa->nic_lock, &enp);
775 goto fail_nic_create;
778 rc = sfc_mcdi_init(sa);
782 sfc_log_init(sa, "probe nic");
783 rc = efx_nic_probe(enp);
787 sfc_log_init(sa, "done");
794 sfc_log_init(sa, "destroy nic");
796 efx_nic_destroy(enp);
799 sfc_mem_bar_fini(sa);
803 sfc_log_init(sa, "failed %d", rc);
808 sfc_unprobe(struct sfc_adapter *sa)
810 efx_nic_t *enp = sa->nic;
812 sfc_log_init(sa, "entry");
814 SFC_ASSERT(sfc_adapter_is_locked(sa));
816 sfc_log_init(sa, "unprobe nic");
817 efx_nic_unprobe(enp);
822 * Make sure there is no pending alarm to restart since we are
823 * going to free device private which is passed as the callback
824 * opaque data. A new alarm cannot be scheduled since MCDI is
827 rte_eal_alarm_cancel(sfc_restart_if_required, sa);
829 sfc_log_init(sa, "destroy nic");
831 efx_nic_destroy(enp);
833 sfc_mem_bar_fini(sa);
836 sa->state = SFC_ADAPTER_UNINITIALIZED;
840 sfc_register_logtype(struct sfc_adapter *sa, const char *lt_prefix_str,
843 size_t lt_prefix_str_size = strlen(lt_prefix_str);
844 size_t lt_str_size_max;
848 if (SIZE_MAX - PCI_PRI_STR_SIZE - 1 > lt_prefix_str_size) {
849 ++lt_prefix_str_size; /* Reserve space for prefix separator */
850 lt_str_size_max = lt_prefix_str_size + PCI_PRI_STR_SIZE + 1;
852 return RTE_LOGTYPE_PMD;
855 lt_str = rte_zmalloc("logtype_str", lt_str_size_max, 0);
857 return RTE_LOGTYPE_PMD;
859 strncpy(lt_str, lt_prefix_str, lt_prefix_str_size);
860 lt_str[lt_prefix_str_size - 1] = '.';
861 rte_pci_device_name(&sa->pci_addr, lt_str + lt_prefix_str_size,
862 lt_str_size_max - lt_prefix_str_size);
863 lt_str[lt_str_size_max - 1] = '\0';
865 ret = rte_log_register_type_and_pick_level(lt_str, ll_default);
868 return (ret < 0) ? RTE_LOGTYPE_PMD : ret;