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