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