6f86c07e08810dedd161299eeeb59848526bc391
[dpdk.git] / drivers / net / sfc / sfc.c
1 /*-
2  * Copyright (c) 2016 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * This software was jointly developed between OKTET Labs (under contract
6  * for Solarflare) and Solarflare Communications, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
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.
16  *
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.
28  */
29
30 /* sysconf() */
31 #include <unistd.h>
32
33 #include <rte_errno.h>
34
35 #include "efx.h"
36
37 #include "sfc.h"
38 #include "sfc_log.h"
39
40
41 int
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)
44 {
45         const struct rte_memzone *mz;
46
47         sfc_log_init(sa, "name=%s id=%u len=%lu socket_id=%d",
48                      name, id, len, socket_id);
49
50         mz = rte_eth_dma_zone_reserve(sa->eth_dev, name, id, len,
51                                       sysconf(_SC_PAGESIZE), socket_id);
52         if (mz == NULL) {
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));
56                 return ENOMEM;
57         }
58
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);
62                 return EFAULT;
63         }
64
65         esmp->esm_mz = mz;
66         esmp->esm_base = mz->addr;
67
68         return 0;
69 }
70
71 void
72 sfc_dma_free(const struct sfc_adapter *sa, efsys_mem_t *esmp)
73 {
74         int rc;
75
76         sfc_log_init(sa, "name=%s", esmp->esm_mz->name);
77
78         rc = rte_memzone_free(esmp->esm_mz);
79         if (rc != 0)
80                 sfc_err(sa, "rte_memzone_free(() failed: %d", rc);
81
82         memset(esmp, 0, sizeof(*esmp));
83 }
84
85 static int
86 sfc_mem_bar_init(struct sfc_adapter *sa)
87 {
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;
91         unsigned int i;
92         struct rte_mem_resource *res;
93
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);
99                         ebp->esb_rid = i;
100                         ebp->esb_dev = pci_dev;
101                         ebp->esb_base = res->addr;
102                         return 0;
103                 }
104         }
105
106         return EFAULT;
107 }
108
109 static void
110 sfc_mem_bar_fini(struct sfc_adapter *sa)
111 {
112         efsys_bar_t *ebp = &sa->mem_bar;
113
114         SFC_BAR_LOCK_DESTROY(ebp);
115         memset(ebp, 0, sizeof(*ebp));
116 }
117
118 int
119 sfc_attach(struct sfc_adapter *sa)
120 {
121         struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(sa->eth_dev);
122         efx_nic_t *enp;
123         int rc;
124
125         sfc_log_init(sa, "entry");
126
127         SFC_ASSERT(sfc_adapter_is_locked(sa));
128
129         sa->socket_id = rte_socket_id();
130
131         sfc_log_init(sa, "init mem bar");
132         rc = sfc_mem_bar_init(sa);
133         if (rc != 0)
134                 goto fail_mem_bar_init;
135
136         sfc_log_init(sa, "get family");
137         rc = efx_family(pci_dev->id.vendor_id, pci_dev->id.device_id,
138                         &sa->family);
139         if (rc != 0)
140                 goto fail_family;
141         sfc_log_init(sa, "family is %u", sa->family);
142
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);
147         if (rc != 0)
148                 goto fail_nic_create;
149         sa->nic = enp;
150
151         rc = sfc_mcdi_init(sa);
152         if (rc != 0)
153                 goto fail_mcdi_init;
154
155         sfc_log_init(sa, "probe nic");
156         rc = efx_nic_probe(enp);
157         if (rc != 0)
158                 goto fail_nic_probe;
159
160         efx_mcdi_new_epoch(enp);
161
162         sfc_log_init(sa, "reset nic");
163         rc = efx_nic_reset(enp);
164         if (rc != 0)
165                 goto fail_nic_reset;
166
167         /* Initialize NIC to double-check hardware */
168         sfc_log_init(sa, "init nic");
169         rc = efx_nic_init(enp);
170         if (rc != 0)
171                 goto fail_nic_init;
172
173         sfc_log_init(sa, "fini nic");
174         efx_nic_fini(enp);
175
176         sa->rxq_max = 1;
177         sa->txq_max = 1;
178
179         sa->state = SFC_ADAPTER_INITIALIZED;
180
181         sfc_log_init(sa, "done");
182         return 0;
183
184 fail_nic_init:
185 fail_nic_reset:
186         sfc_log_init(sa, "unprobe nic");
187         efx_nic_unprobe(enp);
188
189 fail_nic_probe:
190         sfc_mcdi_fini(sa);
191
192 fail_mcdi_init:
193         sfc_log_init(sa, "destroy nic");
194         sa->nic = NULL;
195         efx_nic_destroy(enp);
196
197 fail_nic_create:
198 fail_family:
199         sfc_mem_bar_fini(sa);
200
201 fail_mem_bar_init:
202         sfc_log_init(sa, "failed %d", rc);
203         return rc;
204 }
205
206 void
207 sfc_detach(struct sfc_adapter *sa)
208 {
209         efx_nic_t *enp = sa->nic;
210
211         sfc_log_init(sa, "entry");
212
213         SFC_ASSERT(sfc_adapter_is_locked(sa));
214
215         sfc_log_init(sa, "unprobe nic");
216         efx_nic_unprobe(enp);
217
218         sfc_mcdi_fini(sa);
219
220         sfc_log_init(sa, "destroy nic");
221         sa->nic = NULL;
222         efx_nic_destroy(enp);
223
224         sfc_mem_bar_fini(sa);
225
226         sa->state = SFC_ADAPTER_UNINITIALIZED;
227 }