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