]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpressBlockIo.c
5ac90745bde15ec72761caa2667e840af4125865
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / NvmExpressDxe / NvmExpressBlockIo.c
1 /** @file
2 NvmExpressDxe driver is used to manage non-volatile memory subsystem which follows
3 NVM Express specification.
4
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.
10
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.
13
14 **/
15
16 #include "NvmExpress.h"
17
18 /**
19 Read some sectors from the device.
20
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.
25
26 @retval EFI_SUCCESS Datum are read from the device.
27 @retval Others Fail to read all the datum.
28
29 **/
30 EFI_STATUS
31 ReadSectors (
32 IN NVME_DEVICE_PRIVATE_DATA *Device,
33 IN UINT64 Buffer,
34 IN UINT64 Lba,
35 IN UINT32 Blocks
36 )
37 {
38 NVME_CONTROLLER_PRIVATE_DATA *Private;
39 UINT32 Bytes;
40 EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET CommandPacket;
41 EFI_NVM_EXPRESS_COMMAND Command;
42 EFI_NVM_EXPRESS_COMPLETION Completion;
43 EFI_STATUS Status;
44 UINT32 BlockSize;
45
46 Private = Device->Controller;
47 BlockSize = Device->Media.BlockSize;
48 Bytes = Blocks * BlockSize;
49
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));
53
54 CommandPacket.NvmeCmd = &Command;
55 CommandPacket.NvmeCompletion = &Completion;
56
57 CommandPacket.NvmeCmd->Cdw0.Opcode = NVME_IO_READ_OPC;
58 CommandPacket.NvmeCmd->Nsid = Device->NamespaceId;
59 CommandPacket.TransferBuffer = (VOID *)(UINTN)Buffer;
60
61 CommandPacket.TransferLength = Bytes;
62 CommandPacket.CommandTimeout = NVME_GENERIC_TIMEOUT;
63 CommandPacket.QueueType = NVME_IO_QUEUE;
64
65 CommandPacket.NvmeCmd->Cdw10 = (UINT32)Lba;
66 CommandPacket.NvmeCmd->Cdw11 = (UINT32)RShiftU64(Lba, 32);
67 CommandPacket.NvmeCmd->Cdw12 = (Blocks - 1) & 0xFFFF;
68
69 CommandPacket.NvmeCmd->Flags = CDW10_VALID | CDW11_VALID | CDW12_VALID;
70
71 Status = Private->Passthru.PassThru (
72 &Private->Passthru,
73 Device->NamespaceId,
74 &CommandPacket,
75 NULL
76 );
77
78 return Status;
79 }
80
81 /**
82 Write some sectors to the device.
83
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.
88
89 @retval EFI_SUCCESS Datum are written into the buffer.
90 @retval Others Fail to write all the datum.
91
92 **/
93 EFI_STATUS
94 WriteSectors (
95 IN NVME_DEVICE_PRIVATE_DATA *Device,
96 IN UINT64 Buffer,
97 IN UINT64 Lba,
98 IN UINT32 Blocks
99 )
100 {
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;
105 EFI_STATUS Status;
106 UINT32 Bytes;
107 UINT32 BlockSize;
108
109 Private = Device->Controller;
110 BlockSize = Device->Media.BlockSize;
111 Bytes = Blocks * BlockSize;
112
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));
116
117 CommandPacket.NvmeCmd = &Command;
118 CommandPacket.NvmeCompletion = &Completion;
119
120 CommandPacket.NvmeCmd->Cdw0.Opcode = NVME_IO_WRITE_OPC;
121 CommandPacket.NvmeCmd->Nsid = Device->NamespaceId;
122 CommandPacket.TransferBuffer = (VOID *)(UINTN)Buffer;
123
124 CommandPacket.TransferLength = Bytes;
125 CommandPacket.CommandTimeout = NVME_GENERIC_TIMEOUT;
126 CommandPacket.QueueType = NVME_IO_QUEUE;
127
128 CommandPacket.NvmeCmd->Cdw10 = (UINT32)Lba;
129 CommandPacket.NvmeCmd->Cdw11 = (UINT32)RShiftU64(Lba, 32);
130 CommandPacket.NvmeCmd->Cdw12 = (Blocks - 1) & 0xFFFF;
131
132 CommandPacket.MetadataBuffer = NULL;
133 CommandPacket.MetadataLength = 0;
134
135 CommandPacket.NvmeCmd->Flags = CDW10_VALID | CDW11_VALID | CDW12_VALID;
136
137 Status = Private->Passthru.PassThru (
138 &Private->Passthru,
139 Device->NamespaceId,
140 &CommandPacket,
141 NULL
142 );
143
144 return Status;
145 }
146
147 /**
148 Read some blocks from the device.
149
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.
154
155 @retval EFI_SUCCESS Datum are read from the device.
156 @retval Others Fail to read all the datum.
157
158 **/
159 EFI_STATUS
160 NvmeRead (
161 IN NVME_DEVICE_PRIVATE_DATA *Device,
162 OUT VOID *Buffer,
163 IN UINT64 Lba,
164 IN UINTN Blocks
165 )
166 {
167 EFI_STATUS Status;
168 UINT32 BlockSize;
169 NVME_CONTROLLER_PRIVATE_DATA *Private;
170 UINT32 MaxTransferBlocks;
171 UINTN OrginalBlocks;
172
173 Status = EFI_SUCCESS;
174 Private = Device->Controller;
175 BlockSize = Device->Media.BlockSize;
176 OrginalBlocks = Blocks;
177
178 if (Private->ControllerData->Mdts != 0) {
179 MaxTransferBlocks = (1 << (Private->ControllerData->Mdts)) * (1 << (Private->Cap.Mpsmin + 12)) / BlockSize;
180 } else {
181 MaxTransferBlocks = 1024;
182 }
183
184 while (Blocks > 0) {
185 if (Blocks > MaxTransferBlocks) {
186 Status = ReadSectors (Device, (UINT64)(UINTN)Buffer, Lba, MaxTransferBlocks);
187
188 Blocks -= MaxTransferBlocks;
189 Buffer = (VOID *)(UINTN)((UINT64)(UINTN)Buffer + MaxTransferBlocks * BlockSize);
190 Lba += MaxTransferBlocks;
191 } else {
192 Status = ReadSectors (Device, (UINT64)(UINTN)Buffer, Lba, (UINT32)Blocks);
193 Blocks = 0;
194 }
195
196 if (EFI_ERROR(Status)) {
197 break;
198 }
199 }
200
201 DEBUG ((EFI_D_VERBOSE, "%a: Lba = 0x%08Lx, Original = 0x%08Lx, "
202 "Remaining = 0x%08Lx, BlockSize = 0x%x, Status = %r\n", __FUNCTION__, Lba,
203 (UINT64)OrginalBlocks, (UINT64)Blocks, BlockSize, Status));
204
205 return Status;
206 }
207
208 /**
209 Write some blocks to the device.
210
211 @param Device The pointer to the NVME_DEVICE_PRIVATE_DATA data structure.
212 @param Buffer The buffer to be written into the device.
213 @param Lba The start block number.
214 @param Blocks Total block number to be written.
215
216 @retval EFI_SUCCESS Datum are written into the buffer.
217 @retval Others Fail to write all the datum.
218
219 **/
220 EFI_STATUS
221 NvmeWrite (
222 IN NVME_DEVICE_PRIVATE_DATA *Device,
223 IN VOID *Buffer,
224 IN UINT64 Lba,
225 IN UINTN Blocks
226 )
227 {
228 EFI_STATUS Status;
229 UINT32 BlockSize;
230 NVME_CONTROLLER_PRIVATE_DATA *Private;
231 UINT32 MaxTransferBlocks;
232 UINTN OrginalBlocks;
233
234 Status = EFI_SUCCESS;
235 Private = Device->Controller;
236 BlockSize = Device->Media.BlockSize;
237 OrginalBlocks = Blocks;
238
239 if (Private->ControllerData->Mdts != 0) {
240 MaxTransferBlocks = (1 << (Private->ControllerData->Mdts)) * (1 << (Private->Cap.Mpsmin + 12)) / BlockSize;
241 } else {
242 MaxTransferBlocks = 1024;
243 }
244
245 while (Blocks > 0) {
246 if (Blocks > MaxTransferBlocks) {
247 Status = WriteSectors (Device, (UINT64)(UINTN)Buffer, Lba, MaxTransferBlocks);
248
249 Blocks -= MaxTransferBlocks;
250 Buffer = (VOID *)(UINTN)((UINT64)(UINTN)Buffer + MaxTransferBlocks * BlockSize);
251 Lba += MaxTransferBlocks;
252 } else {
253 Status = WriteSectors (Device, (UINT64)(UINTN)Buffer, Lba, (UINT32)Blocks);
254 Blocks = 0;
255 }
256
257 if (EFI_ERROR(Status)) {
258 break;
259 }
260 }
261
262 DEBUG ((EFI_D_VERBOSE, "%a: Lba = 0x%08Lx, Original = 0x%08Lx, "
263 "Remaining = 0x%08Lx, BlockSize = 0x%x, Status = %r\n", __FUNCTION__, Lba,
264 (UINT64)OrginalBlocks, (UINT64)Blocks, BlockSize, Status));
265
266 return Status;
267 }
268
269 /**
270 Flushes all modified data to the device.
271
272 @param Device The pointer to the NVME_DEVICE_PRIVATE_DATA data structure.
273
274 @retval EFI_SUCCESS Datum are written into the buffer.
275 @retval Others Fail to write all the datum.
276
277 **/
278 EFI_STATUS
279 NvmeFlush (
280 IN NVME_DEVICE_PRIVATE_DATA *Device
281 )
282 {
283 NVME_CONTROLLER_PRIVATE_DATA *Private;
284 EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET CommandPacket;
285 EFI_NVM_EXPRESS_COMMAND Command;
286 EFI_NVM_EXPRESS_COMPLETION Completion;
287 EFI_STATUS Status;
288
289 Private = Device->Controller;
290
291 ZeroMem (&CommandPacket, sizeof(EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET));
292 ZeroMem (&Command, sizeof(EFI_NVM_EXPRESS_COMMAND));
293 ZeroMem (&Completion, sizeof(EFI_NVM_EXPRESS_COMPLETION));
294
295 CommandPacket.NvmeCmd = &Command;
296 CommandPacket.NvmeCompletion = &Completion;
297
298 CommandPacket.NvmeCmd->Cdw0.Opcode = NVME_IO_FLUSH_OPC;
299 CommandPacket.NvmeCmd->Nsid = Device->NamespaceId;
300 CommandPacket.CommandTimeout = NVME_GENERIC_TIMEOUT;
301 CommandPacket.QueueType = NVME_IO_QUEUE;
302
303 Status = Private->Passthru.PassThru (
304 &Private->Passthru,
305 Device->NamespaceId,
306 &CommandPacket,
307 NULL
308 );
309
310 return Status;
311 }
312
313
314 /**
315 Reset the Block Device.
316
317 @param This Indicates a pointer to the calling context.
318 @param ExtendedVerification Driver may perform diagnostics on reset.
319
320 @retval EFI_SUCCESS The device was reset.
321 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
322 not be reset.
323
324 **/
325 EFI_STATUS
326 EFIAPI
327 NvmeBlockIoReset (
328 IN EFI_BLOCK_IO_PROTOCOL *This,
329 IN BOOLEAN ExtendedVerification
330 )
331 {
332 EFI_TPL OldTpl;
333 NVME_CONTROLLER_PRIVATE_DATA *Private;
334 NVME_DEVICE_PRIVATE_DATA *Device;
335 EFI_STATUS Status;
336
337 if (This == NULL) {
338 return EFI_INVALID_PARAMETER;
339 }
340
341 //
342 // For Nvm Express subsystem, reset block device means reset controller.
343 //
344 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
345
346 Device = NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO (This);
347
348 Private = Device->Controller;
349
350 Status = NvmeControllerInit (Private);
351
352 if (EFI_ERROR (Status)) {
353 Status = EFI_DEVICE_ERROR;
354 }
355
356 gBS->RestoreTPL (OldTpl);
357
358 return Status;
359 }
360
361 /**
362 Read BufferSize bytes from Lba into Buffer.
363
364 @param This Indicates a pointer to the calling context.
365 @param MediaId Id of the media, changes every time the media is replaced.
366 @param Lba The starting Logical Block Address to read from.
367 @param BufferSize Size of Buffer, must be a multiple of device block size.
368 @param Buffer A pointer to the destination buffer for the data. The caller is
369 responsible for either having implicit or explicit ownership of the buffer.
370
371 @retval EFI_SUCCESS The data was read correctly from the device.
372 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
373 @retval EFI_NO_MEDIA There is no media in the device.
374 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.
375 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
376 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
377 or the buffer is not on proper alignment.
378
379 **/
380 EFI_STATUS
381 EFIAPI
382 NvmeBlockIoReadBlocks (
383 IN EFI_BLOCK_IO_PROTOCOL *This,
384 IN UINT32 MediaId,
385 IN EFI_LBA Lba,
386 IN UINTN BufferSize,
387 OUT VOID *Buffer
388 )
389 {
390 NVME_DEVICE_PRIVATE_DATA *Device;
391 EFI_STATUS Status;
392 EFI_BLOCK_IO_MEDIA *Media;
393 UINTN BlockSize;
394 UINTN NumberOfBlocks;
395 UINTN IoAlign;
396 EFI_TPL OldTpl;
397
398 //
399 // Check parameters.
400 //
401 if (This == NULL) {
402 return EFI_INVALID_PARAMETER;
403 }
404
405 Media = This->Media;
406
407 if (MediaId != Media->MediaId) {
408 return EFI_MEDIA_CHANGED;
409 }
410
411 if (Buffer == NULL) {
412 return EFI_INVALID_PARAMETER;
413 }
414
415 if (BufferSize == 0) {
416 return EFI_SUCCESS;
417 }
418
419 BlockSize = Media->BlockSize;
420 if ((BufferSize % BlockSize) != 0) {
421 return EFI_BAD_BUFFER_SIZE;
422 }
423
424 NumberOfBlocks = BufferSize / BlockSize;
425 if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {
426 return EFI_INVALID_PARAMETER;
427 }
428
429 IoAlign = Media->IoAlign;
430 if (IoAlign > 0 && (((UINTN) Buffer & (IoAlign - 1)) != 0)) {
431 return EFI_INVALID_PARAMETER;
432 }
433
434 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
435
436 Device = NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO (This);
437
438 Status = NvmeRead (Device, Buffer, Lba, NumberOfBlocks);
439
440 gBS->RestoreTPL (OldTpl);
441 return Status;
442 }
443
444 /**
445 Write BufferSize bytes from Lba into Buffer.
446
447 @param This Indicates a pointer to the calling context.
448 @param MediaId The media ID that the write request is for.
449 @param Lba The starting logical block address to be written. The caller is
450 responsible for writing to only legitimate locations.
451 @param BufferSize Size of Buffer, must be a multiple of device block size.
452 @param Buffer A pointer to the source buffer for the data.
453
454 @retval EFI_SUCCESS The data was written correctly to the device.
455 @retval EFI_WRITE_PROTECTED The device can not be written to.
456 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
457 @retval EFI_NO_MEDIA There is no media in the device.
458 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
459 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
460 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
461 or the buffer is not on proper alignment.
462
463 **/
464 EFI_STATUS
465 EFIAPI
466 NvmeBlockIoWriteBlocks (
467 IN EFI_BLOCK_IO_PROTOCOL *This,
468 IN UINT32 MediaId,
469 IN EFI_LBA Lba,
470 IN UINTN BufferSize,
471 IN VOID *Buffer
472 )
473 {
474 NVME_DEVICE_PRIVATE_DATA *Device;
475 EFI_STATUS Status;
476 EFI_BLOCK_IO_MEDIA *Media;
477 UINTN BlockSize;
478 UINTN NumberOfBlocks;
479 UINTN IoAlign;
480 EFI_TPL OldTpl;
481
482 //
483 // Check parameters.
484 //
485 if (This == NULL) {
486 return EFI_INVALID_PARAMETER;
487 }
488
489 Media = This->Media;
490
491 if (MediaId != Media->MediaId) {
492 return EFI_MEDIA_CHANGED;
493 }
494
495 if (Buffer == NULL) {
496 return EFI_INVALID_PARAMETER;
497 }
498
499 if (BufferSize == 0) {
500 return EFI_SUCCESS;
501 }
502
503 BlockSize = Media->BlockSize;
504 if ((BufferSize % BlockSize) != 0) {
505 return EFI_BAD_BUFFER_SIZE;
506 }
507
508 NumberOfBlocks = BufferSize / BlockSize;
509 if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {
510 return EFI_INVALID_PARAMETER;
511 }
512
513 IoAlign = Media->IoAlign;
514 if (IoAlign > 0 && (((UINTN) Buffer & (IoAlign - 1)) != 0)) {
515 return EFI_INVALID_PARAMETER;
516 }
517
518 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
519
520 Device = NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO (This);
521
522 Status = NvmeWrite (Device, Buffer, Lba, NumberOfBlocks);
523
524 gBS->RestoreTPL (OldTpl);
525
526 return Status;
527 }
528
529 /**
530 Flush the Block Device.
531
532 @param This Indicates a pointer to the calling context.
533
534 @retval EFI_SUCCESS All outstanding data was written to the device.
535 @retval EFI_DEVICE_ERROR The device reported an error while writing back the data.
536 @retval EFI_NO_MEDIA There is no media in the device.
537
538 **/
539 EFI_STATUS
540 EFIAPI
541 NvmeBlockIoFlushBlocks (
542 IN EFI_BLOCK_IO_PROTOCOL *This
543 )
544 {
545 NVME_DEVICE_PRIVATE_DATA *Device;
546 EFI_STATUS Status;
547 EFI_TPL OldTpl;
548
549 //
550 // Check parameters.
551 //
552 if (This == NULL) {
553 return EFI_INVALID_PARAMETER;
554 }
555
556 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
557
558 Device = NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO (This);
559
560 Status = NvmeFlush (Device);
561
562 gBS->RestoreTPL (OldTpl);
563
564 return Status;
565 }
566
567 /**
568 Trust transfer data from/to NVMe device.
569
570 This function performs one NVMe transaction to do a trust transfer from/to NVMe device.
571
572 @param Private The pointer to the NVME_CONTROLLER_PRIVATE_DATA data structure.
573 @param Buffer The pointer to the current transaction buffer.
574 @param SecurityProtocolId The value of the "Security Protocol" parameter of
575 the security protocol command to be sent.
576 @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
577 of the security protocol command to be sent.
578 @param TransferLength The block number or sector count of the transfer.
579 @param IsTrustSend Indicates whether it is a trust send operation or not.
580 @param Timeout The timeout, in 100ns units, to use for the execution
581 of the security protocol command. A Timeout value of 0
582 means that this function will wait indefinitely for the
583 security protocol command to execute. If Timeout is greater
584 than zero, then this function will return EFI_TIMEOUT
585 if the time required to execute the receive data command
586 is greater than Timeout.
587 @param TransferLengthOut A pointer to a buffer to store the size in bytes of the data
588 written to the buffer. Ignore it when IsTrustSend is TRUE.
589
590 @retval EFI_SUCCESS The data transfer is complete successfully.
591 @return others Some error occurs when transferring data.
592
593 **/
594 EFI_STATUS
595 TrustTransferNvmeDevice (
596 IN OUT NVME_CONTROLLER_PRIVATE_DATA *Private,
597 IN OUT VOID *Buffer,
598 IN UINT8 SecurityProtocolId,
599 IN UINT16 SecurityProtocolSpecificData,
600 IN UINTN TransferLength,
601 IN BOOLEAN IsTrustSend,
602 IN UINT64 Timeout,
603 OUT UINTN *TransferLengthOut
604 )
605 {
606 EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET CommandPacket;
607 EFI_NVM_EXPRESS_COMMAND Command;
608 EFI_NVM_EXPRESS_COMPLETION Completion;
609 EFI_STATUS Status;
610 UINT16 SpecificData;
611
612 ZeroMem (&CommandPacket, sizeof (EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET));
613 ZeroMem (&Command, sizeof (EFI_NVM_EXPRESS_COMMAND));
614 ZeroMem (&Completion, sizeof (EFI_NVM_EXPRESS_COMPLETION));
615
616 CommandPacket.NvmeCmd = &Command;
617 CommandPacket.NvmeCompletion = &Completion;
618
619 //
620 // Change Endianness of SecurityProtocolSpecificData
621 //
622 SpecificData = (((SecurityProtocolSpecificData << 8) & 0xFF00) | (SecurityProtocolSpecificData >> 8));
623
624 if (IsTrustSend) {
625 Command.Cdw0.Opcode = NVME_ADMIN_SECURITY_SEND_CMD;
626 CommandPacket.TransferBuffer = Buffer;
627 CommandPacket.TransferLength = (UINT32)TransferLength;
628 CommandPacket.NvmeCmd->Cdw10 = (UINT32)((SecurityProtocolId << 24) | (SpecificData << 8));
629 CommandPacket.NvmeCmd->Cdw11 = (UINT32)TransferLength;
630 } else {
631 Command.Cdw0.Opcode = NVME_ADMIN_SECURITY_RECEIVE_CMD;
632 CommandPacket.TransferBuffer = Buffer;
633 CommandPacket.TransferLength = (UINT32)TransferLength;
634 CommandPacket.NvmeCmd->Cdw10 = (UINT32)((SecurityProtocolId << 24) | (SpecificData << 8));
635 CommandPacket.NvmeCmd->Cdw11 = (UINT32)TransferLength;
636 }
637
638 CommandPacket.NvmeCmd->Flags = CDW10_VALID | CDW11_VALID;
639 CommandPacket.NvmeCmd->Nsid = NVME_CONTROLLER_ID;
640 CommandPacket.CommandTimeout = Timeout;
641 CommandPacket.QueueType = NVME_ADMIN_QUEUE;
642
643 Status = Private->Passthru.PassThru (
644 &Private->Passthru,
645 NVME_CONTROLLER_ID,
646 &CommandPacket,
647 NULL
648 );
649
650 if (!IsTrustSend) {
651 if (EFI_ERROR (Status)) {
652 *TransferLengthOut = 0;
653 } else {
654 *TransferLengthOut = (UINTN) TransferLength;
655 }
656 }
657
658 return Status;
659 }
660
661 /**
662 Send a security protocol command to a device that receives data and/or the result
663 of one or more commands sent by SendData.
664
665 The ReceiveData function sends a security protocol command to the given MediaId.
666 The security protocol command sent is defined by SecurityProtocolId and contains
667 the security protocol specific data SecurityProtocolSpecificData. The function
668 returns the data from the security protocol command in PayloadBuffer.
669
670 For devices supporting the SCSI command set, the security protocol command is sent
671 using the SECURITY PROTOCOL IN command defined in SPC-4.
672
673 For devices supporting the ATA command set, the security protocol command is sent
674 using one of the TRUSTED RECEIVE commands defined in ATA8-ACS if PayloadBufferSize
675 is non-zero.
676
677 If the PayloadBufferSize is zero, the security protocol command is sent using the
678 Trusted Non-Data command defined in ATA8-ACS.
679
680 If PayloadBufferSize is too small to store the available data from the security
681 protocol command, the function shall copy PayloadBufferSize bytes into the
682 PayloadBuffer and return EFI_WARN_BUFFER_TOO_SMALL.
683
684 If PayloadBuffer or PayloadTransferSize is NULL and PayloadBufferSize is non-zero,
685 the function shall return EFI_INVALID_PARAMETER.
686
687 If the given MediaId does not support security protocol commands, the function shall
688 return EFI_UNSUPPORTED. If there is no media in the device, the function returns
689 EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the device,
690 the function returns EFI_MEDIA_CHANGED.
691
692 If the security protocol fails to complete within the Timeout period, the function
693 shall return EFI_TIMEOUT.
694
695 If the security protocol command completes without an error, the function shall
696 return EFI_SUCCESS. If the security protocol command completes with an error, the
697 function shall return EFI_DEVICE_ERROR.
698
699 @param This Indicates a pointer to the calling context.
700 @param MediaId ID of the medium to receive data from.
701 @param Timeout The timeout, in 100ns units, to use for the execution
702 of the security protocol command. A Timeout value of 0
703 means that this function will wait indefinitely for the
704 security protocol command to execute. If Timeout is greater
705 than zero, then this function will return EFI_TIMEOUT
706 if the time required to execute the receive data command
707 is greater than Timeout.
708 @param SecurityProtocolId The value of the "Security Protocol" parameter of
709 the security protocol command to be sent.
710 @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
711 of the security protocol command to be sent.
712 @param PayloadBufferSize Size in bytes of the payload data buffer.
713 @param PayloadBuffer A pointer to a destination buffer to store the security
714 protocol command specific payload data for the security
715 protocol command. The caller is responsible for having
716 either implicit or explicit ownership of the buffer.
717 @param PayloadTransferSize A pointer to a buffer to store the size in bytes of the
718 data written to the payload data buffer.
719
720 @retval EFI_SUCCESS The security protocol command completed successfully.
721 @retval EFI_WARN_BUFFER_TOO_SMALL The PayloadBufferSize was too small to store the available
722 data from the device. The PayloadBuffer contains the truncated data.
723 @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.
724 @retval EFI_DEVICE_ERROR The security protocol command completed with an error.
725 @retval EFI_NO_MEDIA There is no media in the device.
726 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
727 @retval EFI_INVALID_PARAMETER The PayloadBuffer or PayloadTransferSize is NULL and
728 PayloadBufferSize is non-zero.
729 @retval EFI_TIMEOUT A timeout occurred while waiting for the security
730 protocol command to execute.
731
732 **/
733 EFI_STATUS
734 EFIAPI
735 NvmeStorageSecurityReceiveData (
736 IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,
737 IN UINT32 MediaId,
738 IN UINT64 Timeout,
739 IN UINT8 SecurityProtocolId,
740 IN UINT16 SecurityProtocolSpecificData,
741 IN UINTN PayloadBufferSize,
742 OUT VOID *PayloadBuffer,
743 OUT UINTN *PayloadTransferSize
744 )
745 {
746 EFI_STATUS Status;
747 NVME_DEVICE_PRIVATE_DATA *Device;
748
749 Status = EFI_SUCCESS;
750
751 if ((PayloadBuffer == NULL) || (PayloadTransferSize == NULL) || (PayloadBufferSize == 0)) {
752 return EFI_INVALID_PARAMETER;
753 }
754
755 Device = NVME_DEVICE_PRIVATE_DATA_FROM_STORAGE_SECURITY (This);
756
757 if (MediaId != Device->BlockIo.Media->MediaId) {
758 return EFI_MEDIA_CHANGED;
759 }
760
761 if (!Device->BlockIo.Media->MediaPresent) {
762 return EFI_NO_MEDIA;
763 }
764
765 Status = TrustTransferNvmeDevice (
766 Device->Controller,
767 PayloadBuffer,
768 SecurityProtocolId,
769 SecurityProtocolSpecificData,
770 PayloadBufferSize,
771 FALSE,
772 Timeout,
773 PayloadTransferSize
774 );
775
776 return Status;
777 }
778
779 /**
780 Send a security protocol command to a device.
781
782 The SendData function sends a security protocol command containing the payload
783 PayloadBuffer to the given MediaId. The security protocol command sent is
784 defined by SecurityProtocolId and contains the security protocol specific data
785 SecurityProtocolSpecificData. If the underlying protocol command requires a
786 specific padding for the command payload, the SendData function shall add padding
787 bytes to the command payload to satisfy the padding requirements.
788
789 For devices supporting the SCSI command set, the security protocol command is sent
790 using the SECURITY PROTOCOL OUT command defined in SPC-4.
791
792 For devices supporting the ATA command set, the security protocol command is sent
793 using one of the TRUSTED SEND commands defined in ATA8-ACS if PayloadBufferSize
794 is non-zero. If the PayloadBufferSize is zero, the security protocol command is
795 sent using the Trusted Non-Data command defined in ATA8-ACS.
796
797 If PayloadBuffer is NULL and PayloadBufferSize is non-zero, the function shall
798 return EFI_INVALID_PARAMETER.
799
800 If the given MediaId does not support security protocol commands, the function
801 shall return EFI_UNSUPPORTED. If there is no media in the device, the function
802 returns EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the
803 device, the function returns EFI_MEDIA_CHANGED.
804
805 If the security protocol fails to complete within the Timeout period, the function
806 shall return EFI_TIMEOUT.
807
808 If the security protocol command completes without an error, the function shall return
809 EFI_SUCCESS. If the security protocol command completes with an error, the function
810 shall return EFI_DEVICE_ERROR.
811
812 @param This Indicates a pointer to the calling context.
813 @param MediaId ID of the medium to receive data from.
814 @param Timeout The timeout, in 100ns units, to use for the execution
815 of the security protocol command. A Timeout value of 0
816 means that this function will wait indefinitely for the
817 security protocol command to execute. If Timeout is greater
818 than zero, then this function will return EFI_TIMEOUT
819 if the time required to execute the send data command
820 is greater than Timeout.
821 @param SecurityProtocolId The value of the "Security Protocol" parameter of
822 the security protocol command to be sent.
823 @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
824 of the security protocol command to be sent.
825 @param PayloadBufferSize Size in bytes of the payload data buffer.
826 @param PayloadBuffer A pointer to a destination buffer to store the security
827 protocol command specific payload data for the security
828 protocol command.
829
830 @retval EFI_SUCCESS The security protocol command completed successfully.
831 @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.
832 @retval EFI_DEVICE_ERROR The security protocol command completed with an error.
833 @retval EFI_NO_MEDIA There is no media in the device.
834 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
835 @retval EFI_INVALID_PARAMETER The PayloadBuffer is NULL and PayloadBufferSize is non-zero.
836 @retval EFI_TIMEOUT A timeout occurred while waiting for the security
837 protocol command to execute.
838
839 **/
840 EFI_STATUS
841 EFIAPI
842 NvmeStorageSecuritySendData (
843 IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,
844 IN UINT32 MediaId,
845 IN UINT64 Timeout,
846 IN UINT8 SecurityProtocolId,
847 IN UINT16 SecurityProtocolSpecificData,
848 IN UINTN PayloadBufferSize,
849 IN VOID *PayloadBuffer
850 )
851 {
852 EFI_STATUS Status;
853 NVME_DEVICE_PRIVATE_DATA *Device;
854
855 Status = EFI_SUCCESS;
856
857 if ((PayloadBuffer == NULL) && (PayloadBufferSize != 0)) {
858 return EFI_INVALID_PARAMETER;
859 }
860
861 Device = NVME_DEVICE_PRIVATE_DATA_FROM_STORAGE_SECURITY (This);
862
863 if (MediaId != Device->BlockIo.Media->MediaId) {
864 return EFI_MEDIA_CHANGED;
865 }
866
867 if (!Device->BlockIo.Media->MediaPresent) {
868 return EFI_NO_MEDIA;
869 }
870
871 Status = TrustTransferNvmeDevice (
872 Device->Controller,
873 PayloadBuffer,
874 SecurityProtocolId,
875 SecurityProtocolSpecificData,
876 PayloadBufferSize,
877 TRUE,
878 Timeout,
879 NULL
880 );
881
882 return Status;
883 }
884
885
886
887