]> git.proxmox.com Git - mirror_edk2.git/blobdiff - EmbeddedPkg/Drivers/SataSiI3132Dxe/SiI3132AtaPassThru.c
EmbeddedPkg: Added SATA Silicon Image driver
[mirror_edk2.git] / EmbeddedPkg / Drivers / SataSiI3132Dxe / SiI3132AtaPassThru.c
diff --git a/EmbeddedPkg/Drivers/SataSiI3132Dxe/SiI3132AtaPassThru.c b/EmbeddedPkg/Drivers/SataSiI3132Dxe/SiI3132AtaPassThru.c
new file mode 100644 (file)
index 0000000..2fb5fd6
--- /dev/null
@@ -0,0 +1,825 @@
+/** @file\r
+*\r
+*  Copyright (c) 2011-2015, ARM Limited. All rights reserved.\r
+*\r
+*  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
+*\r
+**/\r
+\r
+#include "SataSiI3132.h"\r
+\r
+#include <IndustryStandard/Atapi.h>\r
+#include <Library/DevicePathLib.h>\r
+\r
+SATA_SI3132_DEVICE*\r
+GetSataDevice (\r
+  IN  SATA_SI3132_INSTANCE* SataInstance,\r
+  IN  UINT16 Port,\r
+  IN  UINT16 PortMultiplierPort\r
+) {\r
+  LIST_ENTRY              *List;\r
+  SATA_SI3132_PORT        *SataPort;\r
+  SATA_SI3132_DEVICE      *SataDevice;\r
+\r
+  if (Port >= SATA_SII3132_MAXPORT) {\r
+    return NULL;\r
+  }\r
+\r
+  SataPort = &(SataInstance->Ports[Port]);\r
+  List = SataPort->Devices.ForwardLink;\r
+\r
+  while (List != &SataPort->Devices) {\r
+    SataDevice = (SATA_SI3132_DEVICE*)List;\r
+    if (SataDevice->Index == PortMultiplierPort) {\r
+      return SataDevice;\r
+    }\r
+    List = List->ForwardLink;\r
+  }\r
+  return NULL;\r
+}\r
+\r
+EFI_STATUS\r
+SiI3132AtaPassThruCommand (\r
+  IN     SATA_SI3132_INSTANCE             *SataSiI3132Instance,\r
+  IN     SATA_SI3132_PORT                 *SataPort,\r
+  IN     UINT16                           PortMultiplierPort,\r
+  IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet,\r
+  IN     EFI_EVENT                        Event OPTIONAL\r
+  )\r
+{\r
+  SATA_SI3132_DEVICE      *SataDevice;\r
+  EFI_PHYSICAL_ADDRESS    PhysInDataBuffer;\r
+  UINTN                   InDataBufferLength = 0;\r
+  EFI_PHYSICAL_ADDRESS    PhysOutDataBuffer;\r
+  UINTN                   OutDataBufferLength;\r
+  CONST UINTN             EmptySlot = 0;\r
+  UINTN                   Control = PRB_CTRL_ATA;\r
+  UINTN                   Protocol = 0;\r
+  UINT32                  Value32, Error, Timeout = 0;\r
+  CONST UINT32            IrqMask = (SII3132_PORT_INT_CMDCOMPL | SII3132_PORT_INT_CMDERR) << 16;\r
+  EFI_STATUS              Status;\r
+  VOID*                   PciAllocMapping = NULL;\r
+  EFI_PCI_IO_PROTOCOL     *PciIo;\r
+\r
+  PciIo = SataSiI3132Instance->PciIo;\r
+  ZeroMem (SataPort->HostPRB, sizeof (SATA_SI3132_PRB));\r
+\r
+  // Construct Si3132 PRB\r
+  switch (Packet->Protocol) {\r
+  case EFI_ATA_PASS_THRU_PROTOCOL_ATA_HARDWARE_RESET:\r
+    ASSERT (0); //TODO: Implement me!\r
+    break;\r
+  case EFI_ATA_PASS_THRU_PROTOCOL_ATA_SOFTWARE_RESET:\r
+    SATA_TRACE ("SiI3132AtaPassThru() EFI_ATA_PASS_THRU_PROTOCOL_ATA_SOFTWARE_RESET");\r
+    Control = PRB_CTRL_SRST;\r
+\r
+    if (FeaturePcdGet (PcdSataSiI3132FeaturePMPSupport)) {\r
+        SataPort->HostPRB->Fis.Control = 0x0F;\r
+    }\r
+    break;\r
+  case EFI_ATA_PASS_THRU_PROTOCOL_ATA_NON_DATA:\r
+    ASSERT (0); //TODO: Implement me!\r
+    break;\r
+\r
+  // There is no difference for SiI3132 between PIO and DMA invokation\r
+  case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_IN:\r
+  case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN:\r
+    // Fixup the size for block transfer. Following UEFI Specification, 'InTransferLength' should\r
+    // be in number of bytes. But for most data transfer commands, the value is in number of blocks\r
+    if (Packet->Acb->AtaCommand == ATA_CMD_IDENTIFY_DRIVE) {\r
+      InDataBufferLength = Packet->InTransferLength;\r
+    } else {\r
+      SataDevice = GetSataDevice (SataSiI3132Instance, SataPort->Index, PortMultiplierPort);\r
+      if (!SataDevice || (SataDevice->BlockSize == 0)) {\r
+        return EFI_INVALID_PARAMETER;\r
+      }\r
+\r
+      InDataBufferLength = Packet->InTransferLength * SataDevice->BlockSize;\r
+    }\r
+\r
+    Status = PciIo->Map (\r
+               PciIo, EfiPciIoOperationBusMasterRead,\r
+               Packet->InDataBuffer, &InDataBufferLength, &PhysInDataBuffer, &PciAllocMapping\r
+               );\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+\r
+    // Construct SGEs (32-bit system)\r
+    SataPort->HostPRB->Sge[0].DataAddressLow = (UINT32)PhysInDataBuffer;\r
+    SataPort->HostPRB->Sge[0].DataAddressHigh = (UINT32)(PhysInDataBuffer >> 32);\r
+    SataPort->HostPRB->Sge[0].Attributes = SGE_TRM; // Only one SGE\r
+    SataPort->HostPRB->Sge[0].DataCount = InDataBufferLength;\r
+\r
+    // Copy the Ata Command Block\r
+    CopyMem (&SataPort->HostPRB->Fis, Packet->Acb, sizeof (EFI_ATA_COMMAND_BLOCK));\r
+\r
+    // Fixup the FIS\r
+    SataPort->HostPRB->Fis.FisType = 0x27; // Register - Host to Device FIS\r
+    SataPort->HostPRB->Fis.Control = 1 << 7; // Is a command\r
+    if (FeaturePcdGet (PcdSataSiI3132FeaturePMPSupport)) {\r
+      SataPort->HostPRB->Fis.Control |= PortMultiplierPort & 0xFF;\r
+    }\r
+    break;\r
+  case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_OUT:\r
+  case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT:\r
+    SataDevice = GetSataDevice (SataSiI3132Instance, SataPort->Index, PortMultiplierPort);\r
+    if (!SataDevice || (SataDevice->BlockSize == 0)) {\r
+      return EFI_INVALID_PARAMETER;\r
+    }\r
+\r
+    // Fixup the size for block transfer. Following UEFI Specification, 'InTransferLength' should\r
+    // be in number of bytes. But for most data transfer commands, the value is in number of blocks\r
+    OutDataBufferLength = Packet->OutTransferLength * SataDevice->BlockSize;\r
+\r
+    Status = PciIo->Map (\r
+               PciIo, EfiPciIoOperationBusMasterWrite,\r
+               Packet->OutDataBuffer, &OutDataBufferLength, &PhysOutDataBuffer, &PciAllocMapping\r
+               );\r
+    if (EFI_ERROR (Status)) {\r
+      return Status;\r
+    }\r
+\r
+    // Construct SGEs (32-bit system)\r
+    SataPort->HostPRB->Sge[0].DataAddressLow  = (UINT32)PhysOutDataBuffer;\r
+    SataPort->HostPRB->Sge[0].DataAddressHigh = (UINT32)(PhysOutDataBuffer >> 32);\r
+    SataPort->HostPRB->Sge[0].Attributes      = SGE_TRM; // Only one SGE\r
+    SataPort->HostPRB->Sge[0].DataCount       = OutDataBufferLength;\r
+\r
+    // Copy the Ata Command Block\r
+    CopyMem (&SataPort->HostPRB->Fis, Packet->Acb, sizeof (EFI_ATA_COMMAND_BLOCK));\r
+\r
+    // Fixup the FIS\r
+    SataPort->HostPRB->Fis.FisType = 0x27; // Register - Host to Device FIS\r
+    SataPort->HostPRB->Fis.Control = 1 << 7; // Is a command\r
+    if (FeaturePcdGet (PcdSataSiI3132FeaturePMPSupport)) {\r
+      SataPort->HostPRB->Fis.Control |= PortMultiplierPort & 0xFF;\r
+    }\r
+    break;\r
+  case EFI_ATA_PASS_THRU_PROTOCOL_DMA:\r
+    ASSERT (0); //TODO: Implement me!\r
+    break;\r
+  case EFI_ATA_PASS_THRU_PROTOCOL_DMA_QUEUED:\r
+    ASSERT (0); //TODO: Implement me!\r
+    break;\r
+  case EFI_ATA_PASS_THRU_PROTOCOL_DEVICE_DIAGNOSTIC:\r
+    ASSERT (0); //TODO: Implement me!\r
+    break;\r
+  case EFI_ATA_PASS_THRU_PROTOCOL_DEVICE_RESET:\r
+    ASSERT (0); //TODO: Implement me!\r
+    break;\r
+  case EFI_ATA_PASS_THRU_PROTOCOL_FPDMA:\r
+    ASSERT (0); //TODO: Implement me!\r
+    break;\r
+  case EFI_ATA_PASS_THRU_PROTOCOL_RETURN_RESPONSE:\r
+    ASSERT (0); //TODO: Implement me!\r
+    break;\r
+  default:\r
+    ASSERT (0);\r
+    break;\r
+  }\r
+\r
+  SataPort->HostPRB->Control = Control;\r
+  SataPort->HostPRB->ProtocolOverride = Protocol;\r
+\r
+  // Clear IRQ\r
+  SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_INTSTATUS_REG, IrqMask);\r
+\r
+  if (!FeaturePcdGet (PcdSataSiI3132FeatureDirectCommandIssuing)) {\r
+    // Indirect Command Issuance\r
+\r
+    //TODO: Find which slot is free (maybe use the Cmd FIFO)\r
+    //SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_CMDEXECFIFO_REG, &EmptySlot);\r
+\r
+    SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_CMDACTIV_REG + (EmptySlot * 8),\r
+                     (UINT32)(SataPort->PhysAddrHostPRB & 0xFFFFFFFF));\r
+    SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_CMDACTIV_REG + (EmptySlot * 8) + 4,\r
+                     (UINT32)((SataPort->PhysAddrHostPRB >> 32) & 0xFFFFFFFF));\r
+  } else {\r
+    // Direct Command Issuance\r
+    Status = PciIo->Mem.Write (PciIo, EfiPciIoWidthUint32, 1, // Bar 1\r
+        SataPort->RegBase + (EmptySlot * 0x80),\r
+        sizeof (SATA_SI3132_PRB) / 4,\r
+        SataPort->HostPRB);\r
+    ASSERT_EFI_ERROR (Status);\r
+\r
+    SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_CMDEXECFIFO_REG, EmptySlot);\r
+  }\r
+\r
+#if 0\r
+  // Could need to be implemented if we run multiple command in parallel to know which slot has been completed\r
+  SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_SLOTSTATUS_REG, &Value32);\r
+  Timeout = Packet->Timeout;\r
+  while (!Timeout && !Value32) {\r
+    gBS->Stall (1);\r
+    SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_SLOTSTATUS_REG, &Value32);\r
+    Timeout--;\r
+  }\r
+#else\r
+  SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_INTSTATUS_REG, &Value32);\r
+  if (!Packet->Timeout) {\r
+    while (!(Value32 & IrqMask)) {\r
+      gBS->Stall (1);\r
+      SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_INTSTATUS_REG, &Value32);\r
+    }\r
+  } else {\r
+    Timeout = Packet->Timeout;\r
+    while (Timeout && !(Value32 & IrqMask)) {\r
+      gBS->Stall (1);\r
+      SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_INTSTATUS_REG, &Value32);\r
+      Timeout--;\r
+    }\r
+  }\r
+#endif\r
+  // Fill Packet Ata Status Block\r
+  Status = PciIo->Mem.Read (PciIo, EfiPciIoWidthUint32, 1, // Bar 1\r
+      SataPort->RegBase + 0x08,\r
+      sizeof (EFI_ATA_STATUS_BLOCK) / 4,\r
+      Packet->Asb);\r
+  ASSERT_EFI_ERROR (Status);\r
+\r
+\r
+  if ((Packet->Timeout != 0) && (Timeout == 0)) {\r
+    DEBUG ((EFI_D_ERROR, "SiI3132AtaPassThru() Err:Timeout\n"));\r
+    //ASSERT (0);\r
+    return EFI_TIMEOUT;\r
+  } else if (Value32 & (SII3132_PORT_INT_CMDERR << 16)) {\r
+    SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_CMDERROR_REG, &Error);\r
+    DEBUG ((EFI_D_ERROR, "SiI3132AtaPassThru() CmdErr:0x%X (SiI3132 Err:0x%X)\n", Value32, Error));\r
+    ASSERT (0);\r
+    return EFI_DEVICE_ERROR;\r
+  } else if (Value32 & (SII3132_PORT_INT_CMDCOMPL << 16)) {\r
+    // Clear Command Complete\r
+    SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_INTSTATUS_REG, SII3132_PORT_INT_CMDCOMPL << 16);\r
+\r
+    if (PciAllocMapping) {\r
+      Status = PciIo->Unmap (PciIo, PciAllocMapping);\r
+      ASSERT (!EFI_ERROR (Status));\r
+    }\r
+\r
+    // If the command was ATA_CMD_IDENTIFY_DRIVE then we need to update the BlockSize\r
+    if (Packet->Acb->AtaCommand == ATA_CMD_IDENTIFY_DRIVE) {\r
+      ATA_IDENTIFY_DATA *IdentifyData = (ATA_IDENTIFY_DATA*)Packet->InDataBuffer;\r
+\r
+      // Get the corresponding Block Device\r
+      SataDevice = GetSataDevice (SataSiI3132Instance, SataPort->Index, PortMultiplierPort);\r
+\r
+      // Check logical block size\r
+      if ((IdentifyData->phy_logic_sector_support & BIT12) != 0) {\r
+        ASSERT (SataDevice != NULL);\r
+        SataDevice->BlockSize = (UINT32) (((IdentifyData->logic_sector_size_hi << 16) |\r
+                                            IdentifyData->logic_sector_size_lo) * sizeof (UINT16));\r
+      } else {\r
+        SataDevice->BlockSize = 0x200;\r
+      }\r
+    }\r
+    return EFI_SUCCESS;\r
+  } else {\r
+    ASSERT (0);\r
+    return EFI_DEVICE_ERROR;\r
+  }\r
+}\r
+\r
+/**\r
+  Sends an ATA command to an ATA device that is attached to the ATA controller. This function\r
+  supports both blocking I/O and non-blocking I/O. The blocking I/O functionality is required,\r
+  and the non-blocking I/O functionality is optional.\r
+\r
+  @param[in]     This                A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.\r
+  @param[in]     Port                The port number of the ATA device to send the command.\r
+  @param[in]     PortMultiplierPort  The port multiplier port number of the ATA device to send the command.\r
+                                     If there is no port multiplier, then specify 0.\r
+  @param[in,out] Packet              A pointer to the ATA command to send to the ATA device specified by Port\r
+                                     and PortMultiplierPort.\r
+  @param[in]     Event               If non-blocking I/O is not supported then Event is ignored, and blocking\r
+                                     I/O is performed. If Event is NULL, then blocking I/O is performed. If\r
+                                     Event is not NULL and non blocking I/O is supported, then non-blocking\r
+                                     I/O is performed, and Event will be signaled when the ATA command completes.\r
+\r
+  @retval EFI_SUCCESS                The ATA command was sent by the host. For bi-directional commands,\r
+                                     InTransferLength bytes were transferred from InDataBuffer. For write and\r
+                                     bi-directional commands, OutTransferLength bytes were transferred by OutDataBuffer.\r
+  @retval EFI_BAD_BUFFER_SIZE        The ATA command was not executed. The number of bytes that could be transferred\r
+                                     is returned in InTransferLength. For write and bi-directional commands,\r
+                                     OutTransferLength bytes were transferred by OutDataBuffer.\r
+  @retval EFI_NOT_READY              The ATA command could not be sent because there are too many ATA commands\r
+                                     already queued. The caller may retry again later.\r
+  @retval EFI_DEVICE_ERROR           A device error occurred while attempting to send the ATA command.\r
+  @retval EFI_INVALID_PARAMETER      Port, PortMultiplierPort, or the contents of Acb are invalid. The ATA\r
+                                     command was not sent, so no additional status information is available.\r
+\r
+**/\r
+EFI_STATUS\r
+SiI3132AtaPassThru (\r
+  IN     EFI_ATA_PASS_THRU_PROTOCOL       *This,\r
+  IN     UINT16                           Port,\r
+  IN     UINT16                           PortMultiplierPort,\r
+  IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet,\r
+  IN     EFI_EVENT                        Event OPTIONAL\r
+  )\r
+{\r
+  SATA_SI3132_INSTANCE    *SataSiI3132Instance;\r
+  SATA_SI3132_DEVICE      *SataDevice;\r
+  SATA_SI3132_PORT        *SataPort;\r
+\r
+  SataSiI3132Instance = INSTANCE_FROM_ATAPASSTHRU_THIS (This);\r
+  if (!SataSiI3132Instance) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  SataDevice = GetSataDevice (SataSiI3132Instance, Port, PortMultiplierPort);\r
+  if (!SataDevice) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  SataPort = SataDevice->Port;\r
+\r
+  DEBUG ((EFI_D_INFO, "SiI3132AtaPassThru(%d,%d) : AtaCmd:0x%X Prot:%d\n", Port, PortMultiplierPort,\r
+         Packet->Acb->AtaCommand, Packet->Protocol));\r
+\r
+  return SiI3132AtaPassThruCommand (SataSiI3132Instance, SataPort, PortMultiplierPort, Packet, Event);\r
+}\r
+\r
+/**\r
+  Used to retrieve the list of legal port numbers for ATA devices on an ATA controller.\r
+  These can either be the list of ports where ATA devices are actually present or the\r
+  list of legal port numbers for the ATA controller. Regardless, the caller of this\r
+  function must probe the port number returned to see if an ATA device is actually\r
+  present at that location on the ATA controller.\r
+\r
+  The GetNextPort() function retrieves the port number on an ATA controller. If on input\r
+  Port is 0xFFFF, then the port number of the first port on the ATA controller is returned\r
+  in Port and EFI_SUCCESS is returned.\r
+\r
+  If Port is a port number that was returned on a previous call to GetNextPort(), then the\r
+  port number of the next port on the ATA controller is returned in Port, and EFI_SUCCESS\r
+  is returned. If Port is not 0xFFFF and Port was not returned on a previous call to\r
+  GetNextPort(), then EFI_INVALID_PARAMETER is returned.\r
+\r
+  If Port is the port number of the last port on the ATA controller, then EFI_NOT_FOUND is\r
+  returned.\r
+\r
+  @param[in]     This           A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.\r
+  @param[in,out] Port           On input, a pointer to the port number on the ATA controller.\r
+                                On output, a pointer to the next port number on the ATA\r
+                                controller. An input value of 0xFFFF retrieves the first port\r
+                                number on the ATA controller.\r
+\r
+  @retval EFI_SUCCESS           The next port number on the ATA controller was returned in Port.\r
+  @retval EFI_NOT_FOUND         There are no more ports on this ATA controller.\r
+  @retval EFI_INVALID_PARAMETER Port is not 0xFFFF and Port was not returned on a previous call\r
+                                to GetNextPort().\r
+\r
+**/\r
+EFI_STATUS\r
+SiI3132GetNextPort (\r
+  IN EFI_ATA_PASS_THRU_PROTOCOL *This,\r
+  IN OUT UINT16                 *Port\r
+  )\r
+{\r
+  SATA_SI3132_INSTANCE    *SataSiI3132Instance;\r
+  UINTN                   PrevPort;\r
+  EFI_STATUS              Status = EFI_SUCCESS;\r
+\r
+  SataSiI3132Instance = INSTANCE_FROM_ATAPASSTHRU_THIS (This);\r
+  if (!SataSiI3132Instance) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  PrevPort = *Port;\r
+\r
+  if (PrevPort == 0xFFFF) {\r
+    *Port = 0;\r
+  } else {\r
+    if (PrevPort < SATA_SII3132_MAXPORT) {\r
+        *Port = PrevPort + 1;\r
+    } else {\r
+        Status = EFI_NOT_FOUND;\r
+    }\r
+  }\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Used to retrieve the list of legal port multiplier port numbers for ATA devices on a port of an ATA\r
+  controller. These can either be the list of port multiplier ports where ATA devices are actually\r
+  present on port or the list of legal port multiplier ports on that port. Regardless, the caller of this\r
+  function must probe the port number and port multiplier port number returned to see if an ATA\r
+  device is actually present.\r
+\r
+  The GetNextDevice() function retrieves the port multiplier port number of an ATA device\r
+  present on a port of an ATA controller.\r
+\r
+  If PortMultiplierPort points to a port multiplier port number value that was returned on a\r
+  previous call to GetNextDevice(), then the port multiplier port number of the next ATA device\r
+  on the port of the ATA controller is returned in PortMultiplierPort, and EFI_SUCCESS is\r
+  returned.\r
+\r
+  If PortMultiplierPort points to 0xFFFF, then the port multiplier port number of the first\r
+  ATA device on port of the ATA controller is returned in PortMultiplierPort and\r
+  EFI_SUCCESS is returned.\r
+\r
+  If PortMultiplierPort is not 0xFFFF and the value pointed to by PortMultiplierPort\r
+  was not returned on a previous call to GetNextDevice(), then EFI_INVALID_PARAMETER\r
+  is returned.\r
+\r
+  If PortMultiplierPort is the port multiplier port number of the last ATA device on the port of\r
+  the ATA controller, then EFI_NOT_FOUND is returned.\r
+\r
+  @param[in]     This                A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.\r
+  @param[in]     Port                The port number present on the ATA controller.\r
+  @param[in,out] PortMultiplierPort  On input, a pointer to the port multiplier port number of an\r
+                                     ATA device present on the ATA controller.\r
+                                     If on input a PortMultiplierPort of 0xFFFF is specified,\r
+                                     then the port multiplier port number of the first ATA device\r
+                                     is returned. On output, a pointer to the port multiplier port\r
+                                     number of the next ATA device present on an ATA controller.\r
+\r
+  @retval EFI_SUCCESS                The port multiplier port number of the next ATA device on the port\r
+                                     of the ATA controller was returned in PortMultiplierPort.\r
+  @retval EFI_NOT_FOUND              There are no more ATA devices on this port of the ATA controller.\r
+  @retval EFI_INVALID_PARAMETER      PortMultiplierPort is not 0xFFFF, and PortMultiplierPort was not\r
+                                     returned on a previous call to GetNextDevice().\r
+\r
+**/\r
+EFI_STATUS\r
+SiI3132GetNextDevice (\r
+  IN EFI_ATA_PASS_THRU_PROTOCOL *This,\r
+  IN UINT16                     Port,\r
+  IN OUT UINT16                 *PortMultiplierPort\r
+  )\r
+{\r
+  SATA_SI3132_INSTANCE    *SataSiI3132Instance;\r
+  SATA_SI3132_PORT        *SataPort;\r
+  SATA_SI3132_DEVICE      *SataDevice;\r
+  LIST_ENTRY              *List;\r
+  EFI_STATUS              Status = EFI_SUCCESS;\r
+\r
+  SataSiI3132Instance = INSTANCE_FROM_ATAPASSTHRU_THIS (This);\r
+  if (!SataSiI3132Instance) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Port >= SATA_SII3132_MAXPORT) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  SataPort = &(SataSiI3132Instance->Ports[Port]);\r
+\r
+  if (*PortMultiplierPort == 0xFFFF) {\r
+    List = SataPort->Devices.ForwardLink;\r
+    if (List != &SataPort->Devices) {\r
+      // The list is not empty, return the first device\r
+      *PortMultiplierPort = ((SATA_SI3132_DEVICE*)List)->Index;\r
+    } else {\r
+      Status = EFI_NOT_FOUND;\r
+    }\r
+  } else {\r
+    SataDevice = GetSataDevice (SataSiI3132Instance, Port, *PortMultiplierPort);\r
+    if (SataDevice != NULL) {\r
+      // We have found the previous port multiplier, return the next one\r
+      List = SataDevice->Link.ForwardLink;\r
+      if (List != &SataPort->Devices) {\r
+        *PortMultiplierPort = ((SATA_SI3132_DEVICE*)List)->Index;\r
+      } else {\r
+        Status = EFI_NOT_FOUND;\r
+      }\r
+    } else {\r
+      Status = EFI_NOT_FOUND;\r
+    }\r
+  }\r
+  return Status;\r
+}\r
+\r
+/**\r
+  Used to allocate and build a device path node for an ATA device on an ATA controller.\r
+\r
+  The BuildDevicePath() function allocates and builds a single device node for the ATA\r
+  device specified by Port and PortMultiplierPort. If the ATA device specified by Port and\r
+  PortMultiplierPort is not present on the ATA controller, then EFI_NOT_FOUND is returned.\r
+  If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned. If there are not enough\r
+  resources to allocate the device path node, then EFI_OUT_OF_RESOURCES is returned.\r
+\r
+  Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of\r
+  DevicePath are initialized to describe the ATA device specified by Port and PortMultiplierPort,\r
+  and EFI_SUCCESS is returned.\r
+\r
+  @param[in]     This                A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.\r
+  @param[in]     Port                Port specifies the port number of the ATA device for which a\r
+                                     device path node is to be allocated and built.\r
+  @param[in]     PortMultiplierPort  The port multiplier port number of the ATA device for which a\r
+                                     device path node is to be allocated and built. If there is no\r
+                                     port multiplier, then specify 0.\r
+  @param[in,out] DevicePath          A pointer to a single device path node that describes the ATA\r
+                                     device specified by Port and PortMultiplierPort. This function\r
+                                     is responsible for allocating the buffer DevicePath with the\r
+                                     boot service AllocatePool(). It is the caller's responsibility\r
+                                     to free DevicePath when the caller is finished with DevicePath.\r
+  @retval EFI_SUCCESS                The device path node that describes the ATA device specified by\r
+                                     Port and PortMultiplierPort was allocated and returned in DevicePath.\r
+  @retval EFI_NOT_FOUND              The ATA device specified by Port and PortMultiplierPort does not\r
+                                     exist on the ATA controller.\r
+  @retval EFI_INVALID_PARAMETER      DevicePath is NULL.\r
+  @retval EFI_OUT_OF_RESOURCES       There are not enough resources to allocate DevicePath.\r
+\r
+**/\r
+EFI_STATUS\r
+SiI3132BuildDevicePath (\r
+  IN     EFI_ATA_PASS_THRU_PROTOCOL *This,\r
+  IN     UINT16                     Port,\r
+  IN     UINT16                     PortMultiplierPort,\r
+  IN OUT EFI_DEVICE_PATH_PROTOCOL   **DevicePath\r
+  )\r
+{\r
+  SATA_SI3132_INSTANCE        *SataSiI3132Instance;\r
+  SATA_SI3132_DEVICE          *SataDevice;\r
+  EFI_DEVICE_PATH_PROTOCOL    *SiI3132DevicePath;\r
+\r
+  SATA_TRACE ("SiI3132BuildDevicePath()");\r
+\r
+  SataSiI3132Instance = INSTANCE_FROM_ATAPASSTHRU_THIS (This);\r
+  if (!SataSiI3132Instance) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  SataDevice = GetSataDevice (SataSiI3132Instance, Port, PortMultiplierPort);\r
+  if (SataDevice == NULL) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  SiI3132DevicePath = CreateDeviceNode (MESSAGING_DEVICE_PATH, MSG_SATA_DP, sizeof (SATA_DEVICE_PATH));\r
+  if (SiI3132DevicePath == NULL) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  ((SATA_DEVICE_PATH*)SiI3132DevicePath)->HBAPortNumber = Port;\r
+  if (FeaturePcdGet (PcdSataSiI3132FeaturePMPSupport)) {\r
+    ((SATA_DEVICE_PATH*)SiI3132DevicePath)->PortMultiplierPortNumber = PortMultiplierPort;\r
+  } else {\r
+    //Temp:((SATA_DEVICE_PATH*)SiI3132DevicePath)->PortMultiplierPortNumber = SATA_HBA_DIRECT_CONNECT_FLAG;\r
+    ((SATA_DEVICE_PATH*)SiI3132DevicePath)->PortMultiplierPortNumber = 0;\r
+  }\r
+  ((SATA_DEVICE_PATH*)SiI3132DevicePath)->Lun = Port; //TODO: Search information how to define properly LUN (Logical Unit Number)\r
+\r
+  *DevicePath = SiI3132DevicePath;\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+/**\r
+  Used to translate a device path node to a port number and port multiplier port number.\r
+\r
+  The GetDevice() function determines the port and port multiplier port number associated with\r
+  the ATA device described by DevicePath. If DevicePath is a device path node type that the\r
+  ATA Pass Thru driver supports, then the ATA Pass Thru driver will attempt to translate the contents\r
+  DevicePath into a port number and port multiplier port number.\r
+\r
+  If this translation is successful, then that port number and port multiplier port number are returned\r
+  in Port and PortMultiplierPort, and EFI_SUCCESS is returned.\r
+\r
+  If DevicePath, Port, or PortMultiplierPort are NULL, then EFI_INVALID_PARAMETER is returned.\r
+\r
+  If DevicePath is not a device path node type that the ATA Pass Thru driver supports, then\r
+  EFI_UNSUPPORTED is returned.\r
+\r
+  If DevicePath is a device path node type that the ATA Pass Thru driver supports, but there is not\r
+  a valid translation from DevicePath to a port number and port multiplier port number, then\r
+  EFI_NOT_FOUND is returned.\r
+\r
+  @param[in]  This                A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.\r
+  @param[in]  DevicePath          A pointer to the device path node that describes an ATA device on the\r
+                                  ATA controller.\r
+  @param[out] Port                On return, points to the port number of an ATA device on the ATA controller.\r
+  @param[out] PortMultiplierPort  On return, points to the port multiplier port number of an ATA device\r
+                                  on the ATA controller.\r
+\r
+  @retval EFI_SUCCESS             DevicePath was successfully translated to a port number and port multiplier\r
+                                  port number, and they were returned in Port and PortMultiplierPort.\r
+  @retval EFI_INVALID_PARAMETER   DevicePath is NULL.\r
+  @retval EFI_INVALID_PARAMETER   Port is NULL.\r
+  @retval EFI_INVALID_PARAMETER   PortMultiplierPort is NULL.\r
+  @retval EFI_UNSUPPORTED         This driver does not support the device path node type in DevicePath.\r
+  @retval EFI_NOT_FOUND           A valid translation from DevicePath to a port number and port multiplier\r
+                                  port number does not exist.\r
+**/\r
+EFI_STATUS\r
+SiI3132GetDevice (\r
+  IN  EFI_ATA_PASS_THRU_PROTOCOL *This,\r
+  IN  EFI_DEVICE_PATH_PROTOCOL   *DevicePath,\r
+  OUT UINT16                     *Port,\r
+  OUT UINT16                     *PortMultiplierPort\r
+  )\r
+{\r
+  SATA_SI3132_INSTANCE        *SataSiI3132Instance;\r
+\r
+  SATA_TRACE ("SiI3132GetDevice()");\r
+\r
+  if (!DevicePath || !Port || !PortMultiplierPort) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if ((DevicePath->Type != MESSAGING_DEVICE_PATH) || (DevicePath->SubType != MSG_SATA_DP)) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  SataSiI3132Instance = INSTANCE_FROM_ATAPASSTHRU_THIS (This);\r
+  if (!SataSiI3132Instance) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (((SATA_DEVICE_PATH*)DevicePath)->Lun >= SATA_SII3132_MAXPORT) {\r
+    return EFI_NOT_FOUND;\r
+  }\r
+\r
+  if (FeaturePcdGet (PcdSataSiI3132FeaturePMPSupport)) {\r
+    ASSERT (0); //TODO: Implement me!\r
+    return EFI_UNSUPPORTED;\r
+  } else {\r
+    *Port = ((SATA_DEVICE_PATH*)DevicePath)->Lun;\r
+    // Return the first Sata Sevice as there should be only one directly connected\r
+    *PortMultiplierPort = ((SATA_SI3132_DEVICE*)SataSiI3132Instance->Ports[*Port].Devices.ForwardLink)->Index;\r
+    return EFI_SUCCESS;\r
+  }\r
+}\r
+\r
+EFI_STATUS\r
+SiI3132HwResetPort (\r
+  IN SATA_SI3132_PORT *SataPort\r
+  )\r
+{\r
+  EFI_PCI_IO_PROTOCOL *PciIo;\r
+  UINT32              Value32;\r
+  UINTN               Timeout;\r
+\r
+  SATA_TRACE ("SiI3132HwResetPort()");\r
+\r
+  PciIo = SataPort->Instance->PciIo;\r
+\r
+  // Clear Port Reset\r
+  SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_CONTROLCLEAR_REG, SII3132_PORT_CONTROL_RESET);\r
+\r
+  SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_STATUS_REG, &Value32);\r
+  ASSERT (!(Value32 & SII3132_PORT_CONTROL_RESET));\r
+\r
+  // Initialize error counters\r
+  SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_ERRCOUNTDECODE, 0);\r
+  SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_ERRCOUNTCRC, 0);\r
+  SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_ERRCOUNTHANDSHAKE, 0);\r
+\r
+  // Enable interrupts for command completion and command errors\r
+  SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_ENABLEINT_REG, SII3132_PORT_INT_CMDCOMPL | SII3132_PORT_INT_CMDERR);\r
+\r
+  // Clear IRQ\r
+  SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_ENABLEINT_REG, SII3132_PORT_INT_CMDCOMPL | SII3132_PORT_INT_CMDERR | SII3132_PORT_INT_PORTRDY | (1 << 3));\r
+\r
+  // Wait until Port Ready\r
+  SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_INTSTATUS_REG, &Value32);\r
+  Timeout = 1000;\r
+  while ((Timeout > 0) && ((Value32 & SII3132_PORT_INT_PORTRDY) == 0)) {\r
+    gBS->Stall (1);\r
+    Timeout--;\r
+    SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_INTSTATUS_REG, &Value32);\r
+  }\r
+  // Clear IRQ\r
+  SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_INTSTATUS_REG, SII3132_PORT_INT_PORTRDY);\r
+\r
+  if (Timeout == 0) {\r
+    SATA_TRACE ("SiI3132HwResetPort(): Timeout");\r
+    return EFI_TIMEOUT;\r
+  } else if ((Value32 & SII3132_PORT_INT_PORTRDY) == 0) {\r
+    SATA_TRACE ("SiI3132HwResetPort(): Port Not Ready");\r
+    return EFI_DEVICE_ERROR;\r
+  } else {\r
+    return EFI_SUCCESS;\r
+  }\r
+}\r
+\r
+/**\r
+  Resets a specific port on the ATA controller. This operation also resets all the ATA devices\r
+  connected to the port.\r
+\r
+  The ResetChannel() function resets an a specific port on an ATA controller. This operation\r
+  resets all the ATA devices connected to that port. If this ATA controller does not support\r
+  a reset port operation, then EFI_UNSUPPORTED is returned.\r
+\r
+  If a device error occurs while executing that port reset operation, then EFI_DEVICE_ERROR is\r
+  returned.\r
+\r
+  If a timeout occurs during the execution of the port reset operation, then EFI_TIMEOUT is returned.\r
+\r
+  If the port reset operation is completed, then EFI_SUCCESS is returned.\r
+\r
+  @param[in]  This          A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.\r
+  @param[in]  Port          The port number on the ATA controller.\r
+\r
+  @retval EFI_SUCCESS       The ATA controller port was reset.\r
+  @retval EFI_UNSUPPORTED   The ATA controller does not support a port reset operation.\r
+  @retval EFI_DEVICE_ERROR  A device error occurred while attempting to reset the ATA port.\r
+  @retval EFI_TIMEOUT       A timeout occurred while attempting to reset the ATA port.\r
+\r
+**/\r
+EFI_STATUS\r
+SiI3132ResetPort (\r
+  IN EFI_ATA_PASS_THRU_PROTOCOL *This,\r
+  IN UINT16                     Port\r
+  )\r
+{\r
+  SATA_SI3132_INSTANCE    *SataSiI3132Instance;\r
+  SATA_SI3132_PORT        *SataPort;\r
+\r
+  SATA_TRACE ("SiI3132ResetPort()");\r
+\r
+  SataSiI3132Instance = INSTANCE_FROM_ATAPASSTHRU_THIS (This);\r
+  if (!SataSiI3132Instance) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  if (Port >= SATA_SII3132_MAXPORT) {\r
+    return EFI_UNSUPPORTED;\r
+  }\r
+\r
+  SataPort = &(SataSiI3132Instance->Ports[Port]);\r
+  return SiI3132HwResetPort (SataPort);\r
+}\r
+\r
+/**\r
+  Resets an ATA device that is connected to an ATA controller.\r
+\r
+  The ResetDevice() function resets the ATA device specified by Port and PortMultiplierPort.\r
+  If this ATA controller does not support a device reset operation, then EFI_UNSUPPORTED is\r
+  returned.\r
+\r
+  If Port or PortMultiplierPort are not in a valid range for this ATA controller, then\r
+  EFI_INVALID_PARAMETER is returned.\r
+\r
+  If a device error occurs while executing that device reset operation, then EFI_DEVICE_ERROR\r
+  is returned.\r
+\r
+  If a timeout occurs during the execution of the device reset operation, then EFI_TIMEOUT is\r
+  returned.\r
+\r
+  If the device reset operation is completed, then EFI_SUCCESS is returned.\r
+\r
+  @param[in] This                A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.\r
+  @param[in] Port                Port represents the port number of the ATA device to be reset.\r
+  @param[in] PortMultiplierPort  The port multiplier port number of the ATA device to reset.\r
+                                 If there is no port multiplier, then specify 0.\r
+  @retval EFI_SUCCESS            The ATA device specified by Port and PortMultiplierPort was reset.\r
+  @retval EFI_UNSUPPORTED        The ATA controller does not support a device reset operation.\r
+  @retval EFI_INVALID_PARAMETER  Port or PortMultiplierPort are invalid.\r
+  @retval EFI_DEVICE_ERROR       A device error occurred while attempting to reset the ATA device\r
+                                 specified by Port and PortMultiplierPort.\r
+  @retval EFI_TIMEOUT            A timeout occurred while attempting to reset the ATA device\r
+                                 specified by Port and PortMultiplierPort.\r
+\r
+**/\r
+EFI_STATUS\r
+SiI3132ResetDevice (\r
+  IN EFI_ATA_PASS_THRU_PROTOCOL *This,\r
+  IN UINT16                     Port,\r
+  IN UINT16                     PortMultiplierPort\r
+  )\r
+{\r
+  EFI_PCI_IO_PROTOCOL     *PciIo;\r
+  SATA_SI3132_INSTANCE    *SataSiI3132Instance;\r
+  SATA_SI3132_PORT        *SataPort;\r
+  SATA_SI3132_DEVICE      *SataDevice;\r
+  UINTN                   Timeout;\r
+  UINT32                  Value32;\r
+\r
+  SATA_TRACE ("SiI3132ResetDevice()");\r
+\r
+  SataSiI3132Instance = INSTANCE_FROM_ATAPASSTHRU_THIS (This);\r
+  if (!SataSiI3132Instance) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  PciIo = SataSiI3132Instance->PciIo;\r
+\r
+  SataDevice = GetSataDevice (SataSiI3132Instance, Port, PortMultiplierPort);\r
+  if (!SataDevice) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+  SataPort = SataDevice->Port;\r
+\r
+  SATA_PORT_WRITE32 (SataPort->RegBase + SII3132_PORT_CONTROLSET_REG, SII3132_PORT_DEVICE_RESET);\r
+\r
+  Timeout = 100;\r
+  SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_STATUS_REG, &Value32);\r
+  while ((Timeout > 0) && ((Value32 & SII3132_PORT_DEVICE_RESET) != 0)) {\r
+    gBS->Stall (1);\r
+    SATA_PORT_READ32 (SataPort->RegBase + SII3132_PORT_STATUS_REG, &Value32);\r
+    Timeout--;\r
+  }\r
+\r
+  if (Timeout == 0) {\r
+    SATA_TRACE ("SiI3132ResetDevice(): Timeout");\r
+    return EFI_TIMEOUT;\r
+  } else {\r
+    return EFI_SUCCESS;\r
+  }\r
+}\r