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