net/mlx5: remove useless empty lines
[dpdk.git] / drivers / net / mlx5 / mlx5.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2015 6WIND S.A.
3  * Copyright 2015 Mellanox.
4  */
5
6 #include <stddef.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <assert.h>
10 #include <dlfcn.h>
11 #include <stdint.h>
12 #include <stdlib.h>
13 #include <errno.h>
14 #include <net/if.h>
15 #include <sys/mman.h>
16
17 /* Verbs header. */
18 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
19 #ifdef PEDANTIC
20 #pragma GCC diagnostic ignored "-Wpedantic"
21 #endif
22 #include <infiniband/verbs.h>
23 #ifdef PEDANTIC
24 #pragma GCC diagnostic error "-Wpedantic"
25 #endif
26
27 #include <rte_malloc.h>
28 #include <rte_ethdev_driver.h>
29 #include <rte_ethdev_pci.h>
30 #include <rte_pci.h>
31 #include <rte_bus_pci.h>
32 #include <rte_common.h>
33 #include <rte_config.h>
34 #include <rte_eal_memconfig.h>
35 #include <rte_kvargs.h>
36
37 #include "mlx5.h"
38 #include "mlx5_utils.h"
39 #include "mlx5_rxtx.h"
40 #include "mlx5_autoconf.h"
41 #include "mlx5_defs.h"
42 #include "mlx5_glue.h"
43
44 /* Device parameter to enable RX completion queue compression. */
45 #define MLX5_RXQ_CQE_COMP_EN "rxq_cqe_comp_en"
46
47 /* Device parameter to configure inline send. */
48 #define MLX5_TXQ_INLINE "txq_inline"
49
50 /*
51  * Device parameter to configure the number of TX queues threshold for
52  * enabling inline send.
53  */
54 #define MLX5_TXQS_MIN_INLINE "txqs_min_inline"
55
56 /* Device parameter to enable multi-packet send WQEs. */
57 #define MLX5_TXQ_MPW_EN "txq_mpw_en"
58
59 /* Device parameter to include 2 dsegs in the title WQEBB. */
60 #define MLX5_TXQ_MPW_HDR_DSEG_EN "txq_mpw_hdr_dseg_en"
61
62 /* Device parameter to limit the size of inlining packet. */
63 #define MLX5_TXQ_MAX_INLINE_LEN "txq_max_inline_len"
64
65 /* Device parameter to enable hardware Tx vector. */
66 #define MLX5_TX_VEC_EN "tx_vec_en"
67
68 /* Device parameter to enable hardware Rx vector. */
69 #define MLX5_RX_VEC_EN "rx_vec_en"
70
71 #ifndef HAVE_IBV_MLX5_MOD_MPW
72 #define MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED (1 << 2)
73 #define MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW (1 << 3)
74 #endif
75
76 #ifndef HAVE_IBV_MLX5_MOD_CQE_128B_COMP
77 #define MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP (1 << 4)
78 #endif
79
80 /**
81  * Retrieve integer value from environment variable.
82  *
83  * @param[in] name
84  *   Environment variable name.
85  *
86  * @return
87  *   Integer value, 0 if the variable is not set.
88  */
89 int
90 mlx5_getenv_int(const char *name)
91 {
92         const char *val = getenv(name);
93
94         if (val == NULL)
95                 return 0;
96         return atoi(val);
97 }
98
99 /**
100  * Verbs callback to allocate a memory. This function should allocate the space
101  * according to the size provided residing inside a huge page.
102  * Please note that all allocation must respect the alignment from libmlx5
103  * (i.e. currently sysconf(_SC_PAGESIZE)).
104  *
105  * @param[in] size
106  *   The size in bytes of the memory to allocate.
107  * @param[in] data
108  *   A pointer to the callback data.
109  *
110  * @return
111  *   a pointer to the allocate space.
112  */
113 static void *
114 mlx5_alloc_verbs_buf(size_t size, void *data)
115 {
116         struct priv *priv = data;
117         void *ret;
118         size_t alignment = sysconf(_SC_PAGESIZE);
119         unsigned int socket = SOCKET_ID_ANY;
120
121         if (priv->verbs_alloc_ctx.type == MLX5_VERBS_ALLOC_TYPE_TX_QUEUE) {
122                 const struct mlx5_txq_ctrl *ctrl = priv->verbs_alloc_ctx.obj;
123
124                 socket = ctrl->socket;
125         } else if (priv->verbs_alloc_ctx.type ==
126                    MLX5_VERBS_ALLOC_TYPE_RX_QUEUE) {
127                 const struct mlx5_rxq_ctrl *ctrl = priv->verbs_alloc_ctx.obj;
128
129                 socket = ctrl->socket;
130         }
131         assert(data != NULL);
132         ret = rte_malloc_socket(__func__, size, alignment, socket);
133         DEBUG("Extern alloc size: %lu, align: %lu: %p", size, alignment, ret);
134         return ret;
135 }
136
137 /**
138  * Verbs callback to free a memory.
139  *
140  * @param[in] ptr
141  *   A pointer to the memory to free.
142  * @param[in] data
143  *   A pointer to the callback data.
144  */
145 static void
146 mlx5_free_verbs_buf(void *ptr, void *data __rte_unused)
147 {
148         assert(data != NULL);
149         DEBUG("Extern free request: %p", ptr);
150         rte_free(ptr);
151 }
152
153 /**
154  * DPDK callback to close the device.
155  *
156  * Destroy all queues and objects, free memory.
157  *
158  * @param dev
159  *   Pointer to Ethernet device structure.
160  */
161 static void
162 mlx5_dev_close(struct rte_eth_dev *dev)
163 {
164         struct priv *priv = dev->data->dev_private;
165         unsigned int i;
166         int ret;
167
168         priv_lock(priv);
169         DEBUG("%p: closing device \"%s\"",
170               (void *)dev,
171               ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
172         /* In case mlx5_dev_stop() has not been called. */
173         priv_dev_interrupt_handler_uninstall(priv, dev);
174         priv_dev_traffic_disable(priv, dev);
175         /* Prevent crashes when queues are still in use. */
176         dev->rx_pkt_burst = removed_rx_burst;
177         dev->tx_pkt_burst = removed_tx_burst;
178         if (priv->rxqs != NULL) {
179                 /* XXX race condition if mlx5_rx_burst() is still running. */
180                 usleep(1000);
181                 for (i = 0; (i != priv->rxqs_n); ++i)
182                         mlx5_priv_rxq_release(priv, i);
183                 priv->rxqs_n = 0;
184                 priv->rxqs = NULL;
185         }
186         if (priv->txqs != NULL) {
187                 /* XXX race condition if mlx5_tx_burst() is still running. */
188                 usleep(1000);
189                 for (i = 0; (i != priv->txqs_n); ++i)
190                         mlx5_priv_txq_release(priv, i);
191                 priv->txqs_n = 0;
192                 priv->txqs = NULL;
193         }
194         if (priv->pd != NULL) {
195                 assert(priv->ctx != NULL);
196                 claim_zero(mlx5_glue->dealloc_pd(priv->pd));
197                 claim_zero(mlx5_glue->close_device(priv->ctx));
198         } else
199                 assert(priv->ctx == NULL);
200         if (priv->rss_conf.rss_key != NULL)
201                 rte_free(priv->rss_conf.rss_key);
202         if (priv->reta_idx != NULL)
203                 rte_free(priv->reta_idx);
204         if (priv->primary_socket)
205                 priv_socket_uninit(priv);
206         ret = mlx5_priv_hrxq_ibv_verify(priv);
207         if (ret)
208                 WARN("%p: some Hash Rx queue still remain", (void *)priv);
209         ret = mlx5_priv_ind_table_ibv_verify(priv);
210         if (ret)
211                 WARN("%p: some Indirection table still remain", (void *)priv);
212         ret = mlx5_priv_rxq_ibv_verify(priv);
213         if (ret)
214                 WARN("%p: some Verbs Rx queue still remain", (void *)priv);
215         ret = mlx5_priv_rxq_verify(priv);
216         if (ret)
217                 WARN("%p: some Rx Queues still remain", (void *)priv);
218         ret = mlx5_priv_txq_ibv_verify(priv);
219         if (ret)
220                 WARN("%p: some Verbs Tx queue still remain", (void *)priv);
221         ret = mlx5_priv_txq_verify(priv);
222         if (ret)
223                 WARN("%p: some Tx Queues still remain", (void *)priv);
224         ret = priv_flow_verify(priv);
225         if (ret)
226                 WARN("%p: some flows still remain", (void *)priv);
227         ret = priv_mr_verify(priv);
228         if (ret)
229                 WARN("%p: some Memory Region still remain", (void *)priv);
230         priv_unlock(priv);
231         memset(priv, 0, sizeof(*priv));
232 }
233
234 const struct eth_dev_ops mlx5_dev_ops = {
235         .dev_configure = mlx5_dev_configure,
236         .dev_start = mlx5_dev_start,
237         .dev_stop = mlx5_dev_stop,
238         .dev_set_link_down = mlx5_set_link_down,
239         .dev_set_link_up = mlx5_set_link_up,
240         .dev_close = mlx5_dev_close,
241         .promiscuous_enable = mlx5_promiscuous_enable,
242         .promiscuous_disable = mlx5_promiscuous_disable,
243         .allmulticast_enable = mlx5_allmulticast_enable,
244         .allmulticast_disable = mlx5_allmulticast_disable,
245         .link_update = mlx5_link_update,
246         .stats_get = mlx5_stats_get,
247         .stats_reset = mlx5_stats_reset,
248         .xstats_get = mlx5_xstats_get,
249         .xstats_reset = mlx5_xstats_reset,
250         .xstats_get_names = mlx5_xstats_get_names,
251         .dev_infos_get = mlx5_dev_infos_get,
252         .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
253         .vlan_filter_set = mlx5_vlan_filter_set,
254         .rx_queue_setup = mlx5_rx_queue_setup,
255         .tx_queue_setup = mlx5_tx_queue_setup,
256         .rx_queue_release = mlx5_rx_queue_release,
257         .tx_queue_release = mlx5_tx_queue_release,
258         .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
259         .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
260         .mac_addr_remove = mlx5_mac_addr_remove,
261         .mac_addr_add = mlx5_mac_addr_add,
262         .mac_addr_set = mlx5_mac_addr_set,
263         .mtu_set = mlx5_dev_set_mtu,
264         .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
265         .vlan_offload_set = mlx5_vlan_offload_set,
266         .reta_update = mlx5_dev_rss_reta_update,
267         .reta_query = mlx5_dev_rss_reta_query,
268         .rss_hash_update = mlx5_rss_hash_update,
269         .rss_hash_conf_get = mlx5_rss_hash_conf_get,
270         .filter_ctrl = mlx5_dev_filter_ctrl,
271         .rx_descriptor_status = mlx5_rx_descriptor_status,
272         .tx_descriptor_status = mlx5_tx_descriptor_status,
273         .rx_queue_intr_enable = mlx5_rx_intr_enable,
274         .rx_queue_intr_disable = mlx5_rx_intr_disable,
275         .is_removed = mlx5_is_removed,
276 };
277
278 static const struct eth_dev_ops mlx5_dev_sec_ops = {
279         .stats_get = mlx5_stats_get,
280         .stats_reset = mlx5_stats_reset,
281         .xstats_get = mlx5_xstats_get,
282         .xstats_reset = mlx5_xstats_reset,
283         .xstats_get_names = mlx5_xstats_get_names,
284         .dev_infos_get = mlx5_dev_infos_get,
285         .rx_descriptor_status = mlx5_rx_descriptor_status,
286         .tx_descriptor_status = mlx5_tx_descriptor_status,
287 };
288
289 /* Available operators in flow isolated mode. */
290 const struct eth_dev_ops mlx5_dev_ops_isolate = {
291         .dev_configure = mlx5_dev_configure,
292         .dev_start = mlx5_dev_start,
293         .dev_stop = mlx5_dev_stop,
294         .dev_set_link_down = mlx5_set_link_down,
295         .dev_set_link_up = mlx5_set_link_up,
296         .dev_close = mlx5_dev_close,
297         .link_update = mlx5_link_update,
298         .stats_get = mlx5_stats_get,
299         .stats_reset = mlx5_stats_reset,
300         .xstats_get = mlx5_xstats_get,
301         .xstats_reset = mlx5_xstats_reset,
302         .xstats_get_names = mlx5_xstats_get_names,
303         .dev_infos_get = mlx5_dev_infos_get,
304         .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
305         .vlan_filter_set = mlx5_vlan_filter_set,
306         .rx_queue_setup = mlx5_rx_queue_setup,
307         .tx_queue_setup = mlx5_tx_queue_setup,
308         .rx_queue_release = mlx5_rx_queue_release,
309         .tx_queue_release = mlx5_tx_queue_release,
310         .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
311         .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
312         .mac_addr_remove = mlx5_mac_addr_remove,
313         .mac_addr_add = mlx5_mac_addr_add,
314         .mac_addr_set = mlx5_mac_addr_set,
315         .mtu_set = mlx5_dev_set_mtu,
316         .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
317         .vlan_offload_set = mlx5_vlan_offload_set,
318         .filter_ctrl = mlx5_dev_filter_ctrl,
319         .rx_descriptor_status = mlx5_rx_descriptor_status,
320         .tx_descriptor_status = mlx5_tx_descriptor_status,
321         .rx_queue_intr_enable = mlx5_rx_intr_enable,
322         .rx_queue_intr_disable = mlx5_rx_intr_disable,
323         .is_removed = mlx5_is_removed,
324 };
325
326 static struct {
327         struct rte_pci_addr pci_addr; /* associated PCI address */
328         uint32_t ports; /* physical ports bitfield. */
329 } mlx5_dev[32];
330
331 /**
332  * Get device index in mlx5_dev[] from PCI bus address.
333  *
334  * @param[in] pci_addr
335  *   PCI bus address to look for.
336  *
337  * @return
338  *   mlx5_dev[] index on success, -1 on failure.
339  */
340 static int
341 mlx5_dev_idx(struct rte_pci_addr *pci_addr)
342 {
343         unsigned int i;
344         int ret = -1;
345
346         assert(pci_addr != NULL);
347         for (i = 0; (i != RTE_DIM(mlx5_dev)); ++i) {
348                 if ((mlx5_dev[i].pci_addr.domain == pci_addr->domain) &&
349                     (mlx5_dev[i].pci_addr.bus == pci_addr->bus) &&
350                     (mlx5_dev[i].pci_addr.devid == pci_addr->devid) &&
351                     (mlx5_dev[i].pci_addr.function == pci_addr->function))
352                         return i;
353                 if ((mlx5_dev[i].ports == 0) && (ret == -1))
354                         ret = i;
355         }
356         return ret;
357 }
358
359 /**
360  * Verify and store value for device argument.
361  *
362  * @param[in] key
363  *   Key argument to verify.
364  * @param[in] val
365  *   Value associated with key.
366  * @param opaque
367  *   User data.
368  *
369  * @return
370  *   0 on success, negative errno value on failure.
371  */
372 static int
373 mlx5_args_check(const char *key, const char *val, void *opaque)
374 {
375         struct mlx5_dev_config *config = opaque;
376         unsigned long tmp;
377
378         errno = 0;
379         tmp = strtoul(val, NULL, 0);
380         if (errno) {
381                 WARN("%s: \"%s\" is not a valid integer", key, val);
382                 return errno;
383         }
384         if (strcmp(MLX5_RXQ_CQE_COMP_EN, key) == 0) {
385                 config->cqe_comp = !!tmp;
386         } else if (strcmp(MLX5_TXQ_INLINE, key) == 0) {
387                 config->txq_inline = tmp;
388         } else if (strcmp(MLX5_TXQS_MIN_INLINE, key) == 0) {
389                 config->txqs_inline = tmp;
390         } else if (strcmp(MLX5_TXQ_MPW_EN, key) == 0) {
391                 config->mps = !!tmp ? config->mps : 0;
392         } else if (strcmp(MLX5_TXQ_MPW_HDR_DSEG_EN, key) == 0) {
393                 config->mpw_hdr_dseg = !!tmp;
394         } else if (strcmp(MLX5_TXQ_MAX_INLINE_LEN, key) == 0) {
395                 config->inline_max_packet_sz = tmp;
396         } else if (strcmp(MLX5_TX_VEC_EN, key) == 0) {
397                 config->tx_vec_en = !!tmp;
398         } else if (strcmp(MLX5_RX_VEC_EN, key) == 0) {
399                 config->rx_vec_en = !!tmp;
400         } else {
401                 WARN("%s: unknown parameter", key);
402                 return -EINVAL;
403         }
404         return 0;
405 }
406
407 /**
408  * Parse device parameters.
409  *
410  * @param config
411  *   Pointer to device configuration structure.
412  * @param devargs
413  *   Device arguments structure.
414  *
415  * @return
416  *   0 on success, errno value on failure.
417  */
418 static int
419 mlx5_args(struct mlx5_dev_config *config, struct rte_devargs *devargs)
420 {
421         const char **params = (const char *[]){
422                 MLX5_RXQ_CQE_COMP_EN,
423                 MLX5_TXQ_INLINE,
424                 MLX5_TXQS_MIN_INLINE,
425                 MLX5_TXQ_MPW_EN,
426                 MLX5_TXQ_MPW_HDR_DSEG_EN,
427                 MLX5_TXQ_MAX_INLINE_LEN,
428                 MLX5_TX_VEC_EN,
429                 MLX5_RX_VEC_EN,
430                 NULL,
431         };
432         struct rte_kvargs *kvlist;
433         int ret = 0;
434         int i;
435
436         if (devargs == NULL)
437                 return 0;
438         /* Following UGLY cast is done to pass checkpatch. */
439         kvlist = rte_kvargs_parse(devargs->args, params);
440         if (kvlist == NULL)
441                 return 0;
442         /* Process parameters. */
443         for (i = 0; (params[i] != NULL); ++i) {
444                 if (rte_kvargs_count(kvlist, params[i])) {
445                         ret = rte_kvargs_process(kvlist, params[i],
446                                                  mlx5_args_check, config);
447                         if (ret != 0) {
448                                 rte_kvargs_free(kvlist);
449                                 return ret;
450                         }
451                 }
452         }
453         rte_kvargs_free(kvlist);
454         return 0;
455 }
456
457 static struct rte_pci_driver mlx5_driver;
458
459 /*
460  * Reserved UAR address space for TXQ UAR(hw doorbell) mapping, process
461  * local resource used by both primary and secondary to avoid duplicate
462  * reservation.
463  * The space has to be available on both primary and secondary process,
464  * TXQ UAR maps to this area using fixed mmap w/o double check.
465  */
466 static void *uar_base;
467
468 /**
469  * Reserve UAR address space for primary process.
470  *
471  * @param[in] priv
472  *   Pointer to private structure.
473  *
474  * @return
475  *   0 on success, errno value on failure.
476  */
477 static int
478 priv_uar_init_primary(struct priv *priv)
479 {
480         void *addr = (void *)0;
481         int i;
482         const struct rte_mem_config *mcfg;
483         int ret;
484
485         if (uar_base) { /* UAR address space mapped. */
486                 priv->uar_base = uar_base;
487                 return 0;
488         }
489         /* find out lower bound of hugepage segments */
490         mcfg = rte_eal_get_configuration()->mem_config;
491         for (i = 0; i < RTE_MAX_MEMSEG && mcfg->memseg[i].addr; i++) {
492                 if (addr)
493                         addr = RTE_MIN(addr, mcfg->memseg[i].addr);
494                 else
495                         addr = mcfg->memseg[i].addr;
496         }
497         /* keep distance to hugepages to minimize potential conflicts. */
498         addr = RTE_PTR_SUB(addr, MLX5_UAR_OFFSET + MLX5_UAR_SIZE);
499         /* anonymous mmap, no real memory consumption. */
500         addr = mmap(addr, MLX5_UAR_SIZE,
501                     PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
502         if (addr == MAP_FAILED) {
503                 ERROR("Failed to reserve UAR address space, please adjust "
504                       "MLX5_UAR_SIZE or try --base-virtaddr");
505                 ret = ENOMEM;
506                 return ret;
507         }
508         /* Accept either same addr or a new addr returned from mmap if target
509          * range occupied.
510          */
511         INFO("Reserved UAR address space: %p", addr);
512         priv->uar_base = addr; /* for primary and secondary UAR re-mmap. */
513         uar_base = addr; /* process local, don't reserve again. */
514         return 0;
515 }
516
517 /**
518  * Reserve UAR address space for secondary process, align with
519  * primary process.
520  *
521  * @param[in] priv
522  *   Pointer to private structure.
523  *
524  * @return
525  *   0 on success, errno value on failure.
526  */
527 static int
528 priv_uar_init_secondary(struct priv *priv)
529 {
530         void *addr;
531         int ret;
532
533         assert(priv->uar_base);
534         if (uar_base) { /* already reserved. */
535                 assert(uar_base == priv->uar_base);
536                 return 0;
537         }
538         /* anonymous mmap, no real memory consumption. */
539         addr = mmap(priv->uar_base, MLX5_UAR_SIZE,
540                     PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
541         if (addr == MAP_FAILED) {
542                 ERROR("UAR mmap failed: %p size: %llu",
543                       priv->uar_base, MLX5_UAR_SIZE);
544                 ret = ENXIO;
545                 return ret;
546         }
547         if (priv->uar_base != addr) {
548                 ERROR("UAR address %p size %llu occupied, please adjust "
549                       "MLX5_UAR_OFFSET or try EAL parameter --base-virtaddr",
550                       priv->uar_base, MLX5_UAR_SIZE);
551                 ret = ENXIO;
552                 return ret;
553         }
554         uar_base = addr; /* process local, don't reserve again */
555         INFO("Reserved UAR address space: %p", addr);
556         return 0;
557 }
558
559 /**
560  * DPDK callback to register a PCI device.
561  *
562  * This function creates an Ethernet device for each port of a given
563  * PCI device.
564  *
565  * @param[in] pci_drv
566  *   PCI driver structure (mlx5_driver).
567  * @param[in] pci_dev
568  *   PCI device information.
569  *
570  * @return
571  *   0 on success, negative errno value on failure.
572  */
573 static int
574 mlx5_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
575                struct rte_pci_device *pci_dev)
576 {
577         struct ibv_device **list;
578         struct ibv_device *ibv_dev;
579         int err = 0;
580         struct ibv_context *attr_ctx = NULL;
581         struct ibv_device_attr_ex device_attr;
582         unsigned int mps;
583         unsigned int cqe_comp;
584         unsigned int tunnel_en = 0;
585         int idx;
586         int i;
587         struct mlx5dv_context attrs_out = {0};
588 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
589         struct ibv_counter_set_description cs_desc;
590 #endif
591
592         assert(pci_drv == &mlx5_driver);
593         /* Get mlx5_dev[] index. */
594         idx = mlx5_dev_idx(&pci_dev->addr);
595         if (idx == -1) {
596                 ERROR("this driver cannot support any more adapters");
597                 return -ENOMEM;
598         }
599         DEBUG("using driver device index %d", idx);
600         /* Save PCI address. */
601         mlx5_dev[idx].pci_addr = pci_dev->addr;
602         list = mlx5_glue->get_device_list(&i);
603         if (list == NULL) {
604                 assert(errno);
605                 if (errno == ENOSYS)
606                         ERROR("cannot list devices, is ib_uverbs loaded?");
607                 return -errno;
608         }
609         assert(i >= 0);
610         /*
611          * For each listed device, check related sysfs entry against
612          * the provided PCI ID.
613          */
614         while (i != 0) {
615                 struct rte_pci_addr pci_addr;
616
617                 --i;
618                 DEBUG("checking device \"%s\"", list[i]->name);
619                 if (mlx5_ibv_device_to_pci_addr(list[i], &pci_addr))
620                         continue;
621                 if ((pci_dev->addr.domain != pci_addr.domain) ||
622                     (pci_dev->addr.bus != pci_addr.bus) ||
623                     (pci_dev->addr.devid != pci_addr.devid) ||
624                     (pci_dev->addr.function != pci_addr.function))
625                         continue;
626                 INFO("PCI information matches, using device \"%s\"",
627                      list[i]->name);
628                 attr_ctx = mlx5_glue->open_device(list[i]);
629                 err = errno;
630                 break;
631         }
632         if (attr_ctx == NULL) {
633                 mlx5_glue->free_device_list(list);
634                 switch (err) {
635                 case 0:
636                         ERROR("cannot access device, is mlx5_ib loaded?");
637                         return -ENODEV;
638                 case EINVAL:
639                         ERROR("cannot use device, are drivers up to date?");
640                         return -EINVAL;
641                 }
642                 assert(err > 0);
643                 return -err;
644         }
645         ibv_dev = list[i];
646         DEBUG("device opened");
647         /*
648          * Multi-packet send is supported by ConnectX-4 Lx PF as well
649          * as all ConnectX-5 devices.
650          */
651 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
652         attrs_out.comp_mask |= MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS;
653 #endif
654         mlx5_glue->dv_query_device(attr_ctx, &attrs_out);
655         if (attrs_out.flags & MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED) {
656                 if (attrs_out.flags & MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW) {
657                         DEBUG("Enhanced MPW is supported");
658                         mps = MLX5_MPW_ENHANCED;
659                 } else {
660                         DEBUG("MPW is supported");
661                         mps = MLX5_MPW;
662                 }
663         } else {
664                 DEBUG("MPW isn't supported");
665                 mps = MLX5_MPW_DISABLED;
666         }
667         if (RTE_CACHE_LINE_SIZE == 128 &&
668             !(attrs_out.flags & MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP))
669                 cqe_comp = 0;
670         else
671                 cqe_comp = 1;
672 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
673         if (attrs_out.comp_mask & MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS) {
674                 tunnel_en = ((attrs_out.tunnel_offloads_caps &
675                               MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_VXLAN) &&
676                              (attrs_out.tunnel_offloads_caps &
677                               MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_GRE));
678         }
679         DEBUG("Tunnel offloading is %ssupported", tunnel_en ? "" : "not ");
680 #else
681         WARN("Tunnel offloading disabled due to old OFED/rdma-core version");
682 #endif
683         if (mlx5_glue->query_device_ex(attr_ctx, NULL, &device_attr))
684                 goto error;
685         INFO("%u port(s) detected", device_attr.orig_attr.phys_port_cnt);
686         for (i = 0; i < device_attr.orig_attr.phys_port_cnt; i++) {
687                 char name[RTE_ETH_NAME_MAX_LEN];
688                 int len;
689                 uint32_t port = i + 1; /* ports are indexed from one */
690                 uint32_t test = (1 << i);
691                 struct ibv_context *ctx = NULL;
692                 struct ibv_port_attr port_attr;
693                 struct ibv_pd *pd = NULL;
694                 struct priv *priv = NULL;
695                 struct rte_eth_dev *eth_dev;
696                 struct ibv_device_attr_ex device_attr_ex;
697                 struct ether_addr mac;
698                 struct ibv_device_attr_ex device_attr;
699                 struct mlx5_dev_config config = {
700                         .cqe_comp = cqe_comp,
701                         .mps = mps,
702                         .tunnel_en = tunnel_en,
703                         .tx_vec_en = 1,
704                         .rx_vec_en = 1,
705                         .mpw_hdr_dseg = 0,
706                         .txq_inline = MLX5_ARG_UNSET,
707                         .txqs_inline = MLX5_ARG_UNSET,
708                         .inline_max_packet_sz = MLX5_ARG_UNSET,
709                 };
710
711                 len = snprintf(name, sizeof(name), PCI_PRI_FMT,
712                          pci_dev->addr.domain, pci_dev->addr.bus,
713                          pci_dev->addr.devid, pci_dev->addr.function);
714                 if (device_attr.orig_attr.phys_port_cnt > 1)
715                         snprintf(name + len, sizeof(name), " port %u", i);
716                 mlx5_dev[idx].ports |= test;
717                 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
718                         eth_dev = rte_eth_dev_attach_secondary(name);
719                         if (eth_dev == NULL) {
720                                 ERROR("can not attach rte ethdev");
721                                 err = ENOMEM;
722                                 goto error;
723                         }
724                         eth_dev->device = &pci_dev->device;
725                         eth_dev->dev_ops = &mlx5_dev_sec_ops;
726                         priv = eth_dev->data->dev_private;
727                         err = priv_uar_init_secondary(priv);
728                         if (err < 0) {
729                                 err = -err;
730                                 goto error;
731                         }
732                         /* Receive command fd from primary process */
733                         err = priv_socket_connect(priv);
734                         if (err < 0) {
735                                 err = -err;
736                                 goto error;
737                         }
738                         /* Remap UAR for Tx queues. */
739                         err = priv_tx_uar_remap(priv, err);
740                         if (err)
741                                 goto error;
742                         /*
743                          * Ethdev pointer is still required as input since
744                          * the primary device is not accessible from the
745                          * secondary process.
746                          */
747                         eth_dev->rx_pkt_burst =
748                                 priv_select_rx_function(priv, eth_dev);
749                         eth_dev->tx_pkt_burst =
750                                 priv_select_tx_function(priv, eth_dev);
751                         continue;
752                 }
753                 DEBUG("using port %u (%08" PRIx32 ")", port, test);
754                 ctx = mlx5_glue->open_device(ibv_dev);
755                 if (ctx == NULL) {
756                         err = ENODEV;
757                         goto port_error;
758                 }
759                 mlx5_glue->query_device_ex(ctx, NULL, &device_attr);
760                 /* Check port status. */
761                 err = mlx5_glue->query_port(ctx, port, &port_attr);
762                 if (err) {
763                         ERROR("port query failed: %s", strerror(err));
764                         goto port_error;
765                 }
766                 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
767                         ERROR("port %d is not configured in Ethernet mode",
768                               port);
769                         err = EINVAL;
770                         goto port_error;
771                 }
772                 if (port_attr.state != IBV_PORT_ACTIVE)
773                         DEBUG("port %d is not active: \"%s\" (%d)",
774                               port, mlx5_glue->port_state_str(port_attr.state),
775                               port_attr.state);
776                 /* Allocate protection domain. */
777                 pd = mlx5_glue->alloc_pd(ctx);
778                 if (pd == NULL) {
779                         ERROR("PD allocation failure");
780                         err = ENOMEM;
781                         goto port_error;
782                 }
783                 mlx5_dev[idx].ports |= test;
784                 /* from rte_ethdev.c */
785                 priv = rte_zmalloc("ethdev private structure",
786                                    sizeof(*priv),
787                                    RTE_CACHE_LINE_SIZE);
788                 if (priv == NULL) {
789                         ERROR("priv allocation failure");
790                         err = ENOMEM;
791                         goto port_error;
792                 }
793                 priv->ctx = ctx;
794                 strncpy(priv->ibdev_path, priv->ctx->device->ibdev_path,
795                         sizeof(priv->ibdev_path));
796                 priv->device_attr = device_attr;
797                 priv->port = port;
798                 priv->pd = pd;
799                 priv->mtu = ETHER_MTU;
800                 err = mlx5_args(&config, pci_dev->device.devargs);
801                 if (err) {
802                         ERROR("failed to process device arguments: %s",
803                               strerror(err));
804                         goto port_error;
805                 }
806                 if (mlx5_glue->query_device_ex(ctx, NULL, &device_attr_ex)) {
807                         ERROR("ibv_query_device_ex() failed");
808                         goto port_error;
809                 }
810                 config.hw_csum = !!(device_attr_ex.device_cap_flags_ex &
811                                     IBV_DEVICE_RAW_IP_CSUM);
812                 DEBUG("checksum offloading is %ssupported",
813                       (config.hw_csum ? "" : "not "));
814 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
815                 config.flow_counter_en = !!(device_attr.max_counter_sets);
816                 mlx5_glue->describe_counter_set(ctx, 0, &cs_desc);
817                 DEBUG("counter type = %d, num of cs = %ld, attributes = %d",
818                       cs_desc.counter_type, cs_desc.num_of_cs,
819                       cs_desc.attributes);
820 #endif
821                 config.ind_table_max_size =
822                         device_attr_ex.rss_caps.max_rwq_indirection_table_size;
823                 /* Remove this check once DPDK supports larger/variable
824                  * indirection tables. */
825                 if (config.ind_table_max_size >
826                                 (unsigned int)ETH_RSS_RETA_SIZE_512)
827                         config.ind_table_max_size = ETH_RSS_RETA_SIZE_512;
828                 DEBUG("maximum RX indirection table size is %u",
829                       config.ind_table_max_size);
830                 config.hw_vlan_strip = !!(device_attr_ex.raw_packet_caps &
831                                          IBV_RAW_PACKET_CAP_CVLAN_STRIPPING);
832                 DEBUG("VLAN stripping is %ssupported",
833                       (config.hw_vlan_strip ? "" : "not "));
834
835                 config.hw_fcs_strip = !!(device_attr_ex.raw_packet_caps &
836                                          IBV_RAW_PACKET_CAP_SCATTER_FCS);
837                 DEBUG("FCS stripping configuration is %ssupported",
838                       (config.hw_fcs_strip ? "" : "not "));
839
840 #ifdef HAVE_IBV_WQ_FLAG_RX_END_PADDING
841                 config.hw_padding = !!device_attr_ex.rx_pad_end_addr_align;
842 #endif
843                 DEBUG("hardware RX end alignment padding is %ssupported",
844                       (config.hw_padding ? "" : "not "));
845                 config.tso = ((device_attr_ex.tso_caps.max_tso > 0) &&
846                               (device_attr_ex.tso_caps.supported_qpts &
847                               (1 << IBV_QPT_RAW_PACKET)));
848                 if (config.tso)
849                         config.tso_max_payload_sz =
850                                         device_attr_ex.tso_caps.max_tso;
851                 if (config.mps && !mps) {
852                         ERROR("multi-packet send not supported on this device"
853                               " (" MLX5_TXQ_MPW_EN ")");
854                         err = ENOTSUP;
855                         goto port_error;
856                 }
857                 INFO("%sMPS is %s",
858                      config.mps == MLX5_MPW_ENHANCED ? "Enhanced " : "",
859                      config.mps != MLX5_MPW_DISABLED ? "enabled" : "disabled");
860                 if (config.cqe_comp && !cqe_comp) {
861                         WARN("Rx CQE compression isn't supported");
862                         config.cqe_comp = 0;
863                 }
864                 err = priv_uar_init_primary(priv);
865                 if (err)
866                         goto port_error;
867                 /* Configure the first MAC address by default. */
868                 if (priv_get_mac(priv, &mac.addr_bytes)) {
869                         ERROR("cannot get MAC address, is mlx5_en loaded?"
870                               " (errno: %s)", strerror(errno));
871                         err = ENODEV;
872                         goto port_error;
873                 }
874                 INFO("port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
875                      priv->port,
876                      mac.addr_bytes[0], mac.addr_bytes[1],
877                      mac.addr_bytes[2], mac.addr_bytes[3],
878                      mac.addr_bytes[4], mac.addr_bytes[5]);
879 #ifndef NDEBUG
880                 {
881                         char ifname[IF_NAMESIZE];
882
883                         if (priv_get_ifname(priv, &ifname) == 0)
884                                 DEBUG("port %u ifname is \"%s\"",
885                                       priv->port, ifname);
886                         else
887                                 DEBUG("port %u ifname is unknown", priv->port);
888                 }
889 #endif
890                 /* Get actual MTU if possible. */
891                 priv_get_mtu(priv, &priv->mtu);
892                 DEBUG("port %u MTU is %u", priv->port, priv->mtu);
893                 eth_dev = rte_eth_dev_allocate(name);
894                 if (eth_dev == NULL) {
895                         ERROR("can not allocate rte ethdev");
896                         err = ENOMEM;
897                         goto port_error;
898                 }
899                 eth_dev->data->dev_private = priv;
900                 eth_dev->data->mac_addrs = priv->mac;
901                 eth_dev->device = &pci_dev->device;
902                 rte_eth_copy_pci_info(eth_dev, pci_dev);
903                 eth_dev->device->driver = &mlx5_driver.driver;
904                 /*
905                  * Initialize burst functions to prevent crashes before link-up.
906                  */
907                 eth_dev->rx_pkt_burst = removed_rx_burst;
908                 eth_dev->tx_pkt_burst = removed_tx_burst;
909                 priv->dev = eth_dev;
910                 eth_dev->dev_ops = &mlx5_dev_ops;
911                 /* Register MAC address. */
912                 claim_zero(mlx5_mac_addr_add(eth_dev, &mac, 0, 0));
913                 TAILQ_INIT(&priv->flows);
914                 TAILQ_INIT(&priv->ctrl_flows);
915                 /* Hint libmlx5 to use PMD allocator for data plane resources */
916                 struct mlx5dv_ctx_allocators alctr = {
917                         .alloc = &mlx5_alloc_verbs_buf,
918                         .free = &mlx5_free_verbs_buf,
919                         .data = priv,
920                 };
921                 mlx5_glue->dv_set_context_attr(ctx,
922                                                MLX5DV_CTX_ATTR_BUF_ALLOCATORS,
923                                                (void *)((uintptr_t)&alctr));
924
925                 /* Bring Ethernet device up. */
926                 DEBUG("forcing Ethernet interface up");
927                 priv_set_flags(priv, ~IFF_UP, IFF_UP);
928                 /* Store device configuration on private structure. */
929                 priv->config = config;
930                 continue;
931 port_error:
932                 if (priv)
933                         rte_free(priv);
934                 if (pd)
935                         claim_zero(mlx5_glue->dealloc_pd(pd));
936                 if (ctx)
937                         claim_zero(mlx5_glue->close_device(ctx));
938                 break;
939         }
940         /*
941          * XXX if something went wrong in the loop above, there is a resource
942          * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
943          * long as the dpdk does not provide a way to deallocate a ethdev and a
944          * way to enumerate the registered ethdevs to free the previous ones.
945          */
946         /* no port found, complain */
947         if (!mlx5_dev[idx].ports) {
948                 err = ENODEV;
949                 goto error;
950         }
951 error:
952         if (attr_ctx)
953                 claim_zero(mlx5_glue->close_device(attr_ctx));
954         if (list)
955                 mlx5_glue->free_device_list(list);
956         assert(err >= 0);
957         return -err;
958 }
959
960 static const struct rte_pci_id mlx5_pci_id_map[] = {
961         {
962                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
963                                PCI_DEVICE_ID_MELLANOX_CONNECTX4)
964         },
965         {
966                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
967                                PCI_DEVICE_ID_MELLANOX_CONNECTX4VF)
968         },
969         {
970                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
971                                PCI_DEVICE_ID_MELLANOX_CONNECTX4LX)
972         },
973         {
974                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
975                                PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF)
976         },
977         {
978                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
979                                PCI_DEVICE_ID_MELLANOX_CONNECTX5)
980         },
981         {
982                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
983                                PCI_DEVICE_ID_MELLANOX_CONNECTX5VF)
984         },
985         {
986                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
987                                PCI_DEVICE_ID_MELLANOX_CONNECTX5EX)
988         },
989         {
990                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
991                                PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF)
992         },
993         {
994                 .vendor_id = 0
995         }
996 };
997
998 static struct rte_pci_driver mlx5_driver = {
999         .driver = {
1000                 .name = MLX5_DRIVER_NAME
1001         },
1002         .id_table = mlx5_pci_id_map,
1003         .probe = mlx5_pci_probe,
1004         .drv_flags = RTE_PCI_DRV_INTR_LSC | RTE_PCI_DRV_INTR_RMV,
1005 };
1006
1007 #ifdef RTE_LIBRTE_MLX5_DLOPEN_DEPS
1008
1009 /**
1010  * Suffix RTE_EAL_PMD_PATH with "-glue".
1011  *
1012  * This function performs a sanity check on RTE_EAL_PMD_PATH before
1013  * suffixing its last component.
1014  *
1015  * @param buf[out]
1016  *   Output buffer, should be large enough otherwise NULL is returned.
1017  * @param size
1018  *   Size of @p out.
1019  *
1020  * @return
1021  *   Pointer to @p buf or @p NULL in case suffix cannot be appended.
1022  */
1023 static char *
1024 mlx5_glue_path(char *buf, size_t size)
1025 {
1026         static const char *const bad[] = { "/", ".", "..", NULL };
1027         const char *path = RTE_EAL_PMD_PATH;
1028         size_t len = strlen(path);
1029         size_t off;
1030         int i;
1031
1032         while (len && path[len - 1] == '/')
1033                 --len;
1034         for (off = len; off && path[off - 1] != '/'; --off)
1035                 ;
1036         for (i = 0; bad[i]; ++i)
1037                 if (!strncmp(path + off, bad[i], (int)(len - off)))
1038                         goto error;
1039         i = snprintf(buf, size, "%.*s-glue", (int)len, path);
1040         if (i == -1 || (size_t)i >= size)
1041                 goto error;
1042         return buf;
1043 error:
1044         ERROR("unable to append \"-glue\" to last component of"
1045               " RTE_EAL_PMD_PATH (\"" RTE_EAL_PMD_PATH "\"),"
1046               " please re-configure DPDK");
1047         return NULL;
1048 }
1049
1050 /**
1051  * Initialization routine for run-time dependency on rdma-core.
1052  */
1053 static int
1054 mlx5_glue_init(void)
1055 {
1056         char glue_path[sizeof(RTE_EAL_PMD_PATH) - 1 + sizeof("-glue")];
1057         const char *path[] = {
1058                 /*
1059                  * A basic security check is necessary before trusting
1060                  * MLX5_GLUE_PATH, which may override RTE_EAL_PMD_PATH.
1061                  */
1062                 (geteuid() == getuid() && getegid() == getgid() ?
1063                  getenv("MLX5_GLUE_PATH") : NULL),
1064                 /*
1065                  * When RTE_EAL_PMD_PATH is set, use its glue-suffixed
1066                  * variant, otherwise let dlopen() look up libraries on its
1067                  * own.
1068                  */
1069                 (*RTE_EAL_PMD_PATH ?
1070                  mlx5_glue_path(glue_path, sizeof(glue_path)) : ""),
1071         };
1072         unsigned int i = 0;
1073         void *handle = NULL;
1074         void **sym;
1075         const char *dlmsg;
1076
1077         while (!handle && i != RTE_DIM(path)) {
1078                 const char *end;
1079                 size_t len;
1080                 int ret;
1081
1082                 if (!path[i]) {
1083                         ++i;
1084                         continue;
1085                 }
1086                 end = strpbrk(path[i], ":;");
1087                 if (!end)
1088                         end = path[i] + strlen(path[i]);
1089                 len = end - path[i];
1090                 ret = 0;
1091                 do {
1092                         char name[ret + 1];
1093
1094                         ret = snprintf(name, sizeof(name), "%.*s%s" MLX5_GLUE,
1095                                        (int)len, path[i],
1096                                        (!len || *(end - 1) == '/') ? "" : "/");
1097                         if (ret == -1)
1098                                 break;
1099                         if (sizeof(name) != (size_t)ret + 1)
1100                                 continue;
1101                         DEBUG("looking for rdma-core glue as \"%s\"", name);
1102                         handle = dlopen(name, RTLD_LAZY);
1103                         break;
1104                 } while (1);
1105                 path[i] = end + 1;
1106                 if (!*end)
1107                         ++i;
1108         }
1109         if (!handle) {
1110                 rte_errno = EINVAL;
1111                 dlmsg = dlerror();
1112                 if (dlmsg)
1113                         WARN("cannot load glue library: %s", dlmsg);
1114                 goto glue_error;
1115         }
1116         sym = dlsym(handle, "mlx5_glue");
1117         if (!sym || !*sym) {
1118                 rte_errno = EINVAL;
1119                 dlmsg = dlerror();
1120                 if (dlmsg)
1121                         ERROR("cannot resolve glue symbol: %s", dlmsg);
1122                 goto glue_error;
1123         }
1124         mlx5_glue = *sym;
1125         return 0;
1126 glue_error:
1127         if (handle)
1128                 dlclose(handle);
1129         WARN("cannot initialize PMD due to missing run-time"
1130              " dependency on rdma-core libraries (libibverbs,"
1131              " libmlx5)");
1132         return -rte_errno;
1133 }
1134
1135 #endif
1136
1137 /**
1138  * Driver initialization routine.
1139  */
1140 RTE_INIT(rte_mlx5_pmd_init);
1141 static void
1142 rte_mlx5_pmd_init(void)
1143 {
1144         /* Build the static table for ptype conversion. */
1145         mlx5_set_ptype_table();
1146         /*
1147          * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
1148          * huge pages. Calling ibv_fork_init() during init allows
1149          * applications to use fork() safely for purposes other than
1150          * using this PMD, which is not supported in forked processes.
1151          */
1152         setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
1153         /* Match the size of Rx completion entry to the size of a cacheline. */
1154         if (RTE_CACHE_LINE_SIZE == 128)
1155                 setenv("MLX5_CQE_SIZE", "128", 0);
1156 #ifdef RTE_LIBRTE_MLX5_DLOPEN_DEPS
1157         if (mlx5_glue_init())
1158                 return;
1159         assert(mlx5_glue);
1160 #endif
1161 #ifndef NDEBUG
1162         /* Glue structure must not contain any NULL pointers. */
1163         {
1164                 unsigned int i;
1165
1166                 for (i = 0; i != sizeof(*mlx5_glue) / sizeof(void *); ++i)
1167                         assert(((const void *const *)mlx5_glue)[i]);
1168         }
1169 #endif
1170         if (strcmp(mlx5_glue->version, MLX5_GLUE_VERSION)) {
1171                 ERROR("rdma-core glue \"%s\" mismatch: \"%s\" is required",
1172                       mlx5_glue->version, MLX5_GLUE_VERSION);
1173                 return;
1174         }
1175         mlx5_glue->fork_init();
1176         rte_pci_register(&mlx5_driver);
1177 }
1178
1179 RTE_PMD_EXPORT_NAME(net_mlx5, __COUNTER__);
1180 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5, mlx5_pci_id_map);
1181 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5, "* ib_uverbs & mlx5_core & mlx5_ib");