2 This driver produces XenBus Protocol instances for each Xen PV devices.
4 This XenBus bus driver will first initialize differente services in order to
5 enumerate the ParaVirtualized devices available.
14 Copyright (C) 2014, Citrix Ltd.
16 This program and the accompanying materials
17 are licensed and made available under the terms and conditions of the BSD License
18 which accompanies this distribution. The full text of the license may be found at
19 http://opensource.org/licenses/bsd-license.php
21 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
22 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
26 #include <IndustryStandard/Pci.h>
27 #include <IndustryStandard/Acpi.h>
28 #include <Library/DebugLib.h>
30 #include "XenBusDxe.h"
34 /// Driver Binding Protocol instance
36 EFI_DRIVER_BINDING_PROTOCOL gXenBusDxeDriverBinding
= {
37 XenBusDxeDriverBindingSupported
,
38 XenBusDxeDriverBindingStart
,
39 XenBusDxeDriverBindingStop
,
46 STATIC EFI_LOCK mMyDeviceLock
= EFI_INITIALIZE_LOCK_VARIABLE (TPL_CALLBACK
);
47 STATIC XENBUS_DEVICE
*mMyDevice
= NULL
;
52 @param ImageHandle Handle that identifies the image to be unloaded.
54 @retval EFI_SUCCESS The image has been unloaded.
55 @retval EFI_INVALID_PARAMETER ImageHandle is not a valid image handle.
61 IN EFI_HANDLE ImageHandle
66 EFI_HANDLE
*HandleBuffer
;
71 // Retrieve array of all handles in the handle database
73 Status
= gBS
->LocateHandleBuffer (
80 if (EFI_ERROR (Status
)) {
85 // Disconnect the current driver from handles in the handle database
87 for (Index
= 0; Index
< HandleCount
; Index
++) {
88 gBS
->DisconnectController (HandleBuffer
[Index
], gImageHandle
, NULL
);
92 // Free the array of handles
94 FreePool (HandleBuffer
);
98 // Uninstall protocols installed in the driver entry point
100 Status
= gBS
->UninstallMultipleProtocolInterfaces (
102 &gEfiDriverBindingProtocolGuid
, &gXenBusDxeDriverBinding
,
103 &gEfiComponentNameProtocolGuid
, &gXenBusDxeComponentName
,
104 &gEfiComponentName2ProtocolGuid
, &gXenBusDxeComponentName2
,
107 if (EFI_ERROR (Status
)) {
115 This is the declaration of an EFI image entry point. This entry point is
116 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including
117 both device drivers and bus drivers.
119 @param ImageHandle The firmware allocated handle for the UEFI image.
120 @param SystemTable A pointer to the EFI System Table.
122 @retval EFI_SUCCESS The operation completed successfully.
123 @retval Others An unexpected error occurred.
127 XenBusDxeDriverEntryPoint (
128 IN EFI_HANDLE ImageHandle
,
129 IN EFI_SYSTEM_TABLE
*SystemTable
135 // Install UEFI Driver Model protocol(s).
137 Status
= EfiLibInstallDriverBindingComponentName2 (
140 &gXenBusDxeDriverBinding
,
142 &gXenBusDxeComponentName
,
143 &gXenBusDxeComponentName2
145 ASSERT_EFI_ERROR (Status
);
153 Tests to see if this driver supports a given controller. If a child device is provided,
154 it further tests to see if this driver supports creating a handle for the specified child device.
156 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
157 @param[in] ControllerHandle The handle of the controller to test. This handle
158 must support a protocol interface that supplies
159 an I/O abstraction to the driver.
160 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
161 parameter is ignored by device drivers, and is optional for bus
162 drivers. For bus drivers, if this parameter is not NULL, then
163 the bus driver must determine if the bus controller specified
164 by ControllerHandle and the child controller specified
165 by RemainingDevicePath are both supported by this
168 @retval EFI_SUCCESS The device specified by ControllerHandle and
169 RemainingDevicePath is supported by the driver specified by This.
170 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
171 RemainingDevicePath is already being managed by the driver
173 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
174 RemainingDevicePath is already being managed by a different
175 driver or an application that requires exclusive access.
176 Currently not implemented.
177 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
178 RemainingDevicePath is not supported by the driver specified by This.
182 XenBusDxeDriverBindingSupported (
183 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
184 IN EFI_HANDLE ControllerHandle
,
185 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
189 EFI_PCI_IO_PROTOCOL
*PciIo
;
192 Status
= gBS
->OpenProtocol (
194 &gEfiPciIoProtocolGuid
,
196 This
->DriverBindingHandle
,
198 EFI_OPEN_PROTOCOL_BY_DRIVER
200 if (EFI_ERROR (Status
)) {
204 Status
= PciIo
->Pci
.Read (PciIo
, EfiPciIoWidthUint32
, 0,
205 sizeof Pci
/ sizeof (UINT32
), &Pci
);
207 if (Status
== EFI_SUCCESS
) {
208 if (Pci
.Hdr
.VendorId
== PCI_VENDOR_ID_XEN
&&
209 Pci
.Hdr
.DeviceId
== PCI_DEVICE_ID_XEN_PLATFORM
) {
210 Status
= EFI_SUCCESS
;
212 Status
= EFI_UNSUPPORTED
;
216 gBS
->CloseProtocol (ControllerHandle
, &gEfiPciIoProtocolGuid
,
217 This
->DriverBindingHandle
, ControllerHandle
);
229 XENBUS_DEVICE
*Dev
= Context
;
231 gBS
->DisconnectController(Dev
->ControllerHandle
,
232 Dev
->This
->DriverBindingHandle
, NULL
);
236 Starts a bus controller.
238 The Start() function is designed to be invoked from the EFI boot service ConnectController().
239 As a result, much of the error checking on the parameters to Start() has been moved into this
240 common boot service. It is legal to call Start() from other locations,
241 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
242 1. ControllerHandle must be a valid EFI_HANDLE.
243 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
244 EFI_DEVICE_PATH_PROTOCOL.
245 3. Prior to calling Start(), the Supported() function for the driver specified by This must
246 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
248 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
249 @param[in] ControllerHandle The handle of the controller to start. This handle
250 must support a protocol interface that supplies
251 an I/O abstraction to the driver.
252 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
253 parameter is ignored by device drivers, and is optional for bus
254 drivers. For a bus driver, if this parameter is NULL, then handles
255 for all the children of Controller are created by this driver.
256 If this parameter is not NULL and the first Device Path Node is
257 not the End of Device Path Node, then only the handle for the
258 child device specified by the first Device Path Node of
259 RemainingDevicePath is created by this driver.
260 If the first Device Path Node of RemainingDevicePath is
261 the End of Device Path Node, no child handle is created by this
264 @retval EFI_SUCCESS The device was started.
265 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
266 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
267 @retval Others The driver failded to start the device.
272 XenBusDxeDriverBindingStart (
273 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
274 IN EFI_HANDLE ControllerHandle
,
275 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath OPTIONAL
281 Dev
= AllocateZeroPool (sizeof (*Dev
));
282 Dev
->Signature
= XENBUS_DEVICE_SIGNATURE
;
284 Dev
->ControllerHandle
= ControllerHandle
;
286 EfiAcquireLock (&mMyDeviceLock
);
287 if (mMyDevice
!= NULL
) {
288 EfiReleaseLock (&mMyDeviceLock
);
290 // There is already a XenBus running, only one can be used at a time.
292 Status
= EFI_ALREADY_STARTED
;
296 EfiReleaseLock (&mMyDeviceLock
);
298 Status
= gBS
->CreateEvent (EVT_SIGNAL_EXIT_BOOT_SERVICES
, TPL_CALLBACK
,
301 &Dev
->ExitBootEvent
);
302 ASSERT_EFI_ERROR (Status
);
312 Stops a bus controller.
314 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
315 As a result, much of the error checking on the parameters to Stop() has been moved
316 into this common boot service. It is legal to call Stop() from other locations,
317 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
318 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
319 same driver's Start() function.
320 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
321 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
322 Start() function, and the Start() function must have called OpenProtocol() on
323 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
325 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
326 @param[in] ControllerHandle A handle to the device being stopped. The handle must
327 support a bus specific I/O protocol for the driver
328 to use to stop the device.
329 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
330 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
331 if NumberOfChildren is 0.
333 @retval EFI_SUCCESS The device was stopped.
334 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
339 XenBusDxeDriverBindingStop (
340 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
341 IN EFI_HANDLE ControllerHandle
,
342 IN UINTN NumberOfChildren
,
343 IN EFI_HANDLE
*ChildHandleBuffer OPTIONAL
346 XENBUS_DEVICE
*Dev
= mMyDevice
;
348 gBS
->CloseEvent (Dev
->ExitBootEvent
);