1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2015 6WIND S.A.
3 * Copyright 2020 Mellanox Technologies, Ltd
13 #include <linux/rtnetlink.h>
14 #include <linux/sockios.h>
15 #include <linux/ethtool.h>
19 /* ISO C doesn't support unnamed structs/unions, disabling -pedantic. */
21 #pragma GCC diagnostic ignored "-Wpedantic"
23 #include <infiniband/verbs.h>
25 #pragma GCC diagnostic error "-Wpedantic"
28 #include <rte_malloc.h>
29 #include <rte_ethdev_driver.h>
30 #include <rte_ethdev_pci.h>
32 #include <rte_bus_pci.h>
33 #include <rte_common.h>
34 #include <rte_kvargs.h>
35 #include <rte_rwlock.h>
36 #include <rte_spinlock.h>
37 #include <rte_string_fns.h>
38 #include <rte_alarm.h>
39 #include <rte_eal_paging.h>
41 #include <mlx5_glue.h>
42 #include <mlx5_devx_cmds.h>
43 #include <mlx5_common.h>
44 #include <mlx5_common_mp.h>
45 #include <mlx5_common_mr.h>
46 #include <mlx5_malloc.h>
48 #include "mlx5_defs.h"
50 #include "mlx5_common_os.h"
51 #include "mlx5_utils.h"
52 #include "mlx5_rxtx.h"
53 #include "mlx5_autoconf.h"
55 #include "mlx5_flow.h"
56 #include "rte_pmd_mlx5.h"
57 #include "mlx5_verbs.h"
59 #define MLX5_TAGS_HLIST_ARRAY_SIZE 8192
61 #ifndef HAVE_IBV_MLX5_MOD_MPW
62 #define MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED (1 << 2)
63 #define MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW (1 << 3)
66 #ifndef HAVE_IBV_MLX5_MOD_CQE_128B_COMP
67 #define MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP (1 << 4)
71 * Get MAC address by querying netdevice.
74 * Pointer to Ethernet device.
76 * MAC address output buffer.
79 * 0 on success, a negative errno value otherwise and rte_errno is set.
82 mlx5_get_mac(struct rte_eth_dev *dev, uint8_t (*mac)[RTE_ETHER_ADDR_LEN])
87 ret = mlx5_ifreq(dev, SIOCGIFHWADDR, &request);
90 memcpy(mac, request.ifr_hwaddr.sa_data, RTE_ETHER_ADDR_LEN);
95 * Get mlx5 device attributes. The glue function query_device_ex() is called
96 * with out parameter of type 'struct ibv_device_attr_ex *'. Then fill in mlx5
97 * device attributes from the glue out parameter.
100 * Pointer to ibv context.
103 * Pointer to mlx5 device attributes.
106 * 0 on success, non zero error number otherwise
109 mlx5_os_get_dev_attr(void *ctx, struct mlx5_dev_attr *device_attr)
112 struct ibv_device_attr_ex attr_ex;
113 memset(device_attr, 0, sizeof(*device_attr));
114 err = mlx5_glue->query_device_ex(ctx, NULL, &attr_ex);
118 device_attr->device_cap_flags_ex = attr_ex.device_cap_flags_ex;
119 device_attr->max_qp_wr = attr_ex.orig_attr.max_qp_wr;
120 device_attr->max_sge = attr_ex.orig_attr.max_sge;
121 device_attr->max_cq = attr_ex.orig_attr.max_cq;
122 device_attr->max_qp = attr_ex.orig_attr.max_qp;
123 device_attr->raw_packet_caps = attr_ex.raw_packet_caps;
124 device_attr->max_rwq_indirection_table_size =
125 attr_ex.rss_caps.max_rwq_indirection_table_size;
126 device_attr->max_tso = attr_ex.tso_caps.max_tso;
127 device_attr->tso_supported_qpts = attr_ex.tso_caps.supported_qpts;
129 struct mlx5dv_context dv_attr = { .comp_mask = 0 };
130 err = mlx5_glue->dv_query_device(ctx, &dv_attr);
134 device_attr->flags = dv_attr.flags;
135 device_attr->comp_mask = dv_attr.comp_mask;
136 #ifdef HAVE_IBV_MLX5_MOD_SWP
137 device_attr->sw_parsing_offloads =
138 dv_attr.sw_parsing_caps.sw_parsing_offloads;
140 device_attr->min_single_stride_log_num_of_bytes =
141 dv_attr.striding_rq_caps.min_single_stride_log_num_of_bytes;
142 device_attr->max_single_stride_log_num_of_bytes =
143 dv_attr.striding_rq_caps.max_single_stride_log_num_of_bytes;
144 device_attr->min_single_wqe_log_num_of_strides =
145 dv_attr.striding_rq_caps.min_single_wqe_log_num_of_strides;
146 device_attr->max_single_wqe_log_num_of_strides =
147 dv_attr.striding_rq_caps.max_single_wqe_log_num_of_strides;
148 device_attr->stride_supported_qpts =
149 dv_attr.striding_rq_caps.supported_qpts;
150 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
151 device_attr->tunnel_offloads_caps = dv_attr.tunnel_offloads_caps;
158 * Verbs callback to allocate a memory. This function should allocate the space
159 * according to the size provided residing inside a huge page.
160 * Please note that all allocation must respect the alignment from libmlx5
161 * (i.e. currently rte_mem_page_size()).
164 * The size in bytes of the memory to allocate.
166 * A pointer to the callback data.
169 * Allocated buffer, NULL otherwise and rte_errno is set.
172 mlx5_alloc_verbs_buf(size_t size, void *data)
174 struct mlx5_priv *priv = data;
176 unsigned int socket = SOCKET_ID_ANY;
177 size_t alignment = rte_mem_page_size();
178 if (alignment == (size_t)-1) {
179 DRV_LOG(ERR, "Failed to get mem page size");
184 if (priv->verbs_alloc_ctx.type == MLX5_VERBS_ALLOC_TYPE_TX_QUEUE) {
185 const struct mlx5_txq_ctrl *ctrl = priv->verbs_alloc_ctx.obj;
187 socket = ctrl->socket;
188 } else if (priv->verbs_alloc_ctx.type ==
189 MLX5_VERBS_ALLOC_TYPE_RX_QUEUE) {
190 const struct mlx5_rxq_ctrl *ctrl = priv->verbs_alloc_ctx.obj;
192 socket = ctrl->socket;
194 MLX5_ASSERT(data != NULL);
195 ret = mlx5_malloc(0, size, alignment, socket);
202 * Verbs callback to free a memory.
205 * A pointer to the memory to free.
207 * A pointer to the callback data.
210 mlx5_free_verbs_buf(void *ptr, void *data __rte_unused)
212 MLX5_ASSERT(data != NULL);
217 * Initialize DR related data within private structure.
218 * Routine checks the reference counter and does actual
219 * resources creation/initialization only if counter is zero.
222 * Pointer to the private device data structure.
225 * Zero on success, positive error code otherwise.
228 mlx5_alloc_shared_dr(struct mlx5_priv *priv)
230 struct mlx5_dev_ctx_shared *sh = priv->sh;
231 char s[MLX5_HLIST_NAMESIZE];
235 err = mlx5_alloc_table_hash_list(priv);
237 DRV_LOG(DEBUG, "sh->flow_tbls[%p] already created, reuse\n",
238 (void *)sh->flow_tbls);
241 /* Create tags hash list table. */
242 snprintf(s, sizeof(s), "%s_tags", sh->ibdev_name);
243 sh->tag_table = mlx5_hlist_create(s, MLX5_TAGS_HLIST_ARRAY_SIZE);
244 if (!sh->tag_table) {
245 DRV_LOG(ERR, "tags with hash creation failed.");
249 #ifdef HAVE_MLX5DV_DR
253 /* Shared DV/DR structures is already initialized. */
258 /* Reference counter is zero, we should initialize structures. */
259 domain = mlx5_glue->dr_create_domain(sh->ctx,
260 MLX5DV_DR_DOMAIN_TYPE_NIC_RX);
262 DRV_LOG(ERR, "ingress mlx5dv_dr_create_domain failed");
266 sh->rx_domain = domain;
267 domain = mlx5_glue->dr_create_domain(sh->ctx,
268 MLX5DV_DR_DOMAIN_TYPE_NIC_TX);
270 DRV_LOG(ERR, "egress mlx5dv_dr_create_domain failed");
274 pthread_mutex_init(&sh->dv_mutex, NULL);
275 sh->tx_domain = domain;
276 #ifdef HAVE_MLX5DV_DR_ESWITCH
277 if (priv->config.dv_esw_en) {
278 domain = mlx5_glue->dr_create_domain
279 (sh->ctx, MLX5DV_DR_DOMAIN_TYPE_FDB);
281 DRV_LOG(ERR, "FDB mlx5dv_dr_create_domain failed");
285 sh->fdb_domain = domain;
286 sh->esw_drop_action = mlx5_glue->dr_create_flow_action_drop();
289 if (priv->config.reclaim_mode == MLX5_RCM_AGGR) {
290 mlx5_glue->dr_reclaim_domain_memory(sh->rx_domain, 1);
291 mlx5_glue->dr_reclaim_domain_memory(sh->tx_domain, 1);
293 mlx5_glue->dr_reclaim_domain_memory(sh->fdb_domain, 1);
295 sh->pop_vlan_action = mlx5_glue->dr_create_flow_action_pop_vlan();
296 #endif /* HAVE_MLX5DV_DR */
301 /* Rollback the created objects. */
303 mlx5_glue->dr_destroy_domain(sh->rx_domain);
304 sh->rx_domain = NULL;
307 mlx5_glue->dr_destroy_domain(sh->tx_domain);
308 sh->tx_domain = NULL;
310 if (sh->fdb_domain) {
311 mlx5_glue->dr_destroy_domain(sh->fdb_domain);
312 sh->fdb_domain = NULL;
314 if (sh->esw_drop_action) {
315 mlx5_glue->destroy_flow_action(sh->esw_drop_action);
316 sh->esw_drop_action = NULL;
318 if (sh->pop_vlan_action) {
319 mlx5_glue->destroy_flow_action(sh->pop_vlan_action);
320 sh->pop_vlan_action = NULL;
323 /* tags should be destroyed with flow before. */
324 mlx5_hlist_destroy(sh->tag_table, NULL, NULL);
325 sh->tag_table = NULL;
327 mlx5_free_table_hash_list(priv);
332 * Destroy DR related data within private structure.
335 * Pointer to the private device data structure.
338 mlx5_os_free_shared_dr(struct mlx5_priv *priv)
340 struct mlx5_dev_ctx_shared *sh;
342 if (!priv->dr_shared)
347 #ifdef HAVE_MLX5DV_DR
348 MLX5_ASSERT(sh->dv_refcnt);
349 if (sh->dv_refcnt && --sh->dv_refcnt)
352 mlx5_glue->dr_destroy_domain(sh->rx_domain);
353 sh->rx_domain = NULL;
356 mlx5_glue->dr_destroy_domain(sh->tx_domain);
357 sh->tx_domain = NULL;
359 #ifdef HAVE_MLX5DV_DR_ESWITCH
360 if (sh->fdb_domain) {
361 mlx5_glue->dr_destroy_domain(sh->fdb_domain);
362 sh->fdb_domain = NULL;
364 if (sh->esw_drop_action) {
365 mlx5_glue->destroy_flow_action(sh->esw_drop_action);
366 sh->esw_drop_action = NULL;
369 if (sh->pop_vlan_action) {
370 mlx5_glue->destroy_flow_action(sh->pop_vlan_action);
371 sh->pop_vlan_action = NULL;
373 pthread_mutex_destroy(&sh->dv_mutex);
374 #endif /* HAVE_MLX5DV_DR */
376 /* tags should be destroyed with flow before. */
377 mlx5_hlist_destroy(sh->tag_table, NULL, NULL);
378 sh->tag_table = NULL;
380 mlx5_free_table_hash_list(priv);
384 * Spawn an Ethernet device from Verbs information.
387 * Backing DPDK device.
389 * Verbs device parameters (name, port, switch_info) to spawn.
391 * Device configuration parameters.
394 * A valid Ethernet device object on success, NULL otherwise and rte_errno
395 * is set. The following errors are defined:
397 * EBUSY: device is not supposed to be spawned.
398 * EEXIST: device is already spawned
400 static struct rte_eth_dev *
401 mlx5_dev_spawn(struct rte_device *dpdk_dev,
402 struct mlx5_dev_spawn_data *spawn,
403 struct mlx5_dev_config config)
405 const struct mlx5_switch_info *switch_info = &spawn->info;
406 struct mlx5_dev_ctx_shared *sh = NULL;
407 struct ibv_port_attr port_attr;
408 struct mlx5dv_context dv_attr = { .comp_mask = 0 };
409 struct rte_eth_dev *eth_dev = NULL;
410 struct mlx5_priv *priv = NULL;
412 unsigned int hw_padding = 0;
414 unsigned int cqe_comp;
415 unsigned int cqe_pad = 0;
416 unsigned int tunnel_en = 0;
417 unsigned int mpls_en = 0;
418 unsigned int swp = 0;
419 unsigned int mprq = 0;
420 unsigned int mprq_min_stride_size_n = 0;
421 unsigned int mprq_max_stride_size_n = 0;
422 unsigned int mprq_min_stride_num_n = 0;
423 unsigned int mprq_max_stride_num_n = 0;
424 struct rte_ether_addr mac;
425 char name[RTE_ETH_NAME_MAX_LEN];
426 int own_domain_id = 0;
429 #ifdef HAVE_MLX5DV_DR_DEVX_PORT
430 struct mlx5dv_devx_port devx_port = { .comp_mask = 0 };
433 /* Determine if this port representor is supposed to be spawned. */
434 if (switch_info->representor && dpdk_dev->devargs) {
435 struct rte_eth_devargs eth_da;
437 err = rte_eth_devargs_parse(dpdk_dev->devargs->args, ð_da);
440 DRV_LOG(ERR, "failed to process device arguments: %s",
441 strerror(rte_errno));
444 for (i = 0; i < eth_da.nb_representor_ports; ++i)
445 if (eth_da.representor_ports[i] ==
446 (uint16_t)switch_info->port_name)
448 if (i == eth_da.nb_representor_ports) {
453 /* Build device name. */
454 if (spawn->pf_bond < 0) {
456 if (!switch_info->representor)
457 strlcpy(name, dpdk_dev->name, sizeof(name));
459 snprintf(name, sizeof(name), "%s_representor_%u",
460 dpdk_dev->name, switch_info->port_name);
462 /* Bonding device. */
463 if (!switch_info->representor)
464 snprintf(name, sizeof(name), "%s_%s",
466 mlx5_os_get_dev_device_name(spawn->phys_dev));
468 snprintf(name, sizeof(name), "%s_%s_representor_%u",
470 mlx5_os_get_dev_device_name(spawn->phys_dev),
471 switch_info->port_name);
473 /* check if the device is already spawned */
474 if (rte_eth_dev_get_port_by_name(name, &port_id) == 0) {
478 DRV_LOG(DEBUG, "naming Ethernet device \"%s\"", name);
479 if (rte_eal_process_type() == RTE_PROC_SECONDARY) {
480 struct mlx5_mp_id mp_id;
482 eth_dev = rte_eth_dev_attach_secondary(name);
483 if (eth_dev == NULL) {
484 DRV_LOG(ERR, "can not attach rte ethdev");
488 eth_dev->device = dpdk_dev;
489 eth_dev->dev_ops = &mlx5_os_dev_sec_ops;
490 err = mlx5_proc_priv_init(eth_dev);
493 mp_id.port_id = eth_dev->data->port_id;
494 strlcpy(mp_id.name, MLX5_MP_NAME, RTE_MP_MAX_NAME_LEN);
495 /* Receive command fd from primary process */
496 err = mlx5_mp_req_verbs_cmd_fd(&mp_id);
499 /* Remap UAR for Tx queues. */
500 err = mlx5_tx_uar_init_secondary(eth_dev, err);
504 * Ethdev pointer is still required as input since
505 * the primary device is not accessible from the
508 eth_dev->rx_pkt_burst = mlx5_select_rx_function(eth_dev);
509 eth_dev->tx_pkt_burst = mlx5_select_tx_function(eth_dev);
512 mlx5_dev_close(eth_dev);
516 * Some parameters ("tx_db_nc" in particularly) are needed in
517 * advance to create dv/verbs device context. We proceed the
518 * devargs here to get ones, and later proceed devargs again
519 * to override some hardware settings.
521 err = mlx5_args(&config, dpdk_dev->devargs);
524 DRV_LOG(ERR, "failed to process device arguments: %s",
525 strerror(rte_errno));
528 mlx5_malloc_mem_select(config.sys_mem_en);
529 sh = mlx5_alloc_shared_dev_ctx(spawn, &config);
532 config.devx = sh->devx;
533 #ifdef HAVE_MLX5DV_DR_ACTION_DEST_DEVX_TIR
536 #ifdef HAVE_IBV_MLX5_MOD_SWP
537 dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_SWP;
540 * Multi-packet send is supported by ConnectX-4 Lx PF as well
541 * as all ConnectX-5 devices.
543 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
544 dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS;
546 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
547 dv_attr.comp_mask |= MLX5DV_CONTEXT_MASK_STRIDING_RQ;
549 mlx5_glue->dv_query_device(sh->ctx, &dv_attr);
550 if (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_MPW_ALLOWED) {
551 if (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_ENHANCED_MPW) {
552 DRV_LOG(DEBUG, "enhanced MPW is supported");
553 mps = MLX5_MPW_ENHANCED;
555 DRV_LOG(DEBUG, "MPW is supported");
559 DRV_LOG(DEBUG, "MPW isn't supported");
560 mps = MLX5_MPW_DISABLED;
562 #ifdef HAVE_IBV_MLX5_MOD_SWP
563 if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_SWP)
564 swp = dv_attr.sw_parsing_caps.sw_parsing_offloads;
565 DRV_LOG(DEBUG, "SWP support: %u", swp);
568 #ifdef HAVE_IBV_DEVICE_STRIDING_RQ_SUPPORT
569 if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_STRIDING_RQ) {
570 struct mlx5dv_striding_rq_caps mprq_caps =
571 dv_attr.striding_rq_caps;
573 DRV_LOG(DEBUG, "\tmin_single_stride_log_num_of_bytes: %d",
574 mprq_caps.min_single_stride_log_num_of_bytes);
575 DRV_LOG(DEBUG, "\tmax_single_stride_log_num_of_bytes: %d",
576 mprq_caps.max_single_stride_log_num_of_bytes);
577 DRV_LOG(DEBUG, "\tmin_single_wqe_log_num_of_strides: %d",
578 mprq_caps.min_single_wqe_log_num_of_strides);
579 DRV_LOG(DEBUG, "\tmax_single_wqe_log_num_of_strides: %d",
580 mprq_caps.max_single_wqe_log_num_of_strides);
581 DRV_LOG(DEBUG, "\tsupported_qpts: %d",
582 mprq_caps.supported_qpts);
583 DRV_LOG(DEBUG, "device supports Multi-Packet RQ");
585 mprq_min_stride_size_n =
586 mprq_caps.min_single_stride_log_num_of_bytes;
587 mprq_max_stride_size_n =
588 mprq_caps.max_single_stride_log_num_of_bytes;
589 mprq_min_stride_num_n =
590 mprq_caps.min_single_wqe_log_num_of_strides;
591 mprq_max_stride_num_n =
592 mprq_caps.max_single_wqe_log_num_of_strides;
595 if (RTE_CACHE_LINE_SIZE == 128 &&
596 !(dv_attr.flags & MLX5DV_CONTEXT_FLAGS_CQE_128B_COMP))
600 config.cqe_comp = cqe_comp;
601 #ifdef HAVE_IBV_MLX5_MOD_CQE_128B_PAD
602 /* Whether device supports 128B Rx CQE padding. */
603 cqe_pad = RTE_CACHE_LINE_SIZE == 128 &&
604 (dv_attr.flags & MLX5DV_CONTEXT_FLAGS_CQE_128B_PAD);
606 #ifdef HAVE_IBV_DEVICE_TUNNEL_SUPPORT
607 if (dv_attr.comp_mask & MLX5DV_CONTEXT_MASK_TUNNEL_OFFLOADS) {
608 tunnel_en = ((dv_attr.tunnel_offloads_caps &
609 MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_VXLAN) &&
610 (dv_attr.tunnel_offloads_caps &
611 MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_GRE) &&
612 (dv_attr.tunnel_offloads_caps &
613 MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_GENEVE));
615 DRV_LOG(DEBUG, "tunnel offloading is %ssupported",
616 tunnel_en ? "" : "not ");
619 "tunnel offloading disabled due to old OFED/rdma-core version");
621 config.tunnel_en = tunnel_en;
622 #ifdef HAVE_IBV_DEVICE_MPLS_SUPPORT
623 mpls_en = ((dv_attr.tunnel_offloads_caps &
624 MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_CW_MPLS_OVER_GRE) &&
625 (dv_attr.tunnel_offloads_caps &
626 MLX5DV_RAW_PACKET_CAP_TUNNELED_OFFLOAD_CW_MPLS_OVER_UDP));
627 DRV_LOG(DEBUG, "MPLS over GRE/UDP tunnel offloading is %ssupported",
628 mpls_en ? "" : "not ");
630 DRV_LOG(WARNING, "MPLS over GRE/UDP tunnel offloading disabled due to"
631 " old OFED/rdma-core version or firmware configuration");
633 config.mpls_en = mpls_en;
634 /* Check port status. */
635 err = mlx5_glue->query_port(sh->ctx, spawn->phys_port, &port_attr);
637 DRV_LOG(ERR, "port query failed: %s", strerror(err));
640 if (port_attr.link_layer != IBV_LINK_LAYER_ETHERNET) {
641 DRV_LOG(ERR, "port is not configured in Ethernet mode");
645 if (port_attr.state != IBV_PORT_ACTIVE)
646 DRV_LOG(DEBUG, "port is not active: \"%s\" (%d)",
647 mlx5_glue->port_state_str(port_attr.state),
649 /* Allocate private eth device data. */
650 priv = mlx5_malloc(MLX5_MEM_ZERO | MLX5_MEM_RTE,
652 RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
654 DRV_LOG(ERR, "priv allocation failure");
659 priv->dev_port = spawn->phys_port;
660 priv->pci_dev = spawn->pci_dev;
661 priv->mtu = RTE_ETHER_MTU;
662 priv->mp_id.port_id = port_id;
663 strlcpy(priv->mp_id.name, MLX5_MP_NAME, RTE_MP_MAX_NAME_LEN);
664 /* Some internal functions rely on Netlink sockets, open them now. */
665 priv->nl_socket_rdma = mlx5_nl_init(NETLINK_RDMA);
666 priv->nl_socket_route = mlx5_nl_init(NETLINK_ROUTE);
667 priv->representor = !!switch_info->representor;
668 priv->master = !!switch_info->master;
669 priv->domain_id = RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID;
670 priv->vport_meta_tag = 0;
671 priv->vport_meta_mask = 0;
672 priv->pf_bond = spawn->pf_bond;
673 #ifdef HAVE_MLX5DV_DR_DEVX_PORT
675 * The DevX port query API is implemented. E-Switch may use
676 * either vport or reg_c[0] metadata register to match on
677 * vport index. The engaged part of metadata register is
680 if (switch_info->representor || switch_info->master) {
681 devx_port.comp_mask = MLX5DV_DEVX_PORT_VPORT |
682 MLX5DV_DEVX_PORT_MATCH_REG_C_0;
683 err = mlx5_glue->devx_port_query(sh->ctx, spawn->phys_port,
687 "can't query devx port %d on device %s",
689 mlx5_os_get_dev_device_name(spawn->phys_dev));
690 devx_port.comp_mask = 0;
693 if (devx_port.comp_mask & MLX5DV_DEVX_PORT_MATCH_REG_C_0) {
694 priv->vport_meta_tag = devx_port.reg_c_0.value;
695 priv->vport_meta_mask = devx_port.reg_c_0.mask;
696 if (!priv->vport_meta_mask) {
697 DRV_LOG(ERR, "vport zero mask for port %d"
698 " on bonding device %s",
700 mlx5_os_get_dev_device_name
705 if (priv->vport_meta_tag & ~priv->vport_meta_mask) {
706 DRV_LOG(ERR, "invalid vport tag for port %d"
707 " on bonding device %s",
709 mlx5_os_get_dev_device_name
715 if (devx_port.comp_mask & MLX5DV_DEVX_PORT_VPORT) {
716 priv->vport_id = devx_port.vport_num;
717 } else if (spawn->pf_bond >= 0) {
718 DRV_LOG(ERR, "can't deduce vport index for port %d"
719 " on bonding device %s",
721 mlx5_os_get_dev_device_name(spawn->phys_dev));
725 /* Suppose vport index in compatible way. */
726 priv->vport_id = switch_info->representor ?
727 switch_info->port_name + 1 : -1;
731 * Kernel/rdma_core support single E-Switch per PF configurations
732 * only and vport_id field contains the vport index for
733 * associated VF, which is deduced from representor port name.
734 * For example, let's have the IB device port 10, it has
735 * attached network device eth0, which has port name attribute
736 * pf0vf2, we can deduce the VF number as 2, and set vport index
737 * as 3 (2+1). This assigning schema should be changed if the
738 * multiple E-Switch instances per PF configurations or/and PCI
739 * subfunctions are added.
741 priv->vport_id = switch_info->representor ?
742 switch_info->port_name + 1 : -1;
744 /* representor_id field keeps the unmodified VF index. */
745 priv->representor_id = switch_info->representor ?
746 switch_info->port_name : -1;
748 * Look for sibling devices in order to reuse their switch domain
749 * if any, otherwise allocate one.
751 MLX5_ETH_FOREACH_DEV(port_id, priv->pci_dev) {
752 const struct mlx5_priv *opriv =
753 rte_eth_devices[port_id].data->dev_private;
756 opriv->sh != priv->sh ||
758 RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID)
760 priv->domain_id = opriv->domain_id;
763 if (priv->domain_id == RTE_ETH_DEV_SWITCH_DOMAIN_ID_INVALID) {
764 err = rte_eth_switch_domain_alloc(&priv->domain_id);
767 DRV_LOG(ERR, "unable to allocate switch domain: %s",
768 strerror(rte_errno));
773 /* Override some values set by hardware configuration. */
774 mlx5_args(&config, dpdk_dev->devargs);
775 err = mlx5_dev_check_sibling_config(priv, &config);
778 config.hw_csum = !!(sh->device_attr.device_cap_flags_ex &
779 IBV_DEVICE_RAW_IP_CSUM);
780 DRV_LOG(DEBUG, "checksum offloading is %ssupported",
781 (config.hw_csum ? "" : "not "));
782 #if !defined(HAVE_IBV_DEVICE_COUNTERS_SET_V42) && \
783 !defined(HAVE_IBV_DEVICE_COUNTERS_SET_V45)
784 DRV_LOG(DEBUG, "counters are not supported");
786 #if !defined(HAVE_IBV_FLOW_DV_SUPPORT) || !defined(HAVE_MLX5DV_DR)
787 if (config.dv_flow_en) {
788 DRV_LOG(WARNING, "DV flow is not supported");
789 config.dv_flow_en = 0;
792 config.ind_table_max_size =
793 sh->device_attr.max_rwq_indirection_table_size;
795 * Remove this check once DPDK supports larger/variable
796 * indirection tables.
798 if (config.ind_table_max_size > (unsigned int)ETH_RSS_RETA_SIZE_512)
799 config.ind_table_max_size = ETH_RSS_RETA_SIZE_512;
800 DRV_LOG(DEBUG, "maximum Rx indirection table size is %u",
801 config.ind_table_max_size);
802 config.hw_vlan_strip = !!(sh->device_attr.raw_packet_caps &
803 IBV_RAW_PACKET_CAP_CVLAN_STRIPPING);
804 DRV_LOG(DEBUG, "VLAN stripping is %ssupported",
805 (config.hw_vlan_strip ? "" : "not "));
806 config.hw_fcs_strip = !!(sh->device_attr.raw_packet_caps &
807 IBV_RAW_PACKET_CAP_SCATTER_FCS);
808 #if defined(HAVE_IBV_WQ_FLAG_RX_END_PADDING)
809 hw_padding = !!sh->device_attr.rx_pad_end_addr_align;
810 #elif defined(HAVE_IBV_WQ_FLAGS_PCI_WRITE_END_PADDING)
811 hw_padding = !!(sh->device_attr.device_cap_flags_ex &
812 IBV_DEVICE_PCI_WRITE_END_PADDING);
814 if (config.hw_padding && !hw_padding) {
815 DRV_LOG(DEBUG, "Rx end alignment padding isn't supported");
816 config.hw_padding = 0;
817 } else if (config.hw_padding) {
818 DRV_LOG(DEBUG, "Rx end alignment padding is enabled");
820 config.tso = (sh->device_attr.max_tso > 0 &&
821 (sh->device_attr.tso_supported_qpts &
822 (1 << IBV_QPT_RAW_PACKET)));
824 config.tso_max_payload_sz = sh->device_attr.max_tso;
826 * MPW is disabled by default, while the Enhanced MPW is enabled
829 if (config.mps == MLX5_ARG_UNSET)
830 config.mps = (mps == MLX5_MPW_ENHANCED) ? MLX5_MPW_ENHANCED :
833 config.mps = config.mps ? mps : MLX5_MPW_DISABLED;
834 DRV_LOG(INFO, "%sMPS is %s",
835 config.mps == MLX5_MPW_ENHANCED ? "enhanced " :
836 config.mps == MLX5_MPW ? "legacy " : "",
837 config.mps != MLX5_MPW_DISABLED ? "enabled" : "disabled");
838 if (config.cqe_comp && !cqe_comp) {
839 DRV_LOG(WARNING, "Rx CQE compression isn't supported");
842 if (config.cqe_pad && !cqe_pad) {
843 DRV_LOG(WARNING, "Rx CQE padding isn't supported");
845 } else if (config.cqe_pad) {
846 DRV_LOG(INFO, "Rx CQE padding is enabled");
849 priv->counter_fallback = 0;
850 err = mlx5_devx_cmd_query_hca_attr(sh->ctx, &config.hca_attr);
855 if (!config.hca_attr.flow_counters_dump)
856 priv->counter_fallback = 1;
857 #ifndef HAVE_IBV_DEVX_ASYNC
858 priv->counter_fallback = 1;
860 if (priv->counter_fallback)
861 DRV_LOG(INFO, "Use fall-back DV counter management");
862 /* Check for LRO support. */
863 if (config.dest_tir && config.hca_attr.lro_cap &&
865 /* TBD check tunnel lro caps. */
866 config.lro.supported = config.hca_attr.lro_cap;
867 DRV_LOG(DEBUG, "Device supports LRO");
869 * If LRO timeout is not configured by application,
870 * use the minimal supported value.
872 if (!config.lro.timeout)
874 config.hca_attr.lro_timer_supported_periods[0];
875 DRV_LOG(DEBUG, "LRO session timeout set to %d usec",
878 #if defined(HAVE_MLX5DV_DR) && defined(HAVE_MLX5_DR_CREATE_ACTION_FLOW_METER)
879 if (config.hca_attr.qos.sup && config.hca_attr.qos.srtcm_sup &&
882 config.hca_attr.qos.flow_meter_reg_c_ids;
884 * Meter needs two REG_C's for color match and pre-sfx
885 * flow match. Here get the REG_C for color match.
886 * REG_C_0 and REG_C_1 is reserved for metadata feature.
889 if (__builtin_popcount(reg_c_mask) < 1) {
891 DRV_LOG(WARNING, "No available register for"
894 priv->mtr_color_reg = ffs(reg_c_mask) - 1 +
897 priv->mtr_reg_share =
898 config.hca_attr.qos.flow_meter_reg_share;
899 DRV_LOG(DEBUG, "The REG_C meter uses is %d",
900 priv->mtr_color_reg);
906 DRV_LOG(DEBUG, "Timestamp counter frequency %u kHz",
907 config.hca_attr.dev_freq_khz);
908 DRV_LOG(DEBUG, "Packet pacing is %ssupported",
909 config.hca_attr.qos.packet_pacing ? "" : "not ");
910 DRV_LOG(DEBUG, "Cross channel ops are %ssupported",
911 config.hca_attr.cross_channel ? "" : "not ");
912 DRV_LOG(DEBUG, "WQE index ignore is %ssupported",
913 config.hca_attr.wqe_index_ignore ? "" : "not ");
914 DRV_LOG(DEBUG, "Non-wire SQ feature is %ssupported",
915 config.hca_attr.non_wire_sq ? "" : "not ");
916 DRV_LOG(DEBUG, "Static WQE SQ feature is %ssupported (%d)",
917 config.hca_attr.log_max_static_sq_wq ? "" : "not ",
918 config.hca_attr.log_max_static_sq_wq);
919 DRV_LOG(DEBUG, "WQE rate PP mode is %ssupported",
920 config.hca_attr.qos.wqe_rate_pp ? "" : "not ");
922 DRV_LOG(ERR, "DevX is required for packet pacing");
926 if (!config.hca_attr.qos.packet_pacing) {
927 DRV_LOG(ERR, "Packet pacing is not supported");
931 if (!config.hca_attr.cross_channel) {
932 DRV_LOG(ERR, "Cross channel operations are"
933 " required for packet pacing");
937 if (!config.hca_attr.wqe_index_ignore) {
938 DRV_LOG(ERR, "WQE index ignore feature is"
939 " required for packet pacing");
943 if (!config.hca_attr.non_wire_sq) {
944 DRV_LOG(ERR, "Non-wire SQ feature is"
945 " required for packet pacing");
949 if (!config.hca_attr.log_max_static_sq_wq) {
950 DRV_LOG(ERR, "Static WQE SQ feature is"
951 " required for packet pacing");
955 if (!config.hca_attr.qos.wqe_rate_pp) {
956 DRV_LOG(ERR, "WQE rate mode is required"
957 " for packet pacing");
961 #ifndef HAVE_MLX5DV_DEVX_UAR_OFFSET
962 DRV_LOG(ERR, "DevX does not provide UAR offset,"
963 " can't create queues for packet pacing");
969 uint32_t reg[MLX5_ST_SZ_DW(register_mtutc)];
971 err = mlx5_devx_cmd_register_read
972 (sh->ctx, MLX5_REGISTER_ID_MTUTC, 0,
973 reg, MLX5_ST_SZ_DW(register_mtutc));
977 /* MTUTC register is read successfully. */
978 ts_mode = MLX5_GET(register_mtutc, reg,
980 if (ts_mode == MLX5_MTUTC_TIMESTAMP_MODE_REAL_TIME)
981 config.rt_timestamp = 1;
983 /* Kernel does not support register reading. */
984 if (config.hca_attr.dev_freq_khz ==
985 (NS_PER_S / MS_PER_S))
986 config.rt_timestamp = 1;
990 * If HW has bug working with tunnel packet decapsulation and
991 * scatter FCS, and decapsulation is needed, clear the hw_fcs_strip
992 * bit. Then DEV_RX_OFFLOAD_KEEP_CRC bit will not be set anymore.
994 if (config.hca_attr.scatter_fcs_w_decap_disable && config.decap_en)
995 config.hw_fcs_strip = 0;
996 DRV_LOG(DEBUG, "FCS stripping configuration is %ssupported",
997 (config.hw_fcs_strip ? "" : "not "));
998 if (config.mprq.enabled && mprq) {
999 if (config.mprq.stride_num_n &&
1000 (config.mprq.stride_num_n > mprq_max_stride_num_n ||
1001 config.mprq.stride_num_n < mprq_min_stride_num_n)) {
1002 config.mprq.stride_num_n =
1003 RTE_MIN(RTE_MAX(MLX5_MPRQ_STRIDE_NUM_N,
1004 mprq_min_stride_num_n),
1005 mprq_max_stride_num_n);
1007 "the number of strides"
1008 " for Multi-Packet RQ is out of range,"
1009 " setting default value (%u)",
1010 1 << config.mprq.stride_num_n);
1012 if (config.mprq.stride_size_n &&
1013 (config.mprq.stride_size_n > mprq_max_stride_size_n ||
1014 config.mprq.stride_size_n < mprq_min_stride_size_n)) {
1015 config.mprq.stride_size_n =
1016 RTE_MIN(RTE_MAX(MLX5_MPRQ_STRIDE_SIZE_N,
1017 mprq_min_stride_size_n),
1018 mprq_max_stride_size_n);
1020 "the size of a stride"
1021 " for Multi-Packet RQ is out of range,"
1022 " setting default value (%u)",
1023 1 << config.mprq.stride_size_n);
1025 config.mprq.min_stride_size_n = mprq_min_stride_size_n;
1026 config.mprq.max_stride_size_n = mprq_max_stride_size_n;
1027 } else if (config.mprq.enabled && !mprq) {
1028 DRV_LOG(WARNING, "Multi-Packet RQ isn't supported");
1029 config.mprq.enabled = 0;
1031 if (config.max_dump_files_num == 0)
1032 config.max_dump_files_num = 128;
1033 eth_dev = rte_eth_dev_allocate(name);
1034 if (eth_dev == NULL) {
1035 DRV_LOG(ERR, "can not allocate rte ethdev");
1039 /* Flag to call rte_eth_dev_release_port() in rte_eth_dev_close(). */
1040 eth_dev->data->dev_flags |= RTE_ETH_DEV_CLOSE_REMOVE;
1041 if (priv->representor) {
1042 eth_dev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR;
1043 eth_dev->data->representor_id = priv->representor_id;
1046 * Store associated network device interface index. This index
1047 * is permanent throughout the lifetime of device. So, we may store
1048 * the ifindex here and use the cached value further.
1050 MLX5_ASSERT(spawn->ifindex);
1051 priv->if_index = spawn->ifindex;
1052 eth_dev->data->dev_private = priv;
1053 priv->dev_data = eth_dev->data;
1054 eth_dev->data->mac_addrs = priv->mac;
1055 eth_dev->device = dpdk_dev;
1056 /* Configure the first MAC address by default. */
1057 if (mlx5_get_mac(eth_dev, &mac.addr_bytes)) {
1059 "port %u cannot get MAC address, is mlx5_en"
1060 " loaded? (errno: %s)",
1061 eth_dev->data->port_id, strerror(rte_errno));
1066 "port %u MAC address is %02x:%02x:%02x:%02x:%02x:%02x",
1067 eth_dev->data->port_id,
1068 mac.addr_bytes[0], mac.addr_bytes[1],
1069 mac.addr_bytes[2], mac.addr_bytes[3],
1070 mac.addr_bytes[4], mac.addr_bytes[5]);
1071 #ifdef RTE_LIBRTE_MLX5_DEBUG
1073 char ifname[IF_NAMESIZE];
1075 if (mlx5_get_ifname(eth_dev, &ifname) == 0)
1076 DRV_LOG(DEBUG, "port %u ifname is \"%s\"",
1077 eth_dev->data->port_id, ifname);
1079 DRV_LOG(DEBUG, "port %u ifname is unknown",
1080 eth_dev->data->port_id);
1083 /* Get actual MTU if possible. */
1084 err = mlx5_get_mtu(eth_dev, &priv->mtu);
1089 DRV_LOG(DEBUG, "port %u MTU is %u", eth_dev->data->port_id,
1091 /* Initialize burst functions to prevent crashes before link-up. */
1092 eth_dev->rx_pkt_burst = removed_rx_burst;
1093 eth_dev->tx_pkt_burst = removed_tx_burst;
1094 eth_dev->dev_ops = &mlx5_os_dev_ops;
1095 /* Register MAC address. */
1096 claim_zero(mlx5_mac_addr_add(eth_dev, &mac, 0, 0));
1097 if (config.vf && config.vf_nl_en)
1098 mlx5_nl_mac_addr_sync(priv->nl_socket_route,
1099 mlx5_ifindex(eth_dev),
1100 eth_dev->data->mac_addrs,
1101 MLX5_MAX_MAC_ADDRESSES);
1103 priv->ctrl_flows = 0;
1104 TAILQ_INIT(&priv->flow_meters);
1105 TAILQ_INIT(&priv->flow_meter_profiles);
1106 /* Hint libmlx5 to use PMD allocator for data plane resources */
1107 mlx5_glue->dv_set_context_attr(sh->ctx,
1108 MLX5DV_CTX_ATTR_BUF_ALLOCATORS,
1109 (void *)((uintptr_t)&(struct mlx5dv_ctx_allocators){
1110 .alloc = &mlx5_alloc_verbs_buf,
1111 .free = &mlx5_free_verbs_buf,
1114 /* Bring Ethernet device up. */
1115 DRV_LOG(DEBUG, "port %u forcing Ethernet interface up",
1116 eth_dev->data->port_id);
1117 mlx5_set_link_up(eth_dev);
1119 * Even though the interrupt handler is not installed yet,
1120 * interrupts will still trigger on the async_fd from
1121 * Verbs context returned by ibv_open_device().
1123 mlx5_link_update(eth_dev, 0);
1124 #ifdef HAVE_MLX5DV_DR_ESWITCH
1125 if (!(config.hca_attr.eswitch_manager && config.dv_flow_en &&
1126 (switch_info->representor || switch_info->master)))
1127 config.dv_esw_en = 0;
1129 config.dv_esw_en = 0;
1131 /* Detect minimal data bytes to inline. */
1132 mlx5_set_min_inline(spawn, &config);
1133 /* Store device configuration on private structure. */
1134 priv->config = config;
1135 /* Create context for virtual machine VLAN workaround. */
1136 priv->vmwa_context = mlx5_vlan_vmwa_init(eth_dev, spawn->ifindex);
1137 if (config.dv_flow_en) {
1138 err = mlx5_alloc_shared_dr(priv);
1142 * RSS id is shared with meter flow id. Meter flow id can only
1143 * use the 24 MSB of the register.
1145 priv->qrss_id_pool = mlx5_flow_id_pool_alloc(UINT32_MAX >>
1146 MLX5_MTR_COLOR_BITS);
1147 if (!priv->qrss_id_pool) {
1148 DRV_LOG(ERR, "can't create flow id pool");
1153 /* Supported Verbs flow priority number detection. */
1154 err = mlx5_flow_discover_priorities(eth_dev);
1159 priv->config.flow_prio = err;
1160 if (!priv->config.dv_esw_en &&
1161 priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
1162 DRV_LOG(WARNING, "metadata mode %u is not supported "
1163 "(no E-Switch)", priv->config.dv_xmeta_en);
1164 priv->config.dv_xmeta_en = MLX5_XMETA_MODE_LEGACY;
1166 mlx5_set_metadata_mask(eth_dev);
1167 if (priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
1168 !priv->sh->dv_regc0_mask) {
1169 DRV_LOG(ERR, "metadata mode %u is not supported "
1170 "(no metadata reg_c[0] is available)",
1171 priv->config.dv_xmeta_en);
1176 * Allocate the buffer for flow creating, just once.
1177 * The allocation must be done before any flow creating.
1179 mlx5_flow_alloc_intermediate(eth_dev);
1180 /* Query availability of metadata reg_c's. */
1181 err = mlx5_flow_discover_mreg_c(eth_dev);
1186 if (!mlx5_flow_ext_mreg_supported(eth_dev)) {
1188 "port %u extensive metadata register is not supported",
1189 eth_dev->data->port_id);
1190 if (priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY) {
1191 DRV_LOG(ERR, "metadata mode %u is not supported "
1192 "(no metadata registers available)",
1193 priv->config.dv_xmeta_en);
1198 if (priv->config.dv_flow_en &&
1199 priv->config.dv_xmeta_en != MLX5_XMETA_MODE_LEGACY &&
1200 mlx5_flow_ext_mreg_supported(eth_dev) &&
1201 priv->sh->dv_regc0_mask) {
1202 priv->mreg_cp_tbl = mlx5_hlist_create(MLX5_FLOW_MREG_HNAME,
1203 MLX5_FLOW_MREG_HTABLE_SZ);
1204 if (!priv->mreg_cp_tbl) {
1212 if (priv->mreg_cp_tbl)
1213 mlx5_hlist_destroy(priv->mreg_cp_tbl, NULL, NULL);
1215 mlx5_os_free_shared_dr(priv);
1216 if (priv->nl_socket_route >= 0)
1217 close(priv->nl_socket_route);
1218 if (priv->nl_socket_rdma >= 0)
1219 close(priv->nl_socket_rdma);
1220 if (priv->vmwa_context)
1221 mlx5_vlan_vmwa_exit(priv->vmwa_context);
1222 if (priv->qrss_id_pool)
1223 mlx5_flow_id_pool_release(priv->qrss_id_pool);
1225 claim_zero(rte_eth_switch_domain_free(priv->domain_id));
1227 if (eth_dev != NULL)
1228 eth_dev->data->dev_private = NULL;
1230 if (eth_dev != NULL) {
1231 /* mac_addrs must not be freed alone because part of
1234 eth_dev->data->mac_addrs = NULL;
1235 rte_eth_dev_release_port(eth_dev);
1238 mlx5_free_shared_dev_ctx(sh);
1239 MLX5_ASSERT(err > 0);
1245 * Comparison callback to sort device data.
1247 * This is meant to be used with qsort().
1250 * Pointer to pointer to first data object.
1252 * Pointer to pointer to second data object.
1255 * 0 if both objects are equal, less than 0 if the first argument is less
1256 * than the second, greater than 0 otherwise.
1259 mlx5_dev_spawn_data_cmp(const void *a, const void *b)
1261 const struct mlx5_switch_info *si_a =
1262 &((const struct mlx5_dev_spawn_data *)a)->info;
1263 const struct mlx5_switch_info *si_b =
1264 &((const struct mlx5_dev_spawn_data *)b)->info;
1267 /* Master device first. */
1268 ret = si_b->master - si_a->master;
1271 /* Then representor devices. */
1272 ret = si_b->representor - si_a->representor;
1275 /* Unidentified devices come last in no specific order. */
1276 if (!si_a->representor)
1278 /* Order representors by name. */
1279 return si_a->port_name - si_b->port_name;
1283 * Match PCI information for possible slaves of bonding device.
1285 * @param[in] ibv_dev
1286 * Pointer to Infiniband device structure.
1287 * @param[in] pci_dev
1288 * Pointer to PCI device structure to match PCI address.
1289 * @param[in] nl_rdma
1290 * Netlink RDMA group socket handle.
1293 * negative value if no bonding device found, otherwise
1294 * positive index of slave PF in bonding.
1297 mlx5_device_bond_pci_match(const struct ibv_device *ibv_dev,
1298 const struct rte_pci_device *pci_dev,
1301 char ifname[IF_NAMESIZE + 1];
1302 unsigned int ifindex;
1308 * Try to get master device name. If something goes
1309 * wrong suppose the lack of kernel support and no
1314 if (!strstr(ibv_dev->name, "bond"))
1316 np = mlx5_nl_portnum(nl_rdma, ibv_dev->name);
1320 * The Master device might not be on the predefined
1321 * port (not on port index 1, it is not garanted),
1322 * we have to scan all Infiniband device port and
1325 for (i = 1; i <= np; ++i) {
1326 /* Check whether Infiniband port is populated. */
1327 ifindex = mlx5_nl_ifindex(nl_rdma, ibv_dev->name, i);
1330 if (!if_indextoname(ifindex, ifname))
1332 /* Try to read bonding slave names from sysfs. */
1334 "/sys/class/net/%s/master/bonding/slaves", ifname);
1335 file = fopen(slaves, "r");
1341 /* Use safe format to check maximal buffer length. */
1342 MLX5_ASSERT(atol(RTE_STR(IF_NAMESIZE)) == IF_NAMESIZE);
1343 while (fscanf(file, "%" RTE_STR(IF_NAMESIZE) "s", ifname) == 1) {
1344 char tmp_str[IF_NAMESIZE + 32];
1345 struct rte_pci_addr pci_addr;
1346 struct mlx5_switch_info info;
1348 /* Process slave interface names in the loop. */
1349 snprintf(tmp_str, sizeof(tmp_str),
1350 "/sys/class/net/%s", ifname);
1351 if (mlx5_dev_to_pci_addr(tmp_str, &pci_addr)) {
1352 DRV_LOG(WARNING, "can not get PCI address"
1353 " for netdev \"%s\"", ifname);
1356 if (pci_dev->addr.domain != pci_addr.domain ||
1357 pci_dev->addr.bus != pci_addr.bus ||
1358 pci_dev->addr.devid != pci_addr.devid ||
1359 pci_dev->addr.function != pci_addr.function)
1361 /* Slave interface PCI address match found. */
1363 snprintf(tmp_str, sizeof(tmp_str),
1364 "/sys/class/net/%s/phys_port_name", ifname);
1365 file = fopen(tmp_str, "rb");
1368 info.name_type = MLX5_PHYS_PORT_NAME_TYPE_NOTSET;
1369 if (fscanf(file, "%32s", tmp_str) == 1)
1370 mlx5_translate_port_name(tmp_str, &info);
1371 if (info.name_type == MLX5_PHYS_PORT_NAME_TYPE_LEGACY ||
1372 info.name_type == MLX5_PHYS_PORT_NAME_TYPE_UPLINK)
1373 pf = info.port_name;
1382 * DPDK callback to register a PCI device.
1384 * This function spawns Ethernet devices out of a given PCI device.
1386 * @param[in] pci_drv
1387 * PCI driver structure (mlx5_driver).
1388 * @param[in] pci_dev
1389 * PCI device information.
1392 * 0 on success, a negative errno value otherwise and rte_errno is set.
1395 mlx5_os_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1396 struct rte_pci_device *pci_dev)
1398 struct ibv_device **ibv_list;
1400 * Number of found IB Devices matching with requested PCI BDF.
1401 * nd != 1 means there are multiple IB devices over the same
1402 * PCI device and we have representors and master.
1404 unsigned int nd = 0;
1406 * Number of found IB device Ports. nd = 1 and np = 1..n means
1407 * we have the single multiport IB device, and there may be
1408 * representors attached to some of found ports.
1410 unsigned int np = 0;
1412 * Number of DPDK ethernet devices to Spawn - either over
1413 * multiple IB devices or multiple ports of single IB device.
1414 * Actually this is the number of iterations to spawn.
1416 unsigned int ns = 0;
1419 * < 0 - no bonding device (single one)
1420 * >= 0 - bonding device (value is slave PF index)
1423 struct mlx5_dev_spawn_data *list = NULL;
1424 struct mlx5_dev_config dev_config;
1427 if (mlx5_class_get(pci_dev->device.devargs) != MLX5_CLASS_NET) {
1428 DRV_LOG(DEBUG, "Skip probing - should be probed by other mlx5"
1432 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
1433 mlx5_pmd_socket_init();
1434 ret = mlx5_init_once();
1436 DRV_LOG(ERR, "unable to init PMD global data: %s",
1437 strerror(rte_errno));
1440 MLX5_ASSERT(pci_drv == &mlx5_driver);
1442 ibv_list = mlx5_glue->get_device_list(&ret);
1444 rte_errno = errno ? errno : ENOSYS;
1445 DRV_LOG(ERR, "cannot list devices, is ib_uverbs loaded?");
1449 * First scan the list of all Infiniband devices to find
1450 * matching ones, gathering into the list.
1452 struct ibv_device *ibv_match[ret + 1];
1453 int nl_route = mlx5_nl_init(NETLINK_ROUTE);
1454 int nl_rdma = mlx5_nl_init(NETLINK_RDMA);
1458 struct rte_pci_addr pci_addr;
1460 DRV_LOG(DEBUG, "checking device \"%s\"", ibv_list[ret]->name);
1461 bd = mlx5_device_bond_pci_match
1462 (ibv_list[ret], pci_dev, nl_rdma);
1465 * Bonding device detected. Only one match is allowed,
1466 * the bonding is supported over multi-port IB device,
1467 * there should be no matches on representor PCI
1468 * functions or non VF LAG bonding devices with
1469 * specified address.
1473 "multiple PCI match on bonding device"
1474 "\"%s\" found", ibv_list[ret]->name);
1479 DRV_LOG(INFO, "PCI information matches for"
1480 " slave %d bonding device \"%s\"",
1481 bd, ibv_list[ret]->name);
1482 ibv_match[nd++] = ibv_list[ret];
1485 if (mlx5_dev_to_pci_addr
1486 (ibv_list[ret]->ibdev_path, &pci_addr))
1488 if (pci_dev->addr.domain != pci_addr.domain ||
1489 pci_dev->addr.bus != pci_addr.bus ||
1490 pci_dev->addr.devid != pci_addr.devid ||
1491 pci_dev->addr.function != pci_addr.function)
1493 DRV_LOG(INFO, "PCI information matches for device \"%s\"",
1494 ibv_list[ret]->name);
1495 ibv_match[nd++] = ibv_list[ret];
1497 ibv_match[nd] = NULL;
1499 /* No device matches, just complain and bail out. */
1501 "no Verbs device matches PCI device " PCI_PRI_FMT ","
1502 " are kernel drivers loaded?",
1503 pci_dev->addr.domain, pci_dev->addr.bus,
1504 pci_dev->addr.devid, pci_dev->addr.function);
1511 * Found single matching device may have multiple ports.
1512 * Each port may be representor, we have to check the port
1513 * number and check the representors existence.
1516 np = mlx5_nl_portnum(nl_rdma, ibv_match[0]->name);
1518 DRV_LOG(WARNING, "can not get IB device \"%s\""
1519 " ports number", ibv_match[0]->name);
1520 if (bd >= 0 && !np) {
1521 DRV_LOG(ERR, "can not get ports"
1522 " for bonding device");
1528 #ifndef HAVE_MLX5DV_DR_DEVX_PORT
1531 * This may happen if there is VF LAG kernel support and
1532 * application is compiled with older rdma_core library.
1535 "No kernel/verbs support for VF LAG bonding found.");
1536 rte_errno = ENOTSUP;
1542 * Now we can determine the maximal
1543 * amount of devices to be spawned.
1545 list = mlx5_malloc(MLX5_MEM_ZERO,
1546 sizeof(struct mlx5_dev_spawn_data) *
1548 RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
1550 DRV_LOG(ERR, "spawn data array allocation failure");
1555 if (bd >= 0 || np > 1) {
1557 * Single IB device with multiple ports found,
1558 * it may be E-Switch master device and representors.
1559 * We have to perform identification through the ports.
1561 MLX5_ASSERT(nl_rdma >= 0);
1562 MLX5_ASSERT(ns == 0);
1563 MLX5_ASSERT(nd == 1);
1565 for (i = 1; i <= np; ++i) {
1566 list[ns].max_port = np;
1567 list[ns].phys_port = i;
1568 list[ns].phys_dev = ibv_match[0];
1569 list[ns].eth_dev = NULL;
1570 list[ns].pci_dev = pci_dev;
1571 list[ns].pf_bond = bd;
1572 list[ns].ifindex = mlx5_nl_ifindex
1574 mlx5_os_get_dev_device_name
1575 (list[ns].phys_dev), i);
1576 if (!list[ns].ifindex) {
1578 * No network interface index found for the
1579 * specified port, it means there is no
1580 * representor on this port. It's OK,
1581 * there can be disabled ports, for example
1582 * if sriov_numvfs < sriov_totalvfs.
1588 ret = mlx5_nl_switch_info
1592 if (ret || (!list[ns].info.representor &&
1593 !list[ns].info.master)) {
1595 * We failed to recognize representors with
1596 * Netlink, let's try to perform the task
1599 ret = mlx5_sysfs_switch_info
1603 if (!ret && bd >= 0) {
1604 switch (list[ns].info.name_type) {
1605 case MLX5_PHYS_PORT_NAME_TYPE_UPLINK:
1606 if (list[ns].info.port_name == bd)
1609 case MLX5_PHYS_PORT_NAME_TYPE_PFHPF:
1611 case MLX5_PHYS_PORT_NAME_TYPE_PFVF:
1612 if (list[ns].info.pf_num == bd)
1620 if (!ret && (list[ns].info.representor ^
1621 list[ns].info.master))
1626 "unable to recognize master/representors"
1627 " on the IB device with multiple ports");
1634 * The existence of several matching entries (nd > 1) means
1635 * port representors have been instantiated. No existing Verbs
1636 * call nor sysfs entries can tell them apart, this can only
1637 * be done through Netlink calls assuming kernel drivers are
1638 * recent enough to support them.
1640 * In the event of identification failure through Netlink,
1641 * try again through sysfs, then:
1643 * 1. A single IB device matches (nd == 1) with single
1644 * port (np=0/1) and is not a representor, assume
1645 * no switch support.
1647 * 2. Otherwise no safe assumptions can be made;
1648 * complain louder and bail out.
1650 for (i = 0; i != nd; ++i) {
1651 memset(&list[ns].info, 0, sizeof(list[ns].info));
1652 list[ns].max_port = 1;
1653 list[ns].phys_port = 1;
1654 list[ns].phys_dev = ibv_match[i];
1655 list[ns].eth_dev = NULL;
1656 list[ns].pci_dev = pci_dev;
1657 list[ns].pf_bond = -1;
1658 list[ns].ifindex = 0;
1660 list[ns].ifindex = mlx5_nl_ifindex
1662 mlx5_os_get_dev_device_name
1663 (list[ns].phys_dev), 1);
1664 if (!list[ns].ifindex) {
1665 char ifname[IF_NAMESIZE];
1668 * Netlink failed, it may happen with old
1669 * ib_core kernel driver (before 4.16).
1670 * We can assume there is old driver because
1671 * here we are processing single ports IB
1672 * devices. Let's try sysfs to retrieve
1673 * the ifindex. The method works for
1674 * master device only.
1678 * Multiple devices found, assume
1679 * representors, can not distinguish
1680 * master/representor and retrieve
1681 * ifindex via sysfs.
1685 ret = mlx5_get_ifname_sysfs
1686 (ibv_match[i]->ibdev_path, ifname);
1689 if_nametoindex(ifname);
1690 if (!list[ns].ifindex) {
1692 * No network interface index found
1693 * for the specified device, it means
1694 * there it is neither representor
1702 ret = mlx5_nl_switch_info
1706 if (ret || (!list[ns].info.representor &&
1707 !list[ns].info.master)) {
1709 * We failed to recognize representors with
1710 * Netlink, let's try to perform the task
1713 ret = mlx5_sysfs_switch_info
1717 if (!ret && (list[ns].info.representor ^
1718 list[ns].info.master)) {
1720 } else if ((nd == 1) &&
1721 !list[ns].info.representor &&
1722 !list[ns].info.master) {
1724 * Single IB device with
1725 * one physical port and
1726 * attached network device.
1727 * May be SRIOV is not enabled
1728 * or there is no representors.
1730 DRV_LOG(INFO, "no E-Switch support detected");
1737 "unable to recognize master/representors"
1738 " on the multiple IB devices");
1746 * Sort list to probe devices in natural order for users convenience
1747 * (i.e. master first, then representors from lowest to highest ID).
1749 qsort(list, ns, sizeof(*list), mlx5_dev_spawn_data_cmp);
1750 /* Default configuration. */
1751 dev_config = (struct mlx5_dev_config){
1753 .mps = MLX5_ARG_UNSET,
1754 .dbnc = MLX5_ARG_UNSET,
1756 .txq_inline_max = MLX5_ARG_UNSET,
1757 .txq_inline_min = MLX5_ARG_UNSET,
1758 .txq_inline_mpw = MLX5_ARG_UNSET,
1759 .txqs_inline = MLX5_ARG_UNSET,
1761 .mr_ext_memseg_en = 1,
1763 .enabled = 0, /* Disabled by default. */
1766 .max_memcpy_len = MLX5_MPRQ_MEMCPY_DEFAULT_LEN,
1767 .min_rxqs_num = MLX5_MPRQ_MIN_RXQS,
1772 .log_hp_size = MLX5_ARG_UNSET,
1774 /* Device specific configuration. */
1775 switch (pci_dev->id.device_id) {
1776 case PCI_DEVICE_ID_MELLANOX_CONNECTX4VF:
1777 case PCI_DEVICE_ID_MELLANOX_CONNECTX4LXVF:
1778 case PCI_DEVICE_ID_MELLANOX_CONNECTX5VF:
1779 case PCI_DEVICE_ID_MELLANOX_CONNECTX5EXVF:
1780 case PCI_DEVICE_ID_MELLANOX_CONNECTX5BFVF:
1781 case PCI_DEVICE_ID_MELLANOX_CONNECTX6VF:
1782 case PCI_DEVICE_ID_MELLANOX_CONNECTX6DXVF:
1788 for (i = 0; i != ns; ++i) {
1791 list[i].eth_dev = mlx5_dev_spawn(&pci_dev->device,
1794 if (!list[i].eth_dev) {
1795 if (rte_errno != EBUSY && rte_errno != EEXIST)
1797 /* Device is disabled or already spawned. Ignore it. */
1800 restore = list[i].eth_dev->data->dev_flags;
1801 rte_eth_copy_pci_info(list[i].eth_dev, pci_dev);
1802 /* Restore non-PCI flags cleared by the above call. */
1803 list[i].eth_dev->data->dev_flags |= restore;
1804 rte_eth_dev_probing_finish(list[i].eth_dev);
1808 "probe of PCI device " PCI_PRI_FMT " aborted after"
1809 " encountering an error: %s",
1810 pci_dev->addr.domain, pci_dev->addr.bus,
1811 pci_dev->addr.devid, pci_dev->addr.function,
1812 strerror(rte_errno));
1816 if (!list[i].eth_dev)
1818 mlx5_dev_close(list[i].eth_dev);
1819 /* mac_addrs must not be freed because in dev_private */
1820 list[i].eth_dev->data->mac_addrs = NULL;
1821 claim_zero(rte_eth_dev_release_port(list[i].eth_dev));
1823 /* Restore original error. */
1830 * Do the routine cleanup:
1831 * - close opened Netlink sockets
1832 * - free allocated spawn data array
1833 * - free the Infiniband device list
1841 MLX5_ASSERT(ibv_list);
1842 mlx5_glue->free_device_list(ibv_list);
1847 mlx5_config_doorbell_mapping_env(const struct mlx5_dev_config *config)
1852 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
1853 /* Get environment variable to store. */
1854 env = getenv(MLX5_SHUT_UP_BF);
1855 value = env ? !!strcmp(env, "0") : MLX5_ARG_UNSET;
1856 if (config->dbnc == MLX5_ARG_UNSET)
1857 setenv(MLX5_SHUT_UP_BF, MLX5_SHUT_UP_BF_DEFAULT, 1);
1859 setenv(MLX5_SHUT_UP_BF,
1860 config->dbnc == MLX5_TXDB_NCACHED ? "1" : "0", 1);
1865 mlx5_restore_doorbell_mapping_env(int value)
1867 MLX5_ASSERT(rte_eal_process_type() == RTE_PROC_PRIMARY);
1868 /* Restore the original environment variable state. */
1869 if (value == MLX5_ARG_UNSET)
1870 unsetenv(MLX5_SHUT_UP_BF);
1872 setenv(MLX5_SHUT_UP_BF, value ? "1" : "0", 1);
1876 * Extract pdn of PD object using DV API.
1879 * Pointer to the verbs PD object.
1881 * Pointer to the PD object number variable.
1884 * 0 on success, error value otherwise.
1887 mlx5_os_get_pdn(void *pd, uint32_t *pdn)
1889 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
1890 struct mlx5dv_obj obj;
1891 struct mlx5dv_pd pd_info;
1895 obj.pd.out = &pd_info;
1896 ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_PD);
1898 DRV_LOG(DEBUG, "Fail to get PD object info");
1907 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */
1911 * Function API to open IB device.
1913 * This function calls the Linux glue APIs to open a device.
1916 * Pointer to the IB device attributes (name, port, etc).
1917 * @param[out] config
1918 * Pointer to device configuration structure.
1920 * Pointer to shared context structure.
1923 * 0 on success, a positive error value otherwise.
1926 mlx5_os_open_device(const struct mlx5_dev_spawn_data *spawn,
1927 const struct mlx5_dev_config *config,
1928 struct mlx5_dev_ctx_shared *sh)
1933 sh->numa_node = spawn->pci_dev->device.numa_node;
1934 pthread_mutex_init(&sh->txpp.mutex, NULL);
1936 * Configure environment variable "MLX5_BF_SHUT_UP"
1937 * before the device creation. The rdma_core library
1938 * checks the variable at device creation and
1939 * stores the result internally.
1941 dbmap_env = mlx5_config_doorbell_mapping_env(config);
1942 /* Try to open IB device with DV first, then usual Verbs. */
1944 sh->ctx = mlx5_glue->dv_open_device(spawn->phys_dev);
1947 DRV_LOG(DEBUG, "DevX is supported");
1948 /* The device is created, no need for environment. */
1949 mlx5_restore_doorbell_mapping_env(dbmap_env);
1951 /* The environment variable is still configured. */
1952 sh->ctx = mlx5_glue->open_device(spawn->phys_dev);
1953 err = errno ? errno : ENODEV;
1955 * The environment variable is not needed anymore,
1956 * all device creation attempts are completed.
1958 mlx5_restore_doorbell_mapping_env(dbmap_env);
1961 DRV_LOG(DEBUG, "DevX is NOT supported");
1968 * Install shared asynchronous device events handler.
1969 * This function is implemented to support event sharing
1970 * between multiple ports of single IB device.
1973 * Pointer to mlx5_dev_ctx_shared object.
1976 mlx5_os_dev_shared_handler_install(struct mlx5_dev_ctx_shared *sh)
1981 sh->intr_handle.fd = -1;
1982 flags = fcntl(((struct ibv_context *)sh->ctx)->async_fd, F_GETFL);
1983 ret = fcntl(((struct ibv_context *)sh->ctx)->async_fd,
1984 F_SETFL, flags | O_NONBLOCK);
1986 DRV_LOG(INFO, "failed to change file descriptor async event"
1989 sh->intr_handle.fd = ((struct ibv_context *)sh->ctx)->async_fd;
1990 sh->intr_handle.type = RTE_INTR_HANDLE_EXT;
1991 if (rte_intr_callback_register(&sh->intr_handle,
1992 mlx5_dev_interrupt_handler, sh)) {
1993 DRV_LOG(INFO, "Fail to install the shared interrupt.");
1994 sh->intr_handle.fd = -1;
1998 #ifdef HAVE_IBV_DEVX_ASYNC
1999 sh->intr_handle_devx.fd = -1;
2001 (void *)mlx5_glue->devx_create_cmd_comp(sh->ctx);
2002 struct mlx5dv_devx_cmd_comp *devx_comp = sh->devx_comp;
2004 DRV_LOG(INFO, "failed to allocate devx_comp.");
2007 flags = fcntl(devx_comp->fd, F_GETFL);
2008 ret = fcntl(devx_comp->fd, F_SETFL, flags | O_NONBLOCK);
2010 DRV_LOG(INFO, "failed to change file descriptor"
2014 sh->intr_handle_devx.fd = devx_comp->fd;
2015 sh->intr_handle_devx.type = RTE_INTR_HANDLE_EXT;
2016 if (rte_intr_callback_register(&sh->intr_handle_devx,
2017 mlx5_dev_interrupt_handler_devx, sh)) {
2018 DRV_LOG(INFO, "Fail to install the devx shared"
2020 sh->intr_handle_devx.fd = -1;
2022 #endif /* HAVE_IBV_DEVX_ASYNC */
2027 * Uninstall shared asynchronous device events handler.
2028 * This function is implemented to support event sharing
2029 * between multiple ports of single IB device.
2032 * Pointer to mlx5_dev_ctx_shared object.
2035 mlx5_os_dev_shared_handler_uninstall(struct mlx5_dev_ctx_shared *sh)
2037 if (sh->intr_handle.fd >= 0)
2038 mlx5_intr_callback_unregister(&sh->intr_handle,
2039 mlx5_dev_interrupt_handler, sh);
2040 #ifdef HAVE_IBV_DEVX_ASYNC
2041 if (sh->intr_handle_devx.fd >= 0)
2042 rte_intr_callback_unregister(&sh->intr_handle_devx,
2043 mlx5_dev_interrupt_handler_devx, sh);
2045 mlx5_glue->devx_destroy_cmd_comp(sh->devx_comp);
2050 * Read statistics by a named counter.
2053 * Pointer to the private device data structure.
2054 * @param[in] ctr_name
2055 * Pointer to the name of the statistic counter to read
2057 * Pointer to read statistic value.
2059 * 0 on success and stat is valud, 1 if failed to read the value
2064 mlx5_os_read_dev_stat(struct mlx5_priv *priv, const char *ctr_name,
2070 MKSTR(path, "%s/ports/%d/hw_counters/%s",
2071 priv->sh->ibdev_path,
2074 fd = open(path, O_RDONLY);
2076 char buf[21] = {'\0'};
2077 ssize_t n = read(fd, buf, sizeof(buf));
2081 *stat = strtoull(buf, NULL, 10);
2091 * Read device counters table.
2094 * Pointer to Ethernet device.
2096 * Counters table output buffer.
2099 * 0 on success and stats is filled, negative errno value otherwise and
2103 mlx5_os_read_dev_counters(struct rte_eth_dev *dev, uint64_t *stats)
2105 struct mlx5_priv *priv = dev->data->dev_private;
2106 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
2109 unsigned int stats_sz = xstats_ctrl->stats_n * sizeof(uint64_t);
2110 unsigned char et_stat_buf[sizeof(struct ethtool_stats) + stats_sz];
2111 struct ethtool_stats *et_stats = (struct ethtool_stats *)et_stat_buf;
2114 et_stats->cmd = ETHTOOL_GSTATS;
2115 et_stats->n_stats = xstats_ctrl->stats_n;
2116 ifr.ifr_data = (caddr_t)et_stats;
2117 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
2120 "port %u unable to read statistic values from device",
2121 dev->data->port_id);
2124 for (i = 0; i != xstats_ctrl->mlx5_stats_n; ++i) {
2125 if (xstats_ctrl->info[i].dev) {
2126 ret = mlx5_os_read_dev_stat(priv,
2127 xstats_ctrl->info[i].ctr_name,
2129 /* return last xstats counter if fail to read. */
2131 xstats_ctrl->xstats[i] = stats[i];
2133 stats[i] = xstats_ctrl->xstats[i];
2135 stats[i] = (uint64_t)
2136 et_stats->data[xstats_ctrl->dev_table_idx[i]];
2143 * Query the number of statistics provided by ETHTOOL.
2146 * Pointer to Ethernet device.
2149 * Number of statistics on success, negative errno value otherwise and
2153 mlx5_os_get_stats_n(struct rte_eth_dev *dev)
2155 struct ethtool_drvinfo drvinfo;
2159 drvinfo.cmd = ETHTOOL_GDRVINFO;
2160 ifr.ifr_data = (caddr_t)&drvinfo;
2161 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
2163 DRV_LOG(WARNING, "port %u unable to query number of statistics",
2164 dev->data->port_id);
2167 return drvinfo.n_stats;
2170 static const struct mlx5_counter_ctrl mlx5_counters_init[] = {
2172 .dpdk_name = "rx_port_unicast_bytes",
2173 .ctr_name = "rx_vport_unicast_bytes",
2176 .dpdk_name = "rx_port_multicast_bytes",
2177 .ctr_name = "rx_vport_multicast_bytes",
2180 .dpdk_name = "rx_port_broadcast_bytes",
2181 .ctr_name = "rx_vport_broadcast_bytes",
2184 .dpdk_name = "rx_port_unicast_packets",
2185 .ctr_name = "rx_vport_unicast_packets",
2188 .dpdk_name = "rx_port_multicast_packets",
2189 .ctr_name = "rx_vport_multicast_packets",
2192 .dpdk_name = "rx_port_broadcast_packets",
2193 .ctr_name = "rx_vport_broadcast_packets",
2196 .dpdk_name = "tx_port_unicast_bytes",
2197 .ctr_name = "tx_vport_unicast_bytes",
2200 .dpdk_name = "tx_port_multicast_bytes",
2201 .ctr_name = "tx_vport_multicast_bytes",
2204 .dpdk_name = "tx_port_broadcast_bytes",
2205 .ctr_name = "tx_vport_broadcast_bytes",
2208 .dpdk_name = "tx_port_unicast_packets",
2209 .ctr_name = "tx_vport_unicast_packets",
2212 .dpdk_name = "tx_port_multicast_packets",
2213 .ctr_name = "tx_vport_multicast_packets",
2216 .dpdk_name = "tx_port_broadcast_packets",
2217 .ctr_name = "tx_vport_broadcast_packets",
2220 .dpdk_name = "rx_wqe_err",
2221 .ctr_name = "rx_wqe_err",
2224 .dpdk_name = "rx_crc_errors_phy",
2225 .ctr_name = "rx_crc_errors_phy",
2228 .dpdk_name = "rx_in_range_len_errors_phy",
2229 .ctr_name = "rx_in_range_len_errors_phy",
2232 .dpdk_name = "rx_symbol_err_phy",
2233 .ctr_name = "rx_symbol_err_phy",
2236 .dpdk_name = "tx_errors_phy",
2237 .ctr_name = "tx_errors_phy",
2240 .dpdk_name = "rx_out_of_buffer",
2241 .ctr_name = "out_of_buffer",
2245 .dpdk_name = "tx_packets_phy",
2246 .ctr_name = "tx_packets_phy",
2249 .dpdk_name = "rx_packets_phy",
2250 .ctr_name = "rx_packets_phy",
2253 .dpdk_name = "tx_discards_phy",
2254 .ctr_name = "tx_discards_phy",
2257 .dpdk_name = "rx_discards_phy",
2258 .ctr_name = "rx_discards_phy",
2261 .dpdk_name = "tx_bytes_phy",
2262 .ctr_name = "tx_bytes_phy",
2265 .dpdk_name = "rx_bytes_phy",
2266 .ctr_name = "rx_bytes_phy",
2268 /* Representor only */
2270 .dpdk_name = "rx_packets",
2271 .ctr_name = "vport_rx_packets",
2274 .dpdk_name = "rx_bytes",
2275 .ctr_name = "vport_rx_bytes",
2278 .dpdk_name = "tx_packets",
2279 .ctr_name = "vport_tx_packets",
2282 .dpdk_name = "tx_bytes",
2283 .ctr_name = "vport_tx_bytes",
2287 static const unsigned int xstats_n = RTE_DIM(mlx5_counters_init);
2290 * Init the structures to read device counters.
2293 * Pointer to Ethernet device.
2296 mlx5_os_stats_init(struct rte_eth_dev *dev)
2298 struct mlx5_priv *priv = dev->data->dev_private;
2299 struct mlx5_xstats_ctrl *xstats_ctrl = &priv->xstats_ctrl;
2300 struct mlx5_stats_ctrl *stats_ctrl = &priv->stats_ctrl;
2304 struct ethtool_gstrings *strings = NULL;
2305 unsigned int dev_stats_n;
2306 unsigned int str_sz;
2309 /* So that it won't aggregate for each init. */
2310 xstats_ctrl->mlx5_stats_n = 0;
2311 ret = mlx5_os_get_stats_n(dev);
2313 DRV_LOG(WARNING, "port %u no extended statistics available",
2314 dev->data->port_id);
2318 /* Allocate memory to grab stat names and values. */
2319 str_sz = dev_stats_n * ETH_GSTRING_LEN;
2320 strings = (struct ethtool_gstrings *)
2321 mlx5_malloc(0, str_sz + sizeof(struct ethtool_gstrings), 0,
2324 DRV_LOG(WARNING, "port %u unable to allocate memory for xstats",
2325 dev->data->port_id);
2328 strings->cmd = ETHTOOL_GSTRINGS;
2329 strings->string_set = ETH_SS_STATS;
2330 strings->len = dev_stats_n;
2331 ifr.ifr_data = (caddr_t)strings;
2332 ret = mlx5_ifreq(dev, SIOCETHTOOL, &ifr);
2334 DRV_LOG(WARNING, "port %u unable to get statistic names",
2335 dev->data->port_id);
2338 for (i = 0; i != dev_stats_n; ++i) {
2339 const char *curr_string = (const char *)
2340 &strings->data[i * ETH_GSTRING_LEN];
2342 for (j = 0; j != xstats_n; ++j) {
2343 if (!strcmp(mlx5_counters_init[j].ctr_name,
2345 unsigned int idx = xstats_ctrl->mlx5_stats_n++;
2347 xstats_ctrl->dev_table_idx[idx] = i;
2348 xstats_ctrl->info[idx] = mlx5_counters_init[j];
2353 /* Add dev counters. */
2354 for (i = 0; i != xstats_n; ++i) {
2355 if (mlx5_counters_init[i].dev) {
2356 unsigned int idx = xstats_ctrl->mlx5_stats_n++;
2358 xstats_ctrl->info[idx] = mlx5_counters_init[i];
2359 xstats_ctrl->hw_stats[idx] = 0;
2362 MLX5_ASSERT(xstats_ctrl->mlx5_stats_n <= MLX5_MAX_XSTATS);
2363 xstats_ctrl->stats_n = dev_stats_n;
2364 /* Copy to base at first time. */
2365 ret = mlx5_os_read_dev_counters(dev, xstats_ctrl->base);
2367 DRV_LOG(ERR, "port %u cannot read device counters: %s",
2368 dev->data->port_id, strerror(rte_errno));
2369 mlx5_os_read_dev_stat(priv, "out_of_buffer", &stats_ctrl->imissed_base);
2370 stats_ctrl->imissed = 0;
2376 * Set the reg_mr and dereg_mr call backs
2378 * @param reg_mr_cb[out]
2379 * Pointer to reg_mr func
2380 * @param dereg_mr_cb[out]
2381 * Pointer to dereg_mr func
2385 mlx5_os_set_reg_mr_cb(mlx5_reg_mr_t *reg_mr_cb,
2386 mlx5_dereg_mr_t *dereg_mr_cb)
2388 *reg_mr_cb = mlx5_verbs_ops.reg_mr;
2389 *dereg_mr_cb = mlx5_verbs_ops.dereg_mr;
2393 * Remove a MAC address from device
2396 * Pointer to Ethernet device structure.
2398 * MAC address index.
2401 mlx5_os_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
2403 struct mlx5_priv *priv = dev->data->dev_private;
2404 const int vf = priv->config.vf;
2407 mlx5_nl_mac_addr_remove(priv->nl_socket_route,
2408 mlx5_ifindex(dev), priv->mac_own,
2409 &dev->data->mac_addrs[index], index);
2413 * Adds a MAC address to the device
2416 * Pointer to Ethernet device structure.
2418 * MAC address to register.
2420 * MAC address index.
2423 * 0 on success, a negative errno value otherwise
2426 mlx5_os_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac,
2429 struct mlx5_priv *priv = dev->data->dev_private;
2430 const int vf = priv->config.vf;
2434 ret = mlx5_nl_mac_addr_add(priv->nl_socket_route,
2435 mlx5_ifindex(dev), priv->mac_own,
2441 * Modify a VF MAC address
2444 * Pointer to device private data.
2446 * MAC address to modify into.
2448 * Net device interface index
2453 * 0 on success, a negative errno value otherwise
2456 mlx5_os_vf_mac_addr_modify(struct mlx5_priv *priv,
2457 unsigned int iface_idx,
2458 struct rte_ether_addr *mac_addr,
2461 return mlx5_nl_vf_mac_addr_modify
2462 (priv->nl_socket_route, iface_idx, mac_addr, vf_index);
2466 * Set device promiscuous mode
2469 * Pointer to Ethernet device structure.
2471 * 0 - promiscuous is disabled, otherwise - enabled
2474 * 0 on success, a negative error value otherwise
2477 mlx5_os_set_promisc(struct rte_eth_dev *dev, int enable)
2479 struct mlx5_priv *priv = dev->data->dev_private;
2481 return mlx5_nl_promisc(priv->nl_socket_route,
2482 mlx5_ifindex(dev), !!enable);
2486 * Set device promiscuous mode
2489 * Pointer to Ethernet device structure.
2491 * 0 - all multicase is disabled, otherwise - enabled
2494 * 0 on success, a negative error value otherwise
2497 mlx5_os_set_allmulti(struct rte_eth_dev *dev, int enable)
2499 struct mlx5_priv *priv = dev->data->dev_private;
2501 return mlx5_nl_allmulti(priv->nl_socket_route,
2502 mlx5_ifindex(dev), !!enable);
2505 const struct eth_dev_ops mlx5_os_dev_ops = {
2506 .dev_configure = mlx5_dev_configure,
2507 .dev_start = mlx5_dev_start,
2508 .dev_stop = mlx5_dev_stop,
2509 .dev_set_link_down = mlx5_set_link_down,
2510 .dev_set_link_up = mlx5_set_link_up,
2511 .dev_close = mlx5_dev_close,
2512 .promiscuous_enable = mlx5_promiscuous_enable,
2513 .promiscuous_disable = mlx5_promiscuous_disable,
2514 .allmulticast_enable = mlx5_allmulticast_enable,
2515 .allmulticast_disable = mlx5_allmulticast_disable,
2516 .link_update = mlx5_link_update,
2517 .stats_get = mlx5_stats_get,
2518 .stats_reset = mlx5_stats_reset,
2519 .xstats_get = mlx5_xstats_get,
2520 .xstats_reset = mlx5_xstats_reset,
2521 .xstats_get_names = mlx5_xstats_get_names,
2522 .fw_version_get = mlx5_fw_version_get,
2523 .dev_infos_get = mlx5_dev_infos_get,
2524 .read_clock = mlx5_txpp_read_clock,
2525 .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
2526 .vlan_filter_set = mlx5_vlan_filter_set,
2527 .rx_queue_setup = mlx5_rx_queue_setup,
2528 .rx_hairpin_queue_setup = mlx5_rx_hairpin_queue_setup,
2529 .tx_queue_setup = mlx5_tx_queue_setup,
2530 .tx_hairpin_queue_setup = mlx5_tx_hairpin_queue_setup,
2531 .rx_queue_release = mlx5_rx_queue_release,
2532 .tx_queue_release = mlx5_tx_queue_release,
2533 .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
2534 .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
2535 .mac_addr_remove = mlx5_mac_addr_remove,
2536 .mac_addr_add = mlx5_mac_addr_add,
2537 .mac_addr_set = mlx5_mac_addr_set,
2538 .set_mc_addr_list = mlx5_set_mc_addr_list,
2539 .mtu_set = mlx5_dev_set_mtu,
2540 .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
2541 .vlan_offload_set = mlx5_vlan_offload_set,
2542 .reta_update = mlx5_dev_rss_reta_update,
2543 .reta_query = mlx5_dev_rss_reta_query,
2544 .rss_hash_update = mlx5_rss_hash_update,
2545 .rss_hash_conf_get = mlx5_rss_hash_conf_get,
2546 .filter_ctrl = mlx5_dev_filter_ctrl,
2547 .rx_descriptor_status = mlx5_rx_descriptor_status,
2548 .tx_descriptor_status = mlx5_tx_descriptor_status,
2549 .rxq_info_get = mlx5_rxq_info_get,
2550 .txq_info_get = mlx5_txq_info_get,
2551 .rx_burst_mode_get = mlx5_rx_burst_mode_get,
2552 .tx_burst_mode_get = mlx5_tx_burst_mode_get,
2553 .rx_queue_count = mlx5_rx_queue_count,
2554 .rx_queue_intr_enable = mlx5_rx_intr_enable,
2555 .rx_queue_intr_disable = mlx5_rx_intr_disable,
2556 .is_removed = mlx5_is_removed,
2557 .udp_tunnel_port_add = mlx5_udp_tunnel_port_add,
2558 .get_module_info = mlx5_get_module_info,
2559 .get_module_eeprom = mlx5_get_module_eeprom,
2560 .hairpin_cap_get = mlx5_hairpin_cap_get,
2561 .mtr_ops_get = mlx5_flow_meter_ops_get,
2564 /* Available operations from secondary process. */
2565 const struct eth_dev_ops mlx5_os_dev_sec_ops = {
2566 .stats_get = mlx5_stats_get,
2567 .stats_reset = mlx5_stats_reset,
2568 .xstats_get = mlx5_xstats_get,
2569 .xstats_reset = mlx5_xstats_reset,
2570 .xstats_get_names = mlx5_xstats_get_names,
2571 .fw_version_get = mlx5_fw_version_get,
2572 .dev_infos_get = mlx5_dev_infos_get,
2573 .read_clock = mlx5_txpp_read_clock,
2574 .rx_descriptor_status = mlx5_rx_descriptor_status,
2575 .tx_descriptor_status = mlx5_tx_descriptor_status,
2576 .rxq_info_get = mlx5_rxq_info_get,
2577 .txq_info_get = mlx5_txq_info_get,
2578 .rx_burst_mode_get = mlx5_rx_burst_mode_get,
2579 .tx_burst_mode_get = mlx5_tx_burst_mode_get,
2580 .get_module_info = mlx5_get_module_info,
2581 .get_module_eeprom = mlx5_get_module_eeprom,
2584 /* Available operations in flow isolated mode. */
2585 const struct eth_dev_ops mlx5_os_dev_ops_isolate = {
2586 .dev_configure = mlx5_dev_configure,
2587 .dev_start = mlx5_dev_start,
2588 .dev_stop = mlx5_dev_stop,
2589 .dev_set_link_down = mlx5_set_link_down,
2590 .dev_set_link_up = mlx5_set_link_up,
2591 .dev_close = mlx5_dev_close,
2592 .promiscuous_enable = mlx5_promiscuous_enable,
2593 .promiscuous_disable = mlx5_promiscuous_disable,
2594 .allmulticast_enable = mlx5_allmulticast_enable,
2595 .allmulticast_disable = mlx5_allmulticast_disable,
2596 .link_update = mlx5_link_update,
2597 .stats_get = mlx5_stats_get,
2598 .stats_reset = mlx5_stats_reset,
2599 .xstats_get = mlx5_xstats_get,
2600 .xstats_reset = mlx5_xstats_reset,
2601 .xstats_get_names = mlx5_xstats_get_names,
2602 .fw_version_get = mlx5_fw_version_get,
2603 .dev_infos_get = mlx5_dev_infos_get,
2604 .read_clock = mlx5_txpp_read_clock,
2605 .dev_supported_ptypes_get = mlx5_dev_supported_ptypes_get,
2606 .vlan_filter_set = mlx5_vlan_filter_set,
2607 .rx_queue_setup = mlx5_rx_queue_setup,
2608 .rx_hairpin_queue_setup = mlx5_rx_hairpin_queue_setup,
2609 .tx_queue_setup = mlx5_tx_queue_setup,
2610 .tx_hairpin_queue_setup = mlx5_tx_hairpin_queue_setup,
2611 .rx_queue_release = mlx5_rx_queue_release,
2612 .tx_queue_release = mlx5_tx_queue_release,
2613 .flow_ctrl_get = mlx5_dev_get_flow_ctrl,
2614 .flow_ctrl_set = mlx5_dev_set_flow_ctrl,
2615 .mac_addr_remove = mlx5_mac_addr_remove,
2616 .mac_addr_add = mlx5_mac_addr_add,
2617 .mac_addr_set = mlx5_mac_addr_set,
2618 .set_mc_addr_list = mlx5_set_mc_addr_list,
2619 .mtu_set = mlx5_dev_set_mtu,
2620 .vlan_strip_queue_set = mlx5_vlan_strip_queue_set,
2621 .vlan_offload_set = mlx5_vlan_offload_set,
2622 .filter_ctrl = mlx5_dev_filter_ctrl,
2623 .rx_descriptor_status = mlx5_rx_descriptor_status,
2624 .tx_descriptor_status = mlx5_tx_descriptor_status,
2625 .rxq_info_get = mlx5_rxq_info_get,
2626 .txq_info_get = mlx5_txq_info_get,
2627 .rx_burst_mode_get = mlx5_rx_burst_mode_get,
2628 .tx_burst_mode_get = mlx5_tx_burst_mode_get,
2629 .rx_queue_intr_enable = mlx5_rx_intr_enable,
2630 .rx_queue_intr_disable = mlx5_rx_intr_disable,
2631 .is_removed = mlx5_is_removed,
2632 .get_module_info = mlx5_get_module_info,
2633 .get_module_eeprom = mlx5_get_module_eeprom,
2634 .hairpin_cap_get = mlx5_hairpin_cap_get,
2635 .mtr_ops_get = mlx5_flow_meter_ops_get,