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