Changeset 58459 in vbox for trunk/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c
- Timestamp:
- Oct 28, 2015 8:17:18 PM (9 years ago)
- Location:
- trunk/src/VBox/Devices/EFI/Firmware
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Devices/EFI/Firmware
-
Property svn:mergeinfo
set to (toggle deleted branches)
/vendor/edk2/current 103735-103757
-
Property svn:mergeinfo
set to (toggle deleted branches)
-
trunk/src/VBox/Devices/EFI/Firmware/MdeModulePkg/Bus/Pci/XhciDxe/XhciSched.c
r48674 r58459 3 3 XHCI transfer scheduling routines. 4 4 5 Copyright (c) 2011 - 201 2, Intel Corporation. All rights reserved.<BR>5 Copyright (c) 2011 - 2014, Intel Corporation. All rights reserved.<BR> 6 6 This program and the accompanying materials 7 7 are licensed and made available under the terms and conditions of the BSD License … … 110 110 } 111 111 112 FreePool (Urb);112 XhcFreeUrb (Xhc, Urb); 113 113 114 114 ON_EXIT: … … 177 177 Status = XhcCreateTransferTrb (Xhc, Urb); 178 178 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 } 179 184 180 185 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 **/ 195 VOID 196 XhcFreeUrb ( 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); 181 210 } 182 211 … … 205 234 UINTN Len; 206 235 UINTN TrbNum; 236 EFI_PCI_IO_PROTOCOL_OPERATION MapOp; 237 EFI_PHYSICAL_ADDRESS PhyAddr; 238 VOID *Map; 239 EFI_STATUS Status; 207 240 208 241 SlotId = XhcBusDevAddrToSlotId (Xhc, Urb->Ep.BusAddr); … … 221 254 EPRing = (TRANSFER_RING *)(UINTN) Xhc->UsbDevContext[SlotId].EndpointTransferRing[Dci-1]; 222 255 Urb->Ring = EPRing; 223 OutputContext = (VOID *)(UINTN)Xhc->DCBAA[SlotId];256 OutputContext = Xhc->UsbDevContext[SlotId].OutputContext; 224 257 if (Xhc->HcCParams.Data.Csz == 0) { 225 258 EPType = (UINT8) ((DEVICE_CONTEXT *)OutputContext)->EP[Dci-1].EPType; 226 259 } else { 227 260 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; 228 280 } 229 281 … … 268 320 XhcSyncTrsRing (Xhc, EPRing); 269 321 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); 272 324 TrbStart->TrbCtrData.Lenth = (UINT32) Urb->DataLen; 273 325 TrbStart->TrbCtrData.TDSize = 0; … … 334 386 } 335 387 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); 338 390 TrbStart->TrbNormal.Lenth = (UINT32) Len; 339 391 TrbStart->TrbNormal.TDSize = 0; … … 369 421 } 370 422 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); 373 425 TrbStart->TrbNormal.Lenth = (UINT32) Len; 374 426 TrbStart->TrbNormal.TDSize = 0; … … 413 465 { 414 466 VOID *Dcbaa; 467 EFI_PHYSICAL_ADDRESS DcbaaPhy; 415 468 UINT64 CmdRing; 469 EFI_PHYSICAL_ADDRESS CmdRingPhy; 416 470 UINTN Entries; 417 471 UINT32 MaxScratchpadBufs; 418 472 UINT64 *ScratchBuf; 419 UINT64 *ScratchEntryBuf; 473 EFI_PHYSICAL_ADDRESS ScratchPhy; 474 UINT64 *ScratchEntry; 475 EFI_PHYSICAL_ADDRESS ScratchEntryPhy; 420 476 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); 421 485 422 486 // … … 435 499 // 436 500 Entries = (Xhc->MaxSlotsEn + 1) * sizeof(UINT64); 437 Dcbaa = AllocatePages (EFI_SIZE_TO_PAGES (Entries));501 Dcbaa = UsbHcAllocateMem (Xhc->MemPool, Entries); 438 502 ASSERT (Dcbaa != NULL); 439 503 ZeroMem (Dcbaa, Entries); … … 448 512 ASSERT (MaxScratchpadBufs <= 1023); 449 513 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 452 539 ZeroMem (ScratchBuf, MaxScratchpadBufs * sizeof (UINT64)); 453 540 Xhc->ScratchBuf = ScratchBuf; 454 541 542 // 543 // Allocate each scratch buffer 544 // 455 545 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 } 462 562 // 463 563 // The Scratchpad Buffer Array contains pointers to the Scratchpad Buffers. Entry 0 of the 464 564 // Device Context Base Address Array points to the Scratchpad Buffer Array. 465 565 // 466 *(UINT64 *)Dcbaa = (UINT64)(UINTN) Xhc->ScratchBuf;566 *(UINT64 *)Dcbaa = (UINT64)(UINTN) ScratchPhy; 467 567 } 468 568 … … 476 576 // So divide it to two 32-bytes width register access. 477 577 // 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 480 582 DEBUG ((EFI_D_INFO, "XhcInitSched:DCBAA=0x%x\n", (UINT64)(UINTN)Xhc->DCBAA)); 481 583 … … 493 595 // 494 596 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; 497 600 // 498 601 // Some 3rd party XHCI external cards don't support single 64-bytes width register access, 499 602 // So divide it to two 32-bytes width register access. 500 603 // 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)); 503 606 504 607 DEBUG ((EFI_D_INFO, "XhcInitSched:XHC_CRCR=0x%x\n", Xhc->CmdRing.RingSeg0)); … … 548 651 UINT8 Dci; 549 652 UINT8 SlotId; 653 EFI_PHYSICAL_ADDRESS PhyAddr; 550 654 551 655 Status = EFI_SUCCESS; … … 573 677 (TRB_TEMPLATE **) (UINTN) &EvtTrb 574 678 ); 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 } 576 683 577 684 // … … 579 686 // 580 687 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); 583 691 CmdSetTRDeq.CycleBit = 1; 584 692 CmdSetTRDeq.Type = TRB_TYPE_SET_TR_DEQUE; … … 591 699 (TRB_TEMPLATE **) (UINTN) &EvtTrb 592 700 ); 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 } 594 705 595 706 // … … 598 709 XhcRingDoorBell (Xhc, SlotId, Dci); 599 710 711 Done: 600 712 return Status; 601 713 } … … 616 728 VOID *Buf; 617 729 EVENT_RING_SEG_TABLE_ENTRY *ERSTBase; 730 UINTN Size; 731 EFI_PHYSICAL_ADDRESS ERSTPhy; 732 EFI_PHYSICAL_ADDRESS DequeuePhy; 618 733 619 734 ASSERT (EventRing != NULL); 620 735 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); 622 738 ASSERT (Buf != NULL); 623 739 ASSERT (((UINTN) Buf & 0x3F) == 0); 624 ZeroMem (Buf, sizeof (TRB_TEMPLATE) * EVENT_RING_TRB_NUMBER);740 ZeroMem (Buf, Size); 625 741 626 742 EventRing->EventRingSeg0 = Buf; … … 628 744 EventRing->EventRingDequeue = (TRB_TEMPLATE *) EventRing->EventRingSeg0; 629 745 EventRing->EventRingEnqueue = (TRB_TEMPLATE *) EventRing->EventRingSeg0; 746 747 DequeuePhy = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, Buf, Size); 748 630 749 // 631 750 // Software maintains an Event Ring Consumer Cycle State (CCS) bit, initializing it to '1' … … 634 753 EventRing->EventRingCCS = 1; 635 754 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); 637 757 ASSERT (Buf != NULL); 638 758 ASSERT (((UINTN) Buf & 0x3F) == 0); 639 ZeroMem (Buf, sizeof (EVENT_RING_SEG_TABLE_ENTRY) * ERST_NUMBER);759 ZeroMem (Buf, Size); 640 760 641 761 ERSTBase = (EVENT_RING_SEG_TABLE_ENTRY *) Buf; 642 762 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); 645 765 ERSTBase->RingTrbSize = EVENT_RING_TRB_NUMBER; 766 767 ERSTPhy = UsbHcGetPciAddrForHostAddr (Xhc->MemPool, ERSTBase, Size); 646 768 647 769 // … … 662 784 Xhc, 663 785 XHC_ERDP_OFFSET, 664 XHC_LOW_32BIT((UINT64)(UINTN) EventRing->EventRingDequeue)786 XHC_LOW_32BIT((UINT64)(UINTN)DequeuePhy) 665 787 ); 666 788 XhcWriteRuntimeReg ( 667 789 Xhc, 668 790 XHC_ERDP_OFFSET + 4, 669 XHC_HIGH_32BIT((UINT64)(UINTN) EventRing->EventRingDequeue)791 XHC_HIGH_32BIT((UINT64)(UINTN)DequeuePhy) 670 792 ); 671 793 // … … 678 800 Xhc, 679 801 XHC_ERSTBA_OFFSET, 680 XHC_LOW_32BIT((UINT64)(UINTN)ERST Base)802 XHC_LOW_32BIT((UINT64)(UINTN)ERSTPhy) 681 803 ); 682 804 XhcWriteRuntimeReg ( 683 805 Xhc, 684 806 XHC_ERSTBA_OFFSET + 4, 685 XHC_HIGH_32BIT((UINT64)(UINTN)ERST Base)807 XHC_HIGH_32BIT((UINT64)(UINTN)ERSTPhy) 686 808 ); 687 809 // … … 708 830 VOID *Buf; 709 831 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); 712 835 ASSERT (Buf != NULL); 713 836 ASSERT (((UINTN) Buf & 0x3F) == 0); … … 726 849 EndTrb = (LINK_TRB *) ((UINTN)Buf + sizeof (TRB_TEMPLATE) * (TrbNum - 1)); 727 850 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); 730 854 // 731 855 // Toggle Cycle (TC). When set to '1', the xHC shall toggle its interpretation of the Cycle bit. … … 752 876 ) 753 877 { 754 UINT8 Index;755 EVENT_RING_SEG_TABLE_ENTRY *TablePtr;756 VOID *RingBuf;757 EVENT_RING_SEG_TABLE_ENTRY *EventRingPtr;758 759 878 if(EventRing->EventRingSeg0 == NULL) { 760 879 return EFI_SUCCESS; … … 762 881 763 882 // 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); 782 891 return EFI_SUCCESS; 783 892 } … … 794 903 ) 795 904 { 796 UINT32 Index;797 UINT64 *ScratchBuf;798 905 UINT32 Index; 906 UINT64 *ScratchEntry; 907 799 908 if (Xhc->ScratchBuf != NULL) { 800 Scratch Buf = Xhc->ScratchBuf;909 ScratchEntry = Xhc->ScratchEntry; 801 910 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); 806 930 807 931 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)); 809 933 Xhc->DCBAA = NULL; 810 934 } 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 } 818 943 } 819 944 … … 919 1044 UINT32 High; 920 1045 UINT32 Low; 1046 EFI_PHYSICAL_ADDRESS PhyAddr; 921 1047 922 1048 ASSERT ((Xhc != NULL) && (Urb != NULL)); … … 956 1082 continue; 957 1083 } 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)); 960 1090 961 1091 // … … 978 1108 CheckedUrb->Finished = TRUE; 979 1109 DEBUG ((EFI_D_ERROR, "XhcCheckUrbResult: STALL_ERROR! Completecode = %x\n",EvtTrb->Completecode)); 980 break;1110 goto EXIT; 981 1111 982 1112 case TRB_COMPLETION_BABBLE_ERROR: … … 984 1114 CheckedUrb->Finished = TRUE; 985 1115 DEBUG ((EFI_D_ERROR, "XhcCheckUrbResult: BABBLE_ERROR! Completecode = %x\n",EvtTrb->Completecode)); 986 break;1116 goto EXIT; 987 1117 988 1118 case TRB_COMPLETION_DATA_BUFFER_ERROR: … … 990 1120 CheckedUrb->Finished = TRUE; 991 1121 DEBUG ((EFI_D_ERROR, "XhcCheckUrbResult: ERR_BUFFER! Completecode = %x\n",EvtTrb->Completecode)); 992 break;1122 goto EXIT; 993 1123 994 1124 case TRB_COMPLETION_USB_TRANSACTION_ERROR: … … 996 1126 CheckedUrb->Finished = TRUE; 997 1127 DEBUG ((EFI_D_ERROR, "XhcCheckUrbResult: TRANSACTION_ERROR! Completecode = %x\n",EvtTrb->Completecode)); 998 break;1128 goto EXIT; 999 1129 1000 1130 case TRB_COMPLETION_SHORT_PACKET: … … 1017 1147 CheckedUrb->Result |= EFI_USB_ERR_TIMEOUT; 1018 1148 CheckedUrb->Finished = TRUE; 1019 break;1149 goto EXIT; 1020 1150 } 1021 1151 … … 1049 1179 XhcDequeue = (UINT64)(LShiftU64((UINT64)High, 32) | Low); 1050 1180 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))) { 1052 1184 // 1053 1185 // Some 3rd party XHCI external cards don't support single 64-bytes width register access, 1054 1186 // So divide it to two 32-bytes width register access. 1055 1187 // 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)); 1058 1190 } 1059 1191 … … 1102 1234 1103 1235 Status = EFI_SUCCESS; 1104 Loop = (Timeout * XHC_1_MILLISECOND / XHC_POLL_DELAY) + 1;1236 Loop = Timeout * XHC_1_MILLISECOND; 1105 1237 if (Timeout == 0) { 1106 1238 Loop = 0xFFFFFFFF; … … 1114 1246 break; 1115 1247 } 1116 gBS->Stall (XHC_ POLL_DELAY);1248 gBS->Stall (XHC_1_MICROSECOND); 1117 1249 } 1118 1250 … … 1160 1292 RemoveEntryList (&Urb->UrbList); 1161 1293 FreePool (Urb->Data); 1162 FreePool (Urb);1294 XhcFreeUrb (Xhc, Urb); 1163 1295 return EFI_SUCCESS; 1164 1296 } … … 1187 1319 RemoveEntryList (&Urb->UrbList); 1188 1320 FreePool (Urb->Data); 1189 FreePool (Urb);1321 XhcFreeUrb (Xhc, Urb); 1190 1322 } 1191 1323 } … … 1218 1350 } 1219 1351 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 **/ 1363 EFI_STATUS 1364 XhcFlushAsyncIntMap ( 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 1403 ON_ERROR: 1404 return EFI_DEVICE_ERROR; 1405 } 1220 1406 1221 1407 /** … … 1261 1447 // active, check the next one. 1262 1448 // 1263 Status =XhcCheckUrbResult (Xhc, Urb);1449 XhcCheckUrbResult (Xhc, Urb); 1264 1450 1265 1451 if (!Urb->Finished) { 1266 1452 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")); 1267 1462 } 1268 1463 … … 1342 1537 1343 1538 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 } 1344 1543 1345 1544 if (ParentRouteChart.Dword == 0) { … … 1357 1556 } 1358 1557 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 1359 1567 if (((PortState->PortStatus & USB_PORT_STAT_ENABLE) != 0) && 1360 1568 ((PortState->PortStatus & USB_PORT_STAT_CONNECTION) != 0)) { … … 1374 1582 // 1375 1583 SlotId = XhcRouteStringToSlotId (Xhc, RouteChart); 1376 if ( SlotId == 0) {1584 if ((SlotId == 0) && ((PortState->PortChangeStatus & USB_PORT_STAT_C_RESET) != 0)) { 1377 1585 if (Xhc->HcCParams.Data.Csz == 0) { 1378 1586 Status = XhcInitializeDeviceSlot (Xhc, ParentRouteChart, Port, RouteChart, Speed); … … 1380 1588 Status = XhcInitializeDeviceSlot64 (Xhc, ParentRouteChart, Port, RouteChart, Speed); 1381 1589 } 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 1398 1593 return Status; 1399 1594 } … … 1584 1779 // 1585 1780 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 1587 1782 } 1588 1783 } … … 1626 1821 ) 1627 1822 { 1628 EFI_STATUS Status;1629 TRB_TEMPLATE *EvtTrb;1630 1631 1823 ASSERT (EvtRing != NULL); 1632 1824 1633 EvtTrb = EvtRing->EventRingDequeue;1634 1825 *NewEvtTrb = EvtRing->EventRingDequeue; 1635 1826 … … 1638 1829 } 1639 1830 1640 Status = EFI_SUCCESS;1641 1642 1831 EvtRing->EventRingDequeue++; 1643 1832 // … … 1648 1837 } 1649 1838 1650 return Status;1839 return EFI_SUCCESS; 1651 1840 } 1652 1841 … … 1735 1924 UINT8 ParentSlotId; 1736 1925 DEVICE_CONTEXT *ParentDeviceContext; 1926 EFI_PHYSICAL_ADDRESS PhyAddr; 1737 1927 1738 1928 ZeroMem (&CmdTrb, sizeof (CMD_TRB_ENABLE_SLOT)); … … 1746 1936 (TRB_TEMPLATE **) (UINTN) &EvtTrb 1747 1937 ); 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 } 1749 1942 ASSERT (EvtTrb->SlotId <= Xhc->MaxSlotsEn); 1750 1943 DEBUG ((EFI_D_INFO, "Enable Slot Successfully, The Slot ID = 0x%x\n", EvtTrb->SlotId)); … … 1762 1955 // 1) Allocate an Input Context data structure (6.2.5) and initialize all fields to '0'. 1763 1956 // 1764 InputContext = AllocatePages (EFI_SIZE_TO_PAGES (sizeof (INPUT_CONTEXT)));1957 InputContext = UsbHcAllocateMem (Xhc->MemPool, sizeof (INPUT_CONTEXT)); 1765 1958 ASSERT (InputContext != NULL); 1766 1959 ASSERT (((UINTN) InputContext & 0x3F) == 0); … … 1851 2044 // Init the DCS(dequeue cycle state) as the transfer ring's CCS 1852 2045 // 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); 1855 2053 1856 2054 // 1857 2055 // 6) Allocate the Output Device Context data structure (6.2.1) and initialize it to '0'. 1858 2056 // 1859 OutputContext = AllocatePages (EFI_SIZE_TO_PAGES (sizeof (DEVICE_CONTEXT)));2057 OutputContext = UsbHcAllocateMem (Xhc->MemPool, sizeof (DEVICE_CONTEXT)); 1860 2058 ASSERT (OutputContext != NULL); 1861 2059 ASSERT (((UINTN) OutputContext & 0x3F) == 0); … … 1867 2065 // a pointer to the Output Device Context data structure (6.2.1). 1868 2066 // 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; 1870 2072 1871 2073 // … … 1874 2076 // 1875 2077 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); 1878 2081 CmdTrbAddr.CycleBit = 1; 1879 2082 CmdTrbAddr.Type = TRB_TYPE_ADDRESS_DEV; … … 1885 2088 (TRB_TEMPLATE **) (UINTN) &EvtTrb 1886 2089 ); 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 } 1893 2095 1894 2096 return Status; … … 1928 2130 UINT8 ParentSlotId; 1929 2131 DEVICE_CONTEXT_64 *ParentDeviceContext; 2132 EFI_PHYSICAL_ADDRESS PhyAddr; 1930 2133 1931 2134 ZeroMem (&CmdTrb, sizeof (CMD_TRB_ENABLE_SLOT)); … … 1939 2142 (TRB_TEMPLATE **) (UINTN) &EvtTrb 1940 2143 ); 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 } 1942 2148 ASSERT (EvtTrb->SlotId <= Xhc->MaxSlotsEn); 1943 2149 DEBUG ((EFI_D_INFO, "Enable Slot Successfully, The Slot ID = 0x%x\n", EvtTrb->SlotId)); … … 1955 2161 // 1) Allocate an Input Context data structure (6.2.5) and initialize all fields to '0'. 1956 2162 // 1957 InputContext = AllocatePages (EFI_SIZE_TO_PAGES (sizeof (INPUT_CONTEXT_64)));2163 InputContext = UsbHcAllocateMem (Xhc->MemPool, sizeof (INPUT_CONTEXT_64)); 1958 2164 ASSERT (InputContext != NULL); 1959 2165 ASSERT (((UINTN) InputContext & 0x3F) == 0); … … 2044 2250 // Init the DCS(dequeue cycle state) as the transfer ring's CCS 2045 2251 // 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); 2048 2259 2049 2260 // 2050 2261 // 6) Allocate the Output Device Context data structure (6.2.1) and initialize it to '0'. 2051 2262 // 2052 OutputContext = AllocatePages (EFI_SIZE_TO_PAGES (sizeof (DEVICE_CONTEXT_64)));2263 OutputContext = UsbHcAllocateMem (Xhc->MemPool, sizeof (DEVICE_CONTEXT_64)); 2053 2264 ASSERT (OutputContext != NULL); 2054 2265 ASSERT (((UINTN) OutputContext & 0x3F) == 0); … … 2060 2271 // a pointer to the Output Device Context data structure (6.2.1). 2061 2272 // 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; 2063 2278 2064 2279 // … … 2067 2282 // 2068 2283 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); 2071 2287 CmdTrbAddr.CycleBit = 1; 2072 2288 CmdTrbAddr.Type = TRB_TYPE_ADDRESS_DEV; … … 2078 2294 (TRB_TEMPLATE **) (UINTN) &EvtTrb 2079 2295 ); 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 } 2087 2301 return Status; 2088 2302 } … … 2145 2359 (TRB_TEMPLATE **) (UINTN) &EvtTrb 2146 2360 ); 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 } 2148 2365 // 2149 2366 // Free the slot's device context entry … … 2158 2375 RingSeg = ((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Index])->RingSeg0; 2159 2376 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); 2161 2378 } 2162 2379 FreePool (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Index]); 2380 Xhc->UsbDevContext[SlotId].EndpointTransferRing[Index] = NULL; 2163 2381 } 2164 2382 } … … 2170 2388 } 2171 2389 2390 if (Xhc->UsbDevContext[SlotId].ActiveAlternateSetting != NULL) { 2391 FreePool (Xhc->UsbDevContext[SlotId].ActiveAlternateSetting); 2392 } 2393 2172 2394 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)); 2174 2396 } 2175 2397 2176 2398 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)); 2178 2400 } 2179 2401 // … … 2244 2466 (TRB_TEMPLATE **) (UINTN) &EvtTrb 2245 2467 ); 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 } 2247 2472 // 2248 2473 // Free the slot's device context entry … … 2257 2482 RingSeg = ((TRANSFER_RING *)(UINTN)Xhc->UsbDevContext[SlotId].EndpointTransferRing[Index])->RingSeg0; 2258 2483 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); 2260 2485 } 2261 2486 FreePool (Xhc->UsbDevContext[SlotId].EndpointTransferRing[Index]); 2487 Xhc->UsbDevContext[SlotId].EndpointTransferRing[Index] = NULL; 2262 2488 } 2263 2489 } … … 2269 2495 } 2270 2496 2497 if (Xhc->UsbDevContext[SlotId].ActiveAlternateSetting != NULL) { 2498 FreePool (Xhc->UsbDevContext[SlotId].ActiveAlternateSetting); 2499 } 2500 2271 2501 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)); 2273 2503 } 2274 2504 2275 2505 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)); 2277 2507 } 2278 2508 // … … 2287 2517 } 2288 2518 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 **/ 2531 UINT8 2532 EFIAPI 2533 XhcInitializeEndpointContext ( 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 **/ 2694 UINT8 2695 EFIAPI 2696 XhcInitializeEndpointContext64 ( 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 } 2289 2844 2290 2845 /** … … 2309 2864 { 2310 2865 EFI_STATUS Status; 2311 2312 2866 USB_INTERFACE_DESCRIPTOR *IfDesc; 2313 USB_ENDPOINT_DESCRIPTOR *EpDesc;2314 2867 UINT8 Index; 2315 UINTN NumEp;2316 UINTN EpIndex;2317 UINT8 EpAddr;2318 UINT8 Direction;2319 2868 UINT8 Dci; 2320 2869 UINT8 MaxDci; 2321 UINT32 PhyAddr; 2322 UINT8 Interval; 2323 2324 TRANSFER_RING *EndpointTransferRing; 2870 EFI_PHYSICAL_ADDRESS PhyAddr; 2871 2325 2872 CMD_TRB_CONFIG_ENDPOINT CmdTrbCfgEP; 2326 2873 INPUT_CONTEXT *InputContext; … … 2341 2888 IfDesc = (USB_INTERFACE_DESCRIPTOR *)(ConfigDesc + 1); 2342 2889 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)) { 2344 2891 IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length); 2345 2892 } 2346 2893 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 2456 2899 IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length); 2457 2900 } … … 2463 2906 // 2464 2907 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); 2467 2911 CmdTrbCfgEP.CycleBit = 1; 2468 2912 CmdTrbCfgEP.Type = TRB_TYPE_CON_ENDPOINT; … … 2475 2919 (TRB_TEMPLATE **) (UINTN) &EvtTrb 2476 2920 ); 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 } 2478 2926 2479 2927 return Status; … … 2501 2949 { 2502 2950 EFI_STATUS Status; 2503 2504 2951 USB_INTERFACE_DESCRIPTOR *IfDesc; 2505 USB_ENDPOINT_DESCRIPTOR *EpDesc;2506 2952 UINT8 Index; 2507 UINTN NumEp;2508 UINTN EpIndex;2509 UINT8 EpAddr;2510 UINT8 Direction;2511 2953 UINT8 Dci; 2512 2954 UINT8 MaxDci; 2513 UINT32 PhyAddr; 2514 UINT8 Interval; 2515 2516 TRANSFER_RING *EndpointTransferRing; 2955 EFI_PHYSICAL_ADDRESS PhyAddr; 2956 2517 2957 CMD_TRB_CONFIG_ENDPOINT CmdTrbCfgEP; 2518 2958 INPUT_CONTEXT_64 *InputContext; … … 2533 2973 IfDesc = (USB_INTERFACE_DESCRIPTOR *)(ConfigDesc + 1); 2534 2974 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)) { 2536 2976 IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length); 2537 2977 } 2538 2978 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 2648 2984 IfDesc = (USB_INTERFACE_DESCRIPTOR *)((UINTN)IfDesc + IfDesc->Length); 2649 2985 } … … 2655 2991 // 2656 2992 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); 2659 2996 CmdTrbCfgEP.CycleBit = 1; 2660 2997 CmdTrbCfgEP.Type = TRB_TYPE_CON_ENDPOINT; … … 2667 3004 (TRB_TEMPLATE **) (UINTN) &EvtTrb 2668 3005 ); 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 } 2670 3011 2671 3012 return Status; 2672 3013 } 2673 3014 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 **/ 3026 EFI_STATUS 3027 EFIAPI 3028 XhcStopEndpoint ( 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 **/ 3073 EFI_STATUS 3074 EFIAPI 3075 XhcSetInterface ( 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 **/ 3270 EFI_STATUS 3271 EFIAPI 3272 XhcSetInterface64 ( 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 } 2674 3454 2675 3455 /** … … 2695 3475 EVT_TRB_COMMAND_COMPLETION *EvtTrb; 2696 3476 INPUT_CONTEXT *InputContext; 3477 EFI_PHYSICAL_ADDRESS PhyAddr; 2697 3478 2698 3479 ASSERT (Xhc->UsbDevContext[SlotId].SlotId != 0); … … 2708 3489 2709 3490 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); 2712 3494 CmdTrbEvalu.CycleBit = 1; 2713 3495 CmdTrbEvalu.Type = TRB_TYPE_EVALU_CONTXT; … … 2720 3502 (TRB_TEMPLATE **) (UINTN) &EvtTrb 2721 3503 ); 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 } 2724 3507 return Status; 2725 3508 } … … 2747 3530 EVT_TRB_COMMAND_COMPLETION *EvtTrb; 2748 3531 INPUT_CONTEXT_64 *InputContext; 3532 EFI_PHYSICAL_ADDRESS PhyAddr; 2749 3533 2750 3534 ASSERT (Xhc->UsbDevContext[SlotId].SlotId != 0); … … 2760 3544 2761 3545 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); 2764 3549 CmdTrbEvalu.CycleBit = 1; 2765 3550 CmdTrbEvalu.Type = TRB_TYPE_EVALU_CONTXT; … … 2772 3557 (TRB_TEMPLATE **) (UINTN) &EvtTrb 2773 3558 ); 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 } 2776 3562 return Status; 2777 3563 } … … 2800 3586 { 2801 3587 EFI_STATUS Status; 2802 2803 3588 EVT_TRB_COMMAND_COMPLETION *EvtTrb; 2804 3589 INPUT_CONTEXT *InputContext; 2805 3590 DEVICE_CONTEXT *OutputContext; 2806 3591 CMD_TRB_CONFIG_ENDPOINT CmdTrbCfgEP; 3592 EFI_PHYSICAL_ADDRESS PhyAddr; 2807 3593 2808 3594 ASSERT (Xhc->UsbDevContext[SlotId].SlotId != 0); … … 2827 3613 2828 3614 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); 2831 3618 CmdTrbCfgEP.CycleBit = 1; 2832 3619 CmdTrbCfgEP.Type = TRB_TYPE_CON_ENDPOINT; … … 2839 3626 (TRB_TEMPLATE **) (UINTN) &EvtTrb 2840 3627 ); 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 } 2843 3631 return Status; 2844 3632 } … … 2866 3654 { 2867 3655 EFI_STATUS Status; 2868 2869 3656 EVT_TRB_COMMAND_COMPLETION *EvtTrb; 2870 3657 INPUT_CONTEXT_64 *InputContext; 2871 3658 DEVICE_CONTEXT_64 *OutputContext; 2872 3659 CMD_TRB_CONFIG_ENDPOINT CmdTrbCfgEP; 3660 EFI_PHYSICAL_ADDRESS PhyAddr; 2873 3661 2874 3662 ASSERT (Xhc->UsbDevContext[SlotId].SlotId != 0); … … 2893 3681 2894 3682 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); 2897 3686 CmdTrbCfgEP.CycleBit = 1; 2898 3687 CmdTrbCfgEP.Type = TRB_TYPE_CON_ENDPOINT; … … 2905 3694 (TRB_TEMPLATE **) (UINTN) &EvtTrb 2906 3695 ); 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 } 2909 3699 return Status; 2910 3700 }
Note:
See TracChangeset
for help on using the changeset viewer.