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>
43 sfc_dma_alloc(const struct sfc_adapter *sa, const char *name, uint16_t id,
44 size_t len, int socket_id, efsys_mem_t *esmp)
46 const struct rte_memzone *mz;
48 sfc_log_init(sa, "name=%s id=%u len=%lu socket_id=%d",
49 name, id, len, socket_id);
51 mz = rte_eth_dma_zone_reserve(sa->eth_dev, name, id, len,
52 sysconf(_SC_PAGESIZE), socket_id);
54 sfc_err(sa, "cannot reserve DMA zone for %s:%u %#x@%d: %s",
55 name, (unsigned int)id, (unsigned int)len, socket_id,
56 rte_strerror(rte_errno));
60 esmp->esm_addr = rte_mem_phy2mch(mz->memseg_id, mz->phys_addr);
61 if (esmp->esm_addr == RTE_BAD_PHYS_ADDR) {
62 (void)rte_memzone_free(mz);
67 esmp->esm_base = mz->addr;
73 sfc_dma_free(const struct sfc_adapter *sa, efsys_mem_t *esmp)
77 sfc_log_init(sa, "name=%s", esmp->esm_mz->name);
79 rc = rte_memzone_free(esmp->esm_mz);
81 sfc_err(sa, "rte_memzone_free(() failed: %d", rc);
83 memset(esmp, 0, sizeof(*esmp));
87 * Check requested device level configuration.
88 * Receive and transmit configuration is checked in corresponding
92 sfc_check_conf(struct sfc_adapter *sa)
94 const struct rte_eth_conf *conf = &sa->eth_dev->data->dev_conf;
97 if (conf->link_speeds != ETH_LINK_SPEED_AUTONEG) {
98 sfc_err(sa, "Manual link speed/duplex choice not supported");
102 if (conf->lpbk_mode != 0) {
103 sfc_err(sa, "Loopback not supported");
107 if (conf->dcb_capability_en != 0) {
108 sfc_err(sa, "Priority-based flow control not supported");
112 if (conf->fdir_conf.mode != RTE_FDIR_MODE_NONE) {
113 sfc_err(sa, "Flow Director not supported");
117 if (conf->intr_conf.lsc != 0) {
118 sfc_err(sa, "Link status change interrupt not supported");
122 if (conf->intr_conf.rxq != 0) {
123 sfc_err(sa, "Receive queue interrupt not supported");
131 * Find out maximum number of receive and transmit queues which could be
134 * NIC is kept initialized on success to allow other modules acquire
135 * defaults and capabilities.
138 sfc_estimate_resource_limits(struct sfc_adapter *sa)
140 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
141 efx_drv_limits_t limits;
143 uint32_t evq_allocated;
144 uint32_t rxq_allocated;
145 uint32_t txq_allocated;
147 memset(&limits, 0, sizeof(limits));
149 /* Request at least one Rx and Tx queue */
150 limits.edl_min_rxq_count = 1;
151 limits.edl_min_txq_count = 1;
152 /* Management event queue plus event queue for each Tx and Rx queue */
153 limits.edl_min_evq_count =
154 1 + limits.edl_min_rxq_count + limits.edl_min_txq_count;
156 /* Divide by number of functions to guarantee that all functions
157 * will get promised resources
159 /* FIXME Divide by number of functions (not 2) below */
160 limits.edl_max_evq_count = encp->enc_evq_limit / 2;
161 SFC_ASSERT(limits.edl_max_evq_count >= limits.edl_min_rxq_count);
163 /* Split equally between receive and transmit */
164 limits.edl_max_rxq_count =
165 MIN(encp->enc_rxq_limit, (limits.edl_max_evq_count - 1) / 2);
166 SFC_ASSERT(limits.edl_max_rxq_count >= limits.edl_min_rxq_count);
168 limits.edl_max_txq_count =
169 MIN(encp->enc_txq_limit,
170 limits.edl_max_evq_count - 1 - limits.edl_max_rxq_count);
171 SFC_ASSERT(limits.edl_max_txq_count >= limits.edl_min_rxq_count);
173 /* Configure the minimum required resources needed for the
174 * driver to operate, and the maximum desired resources that the
175 * driver is capable of using.
177 efx_nic_set_drv_limits(sa->nic, &limits);
179 sfc_log_init(sa, "init nic");
180 rc = efx_nic_init(sa->nic);
184 /* Find resource dimensions assigned by firmware to this function */
185 rc = efx_nic_get_vi_pool(sa->nic, &evq_allocated, &rxq_allocated,
188 goto fail_get_vi_pool;
190 /* It still may allocate more than maximum, ensure limit */
191 evq_allocated = MIN(evq_allocated, limits.edl_max_evq_count);
192 rxq_allocated = MIN(rxq_allocated, limits.edl_max_rxq_count);
193 txq_allocated = MIN(txq_allocated, limits.edl_max_txq_count);
195 /* Subtract management EVQ not used for traffic */
196 SFC_ASSERT(evq_allocated > 0);
199 /* Right now we use separate EVQ for Rx and Tx */
200 sa->rxq_max = MIN(rxq_allocated, evq_allocated / 2);
201 sa->txq_max = MIN(txq_allocated, evq_allocated - sa->rxq_max);
203 /* Keep NIC initialized */
208 efx_nic_fini(sa->nic);
213 sfc_set_drv_limits(struct sfc_adapter *sa)
215 const struct rte_eth_dev_data *data = sa->eth_dev->data;
216 efx_drv_limits_t lim;
218 memset(&lim, 0, sizeof(lim));
220 /* Limits are strict since take into account initial estimation */
221 lim.edl_min_evq_count = lim.edl_max_evq_count =
222 1 + data->nb_rx_queues + data->nb_tx_queues;
223 lim.edl_min_rxq_count = lim.edl_max_rxq_count = data->nb_rx_queues;
224 lim.edl_min_txq_count = lim.edl_max_txq_count = data->nb_tx_queues;
226 return efx_nic_set_drv_limits(sa->nic, &lim);
230 sfc_start(struct sfc_adapter *sa)
234 sfc_log_init(sa, "entry");
236 SFC_ASSERT(sfc_adapter_is_locked(sa));
239 case SFC_ADAPTER_CONFIGURED:
241 case SFC_ADAPTER_STARTED:
242 sfc_info(sa, "already started");
249 sa->state = SFC_ADAPTER_STARTING;
251 sfc_log_init(sa, "set resource limits");
252 rc = sfc_set_drv_limits(sa);
254 goto fail_set_drv_limits;
256 sfc_log_init(sa, "init nic");
257 rc = efx_nic_init(sa->nic);
261 rc = sfc_intr_start(sa);
263 goto fail_intr_start;
265 rc = sfc_ev_start(sa);
269 rc = sfc_port_start(sa);
271 goto fail_port_start;
273 sa->state = SFC_ADAPTER_STARTED;
274 sfc_log_init(sa, "done");
284 efx_nic_fini(sa->nic);
288 sa->state = SFC_ADAPTER_CONFIGURED;
290 sfc_log_init(sa, "failed %d", rc);
295 sfc_stop(struct sfc_adapter *sa)
297 sfc_log_init(sa, "entry");
299 SFC_ASSERT(sfc_adapter_is_locked(sa));
302 case SFC_ADAPTER_STARTED:
304 case SFC_ADAPTER_CONFIGURED:
305 sfc_info(sa, "already stopped");
308 sfc_err(sa, "stop in unexpected state %u", sa->state);
313 sa->state = SFC_ADAPTER_STOPPING;
318 efx_nic_fini(sa->nic);
320 sa->state = SFC_ADAPTER_CONFIGURED;
321 sfc_log_init(sa, "done");
325 sfc_configure(struct sfc_adapter *sa)
329 sfc_log_init(sa, "entry");
331 SFC_ASSERT(sfc_adapter_is_locked(sa));
333 SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED);
334 sa->state = SFC_ADAPTER_CONFIGURING;
336 rc = sfc_check_conf(sa);
338 goto fail_check_conf;
340 rc = sfc_intr_init(sa);
344 rc = sfc_ev_init(sa);
348 rc = sfc_port_init(sa);
352 sa->state = SFC_ADAPTER_CONFIGURED;
353 sfc_log_init(sa, "done");
364 sa->state = SFC_ADAPTER_INITIALIZED;
365 sfc_log_init(sa, "failed %d", rc);
370 sfc_close(struct sfc_adapter *sa)
372 sfc_log_init(sa, "entry");
374 SFC_ASSERT(sfc_adapter_is_locked(sa));
376 SFC_ASSERT(sa->state == SFC_ADAPTER_CONFIGURED);
377 sa->state = SFC_ADAPTER_CLOSING;
383 sa->state = SFC_ADAPTER_INITIALIZED;
384 sfc_log_init(sa, "done");
388 sfc_mem_bar_init(struct sfc_adapter *sa)
390 struct rte_eth_dev *eth_dev = sa->eth_dev;
391 struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(eth_dev);
392 efsys_bar_t *ebp = &sa->mem_bar;
394 struct rte_mem_resource *res;
396 for (i = 0; i < RTE_DIM(pci_dev->mem_resource); i++) {
397 res = &pci_dev->mem_resource[i];
398 if ((res->len != 0) && (res->phys_addr != 0)) {
399 /* Found first memory BAR */
400 SFC_BAR_LOCK_INIT(ebp, eth_dev->data->name);
402 ebp->esb_dev = pci_dev;
403 ebp->esb_base = res->addr;
412 sfc_mem_bar_fini(struct sfc_adapter *sa)
414 efsys_bar_t *ebp = &sa->mem_bar;
416 SFC_BAR_LOCK_DESTROY(ebp);
417 memset(ebp, 0, sizeof(*ebp));
421 sfc_attach(struct sfc_adapter *sa)
423 struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(sa->eth_dev);
427 sfc_log_init(sa, "entry");
429 SFC_ASSERT(sfc_adapter_is_locked(sa));
431 sa->socket_id = rte_socket_id();
433 sfc_log_init(sa, "init mem bar");
434 rc = sfc_mem_bar_init(sa);
436 goto fail_mem_bar_init;
438 sfc_log_init(sa, "get family");
439 rc = efx_family(pci_dev->id.vendor_id, pci_dev->id.device_id,
443 sfc_log_init(sa, "family is %u", sa->family);
445 sfc_log_init(sa, "create nic");
446 rte_spinlock_init(&sa->nic_lock);
447 rc = efx_nic_create(sa->family, (efsys_identifier_t *)sa,
448 &sa->mem_bar, &sa->nic_lock, &enp);
450 goto fail_nic_create;
453 rc = sfc_mcdi_init(sa);
457 sfc_log_init(sa, "probe nic");
458 rc = efx_nic_probe(enp);
462 efx_mcdi_new_epoch(enp);
464 sfc_log_init(sa, "reset nic");
465 rc = efx_nic_reset(enp);
469 sfc_log_init(sa, "estimate resource limits");
470 rc = sfc_estimate_resource_limits(sa);
472 goto fail_estimate_rsrc_limits;
474 rc = sfc_intr_attach(sa);
476 goto fail_intr_attach;
478 sfc_log_init(sa, "fini nic");
481 sa->state = SFC_ADAPTER_INITIALIZED;
483 sfc_log_init(sa, "done");
487 fail_estimate_rsrc_limits:
489 sfc_log_init(sa, "unprobe nic");
490 efx_nic_unprobe(enp);
496 sfc_log_init(sa, "destroy nic");
498 efx_nic_destroy(enp);
502 sfc_mem_bar_fini(sa);
505 sfc_log_init(sa, "failed %d", rc);
510 sfc_detach(struct sfc_adapter *sa)
512 efx_nic_t *enp = sa->nic;
514 sfc_log_init(sa, "entry");
516 SFC_ASSERT(sfc_adapter_is_locked(sa));
520 sfc_log_init(sa, "unprobe nic");
521 efx_nic_unprobe(enp);
525 sfc_log_init(sa, "destroy nic");
527 efx_nic_destroy(enp);
529 sfc_mem_bar_fini(sa);
531 sa->state = SFC_ADAPTER_UNINITIALIZED;