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