1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 Mellanox Technologies, Ltd
4 #include <rte_malloc.h>
7 #include <rte_bus_pci.h>
9 #pragma GCC diagnostic ignored "-Wpedantic"
13 #pragma GCC diagnostic error "-Wpedantic"
16 #include <mlx5_glue.h>
17 #include <mlx5_common.h>
18 #include <mlx5_devx_cmds.h>
20 #include "mlx5_vdpa_utils.h"
23 struct mlx5_vdpa_priv {
24 TAILQ_ENTRY(mlx5_vdpa_priv) next;
25 int id; /* vDPA device id. */
26 struct ibv_context *ctx; /* Device context. */
27 struct rte_vdpa_dev_addr dev_addr;
28 struct mlx5_hca_vdpa_attr caps;
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;
71 static struct rte_vdpa_dev_ops mlx5_vdpa_ops = {
72 .get_queue_num = mlx5_vdpa_get_queue_num,
74 .get_protocol_features = NULL,
77 .set_vring_state = NULL,
79 .migration_done = NULL,
80 .get_vfio_group_fd = NULL,
81 .get_vfio_device_fd = NULL,
82 .get_notify_area = NULL,
86 * DPDK callback to register a PCI device.
88 * This function spawns vdpa device out of a given PCI device.
91 * PCI driver structure (mlx5_vpda_driver).
93 * PCI device information.
96 * 0 on success, 1 to skip this driver, a negative errno value otherwise
97 * and rte_errno is set.
100 mlx5_vdpa_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
101 struct rte_pci_device *pci_dev __rte_unused)
103 struct ibv_device **ibv_list;
104 struct ibv_device *ibv_match = NULL;
105 struct mlx5_vdpa_priv *priv = NULL;
106 struct ibv_context *ctx = NULL;
107 struct mlx5_hca_attr attr;
110 if (mlx5_class_get(pci_dev->device.devargs) != MLX5_CLASS_VDPA) {
111 DRV_LOG(DEBUG, "Skip probing - should be probed by other mlx5"
116 ibv_list = mlx5_glue->get_device_list(&ret);
119 DRV_LOG(ERR, "Failed to get device list, is ib_uverbs loaded?");
123 struct rte_pci_addr pci_addr;
125 DRV_LOG(DEBUG, "Checking device \"%s\"..", ibv_list[ret]->name);
126 if (mlx5_dev_to_pci_addr(ibv_list[ret]->ibdev_path, &pci_addr))
128 if (pci_dev->addr.domain != pci_addr.domain ||
129 pci_dev->addr.bus != pci_addr.bus ||
130 pci_dev->addr.devid != pci_addr.devid ||
131 pci_dev->addr.function != pci_addr.function)
133 DRV_LOG(INFO, "PCI information matches for device \"%s\".",
134 ibv_list[ret]->name);
135 ibv_match = ibv_list[ret];
138 mlx5_glue->free_device_list(ibv_list);
140 DRV_LOG(ERR, "No matching IB device for PCI slot "
141 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 ".",
142 pci_dev->addr.domain, pci_dev->addr.bus,
143 pci_dev->addr.devid, pci_dev->addr.function);
147 ctx = mlx5_glue->dv_open_device(ibv_match);
149 DRV_LOG(ERR, "Failed to open IB device \"%s\".",
154 priv = rte_zmalloc("mlx5 vDPA device private", sizeof(*priv),
155 RTE_CACHE_LINE_SIZE);
157 DRV_LOG(ERR, "Failed to allocate private memory.");
161 ret = mlx5_devx_cmd_query_hca_attr(ctx, &attr);
163 DRV_LOG(ERR, "Unable to read HCA capabilities.");
167 if (!attr.vdpa.valid || !attr.vdpa.max_num_virtio_queues) {
168 DRV_LOG(ERR, "Not enough capabilities to support vdpa,"
169 " maybe old FW/OFED version?");
173 priv->caps = attr.vdpa;
176 priv->dev_addr.pci_addr = pci_dev->addr;
177 priv->dev_addr.type = PCI_ADDR;
178 priv->id = rte_vdpa_register_device(&priv->dev_addr, &mlx5_vdpa_ops);
180 DRV_LOG(ERR, "Failed to register vDPA device.");
181 rte_errno = rte_errno ? rte_errno : EINVAL;
184 pthread_mutex_lock(&priv_list_lock);
185 TAILQ_INSERT_TAIL(&priv_list, priv, next);
186 pthread_mutex_unlock(&priv_list_lock);
193 mlx5_glue->close_device(ctx);
198 * DPDK callback to remove a PCI device.
200 * This function removes all vDPA devices belong to a given PCI device.
203 * Pointer to the PCI device.
206 * 0 on success, the function cannot fail.
209 mlx5_vdpa_pci_remove(struct rte_pci_device *pci_dev)
211 struct mlx5_vdpa_priv *priv = NULL;
214 pthread_mutex_lock(&priv_list_lock);
215 TAILQ_FOREACH(priv, &priv_list, next) {
216 if (memcmp(&priv->dev_addr.pci_addr, &pci_dev->addr,
217 sizeof(pci_dev->addr)) == 0) {
223 TAILQ_REMOVE(&priv_list, priv, next);
224 mlx5_glue->close_device(priv->ctx);
227 pthread_mutex_unlock(&priv_list_lock);
231 static const struct rte_pci_id mlx5_vdpa_pci_id_map[] = {
233 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
234 PCI_DEVICE_ID_MELLANOX_CONNECTX5BF)
237 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
238 PCI_DEVICE_ID_MELLANOX_CONNECTX5BFVF)
241 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
242 PCI_DEVICE_ID_MELLANOX_CONNECTX6)
245 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
246 PCI_DEVICE_ID_MELLANOX_CONNECTX6VF)
249 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
250 PCI_DEVICE_ID_MELLANOX_CONNECTX6DX)
253 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
254 PCI_DEVICE_ID_MELLANOX_CONNECTX6DXVF)
261 static struct rte_pci_driver mlx5_vdpa_driver = {
265 .id_table = mlx5_vdpa_pci_id_map,
266 .probe = mlx5_vdpa_pci_probe,
267 .remove = mlx5_vdpa_pci_remove,
272 * Driver initialization routine.
274 RTE_INIT(rte_mlx5_vdpa_init)
276 /* Initialize common log type. */
277 mlx5_vdpa_logtype = rte_log_register("pmd.vdpa.mlx5");
278 if (mlx5_vdpa_logtype >= 0)
279 rte_log_set_level(mlx5_vdpa_logtype, RTE_LOG_NOTICE);
281 rte_pci_register(&mlx5_vdpa_driver);
284 RTE_PMD_EXPORT_NAME(net_mlx5_vdpa, __COUNTER__);
285 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5_vdpa, mlx5_vdpa_pci_id_map);
286 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5_vdpa, "* ib_uverbs & mlx5_core & mlx5_ib");