xref: /dpdk/drivers/net/mlx5/linux/mlx5_os.c (revision aec086c9f1c81b583ca8b9f7b50f32452d27840e)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2020 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 <net/if.h>
13 #include <sys/mman.h>
14 #include <linux/rtnetlink.h>
15 #include <linux/sockios.h>
16 #include <linux/ethtool.h>
17 #include <fcntl.h>
18 
19 /* Verbs header. */
20 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
21 #ifdef PEDANTIC
22 #pragma GCC diagnostic ignored "-Wpedantic"
23 #endif
24 #include <infiniband/verbs.h>
25 #ifdef PEDANTIC
26 #pragma GCC diagnostic error "-Wpedantic"
27 #endif
28 
29 #include <rte_malloc.h>
30 #include <rte_ethdev_driver.h>
31 #include <rte_ethdev_pci.h>
32 #include <rte_pci.h>
33 #include <rte_bus_pci.h>
34 #include <rte_common.h>
35 #include <rte_kvargs.h>
36 #include <rte_rwlock.h>
37 #include <rte_spinlock.h>
38 #include <rte_string_fns.h>
39 #include <rte_alarm.h>
40 
41 #include <mlx5_glue.h>
42 #include <mlx5_devx_cmds.h>
43 #include <mlx5_common.h>
44 #include <mlx5_common_mp.h>
45 #include <mlx5_common_mr.h>
46 
47 #include "mlx5_defs.h"
48 #include "mlx5.h"
49 #include "mlx5_utils.h"
50 #include "mlx5_rxtx.h"
51 #include "mlx5_autoconf.h"
52 #include "mlx5_mr.h"
53 #include "mlx5_flow.h"
54 #include "rte_pmd_mlx5.h"
55 #include "mlx5_verbs.h"
56 
57 #define MLX5_TAGS_HLIST_ARRAY_SIZE 8192
58 
59 #ifndef HAVE_IBV_MLX5_MOD_MPW
60 #define MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED (1 << 2)
61 #define MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW (1 << 3)
62 #endif
63 
64 #ifndef HAVE_IBV_MLX5_MOD_CQE_128B_COMP
65 #define MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP (1 << 4)
66 #endif
67 
68 /**
69  * Get device name. Given an ibv_device pointer - return a
70  * pointer to the corresponding device name.
71  *
72  * @param[in] dev
73  *   Pointer to ibv device.
74  *
75  * @return
76  *   Pointer to device name if dev is valid, NULL otherwise.
77  */
78 const char *
79 mlx5_os_get_dev_device_name(void *dev)
80 {
81 	if (!dev)
82 		return NULL;
83 	return ((struct ibv_device *)dev)->name;
84 }
85 
86 /**
87  * Get ibv device name. Given an ibv_context pointer - return a
88  * pointer to the corresponding device name.
89  *
90  * @param[in] ctx
91  *   Pointer to ibv context.
92  *
93  * @return
94  *   Pointer to device name if ctx is valid, NULL otherwise.
95  */
96 const char *
97 mlx5_os_get_ctx_device_name(void *ctx)
98 {
99 	if (!ctx)
100 		return NULL;
101 	return ((struct ibv_context *)ctx)->device->name;
102 }
103 
104 /**
105  * Get ibv device path name. Given an ibv_context pointer - return a
106  * pointer to the corresponding device path name.
107  *
108  * @param[in] ctx
109  *   Pointer to ibv context.
110  *
111  * @return
112  *   Pointer to device path name if ctx is valid, NULL otherwise.
113  */
114 const char *
115 mlx5_os_get_ctx_device_path(void *ctx)
116 {
117 	if (!ctx)
118 		return NULL;
119 
120 	return ((struct ibv_context *)ctx)->device->ibdev_path;
121 }
122 
123 /**
124  * Get umem id. Given a pointer to umem object of type
125  * 'struct mlx5dv_devx_umem *' - return its id.
126  *
127  * @param[in] umem
128  *   Pointer to umem object.
129  *
130  * @return
131  *   The umem id if umem is valid, 0 otherwise.
132  */
133 uint32_t
134 mlx5_os_get_umem_id(void *umem)
135 {
136 	if (!umem)
137 		return 0;
138 	return ((struct mlx5dv_devx_umem *)umem)->umem_id;
139 }
140 
141 /**
142  * Get mlx5 device attributes. The glue function query_device_ex() is called
143  * with out parameter of type 'struct ibv_device_attr_ex *'. Then fill in mlx5
144  * device attributes from the glue out parameter.
145  *
146  * @param dev
147  *   Pointer to ibv context.
148  *
149  * @param device_attr
150  *   Pointer to mlx5 device attributes.
151  *
152  * @return
153  *   0 on success, non zero error number otherwise
154  */
155 int
156 mlx5_os_get_dev_attr(void *ctx, struct mlx5_dev_attr *device_attr)
157 {
158 	int err;
159 	struct ibv_device_attr_ex attr_ex;
160 	memset(device_attr, 0, sizeof(*device_attr));
161 	err = mlx5_glue->query_device_ex(ctx, NULL, &attr_ex);
162 	if (err)
163 		return err;
164 
165 	device_attr->device_cap_flags_ex = attr_ex.device_cap_flags_ex;
166 	device_attr->max_qp_wr = attr_ex.orig_attr.max_qp_wr;
167 	device_attr->max_sge = attr_ex.orig_attr.max_sge;
168 	device_attr->max_cq = attr_ex.orig_attr.max_cq;
169 	device_attr->max_qp = attr_ex.orig_attr.max_qp;
170 	device_attr->raw_packet_caps = attr_ex.raw_packet_caps;
171 	device_attr->max_rwq_indirection_table_size =
172 		attr_ex.rss_caps.max_rwq_indirection_table_size;
173 	device_attr->max_tso = attr_ex.tso_caps.max_tso;
174 	device_attr->tso_supported_qpts = attr_ex.tso_caps.supported_qpts;
175 
176 	struct mlx5dv_context dv_attr = { .comp_mask = 0 };
177 	err = mlx5_glue->dv_query_device(ctx, &dv_attr);
178 	if (err)
179 		return err;
180 
181 	device_attr->flags = dv_attr.flags;
182 	device_attr->comp_mask = dv_attr.comp_mask;
183 #ifdef HAVE_IBV_MLX5_MOD_SWP
184 	device_attr->sw_parsing_offloads =
185 		dv_attr.sw_parsing_caps.sw_parsing_offloads;
186 #endif
187 	device_attr->min_single_stride_log_num_of_bytes =
188 		dv_attr.striding_rq_caps.min_single_stride_log_num_of_bytes;
189 	device_attr->max_single_stride_log_num_of_bytes =
190 		dv_attr.striding_rq_caps.max_single_stride_log_num_of_bytes;
191 	device_attr->min_single_wqe_log_num_of_strides =
192 		dv_attr.striding_rq_caps.min_single_wqe_log_num_of_strides;
193 	device_attr->max_single_wqe_log_num_of_strides =
194 		dv_attr.striding_rq_caps.max_single_wqe_log_num_of_strides;
195 	device_attr->stride_supported_qpts =
196 		dv_attr.striding_rq_caps.supported_qpts;
197 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
198 	device_attr->tunnel_offloads_caps = dv_attr.tunnel_offloads_caps;
199 #endif
200 
201 	return err;
202 }
203 
204 /**
205  * Verbs callback to allocate a memory. This function should allocate the space
206  * according to the size provided residing inside a huge page.
207  * Please note that all allocation must respect the alignment from libmlx5
208  * (i.e. currently sysconf(_SC_PAGESIZE)).
209  *
210  * @param[in] size
211  *   The size in bytes of the memory to allocate.
212  * @param[in] data
213  *   A pointer to the callback data.
214  *
215  * @return
216  *   Allocated buffer, NULL otherwise and rte_errno is set.
217  */
218 static void *
219 mlx5_alloc_verbs_buf(size_t size, void *data)
220 {
221 	struct mlx5_priv *priv = data;
222 	void *ret;
223 	size_t alignment = sysconf(_SC_PAGESIZE);
224 	unsigned int socket = SOCKET_ID_ANY;
225 
226 	if (priv->verbs_alloc_ctx.type == MLX5_VERBS_ALLOC_TYPE_TX_QUEUE) {
227 		const struct mlx5_txq_ctrl *ctrl = priv->verbs_alloc_ctx.obj;
228 
229 		socket = ctrl->socket;
230 	} else if (priv->verbs_alloc_ctx.type ==
231 		   MLX5_VERBS_ALLOC_TYPE_RX_QUEUE) {
232 		const struct mlx5_rxq_ctrl *ctrl = priv->verbs_alloc_ctx.obj;
233 
234 		socket = ctrl->socket;
235 	}
236 	MLX5_ASSERT(data != NULL);
237 	ret = rte_malloc_socket(__func__, size, alignment, socket);
238 	if (!ret && size)
239 		rte_errno = ENOMEM;
240 	return ret;
241 }
242 
243 /**
244  * Verbs callback to free a memory.
245  *
246  * @param[in] ptr
247  *   A pointer to the memory to free.
248  * @param[in] data
249  *   A pointer to the callback data.
250  */
251 static void
252 mlx5_free_verbs_buf(void *ptr, void *data __rte_unused)
253 {
254 	MLX5_ASSERT(data != NULL);
255 	rte_free(ptr);
256 }
257 
258 /**
259  * Initialize DR related data within private structure.
260  * Routine checks the reference counter and does actual
261  * resources creation/initialization only if counter is zero.
262  *
263  * @param[in] priv
264  *   Pointer to the private device data structure.
265  *
266  * @return
267  *   Zero on success, positive error code otherwise.
268  */
269 static int
270 mlx5_alloc_shared_dr(struct mlx5_priv *priv)
271 {
272 	struct mlx5_dev_ctx_shared *sh = priv->sh;
273 	char s[MLX5_HLIST_NAMESIZE];
274 	int err = 0;
275 
276 	if (!sh->flow_tbls)
277 		err = mlx5_alloc_table_hash_list(priv);
278 	else
279 		DRV_LOG(DEBUG, "sh->flow_tbls[%p] already created, reuse\n",
280 			(void *)sh->flow_tbls);
281 	if (err)
282 		return err;
283 	/* Create tags hash list table. */
284 	snprintf(s, sizeof(s), "%s_tags", sh->ibdev_name);
285 	sh->tag_table = mlx5_hlist_create(s, MLX5_TAGS_HLIST_ARRAY_SIZE);
286 	if (!sh->tag_table) {
287 		DRV_LOG(ERR, "tags with hash creation failed.\n");
288 		err = ENOMEM;
289 		goto error;
290 	}
291 #ifdef HAVE_MLX5DV_DR
292 	void *domain;
293 
294 	if (sh->dv_refcnt) {
295 		/* Shared DV/DR structures is already initialized. */
296 		sh->dv_refcnt++;
297 		priv->dr_shared = 1;
298 		return 0;
299 	}
300 	/* Reference counter is zero, we should initialize structures. */
301 	domain = mlx5_glue->dr_create_domain(sh->ctx,
302 					     MLX5DV_DR_DOMAIN_TYPE_NIC_RX);
303 	if (!domain) {
304 		DRV_LOG(ERR, "ingress mlx5dv_dr_create_domain failed");
305 		err = errno;
306 		goto error;
307 	}
308 	sh->rx_domain = domain;
309 	domain = mlx5_glue->dr_create_domain(sh->ctx,
310 					     MLX5DV_DR_DOMAIN_TYPE_NIC_TX);
311 	if (!domain) {
312 		DRV_LOG(ERR, "egress mlx5dv_dr_create_domain failed");
313 		err = errno;
314 		goto error;
315 	}
316 	pthread_mutex_init(&sh->dv_mutex, NULL);
317 	sh->tx_domain = domain;
318 #ifdef HAVE_MLX5DV_DR_ESWITCH
319 	if (priv->config.dv_esw_en) {
320 		domain  = mlx5_glue->dr_create_domain
321 			(sh->ctx, MLX5DV_DR_DOMAIN_TYPE_FDB);
322 		if (!domain) {
323 			DRV_LOG(ERR, "FDB mlx5dv_dr_create_domain failed");
324 			err = errno;
325 			goto error;
326 		}
327 		sh->fdb_domain = domain;
328 		sh->esw_drop_action = mlx5_glue->dr_create_flow_action_drop();
329 	}
330 #endif
331 	if (priv->config.reclaim_mode == MLX5_RCM_AGGR) {
332 		mlx5_glue->dr_reclaim_domain_memory(sh->rx_domain, 1);
333 		mlx5_glue->dr_reclaim_domain_memory(sh->tx_domain, 1);
334 		if (sh->fdb_domain)
335 			mlx5_glue->dr_reclaim_domain_memory(sh->fdb_domain, 1);
336 	}
337 	sh->pop_vlan_action = mlx5_glue->dr_create_flow_action_pop_vlan();
338 #endif /* HAVE_MLX5DV_DR */
339 	sh->dv_refcnt++;
340 	priv->dr_shared = 1;
341 	return 0;
342 error:
343 	/* Rollback the created objects. */
344 	if (sh->rx_domain) {
345 		mlx5_glue->dr_destroy_domain(sh->rx_domain);
346 		sh->rx_domain = NULL;
347 	}
348 	if (sh->tx_domain) {
349 		mlx5_glue->dr_destroy_domain(sh->tx_domain);
350 		sh->tx_domain = NULL;
351 	}
352 	if (sh->fdb_domain) {
353 		mlx5_glue->dr_destroy_domain(sh->fdb_domain);
354 		sh->fdb_domain = NULL;
355 	}
356 	if (sh->esw_drop_action) {
357 		mlx5_glue->destroy_flow_action(sh->esw_drop_action);
358 		sh->esw_drop_action = NULL;
359 	}
360 	if (sh->pop_vlan_action) {
361 		mlx5_glue->destroy_flow_action(sh->pop_vlan_action);
362 		sh->pop_vlan_action = NULL;
363 	}
364 	if (sh->tag_table) {
365 		/* tags should be destroyed with flow before. */
366 		mlx5_hlist_destroy(sh->tag_table, NULL, NULL);
367 		sh->tag_table = NULL;
368 	}
369 	mlx5_free_table_hash_list(priv);
370 	return err;
371 }
372 
373 /**
374  * Destroy DR related data within private structure.
375  *
376  * @param[in] priv
377  *   Pointer to the private device data structure.
378  */
379 void
380 mlx5_os_free_shared_dr(struct mlx5_priv *priv)
381 {
382 	struct mlx5_dev_ctx_shared *sh;
383 
384 	if (!priv->dr_shared)
385 		return;
386 	priv->dr_shared = 0;
387 	sh = priv->sh;
388 	MLX5_ASSERT(sh);
389 #ifdef HAVE_MLX5DV_DR
390 	MLX5_ASSERT(sh->dv_refcnt);
391 	if (sh->dv_refcnt && --sh->dv_refcnt)
392 		return;
393 	if (sh->rx_domain) {
394 		mlx5_glue->dr_destroy_domain(sh->rx_domain);
395 		sh->rx_domain = NULL;
396 	}
397 	if (sh->tx_domain) {
398 		mlx5_glue->dr_destroy_domain(sh->tx_domain);
399 		sh->tx_domain = NULL;
400 	}
401 #ifdef HAVE_MLX5DV_DR_ESWITCH
402 	if (sh->fdb_domain) {
403 		mlx5_glue->dr_destroy_domain(sh->fdb_domain);
404 		sh->fdb_domain = NULL;
405 	}
406 	if (sh->esw_drop_action) {
407 		mlx5_glue->destroy_flow_action(sh->esw_drop_action);
408 		sh->esw_drop_action = NULL;
409 	}
410 #endif
411 	if (sh->pop_vlan_action) {
412 		mlx5_glue->destroy_flow_action(sh->pop_vlan_action);
413 		sh->pop_vlan_action = NULL;
414 	}
415 	pthread_mutex_destroy(&sh->dv_mutex);
416 #endif /* HAVE_MLX5DV_DR */
417 	if (sh->tag_table) {
418 		/* tags should be destroyed with flow before. */
419 		mlx5_hlist_destroy(sh->tag_table, NULL, NULL);
420 		sh->tag_table = NULL;
421 	}
422 	mlx5_free_table_hash_list(priv);
423 }
424 
425 /**
426  * Spawn an Ethernet device from Verbs information.
427  *
428  * @param dpdk_dev
429  *   Backing DPDK device.
430  * @param spawn
431  *   Verbs device parameters (name, port, switch_info) to spawn.
432  * @param config
433  *   Device configuration parameters.
434  *
435  * @return
436  *   A valid Ethernet device object on success, NULL otherwise and rte_errno
437  *   is set. The following errors are defined:
438  *
439  *   EBUSY: device is not supposed to be spawned.
440  *   EEXIST: device is already spawned
441  */
442 static struct rte_eth_dev *
443 mlx5_dev_spawn(struct rte_device *dpdk_dev,
444 	       struct mlx5_dev_spawn_data *spawn,
445 	       struct mlx5_dev_config config)
446 {
447 	const struct mlx5_switch_info *switch_info = &spawn->info;
448 	struct mlx5_dev_ctx_shared *sh = NULL;
449 	struct ibv_port_attr port_attr;
450 	struct mlx5dv_context dv_attr = { .comp_mask = 0 };
451 	struct rte_eth_dev *eth_dev = NULL;
452 	struct mlx5_priv *priv = NULL;
453 	int err = 0;
454 	unsigned int hw_padding = 0;
455 	unsigned int mps;
456 	unsigned int cqe_comp;
457 	unsigned int cqe_pad = 0;
458 	unsigned int tunnel_en = 0;
459 	unsigned int mpls_en = 0;
460 	unsigned int swp = 0;
461 	unsigned int mprq = 0;
462 	unsigned int mprq_min_stride_size_n = 0;
463 	unsigned int mprq_max_stride_size_n = 0;
464 	unsigned int mprq_min_stride_num_n = 0;
465 	unsigned int mprq_max_stride_num_n = 0;
466 	struct rte_ether_addr mac;
467 	char name[RTE_ETH_NAME_MAX_LEN];
468 	int own_domain_id = 0;
469 	uint16_t port_id;
470 	unsigned int i;
471 #ifdef HAVE_MLX5DV_DR_DEVX_PORT
472 	struct mlx5dv_devx_port devx_port = { .comp_mask = 0 };
473 #endif
474 
475 	/* Determine if this port representor is supposed to be spawned. */
476 	if (switch_info->representor && dpdk_dev->devargs) {
477 		struct rte_eth_devargs eth_da;
478 
479 		err = rte_eth_devargs_parse(dpdk_dev->devargs->args, &eth_da);
480 		if (err) {
481 			rte_errno = -err;
482 			DRV_LOG(ERR, "failed to process device arguments: %s",
483 				strerror(rte_errno));
484 			return NULL;
485 		}
486 		for (i = 0; i < eth_da.nb_representor_ports; ++i)
487 			if (eth_da.representor_ports[i] ==
488 			    (uint16_t)switch_info->port_name)
489 				break;
490 		if (i == eth_da.nb_representor_ports) {
491 			rte_errno = EBUSY;
492 			return NULL;
493 		}
494 	}
495 	/* Build device name. */
496 	if (spawn->pf_bond <  0) {
497 		/* Single device. */
498 		if (!switch_info->representor)
499 			strlcpy(name, dpdk_dev->name, sizeof(name));
500 		else
501 			snprintf(name, sizeof(name), "%s_representor_%u",
502 				 dpdk_dev->name, switch_info->port_name);
503 	} else {
504 		/* Bonding device. */
505 		if (!switch_info->representor)
506 			snprintf(name, sizeof(name), "%s_%s",
507 				 dpdk_dev->name,
508 				 mlx5_os_get_dev_device_name(spawn->phys_dev));
509 		else
510 			snprintf(name, sizeof(name), "%s_%s_representor_%u",
511 				 dpdk_dev->name,
512 				 mlx5_os_get_dev_device_name(spawn->phys_dev),
513 				 switch_info->port_name);
514 	}
515 	/* check if the device is already spawned */
516 	if (rte_eth_dev_get_port_by_name(name, &port_id) == 0) {
517 		rte_errno = EEXIST;
518 		return NULL;
519 	}
520 	DRV_LOG(DEBUG, "naming Ethernet device \"%s\"", name);
521 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
522 		struct mlx5_mp_id mp_id;
523 
524 		eth_dev = rte_eth_dev_attach_secondary(name);
525 		if (eth_dev == NULL) {
526 			DRV_LOG(ERR, "can not attach rte ethdev");
527 			rte_errno = ENOMEM;
528 			return NULL;
529 		}
530 		eth_dev->device = dpdk_dev;
531 		eth_dev->dev_ops = &mlx5_os_dev_sec_ops;
532 		err = mlx5_proc_priv_init(eth_dev);
533 		if (err)
534 			return NULL;
535 		mp_id.port_id = eth_dev->data->port_id;
536 		strlcpy(mp_id.name, MLX5_MP_NAME, RTE_MP_MAX_NAME_LEN);
537 		/* Receive command fd from primary process */
538 		err = mlx5_mp_req_verbs_cmd_fd(&mp_id);
539 		if (err < 0)
540 			goto err_secondary;
541 		/* Remap UAR for Tx queues. */
542 		err = mlx5_tx_uar_init_secondary(eth_dev, err);
543 		if (err)
544 			goto err_secondary;
545 		/*
546 		 * Ethdev pointer is still required as input since
547 		 * the primary device is not accessible from the
548 		 * secondary process.
549 		 */
550 		eth_dev->rx_pkt_burst = mlx5_select_rx_function(eth_dev);
551 		eth_dev->tx_pkt_burst = mlx5_select_tx_function(eth_dev);
552 		return eth_dev;
553 err_secondary:
554 		mlx5_dev_close(eth_dev);
555 		return NULL;
556 	}
557 	/*
558 	 * Some parameters ("tx_db_nc" in particularly) are needed in
559 	 * advance to create dv/verbs device context. We proceed the
560 	 * devargs here to get ones, and later proceed devargs again
561 	 * to override some hardware settings.
562 	 */
563 	err = mlx5_args(&config, dpdk_dev->devargs);
564 	if (err) {
565 		err = rte_errno;
566 		DRV_LOG(ERR, "failed to process device arguments: %s",
567 			strerror(rte_errno));
568 		goto error;
569 	}
570 	sh = mlx5_alloc_shared_dev_ctx(spawn, &config);
571 	if (!sh)
572 		return NULL;
573 	config.devx = sh->devx;
574 #ifdef HAVE_MLX5DV_DR_ACTION_DEST_DEVX_TIR
575 	config.dest_tir = 1;
576 #endif
577 #ifdef HAVE_IBV_MLX5_MOD_SWP
578 	dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_SWP;
579 #endif
580 	/*
581 	 * Multi-packet send is supported by ConnectX-4 Lx PF as well
582 	 * as all ConnectX-5 devices.
583 	 */
584 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
585 	dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS;
586 #endif
587 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
588 	dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_STRIDING_RQ;
589 #endif
590 	mlx5_glue->dv_query_device(sh->ctx, &dv_attr);
591 	if (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED) {
592 		if (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW) {
593 			DRV_LOG(DEBUG, "enhanced MPW is supported");
594 			mps = MLX5_MPW_ENHANCED;
595 		} else {
596 			DRV_LOG(DEBUG, "MPW is supported");
597 			mps = MLX5_MPW;
598 		}
599 	} else {
600 		DRV_LOG(DEBUG, "MPW isn't supported");
601 		mps = MLX5_MPW_DISABLED;
602 	}
603 #ifdef HAVE_IBV_MLX5_MOD_SWP
604 	if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_SWP)
605 		swp = dv_attr.sw_parsing_caps.sw_parsing_offloads;
606 	DRV_LOG(DEBUG, "SWP support: %u", swp);
607 #endif
608 	config.swp = !!swp;
609 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
610 	if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_STRIDING_RQ) {
611 		struct mlx5dv_striding_rq_caps mprq_caps =
612 			dv_attr.striding_rq_caps;
613 
614 		DRV_LOG(DEBUG, "\tmin_single_stride_log_num_of_bytes: %d",
615 			mprq_caps.min_single_stride_log_num_of_bytes);
616 		DRV_LOG(DEBUG, "\tmax_single_stride_log_num_of_bytes: %d",
617 			mprq_caps.max_single_stride_log_num_of_bytes);
618 		DRV_LOG(DEBUG, "\tmin_single_wqe_log_num_of_strides: %d",
619 			mprq_caps.min_single_wqe_log_num_of_strides);
620 		DRV_LOG(DEBUG, "\tmax_single_wqe_log_num_of_strides: %d",
621 			mprq_caps.max_single_wqe_log_num_of_strides);
622 		DRV_LOG(DEBUG, "\tsupported_qpts: %d",
623 			mprq_caps.supported_qpts);
624 		DRV_LOG(DEBUG, "device supports Multi-Packet RQ");
625 		mprq = 1;
626 		mprq_min_stride_size_n =
627 			mprq_caps.min_single_stride_log_num_of_bytes;
628 		mprq_max_stride_size_n =
629 			mprq_caps.max_single_stride_log_num_of_bytes;
630 		mprq_min_stride_num_n =
631 			mprq_caps.min_single_wqe_log_num_of_strides;
632 		mprq_max_stride_num_n =
633 			mprq_caps.max_single_wqe_log_num_of_strides;
634 	}
635 #endif
636 	if (RTE_CACHE_LINE_SIZE == 128 &&
637 	    !(dv_attr.flags & MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP))
638 		cqe_comp = 0;
639 	else
640 		cqe_comp = 1;
641 	config.cqe_comp = cqe_comp;
642 #ifdef HAVE_IBV_MLX5_MOD_CQE_128B_PAD
643 	/* Whether device supports 128B Rx CQE padding. */
644 	cqe_pad = RTE_CACHE_LINE_SIZE == 128 &&
645 		  (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_CQE_128B_PAD);
646 #endif
647 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
648 	if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS) {
649 		tunnel_en = ((dv_attr.tunnel_offloads_caps &
650 			      MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_VXLAN) &&
651 			     (dv_attr.tunnel_offloads_caps &
652 			      MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_GRE) &&
653 			     (dv_attr.tunnel_offloads_caps &
654 			      MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_GENEVE));
655 	}
656 	DRV_LOG(DEBUG, "tunnel offloading is %ssupported",
657 		tunnel_en ? "" : "not ");
658 #else
659 	DRV_LOG(WARNING,
660 		"tunnel offloading disabled due to old OFED/rdma-core version");
661 #endif
662 	config.tunnel_en = tunnel_en;
663 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
664 	mpls_en = ((dv_attr.tunnel_offloads_caps &
665 		    MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_CW_MPLS_OVER_GRE) &&
666 		   (dv_attr.tunnel_offloads_caps &
667 		    MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_CW_MPLS_OVER_UDP));
668 	DRV_LOG(DEBUG, "MPLS over GRE/UDP tunnel offloading is %ssupported",
669 		mpls_en ? "" : "not ");
670 #else
671 	DRV_LOG(WARNING, "MPLS over GRE/UDP tunnel offloading disabled due to"
672 		" old OFED/rdma-core version or firmware configuration");
673 #endif
674 	config.mpls_en = mpls_en;
675 	/* Check port status. */
676 	err = mlx5_glue->query_port(sh->ctx, spawn->phys_port, &port_attr);
677 	if (err) {
678 		DRV_LOG(ERR, "port query failed: %s", strerror(err));
679 		goto error;
680 	}
681 	if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
682 		DRV_LOG(ERR, "port is not configured in Ethernet mode");
683 		err = EINVAL;
684 		goto error;
685 	}
686 	if (port_attr.state != IBV_PORT_ACTIVE)
687 		DRV_LOG(DEBUG, "port is not active: \"%s\" (%d)",
688 			mlx5_glue->port_state_str(port_attr.state),
689 			port_attr.state);
690 	/* Allocate private eth device data. */
691 	priv = rte_zmalloc("ethdev private structure",
692 			   sizeof(*priv),
693 			   RTE_CACHE_LINE_SIZE);
694 	if (priv == NULL) {
695 		DRV_LOG(ERR, "priv allocation failure");
696 		err = ENOMEM;
697 		goto error;
698 	}
699 	priv->sh = sh;
700 	priv->dev_port = spawn->phys_port;
701 	priv->pci_dev = spawn->pci_dev;
702 	priv->mtu = RTE_ETHER_MTU;
703 	priv->mp_id.port_id = port_id;
704 	strlcpy(priv->mp_id.name, MLX5_MP_NAME, RTE_MP_MAX_NAME_LEN);
705 #ifndef RTE_ARCH_64
706 	/* Initialize UAR access locks for 32bit implementations. */
707 	rte_spinlock_init(&priv->uar_lock_cq);
708 	for (i = 0; i < MLX5_UAR_PAGE_NUM_MAX; i++)
709 		rte_spinlock_init(&priv->uar_lock[i]);
710 #endif
711 	/* Some internal functions rely on Netlink sockets, open them now. */
712 	priv->nl_socket_rdma = mlx5_nl_init(NETLINK_RDMA);
713 	priv->nl_socket_route =	mlx5_nl_init(NETLINK_ROUTE);
714 	priv->representor = !!switch_info->representor;
715 	priv->master = !!switch_info->master;
716 	priv->domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
717 	priv->vport_meta_tag = 0;
718 	priv->vport_meta_mask = 0;
719 	priv->pf_bond = spawn->pf_bond;
720 #ifdef HAVE_MLX5DV_DR_DEVX_PORT
721 	/*
722 	 * The DevX port query API is implemented. E-Switch may use
723 	 * either vport or reg_c[0] metadata register to match on
724 	 * vport index. The engaged part of metadata register is
725 	 * defined by mask.
726 	 */
727 	if (switch_info->representor || switch_info->master) {
728 		devx_port.comp_mask = MLX5DV_DEVX_PORT_VPORT |
729 				      MLX5DV_DEVX_PORT_MATCH_REG_C_0;
730 		err = mlx5_glue->devx_port_query(sh->ctx, spawn->phys_port,
731 						 &devx_port);
732 		if (err) {
733 			DRV_LOG(WARNING,
734 				"can't query devx port %d on device %s",
735 				spawn->phys_port,
736 				mlx5_os_get_dev_device_name(spawn->phys_dev));
737 			devx_port.comp_mask = 0;
738 		}
739 	}
740 	if (devx_port.comp_mask & MLX5DV_DEVX_PORT_MATCH_REG_C_0) {
741 		priv->vport_meta_tag = devx_port.reg_c_0.value;
742 		priv->vport_meta_mask = devx_port.reg_c_0.mask;
743 		if (!priv->vport_meta_mask) {
744 			DRV_LOG(ERR, "vport zero mask for port %d"
745 				     " on bonding device %s",
746 				     spawn->phys_port,
747 				     mlx5_os_get_dev_device_name
748 							(spawn->phys_dev));
749 			err = ENOTSUP;
750 			goto error;
751 		}
752 		if (priv->vport_meta_tag & ~priv->vport_meta_mask) {
753 			DRV_LOG(ERR, "invalid vport tag for port %d"
754 				     " on bonding device %s",
755 				     spawn->phys_port,
756 				     mlx5_os_get_dev_device_name
757 							(spawn->phys_dev));
758 			err = ENOTSUP;
759 			goto error;
760 		}
761 	}
762 	if (devx_port.comp_mask & MLX5DV_DEVX_PORT_VPORT) {
763 		priv->vport_id = devx_port.vport_num;
764 	} else if (spawn->pf_bond >= 0) {
765 		DRV_LOG(ERR, "can't deduce vport index for port %d"
766 			     " on bonding device %s",
767 			     spawn->phys_port,
768 			     mlx5_os_get_dev_device_name(spawn->phys_dev));
769 		err = ENOTSUP;
770 		goto error;
771 	} else {
772 		/* Suppose vport index in compatible way. */
773 		priv->vport_id = switch_info->representor ?
774 				 switch_info->port_name + 1 : -1;
775 	}
776 #else
777 	/*
778 	 * Kernel/rdma_core support single E-Switch per PF configurations
779 	 * only and vport_id field contains the vport index for
780 	 * associated VF, which is deduced from representor port name.
781 	 * For example, let's have the IB device port 10, it has
782 	 * attached network device eth0, which has port name attribute
783 	 * pf0vf2, we can deduce the VF number as 2, and set vport index
784 	 * as 3 (2+1). This assigning schema should be changed if the
785 	 * multiple E-Switch instances per PF configurations or/and PCI
786 	 * subfunctions are added.
787 	 */
788 	priv->vport_id = switch_info->representor ?
789 			 switch_info->port_name + 1 : -1;
790 #endif
791 	/* representor_id field keeps the unmodified VF index. */
792 	priv->representor_id = switch_info->representor ?
793 			       switch_info->port_name : -1;
794 	/*
795 	 * Look for sibling devices in order to reuse their switch domain
796 	 * if any, otherwise allocate one.
797 	 */
798 	MLX5_ETH_FOREACH_DEV(port_id, priv->pci_dev) {
799 		const struct mlx5_priv *opriv =
800 			rte_eth_devices[port_id].data->dev_private;
801 
802 		if (!opriv ||
803 		    opriv->sh != priv->sh ||
804 			opriv->domain_id ==
805 			RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID)
806 			continue;
807 		priv->domain_id = opriv->domain_id;
808 		break;
809 	}
810 	if (priv->domain_id == RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
811 		err = rte_eth_switch_domain_alloc(&priv->domain_id);
812 		if (err) {
813 			err = rte_errno;
814 			DRV_LOG(ERR, "unable to allocate switch domain: %s",
815 				strerror(rte_errno));
816 			goto error;
817 		}
818 		own_domain_id = 1;
819 	}
820 	/* Override some values set by hardware configuration. */
821 	mlx5_args(&config, dpdk_dev->devargs);
822 	err = mlx5_dev_check_sibling_config(priv, &config);
823 	if (err)
824 		goto error;
825 	config.hw_csum = !!(sh->device_attr.device_cap_flags_ex &
826 			    IBV_DEVICE_RAW_IP_CSUM);
827 	DRV_LOG(DEBUG, "checksum offloading is %ssupported",
828 		(config.hw_csum ? "" : "not "));
829 #if !defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) && \
830 	!defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
831 	DRV_LOG(DEBUG, "counters are not supported");
832 #endif
833 #if !defined(HAVE_IBV_FLOW_DV_SUPPORT) || !defined(HAVE_MLX5DV_DR)
834 	if (config.dv_flow_en) {
835 		DRV_LOG(WARNING, "DV flow is not supported");
836 		config.dv_flow_en = 0;
837 	}
838 #endif
839 	config.ind_table_max_size =
840 		sh->device_attr.max_rwq_indirection_table_size;
841 	/*
842 	 * Remove this check once DPDK supports larger/variable
843 	 * indirection tables.
844 	 */
845 	if (config.ind_table_max_size > (unsigned int)ETH_RSS_RETA_SIZE_512)
846 		config.ind_table_max_size = ETH_RSS_RETA_SIZE_512;
847 	DRV_LOG(DEBUG, "maximum Rx indirection table size is %u",
848 		config.ind_table_max_size);
849 	config.hw_vlan_strip = !!(sh->device_attr.raw_packet_caps &
850 				  IBV_RAW_PACKET_CAP_CVLAN_STRIPPING);
851 	DRV_LOG(DEBUG, "VLAN stripping is %ssupported",
852 		(config.hw_vlan_strip ? "" : "not "));
853 	config.hw_fcs_strip = !!(sh->device_attr.raw_packet_caps &
854 				 IBV_RAW_PACKET_CAP_SCATTER_FCS);
855 	DRV_LOG(DEBUG, "FCS stripping configuration is %ssupported",
856 		(config.hw_fcs_strip ? "" : "not "));
857 #if defined(HAVE_IBV_WQ_FLAG_RX_END_PADDING)
858 	hw_padding = !!sh->device_attr.rx_pad_end_addr_align;
859 #elif defined(HAVE_IBV_WQ_FLAGS_PCI_WRITE_END_PADDING)
860 	hw_padding = !!(sh->device_attr.device_cap_flags_ex &
861 			IBV_DEVICE_PCI_WRITE_END_PADDING);
862 #endif
863 	if (config.hw_padding && !hw_padding) {
864 		DRV_LOG(DEBUG, "Rx end alignment padding isn't supported");
865 		config.hw_padding = 0;
866 	} else if (config.hw_padding) {
867 		DRV_LOG(DEBUG, "Rx end alignment padding is enabled");
868 	}
869 	config.tso = (sh->device_attr.max_tso > 0 &&
870 		      (sh->device_attr.tso_supported_qpts &
871 		       (1 << IBV_QPT_RAW_PACKET)));
872 	if (config.tso)
873 		config.tso_max_payload_sz = sh->device_attr.max_tso;
874 	/*
875 	 * MPW is disabled by default, while the Enhanced MPW is enabled
876 	 * by default.
877 	 */
878 	if (config.mps == MLX5_ARG_UNSET)
879 		config.mps = (mps == MLX5_MPW_ENHANCED) ? MLX5_MPW_ENHANCED :
880 							  MLX5_MPW_DISABLED;
881 	else
882 		config.mps = config.mps ? mps : MLX5_MPW_DISABLED;
883 	DRV_LOG(INFO, "%sMPS is %s",
884 		config.mps == MLX5_MPW_ENHANCED ? "enhanced " :
885 		config.mps == MLX5_MPW ? "legacy " : "",
886 		config.mps != MLX5_MPW_DISABLED ? "enabled" : "disabled");
887 	if (config.cqe_comp && !cqe_comp) {
888 		DRV_LOG(WARNING, "Rx CQE compression isn't supported");
889 		config.cqe_comp = 0;
890 	}
891 	if (config.cqe_pad && !cqe_pad) {
892 		DRV_LOG(WARNING, "Rx CQE padding isn't supported");
893 		config.cqe_pad = 0;
894 	} else if (config.cqe_pad) {
895 		DRV_LOG(INFO, "Rx CQE padding is enabled");
896 	}
897 	if (config.devx) {
898 		priv->counter_fallback = 0;
899 		err = mlx5_devx_cmd_query_hca_attr(sh->ctx, &config.hca_attr);
900 		if (err) {
901 			err = -err;
902 			goto error;
903 		}
904 		if (!config.hca_attr.flow_counters_dump)
905 			priv->counter_fallback = 1;
906 #ifndef HAVE_IBV_DEVX_ASYNC
907 		priv->counter_fallback = 1;
908 #endif
909 		if (priv->counter_fallback)
910 			DRV_LOG(INFO, "Use fall-back DV counter management");
911 		/* Check for LRO support. */
912 		if (config.dest_tir && config.hca_attr.lro_cap &&
913 		    config.dv_flow_en) {
914 			/* TBD check tunnel lro caps. */
915 			config.lro.supported = config.hca_attr.lro_cap;
916 			DRV_LOG(DEBUG, "Device supports LRO");
917 			/*
918 			 * If LRO timeout is not configured by application,
919 			 * use the minimal supported value.
920 			 */
921 			if (!config.lro.timeout)
922 				config.lro.timeout =
923 				config.hca_attr.lro_timer_supported_periods[0];
924 			DRV_LOG(DEBUG, "LRO session timeout set to %d usec",
925 				config.lro.timeout);
926 		}
927 #if defined(HAVE_MLX5DV_DR) && defined(HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER)
928 		if (config.hca_attr.qos.sup && config.hca_attr.qos.srtcm_sup &&
929 		    config.dv_flow_en) {
930 			uint8_t reg_c_mask =
931 				config.hca_attr.qos.flow_meter_reg_c_ids;
932 			/*
933 			 * Meter needs two REG_C's for color match and pre-sfx
934 			 * flow match. Here get the REG_C for color match.
935 			 * REG_C_0 and REG_C_1 is reserved for metadata feature.
936 			 */
937 			reg_c_mask &= 0xfc;
938 			if (__builtin_popcount(reg_c_mask) < 1) {
939 				priv->mtr_en = 0;
940 				DRV_LOG(WARNING, "No available register for"
941 					" meter.");
942 			} else {
943 				priv->mtr_color_reg = ffs(reg_c_mask) - 1 +
944 						      REG_C_0;
945 				priv->mtr_en = 1;
946 				priv->mtr_reg_share =
947 				      config.hca_attr.qos.flow_meter_reg_share;
948 				DRV_LOG(DEBUG, "The REG_C meter uses is %d",
949 					priv->mtr_color_reg);
950 			}
951 		}
952 #endif
953 	}
954 	if (config.mprq.enabled && mprq) {
955 		if (config.mprq.stride_num_n &&
956 		    (config.mprq.stride_num_n > mprq_max_stride_num_n ||
957 		     config.mprq.stride_num_n < mprq_min_stride_num_n)) {
958 			config.mprq.stride_num_n =
959 				RTE_MIN(RTE_MAX(MLX5_MPRQ_STRIDE_NUM_N,
960 						mprq_min_stride_num_n),
961 					mprq_max_stride_num_n);
962 			DRV_LOG(WARNING,
963 				"the number of strides"
964 				" for Multi-Packet RQ is out of range,"
965 				" setting default value (%u)",
966 				1 << config.mprq.stride_num_n);
967 		}
968 		if (config.mprq.stride_size_n &&
969 		    (config.mprq.stride_size_n > mprq_max_stride_size_n ||
970 		     config.mprq.stride_size_n < mprq_min_stride_size_n)) {
971 			config.mprq.stride_size_n =
972 				RTE_MIN(RTE_MAX(MLX5_MPRQ_STRIDE_SIZE_N,
973 						mprq_min_stride_size_n),
974 					mprq_max_stride_size_n);
975 			DRV_LOG(WARNING,
976 				"the size of a stride"
977 				" for Multi-Packet RQ is out of range,"
978 				" setting default value (%u)",
979 				1 << config.mprq.stride_size_n);
980 		}
981 		config.mprq.min_stride_size_n = mprq_min_stride_size_n;
982 		config.mprq.max_stride_size_n = mprq_max_stride_size_n;
983 	} else if (config.mprq.enabled && !mprq) {
984 		DRV_LOG(WARNING, "Multi-Packet RQ isn't supported");
985 		config.mprq.enabled = 0;
986 	}
987 	if (config.max_dump_files_num == 0)
988 		config.max_dump_files_num = 128;
989 	eth_dev = rte_eth_dev_allocate(name);
990 	if (eth_dev == NULL) {
991 		DRV_LOG(ERR, "can not allocate rte ethdev");
992 		err = ENOMEM;
993 		goto error;
994 	}
995 	/* Flag to call rte_eth_dev_release_port() in rte_eth_dev_close(). */
996 	eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
997 	if (priv->representor) {
998 		eth_dev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR;
999 		eth_dev->data->representor_id = priv->representor_id;
1000 	}
1001 	/*
1002 	 * Store associated network device interface index. This index
1003 	 * is permanent throughout the lifetime of device. So, we may store
1004 	 * the ifindex here and use the cached value further.
1005 	 */
1006 	MLX5_ASSERT(spawn->ifindex);
1007 	priv->if_index = spawn->ifindex;
1008 	eth_dev->data->dev_private = priv;
1009 	priv->dev_data = eth_dev->data;
1010 	eth_dev->data->mac_addrs = priv->mac;
1011 	eth_dev->device = dpdk_dev;
1012 	/* Configure the first MAC address by default. */
1013 	if (mlx5_get_mac(eth_dev, &mac.addr_bytes)) {
1014 		DRV_LOG(ERR,
1015 			"port %u cannot get MAC address, is mlx5_en"
1016 			" loaded? (errno: %s)",
1017 			eth_dev->data->port_id, strerror(rte_errno));
1018 		err = ENODEV;
1019 		goto error;
1020 	}
1021 	DRV_LOG(INFO,
1022 		"port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
1023 		eth_dev->data->port_id,
1024 		mac.addr_bytes[0], mac.addr_bytes[1],
1025 		mac.addr_bytes[2], mac.addr_bytes[3],
1026 		mac.addr_bytes[4], mac.addr_bytes[5]);
1027 #ifdef RTE_LIBRTE_MLX5_DEBUG
1028 	{
1029 		char ifname[IF_NAMESIZE];
1030 
1031 		if (mlx5_get_ifname(eth_dev, &ifname) == 0)
1032 			DRV_LOG(DEBUG, "port %u ifname is \"%s\"",
1033 				eth_dev->data->port_id, ifname);
1034 		else
1035 			DRV_LOG(DEBUG, "port %u ifname is unknown",
1036 				eth_dev->data->port_id);
1037 	}
1038 #endif
1039 	/* Get actual MTU if possible. */
1040 	err = mlx5_get_mtu(eth_dev, &priv->mtu);
1041 	if (err) {
1042 		err = rte_errno;
1043 		goto error;
1044 	}
1045 	DRV_LOG(DEBUG, "port %u MTU is %u", eth_dev->data->port_id,
1046 		priv->mtu);
1047 	/* Initialize burst functions to prevent crashes before link-up. */
1048 	eth_dev->rx_pkt_burst = removed_rx_burst;
1049 	eth_dev->tx_pkt_burst = removed_tx_burst;
1050 	eth_dev->dev_ops = &mlx5_os_dev_ops;
1051 	/* Register MAC address. */
1052 	claim_zero(mlx5_mac_addr_add(eth_dev, &mac, 0, 0));
1053 	if (config.vf && config.vf_nl_en)
1054 		mlx5_nl_mac_addr_sync(priv->nl_socket_route,
1055 				      mlx5_ifindex(eth_dev),
1056 				      eth_dev->data->mac_addrs,
1057 				      MLX5_MAX_MAC_ADDRESSES);
1058 	priv->flows = 0;
1059 	priv->ctrl_flows = 0;
1060 	TAILQ_INIT(&priv->flow_meters);
1061 	TAILQ_INIT(&priv->flow_meter_profiles);
1062 	/* Hint libmlx5 to use PMD allocator for data plane resources */
1063 	struct mlx5dv_ctx_allocators alctr = {
1064 		.alloc = &mlx5_alloc_verbs_buf,
1065 		.free = &mlx5_free_verbs_buf,
1066 		.data = priv,
1067 	};
1068 	mlx5_glue->dv_set_context_attr(sh->ctx,
1069 				       MLX5DV_CTX_ATTR_BUF_ALLOCATORS,
1070 				       (void *)((uintptr_t)&alctr));
1071 	/* Bring Ethernet device up. */
1072 	DRV_LOG(DEBUG, "port %u forcing Ethernet interface up",
1073 		eth_dev->data->port_id);
1074 	mlx5_set_link_up(eth_dev);
1075 	/*
1076 	 * Even though the interrupt handler is not installed yet,
1077 	 * interrupts will still trigger on the async_fd from
1078 	 * Verbs context returned by ibv_open_device().
1079 	 */
1080 	mlx5_link_update(eth_dev, 0);
1081 #ifdef HAVE_MLX5DV_DR_ESWITCH
1082 	if (!(config.hca_attr.eswitch_manager && config.dv_flow_en &&
1083 	      (switch_info->representor || switch_info->master)))
1084 		config.dv_esw_en = 0;
1085 #else
1086 	config.dv_esw_en = 0;
1087 #endif
1088 	/* Detect minimal data bytes to inline. */
1089 	mlx5_set_min_inline(spawn, &config);
1090 	/* Store device configuration on private structure. */
1091 	priv->config = config;
1092 	/* Create context for virtual machine VLAN workaround. */
1093 	priv->vmwa_context = mlx5_vlan_vmwa_init(eth_dev, spawn->ifindex);
1094 	if (config.dv_flow_en) {
1095 		err = mlx5_alloc_shared_dr(priv);
1096 		if (err)
1097 			goto error;
1098 		/*
1099 		 * RSS id is shared with meter flow id. Meter flow id can only
1100 		 * use the 24 MSB of the register.
1101 		 */
1102 		priv->qrss_id_pool = mlx5_flow_id_pool_alloc(UINT32_MAX >>
1103 				     MLX5_MTR_COLOR_BITS);
1104 		if (!priv->qrss_id_pool) {
1105 			DRV_LOG(ERR, "can't create flow id pool");
1106 			err = ENOMEM;
1107 			goto error;
1108 		}
1109 	}
1110 	/* Supported Verbs flow priority number detection. */
1111 	err = mlx5_flow_discover_priorities(eth_dev);
1112 	if (err < 0) {
1113 		err = -err;
1114 		goto error;
1115 	}
1116 	priv->config.flow_prio = err;
1117 	if (!priv->config.dv_esw_en &&
1118 	    priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
1119 		DRV_LOG(WARNING, "metadata mode %u is not supported "
1120 				 "(no E-Switch)", priv->config.dv_xmeta_en);
1121 		priv->config.dv_xmeta_en = MLX5_XMETA_MODE_LEGACY;
1122 	}
1123 	mlx5_set_metadata_mask(eth_dev);
1124 	if (priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
1125 	    !priv->sh->dv_regc0_mask) {
1126 		DRV_LOG(ERR, "metadata mode %u is not supported "
1127 			     "(no metadata reg_c[0] is available)",
1128 			     priv->config.dv_xmeta_en);
1129 			err = ENOTSUP;
1130 			goto error;
1131 	}
1132 	/*
1133 	 * Allocate the buffer for flow creating, just once.
1134 	 * The allocation must be done before any flow creating.
1135 	 */
1136 	mlx5_flow_alloc_intermediate(eth_dev);
1137 	/* Query availability of metadata reg_c's. */
1138 	err = mlx5_flow_discover_mreg_c(eth_dev);
1139 	if (err < 0) {
1140 		err = -err;
1141 		goto error;
1142 	}
1143 	if (!mlx5_flow_ext_mreg_supported(eth_dev)) {
1144 		DRV_LOG(DEBUG,
1145 			"port %u extensive metadata register is not supported",
1146 			eth_dev->data->port_id);
1147 		if (priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
1148 			DRV_LOG(ERR, "metadata mode %u is not supported "
1149 				     "(no metadata registers available)",
1150 				     priv->config.dv_xmeta_en);
1151 			err = ENOTSUP;
1152 			goto error;
1153 		}
1154 	}
1155 	if (priv->config.dv_flow_en &&
1156 	    priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
1157 	    mlx5_flow_ext_mreg_supported(eth_dev) &&
1158 	    priv->sh->dv_regc0_mask) {
1159 		priv->mreg_cp_tbl = mlx5_hlist_create(MLX5_FLOW_MREG_HNAME,
1160 						      MLX5_FLOW_MREG_HTABLE_SZ);
1161 		if (!priv->mreg_cp_tbl) {
1162 			err = ENOMEM;
1163 			goto error;
1164 		}
1165 	}
1166 	return eth_dev;
1167 error:
1168 	if (priv) {
1169 		if (priv->mreg_cp_tbl)
1170 			mlx5_hlist_destroy(priv->mreg_cp_tbl, NULL, NULL);
1171 		if (priv->sh)
1172 			mlx5_os_free_shared_dr(priv);
1173 		if (priv->nl_socket_route >= 0)
1174 			close(priv->nl_socket_route);
1175 		if (priv->nl_socket_rdma >= 0)
1176 			close(priv->nl_socket_rdma);
1177 		if (priv->vmwa_context)
1178 			mlx5_vlan_vmwa_exit(priv->vmwa_context);
1179 		if (priv->qrss_id_pool)
1180 			mlx5_flow_id_pool_release(priv->qrss_id_pool);
1181 		if (own_domain_id)
1182 			claim_zero(rte_eth_switch_domain_free(priv->domain_id));
1183 		rte_free(priv);
1184 		if (eth_dev != NULL)
1185 			eth_dev->data->dev_private = NULL;
1186 	}
1187 	if (eth_dev != NULL) {
1188 		/* mac_addrs must not be freed alone because part of
1189 		 * dev_private
1190 		 **/
1191 		eth_dev->data->mac_addrs = NULL;
1192 		rte_eth_dev_release_port(eth_dev);
1193 	}
1194 	if (sh)
1195 		mlx5_free_shared_dev_ctx(sh);
1196 	MLX5_ASSERT(err > 0);
1197 	rte_errno = err;
1198 	return NULL;
1199 }
1200 
1201 /**
1202  * Comparison callback to sort device data.
1203  *
1204  * This is meant to be used with qsort().
1205  *
1206  * @param a[in]
1207  *   Pointer to pointer to first data object.
1208  * @param b[in]
1209  *   Pointer to pointer to second data object.
1210  *
1211  * @return
1212  *   0 if both objects are equal, less than 0 if the first argument is less
1213  *   than the second, greater than 0 otherwise.
1214  */
1215 static int
1216 mlx5_dev_spawn_data_cmp(const void *a, const void *b)
1217 {
1218 	const struct mlx5_switch_info *si_a =
1219 		&((const struct mlx5_dev_spawn_data *)a)->info;
1220 	const struct mlx5_switch_info *si_b =
1221 		&((const struct mlx5_dev_spawn_data *)b)->info;
1222 	int ret;
1223 
1224 	/* Master device first. */
1225 	ret = si_b->master - si_a->master;
1226 	if (ret)
1227 		return ret;
1228 	/* Then representor devices. */
1229 	ret = si_b->representor - si_a->representor;
1230 	if (ret)
1231 		return ret;
1232 	/* Unidentified devices come last in no specific order. */
1233 	if (!si_a->representor)
1234 		return 0;
1235 	/* Order representors by name. */
1236 	return si_a->port_name - si_b->port_name;
1237 }
1238 
1239 /**
1240  * Match PCI information for possible slaves of bonding device.
1241  *
1242  * @param[in] ibv_dev
1243  *   Pointer to Infiniband device structure.
1244  * @param[in] pci_dev
1245  *   Pointer to PCI device structure to match PCI address.
1246  * @param[in] nl_rdma
1247  *   Netlink RDMA group socket handle.
1248  *
1249  * @return
1250  *   negative value if no bonding device found, otherwise
1251  *   positive index of slave PF in bonding.
1252  */
1253 static int
1254 mlx5_device_bond_pci_match(const struct ibv_device *ibv_dev,
1255 			   const struct rte_pci_device *pci_dev,
1256 			   int nl_rdma)
1257 {
1258 	char ifname[IF_NAMESIZE + 1];
1259 	unsigned int ifindex;
1260 	unsigned int np, i;
1261 	FILE *file = NULL;
1262 	int pf = -1;
1263 
1264 	/*
1265 	 * Try to get master device name. If something goes
1266 	 * wrong suppose the lack of kernel support and no
1267 	 * bonding devices.
1268 	 */
1269 	if (nl_rdma < 0)
1270 		return -1;
1271 	if (!strstr(ibv_dev->name, "bond"))
1272 		return -1;
1273 	np = mlx5_nl_portnum(nl_rdma, ibv_dev->name);
1274 	if (!np)
1275 		return -1;
1276 	/*
1277 	 * The Master device might not be on the predefined
1278 	 * port (not on port index 1, it is not garanted),
1279 	 * we have to scan all Infiniband device port and
1280 	 * find master.
1281 	 */
1282 	for (i = 1; i <= np; ++i) {
1283 		/* Check whether Infiniband port is populated. */
1284 		ifindex = mlx5_nl_ifindex(nl_rdma, ibv_dev->name, i);
1285 		if (!ifindex)
1286 			continue;
1287 		if (!if_indextoname(ifindex, ifname))
1288 			continue;
1289 		/* Try to read bonding slave names from sysfs. */
1290 		MKSTR(slaves,
1291 		      "/sys/class/net/%s/master/bonding/slaves", ifname);
1292 		file = fopen(slaves, "r");
1293 		if (file)
1294 			break;
1295 	}
1296 	if (!file)
1297 		return -1;
1298 	/* Use safe format to check maximal buffer length. */
1299 	MLX5_ASSERT(atol(RTE_STR(IF_NAMESIZE)) == IF_NAMESIZE);
1300 	while (fscanf(file, "%" RTE_STR(IF_NAMESIZE) "s", ifname) == 1) {
1301 		char tmp_str[IF_NAMESIZE + 32];
1302 		struct rte_pci_addr pci_addr;
1303 		struct mlx5_switch_info	info;
1304 
1305 		/* Process slave interface names in the loop. */
1306 		snprintf(tmp_str, sizeof(tmp_str),
1307 			 "/sys/class/net/%s", ifname);
1308 		if (mlx5_dev_to_pci_addr(tmp_str, &pci_addr)) {
1309 			DRV_LOG(WARNING, "can not get PCI address"
1310 					 " for netdev \"%s\"", ifname);
1311 			continue;
1312 		}
1313 		if (pci_dev->addr.domain != pci_addr.domain ||
1314 		    pci_dev->addr.bus != pci_addr.bus ||
1315 		    pci_dev->addr.devid != pci_addr.devid ||
1316 		    pci_dev->addr.function != pci_addr.function)
1317 			continue;
1318 		/* Slave interface PCI address match found. */
1319 		fclose(file);
1320 		snprintf(tmp_str, sizeof(tmp_str),
1321 			 "/sys/class/net/%s/phys_port_name", ifname);
1322 		file = fopen(tmp_str, "rb");
1323 		if (!file)
1324 			break;
1325 		info.name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET;
1326 		if (fscanf(file, "%32s", tmp_str) == 1)
1327 			mlx5_translate_port_name(tmp_str, &info);
1328 		if (info.name_type == MLX5_PHYS_PORT_NAME_TYPE_LEGACY ||
1329 		    info.name_type == MLX5_PHYS_PORT_NAME_TYPE_UPLINK)
1330 			pf = info.port_name;
1331 		break;
1332 	}
1333 	if (file)
1334 		fclose(file);
1335 	return pf;
1336 }
1337 
1338 /**
1339  * DPDK callback to register a PCI device.
1340  *
1341  * This function spawns Ethernet devices out of a given PCI device.
1342  *
1343  * @param[in] pci_drv
1344  *   PCI driver structure (mlx5_driver).
1345  * @param[in] pci_dev
1346  *   PCI device information.
1347  *
1348  * @return
1349  *   0 on success, a negative errno value otherwise and rte_errno is set.
1350  */
1351 int
1352 mlx5_os_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1353 		  struct rte_pci_device *pci_dev)
1354 {
1355 	struct ibv_device **ibv_list;
1356 	/*
1357 	 * Number of found IB Devices matching with requested PCI BDF.
1358 	 * nd != 1 means there are multiple IB devices over the same
1359 	 * PCI device and we have representors and master.
1360 	 */
1361 	unsigned int nd = 0;
1362 	/*
1363 	 * Number of found IB device Ports. nd = 1 and np = 1..n means
1364 	 * we have the single multiport IB device, and there may be
1365 	 * representors attached to some of found ports.
1366 	 */
1367 	unsigned int np = 0;
1368 	/*
1369 	 * Number of DPDK ethernet devices to Spawn - either over
1370 	 * multiple IB devices or multiple ports of single IB device.
1371 	 * Actually this is the number of iterations to spawn.
1372 	 */
1373 	unsigned int ns = 0;
1374 	/*
1375 	 * Bonding device
1376 	 *   < 0 - no bonding device (single one)
1377 	 *  >= 0 - bonding device (value is slave PF index)
1378 	 */
1379 	int bd = -1;
1380 	struct mlx5_dev_spawn_data *list = NULL;
1381 	struct mlx5_dev_config dev_config;
1382 	int ret;
1383 
1384 	if (mlx5_class_get(pci_dev->device.devargs) != MLX5_CLASS_NET) {
1385 		DRV_LOG(DEBUG, "Skip probing - should be probed by other mlx5"
1386 			" driver.");
1387 		return 1;
1388 	}
1389 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1390 		mlx5_pmd_socket_init();
1391 	ret = mlx5_init_once();
1392 	if (ret) {
1393 		DRV_LOG(ERR, "unable to init PMD global data: %s",
1394 			strerror(rte_errno));
1395 		return -rte_errno;
1396 	}
1397 	MLX5_ASSERT(pci_drv == &mlx5_driver);
1398 	errno = 0;
1399 	ibv_list = mlx5_glue->get_device_list(&ret);
1400 	if (!ibv_list) {
1401 		rte_errno = errno ? errno : ENOSYS;
1402 		DRV_LOG(ERR, "cannot list devices, is ib_uverbs loaded?");
1403 		return -rte_errno;
1404 	}
1405 	/*
1406 	 * First scan the list of all Infiniband devices to find
1407 	 * matching ones, gathering into the list.
1408 	 */
1409 	struct ibv_device *ibv_match[ret + 1];
1410 	int nl_route = mlx5_nl_init(NETLINK_ROUTE);
1411 	int nl_rdma = mlx5_nl_init(NETLINK_RDMA);
1412 	unsigned int i;
1413 
1414 	while (ret-- > 0) {
1415 		struct rte_pci_addr pci_addr;
1416 
1417 		DRV_LOG(DEBUG, "checking device \"%s\"", ibv_list[ret]->name);
1418 		bd = mlx5_device_bond_pci_match
1419 				(ibv_list[ret], pci_dev, nl_rdma);
1420 		if (bd >= 0) {
1421 			/*
1422 			 * Bonding device detected. Only one match is allowed,
1423 			 * the bonding is supported over multi-port IB device,
1424 			 * there should be no matches on representor PCI
1425 			 * functions or non VF LAG bonding devices with
1426 			 * specified address.
1427 			 */
1428 			if (nd) {
1429 				DRV_LOG(ERR,
1430 					"multiple PCI match on bonding device"
1431 					"\"%s\" found", ibv_list[ret]->name);
1432 				rte_errno = ENOENT;
1433 				ret = -rte_errno;
1434 				goto exit;
1435 			}
1436 			DRV_LOG(INFO, "PCI information matches for"
1437 				      " slave %d bonding device \"%s\"",
1438 				      bd, ibv_list[ret]->name);
1439 			ibv_match[nd++] = ibv_list[ret];
1440 			break;
1441 		}
1442 		if (mlx5_dev_to_pci_addr
1443 			(ibv_list[ret]->ibdev_path, &pci_addr))
1444 			continue;
1445 		if (pci_dev->addr.domain != pci_addr.domain ||
1446 		    pci_dev->addr.bus != pci_addr.bus ||
1447 		    pci_dev->addr.devid != pci_addr.devid ||
1448 		    pci_dev->addr.function != pci_addr.function)
1449 			continue;
1450 		DRV_LOG(INFO, "PCI information matches for device \"%s\"",
1451 			ibv_list[ret]->name);
1452 		ibv_match[nd++] = ibv_list[ret];
1453 	}
1454 	ibv_match[nd] = NULL;
1455 	if (!nd) {
1456 		/* No device matches, just complain and bail out. */
1457 		DRV_LOG(WARNING,
1458 			"no Verbs device matches PCI device " PCI_PRI_FMT ","
1459 			" are kernel drivers loaded?",
1460 			pci_dev->addr.domain, pci_dev->addr.bus,
1461 			pci_dev->addr.devid, pci_dev->addr.function);
1462 		rte_errno = ENOENT;
1463 		ret = -rte_errno;
1464 		goto exit;
1465 	}
1466 	if (nd == 1) {
1467 		/*
1468 		 * Found single matching device may have multiple ports.
1469 		 * Each port may be representor, we have to check the port
1470 		 * number and check the representors existence.
1471 		 */
1472 		if (nl_rdma >= 0)
1473 			np = mlx5_nl_portnum(nl_rdma, ibv_match[0]->name);
1474 		if (!np)
1475 			DRV_LOG(WARNING, "can not get IB device \"%s\""
1476 					 " ports number", ibv_match[0]->name);
1477 		if (bd >= 0 && !np) {
1478 			DRV_LOG(ERR, "can not get ports"
1479 				     " for bonding device");
1480 			rte_errno = ENOENT;
1481 			ret = -rte_errno;
1482 			goto exit;
1483 		}
1484 	}
1485 #ifndef HAVE_MLX5DV_DR_DEVX_PORT
1486 	if (bd >= 0) {
1487 		/*
1488 		 * This may happen if there is VF LAG kernel support and
1489 		 * application is compiled with older rdma_core library.
1490 		 */
1491 		DRV_LOG(ERR,
1492 			"No kernel/verbs support for VF LAG bonding found.");
1493 		rte_errno = ENOTSUP;
1494 		ret = -rte_errno;
1495 		goto exit;
1496 	}
1497 #endif
1498 	/*
1499 	 * Now we can determine the maximal
1500 	 * amount of devices to be spawned.
1501 	 */
1502 	list = rte_zmalloc("device spawn data",
1503 			 sizeof(struct mlx5_dev_spawn_data) *
1504 			 (np ? np : nd),
1505 			 RTE_CACHE_LINE_SIZE);
1506 	if (!list) {
1507 		DRV_LOG(ERR, "spawn data array allocation failure");
1508 		rte_errno = ENOMEM;
1509 		ret = -rte_errno;
1510 		goto exit;
1511 	}
1512 	if (bd >= 0 || np > 1) {
1513 		/*
1514 		 * Single IB device with multiple ports found,
1515 		 * it may be E-Switch master device and representors.
1516 		 * We have to perform identification through the ports.
1517 		 */
1518 		MLX5_ASSERT(nl_rdma >= 0);
1519 		MLX5_ASSERT(ns == 0);
1520 		MLX5_ASSERT(nd == 1);
1521 		MLX5_ASSERT(np);
1522 		for (i = 1; i <= np; ++i) {
1523 			list[ns].max_port = np;
1524 			list[ns].phys_port = i;
1525 			list[ns].phys_dev = ibv_match[0];
1526 			list[ns].eth_dev = NULL;
1527 			list[ns].pci_dev = pci_dev;
1528 			list[ns].pf_bond = bd;
1529 			list[ns].ifindex = mlx5_nl_ifindex
1530 				(nl_rdma,
1531 				mlx5_os_get_dev_device_name
1532 						(list[ns].phys_dev), i);
1533 			if (!list[ns].ifindex) {
1534 				/*
1535 				 * No network interface index found for the
1536 				 * specified port, it means there is no
1537 				 * representor on this port. It's OK,
1538 				 * there can be disabled ports, for example
1539 				 * if sriov_numvfs < sriov_totalvfs.
1540 				 */
1541 				continue;
1542 			}
1543 			ret = -1;
1544 			if (nl_route >= 0)
1545 				ret = mlx5_nl_switch_info
1546 					       (nl_route,
1547 						list[ns].ifindex,
1548 						&list[ns].info);
1549 			if (ret || (!list[ns].info.representor &&
1550 				    !list[ns].info.master)) {
1551 				/*
1552 				 * We failed to recognize representors with
1553 				 * Netlink, let's try to perform the task
1554 				 * with sysfs.
1555 				 */
1556 				ret =  mlx5_sysfs_switch_info
1557 						(list[ns].ifindex,
1558 						 &list[ns].info);
1559 			}
1560 			if (!ret && bd >= 0) {
1561 				switch (list[ns].info.name_type) {
1562 				case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1563 					if (list[ns].info.port_name == bd)
1564 						ns++;
1565 					break;
1566 				case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1567 					if (list[ns].info.pf_num == bd)
1568 						ns++;
1569 					break;
1570 				default:
1571 					break;
1572 				}
1573 				continue;
1574 			}
1575 			if (!ret && (list[ns].info.representor ^
1576 				     list[ns].info.master))
1577 				ns++;
1578 		}
1579 		if (!ns) {
1580 			DRV_LOG(ERR,
1581 				"unable to recognize master/representors"
1582 				" on the IB device with multiple ports");
1583 			rte_errno = ENOENT;
1584 			ret = -rte_errno;
1585 			goto exit;
1586 		}
1587 	} else {
1588 		/*
1589 		 * The existence of several matching entries (nd > 1) means
1590 		 * port representors have been instantiated. No existing Verbs
1591 		 * call nor sysfs entries can tell them apart, this can only
1592 		 * be done through Netlink calls assuming kernel drivers are
1593 		 * recent enough to support them.
1594 		 *
1595 		 * In the event of identification failure through Netlink,
1596 		 * try again through sysfs, then:
1597 		 *
1598 		 * 1. A single IB device matches (nd == 1) with single
1599 		 *    port (np=0/1) and is not a representor, assume
1600 		 *    no switch support.
1601 		 *
1602 		 * 2. Otherwise no safe assumptions can be made;
1603 		 *    complain louder and bail out.
1604 		 */
1605 		for (i = 0; i != nd; ++i) {
1606 			memset(&list[ns].info, 0, sizeof(list[ns].info));
1607 			list[ns].max_port = 1;
1608 			list[ns].phys_port = 1;
1609 			list[ns].phys_dev = ibv_match[i];
1610 			list[ns].eth_dev = NULL;
1611 			list[ns].pci_dev = pci_dev;
1612 			list[ns].pf_bond = -1;
1613 			list[ns].ifindex = 0;
1614 			if (nl_rdma >= 0)
1615 				list[ns].ifindex = mlx5_nl_ifindex
1616 				(nl_rdma,
1617 				mlx5_os_get_dev_device_name
1618 						(list[ns].phys_dev), 1);
1619 			if (!list[ns].ifindex) {
1620 				char ifname[IF_NAMESIZE];
1621 
1622 				/*
1623 				 * Netlink failed, it may happen with old
1624 				 * ib_core kernel driver (before 4.16).
1625 				 * We can assume there is old driver because
1626 				 * here we are processing single ports IB
1627 				 * devices. Let's try sysfs to retrieve
1628 				 * the ifindex. The method works for
1629 				 * master device only.
1630 				 */
1631 				if (nd > 1) {
1632 					/*
1633 					 * Multiple devices found, assume
1634 					 * representors, can not distinguish
1635 					 * master/representor and retrieve
1636 					 * ifindex via sysfs.
1637 					 */
1638 					continue;
1639 				}
1640 				ret = mlx5_get_ifname_sysfs
1641 					(ibv_match[i]->ibdev_path, ifname);
1642 				if (!ret)
1643 					list[ns].ifindex =
1644 						if_nametoindex(ifname);
1645 				if (!list[ns].ifindex) {
1646 					/*
1647 					 * No network interface index found
1648 					 * for the specified device, it means
1649 					 * there it is neither representor
1650 					 * nor master.
1651 					 */
1652 					continue;
1653 				}
1654 			}
1655 			ret = -1;
1656 			if (nl_route >= 0)
1657 				ret = mlx5_nl_switch_info
1658 					       (nl_route,
1659 						list[ns].ifindex,
1660 						&list[ns].info);
1661 			if (ret || (!list[ns].info.representor &&
1662 				    !list[ns].info.master)) {
1663 				/*
1664 				 * We failed to recognize representors with
1665 				 * Netlink, let's try to perform the task
1666 				 * with sysfs.
1667 				 */
1668 				ret =  mlx5_sysfs_switch_info
1669 						(list[ns].ifindex,
1670 						 &list[ns].info);
1671 			}
1672 			if (!ret && (list[ns].info.representor ^
1673 				     list[ns].info.master)) {
1674 				ns++;
1675 			} else if ((nd == 1) &&
1676 				   !list[ns].info.representor &&
1677 				   !list[ns].info.master) {
1678 				/*
1679 				 * Single IB device with
1680 				 * one physical port and
1681 				 * attached network device.
1682 				 * May be SRIOV is not enabled
1683 				 * or there is no representors.
1684 				 */
1685 				DRV_LOG(INFO, "no E-Switch support detected");
1686 				ns++;
1687 				break;
1688 			}
1689 		}
1690 		if (!ns) {
1691 			DRV_LOG(ERR,
1692 				"unable to recognize master/representors"
1693 				" on the multiple IB devices");
1694 			rte_errno = ENOENT;
1695 			ret = -rte_errno;
1696 			goto exit;
1697 		}
1698 	}
1699 	MLX5_ASSERT(ns);
1700 	/*
1701 	 * Sort list to probe devices in natural order for users convenience
1702 	 * (i.e. master first, then representors from lowest to highest ID).
1703 	 */
1704 	qsort(list, ns, sizeof(*list), mlx5_dev_spawn_data_cmp);
1705 	/* Default configuration. */
1706 	dev_config = (struct mlx5_dev_config){
1707 		.hw_padding = 0,
1708 		.mps = MLX5_ARG_UNSET,
1709 		.dbnc = MLX5_ARG_UNSET,
1710 		.rx_vec_en = 1,
1711 		.txq_inline_max = MLX5_ARG_UNSET,
1712 		.txq_inline_min = MLX5_ARG_UNSET,
1713 		.txq_inline_mpw = MLX5_ARG_UNSET,
1714 		.txqs_inline = MLX5_ARG_UNSET,
1715 		.vf_nl_en = 1,
1716 		.mr_ext_memseg_en = 1,
1717 		.mprq = {
1718 			.enabled = 0, /* Disabled by default. */
1719 			.stride_num_n = 0,
1720 			.stride_size_n = 0,
1721 			.max_memcpy_len = MLX5_MPRQ_MEMCPY_DEFAULT_LEN,
1722 			.min_rxqs_num = MLX5_MPRQ_MIN_RXQS,
1723 		},
1724 		.dv_esw_en = 1,
1725 		.dv_flow_en = 1,
1726 		.log_hp_size = MLX5_ARG_UNSET,
1727 	};
1728 	/* Device specific configuration. */
1729 	switch (pci_dev->id.device_id) {
1730 	case PCI_DEVICE_ID_MELLANOX_CONNECTX4VF:
1731 	case PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF:
1732 	case PCI_DEVICE_ID_MELLANOX_CONNECTX5VF:
1733 	case PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF:
1734 	case PCI_DEVICE_ID_MELLANOX_CONNECTX5BFVF:
1735 	case PCI_DEVICE_ID_MELLANOX_CONNECTX6VF:
1736 	case PCI_DEVICE_ID_MELLANOX_CONNECTX6DXVF:
1737 		dev_config.vf = 1;
1738 		break;
1739 	default:
1740 		break;
1741 	}
1742 	for (i = 0; i != ns; ++i) {
1743 		uint32_t restore;
1744 
1745 		list[i].eth_dev = mlx5_dev_spawn(&pci_dev->device,
1746 						 &list[i],
1747 						 dev_config);
1748 		if (!list[i].eth_dev) {
1749 			if (rte_errno != EBUSY && rte_errno != EEXIST)
1750 				break;
1751 			/* Device is disabled or already spawned. Ignore it. */
1752 			continue;
1753 		}
1754 		restore = list[i].eth_dev->data->dev_flags;
1755 		rte_eth_copy_pci_info(list[i].eth_dev, pci_dev);
1756 		/* Restore non-PCI flags cleared by the above call. */
1757 		list[i].eth_dev->data->dev_flags |= restore;
1758 		rte_eth_dev_probing_finish(list[i].eth_dev);
1759 	}
1760 	if (i != ns) {
1761 		DRV_LOG(ERR,
1762 			"probe of PCI device " PCI_PRI_FMT " aborted after"
1763 			" encountering an error: %s",
1764 			pci_dev->addr.domain, pci_dev->addr.bus,
1765 			pci_dev->addr.devid, pci_dev->addr.function,
1766 			strerror(rte_errno));
1767 		ret = -rte_errno;
1768 		/* Roll back. */
1769 		while (i--) {
1770 			if (!list[i].eth_dev)
1771 				continue;
1772 			mlx5_dev_close(list[i].eth_dev);
1773 			/* mac_addrs must not be freed because in dev_private */
1774 			list[i].eth_dev->data->mac_addrs = NULL;
1775 			claim_zero(rte_eth_dev_release_port(list[i].eth_dev));
1776 		}
1777 		/* Restore original error. */
1778 		rte_errno = -ret;
1779 	} else {
1780 		ret = 0;
1781 	}
1782 exit:
1783 	/*
1784 	 * Do the routine cleanup:
1785 	 * - close opened Netlink sockets
1786 	 * - free allocated spawn data array
1787 	 * - free the Infiniband device list
1788 	 */
1789 	if (nl_rdma >= 0)
1790 		close(nl_rdma);
1791 	if (nl_route >= 0)
1792 		close(nl_route);
1793 	if (list)
1794 		rte_free(list);
1795 	MLX5_ASSERT(ibv_list);
1796 	mlx5_glue->free_device_list(ibv_list);
1797 	return ret;
1798 }
1799 
1800 static int
1801 mlx5_config_doorbell_mapping_env(const struct mlx5_dev_config *config)
1802 {
1803 	char *env;
1804 	int value;
1805 
1806 	MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
1807 	/* Get environment variable to store. */
1808 	env = getenv(MLX5_SHUT_UP_BF);
1809 	value = env ? !!strcmp(env, "0") : MLX5_ARG_UNSET;
1810 	if (config->dbnc == MLX5_ARG_UNSET)
1811 		setenv(MLX5_SHUT_UP_BF, MLX5_SHUT_UP_BF_DEFAULT, 1);
1812 	else
1813 		setenv(MLX5_SHUT_UP_BF,
1814 		       config->dbnc == MLX5_TXDB_NCACHED ? "1" : "0", 1);
1815 	return value;
1816 }
1817 
1818 static void
1819 mlx5_restore_doorbell_mapping_env(int value)
1820 {
1821 	MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
1822 	/* Restore the original environment variable state. */
1823 	if (value == MLX5_ARG_UNSET)
1824 		unsetenv(MLX5_SHUT_UP_BF);
1825 	else
1826 		setenv(MLX5_SHUT_UP_BF, value ? "1" : "0", 1);
1827 }
1828 
1829 /**
1830  * Extract pdn of PD object using DV API.
1831  *
1832  * @param[in] pd
1833  *   Pointer to the verbs PD object.
1834  * @param[out] pdn
1835  *   Pointer to the PD object number variable.
1836  *
1837  * @return
1838  *   0 on success, error value otherwise.
1839  */
1840 int
1841 mlx5_os_get_pdn(void *pd, uint32_t *pdn)
1842 {
1843 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
1844 	struct mlx5dv_obj obj;
1845 	struct mlx5dv_pd pd_info;
1846 	int ret = 0;
1847 
1848 	obj.pd.in = pd;
1849 	obj.pd.out = &pd_info;
1850 	ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_PD);
1851 	if (ret) {
1852 		DRV_LOG(DEBUG, "Fail to get PD object info");
1853 		return ret;
1854 	}
1855 	*pdn = pd_info.pdn;
1856 	return 0;
1857 #else
1858 	(void)pd;
1859 	(void)pdn;
1860 	return -ENOTSUP;
1861 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */
1862 }
1863 
1864 /**
1865  * Function API to open IB device.
1866  *
1867  * This function calls the Linux glue APIs to open a device.
1868  *
1869  * @param[in] spawn
1870  *   Pointer to the IB device attributes (name, port, etc).
1871  * @param[out] config
1872  *   Pointer to device configuration structure.
1873  * @param[out] sh
1874  *   Pointer to shared context structure.
1875  *
1876  * @return
1877  *   0 on success, a positive error value otherwise.
1878  */
1879 int
1880 mlx5_os_open_device(const struct mlx5_dev_spawn_data *spawn,
1881 		     const struct mlx5_dev_config *config,
1882 		     struct mlx5_dev_ctx_shared *sh)
1883 {
1884 	int dbmap_env;
1885 	int err = 0;
1886 	/*
1887 	 * Configure environment variable "MLX5_BF_SHUT_UP"
1888 	 * before the device creation. The rdma_core library
1889 	 * checks the variable at device creation and
1890 	 * stores the result internally.
1891 	 */
1892 	dbmap_env = mlx5_config_doorbell_mapping_env(config);
1893 	/* Try to open IB device with DV first, then usual Verbs. */
1894 	errno = 0;
1895 	sh->ctx = mlx5_glue->dv_open_device(spawn->phys_dev);
1896 	if (sh->ctx) {
1897 		sh->devx = 1;
1898 		DRV_LOG(DEBUG, "DevX is supported");
1899 		/* The device is created, no need for environment. */
1900 		mlx5_restore_doorbell_mapping_env(dbmap_env);
1901 	} else {
1902 		/* The environment variable is still configured. */
1903 		sh->ctx = mlx5_glue->open_device(spawn->phys_dev);
1904 		err = errno ? errno : ENODEV;
1905 		/*
1906 		 * The environment variable is not needed anymore,
1907 		 * all device creation attempts are completed.
1908 		 */
1909 		mlx5_restore_doorbell_mapping_env(dbmap_env);
1910 		if (!sh->ctx)
1911 			return err;
1912 		DRV_LOG(DEBUG, "DevX is NOT supported");
1913 		err = 0;
1914 	}
1915 	return err;
1916 }
1917 
1918 /**
1919  * Install shared asynchronous device events handler.
1920  * This function is implemented to support event sharing
1921  * between multiple ports of single IB device.
1922  *
1923  * @param sh
1924  *   Pointer to mlx5_dev_ctx_shared object.
1925  */
1926 void
1927 mlx5_os_dev_shared_handler_install(struct mlx5_dev_ctx_shared *sh)
1928 {
1929 	int ret;
1930 	int flags;
1931 
1932 	sh->intr_handle.fd = -1;
1933 	flags = fcntl(((struct ibv_context *)sh->ctx)->async_fd, F_GETFL);
1934 	ret = fcntl(((struct ibv_context *)sh->ctx)->async_fd,
1935 		    F_SETFL, flags | O_NONBLOCK);
1936 	if (ret) {
1937 		DRV_LOG(INFO, "failed to change file descriptor async event"
1938 			" queue");
1939 	} else {
1940 		sh->intr_handle.fd = ((struct ibv_context *)sh->ctx)->async_fd;
1941 		sh->intr_handle.type = RTE_INTR_HANDLE_EXT;
1942 		if (rte_intr_callback_register(&sh->intr_handle,
1943 					mlx5_dev_interrupt_handler, sh)) {
1944 			DRV_LOG(INFO, "Fail to install the shared interrupt.");
1945 			sh->intr_handle.fd = -1;
1946 		}
1947 	}
1948 	if (sh->devx) {
1949 #ifdef HAVE_IBV_DEVX_ASYNC
1950 		sh->intr_handle_devx.fd = -1;
1951 		sh->devx_comp =
1952 			(void *)mlx5_glue->devx_create_cmd_comp(sh->ctx);
1953 		struct mlx5dv_devx_cmd_comp *devx_comp = sh->devx_comp;
1954 		if (!devx_comp) {
1955 			DRV_LOG(INFO, "failed to allocate devx_comp.");
1956 			return;
1957 		}
1958 		flags = fcntl(devx_comp->fd, F_GETFL);
1959 		ret = fcntl(devx_comp->fd, F_SETFL, flags | O_NONBLOCK);
1960 		if (ret) {
1961 			DRV_LOG(INFO, "failed to change file descriptor"
1962 				" devx comp");
1963 			return;
1964 		}
1965 		sh->intr_handle_devx.fd = devx_comp->fd;
1966 		sh->intr_handle_devx.type = RTE_INTR_HANDLE_EXT;
1967 		if (rte_intr_callback_register(&sh->intr_handle_devx,
1968 					mlx5_dev_interrupt_handler_devx, sh)) {
1969 			DRV_LOG(INFO, "Fail to install the devx shared"
1970 				" interrupt.");
1971 			sh->intr_handle_devx.fd = -1;
1972 		}
1973 #endif /* HAVE_IBV_DEVX_ASYNC */
1974 	}
1975 }
1976 
1977 /**
1978  * Uninstall shared asynchronous device events handler.
1979  * This function is implemented to support event sharing
1980  * between multiple ports of single IB device.
1981  *
1982  * @param dev
1983  *   Pointer to mlx5_dev_ctx_shared object.
1984  */
1985 void
1986 mlx5_os_dev_shared_handler_uninstall(struct mlx5_dev_ctx_shared *sh)
1987 {
1988 	if (sh->intr_handle.fd >= 0)
1989 		mlx5_intr_callback_unregister(&sh->intr_handle,
1990 					      mlx5_dev_interrupt_handler, sh);
1991 #ifdef HAVE_IBV_DEVX_ASYNC
1992 	if (sh->intr_handle_devx.fd >= 0)
1993 		rte_intr_callback_unregister(&sh->intr_handle_devx,
1994 				  mlx5_dev_interrupt_handler_devx, sh);
1995 	if (sh->devx_comp)
1996 		mlx5_glue->devx_destroy_cmd_comp(sh->devx_comp);
1997 #endif
1998 }
1999 
2000 /**
2001  * Read statistics by a named counter.
2002  *
2003  * @param[in] priv
2004  *   Pointer to the private device data structure.
2005  * @param[in] ctr_name
2006  *   Pointer to the name of the statistic counter to read
2007  * @param[out] stat
2008  *   Pointer to read statistic value.
2009  * @return
2010  *   0 on success and stat is valud, 1 if failed to read the value
2011  *   rte_errno is set.
2012  *
2013  */
2014 int
2015 mlx5_os_read_dev_stat(struct mlx5_priv *priv, const char *ctr_name,
2016 		      uint64_t *stat)
2017 {
2018 	int fd;
2019 
2020 	if (priv->sh) {
2021 		MKSTR(path, "%s/ports/%d/hw_counters/%s",
2022 			  priv->sh->ibdev_path,
2023 			  priv->dev_port,
2024 			  ctr_name);
2025 		fd = open(path, O_RDONLY);
2026 		if (fd != -1) {
2027 			char buf[21] = {'\0'};
2028 			ssize_t n = read(fd, buf, sizeof(buf));
2029 
2030 			close(fd);
2031 			if (n != -1) {
2032 				*stat = strtoull(buf, NULL, 10);
2033 				return 0;
2034 			}
2035 		}
2036 	}
2037 	*stat = 0;
2038 	return 1;
2039 }
2040 
2041 /**
2042  * Read device counters table.
2043  *
2044  * @param dev
2045  *   Pointer to Ethernet device.
2046  * @param[out] stats
2047  *   Counters table output buffer.
2048  *
2049  * @return
2050  *   0 on success and stats is filled, negative errno value otherwise and
2051  *   rte_errno is set.
2052  */
2053 int
2054 mlx5_os_read_dev_counters(struct rte_eth_dev *dev, uint64_t *stats)
2055 {
2056 	struct mlx5_priv *priv = dev->data->dev_private;
2057 	struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
2058 	unsigned int i;
2059 	struct ifreq ifr;
2060 	unsigned int stats_sz = xstats_ctrl->stats_n * sizeof(uint64_t);
2061 	unsigned char et_stat_buf[sizeof(struct ethtool_stats) + stats_sz];
2062 	struct ethtool_stats *et_stats = (struct ethtool_stats *)et_stat_buf;
2063 	int ret;
2064 
2065 	et_stats->cmd = ETHTOOL_GSTATS;
2066 	et_stats->n_stats = xstats_ctrl->stats_n;
2067 	ifr.ifr_data = (caddr_t)et_stats;
2068 	ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
2069 	if (ret) {
2070 		DRV_LOG(WARNING,
2071 			"port %u unable to read statistic values from device",
2072 			dev->data->port_id);
2073 		return ret;
2074 	}
2075 	for (i = 0; i != xstats_ctrl->mlx5_stats_n; ++i) {
2076 		if (xstats_ctrl->info[i].dev) {
2077 			ret = mlx5_os_read_dev_stat(priv,
2078 					    xstats_ctrl->info[i].ctr_name,
2079 					    &stats[i]);
2080 			/* return last xstats counter if fail to read. */
2081 			if (ret == 0)
2082 				xstats_ctrl->xstats[i] = stats[i];
2083 			else
2084 				stats[i] = xstats_ctrl->xstats[i];
2085 		} else {
2086 			stats[i] = (uint64_t)
2087 				et_stats->data[xstats_ctrl->dev_table_idx[i]];
2088 		}
2089 	}
2090 	return 0;
2091 }
2092 
2093 /**
2094  * Query the number of statistics provided by ETHTOOL.
2095  *
2096  * @param dev
2097  *   Pointer to Ethernet device.
2098  *
2099  * @return
2100  *   Number of statistics on success, negative errno value otherwise and
2101  *   rte_errno is set.
2102  */
2103 int
2104 mlx5_os_get_stats_n(struct rte_eth_dev *dev)
2105 {
2106 	struct ethtool_drvinfo drvinfo;
2107 	struct ifreq ifr;
2108 	int ret;
2109 
2110 	drvinfo.cmd = ETHTOOL_GDRVINFO;
2111 	ifr.ifr_data = (caddr_t)&drvinfo;
2112 	ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
2113 	if (ret) {
2114 		DRV_LOG(WARNING, "port %u unable to query number of statistics",
2115 			dev->data->port_id);
2116 		return ret;
2117 	}
2118 	return drvinfo.n_stats;
2119 }
2120 
2121 static const struct mlx5_counter_ctrl mlx5_counters_init[] = {
2122 	{
2123 		.dpdk_name = "rx_port_unicast_bytes",
2124 		.ctr_name = "rx_vport_unicast_bytes",
2125 	},
2126 	{
2127 		.dpdk_name = "rx_port_multicast_bytes",
2128 		.ctr_name = "rx_vport_multicast_bytes",
2129 	},
2130 	{
2131 		.dpdk_name = "rx_port_broadcast_bytes",
2132 		.ctr_name = "rx_vport_broadcast_bytes",
2133 	},
2134 	{
2135 		.dpdk_name = "rx_port_unicast_packets",
2136 		.ctr_name = "rx_vport_unicast_packets",
2137 	},
2138 	{
2139 		.dpdk_name = "rx_port_multicast_packets",
2140 		.ctr_name = "rx_vport_multicast_packets",
2141 	},
2142 	{
2143 		.dpdk_name = "rx_port_broadcast_packets",
2144 		.ctr_name = "rx_vport_broadcast_packets",
2145 	},
2146 	{
2147 		.dpdk_name = "tx_port_unicast_bytes",
2148 		.ctr_name = "tx_vport_unicast_bytes",
2149 	},
2150 	{
2151 		.dpdk_name = "tx_port_multicast_bytes",
2152 		.ctr_name = "tx_vport_multicast_bytes",
2153 	},
2154 	{
2155 		.dpdk_name = "tx_port_broadcast_bytes",
2156 		.ctr_name = "tx_vport_broadcast_bytes",
2157 	},
2158 	{
2159 		.dpdk_name = "tx_port_unicast_packets",
2160 		.ctr_name = "tx_vport_unicast_packets",
2161 	},
2162 	{
2163 		.dpdk_name = "tx_port_multicast_packets",
2164 		.ctr_name = "tx_vport_multicast_packets",
2165 	},
2166 	{
2167 		.dpdk_name = "tx_port_broadcast_packets",
2168 		.ctr_name = "tx_vport_broadcast_packets",
2169 	},
2170 	{
2171 		.dpdk_name = "rx_wqe_err",
2172 		.ctr_name = "rx_wqe_err",
2173 	},
2174 	{
2175 		.dpdk_name = "rx_crc_errors_phy",
2176 		.ctr_name = "rx_crc_errors_phy",
2177 	},
2178 	{
2179 		.dpdk_name = "rx_in_range_len_errors_phy",
2180 		.ctr_name = "rx_in_range_len_errors_phy",
2181 	},
2182 	{
2183 		.dpdk_name = "rx_symbol_err_phy",
2184 		.ctr_name = "rx_symbol_err_phy",
2185 	},
2186 	{
2187 		.dpdk_name = "tx_errors_phy",
2188 		.ctr_name = "tx_errors_phy",
2189 	},
2190 	{
2191 		.dpdk_name = "rx_out_of_buffer",
2192 		.ctr_name = "out_of_buffer",
2193 		.dev = 1,
2194 	},
2195 	{
2196 		.dpdk_name = "tx_packets_phy",
2197 		.ctr_name = "tx_packets_phy",
2198 	},
2199 	{
2200 		.dpdk_name = "rx_packets_phy",
2201 		.ctr_name = "rx_packets_phy",
2202 	},
2203 	{
2204 		.dpdk_name = "tx_discards_phy",
2205 		.ctr_name = "tx_discards_phy",
2206 	},
2207 	{
2208 		.dpdk_name = "rx_discards_phy",
2209 		.ctr_name = "rx_discards_phy",
2210 	},
2211 	{
2212 		.dpdk_name = "tx_bytes_phy",
2213 		.ctr_name = "tx_bytes_phy",
2214 	},
2215 	{
2216 		.dpdk_name = "rx_bytes_phy",
2217 		.ctr_name = "rx_bytes_phy",
2218 	},
2219 	/* Representor only */
2220 	{
2221 		.dpdk_name = "rx_packets",
2222 		.ctr_name = "vport_rx_packets",
2223 	},
2224 	{
2225 		.dpdk_name = "rx_bytes",
2226 		.ctr_name = "vport_rx_bytes",
2227 	},
2228 	{
2229 		.dpdk_name = "tx_packets",
2230 		.ctr_name = "vport_tx_packets",
2231 	},
2232 	{
2233 		.dpdk_name = "tx_bytes",
2234 		.ctr_name = "vport_tx_bytes",
2235 	},
2236 };
2237 
2238 static const unsigned int xstats_n = RTE_DIM(mlx5_counters_init);
2239 
2240 /**
2241  * Init the structures to read device counters.
2242  *
2243  * @param dev
2244  *   Pointer to Ethernet device.
2245  */
2246 void
2247 mlx5_os_stats_init(struct rte_eth_dev *dev)
2248 {
2249 	struct mlx5_priv *priv = dev->data->dev_private;
2250 	struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
2251 	struct mlx5_stats_ctrl *stats_ctrl = &priv->stats_ctrl;
2252 	unsigned int i;
2253 	unsigned int j;
2254 	struct ifreq ifr;
2255 	struct ethtool_gstrings *strings = NULL;
2256 	unsigned int dev_stats_n;
2257 	unsigned int str_sz;
2258 	int ret;
2259 
2260 	/* So that it won't aggregate for each init. */
2261 	xstats_ctrl->mlx5_stats_n = 0;
2262 	ret = mlx5_os_get_stats_n(dev);
2263 	if (ret < 0) {
2264 		DRV_LOG(WARNING, "port %u no extended statistics available",
2265 			dev->data->port_id);
2266 		return;
2267 	}
2268 	dev_stats_n = ret;
2269 	/* Allocate memory to grab stat names and values. */
2270 	str_sz = dev_stats_n * ETH_GSTRING_LEN;
2271 	strings = (struct ethtool_gstrings *)
2272 		  rte_malloc("xstats_strings",
2273 			     str_sz + sizeof(struct ethtool_gstrings), 0);
2274 	if (!strings) {
2275 		DRV_LOG(WARNING, "port %u unable to allocate memory for xstats",
2276 		     dev->data->port_id);
2277 		return;
2278 	}
2279 	strings->cmd = ETHTOOL_GSTRINGS;
2280 	strings->string_set = ETH_SS_STATS;
2281 	strings->len = dev_stats_n;
2282 	ifr.ifr_data = (caddr_t)strings;
2283 	ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
2284 	if (ret) {
2285 		DRV_LOG(WARNING, "port %u unable to get statistic names",
2286 			dev->data->port_id);
2287 		goto free;
2288 	}
2289 	for (i = 0; i != dev_stats_n; ++i) {
2290 		const char *curr_string = (const char *)
2291 			&strings->data[i * ETH_GSTRING_LEN];
2292 
2293 		for (j = 0; j != xstats_n; ++j) {
2294 			if (!strcmp(mlx5_counters_init[j].ctr_name,
2295 				    curr_string)) {
2296 				unsigned int idx = xstats_ctrl->mlx5_stats_n++;
2297 
2298 				xstats_ctrl->dev_table_idx[idx] = i;
2299 				xstats_ctrl->info[idx] = mlx5_counters_init[j];
2300 				break;
2301 			}
2302 		}
2303 	}
2304 	/* Add dev counters. */
2305 	for (i = 0; i != xstats_n; ++i) {
2306 		if (mlx5_counters_init[i].dev) {
2307 			unsigned int idx = xstats_ctrl->mlx5_stats_n++;
2308 
2309 			xstats_ctrl->info[idx] = mlx5_counters_init[i];
2310 			xstats_ctrl->hw_stats[idx] = 0;
2311 		}
2312 	}
2313 	MLX5_ASSERT(xstats_ctrl->mlx5_stats_n <= MLX5_MAX_XSTATS);
2314 	xstats_ctrl->stats_n = dev_stats_n;
2315 	/* Copy to base at first time. */
2316 	ret = mlx5_os_read_dev_counters(dev, xstats_ctrl->base);
2317 	if (ret)
2318 		DRV_LOG(ERR, "port %u cannot read device counters: %s",
2319 			dev->data->port_id, strerror(rte_errno));
2320 	mlx5_os_read_dev_stat(priv, "out_of_buffer", &stats_ctrl->imissed_base);
2321 	stats_ctrl->imissed = 0;
2322 free:
2323 	rte_free(strings);
2324 }
2325 
2326 /**
2327  * Set the reg_mr and dereg_mr call backs
2328  *
2329  * @param reg_mr_cb[out]
2330  *   Pointer to reg_mr func
2331  * @param dereg_mr_cb[out]
2332  *   Pointer to dereg_mr func
2333  *
2334  */
2335 void
2336 mlx5_os_set_reg_mr_cb(mlx5_reg_mr_t *reg_mr_cb,
2337 		      mlx5_dereg_mr_t *dereg_mr_cb)
2338 {
2339 	*reg_mr_cb = mlx5_verbs_ops.reg_mr;
2340 	*dereg_mr_cb = mlx5_verbs_ops.dereg_mr;
2341 }
2342 
2343 const struct eth_dev_ops mlx5_os_dev_ops = {
2344 	.dev_configure = mlx5_dev_configure,
2345 	.dev_start = mlx5_dev_start,
2346 	.dev_stop = mlx5_dev_stop,
2347 	.dev_set_link_down = mlx5_set_link_down,
2348 	.dev_set_link_up = mlx5_set_link_up,
2349 	.dev_close = mlx5_dev_close,
2350 	.promiscuous_enable = mlx5_promiscuous_enable,
2351 	.promiscuous_disable = mlx5_promiscuous_disable,
2352 	.allmulticast_enable = mlx5_allmulticast_enable,
2353 	.allmulticast_disable = mlx5_allmulticast_disable,
2354 	.link_update = mlx5_link_update,
2355 	.stats_get = mlx5_stats_get,
2356 	.stats_reset = mlx5_stats_reset,
2357 	.xstats_get = mlx5_xstats_get,
2358 	.xstats_reset = mlx5_xstats_reset,
2359 	.xstats_get_names = mlx5_xstats_get_names,
2360 	.fw_version_get = mlx5_fw_version_get,
2361 	.dev_infos_get = mlx5_dev_infos_get,
2362 	.read_clock = mlx5_read_clock,
2363 	.dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
2364 	.vlan_filter_set = mlx5_vlan_filter_set,
2365 	.rx_queue_setup = mlx5_rx_queue_setup,
2366 	.rx_hairpin_queue_setup = mlx5_rx_hairpin_queue_setup,
2367 	.tx_queue_setup = mlx5_tx_queue_setup,
2368 	.tx_hairpin_queue_setup = mlx5_tx_hairpin_queue_setup,
2369 	.rx_queue_release = mlx5_rx_queue_release,
2370 	.tx_queue_release = mlx5_tx_queue_release,
2371 	.flow_ctrl_get = mlx5_dev_get_flow_ctrl,
2372 	.flow_ctrl_set = mlx5_dev_set_flow_ctrl,
2373 	.mac_addr_remove = mlx5_mac_addr_remove,
2374 	.mac_addr_add = mlx5_mac_addr_add,
2375 	.mac_addr_set = mlx5_mac_addr_set,
2376 	.set_mc_addr_list = mlx5_set_mc_addr_list,
2377 	.mtu_set = mlx5_dev_set_mtu,
2378 	.vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
2379 	.vlan_offload_set = mlx5_vlan_offload_set,
2380 	.reta_update = mlx5_dev_rss_reta_update,
2381 	.reta_query = mlx5_dev_rss_reta_query,
2382 	.rss_hash_update = mlx5_rss_hash_update,
2383 	.rss_hash_conf_get = mlx5_rss_hash_conf_get,
2384 	.filter_ctrl = mlx5_dev_filter_ctrl,
2385 	.rx_descriptor_status = mlx5_rx_descriptor_status,
2386 	.tx_descriptor_status = mlx5_tx_descriptor_status,
2387 	.rxq_info_get = mlx5_rxq_info_get,
2388 	.txq_info_get = mlx5_txq_info_get,
2389 	.rx_burst_mode_get = mlx5_rx_burst_mode_get,
2390 	.tx_burst_mode_get = mlx5_tx_burst_mode_get,
2391 	.rx_queue_count = mlx5_rx_queue_count,
2392 	.rx_queue_intr_enable = mlx5_rx_intr_enable,
2393 	.rx_queue_intr_disable = mlx5_rx_intr_disable,
2394 	.is_removed = mlx5_is_removed,
2395 	.udp_tunnel_port_add  = mlx5_udp_tunnel_port_add,
2396 	.get_module_info = mlx5_get_module_info,
2397 	.get_module_eeprom = mlx5_get_module_eeprom,
2398 	.hairpin_cap_get = mlx5_hairpin_cap_get,
2399 	.mtr_ops_get = mlx5_flow_meter_ops_get,
2400 };
2401 
2402 /* Available operations from secondary process. */
2403 const struct eth_dev_ops mlx5_os_dev_sec_ops = {
2404 	.stats_get = mlx5_stats_get,
2405 	.stats_reset = mlx5_stats_reset,
2406 	.xstats_get = mlx5_xstats_get,
2407 	.xstats_reset = mlx5_xstats_reset,
2408 	.xstats_get_names = mlx5_xstats_get_names,
2409 	.fw_version_get = mlx5_fw_version_get,
2410 	.dev_infos_get = mlx5_dev_infos_get,
2411 	.rx_descriptor_status = mlx5_rx_descriptor_status,
2412 	.tx_descriptor_status = mlx5_tx_descriptor_status,
2413 	.rxq_info_get = mlx5_rxq_info_get,
2414 	.txq_info_get = mlx5_txq_info_get,
2415 	.rx_burst_mode_get = mlx5_rx_burst_mode_get,
2416 	.tx_burst_mode_get = mlx5_tx_burst_mode_get,
2417 	.get_module_info = mlx5_get_module_info,
2418 	.get_module_eeprom = mlx5_get_module_eeprom,
2419 };
2420 
2421 /* Available operations in flow isolated mode. */
2422 const struct eth_dev_ops mlx5_os_dev_ops_isolate = {
2423 	.dev_configure = mlx5_dev_configure,
2424 	.dev_start = mlx5_dev_start,
2425 	.dev_stop = mlx5_dev_stop,
2426 	.dev_set_link_down = mlx5_set_link_down,
2427 	.dev_set_link_up = mlx5_set_link_up,
2428 	.dev_close = mlx5_dev_close,
2429 	.promiscuous_enable = mlx5_promiscuous_enable,
2430 	.promiscuous_disable = mlx5_promiscuous_disable,
2431 	.allmulticast_enable = mlx5_allmulticast_enable,
2432 	.allmulticast_disable = mlx5_allmulticast_disable,
2433 	.link_update = mlx5_link_update,
2434 	.stats_get = mlx5_stats_get,
2435 	.stats_reset = mlx5_stats_reset,
2436 	.xstats_get = mlx5_xstats_get,
2437 	.xstats_reset = mlx5_xstats_reset,
2438 	.xstats_get_names = mlx5_xstats_get_names,
2439 	.fw_version_get = mlx5_fw_version_get,
2440 	.dev_infos_get = mlx5_dev_infos_get,
2441 	.dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
2442 	.vlan_filter_set = mlx5_vlan_filter_set,
2443 	.rx_queue_setup = mlx5_rx_queue_setup,
2444 	.rx_hairpin_queue_setup = mlx5_rx_hairpin_queue_setup,
2445 	.tx_queue_setup = mlx5_tx_queue_setup,
2446 	.tx_hairpin_queue_setup = mlx5_tx_hairpin_queue_setup,
2447 	.rx_queue_release = mlx5_rx_queue_release,
2448 	.tx_queue_release = mlx5_tx_queue_release,
2449 	.flow_ctrl_get = mlx5_dev_get_flow_ctrl,
2450 	.flow_ctrl_set = mlx5_dev_set_flow_ctrl,
2451 	.mac_addr_remove = mlx5_mac_addr_remove,
2452 	.mac_addr_add = mlx5_mac_addr_add,
2453 	.mac_addr_set = mlx5_mac_addr_set,
2454 	.set_mc_addr_list = mlx5_set_mc_addr_list,
2455 	.mtu_set = mlx5_dev_set_mtu,
2456 	.vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
2457 	.vlan_offload_set = mlx5_vlan_offload_set,
2458 	.filter_ctrl = mlx5_dev_filter_ctrl,
2459 	.rx_descriptor_status = mlx5_rx_descriptor_status,
2460 	.tx_descriptor_status = mlx5_tx_descriptor_status,
2461 	.rxq_info_get = mlx5_rxq_info_get,
2462 	.txq_info_get = mlx5_txq_info_get,
2463 	.rx_burst_mode_get = mlx5_rx_burst_mode_get,
2464 	.tx_burst_mode_get = mlx5_tx_burst_mode_get,
2465 	.rx_queue_intr_enable = mlx5_rx_intr_enable,
2466 	.rx_queue_intr_disable = mlx5_rx_intr_disable,
2467 	.is_removed = mlx5_is_removed,
2468 	.get_module_info = mlx5_get_module_info,
2469 	.get_module_eeprom = mlx5_get_module_eeprom,
2470 	.hairpin_cap_get = mlx5_hairpin_cap_get,
2471 	.mtr_ops_get = mlx5_flow_meter_ops_get,
2472 };
2473