3 * Copyright (C) 2001, 2002 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS Storage Stack
23 * FILE: drivers/storage/scsiport/scsiport.c
24 * PURPOSE: SCSI port driver
25 * PROGRAMMER: Eric Kohl (ekohl@rz-online.de)
26 * Aleksey Bragin (aleksey reactos org)
29 /* INCLUDES *****************************************************************/
45 #include "scsiport_int.h"
52 ULONG InternalDebugLevel
= 0x00;
54 /* TYPES *********************************************************************/
56 /* GLOBALS *******************************************************************/
59 SpiGetPciConfigData(IN PDRIVER_OBJECT DriverObject
,
60 IN PDEVICE_OBJECT DeviceObject
,
61 IN
struct _HW_INITIALIZATION_DATA
*HwInitializationData
,
62 IN OUT PPORT_CONFIGURATION_INFORMATION PortConfig
,
63 IN PUNICODE_STRING RegistryPath
,
65 IN OUT PPCI_SLOT_NUMBER NextSlotNumber
);
67 static NTSTATUS STDCALL
68 ScsiPortCreateClose(IN PDEVICE_OBJECT DeviceObject
,
71 static DRIVER_DISPATCH ScsiPortDispatchScsi
;
72 static NTSTATUS STDCALL
73 ScsiPortDispatchScsi(IN PDEVICE_OBJECT DeviceObject
,
76 static NTSTATUS STDCALL
77 ScsiPortDeviceControl(IN PDEVICE_OBJECT DeviceObject
,
80 static DRIVER_STARTIO ScsiPortStartIo
;
82 ScsiPortStartIo(IN PDEVICE_OBJECT DeviceObject
,
85 static BOOLEAN STDCALL
86 ScsiPortStartPacket(IN OUT PVOID Context
);
90 SpiAdapterControl(PDEVICE_OBJECT DeviceObject
, PIRP Irp
,
91 PVOID MapRegisterBase
, PVOID Context
);
93 static PSCSI_PORT_LUN_EXTENSION
94 SpiAllocateLunExtension (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
);
96 static PSCSI_PORT_LUN_EXTENSION
97 SpiGetLunExtension (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
102 static PSCSI_REQUEST_BLOCK_INFO
103 SpiAllocateSrbStructures(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
104 PSCSI_PORT_LUN_EXTENSION LunExtension
,
105 PSCSI_REQUEST_BLOCK Srb
);
108 SpiSendInquiry (IN PDEVICE_OBJECT DeviceObject
,
109 IN PSCSI_LUN_INFO LunInfo
);
112 SpiScanAdapter (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
);
115 SpiGetInquiryData (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
118 static PSCSI_REQUEST_BLOCK_INFO
119 SpiGetSrbData(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
125 static KSERVICE_ROUTINE ScsiPortIsr
;
126 static BOOLEAN STDCALL
127 ScsiPortIsr(IN PKINTERRUPT Interrupt
,
128 IN PVOID ServiceContext
);
131 ScsiPortDpcForIsr(IN PKDPC Dpc
,
132 IN PDEVICE_OBJECT DpcDeviceObject
,
134 IN PVOID DpcContext
);
137 ScsiPortIoTimer(PDEVICE_OBJECT DeviceObject
,
141 SpiBuildDeviceMap (PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
142 PUNICODE_STRING RegistryPath
);
145 SpiStatusSrbToNt(UCHAR SrbStatus
);
148 SpiSendRequestSense(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
149 IN PSCSI_REQUEST_BLOCK Srb
);
151 static IO_COMPLETION_ROUTINE SpiCompletionRoutine
;
153 SpiCompletionRoutine(PDEVICE_OBJECT DeviceObject
,
159 SpiProcessCompletedRequest(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
160 IN PSCSI_REQUEST_BLOCK_INFO SrbInfo
,
161 OUT PBOOLEAN NeedToCallStartIo
);
164 SpiGetNextRequestFromLun(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
165 IN PSCSI_PORT_LUN_EXTENSION LunExtension
);
168 SpiMiniportTimerDpc(IN
struct _KDPC
*Dpc
,
169 IN PVOID DeviceObject
,
170 IN PVOID SystemArgument1
,
171 IN PVOID SystemArgument2
);
174 SpiCreatePortConfig(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
175 PHW_INITIALIZATION_DATA HwInitData
,
176 PCONFIGURATION_INFO InternalConfigInfo
,
177 PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
181 SpQueryDeviceCallout(IN PVOID Context
,
182 IN PUNICODE_STRING PathName
,
183 IN INTERFACE_TYPE BusType
,
185 IN PKEY_VALUE_FULL_INFORMATION
*BusInformation
,
186 IN CONFIGURATION_TYPE ControllerType
,
187 IN ULONG ControllerNumber
,
188 IN PKEY_VALUE_FULL_INFORMATION
*ControllerInformation
,
189 IN CONFIGURATION_TYPE PeripheralType
,
190 IN ULONG PeripheralNumber
,
191 IN PKEY_VALUE_FULL_INFORMATION
*PeripheralInformation
);
194 SpiParseDeviceInfo(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
196 IN PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
197 IN PCONFIGURATION_INFO InternalConfigInfo
,
201 SpiResourceToConfig(IN PHW_INITIALIZATION_DATA HwInitializationData
,
202 IN PCM_FULL_RESOURCE_DESCRIPTOR ResourceDescriptor
,
203 IN PPORT_CONFIGURATION_INFORMATION PortConfig
);
205 static PCM_RESOURCE_LIST
206 SpiConfigToResource(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
207 PPORT_CONFIGURATION_INFORMATION PortConfig
);
210 SpiCleanupAfterInit(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
);
213 SpiHandleAttachRelease(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
217 SpiAllocateCommonBuffer(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
, ULONG NonCachedSize
);
221 /* FUNCTIONS *****************************************************************/
223 /**********************************************************************
228 * This function initializes the driver.
235 * System allocated Driver Object for this driver.
238 * Name of registry driver service key.
245 DriverEntry(IN PDRIVER_OBJECT DriverObject
,
246 IN PUNICODE_STRING RegistryPath
)
248 DPRINT("ScsiPort Driver %s\n", VERSION
);
249 return(STATUS_SUCCESS
);
253 /**********************************************************************
258 * Prints debugging messages.
265 * Debug level of the given message.
268 * Pointer to printf()-compatible format string.
271 Additional output data (see printf()).
280 ScsiDebugPrint(IN ULONG DebugPrintLevel
,
281 IN PCHAR DebugMessage
,
287 if (DebugPrintLevel
> InternalDebugLevel
)
290 va_start(ap
, DebugMessage
);
291 vsprintf(Buffer
, DebugMessage
, ap
);
302 ScsiPortCompleteRequest(IN PVOID HwDeviceExtension
,
308 DPRINT("ScsiPortCompleteRequest()\n");
316 ScsiPortFlushDma(IN PVOID HwDeviceExtension
)
318 DPRINT("ScsiPortFlushDma()\n");
327 ScsiPortFreeDeviceBase(IN PVOID HwDeviceExtension
,
328 IN PVOID MappedAddress
)
330 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
331 PMAPPED_ADDRESS NextMa
, LastMa
;
333 //DPRINT("ScsiPortFreeDeviceBase() called\n");
335 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
336 SCSI_PORT_DEVICE_EXTENSION
,
337 MiniPortDeviceExtension
);
340 /* Initialize our pointers */
341 NextMa
= DeviceExtension
->MappedAddressList
;
346 if (NextMa
->MappedAddress
== MappedAddress
)
349 MmUnmapIoSpace(MappedAddress
, NextMa
->NumberOfBytes
);
351 /* Remove it from the list */
352 if (NextMa
== DeviceExtension
->MappedAddressList
)
354 /* Remove the first entry */
355 DeviceExtension
->MappedAddressList
= NextMa
->NextMappedAddress
;
359 LastMa
->NextMappedAddress
= NextMa
->NextMappedAddress
;
362 /* Free the resources and quit */
370 NextMa
= NextMa
->NextMappedAddress
;
380 ScsiPortGetBusData(IN PVOID DeviceExtension
,
381 IN ULONG BusDataType
,
382 IN ULONG SystemIoBusNumber
,
389 /* If Length is non-zero, just forward the call to
390 HalGetBusData() function */
391 return HalGetBusData(BusDataType
,
398 /* We have a more complex case here */
408 ScsiPortGetDeviceBase(IN PVOID HwDeviceExtension
,
409 IN INTERFACE_TYPE BusType
,
410 IN ULONG SystemIoBusNumber
,
411 IN SCSI_PHYSICAL_ADDRESS IoAddress
,
412 IN ULONG NumberOfBytes
,
413 IN BOOLEAN InIoSpace
)
415 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
416 PHYSICAL_ADDRESS TranslatedAddress
;
417 PMAPPED_ADDRESS DeviceBase
;
421 //DPRINT ("ScsiPortGetDeviceBase() called\n");
423 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
424 SCSI_PORT_DEVICE_EXTENSION
,
425 MiniPortDeviceExtension
);
427 AddressSpace
= (ULONG
)InIoSpace
;
428 if (HalTranslateBusAddress(BusType
,
432 &TranslatedAddress
) == FALSE
)
438 if (AddressSpace
!= 0)
439 return((PVOID
)TranslatedAddress
.u
.LowPart
);
441 MappedAddress
= MmMapIoSpace(TranslatedAddress
,
445 DeviceBase
= ExAllocatePoolWithTag(NonPagedPool
,
446 sizeof(MAPPED_ADDRESS
), TAG_SCSIPORT
);
448 if (DeviceBase
== NULL
)
449 return MappedAddress
;
451 DeviceBase
->MappedAddress
= MappedAddress
;
452 DeviceBase
->NumberOfBytes
= NumberOfBytes
;
453 DeviceBase
->IoAddress
= IoAddress
;
454 DeviceBase
->BusNumber
= SystemIoBusNumber
;
456 /* Link it to the Device Extension list */
457 DeviceBase
->NextMappedAddress
= DeviceExtension
->MappedAddressList
;
458 DeviceExtension
->MappedAddressList
= DeviceBase
;
460 return MappedAddress
;
467 ScsiPortGetLogicalUnit(IN PVOID HwDeviceExtension
,
474 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
475 PSCSI_PORT_LUN_EXTENSION LunExtension
;
478 DPRINT("ScsiPortGetLogicalUnit() called\n");
480 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
481 SCSI_PORT_DEVICE_EXTENSION
,
482 MiniPortDeviceExtension
);
483 if (IsListEmpty(&DeviceExtension
->LunExtensionListHead
))
486 Entry
= DeviceExtension
->LunExtensionListHead
.Flink
;
487 while (Entry
!= &DeviceExtension
->LunExtensionListHead
)
489 LunExtension
= CONTAINING_RECORD(Entry
,
490 SCSI_PORT_LUN_EXTENSION
,
492 if (LunExtension
->PathId
== PathId
&&
493 LunExtension
->TargetId
== TargetId
&&
494 LunExtension
->Lun
== Lun
)
496 return (PVOID
)&LunExtension
->MiniportLunExtension
;
499 Entry
= Entry
->Flink
;
509 SCSI_PHYSICAL_ADDRESS STDCALL
510 ScsiPortGetPhysicalAddress(IN PVOID HwDeviceExtension
,
511 IN PSCSI_REQUEST_BLOCK Srb OPTIONAL
,
512 IN PVOID VirtualAddress
,
515 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
516 SCSI_PHYSICAL_ADDRESS PhysicalAddress
;
517 ULONG BufferLength
= 0;
519 PSCSI_SG_ADDRESS SGList
;
520 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
522 DPRINT("ScsiPortGetPhysicalAddress(%p %p %p %p)\n",
523 HwDeviceExtension
, Srb
, VirtualAddress
, Length
);
525 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
526 SCSI_PORT_DEVICE_EXTENSION
,
527 MiniPortDeviceExtension
);
529 if (Srb
== NULL
|| Srb
->SenseInfoBuffer
== VirtualAddress
)
531 /* Simply look it up in the allocated common buffer */
532 Offset
= (PUCHAR
)VirtualAddress
- (PUCHAR
)DeviceExtension
->SrbExtensionBuffer
;
534 BufferLength
= DeviceExtension
->CommonBufferLength
- Offset
;
535 PhysicalAddress
.QuadPart
= DeviceExtension
->PhysicalAddress
.QuadPart
+ Offset
;
537 else if (DeviceExtension
->MapRegisters
)
539 /* Scatter-gather list must be used */
540 SrbInfo
= SpiGetSrbData(DeviceExtension
,
546 SGList
= SrbInfo
->ScatterGather
;
548 /* Find needed item in the SG list */
549 Offset
= (PCHAR
)VirtualAddress
- (PCHAR
)Srb
->DataBuffer
;
550 while (Offset
>= SGList
->Length
)
552 Offset
-= SGList
->Length
;
556 /* We're done, store length and physical address */
557 BufferLength
= SGList
->Length
- Offset
;
558 PhysicalAddress
.QuadPart
= SGList
->PhysicalAddress
.QuadPart
+ Offset
;
564 PhysicalAddress
.QuadPart
= (LONGLONG
)(SP_UNINITIALIZED_VALUE
);
567 *Length
= BufferLength
;
568 return PhysicalAddress
;
575 PSCSI_REQUEST_BLOCK STDCALL
576 ScsiPortGetSrb(IN PVOID DeviceExtension
,
582 DPRINT1("ScsiPortGetSrb() unimplemented\n");
592 ScsiPortGetUncachedExtension(IN PVOID HwDeviceExtension
,
593 IN PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
594 IN ULONG NumberOfBytes
)
596 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
597 DEVICE_DESCRIPTION DeviceDescription
;
598 ULONG MapRegistersCount
;
601 DPRINT("ScsiPortGetUncachedExtension(%p %p %lu)\n",
602 HwDeviceExtension
, ConfigInfo
, NumberOfBytes
);
604 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
605 SCSI_PORT_DEVICE_EXTENSION
,
606 MiniPortDeviceExtension
);
608 /* Check for allocated common DMA buffer */
609 if (DeviceExtension
->SrbExtensionBuffer
!= NULL
)
611 DPRINT1("The HBA has already got a common DMA buffer!\n");
615 /* Check for DMA adapter object */
616 if (DeviceExtension
->AdapterObject
== NULL
)
618 /* Initialize DMA adapter description */
619 RtlZeroMemory(&DeviceDescription
, sizeof(DEVICE_DESCRIPTION
));
621 DeviceDescription
.Version
= DEVICE_DESCRIPTION_VERSION
;
622 DeviceDescription
.Master
= ConfigInfo
->Master
;
623 DeviceDescription
.ScatterGather
= ConfigInfo
->ScatterGather
;
624 DeviceDescription
.DemandMode
= ConfigInfo
->DemandMode
;
625 DeviceDescription
.Dma32BitAddresses
= ConfigInfo
->Dma32BitAddresses
;
626 DeviceDescription
.BusNumber
= ConfigInfo
->SystemIoBusNumber
;
627 DeviceDescription
.DmaChannel
= ConfigInfo
->DmaChannel
;
628 DeviceDescription
.InterfaceType
= ConfigInfo
->AdapterInterfaceType
;
629 DeviceDescription
.DmaWidth
= ConfigInfo
->DmaWidth
;
630 DeviceDescription
.DmaSpeed
= ConfigInfo
->DmaSpeed
;
631 DeviceDescription
.MaximumLength
= ConfigInfo
->MaximumTransferLength
;
632 DeviceDescription
.DmaPort
= ConfigInfo
->DmaPort
;
634 /* Get a DMA adapter object */
635 DeviceExtension
->AdapterObject
=
636 HalGetAdapter(&DeviceDescription
, &MapRegistersCount
);
638 /* Fail in case of error */
639 if (DeviceExtension
->AdapterObject
== NULL
)
641 DPRINT1("HalGetAdapter() failed\n");
645 /* Set number of physical breaks */
646 if (ConfigInfo
->NumberOfPhysicalBreaks
!= 0 &&
647 MapRegistersCount
> ConfigInfo
->NumberOfPhysicalBreaks
)
649 DeviceExtension
->PortCapabilities
.MaximumPhysicalPages
=
650 ConfigInfo
->NumberOfPhysicalBreaks
;
654 DeviceExtension
->PortCapabilities
.MaximumPhysicalPages
= MapRegistersCount
;
658 /* Update auto request sense feature */
659 DeviceExtension
->SupportsAutoSense
= ConfigInfo
->AutoRequestSense
;
661 /* Update Srb extension size */
662 if (DeviceExtension
->SrbExtensionSize
!= ConfigInfo
->SrbExtensionSize
)
663 DeviceExtension
->SrbExtensionSize
= ConfigInfo
->SrbExtensionSize
;
665 /* Update Srb extension alloc flag */
666 if (ConfigInfo
->AutoRequestSense
|| DeviceExtension
->SrbExtensionSize
)
667 DeviceExtension
->NeedSrbExtensionAlloc
= TRUE
;
669 /* Allocate a common DMA buffer */
670 Status
= SpiAllocateCommonBuffer(DeviceExtension
, NumberOfBytes
);
672 if (!NT_SUCCESS(Status
))
674 DPRINT1("SpiAllocateCommonBuffer() failed with Status = 0x%08X!\n", Status
);
678 return DeviceExtension
->NonCachedExtension
;
682 SpiAllocateCommonBuffer(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
, ULONG NonCachedSize
)
684 PVOID
*SrbExtension
, CommonBuffer
;
685 ULONG CommonBufferLength
, BufSize
;
687 /* If size is 0, set it to 16 */
688 if (!DeviceExtension
->SrbExtensionSize
)
689 DeviceExtension
->SrbExtensionSize
= 16;
692 BufSize
= DeviceExtension
->SrbExtensionSize
;
694 /* Add autosense data size if needed */
695 if (DeviceExtension
->SupportsAutoSense
)
696 BufSize
+= sizeof(SENSE_DATA
);
700 BufSize
= (BufSize
+ sizeof(LONGLONG
) - 1) & ~(sizeof(LONGLONG
) - 1);
702 /* Sum up into the total common buffer length, and round it to page size */
704 ROUND_TO_PAGES(NonCachedSize
+ BufSize
* DeviceExtension
->RequestsNumber
);
707 if (!DeviceExtension
->AdapterObject
)
709 /* From nonpaged pool if there is no DMA */
710 CommonBuffer
= ExAllocatePoolWithTag(NonPagedPool
, CommonBufferLength
, TAG_SCSIPORT
);
714 /* Perform a full request since we have a DMA adapter*/
715 CommonBuffer
= HalAllocateCommonBuffer(DeviceExtension
->AdapterObject
,
717 &DeviceExtension
->PhysicalAddress
,
721 /* Fail in case of error */
723 return STATUS_INSUFFICIENT_RESOURCES
;
726 RtlZeroMemory(CommonBuffer
, CommonBufferLength
);
728 /* Store its size in Device Extension */
729 DeviceExtension
->CommonBufferLength
= CommonBufferLength
;
731 /* SrbExtension buffer is located at the beginning of the buffer */
732 DeviceExtension
->SrbExtensionBuffer
= CommonBuffer
;
734 /* Non-cached extension buffer is located at the end of
738 CommonBufferLength
-= NonCachedSize
;
739 DeviceExtension
->NonCachedExtension
= (PUCHAR
)CommonBuffer
+ CommonBufferLength
;
743 DeviceExtension
->NonCachedExtension
= NULL
;
746 if (DeviceExtension
->NeedSrbExtensionAlloc
)
748 /* Look up how many SRB data structures we need */
749 DeviceExtension
->SrbDataCount
= CommonBufferLength
/ BufSize
;
751 /* Initialize the free SRB extensions list */
752 SrbExtension
= (PVOID
*)CommonBuffer
;
753 DeviceExtension
->FreeSrbExtensions
= SrbExtension
;
755 /* Fill the remainding pointers (if we have more than 1 SRB) */
756 while (CommonBufferLength
>= 2 * BufSize
)
758 *SrbExtension
= (PVOID
*)((PCHAR
)SrbExtension
+ BufSize
);
759 SrbExtension
= *SrbExtension
;
761 CommonBufferLength
-= BufSize
;
765 return STATUS_SUCCESS
;
774 ScsiPortGetVirtualAddress(IN PVOID HwDeviceExtension
,
775 IN SCSI_PHYSICAL_ADDRESS PhysicalAddress
)
777 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
780 DPRINT("ScsiPortGetVirtualAddress(%p %I64x)\n",
781 HwDeviceExtension
, PhysicalAddress
.QuadPart
);
783 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
784 SCSI_PORT_DEVICE_EXTENSION
,
785 MiniPortDeviceExtension
);
787 if (DeviceExtension
->PhysicalAddress
.QuadPart
> PhysicalAddress
.QuadPart
)
790 Offset
= (ULONG
)(PhysicalAddress
.QuadPart
- DeviceExtension
->PhysicalAddress
.QuadPart
);
792 if (Offset
>= DeviceExtension
->CommonBufferLength
)
795 return (PVOID
)((ULONG_PTR
)DeviceExtension
->SrbExtensionBuffer
+ Offset
);
799 SpiInitOpenKeys(PCONFIGURATION_INFO ConfigInfo
, PUNICODE_STRING RegistryPath
)
801 OBJECT_ATTRIBUTES ObjectAttributes
;
802 UNICODE_STRING KeyName
;
805 /* Open the service key */
806 InitializeObjectAttributes(&ObjectAttributes
,
808 OBJ_CASE_INSENSITIVE
,
812 Status
= ZwOpenKey(&ConfigInfo
->ServiceKey
,
816 if (!NT_SUCCESS(Status
))
818 DPRINT("Unable to open driver's registry key %wZ, status 0x%08x\n", RegistryPath
, Status
);
819 ConfigInfo
->ServiceKey
= NULL
;
822 /* If we could open driver's service key, then proceed to the Parameters key */
823 if (ConfigInfo
->ServiceKey
!= NULL
)
825 RtlInitUnicodeString(&KeyName
, L
"Parameters");
826 InitializeObjectAttributes(&ObjectAttributes
,
828 OBJ_CASE_INSENSITIVE
,
829 ConfigInfo
->ServiceKey
,
830 (PSECURITY_DESCRIPTOR
) NULL
);
833 Status
= ZwOpenKey(&ConfigInfo
->DeviceKey
,
837 if (NT_SUCCESS(Status
))
839 /* Yes, Parameters key exist, and it must be used instead of
841 ZwClose(ConfigInfo
->ServiceKey
);
842 ConfigInfo
->ServiceKey
= ConfigInfo
->DeviceKey
;
843 ConfigInfo
->DeviceKey
= NULL
;
847 if (ConfigInfo
->ServiceKey
!= NULL
)
849 /* Open the Device key */
850 RtlInitUnicodeString(&KeyName
, L
"Device");
851 InitializeObjectAttributes(&ObjectAttributes
,
853 OBJ_CASE_INSENSITIVE
,
854 ConfigInfo
->ServiceKey
,
857 /* We don't check for failure here - not needed */
858 ZwOpenKey(&ConfigInfo
->DeviceKey
,
865 /**********************************************************************
870 * Initializes SCSI port driver specific data.
877 * Pointer to the miniport driver's driver object.
880 * Pointer to the miniport driver's registry path.
882 * HwInitializationData
883 * Pointer to port driver specific configuration data.
886 Miniport driver specific context.
895 ScsiPortInitialize(IN PVOID Argument1
,
897 IN
struct _HW_INITIALIZATION_DATA
*HwInitializationData
,
900 PDRIVER_OBJECT DriverObject
= (PDRIVER_OBJECT
)Argument1
;
901 PUNICODE_STRING RegistryPath
= (PUNICODE_STRING
)Argument2
;
902 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
= NULL
;
903 PCONFIGURATION_INFORMATION SystemConfig
;
904 PPORT_CONFIGURATION_INFORMATION PortConfig
;
905 PORT_CONFIGURATION_INFORMATION InitialPortConfig
;
906 CONFIGURATION_INFO ConfigInfo
;
907 ULONG DeviceExtensionSize
;
908 ULONG PortConfigSize
;
910 BOOLEAN DeviceFound
= FALSE
;
911 BOOLEAN FirstConfigCall
= TRUE
;
916 PCI_SLOT_NUMBER SlotNumber
;
918 PDEVICE_OBJECT PortDeviceObject
;
919 WCHAR NameBuffer
[80];
920 UNICODE_STRING DeviceName
;
921 WCHAR DosNameBuffer
[80];
922 UNICODE_STRING DosDeviceName
;
923 PIO_SCSI_CAPABILITIES PortCapabilities
;
928 PCM_RESOURCE_LIST ResourceList
;
932 DPRINT ("ScsiPortInitialize() called!\n");
934 /* Check params for validity */
935 if ((HwInitializationData
->HwInitialize
== NULL
) ||
936 (HwInitializationData
->HwStartIo
== NULL
) ||
937 (HwInitializationData
->HwInterrupt
== NULL
) ||
938 (HwInitializationData
->HwFindAdapter
== NULL
) ||
939 (HwInitializationData
->HwResetBus
== NULL
))
941 return STATUS_INVALID_PARAMETER
;
945 DriverObject
->DriverStartIo
= ScsiPortStartIo
;
946 DriverObject
->MajorFunction
[IRP_MJ_CREATE
] = ScsiPortCreateClose
;
947 DriverObject
->MajorFunction
[IRP_MJ_CLOSE
] = ScsiPortCreateClose
;
948 DriverObject
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = ScsiPortDeviceControl
;
949 DriverObject
->MajorFunction
[IRP_MJ_INTERNAL_DEVICE_CONTROL
] = ScsiPortDeviceControl
;
950 DriverObject
->MajorFunction
[IRP_MJ_SCSI
] = ScsiPortDispatchScsi
;
952 /* Obtain configuration information */
953 SystemConfig
= IoGetConfigurationInformation();
955 /* Zero the internal configuration info structure */
956 RtlZeroMemory(&ConfigInfo
, sizeof(CONFIGURATION_INFO
));
958 /* Zero starting slot number */
959 SlotNumber
.u
.AsULONG
= 0;
961 /* Allocate space for access ranges */
962 if (HwInitializationData
->NumberOfAccessRanges
)
964 ConfigInfo
.AccessRanges
=
965 ExAllocatePoolWithTag(PagedPool
,
966 HwInitializationData
->NumberOfAccessRanges
* sizeof(ACCESS_RANGE
), TAG_SCSIPORT
);
969 if (ConfigInfo
.AccessRanges
== NULL
)
970 return STATUS_INSUFFICIENT_RESOURCES
;
973 /* Open registry keys */
974 SpiInitOpenKeys(&ConfigInfo
, (PUNICODE_STRING
)Argument2
);
976 /* Last adapter number = not known */
977 ConfigInfo
.LastAdapterNumber
= SP_UNINITIALIZED_VALUE
;
979 /* Calculate sizes of DeviceExtension and PortConfig */
980 DeviceExtensionSize
= sizeof(SCSI_PORT_DEVICE_EXTENSION
) +
981 HwInitializationData
->DeviceExtensionSize
;
983 MaxBus
= (HwInitializationData
->AdapterInterfaceType
== PCIBus
) ? 8 : 1;
984 DPRINT("MaxBus: %lu\n", MaxBus
);
988 /* Create a unicode device name */
990 L
"\\Device\\ScsiPort%lu",
991 SystemConfig
->ScsiPortCount
);
992 RtlInitUnicodeString(&DeviceName
, NameBuffer
);
994 DPRINT("Creating device: %wZ\n", &DeviceName
);
996 /* Create the port device */
997 Status
= IoCreateDevice(DriverObject
,
1000 FILE_DEVICE_CONTROLLER
,
1005 if (!NT_SUCCESS(Status
))
1007 DPRINT1("IoCreateDevice call failed! (Status 0x%lX)\n", Status
);
1008 PortDeviceObject
= NULL
;
1012 DPRINT ("Created device: %wZ (%p)\n", &DeviceName
, PortDeviceObject
);
1014 /* Set the buffering strategy here... */
1015 PortDeviceObject
->Flags
|= DO_DIRECT_IO
;
1016 PortDeviceObject
->AlignmentRequirement
= FILE_WORD_ALIGNMENT
; /* FIXME: Is this really needed? */
1018 /* Fill Device Extension */
1019 DeviceExtension
= PortDeviceObject
->DeviceExtension
;
1020 DeviceExtension
->Length
= DeviceExtensionSize
;
1021 DeviceExtension
->DeviceObject
= PortDeviceObject
;
1022 DeviceExtension
->PortNumber
= SystemConfig
->ScsiPortCount
;
1024 /* Driver's routines... */
1025 DeviceExtension
->HwInitialize
= HwInitializationData
->HwInitialize
;
1026 DeviceExtension
->HwStartIo
= HwInitializationData
->HwStartIo
;
1027 DeviceExtension
->HwInterrupt
= HwInitializationData
->HwInterrupt
;
1028 DeviceExtension
->HwResetBus
= HwInitializationData
->HwResetBus
;
1029 DeviceExtension
->HwDmaStarted
= HwInitializationData
->HwDmaStarted
;
1031 /* Extensions sizes */
1032 DeviceExtension
->MiniPortExtensionSize
= HwInitializationData
->DeviceExtensionSize
;
1033 DeviceExtension
->LunExtensionSize
= HwInitializationData
->SpecificLuExtensionSize
;
1034 DeviceExtension
->SrbExtensionSize
= HwInitializationData
->SrbExtensionSize
;
1036 /* Round Srb extension size to the quadword */
1037 DeviceExtension
->SrbExtensionSize
=
1038 ~(sizeof(LONGLONG
) - 1) & (DeviceExtension
->SrbExtensionSize
+
1039 sizeof(LONGLONG
) - 1);
1041 /* Fill some numbers (bus count, lun count, etc) */
1042 DeviceExtension
->MaxLunCount
= SCSI_MAXIMUM_LOGICAL_UNITS
;
1043 DeviceExtension
->RequestsNumber
= 16;
1045 /* Initialize the spin lock in the controller extension */
1046 KeInitializeSpinLock(&DeviceExtension
->IrqLock
);
1047 KeInitializeSpinLock(&DeviceExtension
->SpinLock
);
1049 /* Initialize the DPC object */
1050 IoInitializeDpcRequest(PortDeviceObject
,
1053 /* Initialize the device timer */
1054 DeviceExtension
->TimerCount
= -1;
1055 IoInitializeTimer(PortDeviceObject
,
1059 /* Initialize miniport timer */
1060 KeInitializeTimer(&DeviceExtension
->MiniportTimer
);
1061 KeInitializeDpc(&DeviceExtension
->MiniportTimerDpc
,
1062 SpiMiniportTimerDpc
,
1067 Status
= SpiCreatePortConfig(DeviceExtension
,
1068 HwInitializationData
,
1073 if (!NT_SUCCESS(Status
))
1076 /* Allocate and initialize port configuration info */
1077 PortConfigSize
= (sizeof(PORT_CONFIGURATION_INFORMATION
) +
1078 HwInitializationData
->NumberOfAccessRanges
*
1079 sizeof(ACCESS_RANGE
) + 7) & ~7;
1080 DeviceExtension
->PortConfig
= ExAllocatePoolWithTag(NonPagedPool
, PortConfigSize
, TAG_SCSIPORT
);
1082 /* Fail if failed */
1083 if (DeviceExtension
->PortConfig
== NULL
)
1085 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1089 PortConfig
= DeviceExtension
->PortConfig
;
1091 /* Copy information here */
1092 RtlCopyMemory(PortConfig
,
1094 sizeof(PORT_CONFIGURATION_INFORMATION
));
1097 /* Copy extension sizes into the PortConfig */
1098 PortConfig
->SpecificLuExtensionSize
= DeviceExtension
->LunExtensionSize
;
1099 PortConfig
->SrbExtensionSize
= DeviceExtension
->SrbExtensionSize
;
1101 /* Initialize Access ranges */
1102 if (HwInitializationData
->NumberOfAccessRanges
!= 0)
1104 PortConfig
->AccessRanges
= (PVOID
)(PortConfig
+1);
1106 /* Align to LONGLONG */
1107 PortConfig
->AccessRanges
= (PVOID
)((ULONG
)(PortConfig
->AccessRanges
) + 7);
1108 PortConfig
->AccessRanges
= (PVOID
)((ULONG
)(PortConfig
->AccessRanges
) & ~7);
1111 RtlCopyMemory(PortConfig
->AccessRanges
,
1112 ConfigInfo
.AccessRanges
,
1113 HwInitializationData
->NumberOfAccessRanges
* sizeof(ACCESS_RANGE
));
1116 /* Search for matching PCI device */
1117 if ((HwInitializationData
->AdapterInterfaceType
== PCIBus
) &&
1118 (HwInitializationData
->VendorIdLength
> 0) &&
1119 (HwInitializationData
->VendorId
!= NULL
) &&
1120 (HwInitializationData
->DeviceIdLength
> 0) &&
1121 (HwInitializationData
->DeviceId
!= NULL
))
1123 PortConfig
->BusInterruptLevel
= 0;
1125 /* Get PCI device data */
1126 DPRINT("VendorId '%.*s' DeviceId '%.*s'\n",
1127 HwInitializationData
->VendorIdLength
,
1128 HwInitializationData
->VendorId
,
1129 HwInitializationData
->DeviceIdLength
,
1130 HwInitializationData
->DeviceId
);
1132 if (!SpiGetPciConfigData(DriverObject
,
1134 HwInitializationData
,
1137 ConfigInfo
.BusNumber
,
1140 /* Continue to the next bus, nothing here */
1141 ConfigInfo
.BusNumber
++;
1142 DeviceExtension
->PortConfig
= NULL
;
1143 ExFreePool(PortConfig
);
1145 goto CreatePortConfig
;
1148 if (!PortConfig
->BusInterruptLevel
)
1150 /* Bypass this slot, because no interrupt was assigned */
1151 DeviceExtension
->PortConfig
= NULL
;
1152 ExFreePool(PortConfig
);
1153 goto CreatePortConfig
;
1158 DPRINT("Non-pci bus\n");
1161 /* Note: HwFindAdapter is called once for each bus */
1163 DPRINT("Calling HwFindAdapter() for Bus %lu\n", PortConfig
->SystemIoBusNumber
);
1164 Result
= (HwInitializationData
->HwFindAdapter
)(&DeviceExtension
->MiniPortDeviceExtension
,
1166 0, /* BusInformation */
1167 ConfigInfo
.Parameter
, /* ArgumentString */
1171 DPRINT("HwFindAdapter() Result: %lu Again: %s\n",
1172 Result
, (Again
) ? "True" : "False");
1174 /* Free MapRegisterBase, it's not needed anymore */
1175 if (DeviceExtension
->MapRegisterBase
!= NULL
)
1177 ExFreePool(DeviceExtension
->MapRegisterBase
);
1178 DeviceExtension
->MapRegisterBase
= NULL
;
1181 /* If result is nothing good... */
1182 if (Result
!= SP_RETURN_FOUND
)
1184 DPRINT("HwFindAdapter() Result: %lu\n", Result
);
1186 if (Result
== SP_RETURN_NOT_FOUND
)
1188 /* We can continue on the next bus */
1189 ConfigInfo
.BusNumber
++;
1192 DeviceExtension
->PortConfig
= NULL
;
1193 ExFreePool(PortConfig
);
1194 goto CreatePortConfig
;
1197 /* Otherwise, break */
1198 Status
= STATUS_INTERNAL_ERROR
;
1202 DPRINT("ScsiPortInitialize(): Found HBA! (%x), adapter Id %d\n",
1203 PortConfig
->BusInterruptVector
, PortConfig
->InitiatorBusId
[0]);
1205 /* If the SRB extension size was updated */
1206 if (!DeviceExtension
->NonCachedExtension
&&
1207 (PortConfig
->SrbExtensionSize
!= DeviceExtension
->SrbExtensionSize
))
1209 /* Set it (rounding to LONGLONG again) */
1210 DeviceExtension
->SrbExtensionSize
=
1211 (PortConfig
->SrbExtensionSize
+
1212 sizeof(LONGLONG
)) & ~(sizeof(LONGLONG
) - 1);
1215 /* The same with LUN extension size */
1216 if (PortConfig
->SpecificLuExtensionSize
!= DeviceExtension
->LunExtensionSize
)
1217 DeviceExtension
->LunExtensionSize
= PortConfig
->SpecificLuExtensionSize
;
1220 if (!((HwInitializationData
->AdapterInterfaceType
== PCIBus
) &&
1221 (HwInitializationData
->VendorIdLength
> 0) &&
1222 (HwInitializationData
->VendorId
!= NULL
) &&
1223 (HwInitializationData
->DeviceIdLength
> 0) &&
1224 (HwInitializationData
->DeviceId
!= NULL
)))
1226 /* Construct a resource list */
1227 ResourceList
= SpiConfigToResource(DeviceExtension
,
1232 UNICODE_STRING UnicodeString
;
1233 RtlInitUnicodeString(&UnicodeString
, L
"ScsiAdapter");
1234 DPRINT("Reporting resources\n");
1235 Status
= IoReportResourceUsage(&UnicodeString
,
1241 FIELD_OFFSET(CM_RESOURCE_LIST
,
1242 List
[0].PartialResourceList
.PartialDescriptors
) +
1243 ResourceList
->List
[0].PartialResourceList
.Count
1244 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
),
1247 ExFreePool(ResourceList
);
1249 /* In case of a failure or a conflict, break */
1250 if (Conflict
|| (!NT_SUCCESS(Status
)))
1253 Status
= STATUS_CONFLICTING_ADDRESSES
;
1259 /* Reset the Conflict var */
1262 /* Copy all stuff which we ever need from PortConfig to the DeviceExtension */
1263 if (PortConfig
->MaximumNumberOfTargets
> SCSI_MAXIMUM_TARGETS_PER_BUS
)
1264 DeviceExtension
->MaxTargedIds
= SCSI_MAXIMUM_TARGETS_PER_BUS
;
1266 DeviceExtension
->MaxTargedIds
= PortConfig
->MaximumNumberOfTargets
;
1268 DeviceExtension
->BusNum
= PortConfig
->NumberOfBuses
;
1269 DeviceExtension
->CachesData
= PortConfig
->CachesData
;
1270 DeviceExtension
->ReceiveEvent
= PortConfig
->ReceiveEvent
;
1271 DeviceExtension
->SupportsTaggedQueuing
= PortConfig
->TaggedQueuing
;
1272 DeviceExtension
->MultipleReqsPerLun
= PortConfig
->MultipleRequestPerLu
;
1274 /* If something was disabled via registry - apply it */
1275 if (ConfigInfo
.DisableMultipleLun
)
1276 DeviceExtension
->MultipleReqsPerLun
= PortConfig
->MultipleRequestPerLu
= FALSE
;
1278 if (ConfigInfo
.DisableTaggedQueueing
)
1279 DeviceExtension
->SupportsTaggedQueuing
= PortConfig
->MultipleRequestPerLu
= FALSE
;
1281 /* Check if we need to alloc SRB data */
1282 if (DeviceExtension
->SupportsTaggedQueuing
||
1283 DeviceExtension
->MultipleReqsPerLun
)
1285 DeviceExtension
->NeedSrbDataAlloc
= TRUE
;
1289 DeviceExtension
->NeedSrbDataAlloc
= FALSE
;
1292 /* Get a pointer to the port capabilities */
1293 PortCapabilities
= &DeviceExtension
->PortCapabilities
;
1295 /* Copy one field there */
1296 DeviceExtension
->MapBuffers
= PortConfig
->MapBuffers
;
1297 PortCapabilities
->AdapterUsesPio
= PortConfig
->MapBuffers
;
1299 if (DeviceExtension
->AdapterObject
== NULL
&&
1300 (PortConfig
->DmaChannel
!= SP_UNINITIALIZED_VALUE
|| PortConfig
->Master
))
1302 DPRINT1("DMA is not supported yet\n");
1306 if (DeviceExtension
->SrbExtensionBuffer
== NULL
&&
1307 (DeviceExtension
->SrbExtensionSize
!= 0 ||
1308 PortConfig
->AutoRequestSense
))
1310 DeviceExtension
->SupportsAutoSense
= PortConfig
->AutoRequestSense
;
1311 DeviceExtension
->NeedSrbExtensionAlloc
= TRUE
;
1313 /* Allocate common buffer */
1314 Status
= SpiAllocateCommonBuffer(DeviceExtension
, 0);
1316 /* Check for failure */
1317 if (!NT_SUCCESS(Status
))
1321 /* Allocate SrbData, if needed */
1322 if (DeviceExtension
->NeedSrbDataAlloc
)
1325 PSCSI_REQUEST_BLOCK_INFO SrbData
;
1327 if (DeviceExtension
->SrbDataCount
!= 0)
1328 Count
= DeviceExtension
->SrbDataCount
;
1330 Count
= DeviceExtension
->RequestsNumber
* 2;
1332 /* Allocate the data */
1333 SrbData
= ExAllocatePoolWithTag(NonPagedPool
, Count
* sizeof(SCSI_REQUEST_BLOCK_INFO
), TAG_SCSIPORT
);
1334 if (SrbData
== NULL
)
1335 return STATUS_INSUFFICIENT_RESOURCES
;
1337 RtlZeroMemory(SrbData
, Count
* sizeof(SCSI_REQUEST_BLOCK_INFO
));
1339 DeviceExtension
->SrbInfo
= SrbData
;
1340 DeviceExtension
->FreeSrbInfo
= SrbData
;
1341 DeviceExtension
->SrbDataCount
= Count
;
1343 /* Link it to the list */
1346 SrbData
->Requests
.Flink
= (PLIST_ENTRY
)(SrbData
+ 1);
1351 /* Mark the last entry of the list */
1353 SrbData
->Requests
.Flink
= NULL
;
1356 /* Initialize port capabilities */
1357 PortCapabilities
= &DeviceExtension
->PortCapabilities
;
1358 PortCapabilities
->Length
= sizeof(IO_SCSI_CAPABILITIES
);
1359 PortCapabilities
->MaximumTransferLength
= PortConfig
->MaximumTransferLength
;
1361 if (PortConfig
->ReceiveEvent
)
1362 PortCapabilities
->SupportedAsynchronousEvents
|= SRBEV_SCSI_ASYNC_NOTIFICATION
;
1364 PortCapabilities
->TaggedQueuing
= DeviceExtension
->SupportsTaggedQueuing
;
1365 PortCapabilities
->AdapterScansDown
= PortConfig
->AdapterScansDown
;
1367 if (PortConfig
->AlignmentMask
> PortDeviceObject
->AlignmentRequirement
)
1368 PortDeviceObject
->AlignmentRequirement
= PortConfig
->AlignmentMask
;
1370 PortCapabilities
->AlignmentMask
= PortDeviceObject
->AlignmentRequirement
;
1372 if (PortCapabilities
->MaximumPhysicalPages
== 0)
1374 PortCapabilities
->MaximumPhysicalPages
=
1375 BYTES_TO_PAGES(PortCapabilities
->MaximumTransferLength
);
1377 /* Apply miniport's limits */
1378 if (PortConfig
->NumberOfPhysicalBreaks
< PortCapabilities
->MaximumPhysicalPages
)
1380 PortCapabilities
->MaximumPhysicalPages
= PortConfig
->NumberOfPhysicalBreaks
;
1384 /* Deal with interrupts */
1385 if (DeviceExtension
->HwInterrupt
== NULL
||
1386 (PortConfig
->BusInterruptLevel
== 0 && PortConfig
->BusInterruptVector
== 0))
1389 KeInitializeSpinLock(&DeviceExtension
->IrqLock
);
1391 /* FIXME: Use synchronization routine */
1392 ASSERT("No interrupts branch requires changes in synchronization\n");
1394 DeviceExtension
->Interrupt
= (PVOID
)DeviceExtension
;
1395 DPRINT("No interrupts\n");
1400 /* Are 2 interrupts needed? */
1401 if (DeviceExtension
->HwInterrupt
!= NULL
&&
1402 (PortConfig
->BusInterruptLevel
!= 0 || PortConfig
->BusInterruptVector
!= 0) &&
1403 (PortConfig
->BusInterruptLevel2
!= 0 || PortConfig
->BusInterruptVector2
!= 0))
1405 DPRINT1("2 interrupts requested! Not yet supported\n");
1410 BOOLEAN InterruptShareable
;
1412 /* No, only 1 interrupt */
1413 DPRINT("1 interrupt, IRQ is %d\n", PortConfig
->BusInterruptLevel
);
1415 DeviceExtension
->InterruptLevel
= PortConfig
->BusInterruptLevel
;
1417 /* Register an interrupt handler for this device */
1418 MappedIrq
= HalGetInterruptVector(PortConfig
->AdapterInterfaceType
,
1419 PortConfig
->SystemIoBusNumber
,
1420 PortConfig
->BusInterruptLevel
,
1421 PortConfig
->BusInterruptVector
,
1425 /* Determing IRQ sharability as usual */
1426 if (PortConfig
->AdapterInterfaceType
== MicroChannel
||
1427 PortConfig
->InterruptMode
== LevelSensitive
)
1429 InterruptShareable
= TRUE
;
1433 InterruptShareable
= FALSE
;
1436 Status
= IoConnectInterrupt(&DeviceExtension
->Interrupt
,
1437 (PKSERVICE_ROUTINE
)ScsiPortIsr
,
1443 PortConfig
->InterruptMode
,
1448 if (!(NT_SUCCESS(Status
)))
1450 DPRINT1("Could not connect interrupt %d\n",
1451 PortConfig
->BusInterruptVector
);
1452 DeviceExtension
->Interrupt
= NULL
;
1459 /* Save IoAddress (from access ranges) */
1460 if (HwInitializationData
->NumberOfAccessRanges
!= 0)
1462 DeviceExtension
->IoAddress
=
1463 ((*(PortConfig
->AccessRanges
))[0]).RangeStart
.LowPart
;
1465 DPRINT("Io Address %x\n", DeviceExtension
->IoAddress
);
1468 /* Set flag that it's allowed to disconnect during this command */
1469 DeviceExtension
->Flags
|= SCSI_PORT_DISCONNECT_ALLOWED
;
1471 /* Initialize counter of active requests (-1 means there are none) */
1472 DeviceExtension
->ActiveRequestCounter
= -1;
1474 /* Analyze what we have about DMA */
1475 if (DeviceExtension
->AdapterObject
!= NULL
&&
1476 PortConfig
->Master
&&
1477 PortConfig
->NeedPhysicalAddresses
)
1479 DeviceExtension
->MapRegisters
= TRUE
;
1483 DeviceExtension
->MapRegisters
= FALSE
;
1486 /* Call HwInitialize at DISPATCH_LEVEL */
1487 KeRaiseIrql(DISPATCH_LEVEL
, &Dirql
);
1489 if (!KeSynchronizeExecution(DeviceExtension
->Interrupt
,
1490 DeviceExtension
->HwInitialize
,
1491 DeviceExtension
->MiniPortDeviceExtension
))
1493 DPRINT1("HwInitialize() failed!\n");
1495 Status
= STATUS_ADAPTER_HARDWARE_ERROR
;
1499 /* Check if a notification is needed */
1500 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
1502 /* Call DPC right away, because we're already at DISPATCH_LEVEL */
1503 ScsiPortDpcForIsr(NULL
,
1504 DeviceExtension
->DeviceObject
,
1509 /* Lower irql back to what it was */
1512 /* Start our timer */
1513 IoStartTimer(PortDeviceObject
);
1515 /* Initialize bus scanning information */
1516 DeviceExtension
->BusesConfig
= ExAllocatePoolWithTag(PagedPool
,
1517 sizeof(PSCSI_BUS_SCAN_INFO
) * DeviceExtension
->PortConfig
->NumberOfBuses
1518 + sizeof(ULONG
), TAG_SCSIPORT
);
1520 if (!DeviceExtension
->BusesConfig
)
1522 DPRINT1("Out of resources!\n");
1523 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1528 RtlZeroMemory(DeviceExtension
->BusesConfig
,
1529 sizeof(PSCSI_BUS_SCAN_INFO
) * DeviceExtension
->PortConfig
->NumberOfBuses
1532 /* Store number of buses there */
1533 DeviceExtension
->BusesConfig
->NumberOfBuses
= (UCHAR
)DeviceExtension
->BusNum
;
1535 /* Scan the adapter for devices */
1536 SpiScanAdapter(DeviceExtension
);
1538 /* Build the registry device map */
1539 SpiBuildDeviceMap(DeviceExtension
,
1540 (PUNICODE_STRING
)Argument2
);
1542 /* Create the dos device link */
1543 swprintf(DosNameBuffer
,
1545 SystemConfig
->ScsiPortCount
);
1546 RtlInitUnicodeString(&DosDeviceName
, DosNameBuffer
);
1547 IoCreateSymbolicLink(&DosDeviceName
, &DeviceName
);
1549 /* Increase the port count */
1550 SystemConfig
->ScsiPortCount
++;
1551 FirstConfigCall
= FALSE
;
1553 /* Increase adapter number and bus number respectively */
1554 ConfigInfo
.AdapterNumber
++;
1557 ConfigInfo
.BusNumber
++;
1559 DPRINT("Bus: %lu MaxBus: %lu\n", BusNumber
, MaxBus
);
1564 /* Clean up the mess */
1565 SpiCleanupAfterInit(DeviceExtension
);
1567 /* Close registry keys */
1568 if (ConfigInfo
.ServiceKey
!= NULL
)
1569 ZwClose(ConfigInfo
.ServiceKey
);
1571 if (ConfigInfo
.DeviceKey
!= NULL
)
1572 ZwClose(ConfigInfo
.DeviceKey
);
1574 if (ConfigInfo
.BusKey
!= NULL
)
1575 ZwClose(ConfigInfo
.BusKey
);
1577 if (ConfigInfo
.AccessRanges
!= NULL
)
1578 ExFreePool(ConfigInfo
.AccessRanges
);
1580 if (ConfigInfo
.Parameter
!= NULL
)
1581 ExFreePool(ConfigInfo
.Parameter
);
1583 DPRINT("ScsiPortInitialize() done, Status = 0x%08X, DeviceFound = %d!\n",
1584 Status
, DeviceFound
);
1586 return (DeviceFound
== FALSE
) ? Status
: STATUS_SUCCESS
;
1590 SpiCleanupAfterInit(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
)
1592 PSCSI_LUN_INFO LunInfo
;
1596 /* Check if we have something to clean up */
1597 if (DeviceExtension
== NULL
)
1600 /* Stop the timer and disconnect the interrupt */
1601 if (DeviceExtension
->Interrupt
)
1603 IoStopTimer(DeviceExtension
->DeviceObject
);
1604 IoDisconnectInterrupt(DeviceExtension
->Interrupt
);
1607 /* Delete ConfigInfo */
1608 if (DeviceExtension
->BusesConfig
)
1610 for (Bus
= 0; Bus
< DeviceExtension
->BusNum
; Bus
++)
1612 if (!DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
])
1615 LunInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LunInfo
;
1619 /* Free current, but save pointer to the next one */
1620 Ptr
= LunInfo
->Next
;
1621 ExFreePool(LunInfo
);
1625 ExFreePool(DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]);
1628 ExFreePool(DeviceExtension
->BusesConfig
);
1631 /* Free PortConfig */
1632 if (DeviceExtension
->PortConfig
)
1633 ExFreePool(DeviceExtension
->PortConfig
);
1636 for(Lun
= 0; Lun
< LUS_NUMBER
; Lun
++)
1638 while (DeviceExtension
->LunExtensionList
[Lun
])
1640 Ptr
= DeviceExtension
->LunExtensionList
[Lun
];
1641 DeviceExtension
->LunExtensionList
[Lun
] = DeviceExtension
->LunExtensionList
[Lun
]->Next
;
1647 /* Free common buffer (if it exists) */
1648 if (DeviceExtension
->SrbExtensionBuffer
!= NULL
&&
1649 DeviceExtension
->CommonBufferLength
!= 0)
1651 if (!DeviceExtension
->AdapterObject
)
1653 ExFreePool(DeviceExtension
->SrbExtensionBuffer
);
1658 HalFreeCommonBuffer(DeviceExtension
->AdapterObject
,
1659 DeviceExtension
->CommonBufferLength
,
1660 DeviceExtension
->PhysicalCommonBuffer
,
1661 DeviceExtension
->SrbExtensionBuffer
,
1668 if (DeviceExtension
->SrbInfo
!= NULL
)
1669 ExFreePool(DeviceExtension
->SrbInfo
);
1671 /* Unmap mapped addresses */
1672 while (DeviceExtension
->MappedAddressList
!= NULL
)
1674 MmUnmapIoSpace(DeviceExtension
->MappedAddressList
->MappedAddress
,
1675 DeviceExtension
->MappedAddressList
->NumberOfBytes
);
1677 Ptr
= DeviceExtension
->MappedAddressList
;
1678 DeviceExtension
->MappedAddressList
= DeviceExtension
->MappedAddressList
->NextMappedAddress
;
1683 /* Finally delete the device object */
1684 DPRINT("Deleting device %p\n", DeviceExtension
->DeviceObject
);
1685 IoDeleteDevice(DeviceExtension
->DeviceObject
);
1694 ScsiPortIoMapTransfer(IN PVOID HwDeviceExtension
,
1695 IN PSCSI_REQUEST_BLOCK Srb
,
1696 IN PVOID LogicalAddress
,
1699 DPRINT1("ScsiPortIoMapTransfer()\n");
1708 ScsiPortLogError(IN PVOID HwDeviceExtension
,
1709 IN PSCSI_REQUEST_BLOCK Srb OPTIONAL
,
1716 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
1718 DPRINT1("ScsiPortLogError() called\n");
1720 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
1721 SCSI_PORT_DEVICE_EXTENSION
,
1722 MiniPortDeviceExtension
);
1725 DPRINT("ScsiPortLogError() done\n");
1733 ScsiPortMoveMemory(OUT PVOID Destination
,
1737 RtlMoveMemory(Destination
,
1747 ScsiPortNotification(IN SCSI_NOTIFICATION_TYPE NotificationType
,
1748 IN PVOID HwDeviceExtension
,
1751 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
1754 DPRINT("ScsiPortNotification() called\n");
1756 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
1757 SCSI_PORT_DEVICE_EXTENSION
,
1758 MiniPortDeviceExtension
);
1760 DPRINT("DeviceExtension %p\n", DeviceExtension
);
1762 va_start(ap
, HwDeviceExtension
);
1764 switch (NotificationType
)
1766 case RequestComplete
:
1768 PSCSI_REQUEST_BLOCK Srb
;
1769 PSCSI_REQUEST_BLOCK_INFO SrbData
;
1771 Srb
= (PSCSI_REQUEST_BLOCK
) va_arg (ap
, PSCSI_REQUEST_BLOCK
);
1773 DPRINT("Notify: RequestComplete (Srb %p)\n", Srb
);
1775 /* Make sure Srb is allright */
1776 ASSERT(Srb
->SrbStatus
!= SRB_STATUS_PENDING
);
1777 ASSERT(Srb
->Function
!= SRB_FUNCTION_EXECUTE_SCSI
|| Srb
->SrbStatus
!= SRB_STATUS_SUCCESS
|| Srb
->ScsiStatus
== SCSISTAT_GOOD
);
1779 if (!(Srb
->SrbFlags
& SRB_FLAGS_IS_ACTIVE
))
1781 /* It's been already completed */
1786 /* It's not active anymore */
1787 Srb
->SrbFlags
&= ~SRB_FLAGS_IS_ACTIVE
;
1789 if (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
)
1791 /* TODO: Treat it specially */
1796 /* Get the SRB data */
1797 SrbData
= SpiGetSrbData(DeviceExtension
,
1803 /* Make sure there are no CompletedRequests and there is a Srb */
1804 ASSERT(SrbData
->CompletedRequests
== NULL
&& SrbData
->Srb
!= NULL
);
1806 /* If it's a read/write request, make sure it has data inside it */
1807 if ((Srb
->SrbStatus
== SRB_STATUS_SUCCESS
) &&
1808 ((Srb
->Cdb
[0] == SCSIOP_READ
) || (Srb
->Cdb
[0] == SCSIOP_WRITE
)))
1810 ASSERT(Srb
->DataTransferLength
);
1813 SrbData
->CompletedRequests
= DeviceExtension
->InterruptData
.CompletedRequests
;
1814 DeviceExtension
->InterruptData
.CompletedRequests
= SrbData
;
1820 DPRINT("Notify: NextRequest\n");
1821 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_NEXT_REQUEST_READY
;
1829 PSCSI_PORT_LUN_EXTENSION LunExtension
;
1831 PathId
= (UCHAR
) va_arg (ap
, int);
1832 TargetId
= (UCHAR
) va_arg (ap
, int);
1833 Lun
= (UCHAR
) va_arg (ap
, int);
1835 DPRINT("Notify: NextLuRequest(PathId %u TargetId %u Lun %u)\n",
1836 PathId
, TargetId
, Lun
);
1838 /* Mark it in the flags field */
1839 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_NEXT_REQUEST_READY
;
1841 /* Get the LUN extension */
1842 LunExtension
= SpiGetLunExtension(DeviceExtension
,
1847 /* This request should not be processed if */
1848 if ((LunExtension
&& LunExtension
->ReadyLun
) ||
1849 (LunExtension
&& LunExtension
->SrbInfo
.Srb
))
1851 /* Nothing to do here */
1855 /* Add this LUN to the list */
1856 LunExtension
->ReadyLun
= DeviceExtension
->InterruptData
.ReadyLun
;
1857 DeviceExtension
->InterruptData
.ReadyLun
= LunExtension
;
1862 DPRINT("Notify: ResetDetected\n");
1863 /* Add RESET flags */
1864 DeviceExtension
->InterruptData
.Flags
|=
1865 SCSI_PORT_RESET
| SCSI_PORT_RESET_REPORTED
;
1869 DPRINT1 ("Unsupported notification %lu\n", NotificationType
);
1875 /* Request a DPC after we're done with the interrupt */
1876 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_NOTIFICATION_NEEDED
;
1884 ScsiPortSetBusDataByOffset(IN PVOID DeviceExtension
,
1885 IN ULONG BusDataType
,
1886 IN ULONG SystemIoBusNumber
,
1887 IN ULONG SlotNumber
,
1892 DPRINT("ScsiPortSetBusDataByOffset()\n");
1893 return(HalSetBusDataByOffset(BusDataType
,
1906 ScsiPortValidateRange(IN PVOID HwDeviceExtension
,
1907 IN INTERFACE_TYPE BusType
,
1908 IN ULONG SystemIoBusNumber
,
1909 IN SCSI_PHYSICAL_ADDRESS IoAddress
,
1910 IN ULONG NumberOfBytes
,
1911 IN BOOLEAN InIoSpace
)
1913 DPRINT("ScsiPortValidateRange()\n");
1918 /* INTERNAL FUNCTIONS ********************************************************/
1921 SpiResourceToConfig(IN PHW_INITIALIZATION_DATA HwInitializationData
,
1922 IN PCM_FULL_RESOURCE_DESCRIPTOR ResourceDescriptor
,
1923 IN PPORT_CONFIGURATION_INFORMATION PortConfig
)
1925 PACCESS_RANGE AccessRange
;
1926 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialData
;
1932 /* Loop through all entries */
1933 for (Index
= 0; Index
< ResourceDescriptor
->PartialResourceList
.Count
; Index
++)
1935 PartialData
= &ResourceDescriptor
->PartialResourceList
.PartialDescriptors
[Index
];
1937 switch (PartialData
->Type
)
1939 case CmResourceTypePort
:
1940 /* Copy access ranges */
1941 if (RangeNumber
< HwInitializationData
->NumberOfAccessRanges
)
1943 AccessRange
= &((*(PortConfig
->AccessRanges
))[RangeNumber
]);
1945 AccessRange
->RangeStart
= PartialData
->u
.Port
.Start
;
1946 AccessRange
->RangeLength
= PartialData
->u
.Port
.Length
;
1948 AccessRange
->RangeInMemory
= FALSE
;
1953 case CmResourceTypeMemory
:
1954 /* Copy access ranges */
1955 if (RangeNumber
< HwInitializationData
->NumberOfAccessRanges
)
1957 AccessRange
= &((*(PortConfig
->AccessRanges
))[RangeNumber
]);
1959 AccessRange
->RangeStart
= PartialData
->u
.Memory
.Start
;
1960 AccessRange
->RangeLength
= PartialData
->u
.Memory
.Length
;
1962 AccessRange
->RangeInMemory
= TRUE
;
1967 case CmResourceTypeInterrupt
:
1968 /* Copy interrupt data */
1969 PortConfig
->BusInterruptLevel
= PartialData
->u
.Interrupt
.Level
;
1970 PortConfig
->BusInterruptVector
= PartialData
->u
.Interrupt
.Vector
;
1972 /* Set interrupt mode accordingly to the resource */
1973 if (PartialData
->Flags
== CM_RESOURCE_INTERRUPT_LATCHED
)
1975 PortConfig
->InterruptMode
= Latched
;
1977 else if (PartialData
->Flags
== CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE
)
1979 PortConfig
->InterruptMode
= LevelSensitive
;
1983 case CmResourceTypeDma
:
1984 PortConfig
->DmaChannel
= PartialData
->u
.Dma
.Channel
;
1985 PortConfig
->DmaPort
= PartialData
->u
.Dma
.Port
;
1991 static PCM_RESOURCE_LIST
1992 SpiConfigToResource(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
1993 PPORT_CONFIGURATION_INFORMATION PortConfig
)
1995 PCONFIGURATION_INFORMATION ConfigInfo
;
1996 PCM_RESOURCE_LIST ResourceList
;
1997 PCM_PARTIAL_RESOURCE_DESCRIPTOR ResourceDescriptor
;
1998 PACCESS_RANGE AccessRange
;
2000 ULONG ListLength
= 0, i
, FullSize
;
2003 /* Get current Atdisk usage from the system */
2004 ConfigInfo
= IoGetConfigurationInformation();
2006 if (PortConfig
->AtdiskPrimaryClaimed
)
2007 ConfigInfo
->AtDiskPrimaryAddressClaimed
= TRUE
;
2009 if (PortConfig
->AtdiskSecondaryClaimed
)
2010 ConfigInfo
->AtDiskSecondaryAddressClaimed
= TRUE
;
2012 /* Do we use DMA? */
2013 if (PortConfig
->DmaChannel
!= SP_UNINITIALIZED_VALUE
||
2014 PortConfig
->DmaPort
!= SP_UNINITIALIZED_VALUE
)
2024 /* How many interrupts to we have? */
2025 if (DeviceExtension
->HwInterrupt
== NULL
||
2026 (PortConfig
->BusInterruptLevel
== 0 &&
2027 PortConfig
->BusInterruptVector
== 0))
2037 if (DeviceExtension
->HwInterrupt
!= NULL
&&
2038 (PortConfig
->BusInterruptLevel2
!= 0 ||
2039 PortConfig
->BusInterruptVector2
!= 0))
2045 /* How many access ranges do we use? */
2046 AccessRange
= &((*(PortConfig
->AccessRanges
))[0]);
2047 for (i
= 0; i
< PortConfig
->NumberOfAccessRanges
; i
++)
2049 if (AccessRange
->RangeLength
!= 0)
2055 /* Allocate the resource list, since we know its size now */
2056 FullSize
= sizeof(CM_RESOURCE_LIST
) + (ListLength
- 1) *
2057 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
2059 ResourceList
= (PCM_RESOURCE_LIST
)ExAllocatePoolWithTag(PagedPool
, FullSize
, TAG_SCSIPORT
);
2065 RtlZeroMemory(ResourceList
, FullSize
);
2068 ResourceList
->Count
= 1;
2069 ResourceList
->List
[0].InterfaceType
= PortConfig
->AdapterInterfaceType
;
2070 ResourceList
->List
[0].BusNumber
= PortConfig
->SystemIoBusNumber
;
2071 ResourceList
->List
[0].PartialResourceList
.Count
= ListLength
;
2072 ResourceDescriptor
= ResourceList
->List
[0].PartialResourceList
.PartialDescriptors
;
2074 /* Copy access ranges array over */
2075 for (i
= 0; i
< PortConfig
->NumberOfAccessRanges
; i
++)
2077 AccessRange
= &((*(PortConfig
->AccessRanges
))[i
]);
2079 /* If the range is empty - skip it */
2080 if (AccessRange
->RangeLength
== 0)
2083 if (AccessRange
->RangeInMemory
)
2085 ResourceDescriptor
->Type
= CmResourceTypeMemory
;
2086 ResourceDescriptor
->Flags
= CM_RESOURCE_MEMORY_READ_WRITE
;
2090 ResourceDescriptor
->Type
= CmResourceTypePort
;
2091 ResourceDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
2094 ResourceDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
2096 ResourceDescriptor
->u
.Memory
.Start
= AccessRange
->RangeStart
;
2097 ResourceDescriptor
->u
.Memory
.Length
= AccessRange
->RangeLength
;
2099 ResourceDescriptor
++;
2102 /* If we use interrupt(s), copy them */
2105 ResourceDescriptor
->Type
= CmResourceTypeInterrupt
;
2107 if (PortConfig
->AdapterInterfaceType
== MicroChannel
||
2108 PortConfig
->InterruptMode
== LevelSensitive
)
2110 ResourceDescriptor
->ShareDisposition
= CmResourceShareShared
;
2111 ResourceDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE
;
2115 ResourceDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
2116 ResourceDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
2119 ResourceDescriptor
->u
.Interrupt
.Level
= PortConfig
->BusInterruptLevel
;
2120 ResourceDescriptor
->u
.Interrupt
.Vector
= PortConfig
->BusInterruptVector
;
2121 ResourceDescriptor
->u
.Interrupt
.Affinity
= 0;
2123 ResourceDescriptor
++;
2127 /* Copy 2nd interrupt
2128 FIXME: Stupid code duplication, remove */
2131 ResourceDescriptor
->Type
= CmResourceTypeInterrupt
;
2133 if (PortConfig
->AdapterInterfaceType
== MicroChannel
||
2134 PortConfig
->InterruptMode
== LevelSensitive
)
2136 ResourceDescriptor
->ShareDisposition
= CmResourceShareShared
;
2137 ResourceDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE
;
2141 ResourceDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
2142 ResourceDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
2145 ResourceDescriptor
->u
.Interrupt
.Level
= PortConfig
->BusInterruptLevel
;
2146 ResourceDescriptor
->u
.Interrupt
.Vector
= PortConfig
->BusInterruptVector
;
2147 ResourceDescriptor
->u
.Interrupt
.Affinity
= 0;
2149 ResourceDescriptor
++;
2155 ResourceDescriptor
->Type
= CmResourceTypeDma
;
2156 ResourceDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
2157 ResourceDescriptor
->u
.Dma
.Channel
= PortConfig
->DmaChannel
;
2158 ResourceDescriptor
->u
.Dma
.Port
= PortConfig
->DmaPort
;
2159 ResourceDescriptor
->Flags
= 0;
2161 if (PortConfig
->DmaChannel
== SP_UNINITIALIZED_VALUE
)
2162 ResourceDescriptor
->u
.Dma
.Channel
= 0;
2164 if (PortConfig
->DmaPort
== SP_UNINITIALIZED_VALUE
)
2165 ResourceDescriptor
->u
.Dma
.Port
= 0;
2168 return ResourceList
;
2173 SpiGetPciConfigData(IN PDRIVER_OBJECT DriverObject
,
2174 IN PDEVICE_OBJECT DeviceObject
,
2175 IN
struct _HW_INITIALIZATION_DATA
*HwInitializationData
,
2176 IN OUT PPORT_CONFIGURATION_INFORMATION PortConfig
,
2177 IN PUNICODE_STRING RegistryPath
,
2179 IN OUT PPCI_SLOT_NUMBER NextSlotNumber
)
2181 PCI_COMMON_CONFIG PciConfig
;
2182 PCI_SLOT_NUMBER SlotNumber
;
2185 ULONG FunctionNumber
;
2186 CHAR VendorIdString
[8];
2187 CHAR DeviceIdString
[8];
2188 UNICODE_STRING UnicodeStr
;
2189 PCM_RESOURCE_LIST ResourceList
;
2192 DPRINT ("SpiGetPciConfiguration() called\n");
2194 RtlZeroMemory(&ResourceList
, sizeof(PCM_RESOURCE_LIST
));
2195 SlotNumber
.u
.AsULONG
= 0;
2197 /* Loop through all devices */
2198 for (DeviceNumber
= NextSlotNumber
->u
.bits
.DeviceNumber
; DeviceNumber
< PCI_MAX_DEVICES
; DeviceNumber
++)
2200 SlotNumber
.u
.bits
.DeviceNumber
= DeviceNumber
;
2202 /* Loop through all functions */
2203 for (FunctionNumber
= NextSlotNumber
->u
.bits
.FunctionNumber
; FunctionNumber
< PCI_MAX_FUNCTION
; FunctionNumber
++)
2205 SlotNumber
.u
.bits
.FunctionNumber
= FunctionNumber
;
2207 /* Get PCI config bytes */
2208 DataSize
= HalGetBusData(PCIConfiguration
,
2210 SlotNumber
.u
.AsULONG
,
2214 /* If result of HalGetBusData is 0, then the bus is wrong */
2218 /* If result is PCI_INVALID_VENDORID, then this device has no more
2220 if (PciConfig
.VendorID
== PCI_INVALID_VENDORID
)
2223 sprintf (VendorIdString
, "%04hx", PciConfig
.VendorID
);
2224 sprintf (DeviceIdString
, "%04hx", PciConfig
.DeviceID
);
2226 if (_strnicmp(VendorIdString
, HwInitializationData
->VendorId
, HwInitializationData
->VendorIdLength
) ||
2227 _strnicmp(DeviceIdString
, HwInitializationData
->DeviceId
, HwInitializationData
->DeviceIdLength
))
2229 /* It is not our device */
2233 DPRINT("Found device 0x%04hx 0x%04hx at %1lu %2lu %1lu\n",
2237 SlotNumber
.u
.bits
.DeviceNumber
,
2238 SlotNumber
.u
.bits
.FunctionNumber
);
2241 RtlInitUnicodeString(&UnicodeStr
, L
"ScsiAdapter");
2242 Status
= HalAssignSlotResources(RegistryPath
,
2248 SlotNumber
.u
.AsULONG
,
2251 if (!NT_SUCCESS(Status
))
2254 /* Create configuration information */
2255 SpiResourceToConfig(HwInitializationData
,
2259 /* Free the resource list */
2260 ExFreePool(ResourceList
);
2262 /* Set dev & fn numbers */
2263 NextSlotNumber
->u
.bits
.DeviceNumber
= DeviceNumber
;
2264 NextSlotNumber
->u
.bits
.FunctionNumber
= FunctionNumber
+ 1;
2266 /* Save the slot number */
2267 PortConfig
->SlotNumber
= SlotNumber
.u
.AsULONG
;
2271 NextSlotNumber
->u
.bits
.FunctionNumber
= 0;
2274 NextSlotNumber
->u
.bits
.DeviceNumber
= 0;
2275 DPRINT ("No device found\n");
2282 /**********************************************************************
2284 * ScsiPortCreateClose
2287 * Answer requests for Create/Close calls: a null operation.
2294 * Pointer to a device object.
2297 * Pointer to an IRP.
2303 static NTSTATUS STDCALL
2304 ScsiPortCreateClose(IN PDEVICE_OBJECT DeviceObject
,
2307 DPRINT("ScsiPortCreateClose()\n");
2309 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
2310 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2312 return STATUS_SUCCESS
;
2316 SpiHandleAttachRelease(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
2319 PSCSI_LUN_INFO LunInfo
;
2320 PIO_STACK_LOCATION IrpStack
;
2321 PDEVICE_OBJECT DeviceObject
;
2322 PSCSI_REQUEST_BLOCK Srb
;
2325 /* Get pointer to the SRB */
2326 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
2327 Srb
= (PSCSI_REQUEST_BLOCK
)IrpStack
->Parameters
.Others
.Argument1
;
2329 /* Check if PathId matches number of buses */
2330 if (DeviceExtension
->BusesConfig
== NULL
||
2331 DeviceExtension
->BusesConfig
->NumberOfBuses
<= Srb
->PathId
)
2333 Srb
->SrbStatus
= SRB_STATUS_NO_DEVICE
;
2334 return STATUS_DEVICE_DOES_NOT_EXIST
;
2337 /* Get pointer to LunInfo */
2338 LunInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Srb
->PathId
]->LunInfo
;
2340 /* Find matching LunInfo */
2343 if (LunInfo
->PathId
== Srb
->PathId
&&
2344 LunInfo
->TargetId
== Srb
->TargetId
&&
2345 LunInfo
->Lun
== Srb
->Lun
)
2350 LunInfo
= LunInfo
->Next
;
2353 /* If we couldn't find it - exit */
2354 if (LunInfo
== NULL
)
2355 return STATUS_DEVICE_DOES_NOT_EXIST
;
2359 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
2361 /* Release, if asked */
2362 if (Srb
->Function
== SRB_FUNCTION_RELEASE_DEVICE
)
2364 LunInfo
->DeviceClaimed
= FALSE
;
2365 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2366 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2368 return STATUS_SUCCESS
;
2371 /* Attach, if not already claimed */
2372 if (LunInfo
->DeviceClaimed
)
2374 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2375 Srb
->SrbStatus
= SRB_STATUS_BUSY
;
2377 return STATUS_DEVICE_BUSY
;
2380 /* Save the device object */
2381 DeviceObject
= LunInfo
->DeviceObject
;
2383 if (Srb
->Function
== SRB_FUNCTION_CLAIM_DEVICE
)
2384 LunInfo
->DeviceClaimed
= TRUE
;
2386 if (Srb
->Function
== SRB_FUNCTION_ATTACH_DEVICE
)
2387 LunInfo
->DeviceObject
= Srb
->DataBuffer
;
2389 Srb
->DataBuffer
= DeviceObject
;
2391 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2392 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2394 return STATUS_SUCCESS
;
2398 /**********************************************************************
2400 * ScsiPortDispatchScsi
2403 * Answer requests for SCSI calls
2409 * Standard dispatch arguments
2415 static NTSTATUS STDCALL
2416 ScsiPortDispatchScsi(IN PDEVICE_OBJECT DeviceObject
,
2419 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
2420 PSCSI_PORT_LUN_EXTENSION LunExtension
;
2421 PIO_STACK_LOCATION Stack
;
2422 PSCSI_REQUEST_BLOCK Srb
;
2424 NTSTATUS Status
= STATUS_SUCCESS
;
2425 PIRP NextIrp
, IrpList
;
2426 PKDEVICE_QUEUE_ENTRY Entry
;
2428 DPRINT("ScsiPortDispatchScsi(DeviceObject %p Irp %p)\n",
2431 DeviceExtension
= DeviceObject
->DeviceExtension
;
2432 Stack
= IoGetCurrentIrpStackLocation(Irp
);
2434 Srb
= Stack
->Parameters
.Scsi
.Srb
;
2437 DPRINT1("ScsiPortDispatchScsi() called with Srb = NULL!\n");
2438 Status
= STATUS_UNSUCCESSFUL
;
2440 Irp
->IoStatus
.Status
= Status
;
2441 Irp
->IoStatus
.Information
= 0;
2443 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2448 DPRINT("Srb: %p\n", Srb
);
2449 DPRINT("Srb->Function: %lu\n", Srb
->Function
);
2450 DPRINT("PathId: %lu TargetId: %lu Lun: %lu\n", Srb
->PathId
, Srb
->TargetId
, Srb
->Lun
);
2452 LunExtension
= SpiGetLunExtension(DeviceExtension
,
2456 if (LunExtension
== NULL
)
2458 DPRINT("ScsiPortDispatchScsi() called with an invalid LUN\n");
2459 Status
= STATUS_NO_SUCH_DEVICE
;
2461 Srb
->SrbStatus
= SRB_STATUS_NO_DEVICE
;
2462 Irp
->IoStatus
.Status
= Status
;
2463 Irp
->IoStatus
.Information
= 0;
2465 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2470 switch (Srb
->Function
)
2472 case SRB_FUNCTION_SHUTDOWN
:
2473 case SRB_FUNCTION_FLUSH
:
2474 DPRINT (" SRB_FUNCTION_SHUTDOWN or FLUSH\n");
2475 if (DeviceExtension
->CachesData
== FALSE
)
2477 /* All success here */
2478 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2479 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
2480 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2481 return STATUS_SUCCESS
;
2483 /* Fall through to a usual execute operation */
2485 case SRB_FUNCTION_EXECUTE_SCSI
:
2486 case SRB_FUNCTION_IO_CONTROL
:
2487 /* Mark IRP as pending in all cases */
2488 IoMarkIrpPending(Irp
);
2490 if (Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
)
2492 /* Start IO directly */
2493 IoStartPacket(DeviceObject
, Irp
, NULL
, NULL
);
2499 /* We need to be at DISPATCH_LEVEL */
2500 KeRaiseIrql (DISPATCH_LEVEL
, &oldIrql
);
2502 /* Insert IRP into the queue */
2503 if (!KeInsertByKeyDeviceQueue(&LunExtension
->DeviceQueue
,
2504 &Irp
->Tail
.Overlay
.DeviceQueueEntry
,
2507 /* It means the queue is empty, and we just start this request */
2508 IoStartPacket(DeviceObject
, Irp
, NULL
, NULL
);
2511 /* Back to the old IRQL */
2512 KeLowerIrql (oldIrql
);
2514 return STATUS_PENDING
;
2516 case SRB_FUNCTION_CLAIM_DEVICE
:
2517 case SRB_FUNCTION_ATTACH_DEVICE
:
2518 DPRINT (" SRB_FUNCTION_CLAIM_DEVICE or ATTACH\n");
2520 /* Reference device object and keep the device object */
2521 Status
= SpiHandleAttachRelease(DeviceExtension
, Irp
);
2524 case SRB_FUNCTION_RELEASE_DEVICE
:
2525 DPRINT (" SRB_FUNCTION_RELEASE_DEVICE\n");
2527 /* Dereference device object and clear the device object */
2528 Status
= SpiHandleAttachRelease(DeviceExtension
, Irp
);
2531 case SRB_FUNCTION_RELEASE_QUEUE
:
2532 DPRINT(" SRB_FUNCTION_RELEASE_QUEUE\n");
2534 /* Guard with the spinlock */
2535 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
2537 if (!(LunExtension
->Flags
& LUNEX_FROZEN_QUEUE
))
2539 DPRINT("Queue is not frozen really\n");
2541 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2542 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2543 Status
= STATUS_SUCCESS
;
2548 /* Unfreeze the queue */
2549 LunExtension
->Flags
&= ~LUNEX_FROZEN_QUEUE
;
2551 if (LunExtension
->SrbInfo
.Srb
== NULL
)
2553 /* Get next logical unit request */
2554 SpiGetNextRequestFromLun(DeviceExtension
, LunExtension
);
2556 /* SpiGetNextRequestFromLun() releases the spinlock */
2561 DPRINT("The queue has active request\n");
2562 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2566 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2567 Status
= STATUS_SUCCESS
;
2570 case SRB_FUNCTION_FLUSH_QUEUE
:
2571 DPRINT(" SRB_FUNCTION_FLUSH_QUEUE\n");
2573 /* Guard with the spinlock */
2574 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
2576 if (!(LunExtension
->Flags
& LUNEX_FROZEN_QUEUE
))
2578 DPRINT("Queue is not frozen really\n");
2580 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2581 Status
= STATUS_INVALID_DEVICE_REQUEST
;
2585 /* Make sure there is no active request */
2586 ASSERT(LunExtension
->SrbInfo
.Srb
== NULL
);
2588 /* Compile a list from the device queue */
2590 while ((Entry
= KeRemoveDeviceQueue(&LunExtension
->DeviceQueue
)) != NULL
)
2592 NextIrp
= CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.DeviceQueueEntry
);
2595 Stack
= IoGetCurrentIrpStackLocation(NextIrp
);
2596 Srb
= Stack
->Parameters
.Scsi
.Srb
;
2599 Srb
->SrbStatus
= SRB_STATUS_REQUEST_FLUSHED
;
2600 NextIrp
->IoStatus
.Status
= STATUS_UNSUCCESSFUL
;
2602 /* Add then to the list */
2603 NextIrp
->Tail
.Overlay
.ListEntry
.Flink
= (PLIST_ENTRY
)IrpList
;
2607 /* Unfreeze the queue */
2608 LunExtension
->Flags
&= ~LUNEX_FROZEN_QUEUE
;
2610 /* Release the spinlock */
2611 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2613 /* Complete those requests */
2617 IrpList
= (PIRP
)NextIrp
->Tail
.Overlay
.ListEntry
.Flink
;
2619 IoCompleteRequest(NextIrp
, 0);
2622 Status
= STATUS_SUCCESS
;
2626 DPRINT1("SRB function not implemented (Function %lu)\n", Srb
->Function
);
2627 Status
= STATUS_NOT_IMPLEMENTED
;
2631 Irp
->IoStatus
.Status
= Status
;
2633 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2639 /**********************************************************************
2641 * ScsiPortDeviceControl
2644 * Answer requests for device control calls
2650 * Standard dispatch arguments
2656 static NTSTATUS STDCALL
2657 ScsiPortDeviceControl(IN PDEVICE_OBJECT DeviceObject
,
2660 PIO_STACK_LOCATION Stack
;
2661 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
2662 NTSTATUS Status
= STATUS_SUCCESS
;;
2664 DPRINT("ScsiPortDeviceControl()\n");
2666 Irp
->IoStatus
.Information
= 0;
2668 Stack
= IoGetCurrentIrpStackLocation(Irp
);
2669 DeviceExtension
= DeviceObject
->DeviceExtension
;
2671 switch (Stack
->Parameters
.DeviceIoControl
.IoControlCode
)
2673 case IOCTL_SCSI_GET_DUMP_POINTERS
:
2675 PDUMP_POINTERS DumpPointers
;
2676 DPRINT(" IOCTL_SCSI_GET_DUMP_POINTERS\n");
2677 DumpPointers
= (PDUMP_POINTERS
)Irp
->AssociatedIrp
.SystemBuffer
;
2678 DumpPointers
->DeviceObject
= DeviceObject
;
2680 Irp
->IoStatus
.Information
= sizeof(DUMP_POINTERS
);
2684 case IOCTL_SCSI_GET_CAPABILITIES
:
2685 DPRINT(" IOCTL_SCSI_GET_CAPABILITIES\n");
2686 if (Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
== sizeof(PVOID
))
2688 *((PVOID
*)Irp
->AssociatedIrp
.SystemBuffer
) = &DeviceExtension
->PortCapabilities
;
2690 Irp
->IoStatus
.Information
= sizeof(PVOID
);
2691 Status
= STATUS_SUCCESS
;
2695 if (Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(IO_SCSI_CAPABILITIES
))
2697 Status
= STATUS_BUFFER_TOO_SMALL
;
2701 RtlCopyMemory(Irp
->AssociatedIrp
.SystemBuffer
,
2702 &DeviceExtension
->PortCapabilities
,
2703 sizeof(IO_SCSI_CAPABILITIES
));
2705 Irp
->IoStatus
.Information
= sizeof(IO_SCSI_CAPABILITIES
);
2706 Status
= STATUS_SUCCESS
;
2709 case IOCTL_SCSI_GET_INQUIRY_DATA
:
2710 DPRINT(" IOCTL_SCSI_GET_INQUIRY_DATA\n");
2712 /* Copy inquiry data to the port device extension */
2713 Status
= SpiGetInquiryData(DeviceExtension
, Irp
);
2717 DPRINT1(" unknown ioctl code: 0x%lX\n",
2718 Stack
->Parameters
.DeviceIoControl
.IoControlCode
);
2722 /* Complete the request with the given status */
2723 Irp
->IoStatus
.Status
= Status
;
2724 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2731 ScsiPortStartIo(IN PDEVICE_OBJECT DeviceObject
,
2734 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
2735 PSCSI_PORT_LUN_EXTENSION LunExtension
;
2736 PIO_STACK_LOCATION IrpStack
;
2737 PSCSI_REQUEST_BLOCK Srb
;
2738 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
2742 DPRINT("ScsiPortStartIo() called!\n");
2744 DeviceExtension
= DeviceObject
->DeviceExtension
;
2745 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
2747 DPRINT("DeviceExtension %p\n", DeviceExtension
);
2749 Srb
= IrpStack
->Parameters
.Scsi
.Srb
;
2751 /* Apply "default" flags */
2752 Srb
->SrbFlags
|= DeviceExtension
->SrbFlags
;
2754 /* Get LUN extension */
2755 LunExtension
= SpiGetLunExtension(DeviceExtension
,
2760 if (DeviceExtension
->NeedSrbDataAlloc
||
2761 DeviceExtension
->NeedSrbExtensionAlloc
)
2764 SrbInfo
= SpiAllocateSrbStructures(DeviceExtension
,
2768 /* Couldn't alloc one or both data structures, return */
2769 if (SrbInfo
== NULL
)
2771 /* We have to call IoStartNextPacket, because this request
2773 if (LunExtension
->Flags
& LUNEX_REQUEST_PENDING
)
2774 IoStartNextPacket(DeviceObject
, FALSE
);
2781 /* No allocations are needed */
2782 SrbInfo
= &LunExtension
->SrbInfo
;
2783 Srb
->SrbExtension
= NULL
;
2784 Srb
->QueueTag
= SP_UNTAGGED
;
2787 /* Increase sequence number of SRB */
2788 if (!SrbInfo
->SequenceNumber
)
2790 /* Increase global sequence number */
2791 DeviceExtension
->SequenceNumber
++;
2794 SrbInfo
->SequenceNumber
= DeviceExtension
->SequenceNumber
;
2797 /* Check some special SRBs */
2798 if (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
)
2800 /* Some special handling */
2801 DPRINT1("Abort command! Unimplemented now\n");
2808 if (Srb
->SrbFlags
& SRB_FLAGS_UNSPECIFIED_DIRECTION
)
2810 // Store the MDL virtual address in SrbInfo structure
2811 SrbInfo
->DataOffset
= MmGetMdlVirtualAddress(Irp
->MdlAddress
);
2813 if (DeviceExtension
->MapBuffers
&& Irp
->MdlAddress
)
2815 /* Calculate offset within DataBuffer */
2816 SrbInfo
->DataOffset
= MmGetSystemAddressForMdl(Irp
->MdlAddress
);
2817 Srb
->DataBuffer
= SrbInfo
->DataOffset
+
2818 (ULONG
)((PUCHAR
)Srb
->DataBuffer
-
2819 (PUCHAR
)MmGetMdlVirtualAddress(Irp
->MdlAddress
));
2822 if (DeviceExtension
->AdapterObject
)
2825 KeFlushIoBuffers(Irp
->MdlAddress
,
2826 Srb
->SrbFlags
& SRB_FLAGS_DATA_IN
? TRUE
: FALSE
,
2830 if (DeviceExtension
->MapRegisters
)
2832 /* Calculate number of needed map registers */
2833 SrbInfo
->NumberOfMapRegisters
= ADDRESS_AND_SIZE_TO_SPAN_PAGES(
2835 Srb
->DataTransferLength
);
2837 /* Allocate adapter channel */
2838 Status
= IoAllocateAdapterChannel(DeviceExtension
->AdapterObject
,
2839 DeviceExtension
->DeviceObject
,
2840 SrbInfo
->NumberOfMapRegisters
,
2844 if (!NT_SUCCESS(Status
))
2846 DPRINT1("IoAllocateAdapterChannel() failed!\n");
2848 Srb
->SrbStatus
= SRB_STATUS_INVALID_REQUEST
;
2849 ScsiPortNotification(RequestComplete
,
2850 DeviceExtension
+ 1,
2853 ScsiPortNotification(NextRequest
,
2854 DeviceExtension
+ 1);
2856 /* Request DPC for that work */
2857 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
2860 /* Control goes to SpiAdapterControl */
2865 /* Increase active request counter */
2866 CounterResult
= InterlockedIncrement(&DeviceExtension
->ActiveRequestCounter
);
2868 if (CounterResult
== 0 &&
2869 DeviceExtension
->AdapterObject
!= NULL
&&
2870 !DeviceExtension
->MapRegisters
)
2873 IoAllocateAdapterChannel(
2874 DeviceExtension
->AdapterObject
,
2876 DeviceExtension
->PortCapabilities
.MaximumPhysicalPages
,
2877 ScsiPortAllocationRoutine
,
2883 /* TODO: DMA is not implemented yet */
2889 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
2891 if (!KeSynchronizeExecution(DeviceExtension
->Interrupt
,
2892 ScsiPortStartPacket
,
2895 DPRINT("Synchronization failed!\n");
2897 Irp
->IoStatus
.Status
= STATUS_UNSUCCESSFUL
;
2898 Irp
->IoStatus
.Information
= 0;
2899 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
2901 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2904 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
2906 DPRINT("ScsiPortStartIo() done\n");
2910 static BOOLEAN STDCALL
2911 ScsiPortStartPacket(IN OUT PVOID Context
)
2913 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
2914 PIO_STACK_LOCATION IrpStack
;
2915 PSCSI_REQUEST_BLOCK Srb
;
2916 PDEVICE_OBJECT DeviceObject
= (PDEVICE_OBJECT
)Context
;
2917 PSCSI_PORT_LUN_EXTENSION LunExtension
;
2918 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
2922 DPRINT("ScsiPortStartPacket() called\n");
2924 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
2926 IrpStack
= IoGetCurrentIrpStackLocation(DeviceObject
->CurrentIrp
);
2927 Srb
= IrpStack
->Parameters
.Scsi
.Srb
;
2929 /* Get LUN extension */
2930 LunExtension
= SpiGetLunExtension(DeviceExtension
,
2935 /* Check if we are in a reset state */
2936 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_RESET
)
2938 /* Mark the we've got requests while being in the reset state */
2939 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_RESET_REQUEST
;
2943 /* Set the time out value */
2944 DeviceExtension
->TimerCount
= Srb
->TimeOutValue
;
2947 DeviceExtension
->Flags
|= SCSI_PORT_DEVICE_BUSY
;
2949 if (LunExtension
->RequestTimeout
!= -1)
2951 /* Timer already active */
2956 /* It hasn't been initialized yet */
2957 LunExtension
->RequestTimeout
= Srb
->TimeOutValue
;
2961 if (Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
)
2963 /* Handle bypass-requests */
2965 /* Is this an abort request? */
2966 if (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
)
2968 /* Get pointer to SRB info structure */
2969 SrbInfo
= SpiGetSrbData(DeviceExtension
,
2975 /* Check if the request is still "active" */
2976 if (SrbInfo
== NULL
||
2977 SrbInfo
->Srb
== NULL
||
2978 !(SrbInfo
->Srb
->SrbFlags
& SRB_FLAGS_IS_ACTIVE
))
2980 /* It's not, mark it as active then */
2981 Srb
->SrbFlags
|= SRB_FLAGS_IS_ACTIVE
;
2984 LunExtension
->RequestTimeout
= -1;
2986 DPRINT("Request has been already completed, but abort request came\n");
2987 Srb
->SrbStatus
= SRB_STATUS_ABORT_FAILED
;
2989 /* Notify about request complete */
2990 ScsiPortNotification(RequestComplete
,
2991 DeviceExtension
->MiniPortDeviceExtension
,
2994 /* and about readiness for the next request */
2995 ScsiPortNotification(NextRequest
,
2996 DeviceExtension
->MiniPortDeviceExtension
);
2998 /* They might ask for some work, so queue the DPC for them */
2999 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
3001 /* We're done in this branch */
3007 /* Add number of queued requests */
3008 LunExtension
->QueueCount
++;
3011 /* Bypass requests don't need request sense */
3012 LunExtension
->Flags
&= ~LUNEX_NEED_REQUEST_SENSE
;
3014 /* Is disconnect disabled for this request? */
3015 if (Srb
->SrbFlags
& SRB_FLAGS_DISABLE_DISCONNECT
)
3017 /* Set the corresponding flag */
3018 DeviceExtension
->Flags
&= ~SCSI_PORT_DISCONNECT_ALLOWED
;
3021 /* Transfer timeout value from Srb to Lun */
3022 LunExtension
->RequestTimeout
= Srb
->TimeOutValue
;
3026 if (Srb
->SrbFlags
& SRB_FLAGS_DISABLE_DISCONNECT
)
3028 /* It's a disconnect, so no more requests can go */
3029 DeviceExtension
->Flags
&= ~SCSI_PORT_DISCONNECT_ALLOWED
;
3032 LunExtension
->Flags
|= SCSI_PORT_LU_ACTIVE
;
3034 /* Increment queue count */
3035 LunExtension
->QueueCount
++;
3037 /* If it's tagged - special thing */
3038 if (Srb
->QueueTag
!= SP_UNTAGGED
)
3040 SrbInfo
= &DeviceExtension
->SrbInfo
[Srb
->QueueTag
- 1];
3042 /* Chek for consistency */
3043 ASSERT(SrbInfo
->Requests
.Blink
== NULL
);
3045 /* Insert it into the list of requests */
3046 InsertTailList(&LunExtension
->SrbInfo
.Requests
, &SrbInfo
->Requests
);
3050 /* Mark this Srb active */
3051 Srb
->SrbFlags
|= SRB_FLAGS_IS_ACTIVE
;
3053 /* Call HwStartIo routine */
3054 Result
= DeviceExtension
->HwStartIo(&DeviceExtension
->MiniPortDeviceExtension
,
3057 /* If notification is needed, then request a DPC */
3058 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
3059 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
3064 IO_ALLOCATION_ACTION
3066 SpiAdapterControl(PDEVICE_OBJECT DeviceObject
,
3068 PVOID MapRegisterBase
,
3071 PSCSI_REQUEST_BLOCK Srb
;
3072 PSCSI_SG_ADDRESS ScatterGatherList
;
3074 PIO_STACK_LOCATION IrpStack
;
3075 ULONG TotalLength
= 0;
3076 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
3077 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
3079 BOOLEAN WriteToDevice
;
3081 /* Get pointers to SrbInfo and DeviceExtension */
3082 SrbInfo
= (PSCSI_REQUEST_BLOCK_INFO
)Context
;
3083 DeviceExtension
= DeviceObject
->DeviceExtension
;
3085 /* Get pointer to SRB */
3086 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
3087 Srb
= (PSCSI_REQUEST_BLOCK
)IrpStack
->Parameters
.Others
.Argument1
;
3089 /* Depending on the map registers number, we allocate
3090 either from NonPagedPool, or from our static list */
3091 if (SrbInfo
->NumberOfMapRegisters
> MAX_SG_LIST
)
3093 SrbInfo
->ScatterGather
= ExAllocatePoolWithTag(
3094 NonPagedPool
, SrbInfo
->NumberOfMapRegisters
* sizeof(SCSI_SG_ADDRESS
), TAG_SCSIPORT
);
3096 if (SrbInfo
->ScatterGather
== NULL
)
3099 Srb
->SrbFlags
|= SRB_FLAGS_SGLIST_FROM_POOL
;
3103 SrbInfo
->ScatterGather
= SrbInfo
->ScatterGatherList
;
3106 /* Use chosen SG list source */
3107 ScatterGatherList
= SrbInfo
->ScatterGather
;
3109 /* Save map registers base */
3110 SrbInfo
->BaseOfMapRegister
= MapRegisterBase
;
3112 /* Determine WriteToDevice flag */
3113 WriteToDevice
= Srb
->SrbFlags
& SRB_FLAGS_DATA_OUT
? TRUE
: FALSE
;
3115 /* Get virtual address of the data buffer */
3116 DataVA
= (PUCHAR
)MmGetMdlVirtualAddress(Irp
->MdlAddress
) +
3117 ((PCHAR
)Srb
->DataBuffer
- SrbInfo
->DataOffset
);
3119 /* Build the actual SG list */
3120 while (TotalLength
< Srb
->DataTransferLength
)
3122 if (!ScatterGatherList
)
3125 ScatterGatherList
->Length
= Srb
->DataTransferLength
- TotalLength
;
3126 ScatterGatherList
->PhysicalAddress
= IoMapTransfer(NULL
,
3129 DataVA
+ TotalLength
,
3130 &ScatterGatherList
->Length
,
3133 TotalLength
+= ScatterGatherList
->Length
;
3134 ScatterGatherList
++;
3137 /* Schedule an active request */
3138 InterlockedIncrement(&DeviceExtension
->ActiveRequestCounter
);
3139 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &CurrentIrql
);
3140 KeSynchronizeExecution(DeviceExtension
->Interrupt
,
3141 ScsiPortStartPacket
,
3143 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, CurrentIrql
);
3145 return DeallocateObjectKeepRegisters
;
3148 static PSCSI_PORT_LUN_EXTENSION
3149 SpiAllocateLunExtension (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
)
3151 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3152 ULONG LunExtensionSize
;
3154 DPRINT("SpiAllocateLunExtension (%p)\n",
3157 /* Round LunExtensionSize first to the sizeof LONGLONG */
3158 LunExtensionSize
= (DeviceExtension
->LunExtensionSize
+
3159 sizeof(LONGLONG
) - 1) & ~(sizeof(LONGLONG
) - 1);
3161 LunExtensionSize
+= sizeof(SCSI_PORT_LUN_EXTENSION
);
3162 DPRINT("LunExtensionSize %lu\n", LunExtensionSize
);
3164 LunExtension
= ExAllocatePoolWithTag(NonPagedPool
, LunExtensionSize
, TAG_SCSIPORT
);
3165 if (LunExtension
== NULL
)
3167 DPRINT1("Out of resources!\n");
3171 /* Zero everything */
3172 RtlZeroMemory(LunExtension
, LunExtensionSize
);
3174 /* Initialize a list of requests */
3175 InitializeListHead(&LunExtension
->SrbInfo
.Requests
);
3177 /* Initialize timeout counter */
3178 LunExtension
->RequestTimeout
= -1;
3180 /* Set maximum queue size */
3181 LunExtension
->MaxQueueCount
= 256;
3183 /* Initialize request queue */
3184 KeInitializeDeviceQueue (&LunExtension
->DeviceQueue
);
3186 return LunExtension
;
3189 static PSCSI_PORT_LUN_EXTENSION
3190 SpiGetLunExtension (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
3195 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3197 DPRINT("SpiGetLunExtension(%p %u %u %u) called\n",
3198 DeviceExtension
, PathId
, TargetId
, Lun
);
3200 /* Get appropriate list */
3201 LunExtension
= DeviceExtension
->LunExtensionList
[(TargetId
+ Lun
) % LUS_NUMBER
];
3203 /* Iterate it until we find what we need */
3204 while (LunExtension
)
3206 if (LunExtension
->TargetId
== TargetId
&&
3207 LunExtension
->Lun
== Lun
&&
3208 LunExtension
->PathId
== PathId
)
3210 /* All matches, return */
3211 return LunExtension
;
3214 /* Advance to the next item */
3215 LunExtension
= LunExtension
->Next
;
3218 /* We did not find anything */
3219 DPRINT("Nothing found\n");
3223 static PSCSI_REQUEST_BLOCK_INFO
3224 SpiAllocateSrbStructures(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
3225 PSCSI_PORT_LUN_EXTENSION LunExtension
,
3226 PSCSI_REQUEST_BLOCK Srb
)
3229 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
3231 /* Spinlock must be held while this function executes */
3232 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
3234 /* Allocate SRB data structure */
3235 if (DeviceExtension
->NeedSrbDataAlloc
)
3237 /* Treat the abort request in a special way */
3238 if (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
)
3240 SrbInfo
= SpiGetSrbData(DeviceExtension
,
3246 else if (Srb
->SrbFlags
&
3247 (SRB_FLAGS_QUEUE_ACTION_ENABLE
| SRB_FLAGS_NO_QUEUE_FREEZE
) &&
3248 !(Srb
->SrbFlags
& SRB_FLAGS_DISABLE_DISCONNECT
)
3251 /* Do not process tagged commands if need request sense is set */
3252 if (LunExtension
->Flags
& LUNEX_NEED_REQUEST_SENSE
)
3254 ASSERT(!(LunExtension
->Flags
& LUNEX_REQUEST_PENDING
));
3256 LunExtension
->PendingRequest
= Srb
->OriginalRequest
;
3257 LunExtension
->Flags
|= LUNEX_REQUEST_PENDING
| SCSI_PORT_LU_ACTIVE
;
3259 /* Relese the spinlock and return */
3260 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3264 ASSERT(LunExtension
->SrbInfo
.Srb
== NULL
);
3265 SrbInfo
= DeviceExtension
->FreeSrbInfo
;
3267 if (SrbInfo
== NULL
)
3269 /* No SRB structures left in the list. We have to leave
3270 and wait while we are called again */
3272 DeviceExtension
->Flags
|= SCSI_PORT_REQUEST_PENDING
;
3273 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3277 DeviceExtension
->FreeSrbInfo
= (PSCSI_REQUEST_BLOCK_INFO
)SrbInfo
->Requests
.Flink
;
3279 /* QueueTag must never be 0, so +1 to it */
3280 Srb
->QueueTag
= (UCHAR
)(SrbInfo
- DeviceExtension
->SrbInfo
) + 1;
3284 /* Usual untagged command */
3286 (!IsListEmpty(&LunExtension
->SrbInfo
.Requests
) ||
3287 LunExtension
->Flags
& LUNEX_NEED_REQUEST_SENSE
) &&
3288 !(Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
)
3291 /* Mark it as pending and leave */
3292 ASSERT(!(LunExtension
->Flags
& LUNEX_REQUEST_PENDING
));
3293 LunExtension
->Flags
|= LUNEX_REQUEST_PENDING
| SCSI_PORT_LU_ACTIVE
;
3294 LunExtension
->PendingRequest
= Srb
->OriginalRequest
;
3296 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3300 Srb
->QueueTag
= SP_UNTAGGED
;
3301 SrbInfo
= &LunExtension
->SrbInfo
;
3306 Srb
->QueueTag
= SP_UNTAGGED
;
3307 SrbInfo
= &LunExtension
->SrbInfo
;
3310 /* Allocate SRB extension structure */
3311 if (DeviceExtension
->NeedSrbExtensionAlloc
)
3313 /* Check the list of free extensions */
3314 SrbExtension
= DeviceExtension
->FreeSrbExtensions
;
3316 /* If no free extensions... */
3317 if (SrbExtension
== NULL
)
3320 if (Srb
->Function
!= SRB_FUNCTION_ABORT_COMMAND
&&
3321 Srb
->QueueTag
!= SP_UNTAGGED
)
3323 SrbInfo
->Requests
.Blink
= NULL
;
3324 SrbInfo
->Requests
.Flink
= (PLIST_ENTRY
)DeviceExtension
->FreeSrbInfo
;
3325 DeviceExtension
->FreeSrbInfo
= SrbInfo
;
3328 /* Return, in order to be called again later */
3329 DeviceExtension
->Flags
|= SCSI_PORT_REQUEST_PENDING
;
3330 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3334 /* Remove that free SRB extension from the list (since
3335 we're going to use it) */
3336 DeviceExtension
->FreeSrbExtensions
= *((PVOID
*)SrbExtension
);
3338 /* Spinlock can be released now */
3339 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3341 Srb
->SrbExtension
= SrbExtension
;
3343 if (Srb
->SenseInfoBuffer
!= NULL
&&
3344 DeviceExtension
->SupportsAutoSense
)
3346 /* Store pointer to the SenseInfo buffer */
3347 SrbInfo
->SaveSenseRequest
= Srb
->SenseInfoBuffer
;
3349 /* Does data fit the buffer? */
3350 if (Srb
->SenseInfoBufferLength
> sizeof(SENSE_DATA
))
3352 /* No, disabling autosense at all */
3353 Srb
->SrbFlags
|= SRB_FLAGS_DISABLE_AUTOSENSE
;
3357 /* Yes, update the buffer pointer */
3358 Srb
->SenseInfoBuffer
= SrbExtension
+ DeviceExtension
->SrbExtensionSize
;
3365 Srb
->SrbExtension
= NULL
;
3366 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3374 SpiSendInquiry (IN PDEVICE_OBJECT DeviceObject
,
3375 IN PSCSI_LUN_INFO LunInfo
)
3377 IO_STATUS_BLOCK IoStatusBlock
;
3378 PIO_STACK_LOCATION IrpStack
;
3383 PINQUIRYDATA InquiryBuffer
;
3384 PSENSE_DATA SenseBuffer
;
3385 BOOLEAN KeepTrying
= TRUE
;
3386 ULONG RetryCount
= 0;
3387 SCSI_REQUEST_BLOCK Srb
;
3389 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3390 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
3392 DPRINT ("SpiSendInquiry() called\n");
3394 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
3396 InquiryBuffer
= ExAllocatePoolWithTag (NonPagedPool
, INQUIRYDATABUFFERSIZE
, TAG_SCSIPORT
);
3397 if (InquiryBuffer
== NULL
)
3398 return STATUS_INSUFFICIENT_RESOURCES
;
3400 SenseBuffer
= ExAllocatePoolWithTag (NonPagedPool
, SENSE_BUFFER_SIZE
, TAG_SCSIPORT
);
3401 if (SenseBuffer
== NULL
)
3403 ExFreePool(InquiryBuffer
);
3404 return STATUS_INSUFFICIENT_RESOURCES
;
3409 /* Initialize event for waiting */
3410 KeInitializeEvent(&Event
,
3415 Irp
= IoBuildDeviceIoControlRequest(IOCTL_SCSI_EXECUTE_IN
,
3420 INQUIRYDATABUFFERSIZE
,
3426 DPRINT("IoBuildDeviceIoControlRequest() failed\n");
3427 return STATUS_INSUFFICIENT_RESOURCES
;
3431 RtlZeroMemory(&Srb
, sizeof(SCSI_REQUEST_BLOCK
));
3433 Srb
.Length
= sizeof(SCSI_REQUEST_BLOCK
);
3434 Srb
.OriginalRequest
= Irp
;
3435 Srb
.PathId
= LunInfo
->PathId
;
3436 Srb
.TargetId
= LunInfo
->TargetId
;
3437 Srb
.Lun
= LunInfo
->Lun
;
3438 Srb
.Function
= SRB_FUNCTION_EXECUTE_SCSI
;
3439 Srb
.SrbFlags
= SRB_FLAGS_DATA_IN
| SRB_FLAGS_DISABLE_SYNCH_TRANSFER
;
3440 Srb
.TimeOutValue
= 4;
3443 Srb
.SenseInfoBuffer
= SenseBuffer
;
3444 Srb
.SenseInfoBufferLength
= SENSE_BUFFER_SIZE
;
3446 Srb
.DataBuffer
= InquiryBuffer
;
3447 Srb
.DataTransferLength
= INQUIRYDATABUFFERSIZE
;
3449 /* Attach Srb to the Irp */
3450 IrpStack
= IoGetNextIrpStackLocation (Irp
);
3451 IrpStack
->Parameters
.Scsi
.Srb
= &Srb
;
3454 Cdb
= (PCDB
)Srb
.Cdb
;
3455 Cdb
->CDB6INQUIRY
.OperationCode
= SCSIOP_INQUIRY
;
3456 Cdb
->CDB6INQUIRY
.LogicalUnitNumber
= LunInfo
->Lun
;
3457 Cdb
->CDB6INQUIRY
.AllocationLength
= INQUIRYDATABUFFERSIZE
;
3459 /* Call the driver */
3460 Status
= IoCallDriver(DeviceObject
, Irp
);
3462 /* Wait for it to complete */
3463 if (Status
== STATUS_PENDING
)
3465 DPRINT("SpiSendInquiry(): Waiting for the driver to process request...\n");
3466 KeWaitForSingleObject(&Event
,
3471 Status
= IoStatusBlock
.Status
;
3474 DPRINT("SpiSendInquiry(): Request processed by driver, status = 0x%08X\n", Status
);
3476 if (SRB_STATUS(Srb
.SrbStatus
) == SRB_STATUS_SUCCESS
)
3478 /* All fine, copy data over */
3479 RtlCopyMemory(LunInfo
->InquiryData
,
3481 INQUIRYDATABUFFERSIZE
);
3483 Status
= STATUS_SUCCESS
;
3488 DPRINT("Inquiry SRB failed with SrbStatus 0x%08X\n", Srb
.SrbStatus
);
3489 /* Check if the queue is frozen */
3490 if (Srb
.SrbStatus
& SRB_STATUS_QUEUE_FROZEN
)
3492 /* Something weird happened, deal with it (unfreeze the queue) */
3495 DPRINT("SpiSendInquiry(): the queue is frozen at TargetId %d\n", Srb
.TargetId
);
3497 LunExtension
= SpiGetLunExtension(DeviceExtension
,
3502 /* Clear frozen flag */
3503 LunExtension
->Flags
&= ~LUNEX_FROZEN_QUEUE
;
3505 /* Acquire the spinlock */
3506 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
3508 /* Process the request */
3509 SpiGetNextRequestFromLun(DeviceObject
->DeviceExtension
, LunExtension
);
3511 /* SpiGetNextRequestFromLun() releases the spinlock,
3512 so we just lower irql back to what it was before */
3516 /* Check if data overrun happened */
3517 if (SRB_STATUS(Srb
.SrbStatus
) == SRB_STATUS_DATA_OVERRUN
)
3519 DPRINT("Data overrun at TargetId %d\n", LunInfo
->TargetId
);
3520 /* Nothing dramatic, just copy data, but limiting the size */
3521 RtlCopyMemory(LunInfo
->InquiryData
,
3523 (Srb
.DataTransferLength
> INQUIRYDATABUFFERSIZE
) ?
3524 INQUIRYDATABUFFERSIZE
: Srb
.DataTransferLength
);
3526 Status
= STATUS_SUCCESS
;
3529 else if ((Srb
.SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
) &&
3530 SenseBuffer
->SenseKey
== SCSI_SENSE_ILLEGAL_REQUEST
)
3532 /* LUN is not valid, but some device responds there.
3533 Mark it as invalid anyway */
3535 Status
= STATUS_INVALID_DEVICE_REQUEST
;
3540 /* Retry a couple of times if no timeout happened */
3541 if ((RetryCount
< 2) &&
3542 (SRB_STATUS(Srb
.SrbStatus
) != SRB_STATUS_NO_DEVICE
) &&
3543 (SRB_STATUS(Srb
.SrbStatus
) != SRB_STATUS_SELECTION_TIMEOUT
))
3550 /* That's all, go to exit */
3553 /* Set status according to SRB status */
3554 if (SRB_STATUS(Srb
.SrbStatus
) == SRB_STATUS_BAD_FUNCTION
||
3555 SRB_STATUS(Srb
.SrbStatus
) == SRB_STATUS_BAD_SRB_BLOCK_LENGTH
)
3557 Status
= STATUS_INVALID_DEVICE_REQUEST
;
3561 Status
= STATUS_IO_DEVICE_ERROR
;
3569 ExFreePool(InquiryBuffer
);
3570 ExFreePool(SenseBuffer
);
3572 DPRINT("SpiSendInquiry() done with Status 0x%08X\n", Status
);
3578 /* Scans all SCSI buses */
3580 SpiScanAdapter(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
)
3582 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3586 PSCSI_BUS_SCAN_INFO BusScanInfo
;
3587 PSCSI_LUN_INFO LastLunInfo
, LunInfo
, LunInfoExists
;
3588 BOOLEAN DeviceExists
;
3593 DPRINT("SpiScanAdapter() called\n");
3595 /* Scan all buses */
3596 for (Bus
= 0; Bus
< DeviceExtension
->BusNum
; Bus
++)
3598 DPRINT(" Scanning bus %d\n", Bus
);
3601 /* Get pointer to the scan information */
3602 BusScanInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
];
3606 /* Find the last LUN info in the list */
3607 LunInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LunInfo
;
3608 LastLunInfo
= LunInfo
;
3610 while (LunInfo
!= NULL
)
3612 LastLunInfo
= LunInfo
;
3613 LunInfo
= LunInfo
->Next
;
3618 /* We need to allocate this buffer */
3619 BusScanInfo
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(SCSI_BUS_SCAN_INFO
), TAG_SCSIPORT
);
3623 DPRINT1("Out of resources!\n");
3627 /* Store the pointer in the BusScanInfo array */
3628 DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
] = BusScanInfo
;
3630 /* Fill this struct (length and bus ids for now) */
3631 BusScanInfo
->Length
= sizeof(SCSI_BUS_SCAN_INFO
);
3632 BusScanInfo
->LogicalUnitsCount
= 0;
3633 BusScanInfo
->BusIdentifier
= DeviceExtension
->PortConfig
->InitiatorBusId
[Bus
];
3634 BusScanInfo
->LunInfo
= NULL
;
3636 /* Set pointer to the last LUN info to NULL */
3640 /* Create LUN information structure */
3641 LunInfo
= ExAllocatePoolWithTag(PagedPool
, sizeof(SCSI_LUN_INFO
), TAG_SCSIPORT
);
3643 if (LunInfo
== NULL
)
3645 DPRINT1("Out of resources!\n");
3649 RtlZeroMemory(LunInfo
, sizeof(SCSI_LUN_INFO
));
3651 /* Create LunExtension */
3652 LunExtension
= SpiAllocateLunExtension (DeviceExtension
);
3654 /* And send INQUIRY to every target */
3655 for (Target
= 0; Target
< DeviceExtension
->PortConfig
->MaximumNumberOfTargets
; Target
++)
3657 /* TODO: Support scan bottom-up */
3659 /* Skip if it's the same address */
3660 if (Target
== BusScanInfo
->BusIdentifier
)
3663 /* Try to find an existing device here */
3664 DeviceExists
= FALSE
;
3665 LunInfoExists
= BusScanInfo
->LunInfo
;
3667 /* Find matching address on this bus */
3668 while (LunInfoExists
)
3670 if (LunInfoExists
->TargetId
== Target
)
3672 DeviceExists
= TRUE
;
3676 /* Advance to the next one */
3677 LunInfoExists
= LunInfoExists
->Next
;
3680 /* No need to bother rescanning, since we already did that before */
3684 /* Scan all logical units */
3685 for (Lun
= 0; Lun
< SCSI_MAXIMUM_LOGICAL_UNITS
; Lun
++)
3687 if ((!LunExtension
) || (!LunInfo
))
3690 /* Add extension to the list */
3691 Hint
= (Target
+ Lun
) % LUS_NUMBER
;
3692 LunExtension
->Next
= DeviceExtension
->LunExtensionList
[Hint
];
3693 DeviceExtension
->LunExtensionList
[Hint
] = LunExtension
;
3695 /* Fill Path, Target, Lun fields */
3696 LunExtension
->PathId
= LunInfo
->PathId
= (UCHAR
)Bus
;
3697 LunExtension
->TargetId
= LunInfo
->TargetId
= (UCHAR
) Target
;
3698 LunExtension
->Lun
= LunInfo
->Lun
= (UCHAR
)Lun
;
3700 /* Set flag to prevent race conditions */
3701 LunExtension
->Flags
|= SCSI_PORT_SCAN_IN_PROGRESS
;
3703 /* Zero LU extension contents */
3704 if (DeviceExtension
->LunExtensionSize
)
3706 RtlZeroMemory(LunExtension
+ 1,
3707 DeviceExtension
->LunExtensionSize
);
3710 /* Finally send the inquiry command */
3711 Status
= SpiSendInquiry(DeviceExtension
->DeviceObject
, LunInfo
);
3713 if (NT_SUCCESS(Status
))
3715 /* Let's see if we really found a device */
3716 PINQUIRYDATA InquiryData
= (PINQUIRYDATA
)LunInfo
->InquiryData
;
3718 /* Check if this device is unsupported */
3719 if (InquiryData
->DeviceTypeQualifier
== DEVICE_QUALIFIER_NOT_SUPPORTED
)
3721 DeviceExtension
->LunExtensionList
[Hint
] =
3722 DeviceExtension
->LunExtensionList
[Hint
]->Next
;
3727 /* Clear the "in scan" flag */
3728 LunExtension
->Flags
&= ~SCSI_PORT_SCAN_IN_PROGRESS
;
3730 DPRINT("SpiScanAdapter(): Found device of type %d at bus %d tid %d lun %d\n",
3731 InquiryData
->DeviceType
, Bus
, Target
, Lun
);
3733 /* Add this info to the linked list */
3734 LunInfo
->Next
= NULL
;
3736 LastLunInfo
->Next
= LunInfo
;
3738 BusScanInfo
->LunInfo
= LunInfo
;
3740 /* Store the last LUN info */
3741 LastLunInfo
= LunInfo
;
3743 /* Store DeviceObject */
3744 LunInfo
->DeviceObject
= DeviceExtension
->DeviceObject
;
3746 /* Allocate another buffer */
3747 LunInfo
= ExAllocatePoolWithTag(PagedPool
, sizeof(SCSI_LUN_INFO
), TAG_SCSIPORT
);
3751 DPRINT1("Out of resources!\n");
3755 RtlZeroMemory(LunInfo
, sizeof(SCSI_LUN_INFO
));
3757 /* Create a new LU extension */
3758 LunExtension
= SpiAllocateLunExtension(DeviceExtension
);
3764 /* Remove this LUN from the list */
3765 DeviceExtension
->LunExtensionList
[Hint
] =
3766 DeviceExtension
->LunExtensionList
[Hint
]->Next
;
3768 /* Decide whether we are continuing or not */
3769 if (Status
== STATUS_INVALID_DEVICE_REQUEST
)
3777 /* Free allocated buffers */
3779 ExFreePool(LunExtension
);
3782 ExFreePool(LunInfo
);
3784 /* Sum what we found */
3785 BusScanInfo
->LogicalUnitsCount
+= (UCHAR
) DevicesFound
;
3786 DPRINT(" Found %d devices on bus %d\n", DevicesFound
, Bus
);
3789 DPRINT ("SpiScanAdapter() done\n");
3794 SpiGetInquiryData(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
3797 ULONG InquiryDataSize
;
3798 PSCSI_LUN_INFO LunInfo
;
3799 ULONG BusCount
, LunCount
, Length
;
3800 PIO_STACK_LOCATION IrpStack
;
3801 PSCSI_ADAPTER_BUS_INFO AdapterBusInfo
;
3802 PSCSI_INQUIRY_DATA InquiryData
;
3803 PSCSI_BUS_DATA BusData
;
3807 DPRINT("SpiGetInquiryData() called\n");
3809 /* Get pointer to the buffer */
3810 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
3811 Buffer
= Irp
->AssociatedIrp
.SystemBuffer
;
3813 /* Initialize bus and LUN counters */
3814 BusCount
= DeviceExtension
->BusesConfig
->NumberOfBuses
;
3817 /* Calculate total number of LUNs */
3818 for (Bus
= 0; Bus
< BusCount
; Bus
++)
3819 LunCount
+= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LogicalUnitsCount
;
3821 /* Calculate size of inquiry data, rounding up to sizeof(ULONG) */
3823 ((sizeof(SCSI_INQUIRY_DATA
) - 1 + INQUIRYDATABUFFERSIZE
+
3824 sizeof(ULONG
) - 1) & ~(sizeof(ULONG
) - 1));
3826 /* Calculate data size */
3827 Length
= sizeof(SCSI_ADAPTER_BUS_INFO
) + (BusCount
- 1) *
3828 sizeof(SCSI_BUS_DATA
);
3830 Length
+= InquiryDataSize
* LunCount
;
3832 /* Check, if all data is going to fit into provided buffer */
3833 if (IrpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
< Length
)
3835 Irp
->IoStatus
.Status
= STATUS_BUFFER_TOO_SMALL
;
3836 return STATUS_BUFFER_TOO_SMALL
;
3839 /* Store data size in the IRP */
3840 Irp
->IoStatus
.Information
= Length
;
3842 DPRINT("Data size: %lu\n", Length
);
3844 AdapterBusInfo
= (PSCSI_ADAPTER_BUS_INFO
)Buffer
;
3846 AdapterBusInfo
->NumberOfBuses
= (UCHAR
)BusCount
;
3848 /* Point InquiryData to the corresponding place inside Buffer */
3849 InquiryData
= (PSCSI_INQUIRY_DATA
)(Buffer
+ sizeof(SCSI_ADAPTER_BUS_INFO
) +
3850 (BusCount
- 1) * sizeof(SCSI_BUS_DATA
));
3853 for (Bus
= 0; Bus
< BusCount
; Bus
++)
3855 BusData
= &AdapterBusInfo
->BusData
[Bus
];
3857 /* Calculate and save an offset of the inquiry data */
3858 BusData
->InquiryDataOffset
= (PUCHAR
)InquiryData
- Buffer
;
3860 /* Get a pointer to the LUN information structure */
3861 LunInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LunInfo
;
3863 /* Store Initiator Bus Id */
3864 BusData
->InitiatorBusId
=
3865 DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->BusIdentifier
;
3867 /* Store LUN count */
3868 BusData
->NumberOfLogicalUnits
=
3869 DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LogicalUnitsCount
;
3872 while (LunInfo
!= NULL
)
3874 DPRINT("(Bus %lu Target %lu Lun %lu)\n",
3875 Bus
, LunInfo
->TargetId
, LunInfo
->Lun
);
3877 /* Fill InquiryData with values */
3878 InquiryData
->PathId
= LunInfo
->PathId
;
3879 InquiryData
->TargetId
= LunInfo
->TargetId
;
3880 InquiryData
->Lun
= LunInfo
->Lun
;
3881 InquiryData
->InquiryDataLength
= INQUIRYDATABUFFERSIZE
;
3882 InquiryData
->DeviceClaimed
= LunInfo
->DeviceClaimed
;
3883 InquiryData
->NextInquiryDataOffset
=
3884 (PUCHAR
)InquiryData
+ InquiryDataSize
- Buffer
;
3886 /* Copy data in it */
3887 RtlCopyMemory(InquiryData
->InquiryData
,
3888 LunInfo
->InquiryData
,
3889 INQUIRYDATABUFFERSIZE
);
3891 /* Move to the next LUN */
3892 LunInfo
= LunInfo
->Next
;
3893 InquiryData
= (PSCSI_INQUIRY_DATA
) ((PCHAR
)InquiryData
+ InquiryDataSize
);
3896 /* Either mark the end, or set offset to 0 */
3897 if (BusData
->NumberOfLogicalUnits
!= 0)
3898 ((PSCSI_INQUIRY_DATA
) ((PCHAR
) InquiryData
- InquiryDataSize
))->NextInquiryDataOffset
= 0;
3900 BusData
->InquiryDataOffset
= 0;
3903 /* Finish with success */
3904 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
3905 return STATUS_SUCCESS
;
3908 static PSCSI_REQUEST_BLOCK_INFO
3909 SpiGetSrbData(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
3915 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3917 if (QueueTag
== SP_UNTAGGED
)
3919 /* Untagged request, get LU and return pointer to SrbInfo */
3920 LunExtension
= SpiGetLunExtension(DeviceExtension
,
3925 /* Return NULL in case of error */
3929 /* Return the pointer to SrbInfo */
3930 return &LunExtension
->SrbInfo
;
3934 /* Make sure the tag is valid, if it is - return the data */
3935 if (QueueTag
> DeviceExtension
->SrbDataCount
|| QueueTag
< 1)
3938 return &DeviceExtension
->SrbInfo
[QueueTag
-1];
3943 SpiSendRequestSense(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
3944 IN PSCSI_REQUEST_BLOCK InitialSrb
)
3946 PSCSI_REQUEST_BLOCK Srb
;
3949 PIO_STACK_LOCATION IrpStack
;
3950 LARGE_INTEGER LargeInt
;
3953 DPRINT("SpiSendRequestSense() entered, InitialSrb %p\n", InitialSrb
);
3956 Srb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(SCSI_REQUEST_BLOCK
) + sizeof(PVOID
), TAG_SCSIPORT
);
3957 RtlZeroMemory(Srb
, sizeof(SCSI_REQUEST_BLOCK
));
3960 LargeInt
.QuadPart
= (LONGLONG
) 1;
3961 Irp
= IoBuildAsynchronousFsdRequest(IRP_MJ_READ
,
3962 DeviceExtension
->DeviceObject
,
3963 InitialSrb
->SenseInfoBuffer
,
3964 InitialSrb
->SenseInfoBufferLength
,
3968 IoSetCompletionRoutine(Irp
,
3969 (PIO_COMPLETION_ROUTINE
)SpiCompletionRoutine
,
3977 DPRINT("SpiSendRequestSense() failed, Srb %p\n", Srb
);
3981 IrpStack
= IoGetNextIrpStackLocation(Irp
);
3982 IrpStack
->MajorFunction
= IRP_MJ_SCSI
;
3984 /* Put Srb address into Irp... */
3985 IrpStack
->Parameters
.Others
.Argument1
= (PVOID
)Srb
;
3987 /* ...and vice versa */
3988 Srb
->OriginalRequest
= Irp
;
3991 Ptr
= (PVOID
*)(Srb
+1);
3994 /* Build CDB for REQUEST SENSE */
3996 Cdb
= (PCDB
)Srb
->Cdb
;
3998 Cdb
->CDB6INQUIRY
.OperationCode
= SCSIOP_REQUEST_SENSE
;
3999 Cdb
->CDB6INQUIRY
.LogicalUnitNumber
= 0;
4000 Cdb
->CDB6INQUIRY
.Reserved1
= 0;
4001 Cdb
->CDB6INQUIRY
.PageCode
= 0;
4002 Cdb
->CDB6INQUIRY
.IReserved
= 0;
4003 Cdb
->CDB6INQUIRY
.AllocationLength
= (UCHAR
)InitialSrb
->SenseInfoBufferLength
;
4004 Cdb
->CDB6INQUIRY
.Control
= 0;
4007 Srb
->TargetId
= InitialSrb
->TargetId
;
4008 Srb
->Lun
= InitialSrb
->Lun
;
4009 Srb
->PathId
= InitialSrb
->PathId
;
4011 Srb
->Function
= SRB_FUNCTION_EXECUTE_SCSI
;
4012 Srb
->Length
= sizeof(SCSI_REQUEST_BLOCK
);
4014 /* Timeout will be 2 seconds */
4015 Srb
->TimeOutValue
= 2;
4017 /* No auto request sense */
4018 Srb
->SenseInfoBufferLength
= 0;
4019 Srb
->SenseInfoBuffer
= NULL
;
4021 /* Set necessary flags */
4022 Srb
->SrbFlags
= SRB_FLAGS_DATA_IN
| SRB_FLAGS_BYPASS_FROZEN_QUEUE
|
4023 SRB_FLAGS_DISABLE_DISCONNECT
;
4025 /* Transfer disable synch transfer flag */
4026 if (InitialSrb
->SrbFlags
& SRB_FLAGS_DISABLE_SYNCH_TRANSFER
)
4027 Srb
->SrbFlags
|= SRB_FLAGS_DISABLE_SYNCH_TRANSFER
;
4029 Srb
->DataBuffer
= InitialSrb
->SenseInfoBuffer
;
4031 /* Fill the transfer length */
4032 Srb
->DataTransferLength
= InitialSrb
->SenseInfoBufferLength
;
4034 /* Clear statuses */
4035 Srb
->ScsiStatus
= Srb
->SrbStatus
= 0;
4038 /* Call the driver */
4039 (VOID
)IoCallDriver(DeviceExtension
->DeviceObject
, Irp
);
4041 DPRINT("SpiSendRequestSense() done\n");
4048 SpiProcessCompletedRequest(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
4049 IN PSCSI_REQUEST_BLOCK_INFO SrbInfo
,
4050 OUT PBOOLEAN NeedToCallStartIo
)
4052 PSCSI_REQUEST_BLOCK Srb
;
4053 PSCSI_PORT_LUN_EXTENSION LunExtension
;
4056 ULONG SequenceNumber
;
4059 Irp
= Srb
->OriginalRequest
;
4061 /* Get Lun extension */
4062 LunExtension
= SpiGetLunExtension(DeviceExtension
,
4067 if (Srb
->SrbFlags
& SRB_FLAGS_UNSPECIFIED_DIRECTION
&&
4068 DeviceExtension
->MapBuffers
&&
4071 /* MDL is shared if transfer is broken into smaller parts */
4072 Srb
->DataBuffer
= (PCCHAR
)MmGetMdlVirtualAddress(Irp
->MdlAddress
) +
4073 ((PCCHAR
)Srb
->DataBuffer
- SrbInfo
->DataOffset
);
4075 /* In case of data going in, flush the buffers */
4076 if (Srb
->SrbFlags
& SRB_FLAGS_DATA_IN
)
4078 KeFlushIoBuffers(Irp
->MdlAddress
,
4085 /* Flush adapter if needed */
4086 if (SrbInfo
->BaseOfMapRegister
)
4088 /* TODO: Implement */
4092 /* Clear the request */
4093 SrbInfo
->Srb
= NULL
;
4095 /* If disconnect is disabled... */
4096 if (Srb
->SrbFlags
& SRB_FLAGS_DISABLE_DISCONNECT
)
4098 /* Acquire the spinlock since we mess with flags */
4099 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4101 /* Set corresponding flag */
4102 DeviceExtension
->Flags
|= SCSI_PORT_DISCONNECT_ALLOWED
;
4104 /* Clear the timer if needed */
4105 if (!(DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_RESET
))
4106 DeviceExtension
->TimerCount
= -1;
4108 /* Spinlock is not needed anymore */
4109 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4111 if (!(DeviceExtension
->Flags
& SCSI_PORT_REQUEST_PENDING
) &&
4112 !(DeviceExtension
->Flags
& SCSI_PORT_DEVICE_BUSY
) &&
4113 !(*NeedToCallStartIo
))
4115 /* We're not busy, but we have a request pending */
4116 IoStartNextPacket(DeviceExtension
->DeviceObject
, FALSE
);
4120 /* Scatter/gather */
4121 if (Srb
->SrbFlags
& SRB_FLAGS_SGLIST_FROM_POOL
)
4123 /* TODO: Implement */
4127 /* Acquire spinlock (we're freeing SrbExtension) */
4128 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4130 /* Free it (if needed) */
4131 if (Srb
->SrbExtension
)
4133 if (Srb
->SenseInfoBuffer
!= NULL
&& DeviceExtension
->SupportsAutoSense
)
4135 ASSERT(Srb
->SenseInfoBuffer
== NULL
|| SrbInfo
->SaveSenseRequest
!= NULL
);
4137 if (Srb
->SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
)
4139 /* Copy sense data to the buffer */
4140 RtlCopyMemory(SrbInfo
->SaveSenseRequest
,
4141 Srb
->SenseInfoBuffer
,
4142 Srb
->SenseInfoBufferLength
);
4145 /* And restore the pointer */
4146 Srb
->SenseInfoBuffer
= SrbInfo
->SaveSenseRequest
;
4149 /* Put it into the free srb extensions list */
4150 *((PVOID
*)Srb
->SrbExtension
) = DeviceExtension
->FreeSrbExtensions
;
4151 DeviceExtension
->FreeSrbExtensions
= Srb
->SrbExtension
;
4154 /* Save transfer length in the IRP */
4155 Irp
->IoStatus
.Information
= Srb
->DataTransferLength
;
4157 SequenceNumber
= SrbInfo
->SequenceNumber
;
4158 SrbInfo
->SequenceNumber
= 0;
4160 /* Decrement the queue count */
4161 LunExtension
->QueueCount
--;
4163 /* Free Srb, if needed*/
4164 if (Srb
->QueueTag
!= SP_UNTAGGED
)
4166 /* Put it into the free list */
4167 SrbInfo
->Requests
.Blink
= NULL
;
4168 SrbInfo
->Requests
.Flink
= (PLIST_ENTRY
)DeviceExtension
->FreeSrbInfo
;
4169 DeviceExtension
->FreeSrbInfo
= SrbInfo
;
4172 /* SrbInfo is not used anymore */
4175 if (DeviceExtension
->Flags
& SCSI_PORT_REQUEST_PENDING
)
4177 /* Clear the flag */
4178 DeviceExtension
->Flags
&= ~SCSI_PORT_REQUEST_PENDING
;
4180 /* Note the caller about StartIo */
4181 *NeedToCallStartIo
= TRUE
;
4184 if (SRB_STATUS(Srb
->SrbStatus
) == SRB_STATUS_SUCCESS
)
4186 /* Start the packet */
4187 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
4189 if (!(Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
) &&
4190 LunExtension
->RequestTimeout
== -1)
4192 /* Start the next packet */
4193 SpiGetNextRequestFromLun(DeviceExtension
, LunExtension
);
4197 /* Release the spinlock */
4198 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4201 DPRINT("IoCompleting request IRP 0x%p\n", Irp
);
4203 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
4205 /* Decrement number of active requests, and analyze the result */
4206 Result
= InterlockedDecrement(&DeviceExtension
->ActiveRequestCounter
);
4209 !DeviceExtension
->MapRegisters
&&
4210 DeviceExtension
->AdapterObject
!= NULL
)
4212 /* Nullify map registers */
4213 DeviceExtension
->MapRegisterBase
= NULL
;
4214 IoFreeAdapterChannel(DeviceExtension
->AdapterObject
);
4217 /* Exit, we're done */
4221 /* Decrement number of active requests, and analyze the result */
4222 Result
= InterlockedDecrement(&DeviceExtension
->ActiveRequestCounter
);
4225 !DeviceExtension
->MapRegisters
&&
4226 DeviceExtension
->AdapterObject
!= NULL
)
4228 /* Result is negative, so this is a slave, free map registers */
4229 DeviceExtension
->MapRegisterBase
= NULL
;
4230 IoFreeAdapterChannel(DeviceExtension
->AdapterObject
);
4233 /* Convert status */
4234 Irp
->IoStatus
.Status
= SpiStatusSrbToNt(Srb
->SrbStatus
);
4236 /* It's not a bypass, it's busy or the queue is full? */
4237 if ((Srb
->ScsiStatus
== SCSISTAT_BUSY
||
4238 Srb
->SrbStatus
== SRB_STATUS_BUSY
||
4239 Srb
->ScsiStatus
== SCSISTAT_QUEUE_FULL
) &&
4240 !(Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
))
4243 DPRINT("Busy SRB status %x\n", Srb
->SrbStatus
);
4245 /* Requeu, if needed */
4246 if (LunExtension
->Flags
& (LUNEX_FROZEN_QUEUE
| LUNEX_BUSY
))
4248 DPRINT("it's being requeued\n");
4250 Srb
->SrbStatus
= SRB_STATUS_PENDING
;
4251 Srb
->ScsiStatus
= 0;
4253 if (!KeInsertByKeyDeviceQueue(&LunExtension
->DeviceQueue
,
4254 &Irp
->Tail
.Overlay
.DeviceQueueEntry
,
4257 /* It's a big f.ck up if we got here */
4258 Srb
->SrbStatus
= SRB_STATUS_ERROR
;
4259 Srb
->ScsiStatus
= SCSISTAT_BUSY
;
4265 /* Release the spinlock */
4266 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4269 else if (LunExtension
->AttemptCount
++ < 20)
4271 /* LUN is still busy */
4272 Srb
->ScsiStatus
= 0;
4273 Srb
->SrbStatus
= SRB_STATUS_PENDING
;
4275 LunExtension
->BusyRequest
= Irp
;
4276 LunExtension
->Flags
|= LUNEX_BUSY
;
4278 /* Release the spinlock */
4279 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4284 /* Freeze the queue*/
4285 Srb
->SrbStatus
|= SRB_STATUS_QUEUE_FROZEN
;
4286 LunExtension
->Flags
|= LUNEX_FROZEN_QUEUE
;
4288 /* "Unfull" the queue */
4289 LunExtension
->Flags
&= ~LUNEX_FULL_QUEUE
;
4291 /* Release the spinlock */
4292 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4294 /* Return status that the device is not ready */
4295 Irp
->IoStatus
.Status
= STATUS_DEVICE_NOT_READY
;
4296 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
4302 /* Start the next request, if LUN is idle, and this is sense request */
4303 if (((Srb
->ScsiStatus
!= SCSISTAT_CHECK_CONDITION
) ||
4304 (Srb
->SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
) ||
4305 !Srb
->SenseInfoBuffer
|| !Srb
->SenseInfoBufferLength
)
4306 && (Srb
->SrbFlags
& SRB_FLAGS_NO_QUEUE_FREEZE
))
4308 if (LunExtension
->RequestTimeout
== -1)
4309 SpiGetNextRequestFromLun(DeviceExtension
, LunExtension
);
4311 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4315 /* Freeze the queue */
4316 Srb
->SrbStatus
|= SRB_STATUS_QUEUE_FROZEN
;
4317 LunExtension
->Flags
|= LUNEX_FROZEN_QUEUE
;
4319 /* Do we need a request sense? */
4320 if (Srb
->ScsiStatus
== SCSISTAT_CHECK_CONDITION
&&
4321 !(Srb
->SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
) &&
4322 Srb
->SenseInfoBuffer
&& Srb
->SenseInfoBufferLength
)
4324 /* If LUN is busy, we have to requeue it in order to allow request sense */
4325 if (LunExtension
->Flags
& LUNEX_BUSY
)
4327 DPRINT("Requeueing busy request to allow request sense\n");
4329 if (!KeInsertByKeyDeviceQueue(&LunExtension
->DeviceQueue
,
4330 &LunExtension
->BusyRequest
->Tail
.Overlay
.DeviceQueueEntry
,
4333 /* We should never get here */
4336 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4337 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
4342 /* Clear busy flags */
4343 LunExtension
->Flags
&= ~(LUNEX_FULL_QUEUE
| LUNEX_BUSY
);
4346 /* Release the spinlock */
4347 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4349 /* Send RequestSense */
4350 SpiSendRequestSense(DeviceExtension
, Srb
);
4356 /* Release the spinlock */
4357 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4360 /* Complete the request */
4361 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
4366 SpiCompletionRoutine(PDEVICE_OBJECT DeviceObject
,
4370 PSCSI_REQUEST_BLOCK Srb
= (PSCSI_REQUEST_BLOCK
)Context
;
4371 PSCSI_REQUEST_BLOCK InitialSrb
;
4374 DPRINT("SpiCompletionRoutine() entered, IRP %p \n", Irp
);
4376 if ((Srb
->Function
== SRB_FUNCTION_RESET_BUS
) ||
4377 (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
))
4379 /* Deallocate SRB and IRP and exit */
4383 return STATUS_MORE_PROCESSING_REQUIRED
;
4386 /* Get a pointer to the SRB and IRP which were initially sent */
4387 InitialSrb
= *((PVOID
*)(Srb
+1));
4388 InitialIrp
= InitialSrb
->OriginalRequest
;
4390 if ((SRB_STATUS(Srb
->SrbStatus
) == SRB_STATUS_SUCCESS
) ||
4391 (SRB_STATUS(Srb
->SrbStatus
) == SRB_STATUS_DATA_OVERRUN
))
4393 /* Sense data is OK */
4394 InitialSrb
->SrbStatus
|= SRB_STATUS_AUTOSENSE_VALID
;
4396 /* Set length to be the same */
4397 InitialSrb
->SenseInfoBufferLength
= (UCHAR
)Srb
->DataTransferLength
;
4400 /* Make sure initial SRB's queue is frozen */
4401 ASSERT(InitialSrb
->SrbStatus
& SRB_STATUS_QUEUE_FROZEN
);
4403 /* Complete this request */
4404 IoCompleteRequest(InitialIrp
, IO_DISK_INCREMENT
);
4406 /* Deallocate everything (internal) */
4409 if (Irp
->MdlAddress
!= NULL
)
4411 MmUnlockPages(Irp
->MdlAddress
);
4412 IoFreeMdl(Irp
->MdlAddress
);
4413 Irp
->MdlAddress
= NULL
;
4417 return STATUS_MORE_PROCESSING_REQUIRED
;
4422 static BOOLEAN STDCALL
4423 ScsiPortIsr(IN PKINTERRUPT Interrupt
,
4424 IN PVOID ServiceContext
)
4426 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
4429 DPRINT("ScsiPortIsr() called!\n");
4431 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)ServiceContext
;
4433 /* If interrupts are disabled - we don't expect any */
4434 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_DISABLE_INTERRUPTS
)
4437 /* Call miniport's HwInterrupt routine */
4438 Result
= DeviceExtension
->HwInterrupt(&DeviceExtension
->MiniPortDeviceExtension
);
4440 /* If flag of notification is set - queue a DPC */
4441 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
4443 IoRequestDpc(DeviceExtension
->DeviceObject
,
4444 DeviceExtension
->CurrentIrp
,
4453 SpiSaveInterruptData(IN PVOID Context
)
4455 PSCSI_PORT_SAVE_INTERRUPT InterruptContext
= Context
;
4456 PSCSI_PORT_LUN_EXTENSION LunExtension
;
4457 PSCSI_REQUEST_BLOCK Srb
;
4458 PSCSI_REQUEST_BLOCK_INFO SrbInfo
, NextSrbInfo
;
4459 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
4462 /* Get pointer to the device extension */
4463 DeviceExtension
= InterruptContext
->DeviceExtension
;
4465 /* If we don't have anything pending - return */
4466 if (!(DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
))
4469 /* Actually save the interrupt data */
4470 *InterruptContext
->InterruptData
= DeviceExtension
->InterruptData
;
4472 /* Clear the data stored in the device extension */
4473 DeviceExtension
->InterruptData
.Flags
&=
4474 (SCSI_PORT_RESET
| SCSI_PORT_RESET_REQUEST
| SCSI_PORT_DISABLE_INTERRUPTS
);
4475 DeviceExtension
->InterruptData
.CompletedAbort
= NULL
;
4476 DeviceExtension
->InterruptData
.ReadyLun
= NULL
;
4477 DeviceExtension
->InterruptData
.CompletedRequests
= NULL
;
4479 /* Loop through the list of completed requests */
4480 SrbInfo
= InterruptContext
->InterruptData
->CompletedRequests
;
4484 /* Make sure we have SRV */
4485 ASSERT(SrbInfo
->Srb
);
4487 /* Get SRB and LunExtension */
4490 LunExtension
= SpiGetLunExtension(DeviceExtension
,
4495 /* We have to check special cases if request is unsuccessfull*/
4496 if (Srb
->SrbStatus
!= SRB_STATUS_SUCCESS
)
4498 /* Check if we need request sense by a few conditions */
4499 if (Srb
->SenseInfoBuffer
&& Srb
->SenseInfoBufferLength
&&
4500 Srb
->ScsiStatus
== SCSISTAT_CHECK_CONDITION
&&
4501 !(Srb
->SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
))
4503 if (LunExtension
->Flags
& LUNEX_NEED_REQUEST_SENSE
)
4505 /* It means: we tried to send REQUEST SENSE, but failed */
4507 Srb
->ScsiStatus
= 0;
4508 Srb
->SrbStatus
= SRB_STATUS_REQUEST_SENSE_FAILED
;
4512 /* Set the corresponding flag, so that REQUEST SENSE
4514 LunExtension
->Flags
|= LUNEX_NEED_REQUEST_SENSE
;
4519 /* Check for a full queue */
4520 if (Srb
->ScsiStatus
== SCSISTAT_QUEUE_FULL
)
4522 /* TODO: Implement when it's encountered */
4527 /* Let's decide if we need to watch timeout or not */
4528 if (Srb
->QueueTag
== SP_UNTAGGED
)
4534 if (LunExtension
->SrbInfo
.Requests
.Flink
== &SrbInfo
->Requests
)
4539 /* Remove it from the queue */
4540 RemoveEntryList(&SrbInfo
->Requests
);
4545 /* We have to maintain timeout counter */
4546 if (IsListEmpty(&LunExtension
->SrbInfo
.Requests
))
4548 LunExtension
->RequestTimeout
= -1;
4552 NextSrbInfo
= CONTAINING_RECORD(LunExtension
->SrbInfo
.Requests
.Flink
,
4553 SCSI_REQUEST_BLOCK_INFO
,
4556 Srb
= NextSrbInfo
->Srb
;
4558 /* Update timeout counter */
4559 LunExtension
->RequestTimeout
= Srb
->TimeOutValue
;
4563 SrbInfo
= SrbInfo
->CompletedRequests
;
4571 SpiGetNextRequestFromLun(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
4572 IN PSCSI_PORT_LUN_EXTENSION LunExtension
)
4574 PIO_STACK_LOCATION IrpStack
;
4576 PKDEVICE_QUEUE_ENTRY Entry
;
4577 PSCSI_REQUEST_BLOCK Srb
;
4580 /* If LUN is not active or queue is more than maximum allowed */
4581 if (LunExtension
->QueueCount
>= LunExtension
->MaxQueueCount
||
4582 !(LunExtension
->Flags
& SCSI_PORT_LU_ACTIVE
))
4584 /* Release the spinlock and exit */
4585 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4589 /* Check if we can get a next request */
4590 if (LunExtension
->Flags
&
4591 (LUNEX_NEED_REQUEST_SENSE
| LUNEX_BUSY
|
4592 LUNEX_FULL_QUEUE
| LUNEX_FROZEN_QUEUE
| LUNEX_REQUEST_PENDING
))
4594 /* Pending requests can only be started if the queue is empty */
4595 if (IsListEmpty(&LunExtension
->SrbInfo
.Requests
) &&
4596 !(LunExtension
->Flags
&
4597 (LUNEX_BUSY
| LUNEX_FROZEN_QUEUE
| LUNEX_FULL_QUEUE
| LUNEX_NEED_REQUEST_SENSE
)))
4599 /* Make sure we have SRB */
4600 ASSERT(LunExtension
->SrbInfo
.Srb
== NULL
);
4602 /* Clear active and pending flags */
4603 LunExtension
->Flags
&= ~(LUNEX_REQUEST_PENDING
| SCSI_PORT_LU_ACTIVE
);
4605 /* Get next Irp, and clear pending requests list */
4606 NextIrp
= LunExtension
->PendingRequest
;
4607 LunExtension
->PendingRequest
= NULL
;
4609 /* Set attempt counter to zero */
4610 LunExtension
->AttemptCount
= 0;
4612 /* Release the spinlock */
4613 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4615 /* Start the next pending request */
4616 IoStartPacket(DeviceExtension
->DeviceObject
, NextIrp
, (PULONG
)NULL
, NULL
);
4622 /* Release the spinlock, without clearing any flags and exit */
4623 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4629 /* Reset active flag */
4630 LunExtension
->Flags
&= ~SCSI_PORT_LU_ACTIVE
;
4632 /* Set attempt counter to zero */
4633 LunExtension
->AttemptCount
= 0;
4635 /* Remove packet from the device queue */
4636 Entry
= KeRemoveByKeyDeviceQueue(&LunExtension
->DeviceQueue
, LunExtension
->SortKey
);
4640 /* Get pointer to the next irp */
4641 NextIrp
= CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.DeviceQueueEntry
);
4643 /* Get point to the SRB */
4644 IrpStack
= IoGetCurrentIrpStackLocation(NextIrp
);
4645 Srb
= (PSCSI_REQUEST_BLOCK
)IrpStack
->Parameters
.Others
.Argument1
;
4648 LunExtension
->SortKey
= Srb
->QueueSortKey
;
4649 LunExtension
->SortKey
++;
4651 /* Release the spinlock */
4652 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4654 /* Start the next pending request */
4655 IoStartPacket(DeviceExtension
->DeviceObject
, NextIrp
, (PULONG
)NULL
, NULL
);
4659 /* Release the spinlock */
4660 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4666 // ScsiPortDpcForIsr
4673 // IN PDEVICE_OBJECT DpcDeviceObject
4675 // IN PVOID DpcContext
4678 ScsiPortDpcForIsr(IN PKDPC Dpc
,
4679 IN PDEVICE_OBJECT DpcDeviceObject
,
4681 IN PVOID DpcContext
)
4683 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
= DpcDeviceObject
->DeviceExtension
;
4684 SCSI_PORT_INTERRUPT_DATA InterruptData
;
4685 SCSI_PORT_SAVE_INTERRUPT Context
;
4686 PSCSI_PORT_LUN_EXTENSION LunExtension
;
4687 BOOLEAN NeedToStartIo
;
4688 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
4690 DPRINT("ScsiPortDpcForIsr(Dpc %p DpcDeviceObject %p DpcIrp %p DpcContext %p)\n",
4691 Dpc
, DpcDeviceObject
, DpcIrp
, DpcContext
);
4693 /* We need to acquire spinlock */
4694 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4696 RtlZeroMemory(&InterruptData
, sizeof(SCSI_PORT_INTERRUPT_DATA
));
4700 /* Interrupt structure must be snapshotted, and only then analyzed */
4701 Context
.InterruptData
= &InterruptData
;
4702 Context
.DeviceExtension
= DeviceExtension
;
4704 if (!KeSynchronizeExecution(DeviceExtension
->Interrupt
,
4705 SpiSaveInterruptData
,
4708 /* Nothing - just return (don't forget to release the spinlock */
4709 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4710 DPRINT("ScsiPortDpcForIsr() done\n");
4714 /* If flush of adapters is needed - do it */
4715 if (InterruptData
.Flags
& SCSI_PORT_FLUSH_ADAPTERS
)
4717 /* TODO: Implement */
4721 /* Check for IoMapTransfer */
4722 if (InterruptData
.Flags
& SCSI_PORT_MAP_TRANSFER
)
4724 /* TODO: Implement */
4728 /* Check if timer is needed */
4729 if (InterruptData
.Flags
& SCIS_PORT_TIMER_NEEDED
)
4731 /* TODO: Implement */
4735 /* If it's ready for the next request */
4736 if (InterruptData
.Flags
& SCSI_PORT_NEXT_REQUEST_READY
)
4738 /* Check for a duplicate request (NextRequest+NextLuRequest) */
4739 if ((DeviceExtension
->Flags
&
4740 (SCSI_PORT_DEVICE_BUSY
| SCSI_PORT_DISCONNECT_ALLOWED
)) ==
4741 (SCSI_PORT_DEVICE_BUSY
| SCSI_PORT_DISCONNECT_ALLOWED
))
4743 /* Clear busy flag set by ScsiPortStartPacket() */
4744 DeviceExtension
->Flags
&= ~SCSI_PORT_DEVICE_BUSY
;
4746 if (!(InterruptData
.Flags
& SCSI_PORT_RESET
))
4748 /* Ready for next, and no reset is happening */
4749 DeviceExtension
->TimerCount
= -1;
4754 /* Not busy, but not ready for the next request */
4755 DeviceExtension
->Flags
&= ~SCSI_PORT_DEVICE_BUSY
;
4756 InterruptData
.Flags
&= ~SCSI_PORT_NEXT_REQUEST_READY
;
4761 if (InterruptData
.Flags
& SCSI_PORT_RESET_REPORTED
)
4763 /* Hold for a bit */
4764 DeviceExtension
->TimerCount
= 4;
4767 /* Any ready LUN? */
4768 if (InterruptData
.ReadyLun
!= NULL
)
4771 /* Process all LUNs from the list*/
4774 /* Remove it from the list first (as processed) */
4775 LunExtension
= InterruptData
.ReadyLun
;
4776 InterruptData
.ReadyLun
= LunExtension
->ReadyLun
;
4777 LunExtension
->ReadyLun
= NULL
;
4779 /* Get next request for this LUN */
4780 SpiGetNextRequestFromLun(DeviceExtension
, LunExtension
);
4782 /* Still ready requests exist?
4783 If yes - get spinlock, if no - stop here */
4784 if (InterruptData
.ReadyLun
!= NULL
)
4785 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4792 /* Release the spinlock */
4793 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4796 /* If we ready for next packet, start it */
4797 if (InterruptData
.Flags
& SCSI_PORT_NEXT_REQUEST_READY
)
4798 IoStartNextPacket(DeviceExtension
->DeviceObject
, FALSE
);
4800 NeedToStartIo
= FALSE
;
4802 /* Loop the completed request list */
4803 while (InterruptData
.CompletedRequests
)
4805 /* Remove the request */
4806 SrbInfo
= InterruptData
.CompletedRequests
;
4807 InterruptData
.CompletedRequests
= SrbInfo
->CompletedRequests
;
4808 SrbInfo
->CompletedRequests
= NULL
;
4811 SpiProcessCompletedRequest(DeviceExtension
,
4816 /* Loop abort request list */
4817 while (InterruptData
.CompletedAbort
)
4819 LunExtension
= InterruptData
.CompletedAbort
;
4821 /* Remove the request */
4822 InterruptData
.CompletedAbort
= LunExtension
->CompletedAbortRequests
;
4824 /* Get spinlock since we're going to change flags */
4825 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4827 /* TODO: Put SrbExtension to the list of free extensions */
4831 /* If we need - call StartIo routine */
4834 /* Make sure CurrentIrp is not null! */
4835 ASSERT(DpcDeviceObject
->CurrentIrp
!= NULL
);
4836 ScsiPortStartIo(DpcDeviceObject
, DpcDeviceObject
->CurrentIrp
);
4839 /* Everything has been done, check */
4840 if (InterruptData
.Flags
& SCSI_PORT_ENABLE_INT_REQUEST
)
4842 /* Synchronize using spinlock */
4843 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4845 /* Request an interrupt */
4846 DeviceExtension
->HwInterrupt(DeviceExtension
->MiniPortDeviceExtension
);
4848 ASSERT(DeviceExtension
->Flags
& SCSI_PORT_DISABLE_INT_REQUESET
);
4850 /* Should interrupts be enabled again? */
4851 if (DeviceExtension
->Flags
& SCSI_PORT_DISABLE_INT_REQUESET
)
4853 /* Clear this flag */
4854 DeviceExtension
->Flags
&= ~SCSI_PORT_DISABLE_INT_REQUESET
;
4856 /* Call a special routine to do this */
4859 KeSynchronizeExecution(DeviceExtension
->Interrupt
,
4860 SpiEnableInterrupts
,
4865 /* If we need a notification again - loop */
4866 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
4869 /* Release the spinlock */
4870 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4873 DPRINT("ScsiPortDpcForIsr() done\n");
4878 SpiProcessTimeout(PVOID ServiceContext
)
4880 PDEVICE_OBJECT DeviceObject
= (PDEVICE_OBJECT
)ServiceContext
;
4881 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
= DeviceObject
->DeviceExtension
;
4884 DPRINT("SpiProcessTimeout() entered\n");
4886 DeviceExtension
->TimerCount
= -1;
4888 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_RESET
)
4890 DeviceExtension
->InterruptData
.Flags
&= ~SCSI_PORT_RESET
;
4892 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_RESET_REQUEST
)
4894 DeviceExtension
->InterruptData
.Flags
&= ~SCSI_PORT_RESET_REQUEST
;
4895 ScsiPortStartPacket(ServiceContext
);
4902 DPRINT("Resetting the bus\n");
4904 for (Bus
= 0; Bus
< DeviceExtension
->BusNum
; Bus
++)
4906 DeviceExtension
->HwResetBus(DeviceExtension
->MiniPortDeviceExtension
, Bus
);
4908 /* Reset flags and set reset timeout to 4 seconds */
4909 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_RESET
;
4910 DeviceExtension
->TimerCount
= 4;
4913 /* If miniport requested - request a dpc for it */
4914 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
4915 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
4924 SpiResetBus(PVOID ServiceContext
)
4926 PRESETBUS_PARAMS ResetParams
= (PRESETBUS_PARAMS
)ServiceContext
;
4927 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
4929 /* Perform the bus reset */
4930 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)ResetParams
->DeviceExtension
;
4931 DeviceExtension
->HwResetBus(DeviceExtension
->MiniPortDeviceExtension
,
4932 ResetParams
->PathId
);
4934 /* Set flags and start the timer */
4935 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_RESET
;
4936 DeviceExtension
->TimerCount
= 4;
4938 /* If miniport requested - give him a DPC */
4939 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
4940 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
4947 // This function handles timeouts and other time delayed processing
4952 // IN PDEVICE_OBJECT DeviceObject Device object registered with timer
4953 // IN PVOID Context the Controller extension for the
4954 // controller the device is on
4957 ScsiPortIoTimer(PDEVICE_OBJECT DeviceObject
,
4960 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
4961 PSCSI_PORT_LUN_EXTENSION LunExtension
;
4965 DPRINT("ScsiPortIoTimer()\n");
4967 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
4969 /* Protect with the spinlock */
4970 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4972 /* Check timeouts */
4973 if (DeviceExtension
->TimerCount
> 0)
4975 /* Decrease the timeout counter */
4976 DeviceExtension
->TimerCount
--;
4978 if (DeviceExtension
->TimerCount
== 0)
4980 /* Timeout, process it */
4981 if (KeSynchronizeExecution(DeviceExtension
->Interrupt
,
4983 DeviceExtension
->DeviceObject
))
4985 DPRINT("Error happened during processing timeout, but nothing critical\n");
4989 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4991 /* We should exit now, since timeout is processed */
4995 /* Per-Lun scanning of timeouts is needed... */
4996 for (Lun
= 0; Lun
< LUS_NUMBER
; Lun
++)
4998 LunExtension
= DeviceExtension
->LunExtensionList
[Lun
];
5000 while (LunExtension
)
5002 if (LunExtension
->Flags
& LUNEX_BUSY
)
5004 if (!(LunExtension
->Flags
&
5005 (LUNEX_NEED_REQUEST_SENSE
| LUNEX_FROZEN_QUEUE
)))
5007 DPRINT("Retrying busy request\n");
5009 /* Clear flags, and retry busy request */
5010 LunExtension
->Flags
&= ~(LUNEX_BUSY
| LUNEX_FULL_QUEUE
);
5011 Irp
= LunExtension
->BusyRequest
;
5013 /* Clearing busy request */
5014 LunExtension
->BusyRequest
= NULL
;
5016 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
5018 IoStartPacket(DeviceObject
, Irp
, (PULONG
)NULL
, NULL
);
5020 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
5023 else if (LunExtension
->RequestTimeout
== 0)
5025 RESETBUS_PARAMS ResetParams
;
5027 LunExtension
->RequestTimeout
= -1;
5029 DPRINT("Request timed out, resetting bus\n");
5031 /* Pass params to the bus reset routine */
5032 ResetParams
.PathId
= LunExtension
->PathId
;
5033 ResetParams
.DeviceExtension
= DeviceExtension
;
5035 if (!KeSynchronizeExecution(DeviceExtension
->Interrupt
,
5039 DPRINT1("Reset failed\n");
5042 else if (LunExtension
->RequestTimeout
> 0)
5044 /* Decrement the timeout counter */
5045 LunExtension
->RequestTimeout
--;
5048 LunExtension
= LunExtension
->Next
;
5052 /* Release the spinlock */
5053 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
5056 /**********************************************************************
5061 * Builds the registry device map of all device which are attached
5062 * to the given SCSI HBA port. The device map is located at:
5063 * \Registry\Machine\DeviceMap\Scsi
5073 * Name of registry driver service key.
5080 SpiBuildDeviceMap (PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
5081 PUNICODE_STRING RegistryPath
)
5083 PSCSI_PORT_LUN_EXTENSION LunExtension
;
5084 OBJECT_ATTRIBUTES ObjectAttributes
;
5085 UNICODE_STRING KeyName
;
5086 UNICODE_STRING ValueName
;
5087 WCHAR NameBuffer
[64];
5090 HANDLE ScsiPortKey
= NULL
;
5091 HANDLE ScsiBusKey
= NULL
;
5092 HANDLE ScsiInitiatorKey
= NULL
;
5093 HANDLE ScsiTargetKey
= NULL
;
5094 HANDLE ScsiLunKey
= NULL
;
5097 ULONG CurrentTarget
;
5104 DPRINT("SpiBuildDeviceMap() called\n");
5106 if (DeviceExtension
== NULL
|| RegistryPath
== NULL
)
5108 DPRINT1("Invalid parameter\n");
5109 return(STATUS_INVALID_PARAMETER
);
5112 /* Open or create the 'Scsi' subkey */
5113 RtlInitUnicodeString(&KeyName
,
5114 L
"\\Registry\\Machine\\Hardware\\DeviceMap\\Scsi");
5115 InitializeObjectAttributes(&ObjectAttributes
,
5117 OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
,
5120 Status
= ZwCreateKey(&ScsiKey
,
5125 REG_OPTION_VOLATILE
,
5127 if (!NT_SUCCESS(Status
))
5129 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5133 /* Create new 'Scsi Port X' subkey */
5134 DPRINT("Scsi Port %lu\n",
5135 DeviceExtension
->PortNumber
);
5137 swprintf(NameBuffer
,
5139 DeviceExtension
->PortNumber
);
5140 RtlInitUnicodeString(&KeyName
,
5142 InitializeObjectAttributes(&ObjectAttributes
,
5147 Status
= ZwCreateKey(&ScsiPortKey
,
5152 REG_OPTION_VOLATILE
,
5155 if (!NT_SUCCESS(Status
))
5157 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5162 * Create port-specific values
5165 /* Set 'DMA Enabled' (REG_DWORD) value */
5166 UlongData
= (ULONG
)!DeviceExtension
->PortCapabilities
.AdapterUsesPio
;
5167 DPRINT(" DMA Enabled = %s\n", (UlongData
) ? "TRUE" : "FALSE");
5168 RtlInitUnicodeString(&ValueName
,
5170 Status
= ZwSetValueKey(ScsiPortKey
,
5176 if (!NT_SUCCESS(Status
))
5178 DPRINT("ZwSetValueKey('DMA Enabled') failed (Status %lx)\n", Status
);
5179 ZwClose(ScsiPortKey
);
5183 /* Set 'Driver' (REG_SZ) value */
5184 DriverName
= wcsrchr(RegistryPath
->Buffer
, L
'\\') + 1;
5185 RtlInitUnicodeString(&ValueName
,
5187 Status
= ZwSetValueKey(ScsiPortKey
,
5192 (wcslen(DriverName
) + 1) * sizeof(WCHAR
));
5193 if (!NT_SUCCESS(Status
))
5195 DPRINT("ZwSetValueKey('Driver') failed (Status %lx)\n", Status
);
5196 ZwClose(ScsiPortKey
);
5200 /* Set 'Interrupt' (REG_DWORD) value (NT4 only) */
5201 UlongData
= (ULONG
)DeviceExtension
->PortConfig
->BusInterruptLevel
;
5202 DPRINT(" Interrupt = %lu\n", UlongData
);
5203 RtlInitUnicodeString(&ValueName
,
5205 Status
= ZwSetValueKey(ScsiPortKey
,
5211 if (!NT_SUCCESS(Status
))
5213 DPRINT("ZwSetValueKey('Interrupt') failed (Status %lx)\n", Status
);
5214 ZwClose(ScsiPortKey
);
5218 /* Set 'IOAddress' (REG_DWORD) value (NT4 only) */
5219 UlongData
= ScsiPortConvertPhysicalAddressToUlong((*DeviceExtension
->PortConfig
->AccessRanges
)[0].RangeStart
);
5220 DPRINT(" IOAddress = %lx\n", UlongData
);
5221 RtlInitUnicodeString(&ValueName
,
5223 Status
= ZwSetValueKey(ScsiPortKey
,
5229 if (!NT_SUCCESS(Status
))
5231 DPRINT("ZwSetValueKey('IOAddress') failed (Status %lx)\n", Status
);
5232 ZwClose(ScsiPortKey
);
5236 /* Enumerate buses */
5237 for (BusNumber
= 0; BusNumber
< DeviceExtension
->PortConfig
->NumberOfBuses
; BusNumber
++)
5239 /* Create 'Scsi Bus X' key */
5240 DPRINT(" Scsi Bus %lu\n", BusNumber
);
5241 swprintf(NameBuffer
,
5244 RtlInitUnicodeString(&KeyName
,
5246 InitializeObjectAttributes(&ObjectAttributes
,
5251 Status
= ZwCreateKey(&ScsiBusKey
,
5256 REG_OPTION_VOLATILE
,
5258 if (!NT_SUCCESS(Status
))
5260 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5264 /* Create 'Initiator Id X' key */
5265 DPRINT(" Initiator Id %u\n",
5266 DeviceExtension
->PortConfig
->InitiatorBusId
[BusNumber
]);
5267 swprintf(NameBuffer
,
5269 (unsigned int)(UCHAR
)DeviceExtension
->PortConfig
->InitiatorBusId
[BusNumber
]);
5270 RtlInitUnicodeString(&KeyName
,
5272 InitializeObjectAttributes(&ObjectAttributes
,
5277 Status
= ZwCreateKey(&ScsiInitiatorKey
,
5282 REG_OPTION_VOLATILE
,
5284 if (!NT_SUCCESS(Status
))
5286 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5290 /* FIXME: Are there any initiator values (??) */
5292 ZwClose(ScsiInitiatorKey
);
5293 ScsiInitiatorKey
= NULL
;
5296 /* Enumerate targets */
5297 CurrentTarget
= (ULONG
)-1;
5298 ScsiTargetKey
= NULL
;
5299 for (Target
= 0; Target
< DeviceExtension
->PortConfig
->MaximumNumberOfTargets
; Target
++)
5301 for (Lun
= 0; Lun
< SCSI_MAXIMUM_LOGICAL_UNITS
; Lun
++)
5303 LunExtension
= SpiGetLunExtension(DeviceExtension
,
5307 if (LunExtension
!= NULL
)
5309 if (Target
!= CurrentTarget
)
5311 /* Close old target key */
5312 if (ScsiTargetKey
!= NULL
)
5314 ZwClose(ScsiTargetKey
);
5315 ScsiTargetKey
= NULL
;
5318 /* Create 'Target Id X' key */
5319 DPRINT(" Target Id %lu\n", Target
);
5320 swprintf(NameBuffer
,
5323 RtlInitUnicodeString(&KeyName
,
5325 InitializeObjectAttributes(&ObjectAttributes
,
5330 Status
= ZwCreateKey(&ScsiTargetKey
,
5335 REG_OPTION_VOLATILE
,
5337 if (!NT_SUCCESS(Status
))
5339 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5343 CurrentTarget
= Target
;
5346 /* Create 'Logical Unit Id X' key */
5347 DPRINT(" Logical Unit Id %lu\n", Lun
);
5348 swprintf(NameBuffer
,
5349 L
"Logical Unit Id %lu",
5351 RtlInitUnicodeString(&KeyName
,
5353 InitializeObjectAttributes(&ObjectAttributes
,
5358 Status
= ZwCreateKey(&ScsiLunKey
,
5363 REG_OPTION_VOLATILE
,
5365 if (!NT_SUCCESS(Status
))
5367 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5371 /* Set 'Identifier' (REG_SZ) value */
5372 swprintf(NameBuffer
,
5374 LunExtension
->InquiryData
.VendorId
,
5375 LunExtension
->InquiryData
.ProductId
,
5376 LunExtension
->InquiryData
.ProductRevisionLevel
);
5377 DPRINT(" Identifier = '%S'\n", NameBuffer
);
5378 RtlInitUnicodeString(&ValueName
,
5380 Status
= ZwSetValueKey(ScsiLunKey
,
5385 (wcslen(NameBuffer
) + 1) * sizeof(WCHAR
));
5386 if (!NT_SUCCESS(Status
))
5388 DPRINT("ZwSetValueKey('Identifier') failed (Status %lx)\n", Status
);
5392 /* Set 'Type' (REG_SZ) value */
5393 switch (LunExtension
->InquiryData
.DeviceType
)
5396 TypeName
= L
"DiskPeripheral";
5399 TypeName
= L
"TapePeripheral";
5402 TypeName
= L
"PrinterPeripheral";
5405 TypeName
= L
"WormPeripheral";
5408 TypeName
= L
"CdRomPeripheral";
5411 TypeName
= L
"ScannerPeripheral";
5414 TypeName
= L
"OpticalDiskPeripheral";
5417 TypeName
= L
"MediumChangerPeripheral";
5420 TypeName
= L
"CommunicationPeripheral";
5423 TypeName
= L
"OtherPeripheral";
5426 DPRINT(" Type = '%S'\n", TypeName
);
5427 RtlInitUnicodeString(&ValueName
,
5429 Status
= ZwSetValueKey(ScsiLunKey
,
5434 (wcslen(TypeName
) + 1) * sizeof(WCHAR
));
5435 if (!NT_SUCCESS(Status
))
5437 DPRINT("ZwSetValueKey('Type') failed (Status %lx)\n", Status
);
5441 ZwClose(ScsiLunKey
);
5446 /* Close old target key */
5447 if (ScsiTargetKey
!= NULL
)
5449 ZwClose(ScsiTargetKey
);
5450 ScsiTargetKey
= NULL
;
5454 ZwClose(ScsiBusKey
);
5459 if (ScsiLunKey
!= NULL
)
5460 ZwClose (ScsiLunKey
);
5462 if (ScsiInitiatorKey
!= NULL
)
5463 ZwClose (ScsiInitiatorKey
);
5465 if (ScsiTargetKey
!= NULL
)
5466 ZwClose (ScsiTargetKey
);
5468 if (ScsiBusKey
!= NULL
)
5469 ZwClose (ScsiBusKey
);
5471 if (ScsiPortKey
!= NULL
)
5472 ZwClose (ScsiPortKey
);
5474 DPRINT("SpiBuildDeviceMap() done\n");
5481 SpiMiniportTimerDpc(IN
struct _KDPC
*Dpc
,
5482 IN PVOID DeviceObject
,
5483 IN PVOID SystemArgument1
,
5484 IN PVOID SystemArgument2
)
5486 DPRINT1("Miniport timer DPC\n");
5490 SpiCreatePortConfig(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
5491 PHW_INITIALIZATION_DATA HwInitData
,
5492 PCONFIGURATION_INFO InternalConfigInfo
,
5493 PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
5496 UNICODE_STRING UnicodeString
;
5497 OBJECT_ATTRIBUTES ObjectAttributes
;
5498 PCONFIGURATION_INFORMATION DdkConfigInformation
;
5499 HANDLE RootKey
, Key
;
5501 WCHAR DeviceBuffer
[16];
5502 WCHAR StrBuffer
[512];
5506 /* Zero out the struct if told so */
5509 /* First zero the portconfig */
5510 RtlZeroMemory(ConfigInfo
, sizeof(PORT_CONFIGURATION_INFORMATION
));
5512 /* Then access ranges */
5513 RtlZeroMemory(InternalConfigInfo
->AccessRanges
,
5514 HwInitData
->NumberOfAccessRanges
* sizeof(ACCESS_RANGE
));
5516 /* Initialize the struct */
5517 ConfigInfo
->Length
= sizeof(PORT_CONFIGURATION_INFORMATION
);
5518 ConfigInfo
->AdapterInterfaceType
= HwInitData
->AdapterInterfaceType
;
5519 ConfigInfo
->InterruptMode
= Latched
;
5520 ConfigInfo
->DmaChannel
= SP_UNINITIALIZED_VALUE
;
5521 ConfigInfo
->DmaPort
= SP_UNINITIALIZED_VALUE
;
5522 ConfigInfo
->MaximumTransferLength
= SP_UNINITIALIZED_VALUE
;
5523 ConfigInfo
->NumberOfAccessRanges
= HwInitData
->NumberOfAccessRanges
;
5524 ConfigInfo
->MaximumNumberOfTargets
= 8;
5526 /* Store parameters */
5527 ConfigInfo
->NeedPhysicalAddresses
= HwInitData
->NeedPhysicalAddresses
;
5528 ConfigInfo
->MapBuffers
= HwInitData
->MapBuffers
;
5529 ConfigInfo
->AutoRequestSense
= HwInitData
->AutoRequestSense
;
5530 ConfigInfo
->ReceiveEvent
= HwInitData
->ReceiveEvent
;
5531 ConfigInfo
->TaggedQueuing
= HwInitData
->TaggedQueuing
;
5532 ConfigInfo
->MultipleRequestPerLu
= HwInitData
->MultipleRequestPerLu
;
5534 /* Get the disk usage */
5535 DdkConfigInformation
= IoGetConfigurationInformation();
5536 ConfigInfo
->AtdiskPrimaryClaimed
= DdkConfigInformation
->AtDiskPrimaryAddressClaimed
;
5537 ConfigInfo
->AtdiskSecondaryClaimed
= DdkConfigInformation
->AtDiskSecondaryAddressClaimed
;
5539 /* Initiator bus id is not set */
5540 for (Bus
= 0; Bus
< 8; Bus
++)
5541 ConfigInfo
->InitiatorBusId
[Bus
] = (CCHAR
)SP_UNINITIALIZED_VALUE
;
5544 ConfigInfo
->NumberOfPhysicalBreaks
= 17;
5546 /* Clear this information */
5547 InternalConfigInfo
->DisableTaggedQueueing
= FALSE
;
5548 InternalConfigInfo
->DisableMultipleLun
= FALSE
;
5550 /* Store Bus Number */
5551 ConfigInfo
->SystemIoBusNumber
= InternalConfigInfo
->BusNumber
;
5555 if (ConfigInfo
->AdapterInterfaceType
== Internal
)
5557 /* Open registry key for HW database */
5558 InitializeObjectAttributes(&ObjectAttributes
,
5559 DeviceExtension
->DeviceObject
->DriverObject
->HardwareDatabase
,
5560 OBJ_CASE_INSENSITIVE
,
5564 Status
= ZwOpenKey(&RootKey
,
5568 if (NT_SUCCESS(Status
))
5570 /* Create name for it */
5571 swprintf(StrBuffer
, L
"ScsiAdapter\\%lu",
5572 InternalConfigInfo
->AdapterNumber
);
5574 RtlInitUnicodeString(&UnicodeString
, StrBuffer
);
5576 /* Open device key */
5577 InitializeObjectAttributes(&ObjectAttributes
,
5579 OBJ_CASE_INSENSITIVE
,
5583 Status
= ZwOpenKey(&Key
,
5589 if (NT_SUCCESS(Status
))
5591 if (InternalConfigInfo
->LastAdapterNumber
!= InternalConfigInfo
->AdapterNumber
)
5593 DPRINT("Hardware info found at %S\n", StrBuffer
);
5596 SpiParseDeviceInfo(DeviceExtension
,
5602 InternalConfigInfo
->BusNumber
= 0;
5606 /* Try the next adapter */
5607 InternalConfigInfo
->AdapterNumber
++;
5613 /* Info was not found, exit */
5614 return STATUS_DEVICE_DOES_NOT_EXIST
;
5620 /* Look at device params */
5622 if (InternalConfigInfo
->Parameter
)
5624 ExFreePool(InternalConfigInfo
->Parameter
);
5625 InternalConfigInfo
->Parameter
= NULL
;
5628 if (InternalConfigInfo
->ServiceKey
!= NULL
)
5630 swprintf(DeviceBuffer
, L
"Device%lu", InternalConfigInfo
->AdapterNumber
);
5631 RtlInitUnicodeString(&UnicodeString
, DeviceBuffer
);
5633 /* Open the service key */
5634 InitializeObjectAttributes(&ObjectAttributes
,
5636 OBJ_CASE_INSENSITIVE
,
5637 InternalConfigInfo
->ServiceKey
,
5640 Status
= ZwOpenKey(&Key
,
5645 /* Parse device key */
5646 if (InternalConfigInfo
->DeviceKey
!= NULL
)
5648 SpiParseDeviceInfo(DeviceExtension
,
5649 InternalConfigInfo
->DeviceKey
,
5655 /* Then parse hw info */
5658 if (InternalConfigInfo
->LastAdapterNumber
!= InternalConfigInfo
->AdapterNumber
)
5660 SpiParseDeviceInfo(DeviceExtension
,
5671 /* Adapter not found, go try the next one */
5672 InternalConfigInfo
->AdapterNumber
++;
5681 /* Update the last adapter number */
5682 InternalConfigInfo
->LastAdapterNumber
= InternalConfigInfo
->AdapterNumber
;
5684 /* Do we have this kind of bus at all? */
5686 Status
= IoQueryDeviceDescription(&HwInitData
->AdapterInterfaceType
,
5687 &InternalConfigInfo
->BusNumber
,
5692 SpQueryDeviceCallout
,
5695 /* This bus was not found */
5698 INTERFACE_TYPE InterfaceType
= Eisa
;
5700 /* Check for EISA */
5701 if (HwInitData
->AdapterInterfaceType
== Isa
)
5703 Status
= IoQueryDeviceDescription(&InterfaceType
,
5704 &InternalConfigInfo
->BusNumber
,
5709 SpQueryDeviceCallout
,
5712 /* Return respectively */
5714 return STATUS_SUCCESS
;
5716 return STATUS_DEVICE_DOES_NOT_EXIST
;
5720 return STATUS_DEVICE_DOES_NOT_EXIST
;
5725 return STATUS_SUCCESS
;
5730 SpiParseDeviceInfo(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
5732 IN PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
5733 IN PCONFIGURATION_INFO InternalConfigInfo
,
5736 PKEY_VALUE_FULL_INFORMATION KeyValueInformation
;
5737 PCM_FULL_RESOURCE_DESCRIPTOR FullResource
;
5738 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
5739 PCM_SCSI_DEVICE_DATA ScsiDeviceData
;
5740 ULONG Length
, Count
;
5741 ULONG Index
= 0, RangeCount
= 0;
5742 UNICODE_STRING UnicodeString
;
5743 ANSI_STRING AnsiString
;
5744 NTSTATUS Status
= STATUS_SUCCESS
;
5747 KeyValueInformation
= (PKEY_VALUE_FULL_INFORMATION
) Buffer
;
5749 /* Loop through all values in the device node */
5752 Status
= ZwEnumerateValueKey(Key
,
5754 KeyValueFullInformation
,
5759 if (!NT_SUCCESS(Status
))
5764 /* Length for DWORD is ok? */
5765 if (KeyValueInformation
->Type
== REG_DWORD
&&
5766 KeyValueInformation
->DataLength
!= sizeof(ULONG
))
5771 /* Get MaximumLogicalUnit */
5772 if (_wcsnicmp(KeyValueInformation
->Name
, L
"MaximumLogicalUnit",
5773 KeyValueInformation
->NameLength
/2) == 0)
5776 if (KeyValueInformation
->Type
!= REG_DWORD
)
5778 DPRINT("Bad data type for MaximumLogicalUnit\n");
5782 DeviceExtension
->MaxLunCount
= *((PUCHAR
)
5783 (Buffer
+ KeyValueInformation
->DataOffset
));
5785 /* Check / reset if needed */
5786 if (DeviceExtension
->MaxLunCount
> SCSI_MAXIMUM_LOGICAL_UNITS
)
5787 DeviceExtension
->MaxLunCount
= SCSI_MAXIMUM_LOGICAL_UNITS
;
5789 DPRINT("MaximumLogicalUnit = %d\n", DeviceExtension
->MaxLunCount
);
5792 /* Get InitiatorTargetId */
5793 if (_wcsnicmp(KeyValueInformation
->Name
, L
"InitiatorTargetId",
5794 KeyValueInformation
->NameLength
/ 2) == 0)
5797 if (KeyValueInformation
->Type
!= REG_DWORD
)
5799 DPRINT("Bad data type for InitiatorTargetId\n");
5803 ConfigInfo
->InitiatorBusId
[0] = *((PUCHAR
)
5804 (Buffer
+ KeyValueInformation
->DataOffset
));
5806 /* Check / reset if needed */
5807 if (ConfigInfo
->InitiatorBusId
[0] > ConfigInfo
->MaximumNumberOfTargets
- 1)
5808 ConfigInfo
->InitiatorBusId
[0] = (CCHAR
)-1;
5810 DPRINT("InitiatorTargetId = %d\n", ConfigInfo
->InitiatorBusId
[0]);
5814 if (_wcsnicmp(KeyValueInformation
->Name
, L
"ScsiDebug",
5815 KeyValueInformation
->NameLength
/2) == 0)
5817 DPRINT("ScsiDebug key not supported\n");
5820 /* Check for a breakpoint */
5821 if (_wcsnicmp(KeyValueInformation
->Name
, L
"BreakPointOnEntry",
5822 KeyValueInformation
->NameLength
/2) == 0)
5824 DPRINT1("Breakpoint on entry requested!\n");
5828 /* Get DisableSynchronousTransfers */
5829 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DisableSynchronousTransfers",
5830 KeyValueInformation
->NameLength
/2) == 0)
5832 DeviceExtension
->SrbFlags
|= SRB_FLAGS_DISABLE_SYNCH_TRANSFER
;
5833 DPRINT("Synch transfers disabled\n");
5836 /* Get DisableDisconnects */
5837 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DisableDisconnects",
5838 KeyValueInformation
->NameLength
/2) == 0)
5840 DeviceExtension
->SrbFlags
|= SRB_FLAGS_DISABLE_DISCONNECT
;
5841 DPRINT("Disconnects disabled\n");
5844 /* Get DisableTaggedQueuing */
5845 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DisableTaggedQueuing",
5846 KeyValueInformation
->NameLength
/2) == 0)
5848 InternalConfigInfo
->DisableTaggedQueueing
= TRUE
;
5849 DPRINT("Tagged queueing disabled\n");
5852 /* Get DisableMultipleRequests */
5853 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DisableMultipleRequests",
5854 KeyValueInformation
->NameLength
/2) == 0)
5856 InternalConfigInfo
->DisableMultipleLun
= TRUE
;
5857 DPRINT("Multiple requests disabled\n");
5860 /* Get DriverParameters */
5861 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DriverParameters",
5862 KeyValueInformation
->NameLength
/2) == 0)
5864 /* Skip if nothing */
5865 if (KeyValueInformation
->DataLength
== 0)
5868 /* If there was something previously allocated - free it */
5869 if (InternalConfigInfo
->Parameter
!= NULL
)
5870 ExFreePool(InternalConfigInfo
->Parameter
);
5873 InternalConfigInfo
->Parameter
= ExAllocatePoolWithTag(NonPagedPool
,
5874 KeyValueInformation
->DataLength
, TAG_SCSIPORT
);
5876 if (InternalConfigInfo
->Parameter
!= NULL
)
5878 if (KeyValueInformation
->Type
!= REG_SZ
)
5882 InternalConfigInfo
->Parameter
,
5883 (PCCHAR
)KeyValueInformation
+ KeyValueInformation
->DataOffset
,
5884 KeyValueInformation
->DataLength
);
5888 /* If it's a unicode string, convert it to ansi */
5889 UnicodeString
.Length
= (USHORT
)KeyValueInformation
->DataLength
;
5890 UnicodeString
.MaximumLength
= (USHORT
)KeyValueInformation
->DataLength
;
5891 UnicodeString
.Buffer
=
5892 (PWSTR
)((PCCHAR
)KeyValueInformation
+ KeyValueInformation
->DataOffset
);
5894 AnsiString
.Length
= 0;
5895 AnsiString
.MaximumLength
= (USHORT
)KeyValueInformation
->DataLength
;
5896 AnsiString
.Buffer
= (PCHAR
)InternalConfigInfo
->Parameter
;
5898 Status
= RtlUnicodeStringToAnsiString(&AnsiString
,
5902 /* In case of error, free the allocated space */
5903 if (!NT_SUCCESS(Status
))
5905 ExFreePool(InternalConfigInfo
->Parameter
);
5906 InternalConfigInfo
->Parameter
= NULL
;
5912 DPRINT("Found driver parameter\n");
5915 /* Get MaximumSGList */
5916 if (_wcsnicmp(KeyValueInformation
->Name
, L
"MaximumSGList",
5917 KeyValueInformation
->NameLength
/2) == 0)
5919 if (KeyValueInformation
->Type
!= REG_DWORD
)
5921 DPRINT("Bad data type for MaximumSGList\n");
5925 ConfigInfo
->NumberOfPhysicalBreaks
= *((PUCHAR
)(Buffer
+ KeyValueInformation
->DataOffset
));
5928 if (ConfigInfo
->NumberOfPhysicalBreaks
> SCSI_MAXIMUM_PHYSICAL_BREAKS
)
5930 ConfigInfo
->NumberOfPhysicalBreaks
= SCSI_MAXIMUM_PHYSICAL_BREAKS
;
5932 else if (ConfigInfo
->NumberOfPhysicalBreaks
< SCSI_MINIMUM_PHYSICAL_BREAKS
)
5934 ConfigInfo
->NumberOfPhysicalBreaks
= SCSI_MINIMUM_PHYSICAL_BREAKS
;
5937 DPRINT("MaximumSGList = %d\n", ConfigInfo
->NumberOfPhysicalBreaks
);
5940 /* Get NumberOfRequests */
5941 if (_wcsnicmp(KeyValueInformation
->Name
, L
"NumberOfRequests",
5942 KeyValueInformation
->NameLength
/2) == 0)
5944 if (KeyValueInformation
->Type
!= REG_DWORD
)
5946 DPRINT("NumberOfRequests has wrong data type\n");
5950 DeviceExtension
->RequestsNumber
= *((PUCHAR
)(Buffer
+ KeyValueInformation
->DataOffset
));
5953 if (DeviceExtension
->RequestsNumber
< 16)
5955 DeviceExtension
->RequestsNumber
= 16;
5957 else if (DeviceExtension
->RequestsNumber
> 512)
5959 DeviceExtension
->RequestsNumber
= 512;
5962 DPRINT("Number Of Requests = %d\n", DeviceExtension
->RequestsNumber
);
5965 /* Get resource list */
5966 if (_wcsnicmp(KeyValueInformation
->Name
, L
"ResourceList",
5967 KeyValueInformation
->NameLength
/2) == 0 ||
5968 _wcsnicmp(KeyValueInformation
->Name
, L
"Configuration Data",
5969 KeyValueInformation
->NameLength
/2) == 0 )
5971 if (KeyValueInformation
->Type
!= REG_FULL_RESOURCE_DESCRIPTOR
||
5972 KeyValueInformation
->DataLength
< sizeof(REG_FULL_RESOURCE_DESCRIPTOR
))
5974 DPRINT("Bad data type for ResourceList\n");
5979 DPRINT("Found ResourceList\n");
5982 FullResource
= (PCM_FULL_RESOURCE_DESCRIPTOR
)(Buffer
+ KeyValueInformation
->DataOffset
);
5984 /* Copy some info from it */
5985 InternalConfigInfo
->BusNumber
= FullResource
->BusNumber
;
5986 ConfigInfo
->SystemIoBusNumber
= FullResource
->BusNumber
;
5988 /* Loop through it */
5989 for (Count
= 0; Count
< FullResource
->PartialResourceList
.Count
; Count
++)
5991 /* Get partial descriptor */
5993 &FullResource
->PartialResourceList
.PartialDescriptors
[Count
];
5995 /* Check datalength */
5996 if ((ULONG
)((PCHAR
)(PartialDescriptor
+ 1) -
5997 (PCHAR
)FullResource
) > KeyValueInformation
->DataLength
)
5999 DPRINT("Resource data is of incorrect size\n");
6003 switch (PartialDescriptor
->Type
)
6005 case CmResourceTypePort
:
6006 if (RangeCount
>= ConfigInfo
->NumberOfAccessRanges
)
6008 DPRINT("Too many access ranges\n");
6012 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeInMemory
= FALSE
;
6013 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeStart
= PartialDescriptor
->u
.Port
.Start
;
6014 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeLength
= PartialDescriptor
->u
.Port
.Length
;
6019 case CmResourceTypeMemory
:
6020 if (RangeCount
>= ConfigInfo
->NumberOfAccessRanges
)
6022 DPRINT("Too many access ranges\n");
6026 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeInMemory
= TRUE
;
6027 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeStart
= PartialDescriptor
->u
.Memory
.Start
;
6028 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeLength
= PartialDescriptor
->u
.Memory
.Length
;
6033 case CmResourceTypeInterrupt
:
6034 ConfigInfo
->BusInterruptLevel
=
6035 PartialDescriptor
->u
.Interrupt
.Level
;
6037 ConfigInfo
->BusInterruptVector
=
6038 PartialDescriptor
->u
.Interrupt
.Vector
;
6041 case CmResourceTypeDma
:
6042 ConfigInfo
->DmaChannel
= PartialDescriptor
->u
.Dma
.Channel
;
6043 ConfigInfo
->DmaPort
= PartialDescriptor
->u
.Dma
.Port
;
6046 case CmResourceTypeDeviceSpecific
:
6047 if (PartialDescriptor
->u
.DeviceSpecificData
.DataSize
<
6048 sizeof(CM_SCSI_DEVICE_DATA
) ||
6049 (PCHAR
) (PartialDescriptor
+ 1) - (PCHAR
)FullResource
+
6050 PartialDescriptor
->u
.DeviceSpecificData
.DataSize
>
6051 KeyValueInformation
->DataLength
)
6053 DPRINT("Resource data length is incorrect");
6057 /* Set only one field from it */
6058 ScsiDeviceData
= (PCM_SCSI_DEVICE_DATA
) (PartialDescriptor
+1);
6059 ConfigInfo
->InitiatorBusId
[0] = ScsiDeviceData
->HostIdentifier
;
6070 SpQueryDeviceCallout(IN PVOID Context
,
6071 IN PUNICODE_STRING PathName
,
6072 IN INTERFACE_TYPE BusType
,
6074 IN PKEY_VALUE_FULL_INFORMATION
*BusInformation
,
6075 IN CONFIGURATION_TYPE ControllerType
,
6076 IN ULONG ControllerNumber
,
6077 IN PKEY_VALUE_FULL_INFORMATION
*ControllerInformation
,
6078 IN CONFIGURATION_TYPE PeripheralType
,
6079 IN ULONG PeripheralNumber
,
6080 IN PKEY_VALUE_FULL_INFORMATION
*PeripheralInformation
)
6082 PBOOLEAN Found
= (PBOOLEAN
)Context
;
6083 /* We just set our Found variable to TRUE */
6086 return STATUS_SUCCESS
;
6093 SpiStatusSrbToNt(UCHAR SrbStatus
)
6095 switch (SRB_STATUS(SrbStatus
))
6097 case SRB_STATUS_TIMEOUT
:
6098 case SRB_STATUS_COMMAND_TIMEOUT
:
6099 return STATUS_IO_TIMEOUT
;
6101 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH
:
6102 case SRB_STATUS_BAD_FUNCTION
:
6103 return STATUS_INVALID_DEVICE_REQUEST
;
6105 case SRB_STATUS_NO_DEVICE
:
6106 case SRB_STATUS_INVALID_LUN
:
6107 case SRB_STATUS_INVALID_TARGET_ID
:
6108 case SRB_STATUS_NO_HBA
:
6109 return STATUS_DEVICE_DOES_NOT_EXIST
;
6111 case SRB_STATUS_DATA_OVERRUN
:
6112 return STATUS_BUFFER_OVERFLOW
;
6114 case SRB_STATUS_SELECTION_TIMEOUT
:
6115 return STATUS_DEVICE_NOT_CONNECTED
;
6118 return STATUS_IO_DEVICE_ERROR
;
6121 return STATUS_IO_DEVICE_ERROR
;
6125 #undef ScsiPortConvertPhysicalAddressToUlong
6130 ScsiPortConvertPhysicalAddressToUlong(IN SCSI_PHYSICAL_ADDRESS Address
)
6132 DPRINT("ScsiPortConvertPhysicalAddressToUlong()\n");
6133 return(Address
.u
.LowPart
);