2 * Copyright (c) 2016 Solarflare Communications Inc.
5 * This software was jointly developed between OKTET Labs (under contract
6 * for Solarflare) and 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.
33 #include <rte_errno.h>
45 sfc_dma_alloc(const struct sfc_adapter *sa, const char *name, uint16_t id,
46 size_t len, int socket_id, efsys_mem_t *esmp)
48 const struct rte_memzone *mz;
50 sfc_log_init(sa, "name=%s id=%u len=%lu socket_id=%d",
51 name, id, len, socket_id);
53 mz = rte_eth_dma_zone_reserve(sa->eth_dev, name, id, len,
54 sysconf(_SC_PAGESIZE), socket_id);
56 sfc_err(sa, "cannot reserve DMA zone for %s:%u %#x@%d: %s",
57 name, (unsigned int)id, (unsigned int)len, socket_id,
58 rte_strerror(rte_errno));
62 esmp->esm_addr = rte_mem_phy2mch(mz->memseg_id, mz->phys_addr);
63 if (esmp->esm_addr == RTE_BAD_PHYS_ADDR) {
64 (void)rte_memzone_free(mz);
69 esmp->esm_base = mz->addr;
75 sfc_dma_free(const struct sfc_adapter *sa, efsys_mem_t *esmp)
79 sfc_log_init(sa, "name=%s", esmp->esm_mz->name);
81 rc = rte_memzone_free(esmp->esm_mz);
83 sfc_err(sa, "rte_memzone_free(() failed: %d", rc);
85 memset(esmp, 0, sizeof(*esmp));
89 * Check requested device level configuration.
90 * Receive and transmit configuration is checked in corresponding
94 sfc_check_conf(struct sfc_adapter *sa)
96 const struct rte_eth_conf *conf = &sa->eth_dev->data->dev_conf;
99 if (conf->link_speeds != ETH_LINK_SPEED_AUTONEG) {
100 sfc_err(sa, "Manual link speed/duplex choice not supported");
104 if (conf->lpbk_mode != 0) {
105 sfc_err(sa, "Loopback not supported");
109 if (conf->dcb_capability_en != 0) {
110 sfc_err(sa, "Priority-based flow control not supported");
114 if (conf->fdir_conf.mode != RTE_FDIR_MODE_NONE) {
115 sfc_err(sa, "Flow Director not supported");
119 if (conf->intr_conf.lsc != 0) {
120 sfc_err(sa, "Link status change interrupt not supported");
124 if (conf->intr_conf.rxq != 0) {
125 sfc_err(sa, "Receive queue interrupt not supported");
133 * Find out maximum number of receive and transmit queues which could be
136 * NIC is kept initialized on success to allow other modules acquire
137 * defaults and capabilities.
140 sfc_estimate_resource_limits(struct sfc_adapter *sa)
142 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
143 efx_drv_limits_t limits;
145 uint32_t evq_allocated;
146 uint32_t rxq_allocated;
147 uint32_t txq_allocated;
149 memset(&limits, 0, sizeof(limits));
151 /* Request at least one Rx and Tx queue */
152 limits.edl_min_rxq_count = 1;
153 limits.edl_min_txq_count = 1;
154 /* Management event queue plus event queue for each Tx and Rx queue */
155 limits.edl_min_evq_count =
156 1 + limits.edl_min_rxq_count + limits.edl_min_txq_count;
158 /* Divide by number of functions to guarantee that all functions
159 * will get promised resources
161 /* FIXME Divide by number of functions (not 2) below */
162 limits.edl_max_evq_count = encp->enc_evq_limit / 2;
163 SFC_ASSERT(limits.edl_max_evq_count >= limits.edl_min_rxq_count);
165 /* Split equally between receive and transmit */
166 limits.edl_max_rxq_count =
167 MIN(encp->enc_rxq_limit, (limits.edl_max_evq_count - 1) / 2);
168 SFC_ASSERT(limits.edl_max_rxq_count >= limits.edl_min_rxq_count);
170 limits.edl_max_txq_count =
171 MIN(encp->enc_txq_limit,
172 limits.edl_max_evq_count - 1 - limits.edl_max_rxq_count);
173 SFC_ASSERT(limits.edl_max_txq_count >= limits.edl_min_rxq_count);
175 /* Configure the minimum required resources needed for the
176 * driver to operate, and the maximum desired resources that the
177 * driver is capable of using.
179 efx_nic_set_drv_limits(sa->nic, &limits);
181 sfc_log_init(sa, "init nic");
182 rc = efx_nic_init(sa->nic);
186 /* Find resource dimensions assigned by firmware to this function */
187 rc = efx_nic_get_vi_pool(sa->nic, &evq_allocated, &rxq_allocated,
190 goto fail_get_vi_pool;
192 /* It still may allocate more than maximum, ensure limit */
193 evq_allocated = MIN(evq_allocated, limits.edl_max_evq_count);
194 rxq_allocated = MIN(rxq_allocated, limits.edl_max_rxq_count);
195 txq_allocated = MIN(txq_allocated, limits.edl_max_txq_count);
197 /* Subtract management EVQ not used for traffic */
198 SFC_ASSERT(evq_allocated > 0);
201 /* Right now we use separate EVQ for Rx and Tx */
202 sa->rxq_max = MIN(rxq_allocated, evq_allocated / 2);
203 sa->txq_max = MIN(txq_allocated, evq_allocated - sa->rxq_max);
205 /* Keep NIC initialized */
210 efx_nic_fini(sa->nic);
215 sfc_set_drv_limits(struct sfc_adapter *sa)
217 const struct rte_eth_dev_data *data = sa->eth_dev->data;
218 efx_drv_limits_t lim;
220 memset(&lim, 0, sizeof(lim));
222 /* Limits are strict since take into account initial estimation */
223 lim.edl_min_evq_count = lim.edl_max_evq_count =
224 1 + data->nb_rx_queues + data->nb_tx_queues;
225 lim.edl_min_rxq_count = lim.edl_max_rxq_count = data->nb_rx_queues;
226 lim.edl_min_txq_count = lim.edl_max_txq_count = data->nb_tx_queues;
228 return efx_nic_set_drv_limits(sa->nic, &lim);
232 sfc_start(struct sfc_adapter *sa)
236 sfc_log_init(sa, "entry");
238 SFC_ASSERT(sfc_adapter_is_locked(sa));
241 case SFC_ADAPTER_CONFIGURED:
243 case SFC_ADAPTER_STARTED:
244 sfc_info(sa, "already started");
251 sa->state = SFC_ADAPTER_STARTING;
253 sfc_log_init(sa, "set resource limits");
254 rc = sfc_set_drv_limits(sa);
256 goto fail_set_drv_limits;
258 sfc_log_init(sa, "init nic");
259 rc = efx_nic_init(sa->nic);
263 rc = sfc_intr_start(sa);
265 goto fail_intr_start;
267 rc = sfc_ev_start(sa);
271 rc = sfc_port_start(sa);
273 goto fail_port_start;
275 rc = sfc_rx_start(sa);
279 sa->state = SFC_ADAPTER_STARTED;
280 sfc_log_init(sa, "done");
293 efx_nic_fini(sa->nic);
297 sa->state = SFC_ADAPTER_CONFIGURED;
299 sfc_log_init(sa, "failed %d", rc);
304 sfc_stop(struct sfc_adapter *sa)
306 sfc_log_init(sa, "entry");
308 SFC_ASSERT(sfc_adapter_is_locked(sa));
311 case SFC_ADAPTER_STARTED:
313 case SFC_ADAPTER_CONFIGURED:
314 sfc_info(sa, "already stopped");
317 sfc_err(sa, "stop in unexpected state %u", sa->state);
322 sa->state = SFC_ADAPTER_STOPPING;
328 efx_nic_fini(sa->nic);
330 sa->state = SFC_ADAPTER_CONFIGURED;
331 sfc_log_init(sa, "done");
335 sfc_configure(struct sfc_adapter *sa)
339 sfc_log_init(sa, "entry");
341 SFC_ASSERT(sfc_adapter_is_locked(sa));
343 SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED);
344 sa->state = SFC_ADAPTER_CONFIGURING;
346 rc = sfc_check_conf(sa);
348 goto fail_check_conf;
350 rc = sfc_intr_init(sa);
354 rc = sfc_ev_init(sa);
358 rc = sfc_port_init(sa);
362 rc = sfc_rx_init(sa);
366 rc = sfc_tx_init(sa);
370 sa->state = SFC_ADAPTER_CONFIGURED;
371 sfc_log_init(sa, "done");
388 sa->state = SFC_ADAPTER_INITIALIZED;
389 sfc_log_init(sa, "failed %d", rc);
394 sfc_close(struct sfc_adapter *sa)
396 sfc_log_init(sa, "entry");
398 SFC_ASSERT(sfc_adapter_is_locked(sa));
400 SFC_ASSERT(sa->state == SFC_ADAPTER_CONFIGURED);
401 sa->state = SFC_ADAPTER_CLOSING;
409 sa->state = SFC_ADAPTER_INITIALIZED;
410 sfc_log_init(sa, "done");
414 sfc_mem_bar_init(struct sfc_adapter *sa)
416 struct rte_eth_dev *eth_dev = sa->eth_dev;
417 struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(eth_dev);
418 efsys_bar_t *ebp = &sa->mem_bar;
420 struct rte_mem_resource *res;
422 for (i = 0; i < RTE_DIM(pci_dev->mem_resource); i++) {
423 res = &pci_dev->mem_resource[i];
424 if ((res->len != 0) && (res->phys_addr != 0)) {
425 /* Found first memory BAR */
426 SFC_BAR_LOCK_INIT(ebp, eth_dev->data->name);
428 ebp->esb_dev = pci_dev;
429 ebp->esb_base = res->addr;
438 sfc_mem_bar_fini(struct sfc_adapter *sa)
440 efsys_bar_t *ebp = &sa->mem_bar;
442 SFC_BAR_LOCK_DESTROY(ebp);
443 memset(ebp, 0, sizeof(*ebp));
447 sfc_attach(struct sfc_adapter *sa)
449 struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(sa->eth_dev);
450 const efx_nic_cfg_t *encp;
454 sfc_log_init(sa, "entry");
456 SFC_ASSERT(sfc_adapter_is_locked(sa));
458 sa->socket_id = rte_socket_id();
460 sfc_log_init(sa, "init mem bar");
461 rc = sfc_mem_bar_init(sa);
463 goto fail_mem_bar_init;
465 sfc_log_init(sa, "get family");
466 rc = efx_family(pci_dev->id.vendor_id, pci_dev->id.device_id,
470 sfc_log_init(sa, "family is %u", sa->family);
472 sfc_log_init(sa, "create nic");
473 rte_spinlock_init(&sa->nic_lock);
474 rc = efx_nic_create(sa->family, (efsys_identifier_t *)sa,
475 &sa->mem_bar, &sa->nic_lock, &enp);
477 goto fail_nic_create;
480 rc = sfc_mcdi_init(sa);
484 sfc_log_init(sa, "probe nic");
485 rc = efx_nic_probe(enp);
489 efx_mcdi_new_epoch(enp);
491 sfc_log_init(sa, "reset nic");
492 rc = efx_nic_reset(enp);
496 sfc_log_init(sa, "estimate resource limits");
497 rc = sfc_estimate_resource_limits(sa);
499 goto fail_estimate_rsrc_limits;
501 encp = efx_nic_cfg_get(sa->nic);
502 sa->txq_max_entries = encp->enc_txq_max_ndescs;
503 SFC_ASSERT(rte_is_power_of_2(sa->txq_max_entries));
505 rc = sfc_intr_attach(sa);
507 goto fail_intr_attach;
509 sfc_log_init(sa, "fini nic");
512 sa->state = SFC_ADAPTER_INITIALIZED;
514 sfc_log_init(sa, "done");
518 fail_estimate_rsrc_limits:
520 sfc_log_init(sa, "unprobe nic");
521 efx_nic_unprobe(enp);
527 sfc_log_init(sa, "destroy nic");
529 efx_nic_destroy(enp);
533 sfc_mem_bar_fini(sa);
536 sfc_log_init(sa, "failed %d", rc);
541 sfc_detach(struct sfc_adapter *sa)
543 efx_nic_t *enp = sa->nic;
545 sfc_log_init(sa, "entry");
547 SFC_ASSERT(sfc_adapter_is_locked(sa));
551 sfc_log_init(sa, "unprobe nic");
552 efx_nic_unprobe(enp);
556 sfc_log_init(sa, "destroy nic");
558 efx_nic_destroy(enp);
560 sfc_mem_bar_fini(sa);
562 sa->state = SFC_ADAPTER_UNINITIALIZED;