]> git.proxmox.com Git - mirror_edk2.git/blob - OvmfPkg/Include/IndustryStandard/Virtio.h
OvmfPkg: rename OFFSET_OF_VHDR() / SIZE_OF_VHDR() to *_OF_VBLK()
[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 //
100 #pragma pack(1)
101 typedef struct {
102 UINT32 VhdrDeviceFeatureBits;
103 UINT32 VhdrGuestFeatureBits;
104 UINT32 VhdrQueueAddress;
105 UINT16 VhdrQueueSize;
106 UINT16 VhdrQueueSelect;
107 UINT16 VhdrQueueNotify;
108 UINT8 VhdrDeviceStatus;
109 UINT8 VhdrISR;
110 } VIRTIO_HDR;
111
112 //
113 // virtio-0.9.5, Appendix D: Block Device
114 //
115 typedef struct {
116 VIRTIO_HDR Generic;
117 UINT64 VhdrCapacity;
118 UINT32 VhdrSizeMax;
119 UINT32 VhdrSegMax;
120 UINT16 VhdrCylinders;
121 UINT8 VhdrHeads;
122 UINT8 VhdrSectors;
123 UINT32 VhdrBlkSize;
124 } VBLK_HDR;
125 #pragma pack()
126
127 #define OFFSET_OF_VBLK(Field) OFFSET_OF (VBLK_HDR, Field)
128 #define SIZE_OF_VBLK(Field) (sizeof ((VBLK_HDR *) 0)->Field)
129
130
131 //
132 // virtio-0.9.5, 2.2.2.1 Device Status
133 //
134 #define VSTAT_ACK BIT0
135 #define VSTAT_DRIVER BIT1
136 #define VSTAT_DRIVER_OK BIT2
137 #define VSTAT_FAILED BIT7
138
139 //
140 // virtio-0.9.5, Appendix B: Reserved (Device-Independent) Feature Bits
141 //
142 #define VIRTIO_F_NOTIFY_ON_EMPTY BIT24
143 #define VIRTIO_F_RING_INDIRECT_DESC BIT28
144 #define VIRTIO_F_RING_EVENT_IDX BIT29
145
146 //
147 // virtio-0.9.5, Appendix D: Block Device
148 //
149 #define VIRTIO_BLK_F_BARRIER BIT0
150 #define VIRTIO_BLK_F_SIZE_MAX BIT1
151 #define VIRTIO_BLK_F_SEG_MAX BIT2
152 #define VIRTIO_BLK_F_GEOMETRY BIT4
153 #define VIRTIO_BLK_F_RO BIT5
154 #define VIRTIO_BLK_F_BLK_SIZE BIT6 // treated as "logical block size" in
155 // practice; actual host side implementation
156 // negotiates "optimal" block size
157 // separately
158 #define VIRTIO_BLK_F_SCSI BIT7
159 #define VIRTIO_BLK_F_FLUSH BIT9 // identical to "write cache enabled"
160
161
162 //
163 // We keep the status byte separate from the rest of the virtio-blk request
164 // header. See description of historical scattering at the end of Appendix D:
165 // we're going to put the status byte in a separate VRING_DESC.
166 //
167 #pragma pack(1)
168 typedef struct {
169 UINT32 Type;
170 UINT32 IoPrio;
171 UINT64 Sector;
172 } VIRTIO_BLK_REQ;
173 #pragma pack()
174
175 #define VIRTIO_BLK_T_IN 0x00000000
176 #define VIRTIO_BLK_T_OUT 0x00000001
177 #define VIRTIO_BLK_T_SCSI_CMD 0x00000002
178 #define VIRTIO_BLK_T_SCSI_CMD_OUT 0x00000003
179 #define VIRTIO_BLK_T_FLUSH 0x00000004
180 #define VIRTIO_BLK_T_FLUSH_OUT 0x00000005
181 #define VIRTIO_BLK_T_BARRIER BIT31
182
183 #define VIRTIO_BLK_S_OK 0x00
184 #define VIRTIO_BLK_S_IOERR 0x01
185 #define VIRTIO_BLK_S_UNSUPP 0x02
186
187 #endif // _VIRTIO_H_