2 PEIM to produce gPeiUsb2HostControllerPpiGuid based on gPeiUsbControllerPpiGuid
3 which is used to enable recovery function from USB Drivers.
5 Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
7 SPDX-License-Identifier: BSD-2-Clause-Patent
14 Delete a single asynchronous interrupt transfer for
15 the device and endpoint.
17 @param Ehc The EHCI device.
18 @param Data Current data not associated with a QTD.
19 @param DataLen The length of the data.
20 @param PktId Packet ID to use in the QTD.
21 @param Toggle Data toggle to use in the QTD.
22 @param MaxPacket Maximu packet length of the endpoint.
24 @retval the pointer to the created QTD or NULL if failed to create one.
29 IN PEI_USB2_HC_DEV
*Ehc
,
45 Qtd
= UsbHcAllocateMem (Ehc
, Ehc
->MemPool
, sizeof (PEI_EHC_QTD
));
51 Qtd
->Signature
= EHC_QTD_SIG
;
55 InitializeListHead (&Qtd
->QtdList
);
58 QtdHw
->NextQtd
= QTD_LINK (NULL
, TRUE
);
59 QtdHw
->AltNext
= QTD_LINK (NULL
, TRUE
);
60 QtdHw
->Status
= QTD_STAT_ACTIVE
;
62 QtdHw
->ErrCnt
= QTD_MAX_ERR
;
64 QtdHw
->TotalBytes
= 0;
65 QtdHw
->DataToggle
= Toggle
;
68 // Fill in the buffer points
73 for (Index
= 0; Index
<= QTD_MAX_BUFFER
; Index
++) {
75 // Set the buffer point (Check page 41 EHCI Spec 1.0). No need to
76 // compute the offset and clear Reserved fields. This is already
77 // done in the data point.
79 QtdHw
->Page
[Index
] = EHC_LOW_32BIT (Data
);
80 QtdHw
->PageHigh
[Index
] = EHC_HIGH_32BIT (Data
);
82 ThisBufLen
= QTD_BUF_LEN
- (EHC_LOW_32BIT (Data
) & QTD_BUF_MASK
);
84 if (Len
+ ThisBufLen
>= DataLen
) {
94 // Need to fix the last pointer if the Qtd can't hold all the
95 // user's data to make sure that the length is in the unit of
96 // max packets. If it can hold all the data, there is no such
100 Len
= Len
- Len
% MaxPacket
;
103 QtdHw
->TotalBytes
= (UINT32
) Len
;
111 Initialize the queue head for interrupt transfer,
112 that is, initialize the following three fields:
113 1. SplitXState in the Status field.
114 2. Microframe S-mask.
115 3. Microframe C-mask.
117 @param Ep The queue head's related endpoint.
118 @param QhHw The queue head to initialize.
128 // Because UEFI interface can't utilitize an endpoint with
129 // poll rate faster than 1ms, only need to set one bit in
130 // the queue head. simple. But it may be changed later. If
131 // sub-1ms interrupt is supported, need to update the S-Mask
134 if (Ep
->DevSpeed
== EFI_USB_SPEED_HIGH
) {
135 QhHw
->SMask
= QH_MICROFRAME_0
;
140 // For low/full speed device, the transfer must go through
141 // the split transaction. Need to update three fields
142 // 1. SplitXState in the status
143 // 2. Microframe S-Mask
144 // 3. Microframe C-Mask
145 // UEFI USB doesn't exercise admission control. It simplely
146 // schedule the high speed transactions in microframe 0, and
147 // full/low speed transactions at microframe 1. This also
148 // avoid the use of FSTN.
150 QhHw
->SMask
= QH_MICROFRAME_1
;
151 QhHw
->CMask
= QH_MICROFRAME_3
| QH_MICROFRAME_4
| QH_MICROFRAME_5
;
155 Allocate and initialize a EHCI queue head.
157 @param Ehci The EHCI device.
158 @param Ep The endpoint to create queue head for.
160 @retval the pointer to the created queue head or NULL if failed to create one.
165 IN PEI_USB2_HC_DEV
*Ehci
,
172 Qh
= UsbHcAllocateMem (Ehci
, Ehci
->MemPool
, sizeof (PEI_EHC_QH
));
178 Qh
->Signature
= EHC_QH_SIG
;
180 Qh
->Interval
= Ep
->PollRate
;
182 InitializeListHead (&Qh
->Qtds
);
185 QhHw
->HorizonLink
= QH_LINK (NULL
, 0, TRUE
);
186 QhHw
->DeviceAddr
= Ep
->DevAddr
;
188 QhHw
->EpNum
= Ep
->EpAddr
;
189 QhHw
->EpSpeed
= Ep
->DevSpeed
;
191 QhHw
->ReclaimHead
= 0;
192 QhHw
->MaxPacketLen
= (UINT32
) Ep
->MaxPacket
;
194 QhHw
->NakReload
= QH_NAK_RELOAD
;
195 QhHw
->HubAddr
= Ep
->HubAddr
;
196 QhHw
->PortNum
= Ep
->HubPort
;
197 QhHw
->Multiplier
= 1;
198 QhHw
->DataToggle
= Ep
->Toggle
;
200 if (Ep
->DevSpeed
!= EFI_USB_SPEED_HIGH
) {
201 QhHw
->Status
|= QTD_STAT_DO_SS
;
205 case EHC_CTRL_TRANSFER
:
207 // Special initialization for the control transfer:
208 // 1. Control transfer initialize data toggle from each QTD
209 // 2. Set the Control Endpoint Flag (C) for low/full speed endpoint.
213 if (Ep
->DevSpeed
!= EFI_USB_SPEED_HIGH
) {
218 case EHC_INT_TRANSFER_ASYNC
:
219 case EHC_INT_TRANSFER_SYNC
:
221 // Special initialization for the interrupt transfer
222 // to set the S-Mask and C-Mask
225 EhcInitIntQh (Ep
, QhHw
);
228 case EHC_BULK_TRANSFER
:
229 if ((Ep
->DevSpeed
== EFI_USB_SPEED_HIGH
) && (Ep
->Direction
== EfiUsbDataOut
)) {
230 QhHw
->Status
|= QTD_STAT_DO_PING
;
240 Convert the poll interval from application to that
241 be used by EHCI interface data structure. Only need
242 to get the max 2^n that is less than interval. UEFI
243 can't support high speed endpoint with a interval less
244 than 8 microframe because interval is specified in
245 the unit of ms (millisecond).
247 @param Interval The interval to convert.
249 @retval The converted interval.
264 // Find the index (1 based) of the highest non-zero bit
268 while (Interval
!= 0) {
273 return (UINTN
)1 << (BitCount
- 1);
279 @param Ehc The EHCI device.
280 @param Qtds The list head of the QTD.
285 IN PEI_USB2_HC_DEV
*Ehc
,
286 IN EFI_LIST_ENTRY
*Qtds
289 EFI_LIST_ENTRY
*Entry
;
290 EFI_LIST_ENTRY
*Next
;
293 EFI_LIST_FOR_EACH_SAFE (Entry
, Next
, Qtds
) {
294 Qtd
= EFI_LIST_CONTAINER (Entry
, PEI_EHC_QTD
, QtdList
);
296 RemoveEntryList (&Qtd
->QtdList
);
297 UsbHcFreeMem (Ehc
, Ehc
->MemPool
, Qtd
, sizeof (PEI_EHC_QTD
));
302 Free an allocated URB. It is possible for it to be partially inited.
304 @param Ehc The EHCI device.
305 @param Urb The URB to free.
310 IN PEI_USB2_HC_DEV
*Ehc
,
314 if (Urb
->RequestPhy
!= NULL
) {
315 IoMmuUnmap (Ehc
->IoMmu
, Urb
->RequestMap
);
318 if (Urb
->DataMap
!= NULL
) {
319 IoMmuUnmap (Ehc
->IoMmu
, Urb
->DataMap
);
322 if (Urb
->Qh
!= NULL
) {
324 // Ensure that this queue head has been unlinked from the
325 // schedule data structures. Free all the associated QTDs
327 EhcFreeQtds (Ehc
, &Urb
->Qh
->Qtds
);
328 UsbHcFreeMem (Ehc
, Ehc
->MemPool
, Urb
->Qh
, sizeof (PEI_EHC_QH
));
333 Create a list of QTDs for the URB.
335 @param Ehc The EHCI device.
336 @param Urb The URB to create QTDs for.
338 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for QTD.
339 @retval EFI_SUCCESS The QTDs are allocated for the URB.
344 IN PEI_USB2_HC_DEV
*Ehc
,
351 PEI_EHC_QTD
*StatusQtd
;
352 PEI_EHC_QTD
*NextQtd
;
353 EFI_LIST_ENTRY
*Entry
;
359 ASSERT ((Urb
!= NULL
) && (Urb
->Qh
!= NULL
));
362 // EHCI follows the alternet next QTD pointer if it meets
363 // a short read and the AlterNext pointer is valid. UEFI
364 // EHCI driver should terminate the transfer except the
371 AlterNext
= QTD_LINK (NULL
, TRUE
);
373 if (Ep
->Direction
== EfiUsbDataIn
) {
374 AlterNext
= QTD_LINK (Ehc
->ShortReadStop
, FALSE
);
378 // Build the Setup and status packets for control transfer
380 if (Urb
->Ep
.Type
== EHC_CTRL_TRANSFER
) {
381 Len
= sizeof (EFI_USB_DEVICE_REQUEST
);
382 Qtd
= EhcCreateQtd (Ehc
, Urb
->RequestPhy
, Len
, QTD_PID_SETUP
, 0, Ep
->MaxPacket
);
385 return EFI_OUT_OF_RESOURCES
;
388 InsertTailList (&Qh
->Qtds
, &Qtd
->QtdList
);
391 // Create the status packet now. Set the AlterNext to it. So, when
392 // EHCI meets a short control read, it can resume at the status stage.
393 // Use the opposite direction of the data stage, or IN if there is
396 if (Ep
->Direction
== EfiUsbDataIn
) {
397 Pid
= QTD_PID_OUTPUT
;
402 StatusQtd
= EhcCreateQtd (Ehc
, NULL
, 0, Pid
, 1, Ep
->MaxPacket
);
404 if (StatusQtd
== NULL
) {
408 if (Ep
->Direction
== EfiUsbDataIn
) {
409 AlterNext
= QTD_LINK (StatusQtd
, FALSE
);
416 // Build the data packets for all the transfers
418 if (Ep
->Direction
== EfiUsbDataIn
) {
421 Pid
= QTD_PID_OUTPUT
;
427 while (Len
< Urb
->DataLen
) {
430 (UINT8
*) Urb
->DataPhy
+ Len
,
441 Qtd
->QtdHw
.AltNext
= AlterNext
;
442 InsertTailList (&Qh
->Qtds
, &Qtd
->QtdList
);
445 // Switch the Toggle bit if odd number of packets are included in the QTD.
447 if (((Qtd
->DataLen
+ Ep
->MaxPacket
- 1) / Ep
->MaxPacket
) % 2) {
448 Toggle
= (UINT8
) (1 - Toggle
);
455 // Insert the status packet for control transfer
457 if (Ep
->Type
== EHC_CTRL_TRANSFER
) {
458 InsertTailList (&Qh
->Qtds
, &StatusQtd
->QtdList
);
462 // OK, all the QTDs needed are created. Now, fix the NextQtd point
464 EFI_LIST_FOR_EACH (Entry
, &Qh
->Qtds
) {
465 Qtd
= EFI_LIST_CONTAINER (Entry
, PEI_EHC_QTD
, QtdList
);
468 // break if it is the last entry on the list
470 if (Entry
->ForwardLink
== &Qh
->Qtds
) {
474 NextQtd
= EFI_LIST_CONTAINER (Entry
->ForwardLink
, PEI_EHC_QTD
, QtdList
);
475 Qtd
->QtdHw
.NextQtd
= QTD_LINK (NextQtd
, FALSE
);
479 // Link the QTDs to the queue head
481 NextQtd
= EFI_LIST_CONTAINER (Qh
->Qtds
.ForwardLink
, PEI_EHC_QTD
, QtdList
);
482 Qh
->QhHw
.NextQtd
= QTD_LINK (NextQtd
, FALSE
);
486 EhcFreeQtds (Ehc
, &Qh
->Qtds
);
487 return EFI_OUT_OF_RESOURCES
;
491 Create a new URB and its associated QTD.
493 @param Ehc The EHCI device.
494 @param DevAddr The device address.
495 @param EpAddr Endpoint addrress & its direction.
496 @param DevSpeed The device speed.
497 @param Toggle Initial data toggle to use.
498 @param MaxPacket The max packet length of the endpoint.
499 @param Hub The transaction translator to use.
500 @param Type The transaction type.
501 @param Request The standard USB request for control transfer.
502 @param Data The user data to transfer.
503 @param DataLen The length of data buffer.
504 @param Callback The function to call when data is transferred.
505 @param Context The context to the callback.
506 @param Interval The interval for interrupt transfer.
508 @retval the pointer to the created URB or NULL.
513 IN PEI_USB2_HC_DEV
*Ehc
,
519 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Hub
,
521 IN EFI_USB_DEVICE_REQUEST
*Request
,
524 IN EFI_ASYNC_USB_TRANSFER_CALLBACK Callback
,
530 EFI_PHYSICAL_ADDRESS PhyAddr
;
531 EDKII_IOMMU_OPERATION MapOp
;
538 Urb
->Signature
= EHC_URB_SIG
;
539 InitializeListHead (&Urb
->UrbList
);
542 Ep
->DevAddr
= DevAddr
;
543 Ep
->EpAddr
= (UINT8
) (EpAddr
& 0x0F);
544 Ep
->Direction
= (((EpAddr
& 0x80) != 0) ? EfiUsbDataIn
: EfiUsbDataOut
);
545 Ep
->DevSpeed
= DevSpeed
;
546 Ep
->MaxPacket
= MaxPacket
;
551 if (DevSpeed
!= EFI_USB_SPEED_HIGH
) {
552 ASSERT (Hub
!= NULL
);
554 Ep
->HubAddr
= Hub
->TranslatorHubAddress
;
555 Ep
->HubPort
= Hub
->TranslatorPortNumber
;
560 Ep
->PollRate
= EhcConvertPollRate (Interval
);
562 Urb
->Request
= Request
;
564 Urb
->DataLen
= DataLen
;
565 Urb
->Callback
= Callback
;
566 Urb
->Context
= Context
;
567 Urb
->Qh
= EhcCreateQh (Ehc
, &Urb
->Ep
);
569 if (Urb
->Qh
== NULL
) {
573 Urb
->RequestPhy
= NULL
;
574 Urb
->RequestMap
= NULL
;
579 // Map the request and user data
581 if (Request
!= NULL
) {
582 Len
= sizeof (EFI_USB_DEVICE_REQUEST
);
583 MapOp
= EdkiiIoMmuOperationBusMasterRead
;
584 Status
= IoMmuMap (Ehc
->IoMmu
, MapOp
, Request
, &Len
, &PhyAddr
, &Map
);
586 if (EFI_ERROR (Status
) || (Len
!= sizeof (EFI_USB_DEVICE_REQUEST
))) {
590 Urb
->RequestPhy
= (VOID
*) ((UINTN
) PhyAddr
);
591 Urb
->RequestMap
= Map
;
597 if (Ep
->Direction
== EfiUsbDataIn
) {
598 MapOp
= EdkiiIoMmuOperationBusMasterWrite
;
600 MapOp
= EdkiiIoMmuOperationBusMasterRead
;
603 Status
= IoMmuMap (Ehc
->IoMmu
, MapOp
, Data
, &Len
, &PhyAddr
, &Map
);
605 if (EFI_ERROR (Status
) || (Len
!= DataLen
)) {
609 Urb
->DataPhy
= (VOID
*) ((UINTN
) PhyAddr
);
613 Status
= EhcCreateQtds (Ehc
, Urb
);
615 if (EFI_ERROR (Status
)) {
622 EhcFreeUrb (Ehc
, Urb
);