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