1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 Mellanox Technologies, Ltd
6 #include <rte_malloc.h>
9 #include <rte_bus_pci.h>
12 #include <mlx5_glue.h>
13 #include <mlx5_common.h>
14 #include <mlx5_devx_cmds.h>
18 #include "mlx5_vdpa_utils.h"
19 #include "mlx5_vdpa.h"
22 #define MLX5_VDPA_DEFAULT_FEATURES ((1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
23 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
24 (1ULL << VIRTIO_NET_F_MQ) | \
25 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
26 (1ULL << VIRTIO_F_ORDER_PLATFORM) | \
27 (1ULL << VHOST_F_LOG_ALL))
29 #define MLX5_VDPA_PROTOCOL_FEATURES \
30 ((1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ) | \
31 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD) | \
32 (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER) | \
33 (1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD) | \
34 (1ULL << VHOST_USER_PROTOCOL_F_MQ))
36 TAILQ_HEAD(mlx5_vdpa_privs, mlx5_vdpa_priv) priv_list =
37 TAILQ_HEAD_INITIALIZER(priv_list);
38 static pthread_mutex_t priv_list_lock = PTHREAD_MUTEX_INITIALIZER;
39 int mlx5_vdpa_logtype;
41 static struct mlx5_vdpa_priv *
42 mlx5_vdpa_find_priv_resource_by_did(int did)
44 struct mlx5_vdpa_priv *priv;
47 pthread_mutex_lock(&priv_list_lock);
48 TAILQ_FOREACH(priv, &priv_list, next) {
49 if (did == priv->id) {
54 pthread_mutex_unlock(&priv_list_lock);
56 DRV_LOG(ERR, "Invalid device id: %d.", did);
64 mlx5_vdpa_get_queue_num(int did, uint32_t *queue_num)
66 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
69 DRV_LOG(ERR, "Invalid device id: %d.", did);
72 *queue_num = priv->caps.max_num_virtio_queues;
77 mlx5_vdpa_get_vdpa_features(int did, uint64_t *features)
79 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
82 DRV_LOG(ERR, "Invalid device id: %d.", did);
85 *features = MLX5_VDPA_DEFAULT_FEATURES;
86 if (priv->caps.virtio_queue_type & (1 << MLX5_VIRTQ_TYPE_PACKED))
87 *features |= (1ULL << VIRTIO_F_RING_PACKED);
88 if (priv->caps.tso_ipv4)
89 *features |= (1ULL << VIRTIO_NET_F_HOST_TSO4);
90 if (priv->caps.tso_ipv6)
91 *features |= (1ULL << VIRTIO_NET_F_HOST_TSO6);
92 if (priv->caps.tx_csum)
93 *features |= (1ULL << VIRTIO_NET_F_CSUM);
94 if (priv->caps.rx_csum)
95 *features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
96 if (priv->caps.virtio_version_1_0)
97 *features |= (1ULL << VIRTIO_F_VERSION_1);
102 mlx5_vdpa_get_protocol_features(int did, uint64_t *features)
104 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
107 DRV_LOG(ERR, "Invalid device id: %d.", did);
110 *features = MLX5_VDPA_PROTOCOL_FEATURES;
115 mlx5_vdpa_set_vring_state(int vid, int vring, int state)
117 int did = rte_vhost_get_vdpa_device_id(vid);
118 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
119 struct mlx5_vdpa_virtq *virtq = NULL;
122 DRV_LOG(ERR, "Invalid device id: %d.", did);
125 SLIST_FOREACH(virtq, &priv->virtq_list, next)
126 if (virtq->index == vring)
129 DRV_LOG(ERR, "Invalid or unconfigured vring id: %d.", vring);
132 return mlx5_vdpa_virtq_enable(virtq, state);
136 mlx5_vdpa_features_set(int vid)
138 int did = rte_vhost_get_vdpa_device_id(vid);
139 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
140 uint64_t log_base, log_size;
145 DRV_LOG(ERR, "Invalid device id: %d.", did);
148 ret = rte_vhost_get_negotiated_features(vid, &features);
150 DRV_LOG(ERR, "Failed to get negotiated features.");
153 if (RTE_VHOST_NEED_LOG(features)) {
154 ret = rte_vhost_get_log_base(vid, &log_base, &log_size);
156 DRV_LOG(ERR, "Failed to get log base.");
159 ret = mlx5_vdpa_dirty_bitmap_set(priv, log_base, log_size);
161 DRV_LOG(ERR, "Failed to set dirty bitmap.");
164 DRV_LOG(INFO, "mlx5 vdpa: enabling dirty logging...");
165 ret = mlx5_vdpa_logging_enable(priv, 1);
167 DRV_LOG(ERR, "Failed t enable dirty logging.");
175 mlx5_vdpa_dev_close(int vid)
177 int did = rte_vhost_get_vdpa_device_id(vid);
178 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
182 DRV_LOG(ERR, "Invalid device id: %d.", did);
185 if (priv->configured)
186 ret |= mlx5_vdpa_lm_log(priv);
187 mlx5_vdpa_cqe_event_unset(priv);
188 ret |= mlx5_vdpa_steer_unset(priv);
189 mlx5_vdpa_virtqs_release(priv);
190 mlx5_vdpa_event_qp_global_release(priv);
191 mlx5_vdpa_mem_dereg(priv);
192 priv->configured = 0;
198 mlx5_vdpa_dev_config(int vid)
200 int did = rte_vhost_get_vdpa_device_id(vid);
201 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
204 DRV_LOG(ERR, "Invalid device id: %d.", did);
207 if (priv->configured && mlx5_vdpa_dev_close(vid)) {
208 DRV_LOG(ERR, "Failed to reconfigure vid %d.", vid);
212 if (mlx5_vdpa_mem_register(priv) || mlx5_vdpa_virtqs_prepare(priv) ||
213 mlx5_vdpa_steer_setup(priv) || mlx5_vdpa_cqe_event_setup(priv)) {
214 mlx5_vdpa_dev_close(vid);
217 priv->configured = 1;
221 static struct rte_vdpa_dev_ops mlx5_vdpa_ops = {
222 .get_queue_num = mlx5_vdpa_get_queue_num,
223 .get_features = mlx5_vdpa_get_vdpa_features,
224 .get_protocol_features = mlx5_vdpa_get_protocol_features,
225 .dev_conf = mlx5_vdpa_dev_config,
226 .dev_close = mlx5_vdpa_dev_close,
227 .set_vring_state = mlx5_vdpa_set_vring_state,
228 .set_features = mlx5_vdpa_features_set,
229 .migration_done = NULL,
230 .get_vfio_group_fd = NULL,
231 .get_vfio_device_fd = NULL,
232 .get_notify_area = NULL,
235 static struct ibv_device *
236 mlx5_vdpa_get_ib_device_match(struct rte_pci_addr *addr)
239 struct ibv_device **ibv_list = mlx5_glue->get_device_list(&n);
240 struct ibv_device *ibv_match = NULL;
247 struct rte_pci_addr pci_addr;
249 DRV_LOG(DEBUG, "Checking device \"%s\"..", ibv_list[n]->name);
250 if (mlx5_dev_to_pci_addr(ibv_list[n]->ibdev_path, &pci_addr))
252 if (memcmp(addr, &pci_addr, sizeof(pci_addr)))
254 ibv_match = ibv_list[n];
259 mlx5_glue->free_device_list(ibv_list);
263 /* Try to disable ROCE by Netlink\Devlink. */
265 mlx5_vdpa_nl_roce_disable(const char *addr)
267 int nlsk_fd = mlx5_nl_init(NETLINK_GENERIC);
274 devlink_id = mlx5_nl_devlink_family_id_get(nlsk_fd);
275 if (devlink_id < 0) {
277 DRV_LOG(DEBUG, "Failed to get devlink id for ROCE operations by"
281 ret = mlx5_nl_enable_roce_get(nlsk_fd, devlink_id, addr, &enable);
283 DRV_LOG(DEBUG, "Failed to get ROCE enable by Netlink: %d.",
286 } else if (!enable) {
287 DRV_LOG(INFO, "ROCE has already disabled(Netlink).");
290 ret = mlx5_nl_enable_roce_set(nlsk_fd, devlink_id, addr, 0);
292 DRV_LOG(DEBUG, "Failed to disable ROCE by Netlink: %d.", ret);
294 DRV_LOG(INFO, "ROCE is disabled by Netlink successfully.");
300 /* Try to disable ROCE by sysfs. */
302 mlx5_vdpa_sys_roce_disable(const char *addr)
308 MKSTR(file_p, "/sys/bus/pci/devices/%s/roce_enable", addr);
309 file_o = fopen(file_p, "rb");
314 ret = fscanf(file_o, "%d", &enable);
319 } else if (!enable) {
321 DRV_LOG(INFO, "ROCE has already disabled(sysfs).");
325 file_o = fopen(file_p, "wb");
330 fprintf(file_o, "0\n");
334 DRV_LOG(DEBUG, "Failed to disable ROCE by sysfs: %d.", ret);
336 DRV_LOG(INFO, "ROCE is disabled by sysfs successfully.");
341 #define MLX5_VDPA_MAX_RETRIES 20
342 #define MLX5_VDPA_USEC 1000
344 mlx5_vdpa_roce_disable(struct rte_pci_addr *addr, struct ibv_device **ibv)
346 char addr_name[64] = {0};
348 rte_pci_device_name(addr, addr_name, sizeof(addr_name));
349 /* Firstly try to disable ROCE by Netlink and fallback to sysfs. */
350 if (mlx5_vdpa_nl_roce_disable(addr_name) == 0 ||
351 mlx5_vdpa_sys_roce_disable(addr_name) == 0) {
353 * Succeed to disable ROCE, wait for the IB device to appear
354 * again after reload.
357 struct ibv_device *ibv_new;
359 for (r = MLX5_VDPA_MAX_RETRIES; r; r--) {
360 ibv_new = mlx5_vdpa_get_ib_device_match(addr);
365 usleep(MLX5_VDPA_USEC);
367 DRV_LOG(ERR, "Cannot much device %s after ROCE disable, "
368 "retries exceed %d", addr_name, MLX5_VDPA_MAX_RETRIES);
375 * DPDK callback to register a PCI device.
377 * This function spawns vdpa device out of a given PCI device.
380 * PCI driver structure (mlx5_vpda_driver).
382 * PCI device information.
385 * 0 on success, 1 to skip this driver, a negative errno value otherwise
386 * and rte_errno is set.
389 mlx5_vdpa_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
390 struct rte_pci_device *pci_dev __rte_unused)
392 struct ibv_device *ibv;
393 struct mlx5_vdpa_priv *priv = NULL;
394 struct ibv_context *ctx = NULL;
395 struct mlx5_hca_attr attr;
398 if (mlx5_class_get(pci_dev->device.devargs) != MLX5_CLASS_VDPA) {
399 DRV_LOG(DEBUG, "Skip probing - should be probed by other mlx5"
403 ibv = mlx5_vdpa_get_ib_device_match(&pci_dev->addr);
405 DRV_LOG(ERR, "No matching IB device for PCI slot "
406 PCI_PRI_FMT ".", pci_dev->addr.domain,
407 pci_dev->addr.bus, pci_dev->addr.devid,
408 pci_dev->addr.function);
411 DRV_LOG(INFO, "PCI information matches for device \"%s\".",
414 if (mlx5_vdpa_roce_disable(&pci_dev->addr, &ibv) != 0) {
415 DRV_LOG(WARNING, "Failed to disable ROCE for \"%s\".",
419 ctx = mlx5_glue->dv_open_device(ibv);
421 DRV_LOG(ERR, "Failed to open IB device \"%s\".", ibv->name);
425 priv = rte_zmalloc("mlx5 vDPA device private", sizeof(*priv),
426 RTE_CACHE_LINE_SIZE);
428 DRV_LOG(ERR, "Failed to allocate private memory.");
432 ret = mlx5_devx_cmd_query_hca_attr(ctx, &attr);
434 DRV_LOG(ERR, "Unable to read HCA capabilities.");
438 if (!attr.vdpa.valid || !attr.vdpa.max_num_virtio_queues) {
439 DRV_LOG(ERR, "Not enough capabilities to support vdpa,"
440 " maybe old FW/OFED version?");
444 priv->caps = attr.vdpa;
445 priv->log_max_rqt_size = attr.log_max_rqt_size;
448 priv->dev_addr.pci_addr = pci_dev->addr;
449 priv->dev_addr.type = PCI_ADDR;
450 priv->id = rte_vdpa_register_device(&priv->dev_addr, &mlx5_vdpa_ops);
452 DRV_LOG(ERR, "Failed to register vDPA device.");
453 rte_errno = rte_errno ? rte_errno : EINVAL;
456 SLIST_INIT(&priv->mr_list);
457 SLIST_INIT(&priv->virtq_list);
458 pthread_mutex_lock(&priv_list_lock);
459 TAILQ_INSERT_TAIL(&priv_list, priv, next);
460 pthread_mutex_unlock(&priv_list_lock);
467 mlx5_glue->close_device(ctx);
472 * DPDK callback to remove a PCI device.
474 * This function removes all vDPA devices belong to a given PCI device.
477 * Pointer to the PCI device.
480 * 0 on success, the function cannot fail.
483 mlx5_vdpa_pci_remove(struct rte_pci_device *pci_dev)
485 struct mlx5_vdpa_priv *priv = NULL;
488 pthread_mutex_lock(&priv_list_lock);
489 TAILQ_FOREACH(priv, &priv_list, next) {
490 if (memcmp(&priv->dev_addr.pci_addr, &pci_dev->addr,
491 sizeof(pci_dev->addr)) == 0) {
497 TAILQ_REMOVE(&priv_list, priv, next);
498 pthread_mutex_unlock(&priv_list_lock);
500 if (priv->configured)
501 mlx5_vdpa_dev_close(priv->vid);
502 mlx5_glue->close_device(priv->ctx);
508 static const struct rte_pci_id mlx5_vdpa_pci_id_map[] = {
510 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
511 PCI_DEVICE_ID_MELLANOX_CONNECTX5BF)
514 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
515 PCI_DEVICE_ID_MELLANOX_CONNECTX5BFVF)
518 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
519 PCI_DEVICE_ID_MELLANOX_CONNECTX6)
522 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
523 PCI_DEVICE_ID_MELLANOX_CONNECTX6VF)
526 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
527 PCI_DEVICE_ID_MELLANOX_CONNECTX6DX)
530 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
531 PCI_DEVICE_ID_MELLANOX_CONNECTX6DXVF)
538 static struct rte_pci_driver mlx5_vdpa_driver = {
542 .id_table = mlx5_vdpa_pci_id_map,
543 .probe = mlx5_vdpa_pci_probe,
544 .remove = mlx5_vdpa_pci_remove,
549 * Driver initialization routine.
551 RTE_INIT(rte_mlx5_vdpa_init)
553 /* Initialize common log type. */
554 mlx5_vdpa_logtype = rte_log_register("pmd.vdpa.mlx5");
555 if (mlx5_vdpa_logtype >= 0)
556 rte_log_set_level(mlx5_vdpa_logtype, RTE_LOG_NOTICE);
558 rte_pci_register(&mlx5_vdpa_driver);
561 RTE_PMD_EXPORT_NAME(net_mlx5_vdpa, __COUNTER__);
562 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5_vdpa, mlx5_vdpa_pci_id_map);
563 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5_vdpa, "* ib_uverbs & mlx5_core & mlx5_ib");