]> git.proxmox.com Git - mirror_edk2.git/blame - OvmfPkg/XenPvBlkDxe/XenPvBlkDxe.c
OvmfPkg/XenPvBlkDxe: Add BlockFront client.
[mirror_edk2.git] / OvmfPkg / XenPvBlkDxe / XenPvBlkDxe.c
CommitLineData
de671da8
AP
1/** @file\r
2 This driver produce a BlockIo protocol instance for a Xen PV block device.\r
3\r
4 This driver support XenBus protocol of type 'vbd'. Every function that\r
5 comsume XenBus protocol are in BlockFront, which the implementation to access\r
6 a Xen PV device. The BlockIo implementation is in it's one file and will call\r
7 BlockFront functions.\r
8\r
9 Copyright (C) 2014, Citrix Ltd.\r
10\r
11 This program and the accompanying materials\r
12 are licensed and made available under the terms and conditions of the BSD License\r
13 which accompanies this distribution. The full text of the license may be found at\r
14 http://opensource.org/licenses/bsd-license.php\r
15\r
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
18\r
19**/\r
20\r
21#include "XenPvBlkDxe.h"\r
22\r
5cce8524
ST
23#include "BlockFront.h"\r
24\r
de671da8
AP
25\r
26///\r
27/// Driver Binding Protocol instance\r
28///\r
29EFI_DRIVER_BINDING_PROTOCOL gXenPvBlkDxeDriverBinding = {\r
30 XenPvBlkDxeDriverBindingSupported,\r
31 XenPvBlkDxeDriverBindingStart,\r
32 XenPvBlkDxeDriverBindingStop,\r
33 XEN_PV_BLK_DXE_VERSION,\r
34 NULL,\r
35 NULL\r
36};\r
37\r
38\r
39/**\r
40 Unloads an image.\r
41\r
42 @param ImageHandle Handle that identifies the image to be unloaded.\r
43\r
44 @retval EFI_SUCCESS The image has been unloaded.\r
45 @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.\r
46\r
47**/\r
48EFI_STATUS\r
49EFIAPI\r
50XenPvBlkDxeUnload (\r
51 IN EFI_HANDLE ImageHandle\r
52 )\r
53{\r
54 EFI_STATUS Status;\r
55\r
56 EFI_HANDLE *HandleBuffer;\r
57 UINTN HandleCount;\r
58 UINTN Index;\r
59\r
60\r
61 //\r
62 // Retrieve array of all handles in the handle database\r
63 //\r
64 Status = gBS->LocateHandleBuffer (\r
65 AllHandles,\r
66 NULL,\r
67 NULL,\r
68 &HandleCount,\r
69 &HandleBuffer\r
70 );\r
71 if (EFI_ERROR (Status)) {\r
72 return Status;\r
73 }\r
74\r
75 //\r
76 // Disconnect the current driver from handles in the handle database\r
77 //\r
78 for (Index = 0; Index < HandleCount; Index++) {\r
79 gBS->DisconnectController (HandleBuffer[Index], gImageHandle, NULL);\r
80 }\r
81\r
82 //\r
83 // Free the array of handles\r
84 //\r
85 FreePool (HandleBuffer);\r
86\r
87\r
88 //\r
89 // Uninstall protocols installed in the driver entry point\r
90 //\r
91 Status = gBS->UninstallMultipleProtocolInterfaces (\r
92 ImageHandle,\r
93 &gEfiDriverBindingProtocolGuid, &gXenPvBlkDxeDriverBinding,\r
94 &gEfiComponentNameProtocolGuid, &gXenPvBlkDxeComponentName,\r
95 &gEfiComponentName2ProtocolGuid, &gXenPvBlkDxeComponentName2,\r
96 NULL\r
97 );\r
98 if (EFI_ERROR (Status)) {\r
99 return Status;\r
100 }\r
101\r
102 return EFI_SUCCESS;\r
103}\r
104\r
105/**\r
106 This is the declaration of an EFI image entry point. This entry point is\r
107 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including\r
108 both device drivers and bus drivers.\r
109\r
110 @param ImageHandle The firmware allocated handle for the UEFI image.\r
111 @param SystemTable A pointer to the EFI System Table.\r
112\r
113 @retval EFI_SUCCESS The operation completed successfully.\r
114 @retval Others An unexpected error occurred.\r
115**/\r
116EFI_STATUS\r
117EFIAPI\r
118XenPvBlkDxeDriverEntryPoint (\r
119 IN EFI_HANDLE ImageHandle,\r
120 IN EFI_SYSTEM_TABLE *SystemTable\r
121 )\r
122{\r
123 EFI_STATUS Status;\r
124\r
125 //\r
126 // Install UEFI Driver Model protocol(s).\r
127 //\r
128 Status = EfiLibInstallDriverBindingComponentName2 (\r
129 ImageHandle,\r
130 SystemTable,\r
131 &gXenPvBlkDxeDriverBinding,\r
132 ImageHandle,\r
133 &gXenPvBlkDxeComponentName,\r
134 &gXenPvBlkDxeComponentName2\r
135 );\r
136 ASSERT_EFI_ERROR (Status);\r
137\r
138 return Status;\r
139}\r
140\r
141\r
142/**\r
143 Tests to see if this driver supports a given controller. If a child device is provided,\r
144 it further tests to see if this driver supports creating a handle for the specified child device.\r
145\r
146 This function checks to see if the driver specified by This supports the device specified by\r
147 ControllerHandle. Drivers will typically use the device path attached to\r
148 ControllerHandle and/or the services from the bus I/O abstraction attached to\r
149 ControllerHandle to determine if the driver supports ControllerHandle. This function\r
150 may be called many times during platform initialization. In order to reduce boot times, the tests\r
151 performed by this function must be very small, and take as little time as possible to execute. This\r
152 function must not change the state of any hardware devices, and this function must be aware that the\r
153 device specified by ControllerHandle may already be managed by the same driver or a\r
154 different driver. This function must match its calls to AllocatePages() with FreePages(),\r
155 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().\r
156 Because ControllerHandle may have been previously started by the same driver, if a protocol is\r
157 already in the opened state, then it must not be closed with CloseProtocol(). This is required\r
158 to guarantee the state of ControllerHandle is not modified by this function.\r
159\r
160 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
161 @param[in] ControllerHandle The handle of the controller to test. This handle\r
162 must support a protocol interface that supplies\r
163 an I/O abstraction to the driver.\r
164 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
165 parameter is ignored by device drivers, and is optional for bus\r
166 drivers. For bus drivers, if this parameter is not NULL, then\r
167 the bus driver must determine if the bus controller specified\r
168 by ControllerHandle and the child controller specified\r
169 by RemainingDevicePath are both supported by this\r
170 bus driver.\r
171\r
172 @retval EFI_SUCCESS The device specified by ControllerHandle and\r
173 RemainingDevicePath is supported by the driver specified by This.\r
174 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and\r
175 RemainingDevicePath is already being managed by the driver\r
176 specified by This.\r
177 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and\r
178 RemainingDevicePath is already being managed by a different\r
179 driver or an application that requires exclusive access.\r
180 Currently not implemented.\r
181 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and\r
182 RemainingDevicePath is not supported by the driver specified by This.\r
183**/\r
184EFI_STATUS\r
185EFIAPI\r
186XenPvBlkDxeDriverBindingSupported (\r
187 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
188 IN EFI_HANDLE ControllerHandle,\r
189 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
190 )\r
191{\r
192 EFI_STATUS Status;\r
193 XENBUS_PROTOCOL *XenBusIo;\r
194\r
195 Status = gBS->OpenProtocol (\r
196 ControllerHandle,\r
197 &gXenBusProtocolGuid,\r
198 (VOID **)&XenBusIo,\r
199 This->DriverBindingHandle,\r
200 ControllerHandle,\r
201 EFI_OPEN_PROTOCOL_BY_DRIVER\r
202 );\r
203 if (EFI_ERROR (Status)) {\r
204 return Status;\r
205 }\r
206 if (AsciiStrCmp (XenBusIo->Type, "vbd") == 0) {\r
207 Status = EFI_SUCCESS;\r
208 } else {\r
209 Status = EFI_UNSUPPORTED;\r
210 }\r
211\r
212 gBS->CloseProtocol (ControllerHandle, &gXenBusProtocolGuid,\r
213 This->DriverBindingHandle, ControllerHandle);\r
214\r
215 return Status;\r
216}\r
217\r
218/**\r
219 Starts a device controller.\r
220\r
221 The Start() function is designed to be invoked from the EFI boot service ConnectController().\r
222 As a result, much of the error checking on the parameters to Start() has been moved into this\r
223 common boot service. It is legal to call Start() from other locations,\r
224 but the following calling restrictions must be followed, or the system behavior will not be deterministic.\r
225 1. ControllerHandle must be a valid EFI_HANDLE.\r
226 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned\r
227 EFI_DEVICE_PATH_PROTOCOL.\r
228 3. Prior to calling Start(), the Supported() function for the driver specified by This must\r
229 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.\r
230\r
231 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
232 @param[in] ControllerHandle The handle of the controller to start. This handle\r
233 must support a protocol interface that supplies\r
234 an I/O abstraction to the driver.\r
235 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
236 parameter is ignored by device drivers, and is optional for bus\r
237 drivers. For a bus driver, if this parameter is NULL, then handles\r
238 for all the children of Controller are created by this driver.\r
239 If this parameter is not NULL and the first Device Path Node is\r
240 not the End of Device Path Node, then only the handle for the\r
241 child device specified by the first Device Path Node of\r
242 RemainingDevicePath is created by this driver.\r
243 If the first Device Path Node of RemainingDevicePath is\r
244 the End of Device Path Node, no child handle is created by this\r
245 driver.\r
246\r
247 @retval EFI_SUCCESS The device was started.\r
248 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.\r
249 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
250 @retval Others The driver failded to start the device.\r
251\r
252**/\r
253EFI_STATUS\r
254EFIAPI\r
255XenPvBlkDxeDriverBindingStart (\r
256 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
257 IN EFI_HANDLE ControllerHandle,\r
258 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
259 )\r
260{\r
261 EFI_STATUS Status;\r
262 XENBUS_PROTOCOL *XenBusIo;\r
5cce8524 263 XEN_BLOCK_FRONT_DEVICE *Dev;\r
de671da8
AP
264\r
265 Status = gBS->OpenProtocol (\r
266 ControllerHandle,\r
267 &gXenBusProtocolGuid,\r
268 (VOID **)&XenBusIo,\r
269 This->DriverBindingHandle,\r
270 ControllerHandle,\r
271 EFI_OPEN_PROTOCOL_BY_DRIVER\r
272 );\r
273 if (EFI_ERROR (Status)) {\r
274 return Status;\r
275 }\r
276\r
5cce8524
ST
277 Status = XenPvBlockFrontInitialization (XenBusIo, XenBusIo->Node, &Dev);\r
278 if (EFI_ERROR (Status)) {\r
279 goto CloseProtocol;\r
280 }\r
281\r
de671da8 282 return EFI_SUCCESS;\r
5cce8524
ST
283\r
284CloseProtocol:\r
285 gBS->CloseProtocol (ControllerHandle, &gXenBusProtocolGuid,\r
286 This->DriverBindingHandle, ControllerHandle);\r
287 return Status;\r
de671da8
AP
288}\r
289\r
290/**\r
291 Stops a device controller.\r
292\r
293 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().\r
294 As a result, much of the error checking on the parameters to Stop() has been moved\r
295 into this common boot service. It is legal to call Stop() from other locations,\r
296 but the following calling restrictions must be followed, or the system behavior will not be deterministic.\r
297 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this\r
298 same driver's Start() function.\r
299 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid\r
300 EFI_HANDLE. In addition, all of these handles must have been created in this driver's\r
301 Start() function, and the Start() function must have called OpenProtocol() on\r
302 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r
303\r
304 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
305 @param[in] ControllerHandle A handle to the device being stopped. The handle must\r
306 support a bus specific I/O protocol for the driver\r
307 to use to stop the device.\r
308 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.\r
309 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL\r
310 if NumberOfChildren is 0.\r
311\r
312 @retval EFI_SUCCESS The device was stopped.\r
313 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.\r
314\r
315**/\r
316EFI_STATUS\r
317EFIAPI\r
318XenPvBlkDxeDriverBindingStop (\r
319 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
320 IN EFI_HANDLE ControllerHandle,\r
321 IN UINTN NumberOfChildren,\r
322 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL\r
323 )\r
324{\r
325 gBS->CloseProtocol (ControllerHandle, &gXenBusProtocolGuid,\r
326 This->DriverBindingHandle, ControllerHandle);\r
327\r
328 return EFI_SUCCESS;\r
329}\r