1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
8 * Device specific vhost lib
12 #include <sys/queue.h>
14 #include <rte_class.h>
15 #include <rte_malloc.h>
16 #include <rte_spinlock.h>
17 #include <rte_tailq.h>
22 /** Double linked list of vDPA devices. */
23 TAILQ_HEAD(vdpa_device_list, rte_vdpa_device);
25 static struct vdpa_device_list vdpa_device_list =
26 TAILQ_HEAD_INITIALIZER(vdpa_device_list);
27 static rte_spinlock_t vdpa_device_list_lock = RTE_SPINLOCK_INITIALIZER;
30 /* Unsafe, needs to be called with vdpa_device_list_lock held */
31 static struct rte_vdpa_device *
32 __vdpa_find_device_by_name(const char *name)
34 struct rte_vdpa_device *dev, *ret = NULL;
39 TAILQ_FOREACH(dev, &vdpa_device_list, next) {
40 if (!strncmp(dev->device->name, name, RTE_DEV_NAME_MAX_LEN)) {
49 struct rte_vdpa_device *
50 rte_vdpa_find_device_by_name(const char *name)
52 struct rte_vdpa_device *dev;
54 rte_spinlock_lock(&vdpa_device_list_lock);
55 dev = __vdpa_find_device_by_name(name);
56 rte_spinlock_unlock(&vdpa_device_list_lock);
62 rte_vdpa_get_rte_device(struct rte_vdpa_device *vdpa_dev)
67 return vdpa_dev->device;
70 struct rte_vdpa_device *
71 rte_vdpa_register_device(struct rte_device *rte_dev,
72 struct rte_vdpa_dev_ops *ops)
74 struct rte_vdpa_device *dev;
79 rte_spinlock_lock(&vdpa_device_list_lock);
80 /* Check the device hasn't been register already */
81 dev = __vdpa_find_device_by_name(rte_dev->name);
87 dev = rte_zmalloc(NULL, sizeof(*dev), 0);
91 dev->device = rte_dev;
93 TAILQ_INSERT_TAIL(&vdpa_device_list, dev, next);
95 rte_spinlock_unlock(&vdpa_device_list_lock);
101 rte_vdpa_unregister_device(struct rte_vdpa_device *dev)
103 struct rte_vdpa_device *cur_dev, *tmp_dev;
106 rte_spinlock_lock(&vdpa_device_list_lock);
107 TAILQ_FOREACH_SAFE(cur_dev, &vdpa_device_list, next, tmp_dev) {
111 TAILQ_REMOVE(&vdpa_device_list, dev, next);
116 rte_spinlock_unlock(&vdpa_device_list_lock);
122 rte_vdpa_relay_vring_used(int vid, uint16_t qid, void *vring_m)
124 struct virtio_net *dev = get_device(vid);
125 uint16_t idx, idx_m, desc_id;
126 struct vhost_virtqueue *vq;
127 struct vring_desc desc;
128 struct vring_desc *desc_ring;
129 struct vring_desc *idesc = NULL;
130 struct vring *s_vring;
135 if (!dev || !vring_m)
138 if (qid >= dev->nr_vring)
141 if (vq_is_packed(dev))
144 s_vring = (struct vring *)vring_m;
145 vq = dev->virtqueue[qid];
147 idx_m = s_vring->used->idx;
148 ret = (uint16_t)(idx_m - idx);
150 while (idx != idx_m) {
151 /* copy used entry, used ring logging is not covered here */
152 vq->used->ring[idx & (vq->size - 1)] =
153 s_vring->used->ring[idx & (vq->size - 1)];
155 desc_id = vq->used->ring[idx & (vq->size - 1)].id;
156 desc_ring = vq->desc;
159 if (unlikely(desc_id >= vq->size))
162 if (vq->desc[desc_id].flags & VRING_DESC_F_INDIRECT) {
163 dlen = vq->desc[desc_id].len;
164 nr_descs = dlen / sizeof(struct vring_desc);
165 if (unlikely(nr_descs > vq->size))
168 desc_ring = (struct vring_desc *)(uintptr_t)
169 vhost_iova_to_vva(dev, vq,
170 vq->desc[desc_id].addr, &dlen,
172 if (unlikely(!desc_ring))
175 if (unlikely(dlen < vq->desc[desc_id].len)) {
176 idesc = vhost_alloc_copy_ind_table(dev, vq,
177 vq->desc[desc_id].addr,
178 vq->desc[desc_id].len);
179 if (unlikely(!idesc))
188 /* dirty page logging for DMA writeable buffer */
190 if (unlikely(desc_id >= vq->size))
192 if (unlikely(nr_descs-- == 0))
194 desc = desc_ring[desc_id];
195 if (desc.flags & VRING_DESC_F_WRITE)
196 vhost_log_write_iova(dev, vq, desc.addr,
199 } while (desc.flags & VRING_DESC_F_NEXT);
201 if (unlikely(idesc)) {
202 free_ind_table(idesc);
210 vq->used->idx = idx_m;
212 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
213 vring_used_event(s_vring) = idx_m;
219 free_ind_table(idesc);
224 rte_vdpa_get_queue_num(struct rte_vdpa_device *dev, uint32_t *queue_num)
226 if (dev == NULL || dev->ops == NULL || dev->ops->get_queue_num == NULL)
229 return dev->ops->get_queue_num(dev, queue_num);
233 rte_vdpa_get_features(struct rte_vdpa_device *dev, uint64_t *features)
235 if (dev == NULL || dev->ops == NULL || dev->ops->get_features == NULL)
238 return dev->ops->get_features(dev, features);
242 rte_vdpa_get_protocol_features(struct rte_vdpa_device *dev, uint64_t *features)
244 if (dev == NULL || dev->ops == NULL ||
245 dev->ops->get_protocol_features == NULL)
248 return dev->ops->get_protocol_features(dev, features);
252 rte_vdpa_get_stats_names(struct rte_vdpa_device *dev,
253 struct rte_vdpa_stat_name *stats_names,
259 RTE_FUNC_PTR_OR_ERR_RET(dev->ops->get_stats_names, -ENOTSUP);
261 return dev->ops->get_stats_names(dev, stats_names, size);
265 rte_vdpa_get_stats(struct rte_vdpa_device *dev, uint16_t qid,
266 struct rte_vdpa_stat *stats, unsigned int n)
268 if (!dev || !stats || !n)
271 RTE_FUNC_PTR_OR_ERR_RET(dev->ops->get_stats, -ENOTSUP);
273 return dev->ops->get_stats(dev, qid, stats, n);
277 rte_vdpa_reset_stats(struct rte_vdpa_device *dev, uint16_t qid)
282 RTE_FUNC_PTR_OR_ERR_RET(dev->ops->reset_stats, -ENOTSUP);
284 return dev->ops->reset_stats(dev, qid);
288 vdpa_dev_match(struct rte_vdpa_device *dev,
289 const struct rte_device *rte_dev)
291 if (dev->device == rte_dev)
297 /* Generic rte_vdpa_dev comparison function. */
298 typedef int (*rte_vdpa_cmp_t)(struct rte_vdpa_device *,
299 const struct rte_device *rte_dev);
301 static struct rte_vdpa_device *
302 vdpa_find_device(const struct rte_vdpa_device *start, rte_vdpa_cmp_t cmp,
303 struct rte_device *rte_dev)
305 struct rte_vdpa_device *dev;
307 rte_spinlock_lock(&vdpa_device_list_lock);
309 dev = TAILQ_FIRST(&vdpa_device_list);
311 dev = TAILQ_NEXT(start, next);
313 while (dev != NULL) {
314 if (cmp(dev, rte_dev) == 0)
317 dev = TAILQ_NEXT(dev, next);
319 rte_spinlock_unlock(&vdpa_device_list_lock);
325 vdpa_dev_iterate(const void *start,
327 const struct rte_dev_iterator *it)
329 struct rte_vdpa_device *vdpa_dev = NULL;
333 vdpa_dev = vdpa_find_device(start, vdpa_dev_match, it->device);
338 static struct rte_class rte_class_vdpa = {
339 .dev_iterate = vdpa_dev_iterate,
342 RTE_REGISTER_CLASS(vdpa, rte_class_vdpa);