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