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