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