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