]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Pci/SdMmcPciHcDxe/SdMmcPciHcDxe.c
MdeModulePkg/Usb: Fix wrong condition judgment to support usb3.1 dev
[mirror_edk2.git] / MdeModulePkg / Bus / Pci / SdMmcPciHcDxe / SdMmcPciHcDxe.c
CommitLineData
48555339
FT
1/** @file\r
2 This driver is used to manage SD/MMC PCI host controllers which are compliance\r
3 with SD Host Controller Simplified Specification version 3.00.\r
4\r
5 It would expose EFI_SD_MMC_PASS_THRU_PROTOCOL for upper layer use.\r
6\r
7 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>\r
8 This program and the accompanying materials\r
9 are licensed and made available under the terms and conditions of the BSD License\r
10 which accompanies this distribution. The full text of the license may be found at\r
11 http://opensource.org/licenses/bsd-license.php\r
12\r
13 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17\r
18#include "SdMmcPciHcDxe.h"\r
19\r
20//\r
21// Driver Global Variables\r
22//\r
23EFI_DRIVER_BINDING_PROTOCOL gSdMmcPciHcDriverBinding = {\r
24 SdMmcPciHcDriverBindingSupported,\r
25 SdMmcPciHcDriverBindingStart,\r
26 SdMmcPciHcDriverBindingStop,\r
27 0x10,\r
28 NULL,\r
29 NULL\r
30};\r
31\r
32//\r
33// Template for SD/MMC host controller private data.\r
34//\r
35SD_MMC_HC_PRIVATE_DATA gSdMmcPciHcTemplate = {\r
36 SD_MMC_HC_PRIVATE_SIGNATURE, // Signature\r
37 NULL, // ControllerHandle\r
38 NULL, // PciIo\r
39 { // PassThru\r
40 sizeof (UINT32),\r
41 SdMmcPassThruPassThru,\r
42 SdMmcPassThruGetNextSlot,\r
43 SdMmcPassThruBuildDevicePath,\r
44 SdMmcPassThruGetSlotNumber,\r
45 SdMmcPassThruResetDevice\r
46 },\r
47 0, // PciAttributes\r
48 0, // PreviousSlot\r
49 NULL, // TimerEvent\r
50 NULL, // ConnectEvent\r
51 // Queue\r
52 INITIALIZE_LIST_HEAD_VARIABLE (gSdMmcPciHcTemplate.Queue),\r
53 { // Slot\r
54 {0, UnknownSlot, 0, 0}, {0, UnknownSlot, 0, 0}, {0, UnknownSlot, 0, 0},\r
55 {0, UnknownSlot, 0, 0}, {0, UnknownSlot, 0, 0}, {0, UnknownSlot, 0, 0}\r
56 },\r
57 { // Capability\r
58 {0},\r
59 },\r
60 { // MaxCurrent\r
61 0,\r
62 },\r
63 0 // ControllerVersion\r
64};\r
65\r
66SD_DEVICE_PATH mSdDpTemplate = {\r
67 {\r
68 MESSAGING_DEVICE_PATH,\r
69 MSG_SD_DP,\r
70 {\r
71 (UINT8) (sizeof (SD_DEVICE_PATH)),\r
72 (UINT8) ((sizeof (SD_DEVICE_PATH)) >> 8)\r
73 }\r
74 },\r
75 0\r
76};\r
77\r
78EMMC_DEVICE_PATH mEmmcDpTemplate = {\r
79 {\r
80 MESSAGING_DEVICE_PATH,\r
81 MSG_EMMC_DP,\r
82 {\r
83 (UINT8) (sizeof (EMMC_DEVICE_PATH)),\r
84 (UINT8) ((sizeof (EMMC_DEVICE_PATH)) >> 8)\r
85 }\r
86 },\r
87 0\r
88};\r
89\r
90//\r
91// Prioritized function list to detect card type.\r
92// User could add other card detection logic here.\r
93//\r
94CARD_TYPE_DETECT_ROUTINE mCardTypeDetectRoutineTable[] = {\r
95 EmmcIdentification,\r
96 SdCardIdentification,\r
97 NULL\r
98};\r
99\r
100/**\r
101 The entry point for SD host controller driver, used to install this driver on the ImageHandle.\r
102\r
103 @param[in] ImageHandle The firmware allocated handle for this driver image.\r
104 @param[in] SystemTable Pointer to the EFI system table.\r
105\r
106 @retval EFI_SUCCESS Driver loaded.\r
107 @retval other Driver not loaded.\r
108\r
109**/\r
110EFI_STATUS\r
111EFIAPI\r
112InitializeSdMmcPciHcDxe (\r
113 IN EFI_HANDLE ImageHandle,\r
114 IN EFI_SYSTEM_TABLE *SystemTable\r
115 )\r
116{\r
117 EFI_STATUS Status;\r
118\r
119 Status = EfiLibInstallDriverBindingComponentName2 (\r
120 ImageHandle,\r
121 SystemTable,\r
122 &gSdMmcPciHcDriverBinding,\r
123 ImageHandle,\r
124 &gSdMmcPciHcComponentName,\r
125 &gSdMmcPciHcComponentName2\r
126 );\r
127 ASSERT_EFI_ERROR (Status);\r
128\r
129 return Status;\r
130}\r
131\r
132/**\r
133 Call back function when the timer event is signaled.\r
134\r
135 @param[in] Event The Event this notify function registered to.\r
136 @param[in] Context Pointer to the context data registered to the\r
137 Event.\r
138\r
139**/\r
140VOID\r
141EFIAPI\r
142ProcessAsyncTaskList (\r
143 IN EFI_EVENT Event,\r
144 IN VOID* Context\r
145 )\r
146{\r
147 SD_MMC_HC_PRIVATE_DATA *Private;\r
148 LIST_ENTRY *Link;\r
149 SD_MMC_HC_TRB *Trb;\r
150 EFI_STATUS Status;\r
151 EFI_SD_MMC_PASS_THRU_COMMAND_PACKET *Packet;\r
152 BOOLEAN InfiniteWait;\r
153 EFI_EVENT TrbEvent;\r
154\r
155 Private = (SD_MMC_HC_PRIVATE_DATA*)Context;\r
156\r
157 //\r
158 // Check if the first entry in the async I/O queue is done or not.\r
159 //\r
160 Status = EFI_SUCCESS;\r
161 Trb = NULL;\r
162 Link = GetFirstNode (&Private->Queue);\r
163 if (!IsNull (&Private->Queue, Link)) {\r
164 Trb = SD_MMC_HC_TRB_FROM_THIS (Link);\r
165 if (Private->Slot[Trb->Slot].MediaPresent == FALSE) {\r
166 Status = EFI_NO_MEDIA;\r
167 goto Done;\r
168 }\r
169 if (!Trb->Started) {\r
170 //\r
171 // Check whether the cmd/data line is ready for transfer.\r
172 //\r
173 Status = SdMmcCheckTrbEnv (Private, Trb);\r
174 if (!EFI_ERROR (Status)) {\r
175 Trb->Started = TRUE;\r
176 Status = SdMmcExecTrb (Private, Trb);\r
177 if (EFI_ERROR (Status)) {\r
178 goto Done;\r
179 }\r
180 } else {\r
181 goto Done;\r
182 }\r
183 }\r
184 Status = SdMmcCheckTrbResult (Private, Trb);\r
185 }\r
186\r
187Done:\r
188 if ((Trb != NULL) && (Status == EFI_NOT_READY)) {\r
189 Packet = Trb->Packet;\r
190 if (Packet->Timeout == 0) {\r
191 InfiniteWait = TRUE;\r
192 } else {\r
193 InfiniteWait = FALSE;\r
194 }\r
195 if ((!InfiniteWait) && (Trb->Timeout-- == 0)) {\r
196 RemoveEntryList (Link);\r
197 Trb->Packet->TransactionStatus = EFI_TIMEOUT;\r
198 TrbEvent = Trb->Event;\r
199 SdMmcFreeTrb (Trb);\r
200 DEBUG ((EFI_D_VERBOSE, "ProcessAsyncTaskList(): Signal Event %p EFI_TIMEOUT\n", TrbEvent));\r
201 gBS->SignalEvent (TrbEvent);\r
202 return;\r
203 }\r
204 }\r
205 if ((Trb != NULL) && (Status != EFI_NOT_READY)) {\r
206 RemoveEntryList (Link);\r
207 Trb->Packet->TransactionStatus = Status;\r
208 TrbEvent = Trb->Event;\r
209 SdMmcFreeTrb (Trb);\r
210 DEBUG ((EFI_D_VERBOSE, "ProcessAsyncTaskList(): Signal Event %p with %r\n", TrbEvent, Status));\r
211 gBS->SignalEvent (TrbEvent);\r
212 }\r
213 return;\r
214}\r
215\r
216/**\r
217 Sd removable device enumeration callback function when the timer event is signaled.\r
218\r
219 @param[in] Event The Event this notify function registered to.\r
220 @param[in] Context Pointer to the context data registered to the\r
221 Event.\r
222\r
223**/\r
224VOID\r
225EFIAPI\r
226SdMmcPciHcEnumerateDevice (\r
227 IN EFI_EVENT Event,\r
228 IN VOID* Context\r
229 )\r
230{\r
231 SD_MMC_HC_PRIVATE_DATA *Private;\r
232 EFI_STATUS Status;\r
233 UINT8 Slot;\r
234 BOOLEAN MediaPresent;\r
235 UINT32 RoutineNum;\r
236 CARD_TYPE_DETECT_ROUTINE *Routine;\r
237 UINTN Index;\r
238 LIST_ENTRY *Link;\r
239 LIST_ENTRY *NextLink;\r
240 SD_MMC_HC_TRB *Trb;\r
241\r
242 Private = (SD_MMC_HC_PRIVATE_DATA*)Context;\r
243\r
244 for (Slot = 0; Slot < SD_MMC_HC_MAX_SLOT; Slot++) {\r
245 if ((Private->Slot[Slot].Enable) && (Private->Slot[Slot].SlotType == RemovableSlot)) {\r
246 Status = SdMmcHcCardDetect (Private->PciIo, Slot, &MediaPresent);\r
247 if ((Status == EFI_MEDIA_CHANGED) && (MediaPresent == FALSE)) {\r
248 DEBUG ((EFI_D_INFO, "SdMmcPciHcEnumerateDevice: device disconnected at slot %d of pci %p\n", Slot, Private->PciIo));\r
249 Private->Slot[Slot].MediaPresent = FALSE;\r
250 //\r
251 // Signal all async task events at the slot with EFI_NO_MEDIA status.\r
252 //\r
253 for (Link = GetFirstNode (&Private->Queue);\r
254 !IsNull (&Private->Queue, Link);\r
255 Link = NextLink) {\r
256 NextLink = GetNextNode (&Private->Queue, Link);\r
257 Trb = SD_MMC_HC_TRB_FROM_THIS (Link);\r
258 if (Trb->Slot == Slot) {\r
259 RemoveEntryList (Link);\r
260 Trb->Packet->TransactionStatus = EFI_NO_MEDIA;\r
261 gBS->SignalEvent (Trb->Event);\r
262 SdMmcFreeTrb (Trb);\r
263 }\r
264 }\r
265 //\r
266 // Notify the upper layer the connect state change through ReinstallProtocolInterface.\r
267 //\r
268 gBS->ReinstallProtocolInterface (\r
269 Private->ControllerHandle,\r
270 &gEfiSdMmcPassThruProtocolGuid,\r
271 &Private->PassThru,\r
272 &Private->PassThru\r
273 );\r
274 }\r
275 if ((Status == EFI_MEDIA_CHANGED) && (MediaPresent == TRUE)) {\r
276 DEBUG ((EFI_D_INFO, "SdMmcPciHcEnumerateDevice: device connected at slot %d of pci %p\n", Slot, Private->PciIo));\r
277 //\r
278 // Reinitialize slot and restart identification process for the new attached device\r
279 //\r
280 Status = SdMmcHcInitHost (Private->PciIo, Slot, Private->Capability[Slot]);\r
281 if (EFI_ERROR (Status)) {\r
282 continue;\r
283 }\r
284\r
285 Private->Slot[Slot].MediaPresent = TRUE;\r
286 RoutineNum = sizeof (mCardTypeDetectRoutineTable) / sizeof (CARD_TYPE_DETECT_ROUTINE);\r
287 for (Index = 0; Index < RoutineNum; Index++) {\r
288 Routine = &mCardTypeDetectRoutineTable[Index];\r
289 if (*Routine != NULL) {\r
290 Status = (*Routine) (Private, Slot);\r
291 if (!EFI_ERROR (Status)) {\r
292 break;\r
293 }\r
294 }\r
295 }\r
296\r
297 //\r
298 // Notify the upper layer the connect state change through ReinstallProtocolInterface.\r
299 //\r
300 gBS->ReinstallProtocolInterface (\r
301 Private->ControllerHandle,\r
302 &gEfiSdMmcPassThruProtocolGuid,\r
303 &Private->PassThru,\r
304 &Private->PassThru\r
305 );\r
306 }\r
307 }\r
308 }\r
309\r
310 return;\r
311}\r
312/**\r
313 Tests to see if this driver supports a given controller. If a child device is provided,\r
314 it further tests to see if this driver supports creating a handle for the specified child device.\r
315\r
316 This function checks to see if the driver specified by This supports the device specified by\r
317 ControllerHandle. Drivers will typically use the device path attached to\r
318 ControllerHandle and/or the services from the bus I/O abstraction attached to\r
319 ControllerHandle to determine if the driver supports ControllerHandle. This function\r
320 may be called many times during platform initialization. In order to reduce boot times, the tests\r
321 performed by this function must be very small, and take as little time as possible to execute. This\r
322 function must not change the state of any hardware devices, and this function must be aware that the\r
323 device specified by ControllerHandle may already be managed by the same driver or a\r
324 different driver. This function must match its calls to AllocatePages() with FreePages(),\r
325 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().\r
326 Since ControllerHandle may have been previously started by the same driver, if a protocol is\r
327 already in the opened state, then it must not be closed with CloseProtocol(). This is required\r
328 to guarantee the state of ControllerHandle is not modified by this function.\r
329\r
330 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
331 @param[in] ControllerHandle The handle of the controller to test. This handle\r
332 must support a protocol interface that supplies\r
333 an I/O abstraction to the driver.\r
334 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
335 parameter is ignored by device drivers, and is optional for bus\r
336 drivers. For bus drivers, if this parameter is not NULL, then\r
337 the bus driver must determine if the bus controller specified\r
338 by ControllerHandle and the child controller specified\r
339 by RemainingDevicePath are both supported by this\r
340 bus driver.\r
341\r
342 @retval EFI_SUCCESS The device specified by ControllerHandle and\r
343 RemainingDevicePath is supported by the driver specified by This.\r
344 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and\r
345 RemainingDevicePath is already being managed by the driver\r
346 specified by This.\r
347 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and\r
348 RemainingDevicePath is already being managed by a different\r
349 driver or an application that requires exclusive access.\r
350 Currently not implemented.\r
351 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and\r
352 RemainingDevicePath is not supported by the driver specified by This.\r
353**/\r
354EFI_STATUS\r
355EFIAPI\r
356SdMmcPciHcDriverBindingSupported (\r
357 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
358 IN EFI_HANDLE Controller,\r
359 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
360 )\r
361{\r
362 EFI_STATUS Status;\r
363 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;\r
364 EFI_PCI_IO_PROTOCOL *PciIo;\r
365 PCI_TYPE00 PciData;\r
366\r
367 PciIo = NULL;\r
368 ParentDevicePath = NULL;\r
369\r
370 //\r
371 // SdPciHcDxe is a device driver, and should ingore the\r
372 // "RemainingDevicePath" according to EFI spec.\r
373 //\r
374 Status = gBS->OpenProtocol (\r
375 Controller,\r
376 &gEfiDevicePathProtocolGuid,\r
377 (VOID *) &ParentDevicePath,\r
378 This->DriverBindingHandle,\r
379 Controller,\r
380 EFI_OPEN_PROTOCOL_BY_DRIVER\r
381 );\r
382 if (EFI_ERROR (Status)) {\r
383 //\r
384 // EFI_ALREADY_STARTED is also an error.\r
385 //\r
386 return Status;\r
387 }\r
388 //\r
389 // Close the protocol because we don't use it here.\r
390 //\r
391 gBS->CloseProtocol (\r
392 Controller,\r
393 &gEfiDevicePathProtocolGuid,\r
394 This->DriverBindingHandle,\r
395 Controller\r
396 );\r
397\r
398 //\r
399 // Now test the EfiPciIoProtocol.\r
400 //\r
401 Status = gBS->OpenProtocol (\r
402 Controller,\r
403 &gEfiPciIoProtocolGuid,\r
404 (VOID **) &PciIo,\r
405 This->DriverBindingHandle,\r
406 Controller,\r
407 EFI_OPEN_PROTOCOL_BY_DRIVER\r
408 );\r
409 if (EFI_ERROR (Status)) {\r
410 return Status;\r
411 }\r
412\r
413 //\r
414 // Now further check the PCI header: Base class (offset 0x08) and\r
415 // Sub Class (offset 0x05). This controller should be an SD/MMC PCI\r
416 // Host Controller.\r
417 //\r
418 Status = PciIo->Pci.Read (\r
419 PciIo,\r
420 EfiPciIoWidthUint8,\r
421 0,\r
422 sizeof (PciData),\r
423 &PciData\r
424 );\r
425 if (EFI_ERROR (Status)) {\r
426 gBS->CloseProtocol (\r
427 Controller,\r
428 &gEfiPciIoProtocolGuid,\r
429 This->DriverBindingHandle,\r
430 Controller\r
431 );\r
432 return EFI_UNSUPPORTED;\r
433 }\r
434 //\r
435 // Since we already got the PciData, we can close protocol to avoid to carry it\r
436 // on for multiple exit points.\r
437 //\r
438 gBS->CloseProtocol (\r
439 Controller,\r
440 &gEfiPciIoProtocolGuid,\r
441 This->DriverBindingHandle,\r
442 Controller\r
443 );\r
444\r
445 //\r
446 // Examine SD PCI Host Controller PCI Configuration table fields.\r
447 //\r
448 if ((PciData.Hdr.ClassCode[2] == PCI_CLASS_SYSTEM_PERIPHERAL) &&\r
449 (PciData.Hdr.ClassCode[1] == PCI_SUBCLASS_SD_HOST_CONTROLLER) &&\r
450 ((PciData.Hdr.ClassCode[0] == 0x00) || (PciData.Hdr.ClassCode[0] == 0x01))) {\r
451 return EFI_SUCCESS;\r
452 }\r
453\r
454 return EFI_UNSUPPORTED;\r
455}\r
456\r
457/**\r
458 Starts a device controller or a bus controller.\r
459\r
460 The Start() function is designed to be invoked from the EFI boot service ConnectController().\r
461 As a result, much of the error checking on the parameters to Start() has been moved into this\r
462 common boot service. It is legal to call Start() from other locations,\r
463 but the following calling restrictions must be followed or the system behavior will not be deterministic.\r
464 1. ControllerHandle must be a valid EFI_HANDLE.\r
465 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned\r
466 EFI_DEVICE_PATH_PROTOCOL.\r
467 3. Prior to calling Start(), the Supported() function for the driver specified by This must\r
468 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.\r
469\r
470 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
471 @param[in] ControllerHandle The handle of the controller to start. This handle\r
472 must support a protocol interface that supplies\r
473 an I/O abstraction to the driver.\r
474 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
475 parameter is ignored by device drivers, and is optional for bus\r
476 drivers. For a bus driver, if this parameter is NULL, then handles\r
477 for all the children of Controller are created by this driver.\r
478 If this parameter is not NULL and the first Device Path Node is\r
479 not the End of Device Path Node, then only the handle for the\r
480 child device specified by the first Device Path Node of\r
481 RemainingDevicePath is created by this driver.\r
482 If the first Device Path Node of RemainingDevicePath is\r
483 the End of Device Path Node, no child handle is created by this\r
484 driver.\r
485\r
486 @retval EFI_SUCCESS The device was started.\r
487 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.\r
488 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
489 @retval Others The driver failded to start the device.\r
490\r
491**/\r
492EFI_STATUS\r
493EFIAPI\r
494SdMmcPciHcDriverBindingStart (\r
495 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
496 IN EFI_HANDLE Controller,\r
497 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
498 )\r
499{\r
500 EFI_STATUS Status;\r
501 SD_MMC_HC_PRIVATE_DATA *Private;\r
502 EFI_PCI_IO_PROTOCOL *PciIo;\r
503 UINT64 Supports;\r
504 UINT64 PciAttributes;\r
505 UINT8 SlotNum;\r
506 UINT8 FirstBar;\r
507 UINT8 Slot;\r
508 UINT8 Index;\r
509 CARD_TYPE_DETECT_ROUTINE *Routine;\r
510 UINT32 RoutineNum;\r
511 BOOLEAN MediaPresent;\r
512\r
513 DEBUG ((EFI_D_INFO, "SdMmcPciHcDriverBindingStart: Start\n"));\r
514\r
515 //\r
516 // Open PCI I/O Protocol and save pointer to open protocol\r
517 // in private data area.\r
518 //\r
519 PciIo = NULL;\r
520 Status = gBS->OpenProtocol (\r
521 Controller,\r
522 &gEfiPciIoProtocolGuid,\r
523 (VOID **) &PciIo,\r
524 This->DriverBindingHandle,\r
525 Controller,\r
526 EFI_OPEN_PROTOCOL_BY_DRIVER\r
527 );\r
528 if (EFI_ERROR (Status)) {\r
529 return Status;\r
530 }\r
531\r
532 //\r
533 // Enable the SD Host Controller MMIO space\r
534 //\r
535 Private = NULL;\r
536 Status = PciIo->Attributes (\r
537 PciIo,\r
538 EfiPciIoAttributeOperationGet,\r
539 0,\r
540 &PciAttributes\r
541 );\r
542\r
543 if (EFI_ERROR (Status)) {\r
544 goto Done;\r
545 }\r
546\r
547 Status = PciIo->Attributes (\r
548 PciIo,\r
549 EfiPciIoAttributeOperationSupported,\r
550 0,\r
551 &Supports\r
552 );\r
553\r
554 if (!EFI_ERROR (Status)) {\r
555 Supports &= (UINT64)EFI_PCI_DEVICE_ENABLE;\r
556 Status = PciIo->Attributes (\r
557 PciIo,\r
558 EfiPciIoAttributeOperationEnable,\r
559 Supports,\r
560 NULL\r
561 );\r
562 } else {\r
563 goto Done;\r
564 }\r
565\r
566 Private = AllocateCopyPool (sizeof (SD_MMC_HC_PRIVATE_DATA), &gSdMmcPciHcTemplate);\r
567 if (Private == NULL) {\r
568 Status = EFI_OUT_OF_RESOURCES;\r
569 goto Done;\r
570 }\r
571\r
572 Private->ControllerHandle = Controller;\r
573 Private->PciIo = PciIo;\r
574 Private->PciAttributes = PciAttributes;\r
575 InitializeListHead (&Private->Queue);\r
576\r
577 //\r
578 // Get SD/MMC Pci Host Controller Slot info\r
579 //\r
580 Status = SdMmcHcGetSlotInfo (PciIo, &FirstBar, &SlotNum);\r
581 if (EFI_ERROR (Status)) {\r
582 goto Done;\r
583 }\r
584\r
585 for (Slot = FirstBar; Slot < (FirstBar + SlotNum); Slot++) {\r
586 Private->Slot[Slot].Enable = TRUE;\r
587\r
588 Status = SdMmcHcGetCapability (PciIo, Slot, &Private->Capability[Slot]);\r
589 if (EFI_ERROR (Status)) {\r
590 continue;\r
591 }\r
592 DumpCapabilityReg (Slot, &Private->Capability[Slot]);\r
593\r
594 Status = SdMmcHcGetMaxCurrent (PciIo, Slot, &Private->MaxCurrent[Slot]);\r
595 if (EFI_ERROR (Status)) {\r
596 continue;\r
597 }\r
598\r
599 Private->Slot[Slot].SlotType = Private->Capability[Slot].SlotType;\r
600 if ((Private->Slot[Slot].SlotType != RemovableSlot) && (Private->Slot[Slot].SlotType != EmbeddedSlot)) {\r
601 DEBUG ((EFI_D_INFO, "SdMmcPciHcDxe doesn't support the slot type [%d]!!!\n", Private->Slot[Slot].SlotType));\r
602 continue;\r
603 }\r
604\r
605 //\r
606 // Reset the specified slot of the SD/MMC Pci Host Controller\r
607 //\r
608 Status = SdMmcHcReset (PciIo, Slot);\r
609 if (EFI_ERROR (Status)) {\r
610 continue;\r
611 }\r
612 //\r
613 // Check whether there is a SD/MMC card attached\r
614 //\r
615 Status = SdMmcHcCardDetect (PciIo, Slot, &MediaPresent);\r
616 if ((Status == EFI_MEDIA_CHANGED) && (MediaPresent == FALSE)) {\r
617 DEBUG ((EFI_D_ERROR, "SdMmcHcCardDetect: No device attached in Slot[%d]!!!\n", Slot));\r
618 continue;\r
619 }\r
620\r
621 Status = SdMmcHcInitHost (PciIo, Slot, Private->Capability[Slot]);\r
622 if (EFI_ERROR (Status)) {\r
623 continue;\r
624 }\r
625\r
626 Private->Slot[Slot].MediaPresent = TRUE;\r
627 RoutineNum = sizeof (mCardTypeDetectRoutineTable) / sizeof (CARD_TYPE_DETECT_ROUTINE);\r
628 for (Index = 0; Index < RoutineNum; Index++) {\r
629 Routine = &mCardTypeDetectRoutineTable[Index];\r
630 if (*Routine != NULL) {\r
631 Status = (*Routine) (Private, Slot);\r
632 if (!EFI_ERROR (Status)) {\r
633 break;\r
634 }\r
635 }\r
636 }\r
637 }\r
638\r
639 //\r
640 // Start the asynchronous I/O monitor\r
641 //\r
642 Status = gBS->CreateEvent (\r
643 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
644 TPL_CALLBACK,\r
645 ProcessAsyncTaskList,\r
646 Private,\r
647 &Private->TimerEvent\r
648 );\r
649 if (EFI_ERROR (Status)) {\r
650 goto Done;\r
651 }\r
652\r
653 Status = gBS->SetTimer (Private->TimerEvent, TimerPeriodic, SD_MMC_HC_ASYNC_TIMER);\r
654 if (EFI_ERROR (Status)) {\r
655 goto Done;\r
656 }\r
657\r
658 //\r
659 // Start the Sd removable device connection enumeration\r
660 //\r
661 Status = gBS->CreateEvent (\r
662 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
663 TPL_CALLBACK,\r
664 SdMmcPciHcEnumerateDevice,\r
665 Private,\r
666 &Private->ConnectEvent\r
667 );\r
668 if (EFI_ERROR (Status)) {\r
669 goto Done;\r
670 }\r
671\r
672 Status = gBS->SetTimer (Private->ConnectEvent, TimerPeriodic, SD_MMC_HC_ENUM_TIMER);\r
673 if (EFI_ERROR (Status)) {\r
674 goto Done;\r
675 }\r
676\r
677 Status = gBS->InstallMultipleProtocolInterfaces (\r
678 &Controller,\r
679 &gEfiSdMmcPassThruProtocolGuid,\r
680 &(Private->PassThru),\r
681 NULL\r
682 );\r
683\r
684 DEBUG ((EFI_D_INFO, "SdMmcPciHcDriverBindingStart: %r End on %x\n", Status, Controller));\r
685\r
686Done:\r
687 if (EFI_ERROR (Status)) {\r
688 if ((Private != NULL) && (Private->PciAttributes != 0)) {\r
689 //\r
690 // Restore original PCI attributes\r
691 //\r
692 PciIo->Attributes (\r
693 PciIo,\r
694 EfiPciIoAttributeOperationSet,\r
695 Private->PciAttributes,\r
696 NULL\r
697 );\r
698 }\r
699 gBS->CloseProtocol (\r
700 Controller,\r
701 &gEfiPciIoProtocolGuid,\r
702 This->DriverBindingHandle,\r
703 Controller\r
704 );\r
705\r
706 if ((Private != NULL) && (Private->TimerEvent != NULL)) {\r
707 gBS->CloseEvent (Private->TimerEvent);\r
708 }\r
709\r
710 if ((Private != NULL) && (Private->ConnectEvent != NULL)) {\r
711 gBS->CloseEvent (Private->ConnectEvent);\r
712 }\r
713\r
714 if (Private != NULL) {\r
715 FreePool (Private);\r
716 }\r
717 }\r
718\r
719 return Status;\r
720}\r
721\r
722/**\r
723 Stops a device controller or a bus controller.\r
724\r
725 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().\r
726 As a result, much of the error checking on the parameters to Stop() has been moved\r
727 into this common boot service. It is legal to call Stop() from other locations,\r
728 but the following calling restrictions must be followed or the system behavior will not be deterministic.\r
729 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this\r
730 same driver's Start() function.\r
731 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid\r
732 EFI_HANDLE. In addition, all of these handles must have been created in this driver's\r
733 Start() function, and the Start() function must have called OpenProtocol() on\r
734 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r
735\r
736 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
737 @param[in] ControllerHandle A handle to the device being stopped. The handle must\r
738 support a bus specific I/O protocol for the driver\r
739 to use to stop the device.\r
740 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.\r
741 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL\r
742 if NumberOfChildren is 0.\r
743\r
744 @retval EFI_SUCCESS The device was stopped.\r
745 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.\r
746\r
747**/\r
748EFI_STATUS\r
749EFIAPI\r
750SdMmcPciHcDriverBindingStop (\r
751 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
752 IN EFI_HANDLE Controller,\r
753 IN UINTN NumberOfChildren,\r
754 IN EFI_HANDLE *ChildHandleBuffer\r
755 )\r
756{\r
757 EFI_STATUS Status;\r
758 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;\r
759 SD_MMC_HC_PRIVATE_DATA *Private;\r
760 EFI_PCI_IO_PROTOCOL *PciIo;\r
761 LIST_ENTRY *Link;\r
762 LIST_ENTRY *NextLink;\r
763 SD_MMC_HC_TRB *Trb;\r
764\r
765 DEBUG ((EFI_D_INFO, "SdMmcPciHcDriverBindingStop: Start\n"));\r
766\r
767 Status = gBS->OpenProtocol (\r
768 Controller,\r
769 &gEfiSdMmcPassThruProtocolGuid,\r
770 (VOID**) &PassThru,\r
771 This->DriverBindingHandle,\r
772 Controller,\r
773 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
774 );\r
775 if (EFI_ERROR (Status)) {\r
776 return Status;\r
777 }\r
778\r
779 Private = SD_MMC_HC_PRIVATE_FROM_THIS (PassThru);\r
780 //\r
781 // Close Non-Blocking timer and free Task list.\r
782 //\r
783 if (Private->TimerEvent != NULL) {\r
784 gBS->CloseEvent (Private->TimerEvent);\r
785 Private->TimerEvent = NULL;\r
786 }\r
787 if (Private->ConnectEvent != NULL) {\r
788 gBS->CloseEvent (Private->ConnectEvent);\r
789 Private->ConnectEvent = NULL;\r
790 }\r
791 //\r
792 // As the timer is closed, there is no needs to use TPL lock to\r
793 // protect the critical region "queue".\r
794 //\r
795 for (Link = GetFirstNode (&Private->Queue);\r
796 !IsNull (&Private->Queue, Link);\r
797 Link = NextLink) {\r
798 NextLink = GetNextNode (&Private->Queue, Link);\r
799 RemoveEntryList (Link);\r
800 Trb = SD_MMC_HC_TRB_FROM_THIS (Link);\r
801 Trb->Packet->TransactionStatus = EFI_ABORTED;\r
802 gBS->SignalEvent (Trb->Event);\r
803 SdMmcFreeTrb (Trb);\r
804 }\r
805\r
806 //\r
807 // Uninstall Block I/O protocol from the device handle\r
808 //\r
809 Status = gBS->UninstallProtocolInterface (\r
810 Controller,\r
811 &gEfiSdMmcPassThruProtocolGuid,\r
812 &(Private->PassThru)\r
813 );\r
814\r
815 if (EFI_ERROR (Status)) {\r
816 return Status;\r
817 }\r
818\r
819 gBS->CloseProtocol (\r
820 Controller,\r
821 &gEfiPciIoProtocolGuid,\r
822 This->DriverBindingHandle,\r
823 Controller\r
824 );\r
825 //\r
826 // Restore original PCI attributes\r
827 //\r
828 PciIo = Private->PciIo;\r
829 Status = PciIo->Attributes (\r
830 PciIo,\r
831 EfiPciIoAttributeOperationSet,\r
832 Private->PciAttributes,\r
833 NULL\r
834 );\r
835 ASSERT_EFI_ERROR (Status);\r
836\r
837 FreePool (Private);\r
838\r
839 DEBUG ((EFI_D_INFO, "SdMmcPciHcDriverBindingStop: End with %r\n", Status));\r
840\r
841 return Status;\r
842}\r
843\r
844/**\r
845 Sends SD command to an SD card that is attached to the SD controller.\r
846\r
847 The PassThru() function sends the SD command specified by Packet to the SD card\r
848 specified by Slot.\r
849\r
850 If Packet is successfully sent to the SD card, then EFI_SUCCESS is returned.\r
851\r
852 If a device error occurs while sending the Packet, then EFI_DEVICE_ERROR is returned.\r
853\r
854 If Slot is not in a valid range for the SD controller, then EFI_INVALID_PARAMETER\r
855 is returned.\r
856\r
857 If Packet defines a data command but both InDataBuffer and OutDataBuffer are NULL,\r
858 EFI_INVALID_PARAMETER is returned.\r
859\r
860 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.\r
861 @param[in] Slot The slot number of the SD card to send the command to.\r
862 @param[in,out] Packet A pointer to the SD command data structure.\r
863 @param[in] Event If Event is NULL, blocking I/O is performed. If Event is\r
864 not NULL, then nonblocking I/O is performed, and Event\r
865 will be signaled when the Packet completes.\r
866\r
867 @retval EFI_SUCCESS The SD Command Packet was sent by the host.\r
868 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SD\r
869 command Packet.\r
870 @retval EFI_INVALID_PARAMETER Packet, Slot, or the contents of the Packet is invalid.\r
871 @retval EFI_INVALID_PARAMETER Packet defines a data command but both InDataBuffer and\r
872 OutDataBuffer are NULL.\r
873 @retval EFI_NO_MEDIA SD Device not present in the Slot.\r
874 @retval EFI_UNSUPPORTED The command described by the SD Command Packet is not\r
875 supported by the host controller.\r
876 @retval EFI_BAD_BUFFER_SIZE The InTransferLength or OutTransferLength exceeds the\r
877 limit supported by SD card ( i.e. if the number of bytes\r
878 exceed the Last LBA).\r
879\r
880**/\r
881EFI_STATUS\r
882EFIAPI\r
883SdMmcPassThruPassThru (\r
884 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *This,\r
885 IN UINT8 Slot,\r
886 IN OUT EFI_SD_MMC_PASS_THRU_COMMAND_PACKET *Packet,\r
887 IN EFI_EVENT Event OPTIONAL\r
888 )\r
889{\r
890 EFI_STATUS Status;\r
891 SD_MMC_HC_PRIVATE_DATA *Private;\r
892 SD_MMC_HC_TRB *Trb;\r
893 EFI_TPL OldTpl;\r
894\r
895 if ((This == NULL) || (Packet == NULL)) {\r
896 return EFI_INVALID_PARAMETER;\r
897 }\r
898\r
899 if ((Packet->SdMmcCmdBlk == NULL) || (Packet->SdMmcStatusBlk == NULL)) {\r
900 return EFI_INVALID_PARAMETER;\r
901 }\r
902\r
903 if ((Packet->OutDataBuffer == NULL) && (Packet->OutTransferLength != 0)) {\r
904 return EFI_INVALID_PARAMETER;\r
905 }\r
906\r
907 if ((Packet->InDataBuffer == NULL) && (Packet->InTransferLength != 0)) {\r
908 return EFI_INVALID_PARAMETER;\r
909 }\r
910\r
911 Private = SD_MMC_HC_PRIVATE_FROM_THIS (This);\r
912\r
913 if (!Private->Slot[Slot].Enable) {\r
914 return EFI_INVALID_PARAMETER;\r
915 }\r
916\r
917 if (!Private->Slot[Slot].MediaPresent) {\r
918 return EFI_NO_MEDIA;\r
919 }\r
920\r
921 Trb = SdMmcCreateTrb (Private, Slot, Packet, Event);\r
922 if (Trb == NULL) {\r
923 return EFI_OUT_OF_RESOURCES;\r
924 }\r
925 //\r
926 // Immediately return for async I/O.\r
927 //\r
928 if (Event != NULL) {\r
929 return EFI_SUCCESS;\r
930 }\r
931\r
932 //\r
933 // Wait async I/O list is empty before execute sync I/O operation.\r
934 //\r
935 while (TRUE) {\r
936 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
937 if (IsListEmpty (&Private->Queue)) {\r
938 gBS->RestoreTPL (OldTpl);\r
939 break;\r
940 }\r
941 gBS->RestoreTPL (OldTpl);\r
942 }\r
943\r
944 Status = SdMmcWaitTrbEnv (Private, Trb);\r
945 if (EFI_ERROR (Status)) {\r
946 goto Done;\r
947 }\r
948\r
949 Status = SdMmcExecTrb (Private, Trb);\r
950 if (EFI_ERROR (Status)) {\r
951 goto Done;\r
952 }\r
953\r
954 Status = SdMmcWaitTrbResult (Private, Trb);\r
955 if (EFI_ERROR (Status)) {\r
956 goto Done;\r
957 }\r
958\r
959Done:\r
960 if ((Trb != NULL) && (Trb->AdmaDesc != NULL)) {\r
961 FreePages (Trb->AdmaDesc, Trb->AdmaPages);\r
962 }\r
963\r
964 if (Trb != NULL) {\r
965 FreePool (Trb);\r
966 }\r
967\r
968 return Status;\r
969}\r
970\r
971/**\r
972 Used to retrieve next slot numbers supported by the SD controller. The function\r
973 returns information about all available slots (populated or not-populated).\r
974\r
975 The GetNextSlot() function retrieves the next slot number on an SD controller.\r
976 If on input Slot is 0xFF, then the slot number of the first slot on the SD controller\r
977 is returned.\r
978\r
979 If Slot is a slot number that was returned on a previous call to GetNextSlot(), then\r
980 the slot number of the next slot on the SD controller is returned.\r
981\r
982 If Slot is not 0xFF and Slot was not returned on a previous call to GetNextSlot(),\r
983 EFI_INVALID_PARAMETER is returned.\r
984\r
985 If Slot is the slot number of the last slot on the SD controller, then EFI_NOT_FOUND\r
986 is returned.\r
987\r
988 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.\r
989 @param[in,out] Slot On input, a pointer to a slot number on the SD controller.\r
990 On output, a pointer to the next slot number on the SD controller.\r
991 An input value of 0xFF retrieves the first slot number on the SD\r
992 controller.\r
993\r
994 @retval EFI_SUCCESS The next slot number on the SD controller was returned in Slot.\r
995 @retval EFI_NOT_FOUND There are no more slots on this SD controller.\r
996 @retval EFI_INVALID_PARAMETER Slot is not 0xFF and Slot was not returned on a previous call\r
997 to GetNextSlot().\r
998\r
999**/\r
1000EFI_STATUS\r
1001EFIAPI\r
1002SdMmcPassThruGetNextSlot (\r
1003 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *This,\r
1004 IN OUT UINT8 *Slot\r
1005 )\r
1006{\r
1007 SD_MMC_HC_PRIVATE_DATA *Private;\r
1008 UINT8 Index;\r
1009\r
1010 if ((This == NULL) || (Slot == NULL)) {\r
1011 return EFI_INVALID_PARAMETER;\r
1012 }\r
1013\r
1014 Private = SD_MMC_HC_PRIVATE_FROM_THIS (This);\r
1015\r
1016 if (*Slot == 0xFF) {\r
1017 for (Index = 0; Index < SD_MMC_HC_MAX_SLOT; Index++) {\r
1018 if (Private->Slot[Index].Enable) {\r
1019 *Slot = Index;\r
1020 Private->PreviousSlot = Index;\r
1021 return EFI_SUCCESS;\r
1022 }\r
1023 }\r
1024 return EFI_NOT_FOUND;\r
1025 } else if (*Slot == Private->PreviousSlot) {\r
1026 for (Index = *Slot + 1; Index < SD_MMC_HC_MAX_SLOT; Index++) {\r
1027 if (Private->Slot[Index].Enable) {\r
1028 *Slot = Index;\r
1029 Private->PreviousSlot = Index;\r
1030 return EFI_SUCCESS;\r
1031 }\r
1032 }\r
1033 return EFI_NOT_FOUND;\r
1034 } else {\r
1035 return EFI_INVALID_PARAMETER;\r
1036 }\r
1037}\r
1038\r
1039/**\r
1040 Used to allocate and build a device path node for an SD card on the SD controller.\r
1041\r
1042 The BuildDevicePath() function allocates and builds a single device node for the SD\r
1043 card specified by Slot.\r
1044\r
1045 If the SD card specified by Slot is not present on the SD controller, then EFI_NOT_FOUND\r
1046 is returned.\r
1047\r
1048 If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned.\r
1049\r
1050 If there are not enough resources to allocate the device path node, then EFI_OUT_OF_RESOURCES\r
1051 is returned.\r
1052\r
1053 Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of\r
1054 DevicePath are initialized to describe the SD card specified by Slot, and EFI_SUCCESS is\r
1055 returned.\r
1056\r
1057 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.\r
1058 @param[in] Slot Specifies the slot number of the SD card for which a device\r
1059 path node is to be allocated and built.\r
1060 @param[in,out] DevicePath A pointer to a single device path node that describes the SD\r
1061 card specified by Slot. This function is responsible for\r
1062 allocating the buffer DevicePath with the boot service\r
1063 AllocatePool(). It is the caller's responsibility to free\r
1064 DevicePath when the caller is finished with DevicePath.\r
1065\r
1066 @retval EFI_SUCCESS The device path node that describes the SD card specified by\r
1067 Slot was allocated and returned in DevicePath.\r
1068 @retval EFI_NOT_FOUND The SD card specified by Slot does not exist on the SD controller.\r
1069 @retval EFI_INVALID_PARAMETER DevicePath is NULL.\r
1070 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.\r
1071\r
1072**/\r
1073EFI_STATUS\r
1074EFIAPI\r
1075SdMmcPassThruBuildDevicePath (\r
1076 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *This,\r
1077 IN UINT8 Slot,\r
1078 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath\r
1079 )\r
1080{\r
1081 SD_MMC_HC_PRIVATE_DATA *Private;\r
1082 SD_DEVICE_PATH *SdNode;\r
1083 EMMC_DEVICE_PATH *EmmcNode;\r
1084\r
1085 if ((This == NULL) || (DevicePath == NULL) || (Slot >= SD_MMC_HC_MAX_SLOT)) {\r
1086 return EFI_INVALID_PARAMETER;\r
1087 }\r
1088\r
1089 Private = SD_MMC_HC_PRIVATE_FROM_THIS (This);\r
1090\r
1091 if ((!Private->Slot[Slot].Enable) || (!Private->Slot[Slot].MediaPresent)) {\r
1092 return EFI_NOT_FOUND;\r
1093 }\r
1094\r
1095 if (Private->Slot[Slot].CardType == SdCardType) {\r
1096 SdNode = AllocateCopyPool (sizeof (SD_DEVICE_PATH), &mSdDpTemplate);\r
1097 if (SdNode == NULL) {\r
1098 return EFI_OUT_OF_RESOURCES;\r
1099 }\r
1100 SdNode->SlotNumber = Slot;\r
1101\r
1102 *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) SdNode;\r
1103 } else if (Private->Slot[Slot].CardType == EmmcCardType) {\r
1104 EmmcNode = AllocateCopyPool (sizeof (EMMC_DEVICE_PATH), &mEmmcDpTemplate);\r
1105 if (EmmcNode == NULL) {\r
1106 return EFI_OUT_OF_RESOURCES;\r
1107 }\r
1108 EmmcNode->SlotNumber = Slot;\r
1109\r
1110 *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) EmmcNode;\r
1111 } else {\r
1112 //\r
1113 // Currently we only support SD and EMMC two device nodes.\r
1114 //\r
1115 return EFI_NOT_FOUND;\r
1116 }\r
1117\r
1118 return EFI_SUCCESS;\r
1119}\r
1120\r
1121/**\r
1122 This function retrieves an SD card slot number based on the input device path.\r
1123\r
1124 The GetSlotNumber() function retrieves slot number for the SD card specified by\r
1125 the DevicePath node. If DevicePath is NULL, EFI_INVALID_PARAMETER is returned.\r
1126\r
1127 If DevicePath is not a device path node type that the SD Pass Thru driver supports,\r
1128 EFI_UNSUPPORTED is returned.\r
1129\r
1130 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.\r
1131 @param[in] DevicePath A pointer to the device path node that describes a SD\r
1132 card on the SD controller.\r
1133 @param[out] Slot On return, points to the slot number of an SD card on\r
1134 the SD controller.\r
1135\r
1136 @retval EFI_SUCCESS SD card slot number is returned in Slot.\r
1137 @retval EFI_INVALID_PARAMETER Slot or DevicePath is NULL.\r
1138 @retval EFI_UNSUPPORTED DevicePath is not a device path node type that the SD\r
1139 Pass Thru driver supports.\r
1140\r
1141**/\r
1142EFI_STATUS\r
1143EFIAPI\r
1144SdMmcPassThruGetSlotNumber (\r
1145 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *This,\r
1146 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
1147 OUT UINT8 *Slot\r
1148 )\r
1149{\r
1150 SD_MMC_HC_PRIVATE_DATA *Private;\r
1151 SD_DEVICE_PATH *SdNode;\r
1152 EMMC_DEVICE_PATH *EmmcNode;\r
1153 UINT8 SlotNumber;\r
1154\r
1155 if ((This == NULL) || (DevicePath == NULL) || (Slot == NULL)) {\r
1156 return EFI_INVALID_PARAMETER;\r
1157 }\r
1158\r
1159 Private = SD_MMC_HC_PRIVATE_FROM_THIS (This);\r
1160\r
1161 //\r
1162 // Check whether the DevicePath belongs to SD_DEVICE_PATH or EMMC_DEVICE_PATH\r
1163 //\r
1164 if ((DevicePath->Type != MESSAGING_DEVICE_PATH) ||\r
1165 ((DevicePath->SubType != MSG_SD_DP) &&\r
1166 (DevicePath->SubType != MSG_EMMC_DP)) ||\r
1167 (DevicePathNodeLength(DevicePath) != sizeof(SD_DEVICE_PATH)) ||\r
1168 (DevicePathNodeLength(DevicePath) != sizeof(EMMC_DEVICE_PATH))) {\r
1169 return EFI_UNSUPPORTED;\r
1170 }\r
1171\r
1172 if (DevicePath->SubType == MSG_SD_DP) {\r
1173 SdNode = (SD_DEVICE_PATH *) DevicePath;\r
1174 SlotNumber = SdNode->SlotNumber;\r
1175 } else {\r
1176 EmmcNode = (EMMC_DEVICE_PATH *) DevicePath;\r
1177 SlotNumber = EmmcNode->SlotNumber;\r
1178 }\r
1179\r
1180 if (SlotNumber >= SD_MMC_HC_MAX_SLOT) {\r
1181 return EFI_NOT_FOUND;\r
1182 }\r
1183\r
1184 if (Private->Slot[SlotNumber].Enable) {\r
1185 *Slot = SlotNumber;\r
1186 return EFI_SUCCESS;\r
1187 } else {\r
1188 return EFI_NOT_FOUND;\r
1189 }\r
1190}\r
1191\r
1192/**\r
1193 Resets an SD card that is connected to the SD controller.\r
1194\r
1195 The ResetDevice() function resets the SD card specified by Slot.\r
1196\r
1197 If this SD controller does not support a device reset operation, EFI_UNSUPPORTED is\r
1198 returned.\r
1199\r
1200 If Slot is not in a valid slot number for this SD controller, EFI_INVALID_PARAMETER\r
1201 is returned.\r
1202\r
1203 If the device reset operation is completed, EFI_SUCCESS is returned.\r
1204\r
1205 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.\r
1206 @param[in] Slot Specifies the slot number of the SD card to be reset.\r
1207\r
1208 @retval EFI_SUCCESS The SD card specified by Slot was reset.\r
1209 @retval EFI_UNSUPPORTED The SD controller does not support a device reset operation.\r
1210 @retval EFI_INVALID_PARAMETER Slot number is invalid.\r
1211 @retval EFI_NO_MEDIA SD Device not present in the Slot.\r
1212 @retval EFI_DEVICE_ERROR The reset command failed due to a device error\r
1213\r
1214**/\r
1215EFI_STATUS\r
1216EFIAPI\r
1217SdMmcPassThruResetDevice (\r
1218 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *This,\r
1219 IN UINT8 Slot\r
1220 )\r
1221{\r
1222 SD_MMC_HC_PRIVATE_DATA *Private;\r
1223 LIST_ENTRY *Link;\r
1224 LIST_ENTRY *NextLink;\r
1225 SD_MMC_HC_TRB *Trb;\r
1226 EFI_TPL OldTpl;\r
1227\r
1228 if (This == NULL) {\r
1229 return EFI_INVALID_PARAMETER;\r
1230 }\r
1231\r
1232 Private = SD_MMC_HC_PRIVATE_FROM_THIS (This);\r
1233\r
1234 if (!Private->Slot[Slot].Enable) {\r
1235 return EFI_INVALID_PARAMETER;\r
1236 }\r
1237\r
1238 if (!Private->Slot[Slot].MediaPresent) {\r
1239 return EFI_NO_MEDIA;\r
1240 }\r
1241 //\r
1242 // Free all async I/O requests in the queue\r
1243 //\r
1244 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
1245\r
1246 for (Link = GetFirstNode (&Private->Queue);\r
1247 !IsNull (&Private->Queue, Link);\r
1248 Link = NextLink) {\r
1249 NextLink = GetNextNode (&Private->Queue, Link);\r
1250 RemoveEntryList (Link);\r
1251 Trb = SD_MMC_HC_TRB_FROM_THIS (Link);\r
1252 Trb->Packet->TransactionStatus = EFI_ABORTED;\r
1253 gBS->SignalEvent (Trb->Event);\r
1254 SdMmcFreeTrb (Trb);\r
1255 }\r
1256\r
1257 gBS->RestoreTPL (OldTpl);\r
1258\r
1259 return EFI_SUCCESS;\r
1260}\r
1261\r