]>
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 | |
35f910f0 | 5 | (C) Copyright 2014 Hewlett-Packard Development Company, L.P.<BR>\r |
946f48eb | 6 | Copyright (c) 2013 - 2016, Intel Corporation. All rights reserved.<BR>\r |
eb290d02 FT |
7 | This program and the accompanying materials\r |
8 | are licensed and made available under the terms and conditions of the BSD License\r | |
9 | which accompanies this distribution. The full text of the license may be found at\r | |
10 | http://opensource.org/licenses/bsd-license.php.\r | |
11 | \r | |
12 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
13 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
14 | \r | |
15 | **/\r | |
16 | \r | |
17 | #include "NvmExpress.h"\r | |
18 | \r | |
eb290d02 FT |
19 | /**\r |
20 | Dump the execution status from a given completion queue entry.\r | |
21 | \r | |
22 | @param[in] Cq A pointer to the NVME_CQ item.\r | |
23 | \r | |
24 | **/\r | |
25 | VOID\r | |
26 | NvmeDumpStatus (\r | |
27 | IN NVME_CQ *Cq\r | |
28 | )\r | |
29 | {\r | |
30 | DEBUG ((EFI_D_VERBOSE, "Dump NVMe Completion Entry Status from [0x%x]:\n", Cq));\r | |
31 | \r | |
32 | DEBUG ((EFI_D_VERBOSE, " SQ Identifier : [0x%x], Phase Tag : [%d], Cmd Identifier : [0x%x]\n", Cq->Sqid, Cq->Pt, Cq->Cid));\r | |
33 | \r | |
34 | DEBUG ((EFI_D_VERBOSE, " NVMe Cmd Execution Result - "));\r | |
35 | \r | |
36 | switch (Cq->Sct) {\r | |
37 | case 0x0:\r | |
38 | switch (Cq->Sc) {\r | |
39 | case 0x0:\r | |
40 | DEBUG ((EFI_D_VERBOSE, "Successful Completion\n"));\r | |
41 | break;\r | |
42 | case 0x1:\r | |
43 | DEBUG ((EFI_D_VERBOSE, "Invalid Command Opcode\n"));\r | |
44 | break;\r | |
45 | case 0x2:\r | |
46 | DEBUG ((EFI_D_VERBOSE, "Invalid Field in Command\n"));\r | |
47 | break;\r | |
48 | case 0x3:\r | |
49 | DEBUG ((EFI_D_VERBOSE, "Command ID Conflict\n"));\r | |
50 | break;\r | |
51 | case 0x4:\r | |
52 | DEBUG ((EFI_D_VERBOSE, "Data Transfer Error\n"));\r | |
53 | break;\r | |
54 | case 0x5:\r | |
55 | DEBUG ((EFI_D_VERBOSE, "Commands Aborted due to Power Loss Notification\n"));\r | |
56 | break;\r | |
57 | case 0x6:\r | |
58 | DEBUG ((EFI_D_VERBOSE, "Internal Device Error\n"));\r | |
59 | break;\r | |
60 | case 0x7:\r | |
61 | DEBUG ((EFI_D_VERBOSE, "Command Abort Requested\n"));\r | |
62 | break;\r | |
63 | case 0x8:\r | |
64 | DEBUG ((EFI_D_VERBOSE, "Command Aborted due to SQ Deletion\n"));\r | |
65 | break;\r | |
66 | case 0x9:\r | |
67 | DEBUG ((EFI_D_VERBOSE, "Command Aborted due to Failed Fused Command\n"));\r | |
68 | break;\r | |
69 | case 0xA:\r | |
70 | DEBUG ((EFI_D_VERBOSE, "Command Aborted due to Missing Fused Command\n"));\r | |
71 | break;\r | |
72 | case 0xB:\r | |
73 | DEBUG ((EFI_D_VERBOSE, "Invalid Namespace or Format\n"));\r | |
74 | break;\r | |
75 | case 0xC:\r | |
76 | DEBUG ((EFI_D_VERBOSE, "Command Sequence Error\n"));\r | |
77 | break;\r | |
78 | case 0xD:\r | |
79 | DEBUG ((EFI_D_VERBOSE, "Invalid SGL Last Segment Descriptor\n"));\r | |
80 | break;\r | |
81 | case 0xE:\r | |
82 | DEBUG ((EFI_D_VERBOSE, "Invalid Number of SGL Descriptors\n"));\r | |
83 | break;\r | |
84 | case 0xF:\r | |
85 | DEBUG ((EFI_D_VERBOSE, "Data SGL Length Invalid\n"));\r | |
86 | break;\r | |
87 | case 0x10:\r | |
88 | DEBUG ((EFI_D_VERBOSE, "Metadata SGL Length Invalid\n"));\r | |
89 | break;\r | |
90 | case 0x11:\r | |
91 | DEBUG ((EFI_D_VERBOSE, "SGL Descriptor Type Invalid\n"));\r | |
92 | break;\r | |
93 | case 0x80:\r | |
94 | DEBUG ((EFI_D_VERBOSE, "LBA Out of Range\n"));\r | |
95 | break;\r | |
96 | case 0x81:\r | |
97 | DEBUG ((EFI_D_VERBOSE, "Capacity Exceeded\n"));\r | |
98 | break;\r | |
99 | case 0x82:\r | |
100 | DEBUG ((EFI_D_VERBOSE, "Namespace Not Ready\n"));\r | |
101 | break;\r | |
102 | case 0x83:\r | |
103 | DEBUG ((EFI_D_VERBOSE, "Reservation Conflict\n"));\r | |
104 | break;\r | |
105 | }\r | |
106 | break;\r | |
107 | \r | |
108 | case 0x1:\r | |
109 | switch (Cq->Sc) {\r | |
110 | case 0x0:\r | |
111 | DEBUG ((EFI_D_VERBOSE, "Completion Queue Invalid\n"));\r | |
112 | break;\r | |
113 | case 0x1:\r | |
114 | DEBUG ((EFI_D_VERBOSE, "Invalid Queue Identifier\n"));\r | |
115 | break;\r | |
116 | case 0x2:\r | |
117 | DEBUG ((EFI_D_VERBOSE, "Maximum Queue Size Exceeded\n"));\r | |
118 | break;\r | |
119 | case 0x3:\r | |
120 | DEBUG ((EFI_D_VERBOSE, "Abort Command Limit Exceeded\n"));\r | |
121 | break;\r | |
122 | case 0x5:\r | |
123 | DEBUG ((EFI_D_VERBOSE, "Asynchronous Event Request Limit Exceeded\n"));\r | |
124 | break;\r | |
125 | case 0x6:\r | |
126 | DEBUG ((EFI_D_VERBOSE, "Invalid Firmware Slot\n"));\r | |
127 | break;\r | |
128 | case 0x7:\r | |
129 | DEBUG ((EFI_D_VERBOSE, "Invalid Firmware Image\n"));\r | |
130 | break;\r | |
131 | case 0x8:\r | |
132 | DEBUG ((EFI_D_VERBOSE, "Invalid Interrupt Vector\n"));\r | |
133 | break;\r | |
134 | case 0x9:\r | |
135 | DEBUG ((EFI_D_VERBOSE, "Invalid Log Page\n"));\r | |
136 | break;\r | |
137 | case 0xA:\r | |
138 | DEBUG ((EFI_D_VERBOSE, "Invalid Format\n"));\r | |
139 | break;\r | |
140 | case 0xB:\r | |
141 | DEBUG ((EFI_D_VERBOSE, "Firmware Application Requires Conventional Reset\n"));\r | |
142 | break;\r | |
143 | case 0xC:\r | |
144 | DEBUG ((EFI_D_VERBOSE, "Invalid Queue Deletion\n"));\r | |
145 | break;\r | |
146 | case 0xD:\r | |
147 | DEBUG ((EFI_D_VERBOSE, "Feature Identifier Not Saveable\n"));\r | |
148 | break;\r | |
149 | case 0xE:\r | |
150 | DEBUG ((EFI_D_VERBOSE, "Feature Not Changeable\n"));\r | |
151 | break;\r | |
152 | case 0xF:\r | |
153 | DEBUG ((EFI_D_VERBOSE, "Feature Not Namespace Specific\n"));\r | |
154 | break;\r | |
155 | case 0x10:\r | |
156 | DEBUG ((EFI_D_VERBOSE, "Firmware Application Requires NVM Subsystem Reset\n"));\r | |
157 | break;\r | |
158 | case 0x80:\r | |
159 | DEBUG ((EFI_D_VERBOSE, "Conflicting Attributes\n"));\r | |
160 | break;\r | |
161 | case 0x81:\r | |
162 | DEBUG ((EFI_D_VERBOSE, "Invalid Protection Information\n"));\r | |
163 | break;\r | |
164 | case 0x82:\r | |
165 | DEBUG ((EFI_D_VERBOSE, "Attempted Write to Read Only Range\n"));\r | |
166 | break;\r | |
167 | }\r | |
168 | break;\r | |
169 | \r | |
170 | case 0x2:\r | |
171 | switch (Cq->Sc) {\r | |
172 | case 0x80:\r | |
173 | DEBUG ((EFI_D_VERBOSE, "Write Fault\n"));\r | |
174 | break;\r | |
175 | case 0x81:\r | |
176 | DEBUG ((EFI_D_VERBOSE, "Unrecovered Read Error\n"));\r | |
177 | break;\r | |
178 | case 0x82:\r | |
179 | DEBUG ((EFI_D_VERBOSE, "End-to-end Guard Check Error\n"));\r | |
180 | break;\r | |
181 | case 0x83:\r | |
182 | DEBUG ((EFI_D_VERBOSE, "End-to-end Application Tag Check Error\n"));\r | |
183 | break;\r | |
184 | case 0x84:\r | |
185 | DEBUG ((EFI_D_VERBOSE, "End-to-end Reference Tag Check Error\n"));\r | |
186 | break;\r | |
187 | case 0x85:\r | |
188 | DEBUG ((EFI_D_VERBOSE, "Compare Failure\n"));\r | |
189 | break;\r | |
190 | case 0x86:\r | |
191 | DEBUG ((EFI_D_VERBOSE, "Access Denied\n"));\r | |
192 | break;\r | |
193 | }\r | |
194 | break;\r | |
195 | \r | |
196 | default:\r | |
197 | break;\r | |
198 | }\r | |
199 | }\r | |
200 | \r | |
201 | /**\r | |
202 | Create PRP lists for data transfer which is larger than 2 memory pages.\r | |
203 | Note here we calcuate the number of required PRP lists and allocate them at one time.\r | |
204 | \r | |
205 | @param[in] PciIo A pointer to the EFI_PCI_IO_PROTOCOL instance.\r | |
206 | @param[in] PhysicalAddr The physical base address of data buffer.\r | |
207 | @param[in] Pages The number of pages to be transfered.\r | |
208 | @param[out] PrpListHost The host base address of PRP lists.\r | |
209 | @param[in,out] PrpListNo The number of PRP List.\r | |
210 | @param[out] Mapping The mapping value returned from PciIo.Map().\r | |
211 | \r | |
212 | @retval The pointer to the first PRP List of the PRP lists.\r | |
213 | \r | |
214 | **/\r | |
215 | VOID*\r | |
216 | NvmeCreatePrpList (\r | |
217 | IN EFI_PCI_IO_PROTOCOL *PciIo,\r | |
218 | IN EFI_PHYSICAL_ADDRESS PhysicalAddr,\r | |
219 | IN UINTN Pages,\r | |
220 | OUT VOID **PrpListHost,\r | |
221 | IN OUT UINTN *PrpListNo,\r | |
222 | OUT VOID **Mapping\r | |
223 | )\r | |
224 | {\r | |
225 | UINTN PrpEntryNo;\r | |
226 | UINT64 PrpListBase;\r | |
227 | UINTN PrpListIndex;\r | |
228 | UINTN PrpEntryIndex;\r | |
229 | UINT64 Remainder;\r | |
230 | EFI_PHYSICAL_ADDRESS PrpListPhyAddr;\r | |
231 | UINTN Bytes;\r | |
232 | EFI_STATUS Status;\r | |
233 | \r | |
234 | //\r | |
235 | // The number of Prp Entry in a memory page.\r | |
236 | //\r | |
237 | PrpEntryNo = EFI_PAGE_SIZE / sizeof (UINT64);\r | |
238 | \r | |
239 | //\r | |
240 | // Calculate total PrpList number.\r | |
241 | //\r | |
769402ef FT |
242 | *PrpListNo = (UINTN)DivU64x64Remainder ((UINT64)Pages, (UINT64)PrpEntryNo - 1, &Remainder);\r |
243 | if (*PrpListNo == 0) {\r | |
244 | *PrpListNo = 1;\r | |
a9ec6d65 | 245 | } else if ((Remainder != 0) && (Remainder != 1)) {\r |
eb290d02 | 246 | *PrpListNo += 1;\r |
769402ef FT |
247 | } else if (Remainder == 1) {\r |
248 | Remainder = PrpEntryNo;\r | |
249 | } else if (Remainder == 0) {\r | |
250 | Remainder = PrpEntryNo - 1;\r | |
eb290d02 FT |
251 | }\r |
252 | \r | |
253 | Status = PciIo->AllocateBuffer (\r | |
254 | PciIo,\r | |
255 | AllocateAnyPages,\r | |
256 | EfiBootServicesData,\r | |
257 | *PrpListNo,\r | |
258 | PrpListHost,\r | |
259 | 0\r | |
260 | );\r | |
261 | \r | |
262 | if (EFI_ERROR (Status)) {\r | |
263 | return NULL;\r | |
264 | }\r | |
265 | \r | |
266 | Bytes = EFI_PAGES_TO_SIZE (*PrpListNo);\r | |
267 | Status = PciIo->Map (\r | |
268 | PciIo,\r | |
269 | EfiPciIoOperationBusMasterCommonBuffer,\r | |
270 | *PrpListHost,\r | |
271 | &Bytes,\r | |
272 | &PrpListPhyAddr,\r | |
273 | Mapping\r | |
274 | );\r | |
275 | \r | |
276 | if (EFI_ERROR (Status) || (Bytes != EFI_PAGES_TO_SIZE (*PrpListNo))) {\r | |
277 | DEBUG ((EFI_D_ERROR, "NvmeCreatePrpList: create PrpList failure!\n"));\r | |
278 | goto EXIT;\r | |
279 | }\r | |
280 | //\r | |
281 | // Fill all PRP lists except of last one.\r | |
282 | //\r | |
283 | ZeroMem (*PrpListHost, Bytes);\r | |
284 | for (PrpListIndex = 0; PrpListIndex < *PrpListNo - 1; ++PrpListIndex) {\r | |
769402ef | 285 | PrpListBase = *(UINT64*)PrpListHost + PrpListIndex * EFI_PAGE_SIZE;\r |
eb290d02 FT |
286 | \r |
287 | for (PrpEntryIndex = 0; PrpEntryIndex < PrpEntryNo; ++PrpEntryIndex) {\r | |
288 | if (PrpEntryIndex != PrpEntryNo - 1) {\r | |
289 | //\r | |
290 | // Fill all PRP entries except of last one.\r | |
291 | //\r | |
292 | *((UINT64*)(UINTN)PrpListBase + PrpEntryIndex) = PhysicalAddr;\r | |
293 | PhysicalAddr += EFI_PAGE_SIZE;\r | |
294 | } else {\r | |
295 | //\r | |
296 | // Fill last PRP entries with next PRP List pointer.\r | |
297 | //\r | |
298 | *((UINT64*)(UINTN)PrpListBase + PrpEntryIndex) = PrpListPhyAddr + (PrpListIndex + 1) * EFI_PAGE_SIZE;\r | |
299 | }\r | |
300 | }\r | |
301 | }\r | |
302 | //\r | |
303 | // Fill last PRP list.\r | |
304 | //\r | |
305 | PrpListBase = *(UINT64*)PrpListHost + PrpListIndex * EFI_PAGE_SIZE;\r | |
769402ef | 306 | for (PrpEntryIndex = 0; PrpEntryIndex < Remainder; ++PrpEntryIndex) {\r |
eb290d02 FT |
307 | *((UINT64*)(UINTN)PrpListBase + PrpEntryIndex) = PhysicalAddr;\r |
308 | PhysicalAddr += EFI_PAGE_SIZE;\r | |
309 | }\r | |
310 | \r | |
311 | return (VOID*)(UINTN)PrpListPhyAddr;\r | |
312 | \r | |
313 | EXIT:\r | |
314 | PciIo->FreeBuffer (PciIo, *PrpListNo, *PrpListHost);\r | |
315 | return NULL;\r | |
316 | }\r | |
317 | \r | |
318 | \r | |
319 | /**\r | |
320 | Sends an NVM Express Command Packet to an NVM Express controller or namespace. This function supports\r | |
d6c55989 | 321 | both blocking I/O and non-blocking I/O. The blocking I/O functionality is required, and the non-blocking\r |
eb290d02 FT |
322 | I/O functionality is optional.\r |
323 | \r | |
d6c55989 FT |
324 | \r |
325 | @param[in] This A pointer to the EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL instance.\r | |
326 | @param[in] NamespaceId A 32 bit namespace ID as defined in the NVMe specification to which the NVM Express Command\r | |
327 | Packet will be sent. A value of 0 denotes the NVM Express controller, a value of all 0xFF's\r | |
328 | (all bytes are 0xFF) in the namespace ID specifies that the command packet should be sent to\r | |
329 | all valid namespaces.\r | |
330 | @param[in,out] Packet A pointer to the NVM Express Command Packet.\r | |
331 | @param[in] Event If non-blocking I/O is not supported then Event is ignored, and blocking I/O is performed.\r | |
332 | If Event is NULL, then blocking I/O is performed. If Event is not NULL and non-blocking I/O\r | |
333 | is supported, then non-blocking I/O is performed, and Event will be signaled when the NVM\r | |
eb290d02 FT |
334 | Express Command Packet completes.\r |
335 | \r | |
336 | @retval EFI_SUCCESS The NVM Express Command Packet was sent by the host. TransferLength bytes were transferred\r | |
337 | to, or from DataBuffer.\r | |
338 | @retval EFI_BAD_BUFFER_SIZE The NVM Express Command Packet was not executed. The number of bytes that could be transferred\r | |
339 | is returned in TransferLength.\r | |
340 | @retval EFI_NOT_READY The NVM Express Command Packet could not be sent because the controller is not ready. The caller\r | |
341 | may retry again later.\r | |
342 | @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the NVM Express Command Packet.\r | |
d6c55989 | 343 | @retval EFI_INVALID_PARAMETER NamespaceId or the contents of EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET are invalid. The NVM\r |
eb290d02 | 344 | Express Command Packet was not sent, so no additional status information is available.\r |
d6c55989 FT |
345 | @retval EFI_UNSUPPORTED The command described by the NVM Express Command Packet is not supported by the NVM Express\r |
346 | controller. The NVM Express Command Packet was not sent so no additional status information\r | |
347 | is available.\r | |
eb290d02 FT |
348 | @retval EFI_TIMEOUT A timeout occurred while waiting for the NVM Express Command Packet to execute.\r |
349 | \r | |
350 | **/\r | |
351 | EFI_STATUS\r | |
352 | EFIAPI\r | |
353 | NvmExpressPassThru (\r | |
d6c55989 | 354 | IN EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL *This,\r |
eb290d02 | 355 | IN UINT32 NamespaceId,\r |
d6c55989 | 356 | IN OUT EFI_NVM_EXPRESS_PASS_THRU_COMMAND_PACKET *Packet,\r |
eb290d02 FT |
357 | IN EFI_EVENT Event OPTIONAL\r |
358 | )\r | |
359 | {\r | |
3c52deaf HW |
360 | NVME_CONTROLLER_PRIVATE_DATA *Private;\r |
361 | EFI_STATUS Status;\r | |
362 | EFI_PCI_IO_PROTOCOL *PciIo;\r | |
363 | NVME_SQ *Sq;\r | |
364 | NVME_CQ *Cq;\r | |
365 | UINT16 QueueId;\r | |
366 | UINT32 Bytes;\r | |
367 | UINT16 Offset;\r | |
368 | EFI_EVENT TimerEvent;\r | |
369 | EFI_PCI_IO_PROTOCOL_OPERATION Flag;\r | |
370 | EFI_PHYSICAL_ADDRESS PhyAddr;\r | |
371 | VOID *MapData;\r | |
372 | VOID *MapMeta;\r | |
373 | VOID *MapPrpList;\r | |
374 | UINTN MapLength;\r | |
375 | UINT64 *Prp;\r | |
376 | VOID *PrpListHost;\r | |
377 | UINTN PrpListNo;\r | |
491f6026 | 378 | UINT32 Attributes;\r |
3c52deaf | 379 | UINT32 IoAlign;\r |
b7f82a3a | 380 | UINT32 MaxTransLen;\r |
3c52deaf HW |
381 | UINT32 Data;\r |
382 | NVME_PASS_THRU_ASYNC_REQ *AsyncRequest;\r | |
383 | EFI_TPL OldTpl;\r | |
eb290d02 FT |
384 | \r |
385 | //\r | |
386 | // check the data fields in Packet parameter.\r | |
387 | //\r | |
388 | if ((This == NULL) || (Packet == NULL)) {\r | |
389 | return EFI_INVALID_PARAMETER;\r | |
390 | }\r | |
391 | \r | |
d6c55989 | 392 | if ((Packet->NvmeCmd == NULL) || (Packet->NvmeCompletion == NULL)) {\r |
eb290d02 FT |
393 | return EFI_INVALID_PARAMETER;\r |
394 | }\r | |
395 | \r | |
d6c55989 | 396 | if (Packet->QueueType != NVME_ADMIN_QUEUE && Packet->QueueType != NVME_IO_QUEUE) {\r |
eb290d02 FT |
397 | return EFI_INVALID_PARAMETER;\r |
398 | }\r | |
399 | \r | |
491f6026 HW |
400 | //\r |
401 | // 'Attributes' with neither EFI_NVM_EXPRESS_PASS_THRU_ATTRIBUTES_LOGICAL nor\r | |
402 | // EFI_NVM_EXPRESS_PASS_THRU_ATTRIBUTES_PHYSICAL set is an illegal\r | |
403 | // configuration.\r | |
404 | //\r | |
405 | Attributes = This->Mode->Attributes;\r | |
406 | if ((Attributes & (EFI_NVM_EXPRESS_PASS_THRU_ATTRIBUTES_PHYSICAL |\r | |
407 | EFI_NVM_EXPRESS_PASS_THRU_ATTRIBUTES_LOGICAL)) == 0) {\r | |
408 | return EFI_INVALID_PARAMETER;\r | |
409 | }\r | |
410 | \r | |
3c52deaf HW |
411 | //\r |
412 | // Buffer alignment check for TransferBuffer & MetadataBuffer.\r | |
413 | //\r | |
491f6026 | 414 | IoAlign = This->Mode->IoAlign;\r |
3c52deaf HW |
415 | if (IoAlign > 0 && (((UINTN) Packet->TransferBuffer & (IoAlign - 1)) != 0)) {\r |
416 | return EFI_INVALID_PARAMETER;\r | |
417 | }\r | |
418 | \r | |
419 | if (IoAlign > 0 && (((UINTN) Packet->MetadataBuffer & (IoAlign - 1)) != 0)) {\r | |
420 | return EFI_INVALID_PARAMETER;\r | |
421 | }\r | |
422 | \r | |
eb290d02 | 423 | Private = NVME_CONTROLLER_PRIVATE_DATA_FROM_PASS_THRU (This);\r |
b7f82a3a HW |
424 | \r |
425 | //\r | |
80b405a6 HW |
426 | // Check NamespaceId is valid or not.\r |
427 | //\r | |
428 | if ((NamespaceId > Private->ControllerData->Nn) &&\r | |
429 | (NamespaceId != (UINT32) -1)) {\r | |
430 | return EFI_INVALID_PARAMETER;\r | |
431 | }\r | |
432 | \r | |
433 | //\r | |
b7f82a3a HW |
434 | // Check whether TransferLength exceeds the maximum data transfer size.\r |
435 | //\r | |
436 | if (Private->ControllerData->Mdts != 0) {\r | |
437 | MaxTransLen = (1 << (Private->ControllerData->Mdts)) *\r | |
438 | (1 << (Private->Cap.Mpsmin + 12));\r | |
439 | if (Packet->TransferLength > MaxTransLen) {\r | |
440 | Packet->TransferLength = MaxTransLen;\r | |
441 | return EFI_BAD_BUFFER_SIZE;\r | |
442 | }\r | |
443 | }\r | |
444 | \r | |
eb290d02 FT |
445 | PciIo = Private->PciIo;\r |
446 | MapData = NULL;\r | |
447 | MapMeta = NULL;\r | |
448 | MapPrpList = NULL;\r | |
449 | PrpListHost = NULL;\r | |
450 | PrpListNo = 0;\r | |
451 | Prp = NULL;\r | |
452 | TimerEvent = NULL;\r | |
453 | Status = EFI_SUCCESS;\r | |
454 | \r | |
758ea946 HW |
455 | if (Packet->QueueType == NVME_ADMIN_QUEUE) {\r |
456 | QueueId = 0;\r | |
457 | } else {\r | |
458 | if (Event == NULL) {\r | |
459 | QueueId = 1;\r | |
460 | } else {\r | |
461 | QueueId = 2;\r | |
462 | \r | |
463 | //\r | |
464 | // Submission queue full check.\r | |
465 | //\r | |
466 | if ((Private->SqTdbl[QueueId].Sqt + 1) % (NVME_ASYNC_CSQ_SIZE + 1) ==\r | |
467 | Private->AsyncSqHead) {\r | |
468 | return EFI_NOT_READY;\r | |
469 | }\r | |
470 | }\r | |
471 | }\r | |
472 | Sq = Private->SqBuffer[QueueId] + Private->SqTdbl[QueueId].Sqt;\r | |
473 | Cq = Private->CqBuffer[QueueId] + Private->CqHdbl[QueueId].Cqh;\r | |
eb290d02 FT |
474 | \r |
475 | if (Packet->NvmeCmd->Nsid != NamespaceId) {\r | |
476 | return EFI_INVALID_PARAMETER;\r | |
477 | }\r | |
478 | \r | |
479 | ZeroMem (Sq, sizeof (NVME_SQ));\r | |
d6c55989 FT |
480 | Sq->Opc = (UINT8)Packet->NvmeCmd->Cdw0.Opcode;\r |
481 | Sq->Fuse = (UINT8)Packet->NvmeCmd->Cdw0.FusedOperation;\r | |
758ea946 | 482 | Sq->Cid = Private->Cid[QueueId]++;\r |
eb290d02 FT |
483 | Sq->Nsid = Packet->NvmeCmd->Nsid;\r |
484 | \r | |
485 | //\r | |
486 | // Currently we only support PRP for data transfer, SGL is NOT supported.\r | |
487 | //\r | |
7b8883c6 FT |
488 | ASSERT (Sq->Psdt == 0);\r |
489 | if (Sq->Psdt != 0) {\r | |
eb290d02 FT |
490 | DEBUG ((EFI_D_ERROR, "NvmExpressPassThru: doesn't support SGL mechanism\n"));\r |
491 | return EFI_UNSUPPORTED;\r | |
492 | }\r | |
493 | \r | |
494 | Sq->Prp[0] = (UINT64)(UINTN)Packet->TransferBuffer;\r | |
495 | //\r | |
496 | // If the NVMe cmd has data in or out, then mapping the user buffer to the PCI controller specific addresses.\r | |
497 | // Note here we don't handle data buffer for CreateIOSubmitionQueue and CreateIOCompletionQueue cmds because\r | |
498 | // these two cmds are special which requires their data buffer must support simultaneous access by both the\r | |
499 | // processor and a PCI Bus Master. It's caller's responsbility to ensure this.\r | |
500 | //\r | |
754b489b | 501 | if (((Sq->Opc & (BIT0 | BIT1)) != 0) && (Sq->Opc != NVME_ADMIN_CRIOCQ_CMD) && (Sq->Opc != NVME_ADMIN_CRIOSQ_CMD)) {\r |
b7f82a3a HW |
502 | if ((Packet->TransferLength == 0) || (Packet->TransferBuffer == NULL)) {\r |
503 | return EFI_INVALID_PARAMETER;\r | |
504 | }\r | |
505 | \r | |
eb290d02 FT |
506 | if ((Sq->Opc & BIT0) != 0) {\r |
507 | Flag = EfiPciIoOperationBusMasterRead;\r | |
508 | } else {\r | |
509 | Flag = EfiPciIoOperationBusMasterWrite;\r | |
510 | }\r | |
511 | \r | |
512 | MapLength = Packet->TransferLength;\r | |
513 | Status = PciIo->Map (\r | |
514 | PciIo,\r | |
515 | Flag,\r | |
516 | Packet->TransferBuffer,\r | |
517 | &MapLength,\r | |
518 | &PhyAddr,\r | |
519 | &MapData\r | |
520 | );\r | |
521 | if (EFI_ERROR (Status) || (Packet->TransferLength != MapLength)) {\r | |
522 | return EFI_OUT_OF_RESOURCES;\r | |
523 | }\r | |
524 | \r | |
525 | Sq->Prp[0] = PhyAddr;\r | |
526 | Sq->Prp[1] = 0;\r | |
527 | \r | |
b7f82a3a | 528 | if((Packet->MetadataLength != 0) && (Packet->MetadataBuffer != NULL)) {\r |
eb290d02 FT |
529 | MapLength = Packet->MetadataLength;\r |
530 | Status = PciIo->Map (\r | |
531 | PciIo,\r | |
532 | Flag,\r | |
533 | Packet->MetadataBuffer,\r | |
534 | &MapLength,\r | |
535 | &PhyAddr,\r | |
536 | &MapMeta\r | |
537 | );\r | |
538 | if (EFI_ERROR (Status) || (Packet->MetadataLength != MapLength)) {\r | |
539 | PciIo->Unmap (\r | |
540 | PciIo,\r | |
541 | MapData\r | |
542 | );\r | |
543 | \r | |
544 | return EFI_OUT_OF_RESOURCES;\r | |
545 | }\r | |
546 | Sq->Mptr = PhyAddr;\r | |
547 | }\r | |
548 | }\r | |
549 | //\r | |
550 | // If the buffer size spans more than two memory pages (page size as defined in CC.Mps),\r | |
551 | // then build a PRP list in the second PRP submission queue entry.\r | |
552 | //\r | |
553 | Offset = ((UINT16)Sq->Prp[0]) & (EFI_PAGE_SIZE - 1);\r | |
554 | Bytes = Packet->TransferLength;\r | |
555 | \r | |
556 | if ((Offset + Bytes) > (EFI_PAGE_SIZE * 2)) {\r | |
557 | //\r | |
558 | // Create PrpList for remaining data buffer.\r | |
559 | //\r | |
560 | PhyAddr = (Sq->Prp[0] + EFI_PAGE_SIZE) & ~(EFI_PAGE_SIZE - 1);\r | |
561 | Prp = NvmeCreatePrpList (PciIo, PhyAddr, EFI_SIZE_TO_PAGES(Offset + Bytes) - 1, &PrpListHost, &PrpListNo, &MapPrpList);\r | |
562 | if (Prp == NULL) {\r | |
563 | goto EXIT;\r | |
564 | }\r | |
565 | \r | |
566 | Sq->Prp[1] = (UINT64)(UINTN)Prp;\r | |
567 | } else if ((Offset + Bytes) > EFI_PAGE_SIZE) {\r | |
568 | Sq->Prp[1] = (Sq->Prp[0] + EFI_PAGE_SIZE) & ~(EFI_PAGE_SIZE - 1);\r | |
569 | }\r | |
570 | \r | |
d6c55989 FT |
571 | if(Packet->NvmeCmd->Flags & CDW2_VALID) {\r |
572 | Sq->Rsvd2 = (UINT64)Packet->NvmeCmd->Cdw2;\r | |
573 | }\r | |
574 | if(Packet->NvmeCmd->Flags & CDW3_VALID) {\r | |
575 | Sq->Rsvd2 |= LShiftU64 ((UINT64)Packet->NvmeCmd->Cdw3, 32);\r | |
576 | }\r | |
eb290d02 FT |
577 | if(Packet->NvmeCmd->Flags & CDW10_VALID) {\r |
578 | Sq->Payload.Raw.Cdw10 = Packet->NvmeCmd->Cdw10;\r | |
579 | }\r | |
580 | if(Packet->NvmeCmd->Flags & CDW11_VALID) {\r | |
581 | Sq->Payload.Raw.Cdw11 = Packet->NvmeCmd->Cdw11;\r | |
582 | }\r | |
583 | if(Packet->NvmeCmd->Flags & CDW12_VALID) {\r | |
584 | Sq->Payload.Raw.Cdw12 = Packet->NvmeCmd->Cdw12;\r | |
585 | }\r | |
586 | if(Packet->NvmeCmd->Flags & CDW13_VALID) {\r | |
587 | Sq->Payload.Raw.Cdw13 = Packet->NvmeCmd->Cdw13;\r | |
588 | }\r | |
589 | if(Packet->NvmeCmd->Flags & CDW14_VALID) {\r | |
590 | Sq->Payload.Raw.Cdw14 = Packet->NvmeCmd->Cdw14;\r | |
591 | }\r | |
592 | if(Packet->NvmeCmd->Flags & CDW15_VALID) {\r | |
593 | Sq->Payload.Raw.Cdw15 = Packet->NvmeCmd->Cdw15;\r | |
594 | }\r | |
595 | \r | |
596 | //\r | |
597 | // Ring the submission queue doorbell.\r | |
598 | //\r | |
758ea946 HW |
599 | if (Event != NULL) {\r |
600 | Private->SqTdbl[QueueId].Sqt =\r | |
601 | (Private->SqTdbl[QueueId].Sqt + 1) % (NVME_ASYNC_CSQ_SIZE + 1);\r | |
602 | } else {\r | |
603 | Private->SqTdbl[QueueId].Sqt ^= 1;\r | |
604 | }\r | |
605 | Data = ReadUnaligned32 ((UINT32*)&Private->SqTdbl[QueueId]);\r | |
eb290d02 FT |
606 | PciIo->Mem.Write (\r |
607 | PciIo,\r | |
608 | EfiPciIoWidthUint32,\r | |
609 | NVME_BAR,\r | |
758ea946 | 610 | NVME_SQTDBL_OFFSET(QueueId, Private->Cap.Dstrd),\r |
eb290d02 | 611 | 1,\r |
7b8883c6 | 612 | &Data\r |
eb290d02 FT |
613 | );\r |
614 | \r | |
758ea946 HW |
615 | //\r |
616 | // For non-blocking requests, return directly if the command is placed\r | |
617 | // in the submission queue.\r | |
618 | //\r | |
619 | if (Event != NULL) {\r | |
620 | AsyncRequest = AllocateZeroPool (sizeof (NVME_PASS_THRU_ASYNC_REQ));\r | |
621 | if (AsyncRequest == NULL) {\r | |
622 | Status = EFI_DEVICE_ERROR;\r | |
623 | goto EXIT;\r | |
624 | }\r | |
625 | \r | |
626 | AsyncRequest->Signature = NVME_PASS_THRU_ASYNC_REQ_SIG;\r | |
627 | AsyncRequest->Packet = Packet;\r | |
628 | AsyncRequest->CommandId = Sq->Cid;\r | |
629 | AsyncRequest->CallerEvent = Event;\r | |
630 | \r | |
631 | OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r | |
632 | InsertTailList (&Private->AsyncPassThruQueue, &AsyncRequest->Link);\r | |
633 | gBS->RestoreTPL (OldTpl);\r | |
634 | \r | |
635 | return EFI_SUCCESS;\r | |
636 | }\r | |
637 | \r | |
eb290d02 FT |
638 | Status = gBS->CreateEvent (\r |
639 | EVT_TIMER,\r | |
640 | TPL_CALLBACK,\r | |
641 | NULL,\r | |
642 | NULL,\r | |
643 | &TimerEvent\r | |
644 | );\r | |
645 | if (EFI_ERROR (Status)) {\r | |
646 | goto EXIT;\r | |
647 | }\r | |
648 | \r | |
649 | Status = gBS->SetTimer(TimerEvent, TimerRelative, Packet->CommandTimeout);\r | |
650 | \r | |
651 | if (EFI_ERROR(Status)) {\r | |
eb290d02 FT |
652 | goto EXIT;\r |
653 | }\r | |
654 | \r | |
655 | //\r | |
656 | // Wait for completion queue to get filled in.\r | |
657 | //\r | |
658 | Status = EFI_TIMEOUT;\r | |
eb290d02 | 659 | while (EFI_ERROR (gBS->CheckEvent (TimerEvent))) {\r |
758ea946 | 660 | if (Cq->Pt != Private->Pt[QueueId]) {\r |
eb290d02 | 661 | Status = EFI_SUCCESS;\r |
eb290d02 FT |
662 | break;\r |
663 | }\r | |
664 | }\r | |
665 | \r | |
eb290d02 | 666 | //\r |
754b489b | 667 | // Check the NVMe cmd execution result\r |
eb290d02 | 668 | //\r |
754b489b TF |
669 | if (Status != EFI_TIMEOUT) {\r |
670 | if ((Cq->Sct == 0) && (Cq->Sc == 0)) {\r | |
671 | Status = EFI_SUCCESS;\r | |
672 | } else {\r | |
673 | Status = EFI_DEVICE_ERROR;\r | |
674 | //\r | |
675 | // Copy the Respose Queue entry for this command to the callers response buffer\r | |
676 | //\r | |
677 | CopyMem(Packet->NvmeCompletion, Cq, sizeof(EFI_NVM_EXPRESS_COMPLETION));\r | |
678 | \r | |
679 | //\r | |
680 | // Dump every completion entry status for debugging.\r | |
681 | //\r | |
682 | DEBUG_CODE_BEGIN();\r | |
683 | NvmeDumpStatus(Cq);\r | |
684 | DEBUG_CODE_END();\r | |
685 | }\r | |
686 | }\r | |
eb290d02 | 687 | \r |
758ea946 HW |
688 | if ((Private->CqHdbl[QueueId].Cqh ^= 1) == 0) {\r |
689 | Private->Pt[QueueId] ^= 1;\r | |
754b489b | 690 | }\r |
eb290d02 | 691 | \r |
758ea946 | 692 | Data = ReadUnaligned32 ((UINT32*)&Private->CqHdbl[QueueId]);\r |
eb290d02 FT |
693 | PciIo->Mem.Write (\r |
694 | PciIo,\r | |
695 | EfiPciIoWidthUint32,\r | |
696 | NVME_BAR,\r | |
758ea946 | 697 | NVME_CQHDBL_OFFSET(QueueId, Private->Cap.Dstrd),\r |
eb290d02 | 698 | 1,\r |
7b8883c6 | 699 | &Data\r |
eb290d02 FT |
700 | );\r |
701 | \r | |
702 | EXIT:\r | |
703 | if (MapData != NULL) {\r | |
704 | PciIo->Unmap (\r | |
705 | PciIo,\r | |
706 | MapData\r | |
707 | );\r | |
708 | }\r | |
709 | \r | |
710 | if (MapMeta != NULL) {\r | |
711 | PciIo->Unmap (\r | |
712 | PciIo,\r | |
713 | MapMeta\r | |
714 | );\r | |
715 | }\r | |
716 | \r | |
717 | if (MapPrpList != NULL) {\r | |
718 | PciIo->Unmap (\r | |
719 | PciIo,\r | |
720 | MapPrpList\r | |
721 | );\r | |
722 | }\r | |
723 | \r | |
724 | if (Prp != NULL) {\r | |
725 | PciIo->FreeBuffer (PciIo, PrpListNo, PrpListHost);\r | |
726 | }\r | |
727 | \r | |
728 | if (TimerEvent != NULL) {\r | |
729 | gBS->CloseEvent (TimerEvent);\r | |
730 | }\r | |
731 | return Status;\r | |
732 | }\r | |
733 | \r | |
734 | /**\r | |
d6c55989 | 735 | Used to retrieve the next namespace ID for this NVM Express controller.\r |
eb290d02 | 736 | \r |
d6c55989 FT |
737 | The EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL.GetNextNamespace() function retrieves the next valid\r |
738 | namespace ID on this NVM Express controller.\r | |
eb290d02 | 739 | \r |
d6c55989 FT |
740 | If on input the value pointed to by NamespaceId is 0xFFFFFFFF, then the first valid namespace\r |
741 | ID defined on the NVM Express controller is returned in the location pointed to by NamespaceId\r | |
742 | and a status of EFI_SUCCESS is returned.\r | |
eb290d02 | 743 | \r |
d6c55989 FT |
744 | If on input the value pointed to by NamespaceId is an invalid namespace ID other than 0xFFFFFFFF,\r |
745 | then EFI_INVALID_PARAMETER is returned.\r | |
eb290d02 | 746 | \r |
d6c55989 FT |
747 | If on input the value pointed to by NamespaceId is a valid namespace ID, then the next valid\r |
748 | namespace ID on the NVM Express controller is returned in the location pointed to by NamespaceId,\r | |
749 | and EFI_SUCCESS is returned.\r | |
eb290d02 | 750 | \r |
d6c55989 FT |
751 | If the value pointed to by NamespaceId is the namespace ID of the last namespace on the NVM\r |
752 | Express controller, then EFI_NOT_FOUND is returned.\r | |
753 | \r | |
754 | @param[in] This A pointer to the EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL instance.\r | |
eb290d02 FT |
755 | @param[in,out] NamespaceId On input, a pointer to a legal NamespaceId for an NVM Express\r |
756 | namespace present on the NVM Express controller. On output, a\r | |
757 | pointer to the next NamespaceId of an NVM Express namespace on\r | |
758 | an NVM Express controller. An input value of 0xFFFFFFFF retrieves\r | |
759 | the first NamespaceId for an NVM Express namespace present on an\r | |
760 | NVM Express controller.\r | |
eb290d02 | 761 | \r |
d6c55989 | 762 | @retval EFI_SUCCESS The Namespace ID of the next Namespace was returned.\r |
eb290d02 | 763 | @retval EFI_NOT_FOUND There are no more namespaces defined on this controller.\r |
d6c55989 | 764 | @retval EFI_INVALID_PARAMETER NamespaceId is an invalid value other than 0xFFFFFFFF.\r |
eb290d02 FT |
765 | \r |
766 | **/\r | |
767 | EFI_STATUS\r | |
768 | EFIAPI\r | |
769 | NvmExpressGetNextNamespace (\r | |
d6c55989 FT |
770 | IN EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL *This,\r |
771 | IN OUT UINT32 *NamespaceId\r | |
eb290d02 FT |
772 | )\r |
773 | {\r | |
774 | NVME_CONTROLLER_PRIVATE_DATA *Private;\r | |
775 | NVME_ADMIN_NAMESPACE_DATA *NamespaceData;\r | |
776 | UINT32 NextNamespaceId;\r | |
777 | EFI_STATUS Status;\r | |
778 | \r | |
779 | if ((This == NULL) || (NamespaceId == NULL)) {\r | |
780 | return EFI_INVALID_PARAMETER;\r | |
781 | }\r | |
782 | \r | |
783 | NamespaceData = NULL;\r | |
784 | Status = EFI_NOT_FOUND;\r | |
785 | \r | |
786 | Private = NVME_CONTROLLER_PRIVATE_DATA_FROM_PASS_THRU (This);\r | |
787 | //\r | |
788 | // If the NamespaceId input value is 0xFFFFFFFF, then get the first valid namespace ID\r | |
789 | //\r | |
790 | if (*NamespaceId == 0xFFFFFFFF) {\r | |
791 | //\r | |
792 | // Start with the first namespace ID\r | |
793 | //\r | |
794 | NextNamespaceId = 1;\r | |
795 | //\r | |
796 | // Allocate buffer for Identify Namespace data.\r | |
797 | //\r | |
798 | NamespaceData = (NVME_ADMIN_NAMESPACE_DATA *)AllocateZeroPool (sizeof (NVME_ADMIN_NAMESPACE_DATA));\r | |
799 | \r | |
800 | if (NamespaceData == NULL) {\r | |
801 | return EFI_NOT_FOUND;\r | |
802 | }\r | |
803 | \r | |
804 | Status = NvmeIdentifyNamespace (Private, NextNamespaceId, NamespaceData);\r | |
805 | if (EFI_ERROR(Status)) {\r | |
806 | goto Done;\r | |
807 | }\r | |
808 | \r | |
809 | *NamespaceId = NextNamespaceId;\r | |
eb290d02 | 810 | } else {\r |
114358ea | 811 | if (*NamespaceId > Private->ControllerData->Nn) {\r |
eb290d02 FT |
812 | return EFI_INVALID_PARAMETER;\r |
813 | }\r | |
814 | \r | |
815 | NextNamespaceId = *NamespaceId + 1;\r | |
114358ea HW |
816 | if (NextNamespaceId > Private->ControllerData->Nn) {\r |
817 | return EFI_NOT_FOUND;\r | |
818 | }\r | |
819 | \r | |
eb290d02 FT |
820 | //\r |
821 | // Allocate buffer for Identify Namespace data.\r | |
822 | //\r | |
823 | NamespaceData = (NVME_ADMIN_NAMESPACE_DATA *)AllocateZeroPool (sizeof (NVME_ADMIN_NAMESPACE_DATA));\r | |
824 | if (NamespaceData == NULL) {\r | |
825 | return EFI_NOT_FOUND;\r | |
826 | }\r | |
827 | \r | |
828 | Status = NvmeIdentifyNamespace (Private, NextNamespaceId, NamespaceData);\r | |
829 | if (EFI_ERROR(Status)) {\r | |
830 | goto Done;\r | |
831 | }\r | |
832 | \r | |
833 | *NamespaceId = NextNamespaceId;\r | |
eb290d02 FT |
834 | }\r |
835 | \r | |
836 | Done:\r | |
837 | if (NamespaceData != NULL) {\r | |
838 | FreePool(NamespaceData);\r | |
839 | }\r | |
840 | \r | |
841 | return Status;\r | |
842 | }\r | |
843 | \r | |
844 | /**\r | |
d6c55989 FT |
845 | Used to translate a device path node to a namespace ID.\r |
846 | \r | |
847 | The EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL.GetNamespace() function determines the namespace ID associated with the\r | |
848 | namespace described by DevicePath.\r | |
eb290d02 | 849 | \r |
d6c55989 FT |
850 | If DevicePath is a device path node type that the NVM Express Pass Thru driver supports, then the NVM Express\r |
851 | Pass Thru driver will attempt to translate the contents DevicePath into a namespace ID.\r | |
eb290d02 | 852 | \r |
d6c55989 FT |
853 | If this translation is successful, then that namespace ID is returned in NamespaceId, and EFI_SUCCESS is returned\r |
854 | \r | |
855 | @param[in] This A pointer to the EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL instance.\r | |
eb290d02 FT |
856 | @param[in] DevicePath A pointer to the device path node that describes an NVM Express namespace on\r |
857 | the NVM Express controller.\r | |
858 | @param[out] NamespaceId The NVM Express namespace ID contained in the device path node.\r | |
eb290d02 | 859 | \r |
d6c55989 FT |
860 | @retval EFI_SUCCESS DevicePath was successfully translated to NamespaceId.\r |
861 | @retval EFI_INVALID_PARAMETER If DevicePath or NamespaceId are NULL, then EFI_INVALID_PARAMETER is returned.\r | |
eb290d02 FT |
862 | @retval EFI_UNSUPPORTED If DevicePath is not a device path node type that the NVM Express Pass Thru driver\r |
863 | supports, then EFI_UNSUPPORTED is returned.\r | |
d6c55989 FT |
864 | @retval EFI_NOT_FOUND If DevicePath is a device path node type that the NVM Express Pass Thru driver\r |
865 | supports, but there is not a valid translation from DevicePath to a namespace ID,\r | |
866 | then EFI_NOT_FOUND is returned.\r | |
eb290d02 FT |
867 | **/\r |
868 | EFI_STATUS\r | |
869 | EFIAPI\r | |
870 | NvmExpressGetNamespace (\r | |
d6c55989 | 871 | IN EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL *This,\r |
eb290d02 | 872 | IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r |
d6c55989 | 873 | OUT UINT32 *NamespaceId\r |
eb290d02 FT |
874 | )\r |
875 | {\r | |
876 | NVME_NAMESPACE_DEVICE_PATH *Node;\r | |
284dc9bf | 877 | NVME_CONTROLLER_PRIVATE_DATA *Private;\r |
eb290d02 | 878 | \r |
d6c55989 | 879 | if ((This == NULL) || (DevicePath == NULL) || (NamespaceId == NULL)) {\r |
eb290d02 FT |
880 | return EFI_INVALID_PARAMETER;\r |
881 | }\r | |
882 | \r | |
883 | if (DevicePath->Type != MESSAGING_DEVICE_PATH) {\r | |
884 | return EFI_UNSUPPORTED;\r | |
885 | }\r | |
886 | \r | |
284dc9bf HW |
887 | Node = (NVME_NAMESPACE_DEVICE_PATH *)DevicePath;\r |
888 | Private = NVME_CONTROLLER_PRIVATE_DATA_FROM_PASS_THRU (This);\r | |
eb290d02 FT |
889 | \r |
890 | if (DevicePath->SubType == MSG_NVME_NAMESPACE_DP) {\r | |
891 | if (DevicePathNodeLength(DevicePath) != sizeof(NVME_NAMESPACE_DEVICE_PATH)) {\r | |
892 | return EFI_NOT_FOUND;\r | |
893 | }\r | |
894 | \r | |
284dc9bf HW |
895 | //\r |
896 | // Check NamespaceId in the device path node is valid or not.\r | |
897 | //\r | |
898 | if ((Node->NamespaceId == 0) ||\r | |
899 | (Node->NamespaceId > Private->ControllerData->Nn)) {\r | |
900 | return EFI_NOT_FOUND;\r | |
901 | }\r | |
902 | \r | |
d6c55989 | 903 | *NamespaceId = Node->NamespaceId;\r |
eb290d02 FT |
904 | \r |
905 | return EFI_SUCCESS;\r | |
906 | } else {\r | |
907 | return EFI_UNSUPPORTED;\r | |
908 | }\r | |
909 | }\r | |
910 | \r | |
911 | /**\r | |
912 | Used to allocate and build a device path node for an NVM Express namespace on an NVM Express controller.\r | |
913 | \r | |
d6c55989 | 914 | The EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL.BuildDevicePath() function allocates and builds a single device\r |
eb290d02 FT |
915 | path node for the NVM Express namespace specified by NamespaceId.\r |
916 | \r | |
d6c55989 | 917 | If the NamespaceId is not valid, then EFI_NOT_FOUND is returned.\r |
eb290d02 FT |
918 | \r |
919 | If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned.\r | |
920 | \r | |
921 | If there are not enough resources to allocate the device path node, then EFI_OUT_OF_RESOURCES is returned.\r | |
922 | \r | |
923 | Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of DevicePath are\r | |
924 | initialized to describe the NVM Express namespace specified by NamespaceId, and EFI_SUCCESS is returned.\r | |
925 | \r | |
d6c55989 | 926 | @param[in] This A pointer to the EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL instance.\r |
eb290d02 FT |
927 | @param[in] NamespaceId The NVM Express namespace ID for which a device path node is to be\r |
928 | allocated and built. Caller must set the NamespaceId to zero if the\r | |
929 | device path node will contain a valid UUID.\r | |
eb290d02 FT |
930 | @param[in,out] DevicePath A pointer to a single device path node that describes the NVM Express\r |
931 | namespace specified by NamespaceId. This function is responsible for\r | |
932 | allocating the buffer DevicePath with the boot service AllocatePool().\r | |
933 | It is the caller's responsibility to free DevicePath when the caller\r | |
934 | is finished with DevicePath.\r | |
935 | @retval EFI_SUCCESS The device path node that describes the NVM Express namespace specified\r | |
936 | by NamespaceId was allocated and returned in DevicePath.\r | |
d6c55989 | 937 | @retval EFI_NOT_FOUND The NamespaceId is not valid.\r |
eb290d02 FT |
938 | @retval EFI_INVALID_PARAMETER DevicePath is NULL.\r |
939 | @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate the DevicePath node.\r | |
940 | \r | |
941 | **/\r | |
942 | EFI_STATUS\r | |
943 | EFIAPI\r | |
944 | NvmExpressBuildDevicePath (\r | |
d6c55989 | 945 | IN EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL *This,\r |
eb290d02 | 946 | IN UINT32 NamespaceId,\r |
eb290d02 FT |
947 | IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath\r |
948 | )\r | |
949 | {\r | |
eb290d02 | 950 | NVME_NAMESPACE_DEVICE_PATH *Node;\r |
d6c55989 FT |
951 | NVME_CONTROLLER_PRIVATE_DATA *Private;\r |
952 | EFI_STATUS Status;\r | |
953 | NVME_ADMIN_NAMESPACE_DATA *NamespaceData;\r | |
eb290d02 FT |
954 | \r |
955 | //\r | |
956 | // Validate parameters\r | |
957 | //\r | |
958 | if ((This == NULL) || (DevicePath == NULL)) {\r | |
959 | return EFI_INVALID_PARAMETER;\r | |
960 | }\r | |
961 | \r | |
d6c55989 FT |
962 | Status = EFI_SUCCESS;\r |
963 | Private = NVME_CONTROLLER_PRIVATE_DATA_FROM_PASS_THRU (This);\r | |
eb290d02 | 964 | \r |
946f48eb HW |
965 | //\r |
966 | // Check NamespaceId is valid or not.\r | |
967 | //\r | |
968 | if ((NamespaceId == 0) ||\r | |
969 | (NamespaceId > Private->ControllerData->Nn)) {\r | |
970 | return EFI_NOT_FOUND;\r | |
971 | }\r | |
972 | \r | |
d6c55989 | 973 | Node = (NVME_NAMESPACE_DEVICE_PATH *)AllocateZeroPool (sizeof (NVME_NAMESPACE_DEVICE_PATH));\r |
eb290d02 FT |
974 | if (Node == NULL) {\r |
975 | return EFI_OUT_OF_RESOURCES;\r | |
976 | }\r | |
977 | \r | |
978 | Node->Header.Type = MESSAGING_DEVICE_PATH;\r | |
979 | Node->Header.SubType = MSG_NVME_NAMESPACE_DP;\r | |
980 | SetDevicePathNodeLength (&Node->Header, sizeof (NVME_NAMESPACE_DEVICE_PATH));\r | |
981 | Node->NamespaceId = NamespaceId;\r | |
d6c55989 FT |
982 | \r |
983 | //\r | |
984 | // Allocate a buffer for Identify Namespace data.\r | |
985 | //\r | |
986 | NamespaceData = NULL;\r | |
987 | NamespaceData = AllocateZeroPool(sizeof (NVME_ADMIN_NAMESPACE_DATA));\r | |
988 | if(NamespaceData == NULL) {\r | |
989 | Status = EFI_OUT_OF_RESOURCES;\r | |
990 | goto Exit;\r | |
991 | }\r | |
992 | \r | |
993 | //\r | |
994 | // Get UUID from specified Identify Namespace data.\r | |
995 | //\r | |
996 | Status = NvmeIdentifyNamespace (\r | |
997 | Private,\r | |
998 | NamespaceId,\r | |
999 | (VOID *)NamespaceData\r | |
1000 | );\r | |
1001 | \r | |
1002 | if (EFI_ERROR(Status)) {\r | |
1003 | goto Exit;\r | |
1004 | }\r | |
1005 | \r | |
1006 | Node->NamespaceUuid = NamespaceData->Eui64;\r | |
eb290d02 FT |
1007 | \r |
1008 | *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *)Node;\r | |
d6c55989 FT |
1009 | \r |
1010 | Exit:\r | |
1011 | if(NamespaceData != NULL) {\r | |
1012 | FreePool (NamespaceData);\r | |
1013 | }\r | |
1014 | \r | |
1015 | if (EFI_ERROR (Status)) {\r | |
1016 | FreePool (Node);\r | |
1017 | }\r | |
1018 | \r | |
1019 | return Status;\r | |
eb290d02 FT |
1020 | }\r |
1021 | \r |