]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Bus/Ata/AtaBusDxe/AtaBus.c
Update the copyright notice format
[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, 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 { // BlockMedia
48 0, // MediaId
49 FALSE, // RemovableMedia
50 TRUE, // MediaPresent
51 FALSE, // LogicPartition
52 FALSE, // ReadOnly
53 FALSE, // WritingCache
54 0x200, // BlockSize
55 0, // IoAlign
56 0, // LastBlock
57 0, // LowestAlignedLba
58 1 // LogicalBlocksPerPhysicalBlock
59 },
60 { // DiskInfo
61 EFI_DISK_INFO_IDE_INTERFACE_GUID,
62 AtaDiskInfoInquiry,
63 AtaDiskInfoIdentify,
64 AtaDiskInfoSenseData,
65 AtaDiskInfoWhichIde
66 },
67 NULL, // DevicePath
68 NULL, // AtaBusDriverData
69 0, // Port
70 0, // PortMultiplierPort
71 { 0, }, // Packet
72 {{ 0}, }, // Acb
73 NULL, // Asb
74 FALSE, // UdmaValid
75 FALSE, // Lba48Bit
76 NULL, // IdentifyData
77 NULL, // ControllerNameTable
78 {L'\0', } // ModelName
79 };
80
81 /**
82 Allocates an aligned buffer for ATA device.
83
84 This function allocates an aligned buffer for the ATA device to perform
85 ATA pass through operations. The alignment requirement is from ATA pass
86 through interface.
87
88 @param AtaDevice The ATA child device involved for the operation.
89 @param BufferSize The request buffer size.
90
91 @return A pointer to the aligned buffer or NULL if the allocation fails.
92
93 **/
94 VOID *
95 AllocateAlignedBuffer (
96 IN ATA_DEVICE *AtaDevice,
97 IN UINTN BufferSize
98 )
99 {
100 return AllocateAlignedPages (EFI_SIZE_TO_PAGES (BufferSize), AtaDevice->AtaBusDriverData->AtaPassThru->Mode->IoAlign);
101 }
102
103 /**
104 Frees an aligned buffer for ATA device.
105
106 This function frees an aligned buffer for the ATA device to perform
107 ATA pass through operations.
108
109 @param Buffer The aligned buffer to be freed.
110 @param BufferSize The request buffer size.
111
112 **/
113 VOID
114 FreeAlignedBuffer (
115 IN VOID *Buffer,
116 IN UINTN BufferSize
117 )
118 {
119 if (Buffer != NULL) {
120 FreePages (Buffer, EFI_SIZE_TO_PAGES (BufferSize));
121 }
122 }
123
124
125 /**
126 Release all the resources allocated for the ATA device.
127
128 This function releases all the resources allocated for the ATA device.
129
130 @param AtaDevice The ATA child device involved for the operation.
131
132 **/
133 VOID
134 ReleaseAtaResources (
135 IN ATA_DEVICE *AtaDevice
136 )
137 {
138 FreeUnicodeStringTable (AtaDevice->ControllerNameTable);
139 FreeAlignedBuffer (AtaDevice->Asb, sizeof (*AtaDevice->Asb));
140 FreeAlignedBuffer (AtaDevice->IdentifyData, sizeof (*AtaDevice->IdentifyData));
141 if (AtaDevice->DevicePath != NULL) {
142 FreePool (AtaDevice->DevicePath);
143 }
144 FreePool (AtaDevice);
145 }
146
147
148 /**
149 Registers an ATA device.
150
151 This function allocates an ATA device structure for the ATA device specified by
152 Port and PortMultiplierPort if the ATA device is identified as a valid one.
153 Then it will create child handle and install Block IO and Disk Info protocol on
154 it.
155
156 @param AtaBusDriverData The parent ATA bus driver data structure.
157 @param Port The port number of the ATA device.
158 @param PortMultiplierPort The port multiplier port number of the ATA device.
159
160 @retval EFI_SUCCESS The ATA device is successfully registered.
161 @retval EFI_OUT_OF_RESOURCES There is not enough memory to allocate the ATA device
162 and related data structures.
163 @return Others Some error occurs when registering the ATA device.
164 **/
165 EFI_STATUS
166 RegisterAtaDevice (
167 IN OUT ATA_BUS_DRIVER_DATA *AtaBusDriverData,
168 IN UINT16 Port,
169 IN UINT16 PortMultiplierPort
170 )
171 {
172 EFI_STATUS Status;
173 ATA_DEVICE *AtaDevice;
174 EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;
175 EFI_DEVICE_PATH_PROTOCOL *NewDevicePathNode;
176
177 NewDevicePathNode = NULL;
178
179 //
180 // Allocate ATA device from the template.
181 //
182 AtaDevice = AllocateCopyPool (sizeof (gAtaDeviceTemplate), &gAtaDeviceTemplate);
183 if (AtaDevice == NULL) {
184 return EFI_OUT_OF_RESOURCES;
185 }
186
187 //
188 // Initializes ATA device structures and allocates the required buffer.
189 //
190 AtaDevice->BlockIo.Media = &AtaDevice->BlockMedia;
191 AtaDevice->AtaBusDriverData = AtaBusDriverData;
192 AtaDevice->Port = Port;
193 AtaDevice->PortMultiplierPort = PortMultiplierPort;
194 AtaDevice->Asb = AllocateAlignedBuffer (AtaDevice, sizeof (*AtaDevice->Asb));
195 if (AtaDevice->Asb == NULL) {
196 Status = EFI_OUT_OF_RESOURCES;
197 goto Done;
198 }
199 AtaDevice->IdentifyData = AllocateAlignedBuffer (AtaDevice, sizeof (*AtaDevice->IdentifyData));
200 if (AtaDevice->IdentifyData == NULL) {
201 Status = EFI_OUT_OF_RESOURCES;
202 goto Done;
203 }
204
205 //
206 // Try to identify the ATA device via the ATA pass through command.
207 //
208 Status = DiscoverAtaDevice (AtaDevice);
209 if (EFI_ERROR (Status)) {
210 goto Done;
211 }
212
213 //
214 // Build controller name for Component Name (2) protocol.
215 //
216 Status = AddUnicodeString2 (
217 "eng",
218 gAtaBusComponentName.SupportedLanguages,
219 &AtaDevice->ControllerNameTable,
220 AtaDevice->ModelName,
221 TRUE
222 );
223 if (EFI_ERROR (Status)) {
224 goto Done;
225 }
226
227 Status = AddUnicodeString2 (
228 "en",
229 gAtaBusComponentName2.SupportedLanguages,
230 &AtaDevice->ControllerNameTable,
231 AtaDevice->ModelName,
232 FALSE
233 );
234 if (EFI_ERROR (Status)) {
235 goto Done;
236 }
237
238 //
239 // Build device path
240 //
241 AtaPassThru = AtaBusDriverData->AtaPassThru;
242 Status = AtaPassThru->BuildDevicePath (AtaPassThru, Port, PortMultiplierPort, &NewDevicePathNode);
243 if (EFI_ERROR (Status)) {
244 goto Done;
245 }
246
247 //
248 // Update to AHCI interface GUID based on device path node. The default one
249 // is IDE interface GUID copied from template.
250 //
251 if (NewDevicePathNode->SubType == MSG_SATA_DP) {
252 CopyGuid (&AtaDevice->DiskInfo.Interface, &gEfiDiskInfoAhciInterfaceGuid);
253 }
254
255 AtaDevice->DevicePath = AppendDevicePathNode (AtaBusDriverData->ParentDevicePath, NewDevicePathNode);
256 if (AtaDevice->DevicePath == NULL) {
257 goto Done;
258 }
259
260 Status = gBS->InstallMultipleProtocolInterfaces (
261 &AtaDevice->Handle,
262 &gEfiDevicePathProtocolGuid,
263 AtaDevice->DevicePath,
264 &gEfiBlockIoProtocolGuid,
265 &AtaDevice->BlockIo,
266 &gEfiDiskInfoProtocolGuid,
267 &AtaDevice->DiskInfo,
268 NULL
269 );
270 if (EFI_ERROR (Status)) {
271 goto Done;
272 }
273
274 gBS->OpenProtocol (
275 AtaBusDriverData->Controller,
276 &gEfiAtaPassThruProtocolGuid,
277 (VOID **) &AtaPassThru,
278 AtaBusDriverData->DriverBindingHandle,
279 AtaDevice->Handle,
280 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
281 );
282
283 Done:
284 if (NewDevicePathNode != NULL) {
285 FreePool (NewDevicePathNode);
286 }
287
288 if (EFI_ERROR (Status)) {
289 ReleaseAtaResources (AtaDevice);
290 DEBUG ((DEBUG_ERROR | DEBUG_INIT, "Failed to initialize Port %x PortMultiplierPort %x, status = %r\n", Port, PortMultiplierPort, Status));
291 }
292 return Status;
293 }
294
295
296 /**
297 Unregisters an ATA device.
298
299 This function removes the protocols installed on the controller handle and
300 frees the resources allocated for the ATA device.
301
302 @param This The pointer to EFI_DRIVER_BINDING_PROTOCOL instance.
303 @param Controller The controller handle of the ATA device.
304 @param Handle The child handle.
305
306 @retval EFI_SUCCESS The ATA device is successfully unregistered.
307 @return Others Some error occurs when unregistering the ATA device.
308
309 **/
310 EFI_STATUS
311 UnregisterAtaDevice (
312 IN EFI_DRIVER_BINDING_PROTOCOL *This,
313 IN EFI_HANDLE Controller,
314 IN EFI_HANDLE Handle
315 )
316 {
317 EFI_STATUS Status;
318 EFI_BLOCK_IO_PROTOCOL *BlockIo;
319 ATA_DEVICE *AtaDevice;
320 EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;
321
322 Status = gBS->OpenProtocol (
323 Handle,
324 &gEfiBlockIoProtocolGuid,
325 (VOID **) &BlockIo,
326 This->DriverBindingHandle,
327 Controller,
328 EFI_OPEN_PROTOCOL_GET_PROTOCOL
329 );
330 if (EFI_ERROR (Status)) {
331 return Status;
332 }
333
334 AtaDevice = ATA_DEVICE_FROM_BLOCK_IO (BlockIo);
335
336 //
337 // Close the child handle
338 //
339 gBS->CloseProtocol (
340 Controller,
341 &gEfiAtaPassThruProtocolGuid,
342 This->DriverBindingHandle,
343 Handle
344 );
345
346 Status = gBS->UninstallMultipleProtocolInterfaces (
347 Handle,
348 &gEfiDevicePathProtocolGuid,
349 AtaDevice->DevicePath,
350 &gEfiBlockIoProtocolGuid,
351 &AtaDevice->BlockIo,
352 &gEfiDiskInfoProtocolGuid,
353 &AtaDevice->DiskInfo,
354 NULL
355 );
356
357 if (EFI_ERROR (Status)) {
358 gBS->OpenProtocol (
359 Controller,
360 &gEfiAtaPassThruProtocolGuid,
361 (VOID **) &AtaPassThru,
362 This->DriverBindingHandle,
363 Handle,
364 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER
365 );
366 return Status;
367 }
368
369 ReleaseAtaResources (AtaDevice);
370
371 return Status;
372 }
373
374
375
376 /**
377 Tests to see if this driver supports a given controller. If a child device is provided,
378 it further tests to see if this driver supports creating a handle for the specified child device.
379
380 This function checks to see if the driver specified by This supports the device specified by
381 ControllerHandle. Drivers will typically use the device path attached to
382 ControllerHandle and/or the services from the bus I/O abstraction attached to
383 ControllerHandle to determine if the driver supports ControllerHandle. This function
384 may be called many times during platform initialization. In order to reduce boot times, the tests
385 performed by this function must be very small, and take as little time as possible to execute. This
386 function must not change the state of any hardware devices, and this function must be aware that the
387 device specified by ControllerHandle may already be managed by the same driver or a
388 different driver. This function must match its calls to AllocatePages() with FreePages(),
389 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
390 Since ControllerHandle may have been previously started by the same driver, if a protocol is
391 already in the opened state, then it must not be closed with CloseProtocol(). This is required
392 to guarantee the state of ControllerHandle is not modified by this function.
393
394 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
395 @param[in] ControllerHandle The handle of the controller to test. This handle
396 must support a protocol interface that supplies
397 an I/O abstraction to the driver.
398 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
399 parameter is ignored by device drivers, and is optional for bus
400 drivers. For bus drivers, if this parameter is not NULL, then
401 the bus driver must determine if the bus controller specified
402 by ControllerHandle and the child controller specified
403 by RemainingDevicePath are both supported by this
404 bus driver.
405
406 @retval EFI_SUCCESS The device specified by ControllerHandle and
407 RemainingDevicePath is supported by the driver specified by This.
408 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
409 RemainingDevicePath is already being managed by the driver
410 specified by This.
411 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
412 RemainingDevicePath is already being managed by a different
413 driver or an application that requires exclusive access.
414 Currently not implemented.
415 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
416 RemainingDevicePath is not supported by the driver specified by This.
417 **/
418 EFI_STATUS
419 EFIAPI
420 AtaBusDriverBindingSupported (
421 IN EFI_DRIVER_BINDING_PROTOCOL *This,
422 IN EFI_HANDLE Controller,
423 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
424 )
425 {
426 EFI_STATUS Status;
427 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
428 EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;
429 UINT16 Port;
430 UINT16 PortMultiplierPort;
431
432 //
433 // Test EFI_ATA_PASS_THRU_PROTOCOL on controller handle.
434 //
435 Status = gBS->OpenProtocol (
436 Controller,
437 &gEfiAtaPassThruProtocolGuid,
438 (VOID **) &AtaPassThru,
439 This->DriverBindingHandle,
440 Controller,
441 EFI_OPEN_PROTOCOL_BY_DRIVER
442 );
443
444 if (Status == EFI_ALREADY_STARTED) {
445 return EFI_SUCCESS;
446 }
447
448 if (EFI_ERROR (Status)) {
449 return Status;
450 }
451
452 //
453 // Test RemainingDevicePath is valid or not.
454 //
455 if ((RemainingDevicePath != NULL) && !IsDevicePathEnd (RemainingDevicePath)) {
456 Status = AtaPassThru->GetDevice (AtaPassThru, RemainingDevicePath, &Port, &PortMultiplierPort);
457 if (EFI_ERROR (Status)) {
458 return Status;
459 }
460 }
461
462 //
463 // Close the I/O Abstraction(s) used to perform the supported test
464 //
465 gBS->CloseProtocol (
466 Controller,
467 &gEfiAtaPassThruProtocolGuid,
468 This->DriverBindingHandle,
469 Controller
470 );
471
472 //
473 // Open the EFI Device Path protocol needed to perform the supported test
474 //
475 Status = gBS->OpenProtocol (
476 Controller,
477 &gEfiDevicePathProtocolGuid,
478 (VOID **) &ParentDevicePath,
479 This->DriverBindingHandle,
480 Controller,
481 EFI_OPEN_PROTOCOL_GET_PROTOCOL
482 );
483 return Status;
484 }
485
486
487 /**
488 Starts a device controller or a bus controller.
489
490 The Start() function is designed to be invoked from the EFI boot service ConnectController().
491 As a result, much of the error checking on the parameters to Start() has been moved into this
492 common boot service. It is legal to call Start() from other locations,
493 but the following calling restrictions must be followed or the system behavior will not be deterministic.
494 1. ControllerHandle must be a valid EFI_HANDLE.
495 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
496 EFI_DEVICE_PATH_PROTOCOL.
497 3. Prior to calling Start(), the Supported() function for the driver specified by This must
498 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
499
500 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
501 @param[in] ControllerHandle The handle of the controller to start. This handle
502 must support a protocol interface that supplies
503 an I/O abstraction to the driver.
504 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
505 parameter is ignored by device drivers, and is optional for bus
506 drivers. For a bus driver, if this parameter is NULL, then handles
507 for all the children of Controller are created by this driver.
508 If this parameter is not NULL and the first Device Path Node is
509 not the End of Device Path Node, then only the handle for the
510 child device specified by the first Device Path Node of
511 RemainingDevicePath is created by this driver.
512 If the first Device Path Node of RemainingDevicePath is
513 the End of Device Path Node, no child handle is created by this
514 driver.
515
516 @retval EFI_SUCCESS The device was started.
517 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
518 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
519 @retval Others The driver failded to start the device.
520
521 **/
522 EFI_STATUS
523 EFIAPI
524 AtaBusDriverBindingStart (
525 IN EFI_DRIVER_BINDING_PROTOCOL *This,
526 IN EFI_HANDLE Controller,
527 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
528 )
529 {
530 EFI_STATUS Status;
531 EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;
532 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
533 ATA_BUS_DRIVER_DATA *AtaBusDriverData;
534 UINT16 Port;
535 UINT16 PortMultiplierPort;
536
537 AtaBusDriverData = NULL;
538
539 Status = gBS->OpenProtocol (
540 Controller,
541 &gEfiDevicePathProtocolGuid,
542 (VOID **) &ParentDevicePath,
543 This->DriverBindingHandle,
544 Controller,
545 EFI_OPEN_PROTOCOL_GET_PROTOCOL
546 );
547 if (EFI_ERROR (Status)) {
548 return Status;
549 }
550
551 Status = gBS->OpenProtocol (
552 Controller,
553 &gEfiAtaPassThruProtocolGuid,
554 (VOID **) &AtaPassThru,
555 This->DriverBindingHandle,
556 Controller,
557 EFI_OPEN_PROTOCOL_BY_DRIVER
558 );
559 if ((EFI_ERROR (Status)) && (Status != EFI_ALREADY_STARTED)) {
560 goto ErrorExit;
561 }
562
563 //
564 // Check EFI_ALREADY_STARTED to reuse the original ATA_BUS_DRIVER_DATA.
565 //
566 if (Status != EFI_ALREADY_STARTED) {
567 AtaBusDriverData = AllocateZeroPool (sizeof (ATA_BUS_DRIVER_DATA));
568 if (AtaBusDriverData == NULL) {
569 Status = EFI_OUT_OF_RESOURCES;
570 goto ErrorExit;
571 }
572
573 AtaBusDriverData->AtaPassThru = AtaPassThru;
574 AtaBusDriverData->Controller = Controller;
575 AtaBusDriverData->ParentDevicePath = ParentDevicePath;
576 AtaBusDriverData->DriverBindingHandle = This->DriverBindingHandle;
577
578 Status = gBS->InstallMultipleProtocolInterfaces (
579 &Controller,
580 &gEfiCallerIdGuid,
581 AtaBusDriverData,
582 NULL
583 );
584 if (EFI_ERROR (Status)) {
585 goto ErrorExit;
586 }
587
588 } else {
589 Status = gBS->OpenProtocol (
590 Controller,
591 &gEfiCallerIdGuid,
592 (VOID **) &AtaBusDriverData,
593 This->DriverBindingHandle,
594 Controller,
595 EFI_OPEN_PROTOCOL_GET_PROTOCOL
596 );
597 if (EFI_ERROR (Status)) {
598 AtaBusDriverData = NULL;
599 goto ErrorExit;
600 }
601 }
602
603 if (RemainingDevicePath == NULL) {
604 Port = 0xFFFF;
605 while (TRUE) {
606 Status = AtaPassThru->GetNextPort (AtaPassThru, &Port);
607 if (EFI_ERROR (Status)) {
608 //
609 // We cannot find more legal port then we are done.
610 //
611 break;
612 }
613
614 PortMultiplierPort = 0xFFFF;
615 while (TRUE) {
616 Status = AtaPassThru->GetNextDevice (AtaPassThru, Port, &PortMultiplierPort);
617 if (EFI_ERROR (Status)) {
618 //
619 // We cannot find more legal port multiplier port number for ATA device
620 // on the port, then we are done.
621 //
622 break;
623 }
624 RegisterAtaDevice (AtaBusDriverData, Port, PortMultiplierPort);
625 }
626 }
627 Status = EFI_SUCCESS;
628 } else if (!IsDevicePathEnd (RemainingDevicePath)) {
629 Status = AtaPassThru->GetDevice (AtaPassThru, RemainingDevicePath, &Port, &PortMultiplierPort);
630 if (!EFI_ERROR (Status)) {
631 Status = RegisterAtaDevice (AtaBusDriverData,Port, PortMultiplierPort);
632 }
633 }
634
635 return Status;
636
637 ErrorExit:
638
639 if (AtaBusDriverData != NULL) {
640 gBS->UninstallMultipleProtocolInterfaces (
641 Controller,
642 &gEfiCallerIdGuid,
643 AtaBusDriverData,
644 NULL
645 );
646 FreePool (AtaBusDriverData);
647 }
648
649 gBS->CloseProtocol (
650 Controller,
651 &gEfiAtaPassThruProtocolGuid,
652 This->DriverBindingHandle,
653 Controller
654 );
655
656 return Status;
657
658 }
659
660
661 /**
662 Stops a device controller or a bus controller.
663
664 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
665 As a result, much of the error checking on the parameters to Stop() has been moved
666 into this common boot service. It is legal to call Stop() from other locations,
667 but the following calling restrictions must be followed or the system behavior will not be deterministic.
668 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
669 same driver's Start() function.
670 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
671 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
672 Start() function, and the Start() function must have called OpenProtocol() on
673 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
674
675 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
676 @param[in] ControllerHandle A handle to the device being stopped. The handle must
677 support a bus specific I/O protocol for the driver
678 to use to stop the device.
679 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
680 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
681 if NumberOfChildren is 0.
682
683 @retval EFI_SUCCESS The device was stopped.
684 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
685
686 **/
687 EFI_STATUS
688 EFIAPI
689 AtaBusDriverBindingStop (
690 IN EFI_DRIVER_BINDING_PROTOCOL *This,
691 IN EFI_HANDLE Controller,
692 IN UINTN NumberOfChildren,
693 IN EFI_HANDLE *ChildHandleBuffer
694 )
695 {
696 EFI_STATUS Status;
697 BOOLEAN AllChildrenStopped;
698 UINTN Index;
699 ATA_BUS_DRIVER_DATA *AtaBusDriverData;
700
701 if (NumberOfChildren == 0) {
702 Status = gBS->OpenProtocol (
703 Controller,
704 &gEfiCallerIdGuid,
705 (VOID **) &AtaBusDriverData,
706 This->DriverBindingHandle,
707 Controller,
708 EFI_OPEN_PROTOCOL_GET_PROTOCOL
709 );
710 if (!EFI_ERROR (Status)) {
711 gBS->UninstallMultipleProtocolInterfaces (
712 Controller,
713 &gEfiCallerIdGuid,
714 AtaBusDriverData,
715 NULL
716 );
717 FreePool (AtaBusDriverData);
718 }
719
720 gBS->CloseProtocol (
721 Controller,
722 &gEfiAtaPassThruProtocolGuid,
723 This->DriverBindingHandle,
724 Controller
725 );
726
727 return EFI_SUCCESS;
728 }
729
730 AllChildrenStopped = TRUE;
731
732 for (Index = 0; Index < NumberOfChildren; Index++) {
733
734 Status = UnregisterAtaDevice (This, Controller, ChildHandleBuffer[Index]);
735 if (EFI_ERROR (Status)) {
736 AllChildrenStopped = FALSE;
737 }
738 }
739
740 if (!AllChildrenStopped) {
741 return EFI_DEVICE_ERROR;
742 }
743
744 return EFI_SUCCESS;
745 }
746
747
748 /**
749 Reset the Block Device.
750
751 @param This Indicates a pointer to the calling context.
752 @param ExtendedVerification Driver may perform diagnostics on reset.
753
754 @retval EFI_SUCCESS The device was reset.
755 @retval EFI_DEVICE_ERROR The device is not functioning properly and could
756 not be reset.
757
758 **/
759 EFI_STATUS
760 EFIAPI
761 AtaBlockIoReset (
762 IN EFI_BLOCK_IO_PROTOCOL *This,
763 IN BOOLEAN ExtendedVerification
764 )
765 {
766 EFI_STATUS Status;
767 ATA_DEVICE *AtaDevice;
768 EFI_TPL OldTpl;
769
770 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
771
772 AtaDevice = ATA_DEVICE_FROM_BLOCK_IO (This);
773
774 Status = ResetAtaDevice (AtaDevice);
775
776 gBS->RestoreTPL (OldTpl);
777 return Status;
778 }
779
780
781 /**
782 Read/Write BufferSize bytes from Lba from/into Buffer.
783
784 @param This Indicates a pointer to the calling context.
785 @param MediaId The media ID that the read/write request is for.
786 @param Lba The starting logical block address to be read/written. The caller is
787 responsible for reading/writing to only legitimate locations.
788 @param BufferSize Size of Buffer, must be a multiple of device block size.
789 @param Buffer A pointer to the destination/source buffer for the data.
790 @param IsWrite Indicates whether it is a write operation.
791
792 @retval EFI_SUCCESS The data was read/written correctly to the device.
793 @retval EFI_WRITE_PROTECTED The device can not be read/written to.
794 @retval EFI_DEVICE_ERROR The device reported an error while performing the read/write.
795 @retval EFI_NO_MEDIA There is no media in the device.
796 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
797 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
798 @retval EFI_INVALID_PARAMETER The read/write request contains LBAs that are not valid,
799 or the buffer is not on proper alignment.
800
801 **/
802 EFI_STATUS
803 BlockIoReadWrite (
804 IN EFI_BLOCK_IO_PROTOCOL *This,
805 IN UINT32 MediaId,
806 IN EFI_LBA Lba,
807 IN UINTN BufferSize,
808 OUT VOID *Buffer,
809 IN BOOLEAN IsWrite
810 )
811 {
812 ATA_DEVICE *AtaDevice;
813 EFI_STATUS Status;
814 EFI_TPL OldTpl;
815 EFI_BLOCK_IO_MEDIA *Media;
816 UINTN BlockSize;
817 UINTN NumberOfBlocks;
818 UINTN IoAlign;
819
820 //
821 // Check parameters.
822 //
823 if (Buffer == NULL) {
824 return EFI_INVALID_PARAMETER;
825 }
826
827 if (BufferSize == 0) {
828 return EFI_SUCCESS;
829 }
830
831 Media = This->Media;
832 if (MediaId != Media->MediaId) {
833 return EFI_MEDIA_CHANGED;
834 }
835
836 BlockSize = Media->BlockSize;
837 if ((BufferSize % BlockSize) != 0) {
838 return EFI_BAD_BUFFER_SIZE;
839 }
840
841 NumberOfBlocks = BufferSize / BlockSize;
842 if ((Lba + NumberOfBlocks - 1) > Media->LastBlock) {
843 return EFI_INVALID_PARAMETER;
844 }
845
846 IoAlign = Media->IoAlign;
847 if (IoAlign > 0 && (((UINTN) Buffer & (IoAlign - 1)) != 0)) {
848 return EFI_INVALID_PARAMETER;
849 }
850
851 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
852
853 AtaDevice = ATA_DEVICE_FROM_BLOCK_IO (This);
854
855 //
856 // Invoke low level AtaDevice Access Routine.
857 //
858 Status = AccessAtaDevice (AtaDevice, Buffer, Lba, NumberOfBlocks, IsWrite);
859
860 gBS->RestoreTPL (OldTpl);
861
862 return Status;
863 }
864
865
866 /**
867 Read BufferSize bytes from Lba into Buffer.
868
869 @param This Indicates a pointer to the calling context.
870 @param MediaId Id of the media, changes every time the media is replaced.
871 @param Lba The starting Logical Block Address to read from
872 @param BufferSize Size of Buffer, must be a multiple of device block size.
873 @param Buffer A pointer to the destination buffer for the data. The caller is
874 responsible for either having implicit or explicit ownership of the buffer.
875
876 @retval EFI_SUCCESS The data was read correctly from the device.
877 @retval EFI_DEVICE_ERROR The device reported an error while performing the read.
878 @retval EFI_NO_MEDIA There is no media in the device.
879 @retval EFI_MEDIA_CHANGED The MediaId does not matched the current device.
880 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
881 @retval EFI_INVALID_PARAMETER The read request contains LBAs that are not valid,
882 or the buffer is not on proper alignment.
883
884 **/
885 EFI_STATUS
886 EFIAPI
887 AtaBlockIoReadBlocks (
888 IN EFI_BLOCK_IO_PROTOCOL *This,
889 IN UINT32 MediaId,
890 IN EFI_LBA Lba,
891 IN UINTN BufferSize,
892 OUT VOID *Buffer
893 )
894 {
895 return BlockIoReadWrite (This, MediaId, Lba, BufferSize, Buffer, FALSE);
896 }
897
898
899 /**
900 Write BufferSize bytes from Lba into Buffer.
901
902 @param This Indicates a pointer to the calling context.
903 @param MediaId The media ID that the write request is for.
904 @param Lba The starting logical block address to be written. The caller is
905 responsible for writing to only legitimate locations.
906 @param BufferSize Size of Buffer, must be a multiple of device block size.
907 @param Buffer A pointer to the source buffer for the data.
908
909 @retval EFI_SUCCESS The data was written correctly to the device.
910 @retval EFI_WRITE_PROTECTED The device can not be written to.
911 @retval EFI_DEVICE_ERROR The device reported an error while performing the write.
912 @retval EFI_NO_MEDIA There is no media in the device.
913 @retval EFI_MEDIA_CHNAGED The MediaId does not matched the current device.
914 @retval EFI_BAD_BUFFER_SIZE The Buffer was not a multiple of the block size of the device.
915 @retval EFI_INVALID_PARAMETER The write request contains LBAs that are not valid,
916 or the buffer is not on proper alignment.
917
918 **/
919 EFI_STATUS
920 EFIAPI
921 AtaBlockIoWriteBlocks (
922 IN EFI_BLOCK_IO_PROTOCOL *This,
923 IN UINT32 MediaId,
924 IN EFI_LBA Lba,
925 IN UINTN BufferSize,
926 IN VOID *Buffer
927 )
928 {
929 return BlockIoReadWrite (This, MediaId, Lba, BufferSize, Buffer, TRUE);
930 }
931
932
933 /**
934 Flush the Block Device.
935
936 @param This Indicates a pointer to the calling context.
937
938 @retval EFI_SUCCESS All outstanding data was written to the device
939 @retval EFI_DEVICE_ERROR The device reported an error while writing back the data
940 @retval EFI_NO_MEDIA There is no media in the device.
941
942 **/
943 EFI_STATUS
944 EFIAPI
945 AtaBlockIoFlushBlocks (
946 IN EFI_BLOCK_IO_PROTOCOL *This
947 )
948 {
949 //
950 // return directly
951 //
952 return EFI_SUCCESS;
953 }
954
955
956 /**
957 Provides inquiry information for the controller type.
958
959 This function is used by the IDE bus driver to get inquiry data. Data format
960 of Identify data is defined by the Interface GUID.
961
962 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
963 @param[in, out] InquiryData Pointer to a buffer for the inquiry data.
964 @param[in, out] InquiryDataSize Pointer to the value for the inquiry data size.
965
966 @retval EFI_SUCCESS The command was accepted without any errors.
967 @retval EFI_NOT_FOUND Device does not support this data class
968 @retval EFI_DEVICE_ERROR Error reading InquiryData from device
969 @retval EFI_BUFFER_TOO_SMALL InquiryDataSize not big enough
970
971 **/
972 EFI_STATUS
973 EFIAPI
974 AtaDiskInfoInquiry (
975 IN EFI_DISK_INFO_PROTOCOL *This,
976 IN OUT VOID *InquiryData,
977 IN OUT UINT32 *InquiryDataSize
978 )
979 {
980 return EFI_NOT_FOUND;
981 }
982
983
984 /**
985 Provides identify information for the controller type.
986
987 This function is used by the IDE bus driver to get identify data. Data format
988 of Identify data is defined by the Interface GUID.
989
990 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL
991 instance.
992 @param[in, out] IdentifyData Pointer to a buffer for the identify data.
993 @param[in, out] IdentifyDataSize Pointer to the value for the identify data
994 size.
995
996 @retval EFI_SUCCESS The command was accepted without any errors.
997 @retval EFI_NOT_FOUND Device does not support this data class
998 @retval EFI_DEVICE_ERROR Error reading IdentifyData from device
999 @retval EFI_BUFFER_TOO_SMALL IdentifyDataSize not big enough
1000
1001 **/
1002 EFI_STATUS
1003 EFIAPI
1004 AtaDiskInfoIdentify (
1005 IN EFI_DISK_INFO_PROTOCOL *This,
1006 IN OUT VOID *IdentifyData,
1007 IN OUT UINT32 *IdentifyDataSize
1008 )
1009 {
1010 EFI_STATUS Status;
1011 ATA_DEVICE *AtaDevice;
1012
1013 AtaDevice = ATA_DEVICE_FROM_DISK_INFO (This);
1014
1015 Status = EFI_BUFFER_TOO_SMALL;
1016 if (*IdentifyDataSize >= sizeof (*AtaDevice->IdentifyData)) {
1017 Status = EFI_SUCCESS;
1018 CopyMem (IdentifyData, AtaDevice->IdentifyData, sizeof (*AtaDevice->IdentifyData));
1019 }
1020 *IdentifyDataSize = sizeof (*AtaDevice->IdentifyData);
1021
1022 return Status;
1023 }
1024
1025
1026 /**
1027 Provides sense data information for the controller type.
1028
1029 This function is used by the IDE bus driver to get sense data.
1030 Data format of Sense data is defined by the Interface GUID.
1031
1032 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
1033 @param[in, out] SenseData Pointer to the SenseData.
1034 @param[in, out] SenseDataSize Size of SenseData in bytes.
1035 @param[out] SenseDataNumber Pointer to the value for the sense data size.
1036
1037 @retval EFI_SUCCESS The command was accepted without any errors.
1038 @retval EFI_NOT_FOUND Device does not support this data class.
1039 @retval EFI_DEVICE_ERROR Error reading SenseData from device.
1040 @retval EFI_BUFFER_TOO_SMALL SenseDataSize not big enough.
1041
1042 **/
1043 EFI_STATUS
1044 EFIAPI
1045 AtaDiskInfoSenseData (
1046 IN EFI_DISK_INFO_PROTOCOL *This,
1047 IN OUT VOID *SenseData,
1048 IN OUT UINT32 *SenseDataSize,
1049 OUT UINT8 *SenseDataNumber
1050 )
1051 {
1052 return EFI_NOT_FOUND;
1053 }
1054
1055
1056 /**
1057 This function is used by the IDE bus driver to get controller information.
1058
1059 @param[in] This Pointer to the EFI_DISK_INFO_PROTOCOL instance.
1060 @param[out] IdeChannel Pointer to the Ide Channel number. Primary or secondary.
1061 @param[out] IdeDevice Pointer to the Ide Device number. Master or slave.
1062
1063 @retval EFI_SUCCESS IdeChannel and IdeDevice are valid.
1064 @retval EFI_UNSUPPORTED This is not an IDE device.
1065
1066 **/
1067 EFI_STATUS
1068 EFIAPI
1069 AtaDiskInfoWhichIde (
1070 IN EFI_DISK_INFO_PROTOCOL *This,
1071 OUT UINT32 *IdeChannel,
1072 OUT UINT32 *IdeDevice
1073 )
1074 {
1075 ATA_DEVICE *AtaDevice;
1076
1077 AtaDevice = ATA_DEVICE_FROM_DISK_INFO (This);
1078 *IdeChannel = AtaDevice->Port;
1079 *IdeDevice = AtaDevice->PortMultiplierPort;
1080
1081 return EFI_SUCCESS;
1082 }
1083
1084
1085 /**
1086 The user Entry Point for module AtaBus. The user code starts with this function.
1087
1088 @param[in] ImageHandle The firmware allocated handle for the EFI image.
1089 @param[in] SystemTable A pointer to the EFI System Table.
1090
1091 @retval EFI_SUCCESS The entry point is executed successfully.
1092 @retval other Some error occurs when executing this entry point.
1093
1094 **/
1095 EFI_STATUS
1096 EFIAPI
1097 InitializeAtaBus(
1098 IN EFI_HANDLE ImageHandle,
1099 IN EFI_SYSTEM_TABLE *SystemTable
1100 )
1101 {
1102 EFI_STATUS Status;
1103
1104 //
1105 // Install driver model protocol(s).
1106 //
1107 Status = EfiLibInstallDriverBindingComponentName2 (
1108 ImageHandle,
1109 SystemTable,
1110 &gAtaBusDriverBinding,
1111 ImageHandle,
1112 &gAtaBusComponentName,
1113 &gAtaBusComponentName2
1114 );
1115 ASSERT_EFI_ERROR (Status);
1116
1117 return Status;
1118 }