1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 Mellanox Technologies, Ltd
6 #include <rte_malloc.h>
9 #include <rte_bus_pci.h>
11 #include <rte_string_fns.h>
13 #include <mlx5_glue.h>
14 #include <mlx5_common.h>
15 #include <mlx5_devx_cmds.h>
19 #include "mlx5_vdpa_utils.h"
20 #include "mlx5_vdpa.h"
23 #define MLX5_VDPA_DEFAULT_FEATURES ((1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
24 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
25 (1ULL << VIRTIO_NET_F_MQ) | \
26 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
27 (1ULL << VIRTIO_F_ORDER_PLATFORM) | \
28 (1ULL << VHOST_F_LOG_ALL))
30 #define MLX5_VDPA_PROTOCOL_FEATURES \
31 ((1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ) | \
32 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD) | \
33 (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER) | \
34 (1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD) | \
35 (1ULL << VHOST_USER_PROTOCOL_F_MQ))
37 TAILQ_HEAD(mlx5_vdpa_privs, mlx5_vdpa_priv) priv_list =
38 TAILQ_HEAD_INITIALIZER(priv_list);
39 static pthread_mutex_t priv_list_lock = PTHREAD_MUTEX_INITIALIZER;
40 int mlx5_vdpa_logtype;
42 static struct mlx5_vdpa_priv *
43 mlx5_vdpa_find_priv_resource_by_did(int did)
45 struct mlx5_vdpa_priv *priv;
48 pthread_mutex_lock(&priv_list_lock);
49 TAILQ_FOREACH(priv, &priv_list, next) {
50 if (did == priv->id) {
55 pthread_mutex_unlock(&priv_list_lock);
57 DRV_LOG(ERR, "Invalid device id: %d.", did);
65 mlx5_vdpa_get_queue_num(int did, uint32_t *queue_num)
67 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
70 DRV_LOG(ERR, "Invalid device id: %d.", did);
73 *queue_num = priv->caps.max_num_virtio_queues;
78 mlx5_vdpa_get_vdpa_features(int did, uint64_t *features)
80 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
83 DRV_LOG(ERR, "Invalid device id: %d.", did);
86 *features = MLX5_VDPA_DEFAULT_FEATURES;
87 if (priv->caps.virtio_queue_type & (1 << MLX5_VIRTQ_TYPE_PACKED))
88 *features |= (1ULL << VIRTIO_F_RING_PACKED);
89 if (priv->caps.tso_ipv4)
90 *features |= (1ULL << VIRTIO_NET_F_HOST_TSO4);
91 if (priv->caps.tso_ipv6)
92 *features |= (1ULL << VIRTIO_NET_F_HOST_TSO6);
93 if (priv->caps.tx_csum)
94 *features |= (1ULL << VIRTIO_NET_F_CSUM);
95 if (priv->caps.rx_csum)
96 *features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
97 if (priv->caps.virtio_version_1_0)
98 *features |= (1ULL << VIRTIO_F_VERSION_1);
103 mlx5_vdpa_get_protocol_features(int did, uint64_t *features)
105 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
108 DRV_LOG(ERR, "Invalid device id: %d.", did);
111 *features = MLX5_VDPA_PROTOCOL_FEATURES;
116 mlx5_vdpa_set_vring_state(int vid, int vring, int state)
118 int did = rte_vhost_get_vdpa_device_id(vid);
119 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
122 DRV_LOG(ERR, "Invalid device id: %d.", did);
125 if (vring >= (int)priv->caps.max_num_virtio_queues * 2) {
126 DRV_LOG(ERR, "Too big vring id: %d.", vring);
129 return mlx5_vdpa_virtq_enable(priv, vring, state);
133 mlx5_vdpa_direct_db_prepare(struct mlx5_vdpa_priv *priv)
137 if (priv->direct_notifier) {
138 ret = rte_vhost_host_notifier_ctrl(priv->vid, false);
140 DRV_LOG(INFO, "Direct HW notifier FD cannot be "
141 "destroyed for device %d: %d.", priv->vid, ret);
144 priv->direct_notifier = 0;
146 ret = rte_vhost_host_notifier_ctrl(priv->vid, true);
148 DRV_LOG(INFO, "Direct HW notifier FD cannot be configured for"
149 " device %d: %d.", priv->vid, ret);
151 priv->direct_notifier = 1;
156 mlx5_vdpa_features_set(int vid)
158 int did = rte_vhost_get_vdpa_device_id(vid);
159 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
160 uint64_t log_base, log_size;
165 DRV_LOG(ERR, "Invalid device id: %d.", did);
168 ret = rte_vhost_get_negotiated_features(vid, &features);
170 DRV_LOG(ERR, "Failed to get negotiated features.");
173 if (RTE_VHOST_NEED_LOG(features)) {
174 ret = rte_vhost_get_log_base(vid, &log_base, &log_size);
176 DRV_LOG(ERR, "Failed to get log base.");
179 ret = mlx5_vdpa_dirty_bitmap_set(priv, log_base, log_size);
181 DRV_LOG(ERR, "Failed to set dirty bitmap.");
184 DRV_LOG(INFO, "mlx5 vdpa: enabling dirty logging...");
185 ret = mlx5_vdpa_logging_enable(priv, 1);
187 DRV_LOG(ERR, "Failed t enable dirty logging.");
195 mlx5_vdpa_pd_create(struct mlx5_vdpa_priv *priv)
197 #ifdef HAVE_IBV_FLOW_DV_SUPPORT
198 priv->pd = mlx5_glue->alloc_pd(priv->ctx);
199 if (priv->pd == NULL) {
200 DRV_LOG(ERR, "Failed to allocate PD.");
201 return errno ? -errno : -ENOMEM;
203 struct mlx5dv_obj obj;
204 struct mlx5dv_pd pd_info;
207 obj.pd.in = priv->pd;
208 obj.pd.out = &pd_info;
209 ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_PD);
211 DRV_LOG(ERR, "Fail to get PD object info.");
212 mlx5_glue->dealloc_pd(priv->pd);
216 priv->pdn = pd_info.pdn;
220 DRV_LOG(ERR, "Cannot get pdn - no DV support.");
222 #endif /* HAVE_IBV_FLOW_DV_SUPPORT */
226 mlx5_vdpa_dev_close(int vid)
228 int did = rte_vhost_get_vdpa_device_id(vid);
229 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
233 DRV_LOG(ERR, "Invalid device id: %d.", did);
236 if (priv->configured)
237 ret |= mlx5_vdpa_lm_log(priv);
238 mlx5_vdpa_cqe_event_unset(priv);
239 mlx5_vdpa_steer_unset(priv);
240 mlx5_vdpa_virtqs_release(priv);
241 mlx5_vdpa_event_qp_global_release(priv);
242 mlx5_vdpa_mem_dereg(priv);
244 claim_zero(mlx5_glue->dealloc_pd(priv->pd));
247 priv->configured = 0;
249 DRV_LOG(INFO, "vDPA device %d was closed.", vid);
254 mlx5_vdpa_dev_config(int vid)
256 int did = rte_vhost_get_vdpa_device_id(vid);
257 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
260 DRV_LOG(ERR, "Invalid device id: %d.", did);
263 if (priv->configured && mlx5_vdpa_dev_close(vid)) {
264 DRV_LOG(ERR, "Failed to reconfigure vid %d.", vid);
268 if (mlx5_vdpa_pd_create(priv) || mlx5_vdpa_mem_register(priv) ||
269 mlx5_vdpa_direct_db_prepare(priv) ||
270 mlx5_vdpa_virtqs_prepare(priv) || mlx5_vdpa_steer_setup(priv) ||
271 mlx5_vdpa_cqe_event_setup(priv)) {
272 mlx5_vdpa_dev_close(vid);
275 priv->configured = 1;
276 DRV_LOG(INFO, "vDPA device %d was configured.", vid);
281 mlx5_vdpa_get_device_fd(int vid)
283 int did = rte_vhost_get_vdpa_device_id(vid);
284 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
287 DRV_LOG(ERR, "Invalid device id: %d.", did);
290 return priv->ctx->cmd_fd;
294 mlx5_vdpa_get_notify_area(int vid, int qid, uint64_t *offset, uint64_t *size)
296 int did = rte_vhost_get_vdpa_device_id(vid);
297 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
301 DRV_LOG(ERR, "Invalid device id: %d.", did);
305 DRV_LOG(ERR, "VAR was not created for device %d, is the device"
306 " configured?.", did);
309 *offset = priv->var->mmap_off;
310 *size = priv->var->length;
315 mlx5_vdpa_get_stats_names(int did, struct rte_vdpa_stat_name *stats_names,
318 static const char *mlx5_vdpa_stats_names[MLX5_VDPA_STATS_MAX] = {
319 "received_descriptors",
320 "completed_descriptors",
321 "bad descriptor errors",
326 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
330 DRV_LOG(ERR, "Invalid device id: %d.", did);
334 return MLX5_VDPA_STATS_MAX;
335 size = RTE_MIN(size, (unsigned int)MLX5_VDPA_STATS_MAX);
336 for (i = 0; i < size; ++i)
337 strlcpy(stats_names[i].name, mlx5_vdpa_stats_names[i],
338 RTE_VDPA_STATS_NAME_SIZE);
343 mlx5_vdpa_get_stats(int did, int qid, struct rte_vdpa_stat *stats,
346 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
349 DRV_LOG(ERR, "Invalid device id: %d.", did);
352 if (!priv->configured) {
353 DRV_LOG(ERR, "Device %d was not configured.", did);
356 if (qid >= (int)priv->nr_virtqs) {
357 DRV_LOG(ERR, "Too big vring id: %d.", qid);
360 if (!priv->caps.queue_counters_valid) {
361 DRV_LOG(ERR, "Virtq statistics is not supported for device %d.",
365 return mlx5_vdpa_virtq_stats_get(priv, qid, stats, n);
369 mlx5_vdpa_reset_stats(int did, int qid)
371 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
374 DRV_LOG(ERR, "Invalid device id: %d.", did);
377 if (!priv->configured) {
378 DRV_LOG(ERR, "Device %d was not configured.", did);
381 if (qid >= (int)priv->nr_virtqs) {
382 DRV_LOG(ERR, "Too big vring id: %d.", qid);
385 if (!priv->caps.queue_counters_valid) {
386 DRV_LOG(ERR, "Virtq statistics is not supported for device %d.",
390 return mlx5_vdpa_virtq_stats_reset(priv, qid);
393 static struct rte_vdpa_dev_ops mlx5_vdpa_ops = {
394 .get_queue_num = mlx5_vdpa_get_queue_num,
395 .get_features = mlx5_vdpa_get_vdpa_features,
396 .get_protocol_features = mlx5_vdpa_get_protocol_features,
397 .dev_conf = mlx5_vdpa_dev_config,
398 .dev_close = mlx5_vdpa_dev_close,
399 .set_vring_state = mlx5_vdpa_set_vring_state,
400 .set_features = mlx5_vdpa_features_set,
401 .migration_done = NULL,
402 .get_vfio_group_fd = NULL,
403 .get_vfio_device_fd = mlx5_vdpa_get_device_fd,
404 .get_notify_area = mlx5_vdpa_get_notify_area,
405 .get_stats_names = mlx5_vdpa_get_stats_names,
406 .get_stats = mlx5_vdpa_get_stats,
407 .reset_stats = mlx5_vdpa_reset_stats,
410 static struct ibv_device *
411 mlx5_vdpa_get_ib_device_match(struct rte_pci_addr *addr)
414 struct ibv_device **ibv_list = mlx5_glue->get_device_list(&n);
415 struct ibv_device *ibv_match = NULL;
422 struct rte_pci_addr pci_addr;
424 DRV_LOG(DEBUG, "Checking device \"%s\"..", ibv_list[n]->name);
425 if (mlx5_dev_to_pci_addr(ibv_list[n]->ibdev_path, &pci_addr))
427 if (rte_pci_addr_cmp(addr, &pci_addr))
429 ibv_match = ibv_list[n];
434 mlx5_glue->free_device_list(ibv_list);
438 /* Try to disable ROCE by Netlink\Devlink. */
440 mlx5_vdpa_nl_roce_disable(const char *addr)
442 int nlsk_fd = mlx5_nl_init(NETLINK_GENERIC);
449 devlink_id = mlx5_nl_devlink_family_id_get(nlsk_fd);
450 if (devlink_id < 0) {
452 DRV_LOG(DEBUG, "Failed to get devlink id for ROCE operations by"
456 ret = mlx5_nl_enable_roce_get(nlsk_fd, devlink_id, addr, &enable);
458 DRV_LOG(DEBUG, "Failed to get ROCE enable by Netlink: %d.",
461 } else if (!enable) {
462 DRV_LOG(INFO, "ROCE has already disabled(Netlink).");
465 ret = mlx5_nl_enable_roce_set(nlsk_fd, devlink_id, addr, 0);
467 DRV_LOG(DEBUG, "Failed to disable ROCE by Netlink: %d.", ret);
469 DRV_LOG(INFO, "ROCE is disabled by Netlink successfully.");
475 /* Try to disable ROCE by sysfs. */
477 mlx5_vdpa_sys_roce_disable(const char *addr)
483 MKSTR(file_p, "/sys/bus/pci/devices/%s/roce_enable", addr);
484 file_o = fopen(file_p, "rb");
489 ret = fscanf(file_o, "%d", &enable);
494 } else if (!enable) {
496 DRV_LOG(INFO, "ROCE has already disabled(sysfs).");
500 file_o = fopen(file_p, "wb");
505 fprintf(file_o, "0\n");
509 DRV_LOG(DEBUG, "Failed to disable ROCE by sysfs: %d.", ret);
511 DRV_LOG(INFO, "ROCE is disabled by sysfs successfully.");
516 #define MLX5_VDPA_MAX_RETRIES 20
517 #define MLX5_VDPA_USEC 1000
519 mlx5_vdpa_roce_disable(struct rte_pci_addr *addr, struct ibv_device **ibv)
521 char addr_name[64] = {0};
523 rte_pci_device_name(addr, addr_name, sizeof(addr_name));
524 /* Firstly try to disable ROCE by Netlink and fallback to sysfs. */
525 if (mlx5_vdpa_nl_roce_disable(addr_name) == 0 ||
526 mlx5_vdpa_sys_roce_disable(addr_name) == 0) {
528 * Succeed to disable ROCE, wait for the IB device to appear
529 * again after reload.
532 struct ibv_device *ibv_new;
534 for (r = MLX5_VDPA_MAX_RETRIES; r; r--) {
535 ibv_new = mlx5_vdpa_get_ib_device_match(addr);
540 usleep(MLX5_VDPA_USEC);
542 DRV_LOG(ERR, "Cannot much device %s after ROCE disable, "
543 "retries exceed %d", addr_name, MLX5_VDPA_MAX_RETRIES);
550 * DPDK callback to register a PCI device.
552 * This function spawns vdpa device out of a given PCI device.
555 * PCI driver structure (mlx5_vpda_driver).
557 * PCI device information.
560 * 0 on success, 1 to skip this driver, a negative errno value otherwise
561 * and rte_errno is set.
564 mlx5_vdpa_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
565 struct rte_pci_device *pci_dev __rte_unused)
567 struct ibv_device *ibv;
568 struct mlx5_vdpa_priv *priv = NULL;
569 struct ibv_context *ctx = NULL;
570 struct mlx5_hca_attr attr;
573 if (mlx5_class_get(pci_dev->device.devargs) != MLX5_CLASS_VDPA) {
574 DRV_LOG(DEBUG, "Skip probing - should be probed by other mlx5"
578 ibv = mlx5_vdpa_get_ib_device_match(&pci_dev->addr);
580 DRV_LOG(ERR, "No matching IB device for PCI slot "
581 PCI_PRI_FMT ".", pci_dev->addr.domain,
582 pci_dev->addr.bus, pci_dev->addr.devid,
583 pci_dev->addr.function);
586 DRV_LOG(INFO, "PCI information matches for device \"%s\".",
589 if (mlx5_vdpa_roce_disable(&pci_dev->addr, &ibv) != 0) {
590 DRV_LOG(WARNING, "Failed to disable ROCE for \"%s\".",
594 ctx = mlx5_glue->dv_open_device(ibv);
596 DRV_LOG(ERR, "Failed to open IB device \"%s\".", ibv->name);
600 ret = mlx5_devx_cmd_query_hca_attr(ctx, &attr);
602 DRV_LOG(ERR, "Unable to read HCA capabilities.");
605 } else if (!attr.vdpa.valid || !attr.vdpa.max_num_virtio_queues) {
606 DRV_LOG(ERR, "Not enough capabilities to support vdpa, maybe "
607 "old FW/OFED version?");
611 if (!attr.vdpa.queue_counters_valid)
612 DRV_LOG(DEBUG, "No capability to support virtq statistics.");
613 priv = rte_zmalloc("mlx5 vDPA device private", sizeof(*priv) +
614 sizeof(struct mlx5_vdpa_virtq) *
615 attr.vdpa.max_num_virtio_queues * 2,
616 RTE_CACHE_LINE_SIZE);
618 DRV_LOG(ERR, "Failed to allocate private memory.");
622 priv->caps = attr.vdpa;
623 priv->log_max_rqt_size = attr.log_max_rqt_size;
625 priv->dev_addr.pci_addr = pci_dev->addr;
626 priv->dev_addr.type = VDPA_ADDR_PCI;
627 priv->var = mlx5_glue->dv_alloc_var(ctx, 0);
629 DRV_LOG(ERR, "Failed to allocate VAR %u.\n", errno);
632 priv->id = rte_vdpa_register_device(&priv->dev_addr, &mlx5_vdpa_ops);
634 DRV_LOG(ERR, "Failed to register vDPA device.");
635 rte_errno = rte_errno ? rte_errno : EINVAL;
638 SLIST_INIT(&priv->mr_list);
639 pthread_mutex_lock(&priv_list_lock);
640 TAILQ_INSERT_TAIL(&priv_list, priv, next);
641 pthread_mutex_unlock(&priv_list_lock);
647 mlx5_glue->dv_free_var(priv->var);
651 mlx5_glue->close_device(ctx);
656 * DPDK callback to remove a PCI device.
658 * This function removes all vDPA devices belong to a given PCI device.
661 * Pointer to the PCI device.
664 * 0 on success, the function cannot fail.
667 mlx5_vdpa_pci_remove(struct rte_pci_device *pci_dev)
669 struct mlx5_vdpa_priv *priv = NULL;
672 pthread_mutex_lock(&priv_list_lock);
673 TAILQ_FOREACH(priv, &priv_list, next) {
674 if (memcmp(&priv->dev_addr.pci_addr, &pci_dev->addr,
675 sizeof(pci_dev->addr)) == 0) {
681 TAILQ_REMOVE(&priv_list, priv, next);
682 pthread_mutex_unlock(&priv_list_lock);
684 if (priv->configured)
685 mlx5_vdpa_dev_close(priv->vid);
687 mlx5_glue->dv_free_var(priv->var);
690 mlx5_glue->close_device(priv->ctx);
696 static const struct rte_pci_id mlx5_vdpa_pci_id_map[] = {
698 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
699 PCI_DEVICE_ID_MELLANOX_CONNECTX6)
702 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
703 PCI_DEVICE_ID_MELLANOX_CONNECTX6VF)
706 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
707 PCI_DEVICE_ID_MELLANOX_CONNECTX6DX)
710 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
711 PCI_DEVICE_ID_MELLANOX_CONNECTX6DXVF)
714 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
715 PCI_DEVICE_ID_MELLANOX_CONNECTX6DXBF)
722 static struct rte_pci_driver mlx5_vdpa_driver = {
726 .id_table = mlx5_vdpa_pci_id_map,
727 .probe = mlx5_vdpa_pci_probe,
728 .remove = mlx5_vdpa_pci_remove,
733 * Driver initialization routine.
735 RTE_INIT(rte_mlx5_vdpa_init)
737 /* Initialize common log type. */
738 mlx5_vdpa_logtype = rte_log_register("pmd.vdpa.mlx5");
739 if (mlx5_vdpa_logtype >= 0)
740 rte_log_set_level(mlx5_vdpa_logtype, RTE_LOG_NOTICE);
742 rte_pci_register(&mlx5_vdpa_driver);
745 RTE_PMD_EXPORT_NAME(net_mlx5_vdpa, __COUNTER__);
746 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5_vdpa, mlx5_vdpa_pci_id_map);
747 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5_vdpa, "* ib_uverbs & mlx5_core & mlx5_ib");