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)
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;
539 struct rte_mem_resource *res;
541 for (i = 0; i < RTE_DIM(pci_dev->mem_resource); i++) {
542 res = &pci_dev->mem_resource[i];
543 if ((res->len != 0) && (res->phys_addr != 0)) {
544 /* Found first memory BAR */
545 SFC_BAR_LOCK_INIT(ebp, eth_dev->data->name);
547 ebp->esb_dev = pci_dev;
548 ebp->esb_base = res->addr;
557 sfc_mem_bar_fini(struct sfc_adapter *sa)
559 efsys_bar_t *ebp = &sa->mem_bar;
561 SFC_BAR_LOCK_DESTROY(ebp);
562 memset(ebp, 0, sizeof(*ebp));
565 #if EFSYS_OPT_RX_SCALE
567 * A fixed RSS key which has a property of being symmetric
568 * (symmetrical flows are distributed to the same CPU)
569 * and also known to give a uniform distribution
570 * (a good distribution of traffic between different CPUs)
572 static const uint8_t default_rss_key[EFX_RSS_KEY_SIZE] = {
573 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
574 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
575 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
576 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
577 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
581 #if EFSYS_OPT_RX_SCALE
583 sfc_set_rss_defaults(struct sfc_adapter *sa)
587 rc = efx_intr_init(sa->nic, sa->intr.type, NULL);
591 rc = efx_ev_init(sa->nic);
595 rc = efx_rx_init(sa->nic);
599 rc = efx_rx_scale_default_support_get(sa->nic, &sa->rss_support);
601 goto fail_scale_support_get;
603 rc = efx_rx_hash_default_support_get(sa->nic, &sa->hash_support);
605 goto fail_hash_support_get;
607 efx_rx_fini(sa->nic);
608 efx_ev_fini(sa->nic);
609 efx_intr_fini(sa->nic);
611 sa->rss_hash_types = sfc_rte_to_efx_hash_type(SFC_RSS_OFFLOADS);
613 rte_memcpy(sa->rss_key, default_rss_key, sizeof(sa->rss_key));
617 fail_hash_support_get:
618 fail_scale_support_get:
620 efx_ev_fini(sa->nic);
623 efx_intr_fini(sa->nic);
630 sfc_set_rss_defaults(__rte_unused struct sfc_adapter *sa)
637 sfc_attach(struct sfc_adapter *sa)
639 const efx_nic_cfg_t *encp;
640 efx_nic_t *enp = sa->nic;
643 sfc_log_init(sa, "entry");
645 SFC_ASSERT(sfc_adapter_is_locked(sa));
647 efx_mcdi_new_epoch(enp);
649 sfc_log_init(sa, "reset nic");
650 rc = efx_nic_reset(enp);
655 * Probed NIC is sufficient for tunnel init.
656 * Initialize tunnel support to be able to use libefx
657 * efx_tunnel_config_udp_{add,remove}() in any state and
658 * efx_tunnel_reconfigure() on start up.
660 rc = efx_tunnel_init(enp);
662 goto fail_tunnel_init;
664 encp = efx_nic_cfg_get(sa->nic);
666 if (sa->dp_tx->features & SFC_DP_TX_FEAT_TSO) {
667 sa->tso = encp->enc_fw_assisted_tso_v2_enabled;
670 "TSO support isn't available on this adapter");
673 sfc_log_init(sa, "estimate resource limits");
674 rc = sfc_estimate_resource_limits(sa);
676 goto fail_estimate_rsrc_limits;
678 sa->txq_max_entries = encp->enc_txq_max_ndescs;
679 SFC_ASSERT(rte_is_power_of_2(sa->txq_max_entries));
681 rc = sfc_intr_attach(sa);
683 goto fail_intr_attach;
685 rc = sfc_ev_attach(sa);
689 rc = sfc_port_attach(sa);
691 goto fail_port_attach;
693 rc = sfc_set_rss_defaults(sa);
695 goto fail_set_rss_defaults;
697 rc = sfc_filter_attach(sa);
699 goto fail_filter_attach;
701 sfc_log_init(sa, "fini nic");
706 sa->state = SFC_ADAPTER_INITIALIZED;
708 sfc_log_init(sa, "done");
712 fail_set_rss_defaults:
722 efx_nic_fini(sa->nic);
724 fail_estimate_rsrc_limits:
726 efx_tunnel_fini(sa->nic);
730 sfc_log_init(sa, "failed %d", rc);
735 sfc_detach(struct sfc_adapter *sa)
737 sfc_log_init(sa, "entry");
739 SFC_ASSERT(sfc_adapter_is_locked(sa));
743 sfc_filter_detach(sa);
747 efx_tunnel_fini(sa->nic);
749 sa->state = SFC_ADAPTER_UNINITIALIZED;
753 sfc_probe(struct sfc_adapter *sa)
755 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(sa->eth_dev);
759 sfc_log_init(sa, "entry");
761 SFC_ASSERT(sfc_adapter_is_locked(sa));
763 sa->socket_id = rte_socket_id();
764 rte_atomic32_init(&sa->restart_required);
766 sfc_log_init(sa, "init mem bar");
767 rc = sfc_mem_bar_init(sa);
769 goto fail_mem_bar_init;
771 sfc_log_init(sa, "get family");
772 rc = efx_family(pci_dev->id.vendor_id, pci_dev->id.device_id,
776 sfc_log_init(sa, "family is %u", sa->family);
778 sfc_log_init(sa, "create nic");
779 rte_spinlock_init(&sa->nic_lock);
780 rc = efx_nic_create(sa->family, (efsys_identifier_t *)sa,
781 &sa->mem_bar, &sa->nic_lock, &enp);
783 goto fail_nic_create;
786 rc = sfc_mcdi_init(sa);
790 sfc_log_init(sa, "probe nic");
791 rc = efx_nic_probe(enp);
795 sfc_log_init(sa, "done");
802 sfc_log_init(sa, "destroy nic");
804 efx_nic_destroy(enp);
808 sfc_mem_bar_fini(sa);
811 sfc_log_init(sa, "failed %d", rc);
816 sfc_unprobe(struct sfc_adapter *sa)
818 efx_nic_t *enp = sa->nic;
820 sfc_log_init(sa, "entry");
822 SFC_ASSERT(sfc_adapter_is_locked(sa));
824 sfc_log_init(sa, "unprobe nic");
825 efx_nic_unprobe(enp);
830 * Make sure there is no pending alarm to restart since we are
831 * going to free device private which is passed as the callback
832 * opaque data. A new alarm cannot be scheduled since MCDI is
835 rte_eal_alarm_cancel(sfc_restart_if_required, sa);
837 sfc_log_init(sa, "destroy nic");
839 efx_nic_destroy(enp);
841 sfc_mem_bar_fini(sa);
844 sa->state = SFC_ADAPTER_UNINITIALIZED;