net/sfc: implement Rx queue start and stop operations
[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         rc = sfc_rx_start(sa);
275         if (rc != 0)
276                 goto fail_rx_start;
277
278         sa->state = SFC_ADAPTER_STARTED;
279         sfc_log_init(sa, "done");
280         return 0;
281
282 fail_rx_start:
283         sfc_port_stop(sa);
284
285 fail_port_start:
286         sfc_ev_stop(sa);
287
288 fail_ev_start:
289         sfc_intr_stop(sa);
290
291 fail_intr_start:
292         efx_nic_fini(sa->nic);
293
294 fail_nic_init:
295 fail_set_drv_limits:
296         sa->state = SFC_ADAPTER_CONFIGURED;
297 fail_bad_state:
298         sfc_log_init(sa, "failed %d", rc);
299         return rc;
300 }
301
302 void
303 sfc_stop(struct sfc_adapter *sa)
304 {
305         sfc_log_init(sa, "entry");
306
307         SFC_ASSERT(sfc_adapter_is_locked(sa));
308
309         switch (sa->state) {
310         case SFC_ADAPTER_STARTED:
311                 break;
312         case SFC_ADAPTER_CONFIGURED:
313                 sfc_info(sa, "already stopped");
314                 return;
315         default:
316                 sfc_err(sa, "stop in unexpected state %u", sa->state);
317                 SFC_ASSERT(B_FALSE);
318                 return;
319         }
320
321         sa->state = SFC_ADAPTER_STOPPING;
322
323         sfc_rx_stop(sa);
324         sfc_port_stop(sa);
325         sfc_ev_stop(sa);
326         sfc_intr_stop(sa);
327         efx_nic_fini(sa->nic);
328
329         sa->state = SFC_ADAPTER_CONFIGURED;
330         sfc_log_init(sa, "done");
331 }
332
333 int
334 sfc_configure(struct sfc_adapter *sa)
335 {
336         int rc;
337
338         sfc_log_init(sa, "entry");
339
340         SFC_ASSERT(sfc_adapter_is_locked(sa));
341
342         SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED);
343         sa->state = SFC_ADAPTER_CONFIGURING;
344
345         rc = sfc_check_conf(sa);
346         if (rc != 0)
347                 goto fail_check_conf;
348
349         rc = sfc_intr_init(sa);
350         if (rc != 0)
351                 goto fail_intr_init;
352
353         rc = sfc_ev_init(sa);
354         if (rc != 0)
355                 goto fail_ev_init;
356
357         rc = sfc_port_init(sa);
358         if (rc != 0)
359                 goto fail_port_init;
360
361         rc = sfc_rx_init(sa);
362         if (rc != 0)
363                 goto fail_rx_init;
364
365         sa->state = SFC_ADAPTER_CONFIGURED;
366         sfc_log_init(sa, "done");
367         return 0;
368
369 fail_rx_init:
370         sfc_port_fini(sa);
371
372 fail_port_init:
373         sfc_ev_fini(sa);
374
375 fail_ev_init:
376         sfc_intr_fini(sa);
377
378 fail_intr_init:
379 fail_check_conf:
380         sa->state = SFC_ADAPTER_INITIALIZED;
381         sfc_log_init(sa, "failed %d", rc);
382         return rc;
383 }
384
385 void
386 sfc_close(struct sfc_adapter *sa)
387 {
388         sfc_log_init(sa, "entry");
389
390         SFC_ASSERT(sfc_adapter_is_locked(sa));
391
392         SFC_ASSERT(sa->state == SFC_ADAPTER_CONFIGURED);
393         sa->state = SFC_ADAPTER_CLOSING;
394
395         sfc_rx_fini(sa);
396         sfc_port_fini(sa);
397         sfc_ev_fini(sa);
398         sfc_intr_fini(sa);
399
400         sa->state = SFC_ADAPTER_INITIALIZED;
401         sfc_log_init(sa, "done");
402 }
403
404 static int
405 sfc_mem_bar_init(struct sfc_adapter *sa)
406 {
407         struct rte_eth_dev *eth_dev = sa->eth_dev;
408         struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(eth_dev);
409         efsys_bar_t *ebp = &sa->mem_bar;
410         unsigned int i;
411         struct rte_mem_resource *res;
412
413         for (i = 0; i < RTE_DIM(pci_dev->mem_resource); i++) {
414                 res = &pci_dev->mem_resource[i];
415                 if ((res->len != 0) && (res->phys_addr != 0)) {
416                         /* Found first memory BAR */
417                         SFC_BAR_LOCK_INIT(ebp, eth_dev->data->name);
418                         ebp->esb_rid = i;
419                         ebp->esb_dev = pci_dev;
420                         ebp->esb_base = res->addr;
421                         return 0;
422                 }
423         }
424
425         return EFAULT;
426 }
427
428 static void
429 sfc_mem_bar_fini(struct sfc_adapter *sa)
430 {
431         efsys_bar_t *ebp = &sa->mem_bar;
432
433         SFC_BAR_LOCK_DESTROY(ebp);
434         memset(ebp, 0, sizeof(*ebp));
435 }
436
437 int
438 sfc_attach(struct sfc_adapter *sa)
439 {
440         struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(sa->eth_dev);
441         efx_nic_t *enp;
442         int rc;
443
444         sfc_log_init(sa, "entry");
445
446         SFC_ASSERT(sfc_adapter_is_locked(sa));
447
448         sa->socket_id = rte_socket_id();
449
450         sfc_log_init(sa, "init mem bar");
451         rc = sfc_mem_bar_init(sa);
452         if (rc != 0)
453                 goto fail_mem_bar_init;
454
455         sfc_log_init(sa, "get family");
456         rc = efx_family(pci_dev->id.vendor_id, pci_dev->id.device_id,
457                         &sa->family);
458         if (rc != 0)
459                 goto fail_family;
460         sfc_log_init(sa, "family is %u", sa->family);
461
462         sfc_log_init(sa, "create nic");
463         rte_spinlock_init(&sa->nic_lock);
464         rc = efx_nic_create(sa->family, (efsys_identifier_t *)sa,
465                             &sa->mem_bar, &sa->nic_lock, &enp);
466         if (rc != 0)
467                 goto fail_nic_create;
468         sa->nic = enp;
469
470         rc = sfc_mcdi_init(sa);
471         if (rc != 0)
472                 goto fail_mcdi_init;
473
474         sfc_log_init(sa, "probe nic");
475         rc = efx_nic_probe(enp);
476         if (rc != 0)
477                 goto fail_nic_probe;
478
479         efx_mcdi_new_epoch(enp);
480
481         sfc_log_init(sa, "reset nic");
482         rc = efx_nic_reset(enp);
483         if (rc != 0)
484                 goto fail_nic_reset;
485
486         sfc_log_init(sa, "estimate resource limits");
487         rc = sfc_estimate_resource_limits(sa);
488         if (rc != 0)
489                 goto fail_estimate_rsrc_limits;
490
491         rc = sfc_intr_attach(sa);
492         if (rc != 0)
493                 goto fail_intr_attach;
494
495         sfc_log_init(sa, "fini nic");
496         efx_nic_fini(enp);
497
498         sa->state = SFC_ADAPTER_INITIALIZED;
499
500         sfc_log_init(sa, "done");
501         return 0;
502
503 fail_intr_attach:
504 fail_estimate_rsrc_limits:
505 fail_nic_reset:
506         sfc_log_init(sa, "unprobe nic");
507         efx_nic_unprobe(enp);
508
509 fail_nic_probe:
510         sfc_mcdi_fini(sa);
511
512 fail_mcdi_init:
513         sfc_log_init(sa, "destroy nic");
514         sa->nic = NULL;
515         efx_nic_destroy(enp);
516
517 fail_nic_create:
518 fail_family:
519         sfc_mem_bar_fini(sa);
520
521 fail_mem_bar_init:
522         sfc_log_init(sa, "failed %d", rc);
523         return rc;
524 }
525
526 void
527 sfc_detach(struct sfc_adapter *sa)
528 {
529         efx_nic_t *enp = sa->nic;
530
531         sfc_log_init(sa, "entry");
532
533         SFC_ASSERT(sfc_adapter_is_locked(sa));
534
535         sfc_intr_detach(sa);
536
537         sfc_log_init(sa, "unprobe nic");
538         efx_nic_unprobe(enp);
539
540         sfc_mcdi_fini(sa);
541
542         sfc_log_init(sa, "destroy nic");
543         sa->nic = NULL;
544         efx_nic_destroy(enp);
545
546         sfc_mem_bar_fini(sa);
547
548         sa->state = SFC_ADAPTER_UNINITIALIZED;
549 }