Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright 2015 6WIND S.A.
3 : : * Copyright 2020 Mellanox Technologies, Ltd
4 : : */
5 : :
6 : : #include <stddef.h>
7 : : #include <unistd.h>
8 : : #include <string.h>
9 : : #include <stdint.h>
10 : : #include <stdlib.h>
11 : : #include <errno.h>
12 : : #include <net/if.h>
13 : : #include <linux/rtnetlink.h>
14 : : #include <linux/sockios.h>
15 : : #include <linux/ethtool.h>
16 : : #include <fcntl.h>
17 : :
18 : : #include <rte_malloc.h>
19 : : #include <ethdev_driver.h>
20 : : #include <ethdev_pci.h>
21 : : #include <rte_pci.h>
22 : : #include <bus_driver.h>
23 : : #include <bus_pci_driver.h>
24 : : #include <bus_auxiliary_driver.h>
25 : : #include <rte_common.h>
26 : : #include <rte_kvargs.h>
27 : : #include <rte_rwlock.h>
28 : : #include <rte_spinlock.h>
29 : : #include <rte_string_fns.h>
30 : : #include <rte_alarm.h>
31 : : #include <rte_eal_paging.h>
32 : :
33 : : #include <mlx5_glue.h>
34 : : #include <mlx5_devx_cmds.h>
35 : : #include <mlx5_common.h>
36 : : #include <mlx5_common_mp.h>
37 : : #include <mlx5_common_mr.h>
38 : : #include <mlx5_malloc.h>
39 : :
40 : : #include "mlx5_defs.h"
41 : : #include "mlx5.h"
42 : : #include "mlx5_common_os.h"
43 : : #include "mlx5_utils.h"
44 : : #include "mlx5_rxtx.h"
45 : : #include "mlx5_rx.h"
46 : : #include "mlx5_tx.h"
47 : : #include "mlx5_autoconf.h"
48 : : #include "mlx5_flow.h"
49 : : #include "rte_pmd_mlx5.h"
50 : : #include "mlx5_verbs.h"
51 : : #include "mlx5_nl.h"
52 : : #include "mlx5_devx.h"
53 : :
54 : : #ifndef HAVE_IBV_MLX5_MOD_MPW
55 : : #define MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED (1 << 2)
56 : : #define MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW (1 << 3)
57 : : #endif
58 : :
59 : : #ifndef HAVE_IBV_MLX5_MOD_CQE_128B_COMP
60 : : #define MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP (1 << 4)
61 : : #endif
62 : :
63 : : static const char *MZ_MLX5_PMD_SHARED_DATA = "mlx5_pmd_shared_data";
64 : :
65 : : /* Spinlock for mlx5_shared_data allocation. */
66 : : static rte_spinlock_t mlx5_shared_data_lock = RTE_SPINLOCK_INITIALIZER;
67 : :
68 : : /* Process local data for secondary processes. */
69 : : static struct mlx5_local_data mlx5_local_data;
70 : :
71 : : /* rte flow indexed pool configuration. */
72 : : static const struct mlx5_indexed_pool_config default_icfg[] = {
73 : : {
74 : : .size = sizeof(struct rte_flow),
75 : : .trunk_size = 64,
76 : : .need_lock = 1,
77 : : .release_mem_en = 0,
78 : : .malloc = mlx5_malloc,
79 : : .free = mlx5_free,
80 : : .per_core_cache = 0,
81 : : .type = "ctl_flow_ipool",
82 : : },
83 : : {
84 : : .size = sizeof(struct rte_flow),
85 : : .trunk_size = 64,
86 : : .grow_trunk = 3,
87 : : .grow_shift = 2,
88 : : .need_lock = 1,
89 : : .release_mem_en = 0,
90 : : .malloc = mlx5_malloc,
91 : : .free = mlx5_free,
92 : : .per_core_cache = 1 << 14,
93 : : .type = "rte_flow_ipool",
94 : : },
95 : : {
96 : : .size = sizeof(struct rte_flow),
97 : : .trunk_size = 64,
98 : : .grow_trunk = 3,
99 : : .grow_shift = 2,
100 : : .need_lock = 1,
101 : : .release_mem_en = 0,
102 : : .malloc = mlx5_malloc,
103 : : .free = mlx5_free,
104 : : .per_core_cache = 0,
105 : : .type = "mcp_flow_ipool",
106 : : },
107 : : };
108 : :
109 : : /**
110 : : * Set the completion channel file descriptor interrupt as non-blocking.
111 : : *
112 : : * @param[in] rxq_obj
113 : : * Pointer to RQ channel object, which includes the channel fd
114 : : *
115 : : * @param[out] fd
116 : : * The file descriptor (representing the interrupt) used in this channel.
117 : : *
118 : : * @return
119 : : * 0 on successfully setting the fd to non-blocking, non-zero otherwise.
120 : : */
121 : : int
122 : 0 : mlx5_os_set_nonblock_channel_fd(int fd)
123 : : {
124 : : int flags;
125 : :
126 : 0 : flags = fcntl(fd, F_GETFL);
127 : 0 : return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
128 : : }
129 : :
130 : : /**
131 : : * Get mlx5 device attributes. The glue function query_device_ex() is called
132 : : * with out parameter of type 'struct ibv_device_attr_ex *'. Then fill in mlx5
133 : : * device attributes from the glue out parameter.
134 : : *
135 : : * @param sh
136 : : * Pointer to shared device context.
137 : : *
138 : : * @return
139 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
140 : : */
141 : : int
142 : 0 : mlx5_os_capabilities_prepare(struct mlx5_dev_ctx_shared *sh)
143 : : {
144 : : int err;
145 : 0 : struct mlx5_common_device *cdev = sh->cdev;
146 : 0 : struct mlx5_hca_attr *hca_attr = &cdev->config.hca_attr;
147 : 0 : struct ibv_device_attr_ex attr_ex = { .comp_mask = 0 };
148 : 0 : struct mlx5dv_context dv_attr = { .comp_mask = 0 };
149 : :
150 : 0 : err = mlx5_glue->query_device_ex(cdev->ctx, NULL, &attr_ex);
151 [ # # ]: 0 : if (err) {
152 : 0 : rte_errno = errno;
153 : 0 : return -rte_errno;
154 : : }
155 : : #ifdef HAVE_IBV_MLX5_MOD_SWP
156 : 0 : dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_SWP;
157 : : #endif
158 : : #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
159 : 0 : dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS;
160 : : #endif
161 : : #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
162 : 0 : dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_STRIDING_RQ;
163 : : #endif
164 : : #ifdef HAVE_IBV_DEVICE_ATTR_ESW_MGR_REG_C0
165 : : dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_REG_C0;
166 : : #endif
167 : 0 : err = mlx5_glue->dv_query_device(cdev->ctx, &dv_attr);
168 [ # # ]: 0 : if (err) {
169 : 0 : rte_errno = errno;
170 : 0 : return -rte_errno;
171 : : }
172 : 0 : memset(&sh->dev_cap, 0, sizeof(struct mlx5_dev_cap));
173 [ # # ]: 0 : if (mlx5_dev_is_pci(cdev->dev))
174 : 0 : sh->dev_cap.vf = mlx5_dev_is_vf_pci(RTE_DEV_TO_PCI(cdev->dev));
175 : : else
176 : 0 : sh->dev_cap.sf = 1;
177 : 0 : sh->dev_cap.max_qp_wr = attr_ex.orig_attr.max_qp_wr;
178 : 0 : sh->dev_cap.max_sge = attr_ex.orig_attr.max_sge;
179 : 0 : sh->dev_cap.max_cq = attr_ex.orig_attr.max_cq;
180 : 0 : sh->dev_cap.max_qp = attr_ex.orig_attr.max_qp;
181 : : #ifdef HAVE_MLX5DV_DR_ACTION_DEST_DEVX_TIR
182 : 0 : sh->dev_cap.dest_tir = 1;
183 : : #endif
184 : : #if defined(HAVE_IBV_FLOW_DV_SUPPORT) && defined(HAVE_MLX5DV_DR)
185 : 0 : DRV_LOG(DEBUG, "DV flow is supported.");
186 : 0 : sh->dev_cap.dv_flow_en = 1;
187 : : #endif
188 : : #ifdef HAVE_MLX5DV_DR_ESWITCH
189 [ # # # # ]: 0 : if (hca_attr->eswitch_manager && sh->dev_cap.dv_flow_en && sh->esw_mode)
190 : 0 : sh->dev_cap.dv_esw_en = 1;
191 : : #endif
192 : : /*
193 : : * Multi-packet send is supported by ConnectX-4 Lx PF as well
194 : : * as all ConnectX-5 devices.
195 : : */
196 [ # # ]: 0 : if (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED) {
197 [ # # ]: 0 : if (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW) {
198 : 0 : DRV_LOG(DEBUG, "Enhanced MPW is supported.");
199 : 0 : sh->dev_cap.mps = MLX5_MPW_ENHANCED;
200 : : } else {
201 : 0 : DRV_LOG(DEBUG, "MPW is supported.");
202 : 0 : sh->dev_cap.mps = MLX5_MPW;
203 : : }
204 : : } else {
205 : 0 : DRV_LOG(DEBUG, "MPW isn't supported.");
206 : 0 : sh->dev_cap.mps = MLX5_MPW_DISABLED;
207 : : }
208 : : #if (RTE_CACHE_LINE_SIZE == 128)
209 : : if (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP)
210 : : sh->dev_cap.cqe_comp = 1;
211 : : DRV_LOG(DEBUG, "Rx CQE 128B compression is %ssupported.",
212 : : sh->dev_cap.cqe_comp ? "" : "not ");
213 : : #else
214 : 0 : sh->dev_cap.cqe_comp = 1;
215 : : #endif
216 : : #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
217 : 0 : sh->dev_cap.mpls_en =
218 : : ((dv_attr.tunnel_offloads_caps &
219 : 0 : MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_CW_MPLS_OVER_GRE) &&
220 : : (dv_attr.tunnel_offloads_caps &
221 : : MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_CW_MPLS_OVER_UDP));
222 [ # # ]: 0 : DRV_LOG(DEBUG, "MPLS over GRE/UDP tunnel offloading is %ssupported.",
223 : : sh->dev_cap.mpls_en ? "" : "not ");
224 : : #else
225 : : DRV_LOG(WARNING,
226 : : "MPLS over GRE/UDP tunnel offloading disabled due to old OFED/rdma-core version or firmware configuration");
227 : : #endif
228 : : #if defined(HAVE_IBV_WQ_FLAG_RX_END_PADDING)
229 : : sh->dev_cap.hw_padding = !!attr_ex.rx_pad_end_addr_align;
230 : : #elif defined(HAVE_IBV_WQ_FLAGS_PCI_WRITE_END_PADDING)
231 : 0 : sh->dev_cap.hw_padding = !!(attr_ex.device_cap_flags_ex &
232 : : IBV_DEVICE_PCI_WRITE_END_PADDING);
233 : : #endif
234 : 0 : sh->dev_cap.hw_csum =
235 : 0 : !!(attr_ex.device_cap_flags_ex & IBV_DEVICE_RAW_IP_CSUM);
236 [ # # ]: 0 : DRV_LOG(DEBUG, "Checksum offloading is %ssupported.",
237 : : sh->dev_cap.hw_csum ? "" : "not ");
238 : 0 : sh->dev_cap.hw_vlan_strip = !!(attr_ex.raw_packet_caps &
239 : : IBV_RAW_PACKET_CAP_CVLAN_STRIPPING);
240 [ # # ]: 0 : DRV_LOG(DEBUG, "VLAN stripping is %ssupported.",
241 : : (sh->dev_cap.hw_vlan_strip ? "" : "not "));
242 : 0 : sh->dev_cap.hw_fcs_strip = !!(attr_ex.raw_packet_caps &
243 : : IBV_RAW_PACKET_CAP_SCATTER_FCS);
244 : : #if !defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) && \
245 : : !defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
246 : : DRV_LOG(DEBUG, "Counters are not supported.");
247 : : #endif
248 : : /*
249 : : * DPDK doesn't support larger/variable indirection tables.
250 : : * Once DPDK supports it, take max size from device attr.
251 : : */
252 : 0 : sh->dev_cap.ind_table_max_size =
253 : 0 : RTE_MIN(attr_ex.rss_caps.max_rwq_indirection_table_size,
254 : : (unsigned int)RTE_ETH_RSS_RETA_SIZE_512);
255 : 0 : DRV_LOG(DEBUG, "Maximum Rx indirection table size is %u",
256 : : sh->dev_cap.ind_table_max_size);
257 [ # # ]: 0 : sh->dev_cap.tso = (attr_ex.tso_caps.max_tso > 0 &&
258 [ # # ]: 0 : (attr_ex.tso_caps.supported_qpts &
259 : : (1 << IBV_QPT_RAW_PACKET)));
260 [ # # ]: 0 : if (sh->dev_cap.tso)
261 : 0 : sh->dev_cap.tso_max_payload_sz = attr_ex.tso_caps.max_tso;
262 [ # # ]: 0 : strlcpy(sh->dev_cap.fw_ver, attr_ex.orig_attr.fw_ver,
263 : : sizeof(sh->dev_cap.fw_ver));
264 : : #ifdef HAVE_IBV_MLX5_MOD_SWP
265 [ # # ]: 0 : if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_SWP)
266 : 0 : sh->dev_cap.swp = dv_attr.sw_parsing_caps.sw_parsing_offloads &
267 : : (MLX5_SW_PARSING_CAP |
268 : : MLX5_SW_PARSING_CSUM_CAP |
269 : : MLX5_SW_PARSING_TSO_CAP);
270 : 0 : DRV_LOG(DEBUG, "SWP support: %u", sh->dev_cap.swp);
271 : : #endif
272 : : #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
273 [ # # ]: 0 : if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_STRIDING_RQ) {
274 : : struct mlx5dv_striding_rq_caps *strd_rq_caps =
275 : : &dv_attr.striding_rq_caps;
276 : :
277 : 0 : sh->dev_cap.mprq.enabled = 1;
278 : 0 : sh->dev_cap.mprq.log_min_stride_size =
279 : 0 : strd_rq_caps->min_single_stride_log_num_of_bytes;
280 : 0 : sh->dev_cap.mprq.log_max_stride_size =
281 : 0 : strd_rq_caps->max_single_stride_log_num_of_bytes;
282 : 0 : sh->dev_cap.mprq.log_min_stride_num =
283 : 0 : strd_rq_caps->min_single_wqe_log_num_of_strides;
284 : 0 : sh->dev_cap.mprq.log_max_stride_num =
285 : 0 : strd_rq_caps->max_single_wqe_log_num_of_strides;
286 : 0 : sh->dev_cap.mprq.log_min_stride_wqe_size =
287 : 0 : cdev->config.devx ?
288 [ # # ]: 0 : hca_attr->log_min_stride_wqe_sz :
289 : : MLX5_MPRQ_LOG_MIN_STRIDE_WQE_SIZE;
290 : 0 : DRV_LOG(DEBUG, "\tmin_single_stride_log_num_of_bytes: %u",
291 : : sh->dev_cap.mprq.log_min_stride_size);
292 : 0 : DRV_LOG(DEBUG, "\tmax_single_stride_log_num_of_bytes: %u",
293 : : sh->dev_cap.mprq.log_max_stride_size);
294 : 0 : DRV_LOG(DEBUG, "\tmin_single_wqe_log_num_of_strides: %u",
295 : : sh->dev_cap.mprq.log_min_stride_num);
296 : 0 : DRV_LOG(DEBUG, "\tmax_single_wqe_log_num_of_strides: %u",
297 : : sh->dev_cap.mprq.log_max_stride_num);
298 : 0 : DRV_LOG(DEBUG, "\tmin_stride_wqe_log_size: %u",
299 : : sh->dev_cap.mprq.log_min_stride_wqe_size);
300 : 0 : DRV_LOG(DEBUG, "\tsupported_qpts: %d",
301 : : strd_rq_caps->supported_qpts);
302 : 0 : DRV_LOG(DEBUG, "Device supports Multi-Packet RQ.");
303 : : }
304 : : #endif
305 : : #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
306 [ # # ]: 0 : if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS) {
307 : 0 : sh->dev_cap.tunnel_en = dv_attr.tunnel_offloads_caps &
308 : : (MLX5_TUNNELED_OFFLOADS_VXLAN_CAP |
309 : : MLX5_TUNNELED_OFFLOADS_GRE_CAP |
310 : : MLX5_TUNNELED_OFFLOADS_GENEVE_CAP);
311 : : }
312 [ # # ]: 0 : if (sh->dev_cap.tunnel_en) {
313 [ # # # # : 0 : DRV_LOG(DEBUG, "Tunnel offloading is supported for %s%s%s",
# # ]
314 : : sh->dev_cap.tunnel_en &
315 : : MLX5_TUNNELED_OFFLOADS_VXLAN_CAP ? "[VXLAN]" : "",
316 : : sh->dev_cap.tunnel_en &
317 : : MLX5_TUNNELED_OFFLOADS_GRE_CAP ? "[GRE]" : "",
318 : : sh->dev_cap.tunnel_en &
319 : : MLX5_TUNNELED_OFFLOADS_GENEVE_CAP ? "[GENEVE]" : "");
320 : : } else {
321 : 0 : DRV_LOG(DEBUG, "Tunnel offloading is not supported.");
322 : : }
323 : : #else
324 : : DRV_LOG(WARNING,
325 : : "Tunnel offloading disabled due to old OFED/rdma-core version");
326 : : #endif
327 [ # # ]: 0 : if (!sh->cdev->config.devx)
328 : : return 0;
329 : : /* Check capabilities for Packet Pacing. */
330 : 0 : DRV_LOG(DEBUG, "Timestamp counter frequency %u kHz.",
331 : : hca_attr->dev_freq_khz);
332 [ # # ]: 0 : DRV_LOG(DEBUG, "Packet pacing is %ssupported.",
333 : : hca_attr->qos.packet_pacing ? "" : "not ");
334 [ # # ]: 0 : DRV_LOG(DEBUG, "Cross channel ops are %ssupported.",
335 : : hca_attr->cross_channel ? "" : "not ");
336 [ # # ]: 0 : DRV_LOG(DEBUG, "WQE index ignore is %ssupported.",
337 : : hca_attr->wqe_index_ignore ? "" : "not ");
338 [ # # ]: 0 : DRV_LOG(DEBUG, "Non-wire SQ feature is %ssupported.",
339 : : hca_attr->non_wire_sq ? "" : "not ");
340 [ # # ]: 0 : DRV_LOG(DEBUG, "Static WQE SQ feature is %ssupported (%d)",
341 : : hca_attr->log_max_static_sq_wq ? "" : "not ",
342 : : hca_attr->log_max_static_sq_wq);
343 [ # # ]: 0 : DRV_LOG(DEBUG, "WQE rate PP mode is %ssupported.",
344 : : hca_attr->qos.wqe_rate_pp ? "" : "not ");
345 : 0 : sh->dev_cap.txpp_en = hca_attr->qos.packet_pacing;
346 [ # # ]: 0 : if (!hca_attr->cross_channel) {
347 : 0 : DRV_LOG(DEBUG,
348 : : "Cross channel operations are required for packet pacing.");
349 : 0 : sh->dev_cap.txpp_en = 0;
350 : : }
351 [ # # ]: 0 : if (!hca_attr->wqe_index_ignore) {
352 : 0 : DRV_LOG(DEBUG,
353 : : "WQE index ignore feature is required for packet pacing.");
354 : 0 : sh->dev_cap.txpp_en = 0;
355 : : }
356 [ # # ]: 0 : if (!hca_attr->non_wire_sq) {
357 : 0 : DRV_LOG(DEBUG,
358 : : "Non-wire SQ feature is required for packet pacing.");
359 : 0 : sh->dev_cap.txpp_en = 0;
360 : : }
361 [ # # ]: 0 : if (!hca_attr->log_max_static_sq_wq) {
362 : 0 : DRV_LOG(DEBUG,
363 : : "Static WQE SQ feature is required for packet pacing.");
364 : 0 : sh->dev_cap.txpp_en = 0;
365 : : }
366 [ # # ]: 0 : if (!hca_attr->qos.wqe_rate_pp) {
367 : 0 : DRV_LOG(DEBUG,
368 : : "WQE rate mode is required for packet pacing.");
369 : 0 : sh->dev_cap.txpp_en = 0;
370 : : }
371 : : #ifndef HAVE_MLX5DV_DEVX_UAR_OFFSET
372 : : DRV_LOG(DEBUG,
373 : : "DevX does not provide UAR offset, can't create queues for packet pacing.");
374 : : sh->dev_cap.txpp_en = 0;
375 : : #endif
376 : 0 : sh->dev_cap.scatter_fcs_w_decap_disable =
377 : 0 : hca_attr->scatter_fcs_w_decap_disable;
378 : 0 : sh->dev_cap.rq_delay_drop_en = hca_attr->rq_delay_drop;
379 : 0 : mlx5_rt_timestamp_config(sh, hca_attr);
380 : 0 : sh->dev_cap.esw_info.vhca_id = hca_attr->vhca_id;
381 : : #ifdef HAVE_IBV_DEVICE_ATTR_ESW_MGR_REG_C0
382 : : if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_REG_C0) {
383 : : sh->dev_cap.esw_info.regc_value = dv_attr.reg_c0.value;
384 : : sh->dev_cap.esw_info.regc_mask = dv_attr.reg_c0.mask;
385 : : }
386 : : #else
387 : 0 : sh->dev_cap.esw_info.regc_value = 0;
388 : 0 : sh->dev_cap.esw_info.regc_mask = 0;
389 : : #endif
390 : 0 : sh->dev_cap.esw_info.is_set = 1;
391 : 0 : return 0;
392 : : }
393 : :
394 : : /**
395 : : * Detect misc5 support or not
396 : : *
397 : : * @param[in] priv
398 : : * Device private data pointer
399 : : */
400 : : #ifdef HAVE_MLX5DV_DR
401 : : static void
402 : 0 : __mlx5_discovery_misc5_cap(struct mlx5_priv *priv)
403 : : {
404 : : #ifdef HAVE_IBV_FLOW_DV_SUPPORT
405 : : /* Dummy VxLAN matcher to detect rdma-core misc5 cap
406 : : * Case: IPv4--->UDP--->VxLAN--->vni
407 : : */
408 : : void *tbl;
409 : : struct mlx5_flow_dv_match_params matcher_mask;
410 : : void *match_m;
411 : : void *matcher;
412 : : void *headers_m;
413 : : void *misc5_m;
414 : : uint32_t *tunnel_header_m;
415 : : struct mlx5dv_flow_matcher_attr dv_attr;
416 : :
417 : : memset(&matcher_mask, 0, sizeof(matcher_mask));
418 : 0 : matcher_mask.size = sizeof(matcher_mask.buf);
419 : : match_m = matcher_mask.buf;
420 : : headers_m = MLX5_ADDR_OF(fte_match_param, match_m, outer_headers);
421 : : misc5_m = MLX5_ADDR_OF(fte_match_param,
422 : : match_m, misc_parameters_5);
423 : : tunnel_header_m = (uint32_t *)
424 : : MLX5_ADDR_OF(fte_match_set_misc5,
425 : : misc5_m, tunnel_header_1);
426 : : MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_protocol, 0xff);
427 [ # # ]: 0 : MLX5_SET(fte_match_set_lyr_2_4, headers_m, ip_version, 4);
428 : 0 : MLX5_SET(fte_match_set_lyr_2_4, headers_m, udp_dport, 0xffff);
429 : 0 : *tunnel_header_m = 0xffffff;
430 : :
431 : 0 : tbl = mlx5_glue->dr_create_flow_tbl(priv->sh->rx_domain, 1);
432 [ # # ]: 0 : if (!tbl) {
433 : 0 : DRV_LOG(INFO, "No SW steering support");
434 : 0 : return;
435 : : }
436 : 0 : dv_attr.type = IBV_FLOW_ATTR_NORMAL;
437 : 0 : dv_attr.match_mask = (void *)&matcher_mask;
438 : 0 : dv_attr.match_criteria_enable =
439 : : (1 << MLX5_MATCH_CRITERIA_ENABLE_OUTER_BIT) |
440 : : (1 << MLX5_MATCH_CRITERIA_ENABLE_MISC5_BIT);
441 : 0 : dv_attr.priority = 3;
442 : : #ifdef HAVE_MLX5DV_DR_ESWITCH
443 : : void *misc2_m;
444 [ # # ]: 0 : if (priv->sh->config.dv_esw_en) {
445 : : /* FDB enabled reg_c_0 */
446 : 0 : dv_attr.match_criteria_enable |=
447 : : (1 << MLX5_MATCH_CRITERIA_ENABLE_MISC2_BIT);
448 : : misc2_m = MLX5_ADDR_OF(fte_match_param,
449 : : match_m, misc_parameters_2);
450 [ # # ]: 0 : MLX5_SET(fte_match_set_misc2, misc2_m,
451 : : metadata_reg_c_0, 0xffff);
452 : : }
453 : : #endif
454 : 0 : matcher = mlx5_glue->dv_create_flow_matcher(priv->sh->cdev->ctx,
455 : : &dv_attr, tbl);
456 [ # # ]: 0 : if (matcher) {
457 : 0 : priv->sh->misc5_cap = 1;
458 : 0 : mlx5_glue->dv_destroy_flow_matcher(matcher);
459 : : }
460 : 0 : mlx5_glue->dr_destroy_flow_tbl(tbl);
461 : : #else
462 : : RTE_SET_USED(priv);
463 : : #endif
464 : : }
465 : : #endif
466 : :
467 : : /**
468 : : * Initialize DR related data within private structure.
469 : : * Routine checks the reference counter and does actual
470 : : * resources creation/initialization only if counter is zero.
471 : : *
472 : : * @param[in] eth_dev
473 : : * Pointer to the device.
474 : : *
475 : : * @return
476 : : * Zero on success, positive error code otherwise.
477 : : */
478 : : static int
479 : 0 : mlx5_alloc_shared_dr(struct rte_eth_dev *eth_dev)
480 : : {
481 : 0 : struct mlx5_priv *priv = eth_dev->data->dev_private;
482 : 0 : struct mlx5_dev_ctx_shared *sh = priv->sh;
483 : : char s[MLX5_NAME_SIZE] __rte_unused;
484 : : int err;
485 : :
486 : : MLX5_ASSERT(sh && sh->refcnt);
487 [ # # ]: 0 : if (sh->refcnt > 1)
488 : : return 0;
489 : 0 : err = mlx5_alloc_table_hash_list(priv);
490 [ # # ]: 0 : if (err)
491 : 0 : goto error;
492 : 0 : sh->default_miss_action =
493 : 0 : mlx5_glue->dr_create_flow_action_default_miss();
494 [ # # ]: 0 : if (!sh->default_miss_action)
495 : 0 : DRV_LOG(WARNING, "Default miss action is not supported.");
496 : : /* The resources below are only valid with DV support. */
497 : : #ifdef HAVE_IBV_FLOW_DV_SUPPORT
498 : : /* Init shared flex parsers list, no need lcore_share */
499 : 0 : snprintf(s, sizeof(s), "%s_flex_parsers_list", sh->ibdev_name);
500 : 0 : sh->flex_parsers_dv = mlx5_list_create(s, sh, false,
501 : : mlx5_flex_parser_create_cb,
502 : : mlx5_flex_parser_match_cb,
503 : : mlx5_flex_parser_remove_cb,
504 : : mlx5_flex_parser_clone_cb,
505 : : mlx5_flex_parser_clone_free_cb);
506 [ # # ]: 0 : if (!sh->flex_parsers_dv)
507 : 0 : goto error;
508 [ # # ]: 0 : if (priv->sh->config.dv_flow_en == 2) {
509 [ # # ]: 0 : if (sh->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
510 [ # # ]: 0 : sh->dv_regc0_mask) {
511 : : /* Reuse DV callback functions. */
512 : 0 : sh->mreg_cp_tbl = mlx5_hlist_create(MLX5_FLOW_MREG_HNAME,
513 : : MLX5_FLOW_MREG_HTABLE_SZ,
514 : : false, true, eth_dev,
515 : : mlx5_flow_nta_mreg_create_cb,
516 : : mlx5_flow_dv_mreg_match_cb,
517 : : mlx5_flow_nta_mreg_remove_cb,
518 : : mlx5_flow_dv_mreg_clone_cb,
519 : : mlx5_flow_dv_mreg_clone_free_cb);
520 [ # # ]: 0 : if (!sh->mreg_cp_tbl) {
521 : : err = ENOMEM;
522 : 0 : goto error;
523 : : }
524 : : }
525 : 0 : return 0;
526 : : }
527 : : /* Init port id action list. */
528 : : snprintf(s, sizeof(s), "%s_port_id_action_list", sh->ibdev_name);
529 : 0 : sh->port_id_action_list = mlx5_list_create(s, sh, true,
530 : : mlx5_flow_dv_port_id_create_cb,
531 : : mlx5_flow_dv_port_id_match_cb,
532 : : mlx5_flow_dv_port_id_remove_cb,
533 : : mlx5_flow_dv_port_id_clone_cb,
534 : : mlx5_flow_dv_port_id_clone_free_cb);
535 [ # # ]: 0 : if (!sh->port_id_action_list)
536 : 0 : goto error;
537 : : /* Init push vlan action list. */
538 : : snprintf(s, sizeof(s), "%s_push_vlan_action_list", sh->ibdev_name);
539 : 0 : sh->push_vlan_action_list = mlx5_list_create(s, sh, true,
540 : : mlx5_flow_dv_push_vlan_create_cb,
541 : : mlx5_flow_dv_push_vlan_match_cb,
542 : : mlx5_flow_dv_push_vlan_remove_cb,
543 : : mlx5_flow_dv_push_vlan_clone_cb,
544 : : mlx5_flow_dv_push_vlan_clone_free_cb);
545 [ # # ]: 0 : if (!sh->push_vlan_action_list)
546 : 0 : goto error;
547 : : /* Init sample action list. */
548 : : snprintf(s, sizeof(s), "%s_sample_action_list", sh->ibdev_name);
549 : 0 : sh->sample_action_list = mlx5_list_create(s, sh, true,
550 : : mlx5_flow_dv_sample_create_cb,
551 : : mlx5_flow_dv_sample_match_cb,
552 : : mlx5_flow_dv_sample_remove_cb,
553 : : mlx5_flow_dv_sample_clone_cb,
554 : : mlx5_flow_dv_sample_clone_free_cb);
555 [ # # ]: 0 : if (!sh->sample_action_list)
556 : 0 : goto error;
557 : : /* Init dest array action list. */
558 : : snprintf(s, sizeof(s), "%s_dest_array_list", sh->ibdev_name);
559 : 0 : sh->dest_array_list = mlx5_list_create(s, sh, true,
560 : : mlx5_flow_dv_dest_array_create_cb,
561 : : mlx5_flow_dv_dest_array_match_cb,
562 : : mlx5_flow_dv_dest_array_remove_cb,
563 : : mlx5_flow_dv_dest_array_clone_cb,
564 : : mlx5_flow_dv_dest_array_clone_free_cb);
565 [ # # ]: 0 : if (!sh->dest_array_list)
566 : 0 : goto error;
567 : : #else
568 : : if (priv->sh->config.dv_flow_en == 2)
569 : : return 0;
570 : : #endif
571 : : #ifdef HAVE_MLX5DV_DR
572 : : void *domain;
573 : :
574 : : /* Reference counter is zero, we should initialize structures. */
575 : 0 : domain = mlx5_glue->dr_create_domain(sh->cdev->ctx,
576 : : MLX5DV_DR_DOMAIN_TYPE_NIC_RX);
577 [ # # ]: 0 : if (!domain) {
578 : 0 : DRV_LOG(ERR, "ingress mlx5dv_dr_create_domain failed");
579 : 0 : err = errno;
580 : 0 : goto error;
581 : : }
582 : 0 : sh->rx_domain = domain;
583 : 0 : domain = mlx5_glue->dr_create_domain(sh->cdev->ctx,
584 : : MLX5DV_DR_DOMAIN_TYPE_NIC_TX);
585 [ # # ]: 0 : if (!domain) {
586 : 0 : DRV_LOG(ERR, "egress mlx5dv_dr_create_domain failed");
587 : 0 : err = errno;
588 : 0 : goto error;
589 : : }
590 : 0 : sh->tx_domain = domain;
591 : : #ifdef HAVE_MLX5DV_DR_ESWITCH
592 [ # # ]: 0 : if (sh->config.dv_esw_en) {
593 : 0 : domain = mlx5_glue->dr_create_domain(sh->cdev->ctx,
594 : : MLX5DV_DR_DOMAIN_TYPE_FDB);
595 [ # # ]: 0 : if (!domain) {
596 : 0 : DRV_LOG(ERR, "FDB mlx5dv_dr_create_domain failed");
597 : 0 : err = errno;
598 : 0 : goto error;
599 : : }
600 : 0 : sh->fdb_domain = domain;
601 : : }
602 : : /*
603 : : * The drop action is just some dummy placeholder in rdma-core. It
604 : : * does not belong to domains and has no any attributes, and, can be
605 : : * shared by the entire device.
606 : : */
607 : 0 : sh->dr_drop_action = mlx5_glue->dr_create_flow_action_drop();
608 [ # # ]: 0 : if (!sh->dr_drop_action) {
609 : 0 : DRV_LOG(ERR, "FDB mlx5dv_dr_create_flow_action_drop");
610 : 0 : err = errno;
611 : 0 : goto error;
612 : : }
613 : :
614 [ # # ]: 0 : if (sh->config.dv_flow_en == 1) {
615 : : /* Query availability of metadata reg_c's. */
616 [ # # ]: 0 : if (!priv->sh->metadata_regc_check_flag) {
617 : 0 : err = mlx5_flow_discover_mreg_c(eth_dev);
618 [ # # ]: 0 : if (err < 0) {
619 : 0 : err = -err;
620 : 0 : goto error;
621 : : }
622 : : }
623 [ # # ]: 0 : if (!mlx5_flow_ext_mreg_supported(eth_dev)) {
624 : 0 : DRV_LOG(DEBUG,
625 : : "port %u extensive metadata register is not supported",
626 : : eth_dev->data->port_id);
627 [ # # ]: 0 : if (sh->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
628 : 0 : DRV_LOG(ERR, "metadata mode %u is not supported "
629 : : "(no metadata registers available)",
630 : : sh->config.dv_xmeta_en);
631 : : err = ENOTSUP;
632 : 0 : goto error;
633 : : }
634 : : }
635 [ # # # # ]: 0 : if (sh->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
636 [ # # ]: 0 : mlx5_flow_ext_mreg_supported(eth_dev) && sh->dv_regc0_mask) {
637 : 0 : sh->mreg_cp_tbl = mlx5_hlist_create(MLX5_FLOW_MREG_HNAME,
638 : : MLX5_FLOW_MREG_HTABLE_SZ,
639 : : false, true, eth_dev,
640 : : mlx5_flow_dv_mreg_create_cb,
641 : : mlx5_flow_dv_mreg_match_cb,
642 : : mlx5_flow_dv_mreg_remove_cb,
643 : : mlx5_flow_dv_mreg_clone_cb,
644 : : mlx5_flow_dv_mreg_clone_free_cb);
645 [ # # ]: 0 : if (!sh->mreg_cp_tbl) {
646 : : err = ENOMEM;
647 : 0 : goto error;
648 : : }
649 : : }
650 : : }
651 : : #endif
652 [ # # # # ]: 0 : if (!sh->tunnel_hub && sh->config.dv_miss_info)
653 : 0 : err = mlx5_alloc_tunnel_hub(sh);
654 [ # # ]: 0 : if (err) {
655 : 0 : DRV_LOG(ERR, "mlx5_alloc_tunnel_hub failed err=%d", err);
656 : 0 : goto error;
657 : : }
658 [ # # ]: 0 : if (sh->config.reclaim_mode == MLX5_RCM_AGGR) {
659 : 0 : mlx5_glue->dr_reclaim_domain_memory(sh->rx_domain, 1);
660 : 0 : mlx5_glue->dr_reclaim_domain_memory(sh->tx_domain, 1);
661 [ # # ]: 0 : if (sh->fdb_domain)
662 : 0 : mlx5_glue->dr_reclaim_domain_memory(sh->fdb_domain, 1);
663 : : }
664 : 0 : sh->pop_vlan_action = mlx5_glue->dr_create_flow_action_pop_vlan();
665 [ # # ]: 0 : if (!sh->config.allow_duplicate_pattern) {
666 : : #ifndef HAVE_MLX5_DR_ALLOW_DUPLICATE
667 : : DRV_LOG(WARNING, "Disallow duplicate pattern is not supported - maybe old rdma-core version?");
668 : : #endif
669 : 0 : mlx5_glue->dr_allow_duplicate_rules(sh->rx_domain, 0);
670 : 0 : mlx5_glue->dr_allow_duplicate_rules(sh->tx_domain, 0);
671 [ # # ]: 0 : if (sh->fdb_domain)
672 : 0 : mlx5_glue->dr_allow_duplicate_rules(sh->fdb_domain, 0);
673 : : }
674 : :
675 : 0 : __mlx5_discovery_misc5_cap(priv);
676 : : #endif /* HAVE_MLX5DV_DR */
677 : 0 : LIST_INIT(&sh->shared_rxqs);
678 : 0 : return 0;
679 : 0 : error:
680 : : /* Rollback the created objects. */
681 [ # # ]: 0 : if (sh->rx_domain) {
682 : 0 : mlx5_glue->dr_destroy_domain(sh->rx_domain);
683 : 0 : sh->rx_domain = NULL;
684 : : }
685 [ # # ]: 0 : if (sh->tx_domain) {
686 : 0 : mlx5_glue->dr_destroy_domain(sh->tx_domain);
687 : 0 : sh->tx_domain = NULL;
688 : : }
689 [ # # ]: 0 : if (sh->fdb_domain) {
690 : 0 : mlx5_glue->dr_destroy_domain(sh->fdb_domain);
691 : 0 : sh->fdb_domain = NULL;
692 : : }
693 [ # # ]: 0 : if (sh->dr_drop_action) {
694 : 0 : mlx5_glue->destroy_flow_action(sh->dr_drop_action);
695 : 0 : sh->dr_drop_action = NULL;
696 : : }
697 [ # # ]: 0 : if (sh->pop_vlan_action) {
698 : 0 : mlx5_glue->destroy_flow_action(sh->pop_vlan_action);
699 : 0 : sh->pop_vlan_action = NULL;
700 : : }
701 [ # # ]: 0 : if (sh->encaps_decaps) {
702 : 0 : mlx5_hlist_destroy(sh->encaps_decaps);
703 : 0 : sh->encaps_decaps = NULL;
704 : : }
705 [ # # ]: 0 : if (sh->modify_cmds) {
706 : 0 : mlx5_hlist_destroy(sh->modify_cmds);
707 : 0 : sh->modify_cmds = NULL;
708 : : }
709 [ # # ]: 0 : if (sh->tag_table) {
710 : : /* tags should be destroyed with flow before. */
711 : 0 : mlx5_hlist_destroy(sh->tag_table);
712 : 0 : sh->tag_table = NULL;
713 : : }
714 [ # # ]: 0 : if (sh->tunnel_hub) {
715 : 0 : mlx5_release_tunnel_hub(sh, priv->dev_port);
716 : 0 : sh->tunnel_hub = NULL;
717 : : }
718 : 0 : mlx5_free_table_hash_list(priv);
719 [ # # ]: 0 : if (sh->port_id_action_list) {
720 : 0 : mlx5_list_destroy(sh->port_id_action_list);
721 : 0 : sh->port_id_action_list = NULL;
722 : : }
723 [ # # ]: 0 : if (sh->push_vlan_action_list) {
724 : 0 : mlx5_list_destroy(sh->push_vlan_action_list);
725 : 0 : sh->push_vlan_action_list = NULL;
726 : : }
727 [ # # ]: 0 : if (sh->sample_action_list) {
728 : 0 : mlx5_list_destroy(sh->sample_action_list);
729 : 0 : sh->sample_action_list = NULL;
730 : : }
731 [ # # ]: 0 : if (sh->dest_array_list) {
732 : 0 : mlx5_list_destroy(sh->dest_array_list);
733 : 0 : sh->dest_array_list = NULL;
734 : : }
735 [ # # ]: 0 : if (sh->mreg_cp_tbl) {
736 : 0 : mlx5_hlist_destroy(sh->mreg_cp_tbl);
737 : 0 : sh->mreg_cp_tbl = NULL;
738 : : }
739 : : return err;
740 : : }
741 : :
742 : : #ifdef HAVE_MLX5DV_DR
743 : : static void
744 : 0 : mlx5_destroy_send_to_kernel_action(struct mlx5_dev_ctx_shared *sh)
745 : : {
746 : : int i;
747 : :
748 [ # # ]: 0 : for (i = 0; i < MLX5DR_TABLE_TYPE_MAX; i++) {
749 [ # # ]: 0 : if (sh->send_to_kernel_action[i].action) {
750 : : void *action = sh->send_to_kernel_action[i].action;
751 : :
752 : 0 : mlx5_glue->destroy_flow_action(action);
753 : 0 : sh->send_to_kernel_action[i].action = NULL;
754 : : }
755 [ # # ]: 0 : if (sh->send_to_kernel_action[i].tbl) {
756 : : struct mlx5_flow_tbl_resource *tbl =
757 : : sh->send_to_kernel_action[i].tbl;
758 : :
759 : 0 : mlx5_flow_dv_tbl_resource_release(sh, tbl);
760 : 0 : sh->send_to_kernel_action[i].tbl = NULL;
761 : : }
762 : : }
763 : 0 : }
764 : : #endif /* HAVE_MLX5DV_DR */
765 : :
766 : : /**
767 : : * Destroy DR related data within private structure.
768 : : *
769 : : * @param[in] priv
770 : : * Pointer to the private device data structure.
771 : : */
772 : : void
773 : 0 : mlx5_os_free_shared_dr(struct mlx5_priv *priv)
774 : : {
775 : 0 : struct mlx5_dev_ctx_shared *sh = priv->sh;
776 : : struct mlx5_rxq_ctrl *rxq_ctrl;
777 : : int i = 0;
778 : :
779 : : MLX5_ASSERT(sh && sh->refcnt);
780 [ # # ]: 0 : if (sh->refcnt > 1)
781 : : return;
782 [ # # ]: 0 : LIST_FOREACH(rxq_ctrl, &sh->shared_rxqs, next) {
783 : 0 : DRV_LOG(DEBUG, "port %u Rx Queue %u still referenced",
784 : : priv->dev_data->port_id, rxq_ctrl->rxq.idx);
785 : 0 : ++i;
786 : : }
787 [ # # ]: 0 : if (i > 0)
788 : 0 : DRV_LOG(WARNING, "port %u some Rx queues still remain %d",
789 : : priv->dev_data->port_id, i);
790 : : MLX5_ASSERT(LIST_EMPTY(&sh->shared_rxqs));
791 : : #ifdef HAVE_MLX5DV_DR
792 : 0 : mlx5_destroy_send_to_kernel_action(sh);
793 [ # # ]: 0 : if (sh->rx_domain) {
794 : 0 : mlx5_glue->dr_destroy_domain(sh->rx_domain);
795 : 0 : sh->rx_domain = NULL;
796 : : }
797 [ # # ]: 0 : if (sh->tx_domain) {
798 : 0 : mlx5_glue->dr_destroy_domain(sh->tx_domain);
799 : 0 : sh->tx_domain = NULL;
800 : : }
801 : : #ifdef HAVE_MLX5DV_DR_ESWITCH
802 [ # # ]: 0 : if (sh->fdb_domain) {
803 : 0 : mlx5_glue->dr_destroy_domain(sh->fdb_domain);
804 : 0 : sh->fdb_domain = NULL;
805 : : }
806 [ # # ]: 0 : if (sh->dr_drop_action) {
807 : 0 : mlx5_glue->destroy_flow_action(sh->dr_drop_action);
808 : 0 : sh->dr_drop_action = NULL;
809 : : }
810 : : #endif
811 [ # # ]: 0 : if (sh->pop_vlan_action) {
812 : 0 : mlx5_glue->destroy_flow_action(sh->pop_vlan_action);
813 : 0 : sh->pop_vlan_action = NULL;
814 : : }
815 [ # # ]: 0 : for (i = 0; i < MLX5DR_TABLE_TYPE_MAX; i++) {
816 [ # # ]: 0 : if (sh->send_to_kernel_action[i].action) {
817 : : void *action = sh->send_to_kernel_action[i].action;
818 : :
819 : 0 : mlx5_glue->destroy_flow_action(action);
820 : 0 : sh->send_to_kernel_action[i].action = NULL;
821 : : }
822 [ # # ]: 0 : if (sh->send_to_kernel_action[i].tbl) {
823 : : struct mlx5_flow_tbl_resource *tbl =
824 : : sh->send_to_kernel_action[i].tbl;
825 : :
826 : 0 : mlx5_flow_dv_tbl_resource_release(sh, tbl);
827 : 0 : sh->send_to_kernel_action[i].tbl = NULL;
828 : : }
829 : : }
830 : : #endif /* HAVE_MLX5DV_DR */
831 [ # # ]: 0 : if (sh->default_miss_action)
832 : 0 : mlx5_glue->destroy_flow_action
833 : : (sh->default_miss_action);
834 [ # # ]: 0 : if (sh->encaps_decaps) {
835 : 0 : mlx5_hlist_destroy(sh->encaps_decaps);
836 : 0 : sh->encaps_decaps = NULL;
837 : : }
838 [ # # ]: 0 : if (sh->modify_cmds) {
839 : 0 : mlx5_hlist_destroy(sh->modify_cmds);
840 : 0 : sh->modify_cmds = NULL;
841 : : }
842 [ # # ]: 0 : if (sh->tag_table) {
843 : : /* tags should be destroyed with flow before. */
844 : 0 : mlx5_hlist_destroy(sh->tag_table);
845 : 0 : sh->tag_table = NULL;
846 : : }
847 [ # # ]: 0 : if (sh->tunnel_hub) {
848 : 0 : mlx5_release_tunnel_hub(sh, priv->dev_port);
849 : 0 : sh->tunnel_hub = NULL;
850 : : }
851 : 0 : mlx5_free_table_hash_list(priv);
852 [ # # ]: 0 : if (sh->port_id_action_list) {
853 : 0 : mlx5_list_destroy(sh->port_id_action_list);
854 : 0 : sh->port_id_action_list = NULL;
855 : : }
856 [ # # ]: 0 : if (sh->push_vlan_action_list) {
857 : 0 : mlx5_list_destroy(sh->push_vlan_action_list);
858 : 0 : sh->push_vlan_action_list = NULL;
859 : : }
860 [ # # ]: 0 : if (sh->sample_action_list) {
861 : 0 : mlx5_list_destroy(sh->sample_action_list);
862 : 0 : sh->sample_action_list = NULL;
863 : : }
864 [ # # ]: 0 : if (sh->dest_array_list) {
865 : 0 : mlx5_list_destroy(sh->dest_array_list);
866 : 0 : sh->dest_array_list = NULL;
867 : : }
868 [ # # ]: 0 : if (sh->mreg_cp_tbl) {
869 : 0 : mlx5_hlist_destroy(sh->mreg_cp_tbl);
870 : 0 : sh->mreg_cp_tbl = NULL;
871 : : }
872 : : }
873 : :
874 : : /**
875 : : * Initialize shared data between primary and secondary process.
876 : : *
877 : : * A memzone is reserved by primary process and secondary processes attach to
878 : : * the memzone.
879 : : *
880 : : * @return
881 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
882 : : */
883 : : static int
884 : 0 : mlx5_init_shared_data(void)
885 : : {
886 : : const struct rte_memzone *mz;
887 : : int ret = 0;
888 : :
889 : : rte_spinlock_lock(&mlx5_shared_data_lock);
890 [ # # ]: 0 : if (mlx5_shared_data == NULL) {
891 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
892 : : /* Allocate shared memory. */
893 : 0 : mz = rte_memzone_reserve(MZ_MLX5_PMD_SHARED_DATA,
894 : : sizeof(*mlx5_shared_data),
895 : : SOCKET_ID_ANY, 0);
896 [ # # ]: 0 : if (mz == NULL) {
897 : 0 : DRV_LOG(ERR,
898 : : "Cannot allocate mlx5 shared data");
899 : 0 : ret = -rte_errno;
900 : 0 : goto error;
901 : : }
902 : 0 : mlx5_shared_data = mz->addr;
903 : : memset(mlx5_shared_data, 0, sizeof(*mlx5_shared_data));
904 : 0 : rte_spinlock_init(&mlx5_shared_data->lock);
905 : : } else {
906 : : /* Lookup allocated shared memory. */
907 : 0 : mz = rte_memzone_lookup(MZ_MLX5_PMD_SHARED_DATA);
908 [ # # ]: 0 : if (mz == NULL) {
909 : 0 : DRV_LOG(ERR,
910 : : "Cannot attach mlx5 shared data");
911 : 0 : ret = -rte_errno;
912 : 0 : goto error;
913 : : }
914 : 0 : mlx5_shared_data = mz->addr;
915 : : memset(&mlx5_local_data, 0, sizeof(mlx5_local_data));
916 : : }
917 : : }
918 : 0 : error:
919 : : rte_spinlock_unlock(&mlx5_shared_data_lock);
920 : 0 : return ret;
921 : : }
922 : :
923 : : /**
924 : : * PMD global initialization.
925 : : *
926 : : * Independent from individual device, this function initializes global
927 : : * per-PMD data structures distinguishing primary and secondary processes.
928 : : * Hence, each initialization is called once per a process.
929 : : *
930 : : * @return
931 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
932 : : */
933 : : static int
934 : 0 : mlx5_init_once(void)
935 : : {
936 : : struct mlx5_shared_data *sd;
937 : : struct mlx5_local_data *ld = &mlx5_local_data;
938 : : int ret = 0;
939 : :
940 [ # # ]: 0 : if (mlx5_init_shared_data())
941 : 0 : return -rte_errno;
942 : 0 : sd = mlx5_shared_data;
943 : : MLX5_ASSERT(sd);
944 : 0 : rte_spinlock_lock(&sd->lock);
945 [ # # # ]: 0 : switch (rte_eal_process_type()) {
946 : 0 : case RTE_PROC_PRIMARY:
947 [ # # ]: 0 : if (sd->init_done)
948 : : break;
949 : 0 : ret = mlx5_mp_init_primary(MLX5_MP_NAME,
950 : : mlx5_mp_os_primary_handle);
951 [ # # ]: 0 : if (ret)
952 : 0 : goto out;
953 : 0 : sd->init_done = true;
954 : 0 : break;
955 : 0 : case RTE_PROC_SECONDARY:
956 [ # # ]: 0 : if (ld->init_done)
957 : : break;
958 : 0 : ret = mlx5_mp_init_secondary(MLX5_MP_NAME,
959 : : mlx5_mp_os_secondary_handle);
960 [ # # ]: 0 : if (ret)
961 : 0 : goto out;
962 : 0 : ++sd->secondary_cnt;
963 : 0 : ld->init_done = true;
964 : 0 : break;
965 : : default:
966 : : break;
967 : : }
968 : 0 : out:
969 : : rte_spinlock_unlock(&sd->lock);
970 : 0 : return ret;
971 : : }
972 : :
973 : : /**
974 : : * DR flow drop action support detect.
975 : : *
976 : : * @param dev
977 : : * Pointer to rte_eth_dev structure.
978 : : *
979 : : */
980 : : static void
981 : 0 : mlx5_flow_drop_action_config(struct rte_eth_dev *dev __rte_unused)
982 : : {
983 : : #ifdef HAVE_MLX5DV_DR
984 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
985 : :
986 [ # # # # ]: 0 : if (!priv->sh->config.dv_flow_en || !priv->sh->dr_drop_action)
987 : : return;
988 : : /**
989 : : * DR supports drop action placeholder when it is supported;
990 : : * otherwise, use the queue drop action.
991 : : */
992 [ # # ]: 0 : if (!priv->sh->drop_action_check_flag) {
993 [ # # ]: 0 : if (!mlx5_flow_discover_dr_action_support(dev))
994 : 0 : priv->sh->dr_root_drop_action_en = 1;
995 : 0 : priv->sh->drop_action_check_flag = 1;
996 : : }
997 [ # # ]: 0 : if (priv->sh->dr_root_drop_action_en)
998 : 0 : priv->root_drop_action = priv->sh->dr_drop_action;
999 : : else
1000 : 0 : priv->root_drop_action = priv->drop_queue.hrxq->action;
1001 : : #endif
1002 : : }
1003 : :
1004 : : static void
1005 : 0 : mlx5_queue_counter_id_prepare(struct rte_eth_dev *dev)
1006 : : {
1007 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
1008 : 0 : void *ctx = priv->sh->cdev->ctx;
1009 : :
1010 : 0 : priv->q_counters = mlx5_devx_cmd_queue_counter_alloc(ctx, NULL);
1011 [ # # ]: 0 : if (!priv->q_counters) {
1012 : 0 : struct ibv_cq *cq = mlx5_glue->create_cq(ctx, 1, NULL, NULL, 0);
1013 : : struct ibv_wq *wq;
1014 : :
1015 : 0 : DRV_LOG(DEBUG, "Port %d queue counter object cannot be created "
1016 : : "by DevX - fall-back to use the kernel driver global "
1017 : : "queue counter.", dev->data->port_id);
1018 : :
1019 : : /* Create WQ by kernel and query its queue counter ID. */
1020 [ # # ]: 0 : if (cq) {
1021 : 0 : wq = mlx5_glue->create_wq(ctx,
1022 : 0 : &(struct ibv_wq_init_attr){
1023 : : .wq_type = IBV_WQT_RQ,
1024 : : .max_wr = 1,
1025 : : .max_sge = 1,
1026 : 0 : .pd = priv->sh->cdev->pd,
1027 : : .cq = cq,
1028 : : });
1029 [ # # ]: 0 : if (wq) {
1030 : : /* Counter is assigned only on RDY state. */
1031 : 0 : int ret = mlx5_glue->modify_wq(wq,
1032 : 0 : &(struct ibv_wq_attr){
1033 : : .attr_mask = IBV_WQ_ATTR_STATE,
1034 : : .wq_state = IBV_WQS_RDY,
1035 : : });
1036 : :
1037 [ # # ]: 0 : if (ret == 0)
1038 : 0 : mlx5_devx_cmd_wq_query(wq,
1039 : : &priv->counter_set_id);
1040 : 0 : claim_zero(mlx5_glue->destroy_wq(wq));
1041 : : }
1042 : 0 : claim_zero(mlx5_glue->destroy_cq(cq));
1043 : : }
1044 : : } else {
1045 : 0 : priv->counter_set_id = priv->q_counters->id;
1046 : : }
1047 [ # # ]: 0 : if (priv->counter_set_id == 0)
1048 : 0 : DRV_LOG(INFO, "Part of the port %d statistics will not be "
1049 : : "available.", dev->data->port_id);
1050 : 0 : }
1051 : :
1052 : : static inline bool
1053 : : mlx5_ignore_pf_representor(const struct rte_eth_devargs *eth_da)
1054 : : {
1055 : 0 : return (eth_da->flags & RTE_ETH_DEVARG_REPRESENTOR_IGNORE_PF) != 0;
1056 : : }
1057 : :
1058 : : static bool
1059 : : is_standard_eswitch(const struct mlx5_dev_spawn_data *spawn)
1060 : : {
1061 : 0 : bool is_bond = spawn->pf_bond >= 0;
1062 : :
1063 [ # # # # : 0 : return !is_bond && spawn->nb_uplinks <= 1 && spawn->nb_hpfs <= 1;
# # # # #
# ]
1064 : : }
1065 : :
1066 : : static bool
1067 : : is_hpf(const struct mlx5_dev_spawn_data *spawn)
1068 : : {
1069 [ # # # # : 0 : return spawn->info.port_name == -1 &&
# # # # ]
1070 [ # # # # ]: 0 : spawn->info.name_type == MLX5_PHYS_PORT_NAME_TYPE_PFHPF;
1071 : : }
1072 : :
1073 : : static int
1074 : 0 : build_port_name(struct rte_device *dpdk_dev,
1075 : : struct mlx5_dev_spawn_data *spawn,
1076 : : char *name,
1077 : : size_t name_sz)
1078 : : {
1079 : 0 : bool is_bond = spawn->pf_bond >= 0;
1080 : : int written = 0;
1081 : : int ret;
1082 : :
1083 [ # # ]: 0 : ret = snprintf(name, name_sz, "%s", dpdk_dev->name);
1084 [ # # ]: 0 : if (ret < 0)
1085 : : return ret;
1086 : : written += ret;
1087 [ # # ]: 0 : if (written >= (int)name_sz)
1088 : : return written;
1089 : :
1090 : : /*
1091 : : * Whenever bond device is detected, include IB device name.
1092 : : * This is kept to keep port naming backward compatible.
1093 : : */
1094 [ # # ]: 0 : if (is_bond) {
1095 [ # # ]: 0 : ret = snprintf(name + written, name_sz - written, "_%s", spawn->phys_dev_name);
1096 [ # # ]: 0 : if (ret < 0)
1097 : : return ret;
1098 : 0 : written += ret;
1099 [ # # ]: 0 : if (written >= (int)name_sz)
1100 : : return written;
1101 : : }
1102 : :
1103 [ # # ]: 0 : if (spawn->info.name_type == MLX5_PHYS_PORT_NAME_TYPE_UPLINK) {
1104 : : /* Add port to name if and only if there is more than one uplink. */
1105 [ # # ]: 0 : if (spawn->nb_uplinks <= 1)
1106 : 0 : goto end;
1107 : :
1108 [ # # ]: 0 : ret = snprintf(name + written, name_sz - written, "_p%u", spawn->info.port_name);
1109 [ # # ]: 0 : if (ret < 0)
1110 : : return ret;
1111 : 0 : written += ret;
1112 : : if (written >= (int)name_sz)
1113 : : return written;
1114 [ # # ]: 0 : } else if (spawn->info.representor) {
1115 : : /*
1116 : : * If port is a representor, then switchdev has been enabled.
1117 : : * In that case add controller, PF and VF/SF indexes to port name
1118 : : * if at least one of these conditions are met:
1119 : : * 1. Device is a bond (VF-LAG).
1120 : : * 2. There are multiple uplinks (MPESW).
1121 : : * 3. There are multiple host PFs (BlueField socket direct).
1122 : : *
1123 : : * If none of these conditions apply, then it is assumed that
1124 : : * this device manages a single non-shared E-Switch with single controller,
1125 : : * where there is only one uplink/PF and one host PF (on BlueField).
1126 : : */
1127 [ # # ]: 0 : if (!is_standard_eswitch(spawn))
1128 [ # # ]: 0 : ret = snprintf(name + written, name_sz - written,
1129 : : "_representor_c%dpf%d%s%u",
1130 : : spawn->info.ctrl_num,
1131 : : spawn->info.pf_num,
1132 : : spawn->info.name_type ==
1133 : : MLX5_PHYS_PORT_NAME_TYPE_PFSF ? "sf" : "vf",
1134 : : spawn->info.port_name);
1135 : : else
1136 [ # # ]: 0 : ret = snprintf(name + written, name_sz - written, "_representor_%s%u",
1137 : : spawn->info.name_type ==
1138 : : MLX5_PHYS_PORT_NAME_TYPE_PFSF ? "sf" : "vf",
1139 : : spawn->info.port_name);
1140 [ # # ]: 0 : if (ret < 0)
1141 : : return ret;
1142 : 0 : written += ret;
1143 : : if (written >= (int)name_sz)
1144 : : return written;
1145 : : }
1146 : :
1147 : 0 : end:
1148 : : return written;
1149 : : }
1150 : :
1151 : : static bool
1152 : : representor_match_uplink(const struct mlx5_dev_spawn_data *spawn,
1153 : : uint16_t port_name,
1154 : : const struct rte_eth_devargs *eth_da,
1155 : : uint16_t eth_da_pf_num)
1156 : : {
1157 : 0 : if (spawn->info.name_type != MLX5_PHYS_PORT_NAME_TYPE_UPLINK)
1158 : : return false;
1159 : : /* One of the uplinks will be a transfer proxy. Must be probed always. */
1160 [ # # # # ]: 0 : if (spawn->info.master)
1161 : : return true;
1162 [ # # # # ]: 0 : if (mlx5_ignore_pf_representor(eth_da))
1163 : : return false;
1164 : :
1165 : 0 : return port_name == eth_da_pf_num;
1166 : : }
1167 : :
1168 : : static bool
1169 : 0 : representor_match_port(const struct mlx5_dev_spawn_data *spawn,
1170 : : const struct rte_eth_devargs *eth_da)
1171 : : {
1172 [ # # ]: 0 : for (uint16_t p = 0; p < eth_da->nb_ports; ++p) {
1173 : 0 : uint16_t pf_num = eth_da->ports[p];
1174 : :
1175 : : /* PF representor in devargs is interpreted as probing uplink port. */
1176 [ # # ]: 0 : if (eth_da->type == RTE_ETH_REPRESENTOR_PF) {
1177 [ # # # # ]: 0 : if (representor_match_uplink(spawn, spawn->info.port_name, eth_da, pf_num))
1178 : : return true;
1179 : :
1180 : 0 : continue;
1181 : : }
1182 : :
1183 : : /* Allow probing related uplink when VF/SF representor is requested. */
1184 [ # # ]: 0 : if ((eth_da->type == RTE_ETH_REPRESENTOR_VF ||
1185 [ # # ]: 0 : eth_da->type == RTE_ETH_REPRESENTOR_SF) &&
1186 [ # # ]: 0 : representor_match_uplink(spawn, spawn->info.pf_num, eth_da, pf_num))
1187 : : return true;
1188 : :
1189 : : /* Uplink ports should not be matched against representor_ports. */
1190 [ # # ]: 0 : if (spawn->info.name_type == MLX5_PHYS_PORT_NAME_TYPE_UPLINK)
1191 : 0 : continue;
1192 : :
1193 [ # # ]: 0 : for (uint16_t f = 0; f < eth_da->nb_representor_ports; ++f) {
1194 [ # # ]: 0 : uint16_t port_num = eth_da->representor_ports[f];
1195 : : bool pf_num_match;
1196 : : bool rep_num_match;
1197 : :
1198 : : /*
1199 : : * In standard E-Switch case, allow probing VFs even if wrong PF index
1200 : : * was provided.
1201 : : */
1202 [ # # ]: 0 : if (is_standard_eswitch(spawn))
1203 : : pf_num_match = true;
1204 : : else
1205 : 0 : pf_num_match = spawn->info.pf_num == pf_num;
1206 : :
1207 : : /* Host PF is indicated through VF/SF representor index == -1. */
1208 [ # # ]: 0 : if (is_hpf(spawn))
1209 : 0 : rep_num_match = port_num == UINT16_MAX;
1210 : : else
1211 : 0 : rep_num_match = port_num == spawn->info.port_name;
1212 : :
1213 [ # # ]: 0 : if (pf_num_match && rep_num_match)
1214 : : return true;
1215 : : }
1216 : : }
1217 : :
1218 : : return false;
1219 : : }
1220 : :
1221 : : /**
1222 : : * Check if representor spawn info match devargs.
1223 : : *
1224 : : * @param spawn
1225 : : * Verbs device parameters (name, port, switch_info) to spawn.
1226 : : * @param eth_da
1227 : : * Device devargs to probe.
1228 : : *
1229 : : * @return
1230 : : * Match result.
1231 : : */
1232 : : static bool
1233 : 0 : mlx5_representor_match(struct mlx5_dev_spawn_data *spawn,
1234 : : struct rte_eth_devargs *eth_da)
1235 : : {
1236 : : struct mlx5_switch_info *switch_info = &spawn->info;
1237 : : unsigned int c;
1238 [ # # ]: 0 : bool ignore_ctrl_num = eth_da->nb_mh_controllers == 0 ||
1239 [ # # ]: 0 : switch_info->name_type == MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
1240 : :
1241 [ # # # # : 0 : switch (eth_da->type) {
# ]
1242 : 0 : case RTE_ETH_REPRESENTOR_PF:
1243 [ # # ]: 0 : if (switch_info->name_type != MLX5_PHYS_PORT_NAME_TYPE_UPLINK) {
1244 : 0 : rte_errno = EBUSY;
1245 : 0 : return false;
1246 : : }
1247 : : break;
1248 : : case RTE_ETH_REPRESENTOR_SF:
1249 [ # # ]: 0 : if (!is_hpf(spawn) &&
1250 [ # # # # ]: 0 : switch_info->name_type != MLX5_PHYS_PORT_NAME_TYPE_PFSF &&
1251 : : switch_info->name_type != MLX5_PHYS_PORT_NAME_TYPE_UPLINK) {
1252 : 0 : rte_errno = EBUSY;
1253 : 0 : return false;
1254 : : }
1255 : : break;
1256 : : case RTE_ETH_REPRESENTOR_VF:
1257 [ # # ]: 0 : if (!is_hpf(spawn) &&
1258 [ # # # # ]: 0 : switch_info->name_type != MLX5_PHYS_PORT_NAME_TYPE_PFVF &&
1259 : : switch_info->name_type != MLX5_PHYS_PORT_NAME_TYPE_UPLINK) {
1260 : 0 : rte_errno = EBUSY;
1261 : 0 : return false;
1262 : : }
1263 : : break;
1264 : 0 : case RTE_ETH_REPRESENTOR_NONE:
1265 : 0 : rte_errno = EBUSY;
1266 : 0 : return false;
1267 : 0 : default:
1268 : 0 : rte_errno = ENOTSUP;
1269 : 0 : DRV_LOG(ERR, "unsupported representor type");
1270 : 0 : return false;
1271 : : }
1272 [ # # ]: 0 : if (!ignore_ctrl_num) {
1273 [ # # ]: 0 : for (c = 0; c < eth_da->nb_mh_controllers; ++c) {
1274 : 0 : uint16_t ctrl_num = eth_da->mh_controllers[c];
1275 : :
1276 [ # # # # ]: 0 : if (spawn->info.ctrl_num == ctrl_num &&
1277 : 0 : representor_match_port(spawn, eth_da))
1278 : : return true;
1279 : : }
1280 : : } else {
1281 [ # # ]: 0 : if (representor_match_port(spawn, eth_da))
1282 : : return true;
1283 : : }
1284 : 0 : rte_errno = EBUSY;
1285 : 0 : return false;
1286 : : }
1287 : :
1288 : : /**
1289 : : * Spawn an Ethernet device from Verbs information.
1290 : : *
1291 : : * @param dpdk_dev
1292 : : * Backing DPDK device.
1293 : : * @param spawn
1294 : : * Verbs device parameters (name, port, switch_info) to spawn.
1295 : : * @param eth_da
1296 : : * Device arguments.
1297 : : * @param mkvlist
1298 : : * Pointer to mlx5 kvargs control, can be NULL if there is no devargs.
1299 : : *
1300 : : * @return
1301 : : * A valid Ethernet device object on success, NULL otherwise and rte_errno
1302 : : * is set. The following errors are defined:
1303 : : *
1304 : : * EBUSY: device is not supposed to be spawned.
1305 : : * EEXIST: device is already spawned
1306 : : */
1307 : : static struct rte_eth_dev *
1308 : 0 : mlx5_dev_spawn(struct rte_device *dpdk_dev,
1309 : : struct mlx5_dev_spawn_data *spawn,
1310 : : struct rte_eth_devargs *eth_da,
1311 : : struct mlx5_kvargs_ctrl *mkvlist)
1312 : : {
1313 : 0 : const struct mlx5_switch_info *switch_info = &spawn->info;
1314 : : struct mlx5_dev_ctx_shared *sh = NULL;
1315 : 0 : struct ibv_port_attr port_attr = { .state = IBV_PORT_NOP };
1316 : : struct rte_eth_dev *eth_dev = NULL;
1317 : : struct mlx5_priv *priv = NULL;
1318 : : int err = 0;
1319 : : struct rte_ether_addr mac;
1320 : : char name[RTE_ETH_NAME_MAX_LEN];
1321 : : int own_domain_id = 0;
1322 : : uint16_t port_id;
1323 : 0 : struct mlx5_port_info vport_info = { .query_flags = 0 };
1324 : : int nl_rdma;
1325 : : int i;
1326 : : struct mlx5_indexed_pool_config icfg[RTE_DIM(default_icfg)];
1327 : :
1328 : : memcpy(icfg, default_icfg, sizeof(icfg));
1329 : : /* Determine if this port representor is supposed to be spawned. */
1330 [ # # # # : 0 : if (switch_info->representor && dpdk_dev->devargs &&
# # ]
1331 : 0 : !mlx5_representor_match(spawn, eth_da))
1332 : : return NULL;
1333 : : /* Build device name. */
1334 : 0 : err = build_port_name(dpdk_dev, spawn, name, sizeof(name));
1335 [ # # ]: 0 : if (err < 0) {
1336 : 0 : DRV_LOG(ERR, "Failed to build port name for IB device %s/%u",
1337 : : spawn->phys_dev_name, spawn->phys_port);
1338 : 0 : rte_errno = EINVAL;
1339 : 0 : return NULL;
1340 : : }
1341 [ # # ]: 0 : if (err >= (int)sizeof(name))
1342 : 0 : DRV_LOG(WARNING, "device name overflow %s", name);
1343 : : /* check if the device is already spawned */
1344 [ # # ]: 0 : if (rte_eth_dev_get_port_by_name(name, &port_id) == 0) {
1345 : : /*
1346 : : * When device is already spawned, its devargs should be set
1347 : : * as used. otherwise, mlx5_kvargs_validate() will fail.
1348 : : */
1349 [ # # ]: 0 : if (mkvlist)
1350 : 0 : mlx5_port_args_set_used(name, port_id, mkvlist);
1351 : 0 : rte_errno = EEXIST;
1352 : 0 : return NULL;
1353 : : }
1354 : 0 : DRV_LOG(DEBUG, "naming Ethernet device \"%s\"", name);
1355 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
1356 : : struct mlx5_mp_id mp_id;
1357 : : int fd;
1358 : :
1359 : 0 : eth_dev = rte_eth_dev_attach_secondary(name);
1360 [ # # ]: 0 : if (eth_dev == NULL) {
1361 : 0 : DRV_LOG(ERR, "can not attach rte ethdev");
1362 : 0 : rte_errno = ENOMEM;
1363 : 0 : return NULL;
1364 : : }
1365 : 0 : eth_dev->device = dpdk_dev;
1366 : 0 : eth_dev->dev_ops = &mlx5_dev_sec_ops;
1367 : 0 : eth_dev->rx_descriptor_status = mlx5_rx_descriptor_status;
1368 : 0 : eth_dev->tx_descriptor_status = mlx5_tx_descriptor_status;
1369 : 0 : err = mlx5_proc_priv_init(eth_dev);
1370 [ # # ]: 0 : if (err)
1371 : : return NULL;
1372 : 0 : mlx5_mp_id_init(&mp_id, eth_dev->data->port_id);
1373 : : /* Receive command fd from primary process */
1374 : 0 : fd = mlx5_mp_req_verbs_cmd_fd(&mp_id);
1375 [ # # ]: 0 : if (fd < 0)
1376 : 0 : goto err_secondary;
1377 : : /* Remap UAR for Tx queues. */
1378 : 0 : err = mlx5_tx_uar_init_secondary(eth_dev, fd);
1379 : 0 : close(fd);
1380 [ # # ]: 0 : if (err)
1381 : 0 : goto err_secondary;
1382 : : /*
1383 : : * Ethdev pointer is still required as input since
1384 : : * the primary device is not accessible from the
1385 : : * secondary process.
1386 : : */
1387 : 0 : eth_dev->rx_pkt_burst = mlx5_select_rx_function(eth_dev);
1388 : 0 : eth_dev->tx_pkt_burst = mlx5_select_tx_function(eth_dev);
1389 : 0 : return eth_dev;
1390 : 0 : err_secondary:
1391 : 0 : mlx5_dev_close(eth_dev);
1392 : 0 : return NULL;
1393 : : }
1394 : 0 : sh = mlx5_alloc_shared_dev_ctx(spawn, mkvlist);
1395 [ # # ]: 0 : if (!sh)
1396 : : return NULL;
1397 : 0 : nl_rdma = mlx5_nl_init(NETLINK_RDMA, 0);
1398 : : /* Check port status. */
1399 [ # # ]: 0 : if (spawn->phys_port <= UINT8_MAX) {
1400 : : /* Legacy Verbs api only support u8 port number. */
1401 : 0 : err = mlx5_glue->query_port(sh->cdev->ctx, spawn->phys_port,
1402 : : &port_attr);
1403 [ # # ]: 0 : if (err) {
1404 : 0 : DRV_LOG(ERR, "port query failed: %s", strerror(err));
1405 : 0 : goto error;
1406 : : }
1407 [ # # ]: 0 : if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
1408 : 0 : DRV_LOG(ERR, "port is not configured in Ethernet mode");
1409 : : err = EINVAL;
1410 : 0 : goto error;
1411 : : }
1412 [ # # ]: 0 : } else if (nl_rdma >= 0) {
1413 : : /* IB doesn't allow more than 255 ports, must be Ethernet. */
1414 : 0 : err = mlx5_nl_port_state(nl_rdma,
1415 : : spawn->phys_dev_name,
1416 : 0 : spawn->phys_port, &spawn->cdev->dev_info);
1417 [ # # ]: 0 : if (err < 0) {
1418 : 0 : DRV_LOG(INFO, "Failed to get netlink port state: %s",
1419 : : strerror(rte_errno));
1420 : 0 : err = -rte_errno;
1421 : 0 : goto error;
1422 : : }
1423 : 0 : port_attr.state = (enum ibv_port_state)err;
1424 : : }
1425 [ # # ]: 0 : if (port_attr.state != IBV_PORT_ACTIVE)
1426 : 0 : DRV_LOG(INFO, "port is not active: \"%s\" (%d)",
1427 : : mlx5_glue->port_state_str(port_attr.state),
1428 : : port_attr.state);
1429 : : /* Allocate private eth device data. */
1430 : 0 : priv = mlx5_malloc(MLX5_MEM_ZERO | MLX5_MEM_RTE,
1431 : : sizeof(*priv),
1432 : : RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
1433 [ # # ]: 0 : if (priv == NULL) {
1434 : 0 : DRV_LOG(ERR, "priv allocation failure");
1435 : : err = ENOMEM;
1436 : 0 : goto error;
1437 : : }
1438 : : /*
1439 : : * When user configures remote PD and CTX and device creates RxQ by
1440 : : * DevX, external RxQ is both supported and requested.
1441 : : */
1442 [ # # # # : 0 : if (mlx5_imported_pd_and_ctx(sh->cdev) && mlx5_devx_obj_ops_en(sh)) {
# # ]
1443 : 0 : priv->ext_rxqs = mlx5_malloc(MLX5_MEM_ZERO | MLX5_MEM_RTE,
1444 : : sizeof(struct mlx5_external_q) *
1445 : : MLX5_MAX_EXT_RX_QUEUES, 0,
1446 : : SOCKET_ID_ANY);
1447 [ # # ]: 0 : if (priv->ext_rxqs == NULL) {
1448 : 0 : DRV_LOG(ERR, "Fail to allocate external RxQ array.");
1449 : : err = ENOMEM;
1450 : 0 : goto error;
1451 : : }
1452 : 0 : priv->ext_txqs = mlx5_malloc(MLX5_MEM_ZERO | MLX5_MEM_RTE,
1453 : : sizeof(struct mlx5_external_q) *
1454 : : MLX5_MAX_EXT_TX_QUEUES, 0,
1455 : : SOCKET_ID_ANY);
1456 [ # # ]: 0 : if (priv->ext_txqs == NULL) {
1457 : 0 : DRV_LOG(ERR, "Fail to allocate external TxQ array.");
1458 : : err = ENOMEM;
1459 : 0 : goto error;
1460 : : }
1461 : 0 : DRV_LOG(DEBUG, "External queue is supported.");
1462 : : }
1463 : 0 : priv->sh = sh;
1464 : 0 : priv->dev_port = spawn->phys_port;
1465 : 0 : priv->pci_dev = spawn->pci_dev;
1466 : : /* Some internal functions rely on Netlink sockets, open them now. */
1467 : 0 : priv->nl_socket_rdma = nl_rdma;
1468 : 0 : priv->nl_socket_route = mlx5_nl_init(NETLINK_ROUTE, 0);
1469 : 0 : priv->representor = !!switch_info->representor;
1470 : 0 : priv->master = !!switch_info->master;
1471 : 0 : priv->domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
1472 : 0 : priv->vport_meta_tag = 0;
1473 : 0 : priv->vport_meta_mask = 0;
1474 : 0 : priv->pf_bond = spawn->pf_bond;
1475 : 0 : priv->mpesw_port = spawn->mpesw_port;
1476 : 0 : priv->mpesw_uplink = false;
1477 : 0 : priv->mpesw_owner = spawn->info.mpesw_owner;
1478 [ # # ]: 0 : if (mlx5_is_port_on_mpesw_device(priv))
1479 : 0 : priv->mpesw_uplink = (spawn->info.name_type == MLX5_PHYS_PORT_NAME_TYPE_UPLINK);
1480 : :
1481 [ # # ]: 0 : DRV_LOG(DEBUG,
1482 : : "dev_port=%u bus=%s pci=%s master=%d representor=%d pf_bond=%d "
1483 : : "mpesw_port=%d mpesw_uplink=%d",
1484 : : priv->dev_port, dpdk_dev->bus->name,
1485 : : priv->pci_dev ? priv->pci_dev->name : "NONE",
1486 : : priv->master, priv->representor, priv->pf_bond,
1487 : : priv->mpesw_port, priv->mpesw_uplink);
1488 : :
1489 [ # # # # ]: 0 : if (mlx5_is_port_on_mpesw_device(priv) && priv->sh->config.dv_flow_en != 2) {
1490 : 0 : DRV_LOG(ERR, "MPESW device is supported only with HWS");
1491 : : err = ENOTSUP;
1492 : 0 : goto error;
1493 : : }
1494 : : /*
1495 : : * If we have E-Switch we should determine the vport attributes.
1496 : : * E-Switch may use either source vport field or reg_c[0] metadata
1497 : : * register to match on vport index. The engaged part of metadata
1498 : : * register is defined by mask.
1499 : : */
1500 [ # # ]: 0 : if (sh->esw_mode) {
1501 : 0 : err = mlx5_glue->devx_port_query(sh->cdev->ctx,
1502 : : spawn->phys_port,
1503 : : &vport_info);
1504 [ # # ]: 0 : if (err) {
1505 : 0 : DRV_LOG(WARNING,
1506 : : "Cannot query devx port %d on device %s",
1507 : : spawn->phys_port, spawn->phys_dev_name);
1508 : 0 : vport_info.query_flags = 0;
1509 : : }
1510 : : }
1511 [ # # ]: 0 : if (vport_info.query_flags & MLX5_PORT_QUERY_REG_C0) {
1512 : 0 : priv->vport_meta_tag = vport_info.vport_meta_tag;
1513 : 0 : priv->vport_meta_mask = vport_info.vport_meta_mask;
1514 [ # # ]: 0 : if (!priv->vport_meta_mask) {
1515 : 0 : DRV_LOG(ERR,
1516 : : "vport zero mask for port %d on bonding device %s",
1517 : : spawn->phys_port, spawn->phys_dev_name);
1518 : : err = ENOTSUP;
1519 : 0 : goto error;
1520 : : }
1521 [ # # ]: 0 : if (priv->vport_meta_tag & ~priv->vport_meta_mask) {
1522 : 0 : DRV_LOG(ERR,
1523 : : "Invalid vport tag for port %d on bonding device %s",
1524 : : spawn->phys_port, spawn->phys_dev_name);
1525 : : err = ENOTSUP;
1526 : 0 : goto error;
1527 : : }
1528 : : }
1529 [ # # ]: 0 : if (vport_info.query_flags & MLX5_PORT_QUERY_VPORT_VHCA_ID)
1530 : 0 : priv->vport_vhca_id = vport_info.vport_vhca_id;
1531 [ # # ]: 0 : if (vport_info.query_flags & MLX5_PORT_QUERY_VPORT) {
1532 : 0 : priv->vport_id = vport_info.vport_id;
1533 [ # # # # ]: 0 : } else if (spawn->pf_bond >= 0 && sh->esw_mode) {
1534 : 0 : DRV_LOG(ERR,
1535 : : "Cannot deduce vport index for port %d on bonding device %s",
1536 : : spawn->phys_port, spawn->phys_dev_name);
1537 : : err = ENOTSUP;
1538 : 0 : goto error;
1539 : : } else {
1540 : : /*
1541 : : * Suppose vport index in compatible way. Kernel/rdma_core
1542 : : * support single E-Switch per PF configurations only and
1543 : : * vport_id field contains the vport index for associated VF,
1544 : : * which is deduced from representor port name.
1545 : : * For example, let's have the IB device port 10, it has
1546 : : * attached network device eth0, which has port name attribute
1547 : : * pf0vf2, we can deduce the VF number as 2, and set vport index
1548 : : * as 3 (2+1). This assigning schema should be changed if the
1549 : : * multiple E-Switch instances per PF configurations or/and PCI
1550 : : * subfunctions are added.
1551 : : */
1552 [ # # ]: 0 : priv->vport_id = switch_info->representor ?
1553 : 0 : switch_info->port_name + 1 : -1;
1554 : : }
1555 : 0 : priv->representor_id = mlx5_representor_id_encode(switch_info,
1556 : : eth_da->type);
1557 : : /*
1558 : : * Look for sibling devices in order to reuse their switch domain
1559 : : * if any, otherwise allocate one.
1560 : : */
1561 [ # # ]: 0 : MLX5_ETH_FOREACH_DEV(port_id, dpdk_dev) {
1562 : 0 : const struct mlx5_priv *opriv =
1563 : 0 : rte_eth_devices[port_id].data->dev_private;
1564 : :
1565 [ # # ]: 0 : if (!opriv ||
1566 [ # # ]: 0 : opriv->sh != priv->sh ||
1567 [ # # ]: 0 : opriv->domain_id ==
1568 : : RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID)
1569 : : continue;
1570 : 0 : priv->domain_id = opriv->domain_id;
1571 : 0 : DRV_LOG(DEBUG, "dev_port-%u inherit domain_id=%u\n",
1572 : : priv->dev_port, priv->domain_id);
1573 : 0 : break;
1574 : : }
1575 [ # # ]: 0 : if (priv->domain_id == RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
1576 : 0 : err = rte_eth_switch_domain_alloc(&priv->domain_id);
1577 [ # # ]: 0 : if (err) {
1578 : 0 : err = rte_errno;
1579 : 0 : DRV_LOG(ERR, "unable to allocate switch domain: %s",
1580 : : strerror(rte_errno));
1581 : 0 : goto error;
1582 : : }
1583 : : own_domain_id = 1;
1584 : 0 : DRV_LOG(DEBUG, "dev_port-%u new domain_id=%u\n",
1585 : : priv->dev_port, priv->domain_id);
1586 : : }
1587 [ # # ]: 0 : if (sh->cdev->config.devx) {
1588 : : struct mlx5_hca_attr *hca_attr = &sh->cdev->config.hca_attr;
1589 : :
1590 : 0 : sh->steering_format_version = hca_attr->steering_format_version;
1591 : : #if defined(HAVE_MLX5_DR_CREATE_ACTION_ASO_EXT)
1592 [ # # # # ]: 0 : if (hca_attr->qos.sup && hca_attr->qos.flow_meter_old &&
1593 : : sh->config.dv_flow_en) {
1594 [ # # ]: 0 : if (sh->registers.aso_reg != REG_NON) {
1595 : 0 : priv->mtr_en = 1;
1596 : 0 : priv->mtr_reg_share = hca_attr->qos.flow_meter;
1597 : : }
1598 : : }
1599 [ # # ]: 0 : if (hca_attr->qos.sup && hca_attr->qos.flow_meter_aso_sup) {
1600 : : uint32_t log_obj_size =
1601 : : rte_log2_u32(MLX5_ASO_MTRS_PER_POOL >> 1);
1602 : 0 : if (log_obj_size >=
1603 [ # # ]: 0 : hca_attr->qos.log_meter_aso_granularity &&
1604 : : log_obj_size <=
1605 [ # # ]: 0 : hca_attr->qos.log_meter_aso_max_alloc)
1606 : 0 : sh->meter_aso_en = 1;
1607 : : }
1608 [ # # ]: 0 : if (priv->mtr_en) {
1609 : 0 : err = mlx5_aso_flow_mtrs_mng_init(priv->sh);
1610 [ # # ]: 0 : if (err) {
1611 : 0 : err = -err;
1612 : 0 : goto error;
1613 : : }
1614 : : }
1615 [ # # ]: 0 : if (hca_attr->flow.tunnel_header_0_1)
1616 : 0 : sh->tunnel_header_0_1 = 1;
1617 [ # # ]: 0 : if (hca_attr->flow.tunnel_header_2_3)
1618 : 0 : sh->tunnel_header_2_3 = 1;
1619 : : #endif /* HAVE_MLX5_DR_CREATE_ACTION_ASO_EXT */
1620 : : #ifdef HAVE_MLX5_DR_CREATE_ACTION_ASO
1621 [ # # # # ]: 0 : if (hca_attr->flow_hit_aso && sh->registers.aso_reg == REG_C_3) {
1622 : 0 : sh->flow_hit_aso_en = 1;
1623 : 0 : err = mlx5_flow_aso_age_mng_init(sh);
1624 [ # # ]: 0 : if (err) {
1625 : 0 : err = -err;
1626 : 0 : goto error;
1627 : : }
1628 : 0 : DRV_LOG(DEBUG, "Flow Hit ASO is supported.");
1629 : : }
1630 : : #endif /* HAVE_MLX5_DR_CREATE_ACTION_ASO */
1631 : : #if defined (HAVE_MLX5_DR_CREATE_ACTION_ASO) && \
1632 : : defined (HAVE_MLX5_DR_ACTION_ASO_CT)
1633 : : /* HWS create CT ASO SQ based on HWS configure queue number. */
1634 [ # # # # ]: 0 : if (sh->config.dv_flow_en != 2 &&
1635 [ # # ]: 0 : hca_attr->ct_offload && sh->registers.aso_reg == REG_C_3) {
1636 : 0 : err = mlx5_flow_aso_ct_mng_init(sh);
1637 [ # # ]: 0 : if (err) {
1638 : 0 : err = -err;
1639 : 0 : goto error;
1640 : : }
1641 : 0 : DRV_LOG(DEBUG, "CT ASO is supported.");
1642 : 0 : sh->ct_aso_en = 1;
1643 : : }
1644 : : #endif /* HAVE_MLX5_DR_CREATE_ACTION_ASO && HAVE_MLX5_DR_ACTION_ASO_CT */
1645 : : #if defined(HAVE_MLX5DV_DR) && defined(HAVE_MLX5_DR_CREATE_ACTION_FLOW_SAMPLE)
1646 [ # # # # ]: 0 : if (hca_attr->log_max_ft_sampler_num > 0 &&
1647 : : sh->config.dv_flow_en) {
1648 : 0 : priv->sampler_en = 1;
1649 : 0 : DRV_LOG(DEBUG, "Sampler enabled!");
1650 : : } else {
1651 : 0 : priv->sampler_en = 0;
1652 [ # # ]: 0 : if (!hca_attr->log_max_ft_sampler_num)
1653 : 0 : DRV_LOG(WARNING,
1654 : : "No available register for sampler.");
1655 : : else
1656 : 0 : DRV_LOG(DEBUG, "DV flow is not supported!");
1657 : : }
1658 : : #endif
1659 [ # # ]: 0 : if (hca_attr->lag_rx_port_affinity) {
1660 : 0 : sh->lag_rx_port_affinity_en = 1;
1661 : 0 : DRV_LOG(DEBUG, "LAG Rx Port Affinity enabled");
1662 : : }
1663 : 0 : priv->num_lag_ports = hca_attr->num_lag_ports;
1664 : 0 : DRV_LOG(DEBUG, "The number of lag ports is %d", priv->num_lag_ports);
1665 : : }
1666 : : /* Process parameters and store port configuration on priv structure. */
1667 : 0 : err = mlx5_port_args_config(priv, mkvlist, &priv->config);
1668 [ # # ]: 0 : if (err) {
1669 : 0 : err = rte_errno;
1670 : 0 : DRV_LOG(ERR, "Failed to process port configure: %s",
1671 : : strerror(rte_errno));
1672 : 0 : goto error;
1673 : : }
1674 : 0 : eth_dev = rte_eth_dev_allocate(name);
1675 [ # # ]: 0 : if (eth_dev == NULL) {
1676 : 0 : DRV_LOG(ERR, "can not allocate rte ethdev");
1677 : : err = ENOMEM;
1678 : 0 : goto error;
1679 : : }
1680 [ # # ]: 0 : if (priv->representor) {
1681 : 0 : eth_dev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR;
1682 : 0 : eth_dev->data->representor_id = priv->representor_id;
1683 [ # # ]: 0 : MLX5_ETH_FOREACH_DEV(port_id, dpdk_dev) {
1684 : 0 : struct mlx5_priv *opriv =
1685 : 0 : rte_eth_devices[port_id].data->dev_private;
1686 [ # # # # ]: 0 : if (opriv &&
1687 : 0 : opriv->master &&
1688 [ # # ]: 0 : opriv->domain_id == priv->domain_id &&
1689 [ # # ]: 0 : opriv->sh == priv->sh) {
1690 : 0 : eth_dev->data->backer_port_id = port_id;
1691 : 0 : break;
1692 : : }
1693 : : }
1694 [ # # ]: 0 : if (port_id >= RTE_MAX_ETHPORTS)
1695 : 0 : eth_dev->data->backer_port_id = eth_dev->data->port_id;
1696 : : }
1697 : 0 : priv->mp_id.port_id = eth_dev->data->port_id;
1698 : 0 : strlcpy(priv->mp_id.name, MLX5_MP_NAME, RTE_MP_MAX_NAME_LEN);
1699 : : /*
1700 : : * Store associated network device interface index. This index
1701 : : * is permanent throughout the lifetime of device. So, we may store
1702 : : * the ifindex here and use the cached value further.
1703 : : */
1704 : : MLX5_ASSERT(spawn->ifindex);
1705 : 0 : priv->if_index = spawn->ifindex;
1706 : 0 : priv->lag_affinity_idx = sh->refcnt - 1;
1707 : 0 : eth_dev->data->dev_private = priv;
1708 : 0 : priv->dev_data = eth_dev->data;
1709 : 0 : eth_dev->data->mac_addrs = priv->mac;
1710 : 0 : eth_dev->device = dpdk_dev;
1711 : 0 : eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
1712 : : /* Fetch minimum and maximum allowed MTU from the device. */
1713 : 0 : mlx5_get_mtu_bounds(eth_dev, &priv->min_mtu, &priv->max_mtu);
1714 : : /* Configure the first MAC address by default. */
1715 [ # # ]: 0 : if (mlx5_get_mac(eth_dev, &mac.addr_bytes)) {
1716 : 0 : DRV_LOG(ERR,
1717 : : "port %u cannot get MAC address, is mlx5_en"
1718 : : " loaded? (errno: %s)",
1719 : : eth_dev->data->port_id, strerror(rte_errno));
1720 : : err = ENODEV;
1721 : 0 : goto error;
1722 : : }
1723 : 0 : DRV_LOG(INFO,
1724 : : "port %u MAC address is " RTE_ETHER_ADDR_PRT_FMT,
1725 : : eth_dev->data->port_id, RTE_ETHER_ADDR_BYTES(&mac));
1726 : : #ifdef RTE_PMD_MLX5_DEBUG
1727 : : {
1728 : : char ifname[MLX5_NAMESIZE];
1729 : :
1730 : : if (mlx5_get_ifname(eth_dev, ifname) == 0)
1731 : : DRV_LOG(DEBUG, "port %u ifname is \"%s\"",
1732 : : eth_dev->data->port_id, ifname);
1733 : : else
1734 : : DRV_LOG(DEBUG, "port %u ifname is unknown",
1735 : : eth_dev->data->port_id);
1736 : : }
1737 : : #endif
1738 : : /* Get actual MTU if possible. */
1739 : 0 : err = mlx5_get_mtu(eth_dev, ð_dev->data->mtu);
1740 [ # # ]: 0 : if (err) {
1741 : 0 : err = rte_errno;
1742 : 0 : goto error;
1743 : : }
1744 : 0 : DRV_LOG(DEBUG, "port %u MTU is %u", eth_dev->data->port_id,
1745 : : eth_dev->data->mtu);
1746 : : /* Initialize burst functions to prevent crashes before link-up. */
1747 : 0 : eth_dev->rx_pkt_burst = rte_eth_pkt_burst_dummy;
1748 : 0 : eth_dev->tx_pkt_burst = rte_eth_pkt_burst_dummy;
1749 : 0 : eth_dev->dev_ops = &mlx5_dev_ops;
1750 : 0 : eth_dev->rx_descriptor_status = mlx5_rx_descriptor_status;
1751 : 0 : eth_dev->tx_descriptor_status = mlx5_tx_descriptor_status;
1752 : 0 : eth_dev->rx_queue_count = mlx5_rx_queue_count;
1753 : : /* Register MAC address. */
1754 : 0 : claim_zero(mlx5_mac_addr_add(eth_dev, &mac, 0, 0));
1755 : : /* Sync mac addresses for PF or VF/SF if vf_nl_en is true */
1756 [ # # # # ]: 0 : if ((!sh->dev_cap.vf && !sh->dev_cap.sf) || sh->config.vf_nl_en)
1757 : 0 : mlx5_nl_mac_addr_sync(priv->nl_socket_route,
1758 : : mlx5_ifindex(eth_dev),
1759 : 0 : eth_dev->data->mac_addrs,
1760 : : MLX5_MAX_MAC_ADDRESSES);
1761 [ # # ]: 0 : priv->ctrl_flows = 0;
1762 : : rte_spinlock_init(&priv->flow_list_lock);
1763 : 0 : TAILQ_INIT(&priv->flow_meters);
1764 [ # # ]: 0 : if (priv->mtr_en) {
1765 : 0 : priv->mtr_profile_tbl = mlx5_l3t_create(MLX5_L3T_TYPE_PTR);
1766 [ # # ]: 0 : if (!priv->mtr_profile_tbl)
1767 : 0 : goto error;
1768 : : }
1769 : : /* Bring Ethernet device up. */
1770 : 0 : DRV_LOG(DEBUG, "port %u forcing Ethernet interface up",
1771 : : eth_dev->data->port_id);
1772 : : /* Read link status in case it is up and there will be no event. */
1773 : 0 : mlx5_link_update(eth_dev, 0);
1774 : : /* Watch LSC interrupts between port probe and port start. */
1775 : 0 : priv->sh->port[priv->dev_port - 1].nl_ih_port_id = eth_dev->data->port_id;
1776 : 0 : mlx5_set_link_up(eth_dev);
1777 [ # # ]: 0 : for (i = 0; i < MLX5_FLOW_TYPE_MAXI; i++) {
1778 : 0 : icfg[i].release_mem_en = !!sh->config.reclaim_mode;
1779 [ # # ]: 0 : if (sh->config.reclaim_mode)
1780 : 0 : icfg[i].per_core_cache = 0;
1781 : : #ifdef HAVE_MLX5_HWS_SUPPORT
1782 [ # # ]: 0 : if (priv->sh->config.dv_flow_en == 2) {
1783 : : icfg[i].size = sizeof(struct rte_flow_hw) + sizeof(struct rte_flow_nt2hws);
1784 : 0 : icfg[i].size += sizeof(struct rte_flow_hw_aux);
1785 : : }
1786 : : #endif
1787 : 0 : priv->flows[i] = mlx5_ipool_create(&icfg[i]);
1788 [ # # ]: 0 : if (!priv->flows[i])
1789 : 0 : goto error;
1790 : : }
1791 : : /* Create context for virtual machine VLAN workaround. */
1792 [ # # ]: 0 : priv->vmwa_context = mlx5_vlan_vmwa_init(eth_dev, spawn->ifindex);
1793 [ # # ]: 0 : if (mlx5_devx_obj_ops_en(sh)) {
1794 : 0 : priv->obj_ops = mlx5_devx_obj_ops;
1795 : 0 : mlx5_queue_counter_id_prepare(eth_dev);
1796 : 0 : priv->obj_ops.lb_dummy_queue_create =
1797 : : mlx5_rxq_ibv_obj_dummy_lb_create;
1798 : 0 : priv->obj_ops.lb_dummy_queue_release =
1799 : : mlx5_rxq_ibv_obj_dummy_lb_release;
1800 [ # # ]: 0 : } else if (spawn->max_port > UINT8_MAX) {
1801 : : /* Verbs can't support ports larger than 255 by design. */
1802 : 0 : DRV_LOG(ERR, "must enable DV and ESW when RDMA link ports > 255");
1803 : : err = ENOTSUP;
1804 : 0 : goto error;
1805 : : } else {
1806 : 0 : priv->obj_ops = mlx5_ibv_obj_ops;
1807 : : }
1808 [ # # ]: 0 : if (sh->config.tx_pp &&
1809 [ # # ]: 0 : priv->obj_ops.txq_obj_new != mlx5_txq_devx_obj_new) {
1810 : : /*
1811 : : * HAVE_MLX5DV_DEVX_UAR_OFFSET is required to support
1812 : : * packet pacing and already checked above.
1813 : : * Hence, we should only make sure the SQs will be created
1814 : : * with DevX, not with Verbs.
1815 : : * Verbs allocates the SQ UAR on its own and it can't be shared
1816 : : * with Clock Queue UAR as required for Tx scheduling.
1817 : : */
1818 : 0 : DRV_LOG(ERR, "Verbs SQs, UAR can't be shared as required for packet pacing");
1819 : : err = ENODEV;
1820 : 0 : goto error;
1821 : : }
1822 : 0 : priv->drop_queue.hrxq = mlx5_drop_action_create(eth_dev);
1823 [ # # ]: 0 : if (!priv->drop_queue.hrxq)
1824 : 0 : goto error;
1825 : 0 : priv->hrxqs = mlx5_list_create("hrxq", eth_dev, true,
1826 : : mlx5_hrxq_create_cb,
1827 : : mlx5_hrxq_match_cb,
1828 : : mlx5_hrxq_remove_cb,
1829 : : mlx5_hrxq_clone_cb,
1830 : : mlx5_hrxq_clone_free_cb);
1831 [ # # ]: 0 : if (!priv->hrxqs)
1832 : 0 : goto error;
1833 : 0 : mlx5_set_metadata_mask(eth_dev);
1834 [ # # ]: 0 : if (sh->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
1835 [ # # ]: 0 : !priv->sh->dv_regc0_mask) {
1836 : 0 : DRV_LOG(ERR, "metadata mode %u is not supported "
1837 : : "(no metadata reg_c[0] is available)",
1838 : : sh->config.dv_xmeta_en);
1839 : : err = ENOTSUP;
1840 : 0 : goto error;
1841 : : }
1842 : : rte_rwlock_init(&priv->ind_tbls_lock);
1843 [ # # ]: 0 : if (sh->config.dv_flow_en) {
1844 : 0 : err = mlx5_alloc_shared_dr(eth_dev);
1845 [ # # ]: 0 : if (err)
1846 : 0 : goto error;
1847 [ # # ]: 0 : if (mlx5_flex_item_port_init(eth_dev) < 0)
1848 : 0 : goto error;
1849 : : }
1850 [ # # ]: 0 : if (sh->phdev->config.ipv6_tc_fallback == MLX5_IPV6_TC_UNKNOWN) {
1851 : 0 : sh->phdev->config.ipv6_tc_fallback = MLX5_IPV6_TC_OK;
1852 [ # # ]: 0 : if (!sh->cdev->config.hca_attr.modify_outer_ipv6_traffic_class ||
1853 [ # # # # ]: 0 : (sh->config.dv_flow_en == 1 && mlx5_flow_discover_ipv6_tc_support(eth_dev)))
1854 : 0 : sh->phdev->config.ipv6_tc_fallback = MLX5_IPV6_TC_FALLBACK;
1855 : : }
1856 : : rte_spinlock_init(&priv->hw_ctrl_lock);
1857 : 0 : LIST_INIT(&priv->hw_ctrl_flows);
1858 : 0 : LIST_INIT(&priv->hw_ext_ctrl_flows);
1859 [ # # ]: 0 : if (priv->sh->config.dv_flow_en == 2) {
1860 : : #ifdef HAVE_MLX5_HWS_SUPPORT
1861 : : /*
1862 : : * Unified FDB flag is only needed for the actions created on the transfer
1863 : : * port. proxy port. It is not needed on the following ports:
1864 : : * 1. NIC PF / VF / SF
1865 : : * 2. in Verbs or DV/DR mode
1866 : : * 3. with unsupported FW
1867 : : * 4. all representors in HWS
1868 : : */
1869 [ # # ]: 0 : priv->unified_fdb_en = sh->cdev->config.hca_attr.fdb_unified_en &&
1870 [ # # ]: 0 : priv->master &&
1871 [ # # ]: 0 : priv->vport_meta_mask != 0;
1872 : : /* Jump FDB Rx works only with unified FDB enabled. */
1873 [ # # ]: 0 : if (priv->unified_fdb_en)
1874 : 0 : priv->jump_fdb_rx_en = sh->cdev->config.hca_attr.jump_fdb_rx_en;
1875 [ # # # # ]: 0 : DRV_LOG(DEBUG, "port %u: unified FDB %s enabled, jump_fdb_rx %s enabled.",
1876 : : eth_dev->data->port_id,
1877 : : priv->unified_fdb_en ? "is" : "isn't",
1878 : : priv->jump_fdb_rx_en ? "is" : "isn't");
1879 : : /* Without vport metadata, PMD must rely on source_vport match. */
1880 [ # # # # ]: 0 : if (priv->sh->config.dv_esw_en && priv->vport_meta_mask == 0)
1881 : 0 : priv->vport_match = 1;
1882 [ # # ]: 0 : if (priv->sh->config.dv_esw_en)
1883 : 0 : mlx5_flow_hw_set_port_info(eth_dev);
1884 [ # # ]: 0 : if (priv->sh->config.dv_esw_en &&
1885 [ # # # # ]: 0 : priv->sh->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
1886 : : priv->sh->config.dv_xmeta_en != MLX5_XMETA_MODE_META32_HWS) {
1887 : 0 : DRV_LOG(ERR,
1888 : : "metadata mode %u is not supported in HWS eswitch mode",
1889 : : priv->sh->config.dv_xmeta_en);
1890 : : err = ENOTSUP;
1891 : 0 : goto error;
1892 : : }
1893 [ # # # # ]: 0 : if (priv->sh->config.dv_esw_en &&
1894 : 0 : mlx5_flow_hw_create_vport_action(eth_dev)) {
1895 : 0 : DRV_LOG(ERR, "port %u failed to create vport action",
1896 : : eth_dev->data->port_id);
1897 : : err = EINVAL;
1898 : 0 : goto error;
1899 : : }
1900 : 0 : eth_dev->data->dev_flags |= RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE;
1901 : 0 : return eth_dev;
1902 : : #else
1903 : : DRV_LOG(ERR, "DV support is missing for HWS.");
1904 : : goto error;
1905 : : #endif
1906 : : }
1907 [ # # ]: 0 : if (!priv->sh->flow_priority_check_flag) {
1908 : : /* Supported Verbs flow priority number detection. */
1909 : 0 : err = mlx5_flow_discover_priorities(eth_dev);
1910 : 0 : priv->sh->flow_max_priority = err;
1911 : 0 : priv->sh->flow_priority_check_flag = 1;
1912 : : } else {
1913 : 0 : err = priv->sh->flow_max_priority;
1914 : : }
1915 [ # # ]: 0 : if (err < 0) {
1916 : 0 : err = -err;
1917 : 0 : goto error;
1918 : : }
1919 : : rte_spinlock_init(&priv->shared_act_sl);
1920 : 0 : mlx5_flow_counter_mode_config(eth_dev);
1921 : 0 : mlx5_flow_drop_action_config(eth_dev);
1922 [ # # ]: 0 : if (sh->config.dv_flow_en)
1923 : 0 : eth_dev->data->dev_flags |= RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE;
1924 : : return eth_dev;
1925 : 0 : error:
1926 [ # # ]: 0 : if (priv) {
1927 : 0 : priv->sh->port[priv->dev_port - 1].nl_ih_port_id =
1928 : : RTE_MAX_ETHPORTS;
1929 : 0 : rte_io_wmb();
1930 : : #ifdef HAVE_MLX5_HWS_SUPPORT
1931 [ # # ]: 0 : if (eth_dev &&
1932 [ # # ]: 0 : priv->sh &&
1933 [ # # ]: 0 : priv->sh->config.dv_flow_en == 2 &&
1934 : : priv->sh->config.dv_esw_en)
1935 : 0 : mlx5_flow_hw_destroy_vport_action(eth_dev);
1936 : : #endif
1937 [ # # ]: 0 : if (priv->sh)
1938 : 0 : mlx5_os_free_shared_dr(priv);
1939 [ # # ]: 0 : if (priv->nl_socket_route >= 0)
1940 : 0 : close(priv->nl_socket_route);
1941 [ # # ]: 0 : if (priv->vmwa_context)
1942 : 0 : mlx5_vlan_vmwa_exit(priv->vmwa_context);
1943 [ # # ]: 0 : for (i = 0; i < MLX5_FLOW_TYPE_MAXI; i++) {
1944 [ # # ]: 0 : if (!priv->flows[i])
1945 : 0 : continue;
1946 : 0 : mlx5_ipool_destroy(priv->flows[i]);
1947 : : }
1948 [ # # # # ]: 0 : if (eth_dev && priv->drop_queue.hrxq)
1949 : 0 : mlx5_drop_action_destroy(eth_dev);
1950 [ # # ]: 0 : if (priv->mtr_profile_tbl)
1951 : 0 : mlx5_l3t_destroy(priv->mtr_profile_tbl);
1952 [ # # ]: 0 : if (own_domain_id)
1953 : 0 : claim_zero(rte_eth_switch_domain_free(priv->domain_id));
1954 [ # # ]: 0 : if (priv->hrxqs)
1955 : 0 : mlx5_list_destroy(priv->hrxqs);
1956 [ # # # # ]: 0 : if (eth_dev && priv->flex_item_map)
1957 : 0 : mlx5_flex_item_port_cleanup(eth_dev);
1958 : 0 : mlx5_free(priv->ext_rxqs);
1959 : 0 : mlx5_free(priv->ext_txqs);
1960 : 0 : mlx5_free(priv);
1961 [ # # ]: 0 : if (eth_dev != NULL)
1962 : 0 : eth_dev->data->dev_private = NULL;
1963 : : }
1964 [ # # ]: 0 : if (eth_dev != NULL) {
1965 : : /* mac_addrs must not be freed alone because part of
1966 : : * dev_private
1967 : : **/
1968 : 0 : eth_dev->data->mac_addrs = NULL;
1969 : 0 : rte_eth_dev_release_port(eth_dev);
1970 : : }
1971 : : if (sh)
1972 : 0 : mlx5_free_shared_dev_ctx(sh);
1973 [ # # ]: 0 : if (nl_rdma >= 0)
1974 : 0 : close(nl_rdma);
1975 : : MLX5_ASSERT(err > 0);
1976 : 0 : rte_errno = err;
1977 : 0 : return NULL;
1978 : : }
1979 : :
1980 : : /**
1981 : : * Comparison callback to sort device data.
1982 : : *
1983 : : * This is meant to be used with qsort().
1984 : : *
1985 : : * @param a[in]
1986 : : * Pointer to pointer to first data object.
1987 : : * @param b[in]
1988 : : * Pointer to pointer to second data object.
1989 : : *
1990 : : * @return
1991 : : * 0 if both objects are equal, less than 0 if the first argument is less
1992 : : * than the second, greater than 0 otherwise.
1993 : : */
1994 : : static int
1995 : 0 : mlx5_dev_spawn_data_cmp(const void *a, const void *b)
1996 : : {
1997 : : const struct mlx5_switch_info *si_a =
1998 : : &((const struct mlx5_dev_spawn_data *)a)->info;
1999 : : const struct mlx5_switch_info *si_b =
2000 : : &((const struct mlx5_dev_spawn_data *)b)->info;
2001 : 0 : int uplink_a = si_a->name_type == MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
2002 : 0 : int uplink_b = si_b->name_type == MLX5_PHYS_PORT_NAME_TYPE_UPLINK;
2003 : : int ret;
2004 : :
2005 : : /* Uplink ports first. */
2006 : 0 : ret = uplink_b - uplink_a;
2007 [ # # ]: 0 : if (ret)
2008 : : return ret;
2009 : : /* Then master devices. */
2010 : 0 : ret = si_b->master - si_a->master;
2011 [ # # ]: 0 : if (ret)
2012 : : return ret;
2013 : : /* Then representor devices. */
2014 : 0 : ret = si_b->representor - si_a->representor;
2015 [ # # ]: 0 : if (ret)
2016 : : return ret;
2017 : : /* Unidentified devices come last in no specific order. */
2018 [ # # ]: 0 : if (!si_a->representor)
2019 : : return 0;
2020 : : /* Order representors by name. */
2021 : 0 : return si_a->port_name - si_b->port_name;
2022 : : }
2023 : :
2024 : : /**
2025 : : * Match PCI information for possible slaves of bonding device.
2026 : : *
2027 : : * @param[in] ibdev
2028 : : * Pointer to IB device.
2029 : : * @param[in] pci_dev
2030 : : * Pointer to primary PCI address structure to match.
2031 : : * @param[in] nl_rdma
2032 : : * Netlink RDMA group socket handle.
2033 : : * @param[in] owner
2034 : : * Representor owner PF index.
2035 : : * @param[in] dev_info
2036 : : * Cached mlx5 device information.
2037 : : * @param[out] bond_info
2038 : : * Pointer to bonding information.
2039 : : *
2040 : : * @return
2041 : : * negative value if no bonding device found, otherwise
2042 : : * positive index of slave PF in bonding.
2043 : : */
2044 : : static int
2045 : 0 : mlx5_device_bond_pci_match(const struct ibv_device *ibdev,
2046 : : const struct rte_pci_addr *pci_dev,
2047 : : int nl_rdma, uint16_t owner,
2048 : : struct mlx5_dev_info *dev_info,
2049 : : struct mlx5_bond_info *bond_info)
2050 : : {
2051 : : char ifname[IF_NAMESIZE + 1];
2052 : : unsigned int ifindex;
2053 : : unsigned int np, i;
2054 : : FILE *bond_file = NULL, *file;
2055 : : int pf = -1;
2056 : : int ret;
2057 : 0 : uint8_t cur_guid[32] = {0};
2058 [ # # ]: 0 : uint8_t guid[32] = {0};
2059 : :
2060 : : /*
2061 : : * Try to get master device name. If something goes wrong suppose
2062 : : * the lack of kernel support and no bonding devices.
2063 : : */
2064 : : memset(bond_info, 0, sizeof(*bond_info));
2065 [ # # ]: 0 : if (nl_rdma < 0)
2066 : : return -1;
2067 [ # # ]: 0 : if (!mlx5_os_is_device_bond(ibdev))
2068 : : return -1;
2069 : 0 : np = mlx5_nl_portnum(nl_rdma, ibdev->name, dev_info);
2070 [ # # ]: 0 : if (!np)
2071 : : return -1;
2072 [ # # ]: 0 : if (mlx5_get_device_guid(pci_dev, cur_guid, sizeof(cur_guid)) < 0)
2073 : : return -1;
2074 : : /*
2075 : : * The master device might not be on the predefined port(not on port
2076 : : * index 1, it is not guaranteed), we have to scan all Infiniband
2077 : : * device ports and find master.
2078 : : */
2079 [ # # ]: 0 : for (i = 1; i <= np; ++i) {
2080 : : /* Check whether Infiniband port is populated. */
2081 : 0 : ifindex = mlx5_nl_ifindex(nl_rdma, ibdev->name, i, dev_info);
2082 [ # # ]: 0 : if (!ifindex)
2083 : 0 : continue;
2084 [ # # ]: 0 : if (!if_indextoname(ifindex, ifname))
2085 : 0 : continue;
2086 : : /* Try to read bonding slave names from sysfs. */
2087 : 0 : MKSTR(slaves,
2088 : : "/sys/class/net/%s/master/bonding/slaves", ifname);
2089 : 0 : bond_file = fopen(slaves, "r");
2090 [ # # ]: 0 : if (bond_file)
2091 : : break;
2092 : : }
2093 [ # # ]: 0 : if (!bond_file)
2094 : : return -1;
2095 : : /* Use safe format to check maximal buffer length. */
2096 : : MLX5_ASSERT(atol(RTE_STR(IF_NAMESIZE)) == IF_NAMESIZE);
2097 [ # # ]: 0 : while (fscanf(bond_file, "%" RTE_STR(IF_NAMESIZE) "s", ifname) == 1) {
2098 : : char tmp_str[IF_NAMESIZE + 32];
2099 : : struct rte_pci_addr pci_addr;
2100 : : struct mlx5_switch_info info;
2101 : : int ret;
2102 : :
2103 : : /* Process slave interface names in the loop. */
2104 : : snprintf(tmp_str, sizeof(tmp_str),
2105 : : "/sys/class/net/%s", ifname);
2106 [ # # ]: 0 : if (mlx5_get_pci_addr(tmp_str, &pci_addr)) {
2107 : 0 : DRV_LOG(WARNING,
2108 : : "Cannot get PCI address for netdev \"%s\".",
2109 : : ifname);
2110 : 0 : continue;
2111 : : }
2112 : : /* Slave interface PCI address match found. */
2113 : : snprintf(tmp_str, sizeof(tmp_str),
2114 : : "/sys/class/net/%s/phys_port_name", ifname);
2115 : 0 : file = fopen(tmp_str, "rb");
2116 [ # # ]: 0 : if (!file)
2117 : : break;
2118 : 0 : info.name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET;
2119 [ # # ]: 0 : if (fscanf(file, "%32s", tmp_str) == 1) {
2120 : 0 : mlx5_translate_port_name(tmp_str, &info);
2121 : 0 : fclose(file);
2122 : : } else {
2123 : 0 : fclose(file);
2124 : 0 : break;
2125 : : }
2126 : : /* Only process PF ports. */
2127 [ # # ]: 0 : if (info.name_type != MLX5_PHYS_PORT_NAME_TYPE_LEGACY &&
2128 : : info.name_type != MLX5_PHYS_PORT_NAME_TYPE_UPLINK)
2129 : 0 : continue;
2130 : : /* Check max bonding member. */
2131 [ # # ]: 0 : if (info.port_name >= MLX5_BOND_MAX_PORTS) {
2132 : 0 : DRV_LOG(WARNING, "bonding index out of range, "
2133 : : "please increase MLX5_BOND_MAX_PORTS: %s",
2134 : : tmp_str);
2135 : 0 : break;
2136 : : }
2137 : : /* Get ifindex. */
2138 : : snprintf(tmp_str, sizeof(tmp_str),
2139 : : "/sys/class/net/%s/ifindex", ifname);
2140 : 0 : file = fopen(tmp_str, "rb");
2141 [ # # ]: 0 : if (!file)
2142 : : break;
2143 : 0 : ret = fscanf(file, "%u", &ifindex);
2144 : 0 : fclose(file);
2145 [ # # ]: 0 : if (ret != 1)
2146 : : break;
2147 : : /* Save bonding info. */
2148 : 0 : snprintf(bond_info->ports[info.port_name].ifname,
2149 : : sizeof(bond_info->ports[0].ifname), "%s", ifname);
2150 : 0 : bond_info->ports[info.port_name].pci_addr = pci_addr;
2151 : 0 : bond_info->ports[info.port_name].ifindex = ifindex;
2152 : 0 : bond_info->n_port++;
2153 : : /*
2154 : : * Under socket direct mode, bonding will use
2155 : : * system_image_guid as identification.
2156 : : * After OFED 5.4, guid is readable (ret >= 0) under sysfs.
2157 : : * All bonding members should have the same guid even if driver
2158 : : * is using PCIe BDF.
2159 : : */
2160 : 0 : ret = mlx5_get_device_guid(&pci_addr, guid, sizeof(guid));
2161 [ # # ]: 0 : if (ret < 0)
2162 : : break;
2163 [ # # ]: 0 : else if (ret > 0) {
2164 [ # # ]: 0 : if (!memcmp(guid, cur_guid, sizeof(guid)) &&
2165 [ # # # # ]: 0 : owner == info.port_name &&
2166 [ # # ]: 0 : (owner != 0 || (owner == 0 &&
2167 : 0 : !rte_pci_addr_cmp(pci_dev, &pci_addr))))
2168 : 0 : pf = info.port_name;
2169 [ # # ]: 0 : } else if (pci_dev->domain == pci_addr.domain &&
2170 [ # # ]: 0 : pci_dev->bus == pci_addr.bus &&
2171 : 0 : pci_dev->devid == pci_addr.devid &&
2172 [ # # ]: 0 : ((pci_dev->function == 0 &&
2173 [ # # ]: 0 : pci_dev->function + owner == pci_addr.function) ||
2174 [ # # ]: 0 : (pci_dev->function == owner &&
2175 [ # # ]: 0 : pci_addr.function == owner)))
2176 : 0 : pf = info.port_name;
2177 : : }
2178 : 0 : fclose(bond_file);
2179 [ # # ]: 0 : if (pf >= 0) {
2180 : : /* Get bond interface info */
2181 : 0 : ret = mlx5_sysfs_bond_info(ifindex, &bond_info->ifindex,
2182 : 0 : bond_info->ifname);
2183 [ # # ]: 0 : if (ret)
2184 : 0 : DRV_LOG(ERR, "unable to get bond info: %s",
2185 : : strerror(rte_errno));
2186 : : else
2187 : 0 : DRV_LOG(INFO, "PF device %u, bond device %u(%s)",
2188 : : ifindex, bond_info->ifindex, bond_info->ifname);
2189 : : }
2190 [ # # ]: 0 : if (owner == 0 && pf != 0) {
2191 : 0 : DRV_LOG(INFO, "PCIe instance " PCI_PRI_FMT " isn't bonding owner",
2192 : : pci_dev->domain, pci_dev->bus, pci_dev->devid,
2193 : : pci_dev->function);
2194 : : }
2195 : : return pf;
2196 : : }
2197 : :
2198 : : static int
2199 : 0 : mlx5_nl_esw_multiport_get(struct rte_pci_addr *pci_addr, int *enabled)
2200 : : {
2201 : 0 : char pci_addr_str[PCI_PRI_STR_SIZE] = { 0 };
2202 : : int nlsk_fd;
2203 : : int devlink_id;
2204 : : int ret;
2205 : :
2206 : : /* Provide correct value to have defined enabled state in case of an error. */
2207 : 0 : *enabled = 0;
2208 : 0 : rte_pci_device_name(pci_addr, pci_addr_str, sizeof(pci_addr_str));
2209 : 0 : nlsk_fd = mlx5_nl_init(NETLINK_GENERIC, 0);
2210 [ # # ]: 0 : if (nlsk_fd < 0)
2211 : : return nlsk_fd;
2212 : 0 : devlink_id = mlx5_nl_devlink_family_id_get(nlsk_fd);
2213 [ # # ]: 0 : if (devlink_id < 0) {
2214 : : ret = devlink_id;
2215 : 0 : DRV_LOG(DEBUG, "Unable to get devlink family id for Multiport E-Switch checks "
2216 : : "by netlink, for PCI device %s", pci_addr_str);
2217 : 0 : goto close_nlsk_fd;
2218 : : }
2219 : 0 : ret = mlx5_nl_devlink_esw_multiport_get(nlsk_fd, devlink_id, pci_addr_str, enabled);
2220 [ # # ]: 0 : if (ret < 0)
2221 : 0 : DRV_LOG(DEBUG, "Unable to get Multiport E-Switch state by Netlink.");
2222 : 0 : close_nlsk_fd:
2223 : 0 : close(nlsk_fd);
2224 : 0 : return ret;
2225 : : }
2226 : :
2227 : : #define SYSFS_MPESW_PARAM_MAX_LEN 16
2228 : :
2229 : : static int
2230 : 0 : mlx5_sysfs_esw_multiport_get(struct ibv_device *ibv, struct rte_pci_addr *pci_addr, int *enabled,
2231 : : struct mlx5_dev_info *dev_info)
2232 : : {
2233 : : int nl_rdma;
2234 : : unsigned int n_ports;
2235 : : unsigned int i;
2236 : : int ret;
2237 : :
2238 : : /* Provide correct value to have defined enabled state in case of an error. */
2239 : 0 : *enabled = 0;
2240 : 0 : nl_rdma = mlx5_nl_init(NETLINK_RDMA, 0);
2241 [ # # ]: 0 : if (nl_rdma < 0)
2242 : : return nl_rdma;
2243 : 0 : n_ports = mlx5_nl_portnum(nl_rdma, ibv->name, dev_info);
2244 [ # # ]: 0 : if (!n_ports) {
2245 : 0 : ret = -rte_errno;
2246 : 0 : goto close_nl_rdma;
2247 : : }
2248 [ # # ]: 0 : for (i = 1; i <= n_ports; ++i) {
2249 : : unsigned int ifindex;
2250 : : char ifname[IF_NAMESIZE + 1];
2251 : 0 : struct rte_pci_addr if_pci_addr = { 0 };
2252 : : char mpesw[SYSFS_MPESW_PARAM_MAX_LEN + 1];
2253 : : FILE *sysfs;
2254 : : int n;
2255 : :
2256 : 0 : ifindex = mlx5_nl_ifindex(nl_rdma, ibv->name, i, dev_info);
2257 [ # # ]: 0 : if (!ifindex)
2258 : 0 : continue;
2259 [ # # ]: 0 : if (!if_indextoname(ifindex, ifname))
2260 : 0 : continue;
2261 : 0 : MKSTR(sysfs_if_path, "/sys/class/net/%s", ifname);
2262 [ # # ]: 0 : if (mlx5_get_pci_addr(sysfs_if_path, &if_pci_addr))
2263 : 0 : continue;
2264 [ # # ]: 0 : if (pci_addr->domain != if_pci_addr.domain ||
2265 [ # # ]: 0 : pci_addr->bus != if_pci_addr.bus ||
2266 : 0 : pci_addr->devid != if_pci_addr.devid ||
2267 [ # # ]: 0 : pci_addr->function != if_pci_addr.function)
2268 : 0 : continue;
2269 : 0 : MKSTR(sysfs_mpesw_path,
2270 : : "/sys/class/net/%s/compat/devlink/lag_port_select_mode", ifname);
2271 : 0 : sysfs = fopen(sysfs_mpesw_path, "r");
2272 [ # # ]: 0 : if (!sysfs)
2273 : 0 : continue;
2274 : 0 : n = fscanf(sysfs, "%" RTE_STR(SYSFS_MPESW_PARAM_MAX_LEN) "s", mpesw);
2275 : 0 : fclose(sysfs);
2276 [ # # ]: 0 : if (n != 1)
2277 : 0 : continue;
2278 : : ret = 0;
2279 [ # # ]: 0 : if (strcmp(mpesw, "multiport_esw") == 0) {
2280 : 0 : *enabled = 1;
2281 : 0 : break;
2282 : : }
2283 : 0 : *enabled = 0;
2284 : 0 : break;
2285 : : }
2286 [ # # ]: 0 : if (i > n_ports) {
2287 : 0 : DRV_LOG(DEBUG, "Unable to get Multiport E-Switch state by sysfs.");
2288 : 0 : rte_errno = ENOENT;
2289 : : ret = -rte_errno;
2290 : : }
2291 : :
2292 : 0 : close_nl_rdma:
2293 : 0 : close(nl_rdma);
2294 : 0 : return ret;
2295 : : }
2296 : :
2297 : : static int
2298 : 0 : mlx5_is_mpesw_enabled(struct ibv_device *ibv, struct rte_pci_addr *ibv_pci_addr, int *enabled,
2299 : : struct mlx5_dev_info *dev_info)
2300 : : {
2301 : : /*
2302 : : * Try getting Multiport E-Switch state through netlink interface
2303 : : * If unable, try sysfs interface. If that is unable as well,
2304 : : * assume that Multiport E-Switch is disabled and return an error.
2305 : : */
2306 [ # # # # ]: 0 : if (mlx5_nl_esw_multiport_get(ibv_pci_addr, enabled) >= 0 ||
2307 : 0 : mlx5_sysfs_esw_multiport_get(ibv, ibv_pci_addr, enabled, dev_info) >= 0)
2308 : 0 : return 0;
2309 : 0 : DRV_LOG(DEBUG, "Unable to check MPESW state for IB device %s "
2310 : : "(PCI: " PCI_PRI_FMT ")",
2311 : : ibv->name,
2312 : : ibv_pci_addr->domain, ibv_pci_addr->bus,
2313 : : ibv_pci_addr->devid, ibv_pci_addr->function);
2314 : 0 : *enabled = 0;
2315 : 0 : return -rte_errno;
2316 : : }
2317 : :
2318 : : static int
2319 : 0 : mlx5_device_mpesw_pci_match(struct ibv_device *ibv,
2320 : : const struct rte_pci_addr *owner_pci,
2321 : : int nl_rdma, struct mlx5_dev_info *dev_info)
2322 : : {
2323 : 0 : struct rte_pci_addr ibdev_pci_addr = { 0 };
2324 : 0 : char ifname[IF_NAMESIZE + 1] = { 0 };
2325 : : unsigned int ifindex;
2326 : : unsigned int np;
2327 : : unsigned int i;
2328 : 0 : int enabled = 0;
2329 : : int ret;
2330 : :
2331 : : /* Check if IB device's PCI address matches the probed PCI address. */
2332 [ # # ]: 0 : if (mlx5_get_pci_addr(ibv->ibdev_path, &ibdev_pci_addr)) {
2333 : 0 : DRV_LOG(DEBUG, "Skipping MPESW check for IB device %s since "
2334 : : "there is no underlying PCI device", ibv->name);
2335 : 0 : rte_errno = ENOENT;
2336 : 0 : return -rte_errno;
2337 : : }
2338 [ # # ]: 0 : if (ibdev_pci_addr.domain != owner_pci->domain ||
2339 [ # # ]: 0 : ibdev_pci_addr.bus != owner_pci->bus ||
2340 : 0 : ibdev_pci_addr.devid != owner_pci->devid ||
2341 [ # # ]: 0 : ibdev_pci_addr.function != owner_pci->function) {
2342 : : return -1;
2343 : : }
2344 : : /* Check if IB device has MPESW enabled. */
2345 [ # # ]: 0 : if (mlx5_is_mpesw_enabled(ibv, &ibdev_pci_addr, &enabled, dev_info))
2346 : : return -1;
2347 [ # # ]: 0 : if (!enabled)
2348 : : return -1;
2349 : : /* Iterate through IB ports to find MPESW master uplink port. */
2350 [ # # ]: 0 : if (nl_rdma < 0)
2351 : : return -1;
2352 : 0 : np = mlx5_nl_portnum(nl_rdma, ibv->name, dev_info);
2353 [ # # ]: 0 : if (!np)
2354 : : return -1;
2355 [ # # ]: 0 : for (i = 1; i <= np; ++i) {
2356 : 0 : struct rte_pci_addr pci_addr = { 0 };
2357 : : FILE *file;
2358 : : char port_name[IF_NAMESIZE + 1];
2359 : : struct mlx5_switch_info info;
2360 : :
2361 : : /* Check whether IB port has a corresponding netdev. */
2362 : 0 : ifindex = mlx5_nl_ifindex(nl_rdma, ibv->name, i, dev_info);
2363 [ # # ]: 0 : if (!ifindex)
2364 : 0 : continue;
2365 [ # # ]: 0 : if (!if_indextoname(ifindex, ifname))
2366 : 0 : continue;
2367 : : /* Read port name and determine its type. */
2368 : 0 : MKSTR(ifphysportname, "/sys/class/net/%s/phys_port_name", ifname);
2369 : 0 : file = fopen(ifphysportname, "rb");
2370 [ # # ]: 0 : if (!file)
2371 : 0 : continue;
2372 : 0 : ret = fscanf(file, "%16s", port_name);
2373 : 0 : fclose(file);
2374 [ # # ]: 0 : if (ret != 1)
2375 : 0 : continue;
2376 : : memset(&info, 0, sizeof(info));
2377 : 0 : mlx5_translate_port_name(port_name, &info);
2378 [ # # ]: 0 : if (info.name_type != MLX5_PHYS_PORT_NAME_TYPE_UPLINK)
2379 : 0 : continue;
2380 : : /* Fetch PCI address of the device to which the netdev is bound. */
2381 : 0 : MKSTR(ifpath, "/sys/class/net/%s", ifname);
2382 [ # # ]: 0 : if (mlx5_get_pci_addr(ifpath, &pci_addr))
2383 : 0 : continue;
2384 [ # # ]: 0 : if (pci_addr.domain == ibdev_pci_addr.domain &&
2385 : : pci_addr.bus == ibdev_pci_addr.bus &&
2386 [ # # ]: 0 : pci_addr.devid == ibdev_pci_addr.devid &&
2387 : : pci_addr.function == ibdev_pci_addr.function) {
2388 : : MLX5_ASSERT(info.port_name >= 0);
2389 : 0 : return info.port_name;
2390 : : }
2391 : : }
2392 : : /* No matching MPESW uplink port was found. */
2393 : : return -1;
2394 : : }
2395 : :
2396 : : static void
2397 : 0 : calc_nb_uplinks_hpfs(struct ibv_device **ibv_match,
2398 : : unsigned int nd,
2399 : : struct mlx5_dev_spawn_data *list,
2400 : : unsigned int ns)
2401 : : {
2402 [ # # ]: 0 : for (unsigned int i = 0; i != nd; i++) {
2403 : : uint32_t nb_uplinks = 0;
2404 : : uint32_t nb_hpfs = 0;
2405 : : uint32_t j;
2406 : :
2407 [ # # ]: 0 : for (unsigned int j = 0; j != ns; j++) {
2408 [ # # ]: 0 : if (strcmp(ibv_match[i]->name, list[j].phys_dev_name) != 0)
2409 : 0 : continue;
2410 : :
2411 [ # # ]: 0 : if (list[j].info.name_type == MLX5_PHYS_PORT_NAME_TYPE_UPLINK)
2412 : 0 : nb_uplinks++;
2413 [ # # ]: 0 : else if (list[j].info.name_type == MLX5_PHYS_PORT_NAME_TYPE_PFHPF)
2414 : 0 : nb_hpfs++;
2415 : : }
2416 : :
2417 [ # # ]: 0 : if (nb_uplinks > 0 || nb_hpfs > 0) {
2418 [ # # ]: 0 : for (j = 0; j != ns; j++) {
2419 [ # # ]: 0 : if (strcmp(ibv_match[i]->name, list[j].phys_dev_name) != 0)
2420 : 0 : continue;
2421 : :
2422 : 0 : list[j].nb_uplinks = nb_uplinks;
2423 : 0 : list[j].nb_hpfs = nb_hpfs;
2424 : : }
2425 : :
2426 : 0 : DRV_LOG(DEBUG, "IB device %s has %u uplinks, %u host PFs",
2427 : : ibv_match[i]->name,
2428 : : nb_uplinks,
2429 : : nb_hpfs);
2430 : : } else {
2431 : 0 : DRV_LOG(DEBUG, "IB device %s unable to recognize uplinks/host PFs",
2432 : : ibv_match[i]->name);
2433 : : }
2434 : : }
2435 : 0 : }
2436 : :
2437 : : /**
2438 : : * Register a PCI device within bonding.
2439 : : *
2440 : : * This function spawns Ethernet devices out of a given PCI device and
2441 : : * bonding owner PF index.
2442 : : *
2443 : : * @param[in] cdev
2444 : : * Pointer to common mlx5 device structure.
2445 : : * @param[in] req_eth_da
2446 : : * Requested ethdev device argument.
2447 : : * @param[in] owner_id
2448 : : * Requested owner PF port ID within bonding device, default to 0.
2449 : : * @param[in, out] mkvlist
2450 : : * Pointer to mlx5 kvargs control, can be NULL if there is no devargs.
2451 : : *
2452 : : * @return
2453 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
2454 : : */
2455 : : static int
2456 : 0 : mlx5_os_pci_probe_pf(struct mlx5_common_device *cdev,
2457 : : struct rte_eth_devargs *req_eth_da,
2458 : : uint16_t owner_id, struct mlx5_kvargs_ctrl *mkvlist)
2459 : 0 : {
2460 : : struct ibv_device **ibv_list;
2461 : : /*
2462 : : * Number of found IB Devices matching with requested PCI BDF.
2463 : : * nd != 1 means there are multiple IB devices over the same
2464 : : * PCI device and we have representors and master.
2465 : : */
2466 : : unsigned int nd = 0;
2467 : : /*
2468 : : * Number of found IB device Ports. nd = 1 and np = 1..n means
2469 : : * we have the single multiport IB device, and there may be
2470 : : * representors attached to some of found ports.
2471 : : */
2472 : : unsigned int np = 0;
2473 : : /*
2474 : : * Number of DPDK ethernet devices to Spawn - either over
2475 : : * multiple IB devices or multiple ports of single IB device.
2476 : : * Actually this is the number of iterations to spawn.
2477 : : */
2478 : : unsigned int ns = 0;
2479 : : /*
2480 : : * Bonding device
2481 : : * < 0 - no bonding device (single one)
2482 : : * >= 0 - bonding device (value is slave PF index)
2483 : : */
2484 : : int bd = -1;
2485 : : /*
2486 : : * Multiport E-Switch (MPESW) device:
2487 : : * < 0 - no MPESW device or could not determine if it is MPESW device,
2488 : : * >= 0 - MPESW device. Value is the port index of the MPESW owner.
2489 : : */
2490 : : int mpesw = MLX5_MPESW_PORT_INVALID;
2491 : 0 : struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(cdev->dev);
2492 : : struct mlx5_dev_spawn_data *list = NULL;
2493 : 0 : struct rte_eth_devargs eth_da = *req_eth_da;
2494 : 0 : struct rte_pci_addr owner_pci = pci_dev->addr; /* Owner PF. */
2495 : : struct mlx5_bond_info bond_info;
2496 : 0 : int ret = -1;
2497 : :
2498 : 0 : errno = 0;
2499 : 0 : ibv_list = mlx5_glue->get_device_list(&ret);
2500 [ # # ]: 0 : if (!ibv_list) {
2501 [ # # ]: 0 : rte_errno = errno ? errno : ENOSYS;
2502 : 0 : DRV_LOG(ERR, "Cannot list devices, is ib_uverbs loaded?");
2503 : 0 : return -rte_errno;
2504 : : }
2505 : : /*
2506 : : * First scan the list of all Infiniband devices to find
2507 : : * matching ones, gathering into the list.
2508 : : */
2509 : 0 : struct ibv_device *ibv_match[ret + 1];
2510 : 0 : struct mlx5_dev_info *info, tmp_info[ret];
2511 : 0 : int nl_route = mlx5_nl_init(NETLINK_ROUTE, 0);
2512 : 0 : int nl_rdma = mlx5_nl_init(NETLINK_RDMA, 0);
2513 : : unsigned int i;
2514 : :
2515 : : memset(tmp_info, 0, sizeof(tmp_info));
2516 [ # # ]: 0 : while (ret-- > 0) {
2517 : : struct rte_pci_addr pci_addr;
2518 : :
2519 [ # # # # ]: 0 : if (cdev->config.probe_opt && cdev->dev_info.port_num) {
2520 [ # # ]: 0 : if (strcmp(ibv_list[ret]->name, cdev->dev_info.ibname)) {
2521 : 0 : DRV_LOG(INFO, "Unmatched caching device \"%s\" \"%s\"",
2522 : : cdev->dev_info.ibname, ibv_list[ret]->name);
2523 : 0 : continue;
2524 : : }
2525 : 0 : info = &cdev->dev_info;
2526 : : } else {
2527 : 0 : info = &tmp_info[ret];
2528 : : }
2529 : 0 : DRV_LOG(DEBUG, "Checking device \"%s\"", ibv_list[ret]->name);
2530 : 0 : bd = mlx5_device_bond_pci_match(ibv_list[ret], &owner_pci,
2531 : : nl_rdma, owner_id,
2532 : : info,
2533 : : &bond_info);
2534 [ # # ]: 0 : if (bd >= 0) {
2535 : : /*
2536 : : * Bonding device detected. Only one match is allowed,
2537 : : * the bonding is supported over multi-port IB device,
2538 : : * there should be no matches on representor PCI
2539 : : * functions or non VF LAG bonding devices with
2540 : : * specified address.
2541 : : */
2542 [ # # ]: 0 : if (nd) {
2543 : 0 : DRV_LOG(ERR,
2544 : : "multiple PCI match on bonding device"
2545 : : "\"%s\" found", ibv_list[ret]->name);
2546 : 0 : rte_errno = ENOENT;
2547 : 0 : ret = -rte_errno;
2548 : 0 : goto exit;
2549 : : }
2550 : : /* Amend owner pci address if owner PF ID specified. */
2551 [ # # ]: 0 : if (eth_da.nb_representor_ports)
2552 : 0 : owner_pci.function += owner_id;
2553 : 0 : DRV_LOG(INFO,
2554 : : "PCI information matches for slave %d bonding device \"%s\"",
2555 : : bd, ibv_list[ret]->name);
2556 : 0 : ibv_match[nd++] = ibv_list[ret];
2557 : 0 : break;
2558 : : }
2559 : 0 : mpesw = mlx5_device_mpesw_pci_match(ibv_list[ret], &owner_pci, nl_rdma,
2560 : : info);
2561 [ # # ]: 0 : if (mpesw >= 0) {
2562 : : /*
2563 : : * MPESW device detected. Only one matching IB device is allowed,
2564 : : * so if any matches were found previously, fail gracefully.
2565 : : */
2566 [ # # ]: 0 : if (nd) {
2567 : 0 : DRV_LOG(ERR,
2568 : : "PCI information matches MPESW device \"%s\", "
2569 : : "but multiple matching PCI devices were found. "
2570 : : "Probing failed.",
2571 : : ibv_list[ret]->name);
2572 : 0 : rte_errno = ENOENT;
2573 : 0 : ret = -rte_errno;
2574 : 0 : goto exit;
2575 : : }
2576 : 0 : DRV_LOG(INFO,
2577 : : "PCI information matches MPESW device \"%s\"",
2578 : : ibv_list[ret]->name);
2579 : 0 : ibv_match[nd++] = ibv_list[ret];
2580 : 0 : break;
2581 : : }
2582 : : /* Bonding or MPESW device was not found. */
2583 [ # # ]: 0 : if (mlx5_get_pci_addr(ibv_list[ret]->ibdev_path,
2584 : : &pci_addr)) {
2585 [ # # ]: 0 : if (tmp_info[ret].port_info != NULL)
2586 : 0 : mlx5_free(tmp_info[ret].port_info);
2587 : 0 : memset(&tmp_info[ret], 0, sizeof(tmp_info[0]));
2588 : 0 : continue;
2589 : : }
2590 [ # # ]: 0 : if (rte_pci_addr_cmp(&owner_pci, &pci_addr) != 0) {
2591 [ # # ]: 0 : if (tmp_info[ret].port_info != NULL)
2592 : 0 : mlx5_free(tmp_info[ret].port_info);
2593 : 0 : memset(&tmp_info[ret], 0, sizeof(tmp_info[0]));
2594 : 0 : continue;
2595 : : }
2596 : 0 : DRV_LOG(INFO, "PCI information matches for device \"%s\"",
2597 : : ibv_list[ret]->name);
2598 : 0 : ibv_match[nd++] = ibv_list[ret];
2599 : : }
2600 : 0 : ibv_match[nd] = NULL;
2601 [ # # ]: 0 : if (!nd) {
2602 : : /* No device matches, just complain and bail out. */
2603 : 0 : DRV_LOG(WARNING,
2604 : : "PF %u doesn't have Verbs device matches PCI device " PCI_PRI_FMT ","
2605 : : " are kernel drivers loaded?",
2606 : : owner_id, owner_pci.domain, owner_pci.bus,
2607 : : owner_pci.devid, owner_pci.function);
2608 : 0 : rte_errno = ENOENT;
2609 : 0 : ret = -rte_errno;
2610 : 0 : goto exit;
2611 : : }
2612 [ # # ]: 0 : if (nd == 1) {
2613 [ # # ]: 0 : if (!cdev->dev_info.port_num) {
2614 [ # # ]: 0 : for (i = 0; i < RTE_DIM(tmp_info); i++) {
2615 [ # # ]: 0 : if (tmp_info[i].port_num) {
2616 : 0 : cdev->dev_info = tmp_info[i];
2617 : 0 : break;
2618 : : }
2619 : : }
2620 : : }
2621 : : /*
2622 : : * Found single matching device may have multiple ports.
2623 : : * Each port may be representor, we have to check the port
2624 : : * number and check the representors existence.
2625 : : */
2626 [ # # ]: 0 : if (nl_rdma >= 0)
2627 : 0 : np = mlx5_nl_portnum(nl_rdma, ibv_match[0]->name, &cdev->dev_info);
2628 [ # # ]: 0 : if (!np)
2629 : 0 : DRV_LOG(WARNING,
2630 : : "Cannot get IB device \"%s\" ports number.",
2631 : : ibv_match[0]->name);
2632 [ # # ]: 0 : if (bd >= 0 && !np) {
2633 : 0 : DRV_LOG(ERR, "Cannot get ports for bonding device.");
2634 : 0 : rte_errno = ENOENT;
2635 : 0 : ret = -rte_errno;
2636 : 0 : goto exit;
2637 : : }
2638 [ # # ]: 0 : if (mpesw >= 0 && !np) {
2639 : 0 : DRV_LOG(ERR, "Cannot get ports for MPESW device.");
2640 : 0 : rte_errno = ENOENT;
2641 : 0 : ret = -rte_errno;
2642 : 0 : goto exit;
2643 : : }
2644 : : } else {
2645 : : /* Can't handle one common device with multiple IB devices caching */
2646 [ # # ]: 0 : for (i = 0; i < RTE_DIM(tmp_info); i++) {
2647 [ # # ]: 0 : if (tmp_info[i].port_info != NULL)
2648 : 0 : mlx5_free(tmp_info[i].port_info);
2649 : 0 : memset(&tmp_info[i], 0, sizeof(tmp_info[0]));
2650 : : }
2651 : 0 : DRV_LOG(INFO, "Cannot handle multiple IB devices info caching in single common device.");
2652 : : }
2653 : : /* Now we can determine the maximal amount of devices to be spawned. */
2654 [ # # ]: 0 : list = mlx5_malloc(MLX5_MEM_ZERO,
2655 : 0 : sizeof(struct mlx5_dev_spawn_data) * (np ? np : nd),
2656 : : RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
2657 [ # # ]: 0 : if (!list) {
2658 : 0 : DRV_LOG(ERR, "Spawn data array allocation failure.");
2659 : 0 : rte_errno = ENOMEM;
2660 : 0 : ret = -rte_errno;
2661 : 0 : goto exit;
2662 : : }
2663 [ # # # # ]: 0 : if (bd >= 0 || mpesw >= 0 || np > 1) {
2664 : : /*
2665 : : * Single IB device with multiple ports found,
2666 : : * it may be E-Switch master device and representors.
2667 : : * We have to perform identification through the ports.
2668 : : */
2669 : : MLX5_ASSERT(nl_rdma >= 0);
2670 : : MLX5_ASSERT(ns == 0);
2671 : : MLX5_ASSERT(nd == 1);
2672 : : MLX5_ASSERT(np);
2673 [ # # ]: 0 : for (i = 1; i <= np; ++i) {
2674 : 0 : list[ns].bond_info = &bond_info;
2675 : 0 : list[ns].max_port = np;
2676 : 0 : list[ns].phys_port = i;
2677 : 0 : list[ns].phys_dev_name = ibv_match[0]->name;
2678 : 0 : list[ns].eth_dev = NULL;
2679 : 0 : list[ns].pci_dev = pci_dev;
2680 : 0 : list[ns].cdev = cdev;
2681 : 0 : list[ns].pf_bond = bd;
2682 : 0 : list[ns].mpesw_port = MLX5_MPESW_PORT_INVALID;
2683 : 0 : list[ns].ifindex = mlx5_nl_ifindex(nl_rdma,
2684 : : ibv_match[0]->name,
2685 : : i, &cdev->dev_info);
2686 [ # # ]: 0 : if (!list[ns].ifindex) {
2687 : : /*
2688 : : * No network interface index found for the
2689 : : * specified port, it means there is no
2690 : : * representor on this port. It's OK,
2691 : : * there can be disabled ports, for example
2692 : : * if sriov_numvfs < sriov_totalvfs.
2693 : : */
2694 : 0 : continue;
2695 : : }
2696 : 0 : ret = -1;
2697 [ # # ]: 0 : if (nl_route >= 0)
2698 : 0 : ret = mlx5_nl_switch_info(nl_route,
2699 : : list[ns].ifindex,
2700 : : &list[ns].info);
2701 [ # # # # ]: 0 : if (ret || (!list[ns].info.representor &&
2702 : : !list[ns].info.master)) {
2703 : : /*
2704 : : * We failed to recognize representors with
2705 : : * Netlink, let's try to perform the task
2706 : : * with sysfs.
2707 : : */
2708 : 0 : ret = mlx5_sysfs_switch_info(list[ns].ifindex,
2709 : : &list[ns].info);
2710 : : }
2711 [ # # # # ]: 0 : if (!ret && bd >= 0) {
2712 [ # # # ]: 0 : switch (list[ns].info.name_type) {
2713 : 0 : case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
2714 [ # # ]: 0 : if (np == 1) {
2715 : : /*
2716 : : * Force standalone bonding
2717 : : * device for ROCE LAG
2718 : : * configurations.
2719 : : */
2720 : 0 : list[ns].info.master = 0;
2721 : 0 : list[ns].info.representor = 0;
2722 : : }
2723 : 0 : ns++;
2724 : 0 : break;
2725 : 0 : case MLX5_PHYS_PORT_NAME_TYPE_PFHPF:
2726 : : /* Fallthrough */
2727 : : case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
2728 : : /* Fallthrough */
2729 : : case MLX5_PHYS_PORT_NAME_TYPE_PFSF:
2730 [ # # ]: 0 : if (list[ns].info.pf_num == bd)
2731 : 0 : ns++;
2732 : : break;
2733 : : default:
2734 : : break;
2735 : : }
2736 : 0 : continue;
2737 : : }
2738 [ # # # # ]: 0 : if (!ret && mpesw >= 0) {
2739 [ # # # ]: 0 : switch (list[ns].info.name_type) {
2740 : 0 : case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
2741 : : /* Owner port is treated as master port. */
2742 [ # # ]: 0 : if (list[ns].info.port_name == mpesw) {
2743 : 0 : list[ns].info.master = 1;
2744 : 0 : list[ns].info.representor = 0;
2745 : : } else {
2746 : 0 : list[ns].info.master = 0;
2747 : 0 : list[ns].info.representor = 1;
2748 : : }
2749 : : /*
2750 : : * Ports of this type have uplink port index
2751 : : * encoded in the name. This index is also a PF index.
2752 : : */
2753 : 0 : list[ns].info.pf_num = list[ns].info.port_name;
2754 : 0 : list[ns].mpesw_port = list[ns].info.port_name;
2755 : 0 : list[ns].info.mpesw_owner = mpesw;
2756 : 0 : ns++;
2757 : 0 : break;
2758 : 0 : case MLX5_PHYS_PORT_NAME_TYPE_PFHPF:
2759 : : case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
2760 : : case MLX5_PHYS_PORT_NAME_TYPE_PFSF:
2761 : : /*
2762 : : * Ports of this type have PF index encoded in name,
2763 : : * which translate to the related uplink port index.
2764 : : */
2765 : 0 : list[ns].mpesw_port = list[ns].info.pf_num;
2766 : : /* MPESW owner is also saved but not used now. */
2767 : 0 : list[ns].info.mpesw_owner = mpesw;
2768 : 0 : ns++;
2769 : 0 : break;
2770 : : default:
2771 : : break;
2772 : : }
2773 : 0 : continue;
2774 : : }
2775 [ # # ]: 0 : if (!ret && (list[ns].info.representor ^
2776 [ # # ]: 0 : list[ns].info.master))
2777 : 0 : ns++;
2778 : : }
2779 : : } else {
2780 : : /*
2781 : : * The existence of several matching entries (nd > 1) means
2782 : : * port representors have been instantiated. No existing Verbs
2783 : : * call nor sysfs entries can tell them apart, this can only
2784 : : * be done through Netlink calls assuming kernel drivers are
2785 : : * recent enough to support them.
2786 : : *
2787 : : * In the event of identification failure through Netlink,
2788 : : * try again through sysfs, then:
2789 : : *
2790 : : * 1. A single IB device matches (nd == 1) with single
2791 : : * port (np=0/1) and is not a representor, assume
2792 : : * no switch support.
2793 : : *
2794 : : * 2. Otherwise no safe assumptions can be made;
2795 : : * complain louder and bail out.
2796 : : */
2797 [ # # ]: 0 : for (i = 0; i != nd; ++i) {
2798 [ # # ]: 0 : memset(&list[ns].info, 0, sizeof(list[ns].info));
2799 : 0 : list[ns].bond_info = NULL;
2800 : 0 : list[ns].max_port = 1;
2801 : 0 : list[ns].phys_port = 1;
2802 : 0 : list[ns].phys_dev_name = ibv_match[i]->name;
2803 : 0 : list[ns].eth_dev = NULL;
2804 : 0 : list[ns].pci_dev = pci_dev;
2805 : 0 : list[ns].cdev = cdev;
2806 : 0 : list[ns].pf_bond = -1;
2807 : 0 : list[ns].mpesw_port = MLX5_MPESW_PORT_INVALID;
2808 : 0 : list[ns].ifindex = 0;
2809 [ # # ]: 0 : if (nl_rdma >= 0)
2810 : 0 : list[ns].ifindex = mlx5_nl_ifindex
2811 : : (nl_rdma,
2812 : : ibv_match[i]->name,
2813 : : 1, &cdev->dev_info);
2814 [ # # ]: 0 : if (!list[ns].ifindex) {
2815 : : char ifname[IF_NAMESIZE];
2816 : :
2817 : : /*
2818 : : * Netlink failed, it may happen with old
2819 : : * ib_core kernel driver (before 4.16).
2820 : : * We can assume there is old driver because
2821 : : * here we are processing single ports IB
2822 : : * devices. Let's try sysfs to retrieve
2823 : : * the ifindex. The method works for
2824 : : * master device only.
2825 : : */
2826 [ # # ]: 0 : if (nd > 1) {
2827 : : /*
2828 : : * Multiple devices found, assume
2829 : : * representors, can not distinguish
2830 : : * master/representor and retrieve
2831 : : * ifindex via sysfs.
2832 : : */
2833 : 0 : continue;
2834 : : }
2835 : 0 : ret = mlx5_get_ifname_sysfs
2836 : 0 : (ibv_match[i]->ibdev_path, ifname);
2837 [ # # ]: 0 : if (!ret)
2838 : 0 : list[ns].ifindex =
2839 : 0 : if_nametoindex(ifname);
2840 [ # # ]: 0 : if (!list[ns].ifindex) {
2841 : : /*
2842 : : * No network interface index found
2843 : : * for the specified device, it means
2844 : : * there it is neither representor
2845 : : * nor master.
2846 : : */
2847 : 0 : continue;
2848 : : }
2849 : : }
2850 : 0 : ret = -1;
2851 [ # # ]: 0 : if (nl_route >= 0)
2852 : 0 : ret = mlx5_nl_switch_info(nl_route,
2853 : : list[ns].ifindex,
2854 : : &list[ns].info);
2855 [ # # # # ]: 0 : if (ret || (!list[ns].info.representor &&
2856 : : !list[ns].info.master)) {
2857 : : /*
2858 : : * We failed to recognize representors with
2859 : : * Netlink, let's try to perform the task
2860 : : * with sysfs.
2861 : : */
2862 : 0 : ret = mlx5_sysfs_switch_info(list[ns].ifindex,
2863 : : &list[ns].info);
2864 : : }
2865 [ # # ]: 0 : if (!ret && (list[ns].info.representor ^
2866 [ # # ]: 0 : list[ns].info.master)) {
2867 : 0 : ns++;
2868 [ # # ]: 0 : } else if ((nd == 1) &&
2869 [ # # ]: 0 : !list[ns].info.representor &&
2870 : : !list[ns].info.master) {
2871 : : /*
2872 : : * Single IB device with one physical port and
2873 : : * attached network device.
2874 : : * May be SRIOV is not enabled or there is no
2875 : : * representors.
2876 : : */
2877 : 0 : DRV_LOG(INFO, "No E-Switch support detected.");
2878 : 0 : ns++;
2879 : 0 : break;
2880 : : }
2881 : : }
2882 [ # # ]: 0 : if (!ns) {
2883 : 0 : DRV_LOG(ERR,
2884 : : "Unable to recognize master/representors on the multiple IB devices.");
2885 : 0 : rte_errno = ENOENT;
2886 : 0 : ret = -rte_errno;
2887 : 0 : goto exit;
2888 : : }
2889 : : /*
2890 : : * New kernels may add the switch_id attribute for the case
2891 : : * there is no E-Switch and we wrongly recognized the only
2892 : : * device as master. Override this if there is the single
2893 : : * device with single port and new device name format present.
2894 : : */
2895 [ # # ]: 0 : if (nd == 1 &&
2896 [ # # ]: 0 : list[0].info.name_type == MLX5_PHYS_PORT_NAME_TYPE_UPLINK) {
2897 : 0 : list[0].info.master = 0;
2898 : 0 : list[0].info.representor = 0;
2899 : : }
2900 : : }
2901 : : MLX5_ASSERT(ns);
2902 : : /* Calculate number of uplinks and host PFs for each matched IB device. */
2903 : 0 : calc_nb_uplinks_hpfs(ibv_match, nd, list, ns);
2904 : : /*
2905 : : * Sort list to probe devices in natural order for users convenience
2906 : : * (i.e. master first, then representors from lowest to highest ID).
2907 : : */
2908 : 0 : qsort(list, ns, sizeof(*list), mlx5_dev_spawn_data_cmp);
2909 [ # # ]: 0 : if (eth_da.type != RTE_ETH_REPRESENTOR_NONE) {
2910 : : /* Set devargs default values. */
2911 [ # # # # ]: 0 : if (eth_da.nb_ports == 0 && ns > 0) {
2912 [ # # # # ]: 0 : if (list[0].pf_bond >= 0 && list[0].info.representor)
2913 : 0 : DRV_LOG(WARNING, "Representor on Bonding device should use pf#vf# syntax: %s",
2914 : : pci_dev->device.devargs->args);
2915 : 0 : eth_da.nb_ports = 1;
2916 : 0 : eth_da.ports[0] = list[0].info.port_name;
2917 : : }
2918 [ # # ]: 0 : if (eth_da.nb_representor_ports == 0) {
2919 : 0 : eth_da.nb_representor_ports = 1;
2920 : 0 : eth_da.representor_ports[0] = 0;
2921 : : }
2922 : : }
2923 [ # # ]: 0 : for (i = 0; i != ns; ++i) {
2924 : : uint32_t restore;
2925 : :
2926 : 0 : list[i].eth_dev = mlx5_dev_spawn(cdev->dev, &list[i], ð_da,
2927 : : mkvlist);
2928 [ # # ]: 0 : if (!list[i].eth_dev) {
2929 [ # # ]: 0 : if (rte_errno != EBUSY && rte_errno != EEXIST)
2930 : : break;
2931 : : /* Device is disabled or already spawned. Ignore it. */
2932 : 0 : continue;
2933 : : }
2934 : 0 : restore = list[i].eth_dev->data->dev_flags;
2935 : 0 : rte_eth_copy_pci_info(list[i].eth_dev, pci_dev);
2936 : : /**
2937 : : * Each representor has a dedicated interrupts vector.
2938 : : * rte_eth_copy_pci_info() assigns PF interrupts handle to
2939 : : * representor eth_dev object because representor and PF
2940 : : * share the same PCI address.
2941 : : * Override representor device with a dedicated
2942 : : * interrupts handle here.
2943 : : * Representor interrupts handle is released in mlx5_dev_stop().
2944 : : */
2945 [ # # ]: 0 : if (list[i].info.representor) {
2946 : : struct rte_intr_handle *intr_handle =
2947 : 0 : rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_SHARED);
2948 [ # # ]: 0 : if (intr_handle == NULL) {
2949 : 0 : DRV_LOG(ERR,
2950 : : "port %u failed to allocate memory for interrupt handler "
2951 : : "Rx interrupts will not be supported",
2952 : : i);
2953 : 0 : rte_errno = ENOMEM;
2954 : 0 : ret = -rte_errno;
2955 : 0 : goto exit;
2956 : : }
2957 : 0 : list[i].eth_dev->intr_handle = intr_handle;
2958 : : }
2959 : : /* Restore non-PCI flags cleared by the above call. */
2960 : 0 : list[i].eth_dev->data->dev_flags |= restore;
2961 : 0 : rte_eth_dev_probing_finish(list[i].eth_dev);
2962 : : }
2963 [ # # ]: 0 : if (i != ns) {
2964 : 0 : DRV_LOG(ERR,
2965 : : "probe of PCI device " PCI_PRI_FMT " aborted after"
2966 : : " encountering an error: %s",
2967 : : owner_pci.domain, owner_pci.bus,
2968 : : owner_pci.devid, owner_pci.function,
2969 : : strerror(rte_errno));
2970 : 0 : ret = -rte_errno;
2971 : : /* Roll back. */
2972 [ # # ]: 0 : while (i--) {
2973 [ # # ]: 0 : if (!list[i].eth_dev)
2974 : 0 : continue;
2975 : 0 : mlx5_dev_close(list[i].eth_dev);
2976 : : /* mac_addrs must not be freed because in dev_private */
2977 : 0 : list[i].eth_dev->data->mac_addrs = NULL;
2978 : 0 : claim_zero(rte_eth_dev_release_port(list[i].eth_dev));
2979 : : }
2980 : : /* Restore original error. */
2981 : 0 : rte_errno = -ret;
2982 : : } else {
2983 : 0 : ret = 0;
2984 : : }
2985 : 0 : exit:
2986 : : /*
2987 : : * Do the routine cleanup:
2988 : : * - close opened Netlink sockets
2989 : : * - free allocated spawn data array
2990 : : * - free the Infiniband device list
2991 : : */
2992 [ # # ]: 0 : if (nl_rdma >= 0)
2993 : 0 : close(nl_rdma);
2994 [ # # ]: 0 : if (nl_route >= 0)
2995 : 0 : close(nl_route);
2996 [ # # ]: 0 : if (list)
2997 : 0 : mlx5_free(list);
2998 : : MLX5_ASSERT(ibv_list);
2999 : 0 : mlx5_glue->free_device_list(ibv_list);
3000 [ # # ]: 0 : if (ret) {
3001 [ # # ]: 0 : if (cdev->dev_info.port_info != NULL)
3002 : 0 : mlx5_free(cdev->dev_info.port_info);
3003 : 0 : memset(&cdev->dev_info, 0, sizeof(cdev->dev_info));
3004 : : }
3005 : 0 : return ret;
3006 : : }
3007 : :
3008 : : static int
3009 : 0 : mlx5_os_parse_eth_devargs(struct rte_device *dev,
3010 : : struct rte_eth_devargs *eth_da)
3011 : : {
3012 : : int ret = 0;
3013 : :
3014 [ # # ]: 0 : if (dev->devargs == NULL)
3015 : : return 0;
3016 : : memset(eth_da, 0, sizeof(*eth_da));
3017 : : /* Parse representor information first from class argument. */
3018 [ # # ]: 0 : if (dev->devargs->cls_str)
3019 : 0 : ret = rte_eth_devargs_parse(dev->devargs->cls_str, eth_da, 1);
3020 [ # # ]: 0 : if (ret < 0) {
3021 : 0 : DRV_LOG(ERR, "failed to parse device arguments: %s",
3022 : : dev->devargs->cls_str);
3023 : 0 : return -rte_errno;
3024 : : }
3025 [ # # # # ]: 0 : if (eth_da->type == RTE_ETH_REPRESENTOR_NONE && dev->devargs->args) {
3026 : : /* Parse legacy device argument */
3027 : 0 : ret = rte_eth_devargs_parse(dev->devargs->args, eth_da, 1);
3028 [ # # ]: 0 : if (ret < 0) {
3029 : 0 : DRV_LOG(ERR, "failed to parse device arguments: %s",
3030 : : dev->devargs->args);
3031 : 0 : return -rte_errno;
3032 : : }
3033 : : }
3034 : : return 0;
3035 : : }
3036 : :
3037 : : /**
3038 : : * Callback to register a PCI device.
3039 : : *
3040 : : * This function spawns Ethernet devices out of a given PCI device.
3041 : : *
3042 : : * @param[in] cdev
3043 : : * Pointer to common mlx5 device structure.
3044 : : * @param[in, out] mkvlist
3045 : : * Pointer to mlx5 kvargs control, can be NULL if there is no devargs.
3046 : : *
3047 : : * @return
3048 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
3049 : : */
3050 : : static int
3051 : 0 : mlx5_os_pci_probe(struct mlx5_common_device *cdev,
3052 : : struct mlx5_kvargs_ctrl *mkvlist)
3053 : : {
3054 : 0 : struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(cdev->dev);
3055 : 0 : struct rte_eth_devargs eth_da = { .nb_ports = 0 };
3056 : : int ret = 0;
3057 : : uint16_t p;
3058 : :
3059 : 0 : ret = mlx5_os_parse_eth_devargs(cdev->dev, ð_da);
3060 [ # # ]: 0 : if (ret != 0)
3061 : : return ret;
3062 : :
3063 [ # # ]: 0 : if (eth_da.nb_ports > 0) {
3064 : : /* Iterate all port if devargs pf is range: "pf[0-1]vf[...]". */
3065 [ # # ]: 0 : for (p = 0; p < eth_da.nb_ports; p++) {
3066 : 0 : ret = mlx5_os_pci_probe_pf(cdev, ð_da,
3067 : 0 : eth_da.ports[p], mkvlist);
3068 [ # # ]: 0 : if (ret) {
3069 : 0 : DRV_LOG(INFO, "Probe of PCI device " PCI_PRI_FMT " "
3070 : : "aborted due to proding failure of PF %u",
3071 : : pci_dev->addr.domain, pci_dev->addr.bus,
3072 : : pci_dev->addr.devid, pci_dev->addr.function,
3073 : : eth_da.ports[p]);
3074 : 0 : mlx5_net_remove(cdev);
3075 [ # # ]: 0 : if (p != 0)
3076 : : break;
3077 : : }
3078 : : }
3079 : : } else {
3080 : 0 : ret = mlx5_os_pci_probe_pf(cdev, ð_da, 0, mkvlist);
3081 : : }
3082 : : return ret;
3083 : : }
3084 : :
3085 : : /* Probe a single SF device on auxiliary bus, no representor support. */
3086 : : static int
3087 : 0 : mlx5_os_auxiliary_probe(struct mlx5_common_device *cdev,
3088 : : struct mlx5_kvargs_ctrl *mkvlist)
3089 : : {
3090 : 0 : struct rte_eth_devargs eth_da = { .nb_ports = 0 };
3091 : 0 : struct mlx5_dev_spawn_data spawn = {
3092 : : .pf_bond = -1,
3093 : : .mpesw_port = MLX5_MPESW_PORT_INVALID,
3094 : : };
3095 : 0 : struct rte_device *dev = cdev->dev;
3096 : 0 : struct rte_auxiliary_device *adev = RTE_DEV_TO_AUXILIARY(dev);
3097 : : struct rte_eth_dev *eth_dev;
3098 : : int ret = 0;
3099 : :
3100 : : /* Parse ethdev devargs. */
3101 : 0 : ret = mlx5_os_parse_eth_devargs(dev, ð_da);
3102 [ # # ]: 0 : if (ret != 0)
3103 : : return ret;
3104 : : /* Init spawn data. */
3105 : 0 : spawn.max_port = 1;
3106 : 0 : spawn.phys_port = 1;
3107 [ # # ]: 0 : spawn.phys_dev_name = mlx5_os_get_ctx_device_name(cdev->ctx);
3108 : 0 : ret = mlx5_auxiliary_get_ifindex(dev->name);
3109 [ # # ]: 0 : if (ret < 0) {
3110 : 0 : DRV_LOG(ERR, "failed to get ethdev ifindex: %s", dev->name);
3111 : 0 : return ret;
3112 : : }
3113 : 0 : spawn.ifindex = ret;
3114 : 0 : spawn.cdev = cdev;
3115 : : /* Spawn device. */
3116 : 0 : eth_dev = mlx5_dev_spawn(dev, &spawn, ð_da, mkvlist);
3117 [ # # ]: 0 : if (eth_dev == NULL)
3118 : 0 : return -rte_errno;
3119 : : /* Post create. */
3120 : 0 : eth_dev->intr_handle = adev->intr_handle;
3121 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
3122 : 0 : eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
3123 : 0 : eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_RMV;
3124 : 0 : eth_dev->data->numa_node = dev->numa_node;
3125 : : }
3126 : 0 : rte_eth_dev_probing_finish(eth_dev);
3127 : 0 : return 0;
3128 : : }
3129 : :
3130 : : /**
3131 : : * Net class driver callback to probe a device.
3132 : : *
3133 : : * This function probe PCI bus device(s) or a single SF on auxiliary bus.
3134 : : *
3135 : : * @param[in] cdev
3136 : : * Pointer to the common mlx5 device.
3137 : : * @param[in, out] mkvlist
3138 : : * Pointer to mlx5 kvargs control, can be NULL if there is no devargs.
3139 : : *
3140 : : * @return
3141 : : * 0 on success, a negative errno value otherwise and rte_errno is set.
3142 : : */
3143 : : int
3144 : 0 : mlx5_os_net_probe(struct mlx5_common_device *cdev,
3145 : : struct mlx5_kvargs_ctrl *mkvlist)
3146 : : {
3147 : : int ret;
3148 : :
3149 [ # # ]: 0 : if (rte_eal_process_type() == RTE_PROC_PRIMARY)
3150 : 0 : mlx5_pmd_socket_init();
3151 : 0 : ret = mlx5_init_once();
3152 [ # # ]: 0 : if (ret) {
3153 : 0 : DRV_LOG(ERR, "Unable to init PMD global data: %s",
3154 : : strerror(rte_errno));
3155 : 0 : return -rte_errno;
3156 : : }
3157 : 0 : ret = mlx5_probe_again_args_validate(cdev, mkvlist);
3158 [ # # ]: 0 : if (ret) {
3159 : 0 : DRV_LOG(ERR, "Probe again parameters are not compatible : %s",
3160 : : strerror(rte_errno));
3161 : 0 : return -rte_errno;
3162 : : }
3163 [ # # ]: 0 : if (mlx5_dev_is_pci(cdev->dev))
3164 : 0 : return mlx5_os_pci_probe(cdev, mkvlist);
3165 : : else
3166 : 0 : return mlx5_os_auxiliary_probe(cdev, mkvlist);
3167 : : }
3168 : :
3169 : : /**
3170 : : * Cleanup resources when the last device is closed.
3171 : : */
3172 : : void
3173 : 0 : mlx5_os_net_cleanup(void)
3174 : : {
3175 : 0 : mlx5_pmd_socket_uninit();
3176 : 0 : }
3177 : :
3178 : : /**
3179 : : * Install shared asynchronous device events handler.
3180 : : * This function is implemented to support event sharing
3181 : : * between multiple ports of single IB device.
3182 : : *
3183 : : * @param sh
3184 : : * Pointer to mlx5_dev_ctx_shared object.
3185 : : */
3186 : : void
3187 : 0 : mlx5_os_dev_shared_handler_install(struct mlx5_dev_ctx_shared *sh)
3188 : : {
3189 : 0 : struct ibv_context *ctx = sh->cdev->ctx;
3190 : : int nlsk_fd;
3191 : 0 : uint8_t rdma_monitor_supp = 0;
3192 : :
3193 : 0 : sh->intr_handle = mlx5_os_interrupt_handler_create
3194 : : (RTE_INTR_INSTANCE_F_SHARED, true,
3195 : : ctx->async_fd, mlx5_dev_interrupt_handler, sh);
3196 [ # # ]: 0 : if (!sh->intr_handle) {
3197 : 0 : DRV_LOG(ERR, "Failed to allocate intr_handle.");
3198 : 0 : return;
3199 : : }
3200 [ # # ]: 0 : if (sh->cdev->dev_info.probe_opt &&
3201 [ # # ]: 0 : sh->cdev->dev_info.port_num > 1 &&
3202 [ # # ]: 0 : !sh->rdma_monitor_supp) {
3203 : 0 : nlsk_fd = mlx5_nl_rdma_monitor_init();
3204 [ # # ]: 0 : if (nlsk_fd < 0) {
3205 : 0 : DRV_LOG(ERR, "Failed to create a socket for RDMA Netlink events: %s",
3206 : : rte_strerror(rte_errno));
3207 : 0 : return;
3208 : : }
3209 [ # # ]: 0 : if (mlx5_nl_rdma_monitor_cap_get(nlsk_fd, &rdma_monitor_supp)) {
3210 : 0 : DRV_LOG(ERR, "Failed to query RDMA monitor support: %s",
3211 : : rte_strerror(rte_errno));
3212 : 0 : close(nlsk_fd);
3213 : 0 : return;
3214 : : }
3215 : 0 : sh->rdma_monitor_supp = rdma_monitor_supp;
3216 [ # # ]: 0 : if (sh->rdma_monitor_supp) {
3217 : 0 : sh->intr_handle_ib = mlx5_os_interrupt_handler_create
3218 : : (RTE_INTR_INSTANCE_F_SHARED, true,
3219 : : nlsk_fd, mlx5_dev_interrupt_handler_ib, sh);
3220 [ # # ]: 0 : if (sh->intr_handle_ib == NULL) {
3221 : 0 : DRV_LOG(ERR, "Fail to allocate intr_handle");
3222 : 0 : close(nlsk_fd);
3223 : 0 : return;
3224 : : }
3225 : 0 : sh->cdev->dev_info.async_mon_ready = 1;
3226 : : } else {
3227 : 0 : close(nlsk_fd);
3228 [ # # ]: 0 : if (sh->cdev->dev_info.probe_opt) {
3229 : 0 : DRV_LOG(INFO, "Failed to create rdma link monitor, disable probe optimization");
3230 : 0 : sh->cdev->dev_info.probe_opt = 0;
3231 : 0 : mlx5_free(sh->cdev->dev_info.port_info);
3232 : 0 : sh->cdev->dev_info.port_info = NULL;
3233 : : }
3234 : : }
3235 : : }
3236 : 0 : nlsk_fd = mlx5_nl_init(NETLINK_ROUTE, RTMGRP_LINK);
3237 [ # # ]: 0 : if (nlsk_fd < 0) {
3238 : 0 : DRV_LOG(ERR, "Failed to create a socket for Netlink events: %s",
3239 : : rte_strerror(rte_errno));
3240 : 0 : return;
3241 : : }
3242 : 0 : sh->intr_handle_nl = mlx5_os_interrupt_handler_create
3243 : : (RTE_INTR_INSTANCE_F_SHARED, true,
3244 : : nlsk_fd, mlx5_dev_interrupt_handler_nl, sh);
3245 [ # # ]: 0 : if (sh->intr_handle_nl == NULL) {
3246 : 0 : DRV_LOG(ERR, "Fail to allocate intr_handle");
3247 : 0 : return;
3248 : : }
3249 [ # # ]: 0 : if (sh->cdev->config.devx) {
3250 : : #ifdef HAVE_IBV_DEVX_ASYNC
3251 : : struct mlx5dv_devx_cmd_comp *devx_comp;
3252 : :
3253 : 0 : sh->devx_comp = (void *)mlx5_glue->devx_create_cmd_comp(ctx);
3254 : : devx_comp = sh->devx_comp;
3255 [ # # ]: 0 : if (!devx_comp) {
3256 : 0 : DRV_LOG(INFO, "failed to allocate devx_comp.");
3257 : 0 : return;
3258 : : }
3259 : 0 : sh->intr_handle_devx = mlx5_os_interrupt_handler_create
3260 : : (RTE_INTR_INSTANCE_F_SHARED, true,
3261 : : devx_comp->fd,
3262 : : mlx5_dev_interrupt_handler_devx, sh);
3263 [ # # ]: 0 : if (!sh->intr_handle_devx) {
3264 : 0 : DRV_LOG(ERR, "Failed to allocate intr_handle.");
3265 : 0 : return;
3266 : : }
3267 : : #endif /* HAVE_IBV_DEVX_ASYNC */
3268 : : }
3269 : : }
3270 : :
3271 : : /**
3272 : : * Uninstall shared asynchronous device events handler.
3273 : : * This function is implemented to support event sharing
3274 : : * between multiple ports of single IB device.
3275 : : *
3276 : : * @param dev
3277 : : * Pointer to mlx5_dev_ctx_shared object.
3278 : : */
3279 : : void
3280 : 0 : mlx5_os_dev_shared_handler_uninstall(struct mlx5_dev_ctx_shared *sh)
3281 : : {
3282 : : int fd;
3283 : :
3284 : 0 : mlx5_os_interrupt_handler_destroy(sh->intr_handle,
3285 : : mlx5_dev_interrupt_handler, sh);
3286 : 0 : fd = rte_intr_fd_get(sh->intr_handle_nl);
3287 : 0 : mlx5_os_interrupt_handler_destroy(sh->intr_handle_nl,
3288 : : mlx5_dev_interrupt_handler_nl, sh);
3289 [ # # ]: 0 : if (fd >= 0)
3290 : 0 : close(fd);
3291 : : #ifdef HAVE_IBV_DEVX_ASYNC
3292 : 0 : mlx5_os_interrupt_handler_destroy(sh->intr_handle_devx,
3293 : : mlx5_dev_interrupt_handler_devx, sh);
3294 [ # # ]: 0 : if (sh->devx_comp)
3295 : 0 : mlx5_glue->devx_destroy_cmd_comp(sh->devx_comp);
3296 : : #endif
3297 : 0 : fd = rte_intr_fd_get(sh->intr_handle_ib);
3298 : 0 : mlx5_os_interrupt_handler_destroy(sh->intr_handle_ib,
3299 : : mlx5_dev_interrupt_handler_ib, sh);
3300 [ # # ]: 0 : if (fd >= 0)
3301 : 0 : close(fd);
3302 : 0 : }
3303 : :
3304 : : /**
3305 : : * Read statistics by a named counter.
3306 : : *
3307 : : * @param[in] priv
3308 : : * Pointer to the private device data structure.
3309 : : * @param[in] ctr_name
3310 : : * Pointer to the name of the statistic counter to read
3311 : : * @param[out] stat
3312 : : * Pointer to read statistic value.
3313 : : * @return
3314 : : * 0 on success and stat is valud, 1 if failed to read the value
3315 : : * rte_errno is set.
3316 : : *
3317 : : */
3318 : : int
3319 : 0 : mlx5_os_read_dev_stat(struct mlx5_priv *priv, const char *ctr_name,
3320 : : uint64_t *stat)
3321 : : {
3322 : : int fd;
3323 : :
3324 [ # # ]: 0 : if (priv->sh) {
3325 [ # # ]: 0 : if (priv->q_counters != NULL &&
3326 [ # # ]: 0 : strcmp(ctr_name, "out_of_buffer") == 0) {
3327 : 0 : return mlx5_read_queue_counter(priv->q_counters, ctr_name, stat);
3328 : : }
3329 [ # # ]: 0 : if (priv->q_counter_hairpin != NULL &&
3330 [ # # ]: 0 : strcmp(ctr_name, "hairpin_out_of_buffer") == 0) {
3331 : 0 : return mlx5_read_queue_counter(priv->q_counter_hairpin, ctr_name, stat);
3332 : : }
3333 : 0 : MKSTR(path, "%s/ports/%d/hw_counters/%s",
3334 : : priv->sh->ibdev_path,
3335 : : priv->dev_port,
3336 : : ctr_name);
3337 : : fd = open(path, O_RDONLY);
3338 : : /*
3339 : : * in switchdev the file location is not per port
3340 : : * but rather in <ibdev_path>/hw_counters/<file_name>.
3341 : : */
3342 [ # # ]: 0 : if (fd == -1) {
3343 : 0 : MKSTR(path1, "%s/hw_counters/%s",
3344 : : priv->sh->ibdev_path,
3345 : : ctr_name);
3346 : : fd = open(path1, O_RDONLY);
3347 : : }
3348 [ # # ]: 0 : if (fd != -1) {
3349 : 0 : char buf[21] = {'\0'};
3350 : : ssize_t n = read(fd, buf, sizeof(buf));
3351 : :
3352 : 0 : close(fd);
3353 [ # # ]: 0 : if (n != -1) {
3354 : 0 : *stat = strtoull(buf, NULL, 10);
3355 : 0 : return 0;
3356 : : }
3357 : : }
3358 : : }
3359 : 0 : *stat = 0;
3360 : 0 : return 1;
3361 : : }
3362 : :
3363 : : /**
3364 : : * Remove a MAC address from device
3365 : : *
3366 : : * @param dev
3367 : : * Pointer to Ethernet device structure.
3368 : : * @param index
3369 : : * MAC address index.
3370 : : */
3371 : : void
3372 : 0 : mlx5_os_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
3373 : : {
3374 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
3375 : 0 : const int vf = priv->sh->dev_cap.vf;
3376 : :
3377 [ # # ]: 0 : if (vf)
3378 : 0 : mlx5_nl_mac_addr_remove(priv->nl_socket_route,
3379 : : mlx5_ifindex(dev),
3380 : 0 : &dev->data->mac_addrs[index], index);
3381 [ # # ]: 0 : if (index < MLX5_MAX_MAC_ADDRESSES)
3382 : 0 : BITFIELD_RESET(priv->mac_own, index);
3383 : 0 : }
3384 : :
3385 : : /**
3386 : : * Adds a MAC address to the device
3387 : : *
3388 : : * @param dev
3389 : : * Pointer to Ethernet device structure.
3390 : : * @param mac_addr
3391 : : * MAC address to register.
3392 : : * @param index
3393 : : * MAC address index.
3394 : : *
3395 : : * @return
3396 : : * 0 on success, a negative errno value otherwise
3397 : : */
3398 : : int
3399 : 0 : mlx5_os_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac,
3400 : : uint32_t index)
3401 : : {
3402 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
3403 : 0 : const int vf = priv->sh->dev_cap.vf;
3404 : : int ret = 0;
3405 : :
3406 [ # # ]: 0 : if (vf)
3407 : 0 : ret = mlx5_nl_mac_addr_add(priv->nl_socket_route,
3408 : : mlx5_ifindex(dev),
3409 : : mac, index);
3410 [ # # ]: 0 : if (!ret)
3411 : 0 : BITFIELD_SET(priv->mac_own, index);
3412 : :
3413 : 0 : return ret;
3414 : : }
3415 : :
3416 : : /**
3417 : : * Modify a VF MAC address
3418 : : *
3419 : : * @param priv
3420 : : * Pointer to device private data.
3421 : : * @param mac_addr
3422 : : * MAC address to modify into.
3423 : : * @param iface_idx
3424 : : * Net device interface index
3425 : : * @param vf_index
3426 : : * VF index
3427 : : *
3428 : : * @return
3429 : : * 0 on success, a negative errno value otherwise
3430 : : */
3431 : : int
3432 : 0 : mlx5_os_vf_mac_addr_modify(struct mlx5_priv *priv,
3433 : : unsigned int iface_idx,
3434 : : struct rte_ether_addr *mac_addr,
3435 : : int vf_index)
3436 : : {
3437 : 0 : return mlx5_nl_vf_mac_addr_modify
3438 : : (priv->nl_socket_route, iface_idx, mac_addr, vf_index);
3439 : : }
3440 : :
3441 : : /**
3442 : : * Set device promiscuous mode
3443 : : *
3444 : : * @param dev
3445 : : * Pointer to Ethernet device structure.
3446 : : * @param enable
3447 : : * 0 - promiscuous is disabled, otherwise - enabled
3448 : : *
3449 : : * @return
3450 : : * 0 on success, a negative error value otherwise
3451 : : */
3452 : : int
3453 : 0 : mlx5_os_set_promisc(struct rte_eth_dev *dev, int enable)
3454 : : {
3455 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
3456 : :
3457 : 0 : return mlx5_nl_promisc(priv->nl_socket_route,
3458 : : mlx5_ifindex(dev), !!enable);
3459 : : }
3460 : :
3461 : : /**
3462 : : * Set device promiscuous mode
3463 : : *
3464 : : * @param dev
3465 : : * Pointer to Ethernet device structure.
3466 : : * @param enable
3467 : : * 0 - all multicase is disabled, otherwise - enabled
3468 : : *
3469 : : * @return
3470 : : * 0 on success, a negative error value otherwise
3471 : : */
3472 : : int
3473 : 0 : mlx5_os_set_allmulti(struct rte_eth_dev *dev, int enable)
3474 : : {
3475 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
3476 : :
3477 : 0 : return mlx5_nl_allmulti(priv->nl_socket_route,
3478 : : mlx5_ifindex(dev), !!enable);
3479 : : }
3480 : :
3481 : : /**
3482 : : * Flush device MAC addresses
3483 : : *
3484 : : * @param dev
3485 : : * Pointer to Ethernet device structure.
3486 : : *
3487 : : */
3488 : : void
3489 : 0 : mlx5_os_mac_addr_flush(struct rte_eth_dev *dev)
3490 : : {
3491 : 0 : struct mlx5_priv *priv = dev->data->dev_private;
3492 : 0 : const int vf = priv->sh->dev_cap.vf;
3493 : :
3494 : 0 : mlx5_nl_mac_addr_flush(priv->nl_socket_route, mlx5_ifindex(dev),
3495 : : dev->data->mac_addrs,
3496 : 0 : MLX5_MAX_MAC_ADDRESSES, priv->mac_own, vf);
3497 : 0 : }
3498 : :
3499 : : static bool
3500 : : mlx5_hws_is_supported(struct mlx5_dev_ctx_shared *sh)
3501 : : {
3502 [ # # # # ]: 0 : return (sh->cdev->config.devx &&
3503 : : sh->cdev->config.hca_attr.wqe_based_flow_table_sup);
3504 : : }
3505 : :
3506 : : static bool
3507 : : mlx5_sws_is_any_supported(struct mlx5_dev_ctx_shared *sh)
3508 : : {
3509 : : struct mlx5_common_device *cdev = sh->cdev;
3510 : : struct mlx5_hca_attr *hca_attr = &cdev->config.hca_attr;
3511 : :
3512 [ # # # # ]: 0 : if (hca_attr->rx_sw_owner_v2 || hca_attr->rx_sw_owner)
3513 : : return true;
3514 : :
3515 [ # # # # ]: 0 : if (hca_attr->tx_sw_owner_v2 || hca_attr->tx_sw_owner)
3516 : : return true;
3517 : :
3518 [ # # # # : 0 : if (hca_attr->eswitch_manager && (hca_attr->esw_sw_owner_v2 || hca_attr->esw_sw_owner))
# # # # ]
3519 : 0 : return true;
3520 : :
3521 : : return false;
3522 : : }
3523 : :
3524 : : /**
3525 : : * Initialize default shared configuration for arguments related to flow engine.
3526 : : *
3527 : : * @param[in] sh
3528 : : * Pointer to shared configuration.
3529 : : * @param[in] sh
3530 : : * Pointer to shared device context.
3531 : : */
3532 : : void
3533 [ # # ]: 0 : mlx5_os_default_flow_config(struct mlx5_sh_config *config, struct mlx5_dev_ctx_shared *sh)
3534 : : {
3535 : : bool hws_is_supported = mlx5_hws_is_supported(sh);
3536 : : bool sws_is_supported = mlx5_sws_is_any_supported(sh);
3537 : :
3538 [ # # ]: 0 : if (!sws_is_supported && hws_is_supported)
3539 : 0 : config->dv_flow_en = 2;
3540 : : else
3541 : 0 : config->dv_flow_en = 1;
3542 : :
3543 [ # # ]: 0 : if (config->dv_flow_en == 2)
3544 : 0 : config->allow_duplicate_pattern = 0;
3545 : : else
3546 : 0 : config->allow_duplicate_pattern = 1;
3547 : 0 : }
3548 : :
3549 : : static bool
3550 : 0 : mlx5_kvargs_is_used(struct mlx5_kvargs_ctrl *mkvlist, const char *key)
3551 : : {
3552 : : const struct rte_kvargs_pair *pair;
3553 : : uint32_t i;
3554 : :
3555 [ # # ]: 0 : for (i = 0; i < mkvlist->kvlist->count; ++i) {
3556 : : pair = &mkvlist->kvlist->pairs[i];
3557 [ # # # # ]: 0 : if (strcmp(pair->key, key) == 0 && mkvlist->is_used[i])
3558 : : return true;
3559 : : }
3560 : : return false;
3561 : : }
3562 : :
3563 [ # # ]: 0 : void mlx5_os_fixup_flow_en(struct mlx5_sh_config *config,
3564 : : struct mlx5_dev_ctx_shared *sh)
3565 : : {
3566 : : bool hws_is_supported = mlx5_hws_is_supported(sh);
3567 : : bool sws_is_supported = mlx5_sws_is_any_supported(sh);
3568 : :
3569 : : /* Inform user if DV flow is not supported. */
3570 [ # # # # ]: 0 : if (config->dv_flow_en == 1 && !sws_is_supported && hws_is_supported) {
3571 : 0 : DRV_LOG(WARNING, "DV flow is not supported. Changing to HWS mode.");
3572 : 0 : config->dv_flow_en = 2;
3573 : : }
3574 : 0 : }
3575 : :
3576 : : void
3577 : 0 : mlx5_os_fixup_duplicate_pattern(struct mlx5_sh_config *config,
3578 : : struct mlx5_kvargs_ctrl *mkvlist,
3579 : : const char *key)
3580 : : {
3581 : : /* Handle allow_duplicate_pattern based on final dv_flow_en mode.
3582 : : * HWS mode (dv_flow_en=2) doesn't support duplicate patterns.
3583 : : * Warn only if user explicitly requested an incompatible setting.
3584 : : */
3585 [ # # # # ]: 0 : bool allow_dup_pattern_set = mkvlist != NULL &&
3586 : 0 : mlx5_kvargs_is_used(mkvlist, key);
3587 [ # # ]: 0 : if (config->dv_flow_en == 2) {
3588 [ # # # # ]: 0 : if (config->allow_duplicate_pattern == 1 && allow_dup_pattern_set)
3589 : 0 : DRV_LOG(WARNING, "Duplicate pattern is not supported with HWS. Disabling it.");
3590 : 0 : config->allow_duplicate_pattern = 0;
3591 [ # # ]: 0 : } else if (!allow_dup_pattern_set) {
3592 : : /* Non-HWS mode: set default to 1 only if not explicitly set by user */
3593 : 0 : config->allow_duplicate_pattern = 1;
3594 : : }
3595 : 0 : }
|