1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018 Intel Corporation
8 * Device specific vhost lib
13 #include <rte_class.h>
14 #include <rte_malloc.h>
18 static struct rte_vdpa_device vdpa_devices[MAX_VHOST_DEVICE];
19 static uint32_t vdpa_device_num;
23 rte_vdpa_find_device_id(struct rte_vdpa_device *dev)
25 struct rte_vdpa_device *tmp_dev;
31 for (i = 0; i < MAX_VHOST_DEVICE; ++i) {
32 tmp_dev = &vdpa_devices[i];
33 if (tmp_dev->ops == NULL)
43 struct rte_vdpa_device *
44 rte_vdpa_find_device_by_name(const char *name)
46 struct rte_vdpa_device *dev;
52 for (i = 0; i < MAX_VHOST_DEVICE; ++i) {
53 dev = &vdpa_devices[i];
57 if (strncmp(dev->device->name, name, RTE_DEV_NAME_MAX_LEN) == 0)
65 rte_vdpa_get_rte_device(struct rte_vdpa_device *vdpa_dev)
70 return vdpa_dev->device;
73 struct rte_vdpa_device *
74 rte_vdpa_get_device(int did)
76 if (did < 0 || did >= MAX_VHOST_DEVICE)
79 return &vdpa_devices[did];
82 struct rte_vdpa_device *
83 rte_vdpa_register_device(struct rte_device *rte_dev,
84 struct rte_vdpa_dev_ops *ops)
86 struct rte_vdpa_device *dev;
89 if (vdpa_device_num >= MAX_VHOST_DEVICE || ops == NULL)
92 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
93 dev = &vdpa_devices[i];
97 if (dev->device == rte_dev)
101 for (i = 0; i < MAX_VHOST_DEVICE; i++) {
102 if (vdpa_devices[i].ops == NULL)
106 if (i == MAX_VHOST_DEVICE)
109 dev = &vdpa_devices[i];
110 dev->device = rte_dev;
118 rte_vdpa_unregister_device(struct rte_vdpa_device *vdev)
120 int did = rte_vdpa_find_device_id(vdev);
122 if (did < 0 || vdpa_devices[did].ops == NULL)
125 memset(&vdpa_devices[did], 0, sizeof(struct rte_vdpa_device));
132 rte_vdpa_get_device_num(void)
134 return vdpa_device_num;
138 rte_vdpa_relay_vring_used(int vid, uint16_t qid, void *vring_m)
140 struct virtio_net *dev = get_device(vid);
141 uint16_t idx, idx_m, desc_id;
142 struct vhost_virtqueue *vq;
143 struct vring_desc desc;
144 struct vring_desc *desc_ring;
145 struct vring_desc *idesc = NULL;
146 struct vring *s_vring;
151 if (!dev || !vring_m)
154 if (qid >= dev->nr_vring)
157 if (vq_is_packed(dev))
160 s_vring = (struct vring *)vring_m;
161 vq = dev->virtqueue[qid];
163 idx_m = s_vring->used->idx;
164 ret = (uint16_t)(idx_m - idx);
166 while (idx != idx_m) {
167 /* copy used entry, used ring logging is not covered here */
168 vq->used->ring[idx & (vq->size - 1)] =
169 s_vring->used->ring[idx & (vq->size - 1)];
171 desc_id = vq->used->ring[idx & (vq->size - 1)].id;
172 desc_ring = vq->desc;
175 if (unlikely(desc_id >= vq->size))
178 if (vq->desc[desc_id].flags & VRING_DESC_F_INDIRECT) {
179 dlen = vq->desc[desc_id].len;
180 nr_descs = dlen / sizeof(struct vring_desc);
181 if (unlikely(nr_descs > vq->size))
184 desc_ring = (struct vring_desc *)(uintptr_t)
185 vhost_iova_to_vva(dev, vq,
186 vq->desc[desc_id].addr, &dlen,
188 if (unlikely(!desc_ring))
191 if (unlikely(dlen < vq->desc[desc_id].len)) {
192 idesc = vhost_alloc_copy_ind_table(dev, vq,
193 vq->desc[desc_id].addr,
194 vq->desc[desc_id].len);
195 if (unlikely(!idesc))
204 /* dirty page logging for DMA writeable buffer */
206 if (unlikely(desc_id >= vq->size))
208 if (unlikely(nr_descs-- == 0))
210 desc = desc_ring[desc_id];
211 if (desc.flags & VRING_DESC_F_WRITE)
212 vhost_log_write_iova(dev, vq, desc.addr,
215 } while (desc.flags & VRING_DESC_F_NEXT);
217 if (unlikely(idesc)) {
218 free_ind_table(idesc);
226 vq->used->idx = idx_m;
228 if (dev->features & (1ULL << VIRTIO_RING_F_EVENT_IDX))
229 vring_used_event(s_vring) = idx_m;
235 free_ind_table(idesc);
240 rte_vdpa_get_stats_names(struct rte_vdpa_device *dev,
241 struct rte_vdpa_stat_name *stats_names,
247 RTE_FUNC_PTR_OR_ERR_RET(dev->ops->get_stats_names, -ENOTSUP);
249 return dev->ops->get_stats_names(dev, stats_names, size);
253 rte_vdpa_get_stats(struct rte_vdpa_device *dev, uint16_t qid,
254 struct rte_vdpa_stat *stats, unsigned int n)
256 if (!dev || !stats || !n)
259 RTE_FUNC_PTR_OR_ERR_RET(dev->ops->get_stats, -ENOTSUP);
261 return dev->ops->get_stats(dev, qid, stats, n);
265 rte_vdpa_reset_stats(struct rte_vdpa_device *dev, uint16_t qid)
270 RTE_FUNC_PTR_OR_ERR_RET(dev->ops->reset_stats, -ENOTSUP);
272 return dev->ops->reset_stats(dev, qid);
276 vdpa_dev_to_id(const struct rte_vdpa_device *dev)
279 return MAX_VHOST_DEVICE;
281 if (dev < &vdpa_devices[0] ||
282 dev >= &vdpa_devices[MAX_VHOST_DEVICE])
283 return MAX_VHOST_DEVICE;
285 return (uint16_t)(dev - vdpa_devices);
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;
310 idx = vdpa_dev_to_id(start) + 1;
313 for (; idx < MAX_VHOST_DEVICE; idx++) {
314 dev = &vdpa_devices[idx];
316 * ToDo: Certainly better to introduce a state field,
317 * but rely on ops being set for now.
319 if (dev->ops == NULL)
321 if (cmp(dev, rte_dev) == 0)
328 vdpa_dev_iterate(const void *start,
330 const struct rte_dev_iterator *it)
332 struct rte_vdpa_device *vdpa_dev = NULL;
336 vdpa_dev = vdpa_find_device(start, vdpa_dev_match, it->device);
341 static struct rte_class rte_class_vdpa = {
342 .dev_iterate = vdpa_dev_iterate,
345 RTE_REGISTER_CLASS(vdpa, rte_class_vdpa);