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