59e535d71635f48568eb32bcb138f823ffa2c9fa
[dpdk.git] / drivers / net / sfc / sfc.c
1 /*-
2  *   BSD LICENSE
3  *
4  * Copyright (c) 2016-2017 Solarflare Communications Inc.
5  * All rights reserved.
6  *
7  * This software was jointly developed between OKTET Labs (under contract
8  * for Solarflare) and Solarflare Communications, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  *    this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 /* sysconf() */
33 #include <unistd.h>
34
35 #include <rte_errno.h>
36 #include <rte_alarm.h>
37
38 #include "efx.h"
39
40 #include "sfc.h"
41 #include "sfc_log.h"
42 #include "sfc_ev.h"
43 #include "sfc_rx.h"
44 #include "sfc_tx.h"
45
46
47 int
48 sfc_dma_alloc(const struct sfc_adapter *sa, const char *name, uint16_t id,
49               size_t len, int socket_id, efsys_mem_t *esmp)
50 {
51         const struct rte_memzone *mz;
52
53         sfc_log_init(sa, "name=%s id=%u len=%lu socket_id=%d",
54                      name, id, len, socket_id);
55
56         mz = rte_eth_dma_zone_reserve(sa->eth_dev, name, id, len,
57                                       sysconf(_SC_PAGESIZE), socket_id);
58         if (mz == NULL) {
59                 sfc_err(sa, "cannot reserve DMA zone for %s:%u %#x@%d: %s",
60                         name, (unsigned int)id, (unsigned int)len, socket_id,
61                         rte_strerror(rte_errno));
62                 return ENOMEM;
63         }
64
65         esmp->esm_addr = mz->iova;
66         if (esmp->esm_addr == RTE_BAD_IOVA) {
67                 (void)rte_memzone_free(mz);
68                 return EFAULT;
69         }
70
71         esmp->esm_mz = mz;
72         esmp->esm_base = mz->addr;
73
74         return 0;
75 }
76
77 void
78 sfc_dma_free(const struct sfc_adapter *sa, efsys_mem_t *esmp)
79 {
80         int rc;
81
82         sfc_log_init(sa, "name=%s", esmp->esm_mz->name);
83
84         rc = rte_memzone_free(esmp->esm_mz);
85         if (rc != 0)
86                 sfc_err(sa, "rte_memzone_free(() failed: %d", rc);
87
88         memset(esmp, 0, sizeof(*esmp));
89 }
90
91 static uint32_t
92 sfc_phy_cap_from_link_speeds(uint32_t speeds)
93 {
94         uint32_t phy_caps = 0;
95
96         if (~speeds & ETH_LINK_SPEED_FIXED) {
97                 phy_caps |= (1 << EFX_PHY_CAP_AN);
98                 /*
99                  * If no speeds are specified in the mask, any supported
100                  * may be negotiated
101                  */
102                 if (speeds == ETH_LINK_SPEED_AUTONEG)
103                         phy_caps |=
104                                 (1 << EFX_PHY_CAP_1000FDX) |
105                                 (1 << EFX_PHY_CAP_10000FDX) |
106                                 (1 << EFX_PHY_CAP_40000FDX);
107         }
108         if (speeds & ETH_LINK_SPEED_1G)
109                 phy_caps |= (1 << EFX_PHY_CAP_1000FDX);
110         if (speeds & ETH_LINK_SPEED_10G)
111                 phy_caps |= (1 << EFX_PHY_CAP_10000FDX);
112         if (speeds & ETH_LINK_SPEED_40G)
113                 phy_caps |= (1 << EFX_PHY_CAP_40000FDX);
114
115         return phy_caps;
116 }
117
118 /*
119  * Check requested device level configuration.
120  * Receive and transmit configuration is checked in corresponding
121  * modules.
122  */
123 static int
124 sfc_check_conf(struct sfc_adapter *sa)
125 {
126         const struct rte_eth_conf *conf = &sa->eth_dev->data->dev_conf;
127         int rc = 0;
128
129         sa->port.phy_adv_cap =
130                 sfc_phy_cap_from_link_speeds(conf->link_speeds) &
131                 sa->port.phy_adv_cap_mask;
132         if ((sa->port.phy_adv_cap & ~(1 << EFX_PHY_CAP_AN)) == 0) {
133                 sfc_err(sa, "No link speeds from mask %#x are supported",
134                         conf->link_speeds);
135                 rc = EINVAL;
136         }
137
138         if (conf->lpbk_mode != 0) {
139                 sfc_err(sa, "Loopback not supported");
140                 rc = EINVAL;
141         }
142
143         if (conf->dcb_capability_en != 0) {
144                 sfc_err(sa, "Priority-based flow control not supported");
145                 rc = EINVAL;
146         }
147
148         if (conf->fdir_conf.mode != RTE_FDIR_MODE_NONE) {
149                 sfc_err(sa, "Flow Director not supported");
150                 rc = EINVAL;
151         }
152
153         if ((conf->intr_conf.lsc != 0) &&
154             (sa->intr.type != EFX_INTR_LINE) &&
155             (sa->intr.type != EFX_INTR_MESSAGE)) {
156                 sfc_err(sa, "Link status change interrupt not supported");
157                 rc = EINVAL;
158         }
159
160         if (conf->intr_conf.rxq != 0) {
161                 sfc_err(sa, "Receive queue interrupt not supported");
162                 rc = EINVAL;
163         }
164
165         return rc;
166 }
167
168 /*
169  * Find out maximum number of receive and transmit queues which could be
170  * advertised.
171  *
172  * NIC is kept initialized on success to allow other modules acquire
173  * defaults and capabilities.
174  */
175 static int
176 sfc_estimate_resource_limits(struct sfc_adapter *sa)
177 {
178         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
179         efx_drv_limits_t limits;
180         int rc;
181         uint32_t evq_allocated;
182         uint32_t rxq_allocated;
183         uint32_t txq_allocated;
184
185         memset(&limits, 0, sizeof(limits));
186
187         /* Request at least one Rx and Tx queue */
188         limits.edl_min_rxq_count = 1;
189         limits.edl_min_txq_count = 1;
190         /* Management event queue plus event queue for each Tx and Rx queue */
191         limits.edl_min_evq_count =
192                 1 + limits.edl_min_rxq_count + limits.edl_min_txq_count;
193
194         /* Divide by number of functions to guarantee that all functions
195          * will get promised resources
196          */
197         /* FIXME Divide by number of functions (not 2) below */
198         limits.edl_max_evq_count = encp->enc_evq_limit / 2;
199         SFC_ASSERT(limits.edl_max_evq_count >= limits.edl_min_rxq_count);
200
201         /* Split equally between receive and transmit */
202         limits.edl_max_rxq_count =
203                 MIN(encp->enc_rxq_limit, (limits.edl_max_evq_count - 1) / 2);
204         SFC_ASSERT(limits.edl_max_rxq_count >= limits.edl_min_rxq_count);
205
206         limits.edl_max_txq_count =
207                 MIN(encp->enc_txq_limit,
208                     limits.edl_max_evq_count - 1 - limits.edl_max_rxq_count);
209
210         if (sa->tso)
211                 limits.edl_max_txq_count =
212                         MIN(limits.edl_max_txq_count,
213                             encp->enc_fw_assisted_tso_v2_n_contexts /
214                             encp->enc_hw_pf_count);
215
216         SFC_ASSERT(limits.edl_max_txq_count >= limits.edl_min_rxq_count);
217
218         /* Configure the minimum required resources needed for the
219          * driver to operate, and the maximum desired resources that the
220          * driver is capable of using.
221          */
222         efx_nic_set_drv_limits(sa->nic, &limits);
223
224         sfc_log_init(sa, "init nic");
225         rc = efx_nic_init(sa->nic);
226         if (rc != 0)
227                 goto fail_nic_init;
228
229         /* Find resource dimensions assigned by firmware to this function */
230         rc = efx_nic_get_vi_pool(sa->nic, &evq_allocated, &rxq_allocated,
231                                  &txq_allocated);
232         if (rc != 0)
233                 goto fail_get_vi_pool;
234
235         /* It still may allocate more than maximum, ensure limit */
236         evq_allocated = MIN(evq_allocated, limits.edl_max_evq_count);
237         rxq_allocated = MIN(rxq_allocated, limits.edl_max_rxq_count);
238         txq_allocated = MIN(txq_allocated, limits.edl_max_txq_count);
239
240         /* Subtract management EVQ not used for traffic */
241         SFC_ASSERT(evq_allocated > 0);
242         evq_allocated--;
243
244         /* Right now we use separate EVQ for Rx and Tx */
245         sa->rxq_max = MIN(rxq_allocated, evq_allocated / 2);
246         sa->txq_max = MIN(txq_allocated, evq_allocated - sa->rxq_max);
247
248         /* Keep NIC initialized */
249         return 0;
250
251 fail_get_vi_pool:
252 fail_nic_init:
253         efx_nic_fini(sa->nic);
254         return rc;
255 }
256
257 static int
258 sfc_set_drv_limits(struct sfc_adapter *sa)
259 {
260         const struct rte_eth_dev_data *data = sa->eth_dev->data;
261         efx_drv_limits_t lim;
262
263         memset(&lim, 0, sizeof(lim));
264
265         /* Limits are strict since take into account initial estimation */
266         lim.edl_min_evq_count = lim.edl_max_evq_count =
267                 1 + data->nb_rx_queues + data->nb_tx_queues;
268         lim.edl_min_rxq_count = lim.edl_max_rxq_count = data->nb_rx_queues;
269         lim.edl_min_txq_count = lim.edl_max_txq_count = data->nb_tx_queues;
270
271         return efx_nic_set_drv_limits(sa->nic, &lim);
272 }
273
274 static int
275 sfc_try_start(struct sfc_adapter *sa)
276 {
277         const efx_nic_cfg_t *encp;
278         int rc;
279
280         sfc_log_init(sa, "entry");
281
282         SFC_ASSERT(sfc_adapter_is_locked(sa));
283         SFC_ASSERT(sa->state == SFC_ADAPTER_STARTING);
284
285         sfc_log_init(sa, "set resource limits");
286         rc = sfc_set_drv_limits(sa);
287         if (rc != 0)
288                 goto fail_set_drv_limits;
289
290         sfc_log_init(sa, "init nic");
291         rc = efx_nic_init(sa->nic);
292         if (rc != 0)
293                 goto fail_nic_init;
294
295         encp = efx_nic_cfg_get(sa->nic);
296         if (encp->enc_tunnel_encapsulations_supported != 0) {
297                 sfc_log_init(sa, "apply tunnel config");
298                 rc = efx_tunnel_reconfigure(sa->nic);
299                 if (rc != 0)
300                         goto fail_tunnel_reconfigure;
301         }
302
303         rc = sfc_intr_start(sa);
304         if (rc != 0)
305                 goto fail_intr_start;
306
307         rc = sfc_ev_start(sa);
308         if (rc != 0)
309                 goto fail_ev_start;
310
311         rc = sfc_port_start(sa);
312         if (rc != 0)
313                 goto fail_port_start;
314
315         rc = sfc_rx_start(sa);
316         if (rc != 0)
317                 goto fail_rx_start;
318
319         rc = sfc_tx_start(sa);
320         if (rc != 0)
321                 goto fail_tx_start;
322
323         rc = sfc_flow_start(sa);
324         if (rc != 0)
325                 goto fail_flows_insert;
326
327         sfc_log_init(sa, "done");
328         return 0;
329
330 fail_flows_insert:
331         sfc_tx_stop(sa);
332
333 fail_tx_start:
334         sfc_rx_stop(sa);
335
336 fail_rx_start:
337         sfc_port_stop(sa);
338
339 fail_port_start:
340         sfc_ev_stop(sa);
341
342 fail_ev_start:
343         sfc_intr_stop(sa);
344
345 fail_intr_start:
346 fail_tunnel_reconfigure:
347         efx_nic_fini(sa->nic);
348
349 fail_nic_init:
350 fail_set_drv_limits:
351         sfc_log_init(sa, "failed %d", rc);
352         return rc;
353 }
354
355 int
356 sfc_start(struct sfc_adapter *sa)
357 {
358         unsigned int start_tries = 3;
359         int rc;
360
361         sfc_log_init(sa, "entry");
362
363         SFC_ASSERT(sfc_adapter_is_locked(sa));
364
365         switch (sa->state) {
366         case SFC_ADAPTER_CONFIGURED:
367                 break;
368         case SFC_ADAPTER_STARTED:
369                 sfc_info(sa, "already started");
370                 return 0;
371         default:
372                 rc = EINVAL;
373                 goto fail_bad_state;
374         }
375
376         sa->state = SFC_ADAPTER_STARTING;
377
378         do {
379                 rc = sfc_try_start(sa);
380         } while ((--start_tries > 0) &&
381                  (rc == EIO || rc == EAGAIN || rc == ENOENT || rc == EINVAL));
382
383         if (rc != 0)
384                 goto fail_try_start;
385
386         sa->state = SFC_ADAPTER_STARTED;
387         sfc_log_init(sa, "done");
388         return 0;
389
390 fail_try_start:
391         sa->state = SFC_ADAPTER_CONFIGURED;
392 fail_bad_state:
393         sfc_log_init(sa, "failed %d", rc);
394         return rc;
395 }
396
397 void
398 sfc_stop(struct sfc_adapter *sa)
399 {
400         sfc_log_init(sa, "entry");
401
402         SFC_ASSERT(sfc_adapter_is_locked(sa));
403
404         switch (sa->state) {
405         case SFC_ADAPTER_STARTED:
406                 break;
407         case SFC_ADAPTER_CONFIGURED:
408                 sfc_info(sa, "already stopped");
409                 return;
410         default:
411                 sfc_err(sa, "stop in unexpected state %u", sa->state);
412                 SFC_ASSERT(B_FALSE);
413                 return;
414         }
415
416         sa->state = SFC_ADAPTER_STOPPING;
417
418         sfc_flow_stop(sa);
419         sfc_tx_stop(sa);
420         sfc_rx_stop(sa);
421         sfc_port_stop(sa);
422         sfc_ev_stop(sa);
423         sfc_intr_stop(sa);
424         efx_nic_fini(sa->nic);
425
426         sa->state = SFC_ADAPTER_CONFIGURED;
427         sfc_log_init(sa, "done");
428 }
429
430 static int
431 sfc_restart(struct sfc_adapter *sa)
432 {
433         int rc;
434
435         SFC_ASSERT(sfc_adapter_is_locked(sa));
436
437         if (sa->state != SFC_ADAPTER_STARTED)
438                 return EINVAL;
439
440         sfc_stop(sa);
441
442         rc = sfc_start(sa);
443         if (rc != 0)
444                 sfc_err(sa, "restart failed");
445
446         return rc;
447 }
448
449 static void
450 sfc_restart_if_required(void *arg)
451 {
452         struct sfc_adapter *sa = arg;
453
454         /* If restart is scheduled, clear the flag and do it */
455         if (rte_atomic32_cmpset((volatile uint32_t *)&sa->restart_required,
456                                 1, 0)) {
457                 sfc_adapter_lock(sa);
458                 if (sa->state == SFC_ADAPTER_STARTED)
459                         (void)sfc_restart(sa);
460                 sfc_adapter_unlock(sa);
461         }
462 }
463
464 void
465 sfc_schedule_restart(struct sfc_adapter *sa)
466 {
467         int rc;
468
469         /* Schedule restart alarm if it is not scheduled yet */
470         if (!rte_atomic32_test_and_set(&sa->restart_required))
471                 return;
472
473         rc = rte_eal_alarm_set(1, sfc_restart_if_required, sa);
474         if (rc == -ENOTSUP)
475                 sfc_warn(sa, "alarms are not supported, restart is pending");
476         else if (rc != 0)
477                 sfc_err(sa, "cannot arm restart alarm (rc=%d)", rc);
478         else
479                 sfc_info(sa, "restart scheduled");
480 }
481
482 int
483 sfc_configure(struct sfc_adapter *sa)
484 {
485         int rc;
486
487         sfc_log_init(sa, "entry");
488
489         SFC_ASSERT(sfc_adapter_is_locked(sa));
490
491         SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED ||
492                    sa->state == SFC_ADAPTER_CONFIGURED);
493         sa->state = SFC_ADAPTER_CONFIGURING;
494
495         rc = sfc_check_conf(sa);
496         if (rc != 0)
497                 goto fail_check_conf;
498
499         rc = sfc_intr_configure(sa);
500         if (rc != 0)
501                 goto fail_intr_configure;
502
503         rc = sfc_port_configure(sa);
504         if (rc != 0)
505                 goto fail_port_configure;
506
507         rc = sfc_rx_configure(sa);
508         if (rc != 0)
509                 goto fail_rx_configure;
510
511         rc = sfc_tx_configure(sa);
512         if (rc != 0)
513                 goto fail_tx_configure;
514
515         sa->state = SFC_ADAPTER_CONFIGURED;
516         sfc_log_init(sa, "done");
517         return 0;
518
519 fail_tx_configure:
520         sfc_rx_close(sa);
521
522 fail_rx_configure:
523         sfc_port_close(sa);
524
525 fail_port_configure:
526         sfc_intr_close(sa);
527
528 fail_intr_configure:
529 fail_check_conf:
530         sa->state = SFC_ADAPTER_INITIALIZED;
531         sfc_log_init(sa, "failed %d", rc);
532         return rc;
533 }
534
535 void
536 sfc_close(struct sfc_adapter *sa)
537 {
538         sfc_log_init(sa, "entry");
539
540         SFC_ASSERT(sfc_adapter_is_locked(sa));
541
542         SFC_ASSERT(sa->state == SFC_ADAPTER_CONFIGURED);
543         sa->state = SFC_ADAPTER_CLOSING;
544
545         sfc_tx_close(sa);
546         sfc_rx_close(sa);
547         sfc_port_close(sa);
548         sfc_intr_close(sa);
549
550         sa->state = SFC_ADAPTER_INITIALIZED;
551         sfc_log_init(sa, "done");
552 }
553
554 static int
555 sfc_mem_bar_init(struct sfc_adapter *sa)
556 {
557         struct rte_eth_dev *eth_dev = sa->eth_dev;
558         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
559         efsys_bar_t *ebp = &sa->mem_bar;
560         unsigned int i;
561         struct rte_mem_resource *res;
562
563         for (i = 0; i < RTE_DIM(pci_dev->mem_resource); i++) {
564                 res = &pci_dev->mem_resource[i];
565                 if ((res->len != 0) && (res->phys_addr != 0)) {
566                         /* Found first memory BAR */
567                         SFC_BAR_LOCK_INIT(ebp, eth_dev->data->name);
568                         ebp->esb_rid = i;
569                         ebp->esb_dev = pci_dev;
570                         ebp->esb_base = res->addr;
571                         return 0;
572                 }
573         }
574
575         return EFAULT;
576 }
577
578 static void
579 sfc_mem_bar_fini(struct sfc_adapter *sa)
580 {
581         efsys_bar_t *ebp = &sa->mem_bar;
582
583         SFC_BAR_LOCK_DESTROY(ebp);
584         memset(ebp, 0, sizeof(*ebp));
585 }
586
587 #if EFSYS_OPT_RX_SCALE
588 /*
589  * A fixed RSS key which has a property of being symmetric
590  * (symmetrical flows are distributed to the same CPU)
591  * and also known to give a uniform distribution
592  * (a good distribution of traffic between different CPUs)
593  */
594 static const uint8_t default_rss_key[EFX_RSS_KEY_SIZE] = {
595         0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
596         0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
597         0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
598         0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
599         0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
600 };
601 #endif
602
603 #if EFSYS_OPT_RX_SCALE
604 static int
605 sfc_set_rss_defaults(struct sfc_adapter *sa)
606 {
607         int rc;
608
609         rc = efx_intr_init(sa->nic, sa->intr.type, NULL);
610         if (rc != 0)
611                 goto fail_intr_init;
612
613         rc = efx_ev_init(sa->nic);
614         if (rc != 0)
615                 goto fail_ev_init;
616
617         rc = efx_rx_init(sa->nic);
618         if (rc != 0)
619                 goto fail_rx_init;
620
621         rc = efx_rx_scale_default_support_get(sa->nic, &sa->rss_support);
622         if (rc != 0)
623                 goto fail_scale_support_get;
624
625         rc = efx_rx_hash_default_support_get(sa->nic, &sa->hash_support);
626         if (rc != 0)
627                 goto fail_hash_support_get;
628
629         efx_rx_fini(sa->nic);
630         efx_ev_fini(sa->nic);
631         efx_intr_fini(sa->nic);
632
633         sa->rss_hash_types = sfc_rte_to_efx_hash_type(SFC_RSS_OFFLOADS);
634
635         rte_memcpy(sa->rss_key, default_rss_key, sizeof(sa->rss_key));
636
637         return 0;
638
639 fail_hash_support_get:
640 fail_scale_support_get:
641 fail_rx_init:
642         efx_ev_fini(sa->nic);
643
644 fail_ev_init:
645         efx_intr_fini(sa->nic);
646
647 fail_intr_init:
648         return rc;
649 }
650 #else
651 static int
652 sfc_set_rss_defaults(__rte_unused struct sfc_adapter *sa)
653 {
654         return 0;
655 }
656 #endif
657
658 int
659 sfc_attach(struct sfc_adapter *sa)
660 {
661         const efx_nic_cfg_t *encp;
662         efx_nic_t *enp = sa->nic;
663         int rc;
664
665         sfc_log_init(sa, "entry");
666
667         SFC_ASSERT(sfc_adapter_is_locked(sa));
668
669         efx_mcdi_new_epoch(enp);
670
671         sfc_log_init(sa, "reset nic");
672         rc = efx_nic_reset(enp);
673         if (rc != 0)
674                 goto fail_nic_reset;
675
676         /*
677          * Probed NIC is sufficient for tunnel init.
678          * Initialize tunnel support to be able to use libefx
679          * efx_tunnel_config_udp_{add,remove}() in any state and
680          * efx_tunnel_reconfigure() on start up.
681          */
682         rc = efx_tunnel_init(enp);
683         if (rc != 0)
684                 goto fail_tunnel_init;
685
686         encp = efx_nic_cfg_get(sa->nic);
687
688         if (sa->dp_tx->features & SFC_DP_TX_FEAT_TSO) {
689                 sa->tso = encp->enc_fw_assisted_tso_v2_enabled;
690                 if (!sa->tso)
691                         sfc_warn(sa,
692                                  "TSO support isn't available on this adapter");
693         }
694
695         sfc_log_init(sa, "estimate resource limits");
696         rc = sfc_estimate_resource_limits(sa);
697         if (rc != 0)
698                 goto fail_estimate_rsrc_limits;
699
700         sa->txq_max_entries = encp->enc_txq_max_ndescs;
701         SFC_ASSERT(rte_is_power_of_2(sa->txq_max_entries));
702
703         rc = sfc_intr_attach(sa);
704         if (rc != 0)
705                 goto fail_intr_attach;
706
707         rc = sfc_ev_attach(sa);
708         if (rc != 0)
709                 goto fail_ev_attach;
710
711         rc = sfc_port_attach(sa);
712         if (rc != 0)
713                 goto fail_port_attach;
714
715         rc = sfc_set_rss_defaults(sa);
716         if (rc != 0)
717                 goto fail_set_rss_defaults;
718
719         rc = sfc_filter_attach(sa);
720         if (rc != 0)
721                 goto fail_filter_attach;
722
723         sfc_log_init(sa, "fini nic");
724         efx_nic_fini(enp);
725
726         sfc_flow_init(sa);
727
728         sa->state = SFC_ADAPTER_INITIALIZED;
729
730         sfc_log_init(sa, "done");
731         return 0;
732
733 fail_filter_attach:
734 fail_set_rss_defaults:
735         sfc_port_detach(sa);
736
737 fail_port_attach:
738         sfc_ev_detach(sa);
739
740 fail_ev_attach:
741         sfc_intr_detach(sa);
742
743 fail_intr_attach:
744         efx_nic_fini(sa->nic);
745
746 fail_estimate_rsrc_limits:
747 fail_tunnel_init:
748         efx_tunnel_fini(sa->nic);
749
750 fail_nic_reset:
751
752         sfc_log_init(sa, "failed %d", rc);
753         return rc;
754 }
755
756 void
757 sfc_detach(struct sfc_adapter *sa)
758 {
759         sfc_log_init(sa, "entry");
760
761         SFC_ASSERT(sfc_adapter_is_locked(sa));
762
763         sfc_flow_fini(sa);
764
765         sfc_filter_detach(sa);
766         sfc_port_detach(sa);
767         sfc_ev_detach(sa);
768         sfc_intr_detach(sa);
769         efx_tunnel_fini(sa->nic);
770
771         sa->state = SFC_ADAPTER_UNINITIALIZED;
772 }
773
774 int
775 sfc_probe(struct sfc_adapter *sa)
776 {
777         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(sa->eth_dev);
778         efx_nic_t *enp;
779         int rc;
780
781         sfc_log_init(sa, "entry");
782
783         SFC_ASSERT(sfc_adapter_is_locked(sa));
784
785         sa->socket_id = rte_socket_id();
786         rte_atomic32_init(&sa->restart_required);
787
788         sfc_log_init(sa, "init mem bar");
789         rc = sfc_mem_bar_init(sa);
790         if (rc != 0)
791                 goto fail_mem_bar_init;
792
793         sfc_log_init(sa, "get family");
794         rc = efx_family(pci_dev->id.vendor_id, pci_dev->id.device_id,
795                         &sa->family);
796         if (rc != 0)
797                 goto fail_family;
798         sfc_log_init(sa, "family is %u", sa->family);
799
800         sfc_log_init(sa, "create nic");
801         rte_spinlock_init(&sa->nic_lock);
802         rc = efx_nic_create(sa->family, (efsys_identifier_t *)sa,
803                             &sa->mem_bar, &sa->nic_lock, &enp);
804         if (rc != 0)
805                 goto fail_nic_create;
806         sa->nic = enp;
807
808         rc = sfc_mcdi_init(sa);
809         if (rc != 0)
810                 goto fail_mcdi_init;
811
812         sfc_log_init(sa, "probe nic");
813         rc = efx_nic_probe(enp);
814         if (rc != 0)
815                 goto fail_nic_probe;
816
817         sfc_log_init(sa, "done");
818         return 0;
819
820 fail_nic_probe:
821         sfc_mcdi_fini(sa);
822
823 fail_mcdi_init:
824         sfc_log_init(sa, "destroy nic");
825         sa->nic = NULL;
826         efx_nic_destroy(enp);
827
828 fail_nic_create:
829 fail_family:
830         sfc_mem_bar_fini(sa);
831
832 fail_mem_bar_init:
833         sfc_log_init(sa, "failed %d", rc);
834         return rc;
835 }
836
837 void
838 sfc_unprobe(struct sfc_adapter *sa)
839 {
840         efx_nic_t *enp = sa->nic;
841
842         sfc_log_init(sa, "entry");
843
844         SFC_ASSERT(sfc_adapter_is_locked(sa));
845
846         sfc_log_init(sa, "unprobe nic");
847         efx_nic_unprobe(enp);
848
849         sfc_mcdi_fini(sa);
850
851         /*
852          * Make sure there is no pending alarm to restart since we are
853          * going to free device private which is passed as the callback
854          * opaque data. A new alarm cannot be scheduled since MCDI is
855          * shut down.
856          */
857         rte_eal_alarm_cancel(sfc_restart_if_required, sa);
858
859         sfc_log_init(sa, "destroy nic");
860         sa->nic = NULL;
861         efx_nic_destroy(enp);
862
863         sfc_mem_bar_fini(sa);
864
865         sfc_flow_fini(sa);
866         sa->state = SFC_ADAPTER_UNINITIALIZED;
867 }