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