3 Copyright (c) 2014 - 2018, Intel Corporation. All rights reserved.<BR>
4 SPDX-License-Identifier: BSD-2-Clause-Patent
8 #include "UfsBlockIoPei.h"
11 Wait for the value of the specified system memory set to the test value.
13 @param Address The system memory address to test.
14 @param MaskValue The mask value of memory.
15 @param TestValue The test value of memory.
16 @param Timeout The time out value for wait memory set, uses 100ns as a unit.
18 @retval EFI_TIMEOUT The system memory setting is time out.
19 @retval EFI_SUCCESS The system memory is correct set.
41 Delay
= DivU64x32 (Timeout
, 10) + 1;
45 // Access PCI MMIO space to see if the value is the tested one.
47 Value
= MmioRead32 (Address
) & MaskValue
;
49 if (Value
== TestValue
) {
54 // Stall for 1 microseconds.
59 } while (InfiniteWait
|| (Delay
> 0));
65 Dump UIC command execution result for debugging.
67 @param[in] UicOpcode The executed UIC opcode.
68 @param[in] Result The result to be parsed.
72 DumpUicCmdExecResult (
77 if (UicOpcode
<= UfsUicDmePeerSet
) {
82 DEBUG ((DEBUG_VERBOSE
, "UIC configuration command fails - INVALID_MIB_ATTRIBUTE\n"));
85 DEBUG ((DEBUG_VERBOSE
, "UIC configuration command fails - INVALID_MIB_ATTRIBUTE_VALUE\n"));
88 DEBUG ((DEBUG_VERBOSE
, "UIC configuration command fails - READ_ONLY_MIB_ATTRIBUTE\n"));
91 DEBUG ((DEBUG_VERBOSE
, "UIC configuration command fails - WRITE_ONLY_MIB_ATTRIBUTE\n"));
94 DEBUG ((DEBUG_VERBOSE
, "UIC configuration command fails - BAD_INDEX\n"));
97 DEBUG ((DEBUG_VERBOSE
, "UIC configuration command fails - LOCKED_MIB_ATTRIBUTE\n"));
100 DEBUG ((DEBUG_VERBOSE
, "UIC configuration command fails - BAD_TEST_FEATURE_INDEX\n"));
103 DEBUG ((DEBUG_VERBOSE
, "UIC configuration command fails - PEER_COMMUNICATION_FAILURE\n"));
106 DEBUG ((DEBUG_VERBOSE
, "UIC configuration command fails - BUSY\n"));
109 DEBUG ((DEBUG_VERBOSE
, "UIC configuration command fails - DME_FAILURE\n"));
120 DEBUG ((DEBUG_VERBOSE
, "UIC control command fails - FAILURE\n"));
130 Dump QUERY RESPONSE UPIU result for debugging.
132 @param[in] Result The result to be parsed.
136 DumpQueryResponseResult (
142 DEBUG ((DEBUG_VERBOSE
, "Query Response with Parameter Not Readable\n"));
145 DEBUG ((DEBUG_VERBOSE
, "Query Response with Parameter Not Writeable\n"));
148 DEBUG ((DEBUG_VERBOSE
, "Query Response with Parameter Already Written\n"));
151 DEBUG ((DEBUG_VERBOSE
, "Query Response with Invalid Length\n"));
154 DEBUG ((DEBUG_VERBOSE
, "Query Response with Invalid Value\n"));
157 DEBUG ((DEBUG_VERBOSE
, "Query Response with Invalid Selector\n"));
160 DEBUG ((DEBUG_VERBOSE
, "Query Response with Invalid Index\n"));
163 DEBUG ((DEBUG_VERBOSE
, "Query Response with Invalid Idn\n"));
166 DEBUG ((DEBUG_VERBOSE
, "Query Response with Invalid Opcode\n"));
169 DEBUG ((DEBUG_VERBOSE
, "Query Response with General Failure\n"));
178 Swap little endian to big endian.
180 @param[in, out] Buffer The data buffer. In input, it contains little endian data.
181 In output, it will become big endian.
182 @param[in] BufferSize The length of converted data.
186 SwapLittleEndianToBigEndian (
187 IN OUT UINT8
*Buffer
,
195 SwapCount
= BufferSize
/ 2;
196 for (Index
= 0; Index
< SwapCount
; Index
++) {
197 Temp
= Buffer
[Index
];
198 Buffer
[Index
] = Buffer
[BufferSize
- 1 - Index
];
199 Buffer
[BufferSize
- 1 - Index
] = Temp
;
204 Fill TSF field of QUERY REQUEST UPIU.
206 @param[in, out] TsfBase The base address of TSF field of QUERY REQUEST UPIU.
207 @param[in] Opcode The opcode of request.
208 @param[in] DescId The descriptor ID of request.
209 @param[in] Index The index of request.
210 @param[in] Selector The selector of request.
211 @param[in] Length The length of transferred data. The maximum is 4.
212 @param[in] Value The value of transferred data.
216 UfsFillTsfOfQueryReqUpiu (
217 IN OUT UTP_UPIU_TSF
*TsfBase
,
219 IN UINT8 DescId OPTIONAL
,
220 IN UINT8 Index OPTIONAL
,
221 IN UINT8 Selector OPTIONAL
,
222 IN UINT16 Length OPTIONAL
,
223 IN UINT32 Value OPTIONAL
226 ASSERT (TsfBase
!= NULL
);
227 ASSERT (Opcode
<= UtpQueryFuncOpcodeTogFlag
);
229 TsfBase
->Opcode
= Opcode
;
230 if (Opcode
!= UtpQueryFuncOpcodeNop
) {
231 TsfBase
->DescId
= DescId
;
232 TsfBase
->Index
= Index
;
233 TsfBase
->Selector
= Selector
;
235 if ((Opcode
== UtpQueryFuncOpcodeRdDesc
) || (Opcode
== UtpQueryFuncOpcodeWrDesc
)) {
236 SwapLittleEndianToBigEndian ((UINT8
*)&Length
, sizeof (Length
));
237 TsfBase
->Length
= Length
;
240 if (Opcode
== UtpQueryFuncOpcodeWrAttr
) {
241 SwapLittleEndianToBigEndian ((UINT8
*)&Value
, sizeof (Value
));
242 TsfBase
->Value
= Value
;
248 Initialize COMMAND UPIU.
250 @param[in, out] Command The base address of COMMAND UPIU.
251 @param[in] Lun The Lun on which the SCSI command is executed.
252 @param[in] TaskTag The task tag of request.
253 @param[in] Cdb The cdb buffer containing SCSI command.
254 @param[in] CdbLength The cdb length.
255 @param[in] DataDirection The direction of data transfer.
256 @param[in] ExpDataTranLen The expected transfer data length.
258 @retval EFI_SUCCESS The initialization succeed.
263 IN OUT UTP_COMMAND_UPIU
*Command
,
268 IN UFS_DATA_DIRECTION DataDirection
,
269 IN UINT32 ExpDataTranLen
274 ASSERT ((Command
!= NULL
) && (Cdb
!= NULL
));
277 // Task attribute is hard-coded to Ordered.
279 if (DataDirection
== UfsDataIn
) {
281 } else if (DataDirection
== UfsDataOut
) {
288 // Fill UTP COMMAND UPIU associated fields.
290 Command
->TransCode
= 0x01;
291 Command
->Flags
= Flags
;
293 Command
->TaskTag
= TaskTag
;
294 Command
->CmdSet
= 0x00;
295 SwapLittleEndianToBigEndian ((UINT8
*)&ExpDataTranLen
, sizeof (ExpDataTranLen
));
296 Command
->ExpDataTranLen
= ExpDataTranLen
;
298 CopyMem (Command
->Cdb
, Cdb
, CdbLength
);
304 Initialize UTP PRDT for data transfer.
306 @param[in] Prdt The base address of PRDT.
307 @param[in] Buffer The buffer to be read or written.
308 @param[in] BufferSize The data size to be read or written.
310 @retval EFI_SUCCESS The initialization succeed.
325 if ((BufferSize
& (BIT0
| BIT1
)) != 0) {
326 BufferSize
&= ~(BIT0
| BIT1
);
327 DEBUG ((DEBUG_WARN
, "UfsInitUtpPrdt: The BufferSize [%d] is not dword-aligned!\n", BufferSize
));
330 if (BufferSize
== 0) {
334 ASSERT (((UINTN
)Buffer
& (BIT0
| BIT1
)) == 0);
336 RemainingLen
= BufferSize
;
338 PrdtNumber
= (UINTN
)DivU64x32 ((UINT64
)BufferSize
+ UFS_MAX_DATA_LEN_PER_PRD
- 1, UFS_MAX_DATA_LEN_PER_PRD
);
340 for (PrdtIndex
= 0; PrdtIndex
< PrdtNumber
; PrdtIndex
++) {
341 if (RemainingLen
< UFS_MAX_DATA_LEN_PER_PRD
) {
342 Prdt
[PrdtIndex
].DbCount
= (UINT32
)RemainingLen
- 1;
344 Prdt
[PrdtIndex
].DbCount
= UFS_MAX_DATA_LEN_PER_PRD
- 1;
347 Prdt
[PrdtIndex
].DbAddr
= (UINT32
)RShiftU64 ((UINT64
)(UINTN
)Remaining
, 2);
348 Prdt
[PrdtIndex
].DbAddrU
= (UINT32
)RShiftU64 ((UINT64
)(UINTN
)Remaining
, 32);
349 RemainingLen
-= UFS_MAX_DATA_LEN_PER_PRD
;
350 Remaining
+= UFS_MAX_DATA_LEN_PER_PRD
;
357 Initialize QUERY REQUEST UPIU.
359 @param[in, out] QueryReq The base address of QUERY REQUEST UPIU.
360 @param[in] TaskTag The task tag of request.
361 @param[in] Opcode The opcode of request.
362 @param[in] DescId The descriptor ID of request.
363 @param[in] Index The index of request.
364 @param[in] Selector The selector of request.
365 @param[in] DataSize The data size to be read or written.
366 @param[in] Data The buffer to be read or written.
368 @retval EFI_SUCCESS The initialization succeed.
372 UfsInitQueryRequestUpiu (
373 IN OUT UTP_QUERY_REQ_UPIU
*QueryReq
,
379 IN UINTN DataSize OPTIONAL
,
380 IN UINT8
*Data OPTIONAL
383 ASSERT (QueryReq
!= NULL
);
385 QueryReq
->TransCode
= 0x16;
386 QueryReq
->TaskTag
= TaskTag
;
387 if ((Opcode
== UtpQueryFuncOpcodeRdDesc
) || (Opcode
== UtpQueryFuncOpcodeRdFlag
) || (Opcode
== UtpQueryFuncOpcodeRdAttr
)) {
388 QueryReq
->QueryFunc
= QUERY_FUNC_STD_READ_REQ
;
390 QueryReq
->QueryFunc
= QUERY_FUNC_STD_WRITE_REQ
;
393 if (Opcode
== UtpQueryFuncOpcodeWrAttr
) {
394 UfsFillTsfOfQueryReqUpiu (&QueryReq
->Tsf
, Opcode
, DescId
, Index
, Selector
, 0, *(UINT32
*)Data
);
395 } else if ((Opcode
== UtpQueryFuncOpcodeRdDesc
) || (Opcode
== UtpQueryFuncOpcodeWrDesc
)) {
396 UfsFillTsfOfQueryReqUpiu (&QueryReq
->Tsf
, Opcode
, DescId
, Index
, Selector
, (UINT16
)DataSize
, 0);
398 UfsFillTsfOfQueryReqUpiu (&QueryReq
->Tsf
, Opcode
, DescId
, Index
, Selector
, 0, 0);
401 if (Opcode
== UtpQueryFuncOpcodeWrDesc
) {
402 CopyMem (QueryReq
+ 1, Data
, DataSize
);
404 SwapLittleEndianToBigEndian ((UINT8
*)&DataSize
, sizeof (UINT16
));
405 QueryReq
->DataSegLen
= (UINT16
)DataSize
;
412 Allocate COMMAND/RESPONSE UPIU for filling UTP TRD's command descriptor field.
414 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
415 @param[in] Lun The Lun on which the SCSI command is executed.
416 @param[in] Packet The pointer to the UFS_SCSI_REQUEST_PACKET data structure.
417 @param[in] Trd The pointer to the UTP Transfer Request Descriptor.
418 @param[out] BufferMap A resulting value, if not NULL, to pass to IoMmuUnmap().
420 @retval EFI_SUCCESS The creation succeed.
421 @retval EFI_DEVICE_ERROR The creation failed.
422 @retval EFI_OUT_OF_RESOURCES The memory resource is insufficient.
426 UfsCreateScsiCommandDesc (
427 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
,
429 IN UFS_SCSI_REQUEST_PACKET
*Packet
,
439 UTP_COMMAND_UPIU
*CommandUpiu
;
440 UTP_TR_PRD
*PrdtBase
;
441 UFS_DATA_DIRECTION DataDirection
;
443 EDKII_IOMMU_OPERATION MapOp
;
445 EFI_PHYSICAL_ADDRESS BufferPhyAddr
;
447 ASSERT ((Private
!= NULL
) && (Packet
!= NULL
) && (Trd
!= NULL
));
451 if (Packet
->DataDirection
== UfsDataIn
) {
452 Buffer
= Packet
->InDataBuffer
;
453 Length
= Packet
->InTransferLength
;
454 DataDirection
= UfsDataIn
;
455 MapOp
= EdkiiIoMmuOperationBusMasterWrite
;
457 Buffer
= Packet
->OutDataBuffer
;
458 Length
= Packet
->OutTransferLength
;
459 DataDirection
= UfsDataOut
;
460 MapOp
= EdkiiIoMmuOperationBusMasterRead
;
464 DataDirection
= UfsNoData
;
467 Status
= IoMmuMap (MapOp
, Buffer
, &MapLength
, &BufferPhyAddr
, BufferMap
);
469 if (EFI_ERROR (Status
) || (MapLength
!= Length
)) {
470 DEBUG ((DEBUG_ERROR
, "UfsCreateScsiCommandDesc: Fail to map data buffer.\n"));
471 return EFI_OUT_OF_RESOURCES
;
475 PrdtNumber
= (UINTN
)DivU64x32 ((UINT64
)Length
+ UFS_MAX_DATA_LEN_PER_PRD
- 1, UFS_MAX_DATA_LEN_PER_PRD
);
477 TotalLen
= ROUNDUP8 (sizeof (UTP_COMMAND_UPIU
)) + ROUNDUP8 (sizeof (UTP_RESPONSE_UPIU
)) + PrdtNumber
* sizeof (UTP_TR_PRD
);
478 CommandDesc
= UfsPeimAllocateMem (Private
->Pool
, TotalLen
);
479 if (CommandDesc
== NULL
) {
480 return EFI_OUT_OF_RESOURCES
;
483 CommandUpiu
= (UTP_COMMAND_UPIU
*)CommandDesc
;
484 PrdtBase
= (UTP_TR_PRD
*)(CommandDesc
+ ROUNDUP8 (sizeof (UTP_COMMAND_UPIU
)) + ROUNDUP8 (sizeof (UTP_RESPONSE_UPIU
)));
486 UfsInitCommandUpiu (CommandUpiu
, Lun
, Private
->TaskTag
++, Packet
->Cdb
, Packet
->CdbLength
, DataDirection
, Length
);
487 UfsInitUtpPrdt (PrdtBase
, (VOID
*)(UINTN
)BufferPhyAddr
, Length
);
490 // Fill UTP_TRD associated fields
491 // NOTE: Some UFS host controllers request the Response UPIU and the Physical Region Description Table
492 // *MUST* be located at a 64-bit aligned boundary.
494 Trd
->Int
= UFS_INTERRUPT_COMMAND
;
495 Trd
->Dd
= DataDirection
;
496 Trd
->Ct
= UFS_STORAGE_COMMAND_TYPE
;
497 Trd
->Ocs
= UFS_HC_TRD_OCS_INIT_VALUE
;
498 Trd
->UcdBa
= (UINT32
)RShiftU64 ((UINT64
)(UINTN
)CommandUpiu
, 7);
499 Trd
->UcdBaU
= (UINT32
)RShiftU64 ((UINT64
)(UINTN
)CommandUpiu
, 32);
500 Trd
->RuL
= (UINT16
)DivU64x32 ((UINT64
)ROUNDUP8 (sizeof (UTP_RESPONSE_UPIU
)), sizeof (UINT32
));
501 Trd
->RuO
= (UINT16
)DivU64x32 ((UINT64
)ROUNDUP8 (sizeof (UTP_COMMAND_UPIU
)), sizeof (UINT32
));
502 Trd
->PrdtL
= (UINT16
)PrdtNumber
;
503 Trd
->PrdtO
= (UINT16
)DivU64x32 ((UINT64
)(ROUNDUP8 (sizeof (UTP_COMMAND_UPIU
)) + ROUNDUP8 (sizeof (UTP_RESPONSE_UPIU
))), sizeof (UINT32
));
508 Allocate QUERY REQUEST/QUERY RESPONSE UPIU for filling UTP TRD's command descriptor field.
510 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
511 @param[in] Packet The pointer to the UFS_DEVICE_MANAGEMENT_REQUEST_PACKET data structure.
512 @param[in] Trd The pointer to the UTP Transfer Request Descriptor.
514 @retval EFI_SUCCESS The creation succeed.
515 @retval EFI_DEVICE_ERROR The creation failed.
516 @retval EFI_OUT_OF_RESOURCES The memory resource is insufficient.
517 @retval EFI_INVALID_PARAMETER The parameter passed in is invalid.
521 UfsCreateDMCommandDesc (
522 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
,
523 IN UFS_DEVICE_MANAGEMENT_REQUEST_PACKET
*Packet
,
529 UTP_QUERY_REQ_UPIU
*QueryReqUpiu
;
535 ASSERT ((Private
!= NULL
) && (Packet
!= NULL
) && (Trd
!= NULL
));
537 Opcode
= Packet
->Opcode
;
538 if ((Opcode
> UtpQueryFuncOpcodeTogFlag
) || (Opcode
== UtpQueryFuncOpcodeNop
)) {
539 return EFI_INVALID_PARAMETER
;
542 DataDirection
= Packet
->DataDirection
;
543 if (DataDirection
== UfsDataIn
) {
544 DataSize
= Packet
->InTransferLength
;
545 Data
= Packet
->InDataBuffer
;
546 } else if (DataDirection
== UfsDataOut
) {
547 DataSize
= Packet
->OutTransferLength
;
548 Data
= Packet
->OutDataBuffer
;
554 if ( ((Opcode
!= UtpQueryFuncOpcodeSetFlag
) && (Opcode
!= UtpQueryFuncOpcodeClrFlag
) && (Opcode
!= UtpQueryFuncOpcodeTogFlag
))
555 && ((DataSize
== 0) || (Data
== NULL
)))
557 return EFI_INVALID_PARAMETER
;
560 if ( ((Opcode
== UtpQueryFuncOpcodeSetFlag
) || (Opcode
== UtpQueryFuncOpcodeClrFlag
) || (Opcode
== UtpQueryFuncOpcodeTogFlag
))
561 && ((DataSize
!= 0) || (Data
!= NULL
)))
563 return EFI_INVALID_PARAMETER
;
566 if ((Opcode
== UtpQueryFuncOpcodeWrAttr
) && (DataSize
!= sizeof (UINT32
))) {
567 return EFI_INVALID_PARAMETER
;
570 if ((Opcode
== UtpQueryFuncOpcodeWrDesc
) || (Opcode
== UtpQueryFuncOpcodeRdDesc
)) {
571 TotalLen
= ROUNDUP8 (sizeof (UTP_QUERY_REQ_UPIU
)) + ROUNDUP8 (sizeof (UTP_QUERY_RESP_UPIU
)) + ROUNDUP8 (DataSize
);
573 TotalLen
= ROUNDUP8 (sizeof (UTP_QUERY_REQ_UPIU
)) + ROUNDUP8 (sizeof (UTP_QUERY_RESP_UPIU
));
576 CommandDesc
= UfsPeimAllocateMem (Private
->Pool
, TotalLen
);
577 if (CommandDesc
== NULL
) {
578 return EFI_OUT_OF_RESOURCES
;
582 // Initialize UTP QUERY REQUEST UPIU
584 QueryReqUpiu
= (UTP_QUERY_REQ_UPIU
*)CommandDesc
;
585 UfsInitQueryRequestUpiu (
597 // Fill UTP_TRD associated fields
598 // NOTE: Some UFS host controllers request the Query Response UPIU *MUST* be located at a 64-bit aligned boundary.
600 Trd
->Int
= UFS_INTERRUPT_COMMAND
;
601 Trd
->Dd
= DataDirection
;
602 Trd
->Ct
= UFS_STORAGE_COMMAND_TYPE
;
603 Trd
->Ocs
= UFS_HC_TRD_OCS_INIT_VALUE
;
604 Trd
->UcdBa
= (UINT32
)RShiftU64 ((UINT64
)(UINTN
)QueryReqUpiu
, 7);
605 Trd
->UcdBaU
= (UINT32
)RShiftU64 ((UINT64
)(UINTN
)QueryReqUpiu
, 32);
606 if (Opcode
== UtpQueryFuncOpcodeWrDesc
) {
607 Trd
->RuL
= (UINT16
)DivU64x32 ((UINT64
)ROUNDUP8 (sizeof (UTP_QUERY_RESP_UPIU
)), sizeof (UINT32
));
608 Trd
->RuO
= (UINT16
)DivU64x32 ((UINT64
)ROUNDUP8 (sizeof (UTP_QUERY_REQ_UPIU
)) + ROUNDUP8 (DataSize
), sizeof (UINT32
));
610 Trd
->RuL
= (UINT16
)DivU64x32 ((UINT64
)ROUNDUP8 (sizeof (UTP_QUERY_RESP_UPIU
)) + ROUNDUP8 (DataSize
), sizeof (UINT32
));
611 Trd
->RuO
= (UINT16
)DivU64x32 ((UINT64
)ROUNDUP8 (sizeof (UTP_QUERY_REQ_UPIU
)), sizeof (UINT32
));
618 Allocate NOP IN and NOP OUT UPIU for filling UTP TRD's command descriptor field.
620 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
621 @param[in] Trd The pointer to the UTP Transfer Request Descriptor.
623 @retval EFI_SUCCESS The creation succeed.
624 @retval EFI_DEVICE_ERROR The creation failed.
625 @retval EFI_OUT_OF_RESOURCES The memory resource is insufficient.
629 UfsCreateNopCommandDesc (
630 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
,
636 UTP_NOP_OUT_UPIU
*NopOutUpiu
;
638 ASSERT ((Private
!= NULL
) && (Trd
!= NULL
));
640 TotalLen
= ROUNDUP8 (sizeof (UTP_NOP_OUT_UPIU
)) + ROUNDUP8 (sizeof (UTP_NOP_IN_UPIU
));
641 CommandDesc
= UfsPeimAllocateMem (Private
->Pool
, TotalLen
);
642 if (CommandDesc
== NULL
) {
643 return EFI_OUT_OF_RESOURCES
;
646 NopOutUpiu
= (UTP_NOP_OUT_UPIU
*)CommandDesc
;
648 NopOutUpiu
->TaskTag
= Private
->TaskTag
++;
651 // Fill UTP_TRD associated fields
652 // NOTE: Some UFS host controllers request the Nop Out UPIU *MUST* be located at a 64-bit aligned boundary.
654 Trd
->Int
= UFS_INTERRUPT_COMMAND
;
656 Trd
->Ct
= UFS_STORAGE_COMMAND_TYPE
;
657 Trd
->Ocs
= UFS_HC_TRD_OCS_INIT_VALUE
;
658 Trd
->UcdBa
= (UINT32
)RShiftU64 ((UINT64
)(UINTN
)NopOutUpiu
, 7);
659 Trd
->UcdBaU
= (UINT32
)RShiftU64 ((UINT64
)(UINTN
)NopOutUpiu
, 32);
660 Trd
->RuL
= (UINT16
)DivU64x32 ((UINT64
)ROUNDUP8 (sizeof (UTP_NOP_IN_UPIU
)), sizeof (UINT32
));
661 Trd
->RuO
= (UINT16
)DivU64x32 ((UINT64
)ROUNDUP8 (sizeof (UTP_NOP_OUT_UPIU
)), sizeof (UINT32
));
667 Find out available slot in transfer list of a UFS device.
669 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
670 @param[out] Slot The available slot.
672 @retval EFI_SUCCESS The available slot was found successfully.
676 UfsFindAvailableSlotInTrl (
677 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
,
681 ASSERT ((Private
!= NULL
) && (Slot
!= NULL
));
684 // The simplest algo to always use slot 0.
685 // TODO: enhance it to support async transfer with multiple slot.
693 Start specified slot in transfer list of a UFS device.
695 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
696 @param[in] Slot The slot to be started.
701 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
,
709 UfsHcBase
= Private
->UfsHcBase
;
711 Address
= UfsHcBase
+ UFS_HC_UTRLRSR_OFFSET
;
712 Data
= MmioRead32 (Address
);
713 if ((Data
& UFS_HC_UTRLRSR
) != UFS_HC_UTRLRSR
) {
714 MmioWrite32 (Address
, UFS_HC_UTRLRSR
);
717 Address
= UfsHcBase
+ UFS_HC_UTRLDBR_OFFSET
;
718 MmioWrite32 (Address
, BIT0
<< Slot
);
722 Stop specified slot in transfer list of a UFS device.
724 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
725 @param[in] Slot The slot to be stop.
730 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
,
738 UfsHcBase
= Private
->UfsHcBase
;
740 Address
= UfsHcBase
+ UFS_HC_UTRLDBR_OFFSET
;
741 Data
= MmioRead32 (Address
);
742 if ((Data
& (BIT0
<< Slot
)) != 0) {
743 Address
= UfsHcBase
+ UFS_HC_UTRLCLR_OFFSET
;
744 Data
= MmioRead32 (Address
);
745 MmioWrite32 (Address
, (Data
& ~(BIT0
<< Slot
)));
750 Read or write specified device descriptor of a UFS device.
752 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
753 @param[in] Read The boolean variable to show r/w direction.
754 @param[in] DescId The ID of device descriptor.
755 @param[in] Index The Index of device descriptor.
756 @param[in] Selector The Selector of device descriptor.
757 @param[in, out] Descriptor The buffer of device descriptor to be read or written.
758 @param[in] DescSize The size of device descriptor buffer.
760 @retval EFI_SUCCESS The device descriptor was read/written successfully.
761 @retval EFI_DEVICE_ERROR A device error occurred while attempting to r/w the device descriptor.
762 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of r/w the device descriptor.
767 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
,
772 IN OUT VOID
*Descriptor
,
777 UFS_DEVICE_MANAGEMENT_REQUEST_PACKET Packet
;
781 UTP_QUERY_RESP_UPIU
*QueryResp
;
784 UINT16 ReturnDataSize
;
786 ZeroMem (&Packet
, sizeof (UFS_DEVICE_MANAGEMENT_REQUEST_PACKET
));
789 Packet
.DataDirection
= UfsDataIn
;
790 Packet
.InDataBuffer
= Descriptor
;
791 Packet
.InTransferLength
= DescSize
;
792 Packet
.Opcode
= UtpQueryFuncOpcodeRdDesc
;
794 Packet
.DataDirection
= UfsDataOut
;
795 Packet
.OutDataBuffer
= Descriptor
;
796 Packet
.OutTransferLength
= DescSize
;
797 Packet
.Opcode
= UtpQueryFuncOpcodeWrDesc
;
800 Packet
.DescId
= DescId
;
801 Packet
.Index
= Index
;
802 Packet
.Selector
= Selector
;
803 Packet
.Timeout
= UFS_TIMEOUT
;
806 // Find out which slot of transfer request list is available.
808 Status
= UfsFindAvailableSlotInTrl (Private
, &Slot
);
809 if (EFI_ERROR (Status
)) {
813 Trd
= ((UTP_TRD
*)Private
->UtpTrlBase
) + Slot
;
815 // Fill transfer request descriptor to this slot.
817 Status
= UfsCreateDMCommandDesc (Private
, &Packet
, Trd
);
818 if (EFI_ERROR (Status
)) {
823 // Check the transfer request result.
825 CmdDescBase
= (UINT8
*)(UINTN
)(LShiftU64 ((UINT64
)Trd
->UcdBaU
, 32) | LShiftU64 ((UINT64
)Trd
->UcdBa
, 7));
826 QueryResp
= (UTP_QUERY_RESP_UPIU
*)(CmdDescBase
+ Trd
->RuO
* sizeof (UINT32
));
827 CmdDescSize
= Trd
->RuO
* sizeof (UINT32
) + Trd
->RuL
* sizeof (UINT32
);
830 // Start to execute the transfer request.
832 UfsStartExecCmd (Private
, Slot
);
835 // Wait for the completion of the transfer request.
837 Address
= Private
->UfsHcBase
+ UFS_HC_UTRLDBR_OFFSET
;
838 Status
= UfsWaitMemSet (Address
, BIT0
<< Slot
, 0, Packet
.Timeout
);
839 if (EFI_ERROR (Status
)) {
843 if (QueryResp
->QueryResp
!= 0) {
844 DumpQueryResponseResult (QueryResp
->QueryResp
);
845 Status
= EFI_DEVICE_ERROR
;
850 ReturnDataSize
= QueryResp
->Tsf
.Length
;
851 SwapLittleEndianToBigEndian ((UINT8
*)&ReturnDataSize
, sizeof (UINT16
));
855 // Make sure the hardware device does not return more data than expected.
857 if (ReturnDataSize
> Packet
.InTransferLength
) {
858 Status
= EFI_DEVICE_ERROR
;
862 CopyMem (Packet
.InDataBuffer
, (QueryResp
+ 1), ReturnDataSize
);
863 Packet
.InTransferLength
= ReturnDataSize
;
865 Packet
.OutTransferLength
= ReturnDataSize
;
868 Status
= EFI_DEVICE_ERROR
;
872 UfsStopExecCmd (Private
, Slot
);
873 UfsPeimFreeMem (Private
->Pool
, CmdDescBase
, CmdDescSize
);
879 Read or write specified flag of a UFS device.
881 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
882 @param[in] Read The boolean variable to show r/w direction.
883 @param[in] FlagId The ID of flag to be read or written.
884 @param[in, out] Value The value to set or clear flag.
886 @retval EFI_SUCCESS The flag was read/written successfully.
887 @retval EFI_DEVICE_ERROR A device error occurred while attempting to r/w the flag.
888 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of r/w the flag.
893 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
,
900 UFS_DEVICE_MANAGEMENT_REQUEST_PACKET Packet
;
904 UTP_QUERY_RESP_UPIU
*QueryResp
;
909 return EFI_INVALID_PARAMETER
;
912 ZeroMem (&Packet
, sizeof (UFS_DEVICE_MANAGEMENT_REQUEST_PACKET
));
915 ASSERT (Value
!= NULL
);
916 Packet
.DataDirection
= UfsDataIn
;
917 Packet
.Opcode
= UtpQueryFuncOpcodeRdFlag
;
919 Packet
.DataDirection
= UfsDataOut
;
921 Packet
.Opcode
= UtpQueryFuncOpcodeSetFlag
;
922 } else if (*Value
== 0) {
923 Packet
.Opcode
= UtpQueryFuncOpcodeClrFlag
;
925 return EFI_INVALID_PARAMETER
;
929 Packet
.DescId
= FlagId
;
932 Packet
.Timeout
= UFS_TIMEOUT
;
935 // Find out which slot of transfer request list is available.
937 Status
= UfsFindAvailableSlotInTrl (Private
, &Slot
);
938 if (EFI_ERROR (Status
)) {
943 // Fill transfer request descriptor to this slot.
945 Trd
= ((UTP_TRD
*)Private
->UtpTrlBase
) + Slot
;
946 Status
= UfsCreateDMCommandDesc (Private
, &Packet
, Trd
);
947 if (EFI_ERROR (Status
)) {
952 // Check the transfer request result.
954 CmdDescBase
= (UINT8
*)(UINTN
)(LShiftU64 ((UINT64
)Trd
->UcdBaU
, 32) | LShiftU64 ((UINT64
)Trd
->UcdBa
, 7));
955 QueryResp
= (UTP_QUERY_RESP_UPIU
*)(CmdDescBase
+ Trd
->RuO
* sizeof (UINT32
));
956 CmdDescSize
= Trd
->RuO
* sizeof (UINT32
) + Trd
->RuL
* sizeof (UINT32
);
959 // Start to execute the transfer request.
961 UfsStartExecCmd (Private
, Slot
);
964 // Wait for the completion of the transfer request.
966 Address
= Private
->UfsHcBase
+ UFS_HC_UTRLDBR_OFFSET
;
967 Status
= UfsWaitMemSet (Address
, BIT0
<< Slot
, 0, Packet
.Timeout
);
968 if (EFI_ERROR (Status
)) {
972 if (QueryResp
->QueryResp
!= 0) {
973 DumpQueryResponseResult (QueryResp
->QueryResp
);
974 Status
= EFI_DEVICE_ERROR
;
980 // The 'FLAG VALUE' field is at byte offset 3 of QueryResp->Tsf.Value
982 *Value
= *((UINT8
*)&(QueryResp
->Tsf
.Value
) + 3);
984 Status
= EFI_DEVICE_ERROR
;
988 UfsStopExecCmd (Private
, Slot
);
989 UfsPeimFreeMem (Private
->Pool
, CmdDescBase
, CmdDescSize
);
995 Set specified flag to 1 on a UFS device.
997 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
998 @param[in] FlagId The ID of flag to be set.
1000 @retval EFI_SUCCESS The flag was set successfully.
1001 @retval EFI_DEVICE_ERROR A device error occurred while attempting to set the flag.
1002 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of setting the flag.
1007 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
,
1015 Status
= UfsRwFlags (Private
, FALSE
, FlagId
, &Value
);
1021 Sends NOP IN cmd to a UFS device for initialization process request.
1022 For more details, please refer to UFS 2.0 spec Figure 13.3.
1024 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
1026 @retval EFI_SUCCESS The NOP IN command was sent by the host. The NOP OUT response was
1027 received successfully.
1028 @retval EFI_DEVICE_ERROR A device error occurred while attempting to execute NOP IN command.
1029 @retval EFI_OUT_OF_RESOURCES The resource for transfer is not available.
1030 @retval EFI_TIMEOUT A timeout occurred while waiting for the NOP IN command to execute.
1035 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
1041 UTP_NOP_IN_UPIU
*NopInUpiu
;
1047 // Find out which slot of transfer request list is available.
1049 Status
= UfsFindAvailableSlotInTrl (Private
, &Slot
);
1050 if (EFI_ERROR (Status
)) {
1054 Trd
= ((UTP_TRD
*)Private
->UtpTrlBase
) + Slot
;
1055 Status
= UfsCreateNopCommandDesc (Private
, Trd
);
1056 if (EFI_ERROR (Status
)) {
1061 // Check the transfer request result.
1063 CmdDescBase
= (UINT8
*)(UINTN
)(LShiftU64 ((UINT64
)Trd
->UcdBaU
, 32) | LShiftU64 ((UINT64
)Trd
->UcdBa
, 7));
1064 NopInUpiu
= (UTP_NOP_IN_UPIU
*)(CmdDescBase
+ Trd
->RuO
* sizeof (UINT32
));
1065 CmdDescSize
= Trd
->RuO
* sizeof (UINT32
) + Trd
->RuL
* sizeof (UINT32
);
1068 // Start to execute the transfer request.
1070 UfsStartExecCmd (Private
, Slot
);
1073 // Wait for the completion of the transfer request.
1075 Address
= Private
->UfsHcBase
+ UFS_HC_UTRLDBR_OFFSET
;
1076 Status
= UfsWaitMemSet (Address
, BIT0
<< Slot
, 0, UFS_TIMEOUT
);
1077 if (EFI_ERROR (Status
)) {
1081 if (NopInUpiu
->Resp
!= 0) {
1082 Status
= EFI_DEVICE_ERROR
;
1084 Status
= EFI_SUCCESS
;
1088 UfsStopExecCmd (Private
, Slot
);
1089 UfsPeimFreeMem (Private
->Pool
, CmdDescBase
, CmdDescSize
);
1095 Sends a UFS-supported SCSI Request Packet to a UFS device that is attached to the UFS host controller.
1097 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
1098 @param[in] Lun The LUN of the UFS device to send the SCSI Request Packet.
1099 @param[in, out] Packet A pointer to the SCSI Request Packet to send to a specified Lun of the
1102 @retval EFI_SUCCESS The SCSI Request Packet was sent by the host. For bi-directional
1103 commands, InTransferLength bytes were transferred from
1104 InDataBuffer. For write and bi-directional commands,
1105 OutTransferLength bytes were transferred by
1107 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SCSI Request
1109 @retval EFI_OUT_OF_RESOURCES The resource for transfer is not available.
1110 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
1115 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
,
1117 IN OUT UFS_SCSI_REQUEST_PACKET
*Packet
1126 UTP_RESPONSE_UPIU
*Response
;
1127 UINT16 SenseDataLen
;
1128 UINT32 ResTranCount
;
1129 VOID
*PacketBufferMap
;
1132 // Find out which slot of transfer request list is available.
1134 Status
= UfsFindAvailableSlotInTrl (Private
, &Slot
);
1135 if (EFI_ERROR (Status
)) {
1139 Trd
= ((UTP_TRD
*)Private
->UtpTrlBase
) + Slot
;
1140 PacketBufferMap
= NULL
;
1143 // Fill transfer request descriptor to this slot.
1145 Status
= UfsCreateScsiCommandDesc (Private
, Lun
, Packet
, Trd
, &PacketBufferMap
);
1146 if (EFI_ERROR (Status
)) {
1150 CmdDescBase
= (UINT8
*)(UINTN
)(LShiftU64 ((UINT64
)Trd
->UcdBaU
, 32) | LShiftU64 ((UINT64
)Trd
->UcdBa
, 7));
1151 CmdDescSize
= Trd
->PrdtO
* sizeof (UINT32
) + Trd
->PrdtL
* sizeof (UTP_TR_PRD
);
1154 // Start to execute the transfer request.
1156 UfsStartExecCmd (Private
, Slot
);
1159 // Wait for the completion of the transfer request.
1161 Address
= Private
->UfsHcBase
+ UFS_HC_UTRLDBR_OFFSET
;
1162 Status
= UfsWaitMemSet (Address
, BIT0
<< Slot
, 0, Packet
->Timeout
);
1163 if (EFI_ERROR (Status
)) {
1168 // Get sense data if exists
1170 Response
= (UTP_RESPONSE_UPIU
*)(CmdDescBase
+ Trd
->RuO
* sizeof (UINT32
));
1171 SenseDataLen
= Response
->SenseDataLen
;
1172 SwapLittleEndianToBigEndian ((UINT8
*)&SenseDataLen
, sizeof (UINT16
));
1174 if ((Packet
->SenseDataLength
!= 0) && (Packet
->SenseData
!= NULL
)) {
1176 // Make sure the hardware device does not return more data than expected.
1178 if (SenseDataLen
<= Packet
->SenseDataLength
) {
1179 CopyMem (Packet
->SenseData
, Response
->SenseData
, SenseDataLen
);
1180 Packet
->SenseDataLength
= (UINT8
)SenseDataLen
;
1182 Packet
->SenseDataLength
= 0;
1187 // Check the transfer request result.
1189 if (Response
->Response
!= 0) {
1190 DEBUG ((DEBUG_ERROR
, "UfsExecScsiCmds() fails with Target Failure\n"));
1191 Status
= EFI_DEVICE_ERROR
;
1195 if (Trd
->Ocs
== 0) {
1196 if (Packet
->DataDirection
== UfsDataIn
) {
1197 if ((Response
->Flags
& BIT5
) == BIT5
) {
1198 ResTranCount
= Response
->ResTranCount
;
1199 SwapLittleEndianToBigEndian ((UINT8
*)&ResTranCount
, sizeof (UINT32
));
1200 Packet
->InTransferLength
-= ResTranCount
;
1202 } else if (Packet
->DataDirection
== UfsDataOut
) {
1203 if ((Response
->Flags
& BIT5
) == BIT5
) {
1204 ResTranCount
= Response
->ResTranCount
;
1205 SwapLittleEndianToBigEndian ((UINT8
*)&ResTranCount
, sizeof (UINT32
));
1206 Packet
->OutTransferLength
-= ResTranCount
;
1210 Status
= EFI_DEVICE_ERROR
;
1214 if (PacketBufferMap
!= NULL
) {
1215 IoMmuUnmap (PacketBufferMap
);
1218 UfsStopExecCmd (Private
, Slot
);
1219 UfsPeimFreeMem (Private
->Pool
, CmdDescBase
, CmdDescSize
);
1225 Sent UIC DME_LINKSTARTUP command to start the link startup procedure.
1227 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
1228 @param[in] UicOpcode The opcode of the UIC command.
1229 @param[in] Arg1 The value for 1st argument of the UIC command.
1230 @param[in] Arg2 The value for 2nd argument of the UIC command.
1231 @param[in] Arg3 The value for 3rd argument of the UIC command.
1233 @return EFI_SUCCESS Successfully execute this UIC command and detect attached UFS device.
1234 @return EFI_DEVICE_ERROR Fail to execute this UIC command and detect attached UFS device.
1235 @return EFI_NOT_FOUND The presence of the UFS device isn't detected.
1239 UfsExecUicCommands (
1240 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
,
1252 UfsHcBase
= Private
->UfsHcBase
;
1253 Address
= UfsHcBase
+ UFS_HC_IS_OFFSET
;
1254 Data
= MmioRead32 (Address
);
1255 if ((Data
& UFS_HC_IS_UCCS
) == UFS_HC_IS_UCCS
) {
1257 // Clear IS.BIT10 UIC Command Completion Status (UCCS) at first.
1259 MmioWrite32 (Address
, Data
);
1263 // When programming UIC command registers, host software shall set the register UICCMD
1264 // only after all the UIC command argument registers (UICCMDARG1, UICCMDARG2 and UICCMDARG3)
1267 Address
= UfsHcBase
+ UFS_HC_UCMD_ARG1_OFFSET
;
1268 MmioWrite32 (Address
, Arg1
);
1270 Address
= UfsHcBase
+ UFS_HC_UCMD_ARG2_OFFSET
;
1271 MmioWrite32 (Address
, Arg2
);
1273 Address
= UfsHcBase
+ UFS_HC_UCMD_ARG3_OFFSET
;
1274 MmioWrite32 (Address
, Arg3
);
1277 // Host software shall only set the UICCMD if HCS.UCRDY is set to 1.
1279 Address
= Private
->UfsHcBase
+ UFS_HC_STATUS_OFFSET
;
1280 Status
= UfsWaitMemSet (Address
, UFS_HC_HCS_UCRDY
, UFS_HC_HCS_UCRDY
, UFS_TIMEOUT
);
1281 if (EFI_ERROR (Status
)) {
1285 Address
= UfsHcBase
+ UFS_HC_UIC_CMD_OFFSET
;
1286 MmioWrite32 (Address
, (UINT32
)UicOpcode
);
1289 // UFS 2.0 spec section 5.3.1 Offset:0x20 IS.Bit10 UIC Command Completion Status (UCCS)
1290 // This bit is set to '1' by the host controller upon completion of a UIC command.
1292 Address
= UfsHcBase
+ UFS_HC_IS_OFFSET
;
1293 Data
= MmioRead32 (Address
);
1294 Status
= UfsWaitMemSet (Address
, UFS_HC_IS_UCCS
, UFS_HC_IS_UCCS
, UFS_TIMEOUT
);
1295 if (EFI_ERROR (Status
)) {
1299 if (UicOpcode
!= UfsUicDmeReset
) {
1300 Address
= UfsHcBase
+ UFS_HC_UCMD_ARG2_OFFSET
;
1301 Data
= MmioRead32 (Address
);
1302 if ((Data
& 0xFF) != 0) {
1303 DEBUG_CODE_BEGIN ();
1304 DumpUicCmdExecResult (UicOpcode
, (UINT8
)(Data
& 0xFF));
1306 return EFI_DEVICE_ERROR
;
1311 // Check value of HCS.DP and make sure that there is a device attached to the Link.
1313 Address
= UfsHcBase
+ UFS_HC_STATUS_OFFSET
;
1314 Data
= MmioRead32 (Address
);
1315 if ((Data
& UFS_HC_HCS_DP
) == 0) {
1316 Address
= UfsHcBase
+ UFS_HC_IS_OFFSET
;
1317 Status
= UfsWaitMemSet (Address
, UFS_HC_IS_ULSS
, UFS_HC_IS_ULSS
, UFS_TIMEOUT
);
1318 if (EFI_ERROR (Status
)) {
1319 return EFI_DEVICE_ERROR
;
1322 return EFI_NOT_FOUND
;
1325 DEBUG ((DEBUG_INFO
, "UfsblockioPei: found a attached UFS device\n"));
1331 Enable the UFS host controller for accessing.
1333 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
1335 @retval EFI_SUCCESS The UFS host controller enabling was executed successfully.
1336 @retval EFI_DEVICE_ERROR A device error occurred while enabling the UFS host controller.
1340 UfsEnableHostController (
1341 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
1349 // UFS 2.0 spec section 7.1.1 - Host Controller Initialization
1351 // Reinitialize the UFS host controller if HCE bit of HC register is set.
1353 Address
= Private
->UfsHcBase
+ UFS_HC_ENABLE_OFFSET
;
1354 Data
= MmioRead32 (Address
);
1355 if ((Data
& UFS_HC_HCE_EN
) == UFS_HC_HCE_EN
) {
1357 // Write a 0 to the HCE register at first to disable the host controller.
1359 MmioWrite32 (Address
, 0);
1361 // Wait until HCE is read as '0' before continuing.
1363 Status
= UfsWaitMemSet (Address
, UFS_HC_HCE_EN
, 0, UFS_TIMEOUT
);
1364 if (EFI_ERROR (Status
)) {
1365 return EFI_DEVICE_ERROR
;
1370 // Write a 1 to the HCE register to enable the UFS host controller.
1372 MmioWrite32 (Address
, UFS_HC_HCE_EN
);
1374 // Wait until HCE is read as '1' before continuing.
1376 Status
= UfsWaitMemSet (Address
, UFS_HC_HCE_EN
, UFS_HC_HCE_EN
, UFS_TIMEOUT
);
1377 if (EFI_ERROR (Status
)) {
1378 return EFI_DEVICE_ERROR
;
1385 Detect if a UFS device attached.
1387 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
1389 @retval EFI_SUCCESS The UFS device detection was executed successfully.
1390 @retval EFI_NOT_FOUND Not found a UFS device attached.
1391 @retval EFI_DEVICE_ERROR A device error occurred while detecting the UFS device.
1395 UfsDeviceDetection (
1396 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
1403 // Start UFS device detection.
1404 // Try up to 3 times for establishing data link with device.
1406 for (Retry
= 0; Retry
< 3; Retry
++) {
1407 Status
= UfsExecUicCommands (Private
, UfsUicDmeLinkStartup
, 0, 0, 0);
1408 if (!EFI_ERROR (Status
)) {
1412 if (Status
== EFI_NOT_FOUND
) {
1416 return EFI_DEVICE_ERROR
;
1420 return EFI_NOT_FOUND
;
1427 Initialize UFS task management request list related h/w context.
1429 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
1431 @retval EFI_SUCCESS The UFS task management list was initialzed successfully.
1432 @retval EFI_DEVICE_ERROR The initialization fails.
1436 UfsInitTaskManagementRequestList (
1437 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
1444 EFI_PHYSICAL_ADDRESS CmdDescPhyAddr
;
1445 VOID
*CmdDescMapping
;
1449 // Initial h/w and s/w context for future operations.
1451 Address
= Private
->UfsHcBase
+ UFS_HC_CAP_OFFSET
;
1452 Data
= MmioRead32 (Address
);
1453 Private
->Capabilities
= Data
;
1456 // Allocate and initialize UTP Task Management Request List.
1458 Nutmrs
= (UINT8
)(RShiftU64 ((Private
->Capabilities
& UFS_HC_CAP_NUTMRS
), 16) + 1);
1459 Status
= IoMmuAllocateBuffer (
1460 EFI_SIZE_TO_PAGES (Nutmrs
* sizeof (UTP_TMRD
)),
1465 if (EFI_ERROR (Status
)) {
1466 return EFI_DEVICE_ERROR
;
1469 ZeroMem (CmdDescHost
, EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (Nutmrs
* sizeof (UTP_TMRD
))));
1472 // Program the UTP Task Management Request List Base Address and UTP Task Management
1473 // Request List Base Address with a 64-bit address allocated at step 6.
1475 Address
= Private
->UfsHcBase
+ UFS_HC_UTMRLBA_OFFSET
;
1476 MmioWrite32 (Address
, (UINT32
)(UINTN
)CmdDescPhyAddr
);
1477 Address
= Private
->UfsHcBase
+ UFS_HC_UTMRLBAU_OFFSET
;
1478 MmioWrite32 (Address
, (UINT32
)RShiftU64 ((UINT64
)CmdDescPhyAddr
, 32));
1479 Private
->UtpTmrlBase
= (VOID
*)(UINTN
)CmdDescHost
;
1480 Private
->Nutmrs
= Nutmrs
;
1481 Private
->TmrlMapping
= CmdDescMapping
;
1484 // Enable the UTP Task Management Request List by setting the UTP Task Management
1485 // Request List RunStop Register (UTMRLRSR) to '1'.
1487 Address
= Private
->UfsHcBase
+ UFS_HC_UTMRLRSR_OFFSET
;
1488 MmioWrite32 (Address
, UFS_HC_UTMRLRSR
);
1494 Initialize UFS transfer request list related h/w context.
1496 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
1498 @retval EFI_SUCCESS The UFS transfer list was initialzed successfully.
1499 @retval EFI_DEVICE_ERROR The initialization fails.
1503 UfsInitTransferRequestList (
1504 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
1511 EFI_PHYSICAL_ADDRESS CmdDescPhyAddr
;
1512 VOID
*CmdDescMapping
;
1516 // Initial h/w and s/w context for future operations.
1518 Address
= Private
->UfsHcBase
+ UFS_HC_CAP_OFFSET
;
1519 Data
= MmioRead32 (Address
);
1520 Private
->Capabilities
= Data
;
1523 // Allocate and initialize UTP Transfer Request List.
1525 Nutrs
= (UINT8
)((Private
->Capabilities
& UFS_HC_CAP_NUTRS
) + 1);
1526 Status
= IoMmuAllocateBuffer (
1527 EFI_SIZE_TO_PAGES (Nutrs
* sizeof (UTP_TRD
)),
1532 if (EFI_ERROR (Status
)) {
1533 return EFI_DEVICE_ERROR
;
1536 ZeroMem (CmdDescHost
, EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (Nutrs
* sizeof (UTP_TRD
))));
1539 // Program the UTP Transfer Request List Base Address and UTP Transfer Request List
1540 // Base Address with a 64-bit address allocated at step 8.
1542 Address
= Private
->UfsHcBase
+ UFS_HC_UTRLBA_OFFSET
;
1543 MmioWrite32 (Address
, (UINT32
)(UINTN
)CmdDescPhyAddr
);
1544 Address
= Private
->UfsHcBase
+ UFS_HC_UTRLBAU_OFFSET
;
1545 MmioWrite32 (Address
, (UINT32
)RShiftU64 ((UINT64
)CmdDescPhyAddr
, 32));
1546 Private
->UtpTrlBase
= (VOID
*)(UINTN
)CmdDescHost
;
1547 Private
->Nutrs
= Nutrs
;
1548 Private
->TrlMapping
= CmdDescMapping
;
1551 // Enable the UTP Transfer Request List by setting the UTP Transfer Request List
1552 // RunStop Register (UTRLRSR) to '1'.
1554 Address
= Private
->UfsHcBase
+ UFS_HC_UTRLRSR_OFFSET
;
1555 MmioWrite32 (Address
, UFS_HC_UTRLRSR
);
1561 Initialize the UFS host controller.
1563 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
1565 @retval EFI_SUCCESS The Ufs Host Controller is initialized successfully.
1566 @retval Others A device error occurred while initializing the controller.
1571 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
1576 Status
= UfsEnableHostController (Private
);
1577 if (EFI_ERROR (Status
)) {
1578 DEBUG ((DEBUG_ERROR
, "UfsDevicePei: Enable Host Controller Fails, Status = %r\n", Status
));
1582 Status
= UfsDeviceDetection (Private
);
1583 if (EFI_ERROR (Status
)) {
1584 DEBUG ((DEBUG_ERROR
, "UfsDevicePei: Device Detection Fails, Status = %r\n", Status
));
1588 Status
= UfsInitTaskManagementRequestList (Private
);
1589 if (EFI_ERROR (Status
)) {
1590 DEBUG ((DEBUG_ERROR
, "UfsDevicePei: Task management list initialization Fails, Status = %r\n", Status
));
1594 Status
= UfsInitTransferRequestList (Private
);
1595 if (EFI_ERROR (Status
)) {
1596 DEBUG ((DEBUG_ERROR
, "UfsDevicePei: Transfer list initialization Fails, Status = %r\n", Status
));
1598 if (Private
->TmrlMapping
!= NULL
) {
1600 EFI_SIZE_TO_PAGES (Private
->Nutmrs
* sizeof (UTP_TMRD
)),
1601 Private
->UtpTmrlBase
,
1602 Private
->TmrlMapping
1604 Private
->TmrlMapping
= NULL
;
1610 DEBUG ((DEBUG_INFO
, "UfsDevicePei Finished\n"));
1615 Stop the UFS host controller.
1617 @param[in] Private The pointer to the UFS_PEIM_HC_PRIVATE_DATA data structure.
1619 @retval EFI_SUCCESS The Ufs Host Controller is stopped successfully.
1620 @retval Others A device error occurred while stopping the controller.
1625 IN UFS_PEIM_HC_PRIVATE_DATA
*Private
1633 // Enable the UTP Task Management Request List by setting the UTP Task Management
1634 // Request List RunStop Register (UTMRLRSR) to '1'.
1636 Address
= Private
->UfsHcBase
+ UFS_HC_UTMRLRSR_OFFSET
;
1637 MmioWrite32 (Address
, 0);
1640 // Enable the UTP Transfer Request List by setting the UTP Transfer Request List
1641 // RunStop Register (UTRLRSR) to '1'.
1643 Address
= Private
->UfsHcBase
+ UFS_HC_UTRLRSR_OFFSET
;
1644 MmioWrite32 (Address
, 0);
1647 // Write a 0 to the HCE register in order to disable the host controller.
1649 Address
= Private
->UfsHcBase
+ UFS_HC_ENABLE_OFFSET
;
1650 Data
= MmioRead32 (Address
);
1651 ASSERT ((Data
& UFS_HC_HCE_EN
) == UFS_HC_HCE_EN
);
1652 MmioWrite32 (Address
, 0);
1655 // Wait until HCE is read as '0' before continuing.
1657 Status
= UfsWaitMemSet (Address
, UFS_HC_HCE_EN
, 0, UFS_TIMEOUT
);
1658 if (EFI_ERROR (Status
)) {
1659 return EFI_DEVICE_ERROR
;
1662 DEBUG ((DEBUG_INFO
, "UfsDevicePei: Stop the UFS Host Controller\n"));