xref: /dpdk/drivers/net/mlx5/linux/mlx5_os.c (revision f1fecffa88df1580501b6a8d5be5f570be46ca74)
1f44b09f9SOphir Munk /* SPDX-License-Identifier: BSD-3-Clause
2f44b09f9SOphir Munk  * Copyright 2015 6WIND S.A.
3f44b09f9SOphir Munk  * Copyright 2020 Mellanox Technologies, Ltd
4f44b09f9SOphir Munk  */
5f44b09f9SOphir Munk 
6f44b09f9SOphir Munk #include <stddef.h>
7f44b09f9SOphir Munk #include <unistd.h>
8f44b09f9SOphir Munk #include <string.h>
9f44b09f9SOphir Munk #include <stdint.h>
10f44b09f9SOphir Munk #include <stdlib.h>
11f44b09f9SOphir Munk #include <errno.h>
12f44b09f9SOphir Munk #include <net/if.h>
13f44b09f9SOphir Munk #include <linux/rtnetlink.h>
1473bf9235SOphir Munk #include <linux/sockios.h>
1573bf9235SOphir Munk #include <linux/ethtool.h>
16f44b09f9SOphir Munk #include <fcntl.h>
17f44b09f9SOphir Munk 
18f44b09f9SOphir Munk #include <rte_malloc.h>
19df96fd0dSBruce Richardson #include <ethdev_driver.h>
20df96fd0dSBruce Richardson #include <ethdev_pci.h>
21f44b09f9SOphir Munk #include <rte_pci.h>
22a04322f6SDavid Marchand #include <bus_driver.h>
231f37cb2bSDavid Marchand #include <bus_pci_driver.h>
24b3f89090SDavid Marchand #include <bus_auxiliary_driver.h>
25f44b09f9SOphir Munk #include <rte_common.h>
26f44b09f9SOphir Munk #include <rte_kvargs.h>
27f44b09f9SOphir Munk #include <rte_rwlock.h>
28f44b09f9SOphir Munk #include <rte_spinlock.h>
29f44b09f9SOphir Munk #include <rte_string_fns.h>
30f44b09f9SOphir Munk #include <rte_alarm.h>
312aba9fc7SOphir Munk #include <rte_eal_paging.h>
32f44b09f9SOphir Munk 
33f44b09f9SOphir Munk #include <mlx5_glue.h>
34f44b09f9SOphir Munk #include <mlx5_devx_cmds.h>
35f44b09f9SOphir Munk #include <mlx5_common.h>
362eb4d010SOphir Munk #include <mlx5_common_mp.h>
37d5ed8aa9SOphir Munk #include <mlx5_common_mr.h>
385522da6bSSuanming Mou #include <mlx5_malloc.h>
39f44b09f9SOphir Munk 
40f44b09f9SOphir Munk #include "mlx5_defs.h"
41f44b09f9SOphir Munk #include "mlx5.h"
42391b8bccSOphir Munk #include "mlx5_common_os.h"
43f44b09f9SOphir Munk #include "mlx5_utils.h"
44f44b09f9SOphir Munk #include "mlx5_rxtx.h"
45151cbe3aSMichael Baum #include "mlx5_rx.h"
46377b69fbSMichael Baum #include "mlx5_tx.h"
47f44b09f9SOphir Munk #include "mlx5_autoconf.h"
48f44b09f9SOphir Munk #include "mlx5_flow.h"
49f44b09f9SOphir Munk #include "rte_pmd_mlx5.h"
504f96d913SOphir Munk #include "mlx5_verbs.h"
51f00f6562SOphir Munk #include "mlx5_nl.h"
526deb19e1SMichael Baum #include "mlx5_devx.h"
53f44b09f9SOphir Munk 
542eb4d010SOphir Munk #ifndef HAVE_IBV_MLX5_MOD_MPW
552eb4d010SOphir Munk #define MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED (1 << 2)
562eb4d010SOphir Munk #define MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW (1 << 3)
572eb4d010SOphir Munk #endif
582eb4d010SOphir Munk 
592eb4d010SOphir Munk #ifndef HAVE_IBV_MLX5_MOD_CQE_128B_COMP
602eb4d010SOphir Munk #define MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP (1 << 4)
612eb4d010SOphir Munk #endif
622eb4d010SOphir Munk 
632e86c4e5SOphir Munk static const char *MZ_MLX5_PMD_SHARED_DATA = "mlx5_pmd_shared_data";
642e86c4e5SOphir Munk 
652e86c4e5SOphir Munk /* Spinlock for mlx5_shared_data allocation. */
662e86c4e5SOphir Munk static rte_spinlock_t mlx5_shared_data_lock = RTE_SPINLOCK_INITIALIZER;
672e86c4e5SOphir Munk 
682e86c4e5SOphir Munk /* Process local data for secondary processes. */
692e86c4e5SOphir Munk static struct mlx5_local_data mlx5_local_data;
702e86c4e5SOphir Munk 
71b4edeaf3SSuanming Mou /* rte flow indexed pool configuration. */
72b4edeaf3SSuanming Mou static struct mlx5_indexed_pool_config icfg[] = {
73b4edeaf3SSuanming Mou 	{
74b4edeaf3SSuanming Mou 		.size = sizeof(struct rte_flow),
75b4edeaf3SSuanming Mou 		.trunk_size = 64,
76b4edeaf3SSuanming Mou 		.need_lock = 1,
77b4edeaf3SSuanming Mou 		.release_mem_en = 0,
78b4edeaf3SSuanming Mou 		.malloc = mlx5_malloc,
79b4edeaf3SSuanming Mou 		.free = mlx5_free,
80b4edeaf3SSuanming Mou 		.per_core_cache = 0,
81b4edeaf3SSuanming Mou 		.type = "ctl_flow_ipool",
82b4edeaf3SSuanming Mou 	},
83b4edeaf3SSuanming Mou 	{
84b4edeaf3SSuanming Mou 		.size = sizeof(struct rte_flow),
85b4edeaf3SSuanming Mou 		.trunk_size = 64,
86b4edeaf3SSuanming Mou 		.grow_trunk = 3,
87b4edeaf3SSuanming Mou 		.grow_shift = 2,
88b4edeaf3SSuanming Mou 		.need_lock = 1,
89b4edeaf3SSuanming Mou 		.release_mem_en = 0,
90b4edeaf3SSuanming Mou 		.malloc = mlx5_malloc,
91b4edeaf3SSuanming Mou 		.free = mlx5_free,
92b4edeaf3SSuanming Mou 		.per_core_cache = 1 << 14,
93b4edeaf3SSuanming Mou 		.type = "rte_flow_ipool",
94b4edeaf3SSuanming Mou 	},
95b4edeaf3SSuanming Mou 	{
96b4edeaf3SSuanming Mou 		.size = sizeof(struct rte_flow),
97b4edeaf3SSuanming Mou 		.trunk_size = 64,
98b4edeaf3SSuanming Mou 		.grow_trunk = 3,
99b4edeaf3SSuanming Mou 		.grow_shift = 2,
100b4edeaf3SSuanming Mou 		.need_lock = 1,
101b4edeaf3SSuanming Mou 		.release_mem_en = 0,
102b4edeaf3SSuanming Mou 		.malloc = mlx5_malloc,
103b4edeaf3SSuanming Mou 		.free = mlx5_free,
104b4edeaf3SSuanming Mou 		.per_core_cache = 0,
105b4edeaf3SSuanming Mou 		.type = "mcp_flow_ipool",
106b4edeaf3SSuanming Mou 	},
107b4edeaf3SSuanming Mou };
108b4edeaf3SSuanming Mou 
109f44b09f9SOphir Munk /**
11008d1838fSDekel Peled  * Set the completion channel file descriptor interrupt as non-blocking.
11108d1838fSDekel Peled  *
11208d1838fSDekel Peled  * @param[in] rxq_obj
11308d1838fSDekel Peled  *   Pointer to RQ channel object, which includes the channel fd
11408d1838fSDekel Peled  *
11508d1838fSDekel Peled  * @param[out] fd
1167be78d02SJosh Soref  *   The file descriptor (representing the interrupt) used in this channel.
11708d1838fSDekel Peled  *
11808d1838fSDekel Peled  * @return
11908d1838fSDekel Peled  *   0 on successfully setting the fd to non-blocking, non-zero otherwise.
12008d1838fSDekel Peled  */
12108d1838fSDekel Peled int
12208d1838fSDekel Peled mlx5_os_set_nonblock_channel_fd(int fd)
12308d1838fSDekel Peled {
12408d1838fSDekel Peled 	int flags;
12508d1838fSDekel Peled 
12608d1838fSDekel Peled 	flags = fcntl(fd, F_GETFL);
12708d1838fSDekel Peled 	return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
12808d1838fSDekel Peled }
12908d1838fSDekel Peled 
13008d1838fSDekel Peled /**
131e85f623eSOphir Munk  * Get mlx5 device attributes. The glue function query_device_ex() is called
132e85f623eSOphir Munk  * with out parameter of type 'struct ibv_device_attr_ex *'. Then fill in mlx5
133e85f623eSOphir Munk  * device attributes from the glue out parameter.
134e85f623eSOphir Munk  *
13591d1cfafSMichael Baum  * @param sh
13691d1cfafSMichael Baum  *   Pointer to shared device context.
137e85f623eSOphir Munk  *
138e85f623eSOphir Munk  * @return
1396be4c57aSMichael Baum  *   0 on success, a negative errno value otherwise and rte_errno is set.
140e85f623eSOphir Munk  */
141e85f623eSOphir Munk int
14291d1cfafSMichael Baum mlx5_os_capabilities_prepare(struct mlx5_dev_ctx_shared *sh)
143e85f623eSOphir Munk {
144e85f623eSOphir Munk 	int err;
14587af0d1eSMichael Baum 	struct mlx5_common_device *cdev = sh->cdev;
14687af0d1eSMichael Baum 	struct mlx5_hca_attr *hca_attr = &cdev->config.hca_attr;
14791d1cfafSMichael Baum 	struct ibv_device_attr_ex attr_ex = { .comp_mask = 0 };
14891d1cfafSMichael Baum 	struct mlx5dv_context dv_attr = { .comp_mask = 0 };
149fe46b20cSMichael Baum 
15087af0d1eSMichael Baum 	err = mlx5_glue->query_device_ex(cdev->ctx, NULL, &attr_ex);
1516be4c57aSMichael Baum 	if (err) {
1526be4c57aSMichael Baum 		rte_errno = errno;
1536be4c57aSMichael Baum 		return -rte_errno;
1546be4c57aSMichael Baum 	}
1558f464810SMichael Baum #ifdef HAVE_IBV_MLX5_MOD_SWP
1568f464810SMichael Baum 	dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_SWP;
1578f464810SMichael Baum #endif
1588f464810SMichael Baum #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
1598f464810SMichael Baum 	dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS;
1608f464810SMichael Baum #endif
1618f464810SMichael Baum #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
1628f464810SMichael Baum 	dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_STRIDING_RQ;
1638f464810SMichael Baum #endif
16487af0d1eSMichael Baum 	err = mlx5_glue->dv_query_device(cdev->ctx, &dv_attr);
1656be4c57aSMichael Baum 	if (err) {
1666be4c57aSMichael Baum 		rte_errno = errno;
1676be4c57aSMichael Baum 		return -rte_errno;
1686be4c57aSMichael Baum 	}
16991d1cfafSMichael Baum 	memset(&sh->dev_cap, 0, sizeof(struct mlx5_dev_cap));
17087af0d1eSMichael Baum 	if (mlx5_dev_is_pci(cdev->dev))
17187af0d1eSMichael Baum 		sh->dev_cap.vf = mlx5_dev_is_vf_pci(RTE_DEV_TO_PCI(cdev->dev));
17287af0d1eSMichael Baum 	else
17387af0d1eSMichael Baum 		sh->dev_cap.sf = 1;
17491d1cfafSMichael Baum 	sh->dev_cap.max_qp_wr = attr_ex.orig_attr.max_qp_wr;
17591d1cfafSMichael Baum 	sh->dev_cap.max_sge = attr_ex.orig_attr.max_sge;
17691d1cfafSMichael Baum 	sh->dev_cap.max_cq = attr_ex.orig_attr.max_cq;
17791d1cfafSMichael Baum 	sh->dev_cap.max_qp = attr_ex.orig_attr.max_qp;
17887af0d1eSMichael Baum #ifdef HAVE_MLX5DV_DR_ACTION_DEST_DEVX_TIR
17987af0d1eSMichael Baum 	sh->dev_cap.dest_tir = 1;
18087af0d1eSMichael Baum #endif
18187af0d1eSMichael Baum #if defined(HAVE_IBV_FLOW_DV_SUPPORT) && defined(HAVE_MLX5DV_DR)
18287af0d1eSMichael Baum 	DRV_LOG(DEBUG, "DV flow is supported.");
18387af0d1eSMichael Baum 	sh->dev_cap.dv_flow_en = 1;
18487af0d1eSMichael Baum #endif
18587af0d1eSMichael Baum #ifdef HAVE_MLX5DV_DR_ESWITCH
18687af0d1eSMichael Baum 	if (hca_attr->eswitch_manager && sh->dev_cap.dv_flow_en && sh->esw_mode)
18787af0d1eSMichael Baum 		sh->dev_cap.dv_esw_en = 1;
18887af0d1eSMichael Baum #endif
18987af0d1eSMichael Baum 	/*
19087af0d1eSMichael Baum 	 * Multi-packet send is supported by ConnectX-4 Lx PF as well
19187af0d1eSMichael Baum 	 * as all ConnectX-5 devices.
19287af0d1eSMichael Baum 	 */
19387af0d1eSMichael Baum 	if (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED) {
19487af0d1eSMichael Baum 		if (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW) {
19587af0d1eSMichael Baum 			DRV_LOG(DEBUG, "Enhanced MPW is supported.");
19687af0d1eSMichael Baum 			sh->dev_cap.mps = MLX5_MPW_ENHANCED;
19787af0d1eSMichael Baum 		} else {
19887af0d1eSMichael Baum 			DRV_LOG(DEBUG, "MPW is supported.");
19987af0d1eSMichael Baum 			sh->dev_cap.mps = MLX5_MPW;
20087af0d1eSMichael Baum 		}
20187af0d1eSMichael Baum 	} else {
20287af0d1eSMichael Baum 		DRV_LOG(DEBUG, "MPW isn't supported.");
20387af0d1eSMichael Baum 		sh->dev_cap.mps = MLX5_MPW_DISABLED;
20487af0d1eSMichael Baum 	}
20587af0d1eSMichael Baum #if (RTE_CACHE_LINE_SIZE == 128)
20687af0d1eSMichael Baum 	if (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP)
20787af0d1eSMichael Baum 		sh->dev_cap.cqe_comp = 1;
20887af0d1eSMichael Baum 	DRV_LOG(DEBUG, "Rx CQE 128B compression is %ssupported.",
20987af0d1eSMichael Baum 		sh->dev_cap.cqe_comp ? "" : "not ");
21087af0d1eSMichael Baum #else
21187af0d1eSMichael Baum 	sh->dev_cap.cqe_comp = 1;
21287af0d1eSMichael Baum #endif
21387af0d1eSMichael Baum #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
21487af0d1eSMichael Baum 	sh->dev_cap.mpls_en =
21587af0d1eSMichael Baum 		((dv_attr.tunnel_offloads_caps &
21687af0d1eSMichael Baum 		  MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_CW_MPLS_OVER_GRE) &&
21787af0d1eSMichael Baum 		 (dv_attr.tunnel_offloads_caps &
21887af0d1eSMichael Baum 		  MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_CW_MPLS_OVER_UDP));
21987af0d1eSMichael Baum 	DRV_LOG(DEBUG, "MPLS over GRE/UDP tunnel offloading is %ssupported.",
22087af0d1eSMichael Baum 		sh->dev_cap.mpls_en ? "" : "not ");
22187af0d1eSMichael Baum #else
22287af0d1eSMichael Baum 	DRV_LOG(WARNING,
22387af0d1eSMichael Baum 		"MPLS over GRE/UDP tunnel offloading disabled due to old OFED/rdma-core version or firmware configuration");
22487af0d1eSMichael Baum #endif
22587af0d1eSMichael Baum #if defined(HAVE_IBV_WQ_FLAG_RX_END_PADDING)
22687af0d1eSMichael Baum 	sh->dev_cap.hw_padding = !!attr_ex.rx_pad_end_addr_align;
22787af0d1eSMichael Baum #elif defined(HAVE_IBV_WQ_FLAGS_PCI_WRITE_END_PADDING)
22887af0d1eSMichael Baum 	sh->dev_cap.hw_padding = !!(attr_ex.device_cap_flags_ex &
22987af0d1eSMichael Baum 				    IBV_DEVICE_PCI_WRITE_END_PADDING);
23087af0d1eSMichael Baum #endif
23187af0d1eSMichael Baum 	sh->dev_cap.hw_csum =
23287af0d1eSMichael Baum 		!!(attr_ex.device_cap_flags_ex & IBV_DEVICE_RAW_IP_CSUM);
23387af0d1eSMichael Baum 	DRV_LOG(DEBUG, "Checksum offloading is %ssupported.",
23487af0d1eSMichael Baum 		sh->dev_cap.hw_csum ? "" : "not ");
23587af0d1eSMichael Baum 	sh->dev_cap.hw_vlan_strip = !!(attr_ex.raw_packet_caps &
23687af0d1eSMichael Baum 				       IBV_RAW_PACKET_CAP_CVLAN_STRIPPING);
23787af0d1eSMichael Baum 	DRV_LOG(DEBUG, "VLAN stripping is %ssupported.",
23887af0d1eSMichael Baum 		(sh->dev_cap.hw_vlan_strip ? "" : "not "));
23987af0d1eSMichael Baum 	sh->dev_cap.hw_fcs_strip = !!(attr_ex.raw_packet_caps &
24087af0d1eSMichael Baum 				      IBV_RAW_PACKET_CAP_SCATTER_FCS);
24187af0d1eSMichael Baum #if !defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) && \
24287af0d1eSMichael Baum 	!defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
24387af0d1eSMichael Baum 	DRV_LOG(DEBUG, "Counters are not supported.");
24487af0d1eSMichael Baum #endif
24587af0d1eSMichael Baum 	/*
24687af0d1eSMichael Baum 	 * DPDK doesn't support larger/variable indirection tables.
24787af0d1eSMichael Baum 	 * Once DPDK supports it, take max size from device attr.
24887af0d1eSMichael Baum 	 */
24987af0d1eSMichael Baum 	sh->dev_cap.ind_table_max_size =
25087af0d1eSMichael Baum 			RTE_MIN(attr_ex.rss_caps.max_rwq_indirection_table_size,
25187af0d1eSMichael Baum 				(unsigned int)RTE_ETH_RSS_RETA_SIZE_512);
25287af0d1eSMichael Baum 	DRV_LOG(DEBUG, "Maximum Rx indirection table size is %u",
25387af0d1eSMichael Baum 		sh->dev_cap.ind_table_max_size);
25487af0d1eSMichael Baum 	sh->dev_cap.tso = (attr_ex.tso_caps.max_tso > 0 &&
25587af0d1eSMichael Baum 			   (attr_ex.tso_caps.supported_qpts &
25687af0d1eSMichael Baum 			    (1 << IBV_QPT_RAW_PACKET)));
25787af0d1eSMichael Baum 	if (sh->dev_cap.tso)
25887af0d1eSMichael Baum 		sh->dev_cap.tso_max_payload_sz = attr_ex.tso_caps.max_tso;
25991d1cfafSMichael Baum 	strlcpy(sh->dev_cap.fw_ver, attr_ex.orig_attr.fw_ver,
26091d1cfafSMichael Baum 		sizeof(sh->dev_cap.fw_ver));
261e85f623eSOphir Munk #ifdef HAVE_IBV_MLX5_MOD_SWP
26287af0d1eSMichael Baum 	if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_SWP)
26387af0d1eSMichael Baum 		sh->dev_cap.swp = dv_attr.sw_parsing_caps.sw_parsing_offloads &
26487af0d1eSMichael Baum 				  (MLX5_SW_PARSING_CAP |
26587af0d1eSMichael Baum 				   MLX5_SW_PARSING_CSUM_CAP |
26687af0d1eSMichael Baum 				   MLX5_SW_PARSING_TSO_CAP);
26787af0d1eSMichael Baum 	DRV_LOG(DEBUG, "SWP support: %u", sh->dev_cap.swp);
268e85f623eSOphir Munk #endif
2698f464810SMichael Baum #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
27087af0d1eSMichael Baum 	if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_STRIDING_RQ) {
27187af0d1eSMichael Baum 		struct mlx5dv_striding_rq_caps *strd_rq_caps =
27287af0d1eSMichael Baum 				&dv_attr.striding_rq_caps;
27387af0d1eSMichael Baum 
27487af0d1eSMichael Baum 		sh->dev_cap.mprq.enabled = 1;
27587af0d1eSMichael Baum 		sh->dev_cap.mprq.log_min_stride_size =
27687af0d1eSMichael Baum 			strd_rq_caps->min_single_stride_log_num_of_bytes;
27787af0d1eSMichael Baum 		sh->dev_cap.mprq.log_max_stride_size =
27887af0d1eSMichael Baum 			strd_rq_caps->max_single_stride_log_num_of_bytes;
27987af0d1eSMichael Baum 		sh->dev_cap.mprq.log_min_stride_num =
28087af0d1eSMichael Baum 			strd_rq_caps->min_single_wqe_log_num_of_strides;
28187af0d1eSMichael Baum 		sh->dev_cap.mprq.log_max_stride_num =
28287af0d1eSMichael Baum 			strd_rq_caps->max_single_wqe_log_num_of_strides;
28387af0d1eSMichael Baum 		sh->dev_cap.mprq.log_min_stride_wqe_size =
28487af0d1eSMichael Baum 					cdev->config.devx ?
28587af0d1eSMichael Baum 					hca_attr->log_min_stride_wqe_sz :
28687af0d1eSMichael Baum 					MLX5_MPRQ_LOG_MIN_STRIDE_WQE_SIZE;
28787af0d1eSMichael Baum 		DRV_LOG(DEBUG, "\tmin_single_stride_log_num_of_bytes: %u",
28887af0d1eSMichael Baum 			sh->dev_cap.mprq.log_min_stride_size);
28987af0d1eSMichael Baum 		DRV_LOG(DEBUG, "\tmax_single_stride_log_num_of_bytes: %u",
29087af0d1eSMichael Baum 			sh->dev_cap.mprq.log_max_stride_size);
29187af0d1eSMichael Baum 		DRV_LOG(DEBUG, "\tmin_single_wqe_log_num_of_strides: %u",
29287af0d1eSMichael Baum 			sh->dev_cap.mprq.log_min_stride_num);
29387af0d1eSMichael Baum 		DRV_LOG(DEBUG, "\tmax_single_wqe_log_num_of_strides: %u",
29487af0d1eSMichael Baum 			sh->dev_cap.mprq.log_max_stride_num);
29587af0d1eSMichael Baum 		DRV_LOG(DEBUG, "\tmin_stride_wqe_log_size: %u",
29687af0d1eSMichael Baum 			sh->dev_cap.mprq.log_min_stride_wqe_size);
29787af0d1eSMichael Baum 		DRV_LOG(DEBUG, "\tsupported_qpts: %d",
29887af0d1eSMichael Baum 			strd_rq_caps->supported_qpts);
29987af0d1eSMichael Baum 		DRV_LOG(DEBUG, "Device supports Multi-Packet RQ.");
30087af0d1eSMichael Baum 	}
3018f464810SMichael Baum #endif
302e85f623eSOphir Munk #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
30387af0d1eSMichael Baum 	if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS) {
30487af0d1eSMichael Baum 		sh->dev_cap.tunnel_en = dv_attr.tunnel_offloads_caps &
30587af0d1eSMichael Baum 					(MLX5_TUNNELED_OFFLOADS_VXLAN_CAP |
30687af0d1eSMichael Baum 					 MLX5_TUNNELED_OFFLOADS_GRE_CAP |
30787af0d1eSMichael Baum 					 MLX5_TUNNELED_OFFLOADS_GENEVE_CAP);
30887af0d1eSMichael Baum 	}
30987af0d1eSMichael Baum 	if (sh->dev_cap.tunnel_en) {
31087af0d1eSMichael Baum 		DRV_LOG(DEBUG, "Tunnel offloading is supported for %s%s%s",
31187af0d1eSMichael Baum 			sh->dev_cap.tunnel_en &
31287af0d1eSMichael Baum 			MLX5_TUNNELED_OFFLOADS_VXLAN_CAP ? "[VXLAN]" : "",
31387af0d1eSMichael Baum 			sh->dev_cap.tunnel_en &
31487af0d1eSMichael Baum 			MLX5_TUNNELED_OFFLOADS_GRE_CAP ? "[GRE]" : "",
31587af0d1eSMichael Baum 			sh->dev_cap.tunnel_en &
31687af0d1eSMichael Baum 			MLX5_TUNNELED_OFFLOADS_GENEVE_CAP ? "[GENEVE]" : "");
31787af0d1eSMichael Baum 	} else {
31887af0d1eSMichael Baum 		DRV_LOG(DEBUG, "Tunnel offloading is not supported.");
31987af0d1eSMichael Baum 	}
32087af0d1eSMichael Baum #else
32187af0d1eSMichael Baum 	DRV_LOG(WARNING,
32287af0d1eSMichael Baum 		"Tunnel offloading disabled due to old OFED/rdma-core version");
323e85f623eSOphir Munk #endif
32487af0d1eSMichael Baum 	if (!sh->cdev->config.devx)
32587af0d1eSMichael Baum 		return 0;
32687af0d1eSMichael Baum 	/* Check capabilities for Packet Pacing. */
32787af0d1eSMichael Baum 	DRV_LOG(DEBUG, "Timestamp counter frequency %u kHz.",
32887af0d1eSMichael Baum 		hca_attr->dev_freq_khz);
32987af0d1eSMichael Baum 	DRV_LOG(DEBUG, "Packet pacing is %ssupported.",
33087af0d1eSMichael Baum 		hca_attr->qos.packet_pacing ? "" : "not ");
33187af0d1eSMichael Baum 	DRV_LOG(DEBUG, "Cross channel ops are %ssupported.",
33287af0d1eSMichael Baum 		hca_attr->cross_channel ? "" : "not ");
33387af0d1eSMichael Baum 	DRV_LOG(DEBUG, "WQE index ignore is %ssupported.",
33487af0d1eSMichael Baum 		hca_attr->wqe_index_ignore ? "" : "not ");
33587af0d1eSMichael Baum 	DRV_LOG(DEBUG, "Non-wire SQ feature is %ssupported.",
33687af0d1eSMichael Baum 		hca_attr->non_wire_sq ? "" : "not ");
33787af0d1eSMichael Baum 	DRV_LOG(DEBUG, "Static WQE SQ feature is %ssupported (%d)",
33887af0d1eSMichael Baum 		hca_attr->log_max_static_sq_wq ? "" : "not ",
33987af0d1eSMichael Baum 		hca_attr->log_max_static_sq_wq);
34087af0d1eSMichael Baum 	DRV_LOG(DEBUG, "WQE rate PP mode is %ssupported.",
34187af0d1eSMichael Baum 		hca_attr->qos.wqe_rate_pp ? "" : "not ");
34287af0d1eSMichael Baum 	sh->dev_cap.txpp_en = hca_attr->qos.packet_pacing;
34387af0d1eSMichael Baum 	if (!hca_attr->cross_channel) {
34487af0d1eSMichael Baum 		DRV_LOG(DEBUG,
34587af0d1eSMichael Baum 			"Cross channel operations are required for packet pacing.");
34687af0d1eSMichael Baum 		sh->dev_cap.txpp_en = 0;
34787af0d1eSMichael Baum 	}
34887af0d1eSMichael Baum 	if (!hca_attr->wqe_index_ignore) {
34987af0d1eSMichael Baum 		DRV_LOG(DEBUG,
35087af0d1eSMichael Baum 			"WQE index ignore feature is required for packet pacing.");
35187af0d1eSMichael Baum 		sh->dev_cap.txpp_en = 0;
35287af0d1eSMichael Baum 	}
35387af0d1eSMichael Baum 	if (!hca_attr->non_wire_sq) {
35487af0d1eSMichael Baum 		DRV_LOG(DEBUG,
35587af0d1eSMichael Baum 			"Non-wire SQ feature is required for packet pacing.");
35687af0d1eSMichael Baum 		sh->dev_cap.txpp_en = 0;
35787af0d1eSMichael Baum 	}
35887af0d1eSMichael Baum 	if (!hca_attr->log_max_static_sq_wq) {
35987af0d1eSMichael Baum 		DRV_LOG(DEBUG,
36087af0d1eSMichael Baum 			"Static WQE SQ feature is required for packet pacing.");
36187af0d1eSMichael Baum 		sh->dev_cap.txpp_en = 0;
36287af0d1eSMichael Baum 	}
36387af0d1eSMichael Baum 	if (!hca_attr->qos.wqe_rate_pp) {
36487af0d1eSMichael Baum 		DRV_LOG(DEBUG,
36587af0d1eSMichael Baum 			"WQE rate mode is required for packet pacing.");
36687af0d1eSMichael Baum 		sh->dev_cap.txpp_en = 0;
36787af0d1eSMichael Baum 	}
36887af0d1eSMichael Baum #ifndef HAVE_MLX5DV_DEVX_UAR_OFFSET
36987af0d1eSMichael Baum 	DRV_LOG(DEBUG,
37087af0d1eSMichael Baum 		"DevX does not provide UAR offset, can't create queues for packet pacing.");
37187af0d1eSMichael Baum 	sh->dev_cap.txpp_en = 0;
37287af0d1eSMichael Baum #endif
37387af0d1eSMichael Baum 	sh->dev_cap.scatter_fcs_w_decap_disable =
37487af0d1eSMichael Baum 					hca_attr->scatter_fcs_w_decap_disable;
37587af0d1eSMichael Baum 	sh->dev_cap.rq_delay_drop_en = hca_attr->rq_delay_drop;
37687af0d1eSMichael Baum 	mlx5_rt_timestamp_config(sh, hca_attr);
3776be4c57aSMichael Baum 	return 0;
378e85f623eSOphir Munk }
3792eb4d010SOphir Munk 
3802eb4d010SOphir Munk /**
381630a587bSRongwei Liu  * Detect misc5 support or not
382630a587bSRongwei Liu  *
383630a587bSRongwei Liu  * @param[in] priv
384630a587bSRongwei Liu  *   Device private data pointer
385630a587bSRongwei Liu  */
386630a587bSRongwei Liu #ifdef HAVE_MLX5DV_DR
387630a587bSRongwei Liu static void
388630a587bSRongwei Liu __mlx5_discovery_misc5_cap(struct mlx5_priv *priv)
389630a587bSRongwei Liu {
390630a587bSRongwei Liu #ifdef HAVE_IBV_FLOW_DV_SUPPORT
391630a587bSRongwei Liu 	/* Dummy VxLAN matcher to detect rdma-core misc5 cap
392630a587bSRongwei Liu 	 * Case: IPv4--->UDP--->VxLAN--->vni
393630a587bSRongwei Liu 	 */
394630a587bSRongwei Liu 	void *tbl;
395630a587bSRongwei Liu 	struct mlx5_flow_dv_match_params matcher_mask;
396630a587bSRongwei Liu 	void *match_m;
397630a587bSRongwei Liu 	void *matcher;
398630a587bSRongwei Liu 	void *headers_m;
399630a587bSRongwei Liu 	void *misc5_m;
400630a587bSRongwei Liu 	uint32_t *tunnel_header_m;
401630a587bSRongwei Liu 	struct mlx5dv_flow_matcher_attr dv_attr;
402630a587bSRongwei Liu 
403630a587bSRongwei Liu 	memset(&matcher_mask, 0, sizeof(matcher_mask));
404630a587bSRongwei Liu 	matcher_mask.size = sizeof(matcher_mask.buf);
405630a587bSRongwei Liu 	match_m = matcher_mask.buf;
406630a587bSRongwei Liu 	headers_m = MLX5_ADDR_OF(fte_match_param, match_m, outer_headers);
407630a587bSRongwei Liu 	misc5_m = MLX5_ADDR_OF(fte_match_param,
408630a587bSRongwei Liu 			       match_m, misc_parameters_5);
409630a587bSRongwei Liu 	tunnel_header_m = (uint32_t *)
410630a587bSRongwei Liu 				MLX5_ADDR_OF(fte_match_set_misc5,
411630a587bSRongwei Liu 				misc5_m, tunnel_header_1);
412630a587bSRongwei Liu 	MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
413630a587bSRongwei Liu 	MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 4);
414630a587bSRongwei Liu 	MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xffff);
415630a587bSRongwei Liu 	*tunnel_header_m = 0xffffff;
416630a587bSRongwei Liu 
417630a587bSRongwei Liu 	tbl = mlx5_glue->dr_create_flow_tbl(priv->sh->rx_domain, 1);
418630a587bSRongwei Liu 	if (!tbl) {
419630a587bSRongwei Liu 		DRV_LOG(INFO, "No SW steering support");
420630a587bSRongwei Liu 		return;
421630a587bSRongwei Liu 	}
422630a587bSRongwei Liu 	dv_attr.type = IBV_FLOW_ATTR_NORMAL,
423630a587bSRongwei Liu 	dv_attr.match_mask = (void *)&matcher_mask,
424630a587bSRongwei Liu 	dv_attr.match_criteria_enable =
425630a587bSRongwei Liu 			(1 << MLX5_MATCH_CRITERIA_ENABLE_OUTER_BIT) |
426630a587bSRongwei Liu 			(1 << MLX5_MATCH_CRITERIA_ENABLE_MISC5_BIT);
427630a587bSRongwei Liu 	dv_attr.priority = 3;
428630a587bSRongwei Liu #ifdef HAVE_MLX5DV_DR_ESWITCH
429630a587bSRongwei Liu 	void *misc2_m;
430a13ec19cSMichael Baum 	if (priv->sh->config.dv_esw_en) {
431630a587bSRongwei Liu 		/* FDB enabled reg_c_0 */
432630a587bSRongwei Liu 		dv_attr.match_criteria_enable |=
433630a587bSRongwei Liu 				(1 << MLX5_MATCH_CRITERIA_ENABLE_MISC2_BIT);
434630a587bSRongwei Liu 		misc2_m = MLX5_ADDR_OF(fte_match_param,
435630a587bSRongwei Liu 				       match_m, misc_parameters_2);
436630a587bSRongwei Liu 		MLX5_SET(fte_match_set_misc2, misc2_m,
437630a587bSRongwei Liu 			 metadata_reg_c_0, 0xffff);
438630a587bSRongwei Liu 	}
439630a587bSRongwei Liu #endif
440ca1418ceSMichael Baum 	matcher = mlx5_glue->dv_create_flow_matcher(priv->sh->cdev->ctx,
441630a587bSRongwei Liu 						    &dv_attr, tbl);
442630a587bSRongwei Liu 	if (matcher) {
443630a587bSRongwei Liu 		priv->sh->misc5_cap = 1;
444630a587bSRongwei Liu 		mlx5_glue->dv_destroy_flow_matcher(matcher);
445630a587bSRongwei Liu 	}
446630a587bSRongwei Liu 	mlx5_glue->dr_destroy_flow_tbl(tbl);
447630a587bSRongwei Liu #else
448630a587bSRongwei Liu 	RTE_SET_USED(priv);
449630a587bSRongwei Liu #endif
450630a587bSRongwei Liu }
451630a587bSRongwei Liu #endif
452630a587bSRongwei Liu 
453630a587bSRongwei Liu /**
4542eb4d010SOphir Munk  * Initialize DR related data within private structure.
4552eb4d010SOphir Munk  * Routine checks the reference counter and does actual
4562eb4d010SOphir Munk  * resources creation/initialization only if counter is zero.
4572eb4d010SOphir Munk  *
4582eb4d010SOphir Munk  * @param[in] priv
4592eb4d010SOphir Munk  *   Pointer to the private device data structure.
4602eb4d010SOphir Munk  *
4612eb4d010SOphir Munk  * @return
4622eb4d010SOphir Munk  *   Zero on success, positive error code otherwise.
4632eb4d010SOphir Munk  */
4642eb4d010SOphir Munk static int
4652eb4d010SOphir Munk mlx5_alloc_shared_dr(struct mlx5_priv *priv)
4662eb4d010SOphir Munk {
4672eb4d010SOphir Munk 	struct mlx5_dev_ctx_shared *sh = priv->sh;
468961b6774SMatan Azrad 	char s[MLX5_NAME_SIZE] __rte_unused;
46916dbba25SXueming Li 	int err;
4702eb4d010SOphir Munk 
47116dbba25SXueming Li 	MLX5_ASSERT(sh && sh->refcnt);
47216dbba25SXueming Li 	if (sh->refcnt > 1)
47316dbba25SXueming Li 		return 0;
4742eb4d010SOphir Munk 	err = mlx5_alloc_table_hash_list(priv);
4752eb4d010SOphir Munk 	if (err)
476291140c6SSuanming Mou 		goto error;
477d84c3cf7SSuanming Mou 	if (priv->sh->config.dv_flow_en == 2)
478d84c3cf7SSuanming Mou 		return 0;
479291140c6SSuanming Mou 	/* The resources below are only valid with DV support. */
480291140c6SSuanming Mou #ifdef HAVE_IBV_FLOW_DV_SUPPORT
481491b7137SMatan Azrad 	/* Init port id action list. */
482e78e5408SMatan Azrad 	snprintf(s, sizeof(s), "%s_port_id_action_list", sh->ibdev_name);
483d03b7860SSuanming Mou 	sh->port_id_action_list = mlx5_list_create(s, sh, true,
4840fd5f82aSXueming Li 						   flow_dv_port_id_create_cb,
4850fd5f82aSXueming Li 						   flow_dv_port_id_match_cb,
486491b7137SMatan Azrad 						   flow_dv_port_id_remove_cb,
487491b7137SMatan Azrad 						   flow_dv_port_id_clone_cb,
488491b7137SMatan Azrad 						 flow_dv_port_id_clone_free_cb);
489679f46c7SMatan Azrad 	if (!sh->port_id_action_list)
490679f46c7SMatan Azrad 		goto error;
491491b7137SMatan Azrad 	/* Init push vlan action list. */
492e78e5408SMatan Azrad 	snprintf(s, sizeof(s), "%s_push_vlan_action_list", sh->ibdev_name);
493d03b7860SSuanming Mou 	sh->push_vlan_action_list = mlx5_list_create(s, sh, true,
4943422af2aSXueming Li 						    flow_dv_push_vlan_create_cb,
4953422af2aSXueming Li 						    flow_dv_push_vlan_match_cb,
496491b7137SMatan Azrad 						    flow_dv_push_vlan_remove_cb,
497491b7137SMatan Azrad 						    flow_dv_push_vlan_clone_cb,
498491b7137SMatan Azrad 					       flow_dv_push_vlan_clone_free_cb);
499679f46c7SMatan Azrad 	if (!sh->push_vlan_action_list)
500679f46c7SMatan Azrad 		goto error;
501491b7137SMatan Azrad 	/* Init sample action list. */
502e78e5408SMatan Azrad 	snprintf(s, sizeof(s), "%s_sample_action_list", sh->ibdev_name);
503d03b7860SSuanming Mou 	sh->sample_action_list = mlx5_list_create(s, sh, true,
50419784141SSuanming Mou 						  flow_dv_sample_create_cb,
50519784141SSuanming Mou 						  flow_dv_sample_match_cb,
506491b7137SMatan Azrad 						  flow_dv_sample_remove_cb,
507491b7137SMatan Azrad 						  flow_dv_sample_clone_cb,
508491b7137SMatan Azrad 						  flow_dv_sample_clone_free_cb);
509679f46c7SMatan Azrad 	if (!sh->sample_action_list)
510679f46c7SMatan Azrad 		goto error;
511491b7137SMatan Azrad 	/* Init dest array action list. */
512e78e5408SMatan Azrad 	snprintf(s, sizeof(s), "%s_dest_array_list", sh->ibdev_name);
513d03b7860SSuanming Mou 	sh->dest_array_list = mlx5_list_create(s, sh, true,
51419784141SSuanming Mou 					       flow_dv_dest_array_create_cb,
51519784141SSuanming Mou 					       flow_dv_dest_array_match_cb,
516491b7137SMatan Azrad 					       flow_dv_dest_array_remove_cb,
517491b7137SMatan Azrad 					       flow_dv_dest_array_clone_cb,
518491b7137SMatan Azrad 					      flow_dv_dest_array_clone_free_cb);
519679f46c7SMatan Azrad 	if (!sh->dest_array_list)
520679f46c7SMatan Azrad 		goto error;
5219086ac09SGregory Etelson 	/* Init shared flex parsers list, no need lcore_share */
5229086ac09SGregory Etelson 	snprintf(s, sizeof(s), "%s_flex_parsers_list", sh->ibdev_name);
5239086ac09SGregory Etelson 	sh->flex_parsers_dv = mlx5_list_create(s, sh, false,
5249086ac09SGregory Etelson 					       mlx5_flex_parser_create_cb,
5259086ac09SGregory Etelson 					       mlx5_flex_parser_match_cb,
5269086ac09SGregory Etelson 					       mlx5_flex_parser_remove_cb,
5279086ac09SGregory Etelson 					       mlx5_flex_parser_clone_cb,
5289086ac09SGregory Etelson 					       mlx5_flex_parser_clone_free_cb);
5299086ac09SGregory Etelson 	if (!sh->flex_parsers_dv)
5309086ac09SGregory Etelson 		goto error;
531291140c6SSuanming Mou #endif
5322eb4d010SOphir Munk #ifdef HAVE_MLX5DV_DR
5332eb4d010SOphir Munk 	void *domain;
5342eb4d010SOphir Munk 
5352eb4d010SOphir Munk 	/* Reference counter is zero, we should initialize structures. */
536ca1418ceSMichael Baum 	domain = mlx5_glue->dr_create_domain(sh->cdev->ctx,
5372eb4d010SOphir Munk 					     MLX5DV_DR_DOMAIN_TYPE_NIC_RX);
5382eb4d010SOphir Munk 	if (!domain) {
5392eb4d010SOphir Munk 		DRV_LOG(ERR, "ingress mlx5dv_dr_create_domain failed");
5402eb4d010SOphir Munk 		err = errno;
5412eb4d010SOphir Munk 		goto error;
5422eb4d010SOphir Munk 	}
5432eb4d010SOphir Munk 	sh->rx_domain = domain;
544ca1418ceSMichael Baum 	domain = mlx5_glue->dr_create_domain(sh->cdev->ctx,
5452eb4d010SOphir Munk 					     MLX5DV_DR_DOMAIN_TYPE_NIC_TX);
5462eb4d010SOphir Munk 	if (!domain) {
5472eb4d010SOphir Munk 		DRV_LOG(ERR, "egress mlx5dv_dr_create_domain failed");
5482eb4d010SOphir Munk 		err = errno;
5492eb4d010SOphir Munk 		goto error;
5502eb4d010SOphir Munk 	}
5512eb4d010SOphir Munk 	sh->tx_domain = domain;
5522eb4d010SOphir Munk #ifdef HAVE_MLX5DV_DR_ESWITCH
553a13ec19cSMichael Baum 	if (sh->config.dv_esw_en) {
554ca1418ceSMichael Baum 		domain = mlx5_glue->dr_create_domain(sh->cdev->ctx,
555ca1418ceSMichael Baum 						     MLX5DV_DR_DOMAIN_TYPE_FDB);
5562eb4d010SOphir Munk 		if (!domain) {
5572eb4d010SOphir Munk 			DRV_LOG(ERR, "FDB mlx5dv_dr_create_domain failed");
5582eb4d010SOphir Munk 			err = errno;
5592eb4d010SOphir Munk 			goto error;
5602eb4d010SOphir Munk 		}
5612eb4d010SOphir Munk 		sh->fdb_domain = domain;
562da845ae9SViacheslav Ovsiienko 	}
563da845ae9SViacheslav Ovsiienko 	/*
564da845ae9SViacheslav Ovsiienko 	 * The drop action is just some dummy placeholder in rdma-core. It
565da845ae9SViacheslav Ovsiienko 	 * does not belong to domains and has no any attributes, and, can be
566da845ae9SViacheslav Ovsiienko 	 * shared by the entire device.
567da845ae9SViacheslav Ovsiienko 	 */
568da845ae9SViacheslav Ovsiienko 	sh->dr_drop_action = mlx5_glue->dr_create_flow_action_drop();
569da845ae9SViacheslav Ovsiienko 	if (!sh->dr_drop_action) {
570da845ae9SViacheslav Ovsiienko 		DRV_LOG(ERR, "FDB mlx5dv_dr_create_flow_action_drop");
571da845ae9SViacheslav Ovsiienko 		err = errno;
572da845ae9SViacheslav Ovsiienko 		goto error;
5732eb4d010SOphir Munk 	}
5742eb4d010SOphir Munk #endif
575a13ec19cSMichael Baum 	if (!sh->tunnel_hub && sh->config.dv_miss_info)
5764ec6360dSGregory Etelson 		err = mlx5_alloc_tunnel_hub(sh);
5774ec6360dSGregory Etelson 	if (err) {
5784ec6360dSGregory Etelson 		DRV_LOG(ERR, "mlx5_alloc_tunnel_hub failed err=%d", err);
5794ec6360dSGregory Etelson 		goto error;
5804ec6360dSGregory Etelson 	}
581a13ec19cSMichael Baum 	if (sh->config.reclaim_mode == MLX5_RCM_AGGR) {
5822eb4d010SOphir Munk 		mlx5_glue->dr_reclaim_domain_memory(sh->rx_domain, 1);
5832eb4d010SOphir Munk 		mlx5_glue->dr_reclaim_domain_memory(sh->tx_domain, 1);
5842eb4d010SOphir Munk 		if (sh->fdb_domain)
5852eb4d010SOphir Munk 			mlx5_glue->dr_reclaim_domain_memory(sh->fdb_domain, 1);
5862eb4d010SOphir Munk 	}
5872eb4d010SOphir Munk 	sh->pop_vlan_action = mlx5_glue->dr_create_flow_action_pop_vlan();
588a13ec19cSMichael Baum 	if (!sh->config.allow_duplicate_pattern) {
589e39226bdSJiawei Wang #ifndef HAVE_MLX5_DR_ALLOW_DUPLICATE
590e39226bdSJiawei Wang 		DRV_LOG(WARNING, "Disallow duplicate pattern is not supported - maybe old rdma-core version?");
591e39226bdSJiawei Wang #endif
592e39226bdSJiawei Wang 		mlx5_glue->dr_allow_duplicate_rules(sh->rx_domain, 0);
593e39226bdSJiawei Wang 		mlx5_glue->dr_allow_duplicate_rules(sh->tx_domain, 0);
594e39226bdSJiawei Wang 		if (sh->fdb_domain)
595e39226bdSJiawei Wang 			mlx5_glue->dr_allow_duplicate_rules(sh->fdb_domain, 0);
596e39226bdSJiawei Wang 	}
597630a587bSRongwei Liu 
598630a587bSRongwei Liu 	__mlx5_discovery_misc5_cap(priv);
5992eb4d010SOphir Munk #endif /* HAVE_MLX5DV_DR */
600b80726dcSSuanming Mou 	sh->default_miss_action =
601b80726dcSSuanming Mou 			mlx5_glue->dr_create_flow_action_default_miss();
602b80726dcSSuanming Mou 	if (!sh->default_miss_action)
603b80726dcSSuanming Mou 		DRV_LOG(WARNING, "Default miss action is not supported.");
60409c25553SXueming Li 	LIST_INIT(&sh->shared_rxqs);
6052eb4d010SOphir Munk 	return 0;
6062eb4d010SOphir Munk error:
6072eb4d010SOphir Munk 	/* Rollback the created objects. */
6082eb4d010SOphir Munk 	if (sh->rx_domain) {
6092eb4d010SOphir Munk 		mlx5_glue->dr_destroy_domain(sh->rx_domain);
6102eb4d010SOphir Munk 		sh->rx_domain = NULL;
6112eb4d010SOphir Munk 	}
6122eb4d010SOphir Munk 	if (sh->tx_domain) {
6132eb4d010SOphir Munk 		mlx5_glue->dr_destroy_domain(sh->tx_domain);
6142eb4d010SOphir Munk 		sh->tx_domain = NULL;
6152eb4d010SOphir Munk 	}
6162eb4d010SOphir Munk 	if (sh->fdb_domain) {
6172eb4d010SOphir Munk 		mlx5_glue->dr_destroy_domain(sh->fdb_domain);
6182eb4d010SOphir Munk 		sh->fdb_domain = NULL;
6192eb4d010SOphir Munk 	}
620da845ae9SViacheslav Ovsiienko 	if (sh->dr_drop_action) {
621da845ae9SViacheslav Ovsiienko 		mlx5_glue->destroy_flow_action(sh->dr_drop_action);
622da845ae9SViacheslav Ovsiienko 		sh->dr_drop_action = NULL;
6232eb4d010SOphir Munk 	}
6242eb4d010SOphir Munk 	if (sh->pop_vlan_action) {
6252eb4d010SOphir Munk 		mlx5_glue->destroy_flow_action(sh->pop_vlan_action);
6262eb4d010SOphir Munk 		sh->pop_vlan_action = NULL;
6272eb4d010SOphir Munk 	}
628bf615b07SSuanming Mou 	if (sh->encaps_decaps) {
629e69a5922SXueming Li 		mlx5_hlist_destroy(sh->encaps_decaps);
630bf615b07SSuanming Mou 		sh->encaps_decaps = NULL;
631bf615b07SSuanming Mou 	}
6323fe88961SSuanming Mou 	if (sh->modify_cmds) {
633e69a5922SXueming Li 		mlx5_hlist_destroy(sh->modify_cmds);
6343fe88961SSuanming Mou 		sh->modify_cmds = NULL;
6353fe88961SSuanming Mou 	}
6362eb4d010SOphir Munk 	if (sh->tag_table) {
6372eb4d010SOphir Munk 		/* tags should be destroyed with flow before. */
638e69a5922SXueming Li 		mlx5_hlist_destroy(sh->tag_table);
6392eb4d010SOphir Munk 		sh->tag_table = NULL;
6402eb4d010SOphir Munk 	}
6414ec6360dSGregory Etelson 	if (sh->tunnel_hub) {
6424ec6360dSGregory Etelson 		mlx5_release_tunnel_hub(sh, priv->dev_port);
6434ec6360dSGregory Etelson 		sh->tunnel_hub = NULL;
6444ec6360dSGregory Etelson 	}
6452eb4d010SOphir Munk 	mlx5_free_table_hash_list(priv);
646679f46c7SMatan Azrad 	if (sh->port_id_action_list) {
647679f46c7SMatan Azrad 		mlx5_list_destroy(sh->port_id_action_list);
648679f46c7SMatan Azrad 		sh->port_id_action_list = NULL;
649679f46c7SMatan Azrad 	}
650679f46c7SMatan Azrad 	if (sh->push_vlan_action_list) {
651679f46c7SMatan Azrad 		mlx5_list_destroy(sh->push_vlan_action_list);
652679f46c7SMatan Azrad 		sh->push_vlan_action_list = NULL;
653679f46c7SMatan Azrad 	}
654679f46c7SMatan Azrad 	if (sh->sample_action_list) {
655679f46c7SMatan Azrad 		mlx5_list_destroy(sh->sample_action_list);
656679f46c7SMatan Azrad 		sh->sample_action_list = NULL;
657679f46c7SMatan Azrad 	}
658679f46c7SMatan Azrad 	if (sh->dest_array_list) {
659679f46c7SMatan Azrad 		mlx5_list_destroy(sh->dest_array_list);
660679f46c7SMatan Azrad 		sh->dest_array_list = NULL;
661679f46c7SMatan Azrad 	}
6622eb4d010SOphir Munk 	return err;
6632eb4d010SOphir Munk }
6642eb4d010SOphir Munk 
6652eb4d010SOphir Munk /**
6662eb4d010SOphir Munk  * Destroy DR related data within private structure.
6672eb4d010SOphir Munk  *
6682eb4d010SOphir Munk  * @param[in] priv
6692eb4d010SOphir Munk  *   Pointer to the private device data structure.
6702eb4d010SOphir Munk  */
6712eb4d010SOphir Munk void
6722eb4d010SOphir Munk mlx5_os_free_shared_dr(struct mlx5_priv *priv)
6732eb4d010SOphir Munk {
67416dbba25SXueming Li 	struct mlx5_dev_ctx_shared *sh = priv->sh;
6752eb4d010SOphir Munk 
67616dbba25SXueming Li 	MLX5_ASSERT(sh && sh->refcnt);
67716dbba25SXueming Li 	if (sh->refcnt > 1)
6782eb4d010SOphir Munk 		return;
67909c25553SXueming Li 	MLX5_ASSERT(LIST_EMPTY(&sh->shared_rxqs));
6802eb4d010SOphir Munk #ifdef HAVE_MLX5DV_DR
6812eb4d010SOphir Munk 	if (sh->rx_domain) {
6822eb4d010SOphir Munk 		mlx5_glue->dr_destroy_domain(sh->rx_domain);
6832eb4d010SOphir Munk 		sh->rx_domain = NULL;
6842eb4d010SOphir Munk 	}
6852eb4d010SOphir Munk 	if (sh->tx_domain) {
6862eb4d010SOphir Munk 		mlx5_glue->dr_destroy_domain(sh->tx_domain);
6872eb4d010SOphir Munk 		sh->tx_domain = NULL;
6882eb4d010SOphir Munk 	}
6892eb4d010SOphir Munk #ifdef HAVE_MLX5DV_DR_ESWITCH
6902eb4d010SOphir Munk 	if (sh->fdb_domain) {
6912eb4d010SOphir Munk 		mlx5_glue->dr_destroy_domain(sh->fdb_domain);
6922eb4d010SOphir Munk 		sh->fdb_domain = NULL;
6932eb4d010SOphir Munk 	}
694da845ae9SViacheslav Ovsiienko 	if (sh->dr_drop_action) {
695da845ae9SViacheslav Ovsiienko 		mlx5_glue->destroy_flow_action(sh->dr_drop_action);
696da845ae9SViacheslav Ovsiienko 		sh->dr_drop_action = NULL;
6972eb4d010SOphir Munk 	}
6982eb4d010SOphir Munk #endif
6992eb4d010SOphir Munk 	if (sh->pop_vlan_action) {
7002eb4d010SOphir Munk 		mlx5_glue->destroy_flow_action(sh->pop_vlan_action);
7012eb4d010SOphir Munk 		sh->pop_vlan_action = NULL;
7022eb4d010SOphir Munk 	}
703f31a141eSMichael Savisko 	if (sh->send_to_kernel_action.action) {
704f31a141eSMichael Savisko 		void *action = sh->send_to_kernel_action.action;
705f31a141eSMichael Savisko 
706f31a141eSMichael Savisko 		mlx5_glue->destroy_flow_action(action);
707f31a141eSMichael Savisko 		sh->send_to_kernel_action.action = NULL;
708f31a141eSMichael Savisko 	}
709f31a141eSMichael Savisko 	if (sh->send_to_kernel_action.tbl) {
710f31a141eSMichael Savisko 		struct mlx5_flow_tbl_resource *tbl =
711f31a141eSMichael Savisko 				sh->send_to_kernel_action.tbl;
712f31a141eSMichael Savisko 
713f31a141eSMichael Savisko 		flow_dv_tbl_resource_release(sh, tbl);
714f31a141eSMichael Savisko 		sh->send_to_kernel_action.tbl = NULL;
715f31a141eSMichael Savisko 	}
7162eb4d010SOphir Munk #endif /* HAVE_MLX5DV_DR */
717b80726dcSSuanming Mou 	if (sh->default_miss_action)
718b80726dcSSuanming Mou 		mlx5_glue->destroy_flow_action
719b80726dcSSuanming Mou 				(sh->default_miss_action);
720bf615b07SSuanming Mou 	if (sh->encaps_decaps) {
721e69a5922SXueming Li 		mlx5_hlist_destroy(sh->encaps_decaps);
722bf615b07SSuanming Mou 		sh->encaps_decaps = NULL;
723bf615b07SSuanming Mou 	}
7243fe88961SSuanming Mou 	if (sh->modify_cmds) {
725e69a5922SXueming Li 		mlx5_hlist_destroy(sh->modify_cmds);
7263fe88961SSuanming Mou 		sh->modify_cmds = NULL;
7273fe88961SSuanming Mou 	}
7282eb4d010SOphir Munk 	if (sh->tag_table) {
7292eb4d010SOphir Munk 		/* tags should be destroyed with flow before. */
730e69a5922SXueming Li 		mlx5_hlist_destroy(sh->tag_table);
7312eb4d010SOphir Munk 		sh->tag_table = NULL;
7322eb4d010SOphir Munk 	}
7334ec6360dSGregory Etelson 	if (sh->tunnel_hub) {
7344ec6360dSGregory Etelson 		mlx5_release_tunnel_hub(sh, priv->dev_port);
7354ec6360dSGregory Etelson 		sh->tunnel_hub = NULL;
7364ec6360dSGregory Etelson 	}
7372eb4d010SOphir Munk 	mlx5_free_table_hash_list(priv);
738679f46c7SMatan Azrad 	if (sh->port_id_action_list) {
739679f46c7SMatan Azrad 		mlx5_list_destroy(sh->port_id_action_list);
740679f46c7SMatan Azrad 		sh->port_id_action_list = NULL;
741679f46c7SMatan Azrad 	}
742679f46c7SMatan Azrad 	if (sh->push_vlan_action_list) {
743679f46c7SMatan Azrad 		mlx5_list_destroy(sh->push_vlan_action_list);
744679f46c7SMatan Azrad 		sh->push_vlan_action_list = NULL;
745679f46c7SMatan Azrad 	}
746679f46c7SMatan Azrad 	if (sh->sample_action_list) {
747679f46c7SMatan Azrad 		mlx5_list_destroy(sh->sample_action_list);
748679f46c7SMatan Azrad 		sh->sample_action_list = NULL;
749679f46c7SMatan Azrad 	}
750679f46c7SMatan Azrad 	if (sh->dest_array_list) {
751679f46c7SMatan Azrad 		mlx5_list_destroy(sh->dest_array_list);
752679f46c7SMatan Azrad 		sh->dest_array_list = NULL;
753679f46c7SMatan Azrad 	}
7542eb4d010SOphir Munk }
7552eb4d010SOphir Munk 
7562eb4d010SOphir Munk /**
7572e86c4e5SOphir Munk  * Initialize shared data between primary and secondary process.
7582e86c4e5SOphir Munk  *
7592e86c4e5SOphir Munk  * A memzone is reserved by primary process and secondary processes attach to
7602e86c4e5SOphir Munk  * the memzone.
7612e86c4e5SOphir Munk  *
7622e86c4e5SOphir Munk  * @return
7632e86c4e5SOphir Munk  *   0 on success, a negative errno value otherwise and rte_errno is set.
7642e86c4e5SOphir Munk  */
7652e86c4e5SOphir Munk static int
7662e86c4e5SOphir Munk mlx5_init_shared_data(void)
7672e86c4e5SOphir Munk {
7682e86c4e5SOphir Munk 	const struct rte_memzone *mz;
7692e86c4e5SOphir Munk 	int ret = 0;
7702e86c4e5SOphir Munk 
7712e86c4e5SOphir Munk 	rte_spinlock_lock(&mlx5_shared_data_lock);
7722e86c4e5SOphir Munk 	if (mlx5_shared_data == NULL) {
7732e86c4e5SOphir Munk 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
7742e86c4e5SOphir Munk 			/* Allocate shared memory. */
7752e86c4e5SOphir Munk 			mz = rte_memzone_reserve(MZ_MLX5_PMD_SHARED_DATA,
7762e86c4e5SOphir Munk 						 sizeof(*mlx5_shared_data),
7772e86c4e5SOphir Munk 						 SOCKET_ID_ANY, 0);
7782e86c4e5SOphir Munk 			if (mz == NULL) {
7792e86c4e5SOphir Munk 				DRV_LOG(ERR,
7802e86c4e5SOphir Munk 					"Cannot allocate mlx5 shared data");
7812e86c4e5SOphir Munk 				ret = -rte_errno;
7822e86c4e5SOphir Munk 				goto error;
7832e86c4e5SOphir Munk 			}
7842e86c4e5SOphir Munk 			mlx5_shared_data = mz->addr;
7852e86c4e5SOphir Munk 			memset(mlx5_shared_data, 0, sizeof(*mlx5_shared_data));
7862e86c4e5SOphir Munk 			rte_spinlock_init(&mlx5_shared_data->lock);
7872e86c4e5SOphir Munk 		} else {
7882e86c4e5SOphir Munk 			/* Lookup allocated shared memory. */
7892e86c4e5SOphir Munk 			mz = rte_memzone_lookup(MZ_MLX5_PMD_SHARED_DATA);
7902e86c4e5SOphir Munk 			if (mz == NULL) {
7912e86c4e5SOphir Munk 				DRV_LOG(ERR,
7922e86c4e5SOphir Munk 					"Cannot attach mlx5 shared data");
7932e86c4e5SOphir Munk 				ret = -rte_errno;
7942e86c4e5SOphir Munk 				goto error;
7952e86c4e5SOphir Munk 			}
7962e86c4e5SOphir Munk 			mlx5_shared_data = mz->addr;
7972e86c4e5SOphir Munk 			memset(&mlx5_local_data, 0, sizeof(mlx5_local_data));
7982e86c4e5SOphir Munk 		}
7992e86c4e5SOphir Munk 	}
8002e86c4e5SOphir Munk error:
8012e86c4e5SOphir Munk 	rte_spinlock_unlock(&mlx5_shared_data_lock);
8022e86c4e5SOphir Munk 	return ret;
8032e86c4e5SOphir Munk }
8042e86c4e5SOphir Munk 
8052e86c4e5SOphir Munk /**
8062e86c4e5SOphir Munk  * PMD global initialization.
8072e86c4e5SOphir Munk  *
8082e86c4e5SOphir Munk  * Independent from individual device, this function initializes global
8092e86c4e5SOphir Munk  * per-PMD data structures distinguishing primary and secondary processes.
8102e86c4e5SOphir Munk  * Hence, each initialization is called once per a process.
8112e86c4e5SOphir Munk  *
8122e86c4e5SOphir Munk  * @return
8132e86c4e5SOphir Munk  *   0 on success, a negative errno value otherwise and rte_errno is set.
8142e86c4e5SOphir Munk  */
8152e86c4e5SOphir Munk static int
8162e86c4e5SOphir Munk mlx5_init_once(void)
8172e86c4e5SOphir Munk {
8182e86c4e5SOphir Munk 	struct mlx5_shared_data *sd;
8192e86c4e5SOphir Munk 	struct mlx5_local_data *ld = &mlx5_local_data;
8202e86c4e5SOphir Munk 	int ret = 0;
8212e86c4e5SOphir Munk 
8222e86c4e5SOphir Munk 	if (mlx5_init_shared_data())
8232e86c4e5SOphir Munk 		return -rte_errno;
8242e86c4e5SOphir Munk 	sd = mlx5_shared_data;
8252e86c4e5SOphir Munk 	MLX5_ASSERT(sd);
8262e86c4e5SOphir Munk 	rte_spinlock_lock(&sd->lock);
8272e86c4e5SOphir Munk 	switch (rte_eal_process_type()) {
8282e86c4e5SOphir Munk 	case RTE_PROC_PRIMARY:
8292e86c4e5SOphir Munk 		if (sd->init_done)
8302e86c4e5SOphir Munk 			break;
8312e86c4e5SOphir Munk 		ret = mlx5_mp_init_primary(MLX5_MP_NAME,
8322e86c4e5SOphir Munk 					   mlx5_mp_os_primary_handle);
8332e86c4e5SOphir Munk 		if (ret)
8342e86c4e5SOphir Munk 			goto out;
8352e86c4e5SOphir Munk 		sd->init_done = true;
8362e86c4e5SOphir Munk 		break;
8372e86c4e5SOphir Munk 	case RTE_PROC_SECONDARY:
8382e86c4e5SOphir Munk 		if (ld->init_done)
8392e86c4e5SOphir Munk 			break;
8402e86c4e5SOphir Munk 		ret = mlx5_mp_init_secondary(MLX5_MP_NAME,
8412e86c4e5SOphir Munk 					     mlx5_mp_os_secondary_handle);
8422e86c4e5SOphir Munk 		if (ret)
8432e86c4e5SOphir Munk 			goto out;
8442e86c4e5SOphir Munk 		++sd->secondary_cnt;
8452e86c4e5SOphir Munk 		ld->init_done = true;
8462e86c4e5SOphir Munk 		break;
8472e86c4e5SOphir Munk 	default:
8482e86c4e5SOphir Munk 		break;
8492e86c4e5SOphir Munk 	}
8502e86c4e5SOphir Munk out:
8512e86c4e5SOphir Munk 	rte_spinlock_unlock(&sd->lock);
8522e86c4e5SOphir Munk 	return ret;
8532e86c4e5SOphir Munk }
8542e86c4e5SOphir Munk 
8552e86c4e5SOphir Munk /**
85645633c46SSuanming Mou  * DR flow drop action support detect.
85745633c46SSuanming Mou  *
85845633c46SSuanming Mou  * @param dev
85945633c46SSuanming Mou  *   Pointer to rte_eth_dev structure.
86045633c46SSuanming Mou  *
86145633c46SSuanming Mou  */
86245633c46SSuanming Mou static void
86345633c46SSuanming Mou mlx5_flow_drop_action_config(struct rte_eth_dev *dev __rte_unused)
86445633c46SSuanming Mou {
86545633c46SSuanming Mou #ifdef HAVE_MLX5DV_DR
86645633c46SSuanming Mou 	struct mlx5_priv *priv = dev->data->dev_private;
86745633c46SSuanming Mou 
868a13ec19cSMichael Baum 	if (!priv->sh->config.dv_flow_en || !priv->sh->dr_drop_action)
86945633c46SSuanming Mou 		return;
87045633c46SSuanming Mou 	/**
87145633c46SSuanming Mou 	 * DR supports drop action placeholder when it is supported;
87245633c46SSuanming Mou 	 * otherwise, use the queue drop action.
87345633c46SSuanming Mou 	 */
8743c4338a4SJiawei Wang 	if (!priv->sh->drop_action_check_flag) {
8753c4338a4SJiawei Wang 		if (!mlx5_flow_discover_dr_action_support(dev))
8763c4338a4SJiawei Wang 			priv->sh->dr_drop_action_en = 1;
8773c4338a4SJiawei Wang 		priv->sh->drop_action_check_flag = 1;
8783c4338a4SJiawei Wang 	}
8793c4338a4SJiawei Wang 	if (priv->sh->dr_drop_action_en)
88045633c46SSuanming Mou 		priv->root_drop_action = priv->sh->dr_drop_action;
8813c4338a4SJiawei Wang 	else
8823c4338a4SJiawei Wang 		priv->root_drop_action = priv->drop_queue.hrxq->action;
88345633c46SSuanming Mou #endif
88445633c46SSuanming Mou }
88545633c46SSuanming Mou 
886e6988afdSMatan Azrad static void
887e6988afdSMatan Azrad mlx5_queue_counter_id_prepare(struct rte_eth_dev *dev)
888e6988afdSMatan Azrad {
889e6988afdSMatan Azrad 	struct mlx5_priv *priv = dev->data->dev_private;
890ca1418ceSMichael Baum 	void *ctx = priv->sh->cdev->ctx;
891e6988afdSMatan Azrad 
892e6988afdSMatan Azrad 	priv->q_counters = mlx5_devx_cmd_queue_counter_alloc(ctx);
893e6988afdSMatan Azrad 	if (!priv->q_counters) {
894e6988afdSMatan Azrad 		struct ibv_cq *cq = mlx5_glue->create_cq(ctx, 1, NULL, NULL, 0);
895e6988afdSMatan Azrad 		struct ibv_wq *wq;
896e6988afdSMatan Azrad 
897e6988afdSMatan Azrad 		DRV_LOG(DEBUG, "Port %d queue counter object cannot be created "
898e6988afdSMatan Azrad 			"by DevX - fall-back to use the kernel driver global "
899e6988afdSMatan Azrad 			"queue counter.", dev->data->port_id);
900e6988afdSMatan Azrad 		/* Create WQ by kernel and query its queue counter ID. */
901e6988afdSMatan Azrad 		if (cq) {
902e6988afdSMatan Azrad 			wq = mlx5_glue->create_wq(ctx,
903e6988afdSMatan Azrad 						  &(struct ibv_wq_init_attr){
904e6988afdSMatan Azrad 						    .wq_type = IBV_WQT_RQ,
905e6988afdSMatan Azrad 						    .max_wr = 1,
906e6988afdSMatan Azrad 						    .max_sge = 1,
907e35ccf24SMichael Baum 						    .pd = priv->sh->cdev->pd,
908e6988afdSMatan Azrad 						    .cq = cq,
909e6988afdSMatan Azrad 						});
910e6988afdSMatan Azrad 			if (wq) {
911e6988afdSMatan Azrad 				/* Counter is assigned only on RDY state. */
912e6988afdSMatan Azrad 				int ret = mlx5_glue->modify_wq(wq,
913e6988afdSMatan Azrad 						 &(struct ibv_wq_attr){
914e6988afdSMatan Azrad 						 .attr_mask = IBV_WQ_ATTR_STATE,
915e6988afdSMatan Azrad 						 .wq_state = IBV_WQS_RDY,
916e6988afdSMatan Azrad 						});
917e6988afdSMatan Azrad 
918e6988afdSMatan Azrad 				if (ret == 0)
919e6988afdSMatan Azrad 					mlx5_devx_cmd_wq_query(wq,
920e6988afdSMatan Azrad 							 &priv->counter_set_id);
921e6988afdSMatan Azrad 				claim_zero(mlx5_glue->destroy_wq(wq));
922e6988afdSMatan Azrad 			}
923e6988afdSMatan Azrad 			claim_zero(mlx5_glue->destroy_cq(cq));
924e6988afdSMatan Azrad 		}
925e6988afdSMatan Azrad 	} else {
926e6988afdSMatan Azrad 		priv->counter_set_id = priv->q_counters->id;
927e6988afdSMatan Azrad 	}
928e6988afdSMatan Azrad 	if (priv->counter_set_id == 0)
929e6988afdSMatan Azrad 		DRV_LOG(INFO, "Part of the port %d statistics will not be "
930e6988afdSMatan Azrad 			"available.", dev->data->port_id);
931e6988afdSMatan Azrad }
932e6988afdSMatan Azrad 
933994829e6SSuanming Mou /**
934f926cce3SXueming Li  * Check if representor spawn info match devargs.
935f926cce3SXueming Li  *
936f926cce3SXueming Li  * @param spawn
937f926cce3SXueming Li  *   Verbs device parameters (name, port, switch_info) to spawn.
938f926cce3SXueming Li  * @param eth_da
939f926cce3SXueming Li  *   Device devargs to probe.
940f926cce3SXueming Li  *
941f926cce3SXueming Li  * @return
942f926cce3SXueming Li  *   Match result.
943f926cce3SXueming Li  */
944f926cce3SXueming Li static bool
945f926cce3SXueming Li mlx5_representor_match(struct mlx5_dev_spawn_data *spawn,
946f926cce3SXueming Li 		       struct rte_eth_devargs *eth_da)
947f926cce3SXueming Li {
948f926cce3SXueming Li 	struct mlx5_switch_info *switch_info = &spawn->info;
949f926cce3SXueming Li 	unsigned int p, f;
950f926cce3SXueming Li 	uint16_t id;
95191766faeSXueming Li 	uint16_t repr_id = mlx5_representor_id_encode(switch_info,
95291766faeSXueming Li 						      eth_da->type);
953f926cce3SXueming Li 
954f926cce3SXueming Li 	switch (eth_da->type) {
955f926cce3SXueming Li 	case RTE_ETH_REPRESENTOR_SF:
95691766faeSXueming Li 		if (!(spawn->info.port_name == -1 &&
95791766faeSXueming Li 		      switch_info->name_type ==
95891766faeSXueming Li 				MLX5_PHYS_PORT_NAME_TYPE_PFHPF) &&
95991766faeSXueming Li 		    switch_info->name_type != MLX5_PHYS_PORT_NAME_TYPE_PFSF) {
960f926cce3SXueming Li 			rte_errno = EBUSY;
961f926cce3SXueming Li 			return false;
962f926cce3SXueming Li 		}
963f926cce3SXueming Li 		break;
964f926cce3SXueming Li 	case RTE_ETH_REPRESENTOR_VF:
965f926cce3SXueming Li 		/* Allows HPF representor index -1 as exception. */
966f926cce3SXueming Li 		if (!(spawn->info.port_name == -1 &&
967f926cce3SXueming Li 		      switch_info->name_type ==
968f926cce3SXueming Li 				MLX5_PHYS_PORT_NAME_TYPE_PFHPF) &&
969f926cce3SXueming Li 		    switch_info->name_type != MLX5_PHYS_PORT_NAME_TYPE_PFVF) {
970f926cce3SXueming Li 			rte_errno = EBUSY;
971f926cce3SXueming Li 			return false;
972f926cce3SXueming Li 		}
973f926cce3SXueming Li 		break;
974f926cce3SXueming Li 	case RTE_ETH_REPRESENTOR_NONE:
975f926cce3SXueming Li 		rte_errno = EBUSY;
976f926cce3SXueming Li 		return false;
977f926cce3SXueming Li 	default:
978f926cce3SXueming Li 		rte_errno = ENOTSUP;
979f926cce3SXueming Li 		DRV_LOG(ERR, "unsupported representor type");
980f926cce3SXueming Li 		return false;
981f926cce3SXueming Li 	}
982f926cce3SXueming Li 	/* Check representor ID: */
983f926cce3SXueming Li 	for (p = 0; p < eth_da->nb_ports; ++p) {
984f926cce3SXueming Li 		if (spawn->pf_bond < 0) {
985f926cce3SXueming Li 			/* For non-LAG mode, allow and ignore pf. */
986f926cce3SXueming Li 			switch_info->pf_num = eth_da->ports[p];
98791766faeSXueming Li 			repr_id = mlx5_representor_id_encode(switch_info,
98891766faeSXueming Li 							     eth_da->type);
989f926cce3SXueming Li 		}
990f926cce3SXueming Li 		for (f = 0; f < eth_da->nb_representor_ports; ++f) {
991f926cce3SXueming Li 			id = MLX5_REPRESENTOR_ID
992f926cce3SXueming Li 				(eth_da->ports[p], eth_da->type,
993f926cce3SXueming Li 				 eth_da->representor_ports[f]);
994f926cce3SXueming Li 			if (repr_id == id)
995f926cce3SXueming Li 				return true;
996f926cce3SXueming Li 		}
997f926cce3SXueming Li 	}
998f926cce3SXueming Li 	rte_errno = EBUSY;
999f926cce3SXueming Li 	return false;
1000f926cce3SXueming Li }
1001f926cce3SXueming Li 
1002f926cce3SXueming Li /**
10032eb4d010SOphir Munk  * Spawn an Ethernet device from Verbs information.
10042eb4d010SOphir Munk  *
10052eb4d010SOphir Munk  * @param dpdk_dev
10062eb4d010SOphir Munk  *   Backing DPDK device.
10072eb4d010SOphir Munk  * @param spawn
10082eb4d010SOphir Munk  *   Verbs device parameters (name, port, switch_info) to spawn.
1009887183efSMichael Baum  * @param eth_da
1010cb95feefSXueming Li  *   Device arguments.
1011a729d2f0SMichael Baum  * @param mkvlist
1012a729d2f0SMichael Baum  *   Pointer to mlx5 kvargs control, can be NULL if there is no devargs.
10132eb4d010SOphir Munk  *
10142eb4d010SOphir Munk  * @return
10152eb4d010SOphir Munk  *   A valid Ethernet device object on success, NULL otherwise and rte_errno
10162eb4d010SOphir Munk  *   is set. The following errors are defined:
10172eb4d010SOphir Munk  *
10182eb4d010SOphir Munk  *   EBUSY: device is not supposed to be spawned.
10192eb4d010SOphir Munk  *   EEXIST: device is already spawned
10202eb4d010SOphir Munk  */
10212eb4d010SOphir Munk static struct rte_eth_dev *
10222eb4d010SOphir Munk mlx5_dev_spawn(struct rte_device *dpdk_dev,
10232eb4d010SOphir Munk 	       struct mlx5_dev_spawn_data *spawn,
1024a729d2f0SMichael Baum 	       struct rte_eth_devargs *eth_da,
1025a729d2f0SMichael Baum 	       struct mlx5_kvargs_ctrl *mkvlist)
10262eb4d010SOphir Munk {
10272eb4d010SOphir Munk 	const struct mlx5_switch_info *switch_info = &spawn->info;
10282eb4d010SOphir Munk 	struct mlx5_dev_ctx_shared *sh = NULL;
10293fd2961eSXueming Li 	struct ibv_port_attr port_attr = { .state = IBV_PORT_NOP };
10302eb4d010SOphir Munk 	struct rte_eth_dev *eth_dev = NULL;
10312eb4d010SOphir Munk 	struct mlx5_priv *priv = NULL;
10322eb4d010SOphir Munk 	int err = 0;
10332eb4d010SOphir Munk 	struct rte_ether_addr mac;
10342eb4d010SOphir Munk 	char name[RTE_ETH_NAME_MAX_LEN];
10352eb4d010SOphir Munk 	int own_domain_id = 0;
10362eb4d010SOphir Munk 	uint16_t port_id;
1037d0cf77e8SViacheslav Ovsiienko 	struct mlx5_port_info vport_info = { .query_flags = 0 };
103845a6df80SMichael Baum 	int nl_rdma;
1039b4edeaf3SSuanming Mou 	int i;
10402eb4d010SOphir Munk 
10412eb4d010SOphir Munk 	/* Determine if this port representor is supposed to be spawned. */
1042f926cce3SXueming Li 	if (switch_info->representor && dpdk_dev->devargs &&
1043f926cce3SXueming Li 	    !mlx5_representor_match(spawn, eth_da))
1044d6541676SXueming Li 		return NULL;
10452eb4d010SOphir Munk 	/* Build device name. */
10462eb4d010SOphir Munk 	if (spawn->pf_bond < 0) {
10472eb4d010SOphir Munk 		/* Single device. */
10482eb4d010SOphir Munk 		if (!switch_info->representor)
10492eb4d010SOphir Munk 			strlcpy(name, dpdk_dev->name, sizeof(name));
10502eb4d010SOphir Munk 		else
1051f926cce3SXueming Li 			err = snprintf(name, sizeof(name), "%s_representor_%s%u",
1052cb95feefSXueming Li 				 dpdk_dev->name,
1053cb95feefSXueming Li 				 switch_info->name_type ==
1054cb95feefSXueming Li 				 MLX5_PHYS_PORT_NAME_TYPE_PFSF ? "sf" : "vf",
1055cb95feefSXueming Li 				 switch_info->port_name);
10562eb4d010SOphir Munk 	} else {
10572eb4d010SOphir Munk 		/* Bonding device. */
1058f926cce3SXueming Li 		if (!switch_info->representor) {
1059f926cce3SXueming Li 			err = snprintf(name, sizeof(name), "%s_%s",
1060887183efSMichael Baum 				       dpdk_dev->name, spawn->phys_dev_name);
1061f926cce3SXueming Li 		} else {
1062f926cce3SXueming Li 			err = snprintf(name, sizeof(name), "%s_%s_representor_c%dpf%d%s%u",
1063887183efSMichael Baum 				dpdk_dev->name, spawn->phys_dev_name,
1064f926cce3SXueming Li 				switch_info->ctrl_num,
1065f926cce3SXueming Li 				switch_info->pf_num,
1066cb95feefSXueming Li 				switch_info->name_type ==
1067cb95feefSXueming Li 				MLX5_PHYS_PORT_NAME_TYPE_PFSF ? "sf" : "vf",
10682eb4d010SOphir Munk 				switch_info->port_name);
10692eb4d010SOphir Munk 		}
1070f926cce3SXueming Li 	}
1071f926cce3SXueming Li 	if (err >= (int)sizeof(name))
1072f926cce3SXueming Li 		DRV_LOG(WARNING, "device name overflow %s", name);
10732eb4d010SOphir Munk 	/* check if the device is already spawned */
10742eb4d010SOphir Munk 	if (rte_eth_dev_get_port_by_name(name, &port_id) == 0) {
1075a729d2f0SMichael Baum 		/*
1076a729d2f0SMichael Baum 		 * When device is already spawned, its devargs should be set
1077a729d2f0SMichael Baum 		 * as used. otherwise, mlx5_kvargs_validate() will fail.
1078a729d2f0SMichael Baum 		 */
1079a729d2f0SMichael Baum 		if (mkvlist)
1080a729d2f0SMichael Baum 			mlx5_port_args_set_used(name, port_id, mkvlist);
10812eb4d010SOphir Munk 		rte_errno = EEXIST;
10822eb4d010SOphir Munk 		return NULL;
10832eb4d010SOphir Munk 	}
10842eb4d010SOphir Munk 	DRV_LOG(DEBUG, "naming Ethernet device \"%s\"", name);
10852eb4d010SOphir Munk 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
10862eb4d010SOphir Munk 		struct mlx5_mp_id mp_id;
1087bc5d8fdbSLong Li 		int fd;
10882eb4d010SOphir Munk 
10892eb4d010SOphir Munk 		eth_dev = rte_eth_dev_attach_secondary(name);
10902eb4d010SOphir Munk 		if (eth_dev == NULL) {
10912eb4d010SOphir Munk 			DRV_LOG(ERR, "can not attach rte ethdev");
10922eb4d010SOphir Munk 			rte_errno = ENOMEM;
10932eb4d010SOphir Munk 			return NULL;
10942eb4d010SOphir Munk 		}
10952eb4d010SOphir Munk 		eth_dev->device = dpdk_dev;
1096b012b4ceSOphir Munk 		eth_dev->dev_ops = &mlx5_dev_sec_ops;
1097cbfc6111SFerruh Yigit 		eth_dev->rx_descriptor_status = mlx5_rx_descriptor_status;
1098cbfc6111SFerruh Yigit 		eth_dev->tx_descriptor_status = mlx5_tx_descriptor_status;
10992eb4d010SOphir Munk 		err = mlx5_proc_priv_init(eth_dev);
11002eb4d010SOphir Munk 		if (err)
11012eb4d010SOphir Munk 			return NULL;
1102fec28ca0SDmitry Kozlyuk 		mlx5_mp_id_init(&mp_id, eth_dev->data->port_id);
11032eb4d010SOphir Munk 		/* Receive command fd from primary process */
1104bc5d8fdbSLong Li 		fd = mlx5_mp_req_verbs_cmd_fd(&mp_id);
1105bc5d8fdbSLong Li 		if (fd < 0)
11062eb4d010SOphir Munk 			goto err_secondary;
11072eb4d010SOphir Munk 		/* Remap UAR for Tx queues. */
1108bc5d8fdbSLong Li 		err = mlx5_tx_uar_init_secondary(eth_dev, fd);
1109bc5d8fdbSLong Li 		close(fd);
11102eb4d010SOphir Munk 		if (err)
11112eb4d010SOphir Munk 			goto err_secondary;
11122eb4d010SOphir Munk 		/*
11132eb4d010SOphir Munk 		 * Ethdev pointer is still required as input since
11142eb4d010SOphir Munk 		 * the primary device is not accessible from the
11152eb4d010SOphir Munk 		 * secondary process.
11162eb4d010SOphir Munk 		 */
11172eb4d010SOphir Munk 		eth_dev->rx_pkt_burst = mlx5_select_rx_function(eth_dev);
11182eb4d010SOphir Munk 		eth_dev->tx_pkt_burst = mlx5_select_tx_function(eth_dev);
11192eb4d010SOphir Munk 		return eth_dev;
11202eb4d010SOphir Munk err_secondary:
11212eb4d010SOphir Munk 		mlx5_dev_close(eth_dev);
11222eb4d010SOphir Munk 		return NULL;
11232eb4d010SOphir Munk 	}
1124a729d2f0SMichael Baum 	sh = mlx5_alloc_shared_dev_ctx(spawn, mkvlist);
11252eb4d010SOphir Munk 	if (!sh)
11262eb4d010SOphir Munk 		return NULL;
1127be66461cSDmitry Kozlyuk 	nl_rdma = mlx5_nl_init(NETLINK_RDMA, 0);
11282eb4d010SOphir Munk 	/* Check port status. */
11293fd2961eSXueming Li 	if (spawn->phys_port <= UINT8_MAX) {
11303fd2961eSXueming Li 		/* Legacy Verbs api only support u8 port number. */
1131ca1418ceSMichael Baum 		err = mlx5_glue->query_port(sh->cdev->ctx, spawn->phys_port,
1132ca1418ceSMichael Baum 					    &port_attr);
11332eb4d010SOphir Munk 		if (err) {
11342eb4d010SOphir Munk 			DRV_LOG(ERR, "port query failed: %s", strerror(err));
11352eb4d010SOphir Munk 			goto error;
11362eb4d010SOphir Munk 		}
11372eb4d010SOphir Munk 		if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
11382eb4d010SOphir Munk 			DRV_LOG(ERR, "port is not configured in Ethernet mode");
11392eb4d010SOphir Munk 			err = EINVAL;
11402eb4d010SOphir Munk 			goto error;
11412eb4d010SOphir Munk 		}
11423fd2961eSXueming Li 	} else if (nl_rdma >= 0) {
11433fd2961eSXueming Li 		/* IB doesn't allow more than 255 ports, must be Ethernet. */
11443fd2961eSXueming Li 		err = mlx5_nl_port_state(nl_rdma,
11453fd2961eSXueming Li 			spawn->phys_dev_name,
11463fd2961eSXueming Li 			spawn->phys_port);
11473fd2961eSXueming Li 		if (err < 0) {
11483fd2961eSXueming Li 			DRV_LOG(INFO, "Failed to get netlink port state: %s",
11493fd2961eSXueming Li 				strerror(rte_errno));
11503fd2961eSXueming Li 			err = -rte_errno;
11513fd2961eSXueming Li 			goto error;
11523fd2961eSXueming Li 		}
11533fd2961eSXueming Li 		port_attr.state = (enum ibv_port_state)err;
11543fd2961eSXueming Li 	}
11552eb4d010SOphir Munk 	if (port_attr.state != IBV_PORT_ACTIVE)
11563fd2961eSXueming Li 		DRV_LOG(INFO, "port is not active: \"%s\" (%d)",
11572eb4d010SOphir Munk 			mlx5_glue->port_state_str(port_attr.state),
11582eb4d010SOphir Munk 			port_attr.state);
11592eb4d010SOphir Munk 	/* Allocate private eth device data. */
11602175c4dcSSuanming Mou 	priv = mlx5_malloc(MLX5_MEM_ZERO | MLX5_MEM_RTE,
11612eb4d010SOphir Munk 			   sizeof(*priv),
11622175c4dcSSuanming Mou 			   RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
11632eb4d010SOphir Munk 	if (priv == NULL) {
11642eb4d010SOphir Munk 		DRV_LOG(ERR, "priv allocation failure");
11652eb4d010SOphir Munk 		err = ENOMEM;
11662eb4d010SOphir Munk 		goto error;
11672eb4d010SOphir Munk 	}
116880f872eeSMichael Baum 	/*
116980f872eeSMichael Baum 	 * When user configures remote PD and CTX and device creates RxQ by
117080f872eeSMichael Baum 	 * DevX, external RxQ is both supported and requested.
117180f872eeSMichael Baum 	 */
117280f872eeSMichael Baum 	if (mlx5_imported_pd_and_ctx(sh->cdev) && mlx5_devx_obj_ops_en(sh)) {
117380f872eeSMichael Baum 		priv->ext_rxqs = mlx5_malloc(MLX5_MEM_ZERO | MLX5_MEM_RTE,
117480f872eeSMichael Baum 					     sizeof(struct mlx5_external_rxq) *
117580f872eeSMichael Baum 					     MLX5_MAX_EXT_RX_QUEUES, 0,
117680f872eeSMichael Baum 					     SOCKET_ID_ANY);
117780f872eeSMichael Baum 		if (priv->ext_rxqs == NULL) {
117880f872eeSMichael Baum 			DRV_LOG(ERR, "Fail to allocate external RxQ array.");
117980f872eeSMichael Baum 			err = ENOMEM;
118080f872eeSMichael Baum 			goto error;
118180f872eeSMichael Baum 		}
118280f872eeSMichael Baum 		DRV_LOG(DEBUG, "External RxQ is supported.");
118380f872eeSMichael Baum 	}
11842eb4d010SOphir Munk 	priv->sh = sh;
118591389890SOphir Munk 	priv->dev_port = spawn->phys_port;
11862eb4d010SOphir Munk 	priv->pci_dev = spawn->pci_dev;
11872eb4d010SOphir Munk 	priv->mtu = RTE_ETHER_MTU;
11882eb4d010SOphir Munk 	/* Some internal functions rely on Netlink sockets, open them now. */
11893fd2961eSXueming Li 	priv->nl_socket_rdma = nl_rdma;
1190be66461cSDmitry Kozlyuk 	priv->nl_socket_route =	mlx5_nl_init(NETLINK_ROUTE, 0);
11912eb4d010SOphir Munk 	priv->representor = !!switch_info->representor;
11922eb4d010SOphir Munk 	priv->master = !!switch_info->master;
11932eb4d010SOphir Munk 	priv->domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
11942eb4d010SOphir Munk 	priv->vport_meta_tag = 0;
11952eb4d010SOphir Munk 	priv->vport_meta_mask = 0;
11962eb4d010SOphir Munk 	priv->pf_bond = spawn->pf_bond;
1197ce4062cbSGregory Etelson 
1198ce4062cbSGregory Etelson 	DRV_LOG(DEBUG,
1199ce4062cbSGregory Etelson 		"dev_port=%u bus=%s pci=%s master=%d representor=%d pf_bond=%d\n",
1200ce4062cbSGregory Etelson 		priv->dev_port, dpdk_dev->bus->name,
1201ce4062cbSGregory Etelson 		priv->pci_dev ? priv->pci_dev->name : "NONE",
1202ce4062cbSGregory Etelson 		priv->master, priv->representor, priv->pf_bond);
1203ce4062cbSGregory Etelson 
12042eb4d010SOphir Munk 	/*
1205d0cf77e8SViacheslav Ovsiienko 	 * If we have E-Switch we should determine the vport attributes.
1206d0cf77e8SViacheslav Ovsiienko 	 * E-Switch may use either source vport field or reg_c[0] metadata
1207d0cf77e8SViacheslav Ovsiienko 	 * register to match on vport index. The engaged part of metadata
1208d0cf77e8SViacheslav Ovsiienko 	 * register is defined by mask.
12092eb4d010SOphir Munk 	 */
1210cf004fd3SMichael Baum 	if (sh->esw_mode) {
1211ca1418ceSMichael Baum 		err = mlx5_glue->devx_port_query(sh->cdev->ctx,
1212d0cf77e8SViacheslav Ovsiienko 						 spawn->phys_port,
1213d0cf77e8SViacheslav Ovsiienko 						 &vport_info);
12142eb4d010SOphir Munk 		if (err) {
12152eb4d010SOphir Munk 			DRV_LOG(WARNING,
1216887183efSMichael Baum 				"Cannot query devx port %d on device %s",
1217887183efSMichael Baum 				spawn->phys_port, spawn->phys_dev_name);
1218d0cf77e8SViacheslav Ovsiienko 			vport_info.query_flags = 0;
12192eb4d010SOphir Munk 		}
12202eb4d010SOphir Munk 	}
1221d0cf77e8SViacheslav Ovsiienko 	if (vport_info.query_flags & MLX5_PORT_QUERY_REG_C0) {
1222d0cf77e8SViacheslav Ovsiienko 		priv->vport_meta_tag = vport_info.vport_meta_tag;
1223d0cf77e8SViacheslav Ovsiienko 		priv->vport_meta_mask = vport_info.vport_meta_mask;
12242eb4d010SOphir Munk 		if (!priv->vport_meta_mask) {
1225887183efSMichael Baum 			DRV_LOG(ERR,
1226887183efSMichael Baum 				"vport zero mask for port %d on bonding device %s",
1227887183efSMichael Baum 				spawn->phys_port, spawn->phys_dev_name);
12282eb4d010SOphir Munk 			err = ENOTSUP;
12292eb4d010SOphir Munk 			goto error;
12302eb4d010SOphir Munk 		}
12312eb4d010SOphir Munk 		if (priv->vport_meta_tag & ~priv->vport_meta_mask) {
1232887183efSMichael Baum 			DRV_LOG(ERR,
1233887183efSMichael Baum 				"Invalid vport tag for port %d on bonding device %s",
1234887183efSMichael Baum 				spawn->phys_port, spawn->phys_dev_name);
12352eb4d010SOphir Munk 			err = ENOTSUP;
12362eb4d010SOphir Munk 			goto error;
12372eb4d010SOphir Munk 		}
12382eb4d010SOphir Munk 	}
1239d0cf77e8SViacheslav Ovsiienko 	if (vport_info.query_flags & MLX5_PORT_QUERY_VPORT) {
1240d0cf77e8SViacheslav Ovsiienko 		priv->vport_id = vport_info.vport_id;
1241cf004fd3SMichael Baum 	} else if (spawn->pf_bond >= 0 && sh->esw_mode) {
1242887183efSMichael Baum 		DRV_LOG(ERR,
1243887183efSMichael Baum 			"Cannot deduce vport index for port %d on bonding device %s",
1244887183efSMichael Baum 			spawn->phys_port, spawn->phys_dev_name);
12452eb4d010SOphir Munk 		err = ENOTSUP;
12462eb4d010SOphir Munk 		goto error;
12472eb4d010SOphir Munk 	} else {
12482eb4d010SOphir Munk 		/*
1249d0cf77e8SViacheslav Ovsiienko 		 * Suppose vport index in compatible way. Kernel/rdma_core
1250d0cf77e8SViacheslav Ovsiienko 		 * support single E-Switch per PF configurations only and
1251d0cf77e8SViacheslav Ovsiienko 		 * vport_id field contains the vport index for associated VF,
1252d0cf77e8SViacheslav Ovsiienko 		 * which is deduced from representor port name.
12532eb4d010SOphir Munk 		 * For example, let's have the IB device port 10, it has
12542eb4d010SOphir Munk 		 * attached network device eth0, which has port name attribute
12552eb4d010SOphir Munk 		 * pf0vf2, we can deduce the VF number as 2, and set vport index
12562eb4d010SOphir Munk 		 * as 3 (2+1). This assigning schema should be changed if the
12572eb4d010SOphir Munk 		 * multiple E-Switch instances per PF configurations or/and PCI
12582eb4d010SOphir Munk 		 * subfunctions are added.
12592eb4d010SOphir Munk 		 */
12602eb4d010SOphir Munk 		priv->vport_id = switch_info->representor ?
12612eb4d010SOphir Munk 				 switch_info->port_name + 1 : -1;
1262d0cf77e8SViacheslav Ovsiienko 	}
126391766faeSXueming Li 	priv->representor_id = mlx5_representor_id_encode(switch_info,
126491766faeSXueming Li 							  eth_da->type);
12652eb4d010SOphir Munk 	/*
12662eb4d010SOphir Munk 	 * Look for sibling devices in order to reuse their switch domain
12672eb4d010SOphir Munk 	 * if any, otherwise allocate one.
12682eb4d010SOphir Munk 	 */
1269ce4062cbSGregory Etelson 	MLX5_ETH_FOREACH_DEV(port_id, dpdk_dev) {
12702eb4d010SOphir Munk 		const struct mlx5_priv *opriv =
12712eb4d010SOphir Munk 			rte_eth_devices[port_id].data->dev_private;
12722eb4d010SOphir Munk 
12732eb4d010SOphir Munk 		if (!opriv ||
12742eb4d010SOphir Munk 		    opriv->sh != priv->sh ||
12752eb4d010SOphir Munk 			opriv->domain_id ==
12762eb4d010SOphir Munk 			RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID)
12772eb4d010SOphir Munk 			continue;
12782eb4d010SOphir Munk 		priv->domain_id = opriv->domain_id;
1279ce4062cbSGregory Etelson 		DRV_LOG(DEBUG, "dev_port-%u inherit domain_id=%u\n",
1280ce4062cbSGregory Etelson 			priv->dev_port, priv->domain_id);
12812eb4d010SOphir Munk 		break;
12822eb4d010SOphir Munk 	}
12832eb4d010SOphir Munk 	if (priv->domain_id == RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
12842eb4d010SOphir Munk 		err = rte_eth_switch_domain_alloc(&priv->domain_id);
12852eb4d010SOphir Munk 		if (err) {
12862eb4d010SOphir Munk 			err = rte_errno;
12872eb4d010SOphir Munk 			DRV_LOG(ERR, "unable to allocate switch domain: %s",
12882eb4d010SOphir Munk 				strerror(rte_errno));
12892eb4d010SOphir Munk 			goto error;
12902eb4d010SOphir Munk 		}
12912eb4d010SOphir Munk 		own_domain_id = 1;
1292ce4062cbSGregory Etelson 		DRV_LOG(DEBUG, "dev_port-%u new domain_id=%u\n",
1293ce4062cbSGregory Etelson 			priv->dev_port, priv->domain_id);
12942eb4d010SOphir Munk 	}
12956dc0cbc6SMichael Baum 	if (sh->cdev->config.devx) {
129645a6df80SMichael Baum 		struct mlx5_hca_attr *hca_attr = &sh->cdev->config.hca_attr;
129745a6df80SMichael Baum 
129853820561SMichael Baum 		sh->steering_format_version = hca_attr->steering_format_version;
1299c99b4f8bSLi Zhang #if defined(HAVE_MLX5DV_DR) && \
1300c99b4f8bSLi Zhang 	(defined(HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER) || \
1301c99b4f8bSLi Zhang 	 defined(HAVE_MLX5_DR_CREATE_ACTION_ASO))
130253820561SMichael Baum 		if (hca_attr->qos.sup && hca_attr->qos.flow_meter_old &&
1303a13ec19cSMichael Baum 		    sh->config.dv_flow_en) {
130453820561SMichael Baum 			uint8_t reg_c_mask = hca_attr->qos.flow_meter_reg_c_ids;
13052eb4d010SOphir Munk 			/*
13062eb4d010SOphir Munk 			 * Meter needs two REG_C's for color match and pre-sfx
13072eb4d010SOphir Munk 			 * flow match. Here get the REG_C for color match.
13082eb4d010SOphir Munk 			 * REG_C_0 and REG_C_1 is reserved for metadata feature.
13092eb4d010SOphir Munk 			 */
13102eb4d010SOphir Munk 			reg_c_mask &= 0xfc;
13112eb4d010SOphir Munk 			if (__builtin_popcount(reg_c_mask) < 1) {
13122eb4d010SOphir Munk 				priv->mtr_en = 0;
13132eb4d010SOphir Munk 				DRV_LOG(WARNING, "No available register for"
13142eb4d010SOphir Munk 					" meter.");
13152eb4d010SOphir Munk 			} else {
131631ef2982SDekel Peled 				/*
131731ef2982SDekel Peled 				 * The meter color register is used by the
131831ef2982SDekel Peled 				 * flow-hit feature as well.
131931ef2982SDekel Peled 				 * The flow-hit feature must use REG_C_3
132031ef2982SDekel Peled 				 * Prefer REG_C_3 if it is available.
132131ef2982SDekel Peled 				 */
132231ef2982SDekel Peled 				if (reg_c_mask & (1 << (REG_C_3 - REG_C_0)))
132331ef2982SDekel Peled 					priv->mtr_color_reg = REG_C_3;
132431ef2982SDekel Peled 				else
132531ef2982SDekel Peled 					priv->mtr_color_reg = ffs(reg_c_mask)
132631ef2982SDekel Peled 							      - 1 + REG_C_0;
13272eb4d010SOphir Munk 				priv->mtr_en = 1;
132853820561SMichael Baum 				priv->mtr_reg_share = hca_attr->qos.flow_meter;
13292eb4d010SOphir Munk 				DRV_LOG(DEBUG, "The REG_C meter uses is %d",
13302eb4d010SOphir Munk 					priv->mtr_color_reg);
13312eb4d010SOphir Munk 			}
13322eb4d010SOphir Munk 		}
133353820561SMichael Baum 		if (hca_attr->qos.sup && hca_attr->qos.flow_meter_aso_sup) {
133429efa63aSLi Zhang 			uint32_t log_obj_size =
133529efa63aSLi Zhang 				rte_log2_u32(MLX5_ASO_MTRS_PER_POOL >> 1);
133629efa63aSLi Zhang 			if (log_obj_size >=
133753820561SMichael Baum 			    hca_attr->qos.log_meter_aso_granularity &&
133829efa63aSLi Zhang 			    log_obj_size <=
133953820561SMichael Baum 			    hca_attr->qos.log_meter_aso_max_alloc)
134029efa63aSLi Zhang 				sh->meter_aso_en = 1;
134144432018SLi Zhang 		}
134244432018SLi Zhang 		if (priv->mtr_en) {
1343afb4aa4fSLi Zhang 			err = mlx5_aso_flow_mtrs_mng_init(priv->sh);
134429efa63aSLi Zhang 			if (err) {
134529efa63aSLi Zhang 				err = -err;
134629efa63aSLi Zhang 				goto error;
134729efa63aSLi Zhang 			}
134829efa63aSLi Zhang 		}
134953820561SMichael Baum 		if (hca_attr->flow.tunnel_header_0_1)
1350630a587bSRongwei Liu 			sh->tunnel_header_0_1 = 1;
13515c4d4917SSean Zhang 		if (hca_attr->flow.tunnel_header_2_3)
13525c4d4917SSean Zhang 			sh->tunnel_header_2_3 = 1;
13532eb4d010SOphir Munk #endif
1354a2999c7bSDekel Peled #ifdef HAVE_MLX5_DR_CREATE_ACTION_ASO
135553820561SMichael Baum 		if (hca_attr->flow_hit_aso && priv->mtr_color_reg == REG_C_3) {
135631ef2982SDekel Peled 			sh->flow_hit_aso_en = 1;
135731ef2982SDekel Peled 			err = mlx5_flow_aso_age_mng_init(sh);
135831ef2982SDekel Peled 			if (err) {
135931ef2982SDekel Peled 				err = -err;
136031ef2982SDekel Peled 				goto error;
136131ef2982SDekel Peled 			}
136231ef2982SDekel Peled 			DRV_LOG(DEBUG, "Flow Hit ASO is supported.");
136331ef2982SDekel Peled 		}
1364a2999c7bSDekel Peled #endif /* HAVE_MLX5_DR_CREATE_ACTION_ASO */
1365ee9e5fadSBing Zhao #if defined(HAVE_MLX5_DR_CREATE_ACTION_ASO) && \
1366ee9e5fadSBing Zhao 	defined(HAVE_MLX5_DR_ACTION_ASO_CT)
136753820561SMichael Baum 		if (hca_attr->ct_offload && priv->mtr_color_reg == REG_C_3) {
1368ee9e5fadSBing Zhao 			err = mlx5_flow_aso_ct_mng_init(sh);
1369ee9e5fadSBing Zhao 			if (err) {
1370ee9e5fadSBing Zhao 				err = -err;
1371ee9e5fadSBing Zhao 				goto error;
1372ee9e5fadSBing Zhao 			}
1373ee9e5fadSBing Zhao 			DRV_LOG(DEBUG, "CT ASO is supported.");
1374ee9e5fadSBing Zhao 			sh->ct_aso_en = 1;
1375ee9e5fadSBing Zhao 		}
1376ee9e5fadSBing Zhao #endif /* HAVE_MLX5_DR_CREATE_ACTION_ASO && HAVE_MLX5_DR_ACTION_ASO_CT */
137796b1f027SJiawei Wang #if defined(HAVE_MLX5DV_DR) && defined(HAVE_MLX5_DR_CREATE_ACTION_FLOW_SAMPLE)
137853820561SMichael Baum 		if (hca_attr->log_max_ft_sampler_num > 0  &&
1379a13ec19cSMichael Baum 		    sh->config.dv_flow_en) {
138096b1f027SJiawei Wang 			priv->sampler_en = 1;
13811b9e9826SThomas Monjalon 			DRV_LOG(DEBUG, "Sampler enabled!");
138296b1f027SJiawei Wang 		} else {
138396b1f027SJiawei Wang 			priv->sampler_en = 0;
138453820561SMichael Baum 			if (!hca_attr->log_max_ft_sampler_num)
13851b9e9826SThomas Monjalon 				DRV_LOG(WARNING,
13861b9e9826SThomas Monjalon 					"No available register for sampler.");
138796b1f027SJiawei Wang 			else
13881b9e9826SThomas Monjalon 				DRV_LOG(DEBUG, "DV flow is not supported!");
138996b1f027SJiawei Wang 		}
139096b1f027SJiawei Wang #endif
13912eb4d010SOphir Munk 	}
139245a6df80SMichael Baum 	/* Process parameters and store port configuration on priv structure. */
1393a729d2f0SMichael Baum 	err = mlx5_port_args_config(priv, mkvlist, &priv->config);
139445a6df80SMichael Baum 	if (err) {
139545a6df80SMichael Baum 		err = rte_errno;
139645a6df80SMichael Baum 		DRV_LOG(ERR, "Failed to process port configure: %s",
139745a6df80SMichael Baum 			strerror(rte_errno));
139845a6df80SMichael Baum 		goto error;
13993d3f4e6dSAlexander Kozyrev 	}
14002eb4d010SOphir Munk 	eth_dev = rte_eth_dev_allocate(name);
14012eb4d010SOphir Munk 	if (eth_dev == NULL) {
14022eb4d010SOphir Munk 		DRV_LOG(ERR, "can not allocate rte ethdev");
14032eb4d010SOphir Munk 		err = ENOMEM;
14042eb4d010SOphir Munk 		goto error;
14052eb4d010SOphir Munk 	}
14062eb4d010SOphir Munk 	if (priv->representor) {
14072eb4d010SOphir Munk 		eth_dev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR;
14082eb4d010SOphir Munk 		eth_dev->data->representor_id = priv->representor_id;
1409ff4e52efSViacheslav Galaktionov 		MLX5_ETH_FOREACH_DEV(port_id, dpdk_dev) {
1410ff4e52efSViacheslav Galaktionov 			struct mlx5_priv *opriv =
1411ff4e52efSViacheslav Galaktionov 				rte_eth_devices[port_id].data->dev_private;
1412ff4e52efSViacheslav Galaktionov 			if (opriv &&
1413ff4e52efSViacheslav Galaktionov 			    opriv->master &&
1414ff4e52efSViacheslav Galaktionov 			    opriv->domain_id == priv->domain_id &&
1415ff4e52efSViacheslav Galaktionov 			    opriv->sh == priv->sh) {
1416ff4e52efSViacheslav Galaktionov 				eth_dev->data->backer_port_id = port_id;
1417ff4e52efSViacheslav Galaktionov 				break;
1418ff4e52efSViacheslav Galaktionov 			}
1419ff4e52efSViacheslav Galaktionov 		}
1420ff4e52efSViacheslav Galaktionov 		if (port_id >= RTE_MAX_ETHPORTS)
1421ff4e52efSViacheslav Galaktionov 			eth_dev->data->backer_port_id = eth_dev->data->port_id;
14222eb4d010SOphir Munk 	}
142339ae7577SSuanming Mou 	priv->mp_id.port_id = eth_dev->data->port_id;
142439ae7577SSuanming Mou 	strlcpy(priv->mp_id.name, MLX5_MP_NAME, RTE_MP_MAX_NAME_LEN);
14252eb4d010SOphir Munk 	/*
14262eb4d010SOphir Munk 	 * Store associated network device interface index. This index
14272eb4d010SOphir Munk 	 * is permanent throughout the lifetime of device. So, we may store
14282eb4d010SOphir Munk 	 * the ifindex here and use the cached value further.
14292eb4d010SOphir Munk 	 */
14302eb4d010SOphir Munk 	MLX5_ASSERT(spawn->ifindex);
14312eb4d010SOphir Munk 	priv->if_index = spawn->ifindex;
1432a89f6433SRongwei Liu 	priv->lag_affinity_idx = sh->refcnt - 1;
14332eb4d010SOphir Munk 	eth_dev->data->dev_private = priv;
14342eb4d010SOphir Munk 	priv->dev_data = eth_dev->data;
14352eb4d010SOphir Munk 	eth_dev->data->mac_addrs = priv->mac;
14362eb4d010SOphir Munk 	eth_dev->device = dpdk_dev;
1437f30e69b4SFerruh Yigit 	eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
14382eb4d010SOphir Munk 	/* Configure the first MAC address by default. */
14392eb4d010SOphir Munk 	if (mlx5_get_mac(eth_dev, &mac.addr_bytes)) {
14402eb4d010SOphir Munk 		DRV_LOG(ERR,
14412eb4d010SOphir Munk 			"port %u cannot get MAC address, is mlx5_en"
14422eb4d010SOphir Munk 			" loaded? (errno: %s)",
14432eb4d010SOphir Munk 			eth_dev->data->port_id, strerror(rte_errno));
14442eb4d010SOphir Munk 		err = ENODEV;
14452eb4d010SOphir Munk 		goto error;
14462eb4d010SOphir Munk 	}
14472eb4d010SOphir Munk 	DRV_LOG(INFO,
1448c2c4f87bSAman Deep Singh 		"port %u MAC address is " RTE_ETHER_ADDR_PRT_FMT,
1449a7db3afcSAman Deep Singh 		eth_dev->data->port_id, RTE_ETHER_ADDR_BYTES(&mac));
14502eb4d010SOphir Munk #ifdef RTE_LIBRTE_MLX5_DEBUG
14512eb4d010SOphir Munk 	{
145228743807STal Shnaiderman 		char ifname[MLX5_NAMESIZE];
14532eb4d010SOphir Munk 
14542eb4d010SOphir Munk 		if (mlx5_get_ifname(eth_dev, &ifname) == 0)
14552eb4d010SOphir Munk 			DRV_LOG(DEBUG, "port %u ifname is \"%s\"",
14562eb4d010SOphir Munk 				eth_dev->data->port_id, ifname);
14572eb4d010SOphir Munk 		else
14582eb4d010SOphir Munk 			DRV_LOG(DEBUG, "port %u ifname is unknown",
14592eb4d010SOphir Munk 				eth_dev->data->port_id);
14602eb4d010SOphir Munk 	}
14612eb4d010SOphir Munk #endif
14622eb4d010SOphir Munk 	/* Get actual MTU if possible. */
14632eb4d010SOphir Munk 	err = mlx5_get_mtu(eth_dev, &priv->mtu);
14642eb4d010SOphir Munk 	if (err) {
14652eb4d010SOphir Munk 		err = rte_errno;
14662eb4d010SOphir Munk 		goto error;
14672eb4d010SOphir Munk 	}
14682eb4d010SOphir Munk 	DRV_LOG(DEBUG, "port %u MTU is %u", eth_dev->data->port_id,
14692eb4d010SOphir Munk 		priv->mtu);
14702eb4d010SOphir Munk 	/* Initialize burst functions to prevent crashes before link-up. */
1471a41f593fSFerruh Yigit 	eth_dev->rx_pkt_burst = rte_eth_pkt_burst_dummy;
1472a41f593fSFerruh Yigit 	eth_dev->tx_pkt_burst = rte_eth_pkt_burst_dummy;
1473b012b4ceSOphir Munk 	eth_dev->dev_ops = &mlx5_dev_ops;
1474cbfc6111SFerruh Yigit 	eth_dev->rx_descriptor_status = mlx5_rx_descriptor_status;
1475cbfc6111SFerruh Yigit 	eth_dev->tx_descriptor_status = mlx5_tx_descriptor_status;
1476cbfc6111SFerruh Yigit 	eth_dev->rx_queue_count = mlx5_rx_queue_count;
14772eb4d010SOphir Munk 	/* Register MAC address. */
14782eb4d010SOphir Munk 	claim_zero(mlx5_mac_addr_add(eth_dev, &mac, 0, 0));
1479a13ec19cSMichael Baum 	if (sh->dev_cap.vf && sh->config.vf_nl_en)
14802eb4d010SOphir Munk 		mlx5_nl_mac_addr_sync(priv->nl_socket_route,
14812eb4d010SOphir Munk 				      mlx5_ifindex(eth_dev),
14822eb4d010SOphir Munk 				      eth_dev->data->mac_addrs,
14832eb4d010SOphir Munk 				      MLX5_MAX_MAC_ADDRESSES);
14842eb4d010SOphir Munk 	priv->ctrl_flows = 0;
1485d163fc2dSXueming Li 	rte_spinlock_init(&priv->flow_list_lock);
14862eb4d010SOphir Munk 	TAILQ_INIT(&priv->flow_meters);
1487a295c69aSShun Hao 	priv->mtr_profile_tbl = mlx5_l3t_create(MLX5_L3T_TYPE_PTR);
1488a295c69aSShun Hao 	if (!priv->mtr_profile_tbl)
1489a295c69aSShun Hao 		goto error;
14902eb4d010SOphir Munk 	/* Bring Ethernet device up. */
14912eb4d010SOphir Munk 	DRV_LOG(DEBUG, "port %u forcing Ethernet interface up",
14922eb4d010SOphir Munk 		eth_dev->data->port_id);
1493655c3c26SDmitry Kozlyuk 	/* Read link status in case it is up and there will be no event. */
14942eb4d010SOphir Munk 	mlx5_link_update(eth_dev, 0);
1495655c3c26SDmitry Kozlyuk 	/* Watch LSC interrupts between port probe and port start. */
1496655c3c26SDmitry Kozlyuk 	priv->sh->port[priv->dev_port - 1].nl_ih_port_id =
1497655c3c26SDmitry Kozlyuk 							eth_dev->data->port_id;
1498655c3c26SDmitry Kozlyuk 	mlx5_set_link_up(eth_dev);
1499b4edeaf3SSuanming Mou 	for (i = 0; i < MLX5_FLOW_TYPE_MAXI; i++) {
1500a13ec19cSMichael Baum 		icfg[i].release_mem_en = !!sh->config.reclaim_mode;
1501a13ec19cSMichael Baum 		if (sh->config.reclaim_mode)
1502b4edeaf3SSuanming Mou 			icfg[i].per_core_cache = 0;
1503b4edeaf3SSuanming Mou 		priv->flows[i] = mlx5_ipool_create(&icfg[i]);
1504b4edeaf3SSuanming Mou 		if (!priv->flows[i])
1505b4edeaf3SSuanming Mou 			goto error;
1506b4edeaf3SSuanming Mou 	}
15072eb4d010SOphir Munk 	/* Create context for virtual machine VLAN workaround. */
15082eb4d010SOphir Munk 	priv->vmwa_context = mlx5_vlan_vmwa_init(eth_dev, spawn->ifindex);
1509a13ec19cSMichael Baum 	if (sh->config.dv_flow_en) {
15102eb4d010SOphir Munk 		err = mlx5_alloc_shared_dr(priv);
15112eb4d010SOphir Munk 		if (err)
15122eb4d010SOphir Munk 			goto error;
1513db25cadcSViacheslav Ovsiienko 		if (mlx5_flex_item_port_init(eth_dev) < 0)
1514db25cadcSViacheslav Ovsiienko 			goto error;
15152eb4d010SOphir Munk 	}
1516c4b86201SMichael Baum 	if (mlx5_devx_obj_ops_en(sh)) {
15175eaf882eSMichael Baum 		priv->obj_ops = devx_obj_ops;
1518e6988afdSMatan Azrad 		mlx5_queue_counter_id_prepare(eth_dev);
151923233fd6SBing Zhao 		priv->obj_ops.lb_dummy_queue_create =
152023233fd6SBing Zhao 					mlx5_rxq_ibv_obj_dummy_lb_create;
152123233fd6SBing Zhao 		priv->obj_ops.lb_dummy_queue_release =
152223233fd6SBing Zhao 					mlx5_rxq_ibv_obj_dummy_lb_release;
1523614966c2SXueming Li 	} else if (spawn->max_port > UINT8_MAX) {
1524614966c2SXueming Li 		/* Verbs can't support ports larger than 255 by design. */
1525614966c2SXueming Li 		DRV_LOG(ERR, "must enable DV and ESW when RDMA link ports > 255");
1526614966c2SXueming Li 		err = ENOTSUP;
1527614966c2SXueming Li 		goto error;
15285eaf882eSMichael Baum 	} else {
15295eaf882eSMichael Baum 		priv->obj_ops = ibv_obj_ops;
15305eaf882eSMichael Baum 	}
1531a13ec19cSMichael Baum 	if (sh->config.tx_pp &&
153211cfe349SViacheslav Ovsiienko 	    priv->obj_ops.txq_obj_new != mlx5_txq_devx_obj_new) {
1533f17e4b4fSViacheslav Ovsiienko 		/*
1534f17e4b4fSViacheslav Ovsiienko 		 * HAVE_MLX5DV_DEVX_UAR_OFFSET is required to support
1535f17e4b4fSViacheslav Ovsiienko 		 * packet pacing and already checked above.
1536f17e4b4fSViacheslav Ovsiienko 		 * Hence, we should only make sure the SQs will be created
1537f17e4b4fSViacheslav Ovsiienko 		 * with DevX, not with Verbs.
1538f17e4b4fSViacheslav Ovsiienko 		 * Verbs allocates the SQ UAR on its own and it can't be shared
1539f17e4b4fSViacheslav Ovsiienko 		 * with Clock Queue UAR as required for Tx scheduling.
1540f17e4b4fSViacheslav Ovsiienko 		 */
1541f17e4b4fSViacheslav Ovsiienko 		DRV_LOG(ERR, "Verbs SQs, UAR can't be shared as required for packet pacing");
1542f17e4b4fSViacheslav Ovsiienko 		err = ENODEV;
1543f17e4b4fSViacheslav Ovsiienko 		goto error;
1544f17e4b4fSViacheslav Ovsiienko 	}
154565b3cd0dSSuanming Mou 	priv->drop_queue.hrxq = mlx5_drop_action_create(eth_dev);
154665b3cd0dSSuanming Mou 	if (!priv->drop_queue.hrxq)
154765b3cd0dSSuanming Mou 		goto error;
15483a2f674bSSuanming Mou 	priv->hrxqs = mlx5_list_create("hrxq", eth_dev, true,
15493a2f674bSSuanming Mou 				       mlx5_hrxq_create_cb,
15503a2f674bSSuanming Mou 				       mlx5_hrxq_match_cb,
15513a2f674bSSuanming Mou 				       mlx5_hrxq_remove_cb,
15523a2f674bSSuanming Mou 				       mlx5_hrxq_clone_cb,
15533a2f674bSSuanming Mou 				       mlx5_hrxq_clone_free_cb);
15543a2f674bSSuanming Mou 	if (!priv->hrxqs)
15553a2f674bSSuanming Mou 		goto error;
15560f4aa72bSSuanming Mou 	mlx5_set_metadata_mask(eth_dev);
15570f4aa72bSSuanming Mou 	if (sh->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
15580f4aa72bSSuanming Mou 	    !priv->sh->dv_regc0_mask) {
15590f4aa72bSSuanming Mou 		DRV_LOG(ERR, "metadata mode %u is not supported "
15600f4aa72bSSuanming Mou 			     "(no metadata reg_c[0] is available)",
15610f4aa72bSSuanming Mou 			     sh->config.dv_xmeta_en);
15620f4aa72bSSuanming Mou 			err = ENOTSUP;
15630f4aa72bSSuanming Mou 			goto error;
15640f4aa72bSSuanming Mou 	}
15653a2f674bSSuanming Mou 	rte_rwlock_init(&priv->ind_tbls_lock);
15665bd0e3e6SDariusz Sosnowski 	if (priv->sh->config.dv_flow_en == 2) {
15671939eb6fSDariusz Sosnowski #ifdef HAVE_MLX5_HWS_SUPPORT
15685bd0e3e6SDariusz Sosnowski 		if (priv->vport_meta_mask)
15695bd0e3e6SDariusz Sosnowski 			flow_hw_set_port_info(eth_dev);
1570ddb68e47SBing Zhao 		if (priv->sh->config.dv_esw_en &&
1571ddb68e47SBing Zhao 		    priv->sh->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
1572ddb68e47SBing Zhao 		    priv->sh->config.dv_xmeta_en != MLX5_XMETA_MODE_META32_HWS) {
1573ddb68e47SBing Zhao 			DRV_LOG(ERR,
1574ddb68e47SBing Zhao 				"metadata mode %u is not supported in HWS eswitch mode",
1575ddb68e47SBing Zhao 				priv->sh->config.dv_xmeta_en);
1576ddb68e47SBing Zhao 				err = ENOTSUP;
1577ddb68e47SBing Zhao 				goto error;
1578ddb68e47SBing Zhao 		}
15798a89038fSBing Zhao 		/* Only HWS requires this information. */
15808a89038fSBing Zhao 		flow_hw_init_tags_set(eth_dev);
1581*f1fecffaSDariusz Sosnowski 		flow_hw_init_flow_metadata_config(eth_dev);
15821939eb6fSDariusz Sosnowski 		if (priv->sh->config.dv_esw_en &&
15831939eb6fSDariusz Sosnowski 		    flow_hw_create_vport_action(eth_dev)) {
15841939eb6fSDariusz Sosnowski 			DRV_LOG(ERR, "port %u failed to create vport action",
15851939eb6fSDariusz Sosnowski 				eth_dev->data->port_id);
15861939eb6fSDariusz Sosnowski 			err = EINVAL;
15871939eb6fSDariusz Sosnowski 			goto error;
15881939eb6fSDariusz Sosnowski 		}
1589d84c3cf7SSuanming Mou 		return eth_dev;
15905bd0e3e6SDariusz Sosnowski #else
15915bd0e3e6SDariusz Sosnowski 		DRV_LOG(ERR, "DV support is missing for HWS.");
15925bd0e3e6SDariusz Sosnowski 		goto error;
15935bd0e3e6SDariusz Sosnowski #endif
15945bd0e3e6SDariusz Sosnowski 	}
15953c4338a4SJiawei Wang 	if (!priv->sh->flow_priority_check_flag) {
15962eb4d010SOphir Munk 		/* Supported Verbs flow priority number detection. */
15972eb4d010SOphir Munk 		err = mlx5_flow_discover_priorities(eth_dev);
15983c4338a4SJiawei Wang 		priv->sh->flow_max_priority = err;
15993c4338a4SJiawei Wang 		priv->sh->flow_priority_check_flag = 1;
16003c4338a4SJiawei Wang 	} else {
16013c4338a4SJiawei Wang 		err = priv->sh->flow_max_priority;
16023c4338a4SJiawei Wang 	}
16032eb4d010SOphir Munk 	if (err < 0) {
16042eb4d010SOphir Munk 		err = -err;
16052eb4d010SOphir Munk 		goto error;
16062eb4d010SOphir Munk 	}
16072eb4d010SOphir Munk 	/* Query availability of metadata reg_c's. */
16083c4338a4SJiawei Wang 	if (!priv->sh->metadata_regc_check_flag) {
16092eb4d010SOphir Munk 		err = mlx5_flow_discover_mreg_c(eth_dev);
16102eb4d010SOphir Munk 		if (err < 0) {
16112eb4d010SOphir Munk 			err = -err;
16122eb4d010SOphir Munk 			goto error;
16132eb4d010SOphir Munk 		}
16143c4338a4SJiawei Wang 	}
16152eb4d010SOphir Munk 	if (!mlx5_flow_ext_mreg_supported(eth_dev)) {
16162eb4d010SOphir Munk 		DRV_LOG(DEBUG,
16172eb4d010SOphir Munk 			"port %u extensive metadata register is not supported",
16182eb4d010SOphir Munk 			eth_dev->data->port_id);
1619a13ec19cSMichael Baum 		if (sh->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
16202eb4d010SOphir Munk 			DRV_LOG(ERR, "metadata mode %u is not supported "
16212eb4d010SOphir Munk 				     "(no metadata registers available)",
1622a13ec19cSMichael Baum 				     sh->config.dv_xmeta_en);
16232eb4d010SOphir Munk 			err = ENOTSUP;
16242eb4d010SOphir Munk 			goto error;
16252eb4d010SOphir Munk 		}
16262eb4d010SOphir Munk 	}
1627a13ec19cSMichael Baum 	if (sh->config.dv_flow_en &&
1628a13ec19cSMichael Baum 	    sh->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
16292eb4d010SOphir Munk 	    mlx5_flow_ext_mreg_supported(eth_dev) &&
16302eb4d010SOphir Munk 	    priv->sh->dv_regc0_mask) {
16312eb4d010SOphir Munk 		priv->mreg_cp_tbl = mlx5_hlist_create(MLX5_FLOW_MREG_HNAME,
1632e69a5922SXueming Li 						      MLX5_FLOW_MREG_HTABLE_SZ,
1633961b6774SMatan Azrad 						      false, true, eth_dev,
1634f7f73ac1SXueming Li 						      flow_dv_mreg_create_cb,
1635f5b0aed2SSuanming Mou 						      flow_dv_mreg_match_cb,
1636961b6774SMatan Azrad 						      flow_dv_mreg_remove_cb,
1637961b6774SMatan Azrad 						      flow_dv_mreg_clone_cb,
1638961b6774SMatan Azrad 						    flow_dv_mreg_clone_free_cb);
16392eb4d010SOphir Munk 		if (!priv->mreg_cp_tbl) {
16402eb4d010SOphir Munk 			err = ENOMEM;
16412eb4d010SOphir Munk 			goto error;
16422eb4d010SOphir Munk 		}
16432eb4d010SOphir Munk 	}
1644cc608e4dSSuanming Mou 	rte_spinlock_init(&priv->shared_act_sl);
1645994829e6SSuanming Mou 	mlx5_flow_counter_mode_config(eth_dev);
164645633c46SSuanming Mou 	mlx5_flow_drop_action_config(eth_dev);
1647a13ec19cSMichael Baum 	if (sh->config.dv_flow_en)
16489fbe97f0SXueming Li 		eth_dev->data->dev_flags |= RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE;
16492eb4d010SOphir Munk 	return eth_dev;
16502eb4d010SOphir Munk error:
16512eb4d010SOphir Munk 	if (priv) {
16521939eb6fSDariusz Sosnowski #ifdef HAVE_MLX5_HWS_SUPPORT
16531939eb6fSDariusz Sosnowski 		if (eth_dev &&
16541939eb6fSDariusz Sosnowski 		    priv->sh &&
16551939eb6fSDariusz Sosnowski 		    priv->sh->config.dv_flow_en == 2 &&
16561939eb6fSDariusz Sosnowski 		    priv->sh->config.dv_esw_en)
16571939eb6fSDariusz Sosnowski 			flow_hw_destroy_vport_action(eth_dev);
16581939eb6fSDariusz Sosnowski #endif
16592eb4d010SOphir Munk 		if (priv->mreg_cp_tbl)
1660e69a5922SXueming Li 			mlx5_hlist_destroy(priv->mreg_cp_tbl);
16612eb4d010SOphir Munk 		if (priv->sh)
16622eb4d010SOphir Munk 			mlx5_os_free_shared_dr(priv);
16632eb4d010SOphir Munk 		if (priv->nl_socket_route >= 0)
16642eb4d010SOphir Munk 			close(priv->nl_socket_route);
16652eb4d010SOphir Munk 		if (priv->vmwa_context)
16662eb4d010SOphir Munk 			mlx5_vlan_vmwa_exit(priv->vmwa_context);
166765b3cd0dSSuanming Mou 		if (eth_dev && priv->drop_queue.hrxq)
166865b3cd0dSSuanming Mou 			mlx5_drop_action_destroy(eth_dev);
1669a295c69aSShun Hao 		if (priv->mtr_profile_tbl)
1670a295c69aSShun Hao 			mlx5_l3t_destroy(priv->mtr_profile_tbl);
16712eb4d010SOphir Munk 		if (own_domain_id)
16722eb4d010SOphir Munk 			claim_zero(rte_eth_switch_domain_free(priv->domain_id));
1673679f46c7SMatan Azrad 		if (priv->hrxqs)
1674679f46c7SMatan Azrad 			mlx5_list_destroy(priv->hrxqs);
1675db25cadcSViacheslav Ovsiienko 		if (eth_dev && priv->flex_item_map)
1676db25cadcSViacheslav Ovsiienko 			mlx5_flex_item_port_cleanup(eth_dev);
167780f872eeSMichael Baum 		mlx5_free(priv->ext_rxqs);
16782175c4dcSSuanming Mou 		mlx5_free(priv);
16792eb4d010SOphir Munk 		if (eth_dev != NULL)
16802eb4d010SOphir Munk 			eth_dev->data->dev_private = NULL;
16812eb4d010SOphir Munk 	}
16822eb4d010SOphir Munk 	if (eth_dev != NULL) {
16832eb4d010SOphir Munk 		/* mac_addrs must not be freed alone because part of
16842eb4d010SOphir Munk 		 * dev_private
16852eb4d010SOphir Munk 		 **/
16862eb4d010SOphir Munk 		eth_dev->data->mac_addrs = NULL;
16872eb4d010SOphir Munk 		rte_eth_dev_release_port(eth_dev);
16882eb4d010SOphir Munk 	}
16892eb4d010SOphir Munk 	if (sh)
169091389890SOphir Munk 		mlx5_free_shared_dev_ctx(sh);
16913fd2961eSXueming Li 	if (nl_rdma >= 0)
16923fd2961eSXueming Li 		close(nl_rdma);
16932eb4d010SOphir Munk 	MLX5_ASSERT(err > 0);
16942eb4d010SOphir Munk 	rte_errno = err;
16952eb4d010SOphir Munk 	return NULL;
16962eb4d010SOphir Munk }
16972eb4d010SOphir Munk 
16982eb4d010SOphir Munk /**
16992eb4d010SOphir Munk  * Comparison callback to sort device data.
17002eb4d010SOphir Munk  *
17012eb4d010SOphir Munk  * This is meant to be used with qsort().
17022eb4d010SOphir Munk  *
17032eb4d010SOphir Munk  * @param a[in]
17042eb4d010SOphir Munk  *   Pointer to pointer to first data object.
17052eb4d010SOphir Munk  * @param b[in]
17062eb4d010SOphir Munk  *   Pointer to pointer to second data object.
17072eb4d010SOphir Munk  *
17082eb4d010SOphir Munk  * @return
17092eb4d010SOphir Munk  *   0 if both objects are equal, less than 0 if the first argument is less
17102eb4d010SOphir Munk  *   than the second, greater than 0 otherwise.
17112eb4d010SOphir Munk  */
17122eb4d010SOphir Munk static int
17132eb4d010SOphir Munk mlx5_dev_spawn_data_cmp(const void *a, const void *b)
17142eb4d010SOphir Munk {
17152eb4d010SOphir Munk 	const struct mlx5_switch_info *si_a =
17162eb4d010SOphir Munk 		&((const struct mlx5_dev_spawn_data *)a)->info;
17172eb4d010SOphir Munk 	const struct mlx5_switch_info *si_b =
17182eb4d010SOphir Munk 		&((const struct mlx5_dev_spawn_data *)b)->info;
17192eb4d010SOphir Munk 	int ret;
17202eb4d010SOphir Munk 
17212eb4d010SOphir Munk 	/* Master device first. */
17222eb4d010SOphir Munk 	ret = si_b->master - si_a->master;
17232eb4d010SOphir Munk 	if (ret)
17242eb4d010SOphir Munk 		return ret;
17252eb4d010SOphir Munk 	/* Then representor devices. */
17262eb4d010SOphir Munk 	ret = si_b->representor - si_a->representor;
17272eb4d010SOphir Munk 	if (ret)
17282eb4d010SOphir Munk 		return ret;
17292eb4d010SOphir Munk 	/* Unidentified devices come last in no specific order. */
17302eb4d010SOphir Munk 	if (!si_a->representor)
17312eb4d010SOphir Munk 		return 0;
17322eb4d010SOphir Munk 	/* Order representors by name. */
17332eb4d010SOphir Munk 	return si_a->port_name - si_b->port_name;
17342eb4d010SOphir Munk }
17352eb4d010SOphir Munk 
17362eb4d010SOphir Munk /**
17372eb4d010SOphir Munk  * Match PCI information for possible slaves of bonding device.
17382eb4d010SOphir Munk  *
1739ca1418ceSMichael Baum  * @param[in] ibdev_name
1740ca1418ceSMichael Baum  *   Name of Infiniband device.
17412eb4d010SOphir Munk  * @param[in] pci_dev
1742f926cce3SXueming Li  *   Pointer to primary PCI address structure to match.
17432eb4d010SOphir Munk  * @param[in] nl_rdma
17442eb4d010SOphir Munk  *   Netlink RDMA group socket handle.
1745f926cce3SXueming Li  * @param[in] owner
1746ca1418ceSMichael Baum  *   Representor owner PF index.
1747f5f4c482SXueming Li  * @param[out] bond_info
1748f5f4c482SXueming Li  *   Pointer to bonding information.
17492eb4d010SOphir Munk  *
17502eb4d010SOphir Munk  * @return
17512eb4d010SOphir Munk  *   negative value if no bonding device found, otherwise
17522eb4d010SOphir Munk  *   positive index of slave PF in bonding.
17532eb4d010SOphir Munk  */
17542eb4d010SOphir Munk static int
1755ca1418ceSMichael Baum mlx5_device_bond_pci_match(const char *ibdev_name,
1756f926cce3SXueming Li 			   const struct rte_pci_addr *pci_dev,
1757f5f4c482SXueming Li 			   int nl_rdma, uint16_t owner,
1758f5f4c482SXueming Li 			   struct mlx5_bond_info *bond_info)
17592eb4d010SOphir Munk {
17602eb4d010SOphir Munk 	char ifname[IF_NAMESIZE + 1];
17612eb4d010SOphir Munk 	unsigned int ifindex;
17622eb4d010SOphir Munk 	unsigned int np, i;
1763f5f4c482SXueming Li 	FILE *bond_file = NULL, *file;
17642eb4d010SOphir Munk 	int pf = -1;
1765f5f4c482SXueming Li 	int ret;
17667299ab68SRongwei Liu 	uint8_t cur_guid[32] = {0};
17677299ab68SRongwei Liu 	uint8_t guid[32] = {0};
17682eb4d010SOphir Munk 
17692eb4d010SOphir Munk 	/*
1770ca1418ceSMichael Baum 	 * Try to get master device name. If something goes wrong suppose
1771ca1418ceSMichael Baum 	 * the lack of kernel support and no bonding devices.
17722eb4d010SOphir Munk 	 */
1773f5f4c482SXueming Li 	memset(bond_info, 0, sizeof(*bond_info));
17742eb4d010SOphir Munk 	if (nl_rdma < 0)
17752eb4d010SOphir Munk 		return -1;
1776ca1418ceSMichael Baum 	if (!strstr(ibdev_name, "bond"))
17772eb4d010SOphir Munk 		return -1;
1778ca1418ceSMichael Baum 	np = mlx5_nl_portnum(nl_rdma, ibdev_name);
17792eb4d010SOphir Munk 	if (!np)
17802eb4d010SOphir Munk 		return -1;
17817299ab68SRongwei Liu 	if (mlx5_get_device_guid(pci_dev, cur_guid, sizeof(cur_guid)) < 0)
17827299ab68SRongwei Liu 		return -1;
17832eb4d010SOphir Munk 	/*
1784ca1418ceSMichael Baum 	 * The master device might not be on the predefined port(not on port
1785ca1418ceSMichael Baum 	 * index 1, it is not guaranteed), we have to scan all Infiniband
1786ca1418ceSMichael Baum 	 * device ports and find master.
17872eb4d010SOphir Munk 	 */
17882eb4d010SOphir Munk 	for (i = 1; i <= np; ++i) {
17892eb4d010SOphir Munk 		/* Check whether Infiniband port is populated. */
1790ca1418ceSMichael Baum 		ifindex = mlx5_nl_ifindex(nl_rdma, ibdev_name, i);
17912eb4d010SOphir Munk 		if (!ifindex)
17922eb4d010SOphir Munk 			continue;
17932eb4d010SOphir Munk 		if (!if_indextoname(ifindex, ifname))
17942eb4d010SOphir Munk 			continue;
17952eb4d010SOphir Munk 		/* Try to read bonding slave names from sysfs. */
17962eb4d010SOphir Munk 		MKSTR(slaves,
17972eb4d010SOphir Munk 		      "/sys/class/net/%s/master/bonding/slaves", ifname);
1798f5f4c482SXueming Li 		bond_file = fopen(slaves, "r");
1799f5f4c482SXueming Li 		if (bond_file)
18002eb4d010SOphir Munk 			break;
18012eb4d010SOphir Munk 	}
1802f5f4c482SXueming Li 	if (!bond_file)
18032eb4d010SOphir Munk 		return -1;
18042eb4d010SOphir Munk 	/* Use safe format to check maximal buffer length. */
18052eb4d010SOphir Munk 	MLX5_ASSERT(atol(RTE_STR(IF_NAMESIZE)) == IF_NAMESIZE);
1806f5f4c482SXueming Li 	while (fscanf(bond_file, "%" RTE_STR(IF_NAMESIZE) "s", ifname) == 1) {
18072eb4d010SOphir Munk 		char tmp_str[IF_NAMESIZE + 32];
18082eb4d010SOphir Munk 		struct rte_pci_addr pci_addr;
18092eb4d010SOphir Munk 		struct mlx5_switch_info	info;
18107299ab68SRongwei Liu 		int ret;
18112eb4d010SOphir Munk 
18122eb4d010SOphir Munk 		/* Process slave interface names in the loop. */
18132eb4d010SOphir Munk 		snprintf(tmp_str, sizeof(tmp_str),
18142eb4d010SOphir Munk 			 "/sys/class/net/%s", ifname);
18154d567938SThomas Monjalon 		if (mlx5_get_pci_addr(tmp_str, &pci_addr)) {
1816ca1418ceSMichael Baum 			DRV_LOG(WARNING,
1817ca1418ceSMichael Baum 				"Cannot get PCI address for netdev \"%s\".",
1818ca1418ceSMichael Baum 				ifname);
18192eb4d010SOphir Munk 			continue;
18202eb4d010SOphir Munk 		}
18212eb4d010SOphir Munk 		/* Slave interface PCI address match found. */
18222eb4d010SOphir Munk 		snprintf(tmp_str, sizeof(tmp_str),
18232eb4d010SOphir Munk 			 "/sys/class/net/%s/phys_port_name", ifname);
18242eb4d010SOphir Munk 		file = fopen(tmp_str, "rb");
18252eb4d010SOphir Munk 		if (!file)
18262eb4d010SOphir Munk 			break;
18272eb4d010SOphir Munk 		info.name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET;
18282eb4d010SOphir Munk 		if (fscanf(file, "%32s", tmp_str) == 1)
18292eb4d010SOphir Munk 			mlx5_translate_port_name(tmp_str, &info);
1830f5f4c482SXueming Li 		fclose(file);
1831f5f4c482SXueming Li 		/* Only process PF ports. */
1832f5f4c482SXueming Li 		if (info.name_type != MLX5_PHYS_PORT_NAME_TYPE_LEGACY &&
1833f5f4c482SXueming Li 		    info.name_type != MLX5_PHYS_PORT_NAME_TYPE_UPLINK)
1834f5f4c482SXueming Li 			continue;
1835f5f4c482SXueming Li 		/* Check max bonding member. */
1836f5f4c482SXueming Li 		if (info.port_name >= MLX5_BOND_MAX_PORTS) {
1837f5f4c482SXueming Li 			DRV_LOG(WARNING, "bonding index out of range, "
1838f5f4c482SXueming Li 				"please increase MLX5_BOND_MAX_PORTS: %s",
1839f5f4c482SXueming Li 				tmp_str);
18402eb4d010SOphir Munk 			break;
18412eb4d010SOphir Munk 		}
1842f5f4c482SXueming Li 		/* Get ifindex. */
1843f5f4c482SXueming Li 		snprintf(tmp_str, sizeof(tmp_str),
1844f5f4c482SXueming Li 			 "/sys/class/net/%s/ifindex", ifname);
1845f5f4c482SXueming Li 		file = fopen(tmp_str, "rb");
1846f5f4c482SXueming Li 		if (!file)
1847f5f4c482SXueming Li 			break;
1848f5f4c482SXueming Li 		ret = fscanf(file, "%u", &ifindex);
18492eb4d010SOphir Munk 		fclose(file);
1850f5f4c482SXueming Li 		if (ret != 1)
1851f5f4c482SXueming Li 			break;
1852f5f4c482SXueming Li 		/* Save bonding info. */
1853f5f4c482SXueming Li 		strncpy(bond_info->ports[info.port_name].ifname, ifname,
1854f5f4c482SXueming Li 			sizeof(bond_info->ports[0].ifname));
1855f5f4c482SXueming Li 		bond_info->ports[info.port_name].pci_addr = pci_addr;
1856f5f4c482SXueming Li 		bond_info->ports[info.port_name].ifindex = ifindex;
1857f5f4c482SXueming Li 		bond_info->n_port++;
18587299ab68SRongwei Liu 		/*
18597299ab68SRongwei Liu 		 * Under socket direct mode, bonding will use
18607299ab68SRongwei Liu 		 * system_image_guid as identification.
18617299ab68SRongwei Liu 		 * After OFED 5.4, guid is readable (ret >= 0) under sysfs.
18627299ab68SRongwei Liu 		 * All bonding members should have the same guid even if driver
18637299ab68SRongwei Liu 		 * is using PCIe BDF.
18647299ab68SRongwei Liu 		 */
18657299ab68SRongwei Liu 		ret = mlx5_get_device_guid(&pci_addr, guid, sizeof(guid));
18667299ab68SRongwei Liu 		if (ret < 0)
18677299ab68SRongwei Liu 			break;
18687299ab68SRongwei Liu 		else if (ret > 0) {
18697299ab68SRongwei Liu 			if (!memcmp(guid, cur_guid, sizeof(guid)) &&
18707299ab68SRongwei Liu 			    owner == info.port_name &&
18717299ab68SRongwei Liu 			    (owner != 0 || (owner == 0 &&
18727299ab68SRongwei Liu 			    !rte_pci_addr_cmp(pci_dev, &pci_addr))))
18737299ab68SRongwei Liu 				pf = info.port_name;
18747299ab68SRongwei Liu 		} else if (pci_dev->domain == pci_addr.domain &&
18757299ab68SRongwei Liu 		    pci_dev->bus == pci_addr.bus &&
18767299ab68SRongwei Liu 		    pci_dev->devid == pci_addr.devid &&
18777299ab68SRongwei Liu 		    ((pci_dev->function == 0 &&
18787299ab68SRongwei Liu 		      pci_dev->function + owner == pci_addr.function) ||
18797299ab68SRongwei Liu 		     (pci_dev->function == owner &&
18807299ab68SRongwei Liu 		      pci_addr.function == owner)))
18817299ab68SRongwei Liu 			pf = info.port_name;
1882f5f4c482SXueming Li 	}
1883f5f4c482SXueming Li 	if (pf >= 0) {
1884f5f4c482SXueming Li 		/* Get bond interface info */
1885f5f4c482SXueming Li 		ret = mlx5_sysfs_bond_info(ifindex, &bond_info->ifindex,
1886f5f4c482SXueming Li 					   bond_info->ifname);
1887f5f4c482SXueming Li 		if (ret)
1888f5f4c482SXueming Li 			DRV_LOG(ERR, "unable to get bond info: %s",
1889f5f4c482SXueming Li 				strerror(rte_errno));
1890f5f4c482SXueming Li 		else
1891f5f4c482SXueming Li 			DRV_LOG(INFO, "PF device %u, bond device %u(%s)",
1892f5f4c482SXueming Li 				ifindex, bond_info->ifindex, bond_info->ifname);
1893f5f4c482SXueming Li 	}
18947299ab68SRongwei Liu 	if (owner == 0 && pf != 0) {
18957299ab68SRongwei Liu 		DRV_LOG(INFO, "PCIe instance %04x:%02x:%02x.%x isn't bonding owner",
18967299ab68SRongwei Liu 				pci_dev->domain, pci_dev->bus, pci_dev->devid,
18977299ab68SRongwei Liu 				pci_dev->function);
18987299ab68SRongwei Liu 	}
18992eb4d010SOphir Munk 	return pf;
19002eb4d010SOphir Munk }
19012eb4d010SOphir Munk 
19022eb4d010SOphir Munk /**
190308c2772fSXueming Li  * Register a PCI device within bonding.
19042eb4d010SOphir Munk  *
190508c2772fSXueming Li  * This function spawns Ethernet devices out of a given PCI device and
190608c2772fSXueming Li  * bonding owner PF index.
19072eb4d010SOphir Munk  *
19087af08c8fSMichael Baum  * @param[in] cdev
19097af08c8fSMichael Baum  *   Pointer to common mlx5 device structure.
191008c2772fSXueming Li  * @param[in] req_eth_da
191108c2772fSXueming Li  *   Requested ethdev device argument.
191208c2772fSXueming Li  * @param[in] owner_id
191308c2772fSXueming Li  *   Requested owner PF port ID within bonding device, default to 0.
1914a729d2f0SMichael Baum  * @param[in, out] mkvlist
1915a729d2f0SMichael Baum  *   Pointer to mlx5 kvargs control, can be NULL if there is no devargs.
19162eb4d010SOphir Munk  *
19172eb4d010SOphir Munk  * @return
19182eb4d010SOphir Munk  *   0 on success, a negative errno value otherwise and rte_errno is set.
19192eb4d010SOphir Munk  */
192008c2772fSXueming Li static int
1921ca1418ceSMichael Baum mlx5_os_pci_probe_pf(struct mlx5_common_device *cdev,
192208c2772fSXueming Li 		     struct rte_eth_devargs *req_eth_da,
1923a729d2f0SMichael Baum 		     uint16_t owner_id, struct mlx5_kvargs_ctrl *mkvlist)
19242eb4d010SOphir Munk {
19252eb4d010SOphir Munk 	struct ibv_device **ibv_list;
19262eb4d010SOphir Munk 	/*
19272eb4d010SOphir Munk 	 * Number of found IB Devices matching with requested PCI BDF.
19282eb4d010SOphir Munk 	 * nd != 1 means there are multiple IB devices over the same
19292eb4d010SOphir Munk 	 * PCI device and we have representors and master.
19302eb4d010SOphir Munk 	 */
19312eb4d010SOphir Munk 	unsigned int nd = 0;
19322eb4d010SOphir Munk 	/*
19332eb4d010SOphir Munk 	 * Number of found IB device Ports. nd = 1 and np = 1..n means
19342eb4d010SOphir Munk 	 * we have the single multiport IB device, and there may be
19352eb4d010SOphir Munk 	 * representors attached to some of found ports.
19362eb4d010SOphir Munk 	 */
19372eb4d010SOphir Munk 	unsigned int np = 0;
19382eb4d010SOphir Munk 	/*
19392eb4d010SOphir Munk 	 * Number of DPDK ethernet devices to Spawn - either over
19402eb4d010SOphir Munk 	 * multiple IB devices or multiple ports of single IB device.
19412eb4d010SOphir Munk 	 * Actually this is the number of iterations to spawn.
19422eb4d010SOphir Munk 	 */
19432eb4d010SOphir Munk 	unsigned int ns = 0;
19442eb4d010SOphir Munk 	/*
19452eb4d010SOphir Munk 	 * Bonding device
19462eb4d010SOphir Munk 	 *   < 0 - no bonding device (single one)
19472eb4d010SOphir Munk 	 *  >= 0 - bonding device (value is slave PF index)
19482eb4d010SOphir Munk 	 */
19492eb4d010SOphir Munk 	int bd = -1;
19507af08c8fSMichael Baum 	struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(cdev->dev);
19512eb4d010SOphir Munk 	struct mlx5_dev_spawn_data *list = NULL;
195208c2772fSXueming Li 	struct rte_eth_devargs eth_da = *req_eth_da;
1953f926cce3SXueming Li 	struct rte_pci_addr owner_pci = pci_dev->addr; /* Owner PF. */
1954f5f4c482SXueming Li 	struct mlx5_bond_info bond_info;
1955f926cce3SXueming Li 	int ret = -1;
19562eb4d010SOphir Munk 
19572eb4d010SOphir Munk 	errno = 0;
19582eb4d010SOphir Munk 	ibv_list = mlx5_glue->get_device_list(&ret);
19592eb4d010SOphir Munk 	if (!ibv_list) {
19602eb4d010SOphir Munk 		rte_errno = errno ? errno : ENOSYS;
1961887183efSMichael Baum 		DRV_LOG(ERR, "Cannot list devices, is ib_uverbs loaded?");
19622eb4d010SOphir Munk 		return -rte_errno;
19632eb4d010SOphir Munk 	}
19642eb4d010SOphir Munk 	/*
19652eb4d010SOphir Munk 	 * First scan the list of all Infiniband devices to find
19662eb4d010SOphir Munk 	 * matching ones, gathering into the list.
19672eb4d010SOphir Munk 	 */
19682eb4d010SOphir Munk 	struct ibv_device *ibv_match[ret + 1];
1969be66461cSDmitry Kozlyuk 	int nl_route = mlx5_nl_init(NETLINK_ROUTE, 0);
1970be66461cSDmitry Kozlyuk 	int nl_rdma = mlx5_nl_init(NETLINK_RDMA, 0);
19712eb4d010SOphir Munk 	unsigned int i;
19722eb4d010SOphir Munk 
19732eb4d010SOphir Munk 	while (ret-- > 0) {
19742eb4d010SOphir Munk 		struct rte_pci_addr pci_addr;
19752eb4d010SOphir Munk 
1976887183efSMichael Baum 		DRV_LOG(DEBUG, "Checking device \"%s\"", ibv_list[ret]->name);
1977ca1418ceSMichael Baum 		bd = mlx5_device_bond_pci_match(ibv_list[ret]->name, &owner_pci,
1978ca1418ceSMichael Baum 						nl_rdma, owner_id, &bond_info);
19792eb4d010SOphir Munk 		if (bd >= 0) {
19802eb4d010SOphir Munk 			/*
19812eb4d010SOphir Munk 			 * Bonding device detected. Only one match is allowed,
19822eb4d010SOphir Munk 			 * the bonding is supported over multi-port IB device,
19832eb4d010SOphir Munk 			 * there should be no matches on representor PCI
19842eb4d010SOphir Munk 			 * functions or non VF LAG bonding devices with
19852eb4d010SOphir Munk 			 * specified address.
19862eb4d010SOphir Munk 			 */
19872eb4d010SOphir Munk 			if (nd) {
19882eb4d010SOphir Munk 				DRV_LOG(ERR,
19892eb4d010SOphir Munk 					"multiple PCI match on bonding device"
19902eb4d010SOphir Munk 					"\"%s\" found", ibv_list[ret]->name);
19912eb4d010SOphir Munk 				rte_errno = ENOENT;
19922eb4d010SOphir Munk 				ret = -rte_errno;
19932eb4d010SOphir Munk 				goto exit;
19942eb4d010SOphir Munk 			}
1995f926cce3SXueming Li 			/* Amend owner pci address if owner PF ID specified. */
1996f926cce3SXueming Li 			if (eth_da.nb_representor_ports)
199708c2772fSXueming Li 				owner_pci.function += owner_id;
1998ca1418ceSMichael Baum 			DRV_LOG(INFO,
1999ca1418ceSMichael Baum 				"PCI information matches for slave %d bonding device \"%s\"",
20002eb4d010SOphir Munk 				bd, ibv_list[ret]->name);
20012eb4d010SOphir Munk 			ibv_match[nd++] = ibv_list[ret];
20022eb4d010SOphir Munk 			break;
2003f926cce3SXueming Li 		} else {
2004f926cce3SXueming Li 			/* Bonding device not found. */
20054d567938SThomas Monjalon 			if (mlx5_get_pci_addr(ibv_list[ret]->ibdev_path,
20064d567938SThomas Monjalon 					      &pci_addr))
20072eb4d010SOphir Munk 				continue;
2008f926cce3SXueming Li 			if (owner_pci.domain != pci_addr.domain ||
2009f926cce3SXueming Li 			    owner_pci.bus != pci_addr.bus ||
2010f926cce3SXueming Li 			    owner_pci.devid != pci_addr.devid ||
2011f926cce3SXueming Li 			    owner_pci.function != pci_addr.function)
20122eb4d010SOphir Munk 				continue;
20132eb4d010SOphir Munk 			DRV_LOG(INFO, "PCI information matches for device \"%s\"",
20142eb4d010SOphir Munk 				ibv_list[ret]->name);
20152eb4d010SOphir Munk 			ibv_match[nd++] = ibv_list[ret];
20162eb4d010SOphir Munk 		}
2017f926cce3SXueming Li 	}
20182eb4d010SOphir Munk 	ibv_match[nd] = NULL;
20192eb4d010SOphir Munk 	if (!nd) {
20202eb4d010SOphir Munk 		/* No device matches, just complain and bail out. */
20212eb4d010SOphir Munk 		DRV_LOG(WARNING,
2022f956d3d4SRongwei Liu 			"PF %u doesn't have Verbs device matches PCI device " PCI_PRI_FMT ","
20232eb4d010SOphir Munk 			" are kernel drivers loaded?",
2024f956d3d4SRongwei Liu 			owner_id, owner_pci.domain, owner_pci.bus,
2025f926cce3SXueming Li 			owner_pci.devid, owner_pci.function);
20262eb4d010SOphir Munk 		rte_errno = ENOENT;
20272eb4d010SOphir Munk 		ret = -rte_errno;
20282eb4d010SOphir Munk 		goto exit;
20292eb4d010SOphir Munk 	}
20302eb4d010SOphir Munk 	if (nd == 1) {
20312eb4d010SOphir Munk 		/*
20322eb4d010SOphir Munk 		 * Found single matching device may have multiple ports.
20332eb4d010SOphir Munk 		 * Each port may be representor, we have to check the port
20342eb4d010SOphir Munk 		 * number and check the representors existence.
20352eb4d010SOphir Munk 		 */
20362eb4d010SOphir Munk 		if (nl_rdma >= 0)
20372eb4d010SOphir Munk 			np = mlx5_nl_portnum(nl_rdma, ibv_match[0]->name);
20382eb4d010SOphir Munk 		if (!np)
2039887183efSMichael Baum 			DRV_LOG(WARNING,
2040887183efSMichael Baum 				"Cannot get IB device \"%s\" ports number.",
2041887183efSMichael Baum 				ibv_match[0]->name);
20422eb4d010SOphir Munk 		if (bd >= 0 && !np) {
2043887183efSMichael Baum 			DRV_LOG(ERR, "Cannot get ports for bonding device.");
20442eb4d010SOphir Munk 			rte_errno = ENOENT;
20452eb4d010SOphir Munk 			ret = -rte_errno;
20462eb4d010SOphir Munk 			goto exit;
20472eb4d010SOphir Munk 		}
20482eb4d010SOphir Munk 	}
2049887183efSMichael Baum 	/* Now we can determine the maximal amount of devices to be spawned. */
20502175c4dcSSuanming Mou 	list = mlx5_malloc(MLX5_MEM_ZERO,
2051887183efSMichael Baum 			   sizeof(struct mlx5_dev_spawn_data) * (np ? np : nd),
20522175c4dcSSuanming Mou 			   RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
20532eb4d010SOphir Munk 	if (!list) {
2054887183efSMichael Baum 		DRV_LOG(ERR, "Spawn data array allocation failure.");
20552eb4d010SOphir Munk 		rte_errno = ENOMEM;
20562eb4d010SOphir Munk 		ret = -rte_errno;
20572eb4d010SOphir Munk 		goto exit;
20582eb4d010SOphir Munk 	}
20592eb4d010SOphir Munk 	if (bd >= 0 || np > 1) {
20602eb4d010SOphir Munk 		/*
20612eb4d010SOphir Munk 		 * Single IB device with multiple ports found,
20622eb4d010SOphir Munk 		 * it may be E-Switch master device and representors.
20632eb4d010SOphir Munk 		 * We have to perform identification through the ports.
20642eb4d010SOphir Munk 		 */
20652eb4d010SOphir Munk 		MLX5_ASSERT(nl_rdma >= 0);
20662eb4d010SOphir Munk 		MLX5_ASSERT(ns == 0);
20672eb4d010SOphir Munk 		MLX5_ASSERT(nd == 1);
20682eb4d010SOphir Munk 		MLX5_ASSERT(np);
20692eb4d010SOphir Munk 		for (i = 1; i <= np; ++i) {
2070f5f4c482SXueming Li 			list[ns].bond_info = &bond_info;
20712eb4d010SOphir Munk 			list[ns].max_port = np;
2072834a9019SOphir Munk 			list[ns].phys_port = i;
2073887183efSMichael Baum 			list[ns].phys_dev_name = ibv_match[0]->name;
20742eb4d010SOphir Munk 			list[ns].eth_dev = NULL;
20752eb4d010SOphir Munk 			list[ns].pci_dev = pci_dev;
20767af08c8fSMichael Baum 			list[ns].cdev = cdev;
20772eb4d010SOphir Munk 			list[ns].pf_bond = bd;
2078887183efSMichael Baum 			list[ns].ifindex = mlx5_nl_ifindex(nl_rdma,
2079887183efSMichael Baum 							   ibv_match[0]->name,
2080887183efSMichael Baum 							   i);
20812eb4d010SOphir Munk 			if (!list[ns].ifindex) {
20822eb4d010SOphir Munk 				/*
20832eb4d010SOphir Munk 				 * No network interface index found for the
20842eb4d010SOphir Munk 				 * specified port, it means there is no
20852eb4d010SOphir Munk 				 * representor on this port. It's OK,
20862eb4d010SOphir Munk 				 * there can be disabled ports, for example
20872eb4d010SOphir Munk 				 * if sriov_numvfs < sriov_totalvfs.
20882eb4d010SOphir Munk 				 */
20892eb4d010SOphir Munk 				continue;
20902eb4d010SOphir Munk 			}
20912eb4d010SOphir Munk 			ret = -1;
20922eb4d010SOphir Munk 			if (nl_route >= 0)
2093887183efSMichael Baum 				ret = mlx5_nl_switch_info(nl_route,
20942eb4d010SOphir Munk 							  list[ns].ifindex,
20952eb4d010SOphir Munk 							  &list[ns].info);
20962eb4d010SOphir Munk 			if (ret || (!list[ns].info.representor &&
20972eb4d010SOphir Munk 				    !list[ns].info.master)) {
20982eb4d010SOphir Munk 				/*
20992eb4d010SOphir Munk 				 * We failed to recognize representors with
21002eb4d010SOphir Munk 				 * Netlink, let's try to perform the task
21012eb4d010SOphir Munk 				 * with sysfs.
21022eb4d010SOphir Munk 				 */
2103887183efSMichael Baum 				ret = mlx5_sysfs_switch_info(list[ns].ifindex,
21042eb4d010SOphir Munk 							     &list[ns].info);
21052eb4d010SOphir Munk 			}
21062eb4d010SOphir Munk 			if (!ret && bd >= 0) {
21072eb4d010SOphir Munk 				switch (list[ns].info.name_type) {
21082eb4d010SOphir Munk 				case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
21099f430dd7SViacheslav Ovsiienko 					if (np == 1) {
21109f430dd7SViacheslav Ovsiienko 						/*
21119f430dd7SViacheslav Ovsiienko 						 * Force standalone bonding
21129f430dd7SViacheslav Ovsiienko 						 * device for ROCE LAG
21137be78d02SJosh Soref 						 * configurations.
21149f430dd7SViacheslav Ovsiienko 						 */
21159f430dd7SViacheslav Ovsiienko 						list[ns].info.master = 0;
21169f430dd7SViacheslav Ovsiienko 						list[ns].info.representor = 0;
21179f430dd7SViacheslav Ovsiienko 					}
21182eb4d010SOphir Munk 					if (list[ns].info.port_name == bd)
21192eb4d010SOphir Munk 						ns++;
21202eb4d010SOphir Munk 					break;
2121420bbdaeSViacheslav Ovsiienko 				case MLX5_PHYS_PORT_NAME_TYPE_PFHPF:
2122420bbdaeSViacheslav Ovsiienko 					/* Fallthrough */
21232eb4d010SOphir Munk 				case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
2124cb95feefSXueming Li 					/* Fallthrough */
2125cb95feefSXueming Li 				case MLX5_PHYS_PORT_NAME_TYPE_PFSF:
21262eb4d010SOphir Munk 					if (list[ns].info.pf_num == bd)
21272eb4d010SOphir Munk 						ns++;
21282eb4d010SOphir Munk 					break;
21292eb4d010SOphir Munk 				default:
21302eb4d010SOphir Munk 					break;
21312eb4d010SOphir Munk 				}
21322eb4d010SOphir Munk 				continue;
21332eb4d010SOphir Munk 			}
21342eb4d010SOphir Munk 			if (!ret && (list[ns].info.representor ^
21352eb4d010SOphir Munk 				     list[ns].info.master))
21362eb4d010SOphir Munk 				ns++;
21372eb4d010SOphir Munk 		}
21382eb4d010SOphir Munk 		if (!ns) {
21392eb4d010SOphir Munk 			DRV_LOG(ERR,
2140887183efSMichael Baum 				"Unable to recognize master/representors on the IB device with multiple ports.");
21412eb4d010SOphir Munk 			rte_errno = ENOENT;
21422eb4d010SOphir Munk 			ret = -rte_errno;
21432eb4d010SOphir Munk 			goto exit;
21442eb4d010SOphir Munk 		}
21452eb4d010SOphir Munk 	} else {
21462eb4d010SOphir Munk 		/*
21472eb4d010SOphir Munk 		 * The existence of several matching entries (nd > 1) means
21482eb4d010SOphir Munk 		 * port representors have been instantiated. No existing Verbs
21492eb4d010SOphir Munk 		 * call nor sysfs entries can tell them apart, this can only
21502eb4d010SOphir Munk 		 * be done through Netlink calls assuming kernel drivers are
21512eb4d010SOphir Munk 		 * recent enough to support them.
21522eb4d010SOphir Munk 		 *
21532eb4d010SOphir Munk 		 * In the event of identification failure through Netlink,
21542eb4d010SOphir Munk 		 * try again through sysfs, then:
21552eb4d010SOphir Munk 		 *
21562eb4d010SOphir Munk 		 * 1. A single IB device matches (nd == 1) with single
21572eb4d010SOphir Munk 		 *    port (np=0/1) and is not a representor, assume
21582eb4d010SOphir Munk 		 *    no switch support.
21592eb4d010SOphir Munk 		 *
21602eb4d010SOphir Munk 		 * 2. Otherwise no safe assumptions can be made;
21612eb4d010SOphir Munk 		 *    complain louder and bail out.
21622eb4d010SOphir Munk 		 */
21632eb4d010SOphir Munk 		for (i = 0; i != nd; ++i) {
21642eb4d010SOphir Munk 			memset(&list[ns].info, 0, sizeof(list[ns].info));
2165f5f4c482SXueming Li 			list[ns].bond_info = NULL;
21662eb4d010SOphir Munk 			list[ns].max_port = 1;
2167834a9019SOphir Munk 			list[ns].phys_port = 1;
2168887183efSMichael Baum 			list[ns].phys_dev_name = ibv_match[i]->name;
21692eb4d010SOphir Munk 			list[ns].eth_dev = NULL;
21702eb4d010SOphir Munk 			list[ns].pci_dev = pci_dev;
21717af08c8fSMichael Baum 			list[ns].cdev = cdev;
21722eb4d010SOphir Munk 			list[ns].pf_bond = -1;
21732eb4d010SOphir Munk 			list[ns].ifindex = 0;
21742eb4d010SOphir Munk 			if (nl_rdma >= 0)
21752eb4d010SOphir Munk 				list[ns].ifindex = mlx5_nl_ifindex
2176834a9019SOphir Munk 							    (nl_rdma,
2177887183efSMichael Baum 							     ibv_match[i]->name,
2178887183efSMichael Baum 							     1);
21792eb4d010SOphir Munk 			if (!list[ns].ifindex) {
21802eb4d010SOphir Munk 				char ifname[IF_NAMESIZE];
21812eb4d010SOphir Munk 
21822eb4d010SOphir Munk 				/*
21832eb4d010SOphir Munk 				 * Netlink failed, it may happen with old
21842eb4d010SOphir Munk 				 * ib_core kernel driver (before 4.16).
21852eb4d010SOphir Munk 				 * We can assume there is old driver because
21862eb4d010SOphir Munk 				 * here we are processing single ports IB
21872eb4d010SOphir Munk 				 * devices. Let's try sysfs to retrieve
21882eb4d010SOphir Munk 				 * the ifindex. The method works for
21892eb4d010SOphir Munk 				 * master device only.
21902eb4d010SOphir Munk 				 */
21912eb4d010SOphir Munk 				if (nd > 1) {
21922eb4d010SOphir Munk 					/*
21932eb4d010SOphir Munk 					 * Multiple devices found, assume
21942eb4d010SOphir Munk 					 * representors, can not distinguish
21952eb4d010SOphir Munk 					 * master/representor and retrieve
21962eb4d010SOphir Munk 					 * ifindex via sysfs.
21972eb4d010SOphir Munk 					 */
21982eb4d010SOphir Munk 					continue;
21992eb4d010SOphir Munk 				}
2200aec086c9SMatan Azrad 				ret = mlx5_get_ifname_sysfs
2201aec086c9SMatan Azrad 					(ibv_match[i]->ibdev_path, ifname);
22022eb4d010SOphir Munk 				if (!ret)
22032eb4d010SOphir Munk 					list[ns].ifindex =
22042eb4d010SOphir Munk 						if_nametoindex(ifname);
22052eb4d010SOphir Munk 				if (!list[ns].ifindex) {
22062eb4d010SOphir Munk 					/*
22072eb4d010SOphir Munk 					 * No network interface index found
22082eb4d010SOphir Munk 					 * for the specified device, it means
22092eb4d010SOphir Munk 					 * there it is neither representor
22102eb4d010SOphir Munk 					 * nor master.
22112eb4d010SOphir Munk 					 */
22122eb4d010SOphir Munk 					continue;
22132eb4d010SOphir Munk 				}
22142eb4d010SOphir Munk 			}
22152eb4d010SOphir Munk 			ret = -1;
22162eb4d010SOphir Munk 			if (nl_route >= 0)
2217ca1418ceSMichael Baum 				ret = mlx5_nl_switch_info(nl_route,
22182eb4d010SOphir Munk 							  list[ns].ifindex,
22192eb4d010SOphir Munk 							  &list[ns].info);
22202eb4d010SOphir Munk 			if (ret || (!list[ns].info.representor &&
22212eb4d010SOphir Munk 				    !list[ns].info.master)) {
22222eb4d010SOphir Munk 				/*
22232eb4d010SOphir Munk 				 * We failed to recognize representors with
22242eb4d010SOphir Munk 				 * Netlink, let's try to perform the task
22252eb4d010SOphir Munk 				 * with sysfs.
22262eb4d010SOphir Munk 				 */
2227887183efSMichael Baum 				ret = mlx5_sysfs_switch_info(list[ns].ifindex,
22282eb4d010SOphir Munk 							     &list[ns].info);
22292eb4d010SOphir Munk 			}
22302eb4d010SOphir Munk 			if (!ret && (list[ns].info.representor ^
22312eb4d010SOphir Munk 				     list[ns].info.master)) {
22322eb4d010SOphir Munk 				ns++;
22332eb4d010SOphir Munk 			} else if ((nd == 1) &&
22342eb4d010SOphir Munk 				   !list[ns].info.representor &&
22352eb4d010SOphir Munk 				   !list[ns].info.master) {
22362eb4d010SOphir Munk 				/*
2237887183efSMichael Baum 				 * Single IB device with one physical port and
22382eb4d010SOphir Munk 				 * attached network device.
2239887183efSMichael Baum 				 * May be SRIOV is not enabled or there is no
2240887183efSMichael Baum 				 * representors.
22412eb4d010SOphir Munk 				 */
2242887183efSMichael Baum 				DRV_LOG(INFO, "No E-Switch support detected.");
22432eb4d010SOphir Munk 				ns++;
22442eb4d010SOphir Munk 				break;
22452eb4d010SOphir Munk 			}
22462eb4d010SOphir Munk 		}
22472eb4d010SOphir Munk 		if (!ns) {
22482eb4d010SOphir Munk 			DRV_LOG(ERR,
2249887183efSMichael Baum 				"Unable to recognize master/representors on the multiple IB devices.");
22502eb4d010SOphir Munk 			rte_errno = ENOENT;
22512eb4d010SOphir Munk 			ret = -rte_errno;
22522eb4d010SOphir Munk 			goto exit;
22532eb4d010SOphir Munk 		}
22546b157f3bSViacheslav Ovsiienko 		/*
22556b157f3bSViacheslav Ovsiienko 		 * New kernels may add the switch_id attribute for the case
2256ca1418ceSMichael Baum 		 * there is no E-Switch and we wrongly recognized the only
2257ca1418ceSMichael Baum 		 * device as master. Override this if there is the single
2258ca1418ceSMichael Baum 		 * device with single port and new device name format present.
22596b157f3bSViacheslav Ovsiienko 		 */
22606b157f3bSViacheslav Ovsiienko 		if (nd == 1 &&
22616b157f3bSViacheslav Ovsiienko 		    list[0].info.name_type == MLX5_PHYS_PORT_NAME_TYPE_UPLINK) {
22626b157f3bSViacheslav Ovsiienko 			list[0].info.master = 0;
22636b157f3bSViacheslav Ovsiienko 			list[0].info.representor = 0;
22646b157f3bSViacheslav Ovsiienko 		}
22652eb4d010SOphir Munk 	}
22662eb4d010SOphir Munk 	MLX5_ASSERT(ns);
22672eb4d010SOphir Munk 	/*
22682eb4d010SOphir Munk 	 * Sort list to probe devices in natural order for users convenience
22692eb4d010SOphir Munk 	 * (i.e. master first, then representors from lowest to highest ID).
22702eb4d010SOphir Munk 	 */
22712eb4d010SOphir Munk 	qsort(list, ns, sizeof(*list), mlx5_dev_spawn_data_cmp);
2272f926cce3SXueming Li 	if (eth_da.type != RTE_ETH_REPRESENTOR_NONE) {
2273f926cce3SXueming Li 		/* Set devargs default values. */
2274f926cce3SXueming Li 		if (eth_da.nb_mh_controllers == 0) {
2275f926cce3SXueming Li 			eth_da.nb_mh_controllers = 1;
2276f926cce3SXueming Li 			eth_da.mh_controllers[0] = 0;
2277f926cce3SXueming Li 		}
2278f926cce3SXueming Li 		if (eth_da.nb_ports == 0 && ns > 0) {
2279f926cce3SXueming Li 			if (list[0].pf_bond >= 0 && list[0].info.representor)
2280f926cce3SXueming Li 				DRV_LOG(WARNING, "Representor on Bonding device should use pf#vf# syntax: %s",
2281f926cce3SXueming Li 					pci_dev->device.devargs->args);
2282f926cce3SXueming Li 			eth_da.nb_ports = 1;
2283f926cce3SXueming Li 			eth_da.ports[0] = list[0].info.pf_num;
2284f926cce3SXueming Li 		}
2285f926cce3SXueming Li 		if (eth_da.nb_representor_ports == 0) {
2286f926cce3SXueming Li 			eth_da.nb_representor_ports = 1;
2287f926cce3SXueming Li 			eth_da.representor_ports[0] = 0;
2288f926cce3SXueming Li 		}
2289f926cce3SXueming Li 	}
22902eb4d010SOphir Munk 	for (i = 0; i != ns; ++i) {
22912eb4d010SOphir Munk 		uint32_t restore;
22922eb4d010SOphir Munk 
2293a729d2f0SMichael Baum 		list[i].eth_dev = mlx5_dev_spawn(cdev->dev, &list[i], &eth_da,
2294a729d2f0SMichael Baum 						 mkvlist);
22952eb4d010SOphir Munk 		if (!list[i].eth_dev) {
22962eb4d010SOphir Munk 			if (rte_errno != EBUSY && rte_errno != EEXIST)
22972eb4d010SOphir Munk 				break;
22982eb4d010SOphir Munk 			/* Device is disabled or already spawned. Ignore it. */
22992eb4d010SOphir Munk 			continue;
23002eb4d010SOphir Munk 		}
23012eb4d010SOphir Munk 		restore = list[i].eth_dev->data->dev_flags;
23022eb4d010SOphir Munk 		rte_eth_copy_pci_info(list[i].eth_dev, pci_dev);
2303494d6863SGregory Etelson 		/**
2304494d6863SGregory Etelson 		 * Each representor has a dedicated interrupts vector.
2305494d6863SGregory Etelson 		 * rte_eth_copy_pci_info() assigns PF interrupts handle to
2306494d6863SGregory Etelson 		 * representor eth_dev object because representor and PF
2307494d6863SGregory Etelson 		 * share the same PCI address.
2308494d6863SGregory Etelson 		 * Override representor device with a dedicated
2309494d6863SGregory Etelson 		 * interrupts handle here.
2310494d6863SGregory Etelson 		 * Representor interrupts handle is released in mlx5_dev_stop().
2311494d6863SGregory Etelson 		 */
2312494d6863SGregory Etelson 		if (list[i].info.representor) {
2313d61138d4SHarman Kalra 			struct rte_intr_handle *intr_handle =
2314d61138d4SHarman Kalra 				rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_SHARED);
2315d61138d4SHarman Kalra 			if (intr_handle == NULL) {
2316494d6863SGregory Etelson 				DRV_LOG(ERR,
2317494d6863SGregory Etelson 					"port %u failed to allocate memory for interrupt handler "
2318494d6863SGregory Etelson 					"Rx interrupts will not be supported",
2319494d6863SGregory Etelson 					i);
2320494d6863SGregory Etelson 				rte_errno = ENOMEM;
2321494d6863SGregory Etelson 				ret = -rte_errno;
2322494d6863SGregory Etelson 				goto exit;
2323494d6863SGregory Etelson 			}
2324494d6863SGregory Etelson 			list[i].eth_dev->intr_handle = intr_handle;
2325494d6863SGregory Etelson 		}
23262eb4d010SOphir Munk 		/* Restore non-PCI flags cleared by the above call. */
23272eb4d010SOphir Munk 		list[i].eth_dev->data->dev_flags |= restore;
23282eb4d010SOphir Munk 		rte_eth_dev_probing_finish(list[i].eth_dev);
23292eb4d010SOphir Munk 	}
23302eb4d010SOphir Munk 	if (i != ns) {
23312eb4d010SOphir Munk 		DRV_LOG(ERR,
23322eb4d010SOphir Munk 			"probe of PCI device " PCI_PRI_FMT " aborted after"
23332eb4d010SOphir Munk 			" encountering an error: %s",
2334f926cce3SXueming Li 			owner_pci.domain, owner_pci.bus,
2335f926cce3SXueming Li 			owner_pci.devid, owner_pci.function,
23362eb4d010SOphir Munk 			strerror(rte_errno));
23372eb4d010SOphir Munk 		ret = -rte_errno;
23382eb4d010SOphir Munk 		/* Roll back. */
23392eb4d010SOphir Munk 		while (i--) {
23402eb4d010SOphir Munk 			if (!list[i].eth_dev)
23412eb4d010SOphir Munk 				continue;
23422eb4d010SOphir Munk 			mlx5_dev_close(list[i].eth_dev);
23432eb4d010SOphir Munk 			/* mac_addrs must not be freed because in dev_private */
23442eb4d010SOphir Munk 			list[i].eth_dev->data->mac_addrs = NULL;
23452eb4d010SOphir Munk 			claim_zero(rte_eth_dev_release_port(list[i].eth_dev));
23462eb4d010SOphir Munk 		}
23472eb4d010SOphir Munk 		/* Restore original error. */
23482eb4d010SOphir Munk 		rte_errno = -ret;
23492eb4d010SOphir Munk 	} else {
23502eb4d010SOphir Munk 		ret = 0;
23512eb4d010SOphir Munk 	}
23522eb4d010SOphir Munk exit:
23532eb4d010SOphir Munk 	/*
23542eb4d010SOphir Munk 	 * Do the routine cleanup:
23552eb4d010SOphir Munk 	 * - close opened Netlink sockets
23562eb4d010SOphir Munk 	 * - free allocated spawn data array
23572eb4d010SOphir Munk 	 * - free the Infiniband device list
23582eb4d010SOphir Munk 	 */
23592eb4d010SOphir Munk 	if (nl_rdma >= 0)
23602eb4d010SOphir Munk 		close(nl_rdma);
23612eb4d010SOphir Munk 	if (nl_route >= 0)
23622eb4d010SOphir Munk 		close(nl_route);
23632eb4d010SOphir Munk 	if (list)
23642175c4dcSSuanming Mou 		mlx5_free(list);
23652eb4d010SOphir Munk 	MLX5_ASSERT(ibv_list);
23662eb4d010SOphir Munk 	mlx5_glue->free_device_list(ibv_list);
23672eb4d010SOphir Munk 	return ret;
23682eb4d010SOphir Munk }
23692eb4d010SOphir Munk 
2370919488fbSXueming Li static int
2371919488fbSXueming Li mlx5_os_parse_eth_devargs(struct rte_device *dev,
2372919488fbSXueming Li 			  struct rte_eth_devargs *eth_da)
2373919488fbSXueming Li {
2374919488fbSXueming Li 	int ret = 0;
2375919488fbSXueming Li 
2376919488fbSXueming Li 	if (dev->devargs == NULL)
2377919488fbSXueming Li 		return 0;
2378919488fbSXueming Li 	memset(eth_da, 0, sizeof(*eth_da));
2379919488fbSXueming Li 	/* Parse representor information first from class argument. */
2380919488fbSXueming Li 	if (dev->devargs->cls_str)
2381919488fbSXueming Li 		ret = rte_eth_devargs_parse(dev->devargs->cls_str, eth_da);
2382919488fbSXueming Li 	if (ret != 0) {
2383919488fbSXueming Li 		DRV_LOG(ERR, "failed to parse device arguments: %s",
2384919488fbSXueming Li 			dev->devargs->cls_str);
2385919488fbSXueming Li 		return -rte_errno;
2386919488fbSXueming Li 	}
2387919488fbSXueming Li 	if (eth_da->type == RTE_ETH_REPRESENTOR_NONE) {
2388919488fbSXueming Li 		/* Parse legacy device argument */
2389919488fbSXueming Li 		ret = rte_eth_devargs_parse(dev->devargs->args, eth_da);
2390919488fbSXueming Li 		if (ret) {
2391919488fbSXueming Li 			DRV_LOG(ERR, "failed to parse device arguments: %s",
2392919488fbSXueming Li 				dev->devargs->args);
2393919488fbSXueming Li 			return -rte_errno;
2394919488fbSXueming Li 		}
2395919488fbSXueming Li 	}
2396919488fbSXueming Li 	return 0;
2397919488fbSXueming Li }
2398919488fbSXueming Li 
239908c2772fSXueming Li /**
2400a7f34989SXueming Li  * Callback to register a PCI device.
240108c2772fSXueming Li  *
240208c2772fSXueming Li  * This function spawns Ethernet devices out of a given PCI device.
240308c2772fSXueming Li  *
24047af08c8fSMichael Baum  * @param[in] cdev
24057af08c8fSMichael Baum  *   Pointer to common mlx5 device structure.
2406a729d2f0SMichael Baum  * @param[in, out] mkvlist
2407a729d2f0SMichael Baum  *   Pointer to mlx5 kvargs control, can be NULL if there is no devargs.
240808c2772fSXueming Li  *
240908c2772fSXueming Li  * @return
241008c2772fSXueming Li  *   0 on success, a negative errno value otherwise and rte_errno is set.
241108c2772fSXueming Li  */
2412a7f34989SXueming Li static int
2413a729d2f0SMichael Baum mlx5_os_pci_probe(struct mlx5_common_device *cdev,
2414a729d2f0SMichael Baum 		  struct mlx5_kvargs_ctrl *mkvlist)
241508c2772fSXueming Li {
24167af08c8fSMichael Baum 	struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(cdev->dev);
2417919488fbSXueming Li 	struct rte_eth_devargs eth_da = { .nb_ports = 0 };
241808c2772fSXueming Li 	int ret = 0;
241908c2772fSXueming Li 	uint16_t p;
242008c2772fSXueming Li 
24217af08c8fSMichael Baum 	ret = mlx5_os_parse_eth_devargs(cdev->dev, &eth_da);
2422919488fbSXueming Li 	if (ret != 0)
2423919488fbSXueming Li 		return ret;
242408c2772fSXueming Li 
242508c2772fSXueming Li 	if (eth_da.nb_ports > 0) {
242608c2772fSXueming Li 		/* Iterate all port if devargs pf is range: "pf[0-1]vf[...]". */
24276856efa5SMichael Baum 		for (p = 0; p < eth_da.nb_ports; p++) {
2428ca1418ceSMichael Baum 			ret = mlx5_os_pci_probe_pf(cdev, &eth_da,
2429a729d2f0SMichael Baum 						   eth_da.ports[p], mkvlist);
24306856efa5SMichael Baum 			if (ret) {
2431f956d3d4SRongwei Liu 				DRV_LOG(INFO, "Probe of PCI device " PCI_PRI_FMT " "
2432f956d3d4SRongwei Liu 					"aborted due to proding failure of PF %u",
24336856efa5SMichael Baum 					pci_dev->addr.domain, pci_dev->addr.bus,
24346856efa5SMichael Baum 					pci_dev->addr.devid, pci_dev->addr.function,
24356856efa5SMichael Baum 					eth_da.ports[p]);
24367af08c8fSMichael Baum 				mlx5_net_remove(cdev);
2437f956d3d4SRongwei Liu 				if (p != 0)
2438f956d3d4SRongwei Liu 					break;
2439f956d3d4SRongwei Liu 			}
24406856efa5SMichael Baum 		}
244108c2772fSXueming Li 	} else {
2442a729d2f0SMichael Baum 		ret = mlx5_os_pci_probe_pf(cdev, &eth_da, 0, mkvlist);
244308c2772fSXueming Li 	}
244408c2772fSXueming Li 	return ret;
244508c2772fSXueming Li }
244608c2772fSXueming Li 
2447919488fbSXueming Li /* Probe a single SF device on auxiliary bus, no representor support. */
2448919488fbSXueming Li static int
2449a729d2f0SMichael Baum mlx5_os_auxiliary_probe(struct mlx5_common_device *cdev,
2450a729d2f0SMichael Baum 			struct mlx5_kvargs_ctrl *mkvlist)
2451919488fbSXueming Li {
2452919488fbSXueming Li 	struct rte_eth_devargs eth_da = { .nb_ports = 0 };
2453919488fbSXueming Li 	struct mlx5_dev_spawn_data spawn = { .pf_bond = -1 };
24547af08c8fSMichael Baum 	struct rte_device *dev = cdev->dev;
2455919488fbSXueming Li 	struct rte_auxiliary_device *adev = RTE_DEV_TO_AUXILIARY(dev);
2456919488fbSXueming Li 	struct rte_eth_dev *eth_dev;
2457919488fbSXueming Li 	int ret = 0;
2458919488fbSXueming Li 
2459919488fbSXueming Li 	/* Parse ethdev devargs. */
2460919488fbSXueming Li 	ret = mlx5_os_parse_eth_devargs(dev, &eth_da);
2461919488fbSXueming Li 	if (ret != 0)
2462919488fbSXueming Li 		return ret;
2463919488fbSXueming Li 	/* Init spawn data. */
2464919488fbSXueming Li 	spawn.max_port = 1;
2465919488fbSXueming Li 	spawn.phys_port = 1;
2466ca1418ceSMichael Baum 	spawn.phys_dev_name = mlx5_os_get_ctx_device_name(cdev->ctx);
2467919488fbSXueming Li 	ret = mlx5_auxiliary_get_ifindex(dev->name);
2468919488fbSXueming Li 	if (ret < 0) {
2469919488fbSXueming Li 		DRV_LOG(ERR, "failed to get ethdev ifindex: %s", dev->name);
2470919488fbSXueming Li 		return ret;
2471919488fbSXueming Li 	}
2472919488fbSXueming Li 	spawn.ifindex = ret;
24737af08c8fSMichael Baum 	spawn.cdev = cdev;
2474919488fbSXueming Li 	/* Spawn device. */
2475a729d2f0SMichael Baum 	eth_dev = mlx5_dev_spawn(dev, &spawn, &eth_da, mkvlist);
2476919488fbSXueming Li 	if (eth_dev == NULL)
2477919488fbSXueming Li 		return -rte_errno;
2478919488fbSXueming Li 	/* Post create. */
2479d61138d4SHarman Kalra 	eth_dev->intr_handle = adev->intr_handle;
2480919488fbSXueming Li 	if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
2481919488fbSXueming Li 		eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
2482919488fbSXueming Li 		eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_RMV;
2483919488fbSXueming Li 		eth_dev->data->numa_node = dev->numa_node;
2484919488fbSXueming Li 	}
2485919488fbSXueming Li 	rte_eth_dev_probing_finish(eth_dev);
2486919488fbSXueming Li 	return 0;
2487919488fbSXueming Li }
2488919488fbSXueming Li 
2489a7f34989SXueming Li /**
2490a7f34989SXueming Li  * Net class driver callback to probe a device.
2491a7f34989SXueming Li  *
2492919488fbSXueming Li  * This function probe PCI bus device(s) or a single SF on auxiliary bus.
2493a7f34989SXueming Li  *
24947af08c8fSMichael Baum  * @param[in] cdev
24957af08c8fSMichael Baum  *   Pointer to the common mlx5 device.
2496a729d2f0SMichael Baum  * @param[in, out] mkvlist
2497a729d2f0SMichael Baum  *   Pointer to mlx5 kvargs control, can be NULL if there is no devargs.
2498a7f34989SXueming Li  *
2499a7f34989SXueming Li  * @return
25007af08c8fSMichael Baum  *   0 on success, a negative errno value otherwise and rte_errno is set.
2501a7f34989SXueming Li  */
2502a7f34989SXueming Li int
2503a729d2f0SMichael Baum mlx5_os_net_probe(struct mlx5_common_device *cdev,
2504a729d2f0SMichael Baum 		  struct mlx5_kvargs_ctrl *mkvlist)
2505a7f34989SXueming Li {
2506a7f34989SXueming Li 	int ret;
2507a7f34989SXueming Li 
2508ca1418ceSMichael Baum 	if (rte_eal_process_type() == RTE_PROC_PRIMARY)
2509a7f34989SXueming Li 		mlx5_pmd_socket_init();
2510a7f34989SXueming Li 	ret = mlx5_init_once();
2511a7f34989SXueming Li 	if (ret) {
25127af08c8fSMichael Baum 		DRV_LOG(ERR, "Unable to init PMD global data: %s",
2513a7f34989SXueming Li 			strerror(rte_errno));
2514a7f34989SXueming Li 		return -rte_errno;
2515a7f34989SXueming Li 	}
2516a729d2f0SMichael Baum 	ret = mlx5_probe_again_args_validate(cdev, mkvlist);
2517a13ec19cSMichael Baum 	if (ret) {
2518a13ec19cSMichael Baum 		DRV_LOG(ERR, "Probe again parameters are not compatible : %s",
2519a13ec19cSMichael Baum 			strerror(rte_errno));
2520a13ec19cSMichael Baum 		return -rte_errno;
2521a13ec19cSMichael Baum 	}
25227af08c8fSMichael Baum 	if (mlx5_dev_is_pci(cdev->dev))
2523a729d2f0SMichael Baum 		return mlx5_os_pci_probe(cdev, mkvlist);
2524919488fbSXueming Li 	else
2525a729d2f0SMichael Baum 		return mlx5_os_auxiliary_probe(cdev, mkvlist);
25262eb4d010SOphir Munk }
25272eb4d010SOphir Munk 
25282eb4d010SOphir Munk /**
2529ea823b2cSDmitry Kozlyuk  * Cleanup resources when the last device is closed.
2530ea823b2cSDmitry Kozlyuk  */
2531ea823b2cSDmitry Kozlyuk void
2532ea823b2cSDmitry Kozlyuk mlx5_os_net_cleanup(void)
2533ea823b2cSDmitry Kozlyuk {
2534ea823b2cSDmitry Kozlyuk 	mlx5_pmd_socket_uninit();
2535ea823b2cSDmitry Kozlyuk }
2536ea823b2cSDmitry Kozlyuk 
2537ea823b2cSDmitry Kozlyuk /**
25382eb4d010SOphir Munk  * Install shared asynchronous device events handler.
25392eb4d010SOphir Munk  * This function is implemented to support event sharing
25402eb4d010SOphir Munk  * between multiple ports of single IB device.
25412eb4d010SOphir Munk  *
25422eb4d010SOphir Munk  * @param sh
25432eb4d010SOphir Munk  *   Pointer to mlx5_dev_ctx_shared object.
25442eb4d010SOphir Munk  */
25452eb4d010SOphir Munk void
25462eb4d010SOphir Munk mlx5_os_dev_shared_handler_install(struct mlx5_dev_ctx_shared *sh)
25472eb4d010SOphir Munk {
2548ca1418ceSMichael Baum 	struct ibv_context *ctx = sh->cdev->ctx;
254972d7efe4SSpike Du 	int nlsk_fd;
25502eb4d010SOphir Munk 
255172d7efe4SSpike Du 	sh->intr_handle = mlx5_os_interrupt_handler_create
255272d7efe4SSpike Du 		(RTE_INTR_INSTANCE_F_SHARED, true,
255372d7efe4SSpike Du 		 ctx->async_fd, mlx5_dev_interrupt_handler, sh);
255472d7efe4SSpike Du 	if (!sh->intr_handle) {
255572d7efe4SSpike Du 		DRV_LOG(ERR, "Failed to allocate intr_handle.");
2556d61138d4SHarman Kalra 		return;
2557d61138d4SHarman Kalra 	}
255872d7efe4SSpike Du 	nlsk_fd = mlx5_nl_init(NETLINK_ROUTE, RTMGRP_LINK);
255972d7efe4SSpike Du 	if (nlsk_fd < 0) {
256072d7efe4SSpike Du 		DRV_LOG(ERR, "Failed to create a socket for Netlink events: %s",
256172d7efe4SSpike Du 			rte_strerror(rte_errno));
256272d7efe4SSpike Du 		return;
25632eb4d010SOphir Munk 	}
256472d7efe4SSpike Du 	sh->intr_handle_nl = mlx5_os_interrupt_handler_create
256572d7efe4SSpike Du 		(RTE_INTR_INSTANCE_F_SHARED, true,
256672d7efe4SSpike Du 		 nlsk_fd, mlx5_dev_interrupt_handler_nl, sh);
256717f95513SDmitry Kozlyuk 	if (sh->intr_handle_nl == NULL) {
256817f95513SDmitry Kozlyuk 		DRV_LOG(ERR, "Fail to allocate intr_handle");
256917f95513SDmitry Kozlyuk 		return;
257017f95513SDmitry Kozlyuk 	}
25716dc0cbc6SMichael Baum 	if (sh->cdev->config.devx) {
25722eb4d010SOphir Munk #ifdef HAVE_IBV_DEVX_ASYNC
257372d7efe4SSpike Du 		struct mlx5dv_devx_cmd_comp *devx_comp;
257472d7efe4SSpike Du 
2575ca1418ceSMichael Baum 		sh->devx_comp = (void *)mlx5_glue->devx_create_cmd_comp(ctx);
257672d7efe4SSpike Du 		devx_comp = sh->devx_comp;
257721b7c452SOphir Munk 		if (!devx_comp) {
25782eb4d010SOphir Munk 			DRV_LOG(INFO, "failed to allocate devx_comp.");
25792eb4d010SOphir Munk 			return;
25802eb4d010SOphir Munk 		}
258172d7efe4SSpike Du 		sh->intr_handle_devx = mlx5_os_interrupt_handler_create
258272d7efe4SSpike Du 			(RTE_INTR_INSTANCE_F_SHARED, true,
258372d7efe4SSpike Du 			 devx_comp->fd,
258472d7efe4SSpike Du 			 mlx5_dev_interrupt_handler_devx, sh);
258572d7efe4SSpike Du 		if (!sh->intr_handle_devx) {
258672d7efe4SSpike Du 			DRV_LOG(ERR, "Failed to allocate intr_handle.");
25872eb4d010SOphir Munk 			return;
25882eb4d010SOphir Munk 		}
25892eb4d010SOphir Munk #endif /* HAVE_IBV_DEVX_ASYNC */
25902eb4d010SOphir Munk 	}
25912eb4d010SOphir Munk }
25922eb4d010SOphir Munk 
25932eb4d010SOphir Munk /**
25942eb4d010SOphir Munk  * Uninstall shared asynchronous device events handler.
25952eb4d010SOphir Munk  * This function is implemented to support event sharing
25962eb4d010SOphir Munk  * between multiple ports of single IB device.
25972eb4d010SOphir Munk  *
25982eb4d010SOphir Munk  * @param dev
25992eb4d010SOphir Munk  *   Pointer to mlx5_dev_ctx_shared object.
26002eb4d010SOphir Munk  */
26012eb4d010SOphir Munk void
26022eb4d010SOphir Munk mlx5_os_dev_shared_handler_uninstall(struct mlx5_dev_ctx_shared *sh)
26032eb4d010SOphir Munk {
260472d7efe4SSpike Du 	mlx5_os_interrupt_handler_destroy(sh->intr_handle,
26052eb4d010SOphir Munk 					  mlx5_dev_interrupt_handler, sh);
260672d7efe4SSpike Du 	mlx5_os_interrupt_handler_destroy(sh->intr_handle_nl,
260772d7efe4SSpike Du 					  mlx5_dev_interrupt_handler_nl, sh);
26082eb4d010SOphir Munk #ifdef HAVE_IBV_DEVX_ASYNC
260972d7efe4SSpike Du 	mlx5_os_interrupt_handler_destroy(sh->intr_handle_devx,
26102eb4d010SOphir Munk 					  mlx5_dev_interrupt_handler_devx, sh);
26112eb4d010SOphir Munk 	if (sh->devx_comp)
26122eb4d010SOphir Munk 		mlx5_glue->devx_destroy_cmd_comp(sh->devx_comp);
26132eb4d010SOphir Munk #endif
26142eb4d010SOphir Munk }
2615042f5c94SOphir Munk 
261673bf9235SOphir Munk /**
261773bf9235SOphir Munk  * Read statistics by a named counter.
261873bf9235SOphir Munk  *
261973bf9235SOphir Munk  * @param[in] priv
262073bf9235SOphir Munk  *   Pointer to the private device data structure.
262173bf9235SOphir Munk  * @param[in] ctr_name
262273bf9235SOphir Munk  *   Pointer to the name of the statistic counter to read
262373bf9235SOphir Munk  * @param[out] stat
262473bf9235SOphir Munk  *   Pointer to read statistic value.
262573bf9235SOphir Munk  * @return
262673bf9235SOphir Munk  *   0 on success and stat is valud, 1 if failed to read the value
262773bf9235SOphir Munk  *   rte_errno is set.
262873bf9235SOphir Munk  *
262973bf9235SOphir Munk  */
263073bf9235SOphir Munk int
263173bf9235SOphir Munk mlx5_os_read_dev_stat(struct mlx5_priv *priv, const char *ctr_name,
263273bf9235SOphir Munk 		      uint64_t *stat)
263373bf9235SOphir Munk {
263473bf9235SOphir Munk 	int fd;
263573bf9235SOphir Munk 
263673bf9235SOphir Munk 	if (priv->sh) {
2637e6988afdSMatan Azrad 		if (priv->q_counters != NULL &&
2638e6988afdSMatan Azrad 		    strcmp(ctr_name, "out_of_buffer") == 0)
2639978a0303SViacheslav Ovsiienko 			return mlx5_devx_cmd_queue_counter_query
2640978a0303SViacheslav Ovsiienko 					(priv->q_counters, 0, (uint32_t *)stat);
264173bf9235SOphir Munk 		MKSTR(path, "%s/ports/%d/hw_counters/%s",
264273bf9235SOphir Munk 		      priv->sh->ibdev_path,
264373bf9235SOphir Munk 		      priv->dev_port,
264473bf9235SOphir Munk 		      ctr_name);
264573bf9235SOphir Munk 		fd = open(path, O_RDONLY);
2646038e7fc0SShy Shyman 		/*
2647038e7fc0SShy Shyman 		 * in switchdev the file location is not per port
2648038e7fc0SShy Shyman 		 * but rather in <ibdev_path>/hw_counters/<file_name>.
2649038e7fc0SShy Shyman 		 */
2650038e7fc0SShy Shyman 		if (fd == -1) {
2651038e7fc0SShy Shyman 			MKSTR(path1, "%s/hw_counters/%s",
2652038e7fc0SShy Shyman 			      priv->sh->ibdev_path,
2653038e7fc0SShy Shyman 			      ctr_name);
2654038e7fc0SShy Shyman 			fd = open(path1, O_RDONLY);
2655038e7fc0SShy Shyman 		}
265673bf9235SOphir Munk 		if (fd != -1) {
265773bf9235SOphir Munk 			char buf[21] = {'\0'};
265873bf9235SOphir Munk 			ssize_t n = read(fd, buf, sizeof(buf));
265973bf9235SOphir Munk 
266073bf9235SOphir Munk 			close(fd);
266173bf9235SOphir Munk 			if (n != -1) {
266273bf9235SOphir Munk 				*stat = strtoull(buf, NULL, 10);
266373bf9235SOphir Munk 				return 0;
266473bf9235SOphir Munk 			}
266573bf9235SOphir Munk 		}
266673bf9235SOphir Munk 	}
266773bf9235SOphir Munk 	*stat = 0;
266873bf9235SOphir Munk 	return 1;
266973bf9235SOphir Munk }
267073bf9235SOphir Munk 
267173bf9235SOphir Munk /**
2672ab27cdd9SOphir Munk  * Remove a MAC address from device
2673ab27cdd9SOphir Munk  *
2674ab27cdd9SOphir Munk  * @param dev
2675ab27cdd9SOphir Munk  *   Pointer to Ethernet device structure.
2676ab27cdd9SOphir Munk  * @param index
2677ab27cdd9SOphir Munk  *   MAC address index.
2678ab27cdd9SOphir Munk  */
2679ab27cdd9SOphir Munk void
2680ab27cdd9SOphir Munk mlx5_os_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
2681ab27cdd9SOphir Munk {
2682ab27cdd9SOphir Munk 	struct mlx5_priv *priv = dev->data->dev_private;
268387af0d1eSMichael Baum 	const int vf = priv->sh->dev_cap.vf;
2684ab27cdd9SOphir Munk 
2685ab27cdd9SOphir Munk 	if (vf)
2686ab27cdd9SOphir Munk 		mlx5_nl_mac_addr_remove(priv->nl_socket_route,
2687ab27cdd9SOphir Munk 					mlx5_ifindex(dev), priv->mac_own,
2688ab27cdd9SOphir Munk 					&dev->data->mac_addrs[index], index);
2689ab27cdd9SOphir Munk }
2690ab27cdd9SOphir Munk 
2691ab27cdd9SOphir Munk /**
2692ab27cdd9SOphir Munk  * Adds a MAC address to the device
2693ab27cdd9SOphir Munk  *
2694ab27cdd9SOphir Munk  * @param dev
2695ab27cdd9SOphir Munk  *   Pointer to Ethernet device structure.
2696ab27cdd9SOphir Munk  * @param mac_addr
2697ab27cdd9SOphir Munk  *   MAC address to register.
2698ab27cdd9SOphir Munk  * @param index
2699ab27cdd9SOphir Munk  *   MAC address index.
2700ab27cdd9SOphir Munk  *
2701ab27cdd9SOphir Munk  * @return
2702ab27cdd9SOphir Munk  *   0 on success, a negative errno value otherwise
2703ab27cdd9SOphir Munk  */
2704ab27cdd9SOphir Munk int
2705ab27cdd9SOphir Munk mlx5_os_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac,
2706ab27cdd9SOphir Munk 		     uint32_t index)
2707ab27cdd9SOphir Munk {
2708ab27cdd9SOphir Munk 	struct mlx5_priv *priv = dev->data->dev_private;
270987af0d1eSMichael Baum 	const int vf = priv->sh->dev_cap.vf;
2710ab27cdd9SOphir Munk 	int ret = 0;
2711ab27cdd9SOphir Munk 
2712ab27cdd9SOphir Munk 	if (vf)
2713ab27cdd9SOphir Munk 		ret = mlx5_nl_mac_addr_add(priv->nl_socket_route,
2714ab27cdd9SOphir Munk 					   mlx5_ifindex(dev), priv->mac_own,
2715ab27cdd9SOphir Munk 					   mac, index);
2716ab27cdd9SOphir Munk 	return ret;
2717ab27cdd9SOphir Munk }
2718ab27cdd9SOphir Munk 
2719ab27cdd9SOphir Munk /**
2720ab27cdd9SOphir Munk  * Modify a VF MAC address
2721ab27cdd9SOphir Munk  *
2722ab27cdd9SOphir Munk  * @param priv
2723ab27cdd9SOphir Munk  *   Pointer to device private data.
2724ab27cdd9SOphir Munk  * @param mac_addr
2725ab27cdd9SOphir Munk  *   MAC address to modify into.
2726ab27cdd9SOphir Munk  * @param iface_idx
2727ab27cdd9SOphir Munk  *   Net device interface index
2728ab27cdd9SOphir Munk  * @param vf_index
2729ab27cdd9SOphir Munk  *   VF index
2730ab27cdd9SOphir Munk  *
2731ab27cdd9SOphir Munk  * @return
2732ab27cdd9SOphir Munk  *   0 on success, a negative errno value otherwise
2733ab27cdd9SOphir Munk  */
2734ab27cdd9SOphir Munk int
2735ab27cdd9SOphir Munk mlx5_os_vf_mac_addr_modify(struct mlx5_priv *priv,
2736ab27cdd9SOphir Munk 			   unsigned int iface_idx,
2737ab27cdd9SOphir Munk 			   struct rte_ether_addr *mac_addr,
2738ab27cdd9SOphir Munk 			   int vf_index)
2739ab27cdd9SOphir Munk {
2740ab27cdd9SOphir Munk 	return mlx5_nl_vf_mac_addr_modify
2741ab27cdd9SOphir Munk 		(priv->nl_socket_route, iface_idx, mac_addr, vf_index);
2742ab27cdd9SOphir Munk }
2743ab27cdd9SOphir Munk 
27444d18abd1SOphir Munk /**
27454d18abd1SOphir Munk  * Set device promiscuous mode
27464d18abd1SOphir Munk  *
27474d18abd1SOphir Munk  * @param dev
27484d18abd1SOphir Munk  *   Pointer to Ethernet device structure.
27494d18abd1SOphir Munk  * @param enable
27504d18abd1SOphir Munk  *   0 - promiscuous is disabled, otherwise - enabled
27514d18abd1SOphir Munk  *
27524d18abd1SOphir Munk  * @return
27534d18abd1SOphir Munk  *   0 on success, a negative error value otherwise
27544d18abd1SOphir Munk  */
27554d18abd1SOphir Munk int
27564d18abd1SOphir Munk mlx5_os_set_promisc(struct rte_eth_dev *dev, int enable)
27574d18abd1SOphir Munk {
27584d18abd1SOphir Munk 	struct mlx5_priv *priv = dev->data->dev_private;
27594d18abd1SOphir Munk 
27604d18abd1SOphir Munk 	return mlx5_nl_promisc(priv->nl_socket_route,
27614d18abd1SOphir Munk 			       mlx5_ifindex(dev), !!enable);
27624d18abd1SOphir Munk }
27634d18abd1SOphir Munk 
27644d18abd1SOphir Munk /**
27654d18abd1SOphir Munk  * Set device promiscuous mode
27664d18abd1SOphir Munk  *
27674d18abd1SOphir Munk  * @param dev
27684d18abd1SOphir Munk  *   Pointer to Ethernet device structure.
27694d18abd1SOphir Munk  * @param enable
27704d18abd1SOphir Munk  *   0 - all multicase is disabled, otherwise - enabled
27714d18abd1SOphir Munk  *
27724d18abd1SOphir Munk  * @return
27734d18abd1SOphir Munk  *   0 on success, a negative error value otherwise
27744d18abd1SOphir Munk  */
27754d18abd1SOphir Munk int
27764d18abd1SOphir Munk mlx5_os_set_allmulti(struct rte_eth_dev *dev, int enable)
27774d18abd1SOphir Munk {
27784d18abd1SOphir Munk 	struct mlx5_priv *priv = dev->data->dev_private;
27794d18abd1SOphir Munk 
27804d18abd1SOphir Munk 	return mlx5_nl_allmulti(priv->nl_socket_route,
27814d18abd1SOphir Munk 				mlx5_ifindex(dev), !!enable);
27824d18abd1SOphir Munk }
27834d18abd1SOphir Munk 
2784f00f6562SOphir Munk /**
2785f00f6562SOphir Munk  * Flush device MAC addresses
2786f00f6562SOphir Munk  *
2787f00f6562SOphir Munk  * @param dev
2788f00f6562SOphir Munk  *   Pointer to Ethernet device structure.
2789f00f6562SOphir Munk  *
2790f00f6562SOphir Munk  */
2791f00f6562SOphir Munk void
2792f00f6562SOphir Munk mlx5_os_mac_addr_flush(struct rte_eth_dev *dev)
2793f00f6562SOphir Munk {
2794f00f6562SOphir Munk 	struct mlx5_priv *priv = dev->data->dev_private;
2795f00f6562SOphir Munk 
2796f00f6562SOphir Munk 	mlx5_nl_mac_addr_flush(priv->nl_socket_route, mlx5_ifindex(dev),
2797f00f6562SOphir Munk 			       dev->data->mac_addrs,
2798f00f6562SOphir Munk 			       MLX5_MAX_MAC_ADDRESSES, priv->mac_own);
2799f00f6562SOphir Munk }
2800