]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Pci/NvmExpressDxe/NvmExpress.c
321dbdef9756d641e8f16c87f1dd0efe47109e17
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / NvmExpressDxe / NvmExpress.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 // NVM Express Driver Binding Protocol Instance
20 //
21 EFI_DRIVER_BINDING_PROTOCOL gNvmExpressDriverBinding = {
22 NvmExpressDriverBindingSupported,
23 NvmExpressDriverBindingStart,
24 NvmExpressDriverBindingStop,
25 0x10,
26 NULL,
27 NULL
28 };
29
30 //
31 // NVM Express EFI Driver Supported EFI Version Protocol Instance
32 //
33 EFI_DRIVER_SUPPORTED_EFI_VERSION_PROTOCOL gNvmExpressDriverSupportedEfiVersion = {
34 sizeof (EFI_DRIVER_SUPPORTED_EFI_VERSION_PROTOCOL), // Size of Protocol structure.
35 0 // Version number to be filled at start up.
36 };
37
38 //
39 // Template for NVM Express Pass Thru Mode data structure.
40 //
41 GLOBAL_REMOVE_IF_UNREFERENCED EFI_NVM_EXPRESS_PASS_THRU_MODE gEfiNvmExpressPassThruMode = {
42 EFI_NVM_EXPRESS_PASS_THRU_ATTRIBUTES_PHYSICAL | EFI_NVM_EXPRESS_PASS_THRU_ATTRIBUTES_LOGICAL | EFI_NVM_EXPRESS_PASS_THRU_ATTRIBUTES_CMD_SET_NVM,
43 sizeof (UINTN),
44 0x10100
45 };
46
47 /**
48 Check if the specified Nvm Express device namespace is active, and create child handles
49 for them with BlockIo and DiskInfo protocol instances.
50
51 @param[in] Private The pointer to the NVME_CONTROLLER_PRIVATE_DATA data structure.
52 @param[in] NamespaceId The NVM Express namespace ID for which a device path node is to be
53 allocated and built. Caller must set the NamespaceId to zero if the
54 device path node will contain a valid UUID.
55
56 @retval EFI_SUCCESS All the namespaces in the device are successfully enumerated.
57 @return Others Some error occurs when enumerating the namespaces.
58
59 **/
60 EFI_STATUS
61 EnumerateNvmeDevNamespace (
62 IN NVME_CONTROLLER_PRIVATE_DATA *Private,
63 UINT32 NamespaceId
64 )
65 {
66 NVME_ADMIN_NAMESPACE_DATA *NamespaceData;
67 EFI_DEVICE_PATH_PROTOCOL *NewDevicePathNode;
68 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
69 EFI_HANDLE DeviceHandle;
70 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
71 EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath;
72 NVME_DEVICE_PRIVATE_DATA *Device;
73 EFI_STATUS Status;
74 UINT32 Lbads;
75 UINT32 Flbas;
76 UINT32 LbaFmtIdx;
77 UINT8 Sn[21];
78 UINT8 Mn[41];
79
80 NewDevicePathNode = NULL;
81 DevicePath = NULL;
82 Device = NULL;
83
84 //
85 // Allocate a buffer for Identify Namespace data
86 //
87 NamespaceData = AllocateZeroPool(sizeof (NVME_ADMIN_NAMESPACE_DATA));
88 if(NamespaceData == NULL) {
89 return EFI_OUT_OF_RESOURCES;
90 }
91
92 ParentDevicePath = Private->ParentDevicePath;
93 //
94 // Identify Namespace
95 //
96 Status = NvmeIdentifyNamespace (
97 Private,
98 NamespaceId,
99 (VOID *)NamespaceData
100 );
101 if (EFI_ERROR(Status)) {
102 goto Exit;
103 }
104 //
105 // Validate Namespace
106 //
107 if (NamespaceData->Ncap == 0) {
108 Status = EFI_DEVICE_ERROR;
109 } else {
110 //
111 // allocate device private data for each discovered namespace
112 //
113 Device = AllocateZeroPool(sizeof(NVME_DEVICE_PRIVATE_DATA));
114 if (Device == NULL) {
115 Status = EFI_OUT_OF_RESOURCES;
116 goto Exit;
117 }
118
119 //
120 // Initialize SSD namespace instance data
121 //
122 Device->Signature = NVME_DEVICE_PRIVATE_DATA_SIGNATURE;
123 Device->NamespaceId = NamespaceId;
124 Device->NamespaceUuid = NamespaceData->Eui64;
125
126 Device->ControllerHandle = Private->ControllerHandle;
127 Device->DriverBindingHandle = Private->DriverBindingHandle;
128 Device->Controller = Private;
129
130 //
131 // Build BlockIo media structure
132 //
133 Device->Media.MediaId = 0;
134 Device->Media.RemovableMedia = FALSE;
135 Device->Media.MediaPresent = TRUE;
136 Device->Media.LogicalPartition = FALSE;
137 Device->Media.ReadOnly = FALSE;
138 Device->Media.WriteCaching = FALSE;
139
140 Flbas = NamespaceData->Flbas;
141 LbaFmtIdx = Flbas & 0xF;
142 Lbads = NamespaceData->LbaFormat[LbaFmtIdx].Lbads;
143 Device->Media.BlockSize = (UINT32)1 << Lbads;
144
145 Device->Media.LastBlock = NamespaceData->Nsze - 1;
146 Device->Media.LogicalBlocksPerPhysicalBlock = 1;
147 Device->Media.LowestAlignedLba = 1;
148
149 //
150 // Create BlockIo Protocol instance
151 //
152 Device->BlockIo.Revision = EFI_BLOCK_IO_PROTOCOL_REVISION2;
153 Device->BlockIo.Media = &Device->Media;
154 Device->BlockIo.Reset = NvmeBlockIoReset;
155 Device->BlockIo.ReadBlocks = NvmeBlockIoReadBlocks;
156 Device->BlockIo.WriteBlocks = NvmeBlockIoWriteBlocks;
157 Device->BlockIo.FlushBlocks = NvmeBlockIoFlushBlocks;
158
159 //
160 // Create StorageSecurityProtocol Instance
161 //
162 Device->StorageSecurity.ReceiveData = NvmeStorageSecurityReceiveData;
163 Device->StorageSecurity.SendData = NvmeStorageSecuritySendData;
164
165 //
166 // Create DiskInfo Protocol instance
167 //
168 CopyMem (&Device->NamespaceData, NamespaceData, sizeof (NVME_ADMIN_NAMESPACE_DATA));
169 InitializeDiskInfo (Device);
170
171 //
172 // Create a Nvm Express Namespace Device Path Node
173 //
174 Status = Private->Passthru.BuildDevicePath (
175 &Private->Passthru,
176 Device->NamespaceId,
177 &NewDevicePathNode
178 );
179
180 if (EFI_ERROR(Status)) {
181 goto Exit;
182 }
183
184 //
185 // Append the SSD node to the controller's device path
186 //
187 DevicePath = AppendDevicePathNode (ParentDevicePath, NewDevicePathNode);
188 if (DevicePath == NULL) {
189 Status = EFI_OUT_OF_RESOURCES;
190 goto Exit;
191 }
192
193 DeviceHandle = NULL;
194 RemainingDevicePath = DevicePath;
195 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &DeviceHandle);
196 if (!EFI_ERROR (Status) && (DeviceHandle != NULL) && IsDevicePathEnd(RemainingDevicePath)) {
197 Status = EFI_ALREADY_STARTED;
198 FreePool (DevicePath);
199 goto Exit;
200 }
201
202 Device->DevicePath = DevicePath;
203
204 //
205 // Make sure the handle is NULL so we create a new handle
206 //
207 Device->DeviceHandle = NULL;
208
209 Status = gBS->InstallMultipleProtocolInterfaces (
210 &Device->DeviceHandle,
211 &gEfiDevicePathProtocolGuid,
212 Device->DevicePath,
213 &gEfiBlockIoProtocolGuid,
214 &Device->BlockIo,
215 &gEfiDiskInfoProtocolGuid,
216 &Device->DiskInfo,
217 NULL
218 );
219
220 if(EFI_ERROR(Status)) {
221 goto Exit;
222 }
223
224 //
225 // Check if the NVMe controller supports the Security Send and Security Receive commands
226 //
227 if ((Private->ControllerData->Oacs & SECURITY_SEND_RECEIVE_SUPPORTED) != 0) {
228 Status = gBS->InstallProtocolInterface (
229 &Device->DeviceHandle,
230 &gEfiStorageSecurityCommandProtocolGuid,
231 EFI_NATIVE_INTERFACE,
232 &Device->StorageSecurity
233 );
234 if(EFI_ERROR(Status)) {
235 gBS->UninstallMultipleProtocolInterfaces (
236 &Device->DeviceHandle,
237 &gEfiDevicePathProtocolGuid,
238 Device->DevicePath,
239 &gEfiBlockIoProtocolGuid,
240 &Device->BlockIo,
241 &gEfiDiskInfoProtocolGuid,
242 &Device->DiskInfo,
243 NULL
244 );
245 goto Exit;
246 }
247 }
248
249 gBS->OpenProtocol (
250 Private->ControllerHandle,
251 &gEfiNvmExpressPassThruProtocolGuid,
252 (VOID **) &Private->Passthru,
253 Private->DriverBindingHandle,
254 Device->DeviceHandle,
255 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
256 );
257
258 //
259 // Dump NvmExpress Identify Namespace Data
260 //
261 DEBUG ((EFI_D_INFO, " == NVME IDENTIFY NAMESPACE [%d] DATA ==\n", NamespaceId));
262 DEBUG ((EFI_D_INFO, " NSZE : 0x%x\n", NamespaceData->Nsze));
263 DEBUG ((EFI_D_INFO, " NCAP : 0x%x\n", NamespaceData->Ncap));
264 DEBUG ((EFI_D_INFO, " NUSE : 0x%x\n", NamespaceData->Nuse));
265 DEBUG ((EFI_D_INFO, " LBAF0.LBADS : 0x%x\n", (NamespaceData->LbaFormat[0].Lbads)));
266
267 //
268 // Build controller name for Component Name (2) protocol.
269 //
270 CopyMem (Sn, Private->ControllerData->Sn, sizeof (Private->ControllerData->Sn));
271 Sn[20] = 0;
272 CopyMem (Mn, Private->ControllerData->Mn, sizeof (Private->ControllerData->Mn));
273 Mn[40] = 0;
274 UnicodeSPrintAsciiFormat (Device->ModelName, sizeof (Device->ModelName), "%a-%a-%x", Sn, Mn, NamespaceData->Eui64);
275
276 AddUnicodeString2 (
277 "eng",
278 gNvmExpressComponentName.SupportedLanguages,
279 &Device->ControllerNameTable,
280 Device->ModelName,
281 TRUE
282 );
283
284 AddUnicodeString2 (
285 "en",
286 gNvmExpressComponentName2.SupportedLanguages,
287 &Device->ControllerNameTable,
288 Device->ModelName,
289 FALSE
290 );
291 }
292
293 Exit:
294 if(NamespaceData != NULL) {
295 FreePool (NamespaceData);
296 }
297
298 if (NewDevicePathNode != NULL) {
299 FreePool (NewDevicePathNode);
300 }
301
302 if(EFI_ERROR(Status) && (Device != NULL) && (Device->DevicePath != NULL)) {
303 FreePool (Device->DevicePath);
304 }
305 if(EFI_ERROR(Status) && (Device != NULL)) {
306 FreePool (Device);
307 }
308 return Status;
309 }
310
311 /**
312 Discover all Nvm Express device namespaces, and create child handles for them with BlockIo
313 and DiskInfo protocol instances.
314
315 @param[in] Private The pointer to the NVME_CONTROLLER_PRIVATE_DATA data structure.
316
317 @retval EFI_SUCCESS All the namespaces in the device are successfully enumerated.
318 @return Others Some error occurs when enumerating the namespaces.
319
320 **/
321 EFI_STATUS
322 DiscoverAllNamespaces (
323 IN NVME_CONTROLLER_PRIVATE_DATA *Private
324 )
325 {
326 EFI_STATUS Status;
327 UINT32 NamespaceId;
328 EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL *Passthru;
329
330 NamespaceId = 0xFFFFFFFF;
331 Passthru = &Private->Passthru;
332
333 while (TRUE) {
334 Status = Passthru->GetNextNamespace (
335 Passthru,
336 (UINT32 *)&NamespaceId
337 );
338
339 if (EFI_ERROR (Status)) {
340 break;
341 }
342
343 Status = EnumerateNvmeDevNamespace (
344 Private,
345 NamespaceId
346 );
347
348 if (EFI_ERROR(Status)) {
349 continue;
350 }
351 }
352
353 return EFI_SUCCESS;
354 }
355
356 /**
357 Unregisters a Nvm Express device namespace.
358
359 This function removes the protocols installed on the controller handle and
360 frees the resources allocated for the namespace.
361
362 @param This The pointer to EFI_DRIVER_BINDING_PROTOCOL instance.
363 @param Controller The controller handle of the namespace.
364 @param Handle The child handle.
365
366 @retval EFI_SUCCESS The namespace is successfully unregistered.
367 @return Others Some error occurs when unregistering the namespace.
368
369 **/
370 EFI_STATUS
371 UnregisterNvmeNamespace (
372 IN EFI_DRIVER_BINDING_PROTOCOL *This,
373 IN EFI_HANDLE Controller,
374 IN EFI_HANDLE Handle
375 )
376 {
377 EFI_STATUS Status;
378 EFI_BLOCK_IO_PROTOCOL *BlockIo;
379 NVME_DEVICE_PRIVATE_DATA *Device;
380 NVME_CONTROLLER_PRIVATE_DATA *Private;
381 EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *StorageSecurity;
382
383 BlockIo = NULL;
384
385 Status = gBS->OpenProtocol (
386 Handle,
387 &gEfiBlockIoProtocolGuid,
388 (VOID **) &BlockIo,
389 This->DriverBindingHandle,
390 Controller,
391 EFI_OPEN_PROTOCOL_GET_PROTOCOL
392 );
393 if (EFI_ERROR (Status)) {
394 return Status;
395 }
396
397 Device = NVME_DEVICE_PRIVATE_DATA_FROM_BLOCK_IO (BlockIo);
398 Private = Device->Controller;
399
400 //
401 // Close the child handle
402 //
403 gBS->CloseProtocol (
404 Controller,
405 &gEfiNvmExpressPassThruProtocolGuid,
406 This->DriverBindingHandle,
407 Handle
408 );
409
410 //
411 // The Nvm Express driver installs the BlockIo and DiskInfo in the DriverBindingStart().
412 // Here should uninstall both of them.
413 //
414 Status = gBS->UninstallMultipleProtocolInterfaces (
415 Handle,
416 &gEfiDevicePathProtocolGuid,
417 Device->DevicePath,
418 &gEfiBlockIoProtocolGuid,
419 &Device->BlockIo,
420 &gEfiDiskInfoProtocolGuid,
421 &Device->DiskInfo,
422 NULL
423 );
424
425 if (EFI_ERROR (Status)) {
426 gBS->OpenProtocol (
427 Controller,
428 &gEfiNvmExpressPassThruProtocolGuid,
429 (VOID **) &Private->Passthru,
430 This->DriverBindingHandle,
431 Handle,
432 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
433 );
434 return Status;
435 }
436
437 //
438 // If Storage Security Command Protocol is installed, then uninstall this protocol.
439 //
440 Status = gBS->OpenProtocol (
441 Handle,
442 &gEfiStorageSecurityCommandProtocolGuid,
443 (VOID **) &StorageSecurity,
444 This->DriverBindingHandle,
445 Controller,
446 EFI_OPEN_PROTOCOL_GET_PROTOCOL
447 );
448
449 if (!EFI_ERROR (Status)) {
450 Status = gBS->UninstallProtocolInterface (
451 Handle,
452 &gEfiStorageSecurityCommandProtocolGuid,
453 &Device->StorageSecurity
454 );
455 if (EFI_ERROR (Status)) {
456 gBS->OpenProtocol (
457 Controller,
458 &gEfiNvmExpressPassThruProtocolGuid,
459 (VOID **) &Private->Passthru,
460 This->DriverBindingHandle,
461 Handle,
462 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
463 );
464 return Status;
465 }
466 }
467
468 if(Device->DevicePath != NULL) {
469 FreePool (Device->DevicePath);
470 }
471
472 if (Device->ControllerNameTable != NULL) {
473 FreeUnicodeStringTable (Device->ControllerNameTable);
474 }
475
476 FreePool (Device);
477
478 return EFI_SUCCESS;
479 }
480
481 /**
482 Tests to see if this driver supports a given controller. If a child device is provided,
483 it further tests to see if this driver supports creating a handle for the specified child device.
484
485 This function checks to see if the driver specified by This supports the device specified by
486 ControllerHandle. Drivers will typically use the device path attached to
487 ControllerHandle and/or the services from the bus I/O abstraction attached to
488 ControllerHandle to determine if the driver supports ControllerHandle. This function
489 may be called many times during platform initialization. In order to reduce boot times, the tests
490 performed by this function must be very small, and take as little time as possible to execute. This
491 function must not change the state of any hardware devices, and this function must be aware that the
492 device specified by ControllerHandle may already be managed by the same driver or a
493 different driver. This function must match its calls to AllocatePages() with FreePages(),
494 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
495 Since ControllerHandle may have been previously started by the same driver, if a protocol is
496 already in the opened state, then it must not be closed with CloseProtocol(). This is required
497 to guarantee the state of ControllerHandle is not modified by this function.
498
499 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
500 @param[in] ControllerHandle The handle of the controller to test. This handle
501 must support a protocol interface that supplies
502 an I/O abstraction to the driver.
503 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
504 parameter is ignored by device drivers, and is optional for bus
505 drivers. For bus drivers, if this parameter is not NULL, then
506 the bus driver must determine if the bus controller specified
507 by ControllerHandle and the child controller specified
508 by RemainingDevicePath are both supported by this
509 bus driver.
510
511 @retval EFI_SUCCESS The device specified by ControllerHandle and
512 RemainingDevicePath is supported by the driver specified by This.
513 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
514 RemainingDevicePath is already being managed by the driver
515 specified by This.
516 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
517 RemainingDevicePath is already being managed by a different
518 driver or an application that requires exclusive access.
519 Currently not implemented.
520 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
521 RemainingDevicePath is not supported by the driver specified by This.
522 **/
523 EFI_STATUS
524 EFIAPI
525 NvmExpressDriverBindingSupported (
526 IN EFI_DRIVER_BINDING_PROTOCOL *This,
527 IN EFI_HANDLE Controller,
528 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
529 )
530 {
531 EFI_STATUS Status;
532 EFI_DEV_PATH_PTR DevicePathNode;
533 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
534 EFI_PCI_IO_PROTOCOL *PciIo;
535 UINT8 ClassCode[3];
536
537 //
538 // Check whether device path is valid
539 //
540 if (RemainingDevicePath != NULL) {
541 //
542 // Check if RemainingDevicePath is the End of Device Path Node,
543 // if yes, go on checking other conditions
544 //
545 if (!IsDevicePathEnd (RemainingDevicePath)) {
546 //
547 // If RemainingDevicePath isn't the End of Device Path Node,
548 // check its validation
549 //
550 DevicePathNode.DevPath = RemainingDevicePath;
551
552 if ((DevicePathNode.DevPath->Type != MESSAGING_DEVICE_PATH) ||
553 (DevicePathNode.DevPath->SubType != MSG_NVME_NAMESPACE_DP) ||
554 (DevicePathNodeLength(DevicePathNode.DevPath) != sizeof(NVME_NAMESPACE_DEVICE_PATH))) {
555 return EFI_UNSUPPORTED;
556 }
557 }
558 }
559
560 //
561 // Open the EFI Device Path protocol needed to perform the supported test
562 //
563 Status = gBS->OpenProtocol (
564 Controller,
565 &gEfiDevicePathProtocolGuid,
566 (VOID **) &ParentDevicePath,
567 This->DriverBindingHandle,
568 Controller,
569 EFI_OPEN_PROTOCOL_BY_DRIVER
570 );
571 if (Status == EFI_ALREADY_STARTED) {
572 return EFI_SUCCESS;
573 }
574
575 if (EFI_ERROR (Status)) {
576 return Status;
577 }
578
579 //
580 // Close protocol, don't use device path protocol in the Support() function
581 //
582 gBS->CloseProtocol (
583 Controller,
584 &gEfiDevicePathProtocolGuid,
585 This->DriverBindingHandle,
586 Controller
587 );
588
589 //
590 // Attempt to Open PCI I/O Protocol
591 //
592 Status = gBS->OpenProtocol (
593 Controller,
594 &gEfiPciIoProtocolGuid,
595 (VOID **) &PciIo,
596 This->DriverBindingHandle,
597 Controller,
598 EFI_OPEN_PROTOCOL_BY_DRIVER
599 );
600 if (Status == EFI_ALREADY_STARTED) {
601 return EFI_SUCCESS;
602 }
603
604 if (EFI_ERROR (Status)) {
605 return Status;
606 }
607
608 //
609 // Now further check the PCI header: Base class (offset 0x0B) and Sub Class (offset 0x0A).
610 // This controller should be a Nvm Express controller.
611 //
612 Status = PciIo->Pci.Read (
613 PciIo,
614 EfiPciIoWidthUint8,
615 PCI_CLASSCODE_OFFSET,
616 sizeof (ClassCode),
617 ClassCode
618 );
619 if (EFI_ERROR (Status)) {
620 goto Done;
621 }
622
623 //
624 // Examine Nvm Express controller PCI Configuration table fields
625 //
626 if ((ClassCode[0] != PCI_IF_NVMHCI) || (ClassCode[1] != PCI_CLASS_MASS_STORAGE_NVM) || (ClassCode[2] != PCI_CLASS_MASS_STORAGE)) {
627 Status = EFI_UNSUPPORTED;
628 }
629
630 Done:
631 gBS->CloseProtocol (
632 Controller,
633 &gEfiPciIoProtocolGuid,
634 This->DriverBindingHandle,
635 Controller
636 );
637
638 return Status;
639 }
640
641
642 /**
643 Starts a device controller or a bus controller.
644
645 The Start() function is designed to be invoked from the EFI boot service ConnectController().
646 As a result, much of the error checking on the parameters to Start() has been moved into this
647 common boot service. It is legal to call Start() from other locations,
648 but the following calling restrictions must be followed or the system behavior will not be deterministic.
649 1. ControllerHandle must be a valid EFI_HANDLE.
650 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
651 EFI_DEVICE_PATH_PROTOCOL.
652 3. Prior to calling Start(), the Supported() function for the driver specified by This must
653 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
654
655 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
656 @param[in] ControllerHandle The handle of the controller to start. This handle
657 must support a protocol interface that supplies
658 an I/O abstraction to the driver.
659 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
660 parameter is ignored by device drivers, and is optional for bus
661 drivers. For a bus driver, if this parameter is NULL, then handles
662 for all the children of Controller are created by this driver.
663 If this parameter is not NULL and the first Device Path Node is
664 not the End of Device Path Node, then only the handle for the
665 child device specified by the first Device Path Node of
666 RemainingDevicePath is created by this driver.
667 If the first Device Path Node of RemainingDevicePath is
668 the End of Device Path Node, no child handle is created by this
669 driver.
670
671 @retval EFI_SUCCESS The device was started.
672 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
673 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
674 @retval Others The driver failded to start the device.
675
676 **/
677 EFI_STATUS
678 EFIAPI
679 NvmExpressDriverBindingStart (
680 IN EFI_DRIVER_BINDING_PROTOCOL *This,
681 IN EFI_HANDLE Controller,
682 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
683 )
684 {
685 EFI_STATUS Status;
686 EFI_PCI_IO_PROTOCOL *PciIo;
687 NVME_CONTROLLER_PRIVATE_DATA *Private;
688 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
689 UINT32 NamespaceId;
690 EFI_PHYSICAL_ADDRESS MappedAddr;
691 UINTN Bytes;
692 EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL *Passthru;
693
694 DEBUG ((EFI_D_INFO, "NvmExpressDriverBindingStart: start\n"));
695
696 Private = NULL;
697 Passthru = NULL;
698 ParentDevicePath = NULL;
699
700 Status = gBS->OpenProtocol (
701 Controller,
702 &gEfiDevicePathProtocolGuid,
703 (VOID **) &ParentDevicePath,
704 This->DriverBindingHandle,
705 Controller,
706 EFI_OPEN_PROTOCOL_BY_DRIVER
707 );
708 if ((EFI_ERROR (Status)) && (Status != EFI_ALREADY_STARTED)) {
709 return Status;
710 }
711
712 Status = gBS->OpenProtocol (
713 Controller,
714 &gEfiPciIoProtocolGuid,
715 (VOID **) &PciIo,
716 This->DriverBindingHandle,
717 Controller,
718 EFI_OPEN_PROTOCOL_BY_DRIVER
719 );
720
721 if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {
722 return Status;
723 }
724
725 //
726 // Check EFI_ALREADY_STARTED to reuse the original NVME_CONTROLLER_PRIVATE_DATA.
727 //
728 if (Status != EFI_ALREADY_STARTED) {
729 Private = AllocateZeroPool (sizeof (NVME_CONTROLLER_PRIVATE_DATA));
730
731 if (Private == NULL) {
732 DEBUG ((EFI_D_ERROR, "NvmExpressDriverBindingStart: allocating pool for Nvme Private Data failed!\n"));
733 Status = EFI_OUT_OF_RESOURCES;
734 goto Exit;
735 }
736
737 //
738 // 4 x 4kB aligned buffers will be carved out of this buffer.
739 // 1st 4kB boundary is the start of the admin submission queue.
740 // 2nd 4kB boundary is the start of the admin completion queue.
741 // 3rd 4kB boundary is the start of I/O submission queue #1.
742 // 4th 4kB boundary is the start of I/O completion queue #1.
743 //
744 // Allocate 4 pages of memory, then map it for bus master read and write.
745 //
746 Status = PciIo->AllocateBuffer (
747 PciIo,
748 AllocateAnyPages,
749 EfiBootServicesData,
750 4,
751 (VOID**)&Private->Buffer,
752 0
753 );
754 if (EFI_ERROR (Status)) {
755 goto Exit;
756 }
757
758 Bytes = EFI_PAGES_TO_SIZE (4);
759 Status = PciIo->Map (
760 PciIo,
761 EfiPciIoOperationBusMasterCommonBuffer,
762 Private->Buffer,
763 &Bytes,
764 &MappedAddr,
765 &Private->Mapping
766 );
767
768 if (EFI_ERROR (Status) || (Bytes != EFI_PAGES_TO_SIZE (4))) {
769 goto Exit;
770 }
771
772 Private->BufferPciAddr = (UINT8 *)(UINTN)MappedAddr;
773
774 Private->Signature = NVME_CONTROLLER_PRIVATE_DATA_SIGNATURE;
775 Private->ControllerHandle = Controller;
776 Private->ImageHandle = This->DriverBindingHandle;
777 Private->DriverBindingHandle = This->DriverBindingHandle;
778 Private->PciIo = PciIo;
779 Private->ParentDevicePath = ParentDevicePath;
780 Private->Passthru.Mode = &Private->PassThruMode;
781 Private->Passthru.PassThru = NvmExpressPassThru;
782 Private->Passthru.GetNextNamespace = NvmExpressGetNextNamespace;
783 Private->Passthru.BuildDevicePath = NvmExpressBuildDevicePath;
784 Private->Passthru.GetNamespace = NvmExpressGetNamespace;
785 CopyMem (&Private->PassThruMode, &gEfiNvmExpressPassThruMode, sizeof (EFI_NVM_EXPRESS_PASS_THRU_MODE));
786
787 Status = NvmeControllerInit (Private);
788 if (EFI_ERROR(Status)) {
789 goto Exit;
790 }
791
792 Status = gBS->InstallMultipleProtocolInterfaces (
793 &Controller,
794 &gEfiNvmExpressPassThruProtocolGuid,
795 &Private->Passthru,
796 NULL
797 );
798 if (EFI_ERROR (Status)) {
799 goto Exit;
800 }
801 } else {
802 Status = gBS->OpenProtocol (
803 Controller,
804 &gEfiNvmExpressPassThruProtocolGuid,
805 (VOID **) &Passthru,
806 This->DriverBindingHandle,
807 Controller,
808 EFI_OPEN_PROTOCOL_GET_PROTOCOL
809 );
810 if (EFI_ERROR (Status)) {
811 goto Exit;
812 }
813
814 Private = NVME_CONTROLLER_PRIVATE_DATA_FROM_PASS_THRU (Passthru);
815 }
816
817 if (RemainingDevicePath == NULL) {
818 //
819 // Enumerate all NVME namespaces in the controller
820 //
821 Status = DiscoverAllNamespaces (
822 Private
823 );
824
825 } else if (!IsDevicePathEnd (RemainingDevicePath)) {
826 //
827 // Enumerate the specified NVME namespace
828 //
829 Status = Private->Passthru.GetNamespace (
830 &Private->Passthru,
831 RemainingDevicePath,
832 &NamespaceId
833 );
834
835 if (!EFI_ERROR (Status)) {
836 Status = EnumerateNvmeDevNamespace (
837 Private,
838 NamespaceId
839 );
840 }
841 }
842
843 DEBUG ((EFI_D_INFO, "NvmExpressDriverBindingStart: end successfully\n"));
844 return EFI_SUCCESS;
845
846 Exit:
847 if ((Private != NULL) && (Private->Mapping != NULL)) {
848 PciIo->Unmap (PciIo, Private->Mapping);
849 }
850
851 if ((Private != NULL) && (Private->Buffer != NULL)) {
852 PciIo->FreeBuffer (PciIo, 4, Private->Buffer);
853 }
854
855 if ((Private != NULL) && (Private->ControllerData != NULL)) {
856 FreePool (Private->ControllerData);
857 }
858
859 if (Private != NULL) {
860 FreePool (Private);
861 }
862
863 gBS->CloseProtocol (
864 Controller,
865 &gEfiPciIoProtocolGuid,
866 This->DriverBindingHandle,
867 Controller
868 );
869
870 gBS->CloseProtocol (
871 Controller,
872 &gEfiDevicePathProtocolGuid,
873 This->DriverBindingHandle,
874 Controller
875 );
876
877 DEBUG ((EFI_D_INFO, "NvmExpressDriverBindingStart: end with %r\n", Status));
878
879 return Status;
880 }
881
882
883 /**
884 Stops a device controller or a bus controller.
885
886 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
887 As a result, much of the error checking on the parameters to Stop() has been moved
888 into this common boot service. It is legal to call Stop() from other locations,
889 but the following calling restrictions must be followed or the system behavior will not be deterministic.
890 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
891 same driver's Start() function.
892 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
893 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
894 Start() function, and the Start() function must have called OpenProtocol() on
895 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
896
897 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
898 @param[in] ControllerHandle A handle to the device being stopped. The handle must
899 support a bus specific I/O protocol for the driver
900 to use to stop the device.
901 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
902 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
903 if NumberOfChildren is 0.
904
905 @retval EFI_SUCCESS The device was stopped.
906 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
907
908 **/
909 EFI_STATUS
910 EFIAPI
911 NvmExpressDriverBindingStop (
912 IN EFI_DRIVER_BINDING_PROTOCOL *This,
913 IN EFI_HANDLE Controller,
914 IN UINTN NumberOfChildren,
915 IN EFI_HANDLE *ChildHandleBuffer
916 )
917 {
918 EFI_STATUS Status;
919 BOOLEAN AllChildrenStopped;
920 UINTN Index;
921 NVME_CONTROLLER_PRIVATE_DATA *Private;
922 EFI_NVM_EXPRESS_PASS_THRU_PROTOCOL *PassThru;
923
924 if (NumberOfChildren == 0) {
925 Status = gBS->OpenProtocol (
926 Controller,
927 &gEfiNvmExpressPassThruProtocolGuid,
928 (VOID **) &PassThru,
929 This->DriverBindingHandle,
930 Controller,
931 EFI_OPEN_PROTOCOL_GET_PROTOCOL
932 );
933
934 if (!EFI_ERROR (Status)) {
935 Private = NVME_CONTROLLER_PRIVATE_DATA_FROM_PASS_THRU (PassThru);
936 gBS->UninstallMultipleProtocolInterfaces (
937 Controller,
938 &gEfiNvmExpressPassThruProtocolGuid,
939 PassThru,
940 NULL
941 );
942
943 if (Private->Mapping != NULL) {
944 Private->PciIo->Unmap (Private->PciIo, Private->Mapping);
945 }
946
947 if (Private->Buffer != NULL) {
948 Private->PciIo->FreeBuffer (Private->PciIo, 4, Private->Buffer);
949 }
950
951 FreePool (Private->ControllerData);
952 FreePool (Private);
953 }
954
955 gBS->CloseProtocol (
956 Controller,
957 &gEfiPciIoProtocolGuid,
958 This->DriverBindingHandle,
959 Controller
960 );
961 gBS->CloseProtocol (
962 Controller,
963 &gEfiDevicePathProtocolGuid,
964 This->DriverBindingHandle,
965 Controller
966 );
967 return EFI_SUCCESS;
968 }
969
970 AllChildrenStopped = TRUE;
971
972 for (Index = 0; Index < NumberOfChildren; Index++) {
973 Status = UnregisterNvmeNamespace (This, Controller, ChildHandleBuffer[Index]);
974 if (EFI_ERROR (Status)) {
975 AllChildrenStopped = FALSE;
976 }
977 }
978
979 if (!AllChildrenStopped) {
980 return EFI_DEVICE_ERROR;
981 }
982
983 return EFI_SUCCESS;
984 }
985
986 /**
987 This is the unload handle for the NVM Express driver.
988
989 Disconnect the driver specified by ImageHandle from the NVMe device in the handle database.
990 Uninstall all the protocols installed in the driver.
991
992 @param[in] ImageHandle The drivers' driver image.
993
994 @retval EFI_SUCCESS The image is unloaded.
995 @retval Others Failed to unload the image.
996
997 **/
998 EFI_STATUS
999 EFIAPI
1000 NvmExpressUnload (
1001 IN EFI_HANDLE ImageHandle
1002 )
1003 {
1004 EFI_STATUS Status;
1005 EFI_HANDLE *DeviceHandleBuffer;
1006 UINTN DeviceHandleCount;
1007 UINTN Index;
1008 EFI_COMPONENT_NAME_PROTOCOL *ComponentName;
1009 EFI_COMPONENT_NAME2_PROTOCOL *ComponentName2;
1010
1011 //
1012 // Get the list of the device handles managed by this driver.
1013 // If there is an error getting the list, then means the driver
1014 // doesn't manage any device. At this way, we would only close
1015 // those protocols installed at image handle.
1016 //
1017 DeviceHandleBuffer = NULL;
1018 Status = gBS->LocateHandleBuffer (
1019 ByProtocol,
1020 &gEfiNvmExpressPassThruProtocolGuid,
1021 NULL,
1022 &DeviceHandleCount,
1023 &DeviceHandleBuffer
1024 );
1025
1026 if (!EFI_ERROR (Status)) {
1027 //
1028 // Disconnect the driver specified by ImageHandle from all
1029 // the devices in the handle database.
1030 //
1031 for (Index = 0; Index < DeviceHandleCount; Index++) {
1032 Status = gBS->DisconnectController (
1033 DeviceHandleBuffer[Index],
1034 ImageHandle,
1035 NULL
1036 );
1037 if (EFI_ERROR (Status)) {
1038 goto EXIT;
1039 }
1040 }
1041 }
1042
1043 //
1044 // Uninstall all the protocols installed in the driver entry point
1045 //
1046 Status = gBS->UninstallMultipleProtocolInterfaces (
1047 ImageHandle,
1048 &gEfiDriverBindingProtocolGuid,
1049 &gNvmExpressDriverBinding,
1050 &gEfiDriverSupportedEfiVersionProtocolGuid,
1051 &gNvmExpressDriverSupportedEfiVersion,
1052 NULL
1053 );
1054
1055 if (EFI_ERROR (Status)) {
1056 goto EXIT;
1057 }
1058
1059 //
1060 // Note we have to one by one uninstall the following protocols.
1061 // It's because some of them are optionally installed based on
1062 // the following PCD settings.
1063 // gEfiMdePkgTokenSpaceGuid.PcdDriverDiagnosticsDisable
1064 // gEfiMdePkgTokenSpaceGuid.PcdComponentNameDisable
1065 // gEfiMdePkgTokenSpaceGuid.PcdDriverDiagnostics2Disable
1066 // gEfiMdePkgTokenSpaceGuid.PcdComponentName2Disable
1067 //
1068 Status = gBS->HandleProtocol (
1069 ImageHandle,
1070 &gEfiComponentNameProtocolGuid,
1071 (VOID **) &ComponentName
1072 );
1073 if (!EFI_ERROR (Status)) {
1074 gBS->UninstallProtocolInterface (
1075 ImageHandle,
1076 &gEfiComponentNameProtocolGuid,
1077 ComponentName
1078 );
1079 }
1080
1081 Status = gBS->HandleProtocol (
1082 ImageHandle,
1083 &gEfiComponentName2ProtocolGuid,
1084 (VOID **) &ComponentName2
1085 );
1086 if (!EFI_ERROR (Status)) {
1087 gBS->UninstallProtocolInterface (
1088 ImageHandle,
1089 &gEfiComponentName2ProtocolGuid,
1090 ComponentName2
1091 );
1092 }
1093
1094 Status = EFI_SUCCESS;
1095
1096 EXIT:
1097 //
1098 // Free the buffer containing the list of handles from the handle database
1099 //
1100 if (DeviceHandleBuffer != NULL) {
1101 gBS->FreePool (DeviceHandleBuffer);
1102 }
1103 return Status;
1104 }
1105
1106 /**
1107 The entry point for Nvm Express driver, used to install Nvm Express driver on the ImageHandle.
1108
1109 @param ImageHandle The firmware allocated handle for this driver image.
1110 @param SystemTable Pointer to the EFI system table.
1111
1112 @retval EFI_SUCCESS Driver loaded.
1113 @retval other Driver not loaded.
1114
1115 **/
1116 EFI_STATUS
1117 EFIAPI
1118 NvmExpressDriverEntry (
1119 IN EFI_HANDLE ImageHandle,
1120 IN EFI_SYSTEM_TABLE *SystemTable
1121 )
1122 {
1123 EFI_STATUS Status;
1124
1125 Status = EfiLibInstallDriverBindingComponentName2 (
1126 ImageHandle,
1127 SystemTable,
1128 &gNvmExpressDriverBinding,
1129 ImageHandle,
1130 &gNvmExpressComponentName,
1131 &gNvmExpressComponentName2
1132 );
1133 ASSERT_EFI_ERROR (Status);
1134
1135 //
1136 // Install EFI Driver Supported EFI Version Protocol required for
1137 // EFI drivers that are on PCI and other plug in cards.
1138 //
1139 gNvmExpressDriverSupportedEfiVersion.FirmwareVersion = 0x00020028;
1140 Status = gBS->InstallMultipleProtocolInterfaces (
1141 &ImageHandle,
1142 &gEfiDriverSupportedEfiVersionProtocolGuid,
1143 &gNvmExpressDriverSupportedEfiVersion,
1144 NULL
1145 );
1146 ASSERT_EFI_ERROR (Status);
1147 return Status;
1148 }