On Fri, Jun 02, 2023 at 05:22:03PM +0800, Xuan Zhuo wrote: > This patch introduces three helpers for premapped mode. > > * virtqueue_get_buf_premapped > * virtqueue_detach_unused_buf_premapped > > The above helpers work like the non-premapped funcs. But a cursor is > passed. > > virtqueue_detach is used to get the dma info of the last buf by > cursor. This isn't very clear from the description but virtqueue_detach is also introduced by this patch as opposed to being used. > Signed-off-by: Xuan Zhuo <xuanzhuo@xxxxxxxxxxxxxxxxx> > --- > drivers/virtio/virtio_ring.c | 83 ++++++++++++++++++++++++++++++++++++ > include/linux/virtio.h | 10 +++++ > 2 files changed, 93 insertions(+) > > diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c > index cbc22daae7e1..6771b9661798 100644 > --- a/drivers/virtio/virtio_ring.c > +++ b/drivers/virtio/virtio_ring.c > @@ -2555,6 +2555,66 @@ void *virtqueue_get_buf(struct virtqueue *_vq, unsigned int *len) > return virtqueue_get_buf_ctx(_vq, len, NULL); > } > EXPORT_SYMBOL_GPL(virtqueue_get_buf); > + > +/** > + * virtqueue_get_buf_premapped - get the next used buffer > + * @_vq: the struct virtqueue we're talking about. > + * @len: the length written into the buffer > + * @ctx: extra context for the token > + * @cursor: detach cursor > + * > + * If the device wrote data into the buffer, @len will be set to the > + * amount written. This means you don't need to clear the buffer > + * beforehand to ensure there's no data leakage in the case of short > + * writes. > + * > + * Caller must ensure we don't call this with other virtqueue > + * operations at the same time (except where noted). > + * > + * This is used for the premapped vq. The cursor is passed by the dirver, that > + * is used for virtqueue_detach. That will be initialized by virtio core > + * internally. > + * > + * Returns NULL if there are no used buffers, or the "data" token > + * handed to virtqueue_add_*(). > + */ > +void *virtqueue_get_buf_premapped(struct virtqueue *_vq, unsigned int *len, > + void **ctx, > + struct virtqueue_detach_cursor *cursor) > +{ > + struct vring_virtqueue *vq = to_vvq(_vq); > + > + return vq->packed_ring ? virtqueue_get_buf_ctx_packed(_vq, len, ctx, cursor) : > + virtqueue_get_buf_ctx_split(_vq, len, ctx, cursor); > +} > +EXPORT_SYMBOL_GPL(virtqueue_get_buf_premapped); > + > +/** > + * virtqueue_detach - get the dma info of last buf detach what from what then? I am guessing this is not the only thing this function does? sounds like a bad name for a function. > + * @_vq: the struct virtqueue we're talking about. > + * @cursor: detach cursor > + * @addr: the dma address what address? it's the 1st time you mention an address ... > + * @len: the length of the dma address > + * @dir: the direction of the dma address > + * > + * This is used for the premapped vq. The cursor is initialized by > + * virtqueue_get_buf_premapped or virtqueue_detach_unused_buf_premapped. > + * > + * Returns: > + * -EAGAIN: there are more dma info, this function should be called more. here too, pls don't return -EAGAIN not in an error case. something like "1" will do. > + * -EINVAL: the process is done, should not call this function > + * 0: no more dma info > + */ > +int virtqueue_detach(struct virtqueue *_vq, struct virtqueue_detach_cursor *cursor, > + dma_addr_t *addr, u32 *len, enum dma_data_direction *dir) > +{ > + struct vring_virtqueue *vq = to_vvq(_vq); > + > + return vq->packed_ring ? virtqueue_detach_packed(_vq, cursor, addr, len, dir) : > + virtqueue_detach_split(_vq, cursor, addr, len, dir); > +} > +EXPORT_SYMBOL_GPL(virtqueue_detach); > + > /** > * virtqueue_disable_cb - disable callbacks > * @_vq: the struct virtqueue we're talking about. > @@ -2682,6 +2742,29 @@ void *virtqueue_detach_unused_buf(struct virtqueue *_vq) > } > EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf); > > +/** > + * virtqueue_detach_unused_buf_premapped - detach first unused buffer > + * @_vq: the struct virtqueue we're talking about. > + * @cursor: detach cursor > + * > + * This is used for the premapped vq. The cursor is passed by the dirver, that > + * is used for virtqueue_detach. That will be initialized by virtio core > + * internally. > + * > + * Returns NULL or the "data" token handed to virtqueue_add_*(). > + * This is not valid on an active queue; it is useful for device > + * shutdown or the reset queue. > + */ > +void *virtqueue_detach_unused_buf_premapped(struct virtqueue *_vq, > + struct virtqueue_detach_cursor *cursor) > +{ > + struct vring_virtqueue *vq = to_vvq(_vq); > + > + return vq->packed_ring ? virtqueue_detach_unused_buf_packed(_vq, cursor) : > + virtqueue_detach_unused_buf_split(_vq, cursor); > +} > +EXPORT_SYMBOL_GPL(virtqueue_detach_unused_buf_premapped); > + > static inline bool more_used(const struct vring_virtqueue *vq) > { > return vq->packed_ring ? more_used_packed(vq) : more_used_split(vq); > diff --git a/include/linux/virtio.h b/include/linux/virtio.h > index 7f137c7a9034..0a11c5b32fe5 100644 > --- a/include/linux/virtio.h > +++ b/include/linux/virtio.h > @@ -3,6 +3,7 @@ > #define _LINUX_VIRTIO_H > /* Everything a virtio driver needs to work with any particular virtio > * implementation. */ > +#include <linux/dma-mapping.h> > #include <linux/types.h> > #include <linux/scatterlist.h> > #include <linux/spinlock.h> > @@ -88,6 +89,10 @@ void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len); > void *virtqueue_get_buf_ctx(struct virtqueue *vq, unsigned int *len, > void **ctx); > > +void *virtqueue_get_buf_premapped(struct virtqueue *_vq, unsigned int *len, > + void **ctx, > + struct virtqueue_detach_cursor *cursor); > + > void virtqueue_disable_cb(struct virtqueue *vq); > > bool virtqueue_enable_cb(struct virtqueue *vq); > @@ -101,6 +106,8 @@ bool virtqueue_poll(struct virtqueue *vq, unsigned); > bool virtqueue_enable_cb_delayed(struct virtqueue *vq); > > void *virtqueue_detach_unused_buf(struct virtqueue *vq); > +void *virtqueue_detach_unused_buf_premapped(struct virtqueue *_vq, > + struct virtqueue_detach_cursor *cursor); > > unsigned int virtqueue_get_vring_size(const struct virtqueue *vq); > > @@ -114,6 +121,9 @@ dma_addr_t virtqueue_get_used_addr(const struct virtqueue *vq); > int virtqueue_resize(struct virtqueue *vq, u32 num, > void (*recycle)(struct virtqueue *vq, void *buf)); > > +int virtqueue_detach(struct virtqueue *_vq, struct virtqueue_detach_cursor *cursor, > + dma_addr_t *addr, u32 *len, enum dma_data_direction *dir); > + > /** > * struct virtio_device - representation of a device using virtio > * @index: unique position on the virtio bus > -- > 2.32.0.3.g01195cf9f