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>
19 #include "mlx5_vdpa_utils.h"
22 struct mlx5_vdpa_priv {
23 TAILQ_ENTRY(mlx5_vdpa_priv) next;
24 int id; /* vDPA device id. */
25 struct ibv_context *ctx; /* Device context. */
26 struct rte_vdpa_dev_addr dev_addr;
29 TAILQ_HEAD(mlx5_vdpa_privs, mlx5_vdpa_priv) priv_list =
30 TAILQ_HEAD_INITIALIZER(priv_list);
31 static pthread_mutex_t priv_list_lock = PTHREAD_MUTEX_INITIALIZER;
32 int mlx5_vdpa_logtype;
34 static struct rte_vdpa_dev_ops mlx5_vdpa_ops = {
35 .get_queue_num = NULL,
37 .get_protocol_features = NULL,
40 .set_vring_state = NULL,
42 .migration_done = NULL,
43 .get_vfio_group_fd = NULL,
44 .get_vfio_device_fd = NULL,
45 .get_notify_area = NULL,
49 * DPDK callback to register a PCI device.
51 * This function spawns vdpa device out of a given PCI device.
54 * PCI driver structure (mlx5_vpda_driver).
56 * PCI device information.
59 * 0 on success, 1 to skip this driver, a negative errno value otherwise
60 * and rte_errno is set.
63 mlx5_vdpa_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
64 struct rte_pci_device *pci_dev __rte_unused)
66 struct ibv_device **ibv_list;
67 struct ibv_device *ibv_match = NULL;
68 struct mlx5_vdpa_priv *priv = NULL;
69 struct ibv_context *ctx = NULL;
72 if (mlx5_class_get(pci_dev->device.devargs) != MLX5_CLASS_VDPA) {
73 DRV_LOG(DEBUG, "Skip probing - should be probed by other mlx5"
78 ibv_list = mlx5_glue->get_device_list(&ret);
81 DRV_LOG(ERR, "Failed to get device list, is ib_uverbs loaded?");
85 struct rte_pci_addr pci_addr;
87 DRV_LOG(DEBUG, "Checking device \"%s\"..", ibv_list[ret]->name);
88 if (mlx5_dev_to_pci_addr(ibv_list[ret]->ibdev_path, &pci_addr))
90 if (pci_dev->addr.domain != pci_addr.domain ||
91 pci_dev->addr.bus != pci_addr.bus ||
92 pci_dev->addr.devid != pci_addr.devid ||
93 pci_dev->addr.function != pci_addr.function)
95 DRV_LOG(INFO, "PCI information matches for device \"%s\".",
97 ibv_match = ibv_list[ret];
100 mlx5_glue->free_device_list(ibv_list);
102 DRV_LOG(ERR, "No matching IB device for PCI slot "
103 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 ".",
104 pci_dev->addr.domain, pci_dev->addr.bus,
105 pci_dev->addr.devid, pci_dev->addr.function);
109 ctx = mlx5_glue->dv_open_device(ibv_match);
111 DRV_LOG(ERR, "Failed to open IB device \"%s\".",
116 priv = rte_zmalloc("mlx5 vDPA device private", sizeof(*priv),
117 RTE_CACHE_LINE_SIZE);
119 DRV_LOG(ERR, "Failed to allocate private memory.");
124 priv->dev_addr.pci_addr = pci_dev->addr;
125 priv->dev_addr.type = PCI_ADDR;
126 priv->id = rte_vdpa_register_device(&priv->dev_addr, &mlx5_vdpa_ops);
128 DRV_LOG(ERR, "Failed to register vDPA device.");
129 rte_errno = rte_errno ? rte_errno : EINVAL;
132 pthread_mutex_lock(&priv_list_lock);
133 TAILQ_INSERT_TAIL(&priv_list, priv, next);
134 pthread_mutex_unlock(&priv_list_lock);
141 mlx5_glue->close_device(ctx);
146 * DPDK callback to remove a PCI device.
148 * This function removes all vDPA devices belong to a given PCI device.
151 * Pointer to the PCI device.
154 * 0 on success, the function cannot fail.
157 mlx5_vdpa_pci_remove(struct rte_pci_device *pci_dev)
159 struct mlx5_vdpa_priv *priv = NULL;
162 pthread_mutex_lock(&priv_list_lock);
163 TAILQ_FOREACH(priv, &priv_list, next) {
164 if (memcmp(&priv->dev_addr.pci_addr, &pci_dev->addr,
165 sizeof(pci_dev->addr)) == 0) {
171 TAILQ_REMOVE(&priv_list, priv, next);
172 mlx5_glue->close_device(priv->ctx);
175 pthread_mutex_unlock(&priv_list_lock);
179 static const struct rte_pci_id mlx5_vdpa_pci_id_map[] = {
181 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
182 PCI_DEVICE_ID_MELLANOX_CONNECTX5BF)
185 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
186 PCI_DEVICE_ID_MELLANOX_CONNECTX5BFVF)
189 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
190 PCI_DEVICE_ID_MELLANOX_CONNECTX6)
193 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
194 PCI_DEVICE_ID_MELLANOX_CONNECTX6VF)
197 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
198 PCI_DEVICE_ID_MELLANOX_CONNECTX6DX)
201 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
202 PCI_DEVICE_ID_MELLANOX_CONNECTX6DXVF)
209 static struct rte_pci_driver mlx5_vdpa_driver = {
213 .id_table = mlx5_vdpa_pci_id_map,
214 .probe = mlx5_vdpa_pci_probe,
215 .remove = mlx5_vdpa_pci_remove,
220 * Driver initialization routine.
222 RTE_INIT(rte_mlx5_vdpa_init)
224 /* Initialize common log type. */
225 mlx5_vdpa_logtype = rte_log_register("pmd.vdpa.mlx5");
226 if (mlx5_vdpa_logtype >= 0)
227 rte_log_set_level(mlx5_vdpa_logtype, RTE_LOG_NOTICE);
229 rte_pci_register(&mlx5_vdpa_driver);
232 RTE_PMD_EXPORT_NAME(net_mlx5_vdpa, __COUNTER__);
233 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5_vdpa, mlx5_vdpa_pci_id_map);
234 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5_vdpa, "* ib_uverbs & mlx5_core & mlx5_ib");