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