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