]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Ata/AtaBusDxe/AtaBus.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdeModulePkg / Bus / Ata / AtaBusDxe / AtaBus.c
1 /** @file
2 This file implements protocol interfaces for ATA bus driver.
3
4 This file implements protocol interfaces: Driver Binding protocol,
5 Block IO protocol and DiskInfo protocol.
6
7 Copyright (c) 2009 - 2015, Intel Corporation. All rights reserved.<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
9
10
11 **/
12
13 #include "AtaBus.h"
14
15 //
16 // ATA Bus Driver Binding Protocol Instance
17 //
18 EFI_DRIVER_BINDING_PROTOCOL gAtaBusDriverBinding = {
19 AtaBusDriverBindingSupported,
20 AtaBusDriverBindingStart,
21 AtaBusDriverBindingStop,
22 0x10,
23 NULL,
24 NULL
25 };
26
27 //
28 // Template for ATA Child Device.
29 //
30 ATA_DEVICE gAtaDeviceTemplate = {
31 ATA_DEVICE_SIGNATURE, // Signature
32 NULL, // Handle
33 { // BlockIo
34 EFI_BLOCK_IO_PROTOCOL_REVISION,
35 NULL,
36 AtaBlockIoReset,
37 AtaBlockIoReadBlocks,
38 AtaBlockIoWriteBlocks,
39 AtaBlockIoFlushBlocks
40 },
41 { // BlockIo2
42 NULL,
43 AtaBlockIoResetEx,
44 AtaBlockIoReadBlocksEx,
45 AtaBlockIoWriteBlocksEx,
46 AtaBlockIoFlushBlocksEx
47 },
48 { // BlockMedia
49 0, // MediaId
50 FALSE, // RemovableMedia
51 TRUE, // MediaPresent
52 FALSE, // LogicPartition
53 FALSE, // ReadOnly
54 FALSE, // WritingCache
55 0x200, // BlockSize
56 0, // IoAlign
57 0, // LastBlock
58 0, // LowestAlignedLba
59 1 // LogicalBlocksPerPhysicalBlock
60 },
61 { // DiskInfo
62 EFI_DISK_INFO_IDE_INTERFACE_GUID,
63 AtaDiskInfoInquiry,
64 AtaDiskInfoIdentify,
65 AtaDiskInfoSenseData,
66 AtaDiskInfoWhichIde
67 },
68 NULL, // DevicePath
69 {
70 AtaStorageSecurityReceiveData,
71 AtaStorageSecuritySendData
72 },
73 NULL, // AtaBusDriverData
74 0, // Port
75 0, // PortMultiplierPort
76 { 0, }, // Packet
77 {
78 { 0 },
79 }, // Acb
80 NULL, // Asb
81 FALSE, // UdmaValid
82 FALSE, // Lba48Bit
83 NULL, // IdentifyData
84 NULL, // ControllerNameTable
85 { L'\0', }, // ModelName
86 { NULL, NULL }, // AtaTaskList
87 { NULL, NULL }, // AtaSubTaskList
88 FALSE // Abort
89 };
90
91 /**
92 Allocates an aligned buffer for ATA device.
93
94 This function allocates an aligned buffer for the ATA device to perform
95 ATA pass through operations. The alignment requirement is from ATA pass
96 through interface.
97
98 @param AtaDevice The ATA child device involved for the operation.
99 @param BufferSize The request buffer size.
100
101 @return A pointer to the aligned buffer or NULL if the allocation fails.
102
103 **/
104 VOID *
105 AllocateAlignedBuffer (
106 IN ATA_DEVICE *AtaDevice,
107 IN UINTN BufferSize
108 )
109 {
110 return AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize), AtaDevice->AtaBusDriverData->AtaPassThru->Mode->IoAlign);
111 }
112
113 /**
114 Frees an aligned buffer for ATA device.
115
116 This function frees an aligned buffer for the ATA device to perform
117 ATA pass through operations.
118
119 @param Buffer The aligned buffer to be freed.
120 @param BufferSize The request buffer size.
121
122 **/
123 VOID
124 FreeAlignedBuffer (
125 IN VOID *Buffer,
126 IN UINTN BufferSize
127 )
128 {
129 if (Buffer != NULL) {
130 FreeAlignedPages (Buffer, EFI_SIZE_TO_PAGES (BufferSize));
131 }
132 }
133
134 /**
135 Release all the resources allocated for the ATA device.
136
137 This function releases all the resources allocated for the ATA device.
138
139 @param AtaDevice The ATA child device involved for the operation.
140
141 **/
142 VOID
143 ReleaseAtaResources (
144 IN ATA_DEVICE *AtaDevice
145 )
146 {
147 ATA_BUS_ASYN_SUB_TASK *SubTask;
148 ATA_BUS_ASYN_TASK *AtaTask;
149 LIST_ENTRY *Entry;
150 LIST_ENTRY *DelEntry;
151 EFI_TPL OldTpl;
152
153 FreeUnicodeStringTable (AtaDevice->ControllerNameTable);
154 FreeAlignedBuffer (AtaDevice->Asb, sizeof (EFI_ATA_STATUS_BLOCK));
155 FreeAlignedBuffer (AtaDevice->IdentifyData, sizeof (ATA_IDENTIFY_DATA));
156 if (AtaDevice->DevicePath != NULL) {
157 FreePool (AtaDevice->DevicePath);
158 }
159
160 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
161 if (!IsListEmpty (&AtaDevice->AtaSubTaskList)) {
162 //
163 // Free the Subtask list.
164 //
165 for (Entry = AtaDevice->AtaSubTaskList.ForwardLink;
166 Entry != (&AtaDevice->AtaSubTaskList);
167 )
168 {
169 DelEntry = Entry;
170 Entry = Entry->ForwardLink;
171 SubTask = ATA_ASYN_SUB_TASK_FROM_ENTRY (DelEntry);
172
173 RemoveEntryList (DelEntry);
174 FreeAtaSubTask (SubTask);
175 }
176 }
177
178 if (!IsListEmpty (&AtaDevice->AtaTaskList)) {
179 //
180 // Free the Subtask list.
181 //
182 for (Entry = AtaDevice->AtaTaskList.ForwardLink;
183 Entry != (&AtaDevice->AtaTaskList);
184 )
185 {
186 DelEntry = Entry;
187 Entry = Entry->ForwardLink;
188 AtaTask = ATA_ASYN_TASK_FROM_ENTRY (DelEntry);
189
190 RemoveEntryList (DelEntry);
191 FreePool (AtaTask);
192 }
193 }
194
195 gBS->RestoreTPL (OldTpl);
196 FreePool (AtaDevice);
197 }
198
199 /**
200 Registers an ATA device.
201
202 This function allocates an ATA device structure for the ATA device specified by
203 Port and PortMultiplierPort if the ATA device is identified as a valid one.
204 Then it will create child handle and install Block IO and Disk Info protocol on
205 it.
206
207 @param AtaBusDriverData The parent ATA bus driver data structure.
208 @param Port The port number of the ATA device.
209 @param PortMultiplierPort The port multiplier port number of the ATA device.
210
211 @retval EFI_SUCCESS The ATA device is successfully registered.
212 @retval EFI_OUT_OF_RESOURCES There is not enough memory to allocate the ATA device
213 and related data structures.
214 @return Others Some error occurs when registering the ATA device.
215 **/
216 EFI_STATUS
217 RegisterAtaDevice (
218 IN OUT ATA_BUS_DRIVER_DATA *AtaBusDriverData,
219 IN UINT16 Port,
220 IN UINT16 PortMultiplierPort
221 )
222 {
223 EFI_STATUS Status;
224 ATA_DEVICE *AtaDevice;
225 EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;
226 EFI_DEVICE_PATH_PROTOCOL *NewDevicePathNode;
227 EFI_DEVICE_PATH_PROTOCOL *DevicePath;
228 EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath;
229 EFI_HANDLE DeviceHandle;
230
231 AtaDevice = NULL;
232 NewDevicePathNode = NULL;
233 DevicePath = NULL;
234 RemainingDevicePath = NULL;
235
236 //
237 // Build device path
238 //
239 AtaPassThru = AtaBusDriverData->AtaPassThru;
240 Status = AtaPassThru->BuildDevicePath (AtaPassThru, Port, PortMultiplierPort, &NewDevicePathNode);
241 if (EFI_ERROR (Status)) {
242 goto Done;
243 }
244
245 DevicePath = AppendDevicePathNode (AtaBusDriverData->ParentDevicePath, NewDevicePathNode);
246 if (DevicePath == NULL) {
247 Status = EFI_OUT_OF_RESOURCES;
248 goto Done;
249 }
250
251 DeviceHandle = NULL;
252 RemainingDevicePath = DevicePath;
253 Status = gBS->LocateDevicePath (&gEfiDevicePathProtocolGuid, &RemainingDevicePath, &DeviceHandle);
254 if (!EFI_ERROR (Status) && (DeviceHandle != NULL) && IsDevicePathEnd (RemainingDevicePath)) {
255 Status = EFI_ALREADY_STARTED;
256 FreePool (DevicePath);
257 goto Done;
258 }
259
260 //
261 // Allocate ATA device from the template.
262 //
263 AtaDevice = AllocateCopyPool (sizeof (ATA_DEVICE), &gAtaDeviceTemplate);
264 if (AtaDevice == NULL) {
265 Status = EFI_OUT_OF_RESOURCES;
266 goto Done;
267 }
268
269 //
270 // Initializes ATA device structures and allocates the required buffer.
271 //
272 AtaDevice->BlockIo.Media = &AtaDevice->BlockMedia;
273 AtaDevice->BlockIo2.Media = &AtaDevice->BlockMedia;
274 AtaDevice->AtaBusDriverData = AtaBusDriverData;
275 AtaDevice->DevicePath = DevicePath;
276 AtaDevice->Port = Port;
277 AtaDevice->PortMultiplierPort = PortMultiplierPort;
278 AtaDevice->Asb = AllocateAlignedBuffer (AtaDevice, sizeof (EFI_ATA_STATUS_BLOCK));
279 if (AtaDevice->Asb == NULL) {
280 Status = EFI_OUT_OF_RESOURCES;
281 goto Done;
282 }
283
284 AtaDevice->IdentifyData = AllocateAlignedBuffer (AtaDevice, sizeof (ATA_IDENTIFY_DATA));
285 if (AtaDevice->IdentifyData == NULL) {
286 Status = EFI_OUT_OF_RESOURCES;
287 goto Done;
288 }
289
290 //
291 // Initial Ata Task List
292 //
293 InitializeListHead (&AtaDevice->AtaTaskList);
294 InitializeListHead (&AtaDevice->AtaSubTaskList);
295
296 //
297 // Report Status Code to indicate the ATA device will be enabled
298 //
299 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
300 EFI_PROGRESS_CODE,
301 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_PC_ENABLE),
302 AtaBusDriverData->ParentDevicePath
303 );
304
305 //
306 // Try to identify the ATA device via the ATA pass through command.
307 //
308 Status = DiscoverAtaDevice (AtaDevice);
309 if (EFI_ERROR (Status)) {
310 goto Done;
311 }
312
313 //
314 // Build controller name for Component Name (2) protocol.
315 //
316 Status = AddUnicodeString2 (
317 "eng",
318 gAtaBusComponentName.SupportedLanguages,
319 &AtaDevice->ControllerNameTable,
320 AtaDevice->ModelName,
321 TRUE
322 );
323 if (EFI_ERROR (Status)) {
324 goto Done;
325 }
326
327 Status = AddUnicodeString2 (
328 "en",
329 gAtaBusComponentName2.SupportedLanguages,
330 &AtaDevice->ControllerNameTable,
331 AtaDevice->ModelName,
332 FALSE
333 );
334 if (EFI_ERROR (Status)) {
335 goto Done;
336 }
337
338 //
339 // Update to AHCI interface GUID based on device path node. The default one
340 // is IDE interface GUID copied from template.
341 //
342 if (NewDevicePathNode->SubType == MSG_SATA_DP) {
343 CopyGuid (&AtaDevice->DiskInfo.Interface, &gEfiDiskInfoAhciInterfaceGuid);
344 }
345
346 Status = gBS->InstallMultipleProtocolInterfaces (
347 &AtaDevice->Handle,
348 &gEfiDevicePathProtocolGuid,
349 AtaDevice->DevicePath,
350 &gEfiBlockIoProtocolGuid,
351 &AtaDevice->BlockIo,
352 &gEfiBlockIo2ProtocolGuid,
353 &AtaDevice->BlockIo2,
354 &gEfiDiskInfoProtocolGuid,
355 &AtaDevice->DiskInfo,
356 NULL
357 );
358 if (EFI_ERROR (Status)) {
359 goto Done;
360 }
361
362 //
363 // See if the ata device support trust computing feature or not.
364 // If yes, then install Storage Security Protocol at the ata device handle.
365 //
366 if ((AtaDevice->IdentifyData->trusted_computing_support & BIT0) != 0) {
367 DEBUG ((DEBUG_INFO, "Found TCG support in Port %x PortMultiplierPort %x\n", Port, PortMultiplierPort));
368 Status = gBS->InstallProtocolInterface (
369 &AtaDevice->Handle,
370 &gEfiStorageSecurityCommandProtocolGuid,
371 EFI_NATIVE_INTERFACE,
372 &AtaDevice->StorageSecurity
373 );
374 if (EFI_ERROR (Status)) {
375 goto Done;
376 }
377
378 DEBUG ((DEBUG_INFO, "Successfully Install Storage Security Protocol on the ATA device\n"));
379 }
380
381 gBS->OpenProtocol (
382 AtaBusDriverData->Controller,
383 &gEfiAtaPassThruProtocolGuid,
384 (VOID **)&AtaPassThru,
385 AtaBusDriverData->DriverBindingHandle,
386 AtaDevice->Handle,
387 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
388 );
389
390 Done:
391 if (NewDevicePathNode != NULL) {
392 FreePool (NewDevicePathNode);
393 }
394
395 if (EFI_ERROR (Status) && (AtaDevice != NULL)) {
396 ReleaseAtaResources (AtaDevice);
397 DEBUG ((DEBUG_ERROR | DEBUG_INIT, "Failed to initialize Port %x PortMultiplierPort %x, status = %r\n", Port, PortMultiplierPort, Status));
398 }
399
400 return Status;
401 }
402
403 /**
404 Unregisters an ATA device.
405
406 This function removes the protocols installed on the controller handle and
407 frees the resources allocated for the ATA device.
408
409 @param This The pointer to EFI_DRIVER_BINDING_PROTOCOL instance.
410 @param Controller The controller handle of the ATA device.
411 @param Handle The child handle.
412
413 @retval EFI_SUCCESS The ATA device is successfully unregistered.
414 @return Others Some error occurs when unregistering the ATA device.
415
416 **/
417 EFI_STATUS
418 UnregisterAtaDevice (
419 IN EFI_DRIVER_BINDING_PROTOCOL *This,
420 IN EFI_HANDLE Controller,
421 IN EFI_HANDLE Handle
422 )
423 {
424 EFI_STATUS Status;
425 EFI_BLOCK_IO_PROTOCOL *BlockIo;
426 EFI_BLOCK_IO2_PROTOCOL *BlockIo2;
427 ATA_DEVICE *AtaDevice;
428 EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;
429 EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *StorageSecurity;
430
431 BlockIo2 = NULL;
432 BlockIo = NULL;
433
434 Status = gBS->OpenProtocol (
435 Handle,
436 &gEfiBlockIoProtocolGuid,
437 (VOID **)&BlockIo,
438 This->DriverBindingHandle,
439 Controller,
440 EFI_OPEN_PROTOCOL_GET_PROTOCOL
441 );
442 if (EFI_ERROR (Status)) {
443 //
444 // Locate BlockIo2 protocol
445 //
446 Status = gBS->OpenProtocol (
447 Handle,
448 &gEfiBlockIo2ProtocolGuid,
449 (VOID **)&BlockIo2,
450 This->DriverBindingHandle,
451 Controller,
452 EFI_OPEN_PROTOCOL_GET_PROTOCOL
453 );
454 if (EFI_ERROR (Status)) {
455 return Status;
456 }
457 }
458
459 //
460 // Get AtaDevice data.
461 //
462 if (BlockIo != NULL) {
463 AtaDevice = ATA_DEVICE_FROM_BLOCK_IO (BlockIo);
464 } else {
465 ASSERT (BlockIo2 != NULL);
466 AtaDevice = ATA_DEVICE_FROM_BLOCK_IO2 (BlockIo2);
467 }
468
469 //
470 // Close the child handle
471 //
472 gBS->CloseProtocol (
473 Controller,
474 &gEfiAtaPassThruProtocolGuid,
475 This->DriverBindingHandle,
476 Handle
477 );
478
479 //
480 // The Ata Bus driver installs the BlockIo and BlockIo2 in the DriverBindingStart().
481 // Here should uninstall both of them.
482 //
483 Status = gBS->UninstallMultipleProtocolInterfaces (
484 Handle,
485 &gEfiDevicePathProtocolGuid,
486 AtaDevice->DevicePath,
487 &gEfiBlockIoProtocolGuid,
488 &AtaDevice->BlockIo,
489 &gEfiBlockIo2ProtocolGuid,
490 &AtaDevice->BlockIo2,
491 &gEfiDiskInfoProtocolGuid,
492 &AtaDevice->DiskInfo,
493 NULL
494 );
495
496 if (EFI_ERROR (Status)) {
497 gBS->OpenProtocol (
498 Controller,
499 &gEfiAtaPassThruProtocolGuid,
500 (VOID **)&AtaPassThru,
501 This->DriverBindingHandle,
502 Handle,
503 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
504 );
505 return Status;
506 }
507
508 //
509 // If Storage Security Command Protocol is installed, then uninstall this protocol.
510 //
511 Status = gBS->OpenProtocol (
512 Handle,
513 &gEfiStorageSecurityCommandProtocolGuid,
514 (VOID **)&StorageSecurity,
515 This->DriverBindingHandle,
516 Controller,
517 EFI_OPEN_PROTOCOL_GET_PROTOCOL
518 );
519
520 if (!EFI_ERROR (Status)) {
521 Status = gBS->UninstallProtocolInterface (
522 Handle,
523 &gEfiStorageSecurityCommandProtocolGuid,
524 &AtaDevice->StorageSecurity
525 );
526 if (EFI_ERROR (Status)) {
527 gBS->OpenProtocol (
528 Controller,
529 &gEfiAtaPassThruProtocolGuid,
530 (VOID **)&AtaPassThru,
531 This->DriverBindingHandle,
532 Handle,
533 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
534 );
535 return Status;
536 }
537 }
538
539 ReleaseAtaResources (AtaDevice);
540 return EFI_SUCCESS;
541 }
542
543 /**
544 Tests to see if this driver supports a given controller. If a child device is provided,
545 it further tests to see if this driver supports creating a handle for the specified child device.
546
547 This function checks to see if the driver specified by This supports the device specified by
548 ControllerHandle. Drivers will typically use the device path attached to
549 ControllerHandle and/or the services from the bus I/O abstraction attached to
550 ControllerHandle to determine if the driver supports ControllerHandle. This function
551 may be called many times during platform initialization. In order to reduce boot times, the tests
552 performed by this function must be very small, and take as little time as possible to execute. This
553 function must not change the state of any hardware devices, and this function must be aware that the
554 device specified by ControllerHandle may already be managed by the same driver or a
555 different driver. This function must match its calls to AllocatePages() with FreePages(),
556 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
557 Since ControllerHandle may have been previously started by the same driver, if a protocol is
558 already in the opened state, then it must not be closed with CloseProtocol(). This is required
559 to guarantee the state of ControllerHandle is not modified by this function.
560
561 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
562 @param[in] ControllerHandle The handle of the controller to test. This handle
563 must support a protocol interface that supplies
564 an I/O abstraction to the driver.
565 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
566 parameter is ignored by device drivers, and is optional for bus
567 drivers. For bus drivers, if this parameter is not NULL, then
568 the bus driver must determine if the bus controller specified
569 by ControllerHandle and the child controller specified
570 by RemainingDevicePath are both supported by this
571 bus driver.
572
573 @retval EFI_SUCCESS The device specified by ControllerHandle and
574 RemainingDevicePath is supported by the driver specified by This.
575 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
576 RemainingDevicePath is already being managed by the driver
577 specified by This.
578 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
579 RemainingDevicePath is already being managed by a different
580 driver or an application that requires exclusive access.
581 Currently not implemented.
582 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
583 RemainingDevicePath is not supported by the driver specified by This.
584 **/
585 EFI_STATUS
586 EFIAPI
587 AtaBusDriverBindingSupported (
588 IN EFI_DRIVER_BINDING_PROTOCOL *This,
589 IN EFI_HANDLE Controller,
590 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
591 )
592 {
593 EFI_STATUS Status;
594 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
595 EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;
596 UINT16 Port;
597 UINT16 PortMultiplierPort;
598
599 //
600 // Test EFI_ATA_PASS_THRU_PROTOCOL on controller handle.
601 //
602 Status = gBS->OpenProtocol (
603 Controller,
604 &gEfiAtaPassThruProtocolGuid,
605 (VOID **)&AtaPassThru,
606 This->DriverBindingHandle,
607 Controller,
608 EFI_OPEN_PROTOCOL_BY_DRIVER
609 );
610
611 if (Status == EFI_ALREADY_STARTED) {
612 return EFI_SUCCESS;
613 }
614
615 if (EFI_ERROR (Status)) {
616 return Status;
617 }
618
619 //
620 // Test to see if this ATA Pass Thru Protocol is for a LOGICAL channel
621 //
622 if ((AtaPassThru->Mode->Attributes & EFI_ATA_PASS_THRU_ATTRIBUTES_LOGICAL) == 0) {
623 //
624 // Close the I/O Abstraction(s) used to perform the supported test
625 //
626 gBS->CloseProtocol (
627 Controller,
628 &gEfiAtaPassThruProtocolGuid,
629 This->DriverBindingHandle,
630 Controller
631 );
632 return EFI_UNSUPPORTED;
633 }
634
635 //
636 // Test RemainingDevicePath is valid or not.
637 //
638 if ((RemainingDevicePath != NULL) && !IsDevicePathEnd (RemainingDevicePath)) {
639 Status = AtaPassThru->GetDevice (AtaPassThru, RemainingDevicePath, &Port, &PortMultiplierPort);
640 if (EFI_ERROR (Status)) {
641 //
642 // Close the I/O Abstraction(s) used to perform the supported test
643 //
644 gBS->CloseProtocol (
645 Controller,
646 &gEfiAtaPassThruProtocolGuid,
647 This->DriverBindingHandle,
648 Controller
649 );
650 return Status;
651 }
652 }
653
654 //
655 // Close the I/O Abstraction(s) used to perform the supported test
656 //
657 gBS->CloseProtocol (
658 Controller,
659 &gEfiAtaPassThruProtocolGuid,
660 This->DriverBindingHandle,
661 Controller
662 );
663
664 //
665 // Open the EFI Device Path protocol needed to perform the supported test
666 //
667 Status = gBS->OpenProtocol (
668 Controller,
669 &gEfiDevicePathProtocolGuid,
670 (VOID **)&ParentDevicePath,
671 This->DriverBindingHandle,
672 Controller,
673 EFI_OPEN_PROTOCOL_GET_PROTOCOL
674 );
675 return Status;
676 }
677
678 /**
679 Starts a device controller or a bus controller.
680
681 The Start() function is designed to be invoked from the EFI boot service ConnectController().
682 As a result, much of the error checking on the parameters to Start() has been moved into this
683 common boot service. It is legal to call Start() from other locations,
684 but the following calling restrictions must be followed or the system behavior will not be deterministic.
685 1. ControllerHandle must be a valid EFI_HANDLE.
686 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
687 EFI_DEVICE_PATH_PROTOCOL.
688 3. Prior to calling Start(), the Supported() function for the driver specified by This must
689 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
690
691 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
692 @param[in] ControllerHandle The handle of the controller to start. This handle
693 must support a protocol interface that supplies
694 an I/O abstraction to the driver.
695 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
696 parameter is ignored by device drivers, and is optional for bus
697 drivers. For a bus driver, if this parameter is NULL, then handles
698 for all the children of Controller are created by this driver.
699 If this parameter is not NULL and the first Device Path Node is
700 not the End of Device Path Node, then only the handle for the
701 child device specified by the first Device Path Node of
702 RemainingDevicePath is created by this driver.
703 If the first Device Path Node of RemainingDevicePath is
704 the End of Device Path Node, no child handle is created by this
705 driver.
706
707 @retval EFI_SUCCESS The device was started.
708 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
709 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
710 @retval Others The driver failed to start the device.
711
712 **/
713 EFI_STATUS
714 EFIAPI
715 AtaBusDriverBindingStart (
716 IN EFI_DRIVER_BINDING_PROTOCOL *This,
717 IN EFI_HANDLE Controller,
718 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
719 )
720 {
721 EFI_STATUS Status;
722 EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;
723 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
724 ATA_BUS_DRIVER_DATA *AtaBusDriverData;
725 UINT16 Port;
726 UINT16 PortMultiplierPort;
727
728 AtaBusDriverData = NULL;
729
730 Status = gBS->OpenProtocol (
731 Controller,
732 &gEfiDevicePathProtocolGuid,
733 (VOID **)&ParentDevicePath,
734 This->DriverBindingHandle,
735 Controller,
736 EFI_OPEN_PROTOCOL_GET_PROTOCOL
737 );
738 if (EFI_ERROR (Status)) {
739 return Status;
740 }
741
742 //
743 // Report Status Code to indicate ATA bus starts
744 //
745 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
746 EFI_PROGRESS_CODE,
747 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_PC_INIT),
748 ParentDevicePath
749 );
750
751 Status = gBS->OpenProtocol (
752 Controller,
753 &gEfiAtaPassThruProtocolGuid,
754 (VOID **)&AtaPassThru,
755 This->DriverBindingHandle,
756 Controller,
757 EFI_OPEN_PROTOCOL_BY_DRIVER
758 );
759 if ((EFI_ERROR (Status)) && (Status != EFI_ALREADY_STARTED)) {
760 goto ErrorExit;
761 }
762
763 //
764 // Check EFI_ALREADY_STARTED to reuse the original ATA_BUS_DRIVER_DATA.
765 //
766 if (Status != EFI_ALREADY_STARTED) {
767 AtaBusDriverData = AllocateZeroPool (sizeof (ATA_BUS_DRIVER_DATA));
768 if (AtaBusDriverData == NULL) {
769 Status = EFI_OUT_OF_RESOURCES;
770 goto ErrorExit;
771 }
772
773 AtaBusDriverData->AtaPassThru = AtaPassThru;
774 AtaBusDriverData->Controller = Controller;
775 AtaBusDriverData->ParentDevicePath = ParentDevicePath;
776 AtaBusDriverData->DriverBindingHandle = This->DriverBindingHandle;
777
778 Status = gBS->InstallMultipleProtocolInterfaces (
779 &Controller,
780 &gEfiCallerIdGuid,
781 AtaBusDriverData,
782 NULL
783 );
784 if (EFI_ERROR (Status)) {
785 goto ErrorExit;
786 }
787 } else {
788 Status = gBS->OpenProtocol (
789 Controller,
790 &gEfiCallerIdGuid,
791 (VOID **)&AtaBusDriverData,
792 This->DriverBindingHandle,
793 Controller,
794 EFI_OPEN_PROTOCOL_GET_PROTOCOL
795 );
796 if (EFI_ERROR (Status)) {
797 AtaBusDriverData = NULL;
798 goto ErrorExit;
799 }
800 }
801
802 //
803 // Report Status Code to indicate detecting devices on bus
804 //
805 REPORT_STATUS_CODE_WITH_DEVICE_PATH (
806 EFI_PROGRESS_CODE,
807 (EFI_IO_BUS_ATA_ATAPI | EFI_IOB_PC_DETECT),
808 ParentDevicePath
809 );
810
811 if (RemainingDevicePath == NULL) {
812 Port = 0xFFFF;
813 while (TRUE) {
814 Status = AtaPassThru->GetNextPort (AtaPassThru, &Port);
815 if (EFI_ERROR (Status)) {
816 //
817 // We cannot find more legal port then we are done.
818 //
819 break;
820 }
821
822 PortMultiplierPort = 0xFFFF;
823 while (TRUE) {
824 Status = AtaPassThru->GetNextDevice (AtaPassThru, Port, &PortMultiplierPort);
825 if (EFI_ERROR (Status)) {
826 //
827 // We cannot find more legal port multiplier port number for ATA device
828 // on the port, then we are done.
829 //
830 break;
831 }
832
833 RegisterAtaDevice (AtaBusDriverData, Port, PortMultiplierPort);
834 }
835 }
836
837 Status = EFI_SUCCESS;
838 } else if (!IsDevicePathEnd (RemainingDevicePath)) {
839 Status = AtaPassThru->GetDevice (AtaPassThru, RemainingDevicePath, &Port, &PortMultiplierPort);
840 if (!EFI_ERROR (Status)) {
841 Status = RegisterAtaDevice (AtaBusDriverData, Port, PortMultiplierPort);
842 }
843 }
844
845 return Status;
846
847 ErrorExit:
848
849 if (AtaBusDriverData != NULL) {
850 gBS->UninstallMultipleProtocolInterfaces (
851 Controller,
852 &gEfiCallerIdGuid,
853 AtaBusDriverData,
854 NULL
855 );
856 FreePool (AtaBusDriverData);
857 }
858
859 gBS->CloseProtocol (
860 Controller,
861 &gEfiAtaPassThruProtocolGuid,
862 This->DriverBindingHandle,
863 Controller
864 );
865
866 return Status;
867 }
868
869 /**
870 Stops a device controller or a bus controller.
871
872 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
873 As a result, much of the error checking on the parameters to Stop() has been moved
874 into this common boot service. It is legal to call Stop() from other locations,
875 but the following calling restrictions must be followed or the system behavior will not be deterministic.
876 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
877 same driver's Start() function.
878 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
879 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
880 Start() function, and the Start() function must have called OpenProtocol() on
881 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
882
883 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
884 @param[in] ControllerHandle A handle to the device being stopped. The handle must
885 support a bus specific I/O protocol for the driver
886 to use to stop the device.
887 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
888 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
889 if NumberOfChildren is 0.
890
891 @retval EFI_SUCCESS The device was stopped.
892 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
893
894 **/
895 EFI_STATUS
896 EFIAPI
897 AtaBusDriverBindingStop (
898 IN EFI_DRIVER_BINDING_PROTOCOL *This,
899 IN EFI_HANDLE Controller,
900 IN UINTN NumberOfChildren,
901 IN EFI_HANDLE *ChildHandleBuffer
902 )
903 {
904 EFI_STATUS Status;
905 BOOLEAN AllChildrenStopped;
906 UINTN Index;
907 ATA_BUS_DRIVER_DATA *AtaBusDriverData;
908
909 if (NumberOfChildren == 0) {
910 Status = gBS->OpenProtocol (
911 Controller,
912 &gEfiCallerIdGuid,
913 (VOID **)&AtaBusDriverData,
914 This->DriverBindingHandle,
915 Controller,
916 EFI_OPEN_PROTOCOL_GET_PROTOCOL
917 );
918 if (!EFI_ERROR (Status)) {
919 gBS->UninstallMultipleProtocolInterfaces (
920 Controller,
921 &gEfiCallerIdGuid,
922 AtaBusDriverData,
923 NULL
924 );
925 FreePool (AtaBusDriverData);
926 }
927
928 gBS->CloseProtocol (
929 Controller,
930 &gEfiAtaPassThruProtocolGuid,
931 This->DriverBindingHandle,
932 Controller
933 );
934
935 return EFI_SUCCESS;
936 }
937
938 AllChildrenStopped = TRUE;
939
940 for (Index = 0; Index < NumberOfChildren; Index++) {
941 Status = UnregisterAtaDevice (This, Controller, ChildHandleBuffer[Index]);
942 if (EFI_ERROR (Status)) {
943 AllChildrenStopped = FALSE;
944 }
945 }
946
947 if (!AllChildrenStopped) {
948 return EFI_DEVICE_ERROR;
949 }
950
951 return EFI_SUCCESS;
952 }
953
954 /**
955 Reset the Block Device.
956
957 @param This Indicates a pointer to the calling context.
958 @param ExtendedVerification Driver may perform diagnostics on reset.
959
960 @retval EFI_SUCCESS The device was reset.
961 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
962 not be reset.
963
964 **/
965 EFI_STATUS
966 EFIAPI
967 AtaBlockIoReset (
968 IN EFI_BLOCK_IO_PROTOCOL *This,
969 IN BOOLEAN ExtendedVerification
970 )
971 {
972 EFI_STATUS Status;
973 ATA_DEVICE *AtaDevice;
974 EFI_TPL OldTpl;
975
976 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
977
978 AtaDevice = ATA_DEVICE_FROM_BLOCK_IO (This);
979
980 Status = ResetAtaDevice (AtaDevice);
981
982 if (EFI_ERROR (Status)) {
983 Status = EFI_DEVICE_ERROR;
984 }
985
986 gBS->RestoreTPL (OldTpl);
987 return Status;
988 }
989
990 /**
991 Read/Write BufferSize bytes from Lba from/into Buffer.
992
993 @param[in] This Indicates a pointer to the calling context. Either be
994 block I/O or block I/O2.
995 @param[in] MediaId The media ID that the read/write request is for.
996 @param[in] Lba The starting logical block address to be read/written.
997 The caller is responsible for reading/writing to only
998 legitimate locations.
999 @param[in, out] Token A pointer to the token associated with the transaction.
1000 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
1001 @param[out] Buffer A pointer to the destination/source buffer for the data.
1002 @param[in] IsBlockIo2 Indicate the calling is from BlockIO or BlockIO2. TRUE is
1003 from BlockIO2, FALSE is for BlockIO.
1004 @param[in] IsWrite Indicates whether it is a write operation.
1005
1006 @retval EFI_SUCCESS The data was read/written correctly to the device.
1007 @retval EFI_WRITE_PROTECTED The device can not be read/written to.
1008 @retval EFI_DEVICE_ERROR The device reported an error while performing the read/write.
1009 @retval EFI_NO_MEDIA There is no media in the device.
1010 @retval EFI_MEDIA_CHANGED The MediaId does not match the current device.
1011 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
1012 @retval EFI_INVALID_PARAMETER The read/write request contains LBAs that are not valid,
1013 or the buffer is not on proper alignment.
1014
1015 **/
1016 EFI_STATUS
1017 BlockIoReadWrite (
1018 IN VOID *This,
1019 IN UINT32 MediaId,
1020 IN EFI_LBA Lba,
1021 IN OUT EFI_BLOCK_IO2_TOKEN *Token,
1022 IN UINTN BufferSize,
1023 OUT VOID *Buffer,
1024 IN BOOLEAN IsBlockIo2,
1025 IN BOOLEAN IsWrite
1026 )
1027 {
1028 ATA_DEVICE *AtaDevice;
1029 EFI_STATUS Status;
1030 EFI_TPL OldTpl;
1031 EFI_BLOCK_IO_MEDIA *Media;
1032 UINTN BlockSize;
1033 UINTN NumberOfBlocks;
1034 UINTN IoAlign;
1035
1036 if (IsBlockIo2) {
1037 Media = ((EFI_BLOCK_IO2_PROTOCOL *)This)->Media;
1038 AtaDevice = ATA_DEVICE_FROM_BLOCK_IO2 (This);
1039 } else {
1040 Media = ((EFI_BLOCK_IO_PROTOCOL *)This)->Media;
1041 AtaDevice = ATA_DEVICE_FROM_BLOCK_IO (This);
1042 }
1043
1044 if (MediaId != Media->MediaId) {
1045 return EFI_MEDIA_CHANGED;
1046 }
1047
1048 //
1049 // Check parameters.
1050 //
1051 if (Buffer == NULL) {
1052 return EFI_INVALID_PARAMETER;
1053 }
1054
1055 if (BufferSize == 0) {
1056 if ((Token != NULL) && (Token->Event != NULL)) {
1057 Token->TransactionStatus = EFI_SUCCESS;
1058 gBS->SignalEvent (Token->Event);
1059 }
1060
1061 return EFI_SUCCESS;
1062 }
1063
1064 BlockSize = Media->BlockSize;
1065 if ((BufferSize % BlockSize) != 0) {
1066 return EFI_BAD_BUFFER_SIZE;
1067 }
1068
1069 NumberOfBlocks = BufferSize / BlockSize;
1070 if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {
1071 return EFI_INVALID_PARAMETER;
1072 }
1073
1074 IoAlign = Media->IoAlign;
1075 if ((IoAlign > 0) && (((UINTN)Buffer & (IoAlign - 1)) != 0)) {
1076 return EFI_INVALID_PARAMETER;
1077 }
1078
1079 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
1080
1081 //
1082 // Invoke low level AtaDevice Access Routine.
1083 //
1084 Status = AccessAtaDevice (AtaDevice, Buffer, Lba, NumberOfBlocks, IsWrite, Token);
1085
1086 gBS->RestoreTPL (OldTpl);
1087
1088 return Status;
1089 }
1090
1091 /**
1092 Read BufferSize bytes from Lba into Buffer.
1093
1094 @param This Indicates a pointer to the calling context.
1095 @param MediaId Id of the media, changes every time the media is replaced.
1096 @param Lba The starting Logical Block Address to read from
1097 @param BufferSize Size of Buffer, must be a multiple of device block size.
1098 @param Buffer A pointer to the destination buffer for the data. The caller is
1099 responsible for either having implicit or explicit ownership of the buffer.
1100
1101 @retval EFI_SUCCESS The data was read correctly from the device.
1102 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
1103 @retval EFI_NO_MEDIA There is no media in the device.
1104 @retval EFI_MEDIA_CHANGED The MediaId does not match the current device.
1105 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
1106 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
1107 or the buffer is not on proper alignment.
1108
1109 **/
1110 EFI_STATUS
1111 EFIAPI
1112 AtaBlockIoReadBlocks (
1113 IN EFI_BLOCK_IO_PROTOCOL *This,
1114 IN UINT32 MediaId,
1115 IN EFI_LBA Lba,
1116 IN UINTN BufferSize,
1117 OUT VOID *Buffer
1118 )
1119 {
1120 return BlockIoReadWrite ((VOID *)This, MediaId, Lba, NULL, BufferSize, Buffer, FALSE, FALSE);
1121 }
1122
1123 /**
1124 Write BufferSize bytes from Lba into Buffer.
1125
1126 @param This Indicates a pointer to the calling context.
1127 @param MediaId The media ID that the write request is for.
1128 @param Lba The starting logical block address to be written. The caller is
1129 responsible for writing to only legitimate locations.
1130 @param BufferSize Size of Buffer, must be a multiple of device block size.
1131 @param Buffer A pointer to the source buffer for the data.
1132
1133 @retval EFI_SUCCESS The data was written correctly to the device.
1134 @retval EFI_WRITE_PROTECTED The device can not be written to.
1135 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
1136 @retval EFI_NO_MEDIA There is no media in the device.
1137 @retval EFI_MEDIA_CHANGED The MediaId does not match the current device.
1138 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
1139 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
1140 or the buffer is not on proper alignment.
1141
1142 **/
1143 EFI_STATUS
1144 EFIAPI
1145 AtaBlockIoWriteBlocks (
1146 IN EFI_BLOCK_IO_PROTOCOL *This,
1147 IN UINT32 MediaId,
1148 IN EFI_LBA Lba,
1149 IN UINTN BufferSize,
1150 IN VOID *Buffer
1151 )
1152 {
1153 return BlockIoReadWrite ((VOID *)This, MediaId, Lba, NULL, BufferSize, Buffer, FALSE, TRUE);
1154 }
1155
1156 /**
1157 Flush the Block Device.
1158
1159 @param This Indicates a pointer to the calling context.
1160
1161 @retval EFI_SUCCESS All outstanding data was written to the device
1162 @retval EFI_DEVICE_ERROR The device reported an error while writing back the data
1163 @retval EFI_NO_MEDIA There is no media in the device.
1164
1165 **/
1166 EFI_STATUS
1167 EFIAPI
1168 AtaBlockIoFlushBlocks (
1169 IN EFI_BLOCK_IO_PROTOCOL *This
1170 )
1171 {
1172 //
1173 // return directly
1174 //
1175 return EFI_SUCCESS;
1176 }
1177
1178 /**
1179 Reset the Block Device.
1180
1181 @param[in] This Indicates a pointer to the calling context.
1182 @param[in] ExtendedVerification Driver may perform diagnostics on reset.
1183
1184 @retval EFI_SUCCESS The device was reset.
1185 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
1186 not be reset.
1187
1188 **/
1189 EFI_STATUS
1190 EFIAPI
1191 AtaBlockIoResetEx (
1192 IN EFI_BLOCK_IO2_PROTOCOL *This,
1193 IN BOOLEAN ExtendedVerification
1194 )
1195 {
1196 EFI_STATUS Status;
1197 ATA_DEVICE *AtaDevice;
1198 EFI_TPL OldTpl;
1199
1200 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
1201
1202 AtaDevice = ATA_DEVICE_FROM_BLOCK_IO2 (This);
1203
1204 AtaTerminateNonBlockingTask (AtaDevice);
1205
1206 Status = ResetAtaDevice (AtaDevice);
1207
1208 if (EFI_ERROR (Status)) {
1209 Status = EFI_DEVICE_ERROR;
1210 }
1211
1212 gBS->RestoreTPL (OldTpl);
1213 return Status;
1214 }
1215
1216 /**
1217 Read BufferSize bytes from Lba into Buffer.
1218
1219 @param[in] This Indicates a pointer to the calling context.
1220 @param[in] MediaId Id of the media, changes every time the media is replaced.
1221 @param[in] Lba The starting Logical Block Address to read from.
1222 @param[in, out] Token A pointer to the token associated with the transaction.
1223 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
1224 @param[out] Buffer A pointer to the destination buffer for the data. The caller is
1225 responsible for either having implicit or explicit ownership of the buffer.
1226
1227 @retval EFI_SUCCESS The read request was queued if Event is not NULL.
1228 The data was read correctly from the device if
1229 the Event is NULL.
1230 @retval EFI_DEVICE_ERROR The device reported an error while performing
1231 the read.
1232 @retval EFI_NO_MEDIA There is no media in the device.
1233 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
1234 @retval EFI_BAD_BUFFER_SIZE The BufferSize parameter is not a multiple of the
1235 intrinsic block size of the device.
1236 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
1237 or the buffer is not on proper alignment.
1238 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack
1239 of resources.
1240
1241 **/
1242 EFI_STATUS
1243 EFIAPI
1244 AtaBlockIoReadBlocksEx (
1245 IN EFI_BLOCK_IO2_PROTOCOL *This,
1246 IN UINT32 MediaId,
1247 IN EFI_LBA Lba,
1248 IN OUT EFI_BLOCK_IO2_TOKEN *Token,
1249 IN UINTN BufferSize,
1250 OUT VOID *Buffer
1251 )
1252 {
1253 return BlockIoReadWrite ((VOID *)This, MediaId, Lba, Token, BufferSize, Buffer, TRUE, FALSE);
1254 }
1255
1256 /**
1257 Write BufferSize bytes from Lba into Buffer.
1258
1259 @param[in] This Indicates a pointer to the calling context.
1260 @param[in] MediaId The media ID that the write request is for.
1261 @param[in] Lba The starting logical block address to be written. The
1262 caller is responsible for writing to only legitimate
1263 locations.
1264 @param[in, out] Token A pointer to the token associated with the transaction.
1265 @param[in] BufferSize Size of Buffer, must be a multiple of device block size.
1266 @param[in] Buffer A pointer to the source buffer for the data.
1267
1268 @retval EFI_SUCCESS The data was written correctly to the device.
1269 @retval EFI_WRITE_PROTECTED The device can not be written to.
1270 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
1271 @retval EFI_NO_MEDIA There is no media in the device.
1272 @retval EFI_MEDIA_CHANGED The MediaId does not match the current device.
1273 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
1274 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
1275 or the buffer is not on proper alignment.
1276
1277 **/
1278 EFI_STATUS
1279 EFIAPI
1280 AtaBlockIoWriteBlocksEx (
1281 IN EFI_BLOCK_IO2_PROTOCOL *This,
1282 IN UINT32 MediaId,
1283 IN EFI_LBA Lba,
1284 IN OUT EFI_BLOCK_IO2_TOKEN *Token,
1285 IN UINTN BufferSize,
1286 IN VOID *Buffer
1287 )
1288 {
1289 return BlockIoReadWrite ((VOID *)This, MediaId, Lba, Token, BufferSize, Buffer, TRUE, TRUE);
1290 }
1291
1292 /**
1293 Flush the Block Device.
1294
1295 @param[in] This Indicates a pointer to the calling context.
1296 @param[in, out] Token A pointer to the token associated with the transaction.
1297
1298 @retval EFI_SUCCESS All outstanding data was written to the device
1299 @retval EFI_DEVICE_ERROR The device reported an error while writing back the data
1300 @retval EFI_NO_MEDIA There is no media in the device.
1301
1302 **/
1303 EFI_STATUS
1304 EFIAPI
1305 AtaBlockIoFlushBlocksEx (
1306 IN EFI_BLOCK_IO2_PROTOCOL *This,
1307 IN OUT EFI_BLOCK_IO2_TOKEN *Token
1308 )
1309 {
1310 //
1311 // Signal event and return directly.
1312 //
1313 if ((Token != NULL) && (Token->Event != NULL)) {
1314 Token->TransactionStatus = EFI_SUCCESS;
1315 gBS->SignalEvent (Token->Event);
1316 }
1317
1318 return EFI_SUCCESS;
1319 }
1320
1321 /**
1322 Provides inquiry information for the controller type.
1323
1324 This function is used by the IDE bus driver to get inquiry data. Data format
1325 of Identify data is defined by the Interface GUID.
1326
1327 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
1328 @param[in, out] InquiryData Pointer to a buffer for the inquiry data.
1329 @param[in, out] InquiryDataSize Pointer to the value for the inquiry data size.
1330
1331 @retval EFI_SUCCESS The command was accepted without any errors.
1332 @retval EFI_NOT_FOUND Device does not support this data class
1333 @retval EFI_DEVICE_ERROR Error reading InquiryData from device
1334 @retval EFI_BUFFER_TOO_SMALL InquiryDataSize not big enough
1335
1336 **/
1337 EFI_STATUS
1338 EFIAPI
1339 AtaDiskInfoInquiry (
1340 IN EFI_DISK_INFO_PROTOCOL *This,
1341 IN OUT VOID *InquiryData,
1342 IN OUT UINT32 *InquiryDataSize
1343 )
1344 {
1345 return EFI_NOT_FOUND;
1346 }
1347
1348 /**
1349 Provides identify information for the controller type.
1350
1351 This function is used by the IDE bus driver to get identify data. Data format
1352 of Identify data is defined by the Interface GUID.
1353
1354 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL
1355 instance.
1356 @param[in, out] IdentifyData Pointer to a buffer for the identify data.
1357 @param[in, out] IdentifyDataSize Pointer to the value for the identify data
1358 size.
1359
1360 @retval EFI_SUCCESS The command was accepted without any errors.
1361 @retval EFI_NOT_FOUND Device does not support this data class
1362 @retval EFI_DEVICE_ERROR Error reading IdentifyData from device
1363 @retval EFI_BUFFER_TOO_SMALL IdentifyDataSize not big enough
1364
1365 **/
1366 EFI_STATUS
1367 EFIAPI
1368 AtaDiskInfoIdentify (
1369 IN EFI_DISK_INFO_PROTOCOL *This,
1370 IN OUT VOID *IdentifyData,
1371 IN OUT UINT32 *IdentifyDataSize
1372 )
1373 {
1374 EFI_STATUS Status;
1375 ATA_DEVICE *AtaDevice;
1376
1377 AtaDevice = ATA_DEVICE_FROM_DISK_INFO (This);
1378
1379 Status = EFI_BUFFER_TOO_SMALL;
1380 if (*IdentifyDataSize >= sizeof (ATA_IDENTIFY_DATA)) {
1381 Status = EFI_SUCCESS;
1382 CopyMem (IdentifyData, AtaDevice->IdentifyData, sizeof (ATA_IDENTIFY_DATA));
1383 }
1384
1385 *IdentifyDataSize = sizeof (ATA_IDENTIFY_DATA);
1386
1387 return Status;
1388 }
1389
1390 /**
1391 Provides sense data information for the controller type.
1392
1393 This function is used by the IDE bus driver to get sense data.
1394 Data format of Sense data is defined by the Interface GUID.
1395
1396 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
1397 @param[in, out] SenseData Pointer to the SenseData.
1398 @param[in, out] SenseDataSize Size of SenseData in bytes.
1399 @param[out] SenseDataNumber Pointer to the value for the sense data size.
1400
1401 @retval EFI_SUCCESS The command was accepted without any errors.
1402 @retval EFI_NOT_FOUND Device does not support this data class.
1403 @retval EFI_DEVICE_ERROR Error reading SenseData from device.
1404 @retval EFI_BUFFER_TOO_SMALL SenseDataSize not big enough.
1405
1406 **/
1407 EFI_STATUS
1408 EFIAPI
1409 AtaDiskInfoSenseData (
1410 IN EFI_DISK_INFO_PROTOCOL *This,
1411 IN OUT VOID *SenseData,
1412 IN OUT UINT32 *SenseDataSize,
1413 OUT UINT8 *SenseDataNumber
1414 )
1415 {
1416 return EFI_NOT_FOUND;
1417 }
1418
1419 /**
1420 This function is used by the IDE bus driver to get controller information.
1421
1422 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
1423 @param[out] IdeChannel Pointer to the Ide Channel number. Primary or secondary.
1424 @param[out] IdeDevice Pointer to the Ide Device number. Master or slave.
1425
1426 @retval EFI_SUCCESS IdeChannel and IdeDevice are valid.
1427 @retval EFI_UNSUPPORTED This is not an IDE device.
1428
1429 **/
1430 EFI_STATUS
1431 EFIAPI
1432 AtaDiskInfoWhichIde (
1433 IN EFI_DISK_INFO_PROTOCOL *This,
1434 OUT UINT32 *IdeChannel,
1435 OUT UINT32 *IdeDevice
1436 )
1437 {
1438 ATA_DEVICE *AtaDevice;
1439
1440 AtaDevice = ATA_DEVICE_FROM_DISK_INFO (This);
1441 *IdeChannel = AtaDevice->Port;
1442 *IdeDevice = AtaDevice->PortMultiplierPort;
1443
1444 return EFI_SUCCESS;
1445 }
1446
1447 /**
1448 Send a security protocol command to a device that receives data and/or the result
1449 of one or more commands sent by SendData.
1450
1451 The ReceiveData function sends a security protocol command to the given MediaId.
1452 The security protocol command sent is defined by SecurityProtocolId and contains
1453 the security protocol specific data SecurityProtocolSpecificData. The function
1454 returns the data from the security protocol command in PayloadBuffer.
1455
1456 For devices supporting the SCSI command set, the security protocol command is sent
1457 using the SECURITY PROTOCOL IN command defined in SPC-4.
1458
1459 For devices supporting the ATA command set, the security protocol command is sent
1460 using one of the TRUSTED RECEIVE commands defined in ATA8-ACS if PayloadBufferSize
1461 is non-zero.
1462
1463 If the PayloadBufferSize is zero, the security protocol command is sent using the
1464 Trusted Non-Data command defined in ATA8-ACS.
1465
1466 If PayloadBufferSize is too small to store the available data from the security
1467 protocol command, the function shall copy PayloadBufferSize bytes into the
1468 PayloadBuffer and return EFI_WARN_BUFFER_TOO_SMALL.
1469
1470 If PayloadBuffer or PayloadTransferSize is NULL and PayloadBufferSize is non-zero,
1471 the function shall return EFI_INVALID_PARAMETER.
1472
1473 If the given MediaId does not support security protocol commands, the function shall
1474 return EFI_UNSUPPORTED. If there is no media in the device, the function returns
1475 EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the device,
1476 the function returns EFI_MEDIA_CHANGED.
1477
1478 If the security protocol fails to complete within the Timeout period, the function
1479 shall return EFI_TIMEOUT.
1480
1481 If the security protocol command completes without an error, the function shall
1482 return EFI_SUCCESS. If the security protocol command completes with an error, the
1483 function shall return EFI_DEVICE_ERROR.
1484
1485 @param This Indicates a pointer to the calling context.
1486 @param MediaId ID of the medium to receive data from.
1487 @param Timeout The timeout, in 100ns units, to use for the execution
1488 of the security protocol command. A Timeout value of 0
1489 means that this function will wait indefinitely for the
1490 security protocol command to execute. If Timeout is greater
1491 than zero, then this function will return EFI_TIMEOUT
1492 if the time required to execute the receive data command
1493 is greater than Timeout.
1494 @param SecurityProtocolId The value of the "Security Protocol" parameter of
1495 the security protocol command to be sent.
1496 @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
1497 of the security protocol command to be sent.
1498 @param PayloadBufferSize Size in bytes of the payload data buffer.
1499 @param PayloadBuffer A pointer to a destination buffer to store the security
1500 protocol command specific payload data for the security
1501 protocol command. The caller is responsible for having
1502 either implicit or explicit ownership of the buffer.
1503 @param PayloadTransferSize A pointer to a buffer to store the size in bytes of the
1504 data written to the payload data buffer.
1505
1506 @retval EFI_SUCCESS The security protocol command completed successfully.
1507 @retval EFI_WARN_BUFFER_TOO_SMALL The PayloadBufferSize was too small to store the available
1508 data from the device. The PayloadBuffer contains the truncated data.
1509 @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.
1510 @retval EFI_DEVICE_ERROR The security protocol command completed with an error.
1511 @retval EFI_NO_MEDIA There is no media in the device.
1512 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
1513 @retval EFI_INVALID_PARAMETER The PayloadBuffer or PayloadTransferSize is NULL and
1514 PayloadBufferSize is non-zero.
1515 @retval EFI_TIMEOUT A timeout occurred while waiting for the security
1516 protocol command to execute.
1517
1518 **/
1519 EFI_STATUS
1520 EFIAPI
1521 AtaStorageSecurityReceiveData (
1522 IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,
1523 IN UINT32 MediaId,
1524 IN UINT64 Timeout,
1525 IN UINT8 SecurityProtocolId,
1526 IN UINT16 SecurityProtocolSpecificData,
1527 IN UINTN PayloadBufferSize,
1528 OUT VOID *PayloadBuffer,
1529 OUT UINTN *PayloadTransferSize
1530 )
1531 {
1532 EFI_STATUS Status;
1533 ATA_DEVICE *Private;
1534 EFI_TPL OldTpl;
1535
1536 DEBUG ((DEBUG_INFO, "EFI Storage Security Protocol - Read\n"));
1537 if (((PayloadBuffer == NULL) || (PayloadTransferSize == NULL)) && (PayloadBufferSize != 0)) {
1538 return EFI_INVALID_PARAMETER;
1539 }
1540
1541 Status = EFI_SUCCESS;
1542 Private = ATA_DEVICE_FROM_STORAGE_SECURITY (This);
1543
1544 if (MediaId != Private->BlockIo.Media->MediaId) {
1545 return EFI_MEDIA_CHANGED;
1546 }
1547
1548 if (!Private->BlockIo.Media->MediaPresent) {
1549 return EFI_NO_MEDIA;
1550 }
1551
1552 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
1553
1554 Status = TrustTransferAtaDevice (
1555 Private,
1556 PayloadBuffer,
1557 SecurityProtocolId,
1558 SecurityProtocolSpecificData,
1559 PayloadBufferSize,
1560 FALSE,
1561 Timeout,
1562 PayloadTransferSize
1563 );
1564
1565 gBS->RestoreTPL (OldTpl);
1566 return Status;
1567 }
1568
1569 /**
1570 Send a security protocol command to a device.
1571
1572 The SendData function sends a security protocol command containing the payload
1573 PayloadBuffer to the given MediaId. The security protocol command sent is
1574 defined by SecurityProtocolId and contains the security protocol specific data
1575 SecurityProtocolSpecificData. If the underlying protocol command requires a
1576 specific padding for the command payload, the SendData function shall add padding
1577 bytes to the command payload to satisfy the padding requirements.
1578
1579 For devices supporting the SCSI command set, the security protocol command is sent
1580 using the SECURITY PROTOCOL OUT command defined in SPC-4.
1581
1582 For devices supporting the ATA command set, the security protocol command is sent
1583 using one of the TRUSTED SEND commands defined in ATA8-ACS if PayloadBufferSize
1584 is non-zero. If the PayloadBufferSize is zero, the security protocol command is
1585 sent using the Trusted Non-Data command defined in ATA8-ACS.
1586
1587 If PayloadBuffer is NULL and PayloadBufferSize is non-zero, the function shall
1588 return EFI_INVALID_PARAMETER.
1589
1590 If the given MediaId does not support security protocol commands, the function
1591 shall return EFI_UNSUPPORTED. If there is no media in the device, the function
1592 returns EFI_NO_MEDIA. If the MediaId is not the ID for the current media in the
1593 device, the function returns EFI_MEDIA_CHANGED.
1594
1595 If the security protocol fails to complete within the Timeout period, the function
1596 shall return EFI_TIMEOUT.
1597
1598 If the security protocol command completes without an error, the function shall return
1599 EFI_SUCCESS. If the security protocol command completes with an error, the function
1600 shall return EFI_DEVICE_ERROR.
1601
1602 @param This Indicates a pointer to the calling context.
1603 @param MediaId ID of the medium to receive data from.
1604 @param Timeout The timeout, in 100ns units, to use for the execution
1605 of the security protocol command. A Timeout value of 0
1606 means that this function will wait indefinitely for the
1607 security protocol command to execute. If Timeout is greater
1608 than zero, then this function will return EFI_TIMEOUT
1609 if the time required to execute the receive data command
1610 is greater than Timeout.
1611 @param SecurityProtocolId The value of the "Security Protocol" parameter of
1612 the security protocol command to be sent.
1613 @param SecurityProtocolSpecificData The value of the "Security Protocol Specific" parameter
1614 of the security protocol command to be sent.
1615 @param PayloadBufferSize Size in bytes of the payload data buffer.
1616 @param PayloadBuffer A pointer to a destination buffer to store the security
1617 protocol command specific payload data for the security
1618 protocol command.
1619
1620 @retval EFI_SUCCESS The security protocol command completed successfully.
1621 @retval EFI_UNSUPPORTED The given MediaId does not support security protocol commands.
1622 @retval EFI_DEVICE_ERROR The security protocol command completed with an error.
1623 @retval EFI_NO_MEDIA There is no media in the device.
1624 @retval EFI_MEDIA_CHANGED The MediaId is not for the current media.
1625 @retval EFI_INVALID_PARAMETER The PayloadBuffer is NULL and PayloadBufferSize is non-zero.
1626 @retval EFI_TIMEOUT A timeout occurred while waiting for the security
1627 protocol command to execute.
1628
1629 **/
1630 EFI_STATUS
1631 EFIAPI
1632 AtaStorageSecuritySendData (
1633 IN EFI_STORAGE_SECURITY_COMMAND_PROTOCOL *This,
1634 IN UINT32 MediaId,
1635 IN UINT64 Timeout,
1636 IN UINT8 SecurityProtocolId,
1637 IN UINT16 SecurityProtocolSpecificData,
1638 IN UINTN PayloadBufferSize,
1639 IN VOID *PayloadBuffer
1640 )
1641 {
1642 EFI_STATUS Status;
1643 ATA_DEVICE *Private;
1644 EFI_TPL OldTpl;
1645
1646 DEBUG ((DEBUG_INFO, "EFI Storage Security Protocol - Send\n"));
1647 if ((PayloadBuffer == NULL) && (PayloadBufferSize != 0)) {
1648 return EFI_INVALID_PARAMETER;
1649 }
1650
1651 Status = EFI_SUCCESS;
1652 Private = ATA_DEVICE_FROM_STORAGE_SECURITY (This);
1653
1654 if (MediaId != Private->BlockIo.Media->MediaId) {
1655 return EFI_MEDIA_CHANGED;
1656 }
1657
1658 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
1659 Status = TrustTransferAtaDevice (
1660 Private,
1661 PayloadBuffer,
1662 SecurityProtocolId,
1663 SecurityProtocolSpecificData,
1664 PayloadBufferSize,
1665 TRUE,
1666 Timeout,
1667 NULL
1668 );
1669
1670 gBS->RestoreTPL (OldTpl);
1671 return Status;
1672 }
1673
1674 /**
1675 The user Entry Point for module AtaBus. The user code starts with this function.
1676
1677 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1678 @param[in] SystemTable A pointer to the EFI System Table.
1679
1680 @retval EFI_SUCCESS The entry point is executed successfully.
1681 @retval other Some error occurs when executing this entry point.
1682
1683 **/
1684 EFI_STATUS
1685 EFIAPI
1686 InitializeAtaBus (
1687 IN EFI_HANDLE ImageHandle,
1688 IN EFI_SYSTEM_TABLE *SystemTable
1689 )
1690 {
1691 EFI_STATUS Status;
1692
1693 //
1694 // Install driver model protocol(s).
1695 //
1696 Status = EfiLibInstallDriverBindingComponentName2 (
1697 ImageHandle,
1698 SystemTable,
1699 &gAtaBusDriverBinding,
1700 ImageHandle,
1701 &gAtaBusComponentName,
1702 &gAtaBusComponentName2
1703 );
1704 ASSERT_EFI_ERROR (Status);
1705
1706 return Status;
1707 }