23 EFI_PEI_RECOVERY_BLOCK_IO2_PPI_REVISION,
29 EFI_PEI_PPI_DESCRIPTOR_PPI,
30 &gEfiPeiVirtualBlockIoPpiGuid,
34 EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST,
35 &gEfiPeiVirtualBlockIo2PpiGuid,
105 (EFI_PEI_PPI_DESCRIPTOR_NOTIFY_CALLBACK | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
106 &gEfiEndOfPeiSignalPpiGuid,
151 OUT VOID *SenseData OPTIONAL,
152 OUT UINT8 *SenseDataLength
156 UINT8 Cdb[UFS_SCSI_OP_LENGTH_SIX];
162 Cdb[0] = EFI_SCSI_OP_TEST_UNIT_READY;
173 if (*SenseDataLength != 0) {
199 OUT VOID *DataBuffer,
200 OUT UINT32 *DataLength,
201 OUT VOID *SenseData OPTIONAL,
202 OUT UINT8 *SenseDataLength
206 UINT8 Cdb[UFS_SCSI_OP_LENGTH_TEN];
212 Cdb[0] = EFI_SCSI_OP_READ_CAPACITY;
225 if (*SenseDataLength != 0) {
229 if (!EFI_ERROR (Status)) {
255 OUT VOID *DataBuffer,
256 OUT UINT32 *DataLength,
257 OUT VOID *SenseData OPTIONAL,
258 OUT UINT8 *SenseDataLength
262 UINT8 Cdb[UFS_SCSI_OP_LENGTH_SIXTEEN];
268 Cdb[0] = EFI_SCSI_OP_READ_CAPACITY16;
283 if (*SenseDataLength != 0) {
287 if (!EFI_ERROR (Status)) {
317 OUT VOID *DataBuffer,
318 OUT UINT32 *DataLength,
319 OUT VOID *SenseData OPTIONAL,
320 OUT UINT8 *SenseDataLength
324 UINT8 Cdb[UFS_SCSI_OP_LENGTH_TEN];
330 Cdb[0] = EFI_SCSI_OP_READ10;
345 if (*SenseDataLength != 0) {
349 if (!EFI_ERROR (Status)) {
379 OUT VOID *DataBuffer,
380 OUT UINT32 *DataLength,
381 OUT VOID *SenseData OPTIONAL,
382 OUT UINT8 *SenseDataLength
386 UINT8 Cdb[UFS_SCSI_OP_LENGTH_SIXTEEN];
392 Cdb[0] = EFI_SCSI_OP_READ16;
407 if (*SenseDataLength != 0) {
411 if (!EFI_ERROR (Status)) {
433 OUT BOOLEAN *NeedRetry
436 if ((SenseData->Sense_Key == EFI_SCSI_SK_NOT_READY) &&
437 (SenseData->Addnl_Sense_Code == EFI_SCSI_ASC_NO_MEDIA))
439 Media->MediaPresent =
FALSE;
441 DEBUG ((DEBUG_VERBOSE,
"UfsBlockIoPei: Is No Media\n"));
442 return EFI_DEVICE_ERROR;
445 if ((SenseData->Sense_Key == EFI_SCSI_SK_UNIT_ATTENTION) &&
446 (SenseData->Addnl_Sense_Code == EFI_SCSI_ASC_MEDIA_CHANGE))
449 DEBUG ((DEBUG_VERBOSE,
"UfsBlockIoPei: Is Media Change\n"));
453 if ((SenseData->Sense_Key == EFI_SCSI_SK_UNIT_ATTENTION) &&
457 DEBUG ((DEBUG_VERBOSE,
"UfsBlockIoPei: Was Reset Before\n"));
461 if ((SenseData->Sense_Key == EFI_SCSI_SK_MEDIUM_ERROR) ||
462 ((SenseData->Sense_Key == EFI_SCSI_SK_NOT_READY) &&
463 (SenseData->Addnl_Sense_Code == EFI_SCSI_ASC_MEDIA_UPSIDE_DOWN)))
466 DEBUG ((DEBUG_VERBOSE,
"UfsBlockIoPei: Media Error\n"));
467 return EFI_DEVICE_ERROR;
470 if (SenseData->Sense_Key == EFI_SCSI_SK_HARDWARE_ERROR) {
472 DEBUG ((DEBUG_VERBOSE,
"UfsBlockIoPei: Hardware Error\n"));
473 return EFI_DEVICE_ERROR;
476 if ((SenseData->Sense_Key == EFI_SCSI_SK_NOT_READY) &&
477 (SenseData->Addnl_Sense_Code == EFI_SCSI_ASC_NOT_READY) &&
478 (SenseData->Addnl_Sense_Code_Qualifier == EFI_SCSI_ASCQ_IN_PROGRESS))
481 DEBUG ((DEBUG_VERBOSE,
"UfsBlockIoPei: Was Reset Before\n"));
486 DEBUG ((DEBUG_VERBOSE,
"UfsBlockIoPei: Sense Key = 0x%x ASC = 0x%x!\n", SenseData->Sense_Key, SenseData->Addnl_Sense_Code));
487 return EFI_DEVICE_ERROR;
522 *NumberBlockDevices = UFS_PEIM_MAX_LUNS;
579 UINT8 SenseDataLength;
586 Private = GET_UFS_PEIM_HC_PRIVATE_DATA_FROM_THIS (This);
589 if ((DeviceIndex == 0) || (DeviceIndex > UFS_PEIM_MAX_LUNS)) {
590 return EFI_INVALID_PARAMETER;
593 Lun = DeviceIndex - 1;
594 if ((Private->Luns.BitMask & (BIT0 << Lun)) == 0) {
595 return EFI_ACCESS_DENIED;
598 ZeroMem (&SenseData,
sizeof (SenseData));
599 ZeroMem (&Capacity,
sizeof (Capacity));
600 ZeroMem (&Capacity16,
sizeof (Capacity16));
601 SenseDataLength =
sizeof (SenseData);
612 if (!EFI_ERROR (Status)) {
616 if (SenseDataLength == 0) {
621 if (EFI_ERROR (Status)) {
622 return EFI_DEVICE_ERROR;
629 if (EFI_ERROR (Status)) {
630 return EFI_DEVICE_ERROR;
633 if ((Capacity.LastLba3 == 0xff) && (Capacity.LastLba2 == 0xff) &&
634 (Capacity.LastLba1 == 0xff) && (Capacity.LastLba0 == 0xff))
639 if (EFI_ERROR (Status)) {
640 return EFI_DEVICE_ERROR;
643 Private->Media[Lun].
LastBlock = ((UINT32)Capacity16.LastLba3 << 24) | (Capacity16.LastLba2 << 16) | (Capacity16.LastLba1 << 8) | Capacity16.LastLba0;
645 Private->Media[Lun].
BlockSize = (Capacity16.BlockSize3 << 24) | (Capacity16.BlockSize2 << 16) | (Capacity16.BlockSize1 << 8) | Capacity16.BlockSize0;
647 Private->Media[Lun].
LastBlock = ((UINT32)Capacity.LastLba3 << 24) | (Capacity.LastLba2 << 16) | (Capacity.LastLba1 << 8) | Capacity.LastLba0;
648 Private->Media[Lun].
BlockSize = (Capacity.BlockSize3 << 24) | (Capacity.BlockSize2 << 16) | (Capacity.BlockSize1 << 8) | Capacity.BlockSize0;
652 MediaInfo->MediaPresent = Private->Media[Lun].
MediaPresent;
654 MediaInfo->BlockSize = Private->Media[Lun].
BlockSize;
706 UINTN NumberOfBlocks;
709 UINT8 SenseDataLength;
715 Private = GET_UFS_PEIM_HC_PRIVATE_DATA_FROM_THIS (This);
717 ZeroMem (&SenseData,
sizeof (SenseData));
718 SenseDataLength =
sizeof (SenseData);
723 if (Buffer ==
NULL) {
724 return EFI_INVALID_PARAMETER;
727 if (BufferSize == 0) {
731 if ((DeviceIndex == 0) || (DeviceIndex > UFS_PEIM_MAX_LUNS)) {
732 return EFI_INVALID_PARAMETER;
735 Lun = DeviceIndex - 1;
736 if ((Private->Luns.BitMask & (BIT0 << Lun)) == 0) {
737 return EFI_ACCESS_DENIED;
740 BlockSize = Private->Media[Lun].
BlockSize;
742 if (BufferSize % BlockSize != 0) {
743 Status = EFI_BAD_BUFFER_SIZE;
746 if (StartLBA > Private->Media[Lun].
LastBlock) {
747 Status = EFI_INVALID_PARAMETER;
750 NumberOfBlocks = BufferSize / BlockSize;
759 if (!EFI_ERROR (Status)) {
763 if (SenseDataLength == 0) {
768 if (EFI_ERROR (Status)) {
769 return EFI_DEVICE_ERROR;
774 if (Private->Media[Lun].
LastBlock < 0xfffffffful) {
779 (UINT32)NumberOfBlocks,
781 (UINT32 *)&BufferSize,
790 (UINT32)NumberOfBlocks,
792 (UINT32 *)&BufferSize,
833 *NumberBlockDevices = UFS_PEIM_MAX_LUNS;
892 Private = GET_UFS_PEIM_HC_PRIVATE_DATA_FROM_THIS2 (This);
900 if (EFI_ERROR (Status)) {
904 Lun = DeviceIndex - 1;
958 Private = GET_UFS_PEIM_HC_PRIVATE_DATA_FROM_THIS2 (This);
992 Private = GET_UFS_PEIM_HC_PRIVATE_DATA_FROM_THIS_NOTIFY (NotifyDescriptor);
994 if ((Private->Pool !=
NULL) && (Private->Pool->Head !=
NULL)) {
998 if (Private->UtpTmrlBase !=
NULL) {
1001 Private->UtpTmrlBase,
1002 Private->TmrlMapping
1006 if (Private->UtpTrlBase !=
NULL) {
1009 Private->UtpTrlBase,
1035 UINT8 DeviceInitStatus;
1038 DeviceInitStatus = 0xFF;
1039 Timeout =
PcdGet32 (PcdUfsInitialCompletionTimeout);
1044 Status =
UfsSetFlag (Private, UfsFlagDevInit);
1045 if (EFI_ERROR (Status)) {
1050 Status =
UfsReadFlag (Private, UfsFlagDevInit, &DeviceInitStatus);
1051 if (EFI_ERROR (Status)) {
1057 }
while (DeviceInitStatus != 0 && Timeout != 0);
1060 DEBUG ((DEBUG_ERROR,
"%a: DeviceInitStatus = %x EFI_TIMEOUT \n", __func__, DeviceInitStatus));
1063 DEBUG ((DEBUG_INFO,
"%a: Timeout left = %x EFI_SUCCESS \n", __func__, Timeout));
1095 EDKII_UFS_CARD_REF_CLK_FREQ_ATTRIBUTE Attributes;
1108 &gEdkiiPeiUfsHostControllerPpiGuid,
1113 if (EFI_ERROR (Status)) {
1114 return EFI_DEVICE_ERROR;
1121 &gEdkiiUfsHcPlatformPpiGuid,
1124 (VOID **)&UfsHcPlatformPpi
1126 if (EFI_ERROR (Status)) {
1127 DEBUG ((DEBUG_ERROR,
"LocatePpi gEdkiiUfsHcPlatformPpiGuid Status :%r\n", Status));
1135 Status = UfsHcPpi->GetUfsHcMmioBar (UfsHcPpi, Controller, &MmioBase);
1139 if (EFI_ERROR (Status)) {
1144 if (Private ==
NULL) {
1145 Status = EFI_OUT_OF_RESOURCES;
1149 Private->BlkIoPpiList.
Ppi = &Private->BlkIoPpi;
1150 Private->BlkIo2PpiList.
Ppi = &Private->BlkIo2Ppi;
1151 Private->UfsHcBase = MmioBase;
1157 if (EFI_ERROR (Status)) {
1158 Status = EFI_OUT_OF_RESOURCES;
1166 if (EFI_ERROR (Status)) {
1167 DEBUG ((DEBUG_ERROR,
"UfsDevicePei: Host Controller Initialization Error, Status = %r\n", Status));
1178 if (EFI_ERROR (Status)) {
1179 DEBUG ((DEBUG_ERROR,
"Ufs Sending NOP IN command Error, Status = %r\n", Status));
1188 if (EFI_ERROR (Status)) {
1189 DEBUG ((DEBUG_ERROR,
"Device failed to finish initialization, Status = %r\n", Status));
1194 if ((UfsHcPlatformPpi !=
NULL) &&
1195 ((UfsHcPlatformPpi->
RefClkFreq == EdkiiUfsCardRefClkFreq19p2Mhz) ||
1196 (UfsHcPlatformPpi->
RefClkFreq == EdkiiUfsCardRefClkFreq26Mhz) ||
1197 (UfsHcPlatformPpi->
RefClkFreq == EdkiiUfsCardRefClkFreq38p4Mhz)))
1199 RefClkAttr = UfsAttrRefClkFreq;
1200 Attributes = EdkiiUfsCardRefClkFreqObsolete;
1203 DEBUG ((DEBUG_INFO,
"UfsRwAttributes #1 Status = %r \n", Status));
1204 if (!EFI_ERROR (Status)) {
1205 if (Attributes != UfsHcPlatformPpi->
RefClkFreq) {
1209 "Setting bRefClkFreq attribute(%x) to %x\n 0 -> 19.2 Mhz\n 1 -> 26 Mhz\n 2 -> 38.4 Mhz\n 3 -> Obsolete\n",
1214 DEBUG ((DEBUG_INFO,
"UfsRwAttributes #2 Status = %r \n", Status));
1215 if (EFI_ERROR (Status)) {
1218 "Failed to Change Reference Clock Attribute to, Status = %r \n",
1226 "Failed to Read Reference Clock Attribute, Status = %r \n",
1233 Status = UfsHcPlatformPpi->
Callback (&Private->UfsHcBase, EdkiiUfsHcPostLinkStartup);
1234 if (EFI_ERROR (Status)) {
1235 DEBUG ((DEBUG_ERROR,
"Failure from platform driver during EdkiiUfsHcPostLinkStartup, Status = %r\n", Status));
1243 for (Index = 0; Index < UFS_PEIM_MAX_LUNS; Index++) {
1245 if (EFI_ERROR (Status)) {
1246 DEBUG ((DEBUG_ERROR,
"Fail to read UFS Unit Descriptor, Index = %X, Status = %r\n", Index, Status));
1250 if (UnitDescriptor.LunEn == 0x1) {
1251 DEBUG ((DEBUG_INFO,
"Ufs %d Lun %d is enabled\n", Controller, Index));
1252 Private->Luns.BitMask |= (BIT0 << Index);
1260 if (EFI_ERROR (Status)) {
1261 DEBUG ((DEBUG_ERROR,
"Ufs Get Configuration Descriptor Error, Status = %r\n", Status));
1266 if (DeviceDescriptor.SecurityLun == 0x1) {
1267 DEBUG ((DEBUG_INFO,
"UFS WLUN RPMB is supported\n"));
1268 Private->Luns.BitMask |= BIT11;
EFI_STATUS IoMmuFreeBuffer(IN UINTN Pages, IN VOID *HostAddress, IN VOID *Mapping)
UINTN EFIAPI MicroSecondDelay(IN UINTN MicroSeconds)
UINT16 EFIAPI SwapBytes16(IN UINT16 Value)
UINT32 EFIAPI SwapBytes32(IN UINT32 Value)
UINT32 EFIAPI WriteUnaligned32(OUT UINT32 *Buffer, IN UINT32 Value)
UINT16 EFIAPI WriteUnaligned16(OUT UINT16 *Buffer, IN UINT16 Value)
UINT64 EFIAPI LShiftU64(IN UINT64 Operand, IN UINTN Count)
UINT64 EFIAPI SwapBytes64(IN UINT64 Value)
UINT64 EFIAPI WriteUnaligned64(OUT UINT64 *Buffer, IN UINT64 Value)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
VOID *EFIAPI AllocateCopyPool(IN UINTN AllocationSize, IN CONST VOID *Buffer)
EFI_STATUS EFIAPI PeiServicesLocatePpi(IN CONST EFI_GUID *Guid, IN UINTN Instance, IN OUT EFI_PEI_PPI_DESCRIPTOR **PpiDescriptor, IN OUT VOID **Ppi)
EFI_STATUS EFIAPI PeiServicesNotifyPpi(IN CONST EFI_PEI_NOTIFY_DESCRIPTOR *NotifyList)
EFI_STATUS EFIAPI PeiServicesInstallPpi(IN CONST EFI_PEI_PPI_DESCRIPTOR *PpiList)
EFI_STATUS EFIAPI PeiServicesRegisterForShadow(IN EFI_PEI_FILE_HANDLE FileHandle)
#define DEBUG(Expression)
@ UfsDevice
The recovery device is a Universal Flash Storage device.
#define PcdGet32(TokenName)
VOID IoMmuInit(OUT EDKII_IOMMU_PPI **IoMmu)
VOID * EFI_PEI_FILE_HANDLE
#define EFI_SCSI_ASC_RESET
Power On Reset or Bus Reset occurred.
#define EFI_SIZE_TO_PAGES(Size)
EFI_STATUS EFIAPI UfsBlockIoPeimReadBlocks(IN EFI_PEI_SERVICES **PeiServices, IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This, IN UINTN DeviceIndex, IN EFI_PEI_LBA StartLBA, IN UINTN BufferSize, OUT VOID *Buffer)
EFI_STATUS UfsFinishDeviceInitialization(IN UFS_PEIM_HC_PRIVATE_DATA *Private)
EFI_STATUS UfsPeimReadCapacity(IN UFS_PEIM_HC_PRIVATE_DATA *Private, IN UINTN Lun, OUT VOID *DataBuffer, OUT UINT32 *DataLength, OUT VOID *SenseData OPTIONAL, OUT UINT8 *SenseDataLength)
EFI_STATUS EFIAPI UfsBlockIoPeimReadBlocks2(IN EFI_PEI_SERVICES **PeiServices, IN EFI_PEI_RECOVERY_BLOCK_IO2_PPI *This, IN UINTN DeviceIndex, IN EFI_PEI_LBA StartLBA, IN UINTN BufferSize, OUT VOID *Buffer)
EFI_STATUS EFIAPI UfsEndOfPei(IN EFI_PEI_SERVICES **PeiServices, IN EFI_PEI_NOTIFY_DESCRIPTOR *NotifyDescriptor, IN VOID *Ppi)
EFI_STATUS UfsPeimReadCapacity16(IN UFS_PEIM_HC_PRIVATE_DATA *Private, IN UINTN Lun, OUT VOID *DataBuffer, OUT UINT32 *DataLength, OUT VOID *SenseData OPTIONAL, OUT UINT8 *SenseDataLength)
EFI_STATUS UfsPeimRead10(IN UFS_PEIM_HC_PRIVATE_DATA *Private, IN UINTN Lun, IN UINTN StartLba, IN UINT32 SectorNum, OUT VOID *DataBuffer, OUT UINT32 *DataLength, OUT VOID *SenseData OPTIONAL, OUT UINT8 *SenseDataLength)
EFI_STATUS UfsPeimTestUnitReady(IN UFS_PEIM_HC_PRIVATE_DATA *Private, IN UINTN Lun, OUT VOID *SenseData OPTIONAL, OUT UINT8 *SenseDataLength)
EFI_STATUS EFIAPI InitializeUfsBlockIoPeim(IN EFI_PEI_FILE_HANDLE FileHandle, IN CONST EFI_PEI_SERVICES **PeiServices)
EFI_STATUS EFIAPI UfsBlockIoPeimGetMediaInfo(IN EFI_PEI_SERVICES **PeiServices, IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This, IN UINTN DeviceIndex, OUT EFI_PEI_BLOCK_IO_MEDIA *MediaInfo)
EFI_STATUS EFIAPI UfsBlockIoPeimGetDeviceNo2(IN EFI_PEI_SERVICES **PeiServices, IN EFI_PEI_RECOVERY_BLOCK_IO2_PPI *This, OUT UINTN *NumberBlockDevices)
EFI_STATUS EFIAPI UfsBlockIoPeimGetMediaInfo2(IN EFI_PEI_SERVICES **PeiServices, IN EFI_PEI_RECOVERY_BLOCK_IO2_PPI *This, IN UINTN DeviceIndex, OUT EFI_PEI_BLOCK_IO2_MEDIA *MediaInfo)
EFI_STATUS UfsPeimRead16(IN UFS_PEIM_HC_PRIVATE_DATA *Private, IN UINTN Lun, IN UINTN StartLba, IN UINT32 SectorNum, OUT VOID *DataBuffer, OUT UINT32 *DataLength, OUT VOID *SenseData OPTIONAL, OUT UINT8 *SenseDataLength)
EFI_STATUS UfsPeimParsingSenseKeys(IN EFI_PEI_BLOCK_IO2_MEDIA *Media, IN EFI_SCSI_SENSE_DATA *SenseData, OUT BOOLEAN *NeedRetry)
EFI_STATUS EFIAPI UfsBlockIoPeimGetDeviceNo(IN EFI_PEI_SERVICES **PeiServices, IN EFI_PEI_RECOVERY_BLOCK_IO_PPI *This, OUT UINTN *NumberBlockDevices)
EFI_STATUS UfsRwDeviceDesc(IN UFS_PEIM_HC_PRIVATE_DATA *Private, IN BOOLEAN Read, IN UINT8 DescId, IN UINT8 Index, IN UINT8 Selector, IN OUT VOID *Descriptor, IN UINT32 DescSize)
EFI_STATUS UfsRwAttributes(IN UFS_PEIM_HC_PRIVATE_DATA *Private, IN BOOLEAN Read, IN UINT8 AttrId, IN UINT8 Index, IN UINT8 Selector, IN OUT UINT32 *Attributes)
EFI_STATUS UfsPeimFreeMemPool(IN UFS_PEIM_MEM_POOL *Pool)
EFI_STATUS UfsReadFlag(IN UFS_PEIM_HC_PRIVATE_DATA *Private, IN UINT8 FlagId, OUT UINT8 *Value)
EFI_STATUS UfsPeimInitMemPool(IN UFS_PEIM_HC_PRIVATE_DATA *Private)
EFI_STATUS UfsExecNopCmds(IN UFS_PEIM_HC_PRIVATE_DATA *Private)
EFI_STATUS UfsControllerInit(IN EDKII_UFS_HC_PLATFORM_PPI *UfsHcPlatformPpi, IN UFS_PEIM_HC_PRIVATE_DATA *Private)
EFI_STATUS UfsControllerStop(IN UFS_PEIM_HC_PRIVATE_DATA *Private)
EFI_STATUS UfsSetFlag(IN UFS_PEIM_HC_PRIVATE_DATA *Private, IN UINT8 FlagId)
EFI_STATUS UfsExecScsiCmds(IN UFS_PEIM_HC_PRIVATE_DATA *Private, IN UINT8 Lun, IN OUT UFS_SCSI_REQUEST_PACKET *Packet)
EDKII_UFS_CARD_REF_CLK_FREQ_ATTRIBUTE RefClkFreq
EDKII_UFS_HC_PLATFORM_PEI_CALLBACK Callback