2 NvmExpressDxe driver is used to manage non-volatile memory subsystem which follows
3 NVM Express specification.
5 Copyright (c) 2013 - 2015, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "NvmExpress.h"
19 // NVM Express Driver Binding Protocol Instance
21 EFI_DRIVER_BINDING_PROTOCOL gNvmExpressDriverBinding
= {
22 NvmExpressDriverBindingSupported
,
23 NvmExpressDriverBindingStart
,
24 NvmExpressDriverBindingStop
,
31 // NVM Express EFI Driver Supported EFI Version Protocol Instance
33 EFI_DRIVER_SUPPORTED_EFI_VERSION_PROTOCOL gNvmExpressDriverSupportedEfiVersion
= {
34 sizeof (EFI_DRIVER_SUPPORTED_EFI_VERSION_PROTOCOL
), // Size of Protocol structure.
35 0 // Version number to be filled at start up.
39 // Template for NVM Express Pass Thru Mode data structure.
41 GLOBAL_REMOVE_IF_UNREFERENCED EFI_NVM_EXPRESS_PASS_THRU_MODE gEfiNvmExpressPassThruMode
= {
42 EFI_NVM_EXPRESS_PASS_THRU_ATTRIBUTES_PHYSICAL
| EFI_NVM_EXPRESS_PASS_THRU_ATTRIBUTES_LOGICAL
| EFI_NVM_EXPRESS_PASS_THRU_ATTRIBUTES_CMD_SET_NVM
,
48 Check if the specified Nvm Express device namespace is active, and create child handles
49 for them with BlockIo and DiskInfo protocol instances.
51 @param[in] Private The pointer to the NVME_CONTROLLER_PRIVATE_DATA data structure.
52 @param[in] NamespaceId The NVM Express namespace ID for which a device path node is to be
53 allocated and built. Caller must set the NamespaceId to zero if the
54 device path node will contain a valid UUID.
56 @retval EFI_SUCCESS All the namespaces in the device are successfully enumerated.
57 @return Others Some error occurs when enumerating the namespaces.
61 EnumerateNvmeDevNamespace (
62 IN NVME_CONTROLLER_PRIVATE_DATA
*Private
,
66 NVME_ADMIN_NAMESPACE_DATA
*NamespaceData
;
67 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePathNode
;
68 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
69 EFI_HANDLE DeviceHandle
;
70 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
71 EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
;
72 NVME_DEVICE_PRIVATE_DATA
*Device
;
78 NewDevicePathNode
= NULL
;
83 // Allocate a buffer for Identify Namespace data
85 NamespaceData
= AllocateZeroPool(sizeof (NVME_ADMIN_NAMESPACE_DATA
));
86 if(NamespaceData
== NULL
) {
87 return EFI_OUT_OF_RESOURCES
;
90 ParentDevicePath
= Private
->ParentDevicePath
;
94 Status
= NvmeIdentifyNamespace (
99 if (EFI_ERROR(Status
)) {
103 // Validate Namespace
105 if (NamespaceData
->Ncap
== 0) {
106 Status
= EFI_DEVICE_ERROR
;
109 // allocate device private data for each discovered namespace
111 Device
= AllocateZeroPool(sizeof(NVME_DEVICE_PRIVATE_DATA
));
112 if (Device
== NULL
) {
113 Status
= EFI_OUT_OF_RESOURCES
;
118 // Initialize SSD namespace instance data
120 Device
->Signature
= NVME_DEVICE_PRIVATE_DATA_SIGNATURE
;
121 Device
->NamespaceId
= NamespaceId
;
122 Device
->NamespaceUuid
= NamespaceData
->Eui64
;
124 Device
->ControllerHandle
= Private
->ControllerHandle
;
125 Device
->DriverBindingHandle
= Private
->DriverBindingHandle
;
126 Device
->Controller
= Private
;
129 // Build BlockIo media structure
131 Device
->Media
.MediaId
= 0;
132 Device
->Media
.RemovableMedia
= FALSE
;
133 Device
->Media
.MediaPresent
= TRUE
;
134 Device
->Media
.LogicalPartition
= FALSE
;
135 Device
->Media
.ReadOnly
= FALSE
;
136 Device
->Media
.WriteCaching
= FALSE
;
138 Flbas
= NamespaceData
->Flbas
;
139 LbaFmtIdx
= Flbas
& 0xF;
140 Lbads
= NamespaceData
->LbaFormat
[LbaFmtIdx
].Lbads
;
141 Device
->Media
.BlockSize
= (UINT32
)1 << Lbads
;
143 Device
->Media
.LastBlock
= NamespaceData
->Nsze
- 1;
144 Device
->Media
.LogicalBlocksPerPhysicalBlock
= 1;
145 Device
->Media
.LowestAlignedLba
= 1;
148 // Create BlockIo Protocol instance
150 Device
->BlockIo
.Revision
= EFI_BLOCK_IO_PROTOCOL_REVISION2
;
151 Device
->BlockIo
.Media
= &Device
->Media
;
152 Device
->BlockIo
.Reset
= NvmeBlockIoReset
;
153 Device
->BlockIo
.ReadBlocks
= NvmeBlockIoReadBlocks
;
154 Device
->BlockIo
.WriteBlocks
= NvmeBlockIoWriteBlocks
;
155 Device
->BlockIo
.FlushBlocks
= NvmeBlockIoFlushBlocks
;
158 // Create DiskInfo Protocol instance
160 InitializeDiskInfo (Device
);
163 // Create a Nvm Express Namespace Device Path Node
165 Status
= Private
->Passthru
.BuildDevicePath (
171 if (EFI_ERROR(Status
)) {
176 // Append the SSD node to the controller's device path
178 DevicePath
= AppendDevicePathNode (ParentDevicePath
, NewDevicePathNode
);
179 if (DevicePath
== NULL
) {
180 Status
= EFI_OUT_OF_RESOURCES
;
185 RemainingDevicePath
= DevicePath
;
186 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &RemainingDevicePath
, &DeviceHandle
);
187 if (!EFI_ERROR (Status
) && (DeviceHandle
!= NULL
) && IsDevicePathEnd(RemainingDevicePath
)) {
188 Status
= EFI_ALREADY_STARTED
;
189 FreePool (DevicePath
);
193 Device
->DevicePath
= DevicePath
;
196 // Make sure the handle is NULL so we create a new handle
198 Device
->DeviceHandle
= NULL
;
200 Status
= gBS
->InstallMultipleProtocolInterfaces (
201 &Device
->DeviceHandle
,
202 &gEfiDevicePathProtocolGuid
,
204 &gEfiBlockIoProtocolGuid
,
206 &gEfiDiskInfoProtocolGuid
,
211 if(EFI_ERROR(Status
)) {
215 Private
->ControllerHandle
,
216 &gEfiPciIoProtocolGuid
,
217 (VOID
**) &Private
->PciIo
,
218 Private
->DriverBindingHandle
,
219 Device
->DeviceHandle
,
220 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
224 // Dump NvmExpress Identify Namespace Data
226 DEBUG ((EFI_D_INFO
, " == NVME IDENTIFY NAMESPACE [%d] DATA ==\n", NamespaceId
));
227 DEBUG ((EFI_D_INFO
, " NSZE : 0x%x\n", NamespaceData
->Nsze
));
228 DEBUG ((EFI_D_INFO
, " NCAP : 0x%x\n", NamespaceData
->Ncap
));
229 DEBUG ((EFI_D_INFO
, " NUSE : 0x%x\n", NamespaceData
->Nuse
));
230 DEBUG ((EFI_D_INFO
, " LBAF0.LBADS : 0x%x\n", (NamespaceData
->LbaFormat
[0].Lbads
)));
233 // Build controller name for Component Name (2) protocol.
235 UnicodeSPrintAsciiFormat (Device
->ModelName
, sizeof (Device
->ModelName
), "%a-%a-%x", Private
->ControllerData
->Sn
, Private
->ControllerData
->Mn
, NamespaceData
->Eui64
);
239 gNvmExpressComponentName
.SupportedLanguages
,
240 &Device
->ControllerNameTable
,
247 gNvmExpressComponentName2
.SupportedLanguages
,
248 &Device
->ControllerNameTable
,
255 if(NamespaceData
!= NULL
) {
256 FreePool (NamespaceData
);
259 if (NewDevicePathNode
!= NULL
) {
260 FreePool (NewDevicePathNode
);
263 if(EFI_ERROR(Status
) && (Device
!= NULL
) && (Device
->DevicePath
!= NULL
)) {
264 FreePool (Device
->DevicePath
);
266 if(EFI_ERROR(Status
) && (Device
!= NULL
)) {
273 Discover all Nvm Express device namespaces, and create child handles for them with BlockIo
274 and DiskInfo protocol instances.
276 @param[in] Private The pointer to the NVME_CONTROLLER_PRIVATE_DATA data structure.
278 @retval EFI_SUCCESS All the namespaces in the device are successfully enumerated.
279 @return Others Some error occurs when enumerating the namespaces.
283 DiscoverAllNamespaces (
284 IN NVME_CONTROLLER_PRIVATE_DATA
*Private
289 EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL
*Passthru
;
291 NamespaceId
= 0xFFFFFFFF;
292 Passthru
= &Private
->Passthru
;
295 Status
= Passthru
->GetNextNamespace (
297 (UINT32
*)&NamespaceId
300 if (EFI_ERROR (Status
)) {
304 Status
= EnumerateNvmeDevNamespace (
309 if (EFI_ERROR(Status
)) {
318 Unregisters a Nvm Express device namespace.
320 This function removes the protocols installed on the controller handle and
321 frees the resources allocated for the namespace.
323 @param This The pointer to EFI_DRIVER_BINDING_PROTOCOL instance.
324 @param Controller The controller handle of the namespace.
325 @param Handle The child handle.
327 @retval EFI_SUCCESS The namespace is successfully unregistered.
328 @return Others Some error occurs when unregistering the namespace.
332 UnregisterNvmeNamespace (
333 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
334 IN EFI_HANDLE Controller
,
339 EFI_PCI_IO_PROTOCOL
*PciIo
;
340 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
341 NVME_DEVICE_PRIVATE_DATA
*Device
;
345 Status
= gBS
->OpenProtocol (
347 &gEfiBlockIoProtocolGuid
,
349 This
->DriverBindingHandle
,
351 EFI_OPEN_PROTOCOL_GET_PROTOCOL
353 if (EFI_ERROR (Status
)) {
357 Device
= NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO (BlockIo
);
360 // Close the child handle
364 &gEfiPciIoProtocolGuid
,
365 This
->DriverBindingHandle
,
370 // The Nvm Express driver installs the BlockIo and DiskInfo in the DriverBindingStart().
371 // Here should uninstall both of them.
373 Status
= gBS
->UninstallMultipleProtocolInterfaces (
375 &gEfiDevicePathProtocolGuid
,
377 &gEfiBlockIoProtocolGuid
,
379 &gEfiDiskInfoProtocolGuid
,
384 if (EFI_ERROR (Status
)) {
387 &gEfiPciIoProtocolGuid
,
389 This
->DriverBindingHandle
,
391 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
396 if(Device
->DevicePath
!= NULL
) {
397 FreePool (Device
->DevicePath
);
400 if (Device
->ControllerNameTable
!= NULL
) {
401 FreeUnicodeStringTable (Device
->ControllerNameTable
);
410 Tests to see if this driver supports a given controller. If a child device is provided,
411 it further tests to see if this driver supports creating a handle for the specified child device.
413 This function checks to see if the driver specified by This supports the device specified by
414 ControllerHandle. Drivers will typically use the device path attached to
415 ControllerHandle and/or the services from the bus I/O abstraction attached to
416 ControllerHandle to determine if the driver supports ControllerHandle. This function
417 may be called many times during platform initialization. In order to reduce boot times, the tests
418 performed by this function must be very small, and take as little time as possible to execute. This
419 function must not change the state of any hardware devices, and this function must be aware that the
420 device specified by ControllerHandle may already be managed by the same driver or a
421 different driver. This function must match its calls to AllocatePages() with FreePages(),
422 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
423 Since ControllerHandle may have been previously started by the same driver, if a protocol is
424 already in the opened state, then it must not be closed with CloseProtocol(). This is required
425 to guarantee the state of ControllerHandle is not modified by this function.
427 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
428 @param[in] ControllerHandle The handle of the controller to test. This handle
429 must support a protocol interface that supplies
430 an I/O abstraction to the driver.
431 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
432 parameter is ignored by device drivers, and is optional for bus
433 drivers. For bus drivers, if this parameter is not NULL, then
434 the bus driver must determine if the bus controller specified
435 by ControllerHandle and the child controller specified
436 by RemainingDevicePath are both supported by this
439 @retval EFI_SUCCESS The device specified by ControllerHandle and
440 RemainingDevicePath is supported by the driver specified by This.
441 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
442 RemainingDevicePath is already being managed by the driver
444 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
445 RemainingDevicePath is already being managed by a different
446 driver or an application that requires exclusive access.
447 Currently not implemented.
448 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
449 RemainingDevicePath is not supported by the driver specified by This.
453 NvmExpressDriverBindingSupported (
454 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
455 IN EFI_HANDLE Controller
,
456 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
460 EFI_DEV_PATH_PTR DevicePathNode
;
461 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
462 EFI_PCI_IO_PROTOCOL
*PciIo
;
466 // Check whether device path is valid
468 if (RemainingDevicePath
!= NULL
) {
470 // Check if RemainingDevicePath is the End of Device Path Node,
471 // if yes, go on checking other conditions
473 if (!IsDevicePathEnd (RemainingDevicePath
)) {
475 // If RemainingDevicePath isn't the End of Device Path Node,
476 // check its validation
478 DevicePathNode
.DevPath
= RemainingDevicePath
;
480 if ((DevicePathNode
.DevPath
->Type
!= MESSAGING_DEVICE_PATH
) ||
481 (DevicePathNode
.DevPath
->SubType
!= MSG_NVME_NAMESPACE_DP
) ||
482 DevicePathNodeLength(DevicePathNode
.DevPath
) != sizeof(NVME_NAMESPACE_DEVICE_PATH
)) {
483 return EFI_UNSUPPORTED
;
489 // Open the EFI Device Path protocol needed to perform the supported test
491 Status
= gBS
->OpenProtocol (
493 &gEfiDevicePathProtocolGuid
,
494 (VOID
**) &ParentDevicePath
,
495 This
->DriverBindingHandle
,
497 EFI_OPEN_PROTOCOL_BY_DRIVER
499 if (Status
== EFI_ALREADY_STARTED
) {
503 if (EFI_ERROR (Status
)) {
508 // Close protocol, don't use device path protocol in the Support() function
512 &gEfiDevicePathProtocolGuid
,
513 This
->DriverBindingHandle
,
518 // Attempt to Open PCI I/O Protocol
520 Status
= gBS
->OpenProtocol (
522 &gEfiPciIoProtocolGuid
,
524 This
->DriverBindingHandle
,
526 EFI_OPEN_PROTOCOL_BY_DRIVER
528 if (Status
== EFI_ALREADY_STARTED
) {
532 if (EFI_ERROR (Status
)) {
537 // Now further check the PCI header: Base class (offset 0x0B) and Sub Class (offset 0x0A).
538 // This controller should be a Nvm Express controller.
540 Status
= PciIo
->Pci
.Read (
543 PCI_CLASSCODE_OFFSET
,
547 if (EFI_ERROR (Status
)) {
552 // Examine Nvm Express controller PCI Configuration table fields
554 if ((ClassCode
[0] != PCI_IF_NVMHCI
) || (ClassCode
[1] != PCI_CLASS_MASS_STORAGE_NVM
) || (ClassCode
[2] != PCI_CLASS_MASS_STORAGE
)) {
555 Status
= EFI_UNSUPPORTED
;
561 &gEfiPciIoProtocolGuid
,
562 This
->DriverBindingHandle
,
571 Starts a device controller or a bus controller.
573 The Start() function is designed to be invoked from the EFI boot service ConnectController().
574 As a result, much of the error checking on the parameters to Start() has been moved into this
575 common boot service. It is legal to call Start() from other locations,
576 but the following calling restrictions must be followed or the system behavior will not be deterministic.
577 1. ControllerHandle must be a valid EFI_HANDLE.
578 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
579 EFI_DEVICE_PATH_PROTOCOL.
580 3. Prior to calling Start(), the Supported() function for the driver specified by This must
581 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
583 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
584 @param[in] ControllerHandle The handle of the controller to start. This handle
585 must support a protocol interface that supplies
586 an I/O abstraction to the driver.
587 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
588 parameter is ignored by device drivers, and is optional for bus
589 drivers. For a bus driver, if this parameter is NULL, then handles
590 for all the children of Controller are created by this driver.
591 If this parameter is not NULL and the first Device Path Node is
592 not the End of Device Path Node, then only the handle for the
593 child device specified by the first Device Path Node of
594 RemainingDevicePath is created by this driver.
595 If the first Device Path Node of RemainingDevicePath is
596 the End of Device Path Node, no child handle is created by this
599 @retval EFI_SUCCESS The device was started.
600 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
601 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
602 @retval Others The driver failded to start the device.
607 NvmExpressDriverBindingStart (
608 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
609 IN EFI_HANDLE Controller
,
610 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
614 EFI_PCI_IO_PROTOCOL
*PciIo
;
615 NVME_CONTROLLER_PRIVATE_DATA
*Private
;
616 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
618 EFI_PHYSICAL_ADDRESS MappedAddr
;
620 EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL
*Passthru
;
622 DEBUG ((EFI_D_INFO
, "NvmExpressDriverBindingStart: start\n"));
626 ParentDevicePath
= NULL
;
628 Status
= gBS
->OpenProtocol (
630 &gEfiDevicePathProtocolGuid
,
631 (VOID
**) &ParentDevicePath
,
632 This
->DriverBindingHandle
,
634 EFI_OPEN_PROTOCOL_BY_DRIVER
636 if ((EFI_ERROR (Status
)) && (Status
!= EFI_ALREADY_STARTED
)) {
640 Status
= gBS
->OpenProtocol (
642 &gEfiPciIoProtocolGuid
,
644 This
->DriverBindingHandle
,
646 EFI_OPEN_PROTOCOL_BY_DRIVER
649 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
654 // Check EFI_ALREADY_STARTED to reuse the original NVME_CONTROLLER_PRIVATE_DATA.
656 if (Status
!= EFI_ALREADY_STARTED
) {
657 Private
= AllocateZeroPool (sizeof (NVME_CONTROLLER_PRIVATE_DATA
));
659 if (Private
== NULL
) {
660 DEBUG ((EFI_D_ERROR
, "NvmExpressDriverBindingStart: allocating pool for Nvme Private Data failed!\n"));
661 Status
= EFI_OUT_OF_RESOURCES
;
666 // 4 x 4kB aligned buffers will be carved out of this buffer.
667 // 1st 4kB boundary is the start of the admin submission queue.
668 // 2nd 4kB boundary is the start of the admin completion queue.
669 // 3rd 4kB boundary is the start of I/O submission queue #1.
670 // 4th 4kB boundary is the start of I/O completion queue #1.
672 // Allocate 4 pages of memory, then map it for bus master read and write.
674 Status
= PciIo
->AllocateBuffer (
679 (VOID
**)&Private
->Buffer
,
682 if (EFI_ERROR (Status
)) {
686 Bytes
= EFI_PAGES_TO_SIZE (4);
687 Status
= PciIo
->Map (
689 EfiPciIoOperationBusMasterCommonBuffer
,
696 if (EFI_ERROR (Status
) || (Bytes
!= EFI_PAGES_TO_SIZE (4))) {
700 Private
->BufferPciAddr
= (UINT8
*)(UINTN
)MappedAddr
;
701 ZeroMem (Private
->Buffer
, EFI_PAGES_TO_SIZE (4));
703 Private
->Signature
= NVME_CONTROLLER_PRIVATE_DATA_SIGNATURE
;
704 Private
->ControllerHandle
= Controller
;
705 Private
->ImageHandle
= This
->DriverBindingHandle
;
706 Private
->DriverBindingHandle
= This
->DriverBindingHandle
;
707 Private
->PciIo
= PciIo
;
708 Private
->ParentDevicePath
= ParentDevicePath
;
709 Private
->Passthru
.Mode
= &Private
->PassThruMode
;
710 Private
->Passthru
.PassThru
= NvmExpressPassThru
;
711 Private
->Passthru
.GetNextNamespace
= NvmExpressGetNextNamespace
;
712 Private
->Passthru
.BuildDevicePath
= NvmExpressBuildDevicePath
;
713 Private
->Passthru
.GetNamespace
= NvmExpressGetNamespace
;
714 CopyMem (&Private
->PassThruMode
, &gEfiNvmExpressPassThruMode
, sizeof (EFI_NVM_EXPRESS_PASS_THRU_MODE
));
716 Status
= NvmeControllerInit (Private
);
717 if (EFI_ERROR(Status
)) {
721 Status
= gBS
->InstallMultipleProtocolInterfaces (
723 &gEfiNvmExpressPassThruProtocolGuid
,
727 if (EFI_ERROR (Status
)) {
731 Status
= gBS
->OpenProtocol (
733 &gEfiNvmExpressPassThruProtocolGuid
,
735 This
->DriverBindingHandle
,
737 EFI_OPEN_PROTOCOL_GET_PROTOCOL
739 if (EFI_ERROR (Status
)) {
743 Private
= NVME_CONTROLLER_PRIVATE_DATA_FROM_PASS_THRU (Passthru
);
746 if (RemainingDevicePath
== NULL
) {
748 // Enumerate all NVME namespaces in the controller
750 Status
= DiscoverAllNamespaces (
754 } else if (!IsDevicePathEnd (RemainingDevicePath
)) {
756 // Enumerate the specified NVME namespace
758 Status
= Private
->Passthru
.GetNamespace (
764 if (!EFI_ERROR (Status
)) {
765 Status
= EnumerateNvmeDevNamespace (
772 DEBUG ((EFI_D_INFO
, "NvmExpressDriverBindingStart: end successfully\n"));
776 if ((Private
!= NULL
) && (Private
->Mapping
!= NULL
)) {
777 PciIo
->Unmap (PciIo
, Private
->Mapping
);
780 if ((Private
!= NULL
) && (Private
->Buffer
!= NULL
)) {
781 PciIo
->FreeBuffer (PciIo
, 4, Private
->Buffer
);
784 if (Private
!= NULL
) {
790 &gEfiPciIoProtocolGuid
,
791 This
->DriverBindingHandle
,
797 &gEfiDevicePathProtocolGuid
,
798 This
->DriverBindingHandle
,
802 DEBUG ((EFI_D_INFO
, "NvmExpressDriverBindingStart: end with %r\n", Status
));
809 Stops a device controller or a bus controller.
811 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
812 As a result, much of the error checking on the parameters to Stop() has been moved
813 into this common boot service. It is legal to call Stop() from other locations,
814 but the following calling restrictions must be followed or the system behavior will not be deterministic.
815 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
816 same driver's Start() function.
817 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
818 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
819 Start() function, and the Start() function must have called OpenProtocol() on
820 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
822 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
823 @param[in] ControllerHandle A handle to the device being stopped. The handle must
824 support a bus specific I/O protocol for the driver
825 to use to stop the device.
826 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
827 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
828 if NumberOfChildren is 0.
830 @retval EFI_SUCCESS The device was stopped.
831 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
836 NvmExpressDriverBindingStop (
837 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
838 IN EFI_HANDLE Controller
,
839 IN UINTN NumberOfChildren
,
840 IN EFI_HANDLE
*ChildHandleBuffer
844 BOOLEAN AllChildrenStopped
;
846 NVME_CONTROLLER_PRIVATE_DATA
*Private
;
848 if (NumberOfChildren
== 0) {
849 Status
= gBS
->OpenProtocol (
853 This
->DriverBindingHandle
,
855 EFI_OPEN_PROTOCOL_GET_PROTOCOL
858 if (!EFI_ERROR (Status
)) {
859 gBS
->UninstallMultipleProtocolInterfaces (
866 if (Private
->Mapping
!= NULL
) {
867 Private
->PciIo
->Unmap (Private
->PciIo
, Private
->Mapping
);
870 if (Private
->Buffer
!= NULL
) {
871 Private
->PciIo
->FreeBuffer (Private
->PciIo
, 4, Private
->Buffer
);
874 FreePool (Private
->ControllerData
);
880 &gEfiPciIoProtocolGuid
,
881 This
->DriverBindingHandle
,
886 &gEfiDevicePathProtocolGuid
,
887 This
->DriverBindingHandle
,
893 AllChildrenStopped
= TRUE
;
895 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
896 Status
= UnregisterNvmeNamespace (This
, Controller
, ChildHandleBuffer
[Index
]);
897 if (EFI_ERROR (Status
)) {
898 AllChildrenStopped
= FALSE
;
902 if (!AllChildrenStopped
) {
903 return EFI_DEVICE_ERROR
;
910 This is the unload handle for the NVM Express driver.
912 Disconnect the driver specified by ImageHandle from the NVMe device in the handle database.
913 Uninstall all the protocols installed in the driver.
915 @param[in] ImageHandle The drivers' driver image.
917 @retval EFI_SUCCESS The image is unloaded.
918 @retval Others Failed to unload the image.
924 IN EFI_HANDLE ImageHandle
928 EFI_HANDLE
*DeviceHandleBuffer
;
929 UINTN DeviceHandleCount
;
931 EFI_COMPONENT_NAME_PROTOCOL
*ComponentName
;
932 EFI_COMPONENT_NAME2_PROTOCOL
*ComponentName2
;
935 // Get the list of the device handles managed by this driver.
936 // If there is an error getting the list, then means the driver
937 // doesn't manage any device. At this way, we would only close
938 // those protocols installed at image handle.
940 DeviceHandleBuffer
= NULL
;
941 Status
= gBS
->LocateHandleBuffer (
949 if (!EFI_ERROR (Status
)) {
951 // Disconnect the driver specified by ImageHandle from all
952 // the devices in the handle database.
954 for (Index
= 0; Index
< DeviceHandleCount
; Index
++) {
955 Status
= gBS
->DisconnectController (
956 DeviceHandleBuffer
[Index
],
960 if (EFI_ERROR (Status
)) {
967 // Uninstall all the protocols installed in the driver entry point
969 Status
= gBS
->UninstallMultipleProtocolInterfaces (
971 &gEfiDriverBindingProtocolGuid
,
972 &gNvmExpressDriverBinding
,
973 &gEfiDriverSupportedEfiVersionProtocolGuid
,
974 &gNvmExpressDriverSupportedEfiVersion
,
978 if (EFI_ERROR (Status
)) {
983 // Note we have to one by one uninstall the following protocols.
984 // It's because some of them are optionally installed based on
985 // the following PCD settings.
986 // gEfiMdePkgTokenSpaceGuid.PcdDriverDiagnosticsDisable
987 // gEfiMdePkgTokenSpaceGuid.PcdComponentNameDisable
988 // gEfiMdePkgTokenSpaceGuid.PcdDriverDiagnostics2Disable
989 // gEfiMdePkgTokenSpaceGuid.PcdComponentName2Disable
991 Status
= gBS
->HandleProtocol (
993 &gEfiComponentNameProtocolGuid
,
994 (VOID
**) &ComponentName
996 if (!EFI_ERROR (Status
)) {
997 gBS
->UninstallProtocolInterface (
999 &gEfiComponentNameProtocolGuid
,
1004 Status
= gBS
->HandleProtocol (
1006 &gEfiComponentName2ProtocolGuid
,
1007 (VOID
**) &ComponentName2
1009 if (!EFI_ERROR (Status
)) {
1010 gBS
->UninstallProtocolInterface (
1012 &gEfiComponentName2ProtocolGuid
,
1017 Status
= EFI_SUCCESS
;
1021 // Free the buffer containing the list of handles from the handle database
1023 if (DeviceHandleBuffer
!= NULL
) {
1024 gBS
->FreePool (DeviceHandleBuffer
);
1030 The entry point for Nvm Express driver, used to install Nvm Express driver on the ImageHandle.
1032 @param ImageHandle The firmware allocated handle for this driver image.
1033 @param SystemTable Pointer to the EFI system table.
1035 @retval EFI_SUCCESS Driver loaded.
1036 @retval other Driver not loaded.
1041 NvmExpressDriverEntry (
1042 IN EFI_HANDLE ImageHandle
,
1043 IN EFI_SYSTEM_TABLE
*SystemTable
1048 Status
= EfiLibInstallDriverBindingComponentName2 (
1051 &gNvmExpressDriverBinding
,
1053 &gNvmExpressComponentName
,
1054 &gNvmExpressComponentName2
1056 ASSERT_EFI_ERROR (Status
);
1059 // Install EFI Driver Supported EFI Version Protocol required for
1060 // EFI drivers that are on PCI and other plug in cards.
1062 gNvmExpressDriverSupportedEfiVersion
.FirmwareVersion
= 0x00020028;
1063 Status
= gBS
->InstallMultipleProtocolInterfaces (
1065 &gEfiDriverSupportedEfiVersionProtocolGuid
,
1066 &gNvmExpressDriverSupportedEfiVersion
,
1069 ASSERT_EFI_ERROR (Status
);