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