ReactOS 0.4.16-dev-125-g798ea90
virtio_pci.h
Go to the documentation of this file.
1/*
2* Virtio PCI driver
3*
4* This module allows virtio devices to be used over a virtual PCI device.
5* This can be used with QEMU based VMMs like KVM or Xen.
6*
7* Copyright IBM Corp. 2007
8*
9* Authors:
10* Anthony Liguori <aliguori@us.ibm.com>
11*
12* This header is BSD licensed so anyone can use the definitions to implement
13* compatible drivers/servers.
14*
15* Redistribution and use in source and binary forms, with or without
16* modification, are permitted provided that the following conditions
17* are met:
18* 1. Redistributions of source code must retain the above copyright
19* notice, this list of conditions and the following disclaimer.
20* 2. Redistributions in binary form must reproduce the above copyright
21* notice, this list of conditions and the following disclaimer in the
22* documentation and/or other materials provided with the distribution.
23* 3. Neither the name of IBM nor the names of its contributors
24* may be used to endorse or promote products derived from this software
25* without specific prior written permission.
26* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
27* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29* ARE DISCLAIMED. IN NO EVENT SHALL IBM OR CONTRIBUTORS BE LIABLE
30* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36* SUCH DAMAGE.
37*/
38
39#ifndef _LINUX_VIRTIO_PCI_H
40#define _LINUX_VIRTIO_PCI_H
41
42#include "linux/types.h"
43#include "linux/virtio_config.h"
44
45#ifndef VIRTIO_PCI_NO_LEGACY
46
47/* A 32-bit r/o bitmask of the features supported by the host */
48#define VIRTIO_PCI_HOST_FEATURES 0
49
50/* A 32-bit r/w bitmask of features activated by the guest */
51#define VIRTIO_PCI_GUEST_FEATURES 4
52
53/* A 32-bit r/w PFN for the currently selected queue */
54#define VIRTIO_PCI_QUEUE_PFN 8
55
56/* A 16-bit r/o queue size for the currently selected queue */
57#define VIRTIO_PCI_QUEUE_NUM 12
58
59/* A 16-bit r/w queue selector */
60#define VIRTIO_PCI_QUEUE_SEL 14
61
62/* A 16-bit r/w queue notifier */
63#define VIRTIO_PCI_QUEUE_NOTIFY 16
64
65/* An 8-bit device status register. */
66#define VIRTIO_PCI_STATUS 18
67
68/* An 8-bit r/o interrupt status register. Reading the value will return the
69* current contents of the ISR and will also clear it. This is effectively
70* a read-and-acknowledge. */
71#define VIRTIO_PCI_ISR 19
72
73/* MSI-X registers: only enabled if MSI-X is enabled. */
74/* A 16-bit vector for configuration changes. */
75#define VIRTIO_MSI_CONFIG_VECTOR 20
76/* A 16-bit vector for selected queue notifications. */
77#define VIRTIO_MSI_QUEUE_VECTOR 22
78
79/* The remaining space is defined by each driver as the per-driver
80* configuration space */
81#define VIRTIO_PCI_CONFIG_OFF(msix_enabled) ((msix_enabled) ? 24 : 20)
82/* Deprecated: please use VIRTIO_PCI_CONFIG_OFF instead */
83#define VIRTIO_PCI_CONFIG(msix_enabled) VIRTIO_PCI_CONFIG_OFF(msix_enabled)
84
85/* How many bits to shift physical queue address written to QUEUE_PFN.
86* 12 is historical, and due to x86 page size. */
87#define VIRTIO_PCI_QUEUE_ADDR_SHIFT 12
88
89/* The alignment to use between consumer and producer parts of vring.
90* x86 pagesize again. */
91#define VIRTIO_PCI_VRING_ALIGN 4096
92
93#endif /* VIRTIO_PCI_NO_LEGACY */
94
95/* The bit of the ISR which indicates a device configuration change. */
96#define VIRTIO_PCI_ISR_CONFIG 0x2
97/* Vector value used to disable MSI for queue */
98#define VIRTIO_MSI_NO_VECTOR 0xffff
99
100/* IDs for different capabilities. Must all exist. */
101
102/* Common configuration */
103#define VIRTIO_PCI_CAP_COMMON_CFG 1
104/* Notifications */
105#define VIRTIO_PCI_CAP_NOTIFY_CFG 2
106/* ISR access */
107#define VIRTIO_PCI_CAP_ISR_CFG 3
108/* Device specific configuration */
109#define VIRTIO_PCI_CAP_DEVICE_CFG 4
110/* PCI configuration access */
111#define VIRTIO_PCI_CAP_PCI_CFG 5
112
113/* This is the PCI capability header: */
115 __u8 cap_vndr; /* Generic PCI field: PCI_CAPABILITY_ID_VENDOR_SPECIFIC */
116 __u8 cap_next; /* Generic PCI field: next ptr. */
117 __u8 cap_len; /* Generic PCI field: capability length */
118 __u8 cfg_type; /* Identifies the structure. */
119 __u8 bar; /* Where to find it. */
120 __u8 padding[3]; /* Pad to full dword. */
121 __le32 offset; /* Offset within bar. */
122 __le32 length; /* Length of the structure, in bytes. */
123};
124
127 __le32 notify_off_multiplier; /* Multiplier for queue_notify_off. */
128};
129
130/* Fields in VIRTIO_PCI_CAP_COMMON_CFG: */
132 /* About the whole device. */
134 __le32 device_feature; /* read-only */
135 __le32 guest_feature_select; /* read-write */
136 __le32 guest_feature; /* read-write */
137 __le16 msix_config; /* read-write */
138 __le16 num_queues; /* read-only */
139 __u8 device_status; /* read-write */
140 __u8 config_generation; /* read-only */
141
142 /* About a specific virtqueue. */
143 __le16 queue_select; /* read-write */
144 __le16 queue_size; /* read-write, power of 2. */
145 __le16 queue_msix_vector; /* read-write */
146 __le16 queue_enable; /* read-write */
147 __le16 queue_notify_off; /* read-only */
148 __le32 queue_desc_lo; /* read-write */
149 __le32 queue_desc_hi; /* read-write */
150 __le32 queue_avail_lo; /* read-write */
151 __le32 queue_avail_hi; /* read-write */
152 __le32 queue_used_lo; /* read-write */
153 __le32 queue_used_hi; /* read-write */
154};
155
156#define MAX_QUEUES_PER_DEVICE_DEFAULT 8
157
158typedef struct virtio_queue_info
159{
160 /* the actual virtqueue */
161 struct virtqueue *vq;
162 /* the number of entries in the queue */
164 /* the virtual address of the ring queue */
165 void *queue;
167
168typedef struct virtio_system_ops {
169 // device register access
173 void (*vdev_write_byte)(ULONG_PTR ulRegister, u8 bValue);
174 void (*vdev_write_word)(ULONG_PTR ulRegister, u16 wValue);
175 void (*vdev_write_dword)(ULONG_PTR ulRegister, u32 ulValue);
176
177 // memory management
178 void *(*mem_alloc_contiguous_pages)(void *context, size_t size);
181 void *(*mem_alloc_nonpaged_block)(void *context, size_t size);
183
184 // PCI config space access
185 int (*pci_read_config_byte)(void *context, int where, u8 *bVal);
186 int (*pci_read_config_word)(void *context, int where, u16 *wVal);
187 int (*pci_read_config_dword)(void *context, int where, u32 *dwVal);
188
189 // PCI resource handling
191 void *(*pci_map_address_range)(void *context, int bar, size_t offset, size_t maxlen);
192
193 // misc
195 void (*vdev_sleep)(void *context, unsigned int msecs);
197
198struct virtio_device;
200
202{
203 // read/write device config and read config generation counter
204 void (*get_config)(VirtIODevice *vdev, unsigned offset, void *buf, unsigned len);
205 void (*set_config)(VirtIODevice *vdev, unsigned offset, const void *buf, unsigned len);
207
208 // read/write device status byte and reset the device
212
213 // get/set device feature bits
216
217 // set config/queue MSI interrupt vector, returns the new vector
220
221 // query virtual queue size and memory requirements
223 unsigned index, unsigned short *pNumEntries,
224 unsigned long *pRingSize,
225 unsigned long *pHeapSize);
226
227 // allocate and initialize a queue
230 unsigned idx, u16 msix_vec);
231
232 // tear down and deallocate a queue
234};
235
237{
238 // the I/O port BAR of the PCI device (legacy virtio devices only)
240
241 // true if the device uses MSI interrupts
243
244 // true if the VIRTIO_RING_F_EVENT_IDX feature flag has been negotiated
246
247 // true if the VIRTIO_F_RING_PACKED feature flag has been negotiated
249
250 // internal device operations, implemented separately for legacy and modern
252
253 // external callbacks implemented separately by different driver model drivers
255
256 // opaque context value passed as first argument to virtio_system_ops callbacks
258
259 // the ISR status field, reading causes the device to de-assert an interrupt
260 volatile u8 *isr;
261
262 // modern virtio device capabilities and related state
264 volatile unsigned char *config;
265 volatile unsigned char *notify_base;
268
271
272 // maximum number of virtqueues that fit in the memory block pointed to by info
274
275 // points to inline_info if not more than MAX_QUEUES_PER_DEVICE_DEFAULT queues
276 // are used, or to an external allocation otherwise
279};
280
281/* Driver API: device init and shutdown
282 * DeviceContext is a driver defined opaque value which will be passed to driver
283 * supplied callbacks described in pSystemOps. pSystemOps must be non-NULL and all
284 * its fields must be non-NULL. msix_used is true if and only if the device is
285 * configured with MSI support.
286 */
288 const VirtIOSystemOps *pSystemOps,
289 void *DeviceContext,
290 bool msix_used);
292
293/* Driver API: device status manipulation
294 * virtio_set_status should not be called by new drivers. Device status should only
295 * be getting its bits set with virtio_add_status and reset all back to 0 with
296 * virtio_device_reset. virtio_device_ready is a special version of virtio_add_status
297 * which adds the VIRTIO_CONFIG_S_DRIVER_OK status bit.
298 */
302
305
306/* Driver API: device feature bitmap manipulation
307 * Features passed to virtio_set_features should be a subset of features offered by
308 * the device as returned from virtio_get_features. virtio_set_features sets the
309 * VIRTIO_CONFIG_S_FEATURES_OK status bit if it is supported by the device.
310 */
311#define virtio_is_feature_enabled(FeaturesList, Feature) (!!((FeaturesList) & (1ULL << (Feature))))
312#define virtio_feature_enable(FeaturesList, Feature) ((FeaturesList) |= (1ULL << (Feature)))
313#define virtio_feature_disable(FeaturesList, Feature) ((FeaturesList) &= ~(1ULL << (Feature)))
314
317
318/* Driver API: device configuration access
319 * Both virtio_get_config and virtio_set_config support arbitrary values of the len
320 * parameter. Config items of length 1, 2, and 4 are read/written using one access,
321 * length 8 is broken down to two 4 bytes accesses, and any other length is read or
322 * written byte by byte.
323 */
324void virtio_get_config(VirtIODevice *vdev, unsigned offset,
325 void *buf, unsigned len);
326void virtio_set_config(VirtIODevice *vdev, unsigned offset,
327 void *buf, unsigned len);
328
329/* Driver API: virtqueue setup
330 * virtio_reserve_queue_memory makes VirtioLib reserve memory for its virtqueue
331 * bookkeeping. Drivers should call this function if they intend to set up queues
332 * one by one with virtio_find_queue. virtio_find_queues (plural) internally takes
333 * care of the reservation and virtio_reserve_queue_memory need not be called.
334 * Note that in addition to queue interrupt vectors, virtio_find_queues also sets
335 * up the device config vector as a convenience.
336 * Drivers should treat the returned struct virtqueue pointers as opaque handles.
337 */
339 unsigned short *pNumEntries,
340 unsigned long *pRingSize,
341 unsigned long *pHeapSize);
342
344
346 struct virtqueue **vq);
347NTSTATUS virtio_find_queues(VirtIODevice *vdev, unsigned nvqs,
348 struct virtqueue *vqs[]);
349
350/* Driver API: virtqueue shutdown
351 * The device must be reset and re-initialized to re-setup queues after they have
352 * been deleted.
353 */
354void virtio_delete_queue(struct virtqueue *vq);
356
357/* Driver API: virtqueue query and manipulation
358 * virtio_get_queue_descriptor_size
359 * is useful in situations where the driver has to prepare for the memory allocation
360 * performed by virtio_reserve_queue_memory beforehand.
361 */
362
365
367{
368 return sizeof(VirtIOQueueInfo);
369}
370
371/* Driver API: interrupt handling
372 * virtio_set_config_vector and virtio_set_queue_vector set the MSI vector used for
373 * device configuration interrupt and queue interrupt, respectively. The driver may
374 * choose to either return the vector from the vdev_get_msix_vector callback (called
375 * as part of queue setup) or call these functions later. Note that setting the vector
376 * may fail which is indicated by the return value of VIRTIO_MSI_NO_VECTOR.
377 * virtio_read_isr_status returns the value of the ISR status register, note that it
378 * is not idempotent, calling the function makes the device de-assert the interrupt.
379 */
382
384
385/* Driver API: miscellaneous helpers
386 * virtio_get_bar_index returns the corresponding BAR index given its physical address.
387 * This tends to be useful to all drivers since Windows doesn't provide reliable BAR
388 * indices as part of resource enumeration. The function returns -1 on failure.
389 */
391
392#endif
LONG NTSTATUS
Definition: precomp.h:26
#define index(s, c)
Definition: various.h:29
u8 __u8
Definition: btrfs.h:17
ULONG32 u32
Definition: btrfs.h:14
ULONG64 u64
Definition: btrfs.h:15
UCHAR u8
Definition: btrfs.h:12
USHORT u16
Definition: btrfs.h:13
Definition: _queue.h:67
unsigned int idx
Definition: utils.c:41
#define NTSTATUS
Definition: precomp.h:21
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
#define __le16
Definition: types.h:43
#define __le32
Definition: types.h:44
__kernel_size_t size_t
Definition: linux.h:237
void bar()
Definition: ehthrow.cxx:142
GLsizeiptr size
Definition: glext.h:5919
GLboolean reset
Definition: glext.h:5666
GLuint index
Definition: glext.h:6031
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLenum const GLvoid * addr
Definition: glext.h:9621
GLenum GLsizei len
Definition: glext.h:6722
GLenum cap
Definition: glext.h:9639
GLintptr offset
Definition: glext.h:5920
_In_ PNDIS_STRING _In_opt_ NDIS_HANDLE DeviceContext
Definition: ndis.h:5246
#define u16
Definition: types.h:8
#define u32
Definition: types.h:9
#define u64
Definition: types.h:10
#define u8
Definition: types.h:7
Definition: http.c:7252
Definition: ps.c:97
NTSTATUS(* set_features)(VirtIODevice *vdev, u64 features)
Definition: virtio_pci.h:215
u16(* set_config_vector)(VirtIODevice *vdev, u16 vector)
Definition: virtio_pci.h:218
void(* get_config)(VirtIODevice *vdev, unsigned offset, void *buf, unsigned len)
Definition: virtio_pci.h:204
u8(* get_status)(VirtIODevice *vdev)
Definition: virtio_pci.h:209
void(* set_config)(VirtIODevice *vdev, unsigned offset, const void *buf, unsigned len)
Definition: virtio_pci.h:205
NTSTATUS(* setup_queue)(struct virtqueue **queue, VirtIODevice *vdev, VirtIOQueueInfo *info, unsigned idx, u16 msix_vec)
Definition: virtio_pci.h:228
void(* set_status)(VirtIODevice *vdev, u8 status)
Definition: virtio_pci.h:210
u16(* set_queue_vector)(struct virtqueue *vq, u16 vector)
Definition: virtio_pci.h:219
void(* delete_queue)(VirtIOQueueInfo *info)
Definition: virtio_pci.h:233
u64(* get_features)(VirtIODevice *vdev)
Definition: virtio_pci.h:214
u32(* get_config_generation)(VirtIODevice *vdev)
Definition: virtio_pci.h:206
NTSTATUS(* query_queue_alloc)(VirtIODevice *vdev, unsigned index, unsigned short *pNumEntries, unsigned long *pRingSize, unsigned long *pHeapSize)
Definition: virtio_pci.h:222
ULONG maxQueues
Definition: virtio_pci.h:273
bool event_suppression_enabled
Definition: virtio_pci.h:245
volatile unsigned char * config
Definition: virtio_pci.h:264
volatile u8 * isr
Definition: virtio_pci.h:260
volatile struct virtio_pci_common_cfg * common
Definition: virtio_pci.h:263
size_t notify_len
Definition: virtio_pci.h:270
int notify_map_cap
Definition: virtio_pci.h:266
VirtIOQueueInfo * info
Definition: virtio_pci.h:277
ULONG_PTR addr
Definition: virtio_pci.h:239
const struct virtio_system_ops * system
Definition: virtio_pci.h:254
const struct virtio_device_ops * device
Definition: virtio_pci.h:251
volatile unsigned char * notify_base
Definition: virtio_pci.h:265
size_t config_len
Definition: virtio_pci.h:269
u32 notify_offset_multiplier
Definition: virtio_pci.h:267
void * DeviceContext
Definition: virtio_pci.h:257
VirtIOQueueInfo inline_info[MAX_QUEUES_PER_DEVICE_DEFAULT]
Definition: virtio_pci.h:278
bool packed_ring
Definition: virtio_pci.h:248
__u8 padding[3]
Definition: virtio_pci.h:120
__le32 device_feature_select
Definition: virtio_pci.h:133
__le32 notify_off_multiplier
Definition: virtio_pci.h:127
struct virtqueue * vq
Definition: virtio_pci.h:161
void(* mem_free_nonpaged_block)(void *context, void *addr)
Definition: virtio_pci.h:182
void(* mem_free_contiguous_pages)(void *context, void *virt)
Definition: virtio_pci.h:179
u16(* vdev_read_word)(ULONG_PTR ulRegister)
Definition: virtio_pci.h:171
void(* vdev_sleep)(void *context, unsigned int msecs)
Definition: virtio_pci.h:195
void(* vdev_write_byte)(ULONG_PTR ulRegister, u8 bValue)
Definition: virtio_pci.h:173
u16(* vdev_get_msix_vector)(void *context, int queue)
Definition: virtio_pci.h:194
u8(* vdev_read_byte)(ULONG_PTR ulRegister)
Definition: virtio_pci.h:170
void(* vdev_write_dword)(ULONG_PTR ulRegister, u32 ulValue)
Definition: virtio_pci.h:175
u32(* vdev_read_dword)(ULONG_PTR ulRegister)
Definition: virtio_pci.h:172
size_t(* pci_get_resource_len)(void *context, int bar)
Definition: virtio_pci.h:190
int(* pci_read_config_dword)(void *context, int where, u32 *dwVal)
Definition: virtio_pci.h:187
int(* pci_read_config_byte)(void *context, int where, u8 *bVal)
Definition: virtio_pci.h:185
ULONGLONG(* mem_get_physical_address)(void *context, void *virt)
Definition: virtio_pci.h:180
void(* vdev_write_word)(ULONG_PTR ulRegister, u16 wValue)
Definition: virtio_pci.h:174
int(* pci_read_config_word)(void *context, int where, u16 *wVal)
Definition: virtio_pci.h:186
uint32_t ULONG_PTR
Definition: typedefs.h:65
uint32_t ULONG
Definition: typedefs.h:59
uint64_t ULONGLONG
Definition: typedefs.h:67
int virtio_get_bar_index(PPCI_COMMON_HEADER pPCIHeader, PHYSICAL_ADDRESS BasePA)
u32 virtio_get_queue_size(struct virtqueue *vq)
struct virtio_queue_info VirtIOQueueInfo
NTSTATUS virtio_find_queues(VirtIODevice *vdev, unsigned nvqs, struct virtqueue *vqs[])
NTSTATUS virtio_device_initialize(VirtIODevice *vdev, const VirtIOSystemOps *pSystemOps, void *DeviceContext, bool msix_used)
void virtio_device_shutdown(VirtIODevice *vdev)
u16 virtio_set_queue_vector(struct virtqueue *vq, u16 vector)
struct virtio_system_ops VirtIOSystemOps
void virtio_delete_queues(VirtIODevice *vdev)
#define MAX_QUEUES_PER_DEVICE_DEFAULT
Definition: virtio_pci.h:156
void virtio_set_status(VirtIODevice *vdev, u8 status)
void virtio_add_status(VirtIODevice *vdev, u8 status)
void virtio_device_ready(VirtIODevice *vdev)
void virtio_set_config(VirtIODevice *vdev, unsigned offset, void *buf, unsigned len)
static ULONG FORCEINLINE virtio_get_queue_descriptor_size()
Definition: virtio_pci.h:366
NTSTATUS virtio_find_queue(VirtIODevice *vdev, unsigned index, struct virtqueue **vq)
NTSTATUS virtio_reserve_queue_memory(VirtIODevice *vdev, unsigned nvqs)
u8 virtio_read_isr_status(VirtIODevice *vdev)
u64 virtio_get_features(VirtIODevice *dev)
u8 virtio_get_status(VirtIODevice *vdev)
void virtio_delete_queue(struct virtqueue *vq)
void virtio_device_reset(VirtIODevice *vdev)
NTSTATUS virtio_query_queue_allocation(VirtIODevice *vdev, unsigned index, unsigned short *pNumEntries, unsigned long *pRingSize, unsigned long *pHeapSize)
void virtio_get_config(VirtIODevice *vdev, unsigned offset, void *buf, unsigned len)
u16 virtio_set_config_vector(VirtIODevice *vdev, u16 vector)
unsigned long virtio_get_indirect_page_capacity()
Definition: VirtIORing.c:550
NTSTATUS virtio_set_features(VirtIODevice *vdev, u64 features)
#define FORCEINLINE
Definition: wdftypes.h:67