net/mlx5: use memseg walk instead of iteration
[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
17 /* Verbs header. */
18 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
19 #ifdef PEDANTIC
20 #pragma GCC diagnostic ignored "-Wpedantic"
21 #endif
22 #include <infiniband/verbs.h>
23 #ifdef PEDANTIC
24 #pragma GCC diagnostic error "-Wpedantic"
25 #endif
26
27 #include <rte_malloc.h>
28 #include <rte_ethdev_driver.h>
29 #include <rte_ethdev_pci.h>
30 #include <rte_pci.h>
31 #include <rte_bus_pci.h>
32 #include <rte_common.h>
33 #include <rte_config.h>
34 #include <rte_eal_memconfig.h>
35 #include <rte_kvargs.h>
36
37 #include "mlx5.h"
38 #include "mlx5_utils.h"
39 #include "mlx5_rxtx.h"
40 #include "mlx5_autoconf.h"
41 #include "mlx5_defs.h"
42 #include "mlx5_glue.h"
43
44 /* Device parameter to enable RX completion queue compression. */
45 #define MLX5_RXQ_CQE_COMP_EN "rxq_cqe_comp_en"
46
47 /* Device parameter to configure inline send. */
48 #define MLX5_TXQ_INLINE "txq_inline"
49
50 /*
51  * Device parameter to configure the number of TX queues threshold for
52  * enabling inline send.
53  */
54 #define MLX5_TXQS_MIN_INLINE "txqs_min_inline"
55
56 /* Device parameter to enable multi-packet send WQEs. */
57 #define MLX5_TXQ_MPW_EN "txq_mpw_en"
58
59 /* Device parameter to include 2 dsegs in the title WQEBB. */
60 #define MLX5_TXQ_MPW_HDR_DSEG_EN "txq_mpw_hdr_dseg_en"
61
62 /* Device parameter to limit the size of inlining packet. */
63 #define MLX5_TXQ_MAX_INLINE_LEN "txq_max_inline_len"
64
65 /* Device parameter to enable hardware Tx vector. */
66 #define MLX5_TX_VEC_EN "tx_vec_en"
67
68 /* Device parameter to enable hardware Rx vector. */
69 #define MLX5_RX_VEC_EN "rx_vec_en"
70
71 #ifndef HAVE_IBV_MLX5_MOD_MPW
72 #define MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED (1 << 2)
73 #define MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW (1 << 3)
74 #endif
75
76 #ifndef HAVE_IBV_MLX5_MOD_CQE_128B_COMP
77 #define MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP (1 << 4)
78 #endif
79
80 /** Driver-specific log messages type. */
81 int mlx5_logtype;
82
83 /**
84  * Retrieve integer value from environment variable.
85  *
86  * @param[in] name
87  *   Environment variable name.
88  *
89  * @return
90  *   Integer value, 0 if the variable is not set.
91  */
92 int
93 mlx5_getenv_int(const char *name)
94 {
95         const char *val = getenv(name);
96
97         if (val == NULL)
98                 return 0;
99         return atoi(val);
100 }
101
102 /**
103  * Verbs callback to allocate a memory. This function should allocate the space
104  * according to the size provided residing inside a huge page.
105  * Please note that all allocation must respect the alignment from libmlx5
106  * (i.e. currently sysconf(_SC_PAGESIZE)).
107  *
108  * @param[in] size
109  *   The size in bytes of the memory to allocate.
110  * @param[in] data
111  *   A pointer to the callback data.
112  *
113  * @return
114  *   Allocated buffer, NULL otherwise and rte_errno is set.
115  */
116 static void *
117 mlx5_alloc_verbs_buf(size_t size, void *data)
118 {
119         struct priv *priv = data;
120         void *ret;
121         size_t alignment = sysconf(_SC_PAGESIZE);
122         unsigned int socket = SOCKET_ID_ANY;
123
124         if (priv->verbs_alloc_ctx.type == MLX5_VERBS_ALLOC_TYPE_TX_QUEUE) {
125                 const struct mlx5_txq_ctrl *ctrl = priv->verbs_alloc_ctx.obj;
126
127                 socket = ctrl->socket;
128         } else if (priv->verbs_alloc_ctx.type ==
129                    MLX5_VERBS_ALLOC_TYPE_RX_QUEUE) {
130                 const struct mlx5_rxq_ctrl *ctrl = priv->verbs_alloc_ctx.obj;
131
132                 socket = ctrl->socket;
133         }
134         assert(data != NULL);
135         ret = rte_malloc_socket(__func__, size, alignment, socket);
136         if (!ret && size)
137                 rte_errno = ENOMEM;
138         return ret;
139 }
140
141 /**
142  * Verbs callback to free a memory.
143  *
144  * @param[in] ptr
145  *   A pointer to the memory to free.
146  * @param[in] data
147  *   A pointer to the callback data.
148  */
149 static void
150 mlx5_free_verbs_buf(void *ptr, void *data __rte_unused)
151 {
152         assert(data != NULL);
153         rte_free(ptr);
154 }
155
156 /**
157  * DPDK callback to close the device.
158  *
159  * Destroy all queues and objects, free memory.
160  *
161  * @param dev
162  *   Pointer to Ethernet device structure.
163  */
164 static void
165 mlx5_dev_close(struct rte_eth_dev *dev)
166 {
167         struct priv *priv = dev->data->dev_private;
168         unsigned int i;
169         int ret;
170
171         DRV_LOG(DEBUG, "port %u closing device \"%s\"",
172                 dev->data->port_id,
173                 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
174         /* In case mlx5_dev_stop() has not been called. */
175         mlx5_dev_interrupt_handler_uninstall(dev);
176         mlx5_traffic_disable(dev);
177         /* Prevent crashes when queues are still in use. */
178         dev->rx_pkt_burst = removed_rx_burst;
179         dev->tx_pkt_burst = removed_tx_burst;
180         if (priv->rxqs != NULL) {
181                 /* XXX race condition if mlx5_rx_burst() is still running. */
182                 usleep(1000);
183                 for (i = 0; (i != priv->rxqs_n); ++i)
184                         mlx5_rxq_release(dev, i);
185                 priv->rxqs_n = 0;
186                 priv->rxqs = NULL;
187         }
188         if (priv->txqs != NULL) {
189                 /* XXX race condition if mlx5_tx_burst() is still running. */
190                 usleep(1000);
191                 for (i = 0; (i != priv->txqs_n); ++i)
192                         mlx5_txq_release(dev, i);
193                 priv->txqs_n = 0;
194                 priv->txqs = NULL;
195         }
196         if (priv->pd != NULL) {
197                 assert(priv->ctx != NULL);
198                 claim_zero(mlx5_glue->dealloc_pd(priv->pd));
199                 claim_zero(mlx5_glue->close_device(priv->ctx));
200         } else
201                 assert(priv->ctx == NULL);
202         if (priv->rss_conf.rss_key != NULL)
203                 rte_free(priv->rss_conf.rss_key);
204         if (priv->reta_idx != NULL)
205                 rte_free(priv->reta_idx);
206         if (priv->primary_socket)
207                 mlx5_socket_uninit(dev);
208         ret = mlx5_hrxq_ibv_verify(dev);
209         if (ret)
210                 DRV_LOG(WARNING, "port %u some hash Rx queue still remain",
211                         dev->data->port_id);
212         ret = mlx5_ind_table_ibv_verify(dev);
213         if (ret)
214                 DRV_LOG(WARNING, "port %u some indirection table still remain",
215                         dev->data->port_id);
216         ret = mlx5_rxq_ibv_verify(dev);
217         if (ret)
218                 DRV_LOG(WARNING, "port %u some Verbs Rx queue still remain",
219                         dev->data->port_id);
220         ret = mlx5_rxq_verify(dev);
221         if (ret)
222                 DRV_LOG(WARNING, "port %u some Rx queues still remain",
223                         dev->data->port_id);
224         ret = mlx5_txq_ibv_verify(dev);
225         if (ret)
226                 DRV_LOG(WARNING, "port %u some Verbs Tx queue still remain",
227                         dev->data->port_id);
228         ret = mlx5_txq_verify(dev);
229         if (ret)
230                 DRV_LOG(WARNING, "port %u some Tx queues still remain",
231                         dev->data->port_id);
232         ret = mlx5_flow_verify(dev);
233         if (ret)
234                 DRV_LOG(WARNING, "port %u some flows still remain",
235                         dev->data->port_id);
236         ret = mlx5_mr_verify(dev);
237         if (ret)
238                 DRV_LOG(WARNING, "port %u some memory region still remain",
239                         dev->data->port_id);
240         memset(priv, 0, sizeof(*priv));
241 }
242
243 const struct eth_dev_ops mlx5_dev_ops = {
244         .dev_configure = mlx5_dev_configure,
245         .dev_start = mlx5_dev_start,
246         .dev_stop = mlx5_dev_stop,
247         .dev_set_link_down = mlx5_set_link_down,
248         .dev_set_link_up = mlx5_set_link_up,
249         .dev_close = mlx5_dev_close,
250         .promiscuous_enable = mlx5_promiscuous_enable,
251         .promiscuous_disable = mlx5_promiscuous_disable,
252         .allmulticast_enable = mlx5_allmulticast_enable,
253         .allmulticast_disable = mlx5_allmulticast_disable,
254         .link_update = mlx5_link_update,
255         .stats_get = mlx5_stats_get,
256         .stats_reset = mlx5_stats_reset,
257         .xstats_get = mlx5_xstats_get,
258         .xstats_reset = mlx5_xstats_reset,
259         .xstats_get_names = mlx5_xstats_get_names,
260         .dev_infos_get = mlx5_dev_infos_get,
261         .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
262         .vlan_filter_set = mlx5_vlan_filter_set,
263         .rx_queue_setup = mlx5_rx_queue_setup,
264         .tx_queue_setup = mlx5_tx_queue_setup,
265         .rx_queue_release = mlx5_rx_queue_release,
266         .tx_queue_release = mlx5_tx_queue_release,
267         .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
268         .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
269         .mac_addr_remove = mlx5_mac_addr_remove,
270         .mac_addr_add = mlx5_mac_addr_add,
271         .mac_addr_set = mlx5_mac_addr_set,
272         .mtu_set = mlx5_dev_set_mtu,
273         .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
274         .vlan_offload_set = mlx5_vlan_offload_set,
275         .reta_update = mlx5_dev_rss_reta_update,
276         .reta_query = mlx5_dev_rss_reta_query,
277         .rss_hash_update = mlx5_rss_hash_update,
278         .rss_hash_conf_get = mlx5_rss_hash_conf_get,
279         .filter_ctrl = mlx5_dev_filter_ctrl,
280         .rx_descriptor_status = mlx5_rx_descriptor_status,
281         .tx_descriptor_status = mlx5_tx_descriptor_status,
282         .rx_queue_intr_enable = mlx5_rx_intr_enable,
283         .rx_queue_intr_disable = mlx5_rx_intr_disable,
284         .is_removed = mlx5_is_removed,
285 };
286
287 static const struct eth_dev_ops mlx5_dev_sec_ops = {
288         .stats_get = mlx5_stats_get,
289         .stats_reset = mlx5_stats_reset,
290         .xstats_get = mlx5_xstats_get,
291         .xstats_reset = mlx5_xstats_reset,
292         .xstats_get_names = mlx5_xstats_get_names,
293         .dev_infos_get = mlx5_dev_infos_get,
294         .rx_descriptor_status = mlx5_rx_descriptor_status,
295         .tx_descriptor_status = mlx5_tx_descriptor_status,
296 };
297
298 /* Available operators in flow isolated mode. */
299 const struct eth_dev_ops mlx5_dev_ops_isolate = {
300         .dev_configure = mlx5_dev_configure,
301         .dev_start = mlx5_dev_start,
302         .dev_stop = mlx5_dev_stop,
303         .dev_set_link_down = mlx5_set_link_down,
304         .dev_set_link_up = mlx5_set_link_up,
305         .dev_close = mlx5_dev_close,
306         .link_update = mlx5_link_update,
307         .stats_get = mlx5_stats_get,
308         .stats_reset = mlx5_stats_reset,
309         .xstats_get = mlx5_xstats_get,
310         .xstats_reset = mlx5_xstats_reset,
311         .xstats_get_names = mlx5_xstats_get_names,
312         .dev_infos_get = mlx5_dev_infos_get,
313         .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
314         .vlan_filter_set = mlx5_vlan_filter_set,
315         .rx_queue_setup = mlx5_rx_queue_setup,
316         .tx_queue_setup = mlx5_tx_queue_setup,
317         .rx_queue_release = mlx5_rx_queue_release,
318         .tx_queue_release = mlx5_tx_queue_release,
319         .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
320         .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
321         .mac_addr_remove = mlx5_mac_addr_remove,
322         .mac_addr_add = mlx5_mac_addr_add,
323         .mac_addr_set = mlx5_mac_addr_set,
324         .mtu_set = mlx5_dev_set_mtu,
325         .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
326         .vlan_offload_set = mlx5_vlan_offload_set,
327         .filter_ctrl = mlx5_dev_filter_ctrl,
328         .rx_descriptor_status = mlx5_rx_descriptor_status,
329         .tx_descriptor_status = mlx5_tx_descriptor_status,
330         .rx_queue_intr_enable = mlx5_rx_intr_enable,
331         .rx_queue_intr_disable = mlx5_rx_intr_disable,
332         .is_removed = mlx5_is_removed,
333 };
334
335 static struct {
336         struct rte_pci_addr pci_addr; /* associated PCI address */
337         uint32_t ports; /* physical ports bitfield. */
338 } mlx5_dev[32];
339
340 /**
341  * Get device index in mlx5_dev[] from PCI bus address.
342  *
343  * @param[in] pci_addr
344  *   PCI bus address to look for.
345  *
346  * @return
347  *   mlx5_dev[] index on success, -1 on failure.
348  */
349 static int
350 mlx5_dev_idx(struct rte_pci_addr *pci_addr)
351 {
352         unsigned int i;
353         int ret = -1;
354
355         assert(pci_addr != NULL);
356         for (i = 0; (i != RTE_DIM(mlx5_dev)); ++i) {
357                 if ((mlx5_dev[i].pci_addr.domain == pci_addr->domain) &&
358                     (mlx5_dev[i].pci_addr.bus == pci_addr->bus) &&
359                     (mlx5_dev[i].pci_addr.devid == pci_addr->devid) &&
360                     (mlx5_dev[i].pci_addr.function == pci_addr->function))
361                         return i;
362                 if ((mlx5_dev[i].ports == 0) && (ret == -1))
363                         ret = i;
364         }
365         return ret;
366 }
367
368 /**
369  * Verify and store value for device argument.
370  *
371  * @param[in] key
372  *   Key argument to verify.
373  * @param[in] val
374  *   Value associated with key.
375  * @param opaque
376  *   User data.
377  *
378  * @return
379  *   0 on success, a negative errno value otherwise and rte_errno is set.
380  */
381 static int
382 mlx5_args_check(const char *key, const char *val, void *opaque)
383 {
384         struct mlx5_dev_config *config = opaque;
385         unsigned long tmp;
386
387         errno = 0;
388         tmp = strtoul(val, NULL, 0);
389         if (errno) {
390                 rte_errno = errno;
391                 DRV_LOG(WARNING, "%s: \"%s\" is not a valid integer", key, val);
392                 return -rte_errno;
393         }
394         if (strcmp(MLX5_RXQ_CQE_COMP_EN, key) == 0) {
395                 config->cqe_comp = !!tmp;
396         } else if (strcmp(MLX5_TXQ_INLINE, key) == 0) {
397                 config->txq_inline = tmp;
398         } else if (strcmp(MLX5_TXQS_MIN_INLINE, key) == 0) {
399                 config->txqs_inline = tmp;
400         } else if (strcmp(MLX5_TXQ_MPW_EN, key) == 0) {
401                 config->mps = !!tmp ? config->mps : 0;
402         } else if (strcmp(MLX5_TXQ_MPW_HDR_DSEG_EN, key) == 0) {
403                 config->mpw_hdr_dseg = !!tmp;
404         } else if (strcmp(MLX5_TXQ_MAX_INLINE_LEN, key) == 0) {
405                 config->inline_max_packet_sz = tmp;
406         } else if (strcmp(MLX5_TX_VEC_EN, key) == 0) {
407                 config->tx_vec_en = !!tmp;
408         } else if (strcmp(MLX5_RX_VEC_EN, key) == 0) {
409                 config->rx_vec_en = !!tmp;
410         } else {
411                 DRV_LOG(WARNING, "%s: unknown parameter", key);
412                 rte_errno = EINVAL;
413                 return -rte_errno;
414         }
415         return 0;
416 }
417
418 /**
419  * Parse device parameters.
420  *
421  * @param config
422  *   Pointer to device configuration structure.
423  * @param devargs
424  *   Device arguments structure.
425  *
426  * @return
427  *   0 on success, a negative errno value otherwise and rte_errno is set.
428  */
429 static int
430 mlx5_args(struct mlx5_dev_config *config, struct rte_devargs *devargs)
431 {
432         const char **params = (const char *[]){
433                 MLX5_RXQ_CQE_COMP_EN,
434                 MLX5_TXQ_INLINE,
435                 MLX5_TXQS_MIN_INLINE,
436                 MLX5_TXQ_MPW_EN,
437                 MLX5_TXQ_MPW_HDR_DSEG_EN,
438                 MLX5_TXQ_MAX_INLINE_LEN,
439                 MLX5_TX_VEC_EN,
440                 MLX5_RX_VEC_EN,
441                 NULL,
442         };
443         struct rte_kvargs *kvlist;
444         int ret = 0;
445         int i;
446
447         if (devargs == NULL)
448                 return 0;
449         /* Following UGLY cast is done to pass checkpatch. */
450         kvlist = rte_kvargs_parse(devargs->args, params);
451         if (kvlist == NULL)
452                 return 0;
453         /* Process parameters. */
454         for (i = 0; (params[i] != NULL); ++i) {
455                 if (rte_kvargs_count(kvlist, params[i])) {
456                         ret = rte_kvargs_process(kvlist, params[i],
457                                                  mlx5_args_check, config);
458                         if (ret) {
459                                 rte_errno = EINVAL;
460                                 rte_kvargs_free(kvlist);
461                                 return -rte_errno;
462                         }
463                 }
464         }
465         rte_kvargs_free(kvlist);
466         return 0;
467 }
468
469 static struct rte_pci_driver mlx5_driver;
470
471 /*
472  * Reserved UAR address space for TXQ UAR(hw doorbell) mapping, process
473  * local resource used by both primary and secondary to avoid duplicate
474  * reservation.
475  * The space has to be available on both primary and secondary process,
476  * TXQ UAR maps to this area using fixed mmap w/o double check.
477  */
478 static void *uar_base;
479
480 static int
481 find_lower_va_bound(const struct rte_memseg *ms, void *arg)
482 {
483         void **addr = arg;
484
485         if (*addr == NULL)
486                 *addr = ms->addr;
487         else
488                 *addr = RTE_MIN(*addr, ms->addr);
489
490         return 0;
491 }
492
493 /**
494  * Reserve UAR address space for primary process.
495  *
496  * @param[in] dev
497  *   Pointer to Ethernet device.
498  *
499  * @return
500  *   0 on success, a negative errno value otherwise and rte_errno is set.
501  */
502 static int
503 mlx5_uar_init_primary(struct rte_eth_dev *dev)
504 {
505         struct priv *priv = dev->data->dev_private;
506         void *addr = (void *)0;
507
508         if (uar_base) { /* UAR address space mapped. */
509                 priv->uar_base = uar_base;
510                 return 0;
511         }
512         /* find out lower bound of hugepage segments */
513         rte_memseg_walk(find_lower_va_bound, &addr);
514
515         /* keep distance to hugepages to minimize potential conflicts. */
516         addr = RTE_PTR_SUB(addr, MLX5_UAR_OFFSET + MLX5_UAR_SIZE);
517         /* anonymous mmap, no real memory consumption. */
518         addr = mmap(addr, MLX5_UAR_SIZE,
519                     PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
520         if (addr == MAP_FAILED) {
521                 DRV_LOG(ERR,
522                         "port %u failed to reserve UAR address space, please"
523                         " adjust MLX5_UAR_SIZE or try --base-virtaddr",
524                         dev->data->port_id);
525                 rte_errno = ENOMEM;
526                 return -rte_errno;
527         }
528         /* Accept either same addr or a new addr returned from mmap if target
529          * range occupied.
530          */
531         DRV_LOG(INFO, "port %u reserved UAR address space: %p",
532                 dev->data->port_id, addr);
533         priv->uar_base = addr; /* for primary and secondary UAR re-mmap. */
534         uar_base = addr; /* process local, don't reserve again. */
535         return 0;
536 }
537
538 /**
539  * Reserve UAR address space for secondary process, align with
540  * primary process.
541  *
542  * @param[in] dev
543  *   Pointer to Ethernet device.
544  *
545  * @return
546  *   0 on success, a negative errno value otherwise and rte_errno is set.
547  */
548 static int
549 mlx5_uar_init_secondary(struct rte_eth_dev *dev)
550 {
551         struct priv *priv = dev->data->dev_private;
552         void *addr;
553
554         assert(priv->uar_base);
555         if (uar_base) { /* already reserved. */
556                 assert(uar_base == priv->uar_base);
557                 return 0;
558         }
559         /* anonymous mmap, no real memory consumption. */
560         addr = mmap(priv->uar_base, MLX5_UAR_SIZE,
561                     PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
562         if (addr == MAP_FAILED) {
563                 DRV_LOG(ERR, "port %u UAR mmap failed: %p size: %llu",
564                         dev->data->port_id, priv->uar_base, MLX5_UAR_SIZE);
565                 rte_errno = ENXIO;
566                 return -rte_errno;
567         }
568         if (priv->uar_base != addr) {
569                 DRV_LOG(ERR,
570                         "port %u UAR address %p size %llu occupied, please"
571                         " adjust MLX5_UAR_OFFSET or try EAL parameter"
572                         " --base-virtaddr",
573                         dev->data->port_id, priv->uar_base, MLX5_UAR_SIZE);
574                 rte_errno = ENXIO;
575                 return -rte_errno;
576         }
577         uar_base = addr; /* process local, don't reserve again */
578         DRV_LOG(INFO, "port %u reserved UAR address space: %p",
579                 dev->data->port_id, addr);
580         return 0;
581 }
582
583 /**
584  * DPDK callback to register a PCI device.
585  *
586  * This function creates an Ethernet device for each port of a given
587  * PCI device.
588  *
589  * @param[in] pci_drv
590  *   PCI driver structure (mlx5_driver).
591  * @param[in] pci_dev
592  *   PCI device information.
593  *
594  * @return
595  *   0 on success, a negative errno value otherwise and rte_errno is set.
596  */
597 static int
598 mlx5_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
599                struct rte_pci_device *pci_dev)
600 {
601         struct ibv_device **list = NULL;
602         struct ibv_device *ibv_dev;
603         int err = 0;
604         struct ibv_context *attr_ctx = NULL;
605         struct ibv_device_attr_ex device_attr;
606         unsigned int mps;
607         unsigned int cqe_comp;
608         unsigned int tunnel_en = 0;
609         int idx;
610         int i;
611         struct mlx5dv_context attrs_out = {0};
612 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
613         struct ibv_counter_set_description cs_desc;
614 #endif
615
616         assert(pci_drv == &mlx5_driver);
617         /* Get mlx5_dev[] index. */
618         idx = mlx5_dev_idx(&pci_dev->addr);
619         if (idx == -1) {
620                 DRV_LOG(ERR, "this driver cannot support any more adapters");
621                 err = ENOMEM;
622                 goto error;
623         }
624         DRV_LOG(DEBUG, "using driver device index %d", idx);
625         /* Save PCI address. */
626         mlx5_dev[idx].pci_addr = pci_dev->addr;
627         list = mlx5_glue->get_device_list(&i);
628         if (list == NULL) {
629                 assert(errno);
630                 err = errno;
631                 if (errno == ENOSYS)
632                         DRV_LOG(ERR,
633                                 "cannot list devices, is ib_uverbs loaded?");
634                 goto error;
635         }
636         assert(i >= 0);
637         /*
638          * For each listed device, check related sysfs entry against
639          * the provided PCI ID.
640          */
641         while (i != 0) {
642                 struct rte_pci_addr pci_addr;
643
644                 --i;
645                 DRV_LOG(DEBUG, "checking device \"%s\"", list[i]->name);
646                 if (mlx5_ibv_device_to_pci_addr(list[i], &pci_addr))
647                         continue;
648                 if ((pci_dev->addr.domain != pci_addr.domain) ||
649                     (pci_dev->addr.bus != pci_addr.bus) ||
650                     (pci_dev->addr.devid != pci_addr.devid) ||
651                     (pci_dev->addr.function != pci_addr.function))
652                         continue;
653                 DRV_LOG(INFO, "PCI information matches, using device \"%s\"",
654                         list[i]->name);
655                 attr_ctx = mlx5_glue->open_device(list[i]);
656                 rte_errno = errno;
657                 err = rte_errno;
658                 break;
659         }
660         if (attr_ctx == NULL) {
661                 mlx5_glue->free_device_list(list);
662                 switch (err) {
663                 case 0:
664                         DRV_LOG(ERR,
665                                 "cannot access device, is mlx5_ib loaded?");
666                         err = ENODEV;
667                         goto error;
668                 case EINVAL:
669                         DRV_LOG(ERR,
670                                 "cannot use device, are drivers up to date?");
671                         goto error;
672                 }
673         }
674         ibv_dev = list[i];
675         DRV_LOG(DEBUG, "device opened");
676         /*
677          * Multi-packet send is supported by ConnectX-4 Lx PF as well
678          * as all ConnectX-5 devices.
679          */
680 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
681         attrs_out.comp_mask |= MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS;
682 #endif
683         mlx5_glue->dv_query_device(attr_ctx, &attrs_out);
684         if (attrs_out.flags & MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED) {
685                 if (attrs_out.flags & MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW) {
686                         DRV_LOG(DEBUG, "enhanced MPW is supported");
687                         mps = MLX5_MPW_ENHANCED;
688                 } else {
689                         DRV_LOG(DEBUG, "MPW is supported");
690                         mps = MLX5_MPW;
691                 }
692         } else {
693                 DRV_LOG(DEBUG, "MPW isn't supported");
694                 mps = MLX5_MPW_DISABLED;
695         }
696         if (RTE_CACHE_LINE_SIZE == 128 &&
697             !(attrs_out.flags & MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP))
698                 cqe_comp = 0;
699         else
700                 cqe_comp = 1;
701 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
702         if (attrs_out.comp_mask & MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS) {
703                 tunnel_en = ((attrs_out.tunnel_offloads_caps &
704                               MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_VXLAN) &&
705                              (attrs_out.tunnel_offloads_caps &
706                               MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_GRE));
707         }
708         DRV_LOG(DEBUG, "tunnel offloading is %ssupported",
709                 tunnel_en ? "" : "not ");
710 #else
711         DRV_LOG(WARNING,
712                 "tunnel offloading disabled due to old OFED/rdma-core version");
713 #endif
714         if (mlx5_glue->query_device_ex(attr_ctx, NULL, &device_attr)) {
715                 err = errno;
716                 goto error;
717         }
718         DRV_LOG(INFO, "%u port(s) detected",
719                 device_attr.orig_attr.phys_port_cnt);
720         for (i = 0; i < device_attr.orig_attr.phys_port_cnt; i++) {
721                 char name[RTE_ETH_NAME_MAX_LEN];
722                 int len;
723                 uint32_t port = i + 1; /* ports are indexed from one */
724                 uint32_t test = (1 << i);
725                 struct ibv_context *ctx = NULL;
726                 struct ibv_port_attr port_attr;
727                 struct ibv_pd *pd = NULL;
728                 struct priv *priv = NULL;
729                 struct rte_eth_dev *eth_dev = NULL;
730                 struct ibv_device_attr_ex device_attr_ex;
731                 struct ether_addr mac;
732                 struct mlx5_dev_config config = {
733                         .cqe_comp = cqe_comp,
734                         .mps = mps,
735                         .tunnel_en = tunnel_en,
736                         .tx_vec_en = 1,
737                         .rx_vec_en = 1,
738                         .mpw_hdr_dseg = 0,
739                         .txq_inline = MLX5_ARG_UNSET,
740                         .txqs_inline = MLX5_ARG_UNSET,
741                         .inline_max_packet_sz = MLX5_ARG_UNSET,
742                 };
743
744                 len = snprintf(name, sizeof(name), PCI_PRI_FMT,
745                          pci_dev->addr.domain, pci_dev->addr.bus,
746                          pci_dev->addr.devid, pci_dev->addr.function);
747                 if (device_attr.orig_attr.phys_port_cnt > 1)
748                         snprintf(name + len, sizeof(name), " port %u", i);
749                 mlx5_dev[idx].ports |= test;
750                 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
751                         eth_dev = rte_eth_dev_attach_secondary(name);
752                         if (eth_dev == NULL) {
753                                 DRV_LOG(ERR, "can not attach rte ethdev");
754                                 rte_errno = ENOMEM;
755                                 err = rte_errno;
756                                 goto error;
757                         }
758                         eth_dev->device = &pci_dev->device;
759                         eth_dev->dev_ops = &mlx5_dev_sec_ops;
760                         err = mlx5_uar_init_secondary(eth_dev);
761                         if (err)
762                                 goto error;
763                         /* Receive command fd from primary process */
764                         err = mlx5_socket_connect(eth_dev);
765                         if (err)
766                                 goto error;
767                         /* Remap UAR for Tx queues. */
768                         err = mlx5_tx_uar_remap(eth_dev, err);
769                         if (err)
770                                 goto error;
771                         /*
772                          * Ethdev pointer is still required as input since
773                          * the primary device is not accessible from the
774                          * secondary process.
775                          */
776                         eth_dev->rx_pkt_burst =
777                                 mlx5_select_rx_function(eth_dev);
778                         eth_dev->tx_pkt_burst =
779                                 mlx5_select_tx_function(eth_dev);
780                         continue;
781                 }
782                 DRV_LOG(DEBUG, "using port %u (%08" PRIx32 ")", port, test);
783                 ctx = mlx5_glue->open_device(ibv_dev);
784                 if (ctx == NULL) {
785                         err = ENODEV;
786                         goto port_error;
787                 }
788                 /* Check port status. */
789                 err = mlx5_glue->query_port(ctx, port, &port_attr);
790                 if (err) {
791                         DRV_LOG(ERR, "port query failed: %s", strerror(err));
792                         goto port_error;
793                 }
794                 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
795                         DRV_LOG(ERR,
796                                 "port %d is not configured in Ethernet mode",
797                                 port);
798                         err = EINVAL;
799                         goto port_error;
800                 }
801                 if (port_attr.state != IBV_PORT_ACTIVE)
802                         DRV_LOG(DEBUG, "port %d is not active: \"%s\" (%d)",
803                                 port,
804                                 mlx5_glue->port_state_str(port_attr.state),
805                                 port_attr.state);
806                 /* Allocate protection domain. */
807                 pd = mlx5_glue->alloc_pd(ctx);
808                 if (pd == NULL) {
809                         DRV_LOG(ERR, "PD allocation failure");
810                         err = ENOMEM;
811                         goto port_error;
812                 }
813                 mlx5_dev[idx].ports |= test;
814                 /* from rte_ethdev.c */
815                 priv = rte_zmalloc("ethdev private structure",
816                                    sizeof(*priv),
817                                    RTE_CACHE_LINE_SIZE);
818                 if (priv == NULL) {
819                         DRV_LOG(ERR, "priv allocation failure");
820                         err = ENOMEM;
821                         goto port_error;
822                 }
823                 priv->ctx = ctx;
824                 strncpy(priv->ibdev_path, priv->ctx->device->ibdev_path,
825                         sizeof(priv->ibdev_path));
826                 priv->device_attr = device_attr;
827                 priv->port = port;
828                 priv->pd = pd;
829                 priv->mtu = ETHER_MTU;
830                 err = mlx5_args(&config, pci_dev->device.devargs);
831                 if (err) {
832                         DRV_LOG(ERR, "failed to process device arguments: %s",
833                                 strerror(err));
834                         goto port_error;
835                 }
836                 if (mlx5_glue->query_device_ex(ctx, NULL, &device_attr_ex)) {
837                         DRV_LOG(ERR, "ibv_query_device_ex() failed");
838                         err = errno;
839                         goto port_error;
840                 }
841                 config.hw_csum = !!(device_attr_ex.device_cap_flags_ex &
842                                     IBV_DEVICE_RAW_IP_CSUM);
843                 DRV_LOG(DEBUG, "checksum offloading is %ssupported",
844                         (config.hw_csum ? "" : "not "));
845 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
846                 config.flow_counter_en = !!(device_attr.max_counter_sets);
847                 mlx5_glue->describe_counter_set(ctx, 0, &cs_desc);
848                 DRV_LOG(DEBUG,
849                         "counter type = %d, num of cs = %ld, attributes = %d",
850                         cs_desc.counter_type, cs_desc.num_of_cs,
851                         cs_desc.attributes);
852 #endif
853                 config.ind_table_max_size =
854                         device_attr_ex.rss_caps.max_rwq_indirection_table_size;
855                 /* Remove this check once DPDK supports larger/variable
856                  * indirection tables. */
857                 if (config.ind_table_max_size >
858                                 (unsigned int)ETH_RSS_RETA_SIZE_512)
859                         config.ind_table_max_size = ETH_RSS_RETA_SIZE_512;
860                 DRV_LOG(DEBUG, "maximum Rx indirection table size is %u",
861                         config.ind_table_max_size);
862                 config.hw_vlan_strip = !!(device_attr_ex.raw_packet_caps &
863                                          IBV_RAW_PACKET_CAP_CVLAN_STRIPPING);
864                 DRV_LOG(DEBUG, "VLAN stripping is %ssupported",
865                         (config.hw_vlan_strip ? "" : "not "));
866
867                 config.hw_fcs_strip = !!(device_attr_ex.raw_packet_caps &
868                                          IBV_RAW_PACKET_CAP_SCATTER_FCS);
869                 DRV_LOG(DEBUG, "FCS stripping configuration is %ssupported",
870                         (config.hw_fcs_strip ? "" : "not "));
871
872 #ifdef HAVE_IBV_WQ_FLAG_RX_END_PADDING
873                 config.hw_padding = !!device_attr_ex.rx_pad_end_addr_align;
874 #endif
875                 DRV_LOG(DEBUG,
876                         "hardware Rx end alignment padding is %ssupported",
877                         (config.hw_padding ? "" : "not "));
878                 config.tso = ((device_attr_ex.tso_caps.max_tso > 0) &&
879                               (device_attr_ex.tso_caps.supported_qpts &
880                               (1 << IBV_QPT_RAW_PACKET)));
881                 if (config.tso)
882                         config.tso_max_payload_sz =
883                                         device_attr_ex.tso_caps.max_tso;
884                 if (config.mps && !mps) {
885                         DRV_LOG(ERR,
886                                 "multi-packet send not supported on this device"
887                                 " (" MLX5_TXQ_MPW_EN ")");
888                         err = ENOTSUP;
889                         goto port_error;
890                 }
891                 DRV_LOG(INFO, "%s MPS is %s",
892                         config.mps == MLX5_MPW_ENHANCED ? "enhanced " : "",
893                         config.mps != MLX5_MPW_DISABLED ? "enabled" :
894                         "disabled");
895                 if (config.cqe_comp && !cqe_comp) {
896                         DRV_LOG(WARNING, "Rx CQE compression isn't supported");
897                         config.cqe_comp = 0;
898                 }
899                 eth_dev = rte_eth_dev_allocate(name);
900                 if (eth_dev == NULL) {
901                         DRV_LOG(ERR, "can not allocate rte ethdev");
902                         err = ENOMEM;
903                         goto port_error;
904                 }
905                 eth_dev->data->dev_private = priv;
906                 priv->dev = eth_dev;
907                 eth_dev->data->mac_addrs = priv->mac;
908                 eth_dev->device = &pci_dev->device;
909                 rte_eth_copy_pci_info(eth_dev, pci_dev);
910                 eth_dev->device->driver = &mlx5_driver.driver;
911                 err = mlx5_uar_init_primary(eth_dev);
912                 if (err)
913                         goto port_error;
914                 /* Configure the first MAC address by default. */
915                 if (mlx5_get_mac(eth_dev, &mac.addr_bytes)) {
916                         DRV_LOG(ERR,
917                                 "port %u cannot get MAC address, is mlx5_en"
918                                 " loaded? (errno: %s)",
919                                 eth_dev->data->port_id, strerror(errno));
920                         err = ENODEV;
921                         goto port_error;
922                 }
923                 DRV_LOG(INFO,
924                         "port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
925                         eth_dev->data->port_id,
926                         mac.addr_bytes[0], mac.addr_bytes[1],
927                         mac.addr_bytes[2], mac.addr_bytes[3],
928                         mac.addr_bytes[4], mac.addr_bytes[5]);
929 #ifndef NDEBUG
930                 {
931                         char ifname[IF_NAMESIZE];
932
933                         if (mlx5_get_ifname(eth_dev, &ifname) == 0)
934                                 DRV_LOG(DEBUG, "port %u ifname is \"%s\"",
935                                         eth_dev->data->port_id, ifname);
936                         else
937                                 DRV_LOG(DEBUG, "port %u ifname is unknown",
938                                         eth_dev->data->port_id);
939                 }
940 #endif
941                 /* Get actual MTU if possible. */
942                 err = mlx5_get_mtu(eth_dev, &priv->mtu);
943                 if (err)
944                         goto port_error;
945                 DRV_LOG(DEBUG, "port %u MTU is %u", eth_dev->data->port_id,
946                         priv->mtu);
947                 /*
948                  * Initialize burst functions to prevent crashes before link-up.
949                  */
950                 eth_dev->rx_pkt_burst = removed_rx_burst;
951                 eth_dev->tx_pkt_burst = removed_tx_burst;
952                 eth_dev->dev_ops = &mlx5_dev_ops;
953                 /* Register MAC address. */
954                 claim_zero(mlx5_mac_addr_add(eth_dev, &mac, 0, 0));
955                 TAILQ_INIT(&priv->flows);
956                 TAILQ_INIT(&priv->ctrl_flows);
957                 /* Hint libmlx5 to use PMD allocator for data plane resources */
958                 struct mlx5dv_ctx_allocators alctr = {
959                         .alloc = &mlx5_alloc_verbs_buf,
960                         .free = &mlx5_free_verbs_buf,
961                         .data = priv,
962                 };
963                 mlx5_glue->dv_set_context_attr(ctx,
964                                                MLX5DV_CTX_ATTR_BUF_ALLOCATORS,
965                                                (void *)((uintptr_t)&alctr));
966                 /* Bring Ethernet device up. */
967                 DRV_LOG(DEBUG, "port %u forcing Ethernet interface up",
968                         eth_dev->data->port_id);
969                 mlx5_set_link_up(eth_dev);
970                 /* Store device configuration on private structure. */
971                 priv->config = config;
972                 continue;
973 port_error:
974                 if (priv)
975                         rte_free(priv);
976                 if (pd)
977                         claim_zero(mlx5_glue->dealloc_pd(pd));
978                 if (ctx)
979                         claim_zero(mlx5_glue->close_device(ctx));
980                 break;
981         }
982         /*
983          * XXX if something went wrong in the loop above, there is a resource
984          * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
985          * long as the dpdk does not provide a way to deallocate a ethdev and a
986          * way to enumerate the registered ethdevs to free the previous ones.
987          */
988         /* no port found, complain */
989         if (!mlx5_dev[idx].ports) {
990                 rte_errno = ENODEV;
991                 err = rte_errno;
992         }
993 error:
994         if (attr_ctx)
995                 claim_zero(mlx5_glue->close_device(attr_ctx));
996         if (list)
997                 mlx5_glue->free_device_list(list);
998         if (err) {
999                 rte_errno = err;
1000                 return -rte_errno;
1001         }
1002         return 0;
1003 }
1004
1005 static const struct rte_pci_id mlx5_pci_id_map[] = {
1006         {
1007                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1008                                PCI_DEVICE_ID_MELLANOX_CONNECTX4)
1009         },
1010         {
1011                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1012                                PCI_DEVICE_ID_MELLANOX_CONNECTX4VF)
1013         },
1014         {
1015                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1016                                PCI_DEVICE_ID_MELLANOX_CONNECTX4LX)
1017         },
1018         {
1019                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1020                                PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF)
1021         },
1022         {
1023                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1024                                PCI_DEVICE_ID_MELLANOX_CONNECTX5)
1025         },
1026         {
1027                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1028                                PCI_DEVICE_ID_MELLANOX_CONNECTX5VF)
1029         },
1030         {
1031                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1032                                PCI_DEVICE_ID_MELLANOX_CONNECTX5EX)
1033         },
1034         {
1035                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1036                                PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF)
1037         },
1038         {
1039                 .vendor_id = 0
1040         }
1041 };
1042
1043 static struct rte_pci_driver mlx5_driver = {
1044         .driver = {
1045                 .name = MLX5_DRIVER_NAME
1046         },
1047         .id_table = mlx5_pci_id_map,
1048         .probe = mlx5_pci_probe,
1049         .drv_flags = RTE_PCI_DRV_INTR_LSC | RTE_PCI_DRV_INTR_RMV,
1050 };
1051
1052 #ifdef RTE_LIBRTE_MLX5_DLOPEN_DEPS
1053
1054 /**
1055  * Suffix RTE_EAL_PMD_PATH with "-glue".
1056  *
1057  * This function performs a sanity check on RTE_EAL_PMD_PATH before
1058  * suffixing its last component.
1059  *
1060  * @param buf[out]
1061  *   Output buffer, should be large enough otherwise NULL is returned.
1062  * @param size
1063  *   Size of @p out.
1064  *
1065  * @return
1066  *   Pointer to @p buf or @p NULL in case suffix cannot be appended.
1067  */
1068 static char *
1069 mlx5_glue_path(char *buf, size_t size)
1070 {
1071         static const char *const bad[] = { "/", ".", "..", NULL };
1072         const char *path = RTE_EAL_PMD_PATH;
1073         size_t len = strlen(path);
1074         size_t off;
1075         int i;
1076
1077         while (len && path[len - 1] == '/')
1078                 --len;
1079         for (off = len; off && path[off - 1] != '/'; --off)
1080                 ;
1081         for (i = 0; bad[i]; ++i)
1082                 if (!strncmp(path + off, bad[i], (int)(len - off)))
1083                         goto error;
1084         i = snprintf(buf, size, "%.*s-glue", (int)len, path);
1085         if (i == -1 || (size_t)i >= size)
1086                 goto error;
1087         return buf;
1088 error:
1089         DRV_LOG(ERR,
1090                 "unable to append \"-glue\" to last component of"
1091                 " RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"),"
1092                 " please re-configure DPDK");
1093         return NULL;
1094 }
1095
1096 /**
1097  * Initialization routine for run-time dependency on rdma-core.
1098  */
1099 static int
1100 mlx5_glue_init(void)
1101 {
1102         char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
1103         const char *path[] = {
1104                 /*
1105                  * A basic security check is necessary before trusting
1106                  * MLX5_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
1107                  */
1108                 (geteuid() == getuid() && getegid() == getgid() ?
1109                  getenv("MLX5_GLUE_PATH") : NULL),
1110                 /*
1111                  * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
1112                  * variant, otherwise let dlopen() look up libraries on its
1113                  * own.
1114                  */
1115                 (*RTE_EAL_PMD_PATH ?
1116                  mlx5_glue_path(glue_path, sizeof(glue_path)) : ""),
1117         };
1118         unsigned int i = 0;
1119         void *handle = NULL;
1120         void **sym;
1121         const char *dlmsg;
1122
1123         while (!handle && i != RTE_DIM(path)) {
1124                 const char *end;
1125                 size_t len;
1126                 int ret;
1127
1128                 if (!path[i]) {
1129                         ++i;
1130                         continue;
1131                 }
1132                 end = strpbrk(path[i], ":;");
1133                 if (!end)
1134                         end = path[i] + strlen(path[i]);
1135                 len = end - path[i];
1136                 ret = 0;
1137                 do {
1138                         char name[ret + 1];
1139
1140                         ret = snprintf(name, sizeof(name), "%.*s%s" MLX5_GLUE,
1141                                        (int)len, path[i],
1142                                        (!len || *(end - 1) == '/') ? "" : "/");
1143                         if (ret == -1)
1144                                 break;
1145                         if (sizeof(name) != (size_t)ret + 1)
1146                                 continue;
1147                         DRV_LOG(DEBUG, "looking for rdma-core glue as \"%s\"",
1148                                 name);
1149                         handle = dlopen(name, RTLD_LAZY);
1150                         break;
1151                 } while (1);
1152                 path[i] = end + 1;
1153                 if (!*end)
1154                         ++i;
1155         }
1156         if (!handle) {
1157                 rte_errno = EINVAL;
1158                 dlmsg = dlerror();
1159                 if (dlmsg)
1160                         DRV_LOG(WARNING, "cannot load glue library: %s", dlmsg);
1161                 goto glue_error;
1162         }
1163         sym = dlsym(handle, "mlx5_glue");
1164         if (!sym || !*sym) {
1165                 rte_errno = EINVAL;
1166                 dlmsg = dlerror();
1167                 if (dlmsg)
1168                         DRV_LOG(ERR, "cannot resolve glue symbol: %s", dlmsg);
1169                 goto glue_error;
1170         }
1171         mlx5_glue = *sym;
1172         return 0;
1173 glue_error:
1174         if (handle)
1175                 dlclose(handle);
1176         DRV_LOG(WARNING,
1177                 "cannot initialize PMD due to missing run-time dependency on"
1178                 " rdma-core libraries (libibverbs, libmlx5)");
1179         return -rte_errno;
1180 }
1181
1182 #endif
1183
1184 /**
1185  * Driver initialization routine.
1186  */
1187 RTE_INIT(rte_mlx5_pmd_init);
1188 static void
1189 rte_mlx5_pmd_init(void)
1190 {
1191         /* Build the static table for ptype conversion. */
1192         mlx5_set_ptype_table();
1193         /*
1194          * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
1195          * huge pages. Calling ibv_fork_init() during init allows
1196          * applications to use fork() safely for purposes other than
1197          * using this PMD, which is not supported in forked processes.
1198          */
1199         setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
1200         /* Match the size of Rx completion entry to the size of a cacheline. */
1201         if (RTE_CACHE_LINE_SIZE == 128)
1202                 setenv("MLX5_CQE_SIZE", "128", 0);
1203 #ifdef RTE_LIBRTE_MLX5_DLOPEN_DEPS
1204         if (mlx5_glue_init())
1205                 return;
1206         assert(mlx5_glue);
1207 #endif
1208 #ifndef NDEBUG
1209         /* Glue structure must not contain any NULL pointers. */
1210         {
1211                 unsigned int i;
1212
1213                 for (i = 0; i != sizeof(*mlx5_glue) / sizeof(void *); ++i)
1214                         assert(((const void *const *)mlx5_glue)[i]);
1215         }
1216 #endif
1217         if (strcmp(mlx5_glue->version, MLX5_GLUE_VERSION)) {
1218                 DRV_LOG(ERR,
1219                         "rdma-core glue \"%s\" mismatch: \"%s\" is required",
1220                         mlx5_glue->version, MLX5_GLUE_VERSION);
1221                 return;
1222         }
1223         mlx5_glue->fork_init();
1224         rte_pci_register(&mlx5_driver);
1225 }
1226
1227 RTE_PMD_EXPORT_NAME(net_mlx5, __COUNTER__);
1228 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5, mlx5_pci_id_map);
1229 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5, "* ib_uverbs & mlx5_core & mlx5_ib");
1230
1231 /** Initialize driver log type. */
1232 RTE_INIT(vdev_netvsc_init_log)
1233 {
1234         mlx5_logtype = rte_log_register("pmd.net.mlx5");
1235         if (mlx5_logtype >= 0)
1236                 rte_log_set_level(mlx5_logtype, RTE_LOG_NOTICE);
1237 }