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