4 * Copyright 2015 6WIND S.A.
5 * Copyright 2015 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
45 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
47 #pragma GCC diagnostic ignored "-Wpedantic"
49 #include <infiniband/verbs.h>
51 #pragma GCC diagnostic error "-Wpedantic"
54 #include <rte_malloc.h>
55 #include <rte_ethdev.h>
56 #include <rte_ethdev_pci.h>
58 #include <rte_bus_pci.h>
59 #include <rte_common.h>
60 #include <rte_eal_memconfig.h>
61 #include <rte_kvargs.h>
64 #include "mlx5_utils.h"
65 #include "mlx5_rxtx.h"
66 #include "mlx5_autoconf.h"
67 #include "mlx5_defs.h"
69 /* Device parameter to enable RX completion queue compression. */
70 #define MLX5_RXQ_CQE_COMP_EN "rxq_cqe_comp_en"
72 /* Device parameter to configure inline send. */
73 #define MLX5_TXQ_INLINE "txq_inline"
76 * Device parameter to configure the number of TX queues threshold for
77 * enabling inline send.
79 #define MLX5_TXQS_MIN_INLINE "txqs_min_inline"
81 /* Device parameter to enable multi-packet send WQEs. */
82 #define MLX5_TXQ_MPW_EN "txq_mpw_en"
84 /* Device parameter to include 2 dsegs in the title WQEBB. */
85 #define MLX5_TXQ_MPW_HDR_DSEG_EN "txq_mpw_hdr_dseg_en"
87 /* Device parameter to limit the size of inlining packet. */
88 #define MLX5_TXQ_MAX_INLINE_LEN "txq_max_inline_len"
90 /* Device parameter to enable hardware TSO offload. */
91 #define MLX5_TSO "tso"
93 /* Device parameter to enable hardware Tx vector. */
94 #define MLX5_TX_VEC_EN "tx_vec_en"
96 /* Device parameter to enable hardware Rx vector. */
97 #define MLX5_RX_VEC_EN "rx_vec_en"
99 /* Default PMD specific parameter value. */
100 #define MLX5_ARG_UNSET (-1)
102 #ifndef HAVE_IBV_MLX5_MOD_MPW
103 #define MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED (1 << 2)
104 #define MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW (1 << 3)
107 #ifndef HAVE_IBV_MLX5_MOD_CQE_128B_COMP
108 #define MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP (1 << 4)
117 int inline_max_packet_sz;
123 /** Driver-specific log messages type. */
127 * Retrieve integer value from environment variable.
130 * Environment variable name.
133 * Integer value, 0 if the variable is not set.
136 mlx5_getenv_int(const char *name)
138 const char *val = getenv(name);
146 * Verbs callback to allocate a memory. This function should allocate the space
147 * according to the size provided residing inside a huge page.
148 * Please note that all allocation must respect the alignment from libmlx5
149 * (i.e. currently sysconf(_SC_PAGESIZE)).
152 * The size in bytes of the memory to allocate.
154 * A pointer to the callback data.
157 * Allocated buffer, NULL otherwise and rte_errno is set.
160 mlx5_alloc_verbs_buf(size_t size, void *data)
162 struct priv *priv = data;
164 size_t alignment = sysconf(_SC_PAGESIZE);
165 unsigned int socket = SOCKET_ID_ANY;
167 if (priv->verbs_alloc_ctx.type == MLX5_VERBS_ALLOC_TYPE_TX_QUEUE) {
168 const struct mlx5_txq_ctrl *ctrl = priv->verbs_alloc_ctx.obj;
170 socket = ctrl->socket;
171 } else if (priv->verbs_alloc_ctx.type ==
172 MLX5_VERBS_ALLOC_TYPE_RX_QUEUE) {
173 const struct mlx5_rxq_ctrl *ctrl = priv->verbs_alloc_ctx.obj;
175 socket = ctrl->socket;
177 assert(data != NULL);
178 ret = rte_malloc_socket(__func__, size, alignment, socket);
185 * Verbs callback to free a memory.
188 * A pointer to the memory to free.
190 * A pointer to the callback data.
193 mlx5_free_verbs_buf(void *ptr, void *data __rte_unused)
195 assert(data != NULL);
200 * DPDK callback to close the device.
202 * Destroy all queues and objects, free memory.
205 * Pointer to Ethernet device structure.
208 mlx5_dev_close(struct rte_eth_dev *dev)
210 struct priv *priv = dev->data->dev_private;
214 DRV_LOG(DEBUG, "port %u closing device \"%s\"",
216 ((priv->ctx != NULL) ? priv->ctx->device->name : ""));
217 /* In case mlx5_dev_stop() has not been called. */
218 mlx5_dev_interrupt_handler_uninstall(dev);
219 mlx5_traffic_disable(dev);
220 /* Prevent crashes when queues are still in use. */
221 dev->rx_pkt_burst = removed_rx_burst;
222 dev->tx_pkt_burst = removed_tx_burst;
223 if (priv->rxqs != NULL) {
224 /* XXX race condition if mlx5_rx_burst() is still running. */
226 for (i = 0; (i != priv->rxqs_n); ++i)
227 mlx5_rxq_release(dev, i);
231 if (priv->txqs != NULL) {
232 /* XXX race condition if mlx5_tx_burst() is still running. */
234 for (i = 0; (i != priv->txqs_n); ++i)
235 mlx5_txq_release(dev, i);
239 if (priv->pd != NULL) {
240 assert(priv->ctx != NULL);
241 claim_zero(ibv_dealloc_pd(priv->pd));
242 claim_zero(ibv_close_device(priv->ctx));
244 assert(priv->ctx == NULL);
245 if (priv->rss_conf.rss_key != NULL)
246 rte_free(priv->rss_conf.rss_key);
247 if (priv->reta_idx != NULL)
248 rte_free(priv->reta_idx);
249 if (priv->primary_socket)
250 mlx5_socket_uninit(dev);
251 ret = mlx5_hrxq_ibv_verify(dev);
253 DRV_LOG(WARNING, "port %u some hash Rx queue still remain",
255 ret = mlx5_ind_table_ibv_verify(dev);
257 DRV_LOG(WARNING, "port %u some indirection table still remain",
259 ret = mlx5_rxq_ibv_verify(dev);
261 DRV_LOG(WARNING, "port %u some Verbs Rx queue still remain",
263 ret = mlx5_rxq_verify(dev);
265 DRV_LOG(WARNING, "port %u some Rx queues still remain",
267 ret = mlx5_txq_ibv_verify(dev);
269 DRV_LOG(WARNING, "port %u some Verbs Tx queue still remain",
271 ret = mlx5_txq_verify(dev);
273 DRV_LOG(WARNING, "port %u some Tx queues still remain",
275 ret = mlx5_flow_verify(dev);
277 DRV_LOG(WARNING, "port %u some flows still remain",
279 ret = mlx5_mr_verify(dev);
281 DRV_LOG(WARNING, "port %u some memory region still remain",
283 memset(priv, 0, sizeof(*priv));
286 const struct eth_dev_ops mlx5_dev_ops = {
287 .dev_configure = mlx5_dev_configure,
288 .dev_start = mlx5_dev_start,
289 .dev_stop = mlx5_dev_stop,
290 .dev_set_link_down = mlx5_set_link_down,
291 .dev_set_link_up = mlx5_set_link_up,
292 .dev_close = mlx5_dev_close,
293 .promiscuous_enable = mlx5_promiscuous_enable,
294 .promiscuous_disable = mlx5_promiscuous_disable,
295 .allmulticast_enable = mlx5_allmulticast_enable,
296 .allmulticast_disable = mlx5_allmulticast_disable,
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 .reta_update = mlx5_dev_rss_reta_update,
319 .reta_query = mlx5_dev_rss_reta_query,
320 .rss_hash_update = mlx5_rss_hash_update,
321 .rss_hash_conf_get = mlx5_rss_hash_conf_get,
322 .filter_ctrl = mlx5_dev_filter_ctrl,
323 .rx_descriptor_status = mlx5_rx_descriptor_status,
324 .tx_descriptor_status = mlx5_tx_descriptor_status,
325 .rx_queue_intr_enable = mlx5_rx_intr_enable,
326 .rx_queue_intr_disable = mlx5_rx_intr_disable,
329 static const struct eth_dev_ops mlx5_dev_sec_ops = {
330 .stats_get = mlx5_stats_get,
331 .stats_reset = mlx5_stats_reset,
332 .xstats_get = mlx5_xstats_get,
333 .xstats_reset = mlx5_xstats_reset,
334 .xstats_get_names = mlx5_xstats_get_names,
335 .dev_infos_get = mlx5_dev_infos_get,
336 .rx_descriptor_status = mlx5_rx_descriptor_status,
337 .tx_descriptor_status = mlx5_tx_descriptor_status,
340 /* Available operators in flow isolated mode. */
341 const struct eth_dev_ops mlx5_dev_ops_isolate = {
342 .dev_configure = mlx5_dev_configure,
343 .dev_start = mlx5_dev_start,
344 .dev_stop = mlx5_dev_stop,
345 .dev_set_link_down = mlx5_set_link_down,
346 .dev_set_link_up = mlx5_set_link_up,
347 .dev_close = mlx5_dev_close,
348 .promiscuous_enable = mlx5_promiscuous_enable,
349 .promiscuous_disable = mlx5_promiscuous_disable,
350 .allmulticast_enable = mlx5_allmulticast_enable,
351 .allmulticast_disable = mlx5_allmulticast_disable,
352 .link_update = mlx5_link_update,
353 .stats_get = mlx5_stats_get,
354 .stats_reset = mlx5_stats_reset,
355 .xstats_get = mlx5_xstats_get,
356 .xstats_reset = mlx5_xstats_reset,
357 .xstats_get_names = mlx5_xstats_get_names,
358 .dev_infos_get = mlx5_dev_infos_get,
359 .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
360 .vlan_filter_set = mlx5_vlan_filter_set,
361 .rx_queue_setup = mlx5_rx_queue_setup,
362 .tx_queue_setup = mlx5_tx_queue_setup,
363 .rx_queue_release = mlx5_rx_queue_release,
364 .tx_queue_release = mlx5_tx_queue_release,
365 .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
366 .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
367 .mac_addr_remove = mlx5_mac_addr_remove,
368 .mac_addr_add = mlx5_mac_addr_add,
369 .mac_addr_set = mlx5_mac_addr_set,
370 .mtu_set = mlx5_dev_set_mtu,
371 .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
372 .vlan_offload_set = mlx5_vlan_offload_set,
373 .filter_ctrl = mlx5_dev_filter_ctrl,
374 .rx_descriptor_status = mlx5_rx_descriptor_status,
375 .tx_descriptor_status = mlx5_tx_descriptor_status,
376 .rx_queue_intr_enable = mlx5_rx_intr_enable,
377 .rx_queue_intr_disable = mlx5_rx_intr_disable,
381 struct rte_pci_addr pci_addr; /* associated PCI address */
382 uint32_t ports; /* physical ports bitfield. */
386 * Get device index in mlx5_dev[] from PCI bus address.
388 * @param[in] pci_addr
389 * PCI bus address to look for.
392 * mlx5_dev[] index on success, -1 on failure.
395 mlx5_dev_idx(struct rte_pci_addr *pci_addr)
400 assert(pci_addr != NULL);
401 for (i = 0; (i != RTE_DIM(mlx5_dev)); ++i) {
402 if ((mlx5_dev[i].pci_addr.domain == pci_addr->domain) &&
403 (mlx5_dev[i].pci_addr.bus == pci_addr->bus) &&
404 (mlx5_dev[i].pci_addr.devid == pci_addr->devid) &&
405 (mlx5_dev[i].pci_addr.function == pci_addr->function))
407 if ((mlx5_dev[i].ports == 0) && (ret == -1))
414 * Verify and store value for device argument.
417 * Key argument to verify.
419 * Value associated with key.
424 * 0 on success, a negative errno value otherwise and rte_errno is set.
427 mlx5_args_check(const char *key, const char *val, void *opaque)
429 struct mlx5_args *args = opaque;
433 tmp = strtoul(val, NULL, 0);
436 DRV_LOG(WARNING, "%s: \"%s\" is not a valid integer", key, val);
439 if (strcmp(MLX5_RXQ_CQE_COMP_EN, key) == 0) {
440 args->cqe_comp = !!tmp;
441 } else if (strcmp(MLX5_TXQ_INLINE, key) == 0) {
442 args->txq_inline = tmp;
443 } else if (strcmp(MLX5_TXQS_MIN_INLINE, key) == 0) {
444 args->txqs_inline = tmp;
445 } else if (strcmp(MLX5_TXQ_MPW_EN, key) == 0) {
447 } else if (strcmp(MLX5_TXQ_MPW_HDR_DSEG_EN, key) == 0) {
448 args->mpw_hdr_dseg = !!tmp;
449 } else if (strcmp(MLX5_TXQ_MAX_INLINE_LEN, key) == 0) {
450 args->inline_max_packet_sz = tmp;
451 } else if (strcmp(MLX5_TSO, key) == 0) {
453 } else if (strcmp(MLX5_TX_VEC_EN, key) == 0) {
454 args->tx_vec_en = !!tmp;
455 } else if (strcmp(MLX5_RX_VEC_EN, key) == 0) {
456 args->rx_vec_en = !!tmp;
458 DRV_LOG(WARNING, "%s: unknown parameter", key);
466 * Parse device parameters.
469 * Pointer to private structure.
471 * Device arguments structure.
474 * 0 on success, a negative errno value otherwise and rte_errno is set.
477 mlx5_args(struct mlx5_args *args, struct rte_devargs *devargs)
479 const char **params = (const char *[]){
480 MLX5_RXQ_CQE_COMP_EN,
482 MLX5_TXQS_MIN_INLINE,
484 MLX5_TXQ_MPW_HDR_DSEG_EN,
485 MLX5_TXQ_MAX_INLINE_LEN,
491 struct rte_kvargs *kvlist;
497 /* Following UGLY cast is done to pass checkpatch. */
498 kvlist = rte_kvargs_parse(devargs->args, params);
501 /* Process parameters. */
502 for (i = 0; (params[i] != NULL); ++i) {
503 if (rte_kvargs_count(kvlist, params[i])) {
504 ret = rte_kvargs_process(kvlist, params[i],
505 mlx5_args_check, args);
508 rte_kvargs_free(kvlist);
513 rte_kvargs_free(kvlist);
517 static struct rte_pci_driver mlx5_driver;
520 * Reserved UAR address space for TXQ UAR(hw doorbell) mapping, process
521 * local resource used by both primary and secondary to avoid duplicate
523 * The space has to be available on both primary and secondary process,
524 * TXQ UAR maps to this area using fixed mmap w/o double check.
526 static void *uar_base;
529 * Reserve UAR address space for primary process.
532 * Pointer to Ethernet device.
535 * 0 on success, a negative errno value otherwise and rte_errno is set.
538 mlx5_uar_init_primary(struct rte_eth_dev *dev)
540 struct priv *priv = dev->data->dev_private;
541 void *addr = (void *)0;
543 const struct rte_mem_config *mcfg;
545 if (uar_base) { /* UAR address space mapped. */
546 priv->uar_base = uar_base;
549 /* find out lower bound of hugepage segments */
550 mcfg = rte_eal_get_configuration()->mem_config;
551 for (i = 0; i < RTE_MAX_MEMSEG && mcfg->memseg[i].addr; i++) {
553 addr = RTE_MIN(addr, mcfg->memseg[i].addr);
555 addr = mcfg->memseg[i].addr;
557 /* keep distance to hugepages to minimize potential conflicts. */
558 addr = RTE_PTR_SUB(addr, MLX5_UAR_OFFSET + MLX5_UAR_SIZE);
559 /* anonymous mmap, no real memory consumption. */
560 addr = mmap(addr, MLX5_UAR_SIZE,
561 PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
562 if (addr == MAP_FAILED) {
564 "port %u failed to reserve UAR address space, please"
565 " adjust MLX5_UAR_SIZE or try --base-virtaddr",
570 /* Accept either same addr or a new addr returned from mmap if target
573 DRV_LOG(INFO, "port %u reserved UAR address space: %p",
574 dev->data->port_id, addr);
575 priv->uar_base = addr; /* for primary and secondary UAR re-mmap. */
576 uar_base = addr; /* process local, don't reserve again. */
581 * Reserve UAR address space for secondary process, align with
585 * Pointer to Ethernet device.
588 * 0 on success, a negative errno value otherwise and rte_errno is set.
591 mlx5_uar_init_secondary(struct rte_eth_dev *dev)
593 struct priv *priv = dev->data->dev_private;
596 assert(priv->uar_base);
597 if (uar_base) { /* already reserved. */
598 assert(uar_base == priv->uar_base);
601 /* anonymous mmap, no real memory consumption. */
602 addr = mmap(priv->uar_base, MLX5_UAR_SIZE,
603 PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
604 if (addr == MAP_FAILED) {
605 DRV_LOG(ERR, "port %u UAR mmap failed: %p size: %llu",
606 dev->data->port_id, priv->uar_base, MLX5_UAR_SIZE);
610 if (priv->uar_base != addr) {
612 "port %u UAR address %p size %llu occupied, please"
613 " adjust MLX5_UAR_OFFSET or try EAL parameter"
615 dev->data->port_id, priv->uar_base, MLX5_UAR_SIZE);
619 uar_base = addr; /* process local, don't reserve again */
620 DRV_LOG(INFO, "port %u reserved UAR address space: %p",
621 dev->data->port_id, addr);
626 * Assign parameters from args into priv, only non default
627 * values are considered.
630 * Pointer to private structure.
632 * Pointer to args values.
635 mlx5_args_assign(struct priv *priv, struct mlx5_args *args)
637 if (args->cqe_comp != MLX5_ARG_UNSET)
638 priv->cqe_comp = args->cqe_comp;
639 if (args->txq_inline != MLX5_ARG_UNSET)
640 priv->txq_inline = args->txq_inline;
641 if (args->txqs_inline != MLX5_ARG_UNSET)
642 priv->txqs_inline = args->txqs_inline;
643 if (args->mps != MLX5_ARG_UNSET)
644 priv->mps = args->mps ? priv->mps : 0;
645 if (args->mpw_hdr_dseg != MLX5_ARG_UNSET)
646 priv->mpw_hdr_dseg = args->mpw_hdr_dseg;
647 if (args->inline_max_packet_sz != MLX5_ARG_UNSET)
648 priv->inline_max_packet_sz = args->inline_max_packet_sz;
649 if (args->tso != MLX5_ARG_UNSET)
650 priv->tso = args->tso;
651 if (args->tx_vec_en != MLX5_ARG_UNSET)
652 priv->tx_vec_en = args->tx_vec_en;
653 if (args->rx_vec_en != MLX5_ARG_UNSET)
654 priv->rx_vec_en = args->rx_vec_en;
658 * DPDK callback to register a PCI device.
660 * This function creates an Ethernet device for each port of a given
664 * PCI driver structure (mlx5_driver).
666 * PCI device information.
669 * 0 on success, a negative errno value otherwise and rte_errno is set.
672 mlx5_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
673 struct rte_pci_device *pci_dev)
675 struct ibv_device **list = NULL;
676 struct ibv_device *ibv_dev;
678 struct ibv_context *attr_ctx = NULL;
679 struct ibv_device_attr_ex device_attr;
681 unsigned int cqe_comp;
682 unsigned int tunnel_en = 0;
685 struct mlx5dv_context attrs_out;
686 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
687 struct ibv_counter_set_description cs_desc = { .counter_type = 0 };
690 assert(pci_drv == &mlx5_driver);
691 /* Get mlx5_dev[] index. */
692 idx = mlx5_dev_idx(&pci_dev->addr);
694 DRV_LOG(ERR, "this driver cannot support any more adapters");
698 DRV_LOG(DEBUG, "using driver device index %d", idx);
699 /* Save PCI address. */
700 mlx5_dev[idx].pci_addr = pci_dev->addr;
701 list = ibv_get_device_list(&i);
707 "cannot list devices, is ib_uverbs loaded?");
712 * For each listed device, check related sysfs entry against
713 * the provided PCI ID.
716 struct rte_pci_addr pci_addr;
719 DRV_LOG(DEBUG, "checking device \"%s\"", list[i]->name);
720 if (mlx5_ibv_device_to_pci_addr(list[i], &pci_addr))
722 if ((pci_dev->addr.domain != pci_addr.domain) ||
723 (pci_dev->addr.bus != pci_addr.bus) ||
724 (pci_dev->addr.devid != pci_addr.devid) ||
725 (pci_dev->addr.function != pci_addr.function))
727 switch (pci_dev->id.device_id) {
728 case PCI_DEVICE_ID_MELLANOX_CONNECTX4:
731 case PCI_DEVICE_ID_MELLANOX_CONNECTX4LX:
732 case PCI_DEVICE_ID_MELLANOX_CONNECTX5:
733 case PCI_DEVICE_ID_MELLANOX_CONNECTX5VF:
734 case PCI_DEVICE_ID_MELLANOX_CONNECTX5EX:
735 case PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF:
741 DRV_LOG(INFO, "PCI information matches, using device \"%s\"",
743 attr_ctx = ibv_open_device(list[i]);
748 if (attr_ctx == NULL) {
752 "cannot access device, is mlx5_ib loaded?");
757 "cannot use device, are drivers up to date?");
763 DRV_LOG(DEBUG, "device opened");
765 * Multi-packet send is supported by ConnectX-4 Lx PF as well
766 * as all ConnectX-5 devices.
768 mlx5dv_query_device(attr_ctx, &attrs_out);
769 if (attrs_out.flags & MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED) {
770 if (attrs_out.flags & MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW) {
771 DRV_LOG(DEBUG, "enhanced MPW is supported");
772 mps = MLX5_MPW_ENHANCED;
774 DRV_LOG(DEBUG, "MPW is supported");
778 DRV_LOG(DEBUG, "MPW isn't supported");
779 mps = MLX5_MPW_DISABLED;
781 if (RTE_CACHE_LINE_SIZE == 128 &&
782 !(attrs_out.flags & MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP))
786 err = ibv_query_device_ex(attr_ctx, NULL, &device_attr);
788 DEBUG("ibv_query_device_ex() failed");
791 DRV_LOG(INFO, "%u port(s) detected",
792 device_attr.orig_attr.phys_port_cnt);
793 for (i = 0; i < device_attr.orig_attr.phys_port_cnt; i++) {
794 char name[RTE_ETH_NAME_MAX_LEN];
795 uint32_t port = i + 1; /* ports are indexed from one */
796 uint32_t test = (1 << i);
797 struct ibv_context *ctx = NULL;
798 struct ibv_port_attr port_attr;
799 struct ibv_pd *pd = NULL;
800 struct priv *priv = NULL;
801 struct rte_eth_dev *eth_dev = NULL;
802 struct ibv_device_attr_ex device_attr_ex;
803 struct ether_addr mac;
804 struct mlx5_args args = {
805 .cqe_comp = MLX5_ARG_UNSET,
806 .txq_inline = MLX5_ARG_UNSET,
807 .txqs_inline = MLX5_ARG_UNSET,
808 .mps = MLX5_ARG_UNSET,
809 .mpw_hdr_dseg = MLX5_ARG_UNSET,
810 .inline_max_packet_sz = MLX5_ARG_UNSET,
811 .tso = MLX5_ARG_UNSET,
812 .tx_vec_en = MLX5_ARG_UNSET,
813 .rx_vec_en = MLX5_ARG_UNSET,
816 snprintf(name, sizeof(name), PCI_PRI_FMT,
817 pci_dev->addr.domain, pci_dev->addr.bus,
818 pci_dev->addr.devid, pci_dev->addr.function);
819 mlx5_dev[idx].ports |= test;
820 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
821 eth_dev = rte_eth_dev_attach_secondary(name);
822 if (eth_dev == NULL) {
823 DRV_LOG(ERR, "can not attach rte ethdev");
828 eth_dev->device = &pci_dev->device;
829 eth_dev->dev_ops = &mlx5_dev_sec_ops;
830 err = mlx5_uar_init_secondary(eth_dev);
835 /* Receive command fd from primary process */
836 err = mlx5_socket_connect(eth_dev);
841 /* Remap UAR for Tx queues. */
842 err = mlx5_tx_uar_remap(eth_dev, err);
848 * Ethdev pointer is still required as input since
849 * the primary device is not accessible from the
852 eth_dev->rx_pkt_burst =
853 mlx5_select_rx_function(eth_dev);
854 eth_dev->tx_pkt_burst =
855 mlx5_select_tx_function(eth_dev);
858 DRV_LOG(DEBUG, "using port %u (%08" PRIx32 ")", port, test);
859 ctx = ibv_open_device(ibv_dev);
864 /* Check port status. */
865 err = ibv_query_port(ctx, port, &port_attr);
867 DRV_LOG(ERR, "port query failed: %s", strerror(err));
870 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
872 "port %d is not configured in Ethernet mode",
877 if (port_attr.state != IBV_PORT_ACTIVE)
878 DRV_LOG(DEBUG, "port %d is not active: \"%s\" (%d)",
879 port, ibv_port_state_str(port_attr.state),
881 /* Allocate protection domain. */
882 pd = ibv_alloc_pd(ctx);
884 DRV_LOG(ERR, "PD allocation failure");
888 mlx5_dev[idx].ports |= test;
889 /* from rte_ethdev.c */
890 priv = rte_zmalloc("ethdev private structure",
892 RTE_CACHE_LINE_SIZE);
894 DRV_LOG(ERR, "priv allocation failure");
899 strncpy(priv->ibdev_path, priv->ctx->device->ibdev_path,
900 sizeof(priv->ibdev_path));
901 priv->device_attr = device_attr;
904 priv->mtu = ETHER_MTU;
905 priv->mps = mps; /* Enable MPW by default if supported. */
906 priv->cqe_comp = cqe_comp;
907 priv->tunnel_en = tunnel_en;
908 /* Enable vector by default if supported. */
911 err = mlx5_args(&args, pci_dev->device.devargs);
913 DRV_LOG(ERR, "failed to process device arguments: %s",
918 mlx5_args_assign(priv, &args);
919 err = ibv_query_device_ex(ctx, NULL, &device_attr_ex);
921 DRV_LOG(ERR, "ibv_query_device_ex() failed");
925 !!(device_attr_ex.device_cap_flags_ex &
926 IBV_DEVICE_RAW_IP_CSUM);
927 DRV_LOG(DEBUG, "checksum offloading is %ssupported",
928 (priv->hw_csum ? "" : "not "));
930 #ifdef HAVE_IBV_DEVICE_VXLAN_SUPPORT
931 priv->hw_csum_l2tun = !!(exp_device_attr.exp_device_cap_flags &
932 IBV_DEVICE_VXLAN_SUPPORT);
934 DRV_LOG(DEBUG, "Rx L2 tunnel checksum offloads are %ssupported",
935 (priv->hw_csum_l2tun ? "" : "not "));
937 #ifdef HAVE_IBV_DEVICE_COUNTERS_SET_SUPPORT
938 priv->counter_set_supported = !!(device_attr.max_counter_sets);
939 ibv_describe_counter_set(ctx, 0, &cs_desc);
941 "counter type = %d, num of cs = %ld, attributes = %d",
942 cs_desc.counter_type, cs_desc.num_of_cs,
945 priv->ind_table_max_size =
946 device_attr_ex.rss_caps.max_rwq_indirection_table_size;
947 /* Remove this check once DPDK supports larger/variable
948 * indirection tables. */
949 if (priv->ind_table_max_size >
950 (unsigned int)ETH_RSS_RETA_SIZE_512)
951 priv->ind_table_max_size = ETH_RSS_RETA_SIZE_512;
952 DRV_LOG(DEBUG, "maximum Rx indirection table size is %u",
953 priv->ind_table_max_size);
954 priv->hw_vlan_strip = !!(device_attr_ex.raw_packet_caps &
955 IBV_RAW_PACKET_CAP_CVLAN_STRIPPING);
956 DRV_LOG(DEBUG, "VLAN stripping is %ssupported",
957 (priv->hw_vlan_strip ? "" : "not "));
959 priv->hw_fcs_strip = !!(device_attr_ex.raw_packet_caps &
960 IBV_RAW_PACKET_CAP_SCATTER_FCS);
961 DRV_LOG(DEBUG, "FCS stripping configuration is %ssupported",
962 (priv->hw_fcs_strip ? "" : "not "));
964 #ifdef HAVE_IBV_WQ_FLAG_RX_END_PADDING
965 priv->hw_padding = !!device_attr_ex.rx_pad_end_addr_align;
968 "hardware Rx end alignment padding is %ssupported",
969 (priv->hw_padding ? "" : "not "));
970 priv->tso = ((priv->tso) &&
971 (device_attr_ex.tso_caps.max_tso > 0) &&
972 (device_attr_ex.tso_caps.supported_qpts &
973 (1 << IBV_QPT_RAW_PACKET)));
975 priv->max_tso_payload_sz =
976 device_attr_ex.tso_caps.max_tso;
977 if (priv->mps && !mps) {
979 "multi-packet send not supported on this device"
980 " (" MLX5_TXQ_MPW_EN ")");
983 } else if (priv->mps && priv->tso) {
985 "multi-packet send not supported in conjunction"
986 " with TSO. MPS disabled");
989 DRV_LOG(INFO, "%s MPS is %s",
990 priv->mps == MLX5_MPW_ENHANCED ? "enhanced " : "",
991 priv->mps != MLX5_MPW_DISABLED ? "enabled" :
993 /* Set default values for Enhanced MPW, a.k.a MPWv2. */
994 if (priv->mps == MLX5_MPW_ENHANCED) {
995 if (args.txqs_inline == MLX5_ARG_UNSET)
996 priv->txqs_inline = MLX5_EMPW_MIN_TXQS;
997 if (args.inline_max_packet_sz == MLX5_ARG_UNSET)
998 priv->inline_max_packet_sz =
999 MLX5_EMPW_MAX_INLINE_LEN;
1000 if (args.txq_inline == MLX5_ARG_UNSET)
1001 priv->txq_inline = MLX5_WQE_SIZE_MAX -
1004 if (priv->cqe_comp && !cqe_comp) {
1005 DRV_LOG(WARNING, "Rx CQE compression isn't supported");
1008 eth_dev = rte_eth_dev_allocate(name);
1009 if (eth_dev == NULL) {
1010 DRV_LOG(ERR, "can not allocate rte ethdev");
1014 eth_dev->data->dev_private = priv;
1015 priv->dev_data = eth_dev->data;
1016 eth_dev->data->mac_addrs = priv->mac;
1017 eth_dev->device = &pci_dev->device;
1018 rte_eth_copy_pci_info(eth_dev, pci_dev);
1019 eth_dev->device->driver = &mlx5_driver.driver;
1020 err = mlx5_uar_init_primary(eth_dev);
1025 /* Configure the first MAC address by default. */
1026 if (mlx5_get_mac(eth_dev, &mac.addr_bytes)) {
1028 "port %u cannot get MAC address, is mlx5_en"
1029 " loaded? (errno: %s)",
1030 eth_dev->data->port_id, strerror(errno));
1035 "port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
1036 eth_dev->data->port_id,
1037 mac.addr_bytes[0], mac.addr_bytes[1],
1038 mac.addr_bytes[2], mac.addr_bytes[3],
1039 mac.addr_bytes[4], mac.addr_bytes[5]);
1042 char ifname[IF_NAMESIZE];
1044 if (mlx5_get_ifname(eth_dev, &ifname) == 0)
1045 DRV_LOG(DEBUG, "port %u ifname is \"%s\"",
1046 eth_dev->data->port_id, ifname);
1048 DRV_LOG(DEBUG, "port %u ifname is unknown",
1049 eth_dev->data->port_id);
1052 /* Get actual MTU if possible. */
1053 err = mlx5_get_mtu(eth_dev, &priv->mtu);
1058 DRV_LOG(DEBUG, "port %u MTU is %u", eth_dev->data->port_id,
1061 * Initialize burst functions to prevent crashes before link-up.
1063 eth_dev->rx_pkt_burst = removed_rx_burst;
1064 eth_dev->tx_pkt_burst = removed_tx_burst;
1065 eth_dev->dev_ops = &mlx5_dev_ops;
1066 /* Register MAC address. */
1067 claim_zero(mlx5_mac_addr_add(eth_dev, &mac, 0, 0));
1068 TAILQ_INIT(&priv->flows);
1069 TAILQ_INIT(&priv->ctrl_flows);
1070 /* Hint libmlx5 to use PMD allocator for data plane resources */
1071 struct mlx5dv_ctx_allocators alctr = {
1072 .alloc = &mlx5_alloc_verbs_buf,
1073 .free = &mlx5_free_verbs_buf,
1076 mlx5dv_set_context_attr(ctx, MLX5DV_CTX_ATTR_BUF_ALLOCATORS,
1077 (void *)((uintptr_t)&alctr));
1078 /* Bring Ethernet device up. */
1079 DRV_LOG(DEBUG, "port %u forcing Ethernet interface up",
1080 eth_dev->data->port_id);
1081 mlx5_set_link_up(eth_dev);
1083 * Even though the interrupt handler is not installed yet,
1084 * interrupts will still trigger on the asyn_fd from
1085 * Verbs context returned by ibv_open_device().
1087 mlx5_link_update(eth_dev, 0);
1093 claim_zero(ibv_dealloc_pd(pd));
1095 claim_zero(ibv_close_device(ctx));
1096 if (eth_dev && rte_eal_process_type() == RTE_PROC_PRIMARY)
1097 rte_eth_dev_release_port(eth_dev);
1101 * XXX if something went wrong in the loop above, there is a resource
1102 * leak (ctx, pd, priv, dpdk ethdev) but we can do nothing about it as
1103 * long as the dpdk does not provide a way to deallocate a ethdev and a
1104 * way to enumerate the registered ethdevs to free the previous ones.
1106 /* no port found, complain */
1107 if (!mlx5_dev[idx].ports) {
1113 claim_zero(ibv_close_device(attr_ctx));
1115 ibv_free_device_list(list);
1123 static const struct rte_pci_id mlx5_pci_id_map[] = {
1125 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1126 PCI_DEVICE_ID_MELLANOX_CONNECTX4)
1129 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1130 PCI_DEVICE_ID_MELLANOX_CONNECTX4VF)
1133 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1134 PCI_DEVICE_ID_MELLANOX_CONNECTX4LX)
1137 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1138 PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF)
1141 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1142 PCI_DEVICE_ID_MELLANOX_CONNECTX5)
1145 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1146 PCI_DEVICE_ID_MELLANOX_CONNECTX5VF)
1149 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1150 PCI_DEVICE_ID_MELLANOX_CONNECTX5EX)
1153 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
1154 PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF)
1161 static struct rte_pci_driver mlx5_driver = {
1163 .name = MLX5_DRIVER_NAME
1165 .id_table = mlx5_pci_id_map,
1166 .probe = mlx5_pci_probe,
1167 .drv_flags = RTE_PCI_DRV_INTR_LSC | RTE_PCI_DRV_INTR_RMV,
1171 * Driver initialization routine.
1173 RTE_INIT(rte_mlx5_pmd_init);
1175 rte_mlx5_pmd_init(void)
1177 /* Build the static table for ptype conversion. */
1178 mlx5_set_ptype_table();
1180 * RDMAV_HUGEPAGES_SAFE tells ibv_fork_init() we intend to use
1181 * huge pages. Calling ibv_fork_init() during init allows
1182 * applications to use fork() safely for purposes other than
1183 * using this PMD, which is not supported in forked processes.
1185 setenv("RDMAV_HUGEPAGES_SAFE", "1", 1);
1186 /* Match the size of Rx completion entry to the size of a cacheline. */
1187 if (RTE_CACHE_LINE_SIZE == 128)
1188 setenv("MLX5_CQE_SIZE", "128", 0);
1190 rte_pci_register(&mlx5_driver);
1193 RTE_PMD_EXPORT_NAME(net_mlx5, __COUNTER__);
1194 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5, mlx5_pci_id_map);
1195 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5, "* ib_uverbs & mlx5_core & mlx5_ib");
1197 /** Initialize driver log type. */
1198 RTE_INIT(vdev_netvsc_init_log)
1200 mlx5_logtype = rte_log_register("pmd.net.mlx5");
1201 if (mlx5_logtype >= 0)
1202 rte_log_set_level(mlx5_logtype, RTE_LOG_NOTICE);