]> git.proxmox.com Git - mirror_edk2.git/blobdiff - IntelFrameworkModulePkg/Bus/Pci/PciBusDxe/PciDeviceSupport.c
Retired PciIncompatibleDeviceSupportLib from IntelFrameworkModulePkg.
[mirror_edk2.git] / IntelFrameworkModulePkg / Bus / Pci / PciBusDxe / PciDeviceSupport.c
index d19ee04bf4c5567e7847161277514f6807164233..40bef06868433eeed146c98930b259d711a51f2a 100644 (file)
-/**@file\r
+/** @file\r
+  Supporting functions implementaion for PCI devices management.\r
 \r
-Copyright (c) 2006, Intel Corporation                                                         \r
-All rights reserved. This program and the accompanying materials                          \r
-are licensed and made available under the terms and conditions of the BSD License         \r
-which accompanies this distribution.  The full text of the license may be found at        \r
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+Copyright (c) 2006 - 2009, Intel Corporation\r
+All rights reserved. This program and the accompanying materials\r
+are licensed and made available under the terms and conditions of the BSD License\r
+which accompanies this distribution.  The full text of the license may be found at\r
+http://opensource.org/licenses/bsd-license.php\r
 \r
-**/\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
+**/\r
 \r
-#include "pcibus.h"\r
-#include "PciDeviceSupport.h"\r
+#include "PciBus.h"\r
 \r
 //\r
 // This device structure is serviced as a header.\r
-// Its Next field points to the first root bridge device node\r
+// Its next field points to the first root bridge device node.\r
 //\r
-LIST_ENTRY  gPciDevicePool;\r
+LIST_ENTRY  mPciDevicePool;\r
 \r
