cfcfb8a8fcaad8ead8c816c05437505ee6091f2d
[dpdk.git] / drivers / net / mlx4 / mlx4.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2012 6WIND S.A.
3  * Copyright 2012 Mellanox Technologies, Ltd
4  */
5
6 /**
7  * @file
8  * mlx4 driver initialization.
9  */
10
11 #include <errno.h>
12 #include <inttypes.h>
13 #include <stddef.h>
14 #include <stdint.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <sys/mman.h>
19 #include <unistd.h>
20 #ifdef RTE_IBVERBS_LINK_DLOPEN
21 #include <dlfcn.h>
22 #endif
23
24 /* Verbs headers do not support -pedantic. */
25 #ifdef PEDANTIC
26 #pragma GCC diagnostic ignored "-Wpedantic"
27 #endif
28 #include <infiniband/verbs.h>
29 #ifdef PEDANTIC
30 #pragma GCC diagnostic error "-Wpedantic"
31 #endif
32
33 #include <rte_common.h>
34 #include <rte_dev.h>
35 #include <rte_errno.h>
36 #include <rte_ethdev_driver.h>
37 #include <rte_ethdev_pci.h>
38 #include <rte_ether.h>
39 #include <rte_flow.h>
40 #include <rte_interrupts.h>
41 #include <rte_kvargs.h>
42 #include <rte_malloc.h>
43 #include <rte_mbuf.h>
44
45 #include "mlx4.h"
46 #include "mlx4_glue.h"
47 #include "mlx4_flow.h"
48 #include "mlx4_mr.h"
49 #include "mlx4_rxtx.h"
50 #include "mlx4_utils.h"
51
52 #ifdef MLX4_GLUE
53 const struct mlx4_glue *mlx4_glue;
54 #endif
55
56 static const char *MZ_MLX4_PMD_SHARED_DATA = "mlx4_pmd_shared_data";
57
58 /* Shared memory between primary and secondary processes. */
59 struct mlx4_shared_data *mlx4_shared_data;
60
61 /* Spinlock for mlx4_shared_data allocation. */
62 static rte_spinlock_t mlx4_shared_data_lock = RTE_SPINLOCK_INITIALIZER;
63
64 /* Process local data for secondary processes. */
65 static struct mlx4_local_data mlx4_local_data;
66
67 /** Configuration structure for device arguments. */
68 struct mlx4_conf {
69         struct {
70                 uint32_t present; /**< Bit-field for existing ports. */
71                 uint32_t enabled; /**< Bit-field for user-enabled ports. */
72         } ports;
73         int mr_ext_memseg_en;
74         /** Whether memseg should be extended for MR creation. */
75 };
76
77 /* Available parameters list. */
78 const char *pmd_mlx4_init_params[] = {
79         MLX4_PMD_PORT_KVARG,
80         MLX4_MR_EXT_MEMSEG_EN_KVARG,
81         NULL,
82 };
83
84 static void mlx4_dev_stop(struct rte_eth_dev *dev);
85
86 /**
87  * Initialize shared data between primary and secondary process.
88  *
89  * A memzone is reserved by primary process and secondary processes attach to
90  * the memzone.
91  *
92  * @return
93  *   0 on success, a negative errno value otherwise and rte_errno is set.
94  */
95 static int
96 mlx4_init_shared_data(void)
97 {
98         const struct rte_memzone *mz;
99         int ret = 0;
100
101         rte_spinlock_lock(&mlx4_shared_data_lock);
102         if (mlx4_shared_data == NULL) {
103                 if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
104                         /* Allocate shared memory. */
105                         mz = rte_memzone_reserve(MZ_MLX4_PMD_SHARED_DATA,
106                                                  sizeof(*mlx4_shared_data),
107                                                  SOCKET_ID_ANY, 0);
108                         if (mz == NULL) {
109                                 ERROR("Cannot allocate mlx4 shared data\n");
110                                 ret = -rte_errno;
111                                 goto error;
112                         }
113                         mlx4_shared_data = mz->addr;
114                         memset(mlx4_shared_data, 0, sizeof(*mlx4_shared_data));
115                         rte_spinlock_init(&mlx4_shared_data->lock);
116                 } else {
117                         /* Lookup allocated shared memory. */
118                         mz = rte_memzone_lookup(MZ_MLX4_PMD_SHARED_DATA);
119                         if (mz == NULL) {
120                                 ERROR("Cannot attach mlx4 shared data\n");
121                                 ret = -rte_errno;
122                                 goto error;
123                         }
124                         mlx4_shared_data = mz->addr;
125                         memset(&mlx4_local_data, 0, sizeof(mlx4_local_data));
126                 }
127         }
128 error:
129         rte_spinlock_unlock(&mlx4_shared_data_lock);
130         return ret;
131 }
132
133 #ifdef HAVE_IBV_MLX4_BUF_ALLOCATORS
134 /**
135  * Verbs callback to allocate a memory. This function should allocate the space
136  * according to the size provided residing inside a huge page.
137  * Please note that all allocation must respect the alignment from libmlx4
138  * (i.e. currently sysconf(_SC_PAGESIZE)).
139  *
140  * @param[in] size
141  *   The size in bytes of the memory to allocate.
142  * @param[in] data
143  *   A pointer to the callback data.
144  *
145  * @return
146  *   Allocated buffer, NULL otherwise and rte_errno is set.
147  */
148 static void *
149 mlx4_alloc_verbs_buf(size_t size, void *data)
150 {
151         struct mlx4_priv *priv = data;
152         void *ret;
153         size_t alignment = sysconf(_SC_PAGESIZE);
154         unsigned int socket = SOCKET_ID_ANY;
155
156         if (priv->verbs_alloc_ctx.type == MLX4_VERBS_ALLOC_TYPE_TX_QUEUE) {
157                 const struct txq *txq = priv->verbs_alloc_ctx.obj;
158
159                 socket = txq->socket;
160         } else if (priv->verbs_alloc_ctx.type ==
161                    MLX4_VERBS_ALLOC_TYPE_RX_QUEUE) {
162                 const struct rxq *rxq = priv->verbs_alloc_ctx.obj;
163
164                 socket = rxq->socket;
165         }
166         MLX4_ASSERT(data != NULL);
167         ret = rte_malloc_socket(__func__, size, alignment, socket);
168         if (!ret && size)
169                 rte_errno = ENOMEM;
170         return ret;
171 }
172
173 /**
174  * Verbs callback to free a memory.
175  *
176  * @param[in] ptr
177  *   A pointer to the memory to free.
178  * @param[in] data
179  *   A pointer to the callback data.
180  */
181 static void
182 mlx4_free_verbs_buf(void *ptr, void *data __rte_unused)
183 {
184         MLX4_ASSERT(data != NULL);
185         rte_free(ptr);
186 }
187 #endif
188
189 /**
190  * Initialize process private data structure.
191  *
192  * @param dev
193  *   Pointer to Ethernet device structure.
194  *
195  * @return
196  *   0 on success, a negative errno value otherwise and rte_errno is set.
197  */
198 static int
199 mlx4_proc_priv_init(struct rte_eth_dev *dev)
200 {
201         struct mlx4_proc_priv *ppriv;
202         size_t ppriv_size;
203
204         /*
205          * UAR register table follows the process private structure. BlueFlame
206          * registers for Tx queues are stored in the table.
207          */
208         ppriv_size = sizeof(struct mlx4_proc_priv) +
209                      dev->data->nb_tx_queues * sizeof(void *);
210         ppriv = rte_malloc_socket("mlx4_proc_priv", ppriv_size,
211                                   RTE_CACHE_LINE_SIZE, dev->device->numa_node);
212         if (!ppriv) {
213                 rte_errno = ENOMEM;
214                 return -rte_errno;
215         }
216         ppriv->uar_table_sz = ppriv_size;
217         dev->process_private = ppriv;
218         return 0;
219 }
220
221 /**
222  * Un-initialize process private data structure.
223  *
224  * @param dev
225  *   Pointer to Ethernet device structure.
226  */
227 static void
228 mlx4_proc_priv_uninit(struct rte_eth_dev *dev)
229 {
230         if (!dev->process_private)
231                 return;
232         rte_free(dev->process_private);
233         dev->process_private = NULL;
234 }
235
236 /**
237  * DPDK callback for Ethernet device configuration.
238  *
239  * @param dev
240  *   Pointer to Ethernet device structure.
241  *
242  * @return
243  *   0 on success, negative errno value otherwise and rte_errno is set.
244  */
245 static int
246 mlx4_dev_configure(struct rte_eth_dev *dev)
247 {
248         struct mlx4_priv *priv = dev->data->dev_private;
249         struct rte_flow_error error;
250         int ret;
251
252         if (dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG)
253                 dev->data->dev_conf.rxmode.offloads |= DEV_RX_OFFLOAD_RSS_HASH;
254
255         /* Prepare internal flow rules. */
256         ret = mlx4_flow_sync(priv, &error);
257         if (ret) {
258                 ERROR("cannot set up internal flow rules (code %d, \"%s\"),"
259                       " flow error type %d, cause %p, message: %s",
260                       -ret, strerror(-ret), error.type, error.cause,
261                       error.message ? error.message : "(unspecified)");
262                 goto exit;
263         }
264         ret = mlx4_intr_install(priv);
265         if (ret) {
266                 ERROR("%p: interrupt handler installation failed",
267                       (void *)dev);
268                 goto exit;
269         }
270         ret = mlx4_proc_priv_init(dev);
271         if (ret) {
272                 ERROR("%p: process private data allocation failed",
273                       (void *)dev);
274                 goto exit;
275         }
276 exit:
277         return ret;
278 }
279
280 /**
281  * DPDK callback to start the device.
282  *
283  * Simulate device start by initializing common RSS resources and attaching
284  * all configured flows.
285  *
286  * @param dev
287  *   Pointer to Ethernet device structure.
288  *
289  * @return
290  *   0 on success, negative errno value otherwise and rte_errno is set.
291  */
292 static int
293 mlx4_dev_start(struct rte_eth_dev *dev)
294 {
295         struct mlx4_priv *priv = dev->data->dev_private;
296         struct rte_flow_error error;
297         int ret;
298
299         if (priv->started)
300                 return 0;
301         DEBUG("%p: attaching configured flows to all RX queues", (void *)dev);
302         priv->started = 1;
303         ret = mlx4_rss_init(priv);
304         if (ret) {
305                 ERROR("%p: cannot initialize RSS resources: %s",
306                       (void *)dev, strerror(-ret));
307                 goto err;
308         }
309 #ifdef RTE_LIBRTE_MLX4_DEBUG
310         mlx4_mr_dump_dev(dev);
311 #endif
312         ret = mlx4_rxq_intr_enable(priv);
313         if (ret) {
314                 ERROR("%p: interrupt handler installation failed",
315                      (void *)dev);
316                 goto err;
317         }
318         ret = mlx4_flow_sync(priv, &error);
319         if (ret) {
320                 ERROR("%p: cannot attach flow rules (code %d, \"%s\"),"
321                       " flow error type %d, cause %p, message: %s",
322                       (void *)dev,
323                       -ret, strerror(-ret), error.type, error.cause,
324                       error.message ? error.message : "(unspecified)");
325                 goto err;
326         }
327         rte_wmb();
328         dev->tx_pkt_burst = mlx4_tx_burst;
329         dev->rx_pkt_burst = mlx4_rx_burst;
330         /* Enable datapath on secondary process. */
331         mlx4_mp_req_start_rxtx(dev);
332         return 0;
333 err:
334         mlx4_dev_stop(dev);
335         return ret;
336 }
337
338 /**
339  * DPDK callback to stop the device.
340  *
341  * Simulate device stop by detaching all configured flows.
342  *
343  * @param dev
344  *   Pointer to Ethernet device structure.
345  */
346 static void
347 mlx4_dev_stop(struct rte_eth_dev *dev)
348 {
349         struct mlx4_priv *priv = dev->data->dev_private;
350
351         if (!priv->started)
352                 return;
353         DEBUG("%p: detaching flows from all RX queues", (void *)dev);
354         priv->started = 0;
355         dev->tx_pkt_burst = mlx4_tx_burst_removed;
356         dev->rx_pkt_burst = mlx4_rx_burst_removed;
357         rte_wmb();
358         /* Disable datapath on secondary process. */
359         mlx4_mp_req_stop_rxtx(dev);
360         mlx4_flow_sync(priv, NULL);
361         mlx4_rxq_intr_disable(priv);
362         mlx4_rss_deinit(priv);
363 }
364
365 /**
366  * DPDK callback to close the device.
367  *
368  * Destroy all queues and objects, free memory.
369  *
370  * @param dev
371  *   Pointer to Ethernet device structure.
372  */
373 static int
374 mlx4_dev_close(struct rte_eth_dev *dev)
375 {
376         struct mlx4_priv *priv = dev->data->dev_private;
377         unsigned int i;
378
379         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
380                 return 0;
381         DEBUG("%p: closing device \"%s\"",
382               (void *)dev,
383               ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
384         dev->rx_pkt_burst = mlx4_rx_burst_removed;
385         dev->tx_pkt_burst = mlx4_tx_burst_removed;
386         rte_wmb();
387         /* Disable datapath on secondary process. */
388         mlx4_mp_req_stop_rxtx(dev);
389         mlx4_flow_clean(priv);
390         mlx4_rss_deinit(priv);
391         for (i = 0; i != dev->data->nb_rx_queues; ++i)
392                 mlx4_rx_queue_release(dev->data->rx_queues[i]);
393         for (i = 0; i != dev->data->nb_tx_queues; ++i)
394                 mlx4_tx_queue_release(dev->data->tx_queues[i]);
395         mlx4_proc_priv_uninit(dev);
396         mlx4_mr_release(dev);
397         if (priv->pd != NULL) {
398                 MLX4_ASSERT(priv->ctx != NULL);
399                 claim_zero(mlx4_glue->dealloc_pd(priv->pd));
400                 claim_zero(mlx4_glue->close_device(priv->ctx));
401         } else
402                 MLX4_ASSERT(priv->ctx == NULL);
403         mlx4_intr_uninstall(priv);
404         memset(priv, 0, sizeof(*priv));
405         /* mac_addrs must not be freed because part of dev_private */
406         dev->data->mac_addrs = NULL;
407         return 0;
408 }
409
410 static const struct eth_dev_ops mlx4_dev_ops = {
411         .dev_configure = mlx4_dev_configure,
412         .dev_start = mlx4_dev_start,
413         .dev_stop = mlx4_dev_stop,
414         .dev_set_link_down = mlx4_dev_set_link_down,
415         .dev_set_link_up = mlx4_dev_set_link_up,
416         .dev_close = mlx4_dev_close,
417         .link_update = mlx4_link_update,
418         .promiscuous_enable = mlx4_promiscuous_enable,
419         .promiscuous_disable = mlx4_promiscuous_disable,
420         .allmulticast_enable = mlx4_allmulticast_enable,
421         .allmulticast_disable = mlx4_allmulticast_disable,
422         .mac_addr_remove = mlx4_mac_addr_remove,
423         .mac_addr_add = mlx4_mac_addr_add,
424         .mac_addr_set = mlx4_mac_addr_set,
425         .set_mc_addr_list = mlx4_set_mc_addr_list,
426         .stats_get = mlx4_stats_get,
427         .stats_reset = mlx4_stats_reset,
428         .fw_version_get = mlx4_fw_version_get,
429         .dev_infos_get = mlx4_dev_infos_get,
430         .dev_supported_ptypes_get = mlx4_dev_supported_ptypes_get,
431         .vlan_filter_set = mlx4_vlan_filter_set,
432         .rx_queue_setup = mlx4_rx_queue_setup,
433         .tx_queue_setup = mlx4_tx_queue_setup,
434         .rx_queue_release = mlx4_rx_queue_release,
435         .tx_queue_release = mlx4_tx_queue_release,
436         .flow_ctrl_get = mlx4_flow_ctrl_get,
437         .flow_ctrl_set = mlx4_flow_ctrl_set,
438         .mtu_set = mlx4_mtu_set,
439         .filter_ctrl = mlx4_filter_ctrl,
440         .rx_queue_intr_enable = mlx4_rx_intr_enable,
441         .rx_queue_intr_disable = mlx4_rx_intr_disable,
442         .is_removed = mlx4_is_removed,
443 };
444
445 /* Available operations from secondary process. */
446 static const struct eth_dev_ops mlx4_dev_sec_ops = {
447         .stats_get = mlx4_stats_get,
448         .stats_reset = mlx4_stats_reset,
449         .fw_version_get = mlx4_fw_version_get,
450         .dev_infos_get = mlx4_dev_infos_get,
451 };
452
453 /**
454  * Get PCI information from struct ibv_device.
455  *
456  * @param device
457  *   Pointer to Ethernet device structure.
458  * @param[out] pci_addr
459  *   PCI bus address output buffer.
460  *
461  * @return
462  *   0 on success, negative errno value otherwise and rte_errno is set.
463  */
464 static int
465 mlx4_ibv_device_to_pci_addr(const struct ibv_device *device,
466                             struct rte_pci_addr *pci_addr)
467 {
468         FILE *file;
469         char line[32];
470         MKSTR(path, "%s/device/uevent", device->ibdev_path);
471
472         file = fopen(path, "rb");
473         if (file == NULL) {
474                 rte_errno = errno;
475                 return -rte_errno;
476         }
477         while (fgets(line, sizeof(line), file) == line) {
478                 size_t len = strlen(line);
479                 int ret;
480
481                 /* Truncate long lines. */
482                 if (len == (sizeof(line) - 1))
483                         while (line[(len - 1)] != '\n') {
484                                 ret = fgetc(file);
485                                 if (ret == EOF)
486                                         break;
487                                 line[(len - 1)] = ret;
488                         }
489                 /* Extract information. */
490                 if (sscanf(line,
491                            "PCI_SLOT_NAME="
492                            "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 "\n",
493                            &pci_addr->domain,
494                            &pci_addr->bus,
495                            &pci_addr->devid,
496                            &pci_addr->function) == 4) {
497                         break;
498                 }
499         }
500         fclose(file);
501         return 0;
502 }
503
504 /**
505  * Verify and store value for device argument.
506  *
507  * @param[in] key
508  *   Key argument to verify.
509  * @param[in] val
510  *   Value associated with key.
511  * @param[in, out] conf
512  *   Shared configuration data.
513  *
514  * @return
515  *   0 on success, negative errno value otherwise and rte_errno is set.
516  */
517 static int
518 mlx4_arg_parse(const char *key, const char *val, struct mlx4_conf *conf)
519 {
520         unsigned long tmp;
521
522         errno = 0;
523         tmp = strtoul(val, NULL, 0);
524         if (errno) {
525                 rte_errno = errno;
526                 WARN("%s: \"%s\" is not a valid integer", key, val);
527                 return -rte_errno;
528         }
529         if (strcmp(MLX4_PMD_PORT_KVARG, key) == 0) {
530                 uint32_t ports = rte_log2_u32(conf->ports.present + 1);
531
532                 if (tmp >= ports) {
533                         ERROR("port index %lu outside range [0,%" PRIu32 ")",
534                               tmp, ports);
535                         return -EINVAL;
536                 }
537                 if (!(conf->ports.present & (1 << tmp))) {
538                         rte_errno = EINVAL;
539                         ERROR("invalid port index %lu", tmp);
540                         return -rte_errno;
541                 }
542                 conf->ports.enabled |= 1 << tmp;
543         } else if (strcmp(MLX4_MR_EXT_MEMSEG_EN_KVARG, key) == 0) {
544                 conf->mr_ext_memseg_en = !!tmp;
545         } else {
546                 rte_errno = EINVAL;
547                 WARN("%s: unknown parameter", key);
548                 return -rte_errno;
549         }
550         return 0;
551 }
552
553 /**
554  * Parse device parameters.
555  *
556  * @param devargs
557  *   Device arguments structure.
558  *
559  * @return
560  *   0 on success, negative errno value otherwise and rte_errno is set.
561  */
562 static int
563 mlx4_args(struct rte_devargs *devargs, struct mlx4_conf *conf)
564 {
565         struct rte_kvargs *kvlist;
566         unsigned int arg_count;
567         int ret = 0;
568         int i;
569
570         if (devargs == NULL)
571                 return 0;
572         kvlist = rte_kvargs_parse(devargs->args, pmd_mlx4_init_params);
573         if (kvlist == NULL) {
574                 rte_errno = EINVAL;
575                 ERROR("failed to parse kvargs");
576                 return -rte_errno;
577         }
578         /* Process parameters. */
579         for (i = 0; pmd_mlx4_init_params[i]; ++i) {
580                 arg_count = rte_kvargs_count(kvlist, pmd_mlx4_init_params[i]);
581                 while (arg_count-- > 0) {
582                         ret = rte_kvargs_process(kvlist,
583                                                  pmd_mlx4_init_params[i],
584                                                  (int (*)(const char *,
585                                                           const char *,
586                                                           void *))
587                                                  mlx4_arg_parse,
588                                                  conf);
589                         if (ret != 0)
590                                 goto free_kvlist;
591                 }
592         }
593 free_kvlist:
594         rte_kvargs_free(kvlist);
595         return ret;
596 }
597
598 /**
599  * Interpret RSS capabilities reported by device.
600  *
601  * This function returns the set of usable Verbs RSS hash fields, kernel
602  * quirks taken into account.
603  *
604  * @param ctx
605  *   Verbs context.
606  * @param pd
607  *   Verbs protection domain.
608  * @param device_attr_ex
609  *   Extended device attributes to interpret.
610  *
611  * @return
612  *   Usable RSS hash fields mask in Verbs format.
613  */
614 static uint64_t
615 mlx4_hw_rss_sup(struct ibv_context *ctx, struct ibv_pd *pd,
616                 struct ibv_device_attr_ex *device_attr_ex)
617 {
618         uint64_t hw_rss_sup = device_attr_ex->rss_caps.rx_hash_fields_mask;
619         struct ibv_cq *cq = NULL;
620         struct ibv_wq *wq = NULL;
621         struct ibv_rwq_ind_table *ind = NULL;
622         struct ibv_qp *qp = NULL;
623
624         if (!hw_rss_sup) {
625                 WARN("no RSS capabilities reported; disabling support for UDP"
626                      " RSS and inner VXLAN RSS");
627                 return IBV_RX_HASH_SRC_IPV4 | IBV_RX_HASH_DST_IPV4 |
628                         IBV_RX_HASH_SRC_IPV6 | IBV_RX_HASH_DST_IPV6 |
629                         IBV_RX_HASH_SRC_PORT_TCP | IBV_RX_HASH_DST_PORT_TCP;
630         }
631         if (!(hw_rss_sup & IBV_RX_HASH_INNER))
632                 return hw_rss_sup;
633         /*
634          * Although reported as supported, missing code in some Linux
635          * versions (v4.15, v4.16) prevents the creation of hash QPs with
636          * inner capability.
637          *
638          * There is no choice but to attempt to instantiate a temporary RSS
639          * context in order to confirm its support.
640          */
641         cq = mlx4_glue->create_cq(ctx, 1, NULL, NULL, 0);
642         wq = cq ? mlx4_glue->create_wq
643                 (ctx,
644                  &(struct ibv_wq_init_attr){
645                         .wq_type = IBV_WQT_RQ,
646                         .max_wr = 1,
647                         .max_sge = 1,
648                         .pd = pd,
649                         .cq = cq,
650                  }) : NULL;
651         ind = wq ? mlx4_glue->create_rwq_ind_table
652                 (ctx,
653                  &(struct ibv_rwq_ind_table_init_attr){
654                         .log_ind_tbl_size = 0,
655                         .ind_tbl = &wq,
656                         .comp_mask = 0,
657                  }) : NULL;
658         qp = ind ? mlx4_glue->create_qp_ex
659                 (ctx,
660                  &(struct ibv_qp_init_attr_ex){
661                         .comp_mask =
662                                 (IBV_QP_INIT_ATTR_PD |
663                                  IBV_QP_INIT_ATTR_RX_HASH |
664                                  IBV_QP_INIT_ATTR_IND_TABLE),
665                         .qp_type = IBV_QPT_RAW_PACKET,
666                         .pd = pd,
667                         .rwq_ind_tbl = ind,
668                         .rx_hash_conf = {
669                                 .rx_hash_function = IBV_RX_HASH_FUNC_TOEPLITZ,
670                                 .rx_hash_key_len = MLX4_RSS_HASH_KEY_SIZE,
671                                 .rx_hash_key = mlx4_rss_hash_key_default,
672                                 .rx_hash_fields_mask = hw_rss_sup,
673                         },
674                  }) : NULL;
675         if (!qp) {
676                 WARN("disabling unusable inner RSS capability due to kernel"
677                      " quirk");
678                 hw_rss_sup &= ~IBV_RX_HASH_INNER;
679         } else {
680                 claim_zero(mlx4_glue->destroy_qp(qp));
681         }
682         if (ind)
683                 claim_zero(mlx4_glue->destroy_rwq_ind_table(ind));
684         if (wq)
685                 claim_zero(mlx4_glue->destroy_wq(wq));
686         if (cq)
687                 claim_zero(mlx4_glue->destroy_cq(cq));
688         return hw_rss_sup;
689 }
690
691 static struct rte_pci_driver mlx4_driver;
692
693 /**
694  * PMD global initialization.
695  *
696  * Independent from individual device, this function initializes global
697  * per-PMD data structures distinguishing primary and secondary processes.
698  * Hence, each initialization is called once per a process.
699  *
700  * @return
701  *   0 on success, a negative errno value otherwise and rte_errno is set.
702  */
703 static int
704 mlx4_init_once(void)
705 {
706         struct mlx4_shared_data *sd;
707         struct mlx4_local_data *ld = &mlx4_local_data;
708         int ret = 0;
709
710         if (mlx4_init_shared_data())
711                 return -rte_errno;
712         sd = mlx4_shared_data;
713         MLX4_ASSERT(sd);
714         rte_spinlock_lock(&sd->lock);
715         switch (rte_eal_process_type()) {
716         case RTE_PROC_PRIMARY:
717                 if (sd->init_done)
718                         break;
719                 LIST_INIT(&sd->mem_event_cb_list);
720                 rte_rwlock_init(&sd->mem_event_rwlock);
721                 rte_mem_event_callback_register("MLX4_MEM_EVENT_CB",
722                                                 mlx4_mr_mem_event_cb, NULL);
723                 ret = mlx4_mp_init_primary();
724                 if (ret)
725                         goto out;
726                 sd->init_done = 1;
727                 break;
728         case RTE_PROC_SECONDARY:
729                 if (ld->init_done)
730                         break;
731                 ret = mlx4_mp_init_secondary();
732                 if (ret)
733                         goto out;
734                 ++sd->secondary_cnt;
735                 ld->init_done = 1;
736                 break;
737         default:
738                 break;
739         }
740 out:
741         rte_spinlock_unlock(&sd->lock);
742         return ret;
743 }
744
745 /**
746  * DPDK callback to register a PCI device.
747  *
748  * This function creates an Ethernet device for each port of a given
749  * PCI device.
750  *
751  * @param[in] pci_drv
752  *   PCI driver structure (mlx4_driver).
753  * @param[in] pci_dev
754  *   PCI device information.
755  *
756  * @return
757  *   0 on success, negative errno value otherwise and rte_errno is set.
758  */
759 static int
760 mlx4_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
761 {
762         struct ibv_device **list;
763         struct ibv_device *ibv_dev;
764         int err = 0;
765         struct ibv_context *attr_ctx = NULL;
766         struct ibv_device_attr device_attr;
767         struct ibv_device_attr_ex device_attr_ex;
768         struct mlx4_conf conf = {
769                 .ports.present = 0,
770                 .mr_ext_memseg_en = 1,
771         };
772         unsigned int vf;
773         int i;
774         char ifname[IF_NAMESIZE];
775
776         (void)pci_drv;
777         err = mlx4_init_once();
778         if (err) {
779                 ERROR("unable to init PMD global data: %s",
780                       strerror(rte_errno));
781                 return -rte_errno;
782         }
783         MLX4_ASSERT(pci_drv == &mlx4_driver);
784         list = mlx4_glue->get_device_list(&i);
785         if (list == NULL) {
786                 rte_errno = errno;
787                 MLX4_ASSERT(rte_errno);
788                 if (rte_errno == ENOSYS)
789                         ERROR("cannot list devices, is ib_uverbs loaded?");
790                 return -rte_errno;
791         }
792         MLX4_ASSERT(i >= 0);
793         /*
794          * For each listed device, check related sysfs entry against
795          * the provided PCI ID.
796          */
797         while (i != 0) {
798                 struct rte_pci_addr pci_addr;
799
800                 --i;
801                 DEBUG("checking device \"%s\"", list[i]->name);
802                 if (mlx4_ibv_device_to_pci_addr(list[i], &pci_addr))
803                         continue;
804                 if ((pci_dev->addr.domain != pci_addr.domain) ||
805                     (pci_dev->addr.bus != pci_addr.bus) ||
806                     (pci_dev->addr.devid != pci_addr.devid) ||
807                     (pci_dev->addr.function != pci_addr.function))
808                         continue;
809                 vf = (pci_dev->id.device_id ==
810                       PCI_DEVICE_ID_MELLANOX_CONNECTX3VF);
811                 INFO("PCI information matches, using device \"%s\" (VF: %s)",
812                      list[i]->name, (vf ? "true" : "false"));
813                 attr_ctx = mlx4_glue->open_device(list[i]);
814                 err = errno;
815                 break;
816         }
817         if (attr_ctx == NULL) {
818                 mlx4_glue->free_device_list(list);
819                 switch (err) {
820                 case 0:
821                         rte_errno = ENODEV;
822                         ERROR("cannot access device, is mlx4_ib loaded?");
823                         return -rte_errno;
824                 case EINVAL:
825                         rte_errno = EINVAL;
826                         ERROR("cannot use device, are drivers up to date?");
827                         return -rte_errno;
828                 }
829                 MLX4_ASSERT(err > 0);
830                 rte_errno = err;
831                 return -rte_errno;
832         }
833         ibv_dev = list[i];
834         DEBUG("device opened");
835         if (mlx4_glue->query_device(attr_ctx, &device_attr)) {
836                 err = ENODEV;
837                 goto error;
838         }
839         INFO("%u port(s) detected", device_attr.phys_port_cnt);
840         conf.ports.present |= (UINT64_C(1) << device_attr.phys_port_cnt) - 1;
841         if (mlx4_args(pci_dev->device.devargs, &conf)) {
842                 ERROR("failed to process device arguments");
843                 err = EINVAL;
844                 goto error;
845         }
846         /* Use all ports when none are defined */
847         if (!conf.ports.enabled)
848                 conf.ports.enabled = conf.ports.present;
849         /* Retrieve extended device attributes. */
850         if (mlx4_glue->query_device_ex(attr_ctx, NULL, &device_attr_ex)) {
851                 err = ENODEV;
852                 goto error;
853         }
854         MLX4_ASSERT(device_attr.max_sge >= MLX4_MAX_SGE);
855         for (i = 0; i < device_attr.phys_port_cnt; i++) {
856                 uint32_t port = i + 1; /* ports are indexed from one */
857                 struct ibv_context *ctx = NULL;
858                 struct ibv_port_attr port_attr;
859                 struct ibv_pd *pd = NULL;
860                 struct mlx4_priv *priv = NULL;
861                 struct rte_eth_dev *eth_dev = NULL;
862                 struct rte_ether_addr mac;
863                 char name[RTE_ETH_NAME_MAX_LEN];
864
865                 /* If port is not enabled, skip. */
866                 if (!(conf.ports.enabled & (1 << i)))
867                         continue;
868                 DEBUG("using port %u", port);
869                 ctx = mlx4_glue->open_device(ibv_dev);
870                 if (ctx == NULL) {
871                         err = ENODEV;
872                         goto port_error;
873                 }
874                 snprintf(name, sizeof(name), "%s port %u",
875                          mlx4_glue->get_device_name(ibv_dev), port);
876                 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
877                         eth_dev = rte_eth_dev_attach_secondary(name);
878                         if (eth_dev == NULL) {
879                                 ERROR("can not attach rte ethdev");
880                                 rte_errno = ENOMEM;
881                                 err = rte_errno;
882                                 goto error;
883                         }
884                         priv = eth_dev->data->dev_private;
885                         if (!priv->verbs_alloc_ctx.enabled) {
886                                 ERROR("secondary process is not supported"
887                                       " due to lack of external allocator"
888                                       " from Verbs");
889                                 rte_errno = ENOTSUP;
890                                 err = rte_errno;
891                                 goto error;
892                         }
893                         eth_dev->device = &pci_dev->device;
894                         eth_dev->dev_ops = &mlx4_dev_sec_ops;
895                         err = mlx4_proc_priv_init(eth_dev);
896                         if (err)
897                                 goto error;
898                         /* Receive command fd from primary process. */
899                         err = mlx4_mp_req_verbs_cmd_fd(eth_dev);
900                         if (err < 0) {
901                                 err = rte_errno;
902                                 goto error;
903                         }
904                         /* Remap UAR for Tx queues. */
905                         err = mlx4_tx_uar_init_secondary(eth_dev, err);
906                         if (err) {
907                                 err = rte_errno;
908                                 goto error;
909                         }
910                         /*
911                          * Ethdev pointer is still required as input since
912                          * the primary device is not accessible from the
913                          * secondary process.
914                          */
915                         eth_dev->tx_pkt_burst = mlx4_tx_burst;
916                         eth_dev->rx_pkt_burst = mlx4_rx_burst;
917                         claim_zero(mlx4_glue->close_device(ctx));
918                         rte_eth_copy_pci_info(eth_dev, pci_dev);
919                         rte_eth_dev_probing_finish(eth_dev);
920                         continue;
921                 }
922                 /* Check port status. */
923                 err = mlx4_glue->query_port(ctx, port, &port_attr);
924                 if (err) {
925                         err = ENODEV;
926                         ERROR("port query failed: %s", strerror(err));
927                         goto port_error;
928                 }
929                 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
930                         err = ENOTSUP;
931                         ERROR("port %d is not configured in Ethernet mode",
932                               port);
933                         goto port_error;
934                 }
935                 if (port_attr.state != IBV_PORT_ACTIVE)
936                         DEBUG("port %d is not active: \"%s\" (%d)",
937                               port, mlx4_glue->port_state_str(port_attr.state),
938                               port_attr.state);
939                 /* Make asynchronous FD non-blocking to handle interrupts. */
940                 err = mlx4_fd_set_non_blocking(ctx->async_fd);
941                 if (err) {
942                         ERROR("cannot make asynchronous FD non-blocking: %s",
943                               strerror(err));
944                         goto port_error;
945                 }
946                 /* Allocate protection domain. */
947                 pd = mlx4_glue->alloc_pd(ctx);
948                 if (pd == NULL) {
949                         err = ENOMEM;
950                         ERROR("PD allocation failure");
951                         goto port_error;
952                 }
953                 /* from rte_ethdev.c */
954                 priv = rte_zmalloc("ethdev private structure",
955                                    sizeof(*priv),
956                                    RTE_CACHE_LINE_SIZE);
957                 if (priv == NULL) {
958                         err = ENOMEM;
959                         ERROR("priv allocation failure");
960                         goto port_error;
961                 }
962                 priv->ctx = ctx;
963                 priv->device_attr = device_attr;
964                 priv->port = port;
965                 priv->pd = pd;
966                 priv->mtu = RTE_ETHER_MTU;
967                 priv->vf = vf;
968                 priv->hw_csum = !!(device_attr.device_cap_flags &
969                                    IBV_DEVICE_RAW_IP_CSUM);
970                 DEBUG("checksum offloading is %ssupported",
971                       (priv->hw_csum ? "" : "not "));
972                 /* Only ConnectX-3 Pro supports tunneling. */
973                 priv->hw_csum_l2tun =
974                         priv->hw_csum &&
975                         (device_attr.vendor_part_id ==
976                          PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO);
977                 DEBUG("L2 tunnel checksum offloads are %ssupported",
978                       priv->hw_csum_l2tun ? "" : "not ");
979                 priv->hw_rss_sup = mlx4_hw_rss_sup(priv->ctx, priv->pd,
980                                                    &device_attr_ex);
981                 DEBUG("supported RSS hash fields mask: %016" PRIx64,
982                       priv->hw_rss_sup);
983                 priv->hw_rss_max_qps =
984                         device_attr_ex.rss_caps.max_rwq_indirection_table_size;
985                 DEBUG("MAX RSS queues %d", priv->hw_rss_max_qps);
986                 priv->hw_fcs_strip = !!(device_attr_ex.raw_packet_caps &
987                                         IBV_RAW_PACKET_CAP_SCATTER_FCS);
988                 DEBUG("FCS stripping toggling is %ssupported",
989                       priv->hw_fcs_strip ? "" : "not ");
990                 priv->tso =
991                         ((device_attr_ex.tso_caps.max_tso > 0) &&
992                          (device_attr_ex.tso_caps.supported_qpts &
993                           (1 << IBV_QPT_RAW_PACKET)));
994                 if (priv->tso)
995                         priv->tso_max_payload_sz =
996                                         device_attr_ex.tso_caps.max_tso;
997                 DEBUG("TSO is %ssupported",
998                       priv->tso ? "" : "not ");
999                 priv->mr_ext_memseg_en = conf.mr_ext_memseg_en;
1000                 /* Configure the first MAC address by default. */
1001                 err = mlx4_get_mac(priv, &mac.addr_bytes);
1002                 if (err) {
1003                         ERROR("cannot get MAC address, is mlx4_en loaded?"
1004                               " (error: %s)", strerror(err));
1005                         goto port_error;
1006                 }
1007                 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
1008                      priv->port,
1009                      mac.addr_bytes[0], mac.addr_bytes[1],
1010                      mac.addr_bytes[2], mac.addr_bytes[3],
1011                      mac.addr_bytes[4], mac.addr_bytes[5]);
1012                 /* Register MAC address. */
1013                 priv->mac[0] = mac;
1014
1015                 if (mlx4_get_ifname(priv, &ifname) == 0) {
1016                         DEBUG("port %u ifname is \"%s\"",
1017                               priv->port, ifname);
1018                         priv->if_index = if_nametoindex(ifname);
1019                 } else {
1020                         DEBUG("port %u ifname is unknown", priv->port);
1021                 }
1022
1023                 /* Get actual MTU if possible. */
1024                 mlx4_mtu_get(priv, &priv->mtu);
1025                 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
1026                 eth_dev = rte_eth_dev_allocate(name);
1027                 if (eth_dev == NULL) {
1028                         err = ENOMEM;
1029                         ERROR("can not allocate rte ethdev");
1030                         goto port_error;
1031                 }
1032                 eth_dev->data->dev_private = priv;
1033                 eth_dev->data->mac_addrs = priv->mac;
1034                 eth_dev->device = &pci_dev->device;
1035                 rte_eth_copy_pci_info(eth_dev, pci_dev);
1036                 /* Initialize local interrupt handle for current port. */
1037                 memset(&priv->intr_handle, 0, sizeof(struct rte_intr_handle));
1038                 priv->intr_handle.fd = -1;
1039                 priv->intr_handle.type = RTE_INTR_HANDLE_EXT;
1040                 /*
1041                  * Override ethdev interrupt handle pointer with private
1042                  * handle instead of that of the parent PCI device used by
1043                  * default. This prevents it from being shared between all
1044                  * ports of the same PCI device since each of them is
1045                  * associated its own Verbs context.
1046                  *
1047                  * Rx interrupts in particular require this as the PMD has
1048                  * no control over the registration of queue interrupts
1049                  * besides setting up eth_dev->intr_handle, the rest is
1050                  * handled by rte_intr_rx_ctl().
1051                  */
1052                 eth_dev->intr_handle = &priv->intr_handle;
1053                 priv->dev_data = eth_dev->data;
1054                 eth_dev->dev_ops = &mlx4_dev_ops;
1055 #ifdef HAVE_IBV_MLX4_BUF_ALLOCATORS
1056                 /* Hint libmlx4 to use PMD allocator for data plane resources */
1057                 err = mlx4_glue->dv_set_context_attr
1058                         (ctx, MLX4DV_SET_CTX_ATTR_BUF_ALLOCATORS,
1059                          (void *)((uintptr_t)&(struct mlx4dv_ctx_allocators){
1060                                  .alloc = &mlx4_alloc_verbs_buf,
1061                                  .free = &mlx4_free_verbs_buf,
1062                                  .data = priv,
1063                         }));
1064                 if (err)
1065                         WARN("Verbs external allocator is not supported");
1066                 else
1067                         priv->verbs_alloc_ctx.enabled = 1;
1068 #endif
1069                 /* Bring Ethernet device up. */
1070                 DEBUG("forcing Ethernet interface up");
1071                 mlx4_dev_set_link_up(eth_dev);
1072                 /* Update link status once if waiting for LSC. */
1073                 if (eth_dev->data->dev_flags & RTE_ETH_DEV_INTR_LSC)
1074                         mlx4_link_update(eth_dev, 0);
1075                 /*
1076                  * Once the device is added to the list of memory event
1077                  * callback, its global MR cache table cannot be expanded
1078                  * on the fly because of deadlock. If it overflows, lookup
1079                  * should be done by searching MR list linearly, which is slow.
1080                  */
1081                 err = mlx4_mr_btree_init(&priv->mr.cache,
1082                                          MLX4_MR_BTREE_CACHE_N * 2,
1083                                          eth_dev->device->numa_node);
1084                 if (err) {
1085                         /* rte_errno is already set. */
1086                         goto port_error;
1087                 }
1088                 /* Add device to memory callback list. */
1089                 rte_rwlock_write_lock(&mlx4_shared_data->mem_event_rwlock);
1090                 LIST_INSERT_HEAD(&mlx4_shared_data->mem_event_cb_list,
1091                                  priv, mem_event_cb);
1092                 rte_rwlock_write_unlock(&mlx4_shared_data->mem_event_rwlock);
1093                 rte_eth_dev_probing_finish(eth_dev);
1094                 continue;
1095 port_error:
1096                 rte_free(priv);
1097                 if (eth_dev != NULL)
1098                         eth_dev->data->dev_private = NULL;
1099                 if (pd)
1100                         claim_zero(mlx4_glue->dealloc_pd(pd));
1101                 if (ctx)
1102                         claim_zero(mlx4_glue->close_device(ctx));
1103                 if (eth_dev != NULL) {
1104                         /* mac_addrs must not be freed because part of dev_private */
1105                         eth_dev->data->mac_addrs = NULL;
1106                         rte_eth_dev_release_port(eth_dev);
1107                 }
1108                 break;
1109         }
1110         /*
1111          * XXX if something went wrong in the loop above, there is a resource
1112          * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
1113          * long as the dpdk does not provide a way to deallocate a ethdev and a
1114          * way to enumerate the registered ethdevs to free the previous ones.
1115          */
1116 error:
1117         if (attr_ctx)
1118                 claim_zero(mlx4_glue->close_device(attr_ctx));
1119         if (list)
1120                 mlx4_glue->free_device_list(list);
1121         if (err)
1122                 rte_errno = err;
1123         return -err;
1124 }
1125
1126 static const struct rte_pci_id mlx4_pci_id_map[] = {
1127         {
1128                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1129                                PCI_DEVICE_ID_MELLANOX_CONNECTX3)
1130         },
1131         {
1132                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1133                                PCI_DEVICE_ID_MELLANOX_CONNECTX3PRO)
1134         },
1135         {
1136                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1137                                PCI_DEVICE_ID_MELLANOX_CONNECTX3VF)
1138         },
1139         {
1140                 .vendor_id = 0
1141         }
1142 };
1143
1144 static struct rte_pci_driver mlx4_driver = {
1145         .driver = {
1146                 .name = MLX4_DRIVER_NAME
1147         },
1148         .id_table = mlx4_pci_id_map,
1149         .probe = mlx4_pci_probe,
1150         .drv_flags = RTE_PCI_DRV_INTR_LSC | RTE_PCI_DRV_INTR_RMV,
1151 };
1152
1153 #ifdef RTE_IBVERBS_LINK_DLOPEN
1154
1155 /**
1156  * Suffix RTE_EAL_PMD_PATH with "-glue".
1157  *
1158  * This function performs a sanity check on RTE_EAL_PMD_PATH before
1159  * suffixing its last component.
1160  *
1161  * @param buf[out]
1162  *   Output buffer, should be large enough otherwise NULL is returned.
1163  * @param size
1164  *   Size of @p out.
1165  *
1166  * @return
1167  *   Pointer to @p buf or @p NULL in case suffix cannot be appended.
1168  */
1169 static char *
1170 mlx4_glue_path(char *buf, size_t size)
1171 {
1172         static const char *const bad[] = { "/", ".", "..", NULL };
1173         const char *path = RTE_EAL_PMD_PATH;
1174         size_t len = strlen(path);
1175         size_t off;
1176         int i;
1177
1178         while (len && path[len - 1] == '/')
1179                 --len;
1180         for (off = len; off && path[off - 1] != '/'; --off)
1181                 ;
1182         for (i = 0; bad[i]; ++i)
1183                 if (!strncmp(path + off, bad[i], (int)(len - off)))
1184                         goto error;
1185         i = snprintf(buf, size, "%.*s-glue", (int)len, path);
1186         if (i == -1 || (size_t)i >= size)
1187                 goto error;
1188         return buf;
1189 error:
1190         ERROR("unable to append \"-glue\" to last component of"
1191               " RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"),"
1192               " please re-configure DPDK");
1193         return NULL;
1194 }
1195
1196 /**
1197  * Initialization routine for run-time dependency on rdma-core.
1198  */
1199 static int
1200 mlx4_glue_init(void)
1201 {
1202         char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
1203         const char *path[] = {
1204                 /*
1205                  * A basic security check is necessary before trusting
1206                  * MLX4_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
1207                  */
1208                 (geteuid() == getuid() && getegid() == getgid() ?
1209                  getenv("MLX4_GLUE_PATH") : NULL),
1210                 /*
1211                  * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
1212                  * variant, otherwise let dlopen() look up libraries on its
1213                  * own.
1214                  */
1215                 (*RTE_EAL_PMD_PATH ?
1216                  mlx4_glue_path(glue_path, sizeof(glue_path)) : ""),
1217         };
1218         unsigned int i = 0;
1219         void *handle = NULL;
1220         void **sym;
1221         const char *dlmsg;
1222
1223         while (!handle && i != RTE_DIM(path)) {
1224                 const char *end;
1225                 size_t len;
1226                 int ret;
1227
1228                 if (!path[i]) {
1229                         ++i;
1230                         continue;
1231                 }
1232                 end = strpbrk(path[i], ":;");
1233                 if (!end)
1234                         end = path[i] + strlen(path[i]);
1235                 len = end - path[i];
1236                 ret = 0;
1237                 do {
1238                         char name[ret + 1];
1239
1240                         ret = snprintf(name, sizeof(name), "%.*s%s" MLX4_GLUE,
1241                                        (int)len, path[i],
1242                                        (!len || *(end - 1) == '/') ? "" : "/");
1243                         if (ret == -1)
1244                                 break;
1245                         if (sizeof(name) != (size_t)ret + 1)
1246                                 continue;
1247                         DEBUG("looking for rdma-core glue as \"%s\"", name);
1248                         handle = dlopen(name, RTLD_LAZY);
1249                         break;
1250                 } while (1);
1251                 path[i] = end + 1;
1252                 if (!*end)
1253                         ++i;
1254         }
1255         if (!handle) {
1256                 rte_errno = EINVAL;
1257                 dlmsg = dlerror();
1258                 if (dlmsg)
1259                         WARN("cannot load glue library: %s", dlmsg);
1260                 goto glue_error;
1261         }
1262         sym = dlsym(handle, "mlx4_glue");
1263         if (!sym || !*sym) {
1264                 rte_errno = EINVAL;
1265                 dlmsg = dlerror();
1266                 if (dlmsg)
1267                         ERROR("cannot resolve glue symbol: %s", dlmsg);
1268                 goto glue_error;
1269         }
1270         mlx4_glue = *sym;
1271         return 0;
1272 glue_error:
1273         if (handle)
1274                 dlclose(handle);
1275         WARN("cannot initialize PMD due to missing run-time"
1276              " dependency on rdma-core libraries (libibverbs,"
1277              " libmlx4)");
1278         return -rte_errno;
1279 }
1280
1281 #endif
1282
1283 /* Initialize driver log type. */
1284 RTE_LOG_REGISTER(mlx4_logtype, pmd.net.mlx4, NOTICE)
1285
1286 /**
1287  * Driver initialization routine.
1288  */
1289 RTE_INIT(rte_mlx4_pmd_init)
1290 {
1291         /*
1292          * MLX4_DEVICE_FATAL_CLEANUP tells ibv_destroy functions we
1293          * want to get success errno value in case of calling them
1294          * when the device was removed.
1295          */
1296         setenv("MLX4_DEVICE_FATAL_CLEANUP", "1", 1);
1297         /*
1298          * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
1299          * huge pages. Calling ibv_fork_init() during init allows
1300          * applications to use fork() safely for purposes other than
1301          * using this PMD, which is not supported in forked processes.
1302          */
1303         setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
1304 #ifdef RTE_IBVERBS_LINK_DLOPEN
1305         if (mlx4_glue_init())
1306                 return;
1307         MLX4_ASSERT(mlx4_glue);
1308 #endif
1309 #ifdef RTE_LIBRTE_MLX4_DEBUG
1310         /* Glue structure must not contain any NULL pointers. */
1311         {
1312                 unsigned int i;
1313
1314                 for (i = 0; i != sizeof(*mlx4_glue) / sizeof(void *); ++i)
1315                         MLX4_ASSERT(((const void *const *)mlx4_glue)[i]);
1316         }
1317 #endif
1318         if (strcmp(mlx4_glue->version, MLX4_GLUE_VERSION)) {
1319                 ERROR("rdma-core glue \"%s\" mismatch: \"%s\" is required",
1320                       mlx4_glue->version, MLX4_GLUE_VERSION);
1321                 return;
1322         }
1323         mlx4_glue->fork_init();
1324         rte_pci_register(&mlx4_driver);
1325 }
1326
1327 RTE_PMD_EXPORT_NAME(net_mlx4, __COUNTER__);
1328 RTE_PMD_REGISTER_PCI_TABLE(net_mlx4, mlx4_pci_id_map);
1329 RTE_PMD_REGISTER_KMOD_DEP(net_mlx4,
1330         "* ib_uverbs & mlx4_en & mlx4_core & mlx4_ib");