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