]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Bus/Ata/AtaAtapiPassThru/AtaAtapiPassThru.c
MdeModulePkg: Validate the return value before use it.
[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
73a9e822 5 Copyright (c) 2010 - 2014, 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
6e1e5405 720 Supports &= (UINT64)EFI_PCI_DEVICE_ENABLE;\r
a41b5272 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
32e817c9
FT
873 Status = gBS->UninstallMultipleProtocolInterfaces (\r
874 Controller,\r
875 &gEfiAtaPassThruProtocolGuid, &(Instance->AtaPassThru),\r
876 &gEfiExtScsiPassThruProtocolGuid, &(Instance->ExtScsiPassThru),\r
877 NULL\r
878 );\r
a41b5272 879\r
32e817c9
FT
880 if (EFI_ERROR (Status)) {\r
881 return EFI_DEVICE_ERROR;\r
a41b5272 882 }\r
883\r
a41b5272 884 //\r
885 // Close protocols opened by AtaAtapiPassThru controller driver\r
886 //\r
887 gBS->CloseProtocol (\r
888 Controller,\r
889 &gEfiIdeControllerInitProtocolGuid,\r
890 This->DriverBindingHandle,\r
891 Controller\r
892 );\r
893\r
32e817c9
FT
894 //\r
895 // Close Non-Blocking timer and free Task list.\r
896 //\r
897 if (Instance->TimerEvent != NULL) {\r
898 gBS->CloseEvent (Instance->TimerEvent);\r
899 Instance->TimerEvent = NULL;\r
900 }\r
901 DestroyAsynTaskList (Instance, FALSE);\r
a41b5272 902 //\r
903 // Free allocated resource\r
904 //\r
32e817c9 905 DestroyDeviceInfoList (Instance);\r
a41b5272 906\r
907 //\r
908 // If the current working mode is AHCI mode, then pre-allocated resource\r
909 // for AHCI initialization should be released.\r
910 //\r
32e817c9
FT
911 PciIo = Instance->PciIo;\r
912\r
a41b5272 913 if (Instance->Mode == EfiAtaAhciMode) {\r
914 AhciRegisters = &Instance->AhciRegisters;\r
915 PciIo->Unmap (\r
916 PciIo,\r
917 AhciRegisters->MapCommandTable\r
918 );\r
919 PciIo->FreeBuffer (\r
920 PciIo,\r
ed365e93 921 EFI_SIZE_TO_PAGES ((UINTN) AhciRegisters->MaxCommandTableSize),\r
a41b5272 922 AhciRegisters->AhciCommandTable\r
923 );\r
924 PciIo->Unmap (\r
925 PciIo,\r
926 AhciRegisters->MapCmdList\r
927 );\r
928 PciIo->FreeBuffer (\r
929 PciIo,\r
ed365e93 930 EFI_SIZE_TO_PAGES ((UINTN) AhciRegisters->MaxCommandListSize),\r
a41b5272 931 AhciRegisters->AhciCmdList\r
932 );\r
933 PciIo->Unmap (\r
934 PciIo,\r
935 AhciRegisters->MapRFis\r
936 );\r
937 PciIo->FreeBuffer (\r
938 PciIo,\r
ed365e93 939 EFI_SIZE_TO_PAGES ((UINTN) AhciRegisters->MaxReceiveFisSize),\r
a41b5272 940 AhciRegisters->AhciRFis\r
941 );\r
942 }\r
32e817c9
FT
943\r
944 //\r
945 // Disable this ATA host controller.\r
946 //\r
947 Status = PciIo->Attributes (\r
948 PciIo,\r
949 EfiPciIoAttributeOperationSupported,\r
950 0,\r
951 &Supports\r
952 );\r
953 if (!EFI_ERROR (Status)) {\r
6e1e5405 954 Supports &= (UINT64)EFI_PCI_DEVICE_ENABLE;\r
32e817c9
FT
955 PciIo->Attributes (\r
956 PciIo,\r
957 EfiPciIoAttributeOperationDisable,\r
958 Supports,\r
959 NULL\r
960 );\r
961 }\r
962\r
963 //\r
964 // Restore original PCI attributes\r
965 //\r
966 Status = PciIo->Attributes (\r
967 PciIo,\r
968 EfiPciIoAttributeOperationSet,\r
969 Instance->OriginalPciAttributes,\r
970 NULL\r
971 );\r
972 ASSERT_EFI_ERROR (Status);\r
973\r
a41b5272 974 FreePool (Instance);\r
975\r
976 return Status;\r
977}\r
978\r
979/**\r
980 Traverse the attached ATA devices list to find out the device to access.\r
58727f29 981\r
a41b5272 982 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.\r
58727f29 983 @param[in] Port The port number of the ATA device to send the command.\r
a41b5272 984 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.\r
985 If there is no port multiplier, then specify 0.\r
986 @param[in] DeviceType The device type of the ATA device.\r
58727f29 987\r
a41b5272 988 @retval The pointer to the data structure of the device info to access.\r
989\r
990**/\r
991LIST_ENTRY *\r
992EFIAPI\r
993SearchDeviceInfoList (\r
994 IN ATA_ATAPI_PASS_THRU_INSTANCE *Instance,\r
995 IN UINT16 Port,\r
996 IN UINT16 PortMultiplier,\r
997 IN EFI_ATA_DEVICE_TYPE DeviceType\r
998 )\r
999{\r
1000 EFI_ATA_DEVICE_INFO *DeviceInfo;\r
1001 LIST_ENTRY *Node;\r
1002\r
1003 Node = GetFirstNode (&Instance->DeviceList);\r
1004 while (!IsNull (&Instance->DeviceList, Node)) {\r
1005 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);\r
1006\r
1007 if ((DeviceInfo->Type == DeviceType) &&\r
58727f29 1008 (Port == DeviceInfo->Port) &&\r
a41b5272 1009 (PortMultiplier == DeviceInfo->PortMultiplier)) {\r
1010 return Node;\r
1011 }\r
1012\r
1013 Node = GetNextNode (&Instance->DeviceList, Node);\r
490b5ea1 1014 }\r
a41b5272 1015\r
1016 return NULL;\r
1017}\r
1018\r
1019/**\r
1020 Allocate device info data structure to contain device info.\r
1021 And insert the data structure to the tail of device list for tracing.\r
58727f29 1022\r
a41b5272 1023 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.\r
58727f29 1024 @param[in] Port The port number of the ATA device to send the command.\r
a41b5272 1025 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.\r
1026 If there is no port multiplier, then specify 0.\r
1027 @param[in] DeviceType The device type of the ATA device.\r
1028 @param[in] IdentifyData The data buffer to store the output of the IDENTIFY cmd.\r
1029\r
1030 @retval EFI_SUCCESS Successfully insert the ata device to the tail of device list.\r
1031 @retval EFI_OUT_OF_RESOURCES Can not allocate enough resource for use.\r
1032\r
1033**/\r
1034EFI_STATUS\r
1035EFIAPI\r
1036CreateNewDeviceInfo (\r
1037 IN ATA_ATAPI_PASS_THRU_INSTANCE *Instance,\r
1038 IN UINT16 Port,\r
1039 IN UINT16 PortMultiplier,\r
1040 IN EFI_ATA_DEVICE_TYPE DeviceType,\r
1041 IN EFI_IDENTIFY_DATA *IdentifyData\r
1042 )\r
1043{\r
1044 EFI_ATA_DEVICE_INFO *DeviceInfo;\r
1045\r
1046 DeviceInfo = AllocateZeroPool (sizeof (EFI_ATA_DEVICE_INFO));\r
1047\r
1048 if (DeviceInfo == NULL) {\r
1049 return EFI_OUT_OF_RESOURCES;\r
1050 }\r
1051\r
1052 DeviceInfo->Signature = ATA_ATAPI_DEVICE_SIGNATURE;\r
1053 DeviceInfo->Port = Port;\r
1054 DeviceInfo->PortMultiplier = PortMultiplier;\r
1055 DeviceInfo->Type = DeviceType;\r
1056\r
1057 if (IdentifyData != NULL) {\r
1058 DeviceInfo->IdentifyData = AllocateCopyPool (sizeof (EFI_IDENTIFY_DATA), IdentifyData);\r
1059 if (DeviceInfo->IdentifyData == NULL) {\r
1060 FreePool (DeviceInfo);\r
1061 return EFI_OUT_OF_RESOURCES;\r
1062 }\r
1063 }\r
1064\r
1065 InsertTailList (&Instance->DeviceList, &DeviceInfo->Link);\r
1066\r
1067 return EFI_SUCCESS;\r
1068}\r
1069\r
1070/**\r
1071 Destroy all attached ATA devices info.\r
58727f29 1072\r
a41b5272 1073 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.\r
1074\r
1075**/\r
1076VOID\r
1077EFIAPI\r
1078DestroyDeviceInfoList (\r
1079 IN ATA_ATAPI_PASS_THRU_INSTANCE *Instance\r
1080 )\r
1081{\r
1082 EFI_ATA_DEVICE_INFO *DeviceInfo;\r
1083 LIST_ENTRY *Node;\r
1084\r
1085 Node = GetFirstNode (&Instance->DeviceList);\r
1086 while (!IsNull (&Instance->DeviceList, Node)) {\r
1087 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);\r
1088\r
1089 Node = GetNextNode (&Instance->DeviceList, Node);\r
1090\r
1091 RemoveEntryList (&DeviceInfo->Link);\r
1092 if (DeviceInfo->IdentifyData != NULL) {\r
1093 FreePool (DeviceInfo->IdentifyData);\r
1094 }\r
1095 FreePool (DeviceInfo);\r
1096 }\r
1097}\r
1098\r
490b5ea1 1099/**\r
1100 Destroy all pending non blocking tasks.\r
58727f29 1101\r
1102 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.\r
1103 @param[in] IsSigEvent Indicate whether signal the task event when remove the\r
1104 task.\r
490b5ea1 1105\r
1106**/\r
1107VOID\r
1108EFIAPI\r
1109DestroyAsynTaskList (\r
58727f29 1110 IN ATA_ATAPI_PASS_THRU_INSTANCE *Instance,\r
1111 IN BOOLEAN IsSigEvent\r
490b5ea1 1112 )\r
1113{\r
1114 LIST_ENTRY *Entry;\r
1115 LIST_ENTRY *DelEntry;\r
1116 ATA_NONBLOCK_TASK *Task;\r
1117 EFI_TPL OldTpl;\r
1118\r
1119 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
1120 if (!IsListEmpty (&Instance->NonBlockingTaskList)) {\r
1121 //\r
1122 // Free the Subtask list.\r
1123 //\r
58727f29 1124 for (Entry = (&Instance->NonBlockingTaskList)->ForwardLink;\r
490b5ea1 1125 Entry != (&Instance->NonBlockingTaskList);\r
1126 ) {\r
1127 DelEntry = Entry;\r
1128 Entry = Entry->ForwardLink;\r
1129 Task = ATA_NON_BLOCK_TASK_FROM_ENTRY (DelEntry);\r
1130\r
1131 RemoveEntryList (DelEntry);\r
58727f29 1132 if (IsSigEvent) {\r
1133 Task->Packet->Asb->AtaStatus = 0x01;\r
1134 gBS->SignalEvent (Task->Event);\r
1135 }\r
490b5ea1 1136 FreePool (Task);\r
1137 }\r
1138 }\r
1139 gBS->RestoreTPL (OldTpl);\r
1140}\r
1141\r
a41b5272 1142/**\r
1143 Enumerate all attached ATA devices at IDE mode or AHCI mode separately.\r
58727f29 1144\r
1145 The function is designed to enumerate all attached ATA devices.\r
1146\r
a41b5272 1147 @param[in] Instance A pointer to the ATA_ATAPI_PASS_THRU_INSTANCE instance.\r
1148\r
1149 @retval EFI_SUCCESS Successfully enumerate attached ATA devices.\r
1150 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.\r
1151\r
1152**/\r
1153EFI_STATUS\r
1154EFIAPI\r
1155EnumerateAttachedDevice (\r
1156 IN ATA_ATAPI_PASS_THRU_INSTANCE *Instance\r
1157 )\r
1158{\r
1159 EFI_STATUS Status;\r
1160 PCI_TYPE00 PciData;\r
1161 UINT8 ClassCode;\r
1162\r
1163 Status = EFI_SUCCESS;\r
1164\r
1165 Status = Instance->PciIo->Pci.Read (\r
1166 Instance->PciIo,\r
1167 EfiPciIoWidthUint8,\r
1168 PCI_CLASSCODE_OFFSET,\r
1169 sizeof (PciData.Hdr.ClassCode),\r
1170 PciData.Hdr.ClassCode\r
1171 );\r
1172 ASSERT_EFI_ERROR (Status);\r
1173\r
1174 ClassCode = PciData.Hdr.ClassCode[1];\r
1175\r
1176 switch (ClassCode) {\r
1177 case PCI_CLASS_MASS_STORAGE_IDE :\r
1178 //\r
1179 // The ATA controller is working at IDE mode\r
1180 //\r
1181 Instance->Mode = EfiAtaIdeMode;\r
1182\r
1183 Status = IdeModeInitialization (Instance);\r
1184 if (EFI_ERROR (Status)) {\r
1185 Status = EFI_DEVICE_ERROR;\r
1186 goto Done;\r
1187 }\r
1188 break;\r
1189 case PCI_CLASS_MASS_STORAGE_SATADPA :\r
1190 //\r
1191 // The ATA controller is working at AHCI mode\r
1192 //\r
1193 Instance->Mode = EfiAtaAhciMode;\r
1194\r
1195 Status = AhciModeInitialization (Instance);\r
1196\r
1197 if (EFI_ERROR (Status)) {\r
1198 Status = EFI_DEVICE_ERROR;\r
1199 goto Done;\r
1200 }\r
1201\r
1202 break;\r
1203 default :\r
1204 Status = EFI_UNSUPPORTED;\r
1205 }\r
1206\r
1207Done:\r
1208 return Status;\r
1209}\r
1210\r
1211/**\r
1212 Sends an ATA command to an ATA device that is attached to the ATA controller. This function\r
1213 supports both blocking I/O and non-blocking I/O. The blocking I/O functionality is required,\r
1214 and the non-blocking I/O functionality is optional.\r
1215\r
58727f29 1216 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.\r
1217 @param[in] Port The port number of the ATA device to send the command.\r
a41b5272 1218 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to send the command.\r
1219 If there is no port multiplier, then specify 0.\r
1220 @param[in, out] Packet A pointer to the ATA command to send to the ATA device specified by Port\r
1221 and PortMultiplierPort.\r
1222 @param[in] Event If non-blocking I/O is not supported then Event is ignored, and blocking\r
1223 I/O is performed. If Event is NULL, then blocking I/O is performed. If\r
1224 Event is not NULL and non blocking I/O is supported, then non-blocking\r
1225 I/O is performed, and Event will be signaled when the ATA command completes.\r
1226\r
58727f29 1227 @retval EFI_SUCCESS The ATA command was sent by the host. For bi-directional commands,\r
a41b5272 1228 InTransferLength bytes were transferred from InDataBuffer. For write and\r
1229 bi-directional commands, OutTransferLength bytes were transferred by OutDataBuffer.\r
1230 @retval EFI_BAD_BUFFER_SIZE The ATA command was not executed. The number of bytes that could be transferred\r
58727f29 1231 is returned in InTransferLength. For write and bi-directional commands,\r
a41b5272 1232 OutTransferLength bytes were transferred by OutDataBuffer.\r
1233 @retval EFI_NOT_READY The ATA command could not be sent because there are too many ATA commands\r
1234 already queued. The caller may retry again later.\r
1235 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the ATA command.\r
1236 @retval EFI_INVALID_PARAMETER Port, PortMultiplierPort, or the contents of Acb are invalid. The ATA\r
1237 command was not sent, so no additional status information is available.\r
1238\r
1239**/\r
1240EFI_STATUS\r
1241EFIAPI\r
1242AtaPassThruPassThru (\r
1243 IN EFI_ATA_PASS_THRU_PROTOCOL *This,\r
1244 IN UINT16 Port,\r
1245 IN UINT16 PortMultiplierPort,\r
1246 IN OUT EFI_ATA_PASS_THRU_COMMAND_PACKET *Packet,\r
1247 IN EFI_EVENT Event OPTIONAL\r
490b5ea1 1248 )\r
a41b5272 1249{\r
a41b5272 1250 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;\r
a41b5272 1251 LIST_ENTRY *Node;\r
1252 EFI_ATA_DEVICE_INFO *DeviceInfo;\r
1253 EFI_IDENTIFY_DATA *IdentifyData;\r
1254 UINT64 Capacity;\r
1255 UINT32 MaxSectorCount;\r
490b5ea1 1256 ATA_NONBLOCK_TASK *Task;\r
1257 EFI_TPL OldTpl;\r
a41b5272 1258\r
1259 Instance = ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);\r
1260\r
1261 if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->InDataBuffer, This->Mode->IoAlign)) {\r
1262 return EFI_INVALID_PARAMETER;\r
1263 }\r
1264\r
1265 if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->OutDataBuffer, This->Mode->IoAlign)) {\r
1266 return EFI_INVALID_PARAMETER;\r
1267 }\r
1268\r
1269 if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->Asb, This->Mode->IoAlign)) {\r
1270 return EFI_INVALID_PARAMETER;\r
1271 }\r
1272\r
df202d72
FT
1273 Node = SearchDeviceInfoList (Instance, Port, PortMultiplierPort, EfiIdeHarddisk);\r
1274\r
1275 if (Node == NULL) {\r
1276 Node = SearchDeviceInfoList(Instance, Port, PortMultiplierPort, EfiIdeCdrom);\r
1277 if (Node == NULL) {\r
1278 return EFI_INVALID_PARAMETER;\r
1279 }\r
1280 }\r
1281\r
a41b5272 1282 //\r
1283 // convert the transfer length from sector count to byte.\r
1284 //\r
58727f29 1285 if (((Packet->Length & EFI_ATA_PASS_THRU_LENGTH_BYTES) == 0) &&\r
a41b5272 1286 (Packet->InTransferLength != 0)) {\r
1287 Packet->InTransferLength = Packet->InTransferLength * 0x200;\r
1288 }\r
1289\r
1290 //\r
1291 // convert the transfer length from sector count to byte.\r
1292 //\r
1293 if (((Packet->Length & EFI_ATA_PASS_THRU_LENGTH_BYTES) == 0) &&\r
1294 (Packet->OutTransferLength != 0)) {\r
1295 Packet->OutTransferLength = Packet->OutTransferLength * 0x200;\r
1296 }\r
1297\r
a41b5272 1298 //\r
1299 // Check whether this device needs 48-bit addressing (ATAPI-6 ata device).\r
1300 // Per ATA-6 spec, word83: bit15 is zero and bit14 is one.\r
58727f29 1301 // If bit10 is one, it means the ata device support 48-bit addressing.\r
a41b5272 1302 //\r
1303 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);\r
1304 IdentifyData = DeviceInfo->IdentifyData;\r
1305 MaxSectorCount = 0x100;\r
1306 if ((IdentifyData->AtaData.command_set_supported_83 & (BIT10 | BIT15 | BIT14)) == 0x4400) {\r
1307 Capacity = *((UINT64 *)IdentifyData->AtaData.maximum_lba_for_48bit_addressing);\r
1308 if (Capacity > 0xFFFFFFF) {\r
1309 //\r
1310 // Capacity exceeds 120GB. 48-bit addressing is really needed\r
1311 // In this case, the max sector count is 0x10000\r
1312 //\r
1313 MaxSectorCount = 0x10000;\r
1314 }\r
1315 }\r
1316\r
1317 //\r
1318 // If the data buffer described by InDataBuffer/OutDataBuffer and InTransferLength/OutTransferLength\r
1319 // is too big to be transferred in a single command, then no data is transferred and EFI_BAD_BUFFER_SIZE\r
58727f29 1320 // is returned.\r
a41b5272 1321 //\r
58727f29 1322 if (((Packet->InTransferLength != 0) && (Packet->InTransferLength > MaxSectorCount * 0x200)) ||\r
a41b5272 1323 ((Packet->OutTransferLength != 0) && (Packet->OutTransferLength > MaxSectorCount * 0x200))) {\r
1324 return EFI_BAD_BUFFER_SIZE;\r
1325 }\r
1326\r
490b5ea1 1327 //\r
1328 // For non-blocking mode, queue the Task into the list.\r
1329 //\r
1330 if (Event != NULL) {\r
1331 Task = AllocateZeroPool (sizeof (ATA_NONBLOCK_TASK));\r
1332 if (Task == NULL) {\r
1333 return EFI_OUT_OF_RESOURCES;\r
1334 }\r
58727f29 1335\r
490b5ea1 1336 Task->Signature = ATA_NONBLOCKING_TASK_SIGNATURE;\r
1337 Task->Port = Port;\r
1338 Task->PortMultiplier = PortMultiplierPort;\r
1339 Task->Packet = Packet;\r
1340 Task->Event = Event;\r
1341 Task->IsStart = FALSE;\r
ab82122d
TF
1342 Task->RetryTimes = DivU64x32(Packet->Timeout, 1000) + 1;\r
1343 if (Packet->Timeout == 0) {\r
1344 Task->InfiniteWait = TRUE;\r
1345 } else {\r
1346 Task->InfiniteWait = FALSE;\r
1347 }\r
490b5ea1 1348\r
1349 OldTpl = gBS->RaiseTPL (TPL_NOTIFY);\r
1350 InsertTailList (&Instance->NonBlockingTaskList, &Task->Link);\r
1351 gBS->RestoreTPL (OldTpl);\r
a41b5272 1352\r
490b5ea1 1353 return EFI_SUCCESS;\r
1354 } else {\r
1355 return AtaPassThruPassThruExecute (\r
1356 Port,\r
1357 PortMultiplierPort,\r
1358 Packet,\r
1359 Instance,\r
1360 NULL\r
1361 );\r
a41b5272 1362 }\r
a41b5272 1363}\r
1364\r
1365/**\r
1366 Used to retrieve the list of legal port numbers for ATA devices on an ATA controller.\r
1367 These can either be the list of ports where ATA devices are actually present or the\r
1368 list of legal port numbers for the ATA controller. Regardless, the caller of this\r
1369 function must probe the port number returned to see if an ATA device is actually\r
1370 present at that location on the ATA controller.\r
1371\r
1372 The GetNextPort() function retrieves the port number on an ATA controller. If on input\r
1373 Port is 0xFFFF, then the port number of the first port on the ATA controller is returned\r
1374 in Port and EFI_SUCCESS is returned.\r
1375\r
1376 If Port is a port number that was returned on a previous call to GetNextPort(), then the\r
1377 port number of the next port on the ATA controller is returned in Port, and EFI_SUCCESS\r
1378 is returned. If Port is not 0xFFFF and Port was not returned on a previous call to\r
1379 GetNextPort(), then EFI_INVALID_PARAMETER is returned.\r
1380\r
1381 If Port is the port number of the last port on the ATA controller, then EFI_NOT_FOUND is\r
1382 returned.\r
1383\r
58727f29 1384 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.\r
a41b5272 1385 @param[in, out] Port On input, a pointer to the port number on the ATA controller.\r
1386 On output, a pointer to the next port number on the ATA\r
1387 controller. An input value of 0xFFFF retrieves the first port\r
1388 number on the ATA controller.\r
1389\r
1390 @retval EFI_SUCCESS The next port number on the ATA controller was returned in Port.\r
1391 @retval EFI_NOT_FOUND There are no more ports on this ATA controller.\r
1392 @retval EFI_INVALID_PARAMETER Port is not 0xFFFF and Port was not returned on a previous call\r
1393 to GetNextPort().\r
1394\r
1395**/\r
1396EFI_STATUS\r
1397EFIAPI\r
1398AtaPassThruGetNextPort (\r
1399 IN EFI_ATA_PASS_THRU_PROTOCOL *This,\r
1400 IN OUT UINT16 *Port\r
1401 )\r
1402{\r
1403 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;\r
1404 LIST_ENTRY *Node;\r
1405 EFI_ATA_DEVICE_INFO *DeviceInfo;\r
1406\r
1407 Instance = ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);\r
1408\r
1409 if (Port == NULL) {\r
1410 return EFI_INVALID_PARAMETER;\r
1411 }\r
1412\r
1413 if (*Port == 0xFFFF) {\r
1414 //\r
1415 // If the Port is all 0xFF's, start to traverse the device list from the beginning\r
1416 //\r
1417 Node = GetFirstNode (&Instance->DeviceList);\r
1418\r
1419 while (!IsNull (&Instance->DeviceList, Node)) {\r
1420 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);\r
1421\r
1422 if (DeviceInfo->Type == EfiIdeHarddisk) {\r
1423 *Port = DeviceInfo->Port;\r
1424 goto Exit;\r
1425 }\r
1426\r
1427 Node = GetNextNode (&Instance->DeviceList, Node);\r
1428 }\r
1429\r
1430 return EFI_NOT_FOUND;\r
1431 } else if (*Port == Instance->PreviousPort) {\r
1432 Node = GetFirstNode (&Instance->DeviceList);\r
1433\r
1434 while (!IsNull (&Instance->DeviceList, Node)) {\r
1435 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);\r
1436\r
1437 if ((DeviceInfo->Type == EfiIdeHarddisk) &&\r
1438 (DeviceInfo->Port > *Port)){\r
1439 *Port = DeviceInfo->Port;\r
1440 goto Exit;\r
1441 }\r
1442\r
1443 Node = GetNextNode (&Instance->DeviceList, Node);\r
1444 }\r
1445\r
1446 return EFI_NOT_FOUND;\r
1447 } else {\r
1448 //\r
1449 // Port is not equal to 0xFFFF and also not equal to previous return value\r
1450 //\r
1451 return EFI_INVALID_PARAMETER;\r
1452 }\r
490b5ea1 1453\r
a41b5272 1454Exit:\r
1455 //\r
1456 // Update the PreviousPort and PreviousPortMultiplier.\r
1457 //\r
1458 Instance->PreviousPort = *Port;\r
1459\r
1460 return EFI_SUCCESS;\r
1461}\r
1462\r
1463/**\r
58727f29 1464 Used to retrieve the list of legal port multiplier port numbers for ATA devices on a port of an ATA\r
1465 controller. These can either be the list of port multiplier ports where ATA devices are actually\r
1466 present on port or the list of legal port multiplier ports on that port. Regardless, the caller of this\r
1467 function must probe the port number and port multiplier port number returned to see if an ATA\r
a41b5272 1468 device is actually present.\r
1469\r
58727f29 1470 The GetNextDevice() function retrieves the port multiplier port number of an ATA device\r
a41b5272 1471 present on a port of an ATA controller.\r
490b5ea1 1472\r
58727f29 1473 If PortMultiplierPort points to a port multiplier port number value that was returned on a\r
a41b5272 1474 previous call to GetNextDevice(), then the port multiplier port number of the next ATA device\r
1475 on the port of the ATA controller is returned in PortMultiplierPort, and EFI_SUCCESS is\r
1476 returned.\r
490b5ea1 1477\r
58727f29 1478 If PortMultiplierPort points to 0xFFFF, then the port multiplier port number of the first\r
1479 ATA device on port of the ATA controller is returned in PortMultiplierPort and\r
a41b5272 1480 EFI_SUCCESS is returned.\r
490b5ea1 1481\r
a41b5272 1482 If PortMultiplierPort is not 0xFFFF and the value pointed to by PortMultiplierPort\r
1483 was not returned on a previous call to GetNextDevice(), then EFI_INVALID_PARAMETER\r
1484 is returned.\r
490b5ea1 1485\r
58727f29 1486 If PortMultiplierPort is the port multiplier port number of the last ATA device on the port of\r
a41b5272 1487 the ATA controller, then EFI_NOT_FOUND is returned.\r
1488\r
1489 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.\r
1490 @param[in] Port The port number present on the ATA controller.\r
1491 @param[in, out] PortMultiplierPort On input, a pointer to the port multiplier port number of an\r
58727f29 1492 ATA device present on the ATA controller.\r
1493 If on input a PortMultiplierPort of 0xFFFF is specified,\r
a41b5272 1494 then the port multiplier port number of the first ATA device\r
1495 is returned. On output, a pointer to the port multiplier port\r
1496 number of the next ATA device present on an ATA controller.\r
1497\r
1498 @retval EFI_SUCCESS The port multiplier port number of the next ATA device on the port\r
1499 of the ATA controller was returned in PortMultiplierPort.\r
1500 @retval EFI_NOT_FOUND There are no more ATA devices on this port of the ATA controller.\r
1501 @retval EFI_INVALID_PARAMETER PortMultiplierPort is not 0xFFFF, and PortMultiplierPort was not\r
1502 returned on a previous call to GetNextDevice().\r
1503\r
1504**/\r
1505EFI_STATUS\r
1506EFIAPI\r
1507AtaPassThruGetNextDevice (\r
1508 IN EFI_ATA_PASS_THRU_PROTOCOL *This,\r
1509 IN UINT16 Port,\r
1510 IN OUT UINT16 *PortMultiplierPort\r
1511 )\r
1512{\r
1513 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;\r
1514 LIST_ENTRY *Node;\r
1515 EFI_ATA_DEVICE_INFO *DeviceInfo;\r
1516\r
1517 Instance = ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);\r
1518\r
1519 if (PortMultiplierPort == NULL) {\r
1520 return EFI_INVALID_PARAMETER;\r
1521 }\r
1522\r
1523 if (*PortMultiplierPort == 0xFFFF) {\r
1524 //\r
1525 // If the PortMultiplierPort is all 0xFF's, start to traverse the device list from the beginning\r
1526 //\r
1527 Node = GetFirstNode (&Instance->DeviceList);\r
1528\r
1529 while (!IsNull (&Instance->DeviceList, Node)) {\r
1530 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);\r
1531\r
1532 if ((DeviceInfo->Type == EfiIdeHarddisk) &&\r
1533 (DeviceInfo->Port == Port)){\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 if (*PortMultiplierPort == Instance->PreviousPortMultiplier) {\r
1543 Node = GetFirstNode (&Instance->DeviceList);\r
1544\r
1545 while (!IsNull (&Instance->DeviceList, Node)) {\r
1546 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);\r
1547\r
1548 if ((DeviceInfo->Type == EfiIdeHarddisk) &&\r
1549 (DeviceInfo->Port == Port) &&\r
1550 (DeviceInfo->PortMultiplier > *PortMultiplierPort)){\r
1551 *PortMultiplierPort = DeviceInfo->PortMultiplier;\r
1552 goto Exit;\r
1553 }\r
1554\r
1555 Node = GetNextNode (&Instance->DeviceList, Node);\r
1556 }\r
1557\r
1558 return EFI_NOT_FOUND;\r
1559 } else {\r
1560 //\r
1561 // PortMultiplierPort is not equal to 0xFFFF and also not equal to previous return value\r
1562 //\r
1563 return EFI_INVALID_PARAMETER;\r
1564 }\r
490b5ea1 1565\r
a41b5272 1566Exit:\r
1567 //\r
1568 // Update the PreviousPort and PreviousPortMultiplier.\r
1569 //\r
1570 Instance->PreviousPortMultiplier = *PortMultiplierPort;\r
1571\r
1572 return EFI_SUCCESS;\r
1573}\r
1574\r
1575/**\r
1576 Used to allocate and build a device path node for an ATA device on an ATA controller.\r
1577\r
1578 The BuildDevicePath() function allocates and builds a single device node for the ATA\r
1579 device specified by Port and PortMultiplierPort. If the ATA device specified by Port and\r
1580 PortMultiplierPort is not present on the ATA controller, then EFI_NOT_FOUND is returned.\r
1581 If DevicePath is NULL, then EFI_INVALID_PARAMETER is returned. If there are not enough\r
1582 resources to allocate the device path node, then EFI_OUT_OF_RESOURCES is returned.\r
1583\r
1584 Otherwise, DevicePath is allocated with the boot service AllocatePool(), the contents of\r
1585 DevicePath are initialized to describe the ATA device specified by Port and PortMultiplierPort,\r
1586 and EFI_SUCCESS is returned.\r
1587\r
1588 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.\r
1589 @param[in] Port Port specifies the port number of the ATA device for which a\r
1590 device path node is to be allocated and built.\r
1591 @param[in] PortMultiplierPort The port multiplier port number of the ATA device for which a\r
1592 device path node is to be allocated and built. If there is no\r
1593 port multiplier, then specify 0.\r
1594 @param[in, out] DevicePath A pointer to a single device path node that describes the ATA\r
1595 device specified by Port and PortMultiplierPort. This function\r
1596 is responsible for allocating the buffer DevicePath with the\r
1597 boot service AllocatePool(). It is the caller's responsibility\r
1598 to free DevicePath when the caller is finished with DevicePath.\r
1599 @retval EFI_SUCCESS The device path node that describes the ATA device specified by\r
1600 Port and PortMultiplierPort was allocated and returned in DevicePath.\r
1601 @retval EFI_NOT_FOUND The ATA device specified by Port and PortMultiplierPort does not\r
1602 exist on the ATA controller.\r
1603 @retval EFI_INVALID_PARAMETER DevicePath is NULL.\r
1604 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.\r
1605\r
1606**/\r
1607EFI_STATUS\r
1608EFIAPI\r
1609AtaPassThruBuildDevicePath (\r
1610 IN EFI_ATA_PASS_THRU_PROTOCOL *This,\r
1611 IN UINT16 Port,\r
1612 IN UINT16 PortMultiplierPort,\r
1613 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath\r
1614 )\r
1615{\r
1616 EFI_DEV_PATH *DevicePathNode;\r
1617 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;\r
1618 LIST_ENTRY *Node;\r
1619\r
1620 Instance = ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);\r
1621\r
1622 //\r
1623 // Validate parameters passed in.\r
1624 //\r
1625 if (DevicePath == NULL) {\r
1626 return EFI_INVALID_PARAMETER;\r
1627 }\r
1628\r
1629 Node = SearchDeviceInfoList(Instance, Port, PortMultiplierPort, EfiIdeHarddisk);\r
1630 if (Node == NULL) {\r
1631 return EFI_NOT_FOUND;\r
1632 }\r
490b5ea1 1633\r
a41b5272 1634 if (Instance->Mode == EfiAtaIdeMode) {\r
1635 DevicePathNode = AllocateCopyPool (sizeof (ATAPI_DEVICE_PATH), &mAtapiDevicePathTemplate);\r
1636 if (DevicePathNode == NULL) {\r
1637 return EFI_OUT_OF_RESOURCES;\r
1638 }\r
1639 DevicePathNode->Atapi.PrimarySecondary = (UINT8) Port;\r
1640 DevicePathNode->Atapi.SlaveMaster = (UINT8) PortMultiplierPort;\r
1641 DevicePathNode->Atapi.Lun = 0;\r
1642 } else {\r
1643 DevicePathNode = AllocateCopyPool (sizeof (SATA_DEVICE_PATH), &mSataDevicePathTemplate);\r
1644 if (DevicePathNode == NULL) {\r
1645 return EFI_OUT_OF_RESOURCES;\r
1646 }\r
1647\r
1648 DevicePathNode->Sata.HBAPortNumber = Port;\r
1649 DevicePathNode->Sata.PortMultiplierPortNumber = PortMultiplierPort;\r
1650 DevicePathNode->Sata.Lun = 0;\r
1651 }\r
1652\r
1653 *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) DevicePathNode;\r
1654\r
1655 return EFI_SUCCESS;\r
1656}\r
1657\r
1658/**\r
1659 Used to translate a device path node to a port number and port multiplier port number.\r
1660\r
1661 The GetDevice() function determines the port and port multiplier port number associated with\r
1662 the ATA device described by DevicePath. If DevicePath is a device path node type that the\r
58727f29 1663 ATA Pass Thru driver supports, then the ATA Pass Thru driver will attempt to translate the contents\r
a41b5272 1664 DevicePath into a port number and port multiplier port number.\r
1665\r
1666 If this translation is successful, then that port number and port multiplier port number are returned\r
1667 in Port and PortMultiplierPort, and EFI_SUCCESS is returned.\r
1668\r
1669 If DevicePath, Port, or PortMultiplierPort are NULL, then EFI_INVALID_PARAMETER is returned.\r
1670\r
58727f29 1671 If DevicePath is not a device path node type that the ATA Pass Thru driver supports, then\r
a41b5272 1672 EFI_UNSUPPORTED is returned.\r
1673\r
58727f29 1674 If DevicePath is a device path node type that the ATA Pass Thru driver supports, but there is not\r
1675 a valid translation from DevicePath to a port number and port multiplier port number, then\r
a41b5272 1676 EFI_NOT_FOUND is returned.\r
1677\r
1678 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.\r
1679 @param[in] DevicePath A pointer to the device path node that describes an ATA device on the\r
1680 ATA controller.\r
1681 @param[out] Port On return, points to the port number of an ATA device on the ATA controller.\r
1682 @param[out] PortMultiplierPort On return, points to the port multiplier port number of an ATA device\r
1683 on the ATA controller.\r
1684\r
1685 @retval EFI_SUCCESS DevicePath was successfully translated to a port number and port multiplier\r
1686 port number, and they were returned in Port and PortMultiplierPort.\r
1687 @retval EFI_INVALID_PARAMETER DevicePath is NULL.\r
1688 @retval EFI_INVALID_PARAMETER Port is NULL.\r
1689 @retval EFI_INVALID_PARAMETER PortMultiplierPort is NULL.\r
1690 @retval EFI_UNSUPPORTED This driver does not support the device path node type in DevicePath.\r
1691 @retval EFI_NOT_FOUND A valid translation from DevicePath to a port number and port multiplier\r
1692 port number does not exist.\r
1693**/\r
1694EFI_STATUS\r
1695EFIAPI\r
1696AtaPassThruGetDevice (\r
1697 IN EFI_ATA_PASS_THRU_PROTOCOL *This,\r
1698 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
1699 OUT UINT16 *Port,\r
1700 OUT UINT16 *PortMultiplierPort\r
1701 )\r
1702{\r
1703 EFI_DEV_PATH *DevicePathNode;\r
1704 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;\r
1705 LIST_ENTRY *Node;\r
1706\r
1707 Instance = ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);\r
1708\r
1709 //\r
1710 // Validate parameters passed in.\r
1711 //\r
1712 if (DevicePath == NULL || Port == NULL || PortMultiplierPort == NULL) {\r
1713 return EFI_INVALID_PARAMETER;\r
1714 }\r
1715\r
1716 //\r
1717 // Check whether the DevicePath belongs to SCSI_DEVICE_PATH or ATAPI_DEVICE_PATH\r
1718 //\r
1719 if ((DevicePath->Type != MESSAGING_DEVICE_PATH) ||\r
1720 ((DevicePath->SubType != MSG_SATA_DP) &&\r
1721 (DevicePath->SubType != MSG_ATAPI_DP)) ||\r
1722 ((DevicePathNodeLength(DevicePath) != sizeof(ATAPI_DEVICE_PATH)) &&\r
1723 (DevicePathNodeLength(DevicePath) != sizeof(SATA_DEVICE_PATH)))) {\r
1724 return EFI_UNSUPPORTED;\r
1725 }\r
1726\r
1727 DevicePathNode = (EFI_DEV_PATH *) DevicePath;\r
1728\r
1729 if (Instance->Mode == EfiAtaIdeMode) {\r
1730 *Port = DevicePathNode->Atapi.PrimarySecondary;\r
1731 *PortMultiplierPort = DevicePathNode->Atapi.SlaveMaster;\r
1732 } else {\r
1733 *Port = DevicePathNode->Sata.HBAPortNumber;\r
1734 *PortMultiplierPort = DevicePathNode->Sata.PortMultiplierPortNumber;\r
1735 }\r
1736\r
1737 Node = SearchDeviceInfoList(Instance, *Port, *PortMultiplierPort, EfiIdeHarddisk);\r
1738\r
1739 if (Node == NULL) {\r
1740 return EFI_NOT_FOUND;\r
1741 }\r
1742\r
1743 return EFI_SUCCESS;\r
1744}\r
1745\r
1746/**\r
1747 Resets a specific port on the ATA controller. This operation also resets all the ATA devices\r
1748 connected to the port.\r
1749\r
1750 The ResetChannel() function resets an a specific port on an ATA controller. This operation\r
1751 resets all the ATA devices connected to that port. If this ATA controller does not support\r
1752 a reset port operation, then EFI_UNSUPPORTED is returned.\r
1753\r
1754 If a device error occurs while executing that port reset operation, then EFI_DEVICE_ERROR is\r
1755 returned.\r
1756\r
1757 If a timeout occurs during the execution of the port reset operation, then EFI_TIMEOUT is returned.\r
1758\r
1759 If the port reset operation is completed, then EFI_SUCCESS is returned.\r
1760\r
1761 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.\r
1762 @param[in] Port The port number on the ATA controller.\r
1763\r
1764 @retval EFI_SUCCESS The ATA controller port was reset.\r
1765 @retval EFI_UNSUPPORTED The ATA controller does not support a port reset operation.\r
1766 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the ATA port.\r
1767 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the ATA port.\r
1768\r
1769**/\r
1770EFI_STATUS\r
1771EFIAPI\r
1772AtaPassThruResetPort (\r
1773 IN EFI_ATA_PASS_THRU_PROTOCOL *This,\r
1774 IN UINT16 Port\r
1775 )\r
1776{\r
71fd9fae
TF
1777 //\r
1778 // Return success directly then upper layer driver could think reset port operation is done.\r
1779 //\r
1780 return EFI_SUCCESS;\r
a41b5272 1781}\r
1782\r
1783/**\r
1784 Resets an ATA device that is connected to an ATA controller.\r
1785\r
1786 The ResetDevice() function resets the ATA device specified by Port and PortMultiplierPort.\r
1787 If this ATA controller does not support a device reset operation, then EFI_UNSUPPORTED is\r
1788 returned.\r
1789\r
58727f29 1790 If Port or PortMultiplierPort are not in a valid range for this ATA controller, then\r
a41b5272 1791 EFI_INVALID_PARAMETER is returned.\r
1792\r
1793 If a device error occurs while executing that device reset operation, then EFI_DEVICE_ERROR\r
1794 is returned.\r
1795\r
1796 If a timeout occurs during the execution of the device reset operation, then EFI_TIMEOUT is\r
1797 returned.\r
1798\r
1799 If the device reset operation is completed, then EFI_SUCCESS is returned.\r
1800\r
1801 @param[in] This A pointer to the EFI_ATA_PASS_THRU_PROTOCOL instance.\r
1802 @param[in] Port Port represents the port number of the ATA device to be reset.\r
1803 @param[in] PortMultiplierPort The port multiplier port number of the ATA device to reset.\r
1804 If there is no port multiplier, then specify 0.\r
1805 @retval EFI_SUCCESS The ATA device specified by Port and PortMultiplierPort was reset.\r
1806 @retval EFI_UNSUPPORTED The ATA controller does not support a device reset operation.\r
1807 @retval EFI_INVALID_PARAMETER Port or PortMultiplierPort are invalid.\r
1808 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the ATA device\r
1809 specified by Port and PortMultiplierPort.\r
1810 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the ATA device\r
1811 specified by Port and PortMultiplierPort.\r
1812\r
1813**/\r
1814EFI_STATUS\r
1815EFIAPI\r
1816AtaPassThruResetDevice (\r
1817 IN EFI_ATA_PASS_THRU_PROTOCOL *This,\r
1818 IN UINT16 Port,\r
1819 IN UINT16 PortMultiplierPort\r
1820 )\r
1821{\r
59b1b9d2
TF
1822 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;\r
1823 LIST_ENTRY *Node;\r
1824\r
1825 Instance = ATA_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);\r
1826\r
1827 Node = SearchDeviceInfoList (Instance, Port, PortMultiplierPort, EfiIdeHarddisk);\r
1828\r
1829 if (Node == NULL) {\r
1830 return EFI_INVALID_PARAMETER;\r
1831 }\r
1832\r
71fd9fae
TF
1833 //\r
1834 // Return success directly then upper layer driver could think reset device operation is done.\r
1835 //\r
1836 return EFI_SUCCESS;\r
a41b5272 1837}\r
1838\r
73a9e822
TF
1839/**\r
1840 Sumbit ATAPI request sense command.\r
1841\r
1842 @param[in] This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.\r
1843 @param[in] Target The Target is an array of size TARGET_MAX_BYTES and it represents\r
1844 the id of the SCSI device to send the SCSI Request Packet. Each\r
1845 transport driver may choose to utilize a subset of this size to suit the needs\r
1846 of transport target representation. For example, a Fibre Channel driver\r
1847 may use only 8 bytes (WWN) to represent an FC target.\r
1848 @param[in] Lun The LUN of the SCSI device to send the SCSI Request Packet.\r
1849 @param[in] SenseData A pointer to store sense data.\r
1850 @param[in] SenseDataLength The sense data length.\r
1851 @param[in] Timeout The timeout value to execute this cmd, uses 100ns as a unit.\r
1852\r
1853 @retval EFI_SUCCESS Send out the ATAPI packet command successfully.\r
1854 @retval EFI_DEVICE_ERROR The device failed to send data.\r
1855\r
1856**/\r
1857EFI_STATUS\r
1858EFIAPI\r
1859AtaPacketRequestSense (\r
1860 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,\r
1861 IN UINT8 *Target,\r
1862 IN UINT64 Lun,\r
1863 IN VOID *SenseData,\r
1864 IN UINT8 SenseDataLength,\r
1865 IN UINT64 Timeout\r
1866 )\r
1867{\r
1868 EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET Packet;\r
1869 UINT8 Cdb[12];\r
1870 EFI_STATUS Status;\r
1871\r
1872 ZeroMem (&Packet, sizeof (EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET));\r
1873 ZeroMem (Cdb, 12);\r
1874\r
1875 Cdb[0] = ATA_CMD_REQUEST_SENSE;\r
1876 Cdb[4] = SenseDataLength;\r
1877\r
1878 Packet.Timeout = Timeout;\r
1879 Packet.Cdb = Cdb;\r
1880 Packet.CdbLength = 12;\r
1881 Packet.DataDirection = EFI_EXT_SCSI_DATA_DIRECTION_READ;\r
1882 Packet.InDataBuffer = SenseData;\r
1883 Packet.InTransferLength = SenseDataLength;\r
1884\r
1885 Status = ExtScsiPassThruPassThru (This, Target, Lun, &Packet, NULL);\r
1886\r
1887 return Status;\r
1888}\r
1889\r
a41b5272 1890/**\r
58727f29 1891 Sends a SCSI Request Packet to a SCSI device that is attached to the SCSI channel. This function\r
a41b5272 1892 supports both blocking I/O and nonblocking I/O. The blocking I/O functionality is required, and the\r
58727f29 1893 nonblocking I/O functionality is optional.\r
a41b5272 1894\r
1895 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.\r
1896 @param Target The Target is an array of size TARGET_MAX_BYTES and it represents\r
1897 the id of the SCSI device to send the SCSI Request Packet. Each\r
1898 transport driver may choose to utilize a subset of this size to suit the needs\r
1899 of transport target representation. For example, a Fibre Channel driver\r
1900 may use only 8 bytes (WWN) to represent an FC target.\r
1901 @param Lun The LUN of the SCSI device to send the SCSI Request Packet.\r
1902 @param Packet A pointer to the SCSI Request Packet to send to the SCSI device\r
1903 specified by Target and Lun.\r
1904 @param Event If nonblocking I/O is not supported then Event is ignored, and blocking\r
1905 I/O is performed. If Event is NULL, then blocking I/O is performed. If\r
1906 Event is not NULL and non blocking I/O is supported, then\r
1907 nonblocking I/O is performed, and Event will be signaled when the\r
1908 SCSI Request Packet completes.\r
1909\r
1910 @retval EFI_SUCCESS The SCSI Request Packet was sent by the host. For bi-directional\r
1911 commands, InTransferLength bytes were transferred from\r
1912 InDataBuffer. For write and bi-directional commands,\r
1913 OutTransferLength bytes were transferred by\r
1914 OutDataBuffer.\r
1915 @retval EFI_BAD_BUFFER_SIZE The SCSI Request Packet was not executed. The number of bytes that\r
1916 could be transferred is returned in InTransferLength. For write\r
1917 and bi-directional commands, OutTransferLength bytes were\r
1918 transferred by OutDataBuffer.\r
1919 @retval EFI_NOT_READY The SCSI Request Packet could not be sent because there are too many\r
1920 SCSI Request Packets already queued. The caller may retry again later.\r
1921 @retval EFI_DEVICE_ERROR A device error occurred while attempting to send the SCSI Request\r
1922 Packet.\r
1923 @retval EFI_INVALID_PARAMETER Target, Lun, or the contents of ScsiRequestPacket are invalid.\r
1924 @retval EFI_UNSUPPORTED The command described by the SCSI Request Packet is not supported\r
1925 by the host adapter. This includes the case of Bi-directional SCSI\r
1926 commands not supported by the implementation. The SCSI Request\r
1927 Packet was not sent, so no additional status information is available.\r
1928 @retval EFI_TIMEOUT A timeout occurred while waiting for the SCSI Request Packet to execute.\r
1929\r
1930**/\r
1931EFI_STATUS\r
1932EFIAPI\r
1933ExtScsiPassThruPassThru (\r
1934 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,\r
1935 IN UINT8 *Target,\r
1936 IN UINT64 Lun,\r
1937 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet,\r
1938 IN EFI_EVENT Event OPTIONAL\r
1939 )\r
1940{\r
1941 EFI_STATUS Status;\r
1942 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;\r
1943 UINT8 Port;\r
1944 UINT8 PortMultiplier;\r
1945 EFI_ATA_HC_WORK_MODE Mode;\r
1946 LIST_ENTRY *Node;\r
1947 EFI_ATA_DEVICE_INFO *DeviceInfo;\r
73a9e822
TF
1948 BOOLEAN SenseReq;\r
1949 EFI_SCSI_SENSE_DATA *PtrSenseData;\r
1950 UINTN SenseDataLen;\r
1951 EFI_STATUS SenseStatus;\r
a41b5272 1952\r
73a9e822
TF
1953 SenseDataLen = 0;\r
1954 Instance = EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);\r
a41b5272 1955\r
1956 if ((Packet == NULL) || (Packet->Cdb == NULL)) {\r
1957 return EFI_INVALID_PARAMETER;\r
1958 }\r
1959\r
1960 //\r
1961 // Don't support variable length CDB\r
1962 //\r
1963 if ((Packet->CdbLength != 6) && (Packet->CdbLength != 10) &&\r
1964 (Packet->CdbLength != 12) && (Packet->CdbLength != 16)) {\r
1965 return EFI_INVALID_PARAMETER;\r
1966 }\r
58727f29 1967\r
73a9e822
TF
1968 if ((Packet->SenseDataLength != 0) && (Packet->SenseData == NULL)) {\r
1969 return EFI_INVALID_PARAMETER;\r
1970 }\r
1971\r
a41b5272 1972 if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->InDataBuffer, This->Mode->IoAlign)) {\r
1973 return EFI_INVALID_PARAMETER;\r
1974 }\r
1975\r
1976 if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->OutDataBuffer, This->Mode->IoAlign)) {\r
1977 return EFI_INVALID_PARAMETER;\r
1978 }\r
1979\r
1980 if ((This->Mode->IoAlign > 1) && !IS_ALIGNED(Packet->SenseData, This->Mode->IoAlign)) {\r
1981 return EFI_INVALID_PARAMETER;\r
1982 }\r
1983\r
1984 //\r
1985 // For ATAPI device, doesn't support multiple LUN device.\r
1986 //\r
1987 if (Lun != 0) {\r
1988 return EFI_INVALID_PARAMETER;\r
1989 }\r
1990\r
58727f29 1991 //\r
a41b5272 1992 // The layout of Target array:\r
1993 // ________________________________________________________________________\r
1994 // | Byte 0 | Byte 1 | ... | TARGET_MAX_BYTES - 1 |\r
1995 // |_____________________|_____________________|_____|______________________|\r
1996 // | | The port multiplier | | |\r
1997 // | The port number | port number | N/A | N/A |\r
1998 // |_____________________|_____________________|_____|______________________|\r
1999 //\r
2000 // For ATAPI device, 2 bytes is enough to represent the location of SCSI device.\r
2001 //\r
2002 Port = Target[0];\r
2003 PortMultiplier = Target[1];\r
2004\r
2005 Node = SearchDeviceInfoList(Instance, Port, PortMultiplier, EfiIdeCdrom);\r
2006 if (Node == NULL) {\r
2007 return EFI_INVALID_PARAMETER;\r
2008 }\r
2009\r
2010 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);\r
2011\r
2012 //\r
2013 // ATA_CMD_IDENTIFY_DEVICE cmd is a ATA cmd but not a SCSI cmd.\r
2014 // Normally it should NOT be passed down through ExtScsiPassThru protocol interface.\r
2015 // But to response EFI_DISK_INFO.Identify() request from ScsiDisk, we should handle this command.\r
2016 //\r
2017 if (*((UINT8*)Packet->Cdb) == ATA_CMD_IDENTIFY_DEVICE) {\r
2018 CopyMem (Packet->InDataBuffer, DeviceInfo->IdentifyData, sizeof (EFI_IDENTIFY_DATA));\r
73a9e822
TF
2019 //\r
2020 // For IDENTIFY DEVICE cmd, we don't need to get sense data.\r
2021 //\r
2022 Packet->SenseDataLength = 0;\r
a41b5272 2023 return EFI_SUCCESS;\r
2024 }\r
2025\r
2026 Mode = Instance->Mode;\r
2027 switch (Mode) {\r
2028 case EfiAtaIdeMode:\r
2029 //\r
2030 // Reassign IDE mode io port registers' base addresses\r
2031 //\r
2032 Status = GetIdeRegisterIoAddr (Instance->PciIo, Instance->IdeRegisters);\r
2033\r
2034 if (EFI_ERROR (Status)) {\r
2035 return Status;\r
2036 }\r
2037\r
2038 Status = AtaPacketCommandExecute (Instance->PciIo, &Instance->IdeRegisters[Port], Port, PortMultiplier, Packet);\r
2039 break;\r
2040 case EfiAtaAhciMode:\r
2041 Status = AhciPacketCommandExecute (Instance->PciIo, &Instance->AhciRegisters, Port, PortMultiplier, Packet);\r
2042 break;\r
2043 default :\r
2044 Status = EFI_DEVICE_ERROR;\r
2045 break;\r
2046 }\r
2047\r
73a9e822
TF
2048 //\r
2049 // If the cmd doesn't get executed correctly, then check sense data.\r
2050 //\r
2051 if (EFI_ERROR (Status) && (Packet->SenseDataLength != 0) && (*((UINT8*)Packet->Cdb) != ATA_CMD_REQUEST_SENSE)) {\r
2052 PtrSenseData = AllocateAlignedPages (EFI_SIZE_TO_PAGES (sizeof (EFI_SCSI_SENSE_DATA)), This->Mode->IoAlign);\r
2053 if (PtrSenseData == NULL) {\r
2054 return EFI_DEVICE_ERROR;\r
2055 }\r
2056\r
2057 for (SenseReq = TRUE; SenseReq;) {\r
2058 SenseStatus = AtaPacketRequestSense (\r
2059 This,\r
2060 Target,\r
2061 Lun,\r
2062 PtrSenseData,\r
2063 sizeof (EFI_SCSI_SENSE_DATA),\r
2064 Packet->Timeout\r
2065 );\r
2066 if (EFI_ERROR (SenseStatus)) {\r
2067 break;\r
2068 }\r
2069\r
2070 CopyMem ((UINT8*)Packet->SenseData + SenseDataLen, PtrSenseData, sizeof (EFI_SCSI_SENSE_DATA));\r
2071 SenseDataLen += sizeof (EFI_SCSI_SENSE_DATA);\r
2072\r
2073 //\r
2074 // no more sense key or number of sense keys exceeds predefined,\r
2075 // skip the loop.\r
2076 //\r
2077 if ((PtrSenseData->Sense_Key == EFI_SCSI_SK_NO_SENSE) || \r
2078 (SenseDataLen + sizeof (EFI_SCSI_SENSE_DATA) > Packet->SenseDataLength)) {\r
2079 SenseReq = FALSE;\r
2080 }\r
2081 }\r
2082 FreeAlignedPages (PtrSenseData, EFI_SIZE_TO_PAGES (sizeof (EFI_SCSI_SENSE_DATA)));\r
2083 }\r
2084 //\r
2085 // Update the SenseDataLength field to the data length received.\r
2086 //\r
2087 Packet->SenseDataLength = (UINT8)SenseDataLen;\r
a41b5272 2088 return Status;\r
2089}\r
2090\r
2091/**\r
58727f29 2092 Used to retrieve the list of legal Target IDs and LUNs for SCSI devices on a SCSI channel. These\r
a41b5272 2093 can either be the list SCSI devices that are actually present on the SCSI channel, or the list of legal\r
58727f29 2094 Target Ids and LUNs for the SCSI channel. Regardless, the caller of this function must probe the\r
2095 Target ID and LUN returned to see if a SCSI device is actually present at that location on the SCSI\r
490b5ea1 2096 channel.\r
a41b5272 2097\r
2098 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.\r
2099 @param Target On input, a pointer to the Target ID (an array of size\r
2100 TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.\r
2101 On output, a pointer to the Target ID (an array of\r
2102 TARGET_MAX_BYTES) of the next SCSI device present on a SCSI\r
2103 channel. An input value of 0xF(all bytes in the array are 0xF) in the\r
2104 Target array retrieves the Target ID of the first SCSI device present on a\r
2105 SCSI channel.\r
2106 @param Lun On input, a pointer to the LUN of a SCSI device present on the SCSI\r
2107 channel. On output, a pointer to the LUN of the next SCSI device present\r
2108 on a SCSI channel.\r
2109\r
2110 @retval EFI_SUCCESS The Target ID and LUN of the next SCSI device on the SCSI\r
2111 channel was returned in Target and Lun.\r
2112 @retval EFI_INVALID_PARAMETER Target array is not all 0xF, and Target and Lun were\r
2113 not returned on a previous call to GetNextTargetLun().\r
2114 @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.\r
2115\r
2116**/\r
2117EFI_STATUS\r
2118EFIAPI\r
2119ExtScsiPassThruGetNextTargetLun (\r
2120 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,\r
2121 IN OUT UINT8 **Target,\r
2122 IN OUT UINT64 *Lun\r
2123 )\r
2124{\r
2125 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;\r
2126 LIST_ENTRY *Node;\r
2127 EFI_ATA_DEVICE_INFO *DeviceInfo;\r
2128 UINT8 *Target8;\r
2129 UINT16 *Target16;\r
2130\r
2131 Instance = EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);\r
2132\r
2133 if (Target == NULL || Lun == NULL) {\r
2134 return EFI_INVALID_PARAMETER;\r
2135 }\r
2136\r
2137 if (*Target == NULL) {\r
2138 return EFI_INVALID_PARAMETER;\r
2139 }\r
2140\r
2141 Target8 = *Target;\r
2142 Target16 = (UINT16 *)*Target;\r
2143\r
2144 if (CompareMem(Target8, mScsiId, TARGET_MAX_BYTES) != 0) {\r
2145 //\r
2146 // For ATAPI device, we use 2 least significant bytes to represent the location of SCSI device.\r
2147 // So the higher bytes in Target array should be 0xFF.\r
2148 //\r
2149 if (CompareMem (&Target8[2], &mScsiId[2], TARGET_MAX_BYTES - 2) != 0) {\r
2150 return EFI_INVALID_PARAMETER;\r
2151 }\r
2152\r
2153 //\r
2154 // When Target is not all 0xFF's, compare 2 least significant bytes with\r
2155 // previous target id to see if it is returned by previous call.\r
2156 //\r
2157 if ((*Target16 != Instance->PreviousTargetId) ||\r
2158 (*Lun != Instance->PreviousLun)) {\r
2159 return EFI_INVALID_PARAMETER;\r
2160 }\r
2161\r
2162 //\r
2163 // Traverse the whole device list to find the next cdrom closed to\r
2164 // the device signified by Target[0] and Target[1].\r
2165 //\r
2166 // Note that we here use a tricky way to find the next cdrom :\r
2167 // All ata devices are detected and inserted into the device list\r
2168 // sequentially.\r
2169 //\r
2170 Node = GetFirstNode (&Instance->DeviceList);\r
2171\r
2172 while (!IsNull (&Instance->DeviceList, Node)) {\r
2173 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);\r
2174\r
2175 if ((DeviceInfo->Type == EfiIdeCdrom) &&\r
58727f29 2176 ((Target8[0] < DeviceInfo->Port) ||\r
d8982b03 2177 ((Target8[0] == DeviceInfo->Port) &&\r
2178 (Target8[1] < DeviceInfo->PortMultiplier)))) {\r
a41b5272 2179 Target8[0] = (UINT8)DeviceInfo->Port;\r
2180 Target8[1] = (UINT8)DeviceInfo->PortMultiplier;\r
2181 goto Exit;\r
2182 }\r
2183\r
2184 Node = GetNextNode (&Instance->DeviceList, Node);\r
2185 }\r
2186\r
2187 return EFI_NOT_FOUND;\r
2188 } else {\r
2189 //\r
2190 // If the array is all 0xFF's, start to traverse the device list from the beginning\r
2191 //\r
2192 Node = GetFirstNode (&Instance->DeviceList);\r
2193 while (!IsNull (&Instance->DeviceList, Node)) {\r
2194 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);\r
2195\r
2196 if (DeviceInfo->Type == EfiIdeCdrom) {\r
2197 Target8[0] = (UINT8)DeviceInfo->Port;\r
2198 Target8[1] = (UINT8)DeviceInfo->PortMultiplier;\r
2199 goto Exit;\r
2200 }\r
2201\r
2202 Node = GetNextNode (&Instance->DeviceList, Node);\r
2203 }\r
2204\r
2205 return EFI_NOT_FOUND;\r
2206 }\r
2207\r
2208Exit:\r
2209 *Lun = 0;\r
2210\r
2211 //\r
2212 // Update the PreviousTargetId.\r
2213 //\r
2214 Instance->PreviousTargetId = *Target16;\r
2215 Instance->PreviousLun = *Lun;\r
58727f29 2216\r
a41b5272 2217 return EFI_SUCCESS;\r
490b5ea1 2218}\r
a41b5272 2219\r
2220/**\r
2221 Used to allocate and build a device path node for a SCSI device on a SCSI channel.\r
2222\r
2223 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.\r
2224 @param Target The Target is an array of size TARGET_MAX_BYTES and it specifies the\r
2225 Target ID of the SCSI device for which a device path node is to be\r
2226 allocated and built. Transport drivers may chose to utilize a subset of\r
2227 this size to suit the representation of targets. For example, a Fibre\r
2228 Channel driver may use only 8 bytes (WWN) in the array to represent a\r
2229 FC target.\r
2230 @param Lun The LUN of the SCSI device for which a device path node is to be\r
2231 allocated and built.\r
2232 @param DevicePath A pointer to a single device path node that describes the SCSI device\r
2233 specified by Target and Lun. This function is responsible for\r
2234 allocating the buffer DevicePath with the boot service\r
2235 AllocatePool(). It is the caller's responsibility to free\r
2236 DevicePath when the caller is finished with DevicePath.\r
2237\r
2238 @retval EFI_SUCCESS The device path node that describes the SCSI device specified by\r
2239 Target and Lun was allocated and returned in\r
2240 DevicePath.\r
2241 @retval EFI_INVALID_PARAMETER DevicePath is NULL.\r
2242 @retval EFI_NOT_FOUND The SCSI devices specified by Target and Lun does not exist\r
2243 on the SCSI channel.\r
2244 @retval EFI_OUT_OF_RESOURCES There are not enough resources to allocate DevicePath.\r
2245\r
2246**/\r
2247EFI_STATUS\r
2248EFIAPI\r
2249ExtScsiPassThruBuildDevicePath (\r
2250 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,\r
2251 IN UINT8 *Target,\r
2252 IN UINT64 Lun,\r
2253 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath\r
2254 )\r
2255{\r
2256 EFI_DEV_PATH *DevicePathNode;\r
2257 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;\r
2258 UINT8 Port;\r
2259 UINT8 PortMultiplier;\r
2260\r
2261 Instance = EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);\r
2262\r
2263 Port = Target[0];\r
2264 PortMultiplier = Target[1];\r
2265\r
2266 //\r
2267 // Validate parameters passed in.\r
2268 //\r
2269 if (DevicePath == NULL) {\r
2270 return EFI_INVALID_PARAMETER;\r
2271 }\r
2272\r
2273 //\r
2274 // can not build device path for the SCSI Host Controller.\r
2275 //\r
2276 if (Lun != 0) {\r
2277 return EFI_NOT_FOUND;\r
2278 }\r
2279\r
2280 if (SearchDeviceInfoList(Instance, Port, PortMultiplier, EfiIdeCdrom) == NULL) {\r
2281 return EFI_NOT_FOUND;\r
2282 }\r
58727f29 2283\r
a41b5272 2284 if (Instance->Mode == EfiAtaIdeMode) {\r
2285 DevicePathNode = AllocateCopyPool (sizeof (ATAPI_DEVICE_PATH), &mAtapiDevicePathTemplate);\r
2286 if (DevicePathNode == NULL) {\r
2287 return EFI_OUT_OF_RESOURCES;\r
2288 }\r
2289\r
2290 DevicePathNode->Atapi.PrimarySecondary = Port;\r
2291 DevicePathNode->Atapi.SlaveMaster = PortMultiplier;\r
2292 DevicePathNode->Atapi.Lun = (UINT16) Lun;\r
2293 } else {\r
2294 DevicePathNode = AllocateCopyPool (sizeof (SATA_DEVICE_PATH), &mSataDevicePathTemplate);\r
2295 if (DevicePathNode == NULL) {\r
2296 return EFI_OUT_OF_RESOURCES;\r
2297 }\r
2298\r
2299 DevicePathNode->Sata.HBAPortNumber = Port;\r
2300 DevicePathNode->Sata.PortMultiplierPortNumber = PortMultiplier;\r
2301 DevicePathNode->Sata.Lun = (UINT16) Lun;\r
2302 }\r
2303\r
2304 *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) DevicePathNode;\r
2305\r
2306 return EFI_SUCCESS;\r
2307}\r
2308\r
2309/**\r
2310 Used to translate a device path node to a Target ID and LUN.\r
2311\r
2312 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.\r
2313 @param DevicePath A pointer to a single device path node that describes the SCSI device\r
2314 on the SCSI channel.\r
2315 @param Target A pointer to the Target Array which represents the ID of a SCSI device\r
2316 on the SCSI channel.\r
2317 @param Lun A pointer to the LUN of a SCSI device on the SCSI channel.\r
2318\r
2319 @retval EFI_SUCCESS DevicePath was successfully translated to a Target ID and\r
2320 LUN, and they were returned in Target and Lun.\r
2321 @retval EFI_INVALID_PARAMETER DevicePath or Target or Lun is NULL.\r
2322 @retval EFI_NOT_FOUND A valid translation from DevicePath to a Target ID and LUN\r
2323 does not exist.\r
2324 @retval EFI_UNSUPPORTED This driver does not support the device path node type in\r
2325 DevicePath.\r
2326\r
2327**/\r
2328EFI_STATUS\r
2329EFIAPI\r
2330ExtScsiPassThruGetTargetLun (\r
2331 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,\r
2332 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
2333 OUT UINT8 **Target,\r
2334 OUT UINT64 *Lun\r
2335 )\r
2336{\r
2337 EFI_DEV_PATH *DevicePathNode;\r
2338 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;\r
2339 LIST_ENTRY *Node;\r
2340\r
2341 Instance = EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);\r
2342\r
2343 //\r
2344 // Validate parameters passed in.\r
2345 //\r
2346 if (DevicePath == NULL || Target == NULL || Lun == NULL) {\r
2347 return EFI_INVALID_PARAMETER;\r
2348 }\r
2349\r
2350 if (*Target == NULL) {\r
2351 return EFI_INVALID_PARAMETER;\r
2352 }\r
2353 //\r
2354 // Check whether the DevicePath belongs to SCSI_DEVICE_PATH\r
2355 //\r
2356 if ((DevicePath->Type != MESSAGING_DEVICE_PATH) ||\r
2357 ((DevicePath->SubType != MSG_ATAPI_DP) &&\r
2358 (DevicePath->SubType != MSG_SATA_DP)) ||\r
2359 ((DevicePathNodeLength(DevicePath) != sizeof(ATAPI_DEVICE_PATH)) &&\r
2360 (DevicePathNodeLength(DevicePath) != sizeof(SATA_DEVICE_PATH)))) {\r
2361 return EFI_UNSUPPORTED;\r
2362 }\r
2363\r
2364 SetMem (*Target, TARGET_MAX_BYTES, 0xFF);\r
2365\r
2366 DevicePathNode = (EFI_DEV_PATH *) DevicePath;\r
2367\r
2368 if (Instance->Mode == EfiAtaIdeMode) {\r
2369 (*Target)[0] = (UINT8) DevicePathNode->Atapi.PrimarySecondary;\r
2370 (*Target)[1] = (UINT8) DevicePathNode->Atapi.SlaveMaster;\r
2371 *Lun = (UINT8) DevicePathNode->Atapi.Lun;\r
2372 } else {\r
2373 (*Target)[0] = (UINT8) DevicePathNode->Sata.HBAPortNumber;\r
2374 (*Target)[1] = (UINT8) DevicePathNode->Sata.PortMultiplierPortNumber;\r
2375 *Lun = (UINT8) DevicePathNode->Sata.Lun;\r
2376 }\r
2377\r
2378 Node = SearchDeviceInfoList(Instance, (*Target)[0], (*Target)[1], EfiIdeCdrom);\r
2379\r
2380 if (Node == NULL) {\r
2381 return EFI_NOT_FOUND;\r
2382 }\r
2383\r
2384 if (*Lun != 0) {\r
2385 return EFI_NOT_FOUND;\r
2386 }\r
2387\r
2388 return EFI_SUCCESS;\r
2389}\r
2390\r
2391/**\r
2392 Resets a SCSI channel. This operation resets all the SCSI devices connected to the SCSI channel.\r
2393\r
2394 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.\r
2395\r
2396 @retval EFI_SUCCESS The SCSI channel was reset.\r
2397 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI channel.\r
2398 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI channel.\r
2399 @retval EFI_UNSUPPORTED The SCSI channel does not support a channel reset operation.\r
2400\r
2401**/\r
2402EFI_STATUS\r
2403EFIAPI\r
2404ExtScsiPassThruResetChannel (\r
2405 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This\r
2406 )\r
2407{\r
71fd9fae
TF
2408 //\r
2409 // Return success directly then upper layer driver could think reset channel operation is done.\r
2410 //\r
2411 return EFI_SUCCESS;\r
490b5ea1 2412}\r
2413\r
a41b5272 2414/**\r
2415 Resets a SCSI logical unit that is connected to a SCSI channel.\r
2416\r
2417 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.\r
2418 @param Target The Target is an array of size TARGET_MAX_BYTE and it represents the\r
2419 target port ID of the SCSI device containing the SCSI logical unit to\r
2420 reset. Transport drivers may chose to utilize a subset of this array to suit\r
2421 the representation of their targets.\r
2422 @param Lun The LUN of the SCSI device to reset.\r
2423\r
2424 @retval EFI_SUCCESS The SCSI device specified by Target and Lun was reset.\r
2425 @retval EFI_INVALID_PARAMETER Target or Lun is NULL.\r
2426 @retval EFI_TIMEOUT A timeout occurred while attempting to reset the SCSI device\r
2427 specified by Target and Lun.\r
2428 @retval EFI_UNSUPPORTED The SCSI channel does not support a target reset operation.\r
2429 @retval EFI_DEVICE_ERROR A device error occurred while attempting to reset the SCSI device\r
2430 specified by Target and Lun.\r
2431\r
2432**/\r
2433EFI_STATUS\r
2434EFIAPI\r
2435ExtScsiPassThruResetTargetLun (\r
2436 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,\r
2437 IN UINT8 *Target,\r
2438 IN UINT64 Lun\r
2439 )\r
2440{\r
59b1b9d2
TF
2441 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;\r
2442 LIST_ENTRY *Node;\r
2443 UINT8 Port;\r
2444 UINT8 PortMultiplier;\r
2445\r
2446 Instance = EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);\r
2447 //\r
2448 // For ATAPI device, doesn't support multiple LUN device.\r
2449 //\r
2450 if (Lun != 0) {\r
2451 return EFI_INVALID_PARAMETER;\r
2452 }\r
2453 //\r
2454 // The layout of Target array:\r
2455 // ________________________________________________________________________\r
2456 // | Byte 0 | Byte 1 | ... | TARGET_MAX_BYTES - 1 |\r
2457 // |_____________________|_____________________|_____|______________________|\r
2458 // | | The port multiplier | | |\r
2459 // | The port number | port number | N/A | N/A |\r
2460 // |_____________________|_____________________|_____|______________________|\r
2461 //\r
2462 // For ATAPI device, 2 bytes is enough to represent the location of SCSI device.\r
2463 //\r
2464 Port = Target[0];\r
2465 PortMultiplier = Target[1];\r
2466\r
2467 Node = SearchDeviceInfoList(Instance, Port, PortMultiplier, EfiIdeCdrom);\r
2468 if (Node == NULL) {\r
2469 return EFI_INVALID_PARAMETER;\r
2470 }\r
2471\r
71fd9fae
TF
2472 //\r
2473 // Return success directly then upper layer driver could think reset target LUN operation is done.\r
2474 //\r
2475 return EFI_SUCCESS;\r
490b5ea1 2476}\r
a41b5272 2477\r
2478/**\r
58727f29 2479 Used to retrieve the list of legal Target IDs for SCSI devices on a SCSI channel. These can either\r
a41b5272 2480 be the list SCSI devices that are actually present on the SCSI channel, or the list of legal Target IDs\r
58727f29 2481 for the SCSI channel. Regardless, the caller of this function must probe the Target ID returned to\r
2482 see if a SCSI device is actually present at that location on the SCSI channel.\r
a41b5272 2483\r
2484 @param This A pointer to the EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.\r
2485 @param Target (TARGET_MAX_BYTES) of a SCSI device present on the SCSI channel.\r
2486 On output, a pointer to the Target ID (an array of\r
2487 TARGET_MAX_BYTES) of the next SCSI device present on a SCSI\r
2488 channel. An input value of 0xF(all bytes in the array are 0xF) in the\r
2489 Target array retrieves the Target ID of the first SCSI device present on a\r
2490 SCSI channel.\r
2491\r
2492 @retval EFI_SUCCESS The Target ID of the next SCSI device on the SCSI\r
2493 channel was returned in Target.\r
2494 @retval EFI_INVALID_PARAMETER Target or Lun is NULL.\r
2495 @retval EFI_TIMEOUT Target array is not all 0xF, and Target was not\r
2496 returned on a previous call to GetNextTarget().\r
2497 @retval EFI_NOT_FOUND There are no more SCSI devices on this SCSI channel.\r
2498\r
2499**/\r
2500EFI_STATUS\r
2501EFIAPI\r
2502ExtScsiPassThruGetNextTarget (\r
2503 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,\r
2504 IN OUT UINT8 **Target\r
2505 )\r
2506{\r
2507 ATA_ATAPI_PASS_THRU_INSTANCE *Instance;\r
2508 LIST_ENTRY *Node;\r
2509 EFI_ATA_DEVICE_INFO *DeviceInfo;\r
2510 UINT8 *Target8;\r
2511 UINT16 *Target16;\r
2512\r
2513 Instance = EXT_SCSI_PASS_THRU_PRIVATE_DATA_FROM_THIS (This);\r
2514\r
2515 if (Target == NULL || *Target == NULL) {\r
2516 return EFI_INVALID_PARAMETER;\r
2517 }\r
2518\r
2519 Target8 = *Target;\r
2520 Target16 = (UINT16 *)*Target;\r
2521\r
2522 if (CompareMem(Target8, mScsiId, TARGET_MAX_BYTES) != 0) {\r
2523 //\r
2524 // For ATAPI device, we use 2 least significant bytes to represent the location of SCSI device.\r
2525 // So the higher bytes in Target array should be 0xFF.\r
2526 //\r
2527 if (CompareMem (&Target8[2], &mScsiId[2], TARGET_MAX_BYTES - 2) != 0) {\r
2528 return EFI_INVALID_PARAMETER;\r
2529 }\r
2530\r
2531 //\r
2532 // When Target is not all 0xFF's, compare 2 least significant bytes with\r
2533 // previous target id to see if it is returned by previous call.\r
2534 //\r
2535 if (*Target16 != Instance->PreviousTargetId) {\r
2536 return EFI_INVALID_PARAMETER;\r
2537 }\r
2538\r
2539 //\r
2540 // Traverse the whole device list to find the next cdrom closed to\r
2541 // the device signified by Target[0] and Target[1].\r
2542 //\r
2543 // Note that we here use a tricky way to find the next cdrom :\r
2544 // All ata devices are detected and inserted into the device list\r
2545 // sequentially.\r
2546 //\r
2547 Node = GetFirstNode (&Instance->DeviceList);\r
2548 while (!IsNull (&Instance->DeviceList, Node)) {\r
2549 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);\r
2550\r
2551 if ((DeviceInfo->Type == EfiIdeCdrom) &&\r
58727f29 2552 ((Target8[0] < DeviceInfo->Port) ||\r
d8982b03 2553 ((Target8[0] == DeviceInfo->Port) &&\r
2554 (Target8[1] < DeviceInfo->PortMultiplier)))) {\r
a41b5272 2555 Target8[0] = (UINT8)DeviceInfo->Port;\r
2556 Target8[1] = (UINT8)DeviceInfo->PortMultiplier;\r
2557 goto Exit;\r
2558 }\r
2559\r
2560 Node = GetNextNode (&Instance->DeviceList, Node);\r
2561 }\r
2562\r
2563 return EFI_NOT_FOUND;\r
2564 } else {\r
2565 //\r
2566 // If the array is all 0xFF's, start to traverse the device list from the beginning\r
2567 //\r
2568 Node = GetFirstNode (&Instance->DeviceList);\r
2569\r
2570 while (!IsNull (&Instance->DeviceList, Node)) {\r
2571 DeviceInfo = ATA_ATAPI_DEVICE_INFO_FROM_THIS (Node);\r
2572\r
2573 if (DeviceInfo->Type == EfiIdeCdrom) {\r
2574 Target8[0] = (UINT8)DeviceInfo->Port;\r
2575 Target8[1] = (UINT8)DeviceInfo->PortMultiplier;\r
2576 goto Exit;\r
2577 }\r
2578\r
2579 Node = GetNextNode (&Instance->DeviceList, Node);\r
2580 }\r
2581\r
2582 return EFI_NOT_FOUND;\r
2583 }\r
2584\r
2585Exit:\r
2586 //\r
2587 // Update the PreviousTargetId.\r
2588 //\r
2589 Instance->PreviousTargetId = *Target16;\r
2590\r
2591 return EFI_SUCCESS;\r
490b5ea1 2592}\r
a41b5272 2593\r