]> xenbits.xensource.com Git - ovmf.git/commitdiff
MdeModulePkg/Ahci: Skip retry for non-transient errors
authorAlbecki, Mateusz <mateusz.albecki@intel.com>
Mon, 27 Mar 2023 21:37:35 +0000 (05:37 +0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Fri, 31 Mar 2023 21:01:13 +0000 (21:01 +0000)
bugzilla: https://bugzilla.tianocore.org/show_bug.cgi?id=4011

Currently AHCI driver will try to retry all failed packets
regardless of the failure cause. This is a problem in password
unlock flow where number of password retries is tracked by the
device. If user passes a wrong password Ahci driver will try
to send the wrong password multiple times which will exhaust
number of password retries and force the user to restart the
machine. This commit introduces a logic to check for the cause
of packet failure and only retry packets which failed due to
transient conditions on the link. With this patch only packets for
which CRC error is flagged are retried.

Cc: Hao A Wu <hao.a.wu@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Hunter Chang <hunter.chang@intel.com>
Cc: Baraneedharan Anbazhagan <anbazhagan@hp.com>
Signed-off-by: Mateusz Albecki <mateusz.albecki@intel.com>
Reviewed-by: Hao A Wu <hao.a.wu@intel.com>
Reviewed-by: Baraneedharan Anbazhagan <anbazhagan@hp.com>
MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AhciMode.c
MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AhciMode.h

index 06c4a3e052876e7f44a27e4cc6ff39b61bcb8179..c0c8ffbd9ec52b85cd0b8f09984ef23ea8d3e65b 100644 (file)
@@ -737,12 +737,68 @@ AhciRecoverPortError (
     Status = AhciResetPort (PciIo, Port);\r
     if (EFI_ERROR (Status)) {\r
       DEBUG ((DEBUG_ERROR, "Failed to reset the port %d\n", Port));\r
+      return EFI_DEVICE_ERROR;\r
     }\r
   }\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
+/**\r
+  This function will check if the failed command should be retired. Only error\r
+  conditions which are a result of transient conditions on a link(either to system or to device).\r
+\r
+  @param[in] PciIo    Pointer to AHCI controller PciIo.\r
+  @param[in] Port     SATA port index on which to check.\r
+\r
+  @retval TRUE   Command failure was caused by transient condition and should be retried\r
+  @retval FALSE  Command should not be retried\r
+**/\r
+BOOLEAN\r
+AhciShouldCmdBeRetried (\r
+  IN EFI_PCI_IO_PROTOCOL  *PciIo,\r
+  IN UINT8                Port\r
+  )\r
+{\r
+  UINT32  Offset;\r
+  UINT32  PortInterrupt;\r
+  UINT32  Serr;\r
+  UINT32  Tfd;\r
+\r
+  Offset        = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_IS;\r
+  PortInterrupt = AhciReadReg (PciIo, Offset);\r
+  Offset        = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_SERR;\r
+  Serr          = AhciReadReg (PciIo, Offset);\r
+  Offset        = EFI_AHCI_PORT_START + Port * EFI_AHCI_PORT_REG_WIDTH + EFI_AHCI_PORT_TFD;\r
+  Tfd           = AhciReadReg (PciIo, Offset);\r
+\r
+  //\r
+  // This can occur if there was a CRC error on a path from system memory to\r
+  // host controller.\r
+  //\r
+  if (PortInterrupt & EFI_AHCI_PORT_IS_HBDS) {\r
+    return TRUE;\r
+    //\r
+    // This can occur if there was a CRC error detected by host during communication\r
+    // with the device\r
+    //\r
+  } else if ((PortInterrupt & (EFI_AHCI_PORT_IS_IFS | EFI_AHCI_PORT_IS_INFS)) &&\r
+             (Serr & EFI_AHCI_PORT_SERR_CRCE))\r
+  {\r
+    return TRUE;\r
+    //\r
+    // This can occur if there was a CRC error detected by device during communication\r
+    // with the host. Device returns error status to host with D2H FIS.\r
+    //\r
+  } else if ((PortInterrupt & EFI_AHCI_PORT_IS_TFES) &&\r
+             (Tfd & EFI_AHCI_PORT_TFD_ERR_INT_CRC))\r
+  {\r
+    return TRUE;\r
+  }\r
+\r
+  return FALSE;\r
+}\r
+\r
 /**\r
   Checks if specified FIS has been received.\r
 \r
@@ -950,6 +1006,7 @@ AhciPioTransfer (
   UINT32                         PrdCount;\r
   UINT32                         Retry;\r
   EFI_STATUS                     RecoveryStatus;\r
+  BOOLEAN                        DoRetry;\r
 \r
   if (Read) {\r
     Flag = EfiPciIoOperationBusMasterWrite;\r
@@ -1027,8 +1084,9 @@ AhciPioTransfer (
 \r
     if (Status == EFI_DEVICE_ERROR) {\r
       DEBUG ((DEBUG_ERROR, "PIO command failed at retry %d\n", Retry));\r
+      DoRetry        = AhciShouldCmdBeRetried (PciIo, Port); // needs to be called before error recovery\r
       RecoveryStatus = AhciRecoverPortError (PciIo, Port);\r
-      if (EFI_ERROR (RecoveryStatus)) {\r
+      if (!DoRetry || EFI_ERROR (RecoveryStatus)) {\r
         break;\r
       }\r
     } else {\r
@@ -1124,6 +1182,7 @@ AhciDmaTransfer (
   EFI_TPL                        OldTpl;\r
   UINT32                         Retry;\r
   EFI_STATUS                     RecoveryStatus;\r
+  BOOLEAN                        DoRetry;\r
 \r
   Map   = NULL;\r
   PciIo = Instance->PciIo;\r
@@ -1222,8 +1281,9 @@ AhciDmaTransfer (
       Status = AhciWaitUntilFisReceived (PciIo, Port, Timeout, SataFisD2H);\r
       if (Status == EFI_DEVICE_ERROR) {\r
         DEBUG ((DEBUG_ERROR, "DMA command failed at retry: %d\n", Retry));\r
+        DoRetry        = AhciShouldCmdBeRetried (PciIo, Port); // needs to be called before error recovery\r
         RecoveryStatus = AhciRecoverPortError (PciIo, Port);\r
-        if (EFI_ERROR (RecoveryStatus)) {\r
+        if (!DoRetry || EFI_ERROR (RecoveryStatus)) {\r
           break;\r
         }\r
       } else {\r
@@ -1263,6 +1323,7 @@ AhciDmaTransfer (
       Status = AhciCheckFisReceived (PciIo, Port, SataFisD2H);\r
       if (Status == EFI_DEVICE_ERROR) {\r
         DEBUG ((DEBUG_ERROR, "DMA command failed at retry: %d\n", Task->RetryTimes));\r
+        DoRetry        = AhciShouldCmdBeRetried (PciIo, Port); // call this before error recovery\r
         RecoveryStatus = AhciRecoverPortError (PciIo, Port);\r
         //\r
         // If recovery passed mark the Task as not started and change the status\r
@@ -1270,7 +1331,7 @@ AhciDmaTransfer (
         // and on next call the command will be re-issued due to IsStart being FALSE.\r
         // This also makes the next condition decrement the RetryTimes.\r
         //\r
-        if (RecoveryStatus == EFI_SUCCESS) {\r
+        if (DoRetry && (RecoveryStatus == EFI_SUCCESS)) {\r
           Task->IsStart = FALSE;\r
           Status        = EFI_NOT_READY;\r
         }\r
@@ -1378,6 +1439,7 @@ AhciNonDataTransfer (
   EFI_AHCI_COMMAND_LIST  CmdList;\r
   UINT32                 Retry;\r
   EFI_STATUS             RecoveryStatus;\r
+  BOOLEAN                DoRetry;\r
 \r
   //\r
   // Package read needed\r
@@ -1418,8 +1480,9 @@ AhciNonDataTransfer (
     Status = AhciWaitUntilFisReceived (PciIo, Port, Timeout, SataFisD2H);\r
     if (Status == EFI_DEVICE_ERROR) {\r
       DEBUG ((DEBUG_ERROR, "Non data transfer failed at retry %d\n", Retry));\r
+      DoRetry        = AhciShouldCmdBeRetried (PciIo, Port); // call this before error recovery\r
       RecoveryStatus = AhciRecoverPortError (PciIo, Port);\r
-      if (EFI_ERROR (RecoveryStatus)) {\r
+      if (!DoRetry || EFI_ERROR (RecoveryStatus)) {\r
         break;\r
       }\r
     } else {\r
index d7434b408ccbfc915c0c5fd1ff8b36d8fc55d0ab..5bb31057ec3c97b9bfa465207f444e11cd2ddfc5 100644 (file)
@@ -146,7 +146,8 @@ typedef union {
 #define   EFI_AHCI_PORT_TFD_BSY           BIT7\r
 #define   EFI_AHCI_PORT_TFD_DRQ           BIT3\r
 #define   EFI_AHCI_PORT_TFD_ERR           BIT0\r
-#define   EFI_AHCI_PORT_TFD_ERR_MASK      0x00FF00\r
+#define   EFI_AHCI_PORT_TFD_ERR_MASK      0x00FF00 // ERROR field is specified by ATA/ATAPI Command Set specification\r
+#define   EFI_AHCI_PORT_TFD_ERR_INT_CRC   BIT15\r
 #define EFI_AHCI_PORT_SIG                 0x0024\r
 #define EFI_AHCI_PORT_SSTS                0x0028\r
 #define   EFI_AHCI_PORT_SSTS_DET_MASK     0x000F\r