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>
44 sfc_dma_alloc(const struct sfc_adapter *sa, const char *name, uint16_t id,
45 size_t len, int socket_id, efsys_mem_t *esmp)
47 const struct rte_memzone *mz;
49 sfc_log_init(sa, "name=%s id=%u len=%lu socket_id=%d",
50 name, id, len, socket_id);
52 mz = rte_eth_dma_zone_reserve(sa->eth_dev, name, id, len,
53 sysconf(_SC_PAGESIZE), socket_id);
55 sfc_err(sa, "cannot reserve DMA zone for %s:%u %#x@%d: %s",
56 name, (unsigned int)id, (unsigned int)len, socket_id,
57 rte_strerror(rte_errno));
61 esmp->esm_addr = rte_mem_phy2mch(mz->memseg_id, mz->phys_addr);
62 if (esmp->esm_addr == RTE_BAD_PHYS_ADDR) {
63 (void)rte_memzone_free(mz);
68 esmp->esm_base = mz->addr;
74 sfc_dma_free(const struct sfc_adapter *sa, efsys_mem_t *esmp)
78 sfc_log_init(sa, "name=%s", esmp->esm_mz->name);
80 rc = rte_memzone_free(esmp->esm_mz);
82 sfc_err(sa, "rte_memzone_free(() failed: %d", rc);
84 memset(esmp, 0, sizeof(*esmp));
88 * Check requested device level configuration.
89 * Receive and transmit configuration is checked in corresponding
93 sfc_check_conf(struct sfc_adapter *sa)
95 const struct rte_eth_conf *conf = &sa->eth_dev->data->dev_conf;
98 if (conf->link_speeds != ETH_LINK_SPEED_AUTONEG) {
99 sfc_err(sa, "Manual link speed/duplex choice not supported");
103 if (conf->lpbk_mode != 0) {
104 sfc_err(sa, "Loopback not supported");
108 if (conf->dcb_capability_en != 0) {
109 sfc_err(sa, "Priority-based flow control not supported");
113 if (conf->fdir_conf.mode != RTE_FDIR_MODE_NONE) {
114 sfc_err(sa, "Flow Director not supported");
118 if (conf->intr_conf.lsc != 0) {
119 sfc_err(sa, "Link status change interrupt not supported");
123 if (conf->intr_conf.rxq != 0) {
124 sfc_err(sa, "Receive queue interrupt not supported");
132 * Find out maximum number of receive and transmit queues which could be
135 * NIC is kept initialized on success to allow other modules acquire
136 * defaults and capabilities.
139 sfc_estimate_resource_limits(struct sfc_adapter *sa)
141 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
142 efx_drv_limits_t limits;
144 uint32_t evq_allocated;
145 uint32_t rxq_allocated;
146 uint32_t txq_allocated;
148 memset(&limits, 0, sizeof(limits));
150 /* Request at least one Rx and Tx queue */
151 limits.edl_min_rxq_count = 1;
152 limits.edl_min_txq_count = 1;
153 /* Management event queue plus event queue for each Tx and Rx queue */
154 limits.edl_min_evq_count =
155 1 + limits.edl_min_rxq_count + limits.edl_min_txq_count;
157 /* Divide by number of functions to guarantee that all functions
158 * will get promised resources
160 /* FIXME Divide by number of functions (not 2) below */
161 limits.edl_max_evq_count = encp->enc_evq_limit / 2;
162 SFC_ASSERT(limits.edl_max_evq_count >= limits.edl_min_rxq_count);
164 /* Split equally between receive and transmit */
165 limits.edl_max_rxq_count =
166 MIN(encp->enc_rxq_limit, (limits.edl_max_evq_count - 1) / 2);
167 SFC_ASSERT(limits.edl_max_rxq_count >= limits.edl_min_rxq_count);
169 limits.edl_max_txq_count =
170 MIN(encp->enc_txq_limit,
171 limits.edl_max_evq_count - 1 - limits.edl_max_rxq_count);
172 SFC_ASSERT(limits.edl_max_txq_count >= limits.edl_min_rxq_count);
174 /* Configure the minimum required resources needed for the
175 * driver to operate, and the maximum desired resources that the
176 * driver is capable of using.
178 efx_nic_set_drv_limits(sa->nic, &limits);
180 sfc_log_init(sa, "init nic");
181 rc = efx_nic_init(sa->nic);
185 /* Find resource dimensions assigned by firmware to this function */
186 rc = efx_nic_get_vi_pool(sa->nic, &evq_allocated, &rxq_allocated,
189 goto fail_get_vi_pool;
191 /* It still may allocate more than maximum, ensure limit */
192 evq_allocated = MIN(evq_allocated, limits.edl_max_evq_count);
193 rxq_allocated = MIN(rxq_allocated, limits.edl_max_rxq_count);
194 txq_allocated = MIN(txq_allocated, limits.edl_max_txq_count);
196 /* Subtract management EVQ not used for traffic */
197 SFC_ASSERT(evq_allocated > 0);
200 /* Right now we use separate EVQ for Rx and Tx */
201 sa->rxq_max = MIN(rxq_allocated, evq_allocated / 2);
202 sa->txq_max = MIN(txq_allocated, evq_allocated - sa->rxq_max);
204 /* Keep NIC initialized */
209 efx_nic_fini(sa->nic);
214 sfc_set_drv_limits(struct sfc_adapter *sa)
216 const struct rte_eth_dev_data *data = sa->eth_dev->data;
217 efx_drv_limits_t lim;
219 memset(&lim, 0, sizeof(lim));
221 /* Limits are strict since take into account initial estimation */
222 lim.edl_min_evq_count = lim.edl_max_evq_count =
223 1 + data->nb_rx_queues + data->nb_tx_queues;
224 lim.edl_min_rxq_count = lim.edl_max_rxq_count = data->nb_rx_queues;
225 lim.edl_min_txq_count = lim.edl_max_txq_count = data->nb_tx_queues;
227 return efx_nic_set_drv_limits(sa->nic, &lim);
231 sfc_start(struct sfc_adapter *sa)
235 sfc_log_init(sa, "entry");
237 SFC_ASSERT(sfc_adapter_is_locked(sa));
240 case SFC_ADAPTER_CONFIGURED:
242 case SFC_ADAPTER_STARTED:
243 sfc_info(sa, "already started");
250 sa->state = SFC_ADAPTER_STARTING;
252 sfc_log_init(sa, "set resource limits");
253 rc = sfc_set_drv_limits(sa);
255 goto fail_set_drv_limits;
257 sfc_log_init(sa, "init nic");
258 rc = efx_nic_init(sa->nic);
262 rc = sfc_intr_start(sa);
264 goto fail_intr_start;
266 rc = sfc_ev_start(sa);
270 rc = sfc_port_start(sa);
272 goto fail_port_start;
274 rc = sfc_rx_start(sa);
278 sa->state = SFC_ADAPTER_STARTED;
279 sfc_log_init(sa, "done");
292 efx_nic_fini(sa->nic);
296 sa->state = SFC_ADAPTER_CONFIGURED;
298 sfc_log_init(sa, "failed %d", rc);
303 sfc_stop(struct sfc_adapter *sa)
305 sfc_log_init(sa, "entry");
307 SFC_ASSERT(sfc_adapter_is_locked(sa));
310 case SFC_ADAPTER_STARTED:
312 case SFC_ADAPTER_CONFIGURED:
313 sfc_info(sa, "already stopped");
316 sfc_err(sa, "stop in unexpected state %u", sa->state);
321 sa->state = SFC_ADAPTER_STOPPING;
327 efx_nic_fini(sa->nic);
329 sa->state = SFC_ADAPTER_CONFIGURED;
330 sfc_log_init(sa, "done");
334 sfc_configure(struct sfc_adapter *sa)
338 sfc_log_init(sa, "entry");
340 SFC_ASSERT(sfc_adapter_is_locked(sa));
342 SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED);
343 sa->state = SFC_ADAPTER_CONFIGURING;
345 rc = sfc_check_conf(sa);
347 goto fail_check_conf;
349 rc = sfc_intr_init(sa);
353 rc = sfc_ev_init(sa);
357 rc = sfc_port_init(sa);
361 rc = sfc_rx_init(sa);
365 sa->state = SFC_ADAPTER_CONFIGURED;
366 sfc_log_init(sa, "done");
380 sa->state = SFC_ADAPTER_INITIALIZED;
381 sfc_log_init(sa, "failed %d", rc);
386 sfc_close(struct sfc_adapter *sa)
388 sfc_log_init(sa, "entry");
390 SFC_ASSERT(sfc_adapter_is_locked(sa));
392 SFC_ASSERT(sa->state == SFC_ADAPTER_CONFIGURED);
393 sa->state = SFC_ADAPTER_CLOSING;
400 sa->state = SFC_ADAPTER_INITIALIZED;
401 sfc_log_init(sa, "done");
405 sfc_mem_bar_init(struct sfc_adapter *sa)
407 struct rte_eth_dev *eth_dev = sa->eth_dev;
408 struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(eth_dev);
409 efsys_bar_t *ebp = &sa->mem_bar;
411 struct rte_mem_resource *res;
413 for (i = 0; i < RTE_DIM(pci_dev->mem_resource); i++) {
414 res = &pci_dev->mem_resource[i];
415 if ((res->len != 0) && (res->phys_addr != 0)) {
416 /* Found first memory BAR */
417 SFC_BAR_LOCK_INIT(ebp, eth_dev->data->name);
419 ebp->esb_dev = pci_dev;
420 ebp->esb_base = res->addr;
429 sfc_mem_bar_fini(struct sfc_adapter *sa)
431 efsys_bar_t *ebp = &sa->mem_bar;
433 SFC_BAR_LOCK_DESTROY(ebp);
434 memset(ebp, 0, sizeof(*ebp));
438 sfc_attach(struct sfc_adapter *sa)
440 struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(sa->eth_dev);
444 sfc_log_init(sa, "entry");
446 SFC_ASSERT(sfc_adapter_is_locked(sa));
448 sa->socket_id = rte_socket_id();
450 sfc_log_init(sa, "init mem bar");
451 rc = sfc_mem_bar_init(sa);
453 goto fail_mem_bar_init;
455 sfc_log_init(sa, "get family");
456 rc = efx_family(pci_dev->id.vendor_id, pci_dev->id.device_id,
460 sfc_log_init(sa, "family is %u", sa->family);
462 sfc_log_init(sa, "create nic");
463 rte_spinlock_init(&sa->nic_lock);
464 rc = efx_nic_create(sa->family, (efsys_identifier_t *)sa,
465 &sa->mem_bar, &sa->nic_lock, &enp);
467 goto fail_nic_create;
470 rc = sfc_mcdi_init(sa);
474 sfc_log_init(sa, "probe nic");
475 rc = efx_nic_probe(enp);
479 efx_mcdi_new_epoch(enp);
481 sfc_log_init(sa, "reset nic");
482 rc = efx_nic_reset(enp);
486 sfc_log_init(sa, "estimate resource limits");
487 rc = sfc_estimate_resource_limits(sa);
489 goto fail_estimate_rsrc_limits;
491 rc = sfc_intr_attach(sa);
493 goto fail_intr_attach;
495 sfc_log_init(sa, "fini nic");
498 sa->state = SFC_ADAPTER_INITIALIZED;
500 sfc_log_init(sa, "done");
504 fail_estimate_rsrc_limits:
506 sfc_log_init(sa, "unprobe nic");
507 efx_nic_unprobe(enp);
513 sfc_log_init(sa, "destroy nic");
515 efx_nic_destroy(enp);
519 sfc_mem_bar_fini(sa);
522 sfc_log_init(sa, "failed %d", rc);
527 sfc_detach(struct sfc_adapter *sa)
529 efx_nic_t *enp = sa->nic;
531 sfc_log_init(sa, "entry");
533 SFC_ASSERT(sfc_adapter_is_locked(sa));
537 sfc_log_init(sa, "unprobe nic");
538 efx_nic_unprobe(enp);
542 sfc_log_init(sa, "destroy nic");
544 efx_nic_destroy(enp);
546 sfc_mem_bar_fini(sa);
548 sa->state = SFC_ADAPTER_UNINITIALIZED;