Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2018 Intel Corporation
3 : : */
4 : :
5 : : /**
6 : : * @file
7 : : *
8 : : * Device specific vhost lib
9 : : */
10 : :
11 : : #include <sys/queue.h>
12 : :
13 : : #include <dev_driver.h>
14 : : #include <eal_export.h>
15 : : #include <rte_class.h>
16 : : #include <rte_malloc.h>
17 : : #include <rte_spinlock.h>
18 : : #include <rte_tailq.h>
19 : :
20 : : #include "rte_vdpa.h"
21 : : #include "vdpa_driver.h"
22 : : #include "vhost.h"
23 : : #include "iotlb.h"
24 : :
25 : : /** Double linked list of vDPA devices. */
26 : : TAILQ_HEAD(vdpa_device_list, rte_vdpa_device);
27 : :
28 : : static struct vdpa_device_list vdpa_device_list__ =
29 : : TAILQ_HEAD_INITIALIZER(vdpa_device_list__);
30 : : static rte_spinlock_t vdpa_device_list_lock = RTE_SPINLOCK_INITIALIZER;
31 : : static struct vdpa_device_list * const vdpa_device_list
32 : : __rte_guarded_by(&vdpa_device_list_lock) = &vdpa_device_list__;
33 : :
34 : : static struct rte_vdpa_device *
35 : 0 : __vdpa_find_device_by_name(const char *name)
36 : : __rte_requires_capability(&vdpa_device_list_lock)
37 : : {
38 : : struct rte_vdpa_device *dev, *ret = NULL;
39 : :
40 [ # # ]: 0 : if (name == NULL)
41 : : return NULL;
42 : :
43 [ # # ]: 0 : TAILQ_FOREACH(dev, vdpa_device_list, next) {
44 [ # # ]: 0 : if (!strncmp(dev->device->name, name, RTE_DEV_NAME_MAX_LEN)) {
45 : : ret = dev;
46 : : break;
47 : : }
48 : : }
49 : :
50 : : return ret;
51 : : }
52 : :
53 : : RTE_EXPORT_SYMBOL(rte_vdpa_find_device_by_name)
54 : : struct rte_vdpa_device *
55 : 0 : rte_vdpa_find_device_by_name(const char *name)
56 : : {
57 : : struct rte_vdpa_device *dev;
58 : :
59 : : rte_spinlock_lock(&vdpa_device_list_lock);
60 : 0 : dev = __vdpa_find_device_by_name(name);
61 : : rte_spinlock_unlock(&vdpa_device_list_lock);
62 : :
63 : 0 : return dev;
64 : : }
65 : :
66 : : RTE_EXPORT_SYMBOL(rte_vdpa_get_rte_device)
67 : : struct rte_device *
68 : 0 : rte_vdpa_get_rte_device(struct rte_vdpa_device *vdpa_dev)
69 : : {
70 [ # # ]: 0 : if (vdpa_dev == NULL)
71 : : return NULL;
72 : :
73 : 0 : return vdpa_dev->device;
74 : : }
75 : :
76 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_vdpa_register_device)
77 : : struct rte_vdpa_device *
78 : 0 : rte_vdpa_register_device(struct rte_device *rte_dev,
79 : : struct rte_vdpa_dev_ops *ops)
80 : : {
81 : : struct rte_vdpa_device *dev;
82 : : int ret = 0;
83 : :
84 [ # # ]: 0 : if (ops == NULL)
85 : : return NULL;
86 : :
87 : : /* Check mandatory ops are implemented */
88 [ # # # # ]: 0 : if (!ops->get_queue_num || !ops->get_features ||
89 [ # # # # ]: 0 : !ops->get_protocol_features || !ops->dev_conf ||
90 [ # # # # ]: 0 : !ops->dev_close || !ops->set_vring_state ||
91 [ # # ]: 0 : !ops->set_features) {
92 : 0 : VHOST_CONFIG_LOG(rte_dev->name, ERR,
93 : : "Some mandatory vDPA ops aren't implemented");
94 : 0 : return NULL;
95 : : }
96 : :
97 : : rte_spinlock_lock(&vdpa_device_list_lock);
98 : : /* Check the device hasn't been register already */
99 : 0 : dev = __vdpa_find_device_by_name(rte_dev->name);
100 [ # # ]: 0 : if (dev) {
101 : : dev = NULL;
102 : 0 : goto out_unlock;
103 : : }
104 : :
105 : 0 : dev = rte_zmalloc(NULL, sizeof(*dev), 0);
106 [ # # ]: 0 : if (!dev)
107 : 0 : goto out_unlock;
108 : :
109 : 0 : dev->device = rte_dev;
110 : 0 : dev->ops = ops;
111 : :
112 [ # # ]: 0 : if (ops->get_dev_type) {
113 : 0 : ret = ops->get_dev_type(dev, &dev->type);
114 [ # # ]: 0 : if (ret) {
115 : 0 : VHOST_CONFIG_LOG(rte_dev->name, ERR,
116 : : "Failed to get vdpa dev type.");
117 : : ret = -1;
118 : 0 : goto out_unlock;
119 : : }
120 : : } else {
121 : : /** by default, we assume vdpa device is a net device */
122 : 0 : dev->type = RTE_VHOST_VDPA_DEVICE_TYPE_NET;
123 : : }
124 : :
125 : 0 : TAILQ_INSERT_TAIL(vdpa_device_list, dev, next);
126 : 0 : out_unlock:
127 : : rte_spinlock_unlock(&vdpa_device_list_lock);
128 : :
129 : 0 : return dev;
130 : : }
131 : :
132 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_vdpa_unregister_device)
133 : : int
134 : 0 : rte_vdpa_unregister_device(struct rte_vdpa_device *dev)
135 : : {
136 : : struct rte_vdpa_device *cur_dev, *tmp_dev;
137 : : int ret = -1;
138 : :
139 : : rte_spinlock_lock(&vdpa_device_list_lock);
140 [ # # ]: 0 : RTE_TAILQ_FOREACH_SAFE(cur_dev, vdpa_device_list, next, tmp_dev) {
141 [ # # ]: 0 : if (dev != cur_dev)
142 : : continue;
143 : :
144 [ # # ]: 0 : TAILQ_REMOVE(vdpa_device_list, dev, next);
145 : 0 : rte_free(dev);
146 : : ret = 0;
147 : 0 : break;
148 : : }
149 : : rte_spinlock_unlock(&vdpa_device_list_lock);
150 : :
151 : 0 : return ret;
152 : : }
153 : :
154 : : RTE_EXPORT_INTERNAL_SYMBOL(rte_vdpa_relay_vring_used)
155 : : int
156 [ # # ]: 0 : rte_vdpa_relay_vring_used(int vid, uint16_t qid, void *vring_m)
157 : : {
158 : : struct virtio_net *dev = get_device(vid);
159 : : uint16_t idx, idx_m, desc_id;
160 : : struct vhost_virtqueue *vq;
161 : : struct vring_desc desc;
162 : : struct vring_desc *desc_ring;
163 : : struct vring_desc *idesc = NULL;
164 : : struct vring *s_vring;
165 : : uint64_t dlen;
166 : : uint32_t nr_descs;
167 : : int ret;
168 : :
169 [ # # ]: 0 : if (!dev || !vring_m)
170 : : return -1;
171 : :
172 [ # # ]: 0 : if (qid >= dev->nr_vring)
173 : : return -1;
174 : :
175 [ # # ]: 0 : if (vq_is_packed(dev))
176 : : return -1;
177 : :
178 : : s_vring = (struct vring *)vring_m;
179 : 0 : vq = dev->virtqueue[qid];
180 : 0 : idx = vq->used->idx;
181 : 0 : idx_m = s_vring->used->idx;
182 : 0 : ret = (uint16_t)(idx_m - idx);
183 : 0 : vq->used->flags = s_vring->used->flags;
184 : :
185 [ # # ]: 0 : while (idx != idx_m) {
186 : : /* copy used entry, used ring logging is not covered here */
187 : 0 : vq->used->ring[idx & (vq->size - 1)] =
188 : 0 : s_vring->used->ring[idx & (vq->size - 1)];
189 : :
190 : 0 : desc_id = vq->used->ring[idx & (vq->size - 1)].id;
191 : 0 : desc_ring = vq->desc;
192 : 0 : nr_descs = vq->size;
193 : :
194 [ # # ]: 0 : if (unlikely(desc_id >= vq->size))
195 : : return -1;
196 : :
197 [ # # ]: 0 : if (vq->desc[desc_id].flags & VRING_DESC_F_INDIRECT) {
198 : 0 : uint64_t desc_len = vq->desc[desc_id].len;
199 : :
200 : 0 : dlen = desc_len;
201 : 0 : nr_descs = dlen / sizeof(struct vring_desc);
202 [ # # ]: 0 : if (unlikely(nr_descs > vq->size))
203 : : return -1;
204 : :
205 : : vhost_user_iotlb_rd_lock(vq);
206 [ # # ]: 0 : desc_ring = (struct vring_desc *)(uintptr_t)
207 : : vhost_iova_to_vva(dev, vq,
208 [ # # ]: 0 : vq->desc[desc_id].addr, &dlen,
209 : : VHOST_ACCESS_RO);
210 : : vhost_user_iotlb_rd_unlock(vq);
211 [ # # ]: 0 : if (unlikely(!desc_ring))
212 : : return -1;
213 : :
214 [ # # ]: 0 : if (unlikely(dlen < desc_len)) {
215 : : vhost_user_iotlb_rd_lock(vq);
216 : 0 : idesc = vhost_alloc_copy_ind_table(dev, vq,
217 : 0 : vq->desc[desc_id].addr,
218 : : desc_len);
219 : : vhost_user_iotlb_rd_unlock(vq);
220 [ # # ]: 0 : if (unlikely(!idesc))
221 : : return -1;
222 : :
223 : : desc_ring = idesc;
224 : : }
225 : :
226 : : desc_id = 0;
227 : : }
228 : :
229 : : /* dirty page logging for DMA writeable buffer */
230 : : do {
231 [ # # ]: 0 : if (unlikely(desc_id >= vq->size))
232 : 0 : goto fail;
233 [ # # ]: 0 : if (unlikely(nr_descs-- == 0))
234 : 0 : goto fail;
235 : 0 : desc = desc_ring[desc_id];
236 [ # # ]: 0 : if (desc.flags & VRING_DESC_F_WRITE) {
237 : : vhost_user_iotlb_rd_lock(vq);
238 [ # # ]: 0 : vhost_log_write_iova(dev, vq, desc.addr,
239 : : desc.len);
240 : : vhost_user_iotlb_rd_unlock(vq);
241 : : }
242 : : desc_id = desc.next;
243 [ # # ]: 0 : } while (desc.flags & VRING_DESC_F_NEXT);
244 : :
245 [ # # ]: 0 : if (unlikely(idesc)) {
246 : : free_ind_table(idesc);
247 : : idesc = NULL;
248 : : }
249 : :
250 : 0 : idx++;
251 : : }
252 : :
253 : : /* used idx is the synchronization point for the split vring */
254 : 0 : rte_atomic_store_explicit((unsigned short __rte_atomic *)&vq->used->idx,
255 : : idx_m, rte_memory_order_release);
256 : :
257 [ # # ]: 0 : if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
258 : 0 : vring_used_event(s_vring) = idx_m;
259 : :
260 : : return ret;
261 : :
262 : 0 : fail:
263 [ # # ]: 0 : if (unlikely(idesc))
264 : : free_ind_table(idesc);
265 : : return -1;
266 : : }
267 : :
268 : : RTE_EXPORT_SYMBOL(rte_vdpa_get_queue_num)
269 : : int
270 : 0 : rte_vdpa_get_queue_num(struct rte_vdpa_device *dev, uint32_t *queue_num)
271 : : {
272 [ # # # # : 0 : if (dev == NULL || dev->ops == NULL || dev->ops->get_queue_num == NULL)
# # ]
273 : : return -1;
274 : :
275 : 0 : return dev->ops->get_queue_num(dev, queue_num);
276 : : }
277 : :
278 : : RTE_EXPORT_SYMBOL(rte_vdpa_get_features)
279 : : int
280 : 0 : rte_vdpa_get_features(struct rte_vdpa_device *dev, uint64_t *features)
281 : : {
282 [ # # # # : 0 : if (dev == NULL || dev->ops == NULL || dev->ops->get_features == NULL)
# # ]
283 : : return -1;
284 : :
285 : 0 : return dev->ops->get_features(dev, features);
286 : : }
287 : :
288 : : RTE_EXPORT_SYMBOL(rte_vdpa_get_protocol_features)
289 : : int
290 : 0 : rte_vdpa_get_protocol_features(struct rte_vdpa_device *dev, uint64_t *features)
291 : : {
292 [ # # # # ]: 0 : if (dev == NULL || dev->ops == NULL ||
293 [ # # ]: 0 : dev->ops->get_protocol_features == NULL)
294 : : return -1;
295 : :
296 : 0 : return dev->ops->get_protocol_features(dev, features);
297 : : }
298 : :
299 : : RTE_EXPORT_SYMBOL(rte_vdpa_get_stats_names)
300 : : int
301 : 0 : rte_vdpa_get_stats_names(struct rte_vdpa_device *dev,
302 : : struct rte_vdpa_stat_name *stats_names,
303 : : unsigned int size)
304 : : {
305 [ # # ]: 0 : if (!dev)
306 : : return -EINVAL;
307 : :
308 [ # # ]: 0 : if (dev->ops->get_stats_names == NULL)
309 : : return -ENOTSUP;
310 : :
311 : 0 : return dev->ops->get_stats_names(dev, stats_names, size);
312 : : }
313 : :
314 : : RTE_EXPORT_SYMBOL(rte_vdpa_get_stats)
315 : : int
316 : 0 : rte_vdpa_get_stats(struct rte_vdpa_device *dev, uint16_t qid,
317 : : struct rte_vdpa_stat *stats, unsigned int n)
318 : : {
319 [ # # # # ]: 0 : if (!dev || !stats || !n)
320 : : return -EINVAL;
321 : :
322 [ # # ]: 0 : if (dev->ops->get_stats == NULL)
323 : : return -ENOTSUP;
324 : :
325 : 0 : return dev->ops->get_stats(dev, qid, stats, n);
326 : : }
327 : :
328 : : RTE_EXPORT_SYMBOL(rte_vdpa_reset_stats)
329 : : int
330 : 0 : rte_vdpa_reset_stats(struct rte_vdpa_device *dev, uint16_t qid)
331 : : {
332 [ # # ]: 0 : if (!dev)
333 : : return -EINVAL;
334 : :
335 [ # # ]: 0 : if (dev->ops->reset_stats == NULL)
336 : : return -ENOTSUP;
337 : :
338 : 0 : return dev->ops->reset_stats(dev, qid);
339 : : }
340 : :
341 : : static int
342 : 0 : vdpa_dev_match(struct rte_vdpa_device *dev,
343 : : const struct rte_device *rte_dev)
344 : : {
345 [ # # ]: 0 : if (dev->device == rte_dev)
346 : 0 : return 0;
347 : :
348 : : return -1;
349 : : }
350 : :
351 : : /* Generic rte_vdpa_dev comparison function. */
352 : : typedef int (*rte_vdpa_cmp_t)(struct rte_vdpa_device *,
353 : : const struct rte_device *rte_dev);
354 : :
355 : : static struct rte_vdpa_device *
356 : 0 : vdpa_find_device(const struct rte_vdpa_device *start, rte_vdpa_cmp_t cmp,
357 : : struct rte_device *rte_dev)
358 : : {
359 : : struct rte_vdpa_device *dev;
360 : :
361 : : rte_spinlock_lock(&vdpa_device_list_lock);
362 [ # # ]: 0 : if (start == NULL)
363 : 0 : dev = TAILQ_FIRST(vdpa_device_list);
364 : : else
365 : 0 : dev = TAILQ_NEXT(start, next);
366 : :
367 [ # # ]: 0 : while (dev != NULL) {
368 [ # # ]: 0 : if (cmp(dev, rte_dev) == 0)
369 : : break;
370 : :
371 : 0 : dev = TAILQ_NEXT(dev, next);
372 : : }
373 : : rte_spinlock_unlock(&vdpa_device_list_lock);
374 : :
375 : 0 : return dev;
376 : : }
377 : :
378 : : static void *
379 : 0 : vdpa_dev_iterate(const void *start,
380 : : const char *str,
381 : : const struct rte_dev_iterator *it)
382 : : {
383 : : struct rte_vdpa_device *vdpa_dev = NULL;
384 : :
385 : : RTE_SET_USED(str);
386 : :
387 : 0 : vdpa_dev = vdpa_find_device(start, vdpa_dev_match, it->device);
388 : :
389 : 0 : return vdpa_dev;
390 : : }
391 : :
392 : : static struct rte_class rte_class_vdpa = {
393 : : .dev_iterate = vdpa_dev_iterate,
394 : : };
395 : :
396 : 305 : RTE_REGISTER_CLASS(vdpa, rte_class_vdpa);
|