2 NvmExpressDxe driver is used to manage non-volatile memory subsystem which follows
3 NVM Express specification.
5 Copyright (c) 2013 - 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 "NvmExpress.h"
19 Read some sectors from the device.
21 @param Device The pointer to the NVME_DEVICE_PRIVATE_DATA data structure.
22 @param Buffer The buffer used to store the data read from the device.
23 @param Lba The start block number.
24 @param Blocks Total block number to be read.
26 @retval EFI_SUCCESS Datum are read from the device.
27 @retval Others Fail to read all the datum.
32 IN NVME_DEVICE_PRIVATE_DATA
*Device
,
38 NVME_CONTROLLER_PRIVATE_DATA
*Private
;
40 EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET CommandPacket
;
41 EFI_NVM_EXPRESS_COMMAND Command
;
42 EFI_NVM_EXPRESS_COMPLETION Completion
;
46 Private
= Device
->Controller
;
47 BlockSize
= Device
->Media
.BlockSize
;
48 Bytes
= Blocks
* BlockSize
;
50 ZeroMem (&CommandPacket
, sizeof(EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET
));
51 ZeroMem (&Command
, sizeof(EFI_NVM_EXPRESS_COMMAND
));
52 ZeroMem (&Completion
, sizeof(EFI_NVM_EXPRESS_COMPLETION
));
54 CommandPacket
.NvmeCmd
= &Command
;
55 CommandPacket
.NvmeCompletion
= &Completion
;
57 CommandPacket
.NvmeCmd
->Cdw0
.Opcode
= NVME_IO_READ_OPC
;
58 CommandPacket
.NvmeCmd
->Nsid
= Device
->NamespaceId
;
59 CommandPacket
.TransferBuffer
= (VOID
*)(UINTN
)Buffer
;
61 CommandPacket
.TransferLength
= Bytes
;
62 CommandPacket
.CommandTimeout
= NVME_GENERIC_TIMEOUT
;
63 CommandPacket
.QueueType
= NVME_IO_QUEUE
;
65 CommandPacket
.NvmeCmd
->Cdw10
= (UINT32
)Lba
;
66 CommandPacket
.NvmeCmd
->Cdw11
= (UINT32
)RShiftU64(Lba
, 32);
67 CommandPacket
.NvmeCmd
->Cdw12
= (Blocks
- 1) & 0xFFFF;
69 CommandPacket
.NvmeCmd
->Flags
= CDW10_VALID
| CDW11_VALID
| CDW12_VALID
;
71 Status
= Private
->Passthru
.PassThru (
82 Write some sectors to the device.
84 @param Device The pointer to the NVME_DEVICE_PRIVATE_DATA data structure.
85 @param Buffer The buffer to be written into the device.
86 @param Lba The start block number.
87 @param Blocks Total block number to be written.
89 @retval EFI_SUCCESS Datum are written into the buffer.
90 @retval Others Fail to write all the datum.
95 IN NVME_DEVICE_PRIVATE_DATA
*Device
,
101 NVME_CONTROLLER_PRIVATE_DATA
*Private
;
102 EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET CommandPacket
;
103 EFI_NVM_EXPRESS_COMMAND Command
;
104 EFI_NVM_EXPRESS_COMPLETION Completion
;
109 Private
= Device
->Controller
;
110 BlockSize
= Device
->Media
.BlockSize
;
111 Bytes
= Blocks
* BlockSize
;
113 ZeroMem (&CommandPacket
, sizeof(EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET
));
114 ZeroMem (&Command
, sizeof(EFI_NVM_EXPRESS_COMMAND
));
115 ZeroMem (&Completion
, sizeof(EFI_NVM_EXPRESS_COMPLETION
));
117 CommandPacket
.NvmeCmd
= &Command
;
118 CommandPacket
.NvmeCompletion
= &Completion
;
120 CommandPacket
.NvmeCmd
->Cdw0
.Opcode
= NVME_IO_WRITE_OPC
;
121 CommandPacket
.NvmeCmd
->Nsid
= Device
->NamespaceId
;
122 CommandPacket
.TransferBuffer
= (VOID
*)(UINTN
)Buffer
;
124 CommandPacket
.TransferLength
= Bytes
;
125 CommandPacket
.CommandTimeout
= NVME_GENERIC_TIMEOUT
;
126 CommandPacket
.QueueType
= NVME_IO_QUEUE
;
128 CommandPacket
.NvmeCmd
->Cdw10
= (UINT32
)Lba
;
129 CommandPacket
.NvmeCmd
->Cdw11
= (UINT32
)RShiftU64(Lba
, 32);
130 CommandPacket
.NvmeCmd
->Cdw12
= (Blocks
- 1) & 0xFFFF;
132 CommandPacket
.MetadataBuffer
= NULL
;
133 CommandPacket
.MetadataLength
= 0;
135 CommandPacket
.NvmeCmd
->Flags
= CDW10_VALID
| CDW11_VALID
| CDW12_VALID
;
137 Status
= Private
->Passthru
.PassThru (
148 Read some blocks from the device.
150 @param Device The pointer to the NVME_DEVICE_PRIVATE_DATA data structure.
151 @param Buffer The buffer used to store the data read from the device.
152 @param Lba The start block number.
153 @param Blocks Total block number to be read.
155 @retval EFI_SUCCESS Datum are read from the device.
156 @retval Others Fail to read all the datum.
161 IN NVME_DEVICE_PRIVATE_DATA
*Device
,
169 NVME_CONTROLLER_PRIVATE_DATA
*Private
;
170 UINT32 MaxTransferBlocks
;
173 Status
= EFI_SUCCESS
;
174 Private
= Device
->Controller
;
175 BlockSize
= Device
->Media
.BlockSize
;
176 OrginalBlocks
= Blocks
;
178 if (Private
->ControllerData
->Mdts
!= 0) {
179 MaxTransferBlocks
= (1 << (Private
->ControllerData
->Mdts
)) * (1 << (Private
->Cap
.Mpsmin
+ 12)) / BlockSize
;
181 MaxTransferBlocks
= 1024;
185 if (Blocks
> MaxTransferBlocks
) {
186 Status
= ReadSectors (Device
, (UINT64
)(UINTN
)Buffer
, Lba
, MaxTransferBlocks
);
188 Blocks
-= MaxTransferBlocks
;
189 Buffer
= (VOID
*)(UINTN
)((UINT64
)(UINTN
)Buffer
+ MaxTransferBlocks
* BlockSize
);
190 Lba
+= MaxTransferBlocks
;
192 Status
= ReadSectors (Device
, (UINT64
)(UINTN
)Buffer
, Lba
, (UINT32
)Blocks
);
196 if (EFI_ERROR(Status
)) {
201 DEBUG ((EFI_D_INFO
, "NvmeRead() Lba = 0x%08x, Original = 0x%08x, Remaining = 0x%08x, BlockSize = 0x%x Status = %r\n", Lba
, OrginalBlocks
, Blocks
, BlockSize
, Status
));
207 Write some blocks to the device.
209 @param Device The pointer to the NVME_DEVICE_PRIVATE_DATA data structure.
210 @param Buffer The buffer to be written into the device.
211 @param Lba The start block number.
212 @param Blocks Total block number to be written.
214 @retval EFI_SUCCESS Datum are written into the buffer.
215 @retval Others Fail to write all the datum.
220 IN NVME_DEVICE_PRIVATE_DATA
*Device
,
228 NVME_CONTROLLER_PRIVATE_DATA
*Private
;
229 UINT32 MaxTransferBlocks
;
232 Status
= EFI_SUCCESS
;
233 Private
= Device
->Controller
;
234 BlockSize
= Device
->Media
.BlockSize
;
235 OrginalBlocks
= Blocks
;
237 if (Private
->ControllerData
->Mdts
!= 0) {
238 MaxTransferBlocks
= (1 << (Private
->ControllerData
->Mdts
)) * (1 << (Private
->Cap
.Mpsmin
+ 12)) / BlockSize
;
240 MaxTransferBlocks
= 1024;
244 if (Blocks
> MaxTransferBlocks
) {
245 Status
= WriteSectors (Device
, (UINT64
)(UINTN
)Buffer
, Lba
, MaxTransferBlocks
);
247 Blocks
-= MaxTransferBlocks
;
248 Buffer
= (VOID
*)(UINTN
)((UINT64
)(UINTN
)Buffer
+ MaxTransferBlocks
* BlockSize
);
249 Lba
+= MaxTransferBlocks
;
251 Status
= WriteSectors (Device
, (UINT64
)(UINTN
)Buffer
, Lba
, (UINT32
)Blocks
);
255 if (EFI_ERROR(Status
)) {
260 DEBUG ((EFI_D_INFO
, "NvmeWrite() Lba = 0x%08x, Original = 0x%08x, Remaining = 0x%08x, BlockSize = 0x%x Status = %r\n", Lba
, OrginalBlocks
, Blocks
, BlockSize
, Status
));
266 Flushes all modified data to the device.
268 @param Device The pointer to the NVME_DEVICE_PRIVATE_DATA data structure.
270 @retval EFI_SUCCESS Datum are written into the buffer.
271 @retval Others Fail to write all the datum.
276 IN NVME_DEVICE_PRIVATE_DATA
*Device
279 NVME_CONTROLLER_PRIVATE_DATA
*Private
;
280 EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET CommandPacket
;
281 EFI_NVM_EXPRESS_COMMAND Command
;
282 EFI_NVM_EXPRESS_COMPLETION Completion
;
285 Private
= Device
->Controller
;
287 ZeroMem (&CommandPacket
, sizeof(EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET
));
288 ZeroMem (&Command
, sizeof(EFI_NVM_EXPRESS_COMMAND
));
289 ZeroMem (&Completion
, sizeof(EFI_NVM_EXPRESS_COMPLETION
));
291 CommandPacket
.NvmeCmd
= &Command
;
292 CommandPacket
.NvmeCompletion
= &Completion
;
294 CommandPacket
.NvmeCmd
->Cdw0
.Opcode
= NVME_IO_FLUSH_OPC
;
295 CommandPacket
.NvmeCmd
->Nsid
= Device
->NamespaceId
;
296 CommandPacket
.CommandTimeout
= NVME_GENERIC_TIMEOUT
;
297 CommandPacket
.QueueType
= NVME_IO_QUEUE
;
299 Status
= Private
->Passthru
.PassThru (
311 Reset the Block Device.
313 @param This Indicates a pointer to the calling context.
314 @param ExtendedVerification Driver may perform diagnostics on reset.
316 @retval EFI_SUCCESS The device was reset.
317 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
324 IN EFI_BLOCK_IO_PROTOCOL
*This
,
325 IN BOOLEAN ExtendedVerification
329 NVME_CONTROLLER_PRIVATE_DATA
*Private
;
330 NVME_DEVICE_PRIVATE_DATA
*Device
;
334 return EFI_INVALID_PARAMETER
;
338 // For Nvm Express subsystem, reset block device means reset controller.
340 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
342 Device
= NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO (This
);
344 Private
= Device
->Controller
;
346 Status
= NvmeControllerInit (Private
);
348 if (EFI_ERROR (Status
)) {
349 Status
= EFI_DEVICE_ERROR
;
352 gBS
->RestoreTPL (OldTpl
);
358 Read BufferSize bytes from Lba into Buffer.
360 @param This Indicates a pointer to the calling context.
361 @param MediaId Id of the media, changes every time the media is replaced.
362 @param Lba The starting Logical Block Address to read from.
363 @param BufferSize Size of Buffer, must be a multiple of device block size.
364 @param Buffer A pointer to the destination buffer for the data. The caller is
365 responsible for either having implicit or explicit ownership of the buffer.
367 @retval EFI_SUCCESS The data was read correctly from the device.
368 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
369 @retval EFI_NO_MEDIA There is no media in the device.
370 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.
371 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
372 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
373 or the buffer is not on proper alignment.
378 NvmeBlockIoReadBlocks (
379 IN EFI_BLOCK_IO_PROTOCOL
*This
,
386 NVME_DEVICE_PRIVATE_DATA
*Device
;
388 EFI_BLOCK_IO_MEDIA
*Media
;
390 UINTN NumberOfBlocks
;
398 return EFI_INVALID_PARAMETER
;
403 if (MediaId
!= Media
->MediaId
) {
404 return EFI_MEDIA_CHANGED
;
407 if (Buffer
== NULL
) {
408 return EFI_INVALID_PARAMETER
;
411 if (BufferSize
== 0) {
415 BlockSize
= Media
->BlockSize
;
416 if ((BufferSize
% BlockSize
) != 0) {
417 return EFI_BAD_BUFFER_SIZE
;
420 NumberOfBlocks
= BufferSize
/ BlockSize
;
421 if ((Lba
+ NumberOfBlocks
- 1) > Media
->LastBlock
) {
422 return EFI_INVALID_PARAMETER
;
425 IoAlign
= Media
->IoAlign
;
426 if (IoAlign
> 0 && (((UINTN
) Buffer
& (IoAlign
- 1)) != 0)) {
427 return EFI_INVALID_PARAMETER
;
430 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
432 Device
= NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO (This
);
434 Status
= NvmeRead (Device
, Buffer
, Lba
, NumberOfBlocks
);
436 gBS
->RestoreTPL (OldTpl
);
441 Write BufferSize bytes from Lba into Buffer.
443 @param This Indicates a pointer to the calling context.
444 @param MediaId The media ID that the write request is for.
445 @param Lba The starting logical block address to be written. The caller is
446 responsible for writing to only legitimate locations.
447 @param BufferSize Size of Buffer, must be a multiple of device block size.
448 @param Buffer A pointer to the source buffer for the data.
450 @retval EFI_SUCCESS The data was written correctly to the device.
451 @retval EFI_WRITE_PROTECTED The device can not be written to.
452 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
453 @retval EFI_NO_MEDIA There is no media in the device.
454 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
455 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
456 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
457 or the buffer is not on proper alignment.
462 NvmeBlockIoWriteBlocks (
463 IN EFI_BLOCK_IO_PROTOCOL
*This
,
470 NVME_DEVICE_PRIVATE_DATA
*Device
;
472 EFI_BLOCK_IO_MEDIA
*Media
;
474 UINTN NumberOfBlocks
;
482 return EFI_INVALID_PARAMETER
;
487 if (MediaId
!= Media
->MediaId
) {
488 return EFI_MEDIA_CHANGED
;
491 if (Buffer
== NULL
) {
492 return EFI_INVALID_PARAMETER
;
495 if (BufferSize
== 0) {
499 BlockSize
= Media
->BlockSize
;
500 if ((BufferSize
% BlockSize
) != 0) {
501 return EFI_BAD_BUFFER_SIZE
;
504 NumberOfBlocks
= BufferSize
/ BlockSize
;
505 if ((Lba
+ NumberOfBlocks
- 1) > Media
->LastBlock
) {
506 return EFI_INVALID_PARAMETER
;
509 IoAlign
= Media
->IoAlign
;
510 if (IoAlign
> 0 && (((UINTN
) Buffer
& (IoAlign
- 1)) != 0)) {
511 return EFI_INVALID_PARAMETER
;
514 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
516 Device
= NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO (This
);
518 Status
= NvmeWrite (Device
, Buffer
, Lba
, NumberOfBlocks
);
520 gBS
->RestoreTPL (OldTpl
);
526 Flush the Block Device.
528 @param This Indicates a pointer to the calling context.
530 @retval EFI_SUCCESS All outstanding data was written to the device.
531 @retval EFI_DEVICE_ERROR The device reported an error while writing back the data.
532 @retval EFI_NO_MEDIA There is no media in the device.
537 NvmeBlockIoFlushBlocks (
538 IN EFI_BLOCK_IO_PROTOCOL
*This
541 NVME_DEVICE_PRIVATE_DATA
*Device
;
549 return EFI_INVALID_PARAMETER
;
552 OldTpl
= gBS
->RaiseTPL (TPL_CALLBACK
);
554 Device
= NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO (This
);
556 Status
= NvmeFlush (Device
);
558 gBS
->RestoreTPL (OldTpl
);
564 Trust transfer data from/to NVMe device.
566 This function performs one NVMe transaction to do a trust transfer from/to NVMe device.
568 @param Private The pointer to the NVME_CONTROLLER_PRIVATE_DATA data structure.
569 @param Buffer The pointer to the current transaction buffer.
570 @param SecurityProtocolId The value of the "Security Protocol" parameter of
571 the security protocol command to be sent.
572 @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
573 of the security protocol command to be sent.
574 @param TransferLength The block number or sector count of the transfer.
575 @param IsTrustSend Indicates whether it is a trust send operation or not.
576 @param Timeout The timeout, in 100ns units, to use for the execution
577 of the security protocol command. A Timeout value of 0
578 means that this function will wait indefinitely for the
579 security protocol command to execute. If Timeout is greater
580 than zero, then this function will return EFI_TIMEOUT
581 if the time required to execute the receive data command
582 is greater than Timeout.
583 @param TransferLengthOut A pointer to a buffer to store the size in bytes of the data
584 written to the buffer. Ignore it when IsTrustSend is TRUE.
586 @retval EFI_SUCCESS The data transfer is complete successfully.
587 @return others Some error occurs when transferring data.
591 TrustTransferNvmeDevice (
592 IN OUT NVME_CONTROLLER_PRIVATE_DATA
*Private
,
594 IN UINT8 SecurityProtocolId
,
595 IN UINT16 SecurityProtocolSpecificData
,
596 IN UINTN TransferLength
,
597 IN BOOLEAN IsTrustSend
,
599 OUT UINTN
*TransferLengthOut
602 EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET CommandPacket
;
603 EFI_NVM_EXPRESS_COMMAND Command
;
604 EFI_NVM_EXPRESS_COMPLETION Completion
;
608 ZeroMem (&CommandPacket
, sizeof (EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET
));
609 ZeroMem (&Command
, sizeof (EFI_NVM_EXPRESS_COMMAND
));
610 ZeroMem (&Completion
, sizeof (EFI_NVM_EXPRESS_COMPLETION
));
612 CommandPacket
.NvmeCmd
= &Command
;
613 CommandPacket
.NvmeCompletion
= &Completion
;
616 // Change Endianness of SecurityProtocolSpecificData
618 SpecificData
= (((SecurityProtocolSpecificData
<< 8) & 0xFF00) | (SecurityProtocolSpecificData
>> 8));
621 Command
.Cdw0
.Opcode
= NVME_ADMIN_SECURITY_SEND_CMD
;
622 CommandPacket
.TransferBuffer
= Buffer
;
623 CommandPacket
.TransferLength
= (UINT32
)TransferLength
;
624 CommandPacket
.NvmeCmd
->Cdw10
= (UINT32
)((SecurityProtocolId
<< 24) | (SpecificData
<< 8));
625 CommandPacket
.NvmeCmd
->Cdw11
= (UINT32
)TransferLength
;
627 Command
.Cdw0
.Opcode
= NVME_ADMIN_SECURITY_RECEIVE_CMD
;
628 CommandPacket
.TransferBuffer
= Buffer
;
629 CommandPacket
.TransferLength
= (UINT32
)TransferLength
;
630 CommandPacket
.NvmeCmd
->Cdw10
= (UINT32
)((SecurityProtocolId
<< 24) | (SpecificData
<< 8));
631 CommandPacket
.NvmeCmd
->Cdw11
= (UINT32
)TransferLength
;
634 CommandPacket
.NvmeCmd
->Flags
= CDW10_VALID
| CDW11_VALID
;
635 CommandPacket
.NvmeCmd
->Nsid
= NVME_CONTROLLER_ID
;
636 CommandPacket
.CommandTimeout
= Timeout
;
637 CommandPacket
.QueueType
= NVME_ADMIN_QUEUE
;
639 Status
= Private
->Passthru
.PassThru (
647 if (EFI_ERROR (Status
)) {
648 *TransferLengthOut
= 0;
650 *TransferLengthOut
= (UINTN
) TransferLength
;
658 Send a security protocol command to a device that receives data and/or the result
659 of one or more commands sent by SendData.
661 The ReceiveData function sends a security protocol command to the given MediaId.
662 The security protocol command sent is defined by SecurityProtocolId and contains
663 the security protocol specific data SecurityProtocolSpecificData. The function
664 returns the data from the security protocol command in PayloadBuffer.
666 For devices supporting the SCSI command set, the security protocol command is sent
667 using the SECURITY PROTOCOL IN command defined in SPC-4.
669 For devices supporting the ATA command set, the security protocol command is sent
670 using one of the TRUSTED RECEIVE commands defined in ATA8-ACS if PayloadBufferSize
673 If the PayloadBufferSize is zero, the security protocol command is sent using the
674 Trusted Non-Data command defined in ATA8-ACS.
676 If PayloadBufferSize is too small to store the available data from the security
677 protocol command, the function shall copy PayloadBufferSize bytes into the
678 PayloadBuffer and return EFI_WARN_BUFFER_TOO_SMALL.
680 If PayloadBuffer or PayloadTransferSize is NULL and PayloadBufferSize is non-zero,
681 the function shall return EFI_INVALID_PARAMETER.
683 If the given MediaId does not support security protocol commands, the function shall
684 return EFI_UNSUPPORTED. If there is no media in the device, the function returns
685 EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the device,
686 the function returns EFI_MEDIA_CHANGED.
688 If the security protocol fails to complete within the Timeout period, the function
689 shall return EFI_TIMEOUT.
691 If the security protocol command completes without an error, the function shall
692 return EFI_SUCCESS. If the security protocol command completes with an error, the
693 function shall return EFI_DEVICE_ERROR.
695 @param This Indicates a pointer to the calling context.
696 @param MediaId ID of the medium to receive data from.
697 @param Timeout The timeout, in 100ns units, to use for the execution
698 of the security protocol command. A Timeout value of 0
699 means that this function will wait indefinitely for the
700 security protocol command to execute. If Timeout is greater
701 than zero, then this function will return EFI_TIMEOUT
702 if the time required to execute the receive data command
703 is greater than Timeout.
704 @param SecurityProtocolId The value of the "Security Protocol" parameter of
705 the security protocol command to be sent.
706 @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
707 of the security protocol command to be sent.
708 @param PayloadBufferSize Size in bytes of the payload data buffer.
709 @param PayloadBuffer A pointer to a destination buffer to store the security
710 protocol command specific payload data for the security
711 protocol command. The caller is responsible for having
712 either implicit or explicit ownership of the buffer.
713 @param PayloadTransferSize A pointer to a buffer to store the size in bytes of the
714 data written to the payload data buffer.
716 @retval EFI_SUCCESS The security protocol command completed successfully.
717 @retval EFI_WARN_BUFFER_TOO_SMALL The PayloadBufferSize was too small to store the available
718 data from the device. The PayloadBuffer contains the truncated data.
719 @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.
720 @retval EFI_DEVICE_ERROR The security protocol command completed with an error.
721 @retval EFI_NO_MEDIA There is no media in the device.
722 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
723 @retval EFI_INVALID_PARAMETER The PayloadBuffer or PayloadTransferSize is NULL and
724 PayloadBufferSize is non-zero.
725 @retval EFI_TIMEOUT A timeout occurred while waiting for the security
726 protocol command to execute.
731 NvmeStorageSecurityReceiveData (
732 IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL
*This
,
735 IN UINT8 SecurityProtocolId
,
736 IN UINT16 SecurityProtocolSpecificData
,
737 IN UINTN PayloadBufferSize
,
738 OUT VOID
*PayloadBuffer
,
739 OUT UINTN
*PayloadTransferSize
743 NVME_DEVICE_PRIVATE_DATA
*Device
;
745 Status
= EFI_SUCCESS
;
747 if ((PayloadBuffer
== NULL
) || (PayloadTransferSize
== NULL
) || (PayloadBufferSize
== 0)) {
748 return EFI_INVALID_PARAMETER
;
751 Device
= NVME_DEVICE_PRIVATE_DATA_FROM_STORAGE_SECURITY (This
);
753 if (MediaId
!= Device
->BlockIo
.Media
->MediaId
) {
754 return EFI_MEDIA_CHANGED
;
757 if (!Device
->BlockIo
.Media
->MediaPresent
) {
761 Status
= TrustTransferNvmeDevice (
765 SecurityProtocolSpecificData
,
776 Send a security protocol command to a device.
778 The SendData function sends a security protocol command containing the payload
779 PayloadBuffer to the given MediaId. The security protocol command sent is
780 defined by SecurityProtocolId and contains the security protocol specific data
781 SecurityProtocolSpecificData. If the underlying protocol command requires a
782 specific padding for the command payload, the SendData function shall add padding
783 bytes to the command payload to satisfy the padding requirements.
785 For devices supporting the SCSI command set, the security protocol command is sent
786 using the SECURITY PROTOCOL OUT command defined in SPC-4.
788 For devices supporting the ATA command set, the security protocol command is sent
789 using one of the TRUSTED SEND commands defined in ATA8-ACS if PayloadBufferSize
790 is non-zero. If the PayloadBufferSize is zero, the security protocol command is
791 sent using the Trusted Non-Data command defined in ATA8-ACS.
793 If PayloadBuffer is NULL and PayloadBufferSize is non-zero, the function shall
794 return EFI_INVALID_PARAMETER.
796 If the given MediaId does not support security protocol commands, the function
797 shall return EFI_UNSUPPORTED. If there is no media in the device, the function
798 returns EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the
799 device, the function returns EFI_MEDIA_CHANGED.
801 If the security protocol fails to complete within the Timeout period, the function
802 shall return EFI_TIMEOUT.
804 If the security protocol command completes without an error, the function shall return
805 EFI_SUCCESS. If the security protocol command completes with an error, the function
806 shall return EFI_DEVICE_ERROR.
808 @param This Indicates a pointer to the calling context.
809 @param MediaId ID of the medium to receive data from.
810 @param Timeout The timeout, in 100ns units, to use for the execution
811 of the security protocol command. A Timeout value of 0
812 means that this function will wait indefinitely for the
813 security protocol command to execute. If Timeout is greater
814 than zero, then this function will return EFI_TIMEOUT
815 if the time required to execute the send data command
816 is greater than Timeout.
817 @param SecurityProtocolId The value of the "Security Protocol" parameter of
818 the security protocol command to be sent.
819 @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
820 of the security protocol command to be sent.
821 @param PayloadBufferSize Size in bytes of the payload data buffer.
822 @param PayloadBuffer A pointer to a destination buffer to store the security
823 protocol command specific payload data for the security
826 @retval EFI_SUCCESS The security protocol command completed successfully.
827 @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.
828 @retval EFI_DEVICE_ERROR The security protocol command completed with an error.
829 @retval EFI_NO_MEDIA There is no media in the device.
830 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
831 @retval EFI_INVALID_PARAMETER The PayloadBuffer is NULL and PayloadBufferSize is non-zero.
832 @retval EFI_TIMEOUT A timeout occurred while waiting for the security
833 protocol command to execute.
838 NvmeStorageSecuritySendData (
839 IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL
*This
,
842 IN UINT8 SecurityProtocolId
,
843 IN UINT16 SecurityProtocolSpecificData
,
844 IN UINTN PayloadBufferSize
,
845 IN VOID
*PayloadBuffer
849 NVME_DEVICE_PRIVATE_DATA
*Device
;
851 Status
= EFI_SUCCESS
;
853 if ((PayloadBuffer
== NULL
) && (PayloadBufferSize
!= 0)) {
854 return EFI_INVALID_PARAMETER
;
857 Device
= NVME_DEVICE_PRIVATE_DATA_FROM_STORAGE_SECURITY (This
);
859 if (MediaId
!= Device
->BlockIo
.Media
->MediaId
) {
860 return EFI_MEDIA_CHANGED
;
863 if (!Device
->BlockIo
.Media
->MediaPresent
) {
867 Status
= TrustTransferNvmeDevice (
871 SecurityProtocolSpecificData
,