2 NvmExpressDxe driver is used to manage non-volatile memory subsystem which follows
3 NVM Express specification.
5 Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>
6 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include "NvmExpress.h"
13 // NVM Express Driver Binding Protocol Instance
15 EFI_DRIVER_BINDING_PROTOCOL gNvmExpressDriverBinding
= {
16 NvmExpressDriverBindingSupported
,
17 NvmExpressDriverBindingStart
,
18 NvmExpressDriverBindingStop
,
25 // NVM Express EFI Driver Supported EFI Version Protocol Instance
27 EFI_DRIVER_SUPPORTED_EFI_VERSION_PROTOCOL gNvmExpressDriverSupportedEfiVersion
= {
28 sizeof (EFI_DRIVER_SUPPORTED_EFI_VERSION_PROTOCOL
), // Size of Protocol structure.
29 0 // Version number to be filled at start up.
33 // Template for NVM Express Pass Thru Mode data structure.
35 GLOBAL_REMOVE_IF_UNREFERENCED EFI_NVM_EXPRESS_PASS_THRU_MODE gEfiNvmExpressPassThruMode
= {
36 EFI_NVM_EXPRESS_PASS_THRU_ATTRIBUTES_PHYSICAL
|
37 EFI_NVM_EXPRESS_PASS_THRU_ATTRIBUTES_LOGICAL
|
38 EFI_NVM_EXPRESS_PASS_THRU_ATTRIBUTES_NONBLOCKIO
|
39 EFI_NVM_EXPRESS_PASS_THRU_ATTRIBUTES_CMD_SET_NVM
,
45 Check if the specified Nvm Express device namespace is active, and create child handles
46 for them with BlockIo and DiskInfo protocol instances.
48 @param[in] Private The pointer to the NVME_CONTROLLER_PRIVATE_DATA data structure.
49 @param[in] NamespaceId The NVM Express namespace ID for which a device path node is to be
50 allocated and built. Caller must set the NamespaceId to zero if the
51 device path node will contain a valid UUID.
53 @retval EFI_SUCCESS All the namespaces in the device are successfully enumerated.
54 @return Others Some error occurs when enumerating the namespaces.
58 EnumerateNvmeDevNamespace (
59 IN NVME_CONTROLLER_PRIVATE_DATA
*Private
,
63 NVME_ADMIN_NAMESPACE_DATA
*NamespaceData
;
64 EFI_DEVICE_PATH_PROTOCOL
*NewDevicePathNode
;
65 EFI_DEVICE_PATH_PROTOCOL
*DevicePath
;
66 EFI_HANDLE DeviceHandle
;
67 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
68 EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
;
69 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
)) {
104 // Validate Namespace
106 if (NamespaceData
->Ncap
== 0) {
107 Status
= EFI_DEVICE_ERROR
;
110 // allocate device private data for each discovered namespace
112 Device
= AllocateZeroPool (sizeof (NVME_DEVICE_PRIVATE_DATA
));
113 if (Device
== NULL
) {
114 Status
= EFI_OUT_OF_RESOURCES
;
119 // Initialize SSD namespace instance data
121 Device
->Signature
= NVME_DEVICE_PRIVATE_DATA_SIGNATURE
;
122 Device
->NamespaceId
= NamespaceId
;
123 Device
->NamespaceUuid
= NamespaceData
->Eui64
;
125 Device
->ControllerHandle
= Private
->ControllerHandle
;
126 Device
->DriverBindingHandle
= Private
->DriverBindingHandle
;
127 Device
->Controller
= Private
;
130 // Build BlockIo media structure
132 Device
->Media
.MediaId
= 0;
133 Device
->Media
.RemovableMedia
= FALSE
;
134 Device
->Media
.MediaPresent
= TRUE
;
135 Device
->Media
.LogicalPartition
= FALSE
;
136 Device
->Media
.ReadOnly
= FALSE
;
137 Device
->Media
.WriteCaching
= FALSE
;
138 Device
->Media
.IoAlign
= Private
->PassThruMode
.IoAlign
;
140 Flbas
= NamespaceData
->Flbas
;
141 LbaFmtIdx
= Flbas
& 0xF;
142 Lbads
= NamespaceData
->LbaFormat
[LbaFmtIdx
].Lbads
;
143 Device
->Media
.BlockSize
= (UINT32
)1 << Lbads
;
145 Device
->Media
.LastBlock
= NamespaceData
->Nsze
- 1;
146 Device
->Media
.LogicalBlocksPerPhysicalBlock
= 1;
147 Device
->Media
.LowestAlignedLba
= 1;
150 // Create BlockIo Protocol instance
152 Device
->BlockIo
.Revision
= EFI_BLOCK_IO_PROTOCOL_REVISION2
;
153 Device
->BlockIo
.Media
= &Device
->Media
;
154 Device
->BlockIo
.Reset
= NvmeBlockIoReset
;
155 Device
->BlockIo
.ReadBlocks
= NvmeBlockIoReadBlocks
;
156 Device
->BlockIo
.WriteBlocks
= NvmeBlockIoWriteBlocks
;
157 Device
->BlockIo
.FlushBlocks
= NvmeBlockIoFlushBlocks
;
160 // Create BlockIo2 Protocol instance
162 Device
->BlockIo2
.Media
= &Device
->Media
;
163 Device
->BlockIo2
.Reset
= NvmeBlockIoResetEx
;
164 Device
->BlockIo2
.ReadBlocksEx
= NvmeBlockIoReadBlocksEx
;
165 Device
->BlockIo2
.WriteBlocksEx
= NvmeBlockIoWriteBlocksEx
;
166 Device
->BlockIo2
.FlushBlocksEx
= NvmeBlockIoFlushBlocksEx
;
167 InitializeListHead (&Device
->AsyncQueue
);
170 // Create StorageSecurityProtocol Instance
172 Device
->StorageSecurity
.ReceiveData
= NvmeStorageSecurityReceiveData
;
173 Device
->StorageSecurity
.SendData
= NvmeStorageSecuritySendData
;
176 // Create DiskInfo Protocol instance
178 CopyMem (&Device
->NamespaceData
, NamespaceData
, sizeof (NVME_ADMIN_NAMESPACE_DATA
));
179 InitializeDiskInfo (Device
);
182 // Create a Nvm Express Namespace Device Path Node
184 Status
= Private
->Passthru
.BuildDevicePath (
190 if (EFI_ERROR (Status
)) {
195 // Append the SSD node to the controller's device path
197 DevicePath
= AppendDevicePathNode (ParentDevicePath
, NewDevicePathNode
);
198 if (DevicePath
== NULL
) {
199 Status
= EFI_OUT_OF_RESOURCES
;
204 RemainingDevicePath
= DevicePath
;
205 Status
= gBS
->LocateDevicePath (&gEfiDevicePathProtocolGuid
, &RemainingDevicePath
, &DeviceHandle
);
206 if (!EFI_ERROR (Status
) && (DeviceHandle
!= NULL
) && IsDevicePathEnd (RemainingDevicePath
)) {
207 Status
= EFI_ALREADY_STARTED
;
208 FreePool (DevicePath
);
212 Device
->DevicePath
= DevicePath
;
215 // Make sure the handle is NULL so we create a new handle
217 Device
->DeviceHandle
= NULL
;
219 Status
= gBS
->InstallMultipleProtocolInterfaces (
220 &Device
->DeviceHandle
,
221 &gEfiDevicePathProtocolGuid
,
223 &gEfiBlockIoProtocolGuid
,
225 &gEfiBlockIo2ProtocolGuid
,
227 &gEfiDiskInfoProtocolGuid
,
232 if (EFI_ERROR (Status
)) {
237 // Check if the NVMe controller supports the Security Send and Security Receive commands
239 if ((Private
->ControllerData
->Oacs
& SECURITY_SEND_RECEIVE_SUPPORTED
) != 0) {
240 Status
= gBS
->InstallProtocolInterface (
241 &Device
->DeviceHandle
,
242 &gEfiStorageSecurityCommandProtocolGuid
,
243 EFI_NATIVE_INTERFACE
,
244 &Device
->StorageSecurity
246 if (EFI_ERROR (Status
)) {
247 gBS
->UninstallMultipleProtocolInterfaces (
248 Device
->DeviceHandle
,
249 &gEfiDevicePathProtocolGuid
,
251 &gEfiBlockIoProtocolGuid
,
253 &gEfiBlockIo2ProtocolGuid
,
255 &gEfiDiskInfoProtocolGuid
,
264 Private
->ControllerHandle
,
265 &gEfiNvmExpressPassThruProtocolGuid
,
266 (VOID
**)&DummyInterface
,
267 Private
->DriverBindingHandle
,
268 Device
->DeviceHandle
,
269 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
273 // Dump NvmExpress Identify Namespace Data
275 DEBUG ((DEBUG_INFO
, " == NVME IDENTIFY NAMESPACE [%d] DATA ==\n", NamespaceId
));
276 DEBUG ((DEBUG_INFO
, " NSZE : 0x%x\n", NamespaceData
->Nsze
));
277 DEBUG ((DEBUG_INFO
, " NCAP : 0x%x\n", NamespaceData
->Ncap
));
278 DEBUG ((DEBUG_INFO
, " NUSE : 0x%x\n", NamespaceData
->Nuse
));
279 DEBUG ((DEBUG_INFO
, " LBAF0.LBADS : 0x%x\n", (NamespaceData
->LbaFormat
[0].Lbads
)));
282 // Build controller name for Component Name (2) protocol.
284 CopyMem (Sn
, Private
->ControllerData
->Sn
, sizeof (Private
->ControllerData
->Sn
));
286 CopyMem (Mn
, Private
->ControllerData
->Mn
, sizeof (Private
->ControllerData
->Mn
));
288 UnicodeSPrintAsciiFormat (Device
->ModelName
, sizeof (Device
->ModelName
), "%a-%a-%x", Sn
, Mn
, NamespaceData
->Eui64
);
292 gNvmExpressComponentName
.SupportedLanguages
,
293 &Device
->ControllerNameTable
,
300 gNvmExpressComponentName2
.SupportedLanguages
,
301 &Device
->ControllerNameTable
,
308 if (NamespaceData
!= NULL
) {
309 FreePool (NamespaceData
);
312 if (NewDevicePathNode
!= NULL
) {
313 FreePool (NewDevicePathNode
);
316 if (EFI_ERROR (Status
) && (Device
!= NULL
) && (Device
->DevicePath
!= NULL
)) {
317 FreePool (Device
->DevicePath
);
320 if (EFI_ERROR (Status
) && (Device
!= NULL
)) {
328 Discover all Nvm Express device namespaces, and create child handles for them with BlockIo
329 and DiskInfo protocol instances.
331 @param[in] Private The pointer to the NVME_CONTROLLER_PRIVATE_DATA data structure.
333 @retval EFI_SUCCESS All the namespaces in the device are successfully enumerated.
334 @return Others Some error occurs when enumerating the namespaces.
338 DiscoverAllNamespaces (
339 IN NVME_CONTROLLER_PRIVATE_DATA
*Private
344 EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL
*Passthru
;
346 NamespaceId
= 0xFFFFFFFF;
347 Passthru
= &Private
->Passthru
;
350 Status
= Passthru
->GetNextNamespace (
352 (UINT32
*)&NamespaceId
355 if (EFI_ERROR (Status
)) {
359 Status
= EnumerateNvmeDevNamespace (
364 if (EFI_ERROR (Status
)) {
373 Unregisters a Nvm Express device namespace.
375 This function removes the protocols installed on the controller handle and
376 frees the resources allocated for the namespace.
378 @param This The pointer to EFI_DRIVER_BINDING_PROTOCOL instance.
379 @param Controller The controller handle of the namespace.
380 @param Handle The child handle.
382 @retval EFI_SUCCESS The namespace is successfully unregistered.
383 @return Others Some error occurs when unregistering the namespace.
387 UnregisterNvmeNamespace (
388 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
389 IN EFI_HANDLE Controller
,
394 EFI_BLOCK_IO_PROTOCOL
*BlockIo
;
395 NVME_DEVICE_PRIVATE_DATA
*Device
;
396 EFI_STORAGE_SECURITY_COMMAND_PROTOCOL
*StorageSecurity
;
399 VOID
*DummyInterface
;
403 Status
= gBS
->OpenProtocol (
405 &gEfiBlockIoProtocolGuid
,
407 This
->DriverBindingHandle
,
409 EFI_OPEN_PROTOCOL_GET_PROTOCOL
411 if (EFI_ERROR (Status
)) {
415 Device
= NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO (BlockIo
);
418 // Wait for the device's asynchronous I/O queue to become empty.
421 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
422 IsEmpty
= IsListEmpty (&Device
->AsyncQueue
);
423 gBS
->RestoreTPL (OldTpl
);
433 // Close the child handle
437 &gEfiNvmExpressPassThruProtocolGuid
,
438 This
->DriverBindingHandle
,
443 // The Nvm Express driver installs the BlockIo and DiskInfo in the DriverBindingStart().
444 // Here should uninstall both of them.
446 Status
= gBS
->UninstallMultipleProtocolInterfaces (
448 &gEfiDevicePathProtocolGuid
,
450 &gEfiBlockIoProtocolGuid
,
452 &gEfiBlockIo2ProtocolGuid
,
454 &gEfiDiskInfoProtocolGuid
,
459 if (EFI_ERROR (Status
)) {
462 &gEfiNvmExpressPassThruProtocolGuid
,
463 (VOID
**)&DummyInterface
,
464 This
->DriverBindingHandle
,
466 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
472 // If Storage Security Command Protocol is installed, then uninstall this protocol.
474 Status
= gBS
->OpenProtocol (
476 &gEfiStorageSecurityCommandProtocolGuid
,
477 (VOID
**)&StorageSecurity
,
478 This
->DriverBindingHandle
,
480 EFI_OPEN_PROTOCOL_GET_PROTOCOL
483 if (!EFI_ERROR (Status
)) {
484 Status
= gBS
->UninstallProtocolInterface (
486 &gEfiStorageSecurityCommandProtocolGuid
,
487 &Device
->StorageSecurity
489 if (EFI_ERROR (Status
)) {
492 &gEfiNvmExpressPassThruProtocolGuid
,
493 (VOID
**)&DummyInterface
,
494 This
->DriverBindingHandle
,
496 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
502 if (Device
->DevicePath
!= NULL
) {
503 FreePool (Device
->DevicePath
);
506 if (Device
->ControllerNameTable
!= NULL
) {
507 FreeUnicodeStringTable (Device
->ControllerNameTable
);
516 Call back function when the timer event is signaled.
518 @param[in] Event The Event this notify function registered to.
519 @param[in] Context Pointer to the context data registered to the
525 ProcessAsyncTaskList (
530 NVME_CONTROLLER_PRIVATE_DATA
*Private
;
531 EFI_PCI_IO_PROTOCOL
*PciIo
;
536 LIST_ENTRY
*NextLink
;
537 NVME_PASS_THRU_ASYNC_REQ
*AsyncRequest
;
538 NVME_BLKIO2_SUBTASK
*Subtask
;
539 NVME_BLKIO2_REQUEST
*BlkIo2Request
;
540 EFI_BLOCK_IO2_TOKEN
*Token
;
544 Private
= (NVME_CONTROLLER_PRIVATE_DATA
*)Context
;
546 Cq
= Private
->CqBuffer
[QueueId
] + Private
->CqHdbl
[QueueId
].Cqh
;
548 PciIo
= Private
->PciIo
;
551 // Submit asynchronous subtasks to the NVMe Submission Queue
553 for (Link
= GetFirstNode (&Private
->UnsubmittedSubtasks
);
554 !IsNull (&Private
->UnsubmittedSubtasks
, Link
);
557 NextLink
= GetNextNode (&Private
->UnsubmittedSubtasks
, Link
);
558 Subtask
= NVME_BLKIO2_SUBTASK_FROM_LINK (Link
);
559 BlkIo2Request
= Subtask
->BlockIo2Request
;
560 Token
= BlkIo2Request
->Token
;
561 RemoveEntryList (Link
);
562 BlkIo2Request
->UnsubmittedSubtaskNum
--;
565 // If any previous subtask fails, do not process subsequent ones.
567 if (Token
->TransactionStatus
!= EFI_SUCCESS
) {
568 if (IsListEmpty (&BlkIo2Request
->SubtasksQueue
) &&
569 BlkIo2Request
->LastSubtaskSubmitted
&&
570 (BlkIo2Request
->UnsubmittedSubtaskNum
== 0))
573 // Remove the BlockIo2 request from the device asynchronous queue.
575 RemoveEntryList (&BlkIo2Request
->Link
);
576 FreePool (BlkIo2Request
);
577 gBS
->SignalEvent (Token
->Event
);
580 FreePool (Subtask
->CommandPacket
->NvmeCmd
);
581 FreePool (Subtask
->CommandPacket
->NvmeCompletion
);
582 FreePool (Subtask
->CommandPacket
);
588 Status
= Private
->Passthru
.PassThru (
590 Subtask
->NamespaceId
,
591 Subtask
->CommandPacket
,
594 if (Status
== EFI_NOT_READY
) {
595 InsertHeadList (&Private
->UnsubmittedSubtasks
, Link
);
596 BlkIo2Request
->UnsubmittedSubtaskNum
++;
598 } else if (EFI_ERROR (Status
)) {
599 Token
->TransactionStatus
= EFI_DEVICE_ERROR
;
601 if (IsListEmpty (&BlkIo2Request
->SubtasksQueue
) &&
605 // Remove the BlockIo2 request from the device asynchronous queue.
607 RemoveEntryList (&BlkIo2Request
->Link
);
608 FreePool (BlkIo2Request
);
609 gBS
->SignalEvent (Token
->Event
);
612 FreePool (Subtask
->CommandPacket
->NvmeCmd
);
613 FreePool (Subtask
->CommandPacket
->NvmeCompletion
);
614 FreePool (Subtask
->CommandPacket
);
617 InsertTailList (&BlkIo2Request
->SubtasksQueue
, Link
);
618 if (Subtask
->IsLast
) {
619 BlkIo2Request
->LastSubtaskSubmitted
= TRUE
;
624 while (Cq
->Pt
!= Private
->Pt
[QueueId
]) {
625 ASSERT (Cq
->Sqid
== QueueId
);
630 // Find the command with given Command Id.
632 for (Link
= GetFirstNode (&Private
->AsyncPassThruQueue
);
633 !IsNull (&Private
->AsyncPassThruQueue
, Link
);
636 NextLink
= GetNextNode (&Private
->AsyncPassThruQueue
, Link
);
637 AsyncRequest
= NVME_PASS_THRU_ASYNC_REQ_FROM_THIS (Link
);
638 if (AsyncRequest
->CommandId
== Cq
->Cid
) {
640 // Copy the Respose Queue entry for this command to the callers
644 AsyncRequest
->Packet
->NvmeCompletion
,
646 sizeof (EFI_NVM_EXPRESS_COMPLETION
)
650 // Free the resources allocated before cmd submission
652 if (AsyncRequest
->MapData
!= NULL
) {
653 PciIo
->Unmap (PciIo
, AsyncRequest
->MapData
);
656 if (AsyncRequest
->MapMeta
!= NULL
) {
657 PciIo
->Unmap (PciIo
, AsyncRequest
->MapMeta
);
660 if (AsyncRequest
->MapPrpList
!= NULL
) {
661 PciIo
->Unmap (PciIo
, AsyncRequest
->MapPrpList
);
664 if (AsyncRequest
->PrpListHost
!= NULL
) {
667 AsyncRequest
->PrpListNo
,
668 AsyncRequest
->PrpListHost
672 RemoveEntryList (Link
);
673 gBS
->SignalEvent (AsyncRequest
->CallerEvent
);
674 FreePool (AsyncRequest
);
677 // Update submission queue head.
679 Private
->AsyncSqHead
= Cq
->Sqhd
;
684 Private
->CqHdbl
[QueueId
].Cqh
++;
685 if (Private
->CqHdbl
[QueueId
].Cqh
> MIN (NVME_ASYNC_CCQ_SIZE
, Private
->Cap
.Mqes
)) {
686 Private
->CqHdbl
[QueueId
].Cqh
= 0;
687 Private
->Pt
[QueueId
] ^= 1;
690 Cq
= Private
->CqBuffer
[QueueId
] + Private
->CqHdbl
[QueueId
].Cqh
;
694 Data
= ReadUnaligned32 ((UINT32
*)&Private
->CqHdbl
[QueueId
]);
699 NVME_CQHDBL_OFFSET (QueueId
, Private
->Cap
.Dstrd
),
707 Tests to see if this driver supports a given controller. If a child device is provided,
708 it further tests to see if this driver supports creating a handle for the specified child device.
710 This function checks to see if the driver specified by This supports the device specified by
711 ControllerHandle. Drivers will typically use the device path attached to
712 ControllerHandle and/or the services from the bus I/O abstraction attached to
713 ControllerHandle to determine if the driver supports ControllerHandle. This function
714 may be called many times during platform initialization. In order to reduce boot times, the tests
715 performed by this function must be very small, and take as little time as possible to execute. This
716 function must not change the state of any hardware devices, and this function must be aware that the
717 device specified by ControllerHandle may already be managed by the same driver or a
718 different driver. This function must match its calls to AllocatePages() with FreePages(),
719 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
720 Since ControllerHandle may have been previously started by the same driver, if a protocol is
721 already in the opened state, then it must not be closed with CloseProtocol(). This is required
722 to guarantee the state of ControllerHandle is not modified by this function.
724 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
725 @param[in] ControllerHandle The handle of the controller to test. This handle
726 must support a protocol interface that supplies
727 an I/O abstraction to the driver.
728 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
729 parameter is ignored by device drivers, and is optional for bus
730 drivers. For bus drivers, if this parameter is not NULL, then
731 the bus driver must determine if the bus controller specified
732 by ControllerHandle and the child controller specified
733 by RemainingDevicePath are both supported by this
736 @retval EFI_SUCCESS The device specified by ControllerHandle and
737 RemainingDevicePath is supported by the driver specified by This.
738 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
739 RemainingDevicePath is already being managed by the driver
741 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
742 RemainingDevicePath is already being managed by a different
743 driver or an application that requires exclusive access.
744 Currently not implemented.
745 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
746 RemainingDevicePath is not supported by the driver specified by This.
750 NvmExpressDriverBindingSupported (
751 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
752 IN EFI_HANDLE Controller
,
753 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
757 EFI_DEV_PATH_PTR DevicePathNode
;
758 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
759 EFI_PCI_IO_PROTOCOL
*PciIo
;
763 // Check whether device path is valid
765 if (RemainingDevicePath
!= NULL
) {
767 // Check if RemainingDevicePath is the End of Device Path Node,
768 // if yes, go on checking other conditions
770 if (!IsDevicePathEnd (RemainingDevicePath
)) {
772 // If RemainingDevicePath isn't the End of Device Path Node,
773 // check its validation
775 DevicePathNode
.DevPath
= RemainingDevicePath
;
777 if ((DevicePathNode
.DevPath
->Type
!= MESSAGING_DEVICE_PATH
) ||
778 (DevicePathNode
.DevPath
->SubType
!= MSG_NVME_NAMESPACE_DP
) ||
779 (DevicePathNodeLength (DevicePathNode
.DevPath
) != sizeof (NVME_NAMESPACE_DEVICE_PATH
)))
781 return EFI_UNSUPPORTED
;
787 // Open the EFI Device Path protocol needed to perform the supported test
789 Status
= gBS
->OpenProtocol (
791 &gEfiDevicePathProtocolGuid
,
792 (VOID
**)&ParentDevicePath
,
793 This
->DriverBindingHandle
,
795 EFI_OPEN_PROTOCOL_BY_DRIVER
797 if (Status
== EFI_ALREADY_STARTED
) {
801 if (EFI_ERROR (Status
)) {
806 // Close protocol, don't use device path protocol in the Support() function
810 &gEfiDevicePathProtocolGuid
,
811 This
->DriverBindingHandle
,
816 // Attempt to Open PCI I/O Protocol
818 Status
= gBS
->OpenProtocol (
820 &gEfiPciIoProtocolGuid
,
822 This
->DriverBindingHandle
,
824 EFI_OPEN_PROTOCOL_BY_DRIVER
826 if (Status
== EFI_ALREADY_STARTED
) {
830 if (EFI_ERROR (Status
)) {
835 // Now further check the PCI header: Base class (offset 0x0B) and Sub Class (offset 0x0A).
836 // This controller should be a Nvm Express controller.
838 Status
= PciIo
->Pci
.Read (
841 PCI_CLASSCODE_OFFSET
,
845 if (EFI_ERROR (Status
)) {
850 // Examine Nvm Express controller PCI Configuration table fields
852 if ((ClassCode
[0] != PCI_IF_NVMHCI
) || (ClassCode
[1] != PCI_CLASS_MASS_STORAGE_NVM
) || (ClassCode
[2] != PCI_CLASS_MASS_STORAGE
)) {
853 Status
= EFI_UNSUPPORTED
;
859 &gEfiPciIoProtocolGuid
,
860 This
->DriverBindingHandle
,
868 Starts a device controller or a bus controller.
870 The Start() function is designed to be invoked from the EFI boot service ConnectController().
871 As a result, much of the error checking on the parameters to Start() has been moved into this
872 common boot service. It is legal to call Start() from other locations,
873 but the following calling restrictions must be followed or the system behavior will not be deterministic.
874 1. ControllerHandle must be a valid EFI_HANDLE.
875 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
876 EFI_DEVICE_PATH_PROTOCOL.
877 3. Prior to calling Start(), the Supported() function for the driver specified by This must
878 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
880 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
881 @param[in] ControllerHandle The handle of the controller to start. This handle
882 must support a protocol interface that supplies
883 an I/O abstraction to the driver.
884 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
885 parameter is ignored by device drivers, and is optional for bus
886 drivers. For a bus driver, if this parameter is NULL, then handles
887 for all the children of Controller are created by this driver.
888 If this parameter is not NULL and the first Device Path Node is
889 not the End of Device Path Node, then only the handle for the
890 child device specified by the first Device Path Node of
891 RemainingDevicePath is created by this driver.
892 If the first Device Path Node of RemainingDevicePath is
893 the End of Device Path Node, no child handle is created by this
896 @retval EFI_SUCCESS The device was started.
897 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
898 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
899 @retval Others The driver failded to start the device.
904 NvmExpressDriverBindingStart (
905 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
906 IN EFI_HANDLE Controller
,
907 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
911 EFI_PCI_IO_PROTOCOL
*PciIo
;
912 NVME_CONTROLLER_PRIVATE_DATA
*Private
;
913 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
915 EFI_PHYSICAL_ADDRESS MappedAddr
;
917 EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL
*Passthru
;
919 DEBUG ((DEBUG_INFO
, "NvmExpressDriverBindingStart: start\n"));
923 ParentDevicePath
= NULL
;
925 Status
= gBS
->OpenProtocol (
927 &gEfiDevicePathProtocolGuid
,
928 (VOID
**)&ParentDevicePath
,
929 This
->DriverBindingHandle
,
931 EFI_OPEN_PROTOCOL_BY_DRIVER
933 if ((EFI_ERROR (Status
)) && (Status
!= EFI_ALREADY_STARTED
)) {
937 Status
= gBS
->OpenProtocol (
939 &gEfiPciIoProtocolGuid
,
941 This
->DriverBindingHandle
,
943 EFI_OPEN_PROTOCOL_BY_DRIVER
946 if (EFI_ERROR (Status
) && (Status
!= EFI_ALREADY_STARTED
)) {
951 // Check EFI_ALREADY_STARTED to reuse the original NVME_CONTROLLER_PRIVATE_DATA.
953 if (Status
!= EFI_ALREADY_STARTED
) {
954 Private
= AllocateZeroPool (sizeof (NVME_CONTROLLER_PRIVATE_DATA
));
956 if (Private
== NULL
) {
957 DEBUG ((DEBUG_ERROR
, "NvmExpressDriverBindingStart: allocating pool for Nvme Private Data failed!\n"));
958 Status
= EFI_OUT_OF_RESOURCES
;
963 // Save original PCI attributes
965 Status
= PciIo
->Attributes (
967 EfiPciIoAttributeOperationGet
,
969 &Private
->PciAttributes
972 if (EFI_ERROR (Status
)) {
977 // Enable 64-bit DMA support in the PCI layer.
979 Status
= PciIo
->Attributes (
981 EfiPciIoAttributeOperationEnable
,
982 EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE
,
985 if (EFI_ERROR (Status
)) {
986 DEBUG ((DEBUG_WARN
, "NvmExpressDriverBindingStart: failed to enable 64-bit DMA (%r)\n", Status
));
990 // 6 x 4kB aligned buffers will be carved out of this buffer.
991 // 1st 4kB boundary is the start of the admin submission queue.
992 // 2nd 4kB boundary is the start of the admin completion queue.
993 // 3rd 4kB boundary is the start of I/O submission queue #1.
994 // 4th 4kB boundary is the start of I/O completion queue #1.
995 // 5th 4kB boundary is the start of I/O submission queue #2.
996 // 6th 4kB boundary is the start of I/O completion queue #2.
998 // Allocate 6 pages of memory, then map it for bus master read and write.
1000 Status
= PciIo
->AllocateBuffer (
1003 EfiBootServicesData
,
1005 (VOID
**)&Private
->Buffer
,
1008 if (EFI_ERROR (Status
)) {
1012 Bytes
= EFI_PAGES_TO_SIZE (6);
1013 Status
= PciIo
->Map (
1015 EfiPciIoOperationBusMasterCommonBuffer
,
1022 if (EFI_ERROR (Status
) || (Bytes
!= EFI_PAGES_TO_SIZE (6))) {
1026 Private
->BufferPciAddr
= (UINT8
*)(UINTN
)MappedAddr
;
1028 Private
->Signature
= NVME_CONTROLLER_PRIVATE_DATA_SIGNATURE
;
1029 Private
->ControllerHandle
= Controller
;
1030 Private
->ImageHandle
= This
->DriverBindingHandle
;
1031 Private
->DriverBindingHandle
= This
->DriverBindingHandle
;
1032 Private
->PciIo
= PciIo
;
1033 Private
->ParentDevicePath
= ParentDevicePath
;
1034 Private
->Passthru
.Mode
= &Private
->PassThruMode
;
1035 Private
->Passthru
.PassThru
= NvmExpressPassThru
;
1036 Private
->Passthru
.GetNextNamespace
= NvmExpressGetNextNamespace
;
1037 Private
->Passthru
.BuildDevicePath
= NvmExpressBuildDevicePath
;
1038 Private
->Passthru
.GetNamespace
= NvmExpressGetNamespace
;
1039 CopyMem (&Private
->PassThruMode
, &gEfiNvmExpressPassThruMode
, sizeof (EFI_NVM_EXPRESS_PASS_THRU_MODE
));
1040 InitializeListHead (&Private
->AsyncPassThruQueue
);
1041 InitializeListHead (&Private
->UnsubmittedSubtasks
);
1043 Status
= NvmeControllerInit (Private
);
1044 if (EFI_ERROR (Status
)) {
1049 // Start the asynchronous I/O completion monitor
1051 Status
= gBS
->CreateEvent (
1052 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
1054 ProcessAsyncTaskList
,
1056 &Private
->TimerEvent
1058 if (EFI_ERROR (Status
)) {
1062 Status
= gBS
->SetTimer (
1063 Private
->TimerEvent
,
1067 if (EFI_ERROR (Status
)) {
1071 Status
= gBS
->InstallMultipleProtocolInterfaces (
1073 &gEfiNvmExpressPassThruProtocolGuid
,
1077 if (EFI_ERROR (Status
)) {
1081 NvmeRegisterShutdownNotification ();
1083 Status
= gBS
->OpenProtocol (
1085 &gEfiNvmExpressPassThruProtocolGuid
,
1087 This
->DriverBindingHandle
,
1089 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1091 if (EFI_ERROR (Status
)) {
1095 Private
= NVME_CONTROLLER_PRIVATE_DATA_FROM_PASS_THRU (Passthru
);
1098 if (RemainingDevicePath
== NULL
) {
1100 // Enumerate all NVME namespaces in the controller
1102 Status
= DiscoverAllNamespaces (
1105 } else if (!IsDevicePathEnd (RemainingDevicePath
)) {
1107 // Enumerate the specified NVME namespace
1109 Status
= Private
->Passthru
.GetNamespace (
1111 RemainingDevicePath
,
1115 if (!EFI_ERROR (Status
)) {
1116 Status
= EnumerateNvmeDevNamespace (
1123 DEBUG ((DEBUG_INFO
, "NvmExpressDriverBindingStart: end successfully\n"));
1127 if ((Private
!= NULL
) && (Private
->Mapping
!= NULL
)) {
1128 PciIo
->Unmap (PciIo
, Private
->Mapping
);
1131 if ((Private
!= NULL
) && (Private
->Buffer
!= NULL
)) {
1132 PciIo
->FreeBuffer (PciIo
, 6, Private
->Buffer
);
1135 if ((Private
!= NULL
) && (Private
->ControllerData
!= NULL
)) {
1136 FreePool (Private
->ControllerData
);
1139 if (Private
!= NULL
) {
1140 if (Private
->TimerEvent
!= NULL
) {
1141 gBS
->CloseEvent (Private
->TimerEvent
);
1147 gBS
->CloseProtocol (
1149 &gEfiPciIoProtocolGuid
,
1150 This
->DriverBindingHandle
,
1154 gBS
->CloseProtocol (
1156 &gEfiDevicePathProtocolGuid
,
1157 This
->DriverBindingHandle
,
1161 DEBUG ((DEBUG_INFO
, "NvmExpressDriverBindingStart: end with %r\n", Status
));
1167 Stops a device controller or a bus controller.
1169 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
1170 As a result, much of the error checking on the parameters to Stop() has been moved
1171 into this common boot service. It is legal to call Stop() from other locations,
1172 but the following calling restrictions must be followed or the system behavior will not be deterministic.
1173 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
1174 same driver's Start() function.
1175 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
1176 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
1177 Start() function, and the Start() function must have called OpenProtocol() on
1178 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
1180 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
1181 @param[in] ControllerHandle A handle to the device being stopped. The handle must
1182 support a bus specific I/O protocol for the driver
1183 to use to stop the device.
1184 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
1185 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
1186 if NumberOfChildren is 0.
1188 @retval EFI_SUCCESS The device was stopped.
1189 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
1194 NvmExpressDriverBindingStop (
1195 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1196 IN EFI_HANDLE Controller
,
1197 IN UINTN NumberOfChildren
,
1198 IN EFI_HANDLE
*ChildHandleBuffer
1202 BOOLEAN AllChildrenStopped
;
1204 NVME_CONTROLLER_PRIVATE_DATA
*Private
;
1205 EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL
*PassThru
;
1209 if (NumberOfChildren
== 0) {
1210 Status
= gBS
->OpenProtocol (
1212 &gEfiNvmExpressPassThruProtocolGuid
,
1214 This
->DriverBindingHandle
,
1216 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1219 if (!EFI_ERROR (Status
)) {
1220 Private
= NVME_CONTROLLER_PRIVATE_DATA_FROM_PASS_THRU (PassThru
);
1223 // Wait for the asynchronous PassThru queue to become empty.
1226 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1227 IsEmpty
= IsListEmpty (&Private
->AsyncPassThruQueue
) &&
1228 IsListEmpty (&Private
->UnsubmittedSubtasks
);
1229 gBS
->RestoreTPL (OldTpl
);
1238 gBS
->UninstallMultipleProtocolInterfaces (
1240 &gEfiNvmExpressPassThruProtocolGuid
,
1245 if (Private
->TimerEvent
!= NULL
) {
1246 gBS
->CloseEvent (Private
->TimerEvent
);
1249 if (Private
->Mapping
!= NULL
) {
1250 Private
->PciIo
->Unmap (Private
->PciIo
, Private
->Mapping
);
1253 if (Private
->Buffer
!= NULL
) {
1254 Private
->PciIo
->FreeBuffer (Private
->PciIo
, 6, Private
->Buffer
);
1257 FreePool (Private
->ControllerData
);
1261 gBS
->CloseProtocol (
1263 &gEfiPciIoProtocolGuid
,
1264 This
->DriverBindingHandle
,
1267 gBS
->CloseProtocol (
1269 &gEfiDevicePathProtocolGuid
,
1270 This
->DriverBindingHandle
,
1274 NvmeUnregisterShutdownNotification ();
1279 AllChildrenStopped
= TRUE
;
1281 for (Index
= 0; Index
< NumberOfChildren
; Index
++) {
1282 Status
= UnregisterNvmeNamespace (This
, Controller
, ChildHandleBuffer
[Index
]);
1283 if (EFI_ERROR (Status
)) {
1284 AllChildrenStopped
= FALSE
;
1288 if (!AllChildrenStopped
) {
1289 return EFI_DEVICE_ERROR
;
1296 This is the unload handle for the NVM Express driver.
1298 Disconnect the driver specified by ImageHandle from the NVMe device in the handle database.
1299 Uninstall all the protocols installed in the driver.
1301 @param[in] ImageHandle The drivers' driver image.
1303 @retval EFI_SUCCESS The image is unloaded.
1304 @retval Others Failed to unload the image.
1310 IN EFI_HANDLE ImageHandle
1314 EFI_HANDLE
*DeviceHandleBuffer
;
1315 UINTN DeviceHandleCount
;
1317 EFI_COMPONENT_NAME_PROTOCOL
*ComponentName
;
1318 EFI_COMPONENT_NAME2_PROTOCOL
*ComponentName2
;
1321 // Get the list of the device handles managed by this driver.
1322 // If there is an error getting the list, then means the driver
1323 // doesn't manage any device. At this way, we would only close
1324 // those protocols installed at image handle.
1326 DeviceHandleBuffer
= NULL
;
1327 Status
= gBS
->LocateHandleBuffer (
1329 &gEfiNvmExpressPassThruProtocolGuid
,
1335 if (!EFI_ERROR (Status
)) {
1337 // Disconnect the driver specified by ImageHandle from all
1338 // the devices in the handle database.
1340 for (Index
= 0; Index
< DeviceHandleCount
; Index
++) {
1341 Status
= gBS
->DisconnectController (
1342 DeviceHandleBuffer
[Index
],
1346 if (EFI_ERROR (Status
)) {
1353 // Uninstall all the protocols installed in the driver entry point
1355 Status
= gBS
->UninstallMultipleProtocolInterfaces (
1357 &gEfiDriverBindingProtocolGuid
,
1358 &gNvmExpressDriverBinding
,
1359 &gEfiDriverSupportedEfiVersionProtocolGuid
,
1360 &gNvmExpressDriverSupportedEfiVersion
,
1364 if (EFI_ERROR (Status
)) {
1369 // Note we have to one by one uninstall the following protocols.
1370 // It's because some of them are optionally installed based on
1371 // the following PCD settings.
1372 // gEfiMdePkgTokenSpaceGuid.PcdDriverDiagnosticsDisable
1373 // gEfiMdePkgTokenSpaceGuid.PcdComponentNameDisable
1374 // gEfiMdePkgTokenSpaceGuid.PcdDriverDiagnostics2Disable
1375 // gEfiMdePkgTokenSpaceGuid.PcdComponentName2Disable
1377 Status
= gBS
->HandleProtocol (
1379 &gEfiComponentNameProtocolGuid
,
1380 (VOID
**)&ComponentName
1382 if (!EFI_ERROR (Status
)) {
1383 gBS
->UninstallProtocolInterface (
1385 &gEfiComponentNameProtocolGuid
,
1390 Status
= gBS
->HandleProtocol (
1392 &gEfiComponentName2ProtocolGuid
,
1393 (VOID
**)&ComponentName2
1395 if (!EFI_ERROR (Status
)) {
1396 gBS
->UninstallProtocolInterface (
1398 &gEfiComponentName2ProtocolGuid
,
1403 Status
= EFI_SUCCESS
;
1407 // Free the buffer containing the list of handles from the handle database
1409 if (DeviceHandleBuffer
!= NULL
) {
1410 gBS
->FreePool (DeviceHandleBuffer
);
1417 The entry point for Nvm Express driver, used to install Nvm Express driver on the ImageHandle.
1419 @param ImageHandle The firmware allocated handle for this driver image.
1420 @param SystemTable Pointer to the EFI system table.
1422 @retval EFI_SUCCESS Driver loaded.
1423 @retval other Driver not loaded.
1428 NvmExpressDriverEntry (
1429 IN EFI_HANDLE ImageHandle
,
1430 IN EFI_SYSTEM_TABLE
*SystemTable
1435 Status
= EfiLibInstallDriverBindingComponentName2 (
1438 &gNvmExpressDriverBinding
,
1440 &gNvmExpressComponentName
,
1441 &gNvmExpressComponentName2
1443 ASSERT_EFI_ERROR (Status
);
1446 // Install EFI Driver Supported EFI Version Protocol required for
1447 // EFI drivers that are on PCI and other plug in cards.
1449 gNvmExpressDriverSupportedEfiVersion
.FirmwareVersion
= 0x00020028;
1450 Status
= gBS
->InstallMultipleProtocolInterfaces (
1452 &gEfiDriverSupportedEfiVersionProtocolGuid
,
1453 &gNvmExpressDriverSupportedEfiVersion
,
1456 ASSERT_EFI_ERROR (Status
);