| Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 1 | #ifndef _LINUX_VIRTIO_CONFIG_H | 
 | 2 | #define _LINUX_VIRTIO_CONFIG_H | 
 | 3 | /* Virtio devices use a standardized configuration space to define their | 
 | 4 |  * features and pass configuration information, but each implementation can | 
 | 5 |  * store and access that space differently. */ | 
 | 6 | #include <linux/types.h> | 
 | 7 |  | 
| Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 8 | /* Status byte for guest to report progress, and synchronize features. */ | 
| Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 9 | /* We have seen device and processed generic fields (VIRTIO_CONFIG_F_VIRTIO) */ | 
 | 10 | #define VIRTIO_CONFIG_S_ACKNOWLEDGE	1 | 
 | 11 | /* We have found a driver for the device. */ | 
 | 12 | #define VIRTIO_CONFIG_S_DRIVER		2 | 
 | 13 | /* Driver has used its parts of the config, and is happy */ | 
 | 14 | #define VIRTIO_CONFIG_S_DRIVER_OK	4 | 
 | 15 | /* We've given up on this device. */ | 
 | 16 | #define VIRTIO_CONFIG_S_FAILED		0x80 | 
 | 17 |  | 
| Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 18 | #ifdef __KERNEL__ | 
 | 19 | struct virtio_device; | 
 | 20 |  | 
 | 21 | /** | 
 | 22 |  * virtio_config_ops - operations for configuring a virtio device | 
| Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 23 |  * @feature: search for a feature in this config | 
| Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 24 |  *	vdev: the virtio_device | 
| Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 25 |  *	bit: the feature bit | 
 | 26 |  *	Returns true if the feature is supported.  Acknowledges the feature | 
 | 27 |  *	so the host can see it. | 
 | 28 |  * @get: read the value of a configuration field | 
| Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 29 |  *	vdev: the virtio_device | 
| Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 30 |  *	offset: the offset of the configuration field | 
| Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 31 |  *	buf: the buffer to write the field value into. | 
| Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 32 |  *	len: the length of the buffer | 
| Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 33 |  *	Note that contents are conventionally little-endian. | 
| Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 34 |  * @set: write the value of a configuration field | 
| Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 35 |  *	vdev: the virtio_device | 
| Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 36 |  *	offset: the offset of the configuration field | 
| Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 37 |  *	buf: the buffer to read the field value from. | 
| Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 38 |  *	len: the length of the buffer | 
| Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 39 |  *	Note that contents are conventionally little-endian. | 
 | 40 |  * @get_status: read the status byte | 
 | 41 |  *	vdev: the virtio_device | 
 | 42 |  *	Returns the status byte | 
 | 43 |  * @set_status: write the status byte | 
 | 44 |  *	vdev: the virtio_device | 
 | 45 |  *	status: the new status byte | 
| Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 46 |  * @reset: reset the device | 
 | 47 |  *	vdev: the virtio device | 
 | 48 |  *	After this, status and feature negotiation must be done again | 
| Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 49 |  * @find_vq: find a virtqueue and instantiate it. | 
| Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 50 |  *	vdev: the virtio_device | 
| Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 51 |  *	index: the 0-based virtqueue number in case there's more than one. | 
| Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 52 |  *	callback: the virqtueue callback | 
| Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 53 |  *	Returns the new virtqueue or ERR_PTR() (eg. -ENOENT). | 
| Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 54 |  * @del_vq: free a virtqueue found by find_vq(). | 
 | 55 |  */ | 
 | 56 | struct virtio_config_ops | 
 | 57 | { | 
| Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 58 | 	bool (*feature)(struct virtio_device *vdev, unsigned bit); | 
 | 59 | 	void (*get)(struct virtio_device *vdev, unsigned offset, | 
| Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 60 | 		    void *buf, unsigned len); | 
| Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 61 | 	void (*set)(struct virtio_device *vdev, unsigned offset, | 
| Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 62 | 		    const void *buf, unsigned len); | 
 | 63 | 	u8 (*get_status)(struct virtio_device *vdev); | 
 | 64 | 	void (*set_status)(struct virtio_device *vdev, u8 status); | 
| Rusty Russell | 6e5aa7e | 2008-02-04 23:50:03 -0500 | [diff] [blame] | 65 | 	void (*reset)(struct virtio_device *vdev); | 
| Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 66 | 	struct virtqueue *(*find_vq)(struct virtio_device *vdev, | 
| Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 67 | 				     unsigned index, | 
| Rusty Russell | 18445c4 | 2008-02-04 23:49:57 -0500 | [diff] [blame] | 68 | 				     void (*callback)(struct virtqueue *)); | 
| Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 69 | 	void (*del_vq)(struct virtqueue *vq); | 
 | 70 | }; | 
 | 71 |  | 
 | 72 | /** | 
| Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 73 |  * virtio_config_val - look for a feature and get a single virtio config. | 
 | 74 |  * @vdev: the virtio device | 
 | 75 |  * @fbit: the feature bit | 
 | 76 |  * @offset: the type to search for. | 
| Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 77 |  * @val: a pointer to the value to fill in. | 
 | 78 |  * | 
| Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 79 |  * The return value is -ENOENT if the feature doesn't exist.  Otherwise | 
 | 80 |  * the value is endian-corrected and returned in v. */ | 
 | 81 | #define virtio_config_val(vdev, fbit, offset, v) ({			\ | 
 | 82 | 	int _err;							\ | 
 | 83 | 	if ((vdev)->config->feature((vdev), (fbit))) {			\ | 
 | 84 | 		__virtio_config_val((vdev), (offset), (v));		\ | 
 | 85 | 		_err = 0;						\ | 
 | 86 | 	} else								\ | 
 | 87 | 		_err = -ENOENT;						\ | 
| Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 88 | 	_err;								\ | 
 | 89 | }) | 
 | 90 |  | 
| Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 91 | /** | 
| Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 92 |  * __virtio_config_val - get a single virtio config without feature check. | 
 | 93 |  * @vdev: the virtio device | 
 | 94 |  * @offset: the type to search for. | 
 | 95 |  * @val: a pointer to the value to fill in. | 
| Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 96 |  * | 
| Rusty Russell | a586d4f | 2008-02-04 23:49:56 -0500 | [diff] [blame] | 97 |  * The value is endian-corrected and returned in v. */ | 
 | 98 | #define __virtio_config_val(vdev, offset, v) do {			\ | 
 | 99 | 	BUILD_BUG_ON(sizeof(*(v)) != 1 && sizeof(*(v)) != 2		\ | 
 | 100 | 		     && sizeof(*(v)) != 4 && sizeof(*(v)) != 8);	\ | 
 | 101 | 	(vdev)->config->get((vdev), (offset), (v), sizeof(*(v)));	\ | 
 | 102 | 	switch (sizeof(*(v))) {						\ | 
 | 103 | 	case 2: le16_to_cpus((__u16 *) v); break;			\ | 
 | 104 | 	case 4: le32_to_cpus((__u32 *) v); break;			\ | 
 | 105 | 	case 8: le64_to_cpus((__u64 *) v); break;			\ | 
 | 106 | 	}								\ | 
 | 107 | } while(0) | 
| Rusty Russell | ec3d41c | 2007-10-22 11:03:36 +1000 | [diff] [blame] | 108 | #endif /* __KERNEL__ */ | 
 | 109 | #endif /* _LINUX_VIRTIO_CONFIG_H */ |