vdpa/mlx5: support queues number operation
[dpdk.git] / drivers / vdpa / mlx5 / mlx5_vdpa.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2019 Mellanox Technologies, Ltd
3  */
4 #include <rte_malloc.h>
5 #include <rte_log.h>
6 #include <rte_errno.h>
7 #include <rte_bus_pci.h>
8 #ifdef PEDANTIC
9 #pragma GCC diagnostic ignored "-Wpedantic"
10 #endif
11 #include <rte_vdpa.h>
12 #ifdef PEDANTIC
13 #pragma GCC diagnostic error "-Wpedantic"
14 #endif
15
16 #include <mlx5_glue.h>
17 #include <mlx5_common.h>
18 #include <mlx5_devx_cmds.h>
19
20 #include "mlx5_vdpa_utils.h"
21
22
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;
29 };
30
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;
35
36 static struct mlx5_vdpa_priv *
37 mlx5_vdpa_find_priv_resource_by_did(int did)
38 {
39         struct mlx5_vdpa_priv *priv;
40         int found = 0;
41
42         pthread_mutex_lock(&priv_list_lock);
43         TAILQ_FOREACH(priv, &priv_list, next) {
44                 if (did == priv->id) {
45                         found = 1;
46                         break;
47                 }
48         }
49         pthread_mutex_unlock(&priv_list_lock);
50         if (!found) {
51                 DRV_LOG(ERR, "Invalid device id: %d.", did);
52                 rte_errno = EINVAL;
53                 return NULL;
54         }
55         return priv;
56 }
57
58 static int
59 mlx5_vdpa_get_queue_num(int did, uint32_t *queue_num)
60 {
61         struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
62
63         if (priv == NULL) {
64                 DRV_LOG(ERR, "Invalid device id: %d.", did);
65                 return -1;
66         }
67         *queue_num = priv->caps.max_num_virtio_queues;
68         return 0;
69 }
70
71 static struct rte_vdpa_dev_ops mlx5_vdpa_ops = {
72         .get_queue_num = mlx5_vdpa_get_queue_num,
73         .get_features = NULL,
74         .get_protocol_features = NULL,
75         .dev_conf = NULL,
76         .dev_close = NULL,
77         .set_vring_state = NULL,
78         .set_features = NULL,
79         .migration_done = NULL,
80         .get_vfio_group_fd = NULL,
81         .get_vfio_device_fd = NULL,
82         .get_notify_area = NULL,
83 };
84
85 /**
86  * DPDK callback to register a PCI device.
87  *
88  * This function spawns vdpa device out of a given PCI device.
89  *
90  * @param[in] pci_drv
91  *   PCI driver structure (mlx5_vpda_driver).
92  * @param[in] pci_dev
93  *   PCI device information.
94  *
95  * @return
96  *   0 on success, 1 to skip this driver, a negative errno value otherwise
97  *   and rte_errno is set.
98  */
99 static int
100 mlx5_vdpa_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
101                     struct rte_pci_device *pci_dev __rte_unused)
102 {
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;
108         int ret;
109
110         if (mlx5_class_get(pci_dev->device.devargs) != MLX5_CLASS_VDPA) {
111                 DRV_LOG(DEBUG, "Skip probing - should be probed by other mlx5"
112                         " driver.");
113                 return 1;
114         }
115         errno = 0;
116         ibv_list = mlx5_glue->get_device_list(&ret);
117         if (!ibv_list) {
118                 rte_errno = ENOSYS;
119                 DRV_LOG(ERR, "Failed to get device list, is ib_uverbs loaded?");
120                 return -rte_errno;
121         }
122         while (ret-- > 0) {
123                 struct rte_pci_addr pci_addr;
124
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))
127                         continue;
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)
132                         continue;
133                 DRV_LOG(INFO, "PCI information matches for device \"%s\".",
134                         ibv_list[ret]->name);
135                 ibv_match = ibv_list[ret];
136                 break;
137         }
138         mlx5_glue->free_device_list(ibv_list);
139         if (!ibv_match) {
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);
144                 rte_errno = ENOENT;
145                 return -rte_errno;
146         }
147         ctx = mlx5_glue->dv_open_device(ibv_match);
148         if (!ctx) {
149                 DRV_LOG(ERR, "Failed to open IB device \"%s\".",
150                         ibv_match->name);
151                 rte_errno = ENODEV;
152                 return -rte_errno;
153         }
154         priv = rte_zmalloc("mlx5 vDPA device private", sizeof(*priv),
155                            RTE_CACHE_LINE_SIZE);
156         if (!priv) {
157                 DRV_LOG(ERR, "Failed to allocate private memory.");
158                 rte_errno = ENOMEM;
159                 goto error;
160         }
161         ret = mlx5_devx_cmd_query_hca_attr(ctx, &attr);
162         if (ret) {
163                 DRV_LOG(ERR, "Unable to read HCA capabilities.");
164                 rte_errno = ENOTSUP;
165                 goto error;
166         } else {
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?");
170                         rte_errno = ENOTSUP;
171                         goto error;
172                 }
173                 priv->caps = attr.vdpa;
174         }
175         priv->ctx = ctx;
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);
179         if (priv->id < 0) {
180                 DRV_LOG(ERR, "Failed to register vDPA device.");
181                 rte_errno = rte_errno ? rte_errno : EINVAL;
182                 goto error;
183         }
184         pthread_mutex_lock(&priv_list_lock);
185         TAILQ_INSERT_TAIL(&priv_list, priv, next);
186         pthread_mutex_unlock(&priv_list_lock);
187         return 0;
188
189 error:
190         if (priv)
191                 rte_free(priv);
192         if (ctx)
193                 mlx5_glue->close_device(ctx);
194         return -rte_errno;
195 }
196
197 /**
198  * DPDK callback to remove a PCI device.
199  *
200  * This function removes all vDPA devices belong to a given PCI device.
201  *
202  * @param[in] pci_dev
203  *   Pointer to the PCI device.
204  *
205  * @return
206  *   0 on success, the function cannot fail.
207  */
208 static int
209 mlx5_vdpa_pci_remove(struct rte_pci_device *pci_dev)
210 {
211         struct mlx5_vdpa_priv *priv = NULL;
212         int found = 0;
213
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) {
218                         found = 1;
219                         break;
220                 }
221         }
222         if (found) {
223                 TAILQ_REMOVE(&priv_list, priv, next);
224                 mlx5_glue->close_device(priv->ctx);
225                 rte_free(priv);
226         }
227         pthread_mutex_unlock(&priv_list_lock);
228         return 0;
229 }
230
231 static const struct rte_pci_id mlx5_vdpa_pci_id_map[] = {
232         {
233                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
234                                PCI_DEVICE_ID_MELLANOX_CONNECTX5BF)
235         },
236         {
237                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
238                                PCI_DEVICE_ID_MELLANOX_CONNECTX5BFVF)
239         },
240         {
241                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
242                                 PCI_DEVICE_ID_MELLANOX_CONNECTX6)
243         },
244         {
245                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
246                                 PCI_DEVICE_ID_MELLANOX_CONNECTX6VF)
247         },
248         {
249                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
250                                 PCI_DEVICE_ID_MELLANOX_CONNECTX6DX)
251         },
252         {
253                 RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
254                                 PCI_DEVICE_ID_MELLANOX_CONNECTX6DXVF)
255         },
256         {
257                 .vendor_id = 0
258         }
259 };
260
261 static struct rte_pci_driver mlx5_vdpa_driver = {
262         .driver = {
263                 .name = "mlx5_vdpa",
264         },
265         .id_table = mlx5_vdpa_pci_id_map,
266         .probe = mlx5_vdpa_pci_probe,
267         .remove = mlx5_vdpa_pci_remove,
268         .drv_flags = 0,
269 };
270
271 /**
272  * Driver initialization routine.
273  */
274 RTE_INIT(rte_mlx5_vdpa_init)
275 {
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);
280         if (mlx5_glue)
281                 rte_pci_register(&mlx5_vdpa_driver);
282 }
283
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");