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