3 XHCI transfer scheduling routines.
5 Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
19 Allocates a buffer of a certain pool type at a specified alignment.
21 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
22 specified by Alignment. The allocated buffer is returned. If AllocationSize is 0, then a valid
23 buffer of 0 size is returned. If there is not enough memory at the specified alignment remaining
24 to satisfy the request, then NULL is returned.
25 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
27 @param PoolType The type of pool to allocate.
28 @param AllocationSize The number of bytes to allocate.
29 @param Alignment The requested alignment of the allocation. Must be a power of two.
30 If Alignment is zero, then byte alignment is used.
32 @return A pointer to the allocated buffer or NULL if allocation fails.
36 InternalAllocateAlignedPool (
37 IN EFI_MEMORY_TYPE PoolType
,
38 IN UINTN AllocationSize
,
45 UINTN OverAllocationSize
;
46 UINTN RealAllocationSize
;
50 // Alignment must be a power of two or zero.
52 ASSERT ((Alignment
& (Alignment
- 1)) == 0);
55 AlignmentMask
= Alignment
;
57 AlignmentMask
= Alignment
- 1;
60 // Calculate the extra memory size, over-allocate memory pool and get the aligned memory address.
62 OverAllocationSize
= sizeof (RawAddress
) + AlignmentMask
;
63 RealAllocationSize
= AllocationSize
+ OverAllocationSize
;
65 // Make sure that AllocationSize plus OverAllocationSize does not overflow.
67 ASSERT (RealAllocationSize
> AllocationSize
);
70 gBS
->AllocatePool (PoolType
, RealAllocationSize
, &RawAddress
);
71 if (RawAddress
== NULL
) {
74 AlignedAddress
= ((UINTN
) RawAddress
+ OverAllocationSize
) & ~AlignmentMask
;
76 // Save the original memory address just before the aligned address.
78 FreePointer
= (VOID
**)(AlignedAddress
- sizeof (RawAddress
));
79 *FreePointer
= RawAddress
;
81 return (VOID
*) AlignedAddress
;
85 Allocates and zeros a buffer of a certain pool type at a specified alignment.
87 Allocates the number bytes specified by AllocationSize of a certain pool type with an alignment
88 specified by Alignment, clears the buffer with zeros, and returns a pointer to the allocated
89 buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there is not
90 enough memory at the specified alignment remaining to satisfy the request, then NULL is returned.
91 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
93 @param PoolType The type of pool to allocate.
94 @param AllocationSize The number of bytes to allocate.
95 @param Alignment The requested alignment of the allocation. Must be a power of two.
96 If Alignment is zero, then byte alignment is used.
98 @return A pointer to the allocated buffer or NULL if allocation fails.
102 InternalAllocateAlignedZeroPool (
103 IN EFI_MEMORY_TYPE PoolType
,
104 IN UINTN AllocationSize
,
109 Memory
= InternalAllocateAlignedPool (PoolType
, AllocationSize
, Alignment
);
110 if (Memory
!= NULL
) {
111 ZeroMem (Memory
, AllocationSize
);
117 Allocates and zeros a buffer of type EfiBootServicesData at a specified alignment.
119 Allocates the number bytes specified by AllocationSize of type EfiBootServicesData with an
120 alignment specified by Alignment, clears the buffer with zeros, and returns a pointer to the
121 allocated buffer. If AllocationSize is 0, then a valid buffer of 0 size is returned. If there
122 is not enough memory at the specified alignment remaining to satisfy the request, then NULL is
124 If Alignment is not a power of two and Alignment is not zero, then ASSERT().
126 @param AllocationSize The number of bytes to allocate.
127 @param Alignment The requested alignment of the allocation. Must be a power of two.
128 If Alignment is zero, then byte alignment is used.
130 @return A pointer to the allocated buffer or NULL if allocation fails.
135 AllocateAlignedZeroPool (
136 IN UINTN AllocationSize
,
140 return InternalAllocateAlignedZeroPool (EfiBootServicesData
, AllocationSize
, Alignment
);
144 Frees a buffer that was previously allocated with one of the aligned pool allocation functions
145 in the Memory Allocation Library.
147 Frees the buffer specified by Buffer. Buffer must have been allocated on a previous call to the
148 aligned pool allocation services of the Memory Allocation Library.
149 If Buffer was not allocated with an aligned pool allocation function in the Memory Allocation
150 Library, then ASSERT().
152 @param Buffer Pointer to the buffer to free.
166 // Get the pre-saved original address in the over-allocate pool.
168 FreePointer
= (VOID
**)((UINTN
) Buffer
- sizeof (RawAddress
));
169 RawAddress
= *FreePointer
;
171 Status
= gBS
->FreePool (RawAddress
);
172 ASSERT_EFI_ERROR (Status
);
176 Create a command transfer TRB to support XHCI command interfaces.
178 @param Xhc The XHCI device.
179 @param CmdTrb The cmd TRB to be executed.
181 @return Created URB or NULL.
186 IN USB_XHCI_DEV
*Xhc
,
192 Urb
= AllocateZeroPool (sizeof (URB
));
197 Urb
->Signature
= XHC_URB_SIG
;
199 Urb
->Ring
= &Xhc
->CmdRing
;
200 XhcSyncTrsRing (Xhc
, Urb
->Ring
);
202 Urb
->TrbStart
= Urb
->Ring
->RingEnqueue
;
203 CopyMem (Urb
->TrbStart
, CmdTrb
, sizeof (TRB
));
204 Urb
->TrbStart
->CycleBit
= Urb
->Ring
->RingPCS
& BIT0
;
205 Urb
->TrbEnd
= Urb
->TrbStart
;
207 Urb
->EvtRing
= &Xhc
->CmdEventRing
;
208 XhcSyncEventRing (Xhc
, Urb
->EvtRing
);
209 Urb
->EvtTrbStart
= Urb
->EvtRing
->EventRingEnqueue
;
215 Execute a XHCI cmd TRB pointed by CmdTrb.
217 @param Xhc The XHCI device.
218 @param CmdTrb The cmd TRB to be executed.
219 @param TimeOut Indicates the maximum time, in millisecond, which the
220 transfer is allowed to complete.
221 @param EvtTrb The event TRB corresponding to the cmd TRB.
223 @retval EFI_SUCCESS The transfer was completed successfully.
224 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
225 @retval EFI_TIMEOUT The transfer failed due to timeout.
226 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
232 IN USB_XHCI_DEV
*Xhc
,
242 // Validate the parameters
244 if ((Xhc
== NULL
) || (CmdTrb
== NULL
)) {
245 return EFI_INVALID_PARAMETER
;
248 Status
= EFI_DEVICE_ERROR
;
250 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
251 DEBUG ((EFI_D_ERROR
, "XhcCmdTransfer: HC is halted\n"));
256 // Create a new URB, then poll the execution status.
258 Urb
= XhcCreateCmdTrb (Xhc
, CmdTrb
);
261 DEBUG ((EFI_D_ERROR
, "XhcCmdTransfer: failed to create URB\n"));
262 Status
= EFI_OUT_OF_RESOURCES
;
266 ASSERT (Urb
->EvtRing
== &Xhc
->CmdEventRing
);
268 Status
= XhcExecTransfer (Xhc
, TRUE
, Urb
, TimeOut
);
269 *EvtTrb
= Urb
->EvtTrbStart
;
271 if (Urb
->Result
== EFI_USB_NOERROR
) {
272 Status
= EFI_SUCCESS
;
282 Create a new URB for a new transaction.
284 @param Xhc The XHCI device
285 @param DevAddr The device address
286 @param EpAddr Endpoint addrress
287 @param DevSpeed The device speed
288 @param MaxPacket The max packet length of the endpoint
289 @param Type The transaction type
290 @param Request The standard USB request for control transfer
291 @param Data The user data to transfer
292 @param DataLen The length of data buffer
293 @param Callback The function to call when data is transferred
294 @param Context The context to the callback
296 @return Created URB or NULL
301 IN USB_XHCI_DEV
*Xhc
,
307 IN EFI_USB_DEVICE_REQUEST
*Request
,
310 IN EFI_ASYNC_USB_TRANSFER_CALLBACK Callback
,
318 Urb
= AllocateZeroPool (sizeof (URB
));
323 Urb
->Signature
= XHC_URB_SIG
;
324 InitializeListHead (&Urb
->UrbList
);
327 Ep
->DevAddr
= DevAddr
;
328 Ep
->EpAddr
= (UINT8
)(EpAddr
& 0x0F);
329 Ep
->Direction
= ((EpAddr
& 0x80) != 0) ? EfiUsbDataIn
: EfiUsbDataOut
;
330 Ep
->DevSpeed
= DevSpeed
;
331 Ep
->MaxPacket
= MaxPacket
;
334 Urb
->Request
= Request
;
336 Urb
->DataLen
= DataLen
;
337 Urb
->Callback
= Callback
;
338 Urb
->Context
= Context
;
340 Status
= XhcCreateTransferTrb (Xhc
, Urb
);
346 Create a transfer TRB.
348 @param Xhc The XHCI device
349 @param Urb The urb used to construct the transfer TRB.
351 @return Created TRB or NULL
355 XhcCreateTransferTrb (
356 IN USB_XHCI_DEV
*Xhc
,
360 DEVICE_CONTEXT
*OutputDevContxt
;
361 TRANSFER_RING
*EPRing
;
370 SlotId
= XhcDevAddrToSlotId(Urb
->Ep
.DevAddr
);
371 Dci
= XhcEndpointToDci (Urb
->Ep
.EpAddr
, (UINT8
)(Urb
->Ep
.Direction
));
372 EPRing
= (TRANSFER_RING
*)(UINTN
) UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1];
374 OutputDevContxt
= (DEVICE_CONTEXT
*)(UINTN
) Xhc
->DCBAA
[SlotId
];
375 EPType
= (UINT8
) OutputDevContxt
->EP
[Dci
-1].EPType
;
380 XhcSyncTrsRing (Xhc
, EPRing
);
381 Urb
->TrbStart
= EPRing
->RingEnqueue
;
383 case ED_CONTROL_BIDIR
:
384 Urb
->EvtRing
= &Xhc
->CtrlTrEventRing
;
385 XhcSyncEventRing (Xhc
, Urb
->EvtRing
);
386 Urb
->EvtTrbStart
= Urb
->EvtRing
->EventRingEnqueue
;
388 // For control transfer, create SETUP_STAGE_TRB first.
390 TrbStart
= EPRing
->RingEnqueue
;
391 ((TRANSFER_TRB_CONTROL_SETUP
*) TrbStart
)->bmRequestType
= Urb
->Request
->RequestType
;
392 ((TRANSFER_TRB_CONTROL_SETUP
*) TrbStart
)->bRequest
= Urb
->Request
->Request
;
393 ((TRANSFER_TRB_CONTROL_SETUP
*) TrbStart
)->wValue
= Urb
->Request
->Value
;
394 ((TRANSFER_TRB_CONTROL_SETUP
*) TrbStart
)->wIndex
= Urb
->Request
->Index
;
395 ((TRANSFER_TRB_CONTROL_SETUP
*) TrbStart
)->wLength
= Urb
->Request
->Length
;
396 ((TRANSFER_TRB_CONTROL_SETUP
*) TrbStart
)->Lenth
= 8;
397 ((TRANSFER_TRB_CONTROL_SETUP
*) TrbStart
)->IntTarget
= Urb
->EvtRing
->EventInterrupter
;
398 ((TRANSFER_TRB_CONTROL_SETUP
*) TrbStart
)->IOC
= 1;
399 ((TRANSFER_TRB_CONTROL_SETUP
*) TrbStart
)->IDT
= 1;
400 ((TRANSFER_TRB_CONTROL_SETUP
*) TrbStart
)->Type
= TRB_TYPE_SETUP_STAGE
;
401 if (Urb
->Ep
.Direction
== EfiUsbDataIn
) {
402 ((TRANSFER_TRB_CONTROL_SETUP
*) TrbStart
)->TRT
= 3;
403 } else if (Urb
->Ep
.Direction
== EfiUsbDataOut
) {
404 ((TRANSFER_TRB_CONTROL_SETUP
*) TrbStart
)->TRT
= 2;
406 ((TRANSFER_TRB_CONTROL_SETUP
*) TrbStart
)->TRT
= 0;
409 // Update the cycle bit
411 ((TRANSFER_TRB_CONTROL_SETUP
*) TrbStart
)->CycleBit
= EPRing
->RingPCS
& BIT0
;
415 // For control transfer, create DATA_STAGE_TRB.
417 if (Urb
->DataLen
> 0) {
418 XhcSyncTrsRing (Xhc
, EPRing
);
419 TrbStart
= EPRing
->RingEnqueue
;
420 ((TRANSFER_TRB_CONTROL_DATA
*) TrbStart
)->TRBPtrLo
= XHC_LOW_32BIT(Urb
->Data
);
421 ((TRANSFER_TRB_CONTROL_DATA
*) TrbStart
)->TRBPtrHi
= XHC_HIGH_32BIT(Urb
->Data
);
422 ((TRANSFER_TRB_CONTROL_DATA
*) TrbStart
)->Lenth
= (UINT32
) Urb
->DataLen
;
423 ((TRANSFER_TRB_CONTROL_DATA
*) TrbStart
)->TDSize
= 0;
424 ((TRANSFER_TRB_CONTROL_DATA
*) TrbStart
)->IntTarget
= Urb
->EvtRing
->EventInterrupter
;
425 ((TRANSFER_TRB_CONTROL_DATA
*) TrbStart
)->ISP
= 1;
426 ((TRANSFER_TRB_CONTROL_DATA
*) TrbStart
)->IOC
= 1;
427 ((TRANSFER_TRB_CONTROL_DATA
*) TrbStart
)->IDT
= 0;
428 ((TRANSFER_TRB_CONTROL_DATA
*) TrbStart
)->CH
= 0;
429 ((TRANSFER_TRB_CONTROL_DATA
*) TrbStart
)->Type
= TRB_TYPE_DATA_STAGE
;
430 if (Urb
->Ep
.Direction
== EfiUsbDataIn
) {
431 ((TRANSFER_TRB_CONTROL_DATA
*) TrbStart
)->DIR = 1;
432 } else if (Urb
->Ep
.Direction
== EfiUsbDataOut
) {
433 ((TRANSFER_TRB_CONTROL_DATA
*) TrbStart
)->DIR = 0;
435 ((TRANSFER_TRB_CONTROL_DATA
*) TrbStart
)->DIR = 0;
438 // Update the cycle bit
440 ((TRANSFER_TRB_CONTROL_DATA
*) TrbStart
)->CycleBit
= EPRing
->RingPCS
& BIT0
;
444 // For control transfer, create STATUS_STAGE_TRB.
445 // Get the pointer to next TRB for status stage use
447 XhcSyncTrsRing (Xhc
, EPRing
);
448 TrbStart
= EPRing
->RingEnqueue
;
449 ((TRANSFER_TRB_CONTROL_STATUS
*) TrbStart
)->IntTarget
= Urb
->EvtRing
->EventInterrupter
;
450 ((TRANSFER_TRB_CONTROL_STATUS
*) TrbStart
)->IOC
= 1;
451 ((TRANSFER_TRB_CONTROL_STATUS
*) TrbStart
)->CH
= 0;
452 ((TRANSFER_TRB_CONTROL_STATUS
*) TrbStart
)->Type
= TRB_TYPE_STATUS_STAGE
;
453 if (Urb
->Ep
.Direction
== EfiUsbDataIn
) {
454 ((TRANSFER_TRB_CONTROL_STATUS
*) TrbStart
)->DIR = 0;
455 } else if (Urb
->Ep
.Direction
== EfiUsbDataOut
) {
456 ((TRANSFER_TRB_CONTROL_STATUS
*) TrbStart
)->DIR = 1;
458 ((TRANSFER_TRB_CONTROL_STATUS
*) TrbStart
)->DIR = 0;
461 // Update the cycle bit
463 ((TRANSFER_TRB_CONTROL_STATUS
*) TrbStart
)->CycleBit
= EPRing
->RingPCS
& BIT0
;
465 // Update the enqueue pointer
467 XhcSyncTrsRing (Xhc
, EPRing
);
469 Urb
->TrbEnd
= TrbStart
;
475 Urb
->EvtRing
= &Xhc
->BulkTrEventRing
;
476 XhcSyncEventRing (Xhc
, Urb
->EvtRing
);
477 Urb
->EvtTrbStart
= Urb
->EvtRing
->EventRingEnqueue
;
482 TrbStart
= EPRing
->RingEnqueue
;
483 while (TotalLen
< Urb
->DataLen
) {
484 if ((TotalLen
+ 0x10000) >= Urb
->DataLen
) {
485 Len
= Urb
->DataLen
- TotalLen
;
489 TrbStart
= EPRing
->RingEnqueue
;
490 ((TRANSFER_TRB_NORMAL
*) TrbStart
)->TRBPtrLo
= XHC_LOW_32BIT((UINT8
*) Urb
->Data
+ TotalLen
);
491 ((TRANSFER_TRB_NORMAL
*) TrbStart
)->TRBPtrHi
= XHC_HIGH_32BIT((UINT8
*) Urb
->Data
+ TotalLen
);
492 ((TRANSFER_TRB_NORMAL
*) TrbStart
)->Lenth
= (UINT32
) Len
;
493 ((TRANSFER_TRB_NORMAL
*) TrbStart
)->TDSize
= 0;
494 ((TRANSFER_TRB_NORMAL
*) TrbStart
)->IntTarget
= Urb
->EvtRing
->EventInterrupter
;
495 ((TRANSFER_TRB_NORMAL
*) TrbStart
)->ISP
= 1;
496 ((TRANSFER_TRB_NORMAL
*) TrbStart
)->IOC
= 1;
497 ((TRANSFER_TRB_NORMAL
*) TrbStart
)->Type
= TRB_TYPE_NORMAL
;
499 // Update the cycle bit
501 ((TRANSFER_TRB_NORMAL
*) TrbStart
)->CycleBit
= EPRing
->RingPCS
& BIT0
;
503 XhcSyncTrsRing (Xhc
, EPRing
);
508 Urb
->TrbNum
= TrbNum
;
509 Urb
->TrbEnd
= TrbStart
;
512 case ED_INTERRUPT_OUT
:
513 case ED_INTERRUPT_IN
:
514 if (Urb
->Ep
.Type
== XHC_INT_TRANSFER_ASYNC
) {
515 Urb
->EvtRing
= &Xhc
->AsynIntTrEventRing
;
516 } else if(Urb
->Ep
.Type
== XHC_INT_TRANSFER_SYNC
){
517 Urb
->EvtRing
= &Xhc
->IntTrEventRing
;
519 DEBUG ((EFI_D_ERROR
, "EP Interrupt type error!\n"));
522 XhcSyncEventRing (Xhc
, Urb
->EvtRing
);
523 Urb
->EvtTrbStart
= Urb
->EvtRing
->EventRingEnqueue
;
528 TrbStart
= EPRing
->RingEnqueue
;
529 while (TotalLen
< Urb
->DataLen
) {
530 if ((TotalLen
+ 0x10000) >= Urb
->DataLen
) {
531 Len
= Urb
->DataLen
- TotalLen
;
535 TrbStart
= EPRing
->RingEnqueue
;
536 ((TRANSFER_TRB_NORMAL
*) TrbStart
)->TRBPtrLo
= XHC_LOW_32BIT((UINT8
*) Urb
->Data
+ TotalLen
);
537 ((TRANSFER_TRB_NORMAL
*) TrbStart
)->TRBPtrHi
= XHC_HIGH_32BIT((UINT8
*) Urb
->Data
+ TotalLen
);
538 ((TRANSFER_TRB_NORMAL
*) TrbStart
)->Lenth
= (UINT32
) Len
;
539 ((TRANSFER_TRB_NORMAL
*) TrbStart
)->TDSize
= 0;
540 ((TRANSFER_TRB_NORMAL
*) TrbStart
)->IntTarget
= Urb
->EvtRing
->EventInterrupter
;
541 ((TRANSFER_TRB_NORMAL
*) TrbStart
)->ISP
= 1;
542 ((TRANSFER_TRB_NORMAL
*) TrbStart
)->IOC
= 1;
543 ((TRANSFER_TRB_NORMAL
*) TrbStart
)->Type
= TRB_TYPE_NORMAL
;
545 // Update the cycle bit
547 ((TRANSFER_TRB_NORMAL
*) TrbStart
)->CycleBit
= EPRing
->RingPCS
& BIT0
;
549 XhcSyncTrsRing (Xhc
, EPRing
);
554 Urb
->TrbNum
= TrbNum
;
555 Urb
->TrbEnd
= TrbStart
;
559 DEBUG ((EFI_D_INFO
, "Not supported EPType 0x%x!\n",EPType
));
569 Initialize the XHCI host controller for schedule.
571 @param Xhc The XHCI device to be initialized.
582 UINT32 MaxScratchpadBufs
;
584 UINT64
*ScratchEntryBuf
;
588 // Program the Max Device Slots Enabled (MaxSlotsEn) field in the CONFIG register (5.4.7)
589 // to enable the device slots that system software is going to use.
591 Xhc
->MaxSlotsEn
= Xhc
->HcSParams1
.Data
.MaxSlots
;
592 ASSERT (Xhc
->MaxSlotsEn
>= 1 && Xhc
->MaxSlotsEn
<= 255);
593 XhcWriteOpReg (Xhc
, XHC_CONFIG_OFFSET
, Xhc
->MaxSlotsEn
);
596 // The Device Context Base Address Array entry associated with each allocated Device Slot
597 // shall contain a 64-bit pointer to the base of the associated Device Context.
598 // The Device Context Base Address Array shall contain MaxSlotsEn + 1 entries.
599 // Software shall set Device Context Base Address Array entries for unallocated Device Slots to '0'.
601 Entries
= (Xhc
->MaxSlotsEn
+ 1) * sizeof(UINT64
);
602 Dcbaa
= AllocateAlignedZeroPool(Entries
, 64);
603 ASSERT (Dcbaa
!= NULL
);
606 // A Scratchpad Buffer is a PAGESIZE block of system memory located on a PAGESIZE boundary.
607 // System software shall allocate the Scratchpad Buffer(s) before placing the xHC in to Run
608 // mode (Run/Stop(R/S) ='1').
610 MaxScratchpadBufs
= ((Xhc
->HcSParams2
.Data
.ScratchBufHi
) << 5) | (Xhc
->HcSParams2
.Data
.ScratchBufLo
);
611 Xhc
->MaxScratchpadBufs
= MaxScratchpadBufs
;
612 ASSERT (MaxScratchpadBufs
<= 1023);
613 if (MaxScratchpadBufs
!= 0) {
614 ScratchBuf
= AllocateAlignedZeroPool(MaxScratchpadBufs
* sizeof (UINT64
), Xhc
->PageSize
);
615 ASSERT (ScratchBuf
!= NULL
);
616 Xhc
->ScratchBuf
= ScratchBuf
;
618 for (Index
= 0; Index
< MaxScratchpadBufs
; Index
++) {
619 ScratchEntryBuf
= AllocateAlignedZeroPool(Xhc
->PageSize
, Xhc
->PageSize
);
620 *ScratchBuf
++ = (UINT64
)(UINTN
)ScratchEntryBuf
;
624 // The Scratchpad Buffer Array contains pointers to the Scratchpad Buffers. Entry 0 of the
625 // Device Context Base Address Array points to the Scratchpad Buffer Array.
627 *(UINT64
*)Dcbaa
= (UINT64
)(UINTN
)Xhc
->ScratchBuf
;
631 // Program the Device Context Base Address Array Pointer (DCBAAP) register (5.4.6) with
632 // a 64-bit address pointing to where the Device Context Base Address Array is located.
634 Xhc
->DCBAA
= (UINT64
*)(UINTN
)Dcbaa
;
635 XhcWriteOpReg64 (Xhc
, XHC_DCBAAP_OFFSET
, (UINT64
)(UINTN
)Xhc
->DCBAA
);
636 DEBUG ((EFI_D_INFO
, "XhcInitSched:DCBAA=0x%x\n", (UINT64
)(UINTN
)Xhc
->DCBAA
));
639 // Define the Command Ring Dequeue Pointer by programming the Command Ring Control Register
640 // (5.4.5) with a 64-bit address pointing to the starting address of the first TRB of the Command Ring.
641 // Note: The Command Ring is 64 byte aligned, so the low order 6 bits of the Command Ring Pointer shall
644 CreateTransferRing (Xhc
, CMD_RING_TRB_NUMBER
, &Xhc
->CmdRing
);
646 // The xHC uses the Enqueue Pointer to determine when a Transfer Ring is empty. As it fetches TRBs from a
647 // Transfer Ring it checks for a Cycle bit transition. If a transition detected, the ring is empty.
648 // So we set RCS as inverted PCS init value to let Command Ring empty
650 CmdRing
= (UINT64
)(UINTN
)Xhc
->CmdRing
.RingSeg0
;
651 ASSERT ((CmdRing
& 0x3F) == 0);
652 CmdRing
|= XHC_CRCR_RCS
;
653 XhcWriteOpReg64 (Xhc
, XHC_CRCR_OFFSET
, CmdRing
);
655 DEBUG ((EFI_D_INFO
, "XhcInitSched:XHC_CRCR=0x%x\n", Xhc
->CmdRing
.RingSeg0
));
658 // Disable the 'interrupter enable' bit in USB_CMD
659 // and clear IE & IP bit in all Interrupter X Management Registers.
661 XhcClearOpRegBit (Xhc
, XHC_USBCMD_OFFSET
, XHC_USBCMD_INTE
);
662 for (Index
= 0; Index
< (UINT16
)(Xhc
->HcSParams1
.Data
.MaxIntrs
); Index
++) {
663 XhcClearRuntimeRegBit (Xhc
, XHC_IMAN_OFFSET
+ (Index
* 32), XHC_IMAN_IE
);
664 XhcSetRuntimeRegBit (Xhc
, XHC_IMAN_OFFSET
+ (Index
* 32), XHC_IMAN_IP
);
668 // Allocate EventRing for Cmd, Ctrl, Bulk, Interrupt, AsynInterrupt transfer
670 CreateEventRing (Xhc
, CMD_INTER
, &Xhc
->CmdEventRing
);
671 CreateEventRing (Xhc
, CTRL_INTER
, &Xhc
->CtrlTrEventRing
);
672 CreateEventRing (Xhc
, BULK_INTER
, &Xhc
->BulkTrEventRing
);
673 CreateEventRing (Xhc
, INT_INTER
, &Xhc
->IntTrEventRing
);
674 CreateEventRing (Xhc
, INT_INTER_ASYNC
, &Xhc
->AsynIntTrEventRing
);
678 System software shall use a Reset Endpoint Command (section 4.11.4.7) to remove the Halted
679 condition in the xHC. After the successful completion of the Reset Endpoint Command, the Endpoint
680 Context is transitioned from the Halted to the Stopped state and the Transfer Ring of the endpoint is
681 reenabled. The next write to the Doorbell of the Endpoint will transition the Endpoint Context from the
682 Stopped to the Running state.
684 @param Xhc The XHCI device.
685 @param Urb The urb which makes the endpoint halted.
687 @retval EFI_SUCCESS The recovery is successful.
688 @retval Others Failed to recovery halted endpoint.
693 XhcRecoverHaltedEndpoint (
694 IN USB_XHCI_DEV
*Xhc
,
699 EVT_TRB_COMMAND
*EvtTrb
;
700 CMD_TRB_RESET_ED CmdTrbResetED
;
701 CMD_SET_TR_DEQ CmdSetTRDeq
;
705 Status
= EFI_SUCCESS
;
706 SlotId
= XhcDevAddrToSlotId(Urb
->Ep
.DevAddr
);
707 Dci
= XhcEndpointToDci(Urb
->Ep
.EpAddr
, (UINT8
)(Urb
->Ep
.Direction
));
709 DEBUG ((EFI_D_INFO
, "Recovery Halted Slot = %x,Dci = %x\n", SlotId
, Dci
));
712 // 1) Send Reset endpoint command to transit from halt to stop state
714 ZeroMem (&CmdTrbResetED
, sizeof (CmdTrbResetED
));
715 CmdTrbResetED
.CycleBit
= 1;
716 CmdTrbResetED
.Type
= TRB_TYPE_RESET_ENDPOINT
;
717 CmdTrbResetED
.EDID
= Dci
;
718 CmdTrbResetED
.SlotId
= SlotId
;
719 Status
= XhcCmdTransfer (
721 (TRB
*) (UINTN
) &CmdTrbResetED
,
723 (TRB
**) (UINTN
) &EvtTrb
725 ASSERT (!EFI_ERROR(Status
));
728 // 2)Set dequeue pointer
730 ZeroMem (&CmdSetTRDeq
, sizeof (CmdSetTRDeq
));
731 CmdSetTRDeq
.PtrLo
= XHC_LOW_32BIT (Urb
->Ring
->RingEnqueue
) | Urb
->Ring
->RingPCS
;
732 CmdSetTRDeq
.PtrHi
= XHC_HIGH_32BIT (Urb
->Ring
->RingEnqueue
);
733 CmdSetTRDeq
.CycleBit
= 1;
734 CmdSetTRDeq
.Type
= TRB_TYPE_SET_TR_DEQUE
;
735 CmdSetTRDeq
.Endpoint
= Dci
;
736 CmdSetTRDeq
.SlotId
= SlotId
;
737 Status
= XhcCmdTransfer (
739 (TRB
*) (UINTN
) &CmdSetTRDeq
,
741 (TRB
**) (UINTN
) &EvtTrb
743 ASSERT (!EFI_ERROR(Status
));
746 // 3)Ring the doorbell to transit from stop to active
748 XhcRingDoorBell (Xhc
, SlotId
, Dci
);
754 Create XHCI event ring.
756 @param Xhc The XHCI device.
757 @param EventInterrupter The interrupter of event.
758 @param EventRing The created event ring.
763 IN USB_XHCI_DEV
*Xhc
,
764 IN UINT8 EventInterrupter
,
765 OUT EVENT_RING
*EventRing
769 EVENT_RING_SEG_TABLE_ENTRY
*ERSTBase
;
771 ASSERT (EventRing
!= NULL
);
773 Buf
= AllocateAlignedZeroPool(sizeof (TRB
) * EVENT_RING_TRB_NUMBER
, 64);
774 ASSERT (Buf
!= NULL
);
775 ASSERT (((UINTN
) Buf
& 0x3F) == 0);
777 EventRing
->EventRingSeg0
= Buf
;
778 EventRing
->EventInterrupter
= EventInterrupter
;
779 EventRing
->TrbNumber
= EVENT_RING_TRB_NUMBER
;
780 EventRing
->EventRingDequeue
= (TRB
*) EventRing
->EventRingSeg0
;
781 EventRing
->EventRingEnqueue
= (TRB
*) EventRing
->EventRingSeg0
;
783 // Software maintains an Event Ring Consumer Cycle State (CCS) bit, initializing it to '1'
784 // and toggling it every time the Event Ring Dequeue Pointer wraps back to the beginning of the Event Ring.
786 EventRing
->EventRingCCS
= 1;
788 Buf
= AllocateAlignedZeroPool(sizeof (EVENT_RING_SEG_TABLE_ENTRY
) * ERST_NUMBER
, 64);
789 ASSERT (Buf
!= NULL
);
790 ASSERT (((UINTN
) Buf
& 0x3F) == 0);
792 ERSTBase
= (EVENT_RING_SEG_TABLE_ENTRY
*) Buf
;
793 EventRing
->ERSTBase
= ERSTBase
;
794 ERSTBase
->PtrLo
= XHC_LOW_32BIT (EventRing
->EventRingSeg0
);
795 ERSTBase
->PtrHi
= XHC_HIGH_32BIT (EventRing
->EventRingSeg0
);
796 ERSTBase
->RingTrbSize
= EVENT_RING_TRB_NUMBER
;
799 // Program the Interrupter Event Ring Segment Table Size (ERSTSZ) register (5.5.2.3.1)
803 XHC_ERSTSZ_OFFSET
+ (32 * EventRing
->EventInterrupter
),
807 // Program the Interrupter Event Ring Dequeue Pointer (ERDP) register (5.5.2.3.3)
809 XhcWriteRuntimeReg64 (
811 XHC_ERDP_OFFSET
+ (32 * EventRing
->EventInterrupter
),
812 (UINT64
)(UINTN
)EventRing
->EventRingDequeue
815 // Program the Interrupter Event Ring Segment Table Base Address (ERSTBA) register(5.5.2.3.2)
817 XhcWriteRuntimeReg64 (
819 XHC_ERSTBA_OFFSET
+ (32 * EventRing
->EventInterrupter
),
820 (UINT64
)(UINTN
)ERSTBase
823 // Need set IMAN IE bit to enble the ring interrupt
825 XhcSetRuntimeRegBit (Xhc
, XHC_IMAN_OFFSET
+ (32 * EventRing
->EventInterrupter
), XHC_IMAN_IE
);
829 Create XHCI transfer ring.
831 @param Xhc The XHCI device.
832 @param TrbNum The number of TRB in the ring.
833 @param TransferRing The created transfer ring.
838 IN USB_XHCI_DEV
*Xhc
,
840 OUT TRANSFER_RING
*TransferRing
846 Buf
= AllocateAlignedZeroPool(sizeof (TRB
) * TrbNum
, 64);
847 ASSERT (Buf
!= NULL
);
848 ASSERT (((UINTN
) Buf
& 0x3F) == 0);
850 TransferRing
->RingSeg0
= Buf
;
851 TransferRing
->TrbNumber
= TrbNum
;
852 TransferRing
->RingEnqueue
= (TRB
*) TransferRing
->RingSeg0
;
853 TransferRing
->RingDequeue
= (TRB
*) TransferRing
->RingSeg0
;
854 TransferRing
->RingPCS
= 1;
856 // 4.9.2 Transfer Ring Management
857 // To form a ring (or circular queue) a Link TRB may be inserted at the end of a ring to
858 // point to the first TRB in the ring.
860 EndTrb
= (LNK_TRB
*) ((UINTN
)Buf
+ sizeof (TRB
) * (TrbNum
- 1));
861 EndTrb
->Type
= TRB_TYPE_LINK
;
862 EndTrb
->PtrLo
= XHC_LOW_32BIT (Buf
);
863 EndTrb
->PtrHi
= XHC_HIGH_32BIT (Buf
);
865 // Toggle Cycle (TC). When set to '1', the xHC shall toggle its interpretation of the Cycle bit.
869 // Set Cycle bit as other TRB PCS init value
871 EndTrb
->CycleBit
= 0;
875 Free XHCI event ring.
877 @param Xhc The XHCI device.
878 @param EventRing The event ring to be freed.
884 IN USB_XHCI_DEV
*Xhc
,
885 IN EVENT_RING
*EventRing
889 EVENT_RING_SEG_TABLE_ENTRY
*TablePtr
;
891 EVENT_RING_SEG_TABLE_ENTRY
*EventRingPtr
;
892 UINTN InterrupterTarget
;
894 if(EventRing
->EventRingSeg0
== NULL
) {
898 InterrupterTarget
= EventRing
->EventInterrupter
;
900 // Get the Event Ring Segment Table base address
902 TablePtr
= (EVENT_RING_SEG_TABLE_ENTRY
*)(EventRing
->ERSTBase
);
905 // Get all the TRBs Ring and release
907 for (Index
= 0; Index
< ERST_NUMBER
; Index
++) {
908 EventRingPtr
= TablePtr
+ Index
;
909 RingBuf
= (VOID
*)(UINTN
)(EventRingPtr
->PtrLo
| ((UINT64
)EventRingPtr
->PtrHi
<< 32));
911 if(RingBuf
!= NULL
) {
912 FreeAlignedPool (RingBuf
);
913 ZeroMem (EventRingPtr
, sizeof (EVENT_RING_SEG_TABLE_ENTRY
));
917 FreeAlignedPool (TablePtr
);
922 Free the resouce allocated at initializing schedule.
924 @param Xhc The XHCI device.
934 if (Xhc
->ScratchBuf
!= NULL
) {
935 for (Index
= 0; Index
< Xhc
->MaxScratchpadBufs
; Index
++) {
936 FreeAlignedPool ((VOID
*)(UINTN
)*Xhc
->ScratchBuf
++);
940 if (Xhc
->DCBAA
!= NULL
) {
941 FreeAlignedPool (Xhc
->DCBAA
);
945 if (Xhc
->CmdRing
.RingSeg0
!= NULL
){
946 FreeAlignedPool (Xhc
->CmdRing
.RingSeg0
);
947 Xhc
->CmdRing
.RingSeg0
= NULL
;
949 XhcFreeEventRing (Xhc
,&Xhc
->CmdEventRing
);
950 XhcFreeEventRing (Xhc
,&Xhc
->CtrlTrEventRing
);
951 XhcFreeEventRing (Xhc
,&Xhc
->BulkTrEventRing
);
952 XhcFreeEventRing (Xhc
,&Xhc
->AsynIntTrEventRing
);
953 XhcFreeEventRing (Xhc
,&Xhc
->IntTrEventRing
);
957 Check if it is ring TRB.
959 @param Ring The transfer ring
960 @param Trb The TRB to check if it's in the transfer ring
962 @retval TRUE It is in the ring
963 @retval FALSE It is not in the ring
968 IN TRANSFER_RING
*Ring
,
976 Trb1
= Ring
->RingSeg0
;
979 ASSERT (Ring
->TrbNumber
== CMD_RING_TRB_NUMBER
|| Ring
->TrbNumber
== TR_RING_TRB_NUMBER
);
981 for (Index
= 0; Index
< Ring
->TrbNumber
; Index
++) {
993 Check the URB's execution result and update the URB's
996 @param Xhc The XHCI device.
997 @param Urb The URB to check result.
999 @return Whether the result of URB transfer is finialized.
1004 IN USB_XHCI_DEV
*Xhc
,
1010 EVT_TRB_TRANSFER
*EvtTrb
;
1016 ASSERT ((Xhc
!= NULL
) && (Urb
!= NULL
));
1019 Urb
->Result
= EFI_USB_NOERROR
;
1020 Status
= EFI_SUCCESS
;
1023 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1024 Urb
->Result
|= EFI_USB_ERR_SYSTEM
;
1025 Status
= EFI_DEVICE_ERROR
;
1030 // Restore the EventRingDequeue and poll the transfer event ring from beginning
1034 Urb
->EvtRing
->EventRingDequeue
= Urb
->EvtTrbStart
;
1035 for (Index
= 0; Index
< Urb
->EvtRing
->TrbNumber
; Index
++) {
1036 XhcSyncEventRing (Xhc
, Urb
->EvtRing
);
1037 Status
= XhcCheckNewEvent (Xhc
, Urb
->EvtRing
, ((TRB
**)&EvtTrb
));
1038 if (Status
== EFI_NOT_READY
) {
1039 Urb
->Result
|= EFI_USB_ERR_TIMEOUT
;
1043 TRBPtr
= (TRB
*)(UINTN
)(EvtTrb
->TRBPtrLo
| (UINT64
) EvtTrb
->TRBPtrHi
<< 32);
1045 switch (EvtTrb
->Completcode
) {
1046 case TRB_COMPLETION_STALL_ERROR
:
1047 Urb
->Result
|= EFI_USB_ERR_STALL
;
1048 Status
= EFI_DEVICE_ERROR
;
1049 DEBUG ((EFI_D_ERROR
, "XhcCheckUrbResult: STALL_ERROR! Completcode = %x\n",EvtTrb
->Completcode
));
1053 case TRB_COMPLETION_BABBLE_ERROR
:
1054 Urb
->Result
|= EFI_USB_ERR_BABBLE
;
1055 Status
= EFI_DEVICE_ERROR
;
1056 DEBUG ((EFI_D_ERROR
, "XhcCheckUrbResult: BABBLE_ERROR! Completcode = %x\n",EvtTrb
->Completcode
));
1060 case TRB_COMPLETION_DATA_BUFFER_ERROR
:
1061 Urb
->Result
|= EFI_USB_ERR_BUFFER
;
1062 Status
= EFI_DEVICE_ERROR
;
1063 DEBUG ((EFI_D_ERROR
, "XhcCheckUrbResult: ERR_BUFFER! Completcode = %x\n",EvtTrb
->Completcode
));
1067 case TRB_COMPLETION_USB_TRANSACTION_ERROR
:
1068 Urb
->Result
|= EFI_USB_ERR_TIMEOUT
;
1069 Status
= EFI_DEVICE_ERROR
;
1070 DEBUG ((EFI_D_ERROR
, "XhcCheckUrbResult: TRANSACTION_ERROR! Completcode = %x\n",EvtTrb
->Completcode
));
1074 case TRB_COMPLETION_SHORT_PACKET
:
1075 case TRB_COMPLETION_SUCCESS
:
1076 if (IsTransferRingTrb (Urb
->Ring
, TRBPtr
)) {
1077 if (EvtTrb
->Completcode
== TRB_COMPLETION_SHORT_PACKET
) {
1078 DEBUG ((EFI_D_ERROR
, "XhcCheckUrbResult: short packet happens!\n"));
1080 TRBType
= (UINT8
) (TRBPtr
->Type
);
1081 if ((TRBType
== TRB_TYPE_DATA_STAGE
) ||
1082 (TRBType
== TRB_TYPE_NORMAL
) ||
1083 (TRBType
== TRB_TYPE_ISOCH
)) {
1084 Urb
->Completed
+= (Urb
->DataLen
- EvtTrb
->Lenth
);
1087 Status
= EFI_SUCCESS
;
1091 DEBUG ((EFI_D_ERROR
, "Transfer Default Error Occur! Completcode = 0x%x!\n",EvtTrb
->Completcode
));
1092 Urb
->Result
|= EFI_USB_ERR_TIMEOUT
;
1093 Status
= EFI_DEVICE_ERROR
;
1099 // Only check first and end Trb event address
1101 if (TRBPtr
== Urb
->TrbStart
) {
1105 if (TRBPtr
== Urb
->TrbEnd
) {
1109 if (StartDone
&& EndDone
) {
1120 Execute the transfer by polling the URB. This is a synchronous operation.
1122 @param Xhc The XHCI device.
1123 @param CmdTransfer The executed URB is for cmd transfer or not.
1124 @param Urb The URB to execute.
1125 @param TimeOut The time to wait before abort, in millisecond.
1127 @return EFI_DEVICE_ERROR The transfer failed due to transfer error.
1128 @return EFI_TIMEOUT The transfer failed due to time out.
1129 @return EFI_SUCCESS The transfer finished OK.
1134 IN USB_XHCI_DEV
*Xhc
,
1135 IN BOOLEAN CmdTransfer
,
1150 SlotId
= XhcDevAddrToSlotId(Urb
->Ep
.DevAddr
);
1151 Dci
= XhcEndpointToDci(Urb
->Ep
.EpAddr
, (UINT8
)(Urb
->Ep
.Direction
));
1154 Status
= EFI_SUCCESS
;
1155 Loop
= (TimeOut
* XHC_1_MILLISECOND
/ XHC_SYNC_POLL_INTERVAL
) + 1;
1160 XhcRingDoorBell (Xhc
, SlotId
, Dci
);
1162 for (Index
= 0; Index
< Loop
; Index
++) {
1163 Status
= XhcCheckUrbResult (Xhc
, Urb
);
1164 if ((Status
!= EFI_NOT_READY
)) {
1167 gBS
->Stall (XHC_SYNC_POLL_INTERVAL
);
1174 Delete a single asynchronous interrupt transfer for
1175 the device and endpoint.
1177 @param Xhc The XHCI device.
1178 @param DevAddr The address of the target device.
1179 @param EpNum The endpoint of the target.
1181 @retval EFI_SUCCESS An asynchronous transfer is removed.
1182 @retval EFI_NOT_FOUND No transfer for the device is found.
1186 XhciDelAsyncIntTransfer (
1187 IN USB_XHCI_DEV
*Xhc
,
1195 EFI_USB_DATA_DIRECTION Direction
;
1198 Direction
= ((EpNum
& 0x80) != 0) ? EfiUsbDataIn
: EfiUsbDataOut
;
1204 EFI_LIST_FOR_EACH_SAFE (Entry
, Next
, &Xhc
->AsyncIntTransfers
) {
1205 Urb
= EFI_LIST_CONTAINER (Entry
, URB
, UrbList
);
1206 if ((Urb
->Ep
.DevAddr
== DevAddr
) &&
1207 (Urb
->Ep
.EpAddr
== EpNum
) &&
1208 (Urb
->Ep
.Direction
== Direction
)) {
1209 RemoveEntryList (&Urb
->UrbList
);
1210 FreePool (Urb
->Data
);
1216 return EFI_NOT_FOUND
;
1220 Remove all the asynchronous interrutp transfers.
1222 @param Xhc The XHCI device.
1226 XhciDelAllAsyncIntTransfers (
1227 IN USB_XHCI_DEV
*Xhc
1234 EFI_LIST_FOR_EACH_SAFE (Entry
, Next
, &Xhc
->AsyncIntTransfers
) {
1235 Urb
= EFI_LIST_CONTAINER (Entry
, URB
, UrbList
);
1236 RemoveEntryList (&Urb
->UrbList
);
1237 FreePool (Urb
->Data
);
1243 Update the queue head for next round of asynchronous transfer
1245 @param Xhc The XHCI device.
1246 @param Urb The URB to update
1250 XhcUpdateAsyncRequest (
1251 IN USB_XHCI_DEV
* Xhc
,
1257 if (Urb
->Result
== EFI_USB_NOERROR
) {
1258 Status
= XhcCreateTransferTrb (Xhc
, Urb
);
1259 ASSERT_EFI_ERROR (Status
);
1260 Status
= RingIntTransferDoorBell (Xhc
, Urb
);
1261 ASSERT_EFI_ERROR (Status
);
1267 Interrupt transfer periodic check handler.
1269 @param Event Interrupt event.
1270 @param Context Pointer to USB_XHCI_DEV.
1275 XhcMonitorAsyncRequests (
1289 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1291 Xhc
= (USB_XHCI_DEV
*) Context
;
1293 EFI_LIST_FOR_EACH_SAFE (Entry
, Next
, &Xhc
->AsyncIntTransfers
) {
1294 Urb
= EFI_LIST_CONTAINER (Entry
, URB
, UrbList
);
1297 // Make sure that the device is available before every check.
1299 SlotId
= XhcDevAddrToSlotId(Urb
->Ep
.DevAddr
);
1305 // Check the result of URB execution. If it is still
1306 // active, check the next one.
1308 Status
= XhcCheckUrbResult (Xhc
, Urb
);
1310 if (Status
== EFI_NOT_READY
) {
1315 // Allocate a buffer then copy the transferred data for user.
1316 // If failed to allocate the buffer, update the URB for next
1317 // round of transfer. Ignore the data of this round.
1320 if (Urb
->Result
== EFI_USB_NOERROR
) {
1321 ASSERT (Urb
->Completed
<= Urb
->DataLen
);
1323 ProcBuf
= AllocatePool (Urb
->Completed
);
1325 if (ProcBuf
== NULL
) {
1326 XhcUpdateAsyncRequest (Xhc
, Urb
);
1330 CopyMem (ProcBuf
, Urb
->Data
, Urb
->Completed
);
1333 XhcUpdateAsyncRequest (Xhc
, Urb
);
1336 // Leave error recovery to its related device driver. A
1337 // common case of the error recovery is to re-submit the
1338 // interrupt transfer which is linked to the head of the
1339 // list. This function scans from head to tail. So the
1340 // re-submitted interrupt transfer's callback function
1341 // will not be called again in this round. Don't touch this
1342 // URB after the callback, it may have been removed by the
1345 if (Urb
->Callback
!= NULL
) {
1347 // Restore the old TPL, USB bus maybe connect device in
1348 // his callback. Some drivers may has a lower TPL restriction.
1350 gBS
->RestoreTPL (OldTpl
);
1351 (Urb
->Callback
) (ProcBuf
, Urb
->Completed
, Urb
->Context
, Urb
->Result
);
1352 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1355 if (ProcBuf
!= NULL
) {
1356 gBS
->FreePool (ProcBuf
);
1359 gBS
->RestoreTPL (OldTpl
);
1363 Monitor the port status change. Enable/Disable device slot if there is a device attached/detached.
1365 @param Xhc The XHCI device.
1366 @param ParentRouteChart The route string pointed to the parent device if it exists.
1367 @param Port The port to be polled.
1368 @param PortState The port state.
1370 @retval EFI_SUCCESS Successfully enable/disable device slot according to port state.
1371 @retval Others Should not appear.
1376 XhcPollPortStatusChange (
1377 IN USB_XHCI_DEV
* Xhc
,
1378 IN USB_DEV_ROUTE ParentRouteChart
,
1380 IN EFI_USB_PORT_STATUS
*PortState
1386 USB_DEV_ROUTE RouteChart
;
1388 Status
= EFI_SUCCESS
;
1390 if (ParentRouteChart
.Dword
== 0) {
1391 RouteChart
.Field
.RouteString
= 0;
1392 RouteChart
.Field
.RootPortNum
= Port
+ 1;
1393 RouteChart
.Field
.TierNum
= 1;
1396 RouteChart
.Field
.RouteString
= ParentRouteChart
.Field
.RouteString
| (Port
<< (4 * (ParentRouteChart
.Field
.TierNum
- 1)));
1398 RouteChart
.Field
.RouteString
= ParentRouteChart
.Field
.RouteString
| (15 << (4 * (ParentRouteChart
.Field
.TierNum
- 1)));
1400 RouteChart
.Field
.RootPortNum
= ParentRouteChart
.Field
.RootPortNum
;
1401 RouteChart
.Field
.TierNum
= ParentRouteChart
.Field
.TierNum
+ 1;
1404 if (((PortState
->PortStatus
& USB_PORT_STAT_ENABLE
) != 0) &&
1405 ((PortState
->PortStatus
& USB_PORT_STAT_CONNECTION
) != 0)) {
1407 // Has a device attached, Identify device speed after port is enabled.
1409 Speed
= EFI_USB_SPEED_FULL
;
1410 if ((PortState
->PortStatus
& USB_PORT_STAT_LOW_SPEED
) != 0) {
1411 Speed
= EFI_USB_SPEED_LOW
;
1412 } else if ((PortState
->PortStatus
& USB_PORT_STAT_HIGH_SPEED
) != 0) {
1413 Speed
= EFI_USB_SPEED_HIGH
;
1414 } else if ((PortState
->PortStatus
& USB_PORT_STAT_SUPER_SPEED
) != 0) {
1415 Speed
= EFI_USB_SPEED_SUPER
;
1418 // Execute Enable_Slot cmd for attached device, initialize device context and assign device address.
1420 SlotId
= XhcRouteStringToSlotId (RouteChart
);
1422 Status
= XhcInitializeDeviceSlot (Xhc
, ParentRouteChart
, Port
, RouteChart
, Speed
);
1423 ASSERT_EFI_ERROR (Status
);
1425 } else if ((PortState
->PortStatus
& USB_PORT_STAT_CONNECTION
) == 0) {
1427 // Device is detached. Disable the allocated device slot and release resource.
1429 SlotId
= XhcRouteStringToSlotId (RouteChart
);
1431 Status
= XhcDisableSlotCmd (Xhc
, SlotId
);
1432 ASSERT_EFI_ERROR (Status
);
1440 Calculate the device context index by endpoint address and direction.
1442 @param EpAddr The target endpoint number.
1443 @param Direction The direction of the target endpoint.
1445 @return The device context index of endpoint.
1459 Index
= (UINT8
) (2 * EpAddr
);
1460 if (Direction
== EfiUsbDataIn
) {
1468 Find out the slot id according to device address assigned by XHCI's Address_Device cmd.
1470 @param DevAddr The device address of the target device.
1472 @return The slot id used by the device.
1476 XhcDevAddrToSlotId (
1482 for (Index
= 0; Index
< 255; Index
++) {
1483 if (UsbDevContext
[Index
+ 1].Enabled
&&
1484 (UsbDevContext
[Index
+ 1].SlotId
!= 0) &&
1485 (UsbDevContext
[Index
+ 1].XhciDevAddr
== DevAddr
)) {
1494 return UsbDevContext
[Index
+ 1].SlotId
;
1498 Find out the actual device address according to the requested device address from UsbBus.
1500 @param BusDevAddr The requested device address by UsbBus upper driver.
1502 @return The actual device address assigned to the device.
1507 XhcBusDevAddrToSlotId (
1513 for (Index
= 0; Index
< 255; Index
++) {
1514 if (UsbDevContext
[Index
+ 1].Enabled
&&
1515 (UsbDevContext
[Index
+ 1].SlotId
!= 0) &&
1516 (UsbDevContext
[Index
+ 1].BusDevAddr
== BusDevAddr
)) {
1525 return UsbDevContext
[Index
+ 1].SlotId
;
1529 Find out the slot id according to the device's route string.
1531 @param RouteString The route string described the device location.
1533 @return The slot id used by the device.
1538 XhcRouteStringToSlotId (
1539 IN USB_DEV_ROUTE RouteString
1544 for (Index
= 0; Index
< 255; Index
++) {
1545 if (UsbDevContext
[Index
+ 1].Enabled
&&
1546 (UsbDevContext
[Index
+ 1].SlotId
!= 0) &&
1547 (UsbDevContext
[Index
+ 1].RouteString
.Dword
== RouteString
.Dword
)) {
1556 return UsbDevContext
[Index
+ 1].SlotId
;
1560 Synchronize the specified event ring to update the enqueue and dequeue pointer.
1562 @param Xhc The XHCI device.
1563 @param EvtRing The event ring to sync.
1565 @retval EFI_SUCCESS The event ring is synchronized successfully.
1571 IN USB_XHCI_DEV
*Xhc
,
1572 IN EVENT_RING
*EvtRing
1580 ASSERT (EvtRing
!= NULL
);
1583 // Calculate the EventRingEnqueue and EventRingCCS.
1584 // Note: only support single Segment
1586 EvtTrb1
= EvtRing
->EventRingSeg0
;
1587 EvtTrb2
= EvtRing
->EventRingSeg0
;
1589 for (Index
= 0; Index
< EvtRing
->TrbNumber
; Index
++) {
1590 if (EvtTrb1
->CycleBit
!= EvtTrb2
->CycleBit
) {
1596 if (Index
< EvtRing
->TrbNumber
) {
1597 EvtRing
->EventRingEnqueue
= EvtTrb1
;
1598 EvtRing
->EventRingCCS
= (EvtTrb2
->CycleBit
) ? 1 : 0;
1600 EvtRing
->EventRingEnqueue
= EvtTrb2
;
1601 EvtRing
->EventRingCCS
= (EvtTrb2
->CycleBit
) ? 0 : 1;
1605 // Apply the EventRingDequeue to Xhc
1607 XhcDequeue
= (TRB
*)(UINTN
) XhcReadRuntimeReg64 (
1609 XHC_ERDP_OFFSET
+ (32 * EvtRing
->EventInterrupter
)
1612 if (((UINT64
)(UINTN
)XhcDequeue
& (~0x0F)) != ((UINT64
)(UINTN
)EvtRing
->EventRingDequeue
& (~0x0F))) {
1613 XhcWriteRuntimeReg64 (
1615 XHC_ERDP_OFFSET
+ (32 * EvtRing
->EventInterrupter
),
1616 (UINT64
)(UINTN
)EvtRing
->EventRingDequeue
| BIT3
1624 Synchronize the specified transfer ring to update the enqueue and dequeue pointer.
1626 @param Xhc The XHCI device.
1627 @param TrsRing The transfer ring to sync.
1629 @retval EFI_SUCCESS The transfer ring is synchronized successfully.
1635 IN USB_XHCI_DEV
*Xhc
,
1636 IN TRANSFER_RING
*TrsRing
1642 ASSERT (TrsRing
!= NULL
);
1644 // Calculate the latest RingEnqueue and RingPCS
1646 TrsTrb
= TrsRing
->RingEnqueue
;
1647 ASSERT (TrsTrb
!= NULL
);
1649 for (Index
= 0; Index
< TrsRing
->TrbNumber
; Index
++) {
1650 if (TrsTrb
->CycleBit
!= (TrsRing
->RingPCS
& BIT0
)) {
1654 if ((UINT8
) TrsTrb
->Type
== TRB_TYPE_LINK
) {
1655 ASSERT (((LNK_TRB
*)TrsTrb
)->TC
!= 0);
1657 // set cycle bit in Link TRB as normal
1659 ((LNK_TRB
*)TrsTrb
)->CycleBit
= TrsRing
->RingPCS
& BIT0
;
1661 // Toggle PCS maintained by software
1663 TrsRing
->RingPCS
= (TrsRing
->RingPCS
& BIT0
) ? 0 : 1;
1664 TrsTrb
= (TRB
*)(UINTN
)((TrsTrb
->Dword1
| ((UINT64
)TrsTrb
->Dword2
<< 32)) & ~0x0F);
1668 ASSERT (Index
!= TrsRing
->TrbNumber
);
1670 if (TrsTrb
!= TrsRing
->RingEnqueue
) {
1671 TrsRing
->RingEnqueue
= TrsTrb
;
1675 // Clear the Trb context for enqueue, but reserve the PCS bit
1688 Check if there is a new generated event.
1690 @param Xhc The XHCI device.
1691 @param EvtRing The event ring to check.
1692 @param NewEvtTrb The new event TRB found.
1694 @retval EFI_SUCCESS Found a new event TRB at the event ring.
1695 @retval EFI_NOT_READY The event ring has no new event.
1701 IN USB_XHCI_DEV
*Xhc
,
1702 IN EVENT_RING
*EvtRing
,
1709 ASSERT (EvtRing
!= NULL
);
1711 EvtTrb
= EvtRing
->EventRingDequeue
;
1712 *NewEvtTrb
= EvtRing
->EventRingDequeue
;
1714 if (EvtRing
->EventRingDequeue
== EvtRing
->EventRingEnqueue
) {
1715 return EFI_NOT_READY
;
1718 Status
= EFI_SUCCESS
;
1720 if (((EvtTrb
->Dword3
>> 24) & 0xFF) != TRB_COMPLETION_SUCCESS
) {
1721 Status
= EFI_DEVICE_ERROR
;
1724 EvtRing
->EventRingDequeue
++;
1726 // If the dequeue pointer is beyond the ring, then roll-back it to the begining of the ring.
1728 if ((UINTN
)EvtRing
->EventRingDequeue
>= ((UINTN
) EvtRing
->EventRingSeg0
+ sizeof (TRB
) * EvtRing
->TrbNumber
)) {
1729 EvtRing
->EventRingDequeue
= EvtRing
->EventRingSeg0
;
1736 Ring the door bell to notify XHCI there is a transaction to be executed.
1738 @param Xhc The XHCI device.
1739 @param SlotId The slot id of the target device.
1740 @param Dci The device context index of the target slot or endpoint.
1742 @retval EFI_SUCCESS Successfully ring the door bell.
1748 IN USB_XHCI_DEV
*Xhc
,
1754 XhcWriteDoorBellReg (Xhc
, 0, 0);
1756 XhcWriteDoorBellReg (Xhc
, SlotId
* sizeof (UINT32
), Dci
);
1763 Ring the door bell to notify XHCI there is a transaction to be executed through URB.
1765 @param Xhc The XHCI device.
1766 @param Urb The URB to be rung.
1768 @retval EFI_SUCCESS Successfully ring the door bell.
1772 RingIntTransferDoorBell (
1773 IN USB_XHCI_DEV
*Xhc
,
1780 SlotId
= XhcDevAddrToSlotId(Urb
->Ep
.DevAddr
);
1781 Dci
= XhcEndpointToDci(Urb
->Ep
.EpAddr
, (UINT8
)(Urb
->Ep
.Direction
));
1782 XhcRingDoorBell (Xhc
, SlotId
, Dci
);
1787 Assign and initialize the device slot for a new device.
1789 @param Xhc The XHCI device.
1790 @param ParentRouteChart The route string pointed to the parent device.
1791 @param ParentPort The port at which the device is located.
1792 @param RouteChart The route string pointed to the device.
1793 @param DeviceSpeed The device speed.
1795 @retval EFI_SUCCESS Successfully assign a slot to the device and assign an address to it.
1800 XhcInitializeDeviceSlot (
1801 IN USB_XHCI_DEV
*Xhc
,
1802 IN USB_DEV_ROUTE ParentRouteChart
,
1803 IN UINT16 ParentPort
,
1804 IN USB_DEV_ROUTE RouteChart
,
1805 IN UINT8 DeviceSpeed
1809 EVT_TRB_COMMAND
*EvtTrb
;
1810 INPUT_CONTEXT
*InputContext
;
1811 DEVICE_CONTEXT
*OutputDevContxt
;
1812 TRANSFER_RING
*EndpointTransferRing
;
1813 CMD_TRB_ADDR_DEV CmdTrbAddr
;
1814 UINT8 DeviceAddress
;
1815 CMD_TRB_EN_SLOT CmdTrb
;
1818 DEVICE_CONTEXT
*ParentDeviceContext
;
1820 ZeroMem (&CmdTrb
, sizeof (CMD_TRB_EN_SLOT
));
1821 CmdTrb
.CycleBit
= 1;
1822 CmdTrb
.Type
= TRB_TYPE_EN_SLOT
;
1824 Status
= XhcCmdTransfer (
1826 (TRB
*) (UINTN
) &CmdTrb
,
1827 XHC_GENERIC_TIMEOUT
,
1828 (TRB
**) (UINTN
) &EvtTrb
1830 ASSERT_EFI_ERROR (Status
);
1831 ASSERT (EvtTrb
->SlotId
<= Xhc
->MaxSlotsEn
);
1832 DEBUG ((EFI_D_INFO
, "Enable Slot Successfully, The Slot ID = 0x%x\n", EvtTrb
->SlotId
));
1833 SlotId
= (UINT8
)EvtTrb
->SlotId
;
1834 ASSERT (SlotId
!= 0);
1836 ZeroMem (&UsbDevContext
[SlotId
], sizeof (USB_DEV_CONTEXT
));
1837 UsbDevContext
[SlotId
].Enabled
= TRUE
;
1838 UsbDevContext
[SlotId
].SlotId
= SlotId
;
1839 UsbDevContext
[SlotId
].RouteString
.Dword
= RouteChart
.Dword
;
1840 UsbDevContext
[SlotId
].ParentRouteString
.Dword
= ParentRouteChart
.Dword
;
1843 // 4.3.3 Device Slot Initialization
1844 // 1) Allocate an Input Context data structure (6.2.5) and initialize all fields to '0'.
1846 InputContext
= AllocateAlignedZeroPool(sizeof (INPUT_CONTEXT
), 64);
1847 ASSERT (InputContext
!= NULL
);
1848 ASSERT (((UINTN
) InputContext
& 0x3F) == 0);
1850 UsbDevContext
[SlotId
].InputContext
= (VOID
*) InputContext
;
1853 // 2) Initialize the Input Control Context (6.2.5.1) of the Input Context by setting the A0 and A1
1854 // flags to '1'. These flags indicate that the Slot Context and the Endpoint 0 Context of the Input
1855 // Context are affected by the command.
1857 InputContext
->InputControlContext
.Dword2
|= (BIT0
| BIT1
);
1860 // 3) Initialize the Input Slot Context data structure
1862 InputContext
->Slot
.RouteStr
= RouteChart
.Field
.RouteString
;
1863 InputContext
->Slot
.Speed
= DeviceSpeed
+ 1;
1864 InputContext
->Slot
.ContextEntries
= 1;
1865 InputContext
->Slot
.RootHubPortNum
= RouteChart
.Field
.RootPortNum
;
1867 if (RouteChart
.Field
.RouteString
) {
1869 // The device is behind of hub device.
1871 ParentSlotId
= XhcRouteStringToSlotId(ParentRouteChart
);
1872 ASSERT (ParentSlotId
!= 0);
1874 //if the Full/Low device attached to a High Speed Hub, Init the TTPortNum and TTHubSlotId field of slot context
1876 ParentDeviceContext
= (DEVICE_CONTEXT
*)UsbDevContext
[ParentSlotId
].OutputDevContxt
;
1877 if ((ParentDeviceContext
->Slot
.TTPortNum
== 0) &&
1878 (ParentDeviceContext
->Slot
.TTHubSlotId
== 0)) {
1879 if ((ParentDeviceContext
->Slot
.Speed
== (EFI_USB_SPEED_HIGH
+ 1)) && (DeviceSpeed
< EFI_USB_SPEED_HIGH
)) {
1881 // Full/Low device attached to High speed hub port that isolates the high speed signaling
1882 // environment from Full/Low speed signaling environment for a device
1884 InputContext
->Slot
.TTPortNum
= ParentPort
;
1885 InputContext
->Slot
.TTHubSlotId
= ParentSlotId
;
1889 // Inherit the TT parameters from parent device.
1891 InputContext
->Slot
.TTPortNum
= ParentDeviceContext
->Slot
.TTPortNum
;
1892 InputContext
->Slot
.TTHubSlotId
= ParentDeviceContext
->Slot
.TTHubSlotId
;
1894 // If the device is a High speed device then down the speed to be the same as its parent Hub
1896 if (DeviceSpeed
== EFI_USB_SPEED_HIGH
) {
1897 InputContext
->Slot
.Speed
= ParentDeviceContext
->Slot
.Speed
;
1903 // 4) Allocate and initialize the Transfer Ring for the Default Control Endpoint.
1905 EndpointTransferRing
= AllocateAlignedZeroPool(sizeof (TRANSFER_RING
), 64);
1906 UsbDevContext
[SlotId
].EndpointTransferRing
[0] = EndpointTransferRing
;
1907 CreateTransferRing(Xhc
, TR_RING_TRB_NUMBER
, (TRANSFER_RING
*)UsbDevContext
[SlotId
].EndpointTransferRing
[0]);
1909 // 5) Initialize the Input default control Endpoint 0 Context (6.2.3).
1911 InputContext
->EP
[0].EPType
= ED_CONTROL_BIDIR
;
1913 if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
1914 InputContext
->EP
[0].MaxPacketSize
= 512;
1915 } else if (DeviceSpeed
== EFI_USB_SPEED_HIGH
) {
1916 InputContext
->EP
[0].MaxPacketSize
= 64;
1918 InputContext
->EP
[0].MaxPacketSize
= 8;
1921 // Initial value of Average TRB Length for Control endpoints would be 8B, Interrupt endpoints
1922 // 1KB, and Bulk and Isoch endpoints 3KB.
1924 InputContext
->EP
[0].AverageTRBLength
= 8;
1925 InputContext
->EP
[0].MaxBurstSize
= 0;
1926 InputContext
->EP
[0].Interval
= 0;
1927 InputContext
->EP
[0].MaxPStreams
= 0;
1928 InputContext
->EP
[0].Mult
= 0;
1929 InputContext
->EP
[0].CErr
= 3;
1932 // Init the DCS(dequeue cycle state) as the transfer ring's CCS
1934 InputContext
->EP
[0].PtrLo
= XHC_LOW_32BIT (((TRANSFER_RING
*)(UINTN
)UsbDevContext
[SlotId
].EndpointTransferRing
[0])->RingSeg0
) | BIT0
;
1935 InputContext
->EP
[0].PtrHi
= XHC_HIGH_32BIT (((TRANSFER_RING
*)(UINTN
)UsbDevContext
[SlotId
].EndpointTransferRing
[0])->RingSeg0
);
1938 // 6) Allocate the Output Device Context data structure (6.2.1) and initialize it to '0'.
1940 OutputDevContxt
= AllocateAlignedZeroPool(sizeof (DEVICE_CONTEXT
), 64);
1941 ASSERT (OutputDevContxt
!= NULL
);
1942 ASSERT (((UINTN
) OutputDevContxt
& 0x3F) == 0);
1944 UsbDevContext
[SlotId
].OutputDevContxt
= OutputDevContxt
;
1946 // 7) Load the appropriate (Device Slot ID) entry in the Device Context Base Address Array (5.4.6) with
1947 // a pointer to the Output Device Context data structure (6.2.1).
1949 Xhc
->DCBAA
[SlotId
] = (UINT64
) (UINTN
) OutputDevContxt
;
1952 // 8) Issue an Address Device Command for the Device Slot, where the command points to the Input
1953 // Context data structure described above.
1955 ZeroMem (&CmdTrbAddr
, sizeof (CmdTrbAddr
));
1956 CmdTrbAddr
.PtrLo
= XHC_LOW_32BIT (UsbDevContext
[SlotId
].InputContext
);
1957 CmdTrbAddr
.PtrHi
= XHC_HIGH_32BIT (UsbDevContext
[SlotId
].InputContext
);
1958 CmdTrbAddr
.CycleBit
= 1;
1959 CmdTrbAddr
.Type
= TRB_TYPE_ADDRESS_DEV
;
1960 CmdTrbAddr
.SlotId
= UsbDevContext
[SlotId
].SlotId
;
1961 Status
= XhcCmdTransfer (
1963 (TRB
*) (UINTN
) &CmdTrbAddr
,
1964 XHC_GENERIC_TIMEOUT
,
1965 (TRB
**) (UINTN
) &EvtTrb
1967 ASSERT (!EFI_ERROR(Status
));
1969 DeviceAddress
= (UINT8
) ((DEVICE_CONTEXT
*) OutputDevContxt
)->Slot
.DeviceAddress
;
1970 DEBUG ((EFI_D_INFO
, " Address %d assigned succeefully\n", DeviceAddress
));
1972 UsbDevContext
[SlotId
].XhciDevAddr
= DeviceAddress
;
1978 Disable the specified device slot.
1980 @param Xhc The XHCI device.
1981 @param SlotId The slot id to be disabled.
1983 @retval EFI_SUCCESS Successfully disable the device slot.
1989 IN USB_XHCI_DEV
*Xhc
,
1995 CMD_TRB_DIS_SLOT CmdTrbDisSlot
;
2000 // Disable the device slots occupied by these devices on its downstream ports.
2001 // Entry 0 is reserved.
2003 for (Index
= 0; Index
< 255; Index
++) {
2004 if (!UsbDevContext
[Index
+ 1].Enabled
||
2005 (UsbDevContext
[Index
+ 1].SlotId
== 0) ||
2006 (UsbDevContext
[Index
+ 1].ParentRouteString
.Dword
!= UsbDevContext
[SlotId
].RouteString
.Dword
)) {
2010 Status
= XhcDisableSlotCmd (Xhc
, UsbDevContext
[Index
+ 1].SlotId
);
2012 if (EFI_ERROR (Status
)) {
2013 DEBUG ((EFI_D_ERROR
, "XhcDisableSlotCmd: failed to disable child, ignore error\n"));
2014 UsbDevContext
[Index
+ 1].SlotId
= 0;
2019 // Construct the disable slot command
2021 DEBUG ((EFI_D_INFO
, "Disable device slot %d!\n", SlotId
));
2023 ZeroMem (&CmdTrbDisSlot
, sizeof (CmdTrbDisSlot
));
2024 CmdTrbDisSlot
.CycleBit
= 1;
2025 CmdTrbDisSlot
.Type
= TRB_TYPE_DIS_SLOT
;
2026 CmdTrbDisSlot
.SlotId
= SlotId
;
2027 Status
= XhcCmdTransfer (
2029 (TRB
*) (UINTN
) &CmdTrbDisSlot
,
2030 XHC_GENERIC_TIMEOUT
,
2031 (TRB
**) (UINTN
) &EvtTrb
2033 ASSERT_EFI_ERROR(Status
);
2035 // Free the slot's device context entry
2037 Xhc
->DCBAA
[SlotId
] = 0;
2040 // Free the slot related data structure
2042 for (Index
= 0; Index
< 31; Index
++) {
2043 if (UsbDevContext
[SlotId
].EndpointTransferRing
[Index
] != NULL
) {
2044 RingSeg
= ((TRANSFER_RING
*)(UINTN
)UsbDevContext
[SlotId
].EndpointTransferRing
[Index
])->RingSeg0
;
2045 if (RingSeg
!= NULL
) {
2046 FreeAlignedPool(RingSeg
);
2048 FreeAlignedPool(UsbDevContext
[SlotId
].EndpointTransferRing
[Index
]);
2052 for (Index
= 0; Index
< UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
; Index
++) {
2053 if (UsbDevContext
[SlotId
].ConfDesc
[Index
] != NULL
) {
2054 FreePool (UsbDevContext
[SlotId
].ConfDesc
[Index
]);
2058 if (UsbDevContext
[SlotId
].InputContext
!= NULL
) {
2059 FreeAlignedPool (UsbDevContext
[SlotId
].InputContext
);
2062 if (UsbDevContext
[SlotId
].OutputDevContxt
!= NULL
) {
2063 FreeAlignedPool (UsbDevContext
[SlotId
].OutputDevContxt
);
2066 // Doesn't zero the entry because XhcAsyncInterruptTransfer() may be invoked to remove the established
2067 // asynchronous interrupt pipe after the device is disabled. It needs the device address mapping info to
2068 // remove urb from XHCI's asynchronous transfer list.
2070 UsbDevContext
[SlotId
].Enabled
= FALSE
;
2076 Configure all the device endpoints through XHCI's Configure_Endpoint cmd.
2078 @param Xhc The XHCI device.
2079 @param SlotId The slot id to be configured.
2080 @param DeviceSpeed The device's speed.
2081 @param ConfigDesc The pointer to the usb device configuration descriptor.
2083 @retval EFI_SUCCESS Successfully configure all the device endpoints.
2089 IN USB_XHCI_DEV
*Xhc
,
2091 IN UINT8 DeviceSpeed
,
2092 IN USB_CONFIG_DESCRIPTOR
*ConfigDesc
2097 USB_INTERFACE_DESCRIPTOR
*IfDesc
;
2098 USB_ENDPOINT_DESCRIPTOR
*EpDesc
;
2109 TRANSFER_RING
*EndpointTransferRing
;
2110 CMD_CFG_ED CmdTrbCfgEP
;
2111 INPUT_CONTEXT
*InputContext
;
2112 DEVICE_CONTEXT
*OutputDevContxt
;
2113 EVT_TRB_COMMAND
*EvtTrb
;
2115 // 4.6.6 Configure Endpoint
2117 InputContext
= UsbDevContext
[SlotId
].InputContext
;
2118 OutputDevContxt
= UsbDevContext
[SlotId
].OutputDevContxt
;
2119 ZeroMem (InputContext
, sizeof (INPUT_CONTEXT
));
2120 CopyMem (&InputContext
->Slot
, &OutputDevContxt
->Slot
, sizeof (SLOT_CONTEXT
));
2122 ASSERT (ConfigDesc
!= NULL
);
2126 IfDesc
= (USB_INTERFACE_DESCRIPTOR
*)(ConfigDesc
+ 1);
2127 for (Index
= 0; Index
< ConfigDesc
->NumInterfaces
; Index
++) {
2128 while (IfDesc
->DescriptorType
!= USB_DESC_TYPE_INTERFACE
) {
2129 IfDesc
= (USB_INTERFACE_DESCRIPTOR
*)((UINTN
)IfDesc
+ IfDesc
->Length
);
2132 NumEp
= IfDesc
->NumEndpoints
;
2134 EpDesc
= (USB_ENDPOINT_DESCRIPTOR
*)(IfDesc
+ 1);
2135 for (EpIndex
= 0; EpIndex
< NumEp
; EpIndex
++) {
2136 while (EpDesc
->DescriptorType
!= USB_DESC_TYPE_ENDPOINT
) {
2137 EpDesc
= (USB_ENDPOINT_DESCRIPTOR
*)((UINTN
)EpDesc
+ EpDesc
->Length
);
2140 EpAddr
= (UINT8
)(EpDesc
->EndpointAddress
& 0x0F);
2141 Direction
= (UINT8
)((EpDesc
->EndpointAddress
& 0x80) ? EfiUsbDataIn
: EfiUsbDataOut
);
2143 Dci
= XhcEndpointToDci (EpAddr
, Direction
);
2148 InputContext
->InputControlContext
.Dword2
|= (BIT0
<< Dci
);
2149 InputContext
->EP
[Dci
-1].MaxPacketSize
= EpDesc
->MaxPacketSize
;
2151 if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
2153 // 6.2.3.4, shall be set to the value defined in the bMaxBurst field of the SuperSpeed Endpoint Companion Descriptor.
2155 InputContext
->EP
[Dci
-1].MaxBurstSize
= 0x0;
2157 InputContext
->EP
[Dci
-1].MaxBurstSize
= 0x0;
2160 switch (EpDesc
->Attributes
& USB_ENDPOINT_TYPE_MASK
) {
2161 case USB_ENDPOINT_BULK
:
2162 if (Direction
== EfiUsbDataIn
) {
2163 InputContext
->EP
[Dci
-1].CErr
= 3;
2164 InputContext
->EP
[Dci
-1].EPType
= ED_BULK_IN
;
2166 InputContext
->EP
[Dci
-1].CErr
= 3;
2167 InputContext
->EP
[Dci
-1].EPType
= ED_BULK_OUT
;
2170 InputContext
->EP
[Dci
-1].AverageTRBLength
= 0x1000;
2171 if (UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1] == NULL
) {
2172 EndpointTransferRing
= AllocateAlignedZeroPool(sizeof (TRANSFER_RING
), 64);
2173 UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1] = (VOID
*) EndpointTransferRing
;
2174 CreateTransferRing(Xhc
, TR_RING_TRB_NUMBER
, (TRANSFER_RING
*)UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1]);
2178 case USB_ENDPOINT_ISO
:
2179 if (Direction
== EfiUsbDataIn
) {
2180 InputContext
->EP
[Dci
-1].CErr
= 0;
2181 InputContext
->EP
[Dci
-1].EPType
= ED_ISOCH_IN
;
2183 InputContext
->EP
[Dci
-1].CErr
= 0;
2184 InputContext
->EP
[Dci
-1].EPType
= ED_ISOCH_OUT
;
2187 case USB_ENDPOINT_INTERRUPT
:
2188 if (Direction
== EfiUsbDataIn
) {
2189 InputContext
->EP
[Dci
-1].CErr
= 3;
2190 InputContext
->EP
[Dci
-1].EPType
= ED_INTERRUPT_IN
;
2192 InputContext
->EP
[Dci
-1].CErr
= 3;
2193 InputContext
->EP
[Dci
-1].EPType
= ED_INTERRUPT_OUT
;
2195 InputContext
->EP
[Dci
-1].AverageTRBLength
= 0x1000;
2196 InputContext
->EP
[Dci
-1].MaxESITPayload
= EpDesc
->MaxPacketSize
;
2198 // Get the bInterval from descriptor and init the the interval field of endpoint context
2200 if ((DeviceSpeed
== EFI_USB_SPEED_FULL
) || (DeviceSpeed
== EFI_USB_SPEED_LOW
)) {
2201 Interval
= EpDesc
->Interval
;
2203 // BUGBUG: Hard code the interval to MAX
2205 InputContext
->EP
[Dci
-1].Interval
= 6;
2206 } else if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
2207 Interval
= EpDesc
->Interval
;
2208 InputContext
->EP
[Dci
-1].Interval
= 0x0F;
2209 InputContext
->EP
[Dci
-1].AverageTRBLength
= 0x1000;
2210 InputContext
->EP
[Dci
-1].MaxESITPayload
= 0x0002;
2211 InputContext
->EP
[Dci
-1].MaxBurstSize
= 0x0;
2212 InputContext
->EP
[Dci
-1].CErr
= 3;
2215 if (UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1] == NULL
) {
2216 EndpointTransferRing
= AllocateAlignedZeroPool(sizeof (TRANSFER_RING
), 64);
2217 UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1] = (VOID
*) EndpointTransferRing
;
2218 CreateTransferRing(Xhc
, TR_RING_TRB_NUMBER
, (TRANSFER_RING
*)UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1]);
2222 case USB_ENDPOINT_CONTROL
:
2228 PhyAddr
= XHC_LOW_32BIT (((TRANSFER_RING
*)(UINTN
)UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1])->RingSeg0
);
2230 PhyAddr
|= ((TRANSFER_RING
*)(UINTN
)UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1])->RingPCS
;
2231 InputContext
->EP
[Dci
-1].PtrLo
= PhyAddr
;
2232 InputContext
->EP
[Dci
-1].PtrHi
= XHC_HIGH_32BIT (((TRANSFER_RING
*)(UINTN
)UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1])->RingSeg0
);
2234 EpDesc
= (USB_ENDPOINT_DESCRIPTOR
*)((UINTN
)EpDesc
+ EpDesc
->Length
);
2236 IfDesc
= (USB_INTERFACE_DESCRIPTOR
*)((UINTN
)IfDesc
+ IfDesc
->Length
);
2239 InputContext
->InputControlContext
.Dword2
|= BIT0
;
2240 InputContext
->Slot
.ContextEntries
= MaxDci
;
2242 // configure endpoint
2244 ZeroMem (&CmdTrbCfgEP
, sizeof (CmdTrbCfgEP
));
2245 CmdTrbCfgEP
.PtrLo
= XHC_LOW_32BIT (InputContext
);
2246 CmdTrbCfgEP
.PtrHi
= XHC_HIGH_32BIT (InputContext
);
2247 CmdTrbCfgEP
.CycleBit
= 1;
2248 CmdTrbCfgEP
.Type
= TRB_TYPE_CON_ENDPOINT
;
2249 CmdTrbCfgEP
.SlotId
= UsbDevContext
[SlotId
].SlotId
;
2250 DEBUG ((EFI_D_INFO
, "Configure Endpoint\n"));
2251 Status
= XhcCmdTransfer (
2253 (TRB
*) (UINTN
) &CmdTrbCfgEP
,
2254 XHC_GENERIC_TIMEOUT
,
2255 (TRB
**) (UINTN
) &EvtTrb
2257 ASSERT_EFI_ERROR(Status
);
2263 Evaluate the endpoint 0 context through XHCI's Evaluate_Context cmd.
2265 @param Xhc The XHCI device.
2266 @param SlotId The slot id to be evaluated.
2267 @param MaxPacketSize The max packet size supported by the device control transfer.
2269 @retval EFI_SUCCESS Successfully evaluate the device endpoint 0.
2274 XhcEvaluateContext (
2275 IN USB_XHCI_DEV
*Xhc
,
2277 IN UINT32 MaxPacketSize
2281 CMD_TRB_EVALU_CONTX CmdTrbEvalu
;
2282 EVT_TRB_COMMAND
*EvtTrb
;
2283 INPUT_CONTEXT
*InputContext
;
2285 ASSERT (UsbDevContext
[SlotId
].SlotId
!= 0);
2288 // 4.6.7 Evaluate Context
2290 InputContext
= UsbDevContext
[SlotId
].InputContext
;
2291 ZeroMem (InputContext
, sizeof (INPUT_CONTEXT
));
2293 InputContext
->InputControlContext
.Dword2
|= BIT1
;
2294 InputContext
->EP
[0].MaxPacketSize
= MaxPacketSize
;
2296 ZeroMem (&CmdTrbEvalu
, sizeof (CmdTrbEvalu
));
2297 CmdTrbEvalu
.PtrLo
= XHC_LOW_32BIT (InputContext
);
2298 CmdTrbEvalu
.PtrHi
= XHC_HIGH_32BIT (InputContext
);
2299 CmdTrbEvalu
.CycleBit
= 1;
2300 CmdTrbEvalu
.Type
= TRB_TYPE_EVALU_CONTXT
;
2301 CmdTrbEvalu
.SlotId
= UsbDevContext
[SlotId
].SlotId
;
2302 DEBUG ((EFI_D_INFO
, "Evaluate context\n"));
2303 Status
= XhcCmdTransfer (
2305 (TRB
*) (UINTN
) &CmdTrbEvalu
,
2306 XHC_GENERIC_TIMEOUT
,
2307 (TRB
**) (UINTN
) &EvtTrb
2309 ASSERT (!EFI_ERROR(Status
));
2315 Evaluate the slot context for hub device through XHCI's Configure_Endpoint cmd.
2317 @param Xhc The XHCI device.
2318 @param SlotId The slot id to be configured.
2319 @param PortNum The total number of downstream port supported by the hub.
2320 @param TTT The TT think time of the hub device.
2321 @param MTT The multi-TT of the hub device.
2323 @retval EFI_SUCCESS Successfully configure the hub device's slot context.
2327 XhcConfigHubContext (
2328 IN USB_XHCI_DEV
*Xhc
,
2337 EVT_TRB_COMMAND
*EvtTrb
;
2338 INPUT_CONTEXT
*InputContext
;
2339 DEVICE_CONTEXT
*OutputDevContxt
;
2340 CMD_CFG_ED CmdTrbCfgEP
;
2342 ASSERT (UsbDevContext
[SlotId
].SlotId
!= 0);
2343 InputContext
= UsbDevContext
[SlotId
].InputContext
;
2344 OutputDevContxt
= UsbDevContext
[SlotId
].OutputDevContxt
;
2347 // 4.6.7 Evaluate Context
2349 ZeroMem (InputContext
, sizeof (INPUT_CONTEXT
));
2351 InputContext
->InputControlContext
.Dword2
|= BIT0
;
2354 // Copy the slot context from OutputContext to Input context
2356 CopyMem(&(InputContext
->Slot
), &(OutputDevContxt
->Slot
), sizeof (SLOT_CONTEXT
));
2357 InputContext
->Slot
.Hub
= 1;
2358 InputContext
->Slot
.PortNum
= PortNum
;
2359 InputContext
->Slot
.TTT
= TTT
;
2360 InputContext
->Slot
.MTT
= MTT
;
2362 ZeroMem (&CmdTrbCfgEP
, sizeof (CmdTrbCfgEP
));
2363 CmdTrbCfgEP
.PtrLo
= XHC_LOW_32BIT (InputContext
);
2364 CmdTrbCfgEP
.PtrHi
= XHC_HIGH_32BIT (InputContext
);
2365 CmdTrbCfgEP
.CycleBit
= 1;
2366 CmdTrbCfgEP
.Type
= TRB_TYPE_CON_ENDPOINT
;
2367 CmdTrbCfgEP
.SlotId
= UsbDevContext
[SlotId
].SlotId
;
2368 DEBUG ((EFI_D_INFO
, "Configure Hub Slot Context\n"));
2369 Status
= XhcCmdTransfer (
2371 (TRB
*) (UINTN
) &CmdTrbCfgEP
,
2372 XHC_GENERIC_TIMEOUT
,
2373 (TRB
**) (UINTN
) &EvtTrb
2375 ASSERT (!EFI_ERROR(Status
));