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_mem_bar_init(struct sfc_adapter *sa)
88 struct rte_eth_dev *eth_dev = sa->eth_dev;
89 struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(eth_dev);
90 efsys_bar_t *ebp = &sa->mem_bar;
92 struct rte_mem_resource *res;
94 for (i = 0; i < RTE_DIM(pci_dev->mem_resource); i++) {
95 res = &pci_dev->mem_resource[i];
96 if ((res->len != 0) && (res->phys_addr != 0)) {
97 /* Found first memory BAR */
98 SFC_BAR_LOCK_INIT(ebp, eth_dev->data->name);
100 ebp->esb_dev = pci_dev;
101 ebp->esb_base = res->addr;
110 sfc_mem_bar_fini(struct sfc_adapter *sa)
112 efsys_bar_t *ebp = &sa->mem_bar;
114 SFC_BAR_LOCK_DESTROY(ebp);
115 memset(ebp, 0, sizeof(*ebp));
119 sfc_attach(struct sfc_adapter *sa)
121 struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(sa->eth_dev);
125 sfc_log_init(sa, "entry");
127 SFC_ASSERT(sfc_adapter_is_locked(sa));
129 sa->socket_id = rte_socket_id();
131 sfc_log_init(sa, "init mem bar");
132 rc = sfc_mem_bar_init(sa);
134 goto fail_mem_bar_init;
136 sfc_log_init(sa, "get family");
137 rc = efx_family(pci_dev->id.vendor_id, pci_dev->id.device_id,
141 sfc_log_init(sa, "family is %u", sa->family);
143 sfc_log_init(sa, "create nic");
144 rte_spinlock_init(&sa->nic_lock);
145 rc = efx_nic_create(sa->family, (efsys_identifier_t *)sa,
146 &sa->mem_bar, &sa->nic_lock, &enp);
148 goto fail_nic_create;
151 rc = sfc_mcdi_init(sa);
155 sfc_log_init(sa, "probe nic");
156 rc = efx_nic_probe(enp);
160 efx_mcdi_new_epoch(enp);
162 sfc_log_init(sa, "reset nic");
163 rc = efx_nic_reset(enp);
167 /* Initialize NIC to double-check hardware */
168 sfc_log_init(sa, "init nic");
169 rc = efx_nic_init(enp);
173 sfc_log_init(sa, "fini nic");
179 sa->state = SFC_ADAPTER_INITIALIZED;
181 sfc_log_init(sa, "done");
186 sfc_log_init(sa, "unprobe nic");
187 efx_nic_unprobe(enp);
193 sfc_log_init(sa, "destroy nic");
195 efx_nic_destroy(enp);
199 sfc_mem_bar_fini(sa);
202 sfc_log_init(sa, "failed %d", rc);
207 sfc_detach(struct sfc_adapter *sa)
209 efx_nic_t *enp = sa->nic;
211 sfc_log_init(sa, "entry");
213 SFC_ASSERT(sfc_adapter_is_locked(sa));
215 sfc_log_init(sa, "unprobe nic");
216 efx_nic_unprobe(enp);
220 sfc_log_init(sa, "destroy nic");
222 efx_nic_destroy(enp);
224 sfc_mem_bar_fini(sa);
226 sa->state = SFC_ADAPTER_UNINITIALIZED;