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