]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Pci/UhciDxe/UhciSched.c
Update the copyright notice format
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / UhciDxe / UhciSched.c
CommitLineData
913cb9dc 1/** @file\r
2\r
ab6495ea 3 The EHCI register operation routines.\r
4\r
cd5ebaa0
HT
5Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>\r
6This program and the accompanying materials\r
913cb9dc 7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
913cb9dc 14**/\r
15\r
16#include "Uhci.h"\r
17\r
18\r
19/**\r
ab6495ea 20 Create Frame List Structure.\r
913cb9dc 21\r
ab6495ea 22 @param Uhc UHCI device.\r
913cb9dc 23\r
ab6495ea 24 @retval EFI_OUT_OF_RESOURCES Can't allocate memory resources.\r
25 @retval EFI_UNSUPPORTED Map memory fail.\r
26 @retval EFI_SUCCESS Success.\r
913cb9dc 27\r
28**/\r
29EFI_STATUS\r
30UhciInitFrameList (\r
31 IN USB_HC_DEV *Uhc\r
32 )\r
33{\r
34 EFI_PHYSICAL_ADDRESS MappedAddr;\r
35 EFI_STATUS Status;\r
36 VOID *Buffer;\r
37 VOID *Mapping;\r
38 UINTN Pages;\r
39 UINTN Bytes;\r
40 UINTN Index;\r
3af875e2 41 EFI_PHYSICAL_ADDRESS PhyAddr;\r
913cb9dc 42\r
43 //\r
44 // The Frame List is a common buffer that will be\r
45 // accessed by both the cpu and the usb bus master\r
46 // at the same time. The Frame List ocupies 4K bytes,\r
47 // and must be aligned on 4-Kbyte boundaries.\r
48 //\r
49 Bytes = 4096;\r
50 Pages = EFI_SIZE_TO_PAGES (Bytes);\r
51\r
52 Status = Uhc->PciIo->AllocateBuffer (\r
53 Uhc->PciIo,\r
54 AllocateAnyPages,\r
55 EfiBootServicesData,\r
56 Pages,\r
57 &Buffer,\r
58 0\r
59 );\r
60\r
61 if (EFI_ERROR (Status)) {\r
62 return EFI_OUT_OF_RESOURCES;\r
63 }\r
64\r
65 Status = Uhc->PciIo->Map (\r
66 Uhc->PciIo,\r
67 EfiPciIoOperationBusMasterCommonBuffer,\r
68 Buffer,\r
69 &Bytes,\r
70 &MappedAddr,\r
71 &Mapping\r
72 );\r
73\r
74 if (EFI_ERROR (Status) || (Bytes != 4096)) {\r
75 Status = EFI_UNSUPPORTED;\r
76 goto ON_ERROR;\r
77 }\r
78\r
aa91de05 79 Uhc->FrameBase = (UINT32 *) (UINTN) Buffer;\r
3af875e2 80 Uhc->FrameMapping = Mapping;\r
913cb9dc 81\r
aa91de05 82 //\r
83 // Tell the Host Controller where the Frame List lies,\r
84 // by set the Frame List Base Address Register.\r
85 //\r
6691cae9 86 UhciSetFrameListBaseAddr (Uhc->PciIo, (VOID *) (UINTN) MappedAddr);\r
aa91de05 87\r
913cb9dc 88 //\r
89 // Allocate the QH used by sync interrupt/control/bulk transfer.\r
90 // FS ctrl/bulk queue head is set to loopback so additional BW\r
91 // can be reclaimed. Notice, LS don't support bulk transfer and\r
92 // also doesn't support BW reclamation.\r
93 //\r
94 Uhc->SyncIntQh = UhciCreateQh (Uhc, 1);\r
95 Uhc->CtrlQh = UhciCreateQh (Uhc, 1);\r
96 Uhc->BulkQh = UhciCreateQh (Uhc, 1);\r
97\r
98 if ((Uhc->SyncIntQh == NULL) || (Uhc->CtrlQh == NULL) || (Uhc->BulkQh == NULL)) {\r
99 Uhc->PciIo->Unmap (Uhc->PciIo, Mapping);\r
100 Status = EFI_OUT_OF_RESOURCES;\r
101 goto ON_ERROR;\r
102 }\r
103\r
104 //\r
105 // +-------------+\r
106 // | |\r
107 // Link the three together: SyncIntQh->CtrlQh->BulkQh <---------+\r
108 // Each frame entry is linked to this sequence of QH. These QH\r
109 // will remain on the schedul, never got removed\r
110 //\r
aa91de05 111 PhyAddr = UsbHcGetPciAddressForHostMem (Uhc->MemPool, Uhc->CtrlQh, sizeof (UHCI_QH_HW));\r
3af875e2 112 Uhc->SyncIntQh->QhHw.HorizonLink = QH_HLINK (PhyAddr, FALSE);\r
913cb9dc 113 Uhc->SyncIntQh->NextQh = Uhc->CtrlQh;\r
114\r
aa91de05 115 PhyAddr = UsbHcGetPciAddressForHostMem (Uhc->MemPool, Uhc->BulkQh, sizeof (UHCI_QH_HW));\r
3af875e2 116 Uhc->CtrlQh->QhHw.HorizonLink = QH_HLINK (PhyAddr, FALSE);\r
913cb9dc 117 Uhc->CtrlQh->NextQh = Uhc->BulkQh;\r
118\r
119 //\r
120 // Some old platform such as Intel's Tiger 4 has a difficult time\r
121 // in supporting the full speed bandwidth reclamation in the previous\r
122 // mentioned form. Most new platforms don't suffer it.\r
123 //\r
3af875e2 124 Uhc->BulkQh->QhHw.HorizonLink = QH_HLINK (PhyAddr, FALSE);\r
913cb9dc 125\r
126 Uhc->BulkQh->NextQh = NULL;\r
127\r
aa91de05 128 Uhc->FrameBaseHostAddr = AllocateZeroPool (4096);\r
129 if (Uhc->FrameBaseHostAddr == NULL) {\r
130 Status = EFI_OUT_OF_RESOURCES;\r
131 goto ON_ERROR;\r
132 }\r
3af875e2 133\r
aa91de05 134 PhyAddr = UsbHcGetPciAddressForHostMem (Uhc->MemPool, Uhc->SyncIntQh, sizeof (UHCI_QH_HW));\r
913cb9dc 135 for (Index = 0; Index < UHCI_FRAME_NUM; Index++) {\r
aa91de05 136 Uhc->FrameBase[Index] = QH_HLINK (PhyAddr, FALSE);\r
137 Uhc->FrameBaseHostAddr[Index] = (UINT32)(UINTN)Uhc->SyncIntQh;\r
913cb9dc 138 }\r
139\r
913cb9dc 140 return EFI_SUCCESS;\r
141\r
142ON_ERROR:\r
143 if (Uhc->SyncIntQh != NULL) {\r
144 UsbHcFreeMem (Uhc->MemPool, Uhc->SyncIntQh, sizeof (UHCI_QH_SW));\r
145 }\r
146\r
147 if (Uhc->CtrlQh != NULL) {\r
148 UsbHcFreeMem (Uhc->MemPool, Uhc->CtrlQh, sizeof (UHCI_QH_SW));\r
149 }\r
150\r
151 if (Uhc->BulkQh != NULL) {\r
152 UsbHcFreeMem (Uhc->MemPool, Uhc->BulkQh, sizeof (UHCI_QH_SW));\r
153 }\r
154\r
155 Uhc->PciIo->FreeBuffer (Uhc->PciIo, Pages, Buffer);\r
156 return Status;\r
157}\r
158\r
159\r
160/**\r
ab6495ea 161 Destory FrameList buffer.\r
913cb9dc 162\r
ab6495ea 163 @param Uhc The UHCI device.\r
913cb9dc 164\r
913cb9dc 165**/\r
166VOID\r
167UhciDestoryFrameList (\r
168 IN USB_HC_DEV *Uhc\r
169 )\r
170{\r
171 //\r
172 // Unmap the common buffer for framelist entry,\r
173 // and free the common buffer.\r
174 // Uhci's frame list occupy 4k memory.\r
175 //\r
176 Uhc->PciIo->Unmap (Uhc->PciIo, Uhc->FrameMapping);\r
177\r
178 Uhc->PciIo->FreeBuffer (\r
179 Uhc->PciIo,\r
180 EFI_SIZE_TO_PAGES (4096),\r
181 (VOID *) Uhc->FrameBase\r
182 );\r
183\r
aa91de05 184 if (Uhc->FrameBaseHostAddr != NULL) {\r
185 FreePool (Uhc->FrameBaseHostAddr);\r
186 }\r
187\r
913cb9dc 188 if (Uhc->SyncIntQh != NULL) {\r
189 UsbHcFreeMem (Uhc->MemPool, Uhc->SyncIntQh, sizeof (UHCI_QH_SW));\r
190 }\r
191\r
192 if (Uhc->CtrlQh != NULL) {\r
193 UsbHcFreeMem (Uhc->MemPool, Uhc->CtrlQh, sizeof (UHCI_QH_SW));\r
194 }\r
195\r
196 if (Uhc->BulkQh != NULL) {\r
197 UsbHcFreeMem (Uhc->MemPool, Uhc->BulkQh, sizeof (UHCI_QH_SW));\r
198 }\r
199\r
3af875e2 200 Uhc->FrameBase = NULL;\r
aa91de05 201 Uhc->FrameBaseHostAddr = NULL;\r
3af875e2 202 Uhc->SyncIntQh = NULL;\r
203 Uhc->CtrlQh = NULL;\r
204 Uhc->BulkQh = NULL;\r
913cb9dc 205}\r
206\r
207\r
208/**\r
209 Convert the poll rate to the maxium 2^n that is smaller\r
ab6495ea 210 than Interval.\r
913cb9dc 211\r
ab6495ea 212 @param Interval The poll rate to convert.\r
913cb9dc 213\r
ab6495ea 214 @return The converted poll rate.\r
913cb9dc 215\r
216**/\r
217UINTN\r
218UhciConvertPollRate (\r
219 IN UINTN Interval\r
220 )\r
221{\r
222 UINTN BitCount;\r
223\r
224 ASSERT (Interval != 0);\r
225\r
226 //\r
227 // Find the index (1 based) of the highest non-zero bit\r
228 //\r
229 BitCount = 0;\r
230\r
231 while (Interval != 0) {\r
232 Interval >>= 1;\r
233 BitCount++;\r
234 }\r
235\r
236 return (UINTN)1 << (BitCount - 1);\r
237}\r
238\r
239\r
240/**\r
241 Link a queue head (for asynchronous interrupt transfer) to\r
242 the frame list.\r
243\r
3af875e2 244 @param Uhc The UHCI device.\r
ab6495ea 245 @param Qh The queue head to link into.\r
913cb9dc 246\r
913cb9dc 247**/\r
248VOID\r
249UhciLinkQhToFrameList (\r
3af875e2 250 USB_HC_DEV *Uhc,\r
913cb9dc 251 UHCI_QH_SW *Qh\r
252 )\r
253{\r
254 UINTN Index;\r
255 UHCI_QH_SW *Prev;\r
256 UHCI_QH_SW *Next;\r
3af875e2 257 EFI_PHYSICAL_ADDRESS PhyAddr;\r
258 EFI_PHYSICAL_ADDRESS QhPciAddr;\r
3af875e2 259\r
260 ASSERT ((Uhc->FrameBase != NULL) && (Qh != NULL));\r
913cb9dc 261\r
aa91de05 262 QhPciAddr = UsbHcGetPciAddressForHostMem (Uhc->MemPool, Qh, sizeof (UHCI_QH_HW));\r
913cb9dc 263\r
264 for (Index = 0; Index < UHCI_FRAME_NUM; Index += Qh->Interval) {\r
265 //\r
266 // First QH can't be NULL because we always keep static queue\r
267 // heads on the frame list\r
268 //\r
3af875e2 269 ASSERT (!LINK_TERMINATED (Uhc->FrameBase[Index]));\r
aa91de05 270 Next = (UHCI_QH_SW*)(UINTN)Uhc->FrameBaseHostAddr[Index];\r
913cb9dc 271 Prev = NULL;\r
272\r
273 //\r
274 // Now, insert the queue head (Qh) into this frame:\r
275 // 1. Find a queue head with the same poll interval, just insert\r
276 // Qh after this queue head, then we are done.\r
277 //\r
278 // 2. Find the position to insert the queue head into:\r
279 // Previous head's interval is bigger than Qh's\r
280 // Next head's interval is less than Qh's\r
281 // Then, insert the Qh between then\r
282 //\r
283 // This method is very much the same as that used by EHCI.\r
284 // Because each QH's interval is round down to 2^n, poll\r
285 // rate is correct.\r
286 //\r
287 while (Next->Interval > Qh->Interval) {\r
288 Prev = Next;\r
289 Next = Next->NextQh;\r
3af875e2 290 ASSERT (Next != NULL);\r
913cb9dc 291 }\r
292\r
913cb9dc 293 //\r
294 // The entry may have been linked into the frame by early insertation.\r
295 // For example: if insert a Qh with Qh.Interval == 4, and there is a Qh\r
296 // with Qh.Interval == 8 on the frame. If so, we are done with this frame.\r
297 // It isn't necessary to compare all the QH with the same interval to\r
298 // Qh. This is because if there is other QH with the same interval, Qh\r
299 // should has been inserted after that at FrameBase[0] and at FrameBase[0] it is\r
300 // impossible (Next == Qh)\r
301 //\r
302 if (Next == Qh) {\r
303 continue;\r
304 }\r
305\r
306 if (Next->Interval == Qh->Interval) {\r
307 //\r
308 // If there is a QH with the same interval, it locates at\r
309 // FrameBase[0], and we can simply insert it after this QH. We\r
310 // are all done.\r
311 //\r
312 ASSERT ((Index == 0) && (Qh->NextQh == NULL));\r
313\r
314 Prev = Next;\r
315 Next = Next->NextQh;\r
316\r
317 Qh->NextQh = Next;\r
318 Prev->NextQh = Qh;\r
319\r
320 Qh->QhHw.HorizonLink = Prev->QhHw.HorizonLink;\r
3af875e2 321\r
322 Prev->QhHw.HorizonLink = QH_HLINK (QhPciAddr, FALSE);\r
913cb9dc 323 break;\r
324 }\r
325\r
326 //\r
327 // OK, find the right position, insert it in. If Qh's next\r
328 // link has already been set, it is in position. This is\r
329 // guarranted by 2^n polling interval.\r
330 //\r
331 if (Qh->NextQh == NULL) {\r
332 Qh->NextQh = Next;\r
aa91de05 333 PhyAddr = UsbHcGetPciAddressForHostMem (Uhc->MemPool, Next, sizeof (UHCI_QH_HW));\r
3af875e2 334 Qh->QhHw.HorizonLink = QH_HLINK (PhyAddr, FALSE);\r
913cb9dc 335 }\r
336\r
337 if (Prev == NULL) {\r
aa91de05 338 Uhc->FrameBase[Index] = QH_HLINK (QhPciAddr, FALSE);\r
339 Uhc->FrameBaseHostAddr[Index] = (UINT32)(UINTN)Qh;\r
913cb9dc 340 } else {\r
341 Prev->NextQh = Qh;\r
3af875e2 342 Prev->QhHw.HorizonLink = QH_HLINK (QhPciAddr, FALSE);\r
913cb9dc 343 }\r
344 }\r
345}\r
346\r
347\r
348/**\r
349 Unlink QH from the frame list is easier: find all\r
350 the precedence node, and pointer there next to QhSw's\r
351 next.\r
352\r
3af875e2 353 @param Uhc The UHCI device.\r
ab6495ea 354 @param Qh The queue head to unlink.\r
913cb9dc 355\r
913cb9dc 356**/\r
357VOID\r
358UhciUnlinkQhFromFrameList (\r
3af875e2 359 USB_HC_DEV *Uhc,\r
360 UHCI_QH_SW *Qh\r
913cb9dc 361 )\r
362{\r
363 UINTN Index;\r
364 UHCI_QH_SW *Prev;\r
365 UHCI_QH_SW *This;\r
366\r
3af875e2 367 ASSERT ((Uhc->FrameBase != NULL) && (Qh != NULL));\r
913cb9dc 368\r
369 for (Index = 0; Index < UHCI_FRAME_NUM; Index += Qh->Interval) {\r
370 //\r
371 // Frame link can't be NULL because we always keep static\r
372 // queue heads on the frame list\r
373 //\r
3af875e2 374 ASSERT (!LINK_TERMINATED (Uhc->FrameBase[Index]));\r
aa91de05 375 This = (UHCI_QH_SW*)(UINTN)Uhc->FrameBaseHostAddr[Index];\r
913cb9dc 376 Prev = NULL;\r
377\r
378 //\r
379 // Walk through the frame's QH list to find the\r
380 // queue head to remove\r
381 //\r
382 while ((This != NULL) && (This != Qh)) {\r
383 Prev = This;\r
384 This = This->NextQh;\r
385 }\r
386\r
387 //\r
388 // Qh may have already been unlinked from this frame\r
389 // by early action.\r
390 //\r
391 if (This == NULL) {\r
392 continue;\r
393 }\r
394\r
395 if (Prev == NULL) {\r
396 //\r
397 // Qh is the first entry in the frame\r
398 //\r
aa91de05 399 Uhc->FrameBase[Index] = Qh->QhHw.HorizonLink;\r
400 Uhc->FrameBaseHostAddr[Index] = (UINT32)(UINTN)Qh->NextQh;\r
913cb9dc 401 } else {\r
402 Prev->NextQh = Qh->NextQh;\r
403 Prev->QhHw.HorizonLink = Qh->QhHw.HorizonLink;\r
404 }\r
405 }\r
406}\r
407\r
408\r
409/**\r
ab6495ea 410 Check TDs Results.\r
913cb9dc 411\r
ab6495ea 412 @param Uhc This UHCI device.\r
413 @param Td UHCI_TD_SW to check.\r
414 @param IsLow Is Low Speed Device.\r
415 @param QhResult Return the result of this TD list.\r
913cb9dc 416\r
417 @return Whether the TD's result is finialized.\r
418\r
419**/\r
913cb9dc 420BOOLEAN\r
421UhciCheckTdStatus (\r
422 IN USB_HC_DEV *Uhc,\r
423 IN UHCI_TD_SW *Td,\r
424 IN BOOLEAN IsLow,\r
425 OUT UHCI_QH_RESULT *QhResult\r
426 )\r
427{\r
428 UINTN Len;\r
429 UINT8 State;\r
430 UHCI_TD_HW *TdHw;\r
431 BOOLEAN Finished;\r
432\r
433 Finished = TRUE;\r
434\r
435 //\r
436 // Initialize the data toggle to that of the first\r
437 // TD. The next toggle to use is either:\r
438 // 1. first TD's toggle if no TD is executed OK\r
439 // 2. the next toggle of last executed-OK TD\r
440 //\r
441 QhResult->Result = EFI_USB_NOERROR;\r
442 QhResult->NextToggle = (UINT8)Td->TdHw.DataToggle;\r
443 QhResult->Complete = 0;\r
444\r
445 while (Td != NULL) {\r
446 TdHw = &Td->TdHw;\r
447 State = (UINT8)TdHw->Status;\r
448\r
449 //\r
450 // UHCI will set STALLED bit when it abort the execution\r
451 // of TD list. There are several reasons:\r
452 // 1. BABBLE error happened\r
453 // 2. Received a STALL response\r
454 // 3. Error count decreased to zero.\r
455 //\r
456 // It also set CRC/Timeout/NAK/Buffer Error/BitStuff Error\r
457 // bits when corresponding conditions happen. But these\r
458 // conditions are not deadly, that is a TD can successfully\r
459 // completes even these bits are set. But it is likely that\r
460 // upper layer won't distinguish these condtions. So, only\r
461 // set these bits when TD is actually halted.\r
462 //\r
ab6495ea 463 if ((State & USBTD_STALLED) != 0) {\r
464 if ((State & USBTD_BABBLE) != 0) {\r
913cb9dc 465 QhResult->Result |= EFI_USB_ERR_BABBLE;\r
466\r
467 } else if (TdHw->ErrorCount != 0) {\r
468 QhResult->Result |= EFI_USB_ERR_STALL;\r
469 }\r
470\r
ab6495ea 471 if ((State & USBTD_CRC) != 0) {\r
913cb9dc 472 QhResult->Result |= EFI_USB_ERR_CRC;\r
473 }\r
474\r
ab6495ea 475 if ((State & USBTD_BUFFERR) != 0) {\r
913cb9dc 476 QhResult->Result |= EFI_USB_ERR_BUFFER;\r
477 }\r
478\r
ab6495ea 479 if ((Td->TdHw.Status & USBTD_BITSTUFF) != 0) {\r
913cb9dc 480 QhResult->Result |= EFI_USB_ERR_BITSTUFF;\r
481 }\r
482\r
483 if (TdHw->ErrorCount == 0) {\r
484 QhResult->Result |= EFI_USB_ERR_TIMEOUT;\r
485 }\r
486\r
487 Finished = TRUE;\r
488 goto ON_EXIT;\r
489\r
ab6495ea 490 } else if ((State & USBTD_ACTIVE) != 0) {\r
913cb9dc 491 //\r
492 // The TD is still active, no need to check further.\r
493 //\r
494 QhResult->Result |= EFI_USB_ERR_NOTEXECUTE;\r
495\r
496 Finished = FALSE;\r
497 goto ON_EXIT;\r
498\r
499 } else {\r
500 //\r
501 // Update the next data toggle, it is always the\r
502 // next to the last known-good TD's data toggle if\r
503 // any TD is executed OK\r
504 //\r
c52fa98c 505 QhResult->NextToggle = (UINT8) (1 - (UINT8)TdHw->DataToggle);\r
913cb9dc 506\r
507 //\r
508 // This TD is finished OK or met short packet read. Update the\r
509 // transfer length if it isn't a SETUP.\r
510 //\r
511 Len = (TdHw->ActualLen + 1) & 0x7FF;\r
512\r
513 if (TdHw->PidCode != SETUP_PACKET_ID) {\r
514 QhResult->Complete += Len;\r
515 }\r
516\r
517 //\r
518 // Short packet condition for full speed input TD, also\r
519 // terminate the transfer\r
520 //\r
521 if (!IsLow && (TdHw->ShortPacket == 1) && (Len < Td->DataLen)) {\r
1c619535 522 DEBUG ((EFI_D_INFO, "UhciCheckTdStatus: short packet read occured\n"));\r
913cb9dc 523\r
524 Finished = TRUE;\r
525 goto ON_EXIT;\r
526 }\r
527 }\r
528\r
529 Td = Td->NextTd;\r
530 }\r
531\r
532ON_EXIT:\r
533 //\r
534 // Check whether HC is halted. Don't move this up. It must be\r
535 // called after data toggle is successfully updated.\r
536 //\r
537 if (!UhciIsHcWorking (Uhc->PciIo)) {\r
538 QhResult->Result |= EFI_USB_ERR_SYSTEM;\r
539 Finished = TRUE;\r
540 }\r
541\r
542 if (Finished) {\r
543 Uhc->PciIo->Flush (Uhc->PciIo);\r
544 }\r
545\r
546 UhciAckAllInterrupt (Uhc);\r
547 return Finished;\r
548}\r
549\r
550\r
913cb9dc 551/**\r
ab6495ea 552 Check the result of the transfer.\r
913cb9dc 553\r
ab6495ea 554 @param Uhc The UHCI device.\r
555 @param Qh The queue head of the transfer.\r
556 @param Td The first TDs of the transfer.\r
557 @param TimeOut TimeOut value in milliseconds.\r
558 @param IsLow Is Low Speed Device.\r
559 @param QhResult The variable to return result.\r
913cb9dc 560\r
ab6495ea 561 @retval EFI_SUCCESS The transfer finished with success.\r
562 @retval EFI_DEVICE_ERROR Transfer failed.\r
913cb9dc 563\r
564**/\r
565EFI_STATUS\r
566UhciExecuteTransfer (\r
567 IN USB_HC_DEV *Uhc,\r
568 IN UHCI_QH_SW *Qh,\r
569 IN UHCI_TD_SW *Td,\r
570 IN UINTN TimeOut,\r
571 IN BOOLEAN IsLow,\r
572 OUT UHCI_QH_RESULT *QhResult\r
573 )\r
574{\r
575 UINTN Index;\r
576 UINTN Delay;\r
577 BOOLEAN Finished;\r
578 EFI_STATUS Status;\r
579\r
580 Finished = FALSE;\r
581 Status = EFI_SUCCESS;\r
41e8ff27 582 Delay = (TimeOut * UHC_1_MILLISECOND / UHC_SYNC_POLL_INTERVAL) + 1;\r
1c619535 583\r
913cb9dc 584 for (Index = 0; Index < Delay; Index++) {\r
585 Finished = UhciCheckTdStatus (Uhc, Td, IsLow, QhResult);\r
586\r
587 //\r
588 // Transfer is OK or some error occured (TD inactive)\r
589 //\r
590 if (Finished) {\r
591 break;\r
592 }\r
593\r
41e8ff27 594 gBS->Stall (UHC_SYNC_POLL_INTERVAL);\r
913cb9dc 595 }\r
596\r
597 if (!Finished) {\r
7df7393f 598 DEBUG ((EFI_D_ERROR, "UhciExecuteTransfer: execution not finished for %dms\n", (UINT32)TimeOut));\r
1c619535 599 UhciDumpQh (Qh);\r
600 UhciDumpTds (Td);\r
913cb9dc 601\r
602 Status = EFI_TIMEOUT;\r
603\r
604 } else if (QhResult->Result != EFI_USB_NOERROR) {\r
1c619535 605 DEBUG ((EFI_D_ERROR, "UhciExecuteTransfer: execution failed with result %x\n", QhResult->Result));\r
606 UhciDumpQh (Qh);\r
607 UhciDumpTds (Td);\r
913cb9dc 608\r
609 Status = EFI_DEVICE_ERROR;\r
610 }\r
611\r
612 return Status;\r
613}\r
614\r
615\r
616/**\r
ab6495ea 617 Update Async Request, QH and TDs.\r
913cb9dc 618\r
3af875e2 619 @param Uhc The UHCI device.\r
ab6495ea 620 @param AsyncReq The UHCI asynchronous transfer to update.\r
621 @param Result Transfer reslut.\r
622 @param NextToggle The toggle of next data.\r
913cb9dc 623\r
913cb9dc 624**/\r
913cb9dc 625VOID\r
626UhciUpdateAsyncReq (\r
3af875e2 627 IN USB_HC_DEV *Uhc,\r
913cb9dc 628 IN UHCI_ASYNC_REQUEST *AsyncReq,\r
629 IN UINT32 Result,\r
630 IN UINT32 NextToggle\r
631 )\r
632{\r
633 UHCI_QH_SW *Qh;\r
634 UHCI_TD_SW *FirstTd;\r
635 UHCI_TD_SW *Td;\r
636\r
637 Qh = AsyncReq->QhSw;\r
638 FirstTd = AsyncReq->FirstTd;\r
639\r
640 if (Result == EFI_USB_NOERROR) {\r
641 //\r
642 // The last transfer succeeds. Then we need to update\r
643 // the Qh and Td for next round of transfer.\r
644 // 1. Update the TD's data toggle\r
645 // 2. Activate all the TDs\r
646 // 3. Link the TD to the queue head again since during\r
647 // execution, queue head's TD pointer is changed by\r
648 // hardware.\r
649 //\r
650 for (Td = FirstTd; Td != NULL; Td = Td->NextTd) {\r
651 Td->TdHw.DataToggle = NextToggle;\r
652 NextToggle ^= 1;\r
653 Td->TdHw.Status |= USBTD_ACTIVE;\r
654 }\r
655\r
3af875e2 656 UhciLinkTdToQh (Uhc, Qh, FirstTd);\r
913cb9dc 657 return ;\r
658 }\r
659}\r
660\r
661\r
662/**\r
ab6495ea 663 Create Async Request node, and Link to List.\r
664\r
665 @param Uhc The UHCI device.\r
666 @param Qh The queue head of the transfer.\r
667 @param FirstTd First TD of the transfer.\r
668 @param DevAddr Device Address.\r
669 @param EndPoint EndPoint Address.\r
670 @param DataLen Data length.\r
671 @param Interval Polling Interval when inserted to frame list.\r
ab6495ea 672 @param Data Data buffer, unmapped.\r
673 @param Callback Callback after interrupt transfeer.\r
674 @param Context Callback Context passed as function parameter.\r
675 @param IsLow Is Low Speed.\r
676\r
677 @retval EFI_SUCCESS An asynchronous transfer is created.\r
678 @retval EFI_INVALID_PARAMETER Paremeter is error.\r
913cb9dc 679 @retval EFI_OUT_OF_RESOURCES Failed because of resource shortage.\r
680\r
681**/\r
682EFI_STATUS\r
683UhciCreateAsyncReq (\r
684 IN USB_HC_DEV *Uhc,\r
685 IN UHCI_QH_SW *Qh,\r
686 IN UHCI_TD_SW *FirstTd,\r
687 IN UINT8 DevAddr,\r
688 IN UINT8 EndPoint,\r
689 IN UINTN DataLen,\r
690 IN UINTN Interval,\r
913cb9dc 691 IN UINT8 *Data,\r
692 IN EFI_ASYNC_USB_TRANSFER_CALLBACK Callback,\r
693 IN VOID *Context,\r
694 IN BOOLEAN IsLow\r
695 )\r
696{\r
697 UHCI_ASYNC_REQUEST *AsyncReq;\r
698\r
699 AsyncReq = AllocatePool (sizeof (UHCI_ASYNC_REQUEST));\r
700\r
701 if (AsyncReq == NULL) {\r
702 return EFI_OUT_OF_RESOURCES;\r
703 }\r
704\r
705 //\r
706 // Fill Request field. Data is allocated host memory, not mapped\r
707 //\r
708 AsyncReq->Signature = UHCI_ASYNC_INT_SIGNATURE;\r
709 AsyncReq->DevAddr = DevAddr;\r
710 AsyncReq->EndPoint = EndPoint;\r
711 AsyncReq->DataLen = DataLen;\r
b4c24e2d 712 AsyncReq->Interval = UhciConvertPollRate(Interval);\r
913cb9dc 713 AsyncReq->Data = Data;\r
714 AsyncReq->Callback = Callback;\r
715 AsyncReq->Context = Context;\r
716 AsyncReq->QhSw = Qh;\r
717 AsyncReq->FirstTd = FirstTd;\r
718 AsyncReq->IsLow = IsLow;\r
719\r
720 //\r
721 // Insert the new interrupt transfer to the head of the list.\r
722 // The interrupt transfer's monitor function scans the whole\r
723 // list from head to tail. The new interrupt transfer MUST be\r
724 // added to the head of the list.\r
725 //\r
726 InsertHeadList (&(Uhc->AsyncIntList), &(AsyncReq->Link));\r
727\r
728 return EFI_SUCCESS;\r
729}\r
730\r
731\r
913cb9dc 732/**\r
ab6495ea 733 Free an asynchronous request's resource such as memory.\r
913cb9dc 734\r
ab6495ea 735 @param Uhc The UHCI device.\r
736 @param AsyncReq The asynchronous request to free.\r
913cb9dc 737\r
913cb9dc 738**/\r
913cb9dc 739VOID\r
740UhciFreeAsyncReq (\r
741 IN USB_HC_DEV *Uhc,\r
742 IN UHCI_ASYNC_REQUEST *AsyncReq\r
743 )\r
744{\r
745 ASSERT ((Uhc != NULL) && (AsyncReq != NULL));\r
746\r
747 UhciDestoryTds (Uhc, AsyncReq->FirstTd);\r
748 UsbHcFreeMem (Uhc->MemPool, AsyncReq->QhSw, sizeof (UHCI_QH_SW));\r
749\r
913cb9dc 750 if (AsyncReq->Data != NULL) {\r
db731882 751 UsbHcFreeMem (Uhc->MemPool, AsyncReq->Data, AsyncReq->DataLen);\r
913cb9dc 752 }\r
753\r
754 gBS->FreePool (AsyncReq);\r
755}\r
756\r
757\r
758/**\r
759 Unlink an asynchronous request's from UHC's asynchronus list.\r
760 also remove the queue head from the frame list. If FreeNow,\r
761 release its resource also. Otherwise, add the request to the\r
762 UHC's recycle list to wait for a while before release the memory.\r
763 Until then, hardware won't hold point to the request.\r
764\r
ab6495ea 765 @param Uhc The UHCI device.\r
766 @param AsyncReq The asynchronous request to free.\r
913cb9dc 767 @param FreeNow If TRUE, free the resource immediately, otherwise\r
768 add the request to recycle wait list.\r
769\r
913cb9dc 770**/\r
913cb9dc 771VOID\r
772UhciUnlinkAsyncReq (\r
773 IN USB_HC_DEV *Uhc,\r
774 IN UHCI_ASYNC_REQUEST *AsyncReq,\r
775 IN BOOLEAN FreeNow\r
776 )\r
777{\r
778 ASSERT ((Uhc != NULL) && (AsyncReq != NULL));\r
779\r
780 RemoveEntryList (&(AsyncReq->Link));\r
3af875e2 781 UhciUnlinkQhFromFrameList (Uhc, AsyncReq->QhSw);\r
913cb9dc 782\r
783 if (FreeNow) {\r
784 UhciFreeAsyncReq (Uhc, AsyncReq);\r
785 } else {\r
786 //\r
787 // To sychronize with hardware, mark the queue head as inactive\r
788 // then add AsyncReq to UHC's recycle list\r
789 //\r
790 AsyncReq->QhSw->QhHw.VerticalLink = QH_VLINK (NULL, TRUE);\r
791 AsyncReq->Recycle = Uhc->RecycleWait;\r
792 Uhc->RecycleWait = AsyncReq;\r
793 }\r
794}\r
795\r
796\r
797/**\r
ab6495ea 798 Delete Async Interrupt QH and TDs.\r
913cb9dc 799\r
ab6495ea 800 @param Uhc The UHCI device.\r
801 @param DevAddr Device Address.\r
802 @param EndPoint EndPoint Address.\r
803 @param Toggle The next data toggle to use.\r
913cb9dc 804\r
ab6495ea 805 @retval EFI_SUCCESS The request is deleted.\r
806 @retval EFI_INVALID_PARAMETER Paremeter is error.\r
807 @retval EFI_NOT_FOUND The asynchronous isn't found.\r
913cb9dc 808\r
809**/\r
810EFI_STATUS\r
811UhciRemoveAsyncReq (\r
812 IN USB_HC_DEV *Uhc,\r
813 IN UINT8 DevAddr,\r
814 IN UINT8 EndPoint,\r
815 OUT UINT8 *Toggle\r
816 )\r
817{\r
818 EFI_STATUS Status;\r
819 UHCI_ASYNC_REQUEST *AsyncReq;\r
820 UHCI_QH_RESULT QhResult;\r
821 LIST_ENTRY *Link;\r
822 BOOLEAN Found;\r
823\r
824 Status = EFI_SUCCESS;\r
825\r
826 //\r
827 // If no asynchronous interrupt transaction exists\r
828 //\r
829 if (IsListEmpty (&(Uhc->AsyncIntList))) {\r
830 return EFI_SUCCESS;\r
831 }\r
832\r
833 //\r
834 // Find the asynchronous transfer to this device/endpoint pair\r
835 //\r
836 Found = FALSE;\r
837 Link = Uhc->AsyncIntList.ForwardLink;\r
838\r
839 do {\r
840 AsyncReq = UHCI_ASYNC_INT_FROM_LINK (Link);\r
841 Link = Link->ForwardLink;\r
842\r
843 if ((AsyncReq->DevAddr == DevAddr) && (AsyncReq->EndPoint == EndPoint)) {\r
844 Found = TRUE;\r
845 break;\r
846 }\r
847\r
848 } while (Link != &(Uhc->AsyncIntList));\r
849\r
850 if (!Found) {\r
851 return EFI_NOT_FOUND;\r
852 }\r
853\r
854 //\r
855 // Check the result of the async transfer then update it\r
856 // to get the next data toggle to use.\r
857 //\r
858 UhciCheckTdStatus (Uhc, AsyncReq->FirstTd, AsyncReq->IsLow, &QhResult);\r
859 *Toggle = QhResult.NextToggle;\r
860\r
861 //\r
862 // Don't release the request now, keep it to synchronize with hardware.\r
863 //\r
864 UhciUnlinkAsyncReq (Uhc, AsyncReq, FALSE);\r
865 return Status;\r
866}\r
867\r
868\r
869/**\r
870 Recycle the asynchronouse request. When a queue head\r
871 is unlinked from frame list, host controller hardware\r
872 may still hold a cached pointer to it. To synchronize\r
873 with hardware, the request is released in two steps:\r
874 first it is linked to the UHC's RecycleWait list. At\r
875 the next time UhciMonitorAsyncReqList is fired, it is\r
876 moved to UHC's Recylelist. Then, at another timer\r
877 activation, all the requests on Recycle list is freed.\r
878 This guarrantes that each unlink queue head keeps\r
879 existing for at least 50ms, far enough for the hardware\r
880 to clear its cache.\r
881\r
ab6495ea 882 @param Uhc The UHCI device.\r
913cb9dc 883\r
913cb9dc 884**/\r
913cb9dc 885VOID\r
886UhciRecycleAsyncReq (\r
887 IN USB_HC_DEV *Uhc\r
888 )\r
889{\r
890 UHCI_ASYNC_REQUEST *Req;\r
891 UHCI_ASYNC_REQUEST *Next;\r
892\r
893 Req = Uhc->Recycle;\r
894\r
895 while (Req != NULL) {\r
896 Next = Req->Recycle;\r
897 UhciFreeAsyncReq (Uhc, Req);\r
898 Req = Next;\r
899 }\r
900\r
901 Uhc->Recycle = Uhc->RecycleWait;\r
902 Uhc->RecycleWait = NULL;\r
903}\r
904\r
905\r
906\r
907/**\r
908 Release all the asynchronous transfers on the lsit.\r
909\r
ab6495ea 910 @param Uhc The UHCI device.\r
913cb9dc 911\r
913cb9dc 912**/\r
913VOID\r
914UhciFreeAllAsyncReq (\r
915 IN USB_HC_DEV *Uhc\r
916 )\r
917{\r
918 LIST_ENTRY *Head;\r
919 UHCI_ASYNC_REQUEST *AsyncReq;\r
920\r
921 //\r
922 // Call UhciRecycleAsyncReq twice. The requests on Recycle\r
923 // will be released at the first call; The requests on\r
924 // RecycleWait will be released at the second call.\r
925 //\r
926 UhciRecycleAsyncReq (Uhc);\r
927 UhciRecycleAsyncReq (Uhc);\r
928\r
929 Head = &(Uhc->AsyncIntList);\r
930\r
931 if (IsListEmpty (Head)) {\r
932 return;\r
933 }\r
934\r
935 while (!IsListEmpty (Head)) {\r
936 AsyncReq = UHCI_ASYNC_INT_FROM_LINK (Head->ForwardLink);\r
937 UhciUnlinkAsyncReq (Uhc, AsyncReq, TRUE);\r
938 }\r
939}\r
940\r
941\r
942/**\r
ab6495ea 943 Interrupt transfer periodic check handler.\r
913cb9dc 944\r
ab6495ea 945 @param Event The event of the time.\r
946 @param Context Context of the event, pointer to USB_HC_DEV.\r
913cb9dc 947\r
913cb9dc 948**/\r
949VOID\r
6d3ea23f 950EFIAPI\r
913cb9dc 951UhciMonitorAsyncReqList (\r
952 IN EFI_EVENT Event,\r
953 IN VOID *Context\r
954 )\r
955{\r
956 UHCI_ASYNC_REQUEST *AsyncReq;\r
957 LIST_ENTRY *Link;\r
958 USB_HC_DEV *Uhc;\r
959 VOID *Data;\r
960 BOOLEAN Finished;\r
961 UHCI_QH_RESULT QhResult;\r
962\r
963 Uhc = (USB_HC_DEV *) Context;\r
964\r
965 //\r
966 // Recycle the asynchronous requests expired, and promote\r
967 // requests waiting to be recycled the next time when this\r
968 // timer expires\r
969 //\r
970 UhciRecycleAsyncReq (Uhc);\r
971\r
972 if (IsListEmpty (&(Uhc->AsyncIntList))) {\r
973 return ;\r
974 }\r
975\r
976 //\r
977 // This loop must be delete safe\r
978 //\r
979 Link = Uhc->AsyncIntList.ForwardLink;\r
980\r
981 do {\r
982 AsyncReq = UHCI_ASYNC_INT_FROM_LINK (Link);\r
983 Link = Link->ForwardLink;\r
984\r
985 Finished = UhciCheckTdStatus (Uhc, AsyncReq->FirstTd, AsyncReq->IsLow, &QhResult);\r
986\r
987 if (!Finished) {\r
988 continue;\r
989 }\r
990\r
991 //\r
992 // Copy the data to temporary buffer if there are some\r
993 // data transferred. We may have zero-length packet\r
994 //\r
995 Data = NULL;\r
996\r
997 if (QhResult.Complete != 0) {\r
998 Data = AllocatePool (QhResult.Complete);\r
999\r
1000 if (Data == NULL) {\r
1001 return ;\r
1002 }\r
1003\r
1004 CopyMem (Data, AsyncReq->FirstTd->Data, QhResult.Complete);\r
1005 }\r
1006\r
3af875e2 1007 UhciUpdateAsyncReq (Uhc, AsyncReq, QhResult.Result, QhResult.NextToggle);\r
913cb9dc 1008\r
1009 //\r
1010 // Now, either transfer is SUCCESS or met errors since\r
1011 // we have skipped to next transfer earlier if current\r
1012 // transfer is still active.\r
1013 //\r
1014 if (QhResult.Result == EFI_USB_NOERROR) {\r
1015 AsyncReq->Callback (Data, QhResult.Complete, AsyncReq->Context, QhResult.Result);\r
1016 } else {\r
1017 //\r
1018 // Leave error recovery to its related device driver.\r
1019 // A common case of the error recovery is to re-submit\r
1020 // the interrupt transfer. When an interrupt transfer\r
1021 // is re-submitted, its position in the linked list is\r
1022 // changed. It is inserted to the head of the linked\r
1023 // list, while this function scans the whole list from\r
1024 // head to tail. Thus, the re-submitted interrupt transfer's\r
1025 // callback function will not be called again in this round.\r
1026 //\r
1027 AsyncReq->Callback (NULL, 0, AsyncReq->Context, QhResult.Result);\r
1028 }\r
1029\r
1030 if (Data != NULL) {\r
1031 gBS->FreePool (Data);\r
1032 }\r
1033 } while (Link != &(Uhc->AsyncIntList));\r
1034}\r