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