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