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