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