xref: /f-stack/dpdk/drivers/net/mlx5/mlx5.c (revision 819aafb6)
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 		rte_errno = EINVAL;
579 		return -rte_errno;
580 	}
581 	/* Process parameters. */
582 	for (i = 0; (params[i] != NULL); ++i) {
583 		if (rte_kvargs_count(kvlist, params[i])) {
584 			ret = rte_kvargs_process(kvlist, params[i],
585 						 mlx5_args_check, config);
586 			if (ret) {
587 				rte_errno = EINVAL;
588 				rte_kvargs_free(kvlist);
589 				return -rte_errno;
590 			}
591 		}
592 	}
593 	rte_kvargs_free(kvlist);
594 	return 0;
595 }
596 
597 static struct rte_pci_driver mlx5_driver;
598 
599 /*
600  * Reserved UAR address space for TXQ UAR(hw doorbell) mapping, process
601  * local resource used by both primary and secondary to avoid duplicate
602  * reservation.
603  * The space has to be available on both primary and secondary process,
604  * TXQ UAR maps to this area using fixed mmap w/o double check.
605  */
606 static void *uar_base;
607 
608 static int
609 find_lower_va_bound(const struct rte_memseg_list *msl,
610 		const struct rte_memseg *ms, void *arg)
611 {
612 	void **addr = arg;
613 
614 	if (msl->external)
615 		return 0;
616 	if (*addr == NULL)
617 		*addr = ms->addr;
618 	else
619 		*addr = RTE_MIN(*addr, ms->addr);
620 
621 	return 0;
622 }
623 
624 /**
625  * Reserve UAR address space for primary process.
626  *
627  * @param[in] dev
628  *   Pointer to Ethernet device.
629  *
630  * @return
631  *   0 on success, a negative errno value otherwise and rte_errno is set.
632  */
633 static int
634 mlx5_uar_init_primary(struct rte_eth_dev *dev)
635 {
636 	struct mlx5_priv *priv = dev->data->dev_private;
637 	void *addr = (void *)0;
638 
639 	if (uar_base) { /* UAR address space mapped. */
640 		priv->uar_base = uar_base;
641 		return 0;
642 	}
643 	/* find out lower bound of hugepage segments */
644 	rte_memseg_walk(find_lower_va_bound, &addr);
645 
646 	/* keep distance to hugepages to minimize potential conflicts. */
647 	addr = RTE_PTR_SUB(addr, (uintptr_t)(MLX5_UAR_OFFSET + MLX5_UAR_SIZE));
648 	/* anonymous mmap, no real memory consumption. */
649 	addr = mmap(addr, MLX5_UAR_SIZE,
650 		    PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
651 	if (addr == MAP_FAILED) {
652 		DRV_LOG(ERR,
653 			"port %u failed to reserve UAR address space, please"
654 			" adjust MLX5_UAR_SIZE or try --base-virtaddr",
655 			dev->data->port_id);
656 		rte_errno = ENOMEM;
657 		return -rte_errno;
658 	}
659 	/* Accept either same addr or a new addr returned from mmap if target
660 	 * range occupied.
661 	 */
662 	DRV_LOG(INFO, "port %u reserved UAR address space: %p",
663 		dev->data->port_id, addr);
664 	priv->uar_base = addr; /* for primary and secondary UAR re-mmap. */
665 	uar_base = addr; /* process local, don't reserve again. */
666 	return 0;
667 }
668 
669 /**
670  * Reserve UAR address space for secondary process, align with
671  * primary process.
672  *
673  * @param[in] dev
674  *   Pointer to Ethernet device.
675  *
676  * @return
677  *   0 on success, a negative errno value otherwise and rte_errno is set.
678  */
679 static int
680 mlx5_uar_init_secondary(struct rte_eth_dev *dev)
681 {
682 	struct mlx5_priv *priv = dev->data->dev_private;
683 	void *addr;
684 
685 	assert(priv->uar_base);
686 	if (uar_base) { /* already reserved. */
687 		assert(uar_base == priv->uar_base);
688 		return 0;
689 	}
690 	/* anonymous mmap, no real memory consumption. */
691 	addr = mmap(priv->uar_base, MLX5_UAR_SIZE,
692 		    PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
693 	if (addr == MAP_FAILED) {
694 		DRV_LOG(ERR, "port %u UAR mmap failed: %p size: %llu",
695 			dev->data->port_id, priv->uar_base, MLX5_UAR_SIZE);
696 		rte_errno = ENXIO;
697 		return -rte_errno;
698 	}
699 	if (priv->uar_base != addr) {
700 		DRV_LOG(ERR,
701 			"port %u UAR address %p size %llu occupied, please"
702 			" adjust MLX5_UAR_OFFSET or try EAL parameter"
703 			" --base-virtaddr",
704 			dev->data->port_id, priv->uar_base, MLX5_UAR_SIZE);
705 		rte_errno = ENXIO;
706 		return -rte_errno;
707 	}
708 	uar_base = addr; /* process local, don't reserve again */
709 	DRV_LOG(INFO, "port %u reserved UAR address space: %p",
710 		dev->data->port_id, addr);
711 	return 0;
712 }
713 
714 /**
715  * Spawn an Ethernet device from Verbs information.
716  *
717  * @param dpdk_dev
718  *   Backing DPDK device.
719  * @param ibv_dev
720  *   Verbs device.
721  * @param config
722  *   Device configuration parameters.
723  * @param[in] switch_info
724  *   Switch properties of Ethernet device.
725  *
726  * @return
727  *   A valid Ethernet device object on success, NULL otherwise and rte_errno
728  *   is set. The following errors are defined:
729  *
730  *   EBUSY: device is not supposed to be spawned.
731  *   EEXIST: device is already spawned
732  */
733 static struct rte_eth_dev *
734 mlx5_dev_spawn(struct rte_device *dpdk_dev,
735 	       struct ibv_device *ibv_dev,
736 	       struct mlx5_dev_config config,
737 	       const struct mlx5_switch_info *switch_info)
738 {
739 	struct ibv_context *ctx;
740 	struct ibv_device_attr_ex attr;
741 	struct ibv_port_attr port_attr;
742 	struct ibv_pd *pd = NULL;
743 	struct mlx5dv_context dv_attr = { .comp_mask = 0 };
744 	struct rte_eth_dev *eth_dev = NULL;
745 	struct mlx5_priv *priv = NULL;
746 	int err = 0;
747 	unsigned int hw_padding = 0;
748 	unsigned int mps;
749 	unsigned int cqe_comp;
750 	unsigned int cqe_pad = 0;
751 	unsigned int tunnel_en = 0;
752 	unsigned int mpls_en = 0;
753 	unsigned int swp = 0;
754 	unsigned int mprq = 0;
755 	unsigned int mprq_min_stride_size_n = 0;
756 	unsigned int mprq_max_stride_size_n = 0;
757 	unsigned int mprq_min_stride_num_n = 0;
758 	unsigned int mprq_max_stride_num_n = 0;
759 	struct ether_addr mac;
760 	char name[RTE_ETH_NAME_MAX_LEN];
761 	int own_domain_id = 0;
762 	uint16_t port_id;
763 	unsigned int i;
764 
765 	/* Determine if this port representor is supposed to be spawned. */
766 	if (switch_info->representor && dpdk_dev->devargs) {
767 		struct rte_eth_devargs eth_da;
768 
769 		err = rte_eth_devargs_parse(dpdk_dev->devargs->args, &eth_da);
770 		if (err) {
771 			rte_errno = -err;
772 			DRV_LOG(ERR, "failed to process device arguments: %s",
773 				strerror(rte_errno));
774 			return NULL;
775 		}
776 		for (i = 0; i < eth_da.nb_representor_ports; ++i)
777 			if (eth_da.representor_ports[i] ==
778 			    (uint16_t)switch_info->port_name)
779 				break;
780 		if (i == eth_da.nb_representor_ports) {
781 			rte_errno = EBUSY;
782 			return NULL;
783 		}
784 	}
785 	/* Build device name. */
786 	if (!switch_info->representor)
787 		rte_strlcpy(name, dpdk_dev->name, sizeof(name));
788 	else
789 		snprintf(name, sizeof(name), "%s_representor_%u",
790 			 dpdk_dev->name, switch_info->port_name);
791 	/* check if the device is already spawned */
792 	if (rte_eth_dev_get_port_by_name(name, &port_id) == 0) {
793 		rte_errno = EEXIST;
794 		return NULL;
795 	}
796 	/* Prepare shared data between primary and secondary process. */
797 	mlx5_prepare_shared_data();
798 	errno = 0;
799 	ctx = mlx5_glue->open_device(ibv_dev);
800 	if (!ctx) {
801 		rte_errno = errno ? errno : ENODEV;
802 		return NULL;
803 	}
804 #ifdef HAVE_IBV_MLX5_MOD_SWP
805 	dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_SWP;
806 #endif
807 	/*
808 	 * Multi-packet send is supported by ConnectX-4 Lx PF as well
809 	 * as all ConnectX-5 devices.
810 	 */
811 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
812 	dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS;
813 #endif
814 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
815 	dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_STRIDING_RQ;
816 #endif
817 	mlx5_glue->dv_query_device(ctx, &dv_attr);
818 	if (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED) {
819 		if (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW) {
820 			DRV_LOG(DEBUG, "enhanced MPW is supported");
821 			mps = MLX5_MPW_ENHANCED;
822 		} else {
823 			DRV_LOG(DEBUG, "MPW is supported");
824 			mps = MLX5_MPW;
825 		}
826 	} else {
827 		DRV_LOG(DEBUG, "MPW isn't supported");
828 		mps = MLX5_MPW_DISABLED;
829 	}
830 #ifdef HAVE_IBV_MLX5_MOD_SWP
831 	if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_SWP)
832 		swp = dv_attr.sw_parsing_caps.sw_parsing_offloads;
833 	DRV_LOG(DEBUG, "SWP support: %u", swp);
834 #endif
835 	config.swp = !!swp;
836 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
837 	if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_STRIDING_RQ) {
838 		struct mlx5dv_striding_rq_caps mprq_caps =
839 			dv_attr.striding_rq_caps;
840 
841 		DRV_LOG(DEBUG, "\tmin_single_stride_log_num_of_bytes: %d",
842 			mprq_caps.min_single_stride_log_num_of_bytes);
843 		DRV_LOG(DEBUG, "\tmax_single_stride_log_num_of_bytes: %d",
844 			mprq_caps.max_single_stride_log_num_of_bytes);
845 		DRV_LOG(DEBUG, "\tmin_single_wqe_log_num_of_strides: %d",
846 			mprq_caps.min_single_wqe_log_num_of_strides);
847 		DRV_LOG(DEBUG, "\tmax_single_wqe_log_num_of_strides: %d",
848 			mprq_caps.max_single_wqe_log_num_of_strides);
849 		DRV_LOG(DEBUG, "\tsupported_qpts: %d",
850 			mprq_caps.supported_qpts);
851 		DRV_LOG(DEBUG, "device supports Multi-Packet RQ");
852 		mprq = 1;
853 		mprq_min_stride_size_n =
854 			mprq_caps.min_single_stride_log_num_of_bytes;
855 		mprq_max_stride_size_n =
856 			mprq_caps.max_single_stride_log_num_of_bytes;
857 		mprq_min_stride_num_n =
858 			mprq_caps.min_single_wqe_log_num_of_strides;
859 		mprq_max_stride_num_n =
860 			mprq_caps.max_single_wqe_log_num_of_strides;
861 		config.mprq.stride_num_n = RTE_MAX(MLX5_MPRQ_STRIDE_NUM_N,
862 						   mprq_min_stride_num_n);
863 	}
864 #endif
865 	if (RTE_CACHE_LINE_SIZE == 128 &&
866 	    !(dv_attr.flags & MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP))
867 		cqe_comp = 0;
868 	else
869 		cqe_comp = 1;
870 	config.cqe_comp = cqe_comp;
871 #ifdef HAVE_IBV_MLX5_MOD_CQE_128B_PAD
872 	/* Whether device supports 128B Rx CQE padding. */
873 	cqe_pad = RTE_CACHE_LINE_SIZE == 128 &&
874 		  (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_CQE_128B_PAD);
875 #endif
876 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
877 	if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS) {
878 		tunnel_en = ((dv_attr.tunnel_offloads_caps &
879 			      MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_VXLAN) &&
880 			     (dv_attr.tunnel_offloads_caps &
881 			      MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_GRE));
882 	}
883 	DRV_LOG(DEBUG, "tunnel offloading is %ssupported",
884 		tunnel_en ? "" : "not ");
885 #else
886 	DRV_LOG(WARNING,
887 		"tunnel offloading disabled due to old OFED/rdma-core version");
888 #endif
889 	config.tunnel_en = tunnel_en;
890 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
891 	mpls_en = ((dv_attr.tunnel_offloads_caps &
892 		    MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_CW_MPLS_OVER_GRE) &&
893 		   (dv_attr.tunnel_offloads_caps &
894 		    MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_CW_MPLS_OVER_UDP));
895 	DRV_LOG(DEBUG, "MPLS over GRE/UDP tunnel offloading is %ssupported",
896 		mpls_en ? "" : "not ");
897 #else
898 	DRV_LOG(WARNING, "MPLS over GRE/UDP tunnel offloading disabled due to"
899 		" old OFED/rdma-core version or firmware configuration");
900 #endif
901 	config.mpls_en = mpls_en;
902 	err = mlx5_glue->query_device_ex(ctx, NULL, &attr);
903 	if (err) {
904 		DEBUG("ibv_query_device_ex() failed");
905 		goto error;
906 	}
907 	DRV_LOG(DEBUG, "naming Ethernet device \"%s\"", name);
908 	if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
909 		eth_dev = rte_eth_dev_attach_secondary(name);
910 		if (eth_dev == NULL) {
911 			DRV_LOG(ERR, "can not attach rte ethdev");
912 			rte_errno = ENOMEM;
913 			err = rte_errno;
914 			goto error;
915 		}
916 		eth_dev->device = dpdk_dev;
917 		eth_dev->dev_ops = &mlx5_dev_sec_ops;
918 		err = mlx5_uar_init_secondary(eth_dev);
919 		if (err) {
920 			err = rte_errno;
921 			goto error;
922 		}
923 		/* Receive command fd from primary process */
924 		err = mlx5_socket_connect(eth_dev);
925 		if (err < 0) {
926 			err = rte_errno;
927 			goto error;
928 		}
929 		/* Remap UAR for Tx queues. */
930 		err = mlx5_tx_uar_remap(eth_dev, err);
931 		if (err) {
932 			err = rte_errno;
933 			goto error;
934 		}
935 		/*
936 		 * Ethdev pointer is still required as input since
937 		 * the primary device is not accessible from the
938 		 * secondary process.
939 		 */
940 		eth_dev->rx_pkt_burst = mlx5_select_rx_function(eth_dev);
941 		eth_dev->tx_pkt_burst = mlx5_select_tx_function(eth_dev);
942 		claim_zero(mlx5_glue->close_device(ctx));
943 		return eth_dev;
944 	}
945 	/* Check port status. */
946 	err = mlx5_glue->query_port(ctx, 1, &port_attr);
947 	if (err) {
948 		DRV_LOG(ERR, "port query failed: %s", strerror(err));
949 		goto error;
950 	}
951 	if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
952 		DRV_LOG(ERR, "port is not configured in Ethernet mode");
953 		err = EINVAL;
954 		goto error;
955 	}
956 	if (port_attr.state != IBV_PORT_ACTIVE)
957 		DRV_LOG(DEBUG, "port is not active: \"%s\" (%d)",
958 			mlx5_glue->port_state_str(port_attr.state),
959 			port_attr.state);
960 	/* Allocate protection domain. */
961 	pd = mlx5_glue->alloc_pd(ctx);
962 	if (pd == NULL) {
963 		DRV_LOG(ERR, "PD allocation failure");
964 		err = ENOMEM;
965 		goto error;
966 	}
967 	priv = rte_zmalloc("ethdev private structure",
968 			   sizeof(*priv),
969 			   RTE_CACHE_LINE_SIZE);
970 	if (priv == NULL) {
971 		DRV_LOG(ERR, "priv allocation failure");
972 		err = ENOMEM;
973 		goto error;
974 	}
975 	priv->ctx = ctx;
976 	strncpy(priv->ibdev_name, priv->ctx->device->name,
977 		sizeof(priv->ibdev_name));
978 	strncpy(priv->ibdev_path, priv->ctx->device->ibdev_path,
979 		sizeof(priv->ibdev_path));
980 	priv->device_attr = attr;
981 	priv->pd = pd;
982 	priv->mtu = ETHER_MTU;
983 #ifndef RTE_ARCH_64
984 	/* Initialize UAR access locks for 32bit implementations. */
985 	rte_spinlock_init(&priv->uar_lock_cq);
986 	for (i = 0; i < MLX5_UAR_PAGE_NUM_MAX; i++)
987 		rte_spinlock_init(&priv->uar_lock[i]);
988 #endif
989 	/* Some internal functions rely on Netlink sockets, open them now. */
990 	priv->nl_socket_rdma = mlx5_nl_init(NETLINK_RDMA);
991 	priv->nl_socket_route =	mlx5_nl_init(NETLINK_ROUTE);
992 	priv->nl_sn = 0;
993 	priv->representor = !!switch_info->representor;
994 	priv->domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
995 	priv->representor_id =
996 		switch_info->representor ? switch_info->port_name : -1;
997 	/*
998 	 * Look for sibling devices in order to reuse their switch domain
999 	 * if any, otherwise allocate one.
1000 	 */
1001 	i = mlx5_dev_to_port_id(dpdk_dev, NULL, 0);
1002 	if (i > 0) {
1003 		uint16_t port_id[i];
1004 
1005 		i = RTE_MIN(mlx5_dev_to_port_id(dpdk_dev, port_id, i), i);
1006 		while (i--) {
1007 			const struct mlx5_priv *opriv =
1008 				rte_eth_devices[port_id[i]].data->dev_private;
1009 
1010 			if (!opriv ||
1011 			    opriv->domain_id ==
1012 			    RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID)
1013 				continue;
1014 			priv->domain_id = opriv->domain_id;
1015 			break;
1016 		}
1017 	}
1018 	if (priv->domain_id == RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
1019 		err = rte_eth_switch_domain_alloc(&priv->domain_id);
1020 		if (err) {
1021 			err = rte_errno;
1022 			DRV_LOG(ERR, "unable to allocate switch domain: %s",
1023 				strerror(rte_errno));
1024 			goto error;
1025 		}
1026 		own_domain_id = 1;
1027 	}
1028 	err = mlx5_args(&config, dpdk_dev->devargs);
1029 	if (err) {
1030 		err = rte_errno;
1031 		DRV_LOG(ERR, "failed to process device arguments: %s",
1032 			strerror(rte_errno));
1033 		goto error;
1034 	}
1035 	config.hw_csum = !!(attr.device_cap_flags_ex & IBV_DEVICE_RAW_IP_CSUM);
1036 	DRV_LOG(DEBUG, "checksum offloading is %ssupported",
1037 		(config.hw_csum ? "" : "not "));
1038 #if !defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) && \
1039 	!defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
1040 	DRV_LOG(DEBUG, "counters are not supported");
1041 #endif
1042 #ifndef HAVE_IBV_FLOW_DV_SUPPORT
1043 	if (config.dv_flow_en) {
1044 		DRV_LOG(WARNING, "DV flow is not supported");
1045 		config.dv_flow_en = 0;
1046 	}
1047 #endif
1048 	config.ind_table_max_size =
1049 		attr.rss_caps.max_rwq_indirection_table_size;
1050 	/*
1051 	 * Remove this check once DPDK supports larger/variable
1052 	 * indirection tables.
1053 	 */
1054 	if (config.ind_table_max_size > (unsigned int)ETH_RSS_RETA_SIZE_512)
1055 		config.ind_table_max_size = ETH_RSS_RETA_SIZE_512;
1056 	DRV_LOG(DEBUG, "maximum Rx indirection table size is %u",
1057 		config.ind_table_max_size);
1058 	config.hw_vlan_strip = !!(attr.raw_packet_caps &
1059 				  IBV_RAW_PACKET_CAP_CVLAN_STRIPPING);
1060 	DRV_LOG(DEBUG, "VLAN stripping is %ssupported",
1061 		(config.hw_vlan_strip ? "" : "not "));
1062 	config.hw_fcs_strip = !!(attr.raw_packet_caps &
1063 				 IBV_RAW_PACKET_CAP_SCATTER_FCS);
1064 	DRV_LOG(DEBUG, "FCS stripping configuration is %ssupported",
1065 		(config.hw_fcs_strip ? "" : "not "));
1066 #if defined(HAVE_IBV_WQ_FLAG_RX_END_PADDING)
1067 	hw_padding = !!attr.rx_pad_end_addr_align;
1068 #elif defined(HAVE_IBV_WQ_FLAGS_PCI_WRITE_END_PADDING)
1069 	hw_padding = !!(attr.device_cap_flags_ex &
1070 			IBV_DEVICE_PCI_WRITE_END_PADDING);
1071 #endif
1072 	if (config.hw_padding && !hw_padding) {
1073 		DRV_LOG(DEBUG, "Rx end alignment padding isn't supported");
1074 		config.hw_padding = 0;
1075 	} else if (config.hw_padding) {
1076 		DRV_LOG(DEBUG, "Rx end alignment padding is enabled");
1077 	}
1078 	config.tso = (attr.tso_caps.max_tso > 0 &&
1079 		      (attr.tso_caps.supported_qpts &
1080 		       (1 << IBV_QPT_RAW_PACKET)));
1081 	if (config.tso)
1082 		config.tso_max_payload_sz = attr.tso_caps.max_tso;
1083 	/*
1084 	 * MPW is disabled by default, while the Enhanced MPW is enabled
1085 	 * by default.
1086 	 */
1087 	if (config.mps == MLX5_ARG_UNSET)
1088 		config.mps = (mps == MLX5_MPW_ENHANCED) ? MLX5_MPW_ENHANCED :
1089 							  MLX5_MPW_DISABLED;
1090 	else
1091 		config.mps = config.mps ? mps : MLX5_MPW_DISABLED;
1092 	DRV_LOG(INFO, "%sMPS is %s",
1093 		config.mps == MLX5_MPW_ENHANCED ? "enhanced " : "",
1094 		config.mps != MLX5_MPW_DISABLED ? "enabled" : "disabled");
1095 	if (config.cqe_comp && !cqe_comp) {
1096 		DRV_LOG(WARNING, "Rx CQE compression isn't supported");
1097 		config.cqe_comp = 0;
1098 	}
1099 	if (config.cqe_pad && !cqe_pad) {
1100 		DRV_LOG(WARNING, "Rx CQE padding isn't supported");
1101 		config.cqe_pad = 0;
1102 	} else if (config.cqe_pad) {
1103 		DRV_LOG(INFO, "Rx CQE padding is enabled");
1104 	}
1105 	if (config.mprq.enabled && mprq) {
1106 		if (config.mprq.stride_num_n > mprq_max_stride_num_n ||
1107 		    config.mprq.stride_num_n < mprq_min_stride_num_n) {
1108 			config.mprq.stride_num_n =
1109 				RTE_MAX(MLX5_MPRQ_STRIDE_NUM_N,
1110 					mprq_min_stride_num_n);
1111 			DRV_LOG(WARNING,
1112 				"the number of strides"
1113 				" for Multi-Packet RQ is out of range,"
1114 				" setting default value (%u)",
1115 				1 << config.mprq.stride_num_n);
1116 		}
1117 		config.mprq.min_stride_size_n = mprq_min_stride_size_n;
1118 		config.mprq.max_stride_size_n = mprq_max_stride_size_n;
1119 	} else if (config.mprq.enabled && !mprq) {
1120 		DRV_LOG(WARNING, "Multi-Packet RQ isn't supported");
1121 		config.mprq.enabled = 0;
1122 	}
1123 	eth_dev = rte_eth_dev_allocate(name);
1124 	if (eth_dev == NULL) {
1125 		DRV_LOG(ERR, "can not allocate rte ethdev");
1126 		err = ENOMEM;
1127 		goto error;
1128 	}
1129 	/* Flag to call rte_eth_dev_release_port() in rte_eth_dev_close(). */
1130 	eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
1131 	if (priv->representor) {
1132 		eth_dev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR;
1133 		eth_dev->data->representor_id = priv->representor_id;
1134 	}
1135 	eth_dev->data->dev_private = priv;
1136 	priv->dev_data = eth_dev->data;
1137 	eth_dev->data->mac_addrs = priv->mac;
1138 	eth_dev->device = dpdk_dev;
1139 	err = mlx5_uar_init_primary(eth_dev);
1140 	if (err) {
1141 		err = rte_errno;
1142 		goto error;
1143 	}
1144 	/* Configure the first MAC address by default. */
1145 	if (mlx5_get_mac(eth_dev, &mac.addr_bytes)) {
1146 		DRV_LOG(ERR,
1147 			"port %u cannot get MAC address, is mlx5_en"
1148 			" loaded? (errno: %s)",
1149 			eth_dev->data->port_id, strerror(rte_errno));
1150 		err = ENODEV;
1151 		goto error;
1152 	}
1153 	DRV_LOG(INFO,
1154 		"port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
1155 		eth_dev->data->port_id,
1156 		mac.addr_bytes[0], mac.addr_bytes[1],
1157 		mac.addr_bytes[2], mac.addr_bytes[3],
1158 		mac.addr_bytes[4], mac.addr_bytes[5]);
1159 #ifndef NDEBUG
1160 	{
1161 		char ifname[IF_NAMESIZE];
1162 
1163 		if (mlx5_get_ifname(eth_dev, &ifname) == 0)
1164 			DRV_LOG(DEBUG, "port %u ifname is \"%s\"",
1165 				eth_dev->data->port_id, ifname);
1166 		else
1167 			DRV_LOG(DEBUG, "port %u ifname is unknown",
1168 				eth_dev->data->port_id);
1169 	}
1170 #endif
1171 	/* Get actual MTU if possible. */
1172 	err = mlx5_get_mtu(eth_dev, &priv->mtu);
1173 	if (err) {
1174 		err = rte_errno;
1175 		goto error;
1176 	}
1177 	DRV_LOG(DEBUG, "port %u MTU is %u", eth_dev->data->port_id,
1178 		priv->mtu);
1179 	/* Initialize burst functions to prevent crashes before link-up. */
1180 	eth_dev->rx_pkt_burst = removed_rx_burst;
1181 	eth_dev->tx_pkt_burst = removed_tx_burst;
1182 	eth_dev->dev_ops = &mlx5_dev_ops;
1183 	/* Register MAC address. */
1184 	claim_zero(mlx5_mac_addr_add(eth_dev, &mac, 0, 0));
1185 	if (config.vf && config.vf_nl_en)
1186 		mlx5_nl_mac_addr_sync(eth_dev);
1187 	priv->tcf_context = mlx5_flow_tcf_context_create();
1188 	if (!priv->tcf_context) {
1189 		err = -rte_errno;
1190 		DRV_LOG(WARNING,
1191 			"flow rules relying on switch offloads will not be"
1192 			" supported: cannot open libmnl socket: %s",
1193 			strerror(rte_errno));
1194 	} else {
1195 		struct rte_flow_error error;
1196 		unsigned int ifindex = mlx5_ifindex(eth_dev);
1197 
1198 		if (!ifindex) {
1199 			err = -rte_errno;
1200 			error.message =
1201 				"cannot retrieve network interface index";
1202 		} else {
1203 			err = mlx5_flow_tcf_init(priv->tcf_context,
1204 						 ifindex, &error);
1205 		}
1206 		if (err) {
1207 			DRV_LOG(WARNING,
1208 				"flow rules relying on switch offloads will"
1209 				" not be supported: %s: %s",
1210 				error.message, strerror(rte_errno));
1211 			mlx5_flow_tcf_context_destroy(priv->tcf_context);
1212 			priv->tcf_context = NULL;
1213 		}
1214 	}
1215 	TAILQ_INIT(&priv->flows);
1216 	TAILQ_INIT(&priv->ctrl_flows);
1217 	/* Hint libmlx5 to use PMD allocator for data plane resources */
1218 	struct mlx5dv_ctx_allocators alctr = {
1219 		.alloc = &mlx5_alloc_verbs_buf,
1220 		.free = &mlx5_free_verbs_buf,
1221 		.data = priv,
1222 	};
1223 	mlx5_glue->dv_set_context_attr(ctx, MLX5DV_CTX_ATTR_BUF_ALLOCATORS,
1224 				       (void *)((uintptr_t)&alctr));
1225 	/* Bring Ethernet device up. */
1226 	DRV_LOG(DEBUG, "port %u forcing Ethernet interface up",
1227 		eth_dev->data->port_id);
1228 	mlx5_set_link_up(eth_dev);
1229 	/*
1230 	 * Even though the interrupt handler is not installed yet,
1231 	 * interrupts will still trigger on the asyn_fd from
1232 	 * Verbs context returned by ibv_open_device().
1233 	 */
1234 	mlx5_link_update(eth_dev, 0);
1235 	/* Store device configuration on private structure. */
1236 	priv->config = config;
1237 	/* Supported Verbs flow priority number detection. */
1238 	err = mlx5_flow_discover_priorities(eth_dev);
1239 	if (err < 0) {
1240 		err = -err;
1241 		goto error;
1242 	}
1243 	priv->config.flow_prio = err;
1244 	/*
1245 	 * Once the device is added to the list of memory event
1246 	 * callback, its global MR cache table cannot be expanded
1247 	 * on the fly because of deadlock. If it overflows, lookup
1248 	 * should be done by searching MR list linearly, which is slow.
1249 	 */
1250 	err = mlx5_mr_btree_init(&priv->mr.cache,
1251 				 MLX5_MR_BTREE_CACHE_N * 2,
1252 				 eth_dev->device->numa_node);
1253 	if (err) {
1254 		err = rte_errno;
1255 		goto error;
1256 	}
1257 	/* Add device to memory callback list. */
1258 	rte_rwlock_write_lock(&mlx5_shared_data->mem_event_rwlock);
1259 	LIST_INSERT_HEAD(&mlx5_shared_data->mem_event_cb_list,
1260 			 priv, mem_event_cb);
1261 	rte_rwlock_write_unlock(&mlx5_shared_data->mem_event_rwlock);
1262 	return eth_dev;
1263 error:
1264 	if (priv) {
1265 		if (priv->nl_socket_route >= 0)
1266 			close(priv->nl_socket_route);
1267 		if (priv->nl_socket_rdma >= 0)
1268 			close(priv->nl_socket_rdma);
1269 		if (priv->tcf_context)
1270 			mlx5_flow_tcf_context_destroy(priv->tcf_context);
1271 		if (own_domain_id)
1272 			claim_zero(rte_eth_switch_domain_free(priv->domain_id));
1273 		rte_free(priv);
1274 		if (eth_dev != NULL)
1275 			eth_dev->data->dev_private = NULL;
1276 	}
1277 	if (pd)
1278 		claim_zero(mlx5_glue->dealloc_pd(pd));
1279 	if (eth_dev != NULL) {
1280 		/* mac_addrs must not be freed alone because part of dev_private */
1281 		eth_dev->data->mac_addrs = NULL;
1282 		rte_eth_dev_release_port(eth_dev);
1283 	}
1284 	if (ctx)
1285 		claim_zero(mlx5_glue->close_device(ctx));
1286 	assert(err > 0);
1287 	rte_errno = err;
1288 	return NULL;
1289 }
1290 
1291 /** Data associated with devices to spawn. */
1292 struct mlx5_dev_spawn_data {
1293 	unsigned int ifindex; /**< Network interface index. */
1294 	struct mlx5_switch_info info; /**< Switch information. */
1295 	struct ibv_device *ibv_dev; /**< Associated IB device. */
1296 	struct rte_eth_dev *eth_dev; /**< Associated Ethernet device. */
1297 };
1298 
1299 /**
1300  * Comparison callback to sort device data.
1301  *
1302  * This is meant to be used with qsort().
1303  *
1304  * @param a[in]
1305  *   Pointer to pointer to first data object.
1306  * @param b[in]
1307  *   Pointer to pointer to second data object.
1308  *
1309  * @return
1310  *   0 if both objects are equal, less than 0 if the first argument is less
1311  *   than the second, greater than 0 otherwise.
1312  */
1313 static int
1314 mlx5_dev_spawn_data_cmp(const void *a, const void *b)
1315 {
1316 	const struct mlx5_switch_info *si_a =
1317 		&((const struct mlx5_dev_spawn_data *)a)->info;
1318 	const struct mlx5_switch_info *si_b =
1319 		&((const struct mlx5_dev_spawn_data *)b)->info;
1320 	int ret;
1321 
1322 	/* Master device first. */
1323 	ret = si_b->master - si_a->master;
1324 	if (ret)
1325 		return ret;
1326 	/* Then representor devices. */
1327 	ret = si_b->representor - si_a->representor;
1328 	if (ret)
1329 		return ret;
1330 	/* Unidentified devices come last in no specific order. */
1331 	if (!si_a->representor)
1332 		return 0;
1333 	/* Order representors by name. */
1334 	return si_a->port_name - si_b->port_name;
1335 }
1336 
1337 /**
1338  * DPDK callback to register a PCI device.
1339  *
1340  * This function spawns Ethernet devices out of a given PCI device.
1341  *
1342  * @param[in] pci_drv
1343  *   PCI driver structure (mlx5_driver).
1344  * @param[in] pci_dev
1345  *   PCI device information.
1346  *
1347  * @return
1348  *   0 on success, a negative errno value otherwise and rte_errno is set.
1349  */
1350 static int
1351 mlx5_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1352 	       struct rte_pci_device *pci_dev)
1353 {
1354 	struct ibv_device **ibv_list;
1355 	unsigned int n = 0;
1356 	struct mlx5_dev_config dev_config;
1357 	int ret;
1358 
1359 	assert(pci_drv == &mlx5_driver);
1360 	errno = 0;
1361 	ibv_list = mlx5_glue->get_device_list(&ret);
1362 	if (!ibv_list) {
1363 		rte_errno = errno ? errno : ENOSYS;
1364 		DRV_LOG(ERR, "cannot list devices, is ib_uverbs loaded?");
1365 		return -rte_errno;
1366 	}
1367 
1368 	struct ibv_device *ibv_match[ret + 1];
1369 
1370 	while (ret-- > 0) {
1371 		struct rte_pci_addr pci_addr;
1372 
1373 		DRV_LOG(DEBUG, "checking device \"%s\"", ibv_list[ret]->name);
1374 		if (mlx5_ibv_device_to_pci_addr(ibv_list[ret], &pci_addr))
1375 			continue;
1376 		if (pci_dev->addr.domain != pci_addr.domain ||
1377 		    pci_dev->addr.bus != pci_addr.bus ||
1378 		    pci_dev->addr.devid != pci_addr.devid ||
1379 		    pci_dev->addr.function != pci_addr.function)
1380 			continue;
1381 		DRV_LOG(INFO, "PCI information matches for device \"%s\"",
1382 			ibv_list[ret]->name);
1383 		ibv_match[n++] = ibv_list[ret];
1384 	}
1385 	ibv_match[n] = NULL;
1386 
1387 	struct mlx5_dev_spawn_data list[n];
1388 	int nl_route = n ? mlx5_nl_init(NETLINK_ROUTE) : -1;
1389 	int nl_rdma = n ? mlx5_nl_init(NETLINK_RDMA) : -1;
1390 	unsigned int i;
1391 	unsigned int u;
1392 
1393 	/*
1394 	 * The existence of several matching entries (n > 1) means port
1395 	 * representors have been instantiated. No existing Verbs call nor
1396 	 * /sys entries can tell them apart, this can only be done through
1397 	 * Netlink calls assuming kernel drivers are recent enough to
1398 	 * support them.
1399 	 *
1400 	 * In the event of identification failure through Netlink, try again
1401 	 * through sysfs, then either:
1402 	 *
1403 	 * 1. No device matches (n == 0), complain and bail out.
1404 	 * 2. A single IB device matches (n == 1) and is not a representor,
1405 	 *    assume no switch support.
1406 	 * 3. Otherwise no safe assumptions can be made; complain louder and
1407 	 *    bail out.
1408 	 */
1409 	for (i = 0; i != n; ++i) {
1410 		list[i].ibv_dev = ibv_match[i];
1411 		list[i].eth_dev = NULL;
1412 		if (nl_rdma < 0)
1413 			list[i].ifindex = 0;
1414 		else
1415 			list[i].ifindex = mlx5_nl_ifindex
1416 				(nl_rdma, list[i].ibv_dev->name);
1417 		if (nl_route < 0 ||
1418 		    !list[i].ifindex ||
1419 		    mlx5_nl_switch_info(nl_route, list[i].ifindex,
1420 					&list[i].info) ||
1421 		    ((!list[i].info.representor && !list[i].info.master) &&
1422 		     mlx5_sysfs_switch_info(list[i].ifindex, &list[i].info))) {
1423 			list[i].ifindex = 0;
1424 			memset(&list[i].info, 0, sizeof(list[i].info));
1425 			continue;
1426 		}
1427 	}
1428 	if (nl_rdma >= 0)
1429 		close(nl_rdma);
1430 	if (nl_route >= 0)
1431 		close(nl_route);
1432 	/* Count unidentified devices. */
1433 	for (u = 0, i = 0; i != n; ++i)
1434 		if (!list[i].info.master && !list[i].info.representor)
1435 			++u;
1436 	if (u) {
1437 		if (n == 1 && u == 1) {
1438 			/* Case #2. */
1439 			DRV_LOG(INFO, "no switch support detected");
1440 		} else {
1441 			/* Case #3. */
1442 			DRV_LOG(ERR,
1443 				"unable to tell which of the matching devices"
1444 				" is the master (lack of kernel support?)");
1445 			n = 0;
1446 		}
1447 	}
1448 	/*
1449 	 * Sort list to probe devices in natural order for users convenience
1450 	 * (i.e. master first, then representors from lowest to highest ID).
1451 	 */
1452 	if (n)
1453 		qsort(list, n, sizeof(*list), mlx5_dev_spawn_data_cmp);
1454 	/* Default configuration. */
1455 	dev_config = (struct mlx5_dev_config){
1456 		.hw_padding = 0,
1457 		.mps = MLX5_ARG_UNSET,
1458 		.tx_vec_en = 1,
1459 		.rx_vec_en = 1,
1460 		.txq_inline = MLX5_ARG_UNSET,
1461 		.txqs_inline = MLX5_ARG_UNSET,
1462 		.txqs_vec = MLX5_ARG_UNSET,
1463 		.inline_max_packet_sz = MLX5_ARG_UNSET,
1464 		.vf_nl_en = 1,
1465 		.mprq = {
1466 			.enabled = 0, /* Disabled by default. */
1467 			.stride_num_n = MLX5_MPRQ_STRIDE_NUM_N,
1468 			.max_memcpy_len = MLX5_MPRQ_MEMCPY_DEFAULT_LEN,
1469 			.min_rxqs_num = MLX5_MPRQ_MIN_RXQS,
1470 		},
1471 	};
1472 	/* Device speicific configuration. */
1473 	switch (pci_dev->id.device_id) {
1474 	case PCI_DEVICE_ID_MELLANOX_CONNECTX5BF:
1475 		dev_config.txqs_vec = MLX5_VPMD_MAX_TXQS_BLUEFIELD;
1476 		break;
1477 	case PCI_DEVICE_ID_MELLANOX_CONNECTX4VF:
1478 	case PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF:
1479 	case PCI_DEVICE_ID_MELLANOX_CONNECTX5VF:
1480 	case PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF:
1481 		dev_config.vf = 1;
1482 		break;
1483 	default:
1484 		break;
1485 	}
1486 	/* Set architecture-dependent default value if unset. */
1487 	if (dev_config.txqs_vec == MLX5_ARG_UNSET)
1488 		dev_config.txqs_vec = MLX5_VPMD_MAX_TXQS;
1489 	for (i = 0; i != n; ++i) {
1490 		uint32_t restore;
1491 
1492 		list[i].eth_dev = mlx5_dev_spawn(&pci_dev->device,
1493 						 list[i].ibv_dev, dev_config,
1494 						 &list[i].info);
1495 		if (!list[i].eth_dev) {
1496 			if (rte_errno != EBUSY && rte_errno != EEXIST)
1497 				break;
1498 			/* Device is disabled or already spawned. Ignore it. */
1499 			continue;
1500 		}
1501 		restore = list[i].eth_dev->data->dev_flags;
1502 		rte_eth_copy_pci_info(list[i].eth_dev, pci_dev);
1503 		/* Restore non-PCI flags cleared by the above call. */
1504 		list[i].eth_dev->data->dev_flags |= restore;
1505 		rte_eth_dev_probing_finish(list[i].eth_dev);
1506 	}
1507 	mlx5_glue->free_device_list(ibv_list);
1508 	if (!n) {
1509 		DRV_LOG(WARNING,
1510 			"no Verbs device matches PCI device " PCI_PRI_FMT ","
1511 			" are kernel drivers loaded?",
1512 			pci_dev->addr.domain, pci_dev->addr.bus,
1513 			pci_dev->addr.devid, pci_dev->addr.function);
1514 		rte_errno = ENOENT;
1515 		ret = -rte_errno;
1516 	} else if (i != n) {
1517 		DRV_LOG(ERR,
1518 			"probe of PCI device " PCI_PRI_FMT " aborted after"
1519 			" encountering an error: %s",
1520 			pci_dev->addr.domain, pci_dev->addr.bus,
1521 			pci_dev->addr.devid, pci_dev->addr.function,
1522 			strerror(rte_errno));
1523 		ret = -rte_errno;
1524 		/* Roll back. */
1525 		while (i--) {
1526 			if (!list[i].eth_dev)
1527 				continue;
1528 			mlx5_dev_close(list[i].eth_dev);
1529 			/* mac_addrs must not be freed because in dev_private */
1530 			list[i].eth_dev->data->mac_addrs = NULL;
1531 			claim_zero(rte_eth_dev_release_port(list[i].eth_dev));
1532 		}
1533 		/* Restore original error. */
1534 		rte_errno = -ret;
1535 	} else {
1536 		ret = 0;
1537 	}
1538 	return ret;
1539 }
1540 
1541 /**
1542  * DPDK callback to remove a PCI device.
1543  *
1544  * This function removes all Ethernet devices belong to a given PCI device.
1545  *
1546  * @param[in] pci_dev
1547  *   Pointer to the PCI device.
1548  *
1549  * @return
1550  *   0 on success, the function cannot fail.
1551  */
1552 static int
1553 mlx5_pci_remove(struct rte_pci_device *pci_dev)
1554 {
1555 	uint16_t port_id;
1556 	struct rte_eth_dev *port;
1557 
1558 	for (port_id = 0; port_id < RTE_MAX_ETHPORTS; port_id++) {
1559 		port = &rte_eth_devices[port_id];
1560 		if (port->state != RTE_ETH_DEV_UNUSED &&
1561 				port->device == &pci_dev->device)
1562 			rte_eth_dev_close(port_id);
1563 	}
1564 	return 0;
1565 }
1566 
1567 static const struct rte_pci_id mlx5_pci_id_map[] = {
1568 	{
1569 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1570 			       PCI_DEVICE_ID_MELLANOX_CONNECTX4)
1571 	},
1572 	{
1573 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1574 			       PCI_DEVICE_ID_MELLANOX_CONNECTX4VF)
1575 	},
1576 	{
1577 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1578 			       PCI_DEVICE_ID_MELLANOX_CONNECTX4LX)
1579 	},
1580 	{
1581 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1582 			       PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF)
1583 	},
1584 	{
1585 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1586 			       PCI_DEVICE_ID_MELLANOX_CONNECTX5)
1587 	},
1588 	{
1589 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1590 			       PCI_DEVICE_ID_MELLANOX_CONNECTX5VF)
1591 	},
1592 	{
1593 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1594 			       PCI_DEVICE_ID_MELLANOX_CONNECTX5EX)
1595 	},
1596 	{
1597 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1598 			       PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF)
1599 	},
1600 	{
1601 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1602 			       PCI_DEVICE_ID_MELLANOX_CONNECTX5BF)
1603 	},
1604 	{
1605 		RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1606 			       PCI_DEVICE_ID_MELLANOX_CONNECTX5BFVF)
1607 	},
1608 	{
1609 		.vendor_id = 0
1610 	}
1611 };
1612 
1613 static struct rte_pci_driver mlx5_driver = {
1614 	.driver = {
1615 		.name = MLX5_DRIVER_NAME
1616 	},
1617 	.id_table = mlx5_pci_id_map,
1618 	.probe = mlx5_pci_probe,
1619 	.remove = mlx5_pci_remove,
1620 	.drv_flags = (RTE_PCI_DRV_INTR_LSC | RTE_PCI_DRV_INTR_RMV |
1621 		      RTE_PCI_DRV_PROBE_AGAIN),
1622 };
1623 
1624 #ifdef RTE_LIBRTE_MLX5_DLOPEN_DEPS
1625 
1626 /**
1627  * Suffix RTE_EAL_PMD_PATH with "-glue".
1628  *
1629  * This function performs a sanity check on RTE_EAL_PMD_PATH before
1630  * suffixing its last component.
1631  *
1632  * @param buf[out]
1633  *   Output buffer, should be large enough otherwise NULL is returned.
1634  * @param size
1635  *   Size of @p out.
1636  *
1637  * @return
1638  *   Pointer to @p buf or @p NULL in case suffix cannot be appended.
1639  */
1640 static char *
1641 mlx5_glue_path(char *buf, size_t size)
1642 {
1643 	static const char *const bad[] = { "/", ".", "..", NULL };
1644 	const char *path = RTE_EAL_PMD_PATH;
1645 	size_t len = strlen(path);
1646 	size_t off;
1647 	int i;
1648 
1649 	while (len && path[len - 1] == '/')
1650 		--len;
1651 	for (off = len; off && path[off - 1] != '/'; --off)
1652 		;
1653 	for (i = 0; bad[i]; ++i)
1654 		if (!strncmp(path + off, bad[i], (int)(len - off)))
1655 			goto error;
1656 	i = snprintf(buf, size, "%.*s-glue", (int)len, path);
1657 	if (i == -1 || (size_t)i >= size)
1658 		goto error;
1659 	return buf;
1660 error:
1661 	DRV_LOG(ERR,
1662 		"unable to append \"-glue\" to last component of"
1663 		" RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"),"
1664 		" please re-configure DPDK");
1665 	return NULL;
1666 }
1667 
1668 /**
1669  * Initialization routine for run-time dependency on rdma-core.
1670  */
1671 static int
1672 mlx5_glue_init(void)
1673 {
1674 	char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
1675 	const char *path[] = {
1676 		/*
1677 		 * A basic security check is necessary before trusting
1678 		 * MLX5_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
1679 		 */
1680 		(geteuid() == getuid() && getegid() == getgid() ?
1681 		 getenv("MLX5_GLUE_PATH") : NULL),
1682 		/*
1683 		 * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
1684 		 * variant, otherwise let dlopen() look up libraries on its
1685 		 * own.
1686 		 */
1687 		(*RTE_EAL_PMD_PATH ?
1688 		 mlx5_glue_path(glue_path, sizeof(glue_path)) : ""),
1689 	};
1690 	unsigned int i = 0;
1691 	void *handle = NULL;
1692 	void **sym;
1693 	const char *dlmsg;
1694 
1695 	while (!handle && i != RTE_DIM(path)) {
1696 		const char *end;
1697 		size_t len;
1698 		int ret;
1699 
1700 		if (!path[i]) {
1701 			++i;
1702 			continue;
1703 		}
1704 		end = strpbrk(path[i], ":;");
1705 		if (!end)
1706 			end = path[i] + strlen(path[i]);
1707 		len = end - path[i];
1708 		ret = 0;
1709 		do {
1710 			char name[ret + 1];
1711 
1712 			ret = snprintf(name, sizeof(name), "%.*s%s" MLX5_GLUE,
1713 				       (int)len, path[i],
1714 				       (!len || *(end - 1) == '/') ? "" : "/");
1715 			if (ret == -1)
1716 				break;
1717 			if (sizeof(name) != (size_t)ret + 1)
1718 				continue;
1719 			DRV_LOG(DEBUG, "looking for rdma-core glue as \"%s\"",
1720 				name);
1721 			handle = dlopen(name, RTLD_LAZY);
1722 			break;
1723 		} while (1);
1724 		path[i] = end + 1;
1725 		if (!*end)
1726 			++i;
1727 	}
1728 	if (!handle) {
1729 		rte_errno = EINVAL;
1730 		dlmsg = dlerror();
1731 		if (dlmsg)
1732 			DRV_LOG(WARNING, "cannot load glue library: %s", dlmsg);
1733 		goto glue_error;
1734 	}
1735 	sym = dlsym(handle, "mlx5_glue");
1736 	if (!sym || !*sym) {
1737 		rte_errno = EINVAL;
1738 		dlmsg = dlerror();
1739 		if (dlmsg)
1740 			DRV_LOG(ERR, "cannot resolve glue symbol: %s", dlmsg);
1741 		goto glue_error;
1742 	}
1743 	mlx5_glue = *sym;
1744 	return 0;
1745 glue_error:
1746 	if (handle)
1747 		dlclose(handle);
1748 	DRV_LOG(WARNING,
1749 		"cannot initialize PMD due to missing run-time dependency on"
1750 		" rdma-core libraries (libibverbs, libmlx5)");
1751 	return -rte_errno;
1752 }
1753 
1754 #endif
1755 
1756 /**
1757  * Driver initialization routine.
1758  */
1759 RTE_INIT(rte_mlx5_pmd_init)
1760 {
1761 	/* Initialize driver log type. */
1762 	mlx5_logtype = rte_log_register("pmd.net.mlx5");
1763 	if (mlx5_logtype >= 0)
1764 		rte_log_set_level(mlx5_logtype, RTE_LOG_NOTICE);
1765 
1766 	/* Build the static tables for Verbs conversion. */
1767 	mlx5_set_ptype_table();
1768 	mlx5_set_cksum_table();
1769 	mlx5_set_swp_types_table();
1770 	/*
1771 	 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
1772 	 * huge pages. Calling ibv_fork_init() during init allows
1773 	 * applications to use fork() safely for purposes other than
1774 	 * using this PMD, which is not supported in forked processes.
1775 	 */
1776 	setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
1777 	/* Match the size of Rx completion entry to the size of a cacheline. */
1778 	if (RTE_CACHE_LINE_SIZE == 128)
1779 		setenv("MLX5_CQE_SIZE", "128", 0);
1780 	/*
1781 	 * MLX5_DEVICE_FATAL_CLEANUP tells ibv_destroy functions to
1782 	 * cleanup all the Verbs resources even when the device was removed.
1783 	 */
1784 	setenv("MLX5_DEVICE_FATAL_CLEANUP", "1", 1);
1785 #ifdef RTE_LIBRTE_MLX5_DLOPEN_DEPS
1786 	if (mlx5_glue_init())
1787 		return;
1788 	assert(mlx5_glue);
1789 #endif
1790 #ifndef NDEBUG
1791 	/* Glue structure must not contain any NULL pointers. */
1792 	{
1793 		unsigned int i;
1794 
1795 		for (i = 0; i != sizeof(*mlx5_glue) / sizeof(void *); ++i)
1796 			assert(((const void *const *)mlx5_glue)[i]);
1797 	}
1798 #endif
1799 	if (strcmp(mlx5_glue->version, MLX5_GLUE_VERSION)) {
1800 		DRV_LOG(ERR,
1801 			"rdma-core glue \"%s\" mismatch: \"%s\" is required",
1802 			mlx5_glue->version, MLX5_GLUE_VERSION);
1803 		return;
1804 	}
1805 	mlx5_glue->fork_init();
1806 	rte_pci_register(&mlx5_driver);
1807 }
1808 
1809 RTE_PMD_EXPORT_NAME(net_mlx5, __COUNTER__);
1810 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5, mlx5_pci_id_map);
1811 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5, "* ib_uverbs & mlx5_core & mlx5_ib");
1812