]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Pci/EhciDxe/EhciSched.c
Add a blank line in the end of file.
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / EhciDxe / EhciSched.c
1 /** @file
2
3 EHCI transfer scheduling routines.
4
5 Copyright (c) 2007 - 2010, Intel Corporation
6 All rights reserved. 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
10
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.
13
14 **/
15
16 #include "Ehci.h"
17
18
19 /**
20 Create helper QTD/QH for the EHCI device.
21
22 @param Ehc The EHCI device.
23
24 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource for helper QTD/QH.
25 @retval EFI_SUCCESS Helper QH/QTD are created.
26
27 **/
28 EFI_STATUS
29 EhcCreateHelpQ (
30 IN USB2_HC_DEV *Ehc
31 )
32 {
33 USB_ENDPOINT Ep;
34 EHC_QH *Qh;
35 QH_HW *QhHw;
36 EHC_QTD *Qtd;
37 EFI_PHYSICAL_ADDRESS PciAddr;
38
39 //
40 // Create an inactive Qtd to terminate the short packet read.
41 //
42 Qtd = EhcCreateQtd (Ehc, NULL, NULL, 0, QTD_PID_INPUT, 0, 64);
43
44 if (Qtd == NULL) {
45 return EFI_OUT_OF_RESOURCES;
46 }
47
48 Qtd->QtdHw.Status = QTD_STAT_HALTED;
49 Ehc->ShortReadStop = Qtd;
50
51 //
52 // Create a QH to act as the EHC reclamation header.
53 // Set the header to loopback to itself.
54 //
55 Ep.DevAddr = 0;
56 Ep.EpAddr = 1;
57 Ep.Direction = EfiUsbDataIn;
58 Ep.DevSpeed = EFI_USB_SPEED_HIGH;
59 Ep.MaxPacket = 64;
60 Ep.HubAddr = 0;
61 Ep.HubPort = 0;
62 Ep.Toggle = 0;
63 Ep.Type = EHC_BULK_TRANSFER;
64 Ep.PollRate = 1;
65
66 Qh = EhcCreateQh (Ehc, &Ep);
67
68 if (Qh == NULL) {
69 return EFI_OUT_OF_RESOURCES;
70 }
71
72 PciAddr = UsbHcGetPciAddressForHostMem (Ehc->MemPool, Qh, sizeof (EHC_QH));
73 QhHw = &Qh->QhHw;
74 QhHw->HorizonLink = QH_LINK (PciAddr + OFFSET_OF(EHC_QH, QhHw), EHC_TYPE_QH, FALSE);
75 QhHw->Status = QTD_STAT_HALTED;
76 QhHw->ReclaimHead = 1;
77 Qh->NextQh = Qh;
78 Ehc->ReclaimHead = Qh;
79
80 //
81 // Create a dummy QH to act as the terminator for periodical schedule
82 //
83 Ep.EpAddr = 2;
84 Ep.Type = EHC_INT_TRANSFER_SYNC;
85
86 Qh = EhcCreateQh (Ehc, &Ep);
87
88 if (Qh == NULL) {
89 return EFI_OUT_OF_RESOURCES;
90 }
91
92 Qh->QhHw.Status = QTD_STAT_HALTED;
93 Ehc->PeriodOne = Qh;
94
95 return EFI_SUCCESS;
96 }
97
98
99 /**
100 Initialize the schedule data structure such as frame list.
101
102 @param Ehc The EHCI device to init schedule data.
103
104 @retval EFI_OUT_OF_RESOURCES Failed to allocate resource to init schedule data.
105 @retval EFI_SUCCESS The schedule data is initialized.
106
107 **/
108 EFI_STATUS
109 EhcInitSched (
110 IN USB2_HC_DEV *Ehc
111 )
112 {
113 EFI_PCI_IO_PROTOCOL *PciIo;
114 VOID *Buf;
115 EFI_PHYSICAL_ADDRESS PhyAddr;
116 VOID *Map;
117 UINTN Pages;
118 UINTN Bytes;
119 UINTN Index;
120 EFI_STATUS Status;
121 EFI_PHYSICAL_ADDRESS PciAddr;
122
123 //
124 // First initialize the periodical schedule data:
125 // 1. Allocate and map the memory for the frame list
126 // 2. Create the help QTD/QH
127 // 3. Initialize the frame entries
128 // 4. Set the frame list register
129 //
130 PciIo = Ehc->PciIo;
131
132 Bytes = 4096;
133 Pages = EFI_SIZE_TO_PAGES (Bytes);
134
135 Status = PciIo->AllocateBuffer (
136 PciIo,
137 AllocateAnyPages,
138 EfiBootServicesData,
139 Pages,
140 &Buf,
141 0
142 );
143
144 if (EFI_ERROR (Status)) {
145 return EFI_OUT_OF_RESOURCES;
146 }
147
148 Status = PciIo->Map (
149 PciIo,
150 EfiPciIoOperationBusMasterCommonBuffer,
151 Buf,
152 &Bytes,
153 &PhyAddr,
154 &Map
155 );
156
157 if (EFI_ERROR (Status) || (Bytes != 4096)) {
158 PciIo->FreeBuffer (PciIo, Pages, Buf);
159 return EFI_OUT_OF_RESOURCES;
160 }
161
162 Ehc->PeriodFrame = Buf;
163 Ehc->PeriodFrameMap = Map;
164
165 //
166 // Program the FRAMELISTBASE register with the low 32 bit addr
167 //
168 EhcWriteOpReg (Ehc, EHC_FRAME_BASE_OFFSET, EHC_LOW_32BIT (PhyAddr));
169 //
170 // Program the CTRLDSSEGMENT register with the high 32 bit addr
171 //
172 EhcWriteOpReg (Ehc, EHC_CTRLDSSEG_OFFSET, EHC_HIGH_32BIT (PhyAddr));
173
174 //
175 // Init memory pool management then create the helper
176 // QTD/QH. If failed, previously allocated resources
177 // will be freed by EhcFreeSched
178 //
179 Ehc->MemPool = UsbHcInitMemPool (
180 PciIo,
181 EHC_BIT_IS_SET (Ehc->HcCapParams, HCCP_64BIT),
182 EHC_HIGH_32BIT (PhyAddr)
183 );
184
185 if (Ehc->MemPool == NULL) {
186 Status = EFI_OUT_OF_RESOURCES;
187 goto ErrorExit1;
188 }
189
190 Status = EhcCreateHelpQ (Ehc);
191
192 if (EFI_ERROR (Status)) {
193 goto ErrorExit;
194 }
195
196 //
197 // Initialize the frame list entries then set the registers
198 //
199 Ehc->PeriodFrameHost = AllocateZeroPool (EHC_FRAME_LEN * sizeof (UINTN));
200 if (Ehc->PeriodFrameHost == NULL) {
201 Status = EFI_OUT_OF_RESOURCES;
202 goto ErrorExit;
203 }
204
205 PciAddr = UsbHcGetPciAddressForHostMem (Ehc->MemPool, Ehc->PeriodOne, sizeof (EHC_QH));
206
207 for (Index = 0; Index < EHC_FRAME_LEN; Index++) {
208 //
209 // Store the pci bus address of the QH in period frame list which will be accessed by pci bus master.
210 //
211 ((UINT32 *)(Ehc->PeriodFrame))[Index] = QH_LINK (PciAddr, EHC_TYPE_QH, FALSE);
212 //
213 // Store the host address of the QH in period frame list which will be accessed by host.
214 //
215 ((UINTN *)(Ehc->PeriodFrameHost))[Index] = (UINTN)Ehc->PeriodOne;
216 }
217
218 //
219 // Second initialize the asynchronous schedule:
220 // Only need to set the AsynListAddr register to
221 // the reclamation header
222 //
223 PciAddr = UsbHcGetPciAddressForHostMem (Ehc->MemPool, Ehc->ReclaimHead, sizeof (EHC_QH));
224 EhcWriteOpReg (Ehc, EHC_ASYNC_HEAD_OFFSET, EHC_LOW_32BIT (PciAddr));
225 return EFI_SUCCESS;
226
227 ErrorExit:
228 if (Ehc->PeriodOne != NULL) {
229 UsbHcFreeMem (Ehc->MemPool, Ehc->PeriodOne, sizeof (EHC_QH));
230 Ehc->PeriodOne = NULL;
231 }
232
233 if (Ehc->ReclaimHead != NULL) {
234 UsbHcFreeMem (Ehc->MemPool, Ehc->ReclaimHead, sizeof (EHC_QH));
235 Ehc->ReclaimHead = NULL;
236 }
237
238 if (Ehc->ShortReadStop != NULL) {
239 UsbHcFreeMem (Ehc->MemPool, Ehc->ShortReadStop, sizeof (EHC_QTD));
240 Ehc->ShortReadStop = NULL;
241 }
242
243 ErrorExit1:
244 PciIo->FreeBuffer (PciIo, Pages, Buf);
245 PciIo->Unmap (PciIo, Map);
246
247 return Status;
248 }
249
250
251 /**
252 Free the schedule data. It may be partially initialized.
253
254 @param Ehc The EHCI device.
255
256 **/
257 VOID
258 EhcFreeSched (
259 IN USB2_HC_DEV *Ehc
260 )
261 {
262 EFI_PCI_IO_PROTOCOL *PciIo;
263
264 EhcWriteOpReg (Ehc, EHC_FRAME_BASE_OFFSET, 0);
265 EhcWriteOpReg (Ehc, EHC_ASYNC_HEAD_OFFSET, 0);
266
267 if (Ehc->PeriodOne != NULL) {
268 UsbHcFreeMem (Ehc->MemPool, Ehc->PeriodOne, sizeof (EHC_QH));
269 Ehc->PeriodOne = NULL;
270 }
271
272 if (Ehc->ReclaimHead != NULL) {
273 UsbHcFreeMem (Ehc->MemPool, Ehc->ReclaimHead, sizeof (EHC_QH));
274 Ehc->ReclaimHead = NULL;
275 }
276
277 if (Ehc->ShortReadStop != NULL) {
278 UsbHcFreeMem (Ehc->MemPool, Ehc->ShortReadStop, sizeof (EHC_QTD));
279 Ehc->ShortReadStop = NULL;
280 }
281
282 if (Ehc->MemPool != NULL) {
283 UsbHcFreeMemPool (Ehc->MemPool);
284 Ehc->MemPool = NULL;
285 }
286
287 if (Ehc->PeriodFrame != NULL) {
288 PciIo = Ehc->PciIo;
289 ASSERT (PciIo != NULL);
290
291 PciIo->Unmap (PciIo, Ehc->PeriodFrameMap);
292
293 PciIo->FreeBuffer (
294 PciIo,
295 EFI_SIZE_TO_PAGES (EFI_PAGE_SIZE),
296 Ehc->PeriodFrame
297 );
298
299 Ehc->PeriodFrame = NULL;
300 }
301
302 if (Ehc->PeriodFrameHost != NULL) {
303 FreePool (Ehc->PeriodFrameHost);
304 Ehc->PeriodFrameHost = NULL;
305 }
306 }
307
308
309 /**
310 Link the queue head to the asynchronous schedule list.
311 UEFI only supports one CTRL/BULK transfer at a time
312 due to its interfaces. This simplifies the AsynList
313 management: A reclamation header is always linked to
314 the AsyncListAddr, the only active QH is appended to it.
315
316 @param Ehc The EHCI device.
317 @param Qh The queue head to link.
318
319 **/
320 VOID
321 EhcLinkQhToAsync (
322 IN USB2_HC_DEV *Ehc,
323 IN EHC_QH *Qh
324 )
325 {
326 EHC_QH *Head;
327 EFI_PHYSICAL_ADDRESS PciAddr;
328
329 //
330 // Append the queue head after the reclaim header, then
331 // fix the hardware visiable parts (EHCI R1.0 page 72).
332 // ReclaimHead is always linked to the EHCI's AsynListAddr.
333 //
334 Head = Ehc->ReclaimHead;
335
336 Qh->NextQh = Head->NextQh;
337 Head->NextQh = Qh;
338
339 PciAddr = UsbHcGetPciAddressForHostMem (Ehc->MemPool, Qh->NextQh, sizeof (EHC_QH));
340 Qh->QhHw.HorizonLink = QH_LINK (PciAddr, EHC_TYPE_QH, FALSE);
341 PciAddr = UsbHcGetPciAddressForHostMem (Ehc->MemPool, Head->NextQh, sizeof (EHC_QH));
342 Head->QhHw.HorizonLink = QH_LINK (PciAddr, EHC_TYPE_QH, FALSE);
343 }
344
345
346 /**
347 Unlink a queue head from the asynchronous schedule list.
348 Need to synchronize with hardware.
349
350 @param Ehc The EHCI device.
351 @param Qh The queue head to unlink.
352
353 **/
354 VOID
355 EhcUnlinkQhFromAsync (
356 IN USB2_HC_DEV *Ehc,
357 IN EHC_QH *Qh
358 )
359 {
360 EHC_QH *Head;
361 EFI_STATUS Status;
362 EFI_PHYSICAL_ADDRESS PciAddr;
363
364 ASSERT (Ehc->ReclaimHead->NextQh == Qh);
365
366 //
367 // Remove the QH from reclamation head, then update the hardware
368 // visiable part: Only need to loopback the ReclaimHead. The Qh
369 // is pointing to ReclaimHead (which is staill in the list).
370 //
371 Head = Ehc->ReclaimHead;
372
373 Head->NextQh = Qh->NextQh;
374 Qh->NextQh = NULL;
375
376 PciAddr = UsbHcGetPciAddressForHostMem (Ehc->MemPool, Head->NextQh, sizeof (EHC_QH));
377 Head->QhHw.HorizonLink = QH_LINK (PciAddr, EHC_TYPE_QH, FALSE);
378
379 //
380 // Set and wait the door bell to synchronize with the hardware
381 //
382 Status = EhcSetAndWaitDoorBell (Ehc, EHC_GENERIC_TIMEOUT);
383
384 if (EFI_ERROR (Status)) {
385 DEBUG ((EFI_D_ERROR, "EhcUnlinkQhFromAsync: Failed to synchronize with doorbell\n"));
386 }
387 }
388
389
390 /**
391 Link a queue head for interrupt transfer to the periodic
392 schedule frame list. This code is very much the same as
393 that in UHCI.
394
395 @param Ehc The EHCI device.
396 @param Qh The queue head to link.
397
398 **/
399 VOID
400 EhcLinkQhToPeriod (
401 IN USB2_HC_DEV *Ehc,
402 IN EHC_QH *Qh
403 )
404 {
405 UINTN Index;
406 EHC_QH *Prev;
407 EHC_QH *Next;
408 EFI_PHYSICAL_ADDRESS PciAddr;
409
410 for (Index = 0; Index < EHC_FRAME_LEN; Index += Qh->Interval) {
411 //
412 // First QH can't be NULL because we always keep PeriodOne
413 // heads on the frame list
414 //
415 ASSERT (!EHC_LINK_TERMINATED (((UINT32*)Ehc->PeriodFrame)[Index]));
416 Next = (EHC_QH*)((UINTN*)Ehc->PeriodFrameHost)[Index];
417 Prev = NULL;
418
419 //
420 // Now, insert the queue head (Qh) into this frame:
421 // 1. Find a queue head with the same poll interval, just insert
422 // Qh after this queue head, then we are done.
423 //
424 // 2. Find the position to insert the queue head into:
425 // Previous head's interval is bigger than Qh's
426 // Next head's interval is less than Qh's
427 // Then, insert the Qh between then
428 //
429 while (Next->Interval > Qh->Interval) {
430 Prev = Next;
431 Next = Next->NextQh;
432 }
433
434 ASSERT (Next != NULL);
435
436 //
437 // The entry may have been linked into the frame by early insertation.
438 // For example: if insert a Qh with Qh.Interval == 4, and there is a Qh
439 // with Qh.Interval == 8 on the frame. If so, we are done with this frame.
440 // It isn't necessary to compare all the QH with the same interval to
441 // Qh. This is because if there is other QH with the same interval, Qh
442 // should has been inserted after that at Frames[0] and at Frames[0] it is
443 // impossible for (Next == Qh)
444 //
445 if (Next == Qh) {
446 continue;
447 }
448
449 if (Next->Interval == Qh->Interval) {
450 //
451 // If there is a QH with the same interval, it locates at
452 // Frames[0], and we can simply insert it after this QH. We
453 // are all done.
454 //
455 ASSERT ((Index == 0) && (Qh->NextQh == NULL));
456
457 Prev = Next;
458 Next = Next->NextQh;
459
460 Qh->NextQh = Next;
461 Prev->NextQh = Qh;
462
463 Qh->QhHw.HorizonLink = Prev->QhHw.HorizonLink;
464 PciAddr = UsbHcGetPciAddressForHostMem (Ehc->MemPool, Qh, sizeof (EHC_QH));
465 Prev->QhHw.HorizonLink = QH_LINK (PciAddr, EHC_TYPE_QH, FALSE);
466 break;
467 }
468
469 //
470 // OK, find the right position, insert it in. If Qh's next
471 // link has already been set, it is in position. This is
472 // guarranted by 2^n polling interval.
473 //
474 if (Qh->NextQh == NULL) {
475 Qh->NextQh = Next;
476 PciAddr = UsbHcGetPciAddressForHostMem (Ehc->MemPool, Next, sizeof (EHC_QH));
477 Qh->QhHw.HorizonLink = QH_LINK (PciAddr, EHC_TYPE_QH, FALSE);
478 }
479
480 PciAddr = UsbHcGetPciAddressForHostMem (Ehc->MemPool, Qh, sizeof (EHC_QH));
481
482 if (Prev == NULL) {
483 ((UINT32*)Ehc->PeriodFrame)[Index] = QH_LINK (PciAddr, EHC_TYPE_QH, FALSE);
484 ((UINTN*)Ehc->PeriodFrameHost)[Index] = (UINTN)Qh;
485 } else {
486 Prev->NextQh = Qh;
487 Prev->QhHw.HorizonLink = QH_LINK (PciAddr, EHC_TYPE_QH, FALSE);
488 }
489 }
490 }
491
492
493 /**
494 Unlink an interrupt queue head from the periodic
495 schedule frame list.
496
497 @param Ehc The EHCI device.
498 @param Qh The queue head to unlink.
499
500 **/
501 VOID
502 EhcUnlinkQhFromPeriod (
503 IN USB2_HC_DEV *Ehc,
504 IN EHC_QH *Qh
505 )
506 {
507 UINTN Index;
508 EHC_QH *Prev;
509 EHC_QH *This;
510
511 for (Index = 0; Index < EHC_FRAME_LEN; Index += Qh->Interval) {
512 //
513 // Frame link can't be NULL because we always keep PeroidOne
514 // on the frame list
515 //
516 ASSERT (!EHC_LINK_TERMINATED (((UINT32*)Ehc->PeriodFrame)[Index]));
517 This = (EHC_QH*)((UINTN*)Ehc->PeriodFrameHost)[Index];
518 Prev = NULL;
519
520 //
521 // Walk through the frame's QH list to find the
522 // queue head to remove
523 //
524 while ((This != NULL) && (This != Qh)) {
525 Prev = This;
526 This = This->NextQh;
527 }
528
529 //
530 // Qh may have already been unlinked from this frame
531 // by early action. See the comments in EhcLinkQhToPeriod.
532 //
533 if (This == NULL) {
534 continue;
535 }
536
537 if (Prev == NULL) {
538 //
539 // Qh is the first entry in the frame
540 //
541 ((UINT32*)Ehc->PeriodFrame)[Index] = Qh->QhHw.HorizonLink;
542 ((UINTN*)Ehc->PeriodFrameHost)[Index] = (UINTN)Qh->NextQh;
543 } else {
544 Prev->NextQh = Qh->NextQh;
545 Prev->QhHw.HorizonLink = Qh->QhHw.HorizonLink;
546 }
547 }
548 }
549
550
551 /**
552 Check the URB's execution result and update the URB's
553 result accordingly.
554
555 @param Ehc The EHCI device.
556 @param Urb The URB to check result.
557
558 @return Whether the result of URB transfer is finialized.
559
560 **/
561 BOOLEAN
562 EhcCheckUrbResult (
563 IN USB2_HC_DEV *Ehc,
564 IN URB *Urb
565 )
566 {
567 LIST_ENTRY *Entry;
568 EHC_QTD *Qtd;
569 QTD_HW *QtdHw;
570 UINT8 State;
571 BOOLEAN Finished;
572 EFI_PHYSICAL_ADDRESS PciAddr;
573
574 ASSERT ((Ehc != NULL) && (Urb != NULL) && (Urb->Qh != NULL));
575
576 Finished = TRUE;
577 Urb->Completed = 0;
578
579 Urb->Result = EFI_USB_NOERROR;
580
581 if (EhcIsHalt (Ehc) || EhcIsSysError (Ehc)) {
582 Urb->Result |= EFI_USB_ERR_SYSTEM;
583 goto ON_EXIT;
584 }
585
586 EFI_LIST_FOR_EACH (Entry, &Urb->Qh->Qtds) {
587 Qtd = EFI_LIST_CONTAINER (Entry, EHC_QTD, QtdList);
588 QtdHw = &Qtd->QtdHw;
589 State = (UINT8) QtdHw->Status;
590
591 if (EHC_BIT_IS_SET (State, QTD_STAT_HALTED)) {
592 //
593 // EHCI will halt the queue head when met some error.
594 // If it is halted, the result of URB is finialized.
595 //
596 if ((State & QTD_STAT_ERR_MASK) == 0) {
597 Urb->Result |= EFI_USB_ERR_STALL;
598 }
599
600 if (EHC_BIT_IS_SET (State, QTD_STAT_BABBLE_ERR)) {
601 Urb->Result |= EFI_USB_ERR_BABBLE;
602 }
603
604 if (EHC_BIT_IS_SET (State, QTD_STAT_BUFF_ERR)) {
605 Urb->Result |= EFI_USB_ERR_BUFFER;
606 }
607
608 if (EHC_BIT_IS_SET (State, QTD_STAT_TRANS_ERR) && (QtdHw->ErrCnt == 0)) {
609 Urb->Result |= EFI_USB_ERR_TIMEOUT;
610 }
611
612 Finished = TRUE;
613 goto ON_EXIT;
614
615 } else if (EHC_BIT_IS_SET (State, QTD_STAT_ACTIVE)) {
616 //
617 // The QTD is still active, no need to check furthur.
618 //
619 Urb->Result |= EFI_USB_ERR_NOTEXECUTE;
620
621 Finished = FALSE;
622 goto ON_EXIT;
623
624 } else {
625 //
626 // This QTD is finished OK or met short packet read. Update the
627 // transfer length if it isn't a setup.
628 //
629 if (QtdHw->Pid != QTD_PID_SETUP) {
630 Urb->Completed += Qtd->DataLen - QtdHw->TotalBytes;
631 }
632
633 if ((QtdHw->TotalBytes != 0) && (QtdHw->Pid == QTD_PID_INPUT)) {
634 EhcDumpQh (Urb->Qh, "Short packet read", FALSE);
635
636 //
637 // Short packet read condition. If it isn't a setup transfer,
638 // no need to check furthur: the queue head will halt at the
639 // ShortReadStop. If it is a setup transfer, need to check the
640 // Status Stage of the setup transfer to get the finial result
641 //
642 PciAddr = UsbHcGetPciAddressForHostMem (Ehc->MemPool, Ehc->ShortReadStop, sizeof (EHC_QTD));
643 if (QtdHw->AltNext == QTD_LINK (PciAddr, FALSE)) {
644 DEBUG ((EFI_D_INFO, "EhcCheckUrbResult: Short packet read, break\n"));
645
646 Finished = TRUE;
647 goto ON_EXIT;
648 }
649
650 DEBUG ((EFI_D_INFO, "EhcCheckUrbResult: Short packet read, continue\n"));
651 }
652 }
653 }
654
655 ON_EXIT:
656 //
657 // Return the data toggle set by EHCI hardware, bulk and interrupt
658 // transfer will use this to initialize the next transaction. For
659 // Control transfer, it always start a new data toggle sequence for
660 // new transfer.
661 //
662 // NOTICE: don't move DT update before the loop, otherwise there is
663 // a race condition that DT is wrong.
664 //
665 Urb->DataToggle = (UINT8) Urb->Qh->QhHw.DataToggle;
666
667 return Finished;
668 }
669
670
671 /**
672 Execute the transfer by polling the URB. This is a synchronous operation.
673
674 @param Ehc The EHCI device.
675 @param Urb The URB to execute.
676 @param TimeOut The time to wait before abort, in millisecond.
677
678 @return EFI_DEVICE_ERROR The transfer failed due to transfer error.
679 @return EFI_TIMEOUT The transfer failed due to time out.
680 @return EFI_SUCCESS The transfer finished OK.
681
682 **/
683 EFI_STATUS
684 EhcExecTransfer (
685 IN USB2_HC_DEV *Ehc,
686 IN URB *Urb,
687 IN UINTN TimeOut
688 )
689 {
690 EFI_STATUS Status;
691 UINTN Index;
692 UINTN Loop;
693 BOOLEAN Finished;
694
695 Status = EFI_SUCCESS;
696 Loop = (TimeOut * EHC_1_MILLISECOND / EHC_SYNC_POLL_INTERVAL) + 1;
697 Finished = FALSE;
698
699 for (Index = 0; Index < Loop; Index++) {
700 Finished = EhcCheckUrbResult (Ehc, Urb);
701
702 if (Finished) {
703 break;
704 }
705
706 gBS->Stall (EHC_SYNC_POLL_INTERVAL);
707 }
708
709 if (!Finished) {
710 DEBUG ((EFI_D_ERROR, "EhcExecTransfer: transfer not finished in %dms\n", (UINT32)TimeOut));
711 EhcDumpQh (Urb->Qh, NULL, FALSE);
712
713 Status = EFI_TIMEOUT;
714
715 } else if (Urb->Result != EFI_USB_NOERROR) {
716 DEBUG ((EFI_D_ERROR, "EhcExecTransfer: transfer failed with %x\n", Urb->Result));
717 EhcDumpQh (Urb->Qh, NULL, FALSE);
718
719 Status = EFI_DEVICE_ERROR;
720 }
721
722 return Status;
723 }
724
725
726 /**
727 Delete a single asynchronous interrupt transfer for
728 the device and endpoint.
729
730 @param Ehc The EHCI device.
731 @param DevAddr The address of the target device.
732 @param EpNum The endpoint of the target.
733 @param DataToggle Return the next data toggle to use.
734
735 @retval EFI_SUCCESS An asynchronous transfer is removed.
736 @retval EFI_NOT_FOUND No transfer for the device is found.
737
738 **/
739 EFI_STATUS
740 EhciDelAsyncIntTransfer (
741 IN USB2_HC_DEV *Ehc,
742 IN UINT8 DevAddr,
743 IN UINT8 EpNum,
744 OUT UINT8 *DataToggle
745 )
746 {
747 LIST_ENTRY *Entry;
748 LIST_ENTRY *Next;
749 URB *Urb;
750 EFI_USB_DATA_DIRECTION Direction;
751
752 Direction = (((EpNum & 0x80) != 0) ? EfiUsbDataIn : EfiUsbDataOut);
753 EpNum &= 0x0F;
754
755 EFI_LIST_FOR_EACH_SAFE (Entry, Next, &Ehc->AsyncIntTransfers) {
756 Urb = EFI_LIST_CONTAINER (Entry, URB, UrbList);
757
758 if ((Urb->Ep.DevAddr == DevAddr) && (Urb->Ep.EpAddr == EpNum) &&
759 (Urb->Ep.Direction == Direction)) {
760 //
761 // Check the URB status to retrieve the next data toggle
762 // from the associated queue head.
763 //
764 EhcCheckUrbResult (Ehc, Urb);
765 *DataToggle = Urb->DataToggle;
766
767 EhcUnlinkQhFromPeriod (Ehc, Urb->Qh);
768 RemoveEntryList (&Urb->UrbList);
769
770 gBS->FreePool (Urb->Data);
771 EhcFreeUrb (Ehc, Urb);
772 return EFI_SUCCESS;
773 }
774 }
775
776 return EFI_NOT_FOUND;
777 }
778
779
780 /**
781 Remove all the asynchronous interrutp transfers.
782
783 @param Ehc The EHCI device.
784
785 **/
786 VOID
787 EhciDelAllAsyncIntTransfers (
788 IN USB2_HC_DEV *Ehc
789 )
790 {
791 LIST_ENTRY *Entry;
792 LIST_ENTRY *Next;
793 URB *Urb;
794
795 EFI_LIST_FOR_EACH_SAFE (Entry, Next, &Ehc->AsyncIntTransfers) {
796 Urb = EFI_LIST_CONTAINER (Entry, URB, UrbList);
797
798 EhcUnlinkQhFromPeriod (Ehc, Urb->Qh);
799 RemoveEntryList (&Urb->UrbList);
800
801 gBS->FreePool (Urb->Data);
802 EhcFreeUrb (Ehc, Urb);
803 }
804 }
805
806
807 /**
808 Flush data from PCI controller specific address to mapped system
809 memory address.
810
811 @param Ehc The EHCI device.
812 @param Urb The URB to unmap.
813
814 @retval EFI_SUCCESS Success to flush data to mapped system memory.
815 @retval EFI_DEVICE_ERROR Fail to flush data to mapped system memory.
816
817 **/
818 EFI_STATUS
819 EhcFlushAsyncIntMap (
820 IN USB2_HC_DEV *Ehc,
821 IN URB *Urb
822 )
823 {
824 EFI_STATUS Status;
825 EFI_PHYSICAL_ADDRESS PhyAddr;
826 EFI_PCI_IO_PROTOCOL_OPERATION MapOp;
827 EFI_PCI_IO_PROTOCOL *PciIo;
828 UINTN Len;
829 VOID *Map;
830
831 PciIo = Ehc->PciIo;
832 Len = Urb->DataLen;
833
834 if (Urb->Ep.Direction == EfiUsbDataIn) {
835 MapOp = EfiPciIoOperationBusMasterWrite;
836 } else {
837 MapOp = EfiPciIoOperationBusMasterRead;
838 }
839
840 Status = PciIo->Unmap (PciIo, Urb->DataMap);
841 if (EFI_ERROR (Status)) {
842 goto ON_ERROR;
843 }
844
845 Urb->DataMap = NULL;
846
847 Status = PciIo->Map (PciIo, MapOp, Urb->Data, &Len, &PhyAddr, &Map);
848 if (EFI_ERROR (Status) || (Len != Urb->DataLen)) {
849 goto ON_ERROR;
850 }
851
852 Urb->DataPhy = (VOID *) ((UINTN) PhyAddr);
853 Urb->DataMap = Map;
854 return EFI_SUCCESS;
855
856 ON_ERROR:
857 return EFI_DEVICE_ERROR;
858 }
859
860
861 /**
862 Update the queue head for next round of asynchronous transfer.
863
864 @param Ehc The EHCI device.
865 @param Urb The URB to update.
866
867 **/
868 VOID
869 EhcUpdateAsyncRequest (
870 IN USB2_HC_DEV *Ehc,
871 IN URB *Urb
872 )
873 {
874 LIST_ENTRY *Entry;
875 EHC_QTD *FirstQtd;
876 QH_HW *QhHw;
877 EHC_QTD *Qtd;
878 QTD_HW *QtdHw;
879 UINTN Index;
880 EFI_PHYSICAL_ADDRESS PciAddr;
881
882 Qtd = NULL;
883
884 if (Urb->Result == EFI_USB_NOERROR) {
885 FirstQtd = NULL;
886
887 EFI_LIST_FOR_EACH (Entry, &Urb->Qh->Qtds) {
888 Qtd = EFI_LIST_CONTAINER (Entry, EHC_QTD, QtdList);
889
890 if (FirstQtd == NULL) {
891 FirstQtd = Qtd;
892 }
893
894 //
895 // Update the QTD for next round of transfer. Host control
896 // may change dt/Total Bytes to Transfer/C_Page/Cerr/Status/
897 // Current Offset. These fields need to be updated. DT isn't
898 // used by interrupt transfer. It uses DT in queue head.
899 // Current Offset is in Page[0], only need to reset Page[0]
900 // to initial data buffer.
901 //
902 QtdHw = &Qtd->QtdHw;
903 QtdHw->Status = QTD_STAT_ACTIVE;
904 QtdHw->ErrCnt = QTD_MAX_ERR;
905 QtdHw->CurPage = 0;
906 QtdHw->TotalBytes = (UINT32) Qtd->DataLen;
907 QtdHw->Page[0] = EHC_LOW_32BIT (Qtd->Data);
908 }
909
910 //
911 // Update QH for next round of transfer. Host control only
912 // touch the fields in transfer overlay area. Only need to
913 // zero out the overlay area and set NextQtd to the first
914 // QTD. DateToggle bit is left untouched.
915 //
916 QhHw = &Urb->Qh->QhHw;
917 QhHw->CurQtd = QTD_LINK (0, TRUE);
918 QhHw->AltQtd = 0;
919
920 QhHw->Status = 0;
921 QhHw->Pid = 0;
922 QhHw->ErrCnt = 0;
923 QhHw->CurPage = 0;
924 QhHw->Ioc = 0;
925 QhHw->TotalBytes = 0;
926
927 for (Index = 0; Index < 5; Index++) {
928 QhHw->Page[Index] = 0;
929 QhHw->PageHigh[Index] = 0;
930 }
931
932 PciAddr = UsbHcGetPciAddressForHostMem (Ehc->MemPool, FirstQtd, sizeof (EHC_QTD));
933 QhHw->NextQtd = QTD_LINK (PciAddr, FALSE);
934 }
935
936 return ;
937 }
938
939
940 /**
941 Interrupt transfer periodic check handler.
942
943 @param Event Interrupt event.
944 @param Context Pointer to USB2_HC_DEV.
945
946 **/
947 VOID
948 EFIAPI
949 EhcMonitorAsyncRequests (
950 IN EFI_EVENT Event,
951 IN VOID *Context
952 )
953 {
954 USB2_HC_DEV *Ehc;
955 EFI_TPL OldTpl;
956 LIST_ENTRY *Entry;
957 LIST_ENTRY *Next;
958 BOOLEAN Finished;
959 UINT8 *ProcBuf;
960 URB *Urb;
961 EFI_STATUS Status;
962
963 OldTpl = gBS->RaiseTPL (EHC_TPL);
964 Ehc = (USB2_HC_DEV *) Context;
965
966 EFI_LIST_FOR_EACH_SAFE (Entry, Next, &Ehc->AsyncIntTransfers) {
967 Urb = EFI_LIST_CONTAINER (Entry, URB, UrbList);
968
969 //
970 // Check the result of URB execution. If it is still
971 // active, check the next one.
972 //
973 Finished = EhcCheckUrbResult (Ehc, Urb);
974
975 if (!Finished) {
976 continue;
977 }
978
979 //
980 // Flush any PCI posted write transactions from a PCI host
981 // bridge to system memory.
982 //
983 Status = EhcFlushAsyncIntMap (Ehc, Urb);
984 if (EFI_ERROR (Status)) {
985 DEBUG ((EFI_D_ERROR, "EhcMonitorAsyncRequests: Fail to Flush AsyncInt Mapped Memeory\n"));
986 }
987
988 //
989 // Allocate a buffer then copy the transferred data for user.
990 // If failed to allocate the buffer, update the URB for next
991 // round of transfer. Ignore the data of this round.
992 //
993 ProcBuf = NULL;
994
995 if (Urb->Result == EFI_USB_NOERROR) {
996 ASSERT (Urb->Completed <= Urb->DataLen);
997
998 ProcBuf = AllocatePool (Urb->Completed);
999
1000 if (ProcBuf == NULL) {
1001 EhcUpdateAsyncRequest (Ehc, Urb);
1002 continue;
1003 }
1004
1005 CopyMem (ProcBuf, Urb->Data, Urb->Completed);
1006 }
1007
1008 EhcUpdateAsyncRequest (Ehc, Urb);
1009
1010 //
1011 // Leave error recovery to its related device driver. A
1012 // common case of the error recovery is to re-submit the
1013 // interrupt transfer which is linked to the head of the
1014 // list. This function scans from head to tail. So the
1015 // re-submitted interrupt transfer's callback function
1016 // will not be called again in this round. Don't touch this
1017 // URB after the callback, it may have been removed by the
1018 // callback.
1019 //
1020 if (Urb->Callback != NULL) {
1021 //
1022 // Restore the old TPL, USB bus maybe connect device in
1023 // his callback. Some drivers may has a lower TPL restriction.
1024 //
1025 gBS->RestoreTPL (OldTpl);
1026 (Urb->Callback) (ProcBuf, Urb->Completed, Urb->Context, Urb->Result);
1027 OldTpl = gBS->RaiseTPL (EHC_TPL);
1028 }
1029
1030 if (ProcBuf != NULL) {
1031 FreePool (ProcBuf);
1032 }
1033 }
1034
1035 gBS->RestoreTPL (OldTpl);
1036 }