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