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