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