net/sfc: handle MC reboot event
[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 int
275 sfc_start(struct sfc_adapter *sa)
276 {
277         int rc;
278
279         sfc_log_init(sa, "entry");
280
281         SFC_ASSERT(sfc_adapter_is_locked(sa));
282
283         switch (sa->state) {
284         case SFC_ADAPTER_CONFIGURED:
285                 break;
286         case SFC_ADAPTER_STARTED:
287                 sfc_info(sa, "already started");
288                 return 0;
289         default:
290                 rc = EINVAL;
291                 goto fail_bad_state;
292         }
293
294         sa->state = SFC_ADAPTER_STARTING;
295
296         sfc_log_init(sa, "set resource limits");
297         rc = sfc_set_drv_limits(sa);
298         if (rc != 0)
299                 goto fail_set_drv_limits;
300
301         sfc_log_init(sa, "init nic");
302         rc = efx_nic_init(sa->nic);
303         if (rc != 0)
304                 goto fail_nic_init;
305
306         rc = sfc_intr_start(sa);
307         if (rc != 0)
308                 goto fail_intr_start;
309
310         rc = sfc_ev_start(sa);
311         if (rc != 0)
312                 goto fail_ev_start;
313
314         rc = sfc_port_start(sa);
315         if (rc != 0)
316                 goto fail_port_start;
317
318         rc = sfc_rx_start(sa);
319         if (rc != 0)
320                 goto fail_rx_start;
321
322         rc = sfc_tx_start(sa);
323         if (rc != 0)
324                 goto fail_tx_start;
325
326         rc = sfc_flow_start(sa);
327         if (rc != 0)
328                 goto fail_flows_insert;
329
330         sa->state = SFC_ADAPTER_STARTED;
331         sfc_log_init(sa, "done");
332         return 0;
333
334 fail_flows_insert:
335         sfc_tx_stop(sa);
336
337 fail_tx_start:
338         sfc_rx_stop(sa);
339
340 fail_rx_start:
341         sfc_port_stop(sa);
342
343 fail_port_start:
344         sfc_ev_stop(sa);
345
346 fail_ev_start:
347         sfc_intr_stop(sa);
348
349 fail_intr_start:
350         efx_nic_fini(sa->nic);
351
352 fail_nic_init:
353 fail_set_drv_limits:
354         sa->state = SFC_ADAPTER_CONFIGURED;
355 fail_bad_state:
356         sfc_log_init(sa, "failed %d", rc);
357         return rc;
358 }
359
360 void
361 sfc_stop(struct sfc_adapter *sa)
362 {
363         sfc_log_init(sa, "entry");
364
365         SFC_ASSERT(sfc_adapter_is_locked(sa));
366
367         switch (sa->state) {
368         case SFC_ADAPTER_STARTED:
369                 break;
370         case SFC_ADAPTER_CONFIGURED:
371                 sfc_info(sa, "already stopped");
372                 return;
373         default:
374                 sfc_err(sa, "stop in unexpected state %u", sa->state);
375                 SFC_ASSERT(B_FALSE);
376                 return;
377         }
378
379         sa->state = SFC_ADAPTER_STOPPING;
380
381         sfc_flow_stop(sa);
382         sfc_tx_stop(sa);
383         sfc_rx_stop(sa);
384         sfc_port_stop(sa);
385         sfc_ev_stop(sa);
386         sfc_intr_stop(sa);
387         efx_nic_fini(sa->nic);
388
389         sa->state = SFC_ADAPTER_CONFIGURED;
390         sfc_log_init(sa, "done");
391 }
392
393 static int
394 sfc_restart(struct sfc_adapter *sa)
395 {
396         int rc;
397
398         SFC_ASSERT(sfc_adapter_is_locked(sa));
399
400         if (sa->state != SFC_ADAPTER_STARTED)
401                 return EINVAL;
402
403         sfc_stop(sa);
404
405         rc = sfc_start(sa);
406         if (rc != 0)
407                 sfc_err(sa, "restart failed");
408
409         return rc;
410 }
411
412 static void
413 sfc_restart_if_required(void *arg)
414 {
415         struct sfc_adapter *sa = arg;
416
417         /* If restart is scheduled, clear the flag and do it */
418         if (rte_atomic32_cmpset((volatile uint32_t *)&sa->restart_required,
419                                 1, 0)) {
420                 sfc_adapter_lock(sa);
421                 if (sa->state == SFC_ADAPTER_STARTED)
422                         (void)sfc_restart(sa);
423                 sfc_adapter_unlock(sa);
424         }
425 }
426
427 void
428 sfc_schedule_restart(struct sfc_adapter *sa)
429 {
430         int rc;
431
432         /* Schedule restart alarm if it is not scheduled yet */
433         if (!rte_atomic32_test_and_set(&sa->restart_required))
434                 return;
435
436         rc = rte_eal_alarm_set(1, sfc_restart_if_required, sa);
437         if (rc == -ENOTSUP)
438                 sfc_warn(sa, "alarms are not supported, restart is pending");
439         else if (rc != 0)
440                 sfc_err(sa, "cannot arm restart alarm (rc=%d)", rc);
441         else
442                 sfc_info(sa, "restart scheduled");
443 }
444
445 int
446 sfc_configure(struct sfc_adapter *sa)
447 {
448         int rc;
449
450         sfc_log_init(sa, "entry");
451
452         SFC_ASSERT(sfc_adapter_is_locked(sa));
453
454         SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED ||
455                    sa->state == SFC_ADAPTER_CONFIGURED);
456         sa->state = SFC_ADAPTER_CONFIGURING;
457
458         rc = sfc_check_conf(sa);
459         if (rc != 0)
460                 goto fail_check_conf;
461
462         rc = sfc_intr_configure(sa);
463         if (rc != 0)
464                 goto fail_intr_configure;
465
466         rc = sfc_port_configure(sa);
467         if (rc != 0)
468                 goto fail_port_configure;
469
470         rc = sfc_rx_configure(sa);
471         if (rc != 0)
472                 goto fail_rx_configure;
473
474         rc = sfc_tx_configure(sa);
475         if (rc != 0)
476                 goto fail_tx_configure;
477
478         sa->state = SFC_ADAPTER_CONFIGURED;
479         sfc_log_init(sa, "done");
480         return 0;
481
482 fail_tx_configure:
483         sfc_rx_close(sa);
484
485 fail_rx_configure:
486         sfc_port_close(sa);
487
488 fail_port_configure:
489         sfc_intr_close(sa);
490
491 fail_intr_configure:
492 fail_check_conf:
493         sa->state = SFC_ADAPTER_INITIALIZED;
494         sfc_log_init(sa, "failed %d", rc);
495         return rc;
496 }
497
498 void
499 sfc_close(struct sfc_adapter *sa)
500 {
501         sfc_log_init(sa, "entry");
502
503         SFC_ASSERT(sfc_adapter_is_locked(sa));
504
505         SFC_ASSERT(sa->state == SFC_ADAPTER_CONFIGURED);
506         sa->state = SFC_ADAPTER_CLOSING;
507
508         sfc_tx_close(sa);
509         sfc_rx_close(sa);
510         sfc_port_close(sa);
511         sfc_intr_close(sa);
512
513         sa->state = SFC_ADAPTER_INITIALIZED;
514         sfc_log_init(sa, "done");
515 }
516
517 static int
518 sfc_mem_bar_init(struct sfc_adapter *sa)
519 {
520         struct rte_eth_dev *eth_dev = sa->eth_dev;
521         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
522         efsys_bar_t *ebp = &sa->mem_bar;
523         unsigned int i;
524         struct rte_mem_resource *res;
525
526         for (i = 0; i < RTE_DIM(pci_dev->mem_resource); i++) {
527                 res = &pci_dev->mem_resource[i];
528                 if ((res->len != 0) && (res->phys_addr != 0)) {
529                         /* Found first memory BAR */
530                         SFC_BAR_LOCK_INIT(ebp, eth_dev->data->name);
531                         ebp->esb_rid = i;
532                         ebp->esb_dev = pci_dev;
533                         ebp->esb_base = res->addr;
534                         return 0;
535                 }
536         }
537
538         return EFAULT;
539 }
540
541 static void
542 sfc_mem_bar_fini(struct sfc_adapter *sa)
543 {
544         efsys_bar_t *ebp = &sa->mem_bar;
545
546         SFC_BAR_LOCK_DESTROY(ebp);
547         memset(ebp, 0, sizeof(*ebp));
548 }
549
550 #if EFSYS_OPT_RX_SCALE
551 /*
552  * A fixed RSS key which has a property of being symmetric
553  * (symmetrical flows are distributed to the same CPU)
554  * and also known to give a uniform distribution
555  * (a good distribution of traffic between different CPUs)
556  */
557 static const uint8_t default_rss_key[EFX_RSS_KEY_SIZE] = {
558         0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
559         0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
560         0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
561         0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
562         0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a, 0x6d, 0x5a,
563 };
564 #endif
565
566 #if EFSYS_OPT_RX_SCALE
567 static int
568 sfc_set_rss_defaults(struct sfc_adapter *sa)
569 {
570         int rc;
571
572         rc = efx_intr_init(sa->nic, sa->intr.type, NULL);
573         if (rc != 0)
574                 goto fail_intr_init;
575
576         rc = efx_ev_init(sa->nic);
577         if (rc != 0)
578                 goto fail_ev_init;
579
580         rc = efx_rx_init(sa->nic);
581         if (rc != 0)
582                 goto fail_rx_init;
583
584         rc = efx_rx_scale_default_support_get(sa->nic, &sa->rss_support);
585         if (rc != 0)
586                 goto fail_scale_support_get;
587
588         rc = efx_rx_hash_default_support_get(sa->nic, &sa->hash_support);
589         if (rc != 0)
590                 goto fail_hash_support_get;
591
592         efx_rx_fini(sa->nic);
593         efx_ev_fini(sa->nic);
594         efx_intr_fini(sa->nic);
595
596         sa->rss_hash_types = sfc_rte_to_efx_hash_type(SFC_RSS_OFFLOADS);
597
598         rte_memcpy(sa->rss_key, default_rss_key, sizeof(sa->rss_key));
599
600         return 0;
601
602 fail_hash_support_get:
603 fail_scale_support_get:
604 fail_rx_init:
605         efx_ev_fini(sa->nic);
606
607 fail_ev_init:
608         efx_intr_fini(sa->nic);
609
610 fail_intr_init:
611         return rc;
612 }
613 #else
614 static int
615 sfc_set_rss_defaults(__rte_unused struct sfc_adapter *sa)
616 {
617         return 0;
618 }
619 #endif
620
621 int
622 sfc_attach(struct sfc_adapter *sa)
623 {
624         const efx_nic_cfg_t *encp;
625         efx_nic_t *enp = sa->nic;
626         int rc;
627
628         sfc_log_init(sa, "entry");
629
630         SFC_ASSERT(sfc_adapter_is_locked(sa));
631
632         efx_mcdi_new_epoch(enp);
633
634         sfc_log_init(sa, "reset nic");
635         rc = efx_nic_reset(enp);
636         if (rc != 0)
637                 goto fail_nic_reset;
638
639         encp = efx_nic_cfg_get(sa->nic);
640
641         if (sa->dp_tx->features & SFC_DP_TX_FEAT_TSO) {
642                 sa->tso = encp->enc_fw_assisted_tso_v2_enabled;
643                 if (!sa->tso)
644                         sfc_warn(sa,
645                                  "TSO support isn't available on this adapter");
646         }
647
648         sfc_log_init(sa, "estimate resource limits");
649         rc = sfc_estimate_resource_limits(sa);
650         if (rc != 0)
651                 goto fail_estimate_rsrc_limits;
652
653         sa->txq_max_entries = encp->enc_txq_max_ndescs;
654         SFC_ASSERT(rte_is_power_of_2(sa->txq_max_entries));
655
656         rc = sfc_intr_attach(sa);
657         if (rc != 0)
658                 goto fail_intr_attach;
659
660         rc = sfc_ev_attach(sa);
661         if (rc != 0)
662                 goto fail_ev_attach;
663
664         rc = sfc_port_attach(sa);
665         if (rc != 0)
666                 goto fail_port_attach;
667
668         rc = sfc_set_rss_defaults(sa);
669         if (rc != 0)
670                 goto fail_set_rss_defaults;
671
672         rc = sfc_filter_attach(sa);
673         if (rc != 0)
674                 goto fail_filter_attach;
675
676         sfc_log_init(sa, "fini nic");
677         efx_nic_fini(enp);
678
679         sfc_flow_init(sa);
680
681         sa->state = SFC_ADAPTER_INITIALIZED;
682
683         sfc_log_init(sa, "done");
684         return 0;
685
686 fail_filter_attach:
687 fail_set_rss_defaults:
688         sfc_port_detach(sa);
689
690 fail_port_attach:
691         sfc_ev_detach(sa);
692
693 fail_ev_attach:
694         sfc_intr_detach(sa);
695
696 fail_intr_attach:
697         efx_nic_fini(sa->nic);
698
699 fail_estimate_rsrc_limits:
700 fail_nic_reset:
701
702         sfc_log_init(sa, "failed %d", rc);
703         return rc;
704 }
705
706 void
707 sfc_detach(struct sfc_adapter *sa)
708 {
709         sfc_log_init(sa, "entry");
710
711         SFC_ASSERT(sfc_adapter_is_locked(sa));
712
713         sfc_flow_fini(sa);
714
715         sfc_filter_detach(sa);
716         sfc_port_detach(sa);
717         sfc_ev_detach(sa);
718         sfc_intr_detach(sa);
719
720         sa->state = SFC_ADAPTER_UNINITIALIZED;
721 }
722
723 int
724 sfc_probe(struct sfc_adapter *sa)
725 {
726         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(sa->eth_dev);
727         efx_nic_t *enp;
728         int rc;
729
730         sfc_log_init(sa, "entry");
731
732         SFC_ASSERT(sfc_adapter_is_locked(sa));
733
734         sa->socket_id = rte_socket_id();
735         rte_atomic32_init(&sa->restart_required);
736
737         sfc_log_init(sa, "init mem bar");
738         rc = sfc_mem_bar_init(sa);
739         if (rc != 0)
740                 goto fail_mem_bar_init;
741
742         sfc_log_init(sa, "get family");
743         rc = efx_family(pci_dev->id.vendor_id, pci_dev->id.device_id,
744                         &sa->family);
745         if (rc != 0)
746                 goto fail_family;
747         sfc_log_init(sa, "family is %u", sa->family);
748
749         sfc_log_init(sa, "create nic");
750         rte_spinlock_init(&sa->nic_lock);
751         rc = efx_nic_create(sa->family, (efsys_identifier_t *)sa,
752                             &sa->mem_bar, &sa->nic_lock, &enp);
753         if (rc != 0)
754                 goto fail_nic_create;
755         sa->nic = enp;
756
757         rc = sfc_mcdi_init(sa);
758         if (rc != 0)
759                 goto fail_mcdi_init;
760
761         sfc_log_init(sa, "probe nic");
762         rc = efx_nic_probe(enp);
763         if (rc != 0)
764                 goto fail_nic_probe;
765
766         sfc_log_init(sa, "done");
767         return 0;
768
769 fail_nic_probe:
770         sfc_mcdi_fini(sa);
771
772 fail_mcdi_init:
773         sfc_log_init(sa, "destroy nic");
774         sa->nic = NULL;
775         efx_nic_destroy(enp);
776
777 fail_nic_create:
778 fail_family:
779         sfc_mem_bar_fini(sa);
780
781 fail_mem_bar_init:
782         sfc_log_init(sa, "failed %d", rc);
783         return rc;
784 }
785
786 void
787 sfc_unprobe(struct sfc_adapter *sa)
788 {
789         efx_nic_t *enp = sa->nic;
790
791         sfc_log_init(sa, "entry");
792
793         SFC_ASSERT(sfc_adapter_is_locked(sa));
794
795         sfc_log_init(sa, "unprobe nic");
796         efx_nic_unprobe(enp);
797
798         sfc_mcdi_fini(sa);
799
800         /*
801          * Make sure there is no pending alarm to restart since we are
802          * going to free device private which is passed as the callback
803          * opaque data. A new alarm cannot be scheduled since MCDI is
804          * shut down.
805          */
806         rte_eal_alarm_cancel(sfc_restart_if_required, sa);
807
808         sfc_log_init(sa, "destroy nic");
809         sa->nic = NULL;
810         efx_nic_destroy(enp);
811
812         sfc_mem_bar_fini(sa);
813
814         sfc_flow_fini(sa);
815         sa->state = SFC_ADAPTER_UNINITIALIZED;
816 }