3 XHCI transfer scheduling routines.
5 Copyright (c) 2011 - 2012, 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
;
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 Create a transfer TRB.
186 @param Xhc The XHCI Instance
187 @param Urb The urb used to construct the transfer TRB.
189 @return Created TRB or NULL
193 XhcCreateTransferTrb (
194 IN USB_XHCI_INSTANCE
*Xhc
,
199 TRANSFER_RING
*EPRing
;
208 SlotId
= XhcBusDevAddrToSlotId (Xhc
, Urb
->Ep
.BusAddr
);
210 return EFI_DEVICE_ERROR
;
213 Urb
->Finished
= FALSE
;
214 Urb
->StartDone
= FALSE
;
215 Urb
->EndDone
= FALSE
;
217 Urb
->Result
= EFI_USB_NOERROR
;
219 Dci
= XhcEndpointToDci (Urb
->Ep
.EpAddr
, (UINT8
)(Urb
->Ep
.Direction
));
221 EPRing
= (TRANSFER_RING
*)(UINTN
) Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1];
223 OutputContext
= (VOID
*)(UINTN
)Xhc
->DCBAA
[SlotId
];
224 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
225 EPType
= (UINT8
) ((DEVICE_CONTEXT
*)OutputContext
)->EP
[Dci
-1].EPType
;
227 EPType
= (UINT8
) ((DEVICE_CONTEXT_64
*)OutputContext
)->EP
[Dci
-1].EPType
;
233 XhcSyncTrsRing (Xhc
, EPRing
);
234 Urb
->TrbStart
= EPRing
->RingEnqueue
;
236 case ED_CONTROL_BIDIR
:
238 // For control transfer, create SETUP_STAGE_TRB first.
240 TrbStart
= (TRB
*)(UINTN
)EPRing
->RingEnqueue
;
241 TrbStart
->TrbCtrSetup
.bmRequestType
= Urb
->Request
->RequestType
;
242 TrbStart
->TrbCtrSetup
.bRequest
= Urb
->Request
->Request
;
243 TrbStart
->TrbCtrSetup
.wValue
= Urb
->Request
->Value
;
244 TrbStart
->TrbCtrSetup
.wIndex
= Urb
->Request
->Index
;
245 TrbStart
->TrbCtrSetup
.wLength
= Urb
->Request
->Length
;
246 TrbStart
->TrbCtrSetup
.Lenth
= 8;
247 TrbStart
->TrbCtrSetup
.IntTarget
= 0;
248 TrbStart
->TrbCtrSetup
.IOC
= 1;
249 TrbStart
->TrbCtrSetup
.IDT
= 1;
250 TrbStart
->TrbCtrSetup
.Type
= TRB_TYPE_SETUP_STAGE
;
251 if (Urb
->Ep
.Direction
== EfiUsbDataIn
) {
252 TrbStart
->TrbCtrSetup
.TRT
= 3;
253 } else if (Urb
->Ep
.Direction
== EfiUsbDataOut
) {
254 TrbStart
->TrbCtrSetup
.TRT
= 2;
256 TrbStart
->TrbCtrSetup
.TRT
= 0;
259 // Update the cycle bit
261 TrbStart
->TrbCtrSetup
.CycleBit
= EPRing
->RingPCS
& BIT0
;
265 // For control transfer, create DATA_STAGE_TRB.
267 if (Urb
->DataLen
> 0) {
268 XhcSyncTrsRing (Xhc
, EPRing
);
269 TrbStart
= (TRB
*)(UINTN
)EPRing
->RingEnqueue
;
270 TrbStart
->TrbCtrData
.TRBPtrLo
= XHC_LOW_32BIT(Urb
->Data
);
271 TrbStart
->TrbCtrData
.TRBPtrHi
= XHC_HIGH_32BIT(Urb
->Data
);
272 TrbStart
->TrbCtrData
.Lenth
= (UINT32
) Urb
->DataLen
;
273 TrbStart
->TrbCtrData
.TDSize
= 0;
274 TrbStart
->TrbCtrData
.IntTarget
= 0;
275 TrbStart
->TrbCtrData
.ISP
= 1;
276 TrbStart
->TrbCtrData
.IOC
= 1;
277 TrbStart
->TrbCtrData
.IDT
= 0;
278 TrbStart
->TrbCtrData
.CH
= 0;
279 TrbStart
->TrbCtrData
.Type
= TRB_TYPE_DATA_STAGE
;
280 if (Urb
->Ep
.Direction
== EfiUsbDataIn
) {
281 TrbStart
->TrbCtrData
.DIR = 1;
282 } else if (Urb
->Ep
.Direction
== EfiUsbDataOut
) {
283 TrbStart
->TrbCtrData
.DIR = 0;
285 TrbStart
->TrbCtrData
.DIR = 0;
288 // Update the cycle bit
290 TrbStart
->TrbCtrData
.CycleBit
= EPRing
->RingPCS
& BIT0
;
294 // For control transfer, create STATUS_STAGE_TRB.
295 // Get the pointer to next TRB for status stage use
297 XhcSyncTrsRing (Xhc
, EPRing
);
298 TrbStart
= (TRB
*)(UINTN
)EPRing
->RingEnqueue
;
299 TrbStart
->TrbCtrStatus
.IntTarget
= 0;
300 TrbStart
->TrbCtrStatus
.IOC
= 1;
301 TrbStart
->TrbCtrStatus
.CH
= 0;
302 TrbStart
->TrbCtrStatus
.Type
= TRB_TYPE_STATUS_STAGE
;
303 if (Urb
->Ep
.Direction
== EfiUsbDataIn
) {
304 TrbStart
->TrbCtrStatus
.DIR = 0;
305 } else if (Urb
->Ep
.Direction
== EfiUsbDataOut
) {
306 TrbStart
->TrbCtrStatus
.DIR = 1;
308 TrbStart
->TrbCtrStatus
.DIR = 0;
311 // Update the cycle bit
313 TrbStart
->TrbCtrStatus
.CycleBit
= EPRing
->RingPCS
& BIT0
;
315 // Update the enqueue pointer
317 XhcSyncTrsRing (Xhc
, EPRing
);
319 Urb
->TrbEnd
= (TRB_TEMPLATE
*)(UINTN
)TrbStart
;
328 TrbStart
= (TRB
*)(UINTN
)EPRing
->RingEnqueue
;
329 while (TotalLen
< Urb
->DataLen
) {
330 if ((TotalLen
+ 0x10000) >= Urb
->DataLen
) {
331 Len
= Urb
->DataLen
- TotalLen
;
335 TrbStart
= (TRB
*)(UINTN
)EPRing
->RingEnqueue
;
336 TrbStart
->TrbNormal
.TRBPtrLo
= XHC_LOW_32BIT((UINT8
*) Urb
->Data
+ TotalLen
);
337 TrbStart
->TrbNormal
.TRBPtrHi
= XHC_HIGH_32BIT((UINT8
*) Urb
->Data
+ TotalLen
);
338 TrbStart
->TrbNormal
.Lenth
= (UINT32
) Len
;
339 TrbStart
->TrbNormal
.TDSize
= 0;
340 TrbStart
->TrbNormal
.IntTarget
= 0;
341 TrbStart
->TrbNormal
.ISP
= 1;
342 TrbStart
->TrbNormal
.IOC
= 1;
343 TrbStart
->TrbNormal
.Type
= TRB_TYPE_NORMAL
;
345 // Update the cycle bit
347 TrbStart
->TrbNormal
.CycleBit
= EPRing
->RingPCS
& BIT0
;
349 XhcSyncTrsRing (Xhc
, EPRing
);
354 Urb
->TrbNum
= TrbNum
;
355 Urb
->TrbEnd
= (TRB_TEMPLATE
*)(UINTN
)TrbStart
;
358 case ED_INTERRUPT_OUT
:
359 case ED_INTERRUPT_IN
:
363 TrbStart
= (TRB
*)(UINTN
)EPRing
->RingEnqueue
;
364 while (TotalLen
< Urb
->DataLen
) {
365 if ((TotalLen
+ 0x10000) >= Urb
->DataLen
) {
366 Len
= Urb
->DataLen
- TotalLen
;
370 TrbStart
= (TRB
*)(UINTN
)EPRing
->RingEnqueue
;
371 TrbStart
->TrbNormal
.TRBPtrLo
= XHC_LOW_32BIT((UINT8
*) Urb
->Data
+ TotalLen
);
372 TrbStart
->TrbNormal
.TRBPtrHi
= XHC_HIGH_32BIT((UINT8
*) Urb
->Data
+ TotalLen
);
373 TrbStart
->TrbNormal
.Lenth
= (UINT32
) Len
;
374 TrbStart
->TrbNormal
.TDSize
= 0;
375 TrbStart
->TrbNormal
.IntTarget
= 0;
376 TrbStart
->TrbNormal
.ISP
= 1;
377 TrbStart
->TrbNormal
.IOC
= 1;
378 TrbStart
->TrbNormal
.Type
= TRB_TYPE_NORMAL
;
380 // Update the cycle bit
382 TrbStart
->TrbNormal
.CycleBit
= EPRing
->RingPCS
& BIT0
;
384 XhcSyncTrsRing (Xhc
, EPRing
);
389 Urb
->TrbNum
= TrbNum
;
390 Urb
->TrbEnd
= (TRB_TEMPLATE
*)(UINTN
)TrbStart
;
394 DEBUG ((EFI_D_INFO
, "Not supported EPType 0x%x!\n",EPType
));
404 Initialize the XHCI host controller for schedule.
406 @param Xhc The XHCI Instance to be initialized.
411 IN USB_XHCI_INSTANCE
*Xhc
417 UINT32 MaxScratchpadBufs
;
419 UINT64
*ScratchEntryBuf
;
423 // Program the Max Device Slots Enabled (MaxSlotsEn) field in the CONFIG register (5.4.7)
424 // to enable the device slots that system software is going to use.
426 Xhc
->MaxSlotsEn
= Xhc
->HcSParams1
.Data
.MaxSlots
;
427 ASSERT (Xhc
->MaxSlotsEn
>= 1 && Xhc
->MaxSlotsEn
<= 255);
428 XhcWriteOpReg (Xhc
, XHC_CONFIG_OFFSET
, Xhc
->MaxSlotsEn
);
431 // The Device Context Base Address Array entry associated with each allocated Device Slot
432 // shall contain a 64-bit pointer to the base of the associated Device Context.
433 // The Device Context Base Address Array shall contain MaxSlotsEn + 1 entries.
434 // Software shall set Device Context Base Address Array entries for unallocated Device Slots to '0'.
436 Entries
= (Xhc
->MaxSlotsEn
+ 1) * sizeof(UINT64
);
437 Dcbaa
= AllocatePages (EFI_SIZE_TO_PAGES (Entries
));
438 ASSERT (Dcbaa
!= NULL
);
439 ZeroMem (Dcbaa
, Entries
);
442 // A Scratchpad Buffer is a PAGESIZE block of system memory located on a PAGESIZE boundary.
443 // System software shall allocate the Scratchpad Buffer(s) before placing the xHC in to Run
444 // mode (Run/Stop(R/S) ='1').
446 MaxScratchpadBufs
= ((Xhc
->HcSParams2
.Data
.ScratchBufHi
) << 5) | (Xhc
->HcSParams2
.Data
.ScratchBufLo
);
447 Xhc
->MaxScratchpadBufs
= MaxScratchpadBufs
;
448 ASSERT (MaxScratchpadBufs
<= 1023);
449 if (MaxScratchpadBufs
!= 0) {
450 ScratchBuf
= AllocateAlignedPages (EFI_SIZE_TO_PAGES (MaxScratchpadBufs
* sizeof (UINT64
)), Xhc
->PageSize
);
451 ASSERT (ScratchBuf
!= NULL
);
452 ZeroMem (ScratchBuf
, MaxScratchpadBufs
* sizeof (UINT64
));
453 Xhc
->ScratchBuf
= ScratchBuf
;
455 for (Index
= 0; Index
< MaxScratchpadBufs
; Index
++) {
456 ScratchEntryBuf
= AllocateAlignedPages (EFI_SIZE_TO_PAGES (Xhc
->PageSize
), Xhc
->PageSize
);
457 ASSERT (ScratchEntryBuf
!= NULL
);
458 ZeroMem (ScratchEntryBuf
, Xhc
->PageSize
);
459 *ScratchBuf
++ = (UINT64
)(UINTN
)ScratchEntryBuf
;
463 // The Scratchpad Buffer Array contains pointers to the Scratchpad Buffers. Entry 0 of the
464 // Device Context Base Address Array points to the Scratchpad Buffer Array.
466 *(UINT64
*)Dcbaa
= (UINT64
)(UINTN
)Xhc
->ScratchBuf
;
470 // Program the Device Context Base Address Array Pointer (DCBAAP) register (5.4.6) with
471 // a 64-bit address pointing to where the Device Context Base Address Array is located.
473 Xhc
->DCBAA
= (UINT64
*)(UINTN
)Dcbaa
;
475 // Some 3rd party XHCI external cards don't support single 64-bytes width register access,
476 // So divide it to two 32-bytes width register access.
478 XhcWriteOpReg (Xhc
, XHC_DCBAAP_OFFSET
, XHC_LOW_32BIT(Xhc
->DCBAA
));
479 XhcWriteOpReg (Xhc
, XHC_DCBAAP_OFFSET
+ 4, XHC_HIGH_32BIT (Xhc
->DCBAA
));
480 DEBUG ((EFI_D_INFO
, "XhcInitSched:DCBAA=0x%x\n", (UINT64
)(UINTN
)Xhc
->DCBAA
));
483 // Define the Command Ring Dequeue Pointer by programming the Command Ring Control Register
484 // (5.4.5) with a 64-bit address pointing to the starting address of the first TRB of the Command Ring.
485 // Note: The Command Ring is 64 byte aligned, so the low order 6 bits of the Command Ring Pointer shall
488 CreateTransferRing (Xhc
, CMD_RING_TRB_NUMBER
, &Xhc
->CmdRing
);
490 // The xHC uses the Enqueue Pointer to determine when a Transfer Ring is empty. As it fetches TRBs from a
491 // Transfer Ring it checks for a Cycle bit transition. If a transition detected, the ring is empty.
492 // So we set RCS as inverted PCS init value to let Command Ring empty
494 CmdRing
= (UINT64
)(UINTN
)Xhc
->CmdRing
.RingSeg0
;
495 ASSERT ((CmdRing
& 0x3F) == 0);
496 CmdRing
|= XHC_CRCR_RCS
;
498 // Some 3rd party XHCI external cards don't support single 64-bytes width register access,
499 // So divide it to two 32-bytes width register access.
501 XhcWriteOpReg (Xhc
, XHC_CRCR_OFFSET
, XHC_LOW_32BIT(CmdRing
));
502 XhcWriteOpReg (Xhc
, XHC_CRCR_OFFSET
+ 4, XHC_HIGH_32BIT (CmdRing
));
504 DEBUG ((EFI_D_INFO
, "XhcInitSched:XHC_CRCR=0x%x\n", Xhc
->CmdRing
.RingSeg0
));
507 // Disable the 'interrupter enable' bit in USB_CMD
508 // and clear IE & IP bit in all Interrupter X Management Registers.
510 XhcClearOpRegBit (Xhc
, XHC_USBCMD_OFFSET
, XHC_USBCMD_INTE
);
511 for (Index
= 0; Index
< (UINT16
)(Xhc
->HcSParams1
.Data
.MaxIntrs
); Index
++) {
512 XhcClearRuntimeRegBit (Xhc
, XHC_IMAN_OFFSET
+ (Index
* 32), XHC_IMAN_IE
);
513 XhcSetRuntimeRegBit (Xhc
, XHC_IMAN_OFFSET
+ (Index
* 32), XHC_IMAN_IP
);
517 // Allocate EventRing for Cmd, Ctrl, Bulk, Interrupt, AsynInterrupt transfer
519 CreateEventRing (Xhc
, &Xhc
->EventRing
);
520 DEBUG ((EFI_D_INFO
, "XhcInitSched:XHC_EVENTRING=0x%x\n", Xhc
->EventRing
.EventRingSeg0
));
524 System software shall use a Reset Endpoint Command (section 4.11.4.7) to remove the Halted
525 condition in the xHC. After the successful completion of the Reset Endpoint Command, the Endpoint
526 Context is transitioned from the Halted to the Stopped state and the Transfer Ring of the endpoint is
527 reenabled. The next write to the Doorbell of the Endpoint will transition the Endpoint Context from the
528 Stopped to the Running state.
530 @param Xhc The XHCI Instance.
531 @param Urb The urb which makes the endpoint halted.
533 @retval EFI_SUCCESS The recovery is successful.
534 @retval Others Failed to recovery halted endpoint.
539 XhcRecoverHaltedEndpoint (
540 IN USB_XHCI_INSTANCE
*Xhc
,
545 EVT_TRB_COMMAND_COMPLETION
*EvtTrb
;
546 CMD_TRB_RESET_ENDPOINT CmdTrbResetED
;
547 CMD_SET_TR_DEQ_POINTER CmdSetTRDeq
;
551 Status
= EFI_SUCCESS
;
552 SlotId
= XhcBusDevAddrToSlotId (Xhc
, Urb
->Ep
.BusAddr
);
554 return EFI_DEVICE_ERROR
;
556 Dci
= XhcEndpointToDci (Urb
->Ep
.EpAddr
, (UINT8
)(Urb
->Ep
.Direction
));
559 DEBUG ((EFI_D_INFO
, "Recovery Halted Slot = %x,Dci = %x\n", SlotId
, Dci
));
562 // 1) Send Reset endpoint command to transit from halt to stop state
564 ZeroMem (&CmdTrbResetED
, sizeof (CmdTrbResetED
));
565 CmdTrbResetED
.CycleBit
= 1;
566 CmdTrbResetED
.Type
= TRB_TYPE_RESET_ENDPOINT
;
567 CmdTrbResetED
.EDID
= Dci
;
568 CmdTrbResetED
.SlotId
= SlotId
;
569 Status
= XhcCmdTransfer (
571 (TRB_TEMPLATE
*) (UINTN
) &CmdTrbResetED
,
573 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
575 ASSERT (!EFI_ERROR(Status
));
578 // 2)Set dequeue pointer
580 ZeroMem (&CmdSetTRDeq
, sizeof (CmdSetTRDeq
));
581 CmdSetTRDeq
.PtrLo
= XHC_LOW_32BIT (Urb
->Ring
->RingEnqueue
) | Urb
->Ring
->RingPCS
;
582 CmdSetTRDeq
.PtrHi
= XHC_HIGH_32BIT (Urb
->Ring
->RingEnqueue
);
583 CmdSetTRDeq
.CycleBit
= 1;
584 CmdSetTRDeq
.Type
= TRB_TYPE_SET_TR_DEQUE
;
585 CmdSetTRDeq
.Endpoint
= Dci
;
586 CmdSetTRDeq
.SlotId
= SlotId
;
587 Status
= XhcCmdTransfer (
589 (TRB_TEMPLATE
*) (UINTN
) &CmdSetTRDeq
,
591 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
593 ASSERT (!EFI_ERROR(Status
));
596 // 3)Ring the doorbell to transit from stop to active
598 XhcRingDoorBell (Xhc
, SlotId
, Dci
);
604 Create XHCI event ring.
606 @param Xhc The XHCI Instance.
607 @param EventRing The created event ring.
612 IN USB_XHCI_INSTANCE
*Xhc
,
613 OUT EVENT_RING
*EventRing
617 EVENT_RING_SEG_TABLE_ENTRY
*ERSTBase
;
619 ASSERT (EventRing
!= NULL
);
621 Buf
= AllocatePages (EFI_SIZE_TO_PAGES (sizeof (TRB_TEMPLATE
) * EVENT_RING_TRB_NUMBER
));
622 ASSERT (Buf
!= NULL
);
623 ASSERT (((UINTN
) Buf
& 0x3F) == 0);
624 ZeroMem (Buf
, sizeof (TRB_TEMPLATE
) * EVENT_RING_TRB_NUMBER
);
626 EventRing
->EventRingSeg0
= Buf
;
627 EventRing
->TrbNumber
= EVENT_RING_TRB_NUMBER
;
628 EventRing
->EventRingDequeue
= (TRB_TEMPLATE
*) EventRing
->EventRingSeg0
;
629 EventRing
->EventRingEnqueue
= (TRB_TEMPLATE
*) EventRing
->EventRingSeg0
;
631 // Software maintains an Event Ring Consumer Cycle State (CCS) bit, initializing it to '1'
632 // and toggling it every time the Event Ring Dequeue Pointer wraps back to the beginning of the Event Ring.
634 EventRing
->EventRingCCS
= 1;
636 Buf
= AllocatePages (EFI_SIZE_TO_PAGES (sizeof (EVENT_RING_SEG_TABLE_ENTRY
) * ERST_NUMBER
));
637 ASSERT (Buf
!= NULL
);
638 ASSERT (((UINTN
) Buf
& 0x3F) == 0);
639 ZeroMem (Buf
, sizeof (EVENT_RING_SEG_TABLE_ENTRY
) * ERST_NUMBER
);
641 ERSTBase
= (EVENT_RING_SEG_TABLE_ENTRY
*) Buf
;
642 EventRing
->ERSTBase
= ERSTBase
;
643 ERSTBase
->PtrLo
= XHC_LOW_32BIT (EventRing
->EventRingSeg0
);
644 ERSTBase
->PtrHi
= XHC_HIGH_32BIT (EventRing
->EventRingSeg0
);
645 ERSTBase
->RingTrbSize
= EVENT_RING_TRB_NUMBER
;
648 // Program the Interrupter Event Ring Segment Table Size (ERSTSZ) register (5.5.2.3.1)
656 // Program the Interrupter Event Ring Dequeue Pointer (ERDP) register (5.5.2.3.3)
658 // Some 3rd party XHCI external cards don't support single 64-bytes width register access,
659 // So divide it to two 32-bytes width register access.
664 XHC_LOW_32BIT((UINT64
)(UINTN
)EventRing
->EventRingDequeue
)
669 XHC_HIGH_32BIT((UINT64
)(UINTN
)EventRing
->EventRingDequeue
)
672 // Program the Interrupter Event Ring Segment Table Base Address (ERSTBA) register(5.5.2.3.2)
674 // Some 3rd party XHCI external cards don't support single 64-bytes width register access,
675 // So divide it to two 32-bytes width register access.
680 XHC_LOW_32BIT((UINT64
)(UINTN
)ERSTBase
)
684 XHC_ERSTBA_OFFSET
+ 4,
685 XHC_HIGH_32BIT((UINT64
)(UINTN
)ERSTBase
)
688 // Need set IMAN IE bit to enble the ring interrupt
690 XhcSetRuntimeRegBit (Xhc
, XHC_IMAN_OFFSET
, XHC_IMAN_IE
);
694 Create XHCI transfer ring.
696 @param Xhc The XHCI Instance.
697 @param TrbNum The number of TRB in the ring.
698 @param TransferRing The created transfer ring.
703 IN USB_XHCI_INSTANCE
*Xhc
,
705 OUT TRANSFER_RING
*TransferRing
711 Buf
= AllocatePages (EFI_SIZE_TO_PAGES (sizeof (TRB_TEMPLATE
) * TrbNum
));
712 ASSERT (Buf
!= NULL
);
713 ASSERT (((UINTN
) Buf
& 0x3F) == 0);
714 ZeroMem (Buf
, sizeof (TRB_TEMPLATE
) * TrbNum
);
716 TransferRing
->RingSeg0
= Buf
;
717 TransferRing
->TrbNumber
= TrbNum
;
718 TransferRing
->RingEnqueue
= (TRB_TEMPLATE
*) TransferRing
->RingSeg0
;
719 TransferRing
->RingDequeue
= (TRB_TEMPLATE
*) TransferRing
->RingSeg0
;
720 TransferRing
->RingPCS
= 1;
722 // 4.9.2 Transfer Ring Management
723 // To form a ring (or circular queue) a Link TRB may be inserted at the end of a ring to
724 // point to the first TRB in the ring.
726 EndTrb
= (LINK_TRB
*) ((UINTN
)Buf
+ sizeof (TRB_TEMPLATE
) * (TrbNum
- 1));
727 EndTrb
->Type
= TRB_TYPE_LINK
;
728 EndTrb
->PtrLo
= XHC_LOW_32BIT (Buf
);
729 EndTrb
->PtrHi
= XHC_HIGH_32BIT (Buf
);
731 // Toggle Cycle (TC). When set to '1', the xHC shall toggle its interpretation of the Cycle bit.
735 // Set Cycle bit as other TRB PCS init value
737 EndTrb
->CycleBit
= 0;
741 Free XHCI event ring.
743 @param Xhc The XHCI Instance.
744 @param EventRing The event ring to be freed.
750 IN USB_XHCI_INSTANCE
*Xhc
,
751 IN EVENT_RING
*EventRing
755 EVENT_RING_SEG_TABLE_ENTRY
*TablePtr
;
757 EVENT_RING_SEG_TABLE_ENTRY
*EventRingPtr
;
759 if(EventRing
->EventRingSeg0
== NULL
) {
764 // Get the Event Ring Segment Table base address
766 TablePtr
= (EVENT_RING_SEG_TABLE_ENTRY
*)(EventRing
->ERSTBase
);
769 // Get all the TRBs Ring and release
771 for (Index
= 0; Index
< ERST_NUMBER
; Index
++) {
772 EventRingPtr
= TablePtr
+ Index
;
773 RingBuf
= (VOID
*)(UINTN
)(EventRingPtr
->PtrLo
| LShiftU64 ((UINT64
)EventRingPtr
->PtrHi
, 32));
775 if(RingBuf
!= NULL
) {
776 FreePages (RingBuf
, EFI_SIZE_TO_PAGES (sizeof (TRB_TEMPLATE
) * EVENT_RING_TRB_NUMBER
));
777 ZeroMem (EventRingPtr
, sizeof (EVENT_RING_SEG_TABLE_ENTRY
));
781 FreePages (TablePtr
, EFI_SIZE_TO_PAGES (sizeof (EVENT_RING_SEG_TABLE_ENTRY
) * ERST_NUMBER
));
786 Free the resouce allocated at initializing schedule.
788 @param Xhc The XHCI Instance.
793 IN USB_XHCI_INSTANCE
*Xhc
799 if (Xhc
->ScratchBuf
!= NULL
) {
800 ScratchBuf
= Xhc
->ScratchBuf
;
801 for (Index
= 0; Index
< Xhc
->MaxScratchpadBufs
; Index
++) {
802 FreeAlignedPages ((VOID
*)(UINTN
)*ScratchBuf
++, EFI_SIZE_TO_PAGES (Xhc
->PageSize
));
804 FreeAlignedPages (Xhc
->ScratchBuf
, EFI_SIZE_TO_PAGES (Xhc
->MaxScratchpadBufs
* sizeof (UINT64
)));
807 if (Xhc
->DCBAA
!= NULL
) {
808 FreePages (Xhc
->DCBAA
, EFI_SIZE_TO_PAGES((Xhc
->MaxSlotsEn
+ 1) * sizeof(UINT64
)));
812 if (Xhc
->CmdRing
.RingSeg0
!= NULL
){
813 FreePages (Xhc
->CmdRing
.RingSeg0
, EFI_SIZE_TO_PAGES (sizeof (TRB_TEMPLATE
) * CMD_RING_TRB_NUMBER
));
814 Xhc
->CmdRing
.RingSeg0
= NULL
;
817 XhcFreeEventRing (Xhc
,&Xhc
->EventRing
);
821 Check if the Trb is a transaction of the URBs in XHCI's asynchronous transfer list.
823 @param Xhc The XHCI Instance.
824 @param Trb The TRB to be checked.
825 @param Urb The pointer to the matched Urb.
827 @retval TRUE The Trb is matched with a transaction of the URBs in the async list.
828 @retval FALSE The Trb is not matched with any URBs in the async list.
833 IN USB_XHCI_INSTANCE
*Xhc
,
834 IN TRB_TEMPLATE
*Trb
,
840 TRB_TEMPLATE
*CheckedTrb
;
844 EFI_LIST_FOR_EACH_SAFE (Entry
, Next
, &Xhc
->AsyncIntTransfers
) {
845 CheckedUrb
= EFI_LIST_CONTAINER (Entry
, URB
, UrbList
);
846 CheckedTrb
= CheckedUrb
->TrbStart
;
847 for (Index
= 0; Index
< CheckedUrb
->TrbNum
; Index
++) {
848 if (Trb
== CheckedTrb
) {
853 if ((UINTN
)CheckedTrb
>= ((UINTN
) CheckedUrb
->Ring
->RingSeg0
+ sizeof (TRB_TEMPLATE
) * CheckedUrb
->Ring
->TrbNumber
)) {
854 CheckedTrb
= (TRB_TEMPLATE
*) CheckedUrb
->Ring
->RingSeg0
;
863 Check if the Trb is a transaction of the URB.
865 @param Trb The TRB to be checked
866 @param Urb The transfer ring to be checked.
868 @retval TRUE It is a transaction of the URB.
869 @retval FALSE It is not any transaction of the URB.
874 IN TRB_TEMPLATE
*Trb
,
878 TRB_TEMPLATE
*CheckedTrb
;
881 CheckedTrb
= Urb
->Ring
->RingSeg0
;
883 ASSERT (Urb
->Ring
->TrbNumber
== CMD_RING_TRB_NUMBER
|| Urb
->Ring
->TrbNumber
== TR_RING_TRB_NUMBER
);
885 for (Index
= 0; Index
< Urb
->Ring
->TrbNumber
; Index
++) {
886 if (Trb
== CheckedTrb
) {
896 Check the URB's execution result and update the URB's
899 @param Xhc The XHCI Instance.
900 @param Urb The URB to check result.
902 @return Whether the result of URB transfer is finialized.
907 IN USB_XHCI_INSTANCE
*Xhc
,
911 EVT_TRB_TRANSFER
*EvtTrb
;
912 TRB_TEMPLATE
*TRBPtr
;
922 ASSERT ((Xhc
!= NULL
) && (Urb
!= NULL
));
924 Status
= EFI_SUCCESS
;
932 if (XhcIsHalt (Xhc
) || XhcIsSysError (Xhc
)) {
933 Urb
->Result
|= EFI_USB_ERR_SYSTEM
;
934 Status
= EFI_DEVICE_ERROR
;
939 // Traverse the event ring to find out all new events from the previous check.
941 XhcSyncEventRing (Xhc
, &Xhc
->EventRing
);
942 for (Index
= 0; Index
< Xhc
->EventRing
.TrbNumber
; Index
++) {
943 Status
= XhcCheckNewEvent (Xhc
, &Xhc
->EventRing
, ((TRB_TEMPLATE
**)&EvtTrb
));
944 if (Status
== EFI_NOT_READY
) {
946 // All new events are handled, return directly.
952 // Only handle COMMAND_COMPLETETION_EVENT and TRANSFER_EVENT.
954 if ((EvtTrb
->Type
!= TRB_TYPE_COMMAND_COMPLT_EVENT
) && (EvtTrb
->Type
!= TRB_TYPE_TRANS_EVENT
)) {
958 TRBPtr
= (TRB_TEMPLATE
*)(UINTN
)(EvtTrb
->TRBPtrLo
| LShiftU64 ((UINT64
) EvtTrb
->TRBPtrHi
, 32));
961 // Update the status of Urb according to the finished event regardless of whether
962 // the urb is current checked one or in the XHCI's async transfer list.
963 // This way is used to avoid that those completed async transfer events don't get
964 // handled in time and are flushed by newer coming events.
966 if (IsTransferRingTrb (TRBPtr
, Urb
)) {
968 } else if (IsAsyncIntTrb (Xhc
, TRBPtr
, &AsyncUrb
)) {
969 CheckedUrb
= AsyncUrb
;
974 switch (EvtTrb
->Completecode
) {
975 case TRB_COMPLETION_STALL_ERROR
:
976 CheckedUrb
->Result
|= EFI_USB_ERR_STALL
;
977 CheckedUrb
->Finished
= TRUE
;
978 DEBUG ((EFI_D_ERROR
, "XhcCheckUrbResult: STALL_ERROR! Completecode = %x\n",EvtTrb
->Completecode
));
981 case TRB_COMPLETION_BABBLE_ERROR
:
982 CheckedUrb
->Result
|= EFI_USB_ERR_BABBLE
;
983 CheckedUrb
->Finished
= TRUE
;
984 DEBUG ((EFI_D_ERROR
, "XhcCheckUrbResult: BABBLE_ERROR! Completecode = %x\n",EvtTrb
->Completecode
));
987 case TRB_COMPLETION_DATA_BUFFER_ERROR
:
988 CheckedUrb
->Result
|= EFI_USB_ERR_BUFFER
;
989 CheckedUrb
->Finished
= TRUE
;
990 DEBUG ((EFI_D_ERROR
, "XhcCheckUrbResult: ERR_BUFFER! Completecode = %x\n",EvtTrb
->Completecode
));
993 case TRB_COMPLETION_USB_TRANSACTION_ERROR
:
994 CheckedUrb
->Result
|= EFI_USB_ERR_TIMEOUT
;
995 CheckedUrb
->Finished
= TRUE
;
996 DEBUG ((EFI_D_ERROR
, "XhcCheckUrbResult: TRANSACTION_ERROR! Completecode = %x\n",EvtTrb
->Completecode
));
999 case TRB_COMPLETION_SHORT_PACKET
:
1000 case TRB_COMPLETION_SUCCESS
:
1001 if (EvtTrb
->Completecode
== TRB_COMPLETION_SHORT_PACKET
) {
1002 DEBUG ((EFI_D_ERROR
, "XhcCheckUrbResult: short packet happens!\n"));
1005 TRBType
= (UINT8
) (TRBPtr
->Type
);
1006 if ((TRBType
== TRB_TYPE_DATA_STAGE
) ||
1007 (TRBType
== TRB_TYPE_NORMAL
) ||
1008 (TRBType
== TRB_TYPE_ISOCH
)) {
1009 CheckedUrb
->Completed
+= (CheckedUrb
->DataLen
- EvtTrb
->Lenth
);
1015 DEBUG ((EFI_D_ERROR
, "Transfer Default Error Occur! Completecode = 0x%x!\n",EvtTrb
->Completecode
));
1016 CheckedUrb
->Result
|= EFI_USB_ERR_TIMEOUT
;
1017 CheckedUrb
->Finished
= TRUE
;
1022 // Only check first and end Trb event address
1024 if (TRBPtr
== CheckedUrb
->TrbStart
) {
1025 CheckedUrb
->StartDone
= TRUE
;
1028 if (TRBPtr
== CheckedUrb
->TrbEnd
) {
1029 CheckedUrb
->EndDone
= TRUE
;
1032 if (CheckedUrb
->StartDone
&& CheckedUrb
->EndDone
) {
1033 CheckedUrb
->Finished
= TRUE
;
1034 CheckedUrb
->EvtTrb
= (TRB_TEMPLATE
*)EvtTrb
;
1041 // Advance event ring to last available entry
1043 // Some 3rd party XHCI external cards don't support single 64-bytes width register access,
1044 // So divide it to two 32-bytes width register access.
1046 Low
= XhcReadRuntimeReg (Xhc
, XHC_ERDP_OFFSET
);
1047 High
= XhcReadRuntimeReg (Xhc
, XHC_ERDP_OFFSET
+ 4);
1048 XhcDequeue
= (UINT64
)(LShiftU64((UINT64
)High
, 32) | Low
);
1050 if ((XhcDequeue
& (~0x0F)) != ((UINT64
)(UINTN
)Xhc
->EventRing
.EventRingDequeue
& (~0x0F))) {
1052 // Some 3rd party XHCI external cards don't support single 64-bytes width register access,
1053 // So divide it to two 32-bytes width register access.
1055 XhcWriteRuntimeReg (Xhc
, XHC_ERDP_OFFSET
, XHC_LOW_32BIT (Xhc
->EventRing
.EventRingDequeue
) | BIT3
);
1056 XhcWriteRuntimeReg (Xhc
, XHC_ERDP_OFFSET
+ 4, XHC_HIGH_32BIT (Xhc
->EventRing
.EventRingDequeue
));
1064 Execute the transfer by polling the URB. This is a synchronous operation.
1066 @param Xhc The XHCI Instance.
1067 @param CmdTransfer The executed URB is for cmd transfer or not.
1068 @param Urb The URB to execute.
1069 @param Timeout The time to wait before abort, in millisecond.
1071 @return EFI_DEVICE_ERROR The transfer failed due to transfer error.
1072 @return EFI_TIMEOUT The transfer failed due to time out.
1073 @return EFI_SUCCESS The transfer finished OK.
1078 IN USB_XHCI_INSTANCE
*Xhc
,
1079 IN BOOLEAN CmdTransfer
,
1094 SlotId
= XhcBusDevAddrToSlotId (Xhc
, Urb
->Ep
.BusAddr
);
1096 return EFI_DEVICE_ERROR
;
1098 Dci
= XhcEndpointToDci (Urb
->Ep
.EpAddr
, (UINT8
)(Urb
->Ep
.Direction
));
1102 Status
= EFI_SUCCESS
;
1103 Loop
= (Timeout
* XHC_1_MILLISECOND
/ XHC_POLL_DELAY
) + 1;
1108 XhcRingDoorBell (Xhc
, SlotId
, Dci
);
1110 for (Index
= 0; Index
< Loop
; Index
++) {
1111 Status
= XhcCheckUrbResult (Xhc
, Urb
);
1112 if (Urb
->Finished
) {
1115 gBS
->Stall (XHC_POLL_DELAY
);
1118 if (Index
== Loop
) {
1119 Urb
->Result
= EFI_USB_ERR_TIMEOUT
;
1126 Delete a single asynchronous interrupt transfer for
1127 the device and endpoint.
1129 @param Xhc The XHCI Instance.
1130 @param BusAddr The logical device address assigned by UsbBus driver.
1131 @param EpNum The endpoint of the target.
1133 @retval EFI_SUCCESS An asynchronous transfer is removed.
1134 @retval EFI_NOT_FOUND No transfer for the device is found.
1138 XhciDelAsyncIntTransfer (
1139 IN USB_XHCI_INSTANCE
*Xhc
,
1147 EFI_USB_DATA_DIRECTION Direction
;
1149 Direction
= ((EpNum
& 0x80) != 0) ? EfiUsbDataIn
: EfiUsbDataOut
;
1154 EFI_LIST_FOR_EACH_SAFE (Entry
, Next
, &Xhc
->AsyncIntTransfers
) {
1155 Urb
= EFI_LIST_CONTAINER (Entry
, URB
, UrbList
);
1156 if ((Urb
->Ep
.BusAddr
== BusAddr
) &&
1157 (Urb
->Ep
.EpAddr
== EpNum
) &&
1158 (Urb
->Ep
.Direction
== Direction
)) {
1159 RemoveEntryList (&Urb
->UrbList
);
1160 FreePool (Urb
->Data
);
1166 return EFI_NOT_FOUND
;
1170 Remove all the asynchronous interrutp transfers.
1172 @param Xhc The XHCI Instance.
1176 XhciDelAllAsyncIntTransfers (
1177 IN USB_XHCI_INSTANCE
*Xhc
1184 EFI_LIST_FOR_EACH_SAFE (Entry
, Next
, &Xhc
->AsyncIntTransfers
) {
1185 Urb
= EFI_LIST_CONTAINER (Entry
, URB
, UrbList
);
1186 RemoveEntryList (&Urb
->UrbList
);
1187 FreePool (Urb
->Data
);
1193 Update the queue head for next round of asynchronous transfer
1195 @param Xhc The XHCI Instance.
1196 @param Urb The URB to update
1200 XhcUpdateAsyncRequest (
1201 IN USB_XHCI_INSTANCE
*Xhc
,
1207 if (Urb
->Result
== EFI_USB_NOERROR
) {
1208 Status
= XhcCreateTransferTrb (Xhc
, Urb
);
1209 if (EFI_ERROR (Status
)) {
1212 Status
= RingIntTransferDoorBell (Xhc
, Urb
);
1213 if (EFI_ERROR (Status
)) {
1221 Interrupt transfer periodic check handler.
1223 @param Event Interrupt event.
1224 @param Context Pointer to USB_XHCI_INSTANCE.
1229 XhcMonitorAsyncRequests (
1234 USB_XHCI_INSTANCE
*Xhc
;
1243 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1245 Xhc
= (USB_XHCI_INSTANCE
*) Context
;
1247 EFI_LIST_FOR_EACH_SAFE (Entry
, Next
, &Xhc
->AsyncIntTransfers
) {
1248 Urb
= EFI_LIST_CONTAINER (Entry
, URB
, UrbList
);
1251 // Make sure that the device is available before every check.
1253 SlotId
= XhcBusDevAddrToSlotId (Xhc
, Urb
->Ep
.BusAddr
);
1259 // Check the result of URB execution. If it is still
1260 // active, check the next one.
1262 Status
= XhcCheckUrbResult (Xhc
, Urb
);
1264 if (!Urb
->Finished
) {
1269 // Allocate a buffer then copy the transferred data for user.
1270 // If failed to allocate the buffer, update the URB for next
1271 // round of transfer. Ignore the data of this round.
1274 if (Urb
->Result
== EFI_USB_NOERROR
) {
1275 ASSERT (Urb
->Completed
<= Urb
->DataLen
);
1277 ProcBuf
= AllocateZeroPool (Urb
->Completed
);
1279 if (ProcBuf
== NULL
) {
1280 XhcUpdateAsyncRequest (Xhc
, Urb
);
1284 CopyMem (ProcBuf
, Urb
->Data
, Urb
->Completed
);
1288 // Leave error recovery to its related device driver. A
1289 // common case of the error recovery is to re-submit the
1290 // interrupt transfer which is linked to the head of the
1291 // list. This function scans from head to tail. So the
1292 // re-submitted interrupt transfer's callback function
1293 // will not be called again in this round. Don't touch this
1294 // URB after the callback, it may have been removed by the
1297 if (Urb
->Callback
!= NULL
) {
1299 // Restore the old TPL, USB bus maybe connect device in
1300 // his callback. Some drivers may has a lower TPL restriction.
1302 gBS
->RestoreTPL (OldTpl
);
1303 (Urb
->Callback
) (ProcBuf
, Urb
->Completed
, Urb
->Context
, Urb
->Result
);
1304 OldTpl
= gBS
->RaiseTPL (XHC_TPL
);
1307 if (ProcBuf
!= NULL
) {
1308 gBS
->FreePool (ProcBuf
);
1311 XhcUpdateAsyncRequest (Xhc
, Urb
);
1313 gBS
->RestoreTPL (OldTpl
);
1317 Monitor the port status change. Enable/Disable device slot if there is a device attached/detached.
1319 @param Xhc The XHCI Instance.
1320 @param ParentRouteChart The route string pointed to the parent device if it exists.
1321 @param Port The port to be polled.
1322 @param PortState The port state.
1324 @retval EFI_SUCCESS Successfully enable/disable device slot according to port state.
1325 @retval Others Should not appear.
1330 XhcPollPortStatusChange (
1331 IN USB_XHCI_INSTANCE
*Xhc
,
1332 IN USB_DEV_ROUTE ParentRouteChart
,
1334 IN EFI_USB_PORT_STATUS
*PortState
1340 USB_DEV_ROUTE RouteChart
;
1342 Status
= EFI_SUCCESS
;
1344 if (ParentRouteChart
.Dword
== 0) {
1345 RouteChart
.Route
.RouteString
= 0;
1346 RouteChart
.Route
.RootPortNum
= Port
+ 1;
1347 RouteChart
.Route
.TierNum
= 1;
1350 RouteChart
.Route
.RouteString
= ParentRouteChart
.Route
.RouteString
| (Port
<< (4 * (ParentRouteChart
.Route
.TierNum
- 1)));
1352 RouteChart
.Route
.RouteString
= ParentRouteChart
.Route
.RouteString
| (15 << (4 * (ParentRouteChart
.Route
.TierNum
- 1)));
1354 RouteChart
.Route
.RootPortNum
= ParentRouteChart
.Route
.RootPortNum
;
1355 RouteChart
.Route
.TierNum
= ParentRouteChart
.Route
.TierNum
+ 1;
1358 if (((PortState
->PortStatus
& USB_PORT_STAT_ENABLE
) != 0) &&
1359 ((PortState
->PortStatus
& USB_PORT_STAT_CONNECTION
) != 0)) {
1361 // Has a device attached, Identify device speed after port is enabled.
1363 Speed
= EFI_USB_SPEED_FULL
;
1364 if ((PortState
->PortStatus
& USB_PORT_STAT_LOW_SPEED
) != 0) {
1365 Speed
= EFI_USB_SPEED_LOW
;
1366 } else if ((PortState
->PortStatus
& USB_PORT_STAT_HIGH_SPEED
) != 0) {
1367 Speed
= EFI_USB_SPEED_HIGH
;
1368 } else if ((PortState
->PortStatus
& USB_PORT_STAT_SUPER_SPEED
) != 0) {
1369 Speed
= EFI_USB_SPEED_SUPER
;
1372 // Execute Enable_Slot cmd for attached device, initialize device context and assign device address.
1374 SlotId
= XhcRouteStringToSlotId (Xhc
, RouteChart
);
1376 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1377 Status
= XhcInitializeDeviceSlot (Xhc
, ParentRouteChart
, Port
, RouteChart
, Speed
);
1379 Status
= XhcInitializeDeviceSlot64 (Xhc
, ParentRouteChart
, Port
, RouteChart
, Speed
);
1381 ASSERT_EFI_ERROR (Status
);
1383 } else if ((PortState
->PortStatus
& USB_PORT_STAT_CONNECTION
) == 0) {
1385 // Device is detached. Disable the allocated device slot and release resource.
1387 SlotId
= XhcRouteStringToSlotId (Xhc
, RouteChart
);
1389 if (Xhc
->HcCParams
.Data
.Csz
== 0) {
1390 Status
= XhcDisableSlotCmd (Xhc
, SlotId
);
1392 Status
= XhcDisableSlotCmd64 (Xhc
, SlotId
);
1394 ASSERT_EFI_ERROR (Status
);
1402 Calculate the device context index by endpoint address and direction.
1404 @param EpAddr The target endpoint number.
1405 @param Direction The direction of the target endpoint.
1407 @return The device context index of endpoint.
1421 Index
= (UINT8
) (2 * EpAddr
);
1422 if (Direction
== EfiUsbDataIn
) {
1430 Find out the actual device address according to the requested device address from UsbBus.
1432 @param Xhc The XHCI Instance.
1433 @param BusDevAddr The requested device address by UsbBus upper driver.
1435 @return The actual device address assigned to the device.
1440 XhcBusDevAddrToSlotId (
1441 IN USB_XHCI_INSTANCE
*Xhc
,
1447 for (Index
= 0; Index
< 255; Index
++) {
1448 if (Xhc
->UsbDevContext
[Index
+ 1].Enabled
&&
1449 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
!= 0) &&
1450 (Xhc
->UsbDevContext
[Index
+ 1].BusDevAddr
== BusDevAddr
)) {
1459 return Xhc
->UsbDevContext
[Index
+ 1].SlotId
;
1463 Find out the slot id according to the device's route string.
1465 @param Xhc The XHCI Instance.
1466 @param RouteString The route string described the device location.
1468 @return The slot id used by the device.
1473 XhcRouteStringToSlotId (
1474 IN USB_XHCI_INSTANCE
*Xhc
,
1475 IN USB_DEV_ROUTE RouteString
1480 for (Index
= 0; Index
< 255; Index
++) {
1481 if (Xhc
->UsbDevContext
[Index
+ 1].Enabled
&&
1482 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
!= 0) &&
1483 (Xhc
->UsbDevContext
[Index
+ 1].RouteString
.Dword
== RouteString
.Dword
)) {
1492 return Xhc
->UsbDevContext
[Index
+ 1].SlotId
;
1496 Synchronize the specified event ring to update the enqueue and dequeue pointer.
1498 @param Xhc The XHCI Instance.
1499 @param EvtRing The event ring to sync.
1501 @retval EFI_SUCCESS The event ring is synchronized successfully.
1507 IN USB_XHCI_INSTANCE
*Xhc
,
1508 IN EVENT_RING
*EvtRing
1512 TRB_TEMPLATE
*EvtTrb1
;
1514 ASSERT (EvtRing
!= NULL
);
1517 // Calculate the EventRingEnqueue and EventRingCCS.
1518 // Note: only support single Segment
1520 EvtTrb1
= EvtRing
->EventRingDequeue
;
1522 for (Index
= 0; Index
< EvtRing
->TrbNumber
; Index
++) {
1523 if (EvtTrb1
->CycleBit
!= EvtRing
->EventRingCCS
) {
1529 if ((UINTN
)EvtTrb1
>= ((UINTN
) EvtRing
->EventRingSeg0
+ sizeof (TRB_TEMPLATE
) * EvtRing
->TrbNumber
)) {
1530 EvtTrb1
= EvtRing
->EventRingSeg0
;
1531 EvtRing
->EventRingCCS
= (EvtRing
->EventRingCCS
) ? 0 : 1;
1535 if (Index
< EvtRing
->TrbNumber
) {
1536 EvtRing
->EventRingEnqueue
= EvtTrb1
;
1545 Synchronize the specified transfer ring to update the enqueue and dequeue pointer.
1547 @param Xhc The XHCI Instance.
1548 @param TrsRing The transfer ring to sync.
1550 @retval EFI_SUCCESS The transfer ring is synchronized successfully.
1556 IN USB_XHCI_INSTANCE
*Xhc
,
1557 IN TRANSFER_RING
*TrsRing
1561 TRB_TEMPLATE
*TrsTrb
;
1563 ASSERT (TrsRing
!= NULL
);
1565 // Calculate the latest RingEnqueue and RingPCS
1567 TrsTrb
= TrsRing
->RingEnqueue
;
1568 ASSERT (TrsTrb
!= NULL
);
1570 for (Index
= 0; Index
< TrsRing
->TrbNumber
; Index
++) {
1571 if (TrsTrb
->CycleBit
!= (TrsRing
->RingPCS
& BIT0
)) {
1575 if ((UINT8
) TrsTrb
->Type
== TRB_TYPE_LINK
) {
1576 ASSERT (((LINK_TRB
*)TrsTrb
)->TC
!= 0);
1578 // set cycle bit in Link TRB as normal
1580 ((LINK_TRB
*)TrsTrb
)->CycleBit
= TrsRing
->RingPCS
& BIT0
;
1582 // Toggle PCS maintained by software
1584 TrsRing
->RingPCS
= (TrsRing
->RingPCS
& BIT0
) ? 0 : 1;
1585 TrsTrb
= (TRB_TEMPLATE
*)(UINTN
)((TrsTrb
->Parameter1
| LShiftU64 ((UINT64
)TrsTrb
->Parameter2
, 32)) & ~0x0F);
1589 ASSERT (Index
!= TrsRing
->TrbNumber
);
1591 if (TrsTrb
!= TrsRing
->RingEnqueue
) {
1592 TrsRing
->RingEnqueue
= TrsTrb
;
1596 // Clear the Trb context for enqueue, but reserve the PCS bit
1598 TrsTrb
->Parameter1
= 0;
1599 TrsTrb
->Parameter2
= 0;
1603 TrsTrb
->Control
= 0;
1609 Check if there is a new generated event.
1611 @param Xhc The XHCI Instance.
1612 @param EvtRing The event ring to check.
1613 @param NewEvtTrb The new event TRB found.
1615 @retval EFI_SUCCESS Found a new event TRB at the event ring.
1616 @retval EFI_NOT_READY The event ring has no new event.
1622 IN USB_XHCI_INSTANCE
*Xhc
,
1623 IN EVENT_RING
*EvtRing
,
1624 OUT TRB_TEMPLATE
**NewEvtTrb
1628 TRB_TEMPLATE
*EvtTrb
;
1630 ASSERT (EvtRing
!= NULL
);
1632 EvtTrb
= EvtRing
->EventRingDequeue
;
1633 *NewEvtTrb
= EvtRing
->EventRingDequeue
;
1635 if (EvtRing
->EventRingDequeue
== EvtRing
->EventRingEnqueue
) {
1636 return EFI_NOT_READY
;
1639 Status
= EFI_SUCCESS
;
1641 EvtRing
->EventRingDequeue
++;
1643 // If the dequeue pointer is beyond the ring, then roll-back it to the begining of the ring.
1645 if ((UINTN
)EvtRing
->EventRingDequeue
>= ((UINTN
) EvtRing
->EventRingSeg0
+ sizeof (TRB_TEMPLATE
) * EvtRing
->TrbNumber
)) {
1646 EvtRing
->EventRingDequeue
= EvtRing
->EventRingSeg0
;
1653 Ring the door bell to notify XHCI there is a transaction to be executed.
1655 @param Xhc The XHCI Instance.
1656 @param SlotId The slot id of the target device.
1657 @param Dci The device context index of the target slot or endpoint.
1659 @retval EFI_SUCCESS Successfully ring the door bell.
1665 IN USB_XHCI_INSTANCE
*Xhc
,
1671 XhcWriteDoorBellReg (Xhc
, 0, 0);
1673 XhcWriteDoorBellReg (Xhc
, SlotId
* sizeof (UINT32
), Dci
);
1680 Ring the door bell to notify XHCI there is a transaction to be executed through URB.
1682 @param Xhc The XHCI Instance.
1683 @param Urb The URB to be rung.
1685 @retval EFI_SUCCESS Successfully ring the door bell.
1689 RingIntTransferDoorBell (
1690 IN USB_XHCI_INSTANCE
*Xhc
,
1697 SlotId
= XhcBusDevAddrToSlotId (Xhc
, Urb
->Ep
.BusAddr
);
1698 Dci
= XhcEndpointToDci (Urb
->Ep
.EpAddr
, (UINT8
)(Urb
->Ep
.Direction
));
1699 XhcRingDoorBell (Xhc
, SlotId
, Dci
);
1704 Assign and initialize the device slot for a new device.
1706 @param Xhc The XHCI Instance.
1707 @param ParentRouteChart The route string pointed to the parent device.
1708 @param ParentPort The port at which the device is located.
1709 @param RouteChart The route string pointed to the device.
1710 @param DeviceSpeed The device speed.
1712 @retval EFI_SUCCESS Successfully assign a slot to the device and assign an address to it.
1717 XhcInitializeDeviceSlot (
1718 IN USB_XHCI_INSTANCE
*Xhc
,
1719 IN USB_DEV_ROUTE ParentRouteChart
,
1720 IN UINT16 ParentPort
,
1721 IN USB_DEV_ROUTE RouteChart
,
1722 IN UINT8 DeviceSpeed
1726 EVT_TRB_COMMAND_COMPLETION
*EvtTrb
;
1727 INPUT_CONTEXT
*InputContext
;
1728 DEVICE_CONTEXT
*OutputContext
;
1729 TRANSFER_RING
*EndpointTransferRing
;
1730 CMD_TRB_ADDRESS_DEVICE CmdTrbAddr
;
1731 UINT8 DeviceAddress
;
1732 CMD_TRB_ENABLE_SLOT CmdTrb
;
1735 DEVICE_CONTEXT
*ParentDeviceContext
;
1737 ZeroMem (&CmdTrb
, sizeof (CMD_TRB_ENABLE_SLOT
));
1738 CmdTrb
.CycleBit
= 1;
1739 CmdTrb
.Type
= TRB_TYPE_EN_SLOT
;
1741 Status
= XhcCmdTransfer (
1743 (TRB_TEMPLATE
*) (UINTN
) &CmdTrb
,
1744 XHC_GENERIC_TIMEOUT
,
1745 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
1747 ASSERT_EFI_ERROR (Status
);
1748 ASSERT (EvtTrb
->SlotId
<= Xhc
->MaxSlotsEn
);
1749 DEBUG ((EFI_D_INFO
, "Enable Slot Successfully, The Slot ID = 0x%x\n", EvtTrb
->SlotId
));
1750 SlotId
= (UINT8
)EvtTrb
->SlotId
;
1751 ASSERT (SlotId
!= 0);
1753 ZeroMem (&Xhc
->UsbDevContext
[SlotId
], sizeof (USB_DEV_CONTEXT
));
1754 Xhc
->UsbDevContext
[SlotId
].Enabled
= TRUE
;
1755 Xhc
->UsbDevContext
[SlotId
].SlotId
= SlotId
;
1756 Xhc
->UsbDevContext
[SlotId
].RouteString
.Dword
= RouteChart
.Dword
;
1757 Xhc
->UsbDevContext
[SlotId
].ParentRouteString
.Dword
= ParentRouteChart
.Dword
;
1760 // 4.3.3 Device Slot Initialization
1761 // 1) Allocate an Input Context data structure (6.2.5) and initialize all fields to '0'.
1763 InputContext
= AllocatePages (EFI_SIZE_TO_PAGES (sizeof (INPUT_CONTEXT
)));
1764 ASSERT (InputContext
!= NULL
);
1765 ASSERT (((UINTN
) InputContext
& 0x3F) == 0);
1766 ZeroMem (InputContext
, sizeof (INPUT_CONTEXT
));
1768 Xhc
->UsbDevContext
[SlotId
].InputContext
= (VOID
*) InputContext
;
1771 // 2) Initialize the Input Control Context (6.2.5.1) of the Input Context by setting the A0 and A1
1772 // flags to '1'. These flags indicate that the Slot Context and the Endpoint 0 Context of the Input
1773 // Context are affected by the command.
1775 InputContext
->InputControlContext
.Dword2
|= (BIT0
| BIT1
);
1778 // 3) Initialize the Input Slot Context data structure
1780 InputContext
->Slot
.RouteString
= RouteChart
.Route
.RouteString
;
1781 InputContext
->Slot
.Speed
= DeviceSpeed
+ 1;
1782 InputContext
->Slot
.ContextEntries
= 1;
1783 InputContext
->Slot
.RootHubPortNum
= RouteChart
.Route
.RootPortNum
;
1785 if (RouteChart
.Route
.RouteString
) {
1787 // The device is behind of hub device.
1789 ParentSlotId
= XhcRouteStringToSlotId(Xhc
, ParentRouteChart
);
1790 ASSERT (ParentSlotId
!= 0);
1792 //if the Full/Low device attached to a High Speed Hub, Init the TTPortNum and TTHubSlotId field of slot context
1794 ParentDeviceContext
= (DEVICE_CONTEXT
*)Xhc
->UsbDevContext
[ParentSlotId
].OutputContext
;
1795 if ((ParentDeviceContext
->Slot
.TTPortNum
== 0) &&
1796 (ParentDeviceContext
->Slot
.TTHubSlotId
== 0)) {
1797 if ((ParentDeviceContext
->Slot
.Speed
== (EFI_USB_SPEED_HIGH
+ 1)) && (DeviceSpeed
< EFI_USB_SPEED_HIGH
)) {
1799 // Full/Low device attached to High speed hub port that isolates the high speed signaling
1800 // environment from Full/Low speed signaling environment for a device
1802 InputContext
->Slot
.TTPortNum
= ParentPort
;
1803 InputContext
->Slot
.TTHubSlotId
= ParentSlotId
;
1807 // Inherit the TT parameters from parent device.
1809 InputContext
->Slot
.TTPortNum
= ParentDeviceContext
->Slot
.TTPortNum
;
1810 InputContext
->Slot
.TTHubSlotId
= ParentDeviceContext
->Slot
.TTHubSlotId
;
1812 // If the device is a High speed device then down the speed to be the same as its parent Hub
1814 if (DeviceSpeed
== EFI_USB_SPEED_HIGH
) {
1815 InputContext
->Slot
.Speed
= ParentDeviceContext
->Slot
.Speed
;
1821 // 4) Allocate and initialize the Transfer Ring for the Default Control Endpoint.
1823 EndpointTransferRing
= AllocateZeroPool (sizeof (TRANSFER_RING
));
1824 Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[0] = EndpointTransferRing
;
1825 CreateTransferRing(Xhc
, TR_RING_TRB_NUMBER
, (TRANSFER_RING
*)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[0]);
1827 // 5) Initialize the Input default control Endpoint 0 Context (6.2.3).
1829 InputContext
->EP
[0].EPType
= ED_CONTROL_BIDIR
;
1831 if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
1832 InputContext
->EP
[0].MaxPacketSize
= 512;
1833 } else if (DeviceSpeed
== EFI_USB_SPEED_HIGH
) {
1834 InputContext
->EP
[0].MaxPacketSize
= 64;
1836 InputContext
->EP
[0].MaxPacketSize
= 8;
1839 // Initial value of Average TRB Length for Control endpoints would be 8B, Interrupt endpoints
1840 // 1KB, and Bulk and Isoch endpoints 3KB.
1842 InputContext
->EP
[0].AverageTRBLength
= 8;
1843 InputContext
->EP
[0].MaxBurstSize
= 0;
1844 InputContext
->EP
[0].Interval
= 0;
1845 InputContext
->EP
[0].MaxPStreams
= 0;
1846 InputContext
->EP
[0].Mult
= 0;
1847 InputContext
->EP
[0].CErr
= 3;
1850 // Init the DCS(dequeue cycle state) as the transfer ring's CCS
1852 InputContext
->EP
[0].PtrLo
= XHC_LOW_32BIT (((TRANSFER_RING
*)(UINTN
)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[0])->RingSeg0
) | BIT0
;
1853 InputContext
->EP
[0].PtrHi
= XHC_HIGH_32BIT (((TRANSFER_RING
*)(UINTN
)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[0])->RingSeg0
);
1856 // 6) Allocate the Output Device Context data structure (6.2.1) and initialize it to '0'.
1858 OutputContext
= AllocatePages (EFI_SIZE_TO_PAGES (sizeof (DEVICE_CONTEXT
)));
1859 ASSERT (OutputContext
!= NULL
);
1860 ASSERT (((UINTN
) OutputContext
& 0x3F) == 0);
1861 ZeroMem (OutputContext
, sizeof (DEVICE_CONTEXT
));
1863 Xhc
->UsbDevContext
[SlotId
].OutputContext
= OutputContext
;
1865 // 7) Load the appropriate (Device Slot ID) entry in the Device Context Base Address Array (5.4.6) with
1866 // a pointer to the Output Device Context data structure (6.2.1).
1868 Xhc
->DCBAA
[SlotId
] = (UINT64
) (UINTN
) OutputContext
;
1871 // 8) Issue an Address Device Command for the Device Slot, where the command points to the Input
1872 // Context data structure described above.
1874 ZeroMem (&CmdTrbAddr
, sizeof (CmdTrbAddr
));
1875 CmdTrbAddr
.PtrLo
= XHC_LOW_32BIT (Xhc
->UsbDevContext
[SlotId
].InputContext
);
1876 CmdTrbAddr
.PtrHi
= XHC_HIGH_32BIT (Xhc
->UsbDevContext
[SlotId
].InputContext
);
1877 CmdTrbAddr
.CycleBit
= 1;
1878 CmdTrbAddr
.Type
= TRB_TYPE_ADDRESS_DEV
;
1879 CmdTrbAddr
.SlotId
= Xhc
->UsbDevContext
[SlotId
].SlotId
;
1880 Status
= XhcCmdTransfer (
1882 (TRB_TEMPLATE
*) (UINTN
) &CmdTrbAddr
,
1883 XHC_GENERIC_TIMEOUT
,
1884 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
1886 ASSERT (!EFI_ERROR(Status
));
1888 DeviceAddress
= (UINT8
) ((DEVICE_CONTEXT
*) OutputContext
)->Slot
.DeviceAddress
;
1889 DEBUG ((EFI_D_INFO
, " Address %d assigned successfully\n", DeviceAddress
));
1891 Xhc
->UsbDevContext
[SlotId
].XhciDevAddr
= DeviceAddress
;
1897 Assign and initialize the device slot for a new device.
1899 @param Xhc The XHCI Instance.
1900 @param ParentRouteChart The route string pointed to the parent device.
1901 @param ParentPort The port at which the device is located.
1902 @param RouteChart The route string pointed to the device.
1903 @param DeviceSpeed The device speed.
1905 @retval EFI_SUCCESS Successfully assign a slot to the device and assign an address to it.
1910 XhcInitializeDeviceSlot64 (
1911 IN USB_XHCI_INSTANCE
*Xhc
,
1912 IN USB_DEV_ROUTE ParentRouteChart
,
1913 IN UINT16 ParentPort
,
1914 IN USB_DEV_ROUTE RouteChart
,
1915 IN UINT8 DeviceSpeed
1919 EVT_TRB_COMMAND_COMPLETION
*EvtTrb
;
1920 INPUT_CONTEXT_64
*InputContext
;
1921 DEVICE_CONTEXT_64
*OutputContext
;
1922 TRANSFER_RING
*EndpointTransferRing
;
1923 CMD_TRB_ADDRESS_DEVICE CmdTrbAddr
;
1924 UINT8 DeviceAddress
;
1925 CMD_TRB_ENABLE_SLOT CmdTrb
;
1928 DEVICE_CONTEXT_64
*ParentDeviceContext
;
1930 ZeroMem (&CmdTrb
, sizeof (CMD_TRB_ENABLE_SLOT
));
1931 CmdTrb
.CycleBit
= 1;
1932 CmdTrb
.Type
= TRB_TYPE_EN_SLOT
;
1934 Status
= XhcCmdTransfer (
1936 (TRB_TEMPLATE
*) (UINTN
) &CmdTrb
,
1937 XHC_GENERIC_TIMEOUT
,
1938 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
1940 ASSERT_EFI_ERROR (Status
);
1941 ASSERT (EvtTrb
->SlotId
<= Xhc
->MaxSlotsEn
);
1942 DEBUG ((EFI_D_INFO
, "Enable Slot Successfully, The Slot ID = 0x%x\n", EvtTrb
->SlotId
));
1943 SlotId
= (UINT8
)EvtTrb
->SlotId
;
1944 ASSERT (SlotId
!= 0);
1946 ZeroMem (&Xhc
->UsbDevContext
[SlotId
], sizeof (USB_DEV_CONTEXT
));
1947 Xhc
->UsbDevContext
[SlotId
].Enabled
= TRUE
;
1948 Xhc
->UsbDevContext
[SlotId
].SlotId
= SlotId
;
1949 Xhc
->UsbDevContext
[SlotId
].RouteString
.Dword
= RouteChart
.Dword
;
1950 Xhc
->UsbDevContext
[SlotId
].ParentRouteString
.Dword
= ParentRouteChart
.Dword
;
1953 // 4.3.3 Device Slot Initialization
1954 // 1) Allocate an Input Context data structure (6.2.5) and initialize all fields to '0'.
1956 InputContext
= AllocatePages (EFI_SIZE_TO_PAGES (sizeof (INPUT_CONTEXT_64
)));
1957 ASSERT (InputContext
!= NULL
);
1958 ASSERT (((UINTN
) InputContext
& 0x3F) == 0);
1959 ZeroMem (InputContext
, sizeof (INPUT_CONTEXT_64
));
1961 Xhc
->UsbDevContext
[SlotId
].InputContext
= (VOID
*) InputContext
;
1964 // 2) Initialize the Input Control Context (6.2.5.1) of the Input Context by setting the A0 and A1
1965 // flags to '1'. These flags indicate that the Slot Context and the Endpoint 0 Context of the Input
1966 // Context are affected by the command.
1968 InputContext
->InputControlContext
.Dword2
|= (BIT0
| BIT1
);
1971 // 3) Initialize the Input Slot Context data structure
1973 InputContext
->Slot
.RouteString
= RouteChart
.Route
.RouteString
;
1974 InputContext
->Slot
.Speed
= DeviceSpeed
+ 1;
1975 InputContext
->Slot
.ContextEntries
= 1;
1976 InputContext
->Slot
.RootHubPortNum
= RouteChart
.Route
.RootPortNum
;
1978 if (RouteChart
.Route
.RouteString
) {
1980 // The device is behind of hub device.
1982 ParentSlotId
= XhcRouteStringToSlotId(Xhc
, ParentRouteChart
);
1983 ASSERT (ParentSlotId
!= 0);
1985 //if the Full/Low device attached to a High Speed Hub, Init the TTPortNum and TTHubSlotId field of slot context
1987 ParentDeviceContext
= (DEVICE_CONTEXT_64
*)Xhc
->UsbDevContext
[ParentSlotId
].OutputContext
;
1988 if ((ParentDeviceContext
->Slot
.TTPortNum
== 0) &&
1989 (ParentDeviceContext
->Slot
.TTHubSlotId
== 0)) {
1990 if ((ParentDeviceContext
->Slot
.Speed
== (EFI_USB_SPEED_HIGH
+ 1)) && (DeviceSpeed
< EFI_USB_SPEED_HIGH
)) {
1992 // Full/Low device attached to High speed hub port that isolates the high speed signaling
1993 // environment from Full/Low speed signaling environment for a device
1995 InputContext
->Slot
.TTPortNum
= ParentPort
;
1996 InputContext
->Slot
.TTHubSlotId
= ParentSlotId
;
2000 // Inherit the TT parameters from parent device.
2002 InputContext
->Slot
.TTPortNum
= ParentDeviceContext
->Slot
.TTPortNum
;
2003 InputContext
->Slot
.TTHubSlotId
= ParentDeviceContext
->Slot
.TTHubSlotId
;
2005 // If the device is a High speed device then down the speed to be the same as its parent Hub
2007 if (DeviceSpeed
== EFI_USB_SPEED_HIGH
) {
2008 InputContext
->Slot
.Speed
= ParentDeviceContext
->Slot
.Speed
;
2014 // 4) Allocate and initialize the Transfer Ring for the Default Control Endpoint.
2016 EndpointTransferRing
= AllocateZeroPool (sizeof (TRANSFER_RING
));
2017 Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[0] = EndpointTransferRing
;
2018 CreateTransferRing(Xhc
, TR_RING_TRB_NUMBER
, (TRANSFER_RING
*)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[0]);
2020 // 5) Initialize the Input default control Endpoint 0 Context (6.2.3).
2022 InputContext
->EP
[0].EPType
= ED_CONTROL_BIDIR
;
2024 if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
2025 InputContext
->EP
[0].MaxPacketSize
= 512;
2026 } else if (DeviceSpeed
== EFI_USB_SPEED_HIGH
) {
2027 InputContext
->EP
[0].MaxPacketSize
= 64;
2029 InputContext
->EP
[0].MaxPacketSize
= 8;
2032 // Initial value of Average TRB Length for Control endpoints would be 8B, Interrupt endpoints
2033 // 1KB, and Bulk and Isoch endpoints 3KB.
2035 InputContext
->EP
[0].AverageTRBLength
= 8;
2036 InputContext
->EP
[0].MaxBurstSize
= 0;
2037 InputContext
->EP
[0].Interval
= 0;
2038 InputContext
->EP
[0].MaxPStreams
= 0;
2039 InputContext
->EP
[0].Mult
= 0;
2040 InputContext
->EP
[0].CErr
= 3;
2043 // Init the DCS(dequeue cycle state) as the transfer ring's CCS
2045 InputContext
->EP
[0].PtrLo
= XHC_LOW_32BIT (((TRANSFER_RING
*)(UINTN
)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[0])->RingSeg0
) | BIT0
;
2046 InputContext
->EP
[0].PtrHi
= XHC_HIGH_32BIT (((TRANSFER_RING
*)(UINTN
)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[0])->RingSeg0
);
2049 // 6) Allocate the Output Device Context data structure (6.2.1) and initialize it to '0'.
2051 OutputContext
= AllocatePages (EFI_SIZE_TO_PAGES (sizeof (DEVICE_CONTEXT_64
)));
2052 ASSERT (OutputContext
!= NULL
);
2053 ASSERT (((UINTN
) OutputContext
& 0x3F) == 0);
2054 ZeroMem (OutputContext
, sizeof (DEVICE_CONTEXT_64
));
2056 Xhc
->UsbDevContext
[SlotId
].OutputContext
= OutputContext
;
2058 // 7) Load the appropriate (Device Slot ID) entry in the Device Context Base Address Array (5.4.6) with
2059 // a pointer to the Output Device Context data structure (6.2.1).
2061 Xhc
->DCBAA
[SlotId
] = (UINT64
) (UINTN
) OutputContext
;
2064 // 8) Issue an Address Device Command for the Device Slot, where the command points to the Input
2065 // Context data structure described above.
2067 ZeroMem (&CmdTrbAddr
, sizeof (CmdTrbAddr
));
2068 CmdTrbAddr
.PtrLo
= XHC_LOW_32BIT (Xhc
->UsbDevContext
[SlotId
].InputContext
);
2069 CmdTrbAddr
.PtrHi
= XHC_HIGH_32BIT (Xhc
->UsbDevContext
[SlotId
].InputContext
);
2070 CmdTrbAddr
.CycleBit
= 1;
2071 CmdTrbAddr
.Type
= TRB_TYPE_ADDRESS_DEV
;
2072 CmdTrbAddr
.SlotId
= Xhc
->UsbDevContext
[SlotId
].SlotId
;
2073 Status
= XhcCmdTransfer (
2075 (TRB_TEMPLATE
*) (UINTN
) &CmdTrbAddr
,
2076 XHC_GENERIC_TIMEOUT
,
2077 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
2079 ASSERT (!EFI_ERROR(Status
));
2081 DeviceAddress
= (UINT8
) ((DEVICE_CONTEXT_64
*) OutputContext
)->Slot
.DeviceAddress
;
2082 DEBUG ((EFI_D_INFO
, " Address %d assigned successfully\n", DeviceAddress
));
2084 Xhc
->UsbDevContext
[SlotId
].XhciDevAddr
= DeviceAddress
;
2091 Disable the specified device slot.
2093 @param Xhc The XHCI Instance.
2094 @param SlotId The slot id to be disabled.
2096 @retval EFI_SUCCESS Successfully disable the device slot.
2102 IN USB_XHCI_INSTANCE
*Xhc
,
2107 TRB_TEMPLATE
*EvtTrb
;
2108 CMD_TRB_DISABLE_SLOT CmdTrbDisSlot
;
2113 // Disable the device slots occupied by these devices on its downstream ports.
2114 // Entry 0 is reserved.
2116 for (Index
= 0; Index
< 255; Index
++) {
2117 if (!Xhc
->UsbDevContext
[Index
+ 1].Enabled
||
2118 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
== 0) ||
2119 (Xhc
->UsbDevContext
[Index
+ 1].ParentRouteString
.Dword
!= Xhc
->UsbDevContext
[SlotId
].RouteString
.Dword
)) {
2123 Status
= XhcDisableSlotCmd (Xhc
, Xhc
->UsbDevContext
[Index
+ 1].SlotId
);
2125 if (EFI_ERROR (Status
)) {
2126 DEBUG ((EFI_D_ERROR
, "XhcDisableSlotCmd: failed to disable child, ignore error\n"));
2127 Xhc
->UsbDevContext
[Index
+ 1].SlotId
= 0;
2132 // Construct the disable slot command
2134 DEBUG ((EFI_D_INFO
, "Disable device slot %d!\n", SlotId
));
2136 ZeroMem (&CmdTrbDisSlot
, sizeof (CmdTrbDisSlot
));
2137 CmdTrbDisSlot
.CycleBit
= 1;
2138 CmdTrbDisSlot
.Type
= TRB_TYPE_DIS_SLOT
;
2139 CmdTrbDisSlot
.SlotId
= SlotId
;
2140 Status
= XhcCmdTransfer (
2142 (TRB_TEMPLATE
*) (UINTN
) &CmdTrbDisSlot
,
2143 XHC_GENERIC_TIMEOUT
,
2144 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
2146 ASSERT_EFI_ERROR(Status
);
2148 // Free the slot's device context entry
2150 Xhc
->DCBAA
[SlotId
] = 0;
2153 // Free the slot related data structure
2155 for (Index
= 0; Index
< 31; Index
++) {
2156 if (Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Index
] != NULL
) {
2157 RingSeg
= ((TRANSFER_RING
*)(UINTN
)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Index
])->RingSeg0
;
2158 if (RingSeg
!= NULL
) {
2159 FreePages (RingSeg
, EFI_SIZE_TO_PAGES (sizeof (TRB_TEMPLATE
) * TR_RING_TRB_NUMBER
));
2161 FreePool (Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Index
]);
2165 for (Index
= 0; Index
< Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
; Index
++) {
2166 if (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
] != NULL
) {
2167 FreePool (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]);
2171 if (Xhc
->UsbDevContext
[SlotId
].InputContext
!= NULL
) {
2172 FreePages (Xhc
->UsbDevContext
[SlotId
].InputContext
, EFI_SIZE_TO_PAGES (sizeof (INPUT_CONTEXT
)));
2175 if (Xhc
->UsbDevContext
[SlotId
].OutputContext
!= NULL
) {
2176 FreePages (Xhc
->UsbDevContext
[SlotId
].OutputContext
, EFI_SIZE_TO_PAGES (sizeof (DEVICE_CONTEXT
)));
2179 // Doesn't zero the entry because XhcAsyncInterruptTransfer() may be invoked to remove the established
2180 // asynchronous interrupt pipe after the device is disabled. It needs the device address mapping info to
2181 // remove urb from XHCI's asynchronous transfer list.
2183 Xhc
->UsbDevContext
[SlotId
].Enabled
= FALSE
;
2184 Xhc
->UsbDevContext
[SlotId
].SlotId
= 0;
2190 Disable the specified device slot.
2192 @param Xhc The XHCI Instance.
2193 @param SlotId The slot id to be disabled.
2195 @retval EFI_SUCCESS Successfully disable the device slot.
2200 XhcDisableSlotCmd64 (
2201 IN USB_XHCI_INSTANCE
*Xhc
,
2206 TRB_TEMPLATE
*EvtTrb
;
2207 CMD_TRB_DISABLE_SLOT CmdTrbDisSlot
;
2212 // Disable the device slots occupied by these devices on its downstream ports.
2213 // Entry 0 is reserved.
2215 for (Index
= 0; Index
< 255; Index
++) {
2216 if (!Xhc
->UsbDevContext
[Index
+ 1].Enabled
||
2217 (Xhc
->UsbDevContext
[Index
+ 1].SlotId
== 0) ||
2218 (Xhc
->UsbDevContext
[Index
+ 1].ParentRouteString
.Dword
!= Xhc
->UsbDevContext
[SlotId
].RouteString
.Dword
)) {
2222 Status
= XhcDisableSlotCmd64 (Xhc
, Xhc
->UsbDevContext
[Index
+ 1].SlotId
);
2224 if (EFI_ERROR (Status
)) {
2225 DEBUG ((EFI_D_ERROR
, "XhcDisableSlotCmd: failed to disable child, ignore error\n"));
2226 Xhc
->UsbDevContext
[Index
+ 1].SlotId
= 0;
2231 // Construct the disable slot command
2233 DEBUG ((EFI_D_INFO
, "Disable device slot %d!\n", SlotId
));
2235 ZeroMem (&CmdTrbDisSlot
, sizeof (CmdTrbDisSlot
));
2236 CmdTrbDisSlot
.CycleBit
= 1;
2237 CmdTrbDisSlot
.Type
= TRB_TYPE_DIS_SLOT
;
2238 CmdTrbDisSlot
.SlotId
= SlotId
;
2239 Status
= XhcCmdTransfer (
2241 (TRB_TEMPLATE
*) (UINTN
) &CmdTrbDisSlot
,
2242 XHC_GENERIC_TIMEOUT
,
2243 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
2245 ASSERT_EFI_ERROR(Status
);
2247 // Free the slot's device context entry
2249 Xhc
->DCBAA
[SlotId
] = 0;
2252 // Free the slot related data structure
2254 for (Index
= 0; Index
< 31; Index
++) {
2255 if (Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Index
] != NULL
) {
2256 RingSeg
= ((TRANSFER_RING
*)(UINTN
)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Index
])->RingSeg0
;
2257 if (RingSeg
!= NULL
) {
2258 FreePages (RingSeg
, EFI_SIZE_TO_PAGES (sizeof (TRB_TEMPLATE
) * TR_RING_TRB_NUMBER
));
2260 FreePool (Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Index
]);
2264 for (Index
= 0; Index
< Xhc
->UsbDevContext
[SlotId
].DevDesc
.NumConfigurations
; Index
++) {
2265 if (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
] != NULL
) {
2266 FreePool (Xhc
->UsbDevContext
[SlotId
].ConfDesc
[Index
]);
2270 if (Xhc
->UsbDevContext
[SlotId
].InputContext
!= NULL
) {
2271 FreePages (Xhc
->UsbDevContext
[SlotId
].InputContext
, EFI_SIZE_TO_PAGES (sizeof (INPUT_CONTEXT_64
)));
2274 if (Xhc
->UsbDevContext
[SlotId
].OutputContext
!= NULL
) {
2275 FreePages (Xhc
->UsbDevContext
[SlotId
].OutputContext
, EFI_SIZE_TO_PAGES (sizeof (DEVICE_CONTEXT_64
)));
2278 // Doesn't zero the entry because XhcAsyncInterruptTransfer() may be invoked to remove the established
2279 // asynchronous interrupt pipe after the device is disabled. It needs the device address mapping info to
2280 // remove urb from XHCI's asynchronous transfer list.
2282 Xhc
->UsbDevContext
[SlotId
].Enabled
= FALSE
;
2283 Xhc
->UsbDevContext
[SlotId
].SlotId
= 0;
2290 Configure all the device endpoints through XHCI's Configure_Endpoint cmd.
2292 @param Xhc The XHCI Instance.
2293 @param SlotId The slot id to be configured.
2294 @param DeviceSpeed The device's speed.
2295 @param ConfigDesc The pointer to the usb device configuration descriptor.
2297 @retval EFI_SUCCESS Successfully configure all the device endpoints.
2303 IN USB_XHCI_INSTANCE
*Xhc
,
2305 IN UINT8 DeviceSpeed
,
2306 IN USB_CONFIG_DESCRIPTOR
*ConfigDesc
2311 USB_INTERFACE_DESCRIPTOR
*IfDesc
;
2312 USB_ENDPOINT_DESCRIPTOR
*EpDesc
;
2323 TRANSFER_RING
*EndpointTransferRing
;
2324 CMD_TRB_CONFIG_ENDPOINT CmdTrbCfgEP
;
2325 INPUT_CONTEXT
*InputContext
;
2326 DEVICE_CONTEXT
*OutputContext
;
2327 EVT_TRB_COMMAND_COMPLETION
*EvtTrb
;
2329 // 4.6.6 Configure Endpoint
2331 InputContext
= Xhc
->UsbDevContext
[SlotId
].InputContext
;
2332 OutputContext
= Xhc
->UsbDevContext
[SlotId
].OutputContext
;
2333 ZeroMem (InputContext
, sizeof (INPUT_CONTEXT
));
2334 CopyMem (&InputContext
->Slot
, &OutputContext
->Slot
, sizeof (SLOT_CONTEXT
));
2336 ASSERT (ConfigDesc
!= NULL
);
2340 IfDesc
= (USB_INTERFACE_DESCRIPTOR
*)(ConfigDesc
+ 1);
2341 for (Index
= 0; Index
< ConfigDesc
->NumInterfaces
; Index
++) {
2342 while (IfDesc
->DescriptorType
!= USB_DESC_TYPE_INTERFACE
) {
2343 IfDesc
= (USB_INTERFACE_DESCRIPTOR
*)((UINTN
)IfDesc
+ IfDesc
->Length
);
2346 NumEp
= IfDesc
->NumEndpoints
;
2348 EpDesc
= (USB_ENDPOINT_DESCRIPTOR
*)(IfDesc
+ 1);
2349 for (EpIndex
= 0; EpIndex
< NumEp
; EpIndex
++) {
2350 while (EpDesc
->DescriptorType
!= USB_DESC_TYPE_ENDPOINT
) {
2351 EpDesc
= (USB_ENDPOINT_DESCRIPTOR
*)((UINTN
)EpDesc
+ EpDesc
->Length
);
2354 EpAddr
= (UINT8
)(EpDesc
->EndpointAddress
& 0x0F);
2355 Direction
= (UINT8
)((EpDesc
->EndpointAddress
& 0x80) ? EfiUsbDataIn
: EfiUsbDataOut
);
2357 Dci
= XhcEndpointToDci (EpAddr
, Direction
);
2363 InputContext
->InputControlContext
.Dword2
|= (BIT0
<< Dci
);
2364 InputContext
->EP
[Dci
-1].MaxPacketSize
= EpDesc
->MaxPacketSize
;
2366 if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
2368 // 6.2.3.4, shall be set to the value defined in the bMaxBurst field of the SuperSpeed Endpoint Companion Descriptor.
2370 InputContext
->EP
[Dci
-1].MaxBurstSize
= 0x0;
2372 InputContext
->EP
[Dci
-1].MaxBurstSize
= 0x0;
2375 switch (EpDesc
->Attributes
& USB_ENDPOINT_TYPE_MASK
) {
2376 case USB_ENDPOINT_BULK
:
2377 if (Direction
== EfiUsbDataIn
) {
2378 InputContext
->EP
[Dci
-1].CErr
= 3;
2379 InputContext
->EP
[Dci
-1].EPType
= ED_BULK_IN
;
2381 InputContext
->EP
[Dci
-1].CErr
= 3;
2382 InputContext
->EP
[Dci
-1].EPType
= ED_BULK_OUT
;
2385 InputContext
->EP
[Dci
-1].AverageTRBLength
= 0x1000;
2386 if (Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1] == NULL
) {
2387 EndpointTransferRing
= AllocateZeroPool(sizeof (TRANSFER_RING
));
2388 Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1] = (VOID
*) EndpointTransferRing
;
2389 CreateTransferRing(Xhc
, TR_RING_TRB_NUMBER
, (TRANSFER_RING
*)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1]);
2393 case USB_ENDPOINT_ISO
:
2394 if (Direction
== EfiUsbDataIn
) {
2395 InputContext
->EP
[Dci
-1].CErr
= 0;
2396 InputContext
->EP
[Dci
-1].EPType
= ED_ISOCH_IN
;
2398 InputContext
->EP
[Dci
-1].CErr
= 0;
2399 InputContext
->EP
[Dci
-1].EPType
= ED_ISOCH_OUT
;
2402 case USB_ENDPOINT_INTERRUPT
:
2403 if (Direction
== EfiUsbDataIn
) {
2404 InputContext
->EP
[Dci
-1].CErr
= 3;
2405 InputContext
->EP
[Dci
-1].EPType
= ED_INTERRUPT_IN
;
2407 InputContext
->EP
[Dci
-1].CErr
= 3;
2408 InputContext
->EP
[Dci
-1].EPType
= ED_INTERRUPT_OUT
;
2410 InputContext
->EP
[Dci
-1].AverageTRBLength
= 0x1000;
2411 InputContext
->EP
[Dci
-1].MaxESITPayload
= EpDesc
->MaxPacketSize
;
2413 // Get the bInterval from descriptor and init the the interval field of endpoint context
2415 if ((DeviceSpeed
== EFI_USB_SPEED_FULL
) || (DeviceSpeed
== EFI_USB_SPEED_LOW
)) {
2416 Interval
= EpDesc
->Interval
;
2418 // Hard code the interval to MAX first, need calculate through the bInterval field of Endpoint descriptor.
2420 InputContext
->EP
[Dci
-1].Interval
= 6;
2421 } else if ((DeviceSpeed
== EFI_USB_SPEED_HIGH
) || (DeviceSpeed
== EFI_USB_SPEED_SUPER
)) {
2422 Interval
= EpDesc
->Interval
;
2423 ASSERT (Interval
>= 1 && Interval
<= 16);
2425 // Refer to XHCI 1.0 spec section 6.2.3.6, table 61
2427 InputContext
->EP
[Dci
-1].Interval
= Interval
- 1;
2428 InputContext
->EP
[Dci
-1].AverageTRBLength
= 0x1000;
2429 InputContext
->EP
[Dci
-1].MaxESITPayload
= 0x0002;
2430 InputContext
->EP
[Dci
-1].MaxBurstSize
= 0x0;
2431 InputContext
->EP
[Dci
-1].CErr
= 3;
2434 if (Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1] == NULL
) {
2435 EndpointTransferRing
= AllocateZeroPool(sizeof (TRANSFER_RING
));
2436 Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1] = (VOID
*) EndpointTransferRing
;
2437 CreateTransferRing(Xhc
, TR_RING_TRB_NUMBER
, (TRANSFER_RING
*)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1]);
2441 case USB_ENDPOINT_CONTROL
:
2447 PhyAddr
= XHC_LOW_32BIT (((TRANSFER_RING
*)(UINTN
)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1])->RingSeg0
);
2449 PhyAddr
|= ((TRANSFER_RING
*)(UINTN
)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1])->RingPCS
;
2450 InputContext
->EP
[Dci
-1].PtrLo
= PhyAddr
;
2451 InputContext
->EP
[Dci
-1].PtrHi
= XHC_HIGH_32BIT (((TRANSFER_RING
*)(UINTN
)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1])->RingSeg0
);
2453 EpDesc
= (USB_ENDPOINT_DESCRIPTOR
*)((UINTN
)EpDesc
+ EpDesc
->Length
);
2455 IfDesc
= (USB_INTERFACE_DESCRIPTOR
*)((UINTN
)IfDesc
+ IfDesc
->Length
);
2458 InputContext
->InputControlContext
.Dword2
|= BIT0
;
2459 InputContext
->Slot
.ContextEntries
= MaxDci
;
2461 // configure endpoint
2463 ZeroMem (&CmdTrbCfgEP
, sizeof (CmdTrbCfgEP
));
2464 CmdTrbCfgEP
.PtrLo
= XHC_LOW_32BIT (InputContext
);
2465 CmdTrbCfgEP
.PtrHi
= XHC_HIGH_32BIT (InputContext
);
2466 CmdTrbCfgEP
.CycleBit
= 1;
2467 CmdTrbCfgEP
.Type
= TRB_TYPE_CON_ENDPOINT
;
2468 CmdTrbCfgEP
.SlotId
= Xhc
->UsbDevContext
[SlotId
].SlotId
;
2469 DEBUG ((EFI_D_INFO
, "Configure Endpoint\n"));
2470 Status
= XhcCmdTransfer (
2472 (TRB_TEMPLATE
*) (UINTN
) &CmdTrbCfgEP
,
2473 XHC_GENERIC_TIMEOUT
,
2474 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
2476 ASSERT_EFI_ERROR(Status
);
2482 Configure all the device endpoints through XHCI's Configure_Endpoint cmd.
2484 @param Xhc The XHCI Instance.
2485 @param SlotId The slot id to be configured.
2486 @param DeviceSpeed The device's speed.
2487 @param ConfigDesc The pointer to the usb device configuration descriptor.
2489 @retval EFI_SUCCESS Successfully configure all the device endpoints.
2495 IN USB_XHCI_INSTANCE
*Xhc
,
2497 IN UINT8 DeviceSpeed
,
2498 IN USB_CONFIG_DESCRIPTOR
*ConfigDesc
2503 USB_INTERFACE_DESCRIPTOR
*IfDesc
;
2504 USB_ENDPOINT_DESCRIPTOR
*EpDesc
;
2515 TRANSFER_RING
*EndpointTransferRing
;
2516 CMD_TRB_CONFIG_ENDPOINT CmdTrbCfgEP
;
2517 INPUT_CONTEXT_64
*InputContext
;
2518 DEVICE_CONTEXT_64
*OutputContext
;
2519 EVT_TRB_COMMAND_COMPLETION
*EvtTrb
;
2521 // 4.6.6 Configure Endpoint
2523 InputContext
= Xhc
->UsbDevContext
[SlotId
].InputContext
;
2524 OutputContext
= Xhc
->UsbDevContext
[SlotId
].OutputContext
;
2525 ZeroMem (InputContext
, sizeof (INPUT_CONTEXT_64
));
2526 CopyMem (&InputContext
->Slot
, &OutputContext
->Slot
, sizeof (SLOT_CONTEXT_64
));
2528 ASSERT (ConfigDesc
!= NULL
);
2532 IfDesc
= (USB_INTERFACE_DESCRIPTOR
*)(ConfigDesc
+ 1);
2533 for (Index
= 0; Index
< ConfigDesc
->NumInterfaces
; Index
++) {
2534 while (IfDesc
->DescriptorType
!= USB_DESC_TYPE_INTERFACE
) {
2535 IfDesc
= (USB_INTERFACE_DESCRIPTOR
*)((UINTN
)IfDesc
+ IfDesc
->Length
);
2538 NumEp
= IfDesc
->NumEndpoints
;
2540 EpDesc
= (USB_ENDPOINT_DESCRIPTOR
*)(IfDesc
+ 1);
2541 for (EpIndex
= 0; EpIndex
< NumEp
; EpIndex
++) {
2542 while (EpDesc
->DescriptorType
!= USB_DESC_TYPE_ENDPOINT
) {
2543 EpDesc
= (USB_ENDPOINT_DESCRIPTOR
*)((UINTN
)EpDesc
+ EpDesc
->Length
);
2546 EpAddr
= (UINT8
)(EpDesc
->EndpointAddress
& 0x0F);
2547 Direction
= (UINT8
)((EpDesc
->EndpointAddress
& 0x80) ? EfiUsbDataIn
: EfiUsbDataOut
);
2549 Dci
= XhcEndpointToDci (EpAddr
, Direction
);
2555 InputContext
->InputControlContext
.Dword2
|= (BIT0
<< Dci
);
2556 InputContext
->EP
[Dci
-1].MaxPacketSize
= EpDesc
->MaxPacketSize
;
2558 if (DeviceSpeed
== EFI_USB_SPEED_SUPER
) {
2560 // 6.2.3.4, shall be set to the value defined in the bMaxBurst field of the SuperSpeed Endpoint Companion Descriptor.
2562 InputContext
->EP
[Dci
-1].MaxBurstSize
= 0x0;
2564 InputContext
->EP
[Dci
-1].MaxBurstSize
= 0x0;
2567 switch (EpDesc
->Attributes
& USB_ENDPOINT_TYPE_MASK
) {
2568 case USB_ENDPOINT_BULK
:
2569 if (Direction
== EfiUsbDataIn
) {
2570 InputContext
->EP
[Dci
-1].CErr
= 3;
2571 InputContext
->EP
[Dci
-1].EPType
= ED_BULK_IN
;
2573 InputContext
->EP
[Dci
-1].CErr
= 3;
2574 InputContext
->EP
[Dci
-1].EPType
= ED_BULK_OUT
;
2577 InputContext
->EP
[Dci
-1].AverageTRBLength
= 0x1000;
2578 if (Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1] == NULL
) {
2579 EndpointTransferRing
= AllocateZeroPool(sizeof (TRANSFER_RING
));
2580 Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1] = (VOID
*) EndpointTransferRing
;
2581 CreateTransferRing(Xhc
, TR_RING_TRB_NUMBER
, (TRANSFER_RING
*)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1]);
2585 case USB_ENDPOINT_ISO
:
2586 if (Direction
== EfiUsbDataIn
) {
2587 InputContext
->EP
[Dci
-1].CErr
= 0;
2588 InputContext
->EP
[Dci
-1].EPType
= ED_ISOCH_IN
;
2590 InputContext
->EP
[Dci
-1].CErr
= 0;
2591 InputContext
->EP
[Dci
-1].EPType
= ED_ISOCH_OUT
;
2594 case USB_ENDPOINT_INTERRUPT
:
2595 if (Direction
== EfiUsbDataIn
) {
2596 InputContext
->EP
[Dci
-1].CErr
= 3;
2597 InputContext
->EP
[Dci
-1].EPType
= ED_INTERRUPT_IN
;
2599 InputContext
->EP
[Dci
-1].CErr
= 3;
2600 InputContext
->EP
[Dci
-1].EPType
= ED_INTERRUPT_OUT
;
2602 InputContext
->EP
[Dci
-1].AverageTRBLength
= 0x1000;
2603 InputContext
->EP
[Dci
-1].MaxESITPayload
= EpDesc
->MaxPacketSize
;
2605 // Get the bInterval from descriptor and init the the interval field of endpoint context
2607 if ((DeviceSpeed
== EFI_USB_SPEED_FULL
) || (DeviceSpeed
== EFI_USB_SPEED_LOW
)) {
2608 Interval
= EpDesc
->Interval
;
2610 // Hard code the interval to MAX first, need calculate through the bInterval field of Endpoint descriptor.
2612 InputContext
->EP
[Dci
-1].Interval
= 6;
2613 } else if ((DeviceSpeed
== EFI_USB_SPEED_HIGH
) || (DeviceSpeed
== EFI_USB_SPEED_SUPER
)) {
2614 Interval
= EpDesc
->Interval
;
2615 ASSERT (Interval
>= 1 && Interval
<= 16);
2617 // Refer to XHCI 1.0 spec section 6.2.3.6, table 61
2619 InputContext
->EP
[Dci
-1].Interval
= Interval
- 1;
2620 InputContext
->EP
[Dci
-1].AverageTRBLength
= 0x1000;
2621 InputContext
->EP
[Dci
-1].MaxESITPayload
= 0x0002;
2622 InputContext
->EP
[Dci
-1].MaxBurstSize
= 0x0;
2623 InputContext
->EP
[Dci
-1].CErr
= 3;
2626 if (Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1] == NULL
) {
2627 EndpointTransferRing
= AllocateZeroPool(sizeof (TRANSFER_RING
));
2628 Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1] = (VOID
*) EndpointTransferRing
;
2629 CreateTransferRing(Xhc
, TR_RING_TRB_NUMBER
, (TRANSFER_RING
*)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1]);
2633 case USB_ENDPOINT_CONTROL
:
2639 PhyAddr
= XHC_LOW_32BIT (((TRANSFER_RING
*)(UINTN
)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1])->RingSeg0
);
2641 PhyAddr
|= ((TRANSFER_RING
*)(UINTN
)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1])->RingPCS
;
2642 InputContext
->EP
[Dci
-1].PtrLo
= PhyAddr
;
2643 InputContext
->EP
[Dci
-1].PtrHi
= XHC_HIGH_32BIT (((TRANSFER_RING
*)(UINTN
)Xhc
->UsbDevContext
[SlotId
].EndpointTransferRing
[Dci
-1])->RingSeg0
);
2645 EpDesc
= (USB_ENDPOINT_DESCRIPTOR
*)((UINTN
)EpDesc
+ EpDesc
->Length
);
2647 IfDesc
= (USB_INTERFACE_DESCRIPTOR
*)((UINTN
)IfDesc
+ IfDesc
->Length
);
2650 InputContext
->InputControlContext
.Dword2
|= BIT0
;
2651 InputContext
->Slot
.ContextEntries
= MaxDci
;
2653 // configure endpoint
2655 ZeroMem (&CmdTrbCfgEP
, sizeof (CmdTrbCfgEP
));
2656 CmdTrbCfgEP
.PtrLo
= XHC_LOW_32BIT (InputContext
);
2657 CmdTrbCfgEP
.PtrHi
= XHC_HIGH_32BIT (InputContext
);
2658 CmdTrbCfgEP
.CycleBit
= 1;
2659 CmdTrbCfgEP
.Type
= TRB_TYPE_CON_ENDPOINT
;
2660 CmdTrbCfgEP
.SlotId
= Xhc
->UsbDevContext
[SlotId
].SlotId
;
2661 DEBUG ((EFI_D_INFO
, "Configure Endpoint\n"));
2662 Status
= XhcCmdTransfer (
2664 (TRB_TEMPLATE
*) (UINTN
) &CmdTrbCfgEP
,
2665 XHC_GENERIC_TIMEOUT
,
2666 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
2668 ASSERT_EFI_ERROR(Status
);
2675 Evaluate the endpoint 0 context through XHCI's Evaluate_Context cmd.
2677 @param Xhc The XHCI Instance.
2678 @param SlotId The slot id to be evaluated.
2679 @param MaxPacketSize The max packet size supported by the device control transfer.
2681 @retval EFI_SUCCESS Successfully evaluate the device endpoint 0.
2686 XhcEvaluateContext (
2687 IN USB_XHCI_INSTANCE
*Xhc
,
2689 IN UINT32 MaxPacketSize
2693 CMD_TRB_EVALUATE_CONTEXT CmdTrbEvalu
;
2694 EVT_TRB_COMMAND_COMPLETION
*EvtTrb
;
2695 INPUT_CONTEXT
*InputContext
;
2697 ASSERT (Xhc
->UsbDevContext
[SlotId
].SlotId
!= 0);
2700 // 4.6.7 Evaluate Context
2702 InputContext
= Xhc
->UsbDevContext
[SlotId
].InputContext
;
2703 ZeroMem (InputContext
, sizeof (INPUT_CONTEXT
));
2705 InputContext
->InputControlContext
.Dword2
|= BIT1
;
2706 InputContext
->EP
[0].MaxPacketSize
= MaxPacketSize
;
2708 ZeroMem (&CmdTrbEvalu
, sizeof (CmdTrbEvalu
));
2709 CmdTrbEvalu
.PtrLo
= XHC_LOW_32BIT (InputContext
);
2710 CmdTrbEvalu
.PtrHi
= XHC_HIGH_32BIT (InputContext
);
2711 CmdTrbEvalu
.CycleBit
= 1;
2712 CmdTrbEvalu
.Type
= TRB_TYPE_EVALU_CONTXT
;
2713 CmdTrbEvalu
.SlotId
= Xhc
->UsbDevContext
[SlotId
].SlotId
;
2714 DEBUG ((EFI_D_INFO
, "Evaluate context\n"));
2715 Status
= XhcCmdTransfer (
2717 (TRB_TEMPLATE
*) (UINTN
) &CmdTrbEvalu
,
2718 XHC_GENERIC_TIMEOUT
,
2719 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
2721 ASSERT (!EFI_ERROR(Status
));
2727 Evaluate the endpoint 0 context through XHCI's Evaluate_Context cmd.
2729 @param Xhc The XHCI Instance.
2730 @param SlotId The slot id to be evaluated.
2731 @param MaxPacketSize The max packet size supported by the device control transfer.
2733 @retval EFI_SUCCESS Successfully evaluate the device endpoint 0.
2738 XhcEvaluateContext64 (
2739 IN USB_XHCI_INSTANCE
*Xhc
,
2741 IN UINT32 MaxPacketSize
2745 CMD_TRB_EVALUATE_CONTEXT CmdTrbEvalu
;
2746 EVT_TRB_COMMAND_COMPLETION
*EvtTrb
;
2747 INPUT_CONTEXT_64
*InputContext
;
2749 ASSERT (Xhc
->UsbDevContext
[SlotId
].SlotId
!= 0);
2752 // 4.6.7 Evaluate Context
2754 InputContext
= Xhc
->UsbDevContext
[SlotId
].InputContext
;
2755 ZeroMem (InputContext
, sizeof (INPUT_CONTEXT_64
));
2757 InputContext
->InputControlContext
.Dword2
|= BIT1
;
2758 InputContext
->EP
[0].MaxPacketSize
= MaxPacketSize
;
2760 ZeroMem (&CmdTrbEvalu
, sizeof (CmdTrbEvalu
));
2761 CmdTrbEvalu
.PtrLo
= XHC_LOW_32BIT (InputContext
);
2762 CmdTrbEvalu
.PtrHi
= XHC_HIGH_32BIT (InputContext
);
2763 CmdTrbEvalu
.CycleBit
= 1;
2764 CmdTrbEvalu
.Type
= TRB_TYPE_EVALU_CONTXT
;
2765 CmdTrbEvalu
.SlotId
= Xhc
->UsbDevContext
[SlotId
].SlotId
;
2766 DEBUG ((EFI_D_INFO
, "Evaluate context\n"));
2767 Status
= XhcCmdTransfer (
2769 (TRB_TEMPLATE
*) (UINTN
) &CmdTrbEvalu
,
2770 XHC_GENERIC_TIMEOUT
,
2771 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
2773 ASSERT (!EFI_ERROR(Status
));
2780 Evaluate the slot context for hub device through XHCI's Configure_Endpoint cmd.
2782 @param Xhc The XHCI Instance.
2783 @param SlotId The slot id to be configured.
2784 @param PortNum The total number of downstream port supported by the hub.
2785 @param TTT The TT think time of the hub device.
2786 @param MTT The multi-TT of the hub device.
2788 @retval EFI_SUCCESS Successfully configure the hub device's slot context.
2792 XhcConfigHubContext (
2793 IN USB_XHCI_INSTANCE
*Xhc
,
2802 EVT_TRB_COMMAND_COMPLETION
*EvtTrb
;
2803 INPUT_CONTEXT
*InputContext
;
2804 DEVICE_CONTEXT
*OutputContext
;
2805 CMD_TRB_CONFIG_ENDPOINT CmdTrbCfgEP
;
2807 ASSERT (Xhc
->UsbDevContext
[SlotId
].SlotId
!= 0);
2808 InputContext
= Xhc
->UsbDevContext
[SlotId
].InputContext
;
2809 OutputContext
= Xhc
->UsbDevContext
[SlotId
].OutputContext
;
2812 // 4.6.7 Evaluate Context
2814 ZeroMem (InputContext
, sizeof (INPUT_CONTEXT
));
2816 InputContext
->InputControlContext
.Dword2
|= BIT0
;
2819 // Copy the slot context from OutputContext to Input context
2821 CopyMem(&(InputContext
->Slot
), &(OutputContext
->Slot
), sizeof (SLOT_CONTEXT
));
2822 InputContext
->Slot
.Hub
= 1;
2823 InputContext
->Slot
.PortNum
= PortNum
;
2824 InputContext
->Slot
.TTT
= TTT
;
2825 InputContext
->Slot
.MTT
= MTT
;
2827 ZeroMem (&CmdTrbCfgEP
, sizeof (CmdTrbCfgEP
));
2828 CmdTrbCfgEP
.PtrLo
= XHC_LOW_32BIT (InputContext
);
2829 CmdTrbCfgEP
.PtrHi
= XHC_HIGH_32BIT (InputContext
);
2830 CmdTrbCfgEP
.CycleBit
= 1;
2831 CmdTrbCfgEP
.Type
= TRB_TYPE_CON_ENDPOINT
;
2832 CmdTrbCfgEP
.SlotId
= Xhc
->UsbDevContext
[SlotId
].SlotId
;
2833 DEBUG ((EFI_D_INFO
, "Configure Hub Slot Context\n"));
2834 Status
= XhcCmdTransfer (
2836 (TRB_TEMPLATE
*) (UINTN
) &CmdTrbCfgEP
,
2837 XHC_GENERIC_TIMEOUT
,
2838 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
2840 ASSERT (!EFI_ERROR(Status
));
2846 Evaluate the slot context for hub device through XHCI's Configure_Endpoint cmd.
2848 @param Xhc The XHCI Instance.
2849 @param SlotId The slot id to be configured.
2850 @param PortNum The total number of downstream port supported by the hub.
2851 @param TTT The TT think time of the hub device.
2852 @param MTT The multi-TT of the hub device.
2854 @retval EFI_SUCCESS Successfully configure the hub device's slot context.
2858 XhcConfigHubContext64 (
2859 IN USB_XHCI_INSTANCE
*Xhc
,
2868 EVT_TRB_COMMAND_COMPLETION
*EvtTrb
;
2869 INPUT_CONTEXT_64
*InputContext
;
2870 DEVICE_CONTEXT_64
*OutputContext
;
2871 CMD_TRB_CONFIG_ENDPOINT CmdTrbCfgEP
;
2873 ASSERT (Xhc
->UsbDevContext
[SlotId
].SlotId
!= 0);
2874 InputContext
= Xhc
->UsbDevContext
[SlotId
].InputContext
;
2875 OutputContext
= Xhc
->UsbDevContext
[SlotId
].OutputContext
;
2878 // 4.6.7 Evaluate Context
2880 ZeroMem (InputContext
, sizeof (INPUT_CONTEXT_64
));
2882 InputContext
->InputControlContext
.Dword2
|= BIT0
;
2885 // Copy the slot context from OutputContext to Input context
2887 CopyMem(&(InputContext
->Slot
), &(OutputContext
->Slot
), sizeof (SLOT_CONTEXT_64
));
2888 InputContext
->Slot
.Hub
= 1;
2889 InputContext
->Slot
.PortNum
= PortNum
;
2890 InputContext
->Slot
.TTT
= TTT
;
2891 InputContext
->Slot
.MTT
= MTT
;
2893 ZeroMem (&CmdTrbCfgEP
, sizeof (CmdTrbCfgEP
));
2894 CmdTrbCfgEP
.PtrLo
= XHC_LOW_32BIT (InputContext
);
2895 CmdTrbCfgEP
.PtrHi
= XHC_HIGH_32BIT (InputContext
);
2896 CmdTrbCfgEP
.CycleBit
= 1;
2897 CmdTrbCfgEP
.Type
= TRB_TYPE_CON_ENDPOINT
;
2898 CmdTrbCfgEP
.SlotId
= Xhc
->UsbDevContext
[SlotId
].SlotId
;
2899 DEBUG ((EFI_D_INFO
, "Configure Hub Slot Context\n"));
2900 Status
= XhcCmdTransfer (
2902 (TRB_TEMPLATE
*) (UINTN
) &CmdTrbCfgEP
,
2903 XHC_GENERIC_TIMEOUT
,
2904 (TRB_TEMPLATE
**) (UINTN
) &EvtTrb
2906 ASSERT (!EFI_ERROR(Status
));