-EFI_STATUS\r
+/**\r
+  Initialize the PCI devices pool.\r
+\r
+**/\r
+VOID\r
 InitializePciDevicePool (\r
   VOID\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Initialize the gPciDevicePool\r
-\r
-Arguments:\r
-\r
-Returns:\r
-\r
-  None\r
-\r
---*/\r
-// TODO:    EFI_SUCCESS - add return value to function comment\r
 {\r
-  InitializeListHead (&gPciDevicePool);\r
-\r
-  return EFI_SUCCESS;\r
+  InitializeListHead (&mPciDevicePool);\r
 }\r
 \r
-EFI_STATUS\r
-InsertRootBridge (\r
-  PCI_IO_DEVICE *RootBridge\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Insert a root bridge into PCI device pool\r
-\r
-Arguments:\r
-\r
-  RootBridge    - A pointer to the PCI_IO_DEVICE.\r
+/**\r
+  Insert a root bridge into PCI device pool.\r
 \r
-Returns:\r
+  @param RootBridge     A pointer to the PCI_IO_DEVICE.\r
 \r
-  None\r
-\r
---*/\r
-// TODO:    EFI_SUCCESS - add return value to function comment\r
+**/\r
+VOID\r
+InsertRootBridge (\r
+  IN PCI_IO_DEVICE      *RootBridge\r
+  )\r
 {\r
-\r
-  InsertTailList (&gPciDevicePool, &(RootBridge->Link));\r
-\r
-  return EFI_SUCCESS;\r
+  InsertTailList (&mPciDevicePool, &(RootBridge->Link));\r
 }\r
 \r
-EFI_STATUS\r
-InsertPciDevice (\r
-  PCI_IO_DEVICE *Bridge,\r
-  PCI_IO_DEVICE *PciDeviceNode\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
+/**\r
   This function is used to insert a PCI device node under\r
-  a bridge\r
-\r
-Arguments:\r
-  Bridge        - A pointer to the PCI_IO_DEVICE.\r
-  PciDeviceNode - A pointer to the PCI_IO_DEVICE.\r
+  a bridge.\r
 \r
-Returns:\r
+  @param Bridge         The PCI bridge.\r
+  @param PciDeviceNode  The PCI device needs inserting.\r
 \r
-  None\r
-\r
---*/\r
-// TODO:    EFI_SUCCESS - add return value to function comment\r
+**/\r
+VOID\r
+InsertPciDevice (\r
+  IN PCI_IO_DEVICE      *Bridge,\r
+  IN PCI_IO_DEVICE      *PciDeviceNode\r
+  )\r
 {\r
-\r
   InsertTailList (&Bridge->ChildList, &(PciDeviceNode->Link));\r
   PciDeviceNode->Parent = Bridge;\r
-\r
-  return EFI_SUCCESS;\r
 }\r
 \r
-EFI_STATUS\r
-DestroyRootBridge (\r
-  IN PCI_IO_DEVICE *RootBridge\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
+/**\r
+  Destroy root bridge and remove it from deivce tree.\r
 \r
-Arguments:\r
+  @param RootBridge     The bridge want to be removed.\r
 \r
-  RootBridge   - A pointer to the PCI_IO_DEVICE.\r
-\r
-Returns:\r
-\r
-  None\r
-\r
---*/\r
-// TODO:    EFI_SUCCESS - add return value to function comment\r
+**/\r
+VOID\r
+DestroyRootBridge (\r
+  IN PCI_IO_DEVICE      *RootBridge\r
+  )\r
 {\r
   DestroyPciDeviceTree (RootBridge);\r
 \r
   FreePciDevice (RootBridge);\r
-\r
-  return EFI_SUCCESS;\r
 }\r
 \r
-EFI_STATUS\r
-FreePciDevice (\r
-  IN PCI_IO_DEVICE *PciIoDevice\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
+/**\r
   Destroy a pci device node.\r
-  Also all direct or indirect allocated resource for this node will be freed.\r
-\r
-Arguments:\r
-\r
-  PciIoDevice   - A pointer to the PCI_IO_DEVICE.\r
 \r
-Returns:\r
+  All direct or indirect allocated resource for this node will be freed.\r
 \r
-  None\r
+  @param PciIoDevice  A pointer to the PCI_IO_DEVICE to be destoried.\r
 \r
---*/\r
-// TODO:    EFI_SUCCESS - add return value to function comment\r
+**/\r
+VOID\r
+FreePciDevice (\r
+  IN PCI_IO_DEVICE    *PciIoDevice\r
+  )\r
 {\r
-\r
+  ASSERT (PciIoDevice != NULL);\r
   //\r
   // Assume all children have been removed underneath this device\r
   //\r
   if (PciIoDevice->ResourcePaddingDescriptors != NULL) {\r
-    gBS->FreePool (PciIoDevice->ResourcePaddingDescriptors);\r
+    FreePool (PciIoDevice->ResourcePaddingDescriptors);\r
   }\r
 \r
   if (PciIoDevice->DevicePath != NULL) {\r
-    gBS->FreePool (PciIoDevice->DevicePath);\r
+    FreePool (PciIoDevice->DevicePath);\r
   }\r
 \r
-  gBS->FreePool (PciIoDevice);\r
-\r
-  return EFI_SUCCESS;\r
+  FreePool (PciIoDevice);\r
 }\r
 \r
-EFI_STATUS\r
-DestroyPciDeviceTree (\r
-  IN PCI_IO_DEVICE *Bridge\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
+/**\r
   Destroy all the pci device node under the bridge.\r
   Bridge itself is not included.\r
 \r
-Arguments:\r
-\r
-  Bridge   - A pointer to the PCI_IO_DEVICE.\r
-\r
-Returns:\r
-\r
-  None\r
+  @param Bridge      A pointer to the PCI_IO_DEVICE.\r
 \r
---*/\r
-// TODO:    EFI_SUCCESS - add return value to function comment\r
+**/\r
+VOID\r
+DestroyPciDeviceTree (\r
+  IN PCI_IO_DEVICE      *Bridge\r
+  )\r
 {\r
   LIST_ENTRY      *CurrentLink;\r
   PCI_IO_DEVICE   *Temp;\r
@@ -210,41 +140,33 @@ Returns:
 \r
     FreePciDevice (Temp);\r
   }\r
-\r
-  return EFI_SUCCESS;\r
 }\r
 \r
-EFI_STATUS\r
-DestroyRootBridgeByHandle (\r
-  EFI_HANDLE Controller\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
+/**\r
   Destroy all device nodes under the root bridge\r
   specified by Controller.\r
-  The root bridge itself is also included.\r
-\r
-Arguments:\r
 \r
-  Controller   - An efi handle.\r
+  The root bridge itself is also included.\r
 \r
-Returns:\r
+  @param  Controller    Root bridge handle.\r
 \r
-  None\r
+  @retval EFI_SUCCESS   Destory all devcie nodes successfully.\r
+  @retval EFI_NOT_FOUND Cannot find any PCI device under specified\r
+                        root bridge.\r
 \r
---*/\r
-// TODO:    EFI_SUCCESS - add return value to function comment\r
-// TODO:    EFI_NOT_FOUND - add return value to function comment\r
+**/\r
+EFI_STATUS\r
+DestroyRootBridgeByHandle (\r
+  IN EFI_HANDLE        Controller\r
+  )\r
 {\r
 \r
   LIST_ENTRY      *CurrentLink;\r
   PCI_IO_DEVICE   *Temp;\r
 \r
-  CurrentLink = gPciDevicePool.ForwardLink;\r
+  CurrentLink = mPciDevicePool.ForwardLink;\r
 \r
-  while (CurrentLink && CurrentLink != &gPciDevicePool) {\r
+  while (CurrentLink != NULL && CurrentLink != &mPciDevicePool) {\r
     Temp = PCI_IO_DEVICE_FROM_LINK (CurrentLink);\r
 \r
     if (Temp->Handle == Controller) {\r
@@ -264,32 +186,27 @@ Returns:
   return EFI_NOT_FOUND;\r
 }\r
 \r
-EFI_STATUS\r
-RegisterPciDevice (\r
-  IN  EFI_HANDLE                     Controller,\r
-  IN  PCI_IO_DEVICE                  *PciIoDevice,\r
-  OUT EFI_HANDLE                     *Handle OPTIONAL\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This function registers the PCI IO device. It creates a handle for this PCI IO device\r
-  (if the handle does not exist), attaches appropriate protocols onto the handle, does\r
-  necessary initialization, and sets up parent/child relationship with its bus controller.\r
+/**\r
+  This function registers the PCI IO device.\r
 \r
-Arguments:\r
+  It creates a handle for this PCI IO device (if the handle does not exist), attaches\r
+  appropriate protocols onto the handle, does necessary initialization, and sets up\r
+  parent/child relationship with its bus controller.\r
 \r
-  Controller    - An EFI handle for the PCI bus controller.\r
-  PciIoDevice   - A PCI_IO_DEVICE pointer to the PCI IO device to be registered.\r
-  Handle        - A pointer to hold the EFI handle for the PCI IO device.\r
+  @param Controller     An EFI handle for the PCI bus controller.\r
+  @param PciIoDevice    A PCI_IO_DEVICE pointer to the PCI IO device to be registered.\r
+  @param Handle         A pointer to hold the returned EFI handle for the PCI IO device.\r
 \r
-Returns:\r
+  @retval EFI_SUCCESS   The PCI device is successfully registered.\r
+  @retval other         An error occurred when registering the PCI device.\r
 \r
-  EFI_SUCCESS   - The PCI device is successfully registered.\r
-  Others        - An error occurred when registering the PCI device.\r
-\r
---*/\r
+**/\r
+EFI_STATUS\r
+RegisterPciDevice (\r
+  IN  EFI_HANDLE          Controller,\r
+  IN  PCI_IO_DEVICE       *PciIoDevice,\r
+  OUT EFI_HANDLE          *Handle      OPTIONAL\r
+  )\r
 {\r
   EFI_STATUS          Status;\r
   VOID                *PlatformOpRomBuffer;\r
@@ -297,6 +214,7 @@ Returns:
   UINT8               PciExpressCapRegOffset;\r
   EFI_PCI_IO_PROTOCOL *PciIo;\r
   UINT8               Data8;\r
+  BOOLEAN             HasEfiImage;\r
 \r
   //\r
   // Install the pciio protocol, device path protocol\r
@@ -326,19 +244,18 @@ Returns:
   if (!EFI_ERROR (Status)) {\r
     PciIoDevice->IsPciExp = TRUE;\r
   }\r
-  \r
+\r
   //\r
   // Force Interrupt line to "Unknown" or "No Connection"\r
   //\r
   PciIo = &(PciIoDevice->PciIo);\r
   Data8 = PCI_INT_LINE_UNKNOWN;\r
-  PciIoWrite (PciIo, EfiPciIoWidthUint8, 0x3C, 1, &Data8);\r
+  PciIo->Pci.Write (PciIo, EfiPciIoWidthUint8, 0x3C, 1, &Data8);\r
 \r
   //\r
   // Process OpRom\r
   //\r
   if (!PciIoDevice->AllOpRomProcessed) {\r
-    PciIoDevice->AllOpRomProcessed = TRUE;\r
 \r
     //\r
     // Get the OpRom provided by platform\r
@@ -356,7 +273,7 @@ Returns:
         PciIoDevice->PciIo.RomImage = PlatformOpRomBuffer;\r
         //\r
         // For OpROM read from gPciPlatformProtocol:\r
-        //   Add the Rom Image to internal database for later PCI light enumeration\r
+        // Add the Rom Image to internal database for later PCI light enumeration\r
         //\r
         PciRomAddImageMapping (\r
           NULL,\r
@@ -367,23 +284,53 @@ Returns:
           (UINT64) (UINTN) PciIoDevice->PciIo.RomImage,\r
           PciIoDevice->PciIo.RomSize\r
           );\r
-        \r
       }\r
     }\r
+  }\r
+\r
+  //\r
+  // Determine if there are EFI images in the option rom\r
+  //\r
+  HasEfiImage = ContainEfiImage (PciIoDevice->PciIo.RomImage, PciIoDevice->PciIo.RomSize);\r
+\r
+  if (HasEfiImage) {\r
+    Status = gBS->InstallMultipleProtocolInterfaces (\r
+                    &PciIoDevice->Handle,\r
+                    &gEfiLoadFile2ProtocolGuid,\r
+                    &PciIoDevice->LoadFile2,\r
+                    NULL\r
+                    );\r
+    if (EFI_ERROR (Status)) {\r
+      gBS->UninstallMultipleProtocolInterfaces (\r
+             &PciIoDevice->Handle,\r
+             &gEfiDevicePathProtocolGuid,\r
+             PciIoDevice->DevicePath,\r
+             &gEfiPciIoProtocolGuid,\r
+             &PciIoDevice->PciIo,\r
+             NULL\r
+             );\r
+      return Status;\r
+    }\r
+  }\r
+\r
+\r
+  if (!PciIoDevice->AllOpRomProcessed) {\r
+\r
+    PciIoDevice->AllOpRomProcessed = TRUE;\r
 \r
     //\r
     // Dispatch the EFI OpRom for the PCI device.\r
     // The OpRom is got from platform in the above code\r
-    //   or loaded from device in previous bus enumeration\r
+    // or loaded from device in the previous round of bus enumeration\r
     //\r
-    if (PciIoDevice->RomSize > 0) {\r
+    if (HasEfiImage) {\r
       ProcessOpRomImage (PciIoDevice);\r
     }\r
   }\r
 \r
   if (PciIoDevice->BusOverride) {\r
     //\r
-    // Install BusSpecificDriverOverride Protocol\r
+    // Install Bus Specific Driver Override Protocol\r
     //\r
     Status = gBS->InstallMultipleProtocolInterfaces (\r
                     &PciIoDevice->Handle,\r
@@ -400,6 +347,14 @@ Returns:
              &PciIoDevice->PciIo,\r
              NULL\r
              );\r
+      if (HasEfiImage) {\r
+        gBS->UninstallMultipleProtocolInterfaces (\r
+               &PciIoDevice->Handle,\r
+               &gEfiLoadFile2ProtocolGuid,\r
+               &PciIoDevice->LoadFile2,\r
+               NULL\r
+               );\r
+      }\r
 \r
       return Status;\r
     }\r
@@ -417,12 +372,6 @@ Returns:
     return Status;\r
   }\r
 \r
-  //\r
-  // Install Pccard Hotplug GUID for Pccard device so that\r
-  // to notify CardBus driver to stop the device when de-register happens\r
-  //\r
-  InstallPciHotplugGuid (PciIoDevice);\r
-\r
   if (Handle != NULL) {\r
     *Handle = PciIoDevice->Handle;\r
   }\r
@@ -435,30 +384,20 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
-EFI_STATUS\r
+/**\r
+  This function is used to remove the whole PCI devices on the specified bridge from\r
+  the root bridge.\r
+\r
+  @param RootBridgeHandle   The root bridge device handle.\r
+  @param Bridge             The bridge device to be removed.\r
+\r
+**/\r
+VOID\r
 RemoveAllPciDeviceOnBridge (\r
   EFI_HANDLE               RootBridgeHandle,\r
   PCI_IO_DEVICE            *Bridge\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  This function is used to remove the whole PCI devices from the bridge.\r
-\r
-Arguments:\r
-\r
-  RootBridgeHandle   - An efi handle.\r
-  Bridge             - A pointer to the PCI_IO_DEVICE.\r
-\r
-Returns:\r
-\r
-  None\r
-\r
---*/\r
-// TODO:    EFI_SUCCESS - add return value to function comment\r
 {\r
-\r
   LIST_ENTRY      *CurrentLink;\r
   PCI_IO_DEVICE   *Temp;\r
 \r
@@ -486,36 +425,27 @@ Returns:
 \r
     FreePciDevice (Temp);\r
   }\r
-\r
-  return EFI_SUCCESS;\r
 }\r
 \r
-EFI_STATUS\r
-DeRegisterPciDevice (\r
-  IN  EFI_HANDLE                     Controller,\r
-  IN  EFI_HANDLE                     Handle\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
+/**\r
+  This function is used to de-register the PCI IO device.\r
 \r
-  This function is used to de-register the PCI device from the EFI,\r
   That includes un-installing PciIo protocol from the specified PCI\r
   device handle.\r
 \r
-Arguments:\r
+  @param Controller    An EFI handle for the PCI bus controller.\r
+  @param Handle        PCI device handle.\r
 \r
-  Controller   - An efi handle.\r
-  Handle       - An efi handle.\r
+  @retval EFI_SUCCESS  The PCI device is successfully de-registered.\r
+  @retval other        An error occurred when de-registering the PCI device.\r
 \r
-Returns:\r
-\r
-  None\r
+**/\r
+EFI_STATUS\r
+DeRegisterPciDevice (\r
+  IN  EFI_HANDLE                     Controller,\r
+  IN  EFI_HANDLE                     Handle\r
+  )\r
 \r
---*/\r
-// TODO:    EFI_SUCCESS - add return value to function comment\r
-// TODO:    EFI_SUCCESS - add return value to function comment\r
-// TODO:    EFI_SUCCESS - add return value to function comment\r
 {\r
   EFI_PCI_IO_PROTOCOL             *PciIo;\r
   EFI_STATUS                      Status;\r
@@ -550,7 +480,7 @@ Returns:
 \r
       CurrentLink = PciIoDevice->ChildList.ForwardLink;\r
 \r
-      while (CurrentLink && CurrentLink != &PciIoDevice->ChildList) {\r
+      while (CurrentLink != NULL && CurrentLink != &PciIoDevice->ChildList) {\r
         Node    = PCI_IO_DEVICE_FROM_LINK (CurrentLink);\r
         Status  = DeRegisterPciDevice (Controller, Node->Handle);\r
 \r
@@ -561,10 +491,6 @@ Returns:
         CurrentLink = CurrentLink->ForwardLink;\r
       }\r
     }\r
-    //\r
-    // Uninstall Pccard Hotplug GUID for Pccard device\r
-    //\r
-    UninstallPciHotplugGuid (PciIoDevice);\r
 \r
     //\r
     // Close the child handle\r
@@ -577,7 +503,8 @@ Returns:
                     );\r
 \r
     //\r
-    // Un-install the device path protocol and pci io protocol\r
+    // Un-install the Device Path protocol and PCI I/O protocol\r
+    // and Bus Specific Driver Override protocol if needed.\r
     //\r
     if (PciIoDevice->BusOverride) {\r
       Status = gBS->UninstallMultipleProtocolInterfaces (\r
@@ -601,6 +528,33 @@ Returns:
                       );\r
     }\r
 \r
+    if (!EFI_ERROR (Status)) {\r
+      //\r
+      // Try to uninstall LoadFile2 protocol if exists\r
+      //\r
+      Status = gBS->OpenProtocol (\r
+                      Handle,\r
+                      &gEfiLoadFile2ProtocolGuid,\r
+                      NULL,\r
+                      gPciBusDriverBinding.DriverBindingHandle,\r
+                      Controller,\r
+                      EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
+                      );\r
+      if (!EFI_ERROR (Status)) {\r
+        Status = gBS->UninstallMultipleProtocolInterfaces (\r
+                        Handle,\r
+                        &gEfiLoadFile2ProtocolGuid,\r
+                        &PciIoDevice->LoadFile2,\r
+                        NULL\r
+                        );\r
+      }\r
+      //\r
+      // Restore Status\r
+      //\r
+      Status = EFI_SUCCESS;\r
+    }\r
+\r
+\r
     if (EFI_ERROR (Status)) {\r
       gBS->OpenProtocol (\r
             Controller,\r
@@ -631,6 +585,21 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  Start to manage the PCI device on specified the root bridge or PCI-PCI Bridge\r
+\r
+  @param Controller          The root bridge handle.\r
+  @param RootBridge          A pointer to the PCI_IO_DEVICE.\r
+  @param RemainingDevicePath A pointer to the EFI_DEVICE_PATH_PROTOCOL.\r
+  @param NumberOfChildren    Children number.\r
+  @param ChildHandleBuffer   A pointer to the child handle buffer.\r
+\r
+  @retval EFI_NOT_READY   Device is not allocated.\r
+  @retval EFI_UNSUPPORTED Device only support PCI-PCI bridge.\r
+  @retval EFI_NOT_FOUND   Can not find the specific device.\r
+  @retval EFI_SUCCESS     Success to start Pci device on bridge.\r
+\r
+**/\r
 EFI_STATUS\r
 StartPciDevicesOnBridge (\r
   IN EFI_HANDLE                          Controller,\r
@@ -639,29 +608,7 @@ StartPciDevicesOnBridge (
   IN OUT UINT8                           *NumberOfChildren,\r
   IN OUT EFI_HANDLE                      *ChildHandleBuffer\r
   )\r
-/*++\r
 \r
-Routine Description:\r
-\r
-  Start to manage the PCI device on specified the root bridge or PCI-PCI Bridge\r
-\r
-Arguments:\r
-\r
-  Controller          - An efi handle.\r
-  RootBridge          - A pointer to the PCI_IO_DEVICE.\r
-  RemainingDevicePath - A pointer to the EFI_DEVICE_PATH_PROTOCOL.\r
-  NumberOfChildren    - Children number.\r
-  ChildHandleBuffer   - A pointer to the child handle buffer.\r
-\r
-Returns:\r
-\r
-  None\r
-\r
---*/\r
-// TODO:    EFI_NOT_READY - add return value to function comment\r
-// TODO:    EFI_SUCCESS - add return value to function comment\r
-// TODO:    EFI_UNSUPPORTED - add return value to function comment\r
-// TODO:    EFI_NOT_FOUND - add return value to function comment\r
 {\r
   PCI_IO_DEVICE             *PciIoDevice;\r
   EFI_DEV_PATH_PTR          Node;\r
@@ -672,14 +619,14 @@ Returns:
 \r
   CurrentLink = RootBridge->ChildList.ForwardLink;\r
 \r
-  while (CurrentLink && CurrentLink != &RootBridge->ChildList) {\r
+  while (CurrentLink != NULL && CurrentLink != &RootBridge->ChildList) {\r
 \r
     PciIoDevice = PCI_IO_DEVICE_FROM_LINK (CurrentLink);\r
     if (RemainingDevicePath != NULL) {\r
 \r
       Node.DevPath = RemainingDevicePath;\r
 \r
-      if (Node.Pci->Device != PciIoDevice->DeviceNumber || \r
+      if (Node.Pci->Device != PciIoDevice->DeviceNumber ||\r
           Node.Pci->Function != PciIoDevice->FunctionNumber) {\r
         CurrentLink = CurrentLink->ForwardLink;\r
         continue;\r
@@ -691,7 +638,7 @@ Returns:
       if (!PciIoDevice->Allocated) {\r
         return EFI_NOT_READY;\r
       }\r
-      \r
+\r
       //\r
       // Check if the current node has been registered before\r
       // If it is not, register it\r
@@ -709,12 +656,12 @@ Returns:
         ChildHandleBuffer[*NumberOfChildren] = PciIoDevice->Handle;\r
         (*NumberOfChildren)++;\r
       }\r
-      \r
+\r
       //\r
       // Get the next device path\r
       //\r
-      CurrentDevicePath = EfiNextDevicePathNode (RemainingDevicePath);\r
-      if (EfiIsDevicePathEnd (CurrentDevicePath)) {\r
+      CurrentDevicePath = NextDevicePathNode (RemainingDevicePath);\r
+      if (IsDevicePathEnd (CurrentDevicePath)) {\r
         return EFI_SUCCESS;\r
       }\r
 \r
@@ -759,7 +706,6 @@ Returns:
       // If remaining device path is NULL,\r
       // try to enable all the pci devices under this bridge\r
       //\r
-\r
       if (!PciIoDevice->Registered && PciIoDevice->Allocated) {\r
         Status = RegisterPciDevice (\r
                    Controller,\r
@@ -806,26 +752,22 @@ Returns:
   return EFI_NOT_FOUND;\r
 }\r
 \r
-EFI_STATUS\r
-StartPciDevices (\r
-  IN EFI_HANDLE                         Controller\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-  Start to manage all the PCI devices it found previously under \r
+/**\r
+  Start to manage all the PCI devices it found previously under\r
   the entire host bridge.\r
 \r
-Arguments:\r
-  Controller          - root bridge handle.\r
-\r
-Returns:\r
+  @param Controller          The root bridge handle.\r
 \r
-  None\r
+  @retval EFI_NOT_READY   Device is not allocated.\r
+  @retval EFI_SUCCESS     Success to start Pci device on host bridge.\r
 \r
---*/\r
+**/\r
+EFI_STATUS\r
+StartPciDevices (\r
+  IN EFI_HANDLE                         Controller\r
+  )\r
 {\r
+  EFI_STATUS        Status;\r
   PCI_IO_DEVICE     *RootBridge;\r
   EFI_HANDLE        ThisHostBridge;\r
   LIST_ENTRY        *CurrentLink;\r
@@ -834,22 +776,25 @@ Returns:
   ASSERT (RootBridge != NULL);\r
   ThisHostBridge = RootBridge->PciRootBridgeIo->ParentHandle;\r
 \r
-  CurrentLink = gPciDevicePool.ForwardLink;\r
+  CurrentLink = mPciDevicePool.ForwardLink;\r
 \r
-  while (CurrentLink && CurrentLink != &gPciDevicePool) {\r
+  while (CurrentLink != NULL && CurrentLink != &mPciDevicePool) {\r
 \r
     RootBridge = PCI_IO_DEVICE_FROM_LINK (CurrentLink);\r
     //\r
     // Locate the right root bridge to start\r
     //\r
     if (RootBridge->PciRootBridgeIo->ParentHandle == ThisHostBridge) {\r
-      StartPciDevicesOnBridge (\r
-        RootBridge->Handle,\r
-        RootBridge,\r
-        NULL,\r
-        NULL,\r
-        NULL\r
-        );\r
+      Status = StartPciDevicesOnBridge (\r
+                  RootBridge->Handle,\r
+                  RootBridge,\r
+                  NULL,\r
+                  NULL,\r
+                  NULL\r
+                  );\r
+      if (EFI_ERROR (Status)) {\r
+        return Status;\r
+      }\r
     }\r
 \r
     CurrentLink = CurrentLink->ForwardLink;\r
@@ -858,42 +803,30 @@ Returns:
   return EFI_SUCCESS;\r
 }\r
 \r
-PCI_IO_DEVICE *\r
-CreateRootBridge (\r
-  IN EFI_HANDLE RootBridgeHandle\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-\r
-Arguments:\r
-  RootBridgeHandle   - An efi handle.\r
+/**\r
+  Create root bridge device.\r
 \r
-Returns:\r
+  @param RootBridgeHandle    Specified root bridge hanle.\r
 \r
-  None\r
+  @return The crated root bridge device instance, NULL means no\r
+          root bridge device instance created.\r
 \r
---*/\r
+**/\r
+PCI_IO_DEVICE *\r
+CreateRootBridge (\r
+  IN EFI_HANDLE                   RootBridgeHandle\r
+  )\r
 {\r
-\r
   EFI_STATUS                      Status;\r
   PCI_IO_DEVICE                   *Dev;\r
   EFI_DEVICE_PATH_PROTOCOL        *ParentDevicePath;\r
   EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL *PciRootBridgeIo;\r
 \r
-  Dev = NULL;\r
-  Status = gBS->AllocatePool (\r
-                  EfiBootServicesData,\r
-                  sizeof (PCI_IO_DEVICE),\r
-                  (VOID **) &Dev\r
-                  );\r
-\r
-  if (EFI_ERROR (Status)) {\r
+  Dev = AllocateZeroPool (sizeof (PCI_IO_DEVICE));\r
+  if (Dev == NULL) {\r
     return NULL;\r
   }\r
 \r
-  ZeroMem (Dev, sizeof (PCI_IO_DEVICE));\r
   Dev->Signature  = PCI_IO_DEVICE_SIGNATURE;\r
   Dev->Handle     = RootBridgeHandle;\r
   InitializeListHead (&Dev->ChildList);\r
@@ -908,7 +841,7 @@ Returns:
                   );\r
 \r
   if (EFI_ERROR (Status)) {\r
-    gBS->FreePool (Dev);\r
+    FreePool (Dev);\r
     return NULL;\r
   }\r
 \r
@@ -939,8 +872,9 @@ Returns:
   //\r
   // Initialize the PCI I/O instance structure\r
   //\r
-  Status  = InitializePciIoInstance (Dev);\r
-  Status  = InitializePciDriverOverrideInstance (Dev);\r
+  InitializePciIoInstance (Dev);\r
+  InitializePciDriverOverrideInstance (Dev);\r
+  InitializePciLoadFile2 (Dev);\r
 \r
   //\r
   // Initialize reserved resource list and\r
@@ -952,31 +886,26 @@ Returns:
   return Dev;\r
 }\r
 \r
+/**\r
+  Get root bridge device instance by specific root bridge handle.\r
+\r
+  @param RootBridgeHandle    Given root bridge handle.\r
+\r
+  @return The root bridge device instance, NULL means no root bridge\r
+          device instance found.\r
+\r
+**/\r
 PCI_IO_DEVICE *\r
 GetRootBridgeByHandle (\r
   EFI_HANDLE RootBridgeHandle\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-\r
-Arguments:\r
-\r
-  RootBridgeHandle    - An efi handle.\r
-\r
-Returns:\r
-\r
-  None\r
-\r
---*/\r
 {\r
   PCI_IO_DEVICE   *RootBridgeDev;\r
   LIST_ENTRY      *CurrentLink;\r
 \r
-  CurrentLink = gPciDevicePool.ForwardLink;\r
+  CurrentLink = mPciDevicePool.ForwardLink;\r
 \r
-  while (CurrentLink && CurrentLink != &gPciDevicePool) {\r
+  while (CurrentLink != NULL && CurrentLink != &mPciDevicePool) {\r
 \r
     RootBridgeDev = PCI_IO_DEVICE_FROM_LINK (CurrentLink);\r
     if (RootBridgeDev->Handle == RootBridgeHandle) {\r
@@ -989,25 +918,21 @@ Returns:
   return NULL;\r
 }\r
 \r
+/**\r
+  Judege whether Pci device existed.\r
+\r
+  @param Bridge       Parent bridege instance.\r
+  @param PciIoDevice  Device instance.\r
+\r
+  @retval TRUE        Pci device existed.\r
+  @retval FALSE       Pci device did not exist.\r
+\r
+**/\r
 BOOLEAN\r
 PciDeviceExisted (\r
   IN PCI_IO_DEVICE    *Bridge,\r
   IN PCI_IO_DEVICE    *PciIoDevice\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-Arguments:\r
-\r
-  Bridge       - A pointer to the PCI_IO_DEVICE.\r
-  PciIoDevice  - A pointer to the PCI_IO_DEVICE.\r
-\r
-Returns:\r
-\r
-  None\r
-\r
---*/\r
 {\r
 \r
   PCI_IO_DEVICE   *Temp;\r
@@ -1015,7 +940,7 @@ Returns:
 \r
   CurrentLink = Bridge->ChildList.ForwardLink;\r
 \r
-  while (CurrentLink && CurrentLink != &Bridge->ChildList) {\r
+  while (CurrentLink != NULL && CurrentLink != &Bridge->ChildList) {\r
 \r
     Temp = PCI_IO_DEVICE_FROM_LINK (CurrentLink);\r
 \r
@@ -1035,30 +960,25 @@ Returns:
   return FALSE;\r
 }\r
 \r
+/**\r
+  Get the active VGA device on the same segment.\r
+\r
+  @param VgaDevice    PCI IO instance for the VGA device.\r
+\r
+  @return The active VGA device on the same segment.\r
+\r
+**/\r
 PCI_IO_DEVICE *\r
 ActiveVGADeviceOnTheSameSegment (\r
   IN PCI_IO_DEVICE        *VgaDevice\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-Arguments:\r
-\r
-  VgaDevice    - A pointer to the PCI_IO_DEVICE.\r
-\r
-Returns:\r
-\r
-  None\r
-\r
---*/\r
 {\r
   LIST_ENTRY      *CurrentLink;\r
   PCI_IO_DEVICE   *Temp;\r
 \r
-  CurrentLink = gPciDevicePool.ForwardLink;\r
+  CurrentLink = mPciDevicePool.ForwardLink;\r
 \r
-  while (CurrentLink && CurrentLink != &gPciDevicePool) {\r
+  while (CurrentLink != NULL && CurrentLink != &mPciDevicePool) {\r
 \r
     Temp = PCI_IO_DEVICE_FROM_LINK (CurrentLink);\r
 \r
@@ -1077,30 +997,25 @@ Returns:
   return NULL;\r
 }\r
 \r
+/**\r
+  Get the active VGA device on the root bridge.\r
+\r
+  @param RootBridge  PCI IO instance for the root bridge.\r
+\r
+  @return The active VGA device.\r
+\r
+**/\r
 PCI_IO_DEVICE *\r
 ActiveVGADeviceOnTheRootBridge (\r
   IN PCI_IO_DEVICE        *RootBridge\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-Arguments:\r
-\r
-  RootBridge    - A pointer to the PCI_IO_DEVICE.\r
-\r
-Returns:\r
-\r
-  None\r
-\r
---*/\r
 {\r
   LIST_ENTRY      *CurrentLink;\r
   PCI_IO_DEVICE   *Temp;\r
 \r
   CurrentLink = RootBridge->ChildList.ForwardLink;\r
 \r
-  while (CurrentLink && CurrentLink != &RootBridge->ChildList) {\r
+  while (CurrentLink != NULL && CurrentLink != &RootBridge->ChildList) {\r
 \r
     Temp = PCI_IO_DEVICE_FROM_LINK (CurrentLink);\r
 \r
@@ -1108,7 +1023,7 @@ Returns:
         (Temp->Attributes &\r
          (EFI_PCI_IO_ATTRIBUTE_VGA_MEMORY |\r
           EFI_PCI_IO_ATTRIBUTE_VGA_IO     |\r
-          EFI_PCI_IO_ATTRIBUTE_VGA_IO_16))) {\r
+          EFI_PCI_IO_ATTRIBUTE_VGA_IO_16)) != 0) {\r
       return Temp;\r
     }\r
 \r
@@ -1127,121 +1042,24 @@ Returns:
   return NULL;\r
 }\r
 \r
-EFI_STATUS\r
-GetHpcPciAddress (\r
-  IN  EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL  *PciRootBridgeIo,\r
-  IN  EFI_DEVICE_PATH_PROTOCOL         *HpcDevicePath,\r
-  OUT UINT64                           *PciAddress\r
-  )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-Arguments:\r
-\r
-  PciRootBridgeIo       - A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.\r
-  HpcDevicePath         - A pointer to the EFI_DEVICE_PATH_PROTOCL.\r
-  PciAddress            - A pointer to the pci address.\r
-\r
-Returns:\r
-\r
-  None\r
 \r
---*/\r
-// TODO:    EFI_NOT_FOUND - add return value to function comment\r
-// TODO:    EFI_NOT_FOUND - add return value to function comment\r
-// TODO:    EFI_SUCCESS - add return value to function comment\r
-// TODO:    EFI_NOT_FOUND - add return value to function comment\r
-{\r
-  EFI_DEVICE_PATH_PROTOCOL  *CurrentDevicePath;\r
-  EFI_DEV_PATH_PTR          Node;\r
-  LIST_ENTRY                *CurrentLink;\r
-  PCI_IO_DEVICE             *RootBridge;\r
-  EFI_STATUS                Status;\r
+/**\r
+  Get HPC PCI address according to its device path.\r
 \r
-  CurrentDevicePath = HpcDevicePath;\r
+  @param RootBridge           Root bridege Io instance.\r
+  @param RemainingDevicePath  Given searching device path.\r
+  @param PciAddress           Buffer holding searched result.\r
 \r
-  //\r
-  // Get the remaining device path for this PCI device, if it is a PCI device\r
-  //\r
-  while (!EfiIsDevicePathEnd (CurrentDevicePath)) {\r
-\r
-    Node.DevPath = CurrentDevicePath;\r
-\r
-    //\r
-    // Check if it is PCI device Path?\r
-    //\r
-    if ((Node.DevPath->Type != HARDWARE_DEVICE_PATH) ||\r
-        ((Node.DevPath->SubType != HW_PCI_DP)         &&\r
-         (DevicePathNodeLength (Node.DevPath) != sizeof (PCI_DEVICE_PATH)))) {\r
-      CurrentDevicePath = EfiNextDevicePathNode (CurrentDevicePath);\r
-      continue;\r
-    }\r
-\r
-    break;\r
-  }\r
-\r
-  //\r
-  // Check if it is not PCI device path\r
-  //\r
-  if (EfiIsDevicePathEnd (CurrentDevicePath)) {\r
-    return EFI_NOT_FOUND;\r
-  }\r
-\r
-  CurrentLink = gPciDevicePool.ForwardLink;\r
-\r
-  while (CurrentLink && CurrentLink != &gPciDevicePool) {\r
-\r
-    RootBridge = PCI_IO_DEVICE_FROM_LINK (CurrentLink);\r
-    //\r
-    // Locate the right root bridge to start\r
-    //\r
-    if (RootBridge->PciRootBridgeIo == PciRootBridgeIo) {\r
-      Status = GetHpcPciAddressFromRootBridge (\r
-                RootBridge,\r
-                CurrentDevicePath,\r
-                PciAddress\r
-                );\r
-      if (EFI_ERROR (Status)) {\r
-        return EFI_NOT_FOUND;\r
-      }\r
-\r
-      return EFI_SUCCESS;\r
-\r
-    }\r
-\r
-    CurrentLink = CurrentLink->ForwardLink;\r
-  }\r
-\r
-  return EFI_NOT_FOUND;\r
-}\r
+  @retval EFI_SUCCESS         PCI address was stored in PciAddress\r
+  @retval EFI_NOT_FOUND       Can not find the specific device path.\r
 \r
+**/\r
 EFI_STATUS\r
 GetHpcPciAddressFromRootBridge (\r
   IN  PCI_IO_DEVICE                    *RootBridge,\r
   IN  EFI_DEVICE_PATH_PROTOCOL         *RemainingDevicePath,\r
   OUT UINT64                           *PciAddress\r
   )\r
-/*++\r
-\r
-Routine Description:\r
-\r
-Arguments:\r
-\r
-  PciRootBridgeIo       - A pointer to the EFI_PCI_ROOT_BRIDGE_IO_PROTOCOL.\r
-  HpcDevicePath         - A pointer to the EFI_DEVICE_PATH_PROTOCL.\r
-  PciAddress            - A pointer to the pci address.\r
-\r
-Returns:\r
-\r
-  None\r
-\r
---*/\r
-// TODO:    RootBridge - add argument and description to function comment\r
-// TODO:    RemainingDevicePath - add argument and description to function comment\r
-// TODO:    EFI_SUCCESS - add return value to function comment\r
-// TODO:    EFI_NOT_FOUND - add return value to function comment\r
-// TODO:    EFI_SUCCESS - add return value to function comment\r
 {\r
   EFI_DEV_PATH_PTR          Node;\r
   PCI_IO_DEVICE             *Temp;\r
@@ -1255,12 +1073,12 @@ Returns:
   Node.DevPath      = CurrentDevicePath;\r
   Temp              = NULL;\r
 \r
-  while (!EfiIsDevicePathEnd (CurrentDevicePath)) {\r
+  while (!IsDevicePathEnd (CurrentDevicePath)) {\r
 \r
     CurrentLink   = RootBridge->ChildList.ForwardLink;\r
     Node.DevPath  = CurrentDevicePath;\r
 \r
-    while (CurrentLink && CurrentLink != &RootBridge->ChildList) {\r
+    while (CurrentLink != NULL && CurrentLink != &RootBridge->ChildList) {\r
       Temp = PCI_IO_DEVICE_FROM_LINK (CurrentLink);\r
 \r
       if (Node.Pci->Device   == Temp->DeviceNumber &&\r
@@ -1282,14 +1100,14 @@ Returns:
 \r
     }\r
 \r
-    CurrentDevicePath = EfiNextDevicePathNode (CurrentDevicePath);\r
+    CurrentDevicePath = NextDevicePathNode (CurrentDevicePath);\r
   }\r
 \r
   if (MisMatch) {\r
 \r
-    CurrentDevicePath = EfiNextDevicePathNode (CurrentDevicePath);\r
+    CurrentDevicePath = NextDevicePathNode (CurrentDevicePath);\r
 \r
-    if (EfiIsDevicePathEnd (CurrentDevicePath)) {\r
+    if (IsDevicePathEnd (CurrentDevicePath)) {\r
       *PciAddress = EFI_PCI_ADDRESS (RootBridge->BusNumber, Node.Pci->Device, Node.Pci->Function, 0);\r
       return EFI_SUCCESS;\r
     }\r
@@ -1297,8 +1115,13 @@ Returns:
     return EFI_NOT_FOUND;\r
   }\r
 \r
-  *PciAddress = EFI_PCI_ADDRESS (Temp->BusNumber, Temp->DeviceNumber, Temp->FunctionNumber, 0);\r
+  if (Temp != NULL) {\r
+    *PciAddress = EFI_PCI_ADDRESS (Temp->BusNumber, Temp->DeviceNumber, Temp->FunctionNumber, 0);\r
+  } else {\r
+    return EFI_NOT_FOUND;\r
+  }\r
 \r
   return EFI_SUCCESS;\r
 \r
 }\r
+\r