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))
24 #define MLX5_VDPA_PROTOCOL_FEATURES \
25 ((1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ) | \
26 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD) | \
27 (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER) | \
28 (1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD) | \
29 (1ULL << VHOST_USER_PROTOCOL_F_MQ))
31 TAILQ_HEAD(mlx5_vdpa_privs, mlx5_vdpa_priv) priv_list =
32 TAILQ_HEAD_INITIALIZER(priv_list);
33 static pthread_mutex_t priv_list_lock = PTHREAD_MUTEX_INITIALIZER;
34 int mlx5_vdpa_logtype;
36 static struct mlx5_vdpa_priv *
37 mlx5_vdpa_find_priv_resource_by_did(int did)
39 struct mlx5_vdpa_priv *priv;
42 pthread_mutex_lock(&priv_list_lock);
43 TAILQ_FOREACH(priv, &priv_list, next) {
44 if (did == priv->id) {
49 pthread_mutex_unlock(&priv_list_lock);
51 DRV_LOG(ERR, "Invalid device id: %d.", did);
59 mlx5_vdpa_get_queue_num(int did, uint32_t *queue_num)
61 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
64 DRV_LOG(ERR, "Invalid device id: %d.", did);
67 *queue_num = priv->caps.max_num_virtio_queues;
72 mlx5_vdpa_get_vdpa_features(int did, uint64_t *features)
74 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
77 DRV_LOG(ERR, "Invalid device id: %d.", did);
80 *features = MLX5_VDPA_DEFAULT_FEATURES;
81 if (priv->caps.virtio_queue_type & (1 << MLX5_VIRTQ_TYPE_PACKED))
82 *features |= (1ULL << VIRTIO_F_RING_PACKED);
83 if (priv->caps.tso_ipv4)
84 *features |= (1ULL << VIRTIO_NET_F_HOST_TSO4);
85 if (priv->caps.tso_ipv6)
86 *features |= (1ULL << VIRTIO_NET_F_HOST_TSO6);
87 if (priv->caps.tx_csum)
88 *features |= (1ULL << VIRTIO_NET_F_CSUM);
89 if (priv->caps.rx_csum)
90 *features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
91 if (priv->caps.virtio_version_1_0)
92 *features |= (1ULL << VIRTIO_F_VERSION_1);
97 mlx5_vdpa_get_protocol_features(int did, uint64_t *features)
99 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
102 DRV_LOG(ERR, "Invalid device id: %d.", did);
105 *features = MLX5_VDPA_PROTOCOL_FEATURES;
110 mlx5_vdpa_set_vring_state(int vid, int vring, int state)
112 int did = rte_vhost_get_vdpa_device_id(vid);
113 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
114 struct mlx5_vdpa_virtq *virtq = NULL;
117 DRV_LOG(ERR, "Invalid device id: %d.", did);
120 SLIST_FOREACH(virtq, &priv->virtq_list, next)
121 if (virtq->index == vring)
124 DRV_LOG(ERR, "Invalid or unconfigured vring id: %d.", vring);
127 return mlx5_vdpa_virtq_enable(virtq, state);
130 static struct rte_vdpa_dev_ops mlx5_vdpa_ops = {
131 .get_queue_num = mlx5_vdpa_get_queue_num,
132 .get_features = mlx5_vdpa_get_vdpa_features,
133 .get_protocol_features = mlx5_vdpa_get_protocol_features,
136 .set_vring_state = mlx5_vdpa_set_vring_state,
137 .set_features = NULL,
138 .migration_done = NULL,
139 .get_vfio_group_fd = NULL,
140 .get_vfio_device_fd = NULL,
141 .get_notify_area = NULL,
145 * DPDK callback to register a PCI device.
147 * This function spawns vdpa device out of a given PCI device.
150 * PCI driver structure (mlx5_vpda_driver).
152 * PCI device information.
155 * 0 on success, 1 to skip this driver, a negative errno value otherwise
156 * and rte_errno is set.
159 mlx5_vdpa_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
160 struct rte_pci_device *pci_dev __rte_unused)
162 struct ibv_device **ibv_list;
163 struct ibv_device *ibv_match = NULL;
164 struct mlx5_vdpa_priv *priv = NULL;
165 struct ibv_context *ctx = NULL;
166 struct mlx5_hca_attr attr;
169 if (mlx5_class_get(pci_dev->device.devargs) != MLX5_CLASS_VDPA) {
170 DRV_LOG(DEBUG, "Skip probing - should be probed by other mlx5"
175 ibv_list = mlx5_glue->get_device_list(&ret);
178 DRV_LOG(ERR, "Failed to get device list, is ib_uverbs loaded?");
182 struct rte_pci_addr pci_addr;
184 DRV_LOG(DEBUG, "Checking device \"%s\"..", ibv_list[ret]->name);
185 if (mlx5_dev_to_pci_addr(ibv_list[ret]->ibdev_path, &pci_addr))
187 if (pci_dev->addr.domain != pci_addr.domain ||
188 pci_dev->addr.bus != pci_addr.bus ||
189 pci_dev->addr.devid != pci_addr.devid ||
190 pci_dev->addr.function != pci_addr.function)
192 DRV_LOG(INFO, "PCI information matches for device \"%s\".",
193 ibv_list[ret]->name);
194 ibv_match = ibv_list[ret];
197 mlx5_glue->free_device_list(ibv_list);
199 DRV_LOG(ERR, "No matching IB device for PCI slot "
200 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 ".",
201 pci_dev->addr.domain, pci_dev->addr.bus,
202 pci_dev->addr.devid, pci_dev->addr.function);
206 ctx = mlx5_glue->dv_open_device(ibv_match);
208 DRV_LOG(ERR, "Failed to open IB device \"%s\".",
213 priv = rte_zmalloc("mlx5 vDPA device private", sizeof(*priv),
214 RTE_CACHE_LINE_SIZE);
216 DRV_LOG(ERR, "Failed to allocate private memory.");
220 ret = mlx5_devx_cmd_query_hca_attr(ctx, &attr);
222 DRV_LOG(ERR, "Unable to read HCA capabilities.");
226 if (!attr.vdpa.valid || !attr.vdpa.max_num_virtio_queues) {
227 DRV_LOG(ERR, "Not enough capabilities to support vdpa,"
228 " maybe old FW/OFED version?");
232 priv->caps = attr.vdpa;
233 priv->log_max_rqt_size = attr.log_max_rqt_size;
236 priv->dev_addr.pci_addr = pci_dev->addr;
237 priv->dev_addr.type = PCI_ADDR;
238 priv->id = rte_vdpa_register_device(&priv->dev_addr, &mlx5_vdpa_ops);
240 DRV_LOG(ERR, "Failed to register vDPA device.");
241 rte_errno = rte_errno ? rte_errno : EINVAL;
244 SLIST_INIT(&priv->mr_list);
245 SLIST_INIT(&priv->virtq_list);
246 pthread_mutex_lock(&priv_list_lock);
247 TAILQ_INSERT_TAIL(&priv_list, priv, next);
248 pthread_mutex_unlock(&priv_list_lock);
255 mlx5_glue->close_device(ctx);
260 * DPDK callback to remove a PCI device.
262 * This function removes all vDPA devices belong to a given PCI device.
265 * Pointer to the PCI device.
268 * 0 on success, the function cannot fail.
271 mlx5_vdpa_pci_remove(struct rte_pci_device *pci_dev)
273 struct mlx5_vdpa_priv *priv = NULL;
276 pthread_mutex_lock(&priv_list_lock);
277 TAILQ_FOREACH(priv, &priv_list, next) {
278 if (memcmp(&priv->dev_addr.pci_addr, &pci_dev->addr,
279 sizeof(pci_dev->addr)) == 0) {
285 TAILQ_REMOVE(&priv_list, priv, next);
286 mlx5_glue->close_device(priv->ctx);
289 pthread_mutex_unlock(&priv_list_lock);
293 static const struct rte_pci_id mlx5_vdpa_pci_id_map[] = {
295 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
296 PCI_DEVICE_ID_MELLANOX_CONNECTX5BF)
299 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
300 PCI_DEVICE_ID_MELLANOX_CONNECTX5BFVF)
303 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
304 PCI_DEVICE_ID_MELLANOX_CONNECTX6)
307 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
308 PCI_DEVICE_ID_MELLANOX_CONNECTX6VF)
311 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
312 PCI_DEVICE_ID_MELLANOX_CONNECTX6DX)
315 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
316 PCI_DEVICE_ID_MELLANOX_CONNECTX6DXVF)
323 static struct rte_pci_driver mlx5_vdpa_driver = {
327 .id_table = mlx5_vdpa_pci_id_map,
328 .probe = mlx5_vdpa_pci_probe,
329 .remove = mlx5_vdpa_pci_remove,
334 * Driver initialization routine.
336 RTE_INIT(rte_mlx5_vdpa_init)
338 /* Initialize common log type. */
339 mlx5_vdpa_logtype = rte_log_register("pmd.vdpa.mlx5");
340 if (mlx5_vdpa_logtype >= 0)
341 rte_log_set_level(mlx5_vdpa_logtype, RTE_LOG_NOTICE);
343 rte_pci_register(&mlx5_vdpa_driver);
346 RTE_PMD_EXPORT_NAME(net_mlx5_vdpa, __COUNTER__);
347 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5_vdpa, mlx5_vdpa_pci_id_map);
348 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5_vdpa, "* ib_uverbs & mlx5_core & mlx5_ib");