virtqueue.h revision 255109
13077Sache/*-
286800Sache * Copyright (c) 2011, Bryan Venteicher <bryanv@FreeBSD.org>
33077Sache * All rights reserved.
43077Sache *
53077Sache * Redistribution and use in source and binary forms, with or without
63077Sache * modification, are permitted provided that the following conditions
73077Sache * are met:
83077Sache * 1. Redistributions of source code must retain the above copyright
93077Sache *    notice unmodified, this list of conditions, and the following
103077Sache *    disclaimer.
113077Sache * 2. Redistributions in binary form must reproduce the above copyright
123077Sache *    notice, this list of conditions and the following disclaimer in the
133077Sache *    documentation and/or other materials provided with the distribution.
143077Sache *
153077Sache * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
163077Sache * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
173077Sache * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
183077Sache * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
193077Sache * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
203077Sache * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
213077Sache * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
223077Sache * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
233077Sache * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
243077Sache * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2586800Sache *
2686800Sache * $FreeBSD: head/sys/dev/virtio/virtqueue.h 255109 2013-09-01 04:16:43Z bryanv $
273077Sache */
283077Sache
293077Sache#ifndef _VIRTIO_VIRTQUEUE_H
3089630Sache#define _VIRTIO_VIRTQUEUE_H
3189630Sache
3289630Sachestruct virtqueue;
3389630Sachestruct sglist;
343077Sache
353077Sache/* Support for indirect buffer descriptors. */
363077Sache#define VIRTIO_RING_F_INDIRECT_DESC	(1 << 28)
373077Sache
383077Sache/* Support to suppress interrupt until specific index is reached. */
393077Sache#define VIRTIO_RING_F_EVENT_IDX		(1 << 29)
403077Sache
413077Sache/* Device callback for a virtqueue interrupt. */
423077Sachetypedef void virtqueue_intr_t(void *);
433077Sache
443077Sache/*
4589630Sache * Hint on how long the next interrupt should be postponed. This is
463077Sache * only used when the EVENT_IDX feature is negotiated.
473077Sache */
483077Sachetypedef enum {
493077Sache	VQ_POSTPONE_SHORT,
503077Sache	VQ_POSTPONE_LONG,
513077Sache	VQ_POSTPONE_EMPTIED	/* Until all available desc are used. */
523077Sache} vq_postpone_t;
533077Sache
543077Sache#define VIRTQUEUE_MAX_NAME_SZ	32
553077Sache
563077Sache/* One for each virtqueue the device wishes to allocate. */
573077Sachestruct vq_alloc_info {
583077Sache	char		   vqai_name[VIRTQUEUE_MAX_NAME_SZ];
593077Sache	int		   vqai_maxindirsz;
603077Sache	virtqueue_intr_t  *vqai_intr;
613077Sache	void		  *vqai_intr_arg;
623077Sache	struct virtqueue **vqai_vq;
63};
64
65#define VQ_ALLOC_INFO_INIT(_i,_nsegs,_intr,_arg,_vqp,_str,...) do {	\
66	snprintf((_i)->vqai_name, VIRTQUEUE_MAX_NAME_SZ, _str,		\
67	    ##__VA_ARGS__);						\
68	(_i)->vqai_maxindirsz = (_nsegs);				\
69	(_i)->vqai_intr = (_intr);					\
70	(_i)->vqai_intr_arg = (_arg);					\
71	(_i)->vqai_vq = (_vqp);						\
72} while (0)
73
74uint64_t virtqueue_filter_features(uint64_t features);
75
76int	 virtqueue_alloc(device_t dev, uint16_t queue, uint16_t size,
77	     int align, vm_paddr_t highaddr, struct vq_alloc_info *info,
78	     struct virtqueue **vqp);
79void	*virtqueue_drain(struct virtqueue *vq, int *last);
80void	 virtqueue_free(struct virtqueue *vq);
81int	 virtqueue_reinit(struct virtqueue *vq, uint16_t size);
82
83int	 virtqueue_intr_filter(struct virtqueue *vq);
84void	 virtqueue_intr(struct virtqueue *vq);
85int	 virtqueue_enable_intr(struct virtqueue *vq);
86int	 virtqueue_postpone_intr(struct virtqueue *vq, vq_postpone_t hint);
87void	 virtqueue_disable_intr(struct virtqueue *vq);
88
89/* Get physical address of the virtqueue ring. */
90vm_paddr_t virtqueue_paddr(struct virtqueue *vq);
91
92int	 virtqueue_full(struct virtqueue *vq);
93int	 virtqueue_empty(struct virtqueue *vq);
94int	 virtqueue_size(struct virtqueue *vq);
95int	 virtqueue_nused(struct virtqueue *vq);
96void	 virtqueue_notify(struct virtqueue *vq);
97void	 virtqueue_dump(struct virtqueue *vq);
98
99int	 virtqueue_enqueue(struct virtqueue *vq, void *cookie,
100	     struct sglist *sg, int readable, int writable);
101void	*virtqueue_dequeue(struct virtqueue *vq, uint32_t *len);
102void	*virtqueue_poll(struct virtqueue *vq, uint32_t *len);
103
104#endif /* _VIRTIO_VIRTQUEUE_H */
105