xref: /dpdk/drivers/net/mlx5/mlx5.c (revision 60531a2c53f4d2b4b96ebb10ca813f62d0a5508d)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2015 Mellanox Technologies, Ltd
4  */
5 
6 #include <stddef.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 
14 #include <rte_malloc.h>
15 #include <ethdev_driver.h>
16 #include <rte_pci.h>
17 #include <bus_pci_driver.h>
18 #include <rte_common.h>
19 #include <rte_kvargs.h>
20 #include <rte_rwlock.h>
21 #include <rte_spinlock.h>
22 #include <rte_string_fns.h>
23 #include <rte_eal_paging.h>
24 #include <rte_alarm.h>
25 #include <rte_cycles.h>
26 #include <rte_interrupts.h>
27 
28 #include <mlx5_glue.h>
29 #include <mlx5_devx_cmds.h>
30 #include <mlx5_common.h>
31 #include <mlx5_common_os.h>
32 #include <mlx5_common_mp.h>
33 #include <mlx5_malloc.h>
34 
35 #include "mlx5_defs.h"
36 #include "mlx5.h"
37 #include "mlx5_utils.h"
38 #include "mlx5_rxtx.h"
39 #include "mlx5_rx.h"
40 #include "mlx5_tx.h"
41 #include "mlx5_autoconf.h"
42 #include "mlx5_flow.h"
43 #include "mlx5_flow_os.h"
44 #include "rte_pmd_mlx5.h"
45 
46 #define MLX5_ETH_DRIVER_NAME mlx5_eth
47 
48 /* Device parameter to enable RX completion queue compression. */
49 #define MLX5_RXQ_CQE_COMP_EN "rxq_cqe_comp_en"
50 
51 /* Device parameter to enable padding Rx packet to cacheline size. */
52 #define MLX5_RXQ_PKT_PAD_EN "rxq_pkt_pad_en"
53 
54 /* Device parameter to enable Multi-Packet Rx queue. */
55 #define MLX5_RX_MPRQ_EN "mprq_en"
56 
57 /* Device parameter to configure log 2 of the number of strides for MPRQ. */
58 #define MLX5_RX_MPRQ_LOG_STRIDE_NUM "mprq_log_stride_num"
59 
60 /* Device parameter to configure log 2 of the stride size for MPRQ. */
61 #define MLX5_RX_MPRQ_LOG_STRIDE_SIZE "mprq_log_stride_size"
62 
63 /* Device parameter to limit the size of memcpy'd packet for MPRQ. */
64 #define MLX5_RX_MPRQ_MAX_MEMCPY_LEN "mprq_max_memcpy_len"
65 
66 /* Device parameter to set the minimum number of Rx queues to enable MPRQ. */
67 #define MLX5_RXQS_MIN_MPRQ "rxqs_min_mprq"
68 
69 /* Device parameter to configure inline send. Deprecated, ignored.*/
70 #define MLX5_TXQ_INLINE "txq_inline"
71 
72 /* Device parameter to limit packet size to inline with ordinary SEND. */
73 #define MLX5_TXQ_INLINE_MAX "txq_inline_max"
74 
75 /* Device parameter to configure minimal data size to inline. */
76 #define MLX5_TXQ_INLINE_MIN "txq_inline_min"
77 
78 /* Device parameter to limit packet size to inline with Enhanced MPW. */
79 #define MLX5_TXQ_INLINE_MPW "txq_inline_mpw"
80 
81 /*
82  * Device parameter to configure the number of TX queues threshold for
83  * enabling inline send.
84  */
85 #define MLX5_TXQS_MIN_INLINE "txqs_min_inline"
86 
87 /*
88  * Device parameter to configure the number of TX queues threshold for
89  * enabling vectorized Tx, deprecated, ignored (no vectorized Tx routines).
90  */
91 #define MLX5_TXQS_MAX_VEC "txqs_max_vec"
92 
93 /* Device parameter to enable multi-packet send WQEs. */
94 #define MLX5_TXQ_MPW_EN "txq_mpw_en"
95 
96 /*
97  * Device parameter to include 2 dsegs in the title WQEBB.
98  * Deprecated, ignored.
99  */
100 #define MLX5_TXQ_MPW_HDR_DSEG_EN "txq_mpw_hdr_dseg_en"
101 
102 /*
103  * Device parameter to limit the size of inlining packet.
104  * Deprecated, ignored.
105  */
106 #define MLX5_TXQ_MAX_INLINE_LEN "txq_max_inline_len"
107 
108 /*
109  * Device parameter to enable Tx scheduling on timestamps
110  * and specify the packet pacing granularity in nanoseconds.
111  */
112 #define MLX5_TX_PP "tx_pp"
113 
114 /*
115  * Device parameter to specify skew in nanoseconds on Tx datapath,
116  * it represents the time between SQ start WQE processing and
117  * appearing actual packet data on the wire.
118  */
119 #define MLX5_TX_SKEW "tx_skew"
120 
121 /*
122  * Device parameter to enable hardware Tx vector.
123  * Deprecated, ignored (no vectorized Tx routines anymore).
124  */
125 #define MLX5_TX_VEC_EN "tx_vec_en"
126 
127 /* Device parameter to enable hardware Rx vector. */
128 #define MLX5_RX_VEC_EN "rx_vec_en"
129 
130 /* Allow L3 VXLAN flow creation. */
131 #define MLX5_L3_VXLAN_EN "l3_vxlan_en"
132 
133 /* Activate DV E-Switch flow steering. */
134 #define MLX5_DV_ESW_EN "dv_esw_en"
135 
136 /* Activate DV flow steering. */
137 #define MLX5_DV_FLOW_EN "dv_flow_en"
138 
139 /* Enable extensive flow metadata support. */
140 #define MLX5_DV_XMETA_EN "dv_xmeta_en"
141 
142 /* Device parameter to let the user manage the lacp traffic of bonding device */
143 #define MLX5_LACP_BY_USER "lacp_by_user"
144 
145 /* Activate Netlink support in VF mode. */
146 #define MLX5_VF_NL_EN "vf_nl_en"
147 
148 /* Select port representors to instantiate. */
149 #define MLX5_REPRESENTOR "representor"
150 
151 /* Device parameter to configure the maximum number of dump files per queue. */
152 #define MLX5_MAX_DUMP_FILES_NUM "max_dump_files_num"
153 
154 /* Configure timeout of LRO session (in microseconds). */
155 #define MLX5_LRO_TIMEOUT_USEC "lro_timeout_usec"
156 
157 /*
158  * Device parameter to configure the total data buffer size for a single
159  * hairpin queue (logarithm value).
160  */
161 #define MLX5_HP_BUF_SIZE "hp_buf_log_sz"
162 
163 /* Flow memory reclaim mode. */
164 #define MLX5_RECLAIM_MEM "reclaim_mem_mode"
165 
166 /* Decap will be used or not. */
167 #define MLX5_DECAP_EN "decap_en"
168 
169 /* Device parameter to configure allow or prevent duplicate rules pattern. */
170 #define MLX5_ALLOW_DUPLICATE_PATTERN "allow_duplicate_pattern"
171 
172 /* Device parameter to configure the delay drop when creating Rxqs. */
173 #define MLX5_DELAY_DROP "delay_drop"
174 
175 /* Device parameter to create the fdb default rule in PMD */
176 #define MLX5_FDB_DEFAULT_RULE_EN "fdb_def_rule_en"
177 
178 /* HW steering counter configuration. */
179 #define MLX5_HWS_CNT_SERVICE_CORE "service_core"
180 
181 /* HW steering counter's query interval. */
182 #define MLX5_HWS_CNT_CYCLE_TIME "svc_cycle_time"
183 
184 /* Device parameter to control representor matching in ingress/egress flows with HWS. */
185 #define MLX5_REPR_MATCHING_EN "repr_matching_en"
186 
187 /* Shared memory between primary and secondary processes. */
188 struct mlx5_shared_data *mlx5_shared_data;
189 
190 /** Driver-specific log messages type. */
191 int mlx5_logtype;
192 
193 static LIST_HEAD(, mlx5_dev_ctx_shared) mlx5_dev_ctx_list =
194 						LIST_HEAD_INITIALIZER();
195 static pthread_mutex_t mlx5_dev_ctx_list_mutex;
196 static const struct mlx5_indexed_pool_config mlx5_ipool_cfg[] = {
197 #if defined(HAVE_IBV_FLOW_DV_SUPPORT) || !defined(HAVE_INFINIBAND_VERBS_H)
198 	[MLX5_IPOOL_DECAP_ENCAP] = {
199 		.size = sizeof(struct mlx5_flow_dv_encap_decap_resource),
200 		.trunk_size = 64,
201 		.grow_trunk = 3,
202 		.grow_shift = 2,
203 		.need_lock = 1,
204 		.release_mem_en = 1,
205 		.malloc = mlx5_malloc,
206 		.free = mlx5_free,
207 		.type = "mlx5_encap_decap_ipool",
208 	},
209 	[MLX5_IPOOL_PUSH_VLAN] = {
210 		.size = sizeof(struct mlx5_flow_dv_push_vlan_action_resource),
211 		.trunk_size = 64,
212 		.grow_trunk = 3,
213 		.grow_shift = 2,
214 		.need_lock = 1,
215 		.release_mem_en = 1,
216 		.malloc = mlx5_malloc,
217 		.free = mlx5_free,
218 		.type = "mlx5_push_vlan_ipool",
219 	},
220 	[MLX5_IPOOL_TAG] = {
221 		.size = sizeof(struct mlx5_flow_dv_tag_resource),
222 		.trunk_size = 64,
223 		.grow_trunk = 3,
224 		.grow_shift = 2,
225 		.need_lock = 1,
226 		.release_mem_en = 0,
227 		.per_core_cache = (1 << 16),
228 		.malloc = mlx5_malloc,
229 		.free = mlx5_free,
230 		.type = "mlx5_tag_ipool",
231 	},
232 	[MLX5_IPOOL_PORT_ID] = {
233 		.size = sizeof(struct mlx5_flow_dv_port_id_action_resource),
234 		.trunk_size = 64,
235 		.grow_trunk = 3,
236 		.grow_shift = 2,
237 		.need_lock = 1,
238 		.release_mem_en = 1,
239 		.malloc = mlx5_malloc,
240 		.free = mlx5_free,
241 		.type = "mlx5_port_id_ipool",
242 	},
243 	[MLX5_IPOOL_JUMP] = {
244 		/*
245 		 * MLX5_IPOOL_JUMP ipool entry size depends on selected flow engine.
246 		 * When HW steering is enabled mlx5_flow_group struct is used.
247 		 * Otherwise mlx5_flow_tbl_data_entry struct is used.
248 		 */
249 		.size = 0,
250 		.trunk_size = 64,
251 		.grow_trunk = 3,
252 		.grow_shift = 2,
253 		.need_lock = 1,
254 		.release_mem_en = 1,
255 		.malloc = mlx5_malloc,
256 		.free = mlx5_free,
257 		.type = "mlx5_jump_ipool",
258 	},
259 	[MLX5_IPOOL_SAMPLE] = {
260 		.size = sizeof(struct mlx5_flow_dv_sample_resource),
261 		.trunk_size = 64,
262 		.grow_trunk = 3,
263 		.grow_shift = 2,
264 		.need_lock = 1,
265 		.release_mem_en = 1,
266 		.malloc = mlx5_malloc,
267 		.free = mlx5_free,
268 		.type = "mlx5_sample_ipool",
269 	},
270 	[MLX5_IPOOL_DEST_ARRAY] = {
271 		.size = sizeof(struct mlx5_flow_dv_dest_array_resource),
272 		.trunk_size = 64,
273 		.grow_trunk = 3,
274 		.grow_shift = 2,
275 		.need_lock = 1,
276 		.release_mem_en = 1,
277 		.malloc = mlx5_malloc,
278 		.free = mlx5_free,
279 		.type = "mlx5_dest_array_ipool",
280 	},
281 	[MLX5_IPOOL_TUNNEL_ID] = {
282 		.size = sizeof(struct mlx5_flow_tunnel),
283 		.trunk_size = MLX5_MAX_TUNNELS,
284 		.need_lock = 1,
285 		.release_mem_en = 1,
286 		.type = "mlx5_tunnel_offload",
287 	},
288 	[MLX5_IPOOL_TNL_TBL_ID] = {
289 		.size = 0,
290 		.need_lock = 1,
291 		.type = "mlx5_flow_tnl_tbl_ipool",
292 	},
293 #endif
294 	[MLX5_IPOOL_MTR] = {
295 		/**
296 		 * The ipool index should grow continually from small to big,
297 		 * for meter idx, so not set grow_trunk to avoid meter index
298 		 * not jump continually.
299 		 */
300 		.size = sizeof(struct mlx5_legacy_flow_meter),
301 		.trunk_size = 64,
302 		.need_lock = 1,
303 		.release_mem_en = 1,
304 		.malloc = mlx5_malloc,
305 		.free = mlx5_free,
306 		.type = "mlx5_meter_ipool",
307 	},
308 	[MLX5_IPOOL_MCP] = {
309 		.size = sizeof(struct mlx5_flow_mreg_copy_resource),
310 		.trunk_size = 64,
311 		.grow_trunk = 3,
312 		.grow_shift = 2,
313 		.need_lock = 1,
314 		.release_mem_en = 1,
315 		.malloc = mlx5_malloc,
316 		.free = mlx5_free,
317 		.type = "mlx5_mcp_ipool",
318 	},
319 	[MLX5_IPOOL_HRXQ] = {
320 		.size = (sizeof(struct mlx5_hrxq) + MLX5_RSS_HASH_KEY_LEN),
321 		.trunk_size = 64,
322 		.grow_trunk = 3,
323 		.grow_shift = 2,
324 		.need_lock = 1,
325 		.release_mem_en = 1,
326 		.malloc = mlx5_malloc,
327 		.free = mlx5_free,
328 		.type = "mlx5_hrxq_ipool",
329 	},
330 	[MLX5_IPOOL_MLX5_FLOW] = {
331 		/*
332 		 * MLX5_IPOOL_MLX5_FLOW size varies for DV and VERBS flows.
333 		 * It set in run time according to PCI function configuration.
334 		 */
335 		.size = 0,
336 		.trunk_size = 64,
337 		.grow_trunk = 3,
338 		.grow_shift = 2,
339 		.need_lock = 1,
340 		.release_mem_en = 0,
341 		.per_core_cache = 1 << 19,
342 		.malloc = mlx5_malloc,
343 		.free = mlx5_free,
344 		.type = "mlx5_flow_handle_ipool",
345 	},
346 	[MLX5_IPOOL_RTE_FLOW] = {
347 		.size = sizeof(struct rte_flow),
348 		.trunk_size = 4096,
349 		.need_lock = 1,
350 		.release_mem_en = 1,
351 		.malloc = mlx5_malloc,
352 		.free = mlx5_free,
353 		.type = "rte_flow_ipool",
354 	},
355 	[MLX5_IPOOL_RSS_EXPANTION_FLOW_ID] = {
356 		.size = 0,
357 		.need_lock = 1,
358 		.type = "mlx5_flow_rss_id_ipool",
359 	},
360 	[MLX5_IPOOL_RSS_SHARED_ACTIONS] = {
361 		.size = sizeof(struct mlx5_shared_action_rss),
362 		.trunk_size = 64,
363 		.grow_trunk = 3,
364 		.grow_shift = 2,
365 		.need_lock = 1,
366 		.release_mem_en = 1,
367 		.malloc = mlx5_malloc,
368 		.free = mlx5_free,
369 		.type = "mlx5_shared_action_rss",
370 	},
371 	[MLX5_IPOOL_MTR_POLICY] = {
372 		/**
373 		 * The ipool index should grow continually from small to big,
374 		 * for policy idx, so not set grow_trunk to avoid policy index
375 		 * not jump continually.
376 		 */
377 		.size = sizeof(struct mlx5_flow_meter_sub_policy),
378 		.trunk_size = 64,
379 		.need_lock = 1,
380 		.release_mem_en = 1,
381 		.malloc = mlx5_malloc,
382 		.free = mlx5_free,
383 		.type = "mlx5_meter_policy_ipool",
384 	},
385 };
386 
387 #define MLX5_FLOW_MIN_ID_POOL_SIZE 512
388 #define MLX5_ID_GENERATION_ARRAY_FACTOR 16
389 
390 #define MLX5_FLOW_TABLE_HLIST_ARRAY_SIZE 1024
391 
392 #define MLX5_RXQ_ENH_CQE_COMP_MASK 0x80
393 
394 /**
395  * Decide whether representor ID is a HPF(host PF) port on BF2.
396  *
397  * @param dev
398  *   Pointer to Ethernet device structure.
399  *
400  * @return
401  *   Non-zero if HPF, otherwise 0.
402  */
403 bool
404 mlx5_is_hpf(struct rte_eth_dev *dev)
405 {
406 	struct mlx5_priv *priv = dev->data->dev_private;
407 	uint16_t repr = MLX5_REPRESENTOR_REPR(priv->representor_id);
408 	int type = MLX5_REPRESENTOR_TYPE(priv->representor_id);
409 
410 	return priv->representor != 0 && type == RTE_ETH_REPRESENTOR_VF &&
411 	       MLX5_REPRESENTOR_REPR(-1) == repr;
412 }
413 
414 /**
415  * Decide whether representor ID is a SF port representor.
416  *
417  * @param dev
418  *   Pointer to Ethernet device structure.
419  *
420  * @return
421  *   Non-zero if HPF, otherwise 0.
422  */
423 bool
424 mlx5_is_sf_repr(struct rte_eth_dev *dev)
425 {
426 	struct mlx5_priv *priv = dev->data->dev_private;
427 	int type = MLX5_REPRESENTOR_TYPE(priv->representor_id);
428 
429 	return priv->representor != 0 && type == RTE_ETH_REPRESENTOR_SF;
430 }
431 
432 /**
433  * Initialize the ASO aging management structure.
434  *
435  * @param[in] sh
436  *   Pointer to mlx5_dev_ctx_shared object to free
437  *
438  * @return
439  *   0 on success, a negative errno value otherwise and rte_errno is set.
440  */
441 int
442 mlx5_flow_aso_age_mng_init(struct mlx5_dev_ctx_shared *sh)
443 {
444 	int err;
445 
446 	if (sh->aso_age_mng)
447 		return 0;
448 	sh->aso_age_mng = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*sh->aso_age_mng),
449 				      RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
450 	if (!sh->aso_age_mng) {
451 		DRV_LOG(ERR, "aso_age_mng allocation was failed.");
452 		rte_errno = ENOMEM;
453 		return -ENOMEM;
454 	}
455 	err = mlx5_aso_queue_init(sh, ASO_OPC_MOD_FLOW_HIT, 1);
456 	if (err) {
457 		mlx5_free(sh->aso_age_mng);
458 		return -1;
459 	}
460 	rte_rwlock_init(&sh->aso_age_mng->resize_rwl);
461 	rte_spinlock_init(&sh->aso_age_mng->free_sl);
462 	LIST_INIT(&sh->aso_age_mng->free);
463 	return 0;
464 }
465 
466 /**
467  * Close and release all the resources of the ASO aging management structure.
468  *
469  * @param[in] sh
470  *   Pointer to mlx5_dev_ctx_shared object to free.
471  */
472 static void
473 mlx5_flow_aso_age_mng_close(struct mlx5_dev_ctx_shared *sh)
474 {
475 	int i, j;
476 
477 	mlx5_aso_flow_hit_queue_poll_stop(sh);
478 	mlx5_aso_queue_uninit(sh, ASO_OPC_MOD_FLOW_HIT);
479 	if (sh->aso_age_mng->pools) {
480 		struct mlx5_aso_age_pool *pool;
481 
482 		for (i = 0; i < sh->aso_age_mng->next; ++i) {
483 			pool = sh->aso_age_mng->pools[i];
484 			claim_zero(mlx5_devx_cmd_destroy
485 						(pool->flow_hit_aso_obj));
486 			for (j = 0; j < MLX5_COUNTERS_PER_POOL; ++j)
487 				if (pool->actions[j].dr_action)
488 					claim_zero
489 					    (mlx5_flow_os_destroy_flow_action
490 					      (pool->actions[j].dr_action));
491 			mlx5_free(pool);
492 		}
493 		mlx5_free(sh->aso_age_mng->pools);
494 	}
495 	mlx5_free(sh->aso_age_mng);
496 }
497 
498 /**
499  * Initialize the shared aging list information per port.
500  *
501  * @param[in] sh
502  *   Pointer to mlx5_dev_ctx_shared object.
503  */
504 static void
505 mlx5_flow_aging_init(struct mlx5_dev_ctx_shared *sh)
506 {
507 	uint32_t i;
508 	struct mlx5_age_info *age_info;
509 
510 	/*
511 	 * In HW steering, aging information structure is initialized later
512 	 * during configure function.
513 	 */
514 	if (sh->config.dv_flow_en == 2)
515 		return;
516 	for (i = 0; i < sh->max_port; i++) {
517 		age_info = &sh->port[i].age_info;
518 		age_info->flags = 0;
519 		TAILQ_INIT(&age_info->aged_counters);
520 		LIST_INIT(&age_info->aged_aso);
521 		rte_spinlock_init(&age_info->aged_sl);
522 		MLX5_AGE_SET(age_info, MLX5_AGE_TRIGGER);
523 	}
524 }
525 
526 /**
527  * DV flow counter mode detect and config.
528  *
529  * @param dev
530  *   Pointer to rte_eth_dev structure.
531  *
532  */
533 void
534 mlx5_flow_counter_mode_config(struct rte_eth_dev *dev __rte_unused)
535 {
536 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
537 	struct mlx5_priv *priv = dev->data->dev_private;
538 	struct mlx5_dev_ctx_shared *sh = priv->sh;
539 	struct mlx5_hca_attr *hca_attr = &sh->cdev->config.hca_attr;
540 	bool fallback;
541 
542 #ifndef HAVE_IBV_DEVX_ASYNC
543 	fallback = true;
544 #else
545 	fallback = false;
546 	if (!sh->cdev->config.devx || !sh->config.dv_flow_en ||
547 	    !hca_attr->flow_counters_dump ||
548 	    !(hca_attr->flow_counter_bulk_alloc_bitmap & 0x4) ||
549 	    (mlx5_flow_dv_discover_counter_offset_support(dev) == -ENOTSUP))
550 		fallback = true;
551 #endif
552 	if (fallback)
553 		DRV_LOG(INFO, "Use fall-back DV counter management. Flow "
554 			"counter dump:%d, bulk_alloc_bitmap:0x%hhx.",
555 			hca_attr->flow_counters_dump,
556 			hca_attr->flow_counter_bulk_alloc_bitmap);
557 	/* Initialize fallback mode only on the port initializes sh. */
558 	if (sh->refcnt == 1)
559 		sh->sws_cmng.counter_fallback = fallback;
560 	else if (fallback != sh->sws_cmng.counter_fallback)
561 		DRV_LOG(WARNING, "Port %d in sh has different fallback mode "
562 			"with others:%d.", PORT_ID(priv), fallback);
563 #endif
564 }
565 
566 /**
567  * Initialize the counters management structure.
568  *
569  * @param[in] sh
570  *   Pointer to mlx5_dev_ctx_shared object to free
571  *
572  * @return
573  *   0 on success, otherwise negative errno value and rte_errno is set.
574  */
575 static int
576 mlx5_flow_counters_mng_init(struct mlx5_dev_ctx_shared *sh)
577 {
578 	int i, j;
579 
580 	if (sh->config.dv_flow_en < 2) {
581 		void *pools;
582 
583 		pools = mlx5_malloc(MLX5_MEM_ZERO,
584 				    sizeof(struct mlx5_flow_counter_pool *) *
585 				    MLX5_COUNTER_POOLS_MAX_NUM,
586 				    0, SOCKET_ID_ANY);
587 		if (!pools) {
588 			DRV_LOG(ERR,
589 				"Counter management allocation was failed.");
590 			rte_errno = ENOMEM;
591 			return -rte_errno;
592 		}
593 		memset(&sh->sws_cmng, 0, sizeof(sh->sws_cmng));
594 		TAILQ_INIT(&sh->sws_cmng.flow_counters);
595 		sh->sws_cmng.min_id = MLX5_CNT_BATCH_OFFSET;
596 		sh->sws_cmng.max_id = -1;
597 		sh->sws_cmng.last_pool_idx = POOL_IDX_INVALID;
598 		sh->sws_cmng.pools = pools;
599 		rte_spinlock_init(&sh->sws_cmng.pool_update_sl);
600 		for (i = 0; i < MLX5_COUNTER_TYPE_MAX; i++) {
601 			TAILQ_INIT(&sh->sws_cmng.counters[i]);
602 			rte_spinlock_init(&sh->sws_cmng.csl[i]);
603 		}
604 	} else {
605 		struct mlx5_hca_attr *attr = &sh->cdev->config.hca_attr;
606 		uint32_t fw_max_nb_cnts = attr->max_flow_counter;
607 		uint8_t log_dcs = log2above(fw_max_nb_cnts) - 1;
608 		uint32_t max_nb_cnts = 0;
609 
610 		for (i = 0, j = 0; j < MLX5_HWS_CNT_DCS_NUM; ++i) {
611 			int log_dcs_i = log_dcs - i;
612 
613 			if (log_dcs_i < 0)
614 				break;
615 			if ((max_nb_cnts | RTE_BIT32(log_dcs_i)) >
616 			    fw_max_nb_cnts)
617 				continue;
618 			max_nb_cnts |= RTE_BIT32(log_dcs_i);
619 			j++;
620 		}
621 		sh->hws_max_log_bulk_sz = log_dcs;
622 		sh->hws_max_nb_counters = max_nb_cnts;
623 	}
624 	return 0;
625 }
626 
627 /**
628  * Destroy all the resources allocated for a counter memory management.
629  *
630  * @param[in] mng
631  *   Pointer to the memory management structure.
632  */
633 static void
634 mlx5_flow_destroy_counter_stat_mem_mng(struct mlx5_counter_stats_mem_mng *mng)
635 {
636 	uint8_t *mem = (uint8_t *)(uintptr_t)mng->raws[0].data;
637 
638 	LIST_REMOVE(mng, next);
639 	mlx5_os_wrapped_mkey_destroy(&mng->wm);
640 	mlx5_free(mem);
641 }
642 
643 /**
644  * Close and release all the resources of the counters management.
645  *
646  * @param[in] sh
647  *   Pointer to mlx5_dev_ctx_shared object to free.
648  */
649 static void
650 mlx5_flow_counters_mng_close(struct mlx5_dev_ctx_shared *sh)
651 {
652 	struct mlx5_counter_stats_mem_mng *mng;
653 	int i, j;
654 	int retries = 1024;
655 
656 	rte_errno = 0;
657 	while (--retries) {
658 		rte_eal_alarm_cancel(mlx5_flow_query_alarm, sh);
659 		if (rte_errno != EINPROGRESS)
660 			break;
661 		rte_pause();
662 	}
663 
664 	if (sh->sws_cmng.pools) {
665 		struct mlx5_flow_counter_pool *pool;
666 		uint16_t n_valid = sh->sws_cmng.n_valid;
667 		bool fallback = sh->sws_cmng.counter_fallback;
668 
669 		for (i = 0; i < n_valid; ++i) {
670 			pool = sh->sws_cmng.pools[i];
671 			if (!fallback && pool->min_dcs)
672 				claim_zero(mlx5_devx_cmd_destroy
673 							       (pool->min_dcs));
674 			for (j = 0; j < MLX5_COUNTERS_PER_POOL; ++j) {
675 				struct mlx5_flow_counter *cnt =
676 						MLX5_POOL_GET_CNT(pool, j);
677 
678 				if (cnt->action)
679 					claim_zero
680 					 (mlx5_flow_os_destroy_flow_action
681 					  (cnt->action));
682 				if (fallback && cnt->dcs_when_free)
683 					claim_zero(mlx5_devx_cmd_destroy
684 						   (cnt->dcs_when_free));
685 			}
686 			mlx5_free(pool);
687 		}
688 		mlx5_free(sh->sws_cmng.pools);
689 	}
690 	mng = LIST_FIRST(&sh->sws_cmng.mem_mngs);
691 	while (mng) {
692 		mlx5_flow_destroy_counter_stat_mem_mng(mng);
693 		mng = LIST_FIRST(&sh->sws_cmng.mem_mngs);
694 	}
695 	memset(&sh->sws_cmng, 0, sizeof(sh->sws_cmng));
696 }
697 
698 /**
699  * Initialize the aso flow meters management structure.
700  *
701  * @param[in] sh
702  *   Pointer to mlx5_dev_ctx_shared object to free
703  */
704 int
705 mlx5_aso_flow_mtrs_mng_init(struct mlx5_dev_ctx_shared *sh)
706 {
707 	if (!sh->mtrmng) {
708 		sh->mtrmng = mlx5_malloc(MLX5_MEM_ZERO,
709 			sizeof(*sh->mtrmng),
710 			RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
711 		if (!sh->mtrmng) {
712 			DRV_LOG(ERR,
713 			"meter management allocation was failed.");
714 			rte_errno = ENOMEM;
715 			return -ENOMEM;
716 		}
717 		if (sh->meter_aso_en) {
718 			rte_spinlock_init(&sh->mtrmng->pools_mng.mtrsl);
719 			rte_rwlock_init(&sh->mtrmng->pools_mng.resize_mtrwl);
720 			LIST_INIT(&sh->mtrmng->pools_mng.meters);
721 		}
722 		sh->mtrmng->def_policy_id = MLX5_INVALID_POLICY_ID;
723 	}
724 	return 0;
725 }
726 
727 /**
728  * Close and release all the resources of
729  * the ASO flow meter management structure.
730  *
731  * @param[in] sh
732  *   Pointer to mlx5_dev_ctx_shared object to free.
733  */
734 static void
735 mlx5_aso_flow_mtrs_mng_close(struct mlx5_dev_ctx_shared *sh)
736 {
737 	struct mlx5_aso_mtr_pool *mtr_pool;
738 	struct mlx5_flow_mtr_mng *mtrmng = sh->mtrmng;
739 	uint32_t idx;
740 #ifdef HAVE_MLX5_DR_CREATE_ACTION_ASO
741 	struct mlx5_aso_mtr *aso_mtr;
742 	int i;
743 #endif /* HAVE_MLX5_DR_CREATE_ACTION_ASO */
744 
745 	if (sh->meter_aso_en) {
746 		mlx5_aso_queue_uninit(sh, ASO_OPC_MOD_POLICER);
747 		idx = mtrmng->pools_mng.n_valid;
748 		while (idx--) {
749 			mtr_pool = mtrmng->pools_mng.pools[idx];
750 #ifdef HAVE_MLX5_DR_CREATE_ACTION_ASO
751 			for (i = 0; i < MLX5_ASO_MTRS_PER_POOL; i++) {
752 				aso_mtr = &mtr_pool->mtrs[i];
753 				if (aso_mtr->fm.meter_action_g)
754 					claim_zero
755 					(mlx5_glue->destroy_flow_action
756 					(aso_mtr->fm.meter_action_g));
757 				if (aso_mtr->fm.meter_action_y)
758 					claim_zero
759 					(mlx5_glue->destroy_flow_action
760 					(aso_mtr->fm.meter_action_y));
761 			}
762 #endif /* HAVE_MLX5_DR_CREATE_ACTION_ASO */
763 			claim_zero(mlx5_devx_cmd_destroy
764 						(mtr_pool->devx_obj));
765 			mtrmng->pools_mng.n_valid--;
766 			mlx5_free(mtr_pool);
767 		}
768 		mlx5_free(sh->mtrmng->pools_mng.pools);
769 	}
770 	mlx5_free(sh->mtrmng);
771 	sh->mtrmng = NULL;
772 }
773 
774 /* Send FLOW_AGED event if needed. */
775 void
776 mlx5_age_event_prepare(struct mlx5_dev_ctx_shared *sh)
777 {
778 	struct mlx5_age_info *age_info;
779 	uint32_t i;
780 
781 	for (i = 0; i < sh->max_port; i++) {
782 		age_info = &sh->port[i].age_info;
783 		if (!MLX5_AGE_GET(age_info, MLX5_AGE_EVENT_NEW))
784 			continue;
785 		MLX5_AGE_UNSET(age_info, MLX5_AGE_EVENT_NEW);
786 		if (MLX5_AGE_GET(age_info, MLX5_AGE_TRIGGER)) {
787 			MLX5_AGE_UNSET(age_info, MLX5_AGE_TRIGGER);
788 			rte_eth_dev_callback_process
789 				(&rte_eth_devices[sh->port[i].devx_ih_port_id],
790 				RTE_ETH_EVENT_FLOW_AGED, NULL);
791 		}
792 	}
793 }
794 
795 /*
796  * Initialize the ASO connection tracking structure.
797  *
798  * @param[in] sh
799  *   Pointer to mlx5_dev_ctx_shared object.
800  *
801  * @return
802  *   0 on success, a negative errno value otherwise and rte_errno is set.
803  */
804 int
805 mlx5_flow_aso_ct_mng_init(struct mlx5_dev_ctx_shared *sh)
806 {
807 	int err;
808 
809 	if (sh->ct_mng)
810 		return 0;
811 	sh->ct_mng = mlx5_malloc(MLX5_MEM_ZERO, sizeof(*sh->ct_mng) +
812 				 sizeof(struct mlx5_aso_sq) * MLX5_ASO_CT_SQ_NUM,
813 				 RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
814 	if (!sh->ct_mng) {
815 		DRV_LOG(ERR, "ASO CT management allocation failed.");
816 		rte_errno = ENOMEM;
817 		return -rte_errno;
818 	}
819 	err = mlx5_aso_queue_init(sh, ASO_OPC_MOD_CONNECTION_TRACKING, MLX5_ASO_CT_SQ_NUM);
820 	if (err) {
821 		mlx5_free(sh->ct_mng);
822 		/* rte_errno should be extracted from the failure. */
823 		rte_errno = EINVAL;
824 		return -rte_errno;
825 	}
826 	rte_spinlock_init(&sh->ct_mng->ct_sl);
827 	rte_rwlock_init(&sh->ct_mng->resize_rwl);
828 	LIST_INIT(&sh->ct_mng->free_cts);
829 	return 0;
830 }
831 
832 /*
833  * Close and release all the resources of the
834  * ASO connection tracking management structure.
835  *
836  * @param[in] sh
837  *   Pointer to mlx5_dev_ctx_shared object to free.
838  */
839 static void
840 mlx5_flow_aso_ct_mng_close(struct mlx5_dev_ctx_shared *sh)
841 {
842 	struct mlx5_aso_ct_pools_mng *mng = sh->ct_mng;
843 	struct mlx5_aso_ct_pool *ct_pool;
844 	struct mlx5_aso_ct_action *ct;
845 	uint32_t idx;
846 	uint32_t val;
847 	uint32_t cnt;
848 	int i;
849 
850 	mlx5_aso_queue_uninit(sh, ASO_OPC_MOD_CONNECTION_TRACKING);
851 	idx = mng->next;
852 	while (idx--) {
853 		cnt = 0;
854 		ct_pool = mng->pools[idx];
855 		for (i = 0; i < MLX5_ASO_CT_ACTIONS_PER_POOL; i++) {
856 			ct = &ct_pool->actions[i];
857 			val = __atomic_fetch_sub(&ct->refcnt, 1,
858 						 __ATOMIC_RELAXED);
859 			MLX5_ASSERT(val == 1);
860 			if (val > 1)
861 				cnt++;
862 #ifdef HAVE_MLX5_DR_ACTION_ASO_CT
863 			if (ct->dr_action_orig)
864 				claim_zero(mlx5_glue->destroy_flow_action
865 							(ct->dr_action_orig));
866 			if (ct->dr_action_rply)
867 				claim_zero(mlx5_glue->destroy_flow_action
868 							(ct->dr_action_rply));
869 #endif
870 		}
871 		claim_zero(mlx5_devx_cmd_destroy(ct_pool->devx_obj));
872 		if (cnt) {
873 			DRV_LOG(DEBUG, "%u ASO CT objects are being used in the pool %u",
874 				cnt, i);
875 		}
876 		mlx5_free(ct_pool);
877 		/* in case of failure. */
878 		mng->next--;
879 	}
880 	mlx5_free(mng->pools);
881 	mlx5_free(mng);
882 	/* Management structure must be cleared to 0s during allocation. */
883 	sh->ct_mng = NULL;
884 }
885 
886 /**
887  * Initialize the flow resources' indexed mempool.
888  *
889  * @param[in] sh
890  *   Pointer to mlx5_dev_ctx_shared object.
891  */
892 static void
893 mlx5_flow_ipool_create(struct mlx5_dev_ctx_shared *sh)
894 {
895 	uint8_t i;
896 	struct mlx5_indexed_pool_config cfg;
897 
898 	for (i = 0; i < MLX5_IPOOL_MAX; ++i) {
899 		cfg = mlx5_ipool_cfg[i];
900 		switch (i) {
901 		default:
902 			break;
903 		/*
904 		 * Set MLX5_IPOOL_MLX5_FLOW ipool size
905 		 * according to PCI function flow configuration.
906 		 */
907 		case MLX5_IPOOL_MLX5_FLOW:
908 			cfg.size = sh->config.dv_flow_en ?
909 				sizeof(struct mlx5_flow_handle) :
910 				MLX5_FLOW_HANDLE_VERBS_SIZE;
911 			break;
912 #if defined(HAVE_IBV_FLOW_DV_SUPPORT) || !defined(HAVE_INFINIBAND_VERBS_H)
913 		/* Set MLX5_IPOOL_JUMP ipool entry size depending on selected flow engine. */
914 		case MLX5_IPOOL_JUMP:
915 			cfg.size = sh->config.dv_flow_en == 2 ?
916 				sizeof(struct mlx5_flow_group) :
917 				sizeof(struct mlx5_flow_tbl_data_entry);
918 			break;
919 #endif
920 		}
921 		if (sh->config.reclaim_mode) {
922 			cfg.release_mem_en = 1;
923 			cfg.per_core_cache = 0;
924 		} else {
925 			cfg.release_mem_en = 0;
926 		}
927 		sh->ipool[i] = mlx5_ipool_create(&cfg);
928 	}
929 }
930 
931 
932 /**
933  * Release the flow resources' indexed mempool.
934  *
935  * @param[in] sh
936  *   Pointer to mlx5_dev_ctx_shared object.
937  */
938 static void
939 mlx5_flow_ipool_destroy(struct mlx5_dev_ctx_shared *sh)
940 {
941 	uint8_t i;
942 
943 	for (i = 0; i < MLX5_IPOOL_MAX; ++i)
944 		mlx5_ipool_destroy(sh->ipool[i]);
945 	for (i = 0; i < MLX5_MAX_MODIFY_NUM; ++i)
946 		if (sh->mdh_ipools[i])
947 			mlx5_ipool_destroy(sh->mdh_ipools[i]);
948 }
949 
950 /*
951  * Check if dynamic flex parser for eCPRI already exists.
952  *
953  * @param dev
954  *   Pointer to Ethernet device structure.
955  *
956  * @return
957  *   true on exists, false on not.
958  */
959 bool
960 mlx5_flex_parser_ecpri_exist(struct rte_eth_dev *dev)
961 {
962 	struct mlx5_priv *priv = dev->data->dev_private;
963 	struct mlx5_ecpri_parser_profile *prf = &priv->sh->ecpri_parser;
964 
965 	return !!prf->obj;
966 }
967 
968 /*
969  * Allocation of a flex parser for eCPRI. Once created, this parser related
970  * resources will be held until the device is closed.
971  *
972  * @param dev
973  *   Pointer to Ethernet device structure.
974  *
975  * @return
976  *   0 on success, a negative errno value otherwise and rte_errno is set.
977  */
978 int
979 mlx5_flex_parser_ecpri_alloc(struct rte_eth_dev *dev)
980 {
981 	struct mlx5_priv *priv = dev->data->dev_private;
982 	struct mlx5_ecpri_parser_profile *prf =	&priv->sh->ecpri_parser;
983 	struct mlx5_devx_graph_node_attr node = {
984 		.modify_field_select = 0,
985 	};
986 	uint32_t ids[8];
987 	int ret;
988 
989 	if (!priv->sh->cdev->config.hca_attr.parse_graph_flex_node) {
990 		DRV_LOG(ERR, "Dynamic flex parser is not supported "
991 			"for device %s.", priv->dev_data->name);
992 		return -ENOTSUP;
993 	}
994 	node.header_length_mode = MLX5_GRAPH_NODE_LEN_FIXED;
995 	/* 8 bytes now: 4B common header + 4B message body header. */
996 	node.header_length_base_value = 0x8;
997 	/* After MAC layer: Ether / VLAN. */
998 	node.in[0].arc_parse_graph_node = MLX5_GRAPH_ARC_NODE_MAC;
999 	/* Type of compared condition should be 0xAEFE in the L2 layer. */
1000 	node.in[0].compare_condition_value = RTE_ETHER_TYPE_ECPRI;
1001 	/* Sample #0: type in common header. */
1002 	node.sample[0].flow_match_sample_en = 1;
1003 	/* Fixed offset. */
1004 	node.sample[0].flow_match_sample_offset_mode = 0x0;
1005 	/* Only the 2nd byte will be used. */
1006 	node.sample[0].flow_match_sample_field_base_offset = 0x0;
1007 	/* Sample #1: message payload. */
1008 	node.sample[1].flow_match_sample_en = 1;
1009 	/* Fixed offset. */
1010 	node.sample[1].flow_match_sample_offset_mode = 0x0;
1011 	/*
1012 	 * Only the first two bytes will be used right now, and its offset will
1013 	 * start after the common header that with the length of a DW(u32).
1014 	 */
1015 	node.sample[1].flow_match_sample_field_base_offset = sizeof(uint32_t);
1016 	prf->obj = mlx5_devx_cmd_create_flex_parser(priv->sh->cdev->ctx, &node);
1017 	if (!prf->obj) {
1018 		DRV_LOG(ERR, "Failed to create flex parser node object.");
1019 		return (rte_errno == 0) ? -ENODEV : -rte_errno;
1020 	}
1021 	prf->num = 2;
1022 	ret = mlx5_devx_cmd_query_parse_samples(prf->obj, ids, prf->num, NULL);
1023 	if (ret) {
1024 		DRV_LOG(ERR, "Failed to query sample IDs.");
1025 		goto error;
1026 	}
1027 	prf->offset[0] = 0x0;
1028 	prf->offset[1] = sizeof(uint32_t);
1029 	prf->ids[0] = ids[0];
1030 	prf->ids[1] = ids[1];
1031 	return 0;
1032 error:
1033 	mlx5_devx_cmd_destroy(prf->obj);
1034 	return (rte_errno == 0) ? -ENODEV : -rte_errno;
1035 }
1036 
1037 /*
1038  * Destroy the flex parser node, including the parser itself, input / output
1039  * arcs and DW samples. Resources could be reused then.
1040  *
1041  * @param dev
1042  *   Pointer to Ethernet device structure.
1043  */
1044 static void
1045 mlx5_flex_parser_ecpri_release(struct rte_eth_dev *dev)
1046 {
1047 	struct mlx5_priv *priv = dev->data->dev_private;
1048 	struct mlx5_ecpri_parser_profile *prf = &priv->sh->ecpri_parser;
1049 
1050 	if (prf->obj)
1051 		mlx5_devx_cmd_destroy(prf->obj);
1052 	prf->obj = NULL;
1053 }
1054 
1055 /*
1056  * Allocation of a flex parser for srh. Once refcnt is zero, the resources held
1057  * by this parser will be freed.
1058  * @param dev
1059  *   Pointer to Ethernet device structure.
1060  *
1061  * @return
1062  *   0 on success, a negative errno value otherwise and rte_errno is set.
1063  */
1064 int
1065 mlx5_alloc_srh_flex_parser(struct rte_eth_dev *dev)
1066 {
1067 	struct mlx5_devx_graph_node_attr node = {
1068 		.modify_field_select = 0,
1069 	};
1070 	uint32_t i;
1071 	uint32_t ids[MLX5_GRAPH_NODE_SAMPLE_NUM];
1072 	struct mlx5_priv *priv = dev->data->dev_private;
1073 	struct mlx5_common_dev_config *config = &priv->sh->cdev->config;
1074 	struct mlx5_hca_flex_attr *attr = &priv->sh->cdev->config.hca_attr.flex;
1075 	void *fp = NULL, *ibv_ctx = priv->sh->cdev->ctx;
1076 	int ret;
1077 
1078 	memset(ids, 0xff, sizeof(ids));
1079 	if (!config->hca_attr.parse_graph_flex_node ||
1080 	    !config->hca_attr.flex.query_match_sample_info) {
1081 		DRV_LOG(ERR, "Dynamic flex parser is not supported on HWS");
1082 		return -ENOTSUP;
1083 	}
1084 	if (__atomic_fetch_add(&priv->sh->srh_flex_parser.refcnt, 1, __ATOMIC_RELAXED) + 1 > 1)
1085 		return 0;
1086 	priv->sh->srh_flex_parser.flex.devx_fp = mlx5_malloc(MLX5_MEM_ZERO,
1087 			sizeof(struct mlx5_flex_parser_devx), 0, SOCKET_ID_ANY);
1088 	if (!priv->sh->srh_flex_parser.flex.devx_fp)
1089 		return -ENOMEM;
1090 	node.header_length_mode = MLX5_GRAPH_NODE_LEN_FIELD;
1091 	/* Srv6 first two DW are not counted in. */
1092 	node.header_length_base_value = 0x8;
1093 	/* The unit is uint64_t. */
1094 	node.header_length_field_shift = 0x3;
1095 	/* Header length is the 2nd byte. */
1096 	node.header_length_field_offset = 0x8;
1097 	if (attr->header_length_mask_width < 8)
1098 		node.header_length_field_offset += 8 - attr->header_length_mask_width;
1099 	node.header_length_field_mask = 0xF;
1100 	/* One byte next header protocol. */
1101 	node.next_header_field_size = 0x8;
1102 	node.in[0].arc_parse_graph_node = MLX5_GRAPH_ARC_NODE_IP;
1103 	node.in[0].compare_condition_value = IPPROTO_ROUTING;
1104 	/* Final IPv6 address. */
1105 	for (i = 0; i <= MLX5_SRV6_SAMPLE_NUM - 1 && i < MLX5_GRAPH_NODE_SAMPLE_NUM; i++) {
1106 		node.sample[i].flow_match_sample_en = 1;
1107 		node.sample[i].flow_match_sample_offset_mode =
1108 					MLX5_GRAPH_SAMPLE_OFFSET_FIXED;
1109 		/* First come first serve no matter inner or outer. */
1110 		node.sample[i].flow_match_sample_tunnel_mode =
1111 					MLX5_GRAPH_SAMPLE_TUNNEL_FIRST;
1112 		node.sample[i].flow_match_sample_field_base_offset =
1113 					(i + 1) * sizeof(uint32_t); /* in bytes */
1114 	}
1115 	node.sample[0].flow_match_sample_field_base_offset = 0;
1116 	node.out[0].arc_parse_graph_node = MLX5_GRAPH_ARC_NODE_TCP;
1117 	node.out[0].compare_condition_value = IPPROTO_TCP;
1118 	node.out[1].arc_parse_graph_node = MLX5_GRAPH_ARC_NODE_UDP;
1119 	node.out[1].compare_condition_value = IPPROTO_UDP;
1120 	node.out[2].arc_parse_graph_node = MLX5_GRAPH_ARC_NODE_IPV6;
1121 	node.out[2].compare_condition_value = IPPROTO_IPV6;
1122 	fp = mlx5_devx_cmd_create_flex_parser(ibv_ctx, &node);
1123 	if (!fp) {
1124 		DRV_LOG(ERR, "Failed to create flex parser node object.");
1125 		goto error;
1126 	}
1127 	priv->sh->srh_flex_parser.flex.devx_fp->devx_obj = fp;
1128 	priv->sh->srh_flex_parser.flex.mapnum = MLX5_SRV6_SAMPLE_NUM;
1129 	priv->sh->srh_flex_parser.flex.devx_fp->num_samples = MLX5_SRV6_SAMPLE_NUM;
1130 
1131 	ret = mlx5_devx_cmd_query_parse_samples(fp, ids, priv->sh->srh_flex_parser.flex.mapnum,
1132 						&priv->sh->srh_flex_parser.flex.devx_fp->anchor_id);
1133 	if (ret) {
1134 		DRV_LOG(ERR, "Failed to query sample IDs.");
1135 		goto error;
1136 	}
1137 	for (i = 0; i <= MLX5_SRV6_SAMPLE_NUM - 1 && i < MLX5_GRAPH_NODE_SAMPLE_NUM; i++) {
1138 		ret = mlx5_devx_cmd_match_sample_info_query(ibv_ctx, ids[i],
1139 					&priv->sh->srh_flex_parser.flex.devx_fp->sample_info[i]);
1140 		if (ret) {
1141 			DRV_LOG(ERR, "Failed to query sample id %u information.", ids[i]);
1142 			goto error;
1143 		}
1144 	}
1145 	for (i = 0; i <= MLX5_SRV6_SAMPLE_NUM - 1 && i < MLX5_GRAPH_NODE_SAMPLE_NUM; i++) {
1146 		priv->sh->srh_flex_parser.flex.devx_fp->sample_ids[i] = ids[i];
1147 		priv->sh->srh_flex_parser.flex.map[i].width = sizeof(uint32_t) * CHAR_BIT;
1148 		priv->sh->srh_flex_parser.flex.map[i].reg_id = i;
1149 		priv->sh->srh_flex_parser.flex.map[i].shift =
1150 						(i + 1) * sizeof(uint32_t) * CHAR_BIT;
1151 	}
1152 	priv->sh->srh_flex_parser.flex.map[0].shift = 0;
1153 	return 0;
1154 error:
1155 	if (fp)
1156 		mlx5_devx_cmd_destroy(fp);
1157 	if (priv->sh->srh_flex_parser.flex.devx_fp)
1158 		mlx5_free(priv->sh->srh_flex_parser.flex.devx_fp);
1159 	return (rte_errno == 0) ? -ENODEV : -rte_errno;
1160 }
1161 
1162 /*
1163  * Destroy the flex parser node, including the parser itself, input / output
1164  * arcs and DW samples. Resources could be reused then.
1165  *
1166  * @param dev
1167  *   Pointer to Ethernet device structure
1168  */
1169 void
1170 mlx5_free_srh_flex_parser(struct rte_eth_dev *dev)
1171 {
1172 	struct mlx5_priv *priv = dev->data->dev_private;
1173 	struct mlx5_internal_flex_parser_profile *fp = &priv->sh->srh_flex_parser;
1174 
1175 	if (__atomic_fetch_sub(&fp->refcnt, 1, __ATOMIC_RELAXED) - 1)
1176 		return;
1177 	mlx5_devx_cmd_destroy(fp->flex.devx_fp->devx_obj);
1178 	mlx5_free(fp->flex.devx_fp);
1179 	fp->flex.devx_fp = NULL;
1180 }
1181 
1182 uint32_t
1183 mlx5_get_supported_sw_parsing_offloads(const struct mlx5_hca_attr *attr)
1184 {
1185 	uint32_t sw_parsing_offloads = 0;
1186 
1187 	if (attr->swp) {
1188 		sw_parsing_offloads |= MLX5_SW_PARSING_CAP;
1189 		if (attr->swp_csum)
1190 			sw_parsing_offloads |= MLX5_SW_PARSING_CSUM_CAP;
1191 
1192 		if (attr->swp_lso)
1193 			sw_parsing_offloads |= MLX5_SW_PARSING_TSO_CAP;
1194 	}
1195 	return sw_parsing_offloads;
1196 }
1197 
1198 uint32_t
1199 mlx5_get_supported_tunneling_offloads(const struct mlx5_hca_attr *attr)
1200 {
1201 	uint32_t tn_offloads = 0;
1202 
1203 	if (attr->tunnel_stateless_vxlan)
1204 		tn_offloads |= MLX5_TUNNELED_OFFLOADS_VXLAN_CAP;
1205 	if (attr->tunnel_stateless_gre)
1206 		tn_offloads |= MLX5_TUNNELED_OFFLOADS_GRE_CAP;
1207 	if (attr->tunnel_stateless_geneve_rx)
1208 		tn_offloads |= MLX5_TUNNELED_OFFLOADS_GENEVE_CAP;
1209 	return tn_offloads;
1210 }
1211 
1212 /* Fill all fields of UAR structure. */
1213 static int
1214 mlx5_rxtx_uars_prepare(struct mlx5_dev_ctx_shared *sh)
1215 {
1216 	int ret;
1217 
1218 	ret = mlx5_devx_uar_prepare(sh->cdev, &sh->tx_uar);
1219 	if (ret) {
1220 		DRV_LOG(ERR, "Failed to prepare Tx DevX UAR.");
1221 		return -rte_errno;
1222 	}
1223 	MLX5_ASSERT(sh->tx_uar.obj);
1224 	MLX5_ASSERT(mlx5_os_get_devx_uar_base_addr(sh->tx_uar.obj));
1225 	ret = mlx5_devx_uar_prepare(sh->cdev, &sh->rx_uar);
1226 	if (ret) {
1227 		DRV_LOG(ERR, "Failed to prepare Rx DevX UAR.");
1228 		mlx5_devx_uar_release(&sh->tx_uar);
1229 		return -rte_errno;
1230 	}
1231 	MLX5_ASSERT(sh->rx_uar.obj);
1232 	MLX5_ASSERT(mlx5_os_get_devx_uar_base_addr(sh->rx_uar.obj));
1233 	return 0;
1234 }
1235 
1236 static void
1237 mlx5_rxtx_uars_release(struct mlx5_dev_ctx_shared *sh)
1238 {
1239 	mlx5_devx_uar_release(&sh->rx_uar);
1240 	mlx5_devx_uar_release(&sh->tx_uar);
1241 }
1242 
1243 /**
1244  * rte_mempool_walk() callback to unregister Rx mempools.
1245  * It used when implicit mempool registration is disabled.
1246  *
1247  * @param mp
1248  *   The mempool being walked.
1249  * @param arg
1250  *   Pointer to the device shared context.
1251  */
1252 static void
1253 mlx5_dev_ctx_shared_rx_mempool_unregister_cb(struct rte_mempool *mp, void *arg)
1254 {
1255 	struct mlx5_dev_ctx_shared *sh = arg;
1256 
1257 	mlx5_dev_mempool_unregister(sh->cdev, mp);
1258 }
1259 
1260 /**
1261  * Callback used when implicit mempool registration is disabled
1262  * in order to track Rx mempool destruction.
1263  *
1264  * @param event
1265  *   Mempool life cycle event.
1266  * @param mp
1267  *   An Rx mempool registered explicitly when the port is started.
1268  * @param arg
1269  *   Pointer to a device shared context.
1270  */
1271 static void
1272 mlx5_dev_ctx_shared_rx_mempool_event_cb(enum rte_mempool_event event,
1273 					struct rte_mempool *mp, void *arg)
1274 {
1275 	struct mlx5_dev_ctx_shared *sh = arg;
1276 
1277 	if (event == RTE_MEMPOOL_EVENT_DESTROY)
1278 		mlx5_dev_mempool_unregister(sh->cdev, mp);
1279 }
1280 
1281 int
1282 mlx5_dev_ctx_shared_mempool_subscribe(struct rte_eth_dev *dev)
1283 {
1284 	struct mlx5_priv *priv = dev->data->dev_private;
1285 	struct mlx5_dev_ctx_shared *sh = priv->sh;
1286 	int ret;
1287 
1288 	/* Check if we only need to track Rx mempool destruction. */
1289 	if (!sh->cdev->config.mr_mempool_reg_en) {
1290 		ret = rte_mempool_event_callback_register
1291 				(mlx5_dev_ctx_shared_rx_mempool_event_cb, sh);
1292 		return ret == 0 || rte_errno == EEXIST ? 0 : ret;
1293 	}
1294 	return mlx5_dev_mempool_subscribe(sh->cdev);
1295 }
1296 
1297 /**
1298  * Set up multiple TISs with different affinities according to
1299  * number of bonding ports
1300  *
1301  * @param priv
1302  * Pointer of shared context.
1303  *
1304  * @return
1305  * Zero on success, -1 otherwise.
1306  */
1307 static int
1308 mlx5_setup_tis(struct mlx5_dev_ctx_shared *sh)
1309 {
1310 	struct mlx5_devx_lag_context lag_ctx = { 0 };
1311 	struct mlx5_devx_tis_attr tis_attr = { 0 };
1312 	int i;
1313 
1314 	tis_attr.transport_domain = sh->td->id;
1315 	if (sh->bond.n_port) {
1316 		if (!mlx5_devx_cmd_query_lag(sh->cdev->ctx, &lag_ctx)) {
1317 			sh->lag.tx_remap_affinity[0] =
1318 				lag_ctx.tx_remap_affinity_1;
1319 			sh->lag.tx_remap_affinity[1] =
1320 				lag_ctx.tx_remap_affinity_2;
1321 			sh->lag.affinity_mode = lag_ctx.port_select_mode;
1322 		} else {
1323 			DRV_LOG(ERR, "Failed to query lag affinity.");
1324 			return -1;
1325 		}
1326 		if (sh->lag.affinity_mode == MLX5_LAG_MODE_TIS)
1327 			DRV_LOG(DEBUG, "LAG number of ports : %d, affinity_1 & 2 : pf%d & %d.\n",
1328 				sh->bond.n_port, lag_ctx.tx_remap_affinity_1,
1329 				lag_ctx.tx_remap_affinity_2);
1330 		else if (sh->lag.affinity_mode == MLX5_LAG_MODE_HASH)
1331 			DRV_LOG(INFO, "Device %s enabled HW hash based LAG.",
1332 					sh->ibdev_name);
1333 	}
1334 	for (i = 0; i <= sh->bond.n_port; i++) {
1335 		/*
1336 		 * lag_tx_port_affinity: 0 auto-selection, 1 PF1, 2 PF2 vice versa.
1337 		 * Each TIS binds to one PF by setting lag_tx_port_affinity (> 0).
1338 		 * Once LAG enabled, we create multiple TISs and bind each one to
1339 		 * different PFs, then TIS[i+1] gets affinity i+1 and goes to PF i+1.
1340 		 * TIS[0] is reserved for HW Hash mode.
1341 		 */
1342 		tis_attr.lag_tx_port_affinity = i;
1343 		sh->tis[i] = mlx5_devx_cmd_create_tis(sh->cdev->ctx, &tis_attr);
1344 		if (!sh->tis[i]) {
1345 			DRV_LOG(ERR, "Failed to create TIS %d/%d for [bonding] device"
1346 				" %s.", i, sh->bond.n_port,
1347 				sh->ibdev_name);
1348 			return -1;
1349 		}
1350 	}
1351 	return 0;
1352 }
1353 
1354 /**
1355  * Verify and store value for share device argument.
1356  *
1357  * @param[in] key
1358  *   Key argument to verify.
1359  * @param[in] val
1360  *   Value associated with key.
1361  * @param opaque
1362  *   User data.
1363  *
1364  * @return
1365  *   0 on success, a negative errno value otherwise and rte_errno is set.
1366  */
1367 static int
1368 mlx5_dev_args_check_handler(const char *key, const char *val, void *opaque)
1369 {
1370 	struct mlx5_sh_config *config = opaque;
1371 	signed long tmp;
1372 
1373 	errno = 0;
1374 	tmp = strtol(val, NULL, 0);
1375 	if (errno) {
1376 		rte_errno = errno;
1377 		DRV_LOG(WARNING, "%s: \"%s\" is not a valid integer", key, val);
1378 		return -rte_errno;
1379 	}
1380 	if (tmp < 0 && strcmp(MLX5_TX_PP, key) && strcmp(MLX5_TX_SKEW, key)) {
1381 		/* Negative values are acceptable for some keys only. */
1382 		rte_errno = EINVAL;
1383 		DRV_LOG(WARNING, "%s: invalid negative value \"%s\"", key, val);
1384 		return -rte_errno;
1385 	}
1386 	if (strcmp(MLX5_TX_PP, key) == 0) {
1387 		unsigned long mod = tmp >= 0 ? tmp : -tmp;
1388 
1389 		if (!mod) {
1390 			DRV_LOG(ERR, "Zero Tx packet pacing parameter.");
1391 			rte_errno = EINVAL;
1392 			return -rte_errno;
1393 		}
1394 		config->tx_pp = tmp;
1395 	} else if (strcmp(MLX5_TX_SKEW, key) == 0) {
1396 		config->tx_skew = tmp;
1397 	} else if (strcmp(MLX5_L3_VXLAN_EN, key) == 0) {
1398 		config->l3_vxlan_en = !!tmp;
1399 	} else if (strcmp(MLX5_VF_NL_EN, key) == 0) {
1400 		config->vf_nl_en = !!tmp;
1401 	} else if (strcmp(MLX5_DV_ESW_EN, key) == 0) {
1402 		config->dv_esw_en = !!tmp;
1403 	} else if (strcmp(MLX5_DV_FLOW_EN, key) == 0) {
1404 		if (tmp > 2) {
1405 			DRV_LOG(ERR, "Invalid %s parameter.", key);
1406 			rte_errno = EINVAL;
1407 			return -rte_errno;
1408 		}
1409 		config->dv_flow_en = tmp;
1410 	} else if (strcmp(MLX5_DV_XMETA_EN, key) == 0) {
1411 		if (tmp != MLX5_XMETA_MODE_LEGACY &&
1412 		    tmp != MLX5_XMETA_MODE_META16 &&
1413 		    tmp != MLX5_XMETA_MODE_META32 &&
1414 		    tmp != MLX5_XMETA_MODE_MISS_INFO &&
1415 		    tmp != MLX5_XMETA_MODE_META32_HWS) {
1416 			DRV_LOG(ERR, "Invalid extensive metadata parameter.");
1417 			rte_errno = EINVAL;
1418 			return -rte_errno;
1419 		}
1420 		if (tmp != MLX5_XMETA_MODE_MISS_INFO)
1421 			config->dv_xmeta_en = tmp;
1422 		else
1423 			config->dv_miss_info = 1;
1424 	} else if (strcmp(MLX5_LACP_BY_USER, key) == 0) {
1425 		config->lacp_by_user = !!tmp;
1426 	} else if (strcmp(MLX5_RECLAIM_MEM, key) == 0) {
1427 		if (tmp != MLX5_RCM_NONE &&
1428 		    tmp != MLX5_RCM_LIGHT &&
1429 		    tmp != MLX5_RCM_AGGR) {
1430 			DRV_LOG(ERR, "Unrecognize %s: \"%s\"", key, val);
1431 			rte_errno = EINVAL;
1432 			return -rte_errno;
1433 		}
1434 		config->reclaim_mode = tmp;
1435 	} else if (strcmp(MLX5_DECAP_EN, key) == 0) {
1436 		config->decap_en = !!tmp;
1437 	} else if (strcmp(MLX5_ALLOW_DUPLICATE_PATTERN, key) == 0) {
1438 		config->allow_duplicate_pattern = !!tmp;
1439 	} else if (strcmp(MLX5_FDB_DEFAULT_RULE_EN, key) == 0) {
1440 		config->fdb_def_rule = !!tmp;
1441 	} else if (strcmp(MLX5_HWS_CNT_SERVICE_CORE, key) == 0) {
1442 		config->cnt_svc.service_core = tmp;
1443 	} else if (strcmp(MLX5_HWS_CNT_CYCLE_TIME, key) == 0) {
1444 		config->cnt_svc.cycle_time = tmp;
1445 	} else if (strcmp(MLX5_REPR_MATCHING_EN, key) == 0) {
1446 		config->repr_matching = !!tmp;
1447 	}
1448 	return 0;
1449 }
1450 
1451 /**
1452  * Parse user device parameters and adjust them according to device
1453  * capabilities.
1454  *
1455  * @param sh
1456  *   Pointer to shared device context.
1457  * @param mkvlist
1458  *   Pointer to mlx5 kvargs control, can be NULL if there is no devargs.
1459  * @param config
1460  *   Pointer to shared device configuration structure.
1461  *
1462  * @return
1463  *   0 on success, a negative errno value otherwise and rte_errno is set.
1464  */
1465 static int
1466 mlx5_shared_dev_ctx_args_config(struct mlx5_dev_ctx_shared *sh,
1467 				struct mlx5_kvargs_ctrl *mkvlist,
1468 				struct mlx5_sh_config *config)
1469 {
1470 	const char **params = (const char *[]){
1471 		MLX5_TX_PP,
1472 		MLX5_TX_SKEW,
1473 		MLX5_L3_VXLAN_EN,
1474 		MLX5_VF_NL_EN,
1475 		MLX5_DV_ESW_EN,
1476 		MLX5_DV_FLOW_EN,
1477 		MLX5_DV_XMETA_EN,
1478 		MLX5_LACP_BY_USER,
1479 		MLX5_RECLAIM_MEM,
1480 		MLX5_DECAP_EN,
1481 		MLX5_ALLOW_DUPLICATE_PATTERN,
1482 		MLX5_FDB_DEFAULT_RULE_EN,
1483 		MLX5_HWS_CNT_SERVICE_CORE,
1484 		MLX5_HWS_CNT_CYCLE_TIME,
1485 		MLX5_REPR_MATCHING_EN,
1486 		NULL,
1487 	};
1488 	int ret = 0;
1489 
1490 	/* Default configuration. */
1491 	memset(config, 0, sizeof(*config));
1492 	config->vf_nl_en = 1;
1493 	config->dv_esw_en = 1;
1494 	config->dv_flow_en = 1;
1495 	config->decap_en = 1;
1496 	config->allow_duplicate_pattern = 1;
1497 	config->fdb_def_rule = 1;
1498 	config->cnt_svc.cycle_time = MLX5_CNT_SVC_CYCLE_TIME_DEFAULT;
1499 	config->cnt_svc.service_core = rte_get_main_lcore();
1500 	config->repr_matching = 1;
1501 	if (mkvlist != NULL) {
1502 		/* Process parameters. */
1503 		ret = mlx5_kvargs_process(mkvlist, params,
1504 					  mlx5_dev_args_check_handler, config);
1505 		if (ret) {
1506 			DRV_LOG(ERR, "Failed to process device arguments: %s",
1507 				strerror(rte_errno));
1508 			return -rte_errno;
1509 		}
1510 	}
1511 	/* Adjust parameters according to device capabilities. */
1512 	if (config->dv_flow_en && !sh->dev_cap.dv_flow_en) {
1513 		DRV_LOG(WARNING, "DV flow is not supported.");
1514 		config->dv_flow_en = 0;
1515 	}
1516 	if (config->dv_esw_en && !sh->dev_cap.dv_esw_en) {
1517 		DRV_LOG(DEBUG, "E-Switch DV flow is not supported.");
1518 		config->dv_esw_en = 0;
1519 	}
1520 	if (config->dv_esw_en && !config->dv_flow_en) {
1521 		DRV_LOG(DEBUG,
1522 			"E-Switch DV flow is supported only when DV flow is enabled.");
1523 		config->dv_esw_en = 0;
1524 	}
1525 	if (config->dv_miss_info && config->dv_esw_en)
1526 		config->dv_xmeta_en = MLX5_XMETA_MODE_META16;
1527 	if (!config->dv_esw_en &&
1528 	    config->dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
1529 		DRV_LOG(WARNING,
1530 			"Metadata mode %u is not supported (no E-Switch).",
1531 			config->dv_xmeta_en);
1532 		config->dv_xmeta_en = MLX5_XMETA_MODE_LEGACY;
1533 	}
1534 	if (config->dv_flow_en != 2 && !config->repr_matching) {
1535 		DRV_LOG(DEBUG, "Disabling representor matching is valid only "
1536 			       "when HW Steering is enabled.");
1537 		config->repr_matching = 1;
1538 	}
1539 	if (config->tx_pp && !sh->dev_cap.txpp_en) {
1540 		DRV_LOG(ERR, "Packet pacing is not supported.");
1541 		rte_errno = ENODEV;
1542 		return -rte_errno;
1543 	}
1544 	if (!config->tx_pp && config->tx_skew &&
1545 	    !sh->cdev->config.hca_attr.wait_on_time) {
1546 		DRV_LOG(WARNING,
1547 			"\"tx_skew\" doesn't affect without \"tx_pp\".");
1548 	}
1549 	/* Check for LRO support. */
1550 	if (mlx5_devx_obj_ops_en(sh) && sh->cdev->config.hca_attr.lro_cap) {
1551 		/* TBD check tunnel lro caps. */
1552 		config->lro_allowed = 1;
1553 		DRV_LOG(DEBUG, "LRO is allowed.");
1554 		DRV_LOG(DEBUG,
1555 			"LRO minimal size of TCP segment required for coalescing is %d bytes.",
1556 			sh->cdev->config.hca_attr.lro_min_mss_size);
1557 	}
1558 	/*
1559 	 * If HW has bug working with tunnel packet decapsulation and scatter
1560 	 * FCS, and decapsulation is needed, clear the hw_fcs_strip bit.
1561 	 * Then RTE_ETH_RX_OFFLOAD_KEEP_CRC bit will not be set anymore.
1562 	 */
1563 	if (sh->dev_cap.scatter_fcs_w_decap_disable && sh->config.decap_en)
1564 		config->hw_fcs_strip = 0;
1565 	else
1566 		config->hw_fcs_strip = sh->dev_cap.hw_fcs_strip;
1567 	DRV_LOG(DEBUG, "FCS stripping configuration is %ssupported",
1568 		(config->hw_fcs_strip ? "" : "not "));
1569 	DRV_LOG(DEBUG, "\"tx_pp\" is %d.", config->tx_pp);
1570 	DRV_LOG(DEBUG, "\"tx_skew\" is %d.", config->tx_skew);
1571 	DRV_LOG(DEBUG, "\"reclaim_mode\" is %u.", config->reclaim_mode);
1572 	DRV_LOG(DEBUG, "\"dv_esw_en\" is %u.", config->dv_esw_en);
1573 	DRV_LOG(DEBUG, "\"dv_flow_en\" is %u.", config->dv_flow_en);
1574 	DRV_LOG(DEBUG, "\"dv_xmeta_en\" is %u.", config->dv_xmeta_en);
1575 	DRV_LOG(DEBUG, "\"dv_miss_info\" is %u.", config->dv_miss_info);
1576 	DRV_LOG(DEBUG, "\"l3_vxlan_en\" is %u.", config->l3_vxlan_en);
1577 	DRV_LOG(DEBUG, "\"vf_nl_en\" is %u.", config->vf_nl_en);
1578 	DRV_LOG(DEBUG, "\"lacp_by_user\" is %u.", config->lacp_by_user);
1579 	DRV_LOG(DEBUG, "\"decap_en\" is %u.", config->decap_en);
1580 	DRV_LOG(DEBUG, "\"allow_duplicate_pattern\" is %u.",
1581 		config->allow_duplicate_pattern);
1582 	DRV_LOG(DEBUG, "\"fdb_def_rule_en\" is %u.", config->fdb_def_rule);
1583 	DRV_LOG(DEBUG, "\"repr_matching_en\" is %u.", config->repr_matching);
1584 	return 0;
1585 }
1586 
1587 /**
1588  * Configure realtime timestamp format.
1589  *
1590  * @param sh
1591  *   Pointer to mlx5_dev_ctx_shared object.
1592  * @param hca_attr
1593  *   Pointer to DevX HCA capabilities structure.
1594  */
1595 void
1596 mlx5_rt_timestamp_config(struct mlx5_dev_ctx_shared *sh,
1597 			 struct mlx5_hca_attr *hca_attr)
1598 {
1599 	uint32_t dw_cnt = MLX5_ST_SZ_DW(register_mtutc);
1600 	uint32_t reg[dw_cnt];
1601 	int ret = ENOTSUP;
1602 
1603 	if (hca_attr->access_register_user)
1604 		ret = mlx5_devx_cmd_register_read(sh->cdev->ctx,
1605 						  MLX5_REGISTER_ID_MTUTC, 0,
1606 						  reg, dw_cnt);
1607 	if (!ret) {
1608 		uint32_t ts_mode;
1609 
1610 		/* MTUTC register is read successfully. */
1611 		ts_mode = MLX5_GET(register_mtutc, reg, time_stamp_mode);
1612 		if (ts_mode == MLX5_MTUTC_TIMESTAMP_MODE_REAL_TIME)
1613 			sh->dev_cap.rt_timestamp = 1;
1614 	} else {
1615 		/* Kernel does not support register reading. */
1616 		if (hca_attr->dev_freq_khz == (NS_PER_S / MS_PER_S))
1617 			sh->dev_cap.rt_timestamp = 1;
1618 	}
1619 }
1620 
1621 static void
1622 mlx5_init_hws_flow_tags_registers(struct mlx5_dev_ctx_shared *sh)
1623 {
1624 	struct mlx5_dev_registers *reg = &sh->registers;
1625 	uint32_t meta_mode = sh->config.dv_xmeta_en;
1626 	uint16_t masks = (uint16_t)sh->cdev->config.hca_attr.set_reg_c;
1627 	uint16_t unset = 0;
1628 	uint32_t i, j;
1629 
1630 	/*
1631 	 * The CAPA is global for common device but only used in net.
1632 	 * It is shared per eswitch domain.
1633 	 */
1634 	if (reg->aso_reg != REG_NON)
1635 		unset |= 1 << mlx5_regc_index(reg->aso_reg);
1636 	unset |= 1 << mlx5_regc_index(REG_C_6);
1637 	if (sh->config.dv_esw_en)
1638 		unset |= 1 << mlx5_regc_index(REG_C_0);
1639 	if (meta_mode == MLX5_XMETA_MODE_META32_HWS)
1640 		unset |= 1 << mlx5_regc_index(REG_C_1);
1641 	masks &= ~unset;
1642 	for (i = 0, j = 0; i < MLX5_FLOW_HW_TAGS_MAX; i++) {
1643 		if (!!((1 << i) & masks))
1644 			reg->hw_avl_tags[j++] = mlx5_regc_value(i);
1645 	}
1646 }
1647 
1648 static void
1649 mlx5_init_aso_register(struct mlx5_dev_ctx_shared *sh)
1650 {
1651 #if defined(HAVE_MLX5_DR_CREATE_ACTION_ASO_EXT)
1652 	const struct mlx5_hca_attr *hca_attr = &sh->cdev->config.hca_attr;
1653 	const struct mlx5_hca_qos_attr *qos =  &hca_attr->qos;
1654 	uint8_t reg_c_mask = qos->flow_meter_reg_c_ids & 0xfc;
1655 
1656 	if (!(qos->sup && qos->flow_meter_old && sh->config.dv_flow_en))
1657 		return;
1658 	/*
1659 	 * Meter needs two REG_C's for color match and pre-sfx
1660 	 * flow match. Here get the REG_C for color match.
1661 	 * REG_C_0 and REG_C_1 is reserved for metadata feature.
1662 	 */
1663 	if (rte_popcount32(reg_c_mask) > 0) {
1664 		/*
1665 		 * The meter color register is used by the
1666 		 * flow-hit feature as well.
1667 		 * The flow-hit feature must use REG_C_3
1668 		 * Prefer REG_C_3 if it is available.
1669 		 */
1670 		if (reg_c_mask & (1 << mlx5_regc_index(REG_C_3)))
1671 			sh->registers.aso_reg = REG_C_3;
1672 		else
1673 			sh->registers.aso_reg =
1674 				mlx5_regc_value(ffs(reg_c_mask) - 1);
1675 	}
1676 #else
1677 	RTE_SET_USED(sh);
1678 #endif
1679 }
1680 
1681 static void
1682 mlx5_init_shared_dev_registers(struct mlx5_dev_ctx_shared *sh)
1683 {
1684 	if (sh->cdev->config.devx)
1685 		mlx5_init_aso_register(sh);
1686 	if (sh->registers.aso_reg != REG_NON) {
1687 		DRV_LOG(DEBUG, "ASO register: REG_C%d",
1688 			mlx5_regc_index(sh->registers.aso_reg));
1689 	} else {
1690 		DRV_LOG(DEBUG, "ASO register: NONE");
1691 	}
1692 	mlx5_init_hws_flow_tags_registers(sh);
1693 }
1694 
1695 /**
1696  * Allocate shared device context. If there is multiport device the
1697  * master and representors will share this context, if there is single
1698  * port dedicated device, the context will be used by only given
1699  * port due to unification.
1700  *
1701  * Routine first searches the context for the specified device name,
1702  * if found the shared context assumed and reference counter is incremented.
1703  * If no context found the new one is created and initialized with specified
1704  * device context and parameters.
1705  *
1706  * @param[in] spawn
1707  *   Pointer to the device attributes (name, port, etc).
1708  * @param mkvlist
1709  *   Pointer to mlx5 kvargs control, can be NULL if there is no devargs.
1710  *
1711  * @return
1712  *   Pointer to mlx5_dev_ctx_shared object on success,
1713  *   otherwise NULL and rte_errno is set.
1714  */
1715 struct mlx5_dev_ctx_shared *
1716 mlx5_alloc_shared_dev_ctx(const struct mlx5_dev_spawn_data *spawn,
1717 			  struct mlx5_kvargs_ctrl *mkvlist)
1718 {
1719 	struct mlx5_dev_ctx_shared *sh;
1720 	int err = 0;
1721 	uint32_t i;
1722 
1723 	MLX5_ASSERT(spawn);
1724 	/* Secondary process should not create the shared context. */
1725 	MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
1726 	pthread_mutex_lock(&mlx5_dev_ctx_list_mutex);
1727 	/* Search for IB context by device name. */
1728 	LIST_FOREACH(sh, &mlx5_dev_ctx_list, next) {
1729 		if (!strcmp(sh->ibdev_name, spawn->phys_dev_name)) {
1730 			sh->refcnt++;
1731 			goto exit;
1732 		}
1733 	}
1734 	/* No device found, we have to create new shared context. */
1735 	MLX5_ASSERT(spawn->max_port);
1736 	sh = mlx5_malloc(MLX5_MEM_ZERO | MLX5_MEM_RTE,
1737 			 sizeof(struct mlx5_dev_ctx_shared) +
1738 			 spawn->max_port * sizeof(struct mlx5_dev_shared_port),
1739 			 RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
1740 	if (!sh) {
1741 		DRV_LOG(ERR, "Shared context allocation failure.");
1742 		rte_errno = ENOMEM;
1743 		goto exit;
1744 	}
1745 	pthread_mutex_init(&sh->txpp.mutex, NULL);
1746 	sh->numa_node = spawn->cdev->dev->numa_node;
1747 	sh->cdev = spawn->cdev;
1748 	sh->esw_mode = !!(spawn->info.master || spawn->info.representor);
1749 	if (spawn->bond_info)
1750 		sh->bond = *spawn->bond_info;
1751 	err = mlx5_os_capabilities_prepare(sh);
1752 	if (err) {
1753 		DRV_LOG(ERR, "Fail to configure device capabilities.");
1754 		goto error;
1755 	}
1756 	err = mlx5_shared_dev_ctx_args_config(sh, mkvlist, &sh->config);
1757 	if (err) {
1758 		DRV_LOG(ERR, "Failed to process device configure: %s",
1759 			strerror(rte_errno));
1760 		goto error;
1761 	}
1762 	sh->refcnt = 1;
1763 	sh->max_port = spawn->max_port;
1764 	strncpy(sh->ibdev_name, mlx5_os_get_ctx_device_name(sh->cdev->ctx),
1765 		sizeof(sh->ibdev_name) - 1);
1766 	strncpy(sh->ibdev_path, mlx5_os_get_ctx_device_path(sh->cdev->ctx),
1767 		sizeof(sh->ibdev_path) - 1);
1768 	/*
1769 	 * Setting port_id to max unallowed value means there is no interrupt
1770 	 * subhandler installed for the given port index i.
1771 	 */
1772 	for (i = 0; i < sh->max_port; i++) {
1773 		sh->port[i].ih_port_id = RTE_MAX_ETHPORTS;
1774 		sh->port[i].devx_ih_port_id = RTE_MAX_ETHPORTS;
1775 		sh->port[i].nl_ih_port_id = RTE_MAX_ETHPORTS;
1776 	}
1777 	if (sh->cdev->config.devx) {
1778 		sh->td = mlx5_devx_cmd_create_td(sh->cdev->ctx);
1779 		if (!sh->td) {
1780 			DRV_LOG(ERR, "TD allocation failure");
1781 			rte_errno = ENOMEM;
1782 			goto error;
1783 		}
1784 		if (mlx5_setup_tis(sh)) {
1785 			DRV_LOG(ERR, "TIS allocation failure");
1786 			rte_errno = ENOMEM;
1787 			goto error;
1788 		}
1789 		err = mlx5_rxtx_uars_prepare(sh);
1790 		if (err)
1791 			goto error;
1792 #ifndef RTE_ARCH_64
1793 	} else {
1794 		/* Initialize UAR access locks for 32bit implementations. */
1795 		rte_spinlock_init(&sh->uar_lock_cq);
1796 		for (i = 0; i < MLX5_UAR_PAGE_NUM_MAX; i++)
1797 			rte_spinlock_init(&sh->uar_lock[i]);
1798 #endif
1799 	}
1800 	mlx5_os_dev_shared_handler_install(sh);
1801 	if (LIST_EMPTY(&mlx5_dev_ctx_list)) {
1802 		err = mlx5_flow_os_init_workspace_once();
1803 		if (err)
1804 			goto error;
1805 	}
1806 	err = mlx5_flow_counters_mng_init(sh);
1807 	if (err) {
1808 		DRV_LOG(ERR, "Fail to initialize counters manage.");
1809 		goto error;
1810 	}
1811 	mlx5_flow_aging_init(sh);
1812 	mlx5_flow_ipool_create(sh);
1813 	/* Add context to the global device list. */
1814 	LIST_INSERT_HEAD(&mlx5_dev_ctx_list, sh, next);
1815 	rte_spinlock_init(&sh->geneve_tlv_opt_sl);
1816 	mlx5_init_shared_dev_registers(sh);
1817 exit:
1818 	pthread_mutex_unlock(&mlx5_dev_ctx_list_mutex);
1819 	return sh;
1820 error:
1821 	err = rte_errno;
1822 	pthread_mutex_destroy(&sh->txpp.mutex);
1823 	pthread_mutex_unlock(&mlx5_dev_ctx_list_mutex);
1824 	MLX5_ASSERT(sh);
1825 	mlx5_rxtx_uars_release(sh);
1826 	i = 0;
1827 	do {
1828 		if (sh->tis[i])
1829 			claim_zero(mlx5_devx_cmd_destroy(sh->tis[i]));
1830 	} while (++i <= (uint32_t)sh->bond.n_port);
1831 	if (sh->td)
1832 		claim_zero(mlx5_devx_cmd_destroy(sh->td));
1833 	mlx5_free(sh);
1834 	rte_errno = err;
1835 	return NULL;
1836 }
1837 
1838 /**
1839  * Create LWM event_channel and interrupt handle for shared device
1840  * context. All rxqs sharing the device context share the event_channel.
1841  * A callback is registered in interrupt thread to receive the LWM event.
1842  *
1843  * @param[in] priv
1844  *   Pointer to mlx5_priv instance.
1845  *
1846  * @return
1847  *   0 on success, negative with rte_errno set.
1848  */
1849 int
1850 mlx5_lwm_setup(struct mlx5_priv *priv)
1851 {
1852 	int fd_lwm;
1853 
1854 	pthread_mutex_init(&priv->sh->lwm_config_lock, NULL);
1855 	priv->sh->devx_channel_lwm = mlx5_os_devx_create_event_channel
1856 			(priv->sh->cdev->ctx,
1857 			 MLX5DV_DEVX_CREATE_EVENT_CHANNEL_FLAGS_OMIT_EV_DATA);
1858 	if (!priv->sh->devx_channel_lwm)
1859 		goto err;
1860 	fd_lwm = mlx5_os_get_devx_channel_fd(priv->sh->devx_channel_lwm);
1861 	priv->sh->intr_handle_lwm = mlx5_os_interrupt_handler_create
1862 		(RTE_INTR_INSTANCE_F_SHARED, true,
1863 		 fd_lwm, mlx5_dev_interrupt_handler_lwm, priv);
1864 	if (!priv->sh->intr_handle_lwm)
1865 		goto err;
1866 	return 0;
1867 err:
1868 	if (priv->sh->devx_channel_lwm) {
1869 		mlx5_os_devx_destroy_event_channel
1870 			(priv->sh->devx_channel_lwm);
1871 		priv->sh->devx_channel_lwm = NULL;
1872 	}
1873 	pthread_mutex_destroy(&priv->sh->lwm_config_lock);
1874 	return -rte_errno;
1875 }
1876 
1877 /**
1878  * Destroy LWM event_channel and interrupt handle for shared device
1879  * context before free this context. The interrupt handler is also
1880  * unregistered.
1881  *
1882  * @param[in] sh
1883  *   Pointer to shared device context.
1884  */
1885 void
1886 mlx5_lwm_unset(struct mlx5_dev_ctx_shared *sh)
1887 {
1888 	if (sh->intr_handle_lwm) {
1889 		mlx5_os_interrupt_handler_destroy(sh->intr_handle_lwm,
1890 			mlx5_dev_interrupt_handler_lwm, (void *)-1);
1891 		sh->intr_handle_lwm = NULL;
1892 	}
1893 	if (sh->devx_channel_lwm) {
1894 		mlx5_os_devx_destroy_event_channel
1895 			(sh->devx_channel_lwm);
1896 		sh->devx_channel_lwm = NULL;
1897 	}
1898 	pthread_mutex_destroy(&sh->lwm_config_lock);
1899 }
1900 
1901 /**
1902  * Free shared IB device context. Decrement counter and if zero free
1903  * all allocated resources and close handles.
1904  *
1905  * @param[in] sh
1906  *   Pointer to mlx5_dev_ctx_shared object to free
1907  */
1908 void
1909 mlx5_free_shared_dev_ctx(struct mlx5_dev_ctx_shared *sh)
1910 {
1911 	int ret;
1912 	int i = 0;
1913 
1914 	pthread_mutex_lock(&mlx5_dev_ctx_list_mutex);
1915 #ifdef RTE_LIBRTE_MLX5_DEBUG
1916 	/* Check the object presence in the list. */
1917 	struct mlx5_dev_ctx_shared *lctx;
1918 
1919 	LIST_FOREACH(lctx, &mlx5_dev_ctx_list, next)
1920 		if (lctx == sh)
1921 			break;
1922 	MLX5_ASSERT(lctx);
1923 	if (lctx != sh) {
1924 		DRV_LOG(ERR, "Freeing non-existing shared IB context");
1925 		goto exit;
1926 	}
1927 #endif
1928 	MLX5_ASSERT(sh);
1929 	MLX5_ASSERT(sh->refcnt);
1930 	/* Secondary process should not free the shared context. */
1931 	MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
1932 	if (--sh->refcnt)
1933 		goto exit;
1934 	/* Stop watching for mempool events and unregister all mempools. */
1935 	if (!sh->cdev->config.mr_mempool_reg_en) {
1936 		ret = rte_mempool_event_callback_unregister
1937 				(mlx5_dev_ctx_shared_rx_mempool_event_cb, sh);
1938 		if (ret == 0)
1939 			rte_mempool_walk
1940 			     (mlx5_dev_ctx_shared_rx_mempool_unregister_cb, sh);
1941 	}
1942 	/* Remove context from the global device list. */
1943 	LIST_REMOVE(sh, next);
1944 	/* Release resources on the last device removal. */
1945 	if (LIST_EMPTY(&mlx5_dev_ctx_list)) {
1946 		mlx5_os_net_cleanup();
1947 		mlx5_flow_os_release_workspace();
1948 	}
1949 	pthread_mutex_unlock(&mlx5_dev_ctx_list_mutex);
1950 	if (sh->flex_parsers_dv) {
1951 		mlx5_list_destroy(sh->flex_parsers_dv);
1952 		sh->flex_parsers_dv = NULL;
1953 	}
1954 	/*
1955 	 *  Ensure there is no async event handler installed.
1956 	 *  Only primary process handles async device events.
1957 	 **/
1958 	mlx5_flow_counters_mng_close(sh);
1959 	if (sh->ct_mng)
1960 		mlx5_flow_aso_ct_mng_close(sh);
1961 	if (sh->aso_age_mng) {
1962 		mlx5_flow_aso_age_mng_close(sh);
1963 		sh->aso_age_mng = NULL;
1964 	}
1965 	if (sh->mtrmng)
1966 		mlx5_aso_flow_mtrs_mng_close(sh);
1967 	mlx5_flow_ipool_destroy(sh);
1968 	mlx5_os_dev_shared_handler_uninstall(sh);
1969 	mlx5_rxtx_uars_release(sh);
1970 	do {
1971 		if (sh->tis[i])
1972 			claim_zero(mlx5_devx_cmd_destroy(sh->tis[i]));
1973 	} while (++i <= sh->bond.n_port);
1974 	if (sh->td)
1975 		claim_zero(mlx5_devx_cmd_destroy(sh->td));
1976 #ifdef HAVE_MLX5_HWS_SUPPORT
1977 	/* HWS manages geneve_tlv_option resource as global. */
1978 	if (sh->config.dv_flow_en == 2)
1979 		flow_dev_geneve_tlv_option_resource_release(sh);
1980 	else
1981 #endif
1982 		MLX5_ASSERT(sh->geneve_tlv_option_resource == NULL);
1983 	pthread_mutex_destroy(&sh->txpp.mutex);
1984 	mlx5_lwm_unset(sh);
1985 	mlx5_free(sh);
1986 	return;
1987 exit:
1988 	pthread_mutex_unlock(&mlx5_dev_ctx_list_mutex);
1989 }
1990 
1991 /**
1992  * Destroy table hash list.
1993  *
1994  * @param[in] priv
1995  *   Pointer to the private device data structure.
1996  */
1997 void
1998 mlx5_free_table_hash_list(struct mlx5_priv *priv)
1999 {
2000 	struct mlx5_dev_ctx_shared *sh = priv->sh;
2001 	struct mlx5_hlist **tbls = (priv->sh->config.dv_flow_en == 2) ?
2002 				   &sh->groups : &sh->flow_tbls;
2003 	if (*tbls == NULL)
2004 		return;
2005 	mlx5_hlist_destroy(*tbls);
2006 	*tbls = NULL;
2007 }
2008 
2009 #ifdef HAVE_MLX5_HWS_SUPPORT
2010 /**
2011  * Allocate HW steering group hash list.
2012  *
2013  * @param[in] priv
2014  *   Pointer to the private device data structure.
2015  */
2016 static int
2017 mlx5_alloc_hw_group_hash_list(struct mlx5_priv *priv)
2018 {
2019 	int err = 0;
2020 	struct mlx5_dev_ctx_shared *sh = priv->sh;
2021 	char s[MLX5_NAME_SIZE];
2022 
2023 	MLX5_ASSERT(sh);
2024 	snprintf(s, sizeof(s), "%s_flow_groups", priv->sh->ibdev_name);
2025 	sh->groups = mlx5_hlist_create
2026 			(s, MLX5_FLOW_TABLE_HLIST_ARRAY_SIZE,
2027 			 false, true, sh,
2028 			 flow_hw_grp_create_cb,
2029 			 flow_hw_grp_match_cb,
2030 			 flow_hw_grp_remove_cb,
2031 			 flow_hw_grp_clone_cb,
2032 			 flow_hw_grp_clone_free_cb);
2033 	if (!sh->groups) {
2034 		DRV_LOG(ERR, "flow groups with hash creation failed.");
2035 		err = ENOMEM;
2036 	}
2037 	return err;
2038 }
2039 #endif
2040 
2041 
2042 /**
2043  * Initialize flow table hash list and create the root tables entry
2044  * for each domain.
2045  *
2046  * @param[in] priv
2047  *   Pointer to the private device data structure.
2048  *
2049  * @return
2050  *   Zero on success, positive error code otherwise.
2051  */
2052 int
2053 mlx5_alloc_table_hash_list(struct mlx5_priv *priv __rte_unused)
2054 {
2055 	int err = 0;
2056 
2057 	/* Tables are only used in DV and DR modes. */
2058 #if defined(HAVE_IBV_FLOW_DV_SUPPORT) || !defined(HAVE_INFINIBAND_VERBS_H)
2059 	struct mlx5_dev_ctx_shared *sh = priv->sh;
2060 	char s[MLX5_NAME_SIZE];
2061 
2062 #ifdef HAVE_MLX5_HWS_SUPPORT
2063 	if (priv->sh->config.dv_flow_en == 2)
2064 		return mlx5_alloc_hw_group_hash_list(priv);
2065 #endif
2066 	MLX5_ASSERT(sh);
2067 	snprintf(s, sizeof(s), "%s_flow_table", priv->sh->ibdev_name);
2068 	sh->flow_tbls = mlx5_hlist_create(s, MLX5_FLOW_TABLE_HLIST_ARRAY_SIZE,
2069 					  false, true, sh,
2070 					  flow_dv_tbl_create_cb,
2071 					  flow_dv_tbl_match_cb,
2072 					  flow_dv_tbl_remove_cb,
2073 					  flow_dv_tbl_clone_cb,
2074 					  flow_dv_tbl_clone_free_cb);
2075 	if (!sh->flow_tbls) {
2076 		DRV_LOG(ERR, "flow tables with hash creation failed.");
2077 		err = ENOMEM;
2078 		return err;
2079 	}
2080 #ifndef HAVE_MLX5DV_DR
2081 	struct rte_flow_error error;
2082 	struct rte_eth_dev *dev = &rte_eth_devices[priv->dev_data->port_id];
2083 
2084 	/*
2085 	 * In case we have not DR support, the zero tables should be created
2086 	 * because DV expect to see them even if they cannot be created by
2087 	 * RDMA-CORE.
2088 	 */
2089 	if (!flow_dv_tbl_resource_get(dev, 0, 0, 0, 0,
2090 		NULL, 0, 1, 0, &error) ||
2091 	    !flow_dv_tbl_resource_get(dev, 0, 1, 0, 0,
2092 		NULL, 0, 1, 0, &error) ||
2093 	    !flow_dv_tbl_resource_get(dev, 0, 0, 1, 0,
2094 		NULL, 0, 1, 0, &error)) {
2095 		err = ENOMEM;
2096 		goto error;
2097 	}
2098 	return err;
2099 error:
2100 	mlx5_free_table_hash_list(priv);
2101 #endif /* HAVE_MLX5DV_DR */
2102 #endif
2103 	return err;
2104 }
2105 
2106 /**
2107  * Retrieve integer value from environment variable.
2108  *
2109  * @param[in] name
2110  *   Environment variable name.
2111  *
2112  * @return
2113  *   Integer value, 0 if the variable is not set.
2114  */
2115 int
2116 mlx5_getenv_int(const char *name)
2117 {
2118 	const char *val = getenv(name);
2119 
2120 	if (val == NULL)
2121 		return 0;
2122 	return atoi(val);
2123 }
2124 
2125 /**
2126  * DPDK callback to add udp tunnel port
2127  *
2128  * @param[in] dev
2129  *   A pointer to eth_dev
2130  * @param[in] udp_tunnel
2131  *   A pointer to udp tunnel
2132  *
2133  * @return
2134  *   0 on valid udp ports and tunnels, -ENOTSUP otherwise.
2135  */
2136 int
2137 mlx5_udp_tunnel_port_add(struct rte_eth_dev *dev __rte_unused,
2138 			 struct rte_eth_udp_tunnel *udp_tunnel)
2139 {
2140 	MLX5_ASSERT(udp_tunnel != NULL);
2141 	if (udp_tunnel->prot_type == RTE_ETH_TUNNEL_TYPE_VXLAN &&
2142 	    udp_tunnel->udp_port == 4789)
2143 		return 0;
2144 	if (udp_tunnel->prot_type == RTE_ETH_TUNNEL_TYPE_VXLAN_GPE &&
2145 	    udp_tunnel->udp_port == 4790)
2146 		return 0;
2147 	return -ENOTSUP;
2148 }
2149 
2150 /**
2151  * Initialize process private data structure.
2152  *
2153  * @param dev
2154  *   Pointer to Ethernet device structure.
2155  *
2156  * @return
2157  *   0 on success, a negative errno value otherwise and rte_errno is set.
2158  */
2159 int
2160 mlx5_proc_priv_init(struct rte_eth_dev *dev)
2161 {
2162 	struct mlx5_priv *priv = dev->data->dev_private;
2163 	struct mlx5_proc_priv *ppriv;
2164 	size_t ppriv_size;
2165 
2166 	mlx5_proc_priv_uninit(dev);
2167 	/*
2168 	 * UAR register table follows the process private structure. BlueFlame
2169 	 * registers for Tx queues are stored in the table.
2170 	 */
2171 	ppriv_size = sizeof(struct mlx5_proc_priv) +
2172 		     priv->txqs_n * sizeof(struct mlx5_uar_data);
2173 	ppriv = mlx5_malloc(MLX5_MEM_RTE | MLX5_MEM_ZERO, ppriv_size,
2174 			    RTE_CACHE_LINE_SIZE, dev->device->numa_node);
2175 	if (!ppriv) {
2176 		rte_errno = ENOMEM;
2177 		return -rte_errno;
2178 	}
2179 	ppriv->uar_table_sz = priv->txqs_n;
2180 	dev->process_private = ppriv;
2181 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
2182 		priv->sh->pppriv = ppriv;
2183 	return 0;
2184 }
2185 
2186 /**
2187  * Un-initialize process private data structure.
2188  *
2189  * @param dev
2190  *   Pointer to Ethernet device structure.
2191  */
2192 void
2193 mlx5_proc_priv_uninit(struct rte_eth_dev *dev)
2194 {
2195 	struct mlx5_proc_priv *ppriv = dev->process_private;
2196 
2197 	if (!ppriv)
2198 		return;
2199 	if (ppriv->hca_bar)
2200 		mlx5_txpp_unmap_hca_bar(dev);
2201 	mlx5_free(dev->process_private);
2202 	dev->process_private = NULL;
2203 }
2204 
2205 /**
2206  * DPDK callback to close the device.
2207  *
2208  * Destroy all queues and objects, free memory.
2209  *
2210  * @param dev
2211  *   Pointer to Ethernet device structure.
2212  */
2213 int
2214 mlx5_dev_close(struct rte_eth_dev *dev)
2215 {
2216 	struct mlx5_priv *priv = dev->data->dev_private;
2217 	unsigned int i;
2218 	int ret;
2219 
2220 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
2221 		/* Check if process_private released. */
2222 		if (!dev->process_private)
2223 			return 0;
2224 		mlx5_tx_uar_uninit_secondary(dev);
2225 		mlx5_proc_priv_uninit(dev);
2226 		rte_eth_dev_release_port(dev);
2227 		return 0;
2228 	}
2229 	if (!priv->sh)
2230 		return 0;
2231 	if (priv->shared_refcnt) {
2232 		DRV_LOG(ERR, "port %u is shared host in use (%u)",
2233 			dev->data->port_id, priv->shared_refcnt);
2234 		rte_errno = EBUSY;
2235 		return -EBUSY;
2236 	}
2237 	DRV_LOG(DEBUG, "port %u closing device \"%s\"",
2238 		dev->data->port_id,
2239 		((priv->sh->cdev->ctx != NULL) ?
2240 		mlx5_os_get_ctx_device_name(priv->sh->cdev->ctx) : ""));
2241 	/*
2242 	 * If default mreg copy action is removed at the stop stage,
2243 	 * the search will return none and nothing will be done anymore.
2244 	 */
2245 	if (priv->sh->config.dv_flow_en != 2)
2246 		mlx5_flow_stop_default(dev);
2247 	mlx5_traffic_disable(dev);
2248 	/*
2249 	 * If all the flows are already flushed in the device stop stage,
2250 	 * then this will return directly without any action.
2251 	 */
2252 	mlx5_flow_list_flush(dev, MLX5_FLOW_TYPE_GEN, true);
2253 	mlx5_action_handle_flush(dev);
2254 	mlx5_flow_meter_flush(dev, NULL);
2255 	/* Prevent crashes when queues are still in use. */
2256 	dev->rx_pkt_burst = rte_eth_pkt_burst_dummy;
2257 	dev->tx_pkt_burst = rte_eth_pkt_burst_dummy;
2258 	rte_wmb();
2259 	/* Disable datapath on secondary process. */
2260 	mlx5_mp_os_req_stop_rxtx(dev);
2261 	/* Free the eCPRI flex parser resource. */
2262 	mlx5_flex_parser_ecpri_release(dev);
2263 	mlx5_flex_item_port_cleanup(dev);
2264 	mlx5_indirect_list_handles_release(dev);
2265 #ifdef HAVE_MLX5_HWS_SUPPORT
2266 	flow_hw_destroy_vport_action(dev);
2267 	flow_hw_resource_release(dev);
2268 	flow_hw_clear_port_info(dev);
2269 #endif
2270 	if (priv->rxq_privs != NULL) {
2271 		/* XXX race condition if mlx5_rx_burst() is still running. */
2272 		rte_delay_us_sleep(1000);
2273 		for (i = 0; (i != priv->rxqs_n); ++i)
2274 			mlx5_rxq_release(dev, i);
2275 		priv->rxqs_n = 0;
2276 		mlx5_free(priv->rxq_privs);
2277 		priv->rxq_privs = NULL;
2278 	}
2279 	if (priv->txqs != NULL) {
2280 		/* XXX race condition if mlx5_tx_burst() is still running. */
2281 		rte_delay_us_sleep(1000);
2282 		for (i = 0; (i != priv->txqs_n); ++i)
2283 			mlx5_txq_release(dev, i);
2284 		priv->txqs_n = 0;
2285 		priv->txqs = NULL;
2286 	}
2287 	mlx5_proc_priv_uninit(dev);
2288 	if (priv->q_counters) {
2289 		mlx5_devx_cmd_destroy(priv->q_counters);
2290 		priv->q_counters = NULL;
2291 	}
2292 	if (priv->drop_queue.hrxq)
2293 		mlx5_drop_action_destroy(dev);
2294 	if (priv->mreg_cp_tbl)
2295 		mlx5_hlist_destroy(priv->mreg_cp_tbl);
2296 	mlx5_mprq_free_mp(dev);
2297 	mlx5_os_free_shared_dr(priv);
2298 	if (priv->rss_conf.rss_key != NULL)
2299 		mlx5_free(priv->rss_conf.rss_key);
2300 	if (priv->reta_idx != NULL)
2301 		mlx5_free(priv->reta_idx);
2302 	if (priv->sh->dev_cap.vf)
2303 		mlx5_os_mac_addr_flush(dev);
2304 	if (priv->nl_socket_route >= 0)
2305 		close(priv->nl_socket_route);
2306 	if (priv->nl_socket_rdma >= 0)
2307 		close(priv->nl_socket_rdma);
2308 	if (priv->vmwa_context)
2309 		mlx5_vlan_vmwa_exit(priv->vmwa_context);
2310 	ret = mlx5_hrxq_verify(dev);
2311 	if (ret)
2312 		DRV_LOG(WARNING, "port %u some hash Rx queue still remain",
2313 			dev->data->port_id);
2314 	ret = mlx5_ind_table_obj_verify(dev);
2315 	if (ret)
2316 		DRV_LOG(WARNING, "port %u some indirection table still remain",
2317 			dev->data->port_id);
2318 	ret = mlx5_rxq_obj_verify(dev);
2319 	if (ret)
2320 		DRV_LOG(WARNING, "port %u some Rx queue objects still remain",
2321 			dev->data->port_id);
2322 	ret = mlx5_ext_rxq_verify(dev);
2323 	if (ret)
2324 		DRV_LOG(WARNING, "Port %u some external RxQ still remain.",
2325 			dev->data->port_id);
2326 	ret = mlx5_rxq_verify(dev);
2327 	if (ret)
2328 		DRV_LOG(WARNING, "port %u some Rx queues still remain",
2329 			dev->data->port_id);
2330 	ret = mlx5_txq_obj_verify(dev);
2331 	if (ret)
2332 		DRV_LOG(WARNING, "port %u some Verbs Tx queue still remain",
2333 			dev->data->port_id);
2334 	ret = mlx5_txq_verify(dev);
2335 	if (ret)
2336 		DRV_LOG(WARNING, "port %u some Tx queues still remain",
2337 			dev->data->port_id);
2338 	ret = mlx5_flow_verify(dev);
2339 	if (ret)
2340 		DRV_LOG(WARNING, "port %u some flows still remain",
2341 			dev->data->port_id);
2342 	if (priv->hrxqs)
2343 		mlx5_list_destroy(priv->hrxqs);
2344 	mlx5_free(priv->ext_rxqs);
2345 	priv->sh->port[priv->dev_port - 1].nl_ih_port_id = RTE_MAX_ETHPORTS;
2346 	/*
2347 	 * The interrupt handler port id must be reset before priv is reset
2348 	 * since 'mlx5_dev_interrupt_nl_cb' uses priv.
2349 	 */
2350 	rte_io_wmb();
2351 	/*
2352 	 * Free the shared context in last turn, because the cleanup
2353 	 * routines above may use some shared fields, like
2354 	 * mlx5_os_mac_addr_flush() uses ibdev_path for retrieving
2355 	 * ifindex if Netlink fails.
2356 	 */
2357 	mlx5_free_shared_dev_ctx(priv->sh);
2358 	if (priv->domain_id != RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
2359 		unsigned int c = 0;
2360 		uint16_t port_id;
2361 
2362 		MLX5_ETH_FOREACH_DEV(port_id, dev->device) {
2363 			struct mlx5_priv *opriv =
2364 				rte_eth_devices[port_id].data->dev_private;
2365 
2366 			if (!opriv ||
2367 			    opriv->domain_id != priv->domain_id ||
2368 			    &rte_eth_devices[port_id] == dev)
2369 				continue;
2370 			++c;
2371 			break;
2372 		}
2373 		if (!c)
2374 			claim_zero(rte_eth_switch_domain_free(priv->domain_id));
2375 	}
2376 	memset(priv, 0, sizeof(*priv));
2377 	priv->domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
2378 	/*
2379 	 * Reset mac_addrs to NULL such that it is not freed as part of
2380 	 * rte_eth_dev_release_port(). mac_addrs is part of dev_private so
2381 	 * it is freed when dev_private is freed.
2382 	 */
2383 	dev->data->mac_addrs = NULL;
2384 	return 0;
2385 }
2386 
2387 const struct eth_dev_ops mlx5_dev_ops = {
2388 	.dev_configure = mlx5_dev_configure,
2389 	.dev_start = mlx5_dev_start,
2390 	.dev_stop = mlx5_dev_stop,
2391 	.dev_set_link_down = mlx5_set_link_down,
2392 	.dev_set_link_up = mlx5_set_link_up,
2393 	.dev_close = mlx5_dev_close,
2394 	.promiscuous_enable = mlx5_promiscuous_enable,
2395 	.promiscuous_disable = mlx5_promiscuous_disable,
2396 	.allmulticast_enable = mlx5_allmulticast_enable,
2397 	.allmulticast_disable = mlx5_allmulticast_disable,
2398 	.link_update = mlx5_link_update,
2399 	.stats_get = mlx5_stats_get,
2400 	.stats_reset = mlx5_stats_reset,
2401 	.xstats_get = mlx5_xstats_get,
2402 	.xstats_reset = mlx5_xstats_reset,
2403 	.xstats_get_names = mlx5_xstats_get_names,
2404 	.fw_version_get = mlx5_fw_version_get,
2405 	.dev_infos_get = mlx5_dev_infos_get,
2406 	.representor_info_get = mlx5_representor_info_get,
2407 	.read_clock = mlx5_txpp_read_clock,
2408 	.dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
2409 	.vlan_filter_set = mlx5_vlan_filter_set,
2410 	.rx_queue_setup = mlx5_rx_queue_setup,
2411 	.rx_queue_avail_thresh_set = mlx5_rx_queue_lwm_set,
2412 	.rx_queue_avail_thresh_query = mlx5_rx_queue_lwm_query,
2413 	.rx_hairpin_queue_setup = mlx5_rx_hairpin_queue_setup,
2414 	.tx_queue_setup = mlx5_tx_queue_setup,
2415 	.tx_hairpin_queue_setup = mlx5_tx_hairpin_queue_setup,
2416 	.rx_queue_release = mlx5_rx_queue_release,
2417 	.tx_queue_release = mlx5_tx_queue_release,
2418 	.rx_queue_start = mlx5_rx_queue_start,
2419 	.rx_queue_stop = mlx5_rx_queue_stop,
2420 	.tx_queue_start = mlx5_tx_queue_start,
2421 	.tx_queue_stop = mlx5_tx_queue_stop,
2422 	.flow_ctrl_get = mlx5_dev_get_flow_ctrl,
2423 	.flow_ctrl_set = mlx5_dev_set_flow_ctrl,
2424 	.mac_addr_remove = mlx5_mac_addr_remove,
2425 	.mac_addr_add = mlx5_mac_addr_add,
2426 	.mac_addr_set = mlx5_mac_addr_set,
2427 	.set_mc_addr_list = mlx5_set_mc_addr_list,
2428 	.mtu_set = mlx5_dev_set_mtu,
2429 	.vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
2430 	.vlan_offload_set = mlx5_vlan_offload_set,
2431 	.reta_update = mlx5_dev_rss_reta_update,
2432 	.reta_query = mlx5_dev_rss_reta_query,
2433 	.rss_hash_update = mlx5_rss_hash_update,
2434 	.rss_hash_conf_get = mlx5_rss_hash_conf_get,
2435 	.flow_ops_get = mlx5_flow_ops_get,
2436 	.rxq_info_get = mlx5_rxq_info_get,
2437 	.txq_info_get = mlx5_txq_info_get,
2438 	.rx_burst_mode_get = mlx5_rx_burst_mode_get,
2439 	.tx_burst_mode_get = mlx5_tx_burst_mode_get,
2440 	.rx_queue_intr_enable = mlx5_rx_intr_enable,
2441 	.rx_queue_intr_disable = mlx5_rx_intr_disable,
2442 	.is_removed = mlx5_is_removed,
2443 	.udp_tunnel_port_add  = mlx5_udp_tunnel_port_add,
2444 	.get_module_info = mlx5_get_module_info,
2445 	.get_module_eeprom = mlx5_get_module_eeprom,
2446 	.hairpin_cap_get = mlx5_hairpin_cap_get,
2447 	.mtr_ops_get = mlx5_flow_meter_ops_get,
2448 	.hairpin_bind = mlx5_hairpin_bind,
2449 	.hairpin_unbind = mlx5_hairpin_unbind,
2450 	.hairpin_get_peer_ports = mlx5_hairpin_get_peer_ports,
2451 	.hairpin_queue_peer_update = mlx5_hairpin_queue_peer_update,
2452 	.hairpin_queue_peer_bind = mlx5_hairpin_queue_peer_bind,
2453 	.hairpin_queue_peer_unbind = mlx5_hairpin_queue_peer_unbind,
2454 	.get_monitor_addr = mlx5_get_monitor_addr,
2455 	.count_aggr_ports = mlx5_count_aggr_ports,
2456 	.map_aggr_tx_affinity = mlx5_map_aggr_tx_affinity,
2457 	.rx_metadata_negotiate = mlx5_flow_rx_metadata_negotiate,
2458 };
2459 
2460 /* Available operations from secondary process. */
2461 const struct eth_dev_ops mlx5_dev_sec_ops = {
2462 	.stats_get = mlx5_stats_get,
2463 	.stats_reset = mlx5_stats_reset,
2464 	.xstats_get = mlx5_xstats_get,
2465 	.xstats_reset = mlx5_xstats_reset,
2466 	.xstats_get_names = mlx5_xstats_get_names,
2467 	.fw_version_get = mlx5_fw_version_get,
2468 	.dev_infos_get = mlx5_dev_infos_get,
2469 	.representor_info_get = mlx5_representor_info_get,
2470 	.read_clock = mlx5_txpp_read_clock,
2471 	.rx_queue_start = mlx5_rx_queue_start,
2472 	.rx_queue_stop = mlx5_rx_queue_stop,
2473 	.tx_queue_start = mlx5_tx_queue_start,
2474 	.tx_queue_stop = mlx5_tx_queue_stop,
2475 	.rxq_info_get = mlx5_rxq_info_get,
2476 	.txq_info_get = mlx5_txq_info_get,
2477 	.rx_burst_mode_get = mlx5_rx_burst_mode_get,
2478 	.tx_burst_mode_get = mlx5_tx_burst_mode_get,
2479 	.get_module_info = mlx5_get_module_info,
2480 	.get_module_eeprom = mlx5_get_module_eeprom,
2481 	.count_aggr_ports = mlx5_count_aggr_ports,
2482 	.map_aggr_tx_affinity = mlx5_map_aggr_tx_affinity,
2483 	.rx_metadata_negotiate = mlx5_flow_rx_metadata_negotiate,
2484 };
2485 
2486 /* Available operations in flow isolated mode. */
2487 const struct eth_dev_ops mlx5_dev_ops_isolate = {
2488 	.dev_configure = mlx5_dev_configure,
2489 	.dev_start = mlx5_dev_start,
2490 	.dev_stop = mlx5_dev_stop,
2491 	.dev_set_link_down = mlx5_set_link_down,
2492 	.dev_set_link_up = mlx5_set_link_up,
2493 	.dev_close = mlx5_dev_close,
2494 	.promiscuous_enable = mlx5_promiscuous_enable,
2495 	.promiscuous_disable = mlx5_promiscuous_disable,
2496 	.allmulticast_enable = mlx5_allmulticast_enable,
2497 	.allmulticast_disable = mlx5_allmulticast_disable,
2498 	.link_update = mlx5_link_update,
2499 	.stats_get = mlx5_stats_get,
2500 	.stats_reset = mlx5_stats_reset,
2501 	.xstats_get = mlx5_xstats_get,
2502 	.xstats_reset = mlx5_xstats_reset,
2503 	.xstats_get_names = mlx5_xstats_get_names,
2504 	.fw_version_get = mlx5_fw_version_get,
2505 	.dev_infos_get = mlx5_dev_infos_get,
2506 	.representor_info_get = mlx5_representor_info_get,
2507 	.read_clock = mlx5_txpp_read_clock,
2508 	.dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
2509 	.vlan_filter_set = mlx5_vlan_filter_set,
2510 	.rx_queue_setup = mlx5_rx_queue_setup,
2511 	.rx_hairpin_queue_setup = mlx5_rx_hairpin_queue_setup,
2512 	.tx_queue_setup = mlx5_tx_queue_setup,
2513 	.tx_hairpin_queue_setup = mlx5_tx_hairpin_queue_setup,
2514 	.rx_queue_release = mlx5_rx_queue_release,
2515 	.tx_queue_release = mlx5_tx_queue_release,
2516 	.rx_queue_start = mlx5_rx_queue_start,
2517 	.rx_queue_stop = mlx5_rx_queue_stop,
2518 	.tx_queue_start = mlx5_tx_queue_start,
2519 	.tx_queue_stop = mlx5_tx_queue_stop,
2520 	.flow_ctrl_get = mlx5_dev_get_flow_ctrl,
2521 	.flow_ctrl_set = mlx5_dev_set_flow_ctrl,
2522 	.mac_addr_remove = mlx5_mac_addr_remove,
2523 	.mac_addr_add = mlx5_mac_addr_add,
2524 	.mac_addr_set = mlx5_mac_addr_set,
2525 	.set_mc_addr_list = mlx5_set_mc_addr_list,
2526 	.mtu_set = mlx5_dev_set_mtu,
2527 	.vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
2528 	.vlan_offload_set = mlx5_vlan_offload_set,
2529 	.flow_ops_get = mlx5_flow_ops_get,
2530 	.rxq_info_get = mlx5_rxq_info_get,
2531 	.txq_info_get = mlx5_txq_info_get,
2532 	.rx_burst_mode_get = mlx5_rx_burst_mode_get,
2533 	.tx_burst_mode_get = mlx5_tx_burst_mode_get,
2534 	.rx_queue_intr_enable = mlx5_rx_intr_enable,
2535 	.rx_queue_intr_disable = mlx5_rx_intr_disable,
2536 	.is_removed = mlx5_is_removed,
2537 	.get_module_info = mlx5_get_module_info,
2538 	.get_module_eeprom = mlx5_get_module_eeprom,
2539 	.hairpin_cap_get = mlx5_hairpin_cap_get,
2540 	.mtr_ops_get = mlx5_flow_meter_ops_get,
2541 	.hairpin_bind = mlx5_hairpin_bind,
2542 	.hairpin_unbind = mlx5_hairpin_unbind,
2543 	.hairpin_get_peer_ports = mlx5_hairpin_get_peer_ports,
2544 	.hairpin_queue_peer_update = mlx5_hairpin_queue_peer_update,
2545 	.hairpin_queue_peer_bind = mlx5_hairpin_queue_peer_bind,
2546 	.hairpin_queue_peer_unbind = mlx5_hairpin_queue_peer_unbind,
2547 	.get_monitor_addr = mlx5_get_monitor_addr,
2548 	.count_aggr_ports = mlx5_count_aggr_ports,
2549 	.map_aggr_tx_affinity = mlx5_map_aggr_tx_affinity,
2550 };
2551 
2552 /**
2553  * Verify and store value for device argument.
2554  *
2555  * @param[in] key
2556  *   Key argument to verify.
2557  * @param[in] val
2558  *   Value associated with key.
2559  * @param opaque
2560  *   User data.
2561  *
2562  * @return
2563  *   0 on success, a negative errno value otherwise and rte_errno is set.
2564  */
2565 static int
2566 mlx5_port_args_check_handler(const char *key, const char *val, void *opaque)
2567 {
2568 	struct mlx5_port_config *config = opaque;
2569 	signed long tmp;
2570 
2571 	/* No-op, port representors are processed in mlx5_dev_spawn(). */
2572 	if (!strcmp(MLX5_REPRESENTOR, key))
2573 		return 0;
2574 	errno = 0;
2575 	tmp = strtol(val, NULL, 0);
2576 	if (errno) {
2577 		rte_errno = errno;
2578 		DRV_LOG(WARNING, "%s: \"%s\" is not a valid integer", key, val);
2579 		return -rte_errno;
2580 	}
2581 	if (tmp < 0) {
2582 		/* Negative values are acceptable for some keys only. */
2583 		rte_errno = EINVAL;
2584 		DRV_LOG(WARNING, "%s: invalid negative value \"%s\"", key, val);
2585 		return -rte_errno;
2586 	}
2587 	if (strcmp(MLX5_RXQ_CQE_COMP_EN, key) == 0) {
2588 		if ((tmp & ~MLX5_RXQ_ENH_CQE_COMP_MASK) >
2589 		    MLX5_CQE_RESP_FORMAT_L34H_STRIDX) {
2590 			DRV_LOG(ERR, "invalid CQE compression "
2591 				     "format parameter");
2592 			rte_errno = EINVAL;
2593 			return -rte_errno;
2594 		}
2595 		config->cqe_comp = !!tmp;
2596 		config->cqe_comp_fmt = tmp & ~MLX5_RXQ_ENH_CQE_COMP_MASK;
2597 		config->enh_cqe_comp = !!(tmp & MLX5_RXQ_ENH_CQE_COMP_MASK);
2598 	} else if (strcmp(MLX5_RXQ_PKT_PAD_EN, key) == 0) {
2599 		config->hw_padding = !!tmp;
2600 	} else if (strcmp(MLX5_RX_MPRQ_EN, key) == 0) {
2601 		config->mprq.enabled = !!tmp;
2602 	} else if (strcmp(MLX5_RX_MPRQ_LOG_STRIDE_NUM, key) == 0) {
2603 		config->mprq.log_stride_num = tmp;
2604 	} else if (strcmp(MLX5_RX_MPRQ_LOG_STRIDE_SIZE, key) == 0) {
2605 		config->mprq.log_stride_size = tmp;
2606 	} else if (strcmp(MLX5_RX_MPRQ_MAX_MEMCPY_LEN, key) == 0) {
2607 		config->mprq.max_memcpy_len = tmp;
2608 	} else if (strcmp(MLX5_RXQS_MIN_MPRQ, key) == 0) {
2609 		config->mprq.min_rxqs_num = tmp;
2610 	} else if (strcmp(MLX5_TXQ_INLINE, key) == 0) {
2611 		DRV_LOG(WARNING, "%s: deprecated parameter,"
2612 				 " converted to txq_inline_max", key);
2613 		config->txq_inline_max = tmp;
2614 	} else if (strcmp(MLX5_TXQ_INLINE_MAX, key) == 0) {
2615 		config->txq_inline_max = tmp;
2616 	} else if (strcmp(MLX5_TXQ_INLINE_MIN, key) == 0) {
2617 		config->txq_inline_min = tmp;
2618 	} else if (strcmp(MLX5_TXQ_INLINE_MPW, key) == 0) {
2619 		config->txq_inline_mpw = tmp;
2620 	} else if (strcmp(MLX5_TXQS_MIN_INLINE, key) == 0) {
2621 		config->txqs_inline = tmp;
2622 	} else if (strcmp(MLX5_TXQS_MAX_VEC, key) == 0) {
2623 		DRV_LOG(WARNING, "%s: deprecated parameter, ignored", key);
2624 	} else if (strcmp(MLX5_TXQ_MPW_EN, key) == 0) {
2625 		config->mps = !!tmp;
2626 	} else if (strcmp(MLX5_TXQ_MPW_HDR_DSEG_EN, key) == 0) {
2627 		DRV_LOG(WARNING, "%s: deprecated parameter, ignored", key);
2628 	} else if (strcmp(MLX5_TXQ_MAX_INLINE_LEN, key) == 0) {
2629 		DRV_LOG(WARNING, "%s: deprecated parameter,"
2630 				 " converted to txq_inline_mpw", key);
2631 		config->txq_inline_mpw = tmp;
2632 	} else if (strcmp(MLX5_TX_VEC_EN, key) == 0) {
2633 		DRV_LOG(WARNING, "%s: deprecated parameter, ignored", key);
2634 	} else if (strcmp(MLX5_RX_VEC_EN, key) == 0) {
2635 		config->rx_vec_en = !!tmp;
2636 	} else if (strcmp(MLX5_MAX_DUMP_FILES_NUM, key) == 0) {
2637 		config->max_dump_files_num = tmp;
2638 	} else if (strcmp(MLX5_LRO_TIMEOUT_USEC, key) == 0) {
2639 		config->lro_timeout = tmp;
2640 	} else if (strcmp(MLX5_HP_BUF_SIZE, key) == 0) {
2641 		config->log_hp_size = tmp;
2642 	} else if (strcmp(MLX5_DELAY_DROP, key) == 0) {
2643 		config->std_delay_drop = !!(tmp & MLX5_DELAY_DROP_STANDARD);
2644 		config->hp_delay_drop = !!(tmp & MLX5_DELAY_DROP_HAIRPIN);
2645 	}
2646 	return 0;
2647 }
2648 
2649 /**
2650  * Parse user port parameters and adjust them according to device capabilities.
2651  *
2652  * @param priv
2653  *   Pointer to shared device context.
2654  * @param mkvlist
2655  *   Pointer to mlx5 kvargs control, can be NULL if there is no devargs.
2656  * @param config
2657  *   Pointer to port configuration structure.
2658  *
2659  * @return
2660  *   0 on success, a negative errno value otherwise and rte_errno is set.
2661  */
2662 int
2663 mlx5_port_args_config(struct mlx5_priv *priv, struct mlx5_kvargs_ctrl *mkvlist,
2664 		      struct mlx5_port_config *config)
2665 {
2666 	struct mlx5_hca_attr *hca_attr = &priv->sh->cdev->config.hca_attr;
2667 	struct mlx5_dev_cap *dev_cap = &priv->sh->dev_cap;
2668 	bool devx = priv->sh->cdev->config.devx;
2669 	const char **params = (const char *[]){
2670 		MLX5_RXQ_CQE_COMP_EN,
2671 		MLX5_RXQ_PKT_PAD_EN,
2672 		MLX5_RX_MPRQ_EN,
2673 		MLX5_RX_MPRQ_LOG_STRIDE_NUM,
2674 		MLX5_RX_MPRQ_LOG_STRIDE_SIZE,
2675 		MLX5_RX_MPRQ_MAX_MEMCPY_LEN,
2676 		MLX5_RXQS_MIN_MPRQ,
2677 		MLX5_TXQ_INLINE,
2678 		MLX5_TXQ_INLINE_MIN,
2679 		MLX5_TXQ_INLINE_MAX,
2680 		MLX5_TXQ_INLINE_MPW,
2681 		MLX5_TXQS_MIN_INLINE,
2682 		MLX5_TXQS_MAX_VEC,
2683 		MLX5_TXQ_MPW_EN,
2684 		MLX5_TXQ_MPW_HDR_DSEG_EN,
2685 		MLX5_TXQ_MAX_INLINE_LEN,
2686 		MLX5_TX_VEC_EN,
2687 		MLX5_RX_VEC_EN,
2688 		MLX5_REPRESENTOR,
2689 		MLX5_MAX_DUMP_FILES_NUM,
2690 		MLX5_LRO_TIMEOUT_USEC,
2691 		MLX5_HP_BUF_SIZE,
2692 		MLX5_DELAY_DROP,
2693 		NULL,
2694 	};
2695 	int ret = 0;
2696 
2697 	/* Default configuration. */
2698 	memset(config, 0, sizeof(*config));
2699 	config->mps = MLX5_ARG_UNSET;
2700 	config->cqe_comp = 1;
2701 	config->rx_vec_en = 1;
2702 	config->txq_inline_max = MLX5_ARG_UNSET;
2703 	config->txq_inline_min = MLX5_ARG_UNSET;
2704 	config->txq_inline_mpw = MLX5_ARG_UNSET;
2705 	config->txqs_inline = MLX5_ARG_UNSET;
2706 	config->mprq.max_memcpy_len = MLX5_MPRQ_MEMCPY_DEFAULT_LEN;
2707 	config->mprq.min_rxqs_num = MLX5_MPRQ_MIN_RXQS;
2708 	config->mprq.log_stride_num = MLX5_MPRQ_DEFAULT_LOG_STRIDE_NUM;
2709 	config->mprq.log_stride_size = MLX5_MPRQ_DEFAULT_LOG_STRIDE_SIZE;
2710 	config->log_hp_size = MLX5_ARG_UNSET;
2711 	config->std_delay_drop = 0;
2712 	config->hp_delay_drop = 0;
2713 	if (mkvlist != NULL) {
2714 		/* Process parameters. */
2715 		ret = mlx5_kvargs_process(mkvlist, params,
2716 					  mlx5_port_args_check_handler, config);
2717 		if (ret) {
2718 			DRV_LOG(ERR, "Failed to process port arguments: %s",
2719 				strerror(rte_errno));
2720 			return -rte_errno;
2721 		}
2722 	}
2723 	/* Adjust parameters according to device capabilities. */
2724 	if (config->hw_padding && !dev_cap->hw_padding) {
2725 		DRV_LOG(DEBUG, "Rx end alignment padding isn't supported.");
2726 		config->hw_padding = 0;
2727 	} else if (config->hw_padding) {
2728 		DRV_LOG(DEBUG, "Rx end alignment padding is enabled.");
2729 	}
2730 	/*
2731 	 * MPW is disabled by default, while the Enhanced MPW is enabled
2732 	 * by default.
2733 	 */
2734 	if (config->mps == MLX5_ARG_UNSET)
2735 		config->mps = (dev_cap->mps == MLX5_MPW_ENHANCED) ?
2736 			      MLX5_MPW_ENHANCED : MLX5_MPW_DISABLED;
2737 	else
2738 		config->mps = config->mps ? dev_cap->mps : MLX5_MPW_DISABLED;
2739 	DRV_LOG(INFO, "%sMPS is %s",
2740 		config->mps == MLX5_MPW_ENHANCED ? "enhanced " :
2741 		config->mps == MLX5_MPW ? "legacy " : "",
2742 		config->mps != MLX5_MPW_DISABLED ? "enabled" : "disabled");
2743 	if (priv->sh->config.lro_allowed) {
2744 		/*
2745 		 * If LRO timeout is not configured by application,
2746 		 * use the minimal supported value.
2747 		 */
2748 		if (!config->lro_timeout)
2749 			config->lro_timeout =
2750 				       hca_attr->lro_timer_supported_periods[0];
2751 		DRV_LOG(DEBUG, "LRO session timeout set to %d usec.",
2752 			config->lro_timeout);
2753 	}
2754 	if (config->cqe_comp && !dev_cap->cqe_comp) {
2755 		DRV_LOG(WARNING, "Rx CQE 128B compression is not supported.");
2756 		config->cqe_comp = 0;
2757 	}
2758 	if (config->cqe_comp_fmt == MLX5_CQE_RESP_FORMAT_FTAG_STRIDX &&
2759 	    (!devx || !hca_attr->mini_cqe_resp_flow_tag)) {
2760 		DRV_LOG(WARNING,
2761 			"Flow Tag CQE compression format isn't supported.");
2762 		config->cqe_comp = 0;
2763 	}
2764 	if (config->cqe_comp_fmt == MLX5_CQE_RESP_FORMAT_L34H_STRIDX &&
2765 	    (!devx || !hca_attr->mini_cqe_resp_l3_l4_tag)) {
2766 		DRV_LOG(WARNING,
2767 			"L3/L4 Header CQE compression format isn't supported.");
2768 		config->cqe_comp = 0;
2769 	}
2770 	if (config->enh_cqe_comp && !hca_attr->enhanced_cqe_compression) {
2771 		DRV_LOG(WARNING,
2772 			"Enhanced CQE compression isn't supported.");
2773 		config->enh_cqe_comp = 0;
2774 	}
2775 	DRV_LOG(DEBUG, "%sRx CQE compression is %ssupported.",
2776 		config->enh_cqe_comp ? "Enhanced " : "",
2777 		config->cqe_comp ? "" : "not ");
2778 	if ((config->std_delay_drop || config->hp_delay_drop) &&
2779 	    !dev_cap->rq_delay_drop_en) {
2780 		config->std_delay_drop = 0;
2781 		config->hp_delay_drop = 0;
2782 		DRV_LOG(WARNING, "dev_port-%u: Rxq delay drop isn't supported.",
2783 			priv->dev_port);
2784 	}
2785 	if (config->mprq.enabled && !priv->sh->dev_cap.mprq.enabled) {
2786 		DRV_LOG(WARNING, "Multi-Packet RQ isn't supported.");
2787 		config->mprq.enabled = 0;
2788 	}
2789 	if (config->max_dump_files_num == 0)
2790 		config->max_dump_files_num = 128;
2791 	/* Detect minimal data bytes to inline. */
2792 	mlx5_set_min_inline(priv);
2793 	DRV_LOG(DEBUG, "VLAN insertion in WQE is %ssupported.",
2794 		config->hw_vlan_insert ? "" : "not ");
2795 	DRV_LOG(DEBUG, "\"rxq_pkt_pad_en\" is %u.", config->hw_padding);
2796 	DRV_LOG(DEBUG, "\"rxq_cqe_comp_en\" is %u.", config->cqe_comp);
2797 	DRV_LOG(DEBUG, "\"cqe_comp_fmt\" is %u.", config->cqe_comp_fmt);
2798 	DRV_LOG(DEBUG, "\"enh_cqe_comp\" is %u.", config->enh_cqe_comp);
2799 	DRV_LOG(DEBUG, "\"rx_vec_en\" is %u.", config->rx_vec_en);
2800 	DRV_LOG(DEBUG, "Standard \"delay_drop\" is %u.",
2801 		config->std_delay_drop);
2802 	DRV_LOG(DEBUG, "Hairpin \"delay_drop\" is %u.", config->hp_delay_drop);
2803 	DRV_LOG(DEBUG, "\"max_dump_files_num\" is %u.",
2804 		config->max_dump_files_num);
2805 	DRV_LOG(DEBUG, "\"log_hp_size\" is %u.", config->log_hp_size);
2806 	DRV_LOG(DEBUG, "\"mprq_en\" is %u.", config->mprq.enabled);
2807 	DRV_LOG(DEBUG, "\"mprq_log_stride_num\" is %u.",
2808 		config->mprq.log_stride_num);
2809 	DRV_LOG(DEBUG, "\"mprq_log_stride_size\" is %u.",
2810 		config->mprq.log_stride_size);
2811 	DRV_LOG(DEBUG, "\"mprq_max_memcpy_len\" is %u.",
2812 		config->mprq.max_memcpy_len);
2813 	DRV_LOG(DEBUG, "\"rxqs_min_mprq\" is %u.", config->mprq.min_rxqs_num);
2814 	DRV_LOG(DEBUG, "\"lro_timeout_usec\" is %u.", config->lro_timeout);
2815 	DRV_LOG(DEBUG, "\"txq_mpw_en\" is %d.", config->mps);
2816 	DRV_LOG(DEBUG, "\"txqs_min_inline\" is %d.", config->txqs_inline);
2817 	DRV_LOG(DEBUG, "\"txq_inline_min\" is %d.", config->txq_inline_min);
2818 	DRV_LOG(DEBUG, "\"txq_inline_max\" is %d.", config->txq_inline_max);
2819 	DRV_LOG(DEBUG, "\"txq_inline_mpw\" is %d.", config->txq_inline_mpw);
2820 	return 0;
2821 }
2822 
2823 /**
2824  * Print the key for device argument.
2825  *
2826  * It is "dummy" handler whose whole purpose is to enable using
2827  * mlx5_kvargs_process() function which set devargs as used.
2828  *
2829  * @param key
2830  *   Key argument.
2831  * @param val
2832  *   Value associated with key, unused.
2833  * @param opaque
2834  *   Unused, can be NULL.
2835  *
2836  * @return
2837  *   0 on success, function cannot fail.
2838  */
2839 static int
2840 mlx5_dummy_handler(const char *key, const char *val, void *opaque)
2841 {
2842 	DRV_LOG(DEBUG, "\tKey: \"%s\" is set as used.", key);
2843 	RTE_SET_USED(opaque);
2844 	RTE_SET_USED(val);
2845 	return 0;
2846 }
2847 
2848 /**
2849  * Set requested devargs as used when device is already spawned.
2850  *
2851  * It is necessary since it is valid to ask probe again for existing device,
2852  * if its devargs don't assign as used, mlx5_kvargs_validate() will fail.
2853  *
2854  * @param name
2855  *   Name of the existing device.
2856  * @param port_id
2857  *   Port identifier of the device.
2858  * @param mkvlist
2859  *   Pointer to mlx5 kvargs control to sign as used.
2860  */
2861 void
2862 mlx5_port_args_set_used(const char *name, uint16_t port_id,
2863 			struct mlx5_kvargs_ctrl *mkvlist)
2864 {
2865 	const char **params = (const char *[]){
2866 		MLX5_RXQ_CQE_COMP_EN,
2867 		MLX5_RXQ_PKT_PAD_EN,
2868 		MLX5_RX_MPRQ_EN,
2869 		MLX5_RX_MPRQ_LOG_STRIDE_NUM,
2870 		MLX5_RX_MPRQ_LOG_STRIDE_SIZE,
2871 		MLX5_RX_MPRQ_MAX_MEMCPY_LEN,
2872 		MLX5_RXQS_MIN_MPRQ,
2873 		MLX5_TXQ_INLINE,
2874 		MLX5_TXQ_INLINE_MIN,
2875 		MLX5_TXQ_INLINE_MAX,
2876 		MLX5_TXQ_INLINE_MPW,
2877 		MLX5_TXQS_MIN_INLINE,
2878 		MLX5_TXQS_MAX_VEC,
2879 		MLX5_TXQ_MPW_EN,
2880 		MLX5_TXQ_MPW_HDR_DSEG_EN,
2881 		MLX5_TXQ_MAX_INLINE_LEN,
2882 		MLX5_TX_VEC_EN,
2883 		MLX5_RX_VEC_EN,
2884 		MLX5_REPRESENTOR,
2885 		MLX5_MAX_DUMP_FILES_NUM,
2886 		MLX5_LRO_TIMEOUT_USEC,
2887 		MLX5_HP_BUF_SIZE,
2888 		MLX5_DELAY_DROP,
2889 		NULL,
2890 	};
2891 
2892 	/* Secondary process should not handle devargs. */
2893 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2894 		return;
2895 	MLX5_ASSERT(mkvlist != NULL);
2896 	DRV_LOG(DEBUG, "Ethernet device \"%s\" for port %u "
2897 		"already exists, set devargs as used:", name, port_id);
2898 	/* This function cannot fail with this handler. */
2899 	mlx5_kvargs_process(mkvlist, params, mlx5_dummy_handler, NULL);
2900 }
2901 
2902 /**
2903  * Check sibling device configurations when probing again.
2904  *
2905  * Sibling devices sharing infiniband device context should have compatible
2906  * configurations. This regards representors and bonding device.
2907  *
2908  * @param cdev
2909  *   Pointer to mlx5 device structure.
2910  * @param mkvlist
2911  *   Pointer to mlx5 kvargs control, can be NULL if there is no devargs.
2912  *
2913  * @return
2914  *   0 on success, a negative errno value otherwise and rte_errno is set.
2915  */
2916 int
2917 mlx5_probe_again_args_validate(struct mlx5_common_device *cdev,
2918 			       struct mlx5_kvargs_ctrl *mkvlist)
2919 {
2920 	struct mlx5_dev_ctx_shared *sh = NULL;
2921 	struct mlx5_sh_config *config;
2922 	int ret;
2923 
2924 	/* Secondary process should not handle devargs. */
2925 	if (rte_eal_process_type() != RTE_PROC_PRIMARY)
2926 		return 0;
2927 	pthread_mutex_lock(&mlx5_dev_ctx_list_mutex);
2928 	/* Search for IB context by common device pointer. */
2929 	LIST_FOREACH(sh, &mlx5_dev_ctx_list, next)
2930 		if (sh->cdev == cdev)
2931 			break;
2932 	pthread_mutex_unlock(&mlx5_dev_ctx_list_mutex);
2933 	/* There is sh for this device -> it isn't probe again. */
2934 	if (sh == NULL)
2935 		return 0;
2936 	config = mlx5_malloc(MLX5_MEM_ZERO | MLX5_MEM_RTE,
2937 			     sizeof(struct mlx5_sh_config),
2938 			     RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
2939 	if (config == NULL) {
2940 		rte_errno = -ENOMEM;
2941 		return -rte_errno;
2942 	}
2943 	/*
2944 	 * Creates a temporary IB context configure structure according to new
2945 	 * devargs attached in probing again.
2946 	 */
2947 	ret = mlx5_shared_dev_ctx_args_config(sh, mkvlist, config);
2948 	if (ret) {
2949 		DRV_LOG(ERR, "Failed to process device configure: %s",
2950 			strerror(rte_errno));
2951 		mlx5_free(config);
2952 		return ret;
2953 	}
2954 	/*
2955 	 * Checks the match between the temporary structure and the existing
2956 	 * IB context structure.
2957 	 */
2958 	if (sh->config.dv_flow_en ^ config->dv_flow_en) {
2959 		DRV_LOG(ERR, "\"dv_flow_en\" "
2960 			"configuration mismatch for shared %s context.",
2961 			sh->ibdev_name);
2962 		goto error;
2963 	}
2964 	if ((sh->config.dv_xmeta_en ^ config->dv_xmeta_en) ||
2965 	    (sh->config.dv_miss_info ^ config->dv_miss_info)) {
2966 		DRV_LOG(ERR, "\"dv_xmeta_en\" "
2967 			"configuration mismatch for shared %s context.",
2968 			sh->ibdev_name);
2969 		goto error;
2970 	}
2971 	if (sh->config.dv_esw_en ^ config->dv_esw_en) {
2972 		DRV_LOG(ERR, "\"dv_esw_en\" "
2973 			"configuration mismatch for shared %s context.",
2974 			sh->ibdev_name);
2975 		goto error;
2976 	}
2977 	if (sh->config.reclaim_mode ^ config->reclaim_mode) {
2978 		DRV_LOG(ERR, "\"reclaim_mode\" "
2979 			"configuration mismatch for shared %s context.",
2980 			sh->ibdev_name);
2981 		goto error;
2982 	}
2983 	if (sh->config.allow_duplicate_pattern ^
2984 	    config->allow_duplicate_pattern) {
2985 		DRV_LOG(ERR, "\"allow_duplicate_pattern\" "
2986 			"configuration mismatch for shared %s context.",
2987 			sh->ibdev_name);
2988 		goto error;
2989 	}
2990 	if (sh->config.fdb_def_rule ^ config->fdb_def_rule) {
2991 		DRV_LOG(ERR, "\"fdb_def_rule_en\" configuration mismatch for shared %s context.",
2992 			sh->ibdev_name);
2993 		goto error;
2994 	}
2995 	if (sh->config.l3_vxlan_en ^ config->l3_vxlan_en) {
2996 		DRV_LOG(ERR, "\"l3_vxlan_en\" "
2997 			"configuration mismatch for shared %s context.",
2998 			sh->ibdev_name);
2999 		goto error;
3000 	}
3001 	if (sh->config.decap_en ^ config->decap_en) {
3002 		DRV_LOG(ERR, "\"decap_en\" "
3003 			"configuration mismatch for shared %s context.",
3004 			sh->ibdev_name);
3005 		goto error;
3006 	}
3007 	if (sh->config.lacp_by_user ^ config->lacp_by_user) {
3008 		DRV_LOG(ERR, "\"lacp_by_user\" "
3009 			"configuration mismatch for shared %s context.",
3010 			sh->ibdev_name);
3011 		goto error;
3012 	}
3013 	if (sh->config.tx_pp ^ config->tx_pp) {
3014 		DRV_LOG(ERR, "\"tx_pp\" "
3015 			"configuration mismatch for shared %s context.",
3016 			sh->ibdev_name);
3017 		goto error;
3018 	}
3019 	if (sh->config.tx_skew ^ config->tx_skew) {
3020 		DRV_LOG(ERR, "\"tx_skew\" "
3021 			"configuration mismatch for shared %s context.",
3022 			sh->ibdev_name);
3023 		goto error;
3024 	}
3025 	mlx5_free(config);
3026 	return 0;
3027 error:
3028 	mlx5_free(config);
3029 	rte_errno = EINVAL;
3030 	return -rte_errno;
3031 }
3032 
3033 /**
3034  * Configures the minimal amount of data to inline into WQE
3035  * while sending packets.
3036  *
3037  * - the txq_inline_min has the maximal priority, if this
3038  *   key is specified in devargs
3039  * - if DevX is enabled the inline mode is queried from the
3040  *   device (HCA attributes and NIC vport context if needed).
3041  * - otherwise L2 mode (18 bytes) is assumed for ConnectX-4/4 Lx
3042  *   and none (0 bytes) for other NICs
3043  *
3044  * @param priv
3045  *   Pointer to the private device data structure.
3046  */
3047 void
3048 mlx5_set_min_inline(struct mlx5_priv *priv)
3049 {
3050 	struct mlx5_hca_attr *hca_attr = &priv->sh->cdev->config.hca_attr;
3051 	struct mlx5_port_config *config = &priv->config;
3052 
3053 	if (config->txq_inline_min != MLX5_ARG_UNSET) {
3054 		/* Application defines size of inlined data explicitly. */
3055 		if (priv->pci_dev != NULL) {
3056 			switch (priv->pci_dev->id.device_id) {
3057 			case PCI_DEVICE_ID_MELLANOX_CONNECTX4:
3058 			case PCI_DEVICE_ID_MELLANOX_CONNECTX4VF:
3059 				if (config->txq_inline_min <
3060 					       (int)MLX5_INLINE_HSIZE_L2) {
3061 					DRV_LOG(DEBUG,
3062 						"txq_inline_mix aligned to minimal ConnectX-4 required value %d",
3063 						(int)MLX5_INLINE_HSIZE_L2);
3064 					config->txq_inline_min =
3065 							MLX5_INLINE_HSIZE_L2;
3066 				}
3067 				break;
3068 			}
3069 		}
3070 		goto exit;
3071 	}
3072 	if (hca_attr->eth_net_offloads) {
3073 		/* We have DevX enabled, inline mode queried successfully. */
3074 		switch (hca_attr->wqe_inline_mode) {
3075 		case MLX5_CAP_INLINE_MODE_L2:
3076 			/* outer L2 header must be inlined. */
3077 			config->txq_inline_min = MLX5_INLINE_HSIZE_L2;
3078 			goto exit;
3079 		case MLX5_CAP_INLINE_MODE_NOT_REQUIRED:
3080 			/* No inline data are required by NIC. */
3081 			config->txq_inline_min = MLX5_INLINE_HSIZE_NONE;
3082 			config->hw_vlan_insert =
3083 				hca_attr->wqe_vlan_insert;
3084 			DRV_LOG(DEBUG, "Tx VLAN insertion is supported");
3085 			goto exit;
3086 		case MLX5_CAP_INLINE_MODE_VPORT_CONTEXT:
3087 			/* inline mode is defined by NIC vport context. */
3088 			if (!hca_attr->eth_virt)
3089 				break;
3090 			switch (hca_attr->vport_inline_mode) {
3091 			case MLX5_INLINE_MODE_NONE:
3092 				config->txq_inline_min =
3093 					MLX5_INLINE_HSIZE_NONE;
3094 				goto exit;
3095 			case MLX5_INLINE_MODE_L2:
3096 				config->txq_inline_min =
3097 					MLX5_INLINE_HSIZE_L2;
3098 				goto exit;
3099 			case MLX5_INLINE_MODE_IP:
3100 				config->txq_inline_min =
3101 					MLX5_INLINE_HSIZE_L3;
3102 				goto exit;
3103 			case MLX5_INLINE_MODE_TCP_UDP:
3104 				config->txq_inline_min =
3105 					MLX5_INLINE_HSIZE_L4;
3106 				goto exit;
3107 			case MLX5_INLINE_MODE_INNER_L2:
3108 				config->txq_inline_min =
3109 					MLX5_INLINE_HSIZE_INNER_L2;
3110 				goto exit;
3111 			case MLX5_INLINE_MODE_INNER_IP:
3112 				config->txq_inline_min =
3113 					MLX5_INLINE_HSIZE_INNER_L3;
3114 				goto exit;
3115 			case MLX5_INLINE_MODE_INNER_TCP_UDP:
3116 				config->txq_inline_min =
3117 					MLX5_INLINE_HSIZE_INNER_L4;
3118 				goto exit;
3119 			}
3120 		}
3121 	}
3122 	if (priv->pci_dev == NULL) {
3123 		config->txq_inline_min = MLX5_INLINE_HSIZE_NONE;
3124 		goto exit;
3125 	}
3126 	/*
3127 	 * We get here if we are unable to deduce
3128 	 * inline data size with DevX. Try PCI ID
3129 	 * to determine old NICs.
3130 	 */
3131 	switch (priv->pci_dev->id.device_id) {
3132 	case PCI_DEVICE_ID_MELLANOX_CONNECTX4:
3133 	case PCI_DEVICE_ID_MELLANOX_CONNECTX4VF:
3134 	case PCI_DEVICE_ID_MELLANOX_CONNECTX4LX:
3135 	case PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF:
3136 		config->txq_inline_min = MLX5_INLINE_HSIZE_L2;
3137 		config->hw_vlan_insert = 0;
3138 		break;
3139 	case PCI_DEVICE_ID_MELLANOX_CONNECTX5:
3140 	case PCI_DEVICE_ID_MELLANOX_CONNECTX5VF:
3141 	case PCI_DEVICE_ID_MELLANOX_CONNECTX5EX:
3142 	case PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF:
3143 		/*
3144 		 * These NICs support VLAN insertion from WQE and
3145 		 * report the wqe_vlan_insert flag. But there is the bug
3146 		 * and PFC control may be broken, so disable feature.
3147 		 */
3148 		config->hw_vlan_insert = 0;
3149 		config->txq_inline_min = MLX5_INLINE_HSIZE_NONE;
3150 		break;
3151 	default:
3152 		config->txq_inline_min = MLX5_INLINE_HSIZE_NONE;
3153 		break;
3154 	}
3155 exit:
3156 	DRV_LOG(DEBUG, "min tx inline configured: %d", config->txq_inline_min);
3157 }
3158 
3159 /**
3160  * Configures the metadata mask fields in the shared context.
3161  *
3162  * @param [in] dev
3163  *   Pointer to Ethernet device.
3164  */
3165 void
3166 mlx5_set_metadata_mask(struct rte_eth_dev *dev)
3167 {
3168 	struct mlx5_priv *priv = dev->data->dev_private;
3169 	struct mlx5_dev_ctx_shared *sh = priv->sh;
3170 	uint32_t meta, mark, reg_c0;
3171 
3172 	reg_c0 = ~priv->vport_meta_mask;
3173 	switch (sh->config.dv_xmeta_en) {
3174 	case MLX5_XMETA_MODE_LEGACY:
3175 		meta = UINT32_MAX;
3176 		mark = MLX5_FLOW_MARK_MASK;
3177 		break;
3178 	case MLX5_XMETA_MODE_META16:
3179 		meta = reg_c0 >> rte_bsf32(reg_c0);
3180 		mark = MLX5_FLOW_MARK_MASK;
3181 		break;
3182 	case MLX5_XMETA_MODE_META32:
3183 		meta = UINT32_MAX;
3184 		mark = (reg_c0 >> rte_bsf32(reg_c0)) & MLX5_FLOW_MARK_MASK;
3185 		break;
3186 	case MLX5_XMETA_MODE_META32_HWS:
3187 		meta = UINT32_MAX;
3188 		mark = MLX5_FLOW_MARK_MASK;
3189 		break;
3190 	default:
3191 		meta = 0;
3192 		mark = 0;
3193 		MLX5_ASSERT(false);
3194 		break;
3195 	}
3196 	if (sh->dv_mark_mask && sh->dv_mark_mask != mark)
3197 		DRV_LOG(WARNING, "metadata MARK mask mismatch %08X:%08X",
3198 				 sh->dv_mark_mask, mark);
3199 	else
3200 		sh->dv_mark_mask = mark;
3201 	if (sh->dv_meta_mask && sh->dv_meta_mask != meta)
3202 		DRV_LOG(WARNING, "metadata META mask mismatch %08X:%08X",
3203 				 sh->dv_meta_mask, meta);
3204 	else
3205 		sh->dv_meta_mask = meta;
3206 	if (sh->dv_regc0_mask && sh->dv_regc0_mask != reg_c0)
3207 		DRV_LOG(WARNING, "metadata reg_c0 mask mismatch %08X:%08X",
3208 				 sh->dv_meta_mask, reg_c0);
3209 	else
3210 		sh->dv_regc0_mask = reg_c0;
3211 	DRV_LOG(DEBUG, "metadata mode %u", sh->config.dv_xmeta_en);
3212 	DRV_LOG(DEBUG, "metadata MARK mask %08X", sh->dv_mark_mask);
3213 	DRV_LOG(DEBUG, "metadata META mask %08X", sh->dv_meta_mask);
3214 	DRV_LOG(DEBUG, "metadata reg_c0 mask %08X", sh->dv_regc0_mask);
3215 }
3216 
3217 int
3218 rte_pmd_mlx5_get_dyn_flag_names(char *names[], unsigned int n)
3219 {
3220 	static const char *const dynf_names[] = {
3221 		RTE_PMD_MLX5_FINE_GRANULARITY_INLINE,
3222 		RTE_MBUF_DYNFLAG_METADATA_NAME,
3223 		RTE_MBUF_DYNFLAG_TX_TIMESTAMP_NAME
3224 	};
3225 	unsigned int i;
3226 
3227 	if (n < RTE_DIM(dynf_names))
3228 		return -ENOMEM;
3229 	for (i = 0; i < RTE_DIM(dynf_names); i++) {
3230 		if (names[i] == NULL)
3231 			return -EINVAL;
3232 		strcpy(names[i], dynf_names[i]);
3233 	}
3234 	return RTE_DIM(dynf_names);
3235 }
3236 
3237 /**
3238  * Look for the ethernet device belonging to mlx5 driver.
3239  *
3240  * @param[in] port_id
3241  *   port_id to start looking for device.
3242  * @param[in] odev
3243  *   Pointer to the hint device. When device is being probed
3244  *   the its siblings (master and preceding representors might
3245  *   not have assigned driver yet (because the mlx5_os_pci_probe()
3246  *   is not completed yet, for this case match on hint
3247  *   device may be used to detect sibling device.
3248  *
3249  * @return
3250  *   port_id of found device, RTE_MAX_ETHPORT if not found.
3251  */
3252 uint16_t
3253 mlx5_eth_find_next(uint16_t port_id, struct rte_device *odev)
3254 {
3255 	while (port_id < RTE_MAX_ETHPORTS) {
3256 		struct rte_eth_dev *dev = &rte_eth_devices[port_id];
3257 
3258 		if (dev->state != RTE_ETH_DEV_UNUSED &&
3259 		    dev->device &&
3260 		    (dev->device == odev ||
3261 		     (dev->device->driver &&
3262 		     dev->device->driver->name &&
3263 		     ((strcmp(dev->device->driver->name,
3264 			      MLX5_PCI_DRIVER_NAME) == 0) ||
3265 		      (strcmp(dev->device->driver->name,
3266 			      MLX5_AUXILIARY_DRIVER_NAME) == 0)))))
3267 			break;
3268 		port_id++;
3269 	}
3270 	if (port_id >= RTE_MAX_ETHPORTS)
3271 		return RTE_MAX_ETHPORTS;
3272 	return port_id;
3273 }
3274 
3275 /**
3276  * Callback to remove a device.
3277  *
3278  * This function removes all Ethernet devices belong to a given device.
3279  *
3280  * @param[in] cdev
3281  *   Pointer to the generic device.
3282  *
3283  * @return
3284  *   0 on success, the function cannot fail.
3285  */
3286 int
3287 mlx5_net_remove(struct mlx5_common_device *cdev)
3288 {
3289 	uint16_t port_id;
3290 	int ret = 0;
3291 
3292 	RTE_ETH_FOREACH_DEV_OF(port_id, cdev->dev) {
3293 		/*
3294 		 * mlx5_dev_close() is not registered to secondary process,
3295 		 * call the close function explicitly for secondary process.
3296 		 */
3297 		if (rte_eal_process_type() == RTE_PROC_SECONDARY)
3298 			ret |= mlx5_dev_close(&rte_eth_devices[port_id]);
3299 		else
3300 			ret |= rte_eth_dev_close(port_id);
3301 	}
3302 	return ret == 0 ? 0 : -EIO;
3303 }
3304 
3305 static const struct rte_pci_id mlx5_pci_id_map[] = {
3306 	{
3307 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3308 			       PCI_DEVICE_ID_MELLANOX_CONNECTX4)
3309 	},
3310 	{
3311 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3312 			       PCI_DEVICE_ID_MELLANOX_CONNECTX4VF)
3313 	},
3314 	{
3315 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3316 			       PCI_DEVICE_ID_MELLANOX_CONNECTX4LX)
3317 	},
3318 	{
3319 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3320 			       PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF)
3321 	},
3322 	{
3323 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3324 			       PCI_DEVICE_ID_MELLANOX_CONNECTX5)
3325 	},
3326 	{
3327 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3328 			       PCI_DEVICE_ID_MELLANOX_CONNECTX5VF)
3329 	},
3330 	{
3331 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3332 			       PCI_DEVICE_ID_MELLANOX_CONNECTX5EX)
3333 	},
3334 	{
3335 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3336 			       PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF)
3337 	},
3338 	{
3339 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3340 			       PCI_DEVICE_ID_MELLANOX_BLUEFIELD)
3341 	},
3342 	{
3343 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3344 			       PCI_DEVICE_ID_MELLANOX_BLUEFIELDVF)
3345 	},
3346 	{
3347 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3348 				PCI_DEVICE_ID_MELLANOX_CONNECTX6)
3349 	},
3350 	{
3351 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3352 				PCI_DEVICE_ID_MELLANOX_CONNECTX6VF)
3353 	},
3354 	{
3355 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3356 				PCI_DEVICE_ID_MELLANOX_CONNECTX6DX)
3357 	},
3358 	{
3359 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3360 				PCI_DEVICE_ID_MELLANOX_CONNECTXVF)
3361 	},
3362 	{
3363 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3364 				PCI_DEVICE_ID_MELLANOX_BLUEFIELD2)
3365 	},
3366 	{
3367 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3368 				PCI_DEVICE_ID_MELLANOX_CONNECTX6LX)
3369 	},
3370 	{
3371 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3372 				PCI_DEVICE_ID_MELLANOX_CONNECTX7)
3373 	},
3374 	{
3375 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
3376 				PCI_DEVICE_ID_MELLANOX_BLUEFIELD3)
3377 	},
3378 	{
3379 		.vendor_id = 0
3380 	}
3381 };
3382 
3383 static struct mlx5_class_driver mlx5_net_driver = {
3384 	.drv_class = MLX5_CLASS_ETH,
3385 	.name = RTE_STR(MLX5_ETH_DRIVER_NAME),
3386 	.id_table = mlx5_pci_id_map,
3387 	.probe = mlx5_os_net_probe,
3388 	.remove = mlx5_net_remove,
3389 	.probe_again = 1,
3390 	.intr_lsc = 1,
3391 	.intr_rmv = 1,
3392 };
3393 
3394 /* Initialize driver log type. */
3395 RTE_LOG_REGISTER_DEFAULT(mlx5_logtype, NOTICE)
3396 
3397 /**
3398  * Driver initialization routine.
3399  */
3400 RTE_INIT(rte_mlx5_pmd_init)
3401 {
3402 	pthread_mutex_init(&mlx5_dev_ctx_list_mutex, NULL);
3403 	mlx5_common_init();
3404 	/* Build the static tables for Verbs conversion. */
3405 	mlx5_set_ptype_table();
3406 	mlx5_set_cksum_table();
3407 	mlx5_set_swp_types_table();
3408 	if (mlx5_glue)
3409 		mlx5_class_driver_register(&mlx5_net_driver);
3410 }
3411 
3412 RTE_PMD_EXPORT_NAME(MLX5_ETH_DRIVER_NAME, __COUNTER__);
3413 RTE_PMD_REGISTER_PCI_TABLE(MLX5_ETH_DRIVER_NAME, mlx5_pci_id_map);
3414 RTE_PMD_REGISTER_KMOD_DEP(MLX5_ETH_DRIVER_NAME, "* ib_uverbs & mlx5_core & mlx5_ib");
3415