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