]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c
MdeModulePkg/Xhci: Remove TRB when canceling Async Int Transfer
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / XhciDxe / XhciSched.c
index c25342dc1f7845ffa919642bdf8bcc7ef6b94ada..9e34faf2637d8031b275fd93872c856dfc6f8033 100644 (file)
@@ -2,7 +2,7 @@
 \r
   XHCI transfer scheduling 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
@@ -1009,7 +1009,11 @@ IsAsyncIntTrb (
         return TRUE;\r
       }\r
       CheckedTrb++;\r
-      if ((UINTN)CheckedTrb >= ((UINTN) CheckedUrb->Ring->RingSeg0 + sizeof (TRB_TEMPLATE) * CheckedUrb->Ring->TrbNumber)) {\r
+      //\r
+      // If the checked TRB is the link TRB at the end of the transfer ring,\r
+      // recircle it to the head of the ring.\r
+      //\r
+      if (CheckedTrb->Type == TRB_TYPE_LINK) {\r
         CheckedTrb = (TRB_TEMPLATE*) CheckedUrb->Ring->RingSeg0;\r
       }\r
     }\r
@@ -1163,7 +1167,7 @@ XhcCheckUrbResult (
       case TRB_COMPLETION_SHORT_PACKET:\r
       case TRB_COMPLETION_SUCCESS:\r
         if (EvtTrb->Completecode == TRB_COMPLETION_SHORT_PACKET) {\r
-          DEBUG ((EFI_D_ERROR, "XhcCheckUrbResult: short packet happens!\n"));\r
+          DEBUG ((EFI_D_VERBOSE, "XhcCheckUrbResult: short packet happens!\n"));\r
         }\r
 \r
         TRBType = (UINT8) (TRBPtr->Type);\r
@@ -1315,6 +1319,7 @@ XhciDelAsyncIntTransfer (
   LIST_ENTRY              *Next;\r
   URB                     *Urb;\r
   EFI_USB_DATA_DIRECTION  Direction;\r
+  EFI_STATUS              Status;\r
 \r
   Direction = ((EpNum & 0x80) != 0) ? EfiUsbDataIn : EfiUsbDataOut;\r
   EpNum    &= 0x0F;\r
@@ -1326,6 +1331,15 @@ XhciDelAsyncIntTransfer (
     if ((Urb->Ep.BusAddr == BusAddr) &&\r
         (Urb->Ep.EpAddr == EpNum) &&\r
         (Urb->Ep.Direction == Direction)) {\r
+      //\r
+      // Device doesn't finish the IntTransfer until real data comes\r
+      // So the TRB should be removed as well.\r
+      //\r
+      Status = XhcDequeueTrbFromEndpoint (Xhc, Urb);\r
+      if (EFI_ERROR (Status)) {\r
+        DEBUG ((EFI_D_ERROR, "XhciDelAsyncIntTransfer: XhcDequeueTrbFromEndpoint failed\n"));\r
+      }\r
+\r
       RemoveEntryList (&Urb->UrbList);\r
       FreePool (Urb->Data);\r
       XhcFreeUrb (Xhc, Urb);\r
@@ -1350,9 +1364,20 @@ XhciDelAllAsyncIntTransfers (
   LIST_ENTRY              *Entry;\r
   LIST_ENTRY              *Next;\r
   URB                     *Urb;\r
+  EFI_STATUS              Status;\r
 \r
   EFI_LIST_FOR_EACH_SAFE (Entry, Next, &Xhc->AsyncIntTransfers) {\r
     Urb = EFI_LIST_CONTAINER (Entry, URB, UrbList);\r
+\r
+      //\r
+      // Device doesn't finish the IntTransfer until real data comes\r
+      // So the TRB should be removed as well.\r
+      //\r
+    Status = XhcDequeueTrbFromEndpoint (Xhc, Urb);\r
+    if (EFI_ERROR (Status)) {\r
+      DEBUG ((EFI_D_ERROR, "XhciDelAllAsyncIntTransfers: XhcDequeueTrbFromEndpoint failed\n"));\r
+    }\r
+\r
     RemoveEntryList (&Urb->UrbList);\r
     FreePool (Urb->Data);\r
     XhcFreeUrb (Xhc, Urb);\r
@@ -2111,6 +2136,10 @@ XhcInitializeDeviceSlot (
   // 8) Issue an Address Device Command for the Device Slot, where the command points to the Input\r
   //    Context data structure described above.\r
   //\r
+  // Delay 10ms to meet TRSTRCY delay requirement in usb 2.0 spec chapter 7.1.7.5 before sending SetAddress() request\r
+  // to device.\r
+  //\r
+  gBS->Stall (XHC_RESET_RECOVERY_DELAY);\r
   ZeroMem (&CmdTrbAddr, sizeof (CmdTrbAddr));\r
   PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, Xhc->UsbDevContext[SlotId].InputContext, sizeof (INPUT_CONTEXT));\r
   CmdTrbAddr.PtrLo    = XHC_LOW_32BIT (PhyAddr);\r
@@ -2317,6 +2346,10 @@ XhcInitializeDeviceSlot64 (
   // 8) Issue an Address Device Command for the Device Slot, where the command points to the Input\r
   //    Context data structure described above.\r
   //\r
+  // Delay 10ms to meet TRSTRCY delay requirement in usb 2.0 spec chapter 7.1.7.5 before sending SetAddress() request\r
+  // to device.\r
+  //\r
+  gBS->Stall (XHC_RESET_RECOVERY_DELAY);\r
   ZeroMem (&CmdTrbAddr, sizeof (CmdTrbAddr));\r
   PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, Xhc->UsbDevContext[SlotId].InputContext, sizeof (INPUT_CONTEXT_64));\r
   CmdTrbAddr.PtrLo    = XHC_LOW_32BIT (PhyAddr);\r
@@ -2596,6 +2629,11 @@ XhcInitializeEndpointContext (
       EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
     }\r
 \r
+    if (EpDesc->Length < sizeof (USB_ENDPOINT_DESCRIPTOR)) {\r
+      EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
+      continue;\r
+    }\r
+\r
     EpAddr    = (UINT8)(EpDesc->EndpointAddress & 0x0F);\r
     Direction = (UINT8)((EpDesc->EndpointAddress & 0x80) ? EfiUsbDataIn : EfiUsbDataOut);\r
 \r
@@ -2643,6 +2681,20 @@ XhcInitializeEndpointContext (
           InputContext->EP[Dci-1].CErr   = 0;\r
           InputContext->EP[Dci-1].EPType = ED_ISOCH_OUT;\r
         }\r
+        //\r
+        // Get the bInterval from descriptor and init the the interval field of endpoint context.\r
+        // Refer to XHCI 1.1 spec section 6.2.3.6.\r
+        //\r
+        if (DeviceSpeed == EFI_USB_SPEED_FULL) {\r
+          Interval = EpDesc->Interval;\r
+          ASSERT (Interval >= 1 && Interval <= 16);\r
+          InputContext->EP[Dci-1].Interval = Interval + 2;\r
+        } else if ((DeviceSpeed == EFI_USB_SPEED_HIGH) || (DeviceSpeed == EFI_USB_SPEED_SUPER)) {\r
+          Interval = EpDesc->Interval;\r
+          ASSERT (Interval >= 1 && Interval <= 16);\r
+          InputContext->EP[Dci-1].Interval = Interval - 1;\r
+        }\r
+\r
         //\r
         // Do not support isochronous transfer now.\r
         //\r
@@ -2759,6 +2811,11 @@ XhcInitializeEndpointContext64 (
       EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
     }\r
 \r
+    if (EpDesc->Length < sizeof (USB_ENDPOINT_DESCRIPTOR)) {\r
+      EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
+      continue;\r
+    }\r
+\r
     EpAddr    = (UINT8)(EpDesc->EndpointAddress & 0x0F);\r
     Direction = (UINT8)((EpDesc->EndpointAddress & 0x80) ? EfiUsbDataIn : EfiUsbDataOut);\r
 \r
@@ -2806,6 +2863,20 @@ XhcInitializeEndpointContext64 (
           InputContext->EP[Dci-1].CErr   = 0;\r
           InputContext->EP[Dci-1].EPType = ED_ISOCH_OUT;\r
         }\r
+        //\r
+        // Get the bInterval from descriptor and init the the interval field of endpoint context.\r
+        // Refer to XHCI 1.1 spec section 6.2.3.6.\r
+        //\r
+        if (DeviceSpeed == EFI_USB_SPEED_FULL) {\r
+          Interval = EpDesc->Interval;\r
+          ASSERT (Interval >= 1 && Interval <= 16);\r
+          InputContext->EP[Dci-1].Interval = Interval + 2;\r
+        } else if ((DeviceSpeed == EFI_USB_SPEED_HIGH) || (DeviceSpeed == EFI_USB_SPEED_SUPER)) {\r
+          Interval = EpDesc->Interval;\r
+          ASSERT (Interval >= 1 && Interval <= 16);\r
+          InputContext->EP[Dci-1].Interval = Interval - 1;\r
+        }\r
+\r
         //\r
         // Do not support isochronous transfer now.\r
         //\r
@@ -2928,6 +2999,11 @@ XhcSetConfigCmd (
       IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length);\r
     }\r
 \r
+    if (IfDesc->Length < sizeof (USB_INTERFACE_DESCRIPTOR)) {\r
+      IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length);\r
+      continue;\r
+    }\r
+\r
     Dci = XhcInitializeEndpointContext (Xhc, SlotId, DeviceSpeed, InputContext, IfDesc);\r
     if (Dci > MaxDci) {\r
       MaxDci = Dci;\r
@@ -3013,6 +3089,11 @@ XhcSetConfigCmd64 (
       IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length);\r
     }\r
 \r
+    if (IfDesc->Length < sizeof (USB_INTERFACE_DESCRIPTOR)) {\r
+      IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length);\r
+      continue;\r
+    }\r
+\r
     Dci = XhcInitializeEndpointContext64 (Xhc, SlotId, DeviceSpeed, InputContext, IfDesc);\r
     if (Dci > MaxDci) {\r
       MaxDci = Dci;\r
@@ -3261,7 +3342,7 @@ XhcSetInterface (
 \r
   IfDesc = (USB_INTERFACE_DESCRIPTOR *)(ConfigDesc + 1);\r
   while ((UINTN) IfDesc < ((UINTN) ConfigDesc + ConfigDesc->TotalLength)) {\r
-    if (IfDesc->DescriptorType == USB_DESC_TYPE_INTERFACE) {\r
+    if ((IfDesc->DescriptorType == USB_DESC_TYPE_INTERFACE) && (IfDesc->Length >= sizeof (USB_INTERFACE_DESCRIPTOR))) {\r
       if (IfDesc->InterfaceNumber == (UINT8) Request->Index) {\r
         if (IfDesc->AlternateSetting == Xhc->UsbDevContext[SlotId].ActiveAlternateSetting[IfDesc->InterfaceNumber]) {\r
           //\r
@@ -3301,6 +3382,11 @@ XhcSetInterface (
         EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
       }\r
 \r
+      if (EpDesc->Length < sizeof (USB_ENDPOINT_DESCRIPTOR)) {\r
+        EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
+        continue;\r
+      }\r
+\r
       EpAddr    = (UINT8) (EpDesc->EndpointAddress & 0x0F);\r
       Direction = (UINT8) ((EpDesc->EndpointAddress & 0x80) ? EfiUsbDataIn : EfiUsbDataOut);\r
 \r
@@ -3458,7 +3544,7 @@ XhcSetInterface64 (
 \r
   IfDesc = (USB_INTERFACE_DESCRIPTOR *)(ConfigDesc + 1);\r
   while ((UINTN) IfDesc < ((UINTN) ConfigDesc + ConfigDesc->TotalLength)) {\r
-    if (IfDesc->DescriptorType == USB_DESC_TYPE_INTERFACE) {\r
+    if ((IfDesc->DescriptorType == USB_DESC_TYPE_INTERFACE) && (IfDesc->Length >= sizeof (USB_INTERFACE_DESCRIPTOR))) {\r
       if (IfDesc->InterfaceNumber == (UINT8) Request->Index) {\r
         if (IfDesc->AlternateSetting == Xhc->UsbDevContext[SlotId].ActiveAlternateSetting[IfDesc->InterfaceNumber]) {\r
           //\r
@@ -3498,6 +3584,11 @@ XhcSetInterface64 (
         EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
       }\r
 \r
+      if (EpDesc->Length < sizeof (USB_ENDPOINT_DESCRIPTOR)) {\r
+        EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);\r
+        continue;\r
+      }\r
+\r
       EpAddr    = (UINT8) (EpDesc->EndpointAddress & 0x0F);\r
       Direction = (UINT8) ((EpDesc->EndpointAddress & 0x80) ? EfiUsbDataIn : EfiUsbDataOut);\r
 \r