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