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