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