2 UfsPassThruDxe driver is used to produce EFI_EXT_SCSI_PASS_THRU protocol interface
3 for upper layer application to execute UFS-supported SCSI cmds.
5 Copyright (c) 2014 - 2017, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php.
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 #include "UfsPassThru.h"
19 Read 32bits data from specified UFS MMIO register.
21 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
22 @param[in] Offset The offset within the UFS Host Controller MMIO space to start
24 @param[out] Value The data buffer to store.
26 @retval EFI_TIMEOUT The operation is time out.
27 @retval EFI_SUCCESS The operation succeeds.
28 @retval Others The operation fails.
33 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
38 EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*UfsHc
;
41 UfsHc
= Private
->UfsHostController
;
43 Status
= UfsHc
->Read (UfsHc
, EfiUfsHcWidthUint32
, Offset
, 1, Value
);
49 Write 32bits data to specified UFS MMIO register.
51 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
52 @param[in] Offset The offset within the UFS Host Controller MMIO space to start
54 @param[in] Value The data to write.
56 @retval EFI_TIMEOUT The operation is time out.
57 @retval EFI_SUCCESS The operation succeeds.
58 @retval Others The operation fails.
63 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
68 EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*UfsHc
;
71 UfsHc
= Private
->UfsHostController
;
73 Status
= UfsHc
->Write (UfsHc
, EfiUfsHcWidthUint32
, Offset
, 1, &Value
);
79 Wait for the value of the specified system memory set to the test value.
81 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
82 @param[in] Offset The offset within the UFS Host Controller MMIO space to start
84 @param[in] MaskValue The mask value of memory.
85 @param[in] TestValue The test value of memory.
86 @param[in] Timeout The time out value for wait memory set, uses 100ns as a unit.
88 @retval EFI_TIMEOUT The system memory setting is time out.
89 @retval EFI_SUCCESS The system memory is correct set.
90 @retval Others The operation fails.
95 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
104 BOOLEAN InfiniteWait
;
110 InfiniteWait
= FALSE
;
113 Delay
= DivU64x32 (Timeout
, 10) + 1;
117 // Access PCI MMIO space to see if the value is the tested one.
119 Status
= UfsMmioRead32 (Private
, Offset
, &Value
);
120 if (EFI_ERROR (Status
)) {
126 if (Value
== TestValue
) {
131 // Stall for 1 microseconds.
133 MicroSecondDelay (1);
137 } while (InfiniteWait
|| (Delay
> 0));
143 Dump UIC command execution result for debugging.
145 @param[in] UicOpcode The executed UIC opcode.
146 @param[in] Result The result to be parsed.
150 DumpUicCmdExecResult (
155 if (UicOpcode
<= UfsUicDmePeerSet
) {
160 DEBUG ((DEBUG_VERBOSE
, "UIC configuration command fails - INVALID_MIB_ATTRIBUTE\n"));
163 DEBUG ((DEBUG_VERBOSE
, "UIC configuration command fails - INVALID_MIB_ATTRIBUTE_VALUE\n"));
166 DEBUG ((DEBUG_VERBOSE
, "UIC configuration command fails - READ_ONLY_MIB_ATTRIBUTE\n"));
169 DEBUG ((DEBUG_VERBOSE
, "UIC configuration command fails - WRITE_ONLY_MIB_ATTRIBUTE\n"));
172 DEBUG ((DEBUG_VERBOSE
, "UIC configuration command fails - BAD_INDEX\n"));
175 DEBUG ((DEBUG_VERBOSE
, "UIC configuration command fails - LOCKED_MIB_ATTRIBUTE\n"));
178 DEBUG ((DEBUG_VERBOSE
, "UIC configuration command fails - BAD_TEST_FEATURE_INDEX\n"));
181 DEBUG ((DEBUG_VERBOSE
, "UIC configuration command fails - PEER_COMMUNICATION_FAILURE\n"));
184 DEBUG ((DEBUG_VERBOSE
, "UIC configuration command fails - BUSY\n"));
187 DEBUG ((DEBUG_VERBOSE
, "UIC configuration command fails - DME_FAILURE\n"));
198 DEBUG ((DEBUG_VERBOSE
, "UIC control command fails - FAILURE\n"));
208 Dump QUERY RESPONSE UPIU result for debugging.
210 @param[in] Result The result to be parsed.
214 DumpQueryResponseResult (
220 DEBUG ((DEBUG_VERBOSE
, "Query Response with Parameter Not Readable\n"));
223 DEBUG ((DEBUG_VERBOSE
, "Query Response with Parameter Not Writeable\n"));
226 DEBUG ((DEBUG_VERBOSE
, "Query Response with Parameter Already Written\n"));
229 DEBUG ((DEBUG_VERBOSE
, "Query Response with Invalid Length\n"));
232 DEBUG ((DEBUG_VERBOSE
, "Query Response with Invalid Value\n"));
235 DEBUG ((DEBUG_VERBOSE
, "Query Response with Invalid Selector\n"));
238 DEBUG ((DEBUG_VERBOSE
, "Query Response with Invalid Index\n"));
241 DEBUG ((DEBUG_VERBOSE
, "Query Response with Invalid Idn\n"));
244 DEBUG ((DEBUG_VERBOSE
, "Query Response with Invalid Opcode\n"));
247 DEBUG ((DEBUG_VERBOSE
, "Query Response with General Failure\n"));
256 Swap little endian to big endian.
258 @param[in, out] Buffer The data buffer. In input, it contains little endian data.
259 In output, it will become big endian.
260 @param[in] BufferSize The length of converted data.
264 SwapLittleEndianToBigEndian (
265 IN OUT UINT8
*Buffer
,
273 SwapCount
= BufferSize
/ 2;
274 for (Index
= 0; Index
< SwapCount
; Index
++) {
275 Temp
= Buffer
[Index
];
276 Buffer
[Index
] = Buffer
[BufferSize
- 1 - Index
];
277 Buffer
[BufferSize
- 1 - Index
] = Temp
;
282 Fill TSF field of QUERY REQUEST UPIU.
284 @param[in, out] TsfBase The base address of TSF field of QUERY REQUEST UPIU.
285 @param[in] Opcode The opcode of request.
286 @param[in] DescId The descriptor ID of request.
287 @param[in] Index The index of request.
288 @param[in] Selector The selector of request.
289 @param[in] Length The length of transferred data. The maximum is 4.
290 @param[in] Value The value of transferred data.
294 UfsFillTsfOfQueryReqUpiu (
295 IN OUT UTP_UPIU_TSF
*TsfBase
,
297 IN UINT8 DescId OPTIONAL
,
298 IN UINT8 Index OPTIONAL
,
299 IN UINT8 Selector OPTIONAL
,
300 IN UINT16 Length OPTIONAL
,
301 IN UINT32 Value OPTIONAL
304 ASSERT (TsfBase
!= NULL
);
305 ASSERT (Opcode
<= UtpQueryFuncOpcodeTogFlag
);
307 TsfBase
->Opcode
= Opcode
;
308 if (Opcode
!= UtpQueryFuncOpcodeNop
) {
309 TsfBase
->DescId
= DescId
;
310 TsfBase
->Index
= Index
;
311 TsfBase
->Selector
= Selector
;
313 if ((Opcode
== UtpQueryFuncOpcodeRdDesc
) || (Opcode
== UtpQueryFuncOpcodeWrDesc
)) {
314 SwapLittleEndianToBigEndian ((UINT8
*)&Length
, sizeof (Length
));
315 TsfBase
->Length
= Length
;
318 if (Opcode
== UtpQueryFuncOpcodeWrAttr
) {
319 SwapLittleEndianToBigEndian ((UINT8
*)&Value
, sizeof (Value
));
320 TsfBase
->Value
= Value
;
326 Initialize COMMAND UPIU.
328 @param[in, out] Command The base address of COMMAND UPIU.
329 @param[in] Lun The Lun on which the SCSI command is executed.
330 @param[in] TaskTag The task tag of request.
331 @param[in] Cdb The cdb buffer containing SCSI command.
332 @param[in] CdbLength The cdb length.
333 @param[in] DataDirection The direction of data transfer.
334 @param[in] ExpDataTranLen The expected transfer data length.
336 @retval EFI_SUCCESS The initialization succeed.
341 IN OUT UTP_COMMAND_UPIU
*Command
,
346 IN UFS_DATA_DIRECTION DataDirection
,
347 IN UINT32 ExpDataTranLen
352 ASSERT ((Command
!= NULL
) && (Cdb
!= NULL
));
355 // Task attribute is hard-coded to Ordered.
357 if (DataDirection
== UfsDataIn
) {
359 } else if (DataDirection
== UfsDataOut
) {
366 // Fill UTP COMMAND UPIU associated fields.
368 Command
->TransCode
= 0x01;
369 Command
->Flags
= Flags
;
371 Command
->TaskTag
= TaskTag
;
372 Command
->CmdSet
= 0x00;
373 SwapLittleEndianToBigEndian ((UINT8
*)&ExpDataTranLen
, sizeof (ExpDataTranLen
));
374 Command
->ExpDataTranLen
= ExpDataTranLen
;
376 CopyMem (Command
->Cdb
, Cdb
, CdbLength
);
382 Initialize UTP PRDT for data transfer.
384 @param[in] Prdt The base address of PRDT.
385 @param[in] Buffer The buffer to be read or written.
386 @param[in] BufferSize The data size to be read or written.
388 @retval EFI_SUCCESS The initialization succeed.
403 if ((BufferSize
& (BIT0
| BIT1
)) != 0) {
404 BufferSize
&= ~(BIT0
| BIT1
);
405 DEBUG ((DEBUG_WARN
, "UfsInitUtpPrdt: The BufferSize [%d] is not dword-aligned!\n", BufferSize
));
408 if (BufferSize
== 0) {
412 ASSERT (((UINTN
)Buffer
& (BIT0
| BIT1
)) == 0);
414 RemainingLen
= BufferSize
;
416 PrdtNumber
= (UINTN
)DivU64x32 ((UINT64
)BufferSize
+ UFS_MAX_DATA_LEN_PER_PRD
- 1, UFS_MAX_DATA_LEN_PER_PRD
);
418 for (PrdtIndex
= 0; PrdtIndex
< PrdtNumber
; PrdtIndex
++) {
419 if (RemainingLen
< UFS_MAX_DATA_LEN_PER_PRD
) {
420 Prdt
[PrdtIndex
].DbCount
= (UINT32
)RemainingLen
- 1;
422 Prdt
[PrdtIndex
].DbCount
= UFS_MAX_DATA_LEN_PER_PRD
- 1;
425 Prdt
[PrdtIndex
].DbAddr
= (UINT32
)RShiftU64 ((UINT64
)(UINTN
)Remaining
, 2);
426 Prdt
[PrdtIndex
].DbAddrU
= (UINT32
)RShiftU64 ((UINT64
)(UINTN
)Remaining
, 32);
427 RemainingLen
-= UFS_MAX_DATA_LEN_PER_PRD
;
428 Remaining
+= UFS_MAX_DATA_LEN_PER_PRD
;
435 Initialize QUERY REQUEST UPIU.
437 @param[in, out] QueryReq The base address of QUERY REQUEST UPIU.
438 @param[in] TaskTag The task tag of request.
439 @param[in] Opcode The opcode of request.
440 @param[in] DescId The descriptor ID of request.
441 @param[in] Index The index of request.
442 @param[in] Selector The selector of request.
443 @param[in] DataSize The data size to be read or written.
444 @param[in] Data The buffer to be read or written.
446 @retval EFI_SUCCESS The initialization succeed.
450 UfsInitQueryRequestUpiu (
451 IN OUT UTP_QUERY_REQ_UPIU
*QueryReq
,
457 IN UINTN DataSize OPTIONAL
,
458 IN UINT8
*Data OPTIONAL
461 ASSERT (QueryReq
!= NULL
);
463 QueryReq
->TransCode
= 0x16;
464 QueryReq
->TaskTag
= TaskTag
;
465 if ((Opcode
== UtpQueryFuncOpcodeRdDesc
) || (Opcode
== UtpQueryFuncOpcodeRdFlag
) || (Opcode
== UtpQueryFuncOpcodeRdAttr
)) {
466 QueryReq
->QueryFunc
= QUERY_FUNC_STD_READ_REQ
;
468 QueryReq
->QueryFunc
= QUERY_FUNC_STD_WRITE_REQ
;
471 if (Opcode
== UtpQueryFuncOpcodeWrAttr
) {
472 UfsFillTsfOfQueryReqUpiu (&QueryReq
->Tsf
, Opcode
, DescId
, Index
, Selector
, 0, *(UINT32
*)Data
);
473 } else if ((Opcode
== UtpQueryFuncOpcodeRdDesc
) || (Opcode
== UtpQueryFuncOpcodeWrDesc
)) {
474 UfsFillTsfOfQueryReqUpiu (&QueryReq
->Tsf
, Opcode
, DescId
, Index
, Selector
, (UINT16
)DataSize
, 0);
476 UfsFillTsfOfQueryReqUpiu (&QueryReq
->Tsf
, Opcode
, DescId
, Index
, Selector
, 0, 0);
479 if (Opcode
== UtpQueryFuncOpcodeWrDesc
) {
480 CopyMem (QueryReq
+ 1, Data
, DataSize
);
487 Allocate COMMAND/RESPONSE UPIU for filling UTP TRD's command descriptor field.
489 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
490 @param[in] Lun The Lun on which the SCSI command is executed.
491 @param[in] Packet The pointer to the EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET data structure.
492 @param[in] Trd The pointer to the UTP Transfer Request Descriptor.
493 @param[out] CmdDescHost A pointer to store the base system memory address of the allocated range.
494 @param[out] CmdDescMapping A resulting value to pass to Unmap().
496 @retval EFI_SUCCESS The creation succeed.
497 @retval EFI_DEVICE_ERROR The creation failed.
498 @retval EFI_OUT_OF_RESOURCES The memory resource is insufficient.
502 UfsCreateScsiCommandDesc (
503 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
505 IN EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
507 OUT VOID
**CmdDescHost
,
508 OUT VOID
**CmdDescMapping
513 UTP_COMMAND_UPIU
*CommandUpiu
;
514 EFI_PHYSICAL_ADDRESS CmdDescPhyAddr
;
517 UFS_DATA_DIRECTION DataDirection
;
519 ASSERT ((Private
!= NULL
) && (Packet
!= NULL
) && (Trd
!= NULL
));
521 if (Packet
->DataDirection
== EFI_EXT_SCSI_DATA_DIRECTION_READ
) {
522 DataLen
= Packet
->InTransferLength
;
523 DataDirection
= UfsDataIn
;
525 DataLen
= Packet
->OutTransferLength
;
526 DataDirection
= UfsDataOut
;
530 DataDirection
= UfsNoData
;
533 PrdtNumber
= (UINTN
)DivU64x32 ((UINT64
)DataLen
+ UFS_MAX_DATA_LEN_PER_PRD
- 1, UFS_MAX_DATA_LEN_PER_PRD
);
535 TotalLen
= ROUNDUP8 (sizeof (UTP_COMMAND_UPIU
)) + ROUNDUP8 (sizeof (UTP_RESPONSE_UPIU
)) + PrdtNumber
* sizeof (UTP_TR_PRD
);
537 Status
= UfsAllocateAlignCommonBuffer (Private
, TotalLen
, CmdDescHost
, &CmdDescPhyAddr
, CmdDescMapping
);
538 if (EFI_ERROR (Status
)) {
542 CommandUpiu
= (UTP_COMMAND_UPIU
*)*CmdDescHost
;
544 UfsInitCommandUpiu (CommandUpiu
, Lun
, Private
->TaskTag
++, Packet
->Cdb
, Packet
->CdbLength
, DataDirection
, DataLen
);
547 // Fill UTP_TRD associated fields
548 // NOTE: Some UFS host controllers request the Response UPIU and the Physical Region Description Table
549 // *MUST* be located at a 64-bit aligned boundary.
551 Trd
->Int
= UFS_INTERRUPT_COMMAND
;
552 Trd
->Dd
= DataDirection
;
553 Trd
->Ct
= UFS_STORAGE_COMMAND_TYPE
;
554 Trd
->Ocs
= UFS_HC_TRD_OCS_INIT_VALUE
;
555 Trd
->UcdBa
= (UINT32
)RShiftU64 ((UINT64
)CmdDescPhyAddr
, 7);
556 Trd
->UcdBaU
= (UINT32
)RShiftU64 ((UINT64
)CmdDescPhyAddr
, 32);
557 Trd
->RuL
= (UINT16
)DivU64x32 ((UINT64
)ROUNDUP8 (sizeof (UTP_RESPONSE_UPIU
)), sizeof (UINT32
));
558 Trd
->RuO
= (UINT16
)DivU64x32 ((UINT64
)ROUNDUP8 (sizeof (UTP_COMMAND_UPIU
)), sizeof (UINT32
));
559 Trd
->PrdtL
= (UINT16
)PrdtNumber
;
560 Trd
->PrdtO
= (UINT16
)DivU64x32 ((UINT64
)(ROUNDUP8 (sizeof (UTP_COMMAND_UPIU
)) + ROUNDUP8 (sizeof (UTP_RESPONSE_UPIU
))), sizeof (UINT32
));
565 Allocate QUERY REQUEST/QUERY RESPONSE UPIU for filling UTP TRD's command descriptor field.
567 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
568 @param[in] Packet The pointer to the UFS_DEVICE_MANAGEMENT_REQUEST_PACKET data structure.
569 @param[in] Trd The pointer to the UTP Transfer Request Descriptor.
570 @param[out] CmdDescHost A pointer to store the base system memory address of the allocated range.
571 @param[out] CmdDescMapping A resulting value to pass to Unmap().
573 @retval EFI_SUCCESS The creation succeed.
574 @retval EFI_DEVICE_ERROR The creation failed.
575 @retval EFI_OUT_OF_RESOURCES The memory resource is insufficient.
576 @retval EFI_INVALID_PARAMETER The parameter passed in is invalid.
580 UfsCreateDMCommandDesc (
581 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
582 IN UFS_DEVICE_MANAGEMENT_REQUEST_PACKET
*Packet
,
584 OUT VOID
**CmdDescHost
,
585 OUT VOID
**CmdDescMapping
589 UTP_QUERY_REQ_UPIU
*QueryReqUpiu
;
594 EFI_PHYSICAL_ADDRESS CmdDescPhyAddr
;
597 ASSERT ((Private
!= NULL
) && (Packet
!= NULL
) && (Trd
!= NULL
));
599 Opcode
= Packet
->Opcode
;
600 if ((Opcode
> UtpQueryFuncOpcodeTogFlag
) || (Opcode
== UtpQueryFuncOpcodeNop
)) {
601 return EFI_INVALID_PARAMETER
;
604 DataDirection
= Packet
->DataDirection
;
605 DataSize
= Packet
->TransferLength
;
606 Data
= Packet
->DataBuffer
;
608 if ((Opcode
== UtpQueryFuncOpcodeWrDesc
) || (Opcode
== UtpQueryFuncOpcodeRdDesc
)) {
609 if (DataSize
== 0 || Data
== NULL
) {
610 return EFI_INVALID_PARAMETER
;
612 TotalLen
= ROUNDUP8 (sizeof (UTP_QUERY_REQ_UPIU
)) + ROUNDUP8 (sizeof (UTP_QUERY_RESP_UPIU
)) + ROUNDUP8 (DataSize
);
614 TotalLen
= ROUNDUP8 (sizeof (UTP_QUERY_REQ_UPIU
)) + ROUNDUP8 (sizeof (UTP_QUERY_RESP_UPIU
));
617 Status
= UfsAllocateAlignCommonBuffer (Private
, TotalLen
, CmdDescHost
, &CmdDescPhyAddr
, CmdDescMapping
);
618 if (EFI_ERROR (Status
)) {
623 // Initialize UTP QUERY REQUEST UPIU
625 QueryReqUpiu
= (UTP_QUERY_REQ_UPIU
*)*CmdDescHost
;
626 ASSERT (QueryReqUpiu
!= NULL
);
627 UfsInitQueryRequestUpiu (
639 // Fill UTP_TRD associated fields
640 // NOTE: Some UFS host controllers request the Query Response UPIU *MUST* be located at a 64-bit aligned boundary.
642 Trd
->Int
= UFS_INTERRUPT_COMMAND
;
643 Trd
->Dd
= DataDirection
;
644 Trd
->Ct
= UFS_STORAGE_COMMAND_TYPE
;
645 Trd
->Ocs
= UFS_HC_TRD_OCS_INIT_VALUE
;
646 Trd
->UcdBa
= (UINT32
)RShiftU64 ((UINT64
)CmdDescPhyAddr
, 7);
647 Trd
->UcdBaU
= (UINT32
)RShiftU64 ((UINT64
)CmdDescPhyAddr
, 32);
648 if (Opcode
== UtpQueryFuncOpcodeWrDesc
) {
649 Trd
->RuL
= (UINT16
)DivU64x32 ((UINT64
)ROUNDUP8 (sizeof (UTP_QUERY_RESP_UPIU
)), sizeof (UINT32
));
650 Trd
->RuO
= (UINT16
)DivU64x32 ((UINT64
)ROUNDUP8 (sizeof (UTP_QUERY_REQ_UPIU
)) + ROUNDUP8 (DataSize
), sizeof (UINT32
));
652 Trd
->RuL
= (UINT16
)DivU64x32 ((UINT64
)ROUNDUP8 (sizeof (UTP_QUERY_RESP_UPIU
)) + ROUNDUP8 (DataSize
), sizeof (UINT32
));
653 Trd
->RuO
= (UINT16
)DivU64x32 ((UINT64
)ROUNDUP8 (sizeof (UTP_QUERY_REQ_UPIU
)), sizeof (UINT32
));
660 Allocate NOP IN and NOP OUT UPIU for filling UTP TRD's command descriptor field.
662 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
663 @param[in] Trd The pointer to the UTP Transfer Request Descriptor.
664 @param[out] CmdDescHost A pointer to store the base system memory address of the allocated range.
665 @param[out] CmdDescMapping A resulting value to pass to Unmap().
667 @retval EFI_SUCCESS The creation succeed.
668 @retval EFI_DEVICE_ERROR The creation failed.
669 @retval EFI_OUT_OF_RESOURCES The memory resource is insufficient.
673 UfsCreateNopCommandDesc (
674 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
676 OUT VOID
**CmdDescHost
,
677 OUT VOID
**CmdDescMapping
681 UTP_NOP_OUT_UPIU
*NopOutUpiu
;
683 EFI_PHYSICAL_ADDRESS CmdDescPhyAddr
;
685 ASSERT ((Private
!= NULL
) && (Trd
!= NULL
));
687 TotalLen
= ROUNDUP8 (sizeof (UTP_NOP_OUT_UPIU
)) + ROUNDUP8 (sizeof (UTP_NOP_IN_UPIU
));
688 Status
= UfsAllocateAlignCommonBuffer (Private
, TotalLen
, CmdDescHost
, &CmdDescPhyAddr
, CmdDescMapping
);
689 if (EFI_ERROR (Status
)) {
693 NopOutUpiu
= (UTP_NOP_OUT_UPIU
*)*CmdDescHost
;
694 ASSERT (NopOutUpiu
!= NULL
);
695 NopOutUpiu
->TaskTag
= Private
->TaskTag
++;
698 // Fill UTP_TRD associated fields
699 // NOTE: Some UFS host controllers request the Nop Out UPIU *MUST* be located at a 64-bit aligned boundary.
701 Trd
->Int
= UFS_INTERRUPT_COMMAND
;
703 Trd
->Ct
= UFS_STORAGE_COMMAND_TYPE
;
704 Trd
->Ocs
= UFS_HC_TRD_OCS_INIT_VALUE
;
705 Trd
->UcdBa
= (UINT32
)RShiftU64 ((UINT64
)CmdDescPhyAddr
, 7);
706 Trd
->UcdBaU
= (UINT32
)RShiftU64 ((UINT64
)CmdDescPhyAddr
, 32);
707 Trd
->RuL
= (UINT16
)DivU64x32 ((UINT64
)ROUNDUP8 (sizeof (UTP_NOP_IN_UPIU
)), sizeof (UINT32
));
708 Trd
->RuO
= (UINT16
)DivU64x32 ((UINT64
)ROUNDUP8 (sizeof (UTP_NOP_OUT_UPIU
)), sizeof (UINT32
));
714 Find out available slot in transfer list of a UFS device.
716 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
717 @param[out] Slot The available slot.
719 @retval EFI_SUCCESS The available slot was found successfully.
720 @retval EFI_NOT_READY No slot is available at this moment.
724 UfsFindAvailableSlotInTrl (
725 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
734 ASSERT ((Private
!= NULL
) && (Slot
!= NULL
));
736 Status
= UfsMmioRead32 (Private
, UFS_HC_UTRLDBR_OFFSET
, &Data
);
737 if (EFI_ERROR (Status
)) {
741 Nutrs
= (UINT8
)((Private
->Capabilities
& UFS_HC_CAP_NUTRS
) + 1);
743 for (Index
= 0; Index
< Nutrs
; Index
++) {
744 if ((Data
& (BIT0
<< Index
)) == 0) {
750 return EFI_NOT_READY
;
754 Find out available slot in task management transfer list of a UFS device.
756 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
757 @param[out] Slot The available slot.
759 @retval EFI_SUCCESS The available slot was found successfully.
763 UfsFindAvailableSlotInTmrl (
764 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
768 ASSERT ((Private
!= NULL
) && (Slot
!= NULL
));
771 // The simplest algo to always use slot 0.
772 // TODO: enhance it to support async transfer with multiple slot.
780 Start specified slot in transfer list of a UFS device.
782 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
783 @param[in] Slot The slot to be started.
788 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
795 Status
= UfsMmioRead32 (Private
, UFS_HC_UTRLRSR_OFFSET
, &Data
);
796 if (EFI_ERROR (Status
)) {
800 if ((Data
& UFS_HC_UTRLRSR
) != UFS_HC_UTRLRSR
) {
801 Status
= UfsMmioWrite32 (Private
, UFS_HC_UTRLRSR_OFFSET
, UFS_HC_UTRLRSR
);
802 if (EFI_ERROR (Status
)) {
807 Status
= UfsMmioWrite32 (Private
, UFS_HC_UTRLDBR_OFFSET
, BIT0
<< Slot
);
808 if (EFI_ERROR (Status
)) {
816 Stop specified slot in transfer list of a UFS device.
818 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
819 @param[in] Slot The slot to be stop.
824 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
831 Status
= UfsMmioRead32 (Private
, UFS_HC_UTRLDBR_OFFSET
, &Data
);
832 if (EFI_ERROR (Status
)) {
836 if ((Data
& (BIT0
<< Slot
)) != 0) {
837 Status
= UfsMmioRead32 (Private
, UFS_HC_UTRLCLR_OFFSET
, &Data
);
838 if (EFI_ERROR (Status
)) {
842 Status
= UfsMmioWrite32 (Private
, UFS_HC_UTRLCLR_OFFSET
, Data
& ~(BIT0
<< Slot
));
843 if (EFI_ERROR (Status
)) {
852 Extracts return data from query response upiu.
854 @param[in] Packet Pointer to the UFS_DEVICE_MANAGEMENT_REQUEST_PACKET.
855 @param[in] QueryResp Pointer to the query response.
857 @retval EFI_INVALID_PARAMETER Packet or QueryResp are empty or opcode is invalid.
858 @retval EFI_SUCCESS Data extracted.
862 UfsGetReturnDataFromQueryResponse (
863 IN UFS_DEVICE_MANAGEMENT_REQUEST_PACKET
*Packet
,
864 IN UTP_QUERY_RESP_UPIU
*QueryResp
867 UINT16 ReturnDataSize
;
870 if (Packet
== NULL
|| QueryResp
== NULL
) {
871 return EFI_INVALID_PARAMETER
;
874 switch (Packet
->Opcode
) {
875 case UtpQueryFuncOpcodeRdDesc
:
876 ReturnDataSize
= QueryResp
->Tsf
.Length
;
877 SwapLittleEndianToBigEndian ((UINT8
*)&ReturnDataSize
, sizeof (UINT16
));
878 CopyMem (Packet
->DataBuffer
, (QueryResp
+ 1), ReturnDataSize
);
879 Packet
->TransferLength
= ReturnDataSize
;
881 case UtpQueryFuncOpcodeWrDesc
:
882 ReturnDataSize
= QueryResp
->Tsf
.Length
;
883 SwapLittleEndianToBigEndian ((UINT8
*)&ReturnDataSize
, sizeof (UINT16
));
884 Packet
->TransferLength
= ReturnDataSize
;
886 case UtpQueryFuncOpcodeRdFlag
:
887 case UtpQueryFuncOpcodeSetFlag
:
888 case UtpQueryFuncOpcodeClrFlag
:
889 case UtpQueryFuncOpcodeTogFlag
:
890 CopyMem (Packet
->DataBuffer
, &QueryResp
->Tsf
.Value
, sizeof (UINT8
));
892 case UtpQueryFuncOpcodeRdAttr
:
893 case UtpQueryFuncOpcodeWrAttr
:
894 ReturnData
= QueryResp
->Tsf
.Value
;
895 SwapLittleEndianToBigEndian ((UINT8
*) &ReturnData
, sizeof (UINT32
));
896 CopyMem (Packet
->DataBuffer
, &ReturnData
, sizeof (UINT32
));
899 return EFI_INVALID_PARAMETER
;
906 Creates Transfer Request descriptor and sends Query Request to the device.
908 @param[in] Private Pointer to the UFS_PASS_THRU_PRIVATE_DATA.
909 @param[in] Packet Pointer to the UFS_DEVICE_MANAGEMENT_REQUEST_PACKET.
911 @retval EFI_SUCCESS The device descriptor was read/written successfully.
912 @retval EFI_DEVICE_ERROR A device error occurred while attempting to r/w the device descriptor.
913 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of r/w the device descriptor.
917 UfsSendDmRequestRetry (
918 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
919 IN UFS_DEVICE_MANAGEMENT_REQUEST_PACKET
*Packet
925 VOID
*CmdDescMapping
;
927 EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*UfsHc
;
928 UTP_QUERY_RESP_UPIU
*QueryResp
;
932 // Find out which slot of transfer request list is available.
934 Status
= UfsFindAvailableSlotInTrl (Private
, &Slot
);
935 if (EFI_ERROR (Status
)) {
939 Trd
= ((UTP_TRD
*)Private
->UtpTrlBase
) + Slot
;
941 // Fill transfer request descriptor to this slot.
943 Status
= UfsCreateDMCommandDesc (Private
, Packet
, Trd
, &CmdDescHost
, &CmdDescMapping
);
944 if (EFI_ERROR (Status
)) {
945 DEBUG ((DEBUG_ERROR
, "Failed to create DM command descriptor\n"));
949 UfsHc
= Private
->UfsHostController
;
950 QueryResp
= (UTP_QUERY_RESP_UPIU
*)((UINT8
*)CmdDescHost
+ Trd
->RuO
* sizeof (UINT32
));
951 ASSERT (QueryResp
!= NULL
);
952 CmdDescSize
= Trd
->RuO
* sizeof (UINT32
) + Trd
->RuL
* sizeof (UINT32
);
955 // Start to execute the transfer request.
957 UfsStartExecCmd (Private
, Slot
);
960 // Wait for the completion of the transfer request.
962 Status
= UfsWaitMemSet (Private
, UFS_HC_UTRLDBR_OFFSET
, BIT0
, 0, Packet
->Timeout
);
963 if (EFI_ERROR (Status
)) {
967 if (Trd
->Ocs
!= 0 || QueryResp
->QueryResp
!= UfsUtpQueryResponseSuccess
) {
968 DEBUG ((DEBUG_ERROR
, "Failed to send query request, OCS = %X, QueryResp = %X\n", Trd
->Ocs
, QueryResp
->QueryResp
));
969 DumpQueryResponseResult (QueryResp
->QueryResp
);
970 Status
= EFI_DEVICE_ERROR
;
974 Status
= UfsGetReturnDataFromQueryResponse (Packet
, QueryResp
);
975 if (EFI_ERROR (Status
)) {
976 DEBUG ((DEBUG_ERROR
, "Failed to get return data from query response\n"));
981 UfsHc
->Flush (UfsHc
);
983 UfsStopExecCmd (Private
, Slot
);
985 if (CmdDescMapping
!= NULL
) {
986 UfsHc
->Unmap (UfsHc
, CmdDescMapping
);
988 if (CmdDescHost
!= NULL
) {
989 UfsHc
->FreeBuffer (UfsHc
, EFI_SIZE_TO_PAGES (CmdDescSize
), CmdDescHost
);
996 Sends Query Request to the device. Query is sent until device responds correctly or counter runs out.
998 @param[in] Private Pointer to the UFS_PASS_THRU_PRIVATE_DATA.
999 @param[in] Packet Pointer to the UFS_DEVICE_MANAGEMENT_PACKET.
1001 @retval EFI_SUCCESS The device responded correctly to the Query request.
1002 @retval EFI_DEVICE_ERROR A device error occurred while waiting for the response from the device.
1003 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of the operation.
1008 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
1009 IN UFS_DEVICE_MANAGEMENT_REQUEST_PACKET
*Packet
1015 Status
= EFI_SUCCESS
;
1017 for (Retry
= 0; Retry
< 5; Retry
++) {
1018 Status
= UfsSendDmRequestRetry (Private
, Packet
);
1019 if (!EFI_ERROR (Status
)) {
1024 DEBUG ((DEBUG_ERROR
, "Failed to get response from the device after %d retries\n", Retry
));
1029 Read or write specified device descriptor of a UFS device.
1031 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1032 @param[in] Read The boolean variable to show r/w direction.
1033 @param[in] DescId The ID of device descriptor.
1034 @param[in] Index The Index of device descriptor.
1035 @param[in] Selector The Selector of device descriptor.
1036 @param[in, out] Descriptor The buffer of device descriptor to be read or written.
1037 @param[in] DescSize The size of device descriptor buffer.
1039 @retval EFI_SUCCESS The device descriptor was read/written successfully.
1040 @retval EFI_DEVICE_ERROR A device error occurred while attempting to r/w the device descriptor.
1041 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of r/w the device descriptor.
1046 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
1051 IN OUT VOID
*Descriptor
,
1055 UFS_DEVICE_MANAGEMENT_REQUEST_PACKET Packet
;
1057 ZeroMem (&Packet
, sizeof (UFS_DEVICE_MANAGEMENT_REQUEST_PACKET
));
1060 Packet
.DataDirection
= UfsDataIn
;
1061 Packet
.Opcode
= UtpQueryFuncOpcodeRdDesc
;
1063 Packet
.DataDirection
= UfsDataOut
;
1064 Packet
.Opcode
= UtpQueryFuncOpcodeWrDesc
;
1066 Packet
.DataBuffer
= Descriptor
;
1067 Packet
.TransferLength
= DescSize
;
1068 Packet
.DescId
= DescId
;
1069 Packet
.Index
= Index
;
1070 Packet
.Selector
= Selector
;
1071 Packet
.Timeout
= UFS_TIMEOUT
;
1073 return UfsSendDmRequest (Private
, &Packet
);
1077 Read or write specified attribute of a UFS device.
1079 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1080 @param[in] Read The boolean variable to show r/w direction.
1081 @param[in] AttrId The ID of Attribute.
1082 @param[in] Index The Index of Attribute.
1083 @param[in] Selector The Selector of Attribute.
1084 @param[in, out] Attributes The value of Attribute to be read or written.
1086 @retval EFI_SUCCESS The Attribute was read/written successfully.
1087 @retval EFI_DEVICE_ERROR A device error occurred while attempting to r/w the Attribute.
1088 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of r/w the Attribute.
1093 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
1098 IN OUT UINT32
*Attributes
1101 UFS_DEVICE_MANAGEMENT_REQUEST_PACKET Packet
;
1103 ZeroMem (&Packet
, sizeof (UFS_DEVICE_MANAGEMENT_REQUEST_PACKET
));
1106 Packet
.DataDirection
= UfsDataIn
;
1107 Packet
.Opcode
= UtpQueryFuncOpcodeRdAttr
;
1109 Packet
.DataDirection
= UfsDataOut
;
1110 Packet
.Opcode
= UtpQueryFuncOpcodeWrAttr
;
1112 Packet
.DataBuffer
= Attributes
;
1113 Packet
.DescId
= AttrId
;
1114 Packet
.Index
= Index
;
1115 Packet
.Selector
= Selector
;
1116 Packet
.Timeout
= UFS_TIMEOUT
;
1118 return UfsSendDmRequest (Private
, &Packet
);
1122 Read or write specified flag of a UFS device.
1124 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1125 @param[in] Read The boolean variable to show r/w direction.
1126 @param[in] FlagId The ID of flag to be read or written.
1127 @param[in, out] Value The value to set or clear flag.
1129 @retval EFI_SUCCESS The flag was read/written successfully.
1130 @retval EFI_DEVICE_ERROR A device error occurred while attempting to r/w the flag.
1131 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of r/w the flag.
1136 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
1142 UFS_DEVICE_MANAGEMENT_REQUEST_PACKET Packet
;
1144 if (Value
== NULL
) {
1145 return EFI_INVALID_PARAMETER
;
1148 ZeroMem (&Packet
, sizeof (UFS_DEVICE_MANAGEMENT_REQUEST_PACKET
));
1151 ASSERT (Value
!= NULL
);
1152 Packet
.DataDirection
= UfsDataIn
;
1153 Packet
.Opcode
= UtpQueryFuncOpcodeRdFlag
;
1155 Packet
.DataDirection
= UfsDataOut
;
1157 Packet
.Opcode
= UtpQueryFuncOpcodeSetFlag
;
1158 } else if (*Value
== 0) {
1159 Packet
.Opcode
= UtpQueryFuncOpcodeClrFlag
;
1161 return EFI_INVALID_PARAMETER
;
1164 Packet
.DataBuffer
= Value
;
1165 Packet
.DescId
= FlagId
;
1167 Packet
.Selector
= 0;
1168 Packet
.Timeout
= UFS_TIMEOUT
;
1170 return UfsSendDmRequest (Private
, &Packet
);
1174 Set specified flag to 1 on a UFS device.
1176 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1177 @param[in] FlagId The ID of flag to be set.
1179 @retval EFI_SUCCESS The flag was set successfully.
1180 @retval EFI_DEVICE_ERROR A device error occurred while attempting to set the flag.
1181 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of setting the flag.
1186 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
1194 Status
= UfsRwFlags (Private
, FALSE
, FlagId
, &Value
);
1200 Clear specified flag to 0 on a UFS device.
1202 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1203 @param[in] FlagId The ID of flag to be cleared.
1205 @retval EFI_SUCCESS The flag was cleared successfully.
1206 @retval EFI_DEVICE_ERROR A device error occurred while attempting to clear the flag.
1207 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of clearing the flag.
1212 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
1220 Status
= UfsRwFlags (Private
, FALSE
, FlagId
, &Value
);
1226 Read specified flag from a UFS device.
1228 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1229 @param[in] FlagId The ID of flag to be read.
1230 @param[out] Value The flag's value.
1232 @retval EFI_SUCCESS The flag was read successfully.
1233 @retval EFI_DEVICE_ERROR A device error occurred while attempting to read the flag.
1234 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of reading the flag.
1239 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
1246 Status
= UfsRwFlags (Private
, TRUE
, FlagId
, Value
);
1252 Sends NOP IN cmd to a UFS device for initialization process request.
1253 For more details, please refer to UFS 2.0 spec Figure 13.3.
1255 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1257 @retval EFI_SUCCESS The NOP IN command was sent by the host. The NOP OUT response was
1258 received successfully.
1259 @retval EFI_DEVICE_ERROR A device error occurred while attempting to execute NOP IN command.
1260 @retval EFI_OUT_OF_RESOURCES The resource for transfer is not available.
1261 @retval EFI_TIMEOUT A timeout occurred while waiting for the NOP IN command to execute.
1266 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
1272 UTP_NOP_IN_UPIU
*NopInUpiu
;
1275 VOID
*CmdDescMapping
;
1276 EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*UfsHc
;
1279 // Find out which slot of transfer request list is available.
1281 Status
= UfsFindAvailableSlotInTrl (Private
, &Slot
);
1282 if (EFI_ERROR (Status
)) {
1286 Trd
= ((UTP_TRD
*)Private
->UtpTrlBase
) + Slot
;
1287 Status
= UfsCreateNopCommandDesc (Private
, Trd
, &CmdDescHost
, &CmdDescMapping
);
1288 if (EFI_ERROR (Status
)) {
1293 // Check the transfer request result.
1295 UfsHc
= Private
->UfsHostController
;
1296 NopInUpiu
= (UTP_NOP_IN_UPIU
*)((UINT8
*)CmdDescHost
+ Trd
->RuO
* sizeof (UINT32
));
1297 ASSERT (NopInUpiu
!= NULL
);
1298 CmdDescSize
= Trd
->RuO
* sizeof (UINT32
) + Trd
->RuL
* sizeof (UINT32
);
1301 // Start to execute the transfer request.
1303 UfsStartExecCmd (Private
, Slot
);
1306 // Wait for the completion of the transfer request.
1308 Status
= UfsWaitMemSet (Private
, UFS_HC_UTRLDBR_OFFSET
, BIT0
<< Slot
, 0, UFS_TIMEOUT
);
1309 if (EFI_ERROR (Status
)) {
1313 if (NopInUpiu
->Resp
!= 0) {
1314 Status
= EFI_DEVICE_ERROR
;
1316 Status
= EFI_SUCCESS
;
1320 UfsHc
->Flush (UfsHc
);
1322 UfsStopExecCmd (Private
, Slot
);
1324 if (CmdDescMapping
!= NULL
) {
1325 UfsHc
->Unmap (UfsHc
, CmdDescMapping
);
1327 if (CmdDescHost
!= NULL
) {
1328 UfsHc
->FreeBuffer (UfsHc
, EFI_SIZE_TO_PAGES (CmdDescSize
), CmdDescHost
);
1335 Sends a UFS-supported SCSI Request Packet to a UFS device that is attached to the UFS host controller.
1337 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1338 @param[in] Lun The LUN of the UFS device to send the SCSI Request Packet.
1339 @param[in, out] Packet A pointer to the SCSI Request Packet to send to a specified Lun of the
1341 @param[in] Event If nonblocking I/O is not supported then Event is ignored, and blocking
1342 I/O is performed. If Event is NULL, then blocking I/O is performed. If
1343 Event is not NULL and non blocking I/O is supported, then
1344 nonblocking I/O is performed, and Event will be signaled when the
1345 SCSI Request Packet completes.
1347 @retval EFI_SUCCESS The SCSI Request Packet was sent by the host. For bi-directional
1348 commands, InTransferLength bytes were transferred from
1349 InDataBuffer. For write and bi-directional commands,
1350 OutTransferLength bytes were transferred by
1352 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SCSI Request
1354 @retval EFI_OUT_OF_RESOURCES The resource for transfer is not available.
1355 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
1360 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
1362 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
1363 IN EFI_EVENT Event OPTIONAL
1367 UTP_RESPONSE_UPIU
*Response
;
1368 UINT16 SenseDataLen
;
1369 UINT32 ResTranCount
;
1371 EFI_PHYSICAL_ADDRESS DataBufPhyAddr
;
1374 EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*UfsHc
;
1375 EDKII_UFS_HOST_CONTROLLER_OPERATION Flag
;
1376 UTP_TR_PRD
*PrdtBase
;
1378 UFS_PASS_THRU_TRANS_REQ
*TransReq
;
1380 TransReq
= AllocateZeroPool (sizeof (UFS_PASS_THRU_TRANS_REQ
));
1381 if (TransReq
== NULL
) {
1382 return EFI_OUT_OF_RESOURCES
;
1385 TransReq
->Signature
= UFS_PASS_THRU_TRANS_REQ_SIG
;
1386 TransReq
->TimeoutRemain
= Packet
->Timeout
;
1388 UfsHc
= Private
->UfsHostController
;
1390 // Find out which slot of transfer request list is available.
1392 Status
= UfsFindAvailableSlotInTrl (Private
, &TransReq
->Slot
);
1393 if (EFI_ERROR (Status
)) {
1397 TransReq
->Trd
= ((UTP_TRD
*)Private
->UtpTrlBase
) + TransReq
->Slot
;
1400 // Fill transfer request descriptor to this slot.
1402 Status
= UfsCreateScsiCommandDesc (
1407 &TransReq
->CmdDescHost
,
1408 &TransReq
->CmdDescMapping
1410 if (EFI_ERROR (Status
)) {
1414 TransReq
->CmdDescSize
= TransReq
->Trd
->PrdtO
* sizeof (UINT32
) + TransReq
->Trd
->PrdtL
* sizeof (UTP_TR_PRD
);
1416 if (Packet
->DataDirection
== EFI_EXT_SCSI_DATA_DIRECTION_READ
) {
1417 DataBuf
= Packet
->InDataBuffer
;
1418 DataLen
= Packet
->InTransferLength
;
1419 Flag
= EdkiiUfsHcOperationBusMasterWrite
;
1421 DataBuf
= Packet
->OutDataBuffer
;
1422 DataLen
= Packet
->OutTransferLength
;
1423 Flag
= EdkiiUfsHcOperationBusMasterRead
;
1427 MapLength
= DataLen
;
1428 Status
= UfsHc
->Map (
1434 &TransReq
->DataBufMapping
1437 if (EFI_ERROR (Status
) || (DataLen
!= MapLength
)) {
1442 // Fill PRDT table of Command UPIU for executed SCSI cmd.
1444 PrdtBase
= (UTP_TR_PRD
*)((UINT8
*)TransReq
->CmdDescHost
+ ROUNDUP8 (sizeof (UTP_COMMAND_UPIU
)) + ROUNDUP8 (sizeof (UTP_RESPONSE_UPIU
)));
1445 ASSERT (PrdtBase
!= NULL
);
1446 UfsInitUtpPrdt (PrdtBase
, (VOID
*)(UINTN
)DataBufPhyAddr
, DataLen
);
1449 // Insert the async SCSI cmd to the Async I/O list
1451 if (Event
!= NULL
) {
1452 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1453 TransReq
->Packet
= Packet
;
1454 TransReq
->CallerEvent
= Event
;
1455 InsertTailList (&Private
->Queue
, &TransReq
->TransferList
);
1456 gBS
->RestoreTPL (OldTpl
);
1460 // Start to execute the transfer request.
1462 UfsStartExecCmd (Private
, TransReq
->Slot
);
1465 // Immediately return for async I/O.
1467 if (Event
!= NULL
) {
1472 // Wait for the completion of the transfer request.
1474 Status
= UfsWaitMemSet (Private
, UFS_HC_UTRLDBR_OFFSET
, BIT0
<< TransReq
->Slot
, 0, Packet
->Timeout
);
1475 if (EFI_ERROR (Status
)) {
1480 // Get sense data if exists
1482 Response
= (UTP_RESPONSE_UPIU
*)((UINT8
*)TransReq
->CmdDescHost
+ TransReq
->Trd
->RuO
* sizeof (UINT32
));
1483 ASSERT (Response
!= NULL
);
1484 SenseDataLen
= Response
->SenseDataLen
;
1485 SwapLittleEndianToBigEndian ((UINT8
*)&SenseDataLen
, sizeof (UINT16
));
1487 if ((Packet
->SenseDataLength
!= 0) && (Packet
->SenseData
!= NULL
)) {
1488 CopyMem (Packet
->SenseData
, Response
->SenseData
, SenseDataLen
);
1489 Packet
->SenseDataLength
= (UINT8
)SenseDataLen
;
1493 // Check the transfer request result.
1495 Packet
->TargetStatus
= Response
->Status
;
1496 if (Response
->Response
!= 0) {
1497 DEBUG ((DEBUG_ERROR
, "UfsExecScsiCmds() fails with Target Failure\n"));
1498 Status
= EFI_DEVICE_ERROR
;
1502 if (TransReq
->Trd
->Ocs
== 0) {
1503 if (Packet
->DataDirection
== EFI_EXT_SCSI_DATA_DIRECTION_READ
) {
1504 if ((Response
->Flags
& BIT5
) == BIT5
) {
1505 ResTranCount
= Response
->ResTranCount
;
1506 SwapLittleEndianToBigEndian ((UINT8
*)&ResTranCount
, sizeof (UINT32
));
1507 Packet
->InTransferLength
-= ResTranCount
;
1510 if ((Response
->Flags
& BIT5
) == BIT5
) {
1511 ResTranCount
= Response
->ResTranCount
;
1512 SwapLittleEndianToBigEndian ((UINT8
*)&ResTranCount
, sizeof (UINT32
));
1513 Packet
->OutTransferLength
-= ResTranCount
;
1517 Status
= EFI_DEVICE_ERROR
;
1521 UfsHc
->Flush (UfsHc
);
1523 UfsStopExecCmd (Private
, TransReq
->Slot
);
1525 if (TransReq
->DataBufMapping
!= NULL
) {
1526 UfsHc
->Unmap (UfsHc
, TransReq
->DataBufMapping
);
1530 if (TransReq
->CmdDescMapping
!= NULL
) {
1531 UfsHc
->Unmap (UfsHc
, TransReq
->CmdDescMapping
);
1533 if (TransReq
->CmdDescHost
!= NULL
) {
1534 UfsHc
->FreeBuffer (UfsHc
, EFI_SIZE_TO_PAGES (TransReq
->CmdDescSize
), TransReq
->CmdDescHost
);
1536 if (TransReq
!= NULL
) {
1537 FreePool (TransReq
);
1544 Sent UIC DME_LINKSTARTUP command to start the link startup procedure.
1546 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1547 @param[in] UicOpcode The opcode of the UIC command.
1548 @param[in] Arg1 The value for 1st argument of the UIC command.
1549 @param[in] Arg2 The value for 2nd argument of the UIC command.
1550 @param[in] Arg3 The value for 3rd argument of the UIC command.
1552 @return EFI_SUCCESS Successfully execute this UIC command and detect attached UFS device.
1553 @return EFI_DEVICE_ERROR Fail to execute this UIC command and detect attached UFS device.
1554 @return EFI_NOT_FOUND The presence of the UFS device isn't detected.
1558 UfsExecUicCommands (
1559 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
1569 Status
= UfsMmioRead32 (Private
, UFS_HC_IS_OFFSET
, &Data
);
1570 if (EFI_ERROR (Status
)) {
1574 if ((Data
& UFS_HC_IS_UCCS
) == UFS_HC_IS_UCCS
) {
1576 // Clear IS.BIT10 UIC Command Completion Status (UCCS) at first.
1578 Status
= UfsMmioWrite32 (Private
, UFS_HC_IS_OFFSET
, Data
);
1579 if (EFI_ERROR (Status
)) {
1585 // When programming UIC command registers, host software shall set the register UICCMD
1586 // only after all the UIC command argument registers (UICCMDARG1, UICCMDARG2 and UICCMDARG3)
1589 Status
= UfsMmioWrite32 (Private
, UFS_HC_UCMD_ARG1_OFFSET
, Arg1
);
1590 if (EFI_ERROR (Status
)) {
1594 Status
= UfsMmioWrite32 (Private
, UFS_HC_UCMD_ARG2_OFFSET
, Arg2
);
1595 if (EFI_ERROR (Status
)) {
1599 Status
= UfsMmioWrite32 (Private
, UFS_HC_UCMD_ARG3_OFFSET
, Arg3
);
1600 if (EFI_ERROR (Status
)) {
1605 // Host software shall only set the UICCMD if HCS.UCRDY is set to 1.
1607 Status
= UfsWaitMemSet (Private
, UFS_HC_STATUS_OFFSET
, UFS_HC_HCS_UCRDY
, UFS_HC_HCS_UCRDY
, UFS_TIMEOUT
);
1608 if (EFI_ERROR (Status
)) {
1612 Status
= UfsMmioWrite32 (Private
, UFS_HC_UIC_CMD_OFFSET
, (UINT32
)UicOpcode
);
1613 if (EFI_ERROR (Status
)) {
1618 // UFS 2.0 spec section 5.3.1 Offset:0x20 IS.Bit10 UIC Command Completion Status (UCCS)
1619 // This bit is set to '1' by the host controller upon completion of a UIC command.
1621 Status
= UfsWaitMemSet (Private
, UFS_HC_IS_OFFSET
, UFS_HC_IS_UCCS
, UFS_HC_IS_UCCS
, UFS_TIMEOUT
);
1622 if (EFI_ERROR (Status
)) {
1626 if (UicOpcode
!= UfsUicDmeReset
) {
1627 Status
= UfsMmioRead32 (Private
, UFS_HC_UCMD_ARG2_OFFSET
, &Data
);
1628 if (EFI_ERROR (Status
)) {
1631 if ((Data
& 0xFF) != 0) {
1633 DumpUicCmdExecResult (UicOpcode
, (UINT8
)(Data
& 0xFF));
1635 return EFI_DEVICE_ERROR
;
1640 // Check value of HCS.DP and make sure that there is a device attached to the Link.
1642 Status
= UfsMmioRead32 (Private
, UFS_HC_STATUS_OFFSET
, &Data
);
1643 if (EFI_ERROR (Status
)) {
1647 if ((Data
& UFS_HC_HCS_DP
) == 0) {
1648 Status
= UfsWaitMemSet (Private
, UFS_HC_IS_OFFSET
, UFS_HC_IS_ULSS
, UFS_HC_IS_ULSS
, UFS_TIMEOUT
);
1649 if (EFI_ERROR (Status
)) {
1650 return EFI_DEVICE_ERROR
;
1652 return EFI_NOT_FOUND
;
1655 DEBUG ((DEBUG_INFO
, "UfsPassThruDxe: found a attached UFS device\n"));
1661 Allocate common buffer for host and UFS bus master access simultaneously.
1663 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1664 @param[in] Size The length of buffer to be allocated.
1665 @param[out] CmdDescHost A pointer to store the base system memory address of the allocated range.
1666 @param[out] CmdDescPhyAddr The resulting map address for the UFS bus master to use to access the hosts CmdDescHost.
1667 @param[out] CmdDescMapping A resulting value to pass to Unmap().
1669 @retval EFI_SUCCESS The common buffer was allocated successfully.
1670 @retval EFI_DEVICE_ERROR The allocation fails.
1671 @retval EFI_OUT_OF_RESOURCES The memory resource is insufficient.
1675 UfsAllocateAlignCommonBuffer (
1676 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
1678 OUT VOID
**CmdDescHost
,
1679 OUT EFI_PHYSICAL_ADDRESS
*CmdDescPhyAddr
,
1680 OUT VOID
**CmdDescMapping
1685 BOOLEAN Is32BitAddr
;
1686 EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*UfsHc
;
1688 if ((Private
->Capabilities
& UFS_HC_CAP_64ADDR
) == UFS_HC_CAP_64ADDR
) {
1689 Is32BitAddr
= FALSE
;
1694 UfsHc
= Private
->UfsHostController
;
1695 Status
= UfsHc
->AllocateBuffer (
1698 EfiBootServicesData
,
1699 EFI_SIZE_TO_PAGES (Size
),
1703 if (EFI_ERROR (Status
)) {
1704 *CmdDescMapping
= NULL
;
1705 *CmdDescHost
= NULL
;
1706 *CmdDescPhyAddr
= 0;
1707 return EFI_OUT_OF_RESOURCES
;
1710 Bytes
= EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (Size
));
1711 Status
= UfsHc
->Map (
1713 EdkiiUfsHcOperationBusMasterCommonBuffer
,
1720 if (EFI_ERROR (Status
) || (Bytes
!= EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (Size
)))) {
1723 EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (Size
)),
1726 *CmdDescHost
= NULL
;
1727 return EFI_OUT_OF_RESOURCES
;
1730 if (Is32BitAddr
&& ((*CmdDescPhyAddr
) > 0x100000000ULL
)) {
1732 // The UFS host controller doesn't support 64bit addressing, so should not get a >4G UFS bus master address.
1740 EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (Size
)),
1743 *CmdDescMapping
= NULL
;
1744 *CmdDescHost
= NULL
;
1745 return EFI_DEVICE_ERROR
;
1748 ZeroMem (*CmdDescHost
, EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (Size
)));
1753 Enable the UFS host controller for accessing.
1755 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1757 @retval EFI_SUCCESS The UFS host controller enabling was executed successfully.
1758 @retval EFI_DEVICE_ERROR A device error occurred while enabling the UFS host controller.
1762 UfsEnableHostController (
1763 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
1770 // UFS 2.0 spec section 7.1.1 - Host Controller Initialization
1772 // Reinitialize the UFS host controller if HCE bit of HC register is set.
1774 Status
= UfsMmioRead32 (Private
, UFS_HC_ENABLE_OFFSET
, &Data
);
1775 if (EFI_ERROR (Status
)) {
1779 if ((Data
& UFS_HC_HCE_EN
) == UFS_HC_HCE_EN
) {
1781 // Write a 0 to the HCE register at first to disable the host controller.
1783 Status
= UfsMmioWrite32 (Private
, UFS_HC_ENABLE_OFFSET
, 0);
1784 if (EFI_ERROR (Status
)) {
1788 // Wait until HCE is read as '0' before continuing.
1790 Status
= UfsWaitMemSet (Private
, UFS_HC_ENABLE_OFFSET
, UFS_HC_HCE_EN
, 0, UFS_TIMEOUT
);
1791 if (EFI_ERROR (Status
)) {
1792 return EFI_DEVICE_ERROR
;
1797 // Write a 1 to the HCE register to enable the UFS host controller.
1799 Status
= UfsMmioWrite32 (Private
, UFS_HC_ENABLE_OFFSET
, UFS_HC_HCE_EN
);
1800 if (EFI_ERROR (Status
)) {
1805 // Wait until HCE is read as '1' before continuing.
1807 Status
= UfsWaitMemSet (Private
, UFS_HC_ENABLE_OFFSET
, UFS_HC_HCE_EN
, UFS_HC_HCE_EN
, UFS_TIMEOUT
);
1808 if (EFI_ERROR (Status
)) {
1809 return EFI_DEVICE_ERROR
;
1816 Detect if a UFS device attached.
1818 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1820 @retval EFI_SUCCESS The UFS device detection was executed successfully.
1821 @retval EFI_NOT_FOUND Not found a UFS device attached.
1822 @retval EFI_DEVICE_ERROR A device error occurred while detecting the UFS device.
1826 UfsDeviceDetection (
1827 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
1834 // Start UFS device detection.
1835 // Try up to 3 times for establishing data link with device.
1837 for (Retry
= 0; Retry
< 3; Retry
++) {
1838 Status
= UfsExecUicCommands (Private
, UfsUicDmeLinkStartup
, 0, 0, 0);
1839 if (!EFI_ERROR (Status
)) {
1843 if (Status
== EFI_NOT_FOUND
) {
1847 return EFI_DEVICE_ERROR
;
1851 return EFI_NOT_FOUND
;
1858 Initialize UFS task management request list related h/w context.
1860 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1862 @retval EFI_SUCCESS The UFS task management list was initialzed successfully.
1863 @retval EFI_DEVICE_ERROR The initialization fails.
1867 UfsInitTaskManagementRequestList (
1868 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
1874 EFI_PHYSICAL_ADDRESS CmdDescPhyAddr
;
1875 VOID
*CmdDescMapping
;
1879 // Initial h/w and s/w context for future operations.
1882 CmdDescMapping
= NULL
;
1885 Status
= UfsMmioRead32 (Private
, UFS_HC_CAP_OFFSET
, &Data
);
1886 if (EFI_ERROR (Status
)) {
1890 Private
->Capabilities
= Data
;
1893 // Allocate and initialize UTP Task Management Request List.
1895 Nutmrs
= (UINT8
) (RShiftU64 ((Private
->Capabilities
& UFS_HC_CAP_NUTMRS
), 16) + 1);
1896 Status
= UfsAllocateAlignCommonBuffer (Private
, Nutmrs
* sizeof (UTP_TMRD
), &CmdDescHost
, &CmdDescPhyAddr
, &CmdDescMapping
);
1897 if (EFI_ERROR (Status
)) {
1902 // Program the UTP Task Management Request List Base Address and UTP Task Management
1903 // Request List Base Address with a 64-bit address allocated at step 6.
1905 Status
= UfsMmioWrite32 (Private
, UFS_HC_UTMRLBA_OFFSET
, (UINT32
)(UINTN
)CmdDescPhyAddr
);
1906 if (EFI_ERROR (Status
)) {
1910 Status
= UfsMmioWrite32 (Private
, UFS_HC_UTMRLBAU_OFFSET
, (UINT32
)RShiftU64 ((UINT64
)CmdDescPhyAddr
, 32));
1911 if (EFI_ERROR (Status
)) {
1914 Private
->UtpTmrlBase
= CmdDescHost
;
1915 Private
->Nutmrs
= Nutmrs
;
1916 Private
->TmrlMapping
= CmdDescMapping
;
1919 // Enable the UTP Task Management Request List by setting the UTP Task Management
1920 // Request List RunStop Register (UTMRLRSR) to '1'.
1922 Status
= UfsMmioWrite32 (Private
, UFS_HC_UTMRLRSR_OFFSET
, UFS_HC_UTMRLRSR
);
1923 if (EFI_ERROR (Status
)) {
1931 Initialize UFS transfer request list related h/w context.
1933 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1935 @retval EFI_SUCCESS The UFS transfer list was initialzed successfully.
1936 @retval EFI_DEVICE_ERROR The initialization fails.
1940 UfsInitTransferRequestList (
1941 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
1947 EFI_PHYSICAL_ADDRESS CmdDescPhyAddr
;
1948 VOID
*CmdDescMapping
;
1952 // Initial h/w and s/w context for future operations.
1955 CmdDescMapping
= NULL
;
1958 Status
= UfsMmioRead32 (Private
, UFS_HC_CAP_OFFSET
, &Data
);
1959 if (EFI_ERROR (Status
)) {
1963 Private
->Capabilities
= Data
;
1966 // Allocate and initialize UTP Transfer Request List.
1968 Nutrs
= (UINT8
)((Private
->Capabilities
& UFS_HC_CAP_NUTRS
) + 1);
1969 Status
= UfsAllocateAlignCommonBuffer (Private
, Nutrs
* sizeof (UTP_TRD
), &CmdDescHost
, &CmdDescPhyAddr
, &CmdDescMapping
);
1970 if (EFI_ERROR (Status
)) {
1975 // Program the UTP Transfer Request List Base Address and UTP Transfer Request List
1976 // Base Address with a 64-bit address allocated at step 8.
1978 Status
= UfsMmioWrite32 (Private
, UFS_HC_UTRLBA_OFFSET
, (UINT32
)(UINTN
)CmdDescPhyAddr
);
1979 if (EFI_ERROR (Status
)) {
1983 Status
= UfsMmioWrite32 (Private
, UFS_HC_UTRLBAU_OFFSET
, (UINT32
)RShiftU64 ((UINT64
)CmdDescPhyAddr
, 32));
1984 if (EFI_ERROR (Status
)) {
1988 Private
->UtpTrlBase
= CmdDescHost
;
1989 Private
->Nutrs
= Nutrs
;
1990 Private
->TrlMapping
= CmdDescMapping
;
1993 // Enable the UTP Transfer Request List by setting the UTP Transfer Request List
1994 // RunStop Register (UTRLRSR) to '1'.
1996 Status
= UfsMmioWrite32 (Private
, UFS_HC_UTRLRSR_OFFSET
, UFS_HC_UTRLRSR
);
1997 if (EFI_ERROR (Status
)) {
2005 Initialize the UFS host controller.
2007 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
2009 @retval EFI_SUCCESS The Ufs Host Controller is initialized successfully.
2010 @retval Others A device error occurred while initializing the controller.
2015 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
2020 Status
= UfsEnableHostController (Private
);
2021 if (EFI_ERROR (Status
)) {
2022 DEBUG ((DEBUG_ERROR
, "UfsControllerInit: Enable Host Controller Fails, Status = %r\n", Status
));
2026 Status
= UfsDeviceDetection (Private
);
2027 if (EFI_ERROR (Status
)) {
2028 DEBUG ((DEBUG_ERROR
, "UfsControllerInit: Device Detection Fails, Status = %r\n", Status
));
2032 Status
= UfsInitTaskManagementRequestList (Private
);
2033 if (EFI_ERROR (Status
)) {
2034 DEBUG ((DEBUG_ERROR
, "UfsControllerInit: Task management list initialization Fails, Status = %r\n", Status
));
2038 Status
= UfsInitTransferRequestList (Private
);
2039 if (EFI_ERROR (Status
)) {
2040 DEBUG ((DEBUG_ERROR
, "UfsControllerInit: Transfer list initialization Fails, Status = %r\n", Status
));
2044 DEBUG ((DEBUG_INFO
, "UfsControllerInit Finished\n"));
2049 Stop the UFS host controller.
2051 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
2053 @retval EFI_SUCCESS The Ufs Host Controller is stopped successfully.
2054 @retval Others A device error occurred while stopping the controller.
2059 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
2066 // Enable the UTP Task Management Request List by setting the UTP Task Management
2067 // Request List RunStop Register (UTMRLRSR) to '1'.
2069 Status
= UfsMmioWrite32 (Private
, UFS_HC_UTMRLRSR_OFFSET
, 0);
2070 if (EFI_ERROR (Status
)) {
2075 // Enable the UTP Transfer Request List by setting the UTP Transfer Request List
2076 // RunStop Register (UTRLRSR) to '1'.
2078 Status
= UfsMmioWrite32 (Private
, UFS_HC_UTRLRSR_OFFSET
, 0);
2079 if (EFI_ERROR (Status
)) {
2084 // Write a 0 to the HCE register in order to disable the host controller.
2086 Status
= UfsMmioRead32 (Private
, UFS_HC_ENABLE_OFFSET
, &Data
);
2087 if (EFI_ERROR (Status
)) {
2090 ASSERT ((Data
& UFS_HC_HCE_EN
) == UFS_HC_HCE_EN
);
2092 Status
= UfsMmioWrite32 (Private
, UFS_HC_ENABLE_OFFSET
, 0);
2093 if (EFI_ERROR (Status
)) {
2098 // Wait until HCE is read as '0' before continuing.
2100 Status
= UfsWaitMemSet (Private
, UFS_HC_ENABLE_OFFSET
, UFS_HC_HCE_EN
, 0, UFS_TIMEOUT
);
2101 if (EFI_ERROR (Status
)) {
2102 return EFI_DEVICE_ERROR
;
2105 DEBUG ((DEBUG_INFO
, "UfsController is stopped\n"));
2112 Internal helper function which will signal the caller event and clean up
2115 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data
2117 @param[in] TransReq The pointer to the UFS_PASS_THRU_TRANS_REQ data
2124 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
2125 IN UFS_PASS_THRU_TRANS_REQ
*TransReq
2128 EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*UfsHc
;
2129 EFI_EVENT CallerEvent
;
2131 ASSERT ((Private
!= NULL
) && (TransReq
!= NULL
));
2133 UfsHc
= Private
->UfsHostController
;
2134 CallerEvent
= TransReq
->CallerEvent
;
2136 RemoveEntryList (&TransReq
->TransferList
);
2138 UfsHc
->Flush (UfsHc
);
2140 UfsStopExecCmd (Private
, TransReq
->Slot
);
2142 if (TransReq
->DataBufMapping
!= NULL
) {
2143 UfsHc
->Unmap (UfsHc
, TransReq
->DataBufMapping
);
2146 if (TransReq
->CmdDescMapping
!= NULL
) {
2147 UfsHc
->Unmap (UfsHc
, TransReq
->CmdDescMapping
);
2149 if (TransReq
->CmdDescHost
!= NULL
) {
2152 EFI_SIZE_TO_PAGES (TransReq
->CmdDescSize
),
2153 TransReq
->CmdDescHost
2157 FreePool (TransReq
);
2159 gBS
->SignalEvent (CallerEvent
);
2164 Call back function when the timer event is signaled.
2166 @param[in] Event The Event this notify function registered to.
2167 @param[in] Context Pointer to the context data registered to the Event.
2172 ProcessAsyncTaskList (
2177 UFS_PASS_THRU_PRIVATE_DATA
*Private
;
2179 LIST_ENTRY
*NextEntry
;
2180 UFS_PASS_THRU_TRANS_REQ
*TransReq
;
2181 EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
;
2182 UTP_RESPONSE_UPIU
*Response
;
2183 UINT16 SenseDataLen
;
2184 UINT32 ResTranCount
;
2189 Private
= (UFS_PASS_THRU_PRIVATE_DATA
*) Context
;
2193 // Check the entries in the async I/O queue are done or not.
2195 if (!IsListEmpty(&Private
->Queue
)) {
2196 EFI_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Private
->Queue
) {
2197 TransReq
= UFS_PASS_THRU_TRANS_REQ_FROM_THIS (Entry
);
2198 Packet
= TransReq
->Packet
;
2200 if ((SlotsMap
& (BIT0
<< TransReq
->Slot
)) != 0) {
2203 SlotsMap
|= BIT0
<< TransReq
->Slot
;
2205 Status
= UfsMmioRead32 (Private
, UFS_HC_UTRLDBR_OFFSET
, &Value
);
2206 if (EFI_ERROR (Status
)) {
2208 // TODO: Should find/add a proper host adapter return status for this
2211 Packet
->HostAdapterStatus
= EFI_EXT_SCSI_STATUS_HOST_ADAPTER_PHASE_ERROR
;
2212 DEBUG ((DEBUG_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p UfsMmioRead32() Error.\n", TransReq
->CallerEvent
));
2213 SignalCallerEvent (Private
, TransReq
);
2217 if ((Value
& (BIT0
<< TransReq
->Slot
)) != 0) {
2219 // Scsi cmd not finished yet.
2221 if (TransReq
->TimeoutRemain
> UFS_HC_ASYNC_TIMER
) {
2222 TransReq
->TimeoutRemain
-= UFS_HC_ASYNC_TIMER
;
2228 Packet
->HostAdapterStatus
= EFI_EXT_SCSI_STATUS_HOST_ADAPTER_TIMEOUT_COMMAND
;
2229 DEBUG ((DEBUG_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p EFI_TIMEOUT.\n", TransReq
->CallerEvent
));
2230 SignalCallerEvent (Private
, TransReq
);
2235 // Scsi cmd finished.
2237 // Get sense data if exists
2239 Response
= (UTP_RESPONSE_UPIU
*)((UINT8
*)TransReq
->CmdDescHost
+ TransReq
->Trd
->RuO
* sizeof (UINT32
));
2240 ASSERT (Response
!= NULL
);
2241 SenseDataLen
= Response
->SenseDataLen
;
2242 SwapLittleEndianToBigEndian ((UINT8
*)&SenseDataLen
, sizeof (UINT16
));
2244 if ((Packet
->SenseDataLength
!= 0) && (Packet
->SenseData
!= NULL
)) {
2245 CopyMem (Packet
->SenseData
, Response
->SenseData
, SenseDataLen
);
2246 Packet
->SenseDataLength
= (UINT8
)SenseDataLen
;
2250 // Check the transfer request result.
2252 Packet
->TargetStatus
= Response
->Status
;
2253 if (Response
->Response
!= 0) {
2254 DEBUG ((DEBUG_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p Target Failure.\n", TransReq
->CallerEvent
));
2255 SignalCallerEvent (Private
, TransReq
);
2259 if (TransReq
->Trd
->Ocs
== 0) {
2260 if (Packet
->DataDirection
== EFI_EXT_SCSI_DATA_DIRECTION_READ
) {
2261 if ((Response
->Flags
& BIT5
) == BIT5
) {
2262 ResTranCount
= Response
->ResTranCount
;
2263 SwapLittleEndianToBigEndian ((UINT8
*)&ResTranCount
, sizeof (UINT32
));
2264 Packet
->InTransferLength
-= ResTranCount
;
2267 if ((Response
->Flags
& BIT5
) == BIT5
) {
2268 ResTranCount
= Response
->ResTranCount
;
2269 SwapLittleEndianToBigEndian ((UINT8
*)&ResTranCount
, sizeof (UINT32
));
2270 Packet
->OutTransferLength
-= ResTranCount
;
2274 DEBUG ((DEBUG_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p Target Device Error.\n", TransReq
->CallerEvent
));
2275 SignalCallerEvent (Private
, TransReq
);
2279 DEBUG ((DEBUG_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p Success.\n", TransReq
->CallerEvent
));
2280 SignalCallerEvent (Private
, TransReq
);