ethdev: change promiscuous callbacks to return status
[dpdk.git] / drivers / net / sfc / sfc_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright (c) 2016-2018 Solarflare Communications Inc.
4  * All rights reserved.
5  *
6  * This software was jointly developed between OKTET Labs (under contract
7  * for Solarflare) and Solarflare Communications, Inc.
8  */
9
10 #include <rte_dev.h>
11 #include <rte_ethdev_driver.h>
12 #include <rte_ethdev_pci.h>
13 #include <rte_pci.h>
14 #include <rte_bus_pci.h>
15 #include <rte_errno.h>
16 #include <rte_string_fns.h>
17 #include <rte_ether.h>
18
19 #include "efx.h"
20
21 #include "sfc.h"
22 #include "sfc_debug.h"
23 #include "sfc_log.h"
24 #include "sfc_kvargs.h"
25 #include "sfc_ev.h"
26 #include "sfc_rx.h"
27 #include "sfc_tx.h"
28 #include "sfc_flow.h"
29 #include "sfc_dp.h"
30 #include "sfc_dp_rx.h"
31
32 uint32_t sfc_logtype_driver;
33
34 static struct sfc_dp_list sfc_dp_head =
35         TAILQ_HEAD_INITIALIZER(sfc_dp_head);
36
37
38 static void sfc_eth_dev_clear_ops(struct rte_eth_dev *dev);
39
40
41 static int
42 sfc_fw_version_get(struct rte_eth_dev *dev, char *fw_version, size_t fw_size)
43 {
44         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
45         efx_nic_fw_info_t enfi;
46         int ret;
47         int rc;
48
49         /*
50          * Return value of the callback is likely supposed to be
51          * equal to or greater than 0, nevertheless, if an error
52          * occurs, it will be desirable to pass it to the caller
53          */
54         if ((fw_version == NULL) || (fw_size == 0))
55                 return -EINVAL;
56
57         rc = efx_nic_get_fw_version(sa->nic, &enfi);
58         if (rc != 0)
59                 return -rc;
60
61         ret = snprintf(fw_version, fw_size,
62                        "%" PRIu16 ".%" PRIu16 ".%" PRIu16 ".%" PRIu16,
63                        enfi.enfi_mc_fw_version[0], enfi.enfi_mc_fw_version[1],
64                        enfi.enfi_mc_fw_version[2], enfi.enfi_mc_fw_version[3]);
65         if (ret < 0)
66                 return ret;
67
68         if (enfi.enfi_dpcpu_fw_ids_valid) {
69                 size_t dpcpu_fw_ids_offset = MIN(fw_size - 1, (size_t)ret);
70                 int ret_extra;
71
72                 ret_extra = snprintf(fw_version + dpcpu_fw_ids_offset,
73                                      fw_size - dpcpu_fw_ids_offset,
74                                      " rx%" PRIx16 " tx%" PRIx16,
75                                      enfi.enfi_rx_dpcpu_fw_id,
76                                      enfi.enfi_tx_dpcpu_fw_id);
77                 if (ret_extra < 0)
78                         return ret_extra;
79
80                 ret += ret_extra;
81         }
82
83         if (fw_size < (size_t)(++ret))
84                 return ret;
85         else
86                 return 0;
87 }
88
89 static int
90 sfc_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
91 {
92         const struct sfc_adapter_priv *sap = sfc_adapter_priv_by_eth_dev(dev);
93         struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
94         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
95         struct sfc_rss *rss = &sas->rss;
96         uint64_t txq_offloads_def = 0;
97
98         sfc_log_init(sa, "entry");
99
100         dev_info->min_mtu = RTE_ETHER_MIN_MTU;
101         dev_info->max_mtu = EFX_MAC_SDU_MAX;
102
103         dev_info->max_rx_pktlen = EFX_MAC_PDU_MAX;
104
105         /* Autonegotiation may be disabled */
106         dev_info->speed_capa = ETH_LINK_SPEED_FIXED;
107         if (sa->port.phy_adv_cap_mask & (1u << EFX_PHY_CAP_1000FDX))
108                 dev_info->speed_capa |= ETH_LINK_SPEED_1G;
109         if (sa->port.phy_adv_cap_mask & (1u << EFX_PHY_CAP_10000FDX))
110                 dev_info->speed_capa |= ETH_LINK_SPEED_10G;
111         if (sa->port.phy_adv_cap_mask & (1u << EFX_PHY_CAP_25000FDX))
112                 dev_info->speed_capa |= ETH_LINK_SPEED_25G;
113         if (sa->port.phy_adv_cap_mask & (1u << EFX_PHY_CAP_40000FDX))
114                 dev_info->speed_capa |= ETH_LINK_SPEED_40G;
115         if (sa->port.phy_adv_cap_mask & (1u << EFX_PHY_CAP_50000FDX))
116                 dev_info->speed_capa |= ETH_LINK_SPEED_50G;
117         if (sa->port.phy_adv_cap_mask & (1u << EFX_PHY_CAP_100000FDX))
118                 dev_info->speed_capa |= ETH_LINK_SPEED_100G;
119
120         dev_info->max_rx_queues = sa->rxq_max;
121         dev_info->max_tx_queues = sa->txq_max;
122
123         /* By default packets are dropped if no descriptors are available */
124         dev_info->default_rxconf.rx_drop_en = 1;
125
126         dev_info->rx_queue_offload_capa = sfc_rx_get_queue_offload_caps(sa);
127
128         /*
129          * rx_offload_capa includes both device and queue offloads since
130          * the latter may be requested on a per device basis which makes
131          * sense when some offloads are needed to be set on all queues.
132          */
133         dev_info->rx_offload_capa = sfc_rx_get_dev_offload_caps(sa) |
134                                     dev_info->rx_queue_offload_capa;
135
136         dev_info->tx_queue_offload_capa = sfc_tx_get_queue_offload_caps(sa);
137
138         /*
139          * tx_offload_capa includes both device and queue offloads since
140          * the latter may be requested on a per device basis which makes
141          * sense when some offloads are needed to be set on all queues.
142          */
143         dev_info->tx_offload_capa = sfc_tx_get_dev_offload_caps(sa) |
144                                     dev_info->tx_queue_offload_capa;
145
146         if (dev_info->tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
147                 txq_offloads_def |= DEV_TX_OFFLOAD_MBUF_FAST_FREE;
148
149         dev_info->default_txconf.offloads |= txq_offloads_def;
150
151         if (rss->context_type != EFX_RX_SCALE_UNAVAILABLE) {
152                 uint64_t rte_hf = 0;
153                 unsigned int i;
154
155                 for (i = 0; i < rss->hf_map_nb_entries; ++i)
156                         rte_hf |= rss->hf_map[i].rte;
157
158                 dev_info->reta_size = EFX_RSS_TBL_SIZE;
159                 dev_info->hash_key_size = EFX_RSS_KEY_SIZE;
160                 dev_info->flow_type_rss_offloads = rte_hf;
161         }
162
163         /* Initialize to hardware limits */
164         dev_info->rx_desc_lim.nb_max = sa->rxq_max_entries;
165         dev_info->rx_desc_lim.nb_min = sa->rxq_min_entries;
166         /* The RXQ hardware requires that the descriptor count is a power
167          * of 2, but rx_desc_lim cannot properly describe that constraint.
168          */
169         dev_info->rx_desc_lim.nb_align = sa->rxq_min_entries;
170
171         /* Initialize to hardware limits */
172         dev_info->tx_desc_lim.nb_max = sa->txq_max_entries;
173         dev_info->tx_desc_lim.nb_min = sa->txq_min_entries;
174         /*
175          * The TXQ hardware requires that the descriptor count is a power
176          * of 2, but tx_desc_lim cannot properly describe that constraint
177          */
178         dev_info->tx_desc_lim.nb_align = sa->txq_min_entries;
179
180         if (sap->dp_rx->get_dev_info != NULL)
181                 sap->dp_rx->get_dev_info(dev_info);
182         if (sap->dp_tx->get_dev_info != NULL)
183                 sap->dp_tx->get_dev_info(dev_info);
184
185         dev_info->dev_capa = RTE_ETH_DEV_CAPA_RUNTIME_RX_QUEUE_SETUP |
186                              RTE_ETH_DEV_CAPA_RUNTIME_TX_QUEUE_SETUP;
187
188         return 0;
189 }
190
191 static const uint32_t *
192 sfc_dev_supported_ptypes_get(struct rte_eth_dev *dev)
193 {
194         const struct sfc_adapter_priv *sap = sfc_adapter_priv_by_eth_dev(dev);
195
196         return sap->dp_rx->supported_ptypes_get(sap->shared->tunnel_encaps);
197 }
198
199 static int
200 sfc_dev_configure(struct rte_eth_dev *dev)
201 {
202         struct rte_eth_dev_data *dev_data = dev->data;
203         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
204         int rc;
205
206         sfc_log_init(sa, "entry n_rxq=%u n_txq=%u",
207                      dev_data->nb_rx_queues, dev_data->nb_tx_queues);
208
209         sfc_adapter_lock(sa);
210         switch (sa->state) {
211         case SFC_ADAPTER_CONFIGURED:
212                 /* FALLTHROUGH */
213         case SFC_ADAPTER_INITIALIZED:
214                 rc = sfc_configure(sa);
215                 break;
216         default:
217                 sfc_err(sa, "unexpected adapter state %u to configure",
218                         sa->state);
219                 rc = EINVAL;
220                 break;
221         }
222         sfc_adapter_unlock(sa);
223
224         sfc_log_init(sa, "done %d", rc);
225         SFC_ASSERT(rc >= 0);
226         return -rc;
227 }
228
229 static int
230 sfc_dev_start(struct rte_eth_dev *dev)
231 {
232         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
233         int rc;
234
235         sfc_log_init(sa, "entry");
236
237         sfc_adapter_lock(sa);
238         rc = sfc_start(sa);
239         sfc_adapter_unlock(sa);
240
241         sfc_log_init(sa, "done %d", rc);
242         SFC_ASSERT(rc >= 0);
243         return -rc;
244 }
245
246 static int
247 sfc_dev_link_update(struct rte_eth_dev *dev, int wait_to_complete)
248 {
249         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
250         struct rte_eth_link current_link;
251         int ret;
252
253         sfc_log_init(sa, "entry");
254
255         if (sa->state != SFC_ADAPTER_STARTED) {
256                 sfc_port_link_mode_to_info(EFX_LINK_UNKNOWN, &current_link);
257         } else if (wait_to_complete) {
258                 efx_link_mode_t link_mode;
259
260                 if (efx_port_poll(sa->nic, &link_mode) != 0)
261                         link_mode = EFX_LINK_UNKNOWN;
262                 sfc_port_link_mode_to_info(link_mode, &current_link);
263
264         } else {
265                 sfc_ev_mgmt_qpoll(sa);
266                 rte_eth_linkstatus_get(dev, &current_link);
267         }
268
269         ret = rte_eth_linkstatus_set(dev, &current_link);
270         if (ret == 0)
271                 sfc_notice(sa, "Link status is %s",
272                            current_link.link_status ? "UP" : "DOWN");
273
274         return ret;
275 }
276
277 static void
278 sfc_dev_stop(struct rte_eth_dev *dev)
279 {
280         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
281
282         sfc_log_init(sa, "entry");
283
284         sfc_adapter_lock(sa);
285         sfc_stop(sa);
286         sfc_adapter_unlock(sa);
287
288         sfc_log_init(sa, "done");
289 }
290
291 static int
292 sfc_dev_set_link_up(struct rte_eth_dev *dev)
293 {
294         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
295         int rc;
296
297         sfc_log_init(sa, "entry");
298
299         sfc_adapter_lock(sa);
300         rc = sfc_start(sa);
301         sfc_adapter_unlock(sa);
302
303         SFC_ASSERT(rc >= 0);
304         return -rc;
305 }
306
307 static int
308 sfc_dev_set_link_down(struct rte_eth_dev *dev)
309 {
310         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
311
312         sfc_log_init(sa, "entry");
313
314         sfc_adapter_lock(sa);
315         sfc_stop(sa);
316         sfc_adapter_unlock(sa);
317
318         return 0;
319 }
320
321 static void
322 sfc_dev_close(struct rte_eth_dev *dev)
323 {
324         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
325
326         sfc_log_init(sa, "entry");
327
328         sfc_adapter_lock(sa);
329         switch (sa->state) {
330         case SFC_ADAPTER_STARTED:
331                 sfc_stop(sa);
332                 SFC_ASSERT(sa->state == SFC_ADAPTER_CONFIGURED);
333                 /* FALLTHROUGH */
334         case SFC_ADAPTER_CONFIGURED:
335                 sfc_close(sa);
336                 SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED);
337                 /* FALLTHROUGH */
338         case SFC_ADAPTER_INITIALIZED:
339                 break;
340         default:
341                 sfc_err(sa, "unexpected adapter state %u on close", sa->state);
342                 break;
343         }
344
345         /*
346          * Cleanup all resources in accordance with RTE_ETH_DEV_CLOSE_REMOVE.
347          * Rollback primary process sfc_eth_dev_init() below.
348          */
349
350         sfc_eth_dev_clear_ops(dev);
351
352         sfc_detach(sa);
353         sfc_unprobe(sa);
354
355         sfc_kvargs_cleanup(sa);
356
357         sfc_adapter_unlock(sa);
358         sfc_adapter_lock_fini(sa);
359
360         sfc_log_init(sa, "done");
361
362         /* Required for logging, so cleanup last */
363         sa->eth_dev = NULL;
364
365         dev->process_private = NULL;
366         free(sa);
367 }
368
369 static int
370 sfc_dev_filter_set(struct rte_eth_dev *dev, enum sfc_dev_filter_mode mode,
371                    boolean_t enabled)
372 {
373         struct sfc_port *port;
374         boolean_t *toggle;
375         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
376         boolean_t allmulti = (mode == SFC_DEV_FILTER_MODE_ALLMULTI);
377         const char *desc = (allmulti) ? "all-multi" : "promiscuous";
378         int rc = 0;
379
380         sfc_adapter_lock(sa);
381
382         port = &sa->port;
383         toggle = (allmulti) ? (&port->allmulti) : (&port->promisc);
384
385         if (*toggle != enabled) {
386                 *toggle = enabled;
387
388                 if (sfc_sa2shared(sa)->isolated) {
389                         sfc_warn(sa, "isolated mode is active on the port");
390                         sfc_warn(sa, "the change is to be applied on the next "
391                                      "start provided that isolated mode is "
392                                      "disabled prior the next start");
393                 } else if ((sa->state == SFC_ADAPTER_STARTED) &&
394                            ((rc = sfc_set_rx_mode(sa)) != 0)) {
395                         *toggle = !(enabled);
396                         sfc_warn(sa, "Failed to %s %s mode",
397                                  ((enabled) ? "enable" : "disable"), desc);
398                 }
399         }
400
401         sfc_adapter_unlock(sa);
402         return rc;
403 }
404
405 static int
406 sfc_dev_promisc_enable(struct rte_eth_dev *dev)
407 {
408         return sfc_dev_filter_set(dev, SFC_DEV_FILTER_MODE_PROMISC, B_TRUE);
409 }
410
411 static int
412 sfc_dev_promisc_disable(struct rte_eth_dev *dev)
413 {
414         return sfc_dev_filter_set(dev, SFC_DEV_FILTER_MODE_PROMISC, B_FALSE);
415 }
416
417 static void
418 sfc_dev_allmulti_enable(struct rte_eth_dev *dev)
419 {
420         sfc_dev_filter_set(dev, SFC_DEV_FILTER_MODE_ALLMULTI, B_TRUE);
421 }
422
423 static void
424 sfc_dev_allmulti_disable(struct rte_eth_dev *dev)
425 {
426         sfc_dev_filter_set(dev, SFC_DEV_FILTER_MODE_ALLMULTI, B_FALSE);
427 }
428
429 static int
430 sfc_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
431                    uint16_t nb_rx_desc, unsigned int socket_id,
432                    const struct rte_eth_rxconf *rx_conf,
433                    struct rte_mempool *mb_pool)
434 {
435         struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
436         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
437         int rc;
438
439         sfc_log_init(sa, "RxQ=%u nb_rx_desc=%u socket_id=%u",
440                      rx_queue_id, nb_rx_desc, socket_id);
441
442         sfc_adapter_lock(sa);
443
444         rc = sfc_rx_qinit(sa, rx_queue_id, nb_rx_desc, socket_id,
445                           rx_conf, mb_pool);
446         if (rc != 0)
447                 goto fail_rx_qinit;
448
449         dev->data->rx_queues[rx_queue_id] = sas->rxq_info[rx_queue_id].dp;
450
451         sfc_adapter_unlock(sa);
452
453         return 0;
454
455 fail_rx_qinit:
456         sfc_adapter_unlock(sa);
457         SFC_ASSERT(rc > 0);
458         return -rc;
459 }
460
461 static void
462 sfc_rx_queue_release(void *queue)
463 {
464         struct sfc_dp_rxq *dp_rxq = queue;
465         struct sfc_rxq *rxq;
466         struct sfc_adapter *sa;
467         unsigned int sw_index;
468
469         if (dp_rxq == NULL)
470                 return;
471
472         rxq = sfc_rxq_by_dp_rxq(dp_rxq);
473         sa = rxq->evq->sa;
474         sfc_adapter_lock(sa);
475
476         sw_index = dp_rxq->dpq.queue_id;
477
478         sfc_log_init(sa, "RxQ=%u", sw_index);
479
480         sfc_rx_qfini(sa, sw_index);
481
482         sfc_adapter_unlock(sa);
483 }
484
485 static int
486 sfc_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
487                    uint16_t nb_tx_desc, unsigned int socket_id,
488                    const struct rte_eth_txconf *tx_conf)
489 {
490         struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
491         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
492         int rc;
493
494         sfc_log_init(sa, "TxQ = %u, nb_tx_desc = %u, socket_id = %u",
495                      tx_queue_id, nb_tx_desc, socket_id);
496
497         sfc_adapter_lock(sa);
498
499         rc = sfc_tx_qinit(sa, tx_queue_id, nb_tx_desc, socket_id, tx_conf);
500         if (rc != 0)
501                 goto fail_tx_qinit;
502
503         dev->data->tx_queues[tx_queue_id] = sas->txq_info[tx_queue_id].dp;
504
505         sfc_adapter_unlock(sa);
506         return 0;
507
508 fail_tx_qinit:
509         sfc_adapter_unlock(sa);
510         SFC_ASSERT(rc > 0);
511         return -rc;
512 }
513
514 static void
515 sfc_tx_queue_release(void *queue)
516 {
517         struct sfc_dp_txq *dp_txq = queue;
518         struct sfc_txq *txq;
519         unsigned int sw_index;
520         struct sfc_adapter *sa;
521
522         if (dp_txq == NULL)
523                 return;
524
525         txq = sfc_txq_by_dp_txq(dp_txq);
526         sw_index = dp_txq->dpq.queue_id;
527
528         SFC_ASSERT(txq->evq != NULL);
529         sa = txq->evq->sa;
530
531         sfc_log_init(sa, "TxQ = %u", sw_index);
532
533         sfc_adapter_lock(sa);
534
535         sfc_tx_qfini(sa, sw_index);
536
537         sfc_adapter_unlock(sa);
538 }
539
540 /*
541  * Some statistics are computed as A - B where A and B each increase
542  * monotonically with some hardware counter(s) and the counters are read
543  * asynchronously.
544  *
545  * If packet X is counted in A, but not counted in B yet, computed value is
546  * greater than real.
547  *
548  * If packet X is not counted in A at the moment of reading the counter,
549  * but counted in B at the moment of reading the counter, computed value
550  * is less than real.
551  *
552  * However, counter which grows backward is worse evil than slightly wrong
553  * value. So, let's try to guarantee that it never happens except may be
554  * the case when the MAC stats are zeroed as a result of a NIC reset.
555  */
556 static void
557 sfc_update_diff_stat(uint64_t *stat, uint64_t newval)
558 {
559         if ((int64_t)(newval - *stat) > 0 || newval == 0)
560                 *stat = newval;
561 }
562
563 static int
564 sfc_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
565 {
566         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
567         struct sfc_port *port = &sa->port;
568         uint64_t *mac_stats;
569         int ret;
570
571         rte_spinlock_lock(&port->mac_stats_lock);
572
573         ret = sfc_port_update_mac_stats(sa);
574         if (ret != 0)
575                 goto unlock;
576
577         mac_stats = port->mac_stats_buf;
578
579         if (EFX_MAC_STAT_SUPPORTED(port->mac_stats_mask,
580                                    EFX_MAC_VADAPTER_RX_UNICAST_PACKETS)) {
581                 stats->ipackets =
582                         mac_stats[EFX_MAC_VADAPTER_RX_UNICAST_PACKETS] +
583                         mac_stats[EFX_MAC_VADAPTER_RX_MULTICAST_PACKETS] +
584                         mac_stats[EFX_MAC_VADAPTER_RX_BROADCAST_PACKETS];
585                 stats->opackets =
586                         mac_stats[EFX_MAC_VADAPTER_TX_UNICAST_PACKETS] +
587                         mac_stats[EFX_MAC_VADAPTER_TX_MULTICAST_PACKETS] +
588                         mac_stats[EFX_MAC_VADAPTER_TX_BROADCAST_PACKETS];
589                 stats->ibytes =
590                         mac_stats[EFX_MAC_VADAPTER_RX_UNICAST_BYTES] +
591                         mac_stats[EFX_MAC_VADAPTER_RX_MULTICAST_BYTES] +
592                         mac_stats[EFX_MAC_VADAPTER_RX_BROADCAST_BYTES];
593                 stats->obytes =
594                         mac_stats[EFX_MAC_VADAPTER_TX_UNICAST_BYTES] +
595                         mac_stats[EFX_MAC_VADAPTER_TX_MULTICAST_BYTES] +
596                         mac_stats[EFX_MAC_VADAPTER_TX_BROADCAST_BYTES];
597                 stats->imissed = mac_stats[EFX_MAC_VADAPTER_RX_BAD_PACKETS];
598                 stats->oerrors = mac_stats[EFX_MAC_VADAPTER_TX_BAD_PACKETS];
599         } else {
600                 stats->opackets = mac_stats[EFX_MAC_TX_PKTS];
601                 stats->ibytes = mac_stats[EFX_MAC_RX_OCTETS];
602                 stats->obytes = mac_stats[EFX_MAC_TX_OCTETS];
603                 /*
604                  * Take into account stats which are whenever supported
605                  * on EF10. If some stat is not supported by current
606                  * firmware variant or HW revision, it is guaranteed
607                  * to be zero in mac_stats.
608                  */
609                 stats->imissed =
610                         mac_stats[EFX_MAC_RX_NODESC_DROP_CNT] +
611                         mac_stats[EFX_MAC_PM_TRUNC_BB_OVERFLOW] +
612                         mac_stats[EFX_MAC_PM_DISCARD_BB_OVERFLOW] +
613                         mac_stats[EFX_MAC_PM_TRUNC_VFIFO_FULL] +
614                         mac_stats[EFX_MAC_PM_DISCARD_VFIFO_FULL] +
615                         mac_stats[EFX_MAC_PM_TRUNC_QBB] +
616                         mac_stats[EFX_MAC_PM_DISCARD_QBB] +
617                         mac_stats[EFX_MAC_PM_DISCARD_MAPPING] +
618                         mac_stats[EFX_MAC_RXDP_Q_DISABLED_PKTS] +
619                         mac_stats[EFX_MAC_RXDP_DI_DROPPED_PKTS];
620                 stats->ierrors =
621                         mac_stats[EFX_MAC_RX_FCS_ERRORS] +
622                         mac_stats[EFX_MAC_RX_ALIGN_ERRORS] +
623                         mac_stats[EFX_MAC_RX_JABBER_PKTS];
624                 /* no oerrors counters supported on EF10 */
625
626                 /* Exclude missed, errors and pauses from Rx packets */
627                 sfc_update_diff_stat(&port->ipackets,
628                         mac_stats[EFX_MAC_RX_PKTS] -
629                         mac_stats[EFX_MAC_RX_PAUSE_PKTS] -
630                         stats->imissed - stats->ierrors);
631                 stats->ipackets = port->ipackets;
632         }
633
634 unlock:
635         rte_spinlock_unlock(&port->mac_stats_lock);
636         SFC_ASSERT(ret >= 0);
637         return -ret;
638 }
639
640 static void
641 sfc_stats_reset(struct rte_eth_dev *dev)
642 {
643         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
644         struct sfc_port *port = &sa->port;
645         int rc;
646
647         if (sa->state != SFC_ADAPTER_STARTED) {
648                 /*
649                  * The operation cannot be done if port is not started; it
650                  * will be scheduled to be done during the next port start
651                  */
652                 port->mac_stats_reset_pending = B_TRUE;
653                 return;
654         }
655
656         rc = sfc_port_reset_mac_stats(sa);
657         if (rc != 0)
658                 sfc_err(sa, "failed to reset statistics (rc = %d)", rc);
659 }
660
661 static int
662 sfc_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
663                unsigned int xstats_count)
664 {
665         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
666         struct sfc_port *port = &sa->port;
667         uint64_t *mac_stats;
668         int rc;
669         unsigned int i;
670         int nstats = 0;
671
672         rte_spinlock_lock(&port->mac_stats_lock);
673
674         rc = sfc_port_update_mac_stats(sa);
675         if (rc != 0) {
676                 SFC_ASSERT(rc > 0);
677                 nstats = -rc;
678                 goto unlock;
679         }
680
681         mac_stats = port->mac_stats_buf;
682
683         for (i = 0; i < EFX_MAC_NSTATS; ++i) {
684                 if (EFX_MAC_STAT_SUPPORTED(port->mac_stats_mask, i)) {
685                         if (xstats != NULL && nstats < (int)xstats_count) {
686                                 xstats[nstats].id = nstats;
687                                 xstats[nstats].value = mac_stats[i];
688                         }
689                         nstats++;
690                 }
691         }
692
693 unlock:
694         rte_spinlock_unlock(&port->mac_stats_lock);
695
696         return nstats;
697 }
698
699 static int
700 sfc_xstats_get_names(struct rte_eth_dev *dev,
701                      struct rte_eth_xstat_name *xstats_names,
702                      unsigned int xstats_count)
703 {
704         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
705         struct sfc_port *port = &sa->port;
706         unsigned int i;
707         unsigned int nstats = 0;
708
709         for (i = 0; i < EFX_MAC_NSTATS; ++i) {
710                 if (EFX_MAC_STAT_SUPPORTED(port->mac_stats_mask, i)) {
711                         if (xstats_names != NULL && nstats < xstats_count)
712                                 strlcpy(xstats_names[nstats].name,
713                                         efx_mac_stat_name(sa->nic, i),
714                                         sizeof(xstats_names[0].name));
715                         nstats++;
716                 }
717         }
718
719         return nstats;
720 }
721
722 static int
723 sfc_xstats_get_by_id(struct rte_eth_dev *dev, const uint64_t *ids,
724                      uint64_t *values, unsigned int n)
725 {
726         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
727         struct sfc_port *port = &sa->port;
728         uint64_t *mac_stats;
729         unsigned int nb_supported = 0;
730         unsigned int nb_written = 0;
731         unsigned int i;
732         int ret;
733         int rc;
734
735         if (unlikely(values == NULL) ||
736             unlikely((ids == NULL) && (n < port->mac_stats_nb_supported)))
737                 return port->mac_stats_nb_supported;
738
739         rte_spinlock_lock(&port->mac_stats_lock);
740
741         rc = sfc_port_update_mac_stats(sa);
742         if (rc != 0) {
743                 SFC_ASSERT(rc > 0);
744                 ret = -rc;
745                 goto unlock;
746         }
747
748         mac_stats = port->mac_stats_buf;
749
750         for (i = 0; (i < EFX_MAC_NSTATS) && (nb_written < n); ++i) {
751                 if (!EFX_MAC_STAT_SUPPORTED(port->mac_stats_mask, i))
752                         continue;
753
754                 if ((ids == NULL) || (ids[nb_written] == nb_supported))
755                         values[nb_written++] = mac_stats[i];
756
757                 ++nb_supported;
758         }
759
760         ret = nb_written;
761
762 unlock:
763         rte_spinlock_unlock(&port->mac_stats_lock);
764
765         return ret;
766 }
767
768 static int
769 sfc_xstats_get_names_by_id(struct rte_eth_dev *dev,
770                            struct rte_eth_xstat_name *xstats_names,
771                            const uint64_t *ids, unsigned int size)
772 {
773         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
774         struct sfc_port *port = &sa->port;
775         unsigned int nb_supported = 0;
776         unsigned int nb_written = 0;
777         unsigned int i;
778
779         if (unlikely(xstats_names == NULL) ||
780             unlikely((ids == NULL) && (size < port->mac_stats_nb_supported)))
781                 return port->mac_stats_nb_supported;
782
783         for (i = 0; (i < EFX_MAC_NSTATS) && (nb_written < size); ++i) {
784                 if (!EFX_MAC_STAT_SUPPORTED(port->mac_stats_mask, i))
785                         continue;
786
787                 if ((ids == NULL) || (ids[nb_written] == nb_supported)) {
788                         char *name = xstats_names[nb_written++].name;
789
790                         strlcpy(name, efx_mac_stat_name(sa->nic, i),
791                                 sizeof(xstats_names[0].name));
792                 }
793
794                 ++nb_supported;
795         }
796
797         return nb_written;
798 }
799
800 static int
801 sfc_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
802 {
803         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
804         unsigned int wanted_fc, link_fc;
805
806         memset(fc_conf, 0, sizeof(*fc_conf));
807
808         sfc_adapter_lock(sa);
809
810         if (sa->state == SFC_ADAPTER_STARTED)
811                 efx_mac_fcntl_get(sa->nic, &wanted_fc, &link_fc);
812         else
813                 link_fc = sa->port.flow_ctrl;
814
815         switch (link_fc) {
816         case 0:
817                 fc_conf->mode = RTE_FC_NONE;
818                 break;
819         case EFX_FCNTL_RESPOND:
820                 fc_conf->mode = RTE_FC_RX_PAUSE;
821                 break;
822         case EFX_FCNTL_GENERATE:
823                 fc_conf->mode = RTE_FC_TX_PAUSE;
824                 break;
825         case (EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE):
826                 fc_conf->mode = RTE_FC_FULL;
827                 break;
828         default:
829                 sfc_err(sa, "%s: unexpected flow control value %#x",
830                         __func__, link_fc);
831         }
832
833         fc_conf->autoneg = sa->port.flow_ctrl_autoneg;
834
835         sfc_adapter_unlock(sa);
836
837         return 0;
838 }
839
840 static int
841 sfc_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
842 {
843         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
844         struct sfc_port *port = &sa->port;
845         unsigned int fcntl;
846         int rc;
847
848         if (fc_conf->high_water != 0 || fc_conf->low_water != 0 ||
849             fc_conf->pause_time != 0 || fc_conf->send_xon != 0 ||
850             fc_conf->mac_ctrl_frame_fwd != 0) {
851                 sfc_err(sa, "unsupported flow control settings specified");
852                 rc = EINVAL;
853                 goto fail_inval;
854         }
855
856         switch (fc_conf->mode) {
857         case RTE_FC_NONE:
858                 fcntl = 0;
859                 break;
860         case RTE_FC_RX_PAUSE:
861                 fcntl = EFX_FCNTL_RESPOND;
862                 break;
863         case RTE_FC_TX_PAUSE:
864                 fcntl = EFX_FCNTL_GENERATE;
865                 break;
866         case RTE_FC_FULL:
867                 fcntl = EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE;
868                 break;
869         default:
870                 rc = EINVAL;
871                 goto fail_inval;
872         }
873
874         sfc_adapter_lock(sa);
875
876         if (sa->state == SFC_ADAPTER_STARTED) {
877                 rc = efx_mac_fcntl_set(sa->nic, fcntl, fc_conf->autoneg);
878                 if (rc != 0)
879                         goto fail_mac_fcntl_set;
880         }
881
882         port->flow_ctrl = fcntl;
883         port->flow_ctrl_autoneg = fc_conf->autoneg;
884
885         sfc_adapter_unlock(sa);
886
887         return 0;
888
889 fail_mac_fcntl_set:
890         sfc_adapter_unlock(sa);
891 fail_inval:
892         SFC_ASSERT(rc > 0);
893         return -rc;
894 }
895
896 static int
897 sfc_check_scatter_on_all_rx_queues(struct sfc_adapter *sa, size_t pdu)
898 {
899         struct sfc_adapter_shared * const sas = sfc_sa2shared(sa);
900         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
901         boolean_t scatter_enabled;
902         const char *error;
903         unsigned int i;
904
905         for (i = 0; i < sas->rxq_count; i++) {
906                 if ((sas->rxq_info[i].state & SFC_RXQ_INITIALIZED) == 0)
907                         continue;
908
909                 scatter_enabled = (sas->rxq_info[i].type_flags &
910                                    EFX_RXQ_FLAG_SCATTER);
911
912                 if (!sfc_rx_check_scatter(pdu, sa->rxq_ctrl[i].buf_size,
913                                           encp->enc_rx_prefix_size,
914                                           scatter_enabled, &error)) {
915                         sfc_err(sa, "MTU check for RxQ %u failed: %s", i,
916                                 error);
917                         return EINVAL;
918                 }
919         }
920
921         return 0;
922 }
923
924 static int
925 sfc_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
926 {
927         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
928         size_t pdu = EFX_MAC_PDU(mtu);
929         size_t old_pdu;
930         int rc;
931
932         sfc_log_init(sa, "mtu=%u", mtu);
933
934         rc = EINVAL;
935         if (pdu < EFX_MAC_PDU_MIN) {
936                 sfc_err(sa, "too small MTU %u (PDU size %u less than min %u)",
937                         (unsigned int)mtu, (unsigned int)pdu,
938                         EFX_MAC_PDU_MIN);
939                 goto fail_inval;
940         }
941         if (pdu > EFX_MAC_PDU_MAX) {
942                 sfc_err(sa, "too big MTU %u (PDU size %u greater than max %u)",
943                         (unsigned int)mtu, (unsigned int)pdu,
944                         (unsigned int)EFX_MAC_PDU_MAX);
945                 goto fail_inval;
946         }
947
948         sfc_adapter_lock(sa);
949
950         rc = sfc_check_scatter_on_all_rx_queues(sa, pdu);
951         if (rc != 0)
952                 goto fail_check_scatter;
953
954         if (pdu != sa->port.pdu) {
955                 if (sa->state == SFC_ADAPTER_STARTED) {
956                         sfc_stop(sa);
957
958                         old_pdu = sa->port.pdu;
959                         sa->port.pdu = pdu;
960                         rc = sfc_start(sa);
961                         if (rc != 0)
962                                 goto fail_start;
963                 } else {
964                         sa->port.pdu = pdu;
965                 }
966         }
967
968         /*
969          * The driver does not use it, but other PMDs update jumbo frame
970          * flag and max_rx_pkt_len when MTU is set.
971          */
972         if (mtu > RTE_ETHER_MAX_LEN) {
973                 struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
974                 rxmode->offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
975         }
976
977         dev->data->dev_conf.rxmode.max_rx_pkt_len = sa->port.pdu;
978
979         sfc_adapter_unlock(sa);
980
981         sfc_log_init(sa, "done");
982         return 0;
983
984 fail_start:
985         sa->port.pdu = old_pdu;
986         if (sfc_start(sa) != 0)
987                 sfc_err(sa, "cannot start with neither new (%u) nor old (%u) "
988                         "PDU max size - port is stopped",
989                         (unsigned int)pdu, (unsigned int)old_pdu);
990
991 fail_check_scatter:
992         sfc_adapter_unlock(sa);
993
994 fail_inval:
995         sfc_log_init(sa, "failed %d", rc);
996         SFC_ASSERT(rc > 0);
997         return -rc;
998 }
999 static int
1000 sfc_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
1001 {
1002         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1003         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
1004         struct sfc_port *port = &sa->port;
1005         struct rte_ether_addr *old_addr = &dev->data->mac_addrs[0];
1006         int rc = 0;
1007
1008         sfc_adapter_lock(sa);
1009
1010         /*
1011          * Copy the address to the device private data so that
1012          * it could be recalled in the case of adapter restart.
1013          */
1014         rte_ether_addr_copy(mac_addr, &port->default_mac_addr);
1015
1016         /*
1017          * Neither of the two following checks can return
1018          * an error. The new MAC address is preserved in
1019          * the device private data and can be activated
1020          * on the next port start if the user prevents
1021          * isolated mode from being enabled.
1022          */
1023         if (sfc_sa2shared(sa)->isolated) {
1024                 sfc_warn(sa, "isolated mode is active on the port");
1025                 sfc_warn(sa, "will not set MAC address");
1026                 goto unlock;
1027         }
1028
1029         if (sa->state != SFC_ADAPTER_STARTED) {
1030                 sfc_notice(sa, "the port is not started");
1031                 sfc_notice(sa, "the new MAC address will be set on port start");
1032
1033                 goto unlock;
1034         }
1035
1036         if (encp->enc_allow_set_mac_with_installed_filters) {
1037                 rc = efx_mac_addr_set(sa->nic, mac_addr->addr_bytes);
1038                 if (rc != 0) {
1039                         sfc_err(sa, "cannot set MAC address (rc = %u)", rc);
1040                         goto unlock;
1041                 }
1042
1043                 /*
1044                  * Changing the MAC address by means of MCDI request
1045                  * has no effect on received traffic, therefore
1046                  * we also need to update unicast filters
1047                  */
1048                 rc = sfc_set_rx_mode(sa);
1049                 if (rc != 0) {
1050                         sfc_err(sa, "cannot set filter (rc = %u)", rc);
1051                         /* Rollback the old address */
1052                         (void)efx_mac_addr_set(sa->nic, old_addr->addr_bytes);
1053                         (void)sfc_set_rx_mode(sa);
1054                 }
1055         } else {
1056                 sfc_warn(sa, "cannot set MAC address with filters installed");
1057                 sfc_warn(sa, "adapter will be restarted to pick the new MAC");
1058                 sfc_warn(sa, "(some traffic may be dropped)");
1059
1060                 /*
1061                  * Since setting MAC address with filters installed is not
1062                  * allowed on the adapter, the new MAC address will be set
1063                  * by means of adapter restart. sfc_start() shall retrieve
1064                  * the new address from the device private data and set it.
1065                  */
1066                 sfc_stop(sa);
1067                 rc = sfc_start(sa);
1068                 if (rc != 0)
1069                         sfc_err(sa, "cannot restart adapter (rc = %u)", rc);
1070         }
1071
1072 unlock:
1073         if (rc != 0)
1074                 rte_ether_addr_copy(old_addr, &port->default_mac_addr);
1075
1076         sfc_adapter_unlock(sa);
1077
1078         SFC_ASSERT(rc >= 0);
1079         return -rc;
1080 }
1081
1082
1083 static int
1084 sfc_set_mc_addr_list(struct rte_eth_dev *dev,
1085                 struct rte_ether_addr *mc_addr_set, uint32_t nb_mc_addr)
1086 {
1087         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1088         struct sfc_port *port = &sa->port;
1089         uint8_t *mc_addrs = port->mcast_addrs;
1090         int rc;
1091         unsigned int i;
1092
1093         if (sfc_sa2shared(sa)->isolated) {
1094                 sfc_err(sa, "isolated mode is active on the port");
1095                 sfc_err(sa, "will not set multicast address list");
1096                 return -ENOTSUP;
1097         }
1098
1099         if (mc_addrs == NULL)
1100                 return -ENOBUFS;
1101
1102         if (nb_mc_addr > port->max_mcast_addrs) {
1103                 sfc_err(sa, "too many multicast addresses: %u > %u",
1104                          nb_mc_addr, port->max_mcast_addrs);
1105                 return -EINVAL;
1106         }
1107
1108         for (i = 0; i < nb_mc_addr; ++i) {
1109                 rte_memcpy(mc_addrs, mc_addr_set[i].addr_bytes,
1110                                  EFX_MAC_ADDR_LEN);
1111                 mc_addrs += EFX_MAC_ADDR_LEN;
1112         }
1113
1114         port->nb_mcast_addrs = nb_mc_addr;
1115
1116         if (sa->state != SFC_ADAPTER_STARTED)
1117                 return 0;
1118
1119         rc = efx_mac_multicast_list_set(sa->nic, port->mcast_addrs,
1120                                         port->nb_mcast_addrs);
1121         if (rc != 0)
1122                 sfc_err(sa, "cannot set multicast address list (rc = %u)", rc);
1123
1124         SFC_ASSERT(rc >= 0);
1125         return -rc;
1126 }
1127
1128 /*
1129  * The function is used by the secondary process as well. It must not
1130  * use any process-local pointers from the adapter data.
1131  */
1132 static void
1133 sfc_rx_queue_info_get(struct rte_eth_dev *dev, uint16_t rx_queue_id,
1134                       struct rte_eth_rxq_info *qinfo)
1135 {
1136         struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1137         struct sfc_rxq_info *rxq_info;
1138
1139         SFC_ASSERT(rx_queue_id < sas->rxq_count);
1140
1141         rxq_info = &sas->rxq_info[rx_queue_id];
1142
1143         qinfo->mp = rxq_info->refill_mb_pool;
1144         qinfo->conf.rx_free_thresh = rxq_info->refill_threshold;
1145         qinfo->conf.rx_drop_en = 1;
1146         qinfo->conf.rx_deferred_start = rxq_info->deferred_start;
1147         qinfo->conf.offloads = dev->data->dev_conf.rxmode.offloads;
1148         if (rxq_info->type_flags & EFX_RXQ_FLAG_SCATTER) {
1149                 qinfo->conf.offloads |= DEV_RX_OFFLOAD_SCATTER;
1150                 qinfo->scattered_rx = 1;
1151         }
1152         qinfo->nb_desc = rxq_info->entries;
1153 }
1154
1155 /*
1156  * The function is used by the secondary process as well. It must not
1157  * use any process-local pointers from the adapter data.
1158  */
1159 static void
1160 sfc_tx_queue_info_get(struct rte_eth_dev *dev, uint16_t tx_queue_id,
1161                       struct rte_eth_txq_info *qinfo)
1162 {
1163         struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1164         struct sfc_txq_info *txq_info;
1165
1166         SFC_ASSERT(tx_queue_id < sas->txq_count);
1167
1168         txq_info = &sas->txq_info[tx_queue_id];
1169
1170         memset(qinfo, 0, sizeof(*qinfo));
1171
1172         qinfo->conf.offloads = txq_info->offloads;
1173         qinfo->conf.tx_free_thresh = txq_info->free_thresh;
1174         qinfo->conf.tx_deferred_start = txq_info->deferred_start;
1175         qinfo->nb_desc = txq_info->entries;
1176 }
1177
1178 /*
1179  * The function is used by the secondary process as well. It must not
1180  * use any process-local pointers from the adapter data.
1181  */
1182 static uint32_t
1183 sfc_rx_queue_count(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1184 {
1185         const struct sfc_adapter_priv *sap = sfc_adapter_priv_by_eth_dev(dev);
1186         struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1187         struct sfc_rxq_info *rxq_info;
1188
1189         SFC_ASSERT(rx_queue_id < sas->rxq_count);
1190         rxq_info = &sas->rxq_info[rx_queue_id];
1191
1192         if ((rxq_info->state & SFC_RXQ_STARTED) == 0)
1193                 return 0;
1194
1195         return sap->dp_rx->qdesc_npending(rxq_info->dp);
1196 }
1197
1198 /*
1199  * The function is used by the secondary process as well. It must not
1200  * use any process-local pointers from the adapter data.
1201  */
1202 static int
1203 sfc_rx_descriptor_done(void *queue, uint16_t offset)
1204 {
1205         struct sfc_dp_rxq *dp_rxq = queue;
1206         const struct sfc_dp_rx *dp_rx;
1207
1208         dp_rx = sfc_dp_rx_by_dp_rxq(dp_rxq);
1209
1210         return offset < dp_rx->qdesc_npending(dp_rxq);
1211 }
1212
1213 /*
1214  * The function is used by the secondary process as well. It must not
1215  * use any process-local pointers from the adapter data.
1216  */
1217 static int
1218 sfc_rx_descriptor_status(void *queue, uint16_t offset)
1219 {
1220         struct sfc_dp_rxq *dp_rxq = queue;
1221         const struct sfc_dp_rx *dp_rx;
1222
1223         dp_rx = sfc_dp_rx_by_dp_rxq(dp_rxq);
1224
1225         return dp_rx->qdesc_status(dp_rxq, offset);
1226 }
1227
1228 /*
1229  * The function is used by the secondary process as well. It must not
1230  * use any process-local pointers from the adapter data.
1231  */
1232 static int
1233 sfc_tx_descriptor_status(void *queue, uint16_t offset)
1234 {
1235         struct sfc_dp_txq *dp_txq = queue;
1236         const struct sfc_dp_tx *dp_tx;
1237
1238         dp_tx = sfc_dp_tx_by_dp_txq(dp_txq);
1239
1240         return dp_tx->qdesc_status(dp_txq, offset);
1241 }
1242
1243 static int
1244 sfc_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1245 {
1246         struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1247         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1248         int rc;
1249
1250         sfc_log_init(sa, "RxQ=%u", rx_queue_id);
1251
1252         sfc_adapter_lock(sa);
1253
1254         rc = EINVAL;
1255         if (sa->state != SFC_ADAPTER_STARTED)
1256                 goto fail_not_started;
1257
1258         if (sas->rxq_info[rx_queue_id].state != SFC_RXQ_INITIALIZED)
1259                 goto fail_not_setup;
1260
1261         rc = sfc_rx_qstart(sa, rx_queue_id);
1262         if (rc != 0)
1263                 goto fail_rx_qstart;
1264
1265         sas->rxq_info[rx_queue_id].deferred_started = B_TRUE;
1266
1267         sfc_adapter_unlock(sa);
1268
1269         return 0;
1270
1271 fail_rx_qstart:
1272 fail_not_setup:
1273 fail_not_started:
1274         sfc_adapter_unlock(sa);
1275         SFC_ASSERT(rc > 0);
1276         return -rc;
1277 }
1278
1279 static int
1280 sfc_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1281 {
1282         struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1283         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1284
1285         sfc_log_init(sa, "RxQ=%u", rx_queue_id);
1286
1287         sfc_adapter_lock(sa);
1288         sfc_rx_qstop(sa, rx_queue_id);
1289
1290         sas->rxq_info[rx_queue_id].deferred_started = B_FALSE;
1291
1292         sfc_adapter_unlock(sa);
1293
1294         return 0;
1295 }
1296
1297 static int
1298 sfc_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
1299 {
1300         struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1301         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1302         int rc;
1303
1304         sfc_log_init(sa, "TxQ = %u", tx_queue_id);
1305
1306         sfc_adapter_lock(sa);
1307
1308         rc = EINVAL;
1309         if (sa->state != SFC_ADAPTER_STARTED)
1310                 goto fail_not_started;
1311
1312         if (sas->txq_info[tx_queue_id].state != SFC_TXQ_INITIALIZED)
1313                 goto fail_not_setup;
1314
1315         rc = sfc_tx_qstart(sa, tx_queue_id);
1316         if (rc != 0)
1317                 goto fail_tx_qstart;
1318
1319         sas->txq_info[tx_queue_id].deferred_started = B_TRUE;
1320
1321         sfc_adapter_unlock(sa);
1322         return 0;
1323
1324 fail_tx_qstart:
1325
1326 fail_not_setup:
1327 fail_not_started:
1328         sfc_adapter_unlock(sa);
1329         SFC_ASSERT(rc > 0);
1330         return -rc;
1331 }
1332
1333 static int
1334 sfc_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
1335 {
1336         struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1337         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1338
1339         sfc_log_init(sa, "TxQ = %u", tx_queue_id);
1340
1341         sfc_adapter_lock(sa);
1342
1343         sfc_tx_qstop(sa, tx_queue_id);
1344
1345         sas->txq_info[tx_queue_id].deferred_started = B_FALSE;
1346
1347         sfc_adapter_unlock(sa);
1348         return 0;
1349 }
1350
1351 static efx_tunnel_protocol_t
1352 sfc_tunnel_rte_type_to_efx_udp_proto(enum rte_eth_tunnel_type rte_type)
1353 {
1354         switch (rte_type) {
1355         case RTE_TUNNEL_TYPE_VXLAN:
1356                 return EFX_TUNNEL_PROTOCOL_VXLAN;
1357         case RTE_TUNNEL_TYPE_GENEVE:
1358                 return EFX_TUNNEL_PROTOCOL_GENEVE;
1359         default:
1360                 return EFX_TUNNEL_NPROTOS;
1361         }
1362 }
1363
1364 enum sfc_udp_tunnel_op_e {
1365         SFC_UDP_TUNNEL_ADD_PORT,
1366         SFC_UDP_TUNNEL_DEL_PORT,
1367 };
1368
1369 static int
1370 sfc_dev_udp_tunnel_op(struct rte_eth_dev *dev,
1371                       struct rte_eth_udp_tunnel *tunnel_udp,
1372                       enum sfc_udp_tunnel_op_e op)
1373 {
1374         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1375         efx_tunnel_protocol_t tunnel_proto;
1376         int rc;
1377
1378         sfc_log_init(sa, "%s udp_port=%u prot_type=%u",
1379                      (op == SFC_UDP_TUNNEL_ADD_PORT) ? "add" :
1380                      (op == SFC_UDP_TUNNEL_DEL_PORT) ? "delete" : "unknown",
1381                      tunnel_udp->udp_port, tunnel_udp->prot_type);
1382
1383         tunnel_proto =
1384                 sfc_tunnel_rte_type_to_efx_udp_proto(tunnel_udp->prot_type);
1385         if (tunnel_proto >= EFX_TUNNEL_NPROTOS) {
1386                 rc = ENOTSUP;
1387                 goto fail_bad_proto;
1388         }
1389
1390         sfc_adapter_lock(sa);
1391
1392         switch (op) {
1393         case SFC_UDP_TUNNEL_ADD_PORT:
1394                 rc = efx_tunnel_config_udp_add(sa->nic,
1395                                                tunnel_udp->udp_port,
1396                                                tunnel_proto);
1397                 break;
1398         case SFC_UDP_TUNNEL_DEL_PORT:
1399                 rc = efx_tunnel_config_udp_remove(sa->nic,
1400                                                   tunnel_udp->udp_port,
1401                                                   tunnel_proto);
1402                 break;
1403         default:
1404                 rc = EINVAL;
1405                 goto fail_bad_op;
1406         }
1407
1408         if (rc != 0)
1409                 goto fail_op;
1410
1411         if (sa->state == SFC_ADAPTER_STARTED) {
1412                 rc = efx_tunnel_reconfigure(sa->nic);
1413                 if (rc == EAGAIN) {
1414                         /*
1415                          * Configuration is accepted by FW and MC reboot
1416                          * is initiated to apply the changes. MC reboot
1417                          * will be handled in a usual way (MC reboot
1418                          * event on management event queue and adapter
1419                          * restart).
1420                          */
1421                         rc = 0;
1422                 } else if (rc != 0) {
1423                         goto fail_reconfigure;
1424                 }
1425         }
1426
1427         sfc_adapter_unlock(sa);
1428         return 0;
1429
1430 fail_reconfigure:
1431         /* Remove/restore entry since the change makes the trouble */
1432         switch (op) {
1433         case SFC_UDP_TUNNEL_ADD_PORT:
1434                 (void)efx_tunnel_config_udp_remove(sa->nic,
1435                                                    tunnel_udp->udp_port,
1436                                                    tunnel_proto);
1437                 break;
1438         case SFC_UDP_TUNNEL_DEL_PORT:
1439                 (void)efx_tunnel_config_udp_add(sa->nic,
1440                                                 tunnel_udp->udp_port,
1441                                                 tunnel_proto);
1442                 break;
1443         }
1444
1445 fail_op:
1446 fail_bad_op:
1447         sfc_adapter_unlock(sa);
1448
1449 fail_bad_proto:
1450         SFC_ASSERT(rc > 0);
1451         return -rc;
1452 }
1453
1454 static int
1455 sfc_dev_udp_tunnel_port_add(struct rte_eth_dev *dev,
1456                             struct rte_eth_udp_tunnel *tunnel_udp)
1457 {
1458         return sfc_dev_udp_tunnel_op(dev, tunnel_udp, SFC_UDP_TUNNEL_ADD_PORT);
1459 }
1460
1461 static int
1462 sfc_dev_udp_tunnel_port_del(struct rte_eth_dev *dev,
1463                             struct rte_eth_udp_tunnel *tunnel_udp)
1464 {
1465         return sfc_dev_udp_tunnel_op(dev, tunnel_udp, SFC_UDP_TUNNEL_DEL_PORT);
1466 }
1467
1468 /*
1469  * The function is used by the secondary process as well. It must not
1470  * use any process-local pointers from the adapter data.
1471  */
1472 static int
1473 sfc_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
1474                           struct rte_eth_rss_conf *rss_conf)
1475 {
1476         struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1477         struct sfc_rss *rss = &sas->rss;
1478
1479         if (rss->context_type != EFX_RX_SCALE_EXCLUSIVE)
1480                 return -ENOTSUP;
1481
1482         /*
1483          * Mapping of hash configuration between RTE and EFX is not one-to-one,
1484          * hence, conversion is done here to derive a correct set of ETH_RSS
1485          * flags which corresponds to the active EFX configuration stored
1486          * locally in 'sfc_adapter' and kept up-to-date
1487          */
1488         rss_conf->rss_hf = sfc_rx_hf_efx_to_rte(rss, rss->hash_types);
1489         rss_conf->rss_key_len = EFX_RSS_KEY_SIZE;
1490         if (rss_conf->rss_key != NULL)
1491                 rte_memcpy(rss_conf->rss_key, rss->key, EFX_RSS_KEY_SIZE);
1492
1493         return 0;
1494 }
1495
1496 static int
1497 sfc_dev_rss_hash_update(struct rte_eth_dev *dev,
1498                         struct rte_eth_rss_conf *rss_conf)
1499 {
1500         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1501         struct sfc_rss *rss = &sfc_sa2shared(sa)->rss;
1502         unsigned int efx_hash_types;
1503         int rc = 0;
1504
1505         if (sfc_sa2shared(sa)->isolated)
1506                 return -ENOTSUP;
1507
1508         if (rss->context_type != EFX_RX_SCALE_EXCLUSIVE) {
1509                 sfc_err(sa, "RSS is not available");
1510                 return -ENOTSUP;
1511         }
1512
1513         if (rss->channels == 0) {
1514                 sfc_err(sa, "RSS is not configured");
1515                 return -EINVAL;
1516         }
1517
1518         if ((rss_conf->rss_key != NULL) &&
1519             (rss_conf->rss_key_len != sizeof(rss->key))) {
1520                 sfc_err(sa, "RSS key size is wrong (should be %lu)",
1521                         sizeof(rss->key));
1522                 return -EINVAL;
1523         }
1524
1525         sfc_adapter_lock(sa);
1526
1527         rc = sfc_rx_hf_rte_to_efx(sa, rss_conf->rss_hf, &efx_hash_types);
1528         if (rc != 0)
1529                 goto fail_rx_hf_rte_to_efx;
1530
1531         rc = efx_rx_scale_mode_set(sa->nic, EFX_RSS_CONTEXT_DEFAULT,
1532                                    rss->hash_alg, efx_hash_types, B_TRUE);
1533         if (rc != 0)
1534                 goto fail_scale_mode_set;
1535
1536         if (rss_conf->rss_key != NULL) {
1537                 if (sa->state == SFC_ADAPTER_STARTED) {
1538                         rc = efx_rx_scale_key_set(sa->nic,
1539                                                   EFX_RSS_CONTEXT_DEFAULT,
1540                                                   rss_conf->rss_key,
1541                                                   sizeof(rss->key));
1542                         if (rc != 0)
1543                                 goto fail_scale_key_set;
1544                 }
1545
1546                 rte_memcpy(rss->key, rss_conf->rss_key, sizeof(rss->key));
1547         }
1548
1549         rss->hash_types = efx_hash_types;
1550
1551         sfc_adapter_unlock(sa);
1552
1553         return 0;
1554
1555 fail_scale_key_set:
1556         if (efx_rx_scale_mode_set(sa->nic, EFX_RSS_CONTEXT_DEFAULT,
1557                                   EFX_RX_HASHALG_TOEPLITZ,
1558                                   rss->hash_types, B_TRUE) != 0)
1559                 sfc_err(sa, "failed to restore RSS mode");
1560
1561 fail_scale_mode_set:
1562 fail_rx_hf_rte_to_efx:
1563         sfc_adapter_unlock(sa);
1564         return -rc;
1565 }
1566
1567 /*
1568  * The function is used by the secondary process as well. It must not
1569  * use any process-local pointers from the adapter data.
1570  */
1571 static int
1572 sfc_dev_rss_reta_query(struct rte_eth_dev *dev,
1573                        struct rte_eth_rss_reta_entry64 *reta_conf,
1574                        uint16_t reta_size)
1575 {
1576         struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1577         struct sfc_rss *rss = &sas->rss;
1578         int entry;
1579
1580         if (rss->context_type != EFX_RX_SCALE_EXCLUSIVE || sas->isolated)
1581                 return -ENOTSUP;
1582
1583         if (rss->channels == 0)
1584                 return -EINVAL;
1585
1586         if (reta_size != EFX_RSS_TBL_SIZE)
1587                 return -EINVAL;
1588
1589         for (entry = 0; entry < reta_size; entry++) {
1590                 int grp = entry / RTE_RETA_GROUP_SIZE;
1591                 int grp_idx = entry % RTE_RETA_GROUP_SIZE;
1592
1593                 if ((reta_conf[grp].mask >> grp_idx) & 1)
1594                         reta_conf[grp].reta[grp_idx] = rss->tbl[entry];
1595         }
1596
1597         return 0;
1598 }
1599
1600 static int
1601 sfc_dev_rss_reta_update(struct rte_eth_dev *dev,
1602                         struct rte_eth_rss_reta_entry64 *reta_conf,
1603                         uint16_t reta_size)
1604 {
1605         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1606         struct sfc_rss *rss = &sfc_sa2shared(sa)->rss;
1607         unsigned int *rss_tbl_new;
1608         uint16_t entry;
1609         int rc = 0;
1610
1611
1612         if (sfc_sa2shared(sa)->isolated)
1613                 return -ENOTSUP;
1614
1615         if (rss->context_type != EFX_RX_SCALE_EXCLUSIVE) {
1616                 sfc_err(sa, "RSS is not available");
1617                 return -ENOTSUP;
1618         }
1619
1620         if (rss->channels == 0) {
1621                 sfc_err(sa, "RSS is not configured");
1622                 return -EINVAL;
1623         }
1624
1625         if (reta_size != EFX_RSS_TBL_SIZE) {
1626                 sfc_err(sa, "RETA size is wrong (should be %u)",
1627                         EFX_RSS_TBL_SIZE);
1628                 return -EINVAL;
1629         }
1630
1631         rss_tbl_new = rte_zmalloc("rss_tbl_new", sizeof(rss->tbl), 0);
1632         if (rss_tbl_new == NULL)
1633                 return -ENOMEM;
1634
1635         sfc_adapter_lock(sa);
1636
1637         rte_memcpy(rss_tbl_new, rss->tbl, sizeof(rss->tbl));
1638
1639         for (entry = 0; entry < reta_size; entry++) {
1640                 int grp_idx = entry % RTE_RETA_GROUP_SIZE;
1641                 struct rte_eth_rss_reta_entry64 *grp;
1642
1643                 grp = &reta_conf[entry / RTE_RETA_GROUP_SIZE];
1644
1645                 if (grp->mask & (1ull << grp_idx)) {
1646                         if (grp->reta[grp_idx] >= rss->channels) {
1647                                 rc = EINVAL;
1648                                 goto bad_reta_entry;
1649                         }
1650                         rss_tbl_new[entry] = grp->reta[grp_idx];
1651                 }
1652         }
1653
1654         if (sa->state == SFC_ADAPTER_STARTED) {
1655                 rc = efx_rx_scale_tbl_set(sa->nic, EFX_RSS_CONTEXT_DEFAULT,
1656                                           rss_tbl_new, EFX_RSS_TBL_SIZE);
1657                 if (rc != 0)
1658                         goto fail_scale_tbl_set;
1659         }
1660
1661         rte_memcpy(rss->tbl, rss_tbl_new, sizeof(rss->tbl));
1662
1663 fail_scale_tbl_set:
1664 bad_reta_entry:
1665         sfc_adapter_unlock(sa);
1666
1667         rte_free(rss_tbl_new);
1668
1669         SFC_ASSERT(rc >= 0);
1670         return -rc;
1671 }
1672
1673 static int
1674 sfc_dev_filter_ctrl(struct rte_eth_dev *dev, enum rte_filter_type filter_type,
1675                     enum rte_filter_op filter_op,
1676                     void *arg)
1677 {
1678         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1679         int rc = ENOTSUP;
1680
1681         sfc_log_init(sa, "entry");
1682
1683         switch (filter_type) {
1684         case RTE_ETH_FILTER_NONE:
1685                 sfc_err(sa, "Global filters configuration not supported");
1686                 break;
1687         case RTE_ETH_FILTER_MACVLAN:
1688                 sfc_err(sa, "MACVLAN filters not supported");
1689                 break;
1690         case RTE_ETH_FILTER_ETHERTYPE:
1691                 sfc_err(sa, "EtherType filters not supported");
1692                 break;
1693         case RTE_ETH_FILTER_FLEXIBLE:
1694                 sfc_err(sa, "Flexible filters not supported");
1695                 break;
1696         case RTE_ETH_FILTER_SYN:
1697                 sfc_err(sa, "SYN filters not supported");
1698                 break;
1699         case RTE_ETH_FILTER_NTUPLE:
1700                 sfc_err(sa, "NTUPLE filters not supported");
1701                 break;
1702         case RTE_ETH_FILTER_TUNNEL:
1703                 sfc_err(sa, "Tunnel filters not supported");
1704                 break;
1705         case RTE_ETH_FILTER_FDIR:
1706                 sfc_err(sa, "Flow Director filters not supported");
1707                 break;
1708         case RTE_ETH_FILTER_HASH:
1709                 sfc_err(sa, "Hash filters not supported");
1710                 break;
1711         case RTE_ETH_FILTER_GENERIC:
1712                 if (filter_op != RTE_ETH_FILTER_GET) {
1713                         rc = EINVAL;
1714                 } else {
1715                         *(const void **)arg = &sfc_flow_ops;
1716                         rc = 0;
1717                 }
1718                 break;
1719         default:
1720                 sfc_err(sa, "Unknown filter type %u", filter_type);
1721                 break;
1722         }
1723
1724         sfc_log_init(sa, "exit: %d", -rc);
1725         SFC_ASSERT(rc >= 0);
1726         return -rc;
1727 }
1728
1729 static int
1730 sfc_pool_ops_supported(struct rte_eth_dev *dev, const char *pool)
1731 {
1732         const struct sfc_adapter_priv *sap = sfc_adapter_priv_by_eth_dev(dev);
1733
1734         /*
1735          * If Rx datapath does not provide callback to check mempool,
1736          * all pools are supported.
1737          */
1738         if (sap->dp_rx->pool_ops_supported == NULL)
1739                 return 1;
1740
1741         return sap->dp_rx->pool_ops_supported(pool);
1742 }
1743
1744 static int
1745 sfc_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
1746 {
1747         const struct sfc_adapter_priv *sap = sfc_adapter_priv_by_eth_dev(dev);
1748         struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1749         struct sfc_rxq_info *rxq_info;
1750
1751         SFC_ASSERT(queue_id < sas->rxq_count);
1752         rxq_info = &sas->rxq_info[queue_id];
1753
1754         return sap->dp_rx->intr_enable(rxq_info->dp);
1755 }
1756
1757 static int
1758 sfc_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
1759 {
1760         const struct sfc_adapter_priv *sap = sfc_adapter_priv_by_eth_dev(dev);
1761         struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1762         struct sfc_rxq_info *rxq_info;
1763
1764         SFC_ASSERT(queue_id < sas->rxq_count);
1765         rxq_info = &sas->rxq_info[queue_id];
1766
1767         return sap->dp_rx->intr_disable(rxq_info->dp);
1768 }
1769
1770 static const struct eth_dev_ops sfc_eth_dev_ops = {
1771         .dev_configure                  = sfc_dev_configure,
1772         .dev_start                      = sfc_dev_start,
1773         .dev_stop                       = sfc_dev_stop,
1774         .dev_set_link_up                = sfc_dev_set_link_up,
1775         .dev_set_link_down              = sfc_dev_set_link_down,
1776         .dev_close                      = sfc_dev_close,
1777         .promiscuous_enable             = sfc_dev_promisc_enable,
1778         .promiscuous_disable            = sfc_dev_promisc_disable,
1779         .allmulticast_enable            = sfc_dev_allmulti_enable,
1780         .allmulticast_disable           = sfc_dev_allmulti_disable,
1781         .link_update                    = sfc_dev_link_update,
1782         .stats_get                      = sfc_stats_get,
1783         .stats_reset                    = sfc_stats_reset,
1784         .xstats_get                     = sfc_xstats_get,
1785         .xstats_reset                   = sfc_stats_reset,
1786         .xstats_get_names               = sfc_xstats_get_names,
1787         .dev_infos_get                  = sfc_dev_infos_get,
1788         .dev_supported_ptypes_get       = sfc_dev_supported_ptypes_get,
1789         .mtu_set                        = sfc_dev_set_mtu,
1790         .rx_queue_start                 = sfc_rx_queue_start,
1791         .rx_queue_stop                  = sfc_rx_queue_stop,
1792         .tx_queue_start                 = sfc_tx_queue_start,
1793         .tx_queue_stop                  = sfc_tx_queue_stop,
1794         .rx_queue_setup                 = sfc_rx_queue_setup,
1795         .rx_queue_release               = sfc_rx_queue_release,
1796         .rx_queue_count                 = sfc_rx_queue_count,
1797         .rx_descriptor_done             = sfc_rx_descriptor_done,
1798         .rx_descriptor_status           = sfc_rx_descriptor_status,
1799         .tx_descriptor_status           = sfc_tx_descriptor_status,
1800         .rx_queue_intr_enable           = sfc_rx_queue_intr_enable,
1801         .rx_queue_intr_disable          = sfc_rx_queue_intr_disable,
1802         .tx_queue_setup                 = sfc_tx_queue_setup,
1803         .tx_queue_release               = sfc_tx_queue_release,
1804         .flow_ctrl_get                  = sfc_flow_ctrl_get,
1805         .flow_ctrl_set                  = sfc_flow_ctrl_set,
1806         .mac_addr_set                   = sfc_mac_addr_set,
1807         .udp_tunnel_port_add            = sfc_dev_udp_tunnel_port_add,
1808         .udp_tunnel_port_del            = sfc_dev_udp_tunnel_port_del,
1809         .reta_update                    = sfc_dev_rss_reta_update,
1810         .reta_query                     = sfc_dev_rss_reta_query,
1811         .rss_hash_update                = sfc_dev_rss_hash_update,
1812         .rss_hash_conf_get              = sfc_dev_rss_hash_conf_get,
1813         .filter_ctrl                    = sfc_dev_filter_ctrl,
1814         .set_mc_addr_list               = sfc_set_mc_addr_list,
1815         .rxq_info_get                   = sfc_rx_queue_info_get,
1816         .txq_info_get                   = sfc_tx_queue_info_get,
1817         .fw_version_get                 = sfc_fw_version_get,
1818         .xstats_get_by_id               = sfc_xstats_get_by_id,
1819         .xstats_get_names_by_id         = sfc_xstats_get_names_by_id,
1820         .pool_ops_supported             = sfc_pool_ops_supported,
1821 };
1822
1823 /**
1824  * Duplicate a string in potentially shared memory required for
1825  * multi-process support.
1826  *
1827  * strdup() allocates from process-local heap/memory.
1828  */
1829 static char *
1830 sfc_strdup(const char *str)
1831 {
1832         size_t size;
1833         char *copy;
1834
1835         if (str == NULL)
1836                 return NULL;
1837
1838         size = strlen(str) + 1;
1839         copy = rte_malloc(__func__, size, 0);
1840         if (copy != NULL)
1841                 rte_memcpy(copy, str, size);
1842
1843         return copy;
1844 }
1845
1846 static int
1847 sfc_eth_dev_set_ops(struct rte_eth_dev *dev)
1848 {
1849         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1850         struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1851         const struct sfc_dp_rx *dp_rx;
1852         const struct sfc_dp_tx *dp_tx;
1853         const efx_nic_cfg_t *encp;
1854         unsigned int avail_caps = 0;
1855         const char *rx_name = NULL;
1856         const char *tx_name = NULL;
1857         int rc;
1858
1859         switch (sa->family) {
1860         case EFX_FAMILY_HUNTINGTON:
1861         case EFX_FAMILY_MEDFORD:
1862         case EFX_FAMILY_MEDFORD2:
1863                 avail_caps |= SFC_DP_HW_FW_CAP_EF10;
1864                 break;
1865         default:
1866                 break;
1867         }
1868
1869         encp = efx_nic_cfg_get(sa->nic);
1870         if (encp->enc_rx_es_super_buffer_supported)
1871                 avail_caps |= SFC_DP_HW_FW_CAP_RX_ES_SUPER_BUFFER;
1872
1873         rc = sfc_kvargs_process(sa, SFC_KVARG_RX_DATAPATH,
1874                                 sfc_kvarg_string_handler, &rx_name);
1875         if (rc != 0)
1876                 goto fail_kvarg_rx_datapath;
1877
1878         if (rx_name != NULL) {
1879                 dp_rx = sfc_dp_find_rx_by_name(&sfc_dp_head, rx_name);
1880                 if (dp_rx == NULL) {
1881                         sfc_err(sa, "Rx datapath %s not found", rx_name);
1882                         rc = ENOENT;
1883                         goto fail_dp_rx;
1884                 }
1885                 if (!sfc_dp_match_hw_fw_caps(&dp_rx->dp, avail_caps)) {
1886                         sfc_err(sa,
1887                                 "Insufficient Hw/FW capabilities to use Rx datapath %s",
1888                                 rx_name);
1889                         rc = EINVAL;
1890                         goto fail_dp_rx_caps;
1891                 }
1892         } else {
1893                 dp_rx = sfc_dp_find_rx_by_caps(&sfc_dp_head, avail_caps);
1894                 if (dp_rx == NULL) {
1895                         sfc_err(sa, "Rx datapath by caps %#x not found",
1896                                 avail_caps);
1897                         rc = ENOENT;
1898                         goto fail_dp_rx;
1899                 }
1900         }
1901
1902         sas->dp_rx_name = sfc_strdup(dp_rx->dp.name);
1903         if (sas->dp_rx_name == NULL) {
1904                 rc = ENOMEM;
1905                 goto fail_dp_rx_name;
1906         }
1907
1908         sfc_notice(sa, "use %s Rx datapath", sas->dp_rx_name);
1909
1910         rc = sfc_kvargs_process(sa, SFC_KVARG_TX_DATAPATH,
1911                                 sfc_kvarg_string_handler, &tx_name);
1912         if (rc != 0)
1913                 goto fail_kvarg_tx_datapath;
1914
1915         if (tx_name != NULL) {
1916                 dp_tx = sfc_dp_find_tx_by_name(&sfc_dp_head, tx_name);
1917                 if (dp_tx == NULL) {
1918                         sfc_err(sa, "Tx datapath %s not found", tx_name);
1919                         rc = ENOENT;
1920                         goto fail_dp_tx;
1921                 }
1922                 if (!sfc_dp_match_hw_fw_caps(&dp_tx->dp, avail_caps)) {
1923                         sfc_err(sa,
1924                                 "Insufficient Hw/FW capabilities to use Tx datapath %s",
1925                                 tx_name);
1926                         rc = EINVAL;
1927                         goto fail_dp_tx_caps;
1928                 }
1929         } else {
1930                 dp_tx = sfc_dp_find_tx_by_caps(&sfc_dp_head, avail_caps);
1931                 if (dp_tx == NULL) {
1932                         sfc_err(sa, "Tx datapath by caps %#x not found",
1933                                 avail_caps);
1934                         rc = ENOENT;
1935                         goto fail_dp_tx;
1936                 }
1937         }
1938
1939         sas->dp_tx_name = sfc_strdup(dp_tx->dp.name);
1940         if (sas->dp_tx_name == NULL) {
1941                 rc = ENOMEM;
1942                 goto fail_dp_tx_name;
1943         }
1944
1945         sfc_notice(sa, "use %s Tx datapath", sas->dp_tx_name);
1946
1947         sa->priv.dp_rx = dp_rx;
1948         sa->priv.dp_tx = dp_tx;
1949
1950         dev->rx_pkt_burst = dp_rx->pkt_burst;
1951         dev->tx_pkt_prepare = dp_tx->pkt_prepare;
1952         dev->tx_pkt_burst = dp_tx->pkt_burst;
1953
1954         dev->dev_ops = &sfc_eth_dev_ops;
1955
1956         return 0;
1957
1958 fail_dp_tx_name:
1959 fail_dp_tx_caps:
1960 fail_dp_tx:
1961 fail_kvarg_tx_datapath:
1962         rte_free(sas->dp_rx_name);
1963         sas->dp_rx_name = NULL;
1964
1965 fail_dp_rx_name:
1966 fail_dp_rx_caps:
1967 fail_dp_rx:
1968 fail_kvarg_rx_datapath:
1969         return rc;
1970 }
1971
1972 static void
1973 sfc_eth_dev_clear_ops(struct rte_eth_dev *dev)
1974 {
1975         struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1976         struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1977
1978         dev->dev_ops = NULL;
1979         dev->tx_pkt_prepare = NULL;
1980         dev->rx_pkt_burst = NULL;
1981         dev->tx_pkt_burst = NULL;
1982
1983         rte_free(sas->dp_tx_name);
1984         sas->dp_tx_name = NULL;
1985         sa->priv.dp_tx = NULL;
1986
1987         rte_free(sas->dp_rx_name);
1988         sas->dp_rx_name = NULL;
1989         sa->priv.dp_rx = NULL;
1990 }
1991
1992 static const struct eth_dev_ops sfc_eth_dev_secondary_ops = {
1993         .dev_supported_ptypes_get       = sfc_dev_supported_ptypes_get,
1994         .rx_queue_count                 = sfc_rx_queue_count,
1995         .rx_descriptor_done             = sfc_rx_descriptor_done,
1996         .rx_descriptor_status           = sfc_rx_descriptor_status,
1997         .tx_descriptor_status           = sfc_tx_descriptor_status,
1998         .reta_query                     = sfc_dev_rss_reta_query,
1999         .rss_hash_conf_get              = sfc_dev_rss_hash_conf_get,
2000         .rxq_info_get                   = sfc_rx_queue_info_get,
2001         .txq_info_get                   = sfc_tx_queue_info_get,
2002 };
2003
2004 static int
2005 sfc_eth_dev_secondary_init(struct rte_eth_dev *dev, uint32_t logtype_main)
2006 {
2007         struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
2008         struct sfc_adapter_priv *sap;
2009         const struct sfc_dp_rx *dp_rx;
2010         const struct sfc_dp_tx *dp_tx;
2011         int rc;
2012
2013         /*
2014          * Allocate process private data from heap, since it should not
2015          * be located in shared memory allocated using rte_malloc() API.
2016          */
2017         sap = calloc(1, sizeof(*sap));
2018         if (sap == NULL) {
2019                 rc = ENOMEM;
2020                 goto fail_alloc_priv;
2021         }
2022
2023         sap->logtype_main = logtype_main;
2024
2025         dp_rx = sfc_dp_find_rx_by_name(&sfc_dp_head, sas->dp_rx_name);
2026         if (dp_rx == NULL) {
2027                 SFC_LOG(sas, RTE_LOG_ERR, logtype_main,
2028                         "cannot find %s Rx datapath", sas->dp_rx_name);
2029                 rc = ENOENT;
2030                 goto fail_dp_rx;
2031         }
2032         if (~dp_rx->features & SFC_DP_RX_FEAT_MULTI_PROCESS) {
2033                 SFC_LOG(sas, RTE_LOG_ERR, logtype_main,
2034                         "%s Rx datapath does not support multi-process",
2035                         sas->dp_rx_name);
2036                 rc = EINVAL;
2037                 goto fail_dp_rx_multi_process;
2038         }
2039
2040         dp_tx = sfc_dp_find_tx_by_name(&sfc_dp_head, sas->dp_tx_name);
2041         if (dp_tx == NULL) {
2042                 SFC_LOG(sas, RTE_LOG_ERR, logtype_main,
2043                         "cannot find %s Tx datapath", sas->dp_tx_name);
2044                 rc = ENOENT;
2045                 goto fail_dp_tx;
2046         }
2047         if (~dp_tx->features & SFC_DP_TX_FEAT_MULTI_PROCESS) {
2048                 SFC_LOG(sas, RTE_LOG_ERR, logtype_main,
2049                         "%s Tx datapath does not support multi-process",
2050                         sas->dp_tx_name);
2051                 rc = EINVAL;
2052                 goto fail_dp_tx_multi_process;
2053         }
2054
2055         sap->dp_rx = dp_rx;
2056         sap->dp_tx = dp_tx;
2057
2058         dev->process_private = sap;
2059         dev->rx_pkt_burst = dp_rx->pkt_burst;
2060         dev->tx_pkt_prepare = dp_tx->pkt_prepare;
2061         dev->tx_pkt_burst = dp_tx->pkt_burst;
2062         dev->dev_ops = &sfc_eth_dev_secondary_ops;
2063
2064         return 0;
2065
2066 fail_dp_tx_multi_process:
2067 fail_dp_tx:
2068 fail_dp_rx_multi_process:
2069 fail_dp_rx:
2070         free(sap);
2071
2072 fail_alloc_priv:
2073         return rc;
2074 }
2075
2076 static void
2077 sfc_eth_dev_secondary_clear_ops(struct rte_eth_dev *dev)
2078 {
2079         free(dev->process_private);
2080         dev->process_private = NULL;
2081         dev->dev_ops = NULL;
2082         dev->tx_pkt_prepare = NULL;
2083         dev->tx_pkt_burst = NULL;
2084         dev->rx_pkt_burst = NULL;
2085 }
2086
2087 static void
2088 sfc_register_dp(void)
2089 {
2090         /* Register once */
2091         if (TAILQ_EMPTY(&sfc_dp_head)) {
2092                 /* Prefer EF10 datapath */
2093                 sfc_dp_register(&sfc_dp_head, &sfc_ef10_essb_rx.dp);
2094                 sfc_dp_register(&sfc_dp_head, &sfc_ef10_rx.dp);
2095                 sfc_dp_register(&sfc_dp_head, &sfc_efx_rx.dp);
2096
2097                 sfc_dp_register(&sfc_dp_head, &sfc_ef10_tx.dp);
2098                 sfc_dp_register(&sfc_dp_head, &sfc_efx_tx.dp);
2099                 sfc_dp_register(&sfc_dp_head, &sfc_ef10_simple_tx.dp);
2100         }
2101 }
2102
2103 static int
2104 sfc_eth_dev_init(struct rte_eth_dev *dev)
2105 {
2106         struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
2107         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2108         uint32_t logtype_main;
2109         struct sfc_adapter *sa;
2110         int rc;
2111         const efx_nic_cfg_t *encp;
2112         const struct rte_ether_addr *from;
2113
2114         sfc_register_dp();
2115
2116         logtype_main = sfc_register_logtype(&pci_dev->addr,
2117                                             SFC_LOGTYPE_MAIN_STR,
2118                                             RTE_LOG_NOTICE);
2119
2120         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2121                 return -sfc_eth_dev_secondary_init(dev, logtype_main);
2122
2123         /* Required for logging */
2124         sas->pci_addr = pci_dev->addr;
2125         sas->port_id = dev->data->port_id;
2126
2127         /*
2128          * Allocate process private data from heap, since it should not
2129          * be located in shared memory allocated using rte_malloc() API.
2130          */
2131         sa = calloc(1, sizeof(*sa));
2132         if (sa == NULL) {
2133                 rc = ENOMEM;
2134                 goto fail_alloc_sa;
2135         }
2136
2137         dev->process_private = sa;
2138
2139         /* Required for logging */
2140         sa->priv.shared = sas;
2141         sa->priv.logtype_main = logtype_main;
2142
2143         sa->eth_dev = dev;
2144
2145         /* Copy PCI device info to the dev->data */
2146         rte_eth_copy_pci_info(dev, pci_dev);
2147
2148         rc = sfc_kvargs_parse(sa);
2149         if (rc != 0)
2150                 goto fail_kvargs_parse;
2151
2152         sfc_log_init(sa, "entry");
2153
2154         dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
2155
2156         dev->data->mac_addrs = rte_zmalloc("sfc", RTE_ETHER_ADDR_LEN, 0);
2157         if (dev->data->mac_addrs == NULL) {
2158                 rc = ENOMEM;
2159                 goto fail_mac_addrs;
2160         }
2161
2162         sfc_adapter_lock_init(sa);
2163         sfc_adapter_lock(sa);
2164
2165         sfc_log_init(sa, "probing");
2166         rc = sfc_probe(sa);
2167         if (rc != 0)
2168                 goto fail_probe;
2169
2170         sfc_log_init(sa, "set device ops");
2171         rc = sfc_eth_dev_set_ops(dev);
2172         if (rc != 0)
2173                 goto fail_set_ops;
2174
2175         sfc_log_init(sa, "attaching");
2176         rc = sfc_attach(sa);
2177         if (rc != 0)
2178                 goto fail_attach;
2179
2180         encp = efx_nic_cfg_get(sa->nic);
2181
2182         /*
2183          * The arguments are really reverse order in comparison to
2184          * Linux kernel. Copy from NIC config to Ethernet device data.
2185          */
2186         from = (const struct rte_ether_addr *)(encp->enc_mac_addr);
2187         rte_ether_addr_copy(from, &dev->data->mac_addrs[0]);
2188
2189         sfc_adapter_unlock(sa);
2190
2191         sfc_log_init(sa, "done");
2192         return 0;
2193
2194 fail_attach:
2195         sfc_eth_dev_clear_ops(dev);
2196
2197 fail_set_ops:
2198         sfc_unprobe(sa);
2199
2200 fail_probe:
2201         sfc_adapter_unlock(sa);
2202         sfc_adapter_lock_fini(sa);
2203         rte_free(dev->data->mac_addrs);
2204         dev->data->mac_addrs = NULL;
2205
2206 fail_mac_addrs:
2207         sfc_kvargs_cleanup(sa);
2208
2209 fail_kvargs_parse:
2210         sfc_log_init(sa, "failed %d", rc);
2211         dev->process_private = NULL;
2212         free(sa);
2213
2214 fail_alloc_sa:
2215         SFC_ASSERT(rc > 0);
2216         return -rc;
2217 }
2218
2219 static int
2220 sfc_eth_dev_uninit(struct rte_eth_dev *dev)
2221 {
2222         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
2223                 sfc_eth_dev_secondary_clear_ops(dev);
2224                 return 0;
2225         }
2226
2227         sfc_dev_close(dev);
2228
2229         return 0;
2230 }
2231
2232 static const struct rte_pci_id pci_id_sfc_efx_map[] = {
2233         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_FARMINGDALE) },
2234         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_FARMINGDALE_VF) },
2235         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_GREENPORT) },
2236         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_GREENPORT_VF) },
2237         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_MEDFORD) },
2238         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_MEDFORD_VF) },
2239         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_MEDFORD2) },
2240         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_MEDFORD2_VF) },
2241         { .vendor_id = 0 /* sentinel */ }
2242 };
2243
2244 static int sfc_eth_dev_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
2245         struct rte_pci_device *pci_dev)
2246 {
2247         return rte_eth_dev_pci_generic_probe(pci_dev,
2248                 sizeof(struct sfc_adapter_shared), sfc_eth_dev_init);
2249 }
2250
2251 static int sfc_eth_dev_pci_remove(struct rte_pci_device *pci_dev)
2252 {
2253         return rte_eth_dev_pci_generic_remove(pci_dev, sfc_eth_dev_uninit);
2254 }
2255
2256 static struct rte_pci_driver sfc_efx_pmd = {
2257         .id_table = pci_id_sfc_efx_map,
2258         .drv_flags =
2259                 RTE_PCI_DRV_INTR_LSC |
2260                 RTE_PCI_DRV_NEED_MAPPING,
2261         .probe = sfc_eth_dev_pci_probe,
2262         .remove = sfc_eth_dev_pci_remove,
2263 };
2264
2265 RTE_PMD_REGISTER_PCI(net_sfc_efx, sfc_efx_pmd);
2266 RTE_PMD_REGISTER_PCI_TABLE(net_sfc_efx, pci_id_sfc_efx_map);
2267 RTE_PMD_REGISTER_KMOD_DEP(net_sfc_efx, "* igb_uio | uio_pci_generic | vfio-pci");
2268 RTE_PMD_REGISTER_PARAM_STRING(net_sfc_efx,
2269         SFC_KVARG_RX_DATAPATH "=" SFC_KVARG_VALUES_RX_DATAPATH " "
2270         SFC_KVARG_TX_DATAPATH "=" SFC_KVARG_VALUES_TX_DATAPATH " "
2271         SFC_KVARG_PERF_PROFILE "=" SFC_KVARG_VALUES_PERF_PROFILE " "
2272         SFC_KVARG_FW_VARIANT "=" SFC_KVARG_VALUES_FW_VARIANT " "
2273         SFC_KVARG_RXD_WAIT_TIMEOUT_NS "=<long> "
2274         SFC_KVARG_STATS_UPDATE_PERIOD_MS "=<long>");
2275
2276 RTE_INIT(sfc_driver_register_logtype)
2277 {
2278         int ret;
2279
2280         ret = rte_log_register_type_and_pick_level(SFC_LOGTYPE_PREFIX "driver",
2281                                                    RTE_LOG_NOTICE);
2282         sfc_logtype_driver = (ret < 0) ? RTE_LOGTYPE_PMD : ret;
2283 }