net/sfc: add device configuration checks
[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 /*
86  * Check requested device level configuration.
87  * Receive and transmit configuration is checked in corresponding
88  * modules.
89  */
90 static int
91 sfc_check_conf(struct sfc_adapter *sa)
92 {
93         const struct rte_eth_conf *conf = &sa->eth_dev->data->dev_conf;
94         int rc = 0;
95
96         if (conf->link_speeds != ETH_LINK_SPEED_AUTONEG) {
97                 sfc_err(sa, "Manual link speed/duplex choice not supported");
98                 rc = EINVAL;
99         }
100
101         if (conf->lpbk_mode != 0) {
102                 sfc_err(sa, "Loopback not supported");
103                 rc = EINVAL;
104         }
105
106         if (conf->dcb_capability_en != 0) {
107                 sfc_err(sa, "Priority-based flow control not supported");
108                 rc = EINVAL;
109         }
110
111         if (conf->fdir_conf.mode != RTE_FDIR_MODE_NONE) {
112                 sfc_err(sa, "Flow Director not supported");
113                 rc = EINVAL;
114         }
115
116         if (conf->intr_conf.lsc != 0) {
117                 sfc_err(sa, "Link status change interrupt not supported");
118                 rc = EINVAL;
119         }
120
121         if (conf->intr_conf.rxq != 0) {
122                 sfc_err(sa, "Receive queue interrupt not supported");
123                 rc = EINVAL;
124         }
125
126         return rc;
127 }
128
129 int
130 sfc_configure(struct sfc_adapter *sa)
131 {
132         int rc;
133
134         sfc_log_init(sa, "entry");
135
136         SFC_ASSERT(sfc_adapter_is_locked(sa));
137
138         SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED);
139         sa->state = SFC_ADAPTER_CONFIGURING;
140
141         rc = sfc_check_conf(sa);
142         if (rc != 0)
143                 goto fail_check_conf;
144
145         sa->state = SFC_ADAPTER_CONFIGURED;
146         sfc_log_init(sa, "done");
147         return 0;
148
149 fail_check_conf:
150         sa->state = SFC_ADAPTER_INITIALIZED;
151         sfc_log_init(sa, "failed %d", rc);
152         return rc;
153 }
154
155 void
156 sfc_close(struct sfc_adapter *sa)
157 {
158         sfc_log_init(sa, "entry");
159
160         SFC_ASSERT(sfc_adapter_is_locked(sa));
161
162         SFC_ASSERT(sa->state == SFC_ADAPTER_CONFIGURED);
163         sa->state = SFC_ADAPTER_CLOSING;
164
165         sa->state = SFC_ADAPTER_INITIALIZED;
166         sfc_log_init(sa, "done");
167 }
168
169 static int
170 sfc_mem_bar_init(struct sfc_adapter *sa)
171 {
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;
175         unsigned int i;
176         struct rte_mem_resource *res;
177
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);
183                         ebp->esb_rid = i;
184                         ebp->esb_dev = pci_dev;
185                         ebp->esb_base = res->addr;
186                         return 0;
187                 }
188         }
189
190         return EFAULT;
191 }
192
193 static void
194 sfc_mem_bar_fini(struct sfc_adapter *sa)
195 {
196         efsys_bar_t *ebp = &sa->mem_bar;
197
198         SFC_BAR_LOCK_DESTROY(ebp);
199         memset(ebp, 0, sizeof(*ebp));
200 }
201
202 int
203 sfc_attach(struct sfc_adapter *sa)
204 {
205         struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(sa->eth_dev);
206         efx_nic_t *enp;
207         int rc;
208
209         sfc_log_init(sa, "entry");
210
211         SFC_ASSERT(sfc_adapter_is_locked(sa));
212
213         sa->socket_id = rte_socket_id();
214
215         sfc_log_init(sa, "init mem bar");
216         rc = sfc_mem_bar_init(sa);
217         if (rc != 0)
218                 goto fail_mem_bar_init;
219
220         sfc_log_init(sa, "get family");
221         rc = efx_family(pci_dev->id.vendor_id, pci_dev->id.device_id,
222                         &sa->family);
223         if (rc != 0)
224                 goto fail_family;
225         sfc_log_init(sa, "family is %u", sa->family);
226
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);
231         if (rc != 0)
232                 goto fail_nic_create;
233         sa->nic = enp;
234
235         rc = sfc_mcdi_init(sa);
236         if (rc != 0)
237                 goto fail_mcdi_init;
238
239         sfc_log_init(sa, "probe nic");
240         rc = efx_nic_probe(enp);
241         if (rc != 0)
242                 goto fail_nic_probe;
243
244         efx_mcdi_new_epoch(enp);
245
246         sfc_log_init(sa, "reset nic");
247         rc = efx_nic_reset(enp);
248         if (rc != 0)
249                 goto fail_nic_reset;
250
251         /* Initialize NIC to double-check hardware */
252         sfc_log_init(sa, "init nic");
253         rc = efx_nic_init(enp);
254         if (rc != 0)
255                 goto fail_nic_init;
256
257         sfc_log_init(sa, "fini nic");
258         efx_nic_fini(enp);
259
260         sa->rxq_max = 1;
261         sa->txq_max = 1;
262
263         sa->state = SFC_ADAPTER_INITIALIZED;
264
265         sfc_log_init(sa, "done");
266         return 0;
267
268 fail_nic_init:
269 fail_nic_reset:
270         sfc_log_init(sa, "unprobe nic");
271         efx_nic_unprobe(enp);
272
273 fail_nic_probe:
274         sfc_mcdi_fini(sa);
275
276 fail_mcdi_init:
277         sfc_log_init(sa, "destroy nic");
278         sa->nic = NULL;
279         efx_nic_destroy(enp);
280
281 fail_nic_create:
282 fail_family:
283         sfc_mem_bar_fini(sa);
284
285 fail_mem_bar_init:
286         sfc_log_init(sa, "failed %d", rc);
287         return rc;
288 }
289
290 void
291 sfc_detach(struct sfc_adapter *sa)
292 {
293         efx_nic_t *enp = sa->nic;
294
295         sfc_log_init(sa, "entry");
296
297         SFC_ASSERT(sfc_adapter_is_locked(sa));
298
299         sfc_log_init(sa, "unprobe nic");
300         efx_nic_unprobe(enp);
301
302         sfc_mcdi_fini(sa);
303
304         sfc_log_init(sa, "destroy nic");
305         sa->nic = NULL;
306         efx_nic_destroy(enp);
307
308         sfc_mem_bar_fini(sa);
309
310         sa->state = SFC_ADAPTER_UNINITIALIZED;
311 }