--- /dev/null
+/** @file\r
+\r
+ Copyright (c) 2014, Intel Corporation. All rights reserved.<BR>\r
+ This program and the accompanying materials\r
+ are licensed and made available under the terms and conditions of the BSD License\r
+ which accompanies this distribution. The full text of the license may be found at\r
+ http://opensource.org/licenses/bsd-license.php\r
+\r
+ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+#include "UfsPassThru.h"\r
+\r
+//\r
+// Template for Ufs Pass Thru private data.\r
+//\r
+UFS_PASS_THRU_PRIVATE_DATA gUfsPassThruTemplate = {\r
+ UFS_PASS_THRU_SIG, // Signature\r
+ NULL, // Handle \r
+ { // ExtScsiPassThruMode\r
+ 0xFFFFFFFF,\r
+ //\r
+ // Note that the driver doesn't support ExtScsiPassThru non blocking I/O.\r
+ //\r
+ EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_PHYSICAL | EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL,\r
+ sizeof (UINTN)\r
+ },\r
+ { // ExtScsiPassThru\r
+ NULL,\r
+ UfsPassThruPassThru,\r
+ UfsPassThruGetNextTargetLun,\r
+ UfsPassThruBuildDevicePath,\r
+ UfsPassThruGetTargetLun,\r
+ UfsPassThruResetChannel,\r
+ UfsPassThruResetTargetLun,\r
+ UfsPassThruGetNextTarget\r
+ },\r
+ 0, // UfsHostController\r
+ 0, // UfsHcBase\r
+ 0, // Capabilities\r
+ 0, // TaskTag\r
+ 0, // UtpTrlBase\r
+ 0, // Nutrs\r
+ 0, // TrlMapping\r
+ 0, // UtpTmrlBase\r
+ 0, // Nutmrs\r
+ 0, // TmrlMapping\r
+ { // Luns\r
+ {\r
+ UFS_LUN_0, // Ufs Common Lun 0\r
+ UFS_LUN_1, // Ufs Common Lun 1\r
+ UFS_LUN_2, // Ufs Common Lun 2\r
+ UFS_LUN_3, // Ufs Common Lun 3\r
+ UFS_LUN_4, // Ufs Common Lun 4\r
+ UFS_LUN_5, // Ufs Common Lun 5\r
+ UFS_LUN_6, // Ufs Common Lun 6\r
+ UFS_LUN_7, // Ufs Common Lun 7\r
+ UFS_WLUN_REPORT_LUNS, // Ufs Reports Luns Well Known Lun\r
+ UFS_WLUN_UFS_DEV, // Ufs Device Well Known Lun\r
+ UFS_WLUN_BOOT, // Ufs Boot Well Known Lun\r
+ UFS_WLUN_RPMB // RPMB Well Known Lun\r
+ },\r
+ 0x0000, // By default don't expose any Luns.\r
+ 0x0\r
+ }\r
+};\r
+\r
+EFI_DRIVER_BINDING_PROTOCOL gUfsPassThruDriverBinding = {\r
+ UfsPassThruDriverBindingSupported,\r
+ UfsPassThruDriverBindingStart,\r
+ UfsPassThruDriverBindingStop,\r
+ 0x10,\r
+ NULL,\r
+ NULL\r
+};\r
+\r
+UFS_DEVICE_PATH mUfsDevicePathTemplate = {\r
+ {\r
+ MESSAGING_DEVICE_PATH,\r
+ MSG_UFS_DP,\r
+ {\r
+ (UINT8) (sizeof (UFS_DEVICE_PATH)),\r
+ (UINT8) ((sizeof (UFS_DEVICE_PATH)) >> 8)\r
+ }\r
+ },\r
+ 0,\r
+ 0\r
+};\r
+\r
+UINT8 mUfsTargetId[TARGET_MAX_BYTES];\r
+\r
+/**\r
+ Sends a SCSI Request Packet to a SCSI device that is attached to the SCSI channel. This function\r
+ supports both blocking I/O and nonblocking I/O. The blocking I/O functionality is required, and the\r
+ nonblocking I/O functionality is optional.\r
+\r
+ @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.\r
+ @param Target The Target is an array of size TARGET_MAX_BYTES and it represents\r
+ the id of the SCSI device to send the SCSI Request Packet. Each\r
+ transport driver may choose to utilize a subset of this size to suit the needs\r
+ of transport target representation. For example, a Fibre Channel driver\r
+ may use only 8 bytes (WWN) to represent an FC target.\r
+ @param Lun The LUN of the SCSI device to send the SCSI Request Packet.\r
+ @param Packet A pointer to the SCSI Request Packet to send to the SCSI device\r
+ specified by Target and Lun.\r
+ @param Event If nonblocking I/O is not supported then Event is ignored, and blocking\r
+ I/O is performed. If Event is NULL, then blocking I/O is performed. If\r
+ Event is not NULL and non blocking I/O is supported, then\r
+ nonblocking I/O is performed, and Event will be signaled when the\r
+ SCSI Request Packet completes.\r
+\r
+ @retval EFI_SUCCESS The SCSI Request Packet was sent by the host. For bi-directional\r
+ commands, InTransferLength bytes were transferred from\r
+ InDataBuffer. For write and bi-directional commands,\r
+ OutTransferLength bytes were transferred by\r
+ OutDataBuffer.\r
+ @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was not executed. The number of bytes that\r
+ could be transferred is returned in InTransferLength. For write\r
+ and bi-directional commands, OutTransferLength bytes were\r
+ transferred by OutDataBuffer.\r
+ @retval EFI_NOT_READY The SCSI Request Packet could not be sent because there are too many\r
+ SCSI Request Packets already queued. The caller may retry again later.\r
+ @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SCSI Request\r
+ Packet.\r
+ @retval EFI_INVALID_PARAMETER Target, Lun, or the contents of ScsiRequestPacket are invalid.\r
+ @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet is not supported\r
+ by the host adapter. This includes the case of Bi-directional SCSI\r
+ commands not supported by the implementation. The SCSI Request\r
+ Packet was not sent, so no additional status information is available.\r
+ @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+UfsPassThruPassThru (\r
+ IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,\r
+ IN UINT8 *Target,\r
+ IN UINT64 Lun,\r
+ IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet,\r
+ IN EFI_EVENT Event OPTIONAL\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UFS_PASS_THRU_PRIVATE_DATA *Private;\r
+ UINT8 UfsLun;\r
+ UINT16 Index;\r
+\r
+ Private = UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);\r
+\r
+ if ((Packet == NULL) || (Packet->Cdb == NULL)) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // Don't support variable length CDB\r
+ //\r
+ if ((Packet->CdbLength != 6) && (Packet->CdbLength != 10) &&\r
+ (Packet->CdbLength != 12) && (Packet->CdbLength != 16)) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if ((Packet->SenseDataLength != 0) && (Packet->SenseData == NULL)) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->InDataBuffer, This->Mode->IoAlign)) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->OutDataBuffer, This->Mode->IoAlign)) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->SenseData, This->Mode->IoAlign)) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // For UFS 2.0 compatible device, 0 is always used to represent the location of the UFS device.\r
+ //\r
+ SetMem (mUfsTargetId, TARGET_MAX_BYTES, 0x00);\r
+ if ((Target == NULL) || (CompareMem(Target, mUfsTargetId, TARGET_MAX_BYTES) != 0)) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // UFS 2.0 spec Section 10.6.7 - Translation of 8-bit UFS LUN to 64-bit SCSI LUN Address\r
+ // 0xC1 in the first 8 bits of the 64-bit address indicates a well known LUN address in the SAM SCSI format.\r
+ // The second 8 bits of the 64-bit address saves the corresponding 8-bit UFS LUN.\r
+ //\r
+ if ((UINT8)Lun == UFS_WLUN_PREFIX) {\r
+ UfsLun = BIT7 | (((UINT8*)&Lun)[1] & 0xFF);\r
+ } else if ((UINT8)Lun == 0) {\r
+ UfsLun = ((UINT8*)&Lun)[1] & 0xFF;\r
+ } else {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ for (Index = 0; Index < UFS_MAX_LUNS; Index++) {\r
+ if ((Private->Luns.BitMask & (BIT0 << Index)) == 0) {\r
+ continue;\r
+ }\r
+ \r
+ if (Private->Luns.Lun[Index] == UfsLun) {\r
+ break;\r
+ }\r
+ }\r
+\r
+ if (Index == UFS_MAX_LUNS) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ Status = UfsExecScsiCmds (Private, UfsLun, Packet);\r
+\r
+ return Status;\r
+}\r
+\r
+/**\r
+ Used to retrieve the list of legal Target IDs and LUNs for SCSI devices on a SCSI channel. These\r
+ can either be the list SCSI devices that are actually present on the SCSI channel, or the list of legal\r
+ Target Ids and LUNs for the SCSI channel. Regardless, the caller of this function must probe the\r
+ Target ID and LUN returned to see if a SCSI device is actually present at that location on the SCSI\r
+ channel.\r
+\r
+ @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.\r
+ @param Target On input, a pointer to the Target ID (an array of size\r
+ TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.\r
+ On output, a pointer to the Target ID (an array of\r
+ TARGET_MAX_BYTES) of the next SCSI device present on a SCSI\r
+ channel. An input value of 0xF(all bytes in the array are 0xF) in the\r
+ Target array retrieves the Target ID of the first SCSI device present on a\r
+ SCSI channel.\r
+ @param Lun On input, a pointer to the LUN of a SCSI device present on the SCSI\r
+ channel. On output, a pointer to the LUN of the next SCSI device present\r
+ on a SCSI channel.\r
+\r
+ @retval EFI_SUCCESS The Target ID and LUN of the next SCSI device on the SCSI\r
+ channel was returned in Target and Lun.\r
+ @retval EFI_INVALID_PARAMETER Target array is not all 0xF, and Target and Lun were\r
+ not returned on a previous call to GetNextTargetLun().\r
+ @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+UfsPassThruGetNextTargetLun (\r
+ IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,\r
+ IN OUT UINT8 **Target,\r
+ IN OUT UINT64 *Lun\r
+ )\r
+{\r
+ UFS_PASS_THRU_PRIVATE_DATA *Private;\r
+ UINT8 UfsLun;\r
+ UINT16 Index;\r
+ UINT16 Next;\r
+\r
+ Private = UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);\r
+\r
+ if (Target == NULL || Lun == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if (*Target == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ UfsLun = 0;\r
+ SetMem (mUfsTargetId, TARGET_MAX_BYTES, 0xFF);\r
+ if (CompareMem (*Target, mUfsTargetId, TARGET_MAX_BYTES) == 0) {\r
+ //\r
+ // If the array is all 0xFF's, return the first exposed Lun to caller.\r
+ //\r
+ SetMem (*Target, TARGET_MAX_BYTES, 0x00);\r
+ for (Index = 0; Index < UFS_MAX_LUNS; Index++) {\r
+ if ((Private->Luns.BitMask & (BIT0 << Index)) != 0) {\r
+ UfsLun = Private->Luns.Lun[Index];\r
+ break;\r
+ }\r
+ }\r
+ if (Index != UFS_MAX_LUNS) {\r
+ *Lun = 0;\r
+ if ((UfsLun & BIT7) == BIT7) {\r
+ ((UINT8*)Lun)[0] = UFS_WLUN_PREFIX;\r
+ ((UINT8*)Lun)[1] = UfsLun & ~BIT7;\r
+ } else {\r
+ ((UINT8*)Lun)[1] = UfsLun;\r
+ }\r
+ return EFI_SUCCESS;\r
+ } else {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+ }\r
+\r
+ SetMem (mUfsTargetId, TARGET_MAX_BYTES, 0x00);\r
+ if (CompareMem (*Target, mUfsTargetId, TARGET_MAX_BYTES) == 0) {\r
+ if (((UINT8*)Lun)[0] == UFS_WLUN_PREFIX) {\r
+ UfsLun = BIT7 | (((UINT8*)Lun)[1] & 0xFF);\r
+ } else if (((UINT8*)Lun)[0] == 0) {\r
+ UfsLun = ((UINT8*)Lun)[1] & 0xFF;\r
+ } else {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+\r
+ for (Index = 0; Index < UFS_MAX_LUNS; Index++) {\r
+ if ((Private->Luns.BitMask & (BIT0 << Index)) == 0) {\r
+ continue;\r
+ }\r
+\r
+ if (Private->Luns.Lun[Index] != UfsLun) {\r
+ continue;\r
+ }\r
+\r
+ for (Next = Index + 1; Next < UFS_MAX_LUNS; Next++) {\r
+ if ((Private->Luns.BitMask & (BIT0 << Next)) != 0) {\r
+ UfsLun = Private->Luns.Lun[Next];\r
+ break;\r
+ }\r
+ }\r
+\r
+ if (Next == UFS_MAX_LUNS) {\r
+ return EFI_NOT_FOUND;\r
+ } else {\r
+ break;\r
+ }\r
+ }\r
+\r
+ if (Index != UFS_MAX_LUNS) {\r
+ *Lun = 0;\r
+ if ((UfsLun & BIT7) == BIT7) {\r
+ ((UINT8*)Lun)[0] = UFS_WLUN_PREFIX;\r
+ ((UINT8*)Lun)[1] = UfsLun & ~BIT7;\r
+ } else {\r
+ ((UINT8*)Lun)[1] = UfsLun;\r
+ }\r
+ return EFI_SUCCESS;\r
+ } else {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+ }\r
+\r
+ return EFI_NOT_FOUND;\r
+}\r
+\r
+/**\r
+ Used to allocate and build a device path node for a SCSI device on a SCSI channel.\r
+\r
+ @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.\r
+ @param Target The Target is an array of size TARGET_MAX_BYTES and it specifies the\r
+ Target ID of the SCSI device for which a device path node is to be\r
+ allocated and built. Transport drivers may chose to utilize a subset of\r
+ this size to suit the representation of targets. For example, a Fibre\r
+ Channel driver may use only 8 bytes (WWN) in the array to represent a\r
+ FC target.\r
+ @param Lun The LUN of the SCSI device for which a device path node is to be\r
+ allocated and built.\r
+ @param DevicePath A pointer to a single device path node that describes the SCSI device\r
+ specified by Target and Lun. This function is responsible for\r
+ allocating the buffer DevicePath with the boot service\r
+ AllocatePool(). It is the caller's responsibility to free\r
+ DevicePath when the caller is finished with DevicePath.\r
+\r
+ @retval EFI_SUCCESS The device path node that describes the SCSI device specified by\r
+ Target and Lun was allocated and returned in\r
+ DevicePath.\r
+ @retval EFI_INVALID_PARAMETER DevicePath is NULL.\r
+ @retval EFI_NOT_FOUND The SCSI devices specified by Target and Lun does not exist\r
+ on the SCSI channel.\r
+ @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+UfsPassThruBuildDevicePath (\r
+ IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,\r
+ IN UINT8 *Target,\r
+ IN UINT64 Lun,\r
+ IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath\r
+ )\r
+{\r
+ UFS_PASS_THRU_PRIVATE_DATA *Private;\r
+ EFI_DEV_PATH *DevicePathNode;\r
+ UINT8 UfsLun;\r
+ UINT16 Index;\r
+\r
+ Private = UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);\r
+\r
+ //\r
+ // Validate parameters passed in.\r
+ //\r
+ SetMem (mUfsTargetId, TARGET_MAX_BYTES, 0x00);\r
+ if (CompareMem (Target, mUfsTargetId, TARGET_MAX_BYTES) != 0) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if ((UINT8)Lun == UFS_WLUN_PREFIX) {\r
+ UfsLun = BIT7 | (((UINT8*)&Lun)[1] & 0xFF);\r
+ } else if ((UINT8)Lun == 0) {\r
+ UfsLun = ((UINT8*)&Lun)[1] & 0xFF;\r
+ } else {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+\r
+ for (Index = 0; Index < UFS_MAX_LUNS; Index++) {\r
+ if ((Private->Luns.BitMask & (BIT0 << Index)) == 0) {\r
+ continue;\r
+ }\r
+ \r
+ if (Private->Luns.Lun[Index] == UfsLun) {\r
+ break;\r
+ }\r
+ }\r
+\r
+ if (Index == UFS_MAX_LUNS) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+\r
+ DevicePathNode = AllocateCopyPool (sizeof (UFS_DEVICE_PATH), &mUfsDevicePathTemplate);\r
+ if (DevicePathNode == NULL) {\r
+ return EFI_OUT_OF_RESOURCES;\r
+ }\r
+\r
+ DevicePathNode->Ufs.Pun = 0;\r
+ DevicePathNode->Ufs.Lun = UfsLun;\r
+\r
+ *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) DevicePathNode;\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Used to translate a device path node to a Target ID and LUN.\r
+\r
+ @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.\r
+ @param DevicePath A pointer to a single device path node that describes the SCSI device\r
+ on the SCSI channel.\r
+ @param Target A pointer to the Target Array which represents the ID of a SCSI device\r
+ on the SCSI channel.\r
+ @param Lun A pointer to the LUN of a SCSI device on the SCSI channel.\r
+\r
+ @retval EFI_SUCCESS DevicePath was successfully translated to a Target ID and\r
+ LUN, and they were returned in Target and Lun.\r
+ @retval EFI_INVALID_PARAMETER DevicePath or Target or Lun is NULL.\r
+ @retval EFI_NOT_FOUND A valid translation from DevicePath to a Target ID and LUN\r
+ does not exist.\r
+ @retval EFI_UNSUPPORTED This driver does not support the device path node type in\r
+ DevicePath.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+UfsPassThruGetTargetLun (\r
+ IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,\r
+ IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
+ OUT UINT8 **Target,\r
+ OUT UINT64 *Lun\r
+ )\r
+{\r
+ UFS_PASS_THRU_PRIVATE_DATA *Private;\r
+ EFI_DEV_PATH *DevicePathNode;\r
+ UINT8 Pun;\r
+ UINT8 UfsLun;\r
+ UINT16 Index;\r
+\r
+ Private = UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);\r
+\r
+ //\r
+ // Validate parameters passed in.\r
+ //\r
+ if (DevicePath == NULL || Target == NULL || Lun == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ if (*Target == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ //\r
+ // Check whether the DevicePath belongs to SCSI_DEVICE_PATH\r
+ //\r
+ if ((DevicePath->Type != MESSAGING_DEVICE_PATH) || (DevicePath->SubType != MSG_UFS_DP) ||\r
+ (DevicePathNodeLength(DevicePath) != sizeof(SCSI_DEVICE_PATH))) {\r
+ return EFI_UNSUPPORTED;\r
+ }\r
+\r
+ DevicePathNode = (EFI_DEV_PATH *) DevicePath;\r
+\r
+ Pun = (UINT8) DevicePathNode->Ufs.Pun;\r
+ UfsLun = (UINT8) DevicePathNode->Ufs.Lun;\r
+\r
+ if (Pun != 0) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+\r
+ for (Index = 0; Index < UFS_MAX_LUNS; Index++) {\r
+ if ((Private->Luns.BitMask & (BIT0 << Index)) == 0) {\r
+ continue;\r
+ }\r
+ \r
+ if (Private->Luns.Lun[Index] == UfsLun) {\r
+ break;\r
+ }\r
+ }\r
+\r
+ if (Index == UFS_MAX_LUNS) {\r
+ return EFI_NOT_FOUND;\r
+ }\r
+\r
+ SetMem (*Target, TARGET_MAX_BYTES, 0x00);\r
+ *Lun = 0;\r
+ if ((UfsLun & BIT7) == BIT7) {\r
+ ((UINT8*)Lun)[0] = UFS_WLUN_PREFIX;\r
+ ((UINT8*)Lun)[1] = UfsLun & ~BIT7;\r
+ } else {\r
+ ((UINT8*)Lun)[1] = UfsLun;\r
+ }\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Resets a SCSI channel. This operation resets all the SCSI devices connected to the SCSI channel.\r
+\r
+ @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.\r
+\r
+ @retval EFI_SUCCESS The SCSI channel was reset.\r
+ @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI channel.\r
+ @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI channel.\r
+ @retval EFI_UNSUPPORTED The SCSI channel does not support a channel reset operation.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+UfsPassThruResetChannel (\r
+ IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This\r
+ )\r
+{\r
+ //\r
+ // Return success directly then upper layer driver could think reset channel operation is done.\r
+ //\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Resets a SCSI logical unit that is connected to a SCSI channel.\r
+\r
+ @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.\r
+ @param Target The Target is an array of size TARGET_MAX_BYTE and it represents the\r
+ target port ID of the SCSI device containing the SCSI logical unit to\r
+ reset. Transport drivers may chose to utilize a subset of this array to suit\r
+ the representation of their targets.\r
+ @param Lun The LUN of the SCSI device to reset.\r
+\r
+ @retval EFI_SUCCESS The SCSI device specified by Target and Lun was reset.\r
+ @retval EFI_INVALID_PARAMETER Target or Lun is NULL.\r
+ @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI device\r
+ specified by Target and Lun.\r
+ @retval EFI_UNSUPPORTED The SCSI channel does not support a target reset operation.\r
+ @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI device\r
+ specified by Target and Lun.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+UfsPassThruResetTargetLun (\r
+ IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,\r
+ IN UINT8 *Target,\r
+ IN UINT64 Lun\r
+ )\r
+{\r
+ //\r
+ // Return success directly then upper layer driver could think reset target LUN operation is done.\r
+ //\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Used to retrieve the list of legal Target IDs for SCSI devices on a SCSI channel. These can either\r
+ be the list SCSI devices that are actually present on the SCSI channel, or the list of legal Target IDs\r
+ for the SCSI channel. Regardless, the caller of this function must probe the Target ID returned to\r
+ see if a SCSI device is actually present at that location on the SCSI channel.\r
+\r
+ @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.\r
+ @param Target (TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.\r
+ On output, a pointer to the Target ID (an array of\r
+ TARGET_MAX_BYTES) of the next SCSI device present on a SCSI\r
+ channel. An input value of 0xF(all bytes in the array are 0xF) in the\r
+ Target array retrieves the Target ID of the first SCSI device present on a\r
+ SCSI channel.\r
+\r
+ @retval EFI_SUCCESS The Target ID of the next SCSI device on the SCSI\r
+ channel was returned in Target.\r
+ @retval EFI_INVALID_PARAMETER Target or Lun is NULL.\r
+ @retval EFI_TIMEOUT Target array is not all 0xF, and Target was not\r
+ returned on a previous call to GetNextTarget().\r
+ @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+UfsPassThruGetNextTarget (\r
+ IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,\r
+ IN OUT UINT8 **Target\r
+ )\r
+{\r
+ UFS_PASS_THRU_PRIVATE_DATA *Private;\r
+\r
+ Private = UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);\r
+\r
+ if (Target == NULL || *Target == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ SetMem (mUfsTargetId, TARGET_MAX_BYTES, 0xFF);\r
+ if (CompareMem(*Target, mUfsTargetId, TARGET_MAX_BYTES) == 0) {\r
+ SetMem (*Target, TARGET_MAX_BYTES, 0x00);\r
+ return EFI_SUCCESS;\r
+ }\r
+\r
+ return EFI_NOT_FOUND;\r
+}\r
+\r
+/**\r
+ Tests to see if this driver supports a given controller. If a child device is provided,\r
+ it further tests to see if this driver supports creating a handle for the specified child device.\r
+\r
+ This function checks to see if the driver specified by This supports the device specified by\r
+ ControllerHandle. Drivers will typically use the device path attached to\r
+ ControllerHandle and/or the services from the bus I/O abstraction attached to\r
+ ControllerHandle to determine if the driver supports ControllerHandle. This function\r
+ may be called many times during platform initialization. In order to reduce boot times, the tests\r
+ performed by this function must be very small, and take as little time as possible to execute. This\r
+ function must not change the state of any hardware devices, and this function must be aware that the\r
+ device specified by ControllerHandle may already be managed by the same driver or a\r
+ different driver. This function must match its calls to AllocatePages() with FreePages(),\r
+ AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().\r
+ Since ControllerHandle may have been previously started by the same driver, if a protocol is\r
+ already in the opened state, then it must not be closed with CloseProtocol(). This is required\r
+ to guarantee the state of ControllerHandle is not modified by this function.\r
+\r
+ @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
+ @param[in] ControllerHandle The handle of the controller to test. This handle\r
+ must support a protocol interface that supplies\r
+ an I/O abstraction to the driver.\r
+ @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
+ parameter is ignored by device drivers, and is optional for bus\r
+ drivers. For bus drivers, if this parameter is not NULL, then\r
+ the bus driver must determine if the bus controller specified\r
+ by ControllerHandle and the child controller specified\r
+ by RemainingDevicePath are both supported by this\r
+ bus driver.\r
+\r
+ @retval EFI_SUCCESS The device specified by ControllerHandle and\r
+ RemainingDevicePath is supported by the driver specified by This.\r
+ @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and\r
+ RemainingDevicePath is already being managed by the driver\r
+ specified by This.\r
+ @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and\r
+ RemainingDevicePath is already being managed by a different\r
+ driver or an application that requires exclusive access.\r
+ Currently not implemented.\r
+ @retval EFI_UNSUPPORTED The device specified by ControllerHandle and\r
+ RemainingDevicePath is not supported by the driver specified by This.\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+UfsPassThruDriverBindingSupported (\r
+ IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
+ IN EFI_HANDLE Controller,\r
+ IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;\r
+ EDKII_UFS_HOST_CONTROLLER_PROTOCOL *UfsHostController;\r
+\r
+ //\r
+ // Ufs Pass Thru driver is a device driver, and should ingore the\r
+ // "RemainingDevicePath" according to UEFI spec\r
+ //\r
+ Status = gBS->OpenProtocol (\r
+ Controller,\r
+ &gEfiDevicePathProtocolGuid,\r
+ (VOID *) &ParentDevicePath,\r
+ This->DriverBindingHandle,\r
+ Controller,\r
+ EFI_OPEN_PROTOCOL_BY_DRIVER\r
+ );\r
+ if (EFI_ERROR (Status)) {\r
+ //\r
+ // EFI_ALREADY_STARTED is also an error\r
+ //\r
+ return Status;\r
+ }\r
+ //\r
+ // Close the protocol because we don't use it here\r
+ //\r
+ gBS->CloseProtocol (\r
+ Controller,\r
+ &gEfiDevicePathProtocolGuid,\r
+ This->DriverBindingHandle,\r
+ Controller\r
+ );\r
+\r
+ Status = gBS->OpenProtocol (\r
+ Controller,\r
+ &gEdkiiUfsHostControllerProtocolGuid,\r
+ (VOID **) &UfsHostController,\r
+ This->DriverBindingHandle,\r
+ Controller,\r
+ EFI_OPEN_PROTOCOL_BY_DRIVER\r
+ );\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ //\r
+ // EFI_ALREADY_STARTED is also an error\r
+ //\r
+ return Status;\r
+ }\r
+\r
+ //\r
+ // Close the I/O Abstraction(s) used to perform the supported test\r
+ //\r
+ gBS->CloseProtocol (\r
+ Controller,\r
+ &gEdkiiUfsHostControllerProtocolGuid,\r
+ This->DriverBindingHandle,\r
+ Controller\r
+ );\r
+ \r
+ return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+ Starts a device controller or a bus controller.\r
+\r
+ The Start() function is designed to be invoked from the EFI boot service ConnectController().\r
+ As a result, much of the error checking on the parameters to Start() has been moved into this\r
+ common boot service. It is legal to call Start() from other locations,\r
+ but the following calling restrictions must be followed or the system behavior will not be deterministic.\r
+ 1. ControllerHandle must be a valid EFI_HANDLE.\r
+ 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned\r
+ EFI_DEVICE_PATH_PROTOCOL.\r
+ 3. Prior to calling Start(), the Supported() function for the driver specified by This must\r
+ have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.\r
+\r
+ @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
+ @param[in] ControllerHandle The handle of the controller to start. This handle\r
+ must support a protocol interface that supplies\r
+ an I/O abstraction to the driver.\r
+ @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
+ parameter is ignored by device drivers, and is optional for bus\r
+ drivers. For a bus driver, if this parameter is NULL, then handles\r
+ for all the children of Controller are created by this driver.\r
+ If this parameter is not NULL and the first Device Path Node is\r
+ not the End of Device Path Node, then only the handle for the\r
+ child device specified by the first Device Path Node of\r
+ RemainingDevicePath is created by this driver.\r
+ If the first Device Path Node of RemainingDevicePath is\r
+ the End of Device Path Node, no child handle is created by this\r
+ driver.\r
+\r
+ @retval EFI_SUCCESS The device was started.\r
+ @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.\r
+ @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
+ @retval Others The driver failded to start the device.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+UfsPassThruDriverBindingStart (\r
+ IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
+ IN EFI_HANDLE Controller,\r
+ IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ EDKII_UFS_HOST_CONTROLLER_PROTOCOL *UfsHc;\r
+ UFS_PASS_THRU_PRIVATE_DATA *Private;\r
+ UINTN UfsHcBase;\r
+ UINT32 Index;\r
+ UFS_CONFIG_DESC Config;\r
+\r
+ Status = EFI_SUCCESS;\r
+ UfsHc = NULL;\r
+ Private = NULL;\r
+ UfsHcBase = 0;\r
+\r
+ DEBUG ((EFI_D_INFO, "==UfsPassThru Start== Controller = %x\n", Controller));\r
+\r
+ Status = gBS->OpenProtocol (\r
+ Controller,\r
+ &gEdkiiUfsHostControllerProtocolGuid,\r
+ (VOID **) &UfsHc,\r
+ This->DriverBindingHandle,\r
+ Controller,\r
+ EFI_OPEN_PROTOCOL_BY_DRIVER\r
+ );\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((EFI_D_ERROR, "Open Ufs Host Controller Protocol Error, Status = %r\n", Status));\r
+ goto Error;\r
+ }\r
+\r
+ //\r
+ // Get the UFS Host Controller MMIO Bar Base Address.\r
+ //\r
+ Status = UfsHc->GetUfsHcMmioBar (UfsHc, &UfsHcBase);\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((EFI_D_ERROR, "Get Ufs Host Controller Mmio Bar Error, Status = %r\n", Status));\r
+ goto Error;\r
+ }\r
+\r
+ //\r
+ // Initialize Ufs Pass Thru private data for managed UFS Host Controller.\r
+ //\r
+ Private = AllocateCopyPool (sizeof (UFS_PASS_THRU_PRIVATE_DATA), &gUfsPassThruTemplate);\r
+ if (Private == NULL) {\r
+ Status = EFI_OUT_OF_RESOURCES;\r
+ goto Error;\r
+ }\r
+\r
+ Private->ExtScsiPassThru.Mode = &Private->ExtScsiPassThruMode;\r
+ Private->UfsHostController = UfsHc;\r
+ Private->UfsHcBase = UfsHcBase;\r
+\r
+ //\r
+ // Initialize UFS Host Controller H/W.\r
+ //\r
+ Status = UfsControllerInit (Private);\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((EFI_D_ERROR, "Ufs Host Controller Initialization Error, Status = %r\n", Status));\r
+ goto Error;\r
+ }\r
+\r
+ //\r
+ // UFS 2.0 spec Section 13.1.3.3:\r
+ // At the end of the UFS Interconnect Layer initialization on both host and device side, \r
+ // the host shall send a NOP OUT UPIU to verify that the device UTP Layer is ready. \r
+ //\r
+ Status = UfsExecNopCmds (Private);\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((EFI_D_ERROR, "Ufs Sending NOP IN command Error, Status = %r\n", Status));\r
+ goto Error;\r
+ }\r
+\r
+ //\r
+ // The host enables the device initialization completion by setting fDeviceInit flag.\r
+ //\r
+ Status = UfsSetFlag (Private, UfsFlagDevInit);\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((EFI_D_ERROR, "Ufs Set fDeviceInit Flag Error, Status = %r\n", Status));\r
+ goto Error;\r
+ }\r
+\r
+ //\r
+ // Get Ufs Device's Lun Info by reading Configuration Descriptor.\r
+ //\r
+ Status = UfsRwDeviceDesc (Private, TRUE, UfsConfigDesc, 0, 0, &Config, sizeof (UFS_CONFIG_DESC));\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((EFI_D_ERROR, "Ufs Get Configuration Descriptor Error, Status = %r\n", Status));\r
+ goto Error;\r
+ }\r
+\r
+ //\r
+ // Check if 8 common luns are active and set corresponding bit mask.\r
+ // TODO: Parse device descriptor to decide if exposing RPMB LUN to upper layer for authentication access.\r
+ //\r
+ for (Index = 0; Index < 8; Index++) {\r
+ if (Config.UnitDescConfParams[Index].LunEn != 0) {\r
+ Private->Luns.BitMask |= (BIT0 << Index);\r
+ DEBUG ((EFI_D_INFO, "Ufs Lun %d is enabled\n", Index));\r
+ }\r
+ }\r
+\r
+ Status = gBS->InstallProtocolInterface (\r
+ &Controller,\r
+ &gEfiExtScsiPassThruProtocolGuid,\r
+ EFI_NATIVE_INTERFACE,\r
+ &(Private->ExtScsiPassThru)\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ return EFI_SUCCESS;\r
+\r
+Error:\r
+ if (Private != NULL) {\r
+ if (Private->TmrlMapping != NULL) {\r
+ UfsHc->Unmap (UfsHc, Private->TmrlMapping); \r
+ }\r
+ if (Private->UtpTmrlBase != NULL) {\r
+ UfsHc->FreeBuffer (UfsHc, EFI_SIZE_TO_PAGES (Private->Nutmrs * sizeof (UTP_TMRD)), Private->UtpTmrlBase);\r
+ }\r
+\r
+ if (Private->TrlMapping != NULL) {\r
+ UfsHc->Unmap (UfsHc, Private->TrlMapping);\r
+ }\r
+ if (Private->UtpTrlBase != NULL) {\r
+ UfsHc->FreeBuffer (UfsHc, EFI_SIZE_TO_PAGES (Private->Nutrs * sizeof (UTP_TMRD)), Private->UtpTrlBase);\r
+ }\r
+\r
+ FreePool (Private);\r
+ }\r
+\r
+ if (UfsHc != NULL) {\r
+ gBS->CloseProtocol (\r
+ Controller,\r
+ &gEdkiiUfsHostControllerProtocolGuid,\r
+ This->DriverBindingHandle,\r
+ Controller\r
+ );\r
+ }\r
+\r
+ return Status;\r
+}\r
+\r
+/**\r
+ Stops a device controller or a bus controller.\r
+\r
+ The Stop() function is designed to be invoked from the EFI boot service DisconnectController().\r
+ As a result, much of the error checking on the parameters to Stop() has been moved\r
+ into this common boot service. It is legal to call Stop() from other locations,\r
+ but the following calling restrictions must be followed or the system behavior will not be deterministic.\r
+ 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this\r
+ same driver's Start() function.\r
+ 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid\r
+ EFI_HANDLE. In addition, all of these handles must have been created in this driver's\r
+ Start() function, and the Start() function must have called OpenProtocol() on\r
+ ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r
+\r
+ @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
+ @param[in] ControllerHandle A handle to the device being stopped. The handle must\r
+ support a bus specific I/O protocol for the driver\r
+ to use to stop the device.\r
+ @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.\r
+ @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL\r
+ if NumberOfChildren is 0.\r
+\r
+ @retval EFI_SUCCESS The device was stopped.\r
+ @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+UfsPassThruDriverBindingStop (\r
+ IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
+ IN EFI_HANDLE Controller,\r
+ IN UINTN NumberOfChildren,\r
+ IN EFI_HANDLE *ChildHandleBuffer\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UFS_PASS_THRU_PRIVATE_DATA *Private;\r
+ EFI_EXT_SCSI_PASS_THRU_PROTOCOL *ExtScsiPassThru;\r
+ EDKII_UFS_HOST_CONTROLLER_PROTOCOL *UfsHc;\r
+\r
+ DEBUG ((EFI_D_INFO, "==UfsPassThru Stop== Controller Controller = %x\n", Controller));\r
+\r
+ Status = gBS->OpenProtocol (\r
+ Controller,\r
+ &gEfiExtScsiPassThruProtocolGuid,\r
+ (VOID **) &ExtScsiPassThru,\r
+ This->DriverBindingHandle,\r
+ Controller,\r
+ EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
+ );\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+\r
+ Private = UFS_PASS_THRU_PRIVATE_DATA_FROM_THIS (ExtScsiPassThru);\r
+ UfsHc = Private->UfsHostController;\r
+\r
+ Status = gBS->UninstallProtocolInterface (\r
+ Controller,\r
+ &gEfiExtScsiPassThruProtocolGuid,\r
+ &(Private->ExtScsiPassThru)\r
+ );\r
+\r
+ if (EFI_ERROR (Status)) {\r
+ return EFI_DEVICE_ERROR;\r
+ }\r
+\r
+ //\r
+ // Stop Ufs Host Controller\r
+ //\r
+ Status = UfsControllerStop (Private);\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ if (Private->TmrlMapping != NULL) {\r
+ UfsHc->Unmap (UfsHc, Private->TmrlMapping);\r
+ }\r
+ if (Private->UtpTmrlBase != NULL) {\r
+ UfsHc->FreeBuffer (UfsHc, EFI_SIZE_TO_PAGES (Private->Nutmrs * sizeof (UTP_TMRD)), Private->UtpTmrlBase);\r
+ }\r
+\r
+ if (Private->TrlMapping != NULL) {\r
+ UfsHc->Unmap (UfsHc, Private->TrlMapping);\r
+ }\r
+ if (Private->UtpTrlBase != NULL) {\r
+ UfsHc->FreeBuffer (UfsHc, EFI_SIZE_TO_PAGES (Private->Nutrs * sizeof (UTP_TMRD)), Private->UtpTrlBase);\r
+ }\r
+\r
+ FreePool (Private);\r
+\r
+ //\r
+ // Close protocols opened by UfsPassThru controller driver\r
+ //\r
+ gBS->CloseProtocol (\r
+ Controller,\r
+ &gEdkiiUfsHostControllerProtocolGuid,\r
+ This->DriverBindingHandle,\r
+ Controller\r
+ );\r
+\r
+ return Status;\r
+}\r
+\r
+\r
+/**\r
+ The user Entry Point for module UfsPassThru. The user code starts with this function.\r
+\r
+ @param[in] ImageHandle The firmware allocated handle for the EFI image.\r
+ @param[in] SystemTable A pointer to the EFI System Table.\r
+\r
+ @retval EFI_SUCCESS The entry point is executed successfully.\r
+ @retval other Some error occurs when executing this entry point.\r
+\r
+**/\r
+EFI_STATUS\r
+EFIAPI\r
+InitializeUfsPassThru (\r
+ IN EFI_HANDLE ImageHandle,\r
+ IN EFI_SYSTEM_TABLE *SystemTable\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+\r
+ //\r
+ // Install driver model protocol(s).\r
+ //\r
+ Status = EfiLibInstallDriverBindingComponentName2 (\r
+ ImageHandle,\r
+ SystemTable,\r
+ &gUfsPassThruDriverBinding,\r
+ ImageHandle,\r
+ &gUfsPassThruComponentName,\r
+ &gUfsPassThruComponentName2\r
+ );\r
+ ASSERT_EFI_ERROR (Status);\r
+\r
+ return Status;\r
+}\r