2007-10-22 08:03:36 +07:00
|
|
|
#ifndef _LINUX_VIRTIO_H
|
|
|
|
#define _LINUX_VIRTIO_H
|
|
|
|
/* Everything a virtio driver needs to work with any particular virtio
|
|
|
|
* implementation. */
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/scatterlist.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/device.h>
|
|
|
|
#include <linux/mod_devicetable.h>
|
2010-04-29 21:26:37 +07:00
|
|
|
#include <linux/gfp.h>
|
2013-03-20 10:21:24 +07:00
|
|
|
#include <linux/vringh.h>
|
2007-10-22 08:03:36 +07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virtqueue - a queue to register buffers for sending or receiving.
|
2009-06-13 11:16:35 +07:00
|
|
|
* @list: the chain of virtqueues for this device
|
2007-10-22 08:03:36 +07:00
|
|
|
* @callback: the function to call when buffers are consumed (can be NULL).
|
2009-06-13 11:16:35 +07:00
|
|
|
* @name: the name of this virtqueue (mainly for debugging)
|
2007-10-22 08:03:36 +07:00
|
|
|
* @vdev: the virtio device this queue was created for.
|
|
|
|
* @priv: a pointer for the virtqueue implementation to use.
|
2012-10-16 20:26:14 +07:00
|
|
|
* @index: the zero-based ordinal number for this queue.
|
|
|
|
* @num_free: number of elements we expect to be able to fit.
|
|
|
|
*
|
|
|
|
* A note on @num_free: with indirect buffers, each buffer needs one
|
|
|
|
* element in the queue, otherwise a buffer will need one element per
|
|
|
|
* sg element.
|
2007-10-22 08:03:36 +07:00
|
|
|
*/
|
2009-06-13 11:16:35 +07:00
|
|
|
struct virtqueue {
|
|
|
|
struct list_head list;
|
2008-02-05 11:49:57 +07:00
|
|
|
void (*callback)(struct virtqueue *vq);
|
2009-06-13 11:16:35 +07:00
|
|
|
const char *name;
|
2007-10-22 08:03:36 +07:00
|
|
|
struct virtio_device *vdev;
|
2012-10-16 20:26:14 +07:00
|
|
|
unsigned int index;
|
|
|
|
unsigned int num_free;
|
2007-10-22 08:03:36 +07:00
|
|
|
void *priv;
|
|
|
|
};
|
|
|
|
|
2013-03-20 12:14:26 +07:00
|
|
|
int virtqueue_add_outbuf(struct virtqueue *vq,
|
|
|
|
struct scatterlist sg[], unsigned int num,
|
|
|
|
void *data,
|
|
|
|
gfp_t gfp);
|
|
|
|
|
|
|
|
int virtqueue_add_inbuf(struct virtqueue *vq,
|
|
|
|
struct scatterlist sg[], unsigned int num,
|
|
|
|
void *data,
|
|
|
|
gfp_t gfp);
|
|
|
|
|
2017-02-07 11:15:13 +07:00
|
|
|
int virtqueue_add_inbuf_ctx(struct virtqueue *vq,
|
|
|
|
struct scatterlist sg[], unsigned int num,
|
|
|
|
void *data,
|
|
|
|
void *ctx,
|
|
|
|
gfp_t gfp);
|
|
|
|
|
2013-03-20 12:07:09 +07:00
|
|
|
int virtqueue_add_sgs(struct virtqueue *vq,
|
|
|
|
struct scatterlist *sgs[],
|
|
|
|
unsigned int out_sgs,
|
|
|
|
unsigned int in_sgs,
|
|
|
|
void *data,
|
|
|
|
gfp_t gfp);
|
|
|
|
|
2013-10-29 06:09:48 +07:00
|
|
|
bool virtqueue_kick(struct virtqueue *vq);
|
2007-10-22 08:03:36 +07:00
|
|
|
|
2012-01-12 12:14:43 +07:00
|
|
|
bool virtqueue_kick_prepare(struct virtqueue *vq);
|
|
|
|
|
2013-10-29 06:09:48 +07:00
|
|
|
bool virtqueue_notify(struct virtqueue *vq);
|
2012-01-12 12:14:43 +07:00
|
|
|
|
2010-04-12 20:19:07 +07:00
|
|
|
void *virtqueue_get_buf(struct virtqueue *vq, unsigned int *len);
|
2010-04-12 20:18:25 +07:00
|
|
|
|
2017-02-07 11:15:13 +07:00
|
|
|
void *virtqueue_get_buf_ctx(struct virtqueue *vq, unsigned int *len,
|
|
|
|
void **ctx);
|
|
|
|
|
2010-04-12 20:19:07 +07:00
|
|
|
void virtqueue_disable_cb(struct virtqueue *vq);
|
2010-04-12 20:18:25 +07:00
|
|
|
|
2010-04-12 20:19:07 +07:00
|
|
|
bool virtqueue_enable_cb(struct virtqueue *vq);
|
2010-04-12 20:18:25 +07:00
|
|
|
|
2013-07-09 17:19:18 +07:00
|
|
|
unsigned virtqueue_enable_cb_prepare(struct virtqueue *vq);
|
|
|
|
|
|
|
|
bool virtqueue_poll(struct virtqueue *vq, unsigned);
|
|
|
|
|
2011-05-20 06:11:14 +07:00
|
|
|
bool virtqueue_enable_cb_delayed(struct virtqueue *vq);
|
|
|
|
|
2010-04-12 20:19:07 +07:00
|
|
|
void *virtqueue_detach_unused_buf(struct virtqueue *vq);
|
2010-04-12 20:18:25 +07:00
|
|
|
|
2011-10-19 15:10:59 +07:00
|
|
|
unsigned int virtqueue_get_vring_size(struct virtqueue *vq);
|
2012-08-28 18:54:13 +07:00
|
|
|
|
2013-10-29 06:10:19 +07:00
|
|
|
bool virtqueue_is_broken(struct virtqueue *vq);
|
|
|
|
|
2016-02-03 12:46:37 +07:00
|
|
|
const struct vring *virtqueue_get_vring(struct virtqueue *vq);
|
|
|
|
dma_addr_t virtqueue_get_desc_addr(struct virtqueue *vq);
|
|
|
|
dma_addr_t virtqueue_get_avail_addr(struct virtqueue *vq);
|
|
|
|
dma_addr_t virtqueue_get_used_addr(struct virtqueue *vq);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Legacy accessors -- in almost all cases, these are the wrong functions
|
|
|
|
* to use.
|
|
|
|
*/
|
|
|
|
static inline void *virtqueue_get_desc(struct virtqueue *vq)
|
|
|
|
{
|
|
|
|
return virtqueue_get_vring(vq)->desc;
|
|
|
|
}
|
|
|
|
static inline void *virtqueue_get_avail(struct virtqueue *vq)
|
|
|
|
{
|
|
|
|
return virtqueue_get_vring(vq)->avail;
|
|
|
|
}
|
|
|
|
static inline void *virtqueue_get_used(struct virtqueue *vq)
|
|
|
|
{
|
|
|
|
return virtqueue_get_vring(vq)->used;
|
|
|
|
}
|
2014-10-07 21:39:47 +07:00
|
|
|
|
2007-10-22 08:03:36 +07:00
|
|
|
/**
|
|
|
|
* virtio_device - representation of a device using virtio
|
|
|
|
* @index: unique position on the virtio bus
|
2014-11-10 06:03:29 +07:00
|
|
|
* @failed: saved value for VIRTIO_CONFIG_S_FAILED bit (for restore)
|
2014-10-15 06:51:55 +07:00
|
|
|
* @config_enabled: configuration change reporting enabled
|
|
|
|
* @config_change_pending: configuration change reported while disabled
|
|
|
|
* @config_lock: protects configuration change reporting
|
2007-10-22 08:03:36 +07:00
|
|
|
* @dev: underlying device.
|
|
|
|
* @id: the device type identification (used to match it with a driver).
|
|
|
|
* @config: the configuration ops for this device.
|
2013-03-20 10:21:24 +07:00
|
|
|
* @vringh_config: configuration ops for host vrings.
|
2009-06-13 11:16:35 +07:00
|
|
|
* @vqs: the list of virtqueues for this device.
|
2008-05-03 09:50:50 +07:00
|
|
|
* @features: the features supported by both driver and device.
|
2007-10-22 08:03:36 +07:00
|
|
|
* @priv: private pointer for the driver's use.
|
|
|
|
*/
|
2009-06-13 11:16:35 +07:00
|
|
|
struct virtio_device {
|
2007-10-22 08:03:36 +07:00
|
|
|
int index;
|
2014-10-14 07:10:35 +07:00
|
|
|
bool failed;
|
2014-10-15 06:51:55 +07:00
|
|
|
bool config_enabled;
|
|
|
|
bool config_change_pending;
|
|
|
|
spinlock_t config_lock;
|
2007-10-22 08:03:36 +07:00
|
|
|
struct device dev;
|
|
|
|
struct virtio_device_id id;
|
2013-02-10 12:27:38 +07:00
|
|
|
const struct virtio_config_ops *config;
|
2013-03-20 10:21:24 +07:00
|
|
|
const struct vringh_config_ops *vringh_config;
|
2009-06-13 11:16:35 +07:00
|
|
|
struct list_head vqs;
|
2014-10-07 21:39:43 +07:00
|
|
|
u64 features;
|
2007-10-22 08:03:36 +07:00
|
|
|
void *priv;
|
|
|
|
};
|
|
|
|
|
2012-12-11 07:34:50 +07:00
|
|
|
static inline struct virtio_device *dev_to_virtio(struct device *_dev)
|
|
|
|
{
|
|
|
|
return container_of(_dev, struct virtio_device, dev);
|
|
|
|
}
|
|
|
|
|
2017-02-03 10:16:01 +07:00
|
|
|
void virtio_add_status(struct virtio_device *dev, unsigned int status);
|
2007-10-22 08:03:36 +07:00
|
|
|
int register_virtio_device(struct virtio_device *dev);
|
|
|
|
void unregister_virtio_device(struct virtio_device *dev);
|
|
|
|
|
2014-04-28 08:45:08 +07:00
|
|
|
void virtio_break_device(struct virtio_device *dev);
|
|
|
|
|
2014-10-14 07:10:34 +07:00
|
|
|
void virtio_config_changed(struct virtio_device *dev);
|
2017-02-03 10:16:01 +07:00
|
|
|
void virtio_config_disable(struct virtio_device *dev);
|
|
|
|
void virtio_config_enable(struct virtio_device *dev);
|
|
|
|
int virtio_finalize_features(struct virtio_device *dev);
|
2014-10-14 07:10:35 +07:00
|
|
|
#ifdef CONFIG_PM_SLEEP
|
|
|
|
int virtio_device_freeze(struct virtio_device *dev);
|
|
|
|
int virtio_device_restore(struct virtio_device *dev);
|
|
|
|
#endif
|
2014-10-14 07:10:34 +07:00
|
|
|
|
2007-10-22 08:03:36 +07:00
|
|
|
/**
|
|
|
|
* virtio_driver - operations for a virtio I/O driver
|
|
|
|
* @driver: underlying device driver (populate name and owner).
|
|
|
|
* @id_table: the ids serviced by this driver.
|
2011-08-25 20:04:05 +07:00
|
|
|
* @feature_table: an array of feature numbers supported by this driver.
|
2008-05-03 09:50:50 +07:00
|
|
|
* @feature_table_size: number of entries in the feature table array.
|
2014-10-23 22:07:47 +07:00
|
|
|
* @feature_table_legacy: same as feature_table but when working in legacy mode.
|
|
|
|
* @feature_table_size_legacy: number of entries in feature table legacy array.
|
2009-06-13 11:16:33 +07:00
|
|
|
* @probe: the function to call when a device is found. Returns 0 or -errno.
|
2011-08-25 20:04:05 +07:00
|
|
|
* @remove: the function to call when a device is removed.
|
2008-02-05 11:49:58 +07:00
|
|
|
* @config_changed: optional function to call when the device configuration
|
|
|
|
* changes; may be called in interrupt context.
|
2007-10-22 08:03:36 +07:00
|
|
|
*/
|
|
|
|
struct virtio_driver {
|
|
|
|
struct device_driver driver;
|
|
|
|
const struct virtio_device_id *id_table;
|
2008-05-03 09:50:50 +07:00
|
|
|
const unsigned int *feature_table;
|
|
|
|
unsigned int feature_table_size;
|
2014-10-23 22:07:47 +07:00
|
|
|
const unsigned int *feature_table_legacy;
|
|
|
|
unsigned int feature_table_size_legacy;
|
2017-03-29 23:06:20 +07:00
|
|
|
int (*validate)(struct virtio_device *dev);
|
2007-10-22 08:03:36 +07:00
|
|
|
int (*probe)(struct virtio_device *dev);
|
2012-07-12 04:22:16 +07:00
|
|
|
void (*scan)(struct virtio_device *dev);
|
2007-10-22 08:03:36 +07:00
|
|
|
void (*remove)(struct virtio_device *dev);
|
2008-02-05 11:49:58 +07:00
|
|
|
void (*config_changed)(struct virtio_device *dev);
|
2011-12-22 18:28:26 +07:00
|
|
|
#ifdef CONFIG_PM
|
|
|
|
int (*freeze)(struct virtio_device *dev);
|
|
|
|
int (*restore)(struct virtio_device *dev);
|
|
|
|
#endif
|
2007-10-22 08:03:36 +07:00
|
|
|
};
|
|
|
|
|
2012-12-10 15:38:33 +07:00
|
|
|
static inline struct virtio_driver *drv_to_virtio(struct device_driver *drv)
|
|
|
|
{
|
|
|
|
return container_of(drv, struct virtio_driver, driver);
|
|
|
|
}
|
|
|
|
|
2007-10-22 08:03:36 +07:00
|
|
|
int register_virtio_driver(struct virtio_driver *drv);
|
|
|
|
void unregister_virtio_driver(struct virtio_driver *drv);
|
2013-02-13 12:22:36 +07:00
|
|
|
|
|
|
|
/* module_virtio_driver() - Helper macro for drivers that don't do
|
|
|
|
* anything special in module init/exit. This eliminates a lot of
|
|
|
|
* boilerplate. Each module may only use this macro once, and
|
|
|
|
* calling it replaces module_init() and module_exit()
|
|
|
|
*/
|
|
|
|
#define module_virtio_driver(__virtio_driver) \
|
|
|
|
module_driver(__virtio_driver, register_virtio_driver, \
|
|
|
|
unregister_virtio_driver)
|
2007-10-22 08:03:36 +07:00
|
|
|
#endif /* _LINUX_VIRTIO_H */
|