3 Copyright (c) 2014 - 2017, Intel Corporation. All rights reserved.<BR>
4 This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 #include "UfsPassThru.h"
17 // Template for Ufs Pass Thru private data.
19 UFS_PASS_THRU_PRIVATE_DATA gUfsPassThruTemplate
= {
20 UFS_PASS_THRU_SIG
, // Signature
22 { // ExtScsiPassThruMode
24 EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_PHYSICAL
| EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL
| EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_NONBLOCKIO
,
30 UfsPassThruGetNextTargetLun
,
31 UfsPassThruBuildDevicePath
,
32 UfsPassThruGetTargetLun
,
33 UfsPassThruResetChannel
,
34 UfsPassThruResetTargetLun
,
35 UfsPassThruGetNextTarget
37 0, // UfsHostController
49 UFS_LUN_0
, // Ufs Common Lun 0
50 UFS_LUN_1
, // Ufs Common Lun 1
51 UFS_LUN_2
, // Ufs Common Lun 2
52 UFS_LUN_3
, // Ufs Common Lun 3
53 UFS_LUN_4
, // Ufs Common Lun 4
54 UFS_LUN_5
, // Ufs Common Lun 5
55 UFS_LUN_6
, // Ufs Common Lun 6
56 UFS_LUN_7
, // Ufs Common Lun 7
57 UFS_WLUN_REPORT_LUNS
, // Ufs Reports Luns Well Known Lun
58 UFS_WLUN_UFS_DEV
, // Ufs Device Well Known Lun
59 UFS_WLUN_BOOT
, // Ufs Boot Well Known Lun
60 UFS_WLUN_RPMB
// RPMB Well Known Lun
62 0x0000, // By default don't expose any Luns.
72 EFI_DRIVER_BINDING_PROTOCOL gUfsPassThruDriverBinding
= {
73 UfsPassThruDriverBindingSupported
,
74 UfsPassThruDriverBindingStart
,
75 UfsPassThruDriverBindingStop
,
81 UFS_DEVICE_PATH mUfsDevicePathTemplate
= {
83 MESSAGING_DEVICE_PATH
,
86 (UINT8
) (sizeof (UFS_DEVICE_PATH
)),
87 (UINT8
) ((sizeof (UFS_DEVICE_PATH
)) >> 8)
94 UINT8 mUfsTargetId
[TARGET_MAX_BYTES
];
97 Sends a SCSI Request Packet to a SCSI device that is attached to the SCSI channel. This function
98 supports both blocking I/O and nonblocking I/O. The blocking I/O functionality is required, and the
99 nonblocking I/O functionality is optional.
101 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
102 @param Target The Target is an array of size TARGET_MAX_BYTES and it represents
103 the id of the SCSI device to send the SCSI Request Packet. Each
104 transport driver may choose to utilize a subset of this size to suit the needs
105 of transport target representation. For example, a Fibre Channel driver
106 may use only 8 bytes (WWN) to represent an FC target.
107 @param Lun The LUN of the SCSI device to send the SCSI Request Packet.
108 @param Packet A pointer to the SCSI Request Packet to send to the SCSI device
109 specified by Target and Lun.
110 @param Event If nonblocking I/O is not supported then Event is ignored, and blocking
111 I/O is performed. If Event is NULL, then blocking I/O is performed. If
112 Event is not NULL and non blocking I/O is supported, then
113 nonblocking I/O is performed, and Event will be signaled when the
114 SCSI Request Packet completes.
116 @retval EFI_SUCCESS The SCSI Request Packet was sent by the host. For bi-directional
117 commands, InTransferLength bytes were transferred from
118 InDataBuffer. For write and bi-directional commands,
119 OutTransferLength bytes were transferred by
121 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was not executed. The number of bytes that
122 could be transferred is returned in InTransferLength. For write
123 and bi-directional commands, OutTransferLength bytes were
124 transferred by OutDataBuffer.
125 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because there are too many
126 SCSI Request Packets already queued. The caller may retry again later.
127 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SCSI Request
129 @retval EFI_INVALID_PARAMETER Target, Lun, or the contents of ScsiRequestPacket are invalid.
130 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet is not supported
131 by the host adapter. This includes the case of Bi-directional SCSI
132 commands not supported by the implementation. The SCSI Request
133 Packet was not sent, so no additional status information is available.
134 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
139 UfsPassThruPassThru (
140 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
143 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
144 IN EFI_EVENT Event OPTIONAL
148 UFS_PASS_THRU_PRIVATE_DATA
*Private
;
152 Private
= UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
154 if ((Packet
== NULL
) || (Packet
->Cdb
== NULL
)) {
155 return EFI_INVALID_PARAMETER
;
159 // Don't support variable length CDB
161 if ((Packet
->CdbLength
!= 6) && (Packet
->CdbLength
!= 10) &&
162 (Packet
->CdbLength
!= 12) && (Packet
->CdbLength
!= 16)) {
163 return EFI_INVALID_PARAMETER
;
166 if ((Packet
->SenseDataLength
!= 0) && (Packet
->SenseData
== NULL
)) {
167 return EFI_INVALID_PARAMETER
;
170 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->InDataBuffer
, This
->Mode
->IoAlign
)) {
171 return EFI_INVALID_PARAMETER
;
174 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->OutDataBuffer
, This
->Mode
->IoAlign
)) {
175 return EFI_INVALID_PARAMETER
;
178 if ((This
->Mode
->IoAlign
> 1) && !IS_ALIGNED(Packet
->SenseData
, This
->Mode
->IoAlign
)) {
179 return EFI_INVALID_PARAMETER
;
183 // For UFS 2.0 compatible device, 0 is always used to represent the location of the UFS device.
185 SetMem (mUfsTargetId
, TARGET_MAX_BYTES
, 0x00);
186 if ((Target
== NULL
) || (CompareMem(Target
, mUfsTargetId
, TARGET_MAX_BYTES
) != 0)) {
187 return EFI_INVALID_PARAMETER
;
191 // UFS 2.0 spec Section 10.6.7 - Translation of 8-bit UFS LUN to 64-bit SCSI LUN Address
192 // 0xC1 in the first 8 bits of the 64-bit address indicates a well known LUN address in the SAM SCSI format.
193 // The second 8 bits of the 64-bit address saves the corresponding 8-bit UFS LUN.
195 if ((UINT8
)Lun
== UFS_WLUN_PREFIX
) {
196 UfsLun
= BIT7
| (((UINT8
*)&Lun
)[1] & 0xFF);
197 } else if ((UINT8
)Lun
== 0) {
198 UfsLun
= ((UINT8
*)&Lun
)[1] & 0xFF;
200 return EFI_INVALID_PARAMETER
;
203 for (Index
= 0; Index
< UFS_MAX_LUNS
; Index
++) {
204 if ((Private
->Luns
.BitMask
& (BIT0
<< Index
)) == 0) {
208 if (Private
->Luns
.Lun
[Index
] == UfsLun
) {
213 if (Index
== UFS_MAX_LUNS
) {
214 return EFI_INVALID_PARAMETER
;
217 Status
= UfsExecScsiCmds (Private
, UfsLun
, Packet
, Event
);
223 Used to retrieve the list of legal Target IDs and LUNs for SCSI devices on a SCSI channel. These
224 can either be the list SCSI devices that are actually present on the SCSI channel, or the list of legal
225 Target Ids and LUNs for the SCSI channel. Regardless, the caller of this function must probe the
226 Target ID and LUN returned to see if a SCSI device is actually present at that location on the SCSI
229 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
230 @param Target On input, a pointer to the Target ID (an array of size
231 TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.
232 On output, a pointer to the Target ID (an array of
233 TARGET_MAX_BYTES) of the next SCSI device present on a SCSI
234 channel. An input value of 0xF(all bytes in the array are 0xF) in the
235 Target array retrieves the Target ID of the first SCSI device present on a
237 @param Lun On input, a pointer to the LUN of a SCSI device present on the SCSI
238 channel. On output, a pointer to the LUN of the next SCSI device present
241 @retval EFI_SUCCESS The Target ID and LUN of the next SCSI device on the SCSI
242 channel was returned in Target and Lun.
243 @retval EFI_INVALID_PARAMETER Target array is not all 0xF, and Target and Lun were
244 not returned on a previous call to GetNextTargetLun().
245 @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.
250 UfsPassThruGetNextTargetLun (
251 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
252 IN OUT UINT8
**Target
,
256 UFS_PASS_THRU_PRIVATE_DATA
*Private
;
261 Private
= UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
263 if (Target
== NULL
|| Lun
== NULL
) {
264 return EFI_INVALID_PARAMETER
;
267 if (*Target
== NULL
) {
268 return EFI_INVALID_PARAMETER
;
272 SetMem (mUfsTargetId
, TARGET_MAX_BYTES
, 0xFF);
273 if (CompareMem (*Target
, mUfsTargetId
, TARGET_MAX_BYTES
) == 0) {
275 // If the array is all 0xFF's, return the first exposed Lun to caller.
277 SetMem (*Target
, TARGET_MAX_BYTES
, 0x00);
278 for (Index
= 0; Index
< UFS_MAX_LUNS
; Index
++) {
279 if ((Private
->Luns
.BitMask
& (BIT0
<< Index
)) != 0) {
280 UfsLun
= Private
->Luns
.Lun
[Index
];
284 if (Index
!= UFS_MAX_LUNS
) {
286 if ((UfsLun
& BIT7
) == BIT7
) {
287 ((UINT8
*)Lun
)[0] = UFS_WLUN_PREFIX
;
288 ((UINT8
*)Lun
)[1] = UfsLun
& ~BIT7
;
290 ((UINT8
*)Lun
)[1] = UfsLun
;
294 return EFI_NOT_FOUND
;
298 SetMem (mUfsTargetId
, TARGET_MAX_BYTES
, 0x00);
299 if (CompareMem (*Target
, mUfsTargetId
, TARGET_MAX_BYTES
) == 0) {
300 if (((UINT8
*)Lun
)[0] == UFS_WLUN_PREFIX
) {
301 UfsLun
= BIT7
| (((UINT8
*)Lun
)[1] & 0xFF);
302 } else if (((UINT8
*)Lun
)[0] == 0) {
303 UfsLun
= ((UINT8
*)Lun
)[1] & 0xFF;
305 return EFI_NOT_FOUND
;
308 for (Index
= 0; Index
< UFS_MAX_LUNS
; Index
++) {
309 if ((Private
->Luns
.BitMask
& (BIT0
<< Index
)) == 0) {
313 if (Private
->Luns
.Lun
[Index
] != UfsLun
) {
317 for (Next
= Index
+ 1; Next
< UFS_MAX_LUNS
; Next
++) {
318 if ((Private
->Luns
.BitMask
& (BIT0
<< Next
)) != 0) {
319 UfsLun
= Private
->Luns
.Lun
[Next
];
324 if (Next
== UFS_MAX_LUNS
) {
325 return EFI_NOT_FOUND
;
331 if (Index
!= UFS_MAX_LUNS
) {
333 if ((UfsLun
& BIT7
) == BIT7
) {
334 ((UINT8
*)Lun
)[0] = UFS_WLUN_PREFIX
;
335 ((UINT8
*)Lun
)[1] = UfsLun
& ~BIT7
;
337 ((UINT8
*)Lun
)[1] = UfsLun
;
341 return EFI_NOT_FOUND
;
345 return EFI_NOT_FOUND
;
349 Used to allocate and build a device path node for a SCSI device on a SCSI channel.
351 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
352 @param Target The Target is an array of size TARGET_MAX_BYTES and it specifies the
353 Target ID of the SCSI device for which a device path node is to be
354 allocated and built. Transport drivers may chose to utilize a subset of
355 this size to suit the representation of targets. For example, a Fibre
356 Channel driver may use only 8 bytes (WWN) in the array to represent a
358 @param Lun The LUN of the SCSI device for which a device path node is to be
360 @param DevicePath A pointer to a single device path node that describes the SCSI device
361 specified by Target and Lun. This function is responsible for
362 allocating the buffer DevicePath with the boot service
363 AllocatePool(). It is the caller's responsibility to free
364 DevicePath when the caller is finished with DevicePath.
366 @retval EFI_SUCCESS The device path node that describes the SCSI device specified by
367 Target and Lun was allocated and returned in
369 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
370 @retval EFI_NOT_FOUND The SCSI devices specified by Target and Lun does not exist
372 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
377 UfsPassThruBuildDevicePath (
378 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
381 IN OUT EFI_DEVICE_PATH_PROTOCOL
**DevicePath
384 UFS_PASS_THRU_PRIVATE_DATA
*Private
;
385 EFI_DEV_PATH
*DevicePathNode
;
389 Private
= UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
392 // Validate parameters passed in.
394 SetMem (mUfsTargetId
, TARGET_MAX_BYTES
, 0x00);
395 if (CompareMem (Target
, mUfsTargetId
, TARGET_MAX_BYTES
) != 0) {
396 return EFI_INVALID_PARAMETER
;
399 if ((UINT8
)Lun
== UFS_WLUN_PREFIX
) {
400 UfsLun
= BIT7
| (((UINT8
*)&Lun
)[1] & 0xFF);
401 } else if ((UINT8
)Lun
== 0) {
402 UfsLun
= ((UINT8
*)&Lun
)[1] & 0xFF;
404 return EFI_NOT_FOUND
;
407 for (Index
= 0; Index
< UFS_MAX_LUNS
; Index
++) {
408 if ((Private
->Luns
.BitMask
& (BIT0
<< Index
)) == 0) {
412 if (Private
->Luns
.Lun
[Index
] == UfsLun
) {
417 if (Index
== UFS_MAX_LUNS
) {
418 return EFI_NOT_FOUND
;
421 DevicePathNode
= AllocateCopyPool (sizeof (UFS_DEVICE_PATH
), &mUfsDevicePathTemplate
);
422 if (DevicePathNode
== NULL
) {
423 return EFI_OUT_OF_RESOURCES
;
426 DevicePathNode
->Ufs
.Pun
= 0;
427 DevicePathNode
->Ufs
.Lun
= UfsLun
;
429 *DevicePath
= (EFI_DEVICE_PATH_PROTOCOL
*) DevicePathNode
;
435 Used to translate a device path node to a Target ID and LUN.
437 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
438 @param DevicePath A pointer to a single device path node that describes the SCSI device
440 @param Target A pointer to the Target Array which represents the ID of a SCSI device
442 @param Lun A pointer to the LUN of a SCSI device on the SCSI channel.
444 @retval EFI_SUCCESS DevicePath was successfully translated to a Target ID and
445 LUN, and they were returned in Target and Lun.
446 @retval EFI_INVALID_PARAMETER DevicePath or Target or Lun is NULL.
447 @retval EFI_NOT_FOUND A valid translation from DevicePath to a Target ID and LUN
449 @retval EFI_UNSUPPORTED This driver does not support the device path node type in
455 UfsPassThruGetTargetLun (
456 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
457 IN EFI_DEVICE_PATH_PROTOCOL
*DevicePath
,
462 UFS_PASS_THRU_PRIVATE_DATA
*Private
;
463 EFI_DEV_PATH
*DevicePathNode
;
468 Private
= UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS (This
);
471 // Validate parameters passed in.
473 if (DevicePath
== NULL
|| Target
== NULL
|| Lun
== NULL
) {
474 return EFI_INVALID_PARAMETER
;
477 if (*Target
== NULL
) {
478 return EFI_INVALID_PARAMETER
;
482 // Check whether the DevicePath belongs to SCSI_DEVICE_PATH
484 if ((DevicePath
->Type
!= MESSAGING_DEVICE_PATH
) || (DevicePath
->SubType
!= MSG_UFS_DP
) ||
485 (DevicePathNodeLength(DevicePath
) != sizeof(SCSI_DEVICE_PATH
))) {
486 return EFI_UNSUPPORTED
;
489 DevicePathNode
= (EFI_DEV_PATH
*) DevicePath
;
491 Pun
= (UINT8
) DevicePathNode
->Ufs
.Pun
;
492 UfsLun
= (UINT8
) DevicePathNode
->Ufs
.Lun
;
495 return EFI_NOT_FOUND
;
498 for (Index
= 0; Index
< UFS_MAX_LUNS
; Index
++) {
499 if ((Private
->Luns
.BitMask
& (BIT0
<< Index
)) == 0) {
503 if (Private
->Luns
.Lun
[Index
] == UfsLun
) {
508 if (Index
== UFS_MAX_LUNS
) {
509 return EFI_NOT_FOUND
;
512 SetMem (*Target
, TARGET_MAX_BYTES
, 0x00);
514 if ((UfsLun
& BIT7
) == BIT7
) {
515 ((UINT8
*)Lun
)[0] = UFS_WLUN_PREFIX
;
516 ((UINT8
*)Lun
)[1] = UfsLun
& ~BIT7
;
518 ((UINT8
*)Lun
)[1] = UfsLun
;
524 Resets a SCSI channel. This operation resets all the SCSI devices connected to the SCSI channel.
526 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
528 @retval EFI_SUCCESS The SCSI channel was reset.
529 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI channel.
530 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI channel.
531 @retval EFI_UNSUPPORTED The SCSI channel does not support a channel reset operation.
536 UfsPassThruResetChannel (
537 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
541 // Return success directly then upper layer driver could think reset channel operation is done.
547 Resets a SCSI logical unit that is connected to a SCSI channel.
549 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
550 @param Target The Target is an array of size TARGET_MAX_BYTE and it represents the
551 target port ID of the SCSI device containing the SCSI logical unit to
552 reset. Transport drivers may chose to utilize a subset of this array to suit
553 the representation of their targets.
554 @param Lun The LUN of the SCSI device to reset.
556 @retval EFI_SUCCESS The SCSI device specified by Target and Lun was reset.
557 @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
558 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI device
559 specified by Target and Lun.
560 @retval EFI_UNSUPPORTED The SCSI channel does not support a target reset operation.
561 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI device
562 specified by Target and Lun.
567 UfsPassThruResetTargetLun (
568 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
574 // Return success directly then upper layer driver could think reset target LUN operation is done.
580 Used to retrieve the list of legal Target IDs for SCSI devices on a SCSI channel. These can either
581 be the list SCSI devices that are actually present on the SCSI channel, or the list of legal Target IDs
582 for the SCSI channel. Regardless, the caller of this function must probe the Target ID returned to
583 see if a SCSI device is actually present at that location on the SCSI channel.
585 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
586 @param Target (TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.
587 On output, a pointer to the Target ID (an array of
588 TARGET_MAX_BYTES) of the next SCSI device present on a SCSI
589 channel. An input value of 0xF(all bytes in the array are 0xF) in the
590 Target array retrieves the Target ID of the first SCSI device present on a
593 @retval EFI_SUCCESS The Target ID of the next SCSI device on the SCSI
594 channel was returned in Target.
595 @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
596 @retval EFI_TIMEOUT Target array is not all 0xF, and Target was not
597 returned on a previous call to GetNextTarget().
598 @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.
603 UfsPassThruGetNextTarget (
604 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*This
,
605 IN OUT UINT8
**Target
608 if (Target
== NULL
|| *Target
== NULL
) {
609 return EFI_INVALID_PARAMETER
;
612 SetMem (mUfsTargetId
, TARGET_MAX_BYTES
, 0xFF);
613 if (CompareMem(*Target
, mUfsTargetId
, TARGET_MAX_BYTES
) == 0) {
614 SetMem (*Target
, TARGET_MAX_BYTES
, 0x00);
618 return EFI_NOT_FOUND
;
622 Tests to see if this driver supports a given controller. If a child device is provided,
623 it further tests to see if this driver supports creating a handle for the specified child device.
625 This function checks to see if the driver specified by This supports the device specified by
626 ControllerHandle. Drivers will typically use the device path attached to
627 ControllerHandle and/or the services from the bus I/O abstraction attached to
628 ControllerHandle to determine if the driver supports ControllerHandle. This function
629 may be called many times during platform initialization. In order to reduce boot times, the tests
630 performed by this function must be very small, and take as little time as possible to execute. This
631 function must not change the state of any hardware devices, and this function must be aware that the
632 device specified by ControllerHandle may already be managed by the same driver or a
633 different driver. This function must match its calls to AllocatePages() with FreePages(),
634 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
635 Since ControllerHandle may have been previously started by the same driver, if a protocol is
636 already in the opened state, then it must not be closed with CloseProtocol(). This is required
637 to guarantee the state of ControllerHandle is not modified by this function.
639 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
640 @param[in] ControllerHandle The handle of the controller to test. This handle
641 must support a protocol interface that supplies
642 an I/O abstraction to the driver.
643 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
644 parameter is ignored by device drivers, and is optional for bus
645 drivers. For bus drivers, if this parameter is not NULL, then
646 the bus driver must determine if the bus controller specified
647 by ControllerHandle and the child controller specified
648 by RemainingDevicePath are both supported by this
651 @retval EFI_SUCCESS The device specified by ControllerHandle and
652 RemainingDevicePath is supported by the driver specified by This.
653 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
654 RemainingDevicePath is already being managed by the driver
656 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
657 RemainingDevicePath is already being managed by a different
658 driver or an application that requires exclusive access.
659 Currently not implemented.
660 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
661 RemainingDevicePath is not supported by the driver specified by This.
665 UfsPassThruDriverBindingSupported (
666 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
667 IN EFI_HANDLE Controller
,
668 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
672 EFI_DEVICE_PATH_PROTOCOL
*ParentDevicePath
;
673 EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*UfsHostController
;
676 // Ufs Pass Thru driver is a device driver, and should ingore the
677 // "RemainingDevicePath" according to UEFI spec
679 Status
= gBS
->OpenProtocol (
681 &gEfiDevicePathProtocolGuid
,
682 (VOID
*) &ParentDevicePath
,
683 This
->DriverBindingHandle
,
685 EFI_OPEN_PROTOCOL_BY_DRIVER
687 if (EFI_ERROR (Status
)) {
689 // EFI_ALREADY_STARTED is also an error
694 // Close the protocol because we don't use it here
698 &gEfiDevicePathProtocolGuid
,
699 This
->DriverBindingHandle
,
703 Status
= gBS
->OpenProtocol (
705 &gEdkiiUfsHostControllerProtocolGuid
,
706 (VOID
**) &UfsHostController
,
707 This
->DriverBindingHandle
,
709 EFI_OPEN_PROTOCOL_BY_DRIVER
712 if (EFI_ERROR (Status
)) {
714 // EFI_ALREADY_STARTED is also an error
720 // Close the I/O Abstraction(s) used to perform the supported test
724 &gEdkiiUfsHostControllerProtocolGuid
,
725 This
->DriverBindingHandle
,
733 Finishes device initialization by setting fDeviceInit flag and waiting untill device responds by
736 @param[in] Private Pointer to the UFS_PASS_THRU_PRIVATE_DATA.
738 @retval EFI_SUCCESS The operation succeeds.
739 @retval Others The operation fails.
743 UfsFinishDeviceInitialization (
744 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
748 UINT8 DeviceInitStatus
;
751 DeviceInitStatus
= 0xFF;
754 // The host enables the device initialization completion by setting fDeviceInit flag.
756 Status
= UfsSetFlag (Private
, UfsFlagDevInit
);
757 if (EFI_ERROR (Status
)) {
763 Status
= UfsReadFlag (Private
, UfsFlagDevInit
, &DeviceInitStatus
);
764 if (EFI_ERROR (Status
)) {
767 MicroSecondDelay (1);
769 } while (DeviceInitStatus
!= 0 && Timeout
!= 0);
775 Starts a device controller or a bus controller.
777 The Start() function is designed to be invoked from the EFI boot service ConnectController().
778 As a result, much of the error checking on the parameters to Start() has been moved into this
779 common boot service. It is legal to call Start() from other locations,
780 but the following calling restrictions must be followed or the system behavior will not be deterministic.
781 1. ControllerHandle must be a valid EFI_HANDLE.
782 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
783 EFI_DEVICE_PATH_PROTOCOL.
784 3. Prior to calling Start(), the Supported() function for the driver specified by This must
785 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
787 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
788 @param[in] ControllerHandle The handle of the controller to start. This handle
789 must support a protocol interface that supplies
790 an I/O abstraction to the driver.
791 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
792 parameter is ignored by device drivers, and is optional for bus
793 drivers. For a bus driver, if this parameter is NULL, then handles
794 for all the children of Controller are created by this driver.
795 If this parameter is not NULL and the first Device Path Node is
796 not the End of Device Path Node, then only the handle for the
797 child device specified by the first Device Path Node of
798 RemainingDevicePath is created by this driver.
799 If the first Device Path Node of RemainingDevicePath is
800 the End of Device Path Node, no child handle is created by this
803 @retval EFI_SUCCESS The device was started.
804 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
805 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
806 @retval Others The driver failded to start the device.
811 UfsPassThruDriverBindingStart (
812 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
813 IN EFI_HANDLE Controller
,
814 IN EFI_DEVICE_PATH_PROTOCOL
*RemainingDevicePath
818 EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*UfsHc
;
819 UFS_PASS_THRU_PRIVATE_DATA
*Private
;
822 UFS_UNIT_DESC UnitDescriptor
;
824 Status
= EFI_SUCCESS
;
829 DEBUG ((DEBUG_INFO
, "==UfsPassThru Start== Controller = %x\n", Controller
));
831 Status
= gBS
->OpenProtocol (
833 &gEdkiiUfsHostControllerProtocolGuid
,
835 This
->DriverBindingHandle
,
837 EFI_OPEN_PROTOCOL_BY_DRIVER
840 if (EFI_ERROR (Status
)) {
841 DEBUG ((DEBUG_ERROR
, "Open Ufs Host Controller Protocol Error, Status = %r\n", Status
));
846 // Get the UFS Host Controller MMIO Bar Base Address.
848 Status
= UfsHc
->GetUfsHcMmioBar (UfsHc
, &UfsHcBase
);
849 if (EFI_ERROR (Status
)) {
850 DEBUG ((DEBUG_ERROR
, "Get Ufs Host Controller Mmio Bar Error, Status = %r\n", Status
));
855 // Initialize Ufs Pass Thru private data for managed UFS Host Controller.
857 Private
= AllocateCopyPool (sizeof (UFS_PASS_THRU_PRIVATE_DATA
), &gUfsPassThruTemplate
);
858 if (Private
== NULL
) {
859 DEBUG ((DEBUG_ERROR
, "Unable to allocate Ufs Pass Thru private data\n"));
860 Status
= EFI_OUT_OF_RESOURCES
;
864 Private
->ExtScsiPassThru
.Mode
= &Private
->ExtScsiPassThruMode
;
865 Private
->UfsHostController
= UfsHc
;
866 Private
->UfsHcBase
= UfsHcBase
;
867 InitializeListHead (&Private
->Queue
);
870 // Initialize UFS Host Controller H/W.
872 Status
= UfsControllerInit (Private
);
873 if (EFI_ERROR (Status
)) {
874 DEBUG ((DEBUG_ERROR
, "Ufs Host Controller Initialization Error, Status = %r\n", Status
));
879 // UFS 2.0 spec Section 13.1.3.3:
880 // At the end of the UFS Interconnect Layer initialization on both host and device side,
881 // the host shall send a NOP OUT UPIU to verify that the device UTP Layer is ready.
883 Status
= UfsExecNopCmds (Private
);
884 if (EFI_ERROR (Status
)) {
885 DEBUG ((DEBUG_ERROR
, "Ufs Sending NOP IN command Error, Status = %r\n", Status
));
889 Status
= UfsFinishDeviceInitialization (Private
);
890 if (EFI_ERROR (Status
)) {
891 DEBUG ((DEBUG_ERROR
, "Device failed to finish initialization, Status = %r\n", Status
));
896 // Check if 8 common luns are active and set corresponding bit mask.
897 // TODO: Parse device descriptor to decide if exposing RPMB LUN to upper layer for authentication access.
899 for (Index
= 0; Index
< 8; Index
++) {
900 Status
= UfsRwDeviceDesc (Private
, TRUE
, UfsUnitDesc
, (UINT8
) Index
, 0, &UnitDescriptor
, sizeof (UFS_UNIT_DESC
));
901 if (EFI_ERROR (Status
)) {
902 DEBUG ((DEBUG_ERROR
, "Failed to read unit descriptor, index = %X, status = %r\n", Index
, Status
));
905 if (UnitDescriptor
.LunEn
== 0x1) {
906 DEBUG ((DEBUG_INFO
, "UFS LUN %X is enabled\n", Index
));
907 Private
->Luns
.BitMask
|= (BIT0
<< Index
);
912 // Start the asynchronous interrupt monitor
914 Status
= gBS
->CreateEvent (
915 EVT_TIMER
| EVT_NOTIFY_SIGNAL
,
917 ProcessAsyncTaskList
,
921 if (EFI_ERROR (Status
)) {
922 DEBUG ((DEBUG_ERROR
, "Ufs Create Async Tasks Event Error, Status = %r\n", Status
));
926 Status
= gBS
->SetTimer (
931 if (EFI_ERROR (Status
)) {
932 DEBUG ((DEBUG_ERROR
, "Ufs Set Periodic Timer Error, Status = %r\n", Status
));
936 Status
= gBS
->InstallProtocolInterface (
938 &gEfiExtScsiPassThruProtocolGuid
,
939 EFI_NATIVE_INTERFACE
,
940 &(Private
->ExtScsiPassThru
)
942 ASSERT_EFI_ERROR (Status
);
947 if (Private
!= NULL
) {
948 if (Private
->TmrlMapping
!= NULL
) {
949 UfsHc
->Unmap (UfsHc
, Private
->TmrlMapping
);
951 if (Private
->UtpTmrlBase
!= NULL
) {
952 UfsHc
->FreeBuffer (UfsHc
, EFI_SIZE_TO_PAGES (Private
->Nutmrs
* sizeof (UTP_TMRD
)), Private
->UtpTmrlBase
);
955 if (Private
->TrlMapping
!= NULL
) {
956 UfsHc
->Unmap (UfsHc
, Private
->TrlMapping
);
958 if (Private
->UtpTrlBase
!= NULL
) {
959 UfsHc
->FreeBuffer (UfsHc
, EFI_SIZE_TO_PAGES (Private
->Nutrs
* sizeof (UTP_TMRD
)), Private
->UtpTrlBase
);
962 if (Private
->TimerEvent
!= NULL
) {
963 gBS
->CloseEvent (Private
->TimerEvent
);
972 &gEdkiiUfsHostControllerProtocolGuid
,
973 This
->DriverBindingHandle
,
982 Stops a device controller or a bus controller.
984 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
985 As a result, much of the error checking on the parameters to Stop() has been moved
986 into this common boot service. It is legal to call Stop() from other locations,
987 but the following calling restrictions must be followed or the system behavior will not be deterministic.
988 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
989 same driver's Start() function.
990 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
991 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
992 Start() function, and the Start() function must have called OpenProtocol() on
993 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
995 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
996 @param[in] ControllerHandle A handle to the device being stopped. The handle must
997 support a bus specific I/O protocol for the driver
998 to use to stop the device.
999 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
1000 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
1001 if NumberOfChildren is 0.
1003 @retval EFI_SUCCESS The device was stopped.
1004 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
1009 UfsPassThruDriverBindingStop (
1010 IN EFI_DRIVER_BINDING_PROTOCOL
*This
,
1011 IN EFI_HANDLE Controller
,
1012 IN UINTN NumberOfChildren
,
1013 IN EFI_HANDLE
*ChildHandleBuffer
1017 UFS_PASS_THRU_PRIVATE_DATA
*Private
;
1018 EFI_EXT_SCSI_PASS_THRU_PROTOCOL
*ExtScsiPassThru
;
1019 EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*UfsHc
;
1020 UFS_PASS_THRU_TRANS_REQ
*TransReq
;
1022 LIST_ENTRY
*NextEntry
;
1024 DEBUG ((DEBUG_INFO
, "==UfsPassThru Stop== Controller Controller = %x\n", Controller
));
1026 Status
= gBS
->OpenProtocol (
1028 &gEfiExtScsiPassThruProtocolGuid
,
1029 (VOID
**) &ExtScsiPassThru
,
1030 This
->DriverBindingHandle
,
1032 EFI_OPEN_PROTOCOL_GET_PROTOCOL
1035 if (EFI_ERROR (Status
)) {
1036 return EFI_DEVICE_ERROR
;
1039 Private
= UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS (ExtScsiPassThru
);
1040 UfsHc
= Private
->UfsHostController
;
1043 // Cleanup the resources of I/O requests in the async I/O queue
1045 if (!IsListEmpty(&Private
->Queue
)) {
1046 EFI_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Private
->Queue
) {
1047 TransReq
= UFS_PASS_THRU_TRANS_REQ_FROM_THIS (Entry
);
1050 // TODO: Should find/add a proper host adapter return status for this
1053 TransReq
->Packet
->HostAdapterStatus
=
1054 EFI_EXT_SCSI_STATUS_HOST_ADAPTER_PHASE_ERROR
;
1056 SignalCallerEvent (Private
, TransReq
);
1060 Status
= gBS
->UninstallProtocolInterface (
1062 &gEfiExtScsiPassThruProtocolGuid
,
1063 &(Private
->ExtScsiPassThru
)
1066 if (EFI_ERROR (Status
)) {
1067 return EFI_DEVICE_ERROR
;
1071 // Stop Ufs Host Controller
1073 Status
= UfsControllerStop (Private
);
1074 ASSERT_EFI_ERROR (Status
);
1076 if (Private
->TmrlMapping
!= NULL
) {
1077 UfsHc
->Unmap (UfsHc
, Private
->TmrlMapping
);
1079 if (Private
->UtpTmrlBase
!= NULL
) {
1080 UfsHc
->FreeBuffer (UfsHc
, EFI_SIZE_TO_PAGES (Private
->Nutmrs
* sizeof (UTP_TMRD
)), Private
->UtpTmrlBase
);
1083 if (Private
->TrlMapping
!= NULL
) {
1084 UfsHc
->Unmap (UfsHc
, Private
->TrlMapping
);
1086 if (Private
->UtpTrlBase
!= NULL
) {
1087 UfsHc
->FreeBuffer (UfsHc
, EFI_SIZE_TO_PAGES (Private
->Nutrs
* sizeof (UTP_TMRD
)), Private
->UtpTrlBase
);
1090 if (Private
->TimerEvent
!= NULL
) {
1091 gBS
->CloseEvent (Private
->TimerEvent
);
1097 // Close protocols opened by UfsPassThru controller driver
1099 gBS
->CloseProtocol (
1101 &gEdkiiUfsHostControllerProtocolGuid
,
1102 This
->DriverBindingHandle
,
1111 The user Entry Point for module UfsPassThru. The user code starts with this function.
1113 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1114 @param[in] SystemTable A pointer to the EFI System Table.
1116 @retval EFI_SUCCESS The entry point is executed successfully.
1117 @retval other Some error occurs when executing this entry point.
1122 InitializeUfsPassThru (
1123 IN EFI_HANDLE ImageHandle
,
1124 IN EFI_SYSTEM_TABLE
*SystemTable
1130 // Install driver model protocol(s).
1132 Status
= EfiLibInstallDriverBindingComponentName2 (
1135 &gUfsPassThruDriverBinding
,
1137 &gUfsPassThruComponentName
,
1138 &gUfsPassThruComponentName2
1140 ASSERT_EFI_ERROR (Status
);