]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Sd/SdDxe/SdDxe.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Bus / Sd / SdDxe / SdDxe.c
1 /** @file
2 The SdDxe driver is used to manage the SD memory card device.
3
4 It produces BlockIo and BlockIo2 protocols to allow upper layer
5 access the SD memory card device.
6
7 Copyright (c) 2015 - 2018, Intel Corporation. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
9
10 **/
11
12 #include "SdDxe.h"
13
14 //
15 // SdDxe Driver Binding Protocol Instance
16 //
17 EFI_DRIVER_BINDING_PROTOCOL gSdDxeDriverBinding = {
18 SdDxeDriverBindingSupported,
19 SdDxeDriverBindingStart,
20 SdDxeDriverBindingStop,
21 0x10,
22 NULL,
23 NULL
24 };
25
26 //
27 // Template for SD_DEVICE data structure.
28 //
29 SD_DEVICE mSdDeviceTemplate = {
30 SD_DEVICE_SIGNATURE, // Signature
31 NULL, // Handle
32 NULL, // DevicePath
33 0xFF, // Slot
34 FALSE, // SectorAddressing
35 { // BlockIo
36 EFI_BLOCK_IO_PROTOCOL_REVISION,
37 NULL,
38 SdReset,
39 SdReadBlocks,
40 SdWriteBlocks,
41 SdFlushBlocks
42 },
43 { // BlockIo2
44 NULL,
45 SdResetEx,
46 SdReadBlocksEx,
47 SdWriteBlocksEx,
48 SdFlushBlocksEx
49 },
50 { // BlockMedia
51 0, // MediaId
52 FALSE, // RemovableMedia
53 TRUE, // MediaPresent
54 FALSE, // LogicPartition
55 FALSE, // ReadOnly
56 FALSE, // WritingCache
57 0x200, // BlockSize
58 0, // IoAlign
59 0 // LastBlock
60 },
61 { // EraseBlock
62 EFI_ERASE_BLOCK_PROTOCOL_REVISION,
63 1,
64 SdEraseBlocks
65 },
66 { // DiskInfo
67 EFI_DISK_INFO_SD_MMC_INTERFACE_GUID,
68 SdDiskInfoInquiry,
69 SdDiskInfoIdentify,
70 SdDiskInfoSenseData,
71 SdDiskInfoWhichIde
72 },
73 { // Queue
74 NULL,
75 NULL
76 },
77 { // Csd
78 0,
79 },
80 { // Cid
81 0,
82 },
83 NULL, // ControllerNameTable
84 { // ModelName
85 0,
86 },
87 NULL // Private
88 };
89
90 /**
91 Decode and print SD CSD Register content.
92
93 @param[in] Csd Pointer to SD_CSD data structure.
94
95 @retval EFI_SUCCESS The function completed successfully
96 **/
97 EFI_STATUS
98 DumpCsd (
99 IN SD_CSD *Csd
100 )
101 {
102 SD_CSD2 *Csd2;
103
104 DEBUG ((DEBUG_INFO, "== Dump Sd Csd Register==\n"));
105 DEBUG ((DEBUG_INFO, " CSD structure 0x%x\n", Csd->CsdStructure));
106 DEBUG ((DEBUG_INFO, " Data read access-time 1 0x%x\n", Csd->Taac));
107 DEBUG ((DEBUG_INFO, " Data read access-time 2 0x%x\n", Csd->Nsac));
108 DEBUG ((DEBUG_INFO, " Max. bus clock frequency 0x%x\n", Csd->TranSpeed));
109 DEBUG ((DEBUG_INFO, " Device command classes 0x%x\n", Csd->Ccc));
110 DEBUG ((DEBUG_INFO, " Max. read data block length 0x%x\n", Csd->ReadBlLen));
111 DEBUG ((DEBUG_INFO, " Partial blocks for read allowed 0x%x\n", Csd->ReadBlPartial));
112 DEBUG ((DEBUG_INFO, " Write block misalignment 0x%x\n", Csd->WriteBlkMisalign));
113 DEBUG ((DEBUG_INFO, " Read block misalignment 0x%x\n", Csd->ReadBlkMisalign));
114 DEBUG ((DEBUG_INFO, " DSR implemented 0x%x\n", Csd->DsrImp));
115 if (Csd->CsdStructure == 0) {
116 DEBUG ((DEBUG_INFO, " Device size 0x%x\n", Csd->CSizeLow | (Csd->CSizeHigh << 2)));
117 DEBUG ((DEBUG_INFO, " Max. read current @ VDD min 0x%x\n", Csd->VddRCurrMin));
118 DEBUG ((DEBUG_INFO, " Max. read current @ VDD max 0x%x\n", Csd->VddRCurrMax));
119 DEBUG ((DEBUG_INFO, " Max. write current @ VDD min 0x%x\n", Csd->VddWCurrMin));
120 DEBUG ((DEBUG_INFO, " Max. write current @ VDD max 0x%x\n", Csd->VddWCurrMax));
121 } else {
122 Csd2 = (SD_CSD2 *)(VOID *)Csd;
123 DEBUG ((DEBUG_INFO, " Device size 0x%x\n", Csd2->CSizeLow | (Csd->CSizeHigh << 16)));
124 }
125
126 DEBUG ((DEBUG_INFO, " Erase sector size 0x%x\n", Csd->SectorSize));
127 DEBUG ((DEBUG_INFO, " Erase single block enable 0x%x\n", Csd->EraseBlkEn));
128 DEBUG ((DEBUG_INFO, " Write protect group size 0x%x\n", Csd->WpGrpSize));
129 DEBUG ((DEBUG_INFO, " Write protect group enable 0x%x\n", Csd->WpGrpEnable));
130 DEBUG ((DEBUG_INFO, " Write speed factor 0x%x\n", Csd->R2WFactor));
131 DEBUG ((DEBUG_INFO, " Max. write data block length 0x%x\n", Csd->WriteBlLen));
132 DEBUG ((DEBUG_INFO, " Partial blocks for write allowed 0x%x\n", Csd->WriteBlPartial));
133 DEBUG ((DEBUG_INFO, " File format group 0x%x\n", Csd->FileFormatGrp));
134 DEBUG ((DEBUG_INFO, " Copy flag (OTP) 0x%x\n", Csd->Copy));
135 DEBUG ((DEBUG_INFO, " Permanent write protection 0x%x\n", Csd->PermWriteProtect));
136 DEBUG ((DEBUG_INFO, " Temporary write protection 0x%x\n", Csd->TmpWriteProtect));
137 DEBUG ((DEBUG_INFO, " File format 0x%x\n", Csd->FileFormat));
138
139 return EFI_SUCCESS;
140 }
141
142 /**
143 Get SD device model name.
144
145 @param[in, out] Device The pointer to the SD_DEVICE data structure.
146 @param[in] Cid Pointer to SD_CID data structure.
147
148 @retval EFI_SUCCESS The function completed successfully
149
150 **/
151 EFI_STATUS
152 GetSdModelName (
153 IN OUT SD_DEVICE *Device,
154 IN SD_CID *Cid
155 )
156 {
157 CHAR8 String[SD_MODEL_NAME_MAX_LEN];
158
159 ZeroMem (String, sizeof (String));
160 CopyMem (String, Cid->OemId, sizeof (Cid->OemId));
161 String[sizeof (Cid->OemId)] = ' ';
162 CopyMem (String + sizeof (Cid->OemId) + 1, Cid->ProductName, sizeof (Cid->ProductName));
163 String[sizeof (Cid->OemId) + sizeof (Cid->ProductName)] = ' ';
164 CopyMem (String + sizeof (Cid->OemId) + sizeof (Cid->ProductName) + 1, Cid->ProductSerialNumber, sizeof (Cid->ProductSerialNumber));
165
166 AsciiStrToUnicodeStrS (String, Device->ModelName, sizeof (Device->ModelName) / sizeof (Device->ModelName[0]));
167
168 return EFI_SUCCESS;
169 }
170
171 /**
172 Discover user area partition in the SD device.
173
174 @param[in] Device The pointer to the SD_DEVICE data structure.
175
176 @retval EFI_SUCCESS The user area partition in the SD device is successfully identified.
177 @return Others Some error occurs when identifying the user area.
178
179 **/
180 EFI_STATUS
181 DiscoverUserArea (
182 IN SD_DEVICE *Device
183 )
184 {
185 EFI_STATUS Status;
186 SD_CSD *Csd;
187 SD_CSD2 *Csd2;
188 SD_CID *Cid;
189 UINT64 Capacity;
190 UINT32 DevStatus;
191 UINT16 Rca;
192 UINT32 CSize;
193 UINT32 CSizeMul;
194 UINT32 ReadBlLen;
195
196 //
197 // Deselect the device to force it enter stby mode.
198 // Note here we don't judge return status as some SD devices return
199 // error but the state has been stby.
200 //
201 SdSelect (Device, 0);
202
203 Status = SdSetRca (Device, &Rca);
204 if (EFI_ERROR (Status)) {
205 DEBUG ((DEBUG_ERROR, "DiscoverUserArea(): Assign new Rca = 0x%x fails with %r\n", Rca, Status));
206 return Status;
207 }
208
209 Csd = &Device->Csd;
210 Status = SdGetCsd (Device, Rca, Csd);
211 if (EFI_ERROR (Status)) {
212 return Status;
213 }
214
215 DumpCsd (Csd);
216
217 Cid = &Device->Cid;
218 Status = SdGetCid (Device, Rca, Cid);
219 if (EFI_ERROR (Status)) {
220 return Status;
221 }
222
223 GetSdModelName (Device, Cid);
224
225 Status = SdSelect (Device, Rca);
226 if (EFI_ERROR (Status)) {
227 DEBUG ((DEBUG_ERROR, "DiscoverUserArea(): Reselect the device 0x%x fails with %r\n", Rca, Status));
228 return Status;
229 }
230
231 Status = SdSendStatus (Device, Rca, &DevStatus);
232 if (EFI_ERROR (Status)) {
233 return Status;
234 }
235
236 if (Csd->CsdStructure == 0) {
237 Device->SectorAddressing = FALSE;
238 CSize = (Csd->CSizeHigh << 2 | Csd->CSizeLow) + 1;
239 CSizeMul = (1 << (Csd->CSizeMul + 2));
240 ReadBlLen = (1 << (Csd->ReadBlLen));
241 Capacity = MultU64x32 (MultU64x32 ((UINT64)CSize, CSizeMul), ReadBlLen);
242 } else {
243 Device->SectorAddressing = TRUE;
244 Csd2 = (SD_CSD2 *)(VOID *)Csd;
245 CSize = (Csd2->CSizeHigh << 16 | Csd2->CSizeLow) + 1;
246 Capacity = MultU64x32 ((UINT64)CSize, SIZE_512KB);
247 }
248
249 Device->BlockIo.Media = &Device->BlockMedia;
250 Device->BlockIo2.Media = &Device->BlockMedia;
251 Device->BlockMedia.IoAlign = Device->Private->PassThru->IoAlign;
252 Device->BlockMedia.BlockSize = 0x200;
253 Device->BlockMedia.LastBlock = 0x00;
254 Device->BlockMedia.RemovableMedia = TRUE;
255 Device->BlockMedia.MediaPresent = TRUE;
256 Device->BlockMedia.LogicalPartition = FALSE;
257 Device->BlockMedia.LastBlock = DivU64x32 (Capacity, Device->BlockMedia.BlockSize) - 1;
258
259 if (Csd->EraseBlkEn) {
260 Device->EraseBlock.EraseLengthGranularity = 1;
261 } else {
262 Device->EraseBlock.EraseLengthGranularity = (Csd->SectorSize + 1) * (1 << (Csd->WriteBlLen - 9));
263 }
264
265 return Status;
266 }
267
268 /**
269 Scan SD Bus to discover the device.
270
271 @param[in] Private The SD driver private data structure.
272 @param[in] Slot The slot number to check device present.
273
274 @retval EFI_SUCCESS Successfully to discover the device and attach
275 SdMmcIoProtocol to it.
276 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
277 of resources.
278 @retval EFI_ALREADY_STARTED The device was discovered before.
279 @retval Others Fail to discover the device.
280
281 **/
282 EFI_STATUS
283 EFIAPI
284 DiscoverSdDevice (
285 IN SD_DRIVER_PRIVATE_DATA *Private,
286 IN UINT8 Slot
287 )
288 {
289 EFI_STATUS Status;
290 SD_DEVICE *Device;
291 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
292 EFI_DEVICE_PATH_PROTOCOL *NewDevicePath;
293 EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath;
294 EFI_HANDLE DeviceHandle;
295 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;
296
297 Device = NULL;
298 DevicePath = NULL;
299 NewDevicePath = NULL;
300 RemainingDevicePath = NULL;
301 PassThru = Private->PassThru;
302
303 //
304 // Build Device Path
305 //
306 Status = PassThru->BuildDevicePath (
307 PassThru,
308 Slot,
309 &DevicePath
310 );
311 if (EFI_ERROR (Status)) {
312 return Status;
313 }
314
315 if (DevicePath->SubType != MSG_SD_DP) {
316 Status = EFI_UNSUPPORTED;
317 goto Error;
318 }
319
320 NewDevicePath = AppendDevicePathNode (
321 Private->ParentDevicePath,
322 DevicePath
323 );
324
325 if (NewDevicePath == NULL) {
326 Status = EFI_OUT_OF_RESOURCES;
327 goto Error;
328 }
329
330 DeviceHandle = NULL;
331 RemainingDevicePath = NewDevicePath;
332 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &DeviceHandle);
333 if (!EFI_ERROR (Status) && (DeviceHandle != NULL) && IsDevicePathEnd (RemainingDevicePath)) {
334 //
335 // The device has been started, directly return to fast boot.
336 //
337 Status = EFI_ALREADY_STARTED;
338 goto Error;
339 }
340
341 //
342 // Allocate buffer to store SD_DEVICE private data.
343 //
344 Device = AllocateCopyPool (sizeof (SD_DEVICE), &mSdDeviceTemplate);
345 if (Device == NULL) {
346 Status = EFI_OUT_OF_RESOURCES;
347 goto Error;
348 }
349
350 Device->DevicePath = NewDevicePath;
351 Device->Slot = Slot;
352 Device->Private = Private;
353 InitializeListHead (&Device->Queue);
354
355 //
356 // Expose user area in the Sd memory card to upper layer.
357 //
358 Status = DiscoverUserArea (Device);
359 if (EFI_ERROR (Status)) {
360 goto Error;
361 }
362
363 Device->ControllerNameTable = NULL;
364 AddUnicodeString2 (
365 "eng",
366 gSdDxeComponentName.SupportedLanguages,
367 &Device->ControllerNameTable,
368 Device->ModelName,
369 TRUE
370 );
371 AddUnicodeString2 (
372 "en",
373 gSdDxeComponentName2.SupportedLanguages,
374 &Device->ControllerNameTable,
375 Device->ModelName,
376 FALSE
377 );
378
379 Status = gBS->InstallMultipleProtocolInterfaces (
380 &Device->Handle,
381 &gEfiDevicePathProtocolGuid,
382 Device->DevicePath,
383 &gEfiBlockIoProtocolGuid,
384 &Device->BlockIo,
385 &gEfiBlockIo2ProtocolGuid,
386 &Device->BlockIo2,
387 &gEfiEraseBlockProtocolGuid,
388 &Device->EraseBlock,
389 &gEfiDiskInfoProtocolGuid,
390 &Device->DiskInfo,
391 NULL
392 );
393
394 if (!EFI_ERROR (Status)) {
395 gBS->OpenProtocol (
396 Private->Controller,
397 &gEfiSdMmcPassThruProtocolGuid,
398 (VOID **)&(Private->PassThru),
399 Private->DriverBindingHandle,
400 Device->Handle,
401 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
402 );
403 }
404
405 Error:
406 FreePool (DevicePath);
407
408 if (EFI_ERROR (Status) && (NewDevicePath != NULL)) {
409 FreePool (NewDevicePath);
410 }
411
412 if (EFI_ERROR (Status) && (Device != NULL)) {
413 FreePool (Device);
414 }
415
416 return Status;
417 }
418
419 /**
420 Tests to see if this driver supports a given controller. If a child device is provided,
421 it further tests to see if this driver supports creating a handle for the specified child device.
422
423 This function checks to see if the driver specified by This supports the device specified by
424 ControllerHandle. Drivers will typically use the device path attached to
425 ControllerHandle and/or the services from the bus I/O abstraction attached to
426 ControllerHandle to determine if the driver supports ControllerHandle. This function
427 may be called many times during platform initialization. In order to reduce boot times, the tests
428 performed by this function must be very small, and take as little time as possible to execute. This
429 function must not change the state of any hardware devices, and this function must be aware that the
430 device specified by ControllerHandle may already be managed by the same driver or a
431 different driver. This function must match its calls to AllocatePages() with FreePages(),
432 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
433 Since ControllerHandle may have been previously started by the same driver, if a protocol is
434 already in the opened state, then it must not be closed with CloseProtocol(). This is required
435 to guarantee the state of ControllerHandle is not modified by this function.
436
437 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
438 @param[in] ControllerHandle The handle of the controller to test. This handle
439 must support a protocol interface that supplies
440 an I/O abstraction to the driver.
441 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
442 parameter is ignored by device drivers, and is optional for bus
443 drivers. For bus drivers, if this parameter is not NULL, then
444 the bus driver must determine if the bus controller specified
445 by ControllerHandle and the child controller specified
446 by RemainingDevicePath are both supported by this
447 bus driver.
448
449 @retval EFI_SUCCESS The device specified by ControllerHandle and
450 RemainingDevicePath is supported by the driver specified by This.
451 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
452 RemainingDevicePath is already being managed by the driver
453 specified by This.
454 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
455 RemainingDevicePath is already being managed by a different
456 driver or an application that requires exclusive access.
457 Currently not implemented.
458 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
459 RemainingDevicePath is not supported by the driver specified by This.
460 **/
461 EFI_STATUS
462 EFIAPI
463 SdDxeDriverBindingSupported (
464 IN EFI_DRIVER_BINDING_PROTOCOL *This,
465 IN EFI_HANDLE Controller,
466 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
467 )
468 {
469 EFI_STATUS Status;
470 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
471 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;
472 UINT8 Slot;
473
474 //
475 // Test EFI_SD_MMC_PASS_THRU_PROTOCOL on the controller handle.
476 //
477 Status = gBS->OpenProtocol (
478 Controller,
479 &gEfiSdMmcPassThruProtocolGuid,
480 (VOID **)&PassThru,
481 This->DriverBindingHandle,
482 Controller,
483 EFI_OPEN_PROTOCOL_BY_DRIVER
484 );
485
486 if (Status == EFI_ALREADY_STARTED) {
487 return EFI_SUCCESS;
488 }
489
490 if (EFI_ERROR (Status)) {
491 return Status;
492 }
493
494 //
495 // Test RemainingDevicePath is valid or not.
496 //
497 if ((RemainingDevicePath != NULL) && !IsDevicePathEnd (RemainingDevicePath)) {
498 Status = PassThru->GetSlotNumber (PassThru, RemainingDevicePath, &Slot);
499 if (EFI_ERROR (Status)) {
500 //
501 // Close the I/O Abstraction(s) used to perform the supported test
502 //
503 gBS->CloseProtocol (
504 Controller,
505 &gEfiSdMmcPassThruProtocolGuid,
506 This->DriverBindingHandle,
507 Controller
508 );
509 return Status;
510 }
511 }
512
513 //
514 // Close the I/O Abstraction(s) used to perform the supported test
515 //
516 gBS->CloseProtocol (
517 Controller,
518 &gEfiSdMmcPassThruProtocolGuid,
519 This->DriverBindingHandle,
520 Controller
521 );
522
523 //
524 // Open the EFI Device Path protocol needed to perform the supported test
525 //
526 Status = gBS->OpenProtocol (
527 Controller,
528 &gEfiDevicePathProtocolGuid,
529 (VOID **)&ParentDevicePath,
530 This->DriverBindingHandle,
531 Controller,
532 EFI_OPEN_PROTOCOL_GET_PROTOCOL
533 );
534 return Status;
535 }
536
537 /**
538 Starts a device controller or a bus controller.
539
540 The Start() function is designed to be invoked from the EFI boot service ConnectController().
541 As a result, much of the error checking on the parameters to Start() has been moved into this
542 common boot service. It is legal to call Start() from other locations,
543 but the following calling restrictions must be followed or the system behavior will not be deterministic.
544 1. ControllerHandle must be a valid EFI_HANDLE.
545 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
546 EFI_DEVICE_PATH_PROTOCOL.
547 3. Prior to calling Start(), the Supported() function for the driver specified by This must
548 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
549
550 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
551 @param[in] ControllerHandle The handle of the controller to start. This handle
552 must support a protocol interface that supplies
553 an I/O abstraction to the driver.
554 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
555 parameter is ignored by device drivers, and is optional for bus
556 drivers. For a bus driver, if this parameter is NULL, then handles
557 for all the children of Controller are created by this driver.
558 If this parameter is not NULL and the first Device Path Node is
559 not the End of Device Path Node, then only the handle for the
560 child device specified by the first Device Path Node of
561 RemainingDevicePath is created by this driver.
562 If the first Device Path Node of RemainingDevicePath is
563 the End of Device Path Node, no child handle is created by this
564 driver.
565
566 @retval EFI_SUCCESS The device was started.
567 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
568 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
569 @retval Others The driver failed to start the device.
570
571 **/
572 EFI_STATUS
573 EFIAPI
574 SdDxeDriverBindingStart (
575 IN EFI_DRIVER_BINDING_PROTOCOL *This,
576 IN EFI_HANDLE Controller,
577 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
578 )
579 {
580 EFI_STATUS Status;
581 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;
582 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
583 SD_DRIVER_PRIVATE_DATA *Private;
584 UINT8 Slot;
585
586 Private = NULL;
587 PassThru = NULL;
588 Status = gBS->OpenProtocol (
589 Controller,
590 &gEfiSdMmcPassThruProtocolGuid,
591 (VOID **)&PassThru,
592 This->DriverBindingHandle,
593 Controller,
594 EFI_OPEN_PROTOCOL_BY_DRIVER
595 );
596 if ((EFI_ERROR (Status)) && (Status != EFI_ALREADY_STARTED)) {
597 return Status;
598 }
599
600 //
601 // Check EFI_ALREADY_STARTED to reuse the original SD_DRIVER_PRIVATE_DATA.
602 //
603 if (Status != EFI_ALREADY_STARTED) {
604 Private = AllocateZeroPool (sizeof (SD_DRIVER_PRIVATE_DATA));
605 if (Private == NULL) {
606 Status = EFI_OUT_OF_RESOURCES;
607 goto Error;
608 }
609
610 Status = gBS->OpenProtocol (
611 Controller,
612 &gEfiDevicePathProtocolGuid,
613 (VOID **)&ParentDevicePath,
614 This->DriverBindingHandle,
615 Controller,
616 EFI_OPEN_PROTOCOL_GET_PROTOCOL
617 );
618 ASSERT_EFI_ERROR (Status);
619 Private->PassThru = PassThru;
620 Private->Controller = Controller;
621 Private->ParentDevicePath = ParentDevicePath;
622 Private->DriverBindingHandle = This->DriverBindingHandle;
623
624 Status = gBS->InstallProtocolInterface (
625 &Controller,
626 &gEfiCallerIdGuid,
627 EFI_NATIVE_INTERFACE,
628 Private
629 );
630 if (EFI_ERROR (Status)) {
631 goto Error;
632 }
633 } else {
634 Status = gBS->OpenProtocol (
635 Controller,
636 &gEfiCallerIdGuid,
637 (VOID **)&Private,
638 This->DriverBindingHandle,
639 Controller,
640 EFI_OPEN_PROTOCOL_GET_PROTOCOL
641 );
642 if (EFI_ERROR (Status)) {
643 goto Error;
644 }
645 }
646
647 if (RemainingDevicePath == NULL) {
648 Slot = 0xFF;
649 while (TRUE) {
650 Status = PassThru->GetNextSlot (PassThru, &Slot);
651 if (EFI_ERROR (Status)) {
652 //
653 // Cannot find more legal slots.
654 //
655 Status = EFI_SUCCESS;
656 break;
657 }
658
659 Status = DiscoverSdDevice (Private, Slot);
660 if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {
661 break;
662 }
663 }
664 } else if (!IsDevicePathEnd (RemainingDevicePath)) {
665 Status = PassThru->GetSlotNumber (PassThru, RemainingDevicePath, &Slot);
666 if (!EFI_ERROR (Status)) {
667 Status = DiscoverSdDevice (Private, Slot);
668 }
669 }
670
671 Error:
672 if (EFI_ERROR (Status) && (Status != EFI_ALREADY_STARTED)) {
673 gBS->CloseProtocol (
674 Controller,
675 &gEfiSdMmcPassThruProtocolGuid,
676 This->DriverBindingHandle,
677 Controller
678 );
679
680 if (Private != NULL) {
681 gBS->UninstallMultipleProtocolInterfaces (
682 Controller,
683 &gEfiCallerIdGuid,
684 Private,
685 NULL
686 );
687 FreePool (Private);
688 }
689 }
690
691 return Status;
692 }
693
694 /**
695 Stops a device controller or a bus controller.
696
697 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
698 As a result, much of the error checking on the parameters to Stop() has been moved
699 into this common boot service. It is legal to call Stop() from other locations,
700 but the following calling restrictions must be followed or the system behavior will not be deterministic.
701 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
702 same driver's Start() function.
703 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
704 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
705 Start() function, and the Start() function must have called OpenProtocol() on
706 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
707
708 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
709 @param[in] ControllerHandle A handle to the device being stopped. The handle must
710 support a bus specific I/O protocol for the driver
711 to use to stop the device.
712 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
713 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
714 if NumberOfChildren is 0.
715
716 @retval EFI_SUCCESS The device was stopped.
717 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
718
719 **/
720 EFI_STATUS
721 EFIAPI
722 SdDxeDriverBindingStop (
723 IN EFI_DRIVER_BINDING_PROTOCOL *This,
724 IN EFI_HANDLE Controller,
725 IN UINTN NumberOfChildren,
726 IN EFI_HANDLE *ChildHandleBuffer
727 )
728 {
729 EFI_STATUS Status;
730 BOOLEAN AllChildrenStopped;
731 UINTN Index;
732 SD_DRIVER_PRIVATE_DATA *Private;
733 SD_DEVICE *Device;
734 EFI_SD_MMC_PASS_THRU_PROTOCOL *PassThru;
735 EFI_BLOCK_IO2_PROTOCOL *BlockIo2;
736 EFI_BLOCK_IO_PROTOCOL *BlockIo;
737 LIST_ENTRY *Link;
738 LIST_ENTRY *NextLink;
739 SD_REQUEST *Request;
740 EFI_TPL OldTpl;
741
742 if (NumberOfChildren == 0) {
743 Status = gBS->OpenProtocol (
744 Controller,
745 &gEfiCallerIdGuid,
746 (VOID **)&Private,
747 This->DriverBindingHandle,
748 Controller,
749 EFI_OPEN_PROTOCOL_GET_PROTOCOL
750 );
751 if (EFI_ERROR (Status)) {
752 return EFI_DEVICE_ERROR;
753 }
754
755 gBS->UninstallProtocolInterface (
756 Controller,
757 &gEfiCallerIdGuid,
758 Private
759 );
760 gBS->CloseProtocol (
761 Controller,
762 &gEfiSdMmcPassThruProtocolGuid,
763 This->DriverBindingHandle,
764 Controller
765 );
766
767 FreePool (Private);
768
769 return EFI_SUCCESS;
770 }
771
772 AllChildrenStopped = TRUE;
773
774 for (Index = 0; Index < NumberOfChildren; Index++) {
775 BlockIo = NULL;
776 BlockIo2 = NULL;
777 Status = gBS->OpenProtocol (
778 ChildHandleBuffer[Index],
779 &gEfiBlockIoProtocolGuid,
780 (VOID **)&BlockIo,
781 This->DriverBindingHandle,
782 Controller,
783 EFI_OPEN_PROTOCOL_GET_PROTOCOL
784 );
785 if (EFI_ERROR (Status)) {
786 Status = gBS->OpenProtocol (
787 ChildHandleBuffer[Index],
788 &gEfiBlockIo2ProtocolGuid,
789 (VOID **)&BlockIo2,
790 This->DriverBindingHandle,
791 Controller,
792 EFI_OPEN_PROTOCOL_GET_PROTOCOL
793 );
794 if (EFI_ERROR (Status)) {
795 AllChildrenStopped = FALSE;
796 continue;
797 }
798 }
799
800 if (BlockIo != NULL) {
801 Device = SD_DEVICE_DATA_FROM_BLKIO (BlockIo);
802 } else {
803 ASSERT (BlockIo2 != NULL);
804 Device = SD_DEVICE_DATA_FROM_BLKIO2 (BlockIo2);
805 }
806
807 //
808 // Free all on-going async tasks.
809 //
810 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
811 for (Link = GetFirstNode (&Device->Queue);
812 !IsNull (&Device->Queue, Link);
813 Link = NextLink)
814 {
815 NextLink = GetNextNode (&Device->Queue, Link);
816 RemoveEntryList (Link);
817
818 Request = SD_REQUEST_FROM_LINK (Link);
819
820 gBS->CloseEvent (Request->Event);
821 Request->Token->TransactionStatus = EFI_ABORTED;
822
823 if (Request->IsEnd) {
824 gBS->SignalEvent (Request->Token->Event);
825 }
826
827 FreePool (Request);
828 }
829
830 gBS->RestoreTPL (OldTpl);
831
832 //
833 // Close the child handle
834 //
835 Status = gBS->CloseProtocol (
836 Controller,
837 &gEfiSdMmcPassThruProtocolGuid,
838 This->DriverBindingHandle,
839 ChildHandleBuffer[Index]
840 );
841
842 Status = gBS->UninstallMultipleProtocolInterfaces (
843 ChildHandleBuffer[Index],
844 &gEfiDevicePathProtocolGuid,
845 Device->DevicePath,
846 &gEfiBlockIoProtocolGuid,
847 &Device->BlockIo,
848 &gEfiBlockIo2ProtocolGuid,
849 &Device->BlockIo2,
850 &gEfiEraseBlockProtocolGuid,
851 &Device->EraseBlock,
852 &gEfiDiskInfoProtocolGuid,
853 &Device->DiskInfo,
854 NULL
855 );
856 if (EFI_ERROR (Status)) {
857 AllChildrenStopped = FALSE;
858 gBS->OpenProtocol (
859 Controller,
860 &gEfiSdMmcPassThruProtocolGuid,
861 (VOID **)&PassThru,
862 This->DriverBindingHandle,
863 ChildHandleBuffer[Index],
864 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
865 );
866 } else {
867 FreePool (Device->DevicePath);
868 FreeUnicodeStringTable (Device->ControllerNameTable);
869 FreePool (Device);
870 }
871 }
872
873 if (!AllChildrenStopped) {
874 return EFI_DEVICE_ERROR;
875 }
876
877 return EFI_SUCCESS;
878 }
879
880 /**
881 The user Entry Point for module SdDxe. The user code starts with this function.
882
883 @param[in] ImageHandle The firmware allocated handle for the EFI image.
884 @param[in] SystemTable A pointer to the EFI System Table.
885
886 @retval EFI_SUCCESS The entry point is executed successfully.
887 @retval other Some errors occur when executing this entry point.
888
889 **/
890 EFI_STATUS
891 EFIAPI
892 InitializeSdDxe (
893 IN EFI_HANDLE ImageHandle,
894 IN EFI_SYSTEM_TABLE *SystemTable
895 )
896 {
897 EFI_STATUS Status;
898
899 //
900 // Install driver model protocol(s).
901 //
902 Status = EfiLibInstallDriverBindingComponentName2 (
903 ImageHandle,
904 SystemTable,
905 &gSdDxeDriverBinding,
906 ImageHandle,
907 &gSdDxeComponentName,
908 &gSdDxeComponentName2
909 );
910 ASSERT_EFI_ERROR (Status);
911
912 return Status;
913 }