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