X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FBus%2FPci%2FXhciDxe%2FXhci.c;h=20ad3f1611979ff187abb7fb47c0ed800be32fa8;hp=61c4b2427fa3aebff6d818b85ad4eaee7094883a;hb=49be9c3c20cea7477b9c9e390c9f97735760e216;hpb=92870c983c6d99d31f449d8dcd729090255dda49 diff --git a/MdeModulePkg/Bus/Pci/XhciDxe/Xhci.c b/MdeModulePkg/Bus/Pci/XhciDxe/Xhci.c index 61c4b2427f..20ad3f1611 100644 --- a/MdeModulePkg/Bus/Pci/XhciDxe/Xhci.c +++ b/MdeModulePkg/Bus/Pci/XhciDxe/Xhci.c @@ -1,7 +1,7 @@ /** @file The XHCI controller driver. -Copyright (c) 2011, Intel Corporation. All rights reserved.
+Copyright (c) 2011 - 2017, 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 @@ -14,11 +14,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include "Xhci.h" -// -// The device context array which supports up to 255 devices, entry 0 is reserved and should not be used. -// -USB_DEV_CONTEXT UsbDevContext[256]; - // // Two arrays used to translate the XHCI port state (change) // to the UEFI protocol's port state (change). @@ -37,6 +32,35 @@ USB_PORT_STATE_MAP mUsbPortChangeMap[] = { {XHC_PORTSC_PRC, USB_PORT_STAT_C_RESET} }; +USB_CLEAR_PORT_MAP mUsbClearPortChangeMap[] = { + {XHC_PORTSC_CSC, EfiUsbPortConnectChange}, + {XHC_PORTSC_PEC, EfiUsbPortEnableChange}, + {XHC_PORTSC_OCC, EfiUsbPortOverCurrentChange}, + {XHC_PORTSC_PRC, EfiUsbPortResetChange} +}; + +USB_PORT_STATE_MAP mUsbHubPortStateMap[] = { + {XHC_HUB_PORTSC_CCS, USB_PORT_STAT_CONNECTION}, + {XHC_HUB_PORTSC_PED, USB_PORT_STAT_ENABLE}, + {XHC_HUB_PORTSC_OCA, USB_PORT_STAT_OVERCURRENT}, + {XHC_HUB_PORTSC_RESET, USB_PORT_STAT_RESET} +}; + +USB_PORT_STATE_MAP mUsbHubPortChangeMap[] = { + {XHC_HUB_PORTSC_CSC, USB_PORT_STAT_C_CONNECTION}, + {XHC_HUB_PORTSC_PEC, USB_PORT_STAT_C_ENABLE}, + {XHC_HUB_PORTSC_OCC, USB_PORT_STAT_C_OVERCURRENT}, + {XHC_HUB_PORTSC_PRC, USB_PORT_STAT_C_RESET} +}; + +USB_CLEAR_PORT_MAP mUsbHubClearPortChangeMap[] = { + {XHC_HUB_PORTSC_CSC, EfiUsbPortConnectChange}, + {XHC_HUB_PORTSC_PEC, EfiUsbPortEnableChange}, + {XHC_HUB_PORTSC_OCC, EfiUsbPortOverCurrentChange}, + {XHC_HUB_PORTSC_PRC, EfiUsbPortResetChange}, + {XHC_HUB_PORTSC_BHRC, Usb3PortBHPortResetChange} +}; + EFI_DRIVER_BINDING_PROTOCOL gXhciDriverBinding = { XhcDriverBindingSupported, XhcDriverBindingStart, @@ -46,6 +70,27 @@ EFI_DRIVER_BINDING_PROTOCOL gXhciDriverBinding = { NULL }; +// +// Template for Xhci's Usb2 Host Controller Protocol Instance. +// +EFI_USB2_HC_PROTOCOL gXhciUsb2HcTemplate = { + XhcGetCapability, + XhcReset, + XhcGetState, + XhcSetState, + XhcControlTransfer, + XhcBulkTransfer, + XhcAsyncInterruptTransfer, + XhcSyncInterruptTransfer, + XhcIsochronousTransfer, + XhcAsyncIsochronousTransfer, + XhcGetRootHubPortStatus, + XhcSetRootHubPortFeature, + XhcClearRootHubPortFeature, + 0x3, + 0x0 +}; + /** Retrieves the capability of root hub ports. @@ -68,8 +113,8 @@ XhcGetCapability ( OUT UINT8 *Is64BitCapable ) { - USB_XHCI_DEV *Xhc; - EFI_TPL OldTpl; + USB_XHCI_INSTANCE *Xhc; + EFI_TPL OldTpl; if ((MaxSpeed == NULL) || (PortNumber == NULL) || (Is64BitCapable == NULL)) { return EFI_INVALID_PARAMETER; @@ -80,7 +125,7 @@ XhcGetCapability ( Xhc = XHC_FROM_THIS (This); *MaxSpeed = EFI_USB_SPEED_SUPER; *PortNumber = (UINT8) (Xhc->HcSParams1.Data.MaxPorts); - *Is64BitCapable = (UINT8) (Xhc->HcCParams.Data.Ac64); + *Is64BitCapable = (UINT8) Xhc->Support64BitDma; DEBUG ((EFI_D_INFO, "XhcGetCapability: %d ports, 64 bit %d\n", *PortNumber, *Is64BitCapable)); gBS->RestoreTPL (OldTpl); @@ -109,13 +154,24 @@ XhcReset ( IN UINT16 Attributes ) { - USB_XHCI_DEV *Xhc; - EFI_STATUS Status; - EFI_TPL OldTpl; + USB_XHCI_INSTANCE *Xhc; + EFI_STATUS Status; + EFI_TPL OldTpl; - OldTpl = gBS->RaiseTPL (XHC_TPL); + Xhc = XHC_FROM_THIS (This); + + if (Xhc->DevicePath != NULL) { + // + // Report Status Code to indicate reset happens + // + REPORT_STATUS_CODE_WITH_DEVICE_PATH ( + EFI_PROGRESS_CODE, + (EFI_IO_BUS_USB | EFI_IOB_PC_RESET), + Xhc->DevicePath + ); + } - Xhc = XHC_FROM_THIS (This); + OldTpl = gBS->RaiseTPL (XHC_TPL); switch (Attributes) { case EFI_USB_HC_RESET_GLOBAL: @@ -123,6 +179,11 @@ XhcReset ( // Flow through, same behavior as Host Controller Reset // case EFI_USB_HC_RESET_HOST_CONTROLLER: + if ((Xhc->DebugCapSupOffset != 0xFFFFFFFF) && ((XhcReadExtCapReg (Xhc, Xhc->DebugCapSupOffset) & 0xFF) == XHC_CAP_USB_DEBUG) && + ((XhcReadExtCapReg (Xhc, Xhc->DebugCapSupOffset + XHC_DC_DCCTRL) & BIT0) != 0)) { + Status = EFI_SUCCESS; + goto ON_EXIT; + } // // Host Controller must be Halt when Reset it // @@ -188,8 +249,8 @@ XhcGetState ( OUT EFI_USB_HC_STATE *State ) { - USB_XHCI_DEV *Xhc; - EFI_TPL OldTpl; + USB_XHCI_INSTANCE *Xhc; + EFI_TPL OldTpl; if (State == NULL) { return EFI_INVALID_PARAMETER; @@ -230,7 +291,7 @@ XhcSetState ( IN EFI_USB_HC_STATE State ) { - USB_XHCI_DEV *Xhc; + USB_XHCI_INSTANCE *Xhc; EFI_STATUS Status; EFI_USB_HC_STATE CurState; EFI_TPL OldTpl; @@ -309,7 +370,7 @@ XhcGetRootHubPortStatus ( OUT EFI_USB_PORT_STATUS *PortStatus ) { - USB_XHCI_DEV *Xhc; + USB_XHCI_INSTANCE *Xhc; UINT32 Offset; UINT32 State; UINT32 TotalPort; @@ -386,6 +447,14 @@ XhcGetRootHubPortStatus ( } } + MapSize = sizeof (mUsbClearPortChangeMap) / sizeof (USB_CLEAR_PORT_MAP); + + for (Index = 0; Index < MapSize; Index++) { + if (XHC_BIT_IS_SET (State, mUsbClearPortChangeMap[Index].HwState)) { + XhcClearRootHubPortFeature (This, PortNumber, (EFI_USB_PORT_FEATURE)mUsbClearPortChangeMap[Index].Selector); + } + } + // // Poll the root port status register to enable/disable corresponding device slot if there is a device attached/detached. // For those devices behind hub, we get its attach/detach event by hooking Get_Port_Status request at control transfer for those hub. @@ -419,12 +488,10 @@ XhcSetRootHubPortFeature ( IN EFI_USB_PORT_FEATURE PortFeature ) { - USB_XHCI_DEV *Xhc; + USB_XHCI_INSTANCE *Xhc; UINT32 Offset; UINT32 State; UINT32 TotalPort; - UINT8 SlotId; - USB_DEV_ROUTE RouteChart; EFI_STATUS Status; EFI_TPL OldTpl; @@ -480,24 +547,13 @@ XhcSetRootHubPortFeature ( } } - RouteChart.Field.RouteString = 0; - RouteChart.Field.RootPortNum = PortNumber + 1; - RouteChart.Field.TierNum = 1; // - // BUGBUG: If the port reset operation happens after the usb super speed device is enabled, - // The subsequent configuration, such as getting device descriptor, will fail. - // So here a workaround is introduced to skip the reset operation if the device is enabled. + // 4.3.1 Resetting a Root Hub Port + // 1) Write the PORTSC register with the Port Reset (PR) bit set to '1'. // - SlotId = XhcRouteStringToSlotId (RouteChart); - if (SlotId == 0) { - // - // 4.3.1 Resetting a Root Hub Port - // 1) Write the PORTSC register with the Port Reset (PR) bit set to '1'. - // - State |= XHC_PORTSC_RESET; - XhcWriteOpReg (Xhc, Offset, State); - XhcWaitOpRegBit(Xhc, Offset, XHC_PORTSC_PRC, TRUE, XHC_GENERIC_TIMEOUT); - } + State |= XHC_PORTSC_RESET; + XhcWriteOpReg (Xhc, Offset, State); + XhcWaitOpRegBit(Xhc, Offset, XHC_PORTSC_PRC, TRUE, XHC_GENERIC_TIMEOUT); break; case EfiUsbPortPower: @@ -549,7 +605,7 @@ XhcClearRootHubPortFeature ( IN EFI_USB_PORT_FEATURE PortFeature ) { - USB_XHCI_DEV *Xhc; + USB_XHCI_INSTANCE *Xhc; UINT32 Offset; UINT32 State; UINT32 TotalPort; @@ -660,6 +716,103 @@ ON_EXIT: return Status; } +/** + Submits a new transaction to a target USB device. + + @param Xhc The XHCI Instance. + @param DeviceAddress The target device address. + @param EndPointAddress Endpoint number and its direction encoded in bit 7 + @param DeviceSpeed Target device speed. + @param MaximumPacketLength Maximum packet size the default control transfer + endpoint is capable of sending or receiving. + @param Type The transaction type. + @param Request USB device request to send. + @param Data Data buffer to be transmitted or received from USB + device. + @param DataLength The size (in bytes) of the data buffer. + @param Timeout Indicates the maximum timeout, in millisecond. + @param TransferResult Return the result of this control transfer. + + @retval EFI_SUCCESS Transfer was completed successfully. + @retval EFI_OUT_OF_RESOURCES The transfer failed due to lack of resources. + @retval EFI_INVALID_PARAMETER Some parameters are invalid. + @retval EFI_TIMEOUT Transfer failed due to timeout. + @retval EFI_DEVICE_ERROR Transfer failed due to host controller or device error. +**/ +EFI_STATUS +XhcTransfer ( + IN USB_XHCI_INSTANCE *Xhc, + IN UINT8 DeviceAddress, + IN UINT8 EndPointAddress, + IN UINT8 DeviceSpeed, + IN UINTN MaximumPacketLength, + IN UINTN Type, + IN EFI_USB_DEVICE_REQUEST *Request, + IN OUT VOID *Data, + IN OUT UINTN *DataLength, + IN UINTN Timeout, + OUT UINT32 *TransferResult + ) +{ + EFI_STATUS Status; + EFI_STATUS RecoveryStatus; + URB *Urb; + + ASSERT ((Type == XHC_CTRL_TRANSFER) || (Type == XHC_BULK_TRANSFER) || (Type == XHC_INT_TRANSFER_SYNC)); + Urb = XhcCreateUrb ( + Xhc, + DeviceAddress, + EndPointAddress, + DeviceSpeed, + MaximumPacketLength, + Type, + Request, + Data, + *DataLength, + NULL, + NULL + ); + + if (Urb == NULL) { + DEBUG ((DEBUG_ERROR, "XhcTransfer[Type=%d]: failed to create URB!\n", Type)); + return EFI_OUT_OF_RESOURCES; + } + + Status = XhcExecTransfer (Xhc, FALSE, Urb, Timeout); + + if (Status == EFI_TIMEOUT) { + // + // The transfer timed out. Abort the transfer by dequeueing of the TD. + // + RecoveryStatus = XhcDequeueTrbFromEndpoint(Xhc, Urb); + if (RecoveryStatus == EFI_ALREADY_STARTED) { + // + // The URB is finished just before stopping endpoint. + // Change returning status from EFI_TIMEOUT to EFI_SUCCESS. + // + ASSERT (Urb->Result == EFI_USB_NOERROR); + Status = EFI_SUCCESS; + DEBUG ((DEBUG_ERROR, "XhcTransfer[Type=%d]: pending URB is finished, Length = %d.\n", Type, Urb->Completed)); + } else if (EFI_ERROR(RecoveryStatus)) { + DEBUG((DEBUG_ERROR, "XhcTransfer[Type=%d]: XhcDequeueTrbFromEndpoint failed!\n", Type)); + } + } + + *TransferResult = Urb->Result; + *DataLength = Urb->Completed; + + if (*TransferResult == EFI_USB_ERR_STALL) { + ASSERT (Status == EFI_DEVICE_ERROR); + RecoveryStatus = XhcRecoverHaltedEndpoint(Xhc, Urb); + if (EFI_ERROR (RecoveryStatus)) { + DEBUG ((DEBUG_ERROR, "XhcTransfer[Type=%d]: XhcRecoverHaltedEndpoint failed!\n", Type)); + } + } + + Xhc->PciIo->Flush (Xhc->PciIo); + XhcFreeUrb (Xhc, Urb); + return Status; +} /** Submits control transfer to a target USB device. @@ -674,7 +827,7 @@ ON_EXIT: @param Data Data buffer to be transmitted or received from USB device. @param DataLength The size (in bytes) of the data buffer. - @param TimeOut Indicates the maximum timeout, in millisecond. + @param Timeout Indicates the maximum timeout, in millisecond. @param Translator Transaction translator to be used by this device. @param TransferResult Return the result of this control transfer. @@ -696,16 +849,14 @@ XhcControlTransfer ( IN EFI_USB_DATA_DIRECTION TransferDirection, IN OUT VOID *Data, IN OUT UINTN *DataLength, - IN UINTN TimeOut, + IN UINTN Timeout, IN EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator, OUT UINT32 *TransferResult ) { - USB_XHCI_DEV *Xhc; - URB *Urb; + USB_XHCI_INSTANCE *Xhc; UINT8 Endpoint; UINT8 Index; - UINT8 XhciDevAddr; UINT8 DescriptorType; UINT8 SlotId; UINT8 TTT; @@ -714,10 +865,11 @@ XhcControlTransfer ( EFI_USB_HUB_DESCRIPTOR *HubDesc; EFI_TPL OldTpl; EFI_STATUS Status; - EFI_STATUS RecoveryStatus; UINTN MapSize; EFI_USB_PORT_STATUS PortStatus; UINT32 State; + EFI_USB_DEVICE_REQUEST ClearPortRequest; + UINTN Len; // // Validate parameters @@ -763,6 +915,7 @@ XhcControlTransfer ( Status = EFI_DEVICE_ERROR; *TransferResult = EFI_USB_ERR_SYSTEM; + Len = 0; if (XhcIsHalt (Xhc) || XhcIsSysError (Xhc)) { DEBUG ((EFI_D_ERROR, "XhcControlTransfer: HC halted at entrance\n")); @@ -772,55 +925,43 @@ XhcControlTransfer ( // // Check if the device is still enabled before every transaction. // - SlotId = XhcBusDevAddrToSlotId (DeviceAddress); + SlotId = XhcBusDevAddrToSlotId (Xhc, DeviceAddress); if (SlotId == 0) { goto ON_EXIT; } - // - // Acquire the actual device address assigned by XHCI's Address_Device cmd. - // - XhciDevAddr = UsbDevContext[SlotId].XhciDevAddr; - // // Hook the Set_Address request from UsbBus. // According to XHCI 1.0 spec, the Set_Address request is replaced by XHCI's Address_Device cmd. // - if (Request->Request == USB_REQ_SET_ADDRESS) { + if ((Request->Request == USB_REQ_SET_ADDRESS) && + (Request->RequestType == USB_REQUEST_TYPE (EfiUsbNoData, USB_REQ_TYPE_STANDARD, USB_TARGET_DEVICE))) { // // Reset the BusDevAddr field of all disabled entries in UsbDevContext array firstly. // This way is used to clean the history to avoid using wrong device address by XhcAsyncInterruptTransfer(). // for (Index = 0; Index < 255; Index++) { - if (!UsbDevContext[Index + 1].Enabled && - (UsbDevContext[Index + 1].SlotId != 0) && - (UsbDevContext[Index + 1].BusDevAddr == (UINT8)Request->Value)) { - UsbDevContext[Index + 1].BusDevAddr = 0; + if (!Xhc->UsbDevContext[Index + 1].Enabled && + (Xhc->UsbDevContext[Index + 1].SlotId == 0) && + (Xhc->UsbDevContext[Index + 1].BusDevAddr == (UINT8)Request->Value)) { + Xhc->UsbDevContext[Index + 1].BusDevAddr = 0; } } + + if (Xhc->UsbDevContext[SlotId].XhciDevAddr == 0) { + Status = EFI_DEVICE_ERROR; + goto ON_EXIT; + } // // The actual device address has been assigned by XHCI during initializing the device slot. // So we just need establish the mapping relationship between the device address requested from UsbBus // and the actual device address assigned by XHCI. The the following invocations through EFI_USB2_HC_PROTOCOL interface // can find out the actual device address by it. // - UsbDevContext[SlotId].BusDevAddr = (UINT8)Request->Value; + Xhc->UsbDevContext[SlotId].BusDevAddr = (UINT8)Request->Value; Status = EFI_SUCCESS; goto ON_EXIT; } - - // - // BUGBUG: If the port reset operation happens after the usb super speed device is enabled, - // The subsequent configuration, such as getting device descriptor, will fail. - // So here a workaround is introduced to skip the reset operation if the device is enabled. - // - if ((Request->Request == USB_REQ_SET_FEATURE) && - (Request->Value == EfiUsbPortReset)) { - if (DeviceSpeed == EFI_USB_SPEED_SUPER) { - Status = EFI_SUCCESS; - goto ON_EXIT; - } - } // // Create a new URB, insert it into the asynchronous @@ -829,112 +970,126 @@ XhcControlTransfer ( // endpoint is bidirectional. XhcCreateUrb expects this // combination of Ep addr and its direction. // - Endpoint = 0 | ((TransferDirection == EfiUsbDataIn) ? 0x80 : 0); - Urb = XhcCreateUrb ( - Xhc, - XhciDevAddr, - Endpoint, - DeviceSpeed, - MaximumPacketLength, - XHC_CTRL_TRANSFER, - Request, - Data, - *DataLength, - NULL, - NULL - ); + Endpoint = (UINT8) (0 | ((TransferDirection == EfiUsbDataIn) ? 0x80 : 0)); + Status = XhcTransfer ( + Xhc, + DeviceAddress, + Endpoint, + DeviceSpeed, + MaximumPacketLength, + XHC_CTRL_TRANSFER, + Request, + Data, + DataLength, + Timeout, + TransferResult + ); - if (Urb == NULL) { - DEBUG ((EFI_D_ERROR, "XhcControlTransfer: failed to create URB")); - Status = EFI_OUT_OF_RESOURCES; + if (EFI_ERROR (Status)) { goto ON_EXIT; } - ASSERT (Urb->EvtRing == &Xhc->CtrlTrEventRing); - Status = XhcExecTransfer (Xhc, FALSE, Urb, TimeOut); - - // - // Get the status from URB. The result is updated in XhcCheckUrbResult - // which is called by XhcExecTransfer - // - *TransferResult = Urb->Result; - *DataLength = Urb->Completed; - - if (*TransferResult == EFI_USB_NOERROR) { - Status = EFI_SUCCESS; - } else if ((*TransferResult == EFI_USB_ERR_STALL) || - (*TransferResult == EFI_USB_ERR_TIMEOUT)) { - RecoveryStatus = XhcRecoverHaltedEndpoint(Xhc, Urb); - ASSERT_EFI_ERROR (RecoveryStatus); - goto FREE_URB; - } // // Hook Get_Descriptor request from UsbBus as we need evaluate context and configure endpoint. // Hook Get_Status request form UsbBus as we need trace device attach/detach event happened at hub. // Hook Set_Config request from UsbBus as we need configure device endpoint. // - if (Request->Request == USB_REQ_GET_DESCRIPTOR) { + if ((Request->Request == USB_REQ_GET_DESCRIPTOR) && + ((Request->RequestType == USB_REQUEST_TYPE (EfiUsbDataIn, USB_REQ_TYPE_STANDARD, USB_TARGET_DEVICE)) || + ((Request->RequestType == USB_REQUEST_TYPE (EfiUsbDataIn, USB_REQ_TYPE_CLASS, USB_TARGET_DEVICE))))) { DescriptorType = (UINT8)(Request->Value >> 8); - if ((DescriptorType == USB_DESC_TYPE_DEVICE) && (*DataLength == sizeof (EFI_USB_DEVICE_DESCRIPTOR))) { + if ((DescriptorType == USB_DESC_TYPE_DEVICE) && ((*DataLength == sizeof (EFI_USB_DEVICE_DESCRIPTOR)) || ((DeviceSpeed == EFI_USB_SPEED_FULL) && (*DataLength == 8)))) { + ASSERT (Data != NULL); // // Store a copy of device scriptor as hub device need this info to configure endpoint. // - CopyMem (&UsbDevContext[SlotId].DevDesc, Data, *DataLength); - if (UsbDevContext[SlotId].DevDesc.BcdUSB == 0x0300) { + CopyMem (&Xhc->UsbDevContext[SlotId].DevDesc, Data, *DataLength); + if (Xhc->UsbDevContext[SlotId].DevDesc.BcdUSB >= 0x0300) { // // If it's a usb3.0 device, then its max packet size is a 2^n. // - MaxPacket0 = 1 << UsbDevContext[SlotId].DevDesc.MaxPacketSize0; + MaxPacket0 = 1 << Xhc->UsbDevContext[SlotId].DevDesc.MaxPacketSize0; } else { - MaxPacket0 = UsbDevContext[SlotId].DevDesc.MaxPacketSize0; + MaxPacket0 = Xhc->UsbDevContext[SlotId].DevDesc.MaxPacketSize0; } - UsbDevContext[SlotId].ConfDesc = AllocateZeroPool (UsbDevContext[SlotId].DevDesc.NumConfigurations * sizeof (EFI_USB_CONFIG_DESCRIPTOR *)); - Status = XhcEvaluateContext (Xhc, SlotId, MaxPacket0); - ASSERT_EFI_ERROR (Status); - } else if ((DescriptorType == USB_DESC_TYPE_CONFIG) && (*DataLength == ((UINT16 *)Data)[1])) { - // - // Get configuration value from request, Store the configuration descriptor for Configure_Endpoint cmd. - // - Index = (UINT8)Request->Value; - ASSERT (Index < UsbDevContext[SlotId].DevDesc.NumConfigurations); - UsbDevContext[SlotId].ConfDesc[Index] = AllocateZeroPool(*DataLength); - CopyMem (UsbDevContext[SlotId].ConfDesc[Index], Data, *DataLength); + Xhc->UsbDevContext[SlotId].ConfDesc = AllocateZeroPool (Xhc->UsbDevContext[SlotId].DevDesc.NumConfigurations * sizeof (EFI_USB_CONFIG_DESCRIPTOR *)); + if (Xhc->HcCParams.Data.Csz == 0) { + Status = XhcEvaluateContext (Xhc, SlotId, MaxPacket0); + } else { + Status = XhcEvaluateContext64 (Xhc, SlotId, MaxPacket0); + } + } else if (DescriptorType == USB_DESC_TYPE_CONFIG) { + ASSERT (Data != NULL); + if (*DataLength == ((UINT16 *)Data)[1]) { + // + // Get configuration value from request, Store the configuration descriptor for Configure_Endpoint cmd. + // + Index = (UINT8)Request->Value; + ASSERT (Index < Xhc->UsbDevContext[SlotId].DevDesc.NumConfigurations); + Xhc->UsbDevContext[SlotId].ConfDesc[Index] = AllocateZeroPool(*DataLength); + CopyMem (Xhc->UsbDevContext[SlotId].ConfDesc[Index], Data, *DataLength); + // + // Default to use AlternateSetting 0 for all interfaces. + // + Xhc->UsbDevContext[SlotId].ActiveAlternateSetting = AllocateZeroPool (Xhc->UsbDevContext[SlotId].ConfDesc[Index]->NumInterfaces * sizeof (UINT8)); + } } else if (((DescriptorType == USB_DESC_TYPE_HUB) || - (DescriptorType == USB_DESC_TYPE_HUB_SUPER_SPEED))) { + (DescriptorType == USB_DESC_TYPE_HUB_SUPER_SPEED)) && (*DataLength > 2)) { + ASSERT (Data != NULL); HubDesc = (EFI_USB_HUB_DESCRIPTOR *)Data; + ASSERT (HubDesc->NumPorts <= 15); // // The bit 5,6 of HubCharacter field of Hub Descriptor is TTT. // TTT = (UINT8)((HubDesc->HubCharacter & (BIT5 | BIT6)) >> 5); - if (UsbDevContext[SlotId].DevDesc.DeviceProtocol == 2) { + if (Xhc->UsbDevContext[SlotId].DevDesc.DeviceProtocol == 2) { // - // BUGBUG: Don't support multi-TT feature for super speed hub. + // Don't support multi-TT feature for super speed hub now. // - MTT = 1; - ASSERT (FALSE); + MTT = 0; + DEBUG ((EFI_D_ERROR, "XHCI: Don't support multi-TT feature for Hub now. (force to disable MTT)\n")); } else { MTT = 0; } - Status = XhcConfigHubContext ( - Xhc, - SlotId, - HubDesc->NumPorts, - TTT, - MTT - ); + if (Xhc->HcCParams.Data.Csz == 0) { + Status = XhcConfigHubContext (Xhc, SlotId, HubDesc->NumPorts, TTT, MTT); + } else { + Status = XhcConfigHubContext64 (Xhc, SlotId, HubDesc->NumPorts, TTT, MTT); + } } - } else if (Request->Request == USB_REQ_SET_CONFIG) { + } else if ((Request->Request == USB_REQ_SET_CONFIG) && + (Request->RequestType == USB_REQUEST_TYPE (EfiUsbNoData, USB_REQ_TYPE_STANDARD, USB_TARGET_DEVICE))) { // // Hook Set_Config request from UsbBus as we need configure device endpoint. // - for (Index = 0; Index < UsbDevContext[SlotId].DevDesc.NumConfigurations; Index++) { - if (UsbDevContext[SlotId].ConfDesc[Index]->ConfigurationValue == (UINT8)Request->Value) { - XhcSetConfigCmd (Xhc, SlotId, DeviceSpeed, UsbDevContext[SlotId].ConfDesc[Index]); + for (Index = 0; Index < Xhc->UsbDevContext[SlotId].DevDesc.NumConfigurations; Index++) { + if (Xhc->UsbDevContext[SlotId].ConfDesc[Index]->ConfigurationValue == (UINT8)Request->Value) { + if (Xhc->HcCParams.Data.Csz == 0) { + Status = XhcSetConfigCmd (Xhc, SlotId, DeviceSpeed, Xhc->UsbDevContext[SlotId].ConfDesc[Index]); + } else { + Status = XhcSetConfigCmd64 (Xhc, SlotId, DeviceSpeed, Xhc->UsbDevContext[SlotId].ConfDesc[Index]); + } break; } } - } else if (Request->Request == USB_REQ_GET_STATUS) { + } else if ((Request->Request == USB_REQ_SET_INTERFACE) && + (Request->RequestType == USB_REQUEST_TYPE (EfiUsbNoData, USB_REQ_TYPE_STANDARD, USB_TARGET_INTERFACE))) { + // + // Hook Set_Interface request from UsbBus as we need configure interface setting. + // Request->Value indicates AlterlateSetting to set + // Request->Index indicates Interface to set + // + if (Xhc->UsbDevContext[SlotId].ActiveAlternateSetting[(UINT8) Request->Index] != (UINT8) Request->Value) { + if (Xhc->HcCParams.Data.Csz == 0) { + Status = XhcSetInterface (Xhc, SlotId, DeviceSpeed, Xhc->UsbDevContext[SlotId].ConfDesc[Xhc->UsbDevContext[SlotId].ActiveConfiguration - 1], Request); + } else { + Status = XhcSetInterface64 (Xhc, SlotId, DeviceSpeed, Xhc->UsbDevContext[SlotId].ConfDesc[Xhc->UsbDevContext[SlotId].ActiveConfiguration - 1], Request); + } + } + } else if ((Request->Request == USB_REQ_GET_STATUS) && + (Request->RequestType == USB_REQUEST_TYPE (EfiUsbDataIn, USB_REQ_TYPE_CLASS, USB_TARGET_OTHER))) { + ASSERT (Data != NULL); // // Hook Get_Status request from UsbBus to keep track of the port status change. // @@ -946,47 +1101,70 @@ XhcControlTransfer ( // // For super speed hub, its bit10~12 presents the attached device speed. // - if ((*(UINT32 *)Data & XHC_PORTSC_PS) >> 10 == 0) { + if ((State & XHC_PORTSC_PS) >> 10 == 0) { PortStatus.PortStatus |= USB_PORT_STAT_SUPER_SPEED; } - } else if (DeviceSpeed == EFI_USB_SPEED_HIGH) { + } else { // - // For high speed hub, its bit9~10 presents the attached device speed. + // For high or full/low speed hub, its bit9~10 presents the attached device speed. // - if (XHC_BIT_IS_SET (*(UINT32 *)Data, BIT9)) { + if (XHC_BIT_IS_SET (State, BIT9)) { PortStatus.PortStatus |= USB_PORT_STAT_LOW_SPEED; - } else if (XHC_BIT_IS_SET (*(UINT32 *)Data, BIT10)) { + } else if (XHC_BIT_IS_SET (State, BIT10)) { PortStatus.PortStatus |= USB_PORT_STAT_HIGH_SPEED; } - } else { - ASSERT (0); } // // Convert the XHCI port/port change state to UEFI status // - MapSize = sizeof (mUsbPortStateMap) / sizeof (USB_PORT_STATE_MAP); + MapSize = sizeof (mUsbHubPortStateMap) / sizeof (USB_PORT_STATE_MAP); for (Index = 0; Index < MapSize; Index++) { - if (XHC_BIT_IS_SET (*(UINT32 *)Data, mUsbPortStateMap[Index].HwState)) { - PortStatus.PortStatus = (UINT16) (PortStatus.PortStatus | mUsbPortStateMap[Index].UefiState); + if (XHC_BIT_IS_SET (State, mUsbHubPortStateMap[Index].HwState)) { + PortStatus.PortStatus = (UINT16) (PortStatus.PortStatus | mUsbHubPortStateMap[Index].UefiState); } } - MapSize = sizeof (mUsbPortChangeMap) / sizeof (USB_PORT_STATE_MAP); + MapSize = sizeof (mUsbHubPortChangeMap) / sizeof (USB_PORT_STATE_MAP); for (Index = 0; Index < MapSize; Index++) { - if (XHC_BIT_IS_SET (*(UINT32 *)Data, mUsbPortChangeMap[Index].HwState)) { - PortStatus.PortChangeStatus = (UINT16) (PortStatus.PortChangeStatus | mUsbPortChangeMap[Index].UefiState); + if (XHC_BIT_IS_SET (State, mUsbHubPortChangeMap[Index].HwState)) { + PortStatus.PortChangeStatus = (UINT16) (PortStatus.PortChangeStatus | mUsbHubPortChangeMap[Index].UefiState); } } - XhcPollPortStatusChange (Xhc, UsbDevContext[SlotId].RouteString, (UINT8)Request->Index, &PortStatus); - } + MapSize = sizeof (mUsbHubClearPortChangeMap) / sizeof (USB_CLEAR_PORT_MAP); + + for (Index = 0; Index < MapSize; Index++) { + if (XHC_BIT_IS_SET (State, mUsbHubClearPortChangeMap[Index].HwState)) { + ZeroMem (&ClearPortRequest, sizeof (EFI_USB_DEVICE_REQUEST)); + ClearPortRequest.RequestType = USB_REQUEST_TYPE (EfiUsbNoData, USB_REQ_TYPE_CLASS, USB_TARGET_OTHER); + ClearPortRequest.Request = (UINT8) USB_REQ_CLEAR_FEATURE; + ClearPortRequest.Value = mUsbHubClearPortChangeMap[Index].Selector; + ClearPortRequest.Index = Request->Index; + ClearPortRequest.Length = 0; + + XhcControlTransfer ( + This, + DeviceAddress, + DeviceSpeed, + MaximumPacketLength, + &ClearPortRequest, + EfiUsbNoData, + NULL, + &Len, + Timeout, + Translator, + TransferResult + ); + } + } -FREE_URB: - FreePool (Urb); + XhcPollPortStatusChange (Xhc, Xhc->UsbDevContext[SlotId].RouteString, (UINT8)Request->Index, &PortStatus); -ON_EXIT: + *(UINT32 *)Data = *(UINT32*)&PortStatus; + } +ON_EXIT: if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "XhcControlTransfer: error - %r, transfer - %x\n", Status, *TransferResult)); } @@ -1014,7 +1192,7 @@ ON_EXIT: @param DataToggle On input, the initial data toggle for the transfer; On output, it is updated to to next data toggle to use of the subsequent bulk transfer. - @param TimeOut Indicates the maximum time, in millisecond, which + @param Timeout Indicates the maximum time, in millisecond, which the transfer is allowed to complete. @param Translator A pointr to the transaction translator data. @param TransferResult A pointer to the detailed result information of the @@ -1039,17 +1217,14 @@ XhcBulkTransfer ( IN OUT VOID *Data[EFI_USB_MAX_BULK_BUFFER_NUM], IN OUT UINTN *DataLength, IN OUT UINT8 *DataToggle, - IN UINTN TimeOut, + IN UINTN Timeout, IN EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator, OUT UINT32 *TransferResult ) { - USB_XHCI_DEV *Xhc; - URB *Urb; - UINT8 XhciDevAddr; + USB_XHCI_INSTANCE *Xhc; UINT8 SlotId; EFI_STATUS Status; - EFI_STATUS RecoveryStatus; EFI_TPL OldTpl; // @@ -1086,59 +1261,30 @@ XhcBulkTransfer ( // // Check if the device is still enabled before every transaction. // - SlotId = XhcBusDevAddrToSlotId (DeviceAddress); + SlotId = XhcBusDevAddrToSlotId (Xhc, DeviceAddress); if (SlotId == 0) { goto ON_EXIT; } - // - // Acquire the actual device address assigned by XHCI's Address_Device cmd. - // - XhciDevAddr = UsbDevContext[SlotId].XhciDevAddr; - // // Create a new URB, insert it into the asynchronous // schedule list, then poll the execution status. // - Urb = XhcCreateUrb ( - Xhc, - XhciDevAddr, - EndPointAddress, - DeviceSpeed, - MaximumPacketLength, - XHC_BULK_TRANSFER, - NULL, - Data[0], - *DataLength, - NULL, - NULL - ); - - if (Urb == NULL) { - DEBUG ((EFI_D_ERROR, "XhcBulkTransfer: failed to create URB\n")); - Status = EFI_OUT_OF_RESOURCES; - goto ON_EXIT; - } - - ASSERT (Urb->EvtRing == &Xhc->BulkTrEventRing); - - Status = XhcExecTransfer (Xhc, FALSE, Urb, TimeOut); - - *TransferResult = Urb->Result; - *DataLength = Urb->Completed; - - if (*TransferResult == EFI_USB_NOERROR) { - Status = EFI_SUCCESS; - } else if ((*TransferResult == EFI_USB_ERR_STALL) || - (*TransferResult == EFI_USB_ERR_TIMEOUT)) { - RecoveryStatus = XhcRecoverHaltedEndpoint(Xhc, Urb); - ASSERT_EFI_ERROR (RecoveryStatus); - } - - FreePool (Urb); + Status = XhcTransfer ( + Xhc, + DeviceAddress, + EndPointAddress, + DeviceSpeed, + MaximumPacketLength, + XHC_BULK_TRANSFER, + NULL, + Data[0], + DataLength, + Timeout, + TransferResult + ); ON_EXIT: - if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "XhcBulkTransfer: error - %r, transfer - %x\n", Status, *TransferResult)); } @@ -1193,10 +1339,9 @@ XhcAsyncInterruptTransfer ( IN VOID *Context OPTIONAL ) { - USB_XHCI_DEV *Xhc; + USB_XHCI_INSTANCE *Xhc; URB *Urb; EFI_STATUS Status; - UINT8 XhciDevAddr; UINT8 SlotId; UINT8 Index; UINT8 *Data; @@ -1235,8 +1380,7 @@ XhcAsyncInterruptTransfer ( // The delete request may happen after device is detached. // for (Index = 0; Index < 255; Index++) { - if ((UsbDevContext[Index + 1].SlotId != 0) && - (UsbDevContext[Index + 1].BusDevAddr == DeviceAddress)) { + if (Xhc->UsbDevContext[Index + 1].BusDevAddr == DeviceAddress) { break; } } @@ -1246,13 +1390,8 @@ XhcAsyncInterruptTransfer ( goto ON_EXIT; } - // - // Acquire the actual device address assigned by XHCI's Address_Device cmd. - // - XhciDevAddr = UsbDevContext[Index + 1].XhciDevAddr; - - Status = XhciDelAsyncIntTransfer (Xhc, XhciDevAddr, EndPointAddress); - DEBUG ((EFI_D_INFO, "XhcAsyncInterruptTransfer: remove old transfer, Status = %r\n", Status)); + Status = XhciDelAsyncIntTransfer (Xhc, DeviceAddress, EndPointAddress); + DEBUG ((EFI_D_INFO, "XhcAsyncInterruptTransfer: remove old transfer for addr %d, Status = %r\n", DeviceAddress, Status)); goto ON_EXIT; } @@ -1267,17 +1406,12 @@ XhcAsyncInterruptTransfer ( // // Check if the device is still enabled before every transaction. // - SlotId = XhcBusDevAddrToSlotId (DeviceAddress); + SlotId = XhcBusDevAddrToSlotId (Xhc, DeviceAddress); if (SlotId == 0) { goto ON_EXIT; } - // - // Acquire the actual device address assigned by XHCI's Address_Device cmd. - // - XhciDevAddr = UsbDevContext[SlotId].XhciDevAddr; - - Data = AllocatePool (DataLength); + Data = AllocateZeroPool (DataLength); if (Data == NULL) { DEBUG ((EFI_D_ERROR, "XhcAsyncInterruptTransfer: failed to allocate buffer\n")); @@ -1287,7 +1421,7 @@ XhcAsyncInterruptTransfer ( Urb = XhcCreateUrb ( Xhc, - XhciDevAddr, + DeviceAddress, EndPointAddress, DeviceSpeed, MaximumPacketLength, @@ -1306,8 +1440,6 @@ XhcAsyncInterruptTransfer ( goto ON_EXIT; } - ASSERT (Urb->EvtRing == &Xhc->AsynIntTrEventRing); - InsertHeadList (&Xhc->AsyncIntTransfers, &Urb->UrbList); // // Ring the doorbell @@ -1315,6 +1447,7 @@ XhcAsyncInterruptTransfer ( Status = RingIntTransferDoorBell (Xhc, Urb); ON_EXIT: + Xhc->PciIo->Flush (Xhc->PciIo); gBS->RestoreTPL (OldTpl); return Status; @@ -1337,7 +1470,7 @@ ON_EXIT: output, the number of bytes transferred. @param DataToggle On input, the initial data toggle to use; on output, it is updated to indicate the next data toggle. - @param TimeOut Maximum time, in second, to complete. + @param Timeout Maximum time, in second, to complete. @param Translator Transaction translator to use. @param TransferResult Variable to receive the transfer result. @@ -1359,17 +1492,14 @@ XhcSyncInterruptTransfer ( IN OUT VOID *Data, IN OUT UINTN *DataLength, IN OUT UINT8 *DataToggle, - IN UINTN TimeOut, + IN UINTN Timeout, IN EFI_USB2_HC_TRANSACTION_TRANSLATOR *Translator, OUT UINT32 *TransferResult ) { - USB_XHCI_DEV *Xhc; - URB *Urb; - UINT8 XhciDevAddr; + USB_XHCI_INSTANCE *Xhc; UINT8 SlotId; EFI_STATUS Status; - EFI_STATUS RecoveryStatus; EFI_TPL OldTpl; // @@ -1380,10 +1510,6 @@ XhcSyncInterruptTransfer ( return EFI_INVALID_PARAMETER; } - if (!XHCI_IS_DATAIN (EndPointAddress)) { - return EFI_INVALID_PARAMETER; - } - if ((*DataToggle != 1) && (*DataToggle != 0)) { return EFI_INVALID_PARAMETER; } @@ -1409,50 +1535,24 @@ XhcSyncInterruptTransfer ( // // Check if the device is still enabled before every transaction. // - SlotId = XhcBusDevAddrToSlotId (DeviceAddress); + SlotId = XhcBusDevAddrToSlotId (Xhc, DeviceAddress); if (SlotId == 0) { goto ON_EXIT; } - // - // Acquire the actual device address assigned by XHCI's Address_Device cmd. - // - XhciDevAddr = UsbDevContext[SlotId].XhciDevAddr; - - Urb = XhcCreateUrb ( - Xhc, - XhciDevAddr, - EndPointAddress, - DeviceSpeed, - MaximumPacketLength, - XHC_INT_TRANSFER_SYNC, - NULL, - Data, - *DataLength, - NULL, - NULL - ); - - if (Urb == NULL) { - DEBUG ((EFI_D_ERROR, "XhcSyncInterruptTransfer: failed to create URB\n")); - Status = EFI_OUT_OF_RESOURCES; - goto ON_EXIT; - } - - Status = XhcExecTransfer (Xhc, FALSE, Urb, TimeOut); - - *TransferResult = Urb->Result; - *DataLength = Urb->Completed; - - if (*TransferResult == EFI_USB_NOERROR) { - Status = EFI_SUCCESS; - } else if ((*TransferResult == EFI_USB_ERR_STALL) || - (*TransferResult == EFI_USB_ERR_TIMEOUT)) { - RecoveryStatus = XhcRecoverHaltedEndpoint(Xhc, Urb); - ASSERT_EFI_ERROR (RecoveryStatus); - } - - FreePool (Urb); + Status = XhcTransfer ( + Xhc, + DeviceAddress, + EndPointAddress, + DeviceSpeed, + MaximumPacketLength, + XHC_INT_TRANSFER_SYNC, + NULL, + Data, + DataLength, + Timeout, + TransferResult + ); ON_EXIT: if (EFI_ERROR (Status)) { @@ -1649,57 +1749,42 @@ ON_EXIT: } /** - Create and initialize a USB_XHCI_DEV. + Create and initialize a USB_XHCI_INSTANCE structure. @param PciIo The PciIo on this device. + @param DevicePath The device path of host controller. @param OriginalPciAttributes Original PCI attributes. - @return The allocated and initialized USB_XHCI_DEV structure if created, + @return The allocated and initialized USB_XHCI_INSTANCE structure if created, otherwise NULL. **/ -USB_XHCI_DEV* +USB_XHCI_INSTANCE* XhcCreateUsbHc ( - IN EFI_PCI_IO_PROTOCOL *PciIo, - IN UINT64 OriginalPciAttributes + IN EFI_PCI_IO_PROTOCOL *PciIo, + IN EFI_DEVICE_PATH_PROTOCOL *DevicePath, + IN UINT64 OriginalPciAttributes ) { - USB_XHCI_DEV *Xhc; + USB_XHCI_INSTANCE *Xhc; EFI_STATUS Status; UINT32 PageSize; UINT16 ExtCapReg; - ZeroMem (UsbDevContext, sizeof (UsbDevContext)); - - Xhc = AllocateZeroPool (sizeof (USB_XHCI_DEV)); + Xhc = AllocateZeroPool (sizeof (USB_XHCI_INSTANCE)); if (Xhc == NULL) { return NULL; } // - // Init EFI_USB2_HC_PROTOCOL interface and private data structure + // Initialize private data structure // - Xhc->Signature = USB_XHCI_DEV_SIGNATURE; - - Xhc->Usb2Hc.GetCapability = XhcGetCapability; - Xhc->Usb2Hc.Reset = XhcReset; - Xhc->Usb2Hc.GetState = XhcGetState; - Xhc->Usb2Hc.SetState = XhcSetState; - Xhc->Usb2Hc.ControlTransfer = XhcControlTransfer; - Xhc->Usb2Hc.BulkTransfer = XhcBulkTransfer; - Xhc->Usb2Hc.AsyncInterruptTransfer = XhcAsyncInterruptTransfer; - Xhc->Usb2Hc.SyncInterruptTransfer = XhcSyncInterruptTransfer; - Xhc->Usb2Hc.IsochronousTransfer = XhcIsochronousTransfer; - Xhc->Usb2Hc.AsyncIsochronousTransfer = XhcAsyncIsochronousTransfer; - Xhc->Usb2Hc.GetRootHubPortStatus = XhcGetRootHubPortStatus; - Xhc->Usb2Hc.SetRootHubPortFeature = XhcSetRootHubPortFeature; - Xhc->Usb2Hc.ClearRootHubPortFeature = XhcClearRootHubPortFeature; - Xhc->Usb2Hc.MajorRevision = 0x3; - Xhc->Usb2Hc.MinorRevision = 0x0; - + Xhc->Signature = XHCI_INSTANCE_SIG; Xhc->PciIo = PciIo; + Xhc->DevicePath = DevicePath; Xhc->OriginalPciAttributes = OriginalPciAttributes; + CopyMem (&Xhc->Usb2Hc, &gXhciUsb2HcTemplate, sizeof (EFI_USB2_HC_PROTOCOL)); InitializeListHead (&Xhc->AsyncIntTransfers); @@ -1720,26 +1805,27 @@ XhcCreateUsbHc ( // PageSize = XhcReadOpReg(Xhc, XHC_PAGESIZE_OFFSET) & XHC_PAGESIZE_MASK; Xhc->PageSize = 1 << (HighBitSet32(PageSize) + 12); - ASSERT (Xhc->PageSize == 0x1000); - ExtCapReg = (UINT16) (Xhc->HcCParams.Data.ExtCapReg); - Xhc->ExtCapRegBase = ExtCapReg << 2; - Xhc->UsbLegSupOffset = XhcGetLegSupCapAddr (Xhc); + ExtCapReg = (UINT16) (Xhc->HcCParams.Data.ExtCapReg); + Xhc->ExtCapRegBase = ExtCapReg << 2; + Xhc->UsbLegSupOffset = XhcGetCapabilityAddr (Xhc, XHC_CAP_USB_LEGACY); + Xhc->DebugCapSupOffset = XhcGetCapabilityAddr (Xhc, XHC_CAP_USB_DEBUG); - DEBUG ((EFI_D_INFO, "XhcCreateUsb3Hc: capability length 0x%x\n", Xhc->CapLength)); + DEBUG ((EFI_D_INFO, "XhcCreateUsb3Hc: Capability length 0x%x\n", Xhc->CapLength)); DEBUG ((EFI_D_INFO, "XhcCreateUsb3Hc: HcSParams1 0x%x\n", Xhc->HcSParams1)); DEBUG ((EFI_D_INFO, "XhcCreateUsb3Hc: HcSParams2 0x%x\n", Xhc->HcSParams2)); DEBUG ((EFI_D_INFO, "XhcCreateUsb3Hc: HcCParams 0x%x\n", Xhc->HcCParams)); DEBUG ((EFI_D_INFO, "XhcCreateUsb3Hc: DBOff 0x%x\n", Xhc->DBOff)); DEBUG ((EFI_D_INFO, "XhcCreateUsb3Hc: RTSOff 0x%x\n", Xhc->RTSOff)); - DEBUG ((EFI_D_INFO, "XhcCreateUsb3Hc: Xhc->UsbLegSupOffset 0x%x\n", Xhc->UsbLegSupOffset)); + DEBUG ((EFI_D_INFO, "XhcCreateUsb3Hc: UsbLegSupOffset 0x%x\n", Xhc->UsbLegSupOffset)); + DEBUG ((EFI_D_INFO, "XhcCreateUsb3Hc: DebugCapSupOffset 0x%x\n", Xhc->DebugCapSupOffset)); // // Create AsyncRequest Polling Timer // Status = gBS->CreateEvent ( EVT_TIMER | EVT_NOTIFY_SIGNAL, - TPL_CALLBACK, + TPL_NOTIFY, XhcMonitorAsyncRequests, Xhc, &Xhc->PollTimer @@ -1760,7 +1846,7 @@ ON_ERROR: One notified function to stop the Host Controller when gBS->ExitBootServices() called. @param Event Pointer to this event - @param Context Event hanlder private data + @param Context Event handler private data **/ VOID @@ -1771,23 +1857,25 @@ XhcExitBootService ( ) { - USB_XHCI_DEV *Xhc; + USB_XHCI_INSTANCE *Xhc; EFI_PCI_IO_PROTOCOL *PciIo; - Xhc = (USB_XHCI_DEV*) Context; + Xhc = (USB_XHCI_INSTANCE*) Context; PciIo = Xhc->PciIo; // // Stop AsyncRequest Polling timer then stop the XHCI driver // and uninstall the XHCI protocl. // - gBS->SetTimer (Xhc->PollTimer, TimerCancel, XHC_ASYNC_POLL_INTERVAL); + gBS->SetTimer (Xhc->PollTimer, TimerCancel, 0); XhcHaltHC (Xhc, XHC_GENERIC_TIMEOUT); if (Xhc->PollTimer != NULL) { gBS->CloseEvent (Xhc->PollTimer); } + XhcClearBiosOwnership (Xhc); + // // Restore original PCI attributes // @@ -1797,8 +1885,6 @@ XhcExitBootService ( Xhc->OriginalPciAttributes, NULL ); - - XhcClearBiosOwnership (Xhc); } /** @@ -1827,7 +1913,8 @@ XhcDriverBindingStart ( UINT64 Supports; UINT64 OriginalPciAttributes; BOOLEAN PciAttributesSaved; - USB_XHCI_DEV *Xhc; + USB_XHCI_INSTANCE *Xhc; + EFI_DEVICE_PATH_PROTOCOL *HcDevicePath; // // Open the PciIo Protocol, then enable the USB host controller @@ -1845,6 +1932,19 @@ XhcDriverBindingStart ( return Status; } + // + // Open Device Path Protocol for on USB host controller + // + HcDevicePath = NULL; + Status = gBS->OpenProtocol ( + Controller, + &gEfiDevicePathProtocolGuid, + (VOID **) &HcDevicePath, + This->DriverBindingHandle, + Controller, + EFI_OPEN_PROTOCOL_GET_PROTOCOL + ); + PciAttributesSaved = FALSE; // // Save original PCI attributes @@ -1868,7 +1968,7 @@ XhcDriverBindingStart ( &Supports ); if (!EFI_ERROR (Status)) { - Supports &= EFI_PCI_DEVICE_ENABLE; + Supports &= (UINT64)EFI_PCI_DEVICE_ENABLE; Status = PciIo->Attributes ( PciIo, EfiPciIoAttributeOperationEnable, @@ -1885,13 +1985,33 @@ XhcDriverBindingStart ( // // Create then install USB2_HC_PROTOCOL // - Xhc = XhcCreateUsbHc (PciIo, OriginalPciAttributes); + Xhc = XhcCreateUsbHc (PciIo, HcDevicePath, OriginalPciAttributes); if (Xhc == NULL) { DEBUG ((EFI_D_ERROR, "XhcDriverBindingStart: failed to create USB2_HC\n")); return EFI_OUT_OF_RESOURCES; } + // + // Enable 64-bit DMA support in the PCI layer if this controller + // supports it. + // + if (Xhc->HcCParams.Data.Ac64 != 0) { + Status = PciIo->Attributes ( + PciIo, + EfiPciIoAttributeOperationEnable, + EFI_PCI_IO_ATTRIBUTE_DUAL_ADDRESS_CYCLE, + NULL + ); + if (!EFI_ERROR (Status)) { + Xhc->Support64BitDma = TRUE; + } else { + DEBUG ((EFI_D_WARN, + "%a: failed to enable 64-bit DMA on 64-bit capable controller @ %p (%r)\n", + __FUNCTION__, Controller, Status)); + } + } + XhcSetBiosOwnership (Xhc); XhcResetHC (Xhc, XHC_RESET_TIMEOUT); @@ -1916,7 +2036,7 @@ XhcDriverBindingStart ( // // Start the asynchronous interrupt monitor // - Status = gBS->SetTimer (Xhc->PollTimer, TimerPeriodic, XHC_ASYNC_POLL_INTERVAL); + Status = gBS->SetTimer (Xhc->PollTimer, TimerPeriodic, XHC_ASYNC_TIMER_INTERVAL); if (EFI_ERROR (Status)) { DEBUG ((EFI_D_ERROR, "XhcDriverBindingStart: failed to start async interrupt monitor\n")); XhcHaltHC (Xhc, XHC_GENERIC_TIMEOUT); @@ -2001,7 +2121,7 @@ CLOSE_PCIIO: /** - Stop this driver on ControllerHandle. Support stoping any child handles + Stop this driver on ControllerHandle. Support stopping any child handles created by this driver. @param This Protocol instance pointer. @@ -2025,7 +2145,8 @@ XhcDriverBindingStop ( EFI_STATUS Status; EFI_USB2_HC_PROTOCOL *Usb2Hc; EFI_PCI_IO_PROTOCOL *PciIo; - USB_XHCI_DEV *Xhc; + USB_XHCI_INSTANCE *Xhc; + UINT8 Index; // // Test whether the Controller handler passed in is a valid @@ -2045,17 +2166,6 @@ XhcDriverBindingStop ( return Status; } - Xhc = XHC_FROM_THIS (Usb2Hc); - PciIo = Xhc->PciIo; - - // - // Stop AsyncRequest Polling timer then stop the XHCI driver - // and uninstall the XHCI protocl. - // - gBS->SetTimer (Xhc->PollTimer, TimerCancel, XHC_ASYNC_POLL_INTERVAL); - XhcHaltHC (Xhc, XHC_GENERIC_TIMEOUT); - XhcClearBiosOwnership (Xhc); - Status = gBS->UninstallProtocolInterface ( Controller, &gEfiUsb2HcProtocolGuid, @@ -2066,6 +2176,31 @@ XhcDriverBindingStop ( return Status; } + Xhc = XHC_FROM_THIS (Usb2Hc); + PciIo = Xhc->PciIo; + + // + // Stop AsyncRequest Polling timer then stop the XHCI driver + // and uninstall the XHCI protocl. + // + gBS->SetTimer (Xhc->PollTimer, TimerCancel, 0); + + // + // Disable the device slots occupied by these devices on its downstream ports. + // Entry 0 is reserved. + // + for (Index = 0; Index < 255; Index++) { + if (!Xhc->UsbDevContext[Index + 1].Enabled || + (Xhc->UsbDevContext[Index + 1].SlotId == 0)) { + continue; + } + if (Xhc->HcCParams.Data.Csz == 0) { + XhcDisableSlotCmd (Xhc, Xhc->UsbDevContext[Index + 1].SlotId); + } else { + XhcDisableSlotCmd64 (Xhc, Xhc->UsbDevContext[Index + 1].SlotId); + } + } + if (Xhc->PollTimer != NULL) { gBS->CloseEvent (Xhc->PollTimer); } @@ -2074,6 +2209,8 @@ XhcDriverBindingStop ( gBS->CloseEvent (Xhc->ExitBootServiceEvent); } + XhcHaltHC (Xhc, XHC_GENERIC_TIMEOUT); + XhcClearBiosOwnership (Xhc); XhciDelAllAsyncIntTransfers (Xhc); XhcFreeSched (Xhc); @@ -2085,11 +2222,11 @@ XhcDriverBindingStop ( // Restore original PCI attributes // PciIo->Attributes ( - PciIo, - EfiPciIoAttributeOperationSet, - Xhc->OriginalPciAttributes, - NULL - ); + PciIo, + EfiPciIoAttributeOperationSet, + Xhc->OriginalPciAttributes, + NULL + ); gBS->CloseProtocol ( Controller,