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