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 rc = sfc_tx_start(sa);
283 sa->state = SFC_ADAPTER_STARTED;
284 sfc_log_init(sa, "done");
300 efx_nic_fini(sa->nic);
304 sa->state = SFC_ADAPTER_CONFIGURED;
306 sfc_log_init(sa, "failed %d", rc);
311 sfc_stop(struct sfc_adapter *sa)
313 sfc_log_init(sa, "entry");
315 SFC_ASSERT(sfc_adapter_is_locked(sa));
318 case SFC_ADAPTER_STARTED:
320 case SFC_ADAPTER_CONFIGURED:
321 sfc_info(sa, "already stopped");
324 sfc_err(sa, "stop in unexpected state %u", sa->state);
329 sa->state = SFC_ADAPTER_STOPPING;
336 efx_nic_fini(sa->nic);
338 sa->state = SFC_ADAPTER_CONFIGURED;
339 sfc_log_init(sa, "done");
343 sfc_configure(struct sfc_adapter *sa)
347 sfc_log_init(sa, "entry");
349 SFC_ASSERT(sfc_adapter_is_locked(sa));
351 SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED);
352 sa->state = SFC_ADAPTER_CONFIGURING;
354 rc = sfc_check_conf(sa);
356 goto fail_check_conf;
358 rc = sfc_intr_init(sa);
362 rc = sfc_ev_init(sa);
366 rc = sfc_port_init(sa);
370 rc = sfc_rx_init(sa);
374 rc = sfc_tx_init(sa);
378 sa->state = SFC_ADAPTER_CONFIGURED;
379 sfc_log_init(sa, "done");
396 sa->state = SFC_ADAPTER_INITIALIZED;
397 sfc_log_init(sa, "failed %d", rc);
402 sfc_close(struct sfc_adapter *sa)
404 sfc_log_init(sa, "entry");
406 SFC_ASSERT(sfc_adapter_is_locked(sa));
408 SFC_ASSERT(sa->state == SFC_ADAPTER_CONFIGURED);
409 sa->state = SFC_ADAPTER_CLOSING;
417 sa->state = SFC_ADAPTER_INITIALIZED;
418 sfc_log_init(sa, "done");
422 sfc_mem_bar_init(struct sfc_adapter *sa)
424 struct rte_eth_dev *eth_dev = sa->eth_dev;
425 struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(eth_dev);
426 efsys_bar_t *ebp = &sa->mem_bar;
428 struct rte_mem_resource *res;
430 for (i = 0; i < RTE_DIM(pci_dev->mem_resource); i++) {
431 res = &pci_dev->mem_resource[i];
432 if ((res->len != 0) && (res->phys_addr != 0)) {
433 /* Found first memory BAR */
434 SFC_BAR_LOCK_INIT(ebp, eth_dev->data->name);
436 ebp->esb_dev = pci_dev;
437 ebp->esb_base = res->addr;
446 sfc_mem_bar_fini(struct sfc_adapter *sa)
448 efsys_bar_t *ebp = &sa->mem_bar;
450 SFC_BAR_LOCK_DESTROY(ebp);
451 memset(ebp, 0, sizeof(*ebp));
455 sfc_attach(struct sfc_adapter *sa)
457 struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(sa->eth_dev);
458 const efx_nic_cfg_t *encp;
462 sfc_log_init(sa, "entry");
464 SFC_ASSERT(sfc_adapter_is_locked(sa));
466 sa->socket_id = rte_socket_id();
468 sfc_log_init(sa, "init mem bar");
469 rc = sfc_mem_bar_init(sa);
471 goto fail_mem_bar_init;
473 sfc_log_init(sa, "get family");
474 rc = efx_family(pci_dev->id.vendor_id, pci_dev->id.device_id,
478 sfc_log_init(sa, "family is %u", sa->family);
480 sfc_log_init(sa, "create nic");
481 rte_spinlock_init(&sa->nic_lock);
482 rc = efx_nic_create(sa->family, (efsys_identifier_t *)sa,
483 &sa->mem_bar, &sa->nic_lock, &enp);
485 goto fail_nic_create;
488 rc = sfc_mcdi_init(sa);
492 sfc_log_init(sa, "probe nic");
493 rc = efx_nic_probe(enp);
497 efx_mcdi_new_epoch(enp);
499 sfc_log_init(sa, "reset nic");
500 rc = efx_nic_reset(enp);
504 sfc_log_init(sa, "estimate resource limits");
505 rc = sfc_estimate_resource_limits(sa);
507 goto fail_estimate_rsrc_limits;
509 encp = efx_nic_cfg_get(sa->nic);
510 sa->txq_max_entries = encp->enc_txq_max_ndescs;
511 SFC_ASSERT(rte_is_power_of_2(sa->txq_max_entries));
513 rc = sfc_intr_attach(sa);
515 goto fail_intr_attach;
517 sfc_log_init(sa, "fini nic");
520 sa->state = SFC_ADAPTER_INITIALIZED;
522 sfc_log_init(sa, "done");
526 fail_estimate_rsrc_limits:
528 sfc_log_init(sa, "unprobe nic");
529 efx_nic_unprobe(enp);
535 sfc_log_init(sa, "destroy nic");
537 efx_nic_destroy(enp);
541 sfc_mem_bar_fini(sa);
544 sfc_log_init(sa, "failed %d", rc);
549 sfc_detach(struct sfc_adapter *sa)
551 efx_nic_t *enp = sa->nic;
553 sfc_log_init(sa, "entry");
555 SFC_ASSERT(sfc_adapter_is_locked(sa));
559 sfc_log_init(sa, "unprobe nic");
560 efx_nic_unprobe(enp);
564 sfc_log_init(sa, "destroy nic");
566 efx_nic_destroy(enp);
568 sfc_mem_bar_fini(sa);
570 sa->state = SFC_ADAPTER_UNINITIALIZED;