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