X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdeModulePkg%2FBus%2FPci%2FEhciDxe%2FEhciSched.c;h=168280be81d7d7a4ce02dfc33d933db35a14b3cf;hb=7fb7259fc0ef9dc25fc17f437658562f0d6f9595;hp=b13e4078b0b85c543cc6f5f560bc07a0dbe1f0e0;hpb=913cb9dc645d6db47d8c2a0be0369083b8bed25d;p=mirror_edk2.git diff --git a/MdeModulePkg/Bus/Pci/EhciDxe/EhciSched.c b/MdeModulePkg/Bus/Pci/EhciDxe/EhciSched.c index b13e4078b0..168280be81 100644 --- a/MdeModulePkg/Bus/Pci/EhciDxe/EhciSched.c +++ b/MdeModulePkg/Bus/Pci/EhciDxe/EhciSched.c @@ -1,7 +1,9 @@ /** @file -Copyright (c) 2007, Intel Corporation -All rights reserved. This program and the accompanying materials + EHCI transfer scheduling routines. + +Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.
+This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at http://opensource.org/licenses/bsd-license.php @@ -9,31 +11,20 @@ http://opensource.org/licenses/bsd-license.php THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. -Module Name: - - EhciSched.c - -Abstract: - - EHCI transfer scheduling routines - -Revision History - **/ #include "Ehci.h" /** - Create helper QTD/QH for the EHCI device + Create helper QTD/QH for the EHCI device. - @param Ehc The EHCI device + @param Ehc The EHCI device. - @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for helper QTD/QH - @retval EFI_SUCCESS Helper QH/QTD are created + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for helper QTD/QH. + @retval EFI_SUCCESS Helper QH/QTD are created. **/ -STATIC EFI_STATUS EhcCreateHelpQ ( IN USB2_HC_DEV *Ehc @@ -43,11 +34,12 @@ EhcCreateHelpQ ( EHC_QH *Qh; QH_HW *QhHw; EHC_QTD *Qtd; + EFI_PHYSICAL_ADDRESS PciAddr; // // Create an inactive Qtd to terminate the short packet read. // - Qtd = EhcCreateQtd (Ehc, NULL, 0, QTD_PID_INPUT, 0, 64); + Qtd = EhcCreateQtd (Ehc, NULL, NULL, 0, QTD_PID_INPUT, 0, 64); if (Qtd == NULL) { return EFI_OUT_OF_RESOURCES; @@ -77,10 +69,12 @@ EhcCreateHelpQ ( return EFI_OUT_OF_RESOURCES; } + PciAddr = UsbHcGetPciAddressForHostMem (Ehc->MemPool, Qh, sizeof (EHC_QH)); QhHw = &Qh->QhHw; - QhHw->HorizonLink = QH_LINK (QhHw, EHC_TYPE_QH, FALSE); + QhHw->HorizonLink = QH_LINK (PciAddr + OFFSET_OF(EHC_QH, QhHw), EHC_TYPE_QH, FALSE); QhHw->Status = QTD_STAT_HALTED; QhHw->ReclaimHead = 1; + Qh->NextQh = Qh; Ehc->ReclaimHead = Qh; // @@ -102,14 +96,13 @@ EhcCreateHelpQ ( } - /** - Initialize the schedule data structure such as frame list + Initialize the schedule data structure such as frame list. - @param Ehc The EHCI device to init schedule data for + @param Ehc The EHCI device to init schedule data. - @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to init schedule data - @retval EFI_SUCCESS The schedule data is initialized + @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to init schedule data. + @retval EFI_SUCCESS The schedule data is initialized. **/ EFI_STATUS @@ -124,8 +117,8 @@ EhcInitSched ( UINTN Pages; UINTN Bytes; UINTN Index; - UINT32 *Desc; EFI_STATUS Status; + EFI_PHYSICAL_ADDRESS PciAddr; // // First initialize the periodical schedule data: @@ -166,10 +159,17 @@ EhcInitSched ( return EFI_OUT_OF_RESOURCES; } - Ehc->PeriodFrameHost = Buf; - Ehc->PeriodFrame = (VOID *) ((UINTN) PhyAddr); + Ehc->PeriodFrame = Buf; Ehc->PeriodFrameMap = Map; - Ehc->High32bitAddr = EHC_HIGH_32BIT (PhyAddr); + + // + // Program the FRAMELISTBASE register with the low 32 bit addr + // + EhcWriteOpReg (Ehc, EHC_FRAME_BASE_OFFSET, EHC_LOW_32BIT (PhyAddr)); + // + // Program the CTRLDSSEGMENT register with the high 32 bit addr + // + EhcWriteOpReg (Ehc, EHC_CTRLDSSEG_OFFSET, EHC_HIGH_32BIT (PhyAddr)); // // Init memory pool management then create the helper @@ -178,48 +178,80 @@ EhcInitSched ( // Ehc->MemPool = UsbHcInitMemPool ( PciIo, - EHC_BIT_IS_SET (Ehc->HcCapParams, HCCP_64BIT), - Ehc->High32bitAddr + Ehc->Support64BitDma, + EHC_HIGH_32BIT (PhyAddr) ); if (Ehc->MemPool == NULL) { - return EFI_OUT_OF_RESOURCES; + Status = EFI_OUT_OF_RESOURCES; + goto ErrorExit1; } Status = EhcCreateHelpQ (Ehc); if (EFI_ERROR (Status)) { - return Status; + goto ErrorExit; } // // Initialize the frame list entries then set the registers // - Desc = (UINT32 *) Ehc->PeriodFrame; + Ehc->PeriodFrameHost = AllocateZeroPool (EHC_FRAME_LEN * sizeof (UINTN)); + if (Ehc->PeriodFrameHost == NULL) { + Status = EFI_OUT_OF_RESOURCES; + goto ErrorExit; + } + + PciAddr = UsbHcGetPciAddressForHostMem (Ehc->MemPool, Ehc->PeriodOne, sizeof (EHC_QH)); for (Index = 0; Index < EHC_FRAME_LEN; Index++) { - Desc[Index] = QH_LINK (Ehc->PeriodOne, EHC_TYPE_QH, FALSE); + // + // Store the pci bus address of the QH in period frame list which will be accessed by pci bus master. + // + ((UINT32 *)(Ehc->PeriodFrame))[Index] = QH_LINK (PciAddr, EHC_TYPE_QH, FALSE); + // + // Store the host address of the QH in period frame list which will be accessed by host. + // + ((UINTN *)(Ehc->PeriodFrameHost))[Index] = (UINTN)Ehc->PeriodOne; } - EhcWriteOpReg (Ehc, EHC_FRAME_BASE_OFFSET, EHC_LOW_32BIT (Ehc->PeriodFrame)); - // // Second initialize the asynchronous schedule: // Only need to set the AsynListAddr register to // the reclamation header // - EhcWriteOpReg (Ehc, EHC_ASYNC_HEAD_OFFSET, EHC_LOW_32BIT (Ehc->ReclaimHead)); + PciAddr = UsbHcGetPciAddressForHostMem (Ehc->MemPool, Ehc->ReclaimHead, sizeof (EHC_QH)); + EhcWriteOpReg (Ehc, EHC_ASYNC_HEAD_OFFSET, EHC_LOW_32BIT (PciAddr)); return EFI_SUCCESS; -} +ErrorExit: + if (Ehc->PeriodOne != NULL) { + UsbHcFreeMem (Ehc->MemPool, Ehc->PeriodOne, sizeof (EHC_QH)); + Ehc->PeriodOne = NULL; + } + + if (Ehc->ReclaimHead != NULL) { + UsbHcFreeMem (Ehc->MemPool, Ehc->ReclaimHead, sizeof (EHC_QH)); + Ehc->ReclaimHead = NULL; + } + + if (Ehc->ShortReadStop != NULL) { + UsbHcFreeMem (Ehc->MemPool, Ehc->ShortReadStop, sizeof (EHC_QTD)); + Ehc->ShortReadStop = NULL; + } + +ErrorExit1: + PciIo->FreeBuffer (PciIo, Pages, Buf); + PciIo->Unmap (PciIo, Map); + + return Status; +} /** Free the schedule data. It may be partially initialized. - @param Ehc The EHCI device - - @return None + @param Ehc The EHCI device. **/ VOID @@ -261,13 +293,17 @@ EhcFreeSched ( PciIo->FreeBuffer ( PciIo, EFI_SIZE_TO_PAGES (EFI_PAGE_SIZE), - Ehc->PeriodFrameHost + Ehc->PeriodFrame ); Ehc->PeriodFrame = NULL; } -} + if (Ehc->PeriodFrameHost != NULL) { + FreePool (Ehc->PeriodFrameHost); + Ehc->PeriodFrameHost = NULL; + } +} /** @@ -277,10 +313,8 @@ EhcFreeSched ( management: A reclamation header is always linked to the AsyncListAddr, the only active QH is appended to it. - @param Ehc The EHCI device - @param Qh The queue head to link - - @return None + @param Ehc The EHCI device. + @param Qh The queue head to link. **/ VOID @@ -290,6 +324,7 @@ EhcLinkQhToAsync ( ) { EHC_QH *Head; + EFI_PHYSICAL_ADDRESS PciAddr; // // Append the queue head after the reclaim header, then @@ -301,19 +336,19 @@ EhcLinkQhToAsync ( Qh->NextQh = Head->NextQh; Head->NextQh = Qh; - Qh->QhHw.HorizonLink = QH_LINK (Head, EHC_TYPE_QH, FALSE);; - Head->QhHw.HorizonLink = QH_LINK (Qh, EHC_TYPE_QH, FALSE); + PciAddr = UsbHcGetPciAddressForHostMem (Ehc->MemPool, Qh->NextQh, sizeof (EHC_QH)); + Qh->QhHw.HorizonLink = QH_LINK (PciAddr, EHC_TYPE_QH, FALSE); + PciAddr = UsbHcGetPciAddressForHostMem (Ehc->MemPool, Head->NextQh, sizeof (EHC_QH)); + Head->QhHw.HorizonLink = QH_LINK (PciAddr, EHC_TYPE_QH, FALSE); } /** Unlink a queue head from the asynchronous schedule list. - Need to synchronize with hardware - - @param Ehc The EHCI device - @param Qh The queue head to unlink + Need to synchronize with hardware. - @return None + @param Ehc The EHCI device. + @param Qh The queue head to unlink. **/ VOID @@ -324,6 +359,7 @@ EhcUnlinkQhFromAsync ( { EHC_QH *Head; EFI_STATUS Status; + EFI_PHYSICAL_ADDRESS PciAddr; ASSERT (Ehc->ReclaimHead->NextQh == Qh); @@ -337,15 +373,16 @@ EhcUnlinkQhFromAsync ( Head->NextQh = Qh->NextQh; Qh->NextQh = NULL; - Head->QhHw.HorizonLink = QH_LINK (Head, EHC_TYPE_QH, FALSE); + PciAddr = UsbHcGetPciAddressForHostMem (Ehc->MemPool, Head->NextQh, sizeof (EHC_QH)); + Head->QhHw.HorizonLink = QH_LINK (PciAddr, EHC_TYPE_QH, FALSE); // // Set and wait the door bell to synchronize with the hardware // - Status = EhcSetAndWaitDoorBell (Ehc, EHC_GENERIC_TIME); + Status = EhcSetAndWaitDoorBell (Ehc, EHC_GENERIC_TIMEOUT); if (EFI_ERROR (Status)) { - EHC_ERROR (("EhcUnlinkQhFromAsync: Failed to synchronize with doorbell\n")); + DEBUG ((EFI_D_ERROR, "EhcUnlinkQhFromAsync: Failed to synchronize with doorbell\n")); } } @@ -355,10 +392,8 @@ EhcUnlinkQhFromAsync ( schedule frame list. This code is very much the same as that in UHCI. - @param Ehc The EHCI device - @param Qh The queue head to link - - @return None + @param Ehc The EHCI device. + @param Qh The queue head to link. **/ VOID @@ -367,20 +402,18 @@ EhcLinkQhToPeriod ( IN EHC_QH *Qh ) { - UINT32 *Frames; UINTN Index; EHC_QH *Prev; EHC_QH *Next; - - Frames = Ehc->PeriodFrame; + EFI_PHYSICAL_ADDRESS PciAddr; for (Index = 0; Index < EHC_FRAME_LEN; Index += Qh->Interval) { // // First QH can't be NULL because we always keep PeriodOne // heads on the frame list // - ASSERT (!EHC_LINK_TERMINATED (Frames[Index])); - Next = EHC_ADDR (Ehc->High32bitAddr, Frames[Index]); + ASSERT (!EHC_LINK_TERMINATED (((UINT32*)Ehc->PeriodFrame)[Index])); + Next = (EHC_QH*)((UINTN*)Ehc->PeriodFrameHost)[Index]; Prev = NULL; // @@ -428,7 +461,8 @@ EhcLinkQhToPeriod ( Prev->NextQh = Qh; Qh->QhHw.HorizonLink = Prev->QhHw.HorizonLink; - Prev->QhHw.HorizonLink = QH_LINK (Qh, EHC_TYPE_QH, FALSE); + PciAddr = UsbHcGetPciAddressForHostMem (Ehc->MemPool, Qh, sizeof (EHC_QH)); + Prev->QhHw.HorizonLink = QH_LINK (PciAddr, EHC_TYPE_QH, FALSE); break; } @@ -439,14 +473,18 @@ EhcLinkQhToPeriod ( // if (Qh->NextQh == NULL) { Qh->NextQh = Next; - Qh->QhHw.HorizonLink = QH_LINK (Next, EHC_TYPE_QH, FALSE); + PciAddr = UsbHcGetPciAddressForHostMem (Ehc->MemPool, Next, sizeof (EHC_QH)); + Qh->QhHw.HorizonLink = QH_LINK (PciAddr, EHC_TYPE_QH, FALSE); } + PciAddr = UsbHcGetPciAddressForHostMem (Ehc->MemPool, Qh, sizeof (EHC_QH)); + if (Prev == NULL) { - Frames[Index] = QH_LINK (Qh, EHC_TYPE_QH, FALSE); + ((UINT32*)Ehc->PeriodFrame)[Index] = QH_LINK (PciAddr, EHC_TYPE_QH, FALSE); + ((UINTN*)Ehc->PeriodFrameHost)[Index] = (UINTN)Qh; } else { Prev->NextQh = Qh; - Prev->QhHw.HorizonLink = QH_LINK (Qh, EHC_TYPE_QH, FALSE); + Prev->QhHw.HorizonLink = QH_LINK (PciAddr, EHC_TYPE_QH, FALSE); } } } @@ -454,12 +492,10 @@ EhcLinkQhToPeriod ( /** Unlink an interrupt queue head from the periodic - schedule frame list + schedule frame list. - @param Ehc The EHCI device - @param Qh The queue head to unlink - - @return None + @param Ehc The EHCI device. + @param Qh The queue head to unlink. **/ VOID @@ -468,20 +504,17 @@ EhcUnlinkQhFromPeriod ( IN EHC_QH *Qh ) { - UINT32 *Frames; UINTN Index; EHC_QH *Prev; EHC_QH *This; - Frames = Ehc->PeriodFrame; - for (Index = 0; Index < EHC_FRAME_LEN; Index += Qh->Interval) { // // Frame link can't be NULL because we always keep PeroidOne // on the frame list // - ASSERT (!EHC_LINK_TERMINATED (Frames[Index])); - This = EHC_ADDR (Ehc->High32bitAddr, Frames[Index]); + ASSERT (!EHC_LINK_TERMINATED (((UINT32*)Ehc->PeriodFrame)[Index])); + This = (EHC_QH*)((UINTN*)Ehc->PeriodFrameHost)[Index]; Prev = NULL; // @@ -505,7 +538,8 @@ EhcUnlinkQhFromPeriod ( // // Qh is the first entry in the frame // - Frames[Index] = Qh->QhHw.HorizonLink; + ((UINT32*)Ehc->PeriodFrame)[Index] = Qh->QhHw.HorizonLink; + ((UINTN*)Ehc->PeriodFrameHost)[Index] = (UINTN)Qh->NextQh; } else { Prev->NextQh = Qh->NextQh; Prev->QhHw.HorizonLink = Qh->QhHw.HorizonLink; @@ -514,18 +548,16 @@ EhcUnlinkQhFromPeriod ( } - /** Check the URB's execution result and update the URB's result accordingly. - @param Ehc The EHCI device - @param Urb The URB to check result + @param Ehc The EHCI device. + @param Urb The URB to check result. @return Whether the result of URB transfer is finialized. **/ -STATIC BOOLEAN EhcCheckUrbResult ( IN USB2_HC_DEV *Ehc, @@ -537,6 +569,7 @@ EhcCheckUrbResult ( QTD_HW *QtdHw; UINT8 State; BOOLEAN Finished; + EFI_PHYSICAL_ADDRESS PciAddr; ASSERT ((Ehc != NULL) && (Urb != NULL) && (Urb->Qh != NULL)); @@ -598,7 +631,7 @@ EhcCheckUrbResult ( } if ((QtdHw->TotalBytes != 0) && (QtdHw->Pid == QTD_PID_INPUT)) { - EHC_DUMP_QH ((Urb->Qh, "Short packet read", FALSE)); + EhcDumpQh (Urb->Qh, "Short packet read", FALSE); // // Short packet read condition. If it isn't a setup transfer, @@ -606,14 +639,15 @@ EhcCheckUrbResult ( // ShortReadStop. If it is a setup transfer, need to check the // Status Stage of the setup transfer to get the finial result // - if (QtdHw->AltNext == QTD_LINK (Ehc->ShortReadStop, FALSE)) { - EHC_DEBUG (("EhcCheckUrbResult: Short packet read, break\n")); + PciAddr = UsbHcGetPciAddressForHostMem (Ehc->MemPool, Ehc->ShortReadStop, sizeof (EHC_QTD)); + if (QtdHw->AltNext == QTD_LINK (PciAddr, FALSE)) { + DEBUG ((EFI_D_VERBOSE, "EhcCheckUrbResult: Short packet read, break\n")); Finished = TRUE; goto ON_EXIT; } - EHC_DEBUG (("EhcCheckUrbResult: Short packet read, continue\n")); + DEBUG ((EFI_D_VERBOSE, "EhcCheckUrbResult: Short packet read, continue\n")); } } } @@ -637,13 +671,13 @@ ON_EXIT: /** Execute the transfer by polling the URB. This is a synchronous operation. - @param Ehc The EHCI device - @param Urb The URB to execute - @param TimeOut The time to wait before abort, in millisecond. + @param Ehc The EHCI device. + @param Urb The URB to execute. + @param TimeOut The time to wait before abort, in millisecond. - @return EFI_DEVICE_ERROR : The transfer failed due to transfer error - @return EFI_TIMEOUT : The transfer failed due to time out - @return EFI_SUCCESS : The transfer finished OK + @return EFI_DEVICE_ERROR The transfer failed due to transfer error. + @return EFI_TIMEOUT The transfer failed due to time out. + @return EFI_SUCCESS The transfer finished OK. **/ EFI_STATUS @@ -657,30 +691,41 @@ EhcExecTransfer ( UINTN Index; UINTN Loop; BOOLEAN Finished; + BOOLEAN InfiniteLoop; - Status = EFI_SUCCESS; - Loop = (TimeOut * EHC_STALL_1_MILLISECOND / EHC_SYNC_POLL_TIME) + 1; - Finished = FALSE; + Status = EFI_SUCCESS; + Loop = TimeOut * EHC_1_MILLISECOND; + Finished = FALSE; + InfiniteLoop = FALSE; + + // + // According to UEFI spec section 16.2.4, If Timeout is 0, then the caller + // must wait for the function to be completed until EFI_SUCCESS or EFI_DEVICE_ERROR + // is returned. + // + if (TimeOut == 0) { + InfiniteLoop = TRUE; + } - for (Index = 0; Index < Loop; Index++) { + for (Index = 0; InfiniteLoop || (Index < Loop); Index++) { Finished = EhcCheckUrbResult (Ehc, Urb); if (Finished) { break; } - gBS->Stall (EHC_SYNC_POLL_TIME); + gBS->Stall (EHC_1_MICROSECOND); } if (!Finished) { - EHC_ERROR (("EhcExecTransfer: transfer not finished in %dms\n", TimeOut)); - EHC_DUMP_QH ((Urb->Qh, NULL, FALSE)); + DEBUG ((EFI_D_ERROR, "EhcExecTransfer: transfer not finished in %dms\n", (UINT32)TimeOut)); + EhcDumpQh (Urb->Qh, NULL, FALSE); Status = EFI_TIMEOUT; } else if (Urb->Result != EFI_USB_NOERROR) { - EHC_ERROR (("EhcExecTransfer: transfer failed with %x\n", Urb->Result)); - EHC_DUMP_QH ((Urb->Qh, NULL, FALSE)); + DEBUG ((EFI_D_ERROR, "EhcExecTransfer: transfer failed with %x\n", Urb->Result)); + EhcDumpQh (Urb->Qh, NULL, FALSE); Status = EFI_DEVICE_ERROR; } @@ -691,15 +736,15 @@ EhcExecTransfer ( /** Delete a single asynchronous interrupt transfer for - the device and endpoint + the device and endpoint. - @param Ehc The EHCI device - @param DevAddr The address of the target device - @param EpNum The endpoint of the target - @param DataToggle Return the next data toggle to use + @param Ehc The EHCI device. + @param DevAddr The address of the target device. + @param EpNum The endpoint of the target. + @param DataToggle Return the next data toggle to use. - @retval EFI_SUCCESS An asynchronous transfer is removed - @retval EFI_NOT_FOUND No transfer for the device is found + @retval EFI_SUCCESS An asynchronous transfer is removed. + @retval EFI_NOT_FOUND No transfer for the device is found. **/ EFI_STATUS @@ -715,7 +760,7 @@ EhciDelAsyncIntTransfer ( URB *Urb; EFI_USB_DATA_DIRECTION Direction; - Direction = ((EpNum & 0x80) ? EfiUsbDataIn : EfiUsbDataOut); + Direction = (((EpNum & 0x80) != 0) ? EfiUsbDataIn : EfiUsbDataOut); EpNum &= 0x0F; EFI_LIST_FOR_EACH_SAFE (Entry, Next, &Ehc->AsyncIntTransfers) { @@ -744,11 +789,9 @@ EhciDelAsyncIntTransfer ( /** - Remove all the asynchronous interrutp transfers - - @param Ehc The EHCI device + Remove all the asynchronous interrutp transfers. - @return None + @param Ehc The EHCI device. **/ VOID @@ -772,18 +815,70 @@ EhciDelAllAsyncIntTransfers ( } - /** - Update the queue head for next round of asynchronous transfer + Flush data from PCI controller specific address to mapped system + memory address. - @param Urb The URB to update + @param Ehc The EHCI device. + @param Urb The URB to unmap. - @return None + @retval EFI_SUCCESS Success to flush data to mapped system memory. + @retval EFI_DEVICE_ERROR Fail to flush data to mapped system memory. + +**/ +EFI_STATUS +EhcFlushAsyncIntMap ( + IN USB2_HC_DEV *Ehc, + IN URB *Urb + ) +{ + EFI_STATUS Status; + EFI_PHYSICAL_ADDRESS PhyAddr; + EFI_PCI_IO_PROTOCOL_OPERATION MapOp; + EFI_PCI_IO_PROTOCOL *PciIo; + UINTN Len; + VOID *Map; + + PciIo = Ehc->PciIo; + Len = Urb->DataLen; + + if (Urb->Ep.Direction == EfiUsbDataIn) { + MapOp = EfiPciIoOperationBusMasterWrite; + } else { + MapOp = EfiPciIoOperationBusMasterRead; + } + + Status = PciIo->Unmap (PciIo, Urb->DataMap); + if (EFI_ERROR (Status)) { + goto ON_ERROR; + } + + Urb->DataMap = NULL; + + Status = PciIo->Map (PciIo, MapOp, Urb->Data, &Len, &PhyAddr, &Map); + if (EFI_ERROR (Status) || (Len != Urb->DataLen)) { + goto ON_ERROR; + } + + Urb->DataPhy = (VOID *) ((UINTN) PhyAddr); + Urb->DataMap = Map; + return EFI_SUCCESS; + +ON_ERROR: + return EFI_DEVICE_ERROR; +} + + +/** + Update the queue head for next round of asynchronous transfer. + + @param Ehc The EHCI device. + @param Urb The URB to update. **/ -STATIC VOID EhcUpdateAsyncRequest ( + IN USB2_HC_DEV *Ehc, IN URB *Urb ) { @@ -793,6 +888,7 @@ EhcUpdateAsyncRequest ( EHC_QTD *Qtd; QTD_HW *QtdHw; UINTN Index; + EFI_PHYSICAL_ADDRESS PciAddr; Qtd = NULL; @@ -819,7 +915,12 @@ EhcUpdateAsyncRequest ( QtdHw->ErrCnt = QTD_MAX_ERR; QtdHw->CurPage = 0; QtdHw->TotalBytes = (UINT32) Qtd->DataLen; - QtdHw->Page[0] = EHC_LOW_32BIT (Qtd->Data); + // + // calculate physical address by offset. + // + PciAddr = (UINTN)Urb->DataPhy + ((UINTN)Qtd->Data - (UINTN)Urb->Data); + QtdHw->Page[0] = EHC_LOW_32BIT (PciAddr); + QtdHw->PageHigh[0]= EHC_HIGH_32BIT (PciAddr); } // @@ -836,7 +937,7 @@ EhcUpdateAsyncRequest ( QhHw->Pid = 0; QhHw->ErrCnt = 0; QhHw->CurPage = 0; - QhHw->IOC = 0; + QhHw->Ioc = 0; QhHw->TotalBytes = 0; for (Index = 0; Index < 5; Index++) { @@ -844,7 +945,8 @@ EhcUpdateAsyncRequest ( QhHw->PageHigh[Index] = 0; } - QhHw->NextQtd = QTD_LINK (FirstQtd, FALSE); + PciAddr = UsbHcGetPciAddressForHostMem (Ehc->MemPool, FirstQtd, sizeof (EHC_QTD)); + QhHw->NextQtd = QTD_LINK (PciAddr, FALSE); } return ; @@ -852,16 +954,15 @@ EhcUpdateAsyncRequest ( /** - Interrupt transfer periodic check handler - - @param Event Interrupt event - @param Context Pointer to USB2_HC_DEV + Interrupt transfer periodic check handler. - @return None + @param Event Interrupt event. + @param Context Pointer to USB2_HC_DEV. **/ VOID -EhcMoniteAsyncRequests ( +EFIAPI +EhcMonitorAsyncRequests ( IN EFI_EVENT Event, IN VOID *Context ) @@ -873,6 +974,7 @@ EhcMoniteAsyncRequests ( BOOLEAN Finished; UINT8 *ProcBuf; URB *Urb; + EFI_STATUS Status; OldTpl = gBS->RaiseTPL (EHC_TPL); Ehc = (USB2_HC_DEV *) Context; @@ -890,6 +992,15 @@ EhcMoniteAsyncRequests ( continue; } + // + // Flush any PCI posted write transactions from a PCI host + // bridge to system memory. + // + Status = EhcFlushAsyncIntMap (Ehc, Urb); + if (EFI_ERROR (Status)) { + DEBUG ((EFI_D_ERROR, "EhcMonitorAsyncRequests: Fail to Flush AsyncInt Mapped Memeory\n")); + } + // // Allocate a buffer then copy the transferred data for user. // If failed to allocate the buffer, update the URB for next @@ -898,19 +1009,22 @@ EhcMoniteAsyncRequests ( ProcBuf = NULL; if (Urb->Result == EFI_USB_NOERROR) { - ASSERT (Urb->Completed <= Urb->DataLen); - - ProcBuf = AllocatePool (Urb->Completed); + // + // Make sure the data received from HW is no more than expected. + // + if (Urb->Completed <= Urb->DataLen) { + ProcBuf = AllocatePool (Urb->Completed); + } if (ProcBuf == NULL) { - EhcUpdateAsyncRequest (Urb); + EhcUpdateAsyncRequest (Ehc, Urb); continue; } CopyMem (ProcBuf, Urb->Data, Urb->Completed); } - EhcUpdateAsyncRequest (Urb); + EhcUpdateAsyncRequest (Ehc, Urb); // // Leave error recovery to its related device driver. A @@ -933,7 +1047,7 @@ EhcMoniteAsyncRequests ( } if (ProcBuf != NULL) { - gBS->FreePool (ProcBuf); + FreePool (ProcBuf); } }