38a7e5297bfd8fbfc083e96005165219ae310510
[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 #include "sfc_ev.h"
40
41
42 int
43 sfc_dma_alloc(const struct sfc_adapter *sa, const char *name, uint16_t id,
44               size_t len, int socket_id, efsys_mem_t *esmp)
45 {
46         const struct rte_memzone *mz;
47
48         sfc_log_init(sa, "name=%s id=%u len=%lu socket_id=%d",
49                      name, id, len, socket_id);
50
51         mz = rte_eth_dma_zone_reserve(sa->eth_dev, name, id, len,
52                                       sysconf(_SC_PAGESIZE), socket_id);
53         if (mz == NULL) {
54                 sfc_err(sa, "cannot reserve DMA zone for %s:%u %#x@%d: %s",
55                         name, (unsigned int)id, (unsigned int)len, socket_id,
56                         rte_strerror(rte_errno));
57                 return ENOMEM;
58         }
59
60         esmp->esm_addr = rte_mem_phy2mch(mz->memseg_id, mz->phys_addr);
61         if (esmp->esm_addr == RTE_BAD_PHYS_ADDR) {
62                 (void)rte_memzone_free(mz);
63                 return EFAULT;
64         }
65
66         esmp->esm_mz = mz;
67         esmp->esm_base = mz->addr;
68
69         return 0;
70 }
71
72 void
73 sfc_dma_free(const struct sfc_adapter *sa, efsys_mem_t *esmp)
74 {
75         int rc;
76
77         sfc_log_init(sa, "name=%s", esmp->esm_mz->name);
78
79         rc = rte_memzone_free(esmp->esm_mz);
80         if (rc != 0)
81                 sfc_err(sa, "rte_memzone_free(() failed: %d", rc);
82
83         memset(esmp, 0, sizeof(*esmp));
84 }
85
86 /*
87  * Check requested device level configuration.
88  * Receive and transmit configuration is checked in corresponding
89  * modules.
90  */
91 static int
92 sfc_check_conf(struct sfc_adapter *sa)
93 {
94         const struct rte_eth_conf *conf = &sa->eth_dev->data->dev_conf;
95         int rc = 0;
96
97         if (conf->link_speeds != ETH_LINK_SPEED_AUTONEG) {
98                 sfc_err(sa, "Manual link speed/duplex choice not supported");
99                 rc = EINVAL;
100         }
101
102         if (conf->lpbk_mode != 0) {
103                 sfc_err(sa, "Loopback not supported");
104                 rc = EINVAL;
105         }
106
107         if (conf->dcb_capability_en != 0) {
108                 sfc_err(sa, "Priority-based flow control not supported");
109                 rc = EINVAL;
110         }
111
112         if (conf->fdir_conf.mode != RTE_FDIR_MODE_NONE) {
113                 sfc_err(sa, "Flow Director not supported");
114                 rc = EINVAL;
115         }
116
117         if (conf->intr_conf.lsc != 0) {
118                 sfc_err(sa, "Link status change interrupt not supported");
119                 rc = EINVAL;
120         }
121
122         if (conf->intr_conf.rxq != 0) {
123                 sfc_err(sa, "Receive queue interrupt not supported");
124                 rc = EINVAL;
125         }
126
127         return rc;
128 }
129
130 /*
131  * Find out maximum number of receive and transmit queues which could be
132  * advertised.
133  *
134  * NIC is kept initialized on success to allow other modules acquire
135  * defaults and capabilities.
136  */
137 static int
138 sfc_estimate_resource_limits(struct sfc_adapter *sa)
139 {
140         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
141         efx_drv_limits_t limits;
142         int rc;
143         uint32_t evq_allocated;
144         uint32_t rxq_allocated;
145         uint32_t txq_allocated;
146
147         memset(&limits, 0, sizeof(limits));
148
149         /* Request at least one Rx and Tx queue */
150         limits.edl_min_rxq_count = 1;
151         limits.edl_min_txq_count = 1;
152         /* Management event queue plus event queue for each Tx and Rx queue */
153         limits.edl_min_evq_count =
154                 1 + limits.edl_min_rxq_count + limits.edl_min_txq_count;
155
156         /* Divide by number of functions to guarantee that all functions
157          * will get promised resources
158          */
159         /* FIXME Divide by number of functions (not 2) below */
160         limits.edl_max_evq_count = encp->enc_evq_limit / 2;
161         SFC_ASSERT(limits.edl_max_evq_count >= limits.edl_min_rxq_count);
162
163         /* Split equally between receive and transmit */
164         limits.edl_max_rxq_count =
165                 MIN(encp->enc_rxq_limit, (limits.edl_max_evq_count - 1) / 2);
166         SFC_ASSERT(limits.edl_max_rxq_count >= limits.edl_min_rxq_count);
167
168         limits.edl_max_txq_count =
169                 MIN(encp->enc_txq_limit,
170                     limits.edl_max_evq_count - 1 - limits.edl_max_rxq_count);
171         SFC_ASSERT(limits.edl_max_txq_count >= limits.edl_min_rxq_count);
172
173         /* Configure the minimum required resources needed for the
174          * driver to operate, and the maximum desired resources that the
175          * driver is capable of using.
176          */
177         efx_nic_set_drv_limits(sa->nic, &limits);
178
179         sfc_log_init(sa, "init nic");
180         rc = efx_nic_init(sa->nic);
181         if (rc != 0)
182                 goto fail_nic_init;
183
184         /* Find resource dimensions assigned by firmware to this function */
185         rc = efx_nic_get_vi_pool(sa->nic, &evq_allocated, &rxq_allocated,
186                                  &txq_allocated);
187         if (rc != 0)
188                 goto fail_get_vi_pool;
189
190         /* It still may allocate more than maximum, ensure limit */
191         evq_allocated = MIN(evq_allocated, limits.edl_max_evq_count);
192         rxq_allocated = MIN(rxq_allocated, limits.edl_max_rxq_count);
193         txq_allocated = MIN(txq_allocated, limits.edl_max_txq_count);
194
195         /* Subtract management EVQ not used for traffic */
196         SFC_ASSERT(evq_allocated > 0);
197         evq_allocated--;
198
199         /* Right now we use separate EVQ for Rx and Tx */
200         sa->rxq_max = MIN(rxq_allocated, evq_allocated / 2);
201         sa->txq_max = MIN(txq_allocated, evq_allocated - sa->rxq_max);
202
203         /* Keep NIC initialized */
204         return 0;
205
206 fail_get_vi_pool:
207 fail_nic_init:
208         efx_nic_fini(sa->nic);
209         return rc;
210 }
211
212 static int
213 sfc_set_drv_limits(struct sfc_adapter *sa)
214 {
215         const struct rte_eth_dev_data *data = sa->eth_dev->data;
216         efx_drv_limits_t lim;
217
218         memset(&lim, 0, sizeof(lim));
219
220         /* Limits are strict since take into account initial estimation */
221         lim.edl_min_evq_count = lim.edl_max_evq_count =
222                 1 + data->nb_rx_queues + data->nb_tx_queues;
223         lim.edl_min_rxq_count = lim.edl_max_rxq_count = data->nb_rx_queues;
224         lim.edl_min_txq_count = lim.edl_max_txq_count = data->nb_tx_queues;
225
226         return efx_nic_set_drv_limits(sa->nic, &lim);
227 }
228
229 int
230 sfc_start(struct sfc_adapter *sa)
231 {
232         int rc;
233
234         sfc_log_init(sa, "entry");
235
236         SFC_ASSERT(sfc_adapter_is_locked(sa));
237
238         switch (sa->state) {
239         case SFC_ADAPTER_CONFIGURED:
240                 break;
241         case SFC_ADAPTER_STARTED:
242                 sfc_info(sa, "already started");
243                 return 0;
244         default:
245                 rc = EINVAL;
246                 goto fail_bad_state;
247         }
248
249         sa->state = SFC_ADAPTER_STARTING;
250
251         sfc_log_init(sa, "set resource limits");
252         rc = sfc_set_drv_limits(sa);
253         if (rc != 0)
254                 goto fail_set_drv_limits;
255
256         sfc_log_init(sa, "init nic");
257         rc = efx_nic_init(sa->nic);
258         if (rc != 0)
259                 goto fail_nic_init;
260
261         rc = sfc_intr_start(sa);
262         if (rc != 0)
263                 goto fail_intr_start;
264
265         rc = sfc_ev_start(sa);
266         if (rc != 0)
267                 goto fail_ev_start;
268
269         sa->state = SFC_ADAPTER_STARTED;
270         sfc_log_init(sa, "done");
271         return 0;
272
273 fail_ev_start:
274         sfc_intr_stop(sa);
275
276 fail_intr_start:
277         efx_nic_fini(sa->nic);
278
279 fail_nic_init:
280 fail_set_drv_limits:
281         sa->state = SFC_ADAPTER_CONFIGURED;
282 fail_bad_state:
283         sfc_log_init(sa, "failed %d", rc);
284         return rc;
285 }
286
287 void
288 sfc_stop(struct sfc_adapter *sa)
289 {
290         sfc_log_init(sa, "entry");
291
292         SFC_ASSERT(sfc_adapter_is_locked(sa));
293
294         switch (sa->state) {
295         case SFC_ADAPTER_STARTED:
296                 break;
297         case SFC_ADAPTER_CONFIGURED:
298                 sfc_info(sa, "already stopped");
299                 return;
300         default:
301                 sfc_err(sa, "stop in unexpected state %u", sa->state);
302                 SFC_ASSERT(B_FALSE);
303                 return;
304         }
305
306         sa->state = SFC_ADAPTER_STOPPING;
307
308         sfc_ev_stop(sa);
309         sfc_intr_stop(sa);
310         efx_nic_fini(sa->nic);
311
312         sa->state = SFC_ADAPTER_CONFIGURED;
313         sfc_log_init(sa, "done");
314 }
315
316 int
317 sfc_configure(struct sfc_adapter *sa)
318 {
319         int rc;
320
321         sfc_log_init(sa, "entry");
322
323         SFC_ASSERT(sfc_adapter_is_locked(sa));
324
325         SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED);
326         sa->state = SFC_ADAPTER_CONFIGURING;
327
328         rc = sfc_check_conf(sa);
329         if (rc != 0)
330                 goto fail_check_conf;
331
332         rc = sfc_intr_init(sa);
333         if (rc != 0)
334                 goto fail_intr_init;
335
336         rc = sfc_ev_init(sa);
337         if (rc != 0)
338                 goto fail_ev_init;
339
340         sa->state = SFC_ADAPTER_CONFIGURED;
341         sfc_log_init(sa, "done");
342         return 0;
343
344 fail_ev_init:
345         sfc_intr_fini(sa);
346
347 fail_intr_init:
348 fail_check_conf:
349         sa->state = SFC_ADAPTER_INITIALIZED;
350         sfc_log_init(sa, "failed %d", rc);
351         return rc;
352 }
353
354 void
355 sfc_close(struct sfc_adapter *sa)
356 {
357         sfc_log_init(sa, "entry");
358
359         SFC_ASSERT(sfc_adapter_is_locked(sa));
360
361         SFC_ASSERT(sa->state == SFC_ADAPTER_CONFIGURED);
362         sa->state = SFC_ADAPTER_CLOSING;
363
364         sfc_ev_fini(sa);
365         sfc_intr_fini(sa);
366
367         sa->state = SFC_ADAPTER_INITIALIZED;
368         sfc_log_init(sa, "done");
369 }
370
371 static int
372 sfc_mem_bar_init(struct sfc_adapter *sa)
373 {
374         struct rte_eth_dev *eth_dev = sa->eth_dev;
375         struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(eth_dev);
376         efsys_bar_t *ebp = &sa->mem_bar;
377         unsigned int i;
378         struct rte_mem_resource *res;
379
380         for (i = 0; i < RTE_DIM(pci_dev->mem_resource); i++) {
381                 res = &pci_dev->mem_resource[i];
382                 if ((res->len != 0) && (res->phys_addr != 0)) {
383                         /* Found first memory BAR */
384                         SFC_BAR_LOCK_INIT(ebp, eth_dev->data->name);
385                         ebp->esb_rid = i;
386                         ebp->esb_dev = pci_dev;
387                         ebp->esb_base = res->addr;
388                         return 0;
389                 }
390         }
391
392         return EFAULT;
393 }
394
395 static void
396 sfc_mem_bar_fini(struct sfc_adapter *sa)
397 {
398         efsys_bar_t *ebp = &sa->mem_bar;
399
400         SFC_BAR_LOCK_DESTROY(ebp);
401         memset(ebp, 0, sizeof(*ebp));
402 }
403
404 int
405 sfc_attach(struct sfc_adapter *sa)
406 {
407         struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(sa->eth_dev);
408         efx_nic_t *enp;
409         int rc;
410
411         sfc_log_init(sa, "entry");
412
413         SFC_ASSERT(sfc_adapter_is_locked(sa));
414
415         sa->socket_id = rte_socket_id();
416
417         sfc_log_init(sa, "init mem bar");
418         rc = sfc_mem_bar_init(sa);
419         if (rc != 0)
420                 goto fail_mem_bar_init;
421
422         sfc_log_init(sa, "get family");
423         rc = efx_family(pci_dev->id.vendor_id, pci_dev->id.device_id,
424                         &sa->family);
425         if (rc != 0)
426                 goto fail_family;
427         sfc_log_init(sa, "family is %u", sa->family);
428
429         sfc_log_init(sa, "create nic");
430         rte_spinlock_init(&sa->nic_lock);
431         rc = efx_nic_create(sa->family, (efsys_identifier_t *)sa,
432                             &sa->mem_bar, &sa->nic_lock, &enp);
433         if (rc != 0)
434                 goto fail_nic_create;
435         sa->nic = enp;
436
437         rc = sfc_mcdi_init(sa);
438         if (rc != 0)
439                 goto fail_mcdi_init;
440
441         sfc_log_init(sa, "probe nic");
442         rc = efx_nic_probe(enp);
443         if (rc != 0)
444                 goto fail_nic_probe;
445
446         efx_mcdi_new_epoch(enp);
447
448         sfc_log_init(sa, "reset nic");
449         rc = efx_nic_reset(enp);
450         if (rc != 0)
451                 goto fail_nic_reset;
452
453         sfc_log_init(sa, "estimate resource limits");
454         rc = sfc_estimate_resource_limits(sa);
455         if (rc != 0)
456                 goto fail_estimate_rsrc_limits;
457
458         rc = sfc_intr_attach(sa);
459         if (rc != 0)
460                 goto fail_intr_attach;
461
462         sfc_log_init(sa, "fini nic");
463         efx_nic_fini(enp);
464
465         sa->state = SFC_ADAPTER_INITIALIZED;
466
467         sfc_log_init(sa, "done");
468         return 0;
469
470 fail_intr_attach:
471 fail_estimate_rsrc_limits:
472 fail_nic_reset:
473         sfc_log_init(sa, "unprobe nic");
474         efx_nic_unprobe(enp);
475
476 fail_nic_probe:
477         sfc_mcdi_fini(sa);
478
479 fail_mcdi_init:
480         sfc_log_init(sa, "destroy nic");
481         sa->nic = NULL;
482         efx_nic_destroy(enp);
483
484 fail_nic_create:
485 fail_family:
486         sfc_mem_bar_fini(sa);
487
488 fail_mem_bar_init:
489         sfc_log_init(sa, "failed %d", rc);
490         return rc;
491 }
492
493 void
494 sfc_detach(struct sfc_adapter *sa)
495 {
496         efx_nic_t *enp = sa->nic;
497
498         sfc_log_init(sa, "entry");
499
500         SFC_ASSERT(sfc_adapter_is_locked(sa));
501
502         sfc_intr_detach(sa);
503
504         sfc_log_init(sa, "unprobe nic");
505         efx_nic_unprobe(enp);
506
507         sfc_mcdi_fini(sa);
508
509         sfc_log_init(sa, "destroy nic");
510         sa->nic = NULL;
511         efx_nic_destroy(enp);
512
513         sfc_mem_bar_fini(sa);
514
515         sa->state = SFC_ADAPTER_UNINITIALIZED;
516 }