2 PEIM to produce gPeiUsb2HostControllerPpiGuid based on gPeiUsbControllerPpiGuid
3 which is used to enable recovery function from USB Drivers.
5 Copyright (c) 2010 - 2017, Intel Corporation. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions
9 of the BSD License which accompanies this distribution. The
10 full text of the license may be found at
11 http://opensource.org/licenses/bsd-license.php
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
21 Delete a single asynchronous interrupt transfer for
22 the device and endpoint.
24 @param Ehc The EHCI device.
25 @param Data Current data not associated with a QTD.
26 @param DataLen The length of the data.
27 @param PktId Packet ID to use in the QTD.
28 @param Toggle Data toggle to use in the QTD.
29 @param MaxPacket Maximu packet length of the endpoint.
31 @retval the pointer to the created QTD or NULL if failed to create one.
36 IN PEI_USB2_HC_DEV
*Ehc
,
52 Qtd
= UsbHcAllocateMem (Ehc
, Ehc
->MemPool
, sizeof (PEI_EHC_QTD
));
58 Qtd
->Signature
= EHC_QTD_SIG
;
62 InitializeListHead (&Qtd
->QtdList
);
65 QtdHw
->NextQtd
= QTD_LINK (NULL
, TRUE
);
66 QtdHw
->AltNext
= QTD_LINK (NULL
, TRUE
);
67 QtdHw
->Status
= QTD_STAT_ACTIVE
;
69 QtdHw
->ErrCnt
= QTD_MAX_ERR
;
71 QtdHw
->TotalBytes
= 0;
72 QtdHw
->DataToggle
= Toggle
;
75 // Fill in the buffer points
80 for (Index
= 0; Index
<= QTD_MAX_BUFFER
; Index
++) {
82 // Set the buffer point (Check page 41 EHCI Spec 1.0). No need to
83 // compute the offset and clear Reserved fields. This is already
84 // done in the data point.
86 QtdHw
->Page
[Index
] = EHC_LOW_32BIT (Data
);
87 QtdHw
->PageHigh
[Index
] = EHC_HIGH_32BIT (Data
);
89 ThisBufLen
= QTD_BUF_LEN
- (EHC_LOW_32BIT (Data
) & QTD_BUF_MASK
);
91 if (Len
+ ThisBufLen
>= DataLen
) {
101 // Need to fix the last pointer if the Qtd can't hold all the
102 // user's data to make sure that the length is in the unit of
103 // max packets. If it can hold all the data, there is no such
107 Len
= Len
- Len
% MaxPacket
;
110 QtdHw
->TotalBytes
= (UINT32
) Len
;
118 Initialize the queue head for interrupt transfer,
119 that is, initialize the following three fields:
120 1. SplitXState in the Status field.
121 2. Microframe S-mask.
122 3. Microframe C-mask.
124 @param Ep The queue head's related endpoint.
125 @param QhHw The queue head to initialize.
135 // Because UEFI interface can't utilitize an endpoint with
136 // poll rate faster than 1ms, only need to set one bit in
137 // the queue head. simple. But it may be changed later. If
138 // sub-1ms interrupt is supported, need to update the S-Mask
141 if (Ep
->DevSpeed
== EFI_USB_SPEED_HIGH
) {
142 QhHw
->SMask
= QH_MICROFRAME_0
;
147 // For low/full speed device, the transfer must go through
148 // the split transaction. Need to update three fields
149 // 1. SplitXState in the status
150 // 2. Microframe S-Mask
151 // 3. Microframe C-Mask
152 // UEFI USB doesn't exercise admission control. It simplely
153 // schedule the high speed transactions in microframe 0, and
154 // full/low speed transactions at microframe 1. This also
155 // avoid the use of FSTN.
157 QhHw
->SMask
= QH_MICROFRAME_1
;
158 QhHw
->CMask
= QH_MICROFRAME_3
| QH_MICROFRAME_4
| QH_MICROFRAME_5
;
162 Allocate and initialize a EHCI queue head.
164 @param Ehci The EHCI device.
165 @param Ep The endpoint to create queue head for.
167 @retval the pointer to the created queue head or NULL if failed to create one.
172 IN PEI_USB2_HC_DEV
*Ehci
,
179 Qh
= UsbHcAllocateMem (Ehci
, Ehci
->MemPool
, sizeof (PEI_EHC_QH
));
185 Qh
->Signature
= EHC_QH_SIG
;
187 Qh
->Interval
= Ep
->PollRate
;
189 InitializeListHead (&Qh
->Qtds
);
192 QhHw
->HorizonLink
= QH_LINK (NULL
, 0, TRUE
);
193 QhHw
->DeviceAddr
= Ep
->DevAddr
;
195 QhHw
->EpNum
= Ep
->EpAddr
;
196 QhHw
->EpSpeed
= Ep
->DevSpeed
;
198 QhHw
->ReclaimHead
= 0;
199 QhHw
->MaxPacketLen
= (UINT32
) Ep
->MaxPacket
;
201 QhHw
->NakReload
= QH_NAK_RELOAD
;
202 QhHw
->HubAddr
= Ep
->HubAddr
;
203 QhHw
->PortNum
= Ep
->HubPort
;
204 QhHw
->Multiplier
= 1;
205 QhHw
->DataToggle
= Ep
->Toggle
;
207 if (Ep
->DevSpeed
!= EFI_USB_SPEED_HIGH
) {
208 QhHw
->Status
|= QTD_STAT_DO_SS
;
212 case EHC_CTRL_TRANSFER
:
214 // Special initialization for the control transfer:
215 // 1. Control transfer initialize data toggle from each QTD
216 // 2. Set the Control Endpoint Flag (C) for low/full speed endpoint.
220 if (Ep
->DevSpeed
!= EFI_USB_SPEED_HIGH
) {
225 case EHC_INT_TRANSFER_ASYNC
:
226 case EHC_INT_TRANSFER_SYNC
:
228 // Special initialization for the interrupt transfer
229 // to set the S-Mask and C-Mask
232 EhcInitIntQh (Ep
, QhHw
);
235 case EHC_BULK_TRANSFER
:
236 if ((Ep
->DevSpeed
== EFI_USB_SPEED_HIGH
) && (Ep
->Direction
== EfiUsbDataOut
)) {
237 QhHw
->Status
|= QTD_STAT_DO_PING
;
247 Convert the poll interval from application to that
248 be used by EHCI interface data structure. Only need
249 to get the max 2^n that is less than interval. UEFI
250 can't support high speed endpoint with a interval less
251 than 8 microframe because interval is specified in
252 the unit of ms (millisecond).
254 @param Interval The interval to convert.
256 @retval The converted interval.
271 // Find the index (1 based) of the highest non-zero bit
275 while (Interval
!= 0) {
280 return (UINTN
)1 << (BitCount
- 1);
286 @param Ehc The EHCI device.
287 @param Qtds The list head of the QTD.
292 IN PEI_USB2_HC_DEV
*Ehc
,
293 IN EFI_LIST_ENTRY
*Qtds
296 EFI_LIST_ENTRY
*Entry
;
297 EFI_LIST_ENTRY
*Next
;
300 EFI_LIST_FOR_EACH_SAFE (Entry
, Next
, Qtds
) {
301 Qtd
= EFI_LIST_CONTAINER (Entry
, PEI_EHC_QTD
, QtdList
);
303 RemoveEntryList (&Qtd
->QtdList
);
304 UsbHcFreeMem (Ehc
, Ehc
->MemPool
, Qtd
, sizeof (PEI_EHC_QTD
));
309 Free an allocated URB. It is possible for it to be partially inited.
311 @param Ehc The EHCI device.
312 @param Urb The URB to free.
317 IN PEI_USB2_HC_DEV
*Ehc
,
321 if (Urb
->RequestPhy
!= NULL
) {
322 IoMmuUnmap (Ehc
->IoMmu
, Urb
->RequestMap
);
325 if (Urb
->DataMap
!= NULL
) {
326 IoMmuUnmap (Ehc
->IoMmu
, Urb
->DataMap
);
329 if (Urb
->Qh
!= NULL
) {
331 // Ensure that this queue head has been unlinked from the
332 // schedule data structures. Free all the associated QTDs
334 EhcFreeQtds (Ehc
, &Urb
->Qh
->Qtds
);
335 UsbHcFreeMem (Ehc
, Ehc
->MemPool
, Urb
->Qh
, sizeof (PEI_EHC_QH
));
340 Create a list of QTDs for the URB.
342 @param Ehc The EHCI device.
343 @param Urb The URB to create QTDs for.
345 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for QTD.
346 @retval EFI_SUCCESS The QTDs are allocated for the URB.
351 IN PEI_USB2_HC_DEV
*Ehc
,
358 PEI_EHC_QTD
*StatusQtd
;
359 PEI_EHC_QTD
*NextQtd
;
360 EFI_LIST_ENTRY
*Entry
;
366 ASSERT ((Urb
!= NULL
) && (Urb
->Qh
!= NULL
));
369 // EHCI follows the alternet next QTD pointer if it meets
370 // a short read and the AlterNext pointer is valid. UEFI
371 // EHCI driver should terminate the transfer except the
378 AlterNext
= QTD_LINK (NULL
, TRUE
);
380 if (Ep
->Direction
== EfiUsbDataIn
) {
381 AlterNext
= QTD_LINK (Ehc
->ShortReadStop
, FALSE
);
385 // Build the Setup and status packets for control transfer
387 if (Urb
->Ep
.Type
== EHC_CTRL_TRANSFER
) {
388 Len
= sizeof (EFI_USB_DEVICE_REQUEST
);
389 Qtd
= EhcCreateQtd (Ehc
, Urb
->RequestPhy
, Len
, QTD_PID_SETUP
, 0, Ep
->MaxPacket
);
392 return EFI_OUT_OF_RESOURCES
;
395 InsertTailList (&Qh
->Qtds
, &Qtd
->QtdList
);
398 // Create the status packet now. Set the AlterNext to it. So, when
399 // EHCI meets a short control read, it can resume at the status stage.
400 // Use the opposite direction of the data stage, or IN if there is
403 if (Ep
->Direction
== EfiUsbDataIn
) {
404 Pid
= QTD_PID_OUTPUT
;
409 StatusQtd
= EhcCreateQtd (Ehc
, NULL
, 0, Pid
, 1, Ep
->MaxPacket
);
411 if (StatusQtd
== NULL
) {
415 if (Ep
->Direction
== EfiUsbDataIn
) {
416 AlterNext
= QTD_LINK (StatusQtd
, FALSE
);
423 // Build the data packets for all the transfers
425 if (Ep
->Direction
== EfiUsbDataIn
) {
428 Pid
= QTD_PID_OUTPUT
;
434 while (Len
< Urb
->DataLen
) {
437 (UINT8
*) Urb
->DataPhy
+ Len
,
448 Qtd
->QtdHw
.AltNext
= AlterNext
;
449 InsertTailList (&Qh
->Qtds
, &Qtd
->QtdList
);
452 // Switch the Toggle bit if odd number of packets are included in the QTD.
454 if (((Qtd
->DataLen
+ Ep
->MaxPacket
- 1) / Ep
->MaxPacket
) % 2) {
455 Toggle
= (UINT8
) (1 - Toggle
);
462 // Insert the status packet for control transfer
464 if (Ep
->Type
== EHC_CTRL_TRANSFER
) {
465 InsertTailList (&Qh
->Qtds
, &StatusQtd
->QtdList
);
469 // OK, all the QTDs needed are created. Now, fix the NextQtd point
471 EFI_LIST_FOR_EACH (Entry
, &Qh
->Qtds
) {
472 Qtd
= EFI_LIST_CONTAINER (Entry
, PEI_EHC_QTD
, QtdList
);
475 // break if it is the last entry on the list
477 if (Entry
->ForwardLink
== &Qh
->Qtds
) {
481 NextQtd
= EFI_LIST_CONTAINER (Entry
->ForwardLink
, PEI_EHC_QTD
, QtdList
);
482 Qtd
->QtdHw
.NextQtd
= QTD_LINK (NextQtd
, FALSE
);
486 // Link the QTDs to the queue head
488 NextQtd
= EFI_LIST_CONTAINER (Qh
->Qtds
.ForwardLink
, PEI_EHC_QTD
, QtdList
);
489 Qh
->QhHw
.NextQtd
= QTD_LINK (NextQtd
, FALSE
);
493 EhcFreeQtds (Ehc
, &Qh
->Qtds
);
494 return EFI_OUT_OF_RESOURCES
;
498 Create a new URB and its associated QTD.
500 @param Ehc The EHCI device.
501 @param DevAddr The device address.
502 @param EpAddr Endpoint addrress & its direction.
503 @param DevSpeed The device speed.
504 @param Toggle Initial data toggle to use.
505 @param MaxPacket The max packet length of the endpoint.
506 @param Hub The transaction translator to use.
507 @param Type The transaction type.
508 @param Request The standard USB request for control transfer.
509 @param Data The user data to transfer.
510 @param DataLen The length of data buffer.
511 @param Callback The function to call when data is transferred.
512 @param Context The context to the callback.
513 @param Interval The interval for interrupt transfer.
515 @retval the pointer to the created URB or NULL.
520 IN PEI_USB2_HC_DEV
*Ehc
,
526 IN EFI_USB2_HC_TRANSACTION_TRANSLATOR
*Hub
,
528 IN EFI_USB_DEVICE_REQUEST
*Request
,
531 IN EFI_ASYNC_USB_TRANSFER_CALLBACK Callback
,
537 EFI_PHYSICAL_ADDRESS PhyAddr
;
538 EDKII_IOMMU_OPERATION MapOp
;
545 Urb
->Signature
= EHC_URB_SIG
;
546 InitializeListHead (&Urb
->UrbList
);
549 Ep
->DevAddr
= DevAddr
;
550 Ep
->EpAddr
= (UINT8
) (EpAddr
& 0x0F);
551 Ep
->Direction
= (((EpAddr
& 0x80) != 0) ? EfiUsbDataIn
: EfiUsbDataOut
);
552 Ep
->DevSpeed
= DevSpeed
;
553 Ep
->MaxPacket
= MaxPacket
;
558 if (DevSpeed
!= EFI_USB_SPEED_HIGH
) {
559 ASSERT (Hub
!= NULL
);
561 Ep
->HubAddr
= Hub
->TranslatorHubAddress
;
562 Ep
->HubPort
= Hub
->TranslatorPortNumber
;
567 Ep
->PollRate
= EhcConvertPollRate (Interval
);
569 Urb
->Request
= Request
;
571 Urb
->DataLen
= DataLen
;
572 Urb
->Callback
= Callback
;
573 Urb
->Context
= Context
;
574 Urb
->Qh
= EhcCreateQh (Ehc
, &Urb
->Ep
);
576 if (Urb
->Qh
== NULL
) {
580 Urb
->RequestPhy
= NULL
;
581 Urb
->RequestMap
= NULL
;
586 // Map the request and user data
588 if (Request
!= NULL
) {
589 Len
= sizeof (EFI_USB_DEVICE_REQUEST
);
590 MapOp
= EdkiiIoMmuOperationBusMasterRead
;
591 Status
= IoMmuMap (Ehc
->IoMmu
, MapOp
, Request
, &Len
, &PhyAddr
, &Map
);
593 if (EFI_ERROR (Status
) || (Len
!= sizeof (EFI_USB_DEVICE_REQUEST
))) {
597 Urb
->RequestPhy
= (VOID
*) ((UINTN
) PhyAddr
);
598 Urb
->RequestMap
= Map
;
604 if (Ep
->Direction
== EfiUsbDataIn
) {
605 MapOp
= EdkiiIoMmuOperationBusMasterWrite
;
607 MapOp
= EdkiiIoMmuOperationBusMasterRead
;
610 Status
= IoMmuMap (Ehc
->IoMmu
, MapOp
, Data
, &Len
, &PhyAddr
, &Map
);
612 if (EFI_ERROR (Status
) || (Len
!= DataLen
)) {
616 Urb
->DataPhy
= (VOID
*) ((UINTN
) PhyAddr
);
620 Status
= EhcCreateQtds (Ehc
, Urb
);
622 if (EFI_ERROR (Status
)) {
629 EhcFreeUrb (Ehc
, Urb
);