3 Copyright (c) 2014 - 2021, Intel Corporation. All rights reserved.<BR>
4 Copyright (c) Microsoft Corporation.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
9 #include "UfsPassThru.h"
12 // Template for Ufs Pass Thru private data.
14 UFS_PASS_THRU_PRIVATE_DATA gUfsPassThruTemplate
= {
15 UFS_PASS_THRU_SIG
, // Signature
17 { // ExtScsiPassThruMode
19 EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_PHYSICAL
| EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL
| EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_NONBLOCKIO
,
25 UfsPassThruGetNextTargetLun
,
26 UfsPassThruBuildDevicePath
,
27 UfsPassThruGetTargetLun
,
28 UfsPassThruResetChannel
,
29 UfsPassThruResetTargetLun
,
30 UfsPassThruGetNextTarget
37 0, // UfsHostController
39 { 0, 0 }, // UfsHcInfo
40 { NULL
, NULL
}, // UfsHcDriverInterface
50 UFS_LUN_0
, // Ufs Common Lun 0
51 UFS_LUN_1
, // Ufs Common Lun 1
52 UFS_LUN_2
, // Ufs Common Lun 2
53 UFS_LUN_3
, // Ufs Common Lun 3
54 UFS_LUN_4
, // Ufs Common Lun 4
55 UFS_LUN_5
, // Ufs Common Lun 5
56 UFS_LUN_6
, // Ufs Common Lun 6
57 UFS_LUN_7
, // Ufs Common Lun 7
58 UFS_WLUN_REPORT_LUNS
, // Ufs Reports Luns Well Known Lun
59 UFS_WLUN_UFS_DEV
, // Ufs Device Well Known Lun
60 UFS_WLUN_BOOT
, // Ufs Boot Well Known Lun
61 UFS_WLUN_RPMB
// RPMB Well Known Lun
63 0x0000, // By default don't expose any Luns.
73 EFI_DRIVER_BINDING_PROTOCOL gUfsPassThruDriverBinding
= {
74 UfsPassThruDriverBindingSupported
,
75 UfsPassThruDriverBindingStart
,
76 UfsPassThruDriverBindingStop
,
82 UFS_DEVICE_PATH mUfsDevicePathTemplate
= {
84 MESSAGING_DEVICE_PATH
,
87 (UINT8
)(sizeof (UFS_DEVICE_PATH
)),
88 (UINT8
)((sizeof (UFS_DEVICE_PATH
)) >> 8)
95 UINT8 mUfsTargetId
[TARGET_MAX_BYTES
];
97 GLOBAL_REMOVE_IF_UNREFERENCED EDKII_UFS_HC_PLATFORM_PROTOCOL
*mUfsHcPlatform
;
100 Sends a SCSI Request Packet to a SCSI device that is attached to the SCSI channel. This function
101 supports both blocking I/O and nonblocking I/O. The blocking I/O functionality is required, and the
102 nonblocking I/O functionality is optional.
104 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
105 @param Target The Target is an array of size TARGET_MAX_BYTES and it represents
106 the id of the SCSI device to send the SCSI Request Packet. Each
107 transport driver may choose to utilize a subset of this size to suit the needs
108 of transport target representation. For example, a Fibre Channel driver
109 may use only 8 bytes (WWN) to represent an FC target.
110 @param Lun The LUN of the SCSI device to send the SCSI Request Packet.
111 @param Packet A pointer to the SCSI Request Packet to send to the SCSI device
112 specified by Target and Lun.
113 @param Event If nonblocking I/O is not supported then Event is ignored, and blocking
114 I/O is performed. If Event is NULL, then blocking I/O is performed. If
115 Event is not NULL and non blocking I/O is supported, then
116 nonblocking I/O is performed, and Event will be signaled when the
117 SCSI Request Packet completes.
119 @retval EFI_SUCCESS The SCSI Request Packet was sent by the host. For bi-directional
120 commands, InTransferLength bytes were transferred from
121 InDataBuffer. For write and bi-directional commands,
122 OutTransferLength bytes were transferred by
124 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was not executed. The number of bytes that
125 could be transferred is returned in InTransferLength. For write
126 and bi-directional commands, OutTransferLength bytes were
127 transferred by OutDataBuffer.
128 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because there are too many
129 SCSI Request Packets already queued. The caller may retry again later.
130 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SCSI Request
132 @retval EFI_INVALID_PARAMETER Target, Lun, or the contents of ScsiRequestPacket are invalid.
133 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet is not supported
134 by the host adapter. This includes the case of Bi-directional SCSI
135 commands not supported by the implementation. The SCSI Request
136 Packet was not sent, so no additional status information is available.
137 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
142 UfsPassThruPassThru (
143 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
146 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
147 IN EFI_EVENT Event OPTIONAL
151 UFS_PASS_THRU_PRIVATE_DATA
*Private
;
155 Private
= UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
157 if ((Packet
== NULL
) || (Packet
->Cdb
== NULL
)) {
158 return EFI_INVALID_PARAMETER
;
162 // Don't support variable length CDB
164 if ((Packet
->CdbLength
!= 6) && (Packet
->CdbLength
!= 10) &&
165 (Packet
->CdbLength
!= 12) && (Packet
->CdbLength
!= 16))
167 return EFI_INVALID_PARAMETER
;
170 if ((Packet
->SenseDataLength
!= 0) && (Packet
->SenseData
== NULL
)) {
171 return EFI_INVALID_PARAMETER
;
174 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED (Packet
->InDataBuffer
, This
->Mode
->IoAlign
)) {
175 return EFI_INVALID_PARAMETER
;
178 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED (Packet
->OutDataBuffer
, This
->Mode
->IoAlign
)) {
179 return EFI_INVALID_PARAMETER
;
182 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED (Packet
->SenseData
, This
->Mode
->IoAlign
)) {
183 return EFI_INVALID_PARAMETER
;
187 // For UFS 2.0 compatible device, 0 is always used to represent the location of the UFS device.
189 SetMem (mUfsTargetId
, TARGET_MAX_BYTES
, 0x00);
190 if ((Target
== NULL
) || (CompareMem (Target
, mUfsTargetId
, TARGET_MAX_BYTES
) != 0)) {
191 return EFI_INVALID_PARAMETER
;
195 // UFS 2.0 spec Section 10.6.7 - Translation of 8-bit UFS LUN to 64-bit SCSI LUN Address
196 // 0xC1 in the first 8 bits of the 64-bit address indicates a well known LUN address in the SAM SCSI format.
197 // The second 8 bits of the 64-bit address saves the corresponding 8-bit UFS LUN.
199 if ((UINT8
)Lun
== UFS_WLUN_PREFIX
) {
200 UfsLun
= BIT7
| (((UINT8
*)&Lun
)[1] & 0xFF);
201 } else if ((UINT8
)Lun
== 0) {
202 UfsLun
= ((UINT8
*)&Lun
)[1] & 0xFF;
204 return EFI_INVALID_PARAMETER
;
207 for (Index
= 0; Index
< UFS_MAX_LUNS
; Index
++) {
208 if ((Private
->Luns
.BitMask
& (BIT0
<< Index
)) == 0) {
212 if (Private
->Luns
.Lun
[Index
] == UfsLun
) {
217 if (Index
== UFS_MAX_LUNS
) {
218 return EFI_INVALID_PARAMETER
;
221 Status
= UfsExecScsiCmds (Private
, UfsLun
, Packet
, Event
);
227 Used to retrieve the list of legal Target IDs and LUNs for SCSI devices on a SCSI channel. These
228 can either be the list SCSI devices that are actually present on the SCSI channel, or the list of legal
229 Target Ids and LUNs for the SCSI channel. Regardless, the caller of this function must probe the
230 Target ID and LUN returned to see if a SCSI device is actually present at that location on the SCSI
233 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
234 @param Target On input, a pointer to the Target ID (an array of size
235 TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.
236 On output, a pointer to the Target ID (an array of
237 TARGET_MAX_BYTES) of the next SCSI device present on a SCSI
238 channel. An input value of 0xF(all bytes in the array are 0xF) in the
239 Target array retrieves the Target ID of the first SCSI device present on a
241 @param Lun On input, a pointer to the LUN of a SCSI device present on the SCSI
242 channel. On output, a pointer to the LUN of the next SCSI device present
245 @retval EFI_SUCCESS The Target ID and LUN of the next SCSI device on the SCSI
246 channel was returned in Target and Lun.
247 @retval EFI_INVALID_PARAMETER Target array is not all 0xF, and Target and Lun were
248 not returned on a previous call to GetNextTargetLun().
249 @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.
254 UfsPassThruGetNextTargetLun (
255 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
256 IN OUT UINT8
**Target
,
260 UFS_PASS_THRU_PRIVATE_DATA
*Private
;
265 Private
= UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
267 if ((Target
== NULL
) || (Lun
== NULL
)) {
268 return EFI_INVALID_PARAMETER
;
271 if (*Target
== NULL
) {
272 return EFI_INVALID_PARAMETER
;
276 SetMem (mUfsTargetId
, TARGET_MAX_BYTES
, 0xFF);
277 if (CompareMem (*Target
, mUfsTargetId
, TARGET_MAX_BYTES
) == 0) {
279 // If the array is all 0xFF's, return the first exposed Lun to caller.
281 SetMem (*Target
, TARGET_MAX_BYTES
, 0x00);
282 for (Index
= 0; Index
< UFS_MAX_LUNS
; Index
++) {
283 if ((Private
->Luns
.BitMask
& (BIT0
<< Index
)) != 0) {
284 UfsLun
= Private
->Luns
.Lun
[Index
];
289 if (Index
!= UFS_MAX_LUNS
) {
291 if ((UfsLun
& BIT7
) == BIT7
) {
292 ((UINT8
*)Lun
)[0] = UFS_WLUN_PREFIX
;
293 ((UINT8
*)Lun
)[1] = UfsLun
& ~BIT7
;
295 ((UINT8
*)Lun
)[1] = UfsLun
;
300 return EFI_NOT_FOUND
;
304 SetMem (mUfsTargetId
, TARGET_MAX_BYTES
, 0x00);
305 if (CompareMem (*Target
, mUfsTargetId
, TARGET_MAX_BYTES
) == 0) {
306 if (((UINT8
*)Lun
)[0] == UFS_WLUN_PREFIX
) {
307 UfsLun
= BIT7
| (((UINT8
*)Lun
)[1] & 0xFF);
308 } else if (((UINT8
*)Lun
)[0] == 0) {
309 UfsLun
= ((UINT8
*)Lun
)[1] & 0xFF;
311 return EFI_NOT_FOUND
;
314 for (Index
= 0; Index
< UFS_MAX_LUNS
; Index
++) {
315 if ((Private
->Luns
.BitMask
& (BIT0
<< Index
)) == 0) {
319 if (Private
->Luns
.Lun
[Index
] != UfsLun
) {
323 for (Next
= Index
+ 1; Next
< UFS_MAX_LUNS
; Next
++) {
324 if ((Private
->Luns
.BitMask
& (BIT0
<< Next
)) != 0) {
325 UfsLun
= Private
->Luns
.Lun
[Next
];
330 if (Next
== UFS_MAX_LUNS
) {
331 return EFI_NOT_FOUND
;
337 if (Index
!= UFS_MAX_LUNS
) {
339 if ((UfsLun
& BIT7
) == BIT7
) {
340 ((UINT8
*)Lun
)[0] = UFS_WLUN_PREFIX
;
341 ((UINT8
*)Lun
)[1] = UfsLun
& ~BIT7
;
343 ((UINT8
*)Lun
)[1] = UfsLun
;
348 return EFI_NOT_FOUND
;
352 return EFI_NOT_FOUND
;
356 Used to allocate and build a device path node for a SCSI device on a SCSI channel.
358 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
359 @param Target The Target is an array of size TARGET_MAX_BYTES and it specifies the
360 Target ID of the SCSI device for which a device path node is to be
361 allocated and built. Transport drivers may chose to utilize a subset of
362 this size to suit the representation of targets. For example, a Fibre
363 Channel driver may use only 8 bytes (WWN) in the array to represent a
365 @param Lun The LUN of the SCSI device for which a device path node is to be
367 @param DevicePath A pointer to a single device path node that describes the SCSI device
368 specified by Target and Lun. This function is responsible for
369 allocating the buffer DevicePath with the boot service
370 AllocatePool(). It is the caller's responsibility to free
371 DevicePath when the caller is finished with DevicePath.
373 @retval EFI_SUCCESS The device path node that describes the SCSI device specified by
374 Target and Lun was allocated and returned in
376 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
377 @retval EFI_NOT_FOUND The SCSI devices specified by Target and Lun does not exist
379 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
384 UfsPassThruBuildDevicePath (
385 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
388 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
391 UFS_PASS_THRU_PRIVATE_DATA
*Private
;
392 EFI_DEV_PATH
*DevicePathNode
;
396 Private
= UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
399 // Validate parameters passed in.
401 SetMem (mUfsTargetId
, TARGET_MAX_BYTES
, 0x00);
402 if (CompareMem (Target
, mUfsTargetId
, TARGET_MAX_BYTES
) != 0) {
403 return EFI_INVALID_PARAMETER
;
406 if ((UINT8
)Lun
== UFS_WLUN_PREFIX
) {
407 UfsLun
= BIT7
| (((UINT8
*)&Lun
)[1] & 0xFF);
408 } else if ((UINT8
)Lun
== 0) {
409 UfsLun
= ((UINT8
*)&Lun
)[1] & 0xFF;
411 return EFI_NOT_FOUND
;
414 for (Index
= 0; Index
< UFS_MAX_LUNS
; Index
++) {
415 if ((Private
->Luns
.BitMask
& (BIT0
<< Index
)) == 0) {
419 if (Private
->Luns
.Lun
[Index
] == UfsLun
) {
424 if (Index
== UFS_MAX_LUNS
) {
425 return EFI_NOT_FOUND
;
428 DevicePathNode
= AllocateCopyPool (sizeof (UFS_DEVICE_PATH
), &mUfsDevicePathTemplate
);
429 if (DevicePathNode
== NULL
) {
430 return EFI_OUT_OF_RESOURCES
;
433 DevicePathNode
->Ufs
.Pun
= 0;
434 DevicePathNode
->Ufs
.Lun
= UfsLun
;
436 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*)DevicePathNode
;
442 Used to translate a device path node to a Target ID and LUN.
444 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
445 @param DevicePath A pointer to a single device path node that describes the SCSI device
447 @param Target A pointer to the Target Array which represents the ID of a SCSI device
449 @param Lun A pointer to the LUN of a SCSI device on the SCSI channel.
451 @retval EFI_SUCCESS DevicePath was successfully translated to a Target ID and
452 LUN, and they were returned in Target and Lun.
453 @retval EFI_INVALID_PARAMETER DevicePath or Target or Lun is NULL.
454 @retval EFI_NOT_FOUND A valid translation from DevicePath to a Target ID and LUN
456 @retval EFI_UNSUPPORTED This driver does not support the device path node type in
462 UfsPassThruGetTargetLun (
463 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
464 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
469 UFS_PASS_THRU_PRIVATE_DATA
*Private
;
470 EFI_DEV_PATH
*DevicePathNode
;
475 Private
= UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
478 // Validate parameters passed in.
480 if ((DevicePath
== NULL
) || (Target
== NULL
) || (Lun
== NULL
)) {
481 return EFI_INVALID_PARAMETER
;
484 if (*Target
== NULL
) {
485 return EFI_INVALID_PARAMETER
;
489 // Check whether the DevicePath belongs to UFS_DEVICE_PATH
491 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) || (DevicePath
->SubType
!= MSG_UFS_DP
) ||
492 (DevicePathNodeLength (DevicePath
) != sizeof (UFS_DEVICE_PATH
)))
494 return EFI_UNSUPPORTED
;
497 DevicePathNode
= (EFI_DEV_PATH
*)DevicePath
;
499 Pun
= (UINT8
)DevicePathNode
->Ufs
.Pun
;
500 UfsLun
= (UINT8
)DevicePathNode
->Ufs
.Lun
;
503 return EFI_NOT_FOUND
;
506 for (Index
= 0; Index
< UFS_MAX_LUNS
; Index
++) {
507 if ((Private
->Luns
.BitMask
& (BIT0
<< Index
)) == 0) {
511 if (Private
->Luns
.Lun
[Index
] == UfsLun
) {
516 if (Index
== UFS_MAX_LUNS
) {
517 return EFI_NOT_FOUND
;
520 SetMem (*Target
, TARGET_MAX_BYTES
, 0x00);
522 if ((UfsLun
& BIT7
) == BIT7
) {
523 ((UINT8
*)Lun
)[0] = UFS_WLUN_PREFIX
;
524 ((UINT8
*)Lun
)[1] = UfsLun
& ~BIT7
;
526 ((UINT8
*)Lun
)[1] = UfsLun
;
533 Resets a SCSI channel. This operation resets all the SCSI devices connected to the SCSI channel.
535 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
537 @retval EFI_SUCCESS The SCSI channel was reset.
538 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI channel.
539 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI channel.
540 @retval EFI_UNSUPPORTED The SCSI channel does not support a channel reset operation.
545 UfsPassThruResetChannel (
546 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
550 // Return success directly then upper layer driver could think reset channel operation is done.
556 Resets a SCSI logical unit that is connected to a SCSI channel.
558 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
559 @param Target The Target is an array of size TARGET_MAX_BYTE and it represents the
560 target port ID of the SCSI device containing the SCSI logical unit to
561 reset. Transport drivers may chose to utilize a subset of this array to suit
562 the representation of their targets.
563 @param Lun The LUN of the SCSI device to reset.
565 @retval EFI_SUCCESS The SCSI device specified by Target and Lun was reset.
566 @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
567 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI device
568 specified by Target and Lun.
569 @retval EFI_UNSUPPORTED The SCSI channel does not support a target reset operation.
570 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI device
571 specified by Target and Lun.
576 UfsPassThruResetTargetLun (
577 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
583 // Return success directly then upper layer driver could think reset target LUN operation is done.
589 Used to retrieve the list of legal Target IDs for SCSI devices on a SCSI channel. These can either
590 be the list SCSI devices that are actually present on the SCSI channel, or the list of legal Target IDs
591 for the SCSI channel. Regardless, the caller of this function must probe the Target ID returned to
592 see if a SCSI device is actually present at that location on the SCSI channel.
594 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
595 @param Target (TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.
596 On output, a pointer to the Target ID (an array of
597 TARGET_MAX_BYTES) of the next SCSI device present on a SCSI
598 channel. An input value of 0xF(all bytes in the array are 0xF) in the
599 Target array retrieves the Target ID of the first SCSI device present on a
602 @retval EFI_SUCCESS The Target ID of the next SCSI device on the SCSI
603 channel was returned in Target.
604 @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
605 @retval EFI_TIMEOUT Target array is not all 0xF, and Target was not
606 returned on a previous call to GetNextTarget().
607 @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.
612 UfsPassThruGetNextTarget (
613 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
614 IN OUT UINT8
**Target
617 if ((Target
== NULL
) || (*Target
== NULL
)) {
618 return EFI_INVALID_PARAMETER
;
621 SetMem (mUfsTargetId
, TARGET_MAX_BYTES
, 0xFF);
622 if (CompareMem (*Target
, mUfsTargetId
, TARGET_MAX_BYTES
) == 0) {
623 SetMem (*Target
, TARGET_MAX_BYTES
, 0x00);
627 return EFI_NOT_FOUND
;
631 Tests to see if this driver supports a given controller. If a child device is provided,
632 it further tests to see if this driver supports creating a handle for the specified child device.
634 This function checks to see if the driver specified by This supports the device specified by
635 ControllerHandle. Drivers will typically use the device path attached to
636 ControllerHandle and/or the services from the bus I/O abstraction attached to
637 ControllerHandle to determine if the driver supports ControllerHandle. This function
638 may be called many times during platform initialization. In order to reduce boot times, the tests
639 performed by this function must be very small, and take as little time as possible to execute. This
640 function must not change the state of any hardware devices, and this function must be aware that the
641 device specified by ControllerHandle may already be managed by the same driver or a
642 different driver. This function must match its calls to AllocatePages() with FreePages(),
643 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
644 Since ControllerHandle may have been previously started by the same driver, if a protocol is
645 already in the opened state, then it must not be closed with CloseProtocol(). This is required
646 to guarantee the state of ControllerHandle is not modified by this function.
648 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
649 @param[in] ControllerHandle The handle of the controller to test. This handle
650 must support a protocol interface that supplies
651 an I/O abstraction to the driver.
652 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
653 parameter is ignored by device drivers, and is optional for bus
654 drivers. For bus drivers, if this parameter is not NULL, then
655 the bus driver must determine if the bus controller specified
656 by ControllerHandle and the child controller specified
657 by RemainingDevicePath are both supported by this
660 @retval EFI_SUCCESS The device specified by ControllerHandle and
661 RemainingDevicePath is supported by the driver specified by This.
662 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
663 RemainingDevicePath is already being managed by the driver
665 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
666 RemainingDevicePath is already being managed by a different
667 driver or an application that requires exclusive access.
668 Currently not implemented.
669 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
670 RemainingDevicePath is not supported by the driver specified by This.
674 UfsPassThruDriverBindingSupported (
675 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
676 IN EFI_HANDLE Controller
,
677 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
681 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
682 EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*UfsHostController
;
685 // Ufs Pass Thru driver is a device driver, and should ingore the
686 // "RemainingDevicePath" according to UEFI spec
688 Status
= gBS
->OpenProtocol (
690 &gEfiDevicePathProtocolGuid
,
691 (VOID
*)&ParentDevicePath
,
692 This
->DriverBindingHandle
,
694 EFI_OPEN_PROTOCOL_BY_DRIVER
696 if (EFI_ERROR (Status
)) {
698 // EFI_ALREADY_STARTED is also an error
704 // Close the protocol because we don't use it here
708 &gEfiDevicePathProtocolGuid
,
709 This
->DriverBindingHandle
,
713 Status
= gBS
->OpenProtocol (
715 &gEdkiiUfsHostControllerProtocolGuid
,
716 (VOID
**)&UfsHostController
,
717 This
->DriverBindingHandle
,
719 EFI_OPEN_PROTOCOL_BY_DRIVER
722 if (EFI_ERROR (Status
)) {
724 // EFI_ALREADY_STARTED is also an error
730 // Close the I/O Abstraction(s) used to perform the supported test
734 &gEdkiiUfsHostControllerProtocolGuid
,
735 This
->DriverBindingHandle
,
743 Finishes device initialization by setting fDeviceInit flag and waiting untill device responds by
746 @param[in] Private Pointer to the UFS_PASS_THRU_PRIVATE_DATA.
748 @retval EFI_SUCCESS The operation succeeds.
749 @retval Others The operation fails.
753 UfsFinishDeviceInitialization (
754 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
758 UINT8 DeviceInitStatus
;
761 DeviceInitStatus
= 0xFF;
764 // The host enables the device initialization completion by setting fDeviceInit flag.
766 Status
= UfsSetFlag (Private
, UfsFlagDevInit
);
767 if (EFI_ERROR (Status
)) {
772 // There are cards that can take upto 600ms to clear fDeviceInit flag.
774 Timeout
= UFS_INIT_COMPLETION_TIMEOUT
;
776 Status
= UfsReadFlag (Private
, UfsFlagDevInit
, &DeviceInitStatus
);
777 if (EFI_ERROR (Status
)) {
781 MicroSecondDelay (1);
783 } while (DeviceInitStatus
!= 0 && Timeout
!= 0);
786 DEBUG ((DEBUG_ERROR
, "UfsFinishDeviceInitialization DeviceInitStatus=%x EFI_TIMEOUT \n", DeviceInitStatus
));
789 DEBUG ((DEBUG_INFO
, "UfsFinishDeviceInitialization Timeout left=%x EFI_SUCCESS \n", Timeout
));
795 Starts a device controller or a bus controller.
797 The Start() function is designed to be invoked from the EFI boot service ConnectController().
798 As a result, much of the error checking on the parameters to Start() has been moved into this
799 common boot service. It is legal to call Start() from other locations,
800 but the following calling restrictions must be followed or the system behavior will not be deterministic.
801 1. ControllerHandle must be a valid EFI_HANDLE.
802 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
803 EFI_DEVICE_PATH_PROTOCOL.
804 3. Prior to calling Start(), the Supported() function for the driver specified by This must
805 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
807 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
808 @param[in] ControllerHandle The handle of the controller to start. This handle
809 must support a protocol interface that supplies
810 an I/O abstraction to the driver.
811 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
812 parameter is ignored by device drivers, and is optional for bus
813 drivers. For a bus driver, if this parameter is NULL, then handles
814 for all the children of Controller are created by this driver.
815 If this parameter is not NULL and the first Device Path Node is
816 not the End of Device Path Node, then only the handle for the
817 child device specified by the first Device Path Node of
818 RemainingDevicePath is created by this driver.
819 If the first Device Path Node of RemainingDevicePath is
820 the End of Device Path Node, no child handle is created by this
823 @retval EFI_SUCCESS The device was started.
824 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
825 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
826 @retval Others The driver failded to start the device.
831 UfsPassThruDriverBindingStart (
832 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
833 IN EFI_HANDLE Controller
,
834 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
838 EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*UfsHc
;
839 UFS_PASS_THRU_PRIVATE_DATA
*Private
;
842 UFS_UNIT_DESC UnitDescriptor
;
843 UFS_DEV_DESC DeviceDescriptor
;
844 UINT32 UnitDescriptorSize
;
845 UINT32 DeviceDescriptorSize
;
847 Status
= EFI_SUCCESS
;
852 DEBUG ((DEBUG_INFO
, "==UfsPassThru Start== Controller = %x\n", Controller
));
854 Status
= gBS
->OpenProtocol (
856 &gEdkiiUfsHostControllerProtocolGuid
,
858 This
->DriverBindingHandle
,
860 EFI_OPEN_PROTOCOL_BY_DRIVER
863 if (EFI_ERROR (Status
)) {
864 DEBUG ((DEBUG_ERROR
, "Open Ufs Host Controller Protocol Error, Status = %r\n", Status
));
869 // Get the UFS Host Controller MMIO Bar Base Address.
871 Status
= UfsHc
->GetUfsHcMmioBar (UfsHc
, &UfsHcBase
);
872 if (EFI_ERROR (Status
)) {
873 DEBUG ((DEBUG_ERROR
, "Get Ufs Host Controller Mmio Bar Error, Status = %r\n", Status
));
878 // Initialize Ufs Pass Thru private data for managed UFS Host Controller.
880 Private
= AllocateCopyPool (sizeof (UFS_PASS_THRU_PRIVATE_DATA
), &gUfsPassThruTemplate
);
881 if (Private
== NULL
) {
882 DEBUG ((DEBUG_ERROR
, "Unable to allocate Ufs Pass Thru private data\n"));
883 Status
= EFI_OUT_OF_RESOURCES
;
887 Private
->ExtScsiPassThru
.Mode
= &Private
->ExtScsiPassThruMode
;
888 Private
->UfsHostController
= UfsHc
;
889 Private
->UfsHcBase
= UfsHcBase
;
890 Private
->Handle
= Controller
;
891 Private
->UfsHcDriverInterface
.UfsHcProtocol
= UfsHc
;
892 Private
->UfsHcDriverInterface
.UfsExecUicCommand
= UfsHcDriverInterfaceExecUicCommand
;
893 InitializeListHead (&Private
->Queue
);
896 // This has to be done before initializing UfsHcInfo or calling the UfsControllerInit
898 if (mUfsHcPlatform
== NULL
) {
899 Status
= gBS
->LocateProtocol (&gEdkiiUfsHcPlatformProtocolGuid
, NULL
, (VOID
**)&mUfsHcPlatform
);
900 if (EFI_ERROR (Status
)) {
901 DEBUG ((DEBUG_INFO
, "No UfsHcPlatformProtocol present\n"));
905 Status
= GetUfsHcInfo (Private
);
906 if (EFI_ERROR (Status
)) {
907 DEBUG ((DEBUG_ERROR
, "Failed to initialize UfsHcInfo\n"));
912 // Initialize UFS Host Controller H/W.
914 Status
= UfsControllerInit (Private
);
915 if (EFI_ERROR (Status
)) {
916 DEBUG ((DEBUG_ERROR
, "Ufs Host Controller Initialization Error, Status = %r\n", Status
));
921 // UFS 2.0 spec Section 13.1.3.3:
922 // At the end of the UFS Interconnect Layer initialization on both host and device side,
923 // the host shall send a NOP OUT UPIU to verify that the device UTP Layer is ready.
925 Status
= UfsExecNopCmds (Private
);
926 if (EFI_ERROR (Status
)) {
927 DEBUG ((DEBUG_ERROR
, "Ufs Sending NOP IN command Error, Status = %r\n", Status
));
931 Status
= UfsFinishDeviceInitialization (Private
);
932 if (EFI_ERROR (Status
)) {
933 DEBUG ((DEBUG_ERROR
, "Device failed to finish initialization, Status = %r\n", Status
));
938 // Check if 8 common luns are active and set corresponding bit mask.
940 UnitDescriptorSize
= sizeof (UFS_UNIT_DESC
);
941 for (Index
= 0; Index
< 8; Index
++) {
942 Status
= UfsRwDeviceDesc (Private
, TRUE
, UfsUnitDesc
, (UINT8
)Index
, 0, &UnitDescriptor
, &UnitDescriptorSize
);
943 if (EFI_ERROR (Status
)) {
944 DEBUG ((DEBUG_ERROR
, "Failed to read unit descriptor, index = %X, status = %r\n", Index
, Status
));
948 if (UnitDescriptor
.LunEn
== 0x1) {
949 DEBUG ((DEBUG_INFO
, "UFS LUN %X is enabled\n", Index
));
950 Private
->Luns
.BitMask
|= (BIT0
<< Index
);
955 // Check if RPMB WLUN is supported and set corresponding bit mask.
957 DeviceDescriptorSize
= sizeof (UFS_DEV_DESC
);
958 Status
= UfsRwDeviceDesc (Private
, TRUE
, UfsDeviceDesc
, 0, 0, &DeviceDescriptor
, &DeviceDescriptorSize
);
959 if (EFI_ERROR (Status
)) {
960 DEBUG ((DEBUG_ERROR
, "Failed to read device descriptor, status = %r\n", Status
));
962 if (DeviceDescriptor
.SecurityLun
== 0x1) {
963 DEBUG ((DEBUG_INFO
, "UFS WLUN RPMB is supported\n"));
964 Private
->Luns
.BitMask
|= BIT11
;
969 // Start the asynchronous interrupt monitor
971 Status
= gBS
->CreateEvent (
972 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
974 ProcessAsyncTaskList
,
978 if (EFI_ERROR (Status
)) {
979 DEBUG ((DEBUG_ERROR
, "Ufs Create Async Tasks Event Error, Status = %r\n", Status
));
983 Status
= gBS
->SetTimer (
988 if (EFI_ERROR (Status
)) {
989 DEBUG ((DEBUG_ERROR
, "Ufs Set Periodic Timer Error, Status = %r\n", Status
));
993 Status
= gBS
->InstallMultipleProtocolInterfaces (
995 &gEfiExtScsiPassThruProtocolGuid
,
996 &(Private
->ExtScsiPassThru
),
997 &gEfiUfsDeviceConfigProtocolGuid
,
998 &(Private
->UfsDevConfig
),
1001 ASSERT_EFI_ERROR (Status
);
1006 if (Private
!= NULL
) {
1007 if (Private
->TmrlMapping
!= NULL
) {
1008 UfsHc
->Unmap (UfsHc
, Private
->TmrlMapping
);
1011 if (Private
->UtpTmrlBase
!= NULL
) {
1012 UfsHc
->FreeBuffer (UfsHc
, EFI_SIZE_TO_PAGES (Private
->Nutmrs
* sizeof (UTP_TMRD
)), Private
->UtpTmrlBase
);
1015 if (Private
->TrlMapping
!= NULL
) {
1016 UfsHc
->Unmap (UfsHc
, Private
->TrlMapping
);
1019 if (Private
->UtpTrlBase
!= NULL
) {
1020 UfsHc
->FreeBuffer (UfsHc
, EFI_SIZE_TO_PAGES (Private
->Nutrs
* sizeof (UTP_TMRD
)), Private
->UtpTrlBase
);
1023 if (Private
->TimerEvent
!= NULL
) {
1024 gBS
->CloseEvent (Private
->TimerEvent
);
1030 if (UfsHc
!= NULL
) {
1031 gBS
->CloseProtocol (
1033 &gEdkiiUfsHostControllerProtocolGuid
,
1034 This
->DriverBindingHandle
,
1043 Stops a device controller or a bus controller.
1045 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
1046 As a result, much of the error checking on the parameters to Stop() has been moved
1047 into this common boot service. It is legal to call Stop() from other locations,
1048 but the following calling restrictions must be followed or the system behavior will not be deterministic.
1049 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
1050 same driver's Start() function.
1051 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
1052 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
1053 Start() function, and the Start() function must have called OpenProtocol() on
1054 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
1056 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
1057 @param[in] ControllerHandle A handle to the device being stopped. The handle must
1058 support a bus specific I/O protocol for the driver
1059 to use to stop the device.
1060 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
1061 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
1062 if NumberOfChildren is 0.
1064 @retval EFI_SUCCESS The device was stopped.
1065 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
1070 UfsPassThruDriverBindingStop (
1071 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1072 IN EFI_HANDLE Controller
,
1073 IN UINTN NumberOfChildren
,
1074 IN EFI_HANDLE
*ChildHandleBuffer
1078 UFS_PASS_THRU_PRIVATE_DATA
*Private
;
1079 EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*ExtScsiPassThru
;
1080 EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*UfsHc
;
1081 UFS_PASS_THRU_TRANS_REQ
*TransReq
;
1083 LIST_ENTRY
*NextEntry
;
1085 DEBUG ((DEBUG_INFO
, "==UfsPassThru Stop== Controller Controller = %x\n", Controller
));
1087 Status
= gBS
->OpenProtocol (
1089 &gEfiExtScsiPassThruProtocolGuid
,
1090 (VOID
**)&ExtScsiPassThru
,
1091 This
->DriverBindingHandle
,
1093 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1096 if (EFI_ERROR (Status
)) {
1097 return EFI_DEVICE_ERROR
;
1100 Private
= UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS (ExtScsiPassThru
);
1101 UfsHc
= Private
->UfsHostController
;
1104 // Cleanup the resources of I/O requests in the async I/O queue
1106 if (!IsListEmpty (&Private
->Queue
)) {
1107 BASE_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Private
->Queue
) {
1108 TransReq
= UFS_PASS_THRU_TRANS_REQ_FROM_THIS (Entry
);
1111 // TODO: Should find/add a proper host adapter return status for this
1114 TransReq
->Packet
->HostAdapterStatus
=
1115 EFI_EXT_SCSI_STATUS_HOST_ADAPTER_PHASE_ERROR
;
1117 SignalCallerEvent (Private
, TransReq
);
1121 Status
= gBS
->UninstallMultipleProtocolInterfaces (
1123 &gEfiExtScsiPassThruProtocolGuid
,
1124 &(Private
->ExtScsiPassThru
),
1125 &gEfiUfsDeviceConfigProtocolGuid
,
1126 &(Private
->UfsDevConfig
),
1130 if (EFI_ERROR (Status
)) {
1131 return EFI_DEVICE_ERROR
;
1135 // Stop Ufs Host Controller
1137 Status
= UfsControllerStop (Private
);
1138 ASSERT_EFI_ERROR (Status
);
1140 if (Private
->TmrlMapping
!= NULL
) {
1141 UfsHc
->Unmap (UfsHc
, Private
->TmrlMapping
);
1144 if (Private
->UtpTmrlBase
!= NULL
) {
1145 UfsHc
->FreeBuffer (UfsHc
, EFI_SIZE_TO_PAGES (Private
->Nutmrs
* sizeof (UTP_TMRD
)), Private
->UtpTmrlBase
);
1148 if (Private
->TrlMapping
!= NULL
) {
1149 UfsHc
->Unmap (UfsHc
, Private
->TrlMapping
);
1152 if (Private
->UtpTrlBase
!= NULL
) {
1153 UfsHc
->FreeBuffer (UfsHc
, EFI_SIZE_TO_PAGES (Private
->Nutrs
* sizeof (UTP_TMRD
)), Private
->UtpTrlBase
);
1156 if (Private
->TimerEvent
!= NULL
) {
1157 gBS
->CloseEvent (Private
->TimerEvent
);
1163 // Close protocols opened by UfsPassThru controller driver
1165 gBS
->CloseProtocol (
1167 &gEdkiiUfsHostControllerProtocolGuid
,
1168 This
->DriverBindingHandle
,
1176 The user Entry Point for module UfsPassThru. The user code starts with this function.
1178 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1179 @param[in] SystemTable A pointer to the EFI System Table.
1181 @retval EFI_SUCCESS The entry point is executed successfully.
1182 @retval other Some error occurs when executing this entry point.
1187 InitializeUfsPassThru (
1188 IN EFI_HANDLE ImageHandle
,
1189 IN EFI_SYSTEM_TABLE
*SystemTable
1195 // Install driver model protocol(s).
1197 Status
= EfiLibInstallDriverBindingComponentName2 (
1200 &gUfsPassThruDriverBinding
,
1202 &gUfsPassThruComponentName
,
1203 &gUfsPassThruComponentName2
1205 ASSERT_EFI_ERROR (Status
);