Fixed potential issues to release resources when error occurs.
[mirror_edk2.git] / OptionRomPkg / AtapiPassThruDxe / AtapiPassThru.c
CommitLineData
823f7d4f 1/** @file\r
2 Copyright (c) 2006, Intel Corporation\r
3 All rights reserved. This program and the accompanying materials\r
4 are licensed and made available under the terms and conditions of the BSD License\r
5 which accompanies this distribution. The full text of the license may be found at\r
6 http://opensource.org/licenses/bsd-license.php\r
7\r
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
10\r
11**/\r
12\r
13#include "AtapiPassThru.h"\r
14\r
15\r
16static SCSI_COMMAND_SET gEndTable = { 0xff, (DATA_DIRECTION) 0xff };\r
17\r
18///\r
19/// This table contains all the supported ATAPI commands.\r
20///\r
21static SCSI_COMMAND_SET gSupportedATAPICommands[] = {\r
22 { OP_INQUIRY, DataIn },\r
23 { OP_LOAD_UNLOAD_CD, NoData },\r
24 { OP_MECHANISM_STATUS, DataIn },\r
25 { OP_MODE_SELECT_10, DataOut },\r
26 { OP_MODE_SENSE_10, DataIn },\r
27 { OP_PAUSE_RESUME, NoData },\r
28 { OP_PLAY_AUDIO_10, DataIn },\r
29 { OP_PLAY_AUDIO_MSF, DataIn },\r
30 { OP_PLAY_CD, DataIn },\r
31 { OP_PLAY_CD_MSF, DataIn },\r
32 { OP_PREVENT_ALLOW_MEDIUM_REMOVAL,NoData },\r
33 { OP_READ_10, DataIn },\r
34 { OP_READ_12, DataIn },\r
35 { OP_READ_CAPACITY, DataIn },\r
36 { OP_READ_CD, DataIn },\r
37 { OP_READ_CD_MSF, DataIn },\r
38 { OP_READ_HEADER, DataIn },\r
39 { OP_READ_SUB_CHANNEL, DataIn },\r
40 { OP_READ_TOC, DataIn },\r
41 { OP_REQUEST_SENSE, DataIn },\r
42 { OP_SCAN, NoData },\r
43 { OP_SEEK_10, NoData },\r
44 { OP_SET_CD_SPEED, DataOut },\r
45 { OP_STOPPLAY_SCAN, NoData },\r
46 { OP_START_STOP_UNIT, NoData },\r
47 { OP_TEST_UNIT_READY, NoData },\r
48 { OP_FORMAT_UNIT, DataOut },\r
49 { OP_READ_FORMAT_CAPACITIES, DataIn },\r
50 { OP_VERIFY, DataOut },\r
51 { OP_WRITE_10, DataOut },\r
52 { OP_WRITE_12, DataOut },\r
53 { OP_WRITE_AND_VERIFY, DataOut },\r
54 { 0xff, (DATA_DIRECTION) 0xff }\r
55};\r
56\r
57GLOBAL_REMOVE_IF_UNREFERENCED EFI_SCSI_PASS_THRU_MODE gScsiPassThruMode = {\r
58 L"ATAPI Controller",\r
59 L"ATAPI Channel",\r
60 4,\r
61 EFI_SCSI_PASS_THRU_ATTRIBUTES_PHYSICAL | EFI_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL,\r
62 0\r
63};\r
64\r
65GLOBAL_REMOVE_IF_UNREFERENCED EFI_SCSI_PASS_THRU_PROTOCOL gScsiPassThruProtocolTemplate = {\r
66 &gScsiPassThruMode,\r
67 AtapiScsiPassThruFunction,\r
68 AtapiScsiPassThruGetNextDevice,\r
69 AtapiScsiPassThruBuildDevicePath,\r
70 AtapiScsiPassThruGetTargetLun,\r
71 AtapiScsiPassThruResetChannel,\r
72 AtapiScsiPassThruResetTarget\r
73};\r
74\r
75GLOBAL_REMOVE_IF_UNREFERENCED EFI_EXT_SCSI_PASS_THRU_MODE gExtScsiPassThruMode = {\r
6a6d955c 76 4,\r
823f7d4f 77 EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_PHYSICAL | EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL,\r
78 0\r
79};\r
80\r
81GLOBAL_REMOVE_IF_UNREFERENCED EFI_EXT_SCSI_PASS_THRU_PROTOCOL gExtScsiPassThruProtocolTemplate = {\r
82 &gExtScsiPassThruMode,\r
83 AtapiExtScsiPassThruFunction,\r
84 AtapiExtScsiPassThruGetNextTargetLun,\r
85 AtapiExtScsiPassThruBuildDevicePath,\r
86 AtapiExtScsiPassThruGetTargetLun,\r
87 AtapiExtScsiPassThruResetChannel,\r
88 AtapiExtScsiPassThruResetTarget,\r
89 AtapiExtScsiPassThruGetNextTarget\r
90};\r
91\r
92EFI_DRIVER_BINDING_PROTOCOL gAtapiScsiPassThruDriverBinding = {\r
93 AtapiScsiPassThruDriverBindingSupported,\r
94 AtapiScsiPassThruDriverBindingStart,\r
95 AtapiScsiPassThruDriverBindingStop,\r
96 0x10,\r
97 NULL,\r
98 NULL\r
99};\r
100\r
101EFI_STATUS\r
102EFIAPI\r
103AtapiScsiPassThruDriverBindingSupported (\r
104 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
105 IN EFI_HANDLE Controller,\r
106 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
107 )\r
108/*++\r
6a6d955c 109\r
823f7d4f 110Routine Description:\r
111 Test to see if this driver supports ControllerHandle. Any ControllerHandle\r
112 that has gEfiPciIoProtocolGuid installed and is IDE Controller it will be supported.\r
6a6d955c 113\r
823f7d4f 114Arguments:\r
115\r
116 This - Protocol instance pointer.\r
117 Controller - Handle of device to test\r
118 RemainingDevicePath - Not used\r
6a6d955c 119\r
823f7d4f 120Returns:\r
121 EFI_STATUS\r
6a6d955c 122\r
823f7d4f 123--*/\r
124{\r
125 EFI_STATUS Status;\r
126 EFI_PCI_IO_PROTOCOL *PciIo;\r
127 PCI_TYPE00 Pci;\r
128\r
129\r
130 //\r
131 // Open the IO Abstraction(s) needed to perform the supported test\r
132 //\r
133 Status = gBS->OpenProtocol (\r
134 Controller,\r
135 &gEfiPciIoProtocolGuid,\r
136 (VOID **) &PciIo,\r
137 This->DriverBindingHandle,\r
138 Controller,\r
139 EFI_OPEN_PROTOCOL_BY_DRIVER\r
140 );\r
141 if (EFI_ERROR (Status)) {\r
142 return Status;\r
143 }\r
144 //\r
145 // Use the PCI I/O Protocol to see if Controller is a IDE Controller that\r
146 // can be managed by this driver. Read the PCI Configuration Header\r
147 // for this device.\r
148 //\r
149 Status = PciIo->Pci.Read (\r
150 PciIo,\r
151 EfiPciIoWidthUint32,\r
152 0,\r
153 sizeof (Pci) / sizeof (UINT32),\r
154 &Pci\r
155 );\r
156 if (EFI_ERROR (Status)) {\r
157 gBS->CloseProtocol (\r
158 Controller,\r
159 &gEfiPciIoProtocolGuid,\r
160 This->DriverBindingHandle,\r
161 Controller\r
162 );\r
163 return EFI_UNSUPPORTED;\r
164 }\r
165\r
166 if (Pci.Hdr.ClassCode[2] != PCI_CLASS_MASS_STORAGE || Pci.Hdr.ClassCode[1] != PCI_CLASS_IDE) {\r
167\r
168 Status = EFI_UNSUPPORTED;\r
169 }\r
170\r
171 gBS->CloseProtocol (\r
172 Controller,\r
173 &gEfiPciIoProtocolGuid,\r
174 This->DriverBindingHandle,\r
175 Controller\r
176 );\r
177\r
178 return Status;\r
179}\r
180\r
181EFI_STATUS\r
182EFIAPI\r
183AtapiScsiPassThruDriverBindingStart (\r
184 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
185 IN EFI_HANDLE Controller,\r
186 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath\r
187 )\r
188/*++\r
6a6d955c 189\r
823f7d4f 190Routine Description:\r
191 Create handles for IDE channels specified by RemainingDevicePath.\r
192 Install SCSI Pass Thru Protocol onto each created handle.\r
6a6d955c 193\r
823f7d4f 194Arguments:\r
195\r
196 This - Protocol instance pointer.\r
197 Controller - Handle of device to test\r
198 RemainingDevicePath - Not used\r
6a6d955c 199\r
823f7d4f 200Returns:\r
201 EFI_STATUS\r
6a6d955c 202\r
823f7d4f 203--*/\r
204{\r
205 EFI_STATUS Status;\r
206 EFI_PCI_IO_PROTOCOL *PciIo;\r
207 UINT64 Supports;\r
208 UINT64 OriginalPciAttributes;\r
6a6d955c 209 BOOLEAN PciAttributesSaved;\r
823f7d4f 210\r
211 PciIo = NULL;\r
212 Status = gBS->OpenProtocol (\r
213 Controller,\r
214 &gEfiPciIoProtocolGuid,\r
215 (VOID **) &PciIo,\r
216 This->DriverBindingHandle,\r
217 Controller,\r
218 EFI_OPEN_PROTOCOL_BY_DRIVER\r
219 );\r
220 if (EFI_ERROR (Status)) {\r
221 return Status;\r
222 }\r
223\r
6a6d955c 224 PciAttributesSaved = FALSE;\r
823f7d4f 225 //\r
226 // Save original PCI attributes\r
227 //\r
228 Status = PciIo->Attributes (\r
229 PciIo,\r
230 EfiPciIoAttributeOperationGet,\r
231 0,\r
232 &OriginalPciAttributes\r
233 );\r
234\r
235 if (EFI_ERROR (Status)) {\r
6a6d955c 236 goto Done;\r
823f7d4f 237 }\r
6a6d955c 238 PciAttributesSaved = TRUE;\r
823f7d4f 239\r
240 Status = PciIo->Attributes (\r
241 PciIo,\r
242 EfiPciIoAttributeOperationSupported,\r
243 0,\r
244 &Supports\r
245 );\r
246 if (!EFI_ERROR (Status)) {\r
247 Supports &= (EFI_PCI_DEVICE_ENABLE |\r
248 EFI_PCI_IO_ATTRIBUTE_IDE_PRIMARY_IO |\r
249 EFI_PCI_IO_ATTRIBUTE_IDE_SECONDARY_IO);\r
250 Status = PciIo->Attributes (\r
251 PciIo,\r
252 EfiPciIoAttributeOperationEnable,\r
253 Supports,\r
254 NULL\r
255 );\r
256 }\r
257 if (EFI_ERROR (Status)) {\r
258 goto Done;\r
259 }\r
260\r
261 //\r
262 // Create SCSI Pass Thru instance for the IDE channel.\r
263 //\r
264 Status = RegisterAtapiScsiPassThru (This, Controller, PciIo, OriginalPciAttributes);\r
265\r
266Done:\r
267 if (EFI_ERROR (Status)) {\r
6a6d955c 268 if (PciAttributesSaved == TRUE) {\r
269 //\r
270 // Restore original PCI attributes\r
271 //\r
272 PciIo->Attributes (\r
273 PciIo,\r
274 EfiPciIoAttributeOperationSet,\r
275 OriginalPciAttributes,\r
276 NULL\r
277 );\r
278 }\r
823f7d4f 279\r
280 gBS->CloseProtocol (\r
281 Controller,\r
282 &gEfiPciIoProtocolGuid,\r
283 This->DriverBindingHandle,\r
284 Controller\r
285 );\r
286 }\r
287\r
288 return Status;\r
289}\r
290\r
291EFI_STATUS\r
292EFIAPI\r
293AtapiScsiPassThruDriverBindingStop (\r
294 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
295 IN EFI_HANDLE Controller,\r
296 IN UINTN NumberOfChildren,\r
297 IN EFI_HANDLE *ChildHandleBuffer\r
298 )\r
299/*++\r
6a6d955c 300\r
823f7d4f 301Routine Description:\r
302\r
303 Stop this driver on ControllerHandle. Support stoping any child handles\r
304 created by this driver.\r
305\r
306Arguments:\r
307\r
308 This - Protocol instance pointer.\r
309 Controller - Handle of device to stop driver on\r
310 NumberOfChildren - Number of Children in the ChildHandleBuffer\r
311 ChildHandleBuffer - List of handles for the children we need to stop.\r
6a6d955c 312\r
823f7d4f 313Returns:\r
314\r
315 EFI_STATUS\r
6a6d955c 316\r
823f7d4f 317--*/\r
318{\r
319 EFI_STATUS Status;\r
320 EFI_SCSI_PASS_THRU_PROTOCOL *ScsiPassThru;\r
321 EFI_EXT_SCSI_PASS_THRU_PROTOCOL *ExtScsiPassThru;\r
322 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate;\r
323\r
324 if (FeaturePcdGet (PcdSupportScsiPassThru)) {\r
325 Status = gBS->OpenProtocol (\r
326 Controller,\r
327 &gEfiScsiPassThruProtocolGuid,\r
328 (VOID **) &ScsiPassThru,\r
329 This->DriverBindingHandle,\r
330 Controller,\r
331 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
332 );\r
333 if (EFI_ERROR (Status)) {\r
334 return Status;\r
335 }\r
336 AtapiScsiPrivate = ATAPI_SCSI_PASS_THRU_DEV_FROM_THIS (ScsiPassThru);\r
337 if (FeaturePcdGet (PcdSupportExtScsiPassThru)) {\r
338 Status = gBS->UninstallMultipleProtocolInterfaces (\r
339 Controller,\r
340 &gEfiScsiPassThruProtocolGuid,\r
341 &AtapiScsiPrivate->ScsiPassThru,\r
342 &gEfiExtScsiPassThruProtocolGuid,\r
343 &AtapiScsiPrivate->ExtScsiPassThru,\r
344 NULL\r
345 );\r
346 } else {\r
347 Status = gBS->UninstallMultipleProtocolInterfaces (\r
348 Controller,\r
349 &gEfiScsiPassThruProtocolGuid,\r
350 &AtapiScsiPrivate->ScsiPassThru,\r
351 NULL\r
352 );\r
353 }\r
354 } else {\r
355 Status = gBS->OpenProtocol (\r
356 Controller,\r
357 &gEfiExtScsiPassThruProtocolGuid,\r
358 (VOID **) &ExtScsiPassThru,\r
359 This->DriverBindingHandle,\r
360 Controller,\r
361 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
362 );\r
363 if (EFI_ERROR (Status)) {\r
364 return Status;\r
365 }\r
366 AtapiScsiPrivate = ATAPI_EXT_SCSI_PASS_THRU_DEV_FROM_THIS (ExtScsiPassThru);\r
367 Status = gBS->UninstallMultipleProtocolInterfaces (\r
368 Controller,\r
369 &gEfiExtScsiPassThruProtocolGuid,\r
370 &AtapiScsiPrivate->ExtScsiPassThru,\r
371 NULL\r
372 );\r
373 }\r
374 if (EFI_ERROR (Status)) {\r
375 return Status;\r
376 }\r
377\r
378 //\r
379 // Restore original PCI attributes\r
380 //\r
381 AtapiScsiPrivate->PciIo->Attributes (\r
382 AtapiScsiPrivate->PciIo,\r
383 EfiPciIoAttributeOperationSet,\r
384 AtapiScsiPrivate->OriginalPciAttributes,\r
385 NULL\r
386 );\r
387\r
388 gBS->CloseProtocol (\r
389 Controller,\r
390 &gEfiPciIoProtocolGuid,\r
391 This->DriverBindingHandle,\r
392 Controller\r
393 );\r
394\r
395 gBS->FreePool (AtapiScsiPrivate);\r
396\r
397 return EFI_SUCCESS;\r
398}\r
399\r
400EFI_STATUS\r
401RegisterAtapiScsiPassThru (\r
402 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
403 IN EFI_HANDLE Controller,\r
404 IN EFI_PCI_IO_PROTOCOL *PciIo,\r
405 IN UINT64 OriginalPciAttributes\r
406 )\r
407/*++\r
6a6d955c 408\r
823f7d4f 409Routine Description:\r
410 Attaches SCSI Pass Thru Protocol for specified IDE channel.\r
6a6d955c 411\r
823f7d4f 412Arguments:\r
413 This - Protocol instance pointer.\r
6a6d955c 414 Controller - Parent device handle to the IDE channel.\r
415 PciIo - PCI I/O protocol attached on the "Controller".\r
416\r
823f7d4f 417Returns:\r
418 Always return EFI_SUCCESS unless installing SCSI Pass Thru Protocol failed.\r
419\r
420--*/\r
421{\r
422 EFI_STATUS Status;\r
423 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate;\r
424 IDE_REGISTERS_BASE_ADDR IdeRegsBaseAddr[ATAPI_MAX_CHANNEL];\r
425\r
426 AtapiScsiPrivate = AllocateZeroPool (sizeof (ATAPI_SCSI_PASS_THRU_DEV));\r
427 if (AtapiScsiPrivate == NULL) {\r
428 return EFI_OUT_OF_RESOURCES;\r
429 }\r
430\r
431 AtapiScsiPrivate->Signature = ATAPI_SCSI_PASS_THRU_DEV_SIGNATURE;\r
432 AtapiScsiPrivate->Handle = Controller;\r
433\r
434 //\r
435 // will reset the IoPort inside each API function.\r
436 //\r
437 AtapiScsiPrivate->IoPort = NULL;\r
438 AtapiScsiPrivate->PciIo = PciIo;\r
439 AtapiScsiPrivate->OriginalPciAttributes = OriginalPciAttributes;\r
440\r
441 //\r
442 // Obtain IDE IO port registers' base addresses\r
443 //\r
444 Status = GetIdeRegistersBaseAddr (PciIo, IdeRegsBaseAddr);\r
445 if (EFI_ERROR (Status)) {\r
446 return Status;\r
447 }\r
448\r
449 InitAtapiIoPortRegisters(AtapiScsiPrivate, IdeRegsBaseAddr);\r
450\r
451 //\r
452 // Initialize the LatestTargetId to MAX_TARGET_ID.\r
453 //\r
454 AtapiScsiPrivate->LatestTargetId = MAX_TARGET_ID;\r
455 AtapiScsiPrivate->LatestLun = 0;\r
456\r
457 Status = InstallScsiPassThruProtocols (&Controller, AtapiScsiPrivate);\r
458\r
459 return Status;\r
460}\r
461\r
462EFI_STATUS\r
463EFIAPI\r
464AtapiScsiPassThruFunction (\r
465 IN EFI_SCSI_PASS_THRU_PROTOCOL *This,\r
466 IN UINT32 Target,\r
467 IN UINT64 Lun,\r
468 IN OUT EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet,\r
469 IN EFI_EVENT Event OPTIONAL\r
470 )\r
471/*++\r
472\r
473Routine Description:\r
474\r
475 Implements EFI_SCSI_PASS_THRU_PROTOCOL.PassThru() function.\r
476\r
477Arguments:\r
478\r
479 This: The EFI_SCSI_PASS_THRU_PROTOCOL instance.\r
6a6d955c 480 Target: The Target ID of the ATAPI device to send the SCSI\r
823f7d4f 481 Request Packet. To ATAPI devices attached on an IDE\r
482 Channel, Target ID 0 indicates Master device;Target\r
483 ID 1 indicates Slave device.\r
484 Lun: The LUN of the ATAPI device to send the SCSI Request\r
485 Packet. To the ATAPI device, Lun is always 0.\r
6a6d955c 486 Packet: The SCSI Request Packet to send to the ATAPI device\r
823f7d4f 487 specified by Target and Lun.\r
6a6d955c 488 Event: If non-blocking I/O is not supported then Event is ignored,\r
823f7d4f 489 and blocking I/O is performed.\r
490 If Event is NULL, then blocking I/O is performed.\r
6a6d955c 491 If Event is not NULL and non blocking I/O is supported,\r
492 then non-blocking I/O is performed, and Event will be signaled\r
493 when the SCSI Request Packet completes.\r
823f7d4f 494\r
6a6d955c 495Returns:\r
823f7d4f 496\r
497 EFI_STATUS\r
6a6d955c 498\r
823f7d4f 499--*/\r
500{\r
501 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate;\r
502 EFI_STATUS Status;\r
503\r
504 AtapiScsiPrivate = ATAPI_SCSI_PASS_THRU_DEV_FROM_THIS (This);\r
505\r
506 //\r
507 // Target is not allowed beyond MAX_TARGET_ID\r
508 //\r
509 if ((Target > MAX_TARGET_ID) || (Lun != 0)) {\r
510 return EFI_INVALID_PARAMETER;\r
511 }\r
6a6d955c 512\r
823f7d4f 513 //\r
514 // check the data fields in Packet parameter.\r
515 //\r
516 Status = CheckSCSIRequestPacket (Packet);\r
517 if (EFI_ERROR (Status)) {\r
518 return Status;\r
519 }\r
520\r
521 //\r
522 // If Request Packet targets at the IDE channel itself,\r
523 // do nothing.\r
524 //\r
525 if (Target == This->Mode->AdapterId) {\r
526 Packet->TransferLength = 0;\r
527 return EFI_SUCCESS;\r
528 }\r
529\r
530 //\r
531 // According to Target ID, reset the Atapi I/O Register mapping\r
532 // (Target Id in [0,1] area, using AtapiIoPortRegisters[0],\r
533 // Target Id in [2,3] area, using AtapiIoPortRegisters[1]\r
534 //\r
535 if ((Target / 2) == 0) {\r
536 Target = Target % 2;\r
537 AtapiScsiPrivate->IoPort = &AtapiScsiPrivate->AtapiIoPortRegisters[0];\r
538 } else {\r
539 Target = Target % 2;\r
540 AtapiScsiPrivate->IoPort = &AtapiScsiPrivate->AtapiIoPortRegisters[1];\r
541 }\r
542\r
543 //\r
544 // the ATAPI SCSI interface does not support non-blocking I/O\r
545 // ignore the Event parameter\r
546 //\r
547 // Performs blocking I/O.\r
548 //\r
549 Status = SubmitBlockingIoCommand (AtapiScsiPrivate, Target, Packet);\r
550 return Status;\r
551}\r
552\r
553EFI_STATUS\r
554EFIAPI\r
555AtapiScsiPassThruGetNextDevice (\r
556 IN EFI_SCSI_PASS_THRU_PROTOCOL *This,\r
557 IN OUT UINT32 *Target,\r
558 IN OUT UINT64 *Lun\r
559 )\r
560/*++\r
561\r
562Routine Description:\r
563\r
6a6d955c 564 Used to retrieve the list of legal Target IDs for SCSI devices\r
823f7d4f 565 on a SCSI channel.\r
566\r
567Arguments:\r
568\r
569 This - Protocol instance pointer.\r
6a6d955c 570 Target - On input, a pointer to the Target ID of a SCSI\r
571 device present on the SCSI channel. On output,\r
823f7d4f 572 a pointer to the Target ID of the next SCSI device\r
6a6d955c 573 present on a SCSI channel. An input value of\r
574 0xFFFFFFFF retrieves the Target ID of the first\r
823f7d4f 575 SCSI device present on a SCSI channel.\r
576 Lun - On input, a pointer to the LUN of a SCSI device\r
577 present on the SCSI channel. On output, a pointer\r
6a6d955c 578 to the LUN of the next SCSI device present on\r
823f7d4f 579 a SCSI channel.\r
580Returns:\r
581\r
6a6d955c 582 EFI_SUCCESS - The Target ID and Lun of the next SCSI device\r
823f7d4f 583 on the SCSI channel was returned in Target and Lun.\r
584 EFI_NOT_FOUND - There are no more SCSI devices on this SCSI channel.\r
585 EFI_INVALID_PARAMETER - Target is not 0xFFFFFFFF,and Target and Lun were not\r
586 returned on a previous call to GetNextDevice().\r
587--*/\r
588{\r
589 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate;\r
590\r
591 //\r
592 // Retrieve Device Private Data Structure.\r
593 //\r
594 AtapiScsiPrivate = ATAPI_SCSI_PASS_THRU_DEV_FROM_THIS (This);\r
595\r
596 //\r
597 // Check whether Target is valid.\r
598 //\r
599 if (Target == NULL || Lun == NULL) {\r
600 return EFI_INVALID_PARAMETER;\r
601 }\r
602\r
603 if ((*Target != 0xFFFFFFFF) &&\r
604 ((*Target != AtapiScsiPrivate->LatestTargetId) ||\r
605 (*Lun != AtapiScsiPrivate->LatestLun))) {\r
606 return EFI_INVALID_PARAMETER;\r
607 }\r
608\r
609 if (*Target == MAX_TARGET_ID) {\r
610 return EFI_NOT_FOUND;\r
611 }\r
612\r
613 if (*Target == 0xFFFFFFFF) {\r
614 *Target = 0;\r
615 } else {\r
616 *Target = AtapiScsiPrivate->LatestTargetId + 1;\r
617 }\r
618\r
619 *Lun = 0;\r
620\r
621 //\r
622 // Update the LatestTargetId.\r
623 //\r
624 AtapiScsiPrivate->LatestTargetId = *Target;\r
625 AtapiScsiPrivate->LatestLun = *Lun;\r
626\r
627 return EFI_SUCCESS;\r
628\r
629}\r
630\r
631EFI_STATUS\r
632EFIAPI\r
633AtapiScsiPassThruBuildDevicePath (\r
634 IN EFI_SCSI_PASS_THRU_PROTOCOL *This,\r
635 IN UINT32 Target,\r
636 IN UINT64 Lun,\r
637 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath\r
638 )\r
639/*++\r
640\r
641Routine Description:\r
642\r
6a6d955c 643 Used to allocate and build a device path node for a SCSI device\r
823f7d4f 644 on a SCSI channel. Would not build device path for a SCSI Host Controller.\r
645\r
646Arguments:\r
647\r
648 This - Protocol instance pointer.\r
649 Target - The Target ID of the SCSI device for which\r
650 a device path node is to be allocated and built.\r
6a6d955c 651 Lun - The LUN of the SCSI device for which a device\r
823f7d4f 652 path node is to be allocated and built.\r
6a6d955c 653 DevicePath - A pointer to a single device path node that\r
654 describes the SCSI device specified by\r
655 Target and Lun. This function is responsible\r
823f7d4f 656 for allocating the buffer DevicePath with the boot\r
6a6d955c 657 service AllocatePool(). It is the caller's\r
823f7d4f 658 responsibility to free DevicePath when the caller\r
6a6d955c 659 is finished with DevicePath.\r
823f7d4f 660 Returns:\r
661 EFI_SUCCESS - The device path node that describes the SCSI device\r
6a6d955c 662 specified by Target and Lun was allocated and\r
823f7d4f 663 returned in DevicePath.\r
664 EFI_NOT_FOUND - The SCSI devices specified by Target and Lun does\r
665 not exist on the SCSI channel.\r
666 EFI_INVALID_PARAMETER - DevicePath is NULL.\r
6a6d955c 667 EFI_OUT_OF_RESOURCES - There are not enough resources to allocate\r
823f7d4f 668 DevicePath.\r
669--*/\r
670{\r
671 EFI_DEV_PATH *Node;\r
672\r
673\r
674 //\r
675 // Validate parameters passed in.\r
676 //\r
677\r
678 if (DevicePath == NULL) {\r
679 return EFI_INVALID_PARAMETER;\r
680 }\r
681\r
682 //\r
683 // can not build device path for the SCSI Host Controller.\r
684 //\r
685 if ((Target > (MAX_TARGET_ID - 1)) || (Lun != 0)) {\r
686 return EFI_NOT_FOUND;\r
687 }\r
688\r
689 Node = AllocateZeroPool (sizeof (EFI_DEV_PATH));\r
690 if (Node == NULL) {\r
691 return EFI_OUT_OF_RESOURCES;\r
692 }\r
693\r
694 Node->DevPath.Type = MESSAGING_DEVICE_PATH;\r
695 Node->DevPath.SubType = MSG_ATAPI_DP;\r
696 SetDevicePathNodeLength (&Node->DevPath, sizeof (ATAPI_DEVICE_PATH));\r
697\r
698 Node->Atapi.PrimarySecondary = (UINT8) (Target / 2);\r
699 Node->Atapi.SlaveMaster = (UINT8) (Target % 2);\r
700 Node->Atapi.Lun = (UINT16) Lun;\r
701\r
702 *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) Node;\r
703\r
704 return EFI_SUCCESS;\r
705}\r
706\r
707EFI_STATUS\r
708EFIAPI\r
709AtapiScsiPassThruGetTargetLun (\r
710 IN EFI_SCSI_PASS_THRU_PROTOCOL *This,\r
711 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
712 OUT UINT32 *Target,\r
713 OUT UINT64 *Lun\r
714 )\r
715/*++\r
716\r
717Routine Description:\r
718\r
719 Used to translate a device path node to a Target ID and LUN.\r
720\r
721Arguments:\r
722\r
723 This - Protocol instance pointer.\r
6a6d955c 724 DevicePath - A pointer to the device path node that\r
823f7d4f 725 describes a SCSI device on the SCSI channel.\r
6a6d955c 726 Target - A pointer to the Target ID of a SCSI device\r
727 on the SCSI channel.\r
728 Lun - A pointer to the LUN of a SCSI device on\r
729 the SCSI channel.\r
823f7d4f 730Returns:\r
731\r
6a6d955c 732 EFI_SUCCESS - DevicePath was successfully translated to a\r
733 Target ID and LUN, and they were returned\r
823f7d4f 734 in Target and Lun.\r
735 EFI_INVALID_PARAMETER - DevicePath/Target/Lun is NULL.\r
6a6d955c 736 EFI_UNSUPPORTED - This driver does not support the device path\r
823f7d4f 737 node type in DevicePath.\r
6a6d955c 738 EFI_NOT_FOUND - A valid translation from DevicePath to a\r
823f7d4f 739 Target ID and LUN does not exist.\r
740--*/\r
741{\r
742 EFI_DEV_PATH *Node;\r
743\r
744 //\r
745 // Validate parameters passed in.\r
746 //\r
747 if (DevicePath == NULL || Target == NULL || Lun == NULL) {\r
748 return EFI_INVALID_PARAMETER;\r
749 }\r
750\r
751 //\r
752 // Check whether the DevicePath belongs to SCSI_DEVICE_PATH\r
753 //\r
754 if ((DevicePath->Type != MESSAGING_DEVICE_PATH) ||\r
755 (DevicePath->SubType != MSG_ATAPI_DP) ||\r
756 (DevicePathNodeLength(DevicePath) != sizeof(ATAPI_DEVICE_PATH))) {\r
757 return EFI_UNSUPPORTED;\r
758 }\r
759\r
760 Node = (EFI_DEV_PATH *) DevicePath;\r
761\r
762 *Target = Node->Atapi.PrimarySecondary * 2 + Node->Atapi.SlaveMaster;\r
763 *Lun = Node->Atapi.Lun;\r
764\r
765 if (*Target > (MAX_TARGET_ID - 1) || *Lun != 0) {\r
766 return EFI_NOT_FOUND;\r
767 }\r
768\r
769 return EFI_SUCCESS;\r
770}\r
771\r
772EFI_STATUS\r
773EFIAPI\r
774AtapiScsiPassThruResetChannel (\r
775 IN EFI_SCSI_PASS_THRU_PROTOCOL *This\r
776 )\r
777/*++\r
778\r
779Routine Description:\r
780\r
6a6d955c 781 Resets a SCSI channel.This operation resets all the\r
823f7d4f 782 SCSI devices connected to the SCSI channel.\r
783\r
784Arguments:\r
785\r
786 This - Protocol instance pointer.\r
787\r
788Returns:\r
789\r
790 EFI_SUCCESS - The SCSI channel was reset.\r
6a6d955c 791 EFI_UNSUPPORTED - The SCSI channel does not support\r
823f7d4f 792 a channel reset operation.\r
6a6d955c 793 EFI_DEVICE_ERROR - A device error occurred while\r
823f7d4f 794 attempting to reset the SCSI channel.\r
6a6d955c 795 EFI_TIMEOUT - A timeout occurred while attempting\r
823f7d4f 796 to reset the SCSI channel.\r
797--*/\r
798{\r
799 UINT8 DeviceControlValue;\r
800 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate;\r
801 UINT8 Index;\r
802 BOOLEAN ResetFlag;\r
803\r
804 AtapiScsiPrivate = ATAPI_SCSI_PASS_THRU_DEV_FROM_THIS (This);\r
805 ResetFlag = FALSE;\r
806\r
807 //\r
808 // Reset both Primary channel and Secondary channel.\r
809 // so, the IoPort pointer must point to the right I/O Register group\r
810 //\r
811 for (Index = 0; Index < 2; Index++) {\r
812 //\r
813 // Reset\r
814 //\r
815 AtapiScsiPrivate->IoPort = &AtapiScsiPrivate->AtapiIoPortRegisters[Index];\r
816\r
817 DeviceControlValue = 0;\r
818 //\r
819 // set SRST bit to initiate soft reset\r
820 //\r
821 DeviceControlValue |= SRST;\r
822 //\r
823 // disable Interrupt\r
824 //\r
825 DeviceControlValue |= bit (1);\r
826 WritePortB (\r
827 AtapiScsiPrivate->PciIo,\r
828 AtapiScsiPrivate->IoPort->Alt.DeviceControl,\r
829 DeviceControlValue\r
830 );\r
831\r
832 //\r
833 // Wait 10us\r
834 //\r
835 gBS->Stall (10);\r
836\r
837 //\r
838 // Clear SRST bit\r
839 // 0xfb:1111,1011\r
840 //\r
841 DeviceControlValue &= 0xfb;\r
842\r
843 WritePortB (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->Alt.DeviceControl, DeviceControlValue);\r
844\r
845 //\r
846 // slave device needs at most 31s to clear BSY\r
847 //\r
848 if (StatusWaitForBSYClear (AtapiScsiPrivate, 31000000) != EFI_TIMEOUT) {\r
849 ResetFlag = TRUE;\r
850 }\r
851 }\r
852\r
853 if (ResetFlag) {\r
854 return EFI_SUCCESS;\r
855 }\r
856\r
857 return EFI_TIMEOUT;\r
858}\r
859\r
860EFI_STATUS\r
861EFIAPI\r
862AtapiScsiPassThruResetTarget (\r
863 IN EFI_SCSI_PASS_THRU_PROTOCOL *This,\r
864 IN UINT32 Target,\r
865 IN UINT64 Lun\r
866 )\r
867/*++\r
868\r
869Routine Description:\r
870\r
871 Resets a SCSI device that is connected to a SCSI channel.\r
872\r
873Arguments:\r
874\r
875 This - Protocol instance pointer.\r
6a6d955c 876 Target - The Target ID of the SCSI device to reset.\r
823f7d4f 877 Lun - The LUN of the SCSI device to reset.\r
6a6d955c 878\r
823f7d4f 879Returns:\r
880\r
6a6d955c 881 EFI_SUCCESS - The SCSI device specified by Target and\r
823f7d4f 882 Lun was reset.\r
883 EFI_UNSUPPORTED - The SCSI channel does not support a target\r
884 reset operation.\r
885 EFI_INVALID_PARAMETER - Target or Lun are invalid.\r
6a6d955c 886 EFI_DEVICE_ERROR - A device error occurred while attempting\r
887 to reset the SCSI device specified by Target\r
823f7d4f 888 and Lun.\r
6a6d955c 889 EFI_TIMEOUT - A timeout occurred while attempting to reset\r
823f7d4f 890 the SCSI device specified by Target and Lun.\r
891--*/\r
892{\r
893 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate;\r
894 UINT8 Command;\r
895 UINT8 DeviceSelect;\r
896\r
897 AtapiScsiPrivate = ATAPI_SCSI_PASS_THRU_DEV_FROM_THIS (This);\r
898\r
899 if ((Target > MAX_TARGET_ID) || (Lun != 0)) {\r
900 return EFI_INVALID_PARAMETER;\r
901 }\r
902 //\r
903 // Directly return EFI_SUCCESS if want to reset the host controller\r
904 //\r
905 if (Target == This->Mode->AdapterId) {\r
906 return EFI_SUCCESS;\r
907 }\r
908\r
909 //\r
910 // According to Target ID, reset the Atapi I/O Register mapping\r
911 // (Target Id in [0,1] area, using AtapiIoPortRegisters[0],\r
912 // Target Id in [2,3] area, using AtapiIoPortRegisters[1]\r
913 //\r
914 if ((Target / 2) == 0) {\r
915 AtapiScsiPrivate->IoPort = &AtapiScsiPrivate->AtapiIoPortRegisters[0];\r
916 } else {\r
917 AtapiScsiPrivate->IoPort = &AtapiScsiPrivate->AtapiIoPortRegisters[1];\r
918 }\r
919\r
920 //\r
921 // for ATAPI device, no need to wait DRDY ready after device selecting.\r
922 //\r
923 // bit7 and bit5 are both set to 1 for backward compatibility\r
924 //\r
925 DeviceSelect = (UINT8) (((bit (7) | bit (5)) | (Target << 4)));\r
926 WritePortB (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->Head, DeviceSelect);\r
927\r
928 Command = ATAPI_SOFT_RESET_CMD;\r
929 WritePortB (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->Reg.Command, Command);\r
930\r
931 //\r
932 // BSY clear is the only status return to the host by the device\r
933 // when reset is complete.\r
934 // slave device needs at most 31s to clear BSY\r
935 //\r
936 if (EFI_ERROR (StatusWaitForBSYClear (AtapiScsiPrivate, 31000000))) {\r
937 return EFI_TIMEOUT;\r
938 }\r
939\r
940 //\r
941 // stall 5 seconds to make the device status stable\r
942 //\r
943 gBS->Stall (5000000);\r
944\r
945 return EFI_SUCCESS;\r
946}\r
947\r
948EFI_STATUS\r
949EFIAPI\r
950AtapiExtScsiPassThruFunction (\r
951 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,\r
952 IN UINT8 *Target,\r
953 IN UINT64 Lun,\r
954 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet,\r
955 IN EFI_EVENT Event OPTIONAL\r
956 )\r
957/*++\r
958\r
959Routine Description:\r
960\r
961 Implements EFI_EXT_SCSI_PASS_THRU_PROTOCOL.PassThru() function.\r
962\r
963Arguments:\r
964\r
965 This: The EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.\r
6a6d955c 966 Target: The Target ID of the ATAPI device to send the SCSI\r
823f7d4f 967 Request Packet. To ATAPI devices attached on an IDE\r
968 Channel, Target ID 0 indicates Master device;Target\r
969 ID 1 indicates Slave device.\r
970 Lun: The LUN of the ATAPI device to send the SCSI Request\r
971 Packet. To the ATAPI device, Lun is always 0.\r
6a6d955c 972 Packet: The SCSI Request Packet to send to the ATAPI device\r
823f7d4f 973 specified by Target and Lun.\r
6a6d955c 974 Event: If non-blocking I/O is not supported then Event is ignored,\r
823f7d4f 975 and blocking I/O is performed.\r
976 If Event is NULL, then blocking I/O is performed.\r
6a6d955c 977 If Event is not NULL and non blocking I/O is supported,\r
978 then non-blocking I/O is performed, and Event will be signaled\r
979 when the SCSI Request Packet completes.\r
823f7d4f 980\r
6a6d955c 981Returns:\r
823f7d4f 982\r
983 EFI_STATUS\r
6a6d955c 984\r
823f7d4f 985--*/\r
986{\r
987 EFI_STATUS Status;\r
988 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate;\r
989 UINT8 TargetId;\r
6a6d955c 990\r
823f7d4f 991 AtapiScsiPrivate = ATAPI_EXT_SCSI_PASS_THRU_DEV_FROM_THIS (This);\r
992\r
993 //\r
994 // For ATAPI device, UINT8 is enough to represent the SCSI ID on channel.\r
995 //\r
996 TargetId = Target[0];\r
6a6d955c 997\r
823f7d4f 998 //\r
999 // Target is not allowed beyond MAX_TARGET_ID\r
1000 //\r
1001 if ((TargetId > MAX_TARGET_ID) || (Lun != 0)) {\r
1002 return EFI_INVALID_PARAMETER;\r
1003 }\r
6a6d955c 1004\r
823f7d4f 1005 //\r
1006 // check the data fields in Packet parameter.\r
1007 //\r
1008 Status = CheckExtSCSIRequestPacket (Packet);\r
1009 if (EFI_ERROR (Status)) {\r
1010 return Status;\r
1011 }\r
1012\r
1013 //\r
1014 // If Request Packet targets at the IDE channel itself,\r
1015 // do nothing.\r
1016 //\r
1017 if (TargetId == (UINT8)This->Mode->AdapterId) {\r
1018 Packet->InTransferLength = Packet->OutTransferLength = 0;\r
1019 return EFI_SUCCESS;\r
1020 }\r
6a6d955c 1021\r
823f7d4f 1022 //\r
1023 // According to Target ID, reset the Atapi I/O Register mapping\r
1024 // (Target Id in [0,1] area, using AtapiIoPortRegisters[0],\r
1025 // Target Id in [2,3] area, using AtapiIoPortRegisters[1]\r
1026 //\r
1027 if ((TargetId / 2) == 0) {\r
1028 TargetId = (UINT8) (TargetId % 2);\r
1029 AtapiScsiPrivate->IoPort = &AtapiScsiPrivate->AtapiIoPortRegisters[0];\r
1030 } else {\r
1031 TargetId = (UINT8) (TargetId % 2);\r
1032 AtapiScsiPrivate->IoPort = &AtapiScsiPrivate->AtapiIoPortRegisters[1];\r
1033 }\r
6a6d955c 1034\r
823f7d4f 1035 //\r
1036 // the ATAPI SCSI interface does not support non-blocking I/O\r
1037 // ignore the Event parameter\r
1038 //\r
1039 // Performs blocking I/O.\r
1040 //\r
1041 Status = SubmitExtBlockingIoCommand (AtapiScsiPrivate, TargetId, Packet);\r
1042 return Status;\r
1043}\r
1044\r
1045EFI_STATUS\r
1046EFIAPI\r
1047AtapiExtScsiPassThruGetNextTargetLun (\r
1048 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,\r
1049 IN OUT UINT8 **Target,\r
1050 IN OUT UINT64 *Lun\r
1051 )\r
1052/*++\r
1053\r
1054Routine Description:\r
1055\r
6a6d955c 1056 Used to retrieve the list of legal Target IDs for SCSI devices\r
823f7d4f 1057 on a SCSI channel.\r
1058\r
1059Arguments:\r
1060\r
1061 This - Protocol instance pointer.\r
6a6d955c 1062 Target - On input, a pointer to the Target ID of a SCSI\r
1063 device present on the SCSI channel. On output,\r
823f7d4f 1064 a pointer to the Target ID of the next SCSI device\r
6a6d955c 1065 present on a SCSI channel. An input value of\r
1066 0xFFFFFFFF retrieves the Target ID of the first\r
823f7d4f 1067 SCSI device present on a SCSI channel.\r
1068 Lun - On input, a pointer to the LUN of a SCSI device\r
1069 present on the SCSI channel. On output, a pointer\r
6a6d955c 1070 to the LUN of the next SCSI device present on\r
823f7d4f 1071 a SCSI channel.\r
1072Returns:\r
1073\r
6a6d955c 1074 EFI_SUCCESS - The Target ID and Lun of the next SCSI device\r
823f7d4f 1075 on the SCSI channel was returned in Target and Lun.\r
1076 EFI_NOT_FOUND - There are no more SCSI devices on this SCSI channel.\r
1077 EFI_INVALID_PARAMETER - Target is not 0xFFFFFFFF,and Target and Lun were not\r
1078 returned on a previous call to GetNextDevice().\r
1079--*/\r
1080{\r
1081 UINT8 ByteIndex;\r
1082 UINT8 TargetId;\r
1083 UINT8 ScsiId[TARGET_MAX_BYTES];\r
6a6d955c 1084 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate;\r
823f7d4f 1085\r
1086 //\r
1087 // Retrieve Device Private Data Structure.\r
1088 //\r
1089 AtapiScsiPrivate = ATAPI_EXT_SCSI_PASS_THRU_DEV_FROM_THIS (This);\r
1090\r
1091 //\r
1092 // Check whether Target is valid.\r
1093 //\r
1094 if (*Target == NULL || Lun == NULL) {\r
1095 return EFI_INVALID_PARAMETER;\r
1096 }\r
1097\r
1098 SetMem (ScsiId, TARGET_MAX_BYTES, 0xFF);\r
1099\r
1100 TargetId = (*Target)[0];\r
1101\r
1102 //\r
1103 // For ATAPI device, we use UINT8 to represent the SCSI ID on channel.\r
1104 //\r
1105 if (CompareMem(*Target, ScsiId, TARGET_MAX_BYTES) != 0) {\r
1106 for (ByteIndex = 1; ByteIndex < TARGET_MAX_BYTES; ByteIndex++) {\r
1107 if ((*Target)[ByteIndex] != 0) {\r
1108 return EFI_INVALID_PARAMETER;\r
1109 }\r
6a6d955c 1110 }\r
823f7d4f 1111 }\r
6a6d955c 1112\r
823f7d4f 1113 if ((CompareMem(*Target, ScsiId, TARGET_MAX_BYTES) != 0) &&\r
1114 ((TargetId != AtapiScsiPrivate->LatestTargetId) ||\r
1115 (*Lun != AtapiScsiPrivate->LatestLun))) {\r
1116 return EFI_INVALID_PARAMETER;\r
1117 }\r
1118\r
1119 if (TargetId == MAX_TARGET_ID) {\r
1120 return EFI_NOT_FOUND;\r
1121 }\r
1122\r
1123 if (CompareMem(*Target, ScsiId, TARGET_MAX_BYTES) == 0) {\r
1124 SetMem (*Target, TARGET_MAX_BYTES,0);\r
1125 } else {\r
1126 (*Target)[0] = (UINT8) (AtapiScsiPrivate->LatestTargetId + 1);\r
1127 }\r
1128\r
1129 *Lun = 0;\r
1130\r
1131 //\r
1132 // Update the LatestTargetId.\r
1133 //\r
1134 AtapiScsiPrivate->LatestTargetId = (*Target)[0];\r
1135 AtapiScsiPrivate->LatestLun = *Lun;\r
1136\r
1137 return EFI_SUCCESS;\r
1138\r
1139}\r
1140\r
1141EFI_STATUS\r
1142EFIAPI\r
1143AtapiExtScsiPassThruBuildDevicePath (\r
1144 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,\r
1145 IN UINT8 *Target,\r
1146 IN UINT64 Lun,\r
1147 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath\r
1148 )\r
1149/*++\r
1150\r
1151Routine Description:\r
1152\r
6a6d955c 1153 Used to allocate and build a device path node for a SCSI device\r
823f7d4f 1154 on a SCSI channel. Would not build device path for a SCSI Host Controller.\r
1155\r
1156Arguments:\r
1157\r
1158 This - Protocol instance pointer.\r
1159 Target - The Target ID of the SCSI device for which\r
1160 a device path node is to be allocated and built.\r
6a6d955c 1161 Lun - The LUN of the SCSI device for which a device\r
823f7d4f 1162 path node is to be allocated and built.\r
6a6d955c 1163 DevicePath - A pointer to a single device path node that\r
1164 describes the SCSI device specified by\r
1165 Target and Lun. This function is responsible\r
823f7d4f 1166 for allocating the buffer DevicePath with the boot\r
6a6d955c 1167 service AllocatePool(). It is the caller's\r
823f7d4f 1168 responsibility to free DevicePath when the caller\r
6a6d955c 1169 is finished with DevicePath.\r
823f7d4f 1170 Returns:\r
1171 EFI_SUCCESS - The device path node that describes the SCSI device\r
6a6d955c 1172 specified by Target and Lun was allocated and\r
823f7d4f 1173 returned in DevicePath.\r
1174 EFI_NOT_FOUND - The SCSI devices specified by Target and Lun does\r
1175 not exist on the SCSI channel.\r
1176 EFI_INVALID_PARAMETER - DevicePath is NULL.\r
6a6d955c 1177 EFI_OUT_OF_RESOURCES - There are not enough resources to allocate\r
823f7d4f 1178 DevicePath.\r
1179--*/\r
1180{\r
1181 EFI_DEV_PATH *Node;\r
1182 UINT8 TargetId;\r
1183\r
6a6d955c 1184 TargetId = Target[0];\r
823f7d4f 1185\r
1186 //\r
1187 // Validate parameters passed in.\r
1188 //\r
6a6d955c 1189\r
823f7d4f 1190 if (DevicePath == NULL) {\r
1191 return EFI_INVALID_PARAMETER;\r
1192 }\r
6a6d955c 1193\r
823f7d4f 1194 //\r
1195 // can not build device path for the SCSI Host Controller.\r
1196 //\r
1197 if ((TargetId > (MAX_TARGET_ID - 1)) || (Lun != 0)) {\r
1198 return EFI_NOT_FOUND;\r
1199 }\r
1200\r
1201 Node = AllocateZeroPool (sizeof (EFI_DEV_PATH));\r
1202 if (Node == NULL) {\r
1203 return EFI_OUT_OF_RESOURCES;\r
1204 }\r
1205\r
1206 Node->DevPath.Type = MESSAGING_DEVICE_PATH;\r
1207 Node->DevPath.SubType = MSG_ATAPI_DP;\r
1208 SetDevicePathNodeLength (&Node->DevPath, sizeof (ATAPI_DEVICE_PATH));\r
1209\r
1210 Node->Atapi.PrimarySecondary = (UINT8) (TargetId / 2);\r
1211 Node->Atapi.SlaveMaster = (UINT8) (TargetId % 2);\r
1212 Node->Atapi.Lun = (UINT16) Lun;\r
1213\r
1214 *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) Node;\r
1215\r
1216 return EFI_SUCCESS;\r
1217}\r
1218\r
1219EFI_STATUS\r
1220EFIAPI\r
1221AtapiExtScsiPassThruGetTargetLun (\r
1222 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,\r
1223 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,\r
1224 OUT UINT8 **Target,\r
1225 OUT UINT64 *Lun\r
1226 )\r
1227/*++\r
1228\r
1229Routine Description:\r
1230\r
1231 Used to translate a device path node to a Target ID and LUN.\r
1232\r
1233Arguments:\r
1234\r
1235 This - Protocol instance pointer.\r
6a6d955c 1236 DevicePath - A pointer to the device path node that\r
823f7d4f 1237 describes a SCSI device on the SCSI channel.\r
6a6d955c 1238 Target - A pointer to the Target ID of a SCSI device\r
1239 on the SCSI channel.\r
1240 Lun - A pointer to the LUN of a SCSI device on\r
1241 the SCSI channel.\r
823f7d4f 1242Returns:\r
1243\r
6a6d955c 1244 EFI_SUCCESS - DevicePath was successfully translated to a\r
1245 Target ID and LUN, and they were returned\r
823f7d4f 1246 in Target and Lun.\r
1247 EFI_INVALID_PARAMETER - DevicePath/Target/Lun is NULL.\r
6a6d955c 1248 EFI_UNSUPPORTED - This driver does not support the device path\r
823f7d4f 1249 node type in DevicePath.\r
6a6d955c 1250 EFI_NOT_FOUND - A valid translation from DevicePath to a\r
823f7d4f 1251 Target ID and LUN does not exist.\r
1252--*/\r
1253{\r
1254 EFI_DEV_PATH *Node;\r
1255\r
1256 //\r
1257 // Validate parameters passed in.\r
1258 //\r
1259 if (DevicePath == NULL || Target == NULL || Lun == NULL) {\r
1260 return EFI_INVALID_PARAMETER;\r
1261 }\r
6a6d955c 1262\r
823f7d4f 1263 //\r
1264 // Check whether the DevicePath belongs to SCSI_DEVICE_PATH\r
1265 //\r
1266 if ((DevicePath->Type != MESSAGING_DEVICE_PATH) ||\r
1267 (DevicePath->SubType != MSG_ATAPI_DP) ||\r
1268 (DevicePathNodeLength(DevicePath) != sizeof(ATAPI_DEVICE_PATH))) {\r
1269 return EFI_UNSUPPORTED;\r
1270 }\r
1271\r
1272 ZeroMem (*Target, TARGET_MAX_BYTES);\r
1273\r
1274 Node = (EFI_DEV_PATH *) DevicePath;\r
1275\r
1276 (*Target)[0] = (UINT8) (Node->Atapi.PrimarySecondary * 2 + Node->Atapi.SlaveMaster);\r
1277 *Lun = Node->Atapi.Lun;\r
1278\r
1279 if ((*Target)[0] > (MAX_TARGET_ID - 1) || *Lun != 0) {\r
1280 return EFI_NOT_FOUND;\r
1281 }\r
1282\r
1283 return EFI_SUCCESS;\r
1284}\r
1285\r
1286EFI_STATUS\r
1287EFIAPI\r
1288AtapiExtScsiPassThruResetChannel (\r
1289 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This\r
1290 )\r
1291/*++\r
1292\r
1293Routine Description:\r
1294\r
6a6d955c 1295 Resets a SCSI channel.This operation resets all the\r
823f7d4f 1296 SCSI devices connected to the SCSI channel.\r
1297\r
1298Arguments:\r
1299\r
1300 This - Protocol instance pointer.\r
1301\r
1302Returns:\r
1303\r
1304 EFI_SUCCESS - The SCSI channel was reset.\r
6a6d955c 1305 EFI_UNSUPPORTED - The SCSI channel does not support\r
823f7d4f 1306 a channel reset operation.\r
6a6d955c 1307 EFI_DEVICE_ERROR - A device error occurred while\r
823f7d4f 1308 attempting to reset the SCSI channel.\r
6a6d955c 1309 EFI_TIMEOUT - A timeout occurred while attempting\r
823f7d4f 1310 to reset the SCSI channel.\r
1311--*/\r
1312{\r
1313 UINT8 DeviceControlValue;\r
1314 UINT8 Index;\r
1315 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate;\r
1316 BOOLEAN ResetFlag;\r
1317\r
1318 AtapiScsiPrivate = ATAPI_EXT_SCSI_PASS_THRU_DEV_FROM_THIS (This);\r
1319 ResetFlag = FALSE;\r
1320 //\r
1321 // Reset both Primary channel and Secondary channel.\r
1322 // so, the IoPort pointer must point to the right I/O Register group\r
1323 // And if there is a channel reset successfully, return EFI_SUCCESS.\r
1324 //\r
1325 for (Index = 0; Index < 2; Index++) {\r
1326 //\r
1327 // Reset\r
1328 //\r
1329 AtapiScsiPrivate->IoPort = &AtapiScsiPrivate->AtapiIoPortRegisters[Index];\r
1330\r
1331 DeviceControlValue = 0;\r
1332 //\r
1333 // set SRST bit to initiate soft reset\r
1334 //\r
1335 DeviceControlValue |= SRST;\r
1336 //\r
1337 // disable Interrupt\r
1338 //\r
1339 DeviceControlValue |= bit (1);\r
1340 WritePortB (\r
1341 AtapiScsiPrivate->PciIo,\r
1342 AtapiScsiPrivate->IoPort->Alt.DeviceControl,\r
1343 DeviceControlValue\r
1344 );\r
1345\r
1346 //\r
1347 // Wait 10us\r
1348 //\r
1349 gBS->Stall (10);\r
1350\r
1351 //\r
1352 // Clear SRST bit\r
1353 // 0xfb:1111,1011\r
1354 //\r
1355 DeviceControlValue &= 0xfb;\r
6a6d955c 1356\r
823f7d4f 1357 WritePortB (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->Alt.DeviceControl, DeviceControlValue);\r
1358\r
1359 //\r
1360 // slave device needs at most 31s to clear BSY\r
1361 //\r
1362 if (StatusWaitForBSYClear (AtapiScsiPrivate, 31000000) != EFI_TIMEOUT) {\r
1363 ResetFlag = TRUE;\r
1364 }\r
1365 }\r
1366\r
1367 if (ResetFlag) {\r
1368 return EFI_SUCCESS;\r
1369 }\r
6a6d955c 1370\r
823f7d4f 1371 return EFI_TIMEOUT;\r
1372}\r
1373\r
1374EFI_STATUS\r
1375EFIAPI\r
1376AtapiExtScsiPassThruResetTarget (\r
1377 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,\r
1378 IN UINT8 *Target,\r
1379 IN UINT64 Lun\r
1380 )\r
1381/*++\r
1382\r
1383Routine Description:\r
1384\r
1385 Resets a SCSI device that is connected to a SCSI channel.\r
1386\r
1387Arguments:\r
1388\r
1389 This - Protocol instance pointer.\r
6a6d955c 1390 Target - The Target ID of the SCSI device to reset.\r
823f7d4f 1391 Lun - The LUN of the SCSI device to reset.\r
6a6d955c 1392\r
823f7d4f 1393Returns:\r
1394\r
6a6d955c 1395 EFI_SUCCESS - The SCSI device specified by Target and\r
823f7d4f 1396 Lun was reset.\r
1397 EFI_UNSUPPORTED - The SCSI channel does not support a target\r
1398 reset operation.\r
1399 EFI_INVALID_PARAMETER - Target or Lun are invalid.\r
6a6d955c 1400 EFI_DEVICE_ERROR - A device error occurred while attempting\r
1401 to reset the SCSI device specified by Target\r
823f7d4f 1402 and Lun.\r
6a6d955c 1403 EFI_TIMEOUT - A timeout occurred while attempting to reset\r
823f7d4f 1404 the SCSI device specified by Target and Lun.\r
1405--*/\r
1406{\r
1407 UINT8 Command;\r
1408 UINT8 DeviceSelect;\r
1409 UINT8 TargetId;\r
1410 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate;\r
6a6d955c 1411\r
823f7d4f 1412 AtapiScsiPrivate = ATAPI_EXT_SCSI_PASS_THRU_DEV_FROM_THIS (This);\r
1413 TargetId = Target[0];\r
6a6d955c 1414\r
823f7d4f 1415 if ((TargetId > MAX_TARGET_ID) || (Lun != 0)) {\r
1416 return EFI_INVALID_PARAMETER;\r
1417 }\r
1418 //\r
1419 // Directly return EFI_SUCCESS if want to reset the host controller\r
1420 //\r
1421 if (TargetId == This->Mode->AdapterId) {\r
1422 return EFI_SUCCESS;\r
1423 }\r
6a6d955c 1424\r
823f7d4f 1425 //\r
1426 // According to Target ID, reset the Atapi I/O Register mapping\r
1427 // (Target Id in [0,1] area, using AtapiIoPortRegisters[0],\r
1428 // Target Id in [2,3] area, using AtapiIoPortRegisters[1]\r
1429 //\r
1430 if ((TargetId / 2) == 0) {\r
1431 AtapiScsiPrivate->IoPort = &AtapiScsiPrivate->AtapiIoPortRegisters[0];\r
1432 } else {\r
1433 AtapiScsiPrivate->IoPort = &AtapiScsiPrivate->AtapiIoPortRegisters[1];\r
1434 }\r
6a6d955c 1435\r
823f7d4f 1436 //\r
1437 // for ATAPI device, no need to wait DRDY ready after device selecting.\r
1438 //\r
1439 // bit7 and bit5 are both set to 1 for backward compatibility\r
1440 //\r
1441 DeviceSelect = (UINT8) (((bit (7) | bit (5)) | (TargetId << 4)));\r
1442 WritePortB (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->Head, DeviceSelect);\r
1443\r
1444 Command = ATAPI_SOFT_RESET_CMD;\r
1445 WritePortB (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->Reg.Command, Command);\r
1446\r
1447 //\r
1448 // BSY clear is the only status return to the host by the device\r
1449 // when reset is complete.\r
1450 // slave device needs at most 31s to clear BSY\r
1451 //\r
1452 if (EFI_ERROR (StatusWaitForBSYClear (AtapiScsiPrivate, 31000000))) {\r
1453 return EFI_TIMEOUT;\r
1454 }\r
6a6d955c 1455\r
823f7d4f 1456 //\r
1457 // stall 5 seconds to make the device status stable\r
1458 //\r
1459 gBS->Stall (5000000);\r
1460\r
1461 return EFI_SUCCESS;\r
1462}\r
1463\r
1464\r
1465EFI_STATUS\r
1466EFIAPI\r
1467AtapiExtScsiPassThruGetNextTarget (\r
1468 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,\r
1469 IN OUT UINT8 **Target\r
1470 )\r
1471/*++\r
1472\r
1473Routine Description:\r
6a6d955c 1474 Used to retrieve the list of legal Target IDs for SCSI devices\r
823f7d4f 1475 on a SCSI channel.\r
1476\r
1477Arguments:\r
1478 This - Protocol instance pointer.\r
6a6d955c 1479 Target - On input, a pointer to the Target ID of a SCSI\r
1480 device present on the SCSI channel. On output,\r
823f7d4f 1481 a pointer to the Target ID of the next SCSI device\r
6a6d955c 1482 present on a SCSI channel. An input value of\r
1483 0xFFFFFFFF retrieves the Target ID of the first\r
823f7d4f 1484 SCSI device present on a SCSI channel.\r
1485 Lun - On input, a pointer to the LUN of a SCSI device\r
1486 present on the SCSI channel. On output, a pointer\r
6a6d955c 1487 to the LUN of the next SCSI device present on\r
823f7d4f 1488 a SCSI channel.\r
6a6d955c 1489\r
823f7d4f 1490Returns:\r
6a6d955c 1491 EFI_SUCCESS - The Target ID and Lun of the next SCSI device\r
823f7d4f 1492 on the SCSI channel was returned in Target and Lun.\r
1493 EFI_NOT_FOUND - There are no more SCSI devices on this SCSI channel.\r
1494 EFI_INVALID_PARAMETER - Target is not 0xFFFFFFFF,and Target and Lun were not\r
1495 returned on a previous call to GetNextDevice().\r
1496--*/\r
1497{\r
1498 UINT8 TargetId;\r
1499 UINT8 ScsiId[TARGET_MAX_BYTES];\r
6a6d955c 1500 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate;\r
823f7d4f 1501 UINT8 ByteIndex;\r
1502\r
1503 //\r
1504 // Retrieve Device Private Data Structure.\r
1505 //\r
1506 AtapiScsiPrivate = ATAPI_EXT_SCSI_PASS_THRU_DEV_FROM_THIS (This);\r
1507\r
1508 //\r
1509 // Check whether Target is valid.\r
1510 //\r
1511 if (*Target == NULL ) {\r
1512 return EFI_INVALID_PARAMETER;\r
1513 }\r
1514\r
1515 TargetId = (*Target)[0];\r
1516 SetMem (ScsiId, TARGET_MAX_BYTES, 0xFF);\r
1517\r
1518 //\r
1519 // For ATAPI device, we use UINT8 to represent the SCSI ID on channel.\r
1520 //\r
1521 if (CompareMem(*Target, ScsiId, TARGET_MAX_BYTES) != 0) {\r
1522 for (ByteIndex = 1; ByteIndex < TARGET_MAX_BYTES; ByteIndex++) {\r
1523 if ((*Target)[ByteIndex] != 0) {\r
1524 return EFI_INVALID_PARAMETER;\r
1525 }\r
1526 }\r
1527 }\r
1528\r
1529 if ((CompareMem(*Target, ScsiId, TARGET_MAX_BYTES) != 0) &&(TargetId != AtapiScsiPrivate->LatestTargetId)) {\r
1530 return EFI_INVALID_PARAMETER;\r
1531 }\r
1532\r
1533 if (TargetId == MAX_TARGET_ID) {\r
1534 return EFI_NOT_FOUND;\r
1535 }\r
1536\r
1537 if ((CompareMem(*Target, ScsiId, TARGET_MAX_BYTES) == 0)) {\r
1538 SetMem (*Target, TARGET_MAX_BYTES, 0);\r
1539 } else {\r
1540 (*Target)[0] = (UINT8) (AtapiScsiPrivate->LatestTargetId + 1);\r
1541 }\r
1542\r
1543 //\r
1544 // Update the LatestTargetId.\r
1545 //\r
1546 AtapiScsiPrivate->LatestTargetId = (*Target)[0];\r
1547 AtapiScsiPrivate->LatestLun = 0;\r
1548\r
1549 return EFI_SUCCESS;\r
1550}\r
1551\r
1552EFI_STATUS\r
1553GetIdeRegistersBaseAddr (\r
1554 IN EFI_PCI_IO_PROTOCOL *PciIo,\r
1555 OUT IDE_REGISTERS_BASE_ADDR *IdeRegsBaseAddr\r
1556 )\r
1557/*++\r
1558\r
1559Routine Description:\r
1560 Get IDE IO port registers' base addresses by mode. In 'Compatibility' mode,\r
1561 use fixed addresses. In Native-PCI mode, get base addresses from BARs in\r
1562 the PCI IDE controller's Configuration Space.\r
1563\r
1564Arguments:\r
1565 PciIo - Pointer to the EFI_PCI_IO_PROTOCOL instance\r
1566 IdeRegsBaseAddr - Pointer to IDE_REGISTERS_BASE_ADDR to\r
1567 receive IDE IO port registers' base addresses\r
1568\r
1569Returns:\r
1570\r
1571 EFI_STATUS\r
1572\r
1573--*/\r
1574{\r
1575 EFI_STATUS Status;\r
1576 PCI_TYPE00 PciData;\r
1577\r
1578 Status = PciIo->Pci.Read (\r
1579 PciIo,\r
1580 EfiPciIoWidthUint8,\r
1581 0,\r
1582 sizeof (PciData),\r
1583 &PciData\r
1584 );\r
1585\r
1586 if (EFI_ERROR (Status)) {\r
1587 return Status;\r
1588 }\r
1589\r
1590 if ((PciData.Hdr.ClassCode[0] & IDE_PRIMARY_OPERATING_MODE) == 0) {\r
1591 IdeRegsBaseAddr[IdePrimary].CommandBlockBaseAddr = 0x1f0;\r
1592 IdeRegsBaseAddr[IdePrimary].ControlBlockBaseAddr = 0x3f6;\r
1593 } else {\r
1594 //\r
1595 // The BARs should be of IO type\r
1596 //\r
1597 if ((PciData.Device.Bar[0] & BIT0) == 0 ||\r
1598 (PciData.Device.Bar[1] & BIT0) == 0) {\r
1599 return EFI_UNSUPPORTED;\r
1600 }\r
1601\r
1602 IdeRegsBaseAddr[IdePrimary].CommandBlockBaseAddr =\r
1603 (UINT16) (PciData.Device.Bar[0] & 0x0000fff8);\r
1604 IdeRegsBaseAddr[IdePrimary].ControlBlockBaseAddr =\r
1605 (UINT16) ((PciData.Device.Bar[1] & 0x0000fffc) + 2);\r
1606 }\r
1607\r
1608 if ((PciData.Hdr.ClassCode[0] & IDE_SECONDARY_OPERATING_MODE) == 0) {\r
1609 IdeRegsBaseAddr[IdeSecondary].CommandBlockBaseAddr = 0x170;\r
1610 IdeRegsBaseAddr[IdeSecondary].ControlBlockBaseAddr = 0x376;\r
1611 } else {\r
1612 //\r
1613 // The BARs should be of IO type\r
1614 //\r
1615 if ((PciData.Device.Bar[2] & BIT0) == 0 ||\r
1616 (PciData.Device.Bar[3] & BIT0) == 0) {\r
1617 return EFI_UNSUPPORTED;\r
1618 }\r
1619\r
1620 IdeRegsBaseAddr[IdeSecondary].CommandBlockBaseAddr =\r
1621 (UINT16) (PciData.Device.Bar[2] & 0x0000fff8);\r
1622 IdeRegsBaseAddr[IdeSecondary].ControlBlockBaseAddr =\r
1623 (UINT16) ((PciData.Device.Bar[3] & 0x0000fffc) + 2);\r
1624 }\r
1625\r
1626 return EFI_SUCCESS;\r
1627}\r
1628\r
1629VOID\r
1630InitAtapiIoPortRegisters (\r
1631 IN ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,\r
1632 IN IDE_REGISTERS_BASE_ADDR *IdeRegsBaseAddr\r
1633 )\r
1634/*++\r
1635\r
1636Routine Description:\r
1637\r
1638 Initialize each Channel's Base Address of CommandBlock and ControlBlock.\r
1639\r
1640Arguments:\r
1641\r
1642 AtapiScsiPrivate - The pointer of ATAPI_SCSI_PASS_THRU_DEV\r
1643 IdeRegsBaseAddr - The pointer of IDE_REGISTERS_BASE_ADDR\r
1644\r
1645Returns:\r
1646\r
1647 None\r
1648\r
1649--*/\r
1650{\r
1651\r
1652 UINT8 IdeChannel;\r
1653 UINT16 CommandBlockBaseAddr;\r
1654 UINT16 ControlBlockBaseAddr;\r
1655 IDE_BASE_REGISTERS *RegisterPointer;\r
1656\r
1657\r
1658 for (IdeChannel = 0; IdeChannel < ATAPI_MAX_CHANNEL; IdeChannel++) {\r
1659\r
1660 RegisterPointer = &AtapiScsiPrivate->AtapiIoPortRegisters[IdeChannel];\r
1661\r
1662 //\r
1663 // Initialize IDE IO port addresses, including Command Block registers\r
1664 // and Control Block registers\r
1665 //\r
1666 CommandBlockBaseAddr = IdeRegsBaseAddr[IdeChannel].CommandBlockBaseAddr;\r
1667 ControlBlockBaseAddr = IdeRegsBaseAddr[IdeChannel].ControlBlockBaseAddr;\r
1668\r
1669 RegisterPointer->Data = CommandBlockBaseAddr;\r
1670 (*(UINT16 *) &RegisterPointer->Reg1) = (UINT16) (CommandBlockBaseAddr + 0x01);\r
1671 RegisterPointer->SectorCount = (UINT16) (CommandBlockBaseAddr + 0x02);\r
1672 RegisterPointer->SectorNumber = (UINT16) (CommandBlockBaseAddr + 0x03);\r
1673 RegisterPointer->CylinderLsb = (UINT16) (CommandBlockBaseAddr + 0x04);\r
1674 RegisterPointer->CylinderMsb = (UINT16) (CommandBlockBaseAddr + 0x05);\r
1675 RegisterPointer->Head = (UINT16) (CommandBlockBaseAddr + 0x06);\r
1676 (*(UINT16 *) &RegisterPointer->Reg) = (UINT16) (CommandBlockBaseAddr + 0x07);\r
1677\r
1678 (*(UINT16 *) &RegisterPointer->Alt) = ControlBlockBaseAddr;\r
1679 RegisterPointer->DriveAddress = (UINT16) (ControlBlockBaseAddr + 0x01);\r
1680 }\r
1681\r
1682}\r
1683\r
1684\r
1685EFI_STATUS\r
1686CheckSCSIRequestPacket (\r
1687 EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet\r
1688 )\r
1689/*++\r
1690\r
1691Routine Description:\r
1692\r
1693 Checks the parameters in the SCSI Request Packet to make sure\r
1694 they are valid for a SCSI Pass Thru request.\r
1695\r
1696Arguments:\r
1697\r
1698 Packet - The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET\r
1699\r
1700Returns:\r
1701\r
1702 EFI_STATUS\r
1703\r
1704--*/\r
1705{\r
1706 if (Packet == NULL) {\r
1707 return EFI_INVALID_PARAMETER;\r
1708 }\r
1709\r
1710 if (!ValidCdbLength (Packet->CdbLength)) {\r
1711 return EFI_INVALID_PARAMETER;\r
1712 }\r
1713\r
1714 if (Packet->Cdb == NULL) {\r
1715 return EFI_INVALID_PARAMETER;\r
1716 }\r
1717\r
1718 //\r
1719 // Checks whether the request command is supported.\r
1720 //\r
1721 if (!IsCommandValid (Packet)) {\r
1722 return EFI_UNSUPPORTED;\r
1723 }\r
1724\r
1725 return EFI_SUCCESS;\r
1726}\r
1727\r
1728BOOLEAN\r
1729IsCommandValid (\r
1730 EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet\r
1731 )\r
1732/*++\r
6a6d955c 1733\r
823f7d4f 1734Routine Description:\r
1735\r
6a6d955c 1736 Checks the requested SCSI command:\r
823f7d4f 1737 Is it supported by this driver?\r
1738 Is the Data transfer direction reasonable?\r
1739\r
1740Arguments:\r
1741\r
6a6d955c 1742 Packet - The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET\r
823f7d4f 1743\r
1744Returns:\r
1745\r
1746 EFI_STATUS\r
1747\r
1748--*/\r
1749{\r
1750 UINT8 Index;\r
1751 UINT8 *OpCode;\r
1752\r
1753 OpCode = (UINT8 *) (Packet->Cdb);\r
1754\r
1755 for (Index = 0; CompareMem (&gSupportedATAPICommands[Index], &gEndTable, sizeof (SCSI_COMMAND_SET)); Index++) {\r
1756\r
1757 if (*OpCode == gSupportedATAPICommands[Index].OpCode) {\r
1758 //\r
1759 // Check whether the requested Command is supported by this driver\r
1760 //\r
1761 if (Packet->DataDirection == DataIn) {\r
1762 //\r
1763 // Check whether the requested data direction conforms to\r
1764 // what it should be.\r
1765 //\r
1766 if (gSupportedATAPICommands[Index].Direction == DataOut) {\r
1767 return FALSE;\r
1768 }\r
1769 }\r
1770\r
1771 if (Packet->DataDirection == DataOut) {\r
1772 //\r
1773 // Check whether the requested data direction conforms to\r
1774 // what it should be.\r
1775 //\r
1776 if (gSupportedATAPICommands[Index].Direction == DataIn) {\r
1777 return FALSE;\r
1778 }\r
1779 }\r
1780\r
1781 return TRUE;\r
1782 }\r
1783 }\r
1784\r
1785 return FALSE;\r
1786}\r
1787\r
1788EFI_STATUS\r
1789SubmitBlockingIoCommand (\r
1790 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,\r
1791 UINT32 Target,\r
1792 EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet\r
1793 )\r
1794/*++\r
1795\r
1796Routine Description:\r
1797\r
1798 Performs blocking I/O request.\r
6a6d955c 1799\r
823f7d4f 1800Arguments:\r
1801\r
1802 AtapiScsiPrivate: Private data structure for the specified channel.\r
6a6d955c 1803 Target: The Target ID of the ATAPI device to send the SCSI\r
823f7d4f 1804 Request Packet. To ATAPI devices attached on an IDE\r
1805 Channel, Target ID 0 indicates Master device;Target\r
1806 ID 1 indicates Slave device.\r
6a6d955c 1807 Packet: The SCSI Request Packet to send to the ATAPI device\r
823f7d4f 1808 specified by Target.\r
6a6d955c 1809\r
1810 Returns: EFI_STATUS\r
1811\r
823f7d4f 1812--*/\r
1813{\r
1814 UINT8 PacketCommand[12];\r
1815 UINT64 TimeoutInMicroSeconds;\r
1816 EFI_STATUS PacketCommandStatus;\r
1817\r
1818 //\r
1819 // Fill ATAPI Command Packet according to CDB\r
1820 //\r
1821 ZeroMem (&PacketCommand, 12);\r
1822 CopyMem (&PacketCommand, Packet->Cdb, Packet->CdbLength);\r
1823\r
1824 //\r
1825 // Timeout is 100ns unit, convert it to 1000ns (1us) unit.\r
1826 //\r
1827 TimeoutInMicroSeconds = DivU64x32 (Packet->Timeout, (UINT32) 10);\r
1828\r
1829 //\r
1830 // Submit ATAPI Command Packet\r
1831 //\r
1832 PacketCommandStatus = AtapiPacketCommand (\r
1833 AtapiScsiPrivate,\r
1834 Target,\r
1835 PacketCommand,\r
1836 Packet->DataBuffer,\r
1837 &(Packet->TransferLength),\r
1838 (DATA_DIRECTION) Packet->DataDirection,\r
1839 TimeoutInMicroSeconds\r
1840 );\r
1841 if (!EFI_ERROR (PacketCommandStatus) || (Packet->SenseData == NULL)) {\r
1842 Packet->SenseDataLength = 0;\r
1843 return PacketCommandStatus;\r
1844 }\r
1845\r
1846 //\r
1847 // Return SenseData if PacketCommandStatus matches\r
1848 // the following return codes.\r
1849 //\r
1850 if ((PacketCommandStatus == EFI_BAD_BUFFER_SIZE) ||\r
1851 (PacketCommandStatus == EFI_DEVICE_ERROR) ||\r
1852 (PacketCommandStatus == EFI_TIMEOUT)) {\r
1853\r
1854 //\r
1855 // avoid submit request sense command continuously.\r
1856 //\r
1857 if (PacketCommand[0] == OP_REQUEST_SENSE) {\r
1858 Packet->SenseDataLength = 0;\r
1859 return PacketCommandStatus;\r
1860 }\r
1861\r
1862 RequestSenseCommand (\r
1863 AtapiScsiPrivate,\r
1864 Target,\r
1865 Packet->Timeout,\r
1866 Packet->SenseData,\r
1867 &Packet->SenseDataLength\r
1868 );\r
1869 }\r
1870\r
1871 return PacketCommandStatus;\r
1872}\r
1873\r
1874EFI_STATUS\r
1875RequestSenseCommand (\r
1876 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,\r
1877 UINT32 Target,\r
1878 UINT64 Timeout,\r
1879 VOID *SenseData,\r
1880 UINT8 *SenseDataLength\r
1881 )\r
1882/*++\r
1883\r
1884Routine Description:\r
1885\r
1886 Sumbit request sense command\r
1887\r
1888Arguments:\r
1889\r
1890 AtapiScsiPrivate - The pionter of ATAPI_SCSI_PASS_THRU_DEV\r
1891 Target - The target ID\r
1892 Timeout - The time to complete the command\r
1893 SenseData - The buffer to fill in sense data\r
1894 SenseDataLength - The length of buffer\r
1895\r
1896Returns:\r
1897\r
1898 EFI_STATUS\r
6a6d955c 1899\r
823f7d4f 1900--*/\r
1901{\r
1902 EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET Packet;\r
1903 UINT8 Cdb[12];\r
1904 EFI_STATUS Status;\r
1905\r
1906 ZeroMem (&Packet, sizeof (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET));\r
1907 ZeroMem (Cdb, 12);\r
1908\r
1909 Cdb[0] = OP_REQUEST_SENSE;\r
1910 Cdb[4] = (UINT8) (*SenseDataLength);\r
1911\r
1912 Packet.Timeout = Timeout;\r
1913 Packet.DataBuffer = SenseData;\r
1914 Packet.SenseData = NULL;\r
1915 Packet.Cdb = Cdb;\r
1916 Packet.TransferLength = *SenseDataLength;\r
1917 Packet.CdbLength = 12;\r
1918 Packet.DataDirection = DataIn;\r
1919\r
1920 Status = SubmitBlockingIoCommand (AtapiScsiPrivate, Target, &Packet);\r
1921 *SenseDataLength = (UINT8) (Packet.TransferLength);\r
1922 return Status;\r
1923}\r
1924\r
1925EFI_STATUS\r
1926CheckExtSCSIRequestPacket (\r
1927 EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet\r
1928 )\r
1929/*++\r
1930\r
1931Routine Description:\r
1932\r
1933 Checks the parameters in the SCSI Request Packet to make sure\r
1934 they are valid for a SCSI Pass Thru request.\r
1935\r
1936Arguments:\r
1937\r
1938 Packet - The pointer of EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET\r
6a6d955c 1939\r
823f7d4f 1940Returns:\r
6a6d955c 1941\r
823f7d4f 1942 EFI_STATUS\r
1943\r
1944--*/\r
1945{\r
1946 if (Packet == NULL) {\r
1947 return EFI_INVALID_PARAMETER;\r
1948 }\r
1949\r
1950 if (!ValidCdbLength (Packet->CdbLength)) {\r
1951 return EFI_INVALID_PARAMETER;\r
1952 }\r
1953\r
1954 if (Packet->Cdb == NULL) {\r
1955 return EFI_INVALID_PARAMETER;\r
1956 }\r
6a6d955c 1957\r
823f7d4f 1958 //\r
1959 // Checks whether the request command is supported.\r
1960 //\r
1961 if (!IsExtCommandValid (Packet)) {\r
1962 return EFI_UNSUPPORTED;\r
1963 }\r
1964\r
1965 return EFI_SUCCESS;\r
1966}\r
1967\r
1968\r
1969BOOLEAN\r
1970IsExtCommandValid (\r
1971 EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet\r
1972 )\r
1973/*++\r
6a6d955c 1974\r
823f7d4f 1975Routine Description:\r
1976\r
6a6d955c 1977 Checks the requested SCSI command:\r
823f7d4f 1978 Is it supported by this driver?\r
1979 Is the Data transfer direction reasonable?\r
1980\r
1981Arguments:\r
1982\r
6a6d955c 1983 Packet - The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET\r
823f7d4f 1984\r
1985Returns:\r
1986\r
1987 EFI_STATUS\r
1988\r
1989--*/\r
1990{\r
1991 UINT8 Index;\r
1992 UINT8 *OpCode;\r
1993\r
1994 OpCode = (UINT8 *) (Packet->Cdb);\r
1995\r
1996 for (Index = 0; CompareMem (&gSupportedATAPICommands[Index], &gEndTable, sizeof (SCSI_COMMAND_SET)); Index++) {\r
1997\r
1998 if (*OpCode == gSupportedATAPICommands[Index].OpCode) {\r
1999 //\r
2000 // Check whether the requested Command is supported by this driver\r
2001 //\r
2002 if (Packet->DataDirection == DataIn) {\r
2003 //\r
2004 // Check whether the requested data direction conforms to\r
2005 // what it should be.\r
2006 //\r
2007 if (gSupportedATAPICommands[Index].Direction == DataOut) {\r
2008 return FALSE;\r
2009 }\r
2010 }\r
2011\r
2012 if (Packet->DataDirection == DataOut) {\r
2013 //\r
2014 // Check whether the requested data direction conforms to\r
2015 // what it should be.\r
2016 //\r
2017 if (gSupportedATAPICommands[Index].Direction == DataIn) {\r
2018 return FALSE;\r
2019 }\r
2020 }\r
2021\r
2022 return TRUE;\r
2023 }\r
2024 }\r
2025\r
2026 return FALSE;\r
2027}\r
2028\r
2029EFI_STATUS\r
2030SubmitExtBlockingIoCommand (\r
2031 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,\r
2032 UINT8 Target,\r
2033 EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet\r
2034 )\r
2035/*++\r
2036\r
2037Routine Description:\r
2038\r
2039 Performs blocking I/O request.\r
6a6d955c 2040\r
823f7d4f 2041Arguments:\r
2042\r
2043 AtapiScsiPrivate: Private data structure for the specified channel.\r
6a6d955c 2044 Target: The Target ID of the ATAPI device to send the SCSI\r
823f7d4f 2045 Request Packet. To ATAPI devices attached on an IDE\r
2046 Channel, Target ID 0 indicates Master device;Target\r
2047 ID 1 indicates Slave device.\r
6a6d955c 2048 Packet: The SCSI Request Packet to send to the ATAPI device\r
823f7d4f 2049 specified by Target.\r
6a6d955c 2050\r
2051 Returns: EFI_STATUS\r
2052\r
823f7d4f 2053--*/\r
2054{\r
2055 UINT8 PacketCommand[12];\r
2056 UINT64 TimeoutInMicroSeconds;\r
2057 EFI_STATUS PacketCommandStatus;\r
2058\r
2059 //\r
2060 // Fill ATAPI Command Packet according to CDB\r
2061 //\r
2062 ZeroMem (&PacketCommand, 12);\r
2063 CopyMem (&PacketCommand, Packet->Cdb, Packet->CdbLength);\r
2064\r
2065 //\r
2066 // Timeout is 100ns unit, convert it to 1000ns (1us) unit.\r
2067 //\r
2068 TimeoutInMicroSeconds = DivU64x32 (Packet->Timeout, (UINT32) 10);\r
2069\r
2070 //\r
2071 // Submit ATAPI Command Packet\r
2072 //\r
2073 if (Packet->DataDirection == DataIn) {\r
2074 PacketCommandStatus = AtapiPacketCommand (\r
2075 AtapiScsiPrivate,\r
2076 Target,\r
2077 PacketCommand,\r
2078 Packet->InDataBuffer,\r
2079 &(Packet->InTransferLength),\r
2080 DataIn,\r
2081 TimeoutInMicroSeconds\r
2082 );\r
2083 } else {\r
2084\r
2085 PacketCommandStatus = AtapiPacketCommand (\r
2086 AtapiScsiPrivate,\r
2087 Target,\r
2088 PacketCommand,\r
2089 Packet->OutDataBuffer,\r
2090 &(Packet->OutTransferLength),\r
2091 DataOut,\r
2092 TimeoutInMicroSeconds\r
2093 );\r
2094 }\r
6a6d955c 2095\r
823f7d4f 2096 if (!EFI_ERROR (PacketCommandStatus) || (Packet->SenseData == NULL)) {\r
2097 Packet->SenseDataLength = 0;\r
2098 return PacketCommandStatus;\r
2099 }\r
6a6d955c 2100\r
823f7d4f 2101 //\r
2102 // Return SenseData if PacketCommandStatus matches\r
2103 // the following return codes.\r
2104 //\r
2105 if ((PacketCommandStatus == EFI_BAD_BUFFER_SIZE) ||\r
2106 (PacketCommandStatus == EFI_DEVICE_ERROR) ||\r
2107 (PacketCommandStatus == EFI_TIMEOUT)) {\r
2108\r
2109 //\r
2110 // avoid submit request sense command continuously.\r
2111 //\r
2112 if (PacketCommand[0] == OP_REQUEST_SENSE) {\r
2113 Packet->SenseDataLength = 0;\r
2114 return PacketCommandStatus;\r
2115 }\r
2116\r
2117 RequestSenseCommand (\r
2118 AtapiScsiPrivate,\r
2119 Target,\r
2120 Packet->Timeout,\r
2121 Packet->SenseData,\r
2122 &Packet->SenseDataLength\r
2123 );\r
2124 }\r
2125\r
2126 return PacketCommandStatus;\r
2127}\r
2128\r
2129\r
2130EFI_STATUS\r
2131AtapiPacketCommand (\r
2132 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,\r
2133 UINT32 Target,\r
2134 UINT8 *PacketCommand,\r
2135 VOID *Buffer,\r
2136 UINT32 *ByteCount,\r
2137 DATA_DIRECTION Direction,\r
2138 UINT64 TimeoutInMicroSeconds\r
2139 )\r
2140/*++\r
2141\r
2142Routine Description:\r
2143\r
2144 Submits ATAPI command packet to the specified ATAPI device.\r
6a6d955c 2145\r
823f7d4f 2146Arguments:\r
2147\r
2148 AtapiScsiPrivate: Private data structure for the specified channel.\r
6a6d955c 2149 Target: The Target ID of the ATAPI device to send the SCSI\r
823f7d4f 2150 Request Packet. To ATAPI devices attached on an IDE\r
2151 Channel, Target ID 0 indicates Master device;Target\r
2152 ID 1 indicates Slave device.\r
2153 PacketCommand: Points to the ATAPI command packet.\r
2154 Buffer: Points to the transferred data.\r
2155 ByteCount: When input,indicates the buffer size; when output,\r
2156 indicates the actually transferred data size.\r
6a6d955c 2157 Direction: Indicates the data transfer direction.\r
823f7d4f 2158 TimeoutInMicroSeconds:\r
6a6d955c 2159 The timeout, in micro second units, to use for the\r
823f7d4f 2160 execution of this ATAPI command.\r
6a6d955c 2161 A TimeoutInMicroSeconds value of 0 means that\r
2162 this function will wait indefinitely for the ATAPI\r
823f7d4f 2163 command to execute.\r
6a6d955c 2164 If TimeoutInMicroSeconds is greater than zero, then\r
2165 this function will return EFI_TIMEOUT if the time\r
2166 required to execute the ATAPI command is greater\r
823f7d4f 2167 than TimeoutInMicroSeconds.\r
6a6d955c 2168\r
823f7d4f 2169Returns:\r
2170\r
2171 EFI_STATUS\r
6a6d955c 2172\r
823f7d4f 2173--*/\r
2174{\r
2175\r
2176 UINT16 *CommandIndex;\r
2177 UINT8 Count;\r
2178 EFI_STATUS Status;\r
2179\r
2180 //\r
2181 // Set all the command parameters by fill related registers.\r
2182 // Before write to all the following registers, BSY must be 0.\r
2183 //\r
2184 Status = StatusWaitForBSYClear (AtapiScsiPrivate, TimeoutInMicroSeconds);\r
2185 if (EFI_ERROR (Status)) {\r
2186 return EFI_DEVICE_ERROR;\r
2187 }\r
2188\r
2189\r
2190 //\r
2191 // Select device via Device/Head Register.\r
2192 // "Target = 0" indicates device 0; "Target = 1" indicates device 1\r
2193 //\r
2194 WritePortB (\r
2195 AtapiScsiPrivate->PciIo,\r
2196 AtapiScsiPrivate->IoPort->Head,\r
2197 (UINT8) ((Target << 4) | DEFAULT_CMD) // DEFAULT_CMD: 0xa0 (1010,0000)\r
2198 );\r
2199\r
2200 //\r
2201 // Set all the command parameters by fill related registers.\r
2202 // Before write to all the following registers, BSY DRQ must be 0.\r
2203 //\r
2204 Status = StatusDRQClear(AtapiScsiPrivate, TimeoutInMicroSeconds);\r
2205\r
2206 if (EFI_ERROR (Status)) {\r
2207 if (Status == EFI_ABORTED) {\r
2208 Status = EFI_DEVICE_ERROR;\r
2209 }\r
2210 *ByteCount = 0;\r
2211 return Status;\r
2212 }\r
2213\r
2214 //\r
2215 // No OVL; No DMA (by setting feature register)\r
2216 //\r
2217 WritePortB (\r
2218 AtapiScsiPrivate->PciIo,\r
2219 AtapiScsiPrivate->IoPort->Reg1.Feature,\r
2220 0x00\r
2221 );\r
2222\r
2223 //\r
2224 // set the transfersize to MAX_ATAPI_BYTE_COUNT to let the device\r
2225 // determine how much data should be transfered.\r
2226 //\r
2227 WritePortB (\r
2228 AtapiScsiPrivate->PciIo,\r
2229 AtapiScsiPrivate->IoPort->CylinderLsb,\r
2230 (UINT8) (MAX_ATAPI_BYTE_COUNT & 0x00ff)\r
2231 );\r
2232 WritePortB (\r
2233 AtapiScsiPrivate->PciIo,\r
2234 AtapiScsiPrivate->IoPort->CylinderMsb,\r
2235 (UINT8) (MAX_ATAPI_BYTE_COUNT >> 8)\r
2236 );\r
2237\r
2238 //\r
2239 // DEFAULT_CTL:0x0a (0000,1010)\r
2240 // Disable interrupt\r
2241 //\r
2242 WritePortB (\r
2243 AtapiScsiPrivate->PciIo,\r
2244 AtapiScsiPrivate->IoPort->Alt.DeviceControl,\r
2245 DEFAULT_CTL\r
2246 );\r
2247\r
2248 //\r
2249 // Send Packet command to inform device\r
2250 // that the following data bytes are command packet.\r
2251 //\r
2252 WritePortB (\r
2253 AtapiScsiPrivate->PciIo,\r
2254 AtapiScsiPrivate->IoPort->Reg.Command,\r
2255 PACKET_CMD\r
2256 );\r
2257\r
2258 //\r
2259 // Before data transfer, BSY should be 0 and DRQ should be 1.\r
2260 // if they are not in specified time frame,\r
2261 // retrieve Sense Key from Error Register before return.\r
2262 //\r
2263 Status = StatusDRQReady (AtapiScsiPrivate, TimeoutInMicroSeconds);\r
2264 if (EFI_ERROR (Status)) {\r
2265 if (Status == EFI_ABORTED) {\r
2266 Status = EFI_DEVICE_ERROR;\r
2267 }\r
2268\r
2269 *ByteCount = 0;\r
2270 return Status;\r
2271 }\r
2272\r
2273 //\r
2274 // Send out command packet\r
2275 //\r
2276 CommandIndex = (UINT16 *) PacketCommand;\r
2277 for (Count = 0; Count < 6; Count++, CommandIndex++) {\r
2278 WritePortW (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->Data, *CommandIndex);\r
2279 }\r
2280\r
2281 //\r
2282 // call AtapiPassThruPioReadWriteData() function to get\r
2283 // requested transfer data form device.\r
2284 //\r
2285 return AtapiPassThruPioReadWriteData (\r
2286 AtapiScsiPrivate,\r
2287 Buffer,\r
2288 ByteCount,\r
2289 Direction,\r
2290 TimeoutInMicroSeconds\r
2291 );\r
2292}\r
2293\r
2294EFI_STATUS\r
2295AtapiPassThruPioReadWriteData (\r
2296 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,\r
2297 UINT16 *Buffer,\r
2298 UINT32 *ByteCount,\r
2299 DATA_DIRECTION Direction,\r
2300 UINT64 TimeoutInMicroSeconds\r
2301 )\r
2302/*++\r
2303\r
2304Routine Description:\r
2305\r
2306 Performs data transfer between ATAPI device and host after the\r
2307 ATAPI command packet is sent.\r
6a6d955c 2308\r
823f7d4f 2309Arguments:\r
2310\r
6a6d955c 2311 AtapiScsiPrivate: Private data structure for the specified channel.\r
823f7d4f 2312 Buffer: Points to the transferred data.\r
2313 ByteCount: When input,indicates the buffer size; when output,\r
2314 indicates the actually transferred data size.\r
6a6d955c 2315 Direction: Indicates the data transfer direction.\r
823f7d4f 2316 TimeoutInMicroSeconds:\r
6a6d955c 2317 The timeout, in micro second units, to use for the\r
823f7d4f 2318 execution of this ATAPI command.\r
6a6d955c 2319 A TimeoutInMicroSeconds value of 0 means that\r
2320 this function will wait indefinitely for the ATAPI\r
823f7d4f 2321 command to execute.\r
6a6d955c 2322 If TimeoutInMicroSeconds is greater than zero, then\r
2323 this function will return EFI_TIMEOUT if the time\r
2324 required to execute the ATAPI command is greater\r
823f7d4f 2325 than TimeoutInMicroSeconds.\r
2326 Returns:\r
2327\r
2328 EFI_STATUS\r
6a6d955c 2329\r
823f7d4f 2330--*/\r
2331{\r
2332 UINT32 Index;\r
2333 UINT32 RequiredWordCount;\r
2334 UINT32 ActualWordCount;\r
2335 UINT32 WordCount;\r
2336 EFI_STATUS Status;\r
2337 UINT16 *ptrBuffer;\r
2338\r
2339 Status = EFI_SUCCESS;\r
2340\r
2341 //\r
2342 // Non Data transfer request is also supported.\r
2343 //\r
2344 if (*ByteCount == 0 || Buffer == NULL) {\r
2345 *ByteCount = 0;\r
2346 if (EFI_ERROR (StatusWaitForBSYClear (AtapiScsiPrivate, TimeoutInMicroSeconds))) {\r
2347 return EFI_DEVICE_ERROR;\r
2348 }\r
2349 }\r
2350\r
2351 ptrBuffer = Buffer;\r
2352 RequiredWordCount = *ByteCount / 2;\r
2353\r
2354 //\r
2355 // ActuralWordCount means the word count of data really transfered.\r
2356 //\r
2357 ActualWordCount = 0;\r
2358\r
2359 while (ActualWordCount < RequiredWordCount) {\r
2360 //\r
2361 // before each data transfer stream, the host should poll DRQ bit ready,\r
2362 // which indicates device's ready for data transfer .\r
2363 //\r
2364 Status = StatusDRQReady (AtapiScsiPrivate, TimeoutInMicroSeconds);\r
2365 if (EFI_ERROR (Status)) {\r
2366 *ByteCount = ActualWordCount * 2;\r
2367\r
2368 AtapiPassThruCheckErrorStatus (AtapiScsiPrivate);\r
2369\r
2370 if (ActualWordCount == 0) {\r
2371 return EFI_DEVICE_ERROR;\r
2372 }\r
2373 //\r
2374 // ActualWordCount > 0\r
2375 //\r
2376 if (ActualWordCount < RequiredWordCount) {\r
2377 return EFI_BAD_BUFFER_SIZE;\r
2378 }\r
2379 }\r
2380 //\r
2381 // get current data transfer size from Cylinder Registers.\r
2382 //\r
2383 WordCount = ReadPortB (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->CylinderMsb) << 8;\r
2384 WordCount = WordCount | ReadPortB (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->CylinderLsb);\r
2385 WordCount = WordCount & 0xffff;\r
2386 WordCount /= 2;\r
2387\r
2388 //\r
2389 // perform a series data In/Out.\r
2390 //\r
2391 for (Index = 0; (Index < WordCount) && (ActualWordCount < RequiredWordCount); Index++, ActualWordCount++) {\r
2392\r
2393 if (Direction == DataIn) {\r
2394\r
2395 *ptrBuffer = ReadPortW (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->Data);\r
2396 } else {\r
2397\r
2398 WritePortW (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->Data, *ptrBuffer);\r
2399 }\r
2400\r
2401 ptrBuffer++;\r
2402\r
2403 }\r
2404 }\r
2405 //\r
2406 // After data transfer is completed, normally, DRQ bit should clear.\r
2407 //\r
2408 StatusDRQClear (AtapiScsiPrivate, TimeoutInMicroSeconds);\r
2409\r
2410 //\r
2411 // read status register to check whether error happens.\r
2412 //\r
2413 Status = AtapiPassThruCheckErrorStatus (AtapiScsiPrivate);\r
2414\r
2415 *ByteCount = ActualWordCount * 2;\r
2416\r
2417 return Status;\r
2418}\r
2419\r
2420\r
2421UINT8\r
2422ReadPortB (\r
2423 IN EFI_PCI_IO_PROTOCOL *PciIo,\r
2424 IN UINT16 Port\r
2425 )\r
2426/*++\r
2427\r
2428Routine Description:\r
2429\r
2430 Read one byte from a specified I/O port.\r
2431\r
2432Arguments:\r
2433\r
2434 PciIo - The pointer of EFI_PCI_IO_PROTOCOL\r
2435 Port - IO port\r
6a6d955c 2436\r
823f7d4f 2437Returns:\r
2438\r
2439 A byte read out\r
2440\r
2441--*/\r
2442{\r
2443 UINT8 Data;\r
2444\r
2445 Data = 0;\r
2446 PciIo->Io.Read (\r
2447 PciIo,\r
2448 EfiPciIoWidthUint8,\r
2449 EFI_PCI_IO_PASS_THROUGH_BAR,\r
2450 (UINT64) Port,\r
2451 1,\r
2452 &Data\r
2453 );\r
2454 return Data;\r
2455}\r
2456\r
2457\r
2458UINT16\r
2459ReadPortW (\r
2460 IN EFI_PCI_IO_PROTOCOL *PciIo,\r
2461 IN UINT16 Port\r
2462 )\r
2463/*++\r
2464\r
2465Routine Description:\r
2466\r
2467 Read one word from a specified I/O port.\r
2468\r
2469Arguments:\r
2470\r
2471 PciIo - The pointer of EFI_PCI_IO_PROTOCOL\r
2472 Port - IO port\r
6a6d955c 2473\r
823f7d4f 2474Returns:\r
2475\r
2476 A word read out\r
2477--*/\r
2478{\r
2479 UINT16 Data;\r
2480\r
2481 Data = 0;\r
2482 PciIo->Io.Read (\r
2483 PciIo,\r
2484 EfiPciIoWidthUint16,\r
2485 EFI_PCI_IO_PASS_THROUGH_BAR,\r
2486 (UINT64) Port,\r
2487 1,\r
2488 &Data\r
2489 );\r
2490 return Data;\r
2491}\r
2492\r
2493\r
2494VOID\r
2495WritePortB (\r
2496 IN EFI_PCI_IO_PROTOCOL *PciIo,\r
2497 IN UINT16 Port,\r
2498 IN UINT8 Data\r
2499 )\r
2500/*++\r
2501\r
2502Routine Description:\r
2503\r
2504 Write one byte to a specified I/O port.\r
2505\r
2506Arguments:\r
2507\r
2508 PciIo - The pointer of EFI_PCI_IO_PROTOCOL\r
2509 Port - IO port\r
2510 Data - The data to write\r
6a6d955c 2511\r
823f7d4f 2512Returns:\r
2513\r
2514 NONE\r
2515\r
2516--*/\r
2517{\r
2518 PciIo->Io.Write (\r
2519 PciIo,\r
2520 EfiPciIoWidthUint8,\r
2521 EFI_PCI_IO_PASS_THROUGH_BAR,\r
2522 (UINT64) Port,\r
2523 1,\r
2524 &Data\r
2525 );\r
2526}\r
2527\r
2528\r
2529VOID\r
2530WritePortW (\r
2531 IN EFI_PCI_IO_PROTOCOL *PciIo,\r
2532 IN UINT16 Port,\r
2533 IN UINT16 Data\r
2534 )\r
2535/*++\r
2536\r
2537Routine Description:\r
2538\r
2539 Write one word to a specified I/O port.\r
2540\r
2541Arguments:\r
2542\r
2543 PciIo - The pointer of EFI_PCI_IO_PROTOCOL\r
2544 Port - IO port\r
2545 Data - The data to write\r
6a6d955c 2546\r
823f7d4f 2547Returns:\r
2548\r
2549 NONE\r
6a6d955c 2550\r
823f7d4f 2551--*/\r
2552{\r
2553 PciIo->Io.Write (\r
2554 PciIo,\r
2555 EfiPciIoWidthUint16,\r
2556 EFI_PCI_IO_PASS_THROUGH_BAR,\r
2557 (UINT64) Port,\r
2558 1,\r
2559 &Data\r
2560 );\r
2561}\r
2562\r
2563EFI_STATUS\r
2564StatusDRQClear (\r
2565 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,\r
2566 UINT64 TimeoutInMicroSeconds\r
2567 )\r
2568/*++\r
2569\r
2570Routine Description:\r
2571\r
2572 Check whether DRQ is clear in the Status Register. (BSY must also be cleared)\r
2573 If TimeoutInMicroSeconds is zero, this routine should wait infinitely for\r
6a6d955c 2574 DRQ clear. Otherwise, it will return EFI_TIMEOUT when specified time is\r
823f7d4f 2575 elapsed.\r
2576\r
2577Arguments:\r
2578\r
2579 AtapiScsiPrivate - The pointer of ATAPI_SCSI_PASS_THRU_DEV\r
2580 TimeoutInMicroSeconds - The time to wait for\r
6a6d955c 2581\r
823f7d4f 2582Returns:\r
2583\r
2584 EFI_STATUS\r
6a6d955c 2585\r
823f7d4f 2586--*/\r
2587{\r
2588 UINT64 Delay;\r
2589 UINT8 StatusRegister;\r
2590 UINT8 ErrRegister;\r
2591\r
2592 if (TimeoutInMicroSeconds == 0) {\r
2593 Delay = 2;\r
2594 } else {\r
2595 Delay = DivU64x32 (TimeoutInMicroSeconds, (UINT32) 30) + 1;\r
2596 }\r
2597\r
2598 do {\r
2599\r
2600 StatusRegister = ReadPortB (\r
2601 AtapiScsiPrivate->PciIo,\r
2602 AtapiScsiPrivate->IoPort->Reg.Status\r
2603 );\r
2604\r
2605 //\r
2606 // wait for BSY == 0 and DRQ == 0\r
2607 //\r
2608 if ((StatusRegister & (DRQ | BSY)) == 0) {\r
2609 break;\r
2610 }\r
2611 //\r
2612 // check whether the command is aborted by the device\r
2613 //\r
2614 if ((StatusRegister & (BSY | ERR)) == ERR) {\r
2615\r
2616 ErrRegister = ReadPortB (\r
2617 AtapiScsiPrivate->PciIo,\r
2618 AtapiScsiPrivate->IoPort->Reg1.Error\r
2619 );\r
2620 if ((ErrRegister & ABRT_ERR) == ABRT_ERR) {\r
2621\r
2622 return EFI_ABORTED;\r
2623 }\r
2624 }\r
2625 //\r
2626 // Stall for 30 us\r
2627 //\r
2628 gBS->Stall (30);\r
2629\r
2630 //\r
2631 // Loop infinitely if not meeting expected condition\r
2632 //\r
2633 if (TimeoutInMicroSeconds == 0) {\r
2634 Delay = 2;\r
2635 }\r
2636\r
2637 Delay--;\r
2638 } while (Delay);\r
2639\r
2640 if (Delay == 0) {\r
2641 return EFI_TIMEOUT;\r
2642 }\r
2643\r
2644 return EFI_SUCCESS;\r
2645}\r
2646\r
2647EFI_STATUS\r
2648AltStatusDRQClear (\r
2649 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,\r
2650 UINT64 TimeoutInMicroSeconds\r
2651 )\r
2652/*++\r
2653\r
2654Routine Description:\r
2655\r
6a6d955c 2656 Check whether DRQ is clear in the Alternate Status Register.\r
2657 (BSY must also be cleared).If TimeoutInMicroSeconds is zero, this routine should\r
2658 wait infinitely for DRQ clear. Otherwise, it will return EFI_TIMEOUT when specified time is\r
823f7d4f 2659 elapsed.\r
2660\r
2661Arguments:\r
2662\r
2663 AtapiScsiPrivate - The pointer of ATAPI_SCSI_PASS_THRU_DEV\r
2664 TimeoutInMicroSeconds - The time to wait for\r
6a6d955c 2665\r
823f7d4f 2666Returns:\r
2667\r
2668 EFI_STATUS\r
2669\r
2670--*/\r
2671{\r
2672 UINT64 Delay;\r
2673 UINT8 AltStatusRegister;\r
2674 UINT8 ErrRegister;\r
2675\r
2676 if (TimeoutInMicroSeconds == 0) {\r
2677 Delay = 2;\r
2678 } else {\r
2679 Delay = DivU64x32 (TimeoutInMicroSeconds, (UINT32) 30) + 1;\r
2680 }\r
2681\r
2682 do {\r
2683\r
2684 AltStatusRegister = ReadPortB (\r
2685 AtapiScsiPrivate->PciIo,\r
2686 AtapiScsiPrivate->IoPort->Alt.AltStatus\r
2687 );\r
2688\r
2689 //\r
2690 // wait for BSY == 0 and DRQ == 0\r
2691 //\r
2692 if ((AltStatusRegister & (DRQ | BSY)) == 0) {\r
2693 break;\r
2694 }\r
2695\r
2696 if ((AltStatusRegister & (BSY | ERR)) == ERR) {\r
2697\r
2698 ErrRegister = ReadPortB (\r
2699 AtapiScsiPrivate->PciIo,\r
2700 AtapiScsiPrivate->IoPort->Reg1.Error\r
2701 );\r
2702 if ((ErrRegister & ABRT_ERR) == ABRT_ERR) {\r
2703\r
2704 return EFI_ABORTED;\r
2705 }\r
2706 }\r
2707 //\r
2708 // Stall for 30 us\r
2709 //\r
2710 gBS->Stall (30);\r
2711\r
2712 //\r
2713 // Loop infinitely if not meeting expected condition\r
2714 //\r
2715 if (TimeoutInMicroSeconds == 0) {\r
2716 Delay = 2;\r
2717 }\r
2718\r
2719 Delay--;\r
2720 } while (Delay);\r
2721\r
2722 if (Delay == 0) {\r
2723 return EFI_TIMEOUT;\r
2724 }\r
2725\r
2726 return EFI_SUCCESS;\r
2727}\r
2728\r
2729EFI_STATUS\r
2730StatusDRQReady (\r
2731 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,\r
2732 UINT64 TimeoutInMicroSeconds\r
2733 )\r
2734/*++\r
2735\r
2736Routine Description:\r
2737\r
2738 Check whether DRQ is ready in the Status Register. (BSY must also be cleared)\r
2739 If TimeoutInMicroSeconds is zero, this routine should wait infinitely for\r
6a6d955c 2740 DRQ ready. Otherwise, it will return EFI_TIMEOUT when specified time is\r
823f7d4f 2741 elapsed.\r
2742\r
2743Arguments:\r
2744\r
2745 AtapiScsiPrivate - The pointer of ATAPI_SCSI_PASS_THRU_DEV\r
2746 TimeoutInMicroSeconds - The time to wait for\r
6a6d955c 2747\r
823f7d4f 2748Returns:\r
2749\r
2750 EFI_STATUS\r
2751\r
2752--*/\r
2753{\r
2754 UINT64 Delay;\r
2755 UINT8 StatusRegister;\r
2756 UINT8 ErrRegister;\r
2757\r
2758 if (TimeoutInMicroSeconds == 0) {\r
2759 Delay = 2;\r
2760 } else {\r
2761 Delay = DivU64x32 (TimeoutInMicroSeconds, (UINT32) 30) + 1;\r
2762 }\r
2763\r
2764 do {\r
2765 //\r
2766 // read Status Register will clear interrupt\r
2767 //\r
2768 StatusRegister = ReadPortB (\r
2769 AtapiScsiPrivate->PciIo,\r
2770 AtapiScsiPrivate->IoPort->Reg.Status\r
2771 );\r
2772\r
2773 //\r
2774 // BSY==0,DRQ==1\r
2775 //\r
2776 if ((StatusRegister & (BSY | DRQ)) == DRQ) {\r
2777 break;\r
2778 }\r
2779\r
2780 if ((StatusRegister & (BSY | ERR)) == ERR) {\r
2781\r
2782 ErrRegister = ReadPortB (\r
2783 AtapiScsiPrivate->PciIo,\r
2784 AtapiScsiPrivate->IoPort->Reg1.Error\r
2785 );\r
2786 if ((ErrRegister & ABRT_ERR) == ABRT_ERR) {\r
2787 return EFI_ABORTED;\r
2788 }\r
2789 }\r
2790\r
2791 //\r
2792 // Stall for 30 us\r
2793 //\r
2794 gBS->Stall (30);\r
2795\r
2796 //\r
2797 // Loop infinitely if not meeting expected condition\r
2798 //\r
2799 if (TimeoutInMicroSeconds == 0) {\r
2800 Delay = 2;\r
2801 }\r
2802\r
2803 Delay--;\r
2804 } while (Delay);\r
2805\r
2806 if (Delay == 0) {\r
2807 return EFI_TIMEOUT;\r
2808 }\r
2809\r
2810 return EFI_SUCCESS;\r
2811}\r
2812\r
2813EFI_STATUS\r
2814AltStatusDRQReady (\r
2815 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,\r
2816 UINT64 TimeoutInMicroSeconds\r
2817 )\r
2818/*++\r
2819\r
2820Routine Description:\r
2821\r
6a6d955c 2822 Check whether DRQ is ready in the Alternate Status Register.\r
823f7d4f 2823 (BSY must also be cleared)\r
2824 If TimeoutInMicroSeconds is zero, this routine should wait infinitely for\r
6a6d955c 2825 DRQ ready. Otherwise, it will return EFI_TIMEOUT when specified time is\r
823f7d4f 2826 elapsed.\r
2827\r
2828Arguments:\r
2829\r
2830 AtapiScsiPrivate - The pointer of ATAPI_SCSI_PASS_THRU_DEV\r
2831 TimeoutInMicroSeconds - The time to wait for\r
6a6d955c 2832\r
823f7d4f 2833Returns:\r
2834\r
2835 EFI_STATUS\r
2836\r
2837--*/\r
2838{\r
2839 UINT64 Delay;\r
2840 UINT8 AltStatusRegister;\r
2841 UINT8 ErrRegister;\r
2842\r
2843 if (TimeoutInMicroSeconds == 0) {\r
2844 Delay = 2;\r
2845 } else {\r
2846 Delay = DivU64x32 (TimeoutInMicroSeconds, (UINT32) 30) + 1;\r
2847 }\r
2848\r
2849 do {\r
2850 //\r
2851 // read Status Register will clear interrupt\r
2852 //\r
2853 AltStatusRegister = ReadPortB (\r
2854 AtapiScsiPrivate->PciIo,\r
2855 AtapiScsiPrivate->IoPort->Alt.AltStatus\r
2856 );\r
2857 //\r
2858 // BSY==0,DRQ==1\r
2859 //\r
2860 if ((AltStatusRegister & (BSY | DRQ)) == DRQ) {\r
2861 break;\r
2862 }\r
2863\r
2864 if ((AltStatusRegister & (BSY | ERR)) == ERR) {\r
2865\r
2866 ErrRegister = ReadPortB (\r
2867 AtapiScsiPrivate->PciIo,\r
2868 AtapiScsiPrivate->IoPort->Reg1.Error\r
2869 );\r
2870 if ((ErrRegister & ABRT_ERR) == ABRT_ERR) {\r
2871 return EFI_ABORTED;\r
2872 }\r
2873 }\r
2874\r
2875 //\r
2876 // Stall for 30 us\r
2877 //\r
2878 gBS->Stall (30);\r
2879\r
2880 //\r
2881 // Loop infinitely if not meeting expected condition\r
2882 //\r
2883 if (TimeoutInMicroSeconds == 0) {\r
2884 Delay = 2;\r
2885 }\r
2886\r
2887 Delay--;\r
2888 } while (Delay);\r
2889\r
2890 if (Delay == 0) {\r
2891 return EFI_TIMEOUT;\r
2892 }\r
2893\r
2894 return EFI_SUCCESS;\r
2895}\r
2896\r
2897EFI_STATUS\r
2898StatusWaitForBSYClear (\r
2899 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,\r
2900 UINT64 TimeoutInMicroSeconds\r
2901 )\r
2902/*++\r
2903\r
2904Routine Description:\r
2905\r
2906 Check whether BSY is clear in the Status Register.\r
2907 If TimeoutInMicroSeconds is zero, this routine should wait infinitely for\r
6a6d955c 2908 BSY clear. Otherwise, it will return EFI_TIMEOUT when specified time is\r
823f7d4f 2909 elapsed.\r
2910\r
2911Arguments:\r
2912\r
2913 AtapiScsiPrivate - The pointer of ATAPI_SCSI_PASS_THRU_DEV\r
2914 TimeoutInMicroSeconds - The time to wait for\r
6a6d955c 2915\r
823f7d4f 2916Returns:\r
2917\r
2918 EFI_STATUS\r
2919\r
2920--*/\r
2921{\r
2922 UINT64 Delay;\r
2923 UINT8 StatusRegister;\r
2924\r
2925 if (TimeoutInMicroSeconds == 0) {\r
2926 Delay = 2;\r
2927 } else {\r
2928 Delay = DivU64x32 (TimeoutInMicroSeconds, (UINT32) 30) + 1;\r
2929 }\r
2930\r
2931 do {\r
2932\r
2933 StatusRegister = ReadPortB (\r
2934 AtapiScsiPrivate->PciIo,\r
2935 AtapiScsiPrivate->IoPort->Reg.Status\r
2936 );\r
2937 if ((StatusRegister & BSY) == 0x00) {\r
2938 break;\r
2939 }\r
2940\r
2941 //\r
2942 // Stall for 30 us\r
2943 //\r
2944 gBS->Stall (30);\r
2945\r
2946 //\r
2947 // Loop infinitely if not meeting expected condition\r
2948 //\r
2949 if (TimeoutInMicroSeconds == 0) {\r
2950 Delay = 2;\r
2951 }\r
2952\r
2953 Delay--;\r
2954 } while (Delay);\r
2955\r
2956 if (Delay == 0) {\r
2957 return EFI_TIMEOUT;\r
2958 }\r
2959\r
2960 return EFI_SUCCESS;\r
2961}\r
2962\r
2963EFI_STATUS\r
2964AltStatusWaitForBSYClear (\r
2965 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,\r
2966 UINT64 TimeoutInMicroSeconds\r
2967 )\r
2968/*++\r
2969\r
2970Routine Description:\r
2971\r
2972 Check whether BSY is clear in the Alternate Status Register.\r
2973 If TimeoutInMicroSeconds is zero, this routine should wait infinitely for\r
6a6d955c 2974 BSY clear. Otherwise, it will return EFI_TIMEOUT when specified time is\r
823f7d4f 2975 elapsed.\r
2976\r
2977Arguments:\r
2978\r
2979 AtapiScsiPrivate - The pointer of ATAPI_SCSI_PASS_THRU_DEV\r
2980 TimeoutInMicroSeconds - The time to wait for\r
6a6d955c 2981\r
823f7d4f 2982Returns:\r
2983\r
2984 EFI_STATUS\r
2985\r
2986--*/\r
2987{\r
2988 UINT64 Delay;\r
2989 UINT8 AltStatusRegister;\r
2990\r
2991 if (TimeoutInMicroSeconds == 0) {\r
2992 Delay = 2;\r
2993 } else {\r
2994 Delay = DivU64x32 (TimeoutInMicroSeconds, (UINT32) 30) + 1;\r
2995 }\r
2996\r
2997 do {\r
2998\r
2999 AltStatusRegister = ReadPortB (\r
3000 AtapiScsiPrivate->PciIo,\r
3001 AtapiScsiPrivate->IoPort->Alt.AltStatus\r
3002 );\r
3003 if ((AltStatusRegister & BSY) == 0x00) {\r
3004 break;\r
3005 }\r
3006\r
3007 //\r
3008 // Stall for 30 us\r
3009 //\r
3010 gBS->Stall (30);\r
3011 //\r
3012 // Loop infinitely if not meeting expected condition\r
3013 //\r
3014 if (TimeoutInMicroSeconds == 0) {\r
3015 Delay = 2;\r
3016 }\r
3017\r
3018 Delay--;\r
3019 } while (Delay);\r
3020\r
3021 if (Delay == 0) {\r
3022 return EFI_TIMEOUT;\r
3023 }\r
3024\r
3025 return EFI_SUCCESS;\r
3026}\r
3027\r
3028EFI_STATUS\r
3029StatusDRDYReady (\r
3030 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,\r
3031 UINT64 TimeoutInMicroSeconds\r
3032 )\r
3033/*++\r
3034\r
3035Routine Description:\r
3036\r
6a6d955c 3037 Check whether DRDY is ready in the Status Register.\r
823f7d4f 3038 (BSY must also be cleared)\r
3039 If TimeoutInMicroSeconds is zero, this routine should wait infinitely for\r
6a6d955c 3040 DRDY ready. Otherwise, it will return EFI_TIMEOUT when specified time is\r
823f7d4f 3041 elapsed.\r
3042\r
3043Arguments:\r
3044\r
3045 AtapiScsiPrivate - The pointer of ATAPI_SCSI_PASS_THRU_DEV\r
3046 TimeoutInMicroSeconds - The time to wait for\r
6a6d955c 3047\r
823f7d4f 3048Returns:\r
3049\r
3050 EFI_STATUS\r
3051\r
3052--*/\r
3053{\r
3054 UINT64 Delay;\r
3055 UINT8 StatusRegister;\r
3056 UINT8 ErrRegister;\r
3057\r
3058 if (TimeoutInMicroSeconds == 0) {\r
3059 Delay = 2;\r
3060 } else {\r
3061 Delay = DivU64x32 (TimeoutInMicroSeconds, (UINT32) 30) + 1;\r
3062 }\r
3063\r
3064 do {\r
3065 StatusRegister = ReadPortB (\r
3066 AtapiScsiPrivate->PciIo,\r
3067 AtapiScsiPrivate->IoPort->Reg.Status\r
3068 );\r
3069 //\r
3070 // BSY == 0 , DRDY == 1\r
3071 //\r
3072 if ((StatusRegister & (DRDY | BSY)) == DRDY) {\r
3073 break;\r
3074 }\r
3075\r
3076 if ((StatusRegister & (BSY | ERR)) == ERR) {\r
3077\r
3078 ErrRegister = ReadPortB (\r
3079 AtapiScsiPrivate->PciIo,\r
3080 AtapiScsiPrivate->IoPort->Reg1.Error\r
3081 );\r
3082 if ((ErrRegister & ABRT_ERR) == ABRT_ERR) {\r
3083 return EFI_ABORTED;\r
3084 }\r
3085 }\r
3086\r
3087 //\r
3088 // Stall for 30 us\r
3089 //\r
3090 gBS->Stall (30);\r
3091 //\r
3092 // Loop infinitely if not meeting expected condition\r
3093 //\r
3094 if (TimeoutInMicroSeconds == 0) {\r
3095 Delay = 2;\r
3096 }\r
3097\r
3098 Delay--;\r
3099 } while (Delay);\r
3100\r
3101 if (Delay == 0) {\r
3102 return EFI_TIMEOUT;\r
3103 }\r
3104\r
3105 return EFI_SUCCESS;\r
3106}\r
3107\r
3108EFI_STATUS\r
3109AltStatusDRDYReady (\r
3110 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,\r
3111 UINT64 TimeoutInMicroSeconds\r
3112 )\r
3113/*++\r
3114\r
3115Routine Description:\r
3116\r
6a6d955c 3117 Check whether DRDY is ready in the Alternate Status Register.\r
823f7d4f 3118 (BSY must also be cleared)\r
3119 If TimeoutInMicroSeconds is zero, this routine should wait infinitely for\r
6a6d955c 3120 DRDY ready. Otherwise, it will return EFI_TIMEOUT when specified time is\r
823f7d4f 3121 elapsed.\r
3122\r
3123Arguments:\r
3124\r
3125 AtapiScsiPrivate - The pointer of ATAPI_SCSI_PASS_THRU_DEV\r
3126 TimeoutInMicroSeconds - The time to wait for\r
6a6d955c 3127\r
823f7d4f 3128Returns:\r
3129\r
3130 EFI_STATUS\r
3131\r
3132--*/\r
3133{\r
3134 UINT64 Delay;\r
3135 UINT8 AltStatusRegister;\r
3136 UINT8 ErrRegister;\r
3137\r
3138 if (TimeoutInMicroSeconds == 0) {\r
3139 Delay = 2;\r
3140 } else {\r
3141 Delay = DivU64x32 (TimeoutInMicroSeconds, (UINT32) 30) + 1;\r
3142 }\r
3143\r
3144 do {\r
3145 AltStatusRegister = ReadPortB (\r
3146 AtapiScsiPrivate->PciIo,\r
3147 AtapiScsiPrivate->IoPort->Alt.AltStatus\r
3148 );\r
3149 //\r
3150 // BSY == 0 , DRDY == 1\r
3151 //\r
3152 if ((AltStatusRegister & (DRDY | BSY)) == DRDY) {\r
3153 break;\r
3154 }\r
3155\r
3156 if ((AltStatusRegister & (BSY | ERR)) == ERR) {\r
3157\r
3158 ErrRegister = ReadPortB (\r
3159 AtapiScsiPrivate->PciIo,\r
3160 AtapiScsiPrivate->IoPort->Reg1.Error\r
3161 );\r
3162 if ((ErrRegister & ABRT_ERR) == ABRT_ERR) {\r
3163 return EFI_ABORTED;\r
3164 }\r
3165 }\r
3166\r
3167 //\r
3168 // Stall for 30 us\r
3169 //\r
3170 gBS->Stall (30);\r
3171 //\r
3172 // Loop infinitely if not meeting expected condition\r
3173 //\r
3174 if (TimeoutInMicroSeconds == 0) {\r
3175 Delay = 2;\r
3176 }\r
3177\r
3178 Delay--;\r
3179 } while (Delay);\r
3180\r