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