1 /* $Id: ide.c,v 1.55 2002/09/07 15:11:58 chorns Exp $
3 * IDE.C - IDE Disk driver
4 * written by Rex Jolliff
5 * with help from various documentation sources and a few peeks at
6 * linux and freebsd sources.
8 * This driver supports PCI controllers and up to 4 ISA controllers
9 * with up to 2 drives each.
10 * The device names are assigned as follows:
11 * \Devices\HarddiskX\Partition0
12 * for the raw device, and
13 * \Devices\HarddiskX\PartitionY
16 * X is computed by counting the available drives from the following
17 * sequence: the controller number (0=0x1f0, 1=0x170, 2=0x1e8,
18 * 3=0x168) * 2 plus the drive number (0,1)
19 * Y is the partition number
21 * The driver exports the following function:
23 * DriverEntry() - NT device driver initialization routine
25 * And the following functions are exported implicitly:
27 * IDEStartIo() - called to start an I/O request packet
28 * IDEDispatchOpenClose() - Called to open/close the device. a NOOP
29 * IDEDispatchReadWrite() - Called to read/write the device.
30 * IDEDispatchQueryInformation() - Called to get device information
31 * IDEDispatchSetInformation() - Called to set device information
32 * IDEDispatchDeviceControl() - Called to execute device control requests
34 * Modification History:
35 * 05/25/98 RJJ Created.
36 * 05/30/98 RJJ Removed IRQ handler and inserted busy waits
37 * just to get something working...
38 * 07/18/98 RJJ Made drastic changes so that the driver
39 * resembles a WinNT driver.
40 * 08/05/98 RJJ Changed to .C extension
41 * 09/19/98 RJJ First release (run for cover!)
44 * 09/17/98 RJJ Pri/MST: 14.12X19 WDC AC31000H Test Passed
49 * FIXME: a timer should be used to watch for device timeouts and errors
50 * FIXME: errors should be retried
51 * FIXME: a drive reset/recalibrate should be attempted if errors occur
52 * FIXME: Use DMA for transfers if drives support it
53 * FIXME: should we support unloading of this driver???
54 * FIXME: the device information should come from AUTODETECT (via registry)
55 * FIXME: really big devices need to be handled correctly
56 * FIXME: should get device info from the registry
57 * FIXME: should report hardware usage to iomgr
58 * FIXME: finish implementation of QueryInformation
59 * FIXME: finish implementation of SetInformation
60 * FIXME: finish implementation of DeviceControl
61 * FIXME: bring up to ATA-3 spec
62 * FIXME: add general support for ATAPI devices
63 * FIXME: add support for ATAPI CDROMs
64 * FIXME: add support for ATAPI ZIP drives/RHDs
65 * FIXME: add support for ATAPI tape drives
68 // -------------------------------------------------------------------------
70 #include <ddk/ntddk.h>
79 #define VERSION "V0.1.5"
81 /* uncomment the following line to enable the secondary ide channel */
82 //#define ENABLE_SECONDARY_IDE_CHANNEL
84 // ------------------------------------------------------- File Static Data
87 typedef struct _IDE_CONTROLLER_PARAMETERS
96 KINTERRUPT_MODE InterruptMode
;
98 } IDE_CONTROLLER_PARAMETERS
, *PIDE_CONTROLLER_PARAMETERS
;
100 // NOTE: Do not increase max drives above 2
102 #define IDE_MAX_DRIVES 2
104 #define IDE_MAX_CONTROLLERS 2
105 IDE_CONTROLLER_PARAMETERS Controllers
[IDE_MAX_CONTROLLERS
] =
107 {0x01f0, 8, 0x03f6, 1, 14, 14, 15, LevelSensitive
, 0xffff},
108 {0x0170, 8, 0x0376, 1, 15, 15, 15, LevelSensitive
, 0xffff}
109 /* {0x01E8, 8, 0x03ee, 1, 11, 11, 15, LevelSensitive, 0xffff},
110 {0x0168, 8, 0x036e, 1, 10, 10, 15, LevelSensitive, 0xffff}*/
113 static BOOLEAN IDEInitialized
= FALSE
;
115 // ----------------------------------------------- Discardable Declarations
119 // make the initialization routines discardable, so that they
122 #pragma alloc_text(init, DriverEntry)
123 #pragma alloc_text(init, IDECreateController)
124 #pragma alloc_text(init, IDECreateDevices)
125 #pragma alloc_text(init, IDECreateDevice)
126 #pragma alloc_text(init, IDEPolledRead)
128 // make the PASSIVE_LEVEL routines pageable, so that they don't
129 // waste nonpaged memory
131 #pragma alloc_text(page, IDEShutdown)
132 #pragma alloc_text(page, IDEDispatchOpenClose)
133 #pragma alloc_text(page, IDEDispatchRead)
134 #pragma alloc_text(page, IDEDispatchWrite)
136 #endif /* ALLOC_PRAGMA */
138 // ---------------------------------------------------- Forward Declarations
141 IdeFindControllers(IN PDRIVER_OBJECT DriverObject
);
144 IdeCreateController(IN PDRIVER_OBJECT DriverObject
,
145 IN PIDE_CONTROLLER_PARAMETERS ControllerParams
,
146 IN
int ControllerIdx
);
148 static BOOLEAN
IDEResetController(IN WORD CommandPort
, IN WORD ControlPort
);
149 static BOOLEAN
IDECreateDevices(IN PDRIVER_OBJECT DriverObject
,
150 IN PCONTROLLER_OBJECT ControllerObject
,
151 IN PIDE_CONTROLLER_EXTENSION ControllerExtension
,
154 static BOOLEAN
IDEGetDriveIdentification(IN
int CommandPort
,
156 OUT PIDE_DRIVE_IDENTIFY DrvParms
);
157 static NTSTATUS
IDECreateDiskDevice(IN PDRIVER_OBJECT DriverObject
,
158 OUT PDEVICE_OBJECT
*DeviceObject
,
159 IN PCONTROLLER_OBJECT ControllerObject
,
162 IN PIDE_DRIVE_IDENTIFY DrvParms
,
163 IN ULONG SectorCount
);
164 static NTSTATUS
IDECreatePartitionDevice(IN PDRIVER_OBJECT DriverObject
,
165 OUT PDEVICE_OBJECT
*DeviceObject
,
166 IN PCONTROLLER_OBJECT ControllerObject
,
167 IN PVOID DiskDeviceExtension
,
170 IN PIDE_DRIVE_IDENTIFY DrvParms
,
171 IN PPARTITION_INFORMATION PartitionInfo
);
172 static int IDEPolledRead(IN WORD Address
,
177 IN BYTE CylinderHigh
,
181 static NTSTATUS STDCALL
IDEDispatchOpenClose(IN PDEVICE_OBJECT pDO
, IN PIRP Irp
);
182 static NTSTATUS STDCALL
IDEDispatchReadWrite(IN PDEVICE_OBJECT pDO
, IN PIRP Irp
);
183 static NTSTATUS STDCALL
IDEDispatchDeviceControl(IN PDEVICE_OBJECT pDO
, IN PIRP Irp
);
184 static VOID STDCALL
IDEStartIo(IN PDEVICE_OBJECT DeviceObject
, IN PIRP Irp
);
185 static IO_ALLOCATION_ACTION STDCALL
186 IDEAllocateController(IN PDEVICE_OBJECT DeviceObject
,
188 IN PVOID MapRegisterBase
,
190 static BOOLEAN STDCALL
191 IDEStartController(IN OUT PVOID Context
);
192 VOID
IDEBeginControllerReset(PIDE_CONTROLLER_EXTENSION ControllerExtension
);
193 static BOOLEAN STDCALL
IDEIsr(IN PKINTERRUPT Interrupt
, IN PVOID ServiceContext
);
194 static VOID
IDEDpcForIsr(IN PKDPC Dpc
,
195 IN PDEVICE_OBJECT DpcDeviceObject
,
197 IN PVOID DpcContext
);
198 static VOID
IDEFinishOperation(PIDE_CONTROLLER_EXTENSION ControllerExtension
);
199 static VOID STDCALL
IDEIoTimer(PDEVICE_OBJECT DeviceObject
, PVOID Context
);
201 // ---------------------------------------------------------------- Inlines
204 IDESwapBytePairs(char *Buf
,
210 for (i
= 0; i
< Cnt
; i
+= 2)
218 // ------------------------------------------------------- Public Interface
223 // This function initializes the driver, locates and claims
224 // hardware resources, and creates various NT objects needed
225 // to process I/O requests.
231 // IN PDRIVER_OBJECT DriverObject System allocated Driver Object
233 // IN PUNICODE_STRING RegistryPath Name of registry driver service
240 DriverEntry(IN PDRIVER_OBJECT DriverObject
,
241 IN PUNICODE_STRING RegistryPath
)
245 DPRINT("IDE Driver %s\n", VERSION
);
247 /* Export other driver entry points... */
248 DriverObject
->DriverStartIo
= IDEStartIo
;
249 DriverObject
->MajorFunction
[IRP_MJ_CREATE
] = IDEDispatchOpenClose
;
250 DriverObject
->MajorFunction
[IRP_MJ_CLOSE
] = IDEDispatchOpenClose
;
251 DriverObject
->MajorFunction
[IRP_MJ_READ
] = IDEDispatchReadWrite
;
252 DriverObject
->MajorFunction
[IRP_MJ_WRITE
] = IDEDispatchReadWrite
;
253 // DriverObject->MajorFunction[IRP_MJ_QUERY_INFORMATION] = IDEDispatchQueryInformation;
254 // DriverObject->MajorFunction[IRP_MJ_SET_INFORMATION] = IDEDispatchSetInformation;
255 DriverObject
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = IDEDispatchDeviceControl
;
257 Status
= IdeFindControllers(DriverObject
);
258 if (NT_SUCCESS(Status
))
260 IDEInitialized
= TRUE
;
266 // ---------------------------------------------------- Discardable statics
269 IdeFindControllers(IN PDRIVER_OBJECT DriverObject
)
271 PCI_COMMON_CONFIG PciConfig
;
276 NTSTATUS ReturnedStatus
= STATUS_NO_SUCH_DEVICE
;
278 INT ControllerIdx
= 0;
279 PCONFIGURATION_INFORMATION ConfigInfo
;
281 DPRINT("IdeFindControllers() called!\n");
283 ConfigInfo
= IoGetConfigurationInformation();
285 /* Search PCI busses for IDE controllers */
286 for (Bus
= 0; Bus
< 8; Bus
++)
288 for (Slot
= 0; Slot
< 256; Slot
++)
290 Size
= HalGetBusData(PCIConfiguration
,
294 sizeof(PCI_COMMON_CONFIG
));
297 if ((PciConfig
.BaseClass
== 0x01) &&
298 (PciConfig
.SubClass
== 0x01))
300 DPRINT("IDE controller found!\n");
302 DPRINT("Bus %1lu Device %2lu Func %1lu VenID 0x%04hx DevID 0x%04hx\n",
308 if ((PciConfig
.HeaderType
& 0x7FFFFFFF) == 0)
310 DPRINT(" IPR 0x%X ILR 0x%X\n",
311 PciConfig
.u
.type0
.InterruptPin
,
312 PciConfig
.u
.type0
.InterruptLine
);
315 if (PciConfig
.ProgIf
& 0x01)
317 DPRINT("Primary channel: PCI native mode\n");
321 DPRINT("Primary channel: Compatibility mode\n");
322 if (ConfigInfo
->AtDiskPrimaryAddressClaimed
== FALSE
)
324 Status
= IdeCreateController(DriverObject
,
327 if (NT_SUCCESS(Status
))
330 ConfigInfo
->AtDiskPrimaryAddressClaimed
= TRUE
;
331 ConfigInfo
->ScsiPortCount
++;
332 ReturnedStatus
= Status
;
338 * FIXME: Switch controller to native pci mode
339 * if it is programmable.
343 if (PciConfig
.ProgIf
& 0x02)
345 DPRINT("Primary channel: programmable\n");
349 DPRINT("Primary channel: not programmable\n");
352 #ifdef ENABLE_SECONDARY_IDE_CHANNEL
353 if (PciConfig
.ProgIf
& 0x04)
355 DPRINT("Secondary channel: PCI native mode\n");
359 DPRINT("Secondary channel: Compatibility mode\n");
360 if (ConfigInfo
->AtDiskSecondaryAddressClaimed
== FALSE
)
362 Status
= IdeCreateController(DriverObject
,
365 if (NT_SUCCESS(Status
))
368 ConfigInfo
->AtDiskSecondaryAddressClaimed
= TRUE
;
369 ConfigInfo
->ScsiPortCount
++;
370 ReturnedStatus
= Status
;
376 * FIXME: Switch controller to native pci mode
377 * if it is programmable.
381 if (PciConfig
.ProgIf
& 0x08)
383 DPRINT("Secondary channel: programmable\n");
387 DPRINT("Secondary channel: not programmable\n");
390 if (PciConfig
.ProgIf
& 0x80)
392 DPRINT("Master IDE device: 1\n");
396 DPRINT("Master IDE device: 0\n");
399 for (i
= 0; i
< PCI_TYPE0_ADDRESSES
; i
++)
401 DPRINT("BaseAddress: 0x%08X\n", PciConfig
.u
.type0
.BaseAddresses
[i
]);
409 /* Search for ISA IDE controller if no primary controller was found */
410 if (ConfigInfo
->AtDiskPrimaryAddressClaimed
== FALSE
)
412 DPRINT("Searching for primary ISA IDE controller!\n");
414 if (IDEResetController(Controllers
[0].CommandPortBase
,
415 Controllers
[0].ControlPortBase
))
417 Status
= IdeCreateController(DriverObject
,
420 if (NT_SUCCESS(Status
))
422 DPRINT(" Found primary ISA IDE controller!\n");
424 ConfigInfo
->AtDiskPrimaryAddressClaimed
= TRUE
;
425 ConfigInfo
->ScsiPortCount
++;
426 ReturnedStatus
= Status
;
431 /* Search for ISA IDE controller if no secondary controller was found */
432 #ifdef ENABLE_SECONDARY_IDE_CHANNEL
433 if (ConfigInfo
->AtDiskSecondaryAddressClaimed
== FALSE
)
435 DPRINT("Searching for secondary ISA IDE controller!\n");
437 if (IDEResetController(Controllers
[1].CommandPortBase
,
438 Controllers
[1].ControlPortBase
))
440 Status
= IdeCreateController(DriverObject
,
443 if (NT_SUCCESS(Status
))
445 DPRINT(" Found secondary ISA IDE controller!\n");
447 ConfigInfo
->AtDiskSecondaryAddressClaimed
= TRUE
;
448 ConfigInfo
->ScsiPortCount
++;
449 ReturnedStatus
= Status
;
455 DPRINT("IdeFindControllers() done!\n");
457 return(ReturnedStatus
);
461 // IdeCreateController
464 // Creates a controller object and a device object for each valid
465 // device on the controller
471 // IN PDRIVER_OBJECT DriverObject The system created driver object
472 // IN PIDE_CONTROLLER_PARAMETERS The parameter block for this
473 // ControllerParams controller
474 // IN int ControllerIdx The index of this controller
477 // TRUE Devices where found on this controller
478 // FALSE The controller does not respond or there are no devices on it
482 IdeCreateController(IN PDRIVER_OBJECT DriverObject
,
483 IN PIDE_CONTROLLER_PARAMETERS ControllerParams
,
484 IN
int ControllerIdx
)
486 BOOLEAN CreatedDevices
, ThisDriveExists
;
489 PCONTROLLER_OBJECT ControllerObject
;
490 PIDE_CONTROLLER_EXTENSION ControllerExtension
;
492 ControllerObject
= IoCreateController(sizeof(IDE_CONTROLLER_EXTENSION
));
493 if (ControllerObject
== NULL
)
495 DbgPrint ("Could not create controller object for controller %d\n",
497 return STATUS_NO_SUCH_DEVICE
;
500 // Fill out Controller extension data
501 ControllerExtension
= (PIDE_CONTROLLER_EXTENSION
)
502 ControllerObject
->ControllerExtension
;
503 ControllerExtension
->Number
= ControllerIdx
;
504 ControllerExtension
->CommandPortBase
= ControllerParams
->CommandPortBase
;
505 ControllerExtension
->ControlPortBase
= ControllerParams
->ControlPortBase
;
506 ControllerExtension
->Vector
= ControllerParams
->Vector
;
507 ControllerExtension
->DMASupported
= FALSE
;
508 ControllerExtension
->ControllerInterruptBug
= FALSE
;
509 ControllerExtension
->OperationInProgress
= FALSE
;
511 // Initialize the spin lock in the controller extension
512 KeInitializeSpinLock(&ControllerExtension
->SpinLock
);
514 // Register an interrupt handler for this controller
515 RC
= IoConnectInterrupt(&ControllerExtension
->Interrupt
,
518 &ControllerExtension
->SpinLock
,
519 ControllerExtension
->Vector
,
520 ControllerParams
->IrqL
,
521 ControllerParams
->SynchronizeIrqL
,
522 ControllerParams
->InterruptMode
,
524 ControllerParams
->Affinity
,
528 DbgPrint ("Could not Connect Interrupt %d\n",
529 ControllerExtension
->Vector
);
530 IoDeleteController (ControllerObject
);
535 IDEInitialized
= TRUE
;
537 // Create device objects for each raw device (and for partitions)
538 CreatedDevices
= FALSE
;
539 for (DriveIdx
= 0; DriveIdx
< IDE_MAX_DRIVES
; DriveIdx
++)
541 ThisDriveExists
= IDECreateDevices(DriverObject
,
545 ControllerIdx
* 2 + DriveIdx
);
548 CreatedDevices
= TRUE
;
554 DbgPrint ("Did not find any devices for controller %d\n",
556 IoDisconnectInterrupt (ControllerExtension
->Interrupt
);
557 IoDeleteController (ControllerObject
);
561 IDEResetController(ControllerParams
->CommandPortBase
,
562 ControllerParams
->ControlPortBase
);
563 IoStartTimer(ControllerExtension
->TimerDevice
);
566 return((CreatedDevices
== TRUE
)?STATUS_SUCCESS
:STATUS_NO_SUCH_DEVICE
);
570 // IDEResetController
573 // Reset the controller and report completion status
579 // IN WORD CommandPort The address of the command port
580 // IN WORD ControlPort The address of the control port
586 IDEResetController(IN WORD CommandPort
,
591 // Assert drive reset line
592 IDEWriteDriveControl(ControlPort
, IDE_DC_SRST
);
594 // Wait for min. 25 microseconds
595 KeStallExecutionProcessor(IDE_RESET_PULSE_LENGTH
);
597 // Negate drive reset line
598 IDEWriteDriveControl(ControlPort
, 0);
600 // Wait for BUSY negation
601 for (Retries
= 0; Retries
< IDE_RESET_BUSY_TIMEOUT
* 1000; Retries
++)
603 if (!(IDEReadStatus(CommandPort
) & IDE_SR_BUSY
))
607 KeStallExecutionProcessor(10);
610 if (Retries
>= IDE_RESET_BUSY_TIMEOUT
* 1000)
615 // return TRUE if controller came back to life. and
616 // the registers are initialized correctly
617 return IDEReadError(CommandPort
) == 1;
624 // Create the raw device and any partition devices on this drive
630 // IN PDRIVER_OBJECT DriverObject The system created driver object
631 // IN PCONTROLLER_OBJECT ControllerObject
632 // IN PIDE_CONTROLLER_EXTENSION ControllerExtension
633 // The IDE controller extension for
635 // IN int DriveIdx The index of the drive on this
637 // IN int HarddiskIdx The NT device number for this
641 // TRUE Drive exists and devices were created
642 // FALSE no devices were created for this device
646 IDECreateDevices(IN PDRIVER_OBJECT DriverObject
,
647 IN PCONTROLLER_OBJECT ControllerObject
,
648 IN PIDE_CONTROLLER_EXTENSION ControllerExtension
,
652 WCHAR NameBuffer
[IDE_MAX_NAME_LENGTH
];
655 IDE_DRIVE_IDENTIFY DrvParms
;
656 PDEVICE_OBJECT DiskDeviceObject
;
657 PDEVICE_OBJECT PartitionDeviceObject
;
658 PIDE_DEVICE_EXTENSION DiskDeviceExtension
;
659 UNICODE_STRING UnicodeDeviceDirName
;
660 OBJECT_ATTRIBUTES DeviceDirAttributes
;
662 ULONG SectorCount
= 0;
663 PDRIVE_LAYOUT_INFORMATION PartitionList
= NULL
;
664 PPARTITION_INFORMATION PartitionEntry
;
667 /* Copy I/O port offsets for convenience */
668 CommandPort
= ControllerExtension
->CommandPortBase
;
669 // ControlPort = ControllerExtension->ControlPortBase;
670 DPRINT("probing IDE controller %d Addr %04lx Drive %d\n",
671 ControllerExtension
->Number
,
675 /* Get the Drive Identification Data */
676 if (!IDEGetDriveIdentification(CommandPort
, DriveIdx
, &DrvParms
))
678 DPRINT("No ATA drive %d found on controller %d...\n",
680 ControllerExtension
->Number
);
684 DPRINT("Found ATA drive %d on controller %d...\n",
686 ControllerExtension
->Number
);
688 /* Create the harddisk device directory */
689 swprintf (NameBuffer
,
690 L
"\\Device\\Harddisk%d",
692 RtlInitUnicodeString(&UnicodeDeviceDirName
,
694 InitializeObjectAttributes(&DeviceDirAttributes
,
695 &UnicodeDeviceDirName
,
699 Status
= ZwCreateDirectoryObject(&Handle
, 0, &DeviceDirAttributes
);
700 if (!NT_SUCCESS(Status
))
702 DbgPrint("Could not create device dir object\n");
706 /* Create the disk device */
707 if (DrvParms
.Capabilities
& IDE_DRID_LBA_SUPPORTED
)
710 (ULONG
)((DrvParms
.TMSectorCountHi
<< 16) + DrvParms
.TMSectorCountLo
);
715 (ULONG
)(DrvParms
.LogicalCyls
* DrvParms
.LogicalHeads
* DrvParms
.SectorsPerTrack
);
717 DPRINT("SectorCount %lu\n", SectorCount
);
719 Status
= IDECreateDiskDevice(DriverObject
,
726 if (!NT_SUCCESS(Status
))
728 DbgPrint("IDECreateDevice call failed for raw device\n");
732 /* Increase number of available physical disk drives */
733 IoGetConfigurationInformation()->DiskCount
++;
736 * Initialize the controller timer here
737 * (since it has to be tied to a device)
741 ControllerExtension
->TimerState
= IDETimerIdle
;
742 ControllerExtension
->TimerCount
= 0;
743 ControllerExtension
->TimerDevice
= DiskDeviceObject
;
744 IoInitializeTimer(DiskDeviceObject
,
746 ControllerExtension
);
749 DPRINT("DrvParms.BytesPerSector %ld\n",DrvParms
.BytesPerSector
);
751 /* Read partition table */
752 Status
= IoReadPartitionTable(DiskDeviceObject
,
753 DrvParms
.BytesPerSector
,
756 if (!NT_SUCCESS(Status
))
758 DbgPrint("IoReadPartitionTable() failed\n");
762 DPRINT(" Number of partitions: %u\n", PartitionList
->PartitionCount
);
763 for (i
=0;i
< PartitionList
->PartitionCount
; i
++)
765 PartitionEntry
= &PartitionList
->PartitionEntry
[i
];
767 DPRINT("Partition %02ld: nr: %d boot: %1x type: %x offset: %I64d size: %I64d\n",
769 PartitionEntry
->PartitionNumber
,
770 PartitionEntry
->BootIndicator
,
771 PartitionEntry
->PartitionType
,
772 PartitionEntry
->StartingOffset
.QuadPart
/ DrvParms
.BytesPerSector
,
773 PartitionEntry
->PartitionLength
.QuadPart
/ DrvParms
.BytesPerSector
);
775 /* Create device for partition */
776 Status
= IDECreatePartitionDevice(DriverObject
,
777 &PartitionDeviceObject
,
779 DiskDeviceObject
->DeviceExtension
,
784 if (!NT_SUCCESS(Status
))
786 DbgPrint("IDECreateDevice() failed\n");
791 if (PartitionList
!= NULL
)
792 ExFreePool(PartitionList
);
797 // IDEGetDriveIdentification
800 // Get the identification block from the drive
806 // IN int CommandPort Address of the command port
807 // IN int DriveNum The drive index (0,1)
808 // OUT PIDE_DRIVE_IDENTIFY DrvParms Address to write drive ident block
811 // TRUE The drive identification block was retrieved successfully
815 IDEGetDriveIdentification(IN
int CommandPort
,
817 OUT PIDE_DRIVE_IDENTIFY DrvParms
)
820 /* Get the Drive Identify block from drive or die */
821 if (IDEPolledRead(CommandPort
, 0, 1, 0, 0, 0, (DriveNum
? IDE_DH_DRV1
: 0),
822 IDE_CMD_IDENT_DRV
, (BYTE
*)DrvParms
) != 0)
827 /* Report on drive parameters if debug mode */
828 IDESwapBytePairs(DrvParms
->SerialNumber
, 20);
829 IDESwapBytePairs(DrvParms
->FirmwareRev
, 8);
830 IDESwapBytePairs(DrvParms
->ModelNumber
, 40);
831 DPRINT("Config:%04x Cyls:%5d Heads:%2d Sectors/Track:%3d Gaps:%02d %02d\n",
832 DrvParms
->ConfigBits
,
833 DrvParms
->LogicalCyls
,
834 DrvParms
->LogicalHeads
,
835 DrvParms
->SectorsPerTrack
,
836 DrvParms
->InterSectorGap
,
837 DrvParms
->InterSectorGapSize
);
838 DPRINT("Bytes/PLO:%3d Vendor Cnt:%2d Serial number:[%.20s]\n",
839 DrvParms
->BytesInPLO
,
840 DrvParms
->VendorUniqueCnt
,
841 DrvParms
->SerialNumber
);
842 DPRINT("Cntlr type:%2d BufSiz:%5d ECC bytes:%3d Firmware Rev:[%.8s]\n",
843 DrvParms
->ControllerType
,
844 DrvParms
->BufferSize
* IDE_SECTOR_BUF_SZ
,
845 DrvParms
->ECCByteCnt
,
846 DrvParms
->FirmwareRev
);
847 DPRINT("Model:[%.40s]\n", DrvParms
->ModelNumber
);
848 DPRINT("RWMult?:%02x LBA:%d DMA:%d MinPIO:%d ns MinDMA:%d ns\n",
849 (DrvParms
->RWMultImplemented
) & 0xff,
850 (DrvParms
->Capabilities
& IDE_DRID_LBA_SUPPORTED
) ? 1 : 0,
851 (DrvParms
->Capabilities
& IDE_DRID_DMA_SUPPORTED
) ? 1 : 0,
852 DrvParms
->MinPIOTransTime
,
853 DrvParms
->MinDMATransTime
);
854 DPRINT("TM:Cyls:%d Heads:%d Sectors/Trk:%d Capacity:%ld\n",
855 DrvParms
->TMCylinders
,
857 DrvParms
->TMSectorsPerTrk
,
858 (ULONG
)(DrvParms
->TMCapacityLo
+ (DrvParms
->TMCapacityHi
<< 16)));
859 DPRINT("TM:SectorCount: 0x%04x%04x = %lu\n",
860 DrvParms
->TMSectorCountHi
,
861 DrvParms
->TMSectorCountLo
,
862 (ULONG
)((DrvParms
->TMSectorCountHi
<< 16) + DrvParms
->TMSectorCountLo
));
865 * Fix default ATA sector size.
866 * Attention: Default ATAPI sector size is 2048 bytes!!
868 DPRINT("BytesPerSector %d\n", DrvParms
->BytesPerSector
);
869 DrvParms
->BytesPerSector
= 512;
870 DPRINT("BytesPerSector %d\n", DrvParms
->BytesPerSector
);
876 // IDECreateDiskDevice
879 // Creates the disk device object
885 // IN PDRIVER_OBJECT DriverObject The system supplied driver object
886 // OUT PDEVICE_OBJECT *DeviceObject The created device object
887 // IN PCONTROLLER_OBJECT ControllerObject The Controller for the device
888 // IN BOOLEAN LBASupported Does the drive support LBA addressing?
889 // IN BOOLEAN DMASupported Does the drive support DMA?
890 // IN int SectorsPerLogCyl Sectors per cylinder
891 // IN int SectorsPerLogTrk Sectors per track
892 // IN DWORD Offset First valid sector for this device
893 // IN DWORD Size Count of valid sectors for this device
900 IDECreateDiskDevice(IN PDRIVER_OBJECT DriverObject
,
901 OUT PDEVICE_OBJECT
*DeviceObject
,
902 IN PCONTROLLER_OBJECT ControllerObject
,
905 IN PIDE_DRIVE_IDENTIFY DrvParms
,
906 IN ULONG SectorCount
)
908 WCHAR NameBuffer
[IDE_MAX_NAME_LENGTH
];
909 UNICODE_STRING DeviceName
;
911 PIDE_DEVICE_EXTENSION DeviceExtension
;
913 /* Create a unicode device name */
915 L
"\\Device\\Harddisk%d\\Partition0",
917 RtlInitUnicodeString(&DeviceName
,
920 /* Create the device */
921 RC
= IoCreateDevice(DriverObject
,
922 sizeof(IDE_DEVICE_EXTENSION
),
930 DbgPrint("IoCreateDevice call failed\n");
934 /* Set the buffering strategy here... */
935 (*DeviceObject
)->Flags
|= DO_DIRECT_IO
;
936 (*DeviceObject
)->AlignmentRequirement
= FILE_WORD_ALIGNMENT
;
938 /* Fill out Device extension data */
939 DeviceExtension
= (PIDE_DEVICE_EXTENSION
) (*DeviceObject
)->DeviceExtension
;
940 DeviceExtension
->DeviceObject
= (*DeviceObject
);
941 DeviceExtension
->ControllerObject
= ControllerObject
;
942 DeviceExtension
->DiskDeviceExtension
= DeviceExtension
;
943 DeviceExtension
->UnitNumber
= UnitNumber
;
944 DeviceExtension
->LBASupported
=
945 (DrvParms
->Capabilities
& IDE_DRID_LBA_SUPPORTED
) ? 1 : 0;
946 DeviceExtension
->DMASupported
=
947 (DrvParms
->Capabilities
& IDE_DRID_DMA_SUPPORTED
) ? 1 : 0;
948 DeviceExtension
->BytesPerSector
= DrvParms
->BytesPerSector
;
949 DeviceExtension
->SectorsPerLogCyl
=
950 DrvParms
->LogicalHeads
* DrvParms
->SectorsPerTrack
;
951 DeviceExtension
->SectorsPerLogTrk
= DrvParms
->SectorsPerTrack
;
952 DeviceExtension
->LogicalHeads
= DrvParms
->LogicalHeads
;
953 DeviceExtension
->LogicalCylinders
=
954 (DrvParms
->Capabilities
& IDE_DRID_LBA_SUPPORTED
) ? DrvParms
->TMCylinders
: DrvParms
->LogicalCyls
;
956 DeviceExtension
->StartingOffset
.QuadPart
= 0;
957 DeviceExtension
->PartitionLength
.QuadPart
= (ULONGLONG
)SectorCount
*
958 (ULONGLONG
)DrvParms
->BytesPerSector
;
959 DeviceExtension
->HiddenSectors
= 0;
960 DeviceExtension
->PartitionNumber
= 0;
961 DeviceExtension
->PartitionType
= 0;
962 DeviceExtension
->BootIndicator
= FALSE
;
964 DPRINT("%wZ: offset %I64d length %I64d\n",
966 DeviceExtension
->StartingOffset
.QuadPart
,
967 DeviceExtension
->PartitionLength
.QuadPart
);
969 /* Initialize the DPC object here */
970 IoInitializeDpcRequest(*DeviceObject
,
977 // IDECreatePartitionDevice
980 // Creates a partition device object
986 // IN PDRIVER_OBJECT DriverObject The system supplied driver object
987 // OUT PDEVICE_OBJECT *DeviceObject The created device object
988 // IN PCONTROLLER_OBJECT ControllerObject The Controller for the device
989 // IN BOOLEAN LBASupported Does the drive support LBA addressing?
990 // IN BOOLEAN DMASupported Does the drive support DMA?
991 // IN int SectorsPerLogCyl Sectors per cylinder
992 // IN int SectorsPerLogTrk Sectors per track
993 // IN DWORD Offset First valid sector for this device
994 // IN DWORD Size Count of valid sectors for this device
1001 IDECreatePartitionDevice(IN PDRIVER_OBJECT DriverObject
,
1002 OUT PDEVICE_OBJECT
*DeviceObject
,
1003 IN PCONTROLLER_OBJECT ControllerObject
,
1004 IN PVOID DiskDeviceExtension
,
1006 IN ULONG DiskNumber
,
1007 IN PIDE_DRIVE_IDENTIFY DrvParms
,
1008 IN PPARTITION_INFORMATION PartitionInfo
)
1010 WCHAR NameBuffer
[IDE_MAX_NAME_LENGTH
];
1011 UNICODE_STRING DeviceName
;
1013 PIDE_DEVICE_EXTENSION DeviceExtension
;
1015 /* Create a unicode device name */
1016 swprintf(NameBuffer
,
1017 L
"\\Device\\Harddisk%d\\Partition%d",
1019 PartitionInfo
->PartitionNumber
);
1020 RtlInitUnicodeString(&DeviceName
,
1023 /* Create the device */
1024 RC
= IoCreateDevice(DriverObject
, sizeof(IDE_DEVICE_EXTENSION
),
1025 &DeviceName
, FILE_DEVICE_DISK
, 0, TRUE
, DeviceObject
);
1026 if (!NT_SUCCESS(RC
))
1028 DbgPrint ("IoCreateDevice call failed\n");
1032 /* Set the buffering strategy here... */
1033 (*DeviceObject
)->Flags
|= DO_DIRECT_IO
;
1034 (*DeviceObject
)->AlignmentRequirement
= FILE_WORD_ALIGNMENT
;
1036 /* Fill out Device extension data */
1037 DeviceExtension
= (PIDE_DEVICE_EXTENSION
)(*DeviceObject
)->DeviceExtension
;
1038 DeviceExtension
->DeviceObject
= (*DeviceObject
);
1039 DeviceExtension
->ControllerObject
= ControllerObject
;
1040 DeviceExtension
->DiskDeviceExtension
= DiskDeviceExtension
;
1041 DeviceExtension
->UnitNumber
= UnitNumber
;
1042 DeviceExtension
->LBASupported
=
1043 (DrvParms
->Capabilities
& IDE_DRID_LBA_SUPPORTED
) ? 1 : 0;
1044 DeviceExtension
->DMASupported
=
1045 (DrvParms
->Capabilities
& IDE_DRID_DMA_SUPPORTED
) ? 1 : 0;
1046 DeviceExtension
->BytesPerSector
= DrvParms
->BytesPerSector
;
1047 DeviceExtension
->SectorsPerLogCyl
= DrvParms
->LogicalHeads
*
1048 DrvParms
->SectorsPerTrack
;
1049 DeviceExtension
->SectorsPerLogTrk
= DrvParms
->SectorsPerTrack
;
1050 DeviceExtension
->LogicalHeads
= DrvParms
->LogicalHeads
;
1051 DeviceExtension
->LogicalCylinders
=
1052 (DrvParms
->Capabilities
& IDE_DRID_LBA_SUPPORTED
) ? DrvParms
->TMCylinders
: DrvParms
->LogicalCyls
;
1054 DeviceExtension
->StartingOffset
= PartitionInfo
->StartingOffset
;
1055 DeviceExtension
->PartitionLength
= PartitionInfo
->PartitionLength
;
1056 DeviceExtension
->HiddenSectors
= PartitionInfo
->HiddenSectors
;
1057 DeviceExtension
->PartitionNumber
= PartitionInfo
->PartitionNumber
;
1058 DeviceExtension
->PartitionType
= PartitionInfo
->PartitionType
;
1059 DeviceExtension
->BootIndicator
= PartitionInfo
->BootIndicator
;
1061 DPRINT("%wZ: offset %I64d size %I64d\n",
1063 DeviceExtension
->StartingOffset
.QuadPart
,
1064 DeviceExtension
->PartitionLength
.QuadPart
);
1066 /* Initialize the DPC object here */
1067 IoInitializeDpcRequest(*DeviceObject
, IDEDpcForIsr
);
1069 DPRINT("%wZ %I64dMB\n",
1071 DeviceExtension
->PartitionLength
.QuadPart
>> 20);
1080 // Read a sector of data from the drive in a polled fashion.
1086 // IN WORD Address Address of command port for drive
1087 // IN BYTE PreComp Value to write to precomp register
1088 // IN BYTE SectorCnt Value to write to sectorCnt register
1089 // IN BYTE SectorNum Value to write to sectorNum register
1090 // IN BYTE CylinderLow Value to write to CylinderLow register
1091 // IN BYTE CylinderHigh Value to write to CylinderHigh register
1092 // IN BYTE DrvHead Value to write to Drive/Head register
1093 // IN BYTE Command Value to write to Command register
1094 // OUT BYTE *Buffer Buffer for output data
1097 // int 0 is success, non 0 is an error code
1101 IDEPolledRead(IN WORD Address
,
1105 IN BYTE CylinderLow
,
1106 IN BYTE CylinderHigh
,
1113 BOOLEAN ReadJunk
= FALSE
;
1114 ULONG SectorCount
= 0;
1116 /* Wait for STATUS.BUSY and STATUS.DRQ to clear */
1117 for (RetryCount
= 0; RetryCount
< IDE_MAX_BUSY_RETRIES
; RetryCount
++)
1119 Status
= IDEReadStatus(Address
);
1120 if (!(Status
& IDE_SR_BUSY
) && !(Status
& IDE_SR_DRQ
))
1124 KeStallExecutionProcessor(10);
1126 if (RetryCount
== IDE_MAX_BUSY_RETRIES
)
1131 /* Write Drive/Head to select drive */
1132 IDEWriteDriveHead(Address
, IDE_DH_FIXED
| DrvHead
);
1134 /* Wait for STATUS.BUSY and STATUS.DRQ to clear */
1135 for (RetryCount
= 0; RetryCount
< IDE_MAX_BUSY_RETRIES
; RetryCount
++)
1137 Status
= IDEReadStatus(Address
);
1138 if (!(Status
& IDE_SR_BUSY
) && !(Status
& IDE_SR_DRQ
))
1142 KeStallExecutionProcessor(10);
1144 if (RetryCount
== IDE_MAX_BUSY_RETRIES
)
1149 /* Issue command to drive */
1150 if (DrvHead
& IDE_DH_LBA
)
1152 DPRINT("READ:DRV=%d:LBA=1:BLK=%08d:SC=%02x:CM=%02x\n",
1153 DrvHead
& IDE_DH_DRV1
? 1 : 0,
1154 ((DrvHead
& 0x0f) << 24) + (CylinderHigh
<< 16) + (CylinderLow
<< 8) + SectorNum
,
1160 DPRINT("READ:DRV=%d:LBA=0:CH=%02x:CL=%02x:HD=%01x:SN=%02x:SC=%02x:CM=%02x\n",
1161 DrvHead
& IDE_DH_DRV1
? 1 : 0,
1170 /* Setup command parameters */
1171 IDEWritePrecomp(Address
, PreComp
);
1172 IDEWriteSectorCount(Address
, SectorCnt
);
1173 IDEWriteSectorNum(Address
, SectorNum
);
1174 IDEWriteCylinderHigh(Address
, CylinderHigh
);
1175 IDEWriteCylinderLow(Address
, CylinderLow
);
1176 IDEWriteDriveHead(Address
, IDE_DH_FIXED
| DrvHead
);
1178 /* Issue the command */
1179 IDEWriteCommand(Address
, Command
);
1180 KeStallExecutionProcessor(50);
1182 /* wait for DRQ or error */
1183 for (RetryCount
= 0; RetryCount
< IDE_MAX_POLL_RETRIES
; RetryCount
++)
1185 Status
= IDEReadStatus(Address
);
1186 if (!(Status
& IDE_SR_BUSY
))
1188 if (Status
& IDE_SR_ERR
)
1190 DPRINT("IDE_SR_ERR asserted!\n");
1192 if ((Status
& IDE_SR_DRQ
) && !(Status
& IDE_SR_ERR
))
1201 KeStallExecutionProcessor(10);
1203 if (RetryCount
>= IDE_MAX_POLL_RETRIES
)
1210 /* Read data into buffer */
1211 if (ReadJunk
== TRUE
)
1213 UCHAR JunkBuffer
[IDE_SECTOR_BUF_SZ
];
1214 IDEReadBlock(Address
, JunkBuffer
, IDE_SECTOR_BUF_SZ
);
1218 IDEReadBlock(Address
, Buffer
, IDE_SECTOR_BUF_SZ
);
1219 Buffer
+= IDE_SECTOR_BUF_SZ
;
1223 /* Check for more sectors to read */
1224 for (RetryCount
= 0; RetryCount
< IDE_MAX_BUSY_RETRIES
; RetryCount
++)
1226 Status
= IDEReadStatus(Address
);
1227 if (!(Status
& IDE_SR_BUSY
))
1229 if (Status
& IDE_SR_ERR
)
1231 DPRINT("IDE_SR_ERR asserted!\n");
1233 if (Status
& IDE_SR_DRQ
)
1235 if (SectorCount
>= SectorCnt
)
1248 // ------------------------------------------- Nondiscardable statics
1250 // IDEDispatchOpenClose
1253 // Answer requests for Open/Close calls: a null operation
1259 // Standard dispatch arguments
1265 static NTSTATUS STDCALL
1266 IDEDispatchOpenClose(IN PDEVICE_OBJECT pDO
,
1269 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
1270 Irp
->IoStatus
.Information
= FILE_OPENED
;
1271 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
1273 return STATUS_SUCCESS
;
1276 // IDEDispatchReadWrite
1279 // Answer requests for reads and writes
1285 // Standard dispatch arguments
1291 static NTSTATUS STDCALL
1292 IDEDispatchReadWrite(IN PDEVICE_OBJECT DeviceObject
,
1296 LARGE_INTEGER AdjustedOffset
, AdjustedExtent
, PartitionExtent
, InsertKeyLI
;
1297 PIO_STACK_LOCATION IrpStack
;
1298 PIDE_DEVICE_EXTENSION DeviceExtension
;
1300 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
1301 DeviceExtension
= (PIDE_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
1303 // Validate operation parameters
1304 AdjustedOffset
.QuadPart
= DeviceExtension
->StartingOffset
.QuadPart
+
1305 IrpStack
->Parameters
.Read
.ByteOffset
.QuadPart
;
1306 DPRINT("AdjustedOffset: %I64x\n", AdjustedOffset
.QuadPart
);
1308 AdjustedExtent
.QuadPart
= AdjustedOffset
.QuadPart
+
1309 (ULONGLONG
)IrpStack
->Parameters
.Read
.Length
;
1310 DPRINT("AdjustedExtent: %I64x\n", AdjustedExtent
.QuadPart
);
1312 PartitionExtent
.QuadPart
= DeviceExtension
->StartingOffset
.QuadPart
+
1313 DeviceExtension
->PartitionLength
.QuadPart
;
1314 DPRINT("PartitionExtent: %I64x\n", PartitionExtent
.QuadPart
);
1316 if ((AdjustedExtent
.QuadPart
> PartitionExtent
.QuadPart
) ||
1317 (IrpStack
->Parameters
.Read
.Length
& (DeviceExtension
->BytesPerSector
- 1)))
1319 DPRINT("Request failed on bad parameters\n",0);
1320 DPRINT("AdjustedExtent=%I64x PartitionExtent=%I64x ReadLength=%lx\n",
1321 AdjustedExtent
.QuadPart
,
1322 PartitionExtent
.QuadPart
,
1323 IrpStack
->Parameters
.Read
.Length
);
1324 Irp
->IoStatus
.Status
= STATUS_INVALID_PARAMETER
;
1325 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
1326 return STATUS_INVALID_PARAMETER
;
1329 // Adjust operation to absolute sector offset
1330 IrpStack
->Parameters
.Read
.ByteOffset
= AdjustedOffset
;
1332 // Start the packet and insert the request in order of sector offset
1333 assert(DeviceExtension
->BytesPerSector
== 512);
1334 InsertKeyLI
.QuadPart
= IrpStack
->Parameters
.Read
.ByteOffset
.QuadPart
>> 9;
1335 IrpInsertKey
= InsertKeyLI
.u
.LowPart
;
1336 IoStartPacket(DeviceObject
, Irp
, &IrpInsertKey
, NULL
);
1338 DPRINT("Returning STATUS_PENDING\n");
1339 return STATUS_PENDING
;
1342 // IDEDispatchDeviceControl
1345 // Answer requests for device control calls
1351 // Standard dispatch arguments
1357 static NTSTATUS STDCALL
1358 IDEDispatchDeviceControl(IN PDEVICE_OBJECT DeviceObject
,
1362 ULONG ControlCode
, InputLength
, OutputLength
;
1363 PIO_STACK_LOCATION IrpStack
;
1364 PIDE_DEVICE_EXTENSION DeviceExtension
;
1365 PIDE_DEVICE_EXTENSION DiskDeviceExtension
;
1368 RC
= STATUS_SUCCESS
;
1369 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
1370 ControlCode
= IrpStack
->Parameters
.DeviceIoControl
.IoControlCode
;
1371 InputLength
= IrpStack
->Parameters
.DeviceIoControl
.InputBufferLength
;
1372 OutputLength
= IrpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
;
1373 DeviceExtension
= (PIDE_DEVICE_EXTENSION
) DeviceObject
->DeviceExtension
;
1374 DiskDeviceExtension
= (PIDE_DEVICE_EXTENSION
)DeviceExtension
->DiskDeviceExtension
;
1375 Increment
= IO_NO_INCREMENT
;
1377 // A huge switch statement in a Windows program?! who would have thought?
1378 switch (ControlCode
)
1380 case IOCTL_DISK_GET_DRIVE_GEOMETRY
:
1381 if (IrpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(DISK_GEOMETRY
))
1383 Irp
->IoStatus
.Status
= STATUS_INVALID_PARAMETER
;
1387 PDISK_GEOMETRY Geometry
;
1389 Geometry
= (PDISK_GEOMETRY
) Irp
->AssociatedIrp
.SystemBuffer
;
1391 Geometry
->MediaType
= FixedMedia
;
1392 Geometry
->Cylinders
.QuadPart
= DiskDeviceExtension
->LogicalCylinders
;
1393 Geometry
->TracksPerCylinder
= DiskDeviceExtension
->SectorsPerLogCyl
/
1394 DiskDeviceExtension
->SectorsPerLogTrk
;
1395 Geometry
->SectorsPerTrack
= DiskDeviceExtension
->SectorsPerLogTrk
;
1396 Geometry
->BytesPerSector
= DiskDeviceExtension
->BytesPerSector
;
1397 DPRINT("DiskDeviceExtension->BytesPerSector %lu\n", DiskDeviceExtension
->BytesPerSector
);
1398 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
1399 Irp
->IoStatus
.Information
= sizeof(DISK_GEOMETRY
);
1403 case IOCTL_DISK_GET_PARTITION_INFO
:
1404 DPRINT("IOCTL_DISK_GET_PARTITION_INFO\n");
1405 if (IrpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
<
1406 sizeof(PARTITION_INFORMATION
))
1408 Irp
->IoStatus
.Status
= STATUS_INFO_LENGTH_MISMATCH
;
1410 else if (DeviceExtension
->PartitionNumber
== 0)
1412 Irp
->IoStatus
.Status
= STATUS_INVALID_DEVICE_REQUEST
;
1416 PPARTITION_INFORMATION PartitionInfo
;
1418 PartitionInfo
= (PPARTITION_INFORMATION
)Irp
->AssociatedIrp
.SystemBuffer
;
1420 PartitionInfo
->PartitionType
= DeviceExtension
->PartitionType
;
1421 PartitionInfo
->StartingOffset
= DeviceExtension
->StartingOffset
;
1422 PartitionInfo
->PartitionLength
= DeviceExtension
->PartitionLength
;
1423 PartitionInfo
->HiddenSectors
= DeviceExtension
->HiddenSectors
;
1424 PartitionInfo
->PartitionNumber
= DeviceExtension
->PartitionNumber
;
1425 PartitionInfo
->BootIndicator
= DeviceExtension
->BootIndicator
;
1426 PartitionInfo
->RewritePartition
= FALSE
;
1427 PartitionInfo
->RecognizedPartition
=
1428 IsRecognizedPartition(DeviceExtension
->PartitionType
);
1430 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
1431 Irp
->IoStatus
.Information
= sizeof(PARTITION_INFORMATION
);
1435 case IOCTL_DISK_SET_PARTITION_INFO
:
1436 RC
= STATUS_INVALID_DEVICE_REQUEST
;
1437 Irp
->IoStatus
.Status
= RC
;
1438 Irp
->IoStatus
.Information
= 0;
1441 case IOCTL_DISK_GET_DRIVE_LAYOUT
:
1442 if (IrpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
<
1443 sizeof(DRIVE_LAYOUT_INFORMATION
))
1445 Irp
->IoStatus
.Status
= STATUS_BUFFER_TOO_SMALL
;
1449 PDRIVE_LAYOUT_INFORMATION PartitionList
;
1451 RC
= IoReadPartitionTable(DiskDeviceExtension
->DeviceObject
,
1452 DiskDeviceExtension
->BytesPerSector
,
1455 if (!NT_SUCCESS(RC
))
1457 Irp
->IoStatus
.Status
= RC
;
1463 BufferSize
= FIELD_OFFSET(DRIVE_LAYOUT_INFORMATION
,
1465 BufferSize
+= PartitionList
->PartitionCount
* sizeof(PARTITION_INFORMATION
);
1467 if (BufferSize
> IrpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
)
1469 Irp
->IoStatus
.Status
= STATUS_BUFFER_TOO_SMALL
;
1473 RtlMoveMemory(Irp
->AssociatedIrp
.SystemBuffer
,
1476 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
1477 Irp
->IoStatus
.Information
= BufferSize
;
1479 ExFreePool(PartitionList
);
1482 Increment
= IO_DISK_INCREMENT
;
1485 case IOCTL_DISK_SET_DRIVE_LAYOUT
:
1486 case IOCTL_DISK_VERIFY
:
1487 case IOCTL_DISK_FORMAT_TRACKS
:
1488 case IOCTL_DISK_PERFORMANCE
:
1489 case IOCTL_DISK_IS_WRITABLE
:
1490 case IOCTL_DISK_LOGGING
:
1491 case IOCTL_DISK_FORMAT_TRACKS_EX
:
1492 case IOCTL_DISK_HISTOGRAM_STRUCTURE
:
1493 case IOCTL_DISK_HISTOGRAM_DATA
:
1494 case IOCTL_DISK_HISTOGRAM_RESET
:
1495 case IOCTL_DISK_REQUEST_STRUCTURE
:
1496 case IOCTL_DISK_REQUEST_DATA
:
1498 // If we get here, something went wrong. inform the requestor
1500 RC
= STATUS_INVALID_DEVICE_REQUEST
;
1501 Irp
->IoStatus
.Status
= RC
;
1502 Irp
->IoStatus
.Information
= 0;
1506 IoCompleteRequest(Irp
, Increment
);
1514 // Get the next requested I/O packet started
1520 // Dispatch routine standard arguments
1527 STDCALL
IDEStartIo(IN PDEVICE_OBJECT DeviceObject
,
1530 LARGE_INTEGER SectorLI
;
1531 PIO_STACK_LOCATION IrpStack
;
1532 PIDE_DEVICE_EXTENSION DeviceExtension
;
1535 DPRINT("IDEStartIo() called!\n");
1537 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
1538 DeviceExtension
= (PIDE_DEVICE_EXTENSION
) DeviceObject
->DeviceExtension
;
1540 // FIXME: implement the supported functions
1542 switch (IrpStack
->MajorFunction
)
1546 DeviceExtension
->Operation
= IrpStack
->MajorFunction
;
1547 DeviceExtension
->BytesRequested
= IrpStack
->Parameters
.Read
.Length
;
1548 assert(DeviceExtension
->BytesPerSector
== 512);
1549 SectorLI
= RtlLargeIntegerShiftRight(IrpStack
->Parameters
.Read
.ByteOffset
, 9);
1550 DeviceExtension
->StartingSector
= SectorLI
.u
.LowPart
;
1551 if (DeviceExtension
->BytesRequested
> DeviceExtension
->BytesPerSector
*
1552 IDE_MAX_SECTORS_PER_XFER
)
1554 DeviceExtension
->BytesToTransfer
= DeviceExtension
->BytesPerSector
*
1555 IDE_MAX_SECTORS_PER_XFER
;
1559 DeviceExtension
->BytesToTransfer
= DeviceExtension
->BytesRequested
;
1561 DeviceExtension
->BytesRequested
-= DeviceExtension
->BytesToTransfer
;
1562 DeviceExtension
->SectorsTransferred
= 0;
1563 DeviceExtension
->TargetAddress
= (BYTE
*)MmGetSystemAddressForMdl(Irp
->MdlAddress
);
1564 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
1565 IoAllocateController(DeviceExtension
->ControllerObject
,
1567 IDEAllocateController
,
1569 KeLowerIrql(OldIrql
);
1573 Irp
->IoStatus
.Status
= STATUS_NOT_SUPPORTED
;
1574 Irp
->IoStatus
.Information
= 0;
1575 KeBugCheck((ULONG
)Irp
);
1576 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
1577 IoStartNextPacket(DeviceObject
, FALSE
);
1580 DPRINT("IDEStartIo() finished!\n");
1583 // IDEAllocateController
1585 static IO_ALLOCATION_ACTION STDCALL
1586 IDEAllocateController(IN PDEVICE_OBJECT DeviceObject
,
1588 IN PVOID MapRegisterBase
,
1591 PIDE_DEVICE_EXTENSION DeviceExtension
;
1592 PIDE_CONTROLLER_EXTENSION ControllerExtension
;
1594 DeviceExtension
= (PIDE_DEVICE_EXTENSION
) DeviceObject
->DeviceExtension
;
1595 ControllerExtension
= (PIDE_CONTROLLER_EXTENSION
)
1596 DeviceExtension
->ControllerObject
->ControllerExtension
;
1597 ControllerExtension
->CurrentIrp
= Irp
;
1598 ControllerExtension
->Retries
= 0;
1599 return KeSynchronizeExecution(ControllerExtension
->Interrupt
,
1601 DeviceExtension
) ? KeepObject
:
1605 // IDEStartController
1608 IDEStartController(IN OUT PVOID Context
)
1610 BYTE SectorCnt
, SectorNum
, CylinderLow
, CylinderHigh
;
1611 BYTE DrvHead
, Command
;
1614 ULONG StartingSector
;
1615 PIDE_DEVICE_EXTENSION DeviceExtension
;
1616 PIDE_CONTROLLER_EXTENSION ControllerExtension
;
1619 DeviceExtension
= (PIDE_DEVICE_EXTENSION
) Context
;
1620 ControllerExtension
= (PIDE_CONTROLLER_EXTENSION
)
1621 DeviceExtension
->ControllerObject
->ControllerExtension
;
1622 ControllerExtension
->OperationInProgress
= TRUE
;
1623 ControllerExtension
->DeviceForOperation
= DeviceExtension
;
1625 // Write controller registers to start opteration
1626 StartingSector
= DeviceExtension
->StartingSector
;
1627 SectorCnt
= DeviceExtension
->BytesToTransfer
/
1628 DeviceExtension
->BytesPerSector
;
1629 if (DeviceExtension
->LBASupported
)
1631 SectorNum
= StartingSector
& 0xff;
1632 CylinderLow
= (StartingSector
>> 8) & 0xff;
1633 CylinderHigh
= (StartingSector
>> 16) & 0xff;
1634 DrvHead
= ((StartingSector
>> 24) & 0x0f) |
1635 (DeviceExtension
->UnitNumber
? IDE_DH_DRV1
: 0) |
1640 SectorNum
= (StartingSector
% DeviceExtension
->SectorsPerLogTrk
) + 1;
1641 StartingSector
/= DeviceExtension
->SectorsPerLogTrk
;
1642 DrvHead
= (StartingSector
% DeviceExtension
->LogicalHeads
) |
1643 (DeviceExtension
->UnitNumber
? IDE_DH_DRV1
: 0);
1644 StartingSector
/= DeviceExtension
->LogicalHeads
;
1645 CylinderLow
= StartingSector
& 0xff;
1646 CylinderHigh
= StartingSector
>> 8;
1648 Command
= DeviceExtension
->Operation
== IRP_MJ_READ
?
1649 IDE_CMD_READ
: IDE_CMD_WRITE
;
1650 if (DrvHead
& IDE_DH_LBA
)
1652 DPRINT("%s:BUS=%04x:DRV=%d:LBA=1:BLK=%08d:SC=%02x:CM=%02x\n",
1653 DeviceExtension
->Operation
== IRP_MJ_READ
? "READ" : "WRITE",
1654 ControllerExtension
->CommandPortBase
,
1655 DrvHead
& IDE_DH_DRV1
? 1 : 0,
1656 ((DrvHead
& 0x0f) << 24) +
1657 (CylinderHigh
<< 16) + (CylinderLow
<< 8) + SectorNum
,
1663 DPRINT("%s:BUS=%04x:DRV=%d:LBA=0:CH=%02x:CL=%02x:HD=%01x:SN=%02x:SC=%02x:CM=%02x\n",
1664 DeviceExtension
->Operation
== IRP_MJ_READ
? "READ" : "WRITE",
1665 ControllerExtension
->CommandPortBase
,
1666 DrvHead
& IDE_DH_DRV1
? 1 : 0,
1675 /* wait for BUSY to clear */
1676 for (Retries
= 0; Retries
< IDE_MAX_BUSY_RETRIES
; Retries
++)
1678 Status
= IDEReadStatus(ControllerExtension
->CommandPortBase
);
1679 if (!(Status
& IDE_SR_BUSY
))
1683 KeStallExecutionProcessor(10);
1685 DPRINT ("status=%02x\n", Status
);
1686 DPRINT ("waited %ld usecs for busy to clear\n", Retries
* 10);
1687 if (Retries
>= IDE_MAX_BUSY_RETRIES
)
1689 DPRINT ("Drive is BUSY for too long\n");
1690 if (++ControllerExtension
->Retries
> IDE_MAX_CMD_RETRIES
)
1692 DbgPrint ("Max Retries on Drive reset reached, returning failure\n");
1693 Irp
= ControllerExtension
->CurrentIrp
;
1694 Irp
->IoStatus
.Status
= STATUS_DISK_OPERATION_FAILED
;
1695 Irp
->IoStatus
.Information
= 0;
1701 DPRINT ("Beginning drive reset sequence\n");
1702 IDEBeginControllerReset(ControllerExtension
);
1708 /* Select the desired drive */
1709 IDEWriteDriveHead(ControllerExtension
->CommandPortBase
, IDE_DH_FIXED
| DrvHead
);
1711 /* wait for BUSY to clear and DRDY to assert */
1712 for (Retries
= 0; Retries
< IDE_MAX_BUSY_RETRIES
; Retries
++)
1714 Status
= IDEReadStatus(ControllerExtension
->CommandPortBase
);
1715 // if (!(Status & IDE_SR_BUSY) && (Status & IDE_SR_DRDY))
1716 if (!(Status
& IDE_SR_BUSY
) && !(Status
& IDE_SR_DRQ
))
1720 KeStallExecutionProcessor(10);
1722 DPRINT ("waited %ld usecs for busy to clear after drive select\n", Retries
* 10);
1723 if (Retries
>= IDE_MAX_BUSY_RETRIES
)
1725 DPRINT ("Drive is BUSY for too long after drive select\n");
1726 if (ControllerExtension
->Retries
++ > IDE_MAX_CMD_RETRIES
)
1728 DbgPrint ("Max Retries on Drive reset reached, returning failure\n");
1729 Irp
= ControllerExtension
->CurrentIrp
;
1730 Irp
->IoStatus
.Status
= STATUS_DISK_OPERATION_FAILED
;
1731 Irp
->IoStatus
.Information
= 0;
1737 DPRINT ("Beginning drive reset sequence\n");
1738 IDEBeginControllerReset(ControllerExtension
);
1744 /* Setup command parameters */
1745 IDEWritePrecomp(ControllerExtension
->CommandPortBase
, 0);
1746 IDEWriteSectorCount(ControllerExtension
->CommandPortBase
, SectorCnt
);
1747 IDEWriteSectorNum(ControllerExtension
->CommandPortBase
, SectorNum
);
1748 IDEWriteCylinderHigh(ControllerExtension
->CommandPortBase
, CylinderHigh
);
1749 IDEWriteCylinderLow(ControllerExtension
->CommandPortBase
, CylinderLow
);
1750 IDEWriteDriveHead(ControllerExtension
->CommandPortBase
, IDE_DH_FIXED
| DrvHead
);
1752 /* Issue command to drive */
1753 IDEWriteCommand(ControllerExtension
->CommandPortBase
, Command
);
1754 ControllerExtension
->TimerState
= IDETimerCmdWait
;
1755 ControllerExtension
->TimerCount
= IDE_CMD_TIMEOUT
;
1757 if (DeviceExtension
->Operation
== IRP_MJ_WRITE
)
1760 // Wait for controller ready
1761 for (Retries
= 0; Retries
< IDE_MAX_WRITE_RETRIES
; Retries
++)
1763 BYTE Status
= IDEReadStatus(ControllerExtension
->CommandPortBase
);
1764 if (!(Status
& IDE_SR_BUSY
) || (Status
& IDE_SR_ERR
))
1768 KeStallExecutionProcessor(10);
1770 if (Retries
>= IDE_MAX_BUSY_RETRIES
)
1772 if (ControllerExtension
->Retries
++ > IDE_MAX_CMD_RETRIES
)
1774 Irp
= ControllerExtension
->CurrentIrp
;
1775 Irp
->IoStatus
.Status
= STATUS_DISK_OPERATION_FAILED
;
1776 Irp
->IoStatus
.Information
= 0;
1782 IDEBeginControllerReset(ControllerExtension
);
1788 // Load the first sector of data into the controller
1789 IDEWriteBlock(ControllerExtension
->CommandPortBase
,
1790 DeviceExtension
->TargetAddress
,
1792 DeviceExtension
->TargetAddress
+= IDE_SECTOR_BUF_SZ
;
1793 DeviceExtension
->BytesToTransfer
-= DeviceExtension
->BytesPerSector
;
1794 DeviceExtension
->SectorsTransferred
++;
1796 DPRINT ("Command issued to drive, IDEStartController done\n");
1801 // IDEBeginControllerReset
1804 IDEBeginControllerReset(PIDE_CONTROLLER_EXTENSION ControllerExtension
)
1808 DPRINT("Controller Reset initiated on %04x\n",
1809 ControllerExtension
->ControlPortBase
);
1811 /* Assert drive reset line */
1812 DPRINT("Asserting reset line\n");
1813 IDEWriteDriveControl(ControllerExtension
->ControlPortBase
, IDE_DC_SRST
);
1815 /* Wait for BSY assertion */
1816 DPRINT("Waiting for BSY assertion\n");
1817 for (Retries
= 0; Retries
< IDE_MAX_RESET_RETRIES
; Retries
++)
1819 BYTE Status
= IDEReadStatus(ControllerExtension
->CommandPortBase
);
1820 if ((Status
& IDE_SR_BUSY
))
1824 KeStallExecutionProcessor(10);
1826 if (Retries
== IDE_MAX_RESET_RETRIES
)
1828 DPRINT("Timeout on BSY assertion\n");
1831 /* Negate drive reset line */
1832 DPRINT("Negating reset line\n");
1833 IDEWriteDriveControl(ControllerExtension
->ControlPortBase
, 0);
1835 // FIXME: handle case of no device 0
1837 /* Set timer to check for end of reset */
1838 ControllerExtension
->TimerState
= IDETimerResetWaitForBusyNegate
;
1839 ControllerExtension
->TimerCount
= IDE_RESET_BUSY_TIMEOUT
;
1845 // Handle interrupts for IDE devices
1851 // IN PKINTERRUPT Interrupt The interrupt level in effect
1852 // IN PVOID ServiceContext The driver supplied context
1853 // (the controller extension)
1855 // TRUE This ISR handled the interrupt
1856 // FALSE Another ISR must handle this interrupt
1858 static BOOLEAN STDCALL
1859 IDEIsr(IN PKINTERRUPT Interrupt
,
1860 IN PVOID ServiceContext
)
1862 BOOLEAN IsLastBlock
, AnErrorOccured
, RequestIsComplete
;
1863 BYTE
*TargetAddress
;
1865 NTSTATUS ErrorStatus
;
1866 ULONG ErrorInformation
;
1868 PIDE_DEVICE_EXTENSION DeviceExtension
;
1869 PIDE_CONTROLLER_EXTENSION ControllerExtension
;
1871 if (IDEInitialized
== FALSE
)
1875 DPRINT ("IDEIsr called\n");
1877 ControllerExtension
= (PIDE_CONTROLLER_EXTENSION
) ServiceContext
;
1879 // Read the status port to clear the interrtupt (even if it's not ours).
1880 ControllerExtension
->DeviceStatus
= IDEReadStatus(ControllerExtension
->CommandPortBase
);
1882 // If the interrupt is not ours, get the heck outta dodge.
1883 if (!ControllerExtension
->OperationInProgress
)
1888 DeviceExtension
= ControllerExtension
->DeviceForOperation
;
1889 IsLastBlock
= FALSE
;
1890 AnErrorOccured
= FALSE
;
1891 RequestIsComplete
= FALSE
;
1892 ErrorStatus
= STATUS_SUCCESS
;
1893 ErrorInformation
= 0;
1895 // Handle error condition if it exists
1896 if (ControllerExtension
->DeviceStatus
& IDE_SR_ERR
)
1898 BYTE ErrorReg
, SectorCount
, SectorNum
, CylinderLow
, CylinderHigh
;
1902 ErrorReg
= IDEReadError(ControllerExtension
->CommandPortBase
);
1903 CylinderLow
= IDEReadCylinderLow(ControllerExtension
->CommandPortBase
);
1904 CylinderHigh
= IDEReadCylinderHigh(ControllerExtension
->CommandPortBase
);
1905 DriveHead
= IDEReadDriveHead(ControllerExtension
->CommandPortBase
);
1906 SectorCount
= IDEReadSectorCount(ControllerExtension
->CommandPortBase
);
1907 SectorNum
= IDEReadSectorNum(ControllerExtension
->CommandPortBase
);
1908 // FIXME: should use the NT error logging facility
1909 DbgPrint ("IDE Error: OP:%02x STAT:%02x ERR:%02x CYLLO:%02x CYLHI:%02x SCNT:%02x SNUM:%02x\n",
1910 DeviceExtension
->Operation
,
1911 ControllerExtension
->DeviceStatus
,
1918 // FIXME: should retry the command and perhaps recalibrate the drive
1920 // Set error status information
1921 AnErrorOccured
= TRUE
;
1922 ErrorStatus
= STATUS_DISK_OPERATION_FAILED
;
1924 (((((((CylinderHigh
<< 8) + CylinderLow
) *
1925 DeviceExtension
->LogicalHeads
) +
1926 (DriveHead
% DeviceExtension
->LogicalHeads
)) *
1927 DeviceExtension
->SectorsPerLogTrk
) + SectorNum
- 1) -
1928 DeviceExtension
->StartingSector
) * DeviceExtension
->BytesPerSector
;
1933 // Check controller and setup for next transfer
1934 switch (DeviceExtension
->Operation
)
1938 // Update controller/device state variables
1939 TargetAddress
= DeviceExtension
->TargetAddress
;
1940 DeviceExtension
->TargetAddress
+= DeviceExtension
->BytesPerSector
;
1941 DeviceExtension
->BytesToTransfer
-= DeviceExtension
->BytesPerSector
;
1942 DeviceExtension
->SectorsTransferred
++;
1944 // Remember whether DRQ should be low at end (last block read)
1945 IsLastBlock
= DeviceExtension
->BytesToTransfer
== 0;
1947 // Wait for DRQ assertion
1948 for (Retries
= 0; Retries
< IDE_MAX_DRQ_RETRIES
&&
1949 !(IDEReadStatus(ControllerExtension
->CommandPortBase
) & IDE_SR_DRQ
);
1952 KeStallExecutionProcessor(10);
1955 // Copy the block of data
1956 IDEReadBlock(ControllerExtension
->CommandPortBase
,
1963 for (Retries
= 0; Retries
< IDE_MAX_BUSY_RETRIES
&&
1964 (IDEReadStatus(ControllerExtension
->CommandPortBase
) & IDE_SR_BUSY
);
1967 KeStallExecutionProcessor(10);
1970 // Check for data overrun
1971 if (IDEReadStatus(ControllerExtension
->CommandPortBase
) & IDE_SR_DRQ
)
1973 AnErrorOccured
= TRUE
;
1974 ErrorStatus
= STATUS_DATA_OVERRUN
;
1975 ErrorInformation
= 0;
1980 // Setup next transfer or set RequestIsComplete
1981 if (DeviceExtension
->BytesRequested
>
1982 DeviceExtension
->BytesPerSector
* IDE_MAX_SECTORS_PER_XFER
)
1984 DeviceExtension
->StartingSector
+= DeviceExtension
->SectorsTransferred
;
1985 DeviceExtension
->SectorsTransferred
= 0;
1986 DeviceExtension
->BytesToTransfer
=
1987 DeviceExtension
->BytesPerSector
* IDE_MAX_SECTORS_PER_XFER
;
1988 DeviceExtension
->BytesRequested
-= DeviceExtension
->BytesToTransfer
;
1990 else if (DeviceExtension
->BytesRequested
> 0)
1992 DeviceExtension
->StartingSector
+= DeviceExtension
->SectorsTransferred
;
1993 DeviceExtension
->SectorsTransferred
= 0;
1994 DeviceExtension
->BytesToTransfer
= DeviceExtension
->BytesRequested
;
1995 DeviceExtension
->BytesRequested
-= DeviceExtension
->BytesToTransfer
;
1999 RequestIsComplete
= TRUE
;
2008 if (DeviceExtension
->BytesToTransfer
== 0)
2010 for (Retries
= 0; Retries
< IDE_MAX_BUSY_RETRIES
&&
2011 (IDEReadStatus(ControllerExtension
->CommandPortBase
) & IDE_SR_BUSY
);
2014 KeStallExecutionProcessor(10);
2017 // Check for data overrun
2018 if (IDEReadStatus(ControllerExtension
->CommandPortBase
) & IDE_SR_DRQ
)
2020 AnErrorOccured
= TRUE
;
2021 ErrorStatus
= STATUS_DATA_OVERRUN
;
2022 ErrorInformation
= 0;
2027 // Setup next transfer or set RequestIsComplete
2029 if (DeviceExtension
->BytesRequested
>
2030 DeviceExtension
->BytesPerSector
* IDE_MAX_SECTORS_PER_XFER
)
2032 DeviceExtension
->StartingSector
+= DeviceExtension
->SectorsTransferred
;
2033 DeviceExtension
->SectorsTransferred
= 0;
2034 DeviceExtension
->BytesToTransfer
=
2035 DeviceExtension
->BytesPerSector
* IDE_MAX_SECTORS_PER_XFER
;
2036 DeviceExtension
->BytesRequested
-= DeviceExtension
->BytesToTransfer
;
2038 else if (DeviceExtension
->BytesRequested
> 0)
2040 DeviceExtension
->StartingSector
+= DeviceExtension
->SectorsTransferred
;
2041 DeviceExtension
->SectorsTransferred
= 0;
2042 DeviceExtension
->BytesToTransfer
= DeviceExtension
->BytesRequested
;
2043 DeviceExtension
->BytesRequested
-= DeviceExtension
->BytesToTransfer
;
2047 RequestIsComplete
= TRUE
;
2054 // Update controller/device state variables
2055 TargetAddress
= DeviceExtension
->TargetAddress
;
2056 DeviceExtension
->TargetAddress
+= DeviceExtension
->BytesPerSector
;
2057 DeviceExtension
->BytesToTransfer
-= DeviceExtension
->BytesPerSector
;
2058 DeviceExtension
->SectorsTransferred
++;
2060 // Write block to controller
2061 IDEWriteBlock(ControllerExtension
->CommandPortBase
,
2069 // If there was an error or the request is done, complete the packet
2070 if (AnErrorOccured
|| RequestIsComplete
)
2072 // Set the return status and info values
2073 Irp
= ControllerExtension
->CurrentIrp
;
2074 Irp
->IoStatus
.Status
= ErrorStatus
;
2075 Irp
->IoStatus
.Information
= ErrorInformation
;
2077 // Clear out controller fields
2078 ControllerExtension
->OperationInProgress
= FALSE
;
2079 ControllerExtension
->DeviceStatus
= 0;
2081 // Queue the Dpc to finish up
2082 IoRequestDpc(DeviceExtension
->DeviceObject
,
2084 ControllerExtension
);
2086 else if (IsLastBlock
)
2088 // Else more data is needed, setup next device I/O
2089 IDEStartController((PVOID
)DeviceExtension
);
2102 // IN PDEVICE_OBJECT DpcDeviceObject
2104 // IN PVOID DpcContext
2107 IDEDpcForIsr(IN PKDPC Dpc
,
2108 IN PDEVICE_OBJECT DpcDeviceObject
,
2110 IN PVOID DpcContext
)
2112 DPRINT("IDEDpcForIsr()\n");
2113 IDEFinishOperation((PIDE_CONTROLLER_EXTENSION
) DpcContext
);
2116 // IDEFinishOperation
2119 IDEFinishOperation(PIDE_CONTROLLER_EXTENSION ControllerExtension
)
2121 PIDE_DEVICE_EXTENSION DeviceExtension
;
2125 DeviceExtension
= ControllerExtension
->DeviceForOperation
;
2126 Irp
= ControllerExtension
->CurrentIrp
;
2127 Operation
= DeviceExtension
->Operation
;
2128 ControllerExtension
->OperationInProgress
= FALSE
;
2129 ControllerExtension
->DeviceForOperation
= 0;
2130 ControllerExtension
->CurrentIrp
= 0;
2132 // Deallocate the controller
2133 IoFreeController(DeviceExtension
->ControllerObject
);
2135 // Start the next packet
2136 IoStartNextPacketByKey(DeviceExtension
->DeviceObject
,
2138 DeviceExtension
->StartingSector
);
2140 // Flush cache if necessary
2141 if (Operation
== IRP_MJ_READ
)
2143 KeFlushIoBuffers(Irp
->MdlAddress
, TRUE
, FALSE
);
2145 // Issue completion of the current packet
2146 // return status information too
2147 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
2148 Irp
->IoStatus
.Information
= DeviceExtension
->SectorsTransferred
* DeviceExtension
->BytesPerSector
;
2149 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
2154 // This function handles timeouts and other time delayed processing
2159 // IN PDEVICE_OBJECT DeviceObject Device object registered with timer
2160 // IN PVOID Context the Controller extension for the
2161 // controller the device is on
2164 IDEIoTimer(PDEVICE_OBJECT DeviceObject
,
2167 PIDE_CONTROLLER_EXTENSION ControllerExtension
;
2169 // Setup Extension pointer
2170 ControllerExtension
= (PIDE_CONTROLLER_EXTENSION
) Context
;
2171 DPRINT("Timer activated for %04lx\n", ControllerExtension
->CommandPortBase
);
2173 // Handle state change if necessary
2174 switch (ControllerExtension
->TimerState
)
2176 case IDETimerResetWaitForBusyNegate
:
2177 if (!(IDEReadStatus(ControllerExtension
->CommandPortBase
) &
2180 DPRINT("Busy line has negated, waiting for DRDY assert\n");
2181 ControllerExtension
->TimerState
= IDETimerResetWaitForDrdyAssert
;
2182 ControllerExtension
->TimerCount
= IDE_RESET_DRDY_TIMEOUT
;
2187 case IDETimerResetWaitForDrdyAssert
:
2188 if (IDEReadStatus(ControllerExtension
->CommandPortBase
) &
2191 DPRINT("DRDY has asserted, reset complete\n");
2192 ControllerExtension
->TimerState
= IDETimerIdle
;
2193 ControllerExtension
->TimerCount
= 0;
2195 // FIXME: get diagnostic code from drive 0
2197 /* Start current packet command again */
2198 if (!KeSynchronizeExecution(ControllerExtension
->Interrupt
,
2200 ControllerExtension
->DeviceForOperation
))
2202 IDEFinishOperation(ControllerExtension
);
2212 // If we're counting down, then count.
2213 if (ControllerExtension
->TimerCount
> 0)
2215 ControllerExtension
->TimerCount
--;
2217 // Else we'll check the state and process if necessary
2221 switch (ControllerExtension
->TimerState
)
2226 case IDETimerCmdWait
:
2227 /* Command timed out, reset drive and try again or fail */
2228 DPRINT("Timeout waiting for command completion\n");
2229 if (++ControllerExtension
->Retries
> IDE_MAX_CMD_RETRIES
)
2231 if (ControllerExtension
->CurrentIrp
!= NULL
)
2233 DbgPrint ("Max retries has been reached, IRP finished with error\n");
2234 ControllerExtension
->CurrentIrp
->IoStatus
.Status
= STATUS_IO_TIMEOUT
;
2235 ControllerExtension
->CurrentIrp
->IoStatus
.Information
= 0;
2236 IDEFinishOperation(ControllerExtension
);
2238 ControllerExtension
->TimerState
= IDETimerIdle
;
2239 ControllerExtension
->TimerCount
= 0;
2243 IDEBeginControllerReset(ControllerExtension
);
2247 case IDETimerResetWaitForBusyNegate
:
2248 case IDETimerResetWaitForDrdyAssert
:
2249 if (ControllerExtension
->CurrentIrp
!= NULL
)
2251 DbgPrint ("Timeout waiting for drive reset, giving up on IRP\n");
2252 ControllerExtension
->CurrentIrp
->IoStatus
.Status
=
2254 ControllerExtension
->CurrentIrp
->IoStatus
.Information
= 0;
2255 IDEFinishOperation(ControllerExtension
);
2257 ControllerExtension
->TimerState
= IDETimerIdle
;
2258 ControllerExtension
->TimerCount
= 0;