xref: /f-stack/dpdk/drivers/net/mlx5/mlx5.c (revision 16d80a6d)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2015 Mellanox Technologies, Ltd
4  */
5 
6 #include <stddef.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <assert.h>
10 #include <dlfcn.h>
11 #include <stdint.h>
12 #include <stdlib.h>
13 #include <errno.h>
14 #include <net/if.h>
15 #include <sys/mman.h>
16 #include <linux/netlink.h>
17 #include <linux/rtnetlink.h>
18 
19 /* Verbs header. */
20 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
21 #ifdef PEDANTIC
22 #pragma GCC diagnostic ignored "-Wpedantic"
23 #endif
24 #include <infiniband/verbs.h>
25 #ifdef PEDANTIC
26 #pragma GCC diagnostic error "-Wpedantic"
27 #endif
28 
29 #include <rte_malloc.h>
30 #include <rte_ethdev_driver.h>
31 #include <rte_ethdev_pci.h>
32 #include <rte_pci.h>
33 #include <rte_bus_pci.h>
34 #include <rte_common.h>
35 #include <rte_config.h>
36 #include <rte_eal_memconfig.h>
37 #include <rte_kvargs.h>
38 #include <rte_rwlock.h>
39 #include <rte_spinlock.h>
40 #include <rte_string_fns.h>
41 
42 #include "mlx5.h"
43 #include "mlx5_utils.h"
44 #include "mlx5_rxtx.h"
45 #include "mlx5_autoconf.h"
46 #include "mlx5_defs.h"
47 #include "mlx5_glue.h"
48 #include "mlx5_mr.h"
49 #include "mlx5_flow.h"
50 
51 /* Device parameter to enable RX completion queue compression. */
52 #define MLX5_RXQ_CQE_COMP_EN "rxq_cqe_comp_en"
53 
54 /* Device parameter to enable RX completion entry padding to 128B. */
55 #define MLX5_RXQ_CQE_PAD_EN "rxq_cqe_pad_en"
56 
57 /* Device parameter to enable padding Rx packet to cacheline size. */
58 #define MLX5_RXQ_PKT_PAD_EN "rxq_pkt_pad_en"
59 
60 /* Device parameter to enable Multi-Packet Rx queue. */
61 #define MLX5_RX_MPRQ_EN "mprq_en"
62 
63 /* Device parameter to configure log 2 of the number of strides for MPRQ. */
64 #define MLX5_RX_MPRQ_LOG_STRIDE_NUM "mprq_log_stride_num"
65 
66 /* Device parameter to limit the size of memcpy'd packet for MPRQ. */
67 #define MLX5_RX_MPRQ_MAX_MEMCPY_LEN "mprq_max_memcpy_len"
68 
69 /* Device parameter to set the minimum number of Rx queues to enable MPRQ. */
70 #define MLX5_RXQS_MIN_MPRQ "rxqs_min_mprq"
71 
72 /* Device parameter to configure inline send. */
73 #define MLX5_TXQ_INLINE "txq_inline"
74 
75 /*
76  * Device parameter to configure the number of TX queues threshold for
77  * enabling inline send.
78  */
79 #define MLX5_TXQS_MIN_INLINE "txqs_min_inline"
80 
81 /*
82  * Device parameter to configure the number of TX queues threshold for
83  * enabling vectorized Tx.
84  */
85 #define MLX5_TXQS_MAX_VEC "txqs_max_vec"
86 
87 /* Device parameter to enable multi-packet send WQEs. */
88 #define MLX5_TXQ_MPW_EN "txq_mpw_en"
89 
90 /* Device parameter to include 2 dsegs in the title WQEBB. */
91 #define MLX5_TXQ_MPW_HDR_DSEG_EN "txq_mpw_hdr_dseg_en"
92 
93 /* Device parameter to limit the size of inlining packet. */
94 #define MLX5_TXQ_MAX_INLINE_LEN "txq_max_inline_len"
95 
96 /* Device parameter to enable hardware Tx vector. */
97 #define MLX5_TX_VEC_EN "tx_vec_en"
98 
99 /* Device parameter to enable hardware Rx vector. */
100 #define MLX5_RX_VEC_EN "rx_vec_en"
101 
102 /* Allow L3 VXLAN flow creation. */
103 #define MLX5_L3_VXLAN_EN "l3_vxlan_en"
104 
105 /* Activate DV flow steering. */
106 #define MLX5_DV_FLOW_EN "dv_flow_en"
107 
108 /* Activate Netlink support in VF mode. */
109 #define MLX5_VF_NL_EN "vf_nl_en"
110 
111 /* Select port representors to instantiate. */
112 #define MLX5_REPRESENTOR "representor"
113 
114 #ifndef HAVE_IBV_MLX5_MOD_MPW
115 #define MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED (1 << 2)
116 #define MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW (1 << 3)
117 #endif
118 
119 #ifndef HAVE_IBV_MLX5_MOD_CQE_128B_COMP
120 #define MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP (1 << 4)
121 #endif
122 
123 static const char *MZ_MLX5_PMD_SHARED_DATA = "mlx5_pmd_shared_data";
124 
125 /* Shared memory between primary and secondary processes. */
126 struct mlx5_shared_data *mlx5_shared_data;
127 
128 /* Spinlock for mlx5_shared_data allocation. */
129 static rte_spinlock_t mlx5_shared_data_lock = RTE_SPINLOCK_INITIALIZER;
130 
131 /** Driver-specific log messages type. */
132 int mlx5_logtype;
133 
134 /**
135  * Prepare shared data between primary and secondary process.
136  */
137 static void
138 mlx5_prepare_shared_data(void)
139 {
140 	const struct rte_memzone *mz;
141 
142 	rte_spinlock_lock(&mlx5_shared_data_lock);
143 	if (mlx5_shared_data == NULL) {
144 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
145 			/* Allocate shared memory. */
146 			mz = rte_memzone_reserve(MZ_MLX5_PMD_SHARED_DATA,
147 						 sizeof(*mlx5_shared_data),
148 						 SOCKET_ID_ANY, 0);
149 		} else {
150 			/* Lookup allocated shared memory. */
151 			mz = rte_memzone_lookup(MZ_MLX5_PMD_SHARED_DATA);
152 		}
153 		if (mz == NULL)
154 			rte_panic("Cannot allocate mlx5 shared data\n");
155 		mlx5_shared_data = mz->addr;
156 		/* Initialize shared data. */
157 		if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
158 			LIST_INIT(&mlx5_shared_data->mem_event_cb_list);
159 			rte_rwlock_init(&mlx5_shared_data->mem_event_rwlock);
160 			rte_mem_event_callback_register("MLX5_MEM_EVENT_CB",
161 							mlx5_mr_mem_event_cb,
162 							NULL);
163 		}
164 	}
165 	rte_spinlock_unlock(&mlx5_shared_data_lock);
166 }
167 
168 /**
169  * Retrieve integer value from environment variable.
170  *
171  * @param[in] name
172  *   Environment variable name.
173  *
174  * @return
175  *   Integer value, 0 if the variable is not set.
176  */
177 int
178 mlx5_getenv_int(const char *name)
179 {
180 	const char *val = getenv(name);
181 
182 	if (val == NULL)
183 		return 0;
184 	return atoi(val);
185 }
186 
187 /**
188  * Verbs callback to allocate a memory. This function should allocate the space
189  * according to the size provided residing inside a huge page.
190  * Please note that all allocation must respect the alignment from libmlx5
191  * (i.e. currently sysconf(_SC_PAGESIZE)).
192  *
193  * @param[in] size
194  *   The size in bytes of the memory to allocate.
195  * @param[in] data
196  *   A pointer to the callback data.
197  *
198  * @return
199  *   Allocated buffer, NULL otherwise and rte_errno is set.
200  */
201 static void *
202 mlx5_alloc_verbs_buf(size_t size, void *data)
203 {
204 	struct mlx5_priv *priv = data;
205 	void *ret;
206 	size_t alignment = sysconf(_SC_PAGESIZE);
207 	unsigned int socket = SOCKET_ID_ANY;
208 
209 	if (priv->verbs_alloc_ctx.type == MLX5_VERBS_ALLOC_TYPE_TX_QUEUE) {
210 		const struct mlx5_txq_ctrl *ctrl = priv->verbs_alloc_ctx.obj;
211 
212 		socket = ctrl->socket;
213 	} else if (priv->verbs_alloc_ctx.type ==
214 		   MLX5_VERBS_ALLOC_TYPE_RX_QUEUE) {
215 		const struct mlx5_rxq_ctrl *ctrl = priv->verbs_alloc_ctx.obj;
216 
217 		socket = ctrl->socket;
218 	}
219 	assert(data != NULL);
220 	ret = rte_malloc_socket(__func__, size, alignment, socket);
221 	if (!ret && size)
222 		rte_errno = ENOMEM;
223 	return ret;
224 }
225 
226 /**
227  * Verbs callback to free a memory.
228  *
229  * @param[in] ptr
230  *   A pointer to the memory to free.
231  * @param[in] data
232  *   A pointer to the callback data.
233  */
234 static void
235 mlx5_free_verbs_buf(void *ptr, void *data __rte_unused)
236 {
237 	assert(data != NULL);
238 	rte_free(ptr);
239 }
240 
241 /**
242  * DPDK callback to close the device.
243  *
244  * Destroy all queues and objects, free memory.
245  *
246  * @param dev
247  *   Pointer to Ethernet device structure.
248  */
249 static void
250 mlx5_dev_close(struct rte_eth_dev *dev)
251 {
252 	struct mlx5_priv *priv = dev->data->dev_private;
253 	unsigned int i;
254 	int ret;
255 
256 	DRV_LOG(DEBUG, "port %u closing device \"%s\"",
257 		dev->data->port_id,
258 		((priv->ctx != NULL) ? priv->ctx->device->name : ""));
259 	/* In case mlx5_dev_stop() has not been called. */
260 	mlx5_dev_interrupt_handler_uninstall(dev);
261 	mlx5_traffic_disable(dev);
262 	mlx5_flow_flush(dev, NULL);
263 	/* Prevent crashes when queues are still in use. */
264 	dev->rx_pkt_burst = removed_rx_burst;
265 	dev->tx_pkt_burst = removed_tx_burst;
266 	if (priv->rxqs != NULL) {
267 		/* XXX race condition if mlx5_rx_burst() is still running. */
268 		usleep(1000);
269 		for (i = 0; (i != priv->rxqs_n); ++i)
270 			mlx5_rxq_release(dev, i);
271 		priv->rxqs_n = 0;
272 		priv->rxqs = NULL;
273 	}
274 	if (priv->txqs != NULL) {
275 		/* XXX race condition if mlx5_tx_burst() is still running. */
276 		usleep(1000);
277 		for (i = 0; (i != priv->txqs_n); ++i)
278 			mlx5_txq_release(dev, i);
279 		priv->txqs_n = 0;
280 		priv->txqs = NULL;
281 	}
282 	mlx5_mprq_free_mp(dev);
283 	mlx5_mr_release(dev);
284 	if (priv->pd != NULL) {
285 		assert(priv->ctx != NULL);
286 		claim_zero(mlx5_glue->dealloc_pd(priv->pd));
287 		claim_zero(mlx5_glue->close_device(priv->ctx));
288 	} else
289 		assert(priv->ctx == NULL);
290 	if (priv->rss_conf.rss_key != NULL)
291 		rte_free(priv->rss_conf.rss_key);
292 	if (priv->reta_idx != NULL)
293 		rte_free(priv->reta_idx);
294 	if (priv->primary_socket)
295 		mlx5_socket_uninit(dev);
296 	if (priv->config.vf)
297 		mlx5_nl_mac_addr_flush(dev);
298 	if (priv->nl_socket_route >= 0)
299 		close(priv->nl_socket_route);
300 	if (priv->nl_socket_rdma >= 0)
301 		close(priv->nl_socket_rdma);
302 	if (priv->tcf_context)
303 		mlx5_flow_tcf_context_destroy(priv->tcf_context);
304 	ret = mlx5_hrxq_ibv_verify(dev);
305 	if (ret)
306 		DRV_LOG(WARNING, "port %u some hash Rx queue still remain",
307 			dev->data->port_id);
308 	ret = mlx5_ind_table_ibv_verify(dev);
309 	if (ret)
310 		DRV_LOG(WARNING, "port %u some indirection table still remain",
311 			dev->data->port_id);
312 	ret = mlx5_rxq_ibv_verify(dev);
313 	if (ret)
314 		DRV_LOG(WARNING, "port %u some Verbs Rx queue still remain",
315 			dev->data->port_id);
316 	ret = mlx5_rxq_verify(dev);
317 	if (ret)
318 		DRV_LOG(WARNING, "port %u some Rx queues still remain",
319 			dev->data->port_id);
320 	ret = mlx5_txq_ibv_verify(dev);
321 	if (ret)
322 		DRV_LOG(WARNING, "port %u some Verbs Tx queue still remain",
323 			dev->data->port_id);
324 	ret = mlx5_txq_verify(dev);
325 	if (ret)
326 		DRV_LOG(WARNING, "port %u some Tx queues still remain",
327 			dev->data->port_id);
328 	ret = mlx5_flow_verify(dev);
329 	if (ret)
330 		DRV_LOG(WARNING, "port %u some flows still remain",
331 			dev->data->port_id);
332 	if (priv->domain_id != RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
333 		unsigned int c = 0;
334 		unsigned int i = mlx5_dev_to_port_id(dev->device, NULL, 0);
335 		uint16_t port_id[i];
336 
337 		i = RTE_MIN(mlx5_dev_to_port_id(dev->device, port_id, i), i);
338 		while (i--) {
339 			struct mlx5_priv *opriv =
340 				rte_eth_devices[port_id[i]].data->dev_private;
341 
342 			if (!opriv ||
343 			    opriv->domain_id != priv->domain_id ||
344 			    &rte_eth_devices[port_id[i]] == dev)
345 				continue;
346 			++c;
347 		}
348 		if (!c)
349 			claim_zero(rte_eth_switch_domain_free(priv->domain_id));
350 	}
351 	memset(priv, 0, sizeof(*priv));
352 	priv->domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
353 	/*
354 	 * Reset mac_addrs to NULL such that it is not freed as part of
355 	 * rte_eth_dev_release_port(). mac_addrs is part of dev_private so
356 	 * it is freed when dev_private is freed.
357 	 */
358 	dev->data->mac_addrs = NULL;
359 }
360 
361 const struct eth_dev_ops mlx5_dev_ops = {
362 	.dev_configure = mlx5_dev_configure,
363 	.dev_start = mlx5_dev_start,
364 	.dev_stop = mlx5_dev_stop,
365 	.dev_set_link_down = mlx5_set_link_down,
366 	.dev_set_link_up = mlx5_set_link_up,
367 	.dev_close = mlx5_dev_close,
368 	.promiscuous_enable = mlx5_promiscuous_enable,
369 	.promiscuous_disable = mlx5_promiscuous_disable,
370 	.allmulticast_enable = mlx5_allmulticast_enable,
371 	.allmulticast_disable = mlx5_allmulticast_disable,
372 	.link_update = mlx5_link_update,
373 	.stats_get = mlx5_stats_get,
374 	.stats_reset = mlx5_stats_reset,
375 	.xstats_get = mlx5_xstats_get,
376 	.xstats_reset = mlx5_xstats_reset,
377 	.xstats_get_names = mlx5_xstats_get_names,
378 	.dev_infos_get = mlx5_dev_infos_get,
379 	.dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
380 	.vlan_filter_set = mlx5_vlan_filter_set,
381 	.rx_queue_setup = mlx5_rx_queue_setup,
382 	.tx_queue_setup = mlx5_tx_queue_setup,
383 	.rx_queue_release = mlx5_rx_queue_release,
384 	.tx_queue_release = mlx5_tx_queue_release,
385 	.flow_ctrl_get = mlx5_dev_get_flow_ctrl,
386 	.flow_ctrl_set = mlx5_dev_set_flow_ctrl,
387 	.mac_addr_remove = mlx5_mac_addr_remove,
388 	.mac_addr_add = mlx5_mac_addr_add,
389 	.mac_addr_set = mlx5_mac_addr_set,
390 	.set_mc_addr_list = mlx5_set_mc_addr_list,
391 	.mtu_set = mlx5_dev_set_mtu,
392 	.vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
393 	.vlan_offload_set = mlx5_vlan_offload_set,
394 	.reta_update = mlx5_dev_rss_reta_update,
395 	.reta_query = mlx5_dev_rss_reta_query,
396 	.rss_hash_update = mlx5_rss_hash_update,
397 	.rss_hash_conf_get = mlx5_rss_hash_conf_get,
398 	.filter_ctrl = mlx5_dev_filter_ctrl,
399 	.rx_descriptor_status = mlx5_rx_descriptor_status,
400 	.tx_descriptor_status = mlx5_tx_descriptor_status,
401 	.rx_queue_count = mlx5_rx_queue_count,
402 	.rx_queue_intr_enable = mlx5_rx_intr_enable,
403 	.rx_queue_intr_disable = mlx5_rx_intr_disable,
404 	.is_removed = mlx5_is_removed,
405 };
406 
407 static const struct eth_dev_ops mlx5_dev_sec_ops = {
408 	.stats_get = mlx5_stats_get,
409 	.stats_reset = mlx5_stats_reset,
410 	.xstats_get = mlx5_xstats_get,
411 	.xstats_reset = mlx5_xstats_reset,
412 	.xstats_get_names = mlx5_xstats_get_names,
413 	.dev_infos_get = mlx5_dev_infos_get,
414 	.rx_descriptor_status = mlx5_rx_descriptor_status,
415 	.tx_descriptor_status = mlx5_tx_descriptor_status,
416 };
417 
418 /* Available operators in flow isolated mode. */
419 const struct eth_dev_ops mlx5_dev_ops_isolate = {
420 	.dev_configure = mlx5_dev_configure,
421 	.dev_start = mlx5_dev_start,
422 	.dev_stop = mlx5_dev_stop,
423 	.dev_set_link_down = mlx5_set_link_down,
424 	.dev_set_link_up = mlx5_set_link_up,
425 	.dev_close = mlx5_dev_close,
426 	.promiscuous_enable = mlx5_promiscuous_enable,
427 	.promiscuous_disable = mlx5_promiscuous_disable,
428 	.allmulticast_enable = mlx5_allmulticast_enable,
429 	.allmulticast_disable = mlx5_allmulticast_disable,
430 	.link_update = mlx5_link_update,
431 	.stats_get = mlx5_stats_get,
432 	.stats_reset = mlx5_stats_reset,
433 	.xstats_get = mlx5_xstats_get,
434 	.xstats_reset = mlx5_xstats_reset,
435 	.xstats_get_names = mlx5_xstats_get_names,
436 	.dev_infos_get = mlx5_dev_infos_get,
437 	.dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
438 	.vlan_filter_set = mlx5_vlan_filter_set,
439 	.rx_queue_setup = mlx5_rx_queue_setup,
440 	.tx_queue_setup = mlx5_tx_queue_setup,
441 	.rx_queue_release = mlx5_rx_queue_release,
442 	.tx_queue_release = mlx5_tx_queue_release,
443 	.flow_ctrl_get = mlx5_dev_get_flow_ctrl,
444 	.flow_ctrl_set = mlx5_dev_set_flow_ctrl,
445 	.mac_addr_remove = mlx5_mac_addr_remove,
446 	.mac_addr_add = mlx5_mac_addr_add,
447 	.mac_addr_set = mlx5_mac_addr_set,
448 	.set_mc_addr_list = mlx5_set_mc_addr_list,
449 	.mtu_set = mlx5_dev_set_mtu,
450 	.vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
451 	.vlan_offload_set = mlx5_vlan_offload_set,
452 	.filter_ctrl = mlx5_dev_filter_ctrl,
453 	.rx_descriptor_status = mlx5_rx_descriptor_status,
454 	.tx_descriptor_status = mlx5_tx_descriptor_status,
455 	.rx_queue_intr_enable = mlx5_rx_intr_enable,
456 	.rx_queue_intr_disable = mlx5_rx_intr_disable,
457 	.is_removed = mlx5_is_removed,
458 };
459 
460 /**
461  * Verify and store value for device argument.
462  *
463  * @param[in] key
464  *   Key argument to verify.
465  * @param[in] val
466  *   Value associated with key.
467  * @param opaque
468  *   User data.
469  *
470  * @return
471  *   0 on success, a negative errno value otherwise and rte_errno is set.
472  */
473 static int
474 mlx5_args_check(const char *key, const char *val, void *opaque)
475 {
476 	struct mlx5_dev_config *config = opaque;
477 	unsigned long tmp;
478 
479 	/* No-op, port representors are processed in mlx5_dev_spawn(). */
480 	if (!strcmp(MLX5_REPRESENTOR, key))
481 		return 0;
482 	errno = 0;
483 	tmp = strtoul(val, NULL, 0);
484 	if (errno) {
485 		rte_errno = errno;
486 		DRV_LOG(WARNING, "%s: \"%s\" is not a valid integer", key, val);
487 		return -rte_errno;
488 	}
489 	if (strcmp(MLX5_RXQ_CQE_COMP_EN, key) == 0) {
490 		config->cqe_comp = !!tmp;
491 	} else if (strcmp(MLX5_RXQ_CQE_PAD_EN, key) == 0) {
492 		config->cqe_pad = !!tmp;
493 	} else if (strcmp(MLX5_RXQ_PKT_PAD_EN, key) == 0) {
494 		config->hw_padding = !!tmp;
495 	} else if (strcmp(MLX5_RX_MPRQ_EN, key) == 0) {
496 		config->mprq.enabled = !!tmp;
497 	} else if (strcmp(MLX5_RX_MPRQ_LOG_STRIDE_NUM, key) == 0) {
498 		config->mprq.stride_num_n = tmp;
499 	} else if (strcmp(MLX5_RX_MPRQ_MAX_MEMCPY_LEN, key) == 0) {
500 		config->mprq.max_memcpy_len = tmp;
501 	} else if (strcmp(MLX5_RXQS_MIN_MPRQ, key) == 0) {
502 		config->mprq.min_rxqs_num = tmp;
503 	} else if (strcmp(MLX5_TXQ_INLINE, key) == 0) {
504 		config->txq_inline = tmp;
505 	} else if (strcmp(MLX5_TXQS_MIN_INLINE, key) == 0) {
506 		config->txqs_inline = tmp;
507 	} else if (strcmp(MLX5_TXQS_MAX_VEC, key) == 0) {
508 		config->txqs_vec = tmp;
509 	} else if (strcmp(MLX5_TXQ_MPW_EN, key) == 0) {
510 		config->mps = !!tmp;
511 	} else if (strcmp(MLX5_TXQ_MPW_HDR_DSEG_EN, key) == 0) {
512 		config->mpw_hdr_dseg = !!tmp;
513 	} else if (strcmp(MLX5_TXQ_MAX_INLINE_LEN, key) == 0) {
514 		config->inline_max_packet_sz = tmp;
515 	} else if (strcmp(MLX5_TX_VEC_EN, key) == 0) {
516 		config->tx_vec_en = !!tmp;
517 	} else if (strcmp(MLX5_RX_VEC_EN, key) == 0) {
518 		config->rx_vec_en = !!tmp;
519 	} else if (strcmp(MLX5_L3_VXLAN_EN, key) == 0) {
520 		config->l3_vxlan_en = !!tmp;
521 	} else if (strcmp(MLX5_VF_NL_EN, key) == 0) {
522 		config->vf_nl_en = !!tmp;
523 	} else if (strcmp(MLX5_DV_FLOW_EN, key) == 0) {
524 		config->dv_flow_en = !!tmp;
525 	} else {
526 		DRV_LOG(WARNING, "%s: unknown parameter", key);
527 		rte_errno = EINVAL;
528 		return -rte_errno;
529 	}
530 	return 0;
531 }
532 
533 /**
534  * Parse device parameters.
535  *
536  * @param config
537  *   Pointer to device configuration structure.
538  * @param devargs
539  *   Device arguments structure.
540  *
541  * @return
542  *   0 on success, a negative errno value otherwise and rte_errno is set.
543  */
544 static int
545 mlx5_args(struct mlx5_dev_config *config, struct rte_devargs *devargs)
546 {
547 	const char **params = (const char *[]){
548 		MLX5_RXQ_CQE_COMP_EN,
549 		MLX5_RXQ_CQE_PAD_EN,
550 		MLX5_RXQ_PKT_PAD_EN,
551 		MLX5_RX_MPRQ_EN,
552 		MLX5_RX_MPRQ_LOG_STRIDE_NUM,
553 		MLX5_RX_MPRQ_MAX_MEMCPY_LEN,
554 		MLX5_RXQS_MIN_MPRQ,
555 		MLX5_TXQ_INLINE,
556 		MLX5_TXQS_MIN_INLINE,
557 		MLX5_TXQS_MAX_VEC,
558 		MLX5_TXQ_MPW_EN,
559 		MLX5_TXQ_MPW_HDR_DSEG_EN,
560 		MLX5_TXQ_MAX_INLINE_LEN,
561 		MLX5_TX_VEC_EN,
562 		MLX5_RX_VEC_EN,
563 		MLX5_L3_VXLAN_EN,
564 		MLX5_VF_NL_EN,
565 		MLX5_DV_FLOW_EN,
566 		MLX5_REPRESENTOR,
567 		NULL,
568 	};
569 	struct rte_kvargs *kvlist;
570 	int ret = 0;
571 	int i;
572 
573 	if (devargs == NULL)
574 		return 0;
575 	/* Following UGLY cast is done to pass checkpatch. */
576 	kvlist = rte_kvargs_parse(devargs->args, params);
577 	if (kvlist == NULL)
578 		return 0;
579 	/* Process parameters. */
580 	for (i = 0; (params[i] != NULL); ++i) {
581 		if (rte_kvargs_count(kvlist, params[i])) {
582 			ret = rte_kvargs_process(kvlist, params[i],
583 						 mlx5_args_check, config);
584 			if (ret) {
585 				rte_errno = EINVAL;
586 				rte_kvargs_free(kvlist);
587 				return -rte_errno;
588 			}
589 		}
590 	}
591 	rte_kvargs_free(kvlist);
592 	return 0;
593 }
594 
595 static struct rte_pci_driver mlx5_driver;
596 
597 /*
598  * Reserved UAR address space for TXQ UAR(hw doorbell) mapping, process
599  * local resource used by both primary and secondary to avoid duplicate
600  * reservation.
601  * The space has to be available on both primary and secondary process,
602  * TXQ UAR maps to this area using fixed mmap w/o double check.
603  */
604 static void *uar_base;
605 
606 static int
607 find_lower_va_bound(const struct rte_memseg_list *msl,
608 		const struct rte_memseg *ms, void *arg)
609 {
610 	void **addr = arg;
611 
612 	if (msl->external)
613 		return 0;
614 	if (*addr == NULL)
615 		*addr = ms->addr;
616 	else
617 		*addr = RTE_MIN(*addr, ms->addr);
618 
619 	return 0;
620 }
621 
622 /**
623  * Reserve UAR address space for primary process.
624  *
625  * @param[in] dev
626  *   Pointer to Ethernet device.
627  *
628  * @return
629  *   0 on success, a negative errno value otherwise and rte_errno is set.
630  */
631 static int
632 mlx5_uar_init_primary(struct rte_eth_dev *dev)
633 {
634 	struct mlx5_priv *priv = dev->data->dev_private;
635 	void *addr = (void *)0;
636 
637 	if (uar_base) { /* UAR address space mapped. */
638 		priv->uar_base = uar_base;
639 		return 0;
640 	}
641 	/* find out lower bound of hugepage segments */
642 	rte_memseg_walk(find_lower_va_bound, &addr);
643 
644 	/* keep distance to hugepages to minimize potential conflicts. */
645 	addr = RTE_PTR_SUB(addr, (uintptr_t)(MLX5_UAR_OFFSET + MLX5_UAR_SIZE));
646 	/* anonymous mmap, no real memory consumption. */
647 	addr = mmap(addr, MLX5_UAR_SIZE,
648 		    PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
649 	if (addr == MAP_FAILED) {
650 		DRV_LOG(ERR,
651 			"port %u failed to reserve UAR address space, please"
652 			" adjust MLX5_UAR_SIZE or try --base-virtaddr",
653 			dev->data->port_id);
654 		rte_errno = ENOMEM;
655 		return -rte_errno;
656 	}
657 	/* Accept either same addr or a new addr returned from mmap if target
658 	 * range occupied.
659 	 */
660 	DRV_LOG(INFO, "port %u reserved UAR address space: %p",
661 		dev->data->port_id, addr);
662 	priv->uar_base = addr; /* for primary and secondary UAR re-mmap. */
663 	uar_base = addr; /* process local, don't reserve again. */
664 	return 0;
665 }
666 
667 /**
668  * Reserve UAR address space for secondary process, align with
669  * primary process.
670  *
671  * @param[in] dev
672  *   Pointer to Ethernet device.
673  *
674  * @return
675  *   0 on success, a negative errno value otherwise and rte_errno is set.
676  */
677 static int
678 mlx5_uar_init_secondary(struct rte_eth_dev *dev)
679 {
680 	struct mlx5_priv *priv = dev->data->dev_private;
681 	void *addr;
682 
683 	assert(priv->uar_base);
684 	if (uar_base) { /* already reserved. */
685 		assert(uar_base == priv->uar_base);
686 		return 0;
687 	}
688 	/* anonymous mmap, no real memory consumption. */
689 	addr = mmap(priv->uar_base, MLX5_UAR_SIZE,
690 		    PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
691 	if (addr == MAP_FAILED) {
692 		DRV_LOG(ERR, "port %u UAR mmap failed: %p size: %llu",
693 			dev->data->port_id, priv->uar_base, MLX5_UAR_SIZE);
694 		rte_errno = ENXIO;
695 		return -rte_errno;
696 	}
697 	if (priv->uar_base != addr) {
698 		DRV_LOG(ERR,
699 			"port %u UAR address %p size %llu occupied, please"
700 			" adjust MLX5_UAR_OFFSET or try EAL parameter"
701 			" --base-virtaddr",
702 			dev->data->port_id, priv->uar_base, MLX5_UAR_SIZE);
703 		rte_errno = ENXIO;
704 		return -rte_errno;
705 	}
706 	uar_base = addr; /* process local, don't reserve again */
707 	DRV_LOG(INFO, "port %u reserved UAR address space: %p",
708 		dev->data->port_id, addr);
709 	return 0;
710 }
711 
712 /**
713  * Spawn an Ethernet device from Verbs information.
714  *
715  * @param dpdk_dev
716  *   Backing DPDK device.
717  * @param ibv_dev
718  *   Verbs device.
719  * @param config
720  *   Device configuration parameters.
721  * @param[in] switch_info
722  *   Switch properties of Ethernet device.
723  *
724  * @return
725  *   A valid Ethernet device object on success, NULL otherwise and rte_errno
726  *   is set. The following errors are defined:
727  *
728  *   EBUSY: device is not supposed to be spawned.
729  *   EEXIST: device is already spawned
730  */
731 static struct rte_eth_dev *
732 mlx5_dev_spawn(struct rte_device *dpdk_dev,
733 	       struct ibv_device *ibv_dev,
734 	       struct mlx5_dev_config config,
735 	       const struct mlx5_switch_info *switch_info)
736 {
737 	struct ibv_context *ctx;
738 	struct ibv_device_attr_ex attr;
739 	struct ibv_port_attr port_attr;
740 	struct ibv_pd *pd = NULL;
741 	struct mlx5dv_context dv_attr = { .comp_mask = 0 };
742 	struct rte_eth_dev *eth_dev = NULL;
743 	struct mlx5_priv *priv = NULL;
744 	int err = 0;
745 	unsigned int hw_padding = 0;
746 	unsigned int mps;
747 	unsigned int cqe_comp;
748 	unsigned int cqe_pad = 0;
749 	unsigned int tunnel_en = 0;
750 	unsigned int mpls_en = 0;
751 	unsigned int swp = 0;
752 	unsigned int mprq = 0;
753 	unsigned int mprq_min_stride_size_n = 0;
754 	unsigned int mprq_max_stride_size_n = 0;
755 	unsigned int mprq_min_stride_num_n = 0;
756 	unsigned int mprq_max_stride_num_n = 0;
757 	struct ether_addr mac;
758 	char name[RTE_ETH_NAME_MAX_LEN];
759 	int own_domain_id = 0;
760 	uint16_t port_id;
761 	unsigned int i;
762 
763 	/* Determine if this port representor is supposed to be spawned. */
764 	if (switch_info->representor && dpdk_dev->devargs) {
765 		struct rte_eth_devargs eth_da;
766 
767 		err = rte_eth_devargs_parse(dpdk_dev->devargs->args, &eth_da);
768 		if (err) {
769 			rte_errno = -err;
770 			DRV_LOG(ERR, "failed to process device arguments: %s",
771 				strerror(rte_errno));
772 			return NULL;
773 		}
774 		for (i = 0; i < eth_da.nb_representor_ports; ++i)
775 			if (eth_da.representor_ports[i] ==
776 			    (uint16_t)switch_info->port_name)
777 				break;
778 		if (i == eth_da.nb_representor_ports) {
779 			rte_errno = EBUSY;
780 			return NULL;
781 		}
782 	}
783 	/* Build device name. */
784 	if (!switch_info->representor)
785 		rte_strlcpy(name, dpdk_dev->name, sizeof(name));
786 	else
787 		snprintf(name, sizeof(name), "%s_representor_%u",
788 			 dpdk_dev->name, switch_info->port_name);
789 	/* check if the device is already spawned */
790 	if (rte_eth_dev_get_port_by_name(name, &port_id) == 0) {
791 		rte_errno = EEXIST;
792 		return NULL;
793 	}
794 	/* Prepare shared data between primary and secondary process. */
795 	mlx5_prepare_shared_data();
796 	errno = 0;
797 	ctx = mlx5_glue->open_device(ibv_dev);
798 	if (!ctx) {
799 		rte_errno = errno ? errno : ENODEV;
800 		return NULL;
801 	}
802 #ifdef HAVE_IBV_MLX5_MOD_SWP
803 	dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_SWP;
804 #endif
805 	/*
806 	 * Multi-packet send is supported by ConnectX-4 Lx PF as well
807 	 * as all ConnectX-5 devices.
808 	 */
809 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
810 	dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS;
811 #endif
812 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
813 	dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_STRIDING_RQ;
814 #endif
815 	mlx5_glue->dv_query_device(ctx, &dv_attr);
816 	if (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED) {
817 		if (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW) {
818 			DRV_LOG(DEBUG, "enhanced MPW is supported");
819 			mps = MLX5_MPW_ENHANCED;
820 		} else {
821 			DRV_LOG(DEBUG, "MPW is supported");
822 			mps = MLX5_MPW;
823 		}
824 	} else {
825 		DRV_LOG(DEBUG, "MPW isn't supported");
826 		mps = MLX5_MPW_DISABLED;
827 	}
828 #ifdef HAVE_IBV_MLX5_MOD_SWP
829 	if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_SWP)
830 		swp = dv_attr.sw_parsing_caps.sw_parsing_offloads;
831 	DRV_LOG(DEBUG, "SWP support: %u", swp);
832 #endif
833 	config.swp = !!swp;
834 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
835 	if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_STRIDING_RQ) {
836 		struct mlx5dv_striding_rq_caps mprq_caps =
837 			dv_attr.striding_rq_caps;
838 
839 		DRV_LOG(DEBUG, "\tmin_single_stride_log_num_of_bytes: %d",
840 			mprq_caps.min_single_stride_log_num_of_bytes);
841 		DRV_LOG(DEBUG, "\tmax_single_stride_log_num_of_bytes: %d",
842 			mprq_caps.max_single_stride_log_num_of_bytes);
843 		DRV_LOG(DEBUG, "\tmin_single_wqe_log_num_of_strides: %d",
844 			mprq_caps.min_single_wqe_log_num_of_strides);
845 		DRV_LOG(DEBUG, "\tmax_single_wqe_log_num_of_strides: %d",
846 			mprq_caps.max_single_wqe_log_num_of_strides);
847 		DRV_LOG(DEBUG, "\tsupported_qpts: %d",
848 			mprq_caps.supported_qpts);
849 		DRV_LOG(DEBUG, "device supports Multi-Packet RQ");
850 		mprq = 1;
851 		mprq_min_stride_size_n =
852 			mprq_caps.min_single_stride_log_num_of_bytes;
853 		mprq_max_stride_size_n =
854 			mprq_caps.max_single_stride_log_num_of_bytes;
855 		mprq_min_stride_num_n =
856 			mprq_caps.min_single_wqe_log_num_of_strides;
857 		mprq_max_stride_num_n =
858 			mprq_caps.max_single_wqe_log_num_of_strides;
859 		config.mprq.stride_num_n = RTE_MAX(MLX5_MPRQ_STRIDE_NUM_N,
860 						   mprq_min_stride_num_n);
861 	}
862 #endif
863 	if (RTE_CACHE_LINE_SIZE == 128 &&
864 	    !(dv_attr.flags & MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP))
865 		cqe_comp = 0;
866 	else
867 		cqe_comp = 1;
868 	config.cqe_comp = cqe_comp;
869 #ifdef HAVE_IBV_MLX5_MOD_CQE_128B_PAD
870 	/* Whether device supports 128B Rx CQE padding. */
871 	cqe_pad = RTE_CACHE_LINE_SIZE == 128 &&
872 		  (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_CQE_128B_PAD);
873 #endif
874 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
875 	if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS) {
876 		tunnel_en = ((dv_attr.tunnel_offloads_caps &
877 			      MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_VXLAN) &&
878 			     (dv_attr.tunnel_offloads_caps &
879 			      MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_GRE));
880 	}
881 	DRV_LOG(DEBUG, "tunnel offloading is %ssupported",
882 		tunnel_en ? "" : "not ");
883 #else
884 	DRV_LOG(WARNING,
885 		"tunnel offloading disabled due to old OFED/rdma-core version");
886 #endif
887 	config.tunnel_en = tunnel_en;
888 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
889 	mpls_en = ((dv_attr.tunnel_offloads_caps &
890 		    MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_CW_MPLS_OVER_GRE) &&
891 		   (dv_attr.tunnel_offloads_caps &
892 		    MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_CW_MPLS_OVER_UDP));
893 	DRV_LOG(DEBUG, "MPLS over GRE/UDP tunnel offloading is %ssupported",
894 		mpls_en ? "" : "not ");
895 #else
896 	DRV_LOG(WARNING, "MPLS over GRE/UDP tunnel offloading disabled due to"
897 		" old OFED/rdma-core version or firmware configuration");
898 #endif
899 	config.mpls_en = mpls_en;
900 	err = mlx5_glue->query_device_ex(ctx, NULL, &attr);
901 	if (err) {
902 		DEBUG("ibv_query_device_ex() failed");
903 		goto error;
904 	}
905 	DRV_LOG(DEBUG, "naming Ethernet device \"%s\"", name);
906 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
907 		eth_dev = rte_eth_dev_attach_secondary(name);
908 		if (eth_dev == NULL) {
909 			DRV_LOG(ERR, "can not attach rte ethdev");
910 			rte_errno = ENOMEM;
911 			err = rte_errno;
912 			goto error;
913 		}
914 		eth_dev->device = dpdk_dev;
915 		eth_dev->dev_ops = &mlx5_dev_sec_ops;
916 		err = mlx5_uar_init_secondary(eth_dev);
917 		if (err) {
918 			err = rte_errno;
919 			goto error;
920 		}
921 		/* Receive command fd from primary process */
922 		err = mlx5_socket_connect(eth_dev);
923 		if (err < 0) {
924 			err = rte_errno;
925 			goto error;
926 		}
927 		/* Remap UAR for Tx queues. */
928 		err = mlx5_tx_uar_remap(eth_dev, err);
929 		if (err) {
930 			err = rte_errno;
931 			goto error;
932 		}
933 		/*
934 		 * Ethdev pointer is still required as input since
935 		 * the primary device is not accessible from the
936 		 * secondary process.
937 		 */
938 		eth_dev->rx_pkt_burst = mlx5_select_rx_function(eth_dev);
939 		eth_dev->tx_pkt_burst = mlx5_select_tx_function(eth_dev);
940 		claim_zero(mlx5_glue->close_device(ctx));
941 		return eth_dev;
942 	}
943 	/* Check port status. */
944 	err = mlx5_glue->query_port(ctx, 1, &port_attr);
945 	if (err) {
946 		DRV_LOG(ERR, "port query failed: %s", strerror(err));
947 		goto error;
948 	}
949 	if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
950 		DRV_LOG(ERR, "port is not configured in Ethernet mode");
951 		err = EINVAL;
952 		goto error;
953 	}
954 	if (port_attr.state != IBV_PORT_ACTIVE)
955 		DRV_LOG(DEBUG, "port is not active: \"%s\" (%d)",
956 			mlx5_glue->port_state_str(port_attr.state),
957 			port_attr.state);
958 	/* Allocate protection domain. */
959 	pd = mlx5_glue->alloc_pd(ctx);
960 	if (pd == NULL) {
961 		DRV_LOG(ERR, "PD allocation failure");
962 		err = ENOMEM;
963 		goto error;
964 	}
965 	priv = rte_zmalloc("ethdev private structure",
966 			   sizeof(*priv),
967 			   RTE_CACHE_LINE_SIZE);
968 	if (priv == NULL) {
969 		DRV_LOG(ERR, "priv allocation failure");
970 		err = ENOMEM;
971 		goto error;
972 	}
973 	priv->ctx = ctx;
974 	strncpy(priv->ibdev_name, priv->ctx->device->name,
975 		sizeof(priv->ibdev_name));
976 	strncpy(priv->ibdev_path, priv->ctx->device->ibdev_path,
977 		sizeof(priv->ibdev_path));
978 	priv->device_attr = attr;
979 	priv->pd = pd;
980 	priv->mtu = ETHER_MTU;
981 #ifndef RTE_ARCH_64
982 	/* Initialize UAR access locks for 32bit implementations. */
983 	rte_spinlock_init(&priv->uar_lock_cq);
984 	for (i = 0; i < MLX5_UAR_PAGE_NUM_MAX; i++)
985 		rte_spinlock_init(&priv->uar_lock[i]);
986 #endif
987 	/* Some internal functions rely on Netlink sockets, open them now. */
988 	priv->nl_socket_rdma = mlx5_nl_init(NETLINK_RDMA);
989 	priv->nl_socket_route =	mlx5_nl_init(NETLINK_ROUTE);
990 	priv->nl_sn = 0;
991 	priv->representor = !!switch_info->representor;
992 	priv->domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
993 	priv->representor_id =
994 		switch_info->representor ? switch_info->port_name : -1;
995 	/*
996 	 * Look for sibling devices in order to reuse their switch domain
997 	 * if any, otherwise allocate one.
998 	 */
999 	i = mlx5_dev_to_port_id(dpdk_dev, NULL, 0);
1000 	if (i > 0) {
1001 		uint16_t port_id[i];
1002 
1003 		i = RTE_MIN(mlx5_dev_to_port_id(dpdk_dev, port_id, i), i);
1004 		while (i--) {
1005 			const struct mlx5_priv *opriv =
1006 				rte_eth_devices[port_id[i]].data->dev_private;
1007 
1008 			if (!opriv ||
1009 			    opriv->domain_id ==
1010 			    RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID)
1011 				continue;
1012 			priv->domain_id = opriv->domain_id;
1013 			break;
1014 		}
1015 	}
1016 	if (priv->domain_id == RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
1017 		err = rte_eth_switch_domain_alloc(&priv->domain_id);
1018 		if (err) {
1019 			err = rte_errno;
1020 			DRV_LOG(ERR, "unable to allocate switch domain: %s",
1021 				strerror(rte_errno));
1022 			goto error;
1023 		}
1024 		own_domain_id = 1;
1025 	}
1026 	err = mlx5_args(&config, dpdk_dev->devargs);
1027 	if (err) {
1028 		err = rte_errno;
1029 		DRV_LOG(ERR, "failed to process device arguments: %s",
1030 			strerror(rte_errno));
1031 		goto error;
1032 	}
1033 	config.hw_csum = !!(attr.device_cap_flags_ex & IBV_DEVICE_RAW_IP_CSUM);
1034 	DRV_LOG(DEBUG, "checksum offloading is %ssupported",
1035 		(config.hw_csum ? "" : "not "));
1036 #if !defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) && \
1037 	!defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
1038 	DRV_LOG(DEBUG, "counters are not supported");
1039 #endif
1040 #ifndef HAVE_IBV_FLOW_DV_SUPPORT
1041 	if (config.dv_flow_en) {
1042 		DRV_LOG(WARNING, "DV flow is not supported");
1043 		config.dv_flow_en = 0;
1044 	}
1045 #endif
1046 	config.ind_table_max_size =
1047 		attr.rss_caps.max_rwq_indirection_table_size;
1048 	/*
1049 	 * Remove this check once DPDK supports larger/variable
1050 	 * indirection tables.
1051 	 */
1052 	if (config.ind_table_max_size > (unsigned int)ETH_RSS_RETA_SIZE_512)
1053 		config.ind_table_max_size = ETH_RSS_RETA_SIZE_512;
1054 	DRV_LOG(DEBUG, "maximum Rx indirection table size is %u",
1055 		config.ind_table_max_size);
1056 	config.hw_vlan_strip = !!(attr.raw_packet_caps &
1057 				  IBV_RAW_PACKET_CAP_CVLAN_STRIPPING);
1058 	DRV_LOG(DEBUG, "VLAN stripping is %ssupported",
1059 		(config.hw_vlan_strip ? "" : "not "));
1060 	config.hw_fcs_strip = !!(attr.raw_packet_caps &
1061 				 IBV_RAW_PACKET_CAP_SCATTER_FCS);
1062 	DRV_LOG(DEBUG, "FCS stripping configuration is %ssupported",
1063 		(config.hw_fcs_strip ? "" : "not "));
1064 #if defined(HAVE_IBV_WQ_FLAG_RX_END_PADDING)
1065 	hw_padding = !!attr.rx_pad_end_addr_align;
1066 #elif defined(HAVE_IBV_WQ_FLAGS_PCI_WRITE_END_PADDING)
1067 	hw_padding = !!(attr.device_cap_flags_ex &
1068 			IBV_DEVICE_PCI_WRITE_END_PADDING);
1069 #endif
1070 	if (config.hw_padding && !hw_padding) {
1071 		DRV_LOG(DEBUG, "Rx end alignment padding isn't supported");
1072 		config.hw_padding = 0;
1073 	} else if (config.hw_padding) {
1074 		DRV_LOG(DEBUG, "Rx end alignment padding is enabled");
1075 	}
1076 	config.tso = (attr.tso_caps.max_tso > 0 &&
1077 		      (attr.tso_caps.supported_qpts &
1078 		       (1 << IBV_QPT_RAW_PACKET)));
1079 	if (config.tso)
1080 		config.tso_max_payload_sz = attr.tso_caps.max_tso;
1081 	/*
1082 	 * MPW is disabled by default, while the Enhanced MPW is enabled
1083 	 * by default.
1084 	 */
1085 	if (config.mps == MLX5_ARG_UNSET)
1086 		config.mps = (mps == MLX5_MPW_ENHANCED) ? MLX5_MPW_ENHANCED :
1087 							  MLX5_MPW_DISABLED;
1088 	else
1089 		config.mps = config.mps ? mps : MLX5_MPW_DISABLED;
1090 	DRV_LOG(INFO, "%sMPS is %s",
1091 		config.mps == MLX5_MPW_ENHANCED ? "enhanced " : "",
1092 		config.mps != MLX5_MPW_DISABLED ? "enabled" : "disabled");
1093 	if (config.cqe_comp && !cqe_comp) {
1094 		DRV_LOG(WARNING, "Rx CQE compression isn't supported");
1095 		config.cqe_comp = 0;
1096 	}
1097 	if (config.cqe_pad && !cqe_pad) {
1098 		DRV_LOG(WARNING, "Rx CQE padding isn't supported");
1099 		config.cqe_pad = 0;
1100 	} else if (config.cqe_pad) {
1101 		DRV_LOG(INFO, "Rx CQE padding is enabled");
1102 	}
1103 	if (config.mprq.enabled && mprq) {
1104 		if (config.mprq.stride_num_n > mprq_max_stride_num_n ||
1105 		    config.mprq.stride_num_n < mprq_min_stride_num_n) {
1106 			config.mprq.stride_num_n =
1107 				RTE_MAX(MLX5_MPRQ_STRIDE_NUM_N,
1108 					mprq_min_stride_num_n);
1109 			DRV_LOG(WARNING,
1110 				"the number of strides"
1111 				" for Multi-Packet RQ is out of range,"
1112 				" setting default value (%u)",
1113 				1 << config.mprq.stride_num_n);
1114 		}
1115 		config.mprq.min_stride_size_n = mprq_min_stride_size_n;
1116 		config.mprq.max_stride_size_n = mprq_max_stride_size_n;
1117 	} else if (config.mprq.enabled && !mprq) {
1118 		DRV_LOG(WARNING, "Multi-Packet RQ isn't supported");
1119 		config.mprq.enabled = 0;
1120 	}
1121 	eth_dev = rte_eth_dev_allocate(name);
1122 	if (eth_dev == NULL) {
1123 		DRV_LOG(ERR, "can not allocate rte ethdev");
1124 		err = ENOMEM;
1125 		goto error;
1126 	}
1127 	/* Flag to call rte_eth_dev_release_port() in rte_eth_dev_close(). */
1128 	eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
1129 	if (priv->representor) {
1130 		eth_dev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR;
1131 		eth_dev->data->representor_id = priv->representor_id;
1132 	}
1133 	eth_dev->data->dev_private = priv;
1134 	priv->dev_data = eth_dev->data;
1135 	eth_dev->data->mac_addrs = priv->mac;
1136 	eth_dev->device = dpdk_dev;
1137 	err = mlx5_uar_init_primary(eth_dev);
1138 	if (err) {
1139 		err = rte_errno;
1140 		goto error;
1141 	}
1142 	/* Configure the first MAC address by default. */
1143 	if (mlx5_get_mac(eth_dev, &mac.addr_bytes)) {
1144 		DRV_LOG(ERR,
1145 			"port %u cannot get MAC address, is mlx5_en"
1146 			" loaded? (errno: %s)",
1147 			eth_dev->data->port_id, strerror(rte_errno));
1148 		err = ENODEV;
1149 		goto error;
1150 	}
1151 	DRV_LOG(INFO,
1152 		"port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
1153 		eth_dev->data->port_id,
1154 		mac.addr_bytes[0], mac.addr_bytes[1],
1155 		mac.addr_bytes[2], mac.addr_bytes[3],
1156 		mac.addr_bytes[4], mac.addr_bytes[5]);
1157 #ifndef NDEBUG
1158 	{
1159 		char ifname[IF_NAMESIZE];
1160 
1161 		if (mlx5_get_ifname(eth_dev, &ifname) == 0)
1162 			DRV_LOG(DEBUG, "port %u ifname is \"%s\"",
1163 				eth_dev->data->port_id, ifname);
1164 		else
1165 			DRV_LOG(DEBUG, "port %u ifname is unknown",
1166 				eth_dev->data->port_id);
1167 	}
1168 #endif
1169 	/* Get actual MTU if possible. */
1170 	err = mlx5_get_mtu(eth_dev, &priv->mtu);
1171 	if (err) {
1172 		err = rte_errno;
1173 		goto error;
1174 	}
1175 	DRV_LOG(DEBUG, "port %u MTU is %u", eth_dev->data->port_id,
1176 		priv->mtu);
1177 	/* Initialize burst functions to prevent crashes before link-up. */
1178 	eth_dev->rx_pkt_burst = removed_rx_burst;
1179 	eth_dev->tx_pkt_burst = removed_tx_burst;
1180 	eth_dev->dev_ops = &mlx5_dev_ops;
1181 	/* Register MAC address. */
1182 	claim_zero(mlx5_mac_addr_add(eth_dev, &mac, 0, 0));
1183 	if (config.vf && config.vf_nl_en)
1184 		mlx5_nl_mac_addr_sync(eth_dev);
1185 	priv->tcf_context = mlx5_flow_tcf_context_create();
1186 	if (!priv->tcf_context) {
1187 		err = -rte_errno;
1188 		DRV_LOG(WARNING,
1189 			"flow rules relying on switch offloads will not be"
1190 			" supported: cannot open libmnl socket: %s",
1191 			strerror(rte_errno));
1192 	} else {
1193 		struct rte_flow_error error;
1194 		unsigned int ifindex = mlx5_ifindex(eth_dev);
1195 
1196 		if (!ifindex) {
1197 			err = -rte_errno;
1198 			error.message =
1199 				"cannot retrieve network interface index";
1200 		} else {
1201 			err = mlx5_flow_tcf_init(priv->tcf_context,
1202 						 ifindex, &error);
1203 		}
1204 		if (err) {
1205 			DRV_LOG(WARNING,
1206 				"flow rules relying on switch offloads will"
1207 				" not be supported: %s: %s",
1208 				error.message, strerror(rte_errno));
1209 			mlx5_flow_tcf_context_destroy(priv->tcf_context);
1210 			priv->tcf_context = NULL;
1211 		}
1212 	}
1213 	TAILQ_INIT(&priv->flows);
1214 	TAILQ_INIT(&priv->ctrl_flows);
1215 	/* Hint libmlx5 to use PMD allocator for data plane resources */
1216 	struct mlx5dv_ctx_allocators alctr = {
1217 		.alloc = &mlx5_alloc_verbs_buf,
1218 		.free = &mlx5_free_verbs_buf,
1219 		.data = priv,
1220 	};
1221 	mlx5_glue->dv_set_context_attr(ctx, MLX5DV_CTX_ATTR_BUF_ALLOCATORS,
1222 				       (void *)((uintptr_t)&alctr));
1223 	/* Bring Ethernet device up. */
1224 	DRV_LOG(DEBUG, "port %u forcing Ethernet interface up",
1225 		eth_dev->data->port_id);
1226 	mlx5_set_link_up(eth_dev);
1227 	/*
1228 	 * Even though the interrupt handler is not installed yet,
1229 	 * interrupts will still trigger on the asyn_fd from
1230 	 * Verbs context returned by ibv_open_device().
1231 	 */
1232 	mlx5_link_update(eth_dev, 0);
1233 	/* Store device configuration on private structure. */
1234 	priv->config = config;
1235 	/* Supported Verbs flow priority number detection. */
1236 	err = mlx5_flow_discover_priorities(eth_dev);
1237 	if (err < 0) {
1238 		err = -err;
1239 		goto error;
1240 	}
1241 	priv->config.flow_prio = err;
1242 	/*
1243 	 * Once the device is added to the list of memory event
1244 	 * callback, its global MR cache table cannot be expanded
1245 	 * on the fly because of deadlock. If it overflows, lookup
1246 	 * should be done by searching MR list linearly, which is slow.
1247 	 */
1248 	err = mlx5_mr_btree_init(&priv->mr.cache,
1249 				 MLX5_MR_BTREE_CACHE_N * 2,
1250 				 eth_dev->device->numa_node);
1251 	if (err) {
1252 		err = rte_errno;
1253 		goto error;
1254 	}
1255 	/* Add device to memory callback list. */
1256 	rte_rwlock_write_lock(&mlx5_shared_data->mem_event_rwlock);
1257 	LIST_INSERT_HEAD(&mlx5_shared_data->mem_event_cb_list,
1258 			 priv, mem_event_cb);
1259 	rte_rwlock_write_unlock(&mlx5_shared_data->mem_event_rwlock);
1260 	return eth_dev;
1261 error:
1262 	if (priv) {
1263 		if (priv->nl_socket_route >= 0)
1264 			close(priv->nl_socket_route);
1265 		if (priv->nl_socket_rdma >= 0)
1266 			close(priv->nl_socket_rdma);
1267 		if (priv->tcf_context)
1268 			mlx5_flow_tcf_context_destroy(priv->tcf_context);
1269 		if (own_domain_id)
1270 			claim_zero(rte_eth_switch_domain_free(priv->domain_id));
1271 		rte_free(priv);
1272 		if (eth_dev != NULL)
1273 			eth_dev->data->dev_private = NULL;
1274 	}
1275 	if (pd)
1276 		claim_zero(mlx5_glue->dealloc_pd(pd));
1277 	if (eth_dev != NULL) {
1278 		/* mac_addrs must not be freed alone because part of dev_private */
1279 		eth_dev->data->mac_addrs = NULL;
1280 		rte_eth_dev_release_port(eth_dev);
1281 	}
1282 	if (ctx)
1283 		claim_zero(mlx5_glue->close_device(ctx));
1284 	assert(err > 0);
1285 	rte_errno = err;
1286 	return NULL;
1287 }
1288 
1289 /** Data associated with devices to spawn. */
1290 struct mlx5_dev_spawn_data {
1291 	unsigned int ifindex; /**< Network interface index. */
1292 	struct mlx5_switch_info info; /**< Switch information. */
1293 	struct ibv_device *ibv_dev; /**< Associated IB device. */
1294 	struct rte_eth_dev *eth_dev; /**< Associated Ethernet device. */
1295 };
1296 
1297 /**
1298  * Comparison callback to sort device data.
1299  *
1300  * This is meant to be used with qsort().
1301  *
1302  * @param a[in]
1303  *   Pointer to pointer to first data object.
1304  * @param b[in]
1305  *   Pointer to pointer to second data object.
1306  *
1307  * @return
1308  *   0 if both objects are equal, less than 0 if the first argument is less
1309  *   than the second, greater than 0 otherwise.
1310  */
1311 static int
1312 mlx5_dev_spawn_data_cmp(const void *a, const void *b)
1313 {
1314 	const struct mlx5_switch_info *si_a =
1315 		&((const struct mlx5_dev_spawn_data *)a)->info;
1316 	const struct mlx5_switch_info *si_b =
1317 		&((const struct mlx5_dev_spawn_data *)b)->info;
1318 	int ret;
1319 
1320 	/* Master device first. */
1321 	ret = si_b->master - si_a->master;
1322 	if (ret)
1323 		return ret;
1324 	/* Then representor devices. */
1325 	ret = si_b->representor - si_a->representor;
1326 	if (ret)
1327 		return ret;
1328 	/* Unidentified devices come last in no specific order. */
1329 	if (!si_a->representor)
1330 		return 0;
1331 	/* Order representors by name. */
1332 	return si_a->port_name - si_b->port_name;
1333 }
1334 
1335 /**
1336  * DPDK callback to register a PCI device.
1337  *
1338  * This function spawns Ethernet devices out of a given PCI device.
1339  *
1340  * @param[in] pci_drv
1341  *   PCI driver structure (mlx5_driver).
1342  * @param[in] pci_dev
1343  *   PCI device information.
1344  *
1345  * @return
1346  *   0 on success, a negative errno value otherwise and rte_errno is set.
1347  */
1348 static int
1349 mlx5_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1350 	       struct rte_pci_device *pci_dev)
1351 {
1352 	struct ibv_device **ibv_list;
1353 	unsigned int n = 0;
1354 	struct mlx5_dev_config dev_config;
1355 	int ret;
1356 
1357 	assert(pci_drv == &mlx5_driver);
1358 	errno = 0;
1359 	ibv_list = mlx5_glue->get_device_list(&ret);
1360 	if (!ibv_list) {
1361 		rte_errno = errno ? errno : ENOSYS;
1362 		DRV_LOG(ERR, "cannot list devices, is ib_uverbs loaded?");
1363 		return -rte_errno;
1364 	}
1365 
1366 	struct ibv_device *ibv_match[ret + 1];
1367 
1368 	while (ret-- > 0) {
1369 		struct rte_pci_addr pci_addr;
1370 
1371 		DRV_LOG(DEBUG, "checking device \"%s\"", ibv_list[ret]->name);
1372 		if (mlx5_ibv_device_to_pci_addr(ibv_list[ret], &pci_addr))
1373 			continue;
1374 		if (pci_dev->addr.domain != pci_addr.domain ||
1375 		    pci_dev->addr.bus != pci_addr.bus ||
1376 		    pci_dev->addr.devid != pci_addr.devid ||
1377 		    pci_dev->addr.function != pci_addr.function)
1378 			continue;
1379 		DRV_LOG(INFO, "PCI information matches for device \"%s\"",
1380 			ibv_list[ret]->name);
1381 		ibv_match[n++] = ibv_list[ret];
1382 	}
1383 	ibv_match[n] = NULL;
1384 
1385 	struct mlx5_dev_spawn_data list[n];
1386 	int nl_route = n ? mlx5_nl_init(NETLINK_ROUTE) : -1;
1387 	int nl_rdma = n ? mlx5_nl_init(NETLINK_RDMA) : -1;
1388 	unsigned int i;
1389 	unsigned int u;
1390 
1391 	/*
1392 	 * The existence of several matching entries (n > 1) means port
1393 	 * representors have been instantiated. No existing Verbs call nor
1394 	 * /sys entries can tell them apart, this can only be done through
1395 	 * Netlink calls assuming kernel drivers are recent enough to
1396 	 * support them.
1397 	 *
1398 	 * In the event of identification failure through Netlink, try again
1399 	 * through sysfs, then either:
1400 	 *
1401 	 * 1. No device matches (n == 0), complain and bail out.
1402 	 * 2. A single IB device matches (n == 1) and is not a representor,
1403 	 *    assume no switch support.
1404 	 * 3. Otherwise no safe assumptions can be made; complain louder and
1405 	 *    bail out.
1406 	 */
1407 	for (i = 0; i != n; ++i) {
1408 		list[i].ibv_dev = ibv_match[i];
1409 		list[i].eth_dev = NULL;
1410 		if (nl_rdma < 0)
1411 			list[i].ifindex = 0;
1412 		else
1413 			list[i].ifindex = mlx5_nl_ifindex
1414 				(nl_rdma, list[i].ibv_dev->name);
1415 		if (nl_route < 0 ||
1416 		    !list[i].ifindex ||
1417 		    mlx5_nl_switch_info(nl_route, list[i].ifindex,
1418 					&list[i].info) ||
1419 		    ((!list[i].info.representor && !list[i].info.master) &&
1420 		     mlx5_sysfs_switch_info(list[i].ifindex, &list[i].info))) {
1421 			list[i].ifindex = 0;
1422 			memset(&list[i].info, 0, sizeof(list[i].info));
1423 			continue;
1424 		}
1425 	}
1426 	if (nl_rdma >= 0)
1427 		close(nl_rdma);
1428 	if (nl_route >= 0)
1429 		close(nl_route);
1430 	/* Count unidentified devices. */
1431 	for (u = 0, i = 0; i != n; ++i)
1432 		if (!list[i].info.master && !list[i].info.representor)
1433 			++u;
1434 	if (u) {
1435 		if (n == 1 && u == 1) {
1436 			/* Case #2. */
1437 			DRV_LOG(INFO, "no switch support detected");
1438 		} else {
1439 			/* Case #3. */
1440 			DRV_LOG(ERR,
1441 				"unable to tell which of the matching devices"
1442 				" is the master (lack of kernel support?)");
1443 			n = 0;
1444 		}
1445 	}
1446 	/*
1447 	 * Sort list to probe devices in natural order for users convenience
1448 	 * (i.e. master first, then representors from lowest to highest ID).
1449 	 */
1450 	if (n)
1451 		qsort(list, n, sizeof(*list), mlx5_dev_spawn_data_cmp);
1452 	/* Default configuration. */
1453 	dev_config = (struct mlx5_dev_config){
1454 		.hw_padding = 0,
1455 		.mps = MLX5_ARG_UNSET,
1456 		.tx_vec_en = 1,
1457 		.rx_vec_en = 1,
1458 		.txq_inline = MLX5_ARG_UNSET,
1459 		.txqs_inline = MLX5_ARG_UNSET,
1460 		.txqs_vec = MLX5_ARG_UNSET,
1461 		.inline_max_packet_sz = MLX5_ARG_UNSET,
1462 		.vf_nl_en = 1,
1463 		.mprq = {
1464 			.enabled = 0, /* Disabled by default. */
1465 			.stride_num_n = MLX5_MPRQ_STRIDE_NUM_N,
1466 			.max_memcpy_len = MLX5_MPRQ_MEMCPY_DEFAULT_LEN,
1467 			.min_rxqs_num = MLX5_MPRQ_MIN_RXQS,
1468 		},
1469 	};
1470 	/* Device speicific configuration. */
1471 	switch (pci_dev->id.device_id) {
1472 	case PCI_DEVICE_ID_MELLANOX_CONNECTX5BF:
1473 		dev_config.txqs_vec = MLX5_VPMD_MAX_TXQS_BLUEFIELD;
1474 		break;
1475 	case PCI_DEVICE_ID_MELLANOX_CONNECTX4VF:
1476 	case PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF:
1477 	case PCI_DEVICE_ID_MELLANOX_CONNECTX5VF:
1478 	case PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF:
1479 		dev_config.vf = 1;
1480 		break;
1481 	default:
1482 		break;
1483 	}
1484 	/* Set architecture-dependent default value if unset. */
1485 	if (dev_config.txqs_vec == MLX5_ARG_UNSET)
1486 		dev_config.txqs_vec = MLX5_VPMD_MAX_TXQS;
1487 	for (i = 0; i != n; ++i) {
1488 		uint32_t restore;
1489 
1490 		list[i].eth_dev = mlx5_dev_spawn(&pci_dev->device,
1491 						 list[i].ibv_dev, dev_config,
1492 						 &list[i].info);
1493 		if (!list[i].eth_dev) {
1494 			if (rte_errno != EBUSY && rte_errno != EEXIST)
1495 				break;
1496 			/* Device is disabled or already spawned. Ignore it. */
1497 			continue;
1498 		}
1499 		restore = list[i].eth_dev->data->dev_flags;
1500 		rte_eth_copy_pci_info(list[i].eth_dev, pci_dev);
1501 		/* Restore non-PCI flags cleared by the above call. */
1502 		list[i].eth_dev->data->dev_flags |= restore;
1503 		rte_eth_dev_probing_finish(list[i].eth_dev);
1504 	}
1505 	mlx5_glue->free_device_list(ibv_list);
1506 	if (!n) {
1507 		DRV_LOG(WARNING,
1508 			"no Verbs device matches PCI device " PCI_PRI_FMT ","
1509 			" are kernel drivers loaded?",
1510 			pci_dev->addr.domain, pci_dev->addr.bus,
1511 			pci_dev->addr.devid, pci_dev->addr.function);
1512 		rte_errno = ENOENT;
1513 		ret = -rte_errno;
1514 	} else if (i != n) {
1515 		DRV_LOG(ERR,
1516 			"probe of PCI device " PCI_PRI_FMT " aborted after"
1517 			" encountering an error: %s",
1518 			pci_dev->addr.domain, pci_dev->addr.bus,
1519 			pci_dev->addr.devid, pci_dev->addr.function,
1520 			strerror(rte_errno));
1521 		ret = -rte_errno;
1522 		/* Roll back. */
1523 		while (i--) {
1524 			if (!list[i].eth_dev)
1525 				continue;
1526 			mlx5_dev_close(list[i].eth_dev);
1527 			/* mac_addrs must not be freed because in dev_private */
1528 			list[i].eth_dev->data->mac_addrs = NULL;
1529 			claim_zero(rte_eth_dev_release_port(list[i].eth_dev));
1530 		}
1531 		/* Restore original error. */
1532 		rte_errno = -ret;
1533 	} else {
1534 		ret = 0;
1535 	}
1536 	return ret;
1537 }
1538 
1539 /**
1540  * DPDK callback to remove a PCI device.
1541  *
1542  * This function removes all Ethernet devices belong to a given PCI device.
1543  *
1544  * @param[in] pci_dev
1545  *   Pointer to the PCI device.
1546  *
1547  * @return
1548  *   0 on success, the function cannot fail.
1549  */
1550 static int
1551 mlx5_pci_remove(struct rte_pci_device *pci_dev)
1552 {
1553 	uint16_t port_id;
1554 	struct rte_eth_dev *port;
1555 
1556 	for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
1557 		port = &rte_eth_devices[port_id];
1558 		if (port->state != RTE_ETH_DEV_UNUSED &&
1559 				port->device == &pci_dev->device)
1560 			rte_eth_dev_close(port_id);
1561 	}
1562 	return 0;
1563 }
1564 
1565 static const struct rte_pci_id mlx5_pci_id_map[] = {
1566 	{
1567 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1568 			       PCI_DEVICE_ID_MELLANOX_CONNECTX4)
1569 	},
1570 	{
1571 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1572 			       PCI_DEVICE_ID_MELLANOX_CONNECTX4VF)
1573 	},
1574 	{
1575 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1576 			       PCI_DEVICE_ID_MELLANOX_CONNECTX4LX)
1577 	},
1578 	{
1579 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1580 			       PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF)
1581 	},
1582 	{
1583 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1584 			       PCI_DEVICE_ID_MELLANOX_CONNECTX5)
1585 	},
1586 	{
1587 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1588 			       PCI_DEVICE_ID_MELLANOX_CONNECTX5VF)
1589 	},
1590 	{
1591 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1592 			       PCI_DEVICE_ID_MELLANOX_CONNECTX5EX)
1593 	},
1594 	{
1595 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1596 			       PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF)
1597 	},
1598 	{
1599 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1600 			       PCI_DEVICE_ID_MELLANOX_CONNECTX5BF)
1601 	},
1602 	{
1603 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1604 			       PCI_DEVICE_ID_MELLANOX_CONNECTX5BFVF)
1605 	},
1606 	{
1607 		.vendor_id = 0
1608 	}
1609 };
1610 
1611 static struct rte_pci_driver mlx5_driver = {
1612 	.driver = {
1613 		.name = MLX5_DRIVER_NAME
1614 	},
1615 	.id_table = mlx5_pci_id_map,
1616 	.probe = mlx5_pci_probe,
1617 	.remove = mlx5_pci_remove,
1618 	.drv_flags = (RTE_PCI_DRV_INTR_LSC | RTE_PCI_DRV_INTR_RMV |
1619 		      RTE_PCI_DRV_PROBE_AGAIN),
1620 };
1621 
1622 #ifdef RTE_LIBRTE_MLX5_DLOPEN_DEPS
1623 
1624 /**
1625  * Suffix RTE_EAL_PMD_PATH with "-glue".
1626  *
1627  * This function performs a sanity check on RTE_EAL_PMD_PATH before
1628  * suffixing its last component.
1629  *
1630  * @param buf[out]
1631  *   Output buffer, should be large enough otherwise NULL is returned.
1632  * @param size
1633  *   Size of @p out.
1634  *
1635  * @return
1636  *   Pointer to @p buf or @p NULL in case suffix cannot be appended.
1637  */
1638 static char *
1639 mlx5_glue_path(char *buf, size_t size)
1640 {
1641 	static const char *const bad[] = { "/", ".", "..", NULL };
1642 	const char *path = RTE_EAL_PMD_PATH;
1643 	size_t len = strlen(path);
1644 	size_t off;
1645 	int i;
1646 
1647 	while (len && path[len - 1] == '/')
1648 		--len;
1649 	for (off = len; off && path[off - 1] != '/'; --off)
1650 		;
1651 	for (i = 0; bad[i]; ++i)
1652 		if (!strncmp(path + off, bad[i], (int)(len - off)))
1653 			goto error;
1654 	i = snprintf(buf, size, "%.*s-glue", (int)len, path);
1655 	if (i == -1 || (size_t)i >= size)
1656 		goto error;
1657 	return buf;
1658 error:
1659 	DRV_LOG(ERR,
1660 		"unable to append \"-glue\" to last component of"
1661 		" RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"),"
1662 		" please re-configure DPDK");
1663 	return NULL;
1664 }
1665 
1666 /**
1667  * Initialization routine for run-time dependency on rdma-core.
1668  */
1669 static int
1670 mlx5_glue_init(void)
1671 {
1672 	char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
1673 	const char *path[] = {
1674 		/*
1675 		 * A basic security check is necessary before trusting
1676 		 * MLX5_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
1677 		 */
1678 		(geteuid() == getuid() && getegid() == getgid() ?
1679 		 getenv("MLX5_GLUE_PATH") : NULL),
1680 		/*
1681 		 * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
1682 		 * variant, otherwise let dlopen() look up libraries on its
1683 		 * own.
1684 		 */
1685 		(*RTE_EAL_PMD_PATH ?
1686 		 mlx5_glue_path(glue_path, sizeof(glue_path)) : ""),
1687 	};
1688 	unsigned int i = 0;
1689 	void *handle = NULL;
1690 	void **sym;
1691 	const char *dlmsg;
1692 
1693 	while (!handle && i != RTE_DIM(path)) {
1694 		const char *end;
1695 		size_t len;
1696 		int ret;
1697 
1698 		if (!path[i]) {
1699 			++i;
1700 			continue;
1701 		}
1702 		end = strpbrk(path[i], ":;");
1703 		if (!end)
1704 			end = path[i] + strlen(path[i]);
1705 		len = end - path[i];
1706 		ret = 0;
1707 		do {
1708 			char name[ret + 1];
1709 
1710 			ret = snprintf(name, sizeof(name), "%.*s%s" MLX5_GLUE,
1711 				       (int)len, path[i],
1712 				       (!len || *(end - 1) == '/') ? "" : "/");
1713 			if (ret == -1)
1714 				break;
1715 			if (sizeof(name) != (size_t)ret + 1)
1716 				continue;
1717 			DRV_LOG(DEBUG, "looking for rdma-core glue as \"%s\"",
1718 				name);
1719 			handle = dlopen(name, RTLD_LAZY);
1720 			break;
1721 		} while (1);
1722 		path[i] = end + 1;
1723 		if (!*end)
1724 			++i;
1725 	}
1726 	if (!handle) {
1727 		rte_errno = EINVAL;
1728 		dlmsg = dlerror();
1729 		if (dlmsg)
1730 			DRV_LOG(WARNING, "cannot load glue library: %s", dlmsg);
1731 		goto glue_error;
1732 	}
1733 	sym = dlsym(handle, "mlx5_glue");
1734 	if (!sym || !*sym) {
1735 		rte_errno = EINVAL;
1736 		dlmsg = dlerror();
1737 		if (dlmsg)
1738 			DRV_LOG(ERR, "cannot resolve glue symbol: %s", dlmsg);
1739 		goto glue_error;
1740 	}
1741 	mlx5_glue = *sym;
1742 	return 0;
1743 glue_error:
1744 	if (handle)
1745 		dlclose(handle);
1746 	DRV_LOG(WARNING,
1747 		"cannot initialize PMD due to missing run-time dependency on"
1748 		" rdma-core libraries (libibverbs, libmlx5)");
1749 	return -rte_errno;
1750 }
1751 
1752 #endif
1753 
1754 /**
1755  * Driver initialization routine.
1756  */
1757 RTE_INIT(rte_mlx5_pmd_init)
1758 {
1759 	/* Initialize driver log type. */
1760 	mlx5_logtype = rte_log_register("pmd.net.mlx5");
1761 	if (mlx5_logtype >= 0)
1762 		rte_log_set_level(mlx5_logtype, RTE_LOG_NOTICE);
1763 
1764 	/* Build the static tables for Verbs conversion. */
1765 	mlx5_set_ptype_table();
1766 	mlx5_set_cksum_table();
1767 	mlx5_set_swp_types_table();
1768 	/*
1769 	 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
1770 	 * huge pages. Calling ibv_fork_init() during init allows
1771 	 * applications to use fork() safely for purposes other than
1772 	 * using this PMD, which is not supported in forked processes.
1773 	 */
1774 	setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
1775 	/* Match the size of Rx completion entry to the size of a cacheline. */
1776 	if (RTE_CACHE_LINE_SIZE == 128)
1777 		setenv("MLX5_CQE_SIZE", "128", 0);
1778 	/*
1779 	 * MLX5_DEVICE_FATAL_CLEANUP tells ibv_destroy functions to
1780 	 * cleanup all the Verbs resources even when the device was removed.
1781 	 */
1782 	setenv("MLX5_DEVICE_FATAL_CLEANUP", "1", 1);
1783 #ifdef RTE_LIBRTE_MLX5_DLOPEN_DEPS
1784 	if (mlx5_glue_init())
1785 		return;
1786 	assert(mlx5_glue);
1787 #endif
1788 #ifndef NDEBUG
1789 	/* Glue structure must not contain any NULL pointers. */
1790 	{
1791 		unsigned int i;
1792 
1793 		for (i = 0; i != sizeof(*mlx5_glue) / sizeof(void *); ++i)
1794 			assert(((const void *const *)mlx5_glue)[i]);
1795 	}
1796 #endif
1797 	if (strcmp(mlx5_glue->version, MLX5_GLUE_VERSION)) {
1798 		DRV_LOG(ERR,
1799 			"rdma-core glue \"%s\" mismatch: \"%s\" is required",
1800 			mlx5_glue->version, MLX5_GLUE_VERSION);
1801 		return;
1802 	}
1803 	mlx5_glue->fork_init();
1804 	rte_pci_register(&mlx5_driver);
1805 }
1806 
1807 RTE_PMD_EXPORT_NAME(net_mlx5, __COUNTER__);
1808 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5, mlx5_pci_id_map);
1809 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5, "* ib_uverbs & mlx5_core & mlx5_ib");
1810