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 sfc_configure(struct sfc_adapter *sa)
88 sfc_log_init(sa, "entry");
90 SFC_ASSERT(sfc_adapter_is_locked(sa));
92 SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED);
93 sa->state = SFC_ADAPTER_CONFIGURING;
95 sa->state = SFC_ADAPTER_CONFIGURED;
96 sfc_log_init(sa, "done");
101 sfc_close(struct sfc_adapter *sa)
103 sfc_log_init(sa, "entry");
105 SFC_ASSERT(sfc_adapter_is_locked(sa));
107 SFC_ASSERT(sa->state == SFC_ADAPTER_CONFIGURED);
108 sa->state = SFC_ADAPTER_CLOSING;
110 sa->state = SFC_ADAPTER_INITIALIZED;
111 sfc_log_init(sa, "done");
115 sfc_mem_bar_init(struct sfc_adapter *sa)
117 struct rte_eth_dev *eth_dev = sa->eth_dev;
118 struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(eth_dev);
119 efsys_bar_t *ebp = &sa->mem_bar;
121 struct rte_mem_resource *res;
123 for (i = 0; i < RTE_DIM(pci_dev->mem_resource); i++) {
124 res = &pci_dev->mem_resource[i];
125 if ((res->len != 0) && (res->phys_addr != 0)) {
126 /* Found first memory BAR */
127 SFC_BAR_LOCK_INIT(ebp, eth_dev->data->name);
129 ebp->esb_dev = pci_dev;
130 ebp->esb_base = res->addr;
139 sfc_mem_bar_fini(struct sfc_adapter *sa)
141 efsys_bar_t *ebp = &sa->mem_bar;
143 SFC_BAR_LOCK_DESTROY(ebp);
144 memset(ebp, 0, sizeof(*ebp));
148 sfc_attach(struct sfc_adapter *sa)
150 struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(sa->eth_dev);
154 sfc_log_init(sa, "entry");
156 SFC_ASSERT(sfc_adapter_is_locked(sa));
158 sa->socket_id = rte_socket_id();
160 sfc_log_init(sa, "init mem bar");
161 rc = sfc_mem_bar_init(sa);
163 goto fail_mem_bar_init;
165 sfc_log_init(sa, "get family");
166 rc = efx_family(pci_dev->id.vendor_id, pci_dev->id.device_id,
170 sfc_log_init(sa, "family is %u", sa->family);
172 sfc_log_init(sa, "create nic");
173 rte_spinlock_init(&sa->nic_lock);
174 rc = efx_nic_create(sa->family, (efsys_identifier_t *)sa,
175 &sa->mem_bar, &sa->nic_lock, &enp);
177 goto fail_nic_create;
180 rc = sfc_mcdi_init(sa);
184 sfc_log_init(sa, "probe nic");
185 rc = efx_nic_probe(enp);
189 efx_mcdi_new_epoch(enp);
191 sfc_log_init(sa, "reset nic");
192 rc = efx_nic_reset(enp);
196 /* Initialize NIC to double-check hardware */
197 sfc_log_init(sa, "init nic");
198 rc = efx_nic_init(enp);
202 sfc_log_init(sa, "fini nic");
208 sa->state = SFC_ADAPTER_INITIALIZED;
210 sfc_log_init(sa, "done");
215 sfc_log_init(sa, "unprobe nic");
216 efx_nic_unprobe(enp);
222 sfc_log_init(sa, "destroy nic");
224 efx_nic_destroy(enp);
228 sfc_mem_bar_fini(sa);
231 sfc_log_init(sa, "failed %d", rc);
236 sfc_detach(struct sfc_adapter *sa)
238 efx_nic_t *enp = sa->nic;
240 sfc_log_init(sa, "entry");
242 SFC_ASSERT(sfc_adapter_is_locked(sa));
244 sfc_log_init(sa, "unprobe nic");
245 efx_nic_unprobe(enp);
249 sfc_log_init(sa, "destroy nic");
251 efx_nic_destroy(enp);
253 sfc_mem_bar_fini(sa);
255 sa->state = SFC_ADAPTER_UNINITIALIZED;