1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2019 Mellanox Technologies, Ltd
4 #include <linux/virtio_net.h>
6 #include <rte_malloc.h>
9 #include <rte_bus_pci.h>
11 #include <mlx5_glue.h>
12 #include <mlx5_common.h>
13 #include <mlx5_devx_cmds.h>
16 #include "mlx5_vdpa_utils.h"
17 #include "mlx5_vdpa.h"
20 #ifndef VIRTIO_F_ORDER_PLATFORM
21 #define VIRTIO_F_ORDER_PLATFORM 36
24 #ifndef VIRTIO_F_RING_PACKED
25 #define VIRTIO_F_RING_PACKED 34
28 #define MLX5_VDPA_DEFAULT_FEATURES ((1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
29 (1ULL << VIRTIO_F_ANY_LAYOUT) | \
30 (1ULL << VIRTIO_NET_F_MQ) | \
31 (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
32 (1ULL << VIRTIO_F_ORDER_PLATFORM))
34 #define MLX5_VDPA_PROTOCOL_FEATURES \
35 ((1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ) | \
36 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD) | \
37 (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER) | \
38 (1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD) | \
39 (1ULL << VHOST_USER_PROTOCOL_F_MQ))
41 TAILQ_HEAD(mlx5_vdpa_privs, mlx5_vdpa_priv) priv_list =
42 TAILQ_HEAD_INITIALIZER(priv_list);
43 static pthread_mutex_t priv_list_lock = PTHREAD_MUTEX_INITIALIZER;
44 int mlx5_vdpa_logtype;
46 static struct mlx5_vdpa_priv *
47 mlx5_vdpa_find_priv_resource_by_did(int did)
49 struct mlx5_vdpa_priv *priv;
52 pthread_mutex_lock(&priv_list_lock);
53 TAILQ_FOREACH(priv, &priv_list, next) {
54 if (did == priv->id) {
59 pthread_mutex_unlock(&priv_list_lock);
61 DRV_LOG(ERR, "Invalid device id: %d.", did);
69 mlx5_vdpa_get_queue_num(int did, uint32_t *queue_num)
71 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
74 DRV_LOG(ERR, "Invalid device id: %d.", did);
77 *queue_num = priv->caps.max_num_virtio_queues;
82 mlx5_vdpa_get_vdpa_features(int did, uint64_t *features)
84 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
87 DRV_LOG(ERR, "Invalid device id: %d.", did);
90 *features = MLX5_VDPA_DEFAULT_FEATURES;
91 if (priv->caps.virtio_queue_type & (1 << MLX5_VIRTQ_TYPE_PACKED))
92 *features |= (1ULL << VIRTIO_F_RING_PACKED);
93 if (priv->caps.tso_ipv4)
94 *features |= (1ULL << VIRTIO_NET_F_HOST_TSO4);
95 if (priv->caps.tso_ipv6)
96 *features |= (1ULL << VIRTIO_NET_F_HOST_TSO6);
97 if (priv->caps.tx_csum)
98 *features |= (1ULL << VIRTIO_NET_F_CSUM);
99 if (priv->caps.rx_csum)
100 *features |= (1ULL << VIRTIO_NET_F_GUEST_CSUM);
101 if (priv->caps.virtio_version_1_0)
102 *features |= (1ULL << VIRTIO_F_VERSION_1);
107 mlx5_vdpa_get_protocol_features(int did, uint64_t *features)
109 struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
112 DRV_LOG(ERR, "Invalid device id: %d.", did);
115 *features = MLX5_VDPA_PROTOCOL_FEATURES;
119 static struct rte_vdpa_dev_ops mlx5_vdpa_ops = {
120 .get_queue_num = mlx5_vdpa_get_queue_num,
121 .get_features = mlx5_vdpa_get_vdpa_features,
122 .get_protocol_features = mlx5_vdpa_get_protocol_features,
125 .set_vring_state = NULL,
126 .set_features = NULL,
127 .migration_done = NULL,
128 .get_vfio_group_fd = NULL,
129 .get_vfio_device_fd = NULL,
130 .get_notify_area = NULL,
134 * DPDK callback to register a PCI device.
136 * This function spawns vdpa device out of a given PCI device.
139 * PCI driver structure (mlx5_vpda_driver).
141 * PCI device information.
144 * 0 on success, 1 to skip this driver, a negative errno value otherwise
145 * and rte_errno is set.
148 mlx5_vdpa_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
149 struct rte_pci_device *pci_dev __rte_unused)
151 struct ibv_device **ibv_list;
152 struct ibv_device *ibv_match = NULL;
153 struct mlx5_vdpa_priv *priv = NULL;
154 struct ibv_context *ctx = NULL;
155 struct mlx5_hca_attr attr;
158 if (mlx5_class_get(pci_dev->device.devargs) != MLX5_CLASS_VDPA) {
159 DRV_LOG(DEBUG, "Skip probing - should be probed by other mlx5"
164 ibv_list = mlx5_glue->get_device_list(&ret);
167 DRV_LOG(ERR, "Failed to get device list, is ib_uverbs loaded?");
171 struct rte_pci_addr pci_addr;
173 DRV_LOG(DEBUG, "Checking device \"%s\"..", ibv_list[ret]->name);
174 if (mlx5_dev_to_pci_addr(ibv_list[ret]->ibdev_path, &pci_addr))
176 if (pci_dev->addr.domain != pci_addr.domain ||
177 pci_dev->addr.bus != pci_addr.bus ||
178 pci_dev->addr.devid != pci_addr.devid ||
179 pci_dev->addr.function != pci_addr.function)
181 DRV_LOG(INFO, "PCI information matches for device \"%s\".",
182 ibv_list[ret]->name);
183 ibv_match = ibv_list[ret];
186 mlx5_glue->free_device_list(ibv_list);
188 DRV_LOG(ERR, "No matching IB device for PCI slot "
189 "%" SCNx32 ":%" SCNx8 ":%" SCNx8 ".%" SCNx8 ".",
190 pci_dev->addr.domain, pci_dev->addr.bus,
191 pci_dev->addr.devid, pci_dev->addr.function);
195 ctx = mlx5_glue->dv_open_device(ibv_match);
197 DRV_LOG(ERR, "Failed to open IB device \"%s\".",
202 priv = rte_zmalloc("mlx5 vDPA device private", sizeof(*priv),
203 RTE_CACHE_LINE_SIZE);
205 DRV_LOG(ERR, "Failed to allocate private memory.");
209 ret = mlx5_devx_cmd_query_hca_attr(ctx, &attr);
211 DRV_LOG(ERR, "Unable to read HCA capabilities.");
215 if (!attr.vdpa.valid || !attr.vdpa.max_num_virtio_queues) {
216 DRV_LOG(ERR, "Not enough capabilities to support vdpa,"
217 " maybe old FW/OFED version?");
221 priv->caps = attr.vdpa;
224 priv->dev_addr.pci_addr = pci_dev->addr;
225 priv->dev_addr.type = PCI_ADDR;
226 priv->id = rte_vdpa_register_device(&priv->dev_addr, &mlx5_vdpa_ops);
228 DRV_LOG(ERR, "Failed to register vDPA device.");
229 rte_errno = rte_errno ? rte_errno : EINVAL;
232 SLIST_INIT(&priv->mr_list);
233 pthread_mutex_lock(&priv_list_lock);
234 TAILQ_INSERT_TAIL(&priv_list, priv, next);
235 pthread_mutex_unlock(&priv_list_lock);
242 mlx5_glue->close_device(ctx);
247 * DPDK callback to remove a PCI device.
249 * This function removes all vDPA devices belong to a given PCI device.
252 * Pointer to the PCI device.
255 * 0 on success, the function cannot fail.
258 mlx5_vdpa_pci_remove(struct rte_pci_device *pci_dev)
260 struct mlx5_vdpa_priv *priv = NULL;
263 pthread_mutex_lock(&priv_list_lock);
264 TAILQ_FOREACH(priv, &priv_list, next) {
265 if (memcmp(&priv->dev_addr.pci_addr, &pci_dev->addr,
266 sizeof(pci_dev->addr)) == 0) {
272 TAILQ_REMOVE(&priv_list, priv, next);
273 mlx5_glue->close_device(priv->ctx);
276 pthread_mutex_unlock(&priv_list_lock);
280 static const struct rte_pci_id mlx5_vdpa_pci_id_map[] = {
282 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
283 PCI_DEVICE_ID_MELLANOX_CONNECTX5BF)
286 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
287 PCI_DEVICE_ID_MELLANOX_CONNECTX5BFVF)
290 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
291 PCI_DEVICE_ID_MELLANOX_CONNECTX6)
294 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
295 PCI_DEVICE_ID_MELLANOX_CONNECTX6VF)
298 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
299 PCI_DEVICE_ID_MELLANOX_CONNECTX6DX)
302 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
303 PCI_DEVICE_ID_MELLANOX_CONNECTX6DXVF)
310 static struct rte_pci_driver mlx5_vdpa_driver = {
314 .id_table = mlx5_vdpa_pci_id_map,
315 .probe = mlx5_vdpa_pci_probe,
316 .remove = mlx5_vdpa_pci_remove,
321 * Driver initialization routine.
323 RTE_INIT(rte_mlx5_vdpa_init)
325 /* Initialize common log type. */
326 mlx5_vdpa_logtype = rte_log_register("pmd.vdpa.mlx5");
327 if (mlx5_vdpa_logtype >= 0)
328 rte_log_set_level(mlx5_vdpa_logtype, RTE_LOG_NOTICE);
330 rte_pci_register(&mlx5_vdpa_driver);
333 RTE_PMD_EXPORT_NAME(net_mlx5_vdpa, __COUNTER__);
334 RTE_PMD_REGISTER_PCI_TABLE(net_mlx5_vdpa, mlx5_vdpa_pci_id_map);
335 RTE_PMD_REGISTER_KMOD_DEP(net_mlx5_vdpa, "* ib_uverbs & mlx5_core & mlx5_ib");