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