]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Pci/SdMmcPciHcDxe/SdMmcPciHcDxe.c
MdeModulePkg/SdMmcPciHc: Reset the slot when sd device is connected
[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
2e9107b8 7 Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>\r
48555339
FT
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
83ceccab
FT
278 // Reset the specified slot of the SD/MMC Pci Host Controller\r
279 //\r
280 Status = SdMmcHcReset (Private->PciIo, Slot);\r
281 if (EFI_ERROR (Status)) {\r
282 continue;\r
283 }\r
284 //\r
48555339
FT
285 // Reinitialize slot and restart identification process for the new attached device\r
286 //\r
287 Status = SdMmcHcInitHost (Private->PciIo, Slot, Private->Capability[Slot]);\r
288 if (EFI_ERROR (Status)) {\r
289 continue;\r
290 }\r
291\r
292 Private->Slot[Slot].MediaPresent = TRUE;\r
293 RoutineNum = sizeof (mCardTypeDetectRoutineTable) / sizeof (CARD_TYPE_DETECT_ROUTINE);\r
294 for (Index = 0; Index < RoutineNum; Index++) {\r
295 Routine = &mCardTypeDetectRoutineTable[Index];\r
296 if (*Routine != NULL) {\r
297 Status = (*Routine) (Private, Slot);\r
298 if (!EFI_ERROR (Status)) {\r
299 break;\r
300 }\r
301 }\r
302 }\r
303\r
304 //\r
305 // Notify the upper layer the connect state change through ReinstallProtocolInterface.\r
306 //\r
307 gBS->ReinstallProtocolInterface (\r
308 Private->ControllerHandle,\r
309 &gEfiSdMmcPassThruProtocolGuid,\r
310 &Private->PassThru,\r
311 &Private->PassThru\r
312 );\r
313 }\r
314 }\r
315 }\r
316\r
317 return;\r
318}\r
319/**\r
320 Tests to see if this driver supports a given controller. If a child device is provided,\r
321 it further tests to see if this driver supports creating a handle for the specified child device.\r
322\r
323 This function checks to see if the driver specified by This supports the device specified by\r
324 ControllerHandle. Drivers will typically use the device path attached to\r
325 ControllerHandle and/or the services from the bus I/O abstraction attached to\r
326 ControllerHandle to determine if the driver supports ControllerHandle. This function\r
327 may be called many times during platform initialization. In order to reduce boot times, the tests\r
328 performed by this function must be very small, and take as little time as possible to execute. This\r
329 function must not change the state of any hardware devices, and this function must be aware that the\r
330 device specified by ControllerHandle may already be managed by the same driver or a\r
331 different driver. This function must match its calls to AllocatePages() with FreePages(),\r
332 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().\r
333 Since ControllerHandle may have been previously started by the same driver, if a protocol is\r
334 already in the opened state, then it must not be closed with CloseProtocol(). This is required\r
335 to guarantee the state of ControllerHandle is not modified by this function.\r
336\r
337 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
338 @param[in] ControllerHandle The handle of the controller to test. This handle\r
339 must support a protocol interface that supplies\r
340 an I/O abstraction to the driver.\r
341 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
342 parameter is ignored by device drivers, and is optional for bus\r
343 drivers. For bus drivers, if this parameter is not NULL, then\r
344 the bus driver must determine if the bus controller specified\r
345 by ControllerHandle and the child controller specified\r
346 by RemainingDevicePath are both supported by this\r
347 bus driver.\r
348\r
349 @retval EFI_SUCCESS The device specified by ControllerHandle and\r
350 RemainingDevicePath is supported by the driver specified by This.\r
351 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and\r
352 RemainingDevicePath is already being managed by the driver\r
353 specified by This.\r
354 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and\r
355 RemainingDevicePath is already being managed by a different\r
356 driver or an application that requires exclusive access.\r
357 Currently not implemented.\r
358 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and\r
359 RemainingDevicePath is not supported by the driver specified by This.\r
360**/\r
361EFI_STATUS\r
362EFIAPI\r
363SdMmcPciHcDriverBindingSupported (\r
364 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
365 IN EFI_HANDLE Controller,\r
366 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
367 )\r
368{\r
369 EFI_STATUS Status;\r
370 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;\r
371 EFI_PCI_IO_PROTOCOL *PciIo;\r
372 PCI_TYPE00 PciData;\r
373\r
374 PciIo = NULL;\r
375 ParentDevicePath = NULL;\r
376\r
377 //\r
378 // SdPciHcDxe is a device driver, and should ingore the\r
379 // "RemainingDevicePath" according to EFI spec.\r
380 //\r
381 Status = gBS->OpenProtocol (\r
382 Controller,\r
383 &gEfiDevicePathProtocolGuid,\r
384 (VOID *) &ParentDevicePath,\r
385 This->DriverBindingHandle,\r
386 Controller,\r
387 EFI_OPEN_PROTOCOL_BY_DRIVER\r
388 );\r
389 if (EFI_ERROR (Status)) {\r
390 //\r
391 // EFI_ALREADY_STARTED is also an error.\r
392 //\r
393 return Status;\r
394 }\r
395 //\r
396 // Close the protocol because we don't use it here.\r
397 //\r
398 gBS->CloseProtocol (\r
399 Controller,\r
400 &gEfiDevicePathProtocolGuid,\r
401 This->DriverBindingHandle,\r
402 Controller\r
403 );\r
404\r
405 //\r
406 // Now test the EfiPciIoProtocol.\r
407 //\r
408 Status = gBS->OpenProtocol (\r
409 Controller,\r
410 &gEfiPciIoProtocolGuid,\r
411 (VOID **) &PciIo,\r
412 This->DriverBindingHandle,\r
413 Controller,\r
414 EFI_OPEN_PROTOCOL_BY_DRIVER\r
415 );\r
416 if (EFI_ERROR (Status)) {\r
417 return Status;\r
418 }\r
419\r
420 //\r
421 // Now further check the PCI header: Base class (offset 0x08) and\r
422 // Sub Class (offset 0x05). This controller should be an SD/MMC PCI\r
423 // Host Controller.\r
424 //\r
425 Status = PciIo->Pci.Read (\r
426 PciIo,\r
427 EfiPciIoWidthUint8,\r
428 0,\r
429 sizeof (PciData),\r
430 &PciData\r
431 );\r
432 if (EFI_ERROR (Status)) {\r
433 gBS->CloseProtocol (\r
434 Controller,\r
435 &gEfiPciIoProtocolGuid,\r
436 This->DriverBindingHandle,\r
437 Controller\r
438 );\r
439 return EFI_UNSUPPORTED;\r
440 }\r
441 //\r
442 // Since we already got the PciData, we can close protocol to avoid to carry it\r
443 // on for multiple exit points.\r
444 //\r
445 gBS->CloseProtocol (\r
446 Controller,\r
447 &gEfiPciIoProtocolGuid,\r
448 This->DriverBindingHandle,\r
449 Controller\r
450 );\r
451\r
452 //\r
453 // Examine SD PCI Host Controller PCI Configuration table fields.\r
454 //\r
455 if ((PciData.Hdr.ClassCode[2] == PCI_CLASS_SYSTEM_PERIPHERAL) &&\r
456 (PciData.Hdr.ClassCode[1] == PCI_SUBCLASS_SD_HOST_CONTROLLER) &&\r
457 ((PciData.Hdr.ClassCode[0] == 0x00) || (PciData.Hdr.ClassCode[0] == 0x01))) {\r
458 return EFI_SUCCESS;\r
459 }\r
460\r
461 return EFI_UNSUPPORTED;\r
462}\r
463\r
464/**\r
465 Starts a device controller or a bus controller.\r
466\r
467 The Start() function is designed to be invoked from the EFI boot service ConnectController().\r
468 As a result, much of the error checking on the parameters to Start() has been moved into this\r
469 common boot service. It is legal to call Start() from other locations,\r
470 but the following calling restrictions must be followed or the system behavior will not be deterministic.\r
471 1. ControllerHandle must be a valid EFI_HANDLE.\r
472 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned\r
473 EFI_DEVICE_PATH_PROTOCOL.\r
474 3. Prior to calling Start(), the Supported() function for the driver specified by This must\r
475 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.\r
476\r
477 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
478 @param[in] ControllerHandle The handle of the controller to start. This handle\r
479 must support a protocol interface that supplies\r
480 an I/O abstraction to the driver.\r
481 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This\r
482 parameter is ignored by device drivers, and is optional for bus\r
483 drivers. For a bus driver, if this parameter is NULL, then handles\r
484 for all the children of Controller are created by this driver.\r
485 If this parameter is not NULL and the first Device Path Node is\r
486 not the End of Device Path Node, then only the handle for the\r
487 child device specified by the first Device Path Node of\r
488 RemainingDevicePath is created by this driver.\r
489 If the first Device Path Node of RemainingDevicePath is\r
490 the End of Device Path Node, no child handle is created by this\r
491 driver.\r
492\r
493 @retval EFI_SUCCESS The device was started.\r
494 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.\r
495 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
496 @retval Others The driver failded to start the device.\r
497\r
498**/\r
499EFI_STATUS\r
500EFIAPI\r
501SdMmcPciHcDriverBindingStart (\r
502 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
503 IN EFI_HANDLE Controller,\r
504 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
505 )\r
506{\r
507 EFI_STATUS Status;\r
508 SD_MMC_HC_PRIVATE_DATA *Private;\r
509 EFI_PCI_IO_PROTOCOL *PciIo;\r
510 UINT64 Supports;\r
511 UINT64 PciAttributes;\r
512 UINT8 SlotNum;\r
513 UINT8 FirstBar;\r
514 UINT8 Slot;\r
515 UINT8 Index;\r
516 CARD_TYPE_DETECT_ROUTINE *Routine;\r
517 UINT32 RoutineNum;\r
518 BOOLEAN MediaPresent;\r
519\r
520 DEBUG ((EFI_D_INFO, "SdMmcPciHcDriverBindingStart: Start\n"));\r
521\r
522 //\r
523 // Open PCI I/O Protocol and save pointer to open protocol\r
524 // in private data area.\r
525 //\r
526 PciIo = NULL;\r
527 Status = gBS->OpenProtocol (\r
528 Controller,\r
529 &gEfiPciIoProtocolGuid,\r
530 (VOID **) &PciIo,\r
531 This->DriverBindingHandle,\r
532 Controller,\r
533 EFI_OPEN_PROTOCOL_BY_DRIVER\r
534 );\r
535 if (EFI_ERROR (Status)) {\r
536 return Status;\r
537 }\r
538\r
539 //\r
540 // Enable the SD Host Controller MMIO space\r
541 //\r
542 Private = NULL;\r
543 Status = PciIo->Attributes (\r
544 PciIo,\r
545 EfiPciIoAttributeOperationGet,\r
546 0,\r
547 &PciAttributes\r
548 );\r
549\r
550 if (EFI_ERROR (Status)) {\r
551 goto Done;\r
552 }\r
553\r
554 Status = PciIo->Attributes (\r
555 PciIo,\r
556 EfiPciIoAttributeOperationSupported,\r
557 0,\r
558 &Supports\r
559 );\r
560\r
561 if (!EFI_ERROR (Status)) {\r
562 Supports &= (UINT64)EFI_PCI_DEVICE_ENABLE;\r
563 Status = PciIo->Attributes (\r
564 PciIo,\r
565 EfiPciIoAttributeOperationEnable,\r
566 Supports,\r
567 NULL\r
568 );\r
569 } else {\r
570 goto Done;\r
571 }\r
572\r
573 Private = AllocateCopyPool (sizeof (SD_MMC_HC_PRIVATE_DATA), &gSdMmcPciHcTemplate);\r
574 if (Private == NULL) {\r
575 Status = EFI_OUT_OF_RESOURCES;\r
576 goto Done;\r
577 }\r
578\r
579 Private->ControllerHandle = Controller;\r
580 Private->PciIo = PciIo;\r
581 Private->PciAttributes = PciAttributes;\r
582 InitializeListHead (&Private->Queue);\r
583\r
584 //\r
585 // Get SD/MMC Pci Host Controller Slot info\r
586 //\r
587 Status = SdMmcHcGetSlotInfo (PciIo, &FirstBar, &SlotNum);\r
588 if (EFI_ERROR (Status)) {\r
589 goto Done;\r
590 }\r
591\r
592 for (Slot = FirstBar; Slot < (FirstBar + SlotNum); Slot++) {\r
593 Private->Slot[Slot].Enable = TRUE;\r
594\r
595 Status = SdMmcHcGetCapability (PciIo, Slot, &Private->Capability[Slot]);\r
596 if (EFI_ERROR (Status)) {\r
597 continue;\r
598 }\r
599 DumpCapabilityReg (Slot, &Private->Capability[Slot]);\r
600\r
601 Status = SdMmcHcGetMaxCurrent (PciIo, Slot, &Private->MaxCurrent[Slot]);\r
602 if (EFI_ERROR (Status)) {\r
603 continue;\r
604 }\r
605\r
606 Private->Slot[Slot].SlotType = Private->Capability[Slot].SlotType;\r
607 if ((Private->Slot[Slot].SlotType != RemovableSlot) && (Private->Slot[Slot].SlotType != EmbeddedSlot)) {\r
608 DEBUG ((EFI_D_INFO, "SdMmcPciHcDxe doesn't support the slot type [%d]!!!\n", Private->Slot[Slot].SlotType));\r
609 continue;\r
610 }\r
611\r
612 //\r
613 // Reset the specified slot of the SD/MMC Pci Host Controller\r
614 //\r
615 Status = SdMmcHcReset (PciIo, Slot);\r
616 if (EFI_ERROR (Status)) {\r
617 continue;\r
618 }\r
619 //\r
620 // Check whether there is a SD/MMC card attached\r
621 //\r
622 Status = SdMmcHcCardDetect (PciIo, Slot, &MediaPresent);\r
2e9107b8
FT
623 if (EFI_ERROR (Status) && (Status != EFI_MEDIA_CHANGED)) {\r
624 continue;\r
625 } else if (MediaPresent == FALSE) {\r
48555339
FT
626 DEBUG ((EFI_D_ERROR, "SdMmcHcCardDetect: No device attached in Slot[%d]!!!\n", Slot));\r
627 continue;\r
628 }\r
629\r
630 Status = SdMmcHcInitHost (PciIo, Slot, Private->Capability[Slot]);\r
631 if (EFI_ERROR (Status)) {\r
632 continue;\r
633 }\r
634\r
635 Private->Slot[Slot].MediaPresent = TRUE;\r
636 RoutineNum = sizeof (mCardTypeDetectRoutineTable) / sizeof (CARD_TYPE_DETECT_ROUTINE);\r
637 for (Index = 0; Index < RoutineNum; Index++) {\r
638 Routine = &mCardTypeDetectRoutineTable[Index];\r
639 if (*Routine != NULL) {\r
640 Status = (*Routine) (Private, Slot);\r
641 if (!EFI_ERROR (Status)) {\r
642 break;\r
643 }\r
644 }\r
645 }\r
646 }\r
647\r
648 //\r
649 // Start the asynchronous I/O monitor\r
650 //\r
651 Status = gBS->CreateEvent (\r
652 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
653 TPL_CALLBACK,\r
654 ProcessAsyncTaskList,\r
655 Private,\r
656 &Private->TimerEvent\r
657 );\r
658 if (EFI_ERROR (Status)) {\r
659 goto Done;\r
660 }\r
661\r
662 Status = gBS->SetTimer (Private->TimerEvent, TimerPeriodic, SD_MMC_HC_ASYNC_TIMER);\r
663 if (EFI_ERROR (Status)) {\r
664 goto Done;\r
665 }\r
666\r
667 //\r
668 // Start the Sd removable device connection enumeration\r
669 //\r
670 Status = gBS->CreateEvent (\r
671 EVT_TIMER | EVT_NOTIFY_SIGNAL,\r
672 TPL_CALLBACK,\r
673 SdMmcPciHcEnumerateDevice,\r
674 Private,\r
675 &Private->ConnectEvent\r
676 );\r
677 if (EFI_ERROR (Status)) {\r
678 goto Done;\r
679 }\r
680\r
681 Status = gBS->SetTimer (Private->ConnectEvent, TimerPeriodic, SD_MMC_HC_ENUM_TIMER);\r
682 if (EFI_ERROR (Status)) {\r
683 goto Done;\r
684 }\r
685\r
686 Status = gBS->InstallMultipleProtocolInterfaces (\r
687 &Controller,\r
688 &gEfiSdMmcPassThruProtocolGuid,\r
689 &(Private->PassThru),\r
690 NULL\r
691 );\r
692\r
693 DEBUG ((EFI_D_INFO, "SdMmcPciHcDriverBindingStart: %r End on %x\n", Status, Controller));\r
694\r
695Done:\r
696 if (EFI_ERROR (Status)) {\r
697 if ((Private != NULL) && (Private->PciAttributes != 0)) {\r
698 //\r
699 // Restore original PCI attributes\r
700 //\r
701 PciIo->Attributes (\r
702 PciIo,\r
703 EfiPciIoAttributeOperationSet,\r
704 Private->PciAttributes,\r
705 NULL\r
706 );\r
707 }\r
708 gBS->CloseProtocol (\r
709 Controller,\r
710 &gEfiPciIoProtocolGuid,\r
711 This->DriverBindingHandle,\r
712 Controller\r
713 );\r
714\r
715 if ((Private != NULL) && (Private->TimerEvent != NULL)) {\r
716 gBS->CloseEvent (Private->TimerEvent);\r
717 }\r
718\r
719 if ((Private != NULL) && (Private->ConnectEvent != NULL)) {\r
720 gBS->CloseEvent (Private->ConnectEvent);\r
721 }\r
722\r
723 if (Private != NULL) {\r
724 FreePool (Private);\r
725 }\r
726 }\r
727\r
728 return Status;\r
729}\r
730\r
731/**\r
732 Stops a device controller or a bus controller.\r
733\r
734 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().\r
735 As a result, much of the error checking on the parameters to Stop() has been moved\r
736 into this common boot service. It is legal to call Stop() from other locations,\r
737 but the following calling restrictions must be followed or the system behavior will not be deterministic.\r
738 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this\r
739 same driver's Start() function.\r
740 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid\r
741 EFI_HANDLE. In addition, all of these handles must have been created in this driver's\r
742 Start() function, and the Start() function must have called OpenProtocol() on\r
743 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r
744\r
745 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
746 @param[in] ControllerHandle A handle to the device being stopped. The handle must\r
747 support a bus specific I/O protocol for the driver\r
748 to use to stop the device.\r
749 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.\r
750 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL\r
751 if NumberOfChildren is 0.\r
752\r
753 @retval EFI_SUCCESS The device was stopped.\r
754 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.\r
755\r
756**/\r
757EFI_STATUS\r
758EFIAPI\r
759SdMmcPciHcDriverBindingStop (\r
760 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
761 IN EFI_HANDLE Controller,\r
762 IN UINTN NumberOfChildren,\r
763 IN EFI_HANDLE *ChildHandleBuffer\r
764 )\r
765{\r
766 EFI_STATUS Status;\r
767 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;\r
768 SD_MMC_HC_PRIVATE_DATA *Private;\r
769 EFI_PCI_IO_PROTOCOL *PciIo;\r
770 LIST_ENTRY *Link;\r
771 LIST_ENTRY *NextLink;\r
772 SD_MMC_HC_TRB *Trb;\r
773\r
774 DEBUG ((EFI_D_INFO, "SdMmcPciHcDriverBindingStop: Start\n"));\r
775\r
776 Status = gBS->OpenProtocol (\r
777 Controller,\r
778 &gEfiSdMmcPassThruProtocolGuid,\r
779 (VOID**) &PassThru,\r
780 This->DriverBindingHandle,\r
781 Controller,\r
782 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
783 );\r
784 if (EFI_ERROR (Status)) {\r
785 return Status;\r
786 }\r
787\r
788 Private = SD_MMC_HC_PRIVATE_FROM_THIS (PassThru);\r
789 //\r
790 // Close Non-Blocking timer and free Task list.\r
791 //\r
792 if (Private->TimerEvent != NULL) {\r
793 gBS->CloseEvent (Private->TimerEvent);\r
794 Private->TimerEvent = NULL;\r
795 }\r
796 if (Private->ConnectEvent != NULL) {\r
797 gBS->CloseEvent (Private->ConnectEvent);\r
798 Private->ConnectEvent = NULL;\r
799 }\r
800 //\r
801 // As the timer is closed, there is no needs to use TPL lock to\r
802 // protect the critical region "queue".\r
803 //\r
804 for (Link = GetFirstNode (&Private->Queue);\r
805 !IsNull (&Private->Queue, Link);\r
806 Link = NextLink) {\r
807 NextLink = GetNextNode (&Private->Queue, Link);\r
808 RemoveEntryList (Link);\r
809 Trb = SD_MMC_HC_TRB_FROM_THIS (Link);\r
810 Trb->Packet->TransactionStatus = EFI_ABORTED;\r
811 gBS->SignalEvent (Trb->Event);\r
812 SdMmcFreeTrb (Trb);\r
813 }\r
814\r
815 //\r
816 // Uninstall Block I/O protocol from the device handle\r
817 //\r
818 Status = gBS->UninstallProtocolInterface (\r
819 Controller,\r
820 &gEfiSdMmcPassThruProtocolGuid,\r
821 &(Private->PassThru)\r
822 );\r
823\r
824 if (EFI_ERROR (Status)) {\r
825 return Status;\r
826 }\r
827\r
828 gBS->CloseProtocol (\r
829 Controller,\r
830 &gEfiPciIoProtocolGuid,\r
831 This->DriverBindingHandle,\r
832 Controller\r
833 );\r
834 //\r
835 // Restore original PCI attributes\r
836 //\r
837 PciIo = Private->PciIo;\r
838 Status = PciIo->Attributes (\r
839 PciIo,\r
840 EfiPciIoAttributeOperationSet,\r
841 Private->PciAttributes,\r
842 NULL\r
843 );\r
844 ASSERT_EFI_ERROR (Status);\r
845\r
846 FreePool (Private);\r
847\r
848 DEBUG ((EFI_D_INFO, "SdMmcPciHcDriverBindingStop: End with %r\n", Status));\r
849\r
850 return Status;\r
851}\r
852\r
853/**\r
854 Sends SD command to an SD card that is attached to the SD controller.\r
855\r
856 The PassThru() function sends the SD command specified by Packet to the SD card\r
857 specified by Slot.\r
858\r
859 If Packet is successfully sent to the SD card, then EFI_SUCCESS is returned.\r
860\r
861 If a device error occurs while sending the Packet, then EFI_DEVICE_ERROR is returned.\r
862\r
863 If Slot is not in a valid range for the SD controller, then EFI_INVALID_PARAMETER\r
864 is returned.\r
865\r
866 If Packet defines a data command but both InDataBuffer and OutDataBuffer are NULL,\r
867 EFI_INVALID_PARAMETER is returned.\r
868\r
869 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.\r
870 @param[in] Slot The slot number of the SD card to send the command to.\r
871 @param[in,out] Packet A pointer to the SD command data structure.\r
872 @param[in] Event If Event is NULL, blocking I/O is performed. If Event is\r
873 not NULL, then nonblocking I/O is performed, and Event\r
874 will be signaled when the Packet completes.\r
875\r
876 @retval EFI_SUCCESS The SD Command Packet was sent by the host.\r
877 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SD\r
878 command Packet.\r
879 @retval EFI_INVALID_PARAMETER Packet, Slot, or the contents of the Packet is invalid.\r
880 @retval EFI_INVALID_PARAMETER Packet defines a data command but both InDataBuffer and\r
881 OutDataBuffer are NULL.\r
882 @retval EFI_NO_MEDIA SD Device not present in the Slot.\r
883 @retval EFI_UNSUPPORTED The command described by the SD Command Packet is not\r
884 supported by the host controller.\r
885 @retval EFI_BAD_BUFFER_SIZE The InTransferLength or OutTransferLength exceeds the\r
886 limit supported by SD card ( i.e. if the number of bytes\r
887 exceed the Last LBA).\r
888\r
889**/\r
890EFI_STATUS\r
891EFIAPI\r
892SdMmcPassThruPassThru (\r
893 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *This,\r
894 IN UINT8 Slot,\r
895 IN OUT EFI_SD_MMC_PASS_THRU_COMMAND_PACKET *Packet,\r
896 IN EFI_EVENT Event OPTIONAL\r
897 )\r
898{\r
899 EFI_STATUS Status;\r
900 SD_MMC_HC_PRIVATE_DATA *Private;\r
901 SD_MMC_HC_TRB *Trb;\r
902 EFI_TPL OldTpl;\r
903\r
904 if ((This == NULL) || (Packet == NULL)) {\r
905 return EFI_INVALID_PARAMETER;\r
906 }\r
907\r
908 if ((Packet->SdMmcCmdBlk == NULL) || (Packet->SdMmcStatusBlk == NULL)) {\r
909 return EFI_INVALID_PARAMETER;\r
910 }\r
911\r
912 if ((Packet->OutDataBuffer == NULL) && (Packet->OutTransferLength != 0)) {\r
913 return EFI_INVALID_PARAMETER;\r
914 }\r
915\r
916 if ((Packet->InDataBuffer == NULL) && (Packet->InTransferLength != 0)) {\r
917 return EFI_INVALID_PARAMETER;\r
918 }\r
919\r
920 Private = SD_MMC_HC_PRIVATE_FROM_THIS (This);\r
921\r
922 if (!Private->Slot[Slot].Enable) {\r
923 return EFI_INVALID_PARAMETER;\r
924 }\r
925\r
926 if (!Private->Slot[Slot].MediaPresent) {\r
927 return EFI_NO_MEDIA;\r
928 }\r
929\r
930 Trb = SdMmcCreateTrb (Private, Slot, Packet, Event);\r
931 if (Trb == NULL) {\r
932 return EFI_OUT_OF_RESOURCES;\r
933 }\r
934 //\r
935 // Immediately return for async I/O.\r
936 //\r
937 if (Event != NULL) {\r
938 return EFI_SUCCESS;\r
939 }\r
940\r
941 //\r
942 // Wait async I/O list is empty before execute sync I/O operation.\r
943 //\r
944 while (TRUE) {\r
945 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
946 if (IsListEmpty (&Private->Queue)) {\r
947 gBS->RestoreTPL (OldTpl);\r
948 break;\r
949 }\r
950 gBS->RestoreTPL (OldTpl);\r
951 }\r
952\r
953 Status = SdMmcWaitTrbEnv (Private, Trb);\r
954 if (EFI_ERROR (Status)) {\r
955 goto Done;\r
956 }\r
957\r
958 Status = SdMmcExecTrb (Private, Trb);\r
959 if (EFI_ERROR (Status)) {\r
960 goto Done;\r
961 }\r
962\r
963 Status = SdMmcWaitTrbResult (Private, Trb);\r
964 if (EFI_ERROR (Status)) {\r
965 goto Done;\r
966 }\r
967\r
968Done:\r
969 if ((Trb != NULL) && (Trb->AdmaDesc != NULL)) {\r
970 FreePages (Trb->AdmaDesc, Trb->AdmaPages);\r
971 }\r
972\r
973 if (Trb != NULL) {\r
974 FreePool (Trb);\r
975 }\r
976\r
977 return Status;\r
978}\r
979\r
980/**\r
981 Used to retrieve next slot numbers supported by the SD controller. The function\r
982 returns information about all available slots (populated or not-populated).\r
983\r
984 The GetNextSlot() function retrieves the next slot number on an SD controller.\r
985 If on input Slot is 0xFF, then the slot number of the first slot on the SD controller\r
986 is returned.\r
987\r
988 If Slot is a slot number that was returned on a previous call to GetNextSlot(), then\r
989 the slot number of the next slot on the SD controller is returned.\r
990\r
991 If Slot is not 0xFF and Slot was not returned on a previous call to GetNextSlot(),\r
992 EFI_INVALID_PARAMETER is returned.\r
993\r
994 If Slot is the slot number of the last slot on the SD controller, then EFI_NOT_FOUND\r
995 is returned.\r
996\r
997 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.\r
998 @param[in,out] Slot On input, a pointer to a slot number on the SD controller.\r
999 On output, a pointer to the next slot number on the SD controller.\r
1000 An input value of 0xFF retrieves the first slot number on the SD\r
1001 controller.\r
1002\r
1003 @retval EFI_SUCCESS The next slot number on the SD controller was returned in Slot.\r
1004 @retval EFI_NOT_FOUND There are no more slots on this SD controller.\r
1005 @retval EFI_INVALID_PARAMETER Slot is not 0xFF and Slot was not returned on a previous call\r
1006 to GetNextSlot().\r
1007\r
1008**/\r
1009EFI_STATUS\r
1010EFIAPI\r
1011SdMmcPassThruGetNextSlot (\r
1012 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *This,\r
1013 IN OUT UINT8 *Slot\r
1014 )\r
1015{\r
1016 SD_MMC_HC_PRIVATE_DATA *Private;\r
1017 UINT8 Index;\r
1018\r
1019 if ((This == NULL) || (Slot == NULL)) {\r
1020 return EFI_INVALID_PARAMETER;\r
1021 }\r
1022\r
1023 Private = SD_MMC_HC_PRIVATE_FROM_THIS (This);\r
1024\r
1025 if (*Slot == 0xFF) {\r
1026 for (Index = 0; 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 if (*Slot == Private->PreviousSlot) {\r
1035 for (Index = *Slot + 1; Index < SD_MMC_HC_MAX_SLOT; Index++) {\r
1036 if (Private->Slot[Index].Enable) {\r
1037 *Slot = Index;\r
1038 Private->PreviousSlot = Index;\r
1039 return EFI_SUCCESS;\r
1040 }\r
1041 }\r
1042 return EFI_NOT_FOUND;\r
1043 } else {\r
1044 return EFI_INVALID_PARAMETER;\r
1045 }\r
1046}\r
1047\r
1048/**\r
1049 Used to allocate and build a device path node for an SD card on the SD controller.\r
1050\r
1051 The BuildDevicePath() function allocates and builds a single device node for the SD\r
1052 card specified by Slot.\r
1053\r
1054 If the SD card specified by Slot is not present on the SD controller, then EFI_NOT_FOUND\r
1055 is returned.\r
1056\r
1057 If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned.\r
1058\r
1059 If there are not enough resources to allocate the device path node, then EFI_OUT_OF_RESOURCES\r
1060 is returned.\r
1061\r
1062 Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of\r
1063 DevicePath are initialized to describe the SD card specified by Slot, and EFI_SUCCESS is\r
1064 returned.\r
1065\r
1066 @param[in] This A pointer to the EFI_SD_MMMC_PASS_THRU_PROTOCOL instance.\r
1067 @param[in] Slot Specifies the slot number of the SD card for which a device\r
1068 path node is to be allocated and built.\r
1069 @param[in,out] DevicePath A pointer to a single device path node that describes the SD\r
1070 card specified by Slot. This function is responsible for\r
1071 allocating the buffer DevicePath with the boot service\r
1072 AllocatePool(). It is the caller's responsibility to free\r
1073 DevicePath when the caller is finished with DevicePath.\r
1074\r
1075 @retval EFI_SUCCESS The device path node that describes the SD card specified by\r
1076 Slot was allocated and returned in DevicePath.\r
1077 @retval EFI_NOT_FOUND The SD card specified by Slot does not exist on the SD controller.\r
1078 @retval EFI_INVALID_PARAMETER DevicePath is NULL.\r
1079 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.\r
1080\r
1081**/\r
1082EFI_STATUS\r
1083EFIAPI\r
1084SdMmcPassThruBuildDevicePath (\r
1085 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *This,\r
1086 IN UINT8 Slot,\r
1087 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath\r
1088 )\r
1089{\r
1090 SD_MMC_HC_PRIVATE_DATA *Private;\r
1091 SD_DEVICE_PATH *SdNode;\r
1092 EMMC_DEVICE_PATH *EmmcNode;\r
1093\r
1094 if ((This == NULL) || (DevicePath == NULL) || (Slot >= SD_MMC_HC_MAX_SLOT)) {\r
1095 return EFI_INVALID_PARAMETER;\r
1096 }\r
1097\r
1098 Private = SD_MMC_HC_PRIVATE_FROM_THIS (This);\r
1099\r
1100 if ((!Private->Slot[Slot].Enable) || (!Private->Slot[Slot].MediaPresent)) {\r
1101 return EFI_NOT_FOUND;\r
1102 }\r
1103\r
1104 if (Private->Slot[Slot].CardType == SdCardType) {\r
1105 SdNode = AllocateCopyPool (sizeof (SD_DEVICE_PATH), &mSdDpTemplate);\r
1106 if (SdNode == NULL) {\r
1107 return EFI_OUT_OF_RESOURCES;\r
1108 }\r
1109 SdNode->SlotNumber = Slot;\r
1110\r
1111 *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) SdNode;\r
1112 } else if (Private->Slot[Slot].CardType == EmmcCardType) {\r
1113 EmmcNode = AllocateCopyPool (sizeof (EMMC_DEVICE_PATH), &mEmmcDpTemplate);\r
1114 if (EmmcNode == NULL) {\r
1115 return EFI_OUT_OF_RESOURCES;\r
1116 }\r
1117 EmmcNode->SlotNumber = Slot;\r
1118\r
1119 *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) EmmcNode;\r
1120 } else {\r
1121 //\r
1122 // Currently we only support SD and EMMC two device nodes.\r
1123 //\r
1124 return EFI_NOT_FOUND;\r
1125 }\r
1126\r
1127 return EFI_SUCCESS;\r
1128}\r
1129\r
1130/**\r
1131 This function retrieves an SD card slot number based on the input device path.\r
1132\r
1133 The GetSlotNumber() function retrieves slot number for the SD card specified by\r
1134 the DevicePath node. If DevicePath is NULL, EFI_INVALID_PARAMETER is returned.\r
1135\r
1136 If DevicePath is not a device path node type that the SD Pass Thru driver supports,\r
1137 EFI_UNSUPPORTED is returned.\r
1138\r
1139 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.\r
1140 @param[in] DevicePath A pointer to the device path node that describes a SD\r
1141 card on the SD controller.\r
1142 @param[out] Slot On return, points to the slot number of an SD card on\r
1143 the SD controller.\r
1144\r
1145 @retval EFI_SUCCESS SD card slot number is returned in Slot.\r
1146 @retval EFI_INVALID_PARAMETER Slot or DevicePath is NULL.\r
1147 @retval EFI_UNSUPPORTED DevicePath is not a device path node type that the SD\r
1148 Pass Thru driver supports.\r
1149\r
1150**/\r
1151EFI_STATUS\r
1152EFIAPI\r
1153SdMmcPassThruGetSlotNumber (\r
1154 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *This,\r
1155 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
1156 OUT UINT8 *Slot\r
1157 )\r
1158{\r
1159 SD_MMC_HC_PRIVATE_DATA *Private;\r
1160 SD_DEVICE_PATH *SdNode;\r
1161 EMMC_DEVICE_PATH *EmmcNode;\r
1162 UINT8 SlotNumber;\r
1163\r
1164 if ((This == NULL) || (DevicePath == NULL) || (Slot == NULL)) {\r
1165 return EFI_INVALID_PARAMETER;\r
1166 }\r
1167\r
1168 Private = SD_MMC_HC_PRIVATE_FROM_THIS (This);\r
1169\r
1170 //\r
1171 // Check whether the DevicePath belongs to SD_DEVICE_PATH or EMMC_DEVICE_PATH\r
1172 //\r
1173 if ((DevicePath->Type != MESSAGING_DEVICE_PATH) ||\r
1174 ((DevicePath->SubType != MSG_SD_DP) &&\r
1175 (DevicePath->SubType != MSG_EMMC_DP)) ||\r
1176 (DevicePathNodeLength(DevicePath) != sizeof(SD_DEVICE_PATH)) ||\r
1177 (DevicePathNodeLength(DevicePath) != sizeof(EMMC_DEVICE_PATH))) {\r
1178 return EFI_UNSUPPORTED;\r
1179 }\r
1180\r
1181 if (DevicePath->SubType == MSG_SD_DP) {\r
1182 SdNode = (SD_DEVICE_PATH *) DevicePath;\r
1183 SlotNumber = SdNode->SlotNumber;\r
1184 } else {\r
1185 EmmcNode = (EMMC_DEVICE_PATH *) DevicePath;\r
1186 SlotNumber = EmmcNode->SlotNumber;\r
1187 }\r
1188\r
1189 if (SlotNumber >= SD_MMC_HC_MAX_SLOT) {\r
1190 return EFI_NOT_FOUND;\r
1191 }\r
1192\r
1193 if (Private->Slot[SlotNumber].Enable) {\r
1194 *Slot = SlotNumber;\r
1195 return EFI_SUCCESS;\r
1196 } else {\r
1197 return EFI_NOT_FOUND;\r
1198 }\r
1199}\r
1200\r
1201/**\r
1202 Resets an SD card that is connected to the SD controller.\r
1203\r
1204 The ResetDevice() function resets the SD card specified by Slot.\r
1205\r
1206 If this SD controller does not support a device reset operation, EFI_UNSUPPORTED is\r
1207 returned.\r
1208\r
1209 If Slot is not in a valid slot number for this SD controller, EFI_INVALID_PARAMETER\r
1210 is returned.\r
1211\r
1212 If the device reset operation is completed, EFI_SUCCESS is returned.\r
1213\r
1214 @param[in] This A pointer to the EFI_SD_MMC_PASS_THRU_PROTOCOL instance.\r
1215 @param[in] Slot Specifies the slot number of the SD card to be reset.\r
1216\r
1217 @retval EFI_SUCCESS The SD card specified by Slot was reset.\r
1218 @retval EFI_UNSUPPORTED The SD controller does not support a device reset operation.\r
1219 @retval EFI_INVALID_PARAMETER Slot number is invalid.\r
1220 @retval EFI_NO_MEDIA SD Device not present in the Slot.\r
1221 @retval EFI_DEVICE_ERROR The reset command failed due to a device error\r
1222\r
1223**/\r
1224EFI_STATUS\r
1225EFIAPI\r
1226SdMmcPassThruResetDevice (\r
1227 IN EFI_SD_MMC_PASS_THRU_PROTOCOL *This,\r
1228 IN UINT8 Slot\r
1229 )\r
1230{\r
1231 SD_MMC_HC_PRIVATE_DATA *Private;\r
1232 LIST_ENTRY *Link;\r
1233 LIST_ENTRY *NextLink;\r
1234 SD_MMC_HC_TRB *Trb;\r
1235 EFI_TPL OldTpl;\r
1236\r
1237 if (This == NULL) {\r
1238 return EFI_INVALID_PARAMETER;\r
1239 }\r
1240\r
1241 Private = SD_MMC_HC_PRIVATE_FROM_THIS (This);\r
1242\r
1243 if (!Private->Slot[Slot].Enable) {\r
1244 return EFI_INVALID_PARAMETER;\r
1245 }\r
1246\r
1247 if (!Private->Slot[Slot].MediaPresent) {\r
1248 return EFI_NO_MEDIA;\r
1249 }\r
1250 //\r
1251 // Free all async I/O requests in the queue\r
1252 //\r
1253 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);\r
1254\r
1255 for (Link = GetFirstNode (&Private->Queue);\r
1256 !IsNull (&Private->Queue, Link);\r
1257 Link = NextLink) {\r
1258 NextLink = GetNextNode (&Private->Queue, Link);\r
1259 RemoveEntryList (Link);\r
1260 Trb = SD_MMC_HC_TRB_FROM_THIS (Link);\r
1261 Trb->Packet->TransactionStatus = EFI_ABORTED;\r
1262 gBS->SignalEvent (Trb->Event);\r
1263 SdMmcFreeTrb (Trb);\r
1264 }\r
1265\r
1266 gBS->RestoreTPL (OldTpl);\r
1267\r
1268 return EFI_SUCCESS;\r
1269}\r
1270\r