]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg/XhciDxe: Check timeout URB again after stopping endpoint
authorRuiyu Ni <ruiyu.ni@intel.com>
Mon, 3 Jul 2017 09:53:49 +0000 (17:53 +0800)
committerRuiyu Ni <ruiyu.ni@intel.com>
Wed, 5 Jul 2017 03:31:06 +0000 (11:31 +0800)
This fixes BULK data loss when transfer is detected as timeout but
finished just before stopping endpoint.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ruiyu Ni <ruiyu.ni@intel.com>
Reviewed-by: Hao Wu <hao.a.wu@intel.com>
Cc: Star Zeng <star.zeng@intel.com>
Cc: Feng Tian <feng.tian@intel.com>
MdeModulePkg/Bus/Pci/XhciDxe/Xhci.c
MdeModulePkg/Bus/Pci/XhciDxe/Xhci.h
MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c
MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.h

index e3cbdbc800387670d060ada28fe2f0b755c876b4..20ad3f1611979ff187abb7fb47c0ed800be32fa8 100644 (file)
@@ -780,26 +780,32 @@ XhcTransfer (
 \r
   Status = XhcExecTransfer (Xhc, FALSE, Urb, Timeout);\r
 \r
 \r
   Status = XhcExecTransfer (Xhc, FALSE, Urb, Timeout);\r
 \r
-  *TransferResult = Urb->Result;\r
-  *DataLength     = Urb->Completed;\r
-\r
   if (Status == EFI_TIMEOUT) {\r
     //\r
     // The transfer timed out. Abort the transfer by dequeueing of the TD.\r
     //\r
   if (Status == EFI_TIMEOUT) {\r
     //\r
     // The transfer timed out. Abort the transfer by dequeueing of the TD.\r
     //\r
-    RecoveryStatus = XhcDequeueTrbFromEndpoint (Xhc, Urb);\r
-    if (EFI_ERROR (RecoveryStatus)) {\r
-      DEBUG((DEBUG_ERROR, "XhcTransfer[Type=%d]: XhcDequeueTrbFromEndpoint failed\n", Type));\r
-    }\r
-  } else {\r
-    if (*TransferResult == EFI_USB_NOERROR) {\r
+    RecoveryStatus = XhcDequeueTrbFromEndpoint(Xhc, Urb);\r
+    if (RecoveryStatus == EFI_ALREADY_STARTED) {\r
+      //\r
+      // The URB is finished just before stopping endpoint.\r
+      // Change returning status from EFI_TIMEOUT to EFI_SUCCESS.\r
+      //\r
+      ASSERT (Urb->Result == EFI_USB_NOERROR);\r
       Status = EFI_SUCCESS;\r
       Status = EFI_SUCCESS;\r
-    } else if (*TransferResult == EFI_USB_ERR_STALL) {\r
-      RecoveryStatus = XhcRecoverHaltedEndpoint (Xhc, Urb);\r
-      if (EFI_ERROR (RecoveryStatus)) {\r
-        DEBUG ((DEBUG_ERROR, "XhcTransfer[Type=%d]: XhcRecoverHaltedEndpoint failed\n", Type));\r
-      }\r
-      Status = EFI_DEVICE_ERROR;\r
+      DEBUG ((DEBUG_ERROR, "XhcTransfer[Type=%d]: pending URB is finished, Length = %d.\n", Type, Urb->Completed));\r
+    } else if (EFI_ERROR(RecoveryStatus)) {\r
+      DEBUG((DEBUG_ERROR, "XhcTransfer[Type=%d]: XhcDequeueTrbFromEndpoint failed!\n", Type));\r
+    }\r
+  }\r
+\r
+  *TransferResult = Urb->Result;\r
+  *DataLength     = Urb->Completed;\r
+\r
+  if (*TransferResult == EFI_USB_ERR_STALL) {\r
+    ASSERT (Status == EFI_DEVICE_ERROR);\r
+    RecoveryStatus = XhcRecoverHaltedEndpoint(Xhc, Urb);\r
+    if (EFI_ERROR (RecoveryStatus)) {\r
+      DEBUG ((DEBUG_ERROR, "XhcTransfer[Type=%d]: XhcRecoverHaltedEndpoint failed!\n", Type));\r
     }\r
   }\r
 \r
     }\r
   }\r
 \r
index 28e240245b907ebd4f94970fe7a36f0262c7b501..76daaff4a4e57668bcd048b6cb1f9917a2f041e6 100644 (file)
@@ -2,7 +2,7 @@
 \r
   Provides some data structure definitions used by the XHCI host controller driver.\r
 \r
 \r
   Provides some data structure definitions used by the XHCI host controller driver.\r
 \r
-Copyright (c) 2011 - 2016, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.<BR>\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
 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
@@ -243,6 +243,7 @@ struct _USB_XHCI_INSTANCE {
   UINT64                    *DCBAA;\r
   VOID                      *DCBAAMap;\r
   UINT32                    MaxSlotsEn;\r
   UINT64                    *DCBAA;\r
   VOID                      *DCBAAMap;\r
   UINT32                    MaxSlotsEn;\r
+  URB                       *PendingUrb;\r
   //\r
   // Cmd Transfer Ring\r
   //\r
   //\r
   // Cmd Transfer Ring\r
   //\r
index f7f3409686b8c9d13484b0f2cbb571475b63809b..f8174d8d03306429e4b95ac7d949004b2a3ea34a 100644 (file)
@@ -696,6 +696,7 @@ Done:
   @param  Urb                   The urb which doesn't get completed in a specified timeout range.\r
 \r
   @retval EFI_SUCCESS           The dequeuing of the TDs is successful.\r
   @param  Urb                   The urb which doesn't get completed in a specified timeout range.\r
 \r
   @retval EFI_SUCCESS           The dequeuing of the TDs is successful.\r
+  @retval EFI_ALREADY_STARTED   The Urb is finished so no deque is needed.\r
   @retval Others                Failed to stop the endpoint and dequeue the TDs.\r
 \r
 **/\r
   @retval Others                Failed to stop the endpoint and dequeue the TDs.\r
 \r
 **/\r
@@ -723,7 +724,7 @@ XhcDequeueTrbFromEndpoint (
   //\r
   // 1) Send Stop endpoint command to stop xHC from executing of the TDs on the endpoint\r
   //\r
   //\r
   // 1) Send Stop endpoint command to stop xHC from executing of the TDs on the endpoint\r
   //\r
-  Status = XhcStopEndpoint(Xhc, SlotId, Dci);\r
+  Status = XhcStopEndpoint(Xhc, SlotId, Dci, Urb);\r
   if (EFI_ERROR(Status)) {\r
     DEBUG ((EFI_D_ERROR, "XhcDequeueTrbFromEndpoint: Stop Endpoint Failed, Status = %r\n", Status));\r
     goto Done;\r
   if (EFI_ERROR(Status)) {\r
     DEBUG ((EFI_D_ERROR, "XhcDequeueTrbFromEndpoint: Stop Endpoint Failed, Status = %r\n", Status));\r
     goto Done;\r
@@ -732,10 +733,20 @@ XhcDequeueTrbFromEndpoint (
   //\r
   // 2)Set dequeue pointer\r
   //\r
   //\r
   // 2)Set dequeue pointer\r
   //\r
-  Status = XhcSetTrDequeuePointer(Xhc, SlotId, Dci, Urb);\r
-  if (EFI_ERROR(Status)) {\r
-    DEBUG ((EFI_D_ERROR, "XhcDequeueTrbFromEndpoint: Set Transfer Ring Dequeue Pointer Failed, Status = %r\n", Status));\r
-    goto Done;\r
+  if (Urb->Finished && Urb->Result == EFI_USB_NOERROR) {\r
+    //\r
+    // Return Already Started to indicate the pending URB is finished.\r
+    // This fixes BULK data loss when transfer is detected as timeout\r
+    // but finished just before stopping endpoint.\r
+    //\r
+    Status = EFI_ALREADY_STARTED;\r
+    DEBUG ((DEBUG_INFO, "XhcDequeueTrbFromEndpoint: Pending URB is finished: Length Actual/Expect = %d/%d!\n", Urb->Completed, Urb->DataLen));\r
+  } else {\r
+    Status = XhcSetTrDequeuePointer(Xhc, SlotId, Dci, Urb);\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((DEBUG_ERROR, "XhcDequeueTrbFromEndpoint: Set Transfer Ring Dequeue Pointer Failed, Status = %r\n", Status));\r
+      goto Done;\r
+    }\r
   }\r
 \r
   //\r
   }\r
 \r
   //\r
@@ -1125,12 +1136,14 @@ XhcCheckUrbResult (
     TRBPtr = (TRB_TEMPLATE *)(UINTN) UsbHcGetHostAddrForPciAddr (Xhc->MemPool, (VOID *)(UINTN) PhyAddr, sizeof (TRB_TEMPLATE));\r
 \r
     //\r
     TRBPtr = (TRB_TEMPLATE *)(UINTN) UsbHcGetHostAddrForPciAddr (Xhc->MemPool, (VOID *)(UINTN) PhyAddr, sizeof (TRB_TEMPLATE));\r
 \r
     //\r
-    // Update the status of Urb according to the finished event regardless of whether\r
-    // the urb is current checked one or in the XHCI's async transfer list.\r
+    // Update the status of URB including the pending URB, the URB that is currently checked,\r
+    // and URBs in the XHCI's async interrupt transfer list.\r
     // This way is used to avoid that those completed async transfer events don't get\r
     // handled in time and are flushed by newer coming events.\r
     //\r
     // This way is used to avoid that those completed async transfer events don't get\r
     // handled in time and are flushed by newer coming events.\r
     //\r
-    if (IsTransferRingTrb (Xhc, TRBPtr, Urb)) {\r
+    if (Xhc->PendingUrb != NULL && IsTransferRingTrb (Xhc, TRBPtr, Xhc->PendingUrb)) {\r
+      CheckedUrb = Xhc->PendingUrb;\r
+    } else if (IsTransferRingTrb (Xhc, TRBPtr, Urb)) {\r
       CheckedUrb = Urb;\r
     } else if (IsAsyncIntTrb (Xhc, TRBPtr, &AsyncUrb)) {    \r
       CheckedUrb = AsyncUrb;\r
       CheckedUrb = Urb;\r
     } else if (IsAsyncIntTrb (Xhc, TRBPtr, &AsyncUrb)) {    \r
       CheckedUrb = AsyncUrb;\r
@@ -1163,6 +1176,16 @@ XhcCheckUrbResult (
         DEBUG ((EFI_D_ERROR, "XhcCheckUrbResult: TRANSACTION_ERROR! Completecode = %x\n",EvtTrb->Completecode));\r
         goto EXIT;\r
 \r
         DEBUG ((EFI_D_ERROR, "XhcCheckUrbResult: TRANSACTION_ERROR! Completecode = %x\n",EvtTrb->Completecode));\r
         goto EXIT;\r
 \r
+      case TRB_COMPLETION_STOPPED:\r
+      case TRB_COMPLETION_STOPPED_LENGTH_INVALID:\r
+        CheckedUrb->Result  |= EFI_USB_ERR_TIMEOUT;\r
+        CheckedUrb->Finished = TRUE;\r
+        //\r
+        // The pending URB is timeout and force stopped when stopping endpoint.\r
+        // Continue the loop to receive the Command Complete Event for stopping endpoint.\r
+        //\r
+        continue;\r
+\r
       case TRB_COMPLETION_SHORT_PACKET:\r
       case TRB_COMPLETION_SUCCESS:\r
         if (EvtTrb->Completecode == TRB_COMPLETION_SHORT_PACKET) {\r
       case TRB_COMPLETION_SHORT_PACKET:\r
       case TRB_COMPLETION_SUCCESS:\r
         if (EvtTrb->Completecode == TRB_COMPLETION_SHORT_PACKET) {\r
@@ -3155,6 +3178,7 @@ XhcSetConfigCmd64 (
   @param  Xhc                   The XHCI Instance.\r
   @param  SlotId                The slot id to be configured.\r
   @param  Dci                   The device context index of endpoint.\r
   @param  Xhc                   The XHCI Instance.\r
   @param  SlotId                The slot id to be configured.\r
   @param  Dci                   The device context index of endpoint.\r
+  @param  PendingUrb            The pending URB to check completion status when stopping the end point.\r
 \r
   @retval EFI_SUCCESS           Stop endpoint successfully.\r
   @retval Others                Failed to stop endpoint.\r
 \r
   @retval EFI_SUCCESS           Stop endpoint successfully.\r
   @retval Others                Failed to stop endpoint.\r
@@ -3165,7 +3189,8 @@ EFIAPI
 XhcStopEndpoint (\r
   IN USB_XHCI_INSTANCE      *Xhc,\r
   IN UINT8                  SlotId,\r
 XhcStopEndpoint (\r
   IN USB_XHCI_INSTANCE      *Xhc,\r
   IN UINT8                  SlotId,\r
-  IN UINT8                  Dci\r
+  IN UINT8                  Dci,\r
+  IN URB                    *PendingUrb  OPTIONAL\r
   )\r
 {\r
   EFI_STATUS                    Status;\r
   )\r
 {\r
   EFI_STATUS                    Status;\r
@@ -3174,6 +3199,29 @@ XhcStopEndpoint (
 \r
   DEBUG ((EFI_D_INFO, "XhcStopEndpoint: Slot = 0x%x, Dci = 0x%x\n", SlotId, Dci));\r
 \r
 \r
   DEBUG ((EFI_D_INFO, "XhcStopEndpoint: Slot = 0x%x, Dci = 0x%x\n", SlotId, Dci));\r
 \r
+  //\r
+  // When XhcCheckUrbResult waits for the Stop_Endpoint completion, it also checks\r
+  // the PendingUrb completion status, because it's possible that the PendingUrb is\r
+  // finished just before stopping the end point, but after the looping check.\r
+  //\r
+  // The PendingUrb could be passed to XhcCmdTransfer to XhcExecTransfer to XhcCheckUrbResult\r
+  // through function parameter, but That will cause every consumer of XhcCmdTransfer,\r
+  // XhcExecTransfer and XhcCheckUrbResult pass a NULL PendingUrb.\r
+  // But actually only XhcCheckUrbResult is aware of the PendingUrb.\r
+  // So we choose to save the PendingUrb into the USB_XHCI_INSTANCE and use it in XhcCheckUrbResult.\r
+  //\r
+  ASSERT (Xhc->PendingUrb == NULL);\r
+  Xhc->PendingUrb = PendingUrb;\r
+  //\r
+  // Reset the URB result from Timeout to NoError.\r
+  // The USB result will be:\r
+  //   changed to Timeout when Stop/StopInvalidLength Transfer Event is received, or\r
+  //   remain NoError when Success/ShortPacket Transfer Event is received.\r
+  //\r
+  if (PendingUrb != NULL) {\r
+    PendingUrb->Result = EFI_USB_NOERROR;\r
+  }\r
+\r
   //\r
   // Send stop endpoint command to transit Endpoint from running to stop state\r
   //\r
   //\r
   // Send stop endpoint command to transit Endpoint from running to stop state\r
   //\r
@@ -3192,6 +3240,8 @@ XhcStopEndpoint (
     DEBUG ((EFI_D_ERROR, "XhcStopEndpoint: Stop Endpoint Failed, Status = %r\n", Status));\r
   }\r
 \r
     DEBUG ((EFI_D_ERROR, "XhcStopEndpoint: Stop Endpoint Failed, Status = %r\n", Status));\r
   }\r
 \r
+  Xhc->PendingUrb = NULL;\r
+\r
   return Status;\r
 }\r
 \r
   return Status;\r
 }\r
 \r
@@ -3418,7 +3468,7 @@ XhcSetInterface (
       // XHCI 4.3.6 - Setting Alternate Interfaces\r
       // 1) Stop any Running Transfer Rings affected by the Alternate Interface setting.\r
       //\r
       // XHCI 4.3.6 - Setting Alternate Interfaces\r
       // 1) Stop any Running Transfer Rings affected by the Alternate Interface setting.\r
       //\r
-      Status = XhcStopEndpoint (Xhc, SlotId, Dci);\r
+      Status = XhcStopEndpoint (Xhc, SlotId, Dci, NULL);\r
       if (EFI_ERROR (Status)) {\r
         return Status;\r
       }\r
       if (EFI_ERROR (Status)) {\r
         return Status;\r
       }\r
@@ -3620,7 +3670,7 @@ XhcSetInterface64 (
       // XHCI 4.3.6 - Setting Alternate Interfaces\r
       // 1) Stop any Running Transfer Rings affected by the Alternate Interface setting.\r
       //\r
       // XHCI 4.3.6 - Setting Alternate Interfaces\r
       // 1) Stop any Running Transfer Rings affected by the Alternate Interface setting.\r
       //\r
-      Status = XhcStopEndpoint (Xhc, SlotId, Dci);\r
+      Status = XhcStopEndpoint (Xhc, SlotId, Dci, NULL);\r
       if (EFI_ERROR (Status)) {\r
         return Status;\r
       }\r
       if (EFI_ERROR (Status)) {\r
         return Status;\r
       }\r
index 931c7efa0c283160f1168788e0b3a569945beb19..f40f94c1e4b711acf47b37adb4f4964cb0c129ac 100644 (file)
@@ -2,7 +2,7 @@
 \r
   This file contains the definition for XHCI host controller schedule routines.\r
 \r
 \r
   This file contains the definition for XHCI host controller schedule routines.\r
 \r
-Copyright (c) 2011 - 2015, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2011 - 2017, Intel Corporation. All rights reserved.<BR>\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
 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
@@ -80,6 +80,8 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #define TRB_COMPLETION_TRB_ERROR              5\r
 #define TRB_COMPLETION_STALL_ERROR            6\r
 #define TRB_COMPLETION_SHORT_PACKET           13\r
 #define TRB_COMPLETION_TRB_ERROR              5\r
 #define TRB_COMPLETION_STALL_ERROR            6\r
 #define TRB_COMPLETION_SHORT_PACKET           13\r
+#define TRB_COMPLETION_STOPPED                26\r
+#define TRB_COMPLETION_STOPPED_LENGTH_INVALID 27\r
 \r
 //\r
 // The topology string used to present usb device location\r
 \r
 //\r
 // The topology string used to present usb device location\r
@@ -1343,6 +1345,7 @@ XhcDequeueTrbFromEndpoint (
   @param  Xhc                   The XHCI Instance.\r
   @param  SlotId                The slot id to be configured.\r
   @param  Dci                   The device context index of endpoint.\r
   @param  Xhc                   The XHCI Instance.\r
   @param  SlotId                The slot id to be configured.\r
   @param  Dci                   The device context index of endpoint.\r
+  @param  PendingUrb            The pending URB to check completion status when stopping the end point.\r
 \r
   @retval EFI_SUCCESS           Stop endpoint successfully.\r
   @retval Others                Failed to stop endpoint.\r
 \r
   @retval EFI_SUCCESS           Stop endpoint successfully.\r
   @retval Others                Failed to stop endpoint.\r
@@ -1353,7 +1356,8 @@ EFIAPI
 XhcStopEndpoint (\r
   IN USB_XHCI_INSTANCE      *Xhc,\r
   IN UINT8                  SlotId,\r
 XhcStopEndpoint (\r
   IN USB_XHCI_INSTANCE      *Xhc,\r
   IN UINT8                  SlotId,\r
-  IN UINT8                  Dci\r
+  IN UINT8                  Dci,\r
+  IN URB                    *PendingUrb  OPTIONAL\r
   );\r
 \r
 /**\r
   );\r
 \r
 /**\r