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 sfc_configure(struct sfc_adapter *sa)
134 sfc_log_init(sa, "entry");
136 SFC_ASSERT(sfc_adapter_is_locked(sa));
138 SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED);
139 sa->state = SFC_ADAPTER_CONFIGURING;
141 rc = sfc_check_conf(sa);
143 goto fail_check_conf;
145 sa->state = SFC_ADAPTER_CONFIGURED;
146 sfc_log_init(sa, "done");
150 sa->state = SFC_ADAPTER_INITIALIZED;
151 sfc_log_init(sa, "failed %d", rc);
156 sfc_close(struct sfc_adapter *sa)
158 sfc_log_init(sa, "entry");
160 SFC_ASSERT(sfc_adapter_is_locked(sa));
162 SFC_ASSERT(sa->state == SFC_ADAPTER_CONFIGURED);
163 sa->state = SFC_ADAPTER_CLOSING;
165 sa->state = SFC_ADAPTER_INITIALIZED;
166 sfc_log_init(sa, "done");
170 sfc_mem_bar_init(struct sfc_adapter *sa)
172 struct rte_eth_dev *eth_dev = sa->eth_dev;
173 struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(eth_dev);
174 efsys_bar_t *ebp = &sa->mem_bar;
176 struct rte_mem_resource *res;
178 for (i = 0; i < RTE_DIM(pci_dev->mem_resource); i++) {
179 res = &pci_dev->mem_resource[i];
180 if ((res->len != 0) && (res->phys_addr != 0)) {
181 /* Found first memory BAR */
182 SFC_BAR_LOCK_INIT(ebp, eth_dev->data->name);
184 ebp->esb_dev = pci_dev;
185 ebp->esb_base = res->addr;
194 sfc_mem_bar_fini(struct sfc_adapter *sa)
196 efsys_bar_t *ebp = &sa->mem_bar;
198 SFC_BAR_LOCK_DESTROY(ebp);
199 memset(ebp, 0, sizeof(*ebp));
203 sfc_attach(struct sfc_adapter *sa)
205 struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(sa->eth_dev);
209 sfc_log_init(sa, "entry");
211 SFC_ASSERT(sfc_adapter_is_locked(sa));
213 sa->socket_id = rte_socket_id();
215 sfc_log_init(sa, "init mem bar");
216 rc = sfc_mem_bar_init(sa);
218 goto fail_mem_bar_init;
220 sfc_log_init(sa, "get family");
221 rc = efx_family(pci_dev->id.vendor_id, pci_dev->id.device_id,
225 sfc_log_init(sa, "family is %u", sa->family);
227 sfc_log_init(sa, "create nic");
228 rte_spinlock_init(&sa->nic_lock);
229 rc = efx_nic_create(sa->family, (efsys_identifier_t *)sa,
230 &sa->mem_bar, &sa->nic_lock, &enp);
232 goto fail_nic_create;
235 rc = sfc_mcdi_init(sa);
239 sfc_log_init(sa, "probe nic");
240 rc = efx_nic_probe(enp);
244 efx_mcdi_new_epoch(enp);
246 sfc_log_init(sa, "reset nic");
247 rc = efx_nic_reset(enp);
251 /* Initialize NIC to double-check hardware */
252 sfc_log_init(sa, "init nic");
253 rc = efx_nic_init(enp);
257 sfc_log_init(sa, "fini nic");
263 sa->state = SFC_ADAPTER_INITIALIZED;
265 sfc_log_init(sa, "done");
270 sfc_log_init(sa, "unprobe nic");
271 efx_nic_unprobe(enp);
277 sfc_log_init(sa, "destroy nic");
279 efx_nic_destroy(enp);
283 sfc_mem_bar_fini(sa);
286 sfc_log_init(sa, "failed %d", rc);
291 sfc_detach(struct sfc_adapter *sa)
293 efx_nic_t *enp = sa->nic;
295 sfc_log_init(sa, "entry");
297 SFC_ASSERT(sfc_adapter_is_locked(sa));
299 sfc_log_init(sa, "unprobe nic");
300 efx_nic_unprobe(enp);
304 sfc_log_init(sa, "destroy nic");
306 efx_nic_destroy(enp);
308 sfc_mem_bar_fini(sa);
310 sa->state = SFC_ADAPTER_UNINITIALIZED;