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