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 - 2016, 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 ((EFI_D_VERBOSE
, "UIC configuration command fails - INVALID_MIB_ATTRIBUTE\n"));
163 DEBUG ((EFI_D_VERBOSE
, "UIC configuration command fails - INVALID_MIB_ATTRIBUTE_VALUE\n"));
166 DEBUG ((EFI_D_VERBOSE
, "UIC configuration command fails - READ_ONLY_MIB_ATTRIBUTE\n"));
169 DEBUG ((EFI_D_VERBOSE
, "UIC configuration command fails - WRITE_ONLY_MIB_ATTRIBUTE\n"));
172 DEBUG ((EFI_D_VERBOSE
, "UIC configuration command fails - BAD_INDEX\n"));
175 DEBUG ((EFI_D_VERBOSE
, "UIC configuration command fails - LOCKED_MIB_ATTRIBUTE\n"));
178 DEBUG ((EFI_D_VERBOSE
, "UIC configuration command fails - BAD_TEST_FEATURE_INDEX\n"));
181 DEBUG ((EFI_D_VERBOSE
, "UIC configuration command fails - PEER_COMMUNICATION_FAILURE\n"));
184 DEBUG ((EFI_D_VERBOSE
, "UIC configuration command fails - BUSY\n"));
187 DEBUG ((EFI_D_VERBOSE
, "UIC configuration command fails - DME_FAILURE\n"));
198 DEBUG ((EFI_D_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 ((EFI_D_VERBOSE
, "Query Response with Parameter Not Readable\n"));
223 DEBUG ((EFI_D_VERBOSE
, "Query Response with Parameter Not Writeable\n"));
226 DEBUG ((EFI_D_VERBOSE
, "Query Response with Parameter Already Written\n"));
229 DEBUG ((EFI_D_VERBOSE
, "Query Response with Invalid Length\n"));
232 DEBUG ((EFI_D_VERBOSE
, "Query Response with Invalid Value\n"));
235 DEBUG ((EFI_D_VERBOSE
, "Query Response with Invalid Selector\n"));
238 DEBUG ((EFI_D_VERBOSE
, "Query Response with Invalid Index\n"));
241 DEBUG ((EFI_D_VERBOSE
, "Query Response with Invalid Idn\n"));
244 DEBUG ((EFI_D_VERBOSE
, "Query Response with Invalid Opcode\n"));
247 DEBUG ((EFI_D_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 ((EFI_D_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
->UcdBa
= (UINT32
)RShiftU64 ((UINT64
)CmdDescPhyAddr
, 7);
555 Trd
->UcdBaU
= (UINT32
)RShiftU64 ((UINT64
)CmdDescPhyAddr
, 32);
556 Trd
->RuL
= (UINT16
)DivU64x32 ((UINT64
)ROUNDUP8 (sizeof (UTP_RESPONSE_UPIU
)), sizeof (UINT32
));
557 Trd
->RuO
= (UINT16
)DivU64x32 ((UINT64
)ROUNDUP8 (sizeof (UTP_COMMAND_UPIU
)), sizeof (UINT32
));
558 Trd
->PrdtL
= (UINT16
)PrdtNumber
;
559 Trd
->PrdtO
= (UINT16
)DivU64x32 ((UINT64
)(ROUNDUP8 (sizeof (UTP_COMMAND_UPIU
)) + ROUNDUP8 (sizeof (UTP_RESPONSE_UPIU
))), sizeof (UINT32
));
564 Allocate QUERY REQUEST/QUERY RESPONSE UPIU for filling UTP TRD's command descriptor field.
566 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
567 @param[in] Packet The pointer to the UFS_DEVICE_MANAGEMENT_REQUEST_PACKET data structure.
568 @param[in] Trd The pointer to the UTP Transfer Request Descriptor.
569 @param[out] CmdDescHost A pointer to store the base system memory address of the allocated range.
570 @param[out] CmdDescMapping A resulting value to pass to Unmap().
572 @retval EFI_SUCCESS The creation succeed.
573 @retval EFI_DEVICE_ERROR The creation failed.
574 @retval EFI_OUT_OF_RESOURCES The memory resource is insufficient.
575 @retval EFI_INVALID_PARAMETER The parameter passed in is invalid.
579 UfsCreateDMCommandDesc (
580 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
581 IN UFS_DEVICE_MANAGEMENT_REQUEST_PACKET
*Packet
,
583 OUT VOID
**CmdDescHost
,
584 OUT VOID
**CmdDescMapping
588 UTP_QUERY_REQ_UPIU
*QueryReqUpiu
;
593 EFI_PHYSICAL_ADDRESS CmdDescPhyAddr
;
596 ASSERT ((Private
!= NULL
) && (Packet
!= NULL
) && (Trd
!= NULL
));
598 Opcode
= Packet
->Opcode
;
599 if ((Opcode
> UtpQueryFuncOpcodeTogFlag
) || (Opcode
== UtpQueryFuncOpcodeNop
)) {
600 return EFI_INVALID_PARAMETER
;
603 DataDirection
= Packet
->DataDirection
;
604 if (DataDirection
== UfsDataIn
) {
605 DataSize
= Packet
->InTransferLength
;
606 Data
= Packet
->InDataBuffer
;
607 } else if (DataDirection
== UfsDataOut
) {
608 DataSize
= Packet
->OutTransferLength
;
609 Data
= Packet
->OutDataBuffer
;
615 if (((Opcode
!= UtpQueryFuncOpcodeSetFlag
) && (Opcode
!= UtpQueryFuncOpcodeClrFlag
) && (Opcode
!= UtpQueryFuncOpcodeTogFlag
))
616 && ((DataSize
== 0) || (Data
== NULL
))) {
617 return EFI_INVALID_PARAMETER
;
620 if (((Opcode
== UtpQueryFuncOpcodeSetFlag
) || (Opcode
== UtpQueryFuncOpcodeClrFlag
) || (Opcode
== UtpQueryFuncOpcodeTogFlag
))
621 && ((DataSize
!= 0) || (Data
!= NULL
))) {
622 return EFI_INVALID_PARAMETER
;
625 if ((Opcode
== UtpQueryFuncOpcodeWrAttr
) && (DataSize
!= sizeof (UINT32
))) {
626 return EFI_INVALID_PARAMETER
;
629 if ((Opcode
== UtpQueryFuncOpcodeWrDesc
) || (Opcode
== UtpQueryFuncOpcodeRdDesc
)) {
630 TotalLen
= ROUNDUP8 (sizeof (UTP_QUERY_REQ_UPIU
)) + ROUNDUP8 (sizeof (UTP_QUERY_RESP_UPIU
)) + ROUNDUP8 (DataSize
);
632 TotalLen
= ROUNDUP8 (sizeof (UTP_QUERY_REQ_UPIU
)) + ROUNDUP8 (sizeof (UTP_QUERY_RESP_UPIU
));
635 Status
= UfsAllocateAlignCommonBuffer (Private
, TotalLen
, CmdDescHost
, &CmdDescPhyAddr
, CmdDescMapping
);
636 if (EFI_ERROR (Status
)) {
641 // Initialize UTP QUERY REQUEST UPIU
643 QueryReqUpiu
= (UTP_QUERY_REQ_UPIU
*)*CmdDescHost
;
644 ASSERT (QueryReqUpiu
!= NULL
);
645 UfsInitQueryRequestUpiu (
657 // Fill UTP_TRD associated fields
658 // NOTE: Some UFS host controllers request the Query Response UPIU *MUST* be located at a 64-bit aligned boundary.
660 Trd
->Int
= UFS_INTERRUPT_COMMAND
;
661 Trd
->Dd
= DataDirection
;
662 Trd
->Ct
= UFS_STORAGE_COMMAND_TYPE
;
664 Trd
->UcdBa
= (UINT32
)RShiftU64 ((UINT64
)CmdDescPhyAddr
, 7);
665 Trd
->UcdBaU
= (UINT32
)RShiftU64 ((UINT64
)CmdDescPhyAddr
, 32);
666 if (Opcode
== UtpQueryFuncOpcodeWrDesc
) {
667 Trd
->RuL
= (UINT16
)DivU64x32 ((UINT64
)ROUNDUP8 (sizeof (UTP_QUERY_RESP_UPIU
)), sizeof (UINT32
));
668 Trd
->RuO
= (UINT16
)DivU64x32 ((UINT64
)(ROUNDUP8 (sizeof (UTP_QUERY_REQ_UPIU
)) + ROUNDUP8 (DataSize
)), sizeof (UINT32
));
670 Trd
->RuL
= (UINT16
)DivU64x32 ((UINT64
)(ROUNDUP8 (sizeof (UTP_QUERY_RESP_UPIU
)) + ROUNDUP8 (DataSize
)), sizeof (UINT32
));
671 Trd
->RuO
= (UINT16
)DivU64x32 ((UINT64
)ROUNDUP8 (sizeof (UTP_QUERY_REQ_UPIU
)), sizeof (UINT32
));
678 Allocate NOP IN and NOP OUT UPIU for filling UTP TRD's command descriptor field.
680 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
681 @param[in] Trd The pointer to the UTP Transfer Request Descriptor.
682 @param[out] CmdDescHost A pointer to store the base system memory address of the allocated range.
683 @param[out] CmdDescMapping A resulting value to pass to Unmap().
685 @retval EFI_SUCCESS The creation succeed.
686 @retval EFI_DEVICE_ERROR The creation failed.
687 @retval EFI_OUT_OF_RESOURCES The memory resource is insufficient.
691 UfsCreateNopCommandDesc (
692 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
694 OUT VOID
**CmdDescHost
,
695 OUT VOID
**CmdDescMapping
699 UTP_NOP_OUT_UPIU
*NopOutUpiu
;
701 EFI_PHYSICAL_ADDRESS CmdDescPhyAddr
;
703 ASSERT ((Private
!= NULL
) && (Trd
!= NULL
));
705 TotalLen
= ROUNDUP8 (sizeof (UTP_NOP_OUT_UPIU
)) + ROUNDUP8 (sizeof (UTP_NOP_IN_UPIU
));
706 Status
= UfsAllocateAlignCommonBuffer (Private
, TotalLen
, CmdDescHost
, &CmdDescPhyAddr
, CmdDescMapping
);
707 if (EFI_ERROR (Status
)) {
711 NopOutUpiu
= (UTP_NOP_OUT_UPIU
*)*CmdDescHost
;
712 ASSERT (NopOutUpiu
!= NULL
);
713 NopOutUpiu
->TaskTag
= Private
->TaskTag
++;
716 // Fill UTP_TRD associated fields
717 // NOTE: Some UFS host controllers request the Nop Out UPIU *MUST* be located at a 64-bit aligned boundary.
719 Trd
->Int
= UFS_INTERRUPT_COMMAND
;
721 Trd
->Ct
= UFS_STORAGE_COMMAND_TYPE
;
722 Trd
->UcdBa
= (UINT32
)RShiftU64 ((UINT64
)CmdDescPhyAddr
, 7);
723 Trd
->UcdBaU
= (UINT32
)RShiftU64 ((UINT64
)CmdDescPhyAddr
, 32);
724 Trd
->RuL
= (UINT16
)DivU64x32 ((UINT64
)ROUNDUP8 (sizeof (UTP_NOP_IN_UPIU
)), sizeof (UINT32
));
725 Trd
->RuO
= (UINT16
)DivU64x32 ((UINT64
)ROUNDUP8 (sizeof (UTP_NOP_OUT_UPIU
)), sizeof (UINT32
));
731 Find out available slot in transfer list of a UFS device.
733 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
734 @param[out] Slot The available slot.
736 @retval EFI_SUCCESS The available slot was found successfully.
737 @retval EFI_NOT_READY No slot is available at this moment.
741 UfsFindAvailableSlotInTrl (
742 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
751 ASSERT ((Private
!= NULL
) && (Slot
!= NULL
));
753 Status
= UfsMmioRead32 (Private
, UFS_HC_UTRLDBR_OFFSET
, &Data
);
754 if (EFI_ERROR (Status
)) {
758 Nutrs
= (UINT8
)((Private
->Capabilities
& UFS_HC_CAP_NUTRS
) + 1);
760 for (Index
= 0; Index
< Nutrs
; Index
++) {
761 if ((Data
& (BIT0
<< Index
)) == 0) {
767 return EFI_NOT_READY
;
771 Find out available slot in task management transfer list of a UFS device.
773 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
774 @param[out] Slot The available slot.
776 @retval EFI_SUCCESS The available slot was found successfully.
780 UfsFindAvailableSlotInTmrl (
781 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
785 ASSERT ((Private
!= NULL
) && (Slot
!= NULL
));
788 // The simplest algo to always use slot 0.
789 // TODO: enhance it to support async transfer with multiple slot.
797 Start specified slot in transfer list of a UFS device.
799 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
800 @param[in] Slot The slot to be started.
805 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
812 Status
= UfsMmioRead32 (Private
, UFS_HC_UTRLRSR_OFFSET
, &Data
);
813 if (EFI_ERROR (Status
)) {
817 if ((Data
& UFS_HC_UTRLRSR
) != UFS_HC_UTRLRSR
) {
818 Status
= UfsMmioWrite32 (Private
, UFS_HC_UTRLRSR_OFFSET
, UFS_HC_UTRLRSR
);
819 if (EFI_ERROR (Status
)) {
824 Status
= UfsMmioWrite32 (Private
, UFS_HC_UTRLDBR_OFFSET
, BIT0
<< Slot
);
825 if (EFI_ERROR (Status
)) {
833 Stop specified slot in transfer list of a UFS device.
835 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
836 @param[in] Slot The slot to be stop.
841 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
848 Status
= UfsMmioRead32 (Private
, UFS_HC_UTRLDBR_OFFSET
, &Data
);
849 if (EFI_ERROR (Status
)) {
853 if ((Data
& (BIT0
<< Slot
)) != 0) {
854 Status
= UfsMmioRead32 (Private
, UFS_HC_UTRLCLR_OFFSET
, &Data
);
855 if (EFI_ERROR (Status
)) {
859 Status
= UfsMmioWrite32 (Private
, UFS_HC_UTRLCLR_OFFSET
, Data
& ~(BIT0
<< Slot
));
860 if (EFI_ERROR (Status
)) {
869 Read or write specified device descriptor of a UFS device.
871 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
872 @param[in] Read The boolean variable to show r/w direction.
873 @param[in] DescId The ID of device descriptor.
874 @param[in] Index The Index of device descriptor.
875 @param[in] Selector The Selector of device descriptor.
876 @param[in, out] Descriptor The buffer of device descriptor to be read or written.
877 @param[in] DescSize The size of device descriptor buffer.
879 @retval EFI_SUCCESS The device descriptor was read/written successfully.
880 @retval EFI_DEVICE_ERROR A device error occurred while attempting to r/w the device descriptor.
881 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of r/w the device descriptor.
886 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
891 IN OUT VOID
*Descriptor
,
896 UFS_DEVICE_MANAGEMENT_REQUEST_PACKET Packet
;
899 UTP_QUERY_RESP_UPIU
*QueryResp
;
901 UINT16 ReturnDataSize
;
903 VOID
*CmdDescMapping
;
904 EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*UfsHc
;
906 ZeroMem (&Packet
, sizeof (UFS_DEVICE_MANAGEMENT_REQUEST_PACKET
));
909 Packet
.DataDirection
= UfsDataIn
;
910 Packet
.InDataBuffer
= Descriptor
;
911 Packet
.InTransferLength
= DescSize
;
912 Packet
.Opcode
= UtpQueryFuncOpcodeRdDesc
;
914 Packet
.DataDirection
= UfsDataOut
;
915 Packet
.OutDataBuffer
= Descriptor
;
916 Packet
.OutTransferLength
= DescSize
;
917 Packet
.Opcode
= UtpQueryFuncOpcodeWrDesc
;
919 Packet
.DescId
= DescId
;
920 Packet
.Index
= Index
;
921 Packet
.Selector
= Selector
;
922 Packet
.Timeout
= UFS_TIMEOUT
;
925 // Find out which slot of transfer request list is available.
927 Status
= UfsFindAvailableSlotInTrl (Private
, &Slot
);
928 if (EFI_ERROR (Status
)) {
932 Trd
= ((UTP_TRD
*)Private
->UtpTrlBase
) + Slot
;
934 // Fill transfer request descriptor to this slot.
936 Status
= UfsCreateDMCommandDesc (Private
, &Packet
, Trd
, &CmdDescHost
, &CmdDescMapping
);
937 if (EFI_ERROR (Status
)) {
942 // Check the transfer request result.
944 UfsHc
= Private
->UfsHostController
;
945 QueryResp
= (UTP_QUERY_RESP_UPIU
*)((UINT8
*)CmdDescHost
+ Trd
->RuO
* sizeof (UINT32
));
946 ASSERT (QueryResp
!= NULL
);
947 CmdDescSize
= Trd
->RuO
* sizeof (UINT32
) + Trd
->RuL
* sizeof (UINT32
);
950 // Start to execute the transfer request.
952 UfsStartExecCmd (Private
, Slot
);
955 // Wait for the completion of the transfer request.
957 Status
= UfsWaitMemSet (Private
, UFS_HC_UTRLDBR_OFFSET
, BIT0
, 0, Packet
.Timeout
);
958 if (EFI_ERROR (Status
)) {
962 if (QueryResp
->QueryResp
!= 0) {
963 DumpQueryResponseResult (QueryResp
->QueryResp
);
964 Status
= EFI_DEVICE_ERROR
;
969 ReturnDataSize
= QueryResp
->Tsf
.Length
;
970 SwapLittleEndianToBigEndian ((UINT8
*)&ReturnDataSize
, sizeof (UINT16
));
973 CopyMem (Packet
.InDataBuffer
, (QueryResp
+ 1), ReturnDataSize
);
974 Packet
.InTransferLength
= ReturnDataSize
;
976 Packet
.OutTransferLength
= ReturnDataSize
;
979 Status
= EFI_DEVICE_ERROR
;
983 UfsHc
->Flush (UfsHc
);
985 UfsStopExecCmd (Private
, Slot
);
987 if (CmdDescMapping
!= NULL
) {
988 UfsHc
->Unmap (UfsHc
, CmdDescMapping
);
990 if (CmdDescHost
!= NULL
) {
991 UfsHc
->FreeBuffer (UfsHc
, EFI_SIZE_TO_PAGES (CmdDescSize
), CmdDescHost
);
998 Read or write specified attribute of a UFS device.
1000 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1001 @param[in] Read The boolean variable to show r/w direction.
1002 @param[in] AttrId The ID of Attribute.
1003 @param[in] Index The Index of Attribute.
1004 @param[in] Selector The Selector of Attribute.
1005 @param[in, out] Attributes The value of Attribute to be read or written.
1007 @retval EFI_SUCCESS The Attribute was read/written successfully.
1008 @retval EFI_DEVICE_ERROR A device error occurred while attempting to r/w the Attribute.
1009 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of r/w the Attribute.
1014 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
1019 IN OUT UINT32
*Attributes
1023 UFS_DEVICE_MANAGEMENT_REQUEST_PACKET Packet
;
1026 UTP_QUERY_RESP_UPIU
*QueryResp
;
1030 VOID
*CmdDescMapping
;
1031 EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*UfsHc
;
1033 ZeroMem (&Packet
, sizeof (UFS_DEVICE_MANAGEMENT_REQUEST_PACKET
));
1036 Packet
.DataDirection
= UfsDataIn
;
1037 Packet
.Opcode
= UtpQueryFuncOpcodeRdAttr
;
1039 Packet
.DataDirection
= UfsDataOut
;
1040 Packet
.Opcode
= UtpQueryFuncOpcodeWrAttr
;
1042 Packet
.DescId
= AttrId
;
1043 Packet
.Index
= Index
;
1044 Packet
.Selector
= Selector
;
1045 Packet
.Timeout
= UFS_TIMEOUT
;
1048 // Find out which slot of transfer request list is available.
1050 Status
= UfsFindAvailableSlotInTrl (Private
, &Slot
);
1051 if (EFI_ERROR (Status
)) {
1055 Trd
= ((UTP_TRD
*)Private
->UtpTrlBase
) + Slot
;
1057 // Fill transfer request descriptor to this slot.
1059 Status
= UfsCreateDMCommandDesc (Private
, &Packet
, Trd
, &CmdDescHost
, &CmdDescMapping
);
1060 if (EFI_ERROR (Status
)) {
1065 // Check the transfer request result.
1067 UfsHc
= Private
->UfsHostController
;
1068 QueryResp
= (UTP_QUERY_RESP_UPIU
*)((UINT8
*)CmdDescHost
+ Trd
->RuO
* sizeof (UINT32
));
1069 ASSERT (QueryResp
!= NULL
);
1070 CmdDescSize
= Trd
->RuO
* sizeof (UINT32
) + Trd
->RuL
* sizeof (UINT32
);
1073 // Start to execute the transfer request.
1075 UfsStartExecCmd (Private
, Slot
);
1078 // Wait for the completion of the transfer request.
1080 Status
= UfsWaitMemSet (Private
, UFS_HC_UTRLDBR_OFFSET
, BIT0
, 0, Packet
.Timeout
);
1081 if (EFI_ERROR (Status
)) {
1085 if (QueryResp
->QueryResp
!= 0) {
1086 DumpQueryResponseResult (QueryResp
->QueryResp
);
1087 Status
= EFI_DEVICE_ERROR
;
1091 if (Trd
->Ocs
== 0) {
1092 ReturnData
= QueryResp
->Tsf
.Value
;
1093 SwapLittleEndianToBigEndian ((UINT8
*)&ReturnData
, sizeof (UINT32
));
1094 *Attributes
= ReturnData
;
1096 Status
= EFI_DEVICE_ERROR
;
1100 UfsHc
->Flush (UfsHc
);
1102 UfsStopExecCmd (Private
, Slot
);
1104 if (CmdDescMapping
!= NULL
) {
1105 UfsHc
->Unmap (UfsHc
, CmdDescMapping
);
1108 if (CmdDescHost
!= NULL
) {
1109 UfsHc
->FreeBuffer (UfsHc
, EFI_SIZE_TO_PAGES (CmdDescSize
), CmdDescHost
);
1116 Read or write specified flag of a UFS device.
1118 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1119 @param[in] Read The boolean variable to show r/w direction.
1120 @param[in] FlagId The ID of flag to be read or written.
1121 @param[in, out] Value The value to set or clear flag.
1123 @retval EFI_SUCCESS The flag was read/written successfully.
1124 @retval EFI_DEVICE_ERROR A device error occurred while attempting to r/w the flag.
1125 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of r/w the flag.
1130 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
1137 UFS_DEVICE_MANAGEMENT_REQUEST_PACKET Packet
;
1140 UTP_QUERY_RESP_UPIU
*QueryResp
;
1143 VOID
*CmdDescMapping
;
1144 EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*UfsHc
;
1146 if (Value
== NULL
) {
1147 return EFI_INVALID_PARAMETER
;
1150 ZeroMem (&Packet
, sizeof (UFS_DEVICE_MANAGEMENT_REQUEST_PACKET
));
1153 ASSERT (Value
!= NULL
);
1154 Packet
.DataDirection
= UfsDataIn
;
1155 Packet
.Opcode
= UtpQueryFuncOpcodeRdFlag
;
1157 Packet
.DataDirection
= UfsDataOut
;
1159 Packet
.Opcode
= UtpQueryFuncOpcodeSetFlag
;
1160 } else if (*Value
== 0) {
1161 Packet
.Opcode
= UtpQueryFuncOpcodeClrFlag
;
1163 return EFI_INVALID_PARAMETER
;
1166 Packet
.DescId
= FlagId
;
1168 Packet
.Selector
= 0;
1169 Packet
.Timeout
= UFS_TIMEOUT
;
1172 // Find out which slot of transfer request list is available.
1174 Status
= UfsFindAvailableSlotInTrl (Private
, &Slot
);
1175 if (EFI_ERROR (Status
)) {
1180 // Fill transfer request descriptor to this slot.
1182 Trd
= ((UTP_TRD
*)Private
->UtpTrlBase
) + Slot
;
1183 Status
= UfsCreateDMCommandDesc (Private
, &Packet
, Trd
, &CmdDescHost
, &CmdDescMapping
);
1184 if (EFI_ERROR (Status
)) {
1189 // Check the transfer request result.
1191 UfsHc
= Private
->UfsHostController
;
1192 QueryResp
= (UTP_QUERY_RESP_UPIU
*)((UINT8
*)CmdDescHost
+ Trd
->RuO
* sizeof (UINT32
));
1193 ASSERT (QueryResp
!= NULL
);
1194 CmdDescSize
= Trd
->RuO
* sizeof (UINT32
) + Trd
->RuL
* sizeof (UINT32
);
1197 // Start to execute the transfer request.
1199 UfsStartExecCmd (Private
, Slot
);
1202 // Wait for the completion of the transfer request.
1204 Status
= UfsWaitMemSet (Private
, UFS_HC_UTRLDBR_OFFSET
, BIT0
, 0, Packet
.Timeout
);
1205 if (EFI_ERROR (Status
)) {
1209 if (QueryResp
->QueryResp
!= 0) {
1210 DumpQueryResponseResult (QueryResp
->QueryResp
);
1211 Status
= EFI_DEVICE_ERROR
;
1215 if (Trd
->Ocs
== 0) {
1216 *Value
= (UINT8
)QueryResp
->Tsf
.Value
;
1218 Status
= EFI_DEVICE_ERROR
;
1222 UfsHc
->Flush (UfsHc
);
1224 UfsStopExecCmd (Private
, Slot
);
1226 if (CmdDescMapping
!= NULL
) {
1227 UfsHc
->Unmap (UfsHc
, CmdDescMapping
);
1229 if (CmdDescHost
!= NULL
) {
1230 UfsHc
->FreeBuffer (UfsHc
, EFI_SIZE_TO_PAGES (CmdDescSize
), CmdDescHost
);
1237 Set specified flag to 1 on a UFS device.
1239 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1240 @param[in] FlagId The ID of flag to be set.
1242 @retval EFI_SUCCESS The flag was set successfully.
1243 @retval EFI_DEVICE_ERROR A device error occurred while attempting to set the flag.
1244 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of setting the flag.
1249 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
1257 Status
= UfsRwFlags (Private
, FALSE
, FlagId
, &Value
);
1263 Clear specified flag to 0 on a UFS device.
1265 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1266 @param[in] FlagId The ID of flag to be cleared.
1268 @retval EFI_SUCCESS The flag was cleared successfully.
1269 @retval EFI_DEVICE_ERROR A device error occurred while attempting to clear the flag.
1270 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of clearing the flag.
1275 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
1283 Status
= UfsRwFlags (Private
, FALSE
, FlagId
, &Value
);
1289 Read specified flag from a UFS device.
1291 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1292 @param[in] FlagId The ID of flag to be read.
1293 @param[out] Value The flag's value.
1295 @retval EFI_SUCCESS The flag was read successfully.
1296 @retval EFI_DEVICE_ERROR A device error occurred while attempting to read the flag.
1297 @retval EFI_TIMEOUT A timeout occurred while waiting for the completion of reading the flag.
1302 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
1309 Status
= UfsRwFlags (Private
, TRUE
, FlagId
, Value
);
1315 Sends NOP IN cmd to a UFS device for initialization process request.
1316 For more details, please refer to UFS 2.0 spec Figure 13.3.
1318 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1320 @retval EFI_SUCCESS The NOP IN command was sent by the host. The NOP OUT response was
1321 received successfully.
1322 @retval EFI_DEVICE_ERROR A device error occurred while attempting to execute NOP IN command.
1323 @retval EFI_OUT_OF_RESOURCES The resource for transfer is not available.
1324 @retval EFI_TIMEOUT A timeout occurred while waiting for the NOP IN command to execute.
1329 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
1335 UTP_NOP_IN_UPIU
*NopInUpiu
;
1338 VOID
*CmdDescMapping
;
1339 EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*UfsHc
;
1342 // Find out which slot of transfer request list is available.
1344 Status
= UfsFindAvailableSlotInTrl (Private
, &Slot
);
1345 if (EFI_ERROR (Status
)) {
1349 Trd
= ((UTP_TRD
*)Private
->UtpTrlBase
) + Slot
;
1350 Status
= UfsCreateNopCommandDesc (Private
, Trd
, &CmdDescHost
, &CmdDescMapping
);
1351 if (EFI_ERROR (Status
)) {
1356 // Check the transfer request result.
1358 UfsHc
= Private
->UfsHostController
;
1359 NopInUpiu
= (UTP_NOP_IN_UPIU
*)((UINT8
*)CmdDescHost
+ Trd
->RuO
* sizeof (UINT32
));
1360 ASSERT (NopInUpiu
!= NULL
);
1361 CmdDescSize
= Trd
->RuO
* sizeof (UINT32
) + Trd
->RuL
* sizeof (UINT32
);
1364 // Start to execute the transfer request.
1366 UfsStartExecCmd (Private
, Slot
);
1369 // Wait for the completion of the transfer request.
1371 Status
= UfsWaitMemSet (Private
, UFS_HC_UTRLDBR_OFFSET
, BIT0
, 0, UFS_TIMEOUT
);
1372 if (EFI_ERROR (Status
)) {
1376 if (NopInUpiu
->Resp
!= 0) {
1377 Status
= EFI_DEVICE_ERROR
;
1379 Status
= EFI_SUCCESS
;
1383 UfsHc
->Flush (UfsHc
);
1385 UfsStopExecCmd (Private
, Slot
);
1387 if (CmdDescMapping
!= NULL
) {
1388 UfsHc
->Unmap (UfsHc
, CmdDescMapping
);
1390 if (CmdDescHost
!= NULL
) {
1391 UfsHc
->FreeBuffer (UfsHc
, EFI_SIZE_TO_PAGES (CmdDescSize
), CmdDescHost
);
1398 Sends a UFS-supported SCSI Request Packet to a UFS device that is attached to the UFS host controller.
1400 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1401 @param[in] Lun The LUN of the UFS device to send the SCSI Request Packet.
1402 @param[in, out] Packet A pointer to the SCSI Request Packet to send to a specified Lun of the
1404 @param[in] Event If nonblocking I/O is not supported then Event is ignored, and blocking
1405 I/O is performed. If Event is NULL, then blocking I/O is performed. If
1406 Event is not NULL and non blocking I/O is supported, then
1407 nonblocking I/O is performed, and Event will be signaled when the
1408 SCSI Request Packet completes.
1410 @retval EFI_SUCCESS The SCSI Request Packet was sent by the host. For bi-directional
1411 commands, InTransferLength bytes were transferred from
1412 InDataBuffer. For write and bi-directional commands,
1413 OutTransferLength bytes were transferred by
1415 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SCSI Request
1417 @retval EFI_OUT_OF_RESOURCES The resource for transfer is not available.
1418 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
1423 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
1425 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
,
1426 IN EFI_EVENT Event OPTIONAL
1430 UTP_RESPONSE_UPIU
*Response
;
1431 UINT16 SenseDataLen
;
1432 UINT32 ResTranCount
;
1434 EFI_PHYSICAL_ADDRESS DataBufPhyAddr
;
1437 EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*UfsHc
;
1438 EDKII_UFS_HOST_CONTROLLER_OPERATION Flag
;
1439 UTP_TR_PRD
*PrdtBase
;
1441 UFS_PASS_THRU_TRANS_REQ
*TransReq
;
1443 TransReq
= AllocateZeroPool (sizeof (UFS_PASS_THRU_TRANS_REQ
));
1444 if (TransReq
== NULL
) {
1445 return EFI_OUT_OF_RESOURCES
;
1448 TransReq
->Signature
= UFS_PASS_THRU_TRANS_REQ_SIG
;
1449 TransReq
->TimeoutRemain
= Packet
->Timeout
;
1451 UfsHc
= Private
->UfsHostController
;
1453 // Find out which slot of transfer request list is available.
1455 Status
= UfsFindAvailableSlotInTrl (Private
, &TransReq
->Slot
);
1456 if (EFI_ERROR (Status
)) {
1460 TransReq
->Trd
= ((UTP_TRD
*)Private
->UtpTrlBase
) + TransReq
->Slot
;
1463 // Fill transfer request descriptor to this slot.
1465 Status
= UfsCreateScsiCommandDesc (
1470 &TransReq
->CmdDescHost
,
1471 &TransReq
->CmdDescMapping
1473 if (EFI_ERROR (Status
)) {
1477 TransReq
->CmdDescSize
= TransReq
->Trd
->PrdtO
* sizeof (UINT32
) + TransReq
->Trd
->PrdtL
* sizeof (UTP_TR_PRD
);
1479 if (Packet
->DataDirection
== EFI_EXT_SCSI_DATA_DIRECTION_READ
) {
1480 DataBuf
= Packet
->InDataBuffer
;
1481 DataLen
= Packet
->InTransferLength
;
1482 Flag
= EdkiiUfsHcOperationBusMasterWrite
;
1484 DataBuf
= Packet
->OutDataBuffer
;
1485 DataLen
= Packet
->OutTransferLength
;
1486 Flag
= EdkiiUfsHcOperationBusMasterRead
;
1490 MapLength
= DataLen
;
1491 Status
= UfsHc
->Map (
1497 &TransReq
->DataBufMapping
1500 if (EFI_ERROR (Status
) || (DataLen
!= MapLength
)) {
1505 // Fill PRDT table of Command UPIU for executed SCSI cmd.
1507 PrdtBase
= (UTP_TR_PRD
*)((UINT8
*)TransReq
->CmdDescHost
+ ROUNDUP8 (sizeof (UTP_COMMAND_UPIU
)) + ROUNDUP8 (sizeof (UTP_RESPONSE_UPIU
)));
1508 ASSERT (PrdtBase
!= NULL
);
1509 UfsInitUtpPrdt (PrdtBase
, (VOID
*)(UINTN
)DataBufPhyAddr
, DataLen
);
1512 // Insert the async SCSI cmd to the Async I/O list
1514 if (Event
!= NULL
) {
1515 OldTpl
= gBS
->RaiseTPL (TPL_NOTIFY
);
1516 TransReq
->Packet
= Packet
;
1517 TransReq
->CallerEvent
= Event
;
1518 InsertTailList (&Private
->Queue
, &TransReq
->TransferList
);
1519 gBS
->RestoreTPL (OldTpl
);
1523 // Start to execute the transfer request.
1525 UfsStartExecCmd (Private
, TransReq
->Slot
);
1528 // Immediately return for async I/O.
1530 if (Event
!= NULL
) {
1535 // Wait for the completion of the transfer request.
1537 Status
= UfsWaitMemSet (Private
, UFS_HC_UTRLDBR_OFFSET
, BIT0
, 0, Packet
->Timeout
);
1538 if (EFI_ERROR (Status
)) {
1543 // Get sense data if exists
1545 Response
= (UTP_RESPONSE_UPIU
*)((UINT8
*)TransReq
->CmdDescHost
+ TransReq
->Trd
->RuO
* sizeof (UINT32
));
1546 ASSERT (Response
!= NULL
);
1547 SenseDataLen
= Response
->SenseDataLen
;
1548 SwapLittleEndianToBigEndian ((UINT8
*)&SenseDataLen
, sizeof (UINT16
));
1550 if ((Packet
->SenseDataLength
!= 0) && (Packet
->SenseData
!= NULL
)) {
1551 CopyMem (Packet
->SenseData
, Response
->SenseData
, SenseDataLen
);
1552 Packet
->SenseDataLength
= (UINT8
)SenseDataLen
;
1556 // Check the transfer request result.
1558 Packet
->TargetStatus
= Response
->Status
;
1559 if (Response
->Response
!= 0) {
1560 DEBUG ((EFI_D_ERROR
, "UfsExecScsiCmds() fails with Target Failure\n"));
1561 Status
= EFI_DEVICE_ERROR
;
1565 if (TransReq
->Trd
->Ocs
== 0) {
1566 if (Packet
->DataDirection
== EFI_EXT_SCSI_DATA_DIRECTION_READ
) {
1567 if ((Response
->Flags
& BIT5
) == BIT5
) {
1568 ResTranCount
= Response
->ResTranCount
;
1569 SwapLittleEndianToBigEndian ((UINT8
*)&ResTranCount
, sizeof (UINT32
));
1570 Packet
->InTransferLength
-= ResTranCount
;
1573 if ((Response
->Flags
& BIT5
) == BIT5
) {
1574 ResTranCount
= Response
->ResTranCount
;
1575 SwapLittleEndianToBigEndian ((UINT8
*)&ResTranCount
, sizeof (UINT32
));
1576 Packet
->OutTransferLength
-= ResTranCount
;
1580 Status
= EFI_DEVICE_ERROR
;
1584 UfsHc
->Flush (UfsHc
);
1586 UfsStopExecCmd (Private
, TransReq
->Slot
);
1588 if (TransReq
->DataBufMapping
!= NULL
) {
1589 UfsHc
->Unmap (UfsHc
, TransReq
->DataBufMapping
);
1593 if (TransReq
->CmdDescMapping
!= NULL
) {
1594 UfsHc
->Unmap (UfsHc
, TransReq
->CmdDescMapping
);
1596 if (TransReq
->CmdDescHost
!= NULL
) {
1597 UfsHc
->FreeBuffer (UfsHc
, EFI_SIZE_TO_PAGES (TransReq
->CmdDescSize
), TransReq
->CmdDescHost
);
1599 if (TransReq
!= NULL
) {
1600 FreePool (TransReq
);
1607 Sent UIC DME_LINKSTARTUP command to start the link startup procedure.
1609 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1610 @param[in] UicOpcode The opcode of the UIC command.
1611 @param[in] Arg1 The value for 1st argument of the UIC command.
1612 @param[in] Arg2 The value for 2nd argument of the UIC command.
1613 @param[in] Arg3 The value for 3rd argument of the UIC command.
1615 @return EFI_SUCCESS Successfully execute this UIC command and detect attached UFS device.
1616 @return EFI_DEVICE_ERROR Fail to execute this UIC command and detect attached UFS device.
1617 @return EFI_NOT_FOUND The presence of the UFS device isn't detected.
1621 UfsExecUicCommands (
1622 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
1632 Status
= UfsMmioRead32 (Private
, UFS_HC_IS_OFFSET
, &Data
);
1633 if (EFI_ERROR (Status
)) {
1637 if ((Data
& UFS_HC_IS_UCCS
) == UFS_HC_IS_UCCS
) {
1639 // Clear IS.BIT10 UIC Command Completion Status (UCCS) at first.
1641 Status
= UfsMmioWrite32 (Private
, UFS_HC_IS_OFFSET
, Data
);
1642 if (EFI_ERROR (Status
)) {
1648 // When programming UIC command registers, host software shall set the register UICCMD
1649 // only after all the UIC command argument registers (UICCMDARG1, UICCMDARG2 and UICCMDARG3)
1652 Status
= UfsMmioWrite32 (Private
, UFS_HC_UCMD_ARG1_OFFSET
, Arg1
);
1653 if (EFI_ERROR (Status
)) {
1657 Status
= UfsMmioWrite32 (Private
, UFS_HC_UCMD_ARG2_OFFSET
, Arg2
);
1658 if (EFI_ERROR (Status
)) {
1662 Status
= UfsMmioWrite32 (Private
, UFS_HC_UCMD_ARG3_OFFSET
, Arg3
);
1663 if (EFI_ERROR (Status
)) {
1668 // Host software shall only set the UICCMD if HCS.UCRDY is set to 1.
1670 Status
= UfsWaitMemSet (Private
, UFS_HC_STATUS_OFFSET
, UFS_HC_HCS_UCRDY
, UFS_HC_HCS_UCRDY
, UFS_TIMEOUT
);
1671 if (EFI_ERROR (Status
)) {
1675 Status
= UfsMmioWrite32 (Private
, UFS_HC_UIC_CMD_OFFSET
, (UINT32
)UicOpcode
);
1676 if (EFI_ERROR (Status
)) {
1681 // UFS 2.0 spec section 5.3.1 Offset:0x20 IS.Bit10 UIC Command Completion Status (UCCS)
1682 // This bit is set to '1' by the host controller upon completion of a UIC command.
1684 Status
= UfsWaitMemSet (Private
, UFS_HC_IS_OFFSET
, UFS_HC_IS_UCCS
, UFS_HC_IS_UCCS
, UFS_TIMEOUT
);
1685 if (EFI_ERROR (Status
)) {
1689 if (UicOpcode
!= UfsUicDmeReset
) {
1690 Status
= UfsMmioRead32 (Private
, UFS_HC_UCMD_ARG2_OFFSET
, &Data
);
1691 if (EFI_ERROR (Status
)) {
1694 if ((Data
& 0xFF) != 0) {
1696 DumpUicCmdExecResult (UicOpcode
, (UINT8
)(Data
& 0xFF));
1698 return EFI_DEVICE_ERROR
;
1703 // Check value of HCS.DP and make sure that there is a device attached to the Link.
1705 Status
= UfsMmioRead32 (Private
, UFS_HC_STATUS_OFFSET
, &Data
);
1706 if (EFI_ERROR (Status
)) {
1710 if ((Data
& UFS_HC_HCS_DP
) == 0) {
1711 Status
= UfsWaitMemSet (Private
, UFS_HC_IS_OFFSET
, UFS_HC_IS_ULSS
, UFS_HC_IS_ULSS
, UFS_TIMEOUT
);
1712 if (EFI_ERROR (Status
)) {
1713 return EFI_DEVICE_ERROR
;
1715 return EFI_NOT_FOUND
;
1718 DEBUG ((EFI_D_INFO
, "UfsPassThruDxe: found a attached UFS device\n"));
1724 Allocate common buffer for host and UFS bus master access simultaneously.
1726 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1727 @param[in] Size The length of buffer to be allocated.
1728 @param[out] CmdDescHost A pointer to store the base system memory address of the allocated range.
1729 @param[out] CmdDescPhyAddr The resulting map address for the UFS bus master to use to access the hosts CmdDescHost.
1730 @param[out] CmdDescMapping A resulting value to pass to Unmap().
1732 @retval EFI_SUCCESS The common buffer was allocated successfully.
1733 @retval EFI_DEVICE_ERROR The allocation fails.
1734 @retval EFI_OUT_OF_RESOURCES The memory resource is insufficient.
1738 UfsAllocateAlignCommonBuffer (
1739 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
1741 OUT VOID
**CmdDescHost
,
1742 OUT EFI_PHYSICAL_ADDRESS
*CmdDescPhyAddr
,
1743 OUT VOID
**CmdDescMapping
1748 BOOLEAN Is32BitAddr
;
1749 EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*UfsHc
;
1751 if ((Private
->Capabilities
& UFS_HC_CAP_64ADDR
) == UFS_HC_CAP_64ADDR
) {
1754 Is32BitAddr
= FALSE
;
1757 UfsHc
= Private
->UfsHostController
;
1758 Status
= UfsHc
->AllocateBuffer (
1761 EfiBootServicesData
,
1762 EFI_SIZE_TO_PAGES (Size
),
1766 if (EFI_ERROR (Status
)) {
1767 *CmdDescMapping
= NULL
;
1768 *CmdDescHost
= NULL
;
1769 *CmdDescPhyAddr
= 0;
1770 return EFI_OUT_OF_RESOURCES
;
1773 Bytes
= EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (Size
));
1774 Status
= UfsHc
->Map (
1776 EdkiiUfsHcOperationBusMasterCommonBuffer
,
1783 if (EFI_ERROR (Status
) || (Bytes
!= EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (Size
)))) {
1786 EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (Size
)),
1789 *CmdDescHost
= NULL
;
1790 return EFI_OUT_OF_RESOURCES
;
1793 if (Is32BitAddr
&& ((*CmdDescPhyAddr
) > 0x100000000ULL
)) {
1795 // The UFS host controller doesn't support 64bit addressing, so should not get a >4G UFS bus master address.
1803 EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (Size
)),
1806 *CmdDescMapping
= NULL
;
1807 *CmdDescHost
= NULL
;
1808 return EFI_DEVICE_ERROR
;
1811 ZeroMem (*CmdDescHost
, EFI_PAGES_TO_SIZE (EFI_SIZE_TO_PAGES (Size
)));
1816 Enable the UFS host controller for accessing.
1818 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1820 @retval EFI_SUCCESS The UFS host controller enabling was executed successfully.
1821 @retval EFI_DEVICE_ERROR A device error occurred while enabling the UFS host controller.
1825 UfsEnableHostController (
1826 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
1833 // UFS 2.0 spec section 7.1.1 - Host Controller Initialization
1835 // Reinitialize the UFS host controller if HCE bit of HC register is set.
1837 Status
= UfsMmioRead32 (Private
, UFS_HC_ENABLE_OFFSET
, &Data
);
1838 if (EFI_ERROR (Status
)) {
1842 if ((Data
& UFS_HC_HCE_EN
) == UFS_HC_HCE_EN
) {
1844 // Write a 0 to the HCE register at first to disable the host controller.
1846 Status
= UfsMmioWrite32 (Private
, UFS_HC_ENABLE_OFFSET
, 0);
1847 if (EFI_ERROR (Status
)) {
1851 // Wait until HCE is read as '0' before continuing.
1853 Status
= UfsWaitMemSet (Private
, UFS_HC_ENABLE_OFFSET
, UFS_HC_HCE_EN
, 0, UFS_TIMEOUT
);
1854 if (EFI_ERROR (Status
)) {
1855 return EFI_DEVICE_ERROR
;
1860 // Write a 1 to the HCE register to enable the UFS host controller.
1862 Status
= UfsMmioWrite32 (Private
, UFS_HC_ENABLE_OFFSET
, UFS_HC_HCE_EN
);
1863 if (EFI_ERROR (Status
)) {
1868 // Wait until HCE is read as '1' before continuing.
1870 Status
= UfsWaitMemSet (Private
, UFS_HC_ENABLE_OFFSET
, UFS_HC_HCE_EN
, UFS_HC_HCE_EN
, UFS_TIMEOUT
);
1871 if (EFI_ERROR (Status
)) {
1872 return EFI_DEVICE_ERROR
;
1879 Detect if a UFS device attached.
1881 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1883 @retval EFI_SUCCESS The UFS device detection was executed successfully.
1884 @retval EFI_NOT_FOUND Not found a UFS device attached.
1885 @retval EFI_DEVICE_ERROR A device error occurred while detecting the UFS device.
1889 UfsDeviceDetection (
1890 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
1897 // Start UFS device detection.
1898 // Try up to 3 times for establishing data link with device.
1900 for (Retry
= 0; Retry
< 3; Retry
++) {
1901 Status
= UfsExecUicCommands (Private
, UfsUicDmeLinkStartup
, 0, 0, 0);
1902 if (!EFI_ERROR (Status
)) {
1906 if (Status
== EFI_NOT_FOUND
) {
1910 return EFI_DEVICE_ERROR
;
1914 return EFI_NOT_FOUND
;
1921 Initialize UFS task management request list related h/w context.
1923 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1925 @retval EFI_SUCCESS The UFS task management list was initialzed successfully.
1926 @retval EFI_DEVICE_ERROR The initialization fails.
1930 UfsInitTaskManagementRequestList (
1931 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
1937 EFI_PHYSICAL_ADDRESS CmdDescPhyAddr
;
1938 VOID
*CmdDescMapping
;
1942 // Initial h/w and s/w context for future operations.
1945 CmdDescMapping
= NULL
;
1948 Status
= UfsMmioRead32 (Private
, UFS_HC_CAP_OFFSET
, &Data
);
1949 if (EFI_ERROR (Status
)) {
1953 Private
->Capabilities
= Data
;
1956 // Allocate and initialize UTP Task Management Request List.
1958 Nutmrs
= (UINT8
) (RShiftU64 ((Private
->Capabilities
& UFS_HC_CAP_NUTMRS
), 16) + 1);
1959 Status
= UfsAllocateAlignCommonBuffer (Private
, Nutmrs
* sizeof (UTP_TMRD
), &CmdDescHost
, &CmdDescPhyAddr
, &CmdDescMapping
);
1960 if (EFI_ERROR (Status
)) {
1965 // Program the UTP Task Management Request List Base Address and UTP Task Management
1966 // Request List Base Address with a 64-bit address allocated at step 6.
1968 Status
= UfsMmioWrite32 (Private
, UFS_HC_UTMRLBA_OFFSET
, (UINT32
)(UINTN
)CmdDescPhyAddr
);
1969 if (EFI_ERROR (Status
)) {
1973 Status
= UfsMmioWrite32 (Private
, UFS_HC_UTMRLBAU_OFFSET
, (UINT32
)RShiftU64 ((UINT64
)CmdDescPhyAddr
, 32));
1974 if (EFI_ERROR (Status
)) {
1977 Private
->UtpTmrlBase
= CmdDescHost
;
1978 Private
->Nutmrs
= Nutmrs
;
1979 Private
->TmrlMapping
= CmdDescMapping
;
1982 // Enable the UTP Task Management Request List by setting the UTP Task Management
1983 // Request List RunStop Register (UTMRLRSR) to '1'.
1985 Status
= UfsMmioWrite32 (Private
, UFS_HC_UTMRLRSR_OFFSET
, UFS_HC_UTMRLRSR
);
1986 if (EFI_ERROR (Status
)) {
1994 Initialize UFS transfer request list related h/w context.
1996 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
1998 @retval EFI_SUCCESS The UFS transfer list was initialzed successfully.
1999 @retval EFI_DEVICE_ERROR The initialization fails.
2003 UfsInitTransferRequestList (
2004 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
2010 EFI_PHYSICAL_ADDRESS CmdDescPhyAddr
;
2011 VOID
*CmdDescMapping
;
2015 // Initial h/w and s/w context for future operations.
2018 CmdDescMapping
= NULL
;
2021 Status
= UfsMmioRead32 (Private
, UFS_HC_CAP_OFFSET
, &Data
);
2022 if (EFI_ERROR (Status
)) {
2026 Private
->Capabilities
= Data
;
2029 // Allocate and initialize UTP Transfer Request List.
2031 Nutrs
= (UINT8
)((Private
->Capabilities
& UFS_HC_CAP_NUTRS
) + 1);
2032 Status
= UfsAllocateAlignCommonBuffer (Private
, Nutrs
* sizeof (UTP_TRD
), &CmdDescHost
, &CmdDescPhyAddr
, &CmdDescMapping
);
2033 if (EFI_ERROR (Status
)) {
2038 // Program the UTP Transfer Request List Base Address and UTP Transfer Request List
2039 // Base Address with a 64-bit address allocated at step 8.
2041 Status
= UfsMmioWrite32 (Private
, UFS_HC_UTRLBA_OFFSET
, (UINT32
)(UINTN
)CmdDescPhyAddr
);
2042 if (EFI_ERROR (Status
)) {
2046 Status
= UfsMmioWrite32 (Private
, UFS_HC_UTRLBAU_OFFSET
, (UINT32
)RShiftU64 ((UINT64
)CmdDescPhyAddr
, 32));
2047 if (EFI_ERROR (Status
)) {
2051 Private
->UtpTrlBase
= CmdDescHost
;
2052 Private
->Nutrs
= Nutrs
;
2053 Private
->TrlMapping
= CmdDescMapping
;
2056 // Enable the UTP Transfer Request List by setting the UTP Transfer Request List
2057 // RunStop Register (UTRLRSR) to '1'.
2059 Status
= UfsMmioWrite32 (Private
, UFS_HC_UTRLRSR_OFFSET
, UFS_HC_UTRLRSR
);
2060 if (EFI_ERROR (Status
)) {
2068 Initialize the UFS host controller.
2070 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
2072 @retval EFI_SUCCESS The Ufs Host Controller is initialized successfully.
2073 @retval Others A device error occurred while initializing the controller.
2078 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
2083 Status
= UfsEnableHostController (Private
);
2084 if (EFI_ERROR (Status
)) {
2085 DEBUG ((EFI_D_ERROR
, "UfsControllerInit: Enable Host Controller Fails, Status = %r\n", Status
));
2089 Status
= UfsDeviceDetection (Private
);
2090 if (EFI_ERROR (Status
)) {
2091 DEBUG ((EFI_D_ERROR
, "UfsControllerInit: Device Detection Fails, Status = %r\n", Status
));
2095 Status
= UfsInitTaskManagementRequestList (Private
);
2096 if (EFI_ERROR (Status
)) {
2097 DEBUG ((EFI_D_ERROR
, "UfsControllerInit: Task management list initialization Fails, Status = %r\n", Status
));
2101 Status
= UfsInitTransferRequestList (Private
);
2102 if (EFI_ERROR (Status
)) {
2103 DEBUG ((EFI_D_ERROR
, "UfsControllerInit: Transfer list initialization Fails, Status = %r\n", Status
));
2107 DEBUG ((EFI_D_INFO
, "UfsControllerInit Finished\n"));
2112 Stop the UFS host controller.
2114 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data structure.
2116 @retval EFI_SUCCESS The Ufs Host Controller is stopped successfully.
2117 @retval Others A device error occurred while stopping the controller.
2122 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
2129 // Enable the UTP Task Management Request List by setting the UTP Task Management
2130 // Request List RunStop Register (UTMRLRSR) to '1'.
2132 Status
= UfsMmioWrite32 (Private
, UFS_HC_UTMRLRSR_OFFSET
, 0);
2133 if (EFI_ERROR (Status
)) {
2138 // Enable the UTP Transfer Request List by setting the UTP Transfer Request List
2139 // RunStop Register (UTRLRSR) to '1'.
2141 Status
= UfsMmioWrite32 (Private
, UFS_HC_UTRLRSR_OFFSET
, 0);
2142 if (EFI_ERROR (Status
)) {
2147 // Write a 0 to the HCE register in order to disable the host controller.
2149 Status
= UfsMmioRead32 (Private
, UFS_HC_ENABLE_OFFSET
, &Data
);
2150 if (EFI_ERROR (Status
)) {
2153 ASSERT ((Data
& UFS_HC_HCE_EN
) == UFS_HC_HCE_EN
);
2155 Status
= UfsMmioWrite32 (Private
, UFS_HC_ENABLE_OFFSET
, 0);
2156 if (EFI_ERROR (Status
)) {
2161 // Wait until HCE is read as '0' before continuing.
2163 Status
= UfsWaitMemSet (Private
, UFS_HC_ENABLE_OFFSET
, UFS_HC_HCE_EN
, 0, UFS_TIMEOUT
);
2164 if (EFI_ERROR (Status
)) {
2165 return EFI_DEVICE_ERROR
;
2168 DEBUG ((EFI_D_INFO
, "UfsController is stopped\n"));
2175 Internal helper function which will signal the caller event and clean up
2178 @param[in] Private The pointer to the UFS_PASS_THRU_PRIVATE_DATA data
2180 @param[in] TransReq The pointer to the UFS_PASS_THRU_TRANS_REQ data
2187 IN UFS_PASS_THRU_PRIVATE_DATA
*Private
,
2188 IN UFS_PASS_THRU_TRANS_REQ
*TransReq
2191 EDKII_UFS_HOST_CONTROLLER_PROTOCOL
*UfsHc
;
2192 EFI_EVENT CallerEvent
;
2194 ASSERT ((Private
!= NULL
) && (TransReq
!= NULL
));
2196 UfsHc
= Private
->UfsHostController
;
2197 CallerEvent
= TransReq
->CallerEvent
;
2199 RemoveEntryList (&TransReq
->TransferList
);
2201 UfsHc
->Flush (UfsHc
);
2203 UfsStopExecCmd (Private
, TransReq
->Slot
);
2205 if (TransReq
->DataBufMapping
!= NULL
) {
2206 UfsHc
->Unmap (UfsHc
, TransReq
->DataBufMapping
);
2209 if (TransReq
->CmdDescMapping
!= NULL
) {
2210 UfsHc
->Unmap (UfsHc
, TransReq
->CmdDescMapping
);
2212 if (TransReq
->CmdDescHost
!= NULL
) {
2215 EFI_SIZE_TO_PAGES (TransReq
->CmdDescSize
),
2216 TransReq
->CmdDescHost
2220 FreePool (TransReq
);
2222 gBS
->SignalEvent (CallerEvent
);
2227 Call back function when the timer event is signaled.
2229 @param[in] Event The Event this notify function registered to.
2230 @param[in] Context Pointer to the context data registered to the Event.
2235 ProcessAsyncTaskList (
2240 UFS_PASS_THRU_PRIVATE_DATA
*Private
;
2242 LIST_ENTRY
*NextEntry
;
2243 UFS_PASS_THRU_TRANS_REQ
*TransReq
;
2244 EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
*Packet
;
2245 UTP_RESPONSE_UPIU
*Response
;
2246 UINT16 SenseDataLen
;
2247 UINT32 ResTranCount
;
2252 Private
= (UFS_PASS_THRU_PRIVATE_DATA
*) Context
;
2256 // Check the entries in the async I/O queue are done or not.
2258 if (!IsListEmpty(&Private
->Queue
)) {
2259 EFI_LIST_FOR_EACH_SAFE (Entry
, NextEntry
, &Private
->Queue
) {
2260 TransReq
= UFS_PASS_THRU_TRANS_REQ_FROM_THIS (Entry
);
2261 Packet
= TransReq
->Packet
;
2263 if ((SlotsMap
& (BIT0
<< TransReq
->Slot
)) != 0) {
2266 SlotsMap
|= BIT0
<< TransReq
->Slot
;
2268 Status
= UfsMmioRead32 (Private
, UFS_HC_UTRLDBR_OFFSET
, &Value
);
2269 if (EFI_ERROR (Status
)) {
2271 // TODO: Should find/add a proper host adapter return status for this
2274 Packet
->HostAdapterStatus
= EFI_EXT_SCSI_STATUS_HOST_ADAPTER_PHASE_ERROR
;
2275 DEBUG ((EFI_D_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p UfsMmioRead32() Error.\n", TransReq
->CallerEvent
));
2276 SignalCallerEvent (Private
, TransReq
);
2280 if ((Value
& (BIT0
<< TransReq
->Slot
)) != 0) {
2282 // Scsi cmd not finished yet.
2284 if (TransReq
->TimeoutRemain
> UFS_HC_ASYNC_TIMER
) {
2285 TransReq
->TimeoutRemain
-= UFS_HC_ASYNC_TIMER
;
2291 Packet
->HostAdapterStatus
= EFI_EXT_SCSI_STATUS_HOST_ADAPTER_TIMEOUT_COMMAND
;
2292 DEBUG ((EFI_D_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p EFI_TIMEOUT.\n", TransReq
->CallerEvent
));
2293 SignalCallerEvent (Private
, TransReq
);
2298 // Scsi cmd finished.
2300 // Get sense data if exists
2302 Response
= (UTP_RESPONSE_UPIU
*)((UINT8
*)TransReq
->CmdDescHost
+ TransReq
->Trd
->RuO
* sizeof (UINT32
));
2303 ASSERT (Response
!= NULL
);
2304 SenseDataLen
= Response
->SenseDataLen
;
2305 SwapLittleEndianToBigEndian ((UINT8
*)&SenseDataLen
, sizeof (UINT16
));
2307 if ((Packet
->SenseDataLength
!= 0) && (Packet
->SenseData
!= NULL
)) {
2308 CopyMem (Packet
->SenseData
, Response
->SenseData
, SenseDataLen
);
2309 Packet
->SenseDataLength
= (UINT8
)SenseDataLen
;
2313 // Check the transfer request result.
2315 Packet
->TargetStatus
= Response
->Status
;
2316 if (Response
->Response
!= 0) {
2317 DEBUG ((EFI_D_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p Target Failure.\n", TransReq
->CallerEvent
));
2318 SignalCallerEvent (Private
, TransReq
);
2322 if (TransReq
->Trd
->Ocs
== 0) {
2323 if (Packet
->DataDirection
== EFI_EXT_SCSI_DATA_DIRECTION_READ
) {
2324 if ((Response
->Flags
& BIT5
) == BIT5
) {
2325 ResTranCount
= Response
->ResTranCount
;
2326 SwapLittleEndianToBigEndian ((UINT8
*)&ResTranCount
, sizeof (UINT32
));
2327 Packet
->InTransferLength
-= ResTranCount
;
2330 if ((Response
->Flags
& BIT5
) == BIT5
) {
2331 ResTranCount
= Response
->ResTranCount
;
2332 SwapLittleEndianToBigEndian ((UINT8
*)&ResTranCount
, sizeof (UINT32
));
2333 Packet
->OutTransferLength
-= ResTranCount
;
2337 DEBUG ((EFI_D_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p Target Device Error.\n", TransReq
->CallerEvent
));
2338 SignalCallerEvent (Private
, TransReq
);
2342 DEBUG ((EFI_D_VERBOSE
, "ProcessAsyncTaskList(): Signal Event %p Success.\n", TransReq
->CallerEvent
));
2343 SignalCallerEvent (Private
, TransReq
);