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