net/mlx5: match Rx completion entry size to cacheline
[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
43 /* Verbs header. */
44 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
45 #ifdef PEDANTIC
46 #pragma GCC diagnostic ignored "-Wpedantic"
47 #endif
48 #include <infiniband/verbs.h>
49 #ifdef PEDANTIC
50 #pragma GCC diagnostic error "-Wpedantic"
51 #endif
52
53 #include <rte_malloc.h>
54 #include <rte_ethdev.h>
55 #include <rte_ethdev_pci.h>
56 #include <rte_pci.h>
57 #include <rte_common.h>
58 #include <rte_kvargs.h>
59
60 #include "mlx5.h"
61 #include "mlx5_utils.h"
62 #include "mlx5_rxtx.h"
63 #include "mlx5_autoconf.h"
64 #include "mlx5_defs.h"
65
66 /* Device parameter to enable RX completion queue compression. */
67 #define MLX5_RXQ_CQE_COMP_EN "rxq_cqe_comp_en"
68
69 /* Device parameter to configure inline send. */
70 #define MLX5_TXQ_INLINE "txq_inline"
71
72 /*
73  * Device parameter to configure the number of TX queues threshold for
74  * enabling inline send.
75  */
76 #define MLX5_TXQS_MIN_INLINE "txqs_min_inline"
77
78 /* Device parameter to enable multi-packet send WQEs. */
79 #define MLX5_TXQ_MPW_EN "txq_mpw_en"
80
81 /* Device parameter to include 2 dsegs in the title WQEBB. */
82 #define MLX5_TXQ_MPW_HDR_DSEG_EN "txq_mpw_hdr_dseg_en"
83
84 /* Device parameter to limit the size of inlining packet. */
85 #define MLX5_TXQ_MAX_INLINE_LEN "txq_max_inline_len"
86
87 /* Device parameter to enable hardware TSO offload. */
88 #define MLX5_TSO "tso"
89
90 /* Device parameter to enable hardware Tx vector. */
91 #define MLX5_TX_VEC_EN "tx_vec_en"
92
93 /* Device parameter to enable hardware Rx vector. */
94 #define MLX5_RX_VEC_EN "rx_vec_en"
95
96 /* Default PMD specific parameter value. */
97 #define MLX5_ARG_UNSET (-1)
98
99 #ifndef HAVE_IBV_MLX5_MOD_MPW
100 #define MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED (1 << 2)
101 #define MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW (1 << 3)
102 #endif
103
104 struct mlx5_args {
105         int cqe_comp;
106         int txq_inline;
107         int txqs_inline;
108         int mps;
109         int mpw_hdr_dseg;
110         int inline_max_packet_sz;
111         int tso;
112         int tx_vec_en;
113         int rx_vec_en;
114 };
115 /**
116  * Retrieve integer value from environment variable.
117  *
118  * @param[in] name
119  *   Environment variable name.
120  *
121  * @return
122  *   Integer value, 0 if the variable is not set.
123  */
124 int
125 mlx5_getenv_int(const char *name)
126 {
127         const char *val = getenv(name);
128
129         if (val == NULL)
130                 return 0;
131         return atoi(val);
132 }
133
134 /**
135  * Verbs callback to allocate a memory. This function should allocate the space
136  * according to the size provided residing inside a huge page.
137  * Please note that all allocation must respect the alignment from libmlx5
138  * (i.e. currently sysconf(_SC_PAGESIZE)).
139  *
140  * @param[in] size
141  *   The size in bytes of the memory to allocate.
142  * @param[in] data
143  *   A pointer to the callback data.
144  *
145  * @return
146  *   a pointer to the allocate space.
147  */
148 static void *
149 mlx5_alloc_verbs_buf(size_t size, void *data)
150 {
151         struct priv *priv = data;
152         void *ret;
153         size_t alignment = sysconf(_SC_PAGESIZE);
154
155         assert(data != NULL);
156         assert(!mlx5_is_secondary());
157         ret = rte_malloc_socket(__func__, size, alignment,
158                                 priv->dev->device->numa_node);
159         DEBUG("Extern alloc size: %lu, align: %lu: %p", size, alignment, ret);
160         return ret;
161 }
162
163 /**
164  * Verbs callback to free a memory.
165  *
166  * @param[in] ptr
167  *   A pointer to the memory to free.
168  * @param[in] data
169  *   A pointer to the callback data.
170  */
171 static void
172 mlx5_free_verbs_buf(void *ptr, void *data __rte_unused)
173 {
174         assert(data != NULL);
175         assert(!mlx5_is_secondary());
176         DEBUG("Extern free request: %p", ptr);
177         rte_free(ptr);
178 }
179
180 /**
181  * DPDK callback to close the device.
182  *
183  * Destroy all queues and objects, free memory.
184  *
185  * @param dev
186  *   Pointer to Ethernet device structure.
187  */
188 static void
189 mlx5_dev_close(struct rte_eth_dev *dev)
190 {
191         struct priv *priv = mlx5_get_priv(dev);
192         unsigned int i;
193         int ret;
194
195         priv_lock(priv);
196         DEBUG("%p: closing device \"%s\"",
197               (void *)dev,
198               ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
199         /* In case mlx5_dev_stop() has not been called. */
200         priv_dev_interrupt_handler_uninstall(priv, dev);
201         priv_dev_traffic_disable(priv, dev);
202         /* Prevent crashes when queues are still in use. */
203         dev->rx_pkt_burst = removed_rx_burst;
204         dev->tx_pkt_burst = removed_tx_burst;
205         if (priv->rxqs != NULL) {
206                 /* XXX race condition if mlx5_rx_burst() is still running. */
207                 usleep(1000);
208                 for (i = 0; (i != priv->rxqs_n); ++i)
209                         mlx5_priv_rxq_release(priv, i);
210                 priv->rxqs_n = 0;
211                 priv->rxqs = NULL;
212         }
213         if (priv->txqs != NULL) {
214                 /* XXX race condition if mlx5_tx_burst() is still running. */
215                 usleep(1000);
216                 for (i = 0; (i != priv->txqs_n); ++i)
217                         mlx5_priv_txq_release(priv, i);
218                 priv->txqs_n = 0;
219                 priv->txqs = NULL;
220         }
221         if (priv->pd != NULL) {
222                 assert(priv->ctx != NULL);
223                 claim_zero(ibv_dealloc_pd(priv->pd));
224                 claim_zero(ibv_close_device(priv->ctx));
225         } else
226                 assert(priv->ctx == NULL);
227         if (priv->rss_conf.rss_key != NULL)
228                 rte_free(priv->rss_conf.rss_key);
229         if (priv->reta_idx != NULL)
230                 rte_free(priv->reta_idx);
231         priv_socket_uninit(priv);
232         ret = mlx5_priv_hrxq_ibv_verify(priv);
233         if (ret)
234                 WARN("%p: some Hash Rx queue still remain", (void *)priv);
235         ret = mlx5_priv_ind_table_ibv_verify(priv);
236         if (ret)
237                 WARN("%p: some Indirection table still remain", (void *)priv);
238         ret = mlx5_priv_rxq_ibv_verify(priv);
239         if (ret)
240                 WARN("%p: some Verbs Rx queue still remain", (void *)priv);
241         ret = mlx5_priv_rxq_verify(priv);
242         if (ret)
243                 WARN("%p: some Rx Queues still remain", (void *)priv);
244         ret = mlx5_priv_txq_ibv_verify(priv);
245         if (ret)
246                 WARN("%p: some Verbs Tx queue still remain", (void *)priv);
247         ret = mlx5_priv_txq_verify(priv);
248         if (ret)
249                 WARN("%p: some Tx Queues still remain", (void *)priv);
250         ret = priv_flow_verify(priv);
251         if (ret)
252                 WARN("%p: some flows still remain", (void *)priv);
253         ret = priv_mr_verify(priv);
254         if (ret)
255                 WARN("%p: some Memory Region still remain", (void *)priv);
256         priv_unlock(priv);
257         memset(priv, 0, sizeof(*priv));
258 }
259
260 const struct eth_dev_ops mlx5_dev_ops = {
261         .dev_configure = mlx5_dev_configure,
262         .dev_start = mlx5_dev_start,
263         .dev_stop = mlx5_dev_stop,
264         .dev_set_link_down = mlx5_set_link_down,
265         .dev_set_link_up = mlx5_set_link_up,
266         .dev_close = mlx5_dev_close,
267         .promiscuous_enable = mlx5_promiscuous_enable,
268         .promiscuous_disable = mlx5_promiscuous_disable,
269         .allmulticast_enable = mlx5_allmulticast_enable,
270         .allmulticast_disable = mlx5_allmulticast_disable,
271         .link_update = mlx5_link_update,
272         .stats_get = mlx5_stats_get,
273         .stats_reset = mlx5_stats_reset,
274         .xstats_get = mlx5_xstats_get,
275         .xstats_reset = mlx5_xstats_reset,
276         .xstats_get_names = mlx5_xstats_get_names,
277         .dev_infos_get = mlx5_dev_infos_get,
278         .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
279         .vlan_filter_set = mlx5_vlan_filter_set,
280         .rx_queue_setup = mlx5_rx_queue_setup,
281         .tx_queue_setup = mlx5_tx_queue_setup,
282         .rx_queue_release = mlx5_rx_queue_release,
283         .tx_queue_release = mlx5_tx_queue_release,
284         .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
285         .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
286         .mac_addr_remove = mlx5_mac_addr_remove,
287         .mac_addr_add = mlx5_mac_addr_add,
288         .mac_addr_set = mlx5_mac_addr_set,
289         .mtu_set = mlx5_dev_set_mtu,
290         .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
291         .vlan_offload_set = mlx5_vlan_offload_set,
292         .reta_update = mlx5_dev_rss_reta_update,
293         .reta_query = mlx5_dev_rss_reta_query,
294         .rss_hash_update = mlx5_rss_hash_update,
295         .rss_hash_conf_get = mlx5_rss_hash_conf_get,
296         .filter_ctrl = mlx5_dev_filter_ctrl,
297         .rx_descriptor_status = mlx5_rx_descriptor_status,
298         .tx_descriptor_status = mlx5_tx_descriptor_status,
299         .rx_queue_intr_enable = mlx5_rx_intr_enable,
300         .rx_queue_intr_disable = mlx5_rx_intr_disable,
301 };
302
303 static const struct eth_dev_ops mlx5_dev_sec_ops = {
304         .stats_get = mlx5_stats_get,
305         .stats_reset = mlx5_stats_reset,
306         .xstats_get = mlx5_xstats_get,
307         .xstats_reset = mlx5_xstats_reset,
308         .xstats_get_names = mlx5_xstats_get_names,
309         .dev_infos_get = mlx5_dev_infos_get,
310         .rx_descriptor_status = mlx5_rx_descriptor_status,
311         .tx_descriptor_status = mlx5_tx_descriptor_status,
312 };
313
314 /* Available operators in flow isolated mode. */
315 const struct eth_dev_ops mlx5_dev_ops_isolate = {
316         .dev_configure = mlx5_dev_configure,
317         .dev_start = mlx5_dev_start,
318         .dev_stop = mlx5_dev_stop,
319         .dev_set_link_down = mlx5_set_link_down,
320         .dev_set_link_up = mlx5_set_link_up,
321         .dev_close = mlx5_dev_close,
322         .link_update = mlx5_link_update,
323         .stats_get = mlx5_stats_get,
324         .stats_reset = mlx5_stats_reset,
325         .xstats_get = mlx5_xstats_get,
326         .xstats_reset = mlx5_xstats_reset,
327         .xstats_get_names = mlx5_xstats_get_names,
328         .dev_infos_get = mlx5_dev_infos_get,
329         .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
330         .vlan_filter_set = mlx5_vlan_filter_set,
331         .rx_queue_setup = mlx5_rx_queue_setup,
332         .tx_queue_setup = mlx5_tx_queue_setup,
333         .rx_queue_release = mlx5_rx_queue_release,
334         .tx_queue_release = mlx5_tx_queue_release,
335         .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
336         .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
337         .mac_addr_remove = mlx5_mac_addr_remove,
338         .mac_addr_add = mlx5_mac_addr_add,
339         .mac_addr_set = mlx5_mac_addr_set,
340         .mtu_set = mlx5_dev_set_mtu,
341         .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
342         .vlan_offload_set = mlx5_vlan_offload_set,
343         .filter_ctrl = mlx5_dev_filter_ctrl,
344         .rx_descriptor_status = mlx5_rx_descriptor_status,
345         .tx_descriptor_status = mlx5_tx_descriptor_status,
346         .rx_queue_intr_enable = mlx5_rx_intr_enable,
347         .rx_queue_intr_disable = mlx5_rx_intr_disable,
348 };
349
350 static struct {
351         struct rte_pci_addr pci_addr; /* associated PCI address */
352         uint32_t ports; /* physical ports bitfield. */
353 } mlx5_dev[32];
354
355 /**
356  * Get device index in mlx5_dev[] from PCI bus address.
357  *
358  * @param[in] pci_addr
359  *   PCI bus address to look for.
360  *
361  * @return
362  *   mlx5_dev[] index on success, -1 on failure.
363  */
364 static int
365 mlx5_dev_idx(struct rte_pci_addr *pci_addr)
366 {
367         unsigned int i;
368         int ret = -1;
369
370         assert(pci_addr != NULL);
371         for (i = 0; (i != RTE_DIM(mlx5_dev)); ++i) {
372                 if ((mlx5_dev[i].pci_addr.domain == pci_addr->domain) &&
373                     (mlx5_dev[i].pci_addr.bus == pci_addr->bus) &&
374                     (mlx5_dev[i].pci_addr.devid == pci_addr->devid) &&
375                     (mlx5_dev[i].pci_addr.function == pci_addr->function))
376                         return i;
377                 if ((mlx5_dev[i].ports == 0) && (ret == -1))
378                         ret = i;
379         }
380         return ret;
381 }
382
383 /**
384  * Verify and store value for device argument.
385  *
386  * @param[in] key
387  *   Key argument to verify.
388  * @param[in] val
389  *   Value associated with key.
390  * @param opaque
391  *   User data.
392  *
393  * @return
394  *   0 on success, negative errno value on failure.
395  */
396 static int
397 mlx5_args_check(const char *key, const char *val, void *opaque)
398 {
399         struct mlx5_args *args = opaque;
400         unsigned long tmp;
401
402         errno = 0;
403         tmp = strtoul(val, NULL, 0);
404         if (errno) {
405                 WARN("%s: \"%s\" is not a valid integer", key, val);
406                 return errno;
407         }
408         if (strcmp(MLX5_RXQ_CQE_COMP_EN, key) == 0) {
409                 args->cqe_comp = !!tmp;
410         } else if (strcmp(MLX5_TXQ_INLINE, key) == 0) {
411                 args->txq_inline = tmp;
412         } else if (strcmp(MLX5_TXQS_MIN_INLINE, key) == 0) {
413                 args->txqs_inline = tmp;
414         } else if (strcmp(MLX5_TXQ_MPW_EN, key) == 0) {
415                 args->mps = !!tmp;
416         } else if (strcmp(MLX5_TXQ_MPW_HDR_DSEG_EN, key) == 0) {
417                 args->mpw_hdr_dseg = !!tmp;
418         } else if (strcmp(MLX5_TXQ_MAX_INLINE_LEN, key) == 0) {
419                 args->inline_max_packet_sz = tmp;
420         } else if (strcmp(MLX5_TSO, key) == 0) {
421                 args->tso = !!tmp;
422         } else if (strcmp(MLX5_TX_VEC_EN, key) == 0) {
423                 args->tx_vec_en = !!tmp;
424         } else if (strcmp(MLX5_RX_VEC_EN, key) == 0) {
425                 args->rx_vec_en = !!tmp;
426         } else {
427                 WARN("%s: unknown parameter", key);
428                 return -EINVAL;
429         }
430         return 0;
431 }
432
433 /**
434  * Parse device parameters.
435  *
436  * @param priv
437  *   Pointer to private structure.
438  * @param devargs
439  *   Device arguments structure.
440  *
441  * @return
442  *   0 on success, errno value on failure.
443  */
444 static int
445 mlx5_args(struct mlx5_args *args, struct rte_devargs *devargs)
446 {
447         const char **params = (const char *[]){
448                 MLX5_RXQ_CQE_COMP_EN,
449                 MLX5_TXQ_INLINE,
450                 MLX5_TXQS_MIN_INLINE,
451                 MLX5_TXQ_MPW_EN,
452                 MLX5_TXQ_MPW_HDR_DSEG_EN,
453                 MLX5_TXQ_MAX_INLINE_LEN,
454                 MLX5_TSO,
455                 MLX5_TX_VEC_EN,
456                 MLX5_RX_VEC_EN,
457                 NULL,
458         };
459         struct rte_kvargs *kvlist;
460         int ret = 0;
461         int i;
462
463         if (devargs == NULL)
464                 return 0;
465         /* Following UGLY cast is done to pass checkpatch. */
466         kvlist = rte_kvargs_parse(devargs->args, params);
467         if (kvlist == NULL)
468                 return 0;
469         /* Process parameters. */
470         for (i = 0; (params[i] != NULL); ++i) {
471                 if (rte_kvargs_count(kvlist, params[i])) {
472                         ret = rte_kvargs_process(kvlist, params[i],
473                                                  mlx5_args_check, args);
474                         if (ret != 0) {
475                                 rte_kvargs_free(kvlist);
476                                 return ret;
477                         }
478                 }
479         }
480         rte_kvargs_free(kvlist);
481         return 0;
482 }
483
484 static struct rte_pci_driver mlx5_driver;
485
486 /**
487  * Assign parameters from args into priv, only non default
488  * values are considered.
489  *
490  * @param[out] priv
491  *   Pointer to private structure.
492  * @param[in] args
493  *   Pointer to args values.
494  */
495 static void
496 mlx5_args_assign(struct priv *priv, struct mlx5_args *args)
497 {
498         if (args->cqe_comp != MLX5_ARG_UNSET)
499                 priv->cqe_comp = args->cqe_comp;
500         if (args->txq_inline != MLX5_ARG_UNSET)
501                 priv->txq_inline = args->txq_inline;
502         if (args->txqs_inline != MLX5_ARG_UNSET)
503                 priv->txqs_inline = args->txqs_inline;
504         if (args->mps != MLX5_ARG_UNSET)
505                 priv->mps = args->mps ? priv->mps : 0;
506         if (args->mpw_hdr_dseg != MLX5_ARG_UNSET)
507                 priv->mpw_hdr_dseg = args->mpw_hdr_dseg;
508         if (args->inline_max_packet_sz != MLX5_ARG_UNSET)
509                 priv->inline_max_packet_sz = args->inline_max_packet_sz;
510         if (args->tso != MLX5_ARG_UNSET)
511                 priv->tso = args->tso;
512         if (args->tx_vec_en != MLX5_ARG_UNSET)
513                 priv->tx_vec_en = args->tx_vec_en;
514         if (args->rx_vec_en != MLX5_ARG_UNSET)
515                 priv->rx_vec_en = args->rx_vec_en;
516 }
517
518 /**
519  * DPDK callback to register a PCI device.
520  *
521  * This function creates an Ethernet device for each port of a given
522  * PCI device.
523  *
524  * @param[in] pci_drv
525  *   PCI driver structure (mlx5_driver).
526  * @param[in] pci_dev
527  *   PCI device information.
528  *
529  * @return
530  *   0 on success, negative errno value on failure.
531  */
532 static int
533 mlx5_pci_probe(struct rte_pci_driver *pci_drv, struct rte_pci_device *pci_dev)
534 {
535         struct ibv_device **list;
536         struct ibv_device *ibv_dev;
537         int err = 0;
538         struct ibv_context *attr_ctx = NULL;
539         struct ibv_device_attr_ex device_attr;
540         unsigned int sriov;
541         unsigned int mps;
542         unsigned int tunnel_en = 0;
543         int idx;
544         int i;
545         struct mlx5dv_context attrs_out;
546
547         (void)pci_drv;
548         assert(pci_drv == &mlx5_driver);
549         /* Get mlx5_dev[] index. */
550         idx = mlx5_dev_idx(&pci_dev->addr);
551         if (idx == -1) {
552                 ERROR("this driver cannot support any more adapters");
553                 return -ENOMEM;
554         }
555         DEBUG("using driver device index %d", idx);
556
557         /* Save PCI address. */
558         mlx5_dev[idx].pci_addr = pci_dev->addr;
559         list = ibv_get_device_list(&i);
560         if (list == NULL) {
561                 assert(errno);
562                 if (errno == ENOSYS)
563                         ERROR("cannot list devices, is ib_uverbs loaded?");
564                 return -errno;
565         }
566         assert(i >= 0);
567         /*
568          * For each listed device, check related sysfs entry against
569          * the provided PCI ID.
570          */
571         while (i != 0) {
572                 struct rte_pci_addr pci_addr;
573
574                 --i;
575                 DEBUG("checking device \"%s\"", list[i]->name);
576                 if (mlx5_ibv_device_to_pci_addr(list[i], &pci_addr))
577                         continue;
578                 if ((pci_dev->addr.domain != pci_addr.domain) ||
579                     (pci_dev->addr.bus != pci_addr.bus) ||
580                     (pci_dev->addr.devid != pci_addr.devid) ||
581                     (pci_dev->addr.function != pci_addr.function))
582                         continue;
583                 sriov = ((pci_dev->id.device_id ==
584                        PCI_DEVICE_ID_MELLANOX_CONNECTX4VF) ||
585                       (pci_dev->id.device_id ==
586                        PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF) ||
587                       (pci_dev->id.device_id ==
588                        PCI_DEVICE_ID_MELLANOX_CONNECTX5VF) ||
589                       (pci_dev->id.device_id ==
590                        PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF));
591                 switch (pci_dev->id.device_id) {
592                 case PCI_DEVICE_ID_MELLANOX_CONNECTX4:
593                         tunnel_en = 1;
594                         break;
595                 case PCI_DEVICE_ID_MELLANOX_CONNECTX4LX:
596                 case PCI_DEVICE_ID_MELLANOX_CONNECTX5:
597                 case PCI_DEVICE_ID_MELLANOX_CONNECTX5VF:
598                 case PCI_DEVICE_ID_MELLANOX_CONNECTX5EX:
599                 case PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF:
600                         tunnel_en = 1;
601                         break;
602                 default:
603                         break;
604                 }
605                 INFO("PCI information matches, using device \"%s\""
606                      " (SR-IOV: %s)",
607                      list[i]->name,
608                      sriov ? "true" : "false");
609                 attr_ctx = ibv_open_device(list[i]);
610                 err = errno;
611                 break;
612         }
613         if (attr_ctx == NULL) {
614                 ibv_free_device_list(list);
615                 switch (err) {
616                 case 0:
617                         ERROR("cannot access device, is mlx5_ib loaded?");
618                         return -ENODEV;
619                 case EINVAL:
620                         ERROR("cannot use device, are drivers up to date?");
621                         return -EINVAL;
622                 }
623                 assert(err > 0);
624                 return -err;
625         }
626         ibv_dev = list[i];
627
628         DEBUG("device opened");
629         /*
630          * Multi-packet send is supported by ConnectX-4 Lx PF as well
631          * as all ConnectX-5 devices.
632          */
633         mlx5dv_query_device(attr_ctx, &attrs_out);
634         if (attrs_out.flags & (MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW |
635                                MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED)) {
636                 INFO("Enhanced MPW is detected\n");
637                 mps = MLX5_MPW_ENHANCED;
638         } else if (attrs_out.flags & MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED) {
639                 INFO("MPW is detected\n");
640                 mps = MLX5_MPW;
641         } else {
642                 INFO("MPW is disabled\n");
643                 mps = MLX5_MPW_DISABLED;
644         }
645         if (ibv_query_device_ex(attr_ctx, NULL, &device_attr))
646                 goto error;
647         INFO("%u port(s) detected", device_attr.orig_attr.phys_port_cnt);
648
649         for (i = 0; i < device_attr.orig_attr.phys_port_cnt; i++) {
650                 uint32_t port = i + 1; /* ports are indexed from one */
651                 uint32_t test = (1 << i);
652                 struct ibv_context *ctx = NULL;
653                 struct ibv_port_attr port_attr;
654                 struct ibv_pd *pd = NULL;
655                 struct priv *priv = NULL;
656                 struct rte_eth_dev *eth_dev;
657                 struct ibv_device_attr_ex device_attr_ex;
658                 struct ether_addr mac;
659                 uint16_t num_vfs = 0;
660                 struct mlx5_args args = {
661                         .cqe_comp = MLX5_ARG_UNSET,
662                         .txq_inline = MLX5_ARG_UNSET,
663                         .txqs_inline = MLX5_ARG_UNSET,
664                         .mps = MLX5_ARG_UNSET,
665                         .mpw_hdr_dseg = MLX5_ARG_UNSET,
666                         .inline_max_packet_sz = MLX5_ARG_UNSET,
667                         .tso = MLX5_ARG_UNSET,
668                         .tx_vec_en = MLX5_ARG_UNSET,
669                         .rx_vec_en = MLX5_ARG_UNSET,
670                 };
671
672                 mlx5_dev[idx].ports |= test;
673
674                 if (mlx5_is_secondary()) {
675                         /* from rte_ethdev.c */
676                         char name[RTE_ETH_NAME_MAX_LEN];
677
678                         snprintf(name, sizeof(name), "%s port %u",
679                                  ibv_get_device_name(ibv_dev), port);
680                         eth_dev = rte_eth_dev_attach_secondary(name);
681                         if (eth_dev == NULL) {
682                                 ERROR("can not attach rte ethdev");
683                                 err = ENOMEM;
684                                 goto error;
685                         }
686                         eth_dev->device = &pci_dev->device;
687                         eth_dev->dev_ops = &mlx5_dev_sec_ops;
688                         priv = eth_dev->data->dev_private;
689                         /* Receive command fd from primary process */
690                         err = priv_socket_connect(priv);
691                         if (err < 0) {
692                                 err = -err;
693                                 goto error;
694                         }
695                         /* Remap UAR for Tx queues. */
696                         err = priv_tx_uar_remap(priv, err);
697                         if (err < 0) {
698                                 err = -err;
699                                 goto error;
700                         }
701                         priv_dev_select_rx_function(priv, eth_dev);
702                         priv_dev_select_tx_function(priv, eth_dev);
703                         continue;
704                 }
705
706                 DEBUG("using port %u (%08" PRIx32 ")", port, test);
707
708                 ctx = ibv_open_device(ibv_dev);
709                 if (ctx == NULL) {
710                         err = ENODEV;
711                         goto port_error;
712                 }
713
714                 /* Check port status. */
715                 err = ibv_query_port(ctx, port, &port_attr);
716                 if (err) {
717                         ERROR("port query failed: %s", strerror(err));
718                         goto port_error;
719                 }
720
721                 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
722                         ERROR("port %d is not configured in Ethernet mode",
723                               port);
724                         err = EINVAL;
725                         goto port_error;
726                 }
727
728                 if (port_attr.state != IBV_PORT_ACTIVE)
729                         DEBUG("port %d is not active: \"%s\" (%d)",
730                               port, ibv_port_state_str(port_attr.state),
731                               port_attr.state);
732
733                 /* Allocate protection domain. */
734                 pd = ibv_alloc_pd(ctx);
735                 if (pd == NULL) {
736                         ERROR("PD allocation failure");
737                         err = ENOMEM;
738                         goto port_error;
739                 }
740
741                 mlx5_dev[idx].ports |= test;
742
743                 /* from rte_ethdev.c */
744                 priv = rte_zmalloc("ethdev private structure",
745                                    sizeof(*priv),
746                                    RTE_CACHE_LINE_SIZE);
747                 if (priv == NULL) {
748                         ERROR("priv allocation failure");
749                         err = ENOMEM;
750                         goto port_error;
751                 }
752
753                 priv->ctx = ctx;
754                 strncpy(priv->ibdev_path, priv->ctx->device->ibdev_path,
755                         sizeof(priv->ibdev_path));
756                 priv->device_attr = device_attr;
757                 priv->port = port;
758                 priv->pd = pd;
759                 priv->mtu = ETHER_MTU;
760                 priv->mps = mps; /* Enable MPW by default if supported. */
761                 priv->cqe_comp = 1; /* Enable compression by default. */
762                 priv->tunnel_en = tunnel_en;
763                 /* Enable vector by default if supported. */
764                 priv->tx_vec_en = 1;
765                 priv->rx_vec_en = 1;
766                 err = mlx5_args(&args, pci_dev->device.devargs);
767                 if (err) {
768                         ERROR("failed to process device arguments: %s",
769                               strerror(err));
770                         goto port_error;
771                 }
772                 mlx5_args_assign(priv, &args);
773                 if (ibv_query_device_ex(ctx, NULL, &device_attr_ex)) {
774                         ERROR("ibv_query_device_ex() failed");
775                         goto port_error;
776                 }
777
778                 priv->hw_csum =
779                         !!(device_attr_ex.device_cap_flags_ex &
780                            IBV_DEVICE_RAW_IP_CSUM);
781                 DEBUG("checksum offloading is %ssupported",
782                       (priv->hw_csum ? "" : "not "));
783
784 #ifdef HAVE_IBV_DEVICE_VXLAN_SUPPORT
785                 priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
786                                          IBV_DEVICE_VXLAN_SUPPORT);
787 #endif
788                 DEBUG("L2 tunnel checksum offloads are %ssupported",
789                       (priv->hw_csum_l2tun ? "" : "not "));
790
791                 priv->ind_table_max_size =
792                         device_attr_ex.rss_caps.max_rwq_indirection_table_size;
793                 /* Remove this check once DPDK supports larger/variable
794                  * indirection tables. */
795                 if (priv->ind_table_max_size >
796                                 (unsigned int)ETH_RSS_RETA_SIZE_512)
797                         priv->ind_table_max_size = ETH_RSS_RETA_SIZE_512;
798                 DEBUG("maximum RX indirection table size is %u",
799                       priv->ind_table_max_size);
800                 priv->hw_vlan_strip = !!(device_attr_ex.raw_packet_caps &
801                                          IBV_RAW_PACKET_CAP_CVLAN_STRIPPING);
802                 DEBUG("VLAN stripping is %ssupported",
803                       (priv->hw_vlan_strip ? "" : "not "));
804
805                 priv->hw_fcs_strip =
806                                 !!(device_attr_ex.orig_attr.device_cap_flags &
807                                 IBV_WQ_FLAGS_SCATTER_FCS);
808                 DEBUG("FCS stripping configuration is %ssupported",
809                       (priv->hw_fcs_strip ? "" : "not "));
810
811 #ifdef HAVE_IBV_WQ_FLAG_RX_END_PADDING
812                 priv->hw_padding = !!device_attr_ex.rx_pad_end_addr_align;
813 #endif
814                 DEBUG("hardware RX end alignment padding is %ssupported",
815                       (priv->hw_padding ? "" : "not "));
816
817                 priv_get_num_vfs(priv, &num_vfs);
818                 priv->sriov = (num_vfs || sriov);
819                 priv->tso = ((priv->tso) &&
820                             (device_attr_ex.tso_caps.max_tso > 0) &&
821                             (device_attr_ex.tso_caps.supported_qpts &
822                             (1 << IBV_QPT_RAW_PACKET)));
823                 if (priv->tso)
824                         priv->max_tso_payload_sz =
825                                 device_attr_ex.tso_caps.max_tso;
826                 if (priv->mps && !mps) {
827                         ERROR("multi-packet send not supported on this device"
828                               " (" MLX5_TXQ_MPW_EN ")");
829                         err = ENOTSUP;
830                         goto port_error;
831                 } else if (priv->mps && priv->tso) {
832                         WARN("multi-packet send not supported in conjunction "
833                               "with TSO. MPS disabled");
834                         priv->mps = 0;
835                 }
836                 INFO("%sMPS is %s",
837                      priv->mps == MLX5_MPW_ENHANCED ? "Enhanced " : "",
838                      priv->mps != MLX5_MPW_DISABLED ? "enabled" : "disabled");
839                 /* Set default values for Enhanced MPW, a.k.a MPWv2. */
840                 if (priv->mps == MLX5_MPW_ENHANCED) {
841                         if (args.txqs_inline == MLX5_ARG_UNSET)
842                                 priv->txqs_inline = MLX5_EMPW_MIN_TXQS;
843                         if (args.inline_max_packet_sz == MLX5_ARG_UNSET)
844                                 priv->inline_max_packet_sz =
845                                         MLX5_EMPW_MAX_INLINE_LEN;
846                         if (args.txq_inline == MLX5_ARG_UNSET)
847                                 priv->txq_inline = MLX5_WQE_SIZE_MAX -
848                                                    MLX5_WQE_SIZE;
849                 }
850                 /* Configure the first MAC address by default. */
851                 if (priv_get_mac(priv, &mac.addr_bytes)) {
852                         ERROR("cannot get MAC address, is mlx5_en loaded?"
853                               " (errno: %s)", strerror(errno));
854                         err = ENODEV;
855                         goto port_error;
856                 }
857                 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
858                      priv->port,
859                      mac.addr_bytes[0], mac.addr_bytes[1],
860                      mac.addr_bytes[2], mac.addr_bytes[3],
861                      mac.addr_bytes[4], mac.addr_bytes[5]);
862 #ifndef NDEBUG
863                 {
864                         char ifname[IF_NAMESIZE];
865
866                         if (priv_get_ifname(priv, &ifname) == 0)
867                                 DEBUG("port %u ifname is \"%s\"",
868                                       priv->port, ifname);
869                         else
870                                 DEBUG("port %u ifname is unknown", priv->port);
871                 }
872 #endif
873                 /* Get actual MTU if possible. */
874                 priv_get_mtu(priv, &priv->mtu);
875                 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
876
877                 /* from rte_ethdev.c */
878                 {
879                         char name[RTE_ETH_NAME_MAX_LEN];
880
881                         snprintf(name, sizeof(name), "%s port %u",
882                                  ibv_get_device_name(ibv_dev), port);
883                         eth_dev = rte_eth_dev_allocate(name);
884                 }
885                 if (eth_dev == NULL) {
886                         ERROR("can not allocate rte ethdev");
887                         err = ENOMEM;
888                         goto port_error;
889                 }
890                 eth_dev->data->dev_private = priv;
891                 eth_dev->data->mac_addrs = priv->mac;
892                 eth_dev->device = &pci_dev->device;
893                 rte_eth_copy_pci_info(eth_dev, pci_dev);
894                 eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
895                 eth_dev->device->driver = &mlx5_driver.driver;
896                 priv->dev = eth_dev;
897                 eth_dev->dev_ops = &mlx5_dev_ops;
898                 /* Register MAC address. */
899                 claim_zero(mlx5_mac_addr_add(eth_dev, &mac, 0, 0));
900                 TAILQ_INIT(&priv->flows);
901                 TAILQ_INIT(&priv->ctrl_flows);
902
903                 /* Hint libmlx5 to use PMD allocator for data plane resources */
904                 struct mlx5dv_ctx_allocators alctr = {
905                         .alloc = &mlx5_alloc_verbs_buf,
906                         .free = &mlx5_free_verbs_buf,
907                         .data = priv,
908                 };
909                 mlx5dv_set_context_attr(ctx, MLX5DV_CTX_ATTR_BUF_ALLOCATORS,
910                                         (void *)((uintptr_t)&alctr));
911
912                 /* Bring Ethernet device up. */
913                 DEBUG("forcing Ethernet interface up");
914                 priv_set_flags(priv, ~IFF_UP, IFF_UP);
915                 mlx5_link_update(priv->dev, 1);
916                 continue;
917
918 port_error:
919                 if (priv)
920                         rte_free(priv);
921                 if (pd)
922                         claim_zero(ibv_dealloc_pd(pd));
923                 if (ctx)
924                         claim_zero(ibv_close_device(ctx));
925                 break;
926         }
927
928         /*
929          * XXX if something went wrong in the loop above, there is a resource
930          * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
931          * long as the dpdk does not provide a way to deallocate a ethdev and a
932          * way to enumerate the registered ethdevs to free the previous ones.
933          */
934
935         /* no port found, complain */
936         if (!mlx5_dev[idx].ports) {
937                 err = ENODEV;
938                 goto error;
939         }
940
941 error:
942         if (attr_ctx)
943                 claim_zero(ibv_close_device(attr_ctx));
944         if (list)
945                 ibv_free_device_list(list);
946         assert(err >= 0);
947         return -err;
948 }
949
950 static const struct rte_pci_id mlx5_pci_id_map[] = {
951         {
952                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
953                                PCI_DEVICE_ID_MELLANOX_CONNECTX4)
954         },
955         {
956                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
957                                PCI_DEVICE_ID_MELLANOX_CONNECTX4VF)
958         },
959         {
960                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
961                                PCI_DEVICE_ID_MELLANOX_CONNECTX4LX)
962         },
963         {
964                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
965                                PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF)
966         },
967         {
968                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
969                                PCI_DEVICE_ID_MELLANOX_CONNECTX5)
970         },
971         {
972                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
973                                PCI_DEVICE_ID_MELLANOX_CONNECTX5VF)
974         },
975         {
976                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
977                                PCI_DEVICE_ID_MELLANOX_CONNECTX5EX)
978         },
979         {
980                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
981                                PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF)
982         },
983         {
984                 .vendor_id = 0
985         }
986 };
987
988 static struct rte_pci_driver mlx5_driver = {
989         .driver = {
990                 .name = MLX5_DRIVER_NAME
991         },
992         .id_table = mlx5_pci_id_map,
993         .probe = mlx5_pci_probe,
994         .drv_flags = RTE_PCI_DRV_INTR_LSC | RTE_PCI_DRV_INTR_RMV,
995 };
996
997 /**
998  * Driver initialization routine.
999  */
1000 RTE_INIT(rte_mlx5_pmd_init);
1001 static void
1002 rte_mlx5_pmd_init(void)
1003 {
1004         /* Build the static table for ptype conversion. */
1005         mlx5_set_ptype_table();
1006         /*
1007          * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
1008          * huge pages. Calling ibv_fork_init() during init allows
1009          * applications to use fork() safely for purposes other than
1010          * using this PMD, which is not supported in forked processes.
1011          */
1012         setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
1013         /* Don't map UAR to WC if BlueFlame is not used.*/
1014         setenv("MLX5_SHUT_UP_BF", "1", 1);
1015         /* Match the size of Rx completion entry to the size of a cacheline. */
1016         if (RTE_CACHE_LINE_SIZE == 128)
1017                 setenv("MLX5_CQE_SIZE", "128", 0);
1018         ibv_fork_init();
1019         rte_pci_register(&mlx5_driver);
1020 }
1021
1022 RTE_PMD_EXPORT_NAME(net_mlx5, __COUNTER__);
1023 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5, mlx5_pci_id_map);
1024 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5, "* ib_uverbs & mlx5_core & mlx5_ib");