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>
20 #include "rte_vdpa_dev.h"
23 /** Double linked list of vDPA devices. */
24 TAILQ_HEAD(vdpa_device_list, rte_vdpa_device);
26 static struct vdpa_device_list vdpa_device_list =
27 TAILQ_HEAD_INITIALIZER(vdpa_device_list);
28 static rte_spinlock_t vdpa_device_list_lock = RTE_SPINLOCK_INITIALIZER;
31 /* Unsafe, needs to be called with vdpa_device_list_lock held */
32 static struct rte_vdpa_device *
33 __vdpa_find_device_by_name(const char *name)
35 struct rte_vdpa_device *dev, *ret = NULL;
40 TAILQ_FOREACH(dev, &vdpa_device_list, next) {
41 if (!strncmp(dev->device->name, name, RTE_DEV_NAME_MAX_LEN)) {
50 struct rte_vdpa_device *
51 rte_vdpa_find_device_by_name(const char *name)
53 struct rte_vdpa_device *dev;
55 rte_spinlock_lock(&vdpa_device_list_lock);
56 dev = __vdpa_find_device_by_name(name);
57 rte_spinlock_unlock(&vdpa_device_list_lock);
63 rte_vdpa_get_rte_device(struct rte_vdpa_device *vdpa_dev)
68 return vdpa_dev->device;
71 struct rte_vdpa_device *
72 rte_vdpa_register_device(struct rte_device *rte_dev,
73 struct rte_vdpa_dev_ops *ops)
75 struct rte_vdpa_device *dev;
80 rte_spinlock_lock(&vdpa_device_list_lock);
81 /* Check the device hasn't been register already */
82 dev = __vdpa_find_device_by_name(rte_dev->name);
88 dev = rte_zmalloc(NULL, sizeof(*dev), 0);
92 dev->device = rte_dev;
94 TAILQ_INSERT_TAIL(&vdpa_device_list, dev, next);
96 rte_spinlock_unlock(&vdpa_device_list_lock);
102 rte_vdpa_unregister_device(struct rte_vdpa_device *dev)
104 struct rte_vdpa_device *cur_dev, *tmp_dev;
107 rte_spinlock_lock(&vdpa_device_list_lock);
108 TAILQ_FOREACH_SAFE(cur_dev, &vdpa_device_list, next, tmp_dev) {
112 TAILQ_REMOVE(&vdpa_device_list, dev, next);
117 rte_spinlock_unlock(&vdpa_device_list_lock);
123 rte_vdpa_relay_vring_used(int vid, uint16_t qid, void *vring_m)
125 struct virtio_net *dev = get_device(vid);
126 uint16_t idx, idx_m, desc_id;
127 struct vhost_virtqueue *vq;
128 struct vring_desc desc;
129 struct vring_desc *desc_ring;
130 struct vring_desc *idesc = NULL;
131 struct vring *s_vring;
136 if (!dev || !vring_m)
139 if (qid >= dev->nr_vring)
142 if (vq_is_packed(dev))
145 s_vring = (struct vring *)vring_m;
146 vq = dev->virtqueue[qid];
148 idx_m = s_vring->used->idx;
149 ret = (uint16_t)(idx_m - idx);
151 while (idx != idx_m) {
152 /* copy used entry, used ring logging is not covered here */
153 vq->used->ring[idx & (vq->size - 1)] =
154 s_vring->used->ring[idx & (vq->size - 1)];
156 desc_id = vq->used->ring[idx & (vq->size - 1)].id;
157 desc_ring = vq->desc;
160 if (unlikely(desc_id >= vq->size))
163 if (vq->desc[desc_id].flags & VRING_DESC_F_INDIRECT) {
164 dlen = vq->desc[desc_id].len;
165 nr_descs = dlen / sizeof(struct vring_desc);
166 if (unlikely(nr_descs > vq->size))
169 desc_ring = (struct vring_desc *)(uintptr_t)
170 vhost_iova_to_vva(dev, vq,
171 vq->desc[desc_id].addr, &dlen,
173 if (unlikely(!desc_ring))
176 if (unlikely(dlen < vq->desc[desc_id].len)) {
177 idesc = vhost_alloc_copy_ind_table(dev, vq,
178 vq->desc[desc_id].addr,
179 vq->desc[desc_id].len);
180 if (unlikely(!idesc))
189 /* dirty page logging for DMA writeable buffer */
191 if (unlikely(desc_id >= vq->size))
193 if (unlikely(nr_descs-- == 0))
195 desc = desc_ring[desc_id];
196 if (desc.flags & VRING_DESC_F_WRITE)
197 vhost_log_write_iova(dev, vq, desc.addr,
200 } while (desc.flags & VRING_DESC_F_NEXT);
202 if (unlikely(idesc)) {
203 free_ind_table(idesc);
211 vq->used->idx = idx_m;
213 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
214 vring_used_event(s_vring) = idx_m;
220 free_ind_table(idesc);
225 rte_vdpa_get_queue_num(struct rte_vdpa_device *dev, uint32_t *queue_num)
227 if (dev == NULL || dev->ops == NULL || dev->ops->get_queue_num == NULL)
230 return dev->ops->get_queue_num(dev, queue_num);
234 rte_vdpa_get_features(struct rte_vdpa_device *dev, uint64_t *features)
236 if (dev == NULL || dev->ops == NULL || dev->ops->get_features == NULL)
239 return dev->ops->get_features(dev, features);
243 rte_vdpa_get_protocol_features(struct rte_vdpa_device *dev, uint64_t *features)
245 if (dev == NULL || dev->ops == NULL ||
246 dev->ops->get_protocol_features == NULL)
249 return dev->ops->get_protocol_features(dev, features);
253 rte_vdpa_get_stats_names(struct rte_vdpa_device *dev,
254 struct rte_vdpa_stat_name *stats_names,
260 RTE_FUNC_PTR_OR_ERR_RET(dev->ops->get_stats_names, -ENOTSUP);
262 return dev->ops->get_stats_names(dev, stats_names, size);
266 rte_vdpa_get_stats(struct rte_vdpa_device *dev, uint16_t qid,
267 struct rte_vdpa_stat *stats, unsigned int n)
269 if (!dev || !stats || !n)
272 RTE_FUNC_PTR_OR_ERR_RET(dev->ops->get_stats, -ENOTSUP);
274 return dev->ops->get_stats(dev, qid, stats, n);
278 rte_vdpa_reset_stats(struct rte_vdpa_device *dev, uint16_t qid)
283 RTE_FUNC_PTR_OR_ERR_RET(dev->ops->reset_stats, -ENOTSUP);
285 return dev->ops->reset_stats(dev, qid);
289 vdpa_dev_match(struct rte_vdpa_device *dev,
290 const struct rte_device *rte_dev)
292 if (dev->device == rte_dev)
298 /* Generic rte_vdpa_dev comparison function. */
299 typedef int (*rte_vdpa_cmp_t)(struct rte_vdpa_device *,
300 const struct rte_device *rte_dev);
302 static struct rte_vdpa_device *
303 vdpa_find_device(const struct rte_vdpa_device *start, rte_vdpa_cmp_t cmp,
304 struct rte_device *rte_dev)
306 struct rte_vdpa_device *dev;
308 rte_spinlock_lock(&vdpa_device_list_lock);
310 dev = TAILQ_FIRST(&vdpa_device_list);
312 dev = TAILQ_NEXT(start, next);
314 while (dev != NULL) {
315 if (cmp(dev, rte_dev) == 0)
318 dev = TAILQ_NEXT(dev, next);
320 rte_spinlock_unlock(&vdpa_device_list_lock);
326 vdpa_dev_iterate(const void *start,
328 const struct rte_dev_iterator *it)
330 struct rte_vdpa_device *vdpa_dev = NULL;
334 vdpa_dev = vdpa_find_device(start, vdpa_dev_match, it->device);
339 static struct rte_class rte_class_vdpa = {
340 .dev_iterate = vdpa_dev_iterate,
343 RTE_REGISTER_CLASS(vdpa, rte_class_vdpa);