VirtualBox

Ignore:
Timestamp:
Oct 28, 2015 8:17:18 PM (9 years ago)
Author:
vboxsync
Message:

EFI/Firmware: 'svn merge /vendor/edk2/UDK2010.SR1 /vendor/edk2/current .', reverting and removing files+dirs listed in ReadMe.vbox, resolving conflicts with help from ../UDK2014.SP1/. This is a raw untested merge.

Location:
trunk/src/VBox/Devices/EFI/Firmware
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/EFI/Firmware

  • trunk/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c

    r48674 r58459  
    33  XHCI transfer scheduling routines.
    44
    5 Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>
     5Copyright (c) 2011 - 2014, Intel Corporation. All rights reserved.<BR>
    66This program and the accompanying materials
    77are licensed and made available under the terms and conditions of the BSD License
     
    110110  }
    111111
    112   FreePool (Urb);
     112  XhcFreeUrb (Xhc, Urb);
    113113
    114114ON_EXIT:
     
    177177  Status = XhcCreateTransferTrb (Xhc, Urb);
    178178  ASSERT_EFI_ERROR (Status);
     179  if (EFI_ERROR (Status)) {
     180    DEBUG ((EFI_D_ERROR, "XhcCreateUrb: XhcCreateTransferTrb Failed, Status = %r\n", Status));
     181    FreePool (Urb);
     182    Urb = NULL;
     183  }
    179184
    180185  return Urb;
     186}
     187
     188/**
     189  Free an allocated URB.
     190
     191  @param  Xhc                   The XHCI device.
     192  @param  Urb                   The URB to free.
     193
     194**/
     195VOID
     196XhcFreeUrb (
     197  IN USB_XHCI_INSTANCE    *Xhc,
     198  IN URB                  *Urb
     199  )
     200{
     201  if ((Xhc == NULL) || (Urb == NULL)) {
     202    return;
     203  }
     204 
     205  if (Urb->DataMap != NULL) {
     206    Xhc->PciIo->Unmap (Xhc->PciIo, Urb->DataMap);
     207  }
     208
     209  FreePool (Urb);
    181210}
    182211
     
    205234  UINTN                         Len;
    206235  UINTN                         TrbNum;
     236  EFI_PCI_IO_PROTOCOL_OPERATION MapOp;
     237  EFI_PHYSICAL_ADDRESS          PhyAddr;
     238  VOID                          *Map;
     239  EFI_STATUS                    Status;
    207240
    208241  SlotId = XhcBusDevAddrToSlotId (Xhc, Urb->Ep.BusAddr);
     
    221254  EPRing    = (TRANSFER_RING *)(UINTN) Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1];
    222255  Urb->Ring = EPRing;
    223   OutputContext = (VOID *)(UINTN)Xhc->DCBAA[SlotId];
     256  OutputContext = Xhc->UsbDevContext[SlotId].OutputContext;
    224257  if (Xhc->HcCParams.Data.Csz == 0) {
    225258    EPType  = (UINT8) ((DEVICE_CONTEXT *)OutputContext)->EP[Dci-1].EPType;
    226259  } else {
    227260    EPType  = (UINT8) ((DEVICE_CONTEXT_64 *)OutputContext)->EP[Dci-1].EPType;
     261  }
     262 
     263  if (Urb->Data != NULL) {
     264    if (((UINT8) (Urb->Ep.Direction)) == EfiUsbDataIn) {
     265      MapOp = EfiPciIoOperationBusMasterWrite;
     266    } else {
     267      MapOp = EfiPciIoOperationBusMasterRead;
     268    }
     269   
     270    Len = Urb->DataLen;
     271    Status  = Xhc->PciIo->Map (Xhc->PciIo, MapOp, Urb->Data, &Len, &PhyAddr, &Map);
     272   
     273    if (EFI_ERROR (Status) || (Len != Urb->DataLen)) {
     274      DEBUG ((EFI_D_ERROR, "XhcCreateTransferTrb: Fail to map Urb->Data.\n"));
     275      return EFI_OUT_OF_RESOURCES;
     276    }
     277   
     278    Urb->DataPhy  = (VOID *) ((UINTN) PhyAddr);
     279    Urb->DataMap  = Map;
    228280  }
    229281
     
    268320        XhcSyncTrsRing (Xhc, EPRing);
    269321        TrbStart = (TRB *)(UINTN)EPRing->RingEnqueue;
    270         TrbStart->TrbCtrData.TRBPtrLo  = XHC_LOW_32BIT(Urb->Data);
    271         TrbStart->TrbCtrData.TRBPtrHi  = XHC_HIGH_32BIT(Urb->Data);
     322        TrbStart->TrbCtrData.TRBPtrLo  = XHC_LOW_32BIT(Urb->DataPhy);
     323        TrbStart->TrbCtrData.TRBPtrHi  = XHC_HIGH_32BIT(Urb->DataPhy);
    272324        TrbStart->TrbCtrData.Lenth     = (UINT32) Urb->DataLen;
    273325        TrbStart->TrbCtrData.TDSize    = 0;
     
    334386        }
    335387        TrbStart = (TRB *)(UINTN)EPRing->RingEnqueue;
    336         TrbStart->TrbNormal.TRBPtrLo  = XHC_LOW_32BIT((UINT8 *) Urb->Data + TotalLen);
    337         TrbStart->TrbNormal.TRBPtrHi  = XHC_HIGH_32BIT((UINT8 *) Urb->Data + TotalLen);
     388        TrbStart->TrbNormal.TRBPtrLo  = XHC_LOW_32BIT((UINT8 *) Urb->DataPhy + TotalLen);
     389        TrbStart->TrbNormal.TRBPtrHi  = XHC_HIGH_32BIT((UINT8 *) Urb->DataPhy + TotalLen);
    338390        TrbStart->TrbNormal.Lenth     = (UINT32) Len;
    339391        TrbStart->TrbNormal.TDSize    = 0;
     
    369421        }
    370422        TrbStart = (TRB *)(UINTN)EPRing->RingEnqueue;
    371         TrbStart->TrbNormal.TRBPtrLo  = XHC_LOW_32BIT((UINT8 *) Urb->Data + TotalLen);
    372         TrbStart->TrbNormal.TRBPtrHi  = XHC_HIGH_32BIT((UINT8 *) Urb->Data + TotalLen);
     423        TrbStart->TrbNormal.TRBPtrLo  = XHC_LOW_32BIT((UINT8 *) Urb->DataPhy + TotalLen);
     424        TrbStart->TrbNormal.TRBPtrHi  = XHC_HIGH_32BIT((UINT8 *) Urb->DataPhy + TotalLen);
    373425        TrbStart->TrbNormal.Lenth     = (UINT32) Len;
    374426        TrbStart->TrbNormal.TDSize    = 0;
     
    413465{
    414466  VOID                  *Dcbaa;
     467  EFI_PHYSICAL_ADDRESS  DcbaaPhy;
    415468  UINT64                CmdRing;
     469  EFI_PHYSICAL_ADDRESS  CmdRingPhy;
    416470  UINTN                 Entries;
    417471  UINT32                MaxScratchpadBufs;
    418472  UINT64                *ScratchBuf;
    419   UINT64                *ScratchEntryBuf;
     473  EFI_PHYSICAL_ADDRESS  ScratchPhy;
     474  UINT64                *ScratchEntry;
     475  EFI_PHYSICAL_ADDRESS  ScratchEntryPhy;
    420476  UINT32                Index;
     477  UINTN                 *ScratchEntryMap;
     478  EFI_STATUS            Status;
     479
     480  //
     481  // Initialize memory management.
     482  //
     483  Xhc->MemPool = UsbHcInitMemPool (Xhc->PciIo);
     484  ASSERT (Xhc->MemPool != NULL);
    421485
    422486  //
     
    435499  //
    436500  Entries = (Xhc->MaxSlotsEn + 1) * sizeof(UINT64);
    437   Dcbaa   = AllocatePages (EFI_SIZE_TO_PAGES (Entries));
     501  Dcbaa = UsbHcAllocateMem (Xhc->MemPool, Entries);
    438502  ASSERT (Dcbaa != NULL);
    439503  ZeroMem (Dcbaa, Entries);
     
    448512  ASSERT (MaxScratchpadBufs <= 1023);
    449513  if (MaxScratchpadBufs != 0) {
    450     ScratchBuf = AllocateAlignedPages (EFI_SIZE_TO_PAGES (MaxScratchpadBufs * sizeof (UINT64)), Xhc->PageSize);
    451     ASSERT (ScratchBuf != NULL);
     514    //
     515    // Allocate the buffer to record the Mapping for each scratch buffer in order to Unmap them
     516    //
     517    ScratchEntryMap = AllocateZeroPool (sizeof (UINTN) * MaxScratchpadBufs);
     518    ASSERT (ScratchEntryMap != NULL);
     519    Xhc->ScratchEntryMap = ScratchEntryMap;
     520   
     521    //
     522    // Allocate the buffer to record the host address for each entry
     523    //
     524    ScratchEntry = AllocateZeroPool (sizeof (UINT64) * MaxScratchpadBufs);
     525    ASSERT (ScratchEntry != NULL);
     526    Xhc->ScratchEntry = ScratchEntry;
     527
     528    ScratchPhy = 0;
     529    Status = UsbHcAllocateAlignedPages (
     530               Xhc->PciIo,
     531               EFI_SIZE_TO_PAGES (MaxScratchpadBufs * sizeof (UINT64)),
     532               Xhc->PageSize,
     533               (VOID **) &ScratchBuf,
     534               &ScratchPhy,
     535               &Xhc->ScratchMap
     536               );
     537    ASSERT_EFI_ERROR (Status);
     538
    452539    ZeroMem (ScratchBuf, MaxScratchpadBufs * sizeof (UINT64));
    453540    Xhc->ScratchBuf = ScratchBuf;
    454541
     542    //
     543    // Allocate each scratch buffer
     544    //
    455545    for (Index = 0; Index < MaxScratchpadBufs; Index++) {
    456       ScratchEntryBuf = AllocateAlignedPages (EFI_SIZE_TO_PAGES (Xhc->PageSize), Xhc->PageSize);
    457       ASSERT (ScratchEntryBuf != NULL);
    458       ZeroMem (ScratchEntryBuf, Xhc->PageSize);
    459       *ScratchBuf++   = (UINT64)(UINTN)ScratchEntryBuf;
    460     }
    461 
     546      ScratchEntryPhy = 0;
     547      Status = UsbHcAllocateAlignedPages (
     548                 Xhc->PciIo,
     549                 EFI_SIZE_TO_PAGES (Xhc->PageSize),
     550                 Xhc->PageSize,
     551                 (VOID **) &ScratchEntry[Index],
     552                 &ScratchEntryPhy,
     553                 (VOID **) &ScratchEntryMap[Index]
     554                 );
     555      ASSERT_EFI_ERROR (Status);
     556      ZeroMem ((VOID *)(UINTN)ScratchEntry[Index], Xhc->PageSize);
     557      //
     558      // Fill with the PCI device address
     559      //
     560      *ScratchBuf++ = ScratchEntryPhy;
     561    }
    462562    //
    463563    // The Scratchpad Buffer Array contains pointers to the Scratchpad Buffers. Entry 0 of the
    464564    // Device Context Base Address Array points to the Scratchpad Buffer Array.
    465565    //
    466     *(UINT64 *)Dcbaa = (UINT64)(UINTN)Xhc->ScratchBuf;
     566    *(UINT64 *)Dcbaa = (UINT64)(UINTN) ScratchPhy;
    467567  }
    468568
     
    476576  // So divide it to two 32-bytes width register access.
    477577  //
    478   XhcWriteOpReg (Xhc, XHC_DCBAAP_OFFSET, XHC_LOW_32BIT(Xhc->DCBAA));
    479   XhcWriteOpReg (Xhc, XHC_DCBAAP_OFFSET + 4, XHC_HIGH_32BIT (Xhc->DCBAA));
     578  DcbaaPhy = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, Dcbaa, Entries);
     579  XhcWriteOpReg (Xhc, XHC_DCBAAP_OFFSET, XHC_LOW_32BIT(DcbaaPhy));
     580  XhcWriteOpReg (Xhc, XHC_DCBAAP_OFFSET + 4, XHC_HIGH_32BIT (DcbaaPhy));
     581
    480582  DEBUG ((EFI_D_INFO, "XhcInitSched:DCBAA=0x%x\n", (UINT64)(UINTN)Xhc->DCBAA));
    481583
     
    493595  //
    494596  CmdRing  = (UINT64)(UINTN)Xhc->CmdRing.RingSeg0;
    495   ASSERT ((CmdRing & 0x3F) == 0);
    496   CmdRing |= XHC_CRCR_RCS;
     597  CmdRingPhy = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, (VOID *)(UINTN) CmdRing, sizeof (TRB_TEMPLATE) * CMD_RING_TRB_NUMBER);
     598  ASSERT ((CmdRingPhy & 0x3F) == 0);
     599  CmdRingPhy |= XHC_CRCR_RCS;
    497600  //
    498601  // Some 3rd party XHCI external cards don't support single 64-bytes width register access,
    499602  // So divide it to two 32-bytes width register access.
    500603  //
    501   XhcWriteOpReg (Xhc, XHC_CRCR_OFFSET, XHC_LOW_32BIT(CmdRing));
    502   XhcWriteOpReg (Xhc, XHC_CRCR_OFFSET + 4, XHC_HIGH_32BIT (CmdRing));
     604  XhcWriteOpReg (Xhc, XHC_CRCR_OFFSET, XHC_LOW_32BIT(CmdRingPhy));
     605  XhcWriteOpReg (Xhc, XHC_CRCR_OFFSET + 4, XHC_HIGH_32BIT (CmdRingPhy));
    503606
    504607  DEBUG ((EFI_D_INFO, "XhcInitSched:XHC_CRCR=0x%x\n", Xhc->CmdRing.RingSeg0));
     
    548651  UINT8                       Dci;
    549652  UINT8                       SlotId;
     653  EFI_PHYSICAL_ADDRESS        PhyAddr;
    550654
    551655  Status = EFI_SUCCESS;
     
    573677             (TRB_TEMPLATE **) (UINTN) &EvtTrb
    574678             );
    575   ASSERT (!EFI_ERROR(Status));
     679  if (EFI_ERROR(Status)) {
     680    DEBUG ((EFI_D_ERROR, "XhcRecoverHaltedEndpoint: Reset Endpoint Failed, Status = %r\n", Status));
     681    goto Done;
     682  }
    576683
    577684  //
     
    579686  //
    580687  ZeroMem (&CmdSetTRDeq, sizeof (CmdSetTRDeq));
    581   CmdSetTRDeq.PtrLo    = XHC_LOW_32BIT (Urb->Ring->RingEnqueue) | Urb->Ring->RingPCS;
    582   CmdSetTRDeq.PtrHi    = XHC_HIGH_32BIT (Urb->Ring->RingEnqueue);
     688  PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, Urb->Ring->RingEnqueue, sizeof (CMD_SET_TR_DEQ_POINTER));
     689  CmdSetTRDeq.PtrLo    = XHC_LOW_32BIT (PhyAddr) | Urb->Ring->RingPCS;
     690  CmdSetTRDeq.PtrHi    = XHC_HIGH_32BIT (PhyAddr);
    583691  CmdSetTRDeq.CycleBit = 1;
    584692  CmdSetTRDeq.Type     = TRB_TYPE_SET_TR_DEQUE;
     
    591699             (TRB_TEMPLATE **) (UINTN) &EvtTrb
    592700             );
    593   ASSERT (!EFI_ERROR(Status));
     701  if (EFI_ERROR(Status)) {
     702    DEBUG ((EFI_D_ERROR, "XhcRecoverHaltedEndpoint: Set Dequeue Pointer Failed, Status = %r\n", Status));
     703    goto Done;
     704  }
    594705
    595706  //
     
    598709  XhcRingDoorBell (Xhc, SlotId, Dci);
    599710
     711Done:
    600712  return Status;
    601713}
     
    616728  VOID                        *Buf;
    617729  EVENT_RING_SEG_TABLE_ENTRY  *ERSTBase;
     730  UINTN                       Size;
     731  EFI_PHYSICAL_ADDRESS        ERSTPhy;
     732  EFI_PHYSICAL_ADDRESS        DequeuePhy;
    618733
    619734  ASSERT (EventRing != NULL);
    620735
    621   Buf = AllocatePages (EFI_SIZE_TO_PAGES (sizeof (TRB_TEMPLATE) * EVENT_RING_TRB_NUMBER));
     736  Size = sizeof (TRB_TEMPLATE) * EVENT_RING_TRB_NUMBER;
     737  Buf = UsbHcAllocateMem (Xhc->MemPool, Size);
    622738  ASSERT (Buf != NULL);
    623739  ASSERT (((UINTN) Buf & 0x3F) == 0);
    624   ZeroMem (Buf, sizeof (TRB_TEMPLATE) * EVENT_RING_TRB_NUMBER);
     740  ZeroMem (Buf, Size);
    625741
    626742  EventRing->EventRingSeg0    = Buf;
     
    628744  EventRing->EventRingDequeue = (TRB_TEMPLATE *) EventRing->EventRingSeg0;
    629745  EventRing->EventRingEnqueue = (TRB_TEMPLATE *) EventRing->EventRingSeg0;
     746 
     747  DequeuePhy = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, Buf, Size);
     748 
    630749  //
    631750  // Software maintains an Event Ring Consumer Cycle State (CCS) bit, initializing it to '1'
     
    634753  EventRing->EventRingCCS = 1;
    635754
    636   Buf = AllocatePages (EFI_SIZE_TO_PAGES (sizeof (EVENT_RING_SEG_TABLE_ENTRY) * ERST_NUMBER));
     755  Size = sizeof (EVENT_RING_SEG_TABLE_ENTRY) * ERST_NUMBER;
     756  Buf = UsbHcAllocateMem (Xhc->MemPool, Size);
    637757  ASSERT (Buf != NULL);
    638758  ASSERT (((UINTN) Buf & 0x3F) == 0);
    639   ZeroMem (Buf, sizeof (EVENT_RING_SEG_TABLE_ENTRY) * ERST_NUMBER);
     759  ZeroMem (Buf, Size);
    640760
    641761  ERSTBase              = (EVENT_RING_SEG_TABLE_ENTRY *) Buf;
    642762  EventRing->ERSTBase   = ERSTBase;
    643   ERSTBase->PtrLo       = XHC_LOW_32BIT (EventRing->EventRingSeg0);
    644   ERSTBase->PtrHi       = XHC_HIGH_32BIT (EventRing->EventRingSeg0);
     763  ERSTBase->PtrLo       = XHC_LOW_32BIT (DequeuePhy);
     764  ERSTBase->PtrHi       = XHC_HIGH_32BIT (DequeuePhy);
    645765  ERSTBase->RingTrbSize = EVENT_RING_TRB_NUMBER;
     766
     767  ERSTPhy = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, ERSTBase, Size);
    646768
    647769  //
     
    662784    Xhc,
    663785    XHC_ERDP_OFFSET,
    664     XHC_LOW_32BIT((UINT64)(UINTN)EventRing->EventRingDequeue)
     786    XHC_LOW_32BIT((UINT64)(UINTN)DequeuePhy)
    665787    );
    666788  XhcWriteRuntimeReg (
    667789    Xhc,
    668790    XHC_ERDP_OFFSET + 4,
    669     XHC_HIGH_32BIT((UINT64)(UINTN)EventRing->EventRingDequeue)
     791    XHC_HIGH_32BIT((UINT64)(UINTN)DequeuePhy)
    670792    );
    671793  //
     
    678800    Xhc,
    679801    XHC_ERSTBA_OFFSET,
    680     XHC_LOW_32BIT((UINT64)(UINTN)ERSTBase)
     802    XHC_LOW_32BIT((UINT64)(UINTN)ERSTPhy)
    681803    );
    682804  XhcWriteRuntimeReg (
    683805    Xhc,
    684806    XHC_ERSTBA_OFFSET + 4,
    685     XHC_HIGH_32BIT((UINT64)(UINTN)ERSTBase)
     807    XHC_HIGH_32BIT((UINT64)(UINTN)ERSTPhy)
    686808    );
    687809  //
     
    708830  VOID                  *Buf;
    709831  LINK_TRB              *EndTrb;
    710 
    711   Buf = AllocatePages (EFI_SIZE_TO_PAGES (sizeof (TRB_TEMPLATE) * TrbNum));
     832  EFI_PHYSICAL_ADDRESS  PhyAddr;
     833
     834  Buf = UsbHcAllocateMem (Xhc->MemPool, sizeof (TRB_TEMPLATE) * TrbNum);
    712835  ASSERT (Buf != NULL);
    713836  ASSERT (((UINTN) Buf & 0x3F) == 0);
     
    726849  EndTrb        = (LINK_TRB *) ((UINTN)Buf + sizeof (TRB_TEMPLATE) * (TrbNum - 1));
    727850  EndTrb->Type  = TRB_TYPE_LINK;
    728   EndTrb->PtrLo = XHC_LOW_32BIT (Buf);
    729   EndTrb->PtrHi = XHC_HIGH_32BIT (Buf);
     851  PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, Buf, sizeof (TRB_TEMPLATE) * TrbNum);
     852  EndTrb->PtrLo = XHC_LOW_32BIT (PhyAddr);
     853  EndTrb->PtrHi = XHC_HIGH_32BIT (PhyAddr);
    730854  //
    731855  // Toggle Cycle (TC). When set to '1', the xHC shall toggle its interpretation of the Cycle bit.
     
    752876)
    753877{
    754   UINT8                         Index;
    755   EVENT_RING_SEG_TABLE_ENTRY    *TablePtr;
    756   VOID                          *RingBuf;
    757   EVENT_RING_SEG_TABLE_ENTRY    *EventRingPtr;
    758 
    759878  if(EventRing->EventRingSeg0 == NULL) {
    760879    return EFI_SUCCESS;
     
    762881
    763882  //
    764   // Get the Event Ring Segment Table base address
    765   //
    766   TablePtr = (EVENT_RING_SEG_TABLE_ENTRY *)(EventRing->ERSTBase);
    767 
    768   //
    769   // Get all the TRBs Ring and release
    770   //
    771   for (Index = 0; Index < ERST_NUMBER; Index++) {
    772     EventRingPtr = TablePtr + Index;
    773     RingBuf      = (VOID *)(UINTN)(EventRingPtr->PtrLo | LShiftU64 ((UINT64)EventRingPtr->PtrHi, 32));
    774 
    775     if(RingBuf != NULL) {
    776       FreePages (RingBuf, EFI_SIZE_TO_PAGES (sizeof (TRB_TEMPLATE) * EVENT_RING_TRB_NUMBER));
    777       ZeroMem (EventRingPtr, sizeof (EVENT_RING_SEG_TABLE_ENTRY));
    778     }
    779   }
    780 
    781   FreePages (TablePtr, EFI_SIZE_TO_PAGES (sizeof (EVENT_RING_SEG_TABLE_ENTRY) * ERST_NUMBER));
     883  // Free EventRing Segment 0
     884  //
     885  UsbHcFreeMem (Xhc->MemPool, EventRing->EventRingSeg0, sizeof (TRB_TEMPLATE) * EVENT_RING_TRB_NUMBER);
     886
     887  //
     888  // Free ESRT table
     889  //
     890  UsbHcFreeMem (Xhc->MemPool, EventRing->ERSTBase, sizeof (EVENT_RING_SEG_TABLE_ENTRY) * ERST_NUMBER);
    782891  return EFI_SUCCESS;
    783892}
     
    794903  )
    795904{
    796   UINT32    Index;
    797   UINT64    *ScratchBuf;
    798 
     905  UINT32                  Index;
     906  UINT64                  *ScratchEntry;
     907 
    799908  if (Xhc->ScratchBuf != NULL) {
    800     ScratchBuf = Xhc->ScratchBuf;
     909    ScratchEntry = Xhc->ScratchEntry;
    801910    for (Index = 0; Index < Xhc->MaxScratchpadBufs; Index++) {
    802       FreeAlignedPages ((VOID*)(UINTN)*ScratchBuf++, EFI_SIZE_TO_PAGES (Xhc->PageSize));
    803     }
    804     FreeAlignedPages (Xhc->ScratchBuf, EFI_SIZE_TO_PAGES (Xhc->MaxScratchpadBufs * sizeof (UINT64)));
    805   }
     911      //
     912      // Free Scratchpad Buffers
     913      //
     914      UsbHcFreeAlignedPages (Xhc->PciIo, (VOID*)(UINTN)ScratchEntry[Index], EFI_SIZE_TO_PAGES (Xhc->PageSize), (VOID *) Xhc->ScratchEntryMap[Index]);
     915    }
     916    //
     917    // Free Scratchpad Buffer Array
     918    //
     919    UsbHcFreeAlignedPages (Xhc->PciIo, Xhc->ScratchBuf, EFI_SIZE_TO_PAGES (Xhc->MaxScratchpadBufs * sizeof (UINT64)), Xhc->ScratchMap);
     920    FreePool (Xhc->ScratchEntryMap);
     921    FreePool (Xhc->ScratchEntry);
     922  }
     923
     924  if (Xhc->CmdRing.RingSeg0 != NULL) {
     925    UsbHcFreeMem (Xhc->MemPool, Xhc->CmdRing.RingSeg0, sizeof (TRB_TEMPLATE) * CMD_RING_TRB_NUMBER);
     926    Xhc->CmdRing.RingSeg0 = NULL;
     927  }
     928 
     929  XhcFreeEventRing (Xhc,&Xhc->EventRing);
    806930
    807931  if (Xhc->DCBAA != NULL) {
    808     FreePages (Xhc->DCBAA, EFI_SIZE_TO_PAGES((Xhc->MaxSlotsEn + 1) * sizeof(UINT64)));
     932    UsbHcFreeMem (Xhc->MemPool, Xhc->DCBAA, (Xhc->MaxSlotsEn + 1) * sizeof(UINT64));
    809933    Xhc->DCBAA = NULL;
    810934  }
    811 
    812   if (Xhc->CmdRing.RingSeg0 != NULL){
    813     FreePages (Xhc->CmdRing.RingSeg0, EFI_SIZE_TO_PAGES (sizeof (TRB_TEMPLATE) * CMD_RING_TRB_NUMBER));
    814     Xhc->CmdRing.RingSeg0 = NULL;
    815   }
    816 
    817   XhcFreeEventRing (Xhc,&Xhc->EventRing);
     935 
     936  //
     937  // Free memory pool at last
     938  //
     939  if (Xhc->MemPool != NULL) {
     940    UsbHcFreeMemPool (Xhc->MemPool);
     941    Xhc->MemPool = NULL;
     942  }
    818943}
    819944
     
    9191044  UINT32                  High;
    9201045  UINT32                  Low;
     1046  EFI_PHYSICAL_ADDRESS    PhyAddr;
    9211047
    9221048  ASSERT ((Xhc != NULL) && (Urb != NULL));
     
    9561082      continue;
    9571083    }
    958 
    959     TRBPtr = (TRB_TEMPLATE *)(UINTN)(EvtTrb->TRBPtrLo | LShiftU64 ((UINT64) EvtTrb->TRBPtrHi, 32));
     1084   
     1085    //
     1086    // Need convert pci device address to host address
     1087    //
     1088    PhyAddr = (EFI_PHYSICAL_ADDRESS)(EvtTrb->TRBPtrLo | LShiftU64 ((UINT64) EvtTrb->TRBPtrHi, 32));
     1089    TRBPtr = (TRB_TEMPLATE *)(UINTN) UsbHcGetHostAddrForPciAddr (Xhc->MemPool, (VOID *)(UINTN) PhyAddr, sizeof (TRB_TEMPLATE));
    9601090
    9611091    //
     
    9781108        CheckedUrb->Finished = TRUE;
    9791109        DEBUG ((EFI_D_ERROR, "XhcCheckUrbResult: STALL_ERROR! Completecode = %x\n",EvtTrb->Completecode));
    980         break;
     1110        goto EXIT;
    9811111
    9821112      case TRB_COMPLETION_BABBLE_ERROR:
     
    9841114        CheckedUrb->Finished = TRUE;
    9851115        DEBUG ((EFI_D_ERROR, "XhcCheckUrbResult: BABBLE_ERROR! Completecode = %x\n",EvtTrb->Completecode));
    986         break;
     1116        goto EXIT;
    9871117
    9881118      case TRB_COMPLETION_DATA_BUFFER_ERROR:
     
    9901120        CheckedUrb->Finished = TRUE;
    9911121        DEBUG ((EFI_D_ERROR, "XhcCheckUrbResult: ERR_BUFFER! Completecode = %x\n",EvtTrb->Completecode));
    992         break;
     1122        goto EXIT;
    9931123
    9941124      case TRB_COMPLETION_USB_TRANSACTION_ERROR:
     
    9961126        CheckedUrb->Finished = TRUE;
    9971127        DEBUG ((EFI_D_ERROR, "XhcCheckUrbResult: TRANSACTION_ERROR! Completecode = %x\n",EvtTrb->Completecode));
    998         break;
     1128        goto EXIT;
    9991129
    10001130      case TRB_COMPLETION_SHORT_PACKET:
     
    10171147        CheckedUrb->Result  |= EFI_USB_ERR_TIMEOUT;
    10181148        CheckedUrb->Finished = TRUE;
    1019         break;
     1149        goto EXIT;
    10201150    }
    10211151
     
    10491179  XhcDequeue = (UINT64)(LShiftU64((UINT64)High, 32) | Low);
    10501180
    1051   if ((XhcDequeue & (~0x0F)) != ((UINT64)(UINTN)Xhc->EventRing.EventRingDequeue & (~0x0F))) {
     1181  PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, Xhc->EventRing.EventRingDequeue, sizeof (TRB_TEMPLATE));
     1182
     1183  if ((XhcDequeue & (~0x0F)) != (PhyAddr & (~0x0F))) {
    10521184    //
    10531185    // Some 3rd party XHCI external cards don't support single 64-bytes width register access,
    10541186    // So divide it to two 32-bytes width register access.
    10551187    //
    1056     XhcWriteRuntimeReg (Xhc, XHC_ERDP_OFFSET, XHC_LOW_32BIT (Xhc->EventRing.EventRingDequeue) | BIT3);
    1057     XhcWriteRuntimeReg (Xhc, XHC_ERDP_OFFSET + 4, XHC_HIGH_32BIT (Xhc->EventRing.EventRingDequeue));
     1188    XhcWriteRuntimeReg (Xhc, XHC_ERDP_OFFSET, XHC_LOW_32BIT (PhyAddr) | BIT3);
     1189    XhcWriteRuntimeReg (Xhc, XHC_ERDP_OFFSET + 4, XHC_HIGH_32BIT (PhyAddr));
    10581190  }
    10591191
     
    11021234
    11031235  Status = EFI_SUCCESS;
    1104   Loop   = (Timeout * XHC_1_MILLISECOND / XHC_POLL_DELAY) + 1;
     1236  Loop   = Timeout * XHC_1_MILLISECOND;
    11051237  if (Timeout == 0) {
    11061238    Loop = 0xFFFFFFFF;
     
    11141246      break;
    11151247    }
    1116     gBS->Stall (XHC_POLL_DELAY);
     1248    gBS->Stall (XHC_1_MICROSECOND);
    11171249  }
    11181250
     
    11601292      RemoveEntryList (&Urb->UrbList);
    11611293      FreePool (Urb->Data);
    1162       FreePool (Urb);
     1294      XhcFreeUrb (Xhc, Urb);
    11631295      return EFI_SUCCESS;
    11641296    }
     
    11871319    RemoveEntryList (&Urb->UrbList);
    11881320    FreePool (Urb->Data);
    1189     FreePool (Urb);
     1321    XhcFreeUrb (Xhc, Urb);
    11901322  }
    11911323}
     
    12181350}
    12191351
     1352/**
     1353  Flush data from PCI controller specific address to mapped system
     1354  memory address.
     1355
     1356  @param  Xhc                The XHCI device.
     1357  @param  Urb                The URB to unmap.
     1358
     1359  @retval EFI_SUCCESS        Success to flush data to mapped system memory.
     1360  @retval EFI_DEVICE_ERROR   Fail to flush data to mapped system memory.
     1361
     1362**/
     1363EFI_STATUS
     1364XhcFlushAsyncIntMap (
     1365  IN  USB_XHCI_INSTANCE   *Xhc,
     1366  IN  URB                 *Urb
     1367  )
     1368{
     1369  EFI_STATUS                    Status;
     1370  EFI_PHYSICAL_ADDRESS          PhyAddr;
     1371  EFI_PCI_IO_PROTOCOL_OPERATION MapOp;
     1372  EFI_PCI_IO_PROTOCOL           *PciIo;
     1373  UINTN                         Len;
     1374  VOID                          *Map;
     1375
     1376  PciIo = Xhc->PciIo;
     1377  Len   = Urb->DataLen;
     1378
     1379  if (Urb->Ep.Direction == EfiUsbDataIn) {
     1380    MapOp = EfiPciIoOperationBusMasterWrite;
     1381  } else {
     1382    MapOp = EfiPciIoOperationBusMasterRead;
     1383  }
     1384
     1385  if (Urb->DataMap != NULL) {
     1386    Status = PciIo->Unmap (PciIo, Urb->DataMap);
     1387    if (EFI_ERROR (Status)) {
     1388      goto ON_ERROR;
     1389    }
     1390  }
     1391
     1392  Urb->DataMap = NULL;
     1393
     1394  Status = PciIo->Map (PciIo, MapOp, Urb->Data, &Len, &PhyAddr, &Map);
     1395  if (EFI_ERROR (Status) || (Len != Urb->DataLen)) {
     1396    goto ON_ERROR;
     1397  }
     1398
     1399  Urb->DataPhy  = (VOID *) ((UINTN) PhyAddr);
     1400  Urb->DataMap  = Map;
     1401  return EFI_SUCCESS;
     1402
     1403ON_ERROR:
     1404  return EFI_DEVICE_ERROR;
     1405}
    12201406
    12211407/**
     
    12611447    // active, check the next one.
    12621448    //
    1263     Status = XhcCheckUrbResult (Xhc, Urb);
     1449    XhcCheckUrbResult (Xhc, Urb);
    12641450
    12651451    if (!Urb->Finished) {
    12661452      continue;
     1453    }
     1454
     1455    //
     1456    // Flush any PCI posted write transactions from a PCI host
     1457    // bridge to system memory.
     1458    //
     1459    Status = XhcFlushAsyncIntMap (Xhc, Urb);
     1460    if (EFI_ERROR (Status)) {
     1461      DEBUG ((EFI_D_ERROR, "XhcMonitorAsyncRequests: Fail to Flush AsyncInt Mapped Memeory\n"));
    12671462    }
    12681463
     
    13421537
    13431538  Status = EFI_SUCCESS;
     1539
     1540  if ((PortState->PortChangeStatus & (USB_PORT_STAT_C_CONNECTION | USB_PORT_STAT_C_ENABLE | USB_PORT_STAT_C_OVERCURRENT | USB_PORT_STAT_C_RESET)) == 0) {
     1541    return EFI_SUCCESS;
     1542  }
    13441543
    13451544  if (ParentRouteChart.Dword == 0) {
     
    13571556  }
    13581557
     1558  SlotId = XhcRouteStringToSlotId (Xhc, RouteChart);
     1559  if (SlotId != 0) {
     1560    if (Xhc->HcCParams.Data.Csz == 0) {
     1561      Status = XhcDisableSlotCmd (Xhc, SlotId);
     1562    } else {
     1563      Status = XhcDisableSlotCmd64 (Xhc, SlotId);
     1564    }
     1565  }
     1566
    13591567  if (((PortState->PortStatus & USB_PORT_STAT_ENABLE) != 0) &&
    13601568      ((PortState->PortStatus & USB_PORT_STAT_CONNECTION) != 0)) {
     
    13741582    //
    13751583    SlotId = XhcRouteStringToSlotId (Xhc, RouteChart);
    1376     if (SlotId == 0) {
     1584    if ((SlotId == 0) && ((PortState->PortChangeStatus & USB_PORT_STAT_C_RESET) != 0)) {
    13771585      if (Xhc->HcCParams.Data.Csz == 0) {
    13781586        Status = XhcInitializeDeviceSlot (Xhc, ParentRouteChart, Port, RouteChart, Speed);
     
    13801588        Status = XhcInitializeDeviceSlot64 (Xhc, ParentRouteChart, Port, RouteChart, Speed);
    13811589      }
    1382       ASSERT_EFI_ERROR (Status);
    1383     }
    1384   } else if ((PortState->PortStatus & USB_PORT_STAT_CONNECTION) == 0) {
    1385     //
    1386     // Device is detached. Disable the allocated device slot and release resource.
    1387     //
    1388     SlotId = XhcRouteStringToSlotId (Xhc, RouteChart);
    1389     if (SlotId != 0) {
    1390       if (Xhc->HcCParams.Data.Csz == 0) {
    1391         Status = XhcDisableSlotCmd (Xhc, SlotId);
    1392       } else {
    1393         Status = XhcDisableSlotCmd64 (Xhc, SlotId);
    1394       }
    1395       ASSERT_EFI_ERROR (Status);
    1396     }
    1397   }
     1590    }
     1591  }
     1592
    13981593  return Status;
    13991594}
     
    15841779      //
    15851780      TrsRing->RingPCS = (TrsRing->RingPCS & BIT0) ? 0 : 1;
    1586       TrsTrb           = (TRB_TEMPLATE *)(UINTN)((TrsTrb->Parameter1 | LShiftU64 ((UINT64)TrsTrb->Parameter2, 32)) & ~0x0F);
     1781      TrsTrb = (TRB_TEMPLATE *) TrsRing->RingSeg0;  // Use host address
    15871782    }
    15881783  }
     
    16261821  )
    16271822{
    1628   EFI_STATUS          Status;
    1629   TRB_TEMPLATE        *EvtTrb;
    1630 
    16311823  ASSERT (EvtRing != NULL);
    16321824
    1633   EvtTrb     = EvtRing->EventRingDequeue;
    16341825  *NewEvtTrb = EvtRing->EventRingDequeue;
    16351826
     
    16381829  }
    16391830
    1640   Status = EFI_SUCCESS;
    1641 
    16421831  EvtRing->EventRingDequeue++;
    16431832  //
     
    16481837  }
    16491838
    1650   return Status;
     1839  return EFI_SUCCESS;
    16511840}
    16521841
     
    17351924  UINT8                       ParentSlotId;
    17361925  DEVICE_CONTEXT              *ParentDeviceContext;
     1926  EFI_PHYSICAL_ADDRESS        PhyAddr;
    17371927
    17381928  ZeroMem (&CmdTrb, sizeof (CMD_TRB_ENABLE_SLOT));
     
    17461936              (TRB_TEMPLATE **) (UINTN) &EvtTrb
    17471937              );
    1748   ASSERT_EFI_ERROR (Status);
     1938  if (EFI_ERROR (Status)) {
     1939    DEBUG ((EFI_D_ERROR, "XhcInitializeDeviceSlot: Enable Slot Failed, Status = %r\n", Status));
     1940    return Status;
     1941  }
    17491942  ASSERT (EvtTrb->SlotId <= Xhc->MaxSlotsEn);
    17501943  DEBUG ((EFI_D_INFO, "Enable Slot Successfully, The Slot ID = 0x%x\n", EvtTrb->SlotId));
     
    17621955  // 1) Allocate an Input Context data structure (6.2.5) and initialize all fields to '0'.
    17631956  //
    1764   InputContext = AllocatePages (EFI_SIZE_TO_PAGES (sizeof (INPUT_CONTEXT)));
     1957  InputContext = UsbHcAllocateMem (Xhc->MemPool, sizeof (INPUT_CONTEXT));
    17651958  ASSERT (InputContext != NULL);
    17661959  ASSERT (((UINTN) InputContext & 0x3F) == 0);
     
    18512044  // Init the DCS(dequeue cycle state) as the transfer ring's CCS
    18522045  //
    1853   InputContext->EP[0].PtrLo = XHC_LOW_32BIT (((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[0])->RingSeg0) | BIT0;
    1854   InputContext->EP[0].PtrHi = XHC_HIGH_32BIT (((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[0])->RingSeg0);
     2046  PhyAddr = UsbHcGetPciAddrForHostAddr (
     2047              Xhc->MemPool,
     2048              ((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[0])->RingSeg0,
     2049              sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER
     2050              );
     2051  InputContext->EP[0].PtrLo = XHC_LOW_32BIT (PhyAddr) | BIT0;
     2052  InputContext->EP[0].PtrHi = XHC_HIGH_32BIT (PhyAddr);
    18552053
    18562054  //
    18572055  // 6) Allocate the Output Device Context data structure (6.2.1) and initialize it to '0'.
    18582056  //
    1859   OutputContext = AllocatePages (EFI_SIZE_TO_PAGES (sizeof (DEVICE_CONTEXT)));
     2057  OutputContext = UsbHcAllocateMem (Xhc->MemPool, sizeof (DEVICE_CONTEXT));
    18602058  ASSERT (OutputContext != NULL);
    18612059  ASSERT (((UINTN) OutputContext & 0x3F) == 0);
     
    18672065  //    a pointer to the Output Device Context data structure (6.2.1).
    18682066  //
    1869   Xhc->DCBAA[SlotId] = (UINT64) (UINTN) OutputContext;
     2067  PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, OutputContext, sizeof (DEVICE_CONTEXT));
     2068  //
     2069  // Fill DCBAA with PCI device address
     2070  //
     2071  Xhc->DCBAA[SlotId] = (UINT64) (UINTN) PhyAddr;
    18702072
    18712073  //
     
    18742076  //
    18752077  ZeroMem (&CmdTrbAddr, sizeof (CmdTrbAddr));
    1876   CmdTrbAddr.PtrLo    = XHC_LOW_32BIT (Xhc->UsbDevContext[SlotId].InputContext);
    1877   CmdTrbAddr.PtrHi    = XHC_HIGH_32BIT (Xhc->UsbDevContext[SlotId].InputContext);
     2078  PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, Xhc->UsbDevContext[SlotId].InputContext, sizeof (INPUT_CONTEXT));
     2079  CmdTrbAddr.PtrLo    = XHC_LOW_32BIT (PhyAddr);
     2080  CmdTrbAddr.PtrHi    = XHC_HIGH_32BIT (PhyAddr);
    18782081  CmdTrbAddr.CycleBit = 1;
    18792082  CmdTrbAddr.Type     = TRB_TYPE_ADDRESS_DEV;
     
    18852088             (TRB_TEMPLATE **) (UINTN) &EvtTrb
    18862089             );
    1887   ASSERT (!EFI_ERROR(Status));
    1888 
    1889   DeviceAddress = (UINT8) ((DEVICE_CONTEXT *) OutputContext)->Slot.DeviceAddress;
    1890   DEBUG ((EFI_D_INFO, "    Address %d assigned successfully\n", DeviceAddress));
    1891 
    1892   Xhc->UsbDevContext[SlotId].XhciDevAddr = DeviceAddress;
     2090  if (!EFI_ERROR (Status)) {
     2091    DeviceAddress = (UINT8) ((DEVICE_CONTEXT *) OutputContext)->Slot.DeviceAddress;
     2092    DEBUG ((EFI_D_INFO, "    Address %d assigned successfully\n", DeviceAddress));
     2093    Xhc->UsbDevContext[SlotId].XhciDevAddr = DeviceAddress;
     2094  }
    18932095
    18942096  return Status;
     
    19282130  UINT8                       ParentSlotId;
    19292131  DEVICE_CONTEXT_64           *ParentDeviceContext;
     2132  EFI_PHYSICAL_ADDRESS        PhyAddr;
    19302133
    19312134  ZeroMem (&CmdTrb, sizeof (CMD_TRB_ENABLE_SLOT));
     
    19392142              (TRB_TEMPLATE **) (UINTN) &EvtTrb
    19402143              );
    1941   ASSERT_EFI_ERROR (Status);
     2144  if (EFI_ERROR (Status)) {
     2145    DEBUG ((EFI_D_ERROR, "XhcInitializeDeviceSlot64: Enable Slot Failed, Status = %r\n", Status));
     2146    return Status;
     2147  }
    19422148  ASSERT (EvtTrb->SlotId <= Xhc->MaxSlotsEn);
    19432149  DEBUG ((EFI_D_INFO, "Enable Slot Successfully, The Slot ID = 0x%x\n", EvtTrb->SlotId));
     
    19552161  // 1) Allocate an Input Context data structure (6.2.5) and initialize all fields to '0'.
    19562162  //
    1957   InputContext = AllocatePages (EFI_SIZE_TO_PAGES (sizeof (INPUT_CONTEXT_64)));
     2163  InputContext = UsbHcAllocateMem (Xhc->MemPool, sizeof (INPUT_CONTEXT_64));
    19582164  ASSERT (InputContext != NULL);
    19592165  ASSERT (((UINTN) InputContext & 0x3F) == 0);
     
    20442250  // Init the DCS(dequeue cycle state) as the transfer ring's CCS
    20452251  //
    2046   InputContext->EP[0].PtrLo = XHC_LOW_32BIT (((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[0])->RingSeg0) | BIT0;
    2047   InputContext->EP[0].PtrHi = XHC_HIGH_32BIT (((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[0])->RingSeg0);
     2252  PhyAddr = UsbHcGetPciAddrForHostAddr (
     2253              Xhc->MemPool,
     2254              ((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[0])->RingSeg0,
     2255              sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER
     2256              );
     2257  InputContext->EP[0].PtrLo = XHC_LOW_32BIT (PhyAddr) | BIT0;
     2258  InputContext->EP[0].PtrHi = XHC_HIGH_32BIT (PhyAddr);
    20482259
    20492260  //
    20502261  // 6) Allocate the Output Device Context data structure (6.2.1) and initialize it to '0'.
    20512262  //
    2052   OutputContext = AllocatePages (EFI_SIZE_TO_PAGES (sizeof (DEVICE_CONTEXT_64)));
     2263  OutputContext = UsbHcAllocateMem (Xhc->MemPool, sizeof (DEVICE_CONTEXT_64));
    20532264  ASSERT (OutputContext != NULL);
    20542265  ASSERT (((UINTN) OutputContext & 0x3F) == 0);
     
    20602271  //    a pointer to the Output Device Context data structure (6.2.1).
    20612272  //
    2062   Xhc->DCBAA[SlotId] = (UINT64) (UINTN) OutputContext;
     2273  PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, OutputContext, sizeof (DEVICE_CONTEXT_64));
     2274  //
     2275  // Fill DCBAA with PCI device address
     2276  //
     2277  Xhc->DCBAA[SlotId] = (UINT64) (UINTN) PhyAddr;
    20632278
    20642279  //
     
    20672282  //
    20682283  ZeroMem (&CmdTrbAddr, sizeof (CmdTrbAddr));
    2069   CmdTrbAddr.PtrLo    = XHC_LOW_32BIT (Xhc->UsbDevContext[SlotId].InputContext);
    2070   CmdTrbAddr.PtrHi    = XHC_HIGH_32BIT (Xhc->UsbDevContext[SlotId].InputContext);
     2284  PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, Xhc->UsbDevContext[SlotId].InputContext, sizeof (INPUT_CONTEXT_64));
     2285  CmdTrbAddr.PtrLo    = XHC_LOW_32BIT (PhyAddr);
     2286  CmdTrbAddr.PtrHi    = XHC_HIGH_32BIT (PhyAddr);
    20712287  CmdTrbAddr.CycleBit = 1;
    20722288  CmdTrbAddr.Type     = TRB_TYPE_ADDRESS_DEV;
     
    20782294             (TRB_TEMPLATE **) (UINTN) &EvtTrb
    20792295             );
    2080   ASSERT (!EFI_ERROR(Status));
    2081 
    2082   DeviceAddress = (UINT8) ((DEVICE_CONTEXT_64 *) OutputContext)->Slot.DeviceAddress;
    2083   DEBUG ((EFI_D_INFO, "    Address %d assigned successfully\n", DeviceAddress));
    2084 
    2085   Xhc->UsbDevContext[SlotId].XhciDevAddr = DeviceAddress;
    2086 
     2296  if (!EFI_ERROR (Status)) {
     2297    DeviceAddress = (UINT8) ((DEVICE_CONTEXT_64 *) OutputContext)->Slot.DeviceAddress;
     2298    DEBUG ((EFI_D_INFO, "    Address %d assigned successfully\n", DeviceAddress));
     2299    Xhc->UsbDevContext[SlotId].XhciDevAddr = DeviceAddress;
     2300  }
    20872301  return Status;
    20882302}
     
    21452359             (TRB_TEMPLATE **) (UINTN) &EvtTrb
    21462360             );
    2147   ASSERT_EFI_ERROR(Status);
     2361  if (EFI_ERROR (Status)) {
     2362    DEBUG ((EFI_D_ERROR, "XhcDisableSlotCmd: Disable Slot Command Failed, Status = %r\n", Status));
     2363    return Status;
     2364  }
    21482365  //
    21492366  // Free the slot's device context entry
     
    21582375      RingSeg = ((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Index])->RingSeg0;
    21592376      if (RingSeg != NULL) {
    2160         FreePages (RingSeg, EFI_SIZE_TO_PAGES (sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER));
     2377        UsbHcFreeMem (Xhc->MemPool, RingSeg, sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER);
    21612378      }
    21622379      FreePool (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Index]);
     2380      Xhc->UsbDevContext[SlotId].EndpointTransferRing[Index] = NULL;
    21632381    }
    21642382  }
     
    21702388  }
    21712389
     2390  if (Xhc->UsbDevContext[SlotId].ActiveAlternateSetting != NULL) {
     2391    FreePool (Xhc->UsbDevContext[SlotId].ActiveAlternateSetting);
     2392  }
     2393
    21722394  if (Xhc->UsbDevContext[SlotId].InputContext != NULL) {
    2173     FreePages (Xhc->UsbDevContext[SlotId].InputContext, EFI_SIZE_TO_PAGES (sizeof (INPUT_CONTEXT)));
     2395    UsbHcFreeMem (Xhc->MemPool, Xhc->UsbDevContext[SlotId].InputContext, sizeof (INPUT_CONTEXT));
    21742396  }
    21752397
    21762398  if (Xhc->UsbDevContext[SlotId].OutputContext != NULL) {
    2177     FreePages (Xhc->UsbDevContext[SlotId].OutputContext, EFI_SIZE_TO_PAGES (sizeof (DEVICE_CONTEXT)));
     2399    UsbHcFreeMem (Xhc->MemPool, Xhc->UsbDevContext[SlotId].OutputContext, sizeof (DEVICE_CONTEXT));
    21782400  }
    21792401  //
     
    22442466             (TRB_TEMPLATE **) (UINTN) &EvtTrb
    22452467             );
    2246   ASSERT_EFI_ERROR(Status);
     2468  if (EFI_ERROR (Status)) {
     2469    DEBUG ((EFI_D_ERROR, "XhcDisableSlotCmd: Disable Slot Command Failed, Status = %r\n", Status));
     2470    return Status;
     2471  }
    22472472  //
    22482473  // Free the slot's device context entry
     
    22572482      RingSeg = ((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Index])->RingSeg0;
    22582483      if (RingSeg != NULL) {
    2259         FreePages (RingSeg, EFI_SIZE_TO_PAGES (sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER));
     2484        UsbHcFreeMem (Xhc->MemPool, RingSeg, sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER);
    22602485      }
    22612486      FreePool (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Index]);
     2487      Xhc->UsbDevContext[SlotId].EndpointTransferRing[Index] = NULL;
    22622488    }
    22632489  }
     
    22692495  }
    22702496
     2497  if (Xhc->UsbDevContext[SlotId].ActiveAlternateSetting != NULL) {
     2498    FreePool (Xhc->UsbDevContext[SlotId].ActiveAlternateSetting);
     2499  }
     2500
    22712501  if (Xhc->UsbDevContext[SlotId].InputContext != NULL) {
    2272     FreePages (Xhc->UsbDevContext[SlotId].InputContext, EFI_SIZE_TO_PAGES (sizeof (INPUT_CONTEXT_64)));
     2502    UsbHcFreeMem (Xhc->MemPool, Xhc->UsbDevContext[SlotId].InputContext, sizeof (INPUT_CONTEXT_64));
    22732503  }
    22742504
    22752505  if (Xhc->UsbDevContext[SlotId].OutputContext != NULL) {
    2276     FreePages (Xhc->UsbDevContext[SlotId].OutputContext, EFI_SIZE_TO_PAGES (sizeof (DEVICE_CONTEXT_64)));
     2506     UsbHcFreeMem (Xhc->MemPool, Xhc->UsbDevContext[SlotId].OutputContext, sizeof (DEVICE_CONTEXT_64));
    22772507  }
    22782508  //
     
    22872517}
    22882518
     2519/**
     2520  Initialize endpoint context in input context.
     2521
     2522  @param Xhc            The XHCI Instance.
     2523  @param SlotId         The slot id to be configured.
     2524  @param DeviceSpeed    The device's speed.
     2525  @param InputContext   The pointer to the input context.
     2526  @param IfDesc         The pointer to the usb device interface descriptor.
     2527
     2528  @return The maximum device context index of endpoint.
     2529
     2530**/
     2531UINT8
     2532EFIAPI
     2533XhcInitializeEndpointContext (
     2534  IN USB_XHCI_INSTANCE          *Xhc,
     2535  IN UINT8                      SlotId,
     2536  IN UINT8                      DeviceSpeed,
     2537  IN INPUT_CONTEXT              *InputContext,
     2538  IN USB_INTERFACE_DESCRIPTOR   *IfDesc
     2539  )
     2540{
     2541  USB_ENDPOINT_DESCRIPTOR       *EpDesc;
     2542  UINTN                         NumEp;
     2543  UINTN                         EpIndex;
     2544  UINT8                         EpAddr;
     2545  UINT8                         Direction;
     2546  UINT8                         Dci;
     2547  UINT8                         MaxDci;
     2548  EFI_PHYSICAL_ADDRESS          PhyAddr;
     2549  UINT8                         Interval;
     2550  TRANSFER_RING                 *EndpointTransferRing;
     2551
     2552  MaxDci = 0;
     2553
     2554  NumEp = IfDesc->NumEndpoints;
     2555
     2556  EpDesc = (USB_ENDPOINT_DESCRIPTOR *)(IfDesc + 1);
     2557  for (EpIndex = 0; EpIndex < NumEp; EpIndex++) {
     2558    while (EpDesc->DescriptorType != USB_DESC_TYPE_ENDPOINT) {
     2559      EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);
     2560    }
     2561
     2562    EpAddr    = (UINT8)(EpDesc->EndpointAddress & 0x0F);
     2563    Direction = (UINT8)((EpDesc->EndpointAddress & 0x80) ? EfiUsbDataIn : EfiUsbDataOut);
     2564
     2565    Dci = XhcEndpointToDci (EpAddr, Direction);
     2566    ASSERT (Dci < 32);
     2567    if (Dci > MaxDci) {
     2568      MaxDci = Dci;
     2569    }
     2570
     2571    InputContext->InputControlContext.Dword2 |= (BIT0 << Dci);
     2572    InputContext->EP[Dci-1].MaxPacketSize     = EpDesc->MaxPacketSize;
     2573
     2574    if (DeviceSpeed == EFI_USB_SPEED_SUPER) {
     2575      //
     2576      // 6.2.3.4, shall be set to the value defined in the bMaxBurst field of the SuperSpeed Endpoint Companion Descriptor.
     2577      //
     2578      InputContext->EP[Dci-1].MaxBurstSize = 0x0;
     2579    } else {
     2580      InputContext->EP[Dci-1].MaxBurstSize = 0x0;
     2581    }
     2582
     2583    switch (EpDesc->Attributes & USB_ENDPOINT_TYPE_MASK) {
     2584      case USB_ENDPOINT_BULK:
     2585        if (Direction == EfiUsbDataIn) {
     2586          InputContext->EP[Dci-1].CErr   = 3;
     2587          InputContext->EP[Dci-1].EPType = ED_BULK_IN;
     2588        } else {
     2589          InputContext->EP[Dci-1].CErr   = 3;
     2590          InputContext->EP[Dci-1].EPType = ED_BULK_OUT;
     2591        }
     2592
     2593        InputContext->EP[Dci-1].AverageTRBLength = 0x1000;
     2594        if (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] == NULL) {
     2595          EndpointTransferRing = AllocateZeroPool(sizeof (TRANSFER_RING));
     2596          Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] = (VOID *) EndpointTransferRing;
     2597          CreateTransferRing(Xhc, TR_RING_TRB_NUMBER, (TRANSFER_RING *)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1]);
     2598        }
     2599
     2600        break;
     2601      case USB_ENDPOINT_ISO:
     2602        if (Direction == EfiUsbDataIn) {
     2603          InputContext->EP[Dci-1].CErr   = 0;
     2604          InputContext->EP[Dci-1].EPType = ED_ISOCH_IN;
     2605        } else {
     2606          InputContext->EP[Dci-1].CErr   = 0;
     2607          InputContext->EP[Dci-1].EPType = ED_ISOCH_OUT;
     2608        }
     2609        //
     2610        // Do not support isochronous transfer now.
     2611        //
     2612        DEBUG ((EFI_D_INFO, "XhcInitializeEndpointContext: Unsupport ISO EP found, Transfer ring is not allocated.\n"));
     2613        EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);
     2614        continue;
     2615      case USB_ENDPOINT_INTERRUPT:
     2616        if (Direction == EfiUsbDataIn) {
     2617          InputContext->EP[Dci-1].CErr   = 3;
     2618          InputContext->EP[Dci-1].EPType = ED_INTERRUPT_IN;
     2619        } else {
     2620          InputContext->EP[Dci-1].CErr   = 3;
     2621          InputContext->EP[Dci-1].EPType = ED_INTERRUPT_OUT;
     2622        }
     2623        InputContext->EP[Dci-1].AverageTRBLength = 0x1000;
     2624        InputContext->EP[Dci-1].MaxESITPayload   = EpDesc->MaxPacketSize;
     2625        //
     2626        // Get the bInterval from descriptor and init the the interval field of endpoint context
     2627        //
     2628        if ((DeviceSpeed == EFI_USB_SPEED_FULL) || (DeviceSpeed == EFI_USB_SPEED_LOW)) {
     2629          Interval = EpDesc->Interval;
     2630          //
     2631          // Calculate through the bInterval field of Endpoint descriptor.
     2632          //
     2633          ASSERT (Interval != 0);
     2634          InputContext->EP[Dci-1].Interval = (UINT32)HighBitSet32((UINT32)Interval) + 3;
     2635        } else if ((DeviceSpeed == EFI_USB_SPEED_HIGH) || (DeviceSpeed == EFI_USB_SPEED_SUPER)) {
     2636          Interval = EpDesc->Interval;
     2637          ASSERT (Interval >= 1 && Interval <= 16);
     2638          //
     2639          // Refer to XHCI 1.0 spec section 6.2.3.6, table 61
     2640          //
     2641          InputContext->EP[Dci-1].Interval         = Interval - 1;
     2642          InputContext->EP[Dci-1].AverageTRBLength = 0x1000;
     2643          InputContext->EP[Dci-1].MaxESITPayload   = 0x0002;
     2644          InputContext->EP[Dci-1].MaxBurstSize     = 0x0;
     2645          InputContext->EP[Dci-1].CErr             = 3;
     2646        }
     2647
     2648        if (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] == NULL) {
     2649          EndpointTransferRing = AllocateZeroPool(sizeof (TRANSFER_RING));
     2650          Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] = (VOID *) EndpointTransferRing;
     2651          CreateTransferRing(Xhc, TR_RING_TRB_NUMBER, (TRANSFER_RING *)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1]);
     2652        }
     2653        break;
     2654
     2655      case USB_ENDPOINT_CONTROL:
     2656        //
     2657        // Do not support control transfer now.
     2658        //
     2659        DEBUG ((EFI_D_INFO, "XhcInitializeEndpointContext: Unsupport Control EP found, Transfer ring is not allocated.\n"));
     2660      default:
     2661        DEBUG ((EFI_D_INFO, "XhcInitializeEndpointContext: Unknown EP found, Transfer ring is not allocated.\n"));
     2662        EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);
     2663        continue;
     2664    }
     2665
     2666    PhyAddr = UsbHcGetPciAddrForHostAddr (
     2667                Xhc->MemPool,
     2668                ((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1])->RingSeg0,
     2669                sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER
     2670                );
     2671    PhyAddr &= ~((EFI_PHYSICAL_ADDRESS)0x0F);
     2672    PhyAddr |= (EFI_PHYSICAL_ADDRESS)((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1])->RingPCS;
     2673    InputContext->EP[Dci-1].PtrLo = XHC_LOW_32BIT (PhyAddr);
     2674    InputContext->EP[Dci-1].PtrHi = XHC_HIGH_32BIT (PhyAddr);
     2675
     2676    EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);
     2677  }
     2678
     2679  return MaxDci;
     2680}
     2681
     2682/**
     2683  Initialize endpoint context in input context.
     2684
     2685  @param Xhc            The XHCI Instance.
     2686  @param SlotId         The slot id to be configured.
     2687  @param DeviceSpeed    The device's speed.
     2688  @param InputContext   The pointer to the input context.
     2689  @param IfDesc         The pointer to the usb device interface descriptor.
     2690
     2691  @return The maximum device context index of endpoint.
     2692
     2693**/
     2694UINT8
     2695EFIAPI
     2696XhcInitializeEndpointContext64 (
     2697  IN USB_XHCI_INSTANCE          *Xhc,
     2698  IN UINT8                      SlotId,
     2699  IN UINT8                      DeviceSpeed,
     2700  IN INPUT_CONTEXT_64           *InputContext,
     2701  IN USB_INTERFACE_DESCRIPTOR   *IfDesc
     2702  )
     2703{
     2704  USB_ENDPOINT_DESCRIPTOR       *EpDesc;
     2705  UINTN                         NumEp;
     2706  UINTN                         EpIndex;
     2707  UINT8                         EpAddr;
     2708  UINT8                         Direction;
     2709  UINT8                         Dci;
     2710  UINT8                         MaxDci;
     2711  EFI_PHYSICAL_ADDRESS          PhyAddr;
     2712  UINT8                         Interval;
     2713  TRANSFER_RING                 *EndpointTransferRing;
     2714
     2715  MaxDci = 0;
     2716
     2717  NumEp = IfDesc->NumEndpoints;
     2718
     2719  EpDesc = (USB_ENDPOINT_DESCRIPTOR *)(IfDesc + 1);
     2720  for (EpIndex = 0; EpIndex < NumEp; EpIndex++) {
     2721    while (EpDesc->DescriptorType != USB_DESC_TYPE_ENDPOINT) {
     2722      EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);
     2723    }
     2724
     2725    EpAddr    = (UINT8)(EpDesc->EndpointAddress & 0x0F);
     2726    Direction = (UINT8)((EpDesc->EndpointAddress & 0x80) ? EfiUsbDataIn : EfiUsbDataOut);
     2727
     2728    Dci = XhcEndpointToDci (EpAddr, Direction);
     2729    ASSERT (Dci < 32);
     2730    if (Dci > MaxDci) {
     2731      MaxDci = Dci;
     2732    }
     2733
     2734    InputContext->InputControlContext.Dword2 |= (BIT0 << Dci);
     2735    InputContext->EP[Dci-1].MaxPacketSize     = EpDesc->MaxPacketSize;
     2736
     2737    if (DeviceSpeed == EFI_USB_SPEED_SUPER) {
     2738      //
     2739      // 6.2.3.4, shall be set to the value defined in the bMaxBurst field of the SuperSpeed Endpoint Companion Descriptor.
     2740      //
     2741      InputContext->EP[Dci-1].MaxBurstSize = 0x0;
     2742    } else {
     2743      InputContext->EP[Dci-1].MaxBurstSize = 0x0;
     2744    }
     2745
     2746    switch (EpDesc->Attributes & USB_ENDPOINT_TYPE_MASK) {
     2747      case USB_ENDPOINT_BULK:
     2748        if (Direction == EfiUsbDataIn) {
     2749          InputContext->EP[Dci-1].CErr   = 3;
     2750          InputContext->EP[Dci-1].EPType = ED_BULK_IN;
     2751        } else {
     2752          InputContext->EP[Dci-1].CErr   = 3;
     2753          InputContext->EP[Dci-1].EPType = ED_BULK_OUT;
     2754        }
     2755
     2756        InputContext->EP[Dci-1].AverageTRBLength = 0x1000;
     2757        if (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] == NULL) {
     2758          EndpointTransferRing = AllocateZeroPool(sizeof (TRANSFER_RING));
     2759          Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] = (VOID *) EndpointTransferRing;
     2760          CreateTransferRing(Xhc, TR_RING_TRB_NUMBER, (TRANSFER_RING *)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1]);
     2761        }
     2762
     2763        break;
     2764      case USB_ENDPOINT_ISO:
     2765        if (Direction == EfiUsbDataIn) {
     2766          InputContext->EP[Dci-1].CErr   = 0;
     2767          InputContext->EP[Dci-1].EPType = ED_ISOCH_IN;
     2768        } else {
     2769          InputContext->EP[Dci-1].CErr   = 0;
     2770          InputContext->EP[Dci-1].EPType = ED_ISOCH_OUT;
     2771        }
     2772        //
     2773        // Do not support isochronous transfer now.
     2774        //
     2775        DEBUG ((EFI_D_INFO, "XhcInitializeEndpointContext64: Unsupport ISO EP found, Transfer ring is not allocated.\n"));
     2776        EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);
     2777        continue;
     2778      case USB_ENDPOINT_INTERRUPT:
     2779        if (Direction == EfiUsbDataIn) {
     2780          InputContext->EP[Dci-1].CErr   = 3;
     2781          InputContext->EP[Dci-1].EPType = ED_INTERRUPT_IN;
     2782        } else {
     2783          InputContext->EP[Dci-1].CErr   = 3;
     2784          InputContext->EP[Dci-1].EPType = ED_INTERRUPT_OUT;
     2785        }
     2786        InputContext->EP[Dci-1].AverageTRBLength = 0x1000;
     2787        InputContext->EP[Dci-1].MaxESITPayload   = EpDesc->MaxPacketSize;
     2788        //
     2789        // Get the bInterval from descriptor and init the the interval field of endpoint context
     2790        //
     2791        if ((DeviceSpeed == EFI_USB_SPEED_FULL) || (DeviceSpeed == EFI_USB_SPEED_LOW)) {
     2792          Interval = EpDesc->Interval;
     2793          //
     2794          // Calculate through the bInterval field of Endpoint descriptor.
     2795          //
     2796          ASSERT (Interval != 0);
     2797          InputContext->EP[Dci-1].Interval = (UINT32)HighBitSet32((UINT32)Interval) + 3;
     2798        } else if ((DeviceSpeed == EFI_USB_SPEED_HIGH) || (DeviceSpeed == EFI_USB_SPEED_SUPER)) {
     2799          Interval = EpDesc->Interval;
     2800          ASSERT (Interval >= 1 && Interval <= 16);
     2801          //
     2802          // Refer to XHCI 1.0 spec section 6.2.3.6, table 61
     2803          //
     2804          InputContext->EP[Dci-1].Interval         = Interval - 1;
     2805          InputContext->EP[Dci-1].AverageTRBLength = 0x1000;
     2806          InputContext->EP[Dci-1].MaxESITPayload   = 0x0002;
     2807          InputContext->EP[Dci-1].MaxBurstSize     = 0x0;
     2808          InputContext->EP[Dci-1].CErr             = 3;
     2809        }
     2810
     2811        if (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] == NULL) {
     2812          EndpointTransferRing = AllocateZeroPool(sizeof (TRANSFER_RING));
     2813          Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] = (VOID *) EndpointTransferRing;
     2814          CreateTransferRing(Xhc, TR_RING_TRB_NUMBER, (TRANSFER_RING *)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1]);
     2815        }
     2816        break;
     2817
     2818      case USB_ENDPOINT_CONTROL:
     2819        //
     2820        // Do not support control transfer now.
     2821        //
     2822        DEBUG ((EFI_D_INFO, "XhcInitializeEndpointContext64: Unsupport Control EP found, Transfer ring is not allocated.\n"));
     2823      default:
     2824        DEBUG ((EFI_D_INFO, "XhcInitializeEndpointContext64: Unknown EP found, Transfer ring is not allocated.\n"));
     2825        EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);
     2826        continue;
     2827    }
     2828
     2829    PhyAddr = UsbHcGetPciAddrForHostAddr (
     2830                Xhc->MemPool,
     2831                ((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1])->RingSeg0,
     2832                sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER
     2833                );
     2834    PhyAddr &= ~((EFI_PHYSICAL_ADDRESS)0x0F);
     2835    PhyAddr |= (EFI_PHYSICAL_ADDRESS)((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1])->RingPCS;
     2836    InputContext->EP[Dci-1].PtrLo = XHC_LOW_32BIT (PhyAddr);
     2837    InputContext->EP[Dci-1].PtrHi = XHC_HIGH_32BIT (PhyAddr);
     2838
     2839    EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);
     2840  }
     2841
     2842  return MaxDci;
     2843}
    22892844
    22902845/**
     
    23092864{
    23102865  EFI_STATUS                  Status;
    2311 
    23122866  USB_INTERFACE_DESCRIPTOR    *IfDesc;
    2313   USB_ENDPOINT_DESCRIPTOR     *EpDesc;
    23142867  UINT8                       Index;
    2315   UINTN                       NumEp;
    2316   UINTN                       EpIndex;
    2317   UINT8                       EpAddr;
    2318   UINT8                       Direction;
    23192868  UINT8                       Dci;
    23202869  UINT8                       MaxDci;
    2321   UINT32                      PhyAddr;
    2322   UINT8                       Interval;
    2323 
    2324   TRANSFER_RING               *EndpointTransferRing;
     2870  EFI_PHYSICAL_ADDRESS        PhyAddr;
     2871
    23252872  CMD_TRB_CONFIG_ENDPOINT     CmdTrbCfgEP;
    23262873  INPUT_CONTEXT               *InputContext;
     
    23412888  IfDesc = (USB_INTERFACE_DESCRIPTOR *)(ConfigDesc + 1);
    23422889  for (Index = 0; Index < ConfigDesc->NumInterfaces; Index++) {
    2343     while (IfDesc->DescriptorType != USB_DESC_TYPE_INTERFACE) {
     2890    while ((IfDesc->DescriptorType != USB_DESC_TYPE_INTERFACE) || (IfDesc->AlternateSetting != 0)) {
    23442891      IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length);
    23452892    }
    23462893
    2347     NumEp = IfDesc->NumEndpoints;
    2348 
    2349     EpDesc = (USB_ENDPOINT_DESCRIPTOR *)(IfDesc + 1);
    2350     for (EpIndex = 0; EpIndex < NumEp; EpIndex++) {
    2351       while (EpDesc->DescriptorType != USB_DESC_TYPE_ENDPOINT) {
    2352         EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);
    2353       }
    2354 
    2355       EpAddr    = (UINT8)(EpDesc->EndpointAddress & 0x0F);
    2356       Direction = (UINT8)((EpDesc->EndpointAddress & 0x80) ? EfiUsbDataIn : EfiUsbDataOut);
    2357 
    2358       Dci = XhcEndpointToDci (EpAddr, Direction);
    2359       ASSERT (Dci < 32);
    2360       if (Dci > MaxDci) {
    2361         MaxDci = Dci;
    2362       }
    2363 
    2364       InputContext->InputControlContext.Dword2 |= (BIT0 << Dci);
    2365       InputContext->EP[Dci-1].MaxPacketSize     = EpDesc->MaxPacketSize;
    2366 
    2367       if (DeviceSpeed == EFI_USB_SPEED_SUPER) {
    2368         //
    2369         // 6.2.3.4, shall be set to the value defined in the bMaxBurst field of the SuperSpeed Endpoint Companion Descriptor.
    2370         //
    2371         InputContext->EP[Dci-1].MaxBurstSize = 0x0;
    2372       } else {
    2373         InputContext->EP[Dci-1].MaxBurstSize = 0x0;
    2374       }
    2375 
    2376       switch (EpDesc->Attributes & USB_ENDPOINT_TYPE_MASK) {
    2377         case USB_ENDPOINT_BULK:
    2378           if (Direction == EfiUsbDataIn) {
    2379             InputContext->EP[Dci-1].CErr   = 3;
    2380             InputContext->EP[Dci-1].EPType = ED_BULK_IN;
    2381           } else {
    2382             InputContext->EP[Dci-1].CErr   = 3;
    2383             InputContext->EP[Dci-1].EPType = ED_BULK_OUT;
    2384           }
    2385 
    2386           InputContext->EP[Dci-1].AverageTRBLength = 0x1000;
    2387           if (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] == NULL) {
    2388             EndpointTransferRing = AllocateZeroPool(sizeof (TRANSFER_RING));
    2389             Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] = (VOID *) EndpointTransferRing;
    2390             CreateTransferRing(Xhc, TR_RING_TRB_NUMBER, (TRANSFER_RING *)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1]);
    2391           }
    2392 
    2393           break;
    2394         case USB_ENDPOINT_ISO:
    2395           if (Direction == EfiUsbDataIn) {
    2396             InputContext->EP[Dci-1].CErr   = 0;
    2397             InputContext->EP[Dci-1].EPType = ED_ISOCH_IN;
    2398           } else {
    2399             InputContext->EP[Dci-1].CErr   = 0;
    2400             InputContext->EP[Dci-1].EPType = ED_ISOCH_OUT;
    2401           }
    2402           break;
    2403         case USB_ENDPOINT_INTERRUPT:
    2404           if (Direction == EfiUsbDataIn) {
    2405             InputContext->EP[Dci-1].CErr   = 3;
    2406             InputContext->EP[Dci-1].EPType = ED_INTERRUPT_IN;
    2407           } else {
    2408             InputContext->EP[Dci-1].CErr   = 3;
    2409             InputContext->EP[Dci-1].EPType = ED_INTERRUPT_OUT;
    2410           }
    2411           InputContext->EP[Dci-1].AverageTRBLength = 0x1000;
    2412           InputContext->EP[Dci-1].MaxESITPayload   = EpDesc->MaxPacketSize;
    2413           //
    2414           // Get the bInterval from descriptor and init the the interval field of endpoint context
    2415           //
    2416           if ((DeviceSpeed == EFI_USB_SPEED_FULL) || (DeviceSpeed == EFI_USB_SPEED_LOW)) {
    2417             Interval = EpDesc->Interval;
    2418             //
    2419             // Hard code the interval to MAX first, need calculate through the bInterval field of Endpoint descriptor.
    2420             //
    2421             InputContext->EP[Dci-1].Interval = 6;
    2422           } else if ((DeviceSpeed == EFI_USB_SPEED_HIGH) || (DeviceSpeed == EFI_USB_SPEED_SUPER)) {
    2423             Interval = EpDesc->Interval;
    2424             ASSERT (Interval >= 1 && Interval <= 16);
    2425             //
    2426             // Refer to XHCI 1.0 spec section 6.2.3.6, table 61
    2427             //
    2428             InputContext->EP[Dci-1].Interval         = Interval - 1;
    2429             InputContext->EP[Dci-1].AverageTRBLength = 0x1000;
    2430             InputContext->EP[Dci-1].MaxESITPayload   = 0x0002;
    2431             InputContext->EP[Dci-1].MaxBurstSize     = 0x0;
    2432             InputContext->EP[Dci-1].CErr             = 3;
    2433           }
    2434 
    2435           if (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] == NULL) {
    2436             EndpointTransferRing = AllocateZeroPool(sizeof (TRANSFER_RING));
    2437             Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] = (VOID *) EndpointTransferRing;
    2438             CreateTransferRing(Xhc, TR_RING_TRB_NUMBER, (TRANSFER_RING *)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1]);
    2439           }
    2440           break;
    2441 
    2442         case USB_ENDPOINT_CONTROL:
    2443         default:
    2444           ASSERT (0);
    2445           break;
    2446       }
    2447 
    2448       PhyAddr  = XHC_LOW_32BIT (((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1])->RingSeg0);
    2449       PhyAddr &= ~(0x0F);
    2450       PhyAddr |= ((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1])->RingPCS;
    2451       InputContext->EP[Dci-1].PtrLo = PhyAddr;
    2452       InputContext->EP[Dci-1].PtrHi = XHC_HIGH_32BIT (((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1])->RingSeg0);
    2453 
    2454       EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);
    2455     }
     2894    Dci = XhcInitializeEndpointContext (Xhc, SlotId, DeviceSpeed, InputContext, IfDesc);
     2895    if (Dci > MaxDci) {
     2896      MaxDci = Dci;
     2897    }
     2898
    24562899    IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length);
    24572900  }
     
    24632906  //
    24642907  ZeroMem (&CmdTrbCfgEP, sizeof (CmdTrbCfgEP));
    2465   CmdTrbCfgEP.PtrLo    = XHC_LOW_32BIT (InputContext);
    2466   CmdTrbCfgEP.PtrHi    = XHC_HIGH_32BIT (InputContext);
     2908  PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, InputContext, sizeof (INPUT_CONTEXT));
     2909  CmdTrbCfgEP.PtrLo    = XHC_LOW_32BIT (PhyAddr);
     2910  CmdTrbCfgEP.PtrHi    = XHC_HIGH_32BIT (PhyAddr);
    24672911  CmdTrbCfgEP.CycleBit = 1;
    24682912  CmdTrbCfgEP.Type     = TRB_TYPE_CON_ENDPOINT;
     
    24752919             (TRB_TEMPLATE **) (UINTN) &EvtTrb
    24762920             );
    2477   ASSERT_EFI_ERROR(Status);
     2921  if (EFI_ERROR (Status)) {
     2922    DEBUG ((EFI_D_ERROR, "XhcSetConfigCmd: Config Endpoint Failed, Status = %r\n", Status));
     2923  } else {
     2924    Xhc->UsbDevContext[SlotId].ActiveConfiguration = ConfigDesc->ConfigurationValue;
     2925  }
    24782926
    24792927  return Status;
     
    25012949{
    25022950  EFI_STATUS                  Status;
    2503 
    25042951  USB_INTERFACE_DESCRIPTOR    *IfDesc;
    2505   USB_ENDPOINT_DESCRIPTOR     *EpDesc;
    25062952  UINT8                       Index;
    2507   UINTN                       NumEp;
    2508   UINTN                       EpIndex;
    2509   UINT8                       EpAddr;
    2510   UINT8                       Direction;
    25112953  UINT8                       Dci;
    25122954  UINT8                       MaxDci;
    2513   UINT32                      PhyAddr;
    2514   UINT8                       Interval;
    2515 
    2516   TRANSFER_RING               *EndpointTransferRing;
     2955  EFI_PHYSICAL_ADDRESS        PhyAddr;
     2956
    25172957  CMD_TRB_CONFIG_ENDPOINT     CmdTrbCfgEP;
    25182958  INPUT_CONTEXT_64            *InputContext;
     
    25332973  IfDesc = (USB_INTERFACE_DESCRIPTOR *)(ConfigDesc + 1);
    25342974  for (Index = 0; Index < ConfigDesc->NumInterfaces; Index++) {
    2535     while (IfDesc->DescriptorType != USB_DESC_TYPE_INTERFACE) {
     2975    while ((IfDesc->DescriptorType != USB_DESC_TYPE_INTERFACE) || (IfDesc->AlternateSetting != 0)) {
    25362976      IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length);
    25372977    }
    25382978
    2539     NumEp = IfDesc->NumEndpoints;
    2540 
    2541     EpDesc = (USB_ENDPOINT_DESCRIPTOR *)(IfDesc + 1);
    2542     for (EpIndex = 0; EpIndex < NumEp; EpIndex++) {
    2543       while (EpDesc->DescriptorType != USB_DESC_TYPE_ENDPOINT) {
    2544         EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);
    2545       }
    2546 
    2547       EpAddr    = (UINT8)(EpDesc->EndpointAddress & 0x0F);
    2548       Direction = (UINT8)((EpDesc->EndpointAddress & 0x80) ? EfiUsbDataIn : EfiUsbDataOut);
    2549 
    2550       Dci = XhcEndpointToDci (EpAddr, Direction);
    2551       ASSERT (Dci < 32);
    2552       if (Dci > MaxDci) {
    2553         MaxDci = Dci;
    2554       }
    2555 
    2556       InputContext->InputControlContext.Dword2 |= (BIT0 << Dci);
    2557       InputContext->EP[Dci-1].MaxPacketSize     = EpDesc->MaxPacketSize;
    2558 
    2559       if (DeviceSpeed == EFI_USB_SPEED_SUPER) {
    2560         //
    2561         // 6.2.3.4, shall be set to the value defined in the bMaxBurst field of the SuperSpeed Endpoint Companion Descriptor.
    2562         //
    2563         InputContext->EP[Dci-1].MaxBurstSize = 0x0;
    2564       } else {
    2565         InputContext->EP[Dci-1].MaxBurstSize = 0x0;
    2566       }
    2567 
    2568       switch (EpDesc->Attributes & USB_ENDPOINT_TYPE_MASK) {
    2569         case USB_ENDPOINT_BULK:
    2570           if (Direction == EfiUsbDataIn) {
    2571             InputContext->EP[Dci-1].CErr   = 3;
    2572             InputContext->EP[Dci-1].EPType = ED_BULK_IN;
    2573           } else {
    2574             InputContext->EP[Dci-1].CErr   = 3;
    2575             InputContext->EP[Dci-1].EPType = ED_BULK_OUT;
    2576           }
    2577 
    2578           InputContext->EP[Dci-1].AverageTRBLength = 0x1000;
    2579           if (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] == NULL) {
    2580             EndpointTransferRing = AllocateZeroPool(sizeof (TRANSFER_RING));
    2581             Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] = (VOID *) EndpointTransferRing;
    2582             CreateTransferRing(Xhc, TR_RING_TRB_NUMBER, (TRANSFER_RING *)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1]);
    2583           }
    2584 
    2585           break;
    2586         case USB_ENDPOINT_ISO:
    2587           if (Direction == EfiUsbDataIn) {
    2588             InputContext->EP[Dci-1].CErr   = 0;
    2589             InputContext->EP[Dci-1].EPType = ED_ISOCH_IN;
    2590           } else {
    2591             InputContext->EP[Dci-1].CErr   = 0;
    2592             InputContext->EP[Dci-1].EPType = ED_ISOCH_OUT;
    2593           }
    2594           break;
    2595         case USB_ENDPOINT_INTERRUPT:
    2596           if (Direction == EfiUsbDataIn) {
    2597             InputContext->EP[Dci-1].CErr   = 3;
    2598             InputContext->EP[Dci-1].EPType = ED_INTERRUPT_IN;
    2599           } else {
    2600             InputContext->EP[Dci-1].CErr   = 3;
    2601             InputContext->EP[Dci-1].EPType = ED_INTERRUPT_OUT;
    2602           }
    2603           InputContext->EP[Dci-1].AverageTRBLength = 0x1000;
    2604           InputContext->EP[Dci-1].MaxESITPayload   = EpDesc->MaxPacketSize;
    2605           //
    2606           // Get the bInterval from descriptor and init the the interval field of endpoint context
    2607           //
    2608           if ((DeviceSpeed == EFI_USB_SPEED_FULL) || (DeviceSpeed == EFI_USB_SPEED_LOW)) {
    2609             Interval = EpDesc->Interval;
    2610             //
    2611             // Hard code the interval to MAX first, need calculate through the bInterval field of Endpoint descriptor.
    2612             //
    2613             InputContext->EP[Dci-1].Interval = 6;
    2614           } else if ((DeviceSpeed == EFI_USB_SPEED_HIGH) || (DeviceSpeed == EFI_USB_SPEED_SUPER)) {
    2615             Interval = EpDesc->Interval;
    2616             ASSERT (Interval >= 1 && Interval <= 16);
    2617             //
    2618             // Refer to XHCI 1.0 spec section 6.2.3.6, table 61
    2619             //
    2620             InputContext->EP[Dci-1].Interval         = Interval - 1;
    2621             InputContext->EP[Dci-1].AverageTRBLength = 0x1000;
    2622             InputContext->EP[Dci-1].MaxESITPayload   = 0x0002;
    2623             InputContext->EP[Dci-1].MaxBurstSize     = 0x0;
    2624             InputContext->EP[Dci-1].CErr             = 3;
    2625           }
    2626 
    2627           if (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] == NULL) {
    2628             EndpointTransferRing = AllocateZeroPool(sizeof (TRANSFER_RING));
    2629             Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1] = (VOID *) EndpointTransferRing;
    2630             CreateTransferRing(Xhc, TR_RING_TRB_NUMBER, (TRANSFER_RING *)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1]);
    2631           }
    2632           break;
    2633 
    2634         case USB_ENDPOINT_CONTROL:
    2635         default:
    2636           ASSERT (0);
    2637           break;
    2638       }
    2639 
    2640       PhyAddr  = XHC_LOW_32BIT (((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1])->RingSeg0);
    2641       PhyAddr &= ~(0x0F);
    2642       PhyAddr |= ((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1])->RingPCS;
    2643       InputContext->EP[Dci-1].PtrLo = PhyAddr;
    2644       InputContext->EP[Dci-1].PtrHi = XHC_HIGH_32BIT (((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1])->RingSeg0);
    2645 
    2646       EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);
    2647     }
     2979    Dci = XhcInitializeEndpointContext64 (Xhc, SlotId, DeviceSpeed, InputContext, IfDesc);
     2980    if (Dci > MaxDci) {
     2981      MaxDci = Dci;
     2982    }
     2983 
    26482984    IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length);
    26492985  }
     
    26552991  //
    26562992  ZeroMem (&CmdTrbCfgEP, sizeof (CmdTrbCfgEP));
    2657   CmdTrbCfgEP.PtrLo    = XHC_LOW_32BIT (InputContext);
    2658   CmdTrbCfgEP.PtrHi    = XHC_HIGH_32BIT (InputContext);
     2993  PhyAddr  = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, InputContext, sizeof (INPUT_CONTEXT_64));
     2994  CmdTrbCfgEP.PtrLo    = XHC_LOW_32BIT (PhyAddr);
     2995  CmdTrbCfgEP.PtrHi    = XHC_HIGH_32BIT (PhyAddr);
    26592996  CmdTrbCfgEP.CycleBit = 1;
    26602997  CmdTrbCfgEP.Type     = TRB_TYPE_CON_ENDPOINT;
     
    26673004             (TRB_TEMPLATE **) (UINTN) &EvtTrb
    26683005             );
    2669   ASSERT_EFI_ERROR(Status);
     3006  if (EFI_ERROR (Status)) {
     3007    DEBUG ((EFI_D_ERROR, "XhcSetConfigCmd64: Config Endpoint Failed, Status = %r\n", Status));
     3008  } else {
     3009    Xhc->UsbDevContext[SlotId].ActiveConfiguration = ConfigDesc->ConfigurationValue;
     3010  }
    26703011
    26713012  return Status;
    26723013}
    26733014
     3015/**
     3016  Stop endpoint through XHCI's Stop_Endpoint cmd.
     3017
     3018  @param  Xhc                   The XHCI Instance.
     3019  @param  SlotId                The slot id to be configured.
     3020  @param  Dci                   The device context index of endpoint.
     3021
     3022  @retval EFI_SUCCESS           Stop endpoint successfully.
     3023  @retval Others                Failed to stop endpoint.
     3024
     3025**/
     3026EFI_STATUS
     3027EFIAPI
     3028XhcStopEndpoint (
     3029  IN USB_XHCI_INSTANCE      *Xhc,
     3030  IN UINT8                  SlotId,
     3031  IN UINT8                  Dci
     3032  )
     3033{
     3034  EFI_STATUS                    Status;
     3035  EVT_TRB_COMMAND_COMPLETION    *EvtTrb;
     3036  CMD_TRB_STOP_ENDPOINT         CmdTrbStopED;
     3037
     3038  DEBUG ((EFI_D_INFO, "XhcStopEndpoint: Slot = 0x%x, Dci = 0x%x\n", SlotId, Dci));
     3039
     3040  //
     3041  // Send stop endpoint command to transit Endpoint from running to stop state
     3042  //
     3043  ZeroMem (&CmdTrbStopED, sizeof (CmdTrbStopED));
     3044  CmdTrbStopED.CycleBit = 1;
     3045  CmdTrbStopED.Type     = TRB_TYPE_STOP_ENDPOINT;
     3046  CmdTrbStopED.EDID     = Dci;
     3047  CmdTrbStopED.SlotId   = SlotId;
     3048  Status = XhcCmdTransfer (
     3049             Xhc,
     3050             (TRB_TEMPLATE *) (UINTN) &CmdTrbStopED,
     3051             XHC_GENERIC_TIMEOUT,
     3052             (TRB_TEMPLATE **) (UINTN) &EvtTrb
     3053             );
     3054  if (EFI_ERROR(Status)) {
     3055    DEBUG ((EFI_D_ERROR, "XhcStopEndpoint: Stop Endpoint Failed, Status = %r\n", Status));
     3056  }
     3057
     3058  return Status;
     3059}
     3060
     3061/**
     3062  Set interface through XHCI's Configure_Endpoint cmd.
     3063
     3064  @param  Xhc           The XHCI Instance.
     3065  @param  SlotId        The slot id to be configured.
     3066  @param  DeviceSpeed   The device's speed.
     3067  @param  ConfigDesc    The pointer to the usb device configuration descriptor.
     3068  @param  Request       USB device request to send.
     3069
     3070  @retval EFI_SUCCESS   Successfully set interface.
     3071
     3072**/
     3073EFI_STATUS
     3074EFIAPI
     3075XhcSetInterface (
     3076  IN USB_XHCI_INSTANCE        *Xhc,
     3077  IN UINT8                    SlotId,
     3078  IN UINT8                    DeviceSpeed,
     3079  IN USB_CONFIG_DESCRIPTOR    *ConfigDesc,
     3080  IN EFI_USB_DEVICE_REQUEST   *Request
     3081  )
     3082{
     3083  EFI_STATUS                  Status;
     3084  USB_INTERFACE_DESCRIPTOR    *IfDescActive;
     3085  USB_INTERFACE_DESCRIPTOR    *IfDescSet;
     3086  USB_INTERFACE_DESCRIPTOR    *IfDesc;
     3087  USB_ENDPOINT_DESCRIPTOR     *EpDesc;
     3088  UINTN                       NumEp;
     3089  UINTN                       EpIndex;
     3090  UINT8                       EpAddr;
     3091  UINT8                       Direction;
     3092  UINT8                       Dci;
     3093  UINT8                       MaxDci;
     3094  EFI_PHYSICAL_ADDRESS        PhyAddr;
     3095  VOID                        *RingSeg;
     3096
     3097  CMD_TRB_CONFIG_ENDPOINT     CmdTrbCfgEP;
     3098  INPUT_CONTEXT               *InputContext;
     3099  DEVICE_CONTEXT              *OutputContext;
     3100  EVT_TRB_COMMAND_COMPLETION  *EvtTrb;
     3101
     3102  Status = EFI_SUCCESS;
     3103
     3104  InputContext  = Xhc->UsbDevContext[SlotId].InputContext;
     3105  OutputContext = Xhc->UsbDevContext[SlotId].OutputContext;
     3106  //
     3107  // XHCI 4.6.6 Configure Endpoint
     3108  // When this command is used to "Set an Alternate Interface on a device", software shall set the Drop
     3109  // Context and Add Context flags as follows:
     3110  // 1) If an endpoint is not modified by the Alternate Interface setting, then software shall set the Drop
     3111  // Context and Add Context flags to '0'.
     3112  //
     3113  // Except the interface indicated by Reqeust->Index, no impact to other interfaces.
     3114  // So the default Drop Context and Add Context flags can be '0' to cover 1).
     3115  //
     3116  ZeroMem (InputContext, sizeof (INPUT_CONTEXT));
     3117  CopyMem (&InputContext->Slot, &OutputContext->Slot, sizeof (SLOT_CONTEXT));
     3118
     3119  ASSERT (ConfigDesc != NULL);
     3120
     3121  MaxDci = 0;
     3122
     3123  IfDescActive = NULL;
     3124  IfDescSet = NULL;
     3125
     3126  IfDesc = (USB_INTERFACE_DESCRIPTOR *)(ConfigDesc + 1);
     3127  while ((UINTN) IfDesc < ((UINTN) ConfigDesc + ConfigDesc->TotalLength)) {
     3128    if (IfDesc->DescriptorType == USB_DESC_TYPE_INTERFACE) {
     3129      if (IfDesc->InterfaceNumber == (UINT8) Request->Index) {
     3130        if (IfDesc->AlternateSetting == Xhc->UsbDevContext[SlotId].ActiveAlternateSetting[IfDesc->InterfaceNumber]) {
     3131          //
     3132          // Find out the active interface descriptor.
     3133          //
     3134          IfDescActive = IfDesc;
     3135        } else if (IfDesc->AlternateSetting == (UINT8) Request->Value) {
     3136          //
     3137          // Find out the interface descriptor to set.
     3138          //
     3139          IfDescSet = IfDesc;
     3140        }
     3141      }
     3142    }
     3143    IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length);
     3144  }
     3145
     3146  //
     3147  // XHCI 4.6.6 Configure Endpoint
     3148  // When this command is used to "Set an Alternate Interface on a device", software shall set the Drop
     3149  // Context and Add Context flags as follows:
     3150  // 2) If an endpoint previously disabled, is enabled by the Alternate Interface setting, then software shall set
     3151  // the Drop Context flag to '0' and Add Context flag to '1', and initialize the Input Endpoint Context.
     3152  // 3) If an endpoint previously enabled, is disabled by the Alternate Interface setting, then software shall set
     3153  // the Drop Context flag to '1' and Add Context flag to '0'.
     3154  // 4) If a parameter of an enabled endpoint is modified by an Alternate Interface setting, the Drop Context
     3155  // and Add Context flags shall be set to '1'.
     3156  //
     3157  // Below codes are to cover 2), 3) and 4).
     3158  //
     3159
     3160  if ((IfDescActive != NULL) && (IfDescSet != NULL)) {
     3161    NumEp = IfDescActive->NumEndpoints;
     3162    EpDesc = (USB_ENDPOINT_DESCRIPTOR *) (IfDescActive + 1);
     3163    for (EpIndex = 0; EpIndex < NumEp; EpIndex++) {
     3164      while (EpDesc->DescriptorType != USB_DESC_TYPE_ENDPOINT) {
     3165        EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);
     3166      }
     3167
     3168      EpAddr    = (UINT8) (EpDesc->EndpointAddress & 0x0F);
     3169      Direction = (UINT8) ((EpDesc->EndpointAddress & 0x80) ? EfiUsbDataIn : EfiUsbDataOut);
     3170
     3171      Dci = XhcEndpointToDci (EpAddr, Direction);
     3172      ASSERT (Dci < 32);
     3173      if (Dci > MaxDci) {
     3174        MaxDci = Dci;
     3175      }
     3176      //
     3177      // XHCI 4.3.6 - Setting Alternate Interfaces
     3178      // 1) Stop any Running Transfer Rings affected by the Alternate Interface setting.
     3179      //
     3180      Status = XhcStopEndpoint (Xhc, SlotId, Dci);
     3181      if (EFI_ERROR (Status)) {
     3182        return Status;
     3183      }
     3184      //
     3185      // XHCI 4.3.6 - Setting Alternate Interfaces
     3186      // 2) Free Transfer Rings of all endpoints that will be affected by the Alternate Interface setting.
     3187      //
     3188      if (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci - 1] != NULL) {
     3189        RingSeg = ((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci - 1])->RingSeg0;
     3190        if (RingSeg != NULL) {
     3191          UsbHcFreeMem (Xhc->MemPool, RingSeg, sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER);
     3192        }
     3193        FreePool (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci - 1]);
     3194        Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci - 1] = NULL;
     3195      }
     3196
     3197      //
     3198      // Set the Drop Context flag to '1'.
     3199      //
     3200      InputContext->InputControlContext.Dword1 |= (BIT0 << Dci);
     3201
     3202      EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);
     3203    }
     3204
     3205    //
     3206    // XHCI 4.3.6 - Setting Alternate Interfaces
     3207    // 3) Clear all the Endpoint Context fields of each endpoint that will be disabled by the Alternate
     3208    // Interface setting, to '0'.
     3209    //
     3210    // The step 3) has been covered by the ZeroMem () to InputContext at the start of the function.
     3211    //
     3212
     3213    //
     3214    // XHCI 4.3.6 - Setting Alternate Interfaces
     3215    // 4) For each endpoint enabled by the Configure Endpoint Command:
     3216    //   a. Allocate a Transfer Ring.
     3217    //   b. Initialize the Transfer Ring Segment(s) by clearing all fields of all TRBs to '0'.
     3218    //   c. Initialize the Endpoint Context data structure.
     3219    //
     3220    Dci = XhcInitializeEndpointContext (Xhc, SlotId, DeviceSpeed, InputContext, IfDescSet);
     3221    if (Dci > MaxDci) {
     3222      MaxDci = Dci;
     3223    }
     3224
     3225    InputContext->InputControlContext.Dword2 |= BIT0;
     3226    InputContext->Slot.ContextEntries         = MaxDci;
     3227    //
     3228    // XHCI 4.3.6 - Setting Alternate Interfaces
     3229    // 5) Issue and successfully complete a Configure Endpoint Command.
     3230    //
     3231    ZeroMem (&CmdTrbCfgEP, sizeof (CmdTrbCfgEP));
     3232    PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, InputContext, sizeof (INPUT_CONTEXT));
     3233    CmdTrbCfgEP.PtrLo    = XHC_LOW_32BIT (PhyAddr);
     3234    CmdTrbCfgEP.PtrHi    = XHC_HIGH_32BIT (PhyAddr);
     3235    CmdTrbCfgEP.CycleBit = 1;
     3236    CmdTrbCfgEP.Type     = TRB_TYPE_CON_ENDPOINT;
     3237    CmdTrbCfgEP.SlotId   = Xhc->UsbDevContext[SlotId].SlotId;
     3238    DEBUG ((EFI_D_INFO, "SetInterface: Configure Endpoint\n"));
     3239    Status = XhcCmdTransfer (
     3240               Xhc,
     3241               (TRB_TEMPLATE *) (UINTN) &CmdTrbCfgEP,
     3242               XHC_GENERIC_TIMEOUT,
     3243               (TRB_TEMPLATE **) (UINTN) &EvtTrb
     3244               );
     3245    if (EFI_ERROR (Status)) {
     3246      DEBUG ((EFI_D_ERROR, "SetInterface: Config Endpoint Failed, Status = %r\n", Status));
     3247    } else {
     3248      //
     3249      // Update the active AlternateSetting.
     3250      //
     3251      Xhc->UsbDevContext[SlotId].ActiveAlternateSetting[(UINT8) Request->Index] = (UINT8) Request->Value;
     3252    }
     3253  }
     3254
     3255  return Status;
     3256}
     3257
     3258/**
     3259  Set interface through XHCI's Configure_Endpoint cmd.
     3260
     3261  @param  Xhc           The XHCI Instance.
     3262  @param  SlotId        The slot id to be configured.
     3263  @param  DeviceSpeed   The device's speed.
     3264  @param  ConfigDesc    The pointer to the usb device configuration descriptor.
     3265  @param  Request       USB device request to send.
     3266
     3267  @retval EFI_SUCCESS   Successfully set interface.
     3268
     3269**/
     3270EFI_STATUS
     3271EFIAPI
     3272XhcSetInterface64 (
     3273  IN USB_XHCI_INSTANCE        *Xhc,
     3274  IN UINT8                    SlotId,
     3275  IN UINT8                    DeviceSpeed,
     3276  IN USB_CONFIG_DESCRIPTOR    *ConfigDesc,
     3277  IN EFI_USB_DEVICE_REQUEST   *Request
     3278  )
     3279{
     3280  EFI_STATUS                  Status;
     3281  USB_INTERFACE_DESCRIPTOR    *IfDescActive;
     3282  USB_INTERFACE_DESCRIPTOR    *IfDescSet;
     3283  USB_INTERFACE_DESCRIPTOR    *IfDesc;
     3284  USB_ENDPOINT_DESCRIPTOR     *EpDesc;
     3285  UINTN                       NumEp;
     3286  UINTN                       EpIndex;
     3287  UINT8                       EpAddr;
     3288  UINT8                       Direction;
     3289  UINT8                       Dci;
     3290  UINT8                       MaxDci;
     3291  EFI_PHYSICAL_ADDRESS        PhyAddr;
     3292  VOID                        *RingSeg;
     3293
     3294  CMD_TRB_CONFIG_ENDPOINT     CmdTrbCfgEP;
     3295  INPUT_CONTEXT_64            *InputContext;
     3296  DEVICE_CONTEXT_64           *OutputContext;
     3297  EVT_TRB_COMMAND_COMPLETION  *EvtTrb;
     3298
     3299  Status = EFI_SUCCESS;
     3300
     3301  InputContext  = Xhc->UsbDevContext[SlotId].InputContext;
     3302  OutputContext = Xhc->UsbDevContext[SlotId].OutputContext;
     3303  //
     3304  // XHCI 4.6.6 Configure Endpoint
     3305  // When this command is used to "Set an Alternate Interface on a device", software shall set the Drop
     3306  // Context and Add Context flags as follows:
     3307  // 1) If an endpoint is not modified by the Alternate Interface setting, then software shall set the Drop
     3308  // Context and Add Context flags to '0'.
     3309  //
     3310  // Except the interface indicated by Reqeust->Index, no impact to other interfaces.
     3311  // So the default Drop Context and Add Context flags can be '0' to cover 1).
     3312  //
     3313  ZeroMem (InputContext, sizeof (INPUT_CONTEXT_64));
     3314  CopyMem (&InputContext->Slot, &OutputContext->Slot, sizeof (SLOT_CONTEXT_64));
     3315
     3316  ASSERT (ConfigDesc != NULL);
     3317
     3318  MaxDci = 0;
     3319
     3320  IfDescActive = NULL;
     3321  IfDescSet = NULL;
     3322
     3323  IfDesc = (USB_INTERFACE_DESCRIPTOR *)(ConfigDesc + 1);
     3324  while ((UINTN) IfDesc < ((UINTN) ConfigDesc + ConfigDesc->TotalLength)) {
     3325    if (IfDesc->DescriptorType == USB_DESC_TYPE_INTERFACE) {
     3326      if (IfDesc->InterfaceNumber == (UINT8) Request->Index) {
     3327        if (IfDesc->AlternateSetting == Xhc->UsbDevContext[SlotId].ActiveAlternateSetting[IfDesc->InterfaceNumber]) {
     3328          //
     3329          // Find out the active interface descriptor.
     3330          //
     3331          IfDescActive = IfDesc;
     3332        } else if (IfDesc->AlternateSetting == (UINT8) Request->Value) {
     3333          //
     3334          // Find out the interface descriptor to set.
     3335          //
     3336          IfDescSet = IfDesc;
     3337        }
     3338      }
     3339    }
     3340    IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length);
     3341  }
     3342
     3343  //
     3344  // XHCI 4.6.6 Configure Endpoint
     3345  // When this command is used to "Set an Alternate Interface on a device", software shall set the Drop
     3346  // Context and Add Context flags as follows:
     3347  // 2) If an endpoint previously disabled, is enabled by the Alternate Interface setting, then software shall set
     3348  // the Drop Context flag to '0' and Add Context flag to '1', and initialize the Input Endpoint Context.
     3349  // 3) If an endpoint previously enabled, is disabled by the Alternate Interface setting, then software shall set
     3350  // the Drop Context flag to '1' and Add Context flag to '0'.
     3351  // 4) If a parameter of an enabled endpoint is modified by an Alternate Interface setting, the Drop Context
     3352  // and Add Context flags shall be set to '1'.
     3353  //
     3354  // Below codes are to cover 2), 3) and 4).
     3355  //
     3356
     3357  if ((IfDescActive != NULL) && (IfDescSet != NULL)) {
     3358    NumEp = IfDescActive->NumEndpoints;
     3359    EpDesc = (USB_ENDPOINT_DESCRIPTOR *) (IfDescActive + 1);
     3360    for (EpIndex = 0; EpIndex < NumEp; EpIndex++) {
     3361      while (EpDesc->DescriptorType != USB_DESC_TYPE_ENDPOINT) {
     3362        EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);
     3363      }
     3364
     3365      EpAddr    = (UINT8) (EpDesc->EndpointAddress & 0x0F);
     3366      Direction = (UINT8) ((EpDesc->EndpointAddress & 0x80) ? EfiUsbDataIn : EfiUsbDataOut);
     3367
     3368      Dci = XhcEndpointToDci (EpAddr, Direction);
     3369      ASSERT (Dci < 32);
     3370      if (Dci > MaxDci) {
     3371        MaxDci = Dci;
     3372      }
     3373      //
     3374      // XHCI 4.3.6 - Setting Alternate Interfaces
     3375      // 1) Stop any Running Transfer Rings affected by the Alternate Interface setting.
     3376      //
     3377      Status = XhcStopEndpoint (Xhc, SlotId, Dci);
     3378      if (EFI_ERROR (Status)) {
     3379        return Status;
     3380      }
     3381      //
     3382      // XHCI 4.3.6 - Setting Alternate Interfaces
     3383      // 2) Free Transfer Rings of all endpoints that will be affected by the Alternate Interface setting.
     3384      //
     3385      if (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci - 1] != NULL) {
     3386        RingSeg = ((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci - 1])->RingSeg0;
     3387        if (RingSeg != NULL) {
     3388          UsbHcFreeMem (Xhc->MemPool, RingSeg, sizeof (TRB_TEMPLATE) * TR_RING_TRB_NUMBER);
     3389        }
     3390        FreePool (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci - 1]);
     3391        Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci - 1] = NULL;
     3392      }
     3393
     3394      //
     3395      // Set the Drop Context flag to '1'.
     3396      //
     3397      InputContext->InputControlContext.Dword1 |= (BIT0 << Dci);
     3398
     3399      EpDesc = (USB_ENDPOINT_DESCRIPTOR *)((UINTN)EpDesc + EpDesc->Length);
     3400    }
     3401
     3402    //
     3403    // XHCI 4.3.6 - Setting Alternate Interfaces
     3404    // 3) Clear all the Endpoint Context fields of each endpoint that will be disabled by the Alternate
     3405    // Interface setting, to '0'.
     3406    //
     3407    // The step 3) has been covered by the ZeroMem () to InputContext at the start of the function.
     3408    //
     3409
     3410    //
     3411    // XHCI 4.3.6 - Setting Alternate Interfaces
     3412    // 4) For each endpoint enabled by the Configure Endpoint Command:
     3413    //   a. Allocate a Transfer Ring.
     3414    //   b. Initialize the Transfer Ring Segment(s) by clearing all fields of all TRBs to '0'.
     3415    //   c. Initialize the Endpoint Context data structure.
     3416    //
     3417    Dci = XhcInitializeEndpointContext64 (Xhc, SlotId, DeviceSpeed, InputContext, IfDescSet);
     3418    if (Dci > MaxDci) {
     3419      MaxDci = Dci;
     3420    }
     3421
     3422    InputContext->InputControlContext.Dword2 |= BIT0;
     3423    InputContext->Slot.ContextEntries         = MaxDci;
     3424    //
     3425    // XHCI 4.3.6 - Setting Alternate Interfaces
     3426    // 5) Issue and successfully complete a Configure Endpoint Command.
     3427    //
     3428    ZeroMem (&CmdTrbCfgEP, sizeof (CmdTrbCfgEP));
     3429    PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, InputContext, sizeof (INPUT_CONTEXT_64));
     3430    CmdTrbCfgEP.PtrLo    = XHC_LOW_32BIT (PhyAddr);
     3431    CmdTrbCfgEP.PtrHi    = XHC_HIGH_32BIT (PhyAddr);
     3432    CmdTrbCfgEP.CycleBit = 1;
     3433    CmdTrbCfgEP.Type     = TRB_TYPE_CON_ENDPOINT;
     3434    CmdTrbCfgEP.SlotId   = Xhc->UsbDevContext[SlotId].SlotId;
     3435    DEBUG ((EFI_D_INFO, "SetInterface64: Configure Endpoint\n"));
     3436    Status = XhcCmdTransfer (
     3437               Xhc,
     3438               (TRB_TEMPLATE *) (UINTN) &CmdTrbCfgEP,
     3439               XHC_GENERIC_TIMEOUT,
     3440               (TRB_TEMPLATE **) (UINTN) &EvtTrb
     3441               );
     3442    if (EFI_ERROR (Status)) {
     3443      DEBUG ((EFI_D_ERROR, "SetInterface64: Config Endpoint Failed, Status = %r\n", Status));
     3444    } else {
     3445      //
     3446      // Update the active AlternateSetting.
     3447      //
     3448      Xhc->UsbDevContext[SlotId].ActiveAlternateSetting[(UINT8) Request->Index] = (UINT8) Request->Value;
     3449    }
     3450  }
     3451
     3452  return Status;
     3453}
    26743454
    26753455/**
     
    26953475  EVT_TRB_COMMAND_COMPLETION  *EvtTrb;
    26963476  INPUT_CONTEXT               *InputContext;
     3477  EFI_PHYSICAL_ADDRESS        PhyAddr;
    26973478
    26983479  ASSERT (Xhc->UsbDevContext[SlotId].SlotId != 0);
     
    27083489
    27093490  ZeroMem (&CmdTrbEvalu, sizeof (CmdTrbEvalu));
    2710   CmdTrbEvalu.PtrLo    = XHC_LOW_32BIT (InputContext);
    2711   CmdTrbEvalu.PtrHi    = XHC_HIGH_32BIT (InputContext);
     3491  PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, InputContext, sizeof (INPUT_CONTEXT));
     3492  CmdTrbEvalu.PtrLo    = XHC_LOW_32BIT (PhyAddr);
     3493  CmdTrbEvalu.PtrHi    = XHC_HIGH_32BIT (PhyAddr);
    27123494  CmdTrbEvalu.CycleBit = 1;
    27133495  CmdTrbEvalu.Type     = TRB_TYPE_EVALU_CONTXT;
     
    27203502             (TRB_TEMPLATE **) (UINTN) &EvtTrb
    27213503             );
    2722   ASSERT (!EFI_ERROR(Status));
    2723 
     3504  if (EFI_ERROR (Status)) {
     3505    DEBUG ((EFI_D_ERROR, "XhcEvaluateContext: Evaluate Context Failed, Status = %r\n", Status));
     3506  }
    27243507  return Status;
    27253508}
     
    27473530  EVT_TRB_COMMAND_COMPLETION  *EvtTrb;
    27483531  INPUT_CONTEXT_64            *InputContext;
     3532  EFI_PHYSICAL_ADDRESS        PhyAddr;
    27493533
    27503534  ASSERT (Xhc->UsbDevContext[SlotId].SlotId != 0);
     
    27603544
    27613545  ZeroMem (&CmdTrbEvalu, sizeof (CmdTrbEvalu));
    2762   CmdTrbEvalu.PtrLo    = XHC_LOW_32BIT (InputContext);
    2763   CmdTrbEvalu.PtrHi    = XHC_HIGH_32BIT (InputContext);
     3546  PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, InputContext, sizeof (INPUT_CONTEXT_64));
     3547  CmdTrbEvalu.PtrLo    = XHC_LOW_32BIT (PhyAddr);
     3548  CmdTrbEvalu.PtrHi    = XHC_HIGH_32BIT (PhyAddr);
    27643549  CmdTrbEvalu.CycleBit = 1;
    27653550  CmdTrbEvalu.Type     = TRB_TYPE_EVALU_CONTXT;
     
    27723557             (TRB_TEMPLATE **) (UINTN) &EvtTrb
    27733558             );
    2774   ASSERT (!EFI_ERROR(Status));
    2775 
     3559  if (EFI_ERROR (Status)) {
     3560    DEBUG ((EFI_D_ERROR, "XhcEvaluateContext64: Evaluate Context Failed, Status = %r\n", Status));
     3561  }
    27763562  return Status;
    27773563}
     
    28003586{
    28013587  EFI_STATUS                  Status;
    2802 
    28033588  EVT_TRB_COMMAND_COMPLETION  *EvtTrb;
    28043589  INPUT_CONTEXT               *InputContext;
    28053590  DEVICE_CONTEXT              *OutputContext;
    28063591  CMD_TRB_CONFIG_ENDPOINT     CmdTrbCfgEP;
     3592  EFI_PHYSICAL_ADDRESS        PhyAddr;
    28073593
    28083594  ASSERT (Xhc->UsbDevContext[SlotId].SlotId != 0);
     
    28273613
    28283614  ZeroMem (&CmdTrbCfgEP, sizeof (CmdTrbCfgEP));
    2829   CmdTrbCfgEP.PtrLo    = XHC_LOW_32BIT (InputContext);
    2830   CmdTrbCfgEP.PtrHi    = XHC_HIGH_32BIT (InputContext);
     3615  PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, InputContext, sizeof (INPUT_CONTEXT));
     3616  CmdTrbCfgEP.PtrLo    = XHC_LOW_32BIT (PhyAddr);
     3617  CmdTrbCfgEP.PtrHi    = XHC_HIGH_32BIT (PhyAddr);
    28313618  CmdTrbCfgEP.CycleBit = 1;
    28323619  CmdTrbCfgEP.Type     = TRB_TYPE_CON_ENDPOINT;
     
    28393626              (TRB_TEMPLATE **) (UINTN) &EvtTrb
    28403627              );
    2841   ASSERT (!EFI_ERROR(Status));
    2842 
     3628  if (EFI_ERROR (Status)) {
     3629    DEBUG ((EFI_D_ERROR, "XhcConfigHubContext: Config Endpoint Failed, Status = %r\n", Status));
     3630  }
    28433631  return Status;
    28443632}
     
    28663654{
    28673655  EFI_STATUS                  Status;
    2868 
    28693656  EVT_TRB_COMMAND_COMPLETION  *EvtTrb;
    28703657  INPUT_CONTEXT_64            *InputContext;
    28713658  DEVICE_CONTEXT_64           *OutputContext;
    28723659  CMD_TRB_CONFIG_ENDPOINT     CmdTrbCfgEP;
     3660  EFI_PHYSICAL_ADDRESS        PhyAddr;
    28733661
    28743662  ASSERT (Xhc->UsbDevContext[SlotId].SlotId != 0);
     
    28933681
    28943682  ZeroMem (&CmdTrbCfgEP, sizeof (CmdTrbCfgEP));
    2895   CmdTrbCfgEP.PtrLo    = XHC_LOW_32BIT (InputContext);
    2896   CmdTrbCfgEP.PtrHi    = XHC_HIGH_32BIT (InputContext);
     3683  PhyAddr = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, InputContext, sizeof (INPUT_CONTEXT_64));
     3684  CmdTrbCfgEP.PtrLo    = XHC_LOW_32BIT (PhyAddr);
     3685  CmdTrbCfgEP.PtrHi    = XHC_HIGH_32BIT (PhyAddr);
    28973686  CmdTrbCfgEP.CycleBit = 1;
    28983687  CmdTrbCfgEP.Type     = TRB_TYPE_CON_ENDPOINT;
     
    29053694              (TRB_TEMPLATE **) (UINTN) &EvtTrb
    29063695              );
    2907   ASSERT (!EFI_ERROR(Status));
    2908 
     3696  if (EFI_ERROR (Status)) {
     3697    DEBUG ((EFI_D_ERROR, "XhcConfigHubContext64: Config Endpoint Failed, Status = %r\n", Status));
     3698  }
    29093699  return Status;
    29103700}
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette