3 XHCI transfer scheduling routines.
5 Copyright (c) 2011 - 2013, 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 Create a command transfer TRB to support XHCI command interfaces.
21 @param Xhc The XHCI Instance.
22 @param CmdTrb The cmd TRB to be executed.
24 @return Created URB or NULL.
29 IN USB_XHCI_INSTANCE
*Xhc
,
30 IN TRB_TEMPLATE
*CmdTrb
35 Urb
= AllocateZeroPool (sizeof (URB
));
40 Urb
->Signature
= XHC_URB_SIG
;
42 Urb
->Ring
= &Xhc
->CmdRing
;
43 XhcSyncTrsRing (Xhc
, Urb
->Ring
);
45 Urb
->TrbStart
= Urb
->Ring
->RingEnqueue
;
46 CopyMem (Urb
->TrbStart
, CmdTrb
, sizeof (TRB_TEMPLATE
));
47 Urb
->TrbStart
->CycleBit
= Urb
->Ring
->RingPCS
& BIT0
;
48 Urb
->TrbEnd
= Urb
->TrbStart
;
54 Execute a XHCI cmd TRB pointed by CmdTrb.
56 @param Xhc The XHCI Instance.
57 @param CmdTrb The cmd TRB to be executed.
58 @param Timeout Indicates the maximum time, in millisecond, which the
59 transfer is allowed to complete.
60 @param EvtTrb The event TRB corresponding to the cmd TRB.
62 @retval EFI_SUCCESS The transfer was completed successfully.
63 @retval EFI_INVALID_PARAMETER Some parameters are invalid.
64 @retval EFI_TIMEOUT The transfer failed due to timeout.
65 @retval EFI_DEVICE_ERROR The transfer failed due to host controller error.
71 IN USB_XHCI_INSTANCE
*Xhc
,
72 IN TRB_TEMPLATE
*CmdTrb
,
74 OUT TRB_TEMPLATE
**EvtTrb
81 // Validate the parameters
83 if ((Xhc
== NULL
) || (CmdTrb
== NULL
)) {
84 return EFI_INVALID_PARAMETER
;
87 Status
= EFI_DEVICE_ERROR
;
89 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
90 DEBUG ((EFI_D_ERROR
, "XhcCmdTransfer: HC is halted\n"));
95 // Create a new URB, then poll the execution status.
97 Urb
= XhcCreateCmdTrb (Xhc
, CmdTrb
);
100 DEBUG ((EFI_D_ERROR
, "XhcCmdTransfer: failed to create URB\n"));
101 Status
= EFI_OUT_OF_RESOURCES
;
105 Status
= XhcExecTransfer (Xhc
, TRUE
, Urb
, Timeout
);
106 *EvtTrb
= Urb
->EvtTrb
;
108 if (Urb
->Result
== EFI_USB_NOERROR
) {
109 Status
= EFI_SUCCESS
;
112 XhcFreeUrb (Xhc
, Urb
);
119 Create a new URB for a new transaction.
121 @param Xhc The XHCI Instance
122 @param BusAddr The logical device address assigned by UsbBus driver
123 @param EpAddr Endpoint addrress
124 @param DevSpeed The device speed
125 @param MaxPacket The max packet length of the endpoint
126 @param Type The transaction type
127 @param Request The standard USB request for control transfer
128 @param Data The user data to transfer
129 @param DataLen The length of data buffer
130 @param Callback The function to call when data is transferred
131 @param Context The context to the callback
133 @return Created URB or NULL
138 IN USB_XHCI_INSTANCE
*Xhc
,
144 IN EFI_USB_DEVICE_REQUEST
*Request
,
147 IN EFI_ASYNC_USB_TRANSFER_CALLBACK Callback
,
155 Urb
= AllocateZeroPool (sizeof (URB
));
160 Urb
->Signature
= XHC_URB_SIG
;
161 InitializeListHead (&Urb
->UrbList
);
164 Ep
->BusAddr
= BusAddr
;
165 Ep
->EpAddr
= (UINT8
)(EpAddr
& 0x0F);
166 Ep
->Direction
= ((EpAddr
& 0x80) != 0) ? EfiUsbDataIn
: EfiUsbDataOut
;
167 Ep
->DevSpeed
= DevSpeed
;
168 Ep
->MaxPacket
= MaxPacket
;
171 Urb
->Request
= Request
;
173 Urb
->DataLen
= DataLen
;
174 Urb
->Callback
= Callback
;
175 Urb
->Context
= Context
;
177 Status
= XhcCreateTransferTrb (Xhc
, Urb
);
178 ASSERT_EFI_ERROR (Status
);
184 Free an allocated URB.
186 @param Xhc The XHCI device.
187 @param Urb The URB to free.
192 IN USB_XHCI_INSTANCE
*Xhc
,
196 if ((Xhc
== NULL
) || (Urb
== NULL
)) {
200 if (Urb
->DataMap
!= NULL
) {
201 Xhc
->PciIo
->Unmap (Xhc
->PciIo
, Urb
->DataMap
);
208 Create a transfer TRB.
210 @param Xhc The XHCI Instance
211 @param Urb The urb used to construct the transfer TRB.
213 @return Created TRB or NULL
217 XhcCreateTransferTrb (
218 IN USB_XHCI_INSTANCE
*Xhc
,
223 TRANSFER_RING
*EPRing
;
231 EFI_PCI_IO_PROTOCOL_OPERATION MapOp
;
232 EFI_PHYSICAL_ADDRESS PhyAddr
;
236 SlotId
= XhcBusDevAddrToSlotId (Xhc
, Urb
->Ep
.BusAddr
);
238 return EFI_DEVICE_ERROR
;
241 Urb
->Finished
= FALSE
;
242 Urb
->StartDone
= FALSE
;
243 Urb
->EndDone
= FALSE
;
245 Urb
->Result
= EFI_USB_NOERROR
;
247 Dci
= XhcEndpointToDci (Urb
->Ep
.EpAddr
, (UINT8
)(Urb
->Ep
.Direction
));
249 EPRing
= (TRANSFER_RING
*)(UINTN
) Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1];
251 OutputContext
= Xhc
->UsbDevContext
[SlotId
].OutputContext
;
252 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
253 EPType
= (UINT8
) ((DEVICE_CONTEXT
*)OutputContext
)->EP
[Dci
-1].EPType
;
255 EPType
= (UINT8
) ((DEVICE_CONTEXT_64
*)OutputContext
)->EP
[Dci
-1].EPType
;
258 if (Urb
->Data
!= NULL
) {
259 if (((UINT8
) (Urb
->Ep
.Direction
)) == EfiUsbDataIn
) {
260 MapOp
= EfiPciIoOperationBusMasterWrite
;
262 MapOp
= EfiPciIoOperationBusMasterRead
;
266 Status
= Xhc
->PciIo
->Map (Xhc
->PciIo
, MapOp
, Urb
->Data
, &Len
, &PhyAddr
, &Map
);
268 if (EFI_ERROR (Status
) || (Len
!= Urb
->DataLen
)) {
269 DEBUG ((EFI_D_ERROR
, "XhcCreateTransferTrb: Fail to map Urb->Data.\n"));
270 return EFI_OUT_OF_RESOURCES
;
273 Urb
->DataPhy
= (VOID
*) ((UINTN
) PhyAddr
);
280 XhcSyncTrsRing (Xhc
, EPRing
);
281 Urb
->TrbStart
= EPRing
->RingEnqueue
;
283 case ED_CONTROL_BIDIR
:
285 // For control transfer, create SETUP_STAGE_TRB first.
287 TrbStart
= (TRB
*)(UINTN
)EPRing
->RingEnqueue
;
288 TrbStart
->TrbCtrSetup
.bmRequestType
= Urb
->Request
->RequestType
;
289 TrbStart
->TrbCtrSetup
.bRequest
= Urb
->Request
->Request
;
290 TrbStart
->TrbCtrSetup
.wValue
= Urb
->Request
->Value
;
291 TrbStart
->TrbCtrSetup
.wIndex
= Urb
->Request
->Index
;
292 TrbStart
->TrbCtrSetup
.wLength
= Urb
->Request
->Length
;
293 TrbStart
->TrbCtrSetup
.Lenth
= 8;
294 TrbStart
->TrbCtrSetup
.IntTarget
= 0;
295 TrbStart
->TrbCtrSetup
.IOC
= 1;
296 TrbStart
->TrbCtrSetup
.IDT
= 1;
297 TrbStart
->TrbCtrSetup
.Type
= TRB_TYPE_SETUP_STAGE
;
298 if (Urb
->Ep
.Direction
== EfiUsbDataIn
) {
299 TrbStart
->TrbCtrSetup
.TRT
= 3;
300 } else if (Urb
->Ep
.Direction
== EfiUsbDataOut
) {
301 TrbStart
->TrbCtrSetup
.TRT
= 2;
303 TrbStart
->TrbCtrSetup
.TRT
= 0;
306 // Update the cycle bit
308 TrbStart
->TrbCtrSetup
.CycleBit
= EPRing
->RingPCS
& BIT0
;
312 // For control transfer, create DATA_STAGE_TRB.
314 if (Urb
->DataLen
> 0) {
315 XhcSyncTrsRing (Xhc
, EPRing
);
316 TrbStart
= (TRB
*)(UINTN
)EPRing
->RingEnqueue
;
317 TrbStart
->TrbCtrData
.TRBPtrLo
= XHC_LOW_32BIT(Urb
->DataPhy
);
318 TrbStart
->TrbCtrData
.TRBPtrHi
= XHC_HIGH_32BIT(Urb
->DataPhy
);
319 TrbStart
->TrbCtrData
.Lenth
= (UINT32
) Urb
->DataLen
;
320 TrbStart
->TrbCtrData
.TDSize
= 0;
321 TrbStart
->TrbCtrData
.IntTarget
= 0;
322 TrbStart
->TrbCtrData
.ISP
= 1;
323 TrbStart
->TrbCtrData
.IOC
= 1;
324 TrbStart
->TrbCtrData
.IDT
= 0;
325 TrbStart
->TrbCtrData
.CH
= 0;
326 TrbStart
->TrbCtrData
.Type
= TRB_TYPE_DATA_STAGE
;
327 if (Urb
->Ep
.Direction
== EfiUsbDataIn
) {
328 TrbStart
->TrbCtrData
.DIR = 1;
329 } else if (Urb
->Ep
.Direction
== EfiUsbDataOut
) {
330 TrbStart
->TrbCtrData
.DIR = 0;
332 TrbStart
->TrbCtrData
.DIR = 0;
335 // Update the cycle bit
337 TrbStart
->TrbCtrData
.CycleBit
= EPRing
->RingPCS
& BIT0
;
341 // For control transfer, create STATUS_STAGE_TRB.
342 // Get the pointer to next TRB for status stage use
344 XhcSyncTrsRing (Xhc
, EPRing
);
345 TrbStart
= (TRB
*)(UINTN
)EPRing
->RingEnqueue
;
346 TrbStart
->TrbCtrStatus
.IntTarget
= 0;
347 TrbStart
->TrbCtrStatus
.IOC
= 1;
348 TrbStart
->TrbCtrStatus
.CH
= 0;
349 TrbStart
->TrbCtrStatus
.Type
= TRB_TYPE_STATUS_STAGE
;
350 if (Urb
->Ep
.Direction
== EfiUsbDataIn
) {
351 TrbStart
->TrbCtrStatus
.DIR = 0;
352 } else if (Urb
->Ep
.Direction
== EfiUsbDataOut
) {
353 TrbStart
->TrbCtrStatus
.DIR = 1;
355 TrbStart
->TrbCtrStatus
.DIR = 0;
358 // Update the cycle bit
360 TrbStart
->TrbCtrStatus
.CycleBit
= EPRing
->RingPCS
& BIT0
;
362 // Update the enqueue pointer
364 XhcSyncTrsRing (Xhc
, EPRing
);
366 Urb
->TrbEnd
= (TRB_TEMPLATE
*)(UINTN
)TrbStart
;
375 TrbStart
= (TRB
*)(UINTN
)EPRing
->RingEnqueue
;
376 while (TotalLen
< Urb
->DataLen
) {
377 if ((TotalLen
+ 0x10000) >= Urb
->DataLen
) {
378 Len
= Urb
->DataLen
- TotalLen
;
382 TrbStart
= (TRB
*)(UINTN
)EPRing
->RingEnqueue
;
383 TrbStart
->TrbNormal
.TRBPtrLo
= XHC_LOW_32BIT((UINT8
*) Urb
->DataPhy
+ TotalLen
);
384 TrbStart
->TrbNormal
.TRBPtrHi
= XHC_HIGH_32BIT((UINT8
*) Urb
->DataPhy
+ TotalLen
);
385 TrbStart
->TrbNormal
.Lenth
= (UINT32
) Len
;
386 TrbStart
->TrbNormal
.TDSize
= 0;
387 TrbStart
->TrbNormal
.IntTarget
= 0;
388 TrbStart
->TrbNormal
.ISP
= 1;
389 TrbStart
->TrbNormal
.IOC
= 1;
390 TrbStart
->TrbNormal
.Type
= TRB_TYPE_NORMAL
;
392 // Update the cycle bit
394 TrbStart
->TrbNormal
.CycleBit
= EPRing
->RingPCS
& BIT0
;
396 XhcSyncTrsRing (Xhc
, EPRing
);
401 Urb
->TrbNum
= TrbNum
;
402 Urb
->TrbEnd
= (TRB_TEMPLATE
*)(UINTN
)TrbStart
;
405 case ED_INTERRUPT_OUT
:
406 case ED_INTERRUPT_IN
:
410 TrbStart
= (TRB
*)(UINTN
)EPRing
->RingEnqueue
;
411 while (TotalLen
< Urb
->DataLen
) {
412 if ((TotalLen
+ 0x10000) >= Urb
->DataLen
) {
413 Len
= Urb
->DataLen
- TotalLen
;
417 TrbStart
= (TRB
*)(UINTN
)EPRing
->RingEnqueue
;
418 TrbStart
->TrbNormal
.TRBPtrLo
= XHC_LOW_32BIT((UINT8
*) Urb
->DataPhy
+ TotalLen
);
419 TrbStart
->TrbNormal
.TRBPtrHi
= XHC_HIGH_32BIT((UINT8
*) Urb
->DataPhy
+ TotalLen
);
420 TrbStart
->TrbNormal
.Lenth
= (UINT32
) Len
;
421 TrbStart
->TrbNormal
.TDSize
= 0;
422 TrbStart
->TrbNormal
.IntTarget
= 0;
423 TrbStart
->TrbNormal
.ISP
= 1;
424 TrbStart
->TrbNormal
.IOC
= 1;
425 TrbStart
->TrbNormal
.Type
= TRB_TYPE_NORMAL
;
427 // Update the cycle bit
429 TrbStart
->TrbNormal
.CycleBit
= EPRing
->RingPCS
& BIT0
;
431 XhcSyncTrsRing (Xhc
, EPRing
);
436 Urb
->TrbNum
= TrbNum
;
437 Urb
->TrbEnd
= (TRB_TEMPLATE
*)(UINTN
)TrbStart
;
441 DEBUG ((EFI_D_INFO
, "Not supported EPType 0x%x!\n",EPType
));
451 Initialize the XHCI host controller for schedule.
453 @param Xhc The XHCI Instance to be initialized.
458 IN USB_XHCI_INSTANCE
*Xhc
462 EFI_PHYSICAL_ADDRESS DcbaaPhy
;
464 EFI_PHYSICAL_ADDRESS CmdRingPhy
;
466 UINT32 MaxScratchpadBufs
;
468 EFI_PHYSICAL_ADDRESS ScratchPhy
;
469 UINT64
*ScratchEntry
;
470 EFI_PHYSICAL_ADDRESS ScratchEntryPhy
;
472 UINTN
*ScratchEntryMap
;
476 // Initialize memory management.
478 Xhc
->MemPool
= UsbHcInitMemPool (Xhc
->PciIo
);
479 ASSERT (Xhc
->MemPool
!= NULL
);
482 // Program the Max Device Slots Enabled (MaxSlotsEn) field in the CONFIG register (5.4.7)
483 // to enable the device slots that system software is going to use.
485 Xhc
->MaxSlotsEn
= Xhc
->HcSParams1
.Data
.MaxSlots
;
486 ASSERT (Xhc
->MaxSlotsEn
>= 1 && Xhc
->MaxSlotsEn
<= 255);
487 XhcWriteOpReg (Xhc
, XHC_CONFIG_OFFSET
, Xhc
->MaxSlotsEn
);
490 // The Device Context Base Address Array entry associated with each allocated Device Slot
491 // shall contain a 64-bit pointer to the base of the associated Device Context.
492 // The Device Context Base Address Array shall contain MaxSlotsEn + 1 entries.
493 // Software shall set Device Context Base Address Array entries for unallocated Device Slots to '0'.
495 Entries
= (Xhc
->MaxSlotsEn
+ 1) * sizeof(UINT64
);
496 Dcbaa
= UsbHcAllocateMem (Xhc
->MemPool
, Entries
);
497 ASSERT (Dcbaa
!= NULL
);
498 ZeroMem (Dcbaa
, Entries
);
501 // A Scratchpad Buffer is a PAGESIZE block of system memory located on a PAGESIZE boundary.
502 // System software shall allocate the Scratchpad Buffer(s) before placing the xHC in to Run
503 // mode (Run/Stop(R/S) ='1').
505 MaxScratchpadBufs
= ((Xhc
->HcSParams2
.Data
.ScratchBufHi
) << 5) | (Xhc
->HcSParams2
.Data
.ScratchBufLo
);
506 Xhc
->MaxScratchpadBufs
= MaxScratchpadBufs
;
507 ASSERT (MaxScratchpadBufs
<= 1023);
508 if (MaxScratchpadBufs
!= 0) {
510 // Allocate the buffer to record the Mapping for each scratch buffer in order to Unmap them
512 ScratchEntryMap
= AllocateZeroPool (sizeof (UINTN
) * MaxScratchpadBufs
);
513 ASSERT (ScratchEntryMap
!= NULL
);
514 Xhc
->ScratchEntryMap
= ScratchEntryMap
;
517 // Allocate the buffer to record the host address for each entry
519 ScratchEntry
= AllocateZeroPool (sizeof (UINT64
) * MaxScratchpadBufs
);
520 ASSERT (ScratchEntry
!= NULL
);
521 Xhc
->ScratchEntry
= ScratchEntry
;
523 Status
= UsbHcAllocateAlignedPages (
525 EFI_SIZE_TO_PAGES (MaxScratchpadBufs
* sizeof (UINT64
)),
527 (VOID
**) &ScratchBuf
,
531 ASSERT_EFI_ERROR (Status
);
533 ZeroMem (ScratchBuf
, MaxScratchpadBufs
* sizeof (UINT64
));
534 Xhc
->ScratchBuf
= ScratchBuf
;
537 // Allocate each scratch buffer
539 for (Index
= 0; Index
< MaxScratchpadBufs
; Index
++) {
540 Status
= UsbHcAllocateAlignedPages (
542 EFI_SIZE_TO_PAGES (Xhc
->PageSize
),
544 (VOID
**) &ScratchEntry
[Index
],
546 (VOID
**) &ScratchEntryMap
[Index
]
548 ASSERT_EFI_ERROR (Status
);
549 ZeroMem ((VOID
*)(UINTN
)ScratchEntry
[Index
], Xhc
->PageSize
);
551 // Fill with the PCI device address
553 *ScratchBuf
++ = ScratchEntryPhy
;
556 // The Scratchpad Buffer Array contains pointers to the Scratchpad Buffers. Entry 0 of the
557 // Device Context Base Address Array points to the Scratchpad Buffer Array.
559 *(UINT64
*)Dcbaa
= (UINT64
)(UINTN
) ScratchPhy
;
563 // Program the Device Context Base Address Array Pointer (DCBAAP) register (5.4.6) with
564 // a 64-bit address pointing to where the Device Context Base Address Array is located.
566 Xhc
->DCBAA
= (UINT64
*)(UINTN
)Dcbaa
;
568 // Some 3rd party XHCI external cards don't support single 64-bytes width register access,
569 // So divide it to two 32-bytes width register access.
571 DcbaaPhy
= UsbHcGetPciAddrForHostAddr (Xhc
->MemPool
, Dcbaa
, Entries
);
572 XhcWriteOpReg (Xhc
, XHC_DCBAAP_OFFSET
, XHC_LOW_32BIT(DcbaaPhy
));
573 XhcWriteOpReg (Xhc
, XHC_DCBAAP_OFFSET
+ 4, XHC_HIGH_32BIT (DcbaaPhy
));
575 DEBUG ((EFI_D_INFO
, "XhcInitSched:DCBAA=0x%x\n", (UINT64
)(UINTN
)Xhc
->DCBAA
));
578 // Define the Command Ring Dequeue Pointer by programming the Command Ring Control Register
579 // (5.4.5) with a 64-bit address pointing to the starting address of the first TRB of the Command Ring.
580 // Note: The Command Ring is 64 byte aligned, so the low order 6 bits of the Command Ring Pointer shall
583 CreateTransferRing (Xhc
, CMD_RING_TRB_NUMBER
, &Xhc
->CmdRing
);
585 // The xHC uses the Enqueue Pointer to determine when a Transfer Ring is empty. As it fetches TRBs from a
586 // Transfer Ring it checks for a Cycle bit transition. If a transition detected, the ring is empty.
587 // So we set RCS as inverted PCS init value to let Command Ring empty
589 CmdRing
= (UINT64
)(UINTN
)Xhc
->CmdRing
.RingSeg0
;
590 CmdRingPhy
= UsbHcGetPciAddrForHostAddr (Xhc
->MemPool
, (VOID
*)(UINTN
) CmdRing
, sizeof (TRB_TEMPLATE
) * CMD_RING_TRB_NUMBER
);
591 ASSERT ((CmdRingPhy
& 0x3F) == 0);
592 CmdRingPhy
|= XHC_CRCR_RCS
;
594 // Some 3rd party XHCI external cards don't support single 64-bytes width register access,
595 // So divide it to two 32-bytes width register access.
597 XhcWriteOpReg (Xhc
, XHC_CRCR_OFFSET
, XHC_LOW_32BIT(CmdRingPhy
));
598 XhcWriteOpReg (Xhc
, XHC_CRCR_OFFSET
+ 4, XHC_HIGH_32BIT (CmdRingPhy
));
600 DEBUG ((EFI_D_INFO
, "XhcInitSched:XHC_CRCR=0x%x\n", Xhc
->CmdRing
.RingSeg0
));
603 // Disable the 'interrupter enable' bit in USB_CMD
604 // and clear IE & IP bit in all Interrupter X Management Registers.
606 XhcClearOpRegBit (Xhc
, XHC_USBCMD_OFFSET
, XHC_USBCMD_INTE
);
607 for (Index
= 0; Index
< (UINT16
)(Xhc
->HcSParams1
.Data
.MaxIntrs
); Index
++) {
608 XhcClearRuntimeRegBit (Xhc
, XHC_IMAN_OFFSET
+ (Index
* 32), XHC_IMAN_IE
);
609 XhcSetRuntimeRegBit (Xhc
, XHC_IMAN_OFFSET
+ (Index
* 32), XHC_IMAN_IP
);
613 // Allocate EventRing for Cmd, Ctrl, Bulk, Interrupt, AsynInterrupt transfer
615 CreateEventRing (Xhc
, &Xhc
->EventRing
);
616 DEBUG ((EFI_D_INFO
, "XhcInitSched:XHC_EVENTRING=0x%x\n", Xhc
->EventRing
.EventRingSeg0
));
620 System software shall use a Reset Endpoint Command (section 4.11.4.7) to remove the Halted
621 condition in the xHC. After the successful completion of the Reset Endpoint Command, the Endpoint
622 Context is transitioned from the Halted to the Stopped state and the Transfer Ring of the endpoint is
623 reenabled. The next write to the Doorbell of the Endpoint will transition the Endpoint Context from the
624 Stopped to the Running state.
626 @param Xhc The XHCI Instance.
627 @param Urb The urb which makes the endpoint halted.
629 @retval EFI_SUCCESS The recovery is successful.
630 @retval Others Failed to recovery halted endpoint.
635 XhcRecoverHaltedEndpoint (
636 IN USB_XHCI_INSTANCE
*Xhc
,
641 EVT_TRB_COMMAND_COMPLETION
*EvtTrb
;
642 CMD_TRB_RESET_ENDPOINT CmdTrbResetED
;
643 CMD_SET_TR_DEQ_POINTER CmdSetTRDeq
;
646 EFI_PHYSICAL_ADDRESS PhyAddr
;
648 Status
= EFI_SUCCESS
;
649 SlotId
= XhcBusDevAddrToSlotId (Xhc
, Urb
->Ep
.BusAddr
);
651 return EFI_DEVICE_ERROR
;
653 Dci
= XhcEndpointToDci (Urb
->Ep
.EpAddr
, (UINT8
)(Urb
->Ep
.Direction
));
656 DEBUG ((EFI_D_INFO
, "Recovery Halted Slot = %x,Dci = %x\n", SlotId
, Dci
));
659 // 1) Send Reset endpoint command to transit from halt to stop state
661 ZeroMem (&CmdTrbResetED
, sizeof (CmdTrbResetED
));
662 CmdTrbResetED
.CycleBit
= 1;
663 CmdTrbResetED
.Type
= TRB_TYPE_RESET_ENDPOINT
;
664 CmdTrbResetED
.EDID
= Dci
;
665 CmdTrbResetED
.SlotId
= SlotId
;
666 Status
= XhcCmdTransfer (
668 (TRB_TEMPLATE
*) (UINTN
) &CmdTrbResetED
,
670 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
672 ASSERT (!EFI_ERROR(Status
));
675 // 2)Set dequeue pointer
677 ZeroMem (&CmdSetTRDeq
, sizeof (CmdSetTRDeq
));
678 PhyAddr
= UsbHcGetPciAddrForHostAddr (Xhc
->MemPool
, Urb
->Ring
->RingEnqueue
, sizeof (CMD_SET_TR_DEQ_POINTER
));
679 CmdSetTRDeq
.PtrLo
= XHC_LOW_32BIT (PhyAddr
) | Urb
->Ring
->RingPCS
;
680 CmdSetTRDeq
.PtrHi
= XHC_HIGH_32BIT (PhyAddr
);
681 CmdSetTRDeq
.CycleBit
= 1;
682 CmdSetTRDeq
.Type
= TRB_TYPE_SET_TR_DEQUE
;
683 CmdSetTRDeq
.Endpoint
= Dci
;
684 CmdSetTRDeq
.SlotId
= SlotId
;
685 Status
= XhcCmdTransfer (
687 (TRB_TEMPLATE
*) (UINTN
) &CmdSetTRDeq
,
689 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
691 ASSERT (!EFI_ERROR(Status
));
694 // 3)Ring the doorbell to transit from stop to active
696 XhcRingDoorBell (Xhc
, SlotId
, Dci
);
702 Create XHCI event ring.
704 @param Xhc The XHCI Instance.
705 @param EventRing The created event ring.
710 IN USB_XHCI_INSTANCE
*Xhc
,
711 OUT EVENT_RING
*EventRing
715 EVENT_RING_SEG_TABLE_ENTRY
*ERSTBase
;
717 EFI_PHYSICAL_ADDRESS ERSTPhy
;
718 EFI_PHYSICAL_ADDRESS DequeuePhy
;
720 ASSERT (EventRing
!= NULL
);
722 Size
= sizeof (TRB_TEMPLATE
) * EVENT_RING_TRB_NUMBER
;
723 Buf
= UsbHcAllocateMem (Xhc
->MemPool
, Size
);
724 ASSERT (Buf
!= NULL
);
725 ASSERT (((UINTN
) Buf
& 0x3F) == 0);
728 EventRing
->EventRingSeg0
= Buf
;
729 EventRing
->TrbNumber
= EVENT_RING_TRB_NUMBER
;
730 EventRing
->EventRingDequeue
= (TRB_TEMPLATE
*) EventRing
->EventRingSeg0
;
731 EventRing
->EventRingEnqueue
= (TRB_TEMPLATE
*) EventRing
->EventRingSeg0
;
733 DequeuePhy
= UsbHcGetPciAddrForHostAddr (Xhc
->MemPool
, Buf
, Size
);
736 // Software maintains an Event Ring Consumer Cycle State (CCS) bit, initializing it to '1'
737 // and toggling it every time the Event Ring Dequeue Pointer wraps back to the beginning of the Event Ring.
739 EventRing
->EventRingCCS
= 1;
741 Size
= EFI_SIZE_TO_PAGES (sizeof (EVENT_RING_SEG_TABLE_ENTRY
) * ERST_NUMBER
);
742 Buf
= UsbHcAllocateMem (Xhc
->MemPool
, Size
);
743 ASSERT (Buf
!= NULL
);
744 ASSERT (((UINTN
) Buf
& 0x3F) == 0);
747 ERSTBase
= (EVENT_RING_SEG_TABLE_ENTRY
*) Buf
;
748 EventRing
->ERSTBase
= ERSTBase
;
749 ERSTBase
->PtrLo
= XHC_LOW_32BIT (DequeuePhy
);
750 ERSTBase
->PtrHi
= XHC_HIGH_32BIT (DequeuePhy
);
751 ERSTBase
->RingTrbSize
= EVENT_RING_TRB_NUMBER
;
753 ERSTPhy
= UsbHcGetPciAddrForHostAddr (Xhc
->MemPool
, ERSTBase
, Size
);
756 // Program the Interrupter Event Ring Segment Table Size (ERSTSZ) register (5.5.2.3.1)
764 // Program the Interrupter Event Ring Dequeue Pointer (ERDP) register (5.5.2.3.3)
766 // Some 3rd party XHCI external cards don't support single 64-bytes width register access,
767 // So divide it to two 32-bytes width register access.
772 XHC_LOW_32BIT((UINT64
)(UINTN
)DequeuePhy
)
777 XHC_HIGH_32BIT((UINT64
)(UINTN
)DequeuePhy
)
780 // Program the Interrupter Event Ring Segment Table Base Address (ERSTBA) register(5.5.2.3.2)
782 // Some 3rd party XHCI external cards don't support single 64-bytes width register access,
783 // So divide it to two 32-bytes width register access.
788 XHC_LOW_32BIT((UINT64
)(UINTN
)ERSTPhy
)
792 XHC_ERSTBA_OFFSET
+ 4,
793 XHC_HIGH_32BIT((UINT64
)(UINTN
)ERSTPhy
)
796 // Need set IMAN IE bit to enble the ring interrupt
798 XhcSetRuntimeRegBit (Xhc
, XHC_IMAN_OFFSET
, XHC_IMAN_IE
);
802 Create XHCI transfer ring.
804 @param Xhc The XHCI Instance.
805 @param TrbNum The number of TRB in the ring.
806 @param TransferRing The created transfer ring.
811 IN USB_XHCI_INSTANCE
*Xhc
,
813 OUT TRANSFER_RING
*TransferRing
818 EFI_PHYSICAL_ADDRESS PhyAddr
;
820 Buf
= UsbHcAllocateMem (Xhc
->MemPool
, sizeof (TRB_TEMPLATE
) * TrbNum
);
821 ASSERT (Buf
!= NULL
);
822 ASSERT (((UINTN
) Buf
& 0x3F) == 0);
823 ZeroMem (Buf
, sizeof (TRB_TEMPLATE
) * TrbNum
);
825 TransferRing
->RingSeg0
= Buf
;
826 TransferRing
->TrbNumber
= TrbNum
;
827 TransferRing
->RingEnqueue
= (TRB_TEMPLATE
*) TransferRing
->RingSeg0
;
828 TransferRing
->RingDequeue
= (TRB_TEMPLATE
*) TransferRing
->RingSeg0
;
829 TransferRing
->RingPCS
= 1;
831 // 4.9.2 Transfer Ring Management
832 // To form a ring (or circular queue) a Link TRB may be inserted at the end of a ring to
833 // point to the first TRB in the ring.
835 EndTrb
= (LINK_TRB
*) ((UINTN
)Buf
+ sizeof (TRB_TEMPLATE
) * (TrbNum
- 1));
836 EndTrb
->Type
= TRB_TYPE_LINK
;
837 PhyAddr
= UsbHcGetPciAddrForHostAddr (Xhc
->MemPool
, Buf
, sizeof (TRB_TEMPLATE
) * TrbNum
);
838 EndTrb
->PtrLo
= XHC_LOW_32BIT (PhyAddr
);
839 EndTrb
->PtrHi
= XHC_HIGH_32BIT (PhyAddr
);
841 // Toggle Cycle (TC). When set to '1', the xHC shall toggle its interpretation of the Cycle bit.
845 // Set Cycle bit as other TRB PCS init value
847 EndTrb
->CycleBit
= 0;
851 Free XHCI event ring.
853 @param Xhc The XHCI Instance.
854 @param EventRing The event ring to be freed.
860 IN USB_XHCI_INSTANCE
*Xhc
,
861 IN EVENT_RING
*EventRing
864 if(EventRing
->EventRingSeg0
== NULL
) {
869 // Free EventRing Segment 0
871 UsbHcFreeMem (Xhc
->MemPool
, EventRing
->EventRingSeg0
, sizeof (TRB_TEMPLATE
) * EVENT_RING_TRB_NUMBER
);
876 UsbHcFreeMem (Xhc
->MemPool
, EventRing
->ERSTBase
, sizeof (EVENT_RING_SEG_TABLE_ENTRY
) * ERST_NUMBER
);
881 Free the resouce allocated at initializing schedule.
883 @param Xhc The XHCI Instance.
888 IN USB_XHCI_INSTANCE
*Xhc
892 UINT64
*ScratchEntry
;
894 if (Xhc
->ScratchBuf
!= NULL
) {
895 ScratchEntry
= Xhc
->ScratchEntry
;
896 for (Index
= 0; Index
< Xhc
->MaxScratchpadBufs
; Index
++) {
898 // Free Scratchpad Buffers
900 UsbHcFreeAlignedPages (Xhc
->PciIo
, (VOID
*)(UINTN
)ScratchEntry
[Index
], EFI_SIZE_TO_PAGES (Xhc
->PageSize
), (VOID
*) Xhc
->ScratchEntryMap
[Index
]);
903 // Free Scratchpad Buffer Array
905 UsbHcFreeAlignedPages (Xhc
->PciIo
, Xhc
->ScratchBuf
, EFI_SIZE_TO_PAGES (Xhc
->MaxScratchpadBufs
* sizeof (UINT64
)), Xhc
->ScratchMap
);
906 FreePool (Xhc
->ScratchEntryMap
);
907 FreePool (Xhc
->ScratchEntry
);
910 if (Xhc
->CmdRing
.RingSeg0
!= NULL
) {
911 UsbHcFreeMem (Xhc
->MemPool
, Xhc
->CmdRing
.RingSeg0
, sizeof (TRB_TEMPLATE
) * CMD_RING_TRB_NUMBER
);
912 Xhc
->CmdRing
.RingSeg0
= NULL
;
915 XhcFreeEventRing (Xhc
,&Xhc
->EventRing
);
917 if (Xhc
->DCBAA
!= NULL
) {
918 UsbHcFreeMem (Xhc
->MemPool
, Xhc
->DCBAA
, (Xhc
->MaxSlotsEn
+ 1) * sizeof(UINT64
));
923 // Free memory pool at last
925 if (Xhc
->MemPool
!= NULL
) {
926 UsbHcFreeMemPool (Xhc
->MemPool
);
932 Check if the Trb is a transaction of the URBs in XHCI's asynchronous transfer list.
934 @param Xhc The XHCI Instance.
935 @param Trb The TRB to be checked.
936 @param Urb The pointer to the matched Urb.
938 @retval TRUE The Trb is matched with a transaction of the URBs in the async list.
939 @retval FALSE The Trb is not matched with any URBs in the async list.
944 IN USB_XHCI_INSTANCE
*Xhc
,
945 IN TRB_TEMPLATE
*Trb
,
951 TRB_TEMPLATE
*CheckedTrb
;
955 EFI_LIST_FOR_EACH_SAFE (Entry
, Next
, &Xhc
->AsyncIntTransfers
) {
956 CheckedUrb
= EFI_LIST_CONTAINER (Entry
, URB
, UrbList
);
957 CheckedTrb
= CheckedUrb
->TrbStart
;
958 for (Index
= 0; Index
< CheckedUrb
->TrbNum
; Index
++) {
959 if (Trb
== CheckedTrb
) {
964 if ((UINTN
)CheckedTrb
>= ((UINTN
) CheckedUrb
->Ring
->RingSeg0
+ sizeof (TRB_TEMPLATE
) * CheckedUrb
->Ring
->TrbNumber
)) {
965 CheckedTrb
= (TRB_TEMPLATE
*) CheckedUrb
->Ring
->RingSeg0
;
974 Check if the Trb is a transaction of the URB.
976 @param Trb The TRB to be checked
977 @param Urb The transfer ring to be checked.
979 @retval TRUE It is a transaction of the URB.
980 @retval FALSE It is not any transaction of the URB.
985 IN TRB_TEMPLATE
*Trb
,
989 TRB_TEMPLATE
*CheckedTrb
;
992 CheckedTrb
= Urb
->Ring
->RingSeg0
;
994 ASSERT (Urb
->Ring
->TrbNumber
== CMD_RING_TRB_NUMBER
|| Urb
->Ring
->TrbNumber
== TR_RING_TRB_NUMBER
);
996 for (Index
= 0; Index
< Urb
->Ring
->TrbNumber
; Index
++) {
997 if (Trb
== CheckedTrb
) {
1007 Check the URB's execution result and update the URB's
1010 @param Xhc The XHCI Instance.
1011 @param Urb The URB to check result.
1013 @return Whether the result of URB transfer is finialized.
1018 IN USB_XHCI_INSTANCE
*Xhc
,
1022 EVT_TRB_TRANSFER
*EvtTrb
;
1023 TRB_TEMPLATE
*TRBPtr
;
1032 EFI_PHYSICAL_ADDRESS PhyAddr
;
1034 ASSERT ((Xhc
!= NULL
) && (Urb
!= NULL
));
1036 Status
= EFI_SUCCESS
;
1039 if (Urb
->Finished
) {
1045 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
1046 Urb
->Result
|= EFI_USB_ERR_SYSTEM
;
1047 Status
= EFI_DEVICE_ERROR
;
1052 // Traverse the event ring to find out all new events from the previous check.
1054 XhcSyncEventRing (Xhc
, &Xhc
->EventRing
);
1055 for (Index
= 0; Index
< Xhc
->EventRing
.TrbNumber
; Index
++) {
1056 Status
= XhcCheckNewEvent (Xhc
, &Xhc
->EventRing
, ((TRB_TEMPLATE
**)&EvtTrb
));
1057 if (Status
== EFI_NOT_READY
) {
1059 // All new events are handled, return directly.
1065 // Only handle COMMAND_COMPLETETION_EVENT and TRANSFER_EVENT.
1067 if ((EvtTrb
->Type
!= TRB_TYPE_COMMAND_COMPLT_EVENT
) && (EvtTrb
->Type
!= TRB_TYPE_TRANS_EVENT
)) {
1072 // Need convert pci device address to host address
1074 PhyAddr
= (EFI_PHYSICAL_ADDRESS
)(EvtTrb
->TRBPtrLo
| LShiftU64 ((UINT64
) EvtTrb
->TRBPtrHi
, 32));
1075 TRBPtr
= (TRB_TEMPLATE
*)(UINTN
) UsbHcGetHostAddrForPciAddr (Xhc
->MemPool
, (VOID
*)(UINTN
) PhyAddr
, sizeof (TRB_TEMPLATE
));
1078 // Update the status of Urb according to the finished event regardless of whether
1079 // the urb is current checked one or in the XHCI's async transfer list.
1080 // This way is used to avoid that those completed async transfer events don't get
1081 // handled in time and are flushed by newer coming events.
1083 if (IsTransferRingTrb (TRBPtr
, Urb
)) {
1085 } else if (IsAsyncIntTrb (Xhc
, TRBPtr
, &AsyncUrb
)) {
1086 CheckedUrb
= AsyncUrb
;
1091 switch (EvtTrb
->Completecode
) {
1092 case TRB_COMPLETION_STALL_ERROR
:
1093 CheckedUrb
->Result
|= EFI_USB_ERR_STALL
;
1094 CheckedUrb
->Finished
= TRUE
;
1095 DEBUG ((EFI_D_ERROR
, "XhcCheckUrbResult: STALL_ERROR! Completecode = %x\n",EvtTrb
->Completecode
));
1098 case TRB_COMPLETION_BABBLE_ERROR
:
1099 CheckedUrb
->Result
|= EFI_USB_ERR_BABBLE
;
1100 CheckedUrb
->Finished
= TRUE
;
1101 DEBUG ((EFI_D_ERROR
, "XhcCheckUrbResult: BABBLE_ERROR! Completecode = %x\n",EvtTrb
->Completecode
));
1104 case TRB_COMPLETION_DATA_BUFFER_ERROR
:
1105 CheckedUrb
->Result
|= EFI_USB_ERR_BUFFER
;
1106 CheckedUrb
->Finished
= TRUE
;
1107 DEBUG ((EFI_D_ERROR
, "XhcCheckUrbResult: ERR_BUFFER! Completecode = %x\n",EvtTrb
->Completecode
));
1110 case TRB_COMPLETION_USB_TRANSACTION_ERROR
:
1111 CheckedUrb
->Result
|= EFI_USB_ERR_TIMEOUT
;
1112 CheckedUrb
->Finished
= TRUE
;
1113 DEBUG ((EFI_D_ERROR
, "XhcCheckUrbResult: TRANSACTION_ERROR! Completecode = %x\n",EvtTrb
->Completecode
));
1116 case TRB_COMPLETION_SHORT_PACKET
:
1117 case TRB_COMPLETION_SUCCESS
:
1118 if (EvtTrb
->Completecode
== TRB_COMPLETION_SHORT_PACKET
) {
1119 DEBUG ((EFI_D_ERROR
, "XhcCheckUrbResult: short packet happens!\n"));
1122 TRBType
= (UINT8
) (TRBPtr
->Type
);
1123 if ((TRBType
== TRB_TYPE_DATA_STAGE
) ||
1124 (TRBType
== TRB_TYPE_NORMAL
) ||
1125 (TRBType
== TRB_TYPE_ISOCH
)) {
1126 CheckedUrb
->Completed
+= (CheckedUrb
->DataLen
- EvtTrb
->Lenth
);
1132 DEBUG ((EFI_D_ERROR
, "Transfer Default Error Occur! Completecode = 0x%x!\n",EvtTrb
->Completecode
));
1133 CheckedUrb
->Result
|= EFI_USB_ERR_TIMEOUT
;
1134 CheckedUrb
->Finished
= TRUE
;
1139 // Only check first and end Trb event address
1141 if (TRBPtr
== CheckedUrb
->TrbStart
) {
1142 CheckedUrb
->StartDone
= TRUE
;
1145 if (TRBPtr
== CheckedUrb
->TrbEnd
) {
1146 CheckedUrb
->EndDone
= TRUE
;
1149 if (CheckedUrb
->StartDone
&& CheckedUrb
->EndDone
) {
1150 CheckedUrb
->Finished
= TRUE
;
1151 CheckedUrb
->EvtTrb
= (TRB_TEMPLATE
*)EvtTrb
;
1158 // Advance event ring to last available entry
1160 // Some 3rd party XHCI external cards don't support single 64-bytes width register access,
1161 // So divide it to two 32-bytes width register access.
1163 Low
= XhcReadRuntimeReg (Xhc
, XHC_ERDP_OFFSET
);
1164 High
= XhcReadRuntimeReg (Xhc
, XHC_ERDP_OFFSET
+ 4);
1165 XhcDequeue
= (UINT64
)(LShiftU64((UINT64
)High
, 32) | Low
);
1167 PhyAddr
= UsbHcGetPciAddrForHostAddr (Xhc
->MemPool
, Xhc
->EventRing
.EventRingDequeue
, sizeof (TRB_TEMPLATE
));
1169 if ((XhcDequeue
& (~0x0F)) != (PhyAddr
& (~0x0F))) {
1171 // Some 3rd party XHCI external cards don't support single 64-bytes width register access,
1172 // So divide it to two 32-bytes width register access.
1174 XhcWriteRuntimeReg (Xhc
, XHC_ERDP_OFFSET
, XHC_LOW_32BIT (PhyAddr
) | BIT3
);
1175 XhcWriteRuntimeReg (Xhc
, XHC_ERDP_OFFSET
+ 4, XHC_HIGH_32BIT (PhyAddr
));
1183 Execute the transfer by polling the URB. This is a synchronous operation.
1185 @param Xhc The XHCI Instance.
1186 @param CmdTransfer The executed URB is for cmd transfer or not.
1187 @param Urb The URB to execute.
1188 @param Timeout The time to wait before abort, in millisecond.
1190 @return EFI_DEVICE_ERROR The transfer failed due to transfer error.
1191 @return EFI_TIMEOUT The transfer failed due to time out.
1192 @return EFI_SUCCESS The transfer finished OK.
1197 IN USB_XHCI_INSTANCE
*Xhc
,
1198 IN BOOLEAN CmdTransfer
,
1213 SlotId
= XhcBusDevAddrToSlotId (Xhc
, Urb
->Ep
.BusAddr
);
1215 return EFI_DEVICE_ERROR
;
1217 Dci
= XhcEndpointToDci (Urb
->Ep
.EpAddr
, (UINT8
)(Urb
->Ep
.Direction
));
1221 Status
= EFI_SUCCESS
;
1222 Loop
= Timeout
* XHC_1_MILLISECOND
;
1227 XhcRingDoorBell (Xhc
, SlotId
, Dci
);
1229 for (Index
= 0; Index
< Loop
; Index
++) {
1230 Status
= XhcCheckUrbResult (Xhc
, Urb
);
1231 if (Urb
->Finished
) {
1234 gBS
->Stall (XHC_1_MICROSECOND
);
1237 if (Index
== Loop
) {
1238 Urb
->Result
= EFI_USB_ERR_TIMEOUT
;
1245 Delete a single asynchronous interrupt transfer for
1246 the device and endpoint.
1248 @param Xhc The XHCI Instance.
1249 @param BusAddr The logical device address assigned by UsbBus driver.
1250 @param EpNum The endpoint of the target.
1252 @retval EFI_SUCCESS An asynchronous transfer is removed.
1253 @retval EFI_NOT_FOUND No transfer for the device is found.
1257 XhciDelAsyncIntTransfer (
1258 IN USB_XHCI_INSTANCE
*Xhc
,
1266 EFI_USB_DATA_DIRECTION Direction
;
1268 Direction
= ((EpNum
& 0x80) != 0) ? EfiUsbDataIn
: EfiUsbDataOut
;
1273 EFI_LIST_FOR_EACH_SAFE (Entry
, Next
, &Xhc
->AsyncIntTransfers
) {
1274 Urb
= EFI_LIST_CONTAINER (Entry
, URB
, UrbList
);
1275 if ((Urb
->Ep
.BusAddr
== BusAddr
) &&
1276 (Urb
->Ep
.EpAddr
== EpNum
) &&
1277 (Urb
->Ep
.Direction
== Direction
)) {
1278 RemoveEntryList (&Urb
->UrbList
);
1279 FreePool (Urb
->Data
);
1280 XhcFreeUrb (Xhc
, Urb
);
1285 return EFI_NOT_FOUND
;
1289 Remove all the asynchronous interrutp transfers.
1291 @param Xhc The XHCI Instance.
1295 XhciDelAllAsyncIntTransfers (
1296 IN USB_XHCI_INSTANCE
*Xhc
1303 EFI_LIST_FOR_EACH_SAFE (Entry
, Next
, &Xhc
->AsyncIntTransfers
) {
1304 Urb
= EFI_LIST_CONTAINER (Entry
, URB
, UrbList
);
1305 RemoveEntryList (&Urb
->UrbList
);
1306 FreePool (Urb
->Data
);
1307 XhcFreeUrb (Xhc
, Urb
);
1312 Update the queue head for next round of asynchronous transfer
1314 @param Xhc The XHCI Instance.
1315 @param Urb The URB to update
1319 XhcUpdateAsyncRequest (
1320 IN USB_XHCI_INSTANCE
*Xhc
,
1326 if (Urb
->Result
== EFI_USB_NOERROR
) {
1327 Status
= XhcCreateTransferTrb (Xhc
, Urb
);
1328 if (EFI_ERROR (Status
)) {
1331 Status
= RingIntTransferDoorBell (Xhc
, Urb
);
1332 if (EFI_ERROR (Status
)) {
1339 Flush data from PCI controller specific address to mapped system
1342 @param Xhc The XHCI device.
1343 @param Urb The URB to unmap.
1345 @retval EFI_SUCCESS Success to flush data to mapped system memory.
1346 @retval EFI_DEVICE_ERROR Fail to flush data to mapped system memory.
1350 XhcFlushAsyncIntMap (
1351 IN USB_XHCI_INSTANCE
*Xhc
,
1356 EFI_PHYSICAL_ADDRESS PhyAddr
;
1357 EFI_PCI_IO_PROTOCOL_OPERATION MapOp
;
1358 EFI_PCI_IO_PROTOCOL
*PciIo
;
1365 if (Urb
->Ep
.Direction
== EfiUsbDataIn
) {
1366 MapOp
= EfiPciIoOperationBusMasterWrite
;
1368 MapOp
= EfiPciIoOperationBusMasterRead
;
1371 if (Urb
->DataMap
!= NULL
) {
1372 Status
= PciIo
->Unmap (PciIo
, Urb
->DataMap
);
1373 if (EFI_ERROR (Status
)) {
1378 Urb
->DataMap
= NULL
;
1380 Status
= PciIo
->Map (PciIo
, MapOp
, Urb
->Data
, &Len
, &PhyAddr
, &Map
);
1381 if (EFI_ERROR (Status
) || (Len
!= Urb
->DataLen
)) {
1385 Urb
->DataPhy
= (VOID
*) ((UINTN
) PhyAddr
);
1390 return EFI_DEVICE_ERROR
;
1394 Interrupt transfer periodic check handler.
1396 @param Event Interrupt event.
1397 @param Context Pointer to USB_XHCI_INSTANCE.
1402 XhcMonitorAsyncRequests (
1407 USB_XHCI_INSTANCE
*Xhc
;
1416 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1418 Xhc
= (USB_XHCI_INSTANCE
*) Context
;
1420 EFI_LIST_FOR_EACH_SAFE (Entry
, Next
, &Xhc
->AsyncIntTransfers
) {
1421 Urb
= EFI_LIST_CONTAINER (Entry
, URB
, UrbList
);
1424 // Make sure that the device is available before every check.
1426 SlotId
= XhcBusDevAddrToSlotId (Xhc
, Urb
->Ep
.BusAddr
);
1432 // Check the result of URB execution. If it is still
1433 // active, check the next one.
1435 XhcCheckUrbResult (Xhc
, Urb
);
1437 if (!Urb
->Finished
) {
1442 // Flush any PCI posted write transactions from a PCI host
1443 // bridge to system memory.
1445 Status
= XhcFlushAsyncIntMap (Xhc
, Urb
);
1446 if (EFI_ERROR (Status
)) {
1447 DEBUG ((EFI_D_ERROR
, "XhcMonitorAsyncRequests: Fail to Flush AsyncInt Mapped Memeory\n"));
1451 // Allocate a buffer then copy the transferred data for user.
1452 // If failed to allocate the buffer, update the URB for next
1453 // round of transfer. Ignore the data of this round.
1456 if (Urb
->Result
== EFI_USB_NOERROR
) {
1457 ASSERT (Urb
->Completed
<= Urb
->DataLen
);
1459 ProcBuf
= AllocateZeroPool (Urb
->Completed
);
1461 if (ProcBuf
== NULL
) {
1462 XhcUpdateAsyncRequest (Xhc
, Urb
);
1466 CopyMem (ProcBuf
, Urb
->Data
, Urb
->Completed
);
1470 // Leave error recovery to its related device driver. A
1471 // common case of the error recovery is to re-submit the
1472 // interrupt transfer which is linked to the head of the
1473 // list. This function scans from head to tail. So the
1474 // re-submitted interrupt transfer's callback function
1475 // will not be called again in this round. Don't touch this
1476 // URB after the callback, it may have been removed by the
1479 if (Urb
->Callback
!= NULL
) {
1481 // Restore the old TPL, USB bus maybe connect device in
1482 // his callback. Some drivers may has a lower TPL restriction.
1484 gBS
->RestoreTPL (OldTpl
);
1485 (Urb
->Callback
) (ProcBuf
, Urb
->Completed
, Urb
->Context
, Urb
->Result
);
1486 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1489 if (ProcBuf
!= NULL
) {
1490 gBS
->FreePool (ProcBuf
);
1493 XhcUpdateAsyncRequest (Xhc
, Urb
);
1495 gBS
->RestoreTPL (OldTpl
);
1499 Monitor the port status change. Enable/Disable device slot if there is a device attached/detached.
1501 @param Xhc The XHCI Instance.
1502 @param ParentRouteChart The route string pointed to the parent device if it exists.
1503 @param Port The port to be polled.
1504 @param PortState The port state.
1506 @retval EFI_SUCCESS Successfully enable/disable device slot according to port state.
1507 @retval Others Should not appear.
1512 XhcPollPortStatusChange (
1513 IN USB_XHCI_INSTANCE
*Xhc
,
1514 IN USB_DEV_ROUTE ParentRouteChart
,
1516 IN EFI_USB_PORT_STATUS
*PortState
1522 USB_DEV_ROUTE RouteChart
;
1524 Status
= EFI_SUCCESS
;
1526 if (ParentRouteChart
.Dword
== 0) {
1527 RouteChart
.Route
.RouteString
= 0;
1528 RouteChart
.Route
.RootPortNum
= Port
+ 1;
1529 RouteChart
.Route
.TierNum
= 1;
1532 RouteChart
.Route
.RouteString
= ParentRouteChart
.Route
.RouteString
| (Port
<< (4 * (ParentRouteChart
.Route
.TierNum
- 1)));
1534 RouteChart
.Route
.RouteString
= ParentRouteChart
.Route
.RouteString
| (15 << (4 * (ParentRouteChart
.Route
.TierNum
- 1)));
1536 RouteChart
.Route
.RootPortNum
= ParentRouteChart
.Route
.RootPortNum
;
1537 RouteChart
.Route
.TierNum
= ParentRouteChart
.Route
.TierNum
+ 1;
1540 if (((PortState
->PortStatus
& USB_PORT_STAT_ENABLE
) != 0) &&
1541 ((PortState
->PortStatus
& USB_PORT_STAT_CONNECTION
) != 0)) {
1543 // Has a device attached, Identify device speed after port is enabled.
1545 Speed
= EFI_USB_SPEED_FULL
;
1546 if ((PortState
->PortStatus
& USB_PORT_STAT_LOW_SPEED
) != 0) {
1547 Speed
= EFI_USB_SPEED_LOW
;
1548 } else if ((PortState
->PortStatus
& USB_PORT_STAT_HIGH_SPEED
) != 0) {
1549 Speed
= EFI_USB_SPEED_HIGH
;
1550 } else if ((PortState
->PortStatus
& USB_PORT_STAT_SUPER_SPEED
) != 0) {
1551 Speed
= EFI_USB_SPEED_SUPER
;
1554 // Execute Enable_Slot cmd for attached device, initialize device context and assign device address.
1556 SlotId
= XhcRouteStringToSlotId (Xhc
, RouteChart
);
1558 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1559 Status
= XhcInitializeDeviceSlot (Xhc
, ParentRouteChart
, Port
, RouteChart
, Speed
);
1561 Status
= XhcInitializeDeviceSlot64 (Xhc
, ParentRouteChart
, Port
, RouteChart
, Speed
);
1563 ASSERT_EFI_ERROR (Status
);
1565 } else if ((PortState
->PortStatus
& USB_PORT_STAT_CONNECTION
) == 0) {
1567 // Device is detached. Disable the allocated device slot and release resource.
1569 SlotId
= XhcRouteStringToSlotId (Xhc
, RouteChart
);
1571 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1572 Status
= XhcDisableSlotCmd (Xhc
, SlotId
);
1574 Status
= XhcDisableSlotCmd64 (Xhc
, SlotId
);
1576 ASSERT_EFI_ERROR (Status
);
1584 Calculate the device context index by endpoint address and direction.
1586 @param EpAddr The target endpoint number.
1587 @param Direction The direction of the target endpoint.
1589 @return The device context index of endpoint.
1603 Index
= (UINT8
) (2 * EpAddr
);
1604 if (Direction
== EfiUsbDataIn
) {
1612 Find out the actual device address according to the requested device address from UsbBus.
1614 @param Xhc The XHCI Instance.
1615 @param BusDevAddr The requested device address by UsbBus upper driver.
1617 @return The actual device address assigned to the device.
1622 XhcBusDevAddrToSlotId (
1623 IN USB_XHCI_INSTANCE
*Xhc
,
1629 for (Index
= 0; Index
< 255; Index
++) {
1630 if (Xhc
->UsbDevContext
[Index
+ 1].Enabled
&&
1631 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
!= 0) &&
1632 (Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
== BusDevAddr
)) {
1641 return Xhc
->UsbDevContext
[Index
+ 1].SlotId
;
1645 Find out the slot id according to the device's route string.
1647 @param Xhc The XHCI Instance.
1648 @param RouteString The route string described the device location.
1650 @return The slot id used by the device.
1655 XhcRouteStringToSlotId (
1656 IN USB_XHCI_INSTANCE
*Xhc
,
1657 IN USB_DEV_ROUTE RouteString
1662 for (Index
= 0; Index
< 255; Index
++) {
1663 if (Xhc
->UsbDevContext
[Index
+ 1].Enabled
&&
1664 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
!= 0) &&
1665 (Xhc
->UsbDevContext
[Index
+ 1].RouteString
.Dword
== RouteString
.Dword
)) {
1674 return Xhc
->UsbDevContext
[Index
+ 1].SlotId
;
1678 Synchronize the specified event ring to update the enqueue and dequeue pointer.
1680 @param Xhc The XHCI Instance.
1681 @param EvtRing The event ring to sync.
1683 @retval EFI_SUCCESS The event ring is synchronized successfully.
1689 IN USB_XHCI_INSTANCE
*Xhc
,
1690 IN EVENT_RING
*EvtRing
1694 TRB_TEMPLATE
*EvtTrb1
;
1696 ASSERT (EvtRing
!= NULL
);
1699 // Calculate the EventRingEnqueue and EventRingCCS.
1700 // Note: only support single Segment
1702 EvtTrb1
= EvtRing
->EventRingDequeue
;
1704 for (Index
= 0; Index
< EvtRing
->TrbNumber
; Index
++) {
1705 if (EvtTrb1
->CycleBit
!= EvtRing
->EventRingCCS
) {
1711 if ((UINTN
)EvtTrb1
>= ((UINTN
) EvtRing
->EventRingSeg0
+ sizeof (TRB_TEMPLATE
) * EvtRing
->TrbNumber
)) {
1712 EvtTrb1
= EvtRing
->EventRingSeg0
;
1713 EvtRing
->EventRingCCS
= (EvtRing
->EventRingCCS
) ? 0 : 1;
1717 if (Index
< EvtRing
->TrbNumber
) {
1718 EvtRing
->EventRingEnqueue
= EvtTrb1
;
1727 Synchronize the specified transfer ring to update the enqueue and dequeue pointer.
1729 @param Xhc The XHCI Instance.
1730 @param TrsRing The transfer ring to sync.
1732 @retval EFI_SUCCESS The transfer ring is synchronized successfully.
1738 IN USB_XHCI_INSTANCE
*Xhc
,
1739 IN TRANSFER_RING
*TrsRing
1743 TRB_TEMPLATE
*TrsTrb
;
1745 ASSERT (TrsRing
!= NULL
);
1747 // Calculate the latest RingEnqueue and RingPCS
1749 TrsTrb
= TrsRing
->RingEnqueue
;
1750 ASSERT (TrsTrb
!= NULL
);
1752 for (Index
= 0; Index
< TrsRing
->TrbNumber
; Index
++) {
1753 if (TrsTrb
->CycleBit
!= (TrsRing
->RingPCS
& BIT0
)) {
1757 if ((UINT8
) TrsTrb
->Type
== TRB_TYPE_LINK
) {
1758 ASSERT (((LINK_TRB
*)TrsTrb
)->TC
!= 0);
1760 // set cycle bit in Link TRB as normal
1762 ((LINK_TRB
*)TrsTrb
)->CycleBit
= TrsRing
->RingPCS
& BIT0
;
1764 // Toggle PCS maintained by software
1766 TrsRing
->RingPCS
= (TrsRing
->RingPCS
& BIT0
) ? 0 : 1;
1767 TrsTrb
= (TRB_TEMPLATE
*) TrsRing
->RingSeg0
; // Use host address
1771 ASSERT (Index
!= TrsRing
->TrbNumber
);
1773 if (TrsTrb
!= TrsRing
->RingEnqueue
) {
1774 TrsRing
->RingEnqueue
= TrsTrb
;
1778 // Clear the Trb context for enqueue, but reserve the PCS bit
1780 TrsTrb
->Parameter1
= 0;
1781 TrsTrb
->Parameter2
= 0;
1785 TrsTrb
->Control
= 0;
1791 Check if there is a new generated event.
1793 @param Xhc The XHCI Instance.
1794 @param EvtRing The event ring to check.
1795 @param NewEvtTrb The new event TRB found.
1797 @retval EFI_SUCCESS Found a new event TRB at the event ring.
1798 @retval EFI_NOT_READY The event ring has no new event.
1804 IN USB_XHCI_INSTANCE
*Xhc
,
1805 IN EVENT_RING
*EvtRing
,
1806 OUT TRB_TEMPLATE
**NewEvtTrb
1809 ASSERT (EvtRing
!= NULL
);
1811 *NewEvtTrb
= EvtRing
->EventRingDequeue
;
1813 if (EvtRing
->EventRingDequeue
== EvtRing
->EventRingEnqueue
) {
1814 return EFI_NOT_READY
;
1817 EvtRing
->EventRingDequeue
++;
1819 // If the dequeue pointer is beyond the ring, then roll-back it to the begining of the ring.
1821 if ((UINTN
)EvtRing
->EventRingDequeue
>= ((UINTN
) EvtRing
->EventRingSeg0
+ sizeof (TRB_TEMPLATE
) * EvtRing
->TrbNumber
)) {
1822 EvtRing
->EventRingDequeue
= EvtRing
->EventRingSeg0
;
1829 Ring the door bell to notify XHCI there is a transaction to be executed.
1831 @param Xhc The XHCI Instance.
1832 @param SlotId The slot id of the target device.
1833 @param Dci The device context index of the target slot or endpoint.
1835 @retval EFI_SUCCESS Successfully ring the door bell.
1841 IN USB_XHCI_INSTANCE
*Xhc
,
1847 XhcWriteDoorBellReg (Xhc
, 0, 0);
1849 XhcWriteDoorBellReg (Xhc
, SlotId
* sizeof (UINT32
), Dci
);
1856 Ring the door bell to notify XHCI there is a transaction to be executed through URB.
1858 @param Xhc The XHCI Instance.
1859 @param Urb The URB to be rung.
1861 @retval EFI_SUCCESS Successfully ring the door bell.
1865 RingIntTransferDoorBell (
1866 IN USB_XHCI_INSTANCE
*Xhc
,
1873 SlotId
= XhcBusDevAddrToSlotId (Xhc
, Urb
->Ep
.BusAddr
);
1874 Dci
= XhcEndpointToDci (Urb
->Ep
.EpAddr
, (UINT8
)(Urb
->Ep
.Direction
));
1875 XhcRingDoorBell (Xhc
, SlotId
, Dci
);
1880 Assign and initialize the device slot for a new device.
1882 @param Xhc The XHCI Instance.
1883 @param ParentRouteChart The route string pointed to the parent device.
1884 @param ParentPort The port at which the device is located.
1885 @param RouteChart The route string pointed to the device.
1886 @param DeviceSpeed The device speed.
1888 @retval EFI_SUCCESS Successfully assign a slot to the device and assign an address to it.
1893 XhcInitializeDeviceSlot (
1894 IN USB_XHCI_INSTANCE
*Xhc
,
1895 IN USB_DEV_ROUTE ParentRouteChart
,
1896 IN UINT16 ParentPort
,
1897 IN USB_DEV_ROUTE RouteChart
,
1898 IN UINT8 DeviceSpeed
1902 EVT_TRB_COMMAND_COMPLETION
*EvtTrb
;
1903 INPUT_CONTEXT
*InputContext
;
1904 DEVICE_CONTEXT
*OutputContext
;
1905 TRANSFER_RING
*EndpointTransferRing
;
1906 CMD_TRB_ADDRESS_DEVICE CmdTrbAddr
;
1907 UINT8 DeviceAddress
;
1908 CMD_TRB_ENABLE_SLOT CmdTrb
;
1911 DEVICE_CONTEXT
*ParentDeviceContext
;
1912 EFI_PHYSICAL_ADDRESS PhyAddr
;
1914 ZeroMem (&CmdTrb
, sizeof (CMD_TRB_ENABLE_SLOT
));
1915 CmdTrb
.CycleBit
= 1;
1916 CmdTrb
.Type
= TRB_TYPE_EN_SLOT
;
1918 Status
= XhcCmdTransfer (
1920 (TRB_TEMPLATE
*) (UINTN
) &CmdTrb
,
1921 XHC_GENERIC_TIMEOUT
,
1922 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
1924 ASSERT_EFI_ERROR (Status
);
1925 ASSERT (EvtTrb
->SlotId
<= Xhc
->MaxSlotsEn
);
1926 DEBUG ((EFI_D_INFO
, "Enable Slot Successfully, The Slot ID = 0x%x\n", EvtTrb
->SlotId
));
1927 SlotId
= (UINT8
)EvtTrb
->SlotId
;
1928 ASSERT (SlotId
!= 0);
1930 ZeroMem (&Xhc
->UsbDevContext
[SlotId
], sizeof (USB_DEV_CONTEXT
));
1931 Xhc
->UsbDevContext
[SlotId
].Enabled
= TRUE
;
1932 Xhc
->UsbDevContext
[SlotId
].SlotId
= SlotId
;
1933 Xhc
->UsbDevContext
[SlotId
].RouteString
.Dword
= RouteChart
.Dword
;
1934 Xhc
->UsbDevContext
[SlotId
].ParentRouteString
.Dword
= ParentRouteChart
.Dword
;
1937 // 4.3.3 Device Slot Initialization
1938 // 1) Allocate an Input Context data structure (6.2.5) and initialize all fields to '0'.
1940 InputContext
= UsbHcAllocateMem (Xhc
->MemPool
, sizeof (INPUT_CONTEXT
));
1941 ASSERT (InputContext
!= NULL
);
1942 ASSERT (((UINTN
) InputContext
& 0x3F) == 0);
1943 ZeroMem (InputContext
, sizeof (INPUT_CONTEXT
));
1945 Xhc
->UsbDevContext
[SlotId
].InputContext
= (VOID
*) InputContext
;
1948 // 2) Initialize the Input Control Context (6.2.5.1) of the Input Context by setting the A0 and A1
1949 // flags to '1'. These flags indicate that the Slot Context and the Endpoint 0 Context of the Input
1950 // Context are affected by the command.
1952 InputContext
->InputControlContext
.Dword2
|= (BIT0
| BIT1
);
1955 // 3) Initialize the Input Slot Context data structure
1957 InputContext
->Slot
.RouteString
= RouteChart
.Route
.RouteString
;
1958 InputContext
->Slot
.Speed
= DeviceSpeed
+ 1;
1959 InputContext
->Slot
.ContextEntries
= 1;
1960 InputContext
->Slot
.RootHubPortNum
= RouteChart
.Route
.RootPortNum
;
1962 if (RouteChart
.Route
.RouteString
) {
1964 // The device is behind of hub device.
1966 ParentSlotId
= XhcRouteStringToSlotId(Xhc
, ParentRouteChart
);
1967 ASSERT (ParentSlotId
!= 0);
1969 //if the Full/Low device attached to a High Speed Hub, Init the TTPortNum and TTHubSlotId field of slot context
1971 ParentDeviceContext
= (DEVICE_CONTEXT
*)Xhc
->UsbDevContext
[ParentSlotId
].OutputContext
;
1972 if ((ParentDeviceContext
->Slot
.TTPortNum
== 0) &&
1973 (ParentDeviceContext
->Slot
.TTHubSlotId
== 0)) {
1974 if ((ParentDeviceContext
->Slot
.Speed
== (EFI_USB_SPEED_HIGH
+ 1)) && (DeviceSpeed
< EFI_USB_SPEED_HIGH
)) {
1976 // Full/Low device attached to High speed hub port that isolates the high speed signaling
1977 // environment from Full/Low speed signaling environment for a device
1979 InputContext
->Slot
.TTPortNum
= ParentPort
;
1980 InputContext
->Slot
.TTHubSlotId
= ParentSlotId
;
1984 // Inherit the TT parameters from parent device.
1986 InputContext
->Slot
.TTPortNum
= ParentDeviceContext
->Slot
.TTPortNum
;
1987 InputContext
->Slot
.TTHubSlotId
= ParentDeviceContext
->Slot
.TTHubSlotId
;
1989 // If the device is a High speed device then down the speed to be the same as its parent Hub
1991 if (DeviceSpeed
== EFI_USB_SPEED_HIGH
) {
1992 InputContext
->Slot
.Speed
= ParentDeviceContext
->Slot
.Speed
;
1998 // 4) Allocate and initialize the Transfer Ring for the Default Control Endpoint.
2000 EndpointTransferRing
= AllocateZeroPool (sizeof (TRANSFER_RING
));
2001 Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[0] = EndpointTransferRing
;
2002 CreateTransferRing(Xhc
, TR_RING_TRB_NUMBER
, (TRANSFER_RING
*)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[0]);
2004 // 5) Initialize the Input default control Endpoint 0 Context (6.2.3).
2006 InputContext
->EP
[0].EPType
= ED_CONTROL_BIDIR
;
2008 if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
2009 InputContext
->EP
[0].MaxPacketSize
= 512;
2010 } else if (DeviceSpeed
== EFI_USB_SPEED_HIGH
) {
2011 InputContext
->EP
[0].MaxPacketSize
= 64;
2013 InputContext
->EP
[0].MaxPacketSize
= 8;
2016 // Initial value of Average TRB Length for Control endpoints would be 8B, Interrupt endpoints
2017 // 1KB, and Bulk and Isoch endpoints 3KB.
2019 InputContext
->EP
[0].AverageTRBLength
= 8;
2020 InputContext
->EP
[0].MaxBurstSize
= 0;
2021 InputContext
->EP
[0].Interval
= 0;
2022 InputContext
->EP
[0].MaxPStreams
= 0;
2023 InputContext
->EP
[0].Mult
= 0;
2024 InputContext
->EP
[0].CErr
= 3;
2027 // Init the DCS(dequeue cycle state) as the transfer ring's CCS
2029 PhyAddr
= UsbHcGetPciAddrForHostAddr (
2031 ((TRANSFER_RING
*)(UINTN
)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[0])->RingSeg0
,
2032 sizeof (TRB_TEMPLATE
) * TR_RING_TRB_NUMBER
2034 InputContext
->EP
[0].PtrLo
= XHC_LOW_32BIT (PhyAddr
) | BIT0
;
2035 InputContext
->EP
[0].PtrHi
= XHC_HIGH_32BIT (PhyAddr
);
2038 // 6) Allocate the Output Device Context data structure (6.2.1) and initialize it to '0'.
2040 OutputContext
= UsbHcAllocateMem (Xhc
->MemPool
, sizeof (DEVICE_CONTEXT
));
2041 ASSERT (OutputContext
!= NULL
);
2042 ASSERT (((UINTN
) OutputContext
& 0x3F) == 0);
2043 ZeroMem (OutputContext
, sizeof (DEVICE_CONTEXT
));
2045 Xhc
->UsbDevContext
[SlotId
].OutputContext
= OutputContext
;
2047 // 7) Load the appropriate (Device Slot ID) entry in the Device Context Base Address Array (5.4.6) with
2048 // a pointer to the Output Device Context data structure (6.2.1).
2050 PhyAddr
= UsbHcGetPciAddrForHostAddr (Xhc
->MemPool
, OutputContext
, sizeof (DEVICE_CONTEXT
));
2052 // Fill DCBAA with PCI device address
2054 Xhc
->DCBAA
[SlotId
] = (UINT64
) (UINTN
) PhyAddr
;
2057 // 8) Issue an Address Device Command for the Device Slot, where the command points to the Input
2058 // Context data structure described above.
2060 ZeroMem (&CmdTrbAddr
, sizeof (CmdTrbAddr
));
2061 PhyAddr
= UsbHcGetPciAddrForHostAddr (Xhc
->MemPool
, Xhc
->UsbDevContext
[SlotId
].InputContext
, sizeof (INPUT_CONTEXT
));
2062 CmdTrbAddr
.PtrLo
= XHC_LOW_32BIT (PhyAddr
);
2063 CmdTrbAddr
.PtrHi
= XHC_HIGH_32BIT (PhyAddr
);
2064 CmdTrbAddr
.CycleBit
= 1;
2065 CmdTrbAddr
.Type
= TRB_TYPE_ADDRESS_DEV
;
2066 CmdTrbAddr
.SlotId
= Xhc
->UsbDevContext
[SlotId
].SlotId
;
2067 Status
= XhcCmdTransfer (
2069 (TRB_TEMPLATE
*) (UINTN
) &CmdTrbAddr
,
2070 XHC_GENERIC_TIMEOUT
,
2071 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
2073 ASSERT (!EFI_ERROR(Status
));
2075 DeviceAddress
= (UINT8
) ((DEVICE_CONTEXT
*) OutputContext
)->Slot
.DeviceAddress
;
2076 DEBUG ((EFI_D_INFO
, " Address %d assigned successfully\n", DeviceAddress
));
2078 Xhc
->UsbDevContext
[SlotId
].XhciDevAddr
= DeviceAddress
;
2084 Assign and initialize the device slot for a new device.
2086 @param Xhc The XHCI Instance.
2087 @param ParentRouteChart The route string pointed to the parent device.
2088 @param ParentPort The port at which the device is located.
2089 @param RouteChart The route string pointed to the device.
2090 @param DeviceSpeed The device speed.
2092 @retval EFI_SUCCESS Successfully assign a slot to the device and assign an address to it.
2097 XhcInitializeDeviceSlot64 (
2098 IN USB_XHCI_INSTANCE
*Xhc
,
2099 IN USB_DEV_ROUTE ParentRouteChart
,
2100 IN UINT16 ParentPort
,
2101 IN USB_DEV_ROUTE RouteChart
,
2102 IN UINT8 DeviceSpeed
2106 EVT_TRB_COMMAND_COMPLETION
*EvtTrb
;
2107 INPUT_CONTEXT_64
*InputContext
;
2108 DEVICE_CONTEXT_64
*OutputContext
;
2109 TRANSFER_RING
*EndpointTransferRing
;
2110 CMD_TRB_ADDRESS_DEVICE CmdTrbAddr
;
2111 UINT8 DeviceAddress
;
2112 CMD_TRB_ENABLE_SLOT CmdTrb
;
2115 DEVICE_CONTEXT_64
*ParentDeviceContext
;
2116 EFI_PHYSICAL_ADDRESS PhyAddr
;
2118 ZeroMem (&CmdTrb
, sizeof (CMD_TRB_ENABLE_SLOT
));
2119 CmdTrb
.CycleBit
= 1;
2120 CmdTrb
.Type
= TRB_TYPE_EN_SLOT
;
2122 Status
= XhcCmdTransfer (
2124 (TRB_TEMPLATE
*) (UINTN
) &CmdTrb
,
2125 XHC_GENERIC_TIMEOUT
,
2126 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
2128 ASSERT_EFI_ERROR (Status
);
2129 ASSERT (EvtTrb
->SlotId
<= Xhc
->MaxSlotsEn
);
2130 DEBUG ((EFI_D_INFO
, "Enable Slot Successfully, The Slot ID = 0x%x\n", EvtTrb
->SlotId
));
2131 SlotId
= (UINT8
)EvtTrb
->SlotId
;
2132 ASSERT (SlotId
!= 0);
2134 ZeroMem (&Xhc
->UsbDevContext
[SlotId
], sizeof (USB_DEV_CONTEXT
));
2135 Xhc
->UsbDevContext
[SlotId
].Enabled
= TRUE
;
2136 Xhc
->UsbDevContext
[SlotId
].SlotId
= SlotId
;
2137 Xhc
->UsbDevContext
[SlotId
].RouteString
.Dword
= RouteChart
.Dword
;
2138 Xhc
->UsbDevContext
[SlotId
].ParentRouteString
.Dword
= ParentRouteChart
.Dword
;
2141 // 4.3.3 Device Slot Initialization
2142 // 1) Allocate an Input Context data structure (6.2.5) and initialize all fields to '0'.
2144 InputContext
= UsbHcAllocateMem (Xhc
->MemPool
, sizeof (INPUT_CONTEXT_64
));
2145 ASSERT (InputContext
!= NULL
);
2146 ASSERT (((UINTN
) InputContext
& 0x3F) == 0);
2147 ZeroMem (InputContext
, sizeof (INPUT_CONTEXT_64
));
2149 Xhc
->UsbDevContext
[SlotId
].InputContext
= (VOID
*) InputContext
;
2152 // 2) Initialize the Input Control Context (6.2.5.1) of the Input Context by setting the A0 and A1
2153 // flags to '1'. These flags indicate that the Slot Context and the Endpoint 0 Context of the Input
2154 // Context are affected by the command.
2156 InputContext
->InputControlContext
.Dword2
|= (BIT0
| BIT1
);
2159 // 3) Initialize the Input Slot Context data structure
2161 InputContext
->Slot
.RouteString
= RouteChart
.Route
.RouteString
;
2162 InputContext
->Slot
.Speed
= DeviceSpeed
+ 1;
2163 InputContext
->Slot
.ContextEntries
= 1;
2164 InputContext
->Slot
.RootHubPortNum
= RouteChart
.Route
.RootPortNum
;
2166 if (RouteChart
.Route
.RouteString
) {
2168 // The device is behind of hub device.
2170 ParentSlotId
= XhcRouteStringToSlotId(Xhc
, ParentRouteChart
);
2171 ASSERT (ParentSlotId
!= 0);
2173 //if the Full/Low device attached to a High Speed Hub, Init the TTPortNum and TTHubSlotId field of slot context
2175 ParentDeviceContext
= (DEVICE_CONTEXT_64
*)Xhc
->UsbDevContext
[ParentSlotId
].OutputContext
;
2176 if ((ParentDeviceContext
->Slot
.TTPortNum
== 0) &&
2177 (ParentDeviceContext
->Slot
.TTHubSlotId
== 0)) {
2178 if ((ParentDeviceContext
->Slot
.Speed
== (EFI_USB_SPEED_HIGH
+ 1)) && (DeviceSpeed
< EFI_USB_SPEED_HIGH
)) {
2180 // Full/Low device attached to High speed hub port that isolates the high speed signaling
2181 // environment from Full/Low speed signaling environment for a device
2183 InputContext
->Slot
.TTPortNum
= ParentPort
;
2184 InputContext
->Slot
.TTHubSlotId
= ParentSlotId
;
2188 // Inherit the TT parameters from parent device.
2190 InputContext
->Slot
.TTPortNum
= ParentDeviceContext
->Slot
.TTPortNum
;
2191 InputContext
->Slot
.TTHubSlotId
= ParentDeviceContext
->Slot
.TTHubSlotId
;
2193 // If the device is a High speed device then down the speed to be the same as its parent Hub
2195 if (DeviceSpeed
== EFI_USB_SPEED_HIGH
) {
2196 InputContext
->Slot
.Speed
= ParentDeviceContext
->Slot
.Speed
;
2202 // 4) Allocate and initialize the Transfer Ring for the Default Control Endpoint.
2204 EndpointTransferRing
= AllocateZeroPool (sizeof (TRANSFER_RING
));
2205 Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[0] = EndpointTransferRing
;
2206 CreateTransferRing(Xhc
, TR_RING_TRB_NUMBER
, (TRANSFER_RING
*)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[0]);
2208 // 5) Initialize the Input default control Endpoint 0 Context (6.2.3).
2210 InputContext
->EP
[0].EPType
= ED_CONTROL_BIDIR
;
2212 if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
2213 InputContext
->EP
[0].MaxPacketSize
= 512;
2214 } else if (DeviceSpeed
== EFI_USB_SPEED_HIGH
) {
2215 InputContext
->EP
[0].MaxPacketSize
= 64;
2217 InputContext
->EP
[0].MaxPacketSize
= 8;
2220 // Initial value of Average TRB Length for Control endpoints would be 8B, Interrupt endpoints
2221 // 1KB, and Bulk and Isoch endpoints 3KB.
2223 InputContext
->EP
[0].AverageTRBLength
= 8;
2224 InputContext
->EP
[0].MaxBurstSize
= 0;
2225 InputContext
->EP
[0].Interval
= 0;
2226 InputContext
->EP
[0].MaxPStreams
= 0;
2227 InputContext
->EP
[0].Mult
= 0;
2228 InputContext
->EP
[0].CErr
= 3;
2231 // Init the DCS(dequeue cycle state) as the transfer ring's CCS
2233 PhyAddr
= UsbHcGetPciAddrForHostAddr (
2235 ((TRANSFER_RING
*)(UINTN
)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[0])->RingSeg0
,
2236 sizeof (TRB_TEMPLATE
) * TR_RING_TRB_NUMBER
2238 InputContext
->EP
[0].PtrLo
= XHC_LOW_32BIT (PhyAddr
) | BIT0
;
2239 InputContext
->EP
[0].PtrHi
= XHC_HIGH_32BIT (PhyAddr
);
2242 // 6) Allocate the Output Device Context data structure (6.2.1) and initialize it to '0'.
2244 OutputContext
= UsbHcAllocateMem (Xhc
->MemPool
, sizeof (DEVICE_CONTEXT_64
));
2245 ASSERT (OutputContext
!= NULL
);
2246 ASSERT (((UINTN
) OutputContext
& 0x3F) == 0);
2247 ZeroMem (OutputContext
, sizeof (DEVICE_CONTEXT_64
));
2249 Xhc
->UsbDevContext
[SlotId
].OutputContext
= OutputContext
;
2251 // 7) Load the appropriate (Device Slot ID) entry in the Device Context Base Address Array (5.4.6) with
2252 // a pointer to the Output Device Context data structure (6.2.1).
2254 PhyAddr
= UsbHcGetPciAddrForHostAddr (Xhc
->MemPool
, OutputContext
, sizeof (DEVICE_CONTEXT_64
));
2256 // Fill DCBAA with PCI device address
2258 Xhc
->DCBAA
[SlotId
] = (UINT64
) (UINTN
) PhyAddr
;
2261 // 8) Issue an Address Device Command for the Device Slot, where the command points to the Input
2262 // Context data structure described above.
2264 ZeroMem (&CmdTrbAddr
, sizeof (CmdTrbAddr
));
2265 PhyAddr
= UsbHcGetPciAddrForHostAddr (Xhc
->MemPool
, Xhc
->UsbDevContext
[SlotId
].InputContext
, sizeof (INPUT_CONTEXT_64
));
2266 CmdTrbAddr
.PtrLo
= XHC_LOW_32BIT (PhyAddr
);
2267 CmdTrbAddr
.PtrHi
= XHC_HIGH_32BIT (PhyAddr
);
2268 CmdTrbAddr
.CycleBit
= 1;
2269 CmdTrbAddr
.Type
= TRB_TYPE_ADDRESS_DEV
;
2270 CmdTrbAddr
.SlotId
= Xhc
->UsbDevContext
[SlotId
].SlotId
;
2271 Status
= XhcCmdTransfer (
2273 (TRB_TEMPLATE
*) (UINTN
) &CmdTrbAddr
,
2274 XHC_GENERIC_TIMEOUT
,
2275 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
2277 ASSERT (!EFI_ERROR(Status
));
2279 DeviceAddress
= (UINT8
) ((DEVICE_CONTEXT_64
*) OutputContext
)->Slot
.DeviceAddress
;
2280 DEBUG ((EFI_D_INFO
, " Address %d assigned successfully\n", DeviceAddress
));
2282 Xhc
->UsbDevContext
[SlotId
].XhciDevAddr
= DeviceAddress
;
2289 Disable the specified device slot.
2291 @param Xhc The XHCI Instance.
2292 @param SlotId The slot id to be disabled.
2294 @retval EFI_SUCCESS Successfully disable the device slot.
2300 IN USB_XHCI_INSTANCE
*Xhc
,
2305 TRB_TEMPLATE
*EvtTrb
;
2306 CMD_TRB_DISABLE_SLOT CmdTrbDisSlot
;
2311 // Disable the device slots occupied by these devices on its downstream ports.
2312 // Entry 0 is reserved.
2314 for (Index
= 0; Index
< 255; Index
++) {
2315 if (!Xhc
->UsbDevContext
[Index
+ 1].Enabled
||
2316 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
== 0) ||
2317 (Xhc
->UsbDevContext
[Index
+ 1].ParentRouteString
.Dword
!= Xhc
->UsbDevContext
[SlotId
].RouteString
.Dword
)) {
2321 Status
= XhcDisableSlotCmd (Xhc
, Xhc
->UsbDevContext
[Index
+ 1].SlotId
);
2323 if (EFI_ERROR (Status
)) {
2324 DEBUG ((EFI_D_ERROR
, "XhcDisableSlotCmd: failed to disable child, ignore error\n"));
2325 Xhc
->UsbDevContext
[Index
+ 1].SlotId
= 0;
2330 // Construct the disable slot command
2332 DEBUG ((EFI_D_INFO
, "Disable device slot %d!\n", SlotId
));
2334 ZeroMem (&CmdTrbDisSlot
, sizeof (CmdTrbDisSlot
));
2335 CmdTrbDisSlot
.CycleBit
= 1;
2336 CmdTrbDisSlot
.Type
= TRB_TYPE_DIS_SLOT
;
2337 CmdTrbDisSlot
.SlotId
= SlotId
;
2338 Status
= XhcCmdTransfer (
2340 (TRB_TEMPLATE
*) (UINTN
) &CmdTrbDisSlot
,
2341 XHC_GENERIC_TIMEOUT
,
2342 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
2344 ASSERT_EFI_ERROR(Status
);
2346 // Free the slot's device context entry
2348 Xhc
->DCBAA
[SlotId
] = 0;
2351 // Free the slot related data structure
2353 for (Index
= 0; Index
< 31; Index
++) {
2354 if (Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Index
] != NULL
) {
2355 RingSeg
= ((TRANSFER_RING
*)(UINTN
)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Index
])->RingSeg0
;
2356 if (RingSeg
!= NULL
) {
2357 UsbHcFreeMem (Xhc
->MemPool
, RingSeg
, sizeof (TRB_TEMPLATE
) * TR_RING_TRB_NUMBER
);
2359 FreePool (Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Index
]);
2360 Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Index
] = NULL
;
2364 for (Index
= 0; Index
< Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
; Index
++) {
2365 if (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
] != NULL
) {
2366 FreePool (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]);
2370 if (Xhc
->UsbDevContext
[SlotId
].InputContext
!= NULL
) {
2371 UsbHcFreeMem (Xhc
->MemPool
, Xhc
->UsbDevContext
[SlotId
].InputContext
, sizeof (INPUT_CONTEXT
));
2374 if (Xhc
->UsbDevContext
[SlotId
].OutputContext
!= NULL
) {
2375 UsbHcFreeMem (Xhc
->MemPool
, Xhc
->UsbDevContext
[SlotId
].OutputContext
, sizeof (DEVICE_CONTEXT
));
2378 // Doesn't zero the entry because XhcAsyncInterruptTransfer() may be invoked to remove the established
2379 // asynchronous interrupt pipe after the device is disabled. It needs the device address mapping info to
2380 // remove urb from XHCI's asynchronous transfer list.
2382 Xhc
->UsbDevContext
[SlotId
].Enabled
= FALSE
;
2383 Xhc
->UsbDevContext
[SlotId
].SlotId
= 0;
2389 Disable the specified device slot.
2391 @param Xhc The XHCI Instance.
2392 @param SlotId The slot id to be disabled.
2394 @retval EFI_SUCCESS Successfully disable the device slot.
2399 XhcDisableSlotCmd64 (
2400 IN USB_XHCI_INSTANCE
*Xhc
,
2405 TRB_TEMPLATE
*EvtTrb
;
2406 CMD_TRB_DISABLE_SLOT CmdTrbDisSlot
;
2411 // Disable the device slots occupied by these devices on its downstream ports.
2412 // Entry 0 is reserved.
2414 for (Index
= 0; Index
< 255; Index
++) {
2415 if (!Xhc
->UsbDevContext
[Index
+ 1].Enabled
||
2416 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
== 0) ||
2417 (Xhc
->UsbDevContext
[Index
+ 1].ParentRouteString
.Dword
!= Xhc
->UsbDevContext
[SlotId
].RouteString
.Dword
)) {
2421 Status
= XhcDisableSlotCmd64 (Xhc
, Xhc
->UsbDevContext
[Index
+ 1].SlotId
);
2423 if (EFI_ERROR (Status
)) {
2424 DEBUG ((EFI_D_ERROR
, "XhcDisableSlotCmd: failed to disable child, ignore error\n"));
2425 Xhc
->UsbDevContext
[Index
+ 1].SlotId
= 0;
2430 // Construct the disable slot command
2432 DEBUG ((EFI_D_INFO
, "Disable device slot %d!\n", SlotId
));
2434 ZeroMem (&CmdTrbDisSlot
, sizeof (CmdTrbDisSlot
));
2435 CmdTrbDisSlot
.CycleBit
= 1;
2436 CmdTrbDisSlot
.Type
= TRB_TYPE_DIS_SLOT
;
2437 CmdTrbDisSlot
.SlotId
= SlotId
;
2438 Status
= XhcCmdTransfer (
2440 (TRB_TEMPLATE
*) (UINTN
) &CmdTrbDisSlot
,
2441 XHC_GENERIC_TIMEOUT
,
2442 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
2444 ASSERT_EFI_ERROR(Status
);
2446 // Free the slot's device context entry
2448 Xhc
->DCBAA
[SlotId
] = 0;
2451 // Free the slot related data structure
2453 for (Index
= 0; Index
< 31; Index
++) {
2454 if (Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Index
] != NULL
) {
2455 RingSeg
= ((TRANSFER_RING
*)(UINTN
)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Index
])->RingSeg0
;
2456 if (RingSeg
!= NULL
) {
2457 UsbHcFreeMem (Xhc
->MemPool
, RingSeg
, sizeof (TRB_TEMPLATE
) * TR_RING_TRB_NUMBER
);
2459 FreePool (Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Index
]);
2460 Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Index
] = NULL
;
2464 for (Index
= 0; Index
< Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
; Index
++) {
2465 if (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
] != NULL
) {
2466 FreePool (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]);
2470 if (Xhc
->UsbDevContext
[SlotId
].InputContext
!= NULL
) {
2471 UsbHcFreeMem (Xhc
->MemPool
, Xhc
->UsbDevContext
[SlotId
].InputContext
, sizeof (INPUT_CONTEXT_64
));
2474 if (Xhc
->UsbDevContext
[SlotId
].OutputContext
!= NULL
) {
2475 UsbHcFreeMem (Xhc
->MemPool
, Xhc
->UsbDevContext
[SlotId
].OutputContext
, sizeof (DEVICE_CONTEXT_64
));
2478 // Doesn't zero the entry because XhcAsyncInterruptTransfer() may be invoked to remove the established
2479 // asynchronous interrupt pipe after the device is disabled. It needs the device address mapping info to
2480 // remove urb from XHCI's asynchronous transfer list.
2482 Xhc
->UsbDevContext
[SlotId
].Enabled
= FALSE
;
2483 Xhc
->UsbDevContext
[SlotId
].SlotId
= 0;
2490 Configure all the device endpoints through XHCI's Configure_Endpoint cmd.
2492 @param Xhc The XHCI Instance.
2493 @param SlotId The slot id to be configured.
2494 @param DeviceSpeed The device's speed.
2495 @param ConfigDesc The pointer to the usb device configuration descriptor.
2497 @retval EFI_SUCCESS Successfully configure all the device endpoints.
2503 IN USB_XHCI_INSTANCE
*Xhc
,
2505 IN UINT8 DeviceSpeed
,
2506 IN USB_CONFIG_DESCRIPTOR
*ConfigDesc
2511 USB_INTERFACE_DESCRIPTOR
*IfDesc
;
2512 USB_ENDPOINT_DESCRIPTOR
*EpDesc
;
2520 EFI_PHYSICAL_ADDRESS PhyAddr
;
2523 TRANSFER_RING
*EndpointTransferRing
;
2524 CMD_TRB_CONFIG_ENDPOINT CmdTrbCfgEP
;
2525 INPUT_CONTEXT
*InputContext
;
2526 DEVICE_CONTEXT
*OutputContext
;
2527 EVT_TRB_COMMAND_COMPLETION
*EvtTrb
;
2529 // 4.6.6 Configure Endpoint
2531 InputContext
= Xhc
->UsbDevContext
[SlotId
].InputContext
;
2532 OutputContext
= Xhc
->UsbDevContext
[SlotId
].OutputContext
;
2533 ZeroMem (InputContext
, sizeof (INPUT_CONTEXT
));
2534 CopyMem (&InputContext
->Slot
, &OutputContext
->Slot
, sizeof (SLOT_CONTEXT
));
2536 ASSERT (ConfigDesc
!= NULL
);
2540 IfDesc
= (USB_INTERFACE_DESCRIPTOR
*)(ConfigDesc
+ 1);
2541 for (Index
= 0; Index
< ConfigDesc
->NumInterfaces
; Index
++) {
2542 while (IfDesc
->DescriptorType
!= USB_DESC_TYPE_INTERFACE
) {
2543 IfDesc
= (USB_INTERFACE_DESCRIPTOR
*)((UINTN
)IfDesc
+ IfDesc
->Length
);
2546 NumEp
= IfDesc
->NumEndpoints
;
2548 EpDesc
= (USB_ENDPOINT_DESCRIPTOR
*)(IfDesc
+ 1);
2549 for (EpIndex
= 0; EpIndex
< NumEp
; EpIndex
++) {
2550 while (EpDesc
->DescriptorType
!= USB_DESC_TYPE_ENDPOINT
) {
2551 EpDesc
= (USB_ENDPOINT_DESCRIPTOR
*)((UINTN
)EpDesc
+ EpDesc
->Length
);
2554 EpAddr
= (UINT8
)(EpDesc
->EndpointAddress
& 0x0F);
2555 Direction
= (UINT8
)((EpDesc
->EndpointAddress
& 0x80) ? EfiUsbDataIn
: EfiUsbDataOut
);
2557 Dci
= XhcEndpointToDci (EpAddr
, Direction
);
2563 InputContext
->InputControlContext
.Dword2
|= (BIT0
<< Dci
);
2564 InputContext
->EP
[Dci
-1].MaxPacketSize
= EpDesc
->MaxPacketSize
;
2566 if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
2568 // 6.2.3.4, shall be set to the value defined in the bMaxBurst field of the SuperSpeed Endpoint Companion Descriptor.
2570 InputContext
->EP
[Dci
-1].MaxBurstSize
= 0x0;
2572 InputContext
->EP
[Dci
-1].MaxBurstSize
= 0x0;
2575 switch (EpDesc
->Attributes
& USB_ENDPOINT_TYPE_MASK
) {
2576 case USB_ENDPOINT_BULK
:
2577 if (Direction
== EfiUsbDataIn
) {
2578 InputContext
->EP
[Dci
-1].CErr
= 3;
2579 InputContext
->EP
[Dci
-1].EPType
= ED_BULK_IN
;
2581 InputContext
->EP
[Dci
-1].CErr
= 3;
2582 InputContext
->EP
[Dci
-1].EPType
= ED_BULK_OUT
;
2585 InputContext
->EP
[Dci
-1].AverageTRBLength
= 0x1000;
2586 if (Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1] == NULL
) {
2587 EndpointTransferRing
= AllocateZeroPool(sizeof (TRANSFER_RING
));
2588 Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1] = (VOID
*) EndpointTransferRing
;
2589 CreateTransferRing(Xhc
, TR_RING_TRB_NUMBER
, (TRANSFER_RING
*)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1]);
2593 case USB_ENDPOINT_ISO
:
2594 if (Direction
== EfiUsbDataIn
) {
2595 InputContext
->EP
[Dci
-1].CErr
= 0;
2596 InputContext
->EP
[Dci
-1].EPType
= ED_ISOCH_IN
;
2598 InputContext
->EP
[Dci
-1].CErr
= 0;
2599 InputContext
->EP
[Dci
-1].EPType
= ED_ISOCH_OUT
;
2602 case USB_ENDPOINT_INTERRUPT
:
2603 if (Direction
== EfiUsbDataIn
) {
2604 InputContext
->EP
[Dci
-1].CErr
= 3;
2605 InputContext
->EP
[Dci
-1].EPType
= ED_INTERRUPT_IN
;
2607 InputContext
->EP
[Dci
-1].CErr
= 3;
2608 InputContext
->EP
[Dci
-1].EPType
= ED_INTERRUPT_OUT
;
2610 InputContext
->EP
[Dci
-1].AverageTRBLength
= 0x1000;
2611 InputContext
->EP
[Dci
-1].MaxESITPayload
= EpDesc
->MaxPacketSize
;
2613 // Get the bInterval from descriptor and init the the interval field of endpoint context
2615 if ((DeviceSpeed
== EFI_USB_SPEED_FULL
) || (DeviceSpeed
== EFI_USB_SPEED_LOW
)) {
2616 Interval
= EpDesc
->Interval
;
2618 // Hard code the interval to MAX first, need calculate through the bInterval field of Endpoint descriptor.
2620 InputContext
->EP
[Dci
-1].Interval
= 6;
2621 } else if ((DeviceSpeed
== EFI_USB_SPEED_HIGH
) || (DeviceSpeed
== EFI_USB_SPEED_SUPER
)) {
2622 Interval
= EpDesc
->Interval
;
2623 ASSERT (Interval
>= 1 && Interval
<= 16);
2625 // Refer to XHCI 1.0 spec section 6.2.3.6, table 61
2627 InputContext
->EP
[Dci
-1].Interval
= Interval
- 1;
2628 InputContext
->EP
[Dci
-1].AverageTRBLength
= 0x1000;
2629 InputContext
->EP
[Dci
-1].MaxESITPayload
= 0x0002;
2630 InputContext
->EP
[Dci
-1].MaxBurstSize
= 0x0;
2631 InputContext
->EP
[Dci
-1].CErr
= 3;
2634 if (Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1] == NULL
) {
2635 EndpointTransferRing
= AllocateZeroPool(sizeof (TRANSFER_RING
));
2636 Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1] = (VOID
*) EndpointTransferRing
;
2637 CreateTransferRing(Xhc
, TR_RING_TRB_NUMBER
, (TRANSFER_RING
*)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1]);
2641 case USB_ENDPOINT_CONTROL
:
2647 PhyAddr
= UsbHcGetPciAddrForHostAddr (
2649 ((TRANSFER_RING
*)(UINTN
)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1])->RingSeg0
,
2650 sizeof (TRB_TEMPLATE
) * TR_RING_TRB_NUMBER
2653 PhyAddr
|= ((TRANSFER_RING
*)(UINTN
)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1])->RingPCS
;
2654 InputContext
->EP
[Dci
-1].PtrLo
= XHC_LOW_32BIT (PhyAddr
);
2655 InputContext
->EP
[Dci
-1].PtrHi
= XHC_HIGH_32BIT (PhyAddr
);
2657 EpDesc
= (USB_ENDPOINT_DESCRIPTOR
*)((UINTN
)EpDesc
+ EpDesc
->Length
);
2659 IfDesc
= (USB_INTERFACE_DESCRIPTOR
*)((UINTN
)IfDesc
+ IfDesc
->Length
);
2662 InputContext
->InputControlContext
.Dword2
|= BIT0
;
2663 InputContext
->Slot
.ContextEntries
= MaxDci
;
2665 // configure endpoint
2667 ZeroMem (&CmdTrbCfgEP
, sizeof (CmdTrbCfgEP
));
2668 PhyAddr
= UsbHcGetPciAddrForHostAddr (Xhc
->MemPool
, InputContext
, sizeof (INPUT_CONTEXT
));
2669 CmdTrbCfgEP
.PtrLo
= XHC_LOW_32BIT (PhyAddr
);
2670 CmdTrbCfgEP
.PtrHi
= XHC_HIGH_32BIT (PhyAddr
);
2671 CmdTrbCfgEP
.CycleBit
= 1;
2672 CmdTrbCfgEP
.Type
= TRB_TYPE_CON_ENDPOINT
;
2673 CmdTrbCfgEP
.SlotId
= Xhc
->UsbDevContext
[SlotId
].SlotId
;
2674 DEBUG ((EFI_D_INFO
, "Configure Endpoint\n"));
2675 Status
= XhcCmdTransfer (
2677 (TRB_TEMPLATE
*) (UINTN
) &CmdTrbCfgEP
,
2678 XHC_GENERIC_TIMEOUT
,
2679 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
2681 ASSERT_EFI_ERROR(Status
);
2687 Configure all the device endpoints through XHCI's Configure_Endpoint cmd.
2689 @param Xhc The XHCI Instance.
2690 @param SlotId The slot id to be configured.
2691 @param DeviceSpeed The device's speed.
2692 @param ConfigDesc The pointer to the usb device configuration descriptor.
2694 @retval EFI_SUCCESS Successfully configure all the device endpoints.
2700 IN USB_XHCI_INSTANCE
*Xhc
,
2702 IN UINT8 DeviceSpeed
,
2703 IN USB_CONFIG_DESCRIPTOR
*ConfigDesc
2708 USB_INTERFACE_DESCRIPTOR
*IfDesc
;
2709 USB_ENDPOINT_DESCRIPTOR
*EpDesc
;
2717 EFI_PHYSICAL_ADDRESS PhyAddr
;
2720 TRANSFER_RING
*EndpointTransferRing
;
2721 CMD_TRB_CONFIG_ENDPOINT CmdTrbCfgEP
;
2722 INPUT_CONTEXT_64
*InputContext
;
2723 DEVICE_CONTEXT_64
*OutputContext
;
2724 EVT_TRB_COMMAND_COMPLETION
*EvtTrb
;
2726 // 4.6.6 Configure Endpoint
2728 InputContext
= Xhc
->UsbDevContext
[SlotId
].InputContext
;
2729 OutputContext
= Xhc
->UsbDevContext
[SlotId
].OutputContext
;
2730 ZeroMem (InputContext
, sizeof (INPUT_CONTEXT_64
));
2731 CopyMem (&InputContext
->Slot
, &OutputContext
->Slot
, sizeof (SLOT_CONTEXT_64
));
2733 ASSERT (ConfigDesc
!= NULL
);
2737 IfDesc
= (USB_INTERFACE_DESCRIPTOR
*)(ConfigDesc
+ 1);
2738 for (Index
= 0; Index
< ConfigDesc
->NumInterfaces
; Index
++) {
2739 while (IfDesc
->DescriptorType
!= USB_DESC_TYPE_INTERFACE
) {
2740 IfDesc
= (USB_INTERFACE_DESCRIPTOR
*)((UINTN
)IfDesc
+ IfDesc
->Length
);
2743 NumEp
= IfDesc
->NumEndpoints
;
2745 EpDesc
= (USB_ENDPOINT_DESCRIPTOR
*)(IfDesc
+ 1);
2746 for (EpIndex
= 0; EpIndex
< NumEp
; EpIndex
++) {
2747 while (EpDesc
->DescriptorType
!= USB_DESC_TYPE_ENDPOINT
) {
2748 EpDesc
= (USB_ENDPOINT_DESCRIPTOR
*)((UINTN
)EpDesc
+ EpDesc
->Length
);
2751 EpAddr
= (UINT8
)(EpDesc
->EndpointAddress
& 0x0F);
2752 Direction
= (UINT8
)((EpDesc
->EndpointAddress
& 0x80) ? EfiUsbDataIn
: EfiUsbDataOut
);
2754 Dci
= XhcEndpointToDci (EpAddr
, Direction
);
2760 InputContext
->InputControlContext
.Dword2
|= (BIT0
<< Dci
);
2761 InputContext
->EP
[Dci
-1].MaxPacketSize
= EpDesc
->MaxPacketSize
;
2763 if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
2765 // 6.2.3.4, shall be set to the value defined in the bMaxBurst field of the SuperSpeed Endpoint Companion Descriptor.
2767 InputContext
->EP
[Dci
-1].MaxBurstSize
= 0x0;
2769 InputContext
->EP
[Dci
-1].MaxBurstSize
= 0x0;
2772 switch (EpDesc
->Attributes
& USB_ENDPOINT_TYPE_MASK
) {
2773 case USB_ENDPOINT_BULK
:
2774 if (Direction
== EfiUsbDataIn
) {
2775 InputContext
->EP
[Dci
-1].CErr
= 3;
2776 InputContext
->EP
[Dci
-1].EPType
= ED_BULK_IN
;
2778 InputContext
->EP
[Dci
-1].CErr
= 3;
2779 InputContext
->EP
[Dci
-1].EPType
= ED_BULK_OUT
;
2782 InputContext
->EP
[Dci
-1].AverageTRBLength
= 0x1000;
2783 if (Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1] == NULL
) {
2784 EndpointTransferRing
= AllocateZeroPool(sizeof (TRANSFER_RING
));
2785 Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1] = (VOID
*) EndpointTransferRing
;
2786 CreateTransferRing(Xhc
, TR_RING_TRB_NUMBER
, (TRANSFER_RING
*)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1]);
2790 case USB_ENDPOINT_ISO
:
2791 if (Direction
== EfiUsbDataIn
) {
2792 InputContext
->EP
[Dci
-1].CErr
= 0;
2793 InputContext
->EP
[Dci
-1].EPType
= ED_ISOCH_IN
;
2795 InputContext
->EP
[Dci
-1].CErr
= 0;
2796 InputContext
->EP
[Dci
-1].EPType
= ED_ISOCH_OUT
;
2799 case USB_ENDPOINT_INTERRUPT
:
2800 if (Direction
== EfiUsbDataIn
) {
2801 InputContext
->EP
[Dci
-1].CErr
= 3;
2802 InputContext
->EP
[Dci
-1].EPType
= ED_INTERRUPT_IN
;
2804 InputContext
->EP
[Dci
-1].CErr
= 3;
2805 InputContext
->EP
[Dci
-1].EPType
= ED_INTERRUPT_OUT
;
2807 InputContext
->EP
[Dci
-1].AverageTRBLength
= 0x1000;
2808 InputContext
->EP
[Dci
-1].MaxESITPayload
= EpDesc
->MaxPacketSize
;
2810 // Get the bInterval from descriptor and init the the interval field of endpoint context
2812 if ((DeviceSpeed
== EFI_USB_SPEED_FULL
) || (DeviceSpeed
== EFI_USB_SPEED_LOW
)) {
2813 Interval
= EpDesc
->Interval
;
2815 // Hard code the interval to MAX first, need calculate through the bInterval field of Endpoint descriptor.
2817 InputContext
->EP
[Dci
-1].Interval
= 6;
2818 } else if ((DeviceSpeed
== EFI_USB_SPEED_HIGH
) || (DeviceSpeed
== EFI_USB_SPEED_SUPER
)) {
2819 Interval
= EpDesc
->Interval
;
2820 ASSERT (Interval
>= 1 && Interval
<= 16);
2822 // Refer to XHCI 1.0 spec section 6.2.3.6, table 61
2824 InputContext
->EP
[Dci
-1].Interval
= Interval
- 1;
2825 InputContext
->EP
[Dci
-1].AverageTRBLength
= 0x1000;
2826 InputContext
->EP
[Dci
-1].MaxESITPayload
= 0x0002;
2827 InputContext
->EP
[Dci
-1].MaxBurstSize
= 0x0;
2828 InputContext
->EP
[Dci
-1].CErr
= 3;
2831 if (Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1] == NULL
) {
2832 EndpointTransferRing
= AllocateZeroPool(sizeof (TRANSFER_RING
));
2833 Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1] = (VOID
*) EndpointTransferRing
;
2834 CreateTransferRing(Xhc
, TR_RING_TRB_NUMBER
, (TRANSFER_RING
*)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1]);
2838 case USB_ENDPOINT_CONTROL
:
2844 PhyAddr
= UsbHcGetPciAddrForHostAddr (
2846 ((TRANSFER_RING
*)(UINTN
)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1])->RingSeg0
,
2847 sizeof (TRB_TEMPLATE
) * TR_RING_TRB_NUMBER
2851 PhyAddr
|= ((TRANSFER_RING
*)(UINTN
)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1])->RingPCS
;
2853 InputContext
->EP
[Dci
-1].PtrLo
= XHC_LOW_32BIT (PhyAddr
);
2854 InputContext
->EP
[Dci
-1].PtrHi
= XHC_HIGH_32BIT (PhyAddr
);
2856 EpDesc
= (USB_ENDPOINT_DESCRIPTOR
*)((UINTN
)EpDesc
+ EpDesc
->Length
);
2858 IfDesc
= (USB_INTERFACE_DESCRIPTOR
*)((UINTN
)IfDesc
+ IfDesc
->Length
);
2861 InputContext
->InputControlContext
.Dword2
|= BIT0
;
2862 InputContext
->Slot
.ContextEntries
= MaxDci
;
2864 // configure endpoint
2866 ZeroMem (&CmdTrbCfgEP
, sizeof (CmdTrbCfgEP
));
2867 PhyAddr
= UsbHcGetPciAddrForHostAddr (Xhc
->MemPool
, InputContext
, sizeof (INPUT_CONTEXT_64
));
2868 CmdTrbCfgEP
.PtrLo
= XHC_LOW_32BIT (PhyAddr
);
2869 CmdTrbCfgEP
.PtrHi
= XHC_HIGH_32BIT (PhyAddr
);
2870 CmdTrbCfgEP
.CycleBit
= 1;
2871 CmdTrbCfgEP
.Type
= TRB_TYPE_CON_ENDPOINT
;
2872 CmdTrbCfgEP
.SlotId
= Xhc
->UsbDevContext
[SlotId
].SlotId
;
2873 DEBUG ((EFI_D_INFO
, "Configure Endpoint\n"));
2874 Status
= XhcCmdTransfer (
2876 (TRB_TEMPLATE
*) (UINTN
) &CmdTrbCfgEP
,
2877 XHC_GENERIC_TIMEOUT
,
2878 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
2880 ASSERT_EFI_ERROR(Status
);
2887 Evaluate the endpoint 0 context through XHCI's Evaluate_Context cmd.
2889 @param Xhc The XHCI Instance.
2890 @param SlotId The slot id to be evaluated.
2891 @param MaxPacketSize The max packet size supported by the device control transfer.
2893 @retval EFI_SUCCESS Successfully evaluate the device endpoint 0.
2898 XhcEvaluateContext (
2899 IN USB_XHCI_INSTANCE
*Xhc
,
2901 IN UINT32 MaxPacketSize
2905 CMD_TRB_EVALUATE_CONTEXT CmdTrbEvalu
;
2906 EVT_TRB_COMMAND_COMPLETION
*EvtTrb
;
2907 INPUT_CONTEXT
*InputContext
;
2908 EFI_PHYSICAL_ADDRESS PhyAddr
;
2910 ASSERT (Xhc
->UsbDevContext
[SlotId
].SlotId
!= 0);
2913 // 4.6.7 Evaluate Context
2915 InputContext
= Xhc
->UsbDevContext
[SlotId
].InputContext
;
2916 ZeroMem (InputContext
, sizeof (INPUT_CONTEXT
));
2918 InputContext
->InputControlContext
.Dword2
|= BIT1
;
2919 InputContext
->EP
[0].MaxPacketSize
= MaxPacketSize
;
2921 ZeroMem (&CmdTrbEvalu
, sizeof (CmdTrbEvalu
));
2922 PhyAddr
= UsbHcGetPciAddrForHostAddr (Xhc
->MemPool
, InputContext
, sizeof (INPUT_CONTEXT
));
2923 CmdTrbEvalu
.PtrLo
= XHC_LOW_32BIT (PhyAddr
);
2924 CmdTrbEvalu
.PtrHi
= XHC_HIGH_32BIT (PhyAddr
);
2925 CmdTrbEvalu
.CycleBit
= 1;
2926 CmdTrbEvalu
.Type
= TRB_TYPE_EVALU_CONTXT
;
2927 CmdTrbEvalu
.SlotId
= Xhc
->UsbDevContext
[SlotId
].SlotId
;
2928 DEBUG ((EFI_D_INFO
, "Evaluate context\n"));
2929 Status
= XhcCmdTransfer (
2931 (TRB_TEMPLATE
*) (UINTN
) &CmdTrbEvalu
,
2932 XHC_GENERIC_TIMEOUT
,
2933 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
2935 ASSERT (!EFI_ERROR(Status
));
2941 Evaluate the endpoint 0 context through XHCI's Evaluate_Context cmd.
2943 @param Xhc The XHCI Instance.
2944 @param SlotId The slot id to be evaluated.
2945 @param MaxPacketSize The max packet size supported by the device control transfer.
2947 @retval EFI_SUCCESS Successfully evaluate the device endpoint 0.
2952 XhcEvaluateContext64 (
2953 IN USB_XHCI_INSTANCE
*Xhc
,
2955 IN UINT32 MaxPacketSize
2959 CMD_TRB_EVALUATE_CONTEXT CmdTrbEvalu
;
2960 EVT_TRB_COMMAND_COMPLETION
*EvtTrb
;
2961 INPUT_CONTEXT_64
*InputContext
;
2962 EFI_PHYSICAL_ADDRESS PhyAddr
;
2964 ASSERT (Xhc
->UsbDevContext
[SlotId
].SlotId
!= 0);
2967 // 4.6.7 Evaluate Context
2969 InputContext
= Xhc
->UsbDevContext
[SlotId
].InputContext
;
2970 ZeroMem (InputContext
, sizeof (INPUT_CONTEXT_64
));
2972 InputContext
->InputControlContext
.Dword2
|= BIT1
;
2973 InputContext
->EP
[0].MaxPacketSize
= MaxPacketSize
;
2975 ZeroMem (&CmdTrbEvalu
, sizeof (CmdTrbEvalu
));
2976 PhyAddr
= UsbHcGetPciAddrForHostAddr (Xhc
->MemPool
, InputContext
, sizeof (INPUT_CONTEXT_64
));
2977 CmdTrbEvalu
.PtrLo
= XHC_LOW_32BIT (PhyAddr
);
2978 CmdTrbEvalu
.PtrHi
= XHC_HIGH_32BIT (PhyAddr
);
2979 CmdTrbEvalu
.CycleBit
= 1;
2980 CmdTrbEvalu
.Type
= TRB_TYPE_EVALU_CONTXT
;
2981 CmdTrbEvalu
.SlotId
= Xhc
->UsbDevContext
[SlotId
].SlotId
;
2982 DEBUG ((EFI_D_INFO
, "Evaluate context\n"));
2983 Status
= XhcCmdTransfer (
2985 (TRB_TEMPLATE
*) (UINTN
) &CmdTrbEvalu
,
2986 XHC_GENERIC_TIMEOUT
,
2987 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
2989 ASSERT (!EFI_ERROR(Status
));
2996 Evaluate the slot context for hub device through XHCI's Configure_Endpoint cmd.
2998 @param Xhc The XHCI Instance.
2999 @param SlotId The slot id to be configured.
3000 @param PortNum The total number of downstream port supported by the hub.
3001 @param TTT The TT think time of the hub device.
3002 @param MTT The multi-TT of the hub device.
3004 @retval EFI_SUCCESS Successfully configure the hub device's slot context.
3008 XhcConfigHubContext (
3009 IN USB_XHCI_INSTANCE
*Xhc
,
3018 EVT_TRB_COMMAND_COMPLETION
*EvtTrb
;
3019 INPUT_CONTEXT
*InputContext
;
3020 DEVICE_CONTEXT
*OutputContext
;
3021 CMD_TRB_CONFIG_ENDPOINT CmdTrbCfgEP
;
3022 EFI_PHYSICAL_ADDRESS PhyAddr
;
3024 ASSERT (Xhc
->UsbDevContext
[SlotId
].SlotId
!= 0);
3025 InputContext
= Xhc
->UsbDevContext
[SlotId
].InputContext
;
3026 OutputContext
= Xhc
->UsbDevContext
[SlotId
].OutputContext
;
3029 // 4.6.7 Evaluate Context
3031 ZeroMem (InputContext
, sizeof (INPUT_CONTEXT
));
3033 InputContext
->InputControlContext
.Dword2
|= BIT0
;
3036 // Copy the slot context from OutputContext to Input context
3038 CopyMem(&(InputContext
->Slot
), &(OutputContext
->Slot
), sizeof (SLOT_CONTEXT
));
3039 InputContext
->Slot
.Hub
= 1;
3040 InputContext
->Slot
.PortNum
= PortNum
;
3041 InputContext
->Slot
.TTT
= TTT
;
3042 InputContext
->Slot
.MTT
= MTT
;
3044 ZeroMem (&CmdTrbCfgEP
, sizeof (CmdTrbCfgEP
));
3045 PhyAddr
= UsbHcGetPciAddrForHostAddr (Xhc
->MemPool
, InputContext
, sizeof (INPUT_CONTEXT
));
3046 CmdTrbCfgEP
.PtrLo
= XHC_LOW_32BIT (PhyAddr
);
3047 CmdTrbCfgEP
.PtrHi
= XHC_HIGH_32BIT (PhyAddr
);
3048 CmdTrbCfgEP
.CycleBit
= 1;
3049 CmdTrbCfgEP
.Type
= TRB_TYPE_CON_ENDPOINT
;
3050 CmdTrbCfgEP
.SlotId
= Xhc
->UsbDevContext
[SlotId
].SlotId
;
3051 DEBUG ((EFI_D_INFO
, "Configure Hub Slot Context\n"));
3052 Status
= XhcCmdTransfer (
3054 (TRB_TEMPLATE
*) (UINTN
) &CmdTrbCfgEP
,
3055 XHC_GENERIC_TIMEOUT
,
3056 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
3058 ASSERT (!EFI_ERROR(Status
));
3064 Evaluate the slot context for hub device through XHCI's Configure_Endpoint cmd.
3066 @param Xhc The XHCI Instance.
3067 @param SlotId The slot id to be configured.
3068 @param PortNum The total number of downstream port supported by the hub.
3069 @param TTT The TT think time of the hub device.
3070 @param MTT The multi-TT of the hub device.
3072 @retval EFI_SUCCESS Successfully configure the hub device's slot context.
3076 XhcConfigHubContext64 (
3077 IN USB_XHCI_INSTANCE
*Xhc
,
3086 EVT_TRB_COMMAND_COMPLETION
*EvtTrb
;
3087 INPUT_CONTEXT_64
*InputContext
;
3088 DEVICE_CONTEXT_64
*OutputContext
;
3089 CMD_TRB_CONFIG_ENDPOINT CmdTrbCfgEP
;
3090 EFI_PHYSICAL_ADDRESS PhyAddr
;
3092 ASSERT (Xhc
->UsbDevContext
[SlotId
].SlotId
!= 0);
3093 InputContext
= Xhc
->UsbDevContext
[SlotId
].InputContext
;
3094 OutputContext
= Xhc
->UsbDevContext
[SlotId
].OutputContext
;
3097 // 4.6.7 Evaluate Context
3099 ZeroMem (InputContext
, sizeof (INPUT_CONTEXT_64
));
3101 InputContext
->InputControlContext
.Dword2
|= BIT0
;
3104 // Copy the slot context from OutputContext to Input context
3106 CopyMem(&(InputContext
->Slot
), &(OutputContext
->Slot
), sizeof (SLOT_CONTEXT_64
));
3107 InputContext
->Slot
.Hub
= 1;
3108 InputContext
->Slot
.PortNum
= PortNum
;
3109 InputContext
->Slot
.TTT
= TTT
;
3110 InputContext
->Slot
.MTT
= MTT
;
3112 ZeroMem (&CmdTrbCfgEP
, sizeof (CmdTrbCfgEP
));
3113 PhyAddr
= UsbHcGetPciAddrForHostAddr (Xhc
->MemPool
, InputContext
, sizeof (INPUT_CONTEXT_64
));
3114 CmdTrbCfgEP
.PtrLo
= XHC_LOW_32BIT (PhyAddr
);
3115 CmdTrbCfgEP
.PtrHi
= XHC_HIGH_32BIT (PhyAddr
);
3116 CmdTrbCfgEP
.CycleBit
= 1;
3117 CmdTrbCfgEP
.Type
= TRB_TYPE_CON_ENDPOINT
;
3118 CmdTrbCfgEP
.SlotId
= Xhc
->UsbDevContext
[SlotId
].SlotId
;
3119 DEBUG ((EFI_D_INFO
, "Configure Hub Slot Context\n"));
3120 Status
= XhcCmdTransfer (
3122 (TRB_TEMPLATE
*) (UINTN
) &CmdTrbCfgEP
,
3123 XHC_GENERIC_TIMEOUT
,
3124 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
3126 ASSERT (!EFI_ERROR(Status
));