]>
Commit | Line | Data |
---|---|---|
263559b8 | 1 | /** @file\r |
2 | \r | |
3 | Declarations of utility functions used by virtio device drivers.\r | |
4 | \r | |
5 | Copyright (C) 2012, Red Hat, Inc.\r | |
6 | \r | |
7 | This program and the accompanying materials are licensed and made available\r | |
8 | under the terms and conditions of the BSD License which accompanies this\r | |
9 | distribution. The full text of the license may be found at\r | |
10 | http://opensource.org/licenses/bsd-license.php\r | |
11 | \r | |
12 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT\r | |
13 | WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
14 | \r | |
15 | **/\r | |
16 | \r | |
17 | #ifndef _VIRTIO_LIB_H_\r | |
18 | #define _VIRTIO_LIB_H_\r | |
19 | \r | |
56f65ed8 OM |
20 | #include <Protocol/VirtioDevice.h>\r |
21 | \r | |
263559b8 | 22 | #include <IndustryStandard/Virtio.h>\r |
23 | \r | |
24 | /**\r | |
25 | \r | |
56f65ed8 | 26 | Write a word into VirtIo Device Specific Region\r |
263559b8 | 27 | \r |
56f65ed8 OM |
28 | The VirtIo Device Specific Region must be an iomem region.\r |
29 | This is an internal function for the driver-specific VIRTIO_CFG_WRITE()\r | |
30 | macros.\r | |
263559b8 | 31 | \r |
56f65ed8 | 32 | @param[in] VirtIo Target Virtio device.\r |
263559b8 | 33 | \r |
34 | @param[in] FieldOffset Destination offset.\r | |
35 | \r | |
36 | @param[in] FieldSize Destination field size, must be in { 1, 2, 4, 8 }.\r | |
37 | \r | |
38 | @param[in] Value Little endian value to write, converted to UINT64.\r | |
39 | The least significant FieldSize bytes will be used.\r | |
40 | \r | |
41 | \r | |
56f65ed8 | 42 | @return Status code returned by VirtIo->WriteDevice().\r |
263559b8 | 43 | \r |
44 | **/\r | |
263559b8 | 45 | EFI_STATUS\r |
9de0355b | 46 | EFIAPI\r |
56f65ed8 OM |
47 | VirtioWriteDevice (\r |
48 | IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r | |
49 | IN UINTN FieldOffset,\r | |
50 | IN UINTN FieldSize,\r | |
51 | IN UINT64 Value\r | |
263559b8 | 52 | );\r |
53 | \r | |
54 | \r | |
55 | /**\r | |
56 | \r | |
56f65ed8 | 57 | Read a word from VirtIo Device Specific Region\r |
263559b8 | 58 | \r |
56f65ed8 OM |
59 | The VirtIo Device Specific Region must be an iomem region.\r |
60 | This is an internal function for the driver-specific VIRTIO_CFG_READ()\r | |
61 | macros.\r | |
263559b8 | 62 | \r |
56f65ed8 | 63 | @param[in] VirtIo Source Virtio device.\r |
263559b8 | 64 | \r |
65 | @param[in] FieldOffset Source offset.\r | |
66 | \r | |
67 | @param[in] FieldSize Source field size, must be in { 1, 2, 4, 8 }.\r | |
68 | \r | |
69 | @param[in] BufferSize Number of bytes available in the target buffer. Must\r | |
70 | equal FieldSize.\r | |
71 | \r | |
72 | @param[out] Buffer Target buffer.\r | |
73 | \r | |
74 | \r | |
56f65ed8 | 75 | @return Status code returned by VirtIo->ReadDevice().\r |
263559b8 | 76 | \r |
77 | **/\r | |
263559b8 | 78 | EFI_STATUS\r |
9de0355b | 79 | EFIAPI\r |
56f65ed8 OM |
80 | VirtioReadDevice (\r |
81 | IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r | |
82 | IN UINTN FieldOffset,\r | |
83 | IN UINTN FieldSize,\r | |
84 | IN UINTN BufferSize,\r | |
85 | OUT VOID *Buffer\r | |
263559b8 | 86 | );\r |
87 | \r | |
88 | \r | |
89 | /**\r | |
90 | \r | |
91 | Configure a virtio ring.\r | |
92 | \r | |
93 | This function sets up internal storage (the guest-host communication area)\r | |
94 | and lays out several "navigation" (ie. no-ownership) pointers to parts of\r | |
95 | that storage.\r | |
96 | \r | |
97 | Relevant sections from the virtio-0.9.5 spec:\r | |
98 | - 1.1 Virtqueues,\r | |
99 | - 2.3 Virtqueue Configuration.\r | |
100 | \r | |
101 | @param[in] The number of descriptors to allocate for the\r | |
102 | virtio ring, as requested by the host.\r | |
103 | \r | |
104 | @param[out] Ring The virtio ring to set up.\r | |
105 | \r | |
106 | @retval EFI_OUT_OF_RESOURCES AllocatePages() failed to allocate contiguous\r | |
107 | pages for the requested QueueSize. Fields of\r | |
108 | Ring have indeterminate value.\r | |
109 | \r | |
110 | @retval EFI_SUCCESS Allocation and setup successful. Ring->Base\r | |
111 | (and nothing else) is responsible for\r | |
112 | deallocation.\r | |
113 | \r | |
114 | **/\r | |
115 | EFI_STATUS\r | |
116 | EFIAPI\r | |
117 | VirtioRingInit (\r | |
118 | IN UINT16 QueueSize,\r | |
119 | OUT VRING *Ring\r | |
120 | );\r | |
121 | \r | |
122 | \r | |
123 | /**\r | |
124 | \r | |
125 | Tear down the internal resources of a configured virtio ring.\r | |
126 | \r | |
127 | The caller is responsible to stop the host from using this ring before\r | |
128 | invoking this function: the VSTAT_DRIVER_OK bit must be clear in\r | |
129 | VhdrDeviceStatus.\r | |
130 | \r | |
131 | @param[out] Ring The virtio ring to clean up.\r | |
132 | \r | |
133 | **/\r | |
134 | VOID\r | |
135 | EFIAPI\r | |
136 | VirtioRingUninit (\r | |
137 | IN OUT VRING *Ring\r | |
138 | );\r | |
139 | \r | |
140 | \r | |
e371e7e5 | 141 | //\r |
142 | // Internal use structure for tracking the submission of a multi-descriptor\r | |
143 | // request.\r | |
144 | //\r | |
145 | typedef struct {\r | |
635a3ca2 | 146 | UINT16 HeadDescIdx;\r |
147 | UINT16 NextDescIdx;\r | |
e371e7e5 | 148 | } DESC_INDICES;\r |
149 | \r | |
150 | \r | |
151 | /**\r | |
152 | \r | |
153 | Turn off interrupt notifications from the host, and prepare for appending\r | |
154 | multiple descriptors to the virtio ring.\r | |
155 | \r | |
156 | The calling driver must be in VSTAT_DRIVER_OK state.\r | |
157 | \r | |
f2965f4e | 158 | @param[in,out] Ring The virtio ring we intend to append descriptors to.\r |
e371e7e5 | 159 | \r |
160 | @param[out] Indices The DESC_INDICES structure to initialize.\r | |
161 | \r | |
162 | **/\r | |
163 | VOID\r | |
164 | EFIAPI\r | |
165 | VirtioPrepare (\r | |
166 | IN OUT VRING *Ring,\r | |
167 | OUT DESC_INDICES *Indices\r | |
168 | );\r | |
169 | \r | |
170 | \r | |
263559b8 | 171 | /**\r |
172 | \r | |
173 | Append a contiguous buffer for transmission / reception via the virtio ring.\r | |
174 | \r | |
635a3ca2 | 175 | This function implements the following section from virtio-0.9.5:\r |
263559b8 | 176 | - 2.4.1.1 Placing Buffers into the Descriptor Table\r |
263559b8 | 177 | \r |
178 | Free space is taken as granted, since the individual drivers support only\r | |
179 | synchronous requests and host side status is processed in lock-step with\r | |
180 | request submission. It is the calling driver's responsibility to verify the\r | |
181 | ring size in advance.\r | |
182 | \r | |
e371e7e5 | 183 | The caller is responsible for initializing *Indices with VirtioPrepare()\r |
184 | first.\r | |
185 | \r | |
11a5fdf4 | 186 | @param[in,out] Ring The virtio ring to append the buffer to, as a\r |
187 | descriptor.\r | |
188 | \r | |
189 | @param[in] BufferPhysAddr (Guest pseudo-physical) start address of the\r | |
190 | transmit / receive buffer.\r | |
191 | \r | |
192 | @param[in] BufferSize Number of bytes to transmit or receive.\r | |
193 | \r | |
194 | @param[in] Flags A bitmask of VRING_DESC_F_* flags. The caller\r | |
195 | computes this mask dependent on further buffers to\r | |
196 | append and transfer direction.\r | |
197 | VRING_DESC_F_INDIRECT is unsupported. The\r | |
198 | VRING_DESC.Next field is always set, but the host\r | |
199 | only interprets it dependent on VRING_DESC_F_NEXT.\r | |
200 | \r | |
201 | @param[in,out] Indices Indices->HeadDescIdx is not accessed.\r | |
202 | On input, Indices->NextDescIdx identifies the next\r | |
203 | descriptor to carry the buffer. On output,\r | |
204 | Indices->NextDescIdx is incremented by one, modulo\r | |
205 | 2^16.\r | |
263559b8 | 206 | \r |
207 | **/\r | |
208 | VOID\r | |
209 | EFIAPI\r | |
7fcacd6c | 210 | VirtioAppendDesc (\r |
e371e7e5 | 211 | IN OUT VRING *Ring,\r |
212 | IN UINTN BufferPhysAddr,\r | |
213 | IN UINT32 BufferSize,\r | |
214 | IN UINT16 Flags,\r | |
215 | IN OUT DESC_INDICES *Indices\r | |
216 | );\r | |
217 | \r | |
218 | \r | |
219 | /**\r | |
220 | \r | |
635a3ca2 | 221 | Notify the host about the descriptor chain just built, and wait until the\r |
222 | host processes it.\r | |
e371e7e5 | 223 | \r |
56f65ed8 | 224 | @param[in] VirtIo The target virtio device to notify.\r |
e371e7e5 | 225 | \r |
226 | @param[in] VirtQueueId Identifies the queue for the target device.\r | |
227 | \r | |
a7615fa8 | 228 | @param[in,out] Ring The virtio ring with descriptors to submit.\r |
e371e7e5 | 229 | \r |
a7615fa8 | 230 | @param[in] Indices Indices->NextDescIdx is not accessed.\r |
231 | Indices->HeadDescIdx identifies the head descriptor\r | |
232 | of the descriptor chain.\r | |
e371e7e5 | 233 | \r |
234 | \r | |
56f65ed8 | 235 | @return Error code from VirtIo->SetQueueNotify() if it fails.\r |
e371e7e5 | 236 | \r |
237 | @retval EFI_SUCCESS Otherwise, the host processed all descriptors.\r | |
238 | \r | |
239 | **/\r | |
240 | EFI_STATUS\r | |
241 | EFIAPI\r | |
242 | VirtioFlush (\r | |
56f65ed8 OM |
243 | IN VIRTIO_DEVICE_PROTOCOL *VirtIo,\r |
244 | IN UINT16 VirtQueueId,\r | |
245 | IN OUT VRING *Ring,\r | |
246 | IN DESC_INDICES *Indices\r | |
263559b8 | 247 | );\r |
248 | \r | |
249 | #endif // _VIRTIO_LIB_H_\r |