drivers/vhost: add vhost_get_vq_buffers() to collect scatter-gather buffers

Add vhost_get_vq_buffers() API to retrieve all chained buffers from a
virtqueue in one call. This simplifies handling of scatter-gather I/O
where data spans multiple descriptors.

The function populates an array of virtqueue_buf structures with buffer
pointers and lengths, returning the descriptor head index on success.

Signed-off-by: Bowen Wang <wangbowen6@xiaomi.com>
Signed-off-by: hongfengchen <hongfengchen@xiaomi.com>
This commit is contained in:
Bowen Wang
2024-09-26 22:04:30 +08:00
committed by Xiang Xiao
parent 5051007bfa
commit 2fc2f44b61
2 changed files with 49 additions and 0 deletions
+46
View File
@@ -164,6 +164,52 @@ static void vhost_defered_probe_work(FAR void *arg)
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: vhost_get_vq_buf
****************************************************************************/
int vhost_get_vq_buffers(FAR struct virtqueue *vq,
FAR struct virtqueue_buf *vb, size_t vbsize,
FAR size_t *vbcnt)
{
FAR void *buf;
uint16_t head;
uint16_t idx;
uint32_t len;
size_t i;
DEBUGASSERT(vb != NULL && vbsize >= 1 && vbcnt != NULL);
buf = virtqueue_get_first_avail_buffer(vq, &head, &len);
if (buf == NULL)
{
return -ENOMEM;
}
vb[0].buf = buf;
vb[0].len = len;
for (i = 1, idx = head; ; i++)
{
buf = virtqueue_get_next_avail_buffer(vq, idx, &idx, &len);
if (buf == NULL)
{
break;
}
else if (i >= vbsize)
{
vhosterr("vbsize %zu is not enough\n", vbsize);
return -EINVAL;
}
vb[i].buf = buf;
vb[i].len = len;
}
*vbcnt = i;
return head;
}
/****************************************************************************
* Name: vhost_register_driver
****************************************************************************/
+3
View File
@@ -88,6 +88,9 @@ int vhost_register_device(FAR struct vhost_device *hdev);
int vhost_register_driver(FAR struct vhost_driver *hdrv);
int vhost_unregister_driver(FAR struct vhost_driver *hdrv);
int vhost_unregister_device(FAR struct vhost_device *hdev);
int vhost_get_vq_buffers(FAR struct virtqueue *vq,
FAR struct virtqueue_buf *vb, size_t vbsize,
FAR size_t *vbcnt);
/****************************************************************************
* Name: vhost_register_drivers