1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 Mellanox Technologies, Ltd
4 #include <rte_malloc.h>
7 #include <rte_bus_pci.h>
10 #include <mlx5_common.h>
11 #include <mlx5_devx_cmds.h>
14 #include "mlx5_vdpa_utils.h"
15 #include "mlx5_vdpa.h"
18 #define MLX5_VDPA_DEFAULT_FEATURES ((1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
19 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
20 (1ULL << VIRTIO_NET_F_MQ) | \
21 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
22 (1ULL << VIRTIO_F_ORDER_PLATFORM) | \
23 (1ULL << VHOST_F_LOG_ALL))
25 #define MLX5_VDPA_PROTOCOL_FEATURES \
26 ((1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ) | \
27 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD) | \
28 (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER) | \
29 (1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD) | \
30 (1ULL << VHOST_USER_PROTOCOL_F_MQ))
32 TAILQ_HEAD(mlx5_vdpa_privs, mlx5_vdpa_priv) priv_list =
33 TAILQ_HEAD_INITIALIZER(priv_list);
34 static pthread_mutex_t priv_list_lock = PTHREAD_MUTEX_INITIALIZER;
35 int mlx5_vdpa_logtype;
37 static struct mlx5_vdpa_priv *
38 mlx5_vdpa_find_priv_resource_by_did(int did)
40 struct mlx5_vdpa_priv *priv;
43 pthread_mutex_lock(&priv_list_lock);
44 TAILQ_FOREACH(priv, &priv_list, next) {
45 if (did == priv->id) {
50 pthread_mutex_unlock(&priv_list_lock);
52 DRV_LOG(ERR, "Invalid device id: %d.", did);
60 mlx5_vdpa_get_queue_num(int did, uint32_t *queue_num)
62 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
65 DRV_LOG(ERR, "Invalid device id: %d.", did);
68 *queue_num = priv->caps.max_num_virtio_queues;
73 mlx5_vdpa_get_vdpa_features(int did, uint64_t *features)
75 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
78 DRV_LOG(ERR, "Invalid device id: %d.", did);
81 *features = MLX5_VDPA_DEFAULT_FEATURES;
82 if (priv->caps.virtio_queue_type & (1 << MLX5_VIRTQ_TYPE_PACKED))
83 *features |= (1ULL << VIRTIO_F_RING_PACKED);
84 if (priv->caps.tso_ipv4)
85 *features |= (1ULL << VIRTIO_NET_F_HOST_TSO4);
86 if (priv->caps.tso_ipv6)
87 *features |= (1ULL << VIRTIO_NET_F_HOST_TSO6);
88 if (priv->caps.tx_csum)
89 *features |= (1ULL << VIRTIO_NET_F_CSUM);
90 if (priv->caps.rx_csum)
91 *features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
92 if (priv->caps.virtio_version_1_0)
93 *features |= (1ULL << VIRTIO_F_VERSION_1);
98 mlx5_vdpa_get_protocol_features(int did, uint64_t *features)
100 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
103 DRV_LOG(ERR, "Invalid device id: %d.", did);
106 *features = MLX5_VDPA_PROTOCOL_FEATURES;
111 mlx5_vdpa_set_vring_state(int vid, int vring, int state)
113 int did = rte_vhost_get_vdpa_device_id(vid);
114 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
115 struct mlx5_vdpa_virtq *virtq = NULL;
118 DRV_LOG(ERR, "Invalid device id: %d.", did);
121 SLIST_FOREACH(virtq, &priv->virtq_list, next)
122 if (virtq->index == vring)
125 DRV_LOG(ERR, "Invalid or unconfigured vring id: %d.", vring);
128 return mlx5_vdpa_virtq_enable(virtq, state);
132 mlx5_vdpa_features_set(int vid)
134 int did = rte_vhost_get_vdpa_device_id(vid);
135 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
136 uint64_t log_base, log_size;
141 DRV_LOG(ERR, "Invalid device id: %d.", did);
144 ret = rte_vhost_get_negotiated_features(vid, &features);
146 DRV_LOG(ERR, "Failed to get negotiated features.");
149 if (RTE_VHOST_NEED_LOG(features)) {
150 ret = rte_vhost_get_log_base(vid, &log_base, &log_size);
152 DRV_LOG(ERR, "Failed to get log base.");
155 ret = mlx5_vdpa_dirty_bitmap_set(priv, log_base, log_size);
157 DRV_LOG(ERR, "Failed to set dirty bitmap.");
160 DRV_LOG(INFO, "mlx5 vdpa: enabling dirty logging...");
161 ret = mlx5_vdpa_logging_enable(priv, 1);
163 DRV_LOG(ERR, "Failed t enable dirty logging.");
170 static struct rte_vdpa_dev_ops mlx5_vdpa_ops = {
171 .get_queue_num = mlx5_vdpa_get_queue_num,
172 .get_features = mlx5_vdpa_get_vdpa_features,
173 .get_protocol_features = mlx5_vdpa_get_protocol_features,
176 .set_vring_state = mlx5_vdpa_set_vring_state,
177 .set_features = mlx5_vdpa_features_set,
178 .migration_done = NULL,
179 .get_vfio_group_fd = NULL,
180 .get_vfio_device_fd = NULL,
181 .get_notify_area = NULL,
185 * DPDK callback to register a PCI device.
187 * This function spawns vdpa device out of a given PCI device.
190 * PCI driver structure (mlx5_vpda_driver).
192 * PCI device information.
195 * 0 on success, 1 to skip this driver, a negative errno value otherwise
196 * and rte_errno is set.
199 mlx5_vdpa_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
200 struct rte_pci_device *pci_dev __rte_unused)
202 struct ibv_device **ibv_list;
203 struct ibv_device *ibv_match = NULL;
204 struct mlx5_vdpa_priv *priv = NULL;
205 struct ibv_context *ctx = NULL;
206 struct mlx5_hca_attr attr;
209 if (mlx5_class_get(pci_dev->device.devargs) != MLX5_CLASS_VDPA) {
210 DRV_LOG(DEBUG, "Skip probing - should be probed by other mlx5"
215 ibv_list = mlx5_glue->get_device_list(&ret);
218 DRV_LOG(ERR, "Failed to get device list, is ib_uverbs loaded?");
222 struct rte_pci_addr pci_addr;
224 DRV_LOG(DEBUG, "Checking device \"%s\"..", ibv_list[ret]->name);
225 if (mlx5_dev_to_pci_addr(ibv_list[ret]->ibdev_path, &pci_addr))
227 if (pci_dev->addr.domain != pci_addr.domain ||
228 pci_dev->addr.bus != pci_addr.bus ||
229 pci_dev->addr.devid != pci_addr.devid ||
230 pci_dev->addr.function != pci_addr.function)
232 DRV_LOG(INFO, "PCI information matches for device \"%s\".",
233 ibv_list[ret]->name);
234 ibv_match = ibv_list[ret];
237 mlx5_glue->free_device_list(ibv_list);
239 DRV_LOG(ERR, "No matching IB device for PCI slot "
240 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 ".",
241 pci_dev->addr.domain, pci_dev->addr.bus,
242 pci_dev->addr.devid, pci_dev->addr.function);
246 ctx = mlx5_glue->dv_open_device(ibv_match);
248 DRV_LOG(ERR, "Failed to open IB device \"%s\".",
253 priv = rte_zmalloc("mlx5 vDPA device private", sizeof(*priv),
254 RTE_CACHE_LINE_SIZE);
256 DRV_LOG(ERR, "Failed to allocate private memory.");
260 ret = mlx5_devx_cmd_query_hca_attr(ctx, &attr);
262 DRV_LOG(ERR, "Unable to read HCA capabilities.");
266 if (!attr.vdpa.valid || !attr.vdpa.max_num_virtio_queues) {
267 DRV_LOG(ERR, "Not enough capabilities to support vdpa,"
268 " maybe old FW/OFED version?");
272 priv->caps = attr.vdpa;
273 priv->log_max_rqt_size = attr.log_max_rqt_size;
276 priv->dev_addr.pci_addr = pci_dev->addr;
277 priv->dev_addr.type = PCI_ADDR;
278 priv->id = rte_vdpa_register_device(&priv->dev_addr, &mlx5_vdpa_ops);
280 DRV_LOG(ERR, "Failed to register vDPA device.");
281 rte_errno = rte_errno ? rte_errno : EINVAL;
284 SLIST_INIT(&priv->mr_list);
285 SLIST_INIT(&priv->virtq_list);
286 pthread_mutex_lock(&priv_list_lock);
287 TAILQ_INSERT_TAIL(&priv_list, priv, next);
288 pthread_mutex_unlock(&priv_list_lock);
295 mlx5_glue->close_device(ctx);
300 * DPDK callback to remove a PCI device.
302 * This function removes all vDPA devices belong to a given PCI device.
305 * Pointer to the PCI device.
308 * 0 on success, the function cannot fail.
311 mlx5_vdpa_pci_remove(struct rte_pci_device *pci_dev)
313 struct mlx5_vdpa_priv *priv = NULL;
316 pthread_mutex_lock(&priv_list_lock);
317 TAILQ_FOREACH(priv, &priv_list, next) {
318 if (memcmp(&priv->dev_addr.pci_addr, &pci_dev->addr,
319 sizeof(pci_dev->addr)) == 0) {
325 TAILQ_REMOVE(&priv_list, priv, next);
326 mlx5_glue->close_device(priv->ctx);
329 pthread_mutex_unlock(&priv_list_lock);
333 static const struct rte_pci_id mlx5_vdpa_pci_id_map[] = {
335 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
336 PCI_DEVICE_ID_MELLANOX_CONNECTX5BF)
339 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
340 PCI_DEVICE_ID_MELLANOX_CONNECTX5BFVF)
343 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
344 PCI_DEVICE_ID_MELLANOX_CONNECTX6)
347 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
348 PCI_DEVICE_ID_MELLANOX_CONNECTX6VF)
351 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
352 PCI_DEVICE_ID_MELLANOX_CONNECTX6DX)
355 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
356 PCI_DEVICE_ID_MELLANOX_CONNECTX6DXVF)
363 static struct rte_pci_driver mlx5_vdpa_driver = {
367 .id_table = mlx5_vdpa_pci_id_map,
368 .probe = mlx5_vdpa_pci_probe,
369 .remove = mlx5_vdpa_pci_remove,
374 * Driver initialization routine.
376 RTE_INIT(rte_mlx5_vdpa_init)
378 /* Initialize common log type. */
379 mlx5_vdpa_logtype = rte_log_register("pmd.vdpa.mlx5");
380 if (mlx5_vdpa_logtype >= 0)
381 rte_log_set_level(mlx5_vdpa_logtype, RTE_LOG_NOTICE);
383 rte_pci_register(&mlx5_vdpa_driver);
386 RTE_PMD_EXPORT_NAME(net_mlx5_vdpa, __COUNTER__);
387 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5_vdpa, mlx5_vdpa_pci_id_map);
388 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5_vdpa, "* ib_uverbs & mlx5_core & mlx5_ib");