]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Include/IndustryStandard/Virtio.h
600d3d272b6a1865d5a348b1a91517415b6351d0
[mirror_edk2.git] / OvmfPkg / Include / IndustryStandard / Virtio.h
1 /** @file
2
3 Type and macro definitions corresponding to the virtio-0.9.5 specification.
4
5 Copyright (C) 2012, Red Hat, Inc.
6
7 This program and the accompanying materials are licensed and made available
8 under the terms and conditions of the BSD License which accompanies this
9 distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT
13 WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #ifndef _VIRTIO_H_
18 #define _VIRTIO_H_
19
20 #include <Base.h>
21
22
23 //
24 // Data in the communication area is defined as packed and accessed as
25 // volatile.
26 //
27 // Some structures contain arrays with dynamically determined size. In such
28 // cases the array and its sibling fields are replaced with pointers.
29 //
30 // All indices (variables and fields named *Idx) are free-running and wrap
31 // around after 0xFFFF. The queue size reported by the host is always an
32 // integral power of 2, not greater than 32768. Actual array indices are
33 // consistently calculated by taking the remainder of a given Idx object modulo
34 // QueueSize. Since 0x10000 is an integral multiple of the QueueSize, UINT16
35 // wraparound is a correct wraparound modulo QueueSize too (it doesn't offset
36 // the remainder class).
37 //
38 // virtio-0.9.5, 2.3.4 Available Ring
39 //
40 #define VRING_AVAIL_F_NO_INTERRUPT BIT0
41
42 typedef struct {
43 volatile UINT16 *Flags;
44 volatile UINT16 *Idx;
45
46 volatile UINT16 *Ring; // QueueSize elements
47 volatile UINT16 *UsedEvent; // unused as per negotiation
48 } VRING_AVAIL;
49
50
51 //
52 // virtio-0.9.5, 2.3.5 Used Ring
53 //
54 #define VRING_USED_F_NO_NOTIFY BIT0
55
56 #pragma pack(1)
57 typedef struct {
58 UINT32 Id;
59 UINT32 Len;
60 } VRING_USED_ELEM;
61 #pragma pack()
62
63 typedef struct {
64 volatile UINT16 *Flags;
65 volatile UINT16 *Idx;
66 volatile VRING_USED_ELEM *UsedElem; // QueueSize elements
67 volatile UINT16 *AvailEvent; // unused as per negotiation
68 } VRING_USED;
69
70
71 //
72 // virtio-0.9.5, 2.3.2 Descriptor Table
73 //
74 #define VRING_DESC_F_NEXT BIT0 // more descriptors in this request
75 #define VRING_DESC_F_WRITE BIT1 // buffer to be written *by the host*
76 #define VRING_DESC_F_INDIRECT BIT2 // unused
77
78 #pragma pack(1)
79 typedef struct {
80 UINT64 Addr;
81 UINT32 Len;
82 UINT16 Flags;
83 UINT16 Next;
84 } VRING_DESC;
85 #pragma pack()
86
87 typedef struct {
88 UINTN NumPages;
89 VOID *Base; // deallocate only this field
90 volatile VRING_DESC *Desc; // QueueSize elements
91 VRING_AVAIL Avail;
92 VRING_USED Used;
93 UINT16 QueueSize;
94 } VRING;
95
96
97 //
98 // virtio-0.9.5, 2.2.2 Virtio Header -- no MSI-X
99 // virtio-0.9.5, Appendix D
100 //
101 #pragma pack(1)
102 typedef struct {
103 UINT32 VhdrDeviceFeatureBits;
104 UINT32 VhdrGuestFeatureBits;
105 UINT32 VhdrQueueAddress;
106 UINT16 VhdrQueueSize;
107 UINT16 VhdrQueueSelect;
108 UINT16 VhdrQueueNotify;
109 UINT8 VhdrDeviceStatus;
110 UINT8 VhdrISR;
111 UINT64 VhdrCapacity;
112 UINT32 VhdrSizeMax;
113 UINT32 VhdrSegMax;
114 UINT16 VhdrCylinders;
115 UINT8 VhdrHeads;
116 UINT8 VhdrSectors;
117 UINT32 VhdrBlkSize;
118 } VBLK_HDR;
119 #pragma pack()
120
121 #define OFFSET_OF_VHDR(Field) OFFSET_OF (VBLK_HDR, Field)
122 #define SIZE_OF_VHDR(Field) (sizeof ((VBLK_HDR *) 0)->Field)
123
124
125 //
126 // virtio-0.9.5, 2.2.2.1 Device Status
127 //
128 #define VSTAT_ACK BIT0
129 #define VSTAT_DRIVER BIT1
130 #define VSTAT_DRIVER_OK BIT2
131 #define VSTAT_FAILED BIT7
132
133 //
134 // virtio-0.9.5, Appendix B: Reserved (Device-Independent) Feature Bits
135 //
136 #define VIRTIO_F_NOTIFY_ON_EMPTY BIT24
137 #define VIRTIO_F_RING_INDIRECT_DESC BIT28
138 #define VIRTIO_F_RING_EVENT_IDX BIT29
139
140 //
141 // virtio-0.9.5, Appendix D: Block Device
142 //
143 #define VIRTIO_BLK_F_BARRIER BIT0
144 #define VIRTIO_BLK_F_SIZE_MAX BIT1
145 #define VIRTIO_BLK_F_SEG_MAX BIT2
146 #define VIRTIO_BLK_F_GEOMETRY BIT4
147 #define VIRTIO_BLK_F_RO BIT5
148 #define VIRTIO_BLK_F_BLK_SIZE BIT6 // treated as "logical block size" in
149 // practice; actual host side implementation
150 // negotiates "optimal" block size
151 // separately
152 #define VIRTIO_BLK_F_SCSI BIT7
153 #define VIRTIO_BLK_F_FLUSH BIT9 // identical to "write cache enabled"
154
155
156 //
157 // We keep the status byte separate from the rest of the virtio-blk request
158 // header. See description of historical scattering at the end of Appendix D:
159 // we're going to put the status byte in a separate VRING_DESC.
160 //
161 #pragma pack(1)
162 typedef struct {
163 UINT32 Type;
164 UINT32 IoPrio;
165 UINT64 Sector;
166 } VIRTIO_BLK_REQ;
167 #pragma pack()
168
169 #define VIRTIO_BLK_T_IN 0x00000000
170 #define VIRTIO_BLK_T_OUT 0x00000001
171 #define VIRTIO_BLK_T_SCSI_CMD 0x00000002
172 #define VIRTIO_BLK_T_SCSI_CMD_OUT 0x00000003
173 #define VIRTIO_BLK_T_FLUSH 0x00000004
174 #define VIRTIO_BLK_T_FLUSH_OUT 0x00000005
175 #define VIRTIO_BLK_T_BARRIER BIT31
176
177 #define VIRTIO_BLK_S_OK 0x00
178 #define VIRTIO_BLK_S_IOERR 0x01
179 #define VIRTIO_BLK_S_UNSUPP 0x02
180
181 #endif // _VIRTIO_H_