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