14 #undef ScsiPortLogError
15 #undef ScsiPortMoveMemory
16 #undef ScsiPortWritePortBufferUchar
17 #undef ScsiPortWritePortBufferUlong
18 #undef ScsiPortWritePortBufferUshort
19 #undef ScsiPortWritePortUchar
20 #undef ScsiPortWritePortUlong
21 #undef ScsiPortWritePortUshort
22 #undef ScsiPortWriteRegisterBufferUchar
23 #undef ScsiPortWriteRegisterBufferUlong
24 #undef ScsiPortWriteRegisterBufferUshort
25 #undef ScsiPortWriteRegisterUchar
26 #undef ScsiPortWriteRegisterUlong
27 #undef ScsiPortWriteRegisterUshort
28 #undef ScsiPortReadPortBufferUchar
29 #undef ScsiPortReadPortBufferUlong
30 #undef ScsiPortReadPortBufferUshort
31 #undef ScsiPortReadPortUchar
32 #undef ScsiPortReadPortUlong
33 #undef ScsiPortReadPortUshort
34 #undef ScsiPortReadRegisterBufferUchar
35 #undef ScsiPortReadRegisterBufferUlong
36 #undef ScsiPortReadRegisterBufferUshort
37 #undef ScsiPortReadRegisterUchar
38 #undef ScsiPortReadRegisterUlong
39 #undef ScsiPortReadRegisterUshort
44 #define SCSI_PORT_NEXT_REQUEST_READY 0x0008
46 DBG_DEFAULT_CHANNEL(SCSIPORT
);
50 PVOID NonCachedExtension
;
57 /* SRB extension stuff */
58 ULONG SrbExtensionSize
;
59 PVOID SrbExtensionBuffer
;
61 IO_SCSI_CAPABILITIES PortCapabilities
;
63 PHW_INITIALIZE HwInitialize
;
64 PHW_STARTIO HwStartIo
;
65 PHW_INTERRUPT HwInterrupt
;
66 PHW_RESET_BUS HwResetBus
;
68 /* DMA related stuff */
69 PADAPTER_OBJECT AdapterObject
;
71 ULONG CommonBufferLength
;
73 PVOID MiniPortDeviceExtension
;
74 } SCSI_PORT_DEVICE_EXTENSION
, *PSCSI_PORT_DEVICE_EXTENSION
;
76 PSCSI_PORT_DEVICE_EXTENSION ScsiDeviceExtensions
[SCSI_MAXIMUM_BUSES
];
83 PFOUR_BYTE Source
= (PFOUR_BYTE
)&Value
;
85 Dest
.Byte0
= Source
->Byte3
;
86 Dest
.Byte1
= Source
->Byte2
;
87 Dest
.Byte2
= Source
->Byte1
;
88 Dest
.Byte3
= Source
->Byte0
;
94 SpiSendSynchronousSrb(
95 IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
96 IN PSCSI_REQUEST_BLOCK Srb
)
100 ASSERT(!(Srb
->SrbFlags
& SRB_FLAGS_IS_ACTIVE
));
102 /* HACK: handle lack of interrupts */
103 while (!(DeviceExtension
->InterruptFlags
& SCSI_PORT_NEXT_REQUEST_READY
))
105 KeStallExecutionProcessor(100 * 1000);
106 DeviceExtension
->HwInterrupt(DeviceExtension
->MiniPortDeviceExtension
);
109 DeviceExtension
->InterruptFlags
&= ~SCSI_PORT_NEXT_REQUEST_READY
;
110 Srb
->SrbFlags
|= SRB_FLAGS_IS_ACTIVE
;
112 if (!DeviceExtension
->HwStartIo(
113 DeviceExtension
->MiniPortDeviceExtension
,
120 /* HACK: handle lack of interrupts */
121 while (Srb
->SrbFlags
& SRB_FLAGS_IS_ACTIVE
)
123 KeStallExecutionProcessor(100 * 1000);
124 DeviceExtension
->HwInterrupt(DeviceExtension
->MiniPortDeviceExtension
);
127 ret
= SRB_STATUS(Srb
->SrbStatus
) == SRB_STATUS_SUCCESS
;
133 typedef struct tagDISKCONTEXT
136 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
141 /* Device characteristics */
143 ULONGLONG SectorOffset
;
144 ULONGLONG SectorCount
;
145 ULONGLONG SectorNumber
;
148 static LONG
DiskClose(ULONG FileId
)
150 DISKCONTEXT
* Context
= FsGetDeviceSpecific(FileId
);
156 static LONG
DiskGetFileInformation(ULONG FileId
, FILEINFORMATION
* Information
)
158 DISKCONTEXT
* Context
= FsGetDeviceSpecific(FileId
);
160 RtlZeroMemory(Information
, sizeof(FILEINFORMATION
));
161 Information
->EndingAddress
.QuadPart
= Context
->SectorCount
* Context
->SectorSize
;
162 Information
->CurrentAddress
.QuadPart
= Context
->SectorNumber
* Context
->SectorSize
;
167 static LONG
DiskOpen(CHAR
* Path
, OPENMODE OpenMode
, ULONG
* FileId
)
169 PSCSI_REQUEST_BLOCK Srb
;
171 READ_CAPACITY_DATA ReadCapacityBuffer
;
173 DISKCONTEXT
* Context
;
174 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
175 ULONG ScsiBus
, PathId
, TargetId
, Lun
, Partition
, PathSyntax
;
177 ULONGLONG SectorOffset
= 0;
178 ULONGLONG SectorCount
;
181 if (!DissectArcPath2(Path
, &ScsiBus
, &TargetId
, &Lun
, &Partition
, &PathSyntax
))
183 if (PathSyntax
!= 0) /* scsi() format */
185 DeviceExtension
= ScsiDeviceExtensions
[ScsiBus
];
186 PathId
= ScsiBus
- DeviceExtension
->BusNum
;
188 /* Get disk capacity and sector size */
189 Srb
= ExAllocatePool(PagedPool
, sizeof(SCSI_REQUEST_BLOCK
));
192 RtlZeroMemory(Srb
, sizeof(SCSI_REQUEST_BLOCK
));
193 Srb
->Length
= sizeof(SCSI_REQUEST_BLOCK
);
194 Srb
->Function
= SRB_FUNCTION_EXECUTE_SCSI
;
195 Srb
->PathId
= (UCHAR
)PathId
;
196 Srb
->TargetId
= (UCHAR
)TargetId
;
197 Srb
->Lun
= (UCHAR
)Lun
;
199 Srb
->SrbFlags
= SRB_FLAGS_DATA_IN
;
200 Srb
->DataTransferLength
= sizeof(READ_CAPACITY_DATA
);
201 Srb
->TimeOutValue
= 5; /* in seconds */
202 Srb
->DataBuffer
= &ReadCapacityBuffer
;
203 Cdb
= (PCDB
)Srb
->Cdb
;
204 Cdb
->CDB10
.OperationCode
= SCSIOP_READ_CAPACITY
;
205 if (!SpiSendSynchronousSrb(DeviceExtension
, Srb
))
210 /* Transform result to host endianness */
211 SectorCount
= ntohl(ReadCapacityBuffer
.LogicalBlockAddress
);
212 SectorSize
= ntohl(ReadCapacityBuffer
.BytesPerBlock
);
216 /* Need to offset start of disk and length */
221 Context
= ExAllocatePool(PagedPool
, sizeof(DISKCONTEXT
));
224 Context
->DeviceExtension
= DeviceExtension
;
225 Context
->PathId
= (UCHAR
)PathId
;
226 Context
->TargetId
= (UCHAR
)TargetId
;
227 Context
->Lun
= (UCHAR
)Lun
;
228 Context
->SectorSize
= SectorSize
;
229 Context
->SectorOffset
= SectorOffset
;
230 Context
->SectorCount
= SectorCount
;
231 Context
->SectorNumber
= 0;
232 FsSetDeviceSpecific(*FileId
, Context
);
237 static LONG
DiskRead(ULONG FileId
, VOID
* Buffer
, ULONG N
, ULONG
* Count
)
239 DISKCONTEXT
* Context
= FsGetDeviceSpecific(FileId
);
240 PSCSI_REQUEST_BLOCK Srb
;
242 ULONG FullSectors
, NbSectors
;
250 FullSectors
= N
/ Context
->SectorSize
;
251 NbSectors
= (N
+ Context
->SectorSize
- 1) / Context
->SectorSize
;
252 if (Context
->SectorNumber
+ NbSectors
>= Context
->SectorCount
)
254 if (FullSectors
> 0xffff)
257 /* Read full sectors */
258 ASSERT(Context
->SectorNumber
< 0xFFFFFFFF);
259 Lba
= (ULONG
)Context
->SectorNumber
;
262 Srb
= ExAllocatePool(PagedPool
, sizeof(SCSI_REQUEST_BLOCK
));
266 RtlZeroMemory(Srb
, sizeof(SCSI_REQUEST_BLOCK
));
267 Srb
->Length
= sizeof(SCSI_REQUEST_BLOCK
);
268 Srb
->Function
= SRB_FUNCTION_EXECUTE_SCSI
;
269 Srb
->PathId
= Context
->PathId
;
270 Srb
->TargetId
= Context
->TargetId
;
271 Srb
->Lun
= Context
->Lun
;
273 Srb
->SrbFlags
= SRB_FLAGS_DATA_IN
;
274 Srb
->DataTransferLength
= FullSectors
* Context
->SectorSize
;
275 Srb
->TimeOutValue
= 5; /* in seconds */
276 Srb
->DataBuffer
= Buffer
;
277 Cdb
= (PCDB
)Srb
->Cdb
;
278 Cdb
->CDB10
.OperationCode
= SCSIOP_READ
;
279 Cdb
->CDB10
.LogicalUnitNumber
= Srb
->Lun
;
280 Cdb
->CDB10
.LogicalBlockByte0
= (Lba
>> 24) & 0xff;
281 Cdb
->CDB10
.LogicalBlockByte1
= (Lba
>> 16) & 0xff;
282 Cdb
->CDB10
.LogicalBlockByte2
= (Lba
>> 8) & 0xff;
283 Cdb
->CDB10
.LogicalBlockByte3
= Lba
& 0xff;
284 Cdb
->CDB10
.TransferBlocksMsb
= (FullSectors
>> 8) & 0xff;
285 Cdb
->CDB10
.TransferBlocksLsb
= FullSectors
& 0xff;
286 if (!SpiSendSynchronousSrb(Context
->DeviceExtension
, Srb
))
290 Buffer
= (PUCHAR
)Buffer
+ FullSectors
* Context
->SectorSize
;
291 N
-= FullSectors
* Context
->SectorSize
;
292 *Count
+= FullSectors
* Context
->SectorSize
;
296 /* Read incomplete last sector */
301 Sector
= ExAllocatePool(PagedPool
, Context
->SectorSize
);
305 Srb
= ExAllocatePool(PagedPool
, sizeof(SCSI_REQUEST_BLOCK
));
312 RtlZeroMemory(Srb
, sizeof(SCSI_REQUEST_BLOCK
));
313 Srb
->Length
= sizeof(SCSI_REQUEST_BLOCK
);
314 Srb
->Function
= SRB_FUNCTION_EXECUTE_SCSI
;
315 Srb
->PathId
= Context
->PathId
;
316 Srb
->TargetId
= Context
->TargetId
;
317 Srb
->Lun
= Context
->Lun
;
319 Srb
->SrbFlags
= SRB_FLAGS_DATA_IN
;
320 Srb
->DataTransferLength
= Context
->SectorSize
;
321 Srb
->TimeOutValue
= 5; /* in seconds */
322 Srb
->DataBuffer
= Sector
;
323 Cdb
= (PCDB
)Srb
->Cdb
;
324 Cdb
->CDB10
.OperationCode
= SCSIOP_READ
;
325 Cdb
->CDB10
.LogicalUnitNumber
= Srb
->Lun
;
326 Cdb
->CDB10
.LogicalBlockByte0
= (Lba
>> 24) & 0xff;
327 Cdb
->CDB10
.LogicalBlockByte1
= (Lba
>> 16) & 0xff;
328 Cdb
->CDB10
.LogicalBlockByte2
= (Lba
>> 8) & 0xff;
329 Cdb
->CDB10
.LogicalBlockByte3
= Lba
& 0xff;
330 Cdb
->CDB10
.TransferBlocksMsb
= 0;
331 Cdb
->CDB10
.TransferBlocksLsb
= 1;
332 if (!SpiSendSynchronousSrb(Context
->DeviceExtension
, Srb
))
337 RtlCopyMemory(Buffer
, Sector
, N
);
345 static LONG
DiskSeek(ULONG FileId
, LARGE_INTEGER
* Position
, SEEKMODE SeekMode
)
347 DISKCONTEXT
* Context
= FsGetDeviceSpecific(FileId
);
349 if (SeekMode
!= SeekAbsolute
)
351 if (Position
->QuadPart
& (Context
->SectorSize
- 1))
354 Context
->SectorNumber
= Position
->QuadPart
/ Context
->SectorSize
;
358 static const DEVVTBL DiskVtbl
= {
360 DiskGetFileInformation
,
368 IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
369 IN PHW_INITIALIZATION_DATA HwInitData
,
370 OUT PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
371 IN BOOLEAN ZeroStruct
)
375 /* Zero out the struct if told so */
378 /* First zero the portconfig */
379 RtlZeroMemory(ConfigInfo
, sizeof(PORT_CONFIGURATION_INFORMATION
));
381 /* Initialize the struct */
382 ConfigInfo
->Length
= sizeof(PORT_CONFIGURATION_INFORMATION
);
383 ConfigInfo
->AdapterInterfaceType
= HwInitData
->AdapterInterfaceType
;
384 ConfigInfo
->InterruptMode
= Latched
;
385 ConfigInfo
->DmaChannel
= SP_UNINITIALIZED_VALUE
;
386 ConfigInfo
->DmaPort
= SP_UNINITIALIZED_VALUE
;
387 ConfigInfo
->MaximumTransferLength
= SP_UNINITIALIZED_VALUE
;
388 ConfigInfo
->MaximumNumberOfTargets
= SCSI_MAXIMUM_TARGETS_PER_BUS
;
390 /* Store parameters */
391 ConfigInfo
->NeedPhysicalAddresses
= HwInitData
->NeedPhysicalAddresses
;
392 ConfigInfo
->MapBuffers
= HwInitData
->MapBuffers
;
393 ConfigInfo
->AutoRequestSense
= HwInitData
->AutoRequestSense
;
394 ConfigInfo
->ReceiveEvent
= HwInitData
->ReceiveEvent
;
395 ConfigInfo
->TaggedQueuing
= HwInitData
->TaggedQueuing
;
396 ConfigInfo
->MultipleRequestPerLu
= HwInitData
->MultipleRequestPerLu
;
398 /* Get the disk usage */
399 ConfigInfo
->AtdiskPrimaryClaimed
= FALSE
; // FIXME
400 ConfigInfo
->AtdiskSecondaryClaimed
= FALSE
; // FIXME
402 /* Initiator bus id is not set */
403 for (Bus
= 0; Bus
< 8; Bus
++)
404 ConfigInfo
->InitiatorBusId
[Bus
] = (CCHAR
)SP_UNINITIALIZED_VALUE
;
407 ConfigInfo
->NumberOfPhysicalBreaks
= 17;
409 return STATUS_SUCCESS
;
415 IN ULONG DebugPrintLevel
,
416 IN PCCHAR DebugMessage
,
423 if (DebugPrintLevel
> 10)
426 va_start(ap
, DebugMessage
);
428 /* Construct a string */
429 Length
= _vsnprintf(Buffer
, 512, DebugMessage
, ap
);
431 /* Check if we went past the buffer */
432 if (Length
== MAXULONG
)
434 /* Terminate it if we went over-board */
435 Buffer
[sizeof(Buffer
) - 1] = '\0';
438 Length
= sizeof(Buffer
);
441 /* Print the message */
450 ScsiPortCompleteRequest(
451 IN PVOID HwDeviceExtension
,
461 #undef ScsiPortConvertPhysicalAddressToUlong
464 ScsiPortConvertPhysicalAddressToUlong(
465 IN SCSI_PHYSICAL_ADDRESS Address
)
467 return Address
.LowPart
;
470 SCSI_PHYSICAL_ADDRESS
472 ScsiPortConvertUlongToPhysicalAddress(
473 IN ULONG_PTR UlongAddress
)
475 SCSI_PHYSICAL_ADDRESS Address
;
477 Address
.QuadPart
= UlongAddress
;
484 IN PVOID DeviceExtension
)
492 ScsiPortFreeDeviceBase(
493 IN PVOID HwDeviceExtension
,
494 IN PVOID MappedAddress
)
502 IN PVOID DeviceExtension
,
503 IN ULONG BusDataType
,
504 IN ULONG SystemIoBusNumber
,
509 return HalGetBusDataByOffset(BusDataType
, SystemIoBusNumber
, SlotNumber
, Buffer
, 0, Length
);
514 ScsiPortGetDeviceBase(
515 IN PVOID HwDeviceExtension
,
516 IN INTERFACE_TYPE BusType
,
517 IN ULONG SystemIoBusNumber
,
518 IN SCSI_PHYSICAL_ADDRESS IoAddress
,
519 IN ULONG NumberOfBytes
,
520 IN BOOLEAN InIoSpace
)
522 PHYSICAL_ADDRESS TranslatedAddress
;
525 AddressSpace
= (ULONG
)InIoSpace
;
526 if (HalTranslateBusAddress(BusType
,
530 &TranslatedAddress
) == FALSE
)
536 if (AddressSpace
!= 0)
537 return (PVOID
)TranslatedAddress
.u
.LowPart
;
541 return (PVOID
)IoAddress
.LowPart
;
546 ScsiPortGetLogicalUnit(
547 IN PVOID HwDeviceExtension
,
557 SCSI_PHYSICAL_ADDRESS
559 ScsiPortGetPhysicalAddress(
560 IN PVOID HwDeviceExtension
,
561 IN PSCSI_REQUEST_BLOCK Srb OPTIONAL
,
562 IN PVOID VirtualAddress
,
565 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
566 SCSI_PHYSICAL_ADDRESS PhysicalAddress
;
567 ULONG BufferLength
= 0;
570 TRACE("ScsiPortGetPhysicalAddress(%p %p %p %p)\n",
571 HwDeviceExtension
, Srb
, VirtualAddress
, Length
);
573 DeviceExtension
= ((PSCSI_PORT_DEVICE_EXTENSION
)HwDeviceExtension
) - 1;
575 if (Srb
== NULL
|| Srb
->SenseInfoBuffer
== VirtualAddress
)
577 /* Simply look it up in the allocated common buffer */
578 Offset
= (PUCHAR
)VirtualAddress
- (PUCHAR
)DeviceExtension
->SrbExtensionBuffer
;
580 BufferLength
= DeviceExtension
->CommonBufferLength
- Offset
;
581 PhysicalAddress
.QuadPart
= Offset
;
587 PhysicalAddress
.QuadPart
= (LONGLONG
)(SP_UNINITIALIZED_VALUE
);
590 *Length
= BufferLength
;
591 return PhysicalAddress
;
597 IN PVOID DeviceExtension
,
609 SpiAllocateCommonBuffer(
610 IN OUT PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
611 IN ULONG NonCachedSize
)
614 ULONG CommonBufferLength
, BufSize
;
616 /* If size is 0, set it to 16 */
617 if (!DeviceExtension
->SrbExtensionSize
)
618 DeviceExtension
->SrbExtensionSize
= 16;
621 BufSize
= DeviceExtension
->SrbExtensionSize
;
624 BufSize
= (BufSize
+ sizeof(LONGLONG
) - 1) & ~(sizeof(LONGLONG
) - 1);
626 /* Sum up into the total common buffer length, and round it to page size */
628 ROUND_TO_PAGES(NonCachedSize
);
631 if (!DeviceExtension
->AdapterObject
)
633 /* From nonpaged pool if there is no DMA */
634 CommonBuffer
= ExAllocatePool(NonPagedPool
, CommonBufferLength
);
638 /* Perform a full request since we have a DMA adapter*/
643 /* Fail in case of error */
645 return STATUS_INSUFFICIENT_RESOURCES
;
648 RtlZeroMemory(CommonBuffer
, CommonBufferLength
);
650 /* Store its size in Device Extension */
651 DeviceExtension
->CommonBufferLength
= CommonBufferLength
;
653 /* SrbExtension buffer is located at the beginning of the buffer */
654 DeviceExtension
->SrbExtensionBuffer
= CommonBuffer
;
656 /* Non-cached extension buffer is located at the end of
660 CommonBufferLength
-= NonCachedSize
;
661 DeviceExtension
->NonCachedExtension
= (PUCHAR
)CommonBuffer
+ CommonBufferLength
;
665 DeviceExtension
->NonCachedExtension
= NULL
;
668 return STATUS_SUCCESS
;
673 ScsiPortGetUncachedExtension(
674 IN PVOID HwDeviceExtension
,
675 IN PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
676 IN ULONG NumberOfBytes
)
678 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
679 DEVICE_DESCRIPTION DeviceDescription
;
680 ULONG MapRegistersCount
;
683 TRACE("ScsiPortGetUncachedExtension(%p %p %lu)\n",
684 HwDeviceExtension
, ConfigInfo
, NumberOfBytes
);
686 DeviceExtension
= ((PSCSI_PORT_DEVICE_EXTENSION
)HwDeviceExtension
) - 1;
688 /* Check for allocated common DMA buffer */
689 if (DeviceExtension
->SrbExtensionBuffer
!= NULL
)
694 /* Check for DMA adapter object */
695 if (DeviceExtension
->AdapterObject
== NULL
)
697 /* Initialize DMA adapter description */
698 RtlZeroMemory(&DeviceDescription
, sizeof(DEVICE_DESCRIPTION
));
700 DeviceDescription
.Version
= DEVICE_DESCRIPTION_VERSION
;
701 DeviceDescription
.Master
= ConfigInfo
->Master
;
702 DeviceDescription
.ScatterGather
= ConfigInfo
->ScatterGather
;
703 DeviceDescription
.DemandMode
= ConfigInfo
->DemandMode
;
704 DeviceDescription
.Dma32BitAddresses
= ConfigInfo
->Dma32BitAddresses
;
705 DeviceDescription
.BusNumber
= ConfigInfo
->SystemIoBusNumber
;
706 DeviceDescription
.DmaChannel
= ConfigInfo
->DmaChannel
;
707 DeviceDescription
.InterfaceType
= ConfigInfo
->AdapterInterfaceType
;
708 DeviceDescription
.DmaWidth
= ConfigInfo
->DmaWidth
;
709 DeviceDescription
.DmaSpeed
= ConfigInfo
->DmaSpeed
;
710 DeviceDescription
.MaximumLength
= ConfigInfo
->MaximumTransferLength
;
711 DeviceDescription
.DmaPort
= ConfigInfo
->DmaPort
;
713 /* Get a DMA adapter object */
715 DeviceExtension
->AdapterObject
=
716 HalGetAdapter(&DeviceDescription
, &MapRegistersCount
);
718 /* Fail in case of error */
719 if (DeviceExtension
->AdapterObject
== NULL
)
724 MapRegistersCount
= 0;
727 /* Set number of physical breaks */
728 if (ConfigInfo
->NumberOfPhysicalBreaks
!= 0 &&
729 MapRegistersCount
> ConfigInfo
->NumberOfPhysicalBreaks
)
731 DeviceExtension
->PortCapabilities
.MaximumPhysicalPages
=
732 ConfigInfo
->NumberOfPhysicalBreaks
;
736 DeviceExtension
->PortCapabilities
.MaximumPhysicalPages
= MapRegistersCount
;
740 /* Update Srb extension size */
741 if (DeviceExtension
->SrbExtensionSize
!= ConfigInfo
->SrbExtensionSize
)
742 DeviceExtension
->SrbExtensionSize
= ConfigInfo
->SrbExtensionSize
;
744 /* Allocate a common DMA buffer */
745 Status
= SpiAllocateCommonBuffer(DeviceExtension
, NumberOfBytes
);
747 if (!NT_SUCCESS(Status
))
749 TRACE("SpiAllocateCommonBuffer() failed with Status = 0x%08X!\n", Status
);
753 return DeviceExtension
->NonCachedExtension
;
758 ScsiPortGetVirtualAddress(
759 IN PVOID HwDeviceExtension
,
760 IN SCSI_PHYSICAL_ADDRESS PhysicalAddress
)
769 IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
778 struct _DRIVE_LAYOUT_INFORMATION
*PartitionBuffer
;
779 CHAR PartitionName
[64];
781 /* Register device with partition(0) suffix */
782 sprintf(PartitionName
, "%spartition(0)", ArcName
);
783 FsRegisterDevice(PartitionName
, &DiskVtbl
);
785 /* Read device partition table */
786 Status
= ArcOpen(PartitionName
, OpenReadOnly
, &FileId
);
787 if (Status
== ESUCCESS
)
789 ret
= HALDISPATCH
->HalIoReadPartitionTable((PDEVICE_OBJECT
)FileId
, 512, FALSE
, &PartitionBuffer
);
792 for (i
= 0; i
< PartitionBuffer
->PartitionCount
; i
++)
794 if (PartitionBuffer
->PartitionEntry
[i
].PartitionType
!= PARTITION_ENTRY_UNUSED
)
796 sprintf(PartitionName
, "%spartition(%lu)",
797 ArcName
, PartitionBuffer
->PartitionEntry
[i
].PartitionNumber
);
798 FsRegisterDevice(PartitionName
, &DiskVtbl
);
801 ExFreePool(PartitionBuffer
);
809 IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
814 PSCSI_REQUEST_BLOCK Srb
;
816 INQUIRYDATA InquiryBuffer
;
820 if (!DeviceExtension
->HwResetBus(DeviceExtension
->MiniPortDeviceExtension
, PathId
))
825 /* Remember the extension */
826 ScsiDeviceExtensions
[ScsiBus
] = DeviceExtension
;
828 for (TargetId
= 0; TargetId
< DeviceExtension
->MaxTargedIds
; TargetId
++)
833 TRACE("Scanning SCSI device %d.%d.%d\n",
834 ScsiBus
, TargetId
, Lun
);
836 Srb
= ExAllocatePool(PagedPool
, sizeof(SCSI_REQUEST_BLOCK
));
839 RtlZeroMemory(Srb
, sizeof(SCSI_REQUEST_BLOCK
));
840 Srb
->Length
= sizeof(SCSI_REQUEST_BLOCK
);
841 Srb
->Function
= SRB_FUNCTION_EXECUTE_SCSI
;
842 Srb
->PathId
= PathId
;
843 Srb
->TargetId
= TargetId
;
846 Srb
->SrbFlags
= SRB_FLAGS_DATA_IN
;
847 Srb
->DataTransferLength
= INQUIRYDATABUFFERSIZE
;
848 Srb
->TimeOutValue
= 5; /* in seconds */
849 Srb
->DataBuffer
= &InquiryBuffer
;
850 Cdb
= (PCDB
)Srb
->Cdb
;
851 Cdb
->CDB6INQUIRY
.OperationCode
= SCSIOP_INQUIRY
;
852 Cdb
->CDB6INQUIRY
.LogicalUnitNumber
= Srb
->Lun
;
853 Cdb
->CDB6INQUIRY
.AllocationLength
= (UCHAR
)Srb
->DataTransferLength
;
854 if (!SpiSendSynchronousSrb(DeviceExtension
, Srb
))
856 /* Don't check next LUNs */
860 /* Device exists, create its ARC name */
861 if (InquiryBuffer
.RemovableMedia
)
863 sprintf(ArcName
, "scsi(%ld)cdrom(%d)fdisk(%d)",
864 ScsiBus
, TargetId
, Lun
);
865 FsRegisterDevice(ArcName
, &DiskVtbl
);
869 sprintf(ArcName
, "scsi(%ld)disk(%d)rdisk(%d)",
870 ScsiBus
, TargetId
, Lun
);
871 /* Now, check if it has partitions */
872 SpiScanDevice(DeviceExtension
, ArcName
, PathId
, TargetId
, Lun
);
877 } while (Lun
< SCSI_MAXIMUM_LOGICAL_UNITS
);
883 IN PHW_INITIALIZATION_DATA HwInitializationData
,
884 IN PCM_FULL_RESOURCE_DESCRIPTOR ResourceDescriptor
,
885 IN OUT PPORT_CONFIGURATION_INFORMATION PortConfig
)
887 PACCESS_RANGE AccessRange
;
888 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialData
;
894 /* Loop through all entries */
895 for (Index
= 0; Index
< ResourceDescriptor
->PartialResourceList
.Count
; Index
++)
897 PartialData
= &ResourceDescriptor
->PartialResourceList
.PartialDescriptors
[Index
];
899 switch (PartialData
->Type
)
901 case CmResourceTypePort
:
902 /* Copy access ranges */
903 if (RangeNumber
< HwInitializationData
->NumberOfAccessRanges
)
905 TRACE("Got port at 0x%I64x, len 0x%x\n",
906 PartialData
->u
.Port
.Start
.QuadPart
, PartialData
->u
.Port
.Length
);
907 AccessRange
= &((*(PortConfig
->AccessRanges
))[RangeNumber
]);
909 AccessRange
->RangeStart
= PartialData
->u
.Port
.Start
;
910 AccessRange
->RangeLength
= PartialData
->u
.Port
.Length
;
912 AccessRange
->RangeInMemory
= FALSE
;
917 case CmResourceTypeMemory
:
918 /* Copy access ranges */
919 if (RangeNumber
< HwInitializationData
->NumberOfAccessRanges
)
921 TRACE("Got memory at 0x%I64x, len 0x%x\n",
922 PartialData
->u
.Memory
.Start
.QuadPart
, PartialData
->u
.Memory
.Length
);
923 AccessRange
= &((*(PortConfig
->AccessRanges
))[RangeNumber
]);
925 AccessRange
->RangeStart
= PartialData
->u
.Memory
.Start
;
926 AccessRange
->RangeLength
= PartialData
->u
.Memory
.Length
;
928 AccessRange
->RangeInMemory
= TRUE
;
933 case CmResourceTypeInterrupt
:
934 /* Copy interrupt data */
935 TRACE("Got interrupt level %d, vector %d\n",
936 PartialData
->u
.Interrupt
.Level
, PartialData
->u
.Interrupt
.Vector
);
937 PortConfig
->BusInterruptLevel
= PartialData
->u
.Interrupt
.Level
;
938 PortConfig
->BusInterruptVector
= PartialData
->u
.Interrupt
.Vector
;
940 /* Set interrupt mode accordingly to the resource */
941 if (PartialData
->Flags
== CM_RESOURCE_INTERRUPT_LATCHED
)
943 PortConfig
->InterruptMode
= Latched
;
945 else if (PartialData
->Flags
== CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE
)
947 PortConfig
->InterruptMode
= LevelSensitive
;
951 case CmResourceTypeDma
:
952 TRACE("Got DMA channel %d, port %d\n",
953 PartialData
->u
.Dma
.Channel
, PartialData
->u
.Dma
.Port
);
954 PortConfig
->DmaChannel
= PartialData
->u
.Dma
.Channel
;
955 PortConfig
->DmaPort
= PartialData
->u
.Dma
.Port
;
963 IN
struct _HW_INITIALIZATION_DATA
*HwInitializationData
,
964 IN OUT PPORT_CONFIGURATION_INFORMATION PortConfig
,
966 IN OUT PPCI_SLOT_NUMBER NextSlotNumber
)
968 PCI_COMMON_CONFIG PciConfig
;
969 PCI_SLOT_NUMBER SlotNumber
;
972 ULONG FunctionNumber
;
973 CHAR VendorIdString
[8];
974 CHAR DeviceIdString
[8];
975 PCM_RESOURCE_LIST ResourceList
= NULL
;
978 SlotNumber
.u
.AsULONG
= 0;
980 /* Loop through all devices */
981 for (DeviceNumber
= NextSlotNumber
->u
.bits
.DeviceNumber
; DeviceNumber
< PCI_MAX_DEVICES
; DeviceNumber
++)
983 SlotNumber
.u
.bits
.DeviceNumber
= DeviceNumber
;
985 /* Loop through all functions */
986 for (FunctionNumber
= NextSlotNumber
->u
.bits
.FunctionNumber
; FunctionNumber
< PCI_MAX_FUNCTION
; FunctionNumber
++)
988 SlotNumber
.u
.bits
.FunctionNumber
= FunctionNumber
;
990 /* Get PCI config bytes */
991 DataSize
= HalGetBusDataByOffset(
994 SlotNumber
.u
.AsULONG
,
999 /* If result of HalGetBusData is 0, then the bus is wrong */
1003 /* If result is PCI_INVALID_VENDORID, then this device has no more
1005 if (PciConfig
.VendorID
== PCI_INVALID_VENDORID
)
1008 sprintf(VendorIdString
, "%04hx", PciConfig
.VendorID
);
1009 sprintf(DeviceIdString
, "%04hx", PciConfig
.DeviceID
);
1011 if (_strnicmp(VendorIdString
, HwInitializationData
->VendorId
, HwInitializationData
->VendorIdLength
) ||
1012 _strnicmp(DeviceIdString
, HwInitializationData
->DeviceId
, HwInitializationData
->DeviceIdLength
))
1014 /* It is not our device */
1018 TRACE( "Found device 0x%04hx 0x%04hx at %1lu %2lu %1lu\n",
1019 PciConfig
.VendorID
, PciConfig
.DeviceID
,
1021 SlotNumber
.u
.bits
.DeviceNumber
, SlotNumber
.u
.bits
.FunctionNumber
);
1023 Status
= HalAssignSlotResources(NULL
,
1029 SlotNumber
.u
.AsULONG
,
1032 if (!NT_SUCCESS(Status
))
1035 /* Create configuration information */
1036 SpiResourceToConfig(HwInitializationData
,
1040 /* Free the resource list */
1041 ExFreePool(ResourceList
);
1043 /* Set dev & fn numbers */
1044 NextSlotNumber
->u
.bits
.DeviceNumber
= DeviceNumber
;
1045 NextSlotNumber
->u
.bits
.FunctionNumber
= FunctionNumber
+ 1;
1047 /* Save the slot number */
1048 PortConfig
->SlotNumber
= SlotNumber
.u
.AsULONG
;
1052 NextSlotNumber
->u
.bits
.FunctionNumber
= 0;
1055 NextSlotNumber
->u
.bits
.DeviceNumber
= 0;
1065 IN
struct _HW_INITIALIZATION_DATA
*HwInitializationData
,
1066 IN PVOID HwContext OPTIONAL
)
1068 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
1069 ULONG DeviceExtensionSize
;
1070 PORT_CONFIGURATION_INFORMATION PortConfig
;
1072 BOOLEAN FirstConfigCall
= TRUE
;
1073 PCI_SLOT_NUMBER SlotNumber
;
1077 if (HwInitializationData
->HwInitializationDataSize
!= sizeof(HW_INITIALIZATION_DATA
))
1079 return STATUS_INVALID_PARAMETER
;
1082 /* Check params for validity */
1083 if ((HwInitializationData
->HwInitialize
== NULL
) ||
1084 (HwInitializationData
->HwStartIo
== NULL
) ||
1085 (HwInitializationData
->HwInterrupt
== NULL
) ||
1086 (HwInitializationData
->HwFindAdapter
== NULL
) ||
1087 (HwInitializationData
->HwResetBus
== NULL
))
1089 return STATUS_INVALID_PARAMETER
;
1092 /* Zero starting slot number */
1093 SlotNumber
.u
.AsULONG
= 0;
1099 DeviceExtensionSize
= sizeof(SCSI_PORT_DEVICE_EXTENSION
) + HwInitializationData
->DeviceExtensionSize
;
1100 DeviceExtension
= MmHeapAlloc(DeviceExtensionSize
);
1101 if (!DeviceExtension
)
1103 return STATUS_NO_MEMORY
;
1105 RtlZeroMemory(DeviceExtension
, DeviceExtensionSize
);
1106 DeviceExtension
->InterruptFlags
= SCSI_PORT_NEXT_REQUEST_READY
;
1107 DeviceExtension
->HwInitialize
= HwInitializationData
->HwInitialize
;
1108 DeviceExtension
->HwStartIo
= HwInitializationData
->HwStartIo
;
1109 DeviceExtension
->HwInterrupt
= HwInitializationData
->HwInterrupt
;
1110 DeviceExtension
->HwResetBus
= HwInitializationData
->HwResetBus
;
1111 DeviceExtension
->MiniPortDeviceExtension
= (PVOID
)(DeviceExtension
+ 1);
1113 Status
= SpiCreatePortConfig(DeviceExtension
,
1114 HwInitializationData
,
1117 if (Status
!= STATUS_SUCCESS
)
1119 MmHeapFree(DeviceExtension
);
1123 PortConfig
.NumberOfAccessRanges
= HwInitializationData
->NumberOfAccessRanges
;
1124 PortConfig
.AccessRanges
= MmHeapAlloc(sizeof(ACCESS_RANGE
) * HwInitializationData
->NumberOfAccessRanges
);
1125 if (!PortConfig
.AccessRanges
)
1127 MmHeapFree(DeviceExtension
);
1128 return STATUS_NO_MEMORY
;
1130 RtlZeroMemory(PortConfig
.AccessRanges
, sizeof(ACCESS_RANGE
) * HwInitializationData
->NumberOfAccessRanges
);
1132 /* Search for matching PCI device */
1133 if ((HwInitializationData
->AdapterInterfaceType
== PCIBus
) &&
1134 (HwInitializationData
->VendorIdLength
> 0) &&
1135 (HwInitializationData
->VendorId
!= NULL
) &&
1136 (HwInitializationData
->DeviceIdLength
> 0) &&
1137 (HwInitializationData
->DeviceId
!= NULL
))
1139 PortConfig
.BusInterruptLevel
= 0;
1141 /* Get PCI device data */
1142 TRACE("VendorId '%.*s' DeviceId '%.*s'\n",
1143 HwInitializationData
->VendorIdLength
,
1144 HwInitializationData
->VendorId
,
1145 HwInitializationData
->DeviceIdLength
,
1146 HwInitializationData
->DeviceId
);
1148 if (!SpiGetPciConfigData(HwInitializationData
,
1153 /* Continue to the next bus, nothing here */
1154 MmHeapFree(DeviceExtension
);
1155 return STATUS_INTERNAL_ERROR
;
1158 if (!PortConfig
.BusInterruptLevel
)
1160 /* Bypass this slot, because no interrupt was assigned */
1161 MmHeapFree(DeviceExtension
);
1162 return STATUS_INTERNAL_ERROR
;
1166 if (HwInitializationData
->HwFindAdapter(
1167 DeviceExtension
->MiniPortDeviceExtension
,
1172 &Again
) != SP_RETURN_FOUND
)
1174 MmHeapFree(DeviceExtension
);
1175 return STATUS_INTERNAL_ERROR
;
1178 /* Copy all stuff which we ever need from PortConfig to the DeviceExtension */
1179 if (PortConfig
.MaximumNumberOfTargets
> SCSI_MAXIMUM_TARGETS_PER_BUS
)
1180 DeviceExtension
->MaxTargedIds
= SCSI_MAXIMUM_TARGETS_PER_BUS
;
1182 DeviceExtension
->MaxTargedIds
= PortConfig
.MaximumNumberOfTargets
;
1184 DeviceExtension
->BusNum
= PortConfig
.SystemIoBusNumber
;
1186 TRACE("Adapter found: buses = %d, targets = %d\n",
1187 PortConfig
.NumberOfBuses
, DeviceExtension
->MaxTargedIds
);
1189 /* Initialize adapter */
1190 if (!DeviceExtension
->HwInitialize(DeviceExtension
->MiniPortDeviceExtension
))
1192 MmHeapFree(DeviceExtension
);
1193 return STATUS_INTERNAL_ERROR
;
1197 for (ScsiBus
= 0; ScsiBus
< PortConfig
.NumberOfBuses
; ScsiBus
++)
1199 SpiScanAdapter(DeviceExtension
, PortConfig
.SystemIoBusNumber
, ScsiBus
);
1200 PortConfig
.SystemIoBusNumber
++;
1203 FirstConfigCall
= FALSE
;
1210 return STATUS_SUCCESS
;
1215 ScsiPortIoMapTransfer(
1216 IN PVOID HwDeviceExtension
,
1217 IN PSCSI_REQUEST_BLOCK Srb
,
1218 IN PVOID LogicalAddress
,
1228 IN PVOID HwDeviceExtension
,
1229 IN PSCSI_REQUEST_BLOCK Srb OPTIONAL
,
1243 IN PVOID WriteBuffer
,
1244 IN PVOID ReadBuffer
,
1247 RtlMoveMemory(WriteBuffer
, ReadBuffer
, Length
);
1252 ScsiPortNotification(
1253 IN SCSI_NOTIFICATION_TYPE NotificationType
,
1254 IN PVOID HwDeviceExtension
,
1257 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
1258 PSCSI_REQUEST_BLOCK Srb
;
1261 DeviceExtension
= ((PSCSI_PORT_DEVICE_EXTENSION
)HwDeviceExtension
) - 1;
1263 va_start(ap
, HwDeviceExtension
);
1265 switch (NotificationType
)
1267 case RequestComplete
:
1268 /* Mask the SRB as completed */
1269 Srb
= va_arg(ap
, PSCSI_REQUEST_BLOCK
);
1270 Srb
->SrbFlags
&= ~SRB_FLAGS_IS_ACTIVE
;
1274 /* Say that device is ready */
1275 DeviceExtension
->InterruptFlags
|= SCSI_PORT_NEXT_REQUEST_READY
;
1288 ScsiPortReadPortBufferUchar(
1293 __inbytestring(H2I(Port
), Buffer
, Count
);
1298 ScsiPortReadPortBufferUlong(
1303 __indwordstring(H2I(Port
), Buffer
, Count
);
1308 ScsiPortReadPortBufferUshort(
1313 __inwordstring(H2I(Port
), Buffer
, Count
);
1318 ScsiPortReadPortUchar(
1321 TRACE("ScsiPortReadPortUchar(%p)\n", Port
);
1323 return READ_PORT_UCHAR(Port
);
1328 ScsiPortReadPortUlong(
1331 return READ_PORT_ULONG(Port
);
1336 ScsiPortReadPortUshort(
1339 return READ_PORT_USHORT(Port
);
1344 ScsiPortReadRegisterBufferUchar(
1355 ScsiPortReadRegisterBufferUlong(
1366 ScsiPortReadRegisterBufferUshort(
1367 IN PUSHORT Register
,
1377 ScsiPortReadRegisterUchar(
1380 return READ_REGISTER_UCHAR(Register
);
1385 ScsiPortReadRegisterUlong(
1388 return READ_REGISTER_ULONG(Register
);
1393 ScsiPortReadRegisterUshort(
1394 IN PUSHORT Register
)
1396 return READ_REGISTER_USHORT(Register
);
1401 ScsiPortSetBusDataByOffset(
1402 IN PVOID DeviceExtension
,
1403 IN ULONG BusDataType
,
1404 IN ULONG SystemIoBusNumber
,
1405 IN ULONG SlotNumber
,
1417 ScsiPortStallExecution(
1420 KeStallExecutionProcessor(Delay
);
1425 ScsiPortValidateRange(
1426 IN PVOID HwDeviceExtension
,
1427 IN INTERFACE_TYPE BusType
,
1428 IN ULONG SystemIoBusNumber
,
1429 IN SCSI_PHYSICAL_ADDRESS IoAddress
,
1430 IN ULONG NumberOfBytes
,
1431 IN BOOLEAN InIoSpace
)
1445 ScsiPortWritePortBufferUchar(
1450 __outbytestring(H2I(Port
), Buffer
, Count
);
1455 ScsiPortWritePortBufferUlong(
1460 __outdwordstring(H2I(Port
), Buffer
, Count
);
1465 ScsiPortWritePortBufferUshort(
1470 __outwordstring(H2I(Port
), Buffer
, Count
);
1475 ScsiPortWritePortUchar(
1479 WRITE_PORT_UCHAR(Port
, Value
);
1484 ScsiPortWritePortUlong(
1488 WRITE_PORT_ULONG(Port
, Value
);
1493 ScsiPortWritePortUshort(
1497 WRITE_PORT_USHORT(Port
, Value
);
1502 ScsiPortWriteRegisterBufferUchar(
1513 ScsiPortWriteRegisterBufferUlong(
1524 ScsiPortWriteRegisterBufferUshort(
1525 IN PUSHORT Register
,
1535 ScsiPortWriteRegisterUchar(
1539 WRITE_REGISTER_UCHAR(Register
, Value
);
1544 ScsiPortWriteRegisterUlong(
1548 WRITE_REGISTER_ULONG(Register
, Value
);
1553 ScsiPortWriteRegisterUshort(
1554 IN PUSHORT Register
,
1557 WRITE_REGISTER_USHORT(Register
, Value
);
1560 extern char __ImageBase
;
1563 LoadBootDeviceDriver(VOID
)
1565 PIMAGE_NT_HEADERS NtHeaders
;
1566 LIST_ENTRY ModuleListHead
;
1567 PIMAGE_IMPORT_DESCRIPTOR ImportTable
;
1568 ULONG ImportTableSize
;
1569 PLDR_DATA_TABLE_ENTRY BootDdDTE
, FreeldrDTE
;
1570 CHAR NtBootDdPath
[MAX_PATH
];
1572 ULONG (NTAPI
*EntryPoint
)(IN PVOID DriverObject
, IN PVOID RegistryPath
);
1575 /* Initialize the loaded module list */
1576 InitializeListHead(&ModuleListHead
);
1578 /* Create full ntbootdd.sys path */
1579 MachDiskGetBootPath(NtBootDdPath
, sizeof(NtBootDdPath
));
1580 strcat(NtBootDdPath
, "\\NTBOOTDD.SYS");
1583 Status
= WinLdrLoadImage(NtBootDdPath
, LoaderBootDriver
, &ImageBase
);
1586 /* That's OK. File simply doesn't exist */
1590 /* Allocate a DTE for ntbootdd */
1591 Status
= WinLdrAllocateDataTableEntry(&ModuleListHead
, "ntbootdd.sys",
1592 "NTBOOTDD.SYS", ImageBase
, &BootDdDTE
);
1596 /* Add the PE part of freeldr.sys to the list of loaded executables, it
1597 contains Scsiport* exports, imported by ntbootdd.sys */
1598 Status
= WinLdrAllocateDataTableEntry(&ModuleListHead
, "scsiport.sys",
1599 "FREELDR.SYS", &__ImageBase
, &FreeldrDTE
);
1602 RemoveEntryList(&BootDdDTE
->InLoadOrderLinks
);
1607 Status
= WinLdrScanImportDescriptorTable(&ModuleListHead
, "", BootDdDTE
);
1609 /* Now unlinkt the DTEs, they won't be valid later */
1610 RemoveEntryList(&BootDdDTE
->InLoadOrderLinks
);
1611 RemoveEntryList(&FreeldrDTE
->InLoadOrderLinks
);
1616 /* Change imports to PA */
1617 ImportTable
= (PIMAGE_IMPORT_DESCRIPTOR
)RtlImageDirectoryEntryToData(VaToPa(BootDdDTE
->DllBase
),
1618 TRUE
, IMAGE_DIRECTORY_ENTRY_IMPORT
, &ImportTableSize
);
1619 for (;(ImportTable
->Name
!= 0) && (ImportTable
->FirstThunk
!= 0);ImportTable
++)
1621 PIMAGE_THUNK_DATA ThunkData
= (PIMAGE_THUNK_DATA
)VaToPa(RVA(BootDdDTE
->DllBase
, ImportTable
->FirstThunk
));
1623 while (((PIMAGE_THUNK_DATA
)ThunkData
)->u1
.AddressOfData
!= 0)
1625 ThunkData
->u1
.Function
= (ULONG
)VaToPa((PVOID
)ThunkData
->u1
.Function
);
1630 /* Relocate image to PA */
1631 NtHeaders
= RtlImageNtHeader(VaToPa(BootDdDTE
->DllBase
));
1634 Status
= (BOOLEAN
)LdrRelocateImageWithBias(
1635 VaToPa(BootDdDTE
->DllBase
),
1636 NtHeaders
->OptionalHeader
.ImageBase
- (ULONG_PTR
)BootDdDTE
->DllBase
,
1639 TRUE
, /* in case of conflict still return success */
1644 /* Call the entrypoint */
1645 EntryPoint
= VaToPa(BootDdDTE
->EntryPoint
);
1646 (*EntryPoint
)(NULL
, NULL
);