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