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 along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS Storage Stack
22 * FILE: drivers/storage/scsiport/scsiport.c
23 * PURPOSE: SCSI port driver
24 * PROGRAMMER: Eric Kohl
25 * Aleksey Bragin (aleksey reactos org)
28 /* INCLUDES *****************************************************************/
42 #include "scsiport_int.h"
44 ULONG InternalDebugLevel
= 0x00;
46 #undef ScsiPortMoveMemory
48 /* GLOBALS *******************************************************************/
51 SpiGetPciConfigData(IN PDRIVER_OBJECT DriverObject
,
52 IN PDEVICE_OBJECT DeviceObject
,
53 IN
struct _HW_INITIALIZATION_DATA
*HwInitializationData
,
54 IN OUT PPORT_CONFIGURATION_INFORMATION PortConfig
,
55 IN PUNICODE_STRING RegistryPath
,
57 IN OUT PPCI_SLOT_NUMBER NextSlotNumber
);
60 ScsiPortCreateClose(IN PDEVICE_OBJECT DeviceObject
,
63 static DRIVER_DISPATCH ScsiPortDispatchScsi
;
65 ScsiPortDispatchScsi(IN PDEVICE_OBJECT DeviceObject
,
69 ScsiPortDeviceControl(IN PDEVICE_OBJECT DeviceObject
,
72 static DRIVER_STARTIO ScsiPortStartIo
;
74 ScsiPortStartIo(IN PDEVICE_OBJECT DeviceObject
,
78 ScsiPortStartPacket(IN OUT PVOID Context
);
82 SpiAdapterControl(PDEVICE_OBJECT DeviceObject
, PIRP Irp
,
83 PVOID MapRegisterBase
, PVOID Context
);
85 static PSCSI_PORT_LUN_EXTENSION
86 SpiAllocateLunExtension (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
);
88 static PSCSI_PORT_LUN_EXTENSION
89 SpiGetLunExtension (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
94 static PSCSI_REQUEST_BLOCK_INFO
95 SpiAllocateSrbStructures(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
96 PSCSI_PORT_LUN_EXTENSION LunExtension
,
97 PSCSI_REQUEST_BLOCK Srb
);
100 SpiSendInquiry (IN PDEVICE_OBJECT DeviceObject
,
101 IN PSCSI_LUN_INFO LunInfo
);
104 SpiScanAdapter (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
);
107 SpiGetInquiryData (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
110 static PSCSI_REQUEST_BLOCK_INFO
111 SpiGetSrbData(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
118 ScsiPortIsr(IN PKINTERRUPT Interrupt
,
119 IN PVOID ServiceContext
);
122 ScsiPortDpcForIsr(IN PKDPC Dpc
,
123 IN PDEVICE_OBJECT DpcDeviceObject
,
125 IN PVOID DpcContext
);
128 ScsiPortIoTimer(PDEVICE_OBJECT DeviceObject
,
133 ScsiPortAllocateAdapterChannel(IN PDEVICE_OBJECT DeviceObject
,
135 IN PVOID MapRegisterBase
,
139 SpiBuildDeviceMap (PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
140 PUNICODE_STRING RegistryPath
);
143 SpiStatusSrbToNt(UCHAR SrbStatus
);
146 SpiSendRequestSense(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
147 IN PSCSI_REQUEST_BLOCK Srb
);
149 static IO_COMPLETION_ROUTINE SpiCompletionRoutine
;
151 SpiCompletionRoutine(PDEVICE_OBJECT DeviceObject
,
157 SpiProcessCompletedRequest(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
158 IN PSCSI_REQUEST_BLOCK_INFO SrbInfo
,
159 OUT PBOOLEAN NeedToCallStartIo
);
162 SpiGetNextRequestFromLun(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
163 IN PSCSI_PORT_LUN_EXTENSION LunExtension
);
166 SpiMiniportTimerDpc(IN
struct _KDPC
*Dpc
,
167 IN PVOID DeviceObject
,
168 IN PVOID SystemArgument1
,
169 IN PVOID SystemArgument2
);
172 SpiCreatePortConfig(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
173 PHW_INITIALIZATION_DATA HwInitData
,
174 PCONFIGURATION_INFO InternalConfigInfo
,
175 PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
179 SpQueryDeviceCallout(IN PVOID Context
,
180 IN PUNICODE_STRING PathName
,
181 IN INTERFACE_TYPE BusType
,
183 IN PKEY_VALUE_FULL_INFORMATION
*BusInformation
,
184 IN CONFIGURATION_TYPE ControllerType
,
185 IN ULONG ControllerNumber
,
186 IN PKEY_VALUE_FULL_INFORMATION
*ControllerInformation
,
187 IN CONFIGURATION_TYPE PeripheralType
,
188 IN ULONG PeripheralNumber
,
189 IN PKEY_VALUE_FULL_INFORMATION
*PeripheralInformation
);
192 SpiParseDeviceInfo(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
194 IN PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
195 IN PCONFIGURATION_INFO InternalConfigInfo
,
199 SpiResourceToConfig(IN PHW_INITIALIZATION_DATA HwInitializationData
,
200 IN PCM_FULL_RESOURCE_DESCRIPTOR ResourceDescriptor
,
201 IN PPORT_CONFIGURATION_INFORMATION PortConfig
);
203 static PCM_RESOURCE_LIST
204 SpiConfigToResource(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
205 PPORT_CONFIGURATION_INFORMATION PortConfig
);
208 SpiCleanupAfterInit(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
);
211 SpiHandleAttachRelease(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
215 SpiAllocateCommonBuffer(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
, ULONG NonCachedSize
);
217 NTHALAPI ULONG NTAPI
HalGetBusData(BUS_DATA_TYPE
, ULONG
, ULONG
, PVOID
, ULONG
);
218 NTHALAPI ULONG NTAPI
HalGetInterruptVector(INTERFACE_TYPE
, ULONG
, ULONG
, ULONG
, PKIRQL
, PKAFFINITY
);
219 NTHALAPI NTSTATUS NTAPI
HalAssignSlotResources(PUNICODE_STRING
, PUNICODE_STRING
, PDRIVER_OBJECT
, PDEVICE_OBJECT
, INTERFACE_TYPE
, ULONG
, ULONG
, PCM_RESOURCE_LIST
*);
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
);
297 /* An internal helper function for ScsiPortCompleteRequest */
300 SpiCompleteRequest(IN PVOID HwDeviceExtension
,
301 IN PSCSI_REQUEST_BLOCK_INFO SrbInfo
,
304 PSCSI_REQUEST_BLOCK Srb
;
306 /* Get current SRB */
309 /* Return if there is no SRB or it is not active */
310 if (!Srb
|| !(Srb
->SrbFlags
& SRB_FLAGS_IS_ACTIVE
)) return;
313 Srb
->SrbStatus
= SrbStatus
;
315 /* Set data transfered to 0 */
316 Srb
->DataTransferLength
= 0;
319 ScsiPortNotification(RequestComplete
,
328 ScsiPortCompleteRequest(IN PVOID HwDeviceExtension
,
334 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
335 PSCSI_PORT_LUN_EXTENSION LunExtension
;
336 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
337 PLIST_ENTRY ListEntry
;
341 DPRINT("ScsiPortCompleteRequest() called\n");
343 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
344 SCSI_PORT_DEVICE_EXTENSION
,
345 MiniPortDeviceExtension
);
347 /* Go through all buses */
348 for (BusNumber
= 0; BusNumber
< 8; BusNumber
++)
350 /* Go through all targets */
351 for (Target
= 0; Target
< DeviceExtension
->MaxTargedIds
; Target
++)
353 /* Get logical unit list head */
354 LunExtension
= DeviceExtension
->LunExtensionList
[Target
% 8];
356 /* Go through all logical units */
359 /* Now match what caller asked with what we are at now */
360 if ((PathId
== SP_UNTAGGED
|| PathId
== LunExtension
->PathId
) &&
361 (TargetId
== SP_UNTAGGED
|| TargetId
== LunExtension
->TargetId
) &&
362 (Lun
== SP_UNTAGGED
|| Lun
== LunExtension
->Lun
))
364 /* Yes, that's what caller asked for. Complete abort requests */
365 if (LunExtension
->CompletedAbortRequests
)
367 /* TODO: Save SrbStatus in this request */
368 DPRINT1("Completing abort request without setting SrbStatus!\n");
370 /* Issue a notification request */
371 ScsiPortNotification(RequestComplete
,
373 LunExtension
->CompletedAbortRequests
);
376 /* Complete the request using our helper */
377 SpiCompleteRequest(HwDeviceExtension
,
378 &LunExtension
->SrbInfo
,
381 /* Go through the queue and complete everything there too */
382 ListEntry
= LunExtension
->SrbInfo
.Requests
.Flink
;
383 while (ListEntry
!= &LunExtension
->SrbInfo
.Requests
)
385 /* Get the actual SRB info entry */
386 SrbInfo
= CONTAINING_RECORD(ListEntry
,
387 SCSI_REQUEST_BLOCK_INFO
,
391 SpiCompleteRequest(HwDeviceExtension
,
395 /* Advance to the next request in queue */
396 ListEntry
= SrbInfo
->Requests
.Flink
;
400 /* Advance to the next one */
401 LunExtension
= LunExtension
->Next
;
411 ScsiPortFlushDma(IN PVOID HwDeviceExtension
)
413 DPRINT("ScsiPortFlushDma()\n");
422 ScsiPortFreeDeviceBase(IN PVOID HwDeviceExtension
,
423 IN PVOID MappedAddress
)
425 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
426 PMAPPED_ADDRESS NextMa
, LastMa
;
428 //DPRINT("ScsiPortFreeDeviceBase() called\n");
430 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
431 SCSI_PORT_DEVICE_EXTENSION
,
432 MiniPortDeviceExtension
);
434 /* Initialize our pointers */
435 NextMa
= DeviceExtension
->MappedAddressList
;
440 if (NextMa
->MappedAddress
== MappedAddress
)
443 MmUnmapIoSpace(MappedAddress
, NextMa
->NumberOfBytes
);
445 /* Remove it from the list */
446 if (NextMa
== DeviceExtension
->MappedAddressList
)
448 /* Remove the first entry */
449 DeviceExtension
->MappedAddressList
= NextMa
->NextMappedAddress
;
453 LastMa
->NextMappedAddress
= NextMa
->NextMappedAddress
;
456 /* Free the resources and quit */
464 NextMa
= NextMa
->NextMappedAddress
;
474 ScsiPortGetBusData(IN PVOID DeviceExtension
,
475 IN ULONG BusDataType
,
476 IN ULONG SystemIoBusNumber
,
481 DPRINT("ScsiPortGetBusData()\n");
485 /* If Length is non-zero, just forward the call to
486 HalGetBusData() function */
487 return HalGetBusData(BusDataType
,
494 /* We have a more complex case here */
503 ScsiPortSetBusDataByOffset(IN PVOID DeviceExtension
,
504 IN ULONG BusDataType
,
505 IN ULONG SystemIoBusNumber
,
511 DPRINT("ScsiPortSetBusDataByOffset()\n");
512 return HalSetBusDataByOffset(BusDataType
,
524 ScsiPortGetDeviceBase(IN PVOID HwDeviceExtension
,
525 IN INTERFACE_TYPE BusType
,
526 IN ULONG SystemIoBusNumber
,
527 IN SCSI_PHYSICAL_ADDRESS IoAddress
,
528 IN ULONG NumberOfBytes
,
529 IN BOOLEAN InIoSpace
)
531 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
532 PHYSICAL_ADDRESS TranslatedAddress
;
533 PMAPPED_ADDRESS DeviceBase
;
537 //DPRINT ("ScsiPortGetDeviceBase() called\n");
539 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
540 SCSI_PORT_DEVICE_EXTENSION
,
541 MiniPortDeviceExtension
);
543 AddressSpace
= (ULONG
)InIoSpace
;
544 if (HalTranslateBusAddress(BusType
,
548 &TranslatedAddress
) == FALSE
)
554 if (AddressSpace
!= 0)
555 return((PVOID
)(ULONG_PTR
)TranslatedAddress
.QuadPart
);
557 MappedAddress
= MmMapIoSpace(TranslatedAddress
,
561 DeviceBase
= ExAllocatePoolWithTag(NonPagedPool
,
562 sizeof(MAPPED_ADDRESS
), TAG_SCSIPORT
);
564 if (DeviceBase
== NULL
)
565 return MappedAddress
;
567 DeviceBase
->MappedAddress
= MappedAddress
;
568 DeviceBase
->NumberOfBytes
= NumberOfBytes
;
569 DeviceBase
->IoAddress
= IoAddress
;
570 DeviceBase
->BusNumber
= SystemIoBusNumber
;
572 /* Link it to the Device Extension list */
573 DeviceBase
->NextMappedAddress
= DeviceExtension
->MappedAddressList
;
574 DeviceExtension
->MappedAddressList
= DeviceBase
;
576 return MappedAddress
;
583 ScsiPortGetLogicalUnit(IN PVOID HwDeviceExtension
,
588 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
589 PSCSI_PORT_LUN_EXTENSION LunExtension
;
591 DPRINT("ScsiPortGetLogicalUnit() called\n");
593 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
594 SCSI_PORT_DEVICE_EXTENSION
,
595 MiniPortDeviceExtension
);
597 /* Check the extension size */
598 if (!DeviceExtension
->LunExtensionSize
)
600 /* They didn't want one */
604 LunExtension
= SpiGetLunExtension(DeviceExtension
,
608 /* Check that the logical unit exists */
611 /* Nope, return NULL */
615 /* Return the logical unit miniport extension */
616 return (LunExtension
+ 1);
623 SCSI_PHYSICAL_ADDRESS NTAPI
624 ScsiPortGetPhysicalAddress(IN PVOID HwDeviceExtension
,
625 IN PSCSI_REQUEST_BLOCK Srb OPTIONAL
,
626 IN PVOID VirtualAddress
,
629 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
630 SCSI_PHYSICAL_ADDRESS PhysicalAddress
;
631 SIZE_T BufferLength
= 0;
633 PSCSI_SG_ADDRESS SGList
;
634 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
636 DPRINT("ScsiPortGetPhysicalAddress(%p %p %p %p)\n",
637 HwDeviceExtension
, Srb
, VirtualAddress
, Length
);
639 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
640 SCSI_PORT_DEVICE_EXTENSION
,
641 MiniPortDeviceExtension
);
643 if (Srb
== NULL
|| Srb
->SenseInfoBuffer
== VirtualAddress
)
645 /* Simply look it up in the allocated common buffer */
646 Offset
= (PUCHAR
)VirtualAddress
- (PUCHAR
)DeviceExtension
->SrbExtensionBuffer
;
648 BufferLength
= DeviceExtension
->CommonBufferLength
- Offset
;
649 PhysicalAddress
.QuadPart
= DeviceExtension
->PhysicalAddress
.QuadPart
+ Offset
;
651 else if (DeviceExtension
->MapRegisters
)
653 /* Scatter-gather list must be used */
654 SrbInfo
= SpiGetSrbData(DeviceExtension
,
660 SGList
= SrbInfo
->ScatterGather
;
662 /* Find needed item in the SG list */
663 Offset
= (PCHAR
)VirtualAddress
- (PCHAR
)Srb
->DataBuffer
;
664 while (Offset
>= SGList
->Length
)
666 Offset
-= SGList
->Length
;
670 /* We're done, store length and physical address */
671 BufferLength
= SGList
->Length
- Offset
;
672 PhysicalAddress
.QuadPart
= SGList
->PhysicalAddress
.QuadPart
+ Offset
;
677 PhysicalAddress
.QuadPart
= (LONGLONG
)(SP_UNINITIALIZED_VALUE
);
680 *Length
= (ULONG
)BufferLength
;
681 return PhysicalAddress
;
688 PSCSI_REQUEST_BLOCK NTAPI
689 ScsiPortGetSrb(IN PVOID DeviceExtension
,
695 DPRINT1("ScsiPortGetSrb() unimplemented\n");
705 ScsiPortGetUncachedExtension(IN PVOID HwDeviceExtension
,
706 IN PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
707 IN ULONG NumberOfBytes
)
709 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
710 DEVICE_DESCRIPTION DeviceDescription
;
711 ULONG MapRegistersCount
;
714 DPRINT("ScsiPortGetUncachedExtension(%p %p %lu)\n",
715 HwDeviceExtension
, ConfigInfo
, NumberOfBytes
);
717 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
718 SCSI_PORT_DEVICE_EXTENSION
,
719 MiniPortDeviceExtension
);
721 /* Check for allocated common DMA buffer */
722 if (DeviceExtension
->SrbExtensionBuffer
!= NULL
)
724 DPRINT1("The HBA has already got a common DMA buffer!\n");
728 /* Check for DMA adapter object */
729 if (DeviceExtension
->AdapterObject
== NULL
)
731 /* Initialize DMA adapter description */
732 RtlZeroMemory(&DeviceDescription
, sizeof(DEVICE_DESCRIPTION
));
734 DeviceDescription
.Version
= DEVICE_DESCRIPTION_VERSION
;
735 DeviceDescription
.Master
= ConfigInfo
->Master
;
736 DeviceDescription
.ScatterGather
= ConfigInfo
->ScatterGather
;
737 DeviceDescription
.DemandMode
= ConfigInfo
->DemandMode
;
738 DeviceDescription
.Dma32BitAddresses
= ConfigInfo
->Dma32BitAddresses
;
739 DeviceDescription
.BusNumber
= ConfigInfo
->SystemIoBusNumber
;
740 DeviceDescription
.DmaChannel
= ConfigInfo
->DmaChannel
;
741 DeviceDescription
.InterfaceType
= ConfigInfo
->AdapterInterfaceType
;
742 DeviceDescription
.DmaWidth
= ConfigInfo
->DmaWidth
;
743 DeviceDescription
.DmaSpeed
= ConfigInfo
->DmaSpeed
;
744 DeviceDescription
.MaximumLength
= ConfigInfo
->MaximumTransferLength
;
745 DeviceDescription
.DmaPort
= ConfigInfo
->DmaPort
;
747 /* Get a DMA adapter object */
748 DeviceExtension
->AdapterObject
=
749 HalGetAdapter(&DeviceDescription
, &MapRegistersCount
);
751 /* Fail in case of error */
752 if (DeviceExtension
->AdapterObject
== NULL
)
754 DPRINT1("HalGetAdapter() failed\n");
758 /* Set number of physical breaks */
759 if (ConfigInfo
->NumberOfPhysicalBreaks
!= 0 &&
760 MapRegistersCount
> ConfigInfo
->NumberOfPhysicalBreaks
)
762 DeviceExtension
->PortCapabilities
.MaximumPhysicalPages
=
763 ConfigInfo
->NumberOfPhysicalBreaks
;
767 DeviceExtension
->PortCapabilities
.MaximumPhysicalPages
= MapRegistersCount
;
771 /* Update auto request sense feature */
772 DeviceExtension
->SupportsAutoSense
= ConfigInfo
->AutoRequestSense
;
774 /* Update Srb extension size */
775 if (DeviceExtension
->SrbExtensionSize
!= ConfigInfo
->SrbExtensionSize
)
776 DeviceExtension
->SrbExtensionSize
= ConfigInfo
->SrbExtensionSize
;
778 /* Update Srb extension alloc flag */
779 if (ConfigInfo
->AutoRequestSense
|| DeviceExtension
->SrbExtensionSize
)
780 DeviceExtension
->NeedSrbExtensionAlloc
= TRUE
;
782 /* Allocate a common DMA buffer */
783 Status
= SpiAllocateCommonBuffer(DeviceExtension
, NumberOfBytes
);
785 if (!NT_SUCCESS(Status
))
787 DPRINT1("SpiAllocateCommonBuffer() failed with Status = 0x%08X!\n", Status
);
791 return DeviceExtension
->NonCachedExtension
;
795 SpiAllocateCommonBuffer(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
, ULONG NonCachedSize
)
797 PVOID
*SrbExtension
, CommonBuffer
;
798 ULONG CommonBufferLength
, BufSize
;
800 /* If size is 0, set it to 16 */
801 if (!DeviceExtension
->SrbExtensionSize
)
802 DeviceExtension
->SrbExtensionSize
= 16;
805 BufSize
= DeviceExtension
->SrbExtensionSize
;
807 /* Add autosense data size if needed */
808 if (DeviceExtension
->SupportsAutoSense
)
809 BufSize
+= sizeof(SENSE_DATA
);
813 BufSize
= (BufSize
+ sizeof(LONGLONG
) - 1) & ~(sizeof(LONGLONG
) - 1);
815 /* Sum up into the total common buffer length, and round it to page size */
817 ROUND_TO_PAGES(NonCachedSize
+ BufSize
* DeviceExtension
->RequestsNumber
);
820 if (!DeviceExtension
->AdapterObject
)
822 /* From nonpaged pool if there is no DMA */
823 CommonBuffer
= ExAllocatePoolWithTag(NonPagedPool
, CommonBufferLength
, TAG_SCSIPORT
);
827 /* Perform a full request since we have a DMA adapter*/
828 CommonBuffer
= HalAllocateCommonBuffer(DeviceExtension
->AdapterObject
,
830 &DeviceExtension
->PhysicalAddress
,
834 /* Fail in case of error */
836 return STATUS_INSUFFICIENT_RESOURCES
;
839 RtlZeroMemory(CommonBuffer
, CommonBufferLength
);
841 /* Store its size in Device Extension */
842 DeviceExtension
->CommonBufferLength
= CommonBufferLength
;
844 /* SrbExtension buffer is located at the beginning of the buffer */
845 DeviceExtension
->SrbExtensionBuffer
= CommonBuffer
;
847 /* Non-cached extension buffer is located at the end of
851 CommonBufferLength
-= NonCachedSize
;
852 DeviceExtension
->NonCachedExtension
= (PUCHAR
)CommonBuffer
+ CommonBufferLength
;
856 DeviceExtension
->NonCachedExtension
= NULL
;
859 if (DeviceExtension
->NeedSrbExtensionAlloc
)
861 /* Look up how many SRB data structures we need */
862 DeviceExtension
->SrbDataCount
= CommonBufferLength
/ BufSize
;
864 /* Initialize the free SRB extensions list */
865 SrbExtension
= (PVOID
*)CommonBuffer
;
866 DeviceExtension
->FreeSrbExtensions
= SrbExtension
;
868 /* Fill the remaining pointers (if we have more than 1 SRB) */
869 while (CommonBufferLength
>= 2 * BufSize
)
871 *SrbExtension
= (PVOID
*)((PCHAR
)SrbExtension
+ BufSize
);
872 SrbExtension
= *SrbExtension
;
874 CommonBufferLength
-= BufSize
;
878 return STATUS_SUCCESS
;
887 ScsiPortGetVirtualAddress(IN PVOID HwDeviceExtension
,
888 IN SCSI_PHYSICAL_ADDRESS PhysicalAddress
)
890 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
893 DPRINT("ScsiPortGetVirtualAddress(%p %I64x)\n",
894 HwDeviceExtension
, PhysicalAddress
.QuadPart
);
896 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
897 SCSI_PORT_DEVICE_EXTENSION
,
898 MiniPortDeviceExtension
);
900 if (DeviceExtension
->PhysicalAddress
.QuadPart
> PhysicalAddress
.QuadPart
)
903 Offset
= (ULONG
)(PhysicalAddress
.QuadPart
- DeviceExtension
->PhysicalAddress
.QuadPart
);
905 if (Offset
>= DeviceExtension
->CommonBufferLength
)
908 return (PVOID
)((ULONG_PTR
)DeviceExtension
->SrbExtensionBuffer
+ Offset
);
912 SpiInitOpenKeys(PCONFIGURATION_INFO ConfigInfo
, PUNICODE_STRING RegistryPath
)
914 OBJECT_ATTRIBUTES ObjectAttributes
;
915 UNICODE_STRING KeyName
;
918 /* Open the service key */
919 InitializeObjectAttributes(&ObjectAttributes
,
921 OBJ_CASE_INSENSITIVE
,
925 Status
= ZwOpenKey(&ConfigInfo
->ServiceKey
,
929 if (!NT_SUCCESS(Status
))
931 DPRINT("Unable to open driver's registry key %wZ, status 0x%08x\n", RegistryPath
, Status
);
932 ConfigInfo
->ServiceKey
= NULL
;
935 /* If we could open driver's service key, then proceed to the Parameters key */
936 if (ConfigInfo
->ServiceKey
!= NULL
)
938 RtlInitUnicodeString(&KeyName
, L
"Parameters");
939 InitializeObjectAttributes(&ObjectAttributes
,
941 OBJ_CASE_INSENSITIVE
,
942 ConfigInfo
->ServiceKey
,
943 (PSECURITY_DESCRIPTOR
) NULL
);
946 Status
= ZwOpenKey(&ConfigInfo
->DeviceKey
,
950 if (NT_SUCCESS(Status
))
952 /* Yes, Parameters key exist, and it must be used instead of
954 ZwClose(ConfigInfo
->ServiceKey
);
955 ConfigInfo
->ServiceKey
= ConfigInfo
->DeviceKey
;
956 ConfigInfo
->DeviceKey
= NULL
;
960 if (ConfigInfo
->ServiceKey
!= NULL
)
962 /* Open the Device key */
963 RtlInitUnicodeString(&KeyName
, L
"Device");
964 InitializeObjectAttributes(&ObjectAttributes
,
966 OBJ_CASE_INSENSITIVE
,
967 ConfigInfo
->ServiceKey
,
970 /* We don't check for failure here - not needed */
971 ZwOpenKey(&ConfigInfo
->DeviceKey
,
978 /**********************************************************************
983 * Initializes SCSI port driver specific data.
990 * Pointer to the miniport driver's driver object.
993 * Pointer to the miniport driver's registry path.
995 * HwInitializationData
996 * Pointer to port driver specific configuration data.
999 Miniport driver specific context.
1008 ScsiPortInitialize(IN PVOID Argument1
,
1010 IN
struct _HW_INITIALIZATION_DATA
*HwInitializationData
,
1013 PDRIVER_OBJECT DriverObject
= (PDRIVER_OBJECT
)Argument1
;
1014 PUNICODE_STRING RegistryPath
= (PUNICODE_STRING
)Argument2
;
1015 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
= NULL
;
1016 PCONFIGURATION_INFORMATION SystemConfig
;
1017 PPORT_CONFIGURATION_INFORMATION PortConfig
;
1018 PORT_CONFIGURATION_INFORMATION InitialPortConfig
;
1019 CONFIGURATION_INFO ConfigInfo
;
1020 ULONG DeviceExtensionSize
;
1021 ULONG PortConfigSize
;
1023 BOOLEAN DeviceFound
= FALSE
;
1024 BOOLEAN FirstConfigCall
= TRUE
;
1028 PCI_SLOT_NUMBER SlotNumber
;
1030 PDEVICE_OBJECT PortDeviceObject
;
1031 WCHAR NameBuffer
[80];
1032 UNICODE_STRING DeviceName
;
1033 WCHAR DosNameBuffer
[80];
1034 UNICODE_STRING DosDeviceName
;
1035 PIO_SCSI_CAPABILITIES PortCapabilities
;
1038 PCM_RESOURCE_LIST ResourceList
;
1040 SIZE_T BusConfigSize
;
1043 DPRINT ("ScsiPortInitialize() called!\n");
1045 /* Check params for validity */
1046 if ((HwInitializationData
->HwInitialize
== NULL
) ||
1047 (HwInitializationData
->HwStartIo
== NULL
) ||
1048 (HwInitializationData
->HwInterrupt
== NULL
) ||
1049 (HwInitializationData
->HwFindAdapter
== NULL
) ||
1050 (HwInitializationData
->HwResetBus
== NULL
))
1052 return STATUS_INVALID_PARAMETER
;
1056 DriverObject
->DriverStartIo
= ScsiPortStartIo
;
1057 DriverObject
->MajorFunction
[IRP_MJ_CREATE
] = ScsiPortCreateClose
;
1058 DriverObject
->MajorFunction
[IRP_MJ_CLOSE
] = ScsiPortCreateClose
;
1059 DriverObject
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = ScsiPortDeviceControl
;
1060 DriverObject
->MajorFunction
[IRP_MJ_SCSI
] = ScsiPortDispatchScsi
;
1062 /* Obtain configuration information */
1063 SystemConfig
= IoGetConfigurationInformation();
1065 /* Zero the internal configuration info structure */
1066 RtlZeroMemory(&ConfigInfo
, sizeof(CONFIGURATION_INFO
));
1068 /* Zero starting slot number */
1069 SlotNumber
.u
.AsULONG
= 0;
1071 /* Allocate space for access ranges */
1072 if (HwInitializationData
->NumberOfAccessRanges
)
1074 ConfigInfo
.AccessRanges
=
1075 ExAllocatePoolWithTag(PagedPool
,
1076 HwInitializationData
->NumberOfAccessRanges
* sizeof(ACCESS_RANGE
), TAG_SCSIPORT
);
1078 /* Fail if failed */
1079 if (ConfigInfo
.AccessRanges
== NULL
)
1080 return STATUS_INSUFFICIENT_RESOURCES
;
1083 /* Open registry keys */
1084 SpiInitOpenKeys(&ConfigInfo
, (PUNICODE_STRING
)Argument2
);
1086 /* Last adapter number = not known */
1087 ConfigInfo
.LastAdapterNumber
= SP_UNINITIALIZED_VALUE
;
1089 /* Calculate sizes of DeviceExtension and PortConfig */
1090 DeviceExtensionSize
= sizeof(SCSI_PORT_DEVICE_EXTENSION
) +
1091 HwInitializationData
->DeviceExtensionSize
;
1093 MaxBus
= (HwInitializationData
->AdapterInterfaceType
== PCIBus
) ? 8 : 1;
1094 DPRINT("MaxBus: %lu\n", MaxBus
);
1098 /* Create a unicode device name */
1099 swprintf(NameBuffer
,
1100 L
"\\Device\\ScsiPort%lu",
1101 SystemConfig
->ScsiPortCount
);
1102 RtlInitUnicodeString(&DeviceName
, NameBuffer
);
1104 DPRINT("Creating device: %wZ\n", &DeviceName
);
1106 /* Create the port device */
1107 Status
= IoCreateDevice(DriverObject
,
1108 DeviceExtensionSize
,
1110 FILE_DEVICE_CONTROLLER
,
1115 if (!NT_SUCCESS(Status
))
1117 DPRINT1("IoCreateDevice call failed! (Status 0x%lX)\n", Status
);
1118 PortDeviceObject
= NULL
;
1122 DPRINT ("Created device: %wZ (%p)\n", &DeviceName
, PortDeviceObject
);
1124 /* Set the buffering strategy here... */
1125 PortDeviceObject
->Flags
|= DO_DIRECT_IO
;
1126 PortDeviceObject
->AlignmentRequirement
= FILE_WORD_ALIGNMENT
; /* FIXME: Is this really needed? */
1128 /* Fill Device Extension */
1129 DeviceExtension
= PortDeviceObject
->DeviceExtension
;
1130 RtlZeroMemory(DeviceExtension
, DeviceExtensionSize
);
1131 DeviceExtension
->Length
= DeviceExtensionSize
;
1132 DeviceExtension
->DeviceObject
= PortDeviceObject
;
1133 DeviceExtension
->PortNumber
= SystemConfig
->ScsiPortCount
;
1135 /* Driver's routines... */
1136 DeviceExtension
->HwInitialize
= HwInitializationData
->HwInitialize
;
1137 DeviceExtension
->HwStartIo
= HwInitializationData
->HwStartIo
;
1138 DeviceExtension
->HwInterrupt
= HwInitializationData
->HwInterrupt
;
1139 DeviceExtension
->HwResetBus
= HwInitializationData
->HwResetBus
;
1140 DeviceExtension
->HwDmaStarted
= HwInitializationData
->HwDmaStarted
;
1142 /* Extensions sizes */
1143 DeviceExtension
->MiniPortExtensionSize
= HwInitializationData
->DeviceExtensionSize
;
1144 DeviceExtension
->LunExtensionSize
= HwInitializationData
->SpecificLuExtensionSize
;
1145 DeviceExtension
->SrbExtensionSize
= HwInitializationData
->SrbExtensionSize
;
1147 /* Round Srb extension size to the quadword */
1148 DeviceExtension
->SrbExtensionSize
=
1149 ~(sizeof(LONGLONG
) - 1) & (DeviceExtension
->SrbExtensionSize
+
1150 sizeof(LONGLONG
) - 1);
1152 /* Fill some numbers (bus count, lun count, etc) */
1153 DeviceExtension
->MaxLunCount
= SCSI_MAXIMUM_LOGICAL_UNITS
;
1154 DeviceExtension
->RequestsNumber
= 16;
1156 /* Initialize the spin lock in the controller extension */
1157 KeInitializeSpinLock(&DeviceExtension
->IrqLock
);
1158 KeInitializeSpinLock(&DeviceExtension
->SpinLock
);
1160 /* Initialize the DPC object */
1161 IoInitializeDpcRequest(PortDeviceObject
,
1164 /* Initialize the device timer */
1165 DeviceExtension
->TimerCount
= -1;
1166 IoInitializeTimer(PortDeviceObject
,
1170 /* Initialize miniport timer */
1171 KeInitializeTimer(&DeviceExtension
->MiniportTimer
);
1172 KeInitializeDpc(&DeviceExtension
->MiniportTimerDpc
,
1173 SpiMiniportTimerDpc
,
1178 Status
= SpiCreatePortConfig(DeviceExtension
,
1179 HwInitializationData
,
1184 if (!NT_SUCCESS(Status
))
1186 DPRINT("SpiCreatePortConfig() failed with Status 0x%08X\n", Status
);
1190 /* Allocate and initialize port configuration info */
1191 PortConfigSize
= (sizeof(PORT_CONFIGURATION_INFORMATION
) +
1192 HwInitializationData
->NumberOfAccessRanges
*
1193 sizeof(ACCESS_RANGE
) + 7) & ~7;
1194 DeviceExtension
->PortConfig
= ExAllocatePoolWithTag(NonPagedPool
, PortConfigSize
, TAG_SCSIPORT
);
1196 /* Fail if failed */
1197 if (DeviceExtension
->PortConfig
== NULL
)
1199 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1203 PortConfig
= DeviceExtension
->PortConfig
;
1205 /* Copy information here */
1206 RtlCopyMemory(PortConfig
,
1208 sizeof(PORT_CONFIGURATION_INFORMATION
));
1211 /* Copy extension sizes into the PortConfig */
1212 PortConfig
->SpecificLuExtensionSize
= DeviceExtension
->LunExtensionSize
;
1213 PortConfig
->SrbExtensionSize
= DeviceExtension
->SrbExtensionSize
;
1215 /* Initialize Access ranges */
1216 if (HwInitializationData
->NumberOfAccessRanges
!= 0)
1218 PortConfig
->AccessRanges
= (PVOID
)(PortConfig
+1);
1220 /* Align to LONGLONG */
1221 PortConfig
->AccessRanges
= (PVOID
)((ULONG_PTR
)(PortConfig
->AccessRanges
) + 7);
1222 PortConfig
->AccessRanges
= (PVOID
)((ULONG_PTR
)(PortConfig
->AccessRanges
) & ~7);
1225 RtlCopyMemory(PortConfig
->AccessRanges
,
1226 ConfigInfo
.AccessRanges
,
1227 HwInitializationData
->NumberOfAccessRanges
* sizeof(ACCESS_RANGE
));
1230 /* Search for matching PCI device */
1231 if ((HwInitializationData
->AdapterInterfaceType
== PCIBus
) &&
1232 (HwInitializationData
->VendorIdLength
> 0) &&
1233 (HwInitializationData
->VendorId
!= NULL
) &&
1234 (HwInitializationData
->DeviceIdLength
> 0) &&
1235 (HwInitializationData
->DeviceId
!= NULL
))
1237 PortConfig
->BusInterruptLevel
= 0;
1239 /* Get PCI device data */
1240 DPRINT("VendorId '%.*s' DeviceId '%.*s'\n",
1241 HwInitializationData
->VendorIdLength
,
1242 HwInitializationData
->VendorId
,
1243 HwInitializationData
->DeviceIdLength
,
1244 HwInitializationData
->DeviceId
);
1246 if (!SpiGetPciConfigData(DriverObject
,
1248 HwInitializationData
,
1251 ConfigInfo
.BusNumber
,
1254 /* Continue to the next bus, nothing here */
1255 ConfigInfo
.BusNumber
++;
1256 DeviceExtension
->PortConfig
= NULL
;
1257 ExFreePool(PortConfig
);
1259 goto CreatePortConfig
;
1262 if (!PortConfig
->BusInterruptLevel
)
1264 /* Bypass this slot, because no interrupt was assigned */
1265 DeviceExtension
->PortConfig
= NULL
;
1266 ExFreePool(PortConfig
);
1267 goto CreatePortConfig
;
1272 DPRINT("Non-pci bus\n");
1275 /* Note: HwFindAdapter is called once for each bus */
1277 DPRINT("Calling HwFindAdapter() for Bus %lu\n", PortConfig
->SystemIoBusNumber
);
1278 Result
= (HwInitializationData
->HwFindAdapter
)(&DeviceExtension
->MiniPortDeviceExtension
,
1280 0, /* BusInformation */
1281 ConfigInfo
.Parameter
, /* ArgumentString */
1285 DPRINT("HwFindAdapter() Result: %lu Again: %s\n",
1286 Result
, (Again
) ? "True" : "False");
1288 /* Free MapRegisterBase, it's not needed anymore */
1289 if (DeviceExtension
->MapRegisterBase
!= NULL
)
1291 ExFreePool(DeviceExtension
->MapRegisterBase
);
1292 DeviceExtension
->MapRegisterBase
= NULL
;
1295 /* If result is nothing good... */
1296 if (Result
!= SP_RETURN_FOUND
)
1298 DPRINT("HwFindAdapter() Result: %lu\n", Result
);
1300 if (Result
== SP_RETURN_NOT_FOUND
)
1302 /* We can continue on the next bus */
1303 ConfigInfo
.BusNumber
++;
1306 DeviceExtension
->PortConfig
= NULL
;
1307 ExFreePool(PortConfig
);
1308 goto CreatePortConfig
;
1311 /* Otherwise, break */
1312 Status
= STATUS_INTERNAL_ERROR
;
1316 DPRINT("ScsiPortInitialize(): Found HBA! (%x), adapter Id %d\n",
1317 PortConfig
->BusInterruptVector
, PortConfig
->InitiatorBusId
[0]);
1319 /* If the SRB extension size was updated */
1320 if (!DeviceExtension
->NonCachedExtension
&&
1321 (PortConfig
->SrbExtensionSize
!= DeviceExtension
->SrbExtensionSize
))
1323 /* Set it (rounding to LONGLONG again) */
1324 DeviceExtension
->SrbExtensionSize
=
1325 (PortConfig
->SrbExtensionSize
+
1326 sizeof(LONGLONG
)) & ~(sizeof(LONGLONG
) - 1);
1329 /* The same with LUN extension size */
1330 if (PortConfig
->SpecificLuExtensionSize
!= DeviceExtension
->LunExtensionSize
)
1331 DeviceExtension
->LunExtensionSize
= PortConfig
->SpecificLuExtensionSize
;
1334 if (!((HwInitializationData
->AdapterInterfaceType
== PCIBus
) &&
1335 (HwInitializationData
->VendorIdLength
> 0) &&
1336 (HwInitializationData
->VendorId
!= NULL
) &&
1337 (HwInitializationData
->DeviceIdLength
> 0) &&
1338 (HwInitializationData
->DeviceId
!= NULL
)))
1340 /* Construct a resource list */
1341 ResourceList
= SpiConfigToResource(DeviceExtension
,
1346 UNICODE_STRING UnicodeString
;
1347 RtlInitUnicodeString(&UnicodeString
, L
"ScsiAdapter");
1348 DPRINT("Reporting resources\n");
1349 Status
= IoReportResourceUsage(&UnicodeString
,
1355 FIELD_OFFSET(CM_RESOURCE_LIST
,
1356 List
[0].PartialResourceList
.PartialDescriptors
) +
1357 ResourceList
->List
[0].PartialResourceList
.Count
1358 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
),
1361 ExFreePool(ResourceList
);
1363 /* In case of a failure or a conflict, break */
1364 if (Conflict
|| (!NT_SUCCESS(Status
)))
1367 Status
= STATUS_CONFLICTING_ADDRESSES
;
1373 /* Reset the Conflict var */
1376 /* Copy all stuff which we ever need from PortConfig to the DeviceExtension */
1377 if (PortConfig
->MaximumNumberOfTargets
> SCSI_MAXIMUM_TARGETS_PER_BUS
)
1378 DeviceExtension
->MaxTargedIds
= SCSI_MAXIMUM_TARGETS_PER_BUS
;
1380 DeviceExtension
->MaxTargedIds
= PortConfig
->MaximumNumberOfTargets
;
1382 DeviceExtension
->BusNum
= PortConfig
->NumberOfBuses
;
1383 DeviceExtension
->CachesData
= PortConfig
->CachesData
;
1384 DeviceExtension
->ReceiveEvent
= PortConfig
->ReceiveEvent
;
1385 DeviceExtension
->SupportsTaggedQueuing
= PortConfig
->TaggedQueuing
;
1386 DeviceExtension
->MultipleReqsPerLun
= PortConfig
->MultipleRequestPerLu
;
1388 /* If something was disabled via registry - apply it */
1389 if (ConfigInfo
.DisableMultipleLun
)
1390 DeviceExtension
->MultipleReqsPerLun
= PortConfig
->MultipleRequestPerLu
= FALSE
;
1392 if (ConfigInfo
.DisableTaggedQueueing
)
1393 DeviceExtension
->SupportsTaggedQueuing
= PortConfig
->MultipleRequestPerLu
= FALSE
;
1395 /* Check if we need to alloc SRB data */
1396 if (DeviceExtension
->SupportsTaggedQueuing
||
1397 DeviceExtension
->MultipleReqsPerLun
)
1399 DeviceExtension
->NeedSrbDataAlloc
= TRUE
;
1403 DeviceExtension
->NeedSrbDataAlloc
= FALSE
;
1406 /* Get a pointer to the port capabilities */
1407 PortCapabilities
= &DeviceExtension
->PortCapabilities
;
1409 /* Copy one field there */
1410 DeviceExtension
->MapBuffers
= PortConfig
->MapBuffers
;
1411 PortCapabilities
->AdapterUsesPio
= PortConfig
->MapBuffers
;
1413 if (DeviceExtension
->AdapterObject
== NULL
&&
1414 (PortConfig
->DmaChannel
!= SP_UNINITIALIZED_VALUE
|| PortConfig
->Master
))
1416 DPRINT1("DMA is not supported yet\n");
1420 if (DeviceExtension
->SrbExtensionBuffer
== NULL
&&
1421 (DeviceExtension
->SrbExtensionSize
!= 0 ||
1422 PortConfig
->AutoRequestSense
))
1424 DeviceExtension
->SupportsAutoSense
= PortConfig
->AutoRequestSense
;
1425 DeviceExtension
->NeedSrbExtensionAlloc
= TRUE
;
1427 /* Allocate common buffer */
1428 Status
= SpiAllocateCommonBuffer(DeviceExtension
, 0);
1430 /* Check for failure */
1431 if (!NT_SUCCESS(Status
))
1435 /* Allocate SrbData, if needed */
1436 if (DeviceExtension
->NeedSrbDataAlloc
)
1439 PSCSI_REQUEST_BLOCK_INFO SrbData
;
1441 if (DeviceExtension
->SrbDataCount
!= 0)
1442 Count
= DeviceExtension
->SrbDataCount
;
1444 Count
= DeviceExtension
->RequestsNumber
* 2;
1446 /* Allocate the data */
1447 SrbData
= ExAllocatePoolWithTag(NonPagedPool
, Count
* sizeof(SCSI_REQUEST_BLOCK_INFO
), TAG_SCSIPORT
);
1448 if (SrbData
== NULL
)
1449 return STATUS_INSUFFICIENT_RESOURCES
;
1451 RtlZeroMemory(SrbData
, Count
* sizeof(SCSI_REQUEST_BLOCK_INFO
));
1453 DeviceExtension
->SrbInfo
= SrbData
;
1454 DeviceExtension
->FreeSrbInfo
= SrbData
;
1455 DeviceExtension
->SrbDataCount
= Count
;
1457 /* Link it to the list */
1460 SrbData
->Requests
.Flink
= (PLIST_ENTRY
)(SrbData
+ 1);
1465 /* Mark the last entry of the list */
1467 SrbData
->Requests
.Flink
= NULL
;
1470 /* Initialize port capabilities */
1471 PortCapabilities
= &DeviceExtension
->PortCapabilities
;
1472 PortCapabilities
->Length
= sizeof(IO_SCSI_CAPABILITIES
);
1473 PortCapabilities
->MaximumTransferLength
= PortConfig
->MaximumTransferLength
;
1475 if (PortConfig
->ReceiveEvent
)
1476 PortCapabilities
->SupportedAsynchronousEvents
|= SRBEV_SCSI_ASYNC_NOTIFICATION
;
1478 PortCapabilities
->TaggedQueuing
= DeviceExtension
->SupportsTaggedQueuing
;
1479 PortCapabilities
->AdapterScansDown
= PortConfig
->AdapterScansDown
;
1481 if (PortConfig
->AlignmentMask
> PortDeviceObject
->AlignmentRequirement
)
1482 PortDeviceObject
->AlignmentRequirement
= PortConfig
->AlignmentMask
;
1484 PortCapabilities
->AlignmentMask
= PortDeviceObject
->AlignmentRequirement
;
1486 if (PortCapabilities
->MaximumPhysicalPages
== 0)
1488 PortCapabilities
->MaximumPhysicalPages
=
1489 BYTES_TO_PAGES(PortCapabilities
->MaximumTransferLength
);
1491 /* Apply miniport's limits */
1492 if (PortConfig
->NumberOfPhysicalBreaks
< PortCapabilities
->MaximumPhysicalPages
)
1494 PortCapabilities
->MaximumPhysicalPages
= PortConfig
->NumberOfPhysicalBreaks
;
1498 /* Deal with interrupts */
1499 if (DeviceExtension
->HwInterrupt
== NULL
||
1500 (PortConfig
->BusInterruptLevel
== 0 && PortConfig
->BusInterruptVector
== 0))
1503 DeviceExtension
->InterruptCount
= 0;
1505 DPRINT1("Interrupt Count: 0\n");
1509 /* This code path will ALWAYS crash so stop it now */
1514 BOOLEAN InterruptShareable
;
1515 KINTERRUPT_MODE InterruptMode
[2];
1516 ULONG InterruptVector
[2], i
, MappedIrq
[2];
1517 KIRQL Dirql
[2], MaxDirql
;
1518 KAFFINITY Affinity
[2];
1520 DeviceExtension
->InterruptLevel
[0] = PortConfig
->BusInterruptLevel
;
1521 DeviceExtension
->InterruptLevel
[1] = PortConfig
->BusInterruptLevel2
;
1523 InterruptVector
[0] = PortConfig
->BusInterruptVector
;
1524 InterruptVector
[1] = PortConfig
->BusInterruptVector2
;
1526 InterruptMode
[0] = PortConfig
->InterruptMode
;
1527 InterruptMode
[1] = PortConfig
->InterruptMode2
;
1529 DeviceExtension
->InterruptCount
= (PortConfig
->BusInterruptLevel2
!= 0 || PortConfig
->BusInterruptVector2
!= 0) ? 2 : 1;
1531 for (i
= 0; i
< DeviceExtension
->InterruptCount
; i
++)
1533 /* Register an interrupt handler for this device */
1534 MappedIrq
[i
] = HalGetInterruptVector(PortConfig
->AdapterInterfaceType
,
1535 PortConfig
->SystemIoBusNumber
,
1536 DeviceExtension
->InterruptLevel
[i
],
1542 if (DeviceExtension
->InterruptCount
== 1 || Dirql
[0] > Dirql
[1])
1543 MaxDirql
= Dirql
[0];
1545 MaxDirql
= Dirql
[1];
1547 for (i
= 0; i
< DeviceExtension
->InterruptCount
; i
++)
1549 /* Determine IRQ sharability as usual */
1550 if (PortConfig
->AdapterInterfaceType
== MicroChannel
||
1551 InterruptMode
[i
] == LevelSensitive
)
1553 InterruptShareable
= TRUE
;
1557 InterruptShareable
= FALSE
;
1560 Status
= IoConnectInterrupt(&DeviceExtension
->Interrupt
[i
],
1561 (PKSERVICE_ROUTINE
)ScsiPortIsr
,
1563 &DeviceExtension
->IrqLock
,
1572 if (!(NT_SUCCESS(Status
)))
1574 DPRINT1("Could not connect interrupt %d\n",
1575 InterruptVector
[i
]);
1576 DeviceExtension
->Interrupt
[i
] = NULL
;
1581 if (!NT_SUCCESS(Status
))
1585 /* Save IoAddress (from access ranges) */
1586 if (HwInitializationData
->NumberOfAccessRanges
!= 0)
1588 DeviceExtension
->IoAddress
=
1589 ((*(PortConfig
->AccessRanges
))[0]).RangeStart
.LowPart
;
1591 DPRINT("Io Address %x\n", DeviceExtension
->IoAddress
);
1594 /* Set flag that it's allowed to disconnect during this command */
1595 DeviceExtension
->Flags
|= SCSI_PORT_DISCONNECT_ALLOWED
;
1597 /* Initialize counter of active requests (-1 means there are none) */
1598 DeviceExtension
->ActiveRequestCounter
= -1;
1600 /* Analyze what we have about DMA */
1601 if (DeviceExtension
->AdapterObject
!= NULL
&&
1602 PortConfig
->Master
&&
1603 PortConfig
->NeedPhysicalAddresses
)
1605 DeviceExtension
->MapRegisters
= TRUE
;
1609 DeviceExtension
->MapRegisters
= FALSE
;
1612 /* Call HwInitialize at DISPATCH_LEVEL */
1613 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
1615 if (!KeSynchronizeExecution(DeviceExtension
->Interrupt
[0],
1616 DeviceExtension
->HwInitialize
,
1617 DeviceExtension
->MiniPortDeviceExtension
))
1619 DPRINT1("HwInitialize() failed!\n");
1620 KeLowerIrql(OldIrql
);
1621 Status
= STATUS_ADAPTER_HARDWARE_ERROR
;
1625 /* Check if a notification is needed */
1626 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
1628 /* Call DPC right away, because we're already at DISPATCH_LEVEL */
1629 ScsiPortDpcForIsr(NULL
,
1630 DeviceExtension
->DeviceObject
,
1635 /* Lower irql back to what it was */
1636 KeLowerIrql(OldIrql
);
1638 /* Start our timer */
1639 IoStartTimer(PortDeviceObject
);
1641 /* Initialize bus scanning information */
1642 BusConfigSize
= FIELD_OFFSET(BUSES_CONFIGURATION_INFORMATION
,
1643 BusScanInfo
[DeviceExtension
->PortConfig
->NumberOfBuses
]);
1644 DeviceExtension
->BusesConfig
= ExAllocatePoolWithTag(PagedPool
,
1647 if (!DeviceExtension
->BusesConfig
)
1649 DPRINT1("Out of resources!\n");
1650 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1655 RtlZeroMemory(DeviceExtension
->BusesConfig
, BusConfigSize
);
1657 /* Store number of buses there */
1658 DeviceExtension
->BusesConfig
->NumberOfBuses
= (UCHAR
)DeviceExtension
->BusNum
;
1660 /* Scan the adapter for devices */
1661 SpiScanAdapter(DeviceExtension
);
1663 /* Build the registry device map */
1664 SpiBuildDeviceMap(DeviceExtension
,
1665 (PUNICODE_STRING
)Argument2
);
1667 /* Create the dos device link */
1668 swprintf(DosNameBuffer
,
1670 SystemConfig
->ScsiPortCount
);
1671 RtlInitUnicodeString(&DosDeviceName
, DosNameBuffer
);
1672 IoCreateSymbolicLink(&DosDeviceName
, &DeviceName
);
1674 /* Increase the port count */
1675 SystemConfig
->ScsiPortCount
++;
1676 FirstConfigCall
= FALSE
;
1678 /* Increase adapter number and bus number respectively */
1679 ConfigInfo
.AdapterNumber
++;
1682 ConfigInfo
.BusNumber
++;
1684 DPRINT("Bus: %lu MaxBus: %lu\n", ConfigInfo
.BusNumber
, MaxBus
);
1689 /* Clean up the mess */
1690 SpiCleanupAfterInit(DeviceExtension
);
1692 /* Close registry keys */
1693 if (ConfigInfo
.ServiceKey
!= NULL
)
1694 ZwClose(ConfigInfo
.ServiceKey
);
1696 if (ConfigInfo
.DeviceKey
!= NULL
)
1697 ZwClose(ConfigInfo
.DeviceKey
);
1699 if (ConfigInfo
.BusKey
!= NULL
)
1700 ZwClose(ConfigInfo
.BusKey
);
1702 if (ConfigInfo
.AccessRanges
!= NULL
)
1703 ExFreePool(ConfigInfo
.AccessRanges
);
1705 if (ConfigInfo
.Parameter
!= NULL
)
1706 ExFreePool(ConfigInfo
.Parameter
);
1708 DPRINT("ScsiPortInitialize() done, Status = 0x%08X, DeviceFound = %d!\n",
1709 Status
, DeviceFound
);
1711 return (DeviceFound
== FALSE
) ? Status
: STATUS_SUCCESS
;
1715 SpiCleanupAfterInit(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
)
1717 PSCSI_LUN_INFO LunInfo
;
1721 /* Check if we have something to clean up */
1722 if (DeviceExtension
== NULL
)
1725 /* Stop the timer */
1726 IoStopTimer(DeviceExtension
->DeviceObject
);
1728 /* Disconnect the interrupts */
1729 while (DeviceExtension
->InterruptCount
)
1731 if (DeviceExtension
->Interrupt
[--DeviceExtension
->InterruptCount
])
1732 IoDisconnectInterrupt(DeviceExtension
->Interrupt
[DeviceExtension
->InterruptCount
]);
1735 /* Delete ConfigInfo */
1736 if (DeviceExtension
->BusesConfig
)
1738 for (Bus
= 0; Bus
< DeviceExtension
->BusNum
; Bus
++)
1740 if (!DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
])
1743 LunInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LunInfo
;
1747 /* Free current, but save pointer to the next one */
1748 Ptr
= LunInfo
->Next
;
1749 ExFreePool(LunInfo
);
1753 ExFreePool(DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]);
1756 ExFreePool(DeviceExtension
->BusesConfig
);
1759 /* Free PortConfig */
1760 if (DeviceExtension
->PortConfig
)
1761 ExFreePool(DeviceExtension
->PortConfig
);
1764 for(Lun
= 0; Lun
< LUS_NUMBER
; Lun
++)
1766 while (DeviceExtension
->LunExtensionList
[Lun
])
1768 Ptr
= DeviceExtension
->LunExtensionList
[Lun
];
1769 DeviceExtension
->LunExtensionList
[Lun
] = DeviceExtension
->LunExtensionList
[Lun
]->Next
;
1775 /* Free common buffer (if it exists) */
1776 if (DeviceExtension
->SrbExtensionBuffer
!= NULL
&&
1777 DeviceExtension
->CommonBufferLength
!= 0)
1779 if (!DeviceExtension
->AdapterObject
)
1781 ExFreePool(DeviceExtension
->SrbExtensionBuffer
);
1785 HalFreeCommonBuffer(DeviceExtension
->AdapterObject
,
1786 DeviceExtension
->CommonBufferLength
,
1787 DeviceExtension
->PhysicalAddress
,
1788 DeviceExtension
->SrbExtensionBuffer
,
1794 if (DeviceExtension
->SrbInfo
!= NULL
)
1795 ExFreePool(DeviceExtension
->SrbInfo
);
1797 /* Unmap mapped addresses */
1798 while (DeviceExtension
->MappedAddressList
!= NULL
)
1800 MmUnmapIoSpace(DeviceExtension
->MappedAddressList
->MappedAddress
,
1801 DeviceExtension
->MappedAddressList
->NumberOfBytes
);
1803 Ptr
= DeviceExtension
->MappedAddressList
;
1804 DeviceExtension
->MappedAddressList
= DeviceExtension
->MappedAddressList
->NextMappedAddress
;
1809 /* Finally delete the device object */
1810 DPRINT("Deleting device %p\n", DeviceExtension
->DeviceObject
);
1811 IoDeleteDevice(DeviceExtension
->DeviceObject
);
1818 ScsiPortIoMapTransfer(IN PVOID HwDeviceExtension
,
1819 IN PSCSI_REQUEST_BLOCK Srb
,
1820 IN PVOID LogicalAddress
,
1823 DPRINT1("ScsiPortIoMapTransfer()\n");
1831 ScsiPortLogError(IN PVOID HwDeviceExtension
,
1832 IN PSCSI_REQUEST_BLOCK Srb OPTIONAL
,
1839 //PSCSI_PORT_DEVICE_EXTENSION DeviceExtension;
1841 DPRINT1("ScsiPortLogError() called\n");
1842 DPRINT1("PathId: 0x%02x TargetId: 0x%02x Lun: 0x%02x ErrorCode: 0x%08lx UniqueId: 0x%08lx\n",
1843 PathId
, TargetId
, Lun
, ErrorCode
, UniqueId
);
1845 //DeviceExtension = CONTAINING_RECORD(HwDeviceExtension, SCSI_PORT_DEVICE_EXTENSION, MiniPortDeviceExtension);
1848 DPRINT("ScsiPortLogError() done\n");
1855 ScsiPortMoveMemory(OUT PVOID Destination
,
1859 RtlMoveMemory(Destination
,
1869 ScsiPortNotification(IN SCSI_NOTIFICATION_TYPE NotificationType
,
1870 IN PVOID HwDeviceExtension
,
1873 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
1876 DPRINT("ScsiPortNotification() called\n");
1878 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
1879 SCSI_PORT_DEVICE_EXTENSION
,
1880 MiniPortDeviceExtension
);
1882 DPRINT("DeviceExtension %p\n", DeviceExtension
);
1884 va_start(ap
, HwDeviceExtension
);
1886 switch (NotificationType
)
1888 case RequestComplete
:
1890 PSCSI_REQUEST_BLOCK Srb
;
1891 PSCSI_REQUEST_BLOCK_INFO SrbData
;
1893 Srb
= (PSCSI_REQUEST_BLOCK
) va_arg (ap
, PSCSI_REQUEST_BLOCK
);
1895 DPRINT("Notify: RequestComplete (Srb %p)\n", Srb
);
1897 /* Make sure Srb is alright */
1898 ASSERT(Srb
->SrbStatus
!= SRB_STATUS_PENDING
);
1899 ASSERT(Srb
->Function
!= SRB_FUNCTION_EXECUTE_SCSI
|| Srb
->SrbStatus
!= SRB_STATUS_SUCCESS
|| Srb
->ScsiStatus
== SCSISTAT_GOOD
);
1901 if (!(Srb
->SrbFlags
& SRB_FLAGS_IS_ACTIVE
))
1903 /* It's been already completed */
1908 /* It's not active anymore */
1909 Srb
->SrbFlags
&= ~SRB_FLAGS_IS_ACTIVE
;
1911 if (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
)
1913 /* TODO: Treat it specially */
1918 /* Get the SRB data */
1919 SrbData
= SpiGetSrbData(DeviceExtension
,
1925 /* Make sure there are no CompletedRequests and there is a Srb */
1926 ASSERT(SrbData
->CompletedRequests
== NULL
&& SrbData
->Srb
!= NULL
);
1928 /* If it's a read/write request, make sure it has data inside it */
1929 if ((Srb
->SrbStatus
== SRB_STATUS_SUCCESS
) &&
1930 ((Srb
->Cdb
[0] == SCSIOP_READ
) || (Srb
->Cdb
[0] == SCSIOP_WRITE
)))
1932 ASSERT(Srb
->DataTransferLength
);
1935 SrbData
->CompletedRequests
= DeviceExtension
->InterruptData
.CompletedRequests
;
1936 DeviceExtension
->InterruptData
.CompletedRequests
= SrbData
;
1942 DPRINT("Notify: NextRequest\n");
1943 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_NEXT_REQUEST_READY
;
1951 PSCSI_PORT_LUN_EXTENSION LunExtension
;
1953 PathId
= (UCHAR
) va_arg (ap
, int);
1954 TargetId
= (UCHAR
) va_arg (ap
, int);
1955 Lun
= (UCHAR
) va_arg (ap
, int);
1957 DPRINT("Notify: NextLuRequest(PathId %u TargetId %u Lun %u)\n",
1958 PathId
, TargetId
, Lun
);
1960 /* Mark it in the flags field */
1961 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_NEXT_REQUEST_READY
;
1963 /* Get the LUN extension */
1964 LunExtension
= SpiGetLunExtension(DeviceExtension
,
1969 /* If returned LunExtension is NULL, break out */
1970 if (!LunExtension
) break;
1972 /* This request should not be processed if */
1973 if ((LunExtension
->ReadyLun
) ||
1974 (LunExtension
->SrbInfo
.Srb
))
1976 /* Nothing to do here */
1980 /* Add this LUN to the list */
1981 LunExtension
->ReadyLun
= DeviceExtension
->InterruptData
.ReadyLun
;
1982 DeviceExtension
->InterruptData
.ReadyLun
= LunExtension
;
1987 DPRINT("Notify: ResetDetected\n");
1988 /* Add RESET flags */
1989 DeviceExtension
->InterruptData
.Flags
|=
1990 SCSI_PORT_RESET
| SCSI_PORT_RESET_REPORTED
;
1993 case CallDisableInterrupts
:
1994 DPRINT1("UNIMPLEMENTED SCSI Notification called: CallDisableInterrupts!\n");
1997 case CallEnableInterrupts
:
1998 DPRINT1("UNIMPLEMENTED SCSI Notification called: CallEnableInterrupts!\n");
2001 case RequestTimerCall
:
2002 DPRINT("Notify: RequestTimerCall\n");
2003 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_TIMER_NEEDED
;
2004 DeviceExtension
->InterruptData
.HwScsiTimer
= (PHW_TIMER
)va_arg(ap
, PHW_TIMER
);
2005 DeviceExtension
->InterruptData
.MiniportTimerValue
= (ULONG
)va_arg(ap
, ULONG
);
2008 case BusChangeDetected
:
2009 DPRINT1("UNIMPLEMENTED SCSI Notification called: BusChangeDetected!\n");
2013 DPRINT1 ("Unsupported notification from WMI: %lu\n", NotificationType
);
2019 /* Request a DPC after we're done with the interrupt */
2020 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_NOTIFICATION_NEEDED
;
2027 ScsiPortValidateRange(IN PVOID HwDeviceExtension
,
2028 IN INTERFACE_TYPE BusType
,
2029 IN ULONG SystemIoBusNumber
,
2030 IN SCSI_PHYSICAL_ADDRESS IoAddress
,
2031 IN ULONG NumberOfBytes
,
2032 IN BOOLEAN InIoSpace
)
2034 DPRINT("ScsiPortValidateRange()\n");
2039 /* INTERNAL FUNCTIONS ********************************************************/
2042 SpiResourceToConfig(IN PHW_INITIALIZATION_DATA HwInitializationData
,
2043 IN PCM_FULL_RESOURCE_DESCRIPTOR ResourceDescriptor
,
2044 IN PPORT_CONFIGURATION_INFORMATION PortConfig
)
2046 PACCESS_RANGE AccessRange
;
2047 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialData
;
2050 ULONG Interrupt
= 0;
2055 /* Loop through all entries */
2056 for (Index
= 0; Index
< ResourceDescriptor
->PartialResourceList
.Count
; Index
++)
2058 PartialData
= &ResourceDescriptor
->PartialResourceList
.PartialDescriptors
[Index
];
2060 switch (PartialData
->Type
)
2062 case CmResourceTypePort
:
2063 /* Copy access ranges */
2064 if (RangeNumber
< HwInitializationData
->NumberOfAccessRanges
)
2066 AccessRange
= &((*(PortConfig
->AccessRanges
))[RangeNumber
]);
2068 AccessRange
->RangeStart
= PartialData
->u
.Port
.Start
;
2069 AccessRange
->RangeLength
= PartialData
->u
.Port
.Length
;
2071 AccessRange
->RangeInMemory
= FALSE
;
2076 case CmResourceTypeMemory
:
2077 /* Copy access ranges */
2078 if (RangeNumber
< HwInitializationData
->NumberOfAccessRanges
)
2080 AccessRange
= &((*(PortConfig
->AccessRanges
))[RangeNumber
]);
2082 AccessRange
->RangeStart
= PartialData
->u
.Memory
.Start
;
2083 AccessRange
->RangeLength
= PartialData
->u
.Memory
.Length
;
2085 AccessRange
->RangeInMemory
= TRUE
;
2090 case CmResourceTypeInterrupt
:
2094 /* Copy interrupt data */
2095 PortConfig
->BusInterruptLevel
= PartialData
->u
.Interrupt
.Level
;
2096 PortConfig
->BusInterruptVector
= PartialData
->u
.Interrupt
.Vector
;
2098 /* Set interrupt mode accordingly to the resource */
2099 if (PartialData
->Flags
== CM_RESOURCE_INTERRUPT_LATCHED
)
2101 PortConfig
->InterruptMode
= Latched
;
2103 else if (PartialData
->Flags
== CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE
)
2105 PortConfig
->InterruptMode
= LevelSensitive
;
2108 else if (Interrupt
== 1)
2110 /* Copy interrupt data */
2111 PortConfig
->BusInterruptLevel2
= PartialData
->u
.Interrupt
.Level
;
2112 PortConfig
->BusInterruptVector2
= PartialData
->u
.Interrupt
.Vector
;
2114 /* Set interrupt mode accordingly to the resource */
2115 if (PartialData
->Flags
== CM_RESOURCE_INTERRUPT_LATCHED
)
2117 PortConfig
->InterruptMode2
= Latched
;
2119 else if (PartialData
->Flags
== CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE
)
2121 PortConfig
->InterruptMode2
= LevelSensitive
;
2128 case CmResourceTypeDma
:
2132 PortConfig
->DmaChannel
= PartialData
->u
.Dma
.Channel
;
2133 PortConfig
->DmaPort
= PartialData
->u
.Dma
.Port
;
2135 if (PartialData
->Flags
& CM_RESOURCE_DMA_8
)
2136 PortConfig
->DmaWidth
= Width8Bits
;
2137 else if ((PartialData
->Flags
& CM_RESOURCE_DMA_16
) ||
2138 (PartialData
->Flags
& CM_RESOURCE_DMA_8_AND_16
)) //???
2139 PortConfig
->DmaWidth
= Width16Bits
;
2140 else if (PartialData
->Flags
& CM_RESOURCE_DMA_32
)
2141 PortConfig
->DmaWidth
= Width32Bits
;
2145 PortConfig
->DmaChannel2
= PartialData
->u
.Dma
.Channel
;
2146 PortConfig
->DmaPort2
= PartialData
->u
.Dma
.Port
;
2148 if (PartialData
->Flags
& CM_RESOURCE_DMA_8
)
2149 PortConfig
->DmaWidth2
= Width8Bits
;
2150 else if ((PartialData
->Flags
& CM_RESOURCE_DMA_16
) ||
2151 (PartialData
->Flags
& CM_RESOURCE_DMA_8_AND_16
)) //???
2152 PortConfig
->DmaWidth2
= Width16Bits
;
2153 else if (PartialData
->Flags
& CM_RESOURCE_DMA_32
)
2154 PortConfig
->DmaWidth2
= Width32Bits
;
2161 static PCM_RESOURCE_LIST
2162 SpiConfigToResource(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
2163 PPORT_CONFIGURATION_INFORMATION PortConfig
)
2165 PCONFIGURATION_INFORMATION ConfigInfo
;
2166 PCM_RESOURCE_LIST ResourceList
;
2167 PCM_PARTIAL_RESOURCE_DESCRIPTOR ResourceDescriptor
;
2168 PACCESS_RANGE AccessRange
;
2169 ULONG ListLength
= 0, i
, FullSize
;
2170 ULONG Interrupt
, Dma
;
2172 /* Get current Atdisk usage from the system */
2173 ConfigInfo
= IoGetConfigurationInformation();
2175 if (PortConfig
->AtdiskPrimaryClaimed
)
2176 ConfigInfo
->AtDiskPrimaryAddressClaimed
= TRUE
;
2178 if (PortConfig
->AtdiskSecondaryClaimed
)
2179 ConfigInfo
->AtDiskSecondaryAddressClaimed
= TRUE
;
2181 /* Do we use DMA? */
2182 if (PortConfig
->DmaChannel
!= SP_UNINITIALIZED_VALUE
||
2183 PortConfig
->DmaPort
!= SP_UNINITIALIZED_VALUE
)
2187 if (PortConfig
->DmaChannel2
!= SP_UNINITIALIZED_VALUE
||
2188 PortConfig
->DmaPort2
!= SP_UNINITIALIZED_VALUE
)
2197 /* How many interrupts to we have? */
2198 Interrupt
= DeviceExtension
->InterruptCount
;
2199 ListLength
+= Interrupt
;
2201 /* How many access ranges do we use? */
2202 AccessRange
= &((*(PortConfig
->AccessRanges
))[0]);
2203 for (i
= 0; i
< PortConfig
->NumberOfAccessRanges
; i
++)
2205 if (AccessRange
->RangeLength
!= 0)
2211 /* Allocate the resource list, since we know its size now */
2212 FullSize
= sizeof(CM_RESOURCE_LIST
) + (ListLength
- 1) *
2213 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
2215 ResourceList
= (PCM_RESOURCE_LIST
)ExAllocatePoolWithTag(PagedPool
, FullSize
, TAG_SCSIPORT
);
2221 RtlZeroMemory(ResourceList
, FullSize
);
2224 ResourceList
->Count
= 1;
2225 ResourceList
->List
[0].InterfaceType
= PortConfig
->AdapterInterfaceType
;
2226 ResourceList
->List
[0].BusNumber
= PortConfig
->SystemIoBusNumber
;
2227 ResourceList
->List
[0].PartialResourceList
.Count
= ListLength
;
2228 ResourceDescriptor
= ResourceList
->List
[0].PartialResourceList
.PartialDescriptors
;
2230 /* Copy access ranges array over */
2231 for (i
= 0; i
< PortConfig
->NumberOfAccessRanges
; i
++)
2233 AccessRange
= &((*(PortConfig
->AccessRanges
))[i
]);
2235 /* If the range is empty - skip it */
2236 if (AccessRange
->RangeLength
== 0)
2239 if (AccessRange
->RangeInMemory
)
2241 ResourceDescriptor
->Type
= CmResourceTypeMemory
;
2242 ResourceDescriptor
->Flags
= CM_RESOURCE_MEMORY_READ_WRITE
;
2246 ResourceDescriptor
->Type
= CmResourceTypePort
;
2247 ResourceDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
2250 ResourceDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
2252 ResourceDescriptor
->u
.Memory
.Start
= AccessRange
->RangeStart
;
2253 ResourceDescriptor
->u
.Memory
.Length
= AccessRange
->RangeLength
;
2255 ResourceDescriptor
++;
2258 /* If we use interrupt(s), copy them */
2261 ResourceDescriptor
->Type
= CmResourceTypeInterrupt
;
2263 if (PortConfig
->AdapterInterfaceType
== MicroChannel
||
2264 ((Interrupt
== 2) ? PortConfig
->InterruptMode2
: PortConfig
->InterruptMode
) == LevelSensitive
)
2266 ResourceDescriptor
->ShareDisposition
= CmResourceShareShared
;
2267 ResourceDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE
;
2271 ResourceDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
2272 ResourceDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
2275 ResourceDescriptor
->u
.Interrupt
.Level
= (Interrupt
== 2) ? PortConfig
->BusInterruptLevel2
: PortConfig
->BusInterruptLevel
;
2276 ResourceDescriptor
->u
.Interrupt
.Vector
= (Interrupt
== 2) ? PortConfig
->BusInterruptVector2
: PortConfig
->BusInterruptVector
;
2277 ResourceDescriptor
->u
.Interrupt
.Affinity
= 0;
2279 ResourceDescriptor
++;
2286 ResourceDescriptor
->Type
= CmResourceTypeDma
;
2287 ResourceDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
2288 ResourceDescriptor
->u
.Dma
.Channel
= (Dma
== 2) ? PortConfig
->DmaChannel2
: PortConfig
->DmaChannel
;
2289 ResourceDescriptor
->u
.Dma
.Port
= (Dma
== 2) ? PortConfig
->DmaPort2
: PortConfig
->DmaPort
;
2290 ResourceDescriptor
->Flags
= 0;
2292 if (((Dma
== 2) ? PortConfig
->DmaWidth2
: PortConfig
->DmaWidth
) == Width8Bits
)
2293 ResourceDescriptor
->Flags
|= CM_RESOURCE_DMA_8
;
2294 else if (((Dma
== 2) ? PortConfig
->DmaWidth2
: PortConfig
->DmaWidth
) == Width16Bits
)
2295 ResourceDescriptor
->Flags
|= CM_RESOURCE_DMA_16
;
2297 ResourceDescriptor
->Flags
|= CM_RESOURCE_DMA_32
;
2299 if (((Dma
== 2) ? PortConfig
->DmaChannel2
: PortConfig
->DmaChannel
) == SP_UNINITIALIZED_VALUE
)
2300 ResourceDescriptor
->u
.Dma
.Channel
= 0;
2302 if (((Dma
== 2) ? PortConfig
->DmaPort2
: PortConfig
->DmaPort
) == SP_UNINITIALIZED_VALUE
)
2303 ResourceDescriptor
->u
.Dma
.Port
= 0;
2305 ResourceDescriptor
++;
2309 return ResourceList
;
2314 SpiGetPciConfigData(IN PDRIVER_OBJECT DriverObject
,
2315 IN PDEVICE_OBJECT DeviceObject
,
2316 IN
struct _HW_INITIALIZATION_DATA
*HwInitializationData
,
2317 IN OUT PPORT_CONFIGURATION_INFORMATION PortConfig
,
2318 IN PUNICODE_STRING RegistryPath
,
2320 IN OUT PPCI_SLOT_NUMBER NextSlotNumber
)
2322 PCI_COMMON_CONFIG PciConfig
;
2323 PCI_SLOT_NUMBER SlotNumber
;
2326 ULONG FunctionNumber
;
2327 CHAR VendorIdString
[8];
2328 CHAR DeviceIdString
[8];
2329 UNICODE_STRING UnicodeStr
;
2330 PCM_RESOURCE_LIST ResourceList
= NULL
;
2333 DPRINT ("SpiGetPciConfiguration() called\n");
2335 SlotNumber
.u
.AsULONG
= 0;
2337 /* Loop through all devices */
2338 for (DeviceNumber
= NextSlotNumber
->u
.bits
.DeviceNumber
; DeviceNumber
< PCI_MAX_DEVICES
; DeviceNumber
++)
2340 SlotNumber
.u
.bits
.DeviceNumber
= DeviceNumber
;
2342 /* Loop through all functions */
2343 for (FunctionNumber
= NextSlotNumber
->u
.bits
.FunctionNumber
; FunctionNumber
< PCI_MAX_FUNCTION
; FunctionNumber
++)
2345 SlotNumber
.u
.bits
.FunctionNumber
= FunctionNumber
;
2347 /* Get PCI config bytes */
2348 DataSize
= HalGetBusData(PCIConfiguration
,
2350 SlotNumber
.u
.AsULONG
,
2354 /* If result of HalGetBusData is 0, then the bus is wrong */
2358 /* Check if result is PCI_INVALID_VENDORID or too small */
2359 if ((DataSize
< sizeof(ULONG
)) ||
2360 (PciConfig
.VendorID
== PCI_INVALID_VENDORID
))
2362 /* Continue to try the next function */
2366 sprintf (VendorIdString
, "%04hx", PciConfig
.VendorID
);
2367 sprintf (DeviceIdString
, "%04hx", PciConfig
.DeviceID
);
2369 if (_strnicmp(VendorIdString
, HwInitializationData
->VendorId
, HwInitializationData
->VendorIdLength
) ||
2370 _strnicmp(DeviceIdString
, HwInitializationData
->DeviceId
, HwInitializationData
->DeviceIdLength
))
2372 /* It is not our device */
2376 DPRINT("Found device 0x%04hx 0x%04hx at %1lu %2lu %1lu\n",
2380 SlotNumber
.u
.bits
.DeviceNumber
,
2381 SlotNumber
.u
.bits
.FunctionNumber
);
2384 RtlInitUnicodeString(&UnicodeStr
, L
"ScsiAdapter");
2385 Status
= HalAssignSlotResources(RegistryPath
,
2391 SlotNumber
.u
.AsULONG
,
2394 if (!NT_SUCCESS(Status
))
2397 /* Create configuration information */
2398 SpiResourceToConfig(HwInitializationData
,
2402 /* Free the resource list */
2403 ExFreePool(ResourceList
);
2405 /* Set dev & fn numbers */
2406 NextSlotNumber
->u
.bits
.DeviceNumber
= DeviceNumber
;
2407 NextSlotNumber
->u
.bits
.FunctionNumber
= FunctionNumber
+ 1;
2409 /* Save the slot number */
2410 PortConfig
->SlotNumber
= SlotNumber
.u
.AsULONG
;
2414 NextSlotNumber
->u
.bits
.FunctionNumber
= 0;
2417 NextSlotNumber
->u
.bits
.DeviceNumber
= 0;
2418 DPRINT ("No device found\n");
2425 /**********************************************************************
2427 * ScsiPortCreateClose
2430 * Answer requests for Create/Close calls: a null operation.
2437 * Pointer to a device object.
2440 * Pointer to an IRP.
2446 static NTSTATUS NTAPI
2447 ScsiPortCreateClose(IN PDEVICE_OBJECT DeviceObject
,
2450 DPRINT("ScsiPortCreateClose()\n");
2452 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
2453 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2455 return STATUS_SUCCESS
;
2459 SpiHandleAttachRelease(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
2462 PSCSI_LUN_INFO LunInfo
;
2463 PIO_STACK_LOCATION IrpStack
;
2464 PDEVICE_OBJECT DeviceObject
;
2465 PSCSI_REQUEST_BLOCK Srb
;
2468 /* Get pointer to the SRB */
2469 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
2470 Srb
= (PSCSI_REQUEST_BLOCK
)IrpStack
->Parameters
.Others
.Argument1
;
2472 /* Check if PathId matches number of buses */
2473 if (DeviceExtension
->BusesConfig
== NULL
||
2474 DeviceExtension
->BusesConfig
->NumberOfBuses
<= Srb
->PathId
)
2476 Srb
->SrbStatus
= SRB_STATUS_NO_DEVICE
;
2477 return STATUS_DEVICE_DOES_NOT_EXIST
;
2480 /* Get pointer to LunInfo */
2481 LunInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Srb
->PathId
]->LunInfo
;
2483 /* Find matching LunInfo */
2486 if (LunInfo
->PathId
== Srb
->PathId
&&
2487 LunInfo
->TargetId
== Srb
->TargetId
&&
2488 LunInfo
->Lun
== Srb
->Lun
)
2493 LunInfo
= LunInfo
->Next
;
2496 /* If we couldn't find it - exit */
2497 if (LunInfo
== NULL
)
2498 return STATUS_DEVICE_DOES_NOT_EXIST
;
2502 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
2504 /* Release, if asked */
2505 if (Srb
->Function
== SRB_FUNCTION_RELEASE_DEVICE
)
2507 LunInfo
->DeviceClaimed
= FALSE
;
2508 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2509 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2511 return STATUS_SUCCESS
;
2514 /* Attach, if not already claimed */
2515 if (LunInfo
->DeviceClaimed
)
2517 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2518 Srb
->SrbStatus
= SRB_STATUS_BUSY
;
2520 return STATUS_DEVICE_BUSY
;
2523 /* Save the device object */
2524 DeviceObject
= LunInfo
->DeviceObject
;
2526 if (Srb
->Function
== SRB_FUNCTION_CLAIM_DEVICE
)
2527 LunInfo
->DeviceClaimed
= TRUE
;
2529 if (Srb
->Function
== SRB_FUNCTION_ATTACH_DEVICE
)
2530 LunInfo
->DeviceObject
= Srb
->DataBuffer
;
2532 Srb
->DataBuffer
= DeviceObject
;
2534 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2535 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2537 return STATUS_SUCCESS
;
2541 /**********************************************************************
2543 * ScsiPortDispatchScsi
2546 * Answer requests for SCSI calls
2552 * Standard dispatch arguments
2558 static NTSTATUS NTAPI
2559 ScsiPortDispatchScsi(IN PDEVICE_OBJECT DeviceObject
,
2562 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
2563 PSCSI_PORT_LUN_EXTENSION LunExtension
;
2564 PIO_STACK_LOCATION Stack
;
2565 PSCSI_REQUEST_BLOCK Srb
;
2567 NTSTATUS Status
= STATUS_SUCCESS
;
2568 PIRP NextIrp
, IrpList
;
2569 PKDEVICE_QUEUE_ENTRY Entry
;
2571 DPRINT("ScsiPortDispatchScsi(DeviceObject %p Irp %p)\n",
2574 DeviceExtension
= DeviceObject
->DeviceExtension
;
2575 Stack
= IoGetCurrentIrpStackLocation(Irp
);
2577 Srb
= Stack
->Parameters
.Scsi
.Srb
;
2580 DPRINT1("ScsiPortDispatchScsi() called with Srb = NULL!\n");
2581 Status
= STATUS_UNSUCCESSFUL
;
2583 Irp
->IoStatus
.Status
= Status
;
2584 Irp
->IoStatus
.Information
= 0;
2586 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2591 DPRINT("Srb: %p\n", Srb
);
2592 DPRINT("Srb->Function: %lu\n", Srb
->Function
);
2593 DPRINT("PathId: %lu TargetId: %lu Lun: %lu\n", Srb
->PathId
, Srb
->TargetId
, Srb
->Lun
);
2595 LunExtension
= SpiGetLunExtension(DeviceExtension
,
2599 if (LunExtension
== NULL
)
2601 DPRINT("ScsiPortDispatchScsi() called with an invalid LUN\n");
2602 Status
= STATUS_NO_SUCH_DEVICE
;
2604 Srb
->SrbStatus
= SRB_STATUS_NO_DEVICE
;
2605 Irp
->IoStatus
.Status
= Status
;
2606 Irp
->IoStatus
.Information
= 0;
2608 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2613 switch (Srb
->Function
)
2615 case SRB_FUNCTION_SHUTDOWN
:
2616 case SRB_FUNCTION_FLUSH
:
2617 DPRINT (" SRB_FUNCTION_SHUTDOWN or FLUSH\n");
2618 if (DeviceExtension
->CachesData
== FALSE
)
2620 /* All success here */
2621 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2622 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
2623 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2624 return STATUS_SUCCESS
;
2626 /* Fall through to a usual execute operation */
2628 case SRB_FUNCTION_EXECUTE_SCSI
:
2629 case SRB_FUNCTION_IO_CONTROL
:
2630 DPRINT(" SRB_FUNCTION_EXECUTE_SCSI or SRB_FUNCTION_IO_CONTROL\n");
2631 /* Mark IRP as pending in all cases */
2632 IoMarkIrpPending(Irp
);
2634 if (Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
)
2636 /* Start IO directly */
2637 IoStartPacket(DeviceObject
, Irp
, NULL
, NULL
);
2643 /* We need to be at DISPATCH_LEVEL */
2644 KeRaiseIrql (DISPATCH_LEVEL
, &oldIrql
);
2646 /* Insert IRP into the queue */
2647 if (!KeInsertByKeyDeviceQueue(&LunExtension
->DeviceQueue
,
2648 &Irp
->Tail
.Overlay
.DeviceQueueEntry
,
2651 /* It means the queue is empty, and we just start this request */
2652 IoStartPacket(DeviceObject
, Irp
, NULL
, NULL
);
2655 /* Back to the old IRQL */
2656 KeLowerIrql (oldIrql
);
2658 return STATUS_PENDING
;
2660 case SRB_FUNCTION_CLAIM_DEVICE
:
2661 case SRB_FUNCTION_ATTACH_DEVICE
:
2662 DPRINT (" SRB_FUNCTION_CLAIM_DEVICE or ATTACH\n");
2664 /* Reference device object and keep the device object */
2665 Status
= SpiHandleAttachRelease(DeviceExtension
, Irp
);
2668 case SRB_FUNCTION_RELEASE_DEVICE
:
2669 DPRINT (" SRB_FUNCTION_RELEASE_DEVICE\n");
2671 /* Dereference device object and clear the device object */
2672 Status
= SpiHandleAttachRelease(DeviceExtension
, Irp
);
2675 case SRB_FUNCTION_RELEASE_QUEUE
:
2676 DPRINT(" SRB_FUNCTION_RELEASE_QUEUE\n");
2678 /* Guard with the spinlock */
2679 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
2681 if (!(LunExtension
->Flags
& LUNEX_FROZEN_QUEUE
))
2683 DPRINT("Queue is not frozen really\n");
2685 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2686 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2687 Status
= STATUS_SUCCESS
;
2692 /* Unfreeze the queue */
2693 LunExtension
->Flags
&= ~LUNEX_FROZEN_QUEUE
;
2695 if (LunExtension
->SrbInfo
.Srb
== NULL
)
2697 /* Get next logical unit request */
2698 SpiGetNextRequestFromLun(DeviceExtension
, LunExtension
);
2700 /* SpiGetNextRequestFromLun() releases the spinlock */
2705 DPRINT("The queue has active request\n");
2706 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2710 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2711 Status
= STATUS_SUCCESS
;
2714 case SRB_FUNCTION_FLUSH_QUEUE
:
2715 DPRINT(" SRB_FUNCTION_FLUSH_QUEUE\n");
2717 /* Guard with the spinlock */
2718 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
2720 if (!(LunExtension
->Flags
& LUNEX_FROZEN_QUEUE
))
2722 DPRINT("Queue is not frozen really\n");
2724 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2725 Status
= STATUS_INVALID_DEVICE_REQUEST
;
2729 /* Make sure there is no active request */
2730 ASSERT(LunExtension
->SrbInfo
.Srb
== NULL
);
2732 /* Compile a list from the device queue */
2734 while ((Entry
= KeRemoveDeviceQueue(&LunExtension
->DeviceQueue
)) != NULL
)
2736 NextIrp
= CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.DeviceQueueEntry
);
2739 Stack
= IoGetCurrentIrpStackLocation(NextIrp
);
2740 Srb
= Stack
->Parameters
.Scsi
.Srb
;
2743 Srb
->SrbStatus
= SRB_STATUS_REQUEST_FLUSHED
;
2744 NextIrp
->IoStatus
.Status
= STATUS_UNSUCCESSFUL
;
2746 /* Add then to the list */
2747 NextIrp
->Tail
.Overlay
.ListEntry
.Flink
= (PLIST_ENTRY
)IrpList
;
2751 /* Unfreeze the queue */
2752 LunExtension
->Flags
&= ~LUNEX_FROZEN_QUEUE
;
2754 /* Release the spinlock */
2755 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2757 /* Complete those requests */
2761 IrpList
= (PIRP
)NextIrp
->Tail
.Overlay
.ListEntry
.Flink
;
2763 IoCompleteRequest(NextIrp
, 0);
2766 Status
= STATUS_SUCCESS
;
2770 DPRINT1("SRB function not implemented (Function %lu)\n", Srb
->Function
);
2771 Status
= STATUS_NOT_IMPLEMENTED
;
2775 Irp
->IoStatus
.Status
= Status
;
2777 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2783 /**********************************************************************
2785 * ScsiPortDeviceControl
2788 * Answer requests for device control calls
2794 * Standard dispatch arguments
2800 static NTSTATUS NTAPI
2801 ScsiPortDeviceControl(IN PDEVICE_OBJECT DeviceObject
,
2804 PIO_STACK_LOCATION Stack
;
2805 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
2806 PDUMP_POINTERS DumpPointers
;
2809 DPRINT("ScsiPortDeviceControl()\n");
2811 Irp
->IoStatus
.Information
= 0;
2813 Stack
= IoGetCurrentIrpStackLocation(Irp
);
2814 DeviceExtension
= DeviceObject
->DeviceExtension
;
2816 switch (Stack
->Parameters
.DeviceIoControl
.IoControlCode
)
2818 case IOCTL_SCSI_GET_DUMP_POINTERS
:
2819 DPRINT(" IOCTL_SCSI_GET_DUMP_POINTERS\n");
2821 if (Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(DUMP_POINTERS
))
2823 Status
= STATUS_BUFFER_OVERFLOW
;
2824 Irp
->IoStatus
.Information
= sizeof(DUMP_POINTERS
);
2828 DumpPointers
= Irp
->AssociatedIrp
.SystemBuffer
;
2829 DumpPointers
->DeviceObject
= DeviceObject
;
2832 Status
= STATUS_SUCCESS
;
2833 Irp
->IoStatus
.Information
= sizeof(DUMP_POINTERS
);
2836 case IOCTL_SCSI_GET_CAPABILITIES
:
2837 DPRINT(" IOCTL_SCSI_GET_CAPABILITIES\n");
2838 if (Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
== sizeof(PVOID
))
2840 *((PVOID
*)Irp
->AssociatedIrp
.SystemBuffer
) = &DeviceExtension
->PortCapabilities
;
2842 Irp
->IoStatus
.Information
= sizeof(PVOID
);
2843 Status
= STATUS_SUCCESS
;
2847 if (Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(IO_SCSI_CAPABILITIES
))
2849 Status
= STATUS_BUFFER_TOO_SMALL
;
2853 RtlCopyMemory(Irp
->AssociatedIrp
.SystemBuffer
,
2854 &DeviceExtension
->PortCapabilities
,
2855 sizeof(IO_SCSI_CAPABILITIES
));
2857 Irp
->IoStatus
.Information
= sizeof(IO_SCSI_CAPABILITIES
);
2858 Status
= STATUS_SUCCESS
;
2861 case IOCTL_SCSI_GET_INQUIRY_DATA
:
2862 DPRINT(" IOCTL_SCSI_GET_INQUIRY_DATA\n");
2864 /* Copy inquiry data to the port device extension */
2865 Status
= SpiGetInquiryData(DeviceExtension
, Irp
);
2868 case IOCTL_SCSI_MINIPORT
:
2869 DPRINT1("IOCTL_SCSI_MINIPORT unimplemented!\n");
2870 Status
= STATUS_NOT_IMPLEMENTED
;
2873 case IOCTL_SCSI_PASS_THROUGH
:
2874 DPRINT1("IOCTL_SCSI_PASS_THROUGH unimplemented!\n");
2875 Status
= STATUS_NOT_IMPLEMENTED
;
2879 if (DEVICE_TYPE_FROM_CTL_CODE(Stack
->Parameters
.DeviceIoControl
.IoControlCode
) == MOUNTDEVCONTROLTYPE
)
2881 switch (Stack
->Parameters
.DeviceIoControl
.IoControlCode
)
2883 case IOCTL_MOUNTDEV_QUERY_DEVICE_NAME
:
2884 DPRINT1("Got unexpected IOCTL_MOUNTDEV_QUERY_DEVICE_NAME\n");
2886 case IOCTL_MOUNTDEV_QUERY_UNIQUE_ID
:
2887 DPRINT1("Got unexpected IOCTL_MOUNTDEV_QUERY_UNIQUE_ID\n");
2890 DPRINT1(" got ioctl intended for the mount manager: 0x%lX\n", Stack
->Parameters
.DeviceIoControl
.IoControlCode
);
2894 DPRINT1(" unknown ioctl code: 0x%lX\n", Stack
->Parameters
.DeviceIoControl
.IoControlCode
);
2896 Status
= STATUS_NOT_IMPLEMENTED
;
2900 /* Complete the request with the given status */
2901 Irp
->IoStatus
.Status
= Status
;
2902 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2909 ScsiPortStartIo(IN PDEVICE_OBJECT DeviceObject
,
2912 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
2913 PSCSI_PORT_LUN_EXTENSION LunExtension
;
2914 PIO_STACK_LOCATION IrpStack
;
2915 PSCSI_REQUEST_BLOCK Srb
;
2916 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
2920 DPRINT("ScsiPortStartIo() called!\n");
2922 DeviceExtension
= DeviceObject
->DeviceExtension
;
2923 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
2925 DPRINT("DeviceExtension %p\n", DeviceExtension
);
2927 Srb
= IrpStack
->Parameters
.Scsi
.Srb
;
2929 /* Apply "default" flags */
2930 Srb
->SrbFlags
|= DeviceExtension
->SrbFlags
;
2932 /* Get LUN extension */
2933 LunExtension
= SpiGetLunExtension(DeviceExtension
,
2938 if (DeviceExtension
->NeedSrbDataAlloc
||
2939 DeviceExtension
->NeedSrbExtensionAlloc
)
2942 SrbInfo
= SpiAllocateSrbStructures(DeviceExtension
,
2946 /* Couldn't alloc one or both data structures, return */
2947 if (SrbInfo
== NULL
)
2949 /* We have to call IoStartNextPacket, because this request
2951 if (LunExtension
->Flags
& LUNEX_REQUEST_PENDING
)
2952 IoStartNextPacket(DeviceObject
, FALSE
);
2959 /* No allocations are needed */
2960 SrbInfo
= &LunExtension
->SrbInfo
;
2961 Srb
->SrbExtension
= NULL
;
2962 Srb
->QueueTag
= SP_UNTAGGED
;
2965 /* Increase sequence number of SRB */
2966 if (!SrbInfo
->SequenceNumber
)
2968 /* Increase global sequence number */
2969 DeviceExtension
->SequenceNumber
++;
2972 SrbInfo
->SequenceNumber
= DeviceExtension
->SequenceNumber
;
2975 /* Check some special SRBs */
2976 if (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
)
2978 /* Some special handling */
2979 DPRINT1("Abort command! Unimplemented now\n");
2986 if (Srb
->SrbFlags
& SRB_FLAGS_UNSPECIFIED_DIRECTION
)
2988 // Store the MDL virtual address in SrbInfo structure
2989 SrbInfo
->DataOffset
= MmGetMdlVirtualAddress(Irp
->MdlAddress
);
2991 if (DeviceExtension
->MapBuffers
)
2993 /* Calculate offset within DataBuffer */
2994 SrbInfo
->DataOffset
= MmGetSystemAddressForMdl(Irp
->MdlAddress
);
2995 Srb
->DataBuffer
= SrbInfo
->DataOffset
+
2996 (ULONG
)((PUCHAR
)Srb
->DataBuffer
-
2997 (PUCHAR
)MmGetMdlVirtualAddress(Irp
->MdlAddress
));
3000 if (DeviceExtension
->AdapterObject
)
3003 KeFlushIoBuffers(Irp
->MdlAddress
,
3004 Srb
->SrbFlags
& SRB_FLAGS_DATA_IN
? TRUE
: FALSE
,
3008 if (DeviceExtension
->MapRegisters
)
3010 /* Calculate number of needed map registers */
3011 SrbInfo
->NumberOfMapRegisters
= ADDRESS_AND_SIZE_TO_SPAN_PAGES(
3013 Srb
->DataTransferLength
);
3015 /* Allocate adapter channel */
3016 Status
= IoAllocateAdapterChannel(DeviceExtension
->AdapterObject
,
3017 DeviceExtension
->DeviceObject
,
3018 SrbInfo
->NumberOfMapRegisters
,
3022 if (!NT_SUCCESS(Status
))
3024 DPRINT1("IoAllocateAdapterChannel() failed!\n");
3026 Srb
->SrbStatus
= SRB_STATUS_INVALID_REQUEST
;
3027 ScsiPortNotification(RequestComplete
,
3028 DeviceExtension
+ 1,
3031 ScsiPortNotification(NextRequest
,
3032 DeviceExtension
+ 1);
3034 /* Request DPC for that work */
3035 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
3038 /* Control goes to SpiAdapterControl */
3043 /* Increase active request counter */
3044 CounterResult
= InterlockedIncrement(&DeviceExtension
->ActiveRequestCounter
);
3046 if (CounterResult
== 0 &&
3047 DeviceExtension
->AdapterObject
!= NULL
&&
3048 !DeviceExtension
->MapRegisters
)
3050 IoAllocateAdapterChannel(
3051 DeviceExtension
->AdapterObject
,
3053 DeviceExtension
->PortCapabilities
.MaximumPhysicalPages
,
3054 ScsiPortAllocateAdapterChannel
,
3061 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
3063 if (!KeSynchronizeExecution(DeviceExtension
->Interrupt
[0],
3064 ScsiPortStartPacket
,
3067 DPRINT("Synchronization failed!\n");
3069 Irp
->IoStatus
.Status
= STATUS_UNSUCCESSFUL
;
3070 Irp
->IoStatus
.Information
= 0;
3071 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3073 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
3077 /* Release the spinlock only */
3078 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3082 DPRINT("ScsiPortStartIo() done\n");
3086 static BOOLEAN NTAPI
3087 ScsiPortStartPacket(IN OUT PVOID Context
)
3089 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
3090 PIO_STACK_LOCATION IrpStack
;
3091 PSCSI_REQUEST_BLOCK Srb
;
3092 PDEVICE_OBJECT DeviceObject
= (PDEVICE_OBJECT
)Context
;
3093 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3094 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
3098 DPRINT("ScsiPortStartPacket() called\n");
3100 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
3102 IrpStack
= IoGetCurrentIrpStackLocation(DeviceObject
->CurrentIrp
);
3103 Srb
= IrpStack
->Parameters
.Scsi
.Srb
;
3105 /* Get LUN extension */
3106 LunExtension
= SpiGetLunExtension(DeviceExtension
,
3111 /* Check if we are in a reset state */
3112 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_RESET
)
3114 /* Mark the we've got requests while being in the reset state */
3115 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_RESET_REQUEST
;
3119 /* Set the time out value */
3120 DeviceExtension
->TimerCount
= Srb
->TimeOutValue
;
3123 DeviceExtension
->Flags
|= SCSI_PORT_DEVICE_BUSY
;
3125 if (LunExtension
->RequestTimeout
!= -1)
3127 /* Timer already active */
3132 /* It hasn't been initialized yet */
3133 LunExtension
->RequestTimeout
= Srb
->TimeOutValue
;
3137 if (Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
)
3139 /* Handle bypass-requests */
3141 /* Is this an abort request? */
3142 if (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
)
3144 /* Get pointer to SRB info structure */
3145 SrbInfo
= SpiGetSrbData(DeviceExtension
,
3151 /* Check if the request is still "active" */
3152 if (SrbInfo
== NULL
||
3153 SrbInfo
->Srb
== NULL
||
3154 !(SrbInfo
->Srb
->SrbFlags
& SRB_FLAGS_IS_ACTIVE
))
3156 /* It's not, mark it as active then */
3157 Srb
->SrbFlags
|= SRB_FLAGS_IS_ACTIVE
;
3160 LunExtension
->RequestTimeout
= -1;
3162 DPRINT("Request has been already completed, but abort request came\n");
3163 Srb
->SrbStatus
= SRB_STATUS_ABORT_FAILED
;
3165 /* Notify about request complete */
3166 ScsiPortNotification(RequestComplete
,
3167 DeviceExtension
->MiniPortDeviceExtension
,
3170 /* and about readiness for the next request */
3171 ScsiPortNotification(NextRequest
,
3172 DeviceExtension
->MiniPortDeviceExtension
);
3174 /* They might ask for some work, so queue the DPC for them */
3175 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
3177 /* We're done in this branch */
3183 /* Add number of queued requests */
3184 LunExtension
->QueueCount
++;
3187 /* Bypass requests don't need request sense */
3188 LunExtension
->Flags
&= ~LUNEX_NEED_REQUEST_SENSE
;
3190 /* Is disconnect disabled for this request? */
3191 if (Srb
->SrbFlags
& SRB_FLAGS_DISABLE_DISCONNECT
)
3193 /* Set the corresponding flag */
3194 DeviceExtension
->Flags
&= ~SCSI_PORT_DISCONNECT_ALLOWED
;
3197 /* Transfer timeout value from Srb to Lun */
3198 LunExtension
->RequestTimeout
= Srb
->TimeOutValue
;
3202 if (Srb
->SrbFlags
& SRB_FLAGS_DISABLE_DISCONNECT
)
3204 /* It's a disconnect, so no more requests can go */
3205 DeviceExtension
->Flags
&= ~SCSI_PORT_DISCONNECT_ALLOWED
;
3208 LunExtension
->Flags
|= SCSI_PORT_LU_ACTIVE
;
3210 /* Increment queue count */
3211 LunExtension
->QueueCount
++;
3213 /* If it's tagged - special thing */
3214 if (Srb
->QueueTag
!= SP_UNTAGGED
)
3216 SrbInfo
= &DeviceExtension
->SrbInfo
[Srb
->QueueTag
- 1];
3218 /* Chek for consistency */
3219 ASSERT(SrbInfo
->Requests
.Blink
== NULL
);
3221 /* Insert it into the list of requests */
3222 InsertTailList(&LunExtension
->SrbInfo
.Requests
, &SrbInfo
->Requests
);
3226 /* Mark this Srb active */
3227 Srb
->SrbFlags
|= SRB_FLAGS_IS_ACTIVE
;
3229 /* Call HwStartIo routine */
3230 Result
= DeviceExtension
->HwStartIo(&DeviceExtension
->MiniPortDeviceExtension
,
3233 /* If notification is needed, then request a DPC */
3234 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
3235 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
3240 IO_ALLOCATION_ACTION
3242 SpiAdapterControl(PDEVICE_OBJECT DeviceObject
,
3244 PVOID MapRegisterBase
,
3247 PSCSI_REQUEST_BLOCK Srb
;
3248 PSCSI_SG_ADDRESS ScatterGatherList
;
3250 PIO_STACK_LOCATION IrpStack
;
3251 ULONG TotalLength
= 0;
3252 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
3253 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
3255 BOOLEAN WriteToDevice
;
3257 /* Get pointers to SrbInfo and DeviceExtension */
3258 SrbInfo
= (PSCSI_REQUEST_BLOCK_INFO
)Context
;
3259 DeviceExtension
= DeviceObject
->DeviceExtension
;
3261 /* Get pointer to SRB */
3262 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
3263 Srb
= (PSCSI_REQUEST_BLOCK
)IrpStack
->Parameters
.Others
.Argument1
;
3265 /* Depending on the map registers number, we allocate
3266 either from NonPagedPool, or from our static list */
3267 if (SrbInfo
->NumberOfMapRegisters
> MAX_SG_LIST
)
3269 SrbInfo
->ScatterGather
= ExAllocatePoolWithTag(
3270 NonPagedPool
, SrbInfo
->NumberOfMapRegisters
* sizeof(SCSI_SG_ADDRESS
), TAG_SCSIPORT
);
3272 if (SrbInfo
->ScatterGather
== NULL
)
3275 Srb
->SrbFlags
|= SRB_FLAGS_SGLIST_FROM_POOL
;
3279 SrbInfo
->ScatterGather
= SrbInfo
->ScatterGatherList
;
3282 /* Use chosen SG list source */
3283 ScatterGatherList
= SrbInfo
->ScatterGather
;
3285 /* Save map registers base */
3286 SrbInfo
->BaseOfMapRegister
= MapRegisterBase
;
3288 /* Determine WriteToDevice flag */
3289 WriteToDevice
= Srb
->SrbFlags
& SRB_FLAGS_DATA_OUT
? TRUE
: FALSE
;
3291 /* Get virtual address of the data buffer */
3292 DataVA
= (PUCHAR
)MmGetMdlVirtualAddress(Irp
->MdlAddress
) +
3293 ((PCHAR
)Srb
->DataBuffer
- SrbInfo
->DataOffset
);
3295 /* Build the actual SG list */
3296 while (TotalLength
< Srb
->DataTransferLength
)
3298 if (!ScatterGatherList
)
3301 ScatterGatherList
->Length
= Srb
->DataTransferLength
- TotalLength
;
3302 ScatterGatherList
->PhysicalAddress
= IoMapTransfer(DeviceExtension
->AdapterObject
,
3305 DataVA
+ TotalLength
,
3306 &ScatterGatherList
->Length
,
3309 TotalLength
+= ScatterGatherList
->Length
;
3310 ScatterGatherList
++;
3313 /* Schedule an active request */
3314 InterlockedIncrement(&DeviceExtension
->ActiveRequestCounter
);
3315 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &CurrentIrql
);
3316 KeSynchronizeExecution(DeviceExtension
->Interrupt
[0],
3317 ScsiPortStartPacket
,
3319 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, CurrentIrql
);
3321 return DeallocateObjectKeepRegisters
;
3324 static PSCSI_PORT_LUN_EXTENSION
3325 SpiAllocateLunExtension (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
)
3327 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3328 ULONG LunExtensionSize
;
3330 DPRINT("SpiAllocateLunExtension (%p)\n",
3333 /* Round LunExtensionSize first to the sizeof LONGLONG */
3334 LunExtensionSize
= (DeviceExtension
->LunExtensionSize
+
3335 sizeof(LONGLONG
) - 1) & ~(sizeof(LONGLONG
) - 1);
3337 LunExtensionSize
+= sizeof(SCSI_PORT_LUN_EXTENSION
);
3338 DPRINT("LunExtensionSize %lu\n", LunExtensionSize
);
3340 LunExtension
= ExAllocatePoolWithTag(NonPagedPool
, LunExtensionSize
, TAG_SCSIPORT
);
3341 if (LunExtension
== NULL
)
3343 DPRINT1("Out of resources!\n");
3347 /* Zero everything */
3348 RtlZeroMemory(LunExtension
, LunExtensionSize
);
3350 /* Initialize a list of requests */
3351 InitializeListHead(&LunExtension
->SrbInfo
.Requests
);
3353 /* Initialize timeout counter */
3354 LunExtension
->RequestTimeout
= -1;
3356 /* Set maximum queue size */
3357 LunExtension
->MaxQueueCount
= 256;
3359 /* Initialize request queue */
3360 KeInitializeDeviceQueue (&LunExtension
->DeviceQueue
);
3362 return LunExtension
;
3365 static PSCSI_PORT_LUN_EXTENSION
3366 SpiGetLunExtension (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
3371 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3373 DPRINT("SpiGetLunExtension(%p %u %u %u) called\n",
3374 DeviceExtension
, PathId
, TargetId
, Lun
);
3376 /* Get appropriate list */
3377 LunExtension
= DeviceExtension
->LunExtensionList
[(TargetId
+ Lun
) % LUS_NUMBER
];
3379 /* Iterate it until we find what we need */
3380 while (LunExtension
)
3382 if (LunExtension
->TargetId
== TargetId
&&
3383 LunExtension
->Lun
== Lun
&&
3384 LunExtension
->PathId
== PathId
)
3386 /* All matches, return */
3387 return LunExtension
;
3390 /* Advance to the next item */
3391 LunExtension
= LunExtension
->Next
;
3394 /* We did not find anything */
3395 DPRINT("Nothing found\n");
3399 static PSCSI_REQUEST_BLOCK_INFO
3400 SpiAllocateSrbStructures(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
3401 PSCSI_PORT_LUN_EXTENSION LunExtension
,
3402 PSCSI_REQUEST_BLOCK Srb
)
3405 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
3407 /* Spinlock must be held while this function executes */
3408 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
3410 /* Allocate SRB data structure */
3411 if (DeviceExtension
->NeedSrbDataAlloc
)
3413 /* Treat the abort request in a special way */
3414 if (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
)
3416 SrbInfo
= SpiGetSrbData(DeviceExtension
,
3422 else if (Srb
->SrbFlags
&
3423 (SRB_FLAGS_QUEUE_ACTION_ENABLE
| SRB_FLAGS_NO_QUEUE_FREEZE
) &&
3424 !(Srb
->SrbFlags
& SRB_FLAGS_DISABLE_DISCONNECT
)
3427 /* Do not process tagged commands if need request sense is set */
3428 if (LunExtension
->Flags
& LUNEX_NEED_REQUEST_SENSE
)
3430 ASSERT(!(LunExtension
->Flags
& LUNEX_REQUEST_PENDING
));
3432 LunExtension
->PendingRequest
= Srb
->OriginalRequest
;
3433 LunExtension
->Flags
|= LUNEX_REQUEST_PENDING
| SCSI_PORT_LU_ACTIVE
;
3435 /* Release the spinlock and return */
3436 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3440 ASSERT(LunExtension
->SrbInfo
.Srb
== NULL
);
3441 SrbInfo
= DeviceExtension
->FreeSrbInfo
;
3443 if (SrbInfo
== NULL
)
3445 /* No SRB structures left in the list. We have to leave
3446 and wait while we are called again */
3448 DeviceExtension
->Flags
|= SCSI_PORT_REQUEST_PENDING
;
3449 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3453 DeviceExtension
->FreeSrbInfo
= (PSCSI_REQUEST_BLOCK_INFO
)SrbInfo
->Requests
.Flink
;
3455 /* QueueTag must never be 0, so +1 to it */
3456 Srb
->QueueTag
= (UCHAR
)(SrbInfo
- DeviceExtension
->SrbInfo
) + 1;
3460 /* Usual untagged command */
3462 (!IsListEmpty(&LunExtension
->SrbInfo
.Requests
) ||
3463 LunExtension
->Flags
& LUNEX_NEED_REQUEST_SENSE
) &&
3464 !(Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
)
3467 /* Mark it as pending and leave */
3468 ASSERT(!(LunExtension
->Flags
& LUNEX_REQUEST_PENDING
));
3469 LunExtension
->Flags
|= LUNEX_REQUEST_PENDING
| SCSI_PORT_LU_ACTIVE
;
3470 LunExtension
->PendingRequest
= Srb
->OriginalRequest
;
3472 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3476 Srb
->QueueTag
= SP_UNTAGGED
;
3477 SrbInfo
= &LunExtension
->SrbInfo
;
3482 Srb
->QueueTag
= SP_UNTAGGED
;
3483 SrbInfo
= &LunExtension
->SrbInfo
;
3486 /* Allocate SRB extension structure */
3487 if (DeviceExtension
->NeedSrbExtensionAlloc
)
3489 /* Check the list of free extensions */
3490 SrbExtension
= DeviceExtension
->FreeSrbExtensions
;
3492 /* If no free extensions... */
3493 if (SrbExtension
== NULL
)
3496 if (Srb
->Function
!= SRB_FUNCTION_ABORT_COMMAND
&&
3497 Srb
->QueueTag
!= SP_UNTAGGED
)
3499 SrbInfo
->Requests
.Blink
= NULL
;
3500 SrbInfo
->Requests
.Flink
= (PLIST_ENTRY
)DeviceExtension
->FreeSrbInfo
;
3501 DeviceExtension
->FreeSrbInfo
= SrbInfo
;
3504 /* Return, in order to be called again later */
3505 DeviceExtension
->Flags
|= SCSI_PORT_REQUEST_PENDING
;
3506 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3510 /* Remove that free SRB extension from the list (since
3511 we're going to use it) */
3512 DeviceExtension
->FreeSrbExtensions
= *((PVOID
*)SrbExtension
);
3514 /* Spinlock can be released now */
3515 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3517 Srb
->SrbExtension
= SrbExtension
;
3519 if (Srb
->SenseInfoBuffer
!= NULL
&&
3520 DeviceExtension
->SupportsAutoSense
)
3522 /* Store pointer to the SenseInfo buffer */
3523 SrbInfo
->SaveSenseRequest
= Srb
->SenseInfoBuffer
;
3525 /* Does data fit the buffer? */
3526 if (Srb
->SenseInfoBufferLength
> sizeof(SENSE_DATA
))
3528 /* No, disabling autosense at all */
3529 Srb
->SrbFlags
|= SRB_FLAGS_DISABLE_AUTOSENSE
;
3533 /* Yes, update the buffer pointer */
3534 Srb
->SenseInfoBuffer
= SrbExtension
+ DeviceExtension
->SrbExtensionSize
;
3541 Srb
->SrbExtension
= NULL
;
3542 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3550 SpiSendInquiry (IN PDEVICE_OBJECT DeviceObject
,
3551 IN PSCSI_LUN_INFO LunInfo
)
3553 IO_STATUS_BLOCK IoStatusBlock
;
3554 PIO_STACK_LOCATION IrpStack
;
3559 PINQUIRYDATA InquiryBuffer
;
3560 PSENSE_DATA SenseBuffer
;
3561 BOOLEAN KeepTrying
= TRUE
;
3562 ULONG RetryCount
= 0;
3563 SCSI_REQUEST_BLOCK Srb
;
3565 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3566 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
3568 DPRINT("SpiSendInquiry() called\n");
3570 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
3572 InquiryBuffer
= ExAllocatePoolWithTag(NonPagedPool
, INQUIRYDATABUFFERSIZE
, TAG_SCSIPORT
);
3573 if (InquiryBuffer
== NULL
)
3574 return STATUS_INSUFFICIENT_RESOURCES
;
3576 SenseBuffer
= ExAllocatePoolWithTag(NonPagedPool
, SENSE_BUFFER_SIZE
, TAG_SCSIPORT
);
3577 if (SenseBuffer
== NULL
)
3579 ExFreePoolWithTag(InquiryBuffer
, TAG_SCSIPORT
);
3580 return STATUS_INSUFFICIENT_RESOURCES
;
3585 /* Initialize event for waiting */
3586 KeInitializeEvent(&Event
,
3591 Irp
= IoBuildDeviceIoControlRequest(IOCTL_SCSI_EXECUTE_IN
,
3596 INQUIRYDATABUFFERSIZE
,
3602 DPRINT("IoBuildDeviceIoControlRequest() failed\n");
3605 Status
= STATUS_INSUFFICIENT_RESOURCES
;
3611 RtlZeroMemory(&Srb
, sizeof(SCSI_REQUEST_BLOCK
));
3613 Srb
.Length
= sizeof(SCSI_REQUEST_BLOCK
);
3614 Srb
.OriginalRequest
= Irp
;
3615 Srb
.PathId
= LunInfo
->PathId
;
3616 Srb
.TargetId
= LunInfo
->TargetId
;
3617 Srb
.Lun
= LunInfo
->Lun
;
3618 Srb
.Function
= SRB_FUNCTION_EXECUTE_SCSI
;
3619 Srb
.SrbFlags
= SRB_FLAGS_DATA_IN
| SRB_FLAGS_DISABLE_SYNCH_TRANSFER
;
3620 Srb
.TimeOutValue
= 4;
3623 Srb
.SenseInfoBuffer
= SenseBuffer
;
3624 Srb
.SenseInfoBufferLength
= SENSE_BUFFER_SIZE
;
3626 Srb
.DataBuffer
= InquiryBuffer
;
3627 Srb
.DataTransferLength
= INQUIRYDATABUFFERSIZE
;
3629 /* Attach Srb to the Irp */
3630 IrpStack
= IoGetNextIrpStackLocation (Irp
);
3631 IrpStack
->Parameters
.Scsi
.Srb
= &Srb
;
3634 Cdb
= (PCDB
)Srb
.Cdb
;
3635 Cdb
->CDB6INQUIRY
.OperationCode
= SCSIOP_INQUIRY
;
3636 Cdb
->CDB6INQUIRY
.LogicalUnitNumber
= LunInfo
->Lun
;
3637 Cdb
->CDB6INQUIRY
.AllocationLength
= INQUIRYDATABUFFERSIZE
;
3639 /* Call the driver */
3640 Status
= IoCallDriver(DeviceObject
, Irp
);
3642 /* Wait for it to complete */
3643 if (Status
== STATUS_PENDING
)
3645 DPRINT("SpiSendInquiry(): Waiting for the driver to process request...\n");
3646 KeWaitForSingleObject(&Event
,
3651 Status
= IoStatusBlock
.Status
;
3654 DPRINT("SpiSendInquiry(): Request processed by driver, status = 0x%08X\n", Status
);
3656 if (SRB_STATUS(Srb
.SrbStatus
) == SRB_STATUS_SUCCESS
)
3658 /* All fine, copy data over */
3659 RtlCopyMemory(LunInfo
->InquiryData
,
3661 INQUIRYDATABUFFERSIZE
);
3664 Status
= STATUS_SUCCESS
;
3669 DPRINT("Inquiry SRB failed with SrbStatus 0x%08X\n", Srb
.SrbStatus
);
3671 /* Check if the queue is frozen */
3672 if (Srb
.SrbStatus
& SRB_STATUS_QUEUE_FROZEN
)
3674 /* Something weird happened, deal with it (unfreeze the queue) */
3677 DPRINT("SpiSendInquiry(): the queue is frozen at TargetId %d\n", Srb
.TargetId
);
3679 LunExtension
= SpiGetLunExtension(DeviceExtension
,
3684 /* Clear frozen flag */
3685 LunExtension
->Flags
&= ~LUNEX_FROZEN_QUEUE
;
3687 /* Acquire the spinlock */
3688 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
3690 /* Process the request */
3691 SpiGetNextRequestFromLun(DeviceObject
->DeviceExtension
, LunExtension
);
3693 /* SpiGetNextRequestFromLun() releases the spinlock,
3694 so we just lower irql back to what it was before */
3698 /* Check if data overrun happened */
3699 if (SRB_STATUS(Srb
.SrbStatus
) == SRB_STATUS_DATA_OVERRUN
)
3701 DPRINT("Data overrun at TargetId %d\n", LunInfo
->TargetId
);
3703 /* Nothing dramatic, just copy data, but limiting the size */
3704 RtlCopyMemory(LunInfo
->InquiryData
,
3706 (Srb
.DataTransferLength
> INQUIRYDATABUFFERSIZE
) ?
3707 INQUIRYDATABUFFERSIZE
: Srb
.DataTransferLength
);
3710 Status
= STATUS_SUCCESS
;
3713 else if ((Srb
.SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
) &&
3714 SenseBuffer
->SenseKey
== SCSI_SENSE_ILLEGAL_REQUEST
)
3716 /* LUN is not valid, but some device responds there.
3717 Mark it as invalid anyway */
3720 Status
= STATUS_INVALID_DEVICE_REQUEST
;
3725 /* Retry a couple of times if no timeout happened */
3726 if ((RetryCount
< 2) &&
3727 (SRB_STATUS(Srb
.SrbStatus
) != SRB_STATUS_NO_DEVICE
) &&
3728 (SRB_STATUS(Srb
.SrbStatus
) != SRB_STATUS_SELECTION_TIMEOUT
))
3735 /* That's all, quit the loop */
3738 /* Set status according to SRB status */
3739 if (SRB_STATUS(Srb
.SrbStatus
) == SRB_STATUS_BAD_FUNCTION
||
3740 SRB_STATUS(Srb
.SrbStatus
) == SRB_STATUS_BAD_SRB_BLOCK_LENGTH
)
3742 Status
= STATUS_INVALID_DEVICE_REQUEST
;
3746 Status
= STATUS_IO_DEVICE_ERROR
;
3753 ExFreePoolWithTag(InquiryBuffer
, TAG_SCSIPORT
);
3754 ExFreePoolWithTag(SenseBuffer
, TAG_SCSIPORT
);
3756 DPRINT("SpiSendInquiry() done with Status 0x%08X\n", Status
);
3762 /* Scans all SCSI buses */
3764 SpiScanAdapter(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
)
3766 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3770 PSCSI_BUS_SCAN_INFO BusScanInfo
;
3771 PSCSI_LUN_INFO LastLunInfo
, LunInfo
, LunInfoExists
;
3772 BOOLEAN DeviceExists
;
3777 DPRINT("SpiScanAdapter() called\n");
3779 /* Scan all buses */
3780 for (Bus
= 0; Bus
< DeviceExtension
->BusNum
; Bus
++)
3782 DPRINT(" Scanning bus %d\n", Bus
);
3785 /* Get pointer to the scan information */
3786 BusScanInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
];
3790 /* Find the last LUN info in the list */
3791 LunInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LunInfo
;
3792 LastLunInfo
= LunInfo
;
3794 while (LunInfo
!= NULL
)
3796 LastLunInfo
= LunInfo
;
3797 LunInfo
= LunInfo
->Next
;
3802 /* We need to allocate this buffer */
3803 BusScanInfo
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(SCSI_BUS_SCAN_INFO
), TAG_SCSIPORT
);
3807 DPRINT1("Out of resources!\n");
3811 /* Store the pointer in the BusScanInfo array */
3812 DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
] = BusScanInfo
;
3814 /* Fill this struct (length and bus ids for now) */
3815 BusScanInfo
->Length
= sizeof(SCSI_BUS_SCAN_INFO
);
3816 BusScanInfo
->LogicalUnitsCount
= 0;
3817 BusScanInfo
->BusIdentifier
= DeviceExtension
->PortConfig
->InitiatorBusId
[Bus
];
3818 BusScanInfo
->LunInfo
= NULL
;
3820 /* Set pointer to the last LUN info to NULL */
3824 /* Create LUN information structure */
3825 LunInfo
= ExAllocatePoolWithTag(PagedPool
, sizeof(SCSI_LUN_INFO
), TAG_SCSIPORT
);
3827 if (LunInfo
== NULL
)
3829 DPRINT1("Out of resources!\n");
3833 RtlZeroMemory(LunInfo
, sizeof(SCSI_LUN_INFO
));
3835 /* Create LunExtension */
3836 LunExtension
= SpiAllocateLunExtension (DeviceExtension
);
3838 /* And send INQUIRY to every target */
3839 for (Target
= 0; Target
< DeviceExtension
->PortConfig
->MaximumNumberOfTargets
; Target
++)
3841 /* TODO: Support scan bottom-up */
3843 /* Skip if it's the same address */
3844 if (Target
== BusScanInfo
->BusIdentifier
)
3847 /* Try to find an existing device here */
3848 DeviceExists
= FALSE
;
3849 LunInfoExists
= BusScanInfo
->LunInfo
;
3851 /* Find matching address on this bus */
3852 while (LunInfoExists
)
3854 if (LunInfoExists
->TargetId
== Target
)
3856 DeviceExists
= TRUE
;
3860 /* Advance to the next one */
3861 LunInfoExists
= LunInfoExists
->Next
;
3864 /* No need to bother rescanning, since we already did that before */
3868 /* Scan all logical units */
3869 for (Lun
= 0; Lun
< SCSI_MAXIMUM_LOGICAL_UNITS
; Lun
++)
3871 if ((!LunExtension
) || (!LunInfo
))
3874 /* Add extension to the list */
3875 Hint
= (Target
+ Lun
) % LUS_NUMBER
;
3876 LunExtension
->Next
= DeviceExtension
->LunExtensionList
[Hint
];
3877 DeviceExtension
->LunExtensionList
[Hint
] = LunExtension
;
3879 /* Fill Path, Target, Lun fields */
3880 LunExtension
->PathId
= LunInfo
->PathId
= (UCHAR
)Bus
;
3881 LunExtension
->TargetId
= LunInfo
->TargetId
= (UCHAR
) Target
;
3882 LunExtension
->Lun
= LunInfo
->Lun
= (UCHAR
)Lun
;
3884 /* Set flag to prevent race conditions */
3885 LunExtension
->Flags
|= SCSI_PORT_SCAN_IN_PROGRESS
;
3887 /* Zero LU extension contents */
3888 if (DeviceExtension
->LunExtensionSize
)
3890 RtlZeroMemory(LunExtension
+ 1,
3891 DeviceExtension
->LunExtensionSize
);
3894 /* Finally send the inquiry command */
3895 Status
= SpiSendInquiry(DeviceExtension
->DeviceObject
, LunInfo
);
3897 if (NT_SUCCESS(Status
))
3899 /* Let's see if we really found a device */
3900 PINQUIRYDATA InquiryData
= (PINQUIRYDATA
)LunInfo
->InquiryData
;
3902 /* Check if this device is unsupported */
3903 if (InquiryData
->DeviceTypeQualifier
== DEVICE_QUALIFIER_NOT_SUPPORTED
)
3905 DeviceExtension
->LunExtensionList
[Hint
] =
3906 DeviceExtension
->LunExtensionList
[Hint
]->Next
;
3911 /* Clear the "in scan" flag */
3912 LunExtension
->Flags
&= ~SCSI_PORT_SCAN_IN_PROGRESS
;
3914 DPRINT("SpiScanAdapter(): Found device of type %d at bus %d tid %d lun %d\n",
3915 InquiryData
->DeviceType
, Bus
, Target
, Lun
);
3917 /* Add this info to the linked list */
3918 LunInfo
->Next
= NULL
;
3920 LastLunInfo
->Next
= LunInfo
;
3922 BusScanInfo
->LunInfo
= LunInfo
;
3924 /* Store the last LUN info */
3925 LastLunInfo
= LunInfo
;
3927 /* Store DeviceObject */
3928 LunInfo
->DeviceObject
= DeviceExtension
->DeviceObject
;
3930 /* Allocate another buffer */
3931 LunInfo
= ExAllocatePoolWithTag(PagedPool
, sizeof(SCSI_LUN_INFO
), TAG_SCSIPORT
);
3935 DPRINT1("Out of resources!\n");
3939 RtlZeroMemory(LunInfo
, sizeof(SCSI_LUN_INFO
));
3941 /* Create a new LU extension */
3942 LunExtension
= SpiAllocateLunExtension(DeviceExtension
);
3948 /* Remove this LUN from the list */
3949 DeviceExtension
->LunExtensionList
[Hint
] =
3950 DeviceExtension
->LunExtensionList
[Hint
]->Next
;
3952 /* Decide whether we are continuing or not */
3953 if (Status
== STATUS_INVALID_DEVICE_REQUEST
)
3961 /* Free allocated buffers */
3963 ExFreePool(LunExtension
);
3966 ExFreePool(LunInfo
);
3968 /* Sum what we found */
3969 BusScanInfo
->LogicalUnitsCount
+= (UCHAR
) DevicesFound
;
3970 DPRINT(" Found %d devices on bus %d\n", DevicesFound
, Bus
);
3973 DPRINT ("SpiScanAdapter() done\n");
3978 SpiGetInquiryData(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
3981 ULONG InquiryDataSize
;
3982 PSCSI_LUN_INFO LunInfo
;
3983 ULONG BusCount
, LunCount
, Length
;
3984 PIO_STACK_LOCATION IrpStack
;
3985 PSCSI_ADAPTER_BUS_INFO AdapterBusInfo
;
3986 PSCSI_INQUIRY_DATA InquiryData
;
3987 PSCSI_BUS_DATA BusData
;
3991 DPRINT("SpiGetInquiryData() called\n");
3993 /* Get pointer to the buffer */
3994 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
3995 Buffer
= Irp
->AssociatedIrp
.SystemBuffer
;
3997 /* Initialize bus and LUN counters */
3998 BusCount
= DeviceExtension
->BusesConfig
->NumberOfBuses
;
4001 /* Calculate total number of LUNs */
4002 for (Bus
= 0; Bus
< BusCount
; Bus
++)
4003 LunCount
+= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LogicalUnitsCount
;
4005 /* Calculate size of inquiry data, rounding up to sizeof(ULONG) */
4007 ((sizeof(SCSI_INQUIRY_DATA
) - 1 + INQUIRYDATABUFFERSIZE
+
4008 sizeof(ULONG
) - 1) & ~(sizeof(ULONG
) - 1));
4010 /* Calculate data size */
4011 Length
= sizeof(SCSI_ADAPTER_BUS_INFO
) + (BusCount
- 1) *
4012 sizeof(SCSI_BUS_DATA
);
4014 Length
+= InquiryDataSize
* LunCount
;
4016 /* Check, if all data is going to fit into provided buffer */
4017 if (IrpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
< Length
)
4019 Irp
->IoStatus
.Status
= STATUS_BUFFER_TOO_SMALL
;
4020 return STATUS_BUFFER_TOO_SMALL
;
4023 /* Store data size in the IRP */
4024 Irp
->IoStatus
.Information
= Length
;
4026 DPRINT("Data size: %lu\n", Length
);
4028 AdapterBusInfo
= (PSCSI_ADAPTER_BUS_INFO
)Buffer
;
4030 AdapterBusInfo
->NumberOfBuses
= (UCHAR
)BusCount
;
4032 /* Point InquiryData to the corresponding place inside Buffer */
4033 InquiryData
= (PSCSI_INQUIRY_DATA
)(Buffer
+ sizeof(SCSI_ADAPTER_BUS_INFO
) +
4034 (BusCount
- 1) * sizeof(SCSI_BUS_DATA
));
4037 for (Bus
= 0; Bus
< BusCount
; Bus
++)
4039 BusData
= &AdapterBusInfo
->BusData
[Bus
];
4041 /* Calculate and save an offset of the inquiry data */
4042 BusData
->InquiryDataOffset
= (ULONG
)((PUCHAR
)InquiryData
- Buffer
);
4044 /* Get a pointer to the LUN information structure */
4045 LunInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LunInfo
;
4047 /* Store Initiator Bus Id */
4048 BusData
->InitiatorBusId
=
4049 DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->BusIdentifier
;
4051 /* Store LUN count */
4052 BusData
->NumberOfLogicalUnits
=
4053 DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LogicalUnitsCount
;
4056 while (LunInfo
!= NULL
)
4058 DPRINT("(Bus %lu Target %lu Lun %lu)\n",
4059 Bus
, LunInfo
->TargetId
, LunInfo
->Lun
);
4061 /* Fill InquiryData with values */
4062 InquiryData
->PathId
= LunInfo
->PathId
;
4063 InquiryData
->TargetId
= LunInfo
->TargetId
;
4064 InquiryData
->Lun
= LunInfo
->Lun
;
4065 InquiryData
->InquiryDataLength
= INQUIRYDATABUFFERSIZE
;
4066 InquiryData
->DeviceClaimed
= LunInfo
->DeviceClaimed
;
4067 InquiryData
->NextInquiryDataOffset
=
4068 (ULONG
)((PUCHAR
)InquiryData
+ InquiryDataSize
- Buffer
);
4070 /* Copy data in it */
4071 RtlCopyMemory(InquiryData
->InquiryData
,
4072 LunInfo
->InquiryData
,
4073 INQUIRYDATABUFFERSIZE
);
4075 /* Move to the next LUN */
4076 LunInfo
= LunInfo
->Next
;
4077 InquiryData
= (PSCSI_INQUIRY_DATA
) ((PCHAR
)InquiryData
+ InquiryDataSize
);
4080 /* Either mark the end, or set offset to 0 */
4081 if (BusData
->NumberOfLogicalUnits
!= 0)
4082 ((PSCSI_INQUIRY_DATA
) ((PCHAR
) InquiryData
- InquiryDataSize
))->NextInquiryDataOffset
= 0;
4084 BusData
->InquiryDataOffset
= 0;
4087 /* Finish with success */
4088 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
4089 return STATUS_SUCCESS
;
4092 static PSCSI_REQUEST_BLOCK_INFO
4093 SpiGetSrbData(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
4099 PSCSI_PORT_LUN_EXTENSION LunExtension
;
4101 if (QueueTag
== SP_UNTAGGED
)
4103 /* Untagged request, get LU and return pointer to SrbInfo */
4104 LunExtension
= SpiGetLunExtension(DeviceExtension
,
4109 /* Return NULL in case of error */
4113 /* Return the pointer to SrbInfo */
4114 return &LunExtension
->SrbInfo
;
4118 /* Make sure the tag is valid, if it is - return the data */
4119 if (QueueTag
> DeviceExtension
->SrbDataCount
|| QueueTag
< 1)
4122 return &DeviceExtension
->SrbInfo
[QueueTag
-1];
4127 SpiSendRequestSense(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
4128 IN PSCSI_REQUEST_BLOCK InitialSrb
)
4130 PSCSI_REQUEST_BLOCK Srb
;
4133 PIO_STACK_LOCATION IrpStack
;
4134 LARGE_INTEGER LargeInt
;
4137 DPRINT("SpiSendRequestSense() entered, InitialSrb %p\n", InitialSrb
);
4140 Srb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(SCSI_REQUEST_BLOCK
) + sizeof(PVOID
), TAG_SCSIPORT
);
4141 RtlZeroMemory(Srb
, sizeof(SCSI_REQUEST_BLOCK
));
4144 LargeInt
.QuadPart
= (LONGLONG
) 1;
4145 Irp
= IoBuildAsynchronousFsdRequest(IRP_MJ_READ
,
4146 DeviceExtension
->DeviceObject
,
4147 InitialSrb
->SenseInfoBuffer
,
4148 InitialSrb
->SenseInfoBufferLength
,
4152 IoSetCompletionRoutine(Irp
,
4153 (PIO_COMPLETION_ROUTINE
)SpiCompletionRoutine
,
4161 DPRINT("SpiSendRequestSense() failed, Srb %p\n", Srb
);
4165 IrpStack
= IoGetNextIrpStackLocation(Irp
);
4166 IrpStack
->MajorFunction
= IRP_MJ_SCSI
;
4168 /* Put Srb address into Irp... */
4169 IrpStack
->Parameters
.Others
.Argument1
= (PVOID
)Srb
;
4171 /* ...and vice versa */
4172 Srb
->OriginalRequest
= Irp
;
4175 Ptr
= (PVOID
*)(Srb
+1);
4178 /* Build CDB for REQUEST SENSE */
4180 Cdb
= (PCDB
)Srb
->Cdb
;
4182 Cdb
->CDB6INQUIRY
.OperationCode
= SCSIOP_REQUEST_SENSE
;
4183 Cdb
->CDB6INQUIRY
.LogicalUnitNumber
= 0;
4184 Cdb
->CDB6INQUIRY
.Reserved1
= 0;
4185 Cdb
->CDB6INQUIRY
.PageCode
= 0;
4186 Cdb
->CDB6INQUIRY
.IReserved
= 0;
4187 Cdb
->CDB6INQUIRY
.AllocationLength
= (UCHAR
)InitialSrb
->SenseInfoBufferLength
;
4188 Cdb
->CDB6INQUIRY
.Control
= 0;
4191 Srb
->TargetId
= InitialSrb
->TargetId
;
4192 Srb
->Lun
= InitialSrb
->Lun
;
4193 Srb
->PathId
= InitialSrb
->PathId
;
4195 Srb
->Function
= SRB_FUNCTION_EXECUTE_SCSI
;
4196 Srb
->Length
= sizeof(SCSI_REQUEST_BLOCK
);
4198 /* Timeout will be 2 seconds */
4199 Srb
->TimeOutValue
= 2;
4201 /* No auto request sense */
4202 Srb
->SenseInfoBufferLength
= 0;
4203 Srb
->SenseInfoBuffer
= NULL
;
4205 /* Set necessary flags */
4206 Srb
->SrbFlags
= SRB_FLAGS_DATA_IN
| SRB_FLAGS_BYPASS_FROZEN_QUEUE
|
4207 SRB_FLAGS_DISABLE_DISCONNECT
;
4209 /* Transfer disable synch transfer flag */
4210 if (InitialSrb
->SrbFlags
& SRB_FLAGS_DISABLE_SYNCH_TRANSFER
)
4211 Srb
->SrbFlags
|= SRB_FLAGS_DISABLE_SYNCH_TRANSFER
;
4213 Srb
->DataBuffer
= InitialSrb
->SenseInfoBuffer
;
4215 /* Fill the transfer length */
4216 Srb
->DataTransferLength
= InitialSrb
->SenseInfoBufferLength
;
4218 /* Clear statuses */
4219 Srb
->ScsiStatus
= Srb
->SrbStatus
= 0;
4222 /* Call the driver */
4223 (VOID
)IoCallDriver(DeviceExtension
->DeviceObject
, Irp
);
4225 DPRINT("SpiSendRequestSense() done\n");
4232 SpiProcessCompletedRequest(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
4233 IN PSCSI_REQUEST_BLOCK_INFO SrbInfo
,
4234 OUT PBOOLEAN NeedToCallStartIo
)
4236 PSCSI_REQUEST_BLOCK Srb
;
4237 PSCSI_PORT_LUN_EXTENSION LunExtension
;
4240 //ULONG SequenceNumber;
4243 Irp
= Srb
->OriginalRequest
;
4245 /* Get Lun extension */
4246 LunExtension
= SpiGetLunExtension(DeviceExtension
,
4251 if (Srb
->SrbFlags
& SRB_FLAGS_UNSPECIFIED_DIRECTION
&&
4252 DeviceExtension
->MapBuffers
&&
4255 /* MDL is shared if transfer is broken into smaller parts */
4256 Srb
->DataBuffer
= (PCCHAR
)MmGetMdlVirtualAddress(Irp
->MdlAddress
) +
4257 ((PCCHAR
)Srb
->DataBuffer
- SrbInfo
->DataOffset
);
4259 /* In case of data going in, flush the buffers */
4260 if (Srb
->SrbFlags
& SRB_FLAGS_DATA_IN
)
4262 KeFlushIoBuffers(Irp
->MdlAddress
,
4268 /* Flush adapter if needed */
4269 if (SrbInfo
->BaseOfMapRegister
)
4271 /* TODO: Implement */
4275 /* Clear the request */
4276 SrbInfo
->Srb
= NULL
;
4278 /* If disconnect is disabled... */
4279 if (Srb
->SrbFlags
& SRB_FLAGS_DISABLE_DISCONNECT
)
4281 /* Acquire the spinlock since we mess with flags */
4282 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4284 /* Set corresponding flag */
4285 DeviceExtension
->Flags
|= SCSI_PORT_DISCONNECT_ALLOWED
;
4287 /* Clear the timer if needed */
4288 if (!(DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_RESET
))
4289 DeviceExtension
->TimerCount
= -1;
4291 /* Spinlock is not needed anymore */
4292 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4294 if (!(DeviceExtension
->Flags
& SCSI_PORT_REQUEST_PENDING
) &&
4295 !(DeviceExtension
->Flags
& SCSI_PORT_DEVICE_BUSY
) &&
4296 !(*NeedToCallStartIo
))
4298 /* We're not busy, but we have a request pending */
4299 IoStartNextPacket(DeviceExtension
->DeviceObject
, FALSE
);
4303 /* Scatter/gather */
4304 if (Srb
->SrbFlags
& SRB_FLAGS_SGLIST_FROM_POOL
)
4306 /* TODO: Implement */
4310 /* Acquire spinlock (we're freeing SrbExtension) */
4311 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4313 /* Free it (if needed) */
4314 if (Srb
->SrbExtension
)
4316 if (Srb
->SenseInfoBuffer
!= NULL
&& DeviceExtension
->SupportsAutoSense
)
4318 ASSERT(Srb
->SenseInfoBuffer
== NULL
|| SrbInfo
->SaveSenseRequest
!= NULL
);
4320 if (Srb
->SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
)
4322 /* Copy sense data to the buffer */
4323 RtlCopyMemory(SrbInfo
->SaveSenseRequest
,
4324 Srb
->SenseInfoBuffer
,
4325 Srb
->SenseInfoBufferLength
);
4328 /* And restore the pointer */
4329 Srb
->SenseInfoBuffer
= SrbInfo
->SaveSenseRequest
;
4332 /* Put it into the free srb extensions list */
4333 *((PVOID
*)Srb
->SrbExtension
) = DeviceExtension
->FreeSrbExtensions
;
4334 DeviceExtension
->FreeSrbExtensions
= Srb
->SrbExtension
;
4337 /* Save transfer length in the IRP */
4338 Irp
->IoStatus
.Information
= Srb
->DataTransferLength
;
4340 //SequenceNumber = SrbInfo->SequenceNumber;
4341 SrbInfo
->SequenceNumber
= 0;
4343 /* Decrement the queue count */
4344 LunExtension
->QueueCount
--;
4346 /* Free Srb, if needed*/
4347 if (Srb
->QueueTag
!= SP_UNTAGGED
)
4349 /* Put it into the free list */
4350 SrbInfo
->Requests
.Blink
= NULL
;
4351 SrbInfo
->Requests
.Flink
= (PLIST_ENTRY
)DeviceExtension
->FreeSrbInfo
;
4352 DeviceExtension
->FreeSrbInfo
= SrbInfo
;
4355 /* SrbInfo is not used anymore */
4358 if (DeviceExtension
->Flags
& SCSI_PORT_REQUEST_PENDING
)
4360 /* Clear the flag */
4361 DeviceExtension
->Flags
&= ~SCSI_PORT_REQUEST_PENDING
;
4363 /* Note the caller about StartIo */
4364 *NeedToCallStartIo
= TRUE
;
4367 if (SRB_STATUS(Srb
->SrbStatus
) == SRB_STATUS_SUCCESS
)
4369 /* Start the packet */
4370 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
4372 if (!(Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
) &&
4373 LunExtension
->RequestTimeout
== -1)
4375 /* Start the next packet */
4376 SpiGetNextRequestFromLun(DeviceExtension
, LunExtension
);
4380 /* Release the spinlock */
4381 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4384 DPRINT("IoCompleting request IRP 0x%p\n", Irp
);
4386 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
4388 /* Decrement number of active requests, and analyze the result */
4389 Result
= InterlockedDecrement(&DeviceExtension
->ActiveRequestCounter
);
4392 !DeviceExtension
->MapRegisters
&&
4393 DeviceExtension
->AdapterObject
!= NULL
)
4395 /* Nullify map registers */
4396 DeviceExtension
->MapRegisterBase
= NULL
;
4397 IoFreeAdapterChannel(DeviceExtension
->AdapterObject
);
4400 /* Exit, we're done */
4404 /* Decrement number of active requests, and analyze the result */
4405 Result
= InterlockedDecrement(&DeviceExtension
->ActiveRequestCounter
);
4408 !DeviceExtension
->MapRegisters
&&
4409 DeviceExtension
->AdapterObject
!= NULL
)
4411 /* Result is negative, so this is a slave, free map registers */
4412 DeviceExtension
->MapRegisterBase
= NULL
;
4413 IoFreeAdapterChannel(DeviceExtension
->AdapterObject
);
4416 /* Convert status */
4417 Irp
->IoStatus
.Status
= SpiStatusSrbToNt(Srb
->SrbStatus
);
4419 /* It's not a bypass, it's busy or the queue is full? */
4420 if ((Srb
->ScsiStatus
== SCSISTAT_BUSY
||
4421 Srb
->SrbStatus
== SRB_STATUS_BUSY
||
4422 Srb
->ScsiStatus
== SCSISTAT_QUEUE_FULL
) &&
4423 !(Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
))
4426 DPRINT("Busy SRB status %x\n", Srb
->SrbStatus
);
4428 /* Requeue, if needed */
4429 if (LunExtension
->Flags
& (LUNEX_FROZEN_QUEUE
| LUNEX_BUSY
))
4431 DPRINT("it's being requeued\n");
4433 Srb
->SrbStatus
= SRB_STATUS_PENDING
;
4434 Srb
->ScsiStatus
= 0;
4436 if (!KeInsertByKeyDeviceQueue(&LunExtension
->DeviceQueue
,
4437 &Irp
->Tail
.Overlay
.DeviceQueueEntry
,
4440 /* It's a big f.ck up if we got here */
4441 Srb
->SrbStatus
= SRB_STATUS_ERROR
;
4442 Srb
->ScsiStatus
= SCSISTAT_BUSY
;
4448 /* Release the spinlock */
4449 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4452 else if (LunExtension
->AttemptCount
++ < 20)
4454 /* LUN is still busy */
4455 Srb
->ScsiStatus
= 0;
4456 Srb
->SrbStatus
= SRB_STATUS_PENDING
;
4458 LunExtension
->BusyRequest
= Irp
;
4459 LunExtension
->Flags
|= LUNEX_BUSY
;
4461 /* Release the spinlock */
4462 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4467 /* Freeze the queue*/
4468 Srb
->SrbStatus
|= SRB_STATUS_QUEUE_FROZEN
;
4469 LunExtension
->Flags
|= LUNEX_FROZEN_QUEUE
;
4471 /* "Unfull" the queue */
4472 LunExtension
->Flags
&= ~LUNEX_FULL_QUEUE
;
4474 /* Release the spinlock */
4475 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4477 /* Return status that the device is not ready */
4478 Irp
->IoStatus
.Status
= STATUS_DEVICE_NOT_READY
;
4479 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
4485 /* Start the next request, if LUN is idle, and this is sense request */
4486 if (((Srb
->ScsiStatus
!= SCSISTAT_CHECK_CONDITION
) ||
4487 (Srb
->SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
) ||
4488 !Srb
->SenseInfoBuffer
|| !Srb
->SenseInfoBufferLength
)
4489 && (Srb
->SrbFlags
& SRB_FLAGS_NO_QUEUE_FREEZE
))
4491 if (LunExtension
->RequestTimeout
== -1)
4492 SpiGetNextRequestFromLun(DeviceExtension
, LunExtension
);
4494 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4498 /* Freeze the queue */
4499 Srb
->SrbStatus
|= SRB_STATUS_QUEUE_FROZEN
;
4500 LunExtension
->Flags
|= LUNEX_FROZEN_QUEUE
;
4502 /* Do we need a request sense? */
4503 if (Srb
->ScsiStatus
== SCSISTAT_CHECK_CONDITION
&&
4504 !(Srb
->SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
) &&
4505 Srb
->SenseInfoBuffer
&& Srb
->SenseInfoBufferLength
)
4507 /* If LUN is busy, we have to requeue it in order to allow request sense */
4508 if (LunExtension
->Flags
& LUNEX_BUSY
)
4510 DPRINT("Requeuing busy request to allow request sense\n");
4512 if (!KeInsertByKeyDeviceQueue(&LunExtension
->DeviceQueue
,
4513 &LunExtension
->BusyRequest
->Tail
.Overlay
.DeviceQueueEntry
,
4516 /* We should never get here */
4519 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4520 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
4525 /* Clear busy flags */
4526 LunExtension
->Flags
&= ~(LUNEX_FULL_QUEUE
| LUNEX_BUSY
);
4529 /* Release the spinlock */
4530 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4532 /* Send RequestSense */
4533 SpiSendRequestSense(DeviceExtension
, Srb
);
4539 /* Release the spinlock */
4540 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4543 /* Complete the request */
4544 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
4549 SpiCompletionRoutine(PDEVICE_OBJECT DeviceObject
,
4553 PSCSI_REQUEST_BLOCK Srb
= (PSCSI_REQUEST_BLOCK
)Context
;
4554 PSCSI_REQUEST_BLOCK InitialSrb
;
4557 DPRINT("SpiCompletionRoutine() entered, IRP %p \n", Irp
);
4559 if ((Srb
->Function
== SRB_FUNCTION_RESET_BUS
) ||
4560 (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
))
4562 /* Deallocate SRB and IRP and exit */
4566 return STATUS_MORE_PROCESSING_REQUIRED
;
4569 /* Get a pointer to the SRB and IRP which were initially sent */
4570 InitialSrb
= *((PVOID
*)(Srb
+1));
4571 InitialIrp
= InitialSrb
->OriginalRequest
;
4573 if ((SRB_STATUS(Srb
->SrbStatus
) == SRB_STATUS_SUCCESS
) ||
4574 (SRB_STATUS(Srb
->SrbStatus
) == SRB_STATUS_DATA_OVERRUN
))
4576 /* Sense data is OK */
4577 InitialSrb
->SrbStatus
|= SRB_STATUS_AUTOSENSE_VALID
;
4579 /* Set length to be the same */
4580 InitialSrb
->SenseInfoBufferLength
= (UCHAR
)Srb
->DataTransferLength
;
4583 /* Make sure initial SRB's queue is frozen */
4584 ASSERT(InitialSrb
->SrbStatus
& SRB_STATUS_QUEUE_FROZEN
);
4586 /* Complete this request */
4587 IoCompleteRequest(InitialIrp
, IO_DISK_INCREMENT
);
4589 /* Deallocate everything (internal) */
4592 if (Irp
->MdlAddress
!= NULL
)
4594 MmUnlockPages(Irp
->MdlAddress
);
4595 IoFreeMdl(Irp
->MdlAddress
);
4596 Irp
->MdlAddress
= NULL
;
4600 return STATUS_MORE_PROCESSING_REQUIRED
;
4603 static BOOLEAN NTAPI
4604 ScsiPortIsr(IN PKINTERRUPT Interrupt
,
4605 IN PVOID ServiceContext
)
4607 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
4609 DPRINT("ScsiPortIsr() called!\n");
4611 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)ServiceContext
;
4613 /* If interrupts are disabled - we don't expect any */
4614 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_DISABLE_INTERRUPTS
)
4617 /* Call miniport's HwInterrupt routine */
4618 if (DeviceExtension
->HwInterrupt(&DeviceExtension
->MiniPortDeviceExtension
) == FALSE
)
4620 /* This interrupt doesn't belong to us */
4624 /* If flag of notification is set - queue a DPC */
4625 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
4627 IoRequestDpc(DeviceExtension
->DeviceObject
,
4628 DeviceExtension
->CurrentIrp
,
4637 SpiSaveInterruptData(IN PVOID Context
)
4639 PSCSI_PORT_SAVE_INTERRUPT InterruptContext
= Context
;
4640 PSCSI_PORT_LUN_EXTENSION LunExtension
;
4641 PSCSI_REQUEST_BLOCK Srb
;
4642 PSCSI_REQUEST_BLOCK_INFO SrbInfo
, NextSrbInfo
;
4643 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
4646 /* Get pointer to the device extension */
4647 DeviceExtension
= InterruptContext
->DeviceExtension
;
4649 /* If we don't have anything pending - return */
4650 if (!(DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
))
4653 /* Actually save the interrupt data */
4654 *InterruptContext
->InterruptData
= DeviceExtension
->InterruptData
;
4656 /* Clear the data stored in the device extension */
4657 DeviceExtension
->InterruptData
.Flags
&=
4658 (SCSI_PORT_RESET
| SCSI_PORT_RESET_REQUEST
| SCSI_PORT_DISABLE_INTERRUPTS
);
4659 DeviceExtension
->InterruptData
.CompletedAbort
= NULL
;
4660 DeviceExtension
->InterruptData
.ReadyLun
= NULL
;
4661 DeviceExtension
->InterruptData
.CompletedRequests
= NULL
;
4663 /* Loop through the list of completed requests */
4664 SrbInfo
= InterruptContext
->InterruptData
->CompletedRequests
;
4668 /* Make sure we have SRV */
4669 ASSERT(SrbInfo
->Srb
);
4671 /* Get SRB and LunExtension */
4674 LunExtension
= SpiGetLunExtension(DeviceExtension
,
4679 /* We have to check special cases if request is unsuccessful*/
4680 if (Srb
->SrbStatus
!= SRB_STATUS_SUCCESS
)
4682 /* Check if we need request sense by a few conditions */
4683 if (Srb
->SenseInfoBuffer
&& Srb
->SenseInfoBufferLength
&&
4684 Srb
->ScsiStatus
== SCSISTAT_CHECK_CONDITION
&&
4685 !(Srb
->SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
))
4687 if (LunExtension
->Flags
& LUNEX_NEED_REQUEST_SENSE
)
4689 /* It means: we tried to send REQUEST SENSE, but failed */
4691 Srb
->ScsiStatus
= 0;
4692 Srb
->SrbStatus
= SRB_STATUS_REQUEST_SENSE_FAILED
;
4696 /* Set the corresponding flag, so that REQUEST SENSE
4698 LunExtension
->Flags
|= LUNEX_NEED_REQUEST_SENSE
;
4703 /* Check for a full queue */
4704 if (Srb
->ScsiStatus
== SCSISTAT_QUEUE_FULL
)
4706 /* TODO: Implement when it's encountered */
4711 /* Let's decide if we need to watch timeout or not */
4712 if (Srb
->QueueTag
== SP_UNTAGGED
)
4718 if (LunExtension
->SrbInfo
.Requests
.Flink
== &SrbInfo
->Requests
)
4723 /* Remove it from the queue */
4724 RemoveEntryList(&SrbInfo
->Requests
);
4729 /* We have to maintain timeout counter */
4730 if (IsListEmpty(&LunExtension
->SrbInfo
.Requests
))
4732 LunExtension
->RequestTimeout
= -1;
4736 NextSrbInfo
= CONTAINING_RECORD(LunExtension
->SrbInfo
.Requests
.Flink
,
4737 SCSI_REQUEST_BLOCK_INFO
,
4740 Srb
= NextSrbInfo
->Srb
;
4742 /* Update timeout counter */
4743 LunExtension
->RequestTimeout
= Srb
->TimeOutValue
;
4747 SrbInfo
= SrbInfo
->CompletedRequests
;
4755 SpiGetNextRequestFromLun(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
4756 IN PSCSI_PORT_LUN_EXTENSION LunExtension
)
4758 PIO_STACK_LOCATION IrpStack
;
4760 PKDEVICE_QUEUE_ENTRY Entry
;
4761 PSCSI_REQUEST_BLOCK Srb
;
4764 /* If LUN is not active or queue is more than maximum allowed */
4765 if (LunExtension
->QueueCount
>= LunExtension
->MaxQueueCount
||
4766 !(LunExtension
->Flags
& SCSI_PORT_LU_ACTIVE
))
4768 /* Release the spinlock and exit */
4769 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4773 /* Check if we can get a next request */
4774 if (LunExtension
->Flags
&
4775 (LUNEX_NEED_REQUEST_SENSE
| LUNEX_BUSY
|
4776 LUNEX_FULL_QUEUE
| LUNEX_FROZEN_QUEUE
| LUNEX_REQUEST_PENDING
))
4778 /* Pending requests can only be started if the queue is empty */
4779 if (IsListEmpty(&LunExtension
->SrbInfo
.Requests
) &&
4780 !(LunExtension
->Flags
&
4781 (LUNEX_BUSY
| LUNEX_FROZEN_QUEUE
| LUNEX_FULL_QUEUE
| LUNEX_NEED_REQUEST_SENSE
)))
4783 /* Make sure we have SRB */
4784 ASSERT(LunExtension
->SrbInfo
.Srb
== NULL
);
4786 /* Clear active and pending flags */
4787 LunExtension
->Flags
&= ~(LUNEX_REQUEST_PENDING
| SCSI_PORT_LU_ACTIVE
);
4789 /* Get next Irp, and clear pending requests list */
4790 NextIrp
= LunExtension
->PendingRequest
;
4791 LunExtension
->PendingRequest
= NULL
;
4793 /* Set attempt counter to zero */
4794 LunExtension
->AttemptCount
= 0;
4796 /* Release the spinlock */
4797 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4799 /* Start the next pending request */
4800 IoStartPacket(DeviceExtension
->DeviceObject
, NextIrp
, (PULONG
)NULL
, NULL
);
4806 /* Release the spinlock, without clearing any flags and exit */
4807 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4813 /* Reset active flag */
4814 LunExtension
->Flags
&= ~SCSI_PORT_LU_ACTIVE
;
4816 /* Set attempt counter to zero */
4817 LunExtension
->AttemptCount
= 0;
4819 /* Remove packet from the device queue */
4820 Entry
= KeRemoveByKeyDeviceQueue(&LunExtension
->DeviceQueue
, LunExtension
->SortKey
);
4824 /* Get pointer to the next irp */
4825 NextIrp
= CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.DeviceQueueEntry
);
4827 /* Get point to the SRB */
4828 IrpStack
= IoGetCurrentIrpStackLocation(NextIrp
);
4829 Srb
= (PSCSI_REQUEST_BLOCK
)IrpStack
->Parameters
.Others
.Argument1
;
4832 LunExtension
->SortKey
= Srb
->QueueSortKey
;
4833 LunExtension
->SortKey
++;
4835 /* Release the spinlock */
4836 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4838 /* Start the next pending request */
4839 IoStartPacket(DeviceExtension
->DeviceObject
, NextIrp
, (PULONG
)NULL
, NULL
);
4843 /* Release the spinlock */
4844 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4850 // ScsiPortDpcForIsr
4857 // IN PDEVICE_OBJECT DpcDeviceObject
4859 // IN PVOID DpcContext
4862 ScsiPortDpcForIsr(IN PKDPC Dpc
,
4863 IN PDEVICE_OBJECT DpcDeviceObject
,
4865 IN PVOID DpcContext
)
4867 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
= DpcDeviceObject
->DeviceExtension
;
4868 SCSI_PORT_INTERRUPT_DATA InterruptData
;
4869 SCSI_PORT_SAVE_INTERRUPT Context
;
4870 PSCSI_PORT_LUN_EXTENSION LunExtension
;
4871 BOOLEAN NeedToStartIo
;
4872 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
4873 LARGE_INTEGER TimerValue
;
4875 DPRINT("ScsiPortDpcForIsr(Dpc %p DpcDeviceObject %p DpcIrp %p DpcContext %p)\n",
4876 Dpc
, DpcDeviceObject
, DpcIrp
, DpcContext
);
4878 /* We need to acquire spinlock */
4879 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4881 RtlZeroMemory(&InterruptData
, sizeof(SCSI_PORT_INTERRUPT_DATA
));
4885 /* Interrupt structure must be snapshotted, and only then analyzed */
4886 Context
.InterruptData
= &InterruptData
;
4887 Context
.DeviceExtension
= DeviceExtension
;
4889 if (!KeSynchronizeExecution(DeviceExtension
->Interrupt
[0],
4890 SpiSaveInterruptData
,
4893 /* Nothing - just return (don't forget to release the spinlock */
4894 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4895 DPRINT("ScsiPortDpcForIsr() done\n");
4899 /* If flush of adapters is needed - do it */
4900 if (InterruptData
.Flags
& SCSI_PORT_FLUSH_ADAPTERS
)
4902 /* TODO: Implement */
4906 /* Check for IoMapTransfer */
4907 if (InterruptData
.Flags
& SCSI_PORT_MAP_TRANSFER
)
4909 /* TODO: Implement */
4913 /* Check if timer is needed */
4914 if (InterruptData
.Flags
& SCSI_PORT_TIMER_NEEDED
)
4916 /* Save the timer routine */
4917 DeviceExtension
->HwScsiTimer
= InterruptData
.HwScsiTimer
;
4919 if (InterruptData
.MiniportTimerValue
== 0)
4921 /* Cancel the timer */
4922 KeCancelTimer(&DeviceExtension
->MiniportTimer
);
4926 /* Convert timer value */
4927 TimerValue
.QuadPart
= Int32x32To64(InterruptData
.MiniportTimerValue
, -10);
4930 KeSetTimer(&DeviceExtension
->MiniportTimer
,
4932 &DeviceExtension
->MiniportTimerDpc
);
4936 /* If it's ready for the next request */
4937 if (InterruptData
.Flags
& SCSI_PORT_NEXT_REQUEST_READY
)
4939 /* Check for a duplicate request (NextRequest+NextLuRequest) */
4940 if ((DeviceExtension
->Flags
&
4941 (SCSI_PORT_DEVICE_BUSY
| SCSI_PORT_DISCONNECT_ALLOWED
)) ==
4942 (SCSI_PORT_DEVICE_BUSY
| SCSI_PORT_DISCONNECT_ALLOWED
))
4944 /* Clear busy flag set by ScsiPortStartPacket() */
4945 DeviceExtension
->Flags
&= ~SCSI_PORT_DEVICE_BUSY
;
4947 if (!(InterruptData
.Flags
& SCSI_PORT_RESET
))
4949 /* Ready for next, and no reset is happening */
4950 DeviceExtension
->TimerCount
= -1;
4955 /* Not busy, but not ready for the next request */
4956 DeviceExtension
->Flags
&= ~SCSI_PORT_DEVICE_BUSY
;
4957 InterruptData
.Flags
&= ~SCSI_PORT_NEXT_REQUEST_READY
;
4962 if (InterruptData
.Flags
& SCSI_PORT_RESET_REPORTED
)
4964 /* Hold for a bit */
4965 DeviceExtension
->TimerCount
= 4;
4968 /* Any ready LUN? */
4969 if (InterruptData
.ReadyLun
!= NULL
)
4972 /* Process all LUNs from the list*/
4975 /* Remove it from the list first (as processed) */
4976 LunExtension
= InterruptData
.ReadyLun
;
4977 InterruptData
.ReadyLun
= LunExtension
->ReadyLun
;
4978 LunExtension
->ReadyLun
= NULL
;
4980 /* Get next request for this LUN */
4981 SpiGetNextRequestFromLun(DeviceExtension
, LunExtension
);
4983 /* Still ready requests exist?
4984 If yes - get spinlock, if no - stop here */
4985 if (InterruptData
.ReadyLun
!= NULL
)
4986 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4993 /* Release the spinlock */
4994 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4997 /* If we ready for next packet, start it */
4998 if (InterruptData
.Flags
& SCSI_PORT_NEXT_REQUEST_READY
)
4999 IoStartNextPacket(DeviceExtension
->DeviceObject
, FALSE
);
5001 NeedToStartIo
= FALSE
;
5003 /* Loop the completed request list */
5004 while (InterruptData
.CompletedRequests
)
5006 /* Remove the request */
5007 SrbInfo
= InterruptData
.CompletedRequests
;
5008 InterruptData
.CompletedRequests
= SrbInfo
->CompletedRequests
;
5009 SrbInfo
->CompletedRequests
= NULL
;
5012 SpiProcessCompletedRequest(DeviceExtension
,
5017 /* Loop abort request list */
5018 while (InterruptData
.CompletedAbort
)
5020 LunExtension
= InterruptData
.CompletedAbort
;
5022 /* Remove the request */
5023 InterruptData
.CompletedAbort
= LunExtension
->CompletedAbortRequests
;
5025 /* Get spinlock since we're going to change flags */
5026 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
5028 /* TODO: Put SrbExtension to the list of free extensions */
5032 /* If we need - call StartIo routine */
5035 /* Make sure CurrentIrp is not null! */
5036 ASSERT(DpcDeviceObject
->CurrentIrp
!= NULL
);
5037 ScsiPortStartIo(DpcDeviceObject
, DpcDeviceObject
->CurrentIrp
);
5040 /* Everything has been done, check */
5041 if (InterruptData
.Flags
& SCSI_PORT_ENABLE_INT_REQUEST
)
5043 /* Synchronize using spinlock */
5044 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
5046 /* Request an interrupt */
5047 DeviceExtension
->HwInterrupt(DeviceExtension
->MiniPortDeviceExtension
);
5049 ASSERT(DeviceExtension
->Flags
& SCSI_PORT_DISABLE_INT_REQUESET
);
5051 /* Should interrupts be enabled again? */
5052 if (DeviceExtension
->Flags
& SCSI_PORT_DISABLE_INT_REQUESET
)
5054 /* Clear this flag */
5055 DeviceExtension
->Flags
&= ~SCSI_PORT_DISABLE_INT_REQUESET
;
5057 /* Call a special routine to do this */
5060 KeSynchronizeExecution(DeviceExtension
->Interrupt
,
5061 SpiEnableInterrupts
,
5066 /* If we need a notification again - loop */
5067 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
5070 /* Release the spinlock */
5071 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
5074 DPRINT("ScsiPortDpcForIsr() done\n");
5079 SpiProcessTimeout(PVOID ServiceContext
)
5081 PDEVICE_OBJECT DeviceObject
= (PDEVICE_OBJECT
)ServiceContext
;
5082 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
= DeviceObject
->DeviceExtension
;
5085 DPRINT("SpiProcessTimeout() entered\n");
5087 DeviceExtension
->TimerCount
= -1;
5089 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_RESET
)
5091 DeviceExtension
->InterruptData
.Flags
&= ~SCSI_PORT_RESET
;
5093 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_RESET_REQUEST
)
5095 DeviceExtension
->InterruptData
.Flags
&= ~SCSI_PORT_RESET_REQUEST
;
5096 ScsiPortStartPacket(ServiceContext
);
5103 DPRINT("Resetting the bus\n");
5105 for (Bus
= 0; Bus
< DeviceExtension
->BusNum
; Bus
++)
5107 DeviceExtension
->HwResetBus(DeviceExtension
->MiniPortDeviceExtension
, Bus
);
5109 /* Reset flags and set reset timeout to 4 seconds */
5110 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_RESET
;
5111 DeviceExtension
->TimerCount
= 4;
5114 /* If miniport requested - request a dpc for it */
5115 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
5116 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
5125 SpiResetBus(PVOID ServiceContext
)
5127 PRESETBUS_PARAMS ResetParams
= (PRESETBUS_PARAMS
)ServiceContext
;
5128 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
5130 /* Perform the bus reset */
5131 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)ResetParams
->DeviceExtension
;
5132 DeviceExtension
->HwResetBus(DeviceExtension
->MiniPortDeviceExtension
,
5133 ResetParams
->PathId
);
5135 /* Set flags and start the timer */
5136 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_RESET
;
5137 DeviceExtension
->TimerCount
= 4;
5139 /* If miniport requested - give him a DPC */
5140 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
5141 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
5148 // This function handles timeouts and other time delayed processing
5153 // IN PDEVICE_OBJECT DeviceObject Device object registered with timer
5154 // IN PVOID Context the Controller extension for the
5155 // controller the device is on
5158 ScsiPortIoTimer(PDEVICE_OBJECT DeviceObject
,
5161 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
5162 PSCSI_PORT_LUN_EXTENSION LunExtension
;
5166 DPRINT("ScsiPortIoTimer()\n");
5168 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
5170 /* Protect with the spinlock */
5171 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
5173 /* Check timeouts */
5174 if (DeviceExtension
->TimerCount
> 0)
5176 /* Decrease the timeout counter */
5177 DeviceExtension
->TimerCount
--;
5179 if (DeviceExtension
->TimerCount
== 0)
5181 /* Timeout, process it */
5182 if (KeSynchronizeExecution(DeviceExtension
->Interrupt
[0],
5184 DeviceExtension
->DeviceObject
))
5186 DPRINT("Error happened during processing timeout, but nothing critical\n");
5190 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
5192 /* We should exit now, since timeout is processed */
5196 /* Per-Lun scanning of timeouts is needed... */
5197 for (Lun
= 0; Lun
< LUS_NUMBER
; Lun
++)
5199 LunExtension
= DeviceExtension
->LunExtensionList
[Lun
];
5201 while (LunExtension
)
5203 if (LunExtension
->Flags
& LUNEX_BUSY
)
5205 if (!(LunExtension
->Flags
&
5206 (LUNEX_NEED_REQUEST_SENSE
| LUNEX_FROZEN_QUEUE
)))
5208 DPRINT("Retrying busy request\n");
5210 /* Clear flags, and retry busy request */
5211 LunExtension
->Flags
&= ~(LUNEX_BUSY
| LUNEX_FULL_QUEUE
);
5212 Irp
= LunExtension
->BusyRequest
;
5214 /* Clearing busy request */
5215 LunExtension
->BusyRequest
= NULL
;
5217 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
5219 IoStartPacket(DeviceObject
, Irp
, (PULONG
)NULL
, NULL
);
5221 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
5224 else if (LunExtension
->RequestTimeout
== 0)
5226 RESETBUS_PARAMS ResetParams
;
5228 LunExtension
->RequestTimeout
= -1;
5230 DPRINT("Request timed out, resetting bus\n");
5232 /* Pass params to the bus reset routine */
5233 ResetParams
.PathId
= LunExtension
->PathId
;
5234 ResetParams
.DeviceExtension
= DeviceExtension
;
5236 if (!KeSynchronizeExecution(DeviceExtension
->Interrupt
[0],
5240 DPRINT1("Reset failed\n");
5243 else if (LunExtension
->RequestTimeout
> 0)
5245 /* Decrement the timeout counter */
5246 LunExtension
->RequestTimeout
--;
5249 LunExtension
= LunExtension
->Next
;
5253 /* Release the spinlock */
5254 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
5257 /**********************************************************************
5262 * Builds the registry device map of all device which are attached
5263 * to the given SCSI HBA port. The device map is located at:
5264 * \Registry\Machine\DeviceMap\Scsi
5274 * Name of registry driver service key.
5281 SpiBuildDeviceMap(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
5282 IN PUNICODE_STRING RegistryPath
)
5284 PSCSI_PORT_LUN_EXTENSION LunExtension
;
5285 OBJECT_ATTRIBUTES ObjectAttributes
;
5286 UNICODE_STRING KeyName
;
5287 UNICODE_STRING ValueName
;
5288 WCHAR NameBuffer
[64];
5291 HANDLE ScsiPortKey
= NULL
;
5292 HANDLE ScsiBusKey
= NULL
;
5293 HANDLE ScsiInitiatorKey
= NULL
;
5294 HANDLE ScsiTargetKey
= NULL
;
5295 HANDLE ScsiLunKey
= NULL
;
5298 ULONG CurrentTarget
;
5305 DPRINT("SpiBuildDeviceMap() called\n");
5307 if (DeviceExtension
== NULL
|| RegistryPath
== NULL
)
5309 DPRINT1("Invalid parameter\n");
5310 return STATUS_INVALID_PARAMETER
;
5313 /* Open or create the 'Scsi' subkey */
5314 RtlInitUnicodeString(&KeyName
,
5315 L
"\\Registry\\Machine\\Hardware\\DeviceMap\\Scsi");
5316 InitializeObjectAttributes(&ObjectAttributes
,
5318 OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
| OBJ_KERNEL_HANDLE
,
5321 Status
= ZwCreateKey(&ScsiKey
,
5326 REG_OPTION_VOLATILE
,
5328 if (!NT_SUCCESS(Status
))
5330 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5334 /* Create new 'Scsi Port X' subkey */
5335 DPRINT("Scsi Port %lu\n", DeviceExtension
->PortNumber
);
5337 swprintf(NameBuffer
,
5339 DeviceExtension
->PortNumber
);
5340 RtlInitUnicodeString(&KeyName
, NameBuffer
);
5341 InitializeObjectAttributes(&ObjectAttributes
,
5346 Status
= ZwCreateKey(&ScsiPortKey
,
5351 REG_OPTION_VOLATILE
,
5354 if (!NT_SUCCESS(Status
))
5356 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5361 * Create port-specific values
5364 /* Set 'DMA Enabled' (REG_DWORD) value */
5365 UlongData
= (ULONG
)!DeviceExtension
->PortCapabilities
.AdapterUsesPio
;
5366 DPRINT(" DMA Enabled = %s\n", UlongData
? "TRUE" : "FALSE");
5367 RtlInitUnicodeString(&ValueName
, L
"DMA Enabled");
5368 Status
= ZwSetValueKey(ScsiPortKey
,
5374 if (!NT_SUCCESS(Status
))
5376 DPRINT("ZwSetValueKey('DMA Enabled') failed (Status %lx)\n", Status
);
5377 ZwClose(ScsiPortKey
);
5381 /* Set 'Driver' (REG_SZ) value */
5382 DriverName
= wcsrchr(RegistryPath
->Buffer
, L
'\\') + 1;
5383 RtlInitUnicodeString(&ValueName
, L
"Driver");
5384 Status
= ZwSetValueKey(ScsiPortKey
,
5389 (ULONG
)((wcslen(DriverName
) + 1) * sizeof(WCHAR
)));
5390 if (!NT_SUCCESS(Status
))
5392 DPRINT("ZwSetValueKey('Driver') failed (Status %lx)\n", Status
);
5393 ZwClose(ScsiPortKey
);
5397 /* Set 'Interrupt' (REG_DWORD) value (NT4 only) */
5398 UlongData
= (ULONG
)DeviceExtension
->PortConfig
->BusInterruptLevel
;
5399 DPRINT(" Interrupt = %lu\n", UlongData
);
5400 RtlInitUnicodeString(&ValueName
, L
"Interrupt");
5401 Status
= ZwSetValueKey(ScsiPortKey
,
5407 if (!NT_SUCCESS(Status
))
5409 DPRINT("ZwSetValueKey('Interrupt') failed (Status %lx)\n", Status
);
5410 ZwClose(ScsiPortKey
);
5414 /* Set 'IOAddress' (REG_DWORD) value (NT4 only) */
5415 UlongData
= ScsiPortConvertPhysicalAddressToUlong((*DeviceExtension
->PortConfig
->AccessRanges
)[0].RangeStart
);
5416 DPRINT(" IOAddress = %lx\n", UlongData
);
5417 RtlInitUnicodeString(&ValueName
, L
"IOAddress");
5418 Status
= ZwSetValueKey(ScsiPortKey
,
5424 if (!NT_SUCCESS(Status
))
5426 DPRINT("ZwSetValueKey('IOAddress') failed (Status %lx)\n", Status
);
5427 ZwClose(ScsiPortKey
);
5431 /* Enumerate buses */
5432 for (BusNumber
= 0; BusNumber
< DeviceExtension
->PortConfig
->NumberOfBuses
; BusNumber
++)
5434 /* Create 'Scsi Bus X' key */
5435 DPRINT(" Scsi Bus %lu\n", BusNumber
);
5436 swprintf(NameBuffer
,
5439 RtlInitUnicodeString(&KeyName
, NameBuffer
);
5440 InitializeObjectAttributes(&ObjectAttributes
,
5445 Status
= ZwCreateKey(&ScsiBusKey
,
5450 REG_OPTION_VOLATILE
,
5452 if (!NT_SUCCESS(Status
))
5454 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5458 /* Create 'Initiator Id X' key */
5459 DPRINT(" Initiator Id %lu\n",
5460 DeviceExtension
->PortConfig
->InitiatorBusId
[BusNumber
]);
5461 swprintf(NameBuffer
,
5462 L
"Initiator Id %lu",
5463 (ULONG
)(UCHAR
)DeviceExtension
->PortConfig
->InitiatorBusId
[BusNumber
]);
5464 RtlInitUnicodeString(&KeyName
, NameBuffer
);
5465 InitializeObjectAttributes(&ObjectAttributes
,
5470 Status
= ZwCreateKey(&ScsiInitiatorKey
,
5475 REG_OPTION_VOLATILE
,
5477 if (!NT_SUCCESS(Status
))
5479 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5483 /* FIXME: Are there any initiator values (??) */
5485 ZwClose(ScsiInitiatorKey
);
5486 ScsiInitiatorKey
= NULL
;
5489 /* Enumerate targets */
5490 CurrentTarget
= (ULONG
)-1;
5491 ScsiTargetKey
= NULL
;
5492 for (Target
= 0; Target
< DeviceExtension
->PortConfig
->MaximumNumberOfTargets
; Target
++)
5494 for (Lun
= 0; Lun
< SCSI_MAXIMUM_LOGICAL_UNITS
; Lun
++)
5496 LunExtension
= SpiGetLunExtension(DeviceExtension
,
5500 if (LunExtension
== NULL
)
5503 if (Target
!= CurrentTarget
)
5505 /* Close old target key */
5506 if (ScsiTargetKey
!= NULL
)
5508 ZwClose(ScsiTargetKey
);
5509 ScsiTargetKey
= NULL
;
5512 /* Create 'Target Id X' key */
5513 DPRINT(" Target Id %lu\n", Target
);
5514 swprintf(NameBuffer
,
5517 RtlInitUnicodeString(&KeyName
, NameBuffer
);
5518 InitializeObjectAttributes(&ObjectAttributes
,
5523 Status
= ZwCreateKey(&ScsiTargetKey
,
5528 REG_OPTION_VOLATILE
,
5530 if (!NT_SUCCESS(Status
))
5532 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5536 CurrentTarget
= Target
;
5539 /* Create 'Logical Unit Id X' key */
5540 DPRINT(" Logical Unit Id %lu\n", Lun
);
5541 swprintf(NameBuffer
,
5542 L
"Logical Unit Id %lu",
5544 RtlInitUnicodeString(&KeyName
, NameBuffer
);
5545 InitializeObjectAttributes(&ObjectAttributes
,
5550 Status
= ZwCreateKey(&ScsiLunKey
,
5555 REG_OPTION_VOLATILE
,
5557 if (!NT_SUCCESS(Status
))
5559 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5563 /* Set 'Identifier' (REG_SZ) value */
5564 swprintf(NameBuffer
,
5566 LunExtension
->InquiryData
.VendorId
,
5567 LunExtension
->InquiryData
.ProductId
,
5568 LunExtension
->InquiryData
.ProductRevisionLevel
);
5569 DPRINT(" Identifier = '%S'\n", NameBuffer
);
5570 RtlInitUnicodeString(&ValueName
, L
"Identifier");
5571 Status
= ZwSetValueKey(ScsiLunKey
,
5576 (ULONG
)((wcslen(NameBuffer
) + 1) * sizeof(WCHAR
)));
5577 if (!NT_SUCCESS(Status
))
5579 DPRINT("ZwSetValueKey('Identifier') failed (Status %lx)\n", Status
);
5583 /* Set 'Type' (REG_SZ) value */
5585 * See https://docs.microsoft.com/en-us/windows-hardware/drivers/install/identifiers-for-ide-devices
5586 * and https://docs.microsoft.com/en-us/windows-hardware/drivers/install/identifiers-for-scsi-devices
5587 * for a list of types with their human-readable forms.
5589 switch (LunExtension
->InquiryData
.DeviceType
)
5592 TypeName
= L
"DiskPeripheral";
5595 TypeName
= L
"TapePeripheral";
5598 TypeName
= L
"PrinterPeripheral";
5600 // case 3: "ProcessorPeripheral", classified as 'other': fall back to default case.
5602 TypeName
= L
"WormPeripheral";
5605 TypeName
= L
"CdRomPeripheral";
5608 TypeName
= L
"ScannerPeripheral";
5611 TypeName
= L
"OpticalDiskPeripheral";
5614 TypeName
= L
"MediumChangerPeripheral";
5617 TypeName
= L
"CommunicationsPeripheral";
5620 /* New peripheral types (SCSI only) */
5622 TypeName
= L
"ASCPrePressGraphicsPeripheral";
5625 TypeName
= L
"ArrayPeripheral";
5628 TypeName
= L
"EnclosurePeripheral";
5631 TypeName
= L
"RBCPeripheral";
5634 TypeName
= L
"CardReaderPeripheral";
5637 TypeName
= L
"BridgePeripheral";
5641 TypeName
= L
"OtherPeripheral";
5644 DPRINT(" Type = '%S'\n", TypeName
);
5645 RtlInitUnicodeString(&ValueName
, L
"Type");
5646 Status
= ZwSetValueKey(ScsiLunKey
,
5651 (ULONG
)((wcslen(TypeName
) + 1) * sizeof(WCHAR
)));
5652 if (!NT_SUCCESS(Status
))
5654 DPRINT("ZwSetValueKey('Type') failed (Status %lx)\n", Status
);
5658 ZwClose(ScsiLunKey
);
5662 /* Close old target key */
5663 if (ScsiTargetKey
!= NULL
)
5665 ZwClose(ScsiTargetKey
);
5666 ScsiTargetKey
= NULL
;
5670 ZwClose(ScsiBusKey
);
5675 if (ScsiLunKey
!= NULL
)
5676 ZwClose(ScsiLunKey
);
5678 if (ScsiInitiatorKey
!= NULL
)
5679 ZwClose(ScsiInitiatorKey
);
5681 if (ScsiTargetKey
!= NULL
)
5682 ZwClose(ScsiTargetKey
);
5684 if (ScsiBusKey
!= NULL
)
5685 ZwClose(ScsiBusKey
);
5687 if (ScsiPortKey
!= NULL
)
5688 ZwClose(ScsiPortKey
);
5690 DPRINT("SpiBuildDeviceMap() done\n");
5697 SpiMiniportTimerDpc(IN
struct _KDPC
*Dpc
,
5698 IN PVOID DeviceObject
,
5699 IN PVOID SystemArgument1
,
5700 IN PVOID SystemArgument2
)
5702 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
5704 DPRINT("Miniport timer DPC\n");
5706 DeviceExtension
= ((PDEVICE_OBJECT
)DeviceObject
)->DeviceExtension
;
5708 /* Acquire the spinlock */
5709 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
5711 /* Call the timer routine */
5712 if (DeviceExtension
->HwScsiTimer
!= NULL
)
5714 DeviceExtension
->HwScsiTimer(&DeviceExtension
->MiniPortDeviceExtension
);
5717 /* Release the spinlock */
5718 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
5720 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
5722 ScsiPortDpcForIsr(NULL
,
5723 DeviceExtension
->DeviceObject
,
5730 SpiCreatePortConfig(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
5731 PHW_INITIALIZATION_DATA HwInitData
,
5732 PCONFIGURATION_INFO InternalConfigInfo
,
5733 PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
5736 UNICODE_STRING UnicodeString
;
5737 OBJECT_ATTRIBUTES ObjectAttributes
;
5738 PCONFIGURATION_INFORMATION DdkConfigInformation
;
5739 HANDLE RootKey
, Key
;
5741 WCHAR DeviceBuffer
[16];
5742 WCHAR StrBuffer
[512];
5746 /* Zero out the struct if told so */
5749 /* First zero the portconfig */
5750 RtlZeroMemory(ConfigInfo
, sizeof(PORT_CONFIGURATION_INFORMATION
));
5752 /* Then access ranges */
5753 RtlZeroMemory(InternalConfigInfo
->AccessRanges
,
5754 HwInitData
->NumberOfAccessRanges
* sizeof(ACCESS_RANGE
));
5756 /* Initialize the struct */
5757 ConfigInfo
->Length
= sizeof(PORT_CONFIGURATION_INFORMATION
);
5758 ConfigInfo
->AdapterInterfaceType
= HwInitData
->AdapterInterfaceType
;
5759 ConfigInfo
->InterruptMode
= Latched
;
5760 ConfigInfo
->DmaChannel
= SP_UNINITIALIZED_VALUE
;
5761 ConfigInfo
->DmaPort
= SP_UNINITIALIZED_VALUE
;
5762 ConfigInfo
->DmaChannel2
= SP_UNINITIALIZED_VALUE
;
5763 ConfigInfo
->DmaPort2
= SP_UNINITIALIZED_VALUE
;
5764 ConfigInfo
->MaximumTransferLength
= SP_UNINITIALIZED_VALUE
;
5765 ConfigInfo
->NumberOfAccessRanges
= HwInitData
->NumberOfAccessRanges
;
5766 ConfigInfo
->MaximumNumberOfTargets
= 8;
5768 /* Store parameters */
5769 ConfigInfo
->NeedPhysicalAddresses
= HwInitData
->NeedPhysicalAddresses
;
5770 ConfigInfo
->MapBuffers
= HwInitData
->MapBuffers
;
5771 ConfigInfo
->AutoRequestSense
= HwInitData
->AutoRequestSense
;
5772 ConfigInfo
->ReceiveEvent
= HwInitData
->ReceiveEvent
;
5773 ConfigInfo
->TaggedQueuing
= HwInitData
->TaggedQueuing
;
5774 ConfigInfo
->MultipleRequestPerLu
= HwInitData
->MultipleRequestPerLu
;
5776 /* Get the disk usage */
5777 DdkConfigInformation
= IoGetConfigurationInformation();
5778 ConfigInfo
->AtdiskPrimaryClaimed
= DdkConfigInformation
->AtDiskPrimaryAddressClaimed
;
5779 ConfigInfo
->AtdiskSecondaryClaimed
= DdkConfigInformation
->AtDiskSecondaryAddressClaimed
;
5781 /* Initiator bus id is not set */
5782 for (Bus
= 0; Bus
< 8; Bus
++)
5783 ConfigInfo
->InitiatorBusId
[Bus
] = (CCHAR
)SP_UNINITIALIZED_VALUE
;
5786 ConfigInfo
->NumberOfPhysicalBreaks
= 17;
5788 /* Clear this information */
5789 InternalConfigInfo
->DisableTaggedQueueing
= FALSE
;
5790 InternalConfigInfo
->DisableMultipleLun
= FALSE
;
5792 /* Store Bus Number */
5793 ConfigInfo
->SystemIoBusNumber
= InternalConfigInfo
->BusNumber
;
5797 if (ConfigInfo
->AdapterInterfaceType
== Internal
)
5799 /* Open registry key for HW database */
5800 InitializeObjectAttributes(&ObjectAttributes
,
5801 DeviceExtension
->DeviceObject
->DriverObject
->HardwareDatabase
,
5802 OBJ_CASE_INSENSITIVE
,
5806 Status
= ZwOpenKey(&RootKey
,
5810 if (NT_SUCCESS(Status
))
5812 /* Create name for it */
5813 swprintf(StrBuffer
, L
"ScsiAdapter\\%lu",
5814 InternalConfigInfo
->AdapterNumber
);
5816 RtlInitUnicodeString(&UnicodeString
, StrBuffer
);
5818 /* Open device key */
5819 InitializeObjectAttributes(&ObjectAttributes
,
5821 OBJ_CASE_INSENSITIVE
,
5825 Status
= ZwOpenKey(&Key
,
5831 if (NT_SUCCESS(Status
))
5833 if (InternalConfigInfo
->LastAdapterNumber
!= InternalConfigInfo
->AdapterNumber
)
5835 DPRINT("Hardware info found at %S\n", StrBuffer
);
5838 SpiParseDeviceInfo(DeviceExtension
,
5844 InternalConfigInfo
->BusNumber
= 0;
5848 /* Try the next adapter */
5849 InternalConfigInfo
->AdapterNumber
++;
5855 /* Info was not found, exit */
5856 DPRINT1("ZwOpenKey() failed with Status=0x%08X\n", Status
);
5857 return STATUS_DEVICE_DOES_NOT_EXIST
;
5862 DPRINT1("ZwOpenKey() failed with Status=0x%08X\n", Status
);
5866 /* Look at device params */
5868 if (InternalConfigInfo
->Parameter
)
5870 ExFreePool(InternalConfigInfo
->Parameter
);
5871 InternalConfigInfo
->Parameter
= NULL
;
5874 if (InternalConfigInfo
->ServiceKey
!= NULL
)
5876 swprintf(DeviceBuffer
, L
"Device%lu", InternalConfigInfo
->AdapterNumber
);
5877 RtlInitUnicodeString(&UnicodeString
, DeviceBuffer
);
5879 /* Open the service key */
5880 InitializeObjectAttributes(&ObjectAttributes
,
5882 OBJ_CASE_INSENSITIVE
,
5883 InternalConfigInfo
->ServiceKey
,
5886 Status
= ZwOpenKey(&Key
,
5891 /* Parse device key */
5892 if (InternalConfigInfo
->DeviceKey
!= NULL
)
5894 SpiParseDeviceInfo(DeviceExtension
,
5895 InternalConfigInfo
->DeviceKey
,
5901 /* Then parse hw info */
5904 if (InternalConfigInfo
->LastAdapterNumber
!= InternalConfigInfo
->AdapterNumber
)
5906 SpiParseDeviceInfo(DeviceExtension
,
5917 /* Adapter not found, go try the next one */
5918 InternalConfigInfo
->AdapterNumber
++;
5927 /* Update the last adapter number */
5928 InternalConfigInfo
->LastAdapterNumber
= InternalConfigInfo
->AdapterNumber
;
5930 /* Do we have this kind of bus at all? */
5932 Status
= IoQueryDeviceDescription(&HwInitData
->AdapterInterfaceType
,
5933 &InternalConfigInfo
->BusNumber
,
5938 SpQueryDeviceCallout
,
5941 /* This bus was not found */
5944 INTERFACE_TYPE InterfaceType
= Eisa
;
5946 /* Check for EISA */
5947 if (HwInitData
->AdapterInterfaceType
== Isa
)
5949 Status
= IoQueryDeviceDescription(&InterfaceType
,
5950 &InternalConfigInfo
->BusNumber
,
5955 SpQueryDeviceCallout
,
5958 /* Return respectively */
5960 return STATUS_SUCCESS
;
5962 return STATUS_DEVICE_DOES_NOT_EXIST
;
5966 return STATUS_DEVICE_DOES_NOT_EXIST
;
5971 return STATUS_SUCCESS
;
5976 SpiParseDeviceInfo(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
5978 IN PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
5979 IN PCONFIGURATION_INFO InternalConfigInfo
,
5982 PKEY_VALUE_FULL_INFORMATION KeyValueInformation
;
5983 PCM_FULL_RESOURCE_DESCRIPTOR FullResource
;
5984 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
5985 PCM_SCSI_DEVICE_DATA ScsiDeviceData
;
5986 ULONG Length
, Count
, Dma
= 0, Interrupt
= 0;
5987 ULONG Index
= 0, RangeCount
= 0;
5988 UNICODE_STRING UnicodeString
;
5989 ANSI_STRING AnsiString
;
5990 NTSTATUS Status
= STATUS_SUCCESS
;
5993 KeyValueInformation
= (PKEY_VALUE_FULL_INFORMATION
) Buffer
;
5995 /* Loop through all values in the device node */
5998 Status
= ZwEnumerateValueKey(Key
,
6000 KeyValueFullInformation
,
6005 if (!NT_SUCCESS(Status
))
6010 /* Length for DWORD is ok? */
6011 if (KeyValueInformation
->Type
== REG_DWORD
&&
6012 KeyValueInformation
->DataLength
!= sizeof(ULONG
))
6017 /* Get MaximumLogicalUnit */
6018 if (_wcsnicmp(KeyValueInformation
->Name
, L
"MaximumLogicalUnit",
6019 KeyValueInformation
->NameLength
/2) == 0)
6022 if (KeyValueInformation
->Type
!= REG_DWORD
)
6024 DPRINT("Bad data type for MaximumLogicalUnit\n");
6028 DeviceExtension
->MaxLunCount
= *((PUCHAR
)
6029 (Buffer
+ KeyValueInformation
->DataOffset
));
6031 /* Check / reset if needed */
6032 if (DeviceExtension
->MaxLunCount
> SCSI_MAXIMUM_LOGICAL_UNITS
)
6033 DeviceExtension
->MaxLunCount
= SCSI_MAXIMUM_LOGICAL_UNITS
;
6035 DPRINT("MaximumLogicalUnit = %d\n", DeviceExtension
->MaxLunCount
);
6038 /* Get InitiatorTargetId */
6039 if (_wcsnicmp(KeyValueInformation
->Name
, L
"InitiatorTargetId",
6040 KeyValueInformation
->NameLength
/ 2) == 0)
6043 if (KeyValueInformation
->Type
!= REG_DWORD
)
6045 DPRINT("Bad data type for InitiatorTargetId\n");
6049 ConfigInfo
->InitiatorBusId
[0] = *((PUCHAR
)
6050 (Buffer
+ KeyValueInformation
->DataOffset
));
6052 /* Check / reset if needed */
6053 if (ConfigInfo
->InitiatorBusId
[0] > ConfigInfo
->MaximumNumberOfTargets
- 1)
6054 ConfigInfo
->InitiatorBusId
[0] = (CCHAR
)-1;
6056 DPRINT("InitiatorTargetId = %d\n", ConfigInfo
->InitiatorBusId
[0]);
6060 if (_wcsnicmp(KeyValueInformation
->Name
, L
"ScsiDebug",
6061 KeyValueInformation
->NameLength
/2) == 0)
6063 DPRINT("ScsiDebug key not supported\n");
6066 /* Check for a breakpoint */
6067 if (_wcsnicmp(KeyValueInformation
->Name
, L
"BreakPointOnEntry",
6068 KeyValueInformation
->NameLength
/2) == 0)
6070 DPRINT1("Breakpoint on entry requested!\n");
6074 /* Get DisableSynchronousTransfers */
6075 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DisableSynchronousTransfers",
6076 KeyValueInformation
->NameLength
/2) == 0)
6078 DeviceExtension
->SrbFlags
|= SRB_FLAGS_DISABLE_SYNCH_TRANSFER
;
6079 DPRINT("Synch transfers disabled\n");
6082 /* Get DisableDisconnects */
6083 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DisableDisconnects",
6084 KeyValueInformation
->NameLength
/2) == 0)
6086 DeviceExtension
->SrbFlags
|= SRB_FLAGS_DISABLE_DISCONNECT
;
6087 DPRINT("Disconnects disabled\n");
6090 /* Get DisableTaggedQueuing */
6091 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DisableTaggedQueuing",
6092 KeyValueInformation
->NameLength
/2) == 0)
6094 InternalConfigInfo
->DisableTaggedQueueing
= TRUE
;
6095 DPRINT("Tagged queueing disabled\n");
6098 /* Get DisableMultipleRequests */
6099 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DisableMultipleRequests",
6100 KeyValueInformation
->NameLength
/2) == 0)
6102 InternalConfigInfo
->DisableMultipleLun
= TRUE
;
6103 DPRINT("Multiple requests disabled\n");
6106 /* Get DriverParameters */
6107 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DriverParameters",
6108 KeyValueInformation
->NameLength
/2) == 0)
6110 /* Skip if nothing */
6111 if (KeyValueInformation
->DataLength
== 0)
6114 /* If there was something previously allocated - free it */
6115 if (InternalConfigInfo
->Parameter
!= NULL
)
6116 ExFreePool(InternalConfigInfo
->Parameter
);
6119 InternalConfigInfo
->Parameter
= ExAllocatePoolWithTag(NonPagedPool
,
6120 KeyValueInformation
->DataLength
, TAG_SCSIPORT
);
6122 if (InternalConfigInfo
->Parameter
!= NULL
)
6124 if (KeyValueInformation
->Type
!= REG_SZ
)
6128 InternalConfigInfo
->Parameter
,
6129 (PCCHAR
)KeyValueInformation
+ KeyValueInformation
->DataOffset
,
6130 KeyValueInformation
->DataLength
);
6134 /* If it's a unicode string, convert it to ansi */
6135 UnicodeString
.Length
= (USHORT
)KeyValueInformation
->DataLength
;
6136 UnicodeString
.MaximumLength
= (USHORT
)KeyValueInformation
->DataLength
;
6137 UnicodeString
.Buffer
=
6138 (PWSTR
)((PCCHAR
)KeyValueInformation
+ KeyValueInformation
->DataOffset
);
6140 AnsiString
.Length
= 0;
6141 AnsiString
.MaximumLength
= (USHORT
)KeyValueInformation
->DataLength
;
6142 AnsiString
.Buffer
= (PCHAR
)InternalConfigInfo
->Parameter
;
6144 Status
= RtlUnicodeStringToAnsiString(&AnsiString
,
6148 /* In case of error, free the allocated space */
6149 if (!NT_SUCCESS(Status
))
6151 ExFreePool(InternalConfigInfo
->Parameter
);
6152 InternalConfigInfo
->Parameter
= NULL
;
6158 DPRINT("Found driver parameter\n");
6161 /* Get MaximumSGList */
6162 if (_wcsnicmp(KeyValueInformation
->Name
, L
"MaximumSGList",
6163 KeyValueInformation
->NameLength
/2) == 0)
6165 if (KeyValueInformation
->Type
!= REG_DWORD
)
6167 DPRINT("Bad data type for MaximumSGList\n");
6171 ConfigInfo
->NumberOfPhysicalBreaks
= *((PUCHAR
)(Buffer
+ KeyValueInformation
->DataOffset
));
6174 if (ConfigInfo
->NumberOfPhysicalBreaks
> SCSI_MAXIMUM_PHYSICAL_BREAKS
)
6176 ConfigInfo
->NumberOfPhysicalBreaks
= SCSI_MAXIMUM_PHYSICAL_BREAKS
;
6178 else if (ConfigInfo
->NumberOfPhysicalBreaks
< SCSI_MINIMUM_PHYSICAL_BREAKS
)
6180 ConfigInfo
->NumberOfPhysicalBreaks
= SCSI_MINIMUM_PHYSICAL_BREAKS
;
6183 DPRINT("MaximumSGList = %d\n", ConfigInfo
->NumberOfPhysicalBreaks
);
6186 /* Get NumberOfRequests */
6187 if (_wcsnicmp(KeyValueInformation
->Name
, L
"NumberOfRequests",
6188 KeyValueInformation
->NameLength
/2) == 0)
6190 if (KeyValueInformation
->Type
!= REG_DWORD
)
6192 DPRINT("NumberOfRequests has wrong data type\n");
6196 DeviceExtension
->RequestsNumber
= *((PUCHAR
)(Buffer
+ KeyValueInformation
->DataOffset
));
6199 if (DeviceExtension
->RequestsNumber
< 16)
6201 DeviceExtension
->RequestsNumber
= 16;
6203 else if (DeviceExtension
->RequestsNumber
> 512)
6205 DeviceExtension
->RequestsNumber
= 512;
6208 DPRINT("Number Of Requests = %d\n", DeviceExtension
->RequestsNumber
);
6211 /* Get resource list */
6212 if (_wcsnicmp(KeyValueInformation
->Name
, L
"ResourceList",
6213 KeyValueInformation
->NameLength
/2) == 0 ||
6214 _wcsnicmp(KeyValueInformation
->Name
, L
"Configuration Data",
6215 KeyValueInformation
->NameLength
/2) == 0 )
6217 if (KeyValueInformation
->Type
!= REG_FULL_RESOURCE_DESCRIPTOR
||
6218 KeyValueInformation
->DataLength
< sizeof(REG_FULL_RESOURCE_DESCRIPTOR
))
6220 DPRINT("Bad data type for ResourceList\n");
6225 DPRINT("Found ResourceList\n");
6228 FullResource
= (PCM_FULL_RESOURCE_DESCRIPTOR
)(Buffer
+ KeyValueInformation
->DataOffset
);
6230 /* Copy some info from it */
6231 InternalConfigInfo
->BusNumber
= FullResource
->BusNumber
;
6232 ConfigInfo
->SystemIoBusNumber
= FullResource
->BusNumber
;
6234 /* Loop through it */
6235 for (Count
= 0; Count
< FullResource
->PartialResourceList
.Count
; Count
++)
6237 /* Get partial descriptor */
6239 &FullResource
->PartialResourceList
.PartialDescriptors
[Count
];
6241 /* Check datalength */
6242 if ((ULONG
)((PCHAR
)(PartialDescriptor
+ 1) -
6243 (PCHAR
)FullResource
) > KeyValueInformation
->DataLength
)
6245 DPRINT("Resource data is of incorrect size\n");
6249 switch (PartialDescriptor
->Type
)
6251 case CmResourceTypePort
:
6252 if (RangeCount
>= ConfigInfo
->NumberOfAccessRanges
)
6254 DPRINT("Too many access ranges\n");
6258 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeInMemory
= FALSE
;
6259 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeStart
= PartialDescriptor
->u
.Port
.Start
;
6260 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeLength
= PartialDescriptor
->u
.Port
.Length
;
6265 case CmResourceTypeMemory
:
6266 if (RangeCount
>= ConfigInfo
->NumberOfAccessRanges
)
6268 DPRINT("Too many access ranges\n");
6272 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeInMemory
= TRUE
;
6273 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeStart
= PartialDescriptor
->u
.Memory
.Start
;
6274 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeLength
= PartialDescriptor
->u
.Memory
.Length
;
6279 case CmResourceTypeInterrupt
:
6283 ConfigInfo
->BusInterruptLevel
=
6284 PartialDescriptor
->u
.Interrupt
.Level
;
6286 ConfigInfo
->BusInterruptVector
=
6287 PartialDescriptor
->u
.Interrupt
.Vector
;
6289 ConfigInfo
->InterruptMode
= (PartialDescriptor
->Flags
& CM_RESOURCE_INTERRUPT_LATCHED
) ? Latched
: LevelSensitive
;
6291 else if (Interrupt
== 1)
6293 ConfigInfo
->BusInterruptLevel2
=
6294 PartialDescriptor
->u
.Interrupt
.Level
;
6296 ConfigInfo
->BusInterruptVector2
=
6297 PartialDescriptor
->u
.Interrupt
.Vector
;
6299 ConfigInfo
->InterruptMode2
= (PartialDescriptor
->Flags
& CM_RESOURCE_INTERRUPT_LATCHED
) ? Latched
: LevelSensitive
;
6305 case CmResourceTypeDma
:
6309 ConfigInfo
->DmaChannel
= PartialDescriptor
->u
.Dma
.Channel
;
6310 ConfigInfo
->DmaPort
= PartialDescriptor
->u
.Dma
.Port
;
6312 if (PartialDescriptor
->Flags
& CM_RESOURCE_DMA_8
)
6313 ConfigInfo
->DmaWidth
= Width8Bits
;
6314 else if ((PartialDescriptor
->Flags
& CM_RESOURCE_DMA_16
) ||
6315 (PartialDescriptor
->Flags
& CM_RESOURCE_DMA_8_AND_16
)) //???
6316 ConfigInfo
->DmaWidth
= Width16Bits
;
6317 else if (PartialDescriptor
->Flags
& CM_RESOURCE_DMA_32
)
6318 ConfigInfo
->DmaWidth
= Width32Bits
;
6322 ConfigInfo
->DmaChannel2
= PartialDescriptor
->u
.Dma
.Channel
;
6323 ConfigInfo
->DmaPort2
= PartialDescriptor
->u
.Dma
.Port
;
6325 if (PartialDescriptor
->Flags
& CM_RESOURCE_DMA_8
)
6326 ConfigInfo
->DmaWidth2
= Width8Bits
;
6327 else if ((PartialDescriptor
->Flags
& CM_RESOURCE_DMA_16
) ||
6328 (PartialDescriptor
->Flags
& CM_RESOURCE_DMA_8_AND_16
)) //???
6329 ConfigInfo
->DmaWidth2
= Width16Bits
;
6330 else if (PartialDescriptor
->Flags
& CM_RESOURCE_DMA_32
)
6331 ConfigInfo
->DmaWidth2
= Width32Bits
;
6337 case CmResourceTypeDeviceSpecific
:
6338 if (PartialDescriptor
->u
.DeviceSpecificData
.DataSize
<
6339 sizeof(CM_SCSI_DEVICE_DATA
) ||
6340 (PCHAR
) (PartialDescriptor
+ 1) - (PCHAR
)FullResource
+
6341 PartialDescriptor
->u
.DeviceSpecificData
.DataSize
>
6342 KeyValueInformation
->DataLength
)
6344 DPRINT("Resource data length is incorrect");
6348 /* Set only one field from it */
6349 ScsiDeviceData
= (PCM_SCSI_DEVICE_DATA
) (PartialDescriptor
+1);
6350 ConfigInfo
->InitiatorBusId
[0] = ScsiDeviceData
->HostIdentifier
;
6360 SpQueryDeviceCallout(IN PVOID Context
,
6361 IN PUNICODE_STRING PathName
,
6362 IN INTERFACE_TYPE BusType
,
6364 IN PKEY_VALUE_FULL_INFORMATION
*BusInformation
,
6365 IN CONFIGURATION_TYPE ControllerType
,
6366 IN ULONG ControllerNumber
,
6367 IN PKEY_VALUE_FULL_INFORMATION
*ControllerInformation
,
6368 IN CONFIGURATION_TYPE PeripheralType
,
6369 IN ULONG PeripheralNumber
,
6370 IN PKEY_VALUE_FULL_INFORMATION
*PeripheralInformation
)
6372 PBOOLEAN Found
= (PBOOLEAN
)Context
;
6373 /* We just set our Found variable to TRUE */
6376 return STATUS_SUCCESS
;
6379 IO_ALLOCATION_ACTION
6381 ScsiPortAllocateAdapterChannel(IN PDEVICE_OBJECT DeviceObject
,
6383 IN PVOID MapRegisterBase
,
6387 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
= DeviceObject
->DeviceExtension
;
6389 /* Guard access with the spinlock */
6390 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
6392 /* Save MapRegisterBase we've got here */
6393 DeviceExtension
->MapRegisterBase
= MapRegisterBase
;
6395 /* Start pending request */
6396 KeSynchronizeExecution(DeviceExtension
->Interrupt
[0],
6397 ScsiPortStartPacket
, DeviceObject
);
6399 /* Release spinlock we took */
6400 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
6407 SpiStatusSrbToNt(UCHAR SrbStatus
)
6409 switch (SRB_STATUS(SrbStatus
))
6411 case SRB_STATUS_TIMEOUT
:
6412 case SRB_STATUS_COMMAND_TIMEOUT
:
6413 return STATUS_IO_TIMEOUT
;
6415 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH
:
6416 case SRB_STATUS_BAD_FUNCTION
:
6417 return STATUS_INVALID_DEVICE_REQUEST
;
6419 case SRB_STATUS_NO_DEVICE
:
6420 case SRB_STATUS_INVALID_LUN
:
6421 case SRB_STATUS_INVALID_TARGET_ID
:
6422 case SRB_STATUS_NO_HBA
:
6423 return STATUS_DEVICE_DOES_NOT_EXIST
;
6425 case SRB_STATUS_DATA_OVERRUN
:
6426 return STATUS_BUFFER_OVERFLOW
;
6428 case SRB_STATUS_SELECTION_TIMEOUT
:
6429 return STATUS_DEVICE_NOT_CONNECTED
;
6432 return STATUS_IO_DEVICE_ERROR
;
6435 return STATUS_IO_DEVICE_ERROR
;
6439 #undef ScsiPortConvertPhysicalAddressToUlong
6444 ScsiPortConvertPhysicalAddressToUlong(IN SCSI_PHYSICAL_ADDRESS Address
)
6446 DPRINT("ScsiPortConvertPhysicalAddressToUlong()\n");
6447 return(Address
.u
.LowPart
);