minimum updating.
[edk2.git] / OptionRomPkg / AtapiPassThruDxe / AtapiPassThru.c
blob43b18113d255ed236c0a931eaa2ade274cd03789
1 /** @file
2 Copyright (c) 2006, Intel Corporation
3 All rights reserved. This program and the accompanying materials
4 are licensed and made available under the terms and conditions of the BSD License
5 which accompanies this distribution. The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.php
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
11 **/
13 #include "AtapiPassThru.h"
16 SCSI_COMMAND_SET gEndTable = { 0xff, (DATA_DIRECTION) 0xff };
18 ///
19 /// This table contains all the supported ATAPI commands.
20 ///
21 SCSI_COMMAND_SET gSupportedATAPICommands[] = {
22 { OP_INQUIRY, DataIn },
23 { OP_LOAD_UNLOAD_CD, NoData },
24 { OP_MECHANISM_STATUS, DataIn },
25 { OP_MODE_SELECT_10, DataOut },
26 { OP_MODE_SENSE_10, DataIn },
27 { OP_PAUSE_RESUME, NoData },
28 { OP_PLAY_AUDIO_10, DataIn },
29 { OP_PLAY_AUDIO_MSF, DataIn },
30 { OP_PLAY_CD, DataIn },
31 { OP_PLAY_CD_MSF, DataIn },
32 { OP_PREVENT_ALLOW_MEDIUM_REMOVAL,NoData },
33 { OP_READ_10, DataIn },
34 { OP_READ_12, DataIn },
35 { OP_READ_CAPACITY, DataIn },
36 { OP_READ_CD, DataIn },
37 { OP_READ_CD_MSF, DataIn },
38 { OP_READ_HEADER, DataIn },
39 { OP_READ_SUB_CHANNEL, DataIn },
40 { OP_READ_TOC, DataIn },
41 { OP_REQUEST_SENSE, DataIn },
42 { OP_SCAN, NoData },
43 { OP_SEEK_10, NoData },
44 { OP_SET_CD_SPEED, DataOut },
45 { OP_STOPPLAY_SCAN, NoData },
46 { OP_START_STOP_UNIT, NoData },
47 { OP_TEST_UNIT_READY, NoData },
48 { OP_FORMAT_UNIT, DataOut },
49 { OP_READ_FORMAT_CAPACITIES, DataIn },
50 { OP_VERIFY, DataOut },
51 { OP_WRITE_10, DataOut },
52 { OP_WRITE_12, DataOut },
53 { OP_WRITE_AND_VERIFY, DataOut },
54 { 0xff, (DATA_DIRECTION) 0xff }
57 GLOBAL_REMOVE_IF_UNREFERENCED EFI_SCSI_PASS_THRU_MODE gScsiPassThruMode = {
58 L"ATAPI Controller",
59 L"ATAPI Channel",
61 EFI_SCSI_PASS_THRU_ATTRIBUTES_PHYSICAL | EFI_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL,
65 GLOBAL_REMOVE_IF_UNREFERENCED EFI_SCSI_PASS_THRU_PROTOCOL gScsiPassThruProtocolTemplate = {
66 &gScsiPassThruMode,
67 AtapiScsiPassThruFunction,
68 AtapiScsiPassThruGetNextDevice,
69 AtapiScsiPassThruBuildDevicePath,
70 AtapiScsiPassThruGetTargetLun,
71 AtapiScsiPassThruResetChannel,
72 AtapiScsiPassThruResetTarget
75 GLOBAL_REMOVE_IF_UNREFERENCED EFI_EXT_SCSI_PASS_THRU_MODE gExtScsiPassThruMode = {
77 EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_PHYSICAL | EFI_EXT_SCSI_PASS_THRU_ATTRIBUTES_LOGICAL,
81 GLOBAL_REMOVE_IF_UNREFERENCED EFI_EXT_SCSI_PASS_THRU_PROTOCOL gExtScsiPassThruProtocolTemplate = {
82 &gExtScsiPassThruMode,
83 AtapiExtScsiPassThruFunction,
84 AtapiExtScsiPassThruGetNextTargetLun,
85 AtapiExtScsiPassThruBuildDevicePath,
86 AtapiExtScsiPassThruGetTargetLun,
87 AtapiExtScsiPassThruResetChannel,
88 AtapiExtScsiPassThruResetTarget,
89 AtapiExtScsiPassThruGetNextTarget
92 EFI_DRIVER_BINDING_PROTOCOL gAtapiScsiPassThruDriverBinding = {
93 AtapiScsiPassThruDriverBindingSupported,
94 AtapiScsiPassThruDriverBindingStart,
95 AtapiScsiPassThruDriverBindingStop,
96 0x10,
97 NULL,
98 NULL
101 EFI_STATUS
102 EFIAPI
103 AtapiScsiPassThruDriverBindingSupported (
104 IN EFI_DRIVER_BINDING_PROTOCOL *This,
105 IN EFI_HANDLE Controller,
106 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
108 /*++
110 Routine Description:
111 Test to see if this driver supports ControllerHandle. Any ControllerHandle
112 that has gEfiPciIoProtocolGuid installed and is IDE Controller it will be supported.
114 Arguments:
116 This - Protocol instance pointer.
117 Controller - Handle of device to test
118 RemainingDevicePath - Not used
120 Returns:
121 EFI_STATUS
123 --*/
125 EFI_STATUS Status;
126 EFI_PCI_IO_PROTOCOL *PciIo;
127 PCI_TYPE00 Pci;
131 // Open the IO Abstraction(s) needed to perform the supported test
133 Status = gBS->OpenProtocol (
134 Controller,
135 &gEfiPciIoProtocolGuid,
136 (VOID **) &PciIo,
137 This->DriverBindingHandle,
138 Controller,
139 EFI_OPEN_PROTOCOL_BY_DRIVER
141 if (EFI_ERROR (Status)) {
142 return Status;
145 // Use the PCI I/O Protocol to see if Controller is a IDE Controller that
146 // can be managed by this driver. Read the PCI Configuration Header
147 // for this device.
149 Status = PciIo->Pci.Read (
150 PciIo,
151 EfiPciIoWidthUint32,
153 sizeof (Pci) / sizeof (UINT32),
154 &Pci
156 if (EFI_ERROR (Status)) {
157 gBS->CloseProtocol (
158 Controller,
159 &gEfiPciIoProtocolGuid,
160 This->DriverBindingHandle,
161 Controller
163 return EFI_UNSUPPORTED;
166 if (Pci.Hdr.ClassCode[2] != PCI_CLASS_MASS_STORAGE || Pci.Hdr.ClassCode[1] != PCI_CLASS_MASS_STORAGE_IDE) {
168 Status = EFI_UNSUPPORTED;
171 gBS->CloseProtocol (
172 Controller,
173 &gEfiPciIoProtocolGuid,
174 This->DriverBindingHandle,
175 Controller
178 return Status;
181 EFI_STATUS
182 EFIAPI
183 AtapiScsiPassThruDriverBindingStart (
184 IN EFI_DRIVER_BINDING_PROTOCOL *This,
185 IN EFI_HANDLE Controller,
186 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath
188 /*++
190 Routine Description:
191 Create handles for IDE channels specified by RemainingDevicePath.
192 Install SCSI Pass Thru Protocol onto each created handle.
194 Arguments:
196 This - Protocol instance pointer.
197 Controller - Handle of device to test
198 RemainingDevicePath - Not used
200 Returns:
201 EFI_STATUS
203 --*/
205 EFI_STATUS Status;
206 EFI_PCI_IO_PROTOCOL *PciIo;
207 UINT64 Supports;
208 UINT64 OriginalPciAttributes;
209 BOOLEAN PciAttributesSaved;
211 PciIo = NULL;
212 Status = gBS->OpenProtocol (
213 Controller,
214 &gEfiPciIoProtocolGuid,
215 (VOID **) &PciIo,
216 This->DriverBindingHandle,
217 Controller,
218 EFI_OPEN_PROTOCOL_BY_DRIVER
220 if (EFI_ERROR (Status)) {
221 return Status;
224 PciAttributesSaved = FALSE;
226 // Save original PCI attributes
228 Status = PciIo->Attributes (
229 PciIo,
230 EfiPciIoAttributeOperationGet,
232 &OriginalPciAttributes
235 if (EFI_ERROR (Status)) {
236 goto Done;
238 PciAttributesSaved = TRUE;
240 Status = PciIo->Attributes (
241 PciIo,
242 EfiPciIoAttributeOperationSupported,
244 &Supports
246 if (!EFI_ERROR (Status)) {
247 Supports &= (EFI_PCI_DEVICE_ENABLE |
248 EFI_PCI_IO_ATTRIBUTE_IDE_PRIMARY_IO |
249 EFI_PCI_IO_ATTRIBUTE_IDE_SECONDARY_IO);
250 Status = PciIo->Attributes (
251 PciIo,
252 EfiPciIoAttributeOperationEnable,
253 Supports,
254 NULL
257 if (EFI_ERROR (Status)) {
258 goto Done;
262 // Create SCSI Pass Thru instance for the IDE channel.
264 Status = RegisterAtapiScsiPassThru (This, Controller, PciIo, OriginalPciAttributes);
266 Done:
267 if (EFI_ERROR (Status)) {
268 if (PciAttributesSaved == TRUE) {
270 // Restore original PCI attributes
272 PciIo->Attributes (
273 PciIo,
274 EfiPciIoAttributeOperationSet,
275 OriginalPciAttributes,
276 NULL
280 gBS->CloseProtocol (
281 Controller,
282 &gEfiPciIoProtocolGuid,
283 This->DriverBindingHandle,
284 Controller
288 return Status;
291 EFI_STATUS
292 EFIAPI
293 AtapiScsiPassThruDriverBindingStop (
294 IN EFI_DRIVER_BINDING_PROTOCOL *This,
295 IN EFI_HANDLE Controller,
296 IN UINTN NumberOfChildren,
297 IN EFI_HANDLE *ChildHandleBuffer
299 /*++
301 Routine Description:
303 Stop this driver on ControllerHandle. Support stoping any child handles
304 created by this driver.
306 Arguments:
308 This - Protocol instance pointer.
309 Controller - Handle of device to stop driver on
310 NumberOfChildren - Number of Children in the ChildHandleBuffer
311 ChildHandleBuffer - List of handles for the children we need to stop.
313 Returns:
315 EFI_STATUS
317 --*/
319 EFI_STATUS Status;
320 EFI_SCSI_PASS_THRU_PROTOCOL *ScsiPassThru;
321 EFI_EXT_SCSI_PASS_THRU_PROTOCOL *ExtScsiPassThru;
322 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate;
324 if (FeaturePcdGet (PcdSupportScsiPassThru)) {
325 Status = gBS->OpenProtocol (
326 Controller,
327 &gEfiScsiPassThruProtocolGuid,
328 (VOID **) &ScsiPassThru,
329 This->DriverBindingHandle,
330 Controller,
331 EFI_OPEN_PROTOCOL_GET_PROTOCOL
333 if (EFI_ERROR (Status)) {
334 return Status;
336 AtapiScsiPrivate = ATAPI_SCSI_PASS_THRU_DEV_FROM_THIS (ScsiPassThru);
337 if (FeaturePcdGet (PcdSupportExtScsiPassThru)) {
338 Status = gBS->UninstallMultipleProtocolInterfaces (
339 Controller,
340 &gEfiScsiPassThruProtocolGuid,
341 &AtapiScsiPrivate->ScsiPassThru,
342 &gEfiExtScsiPassThruProtocolGuid,
343 &AtapiScsiPrivate->ExtScsiPassThru,
344 NULL
346 } else {
347 Status = gBS->UninstallMultipleProtocolInterfaces (
348 Controller,
349 &gEfiScsiPassThruProtocolGuid,
350 &AtapiScsiPrivate->ScsiPassThru,
351 NULL
354 } else {
355 Status = gBS->OpenProtocol (
356 Controller,
357 &gEfiExtScsiPassThruProtocolGuid,
358 (VOID **) &ExtScsiPassThru,
359 This->DriverBindingHandle,
360 Controller,
361 EFI_OPEN_PROTOCOL_GET_PROTOCOL
363 if (EFI_ERROR (Status)) {
364 return Status;
366 AtapiScsiPrivate = ATAPI_EXT_SCSI_PASS_THRU_DEV_FROM_THIS (ExtScsiPassThru);
367 Status = gBS->UninstallMultipleProtocolInterfaces (
368 Controller,
369 &gEfiExtScsiPassThruProtocolGuid,
370 &AtapiScsiPrivate->ExtScsiPassThru,
371 NULL
374 if (EFI_ERROR (Status)) {
375 return Status;
379 // Restore original PCI attributes
381 AtapiScsiPrivate->PciIo->Attributes (
382 AtapiScsiPrivate->PciIo,
383 EfiPciIoAttributeOperationSet,
384 AtapiScsiPrivate->OriginalPciAttributes,
385 NULL
388 gBS->CloseProtocol (
389 Controller,
390 &gEfiPciIoProtocolGuid,
391 This->DriverBindingHandle,
392 Controller
395 gBS->FreePool (AtapiScsiPrivate);
397 return EFI_SUCCESS;
400 EFI_STATUS
401 RegisterAtapiScsiPassThru (
402 IN EFI_DRIVER_BINDING_PROTOCOL *This,
403 IN EFI_HANDLE Controller,
404 IN EFI_PCI_IO_PROTOCOL *PciIo,
405 IN UINT64 OriginalPciAttributes
407 /*++
409 Routine Description:
410 Attaches SCSI Pass Thru Protocol for specified IDE channel.
412 Arguments:
413 This - Protocol instance pointer.
414 Controller - Parent device handle to the IDE channel.
415 PciIo - PCI I/O protocol attached on the "Controller".
417 Returns:
418 Always return EFI_SUCCESS unless installing SCSI Pass Thru Protocol failed.
420 --*/
422 EFI_STATUS Status;
423 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate;
424 IDE_REGISTERS_BASE_ADDR IdeRegsBaseAddr[ATAPI_MAX_CHANNEL];
426 AtapiScsiPrivate = AllocateZeroPool (sizeof (ATAPI_SCSI_PASS_THRU_DEV));
427 if (AtapiScsiPrivate == NULL) {
428 return EFI_OUT_OF_RESOURCES;
431 AtapiScsiPrivate->Signature = ATAPI_SCSI_PASS_THRU_DEV_SIGNATURE;
432 AtapiScsiPrivate->Handle = Controller;
435 // will reset the IoPort inside each API function.
437 AtapiScsiPrivate->IoPort = NULL;
438 AtapiScsiPrivate->PciIo = PciIo;
439 AtapiScsiPrivate->OriginalPciAttributes = OriginalPciAttributes;
442 // Obtain IDE IO port registers' base addresses
444 Status = GetIdeRegistersBaseAddr (PciIo, IdeRegsBaseAddr);
445 if (EFI_ERROR (Status)) {
446 return Status;
449 InitAtapiIoPortRegisters(AtapiScsiPrivate, IdeRegsBaseAddr);
452 // Initialize the LatestTargetId to MAX_TARGET_ID.
454 AtapiScsiPrivate->LatestTargetId = MAX_TARGET_ID;
455 AtapiScsiPrivate->LatestLun = 0;
457 Status = InstallScsiPassThruProtocols (&Controller, AtapiScsiPrivate);
459 return Status;
462 EFI_STATUS
463 EFIAPI
464 AtapiScsiPassThruFunction (
465 IN EFI_SCSI_PASS_THRU_PROTOCOL *This,
466 IN UINT32 Target,
467 IN UINT64 Lun,
468 IN OUT EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet,
469 IN EFI_EVENT Event OPTIONAL
471 /*++
473 Routine Description:
475 Implements EFI_SCSI_PASS_THRU_PROTOCOL.PassThru() function.
477 Arguments:
479 This: The EFI_SCSI_PASS_THRU_PROTOCOL instance.
480 Target: The Target ID of the ATAPI device to send the SCSI
481 Request Packet. To ATAPI devices attached on an IDE
482 Channel, Target ID 0 indicates Master device;Target
483 ID 1 indicates Slave device.
484 Lun: The LUN of the ATAPI device to send the SCSI Request
485 Packet. To the ATAPI device, Lun is always 0.
486 Packet: The SCSI Request Packet to send to the ATAPI device
487 specified by Target and Lun.
488 Event: If non-blocking I/O is not supported then Event is ignored,
489 and blocking I/O is performed.
490 If Event is NULL, then blocking I/O is performed.
491 If Event is not NULL and non blocking I/O is supported,
492 then non-blocking I/O is performed, and Event will be signaled
493 when the SCSI Request Packet completes.
495 Returns:
497 EFI_STATUS
499 --*/
501 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate;
502 EFI_STATUS Status;
504 AtapiScsiPrivate = ATAPI_SCSI_PASS_THRU_DEV_FROM_THIS (This);
507 // Target is not allowed beyond MAX_TARGET_ID
509 if ((Target > MAX_TARGET_ID) || (Lun != 0)) {
510 return EFI_INVALID_PARAMETER;
514 // check the data fields in Packet parameter.
516 Status = CheckSCSIRequestPacket (Packet);
517 if (EFI_ERROR (Status)) {
518 return Status;
522 // If Request Packet targets at the IDE channel itself,
523 // do nothing.
525 if (Target == This->Mode->AdapterId) {
526 Packet->TransferLength = 0;
527 return EFI_SUCCESS;
531 // According to Target ID, reset the Atapi I/O Register mapping
532 // (Target Id in [0,1] area, using AtapiIoPortRegisters[0],
533 // Target Id in [2,3] area, using AtapiIoPortRegisters[1]
535 if ((Target / 2) == 0) {
536 Target = Target % 2;
537 AtapiScsiPrivate->IoPort = &AtapiScsiPrivate->AtapiIoPortRegisters[0];
538 } else {
539 Target = Target % 2;
540 AtapiScsiPrivate->IoPort = &AtapiScsiPrivate->AtapiIoPortRegisters[1];
544 // the ATAPI SCSI interface does not support non-blocking I/O
545 // ignore the Event parameter
547 // Performs blocking I/O.
549 Status = SubmitBlockingIoCommand (AtapiScsiPrivate, Target, Packet);
550 return Status;
553 EFI_STATUS
554 EFIAPI
555 AtapiScsiPassThruGetNextDevice (
556 IN EFI_SCSI_PASS_THRU_PROTOCOL *This,
557 IN OUT UINT32 *Target,
558 IN OUT UINT64 *Lun
560 /*++
562 Routine Description:
564 Used to retrieve the list of legal Target IDs for SCSI devices
565 on a SCSI channel.
567 Arguments:
569 This - Protocol instance pointer.
570 Target - On input, a pointer to the Target ID of a SCSI
571 device present on the SCSI channel. On output,
572 a pointer to the Target ID of the next SCSI device
573 present on a SCSI channel. An input value of
574 0xFFFFFFFF retrieves the Target ID of the first
575 SCSI device present on a SCSI channel.
576 Lun - On input, a pointer to the LUN of a SCSI device
577 present on the SCSI channel. On output, a pointer
578 to the LUN of the next SCSI device present on
579 a SCSI channel.
580 Returns:
582 EFI_SUCCESS - The Target ID and Lun of the next SCSI device
583 on the SCSI channel was returned in Target and Lun.
584 EFI_NOT_FOUND - There are no more SCSI devices on this SCSI channel.
585 EFI_INVALID_PARAMETER - Target is not 0xFFFFFFFF,and Target and Lun were not
586 returned on a previous call to GetNextDevice().
587 --*/
589 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate;
592 // Retrieve Device Private Data Structure.
594 AtapiScsiPrivate = ATAPI_SCSI_PASS_THRU_DEV_FROM_THIS (This);
597 // Check whether Target is valid.
599 if (Target == NULL || Lun == NULL) {
600 return EFI_INVALID_PARAMETER;
603 if ((*Target != 0xFFFFFFFF) &&
604 ((*Target != AtapiScsiPrivate->LatestTargetId) ||
605 (*Lun != AtapiScsiPrivate->LatestLun))) {
606 return EFI_INVALID_PARAMETER;
609 if (*Target == MAX_TARGET_ID) {
610 return EFI_NOT_FOUND;
613 if (*Target == 0xFFFFFFFF) {
614 *Target = 0;
615 } else {
616 *Target = AtapiScsiPrivate->LatestTargetId + 1;
619 *Lun = 0;
622 // Update the LatestTargetId.
624 AtapiScsiPrivate->LatestTargetId = *Target;
625 AtapiScsiPrivate->LatestLun = *Lun;
627 return EFI_SUCCESS;
631 EFI_STATUS
632 EFIAPI
633 AtapiScsiPassThruBuildDevicePath (
634 IN EFI_SCSI_PASS_THRU_PROTOCOL *This,
635 IN UINT32 Target,
636 IN UINT64 Lun,
637 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath
639 /*++
641 Routine Description:
643 Used to allocate and build a device path node for a SCSI device
644 on a SCSI channel. Would not build device path for a SCSI Host Controller.
646 Arguments:
648 This - Protocol instance pointer.
649 Target - The Target ID of the SCSI device for which
650 a device path node is to be allocated and built.
651 Lun - The LUN of the SCSI device for which a device
652 path node is to be allocated and built.
653 DevicePath - A pointer to a single device path node that
654 describes the SCSI device specified by
655 Target and Lun. This function is responsible
656 for allocating the buffer DevicePath with the boot
657 service AllocatePool(). It is the caller's
658 responsibility to free DevicePath when the caller
659 is finished with DevicePath.
660 Returns:
661 EFI_SUCCESS - The device path node that describes the SCSI device
662 specified by Target and Lun was allocated and
663 returned in DevicePath.
664 EFI_NOT_FOUND - The SCSI devices specified by Target and Lun does
665 not exist on the SCSI channel.
666 EFI_INVALID_PARAMETER - DevicePath is NULL.
667 EFI_OUT_OF_RESOURCES - There are not enough resources to allocate
668 DevicePath.
669 --*/
671 EFI_DEV_PATH *Node;
675 // Validate parameters passed in.
678 if (DevicePath == NULL) {
679 return EFI_INVALID_PARAMETER;
683 // can not build device path for the SCSI Host Controller.
685 if ((Target > (MAX_TARGET_ID - 1)) || (Lun != 0)) {
686 return EFI_NOT_FOUND;
689 Node = AllocateZeroPool (sizeof (EFI_DEV_PATH));
690 if (Node == NULL) {
691 return EFI_OUT_OF_RESOURCES;
694 Node->DevPath.Type = MESSAGING_DEVICE_PATH;
695 Node->DevPath.SubType = MSG_ATAPI_DP;
696 SetDevicePathNodeLength (&Node->DevPath, sizeof (ATAPI_DEVICE_PATH));
698 Node->Atapi.PrimarySecondary = (UINT8) (Target / 2);
699 Node->Atapi.SlaveMaster = (UINT8) (Target % 2);
700 Node->Atapi.Lun = (UINT16) Lun;
702 *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) Node;
704 return EFI_SUCCESS;
707 EFI_STATUS
708 EFIAPI
709 AtapiScsiPassThruGetTargetLun (
710 IN EFI_SCSI_PASS_THRU_PROTOCOL *This,
711 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
712 OUT UINT32 *Target,
713 OUT UINT64 *Lun
715 /*++
717 Routine Description:
719 Used to translate a device path node to a Target ID and LUN.
721 Arguments:
723 This - Protocol instance pointer.
724 DevicePath - A pointer to the device path node that
725 describes a SCSI device on the SCSI channel.
726 Target - A pointer to the Target ID of a SCSI device
727 on the SCSI channel.
728 Lun - A pointer to the LUN of a SCSI device on
729 the SCSI channel.
730 Returns:
732 EFI_SUCCESS - DevicePath was successfully translated to a
733 Target ID and LUN, and they were returned
734 in Target and Lun.
735 EFI_INVALID_PARAMETER - DevicePath/Target/Lun is NULL.
736 EFI_UNSUPPORTED - This driver does not support the device path
737 node type in DevicePath.
738 EFI_NOT_FOUND - A valid translation from DevicePath to a
739 Target ID and LUN does not exist.
740 --*/
742 EFI_DEV_PATH *Node;
745 // Validate parameters passed in.
747 if (DevicePath == NULL || Target == NULL || Lun == NULL) {
748 return EFI_INVALID_PARAMETER;
752 // Check whether the DevicePath belongs to SCSI_DEVICE_PATH
754 if ((DevicePath->Type != MESSAGING_DEVICE_PATH) ||
755 (DevicePath->SubType != MSG_ATAPI_DP) ||
756 (DevicePathNodeLength(DevicePath) != sizeof(ATAPI_DEVICE_PATH))) {
757 return EFI_UNSUPPORTED;
760 Node = (EFI_DEV_PATH *) DevicePath;
762 *Target = Node->Atapi.PrimarySecondary * 2 + Node->Atapi.SlaveMaster;
763 *Lun = Node->Atapi.Lun;
765 if (*Target > (MAX_TARGET_ID - 1) || *Lun != 0) {
766 return EFI_NOT_FOUND;
769 return EFI_SUCCESS;
772 EFI_STATUS
773 EFIAPI
774 AtapiScsiPassThruResetChannel (
775 IN EFI_SCSI_PASS_THRU_PROTOCOL *This
777 /*++
779 Routine Description:
781 Resets a SCSI channel.This operation resets all the
782 SCSI devices connected to the SCSI channel.
784 Arguments:
786 This - Protocol instance pointer.
788 Returns:
790 EFI_SUCCESS - The SCSI channel was reset.
791 EFI_UNSUPPORTED - The SCSI channel does not support
792 a channel reset operation.
793 EFI_DEVICE_ERROR - A device error occurred while
794 attempting to reset the SCSI channel.
795 EFI_TIMEOUT - A timeout occurred while attempting
796 to reset the SCSI channel.
797 --*/
799 UINT8 DeviceControlValue;
800 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate;
801 UINT8 Index;
802 BOOLEAN ResetFlag;
804 AtapiScsiPrivate = ATAPI_SCSI_PASS_THRU_DEV_FROM_THIS (This);
805 ResetFlag = FALSE;
808 // Reset both Primary channel and Secondary channel.
809 // so, the IoPort pointer must point to the right I/O Register group
811 for (Index = 0; Index < 2; Index++) {
813 // Reset
815 AtapiScsiPrivate->IoPort = &AtapiScsiPrivate->AtapiIoPortRegisters[Index];
817 DeviceControlValue = 0;
819 // set SRST bit to initiate soft reset
821 DeviceControlValue |= SRST;
823 // disable Interrupt
825 DeviceControlValue |= BIT1;
826 WritePortB (
827 AtapiScsiPrivate->PciIo,
828 AtapiScsiPrivate->IoPort->Alt.DeviceControl,
829 DeviceControlValue
833 // Wait 10us
835 gBS->Stall (10);
838 // Clear SRST bit
839 // 0xfb:1111,1011
841 DeviceControlValue &= 0xfb;
843 WritePortB (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->Alt.DeviceControl, DeviceControlValue);
846 // slave device needs at most 31s to clear BSY
848 if (StatusWaitForBSYClear (AtapiScsiPrivate, 31000000) != EFI_TIMEOUT) {
849 ResetFlag = TRUE;
853 if (ResetFlag) {
854 return EFI_SUCCESS;
857 return EFI_TIMEOUT;
860 EFI_STATUS
861 EFIAPI
862 AtapiScsiPassThruResetTarget (
863 IN EFI_SCSI_PASS_THRU_PROTOCOL *This,
864 IN UINT32 Target,
865 IN UINT64 Lun
867 /*++
869 Routine Description:
871 Resets a SCSI device that is connected to a SCSI channel.
873 Arguments:
875 This - Protocol instance pointer.
876 Target - The Target ID of the SCSI device to reset.
877 Lun - The LUN of the SCSI device to reset.
879 Returns:
881 EFI_SUCCESS - The SCSI device specified by Target and
882 Lun was reset.
883 EFI_UNSUPPORTED - The SCSI channel does not support a target
884 reset operation.
885 EFI_INVALID_PARAMETER - Target or Lun are invalid.
886 EFI_DEVICE_ERROR - A device error occurred while attempting
887 to reset the SCSI device specified by Target
888 and Lun.
889 EFI_TIMEOUT - A timeout occurred while attempting to reset
890 the SCSI device specified by Target and Lun.
891 --*/
893 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate;
894 UINT8 Command;
895 UINT8 DeviceSelect;
897 AtapiScsiPrivate = ATAPI_SCSI_PASS_THRU_DEV_FROM_THIS (This);
899 if ((Target > MAX_TARGET_ID) || (Lun != 0)) {
900 return EFI_INVALID_PARAMETER;
903 // Directly return EFI_SUCCESS if want to reset the host controller
905 if (Target == This->Mode->AdapterId) {
906 return EFI_SUCCESS;
910 // According to Target ID, reset the Atapi I/O Register mapping
911 // (Target Id in [0,1] area, using AtapiIoPortRegisters[0],
912 // Target Id in [2,3] area, using AtapiIoPortRegisters[1]
914 if ((Target / 2) == 0) {
915 AtapiScsiPrivate->IoPort = &AtapiScsiPrivate->AtapiIoPortRegisters[0];
916 } else {
917 AtapiScsiPrivate->IoPort = &AtapiScsiPrivate->AtapiIoPortRegisters[1];
921 // for ATAPI device, no need to wait DRDY ready after device selecting.
923 // bit7 and bit5 are both set to 1 for backward compatibility
925 DeviceSelect = (UINT8) (((BIT7 | BIT5) | (Target << 4)));
926 WritePortB (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->Head, DeviceSelect);
928 Command = ATAPI_SOFT_RESET_CMD;
929 WritePortB (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->Reg.Command, Command);
932 // BSY clear is the only status return to the host by the device
933 // when reset is complete.
934 // slave device needs at most 31s to clear BSY
936 if (EFI_ERROR (StatusWaitForBSYClear (AtapiScsiPrivate, 31000000))) {
937 return EFI_TIMEOUT;
941 // stall 5 seconds to make the device status stable
943 gBS->Stall (5000000);
945 return EFI_SUCCESS;
948 EFI_STATUS
949 EFIAPI
950 AtapiExtScsiPassThruFunction (
951 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
952 IN UINT8 *Target,
953 IN UINT64 Lun,
954 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet,
955 IN EFI_EVENT Event OPTIONAL
957 /*++
959 Routine Description:
961 Implements EFI_EXT_SCSI_PASS_THRU_PROTOCOL.PassThru() function.
963 Arguments:
965 This: The EFI_EXT_SCSI_PASS_THRU_PROTOCOL instance.
966 Target: The Target ID of the ATAPI device to send the SCSI
967 Request Packet. To ATAPI devices attached on an IDE
968 Channel, Target ID 0 indicates Master device;Target
969 ID 1 indicates Slave device.
970 Lun: The LUN of the ATAPI device to send the SCSI Request
971 Packet. To the ATAPI device, Lun is always 0.
972 Packet: The SCSI Request Packet to send to the ATAPI device
973 specified by Target and Lun.
974 Event: If non-blocking I/O is not supported then Event is ignored,
975 and blocking I/O is performed.
976 If Event is NULL, then blocking I/O is performed.
977 If Event is not NULL and non blocking I/O is supported,
978 then non-blocking I/O is performed, and Event will be signaled
979 when the SCSI Request Packet completes.
981 Returns:
983 EFI_STATUS
985 --*/
987 EFI_STATUS Status;
988 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate;
989 UINT8 TargetId;
991 AtapiScsiPrivate = ATAPI_EXT_SCSI_PASS_THRU_DEV_FROM_THIS (This);
994 // For ATAPI device, UINT8 is enough to represent the SCSI ID on channel.
996 TargetId = Target[0];
999 // Target is not allowed beyond MAX_TARGET_ID
1001 if ((TargetId > MAX_TARGET_ID) || (Lun != 0)) {
1002 return EFI_INVALID_PARAMETER;
1006 // check the data fields in Packet parameter.
1008 Status = CheckExtSCSIRequestPacket (Packet);
1009 if (EFI_ERROR (Status)) {
1010 return Status;
1014 // If Request Packet targets at the IDE channel itself,
1015 // do nothing.
1017 if (TargetId == (UINT8)This->Mode->AdapterId) {
1018 Packet->InTransferLength = Packet->OutTransferLength = 0;
1019 return EFI_SUCCESS;
1023 // According to Target ID, reset the Atapi I/O Register mapping
1024 // (Target Id in [0,1] area, using AtapiIoPortRegisters[0],
1025 // Target Id in [2,3] area, using AtapiIoPortRegisters[1]
1027 if ((TargetId / 2) == 0) {
1028 TargetId = (UINT8) (TargetId % 2);
1029 AtapiScsiPrivate->IoPort = &AtapiScsiPrivate->AtapiIoPortRegisters[0];
1030 } else {
1031 TargetId = (UINT8) (TargetId % 2);
1032 AtapiScsiPrivate->IoPort = &AtapiScsiPrivate->AtapiIoPortRegisters[1];
1036 // the ATAPI SCSI interface does not support non-blocking I/O
1037 // ignore the Event parameter
1039 // Performs blocking I/O.
1041 Status = SubmitExtBlockingIoCommand (AtapiScsiPrivate, TargetId, Packet);
1042 return Status;
1045 EFI_STATUS
1046 EFIAPI
1047 AtapiExtScsiPassThruGetNextTargetLun (
1048 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
1049 IN OUT UINT8 **Target,
1050 IN OUT UINT64 *Lun
1052 /*++
1054 Routine Description:
1056 Used to retrieve the list of legal Target IDs for SCSI devices
1057 on a SCSI channel.
1059 Arguments:
1061 This - Protocol instance pointer.
1062 Target - On input, a pointer to the Target ID of a SCSI
1063 device present on the SCSI channel. On output,
1064 a pointer to the Target ID of the next SCSI device
1065 present on a SCSI channel. An input value of
1066 0xFFFFFFFF retrieves the Target ID of the first
1067 SCSI device present on a SCSI channel.
1068 Lun - On input, a pointer to the LUN of a SCSI device
1069 present on the SCSI channel. On output, a pointer
1070 to the LUN of the next SCSI device present on
1071 a SCSI channel.
1072 Returns:
1074 EFI_SUCCESS - The Target ID and Lun of the next SCSI device
1075 on the SCSI channel was returned in Target and Lun.
1076 EFI_NOT_FOUND - There are no more SCSI devices on this SCSI channel.
1077 EFI_INVALID_PARAMETER - Target is not 0xFFFFFFFF,and Target and Lun were not
1078 returned on a previous call to GetNextDevice().
1079 --*/
1081 UINT8 ByteIndex;
1082 UINT8 TargetId;
1083 UINT8 ScsiId[TARGET_MAX_BYTES];
1084 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate;
1087 // Retrieve Device Private Data Structure.
1089 AtapiScsiPrivate = ATAPI_EXT_SCSI_PASS_THRU_DEV_FROM_THIS (This);
1092 // Check whether Target is valid.
1094 if (*Target == NULL || Lun == NULL) {
1095 return EFI_INVALID_PARAMETER;
1098 SetMem (ScsiId, TARGET_MAX_BYTES, 0xFF);
1100 TargetId = (*Target)[0];
1103 // For ATAPI device, we use UINT8 to represent the SCSI ID on channel.
1105 if (CompareMem(*Target, ScsiId, TARGET_MAX_BYTES) != 0) {
1106 for (ByteIndex = 1; ByteIndex < TARGET_MAX_BYTES; ByteIndex++) {
1107 if ((*Target)[ByteIndex] != 0) {
1108 return EFI_INVALID_PARAMETER;
1113 if ((CompareMem(*Target, ScsiId, TARGET_MAX_BYTES) != 0) &&
1114 ((TargetId != AtapiScsiPrivate->LatestTargetId) ||
1115 (*Lun != AtapiScsiPrivate->LatestLun))) {
1116 return EFI_INVALID_PARAMETER;
1119 if (TargetId == MAX_TARGET_ID) {
1120 return EFI_NOT_FOUND;
1123 if (CompareMem(*Target, ScsiId, TARGET_MAX_BYTES) == 0) {
1124 SetMem (*Target, TARGET_MAX_BYTES,0);
1125 } else {
1126 (*Target)[0] = (UINT8) (AtapiScsiPrivate->LatestTargetId + 1);
1129 *Lun = 0;
1132 // Update the LatestTargetId.
1134 AtapiScsiPrivate->LatestTargetId = (*Target)[0];
1135 AtapiScsiPrivate->LatestLun = *Lun;
1137 return EFI_SUCCESS;
1141 EFI_STATUS
1142 EFIAPI
1143 AtapiExtScsiPassThruBuildDevicePath (
1144 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
1145 IN UINT8 *Target,
1146 IN UINT64 Lun,
1147 IN OUT EFI_DEVICE_PATH_PROTOCOL **DevicePath
1149 /*++
1151 Routine Description:
1153 Used to allocate and build a device path node for a SCSI device
1154 on a SCSI channel. Would not build device path for a SCSI Host Controller.
1156 Arguments:
1158 This - Protocol instance pointer.
1159 Target - The Target ID of the SCSI device for which
1160 a device path node is to be allocated and built.
1161 Lun - The LUN of the SCSI device for which a device
1162 path node is to be allocated and built.
1163 DevicePath - A pointer to a single device path node that
1164 describes the SCSI device specified by
1165 Target and Lun. This function is responsible
1166 for allocating the buffer DevicePath with the boot
1167 service AllocatePool(). It is the caller's
1168 responsibility to free DevicePath when the caller
1169 is finished with DevicePath.
1170 Returns:
1171 EFI_SUCCESS - The device path node that describes the SCSI device
1172 specified by Target and Lun was allocated and
1173 returned in DevicePath.
1174 EFI_NOT_FOUND - The SCSI devices specified by Target and Lun does
1175 not exist on the SCSI channel.
1176 EFI_INVALID_PARAMETER - DevicePath is NULL.
1177 EFI_OUT_OF_RESOURCES - There are not enough resources to allocate
1178 DevicePath.
1179 --*/
1181 EFI_DEV_PATH *Node;
1182 UINT8 TargetId;
1184 TargetId = Target[0];
1187 // Validate parameters passed in.
1190 if (DevicePath == NULL) {
1191 return EFI_INVALID_PARAMETER;
1195 // can not build device path for the SCSI Host Controller.
1197 if ((TargetId > (MAX_TARGET_ID - 1)) || (Lun != 0)) {
1198 return EFI_NOT_FOUND;
1201 Node = AllocateZeroPool (sizeof (EFI_DEV_PATH));
1202 if (Node == NULL) {
1203 return EFI_OUT_OF_RESOURCES;
1206 Node->DevPath.Type = MESSAGING_DEVICE_PATH;
1207 Node->DevPath.SubType = MSG_ATAPI_DP;
1208 SetDevicePathNodeLength (&Node->DevPath, sizeof (ATAPI_DEVICE_PATH));
1210 Node->Atapi.PrimarySecondary = (UINT8) (TargetId / 2);
1211 Node->Atapi.SlaveMaster = (UINT8) (TargetId % 2);
1212 Node->Atapi.Lun = (UINT16) Lun;
1214 *DevicePath = (EFI_DEVICE_PATH_PROTOCOL *) Node;
1216 return EFI_SUCCESS;
1219 EFI_STATUS
1220 EFIAPI
1221 AtapiExtScsiPassThruGetTargetLun (
1222 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
1223 IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
1224 OUT UINT8 **Target,
1225 OUT UINT64 *Lun
1227 /*++
1229 Routine Description:
1231 Used to translate a device path node to a Target ID and LUN.
1233 Arguments:
1235 This - Protocol instance pointer.
1236 DevicePath - A pointer to the device path node that
1237 describes a SCSI device on the SCSI channel.
1238 Target - A pointer to the Target ID of a SCSI device
1239 on the SCSI channel.
1240 Lun - A pointer to the LUN of a SCSI device on
1241 the SCSI channel.
1242 Returns:
1244 EFI_SUCCESS - DevicePath was successfully translated to a
1245 Target ID and LUN, and they were returned
1246 in Target and Lun.
1247 EFI_INVALID_PARAMETER - DevicePath/Target/Lun is NULL.
1248 EFI_UNSUPPORTED - This driver does not support the device path
1249 node type in DevicePath.
1250 EFI_NOT_FOUND - A valid translation from DevicePath to a
1251 Target ID and LUN does not exist.
1252 --*/
1254 EFI_DEV_PATH *Node;
1257 // Validate parameters passed in.
1259 if (DevicePath == NULL || Target == NULL || Lun == NULL) {
1260 return EFI_INVALID_PARAMETER;
1264 // Check whether the DevicePath belongs to SCSI_DEVICE_PATH
1266 if ((DevicePath->Type != MESSAGING_DEVICE_PATH) ||
1267 (DevicePath->SubType != MSG_ATAPI_DP) ||
1268 (DevicePathNodeLength(DevicePath) != sizeof(ATAPI_DEVICE_PATH))) {
1269 return EFI_UNSUPPORTED;
1272 ZeroMem (*Target, TARGET_MAX_BYTES);
1274 Node = (EFI_DEV_PATH *) DevicePath;
1276 (*Target)[0] = (UINT8) (Node->Atapi.PrimarySecondary * 2 + Node->Atapi.SlaveMaster);
1277 *Lun = Node->Atapi.Lun;
1279 if ((*Target)[0] > (MAX_TARGET_ID - 1) || *Lun != 0) {
1280 return EFI_NOT_FOUND;
1283 return EFI_SUCCESS;
1286 EFI_STATUS
1287 EFIAPI
1288 AtapiExtScsiPassThruResetChannel (
1289 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This
1291 /*++
1293 Routine Description:
1295 Resets a SCSI channel.This operation resets all the
1296 SCSI devices connected to the SCSI channel.
1298 Arguments:
1300 This - Protocol instance pointer.
1302 Returns:
1304 EFI_SUCCESS - The SCSI channel was reset.
1305 EFI_UNSUPPORTED - The SCSI channel does not support
1306 a channel reset operation.
1307 EFI_DEVICE_ERROR - A device error occurred while
1308 attempting to reset the SCSI channel.
1309 EFI_TIMEOUT - A timeout occurred while attempting
1310 to reset the SCSI channel.
1311 --*/
1313 UINT8 DeviceControlValue;
1314 UINT8 Index;
1315 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate;
1316 BOOLEAN ResetFlag;
1318 AtapiScsiPrivate = ATAPI_EXT_SCSI_PASS_THRU_DEV_FROM_THIS (This);
1319 ResetFlag = FALSE;
1321 // Reset both Primary channel and Secondary channel.
1322 // so, the IoPort pointer must point to the right I/O Register group
1323 // And if there is a channel reset successfully, return EFI_SUCCESS.
1325 for (Index = 0; Index < 2; Index++) {
1327 // Reset
1329 AtapiScsiPrivate->IoPort = &AtapiScsiPrivate->AtapiIoPortRegisters[Index];
1331 DeviceControlValue = 0;
1333 // set SRST bit to initiate soft reset
1335 DeviceControlValue |= SRST;
1337 // disable Interrupt
1339 DeviceControlValue |= BIT1;
1340 WritePortB (
1341 AtapiScsiPrivate->PciIo,
1342 AtapiScsiPrivate->IoPort->Alt.DeviceControl,
1343 DeviceControlValue
1347 // Wait 10us
1349 gBS->Stall (10);
1352 // Clear SRST bit
1353 // 0xfb:1111,1011
1355 DeviceControlValue &= 0xfb;
1357 WritePortB (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->Alt.DeviceControl, DeviceControlValue);
1360 // slave device needs at most 31s to clear BSY
1362 if (StatusWaitForBSYClear (AtapiScsiPrivate, 31000000) != EFI_TIMEOUT) {
1363 ResetFlag = TRUE;
1367 if (ResetFlag) {
1368 return EFI_SUCCESS;
1371 return EFI_TIMEOUT;
1374 EFI_STATUS
1375 EFIAPI
1376 AtapiExtScsiPassThruResetTarget (
1377 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
1378 IN UINT8 *Target,
1379 IN UINT64 Lun
1381 /*++
1383 Routine Description:
1385 Resets a SCSI device that is connected to a SCSI channel.
1387 Arguments:
1389 This - Protocol instance pointer.
1390 Target - The Target ID of the SCSI device to reset.
1391 Lun - The LUN of the SCSI device to reset.
1393 Returns:
1395 EFI_SUCCESS - The SCSI device specified by Target and
1396 Lun was reset.
1397 EFI_UNSUPPORTED - The SCSI channel does not support a target
1398 reset operation.
1399 EFI_INVALID_PARAMETER - Target or Lun are invalid.
1400 EFI_DEVICE_ERROR - A device error occurred while attempting
1401 to reset the SCSI device specified by Target
1402 and Lun.
1403 EFI_TIMEOUT - A timeout occurred while attempting to reset
1404 the SCSI device specified by Target and Lun.
1405 --*/
1407 UINT8 Command;
1408 UINT8 DeviceSelect;
1409 UINT8 TargetId;
1410 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate;
1412 AtapiScsiPrivate = ATAPI_EXT_SCSI_PASS_THRU_DEV_FROM_THIS (This);
1413 TargetId = Target[0];
1415 if ((TargetId > MAX_TARGET_ID) || (Lun != 0)) {
1416 return EFI_INVALID_PARAMETER;
1419 // Directly return EFI_SUCCESS if want to reset the host controller
1421 if (TargetId == This->Mode->AdapterId) {
1422 return EFI_SUCCESS;
1426 // According to Target ID, reset the Atapi I/O Register mapping
1427 // (Target Id in [0,1] area, using AtapiIoPortRegisters[0],
1428 // Target Id in [2,3] area, using AtapiIoPortRegisters[1]
1430 if ((TargetId / 2) == 0) {
1431 AtapiScsiPrivate->IoPort = &AtapiScsiPrivate->AtapiIoPortRegisters[0];
1432 } else {
1433 AtapiScsiPrivate->IoPort = &AtapiScsiPrivate->AtapiIoPortRegisters[1];
1437 // for ATAPI device, no need to wait DRDY ready after device selecting.
1439 // bit7 and bit5 are both set to 1 for backward compatibility
1441 DeviceSelect = (UINT8) ((BIT7 | BIT5) | (TargetId << 4));
1442 WritePortB (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->Head, DeviceSelect);
1444 Command = ATAPI_SOFT_RESET_CMD;
1445 WritePortB (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->Reg.Command, Command);
1448 // BSY clear is the only status return to the host by the device
1449 // when reset is complete.
1450 // slave device needs at most 31s to clear BSY
1452 if (EFI_ERROR (StatusWaitForBSYClear (AtapiScsiPrivate, 31000000))) {
1453 return EFI_TIMEOUT;
1457 // stall 5 seconds to make the device status stable
1459 gBS->Stall (5000000);
1461 return EFI_SUCCESS;
1465 EFI_STATUS
1466 EFIAPI
1467 AtapiExtScsiPassThruGetNextTarget (
1468 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *This,
1469 IN OUT UINT8 **Target
1471 /*++
1473 Routine Description:
1474 Used to retrieve the list of legal Target IDs for SCSI devices
1475 on a SCSI channel.
1477 Arguments:
1478 This - Protocol instance pointer.
1479 Target - On input, a pointer to the Target ID of a SCSI
1480 device present on the SCSI channel. On output,
1481 a pointer to the Target ID of the next SCSI device
1482 present on a SCSI channel. An input value of
1483 0xFFFFFFFF retrieves the Target ID of the first
1484 SCSI device present on a SCSI channel.
1485 Lun - On input, a pointer to the LUN of a SCSI device
1486 present on the SCSI channel. On output, a pointer
1487 to the LUN of the next SCSI device present on
1488 a SCSI channel.
1490 Returns:
1491 EFI_SUCCESS - The Target ID and Lun of the next SCSI device
1492 on the SCSI channel was returned in Target and Lun.
1493 EFI_NOT_FOUND - There are no more SCSI devices on this SCSI channel.
1494 EFI_INVALID_PARAMETER - Target is not 0xFFFFFFFF,and Target and Lun were not
1495 returned on a previous call to GetNextDevice().
1496 --*/
1498 UINT8 TargetId;
1499 UINT8 ScsiId[TARGET_MAX_BYTES];
1500 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate;
1501 UINT8 ByteIndex;
1504 // Retrieve Device Private Data Structure.
1506 AtapiScsiPrivate = ATAPI_EXT_SCSI_PASS_THRU_DEV_FROM_THIS (This);
1509 // Check whether Target is valid.
1511 if (*Target == NULL ) {
1512 return EFI_INVALID_PARAMETER;
1515 TargetId = (*Target)[0];
1516 SetMem (ScsiId, TARGET_MAX_BYTES, 0xFF);
1519 // For ATAPI device, we use UINT8 to represent the SCSI ID on channel.
1521 if (CompareMem(*Target, ScsiId, TARGET_MAX_BYTES) != 0) {
1522 for (ByteIndex = 1; ByteIndex < TARGET_MAX_BYTES; ByteIndex++) {
1523 if ((*Target)[ByteIndex] != 0) {
1524 return EFI_INVALID_PARAMETER;
1529 if ((CompareMem(*Target, ScsiId, TARGET_MAX_BYTES) != 0) &&(TargetId != AtapiScsiPrivate->LatestTargetId)) {
1530 return EFI_INVALID_PARAMETER;
1533 if (TargetId == MAX_TARGET_ID) {
1534 return EFI_NOT_FOUND;
1537 if ((CompareMem(*Target, ScsiId, TARGET_MAX_BYTES) == 0)) {
1538 SetMem (*Target, TARGET_MAX_BYTES, 0);
1539 } else {
1540 (*Target)[0] = (UINT8) (AtapiScsiPrivate->LatestTargetId + 1);
1544 // Update the LatestTargetId.
1546 AtapiScsiPrivate->LatestTargetId = (*Target)[0];
1547 AtapiScsiPrivate->LatestLun = 0;
1549 return EFI_SUCCESS;
1552 EFI_STATUS
1553 GetIdeRegistersBaseAddr (
1554 IN EFI_PCI_IO_PROTOCOL *PciIo,
1555 OUT IDE_REGISTERS_BASE_ADDR *IdeRegsBaseAddr
1557 /*++
1559 Routine Description:
1560 Get IDE IO port registers' base addresses by mode. In 'Compatibility' mode,
1561 use fixed addresses. In Native-PCI mode, get base addresses from BARs in
1562 the PCI IDE controller's Configuration Space.
1564 Arguments:
1565 PciIo - Pointer to the EFI_PCI_IO_PROTOCOL instance
1566 IdeRegsBaseAddr - Pointer to IDE_REGISTERS_BASE_ADDR to
1567 receive IDE IO port registers' base addresses
1569 Returns:
1571 EFI_STATUS
1573 --*/
1575 EFI_STATUS Status;
1576 PCI_TYPE00 PciData;
1578 Status = PciIo->Pci.Read (
1579 PciIo,
1580 EfiPciIoWidthUint8,
1582 sizeof (PciData),
1583 &PciData
1586 if (EFI_ERROR (Status)) {
1587 return Status;
1590 if ((PciData.Hdr.ClassCode[0] & IDE_PRIMARY_OPERATING_MODE) == 0) {
1591 IdeRegsBaseAddr[IdePrimary].CommandBlockBaseAddr = 0x1f0;
1592 IdeRegsBaseAddr[IdePrimary].ControlBlockBaseAddr = 0x3f6;
1593 } else {
1595 // The BARs should be of IO type
1597 if ((PciData.Device.Bar[0] & BIT0) == 0 ||
1598 (PciData.Device.Bar[1] & BIT0) == 0) {
1599 return EFI_UNSUPPORTED;
1602 IdeRegsBaseAddr[IdePrimary].CommandBlockBaseAddr =
1603 (UINT16) (PciData.Device.Bar[0] & 0x0000fff8);
1604 IdeRegsBaseAddr[IdePrimary].ControlBlockBaseAddr =
1605 (UINT16) ((PciData.Device.Bar[1] & 0x0000fffc) + 2);
1608 if ((PciData.Hdr.ClassCode[0] & IDE_SECONDARY_OPERATING_MODE) == 0) {
1609 IdeRegsBaseAddr[IdeSecondary].CommandBlockBaseAddr = 0x170;
1610 IdeRegsBaseAddr[IdeSecondary].ControlBlockBaseAddr = 0x376;
1611 } else {
1613 // The BARs should be of IO type
1615 if ((PciData.Device.Bar[2] & BIT0) == 0 ||
1616 (PciData.Device.Bar[3] & BIT0) == 0) {
1617 return EFI_UNSUPPORTED;
1620 IdeRegsBaseAddr[IdeSecondary].CommandBlockBaseAddr =
1621 (UINT16) (PciData.Device.Bar[2] & 0x0000fff8);
1622 IdeRegsBaseAddr[IdeSecondary].ControlBlockBaseAddr =
1623 (UINT16) ((PciData.Device.Bar[3] & 0x0000fffc) + 2);
1626 return EFI_SUCCESS;
1629 VOID
1630 InitAtapiIoPortRegisters (
1631 IN ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,
1632 IN IDE_REGISTERS_BASE_ADDR *IdeRegsBaseAddr
1634 /*++
1636 Routine Description:
1638 Initialize each Channel's Base Address of CommandBlock and ControlBlock.
1640 Arguments:
1642 AtapiScsiPrivate - The pointer of ATAPI_SCSI_PASS_THRU_DEV
1643 IdeRegsBaseAddr - The pointer of IDE_REGISTERS_BASE_ADDR
1645 Returns:
1647 None
1649 --*/
1652 UINT8 IdeChannel;
1653 UINT16 CommandBlockBaseAddr;
1654 UINT16 ControlBlockBaseAddr;
1655 IDE_BASE_REGISTERS *RegisterPointer;
1658 for (IdeChannel = 0; IdeChannel < ATAPI_MAX_CHANNEL; IdeChannel++) {
1660 RegisterPointer = &AtapiScsiPrivate->AtapiIoPortRegisters[IdeChannel];
1663 // Initialize IDE IO port addresses, including Command Block registers
1664 // and Control Block registers
1666 CommandBlockBaseAddr = IdeRegsBaseAddr[IdeChannel].CommandBlockBaseAddr;
1667 ControlBlockBaseAddr = IdeRegsBaseAddr[IdeChannel].ControlBlockBaseAddr;
1669 RegisterPointer->Data = CommandBlockBaseAddr;
1670 (*(UINT16 *) &RegisterPointer->Reg1) = (UINT16) (CommandBlockBaseAddr + 0x01);
1671 RegisterPointer->SectorCount = (UINT16) (CommandBlockBaseAddr + 0x02);
1672 RegisterPointer->SectorNumber = (UINT16) (CommandBlockBaseAddr + 0x03);
1673 RegisterPointer->CylinderLsb = (UINT16) (CommandBlockBaseAddr + 0x04);
1674 RegisterPointer->CylinderMsb = (UINT16) (CommandBlockBaseAddr + 0x05);
1675 RegisterPointer->Head = (UINT16) (CommandBlockBaseAddr + 0x06);
1676 (*(UINT16 *) &RegisterPointer->Reg) = (UINT16) (CommandBlockBaseAddr + 0x07);
1678 (*(UINT16 *) &RegisterPointer->Alt) = ControlBlockBaseAddr;
1679 RegisterPointer->DriveAddress = (UINT16) (ControlBlockBaseAddr + 0x01);
1685 EFI_STATUS
1686 CheckSCSIRequestPacket (
1687 EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet
1689 /*++
1691 Routine Description:
1693 Checks the parameters in the SCSI Request Packet to make sure
1694 they are valid for a SCSI Pass Thru request.
1696 Arguments:
1698 Packet - The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
1700 Returns:
1702 EFI_STATUS
1704 --*/
1706 if (Packet == NULL) {
1707 return EFI_INVALID_PARAMETER;
1710 if (!ValidCdbLength (Packet->CdbLength)) {
1711 return EFI_INVALID_PARAMETER;
1714 if (Packet->Cdb == NULL) {
1715 return EFI_INVALID_PARAMETER;
1719 // Checks whether the request command is supported.
1721 if (!IsCommandValid (Packet)) {
1722 return EFI_UNSUPPORTED;
1725 return EFI_SUCCESS;
1728 BOOLEAN
1729 IsCommandValid (
1730 EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet
1732 /*++
1734 Routine Description:
1736 Checks the requested SCSI command:
1737 Is it supported by this driver?
1738 Is the Data transfer direction reasonable?
1740 Arguments:
1742 Packet - The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
1744 Returns:
1746 EFI_STATUS
1748 --*/
1750 UINT8 Index;
1751 UINT8 *OpCode;
1753 OpCode = (UINT8 *) (Packet->Cdb);
1755 for (Index = 0; CompareMem (&gSupportedATAPICommands[Index], &gEndTable, sizeof (SCSI_COMMAND_SET)); Index++) {
1757 if (*OpCode == gSupportedATAPICommands[Index].OpCode) {
1759 // Check whether the requested Command is supported by this driver
1761 if (Packet->DataDirection == DataIn) {
1763 // Check whether the requested data direction conforms to
1764 // what it should be.
1766 if (gSupportedATAPICommands[Index].Direction == DataOut) {
1767 return FALSE;
1771 if (Packet->DataDirection == DataOut) {
1773 // Check whether the requested data direction conforms to
1774 // what it should be.
1776 if (gSupportedATAPICommands[Index].Direction == DataIn) {
1777 return FALSE;
1781 return TRUE;
1785 return FALSE;
1788 EFI_STATUS
1789 SubmitBlockingIoCommand (
1790 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,
1791 UINT32 Target,
1792 EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet
1794 /*++
1796 Routine Description:
1798 Performs blocking I/O request.
1800 Arguments:
1802 AtapiScsiPrivate: Private data structure for the specified channel.
1803 Target: The Target ID of the ATAPI device to send the SCSI
1804 Request Packet. To ATAPI devices attached on an IDE
1805 Channel, Target ID 0 indicates Master device;Target
1806 ID 1 indicates Slave device.
1807 Packet: The SCSI Request Packet to send to the ATAPI device
1808 specified by Target.
1810 Returns: EFI_STATUS
1812 --*/
1814 UINT8 PacketCommand[12];
1815 UINT64 TimeoutInMicroSeconds;
1816 EFI_STATUS PacketCommandStatus;
1819 // Fill ATAPI Command Packet according to CDB
1821 ZeroMem (&PacketCommand, 12);
1822 CopyMem (&PacketCommand, Packet->Cdb, Packet->CdbLength);
1825 // Timeout is 100ns unit, convert it to 1000ns (1us) unit.
1827 TimeoutInMicroSeconds = DivU64x32 (Packet->Timeout, (UINT32) 10);
1830 // Submit ATAPI Command Packet
1832 PacketCommandStatus = AtapiPacketCommand (
1833 AtapiScsiPrivate,
1834 Target,
1835 PacketCommand,
1836 Packet->DataBuffer,
1837 &(Packet->TransferLength),
1838 (DATA_DIRECTION) Packet->DataDirection,
1839 TimeoutInMicroSeconds
1841 if (!EFI_ERROR (PacketCommandStatus) || (Packet->SenseData == NULL)) {
1842 Packet->SenseDataLength = 0;
1843 return PacketCommandStatus;
1847 // Return SenseData if PacketCommandStatus matches
1848 // the following return codes.
1850 if ((PacketCommandStatus == EFI_BAD_BUFFER_SIZE) ||
1851 (PacketCommandStatus == EFI_DEVICE_ERROR) ||
1852 (PacketCommandStatus == EFI_TIMEOUT)) {
1855 // avoid submit request sense command continuously.
1857 if (PacketCommand[0] == OP_REQUEST_SENSE) {
1858 Packet->SenseDataLength = 0;
1859 return PacketCommandStatus;
1862 RequestSenseCommand (
1863 AtapiScsiPrivate,
1864 Target,
1865 Packet->Timeout,
1866 Packet->SenseData,
1867 &Packet->SenseDataLength
1871 return PacketCommandStatus;
1874 EFI_STATUS
1875 RequestSenseCommand (
1876 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,
1877 UINT32 Target,
1878 UINT64 Timeout,
1879 VOID *SenseData,
1880 UINT8 *SenseDataLength
1882 /*++
1884 Routine Description:
1886 Sumbit request sense command
1888 Arguments:
1890 AtapiScsiPrivate - The pionter of ATAPI_SCSI_PASS_THRU_DEV
1891 Target - The target ID
1892 Timeout - The time to complete the command
1893 SenseData - The buffer to fill in sense data
1894 SenseDataLength - The length of buffer
1896 Returns:
1898 EFI_STATUS
1900 --*/
1902 EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET Packet;
1903 UINT8 Cdb[12];
1904 EFI_STATUS Status;
1906 ZeroMem (&Packet, sizeof (EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET));
1907 ZeroMem (Cdb, 12);
1909 Cdb[0] = OP_REQUEST_SENSE;
1910 Cdb[4] = (UINT8) (*SenseDataLength);
1912 Packet.Timeout = Timeout;
1913 Packet.DataBuffer = SenseData;
1914 Packet.SenseData = NULL;
1915 Packet.Cdb = Cdb;
1916 Packet.TransferLength = *SenseDataLength;
1917 Packet.CdbLength = 12;
1918 Packet.DataDirection = DataIn;
1920 Status = SubmitBlockingIoCommand (AtapiScsiPrivate, Target, &Packet);
1921 *SenseDataLength = (UINT8) (Packet.TransferLength);
1922 return Status;
1925 EFI_STATUS
1926 CheckExtSCSIRequestPacket (
1927 EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet
1929 /*++
1931 Routine Description:
1933 Checks the parameters in the SCSI Request Packet to make sure
1934 they are valid for a SCSI Pass Thru request.
1936 Arguments:
1938 Packet - The pointer of EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
1940 Returns:
1942 EFI_STATUS
1944 --*/
1946 if (Packet == NULL) {
1947 return EFI_INVALID_PARAMETER;
1950 if (!ValidCdbLength (Packet->CdbLength)) {
1951 return EFI_INVALID_PARAMETER;
1954 if (Packet->Cdb == NULL) {
1955 return EFI_INVALID_PARAMETER;
1959 // Checks whether the request command is supported.
1961 if (!IsExtCommandValid (Packet)) {
1962 return EFI_UNSUPPORTED;
1965 return EFI_SUCCESS;
1969 BOOLEAN
1970 IsExtCommandValid (
1971 EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet
1973 /*++
1975 Routine Description:
1977 Checks the requested SCSI command:
1978 Is it supported by this driver?
1979 Is the Data transfer direction reasonable?
1981 Arguments:
1983 Packet - The pointer of EFI_SCSI_PASS_THRU_SCSI_REQUEST_PACKET
1985 Returns:
1987 EFI_STATUS
1989 --*/
1991 UINT8 Index;
1992 UINT8 *OpCode;
1994 OpCode = (UINT8 *) (Packet->Cdb);
1996 for (Index = 0; CompareMem (&gSupportedATAPICommands[Index], &gEndTable, sizeof (SCSI_COMMAND_SET)); Index++) {
1998 if (*OpCode == gSupportedATAPICommands[Index].OpCode) {
2000 // Check whether the requested Command is supported by this driver
2002 if (Packet->DataDirection == DataIn) {
2004 // Check whether the requested data direction conforms to
2005 // what it should be.
2007 if (gSupportedATAPICommands[Index].Direction == DataOut) {
2008 return FALSE;
2012 if (Packet->DataDirection == DataOut) {
2014 // Check whether the requested data direction conforms to
2015 // what it should be.
2017 if (gSupportedATAPICommands[Index].Direction == DataIn) {
2018 return FALSE;
2022 return TRUE;
2026 return FALSE;
2029 EFI_STATUS
2030 SubmitExtBlockingIoCommand (
2031 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,
2032 UINT8 Target,
2033 EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet
2035 /*++
2037 Routine Description:
2039 Performs blocking I/O request.
2041 Arguments:
2043 AtapiScsiPrivate: Private data structure for the specified channel.
2044 Target: The Target ID of the ATAPI device to send the SCSI
2045 Request Packet. To ATAPI devices attached on an IDE
2046 Channel, Target ID 0 indicates Master device;Target
2047 ID 1 indicates Slave device.
2048 Packet: The SCSI Request Packet to send to the ATAPI device
2049 specified by Target.
2051 Returns: EFI_STATUS
2053 --*/
2055 UINT8 PacketCommand[12];
2056 UINT64 TimeoutInMicroSeconds;
2057 EFI_STATUS PacketCommandStatus;
2060 // Fill ATAPI Command Packet according to CDB
2062 ZeroMem (&PacketCommand, 12);
2063 CopyMem (&PacketCommand, Packet->Cdb, Packet->CdbLength);
2066 // Timeout is 100ns unit, convert it to 1000ns (1us) unit.
2068 TimeoutInMicroSeconds = DivU64x32 (Packet->Timeout, (UINT32) 10);
2071 // Submit ATAPI Command Packet
2073 if (Packet->DataDirection == DataIn) {
2074 PacketCommandStatus = AtapiPacketCommand (
2075 AtapiScsiPrivate,
2076 Target,
2077 PacketCommand,
2078 Packet->InDataBuffer,
2079 &(Packet->InTransferLength),
2080 DataIn,
2081 TimeoutInMicroSeconds
2083 } else {
2085 PacketCommandStatus = AtapiPacketCommand (
2086 AtapiScsiPrivate,
2087 Target,
2088 PacketCommand,
2089 Packet->OutDataBuffer,
2090 &(Packet->OutTransferLength),
2091 DataOut,
2092 TimeoutInMicroSeconds
2096 if (!EFI_ERROR (PacketCommandStatus) || (Packet->SenseData == NULL)) {
2097 Packet->SenseDataLength = 0;
2098 return PacketCommandStatus;
2102 // Return SenseData if PacketCommandStatus matches
2103 // the following return codes.
2105 if ((PacketCommandStatus == EFI_BAD_BUFFER_SIZE) ||
2106 (PacketCommandStatus == EFI_DEVICE_ERROR) ||
2107 (PacketCommandStatus == EFI_TIMEOUT)) {
2110 // avoid submit request sense command continuously.
2112 if (PacketCommand[0] == OP_REQUEST_SENSE) {
2113 Packet->SenseDataLength = 0;
2114 return PacketCommandStatus;
2117 RequestSenseCommand (
2118 AtapiScsiPrivate,
2119 Target,
2120 Packet->Timeout,
2121 Packet->SenseData,
2122 &Packet->SenseDataLength
2126 return PacketCommandStatus;
2130 EFI_STATUS
2131 AtapiPacketCommand (
2132 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,
2133 UINT32 Target,
2134 UINT8 *PacketCommand,
2135 VOID *Buffer,
2136 UINT32 *ByteCount,
2137 DATA_DIRECTION Direction,
2138 UINT64 TimeoutInMicroSeconds
2140 /*++
2142 Routine Description:
2144 Submits ATAPI command packet to the specified ATAPI device.
2146 Arguments:
2148 AtapiScsiPrivate: Private data structure for the specified channel.
2149 Target: The Target ID of the ATAPI device to send the SCSI
2150 Request Packet. To ATAPI devices attached on an IDE
2151 Channel, Target ID 0 indicates Master device;Target
2152 ID 1 indicates Slave device.
2153 PacketCommand: Points to the ATAPI command packet.
2154 Buffer: Points to the transferred data.
2155 ByteCount: When input,indicates the buffer size; when output,
2156 indicates the actually transferred data size.
2157 Direction: Indicates the data transfer direction.
2158 TimeoutInMicroSeconds:
2159 The timeout, in micro second units, to use for the
2160 execution of this ATAPI command.
2161 A TimeoutInMicroSeconds value of 0 means that
2162 this function will wait indefinitely for the ATAPI
2163 command to execute.
2164 If TimeoutInMicroSeconds is greater than zero, then
2165 this function will return EFI_TIMEOUT if the time
2166 required to execute the ATAPI command is greater
2167 than TimeoutInMicroSeconds.
2169 Returns:
2171 EFI_STATUS
2173 --*/
2176 UINT16 *CommandIndex;
2177 UINT8 Count;
2178 EFI_STATUS Status;
2181 // Set all the command parameters by fill related registers.
2182 // Before write to all the following registers, BSY must be 0.
2184 Status = StatusWaitForBSYClear (AtapiScsiPrivate, TimeoutInMicroSeconds);
2185 if (EFI_ERROR (Status)) {
2186 return EFI_DEVICE_ERROR;
2191 // Select device via Device/Head Register.
2192 // "Target = 0" indicates device 0; "Target = 1" indicates device 1
2194 WritePortB (
2195 AtapiScsiPrivate->PciIo,
2196 AtapiScsiPrivate->IoPort->Head,
2197 (UINT8) ((Target << 4) | DEFAULT_CMD) // DEFAULT_CMD: 0xa0 (1010,0000)
2201 // Set all the command parameters by fill related registers.
2202 // Before write to all the following registers, BSY DRQ must be 0.
2204 Status = StatusDRQClear(AtapiScsiPrivate, TimeoutInMicroSeconds);
2206 if (EFI_ERROR (Status)) {
2207 if (Status == EFI_ABORTED) {
2208 Status = EFI_DEVICE_ERROR;
2210 *ByteCount = 0;
2211 return Status;
2215 // No OVL; No DMA (by setting feature register)
2217 WritePortB (
2218 AtapiScsiPrivate->PciIo,
2219 AtapiScsiPrivate->IoPort->Reg1.Feature,
2220 0x00
2224 // set the transfersize to MAX_ATAPI_BYTE_COUNT to let the device
2225 // determine how much data should be transfered.
2227 WritePortB (
2228 AtapiScsiPrivate->PciIo,
2229 AtapiScsiPrivate->IoPort->CylinderLsb,
2230 (UINT8) (MAX_ATAPI_BYTE_COUNT & 0x00ff)
2232 WritePortB (
2233 AtapiScsiPrivate->PciIo,
2234 AtapiScsiPrivate->IoPort->CylinderMsb,
2235 (UINT8) (MAX_ATAPI_BYTE_COUNT >> 8)
2239 // DEFAULT_CTL:0x0a (0000,1010)
2240 // Disable interrupt
2242 WritePortB (
2243 AtapiScsiPrivate->PciIo,
2244 AtapiScsiPrivate->IoPort->Alt.DeviceControl,
2245 DEFAULT_CTL
2249 // Send Packet command to inform device
2250 // that the following data bytes are command packet.
2252 WritePortB (
2253 AtapiScsiPrivate->PciIo,
2254 AtapiScsiPrivate->IoPort->Reg.Command,
2255 PACKET_CMD
2259 // Before data transfer, BSY should be 0 and DRQ should be 1.
2260 // if they are not in specified time frame,
2261 // retrieve Sense Key from Error Register before return.
2263 Status = StatusDRQReady (AtapiScsiPrivate, TimeoutInMicroSeconds);
2264 if (EFI_ERROR (Status)) {
2265 if (Status == EFI_ABORTED) {
2266 Status = EFI_DEVICE_ERROR;
2269 *ByteCount = 0;
2270 return Status;
2274 // Send out command packet
2276 CommandIndex = (UINT16 *) PacketCommand;
2277 for (Count = 0; Count < 6; Count++, CommandIndex++) {
2278 WritePortW (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->Data, *CommandIndex);
2282 // call AtapiPassThruPioReadWriteData() function to get
2283 // requested transfer data form device.
2285 return AtapiPassThruPioReadWriteData (
2286 AtapiScsiPrivate,
2287 Buffer,
2288 ByteCount,
2289 Direction,
2290 TimeoutInMicroSeconds
2294 EFI_STATUS
2295 AtapiPassThruPioReadWriteData (
2296 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,
2297 UINT16 *Buffer,
2298 UINT32 *ByteCount,
2299 DATA_DIRECTION Direction,
2300 UINT64 TimeoutInMicroSeconds
2302 /*++
2304 Routine Description:
2306 Performs data transfer between ATAPI device and host after the
2307 ATAPI command packet is sent.
2309 Arguments:
2311 AtapiScsiPrivate: Private data structure for the specified channel.
2312 Buffer: Points to the transferred data.
2313 ByteCount: When input,indicates the buffer size; when output,
2314 indicates the actually transferred data size.
2315 Direction: Indicates the data transfer direction.
2316 TimeoutInMicroSeconds:
2317 The timeout, in micro second units, to use for the
2318 execution of this ATAPI command.
2319 A TimeoutInMicroSeconds value of 0 means that
2320 this function will wait indefinitely for the ATAPI
2321 command to execute.
2322 If TimeoutInMicroSeconds is greater than zero, then
2323 this function will return EFI_TIMEOUT if the time
2324 required to execute the ATAPI command is greater
2325 than TimeoutInMicroSeconds.
2326 Returns:
2328 EFI_STATUS
2330 --*/
2332 UINT32 Index;
2333 UINT32 RequiredWordCount;
2334 UINT32 ActualWordCount;
2335 UINT32 WordCount;
2336 EFI_STATUS Status;
2337 UINT16 *ptrBuffer;
2339 Status = EFI_SUCCESS;
2342 // Non Data transfer request is also supported.
2344 if (*ByteCount == 0 || Buffer == NULL) {
2345 *ByteCount = 0;
2346 if (EFI_ERROR (StatusWaitForBSYClear (AtapiScsiPrivate, TimeoutInMicroSeconds))) {
2347 return EFI_DEVICE_ERROR;
2351 ptrBuffer = Buffer;
2352 RequiredWordCount = *ByteCount / 2;
2355 // ActuralWordCount means the word count of data really transfered.
2357 ActualWordCount = 0;
2359 while (ActualWordCount < RequiredWordCount) {
2361 // before each data transfer stream, the host should poll DRQ bit ready,
2362 // which indicates device's ready for data transfer .
2364 Status = StatusDRQReady (AtapiScsiPrivate, TimeoutInMicroSeconds);
2365 if (EFI_ERROR (Status)) {
2366 *ByteCount = ActualWordCount * 2;
2368 AtapiPassThruCheckErrorStatus (AtapiScsiPrivate);
2370 if (ActualWordCount == 0) {
2371 return EFI_DEVICE_ERROR;
2374 // ActualWordCount > 0
2376 if (ActualWordCount < RequiredWordCount) {
2377 return EFI_BAD_BUFFER_SIZE;
2381 // get current data transfer size from Cylinder Registers.
2383 WordCount = ReadPortB (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->CylinderMsb) << 8;
2384 WordCount = WordCount | ReadPortB (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->CylinderLsb);
2385 WordCount = WordCount & 0xffff;
2386 WordCount /= 2;
2389 // perform a series data In/Out.
2391 for (Index = 0; (Index < WordCount) && (ActualWordCount < RequiredWordCount); Index++, ActualWordCount++) {
2393 if (Direction == DataIn) {
2395 *ptrBuffer = ReadPortW (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->Data);
2396 } else {
2398 WritePortW (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->Data, *ptrBuffer);
2401 ptrBuffer++;
2406 // After data transfer is completed, normally, DRQ bit should clear.
2408 StatusDRQClear (AtapiScsiPrivate, TimeoutInMicroSeconds);
2411 // read status register to check whether error happens.
2413 Status = AtapiPassThruCheckErrorStatus (AtapiScsiPrivate);
2415 *ByteCount = ActualWordCount * 2;
2417 return Status;
2421 UINT8
2422 ReadPortB (
2423 IN EFI_PCI_IO_PROTOCOL *PciIo,
2424 IN UINT16 Port
2426 /*++
2428 Routine Description:
2430 Read one byte from a specified I/O port.
2432 Arguments:
2434 PciIo - The pointer of EFI_PCI_IO_PROTOCOL
2435 Port - IO port
2437 Returns:
2439 A byte read out
2441 --*/
2443 UINT8 Data;
2445 Data = 0;
2446 PciIo->Io.Read (
2447 PciIo,
2448 EfiPciIoWidthUint8,
2449 EFI_PCI_IO_PASS_THROUGH_BAR,
2450 (UINT64) Port,
2452 &Data
2454 return Data;
2458 UINT16
2459 ReadPortW (
2460 IN EFI_PCI_IO_PROTOCOL *PciIo,
2461 IN UINT16 Port
2463 /*++
2465 Routine Description:
2467 Read one word from a specified I/O port.
2469 Arguments:
2471 PciIo - The pointer of EFI_PCI_IO_PROTOCOL
2472 Port - IO port
2474 Returns:
2476 A word read out
2477 --*/
2479 UINT16 Data;
2481 Data = 0;
2482 PciIo->Io.Read (
2483 PciIo,
2484 EfiPciIoWidthUint16,
2485 EFI_PCI_IO_PASS_THROUGH_BAR,
2486 (UINT64) Port,
2488 &Data
2490 return Data;
2494 VOID
2495 WritePortB (
2496 IN EFI_PCI_IO_PROTOCOL *PciIo,
2497 IN UINT16 Port,
2498 IN UINT8 Data
2500 /*++
2502 Routine Description:
2504 Write one byte to a specified I/O port.
2506 Arguments:
2508 PciIo - The pointer of EFI_PCI_IO_PROTOCOL
2509 Port - IO port
2510 Data - The data to write
2512 Returns:
2514 NONE
2516 --*/
2518 PciIo->Io.Write (
2519 PciIo,
2520 EfiPciIoWidthUint8,
2521 EFI_PCI_IO_PASS_THROUGH_BAR,
2522 (UINT64) Port,
2524 &Data
2529 VOID
2530 WritePortW (
2531 IN EFI_PCI_IO_PROTOCOL *PciIo,
2532 IN UINT16 Port,
2533 IN UINT16 Data
2535 /*++
2537 Routine Description:
2539 Write one word to a specified I/O port.
2541 Arguments:
2543 PciIo - The pointer of EFI_PCI_IO_PROTOCOL
2544 Port - IO port
2545 Data - The data to write
2547 Returns:
2549 NONE
2551 --*/
2553 PciIo->Io.Write (
2554 PciIo,
2555 EfiPciIoWidthUint16,
2556 EFI_PCI_IO_PASS_THROUGH_BAR,
2557 (UINT64) Port,
2559 &Data
2563 EFI_STATUS
2564 StatusDRQClear (
2565 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,
2566 UINT64 TimeoutInMicroSeconds
2568 /*++
2570 Routine Description:
2572 Check whether DRQ is clear in the Status Register. (BSY must also be cleared)
2573 If TimeoutInMicroSeconds is zero, this routine should wait infinitely for
2574 DRQ clear. Otherwise, it will return EFI_TIMEOUT when specified time is
2575 elapsed.
2577 Arguments:
2579 AtapiScsiPrivate - The pointer of ATAPI_SCSI_PASS_THRU_DEV
2580 TimeoutInMicroSeconds - The time to wait for
2582 Returns:
2584 EFI_STATUS
2586 --*/
2588 UINT64 Delay;
2589 UINT8 StatusRegister;
2590 UINT8 ErrRegister;
2592 if (TimeoutInMicroSeconds == 0) {
2593 Delay = 2;
2594 } else {
2595 Delay = DivU64x32 (TimeoutInMicroSeconds, (UINT32) 30) + 1;
2598 do {
2600 StatusRegister = ReadPortB (
2601 AtapiScsiPrivate->PciIo,
2602 AtapiScsiPrivate->IoPort->Reg.Status
2606 // wait for BSY == 0 and DRQ == 0
2608 if ((StatusRegister & (DRQ | BSY)) == 0) {
2609 break;
2612 // check whether the command is aborted by the device
2614 if ((StatusRegister & (BSY | ERR)) == ERR) {
2616 ErrRegister = ReadPortB (
2617 AtapiScsiPrivate->PciIo,
2618 AtapiScsiPrivate->IoPort->Reg1.Error
2620 if ((ErrRegister & ABRT_ERR) == ABRT_ERR) {
2622 return EFI_ABORTED;
2626 // Stall for 30 us
2628 gBS->Stall (30);
2631 // Loop infinitely if not meeting expected condition
2633 if (TimeoutInMicroSeconds == 0) {
2634 Delay = 2;
2637 Delay--;
2638 } while (Delay);
2640 if (Delay == 0) {
2641 return EFI_TIMEOUT;
2644 return EFI_SUCCESS;
2647 EFI_STATUS
2648 AltStatusDRQClear (
2649 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,
2650 UINT64 TimeoutInMicroSeconds
2652 /*++
2654 Routine Description:
2656 Check whether DRQ is clear in the Alternate Status Register.
2657 (BSY must also be cleared).If TimeoutInMicroSeconds is zero, this routine should
2658 wait infinitely for DRQ clear. Otherwise, it will return EFI_TIMEOUT when specified time is
2659 elapsed.
2661 Arguments:
2663 AtapiScsiPrivate - The pointer of ATAPI_SCSI_PASS_THRU_DEV
2664 TimeoutInMicroSeconds - The time to wait for
2666 Returns:
2668 EFI_STATUS
2670 --*/
2672 UINT64 Delay;
2673 UINT8 AltStatusRegister;
2674 UINT8 ErrRegister;
2676 if (TimeoutInMicroSeconds == 0) {
2677 Delay = 2;
2678 } else {
2679 Delay = DivU64x32 (TimeoutInMicroSeconds, (UINT32) 30) + 1;
2682 do {
2684 AltStatusRegister = ReadPortB (
2685 AtapiScsiPrivate->PciIo,
2686 AtapiScsiPrivate->IoPort->Alt.AltStatus
2690 // wait for BSY == 0 and DRQ == 0
2692 if ((AltStatusRegister & (DRQ | BSY)) == 0) {
2693 break;
2696 if ((AltStatusRegister & (BSY | ERR)) == ERR) {
2698 ErrRegister = ReadPortB (
2699 AtapiScsiPrivate->PciIo,
2700 AtapiScsiPrivate->IoPort->Reg1.Error
2702 if ((ErrRegister & ABRT_ERR) == ABRT_ERR) {
2704 return EFI_ABORTED;
2708 // Stall for 30 us
2710 gBS->Stall (30);
2713 // Loop infinitely if not meeting expected condition
2715 if (TimeoutInMicroSeconds == 0) {
2716 Delay = 2;
2719 Delay--;
2720 } while (Delay);
2722 if (Delay == 0) {
2723 return EFI_TIMEOUT;
2726 return EFI_SUCCESS;
2729 EFI_STATUS
2730 StatusDRQReady (
2731 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,
2732 UINT64 TimeoutInMicroSeconds
2734 /*++
2736 Routine Description:
2738 Check whether DRQ is ready in the Status Register. (BSY must also be cleared)
2739 If TimeoutInMicroSeconds is zero, this routine should wait infinitely for
2740 DRQ ready. Otherwise, it will return EFI_TIMEOUT when specified time is
2741 elapsed.
2743 Arguments:
2745 AtapiScsiPrivate - The pointer of ATAPI_SCSI_PASS_THRU_DEV
2746 TimeoutInMicroSeconds - The time to wait for
2748 Returns:
2750 EFI_STATUS
2752 --*/
2754 UINT64 Delay;
2755 UINT8 StatusRegister;
2756 UINT8 ErrRegister;
2758 if (TimeoutInMicroSeconds == 0) {
2759 Delay = 2;
2760 } else {
2761 Delay = DivU64x32 (TimeoutInMicroSeconds, (UINT32) 30) + 1;
2764 do {
2766 // read Status Register will clear interrupt
2768 StatusRegister = ReadPortB (
2769 AtapiScsiPrivate->PciIo,
2770 AtapiScsiPrivate->IoPort->Reg.Status
2774 // BSY==0,DRQ==1
2776 if ((StatusRegister & (BSY | DRQ)) == DRQ) {
2777 break;
2780 if ((StatusRegister & (BSY | ERR)) == ERR) {
2782 ErrRegister = ReadPortB (
2783 AtapiScsiPrivate->PciIo,
2784 AtapiScsiPrivate->IoPort->Reg1.Error
2786 if ((ErrRegister & ABRT_ERR) == ABRT_ERR) {
2787 return EFI_ABORTED;
2792 // Stall for 30 us
2794 gBS->Stall (30);
2797 // Loop infinitely if not meeting expected condition
2799 if (TimeoutInMicroSeconds == 0) {
2800 Delay = 2;
2803 Delay--;
2804 } while (Delay);
2806 if (Delay == 0) {
2807 return EFI_TIMEOUT;
2810 return EFI_SUCCESS;
2813 EFI_STATUS
2814 AltStatusDRQReady (
2815 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,
2816 UINT64 TimeoutInMicroSeconds
2818 /*++
2820 Routine Description:
2822 Check whether DRQ is ready in the Alternate Status Register.
2823 (BSY must also be cleared)
2824 If TimeoutInMicroSeconds is zero, this routine should wait infinitely for
2825 DRQ ready. Otherwise, it will return EFI_TIMEOUT when specified time is
2826 elapsed.
2828 Arguments:
2830 AtapiScsiPrivate - The pointer of ATAPI_SCSI_PASS_THRU_DEV
2831 TimeoutInMicroSeconds - The time to wait for
2833 Returns:
2835 EFI_STATUS
2837 --*/
2839 UINT64 Delay;
2840 UINT8 AltStatusRegister;
2841 UINT8 ErrRegister;
2843 if (TimeoutInMicroSeconds == 0) {
2844 Delay = 2;
2845 } else {
2846 Delay = DivU64x32 (TimeoutInMicroSeconds, (UINT32) 30) + 1;
2849 do {
2851 // read Status Register will clear interrupt
2853 AltStatusRegister = ReadPortB (
2854 AtapiScsiPrivate->PciIo,
2855 AtapiScsiPrivate->IoPort->Alt.AltStatus
2858 // BSY==0,DRQ==1
2860 if ((AltStatusRegister & (BSY | DRQ)) == DRQ) {
2861 break;
2864 if ((AltStatusRegister & (BSY | ERR)) == ERR) {
2866 ErrRegister = ReadPortB (
2867 AtapiScsiPrivate->PciIo,
2868 AtapiScsiPrivate->IoPort->Reg1.Error
2870 if ((ErrRegister & ABRT_ERR) == ABRT_ERR) {
2871 return EFI_ABORTED;
2876 // Stall for 30 us
2878 gBS->Stall (30);
2881 // Loop infinitely if not meeting expected condition
2883 if (TimeoutInMicroSeconds == 0) {
2884 Delay = 2;
2887 Delay--;
2888 } while (Delay);
2890 if (Delay == 0) {
2891 return EFI_TIMEOUT;
2894 return EFI_SUCCESS;
2897 EFI_STATUS
2898 StatusWaitForBSYClear (
2899 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,
2900 UINT64 TimeoutInMicroSeconds
2902 /*++
2904 Routine Description:
2906 Check whether BSY is clear in the Status Register.
2907 If TimeoutInMicroSeconds is zero, this routine should wait infinitely for
2908 BSY clear. Otherwise, it will return EFI_TIMEOUT when specified time is
2909 elapsed.
2911 Arguments:
2913 AtapiScsiPrivate - The pointer of ATAPI_SCSI_PASS_THRU_DEV
2914 TimeoutInMicroSeconds - The time to wait for
2916 Returns:
2918 EFI_STATUS
2920 --*/
2922 UINT64 Delay;
2923 UINT8 StatusRegister;
2925 if (TimeoutInMicroSeconds == 0) {
2926 Delay = 2;
2927 } else {
2928 Delay = DivU64x32 (TimeoutInMicroSeconds, (UINT32) 30) + 1;
2931 do {
2933 StatusRegister = ReadPortB (
2934 AtapiScsiPrivate->PciIo,
2935 AtapiScsiPrivate->IoPort->Reg.Status
2937 if ((StatusRegister & BSY) == 0x00) {
2938 break;
2942 // Stall for 30 us
2944 gBS->Stall (30);
2947 // Loop infinitely if not meeting expected condition
2949 if (TimeoutInMicroSeconds == 0) {
2950 Delay = 2;
2953 Delay--;
2954 } while (Delay);
2956 if (Delay == 0) {
2957 return EFI_TIMEOUT;
2960 return EFI_SUCCESS;
2963 EFI_STATUS
2964 AltStatusWaitForBSYClear (
2965 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,
2966 UINT64 TimeoutInMicroSeconds
2968 /*++
2970 Routine Description:
2972 Check whether BSY is clear in the Alternate Status Register.
2973 If TimeoutInMicroSeconds is zero, this routine should wait infinitely for
2974 BSY clear. Otherwise, it will return EFI_TIMEOUT when specified time is
2975 elapsed.
2977 Arguments:
2979 AtapiScsiPrivate - The pointer of ATAPI_SCSI_PASS_THRU_DEV
2980 TimeoutInMicroSeconds - The time to wait for
2982 Returns:
2984 EFI_STATUS
2986 --*/
2988 UINT64 Delay;
2989 UINT8 AltStatusRegister;
2991 if (TimeoutInMicroSeconds == 0) {
2992 Delay = 2;
2993 } else {
2994 Delay = DivU64x32 (TimeoutInMicroSeconds, (UINT32) 30) + 1;
2997 do {
2999 AltStatusRegister = ReadPortB (
3000 AtapiScsiPrivate->PciIo,
3001 AtapiScsiPrivate->IoPort->Alt.AltStatus
3003 if ((AltStatusRegister & BSY) == 0x00) {
3004 break;
3008 // Stall for 30 us
3010 gBS->Stall (30);
3012 // Loop infinitely if not meeting expected condition
3014 if (TimeoutInMicroSeconds == 0) {
3015 Delay = 2;
3018 Delay--;
3019 } while (Delay);
3021 if (Delay == 0) {
3022 return EFI_TIMEOUT;
3025 return EFI_SUCCESS;
3028 EFI_STATUS
3029 StatusDRDYReady (
3030 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,
3031 UINT64 TimeoutInMicroSeconds
3033 /*++
3035 Routine Description:
3037 Check whether DRDY is ready in the Status Register.
3038 (BSY must also be cleared)
3039 If TimeoutInMicroSeconds is zero, this routine should wait infinitely for
3040 DRDY ready. Otherwise, it will return EFI_TIMEOUT when specified time is
3041 elapsed.
3043 Arguments:
3045 AtapiScsiPrivate - The pointer of ATAPI_SCSI_PASS_THRU_DEV
3046 TimeoutInMicroSeconds - The time to wait for
3048 Returns:
3050 EFI_STATUS
3052 --*/
3054 UINT64 Delay;
3055 UINT8 StatusRegister;
3056 UINT8 ErrRegister;
3058 if (TimeoutInMicroSeconds == 0) {
3059 Delay = 2;
3060 } else {
3061 Delay = DivU64x32 (TimeoutInMicroSeconds, (UINT32) 30) + 1;
3064 do {
3065 StatusRegister = ReadPortB (
3066 AtapiScsiPrivate->PciIo,
3067 AtapiScsiPrivate->IoPort->Reg.Status
3070 // BSY == 0 , DRDY == 1
3072 if ((StatusRegister & (DRDY | BSY)) == DRDY) {
3073 break;
3076 if ((StatusRegister & (BSY | ERR)) == ERR) {
3078 ErrRegister = ReadPortB (
3079 AtapiScsiPrivate->PciIo,
3080 AtapiScsiPrivate->IoPort->Reg1.Error
3082 if ((ErrRegister & ABRT_ERR) == ABRT_ERR) {
3083 return EFI_ABORTED;
3088 // Stall for 30 us
3090 gBS->Stall (30);
3092 // Loop infinitely if not meeting expected condition
3094 if (TimeoutInMicroSeconds == 0) {
3095 Delay = 2;
3098 Delay--;
3099 } while (Delay);
3101 if (Delay == 0) {
3102 return EFI_TIMEOUT;
3105 return EFI_SUCCESS;
3108 EFI_STATUS
3109 AltStatusDRDYReady (
3110 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate,
3111 UINT64 TimeoutInMicroSeconds
3113 /*++
3115 Routine Description:
3117 Check whether DRDY is ready in the Alternate Status Register.
3118 (BSY must also be cleared)
3119 If TimeoutInMicroSeconds is zero, this routine should wait infinitely for
3120 DRDY ready. Otherwise, it will return EFI_TIMEOUT when specified time is
3121 elapsed.
3123 Arguments:
3125 AtapiScsiPrivate - The pointer of ATAPI_SCSI_PASS_THRU_DEV
3126 TimeoutInMicroSeconds - The time to wait for
3128 Returns:
3130 EFI_STATUS
3132 --*/
3134 UINT64 Delay;
3135 UINT8 AltStatusRegister;
3136 UINT8 ErrRegister;
3138 if (TimeoutInMicroSeconds == 0) {
3139 Delay = 2;
3140 } else {
3141 Delay = DivU64x32 (TimeoutInMicroSeconds, (UINT32) 30) + 1;
3144 do {
3145 AltStatusRegister = ReadPortB (
3146 AtapiScsiPrivate->PciIo,
3147 AtapiScsiPrivate->IoPort->Alt.AltStatus
3150 // BSY == 0 , DRDY == 1
3152 if ((AltStatusRegister & (DRDY | BSY)) == DRDY) {
3153 break;
3156 if ((AltStatusRegister & (BSY | ERR)) == ERR) {
3158 ErrRegister = ReadPortB (
3159 AtapiScsiPrivate->PciIo,
3160 AtapiScsiPrivate->IoPort->Reg1.Error
3162 if ((ErrRegister & ABRT_ERR) == ABRT_ERR) {
3163 return EFI_ABORTED;
3168 // Stall for 30 us
3170 gBS->Stall (30);
3172 // Loop infinitely if not meeting expected condition
3174 if (TimeoutInMicroSeconds == 0) {
3175 Delay = 2;
3178 Delay--;
3179 } while (Delay);
3181 if (Delay == 0) {
3182 return EFI_TIMEOUT;
3185 return EFI_SUCCESS;
3188 EFI_STATUS
3189 AtapiPassThruCheckErrorStatus (
3190 ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate
3192 /*++
3194 Routine Description:
3196 Check Error Register for Error Information.
3198 Arguments:
3200 AtapiScsiPrivate - The pointer of ATAPI_SCSI_PASS_THRU_DEV
3202 Returns:
3204 EFI_STATUS
3206 --*/
3208 UINT8 StatusRegister;
3209 UINT8 ErrorRegister;
3211 StatusRegister = ReadPortB (
3212 AtapiScsiPrivate->PciIo,
3213 AtapiScsiPrivate->IoPort->Reg.Status
3216 DEBUG_CODE_BEGIN ();
3218 if (StatusRegister & DWF) {
3219 DEBUG (
3220 (EFI_D_BLKIO,
3221 "AtapiPassThruCheckErrorStatus()-- %02x : Error : Write Fault\n",
3222 StatusRegister)
3226 if (StatusRegister & CORR) {
3227 DEBUG (
3228 (EFI_D_BLKIO,
3229 "AtapiPassThruCheckErrorStatus()-- %02x : Error : Corrected Data\n",
3230 StatusRegister)
3234 if (StatusRegister & ERR) {
3235 ErrorRegister = ReadPortB (AtapiScsiPrivate->PciIo, AtapiScsiPrivate->IoPort->Reg1.Error);
3238 if (ErrorRegister & BBK_ERR) {
3239 DEBUG (
3240 (EFI_D_BLKIO,
3241 "AtapiPassThruCheckErrorStatus()-- %02x : Error : Bad Block Detected\n",
3242 ErrorRegister)
3246 if (ErrorRegister & UNC_ERR) {
3247 DEBUG (
3248 (EFI_D_BLKIO,
3249 "AtapiPassThruCheckErrorStatus()-- %02x : Error : Uncorrectable Data\n",
3250 ErrorRegister)
3254 if (ErrorRegister & MC_ERR) {
3255 DEBUG (
3256 (EFI_D_BLKIO,
3257 "AtapiPassThruCheckErrorStatus()-- %02x : Error : Media Change\n",
3258 ErrorRegister)
3262 if (ErrorRegister & ABRT_ERR) {
3263 DEBUG (
3264 (EFI_D_BLKIO,
3265 "AtapiPassThruCheckErrorStatus()-- %02x : Error : Abort\n",
3266 ErrorRegister)
3270 if (ErrorRegister & TK0NF_ERR) {
3271 DEBUG (
3272 (EFI_D_BLKIO,
3273 "AtapiPassThruCheckErrorStatus()-- %02x : Error : Track 0 Not Found\n",
3274 ErrorRegister)
3278 if (ErrorRegister & AMNF_ERR) {
3279 DEBUG (
3280 (EFI_D_BLKIO,
3281 "AtapiPassThruCheckErrorStatus()-- %02x : Error : Address Mark Not Found\n",
3282 ErrorRegister)
3287 DEBUG_CODE_END ();
3289 if ((StatusRegister & (ERR | DWF | CORR)) == 0) {
3290 return EFI_SUCCESS;
3294 return EFI_DEVICE_ERROR;
3299 Installs Scsi Pass Thru and/or Ext Scsi Pass Thru
3300 protocols based on feature flags.
3302 @param Controller The controller handle to
3303 install these protocols on.
3304 @param AtapiScsiPrivate A pointer to the protocol private
3305 data structure.
3307 @retval EFI_SUCCESS The installation succeeds.
3308 @retval other The installation fails.
3311 EFI_STATUS
3312 InstallScsiPassThruProtocols (
3313 IN EFI_HANDLE *ControllerHandle,
3314 IN ATAPI_SCSI_PASS_THRU_DEV *AtapiScsiPrivate
3317 EFI_STATUS Status;
3318 EFI_SCSI_PASS_THRU_PROTOCOL *ScsiPassThru;
3319 EFI_EXT_SCSI_PASS_THRU_PROTOCOL *ExtScsiPassThru;
3321 ScsiPassThru = &AtapiScsiPrivate->ScsiPassThru;
3322 ExtScsiPassThru = &AtapiScsiPrivate->ExtScsiPassThru;
3324 if (FeaturePcdGet (PcdSupportScsiPassThru)) {
3325 ScsiPassThru = CopyMem (ScsiPassThru, &gScsiPassThruProtocolTemplate, sizeof (*ScsiPassThru));
3326 if (FeaturePcdGet (PcdSupportExtScsiPassThru)) {
3327 ExtScsiPassThru = CopyMem (ExtScsiPassThru, &gExtScsiPassThruProtocolTemplate, sizeof (*ExtScsiPassThru));
3328 Status = gBS->InstallMultipleProtocolInterfaces (
3329 ControllerHandle,
3330 &gEfiScsiPassThruProtocolGuid,
3331 ScsiPassThru,
3332 &gEfiExtScsiPassThruProtocolGuid,
3333 ExtScsiPassThru,
3334 NULL
3336 } else {
3337 Status = gBS->InstallMultipleProtocolInterfaces (
3338 ControllerHandle,
3339 &gEfiScsiPassThruProtocolGuid,
3340 ScsiPassThru,
3341 NULL
3344 } else {
3345 if (FeaturePcdGet (PcdSupportExtScsiPassThru)) {
3346 ExtScsiPassThru = CopyMem (ExtScsiPassThru, &gExtScsiPassThruProtocolTemplate, sizeof (*ExtScsiPassThru));
3347 Status = gBS->InstallMultipleProtocolInterfaces (
3348 ControllerHandle,
3349 &gEfiExtScsiPassThruProtocolGuid,
3350 ExtScsiPassThru,
3351 NULL
3353 } else {
3355 // This driver must support either ScsiPassThru or
3356 // ExtScsiPassThru protocols
3358 ASSERT (FALSE);
3359 Status = EFI_UNSUPPORTED;
3363 return Status;
3367 The user Entry Point for module AtapiPassThru. The user code starts with this function.
3369 @param[in] ImageHandle The firmware allocated handle for the EFI image.
3370 @param[in] SystemTable A pointer to the EFI System Table.
3372 @retval EFI_SUCCESS The entry point is executed successfully.
3373 @retval other Some error occurs when executing this entry point.
3376 EFI_STATUS
3377 EFIAPI
3378 InitializeAtapiPassThru(
3379 IN EFI_HANDLE ImageHandle,
3380 IN EFI_SYSTEM_TABLE *SystemTable
3383 EFI_STATUS Status;
3386 // Install driver model protocol(s).
3388 Status = EfiLibInstallDriverBindingComponentName2 (
3389 ImageHandle,
3390 SystemTable,
3391 &gAtapiScsiPassThruDriverBinding,
3392 ImageHandle,
3393 &gAtapiScsiPassThruComponentName,
3394 &gAtapiScsiPassThruComponentName2
3396 ASSERT_EFI_ERROR (Status);
3399 // Install EFI Driver Supported EFI Version Protocol required for
3400 // EFI drivers that are on PCI and other plug in cards.
3402 gAtapiScsiPassThruDriverSupportedEfiVersion.FirmwareVersion = PcdGet32 (PcdDriverSupportedEfiVersion);
3403 Status = gBS->InstallMultipleProtocolInterfaces (
3404 &ImageHandle,
3405 &gEfiDriverSupportedEfiVersionProtocolGuid,
3406 &gAtapiScsiPassThruDriverSupportedEfiVersion,
3407 NULL
3409 ASSERT_EFI_ERROR (Status);
3411 return Status;