xref: /dpdk/drivers/net/sfc/sfc_ethdev.c (revision 44db08d53be366d69bb7d16bffc3e55ba2d7398a)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright(c) 2019-2021 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 <ethdev_driver.h>
12 #include <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 #include "sfc_repr.h"
32 #include "sfc_sw_stats.h"
33 #include "sfc_switch.h"
34 
35 #define SFC_XSTAT_ID_INVALID_VAL  UINT64_MAX
36 #define SFC_XSTAT_ID_INVALID_NAME '\0'
37 
38 uint32_t sfc_logtype_driver;
39 
40 static struct sfc_dp_list sfc_dp_head =
41 	TAILQ_HEAD_INITIALIZER(sfc_dp_head);
42 
43 
44 static void sfc_eth_dev_clear_ops(struct rte_eth_dev *dev);
45 
46 
47 static int
48 sfc_fw_version_get(struct rte_eth_dev *dev, char *fw_version, size_t fw_size)
49 {
50 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
51 	efx_nic_fw_info_t enfi;
52 	int ret;
53 	int rc;
54 
55 	rc = efx_nic_get_fw_version(sa->nic, &enfi);
56 	if (rc != 0)
57 		return -rc;
58 
59 	ret = snprintf(fw_version, fw_size,
60 		       "%" PRIu16 ".%" PRIu16 ".%" PRIu16 ".%" PRIu16,
61 		       enfi.enfi_mc_fw_version[0], enfi.enfi_mc_fw_version[1],
62 		       enfi.enfi_mc_fw_version[2], enfi.enfi_mc_fw_version[3]);
63 	if (ret < 0)
64 		return ret;
65 
66 	if (enfi.enfi_dpcpu_fw_ids_valid) {
67 		size_t dpcpu_fw_ids_offset = MIN(fw_size - 1, (size_t)ret);
68 		int ret_extra;
69 
70 		ret_extra = snprintf(fw_version + dpcpu_fw_ids_offset,
71 				     fw_size - dpcpu_fw_ids_offset,
72 				     " rx%" PRIx16 " tx%" PRIx16,
73 				     enfi.enfi_rx_dpcpu_fw_id,
74 				     enfi.enfi_tx_dpcpu_fw_id);
75 		if (ret_extra < 0)
76 			return ret_extra;
77 
78 		ret += ret_extra;
79 	}
80 
81 	if (fw_size < (size_t)(++ret))
82 		return ret;
83 	else
84 		return 0;
85 }
86 
87 static int
88 sfc_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
89 {
90 	const struct sfc_adapter_priv *sap = sfc_adapter_priv_by_eth_dev(dev);
91 	struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
92 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
93 	struct sfc_rss *rss = &sas->rss;
94 	struct sfc_mae *mae = &sa->mae;
95 	uint64_t txq_offloads_def = 0;
96 
97 	sfc_log_init(sa, "entry");
98 
99 	dev_info->min_mtu = RTE_ETHER_MIN_MTU;
100 	dev_info->max_mtu = EFX_MAC_SDU_MAX;
101 
102 	dev_info->max_rx_pktlen = EFX_MAC_PDU_MAX;
103 
104 	dev_info->max_vfs = sa->sriov.num_vfs;
105 
106 	/* Autonegotiation may be disabled */
107 	dev_info->speed_capa = ETH_LINK_SPEED_FIXED;
108 	if (sa->port.phy_adv_cap_mask & (1u << EFX_PHY_CAP_1000FDX))
109 		dev_info->speed_capa |= ETH_LINK_SPEED_1G;
110 	if (sa->port.phy_adv_cap_mask & (1u << EFX_PHY_CAP_10000FDX))
111 		dev_info->speed_capa |= ETH_LINK_SPEED_10G;
112 	if (sa->port.phy_adv_cap_mask & (1u << EFX_PHY_CAP_25000FDX))
113 		dev_info->speed_capa |= ETH_LINK_SPEED_25G;
114 	if (sa->port.phy_adv_cap_mask & (1u << EFX_PHY_CAP_40000FDX))
115 		dev_info->speed_capa |= ETH_LINK_SPEED_40G;
116 	if (sa->port.phy_adv_cap_mask & (1u << EFX_PHY_CAP_50000FDX))
117 		dev_info->speed_capa |= ETH_LINK_SPEED_50G;
118 	if (sa->port.phy_adv_cap_mask & (1u << EFX_PHY_CAP_100000FDX))
119 		dev_info->speed_capa |= ETH_LINK_SPEED_100G;
120 
121 	dev_info->max_rx_queues = sa->rxq_max;
122 	dev_info->max_tx_queues = sa->txq_max;
123 
124 	/* By default packets are dropped if no descriptors are available */
125 	dev_info->default_rxconf.rx_drop_en = 1;
126 
127 	dev_info->rx_queue_offload_capa = sfc_rx_get_queue_offload_caps(sa);
128 
129 	/*
130 	 * rx_offload_capa includes both device and queue offloads since
131 	 * the latter may be requested on a per device basis which makes
132 	 * sense when some offloads are needed to be set on all queues.
133 	 */
134 	dev_info->rx_offload_capa = sfc_rx_get_dev_offload_caps(sa) |
135 				    dev_info->rx_queue_offload_capa;
136 
137 	dev_info->tx_queue_offload_capa = sfc_tx_get_queue_offload_caps(sa);
138 
139 	/*
140 	 * tx_offload_capa includes both device and queue offloads since
141 	 * the latter may be requested on a per device basis which makes
142 	 * sense when some offloads are needed to be set on all queues.
143 	 */
144 	dev_info->tx_offload_capa = sfc_tx_get_dev_offload_caps(sa) |
145 				    dev_info->tx_queue_offload_capa;
146 
147 	if (dev_info->tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
148 		txq_offloads_def |= DEV_TX_OFFLOAD_MBUF_FAST_FREE;
149 
150 	dev_info->default_txconf.offloads |= txq_offloads_def;
151 
152 	if (rss->context_type != EFX_RX_SCALE_UNAVAILABLE) {
153 		uint64_t rte_hf = 0;
154 		unsigned int i;
155 
156 		for (i = 0; i < rss->hf_map_nb_entries; ++i)
157 			rte_hf |= rss->hf_map[i].rte;
158 
159 		dev_info->reta_size = EFX_RSS_TBL_SIZE;
160 		dev_info->hash_key_size = EFX_RSS_KEY_SIZE;
161 		dev_info->flow_type_rss_offloads = rte_hf;
162 	}
163 
164 	/* Initialize to hardware limits */
165 	dev_info->rx_desc_lim.nb_max = sa->rxq_max_entries;
166 	dev_info->rx_desc_lim.nb_min = sa->rxq_min_entries;
167 	/* The RXQ hardware requires that the descriptor count is a power
168 	 * of 2, but rx_desc_lim cannot properly describe that constraint.
169 	 */
170 	dev_info->rx_desc_lim.nb_align = sa->rxq_min_entries;
171 
172 	/* Initialize to hardware limits */
173 	dev_info->tx_desc_lim.nb_max = sa->txq_max_entries;
174 	dev_info->tx_desc_lim.nb_min = sa->txq_min_entries;
175 	/*
176 	 * The TXQ hardware requires that the descriptor count is a power
177 	 * of 2, but tx_desc_lim cannot properly describe that constraint
178 	 */
179 	dev_info->tx_desc_lim.nb_align = sa->txq_min_entries;
180 
181 	if (sap->dp_rx->get_dev_info != NULL)
182 		sap->dp_rx->get_dev_info(dev_info);
183 	if (sap->dp_tx->get_dev_info != NULL)
184 		sap->dp_tx->get_dev_info(dev_info);
185 
186 	dev_info->dev_capa = RTE_ETH_DEV_CAPA_RUNTIME_RX_QUEUE_SETUP |
187 			     RTE_ETH_DEV_CAPA_RUNTIME_TX_QUEUE_SETUP;
188 
189 	if (mae->status == SFC_MAE_STATUS_SUPPORTED) {
190 		dev_info->switch_info.name = dev->device->driver->name;
191 		dev_info->switch_info.domain_id = mae->switch_domain_id;
192 		dev_info->switch_info.port_id = mae->switch_port_id;
193 	}
194 
195 	return 0;
196 }
197 
198 static const uint32_t *
199 sfc_dev_supported_ptypes_get(struct rte_eth_dev *dev)
200 {
201 	const struct sfc_adapter_priv *sap = sfc_adapter_priv_by_eth_dev(dev);
202 
203 	return sap->dp_rx->supported_ptypes_get(sap->shared->tunnel_encaps);
204 }
205 
206 static int
207 sfc_dev_configure(struct rte_eth_dev *dev)
208 {
209 	struct rte_eth_dev_data *dev_data = dev->data;
210 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
211 	int rc;
212 
213 	sfc_log_init(sa, "entry n_rxq=%u n_txq=%u",
214 		     dev_data->nb_rx_queues, dev_data->nb_tx_queues);
215 
216 	sfc_adapter_lock(sa);
217 	switch (sa->state) {
218 	case SFC_ETHDEV_CONFIGURED:
219 		/* FALLTHROUGH */
220 	case SFC_ETHDEV_INITIALIZED:
221 		rc = sfc_configure(sa);
222 		break;
223 	default:
224 		sfc_err(sa, "unexpected adapter state %u to configure",
225 			sa->state);
226 		rc = EINVAL;
227 		break;
228 	}
229 	sfc_adapter_unlock(sa);
230 
231 	sfc_log_init(sa, "done %d", rc);
232 	SFC_ASSERT(rc >= 0);
233 	return -rc;
234 }
235 
236 static int
237 sfc_dev_start(struct rte_eth_dev *dev)
238 {
239 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
240 	int rc;
241 
242 	sfc_log_init(sa, "entry");
243 
244 	sfc_adapter_lock(sa);
245 	rc = sfc_start(sa);
246 	sfc_adapter_unlock(sa);
247 
248 	sfc_log_init(sa, "done %d", rc);
249 	SFC_ASSERT(rc >= 0);
250 	return -rc;
251 }
252 
253 static int
254 sfc_dev_link_update(struct rte_eth_dev *dev, int wait_to_complete)
255 {
256 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
257 	struct rte_eth_link current_link;
258 	int ret;
259 
260 	sfc_log_init(sa, "entry");
261 
262 	if (sa->state != SFC_ETHDEV_STARTED) {
263 		sfc_port_link_mode_to_info(EFX_LINK_UNKNOWN, &current_link);
264 	} else if (wait_to_complete) {
265 		efx_link_mode_t link_mode;
266 
267 		if (efx_port_poll(sa->nic, &link_mode) != 0)
268 			link_mode = EFX_LINK_UNKNOWN;
269 		sfc_port_link_mode_to_info(link_mode, &current_link);
270 
271 	} else {
272 		sfc_ev_mgmt_qpoll(sa);
273 		rte_eth_linkstatus_get(dev, &current_link);
274 	}
275 
276 	ret = rte_eth_linkstatus_set(dev, &current_link);
277 	if (ret == 0)
278 		sfc_notice(sa, "Link status is %s",
279 			   current_link.link_status ? "UP" : "DOWN");
280 
281 	return ret;
282 }
283 
284 static int
285 sfc_dev_stop(struct rte_eth_dev *dev)
286 {
287 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
288 
289 	sfc_log_init(sa, "entry");
290 
291 	sfc_adapter_lock(sa);
292 	sfc_stop(sa);
293 	sfc_adapter_unlock(sa);
294 
295 	sfc_log_init(sa, "done");
296 
297 	return 0;
298 }
299 
300 static int
301 sfc_dev_set_link_up(struct rte_eth_dev *dev)
302 {
303 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
304 	int rc;
305 
306 	sfc_log_init(sa, "entry");
307 
308 	sfc_adapter_lock(sa);
309 	rc = sfc_start(sa);
310 	sfc_adapter_unlock(sa);
311 
312 	SFC_ASSERT(rc >= 0);
313 	return -rc;
314 }
315 
316 static int
317 sfc_dev_set_link_down(struct rte_eth_dev *dev)
318 {
319 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
320 
321 	sfc_log_init(sa, "entry");
322 
323 	sfc_adapter_lock(sa);
324 	sfc_stop(sa);
325 	sfc_adapter_unlock(sa);
326 
327 	return 0;
328 }
329 
330 static void
331 sfc_eth_dev_secondary_clear_ops(struct rte_eth_dev *dev)
332 {
333 	free(dev->process_private);
334 	rte_eth_dev_release_port(dev);
335 }
336 
337 static int
338 sfc_dev_close(struct rte_eth_dev *dev)
339 {
340 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
341 
342 	sfc_log_init(sa, "entry");
343 
344 	if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
345 		sfc_eth_dev_secondary_clear_ops(dev);
346 		return 0;
347 	}
348 
349 	sfc_pre_detach(sa);
350 
351 	sfc_adapter_lock(sa);
352 	switch (sa->state) {
353 	case SFC_ETHDEV_STARTED:
354 		sfc_stop(sa);
355 		SFC_ASSERT(sa->state == SFC_ETHDEV_CONFIGURED);
356 		/* FALLTHROUGH */
357 	case SFC_ETHDEV_CONFIGURED:
358 		sfc_close(sa);
359 		SFC_ASSERT(sa->state == SFC_ETHDEV_INITIALIZED);
360 		/* FALLTHROUGH */
361 	case SFC_ETHDEV_INITIALIZED:
362 		break;
363 	default:
364 		sfc_err(sa, "unexpected adapter state %u on close", sa->state);
365 		break;
366 	}
367 
368 	/*
369 	 * Cleanup all resources.
370 	 * Rollback primary process sfc_eth_dev_init() below.
371 	 */
372 
373 	sfc_eth_dev_clear_ops(dev);
374 
375 	sfc_detach(sa);
376 	sfc_unprobe(sa);
377 
378 	sfc_kvargs_cleanup(sa);
379 
380 	sfc_adapter_unlock(sa);
381 	sfc_adapter_lock_fini(sa);
382 
383 	sfc_log_init(sa, "done");
384 
385 	/* Required for logging, so cleanup last */
386 	sa->eth_dev = NULL;
387 
388 	free(sa);
389 
390 	return 0;
391 }
392 
393 static int
394 sfc_dev_filter_set(struct rte_eth_dev *dev, enum sfc_dev_filter_mode mode,
395 		   boolean_t enabled)
396 {
397 	struct sfc_port *port;
398 	boolean_t *toggle;
399 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
400 	boolean_t allmulti = (mode == SFC_DEV_FILTER_MODE_ALLMULTI);
401 	const char *desc = (allmulti) ? "all-multi" : "promiscuous";
402 	int rc = 0;
403 
404 	sfc_adapter_lock(sa);
405 
406 	port = &sa->port;
407 	toggle = (allmulti) ? (&port->allmulti) : (&port->promisc);
408 
409 	if (*toggle != enabled) {
410 		*toggle = enabled;
411 
412 		if (sfc_sa2shared(sa)->isolated) {
413 			sfc_warn(sa, "isolated mode is active on the port");
414 			sfc_warn(sa, "the change is to be applied on the next "
415 				     "start provided that isolated mode is "
416 				     "disabled prior the next start");
417 		} else if ((sa->state == SFC_ETHDEV_STARTED) &&
418 			   ((rc = sfc_set_rx_mode(sa)) != 0)) {
419 			*toggle = !(enabled);
420 			sfc_warn(sa, "Failed to %s %s mode, rc = %d",
421 				 ((enabled) ? "enable" : "disable"), desc, rc);
422 
423 			/*
424 			 * For promiscuous and all-multicast filters a
425 			 * permission failure should be reported as an
426 			 * unsupported filter.
427 			 */
428 			if (rc == EPERM)
429 				rc = ENOTSUP;
430 		}
431 	}
432 
433 	sfc_adapter_unlock(sa);
434 	return rc;
435 }
436 
437 static int
438 sfc_dev_promisc_enable(struct rte_eth_dev *dev)
439 {
440 	int rc = sfc_dev_filter_set(dev, SFC_DEV_FILTER_MODE_PROMISC, B_TRUE);
441 
442 	SFC_ASSERT(rc >= 0);
443 	return -rc;
444 }
445 
446 static int
447 sfc_dev_promisc_disable(struct rte_eth_dev *dev)
448 {
449 	int rc = sfc_dev_filter_set(dev, SFC_DEV_FILTER_MODE_PROMISC, B_FALSE);
450 
451 	SFC_ASSERT(rc >= 0);
452 	return -rc;
453 }
454 
455 static int
456 sfc_dev_allmulti_enable(struct rte_eth_dev *dev)
457 {
458 	int rc = sfc_dev_filter_set(dev, SFC_DEV_FILTER_MODE_ALLMULTI, B_TRUE);
459 
460 	SFC_ASSERT(rc >= 0);
461 	return -rc;
462 }
463 
464 static int
465 sfc_dev_allmulti_disable(struct rte_eth_dev *dev)
466 {
467 	int rc = sfc_dev_filter_set(dev, SFC_DEV_FILTER_MODE_ALLMULTI, B_FALSE);
468 
469 	SFC_ASSERT(rc >= 0);
470 	return -rc;
471 }
472 
473 static int
474 sfc_rx_queue_setup(struct rte_eth_dev *dev, uint16_t ethdev_qid,
475 		   uint16_t nb_rx_desc, unsigned int socket_id,
476 		   const struct rte_eth_rxconf *rx_conf,
477 		   struct rte_mempool *mb_pool)
478 {
479 	struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
480 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
481 	sfc_ethdev_qid_t sfc_ethdev_qid = ethdev_qid;
482 	struct sfc_rxq_info *rxq_info;
483 	sfc_sw_index_t sw_index;
484 	int rc;
485 
486 	sfc_log_init(sa, "RxQ=%u nb_rx_desc=%u socket_id=%u",
487 		     ethdev_qid, nb_rx_desc, socket_id);
488 
489 	sfc_adapter_lock(sa);
490 
491 	sw_index = sfc_rxq_sw_index_by_ethdev_rx_qid(sas, sfc_ethdev_qid);
492 	rc = sfc_rx_qinit(sa, sw_index, nb_rx_desc, socket_id,
493 			  rx_conf, mb_pool);
494 	if (rc != 0)
495 		goto fail_rx_qinit;
496 
497 	rxq_info = sfc_rxq_info_by_ethdev_qid(sas, sfc_ethdev_qid);
498 	dev->data->rx_queues[ethdev_qid] = rxq_info->dp;
499 
500 	sfc_adapter_unlock(sa);
501 
502 	return 0;
503 
504 fail_rx_qinit:
505 	sfc_adapter_unlock(sa);
506 	SFC_ASSERT(rc > 0);
507 	return -rc;
508 }
509 
510 static void
511 sfc_rx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
512 {
513 	struct sfc_dp_rxq *dp_rxq = dev->data->rx_queues[qid];
514 	struct sfc_rxq *rxq;
515 	struct sfc_adapter *sa;
516 	sfc_sw_index_t sw_index;
517 
518 	if (dp_rxq == NULL)
519 		return;
520 
521 	rxq = sfc_rxq_by_dp_rxq(dp_rxq);
522 	sa = rxq->evq->sa;
523 	sfc_adapter_lock(sa);
524 
525 	sw_index = dp_rxq->dpq.queue_id;
526 
527 	sfc_log_init(sa, "RxQ=%u", sw_index);
528 
529 	sfc_rx_qfini(sa, sw_index);
530 
531 	sfc_adapter_unlock(sa);
532 }
533 
534 static int
535 sfc_tx_queue_setup(struct rte_eth_dev *dev, uint16_t ethdev_qid,
536 		   uint16_t nb_tx_desc, unsigned int socket_id,
537 		   const struct rte_eth_txconf *tx_conf)
538 {
539 	struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
540 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
541 	struct sfc_txq_info *txq_info;
542 	sfc_sw_index_t sw_index;
543 	int rc;
544 
545 	sfc_log_init(sa, "TxQ = %u, nb_tx_desc = %u, socket_id = %u",
546 		     ethdev_qid, nb_tx_desc, socket_id);
547 
548 	sfc_adapter_lock(sa);
549 
550 	sw_index = sfc_txq_sw_index_by_ethdev_tx_qid(sas, ethdev_qid);
551 	rc = sfc_tx_qinit(sa, sw_index, nb_tx_desc, socket_id, tx_conf);
552 	if (rc != 0)
553 		goto fail_tx_qinit;
554 
555 	txq_info = sfc_txq_info_by_ethdev_qid(sas, ethdev_qid);
556 	dev->data->tx_queues[ethdev_qid] = txq_info->dp;
557 
558 	sfc_adapter_unlock(sa);
559 	return 0;
560 
561 fail_tx_qinit:
562 	sfc_adapter_unlock(sa);
563 	SFC_ASSERT(rc > 0);
564 	return -rc;
565 }
566 
567 static void
568 sfc_tx_queue_release(struct rte_eth_dev *dev, uint16_t qid)
569 {
570 	struct sfc_dp_txq *dp_txq = dev->data->tx_queues[qid];
571 	struct sfc_txq *txq;
572 	sfc_sw_index_t sw_index;
573 	struct sfc_adapter *sa;
574 
575 	if (dp_txq == NULL)
576 		return;
577 
578 	txq = sfc_txq_by_dp_txq(dp_txq);
579 	sw_index = dp_txq->dpq.queue_id;
580 
581 	SFC_ASSERT(txq->evq != NULL);
582 	sa = txq->evq->sa;
583 
584 	sfc_log_init(sa, "TxQ = %u", sw_index);
585 
586 	sfc_adapter_lock(sa);
587 
588 	sfc_tx_qfini(sa, sw_index);
589 
590 	sfc_adapter_unlock(sa);
591 }
592 
593 static void
594 sfc_stats_get_dp_rx(struct sfc_adapter *sa, uint64_t *pkts, uint64_t *bytes)
595 {
596 	struct sfc_adapter_shared *sas = sfc_sa2shared(sa);
597 	uint64_t pkts_sum = 0;
598 	uint64_t bytes_sum = 0;
599 	unsigned int i;
600 
601 	for (i = 0; i < sas->ethdev_rxq_count; ++i) {
602 		struct sfc_rxq_info *rxq_info;
603 
604 		rxq_info = sfc_rxq_info_by_ethdev_qid(sas, i);
605 		if (rxq_info->state & SFC_RXQ_INITIALIZED) {
606 			union sfc_pkts_bytes qstats;
607 
608 			sfc_pkts_bytes_get(&rxq_info->dp->dpq.stats, &qstats);
609 			pkts_sum += qstats.pkts -
610 					sa->sw_stats.reset_rx_pkts[i];
611 			bytes_sum += qstats.bytes -
612 					sa->sw_stats.reset_rx_bytes[i];
613 		}
614 	}
615 
616 	*pkts = pkts_sum;
617 	*bytes = bytes_sum;
618 }
619 
620 static void
621 sfc_stats_get_dp_tx(struct sfc_adapter *sa, uint64_t *pkts, uint64_t *bytes)
622 {
623 	struct sfc_adapter_shared *sas = sfc_sa2shared(sa);
624 	uint64_t pkts_sum = 0;
625 	uint64_t bytes_sum = 0;
626 	unsigned int i;
627 
628 	for (i = 0; i < sas->ethdev_txq_count; ++i) {
629 		struct sfc_txq_info *txq_info;
630 
631 		txq_info = sfc_txq_info_by_ethdev_qid(sas, i);
632 		if (txq_info->state & SFC_TXQ_INITIALIZED) {
633 			union sfc_pkts_bytes qstats;
634 
635 			sfc_pkts_bytes_get(&txq_info->dp->dpq.stats, &qstats);
636 			pkts_sum += qstats.pkts -
637 					sa->sw_stats.reset_tx_pkts[i];
638 			bytes_sum += qstats.bytes -
639 					sa->sw_stats.reset_tx_bytes[i];
640 		}
641 	}
642 
643 	*pkts = pkts_sum;
644 	*bytes = bytes_sum;
645 }
646 
647 /*
648  * Some statistics are computed as A - B where A and B each increase
649  * monotonically with some hardware counter(s) and the counters are read
650  * asynchronously.
651  *
652  * If packet X is counted in A, but not counted in B yet, computed value is
653  * greater than real.
654  *
655  * If packet X is not counted in A at the moment of reading the counter,
656  * but counted in B at the moment of reading the counter, computed value
657  * is less than real.
658  *
659  * However, counter which grows backward is worse evil than slightly wrong
660  * value. So, let's try to guarantee that it never happens except may be
661  * the case when the MAC stats are zeroed as a result of a NIC reset.
662  */
663 static void
664 sfc_update_diff_stat(uint64_t *stat, uint64_t newval)
665 {
666 	if ((int64_t)(newval - *stat) > 0 || newval == 0)
667 		*stat = newval;
668 }
669 
670 static int
671 sfc_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
672 {
673 	const struct sfc_adapter_priv *sap = sfc_adapter_priv_by_eth_dev(dev);
674 	bool have_dp_rx_stats = sap->dp_rx->features & SFC_DP_RX_FEAT_STATS;
675 	bool have_dp_tx_stats = sap->dp_tx->features & SFC_DP_TX_FEAT_STATS;
676 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
677 	struct sfc_port *port = &sa->port;
678 	uint64_t *mac_stats;
679 	int ret;
680 
681 	sfc_adapter_lock(sa);
682 
683 	if (have_dp_rx_stats)
684 		sfc_stats_get_dp_rx(sa, &stats->ipackets, &stats->ibytes);
685 	if (have_dp_tx_stats)
686 		sfc_stats_get_dp_tx(sa, &stats->opackets, &stats->obytes);
687 
688 	ret = sfc_port_update_mac_stats(sa, B_FALSE);
689 	if (ret != 0)
690 		goto unlock;
691 
692 	mac_stats = port->mac_stats_buf;
693 
694 	if (EFX_MAC_STAT_SUPPORTED(port->mac_stats_mask,
695 				   EFX_MAC_VADAPTER_RX_UNICAST_PACKETS)) {
696 		if (!have_dp_rx_stats) {
697 			stats->ipackets =
698 				mac_stats[EFX_MAC_VADAPTER_RX_UNICAST_PACKETS] +
699 				mac_stats[EFX_MAC_VADAPTER_RX_MULTICAST_PACKETS] +
700 				mac_stats[EFX_MAC_VADAPTER_RX_BROADCAST_PACKETS];
701 			stats->ibytes =
702 				mac_stats[EFX_MAC_VADAPTER_RX_UNICAST_BYTES] +
703 				mac_stats[EFX_MAC_VADAPTER_RX_MULTICAST_BYTES] +
704 				mac_stats[EFX_MAC_VADAPTER_RX_BROADCAST_BYTES];
705 
706 			/* CRC is included in these stats, but shouldn't be */
707 			stats->ibytes -= stats->ipackets * RTE_ETHER_CRC_LEN;
708 		}
709 		if (!have_dp_tx_stats) {
710 			stats->opackets =
711 				mac_stats[EFX_MAC_VADAPTER_TX_UNICAST_PACKETS] +
712 				mac_stats[EFX_MAC_VADAPTER_TX_MULTICAST_PACKETS] +
713 				mac_stats[EFX_MAC_VADAPTER_TX_BROADCAST_PACKETS];
714 			stats->obytes =
715 				mac_stats[EFX_MAC_VADAPTER_TX_UNICAST_BYTES] +
716 				mac_stats[EFX_MAC_VADAPTER_TX_MULTICAST_BYTES] +
717 				mac_stats[EFX_MAC_VADAPTER_TX_BROADCAST_BYTES];
718 
719 			/* CRC is included in these stats, but shouldn't be */
720 			stats->obytes -= stats->opackets * RTE_ETHER_CRC_LEN;
721 		}
722 		stats->imissed = mac_stats[EFX_MAC_VADAPTER_RX_BAD_PACKETS];
723 		stats->oerrors = mac_stats[EFX_MAC_VADAPTER_TX_BAD_PACKETS];
724 	} else {
725 		if (!have_dp_tx_stats) {
726 			stats->opackets = mac_stats[EFX_MAC_TX_PKTS];
727 			stats->obytes = mac_stats[EFX_MAC_TX_OCTETS] -
728 				mac_stats[EFX_MAC_TX_PKTS] * RTE_ETHER_CRC_LEN;
729 		}
730 
731 		/*
732 		 * Take into account stats which are whenever supported
733 		 * on EF10. If some stat is not supported by current
734 		 * firmware variant or HW revision, it is guaranteed
735 		 * to be zero in mac_stats.
736 		 */
737 		stats->imissed =
738 			mac_stats[EFX_MAC_RX_NODESC_DROP_CNT] +
739 			mac_stats[EFX_MAC_PM_TRUNC_BB_OVERFLOW] +
740 			mac_stats[EFX_MAC_PM_DISCARD_BB_OVERFLOW] +
741 			mac_stats[EFX_MAC_PM_TRUNC_VFIFO_FULL] +
742 			mac_stats[EFX_MAC_PM_DISCARD_VFIFO_FULL] +
743 			mac_stats[EFX_MAC_PM_TRUNC_QBB] +
744 			mac_stats[EFX_MAC_PM_DISCARD_QBB] +
745 			mac_stats[EFX_MAC_PM_DISCARD_MAPPING] +
746 			mac_stats[EFX_MAC_RXDP_Q_DISABLED_PKTS] +
747 			mac_stats[EFX_MAC_RXDP_DI_DROPPED_PKTS];
748 		stats->ierrors =
749 			mac_stats[EFX_MAC_RX_FCS_ERRORS] +
750 			mac_stats[EFX_MAC_RX_ALIGN_ERRORS] +
751 			mac_stats[EFX_MAC_RX_JABBER_PKTS];
752 		/* no oerrors counters supported on EF10 */
753 
754 		if (!have_dp_rx_stats) {
755 			/* Exclude missed, errors and pauses from Rx packets */
756 			sfc_update_diff_stat(&port->ipackets,
757 				mac_stats[EFX_MAC_RX_PKTS] -
758 				mac_stats[EFX_MAC_RX_PAUSE_PKTS] -
759 				stats->imissed - stats->ierrors);
760 			stats->ipackets = port->ipackets;
761 			stats->ibytes = mac_stats[EFX_MAC_RX_OCTETS] -
762 				mac_stats[EFX_MAC_RX_PKTS] * RTE_ETHER_CRC_LEN;
763 		}
764 	}
765 
766 unlock:
767 	sfc_adapter_unlock(sa);
768 	SFC_ASSERT(ret >= 0);
769 	return -ret;
770 }
771 
772 static int
773 sfc_stats_reset(struct rte_eth_dev *dev)
774 {
775 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
776 	struct sfc_port *port = &sa->port;
777 	int rc;
778 
779 	sfc_adapter_lock(sa);
780 
781 	if (sa->state != SFC_ETHDEV_STARTED) {
782 		/*
783 		 * The operation cannot be done if port is not started; it
784 		 * will be scheduled to be done during the next port start
785 		 */
786 		port->mac_stats_reset_pending = B_TRUE;
787 		sfc_adapter_unlock(sa);
788 		return 0;
789 	}
790 
791 	rc = sfc_port_reset_mac_stats(sa);
792 	if (rc != 0)
793 		sfc_err(sa, "failed to reset statistics (rc = %d)", rc);
794 
795 	sfc_sw_xstats_reset(sa);
796 
797 	sfc_adapter_unlock(sa);
798 
799 	SFC_ASSERT(rc >= 0);
800 	return -rc;
801 }
802 
803 static unsigned int
804 sfc_xstats_get_nb_supported(struct sfc_adapter *sa)
805 {
806 	struct sfc_port *port = &sa->port;
807 	unsigned int nb_supported;
808 
809 	sfc_adapter_lock(sa);
810 	nb_supported = port->mac_stats_nb_supported +
811 		       sfc_sw_xstats_get_nb_supported(sa);
812 	sfc_adapter_unlock(sa);
813 
814 	return nb_supported;
815 }
816 
817 static int
818 sfc_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
819 	       unsigned int xstats_count)
820 {
821 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
822 	unsigned int nb_written = 0;
823 	unsigned int nb_supported = 0;
824 	int rc;
825 
826 	if (unlikely(xstats == NULL))
827 		return sfc_xstats_get_nb_supported(sa);
828 
829 	rc = sfc_port_get_mac_stats(sa, xstats, xstats_count, &nb_written);
830 	if (rc < 0)
831 		return rc;
832 
833 	nb_supported = rc;
834 	sfc_sw_xstats_get_vals(sa, xstats, xstats_count, &nb_written,
835 			       &nb_supported);
836 
837 	return nb_supported;
838 }
839 
840 static int
841 sfc_xstats_get_names(struct rte_eth_dev *dev,
842 		     struct rte_eth_xstat_name *xstats_names,
843 		     unsigned int xstats_count)
844 {
845 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
846 	struct sfc_port *port = &sa->port;
847 	unsigned int i;
848 	unsigned int nstats = 0;
849 	unsigned int nb_written = 0;
850 	int ret;
851 
852 	if (unlikely(xstats_names == NULL))
853 		return sfc_xstats_get_nb_supported(sa);
854 
855 	for (i = 0; i < EFX_MAC_NSTATS; ++i) {
856 		if (EFX_MAC_STAT_SUPPORTED(port->mac_stats_mask, i)) {
857 			if (nstats < xstats_count) {
858 				strlcpy(xstats_names[nstats].name,
859 					efx_mac_stat_name(sa->nic, i),
860 					sizeof(xstats_names[0].name));
861 				nb_written++;
862 			}
863 			nstats++;
864 		}
865 	}
866 
867 	ret = sfc_sw_xstats_get_names(sa, xstats_names, xstats_count,
868 				      &nb_written, &nstats);
869 	if (ret != 0) {
870 		SFC_ASSERT(ret < 0);
871 		return ret;
872 	}
873 
874 	return nstats;
875 }
876 
877 static int
878 sfc_xstats_get_by_id(struct rte_eth_dev *dev, const uint64_t *ids,
879 		     uint64_t *values, unsigned int n)
880 {
881 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
882 	struct sfc_port *port = &sa->port;
883 	unsigned int nb_supported;
884 	unsigned int i;
885 	int rc;
886 
887 	if (unlikely(ids == NULL || values == NULL))
888 		return -EINVAL;
889 
890 	/*
891 	 * Values array could be filled in nonsequential order. Fill values with
892 	 * constant indicating invalid ID first.
893 	 */
894 	for (i = 0; i < n; i++)
895 		values[i] = SFC_XSTAT_ID_INVALID_VAL;
896 
897 	rc = sfc_port_get_mac_stats_by_id(sa, ids, values, n);
898 	if (rc != 0)
899 		return rc;
900 
901 	nb_supported = port->mac_stats_nb_supported;
902 	sfc_sw_xstats_get_vals_by_id(sa, ids, values, n, &nb_supported);
903 
904 	/* Return number of written stats before invalid ID is encountered. */
905 	for (i = 0; i < n; i++) {
906 		if (values[i] == SFC_XSTAT_ID_INVALID_VAL)
907 			return i;
908 	}
909 
910 	return n;
911 }
912 
913 static int
914 sfc_xstats_get_names_by_id(struct rte_eth_dev *dev,
915 			   const uint64_t *ids,
916 			   struct rte_eth_xstat_name *xstats_names,
917 			   unsigned int size)
918 {
919 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
920 	struct sfc_port *port = &sa->port;
921 	unsigned int nb_supported;
922 	unsigned int i;
923 	int ret;
924 
925 	if (unlikely(xstats_names == NULL && ids != NULL) ||
926 	    unlikely(xstats_names != NULL && ids == NULL))
927 		return -EINVAL;
928 
929 	if (unlikely(xstats_names == NULL && ids == NULL))
930 		return sfc_xstats_get_nb_supported(sa);
931 
932 	/*
933 	 * Names array could be filled in nonsequential order. Fill names with
934 	 * string indicating invalid ID first.
935 	 */
936 	for (i = 0; i < size; i++)
937 		xstats_names[i].name[0] = SFC_XSTAT_ID_INVALID_NAME;
938 
939 	sfc_adapter_lock(sa);
940 
941 	SFC_ASSERT(port->mac_stats_nb_supported <=
942 		   RTE_DIM(port->mac_stats_by_id));
943 
944 	for (i = 0; i < size; i++) {
945 		if (ids[i] < port->mac_stats_nb_supported) {
946 			strlcpy(xstats_names[i].name,
947 				efx_mac_stat_name(sa->nic,
948 						 port->mac_stats_by_id[ids[i]]),
949 				sizeof(xstats_names[0].name));
950 		}
951 	}
952 
953 	nb_supported = port->mac_stats_nb_supported;
954 
955 	sfc_adapter_unlock(sa);
956 
957 	ret = sfc_sw_xstats_get_names_by_id(sa, ids, xstats_names, size,
958 					    &nb_supported);
959 	if (ret != 0) {
960 		SFC_ASSERT(ret < 0);
961 		return ret;
962 	}
963 
964 	/* Return number of written names before invalid ID is encountered. */
965 	for (i = 0; i < size; i++) {
966 		if (xstats_names[i].name[0] == SFC_XSTAT_ID_INVALID_NAME)
967 			return i;
968 	}
969 
970 	return size;
971 }
972 
973 static int
974 sfc_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
975 {
976 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
977 	unsigned int wanted_fc, link_fc;
978 
979 	memset(fc_conf, 0, sizeof(*fc_conf));
980 
981 	sfc_adapter_lock(sa);
982 
983 	if (sa->state == SFC_ETHDEV_STARTED)
984 		efx_mac_fcntl_get(sa->nic, &wanted_fc, &link_fc);
985 	else
986 		link_fc = sa->port.flow_ctrl;
987 
988 	switch (link_fc) {
989 	case 0:
990 		fc_conf->mode = RTE_FC_NONE;
991 		break;
992 	case EFX_FCNTL_RESPOND:
993 		fc_conf->mode = RTE_FC_RX_PAUSE;
994 		break;
995 	case EFX_FCNTL_GENERATE:
996 		fc_conf->mode = RTE_FC_TX_PAUSE;
997 		break;
998 	case (EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE):
999 		fc_conf->mode = RTE_FC_FULL;
1000 		break;
1001 	default:
1002 		sfc_err(sa, "%s: unexpected flow control value %#x",
1003 			__func__, link_fc);
1004 	}
1005 
1006 	fc_conf->autoneg = sa->port.flow_ctrl_autoneg;
1007 
1008 	sfc_adapter_unlock(sa);
1009 
1010 	return 0;
1011 }
1012 
1013 static int
1014 sfc_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
1015 {
1016 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1017 	struct sfc_port *port = &sa->port;
1018 	unsigned int fcntl;
1019 	int rc;
1020 
1021 	if (fc_conf->high_water != 0 || fc_conf->low_water != 0 ||
1022 	    fc_conf->pause_time != 0 || fc_conf->send_xon != 0 ||
1023 	    fc_conf->mac_ctrl_frame_fwd != 0) {
1024 		sfc_err(sa, "unsupported flow control settings specified");
1025 		rc = EINVAL;
1026 		goto fail_inval;
1027 	}
1028 
1029 	switch (fc_conf->mode) {
1030 	case RTE_FC_NONE:
1031 		fcntl = 0;
1032 		break;
1033 	case RTE_FC_RX_PAUSE:
1034 		fcntl = EFX_FCNTL_RESPOND;
1035 		break;
1036 	case RTE_FC_TX_PAUSE:
1037 		fcntl = EFX_FCNTL_GENERATE;
1038 		break;
1039 	case RTE_FC_FULL:
1040 		fcntl = EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE;
1041 		break;
1042 	default:
1043 		rc = EINVAL;
1044 		goto fail_inval;
1045 	}
1046 
1047 	sfc_adapter_lock(sa);
1048 
1049 	if (sa->state == SFC_ETHDEV_STARTED) {
1050 		rc = efx_mac_fcntl_set(sa->nic, fcntl, fc_conf->autoneg);
1051 		if (rc != 0)
1052 			goto fail_mac_fcntl_set;
1053 	}
1054 
1055 	port->flow_ctrl = fcntl;
1056 	port->flow_ctrl_autoneg = fc_conf->autoneg;
1057 
1058 	sfc_adapter_unlock(sa);
1059 
1060 	return 0;
1061 
1062 fail_mac_fcntl_set:
1063 	sfc_adapter_unlock(sa);
1064 fail_inval:
1065 	SFC_ASSERT(rc > 0);
1066 	return -rc;
1067 }
1068 
1069 static int
1070 sfc_check_scatter_on_all_rx_queues(struct sfc_adapter *sa, size_t pdu)
1071 {
1072 	struct sfc_adapter_shared * const sas = sfc_sa2shared(sa);
1073 	const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
1074 	boolean_t scatter_enabled;
1075 	const char *error;
1076 	unsigned int i;
1077 
1078 	for (i = 0; i < sas->rxq_count; i++) {
1079 		if ((sas->rxq_info[i].state & SFC_RXQ_INITIALIZED) == 0)
1080 			continue;
1081 
1082 		scatter_enabled = (sas->rxq_info[i].type_flags &
1083 				   EFX_RXQ_FLAG_SCATTER);
1084 
1085 		if (!sfc_rx_check_scatter(pdu, sa->rxq_ctrl[i].buf_size,
1086 					  encp->enc_rx_prefix_size,
1087 					  scatter_enabled,
1088 					  encp->enc_rx_scatter_max, &error)) {
1089 			sfc_err(sa, "MTU check for RxQ %u failed: %s", i,
1090 				error);
1091 			return EINVAL;
1092 		}
1093 	}
1094 
1095 	return 0;
1096 }
1097 
1098 static int
1099 sfc_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
1100 {
1101 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1102 	size_t pdu = EFX_MAC_PDU(mtu);
1103 	size_t old_pdu;
1104 	int rc;
1105 
1106 	sfc_log_init(sa, "mtu=%u", mtu);
1107 
1108 	rc = EINVAL;
1109 	if (pdu < EFX_MAC_PDU_MIN) {
1110 		sfc_err(sa, "too small MTU %u (PDU size %u less than min %u)",
1111 			(unsigned int)mtu, (unsigned int)pdu,
1112 			EFX_MAC_PDU_MIN);
1113 		goto fail_inval;
1114 	}
1115 	if (pdu > EFX_MAC_PDU_MAX) {
1116 		sfc_err(sa, "too big MTU %u (PDU size %u greater than max %u)",
1117 			(unsigned int)mtu, (unsigned int)pdu,
1118 			(unsigned int)EFX_MAC_PDU_MAX);
1119 		goto fail_inval;
1120 	}
1121 
1122 	sfc_adapter_lock(sa);
1123 
1124 	rc = sfc_check_scatter_on_all_rx_queues(sa, pdu);
1125 	if (rc != 0)
1126 		goto fail_check_scatter;
1127 
1128 	if (pdu != sa->port.pdu) {
1129 		if (sa->state == SFC_ETHDEV_STARTED) {
1130 			sfc_stop(sa);
1131 
1132 			old_pdu = sa->port.pdu;
1133 			sa->port.pdu = pdu;
1134 			rc = sfc_start(sa);
1135 			if (rc != 0)
1136 				goto fail_start;
1137 		} else {
1138 			sa->port.pdu = pdu;
1139 		}
1140 	}
1141 
1142 	/*
1143 	 * The driver does not use it, but other PMDs update jumbo frame
1144 	 * flag and max_rx_pkt_len when MTU is set.
1145 	 */
1146 	if (mtu > RTE_ETHER_MTU) {
1147 		struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
1148 		rxmode->offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
1149 	}
1150 
1151 	dev->data->dev_conf.rxmode.max_rx_pkt_len = sa->port.pdu;
1152 
1153 	sfc_adapter_unlock(sa);
1154 
1155 	sfc_log_init(sa, "done");
1156 	return 0;
1157 
1158 fail_start:
1159 	sa->port.pdu = old_pdu;
1160 	if (sfc_start(sa) != 0)
1161 		sfc_err(sa, "cannot start with neither new (%u) nor old (%u) "
1162 			"PDU max size - port is stopped",
1163 			(unsigned int)pdu, (unsigned int)old_pdu);
1164 
1165 fail_check_scatter:
1166 	sfc_adapter_unlock(sa);
1167 
1168 fail_inval:
1169 	sfc_log_init(sa, "failed %d", rc);
1170 	SFC_ASSERT(rc > 0);
1171 	return -rc;
1172 }
1173 static int
1174 sfc_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
1175 {
1176 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1177 	const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
1178 	struct sfc_port *port = &sa->port;
1179 	struct rte_ether_addr *old_addr = &dev->data->mac_addrs[0];
1180 	int rc = 0;
1181 
1182 	sfc_adapter_lock(sa);
1183 
1184 	if (rte_is_same_ether_addr(mac_addr, &port->default_mac_addr))
1185 		goto unlock;
1186 
1187 	/*
1188 	 * Copy the address to the device private data so that
1189 	 * it could be recalled in the case of adapter restart.
1190 	 */
1191 	rte_ether_addr_copy(mac_addr, &port->default_mac_addr);
1192 
1193 	/*
1194 	 * Neither of the two following checks can return
1195 	 * an error. The new MAC address is preserved in
1196 	 * the device private data and can be activated
1197 	 * on the next port start if the user prevents
1198 	 * isolated mode from being enabled.
1199 	 */
1200 	if (sfc_sa2shared(sa)->isolated) {
1201 		sfc_warn(sa, "isolated mode is active on the port");
1202 		sfc_warn(sa, "will not set MAC address");
1203 		goto unlock;
1204 	}
1205 
1206 	if (sa->state != SFC_ETHDEV_STARTED) {
1207 		sfc_notice(sa, "the port is not started");
1208 		sfc_notice(sa, "the new MAC address will be set on port start");
1209 
1210 		goto unlock;
1211 	}
1212 
1213 	if (encp->enc_allow_set_mac_with_installed_filters) {
1214 		rc = efx_mac_addr_set(sa->nic, mac_addr->addr_bytes);
1215 		if (rc != 0) {
1216 			sfc_err(sa, "cannot set MAC address (rc = %u)", rc);
1217 			goto unlock;
1218 		}
1219 
1220 		/*
1221 		 * Changing the MAC address by means of MCDI request
1222 		 * has no effect on received traffic, therefore
1223 		 * we also need to update unicast filters
1224 		 */
1225 		rc = sfc_set_rx_mode_unchecked(sa);
1226 		if (rc != 0) {
1227 			sfc_err(sa, "cannot set filter (rc = %u)", rc);
1228 			/* Rollback the old address */
1229 			(void)efx_mac_addr_set(sa->nic, old_addr->addr_bytes);
1230 			(void)sfc_set_rx_mode_unchecked(sa);
1231 		}
1232 	} else {
1233 		sfc_warn(sa, "cannot set MAC address with filters installed");
1234 		sfc_warn(sa, "adapter will be restarted to pick the new MAC");
1235 		sfc_warn(sa, "(some traffic may be dropped)");
1236 
1237 		/*
1238 		 * Since setting MAC address with filters installed is not
1239 		 * allowed on the adapter, the new MAC address will be set
1240 		 * by means of adapter restart. sfc_start() shall retrieve
1241 		 * the new address from the device private data and set it.
1242 		 */
1243 		sfc_stop(sa);
1244 		rc = sfc_start(sa);
1245 		if (rc != 0)
1246 			sfc_err(sa, "cannot restart adapter (rc = %u)", rc);
1247 	}
1248 
1249 unlock:
1250 	if (rc != 0)
1251 		rte_ether_addr_copy(old_addr, &port->default_mac_addr);
1252 
1253 	sfc_adapter_unlock(sa);
1254 
1255 	SFC_ASSERT(rc >= 0);
1256 	return -rc;
1257 }
1258 
1259 
1260 static int
1261 sfc_set_mc_addr_list(struct rte_eth_dev *dev,
1262 		struct rte_ether_addr *mc_addr_set, uint32_t nb_mc_addr)
1263 {
1264 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1265 	struct sfc_port *port = &sa->port;
1266 	uint8_t *mc_addrs = port->mcast_addrs;
1267 	int rc;
1268 	unsigned int i;
1269 
1270 	if (sfc_sa2shared(sa)->isolated) {
1271 		sfc_err(sa, "isolated mode is active on the port");
1272 		sfc_err(sa, "will not set multicast address list");
1273 		return -ENOTSUP;
1274 	}
1275 
1276 	if (mc_addrs == NULL)
1277 		return -ENOBUFS;
1278 
1279 	if (nb_mc_addr > port->max_mcast_addrs) {
1280 		sfc_err(sa, "too many multicast addresses: %u > %u",
1281 			 nb_mc_addr, port->max_mcast_addrs);
1282 		return -EINVAL;
1283 	}
1284 
1285 	for (i = 0; i < nb_mc_addr; ++i) {
1286 		rte_memcpy(mc_addrs, mc_addr_set[i].addr_bytes,
1287 				 EFX_MAC_ADDR_LEN);
1288 		mc_addrs += EFX_MAC_ADDR_LEN;
1289 	}
1290 
1291 	port->nb_mcast_addrs = nb_mc_addr;
1292 
1293 	if (sa->state != SFC_ETHDEV_STARTED)
1294 		return 0;
1295 
1296 	rc = efx_mac_multicast_list_set(sa->nic, port->mcast_addrs,
1297 					port->nb_mcast_addrs);
1298 	if (rc != 0)
1299 		sfc_err(sa, "cannot set multicast address list (rc = %u)", rc);
1300 
1301 	SFC_ASSERT(rc >= 0);
1302 	return -rc;
1303 }
1304 
1305 /*
1306  * The function is used by the secondary process as well. It must not
1307  * use any process-local pointers from the adapter data.
1308  */
1309 static void
1310 sfc_rx_queue_info_get(struct rte_eth_dev *dev, uint16_t ethdev_qid,
1311 		      struct rte_eth_rxq_info *qinfo)
1312 {
1313 	struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1314 	sfc_ethdev_qid_t sfc_ethdev_qid = ethdev_qid;
1315 	struct sfc_rxq_info *rxq_info;
1316 
1317 	rxq_info = sfc_rxq_info_by_ethdev_qid(sas, sfc_ethdev_qid);
1318 
1319 	qinfo->mp = rxq_info->refill_mb_pool;
1320 	qinfo->conf.rx_free_thresh = rxq_info->refill_threshold;
1321 	qinfo->conf.rx_drop_en = 1;
1322 	qinfo->conf.rx_deferred_start = rxq_info->deferred_start;
1323 	qinfo->conf.offloads = dev->data->dev_conf.rxmode.offloads;
1324 	if (rxq_info->type_flags & EFX_RXQ_FLAG_SCATTER) {
1325 		qinfo->conf.offloads |= DEV_RX_OFFLOAD_SCATTER;
1326 		qinfo->scattered_rx = 1;
1327 	}
1328 	qinfo->nb_desc = rxq_info->entries;
1329 }
1330 
1331 /*
1332  * The function is used by the secondary process as well. It must not
1333  * use any process-local pointers from the adapter data.
1334  */
1335 static void
1336 sfc_tx_queue_info_get(struct rte_eth_dev *dev, uint16_t ethdev_qid,
1337 		      struct rte_eth_txq_info *qinfo)
1338 {
1339 	struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1340 	struct sfc_txq_info *txq_info;
1341 
1342 	SFC_ASSERT(ethdev_qid < sas->ethdev_txq_count);
1343 
1344 	txq_info = sfc_txq_info_by_ethdev_qid(sas, ethdev_qid);
1345 
1346 	memset(qinfo, 0, sizeof(*qinfo));
1347 
1348 	qinfo->conf.offloads = txq_info->offloads;
1349 	qinfo->conf.tx_free_thresh = txq_info->free_thresh;
1350 	qinfo->conf.tx_deferred_start = txq_info->deferred_start;
1351 	qinfo->nb_desc = txq_info->entries;
1352 }
1353 
1354 /*
1355  * The function is used by the secondary process as well. It must not
1356  * use any process-local pointers from the adapter data.
1357  */
1358 static uint32_t
1359 sfc_rx_queue_count(struct rte_eth_dev *dev, uint16_t ethdev_qid)
1360 {
1361 	const struct sfc_adapter_priv *sap = sfc_adapter_priv_by_eth_dev(dev);
1362 	struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1363 	sfc_ethdev_qid_t sfc_ethdev_qid = ethdev_qid;
1364 	struct sfc_rxq_info *rxq_info;
1365 
1366 	rxq_info = sfc_rxq_info_by_ethdev_qid(sas, sfc_ethdev_qid);
1367 
1368 	if ((rxq_info->state & SFC_RXQ_STARTED) == 0)
1369 		return 0;
1370 
1371 	return sap->dp_rx->qdesc_npending(rxq_info->dp);
1372 }
1373 
1374 /*
1375  * The function is used by the secondary process as well. It must not
1376  * use any process-local pointers from the adapter data.
1377  */
1378 static int
1379 sfc_rx_descriptor_status(void *queue, uint16_t offset)
1380 {
1381 	struct sfc_dp_rxq *dp_rxq = queue;
1382 	const struct sfc_dp_rx *dp_rx;
1383 
1384 	dp_rx = sfc_dp_rx_by_dp_rxq(dp_rxq);
1385 
1386 	return dp_rx->qdesc_status(dp_rxq, offset);
1387 }
1388 
1389 /*
1390  * The function is used by the secondary process as well. It must not
1391  * use any process-local pointers from the adapter data.
1392  */
1393 static int
1394 sfc_tx_descriptor_status(void *queue, uint16_t offset)
1395 {
1396 	struct sfc_dp_txq *dp_txq = queue;
1397 	const struct sfc_dp_tx *dp_tx;
1398 
1399 	dp_tx = sfc_dp_tx_by_dp_txq(dp_txq);
1400 
1401 	return dp_tx->qdesc_status(dp_txq, offset);
1402 }
1403 
1404 static int
1405 sfc_rx_queue_start(struct rte_eth_dev *dev, uint16_t ethdev_qid)
1406 {
1407 	struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1408 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1409 	sfc_ethdev_qid_t sfc_ethdev_qid = ethdev_qid;
1410 	struct sfc_rxq_info *rxq_info;
1411 	sfc_sw_index_t sw_index;
1412 	int rc;
1413 
1414 	sfc_log_init(sa, "RxQ=%u", ethdev_qid);
1415 
1416 	sfc_adapter_lock(sa);
1417 
1418 	rc = EINVAL;
1419 	if (sa->state != SFC_ETHDEV_STARTED)
1420 		goto fail_not_started;
1421 
1422 	rxq_info = sfc_rxq_info_by_ethdev_qid(sas, sfc_ethdev_qid);
1423 	if (rxq_info->state != SFC_RXQ_INITIALIZED)
1424 		goto fail_not_setup;
1425 
1426 	sw_index = sfc_rxq_sw_index_by_ethdev_rx_qid(sas, sfc_ethdev_qid);
1427 	rc = sfc_rx_qstart(sa, sw_index);
1428 	if (rc != 0)
1429 		goto fail_rx_qstart;
1430 
1431 	rxq_info->deferred_started = B_TRUE;
1432 
1433 	sfc_adapter_unlock(sa);
1434 
1435 	return 0;
1436 
1437 fail_rx_qstart:
1438 fail_not_setup:
1439 fail_not_started:
1440 	sfc_adapter_unlock(sa);
1441 	SFC_ASSERT(rc > 0);
1442 	return -rc;
1443 }
1444 
1445 static int
1446 sfc_rx_queue_stop(struct rte_eth_dev *dev, uint16_t ethdev_qid)
1447 {
1448 	struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1449 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1450 	sfc_ethdev_qid_t sfc_ethdev_qid = ethdev_qid;
1451 	struct sfc_rxq_info *rxq_info;
1452 	sfc_sw_index_t sw_index;
1453 
1454 	sfc_log_init(sa, "RxQ=%u", ethdev_qid);
1455 
1456 	sfc_adapter_lock(sa);
1457 
1458 	sw_index = sfc_rxq_sw_index_by_ethdev_rx_qid(sas, sfc_ethdev_qid);
1459 	sfc_rx_qstop(sa, sw_index);
1460 
1461 	rxq_info = sfc_rxq_info_by_ethdev_qid(sas, sfc_ethdev_qid);
1462 	rxq_info->deferred_started = B_FALSE;
1463 
1464 	sfc_adapter_unlock(sa);
1465 
1466 	return 0;
1467 }
1468 
1469 static int
1470 sfc_tx_queue_start(struct rte_eth_dev *dev, uint16_t ethdev_qid)
1471 {
1472 	struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1473 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1474 	struct sfc_txq_info *txq_info;
1475 	sfc_sw_index_t sw_index;
1476 	int rc;
1477 
1478 	sfc_log_init(sa, "TxQ = %u", ethdev_qid);
1479 
1480 	sfc_adapter_lock(sa);
1481 
1482 	rc = EINVAL;
1483 	if (sa->state != SFC_ETHDEV_STARTED)
1484 		goto fail_not_started;
1485 
1486 	txq_info = sfc_txq_info_by_ethdev_qid(sas, ethdev_qid);
1487 	if (txq_info->state != SFC_TXQ_INITIALIZED)
1488 		goto fail_not_setup;
1489 
1490 	sw_index = sfc_txq_sw_index_by_ethdev_tx_qid(sas, ethdev_qid);
1491 	rc = sfc_tx_qstart(sa, sw_index);
1492 	if (rc != 0)
1493 		goto fail_tx_qstart;
1494 
1495 	txq_info->deferred_started = B_TRUE;
1496 
1497 	sfc_adapter_unlock(sa);
1498 	return 0;
1499 
1500 fail_tx_qstart:
1501 
1502 fail_not_setup:
1503 fail_not_started:
1504 	sfc_adapter_unlock(sa);
1505 	SFC_ASSERT(rc > 0);
1506 	return -rc;
1507 }
1508 
1509 static int
1510 sfc_tx_queue_stop(struct rte_eth_dev *dev, uint16_t ethdev_qid)
1511 {
1512 	struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1513 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1514 	struct sfc_txq_info *txq_info;
1515 	sfc_sw_index_t sw_index;
1516 
1517 	sfc_log_init(sa, "TxQ = %u", ethdev_qid);
1518 
1519 	sfc_adapter_lock(sa);
1520 
1521 	sw_index = sfc_txq_sw_index_by_ethdev_tx_qid(sas, ethdev_qid);
1522 	sfc_tx_qstop(sa, sw_index);
1523 
1524 	txq_info = sfc_txq_info_by_ethdev_qid(sas, ethdev_qid);
1525 	txq_info->deferred_started = B_FALSE;
1526 
1527 	sfc_adapter_unlock(sa);
1528 	return 0;
1529 }
1530 
1531 static efx_tunnel_protocol_t
1532 sfc_tunnel_rte_type_to_efx_udp_proto(enum rte_eth_tunnel_type rte_type)
1533 {
1534 	switch (rte_type) {
1535 	case RTE_TUNNEL_TYPE_VXLAN:
1536 		return EFX_TUNNEL_PROTOCOL_VXLAN;
1537 	case RTE_TUNNEL_TYPE_GENEVE:
1538 		return EFX_TUNNEL_PROTOCOL_GENEVE;
1539 	default:
1540 		return EFX_TUNNEL_NPROTOS;
1541 	}
1542 }
1543 
1544 enum sfc_udp_tunnel_op_e {
1545 	SFC_UDP_TUNNEL_ADD_PORT,
1546 	SFC_UDP_TUNNEL_DEL_PORT,
1547 };
1548 
1549 static int
1550 sfc_dev_udp_tunnel_op(struct rte_eth_dev *dev,
1551 		      struct rte_eth_udp_tunnel *tunnel_udp,
1552 		      enum sfc_udp_tunnel_op_e op)
1553 {
1554 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1555 	efx_tunnel_protocol_t tunnel_proto;
1556 	int rc;
1557 
1558 	sfc_log_init(sa, "%s udp_port=%u prot_type=%u",
1559 		     (op == SFC_UDP_TUNNEL_ADD_PORT) ? "add" :
1560 		     (op == SFC_UDP_TUNNEL_DEL_PORT) ? "delete" : "unknown",
1561 		     tunnel_udp->udp_port, tunnel_udp->prot_type);
1562 
1563 	tunnel_proto =
1564 		sfc_tunnel_rte_type_to_efx_udp_proto(tunnel_udp->prot_type);
1565 	if (tunnel_proto >= EFX_TUNNEL_NPROTOS) {
1566 		rc = ENOTSUP;
1567 		goto fail_bad_proto;
1568 	}
1569 
1570 	sfc_adapter_lock(sa);
1571 
1572 	switch (op) {
1573 	case SFC_UDP_TUNNEL_ADD_PORT:
1574 		rc = efx_tunnel_config_udp_add(sa->nic,
1575 					       tunnel_udp->udp_port,
1576 					       tunnel_proto);
1577 		break;
1578 	case SFC_UDP_TUNNEL_DEL_PORT:
1579 		rc = efx_tunnel_config_udp_remove(sa->nic,
1580 						  tunnel_udp->udp_port,
1581 						  tunnel_proto);
1582 		break;
1583 	default:
1584 		rc = EINVAL;
1585 		goto fail_bad_op;
1586 	}
1587 
1588 	if (rc != 0)
1589 		goto fail_op;
1590 
1591 	if (sa->state == SFC_ETHDEV_STARTED) {
1592 		rc = efx_tunnel_reconfigure(sa->nic);
1593 		if (rc == EAGAIN) {
1594 			/*
1595 			 * Configuration is accepted by FW and MC reboot
1596 			 * is initiated to apply the changes. MC reboot
1597 			 * will be handled in a usual way (MC reboot
1598 			 * event on management event queue and adapter
1599 			 * restart).
1600 			 */
1601 			rc = 0;
1602 		} else if (rc != 0) {
1603 			goto fail_reconfigure;
1604 		}
1605 	}
1606 
1607 	sfc_adapter_unlock(sa);
1608 	return 0;
1609 
1610 fail_reconfigure:
1611 	/* Remove/restore entry since the change makes the trouble */
1612 	switch (op) {
1613 	case SFC_UDP_TUNNEL_ADD_PORT:
1614 		(void)efx_tunnel_config_udp_remove(sa->nic,
1615 						   tunnel_udp->udp_port,
1616 						   tunnel_proto);
1617 		break;
1618 	case SFC_UDP_TUNNEL_DEL_PORT:
1619 		(void)efx_tunnel_config_udp_add(sa->nic,
1620 						tunnel_udp->udp_port,
1621 						tunnel_proto);
1622 		break;
1623 	}
1624 
1625 fail_op:
1626 fail_bad_op:
1627 	sfc_adapter_unlock(sa);
1628 
1629 fail_bad_proto:
1630 	SFC_ASSERT(rc > 0);
1631 	return -rc;
1632 }
1633 
1634 static int
1635 sfc_dev_udp_tunnel_port_add(struct rte_eth_dev *dev,
1636 			    struct rte_eth_udp_tunnel *tunnel_udp)
1637 {
1638 	return sfc_dev_udp_tunnel_op(dev, tunnel_udp, SFC_UDP_TUNNEL_ADD_PORT);
1639 }
1640 
1641 static int
1642 sfc_dev_udp_tunnel_port_del(struct rte_eth_dev *dev,
1643 			    struct rte_eth_udp_tunnel *tunnel_udp)
1644 {
1645 	return sfc_dev_udp_tunnel_op(dev, tunnel_udp, SFC_UDP_TUNNEL_DEL_PORT);
1646 }
1647 
1648 /*
1649  * The function is used by the secondary process as well. It must not
1650  * use any process-local pointers from the adapter data.
1651  */
1652 static int
1653 sfc_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
1654 			  struct rte_eth_rss_conf *rss_conf)
1655 {
1656 	struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1657 	struct sfc_rss *rss = &sas->rss;
1658 
1659 	if (rss->context_type != EFX_RX_SCALE_EXCLUSIVE)
1660 		return -ENOTSUP;
1661 
1662 	/*
1663 	 * Mapping of hash configuration between RTE and EFX is not one-to-one,
1664 	 * hence, conversion is done here to derive a correct set of ETH_RSS
1665 	 * flags which corresponds to the active EFX configuration stored
1666 	 * locally in 'sfc_adapter' and kept up-to-date
1667 	 */
1668 	rss_conf->rss_hf = sfc_rx_hf_efx_to_rte(rss, rss->hash_types);
1669 	rss_conf->rss_key_len = EFX_RSS_KEY_SIZE;
1670 	if (rss_conf->rss_key != NULL)
1671 		rte_memcpy(rss_conf->rss_key, rss->key, EFX_RSS_KEY_SIZE);
1672 
1673 	return 0;
1674 }
1675 
1676 static int
1677 sfc_dev_rss_hash_update(struct rte_eth_dev *dev,
1678 			struct rte_eth_rss_conf *rss_conf)
1679 {
1680 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1681 	struct sfc_rss *rss = &sfc_sa2shared(sa)->rss;
1682 	unsigned int efx_hash_types;
1683 	uint32_t contexts[] = {EFX_RSS_CONTEXT_DEFAULT, rss->dummy_rss_context};
1684 	unsigned int n_contexts;
1685 	unsigned int mode_i = 0;
1686 	unsigned int key_i = 0;
1687 	unsigned int i = 0;
1688 	int rc = 0;
1689 
1690 	n_contexts = rss->dummy_rss_context == EFX_RSS_CONTEXT_DEFAULT ? 1 : 2;
1691 
1692 	if (sfc_sa2shared(sa)->isolated)
1693 		return -ENOTSUP;
1694 
1695 	if (rss->context_type != EFX_RX_SCALE_EXCLUSIVE) {
1696 		sfc_err(sa, "RSS is not available");
1697 		return -ENOTSUP;
1698 	}
1699 
1700 	if (rss->channels == 0) {
1701 		sfc_err(sa, "RSS is not configured");
1702 		return -EINVAL;
1703 	}
1704 
1705 	if ((rss_conf->rss_key != NULL) &&
1706 	    (rss_conf->rss_key_len != sizeof(rss->key))) {
1707 		sfc_err(sa, "RSS key size is wrong (should be %zu)",
1708 			sizeof(rss->key));
1709 		return -EINVAL;
1710 	}
1711 
1712 	sfc_adapter_lock(sa);
1713 
1714 	rc = sfc_rx_hf_rte_to_efx(sa, rss_conf->rss_hf, &efx_hash_types);
1715 	if (rc != 0)
1716 		goto fail_rx_hf_rte_to_efx;
1717 
1718 	for (mode_i = 0; mode_i < n_contexts; mode_i++) {
1719 		rc = efx_rx_scale_mode_set(sa->nic, contexts[mode_i],
1720 					   rss->hash_alg, efx_hash_types,
1721 					   B_TRUE);
1722 		if (rc != 0)
1723 			goto fail_scale_mode_set;
1724 	}
1725 
1726 	if (rss_conf->rss_key != NULL) {
1727 		if (sa->state == SFC_ETHDEV_STARTED) {
1728 			for (key_i = 0; key_i < n_contexts; key_i++) {
1729 				rc = efx_rx_scale_key_set(sa->nic,
1730 							  contexts[key_i],
1731 							  rss_conf->rss_key,
1732 							  sizeof(rss->key));
1733 				if (rc != 0)
1734 					goto fail_scale_key_set;
1735 			}
1736 		}
1737 
1738 		rte_memcpy(rss->key, rss_conf->rss_key, sizeof(rss->key));
1739 	}
1740 
1741 	rss->hash_types = efx_hash_types;
1742 
1743 	sfc_adapter_unlock(sa);
1744 
1745 	return 0;
1746 
1747 fail_scale_key_set:
1748 	for (i = 0; i < key_i; i++) {
1749 		if (efx_rx_scale_key_set(sa->nic, contexts[i], rss->key,
1750 					 sizeof(rss->key)) != 0)
1751 			sfc_err(sa, "failed to restore RSS key");
1752 	}
1753 
1754 fail_scale_mode_set:
1755 	for (i = 0; i < mode_i; i++) {
1756 		if (efx_rx_scale_mode_set(sa->nic, contexts[i],
1757 					  EFX_RX_HASHALG_TOEPLITZ,
1758 					  rss->hash_types, B_TRUE) != 0)
1759 			sfc_err(sa, "failed to restore RSS mode");
1760 	}
1761 
1762 fail_rx_hf_rte_to_efx:
1763 	sfc_adapter_unlock(sa);
1764 	return -rc;
1765 }
1766 
1767 /*
1768  * The function is used by the secondary process as well. It must not
1769  * use any process-local pointers from the adapter data.
1770  */
1771 static int
1772 sfc_dev_rss_reta_query(struct rte_eth_dev *dev,
1773 		       struct rte_eth_rss_reta_entry64 *reta_conf,
1774 		       uint16_t reta_size)
1775 {
1776 	struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1777 	struct sfc_rss *rss = &sas->rss;
1778 	int entry;
1779 
1780 	if (rss->context_type != EFX_RX_SCALE_EXCLUSIVE || sas->isolated)
1781 		return -ENOTSUP;
1782 
1783 	if (rss->channels == 0)
1784 		return -EINVAL;
1785 
1786 	if (reta_size != EFX_RSS_TBL_SIZE)
1787 		return -EINVAL;
1788 
1789 	for (entry = 0; entry < reta_size; entry++) {
1790 		int grp = entry / RTE_RETA_GROUP_SIZE;
1791 		int grp_idx = entry % RTE_RETA_GROUP_SIZE;
1792 
1793 		if ((reta_conf[grp].mask >> grp_idx) & 1)
1794 			reta_conf[grp].reta[grp_idx] = rss->tbl[entry];
1795 	}
1796 
1797 	return 0;
1798 }
1799 
1800 static int
1801 sfc_dev_rss_reta_update(struct rte_eth_dev *dev,
1802 			struct rte_eth_rss_reta_entry64 *reta_conf,
1803 			uint16_t reta_size)
1804 {
1805 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
1806 	struct sfc_rss *rss = &sfc_sa2shared(sa)->rss;
1807 	unsigned int *rss_tbl_new;
1808 	uint16_t entry;
1809 	int rc = 0;
1810 
1811 
1812 	if (sfc_sa2shared(sa)->isolated)
1813 		return -ENOTSUP;
1814 
1815 	if (rss->context_type != EFX_RX_SCALE_EXCLUSIVE) {
1816 		sfc_err(sa, "RSS is not available");
1817 		return -ENOTSUP;
1818 	}
1819 
1820 	if (rss->channels == 0) {
1821 		sfc_err(sa, "RSS is not configured");
1822 		return -EINVAL;
1823 	}
1824 
1825 	if (reta_size != EFX_RSS_TBL_SIZE) {
1826 		sfc_err(sa, "RETA size is wrong (should be %u)",
1827 			EFX_RSS_TBL_SIZE);
1828 		return -EINVAL;
1829 	}
1830 
1831 	rss_tbl_new = rte_zmalloc("rss_tbl_new", sizeof(rss->tbl), 0);
1832 	if (rss_tbl_new == NULL)
1833 		return -ENOMEM;
1834 
1835 	sfc_adapter_lock(sa);
1836 
1837 	rte_memcpy(rss_tbl_new, rss->tbl, sizeof(rss->tbl));
1838 
1839 	for (entry = 0; entry < reta_size; entry++) {
1840 		int grp_idx = entry % RTE_RETA_GROUP_SIZE;
1841 		struct rte_eth_rss_reta_entry64 *grp;
1842 
1843 		grp = &reta_conf[entry / RTE_RETA_GROUP_SIZE];
1844 
1845 		if (grp->mask & (1ull << grp_idx)) {
1846 			if (grp->reta[grp_idx] >= rss->channels) {
1847 				rc = EINVAL;
1848 				goto bad_reta_entry;
1849 			}
1850 			rss_tbl_new[entry] = grp->reta[grp_idx];
1851 		}
1852 	}
1853 
1854 	if (sa->state == SFC_ETHDEV_STARTED) {
1855 		rc = efx_rx_scale_tbl_set(sa->nic, EFX_RSS_CONTEXT_DEFAULT,
1856 					  rss_tbl_new, EFX_RSS_TBL_SIZE);
1857 		if (rc != 0)
1858 			goto fail_scale_tbl_set;
1859 	}
1860 
1861 	rte_memcpy(rss->tbl, rss_tbl_new, sizeof(rss->tbl));
1862 
1863 fail_scale_tbl_set:
1864 bad_reta_entry:
1865 	sfc_adapter_unlock(sa);
1866 
1867 	rte_free(rss_tbl_new);
1868 
1869 	SFC_ASSERT(rc >= 0);
1870 	return -rc;
1871 }
1872 
1873 static int
1874 sfc_dev_flow_ops_get(struct rte_eth_dev *dev __rte_unused,
1875 		     const struct rte_flow_ops **ops)
1876 {
1877 	*ops = &sfc_flow_ops;
1878 	return 0;
1879 }
1880 
1881 static int
1882 sfc_pool_ops_supported(struct rte_eth_dev *dev, const char *pool)
1883 {
1884 	const struct sfc_adapter_priv *sap = sfc_adapter_priv_by_eth_dev(dev);
1885 
1886 	/*
1887 	 * If Rx datapath does not provide callback to check mempool,
1888 	 * all pools are supported.
1889 	 */
1890 	if (sap->dp_rx->pool_ops_supported == NULL)
1891 		return 1;
1892 
1893 	return sap->dp_rx->pool_ops_supported(pool);
1894 }
1895 
1896 static int
1897 sfc_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t ethdev_qid)
1898 {
1899 	const struct sfc_adapter_priv *sap = sfc_adapter_priv_by_eth_dev(dev);
1900 	struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1901 	sfc_ethdev_qid_t sfc_ethdev_qid = ethdev_qid;
1902 	struct sfc_rxq_info *rxq_info;
1903 
1904 	rxq_info = sfc_rxq_info_by_ethdev_qid(sas, sfc_ethdev_qid);
1905 
1906 	return sap->dp_rx->intr_enable(rxq_info->dp);
1907 }
1908 
1909 static int
1910 sfc_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t ethdev_qid)
1911 {
1912 	const struct sfc_adapter_priv *sap = sfc_adapter_priv_by_eth_dev(dev);
1913 	struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
1914 	sfc_ethdev_qid_t sfc_ethdev_qid = ethdev_qid;
1915 	struct sfc_rxq_info *rxq_info;
1916 
1917 	rxq_info = sfc_rxq_info_by_ethdev_qid(sas, sfc_ethdev_qid);
1918 
1919 	return sap->dp_rx->intr_disable(rxq_info->dp);
1920 }
1921 
1922 struct sfc_mport_journal_ctx {
1923 	struct sfc_adapter		*sa;
1924 	uint16_t			switch_domain_id;
1925 	uint32_t			mcdi_handle;
1926 	bool				controllers_assigned;
1927 	efx_pcie_interface_t		*controllers;
1928 	size_t				nb_controllers;
1929 };
1930 
1931 static int
1932 sfc_journal_ctx_add_controller(struct sfc_mport_journal_ctx *ctx,
1933 			       efx_pcie_interface_t intf)
1934 {
1935 	efx_pcie_interface_t *new_controllers;
1936 	size_t i, target;
1937 	size_t new_size;
1938 
1939 	if (ctx->controllers == NULL) {
1940 		ctx->controllers = rte_malloc("sfc_controller_mapping",
1941 					      sizeof(ctx->controllers[0]), 0);
1942 		if (ctx->controllers == NULL)
1943 			return ENOMEM;
1944 
1945 		ctx->controllers[0] = intf;
1946 		ctx->nb_controllers = 1;
1947 
1948 		return 0;
1949 	}
1950 
1951 	for (i = 0; i < ctx->nb_controllers; i++) {
1952 		if (ctx->controllers[i] == intf)
1953 			return 0;
1954 		if (ctx->controllers[i] > intf)
1955 			break;
1956 	}
1957 	target = i;
1958 
1959 	ctx->nb_controllers += 1;
1960 	new_size = ctx->nb_controllers * sizeof(ctx->controllers[0]);
1961 
1962 	new_controllers = rte_realloc(ctx->controllers, new_size, 0);
1963 	if (new_controllers == NULL) {
1964 		rte_free(ctx->controllers);
1965 		return ENOMEM;
1966 	}
1967 	ctx->controllers = new_controllers;
1968 
1969 	for (i = target + 1; i < ctx->nb_controllers; i++)
1970 		ctx->controllers[i] = ctx->controllers[i - 1];
1971 
1972 	ctx->controllers[target] = intf;
1973 
1974 	return 0;
1975 }
1976 
1977 static efx_rc_t
1978 sfc_process_mport_journal_entry(struct sfc_mport_journal_ctx *ctx,
1979 				efx_mport_desc_t *mport)
1980 {
1981 	efx_mport_sel_t ethdev_mport;
1982 	int rc;
1983 
1984 	sfc_dbg(ctx->sa,
1985 		"processing mport id %u (controller %u pf %u vf %u)",
1986 		mport->emd_id.id, mport->emd_vnic.ev_intf,
1987 		mport->emd_vnic.ev_pf, mport->emd_vnic.ev_vf);
1988 	efx_mae_mport_invalid(&ethdev_mport);
1989 
1990 	if (!ctx->controllers_assigned) {
1991 		rc = sfc_journal_ctx_add_controller(ctx,
1992 						    mport->emd_vnic.ev_intf);
1993 		if (rc != 0)
1994 			return rc;
1995 	}
1996 
1997 	return 0;
1998 }
1999 
2000 static efx_rc_t
2001 sfc_process_mport_journal_cb(void *data, efx_mport_desc_t *mport,
2002 			     size_t mport_len)
2003 {
2004 	struct sfc_mport_journal_ctx *ctx = data;
2005 
2006 	if (ctx == NULL || ctx->sa == NULL) {
2007 		sfc_err(ctx->sa, "received NULL context or SFC adapter");
2008 		return EINVAL;
2009 	}
2010 
2011 	if (mport_len != sizeof(*mport)) {
2012 		sfc_err(ctx->sa, "actual and expected mport buffer sizes differ");
2013 		return EINVAL;
2014 	}
2015 
2016 	SFC_ASSERT(sfc_adapter_is_locked(ctx->sa));
2017 
2018 	/*
2019 	 * If a zombie flag is set, it means the mport has been marked for
2020 	 * deletion and cannot be used for any new operations. The mport will
2021 	 * be destroyed completely once all references to it are released.
2022 	 */
2023 	if (mport->emd_zombie) {
2024 		sfc_dbg(ctx->sa, "mport is a zombie, skipping");
2025 		return 0;
2026 	}
2027 	if (mport->emd_type != EFX_MPORT_TYPE_VNIC) {
2028 		sfc_dbg(ctx->sa, "mport is not a VNIC, skipping");
2029 		return 0;
2030 	}
2031 	if (mport->emd_vnic.ev_client_type != EFX_MPORT_VNIC_CLIENT_FUNCTION) {
2032 		sfc_dbg(ctx->sa, "mport is not a function, skipping");
2033 		return 0;
2034 	}
2035 	if (mport->emd_vnic.ev_handle == ctx->mcdi_handle) {
2036 		sfc_dbg(ctx->sa, "mport is this driver instance, skipping");
2037 		return 0;
2038 	}
2039 
2040 	return sfc_process_mport_journal_entry(ctx, mport);
2041 }
2042 
2043 static int
2044 sfc_process_mport_journal(struct sfc_adapter *sa)
2045 {
2046 	struct sfc_mport_journal_ctx ctx;
2047 	const efx_pcie_interface_t *controllers;
2048 	size_t nb_controllers;
2049 	efx_rc_t efx_rc;
2050 	int rc;
2051 
2052 	memset(&ctx, 0, sizeof(ctx));
2053 	ctx.sa = sa;
2054 	ctx.switch_domain_id = sa->mae.switch_domain_id;
2055 
2056 	efx_rc = efx_mcdi_get_own_client_handle(sa->nic, &ctx.mcdi_handle);
2057 	if (efx_rc != 0) {
2058 		sfc_err(sa, "failed to get own MCDI handle");
2059 		SFC_ASSERT(efx_rc > 0);
2060 		return efx_rc;
2061 	}
2062 
2063 	rc = sfc_mae_switch_domain_controllers(ctx.switch_domain_id,
2064 					       &controllers, &nb_controllers);
2065 	if (rc != 0) {
2066 		sfc_err(sa, "failed to get controller mapping");
2067 		return rc;
2068 	}
2069 
2070 	ctx.controllers_assigned = controllers != NULL;
2071 	ctx.controllers = NULL;
2072 	ctx.nb_controllers = 0;
2073 
2074 	efx_rc = efx_mae_read_mport_journal(sa->nic,
2075 					    sfc_process_mport_journal_cb, &ctx);
2076 	if (efx_rc != 0) {
2077 		sfc_err(sa, "failed to process MAE mport journal");
2078 		SFC_ASSERT(efx_rc > 0);
2079 		return efx_rc;
2080 	}
2081 
2082 	if (controllers == NULL) {
2083 		rc = sfc_mae_switch_domain_map_controllers(ctx.switch_domain_id,
2084 							   ctx.controllers,
2085 							   ctx.nb_controllers);
2086 		if (rc != 0)
2087 			return rc;
2088 	}
2089 
2090 	return 0;
2091 }
2092 
2093 static const struct eth_dev_ops sfc_eth_dev_ops = {
2094 	.dev_configure			= sfc_dev_configure,
2095 	.dev_start			= sfc_dev_start,
2096 	.dev_stop			= sfc_dev_stop,
2097 	.dev_set_link_up		= sfc_dev_set_link_up,
2098 	.dev_set_link_down		= sfc_dev_set_link_down,
2099 	.dev_close			= sfc_dev_close,
2100 	.promiscuous_enable		= sfc_dev_promisc_enable,
2101 	.promiscuous_disable		= sfc_dev_promisc_disable,
2102 	.allmulticast_enable		= sfc_dev_allmulti_enable,
2103 	.allmulticast_disable		= sfc_dev_allmulti_disable,
2104 	.link_update			= sfc_dev_link_update,
2105 	.stats_get			= sfc_stats_get,
2106 	.stats_reset			= sfc_stats_reset,
2107 	.xstats_get			= sfc_xstats_get,
2108 	.xstats_reset			= sfc_stats_reset,
2109 	.xstats_get_names		= sfc_xstats_get_names,
2110 	.dev_infos_get			= sfc_dev_infos_get,
2111 	.dev_supported_ptypes_get	= sfc_dev_supported_ptypes_get,
2112 	.mtu_set			= sfc_dev_set_mtu,
2113 	.rx_queue_start			= sfc_rx_queue_start,
2114 	.rx_queue_stop			= sfc_rx_queue_stop,
2115 	.tx_queue_start			= sfc_tx_queue_start,
2116 	.tx_queue_stop			= sfc_tx_queue_stop,
2117 	.rx_queue_setup			= sfc_rx_queue_setup,
2118 	.rx_queue_release		= sfc_rx_queue_release,
2119 	.rx_queue_intr_enable		= sfc_rx_queue_intr_enable,
2120 	.rx_queue_intr_disable		= sfc_rx_queue_intr_disable,
2121 	.tx_queue_setup			= sfc_tx_queue_setup,
2122 	.tx_queue_release		= sfc_tx_queue_release,
2123 	.flow_ctrl_get			= sfc_flow_ctrl_get,
2124 	.flow_ctrl_set			= sfc_flow_ctrl_set,
2125 	.mac_addr_set			= sfc_mac_addr_set,
2126 	.udp_tunnel_port_add		= sfc_dev_udp_tunnel_port_add,
2127 	.udp_tunnel_port_del		= sfc_dev_udp_tunnel_port_del,
2128 	.reta_update			= sfc_dev_rss_reta_update,
2129 	.reta_query			= sfc_dev_rss_reta_query,
2130 	.rss_hash_update		= sfc_dev_rss_hash_update,
2131 	.rss_hash_conf_get		= sfc_dev_rss_hash_conf_get,
2132 	.flow_ops_get			= sfc_dev_flow_ops_get,
2133 	.set_mc_addr_list		= sfc_set_mc_addr_list,
2134 	.rxq_info_get			= sfc_rx_queue_info_get,
2135 	.txq_info_get			= sfc_tx_queue_info_get,
2136 	.fw_version_get			= sfc_fw_version_get,
2137 	.xstats_get_by_id		= sfc_xstats_get_by_id,
2138 	.xstats_get_names_by_id		= sfc_xstats_get_names_by_id,
2139 	.pool_ops_supported		= sfc_pool_ops_supported,
2140 };
2141 
2142 struct sfc_ethdev_init_data {
2143 	uint16_t		nb_representors;
2144 };
2145 
2146 /**
2147  * Duplicate a string in potentially shared memory required for
2148  * multi-process support.
2149  *
2150  * strdup() allocates from process-local heap/memory.
2151  */
2152 static char *
2153 sfc_strdup(const char *str)
2154 {
2155 	size_t size;
2156 	char *copy;
2157 
2158 	if (str == NULL)
2159 		return NULL;
2160 
2161 	size = strlen(str) + 1;
2162 	copy = rte_malloc(__func__, size, 0);
2163 	if (copy != NULL)
2164 		rte_memcpy(copy, str, size);
2165 
2166 	return copy;
2167 }
2168 
2169 static int
2170 sfc_eth_dev_set_ops(struct rte_eth_dev *dev)
2171 {
2172 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
2173 	struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
2174 	const struct sfc_dp_rx *dp_rx;
2175 	const struct sfc_dp_tx *dp_tx;
2176 	const efx_nic_cfg_t *encp;
2177 	unsigned int avail_caps = 0;
2178 	const char *rx_name = NULL;
2179 	const char *tx_name = NULL;
2180 	int rc;
2181 
2182 	switch (sa->family) {
2183 	case EFX_FAMILY_HUNTINGTON:
2184 	case EFX_FAMILY_MEDFORD:
2185 	case EFX_FAMILY_MEDFORD2:
2186 		avail_caps |= SFC_DP_HW_FW_CAP_EF10;
2187 		avail_caps |= SFC_DP_HW_FW_CAP_RX_EFX;
2188 		avail_caps |= SFC_DP_HW_FW_CAP_TX_EFX;
2189 		break;
2190 	case EFX_FAMILY_RIVERHEAD:
2191 		avail_caps |= SFC_DP_HW_FW_CAP_EF100;
2192 		break;
2193 	default:
2194 		break;
2195 	}
2196 
2197 	encp = efx_nic_cfg_get(sa->nic);
2198 	if (encp->enc_rx_es_super_buffer_supported)
2199 		avail_caps |= SFC_DP_HW_FW_CAP_RX_ES_SUPER_BUFFER;
2200 
2201 	rc = sfc_kvargs_process(sa, SFC_KVARG_RX_DATAPATH,
2202 				sfc_kvarg_string_handler, &rx_name);
2203 	if (rc != 0)
2204 		goto fail_kvarg_rx_datapath;
2205 
2206 	if (rx_name != NULL) {
2207 		dp_rx = sfc_dp_find_rx_by_name(&sfc_dp_head, rx_name);
2208 		if (dp_rx == NULL) {
2209 			sfc_err(sa, "Rx datapath %s not found", rx_name);
2210 			rc = ENOENT;
2211 			goto fail_dp_rx;
2212 		}
2213 		if (!sfc_dp_match_hw_fw_caps(&dp_rx->dp, avail_caps)) {
2214 			sfc_err(sa,
2215 				"Insufficient Hw/FW capabilities to use Rx datapath %s",
2216 				rx_name);
2217 			rc = EINVAL;
2218 			goto fail_dp_rx_caps;
2219 		}
2220 	} else {
2221 		dp_rx = sfc_dp_find_rx_by_caps(&sfc_dp_head, avail_caps);
2222 		if (dp_rx == NULL) {
2223 			sfc_err(sa, "Rx datapath by caps %#x not found",
2224 				avail_caps);
2225 			rc = ENOENT;
2226 			goto fail_dp_rx;
2227 		}
2228 	}
2229 
2230 	sas->dp_rx_name = sfc_strdup(dp_rx->dp.name);
2231 	if (sas->dp_rx_name == NULL) {
2232 		rc = ENOMEM;
2233 		goto fail_dp_rx_name;
2234 	}
2235 
2236 	sfc_notice(sa, "use %s Rx datapath", sas->dp_rx_name);
2237 
2238 	rc = sfc_kvargs_process(sa, SFC_KVARG_TX_DATAPATH,
2239 				sfc_kvarg_string_handler, &tx_name);
2240 	if (rc != 0)
2241 		goto fail_kvarg_tx_datapath;
2242 
2243 	if (tx_name != NULL) {
2244 		dp_tx = sfc_dp_find_tx_by_name(&sfc_dp_head, tx_name);
2245 		if (dp_tx == NULL) {
2246 			sfc_err(sa, "Tx datapath %s not found", tx_name);
2247 			rc = ENOENT;
2248 			goto fail_dp_tx;
2249 		}
2250 		if (!sfc_dp_match_hw_fw_caps(&dp_tx->dp, avail_caps)) {
2251 			sfc_err(sa,
2252 				"Insufficient Hw/FW capabilities to use Tx datapath %s",
2253 				tx_name);
2254 			rc = EINVAL;
2255 			goto fail_dp_tx_caps;
2256 		}
2257 	} else {
2258 		dp_tx = sfc_dp_find_tx_by_caps(&sfc_dp_head, avail_caps);
2259 		if (dp_tx == NULL) {
2260 			sfc_err(sa, "Tx datapath by caps %#x not found",
2261 				avail_caps);
2262 			rc = ENOENT;
2263 			goto fail_dp_tx;
2264 		}
2265 	}
2266 
2267 	sas->dp_tx_name = sfc_strdup(dp_tx->dp.name);
2268 	if (sas->dp_tx_name == NULL) {
2269 		rc = ENOMEM;
2270 		goto fail_dp_tx_name;
2271 	}
2272 
2273 	sfc_notice(sa, "use %s Tx datapath", sas->dp_tx_name);
2274 
2275 	sa->priv.dp_rx = dp_rx;
2276 	sa->priv.dp_tx = dp_tx;
2277 
2278 	dev->rx_pkt_burst = dp_rx->pkt_burst;
2279 	dev->tx_pkt_prepare = dp_tx->pkt_prepare;
2280 	dev->tx_pkt_burst = dp_tx->pkt_burst;
2281 
2282 	dev->rx_queue_count = sfc_rx_queue_count;
2283 	dev->rx_descriptor_status = sfc_rx_descriptor_status;
2284 	dev->tx_descriptor_status = sfc_tx_descriptor_status;
2285 	dev->dev_ops = &sfc_eth_dev_ops;
2286 
2287 	return 0;
2288 
2289 fail_dp_tx_name:
2290 fail_dp_tx_caps:
2291 fail_dp_tx:
2292 fail_kvarg_tx_datapath:
2293 	rte_free(sas->dp_rx_name);
2294 	sas->dp_rx_name = NULL;
2295 
2296 fail_dp_rx_name:
2297 fail_dp_rx_caps:
2298 fail_dp_rx:
2299 fail_kvarg_rx_datapath:
2300 	return rc;
2301 }
2302 
2303 static void
2304 sfc_eth_dev_clear_ops(struct rte_eth_dev *dev)
2305 {
2306 	struct sfc_adapter *sa = sfc_adapter_by_eth_dev(dev);
2307 	struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
2308 
2309 	dev->dev_ops = NULL;
2310 	dev->tx_pkt_prepare = NULL;
2311 	dev->rx_pkt_burst = NULL;
2312 	dev->tx_pkt_burst = NULL;
2313 
2314 	rte_free(sas->dp_tx_name);
2315 	sas->dp_tx_name = NULL;
2316 	sa->priv.dp_tx = NULL;
2317 
2318 	rte_free(sas->dp_rx_name);
2319 	sas->dp_rx_name = NULL;
2320 	sa->priv.dp_rx = NULL;
2321 }
2322 
2323 static const struct eth_dev_ops sfc_eth_dev_secondary_ops = {
2324 	.dev_supported_ptypes_get	= sfc_dev_supported_ptypes_get,
2325 	.reta_query			= sfc_dev_rss_reta_query,
2326 	.rss_hash_conf_get		= sfc_dev_rss_hash_conf_get,
2327 	.rxq_info_get			= sfc_rx_queue_info_get,
2328 	.txq_info_get			= sfc_tx_queue_info_get,
2329 };
2330 
2331 static int
2332 sfc_eth_dev_secondary_init(struct rte_eth_dev *dev, uint32_t logtype_main)
2333 {
2334 	struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
2335 	struct sfc_adapter_priv *sap;
2336 	const struct sfc_dp_rx *dp_rx;
2337 	const struct sfc_dp_tx *dp_tx;
2338 	int rc;
2339 
2340 	/*
2341 	 * Allocate process private data from heap, since it should not
2342 	 * be located in shared memory allocated using rte_malloc() API.
2343 	 */
2344 	sap = calloc(1, sizeof(*sap));
2345 	if (sap == NULL) {
2346 		rc = ENOMEM;
2347 		goto fail_alloc_priv;
2348 	}
2349 
2350 	sap->logtype_main = logtype_main;
2351 
2352 	dp_rx = sfc_dp_find_rx_by_name(&sfc_dp_head, sas->dp_rx_name);
2353 	if (dp_rx == NULL) {
2354 		SFC_LOG(sas, RTE_LOG_ERR, logtype_main,
2355 			"cannot find %s Rx datapath", sas->dp_rx_name);
2356 		rc = ENOENT;
2357 		goto fail_dp_rx;
2358 	}
2359 	if (~dp_rx->features & SFC_DP_RX_FEAT_MULTI_PROCESS) {
2360 		SFC_LOG(sas, RTE_LOG_ERR, logtype_main,
2361 			"%s Rx datapath does not support multi-process",
2362 			sas->dp_rx_name);
2363 		rc = EINVAL;
2364 		goto fail_dp_rx_multi_process;
2365 	}
2366 
2367 	dp_tx = sfc_dp_find_tx_by_name(&sfc_dp_head, sas->dp_tx_name);
2368 	if (dp_tx == NULL) {
2369 		SFC_LOG(sas, RTE_LOG_ERR, logtype_main,
2370 			"cannot find %s Tx datapath", sas->dp_tx_name);
2371 		rc = ENOENT;
2372 		goto fail_dp_tx;
2373 	}
2374 	if (~dp_tx->features & SFC_DP_TX_FEAT_MULTI_PROCESS) {
2375 		SFC_LOG(sas, RTE_LOG_ERR, logtype_main,
2376 			"%s Tx datapath does not support multi-process",
2377 			sas->dp_tx_name);
2378 		rc = EINVAL;
2379 		goto fail_dp_tx_multi_process;
2380 	}
2381 
2382 	sap->dp_rx = dp_rx;
2383 	sap->dp_tx = dp_tx;
2384 
2385 	dev->process_private = sap;
2386 	dev->rx_pkt_burst = dp_rx->pkt_burst;
2387 	dev->tx_pkt_prepare = dp_tx->pkt_prepare;
2388 	dev->tx_pkt_burst = dp_tx->pkt_burst;
2389 	dev->rx_queue_count = sfc_rx_queue_count;
2390 	dev->rx_descriptor_status = sfc_rx_descriptor_status;
2391 	dev->tx_descriptor_status = sfc_tx_descriptor_status;
2392 	dev->dev_ops = &sfc_eth_dev_secondary_ops;
2393 
2394 	return 0;
2395 
2396 fail_dp_tx_multi_process:
2397 fail_dp_tx:
2398 fail_dp_rx_multi_process:
2399 fail_dp_rx:
2400 	free(sap);
2401 
2402 fail_alloc_priv:
2403 	return rc;
2404 }
2405 
2406 static void
2407 sfc_register_dp(void)
2408 {
2409 	/* Register once */
2410 	if (TAILQ_EMPTY(&sfc_dp_head)) {
2411 		/* Prefer EF10 datapath */
2412 		sfc_dp_register(&sfc_dp_head, &sfc_ef100_rx.dp);
2413 		sfc_dp_register(&sfc_dp_head, &sfc_ef10_essb_rx.dp);
2414 		sfc_dp_register(&sfc_dp_head, &sfc_ef10_rx.dp);
2415 		sfc_dp_register(&sfc_dp_head, &sfc_efx_rx.dp);
2416 
2417 		sfc_dp_register(&sfc_dp_head, &sfc_ef100_tx.dp);
2418 		sfc_dp_register(&sfc_dp_head, &sfc_ef10_tx.dp);
2419 		sfc_dp_register(&sfc_dp_head, &sfc_efx_tx.dp);
2420 		sfc_dp_register(&sfc_dp_head, &sfc_ef10_simple_tx.dp);
2421 	}
2422 }
2423 
2424 static int
2425 sfc_parse_switch_mode(struct sfc_adapter *sa, bool has_representors)
2426 {
2427 	const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
2428 	const char *switch_mode = NULL;
2429 	int rc;
2430 
2431 	sfc_log_init(sa, "entry");
2432 
2433 	rc = sfc_kvargs_process(sa, SFC_KVARG_SWITCH_MODE,
2434 				sfc_kvarg_string_handler, &switch_mode);
2435 	if (rc != 0)
2436 		goto fail_kvargs;
2437 
2438 	if (switch_mode == NULL) {
2439 		sa->switchdev = encp->enc_mae_supported &&
2440 				(!encp->enc_datapath_cap_evb ||
2441 				 has_representors);
2442 	} else if (strcasecmp(switch_mode, SFC_KVARG_SWITCH_MODE_LEGACY) == 0) {
2443 		sa->switchdev = false;
2444 	} else if (strcasecmp(switch_mode,
2445 			      SFC_KVARG_SWITCH_MODE_SWITCHDEV) == 0) {
2446 		sa->switchdev = true;
2447 	} else {
2448 		sfc_err(sa, "invalid switch mode device argument '%s'",
2449 			switch_mode);
2450 		rc = EINVAL;
2451 		goto fail_mode;
2452 	}
2453 
2454 	sfc_log_init(sa, "done");
2455 
2456 	return 0;
2457 
2458 fail_mode:
2459 fail_kvargs:
2460 	sfc_log_init(sa, "failed: %s", rte_strerror(rc));
2461 
2462 	return rc;
2463 }
2464 
2465 static int
2466 sfc_eth_dev_init(struct rte_eth_dev *dev, void *init_params)
2467 {
2468 	struct sfc_adapter_shared *sas = sfc_adapter_shared_by_eth_dev(dev);
2469 	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
2470 	struct sfc_ethdev_init_data *init_data = init_params;
2471 	uint32_t logtype_main;
2472 	struct sfc_adapter *sa;
2473 	int rc;
2474 	const efx_nic_cfg_t *encp;
2475 	const struct rte_ether_addr *from;
2476 	int ret;
2477 
2478 	if (sfc_efx_dev_class_get(pci_dev->device.devargs) !=
2479 			SFC_EFX_DEV_CLASS_NET) {
2480 		SFC_GENERIC_LOG(DEBUG,
2481 			"Incompatible device class: skip probing, should be probed by other sfc driver.");
2482 		return 1;
2483 	}
2484 
2485 	rc = sfc_dp_mport_register();
2486 	if (rc != 0)
2487 		return rc;
2488 
2489 	sfc_register_dp();
2490 
2491 	logtype_main = sfc_register_logtype(&pci_dev->addr,
2492 					    SFC_LOGTYPE_MAIN_STR,
2493 					    RTE_LOG_NOTICE);
2494 
2495 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2496 		return -sfc_eth_dev_secondary_init(dev, logtype_main);
2497 
2498 	/* Required for logging */
2499 	ret = snprintf(sas->log_prefix, sizeof(sas->log_prefix),
2500 			"PMD: sfc_efx " PCI_PRI_FMT " #%" PRIu16 ": ",
2501 			pci_dev->addr.domain, pci_dev->addr.bus,
2502 			pci_dev->addr.devid, pci_dev->addr.function,
2503 			dev->data->port_id);
2504 	if (ret < 0 || ret >= (int)sizeof(sas->log_prefix)) {
2505 		SFC_GENERIC_LOG(ERR,
2506 			"reserved log prefix is too short for " PCI_PRI_FMT,
2507 			pci_dev->addr.domain, pci_dev->addr.bus,
2508 			pci_dev->addr.devid, pci_dev->addr.function);
2509 		return -EINVAL;
2510 	}
2511 	sas->pci_addr = pci_dev->addr;
2512 	sas->port_id = dev->data->port_id;
2513 
2514 	/*
2515 	 * Allocate process private data from heap, since it should not
2516 	 * be located in shared memory allocated using rte_malloc() API.
2517 	 */
2518 	sa = calloc(1, sizeof(*sa));
2519 	if (sa == NULL) {
2520 		rc = ENOMEM;
2521 		goto fail_alloc_sa;
2522 	}
2523 
2524 	dev->process_private = sa;
2525 
2526 	/* Required for logging */
2527 	sa->priv.shared = sas;
2528 	sa->priv.logtype_main = logtype_main;
2529 
2530 	sa->eth_dev = dev;
2531 
2532 	/* Copy PCI device info to the dev->data */
2533 	rte_eth_copy_pci_info(dev, pci_dev);
2534 	dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
2535 	dev->data->dev_flags |= RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE;
2536 
2537 	rc = sfc_kvargs_parse(sa);
2538 	if (rc != 0)
2539 		goto fail_kvargs_parse;
2540 
2541 	sfc_log_init(sa, "entry");
2542 
2543 	dev->data->mac_addrs = rte_zmalloc("sfc", RTE_ETHER_ADDR_LEN, 0);
2544 	if (dev->data->mac_addrs == NULL) {
2545 		rc = ENOMEM;
2546 		goto fail_mac_addrs;
2547 	}
2548 
2549 	sfc_adapter_lock_init(sa);
2550 	sfc_adapter_lock(sa);
2551 
2552 	sfc_log_init(sa, "probing");
2553 	rc = sfc_probe(sa);
2554 	if (rc != 0)
2555 		goto fail_probe;
2556 
2557 	/*
2558 	 * Selecting a default switch mode requires the NIC to be probed and
2559 	 * to have its capabilities filled in.
2560 	 */
2561 	rc = sfc_parse_switch_mode(sa, init_data->nb_representors > 0);
2562 	if (rc != 0)
2563 		goto fail_switch_mode;
2564 
2565 	sfc_log_init(sa, "set device ops");
2566 	rc = sfc_eth_dev_set_ops(dev);
2567 	if (rc != 0)
2568 		goto fail_set_ops;
2569 
2570 	sfc_log_init(sa, "attaching");
2571 	rc = sfc_attach(sa);
2572 	if (rc != 0)
2573 		goto fail_attach;
2574 
2575 	if (sa->switchdev && sa->mae.status != SFC_MAE_STATUS_SUPPORTED) {
2576 		sfc_err(sa,
2577 			"failed to enable switchdev mode without MAE support");
2578 		rc = ENOTSUP;
2579 		goto fail_switchdev_no_mae;
2580 	}
2581 
2582 	encp = efx_nic_cfg_get(sa->nic);
2583 
2584 	/*
2585 	 * The arguments are really reverse order in comparison to
2586 	 * Linux kernel. Copy from NIC config to Ethernet device data.
2587 	 */
2588 	from = (const struct rte_ether_addr *)(encp->enc_mac_addr);
2589 	rte_ether_addr_copy(from, &dev->data->mac_addrs[0]);
2590 
2591 	sfc_adapter_unlock(sa);
2592 
2593 	sfc_log_init(sa, "done");
2594 	return 0;
2595 
2596 fail_switchdev_no_mae:
2597 	sfc_detach(sa);
2598 
2599 fail_attach:
2600 	sfc_eth_dev_clear_ops(dev);
2601 
2602 fail_set_ops:
2603 fail_switch_mode:
2604 	sfc_unprobe(sa);
2605 
2606 fail_probe:
2607 	sfc_adapter_unlock(sa);
2608 	sfc_adapter_lock_fini(sa);
2609 	rte_free(dev->data->mac_addrs);
2610 	dev->data->mac_addrs = NULL;
2611 
2612 fail_mac_addrs:
2613 	sfc_kvargs_cleanup(sa);
2614 
2615 fail_kvargs_parse:
2616 	sfc_log_init(sa, "failed %d", rc);
2617 	dev->process_private = NULL;
2618 	free(sa);
2619 
2620 fail_alloc_sa:
2621 	SFC_ASSERT(rc > 0);
2622 	return -rc;
2623 }
2624 
2625 static int
2626 sfc_eth_dev_uninit(struct rte_eth_dev *dev)
2627 {
2628 	sfc_dev_close(dev);
2629 
2630 	return 0;
2631 }
2632 
2633 static const struct rte_pci_id pci_id_sfc_efx_map[] = {
2634 	{ RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_FARMINGDALE) },
2635 	{ RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_FARMINGDALE_VF) },
2636 	{ RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_GREENPORT) },
2637 	{ RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_GREENPORT_VF) },
2638 	{ RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_MEDFORD) },
2639 	{ RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_MEDFORD_VF) },
2640 	{ RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_MEDFORD2) },
2641 	{ RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_MEDFORD2_VF) },
2642 	{ RTE_PCI_DEVICE(EFX_PCI_VENID_XILINX, EFX_PCI_DEVID_RIVERHEAD) },
2643 	{ .vendor_id = 0 /* sentinel */ }
2644 };
2645 
2646 static int
2647 sfc_parse_rte_devargs(const char *args, struct rte_eth_devargs *devargs)
2648 {
2649 	struct rte_eth_devargs eth_da = { .nb_representor_ports = 0 };
2650 	int rc;
2651 
2652 	if (args != NULL) {
2653 		rc = rte_eth_devargs_parse(args, &eth_da);
2654 		if (rc != 0) {
2655 			SFC_GENERIC_LOG(ERR,
2656 					"Failed to parse generic devargs '%s'",
2657 					args);
2658 			return rc;
2659 		}
2660 	}
2661 
2662 	*devargs = eth_da;
2663 
2664 	return 0;
2665 }
2666 
2667 static int
2668 sfc_eth_dev_find_or_create(struct rte_pci_device *pci_dev,
2669 			   struct sfc_ethdev_init_data *init_data,
2670 			   struct rte_eth_dev **devp,
2671 			   bool *dev_created)
2672 {
2673 	struct rte_eth_dev *dev;
2674 	bool created = false;
2675 	int rc;
2676 
2677 	dev = rte_eth_dev_allocated(pci_dev->device.name);
2678 	if (dev == NULL) {
2679 		rc = rte_eth_dev_create(&pci_dev->device, pci_dev->device.name,
2680 					sizeof(struct sfc_adapter_shared),
2681 					eth_dev_pci_specific_init, pci_dev,
2682 					sfc_eth_dev_init, init_data);
2683 		if (rc != 0) {
2684 			SFC_GENERIC_LOG(ERR, "Failed to create sfc ethdev '%s'",
2685 					pci_dev->device.name);
2686 			return rc;
2687 		}
2688 
2689 		created = true;
2690 
2691 		dev = rte_eth_dev_allocated(pci_dev->device.name);
2692 		if (dev == NULL) {
2693 			SFC_GENERIC_LOG(ERR,
2694 				"Failed to find allocated sfc ethdev '%s'",
2695 				pci_dev->device.name);
2696 			return -ENODEV;
2697 		}
2698 	}
2699 
2700 	*devp = dev;
2701 	*dev_created = created;
2702 
2703 	return 0;
2704 }
2705 
2706 static int
2707 sfc_eth_dev_create_representors(struct rte_eth_dev *dev,
2708 				const struct rte_eth_devargs *eth_da)
2709 {
2710 	struct sfc_adapter *sa;
2711 	unsigned int i;
2712 	int rc;
2713 
2714 	if (eth_da->nb_representor_ports == 0)
2715 		return 0;
2716 
2717 	sa = sfc_adapter_by_eth_dev(dev);
2718 
2719 	if (!sa->switchdev) {
2720 		sfc_err(sa, "cannot create representors in non-switchdev mode");
2721 		return -EINVAL;
2722 	}
2723 
2724 	if (!sfc_repr_available(sfc_sa2shared(sa))) {
2725 		sfc_err(sa, "cannot create representors: unsupported");
2726 
2727 		return -ENOTSUP;
2728 	}
2729 
2730 	/*
2731 	 * This is needed to construct the DPDK controller -> EFX interface
2732 	 * mapping.
2733 	 */
2734 	sfc_adapter_lock(sa);
2735 	rc = sfc_process_mport_journal(sa);
2736 	sfc_adapter_unlock(sa);
2737 	if (rc != 0) {
2738 		SFC_ASSERT(rc > 0);
2739 		return -rc;
2740 	}
2741 
2742 	for (i = 0; i < eth_da->nb_representor_ports; ++i) {
2743 		const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
2744 		efx_mport_sel_t mport_sel;
2745 
2746 		rc = efx_mae_mport_by_pcie_function(encp->enc_pf,
2747 				eth_da->representor_ports[i], &mport_sel);
2748 		if (rc != 0) {
2749 			sfc_err(sa,
2750 				"failed to get representor %u m-port: %s - ignore",
2751 				eth_da->representor_ports[i],
2752 				rte_strerror(-rc));
2753 			continue;
2754 		}
2755 
2756 		rc = sfc_repr_create(dev, eth_da->representor_ports[i],
2757 				     sa->mae.switch_domain_id, &mport_sel);
2758 		if (rc != 0) {
2759 			sfc_err(sa, "cannot create representor %u: %s - ignore",
2760 				eth_da->representor_ports[i],
2761 				rte_strerror(-rc));
2762 		}
2763 	}
2764 
2765 	return 0;
2766 }
2767 
2768 static int sfc_eth_dev_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
2769 	struct rte_pci_device *pci_dev)
2770 {
2771 	struct sfc_ethdev_init_data init_data;
2772 	struct rte_eth_devargs eth_da;
2773 	struct rte_eth_dev *dev;
2774 	bool dev_created;
2775 	int rc;
2776 
2777 	if (pci_dev->device.devargs != NULL) {
2778 		rc = sfc_parse_rte_devargs(pci_dev->device.devargs->args,
2779 					   &eth_da);
2780 		if (rc != 0)
2781 			return rc;
2782 	} else {
2783 		memset(&eth_da, 0, sizeof(eth_da));
2784 	}
2785 
2786 	init_data.nb_representors = eth_da.nb_representor_ports;
2787 
2788 	if (eth_da.nb_representor_ports > 0 &&
2789 	    rte_eal_process_type() != RTE_PROC_PRIMARY) {
2790 		SFC_GENERIC_LOG(ERR,
2791 			"Create representors from secondary process not supported, dev '%s'",
2792 			pci_dev->device.name);
2793 		return -ENOTSUP;
2794 	}
2795 
2796 	/*
2797 	 * Driver supports RTE_PCI_DRV_PROBE_AGAIN. Hence create device only
2798 	 * if it does not already exist. Re-probing an existing device is
2799 	 * expected to allow additional representors to be configured.
2800 	 */
2801 	rc = sfc_eth_dev_find_or_create(pci_dev, &init_data, &dev,
2802 					&dev_created);
2803 	if (rc != 0)
2804 		return rc;
2805 
2806 	rc = sfc_eth_dev_create_representors(dev, &eth_da);
2807 	if (rc != 0) {
2808 		if (dev_created)
2809 			(void)rte_eth_dev_destroy(dev, sfc_eth_dev_uninit);
2810 
2811 		return rc;
2812 	}
2813 
2814 	return 0;
2815 }
2816 
2817 static int sfc_eth_dev_pci_remove(struct rte_pci_device *pci_dev)
2818 {
2819 	return rte_eth_dev_pci_generic_remove(pci_dev, sfc_eth_dev_uninit);
2820 }
2821 
2822 static struct rte_pci_driver sfc_efx_pmd = {
2823 	.id_table = pci_id_sfc_efx_map,
2824 	.drv_flags =
2825 		RTE_PCI_DRV_INTR_LSC |
2826 		RTE_PCI_DRV_NEED_MAPPING |
2827 		RTE_PCI_DRV_PROBE_AGAIN,
2828 	.probe = sfc_eth_dev_pci_probe,
2829 	.remove = sfc_eth_dev_pci_remove,
2830 };
2831 
2832 RTE_PMD_REGISTER_PCI(net_sfc_efx, sfc_efx_pmd);
2833 RTE_PMD_REGISTER_PCI_TABLE(net_sfc_efx, pci_id_sfc_efx_map);
2834 RTE_PMD_REGISTER_KMOD_DEP(net_sfc_efx, "* igb_uio | uio_pci_generic | vfio-pci");
2835 RTE_PMD_REGISTER_PARAM_STRING(net_sfc_efx,
2836 	SFC_KVARG_SWITCH_MODE "=" SFC_KVARG_VALUES_SWITCH_MODE " "
2837 	SFC_KVARG_RX_DATAPATH "=" SFC_KVARG_VALUES_RX_DATAPATH " "
2838 	SFC_KVARG_TX_DATAPATH "=" SFC_KVARG_VALUES_TX_DATAPATH " "
2839 	SFC_KVARG_PERF_PROFILE "=" SFC_KVARG_VALUES_PERF_PROFILE " "
2840 	SFC_KVARG_FW_VARIANT "=" SFC_KVARG_VALUES_FW_VARIANT " "
2841 	SFC_KVARG_RXD_WAIT_TIMEOUT_NS "=<long> "
2842 	SFC_KVARG_STATS_UPDATE_PERIOD_MS "=<long>");
2843 
2844 RTE_INIT(sfc_driver_register_logtype)
2845 {
2846 	int ret;
2847 
2848 	ret = rte_log_register_type_and_pick_level(SFC_LOGTYPE_PREFIX "driver",
2849 						   RTE_LOG_NOTICE);
2850 	sfc_logtype_driver = (ret < 0) ? RTE_LOGTYPE_PMD : ret;
2851 }
2852