MdeModulePkg/AtaAtapiPassThru: Revert patch to disable Bus Master
[mirror_edk2.git] / MdeModulePkg / Bus / Ata / AtaAtapiPassThru / AtaAtapiPassThru.c
1 /** @file
2 This file implements ATA_PASSTHRU_PROCTOCOL and EXT_SCSI_PASSTHRU_PROTOCOL interfaces
3 for managed ATA controllers.
4
5 Copyright (c) 2010 - 2016, Intel Corporation. All rights reserved.<BR>
6 This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "AtaAtapiPassThru.h"
17
18 //
19 // EFI_DRIVER_BINDING_PROTOCOL instance
20 //
21 EFI_DRIVER_BINDING_PROTOCOL gAtaAtapiPassThruDriverBinding = {
22 AtaAtapiPassThruSupported,
23 AtaAtapiPassThruStart,
24 AtaAtapiPassThruStop,
25 0x10,
26 NULL,
27 NULL
28 };
29
30 ATA_ATAPI_PASS_THRU_INSTANCE gAtaAtapiPassThruInstanceTemplate = {
31 ATA_ATAPI_PASS_THRU_SIGNATURE,
32 0, // Controller Handle
33 NULL, // PciIo Protocol
34 NULL, // IdeControllerInit Protocol
35 { // AtaPassThruMode
36 //
37 // According to UEFI2.3 spec Section 12.10, Drivers for non-RAID ATA controllers should set
38 // both EFI_ATA_PASS_THRU_ATTRIBUTES_PHYSICAL and EFI_ATA_PASS_THRU_ATTRIBUTES_LOGICAL
39 // bits.
40 // Note that the driver doesn't support AtaPassThru non blocking I/O.
41 //
42 EFI_ATA_PASS_THRU_ATTRIBUTES_PHYSICAL | EFI_ATA_PASS_THRU_ATTRIBUTES_LOGICAL | EFI_ATA_PASS_THRU_ATTRIBUTES_NONBLOCKIO,
43 //
44 // IoAlign
45 //
46 sizeof (UINTN)
47 },
48 { // AtaPassThru
49 NULL,
50 AtaPassThruPassThru,
51 AtaPassThruGetNextPort,
52 AtaPassThruGetNextDevice,
53 AtaPassThruBuildDevicePath,
54 AtaPassThruGetDevice,
55 AtaPassThruResetPort,
56 AtaPassThruResetDevice
57 },
58 { // ExtScsiPassThruMode
59 //
60 // AdapterId
61 //
62 0,
63 //
64 // According to UEFI2.3 spec Section 14.7, Drivers for non-RAID SCSI controllers should set
65 // both EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_PHYSICAL and EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL
66 // bits.
67 // Note that the driver doesn't support ExtScsiPassThru non blocking I/O.
68 //
69 EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_PHYSICAL | EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL,
70 //
71 // IoAlign
72 //
73 sizeof (UINTN)
74 },
75 { // ExtScsiPassThru
76 NULL,
77 ExtScsiPassThruPassThru,
78 ExtScsiPassThruGetNextTargetLun,
79 ExtScsiPassThruBuildDevicePath,
80 ExtScsiPassThruGetTargetLun,
81 ExtScsiPassThruResetChannel,
82 ExtScsiPassThruResetTargetLun,
83 ExtScsiPassThruGetNextTarget
84 },
85 EfiAtaUnknownMode, // Work Mode
86 { // IdeRegisters
87 {0},
88 {0}
89 },
90 { // AhciRegisters
91 0
92 },
93 { // DeviceList
94 NULL,
95 NULL
96 },
97 0, // EnabledPciAttributes
98 0, // OriginalAttributes
99 0, // PreviousPort
100 0, // PreviousPortMultiplier
101 0, // PreviousTargetId
102 0, // PreviousLun
103 NULL, // Timer event
104 { // NonBlocking TaskList
105 NULL,
106 NULL
107 },
108 NULL, // ExitBootEvent
109 };
110
111 ATAPI_DEVICE_PATH mAtapiDevicePathTemplate = {
112 {
113 MESSAGING_DEVICE_PATH,
114 MSG_ATAPI_DP,
115 {
116 (UINT8) (sizeof (ATAPI_DEVICE_PATH)),
117 (UINT8) ((sizeof (ATAPI_DEVICE_PATH)) >> 8)
118 }
119 },
120 0,
121 0,
122 0
123 };
124
125 SATA_DEVICE_PATH mSataDevicePathTemplate = {
126 {
127 MESSAGING_DEVICE_PATH,
128 MSG_SATA_DP,
129 {
130 (UINT8) (sizeof (SATA_DEVICE_PATH)),
131 (UINT8) ((sizeof (SATA_DEVICE_PATH)) >> 8)
132 }
133 },
134 0,
135 0,
136 0
137 };
138
139 UINT8 mScsiId[TARGET_MAX_BYTES] = {
140 0xFF, 0xFF, 0xFF, 0xFF,
141 0xFF, 0xFF, 0xFF, 0xFF,
142 0xFF, 0xFF, 0xFF, 0xFF,
143 0xFF, 0xFF, 0xFF, 0xFF
144 };
145
146 /**
147 Sends an ATA command to an ATA device that is attached to the ATA controller. This function
148 supports both blocking I/O and non-blocking I/O. The blocking I/O functionality is required,
149 and the non-blocking I/O functionality is optional.
150
151 @param[in] Port The port number of the ATA device to send the command.
152 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.
153 If there is no port multiplier, then specify 0xFFFF.
154 @param[in, out] Packet A pointer to the ATA command to send to the ATA device specified by Port
155 and PortMultiplierPort.
156 @param[in] Instance Pointer to the ATA_ATAPI_PASS_THRU_INSTANCE.
157 @param[in] Task Optional. Pointer to the ATA_NONBLOCK_TASK
158 used by non-blocking mode.
159
160 @retval EFI_SUCCESS The ATA command was sent by the host. For
161 bi-directional commands, InTransferLength bytes
162 were transferred from InDataBuffer. For
163 write and bi-directional commands, OutTransferLength
164 bytes were transferred by OutDataBuffer.
165 @retval EFI_BAD_BUFFER_SIZE The ATA command was not executed. The number
166 of bytes that could be transferred is returned
167 in InTransferLength. For write and bi-directional
168 commands, OutTransferLength bytes were transferred
169 by OutDataBuffer.
170 @retval EFI_NOT_READY The ATA command could not be sent because
171 there are too many ATA commands already
172 queued. The caller may retry again later.
173 @retval EFI_DEVICE_ERROR A device error occurred while attempting
174 to send the ATA command.
175 @retval EFI_INVALID_PARAMETER Port, PortMultiplierPort, or the contents
176 of Acb are invalid. The ATA command was
177 not sent, so no additional status information
178 is available.
179
180 **/
181 EFI_STATUS
182 EFIAPI
183 AtaPassThruPassThruExecute (
184 IN UINT16 Port,
185 IN UINT16 PortMultiplierPort,
186 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet,
187 IN ATA_ATAPI_PASS_THRU_INSTANCE *Instance,
188 IN ATA_NONBLOCK_TASK *Task OPTIONAL
189 )
190 {
191 EFI_ATA_PASS_THRU_CMD_PROTOCOL Protocol;
192 EFI_ATA_HC_WORK_MODE Mode;
193 EFI_STATUS Status;
194
195 Protocol = Packet->Protocol;
196
197 Mode = Instance->Mode;
198 switch (Mode) {
199 case EfiAtaIdeMode:
200 //
201 // Reassign IDE mode io port registers' base addresses
202 //
203 Status = GetIdeRegisterIoAddr (Instance->PciIo, Instance->IdeRegisters);
204
205 if (EFI_ERROR (Status)) {
206 return Status;
207 }
208
209 switch (Protocol) {
210 case EFI_ATA_PASS_THRU_PROTOCOL_ATA_NON_DATA:
211 Status = AtaNonDataCommandIn (
212 Instance->PciIo,
213 &Instance->IdeRegisters[Port],
214 Packet->Acb,
215 Packet->Asb,
216 Packet->Timeout,
217 Task
218 );
219 break;
220 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN:
221 Status = AtaPioDataInOut (
222 Instance->PciIo,
223 &Instance->IdeRegisters[Port],
224 Packet->InDataBuffer,
225 Packet->InTransferLength,
226 TRUE,
227 Packet->Acb,
228 Packet->Asb,
229 Packet->Timeout,
230 Task
231 );
232 break;
233 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT:
234 Status = AtaPioDataInOut (
235 Instance->PciIo,
236 &Instance->IdeRegisters[Port],
237 Packet->OutDataBuffer,
238 Packet->OutTransferLength,
239 FALSE,
240 Packet->Acb,
241 Packet->Asb,
242 Packet->Timeout,
243 Task
244 );
245 break;
246 case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_IN:
247 Status = AtaUdmaInOut (
248 Instance,
249 &Instance->IdeRegisters[Port],
250 TRUE,
251 Packet->InDataBuffer,
252 Packet->InTransferLength,
253 Packet->Acb,
254 Packet->Asb,
255 Packet->Timeout,
256 Task
257 );
258 break;
259 case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_OUT:
260 Status = AtaUdmaInOut (
261 Instance,
262 &Instance->IdeRegisters[Port],
263 FALSE,
264 Packet->OutDataBuffer,
265 Packet->OutTransferLength,
266 Packet->Acb,
267 Packet->Asb,
268 Packet->Timeout,
269 Task
270 );
271 break;
272 default :
273 return EFI_UNSUPPORTED;
274 }
275 break;
276 case EfiAtaAhciMode :
277 if (PortMultiplierPort == 0xFFFF) {
278 //
279 // If there is no port multiplier, PortMultiplierPort will be 0xFFFF
280 // according to UEFI spec. Here, we convert its value to 0 to follow
281 // AHCI spec.
282 //
283 PortMultiplierPort = 0;
284 }
285 switch (Protocol) {
286 case EFI_ATA_PASS_THRU_PROTOCOL_ATA_NON_DATA:
287 Status = AhciNonDataTransfer (
288 Instance->PciIo,
289 &Instance->AhciRegisters,
290 (UINT8)Port,
291 (UINT8)PortMultiplierPort,
292 NULL,
293 0,
294 Packet->Acb,
295 Packet->Asb,
296 Packet->Timeout,
297 Task
298 );
299 break;
300 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_IN:
301 Status = AhciPioTransfer (
302 Instance->PciIo,
303 &Instance->AhciRegisters,
304 (UINT8)Port,
305 (UINT8)PortMultiplierPort,
306 NULL,
307 0,
308 TRUE,
309 Packet->Acb,
310 Packet->Asb,
311 Packet->InDataBuffer,
312 Packet->InTransferLength,
313 Packet->Timeout,
314 Task
315 );
316 break;
317 case EFI_ATA_PASS_THRU_PROTOCOL_PIO_DATA_OUT:
318 Status = AhciPioTransfer (
319 Instance->PciIo,
320 &Instance->AhciRegisters,
321 (UINT8)Port,
322 (UINT8)PortMultiplierPort,
323 NULL,
324 0,
325 FALSE,
326 Packet->Acb,
327 Packet->Asb,
328 Packet->OutDataBuffer,
329 Packet->OutTransferLength,
330 Packet->Timeout,
331 Task
332 );
333 break;
334 case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_IN:
335 Status = AhciDmaTransfer (
336 Instance,
337 &Instance->AhciRegisters,
338 (UINT8)Port,
339 (UINT8)PortMultiplierPort,
340 NULL,
341 0,
342 TRUE,
343 Packet->Acb,
344 Packet->Asb,
345 Packet->InDataBuffer,
346 Packet->InTransferLength,
347 Packet->Timeout,
348 Task
349 );
350 break;
351 case EFI_ATA_PASS_THRU_PROTOCOL_UDMA_DATA_OUT:
352 Status = AhciDmaTransfer (
353 Instance,
354 &Instance->AhciRegisters,
355 (UINT8)Port,
356 (UINT8)PortMultiplierPort,
357 NULL,
358 0,
359 FALSE,
360 Packet->Acb,
361 Packet->Asb,
362 Packet->OutDataBuffer,
363 Packet->OutTransferLength,
364 Packet->Timeout,
365 Task
366 );
367 break;
368 default :
369 return EFI_UNSUPPORTED;
370 }
371 break;
372
373 default:
374 Status = EFI_DEVICE_ERROR;
375 break;
376 }
377
378 return Status;
379 }
380
381 /**
382 Call back function when the timer event is signaled.
383
384 @param[in] Event The Event this notify function registered to.
385 @param[in] Context Pointer to the context data registered to the
386 Event.
387
388 **/
389 VOID
390 EFIAPI
391 AsyncNonBlockingTransferRoutine (
392 EFI_EVENT Event,
393 VOID* Context
394 )
395 {
396 LIST_ENTRY *Entry;
397 LIST_ENTRY *EntryHeader;
398 ATA_NONBLOCK_TASK *Task;
399 EFI_STATUS Status;
400 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;
401
402 Instance = (ATA_ATAPI_PASS_THRU_INSTANCE *) Context;
403 EntryHeader = &Instance->NonBlockingTaskList;
404 //
405 // Get the Taks from the Taks List and execute it, until there is
406 // no task in the list or the device is busy with task (EFI_NOT_READY).
407 //
408 while (TRUE) {
409 if (!IsListEmpty (EntryHeader)) {
410 Entry = GetFirstNode (EntryHeader);
411 Task = ATA_NON_BLOCK_TASK_FROM_ENTRY (Entry);
412 } else {
413 return;
414 }
415
416 Status = AtaPassThruPassThruExecute (
417 Task->Port,
418 Task->PortMultiplier,
419 Task->Packet,
420 Instance,
421 Task
422 );
423
424 //
425 // If the data transfer meet a error, remove all tasks in the list since these tasks are
426 // associated with one task from Ata Bus and signal the event with error status.
427 //
428 if ((Status != EFI_NOT_READY) && (Status != EFI_SUCCESS)) {
429 DestroyAsynTaskList (Instance, TRUE);
430 break;
431 }
432
433 //
434 // For Non blocking mode, the Status of EFI_NOT_READY means the operation
435 // is not finished yet. Otherwise the operation is successful.
436 //
437 if (Status == EFI_NOT_READY) {
438 break;
439 } else {
440 RemoveEntryList (&Task->Link);
441 gBS->SignalEvent (Task->Event);
442 FreePool (Task);
443 }
444 }
445 }
446
447 /**
448 The Entry Point of module.
449
450 @param[in] ImageHandle The firmware allocated handle for the EFI image.
451 @param[in] SystemTable A pointer to the EFI System Table.
452
453 @retval EFI_SUCCESS The entry point is executed successfully.
454 @retval other Some error occurs when executing this entry point.
455
456 **/
457 EFI_STATUS
458 EFIAPI
459 InitializeAtaAtapiPassThru (
460 IN EFI_HANDLE ImageHandle,
461 IN EFI_SYSTEM_TABLE *SystemTable
462 )
463 {
464 EFI_STATUS Status;
465
466 //
467 // Install driver model protocol(s).
468 //
469 Status = EfiLibInstallDriverBindingComponentName2 (
470 ImageHandle,
471 SystemTable,
472 &gAtaAtapiPassThruDriverBinding,
473 ImageHandle,
474 &gAtaAtapiPassThruComponentName,
475 &gAtaAtapiPassThruComponentName2
476 );
477 ASSERT_EFI_ERROR (Status);
478
479 return Status;
480 }
481
482 /**
483 Disable the device (especially Bus Master DMA) when exiting the boot
484 services.
485
486 @param[in] Event Event for which this notification function is being
487 called.
488 @param[in] Context Pointer to the ATA_ATAPI_PASS_THRU_INSTANCE that
489 represents the HBA.
490 **/
491 VOID
492 EFIAPI
493 AtaPassThruExitBootServices (
494 IN EFI_EVENT Event,
495 IN VOID *Context
496 )
497 {
498 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;
499 EFI_PCI_IO_PROTOCOL *PciIo;
500
501 DEBUG ((DEBUG_VERBOSE, "%a: Context=0x%p\n", __FUNCTION__, Context));
502
503 Instance = Context;
504 PciIo = Instance->PciIo;
505
506 PciIo->Attributes (
507 PciIo,
508 EfiPciIoAttributeOperationDisable,
509 Instance->EnabledPciAttributes,
510 NULL
511 );
512 }
513
514 /**
515 Tests to see if this driver supports a given controller. If a child device is provided,
516 it further tests to see if this driver supports creating a handle for the specified child device.
517
518 This function checks to see if the driver specified by This supports the device specified by
519 ControllerHandle. Drivers will typically use the device path attached to
520 ControllerHandle and/or the services from the bus I/O abstraction attached to
521 ControllerHandle to determine if the driver supports ControllerHandle. This function
522 may be called many times during platform initialization. In order to reduce boot times, the tests
523 performed by this function must be very small, and take as little time as possible to execute. This
524 function must not change the state of any hardware devices, and this function must be aware that the
525 device specified by ControllerHandle may already be managed by the same driver or a
526 different driver. This function must match its calls to AllocatePages() with FreePages(),
527 AllocatePool() with FreePool(), and OpenProtocol() with CloseProtocol().
528 Because ControllerHandle may have been previously started by the same driver, if a protocol is
529 already in the opened state, then it must not be closed with CloseProtocol(). This is required
530 to guarantee the state of ControllerHandle is not modified by this function.
531
532 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
533 @param[in] ControllerHandle The handle of the controller to test. This handle
534 must support a protocol interface that supplies
535 an I/O abstraction to the driver.
536 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
537 parameter is ignored by device drivers, and is optional for bus
538 drivers. For bus drivers, if this parameter is not NULL, then
539 the bus driver must determine if the bus controller specified
540 by ControllerHandle and the child controller specified
541 by RemainingDevicePath are both supported by this
542 bus driver.
543
544 @retval EFI_SUCCESS The device specified by ControllerHandle and
545 RemainingDevicePath is supported by the driver specified by This.
546 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and
547 RemainingDevicePath is already being managed by the driver
548 specified by This.
549 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and
550 RemainingDevicePath is already being managed by a different
551 driver or an application that requires exclusive access.
552 Currently not implemented.
553 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and
554 RemainingDevicePath is not supported by the driver specified by This.
555 **/
556 EFI_STATUS
557 EFIAPI
558 AtaAtapiPassThruSupported (
559 IN EFI_DRIVER_BINDING_PROTOCOL *This,
560 IN EFI_HANDLE Controller,
561 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
562 )
563 {
564 EFI_STATUS Status;
565 EFI_DEVICE_PATH_PROTOCOL *ParentDevicePath;
566 EFI_PCI_IO_PROTOCOL *PciIo;
567 PCI_TYPE00 PciData;
568 EFI_IDE_CONTROLLER_INIT_PROTOCOL *IdeControllerInit;
569
570 //
571 // SATA Controller is a device driver, and should ingore the
572 // "RemainingDevicePath" according to UEFI spec
573 //
574 Status = gBS->OpenProtocol (
575 Controller,
576 &gEfiDevicePathProtocolGuid,
577 (VOID *) &ParentDevicePath,
578 This->DriverBindingHandle,
579 Controller,
580 EFI_OPEN_PROTOCOL_BY_DRIVER
581 );
582 if (EFI_ERROR (Status)) {
583 //
584 // EFI_ALREADY_STARTED is also an error
585 //
586 return Status;
587 }
588 //
589 // Close the protocol because we don't use it here
590 //
591 gBS->CloseProtocol (
592 Controller,
593 &gEfiDevicePathProtocolGuid,
594 This->DriverBindingHandle,
595 Controller
596 );
597
598 Status = gBS->OpenProtocol (
599 Controller,
600 &gEfiIdeControllerInitProtocolGuid,
601 (VOID **) &IdeControllerInit,
602 This->DriverBindingHandle,
603 Controller,
604 EFI_OPEN_PROTOCOL_BY_DRIVER
605 );
606
607 if (EFI_ERROR (Status)) {
608 //
609 // EFI_ALREADY_STARTED is also an error
610 //
611 return Status;
612 }
613
614 //
615 // Close the I/O Abstraction(s) used to perform the supported test
616 //
617 gBS->CloseProtocol (
618 Controller,
619 &gEfiIdeControllerInitProtocolGuid,
620 This->DriverBindingHandle,
621 Controller
622 );
623
624 //
625 // Now test the EfiPciIoProtocol
626 //
627 Status = gBS->OpenProtocol (
628 Controller,
629 &gEfiPciIoProtocolGuid,
630 (VOID **) &PciIo,
631 This->DriverBindingHandle,
632 Controller,
633 EFI_OPEN_PROTOCOL_GET_PROTOCOL
634 );
635 if (EFI_ERROR (Status)) {
636 return Status;
637 }
638 //
639 // Now further check the PCI header: Base class (offset 0x0B) and
640 // Sub Class (offset 0x0A). This controller should be an ATA controller
641 //
642 Status = PciIo->Pci.Read (
643 PciIo,
644 EfiPciIoWidthUint8,
645 PCI_CLASSCODE_OFFSET,
646 sizeof (PciData.Hdr.ClassCode),
647 PciData.Hdr.ClassCode
648 );
649 if (EFI_ERROR (Status)) {
650 return EFI_UNSUPPORTED;
651 }
652
653 if (IS_PCI_IDE (&PciData) || IS_PCI_SATADPA (&PciData)) {
654 return EFI_SUCCESS;
655 }
656
657 return EFI_UNSUPPORTED;
658 }
659
660 /**
661 Starts a device controller or a bus controller.
662
663 The Start() function is designed to be invoked from the EFI boot service ConnectController().
664 As a result, much of the error checking on the parameters to Start() has been moved into this
665 common boot service. It is legal to call Start() from other locations,
666 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
667 1. ControllerHandle must be a valid EFI_HANDLE.
668 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned
669 EFI_DEVICE_PATH_PROTOCOL.
670 3. Prior to calling Start(), the Supported() function for the driver specified by This must
671 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS.
672
673 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
674 @param[in] ControllerHandle The handle of the controller to start. This handle
675 must support a protocol interface that supplies
676 an I/O abstraction to the driver.
677 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. This
678 parameter is ignored by device drivers, and is optional for bus
679 drivers. For a bus driver, if this parameter is NULL, then handles
680 for all the children of Controller are created by this driver.
681 If this parameter is not NULL and the first Device Path Node is
682 not the End of Device Path Node, then only the handle for the
683 child device specified by the first Device Path Node of
684 RemainingDevicePath is created by this driver.
685 If the first Device Path Node of RemainingDevicePath is
686 the End of Device Path Node, no child handle is created by this
687 driver.
688
689 @retval EFI_SUCCESS The device was started.
690 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.Currently not implemented.
691 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.
692 @retval Others The driver failded to start the device.
693
694 **/
695 EFI_STATUS
696 EFIAPI
697 AtaAtapiPassThruStart (
698 IN EFI_DRIVER_BINDING_PROTOCOL *This,
699 IN EFI_HANDLE Controller,
700 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
701 )
702 {
703 EFI_STATUS Status;
704 EFI_IDE_CONTROLLER_INIT_PROTOCOL *IdeControllerInit;
705 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;
706 EFI_PCI_IO_PROTOCOL *PciIo;
707 UINT64 EnabledPciAttributes;
708 UINT64 OriginalPciAttributes;
709
710 Status = EFI_SUCCESS;
711 IdeControllerInit = NULL;
712 Instance = NULL;
713 OriginalPciAttributes = 0;
714
715 DEBUG ((EFI_D_INFO, "==AtaAtapiPassThru Start== Controller = %x\n", Controller));
716
717 Status = gBS->OpenProtocol (
718 Controller,
719 &gEfiIdeControllerInitProtocolGuid,
720 (VOID **) &IdeControllerInit,
721 This->DriverBindingHandle,
722 Controller,
723 EFI_OPEN_PROTOCOL_BY_DRIVER
724 );
725
726 if (EFI_ERROR (Status)) {
727 DEBUG ((EFI_D_ERROR, "Open Ide_Controller_Init Error, Status=%r", Status));
728 goto ErrorExit;
729 }
730
731 Status = gBS->OpenProtocol (
732 Controller,
733 &gEfiPciIoProtocolGuid,
734 (VOID **) &PciIo,
735 This->DriverBindingHandle,
736 Controller,
737 EFI_OPEN_PROTOCOL_GET_PROTOCOL
738 );
739 if (EFI_ERROR (Status)) {
740 DEBUG ((EFI_D_ERROR, "Get Pci_Io Protocol Error, Status=%r", Status));
741 goto ErrorExit;
742 }
743
744 Status = PciIo->Attributes (
745 PciIo,
746 EfiPciIoAttributeOperationGet,
747 0,
748 &OriginalPciAttributes
749 );
750
751 if (EFI_ERROR (Status)) {
752 goto ErrorExit;
753 }
754
755 Status = PciIo->Attributes (
756 PciIo,
757 EfiPciIoAttributeOperationSupported,
758 0,
759 &EnabledPciAttributes
760 );
761 if (!EFI_ERROR (Status)) {
762 EnabledPciAttributes &= (UINT64)EFI_PCI_DEVICE_ENABLE;
763 Status = PciIo->Attributes (
764 PciIo,
765 EfiPciIoAttributeOperationEnable,
766 EnabledPciAttributes,
767 NULL
768 );
769 }
770
771 if (EFI_ERROR (Status)) {
772 goto ErrorExit;
773 }
774
775 //
776 // Allocate a buffer to store the ATA_ATAPI_PASS_THRU_INSTANCE data structure
777 //
778 Instance = AllocateCopyPool (sizeof (ATA_ATAPI_PASS_THRU_INSTANCE), &gAtaAtapiPassThruInstanceTemplate);
779 if (Instance == NULL) {
780 goto ErrorExit;
781 }
782
783 Instance->ControllerHandle = Controller;
784 Instance->IdeControllerInit = IdeControllerInit;
785 Instance->PciIo = PciIo;
786 Instance->EnabledPciAttributes = EnabledPciAttributes;
787 Instance->OriginalPciAttributes = OriginalPciAttributes;
788 Instance->AtaPassThru.Mode = &Instance->AtaPassThruMode;
789 Instance->ExtScsiPassThru.Mode = &Instance->ExtScsiPassThruMode;
790 InitializeListHead(&Instance->DeviceList);
791 InitializeListHead(&Instance->NonBlockingTaskList);
792
793 Status = gBS->CreateEvent (
794 EVT_SIGNAL_EXIT_BOOT_SERVICES,
795 TPL_CALLBACK,
796 AtaPassThruExitBootServices,
797 Instance,
798 &Instance->ExitBootEvent
799 );
800 if (EFI_ERROR (Status)) {
801 goto ErrorExit;
802 }
803
804 Instance->TimerEvent = NULL;
805
806 Status = gBS->CreateEvent (
807 EVT_TIMER | EVT_NOTIFY_SIGNAL,
808 TPL_NOTIFY,
809 AsyncNonBlockingTransferRoutine,
810 Instance,
811 &Instance->TimerEvent
812 );
813 if (EFI_ERROR (Status)) {
814 goto ErrorExit;
815 }
816
817 //
818 // Set 1ms timer.
819 //
820 Status = gBS->SetTimer (Instance->TimerEvent, TimerPeriodic, 10000);
821 if (EFI_ERROR (Status)) {
822 goto ErrorExit;
823 }
824
825 //
826 // Enumerate all inserted ATA devices.
827 //
828 Status = EnumerateAttachedDevice (Instance);
829 if (EFI_ERROR (Status)) {
830 goto ErrorExit;
831 }
832
833 Status = gBS->InstallMultipleProtocolInterfaces (
834 &Controller,
835 &gEfiAtaPassThruProtocolGuid, &(Instance->AtaPassThru),
836 &gEfiExtScsiPassThruProtocolGuid, &(Instance->ExtScsiPassThru),
837 NULL
838 );
839 ASSERT_EFI_ERROR (Status);
840
841 return Status;
842
843 ErrorExit:
844 if (IdeControllerInit != NULL) {
845 gBS->CloseProtocol (
846 Controller,
847 &gEfiIdeControllerInitProtocolGuid,
848 This->DriverBindingHandle,
849 Controller
850 );
851 }
852
853 if ((Instance != NULL) && (Instance->TimerEvent != NULL)) {
854 gBS->CloseEvent (Instance->TimerEvent);
855 }
856
857 if ((Instance != NULL) && (Instance->ExitBootEvent != NULL)) {
858 gBS->CloseEvent (Instance->ExitBootEvent);
859 }
860
861 //
862 // Remove all inserted ATA devices.
863 //
864 DestroyDeviceInfoList(Instance);
865
866 if (Instance != NULL) {
867 FreePool (Instance);
868 }
869 return EFI_UNSUPPORTED;
870 }
871
872 /**
873 Stops a device controller or a bus controller.
874
875 The Stop() function is designed to be invoked from the EFI boot service DisconnectController().
876 As a result, much of the error checking on the parameters to Stop() has been moved
877 into this common boot service. It is legal to call Stop() from other locations,
878 but the following calling restrictions must be followed, or the system behavior will not be deterministic.
879 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this
880 same driver's Start() function.
881 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid
882 EFI_HANDLE. In addition, all of these handles must have been created in this driver's
883 Start() function, and the Start() function must have called OpenProtocol() on
884 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.
885
886 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
887 @param[in] ControllerHandle A handle to the device being stopped. The handle must
888 support a bus specific I/O protocol for the driver
889 to use to stop the device.
890 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.
891 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL
892 if NumberOfChildren is 0.
893
894 @retval EFI_SUCCESS The device was stopped.
895 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
896
897 **/
898 EFI_STATUS
899 EFIAPI
900 AtaAtapiPassThruStop (
901 IN EFI_DRIVER_BINDING_PROTOCOL *This,
902 IN EFI_HANDLE Controller,
903 IN UINTN NumberOfChildren,
904 IN EFI_HANDLE *ChildHandleBuffer
905 )
906 {
907 EFI_STATUS Status;
908 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;
909 EFI_ATA_PASS_THRU_PROTOCOL *AtaPassThru;
910 EFI_PCI_IO_PROTOCOL *PciIo;
911 EFI_AHCI_REGISTERS *AhciRegisters;
912
913 DEBUG ((EFI_D_INFO, "==AtaAtapiPassThru Stop== Controller = %x\n", Controller));
914
915 Status = gBS->OpenProtocol (
916 Controller,
917 &gEfiAtaPassThruProtocolGuid,
918 (VOID **) &AtaPassThru,
919 This->DriverBindingHandle,
920 Controller,
921 EFI_OPEN_PROTOCOL_GET_PROTOCOL
922 );
923
924 if (EFI_ERROR (Status)) {
925 return EFI_DEVICE_ERROR;
926 }
927
928 Instance = ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (AtaPassThru);
929
930 Status = gBS->UninstallMultipleProtocolInterfaces (
931 Controller,
932 &gEfiAtaPassThruProtocolGuid, &(Instance->AtaPassThru),
933 &gEfiExtScsiPassThruProtocolGuid, &(Instance->ExtScsiPassThru),
934 NULL
935 );
936
937 if (EFI_ERROR (Status)) {
938 return EFI_DEVICE_ERROR;
939 }
940
941 //
942 // Close protocols opened by AtaAtapiPassThru controller driver
943 //
944 gBS->CloseProtocol (
945 Controller,
946 &gEfiIdeControllerInitProtocolGuid,
947 This->DriverBindingHandle,
948 Controller
949 );
950
951 //
952 // Close Non-Blocking timer and free Task list.
953 //
954 if (Instance->TimerEvent != NULL) {
955 gBS->CloseEvent (Instance->TimerEvent);
956 Instance->TimerEvent = NULL;
957 }
958 DestroyAsynTaskList (Instance, FALSE);
959
960 //
961 // Close event signaled at gBS->ExitBootServices().
962 //
963 if (Instance->ExitBootEvent != NULL) {
964 gBS->CloseEvent (Instance->ExitBootEvent);
965 Instance->ExitBootEvent = NULL;
966 }
967
968 //
969 // Free allocated resource
970 //
971 DestroyDeviceInfoList (Instance);
972
973 PciIo = Instance->PciIo;
974
975 //
976 // Disable this ATA host controller.
977 //
978 PciIo->Attributes (
979 PciIo,
980 EfiPciIoAttributeOperationDisable,
981 Instance->EnabledPciAttributes,
982 NULL
983 );
984
985 //
986 // If the current working mode is AHCI mode, then pre-allocated resource
987 // for AHCI initialization should be released.
988 //
989 if (Instance->Mode == EfiAtaAhciMode) {
990 AhciRegisters = &Instance->AhciRegisters;
991 PciIo->Unmap (
992 PciIo,
993 AhciRegisters->MapCommandTable
994 );
995 PciIo->FreeBuffer (
996 PciIo,
997 EFI_SIZE_TO_PAGES ((UINTN) AhciRegisters->MaxCommandTableSize),
998 AhciRegisters->AhciCommandTable
999 );
1000 PciIo->Unmap (
1001 PciIo,
1002 AhciRegisters->MapCmdList
1003 );
1004 PciIo->FreeBuffer (
1005 PciIo,
1006 EFI_SIZE_TO_PAGES ((UINTN) AhciRegisters->MaxCommandListSize),
1007 AhciRegisters->AhciCmdList
1008 );
1009 PciIo->Unmap (
1010 PciIo,
1011 AhciRegisters->MapRFis
1012 );
1013 PciIo->FreeBuffer (
1014 PciIo,
1015 EFI_SIZE_TO_PAGES ((UINTN) AhciRegisters->MaxReceiveFisSize),
1016 AhciRegisters->AhciRFis
1017 );
1018 }
1019
1020 //
1021 // Restore original PCI attributes
1022 //
1023 Status = PciIo->Attributes (
1024 PciIo,
1025 EfiPciIoAttributeOperationSet,
1026 Instance->OriginalPciAttributes,
1027 NULL
1028 );
1029 ASSERT_EFI_ERROR (Status);
1030
1031 FreePool (Instance);
1032
1033 return Status;
1034 }
1035
1036 /**
1037 Traverse the attached ATA devices list to find out the device to access.
1038
1039 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1040 @param[in] Port The port number of the ATA device to send the command.
1041 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.
1042 If there is no port multiplier, then specify 0xFFFF.
1043 @param[in] DeviceType The device type of the ATA device.
1044
1045 @retval The pointer to the data structure of the device info to access.
1046
1047 **/
1048 LIST_ENTRY *
1049 EFIAPI
1050 SearchDeviceInfoList (
1051 IN ATA_ATAPI_PASS_THRU_INSTANCE *Instance,
1052 IN UINT16 Port,
1053 IN UINT16 PortMultiplier,
1054 IN EFI_ATA_DEVICE_TYPE DeviceType
1055 )
1056 {
1057 EFI_ATA_DEVICE_INFO *DeviceInfo;
1058 LIST_ENTRY *Node;
1059
1060 Node = GetFirstNode (&Instance->DeviceList);
1061 while (!IsNull (&Instance->DeviceList, Node)) {
1062 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);
1063
1064 //
1065 // For CD-ROM working in the AHCI mode, only 8 bits are used to record
1066 // the PortMultiplier information. If the CD-ROM is directly attached
1067 // on a SATA port, the PortMultiplier should be translated from 0xFF
1068 // to 0xFFFF according to the UEFI spec.
1069 //
1070 if ((Instance->Mode == EfiAtaAhciMode) &&
1071 (DeviceInfo->Type == EfiIdeCdrom) &&
1072 (PortMultiplier == 0xFF)) {
1073 PortMultiplier = 0xFFFF;
1074 }
1075
1076 if ((DeviceInfo->Type == DeviceType) &&
1077 (Port == DeviceInfo->Port) &&
1078 (PortMultiplier == DeviceInfo->PortMultiplier)) {
1079 return Node;
1080 }
1081
1082 Node = GetNextNode (&Instance->DeviceList, Node);
1083 }
1084
1085 return NULL;
1086 }
1087
1088 /**
1089 Allocate device info data structure to contain device info.
1090 And insert the data structure to the tail of device list for tracing.
1091
1092 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1093 @param[in] Port The port number of the ATA device to send the command.
1094 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.
1095 If there is no port multiplier, then specify 0xFFFF.
1096 @param[in] DeviceType The device type of the ATA device.
1097 @param[in] IdentifyData The data buffer to store the output of the IDENTIFY cmd.
1098
1099 @retval EFI_SUCCESS Successfully insert the ata device to the tail of device list.
1100 @retval EFI_OUT_OF_RESOURCES Can not allocate enough resource for use.
1101
1102 **/
1103 EFI_STATUS
1104 EFIAPI
1105 CreateNewDeviceInfo (
1106 IN ATA_ATAPI_PASS_THRU_INSTANCE *Instance,
1107 IN UINT16 Port,
1108 IN UINT16 PortMultiplier,
1109 IN EFI_ATA_DEVICE_TYPE DeviceType,
1110 IN EFI_IDENTIFY_DATA *IdentifyData
1111 )
1112 {
1113 EFI_ATA_DEVICE_INFO *DeviceInfo;
1114
1115 DeviceInfo = AllocateZeroPool (sizeof (EFI_ATA_DEVICE_INFO));
1116
1117 if (DeviceInfo == NULL) {
1118 return EFI_OUT_OF_RESOURCES;
1119 }
1120
1121 DeviceInfo->Signature = ATA_ATAPI_DEVICE_SIGNATURE;
1122 DeviceInfo->Port = Port;
1123 DeviceInfo->PortMultiplier = PortMultiplier;
1124 DeviceInfo->Type = DeviceType;
1125
1126 if (IdentifyData != NULL) {
1127 DeviceInfo->IdentifyData = AllocateCopyPool (sizeof (EFI_IDENTIFY_DATA), IdentifyData);
1128 if (DeviceInfo->IdentifyData == NULL) {
1129 FreePool (DeviceInfo);
1130 return EFI_OUT_OF_RESOURCES;
1131 }
1132 }
1133
1134 InsertTailList (&Instance->DeviceList, &DeviceInfo->Link);
1135
1136 return EFI_SUCCESS;
1137 }
1138
1139 /**
1140 Destroy all attached ATA devices info.
1141
1142 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1143
1144 **/
1145 VOID
1146 EFIAPI
1147 DestroyDeviceInfoList (
1148 IN ATA_ATAPI_PASS_THRU_INSTANCE *Instance
1149 )
1150 {
1151 EFI_ATA_DEVICE_INFO *DeviceInfo;
1152 LIST_ENTRY *Node;
1153
1154 Node = GetFirstNode (&Instance->DeviceList);
1155 while (!IsNull (&Instance->DeviceList, Node)) {
1156 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);
1157
1158 Node = GetNextNode (&Instance->DeviceList, Node);
1159
1160 RemoveEntryList (&DeviceInfo->Link);
1161 if (DeviceInfo->IdentifyData != NULL) {
1162 FreePool (DeviceInfo->IdentifyData);
1163 }
1164 FreePool (DeviceInfo);
1165 }
1166 }
1167
1168 /**
1169 Destroy all pending non blocking tasks.
1170
1171 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1172 @param[in] IsSigEvent Indicate whether signal the task event when remove the
1173 task.
1174
1175 **/
1176 VOID
1177 EFIAPI
1178 DestroyAsynTaskList (
1179 IN ATA_ATAPI_PASS_THRU_INSTANCE *Instance,
1180 IN BOOLEAN IsSigEvent
1181 )
1182 {
1183 LIST_ENTRY *Entry;
1184 LIST_ENTRY *DelEntry;
1185 ATA_NONBLOCK_TASK *Task;
1186 EFI_TPL OldTpl;
1187
1188 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
1189 if (!IsListEmpty (&Instance->NonBlockingTaskList)) {
1190 //
1191 // Free the Subtask list.
1192 //
1193 for (Entry = (&Instance->NonBlockingTaskList)->ForwardLink;
1194 Entry != (&Instance->NonBlockingTaskList);
1195 ) {
1196 DelEntry = Entry;
1197 Entry = Entry->ForwardLink;
1198 Task = ATA_NON_BLOCK_TASK_FROM_ENTRY (DelEntry);
1199
1200 RemoveEntryList (DelEntry);
1201 if (IsSigEvent) {
1202 Task->Packet->Asb->AtaStatus = 0x01;
1203 gBS->SignalEvent (Task->Event);
1204 }
1205 FreePool (Task);
1206 }
1207 }
1208 gBS->RestoreTPL (OldTpl);
1209 }
1210
1211 /**
1212 Enumerate all attached ATA devices at IDE mode or AHCI mode separately.
1213
1214 The function is designed to enumerate all attached ATA devices.
1215
1216 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.
1217
1218 @retval EFI_SUCCESS Successfully enumerate attached ATA devices.
1219 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.
1220
1221 **/
1222 EFI_STATUS
1223 EFIAPI
1224 EnumerateAttachedDevice (
1225 IN ATA_ATAPI_PASS_THRU_INSTANCE *Instance
1226 )
1227 {
1228 EFI_STATUS Status;
1229 PCI_TYPE00 PciData;
1230 UINT8 ClassCode;
1231
1232 Status = EFI_SUCCESS;
1233
1234 Status = Instance->PciIo->Pci.Read (
1235 Instance->PciIo,
1236 EfiPciIoWidthUint8,
1237 PCI_CLASSCODE_OFFSET,
1238 sizeof (PciData.Hdr.ClassCode),
1239 PciData.Hdr.ClassCode
1240 );
1241 ASSERT_EFI_ERROR (Status);
1242
1243 ClassCode = PciData.Hdr.ClassCode[1];
1244
1245 switch (ClassCode) {
1246 case PCI_CLASS_MASS_STORAGE_IDE :
1247 //
1248 // The ATA controller is working at IDE mode
1249 //
1250 Instance->Mode = EfiAtaIdeMode;
1251
1252 Status = IdeModeInitialization (Instance);
1253 if (EFI_ERROR (Status)) {
1254 Status = EFI_DEVICE_ERROR;
1255 goto Done;
1256 }
1257 break;
1258 case PCI_CLASS_MASS_STORAGE_SATADPA :
1259 //
1260 // The ATA controller is working at AHCI mode
1261 //
1262 Instance->Mode = EfiAtaAhciMode;
1263
1264 Status = AhciModeInitialization (Instance);
1265
1266 if (EFI_ERROR (Status)) {
1267 Status = EFI_DEVICE_ERROR;
1268 goto Done;
1269 }
1270
1271 break;
1272 default :
1273 Status = EFI_UNSUPPORTED;
1274 }
1275
1276 Done:
1277 return Status;
1278 }
1279
1280 /**
1281 Sends an ATA command to an ATA device that is attached to the ATA controller. This function
1282 supports both blocking I/O and non-blocking I/O. The blocking I/O functionality is required,
1283 and the non-blocking I/O functionality is optional.
1284
1285 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1286 @param[in] Port The port number of the ATA device to send the command.
1287 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.
1288 If there is no port multiplier, then specify 0xFFFF.
1289 @param[in, out] Packet A pointer to the ATA command to send to the ATA device specified by Port
1290 and PortMultiplierPort.
1291 @param[in] Event If non-blocking I/O is not supported then Event is ignored, and blocking
1292 I/O is performed. If Event is NULL, then blocking I/O is performed. If
1293 Event is not NULL and non blocking I/O is supported, then non-blocking
1294 I/O is performed, and Event will be signaled when the ATA command completes.
1295
1296 @retval EFI_SUCCESS The ATA command was sent by the host. For bi-directional commands,
1297 InTransferLength bytes were transferred from InDataBuffer. For write and
1298 bi-directional commands, OutTransferLength bytes were transferred by OutDataBuffer.
1299 @retval EFI_BAD_BUFFER_SIZE The ATA command was not executed. The number of bytes that could be transferred
1300 is returned in InTransferLength. For write and bi-directional commands,
1301 OutTransferLength bytes were transferred by OutDataBuffer.
1302 @retval EFI_NOT_READY The ATA command could not be sent because there are too many ATA commands
1303 already queued. The caller may retry again later.
1304 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the ATA command.
1305 @retval EFI_INVALID_PARAMETER Port, PortMultiplierPort, or the contents of Acb are invalid. The ATA
1306 command was not sent, so no additional status information is available.
1307
1308 **/
1309 EFI_STATUS
1310 EFIAPI
1311 AtaPassThruPassThru (
1312 IN EFI_ATA_PASS_THRU_PROTOCOL *This,
1313 IN UINT16 Port,
1314 IN UINT16 PortMultiplierPort,
1315 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet,
1316 IN EFI_EVENT Event OPTIONAL
1317 )
1318 {
1319 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;
1320 LIST_ENTRY *Node;
1321 EFI_ATA_DEVICE_INFO *DeviceInfo;
1322 EFI_IDENTIFY_DATA *IdentifyData;
1323 UINT64 Capacity;
1324 UINT32 MaxSectorCount;
1325 ATA_NONBLOCK_TASK *Task;
1326 EFI_TPL OldTpl;
1327 UINT32 BlockSize;
1328
1329 Instance = ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);
1330
1331 if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->InDataBuffer, This->Mode->IoAlign)) {
1332 return EFI_INVALID_PARAMETER;
1333 }
1334
1335 if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->OutDataBuffer, This->Mode->IoAlign)) {
1336 return EFI_INVALID_PARAMETER;
1337 }
1338
1339 if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->Asb, This->Mode->IoAlign)) {
1340 return EFI_INVALID_PARAMETER;
1341 }
1342
1343 Node = SearchDeviceInfoList (Instance, Port, PortMultiplierPort, EfiIdeHarddisk);
1344
1345 if (Node == NULL) {
1346 Node = SearchDeviceInfoList(Instance, Port, PortMultiplierPort, EfiIdeCdrom);
1347 if (Node == NULL) {
1348 return EFI_INVALID_PARAMETER;
1349 }
1350 }
1351
1352 //
1353 // Check whether this device needs 48-bit addressing (ATAPI-6 ata device).
1354 // Per ATA-6 spec, word83: bit15 is zero and bit14 is one.
1355 // If bit10 is one, it means the ata device support 48-bit addressing.
1356 //
1357 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);
1358 IdentifyData = DeviceInfo->IdentifyData;
1359 MaxSectorCount = 0x100;
1360 if ((IdentifyData->AtaData.command_set_supported_83 & (BIT10 | BIT15 | BIT14)) == 0x4400) {
1361 Capacity = *((UINT64 *)IdentifyData->AtaData.maximum_lba_for_48bit_addressing);
1362 if (Capacity > 0xFFFFFFF) {
1363 //
1364 // Capacity exceeds 120GB. 48-bit addressing is really needed
1365 // In this case, the max sector count is 0x10000
1366 //
1367 MaxSectorCount = 0x10000;
1368 }
1369 }
1370
1371 BlockSize = 0x200;
1372 if ((IdentifyData->AtaData.phy_logic_sector_support & (BIT14 | BIT15)) == BIT14) {
1373 //
1374 // Check logical block size
1375 //
1376 if ((IdentifyData->AtaData.phy_logic_sector_support & BIT12) != 0) {
1377 BlockSize = (UINT32) (((IdentifyData->AtaData.logic_sector_size_hi << 16) | IdentifyData->AtaData.logic_sector_size_lo) * sizeof (UINT16));
1378 }
1379 }
1380
1381 //
1382 // convert the transfer length from sector count to byte.
1383 //
1384 if (((Packet->Length & EFI_ATA_PASS_THRU_LENGTH_BYTES) == 0) &&
1385 (Packet->InTransferLength != 0)) {
1386 Packet->InTransferLength = Packet->InTransferLength * BlockSize;
1387 }
1388
1389 //
1390 // convert the transfer length from sector count to byte.
1391 //
1392 if (((Packet->Length & EFI_ATA_PASS_THRU_LENGTH_BYTES) == 0) &&
1393 (Packet->OutTransferLength != 0)) {
1394 Packet->OutTransferLength = Packet->OutTransferLength * BlockSize;
1395 }
1396
1397 //
1398 // If the data buffer described by InDataBuffer/OutDataBuffer and InTransferLength/OutTransferLength
1399 // is too big to be transferred in a single command, then no data is transferred and EFI_BAD_BUFFER_SIZE
1400 // is returned.
1401 //
1402 if (((Packet->InTransferLength != 0) && (Packet->InTransferLength > MaxSectorCount * BlockSize)) ||
1403 ((Packet->OutTransferLength != 0) && (Packet->OutTransferLength > MaxSectorCount * BlockSize))) {
1404 return EFI_BAD_BUFFER_SIZE;
1405 }
1406
1407 //
1408 // For non-blocking mode, queue the Task into the list.
1409 //
1410 if (Event != NULL) {
1411 Task = AllocateZeroPool (sizeof (ATA_NONBLOCK_TASK));
1412 if (Task == NULL) {
1413 return EFI_OUT_OF_RESOURCES;
1414 }
1415
1416 Task->Signature = ATA_NONBLOCKING_TASK_SIGNATURE;
1417 Task->Port = Port;
1418 Task->PortMultiplier = PortMultiplierPort;
1419 Task->Packet = Packet;
1420 Task->Event = Event;
1421 Task->IsStart = FALSE;
1422 Task->RetryTimes = DivU64x32(Packet->Timeout, 1000) + 1;
1423 if (Packet->Timeout == 0) {
1424 Task->InfiniteWait = TRUE;
1425 } else {
1426 Task->InfiniteWait = FALSE;
1427 }
1428
1429 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);
1430 InsertTailList (&Instance->NonBlockingTaskList, &Task->Link);
1431 gBS->RestoreTPL (OldTpl);
1432
1433 return EFI_SUCCESS;
1434 } else {
1435 return AtaPassThruPassThruExecute (
1436 Port,
1437 PortMultiplierPort,
1438 Packet,
1439 Instance,
1440 NULL
1441 );
1442 }
1443 }
1444
1445 /**
1446 Used to retrieve the list of legal port numbers for ATA devices on an ATA controller.
1447 These can either be the list of ports where ATA devices are actually present or the
1448 list of legal port numbers for the ATA controller. Regardless, the caller of this
1449 function must probe the port number returned to see if an ATA device is actually
1450 present at that location on the ATA controller.
1451
1452 The GetNextPort() function retrieves the port number on an ATA controller. If on input
1453 Port is 0xFFFF, then the port number of the first port on the ATA controller is returned
1454 in Port and EFI_SUCCESS is returned.
1455
1456 If Port is a port number that was returned on a previous call to GetNextPort(), then the
1457 port number of the next port on the ATA controller is returned in Port, and EFI_SUCCESS
1458 is returned. If Port is not 0xFFFF and Port was not returned on a previous call to
1459 GetNextPort(), then EFI_INVALID_PARAMETER is returned.
1460
1461 If Port is the port number of the last port on the ATA controller, then EFI_NOT_FOUND is
1462 returned.
1463
1464 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1465 @param[in, out] Port On input, a pointer to the port number on the ATA controller.
1466 On output, a pointer to the next port number on the ATA
1467 controller. An input value of 0xFFFF retrieves the first port
1468 number on the ATA controller.
1469
1470 @retval EFI_SUCCESS The next port number on the ATA controller was returned in Port.
1471 @retval EFI_NOT_FOUND There are no more ports on this ATA controller.
1472 @retval EFI_INVALID_PARAMETER Port is not 0xFFFF and Port was not returned on a previous call
1473 to GetNextPort().
1474
1475 **/
1476 EFI_STATUS
1477 EFIAPI
1478 AtaPassThruGetNextPort (
1479 IN EFI_ATA_PASS_THRU_PROTOCOL *This,
1480 IN OUT UINT16 *Port
1481 )
1482 {
1483 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;
1484 LIST_ENTRY *Node;
1485 EFI_ATA_DEVICE_INFO *DeviceInfo;
1486
1487 Instance = ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);
1488
1489 if (Port == NULL) {
1490 return EFI_INVALID_PARAMETER;
1491 }
1492
1493 if (*Port == 0xFFFF) {
1494 //
1495 // If the Port is all 0xFF's, start to traverse the device list from the beginning
1496 //
1497 Node = GetFirstNode (&Instance->DeviceList);
1498
1499 while (!IsNull (&Instance->DeviceList, Node)) {
1500 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);
1501
1502 if (DeviceInfo->Type == EfiIdeHarddisk) {
1503 *Port = DeviceInfo->Port;
1504 goto Exit;
1505 }
1506
1507 Node = GetNextNode (&Instance->DeviceList, Node);
1508 }
1509
1510 return EFI_NOT_FOUND;
1511 } else if (*Port == Instance->PreviousPort) {
1512 Node = GetFirstNode (&Instance->DeviceList);
1513
1514 while (!IsNull (&Instance->DeviceList, Node)) {
1515 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);
1516
1517 if ((DeviceInfo->Type == EfiIdeHarddisk) &&
1518 (DeviceInfo->Port > *Port)){
1519 *Port = DeviceInfo->Port;
1520 goto Exit;
1521 }
1522
1523 Node = GetNextNode (&Instance->DeviceList, Node);
1524 }
1525
1526 return EFI_NOT_FOUND;
1527 } else {
1528 //
1529 // Port is not equal to 0xFFFF and also not equal to previous return value
1530 //
1531 return EFI_INVALID_PARAMETER;
1532 }
1533
1534 Exit:
1535 //
1536 // Update the PreviousPort and PreviousPortMultiplier.
1537 //
1538 Instance->PreviousPort = *Port;
1539
1540 return EFI_SUCCESS;
1541 }
1542
1543 /**
1544 Used to retrieve the list of legal port multiplier port numbers for ATA devices on a port of an ATA
1545 controller. These can either be the list of port multiplier ports where ATA devices are actually
1546 present on port or the list of legal port multiplier ports on that port. Regardless, the caller of this
1547 function must probe the port number and port multiplier port number returned to see if an ATA
1548 device is actually present.
1549
1550 The GetNextDevice() function retrieves the port multiplier port number of an ATA device
1551 present on a port of an ATA controller.
1552
1553 If PortMultiplierPort points to a port multiplier port number value that was returned on a
1554 previous call to GetNextDevice(), then the port multiplier port number of the next ATA device
1555 on the port of the ATA controller is returned in PortMultiplierPort, and EFI_SUCCESS is
1556 returned.
1557
1558 If PortMultiplierPort points to 0xFFFF, then the port multiplier port number of the first
1559 ATA device on port of the ATA controller is returned in PortMultiplierPort and
1560 EFI_SUCCESS is returned.
1561
1562 If PortMultiplierPort is not 0xFFFF and the value pointed to by PortMultiplierPort
1563 was not returned on a previous call to GetNextDevice(), then EFI_INVALID_PARAMETER
1564 is returned.
1565
1566 If PortMultiplierPort is the port multiplier port number of the last ATA device on the port of
1567 the ATA controller, then EFI_NOT_FOUND is returned.
1568
1569 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1570 @param[in] Port The port number present on the ATA controller.
1571 @param[in, out] PortMultiplierPort On input, a pointer to the port multiplier port number of an
1572 ATA device present on the ATA controller.
1573 If on input a PortMultiplierPort of 0xFFFF is specified,
1574 then the port multiplier port number of the first ATA device
1575 is returned. On output, a pointer to the port multiplier port
1576 number of the next ATA device present on an ATA controller.
1577
1578 @retval EFI_SUCCESS The port multiplier port number of the next ATA device on the port
1579 of the ATA controller was returned in PortMultiplierPort.
1580 @retval EFI_NOT_FOUND There are no more ATA devices on this port of the ATA controller.
1581 @retval EFI_INVALID_PARAMETER PortMultiplierPort is not 0xFFFF, and PortMultiplierPort was not
1582 returned on a previous call to GetNextDevice().
1583
1584 **/
1585 EFI_STATUS
1586 EFIAPI
1587 AtaPassThruGetNextDevice (
1588 IN EFI_ATA_PASS_THRU_PROTOCOL *This,
1589 IN UINT16 Port,
1590 IN OUT UINT16 *PortMultiplierPort
1591 )
1592 {
1593 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;
1594 LIST_ENTRY *Node;
1595 EFI_ATA_DEVICE_INFO *DeviceInfo;
1596
1597 Instance = ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);
1598
1599 if (PortMultiplierPort == NULL) {
1600 return EFI_INVALID_PARAMETER;
1601 }
1602
1603 if (Instance->PreviousPortMultiplier == 0xFFFF) {
1604 //
1605 // If a device is directly attached on a port, previous call to this
1606 // function will return the value 0xFFFF for PortMultiplierPort. In
1607 // this case, there should be no more device on the port multiplier.
1608 //
1609 Instance->PreviousPortMultiplier = 0;
1610 return EFI_NOT_FOUND;
1611 }
1612
1613 if (*PortMultiplierPort == Instance->PreviousPortMultiplier) {
1614 Node = GetFirstNode (&Instance->DeviceList);
1615
1616 while (!IsNull (&Instance->DeviceList, Node)) {
1617 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);
1618
1619 if ((DeviceInfo->Type == EfiIdeHarddisk) &&
1620 (DeviceInfo->Port == Port) &&
1621 (DeviceInfo->PortMultiplier > *PortMultiplierPort)){
1622 *PortMultiplierPort = DeviceInfo->PortMultiplier;
1623 goto Exit;
1624 }
1625
1626 Node = GetNextNode (&Instance->DeviceList, Node);
1627 }
1628
1629 return EFI_NOT_FOUND;
1630 } else if (*PortMultiplierPort == 0xFFFF) {
1631 //
1632 // If the PortMultiplierPort is all 0xFF's, start to traverse the device list from the beginning
1633 //
1634 Node = GetFirstNode (&Instance->DeviceList);
1635
1636 while (!IsNull (&Instance->DeviceList, Node)) {
1637 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);
1638
1639 if ((DeviceInfo->Type == EfiIdeHarddisk) &&
1640 (DeviceInfo->Port == Port)){
1641 *PortMultiplierPort = DeviceInfo->PortMultiplier;
1642 goto Exit;
1643 }
1644
1645 Node = GetNextNode (&Instance->DeviceList, Node);
1646 }
1647
1648 return EFI_NOT_FOUND;
1649 } else {
1650 //
1651 // PortMultiplierPort is not equal to 0xFFFF and also not equal to previous return value
1652 //
1653 return EFI_INVALID_PARAMETER;
1654 }
1655
1656 Exit:
1657 //
1658 // Update the PreviousPort and PreviousPortMultiplier.
1659 //
1660 Instance->PreviousPortMultiplier = *PortMultiplierPort;
1661
1662 return EFI_SUCCESS;
1663 }
1664
1665 /**
1666 Used to allocate and build a device path node for an ATA device on an ATA controller.
1667
1668 The BuildDevicePath() function allocates and builds a single device node for the ATA
1669 device specified by Port and PortMultiplierPort. If the ATA device specified by Port and
1670 PortMultiplierPort is not present on the ATA controller, then EFI_NOT_FOUND is returned.
1671 If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned. If there are not enough
1672 resources to allocate the device path node, then EFI_OUT_OF_RESOURCES is returned.
1673
1674 Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of
1675 DevicePath are initialized to describe the ATA device specified by Port and PortMultiplierPort,
1676 and EFI_SUCCESS is returned.
1677
1678 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1679 @param[in] Port Port specifies the port number of the ATA device for which a
1680 device path node is to be allocated and built.
1681 @param[in] PortMultiplierPort The port multiplier port number of the ATA device for which a
1682 device path node is to be allocated and built. If there is no
1683 port multiplier, then specify 0xFFFF.
1684 @param[in, out] DevicePath A pointer to a single device path node that describes the ATA
1685 device specified by Port and PortMultiplierPort. This function
1686 is responsible for allocating the buffer DevicePath with the
1687 boot service AllocatePool(). It is the caller's responsibility
1688 to free DevicePath when the caller is finished with DevicePath.
1689 @retval EFI_SUCCESS The device path node that describes the ATA device specified by
1690 Port and PortMultiplierPort was allocated and returned in DevicePath.
1691 @retval EFI_NOT_FOUND The ATA device specified by Port and PortMultiplierPort does not
1692 exist on the ATA controller.
1693 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1694 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
1695
1696 **/
1697 EFI_STATUS
1698 EFIAPI
1699 AtaPassThruBuildDevicePath (
1700 IN EFI_ATA_PASS_THRU_PROTOCOL *This,
1701 IN UINT16 Port,
1702 IN UINT16 PortMultiplierPort,
1703 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath
1704 )
1705 {
1706 EFI_DEV_PATH *DevicePathNode;
1707 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;
1708 LIST_ENTRY *Node;
1709
1710 Instance = ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);
1711
1712 //
1713 // Validate parameters passed in.
1714 //
1715 if (DevicePath == NULL) {
1716 return EFI_INVALID_PARAMETER;
1717 }
1718
1719 Node = SearchDeviceInfoList(Instance, Port, PortMultiplierPort, EfiIdeHarddisk);
1720 if (Node == NULL) {
1721 return EFI_NOT_FOUND;
1722 }
1723
1724 if (Instance->Mode == EfiAtaIdeMode) {
1725 DevicePathNode = AllocateCopyPool (sizeof (ATAPI_DEVICE_PATH), &mAtapiDevicePathTemplate);
1726 if (DevicePathNode == NULL) {
1727 return EFI_OUT_OF_RESOURCES;
1728 }
1729 DevicePathNode->Atapi.PrimarySecondary = (UINT8) Port;
1730 DevicePathNode->Atapi.SlaveMaster = (UINT8) PortMultiplierPort;
1731 DevicePathNode->Atapi.Lun = 0;
1732 } else {
1733 DevicePathNode = AllocateCopyPool (sizeof (SATA_DEVICE_PATH), &mSataDevicePathTemplate);
1734 if (DevicePathNode == NULL) {
1735 return EFI_OUT_OF_RESOURCES;
1736 }
1737
1738 DevicePathNode->Sata.HBAPortNumber = Port;
1739 DevicePathNode->Sata.PortMultiplierPortNumber = PortMultiplierPort;
1740 DevicePathNode->Sata.Lun = 0;
1741 }
1742
1743 *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) DevicePathNode;
1744
1745 return EFI_SUCCESS;
1746 }
1747
1748 /**
1749 Used to translate a device path node to a port number and port multiplier port number.
1750
1751 The GetDevice() function determines the port and port multiplier port number associated with
1752 the ATA device described by DevicePath. If DevicePath is a device path node type that the
1753 ATA Pass Thru driver supports, then the ATA Pass Thru driver will attempt to translate the contents
1754 DevicePath into a port number and port multiplier port number.
1755
1756 If this translation is successful, then that port number and port multiplier port number are returned
1757 in Port and PortMultiplierPort, and EFI_SUCCESS is returned.
1758
1759 If DevicePath, Port, or PortMultiplierPort are NULL, then EFI_INVALID_PARAMETER is returned.
1760
1761 If DevicePath is not a device path node type that the ATA Pass Thru driver supports, then
1762 EFI_UNSUPPORTED is returned.
1763
1764 If DevicePath is a device path node type that the ATA Pass Thru driver supports, but there is not
1765 a valid translation from DevicePath to a port number and port multiplier port number, then
1766 EFI_NOT_FOUND is returned.
1767
1768 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1769 @param[in] DevicePath A pointer to the device path node that describes an ATA device on the
1770 ATA controller.
1771 @param[out] Port On return, points to the port number of an ATA device on the ATA controller.
1772 @param[out] PortMultiplierPort On return, points to the port multiplier port number of an ATA device
1773 on the ATA controller.
1774
1775 @retval EFI_SUCCESS DevicePath was successfully translated to a port number and port multiplier
1776 port number, and they were returned in Port and PortMultiplierPort.
1777 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
1778 @retval EFI_INVALID_PARAMETER Port is NULL.
1779 @retval EFI_INVALID_PARAMETER PortMultiplierPort is NULL.
1780 @retval EFI_UNSUPPORTED This driver does not support the device path node type in DevicePath.
1781 @retval EFI_NOT_FOUND A valid translation from DevicePath to a port number and port multiplier
1782 port number does not exist.
1783 **/
1784 EFI_STATUS
1785 EFIAPI
1786 AtaPassThruGetDevice (
1787 IN EFI_ATA_PASS_THRU_PROTOCOL *This,
1788 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
1789 OUT UINT16 *Port,
1790 OUT UINT16 *PortMultiplierPort
1791 )
1792 {
1793 EFI_DEV_PATH *DevicePathNode;
1794 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;
1795 LIST_ENTRY *Node;
1796
1797 Instance = ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);
1798
1799 //
1800 // Validate parameters passed in.
1801 //
1802 if (DevicePath == NULL || Port == NULL || PortMultiplierPort == NULL) {
1803 return EFI_INVALID_PARAMETER;
1804 }
1805
1806 //
1807 // Check whether the DevicePath belongs to SCSI_DEVICE_PATH or ATAPI_DEVICE_PATH
1808 //
1809 if ((DevicePath->Type != MESSAGING_DEVICE_PATH) ||
1810 ((DevicePath->SubType != MSG_SATA_DP) &&
1811 (DevicePath->SubType != MSG_ATAPI_DP)) ||
1812 ((DevicePathNodeLength(DevicePath) != sizeof(ATAPI_DEVICE_PATH)) &&
1813 (DevicePathNodeLength(DevicePath) != sizeof(SATA_DEVICE_PATH)))) {
1814 return EFI_UNSUPPORTED;
1815 }
1816
1817 DevicePathNode = (EFI_DEV_PATH *) DevicePath;
1818
1819 if (Instance->Mode == EfiAtaIdeMode) {
1820 *Port = DevicePathNode->Atapi.PrimarySecondary;
1821 *PortMultiplierPort = DevicePathNode->Atapi.SlaveMaster;
1822 } else {
1823 *Port = DevicePathNode->Sata.HBAPortNumber;
1824 *PortMultiplierPort = DevicePathNode->Sata.PortMultiplierPortNumber;
1825 }
1826
1827 Node = SearchDeviceInfoList(Instance, *Port, *PortMultiplierPort, EfiIdeHarddisk);
1828
1829 if (Node == NULL) {
1830 return EFI_NOT_FOUND;
1831 }
1832
1833 return EFI_SUCCESS;
1834 }
1835
1836 /**
1837 Resets a specific port on the ATA controller. This operation also resets all the ATA devices
1838 connected to the port.
1839
1840 The ResetChannel() function resets an a specific port on an ATA controller. This operation
1841 resets all the ATA devices connected to that port. If this ATA controller does not support
1842 a reset port operation, then EFI_UNSUPPORTED is returned.
1843
1844 If a device error occurs while executing that port reset operation, then EFI_DEVICE_ERROR is
1845 returned.
1846
1847 If a timeout occurs during the execution of the port reset operation, then EFI_TIMEOUT is returned.
1848
1849 If the port reset operation is completed, then EFI_SUCCESS is returned.
1850
1851 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1852 @param[in] Port The port number on the ATA controller.
1853
1854 @retval EFI_SUCCESS The ATA controller port was reset.
1855 @retval EFI_UNSUPPORTED The ATA controller does not support a port reset operation.
1856 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the ATA port.
1857 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the ATA port.
1858
1859 **/
1860 EFI_STATUS
1861 EFIAPI
1862 AtaPassThruResetPort (
1863 IN EFI_ATA_PASS_THRU_PROTOCOL *This,
1864 IN UINT16 Port
1865 )
1866 {
1867 //
1868 // Return success directly then upper layer driver could think reset port operation is done.
1869 //
1870 return EFI_SUCCESS;
1871 }
1872
1873 /**
1874 Resets an ATA device that is connected to an ATA controller.
1875
1876 The ResetDevice() function resets the ATA device specified by Port and PortMultiplierPort.
1877 If this ATA controller does not support a device reset operation, then EFI_UNSUPPORTED is
1878 returned.
1879
1880 If Port or PortMultiplierPort are not in a valid range for this ATA controller, then
1881 EFI_INVALID_PARAMETER is returned.
1882
1883 If a device error occurs while executing that device reset operation, then EFI_DEVICE_ERROR
1884 is returned.
1885
1886 If a timeout occurs during the execution of the device reset operation, then EFI_TIMEOUT is
1887 returned.
1888
1889 If the device reset operation is completed, then EFI_SUCCESS is returned.
1890
1891 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.
1892 @param[in] Port Port represents the port number of the ATA device to be reset.
1893 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to reset.
1894 If there is no port multiplier, then specify 0xFFFF.
1895 @retval EFI_SUCCESS The ATA device specified by Port and PortMultiplierPort was reset.
1896 @retval EFI_UNSUPPORTED The ATA controller does not support a device reset operation.
1897 @retval EFI_INVALID_PARAMETER Port or PortMultiplierPort are invalid.
1898 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the ATA device
1899 specified by Port and PortMultiplierPort.
1900 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the ATA device
1901 specified by Port and PortMultiplierPort.
1902
1903 **/
1904 EFI_STATUS
1905 EFIAPI
1906 AtaPassThruResetDevice (
1907 IN EFI_ATA_PASS_THRU_PROTOCOL *This,
1908 IN UINT16 Port,
1909 IN UINT16 PortMultiplierPort
1910 )
1911 {
1912 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;
1913 LIST_ENTRY *Node;
1914
1915 Instance = ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);
1916
1917 Node = SearchDeviceInfoList (Instance, Port, PortMultiplierPort, EfiIdeHarddisk);
1918
1919 if (Node == NULL) {
1920 return EFI_INVALID_PARAMETER;
1921 }
1922
1923 //
1924 // Return success directly then upper layer driver could think reset device operation is done.
1925 //
1926 return EFI_SUCCESS;
1927 }
1928
1929 /**
1930 Submit ATAPI request sense command.
1931
1932 @param[in] This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
1933 @param[in] Target The Target is an array of size TARGET_MAX_BYTES and it represents
1934 the id of the SCSI device to send the SCSI Request Packet. Each
1935 transport driver may choose to utilize a subset of this size to suit the needs
1936 of transport target representation. For example, a Fibre Channel driver
1937 may use only 8 bytes (WWN) to represent an FC target.
1938 @param[in] Lun The LUN of the SCSI device to send the SCSI Request Packet.
1939 @param[in] SenseData A pointer to store sense data.
1940 @param[in] SenseDataLength The sense data length.
1941 @param[in] Timeout The timeout value to execute this cmd, uses 100ns as a unit.
1942
1943 @retval EFI_SUCCESS Send out the ATAPI packet command successfully.
1944 @retval EFI_DEVICE_ERROR The device failed to send data.
1945
1946 **/
1947 EFI_STATUS
1948 EFIAPI
1949 AtaPacketRequestSense (
1950 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
1951 IN UINT8 *Target,
1952 IN UINT64 Lun,
1953 IN VOID *SenseData,
1954 IN UINT8 SenseDataLength,
1955 IN UINT64 Timeout
1956 )
1957 {
1958 EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET Packet;
1959 UINT8 Cdb[12];
1960 EFI_STATUS Status;
1961
1962 ZeroMem (&Packet, sizeof (EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET));
1963 ZeroMem (Cdb, 12);
1964
1965 Cdb[0] = ATA_CMD_REQUEST_SENSE;
1966 Cdb[4] = SenseDataLength;
1967
1968 Packet.Timeout = Timeout;
1969 Packet.Cdb = Cdb;
1970 Packet.CdbLength = 12;
1971 Packet.DataDirection = EFI_EXT_SCSI_DATA_DIRECTION_READ;
1972 Packet.InDataBuffer = SenseData;
1973 Packet.InTransferLength = SenseDataLength;
1974
1975 Status = ExtScsiPassThruPassThru (This, Target, Lun, &Packet, NULL);
1976
1977 return Status;
1978 }
1979
1980 /**
1981 Sends a SCSI Request Packet to a SCSI device that is attached to the SCSI channel. This function
1982 supports both blocking I/O and nonblocking I/O. The blocking I/O functionality is required, and the
1983 nonblocking I/O functionality is optional.
1984
1985 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
1986 @param Target The Target is an array of size TARGET_MAX_BYTES and it represents
1987 the id of the SCSI device to send the SCSI Request Packet. Each
1988 transport driver may choose to utilize a subset of this size to suit the needs
1989 of transport target representation. For example, a Fibre Channel driver
1990 may use only 8 bytes (WWN) to represent an FC target.
1991 @param Lun The LUN of the SCSI device to send the SCSI Request Packet.
1992 @param Packet A pointer to the SCSI Request Packet to send to the SCSI device
1993 specified by Target and Lun.
1994 @param Event If nonblocking I/O is not supported then Event is ignored, and blocking
1995 I/O is performed. If Event is NULL, then blocking I/O is performed. If
1996 Event is not NULL and non blocking I/O is supported, then
1997 nonblocking I/O is performed, and Event will be signaled when the
1998 SCSI Request Packet completes.
1999
2000 @retval EFI_SUCCESS The SCSI Request Packet was sent by the host. For bi-directional
2001 commands, InTransferLength bytes were transferred from
2002 InDataBuffer. For write and bi-directional commands,
2003 OutTransferLength bytes were transferred by
2004 OutDataBuffer.
2005 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was not executed. The number of bytes that
2006 could be transferred is returned in InTransferLength. For write
2007 and bi-directional commands, OutTransferLength bytes were
2008 transferred by OutDataBuffer.
2009 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because there are too many
2010 SCSI Request Packets already queued. The caller may retry again later.
2011 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SCSI Request
2012 Packet.
2013 @retval EFI_INVALID_PARAMETER Target, Lun, or the contents of ScsiRequestPacket are invalid.
2014 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet is not supported
2015 by the host adapter. This includes the case of Bi-directional SCSI
2016 commands not supported by the implementation. The SCSI Request
2017 Packet was not sent, so no additional status information is available.
2018 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.
2019
2020 **/
2021 EFI_STATUS
2022 EFIAPI
2023 ExtScsiPassThruPassThru (
2024 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
2025 IN UINT8 *Target,
2026 IN UINT64 Lun,
2027 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet,
2028 IN EFI_EVENT Event OPTIONAL
2029 )
2030 {
2031 EFI_STATUS Status;
2032 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;
2033 UINT8 Port;
2034 UINT8 PortMultiplier;
2035 EFI_ATA_HC_WORK_MODE Mode;
2036 LIST_ENTRY *Node;
2037 EFI_ATA_DEVICE_INFO *DeviceInfo;
2038 BOOLEAN SenseReq;
2039 EFI_SCSI_SENSE_DATA *PtrSenseData;
2040 UINTN SenseDataLen;
2041 EFI_STATUS SenseStatus;
2042
2043 SenseDataLen = 0;
2044 Instance = EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);
2045
2046 if ((Packet == NULL) || (Packet->Cdb == NULL)) {
2047 return EFI_INVALID_PARAMETER;
2048 }
2049
2050 //
2051 // Don't support variable length CDB
2052 //
2053 if ((Packet->CdbLength != 6) && (Packet->CdbLength != 10) &&
2054 (Packet->CdbLength != 12) && (Packet->CdbLength != 16)) {
2055 return EFI_INVALID_PARAMETER;
2056 }
2057
2058 if ((Packet->SenseDataLength != 0) && (Packet->SenseData == NULL)) {
2059 return EFI_INVALID_PARAMETER;
2060 }
2061
2062 if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->InDataBuffer, This->Mode->IoAlign)) {
2063 return EFI_INVALID_PARAMETER;
2064 }
2065
2066 if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->OutDataBuffer, This->Mode->IoAlign)) {
2067 return EFI_INVALID_PARAMETER;
2068 }
2069
2070 if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->SenseData, This->Mode->IoAlign)) {
2071 return EFI_INVALID_PARAMETER;
2072 }
2073
2074 //
2075 // For ATAPI device, doesn't support multiple LUN device.
2076 //
2077 if (Lun != 0) {
2078 return EFI_INVALID_PARAMETER;
2079 }
2080
2081 //
2082 // The layout of Target array:
2083 // ________________________________________________________________________
2084 // | Byte 0 | Byte 1 | ... | TARGET_MAX_BYTES - 1 |
2085 // |_____________________|_____________________|_____|______________________|
2086 // | | The port multiplier | | |
2087 // | The port number | port number | N/A | N/A |
2088 // |_____________________|_____________________|_____|______________________|
2089 //
2090 // For ATAPI device, 2 bytes is enough to represent the location of SCSI device.
2091 //
2092 Port = Target[0];
2093 PortMultiplier = Target[1];
2094
2095 Node = SearchDeviceInfoList(Instance, Port, PortMultiplier, EfiIdeCdrom);
2096 if (Node == NULL) {
2097 return EFI_INVALID_PARAMETER;
2098 }
2099
2100 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);
2101
2102 //
2103 // ATA_CMD_IDENTIFY_DEVICE cmd is a ATA cmd but not a SCSI cmd.
2104 // Normally it should NOT be passed down through ExtScsiPassThru protocol interface.
2105 // But to response EFI_DISK_INFO.Identify() request from ScsiDisk, we should handle this command.
2106 //
2107 if (*((UINT8*)Packet->Cdb) == ATA_CMD_IDENTIFY_DEVICE) {
2108 CopyMem (Packet->InDataBuffer, DeviceInfo->IdentifyData, sizeof (EFI_IDENTIFY_DATA));
2109 //
2110 // For IDENTIFY DEVICE cmd, we don't need to get sense data.
2111 //
2112 Packet->SenseDataLength = 0;
2113 return EFI_SUCCESS;
2114 }
2115
2116 Mode = Instance->Mode;
2117 switch (Mode) {
2118 case EfiAtaIdeMode:
2119 //
2120 // Reassign IDE mode io port registers' base addresses
2121 //
2122 Status = GetIdeRegisterIoAddr (Instance->PciIo, Instance->IdeRegisters);
2123
2124 if (EFI_ERROR (Status)) {
2125 return Status;
2126 }
2127
2128 Status = AtaPacketCommandExecute (Instance->PciIo, &Instance->IdeRegisters[Port], Port, PortMultiplier, Packet);
2129 break;
2130 case EfiAtaAhciMode:
2131 if (PortMultiplier == 0xFF) {
2132 //
2133 // If there is no port multiplier, the PortMultiplier will be 0xFF
2134 // Here, we convert its value to 0 to follow the AHCI spec.
2135 //
2136 PortMultiplier = 0;
2137 }
2138 Status = AhciPacketCommandExecute (Instance->PciIo, &Instance->AhciRegisters, Port, PortMultiplier, Packet);
2139 break;
2140 default :
2141 Status = EFI_DEVICE_ERROR;
2142 break;
2143 }
2144
2145 //
2146 // If the cmd doesn't get executed correctly, then check sense data.
2147 //
2148 if (EFI_ERROR (Status) && (Packet->SenseDataLength != 0) && (*((UINT8*)Packet->Cdb) != ATA_CMD_REQUEST_SENSE)) {
2149 PtrSenseData = AllocateAlignedPages (EFI_SIZE_TO_PAGES (sizeof (EFI_SCSI_SENSE_DATA)), This->Mode->IoAlign);
2150 if (PtrSenseData == NULL) {
2151 return EFI_DEVICE_ERROR;
2152 }
2153
2154 for (SenseReq = TRUE; SenseReq;) {
2155 SenseStatus = AtaPacketRequestSense (
2156 This,
2157 Target,
2158 Lun,
2159 PtrSenseData,
2160 sizeof (EFI_SCSI_SENSE_DATA),
2161 Packet->Timeout
2162 );
2163 if (EFI_ERROR (SenseStatus)) {
2164 break;
2165 }
2166
2167 CopyMem ((UINT8*)Packet->SenseData + SenseDataLen, PtrSenseData, sizeof (EFI_SCSI_SENSE_DATA));
2168 SenseDataLen += sizeof (EFI_SCSI_SENSE_DATA);
2169
2170 //
2171 // no more sense key or number of sense keys exceeds predefined,
2172 // skip the loop.
2173 //
2174 if ((PtrSenseData->Sense_Key == EFI_SCSI_SK_NO_SENSE) ||
2175 (SenseDataLen + sizeof (EFI_SCSI_SENSE_DATA) > Packet->SenseDataLength)) {
2176 SenseReq = FALSE;
2177 }
2178 }
2179 FreeAlignedPages (PtrSenseData, EFI_SIZE_TO_PAGES (sizeof (EFI_SCSI_SENSE_DATA)));
2180 }
2181 //
2182 // Update the SenseDataLength field to the data length received.
2183 //
2184 Packet->SenseDataLength = (UINT8)SenseDataLen;
2185 return Status;
2186 }
2187
2188 /**
2189 Used to retrieve the list of legal Target IDs and LUNs for SCSI devices on a SCSI channel. These
2190 can either be the list SCSI devices that are actually present on the SCSI channel, or the list of legal
2191 Target Ids and LUNs for the SCSI channel. Regardless, the caller of this function must probe the
2192 Target ID and LUN returned to see if a SCSI device is actually present at that location on the SCSI
2193 channel.
2194
2195 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2196 @param Target On input, a pointer to the Target ID (an array of size
2197 TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.
2198 On output, a pointer to the Target ID (an array of
2199 TARGET_MAX_BYTES) of the next SCSI device present on a SCSI
2200 channel. An input value of 0xF(all bytes in the array are 0xF) in the
2201 Target array retrieves the Target ID of the first SCSI device present on a
2202 SCSI channel.
2203 @param Lun On input, a pointer to the LUN of a SCSI device present on the SCSI
2204 channel. On output, a pointer to the LUN of the next SCSI device present
2205 on a SCSI channel.
2206
2207 @retval EFI_SUCCESS The Target ID and LUN of the next SCSI device on the SCSI
2208 channel was returned in Target and Lun.
2209 @retval EFI_INVALID_PARAMETER Target array is not all 0xF, and Target and Lun were
2210 not returned on a previous call to GetNextTargetLun().
2211 @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.
2212
2213 **/
2214 EFI_STATUS
2215 EFIAPI
2216 ExtScsiPassThruGetNextTargetLun (
2217 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
2218 IN OUT UINT8 **Target,
2219 IN OUT UINT64 *Lun
2220 )
2221 {
2222 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;
2223 LIST_ENTRY *Node;
2224 EFI_ATA_DEVICE_INFO *DeviceInfo;
2225 UINT8 *Target8;
2226 UINT16 *Target16;
2227
2228 Instance = EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);
2229
2230 if (Target == NULL || Lun == NULL) {
2231 return EFI_INVALID_PARAMETER;
2232 }
2233
2234 if (*Target == NULL) {
2235 return EFI_INVALID_PARAMETER;
2236 }
2237
2238 Target8 = *Target;
2239 Target16 = (UINT16 *)*Target;
2240
2241 if (CompareMem(Target8, mScsiId, TARGET_MAX_BYTES) != 0) {
2242 //
2243 // For ATAPI device, we use 2 least significant bytes to represent the location of SCSI device.
2244 // So the higher bytes in Target array should be 0xFF.
2245 //
2246 if (CompareMem (&Target8[2], &mScsiId[2], TARGET_MAX_BYTES - 2) != 0) {
2247 return EFI_INVALID_PARAMETER;
2248 }
2249
2250 //
2251 // When Target is not all 0xFF's, compare 2 least significant bytes with
2252 // previous target id to see if it is returned by previous call.
2253 //
2254 if ((*Target16 != Instance->PreviousTargetId) ||
2255 (*Lun != Instance->PreviousLun)) {
2256 return EFI_INVALID_PARAMETER;
2257 }
2258
2259 //
2260 // Traverse the whole device list to find the next cdrom closed to
2261 // the device signified by Target[0] and Target[1].
2262 //
2263 // Note that we here use a tricky way to find the next cdrom :
2264 // All ata devices are detected and inserted into the device list
2265 // sequentially.
2266 //
2267 Node = GetFirstNode (&Instance->DeviceList);
2268
2269 while (!IsNull (&Instance->DeviceList, Node)) {
2270 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);
2271
2272 if ((DeviceInfo->Type == EfiIdeCdrom) &&
2273 ((Target8[0] < DeviceInfo->Port) ||
2274 ((Target8[0] == DeviceInfo->Port) &&
2275 (Target8[1] < (UINT8)DeviceInfo->PortMultiplier)))) {
2276 Target8[0] = (UINT8)DeviceInfo->Port;
2277 Target8[1] = (UINT8)DeviceInfo->PortMultiplier;
2278 goto Exit;
2279 }
2280
2281 Node = GetNextNode (&Instance->DeviceList, Node);
2282 }
2283
2284 return EFI_NOT_FOUND;
2285 } else {
2286 //
2287 // If the array is all 0xFF's, start to traverse the device list from the beginning
2288 //
2289 Node = GetFirstNode (&Instance->DeviceList);
2290 while (!IsNull (&Instance->DeviceList, Node)) {
2291 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);
2292
2293 if (DeviceInfo->Type == EfiIdeCdrom) {
2294 Target8[0] = (UINT8)DeviceInfo->Port;
2295 Target8[1] = (UINT8)DeviceInfo->PortMultiplier;
2296 goto Exit;
2297 }
2298
2299 Node = GetNextNode (&Instance->DeviceList, Node);
2300 }
2301
2302 return EFI_NOT_FOUND;
2303 }
2304
2305 Exit:
2306 *Lun = 0;
2307
2308 //
2309 // Update the PreviousTargetId.
2310 //
2311 Instance->PreviousTargetId = *Target16;
2312 Instance->PreviousLun = *Lun;
2313
2314 return EFI_SUCCESS;
2315 }
2316
2317 /**
2318 Used to allocate and build a device path node for a SCSI device on a SCSI channel.
2319
2320 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2321 @param Target The Target is an array of size TARGET_MAX_BYTES and it specifies the
2322 Target ID of the SCSI device for which a device path node is to be
2323 allocated and built. Transport drivers may chose to utilize a subset of
2324 this size to suit the representation of targets. For example, a Fibre
2325 Channel driver may use only 8 bytes (WWN) in the array to represent a
2326 FC target.
2327 @param Lun The LUN of the SCSI device for which a device path node is to be
2328 allocated and built.
2329 @param DevicePath A pointer to a single device path node that describes the SCSI device
2330 specified by Target and Lun. This function is responsible for
2331 allocating the buffer DevicePath with the boot service
2332 AllocatePool(). It is the caller's responsibility to free
2333 DevicePath when the caller is finished with DevicePath.
2334
2335 @retval EFI_SUCCESS The device path node that describes the SCSI device specified by
2336 Target and Lun was allocated and returned in
2337 DevicePath.
2338 @retval EFI_INVALID_PARAMETER DevicePath is NULL.
2339 @retval EFI_NOT_FOUND The SCSI devices specified by Target and Lun does not exist
2340 on the SCSI channel.
2341 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.
2342
2343 **/
2344 EFI_STATUS
2345 EFIAPI
2346 ExtScsiPassThruBuildDevicePath (
2347 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
2348 IN UINT8 *Target,
2349 IN UINT64 Lun,
2350 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath
2351 )
2352 {
2353 EFI_DEV_PATH *DevicePathNode;
2354 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;
2355 UINT8 Port;
2356 UINT8 PortMultiplier;
2357
2358 Instance = EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);
2359
2360 Port = Target[0];
2361 PortMultiplier = Target[1];
2362
2363 //
2364 // Validate parameters passed in.
2365 //
2366 if (DevicePath == NULL) {
2367 return EFI_INVALID_PARAMETER;
2368 }
2369
2370 //
2371 // can not build device path for the SCSI Host Controller.
2372 //
2373 if (Lun != 0) {
2374 return EFI_NOT_FOUND;
2375 }
2376
2377 if (SearchDeviceInfoList(Instance, Port, PortMultiplier, EfiIdeCdrom) == NULL) {
2378 return EFI_NOT_FOUND;
2379 }
2380
2381 if (Instance->Mode == EfiAtaIdeMode) {
2382 DevicePathNode = AllocateCopyPool (sizeof (ATAPI_DEVICE_PATH), &mAtapiDevicePathTemplate);
2383 if (DevicePathNode == NULL) {
2384 return EFI_OUT_OF_RESOURCES;
2385 }
2386
2387 DevicePathNode->Atapi.PrimarySecondary = Port;
2388 DevicePathNode->Atapi.SlaveMaster = PortMultiplier;
2389 DevicePathNode->Atapi.Lun = (UINT16) Lun;
2390 } else {
2391 DevicePathNode = AllocateCopyPool (sizeof (SATA_DEVICE_PATH), &mSataDevicePathTemplate);
2392 if (DevicePathNode == NULL) {
2393 return EFI_OUT_OF_RESOURCES;
2394 }
2395
2396 DevicePathNode->Sata.HBAPortNumber = Port;
2397 //
2398 // For CD-ROM working in the AHCI mode, only 8 bits are used to record
2399 // the PortMultiplier information. If the CD-ROM is directly attached
2400 // on a SATA port, the PortMultiplier should be translated from 0xFF
2401 // to 0xFFFF according to the UEFI spec.
2402 //
2403 DevicePathNode->Sata.PortMultiplierPortNumber = PortMultiplier == 0xFF ? 0xFFFF : PortMultiplier;
2404 DevicePathNode->Sata.Lun = (UINT16) Lun;
2405 }
2406
2407 *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) DevicePathNode;
2408
2409 return EFI_SUCCESS;
2410 }
2411
2412 /**
2413 Used to translate a device path node to a Target ID and LUN.
2414
2415 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2416 @param DevicePath A pointer to a single device path node that describes the SCSI device
2417 on the SCSI channel.
2418 @param Target A pointer to the Target Array which represents the ID of a SCSI device
2419 on the SCSI channel.
2420 @param Lun A pointer to the LUN of a SCSI device on the SCSI channel.
2421
2422 @retval EFI_SUCCESS DevicePath was successfully translated to a Target ID and
2423 LUN, and they were returned in Target and Lun.
2424 @retval EFI_INVALID_PARAMETER DevicePath or Target or Lun is NULL.
2425 @retval EFI_NOT_FOUND A valid translation from DevicePath to a Target ID and LUN
2426 does not exist.
2427 @retval EFI_UNSUPPORTED This driver does not support the device path node type in
2428 DevicePath.
2429
2430 **/
2431 EFI_STATUS
2432 EFIAPI
2433 ExtScsiPassThruGetTargetLun (
2434 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
2435 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
2436 OUT UINT8 **Target,
2437 OUT UINT64 *Lun
2438 )
2439 {
2440 EFI_DEV_PATH *DevicePathNode;
2441 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;
2442 LIST_ENTRY *Node;
2443
2444 Instance = EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);
2445
2446 //
2447 // Validate parameters passed in.
2448 //
2449 if (DevicePath == NULL || Target == NULL || Lun == NULL) {
2450 return EFI_INVALID_PARAMETER;
2451 }
2452
2453 if (*Target == NULL) {
2454 return EFI_INVALID_PARAMETER;
2455 }
2456 //
2457 // Check whether the DevicePath belongs to SCSI_DEVICE_PATH
2458 //
2459 if ((DevicePath->Type != MESSAGING_DEVICE_PATH) ||
2460 ((DevicePath->SubType != MSG_ATAPI_DP) &&
2461 (DevicePath->SubType != MSG_SATA_DP)) ||
2462 ((DevicePathNodeLength(DevicePath) != sizeof(ATAPI_DEVICE_PATH)) &&
2463 (DevicePathNodeLength(DevicePath) != sizeof(SATA_DEVICE_PATH)))) {
2464 return EFI_UNSUPPORTED;
2465 }
2466
2467 SetMem (*Target, TARGET_MAX_BYTES, 0xFF);
2468
2469 DevicePathNode = (EFI_DEV_PATH *) DevicePath;
2470
2471 if (Instance->Mode == EfiAtaIdeMode) {
2472 (*Target)[0] = (UINT8) DevicePathNode->Atapi.PrimarySecondary;
2473 (*Target)[1] = (UINT8) DevicePathNode->Atapi.SlaveMaster;
2474 *Lun = (UINT8) DevicePathNode->Atapi.Lun;
2475 } else {
2476 (*Target)[0] = (UINT8) DevicePathNode->Sata.HBAPortNumber;
2477 (*Target)[1] = (UINT8) DevicePathNode->Sata.PortMultiplierPortNumber;
2478 *Lun = (UINT8) DevicePathNode->Sata.Lun;
2479 }
2480
2481 Node = SearchDeviceInfoList(Instance, (*Target)[0], (*Target)[1], EfiIdeCdrom);
2482
2483 if (Node == NULL) {
2484 return EFI_NOT_FOUND;
2485 }
2486
2487 if (*Lun != 0) {
2488 return EFI_NOT_FOUND;
2489 }
2490
2491 return EFI_SUCCESS;
2492 }
2493
2494 /**
2495 Resets a SCSI channel. This operation resets all the SCSI devices connected to the SCSI channel.
2496
2497 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2498
2499 @retval EFI_SUCCESS The SCSI channel was reset.
2500 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI channel.
2501 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI channel.
2502 @retval EFI_UNSUPPORTED The SCSI channel does not support a channel reset operation.
2503
2504 **/
2505 EFI_STATUS
2506 EFIAPI
2507 ExtScsiPassThruResetChannel (
2508 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This
2509 )
2510 {
2511 //
2512 // Return success directly then upper layer driver could think reset channel operation is done.
2513 //
2514 return EFI_SUCCESS;
2515 }
2516
2517 /**
2518 Resets a SCSI logical unit that is connected to a SCSI channel.
2519
2520 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2521 @param Target The Target is an array of size TARGET_MAX_BYTE and it represents the
2522 target port ID of the SCSI device containing the SCSI logical unit to
2523 reset. Transport drivers may chose to utilize a subset of this array to suit
2524 the representation of their targets.
2525 @param Lun The LUN of the SCSI device to reset.
2526
2527 @retval EFI_SUCCESS The SCSI device specified by Target and Lun was reset.
2528 @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
2529 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI device
2530 specified by Target and Lun.
2531 @retval EFI_UNSUPPORTED The SCSI channel does not support a target reset operation.
2532 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI device
2533 specified by Target and Lun.
2534
2535 **/
2536 EFI_STATUS
2537 EFIAPI
2538 ExtScsiPassThruResetTargetLun (
2539 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
2540 IN UINT8 *Target,
2541 IN UINT64 Lun
2542 )
2543 {
2544 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;
2545 LIST_ENTRY *Node;
2546 UINT8 Port;
2547 UINT8 PortMultiplier;
2548
2549 Instance = EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);
2550 //
2551 // For ATAPI device, doesn't support multiple LUN device.
2552 //
2553 if (Lun != 0) {
2554 return EFI_INVALID_PARAMETER;
2555 }
2556 //
2557 // The layout of Target array:
2558 // ________________________________________________________________________
2559 // | Byte 0 | Byte 1 | ... | TARGET_MAX_BYTES - 1 |
2560 // |_____________________|_____________________|_____|______________________|
2561 // | | The port multiplier | | |
2562 // | The port number | port number | N/A | N/A |
2563 // |_____________________|_____________________|_____|______________________|
2564 //
2565 // For ATAPI device, 2 bytes is enough to represent the location of SCSI device.
2566 //
2567 Port = Target[0];
2568 PortMultiplier = Target[1];
2569
2570 Node = SearchDeviceInfoList(Instance, Port, PortMultiplier, EfiIdeCdrom);
2571 if (Node == NULL) {
2572 return EFI_INVALID_PARAMETER;
2573 }
2574
2575 //
2576 // Return success directly then upper layer driver could think reset target LUN operation is done.
2577 //
2578 return EFI_SUCCESS;
2579 }
2580
2581 /**
2582 Used to retrieve the list of legal Target IDs for SCSI devices on a SCSI channel. These can either
2583 be the list SCSI devices that are actually present on the SCSI channel, or the list of legal Target IDs
2584 for the SCSI channel. Regardless, the caller of this function must probe the Target ID returned to
2585 see if a SCSI device is actually present at that location on the SCSI channel.
2586
2587 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
2588 @param Target (TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.
2589 On output, a pointer to the Target ID (an array of
2590 TARGET_MAX_BYTES) of the next SCSI device present on a SCSI
2591 channel. An input value of 0xF(all bytes in the array are 0xF) in the
2592 Target array retrieves the Target ID of the first SCSI device present on a
2593 SCSI channel.
2594
2595 @retval EFI_SUCCESS The Target ID of the next SCSI device on the SCSI
2596 channel was returned in Target.
2597 @retval EFI_INVALID_PARAMETER Target or Lun is NULL.
2598 @retval EFI_TIMEOUT Target array is not all 0xF, and Target was not
2599 returned on a previous call to GetNextTarget().
2600 @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.
2601
2602 **/
2603 EFI_STATUS
2604 EFIAPI
2605 ExtScsiPassThruGetNextTarget (
2606 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
2607 IN OUT UINT8 **Target
2608 )
2609 {
2610 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;
2611 LIST_ENTRY *Node;
2612 EFI_ATA_DEVICE_INFO *DeviceInfo;
2613 UINT8 *Target8;
2614 UINT16 *Target16;
2615
2616 Instance = EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);
2617
2618 if (Target == NULL || *Target == NULL) {
2619 return EFI_INVALID_PARAMETER;
2620 }
2621
2622 Target8 = *Target;
2623 Target16 = (UINT16 *)*Target;
2624
2625 if (CompareMem(Target8, mScsiId, TARGET_MAX_BYTES) != 0) {
2626 //
2627 // For ATAPI device, we use 2 least significant bytes to represent the location of SCSI device.
2628 // So the higher bytes in Target array should be 0xFF.
2629 //
2630 if (CompareMem (&Target8[2], &mScsiId[2], TARGET_MAX_BYTES - 2) != 0) {
2631 return EFI_INVALID_PARAMETER;
2632 }
2633
2634 //
2635 // When Target is not all 0xFF's, compare 2 least significant bytes with
2636 // previous target id to see if it is returned by previous call.
2637 //
2638 if (*Target16 != Instance->PreviousTargetId) {
2639 return EFI_INVALID_PARAMETER;
2640 }
2641
2642 //
2643 // Traverse the whole device list to find the next cdrom closed to
2644 // the device signified by Target[0] and Target[1].
2645 //
2646 // Note that we here use a tricky way to find the next cdrom :
2647 // All ata devices are detected and inserted into the device list
2648 // sequentially.
2649 //
2650 Node = GetFirstNode (&Instance->DeviceList);
2651 while (!IsNull (&Instance->DeviceList, Node)) {
2652 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);
2653
2654 if ((DeviceInfo->Type == EfiIdeCdrom) &&
2655 ((Target8[0] < DeviceInfo->Port) ||
2656 ((Target8[0] == DeviceInfo->Port) &&
2657 (Target8[1] < (UINT8)DeviceInfo->PortMultiplier)))) {
2658 Target8[0] = (UINT8)DeviceInfo->Port;
2659 Target8[1] = (UINT8)DeviceInfo->PortMultiplier;
2660 goto Exit;
2661 }
2662
2663 Node = GetNextNode (&Instance->DeviceList, Node);
2664 }
2665
2666 return EFI_NOT_FOUND;
2667 } else {
2668 //
2669 // If the array is all 0xFF's, start to traverse the device list from the beginning
2670 //
2671 Node = GetFirstNode (&Instance->DeviceList);
2672
2673 while (!IsNull (&Instance->DeviceList, Node)) {
2674 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);
2675
2676 if (DeviceInfo->Type == EfiIdeCdrom) {
2677 Target8[0] = (UINT8)DeviceInfo->Port;
2678 Target8[1] = (UINT8)DeviceInfo->PortMultiplier;
2679 goto Exit;
2680 }
2681
2682 Node = GetNextNode (&Instance->DeviceList, Node);
2683 }
2684
2685 return EFI_NOT_FOUND;
2686 }
2687
2688 Exit:
2689 //
2690 // Update the PreviousTargetId.
2691 //
2692 Instance->PreviousTargetId = *Target16;
2693
2694 return EFI_SUCCESS;
2695 }
2696