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