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>
42 sfc_dma_alloc(const struct sfc_adapter *sa, const char *name, uint16_t id,
43 size_t len, int socket_id, efsys_mem_t *esmp)
45 const struct rte_memzone *mz;
47 sfc_log_init(sa, "name=%s id=%u len=%lu socket_id=%d",
48 name, id, len, socket_id);
50 mz = rte_eth_dma_zone_reserve(sa->eth_dev, name, id, len,
51 sysconf(_SC_PAGESIZE), socket_id);
53 sfc_err(sa, "cannot reserve DMA zone for %s:%u %#x@%d: %s",
54 name, (unsigned int)id, (unsigned int)len, socket_id,
55 rte_strerror(rte_errno));
59 esmp->esm_addr = rte_mem_phy2mch(mz->memseg_id, mz->phys_addr);
60 if (esmp->esm_addr == RTE_BAD_PHYS_ADDR) {
61 (void)rte_memzone_free(mz);
66 esmp->esm_base = mz->addr;
72 sfc_dma_free(const struct sfc_adapter *sa, efsys_mem_t *esmp)
76 sfc_log_init(sa, "name=%s", esmp->esm_mz->name);
78 rc = rte_memzone_free(esmp->esm_mz);
80 sfc_err(sa, "rte_memzone_free(() failed: %d", rc);
82 memset(esmp, 0, sizeof(*esmp));
86 * Check requested device level configuration.
87 * Receive and transmit configuration is checked in corresponding
91 sfc_check_conf(struct sfc_adapter *sa)
93 const struct rte_eth_conf *conf = &sa->eth_dev->data->dev_conf;
96 if (conf->link_speeds != ETH_LINK_SPEED_AUTONEG) {
97 sfc_err(sa, "Manual link speed/duplex choice not supported");
101 if (conf->lpbk_mode != 0) {
102 sfc_err(sa, "Loopback not supported");
106 if (conf->dcb_capability_en != 0) {
107 sfc_err(sa, "Priority-based flow control not supported");
111 if (conf->fdir_conf.mode != RTE_FDIR_MODE_NONE) {
112 sfc_err(sa, "Flow Director not supported");
116 if (conf->intr_conf.lsc != 0) {
117 sfc_err(sa, "Link status change interrupt not supported");
121 if (conf->intr_conf.rxq != 0) {
122 sfc_err(sa, "Receive queue interrupt not supported");
130 * Find out maximum number of receive and transmit queues which could be
133 * NIC is kept initialized on success to allow other modules acquire
134 * defaults and capabilities.
137 sfc_estimate_resource_limits(struct sfc_adapter *sa)
139 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
140 efx_drv_limits_t limits;
142 uint32_t evq_allocated;
143 uint32_t rxq_allocated;
144 uint32_t txq_allocated;
146 memset(&limits, 0, sizeof(limits));
148 /* Request at least one Rx and Tx queue */
149 limits.edl_min_rxq_count = 1;
150 limits.edl_min_txq_count = 1;
151 /* Management event queue plus event queue for each Tx and Rx queue */
152 limits.edl_min_evq_count =
153 1 + limits.edl_min_rxq_count + limits.edl_min_txq_count;
155 /* Divide by number of functions to guarantee that all functions
156 * will get promised resources
158 /* FIXME Divide by number of functions (not 2) below */
159 limits.edl_max_evq_count = encp->enc_evq_limit / 2;
160 SFC_ASSERT(limits.edl_max_evq_count >= limits.edl_min_rxq_count);
162 /* Split equally between receive and transmit */
163 limits.edl_max_rxq_count =
164 MIN(encp->enc_rxq_limit, (limits.edl_max_evq_count - 1) / 2);
165 SFC_ASSERT(limits.edl_max_rxq_count >= limits.edl_min_rxq_count);
167 limits.edl_max_txq_count =
168 MIN(encp->enc_txq_limit,
169 limits.edl_max_evq_count - 1 - limits.edl_max_rxq_count);
170 SFC_ASSERT(limits.edl_max_txq_count >= limits.edl_min_rxq_count);
172 /* Configure the minimum required resources needed for the
173 * driver to operate, and the maximum desired resources that the
174 * driver is capable of using.
176 efx_nic_set_drv_limits(sa->nic, &limits);
178 sfc_log_init(sa, "init nic");
179 rc = efx_nic_init(sa->nic);
183 /* Find resource dimensions assigned by firmware to this function */
184 rc = efx_nic_get_vi_pool(sa->nic, &evq_allocated, &rxq_allocated,
187 goto fail_get_vi_pool;
189 /* It still may allocate more than maximum, ensure limit */
190 evq_allocated = MIN(evq_allocated, limits.edl_max_evq_count);
191 rxq_allocated = MIN(rxq_allocated, limits.edl_max_rxq_count);
192 txq_allocated = MIN(txq_allocated, limits.edl_max_txq_count);
194 /* Subtract management EVQ not used for traffic */
195 SFC_ASSERT(evq_allocated > 0);
198 /* Right now we use separate EVQ for Rx and Tx */
199 sa->rxq_max = MIN(rxq_allocated, evq_allocated / 2);
200 sa->txq_max = MIN(txq_allocated, evq_allocated - sa->rxq_max);
202 /* Keep NIC initialized */
207 efx_nic_fini(sa->nic);
212 sfc_set_drv_limits(struct sfc_adapter *sa)
214 const struct rte_eth_dev_data *data = sa->eth_dev->data;
215 efx_drv_limits_t lim;
217 memset(&lim, 0, sizeof(lim));
219 /* Limits are strict since take into account initial estimation */
220 lim.edl_min_evq_count = lim.edl_max_evq_count =
221 1 + data->nb_rx_queues + data->nb_tx_queues;
222 lim.edl_min_rxq_count = lim.edl_max_rxq_count = data->nb_rx_queues;
223 lim.edl_min_txq_count = lim.edl_max_txq_count = data->nb_tx_queues;
225 return efx_nic_set_drv_limits(sa->nic, &lim);
229 sfc_start(struct sfc_adapter *sa)
233 sfc_log_init(sa, "entry");
235 SFC_ASSERT(sfc_adapter_is_locked(sa));
238 case SFC_ADAPTER_CONFIGURED:
240 case SFC_ADAPTER_STARTED:
241 sfc_info(sa, "already started");
248 sa->state = SFC_ADAPTER_STARTING;
250 sfc_log_init(sa, "set resource limits");
251 rc = sfc_set_drv_limits(sa);
253 goto fail_set_drv_limits;
255 sfc_log_init(sa, "init nic");
256 rc = efx_nic_init(sa->nic);
260 rc = sfc_intr_start(sa);
262 goto fail_intr_start;
264 sa->state = SFC_ADAPTER_STARTED;
265 sfc_log_init(sa, "done");
269 efx_nic_fini(sa->nic);
273 sa->state = SFC_ADAPTER_CONFIGURED;
275 sfc_log_init(sa, "failed %d", rc);
280 sfc_stop(struct sfc_adapter *sa)
282 sfc_log_init(sa, "entry");
284 SFC_ASSERT(sfc_adapter_is_locked(sa));
287 case SFC_ADAPTER_STARTED:
289 case SFC_ADAPTER_CONFIGURED:
290 sfc_info(sa, "already stopped");
293 sfc_err(sa, "stop in unexpected state %u", sa->state);
298 sa->state = SFC_ADAPTER_STOPPING;
301 efx_nic_fini(sa->nic);
303 sa->state = SFC_ADAPTER_CONFIGURED;
304 sfc_log_init(sa, "done");
308 sfc_configure(struct sfc_adapter *sa)
312 sfc_log_init(sa, "entry");
314 SFC_ASSERT(sfc_adapter_is_locked(sa));
316 SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED);
317 sa->state = SFC_ADAPTER_CONFIGURING;
319 rc = sfc_check_conf(sa);
321 goto fail_check_conf;
323 rc = sfc_intr_init(sa);
327 sa->state = SFC_ADAPTER_CONFIGURED;
328 sfc_log_init(sa, "done");
333 sa->state = SFC_ADAPTER_INITIALIZED;
334 sfc_log_init(sa, "failed %d", rc);
339 sfc_close(struct sfc_adapter *sa)
341 sfc_log_init(sa, "entry");
343 SFC_ASSERT(sfc_adapter_is_locked(sa));
345 SFC_ASSERT(sa->state == SFC_ADAPTER_CONFIGURED);
346 sa->state = SFC_ADAPTER_CLOSING;
350 sa->state = SFC_ADAPTER_INITIALIZED;
351 sfc_log_init(sa, "done");
355 sfc_mem_bar_init(struct sfc_adapter *sa)
357 struct rte_eth_dev *eth_dev = sa->eth_dev;
358 struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(eth_dev);
359 efsys_bar_t *ebp = &sa->mem_bar;
361 struct rte_mem_resource *res;
363 for (i = 0; i < RTE_DIM(pci_dev->mem_resource); i++) {
364 res = &pci_dev->mem_resource[i];
365 if ((res->len != 0) && (res->phys_addr != 0)) {
366 /* Found first memory BAR */
367 SFC_BAR_LOCK_INIT(ebp, eth_dev->data->name);
369 ebp->esb_dev = pci_dev;
370 ebp->esb_base = res->addr;
379 sfc_mem_bar_fini(struct sfc_adapter *sa)
381 efsys_bar_t *ebp = &sa->mem_bar;
383 SFC_BAR_LOCK_DESTROY(ebp);
384 memset(ebp, 0, sizeof(*ebp));
388 sfc_attach(struct sfc_adapter *sa)
390 struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(sa->eth_dev);
394 sfc_log_init(sa, "entry");
396 SFC_ASSERT(sfc_adapter_is_locked(sa));
398 sa->socket_id = rte_socket_id();
400 sfc_log_init(sa, "init mem bar");
401 rc = sfc_mem_bar_init(sa);
403 goto fail_mem_bar_init;
405 sfc_log_init(sa, "get family");
406 rc = efx_family(pci_dev->id.vendor_id, pci_dev->id.device_id,
410 sfc_log_init(sa, "family is %u", sa->family);
412 sfc_log_init(sa, "create nic");
413 rte_spinlock_init(&sa->nic_lock);
414 rc = efx_nic_create(sa->family, (efsys_identifier_t *)sa,
415 &sa->mem_bar, &sa->nic_lock, &enp);
417 goto fail_nic_create;
420 rc = sfc_mcdi_init(sa);
424 sfc_log_init(sa, "probe nic");
425 rc = efx_nic_probe(enp);
429 efx_mcdi_new_epoch(enp);
431 sfc_log_init(sa, "reset nic");
432 rc = efx_nic_reset(enp);
436 sfc_log_init(sa, "estimate resource limits");
437 rc = sfc_estimate_resource_limits(sa);
439 goto fail_estimate_rsrc_limits;
441 rc = sfc_intr_attach(sa);
443 goto fail_intr_attach;
445 sfc_log_init(sa, "fini nic");
448 sa->state = SFC_ADAPTER_INITIALIZED;
450 sfc_log_init(sa, "done");
454 fail_estimate_rsrc_limits:
456 sfc_log_init(sa, "unprobe nic");
457 efx_nic_unprobe(enp);
463 sfc_log_init(sa, "destroy nic");
465 efx_nic_destroy(enp);
469 sfc_mem_bar_fini(sa);
472 sfc_log_init(sa, "failed %d", rc);
477 sfc_detach(struct sfc_adapter *sa)
479 efx_nic_t *enp = sa->nic;
481 sfc_log_init(sa, "entry");
483 SFC_ASSERT(sfc_adapter_is_locked(sa));
487 sfc_log_init(sa, "unprobe nic");
488 efx_nic_unprobe(enp);
492 sfc_log_init(sa, "destroy nic");
494 efx_nic_destroy(enp);
496 sfc_mem_bar_fini(sa);
498 sa->state = SFC_ADAPTER_UNINITIALIZED;