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