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 *****************************************************************/
44 #include "scsiport_int.h"
46 ULONG InternalDebugLevel
= 0x00;
48 #undef ScsiPortMoveMemory
50 /* TYPES *********************************************************************/
52 /* GLOBALS *******************************************************************/
55 SpiGetPciConfigData(IN PDRIVER_OBJECT DriverObject
,
56 IN PDEVICE_OBJECT DeviceObject
,
57 IN
struct _HW_INITIALIZATION_DATA
*HwInitializationData
,
58 IN OUT PPORT_CONFIGURATION_INFORMATION PortConfig
,
59 IN PUNICODE_STRING RegistryPath
,
61 IN OUT PPCI_SLOT_NUMBER NextSlotNumber
);
64 ScsiPortCreateClose(IN PDEVICE_OBJECT DeviceObject
,
67 static DRIVER_DISPATCH ScsiPortDispatchScsi
;
69 ScsiPortDispatchScsi(IN PDEVICE_OBJECT DeviceObject
,
73 ScsiPortDeviceControl(IN PDEVICE_OBJECT DeviceObject
,
76 static DRIVER_STARTIO ScsiPortStartIo
;
78 ScsiPortStartIo(IN PDEVICE_OBJECT DeviceObject
,
82 ScsiPortStartPacket(IN OUT PVOID Context
);
86 SpiAdapterControl(PDEVICE_OBJECT DeviceObject
, PIRP Irp
,
87 PVOID MapRegisterBase
, PVOID Context
);
89 static PSCSI_PORT_LUN_EXTENSION
90 SpiAllocateLunExtension (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
);
92 static PSCSI_PORT_LUN_EXTENSION
93 SpiGetLunExtension (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
98 static PSCSI_REQUEST_BLOCK_INFO
99 SpiAllocateSrbStructures(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
100 PSCSI_PORT_LUN_EXTENSION LunExtension
,
101 PSCSI_REQUEST_BLOCK Srb
);
104 SpiSendInquiry (IN PDEVICE_OBJECT DeviceObject
,
105 IN PSCSI_LUN_INFO LunInfo
);
108 SpiScanAdapter (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
);
111 SpiGetInquiryData (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
114 static PSCSI_REQUEST_BLOCK_INFO
115 SpiGetSrbData(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
122 ScsiPortIsr(IN PKINTERRUPT Interrupt
,
123 IN PVOID ServiceContext
);
126 ScsiPortDpcForIsr(IN PKDPC Dpc
,
127 IN PDEVICE_OBJECT DpcDeviceObject
,
129 IN PVOID DpcContext
);
132 ScsiPortIoTimer(PDEVICE_OBJECT DeviceObject
,
137 ScsiPortAllocateAdapterChannel(IN PDEVICE_OBJECT DeviceObject
,
139 IN PVOID MapRegisterBase
,
143 SpiBuildDeviceMap (PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
144 PUNICODE_STRING RegistryPath
);
147 SpiStatusSrbToNt(UCHAR SrbStatus
);
150 SpiSendRequestSense(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
151 IN PSCSI_REQUEST_BLOCK Srb
);
153 static IO_COMPLETION_ROUTINE SpiCompletionRoutine
;
155 SpiCompletionRoutine(PDEVICE_OBJECT DeviceObject
,
161 SpiProcessCompletedRequest(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
162 IN PSCSI_REQUEST_BLOCK_INFO SrbInfo
,
163 OUT PBOOLEAN NeedToCallStartIo
);
166 SpiGetNextRequestFromLun(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
167 IN PSCSI_PORT_LUN_EXTENSION LunExtension
);
170 SpiMiniportTimerDpc(IN
struct _KDPC
*Dpc
,
171 IN PVOID DeviceObject
,
172 IN PVOID SystemArgument1
,
173 IN PVOID SystemArgument2
);
176 SpiCreatePortConfig(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
177 PHW_INITIALIZATION_DATA HwInitData
,
178 PCONFIGURATION_INFO InternalConfigInfo
,
179 PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
183 SpQueryDeviceCallout(IN PVOID Context
,
184 IN PUNICODE_STRING PathName
,
185 IN INTERFACE_TYPE BusType
,
187 IN PKEY_VALUE_FULL_INFORMATION
*BusInformation
,
188 IN CONFIGURATION_TYPE ControllerType
,
189 IN ULONG ControllerNumber
,
190 IN PKEY_VALUE_FULL_INFORMATION
*ControllerInformation
,
191 IN CONFIGURATION_TYPE PeripheralType
,
192 IN ULONG PeripheralNumber
,
193 IN PKEY_VALUE_FULL_INFORMATION
*PeripheralInformation
);
196 SpiParseDeviceInfo(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
198 IN PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
199 IN PCONFIGURATION_INFO InternalConfigInfo
,
203 SpiResourceToConfig(IN PHW_INITIALIZATION_DATA HwInitializationData
,
204 IN PCM_FULL_RESOURCE_DESCRIPTOR ResourceDescriptor
,
205 IN PPORT_CONFIGURATION_INFORMATION PortConfig
);
207 static PCM_RESOURCE_LIST
208 SpiConfigToResource(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
209 PPORT_CONFIGURATION_INFORMATION PortConfig
);
212 SpiCleanupAfterInit(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
);
215 SpiHandleAttachRelease(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
219 SpiAllocateCommonBuffer(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
, ULONG NonCachedSize
);
221 NTHALAPI ULONG NTAPI
HalGetBusData(BUS_DATA_TYPE
, ULONG
, ULONG
, PVOID
, ULONG
);
222 NTHALAPI ULONG NTAPI
HalGetInterruptVector(INTERFACE_TYPE
, ULONG
, ULONG
, ULONG
, PKIRQL
, PKAFFINITY
);
223 NTHALAPI NTSTATUS NTAPI
HalAssignSlotResources(PUNICODE_STRING
, PUNICODE_STRING
, PDRIVER_OBJECT
, PDEVICE_OBJECT
, INTERFACE_TYPE
, ULONG
, ULONG
, PCM_RESOURCE_LIST
*);
225 /* FUNCTIONS *****************************************************************/
227 /**********************************************************************
232 * This function initializes the driver.
239 * System allocated Driver Object for this driver.
242 * Name of registry driver service key.
249 DriverEntry(IN PDRIVER_OBJECT DriverObject
,
250 IN PUNICODE_STRING RegistryPath
)
252 DPRINT("ScsiPort Driver %s\n", VERSION
);
253 return(STATUS_SUCCESS
);
257 /**********************************************************************
262 * Prints debugging messages.
269 * Debug level of the given message.
272 * Pointer to printf()-compatible format string.
275 Additional output data (see printf()).
284 ScsiDebugPrint(IN ULONG DebugPrintLevel
,
285 IN PCHAR DebugMessage
,
291 if (DebugPrintLevel
> InternalDebugLevel
)
294 va_start(ap
, DebugMessage
);
295 vsprintf(Buffer
, DebugMessage
, ap
);
301 /* An internal helper function for ScsiPortCompleteRequest */
304 SpiCompleteRequest(IN PVOID HwDeviceExtension
,
305 IN PSCSI_REQUEST_BLOCK_INFO SrbInfo
,
308 PSCSI_REQUEST_BLOCK Srb
;
310 /* Get current SRB */
313 /* Return if there is no SRB or it is not active */
314 if (!Srb
|| !(Srb
->SrbFlags
& SRB_FLAGS_IS_ACTIVE
)) return;
317 Srb
->SrbStatus
= SrbStatus
;
319 /* Set data transfered to 0 */
320 Srb
->DataTransferLength
= 0;
323 ScsiPortNotification(RequestComplete
,
332 ScsiPortCompleteRequest(IN PVOID HwDeviceExtension
,
338 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
339 PSCSI_PORT_LUN_EXTENSION LunExtension
;
340 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
341 PLIST_ENTRY ListEntry
;
345 DPRINT("ScsiPortCompleteRequest() called\n");
347 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
348 SCSI_PORT_DEVICE_EXTENSION
,
349 MiniPortDeviceExtension
);
351 /* Go through all buses */
352 for (BusNumber
= 0; BusNumber
< 8; BusNumber
++)
354 /* Go through all targets */
355 for (Target
= 0; Target
< DeviceExtension
->MaxTargedIds
; Target
++)
357 /* Get logical unit list head */
358 LunExtension
= DeviceExtension
->LunExtensionList
[Target
% 8];
360 /* Go through all logical units */
363 /* Now match what caller asked with what we are at now */
364 if ((PathId
== SP_UNTAGGED
|| PathId
== LunExtension
->PathId
) &&
365 (TargetId
== SP_UNTAGGED
|| TargetId
== LunExtension
->TargetId
) &&
366 (Lun
== SP_UNTAGGED
|| Lun
== LunExtension
->Lun
))
368 /* Yes, that's what caller asked for. Complete abort requests */
369 if (LunExtension
->CompletedAbortRequests
)
371 /* TODO: Save SrbStatus in this request */
372 DPRINT1("Completing abort request without setting SrbStatus!\n");
374 /* Issue a notification request */
375 ScsiPortNotification(RequestComplete
,
377 LunExtension
->CompletedAbortRequests
);
380 /* Complete the request using our helper */
381 SpiCompleteRequest(HwDeviceExtension
,
382 &LunExtension
->SrbInfo
,
385 /* Go through the queue and complete everything there too */
386 ListEntry
= LunExtension
->SrbInfo
.Requests
.Flink
;
387 while (ListEntry
!= &LunExtension
->SrbInfo
.Requests
)
389 /* Get the actual SRB info entry */
390 SrbInfo
= CONTAINING_RECORD(ListEntry
,
391 SCSI_REQUEST_BLOCK_INFO
,
395 SpiCompleteRequest(HwDeviceExtension
,
399 /* Advance to the next request in queue */
400 ListEntry
= SrbInfo
->Requests
.Flink
;
404 /* Advance to the next one */
405 LunExtension
= LunExtension
->Next
;
415 ScsiPortFlushDma(IN PVOID HwDeviceExtension
)
417 DPRINT("ScsiPortFlushDma()\n");
426 ScsiPortFreeDeviceBase(IN PVOID HwDeviceExtension
,
427 IN PVOID MappedAddress
)
429 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
430 PMAPPED_ADDRESS NextMa
, LastMa
;
432 //DPRINT("ScsiPortFreeDeviceBase() called\n");
434 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
435 SCSI_PORT_DEVICE_EXTENSION
,
436 MiniPortDeviceExtension
);
438 /* Initialize our pointers */
439 NextMa
= DeviceExtension
->MappedAddressList
;
444 if (NextMa
->MappedAddress
== MappedAddress
)
447 MmUnmapIoSpace(MappedAddress
, NextMa
->NumberOfBytes
);
449 /* Remove it from the list */
450 if (NextMa
== DeviceExtension
->MappedAddressList
)
452 /* Remove the first entry */
453 DeviceExtension
->MappedAddressList
= NextMa
->NextMappedAddress
;
457 LastMa
->NextMappedAddress
= NextMa
->NextMappedAddress
;
460 /* Free the resources and quit */
468 NextMa
= NextMa
->NextMappedAddress
;
478 ScsiPortGetBusData(IN PVOID DeviceExtension
,
479 IN ULONG BusDataType
,
480 IN ULONG SystemIoBusNumber
,
485 DPRINT("ScsiPortGetBusData()\n");
489 /* If Length is non-zero, just forward the call to
490 HalGetBusData() function */
491 return HalGetBusData(BusDataType
,
498 /* We have a more complex case here */
507 ScsiPortSetBusDataByOffset(IN PVOID DeviceExtension
,
508 IN ULONG BusDataType
,
509 IN ULONG SystemIoBusNumber
,
515 DPRINT("ScsiPortSetBusDataByOffset()\n");
516 return HalSetBusDataByOffset(BusDataType
,
528 ScsiPortGetDeviceBase(IN PVOID HwDeviceExtension
,
529 IN INTERFACE_TYPE BusType
,
530 IN ULONG SystemIoBusNumber
,
531 IN SCSI_PHYSICAL_ADDRESS IoAddress
,
532 IN ULONG NumberOfBytes
,
533 IN BOOLEAN InIoSpace
)
535 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
536 PHYSICAL_ADDRESS TranslatedAddress
;
537 PMAPPED_ADDRESS DeviceBase
;
541 //DPRINT ("ScsiPortGetDeviceBase() called\n");
543 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
544 SCSI_PORT_DEVICE_EXTENSION
,
545 MiniPortDeviceExtension
);
547 AddressSpace
= (ULONG
)InIoSpace
;
548 if (HalTranslateBusAddress(BusType
,
552 &TranslatedAddress
) == FALSE
)
558 if (AddressSpace
!= 0)
559 return((PVOID
)(ULONG_PTR
)TranslatedAddress
.QuadPart
);
561 MappedAddress
= MmMapIoSpace(TranslatedAddress
,
565 DeviceBase
= ExAllocatePoolWithTag(NonPagedPool
,
566 sizeof(MAPPED_ADDRESS
), TAG_SCSIPORT
);
568 if (DeviceBase
== NULL
)
569 return MappedAddress
;
571 DeviceBase
->MappedAddress
= MappedAddress
;
572 DeviceBase
->NumberOfBytes
= NumberOfBytes
;
573 DeviceBase
->IoAddress
= IoAddress
;
574 DeviceBase
->BusNumber
= SystemIoBusNumber
;
576 /* Link it to the Device Extension list */
577 DeviceBase
->NextMappedAddress
= DeviceExtension
->MappedAddressList
;
578 DeviceExtension
->MappedAddressList
= DeviceBase
;
580 return MappedAddress
;
587 ScsiPortGetLogicalUnit(IN PVOID HwDeviceExtension
,
594 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
595 PSCSI_PORT_LUN_EXTENSION LunExtension
;
598 DPRINT("ScsiPortGetLogicalUnit() called\n");
600 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
601 SCSI_PORT_DEVICE_EXTENSION
,
602 MiniPortDeviceExtension
);
603 if (IsListEmpty(&DeviceExtension
->LunExtensionListHead
))
606 Entry
= DeviceExtension
->LunExtensionListHead
.Flink
;
607 while (Entry
!= &DeviceExtension
->LunExtensionListHead
)
609 LunExtension
= CONTAINING_RECORD(Entry
,
610 SCSI_PORT_LUN_EXTENSION
,
612 if (LunExtension
->PathId
== PathId
&&
613 LunExtension
->TargetId
== TargetId
&&
614 LunExtension
->Lun
== Lun
)
616 return (PVOID
)&LunExtension
->MiniportLunExtension
;
619 Entry
= Entry
->Flink
;
629 SCSI_PHYSICAL_ADDRESS NTAPI
630 ScsiPortGetPhysicalAddress(IN PVOID HwDeviceExtension
,
631 IN PSCSI_REQUEST_BLOCK Srb OPTIONAL
,
632 IN PVOID VirtualAddress
,
635 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
636 SCSI_PHYSICAL_ADDRESS PhysicalAddress
;
637 ULONG BufferLength
= 0;
639 PSCSI_SG_ADDRESS SGList
;
640 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
642 DPRINT("ScsiPortGetPhysicalAddress(%p %p %p %p)\n",
643 HwDeviceExtension
, Srb
, VirtualAddress
, Length
);
645 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
646 SCSI_PORT_DEVICE_EXTENSION
,
647 MiniPortDeviceExtension
);
649 if (Srb
== NULL
|| Srb
->SenseInfoBuffer
== VirtualAddress
)
651 /* Simply look it up in the allocated common buffer */
652 Offset
= (PUCHAR
)VirtualAddress
- (PUCHAR
)DeviceExtension
->SrbExtensionBuffer
;
654 BufferLength
= DeviceExtension
->CommonBufferLength
- Offset
;
655 PhysicalAddress
.QuadPart
= DeviceExtension
->PhysicalAddress
.QuadPart
+ Offset
;
657 else if (DeviceExtension
->MapRegisters
)
659 /* Scatter-gather list must be used */
660 SrbInfo
= SpiGetSrbData(DeviceExtension
,
666 SGList
= SrbInfo
->ScatterGather
;
668 /* Find needed item in the SG list */
669 Offset
= (PCHAR
)VirtualAddress
- (PCHAR
)Srb
->DataBuffer
;
670 while (Offset
>= SGList
->Length
)
672 Offset
-= SGList
->Length
;
676 /* We're done, store length and physical address */
677 BufferLength
= SGList
->Length
- Offset
;
678 PhysicalAddress
.QuadPart
= SGList
->PhysicalAddress
.QuadPart
+ Offset
;
684 PhysicalAddress
.QuadPart
= (LONGLONG
)(SP_UNINITIALIZED_VALUE
);
687 *Length
= BufferLength
;
688 return PhysicalAddress
;
695 PSCSI_REQUEST_BLOCK NTAPI
696 ScsiPortGetSrb(IN PVOID DeviceExtension
,
702 DPRINT1("ScsiPortGetSrb() unimplemented\n");
712 ScsiPortGetUncachedExtension(IN PVOID HwDeviceExtension
,
713 IN PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
714 IN ULONG NumberOfBytes
)
716 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
717 DEVICE_DESCRIPTION DeviceDescription
;
718 ULONG MapRegistersCount
;
721 DPRINT("ScsiPortGetUncachedExtension(%p %p %lu)\n",
722 HwDeviceExtension
, ConfigInfo
, NumberOfBytes
);
724 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
725 SCSI_PORT_DEVICE_EXTENSION
,
726 MiniPortDeviceExtension
);
728 /* Check for allocated common DMA buffer */
729 if (DeviceExtension
->SrbExtensionBuffer
!= NULL
)
731 DPRINT1("The HBA has already got a common DMA buffer!\n");
735 /* Check for DMA adapter object */
736 if (DeviceExtension
->AdapterObject
== NULL
)
738 /* Initialize DMA adapter description */
739 RtlZeroMemory(&DeviceDescription
, sizeof(DEVICE_DESCRIPTION
));
741 DeviceDescription
.Version
= DEVICE_DESCRIPTION_VERSION
;
742 DeviceDescription
.Master
= ConfigInfo
->Master
;
743 DeviceDescription
.ScatterGather
= ConfigInfo
->ScatterGather
;
744 DeviceDescription
.DemandMode
= ConfigInfo
->DemandMode
;
745 DeviceDescription
.Dma32BitAddresses
= ConfigInfo
->Dma32BitAddresses
;
746 DeviceDescription
.BusNumber
= ConfigInfo
->SystemIoBusNumber
;
747 DeviceDescription
.DmaChannel
= ConfigInfo
->DmaChannel
;
748 DeviceDescription
.InterfaceType
= ConfigInfo
->AdapterInterfaceType
;
749 DeviceDescription
.DmaWidth
= ConfigInfo
->DmaWidth
;
750 DeviceDescription
.DmaSpeed
= ConfigInfo
->DmaSpeed
;
751 DeviceDescription
.MaximumLength
= ConfigInfo
->MaximumTransferLength
;
752 DeviceDescription
.DmaPort
= ConfigInfo
->DmaPort
;
754 /* Get a DMA adapter object */
755 DeviceExtension
->AdapterObject
=
756 HalGetAdapter(&DeviceDescription
, &MapRegistersCount
);
758 /* Fail in case of error */
759 if (DeviceExtension
->AdapterObject
== NULL
)
761 DPRINT1("HalGetAdapter() failed\n");
765 /* Set number of physical breaks */
766 if (ConfigInfo
->NumberOfPhysicalBreaks
!= 0 &&
767 MapRegistersCount
> ConfigInfo
->NumberOfPhysicalBreaks
)
769 DeviceExtension
->PortCapabilities
.MaximumPhysicalPages
=
770 ConfigInfo
->NumberOfPhysicalBreaks
;
774 DeviceExtension
->PortCapabilities
.MaximumPhysicalPages
= MapRegistersCount
;
778 /* Update auto request sense feature */
779 DeviceExtension
->SupportsAutoSense
= ConfigInfo
->AutoRequestSense
;
781 /* Update Srb extension size */
782 if (DeviceExtension
->SrbExtensionSize
!= ConfigInfo
->SrbExtensionSize
)
783 DeviceExtension
->SrbExtensionSize
= ConfigInfo
->SrbExtensionSize
;
785 /* Update Srb extension alloc flag */
786 if (ConfigInfo
->AutoRequestSense
|| DeviceExtension
->SrbExtensionSize
)
787 DeviceExtension
->NeedSrbExtensionAlloc
= TRUE
;
789 /* Allocate a common DMA buffer */
790 Status
= SpiAllocateCommonBuffer(DeviceExtension
, NumberOfBytes
);
792 if (!NT_SUCCESS(Status
))
794 DPRINT1("SpiAllocateCommonBuffer() failed with Status = 0x%08X!\n", Status
);
798 return DeviceExtension
->NonCachedExtension
;
802 SpiAllocateCommonBuffer(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
, ULONG NonCachedSize
)
804 PVOID
*SrbExtension
, CommonBuffer
;
805 ULONG CommonBufferLength
, BufSize
;
807 /* If size is 0, set it to 16 */
808 if (!DeviceExtension
->SrbExtensionSize
)
809 DeviceExtension
->SrbExtensionSize
= 16;
812 BufSize
= DeviceExtension
->SrbExtensionSize
;
814 /* Add autosense data size if needed */
815 if (DeviceExtension
->SupportsAutoSense
)
816 BufSize
+= sizeof(SENSE_DATA
);
820 BufSize
= (BufSize
+ sizeof(LONGLONG
) - 1) & ~(sizeof(LONGLONG
) - 1);
822 /* Sum up into the total common buffer length, and round it to page size */
824 ROUND_TO_PAGES(NonCachedSize
+ BufSize
* DeviceExtension
->RequestsNumber
);
827 if (!DeviceExtension
->AdapterObject
)
829 /* From nonpaged pool if there is no DMA */
830 CommonBuffer
= ExAllocatePoolWithTag(NonPagedPool
, CommonBufferLength
, TAG_SCSIPORT
);
834 /* Perform a full request since we have a DMA adapter*/
835 CommonBuffer
= HalAllocateCommonBuffer(DeviceExtension
->AdapterObject
,
837 &DeviceExtension
->PhysicalAddress
,
841 /* Fail in case of error */
843 return STATUS_INSUFFICIENT_RESOURCES
;
846 RtlZeroMemory(CommonBuffer
, CommonBufferLength
);
848 /* Store its size in Device Extension */
849 DeviceExtension
->CommonBufferLength
= CommonBufferLength
;
851 /* SrbExtension buffer is located at the beginning of the buffer */
852 DeviceExtension
->SrbExtensionBuffer
= CommonBuffer
;
854 /* Non-cached extension buffer is located at the end of
858 CommonBufferLength
-= NonCachedSize
;
859 DeviceExtension
->NonCachedExtension
= (PUCHAR
)CommonBuffer
+ CommonBufferLength
;
863 DeviceExtension
->NonCachedExtension
= NULL
;
866 if (DeviceExtension
->NeedSrbExtensionAlloc
)
868 /* Look up how many SRB data structures we need */
869 DeviceExtension
->SrbDataCount
= CommonBufferLength
/ BufSize
;
871 /* Initialize the free SRB extensions list */
872 SrbExtension
= (PVOID
*)CommonBuffer
;
873 DeviceExtension
->FreeSrbExtensions
= SrbExtension
;
875 /* Fill the remainding pointers (if we have more than 1 SRB) */
876 while (CommonBufferLength
>= 2 * BufSize
)
878 *SrbExtension
= (PVOID
*)((PCHAR
)SrbExtension
+ BufSize
);
879 SrbExtension
= *SrbExtension
;
881 CommonBufferLength
-= BufSize
;
885 return STATUS_SUCCESS
;
894 ScsiPortGetVirtualAddress(IN PVOID HwDeviceExtension
,
895 IN SCSI_PHYSICAL_ADDRESS PhysicalAddress
)
897 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
900 DPRINT("ScsiPortGetVirtualAddress(%p %I64x)\n",
901 HwDeviceExtension
, PhysicalAddress
.QuadPart
);
903 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
904 SCSI_PORT_DEVICE_EXTENSION
,
905 MiniPortDeviceExtension
);
907 if (DeviceExtension
->PhysicalAddress
.QuadPart
> PhysicalAddress
.QuadPart
)
910 Offset
= (ULONG
)(PhysicalAddress
.QuadPart
- DeviceExtension
->PhysicalAddress
.QuadPart
);
912 if (Offset
>= DeviceExtension
->CommonBufferLength
)
915 return (PVOID
)((ULONG_PTR
)DeviceExtension
->SrbExtensionBuffer
+ Offset
);
919 SpiInitOpenKeys(PCONFIGURATION_INFO ConfigInfo
, PUNICODE_STRING RegistryPath
)
921 OBJECT_ATTRIBUTES ObjectAttributes
;
922 UNICODE_STRING KeyName
;
925 /* Open the service key */
926 InitializeObjectAttributes(&ObjectAttributes
,
928 OBJ_CASE_INSENSITIVE
,
932 Status
= ZwOpenKey(&ConfigInfo
->ServiceKey
,
936 if (!NT_SUCCESS(Status
))
938 DPRINT("Unable to open driver's registry key %wZ, status 0x%08x\n", RegistryPath
, Status
);
939 ConfigInfo
->ServiceKey
= NULL
;
942 /* If we could open driver's service key, then proceed to the Parameters key */
943 if (ConfigInfo
->ServiceKey
!= NULL
)
945 RtlInitUnicodeString(&KeyName
, L
"Parameters");
946 InitializeObjectAttributes(&ObjectAttributes
,
948 OBJ_CASE_INSENSITIVE
,
949 ConfigInfo
->ServiceKey
,
950 (PSECURITY_DESCRIPTOR
) NULL
);
953 Status
= ZwOpenKey(&ConfigInfo
->DeviceKey
,
957 if (NT_SUCCESS(Status
))
959 /* Yes, Parameters key exist, and it must be used instead of
961 ZwClose(ConfigInfo
->ServiceKey
);
962 ConfigInfo
->ServiceKey
= ConfigInfo
->DeviceKey
;
963 ConfigInfo
->DeviceKey
= NULL
;
967 if (ConfigInfo
->ServiceKey
!= NULL
)
969 /* Open the Device key */
970 RtlInitUnicodeString(&KeyName
, L
"Device");
971 InitializeObjectAttributes(&ObjectAttributes
,
973 OBJ_CASE_INSENSITIVE
,
974 ConfigInfo
->ServiceKey
,
977 /* We don't check for failure here - not needed */
978 ZwOpenKey(&ConfigInfo
->DeviceKey
,
985 /**********************************************************************
990 * Initializes SCSI port driver specific data.
997 * Pointer to the miniport driver's driver object.
1000 * Pointer to the miniport driver's registry path.
1002 * HwInitializationData
1003 * Pointer to port driver specific configuration data.
1006 Miniport driver specific context.
1015 ScsiPortInitialize(IN PVOID Argument1
,
1017 IN
struct _HW_INITIALIZATION_DATA
*HwInitializationData
,
1020 PDRIVER_OBJECT DriverObject
= (PDRIVER_OBJECT
)Argument1
;
1021 PUNICODE_STRING RegistryPath
= (PUNICODE_STRING
)Argument2
;
1022 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
= NULL
;
1023 PCONFIGURATION_INFORMATION SystemConfig
;
1024 PPORT_CONFIGURATION_INFORMATION PortConfig
;
1025 PORT_CONFIGURATION_INFORMATION InitialPortConfig
;
1026 CONFIGURATION_INFO ConfigInfo
;
1027 ULONG DeviceExtensionSize
;
1028 ULONG PortConfigSize
;
1030 BOOLEAN DeviceFound
= FALSE
;
1031 BOOLEAN FirstConfigCall
= TRUE
;
1035 ULONG BusNumber
= 0;
1036 PCI_SLOT_NUMBER SlotNumber
;
1038 PDEVICE_OBJECT PortDeviceObject
;
1039 WCHAR NameBuffer
[80];
1040 UNICODE_STRING DeviceName
;
1041 WCHAR DosNameBuffer
[80];
1042 UNICODE_STRING DosDeviceName
;
1043 PIO_SCSI_CAPABILITIES PortCapabilities
;
1048 PCM_RESOURCE_LIST ResourceList
;
1052 DPRINT ("ScsiPortInitialize() called!\n");
1054 /* Check params for validity */
1055 if ((HwInitializationData
->HwInitialize
== NULL
) ||
1056 (HwInitializationData
->HwStartIo
== NULL
) ||
1057 (HwInitializationData
->HwInterrupt
== NULL
) ||
1058 (HwInitializationData
->HwFindAdapter
== NULL
) ||
1059 (HwInitializationData
->HwResetBus
== NULL
))
1061 return STATUS_INVALID_PARAMETER
;
1065 DriverObject
->DriverStartIo
= ScsiPortStartIo
;
1066 DriverObject
->MajorFunction
[IRP_MJ_CREATE
] = ScsiPortCreateClose
;
1067 DriverObject
->MajorFunction
[IRP_MJ_CLOSE
] = ScsiPortCreateClose
;
1068 DriverObject
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = ScsiPortDeviceControl
;
1069 DriverObject
->MajorFunction
[IRP_MJ_INTERNAL_DEVICE_CONTROL
] = ScsiPortDeviceControl
;
1070 DriverObject
->MajorFunction
[IRP_MJ_SCSI
] = ScsiPortDispatchScsi
;
1072 /* Obtain configuration information */
1073 SystemConfig
= IoGetConfigurationInformation();
1075 /* Zero the internal configuration info structure */
1076 RtlZeroMemory(&ConfigInfo
, sizeof(CONFIGURATION_INFO
));
1078 /* Zero starting slot number */
1079 SlotNumber
.u
.AsULONG
= 0;
1081 /* Allocate space for access ranges */
1082 if (HwInitializationData
->NumberOfAccessRanges
)
1084 ConfigInfo
.AccessRanges
=
1085 ExAllocatePoolWithTag(PagedPool
,
1086 HwInitializationData
->NumberOfAccessRanges
* sizeof(ACCESS_RANGE
), TAG_SCSIPORT
);
1088 /* Fail if failed */
1089 if (ConfigInfo
.AccessRanges
== NULL
)
1090 return STATUS_INSUFFICIENT_RESOURCES
;
1093 /* Open registry keys */
1094 SpiInitOpenKeys(&ConfigInfo
, (PUNICODE_STRING
)Argument2
);
1096 /* Last adapter number = not known */
1097 ConfigInfo
.LastAdapterNumber
= SP_UNINITIALIZED_VALUE
;
1099 /* Calculate sizes of DeviceExtension and PortConfig */
1100 DeviceExtensionSize
= sizeof(SCSI_PORT_DEVICE_EXTENSION
) +
1101 HwInitializationData
->DeviceExtensionSize
;
1103 MaxBus
= (HwInitializationData
->AdapterInterfaceType
== PCIBus
) ? 8 : 1;
1104 DPRINT("MaxBus: %lu\n", MaxBus
);
1108 /* Create a unicode device name */
1109 swprintf(NameBuffer
,
1110 L
"\\Device\\ScsiPort%lu",
1111 SystemConfig
->ScsiPortCount
);
1112 RtlInitUnicodeString(&DeviceName
, NameBuffer
);
1114 DPRINT("Creating device: %wZ\n", &DeviceName
);
1116 /* Create the port device */
1117 Status
= IoCreateDevice(DriverObject
,
1118 DeviceExtensionSize
,
1120 FILE_DEVICE_CONTROLLER
,
1125 if (!NT_SUCCESS(Status
))
1127 DPRINT1("IoCreateDevice call failed! (Status 0x%lX)\n", Status
);
1128 PortDeviceObject
= NULL
;
1132 DPRINT ("Created device: %wZ (%p)\n", &DeviceName
, PortDeviceObject
);
1134 /* Set the buffering strategy here... */
1135 PortDeviceObject
->Flags
|= DO_DIRECT_IO
;
1136 PortDeviceObject
->AlignmentRequirement
= FILE_WORD_ALIGNMENT
; /* FIXME: Is this really needed? */
1138 /* Fill Device Extension */
1139 DeviceExtension
= PortDeviceObject
->DeviceExtension
;
1140 DeviceExtension
->Length
= DeviceExtensionSize
;
1141 DeviceExtension
->DeviceObject
= PortDeviceObject
;
1142 DeviceExtension
->PortNumber
= SystemConfig
->ScsiPortCount
;
1144 /* Driver's routines... */
1145 DeviceExtension
->HwInitialize
= HwInitializationData
->HwInitialize
;
1146 DeviceExtension
->HwStartIo
= HwInitializationData
->HwStartIo
;
1147 DeviceExtension
->HwInterrupt
= HwInitializationData
->HwInterrupt
;
1148 DeviceExtension
->HwResetBus
= HwInitializationData
->HwResetBus
;
1149 DeviceExtension
->HwDmaStarted
= HwInitializationData
->HwDmaStarted
;
1151 /* Extensions sizes */
1152 DeviceExtension
->MiniPortExtensionSize
= HwInitializationData
->DeviceExtensionSize
;
1153 DeviceExtension
->LunExtensionSize
= HwInitializationData
->SpecificLuExtensionSize
;
1154 DeviceExtension
->SrbExtensionSize
= HwInitializationData
->SrbExtensionSize
;
1156 /* Round Srb extension size to the quadword */
1157 DeviceExtension
->SrbExtensionSize
=
1158 ~(sizeof(LONGLONG
) - 1) & (DeviceExtension
->SrbExtensionSize
+
1159 sizeof(LONGLONG
) - 1);
1161 /* Fill some numbers (bus count, lun count, etc) */
1162 DeviceExtension
->MaxLunCount
= SCSI_MAXIMUM_LOGICAL_UNITS
;
1163 DeviceExtension
->RequestsNumber
= 16;
1165 /* Initialize the spin lock in the controller extension */
1166 KeInitializeSpinLock(&DeviceExtension
->IrqLock
);
1167 KeInitializeSpinLock(&DeviceExtension
->SpinLock
);
1169 /* Initialize the DPC object */
1170 IoInitializeDpcRequest(PortDeviceObject
,
1173 /* Initialize the device timer */
1174 DeviceExtension
->TimerCount
= -1;
1175 IoInitializeTimer(PortDeviceObject
,
1179 /* Initialize miniport timer */
1180 KeInitializeTimer(&DeviceExtension
->MiniportTimer
);
1181 KeInitializeDpc(&DeviceExtension
->MiniportTimerDpc
,
1182 SpiMiniportTimerDpc
,
1187 Status
= SpiCreatePortConfig(DeviceExtension
,
1188 HwInitializationData
,
1193 if (!NT_SUCCESS(Status
))
1195 DPRINT("SpiCreatePortConfig() failed with Status 0x%08X\n", Status
);
1199 /* Allocate and initialize port configuration info */
1200 PortConfigSize
= (sizeof(PORT_CONFIGURATION_INFORMATION
) +
1201 HwInitializationData
->NumberOfAccessRanges
*
1202 sizeof(ACCESS_RANGE
) + 7) & ~7;
1203 DeviceExtension
->PortConfig
= ExAllocatePoolWithTag(NonPagedPool
, PortConfigSize
, TAG_SCSIPORT
);
1205 /* Fail if failed */
1206 if (DeviceExtension
->PortConfig
== NULL
)
1208 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1212 PortConfig
= DeviceExtension
->PortConfig
;
1214 /* Copy information here */
1215 RtlCopyMemory(PortConfig
,
1217 sizeof(PORT_CONFIGURATION_INFORMATION
));
1220 /* Copy extension sizes into the PortConfig */
1221 PortConfig
->SpecificLuExtensionSize
= DeviceExtension
->LunExtensionSize
;
1222 PortConfig
->SrbExtensionSize
= DeviceExtension
->SrbExtensionSize
;
1224 /* Initialize Access ranges */
1225 if (HwInitializationData
->NumberOfAccessRanges
!= 0)
1227 PortConfig
->AccessRanges
= (PVOID
)(PortConfig
+1);
1229 /* Align to LONGLONG */
1230 PortConfig
->AccessRanges
= (PVOID
)((ULONG_PTR
)(PortConfig
->AccessRanges
) + 7);
1231 PortConfig
->AccessRanges
= (PVOID
)((ULONG_PTR
)(PortConfig
->AccessRanges
) & ~7);
1234 RtlCopyMemory(PortConfig
->AccessRanges
,
1235 ConfigInfo
.AccessRanges
,
1236 HwInitializationData
->NumberOfAccessRanges
* sizeof(ACCESS_RANGE
));
1239 /* Search for matching PCI device */
1240 if ((HwInitializationData
->AdapterInterfaceType
== PCIBus
) &&
1241 (HwInitializationData
->VendorIdLength
> 0) &&
1242 (HwInitializationData
->VendorId
!= NULL
) &&
1243 (HwInitializationData
->DeviceIdLength
> 0) &&
1244 (HwInitializationData
->DeviceId
!= NULL
))
1246 PortConfig
->BusInterruptLevel
= 0;
1248 /* Get PCI device data */
1249 DPRINT("VendorId '%.*s' DeviceId '%.*s'\n",
1250 HwInitializationData
->VendorIdLength
,
1251 HwInitializationData
->VendorId
,
1252 HwInitializationData
->DeviceIdLength
,
1253 HwInitializationData
->DeviceId
);
1255 if (!SpiGetPciConfigData(DriverObject
,
1257 HwInitializationData
,
1260 ConfigInfo
.BusNumber
,
1263 /* Continue to the next bus, nothing here */
1264 ConfigInfo
.BusNumber
++;
1265 DeviceExtension
->PortConfig
= NULL
;
1266 ExFreePool(PortConfig
);
1268 goto CreatePortConfig
;
1271 if (!PortConfig
->BusInterruptLevel
)
1273 /* Bypass this slot, because no interrupt was assigned */
1274 DeviceExtension
->PortConfig
= NULL
;
1275 ExFreePool(PortConfig
);
1276 goto CreatePortConfig
;
1281 DPRINT("Non-pci bus\n");
1284 /* Note: HwFindAdapter is called once for each bus */
1286 DPRINT("Calling HwFindAdapter() for Bus %lu\n", PortConfig
->SystemIoBusNumber
);
1287 Result
= (HwInitializationData
->HwFindAdapter
)(&DeviceExtension
->MiniPortDeviceExtension
,
1289 0, /* BusInformation */
1290 ConfigInfo
.Parameter
, /* ArgumentString */
1294 DPRINT("HwFindAdapter() Result: %lu Again: %s\n",
1295 Result
, (Again
) ? "True" : "False");
1297 /* Free MapRegisterBase, it's not needed anymore */
1298 if (DeviceExtension
->MapRegisterBase
!= NULL
)
1300 ExFreePool(DeviceExtension
->MapRegisterBase
);
1301 DeviceExtension
->MapRegisterBase
= NULL
;
1304 /* If result is nothing good... */
1305 if (Result
!= SP_RETURN_FOUND
)
1307 DPRINT("HwFindAdapter() Result: %lu\n", Result
);
1309 if (Result
== SP_RETURN_NOT_FOUND
)
1311 /* We can continue on the next bus */
1312 ConfigInfo
.BusNumber
++;
1315 DeviceExtension
->PortConfig
= NULL
;
1316 ExFreePool(PortConfig
);
1317 goto CreatePortConfig
;
1320 /* Otherwise, break */
1321 Status
= STATUS_INTERNAL_ERROR
;
1325 DPRINT("ScsiPortInitialize(): Found HBA! (%x), adapter Id %d\n",
1326 PortConfig
->BusInterruptVector
, PortConfig
->InitiatorBusId
[0]);
1328 /* If the SRB extension size was updated */
1329 if (!DeviceExtension
->NonCachedExtension
&&
1330 (PortConfig
->SrbExtensionSize
!= DeviceExtension
->SrbExtensionSize
))
1332 /* Set it (rounding to LONGLONG again) */
1333 DeviceExtension
->SrbExtensionSize
=
1334 (PortConfig
->SrbExtensionSize
+
1335 sizeof(LONGLONG
)) & ~(sizeof(LONGLONG
) - 1);
1338 /* The same with LUN extension size */
1339 if (PortConfig
->SpecificLuExtensionSize
!= DeviceExtension
->LunExtensionSize
)
1340 DeviceExtension
->LunExtensionSize
= PortConfig
->SpecificLuExtensionSize
;
1343 if (!((HwInitializationData
->AdapterInterfaceType
== PCIBus
) &&
1344 (HwInitializationData
->VendorIdLength
> 0) &&
1345 (HwInitializationData
->VendorId
!= NULL
) &&
1346 (HwInitializationData
->DeviceIdLength
> 0) &&
1347 (HwInitializationData
->DeviceId
!= NULL
)))
1349 /* Construct a resource list */
1350 ResourceList
= SpiConfigToResource(DeviceExtension
,
1355 UNICODE_STRING UnicodeString
;
1356 RtlInitUnicodeString(&UnicodeString
, L
"ScsiAdapter");
1357 DPRINT("Reporting resources\n");
1358 Status
= IoReportResourceUsage(&UnicodeString
,
1364 FIELD_OFFSET(CM_RESOURCE_LIST
,
1365 List
[0].PartialResourceList
.PartialDescriptors
) +
1366 ResourceList
->List
[0].PartialResourceList
.Count
1367 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
),
1370 ExFreePool(ResourceList
);
1372 /* In case of a failure or a conflict, break */
1373 if (Conflict
|| (!NT_SUCCESS(Status
)))
1376 Status
= STATUS_CONFLICTING_ADDRESSES
;
1382 /* Reset the Conflict var */
1385 /* Copy all stuff which we ever need from PortConfig to the DeviceExtension */
1386 if (PortConfig
->MaximumNumberOfTargets
> SCSI_MAXIMUM_TARGETS_PER_BUS
)
1387 DeviceExtension
->MaxTargedIds
= SCSI_MAXIMUM_TARGETS_PER_BUS
;
1389 DeviceExtension
->MaxTargedIds
= PortConfig
->MaximumNumberOfTargets
;
1391 DeviceExtension
->BusNum
= PortConfig
->NumberOfBuses
;
1392 DeviceExtension
->CachesData
= PortConfig
->CachesData
;
1393 DeviceExtension
->ReceiveEvent
= PortConfig
->ReceiveEvent
;
1394 DeviceExtension
->SupportsTaggedQueuing
= PortConfig
->TaggedQueuing
;
1395 DeviceExtension
->MultipleReqsPerLun
= PortConfig
->MultipleRequestPerLu
;
1397 /* If something was disabled via registry - apply it */
1398 if (ConfigInfo
.DisableMultipleLun
)
1399 DeviceExtension
->MultipleReqsPerLun
= PortConfig
->MultipleRequestPerLu
= FALSE
;
1401 if (ConfigInfo
.DisableTaggedQueueing
)
1402 DeviceExtension
->SupportsTaggedQueuing
= PortConfig
->MultipleRequestPerLu
= FALSE
;
1404 /* Check if we need to alloc SRB data */
1405 if (DeviceExtension
->SupportsTaggedQueuing
||
1406 DeviceExtension
->MultipleReqsPerLun
)
1408 DeviceExtension
->NeedSrbDataAlloc
= TRUE
;
1412 DeviceExtension
->NeedSrbDataAlloc
= FALSE
;
1415 /* Get a pointer to the port capabilities */
1416 PortCapabilities
= &DeviceExtension
->PortCapabilities
;
1418 /* Copy one field there */
1419 DeviceExtension
->MapBuffers
= PortConfig
->MapBuffers
;
1420 PortCapabilities
->AdapterUsesPio
= PortConfig
->MapBuffers
;
1422 if (DeviceExtension
->AdapterObject
== NULL
&&
1423 (PortConfig
->DmaChannel
!= SP_UNINITIALIZED_VALUE
|| PortConfig
->Master
))
1425 DPRINT1("DMA is not supported yet\n");
1429 if (DeviceExtension
->SrbExtensionBuffer
== NULL
&&
1430 (DeviceExtension
->SrbExtensionSize
!= 0 ||
1431 PortConfig
->AutoRequestSense
))
1433 DeviceExtension
->SupportsAutoSense
= PortConfig
->AutoRequestSense
;
1434 DeviceExtension
->NeedSrbExtensionAlloc
= TRUE
;
1436 /* Allocate common buffer */
1437 Status
= SpiAllocateCommonBuffer(DeviceExtension
, 0);
1439 /* Check for failure */
1440 if (!NT_SUCCESS(Status
))
1444 /* Allocate SrbData, if needed */
1445 if (DeviceExtension
->NeedSrbDataAlloc
)
1448 PSCSI_REQUEST_BLOCK_INFO SrbData
;
1450 if (DeviceExtension
->SrbDataCount
!= 0)
1451 Count
= DeviceExtension
->SrbDataCount
;
1453 Count
= DeviceExtension
->RequestsNumber
* 2;
1455 /* Allocate the data */
1456 SrbData
= ExAllocatePoolWithTag(NonPagedPool
, Count
* sizeof(SCSI_REQUEST_BLOCK_INFO
), TAG_SCSIPORT
);
1457 if (SrbData
== NULL
)
1458 return STATUS_INSUFFICIENT_RESOURCES
;
1460 RtlZeroMemory(SrbData
, Count
* sizeof(SCSI_REQUEST_BLOCK_INFO
));
1462 DeviceExtension
->SrbInfo
= SrbData
;
1463 DeviceExtension
->FreeSrbInfo
= SrbData
;
1464 DeviceExtension
->SrbDataCount
= Count
;
1466 /* Link it to the list */
1469 SrbData
->Requests
.Flink
= (PLIST_ENTRY
)(SrbData
+ 1);
1474 /* Mark the last entry of the list */
1476 SrbData
->Requests
.Flink
= NULL
;
1479 /* Initialize port capabilities */
1480 PortCapabilities
= &DeviceExtension
->PortCapabilities
;
1481 PortCapabilities
->Length
= sizeof(IO_SCSI_CAPABILITIES
);
1482 PortCapabilities
->MaximumTransferLength
= PortConfig
->MaximumTransferLength
;
1484 if (PortConfig
->ReceiveEvent
)
1485 PortCapabilities
->SupportedAsynchronousEvents
|= SRBEV_SCSI_ASYNC_NOTIFICATION
;
1487 PortCapabilities
->TaggedQueuing
= DeviceExtension
->SupportsTaggedQueuing
;
1488 PortCapabilities
->AdapterScansDown
= PortConfig
->AdapterScansDown
;
1490 if (PortConfig
->AlignmentMask
> PortDeviceObject
->AlignmentRequirement
)
1491 PortDeviceObject
->AlignmentRequirement
= PortConfig
->AlignmentMask
;
1493 PortCapabilities
->AlignmentMask
= PortDeviceObject
->AlignmentRequirement
;
1495 if (PortCapabilities
->MaximumPhysicalPages
== 0)
1497 PortCapabilities
->MaximumPhysicalPages
=
1498 BYTES_TO_PAGES(PortCapabilities
->MaximumTransferLength
);
1500 /* Apply miniport's limits */
1501 if (PortConfig
->NumberOfPhysicalBreaks
< PortCapabilities
->MaximumPhysicalPages
)
1503 PortCapabilities
->MaximumPhysicalPages
= PortConfig
->NumberOfPhysicalBreaks
;
1507 /* Deal with interrupts */
1508 if (DeviceExtension
->HwInterrupt
== NULL
||
1509 (PortConfig
->BusInterruptLevel
== 0 && PortConfig
->BusInterruptVector
== 0))
1512 KeInitializeSpinLock(&DeviceExtension
->IrqLock
);
1514 /* FIXME: Use synchronization routine */
1515 ASSERT("No interrupts branch requires changes in synchronization\n");
1517 DeviceExtension
->Interrupt
= (PVOID
)DeviceExtension
;
1518 DPRINT("No interrupts\n");
1523 /* Are 2 interrupts needed? */
1524 if (DeviceExtension
->HwInterrupt
!= NULL
&&
1525 (PortConfig
->BusInterruptLevel
!= 0 || PortConfig
->BusInterruptVector
!= 0) &&
1526 (PortConfig
->BusInterruptLevel2
!= 0 || PortConfig
->BusInterruptVector2
!= 0))
1528 DPRINT1("2 interrupts requested! Not yet supported\n");
1533 BOOLEAN InterruptShareable
;
1535 /* No, only 1 interrupt */
1536 DPRINT("1 interrupt, IRQ is %d\n", PortConfig
->BusInterruptLevel
);
1538 DeviceExtension
->InterruptLevel
= PortConfig
->BusInterruptLevel
;
1540 /* Register an interrupt handler for this device */
1541 MappedIrq
= HalGetInterruptVector(PortConfig
->AdapterInterfaceType
,
1542 PortConfig
->SystemIoBusNumber
,
1543 PortConfig
->BusInterruptLevel
,
1544 PortConfig
->BusInterruptVector
,
1548 /* Determing IRQ sharability as usual */
1549 if (PortConfig
->AdapterInterfaceType
== MicroChannel
||
1550 PortConfig
->InterruptMode
== LevelSensitive
)
1552 InterruptShareable
= TRUE
;
1556 InterruptShareable
= FALSE
;
1559 Status
= IoConnectInterrupt(&DeviceExtension
->Interrupt
,
1560 (PKSERVICE_ROUTINE
)ScsiPortIsr
,
1566 PortConfig
->InterruptMode
,
1571 if (!(NT_SUCCESS(Status
)))
1573 DPRINT1("Could not connect interrupt %d\n",
1574 PortConfig
->BusInterruptVector
);
1575 DeviceExtension
->Interrupt
= NULL
;
1582 /* Save IoAddress (from access ranges) */
1583 if (HwInitializationData
->NumberOfAccessRanges
!= 0)
1585 DeviceExtension
->IoAddress
=
1586 ((*(PortConfig
->AccessRanges
))[0]).RangeStart
.LowPart
;
1588 DPRINT("Io Address %x\n", DeviceExtension
->IoAddress
);
1591 /* Set flag that it's allowed to disconnect during this command */
1592 DeviceExtension
->Flags
|= SCSI_PORT_DISCONNECT_ALLOWED
;
1594 /* Initialize counter of active requests (-1 means there are none) */
1595 DeviceExtension
->ActiveRequestCounter
= -1;
1597 /* Analyze what we have about DMA */
1598 if (DeviceExtension
->AdapterObject
!= NULL
&&
1599 PortConfig
->Master
&&
1600 PortConfig
->NeedPhysicalAddresses
)
1602 DeviceExtension
->MapRegisters
= TRUE
;
1606 DeviceExtension
->MapRegisters
= FALSE
;
1609 /* Call HwInitialize at DISPATCH_LEVEL */
1610 KeRaiseIrql(DISPATCH_LEVEL
, &Dirql
);
1612 if (!KeSynchronizeExecution(DeviceExtension
->Interrupt
,
1613 DeviceExtension
->HwInitialize
,
1614 DeviceExtension
->MiniPortDeviceExtension
))
1616 DPRINT1("HwInitialize() failed!\n");
1618 Status
= STATUS_ADAPTER_HARDWARE_ERROR
;
1622 /* Check if a notification is needed */
1623 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
1625 /* Call DPC right away, because we're already at DISPATCH_LEVEL */
1626 ScsiPortDpcForIsr(NULL
,
1627 DeviceExtension
->DeviceObject
,
1632 /* Lower irql back to what it was */
1635 /* Start our timer */
1636 IoStartTimer(PortDeviceObject
);
1638 /* Initialize bus scanning information */
1639 DeviceExtension
->BusesConfig
= ExAllocatePoolWithTag(PagedPool
,
1640 sizeof(PSCSI_BUS_SCAN_INFO
) * DeviceExtension
->PortConfig
->NumberOfBuses
1641 + sizeof(ULONG
), TAG_SCSIPORT
);
1643 if (!DeviceExtension
->BusesConfig
)
1645 DPRINT1("Out of resources!\n");
1646 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1651 RtlZeroMemory(DeviceExtension
->BusesConfig
,
1652 sizeof(PSCSI_BUS_SCAN_INFO
) * DeviceExtension
->PortConfig
->NumberOfBuses
1655 /* Store number of buses there */
1656 DeviceExtension
->BusesConfig
->NumberOfBuses
= (UCHAR
)DeviceExtension
->BusNum
;
1658 /* Scan the adapter for devices */
1659 SpiScanAdapter(DeviceExtension
);
1661 /* Build the registry device map */
1662 SpiBuildDeviceMap(DeviceExtension
,
1663 (PUNICODE_STRING
)Argument2
);
1665 /* Create the dos device link */
1666 swprintf(DosNameBuffer
,
1668 SystemConfig
->ScsiPortCount
);
1669 RtlInitUnicodeString(&DosDeviceName
, DosNameBuffer
);
1670 IoCreateSymbolicLink(&DosDeviceName
, &DeviceName
);
1672 /* Increase the port count */
1673 SystemConfig
->ScsiPortCount
++;
1674 FirstConfigCall
= FALSE
;
1676 /* Increase adapter number and bus number respectively */
1677 ConfigInfo
.AdapterNumber
++;
1680 ConfigInfo
.BusNumber
++;
1682 DPRINT("Bus: %lu MaxBus: %lu\n", BusNumber
, MaxBus
);
1687 /* Clean up the mess */
1688 SpiCleanupAfterInit(DeviceExtension
);
1690 /* Close registry keys */
1691 if (ConfigInfo
.ServiceKey
!= NULL
)
1692 ZwClose(ConfigInfo
.ServiceKey
);
1694 if (ConfigInfo
.DeviceKey
!= NULL
)
1695 ZwClose(ConfigInfo
.DeviceKey
);
1697 if (ConfigInfo
.BusKey
!= NULL
)
1698 ZwClose(ConfigInfo
.BusKey
);
1700 if (ConfigInfo
.AccessRanges
!= NULL
)
1701 ExFreePool(ConfigInfo
.AccessRanges
);
1703 if (ConfigInfo
.Parameter
!= NULL
)
1704 ExFreePool(ConfigInfo
.Parameter
);
1706 DPRINT("ScsiPortInitialize() done, Status = 0x%08X, DeviceFound = %d!\n",
1707 Status
, DeviceFound
);
1709 return (DeviceFound
== FALSE
) ? Status
: STATUS_SUCCESS
;
1713 SpiCleanupAfterInit(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
)
1715 PSCSI_LUN_INFO LunInfo
;
1719 /* Check if we have something to clean up */
1720 if (DeviceExtension
== NULL
)
1723 /* Stop the timer and disconnect the interrupt */
1724 if (DeviceExtension
->Interrupt
)
1726 IoStopTimer(DeviceExtension
->DeviceObject
);
1727 IoDisconnectInterrupt(DeviceExtension
->Interrupt
);
1730 /* Delete ConfigInfo */
1731 if (DeviceExtension
->BusesConfig
)
1733 for (Bus
= 0; Bus
< DeviceExtension
->BusNum
; Bus
++)
1735 if (!DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
])
1738 LunInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LunInfo
;
1742 /* Free current, but save pointer to the next one */
1743 Ptr
= LunInfo
->Next
;
1744 ExFreePool(LunInfo
);
1748 ExFreePool(DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]);
1751 ExFreePool(DeviceExtension
->BusesConfig
);
1754 /* Free PortConfig */
1755 if (DeviceExtension
->PortConfig
)
1756 ExFreePool(DeviceExtension
->PortConfig
);
1759 for(Lun
= 0; Lun
< LUS_NUMBER
; Lun
++)
1761 while (DeviceExtension
->LunExtensionList
[Lun
])
1763 Ptr
= DeviceExtension
->LunExtensionList
[Lun
];
1764 DeviceExtension
->LunExtensionList
[Lun
] = DeviceExtension
->LunExtensionList
[Lun
]->Next
;
1770 /* Free common buffer (if it exists) */
1771 if (DeviceExtension
->SrbExtensionBuffer
!= NULL
&&
1772 DeviceExtension
->CommonBufferLength
!= 0)
1774 if (!DeviceExtension
->AdapterObject
)
1776 ExFreePool(DeviceExtension
->SrbExtensionBuffer
);
1780 HalFreeCommonBuffer(DeviceExtension
->AdapterObject
,
1781 DeviceExtension
->CommonBufferLength
,
1782 DeviceExtension
->PhysicalAddress
,
1783 DeviceExtension
->SrbExtensionBuffer
,
1789 if (DeviceExtension
->SrbInfo
!= NULL
)
1790 ExFreePool(DeviceExtension
->SrbInfo
);
1792 /* Unmap mapped addresses */
1793 while (DeviceExtension
->MappedAddressList
!= NULL
)
1795 MmUnmapIoSpace(DeviceExtension
->MappedAddressList
->MappedAddress
,
1796 DeviceExtension
->MappedAddressList
->NumberOfBytes
);
1798 Ptr
= DeviceExtension
->MappedAddressList
;
1799 DeviceExtension
->MappedAddressList
= DeviceExtension
->MappedAddressList
->NextMappedAddress
;
1804 /* Finally delete the device object */
1805 DPRINT("Deleting device %p\n", DeviceExtension
->DeviceObject
);
1806 IoDeleteDevice(DeviceExtension
->DeviceObject
);
1813 ScsiPortIoMapTransfer(IN PVOID HwDeviceExtension
,
1814 IN PSCSI_REQUEST_BLOCK Srb
,
1815 IN PVOID LogicalAddress
,
1818 DPRINT1("ScsiPortIoMapTransfer()\n");
1826 ScsiPortLogError(IN PVOID HwDeviceExtension
,
1827 IN PSCSI_REQUEST_BLOCK Srb OPTIONAL
,
1834 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
1836 DPRINT1("ScsiPortLogError() called\n");
1838 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
1839 SCSI_PORT_DEVICE_EXTENSION
,
1840 MiniPortDeviceExtension
);
1843 DPRINT("ScsiPortLogError() done\n");
1850 ScsiPortMoveMemory(OUT PVOID Destination
,
1854 RtlMoveMemory(Destination
,
1864 ScsiPortNotification(IN SCSI_NOTIFICATION_TYPE NotificationType
,
1865 IN PVOID HwDeviceExtension
,
1868 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
1871 DPRINT("ScsiPortNotification() called\n");
1873 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
1874 SCSI_PORT_DEVICE_EXTENSION
,
1875 MiniPortDeviceExtension
);
1877 DPRINT("DeviceExtension %p\n", DeviceExtension
);
1879 va_start(ap
, HwDeviceExtension
);
1881 switch (NotificationType
)
1883 case RequestComplete
:
1885 PSCSI_REQUEST_BLOCK Srb
;
1886 PSCSI_REQUEST_BLOCK_INFO SrbData
;
1888 Srb
= (PSCSI_REQUEST_BLOCK
) va_arg (ap
, PSCSI_REQUEST_BLOCK
);
1890 DPRINT("Notify: RequestComplete (Srb %p)\n", Srb
);
1892 /* Make sure Srb is allright */
1893 ASSERT(Srb
->SrbStatus
!= SRB_STATUS_PENDING
);
1894 ASSERT(Srb
->Function
!= SRB_FUNCTION_EXECUTE_SCSI
|| Srb
->SrbStatus
!= SRB_STATUS_SUCCESS
|| Srb
->ScsiStatus
== SCSISTAT_GOOD
);
1896 if (!(Srb
->SrbFlags
& SRB_FLAGS_IS_ACTIVE
))
1898 /* It's been already completed */
1903 /* It's not active anymore */
1904 Srb
->SrbFlags
&= ~SRB_FLAGS_IS_ACTIVE
;
1906 if (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
)
1908 /* TODO: Treat it specially */
1913 /* Get the SRB data */
1914 SrbData
= SpiGetSrbData(DeviceExtension
,
1920 /* Make sure there are no CompletedRequests and there is a Srb */
1921 ASSERT(SrbData
->CompletedRequests
== NULL
&& SrbData
->Srb
!= NULL
);
1923 /* If it's a read/write request, make sure it has data inside it */
1924 if ((Srb
->SrbStatus
== SRB_STATUS_SUCCESS
) &&
1925 ((Srb
->Cdb
[0] == SCSIOP_READ
) || (Srb
->Cdb
[0] == SCSIOP_WRITE
)))
1927 ASSERT(Srb
->DataTransferLength
);
1930 SrbData
->CompletedRequests
= DeviceExtension
->InterruptData
.CompletedRequests
;
1931 DeviceExtension
->InterruptData
.CompletedRequests
= SrbData
;
1937 DPRINT("Notify: NextRequest\n");
1938 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_NEXT_REQUEST_READY
;
1946 PSCSI_PORT_LUN_EXTENSION LunExtension
;
1948 PathId
= (UCHAR
) va_arg (ap
, int);
1949 TargetId
= (UCHAR
) va_arg (ap
, int);
1950 Lun
= (UCHAR
) va_arg (ap
, int);
1952 DPRINT("Notify: NextLuRequest(PathId %u TargetId %u Lun %u)\n",
1953 PathId
, TargetId
, Lun
);
1955 /* Mark it in the flags field */
1956 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_NEXT_REQUEST_READY
;
1958 /* Get the LUN extension */
1959 LunExtension
= SpiGetLunExtension(DeviceExtension
,
1964 /* If returned LunExtension is NULL, break out */
1965 if (!LunExtension
) break;
1967 /* This request should not be processed if */
1968 if ((LunExtension
->ReadyLun
) ||
1969 (LunExtension
->SrbInfo
.Srb
))
1971 /* Nothing to do here */
1975 /* Add this LUN to the list */
1976 LunExtension
->ReadyLun
= DeviceExtension
->InterruptData
.ReadyLun
;
1977 DeviceExtension
->InterruptData
.ReadyLun
= LunExtension
;
1982 DPRINT("Notify: ResetDetected\n");
1983 /* Add RESET flags */
1984 DeviceExtension
->InterruptData
.Flags
|=
1985 SCSI_PORT_RESET
| SCSI_PORT_RESET_REPORTED
;
1988 case CallDisableInterrupts
:
1989 DPRINT1("UNIMPLEMENTED SCSI Notification called: CallDisableInterrupts!\n");
1992 case CallEnableInterrupts
:
1993 DPRINT1("UNIMPLEMENTED SCSI Notification called: CallEnableInterrupts!\n");
1996 case RequestTimerCall
:
1997 DPRINT("Notify: RequestTimerCall\n");
1998 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_TIMER_NEEDED
;
1999 DeviceExtension
->InterruptData
.HwScsiTimer
= (PHW_TIMER
)va_arg(ap
, PHW_TIMER
);
2000 DeviceExtension
->InterruptData
.MiniportTimerValue
= (ULONG
)va_arg(ap
, ULONG
);
2003 case BusChangeDetected
:
2004 DPRINT1("UNIMPLEMENTED SCSI Notification called: BusChangeDetected!\n");
2008 DPRINT1 ("Unsupported notification from WMI: %lu\n", NotificationType
);
2014 /* Request a DPC after we're done with the interrupt */
2015 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_NOTIFICATION_NEEDED
;
2022 ScsiPortValidateRange(IN PVOID HwDeviceExtension
,
2023 IN INTERFACE_TYPE BusType
,
2024 IN ULONG SystemIoBusNumber
,
2025 IN SCSI_PHYSICAL_ADDRESS IoAddress
,
2026 IN ULONG NumberOfBytes
,
2027 IN BOOLEAN InIoSpace
)
2029 DPRINT("ScsiPortValidateRange()\n");
2034 /* INTERNAL FUNCTIONS ********************************************************/
2037 SpiResourceToConfig(IN PHW_INITIALIZATION_DATA HwInitializationData
,
2038 IN PCM_FULL_RESOURCE_DESCRIPTOR ResourceDescriptor
,
2039 IN PPORT_CONFIGURATION_INFORMATION PortConfig
)
2041 PACCESS_RANGE AccessRange
;
2042 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialData
;
2048 /* Loop through all entries */
2049 for (Index
= 0; Index
< ResourceDescriptor
->PartialResourceList
.Count
; Index
++)
2051 PartialData
= &ResourceDescriptor
->PartialResourceList
.PartialDescriptors
[Index
];
2053 switch (PartialData
->Type
)
2055 case CmResourceTypePort
:
2056 /* Copy access ranges */
2057 if (RangeNumber
< HwInitializationData
->NumberOfAccessRanges
)
2059 AccessRange
= &((*(PortConfig
->AccessRanges
))[RangeNumber
]);
2061 AccessRange
->RangeStart
= PartialData
->u
.Port
.Start
;
2062 AccessRange
->RangeLength
= PartialData
->u
.Port
.Length
;
2064 AccessRange
->RangeInMemory
= FALSE
;
2069 case CmResourceTypeMemory
:
2070 /* Copy access ranges */
2071 if (RangeNumber
< HwInitializationData
->NumberOfAccessRanges
)
2073 AccessRange
= &((*(PortConfig
->AccessRanges
))[RangeNumber
]);
2075 AccessRange
->RangeStart
= PartialData
->u
.Memory
.Start
;
2076 AccessRange
->RangeLength
= PartialData
->u
.Memory
.Length
;
2078 AccessRange
->RangeInMemory
= TRUE
;
2083 case CmResourceTypeInterrupt
:
2084 /* Copy interrupt data */
2085 PortConfig
->BusInterruptLevel
= PartialData
->u
.Interrupt
.Level
;
2086 PortConfig
->BusInterruptVector
= PartialData
->u
.Interrupt
.Vector
;
2088 /* Set interrupt mode accordingly to the resource */
2089 if (PartialData
->Flags
== CM_RESOURCE_INTERRUPT_LATCHED
)
2091 PortConfig
->InterruptMode
= Latched
;
2093 else if (PartialData
->Flags
== CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE
)
2095 PortConfig
->InterruptMode
= LevelSensitive
;
2099 case CmResourceTypeDma
:
2100 PortConfig
->DmaChannel
= PartialData
->u
.Dma
.Channel
;
2101 PortConfig
->DmaPort
= PartialData
->u
.Dma
.Port
;
2107 static PCM_RESOURCE_LIST
2108 SpiConfigToResource(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
2109 PPORT_CONFIGURATION_INFORMATION PortConfig
)
2111 PCONFIGURATION_INFORMATION ConfigInfo
;
2112 PCM_RESOURCE_LIST ResourceList
;
2113 PCM_PARTIAL_RESOURCE_DESCRIPTOR ResourceDescriptor
;
2114 PACCESS_RANGE AccessRange
;
2116 ULONG ListLength
= 0, i
, FullSize
;
2119 /* Get current Atdisk usage from the system */
2120 ConfigInfo
= IoGetConfigurationInformation();
2122 if (PortConfig
->AtdiskPrimaryClaimed
)
2123 ConfigInfo
->AtDiskPrimaryAddressClaimed
= TRUE
;
2125 if (PortConfig
->AtdiskSecondaryClaimed
)
2126 ConfigInfo
->AtDiskSecondaryAddressClaimed
= TRUE
;
2128 /* Do we use DMA? */
2129 if (PortConfig
->DmaChannel
!= SP_UNINITIALIZED_VALUE
||
2130 PortConfig
->DmaPort
!= SP_UNINITIALIZED_VALUE
)
2140 /* How many interrupts to we have? */
2141 if (DeviceExtension
->HwInterrupt
== NULL
||
2142 (PortConfig
->BusInterruptLevel
== 0 &&
2143 PortConfig
->BusInterruptVector
== 0))
2153 if (DeviceExtension
->HwInterrupt
!= NULL
&&
2154 (PortConfig
->BusInterruptLevel2
!= 0 ||
2155 PortConfig
->BusInterruptVector2
!= 0))
2161 /* How many access ranges do we use? */
2162 AccessRange
= &((*(PortConfig
->AccessRanges
))[0]);
2163 for (i
= 0; i
< PortConfig
->NumberOfAccessRanges
; i
++)
2165 if (AccessRange
->RangeLength
!= 0)
2171 /* Allocate the resource list, since we know its size now */
2172 FullSize
= sizeof(CM_RESOURCE_LIST
) + (ListLength
- 1) *
2173 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
2175 ResourceList
= (PCM_RESOURCE_LIST
)ExAllocatePoolWithTag(PagedPool
, FullSize
, TAG_SCSIPORT
);
2181 RtlZeroMemory(ResourceList
, FullSize
);
2184 ResourceList
->Count
= 1;
2185 ResourceList
->List
[0].InterfaceType
= PortConfig
->AdapterInterfaceType
;
2186 ResourceList
->List
[0].BusNumber
= PortConfig
->SystemIoBusNumber
;
2187 ResourceList
->List
[0].PartialResourceList
.Count
= ListLength
;
2188 ResourceDescriptor
= ResourceList
->List
[0].PartialResourceList
.PartialDescriptors
;
2190 /* Copy access ranges array over */
2191 for (i
= 0; i
< PortConfig
->NumberOfAccessRanges
; i
++)
2193 AccessRange
= &((*(PortConfig
->AccessRanges
))[i
]);
2195 /* If the range is empty - skip it */
2196 if (AccessRange
->RangeLength
== 0)
2199 if (AccessRange
->RangeInMemory
)
2201 ResourceDescriptor
->Type
= CmResourceTypeMemory
;
2202 ResourceDescriptor
->Flags
= CM_RESOURCE_MEMORY_READ_WRITE
;
2206 ResourceDescriptor
->Type
= CmResourceTypePort
;
2207 ResourceDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
2210 ResourceDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
2212 ResourceDescriptor
->u
.Memory
.Start
= AccessRange
->RangeStart
;
2213 ResourceDescriptor
->u
.Memory
.Length
= AccessRange
->RangeLength
;
2215 ResourceDescriptor
++;
2218 /* If we use interrupt(s), copy them */
2221 ResourceDescriptor
->Type
= CmResourceTypeInterrupt
;
2223 if (PortConfig
->AdapterInterfaceType
== MicroChannel
||
2224 PortConfig
->InterruptMode
== LevelSensitive
)
2226 ResourceDescriptor
->ShareDisposition
= CmResourceShareShared
;
2227 ResourceDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE
;
2231 ResourceDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
2232 ResourceDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
2235 ResourceDescriptor
->u
.Interrupt
.Level
= PortConfig
->BusInterruptLevel
;
2236 ResourceDescriptor
->u
.Interrupt
.Vector
= PortConfig
->BusInterruptVector
;
2237 ResourceDescriptor
->u
.Interrupt
.Affinity
= 0;
2239 ResourceDescriptor
++;
2243 /* Copy 2nd interrupt
2244 FIXME: Stupid code duplication, remove */
2247 ResourceDescriptor
->Type
= CmResourceTypeInterrupt
;
2249 if (PortConfig
->AdapterInterfaceType
== MicroChannel
||
2250 PortConfig
->InterruptMode
== LevelSensitive
)
2252 ResourceDescriptor
->ShareDisposition
= CmResourceShareShared
;
2253 ResourceDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE
;
2257 ResourceDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
2258 ResourceDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
2261 ResourceDescriptor
->u
.Interrupt
.Level
= PortConfig
->BusInterruptLevel
;
2262 ResourceDescriptor
->u
.Interrupt
.Vector
= PortConfig
->BusInterruptVector
;
2263 ResourceDescriptor
->u
.Interrupt
.Affinity
= 0;
2265 ResourceDescriptor
++;
2271 ResourceDescriptor
->Type
= CmResourceTypeDma
;
2272 ResourceDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
2273 ResourceDescriptor
->u
.Dma
.Channel
= PortConfig
->DmaChannel
;
2274 ResourceDescriptor
->u
.Dma
.Port
= PortConfig
->DmaPort
;
2275 ResourceDescriptor
->Flags
= 0;
2277 if (PortConfig
->DmaChannel
== SP_UNINITIALIZED_VALUE
)
2278 ResourceDescriptor
->u
.Dma
.Channel
= 0;
2280 if (PortConfig
->DmaPort
== SP_UNINITIALIZED_VALUE
)
2281 ResourceDescriptor
->u
.Dma
.Port
= 0;
2284 return ResourceList
;
2289 SpiGetPciConfigData(IN PDRIVER_OBJECT DriverObject
,
2290 IN PDEVICE_OBJECT DeviceObject
,
2291 IN
struct _HW_INITIALIZATION_DATA
*HwInitializationData
,
2292 IN OUT PPORT_CONFIGURATION_INFORMATION PortConfig
,
2293 IN PUNICODE_STRING RegistryPath
,
2295 IN OUT PPCI_SLOT_NUMBER NextSlotNumber
)
2297 PCI_COMMON_CONFIG PciConfig
;
2298 PCI_SLOT_NUMBER SlotNumber
;
2301 ULONG FunctionNumber
;
2302 CHAR VendorIdString
[8];
2303 CHAR DeviceIdString
[8];
2304 UNICODE_STRING UnicodeStr
;
2305 PCM_RESOURCE_LIST ResourceList
;
2308 DPRINT ("SpiGetPciConfiguration() called\n");
2310 RtlZeroMemory(&ResourceList
, sizeof(PCM_RESOURCE_LIST
));
2311 SlotNumber
.u
.AsULONG
= 0;
2313 /* Loop through all devices */
2314 for (DeviceNumber
= NextSlotNumber
->u
.bits
.DeviceNumber
; DeviceNumber
< PCI_MAX_DEVICES
; DeviceNumber
++)
2316 SlotNumber
.u
.bits
.DeviceNumber
= DeviceNumber
;
2318 /* Loop through all functions */
2319 for (FunctionNumber
= NextSlotNumber
->u
.bits
.FunctionNumber
; FunctionNumber
< PCI_MAX_FUNCTION
; FunctionNumber
++)
2321 SlotNumber
.u
.bits
.FunctionNumber
= FunctionNumber
;
2323 /* Get PCI config bytes */
2324 DataSize
= HalGetBusData(PCIConfiguration
,
2326 SlotNumber
.u
.AsULONG
,
2330 /* If result of HalGetBusData is 0, then the bus is wrong */
2334 /* If result is PCI_INVALID_VENDORID, then this device has no more
2336 if (PciConfig
.VendorID
== PCI_INVALID_VENDORID
)
2339 sprintf (VendorIdString
, "%04hx", PciConfig
.VendorID
);
2340 sprintf (DeviceIdString
, "%04hx", PciConfig
.DeviceID
);
2342 if (_strnicmp(VendorIdString
, HwInitializationData
->VendorId
, HwInitializationData
->VendorIdLength
) ||
2343 _strnicmp(DeviceIdString
, HwInitializationData
->DeviceId
, HwInitializationData
->DeviceIdLength
))
2345 /* It is not our device */
2349 DPRINT("Found device 0x%04hx 0x%04hx at %1lu %2lu %1lu\n",
2353 SlotNumber
.u
.bits
.DeviceNumber
,
2354 SlotNumber
.u
.bits
.FunctionNumber
);
2357 RtlInitUnicodeString(&UnicodeStr
, L
"ScsiAdapter");
2358 Status
= HalAssignSlotResources(RegistryPath
,
2364 SlotNumber
.u
.AsULONG
,
2367 if (!NT_SUCCESS(Status
))
2370 /* Create configuration information */
2371 SpiResourceToConfig(HwInitializationData
,
2375 /* Free the resource list */
2376 ExFreePool(ResourceList
);
2378 /* Set dev & fn numbers */
2379 NextSlotNumber
->u
.bits
.DeviceNumber
= DeviceNumber
;
2380 NextSlotNumber
->u
.bits
.FunctionNumber
= FunctionNumber
+ 1;
2382 /* Save the slot number */
2383 PortConfig
->SlotNumber
= SlotNumber
.u
.AsULONG
;
2387 NextSlotNumber
->u
.bits
.FunctionNumber
= 0;
2390 NextSlotNumber
->u
.bits
.DeviceNumber
= 0;
2391 DPRINT ("No device found\n");
2398 /**********************************************************************
2400 * ScsiPortCreateClose
2403 * Answer requests for Create/Close calls: a null operation.
2410 * Pointer to a device object.
2413 * Pointer to an IRP.
2419 static NTSTATUS NTAPI
2420 ScsiPortCreateClose(IN PDEVICE_OBJECT DeviceObject
,
2423 DPRINT("ScsiPortCreateClose()\n");
2425 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
2426 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2428 return STATUS_SUCCESS
;
2432 SpiHandleAttachRelease(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
2435 PSCSI_LUN_INFO LunInfo
;
2436 PIO_STACK_LOCATION IrpStack
;
2437 PDEVICE_OBJECT DeviceObject
;
2438 PSCSI_REQUEST_BLOCK Srb
;
2441 /* Get pointer to the SRB */
2442 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
2443 Srb
= (PSCSI_REQUEST_BLOCK
)IrpStack
->Parameters
.Others
.Argument1
;
2445 /* Check if PathId matches number of buses */
2446 if (DeviceExtension
->BusesConfig
== NULL
||
2447 DeviceExtension
->BusesConfig
->NumberOfBuses
<= Srb
->PathId
)
2449 Srb
->SrbStatus
= SRB_STATUS_NO_DEVICE
;
2450 return STATUS_DEVICE_DOES_NOT_EXIST
;
2453 /* Get pointer to LunInfo */
2454 LunInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Srb
->PathId
]->LunInfo
;
2456 /* Find matching LunInfo */
2459 if (LunInfo
->PathId
== Srb
->PathId
&&
2460 LunInfo
->TargetId
== Srb
->TargetId
&&
2461 LunInfo
->Lun
== Srb
->Lun
)
2466 LunInfo
= LunInfo
->Next
;
2469 /* If we couldn't find it - exit */
2470 if (LunInfo
== NULL
)
2471 return STATUS_DEVICE_DOES_NOT_EXIST
;
2475 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
2477 /* Release, if asked */
2478 if (Srb
->Function
== SRB_FUNCTION_RELEASE_DEVICE
)
2480 LunInfo
->DeviceClaimed
= FALSE
;
2481 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2482 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2484 return STATUS_SUCCESS
;
2487 /* Attach, if not already claimed */
2488 if (LunInfo
->DeviceClaimed
)
2490 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2491 Srb
->SrbStatus
= SRB_STATUS_BUSY
;
2493 return STATUS_DEVICE_BUSY
;
2496 /* Save the device object */
2497 DeviceObject
= LunInfo
->DeviceObject
;
2499 if (Srb
->Function
== SRB_FUNCTION_CLAIM_DEVICE
)
2500 LunInfo
->DeviceClaimed
= TRUE
;
2502 if (Srb
->Function
== SRB_FUNCTION_ATTACH_DEVICE
)
2503 LunInfo
->DeviceObject
= Srb
->DataBuffer
;
2505 Srb
->DataBuffer
= DeviceObject
;
2507 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2508 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2510 return STATUS_SUCCESS
;
2514 /**********************************************************************
2516 * ScsiPortDispatchScsi
2519 * Answer requests for SCSI calls
2525 * Standard dispatch arguments
2531 static NTSTATUS NTAPI
2532 ScsiPortDispatchScsi(IN PDEVICE_OBJECT DeviceObject
,
2535 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
2536 PSCSI_PORT_LUN_EXTENSION LunExtension
;
2537 PIO_STACK_LOCATION Stack
;
2538 PSCSI_REQUEST_BLOCK Srb
;
2540 NTSTATUS Status
= STATUS_SUCCESS
;
2541 PIRP NextIrp
, IrpList
;
2542 PKDEVICE_QUEUE_ENTRY Entry
;
2544 DPRINT("ScsiPortDispatchScsi(DeviceObject %p Irp %p)\n",
2547 DeviceExtension
= DeviceObject
->DeviceExtension
;
2548 Stack
= IoGetCurrentIrpStackLocation(Irp
);
2550 Srb
= Stack
->Parameters
.Scsi
.Srb
;
2553 DPRINT1("ScsiPortDispatchScsi() called with Srb = NULL!\n");
2554 Status
= STATUS_UNSUCCESSFUL
;
2556 Irp
->IoStatus
.Status
= Status
;
2557 Irp
->IoStatus
.Information
= 0;
2559 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2564 DPRINT("Srb: %p\n", Srb
);
2565 DPRINT("Srb->Function: %lu\n", Srb
->Function
);
2566 DPRINT("PathId: %lu TargetId: %lu Lun: %lu\n", Srb
->PathId
, Srb
->TargetId
, Srb
->Lun
);
2568 LunExtension
= SpiGetLunExtension(DeviceExtension
,
2572 if (LunExtension
== NULL
)
2574 DPRINT("ScsiPortDispatchScsi() called with an invalid LUN\n");
2575 Status
= STATUS_NO_SUCH_DEVICE
;
2577 Srb
->SrbStatus
= SRB_STATUS_NO_DEVICE
;
2578 Irp
->IoStatus
.Status
= Status
;
2579 Irp
->IoStatus
.Information
= 0;
2581 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2586 switch (Srb
->Function
)
2588 case SRB_FUNCTION_SHUTDOWN
:
2589 case SRB_FUNCTION_FLUSH
:
2590 DPRINT (" SRB_FUNCTION_SHUTDOWN or FLUSH\n");
2591 if (DeviceExtension
->CachesData
== FALSE
)
2593 /* All success here */
2594 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2595 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
2596 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2597 return STATUS_SUCCESS
;
2599 /* Fall through to a usual execute operation */
2601 case SRB_FUNCTION_EXECUTE_SCSI
:
2602 case SRB_FUNCTION_IO_CONTROL
:
2603 /* Mark IRP as pending in all cases */
2604 IoMarkIrpPending(Irp
);
2606 if (Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
)
2608 /* Start IO directly */
2609 IoStartPacket(DeviceObject
, Irp
, NULL
, NULL
);
2615 /* We need to be at DISPATCH_LEVEL */
2616 KeRaiseIrql (DISPATCH_LEVEL
, &oldIrql
);
2618 /* Insert IRP into the queue */
2619 if (!KeInsertByKeyDeviceQueue(&LunExtension
->DeviceQueue
,
2620 &Irp
->Tail
.Overlay
.DeviceQueueEntry
,
2623 /* It means the queue is empty, and we just start this request */
2624 IoStartPacket(DeviceObject
, Irp
, NULL
, NULL
);
2627 /* Back to the old IRQL */
2628 KeLowerIrql (oldIrql
);
2630 return STATUS_PENDING
;
2632 case SRB_FUNCTION_CLAIM_DEVICE
:
2633 case SRB_FUNCTION_ATTACH_DEVICE
:
2634 DPRINT (" SRB_FUNCTION_CLAIM_DEVICE or ATTACH\n");
2636 /* Reference device object and keep the device object */
2637 Status
= SpiHandleAttachRelease(DeviceExtension
, Irp
);
2640 case SRB_FUNCTION_RELEASE_DEVICE
:
2641 DPRINT (" SRB_FUNCTION_RELEASE_DEVICE\n");
2643 /* Dereference device object and clear the device object */
2644 Status
= SpiHandleAttachRelease(DeviceExtension
, Irp
);
2647 case SRB_FUNCTION_RELEASE_QUEUE
:
2648 DPRINT(" SRB_FUNCTION_RELEASE_QUEUE\n");
2650 /* Guard with the spinlock */
2651 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
2653 if (!(LunExtension
->Flags
& LUNEX_FROZEN_QUEUE
))
2655 DPRINT("Queue is not frozen really\n");
2657 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2658 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2659 Status
= STATUS_SUCCESS
;
2664 /* Unfreeze the queue */
2665 LunExtension
->Flags
&= ~LUNEX_FROZEN_QUEUE
;
2667 if (LunExtension
->SrbInfo
.Srb
== NULL
)
2669 /* Get next logical unit request */
2670 SpiGetNextRequestFromLun(DeviceExtension
, LunExtension
);
2672 /* SpiGetNextRequestFromLun() releases the spinlock */
2677 DPRINT("The queue has active request\n");
2678 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2682 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2683 Status
= STATUS_SUCCESS
;
2686 case SRB_FUNCTION_FLUSH_QUEUE
:
2687 DPRINT(" SRB_FUNCTION_FLUSH_QUEUE\n");
2689 /* Guard with the spinlock */
2690 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
2692 if (!(LunExtension
->Flags
& LUNEX_FROZEN_QUEUE
))
2694 DPRINT("Queue is not frozen really\n");
2696 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2697 Status
= STATUS_INVALID_DEVICE_REQUEST
;
2701 /* Make sure there is no active request */
2702 ASSERT(LunExtension
->SrbInfo
.Srb
== NULL
);
2704 /* Compile a list from the device queue */
2706 while ((Entry
= KeRemoveDeviceQueue(&LunExtension
->DeviceQueue
)) != NULL
)
2708 NextIrp
= CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.DeviceQueueEntry
);
2711 Stack
= IoGetCurrentIrpStackLocation(NextIrp
);
2712 Srb
= Stack
->Parameters
.Scsi
.Srb
;
2715 Srb
->SrbStatus
= SRB_STATUS_REQUEST_FLUSHED
;
2716 NextIrp
->IoStatus
.Status
= STATUS_UNSUCCESSFUL
;
2718 /* Add then to the list */
2719 NextIrp
->Tail
.Overlay
.ListEntry
.Flink
= (PLIST_ENTRY
)IrpList
;
2723 /* Unfreeze the queue */
2724 LunExtension
->Flags
&= ~LUNEX_FROZEN_QUEUE
;
2726 /* Release the spinlock */
2727 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2729 /* Complete those requests */
2733 IrpList
= (PIRP
)NextIrp
->Tail
.Overlay
.ListEntry
.Flink
;
2735 IoCompleteRequest(NextIrp
, 0);
2738 Status
= STATUS_SUCCESS
;
2742 DPRINT1("SRB function not implemented (Function %lu)\n", Srb
->Function
);
2743 Status
= STATUS_NOT_IMPLEMENTED
;
2747 Irp
->IoStatus
.Status
= Status
;
2749 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2755 /**********************************************************************
2757 * ScsiPortDeviceControl
2760 * Answer requests for device control calls
2766 * Standard dispatch arguments
2772 static NTSTATUS NTAPI
2773 ScsiPortDeviceControl(IN PDEVICE_OBJECT DeviceObject
,
2776 PIO_STACK_LOCATION Stack
;
2777 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
2778 NTSTATUS Status
= STATUS_SUCCESS
;
2780 DPRINT("ScsiPortDeviceControl()\n");
2782 Irp
->IoStatus
.Information
= 0;
2784 Stack
= IoGetCurrentIrpStackLocation(Irp
);
2785 DeviceExtension
= DeviceObject
->DeviceExtension
;
2787 switch (Stack
->Parameters
.DeviceIoControl
.IoControlCode
)
2789 case IOCTL_SCSI_GET_DUMP_POINTERS
:
2791 PDUMP_POINTERS DumpPointers
;
2792 DPRINT(" IOCTL_SCSI_GET_DUMP_POINTERS\n");
2793 DumpPointers
= (PDUMP_POINTERS
)Irp
->AssociatedIrp
.SystemBuffer
;
2794 DumpPointers
->DeviceObject
= DeviceObject
;
2796 Irp
->IoStatus
.Information
= sizeof(DUMP_POINTERS
);
2800 case IOCTL_SCSI_GET_CAPABILITIES
:
2801 DPRINT(" IOCTL_SCSI_GET_CAPABILITIES\n");
2802 if (Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
== sizeof(PVOID
))
2804 *((PVOID
*)Irp
->AssociatedIrp
.SystemBuffer
) = &DeviceExtension
->PortCapabilities
;
2806 Irp
->IoStatus
.Information
= sizeof(PVOID
);
2807 Status
= STATUS_SUCCESS
;
2811 if (Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(IO_SCSI_CAPABILITIES
))
2813 Status
= STATUS_BUFFER_TOO_SMALL
;
2817 RtlCopyMemory(Irp
->AssociatedIrp
.SystemBuffer
,
2818 &DeviceExtension
->PortCapabilities
,
2819 sizeof(IO_SCSI_CAPABILITIES
));
2821 Irp
->IoStatus
.Information
= sizeof(IO_SCSI_CAPABILITIES
);
2822 Status
= STATUS_SUCCESS
;
2825 case IOCTL_SCSI_GET_INQUIRY_DATA
:
2826 DPRINT(" IOCTL_SCSI_GET_INQUIRY_DATA\n");
2828 /* Copy inquiry data to the port device extension */
2829 Status
= SpiGetInquiryData(DeviceExtension
, Irp
);
2832 case IOCTL_SCSI_MINIPORT
:
2833 DPRINT1("IOCTL_SCSI_MINIPORT unimplemented!\n");
2836 case IOCTL_SCSI_PASS_THROUGH
:
2837 DPRINT1("IOCTL_SCSI_PASS_THROUGH unimplemented!\n");
2841 DPRINT1(" unknown ioctl code: 0x%lX\n",
2842 Stack
->Parameters
.DeviceIoControl
.IoControlCode
);
2846 /* Complete the request with the given status */
2847 Irp
->IoStatus
.Status
= Status
;
2848 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2855 ScsiPortStartIo(IN PDEVICE_OBJECT DeviceObject
,
2858 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
2859 PSCSI_PORT_LUN_EXTENSION LunExtension
;
2860 PIO_STACK_LOCATION IrpStack
;
2861 PSCSI_REQUEST_BLOCK Srb
;
2862 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
2866 DPRINT("ScsiPortStartIo() called!\n");
2868 DeviceExtension
= DeviceObject
->DeviceExtension
;
2869 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
2871 DPRINT("DeviceExtension %p\n", DeviceExtension
);
2873 Srb
= IrpStack
->Parameters
.Scsi
.Srb
;
2875 /* Apply "default" flags */
2876 Srb
->SrbFlags
|= DeviceExtension
->SrbFlags
;
2878 /* Get LUN extension */
2879 LunExtension
= SpiGetLunExtension(DeviceExtension
,
2884 if (DeviceExtension
->NeedSrbDataAlloc
||
2885 DeviceExtension
->NeedSrbExtensionAlloc
)
2888 SrbInfo
= SpiAllocateSrbStructures(DeviceExtension
,
2892 /* Couldn't alloc one or both data structures, return */
2893 if (SrbInfo
== NULL
)
2895 /* We have to call IoStartNextPacket, because this request
2897 if (LunExtension
->Flags
& LUNEX_REQUEST_PENDING
)
2898 IoStartNextPacket(DeviceObject
, FALSE
);
2905 /* No allocations are needed */
2906 SrbInfo
= &LunExtension
->SrbInfo
;
2907 Srb
->SrbExtension
= NULL
;
2908 Srb
->QueueTag
= SP_UNTAGGED
;
2911 /* Increase sequence number of SRB */
2912 if (!SrbInfo
->SequenceNumber
)
2914 /* Increase global sequence number */
2915 DeviceExtension
->SequenceNumber
++;
2918 SrbInfo
->SequenceNumber
= DeviceExtension
->SequenceNumber
;
2921 /* Check some special SRBs */
2922 if (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
)
2924 /* Some special handling */
2925 DPRINT1("Abort command! Unimplemented now\n");
2932 if (Srb
->SrbFlags
& SRB_FLAGS_UNSPECIFIED_DIRECTION
)
2934 // Store the MDL virtual address in SrbInfo structure
2935 SrbInfo
->DataOffset
= MmGetMdlVirtualAddress(Irp
->MdlAddress
);
2937 if (DeviceExtension
->MapBuffers
)
2939 /* Calculate offset within DataBuffer */
2940 SrbInfo
->DataOffset
= MmGetSystemAddressForMdl(Irp
->MdlAddress
);
2941 Srb
->DataBuffer
= SrbInfo
->DataOffset
+
2942 (ULONG
)((PUCHAR
)Srb
->DataBuffer
-
2943 (PUCHAR
)MmGetMdlVirtualAddress(Irp
->MdlAddress
));
2946 if (DeviceExtension
->AdapterObject
)
2949 KeFlushIoBuffers(Irp
->MdlAddress
,
2950 Srb
->SrbFlags
& SRB_FLAGS_DATA_IN
? TRUE
: FALSE
,
2954 if (DeviceExtension
->MapRegisters
)
2956 /* Calculate number of needed map registers */
2957 SrbInfo
->NumberOfMapRegisters
= ADDRESS_AND_SIZE_TO_SPAN_PAGES(
2959 Srb
->DataTransferLength
);
2961 /* Allocate adapter channel */
2962 Status
= IoAllocateAdapterChannel(DeviceExtension
->AdapterObject
,
2963 DeviceExtension
->DeviceObject
,
2964 SrbInfo
->NumberOfMapRegisters
,
2968 if (!NT_SUCCESS(Status
))
2970 DPRINT1("IoAllocateAdapterChannel() failed!\n");
2972 Srb
->SrbStatus
= SRB_STATUS_INVALID_REQUEST
;
2973 ScsiPortNotification(RequestComplete
,
2974 DeviceExtension
+ 1,
2977 ScsiPortNotification(NextRequest
,
2978 DeviceExtension
+ 1);
2980 /* Request DPC for that work */
2981 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
2984 /* Control goes to SpiAdapterControl */
2989 /* Increase active request counter */
2990 CounterResult
= InterlockedIncrement(&DeviceExtension
->ActiveRequestCounter
);
2992 if (CounterResult
== 0 &&
2993 DeviceExtension
->AdapterObject
!= NULL
&&
2994 !DeviceExtension
->MapRegisters
)
2996 IoAllocateAdapterChannel(
2997 DeviceExtension
->AdapterObject
,
2999 DeviceExtension
->PortCapabilities
.MaximumPhysicalPages
,
3000 ScsiPortAllocateAdapterChannel
,
3007 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
3009 if (!KeSynchronizeExecution(DeviceExtension
->Interrupt
,
3010 ScsiPortStartPacket
,
3013 DPRINT("Synchronization failed!\n");
3015 Irp
->IoStatus
.Status
= STATUS_UNSUCCESSFUL
;
3016 Irp
->IoStatus
.Information
= 0;
3017 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3019 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
3023 /* Release the spinlock only */
3024 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3028 DPRINT("ScsiPortStartIo() done\n");
3032 static BOOLEAN NTAPI
3033 ScsiPortStartPacket(IN OUT PVOID Context
)
3035 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
3036 PIO_STACK_LOCATION IrpStack
;
3037 PSCSI_REQUEST_BLOCK Srb
;
3038 PDEVICE_OBJECT DeviceObject
= (PDEVICE_OBJECT
)Context
;
3039 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3040 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
3044 DPRINT("ScsiPortStartPacket() called\n");
3046 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
3048 IrpStack
= IoGetCurrentIrpStackLocation(DeviceObject
->CurrentIrp
);
3049 Srb
= IrpStack
->Parameters
.Scsi
.Srb
;
3051 /* Get LUN extension */
3052 LunExtension
= SpiGetLunExtension(DeviceExtension
,
3057 /* Check if we are in a reset state */
3058 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_RESET
)
3060 /* Mark the we've got requests while being in the reset state */
3061 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_RESET_REQUEST
;
3065 /* Set the time out value */
3066 DeviceExtension
->TimerCount
= Srb
->TimeOutValue
;
3069 DeviceExtension
->Flags
|= SCSI_PORT_DEVICE_BUSY
;
3071 if (LunExtension
->RequestTimeout
!= -1)
3073 /* Timer already active */
3078 /* It hasn't been initialized yet */
3079 LunExtension
->RequestTimeout
= Srb
->TimeOutValue
;
3083 if (Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
)
3085 /* Handle bypass-requests */
3087 /* Is this an abort request? */
3088 if (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
)
3090 /* Get pointer to SRB info structure */
3091 SrbInfo
= SpiGetSrbData(DeviceExtension
,
3097 /* Check if the request is still "active" */
3098 if (SrbInfo
== NULL
||
3099 SrbInfo
->Srb
== NULL
||
3100 !(SrbInfo
->Srb
->SrbFlags
& SRB_FLAGS_IS_ACTIVE
))
3102 /* It's not, mark it as active then */
3103 Srb
->SrbFlags
|= SRB_FLAGS_IS_ACTIVE
;
3106 LunExtension
->RequestTimeout
= -1;
3108 DPRINT("Request has been already completed, but abort request came\n");
3109 Srb
->SrbStatus
= SRB_STATUS_ABORT_FAILED
;
3111 /* Notify about request complete */
3112 ScsiPortNotification(RequestComplete
,
3113 DeviceExtension
->MiniPortDeviceExtension
,
3116 /* and about readiness for the next request */
3117 ScsiPortNotification(NextRequest
,
3118 DeviceExtension
->MiniPortDeviceExtension
);
3120 /* They might ask for some work, so queue the DPC for them */
3121 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
3123 /* We're done in this branch */
3129 /* Add number of queued requests */
3130 LunExtension
->QueueCount
++;
3133 /* Bypass requests don't need request sense */
3134 LunExtension
->Flags
&= ~LUNEX_NEED_REQUEST_SENSE
;
3136 /* Is disconnect disabled for this request? */
3137 if (Srb
->SrbFlags
& SRB_FLAGS_DISABLE_DISCONNECT
)
3139 /* Set the corresponding flag */
3140 DeviceExtension
->Flags
&= ~SCSI_PORT_DISCONNECT_ALLOWED
;
3143 /* Transfer timeout value from Srb to Lun */
3144 LunExtension
->RequestTimeout
= Srb
->TimeOutValue
;
3148 if (Srb
->SrbFlags
& SRB_FLAGS_DISABLE_DISCONNECT
)
3150 /* It's a disconnect, so no more requests can go */
3151 DeviceExtension
->Flags
&= ~SCSI_PORT_DISCONNECT_ALLOWED
;
3154 LunExtension
->Flags
|= SCSI_PORT_LU_ACTIVE
;
3156 /* Increment queue count */
3157 LunExtension
->QueueCount
++;
3159 /* If it's tagged - special thing */
3160 if (Srb
->QueueTag
!= SP_UNTAGGED
)
3162 SrbInfo
= &DeviceExtension
->SrbInfo
[Srb
->QueueTag
- 1];
3164 /* Chek for consistency */
3165 ASSERT(SrbInfo
->Requests
.Blink
== NULL
);
3167 /* Insert it into the list of requests */
3168 InsertTailList(&LunExtension
->SrbInfo
.Requests
, &SrbInfo
->Requests
);
3172 /* Mark this Srb active */
3173 Srb
->SrbFlags
|= SRB_FLAGS_IS_ACTIVE
;
3175 /* Call HwStartIo routine */
3176 Result
= DeviceExtension
->HwStartIo(&DeviceExtension
->MiniPortDeviceExtension
,
3179 /* If notification is needed, then request a DPC */
3180 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
3181 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
3186 IO_ALLOCATION_ACTION
3188 SpiAdapterControl(PDEVICE_OBJECT DeviceObject
,
3190 PVOID MapRegisterBase
,
3193 PSCSI_REQUEST_BLOCK Srb
;
3194 PSCSI_SG_ADDRESS ScatterGatherList
;
3196 PIO_STACK_LOCATION IrpStack
;
3197 ULONG TotalLength
= 0;
3198 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
3199 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
3201 BOOLEAN WriteToDevice
;
3203 /* Get pointers to SrbInfo and DeviceExtension */
3204 SrbInfo
= (PSCSI_REQUEST_BLOCK_INFO
)Context
;
3205 DeviceExtension
= DeviceObject
->DeviceExtension
;
3207 /* Get pointer to SRB */
3208 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
3209 Srb
= (PSCSI_REQUEST_BLOCK
)IrpStack
->Parameters
.Others
.Argument1
;
3211 /* Depending on the map registers number, we allocate
3212 either from NonPagedPool, or from our static list */
3213 if (SrbInfo
->NumberOfMapRegisters
> MAX_SG_LIST
)
3215 SrbInfo
->ScatterGather
= ExAllocatePoolWithTag(
3216 NonPagedPool
, SrbInfo
->NumberOfMapRegisters
* sizeof(SCSI_SG_ADDRESS
), TAG_SCSIPORT
);
3218 if (SrbInfo
->ScatterGather
== NULL
)
3221 Srb
->SrbFlags
|= SRB_FLAGS_SGLIST_FROM_POOL
;
3225 SrbInfo
->ScatterGather
= SrbInfo
->ScatterGatherList
;
3228 /* Use chosen SG list source */
3229 ScatterGatherList
= SrbInfo
->ScatterGather
;
3231 /* Save map registers base */
3232 SrbInfo
->BaseOfMapRegister
= MapRegisterBase
;
3234 /* Determine WriteToDevice flag */
3235 WriteToDevice
= Srb
->SrbFlags
& SRB_FLAGS_DATA_OUT
? TRUE
: FALSE
;
3237 /* Get virtual address of the data buffer */
3238 DataVA
= (PUCHAR
)MmGetMdlVirtualAddress(Irp
->MdlAddress
) +
3239 ((PCHAR
)Srb
->DataBuffer
- SrbInfo
->DataOffset
);
3241 /* Build the actual SG list */
3242 while (TotalLength
< Srb
->DataTransferLength
)
3244 if (!ScatterGatherList
)
3247 ScatterGatherList
->Length
= Srb
->DataTransferLength
- TotalLength
;
3248 ScatterGatherList
->PhysicalAddress
= IoMapTransfer(NULL
,
3251 DataVA
+ TotalLength
,
3252 &ScatterGatherList
->Length
,
3255 TotalLength
+= ScatterGatherList
->Length
;
3256 ScatterGatherList
++;
3259 /* Schedule an active request */
3260 InterlockedIncrement(&DeviceExtension
->ActiveRequestCounter
);
3261 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &CurrentIrql
);
3262 KeSynchronizeExecution(DeviceExtension
->Interrupt
,
3263 ScsiPortStartPacket
,
3265 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, CurrentIrql
);
3267 return DeallocateObjectKeepRegisters
;
3270 static PSCSI_PORT_LUN_EXTENSION
3271 SpiAllocateLunExtension (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
)
3273 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3274 ULONG LunExtensionSize
;
3276 DPRINT("SpiAllocateLunExtension (%p)\n",
3279 /* Round LunExtensionSize first to the sizeof LONGLONG */
3280 LunExtensionSize
= (DeviceExtension
->LunExtensionSize
+
3281 sizeof(LONGLONG
) - 1) & ~(sizeof(LONGLONG
) - 1);
3283 LunExtensionSize
+= sizeof(SCSI_PORT_LUN_EXTENSION
);
3284 DPRINT("LunExtensionSize %lu\n", LunExtensionSize
);
3286 LunExtension
= ExAllocatePoolWithTag(NonPagedPool
, LunExtensionSize
, TAG_SCSIPORT
);
3287 if (LunExtension
== NULL
)
3289 DPRINT1("Out of resources!\n");
3293 /* Zero everything */
3294 RtlZeroMemory(LunExtension
, LunExtensionSize
);
3296 /* Initialize a list of requests */
3297 InitializeListHead(&LunExtension
->SrbInfo
.Requests
);
3299 /* Initialize timeout counter */
3300 LunExtension
->RequestTimeout
= -1;
3302 /* Set maximum queue size */
3303 LunExtension
->MaxQueueCount
= 256;
3305 /* Initialize request queue */
3306 KeInitializeDeviceQueue (&LunExtension
->DeviceQueue
);
3308 return LunExtension
;
3311 static PSCSI_PORT_LUN_EXTENSION
3312 SpiGetLunExtension (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
3317 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3319 DPRINT("SpiGetLunExtension(%p %u %u %u) called\n",
3320 DeviceExtension
, PathId
, TargetId
, Lun
);
3322 /* Get appropriate list */
3323 LunExtension
= DeviceExtension
->LunExtensionList
[(TargetId
+ Lun
) % LUS_NUMBER
];
3325 /* Iterate it until we find what we need */
3326 while (LunExtension
)
3328 if (LunExtension
->TargetId
== TargetId
&&
3329 LunExtension
->Lun
== Lun
&&
3330 LunExtension
->PathId
== PathId
)
3332 /* All matches, return */
3333 return LunExtension
;
3336 /* Advance to the next item */
3337 LunExtension
= LunExtension
->Next
;
3340 /* We did not find anything */
3341 DPRINT("Nothing found\n");
3345 static PSCSI_REQUEST_BLOCK_INFO
3346 SpiAllocateSrbStructures(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
3347 PSCSI_PORT_LUN_EXTENSION LunExtension
,
3348 PSCSI_REQUEST_BLOCK Srb
)
3351 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
3353 /* Spinlock must be held while this function executes */
3354 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
3356 /* Allocate SRB data structure */
3357 if (DeviceExtension
->NeedSrbDataAlloc
)
3359 /* Treat the abort request in a special way */
3360 if (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
)
3362 SrbInfo
= SpiGetSrbData(DeviceExtension
,
3368 else if (Srb
->SrbFlags
&
3369 (SRB_FLAGS_QUEUE_ACTION_ENABLE
| SRB_FLAGS_NO_QUEUE_FREEZE
) &&
3370 !(Srb
->SrbFlags
& SRB_FLAGS_DISABLE_DISCONNECT
)
3373 /* Do not process tagged commands if need request sense is set */
3374 if (LunExtension
->Flags
& LUNEX_NEED_REQUEST_SENSE
)
3376 ASSERT(!(LunExtension
->Flags
& LUNEX_REQUEST_PENDING
));
3378 LunExtension
->PendingRequest
= Srb
->OriginalRequest
;
3379 LunExtension
->Flags
|= LUNEX_REQUEST_PENDING
| SCSI_PORT_LU_ACTIVE
;
3381 /* Relese the spinlock and return */
3382 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3386 ASSERT(LunExtension
->SrbInfo
.Srb
== NULL
);
3387 SrbInfo
= DeviceExtension
->FreeSrbInfo
;
3389 if (SrbInfo
== NULL
)
3391 /* No SRB structures left in the list. We have to leave
3392 and wait while we are called again */
3394 DeviceExtension
->Flags
|= SCSI_PORT_REQUEST_PENDING
;
3395 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3399 DeviceExtension
->FreeSrbInfo
= (PSCSI_REQUEST_BLOCK_INFO
)SrbInfo
->Requests
.Flink
;
3401 /* QueueTag must never be 0, so +1 to it */
3402 Srb
->QueueTag
= (UCHAR
)(SrbInfo
- DeviceExtension
->SrbInfo
) + 1;
3406 /* Usual untagged command */
3408 (!IsListEmpty(&LunExtension
->SrbInfo
.Requests
) ||
3409 LunExtension
->Flags
& LUNEX_NEED_REQUEST_SENSE
) &&
3410 !(Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
)
3413 /* Mark it as pending and leave */
3414 ASSERT(!(LunExtension
->Flags
& LUNEX_REQUEST_PENDING
));
3415 LunExtension
->Flags
|= LUNEX_REQUEST_PENDING
| SCSI_PORT_LU_ACTIVE
;
3416 LunExtension
->PendingRequest
= Srb
->OriginalRequest
;
3418 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3422 Srb
->QueueTag
= SP_UNTAGGED
;
3423 SrbInfo
= &LunExtension
->SrbInfo
;
3428 Srb
->QueueTag
= SP_UNTAGGED
;
3429 SrbInfo
= &LunExtension
->SrbInfo
;
3432 /* Allocate SRB extension structure */
3433 if (DeviceExtension
->NeedSrbExtensionAlloc
)
3435 /* Check the list of free extensions */
3436 SrbExtension
= DeviceExtension
->FreeSrbExtensions
;
3438 /* If no free extensions... */
3439 if (SrbExtension
== NULL
)
3442 if (Srb
->Function
!= SRB_FUNCTION_ABORT_COMMAND
&&
3443 Srb
->QueueTag
!= SP_UNTAGGED
)
3445 SrbInfo
->Requests
.Blink
= NULL
;
3446 SrbInfo
->Requests
.Flink
= (PLIST_ENTRY
)DeviceExtension
->FreeSrbInfo
;
3447 DeviceExtension
->FreeSrbInfo
= SrbInfo
;
3450 /* Return, in order to be called again later */
3451 DeviceExtension
->Flags
|= SCSI_PORT_REQUEST_PENDING
;
3452 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3456 /* Remove that free SRB extension from the list (since
3457 we're going to use it) */
3458 DeviceExtension
->FreeSrbExtensions
= *((PVOID
*)SrbExtension
);
3460 /* Spinlock can be released now */
3461 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3463 Srb
->SrbExtension
= SrbExtension
;
3465 if (Srb
->SenseInfoBuffer
!= NULL
&&
3466 DeviceExtension
->SupportsAutoSense
)
3468 /* Store pointer to the SenseInfo buffer */
3469 SrbInfo
->SaveSenseRequest
= Srb
->SenseInfoBuffer
;
3471 /* Does data fit the buffer? */
3472 if (Srb
->SenseInfoBufferLength
> sizeof(SENSE_DATA
))
3474 /* No, disabling autosense at all */
3475 Srb
->SrbFlags
|= SRB_FLAGS_DISABLE_AUTOSENSE
;
3479 /* Yes, update the buffer pointer */
3480 Srb
->SenseInfoBuffer
= SrbExtension
+ DeviceExtension
->SrbExtensionSize
;
3487 Srb
->SrbExtension
= NULL
;
3488 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3496 SpiSendInquiry (IN PDEVICE_OBJECT DeviceObject
,
3497 IN PSCSI_LUN_INFO LunInfo
)
3499 IO_STATUS_BLOCK IoStatusBlock
;
3500 PIO_STACK_LOCATION IrpStack
;
3505 PINQUIRYDATA InquiryBuffer
;
3506 PSENSE_DATA SenseBuffer
;
3507 BOOLEAN KeepTrying
= TRUE
;
3508 ULONG RetryCount
= 0;
3509 SCSI_REQUEST_BLOCK Srb
;
3511 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3512 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
3514 DPRINT ("SpiSendInquiry() called\n");
3516 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
3518 InquiryBuffer
= ExAllocatePoolWithTag (NonPagedPool
, INQUIRYDATABUFFERSIZE
, TAG_SCSIPORT
);
3519 if (InquiryBuffer
== NULL
)
3520 return STATUS_INSUFFICIENT_RESOURCES
;
3522 SenseBuffer
= ExAllocatePoolWithTag (NonPagedPool
, SENSE_BUFFER_SIZE
, TAG_SCSIPORT
);
3523 if (SenseBuffer
== NULL
)
3525 ExFreePool(InquiryBuffer
);
3526 return STATUS_INSUFFICIENT_RESOURCES
;
3531 /* Initialize event for waiting */
3532 KeInitializeEvent(&Event
,
3537 Irp
= IoBuildDeviceIoControlRequest(IOCTL_SCSI_EXECUTE_IN
,
3542 INQUIRYDATABUFFERSIZE
,
3548 DPRINT("IoBuildDeviceIoControlRequest() failed\n");
3549 return STATUS_INSUFFICIENT_RESOURCES
;
3553 RtlZeroMemory(&Srb
, sizeof(SCSI_REQUEST_BLOCK
));
3555 Srb
.Length
= sizeof(SCSI_REQUEST_BLOCK
);
3556 Srb
.OriginalRequest
= Irp
;
3557 Srb
.PathId
= LunInfo
->PathId
;
3558 Srb
.TargetId
= LunInfo
->TargetId
;
3559 Srb
.Lun
= LunInfo
->Lun
;
3560 Srb
.Function
= SRB_FUNCTION_EXECUTE_SCSI
;
3561 Srb
.SrbFlags
= SRB_FLAGS_DATA_IN
| SRB_FLAGS_DISABLE_SYNCH_TRANSFER
;
3562 Srb
.TimeOutValue
= 4;
3565 Srb
.SenseInfoBuffer
= SenseBuffer
;
3566 Srb
.SenseInfoBufferLength
= SENSE_BUFFER_SIZE
;
3568 Srb
.DataBuffer
= InquiryBuffer
;
3569 Srb
.DataTransferLength
= INQUIRYDATABUFFERSIZE
;
3571 /* Attach Srb to the Irp */
3572 IrpStack
= IoGetNextIrpStackLocation (Irp
);
3573 IrpStack
->Parameters
.Scsi
.Srb
= &Srb
;
3576 Cdb
= (PCDB
)Srb
.Cdb
;
3577 Cdb
->CDB6INQUIRY
.OperationCode
= SCSIOP_INQUIRY
;
3578 Cdb
->CDB6INQUIRY
.LogicalUnitNumber
= LunInfo
->Lun
;
3579 Cdb
->CDB6INQUIRY
.AllocationLength
= INQUIRYDATABUFFERSIZE
;
3581 /* Call the driver */
3582 Status
= IoCallDriver(DeviceObject
, Irp
);
3584 /* Wait for it to complete */
3585 if (Status
== STATUS_PENDING
)
3587 DPRINT("SpiSendInquiry(): Waiting for the driver to process request...\n");
3588 KeWaitForSingleObject(&Event
,
3593 Status
= IoStatusBlock
.Status
;
3596 DPRINT("SpiSendInquiry(): Request processed by driver, status = 0x%08X\n", Status
);
3598 if (SRB_STATUS(Srb
.SrbStatus
) == SRB_STATUS_SUCCESS
)
3600 /* All fine, copy data over */
3601 RtlCopyMemory(LunInfo
->InquiryData
,
3603 INQUIRYDATABUFFERSIZE
);
3605 Status
= STATUS_SUCCESS
;
3610 DPRINT("Inquiry SRB failed with SrbStatus 0x%08X\n", Srb
.SrbStatus
);
3611 /* Check if the queue is frozen */
3612 if (Srb
.SrbStatus
& SRB_STATUS_QUEUE_FROZEN
)
3614 /* Something weird happened, deal with it (unfreeze the queue) */
3617 DPRINT("SpiSendInquiry(): the queue is frozen at TargetId %d\n", Srb
.TargetId
);
3619 LunExtension
= SpiGetLunExtension(DeviceExtension
,
3624 /* Clear frozen flag */
3625 LunExtension
->Flags
&= ~LUNEX_FROZEN_QUEUE
;
3627 /* Acquire the spinlock */
3628 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
3630 /* Process the request */
3631 SpiGetNextRequestFromLun(DeviceObject
->DeviceExtension
, LunExtension
);
3633 /* SpiGetNextRequestFromLun() releases the spinlock,
3634 so we just lower irql back to what it was before */
3638 /* Check if data overrun happened */
3639 if (SRB_STATUS(Srb
.SrbStatus
) == SRB_STATUS_DATA_OVERRUN
)
3641 DPRINT("Data overrun at TargetId %d\n", LunInfo
->TargetId
);
3642 /* Nothing dramatic, just copy data, but limiting the size */
3643 RtlCopyMemory(LunInfo
->InquiryData
,
3645 (Srb
.DataTransferLength
> INQUIRYDATABUFFERSIZE
) ?
3646 INQUIRYDATABUFFERSIZE
: Srb
.DataTransferLength
);
3648 Status
= STATUS_SUCCESS
;
3651 else if ((Srb
.SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
) &&
3652 SenseBuffer
->SenseKey
== SCSI_SENSE_ILLEGAL_REQUEST
)
3654 /* LUN is not valid, but some device responds there.
3655 Mark it as invalid anyway */
3657 Status
= STATUS_INVALID_DEVICE_REQUEST
;
3662 /* Retry a couple of times if no timeout happened */
3663 if ((RetryCount
< 2) &&
3664 (SRB_STATUS(Srb
.SrbStatus
) != SRB_STATUS_NO_DEVICE
) &&
3665 (SRB_STATUS(Srb
.SrbStatus
) != SRB_STATUS_SELECTION_TIMEOUT
))
3672 /* That's all, go to exit */
3675 /* Set status according to SRB status */
3676 if (SRB_STATUS(Srb
.SrbStatus
) == SRB_STATUS_BAD_FUNCTION
||
3677 SRB_STATUS(Srb
.SrbStatus
) == SRB_STATUS_BAD_SRB_BLOCK_LENGTH
)
3679 Status
= STATUS_INVALID_DEVICE_REQUEST
;
3683 Status
= STATUS_IO_DEVICE_ERROR
;
3691 ExFreePool(InquiryBuffer
);
3692 ExFreePool(SenseBuffer
);
3694 DPRINT("SpiSendInquiry() done with Status 0x%08X\n", Status
);
3700 /* Scans all SCSI buses */
3702 SpiScanAdapter(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
)
3704 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3708 PSCSI_BUS_SCAN_INFO BusScanInfo
;
3709 PSCSI_LUN_INFO LastLunInfo
, LunInfo
, LunInfoExists
;
3710 BOOLEAN DeviceExists
;
3715 DPRINT("SpiScanAdapter() called\n");
3717 /* Scan all buses */
3718 for (Bus
= 0; Bus
< DeviceExtension
->BusNum
; Bus
++)
3720 DPRINT(" Scanning bus %d\n", Bus
);
3723 /* Get pointer to the scan information */
3724 BusScanInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
];
3728 /* Find the last LUN info in the list */
3729 LunInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LunInfo
;
3730 LastLunInfo
= LunInfo
;
3732 while (LunInfo
!= NULL
)
3734 LastLunInfo
= LunInfo
;
3735 LunInfo
= LunInfo
->Next
;
3740 /* We need to allocate this buffer */
3741 BusScanInfo
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(SCSI_BUS_SCAN_INFO
), TAG_SCSIPORT
);
3745 DPRINT1("Out of resources!\n");
3749 /* Store the pointer in the BusScanInfo array */
3750 DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
] = BusScanInfo
;
3752 /* Fill this struct (length and bus ids for now) */
3753 BusScanInfo
->Length
= sizeof(SCSI_BUS_SCAN_INFO
);
3754 BusScanInfo
->LogicalUnitsCount
= 0;
3755 BusScanInfo
->BusIdentifier
= DeviceExtension
->PortConfig
->InitiatorBusId
[Bus
];
3756 BusScanInfo
->LunInfo
= NULL
;
3758 /* Set pointer to the last LUN info to NULL */
3762 /* Create LUN information structure */
3763 LunInfo
= ExAllocatePoolWithTag(PagedPool
, sizeof(SCSI_LUN_INFO
), TAG_SCSIPORT
);
3765 if (LunInfo
== NULL
)
3767 DPRINT1("Out of resources!\n");
3771 RtlZeroMemory(LunInfo
, sizeof(SCSI_LUN_INFO
));
3773 /* Create LunExtension */
3774 LunExtension
= SpiAllocateLunExtension (DeviceExtension
);
3776 /* And send INQUIRY to every target */
3777 for (Target
= 0; Target
< DeviceExtension
->PortConfig
->MaximumNumberOfTargets
; Target
++)
3779 /* TODO: Support scan bottom-up */
3781 /* Skip if it's the same address */
3782 if (Target
== BusScanInfo
->BusIdentifier
)
3785 /* Try to find an existing device here */
3786 DeviceExists
= FALSE
;
3787 LunInfoExists
= BusScanInfo
->LunInfo
;
3789 /* Find matching address on this bus */
3790 while (LunInfoExists
)
3792 if (LunInfoExists
->TargetId
== Target
)
3794 DeviceExists
= TRUE
;
3798 /* Advance to the next one */
3799 LunInfoExists
= LunInfoExists
->Next
;
3802 /* No need to bother rescanning, since we already did that before */
3806 /* Scan all logical units */
3807 for (Lun
= 0; Lun
< SCSI_MAXIMUM_LOGICAL_UNITS
; Lun
++)
3809 if ((!LunExtension
) || (!LunInfo
))
3812 /* Add extension to the list */
3813 Hint
= (Target
+ Lun
) % LUS_NUMBER
;
3814 LunExtension
->Next
= DeviceExtension
->LunExtensionList
[Hint
];
3815 DeviceExtension
->LunExtensionList
[Hint
] = LunExtension
;
3817 /* Fill Path, Target, Lun fields */
3818 LunExtension
->PathId
= LunInfo
->PathId
= (UCHAR
)Bus
;
3819 LunExtension
->TargetId
= LunInfo
->TargetId
= (UCHAR
) Target
;
3820 LunExtension
->Lun
= LunInfo
->Lun
= (UCHAR
)Lun
;
3822 /* Set flag to prevent race conditions */
3823 LunExtension
->Flags
|= SCSI_PORT_SCAN_IN_PROGRESS
;
3825 /* Zero LU extension contents */
3826 if (DeviceExtension
->LunExtensionSize
)
3828 RtlZeroMemory(LunExtension
+ 1,
3829 DeviceExtension
->LunExtensionSize
);
3832 /* Finally send the inquiry command */
3833 Status
= SpiSendInquiry(DeviceExtension
->DeviceObject
, LunInfo
);
3835 if (NT_SUCCESS(Status
))
3837 /* Let's see if we really found a device */
3838 PINQUIRYDATA InquiryData
= (PINQUIRYDATA
)LunInfo
->InquiryData
;
3840 /* Check if this device is unsupported */
3841 if (InquiryData
->DeviceTypeQualifier
== DEVICE_QUALIFIER_NOT_SUPPORTED
)
3843 DeviceExtension
->LunExtensionList
[Hint
] =
3844 DeviceExtension
->LunExtensionList
[Hint
]->Next
;
3849 /* Clear the "in scan" flag */
3850 LunExtension
->Flags
&= ~SCSI_PORT_SCAN_IN_PROGRESS
;
3852 DPRINT("SpiScanAdapter(): Found device of type %d at bus %d tid %d lun %d\n",
3853 InquiryData
->DeviceType
, Bus
, Target
, Lun
);
3855 /* Add this info to the linked list */
3856 LunInfo
->Next
= NULL
;
3858 LastLunInfo
->Next
= LunInfo
;
3860 BusScanInfo
->LunInfo
= LunInfo
;
3862 /* Store the last LUN info */
3863 LastLunInfo
= LunInfo
;
3865 /* Store DeviceObject */
3866 LunInfo
->DeviceObject
= DeviceExtension
->DeviceObject
;
3868 /* Allocate another buffer */
3869 LunInfo
= ExAllocatePoolWithTag(PagedPool
, sizeof(SCSI_LUN_INFO
), TAG_SCSIPORT
);
3873 DPRINT1("Out of resources!\n");
3877 RtlZeroMemory(LunInfo
, sizeof(SCSI_LUN_INFO
));
3879 /* Create a new LU extension */
3880 LunExtension
= SpiAllocateLunExtension(DeviceExtension
);
3886 /* Remove this LUN from the list */
3887 DeviceExtension
->LunExtensionList
[Hint
] =
3888 DeviceExtension
->LunExtensionList
[Hint
]->Next
;
3890 /* Decide whether we are continuing or not */
3891 if (Status
== STATUS_INVALID_DEVICE_REQUEST
)
3899 /* Free allocated buffers */
3901 ExFreePool(LunExtension
);
3904 ExFreePool(LunInfo
);
3906 /* Sum what we found */
3907 BusScanInfo
->LogicalUnitsCount
+= (UCHAR
) DevicesFound
;
3908 DPRINT(" Found %d devices on bus %d\n", DevicesFound
, Bus
);
3911 DPRINT ("SpiScanAdapter() done\n");
3916 SpiGetInquiryData(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
3919 ULONG InquiryDataSize
;
3920 PSCSI_LUN_INFO LunInfo
;
3921 ULONG BusCount
, LunCount
, Length
;
3922 PIO_STACK_LOCATION IrpStack
;
3923 PSCSI_ADAPTER_BUS_INFO AdapterBusInfo
;
3924 PSCSI_INQUIRY_DATA InquiryData
;
3925 PSCSI_BUS_DATA BusData
;
3929 DPRINT("SpiGetInquiryData() called\n");
3931 /* Get pointer to the buffer */
3932 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
3933 Buffer
= Irp
->AssociatedIrp
.SystemBuffer
;
3935 /* Initialize bus and LUN counters */
3936 BusCount
= DeviceExtension
->BusesConfig
->NumberOfBuses
;
3939 /* Calculate total number of LUNs */
3940 for (Bus
= 0; Bus
< BusCount
; Bus
++)
3941 LunCount
+= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LogicalUnitsCount
;
3943 /* Calculate size of inquiry data, rounding up to sizeof(ULONG) */
3945 ((sizeof(SCSI_INQUIRY_DATA
) - 1 + INQUIRYDATABUFFERSIZE
+
3946 sizeof(ULONG
) - 1) & ~(sizeof(ULONG
) - 1));
3948 /* Calculate data size */
3949 Length
= sizeof(SCSI_ADAPTER_BUS_INFO
) + (BusCount
- 1) *
3950 sizeof(SCSI_BUS_DATA
);
3952 Length
+= InquiryDataSize
* LunCount
;
3954 /* Check, if all data is going to fit into provided buffer */
3955 if (IrpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
< Length
)
3957 Irp
->IoStatus
.Status
= STATUS_BUFFER_TOO_SMALL
;
3958 return STATUS_BUFFER_TOO_SMALL
;
3961 /* Store data size in the IRP */
3962 Irp
->IoStatus
.Information
= Length
;
3964 DPRINT("Data size: %lu\n", Length
);
3966 AdapterBusInfo
= (PSCSI_ADAPTER_BUS_INFO
)Buffer
;
3968 AdapterBusInfo
->NumberOfBuses
= (UCHAR
)BusCount
;
3970 /* Point InquiryData to the corresponding place inside Buffer */
3971 InquiryData
= (PSCSI_INQUIRY_DATA
)(Buffer
+ sizeof(SCSI_ADAPTER_BUS_INFO
) +
3972 (BusCount
- 1) * sizeof(SCSI_BUS_DATA
));
3975 for (Bus
= 0; Bus
< BusCount
; Bus
++)
3977 BusData
= &AdapterBusInfo
->BusData
[Bus
];
3979 /* Calculate and save an offset of the inquiry data */
3980 BusData
->InquiryDataOffset
= (PUCHAR
)InquiryData
- Buffer
;
3982 /* Get a pointer to the LUN information structure */
3983 LunInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LunInfo
;
3985 /* Store Initiator Bus Id */
3986 BusData
->InitiatorBusId
=
3987 DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->BusIdentifier
;
3989 /* Store LUN count */
3990 BusData
->NumberOfLogicalUnits
=
3991 DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LogicalUnitsCount
;
3994 while (LunInfo
!= NULL
)
3996 DPRINT("(Bus %lu Target %lu Lun %lu)\n",
3997 Bus
, LunInfo
->TargetId
, LunInfo
->Lun
);
3999 /* Fill InquiryData with values */
4000 InquiryData
->PathId
= LunInfo
->PathId
;
4001 InquiryData
->TargetId
= LunInfo
->TargetId
;
4002 InquiryData
->Lun
= LunInfo
->Lun
;
4003 InquiryData
->InquiryDataLength
= INQUIRYDATABUFFERSIZE
;
4004 InquiryData
->DeviceClaimed
= LunInfo
->DeviceClaimed
;
4005 InquiryData
->NextInquiryDataOffset
=
4006 (PUCHAR
)InquiryData
+ InquiryDataSize
- Buffer
;
4008 /* Copy data in it */
4009 RtlCopyMemory(InquiryData
->InquiryData
,
4010 LunInfo
->InquiryData
,
4011 INQUIRYDATABUFFERSIZE
);
4013 /* Move to the next LUN */
4014 LunInfo
= LunInfo
->Next
;
4015 InquiryData
= (PSCSI_INQUIRY_DATA
) ((PCHAR
)InquiryData
+ InquiryDataSize
);
4018 /* Either mark the end, or set offset to 0 */
4019 if (BusData
->NumberOfLogicalUnits
!= 0)
4020 ((PSCSI_INQUIRY_DATA
) ((PCHAR
) InquiryData
- InquiryDataSize
))->NextInquiryDataOffset
= 0;
4022 BusData
->InquiryDataOffset
= 0;
4025 /* Finish with success */
4026 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
4027 return STATUS_SUCCESS
;
4030 static PSCSI_REQUEST_BLOCK_INFO
4031 SpiGetSrbData(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
4037 PSCSI_PORT_LUN_EXTENSION LunExtension
;
4039 if (QueueTag
== SP_UNTAGGED
)
4041 /* Untagged request, get LU and return pointer to SrbInfo */
4042 LunExtension
= SpiGetLunExtension(DeviceExtension
,
4047 /* Return NULL in case of error */
4051 /* Return the pointer to SrbInfo */
4052 return &LunExtension
->SrbInfo
;
4056 /* Make sure the tag is valid, if it is - return the data */
4057 if (QueueTag
> DeviceExtension
->SrbDataCount
|| QueueTag
< 1)
4060 return &DeviceExtension
->SrbInfo
[QueueTag
-1];
4065 SpiSendRequestSense(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
4066 IN PSCSI_REQUEST_BLOCK InitialSrb
)
4068 PSCSI_REQUEST_BLOCK Srb
;
4071 PIO_STACK_LOCATION IrpStack
;
4072 LARGE_INTEGER LargeInt
;
4075 DPRINT("SpiSendRequestSense() entered, InitialSrb %p\n", InitialSrb
);
4078 Srb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(SCSI_REQUEST_BLOCK
) + sizeof(PVOID
), TAG_SCSIPORT
);
4079 RtlZeroMemory(Srb
, sizeof(SCSI_REQUEST_BLOCK
));
4082 LargeInt
.QuadPart
= (LONGLONG
) 1;
4083 Irp
= IoBuildAsynchronousFsdRequest(IRP_MJ_READ
,
4084 DeviceExtension
->DeviceObject
,
4085 InitialSrb
->SenseInfoBuffer
,
4086 InitialSrb
->SenseInfoBufferLength
,
4090 IoSetCompletionRoutine(Irp
,
4091 (PIO_COMPLETION_ROUTINE
)SpiCompletionRoutine
,
4099 DPRINT("SpiSendRequestSense() failed, Srb %p\n", Srb
);
4103 IrpStack
= IoGetNextIrpStackLocation(Irp
);
4104 IrpStack
->MajorFunction
= IRP_MJ_SCSI
;
4106 /* Put Srb address into Irp... */
4107 IrpStack
->Parameters
.Others
.Argument1
= (PVOID
)Srb
;
4109 /* ...and vice versa */
4110 Srb
->OriginalRequest
= Irp
;
4113 Ptr
= (PVOID
*)(Srb
+1);
4116 /* Build CDB for REQUEST SENSE */
4118 Cdb
= (PCDB
)Srb
->Cdb
;
4120 Cdb
->CDB6INQUIRY
.OperationCode
= SCSIOP_REQUEST_SENSE
;
4121 Cdb
->CDB6INQUIRY
.LogicalUnitNumber
= 0;
4122 Cdb
->CDB6INQUIRY
.Reserved1
= 0;
4123 Cdb
->CDB6INQUIRY
.PageCode
= 0;
4124 Cdb
->CDB6INQUIRY
.IReserved
= 0;
4125 Cdb
->CDB6INQUIRY
.AllocationLength
= (UCHAR
)InitialSrb
->SenseInfoBufferLength
;
4126 Cdb
->CDB6INQUIRY
.Control
= 0;
4129 Srb
->TargetId
= InitialSrb
->TargetId
;
4130 Srb
->Lun
= InitialSrb
->Lun
;
4131 Srb
->PathId
= InitialSrb
->PathId
;
4133 Srb
->Function
= SRB_FUNCTION_EXECUTE_SCSI
;
4134 Srb
->Length
= sizeof(SCSI_REQUEST_BLOCK
);
4136 /* Timeout will be 2 seconds */
4137 Srb
->TimeOutValue
= 2;
4139 /* No auto request sense */
4140 Srb
->SenseInfoBufferLength
= 0;
4141 Srb
->SenseInfoBuffer
= NULL
;
4143 /* Set necessary flags */
4144 Srb
->SrbFlags
= SRB_FLAGS_DATA_IN
| SRB_FLAGS_BYPASS_FROZEN_QUEUE
|
4145 SRB_FLAGS_DISABLE_DISCONNECT
;
4147 /* Transfer disable synch transfer flag */
4148 if (InitialSrb
->SrbFlags
& SRB_FLAGS_DISABLE_SYNCH_TRANSFER
)
4149 Srb
->SrbFlags
|= SRB_FLAGS_DISABLE_SYNCH_TRANSFER
;
4151 Srb
->DataBuffer
= InitialSrb
->SenseInfoBuffer
;
4153 /* Fill the transfer length */
4154 Srb
->DataTransferLength
= InitialSrb
->SenseInfoBufferLength
;
4156 /* Clear statuses */
4157 Srb
->ScsiStatus
= Srb
->SrbStatus
= 0;
4160 /* Call the driver */
4161 (VOID
)IoCallDriver(DeviceExtension
->DeviceObject
, Irp
);
4163 DPRINT("SpiSendRequestSense() done\n");
4170 SpiProcessCompletedRequest(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
4171 IN PSCSI_REQUEST_BLOCK_INFO SrbInfo
,
4172 OUT PBOOLEAN NeedToCallStartIo
)
4174 PSCSI_REQUEST_BLOCK Srb
;
4175 PSCSI_PORT_LUN_EXTENSION LunExtension
;
4178 ULONG SequenceNumber
;
4181 Irp
= Srb
->OriginalRequest
;
4183 /* Get Lun extension */
4184 LunExtension
= SpiGetLunExtension(DeviceExtension
,
4189 if (Srb
->SrbFlags
& SRB_FLAGS_UNSPECIFIED_DIRECTION
&&
4190 DeviceExtension
->MapBuffers
&&
4193 /* MDL is shared if transfer is broken into smaller parts */
4194 Srb
->DataBuffer
= (PCCHAR
)MmGetMdlVirtualAddress(Irp
->MdlAddress
) +
4195 ((PCCHAR
)Srb
->DataBuffer
- SrbInfo
->DataOffset
);
4197 /* In case of data going in, flush the buffers */
4198 if (Srb
->SrbFlags
& SRB_FLAGS_DATA_IN
)
4200 KeFlushIoBuffers(Irp
->MdlAddress
,
4206 /* Flush adapter if needed */
4207 if (SrbInfo
->BaseOfMapRegister
)
4209 /* TODO: Implement */
4213 /* Clear the request */
4214 SrbInfo
->Srb
= NULL
;
4216 /* If disconnect is disabled... */
4217 if (Srb
->SrbFlags
& SRB_FLAGS_DISABLE_DISCONNECT
)
4219 /* Acquire the spinlock since we mess with flags */
4220 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4222 /* Set corresponding flag */
4223 DeviceExtension
->Flags
|= SCSI_PORT_DISCONNECT_ALLOWED
;
4225 /* Clear the timer if needed */
4226 if (!(DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_RESET
))
4227 DeviceExtension
->TimerCount
= -1;
4229 /* Spinlock is not needed anymore */
4230 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4232 if (!(DeviceExtension
->Flags
& SCSI_PORT_REQUEST_PENDING
) &&
4233 !(DeviceExtension
->Flags
& SCSI_PORT_DEVICE_BUSY
) &&
4234 !(*NeedToCallStartIo
))
4236 /* We're not busy, but we have a request pending */
4237 IoStartNextPacket(DeviceExtension
->DeviceObject
, FALSE
);
4241 /* Scatter/gather */
4242 if (Srb
->SrbFlags
& SRB_FLAGS_SGLIST_FROM_POOL
)
4244 /* TODO: Implement */
4248 /* Acquire spinlock (we're freeing SrbExtension) */
4249 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4251 /* Free it (if needed) */
4252 if (Srb
->SrbExtension
)
4254 if (Srb
->SenseInfoBuffer
!= NULL
&& DeviceExtension
->SupportsAutoSense
)
4256 ASSERT(Srb
->SenseInfoBuffer
== NULL
|| SrbInfo
->SaveSenseRequest
!= NULL
);
4258 if (Srb
->SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
)
4260 /* Copy sense data to the buffer */
4261 RtlCopyMemory(SrbInfo
->SaveSenseRequest
,
4262 Srb
->SenseInfoBuffer
,
4263 Srb
->SenseInfoBufferLength
);
4266 /* And restore the pointer */
4267 Srb
->SenseInfoBuffer
= SrbInfo
->SaveSenseRequest
;
4270 /* Put it into the free srb extensions list */
4271 *((PVOID
*)Srb
->SrbExtension
) = DeviceExtension
->FreeSrbExtensions
;
4272 DeviceExtension
->FreeSrbExtensions
= Srb
->SrbExtension
;
4275 /* Save transfer length in the IRP */
4276 Irp
->IoStatus
.Information
= Srb
->DataTransferLength
;
4278 SequenceNumber
= SrbInfo
->SequenceNumber
;
4279 SrbInfo
->SequenceNumber
= 0;
4281 /* Decrement the queue count */
4282 LunExtension
->QueueCount
--;
4284 /* Free Srb, if needed*/
4285 if (Srb
->QueueTag
!= SP_UNTAGGED
)
4287 /* Put it into the free list */
4288 SrbInfo
->Requests
.Blink
= NULL
;
4289 SrbInfo
->Requests
.Flink
= (PLIST_ENTRY
)DeviceExtension
->FreeSrbInfo
;
4290 DeviceExtension
->FreeSrbInfo
= SrbInfo
;
4293 /* SrbInfo is not used anymore */
4296 if (DeviceExtension
->Flags
& SCSI_PORT_REQUEST_PENDING
)
4298 /* Clear the flag */
4299 DeviceExtension
->Flags
&= ~SCSI_PORT_REQUEST_PENDING
;
4301 /* Note the caller about StartIo */
4302 *NeedToCallStartIo
= TRUE
;
4305 if (SRB_STATUS(Srb
->SrbStatus
) == SRB_STATUS_SUCCESS
)
4307 /* Start the packet */
4308 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
4310 if (!(Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
) &&
4311 LunExtension
->RequestTimeout
== -1)
4313 /* Start the next packet */
4314 SpiGetNextRequestFromLun(DeviceExtension
, LunExtension
);
4318 /* Release the spinlock */
4319 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4322 DPRINT("IoCompleting request IRP 0x%p\n", Irp
);
4324 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
4326 /* Decrement number of active requests, and analyze the result */
4327 Result
= InterlockedDecrement(&DeviceExtension
->ActiveRequestCounter
);
4330 !DeviceExtension
->MapRegisters
&&
4331 DeviceExtension
->AdapterObject
!= NULL
)
4333 /* Nullify map registers */
4334 DeviceExtension
->MapRegisterBase
= NULL
;
4335 IoFreeAdapterChannel(DeviceExtension
->AdapterObject
);
4338 /* Exit, we're done */
4342 /* Decrement number of active requests, and analyze the result */
4343 Result
= InterlockedDecrement(&DeviceExtension
->ActiveRequestCounter
);
4346 !DeviceExtension
->MapRegisters
&&
4347 DeviceExtension
->AdapterObject
!= NULL
)
4349 /* Result is negative, so this is a slave, free map registers */
4350 DeviceExtension
->MapRegisterBase
= NULL
;
4351 IoFreeAdapterChannel(DeviceExtension
->AdapterObject
);
4354 /* Convert status */
4355 Irp
->IoStatus
.Status
= SpiStatusSrbToNt(Srb
->SrbStatus
);
4357 /* It's not a bypass, it's busy or the queue is full? */
4358 if ((Srb
->ScsiStatus
== SCSISTAT_BUSY
||
4359 Srb
->SrbStatus
== SRB_STATUS_BUSY
||
4360 Srb
->ScsiStatus
== SCSISTAT_QUEUE_FULL
) &&
4361 !(Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
))
4364 DPRINT("Busy SRB status %x\n", Srb
->SrbStatus
);
4366 /* Requeu, if needed */
4367 if (LunExtension
->Flags
& (LUNEX_FROZEN_QUEUE
| LUNEX_BUSY
))
4369 DPRINT("it's being requeued\n");
4371 Srb
->SrbStatus
= SRB_STATUS_PENDING
;
4372 Srb
->ScsiStatus
= 0;
4374 if (!KeInsertByKeyDeviceQueue(&LunExtension
->DeviceQueue
,
4375 &Irp
->Tail
.Overlay
.DeviceQueueEntry
,
4378 /* It's a big f.ck up if we got here */
4379 Srb
->SrbStatus
= SRB_STATUS_ERROR
;
4380 Srb
->ScsiStatus
= SCSISTAT_BUSY
;
4386 /* Release the spinlock */
4387 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4390 else if (LunExtension
->AttemptCount
++ < 20)
4392 /* LUN is still busy */
4393 Srb
->ScsiStatus
= 0;
4394 Srb
->SrbStatus
= SRB_STATUS_PENDING
;
4396 LunExtension
->BusyRequest
= Irp
;
4397 LunExtension
->Flags
|= LUNEX_BUSY
;
4399 /* Release the spinlock */
4400 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4405 /* Freeze the queue*/
4406 Srb
->SrbStatus
|= SRB_STATUS_QUEUE_FROZEN
;
4407 LunExtension
->Flags
|= LUNEX_FROZEN_QUEUE
;
4409 /* "Unfull" the queue */
4410 LunExtension
->Flags
&= ~LUNEX_FULL_QUEUE
;
4412 /* Release the spinlock */
4413 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4415 /* Return status that the device is not ready */
4416 Irp
->IoStatus
.Status
= STATUS_DEVICE_NOT_READY
;
4417 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
4423 /* Start the next request, if LUN is idle, and this is sense request */
4424 if (((Srb
->ScsiStatus
!= SCSISTAT_CHECK_CONDITION
) ||
4425 (Srb
->SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
) ||
4426 !Srb
->SenseInfoBuffer
|| !Srb
->SenseInfoBufferLength
)
4427 && (Srb
->SrbFlags
& SRB_FLAGS_NO_QUEUE_FREEZE
))
4429 if (LunExtension
->RequestTimeout
== -1)
4430 SpiGetNextRequestFromLun(DeviceExtension
, LunExtension
);
4432 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4436 /* Freeze the queue */
4437 Srb
->SrbStatus
|= SRB_STATUS_QUEUE_FROZEN
;
4438 LunExtension
->Flags
|= LUNEX_FROZEN_QUEUE
;
4440 /* Do we need a request sense? */
4441 if (Srb
->ScsiStatus
== SCSISTAT_CHECK_CONDITION
&&
4442 !(Srb
->SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
) &&
4443 Srb
->SenseInfoBuffer
&& Srb
->SenseInfoBufferLength
)
4445 /* If LUN is busy, we have to requeue it in order to allow request sense */
4446 if (LunExtension
->Flags
& LUNEX_BUSY
)
4448 DPRINT("Requeueing busy request to allow request sense\n");
4450 if (!KeInsertByKeyDeviceQueue(&LunExtension
->DeviceQueue
,
4451 &LunExtension
->BusyRequest
->Tail
.Overlay
.DeviceQueueEntry
,
4454 /* We should never get here */
4457 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4458 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
4463 /* Clear busy flags */
4464 LunExtension
->Flags
&= ~(LUNEX_FULL_QUEUE
| LUNEX_BUSY
);
4467 /* Release the spinlock */
4468 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4470 /* Send RequestSense */
4471 SpiSendRequestSense(DeviceExtension
, Srb
);
4477 /* Release the spinlock */
4478 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4481 /* Complete the request */
4482 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
4487 SpiCompletionRoutine(PDEVICE_OBJECT DeviceObject
,
4491 PSCSI_REQUEST_BLOCK Srb
= (PSCSI_REQUEST_BLOCK
)Context
;
4492 PSCSI_REQUEST_BLOCK InitialSrb
;
4495 DPRINT("SpiCompletionRoutine() entered, IRP %p \n", Irp
);
4497 if ((Srb
->Function
== SRB_FUNCTION_RESET_BUS
) ||
4498 (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
))
4500 /* Deallocate SRB and IRP and exit */
4504 return STATUS_MORE_PROCESSING_REQUIRED
;
4507 /* Get a pointer to the SRB and IRP which were initially sent */
4508 InitialSrb
= *((PVOID
*)(Srb
+1));
4509 InitialIrp
= InitialSrb
->OriginalRequest
;
4511 if ((SRB_STATUS(Srb
->SrbStatus
) == SRB_STATUS_SUCCESS
) ||
4512 (SRB_STATUS(Srb
->SrbStatus
) == SRB_STATUS_DATA_OVERRUN
))
4514 /* Sense data is OK */
4515 InitialSrb
->SrbStatus
|= SRB_STATUS_AUTOSENSE_VALID
;
4517 /* Set length to be the same */
4518 InitialSrb
->SenseInfoBufferLength
= (UCHAR
)Srb
->DataTransferLength
;
4521 /* Make sure initial SRB's queue is frozen */
4522 ASSERT(InitialSrb
->SrbStatus
& SRB_STATUS_QUEUE_FROZEN
);
4524 /* Complete this request */
4525 IoCompleteRequest(InitialIrp
, IO_DISK_INCREMENT
);
4527 /* Deallocate everything (internal) */
4530 if (Irp
->MdlAddress
!= NULL
)
4532 MmUnlockPages(Irp
->MdlAddress
);
4533 IoFreeMdl(Irp
->MdlAddress
);
4534 Irp
->MdlAddress
= NULL
;
4538 return STATUS_MORE_PROCESSING_REQUIRED
;
4541 static BOOLEAN NTAPI
4542 ScsiPortIsr(IN PKINTERRUPT Interrupt
,
4543 IN PVOID ServiceContext
)
4545 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
4548 DPRINT("ScsiPortIsr() called!\n");
4550 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)ServiceContext
;
4552 /* If interrupts are disabled - we don't expect any */
4553 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_DISABLE_INTERRUPTS
)
4556 /* Call miniport's HwInterrupt routine */
4557 Result
= DeviceExtension
->HwInterrupt(&DeviceExtension
->MiniPortDeviceExtension
);
4559 /* If flag of notification is set - queue a DPC */
4560 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
4562 IoRequestDpc(DeviceExtension
->DeviceObject
,
4563 DeviceExtension
->CurrentIrp
,
4572 SpiSaveInterruptData(IN PVOID Context
)
4574 PSCSI_PORT_SAVE_INTERRUPT InterruptContext
= Context
;
4575 PSCSI_PORT_LUN_EXTENSION LunExtension
;
4576 PSCSI_REQUEST_BLOCK Srb
;
4577 PSCSI_REQUEST_BLOCK_INFO SrbInfo
, NextSrbInfo
;
4578 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
4581 /* Get pointer to the device extension */
4582 DeviceExtension
= InterruptContext
->DeviceExtension
;
4584 /* If we don't have anything pending - return */
4585 if (!(DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
))
4588 /* Actually save the interrupt data */
4589 *InterruptContext
->InterruptData
= DeviceExtension
->InterruptData
;
4591 /* Clear the data stored in the device extension */
4592 DeviceExtension
->InterruptData
.Flags
&=
4593 (SCSI_PORT_RESET
| SCSI_PORT_RESET_REQUEST
| SCSI_PORT_DISABLE_INTERRUPTS
);
4594 DeviceExtension
->InterruptData
.CompletedAbort
= NULL
;
4595 DeviceExtension
->InterruptData
.ReadyLun
= NULL
;
4596 DeviceExtension
->InterruptData
.CompletedRequests
= NULL
;
4598 /* Loop through the list of completed requests */
4599 SrbInfo
= InterruptContext
->InterruptData
->CompletedRequests
;
4603 /* Make sure we have SRV */
4604 ASSERT(SrbInfo
->Srb
);
4606 /* Get SRB and LunExtension */
4609 LunExtension
= SpiGetLunExtension(DeviceExtension
,
4614 /* We have to check special cases if request is unsuccessfull*/
4615 if (Srb
->SrbStatus
!= SRB_STATUS_SUCCESS
)
4617 /* Check if we need request sense by a few conditions */
4618 if (Srb
->SenseInfoBuffer
&& Srb
->SenseInfoBufferLength
&&
4619 Srb
->ScsiStatus
== SCSISTAT_CHECK_CONDITION
&&
4620 !(Srb
->SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
))
4622 if (LunExtension
->Flags
& LUNEX_NEED_REQUEST_SENSE
)
4624 /* It means: we tried to send REQUEST SENSE, but failed */
4626 Srb
->ScsiStatus
= 0;
4627 Srb
->SrbStatus
= SRB_STATUS_REQUEST_SENSE_FAILED
;
4631 /* Set the corresponding flag, so that REQUEST SENSE
4633 LunExtension
->Flags
|= LUNEX_NEED_REQUEST_SENSE
;
4638 /* Check for a full queue */
4639 if (Srb
->ScsiStatus
== SCSISTAT_QUEUE_FULL
)
4641 /* TODO: Implement when it's encountered */
4646 /* Let's decide if we need to watch timeout or not */
4647 if (Srb
->QueueTag
== SP_UNTAGGED
)
4653 if (LunExtension
->SrbInfo
.Requests
.Flink
== &SrbInfo
->Requests
)
4658 /* Remove it from the queue */
4659 RemoveEntryList(&SrbInfo
->Requests
);
4664 /* We have to maintain timeout counter */
4665 if (IsListEmpty(&LunExtension
->SrbInfo
.Requests
))
4667 LunExtension
->RequestTimeout
= -1;
4671 NextSrbInfo
= CONTAINING_RECORD(LunExtension
->SrbInfo
.Requests
.Flink
,
4672 SCSI_REQUEST_BLOCK_INFO
,
4675 Srb
= NextSrbInfo
->Srb
;
4677 /* Update timeout counter */
4678 LunExtension
->RequestTimeout
= Srb
->TimeOutValue
;
4682 SrbInfo
= SrbInfo
->CompletedRequests
;
4690 SpiGetNextRequestFromLun(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
4691 IN PSCSI_PORT_LUN_EXTENSION LunExtension
)
4693 PIO_STACK_LOCATION IrpStack
;
4695 PKDEVICE_QUEUE_ENTRY Entry
;
4696 PSCSI_REQUEST_BLOCK Srb
;
4699 /* If LUN is not active or queue is more than maximum allowed */
4700 if (LunExtension
->QueueCount
>= LunExtension
->MaxQueueCount
||
4701 !(LunExtension
->Flags
& SCSI_PORT_LU_ACTIVE
))
4703 /* Release the spinlock and exit */
4704 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4708 /* Check if we can get a next request */
4709 if (LunExtension
->Flags
&
4710 (LUNEX_NEED_REQUEST_SENSE
| LUNEX_BUSY
|
4711 LUNEX_FULL_QUEUE
| LUNEX_FROZEN_QUEUE
| LUNEX_REQUEST_PENDING
))
4713 /* Pending requests can only be started if the queue is empty */
4714 if (IsListEmpty(&LunExtension
->SrbInfo
.Requests
) &&
4715 !(LunExtension
->Flags
&
4716 (LUNEX_BUSY
| LUNEX_FROZEN_QUEUE
| LUNEX_FULL_QUEUE
| LUNEX_NEED_REQUEST_SENSE
)))
4718 /* Make sure we have SRB */
4719 ASSERT(LunExtension
->SrbInfo
.Srb
== NULL
);
4721 /* Clear active and pending flags */
4722 LunExtension
->Flags
&= ~(LUNEX_REQUEST_PENDING
| SCSI_PORT_LU_ACTIVE
);
4724 /* Get next Irp, and clear pending requests list */
4725 NextIrp
= LunExtension
->PendingRequest
;
4726 LunExtension
->PendingRequest
= NULL
;
4728 /* Set attempt counter to zero */
4729 LunExtension
->AttemptCount
= 0;
4731 /* Release the spinlock */
4732 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4734 /* Start the next pending request */
4735 IoStartPacket(DeviceExtension
->DeviceObject
, NextIrp
, (PULONG
)NULL
, NULL
);
4741 /* Release the spinlock, without clearing any flags and exit */
4742 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4748 /* Reset active flag */
4749 LunExtension
->Flags
&= ~SCSI_PORT_LU_ACTIVE
;
4751 /* Set attempt counter to zero */
4752 LunExtension
->AttemptCount
= 0;
4754 /* Remove packet from the device queue */
4755 Entry
= KeRemoveByKeyDeviceQueue(&LunExtension
->DeviceQueue
, LunExtension
->SortKey
);
4759 /* Get pointer to the next irp */
4760 NextIrp
= CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.DeviceQueueEntry
);
4762 /* Get point to the SRB */
4763 IrpStack
= IoGetCurrentIrpStackLocation(NextIrp
);
4764 Srb
= (PSCSI_REQUEST_BLOCK
)IrpStack
->Parameters
.Others
.Argument1
;
4767 LunExtension
->SortKey
= Srb
->QueueSortKey
;
4768 LunExtension
->SortKey
++;
4770 /* Release the spinlock */
4771 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4773 /* Start the next pending request */
4774 IoStartPacket(DeviceExtension
->DeviceObject
, NextIrp
, (PULONG
)NULL
, NULL
);
4778 /* Release the spinlock */
4779 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4785 // ScsiPortDpcForIsr
4792 // IN PDEVICE_OBJECT DpcDeviceObject
4794 // IN PVOID DpcContext
4797 ScsiPortDpcForIsr(IN PKDPC Dpc
,
4798 IN PDEVICE_OBJECT DpcDeviceObject
,
4800 IN PVOID DpcContext
)
4802 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
= DpcDeviceObject
->DeviceExtension
;
4803 SCSI_PORT_INTERRUPT_DATA InterruptData
;
4804 SCSI_PORT_SAVE_INTERRUPT Context
;
4805 PSCSI_PORT_LUN_EXTENSION LunExtension
;
4806 BOOLEAN NeedToStartIo
;
4807 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
4808 LARGE_INTEGER TimerValue
;
4810 DPRINT("ScsiPortDpcForIsr(Dpc %p DpcDeviceObject %p DpcIrp %p DpcContext %p)\n",
4811 Dpc
, DpcDeviceObject
, DpcIrp
, DpcContext
);
4813 /* We need to acquire spinlock */
4814 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4816 RtlZeroMemory(&InterruptData
, sizeof(SCSI_PORT_INTERRUPT_DATA
));
4820 /* Interrupt structure must be snapshotted, and only then analyzed */
4821 Context
.InterruptData
= &InterruptData
;
4822 Context
.DeviceExtension
= DeviceExtension
;
4824 if (!KeSynchronizeExecution(DeviceExtension
->Interrupt
,
4825 SpiSaveInterruptData
,
4828 /* Nothing - just return (don't forget to release the spinlock */
4829 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4830 DPRINT("ScsiPortDpcForIsr() done\n");
4834 /* If flush of adapters is needed - do it */
4835 if (InterruptData
.Flags
& SCSI_PORT_FLUSH_ADAPTERS
)
4837 /* TODO: Implement */
4841 /* Check for IoMapTransfer */
4842 if (InterruptData
.Flags
& SCSI_PORT_MAP_TRANSFER
)
4844 /* TODO: Implement */
4848 /* Check if timer is needed */
4849 if (InterruptData
.Flags
& SCSI_PORT_TIMER_NEEDED
)
4851 /* Save the timer routine */
4852 DeviceExtension
->HwScsiTimer
= InterruptData
.HwScsiTimer
;
4854 if (InterruptData
.MiniportTimerValue
== 0)
4856 /* Cancel the timer */
4857 KeCancelTimer(&DeviceExtension
->MiniportTimer
);
4861 /* Convert timer value */
4862 TimerValue
.QuadPart
= Int32x32To64(InterruptData
.MiniportTimerValue
, -10);
4865 KeSetTimer(&DeviceExtension
->MiniportTimer
,
4867 &DeviceExtension
->MiniportTimerDpc
);
4871 /* If it's ready for the next request */
4872 if (InterruptData
.Flags
& SCSI_PORT_NEXT_REQUEST_READY
)
4874 /* Check for a duplicate request (NextRequest+NextLuRequest) */
4875 if ((DeviceExtension
->Flags
&
4876 (SCSI_PORT_DEVICE_BUSY
| SCSI_PORT_DISCONNECT_ALLOWED
)) ==
4877 (SCSI_PORT_DEVICE_BUSY
| SCSI_PORT_DISCONNECT_ALLOWED
))
4879 /* Clear busy flag set by ScsiPortStartPacket() */
4880 DeviceExtension
->Flags
&= ~SCSI_PORT_DEVICE_BUSY
;
4882 if (!(InterruptData
.Flags
& SCSI_PORT_RESET
))
4884 /* Ready for next, and no reset is happening */
4885 DeviceExtension
->TimerCount
= -1;
4890 /* Not busy, but not ready for the next request */
4891 DeviceExtension
->Flags
&= ~SCSI_PORT_DEVICE_BUSY
;
4892 InterruptData
.Flags
&= ~SCSI_PORT_NEXT_REQUEST_READY
;
4897 if (InterruptData
.Flags
& SCSI_PORT_RESET_REPORTED
)
4899 /* Hold for a bit */
4900 DeviceExtension
->TimerCount
= 4;
4903 /* Any ready LUN? */
4904 if (InterruptData
.ReadyLun
!= NULL
)
4907 /* Process all LUNs from the list*/
4910 /* Remove it from the list first (as processed) */
4911 LunExtension
= InterruptData
.ReadyLun
;
4912 InterruptData
.ReadyLun
= LunExtension
->ReadyLun
;
4913 LunExtension
->ReadyLun
= NULL
;
4915 /* Get next request for this LUN */
4916 SpiGetNextRequestFromLun(DeviceExtension
, LunExtension
);
4918 /* Still ready requests exist?
4919 If yes - get spinlock, if no - stop here */
4920 if (InterruptData
.ReadyLun
!= NULL
)
4921 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4928 /* Release the spinlock */
4929 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4932 /* If we ready for next packet, start it */
4933 if (InterruptData
.Flags
& SCSI_PORT_NEXT_REQUEST_READY
)
4934 IoStartNextPacket(DeviceExtension
->DeviceObject
, FALSE
);
4936 NeedToStartIo
= FALSE
;
4938 /* Loop the completed request list */
4939 while (InterruptData
.CompletedRequests
)
4941 /* Remove the request */
4942 SrbInfo
= InterruptData
.CompletedRequests
;
4943 InterruptData
.CompletedRequests
= SrbInfo
->CompletedRequests
;
4944 SrbInfo
->CompletedRequests
= NULL
;
4947 SpiProcessCompletedRequest(DeviceExtension
,
4952 /* Loop abort request list */
4953 while (InterruptData
.CompletedAbort
)
4955 LunExtension
= InterruptData
.CompletedAbort
;
4957 /* Remove the request */
4958 InterruptData
.CompletedAbort
= LunExtension
->CompletedAbortRequests
;
4960 /* Get spinlock since we're going to change flags */
4961 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4963 /* TODO: Put SrbExtension to the list of free extensions */
4967 /* If we need - call StartIo routine */
4970 /* Make sure CurrentIrp is not null! */
4971 ASSERT(DpcDeviceObject
->CurrentIrp
!= NULL
);
4972 ScsiPortStartIo(DpcDeviceObject
, DpcDeviceObject
->CurrentIrp
);
4975 /* Everything has been done, check */
4976 if (InterruptData
.Flags
& SCSI_PORT_ENABLE_INT_REQUEST
)
4978 /* Synchronize using spinlock */
4979 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4981 /* Request an interrupt */
4982 DeviceExtension
->HwInterrupt(DeviceExtension
->MiniPortDeviceExtension
);
4984 ASSERT(DeviceExtension
->Flags
& SCSI_PORT_DISABLE_INT_REQUESET
);
4986 /* Should interrupts be enabled again? */
4987 if (DeviceExtension
->Flags
& SCSI_PORT_DISABLE_INT_REQUESET
)
4989 /* Clear this flag */
4990 DeviceExtension
->Flags
&= ~SCSI_PORT_DISABLE_INT_REQUESET
;
4992 /* Call a special routine to do this */
4995 KeSynchronizeExecution(DeviceExtension
->Interrupt
,
4996 SpiEnableInterrupts
,
5001 /* If we need a notification again - loop */
5002 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
5005 /* Release the spinlock */
5006 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
5009 DPRINT("ScsiPortDpcForIsr() done\n");
5014 SpiProcessTimeout(PVOID ServiceContext
)
5016 PDEVICE_OBJECT DeviceObject
= (PDEVICE_OBJECT
)ServiceContext
;
5017 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
= DeviceObject
->DeviceExtension
;
5020 DPRINT("SpiProcessTimeout() entered\n");
5022 DeviceExtension
->TimerCount
= -1;
5024 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_RESET
)
5026 DeviceExtension
->InterruptData
.Flags
&= ~SCSI_PORT_RESET
;
5028 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_RESET_REQUEST
)
5030 DeviceExtension
->InterruptData
.Flags
&= ~SCSI_PORT_RESET_REQUEST
;
5031 ScsiPortStartPacket(ServiceContext
);
5038 DPRINT("Resetting the bus\n");
5040 for (Bus
= 0; Bus
< DeviceExtension
->BusNum
; Bus
++)
5042 DeviceExtension
->HwResetBus(DeviceExtension
->MiniPortDeviceExtension
, Bus
);
5044 /* Reset flags and set reset timeout to 4 seconds */
5045 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_RESET
;
5046 DeviceExtension
->TimerCount
= 4;
5049 /* If miniport requested - request a dpc for it */
5050 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
5051 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
5060 SpiResetBus(PVOID ServiceContext
)
5062 PRESETBUS_PARAMS ResetParams
= (PRESETBUS_PARAMS
)ServiceContext
;
5063 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
5065 /* Perform the bus reset */
5066 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)ResetParams
->DeviceExtension
;
5067 DeviceExtension
->HwResetBus(DeviceExtension
->MiniPortDeviceExtension
,
5068 ResetParams
->PathId
);
5070 /* Set flags and start the timer */
5071 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_RESET
;
5072 DeviceExtension
->TimerCount
= 4;
5074 /* If miniport requested - give him a DPC */
5075 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
5076 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
5083 // This function handles timeouts and other time delayed processing
5088 // IN PDEVICE_OBJECT DeviceObject Device object registered with timer
5089 // IN PVOID Context the Controller extension for the
5090 // controller the device is on
5093 ScsiPortIoTimer(PDEVICE_OBJECT DeviceObject
,
5096 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
5097 PSCSI_PORT_LUN_EXTENSION LunExtension
;
5101 DPRINT("ScsiPortIoTimer()\n");
5103 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
5105 /* Protect with the spinlock */
5106 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
5108 /* Check timeouts */
5109 if (DeviceExtension
->TimerCount
> 0)
5111 /* Decrease the timeout counter */
5112 DeviceExtension
->TimerCount
--;
5114 if (DeviceExtension
->TimerCount
== 0)
5116 /* Timeout, process it */
5117 if (KeSynchronizeExecution(DeviceExtension
->Interrupt
,
5119 DeviceExtension
->DeviceObject
))
5121 DPRINT("Error happened during processing timeout, but nothing critical\n");
5125 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
5127 /* We should exit now, since timeout is processed */
5131 /* Per-Lun scanning of timeouts is needed... */
5132 for (Lun
= 0; Lun
< LUS_NUMBER
; Lun
++)
5134 LunExtension
= DeviceExtension
->LunExtensionList
[Lun
];
5136 while (LunExtension
)
5138 if (LunExtension
->Flags
& LUNEX_BUSY
)
5140 if (!(LunExtension
->Flags
&
5141 (LUNEX_NEED_REQUEST_SENSE
| LUNEX_FROZEN_QUEUE
)))
5143 DPRINT("Retrying busy request\n");
5145 /* Clear flags, and retry busy request */
5146 LunExtension
->Flags
&= ~(LUNEX_BUSY
| LUNEX_FULL_QUEUE
);
5147 Irp
= LunExtension
->BusyRequest
;
5149 /* Clearing busy request */
5150 LunExtension
->BusyRequest
= NULL
;
5152 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
5154 IoStartPacket(DeviceObject
, Irp
, (PULONG
)NULL
, NULL
);
5156 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
5159 else if (LunExtension
->RequestTimeout
== 0)
5161 RESETBUS_PARAMS ResetParams
;
5163 LunExtension
->RequestTimeout
= -1;
5165 DPRINT("Request timed out, resetting bus\n");
5167 /* Pass params to the bus reset routine */
5168 ResetParams
.PathId
= LunExtension
->PathId
;
5169 ResetParams
.DeviceExtension
= DeviceExtension
;
5171 if (!KeSynchronizeExecution(DeviceExtension
->Interrupt
,
5175 DPRINT1("Reset failed\n");
5178 else if (LunExtension
->RequestTimeout
> 0)
5180 /* Decrement the timeout counter */
5181 LunExtension
->RequestTimeout
--;
5184 LunExtension
= LunExtension
->Next
;
5188 /* Release the spinlock */
5189 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
5192 /**********************************************************************
5197 * Builds the registry device map of all device which are attached
5198 * to the given SCSI HBA port. The device map is located at:
5199 * \Registry\Machine\DeviceMap\Scsi
5209 * Name of registry driver service key.
5216 SpiBuildDeviceMap (PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
5217 PUNICODE_STRING RegistryPath
)
5219 PSCSI_PORT_LUN_EXTENSION LunExtension
;
5220 OBJECT_ATTRIBUTES ObjectAttributes
;
5221 UNICODE_STRING KeyName
;
5222 UNICODE_STRING ValueName
;
5223 WCHAR NameBuffer
[64];
5226 HANDLE ScsiPortKey
= NULL
;
5227 HANDLE ScsiBusKey
= NULL
;
5228 HANDLE ScsiInitiatorKey
= NULL
;
5229 HANDLE ScsiTargetKey
= NULL
;
5230 HANDLE ScsiLunKey
= NULL
;
5233 ULONG CurrentTarget
;
5240 DPRINT("SpiBuildDeviceMap() called\n");
5242 if (DeviceExtension
== NULL
|| RegistryPath
== NULL
)
5244 DPRINT1("Invalid parameter\n");
5245 return(STATUS_INVALID_PARAMETER
);
5248 /* Open or create the 'Scsi' subkey */
5249 RtlInitUnicodeString(&KeyName
,
5250 L
"\\Registry\\Machine\\Hardware\\DeviceMap\\Scsi");
5251 InitializeObjectAttributes(&ObjectAttributes
,
5253 OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
,
5256 Status
= ZwCreateKey(&ScsiKey
,
5261 REG_OPTION_VOLATILE
,
5263 if (!NT_SUCCESS(Status
))
5265 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5269 /* Create new 'Scsi Port X' subkey */
5270 DPRINT("Scsi Port %lu\n",
5271 DeviceExtension
->PortNumber
);
5273 swprintf(NameBuffer
,
5275 DeviceExtension
->PortNumber
);
5276 RtlInitUnicodeString(&KeyName
,
5278 InitializeObjectAttributes(&ObjectAttributes
,
5283 Status
= ZwCreateKey(&ScsiPortKey
,
5288 REG_OPTION_VOLATILE
,
5291 if (!NT_SUCCESS(Status
))
5293 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5298 * Create port-specific values
5301 /* Set 'DMA Enabled' (REG_DWORD) value */
5302 UlongData
= (ULONG
)!DeviceExtension
->PortCapabilities
.AdapterUsesPio
;
5303 DPRINT(" DMA Enabled = %s\n", (UlongData
) ? "TRUE" : "FALSE");
5304 RtlInitUnicodeString(&ValueName
,
5306 Status
= ZwSetValueKey(ScsiPortKey
,
5312 if (!NT_SUCCESS(Status
))
5314 DPRINT("ZwSetValueKey('DMA Enabled') failed (Status %lx)\n", Status
);
5315 ZwClose(ScsiPortKey
);
5319 /* Set 'Driver' (REG_SZ) value */
5320 DriverName
= wcsrchr(RegistryPath
->Buffer
, L
'\\') + 1;
5321 RtlInitUnicodeString(&ValueName
,
5323 Status
= ZwSetValueKey(ScsiPortKey
,
5328 (wcslen(DriverName
) + 1) * sizeof(WCHAR
));
5329 if (!NT_SUCCESS(Status
))
5331 DPRINT("ZwSetValueKey('Driver') failed (Status %lx)\n", Status
);
5332 ZwClose(ScsiPortKey
);
5336 /* Set 'Interrupt' (REG_DWORD) value (NT4 only) */
5337 UlongData
= (ULONG
)DeviceExtension
->PortConfig
->BusInterruptLevel
;
5338 DPRINT(" Interrupt = %lu\n", UlongData
);
5339 RtlInitUnicodeString(&ValueName
,
5341 Status
= ZwSetValueKey(ScsiPortKey
,
5347 if (!NT_SUCCESS(Status
))
5349 DPRINT("ZwSetValueKey('Interrupt') failed (Status %lx)\n", Status
);
5350 ZwClose(ScsiPortKey
);
5354 /* Set 'IOAddress' (REG_DWORD) value (NT4 only) */
5355 UlongData
= ScsiPortConvertPhysicalAddressToUlong((*DeviceExtension
->PortConfig
->AccessRanges
)[0].RangeStart
);
5356 DPRINT(" IOAddress = %lx\n", UlongData
);
5357 RtlInitUnicodeString(&ValueName
,
5359 Status
= ZwSetValueKey(ScsiPortKey
,
5365 if (!NT_SUCCESS(Status
))
5367 DPRINT("ZwSetValueKey('IOAddress') failed (Status %lx)\n", Status
);
5368 ZwClose(ScsiPortKey
);
5372 /* Enumerate buses */
5373 for (BusNumber
= 0; BusNumber
< DeviceExtension
->PortConfig
->NumberOfBuses
; BusNumber
++)
5375 /* Create 'Scsi Bus X' key */
5376 DPRINT(" Scsi Bus %lu\n", BusNumber
);
5377 swprintf(NameBuffer
,
5380 RtlInitUnicodeString(&KeyName
,
5382 InitializeObjectAttributes(&ObjectAttributes
,
5387 Status
= ZwCreateKey(&ScsiBusKey
,
5392 REG_OPTION_VOLATILE
,
5394 if (!NT_SUCCESS(Status
))
5396 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5400 /* Create 'Initiator Id X' key */
5401 DPRINT(" Initiator Id %u\n",
5402 DeviceExtension
->PortConfig
->InitiatorBusId
[BusNumber
]);
5403 swprintf(NameBuffer
,
5405 (unsigned int)(UCHAR
)DeviceExtension
->PortConfig
->InitiatorBusId
[BusNumber
]);
5406 RtlInitUnicodeString(&KeyName
,
5408 InitializeObjectAttributes(&ObjectAttributes
,
5413 Status
= ZwCreateKey(&ScsiInitiatorKey
,
5418 REG_OPTION_VOLATILE
,
5420 if (!NT_SUCCESS(Status
))
5422 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5426 /* FIXME: Are there any initiator values (??) */
5428 ZwClose(ScsiInitiatorKey
);
5429 ScsiInitiatorKey
= NULL
;
5432 /* Enumerate targets */
5433 CurrentTarget
= (ULONG
)-1;
5434 ScsiTargetKey
= NULL
;
5435 for (Target
= 0; Target
< DeviceExtension
->PortConfig
->MaximumNumberOfTargets
; Target
++)
5437 for (Lun
= 0; Lun
< SCSI_MAXIMUM_LOGICAL_UNITS
; Lun
++)
5439 LunExtension
= SpiGetLunExtension(DeviceExtension
,
5443 if (LunExtension
!= NULL
)
5445 if (Target
!= CurrentTarget
)
5447 /* Close old target key */
5448 if (ScsiTargetKey
!= NULL
)
5450 ZwClose(ScsiTargetKey
);
5451 ScsiTargetKey
= NULL
;
5454 /* Create 'Target Id X' key */
5455 DPRINT(" Target Id %lu\n", Target
);
5456 swprintf(NameBuffer
,
5459 RtlInitUnicodeString(&KeyName
,
5461 InitializeObjectAttributes(&ObjectAttributes
,
5466 Status
= ZwCreateKey(&ScsiTargetKey
,
5471 REG_OPTION_VOLATILE
,
5473 if (!NT_SUCCESS(Status
))
5475 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5479 CurrentTarget
= Target
;
5482 /* Create 'Logical Unit Id X' key */
5483 DPRINT(" Logical Unit Id %lu\n", Lun
);
5484 swprintf(NameBuffer
,
5485 L
"Logical Unit Id %lu",
5487 RtlInitUnicodeString(&KeyName
,
5489 InitializeObjectAttributes(&ObjectAttributes
,
5494 Status
= ZwCreateKey(&ScsiLunKey
,
5499 REG_OPTION_VOLATILE
,
5501 if (!NT_SUCCESS(Status
))
5503 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5507 /* Set 'Identifier' (REG_SZ) value */
5508 swprintf(NameBuffer
,
5510 LunExtension
->InquiryData
.VendorId
,
5511 LunExtension
->InquiryData
.ProductId
,
5512 LunExtension
->InquiryData
.ProductRevisionLevel
);
5513 DPRINT(" Identifier = '%S'\n", NameBuffer
);
5514 RtlInitUnicodeString(&ValueName
,
5516 Status
= ZwSetValueKey(ScsiLunKey
,
5521 (wcslen(NameBuffer
) + 1) * sizeof(WCHAR
));
5522 if (!NT_SUCCESS(Status
))
5524 DPRINT("ZwSetValueKey('Identifier') failed (Status %lx)\n", Status
);
5528 /* Set 'Type' (REG_SZ) value */
5529 switch (LunExtension
->InquiryData
.DeviceType
)
5532 TypeName
= L
"DiskPeripheral";
5535 TypeName
= L
"TapePeripheral";
5538 TypeName
= L
"PrinterPeripheral";
5541 TypeName
= L
"WormPeripheral";
5544 TypeName
= L
"CdRomPeripheral";
5547 TypeName
= L
"ScannerPeripheral";
5550 TypeName
= L
"OpticalDiskPeripheral";
5553 TypeName
= L
"MediumChangerPeripheral";
5556 TypeName
= L
"CommunicationPeripheral";
5559 TypeName
= L
"OtherPeripheral";
5562 DPRINT(" Type = '%S'\n", TypeName
);
5563 RtlInitUnicodeString(&ValueName
,
5565 Status
= ZwSetValueKey(ScsiLunKey
,
5570 (wcslen(TypeName
) + 1) * sizeof(WCHAR
));
5571 if (!NT_SUCCESS(Status
))
5573 DPRINT("ZwSetValueKey('Type') failed (Status %lx)\n", Status
);
5577 ZwClose(ScsiLunKey
);
5582 /* Close old target key */
5583 if (ScsiTargetKey
!= NULL
)
5585 ZwClose(ScsiTargetKey
);
5586 ScsiTargetKey
= NULL
;
5590 ZwClose(ScsiBusKey
);
5595 if (ScsiLunKey
!= NULL
)
5596 ZwClose (ScsiLunKey
);
5598 if (ScsiInitiatorKey
!= NULL
)
5599 ZwClose (ScsiInitiatorKey
);
5601 if (ScsiTargetKey
!= NULL
)
5602 ZwClose (ScsiTargetKey
);
5604 if (ScsiBusKey
!= NULL
)
5605 ZwClose (ScsiBusKey
);
5607 if (ScsiPortKey
!= NULL
)
5608 ZwClose (ScsiPortKey
);
5610 DPRINT("SpiBuildDeviceMap() done\n");
5617 SpiMiniportTimerDpc(IN
struct _KDPC
*Dpc
,
5618 IN PVOID DeviceObject
,
5619 IN PVOID SystemArgument1
,
5620 IN PVOID SystemArgument2
)
5622 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
5624 DPRINT("Miniport timer DPC\n");
5626 DeviceExtension
= ((PDEVICE_OBJECT
)DeviceObject
)->DeviceExtension
;
5628 /* Acquire the spinlock */
5629 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
5631 /* Call the timer routine */
5632 if (DeviceExtension
->HwScsiTimer
!= NULL
)
5634 DeviceExtension
->HwScsiTimer(&DeviceExtension
->MiniPortDeviceExtension
);
5637 /* Release the spinlock */
5638 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
5640 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
5642 ScsiPortDpcForIsr(NULL
,
5643 DeviceExtension
->DeviceObject
,
5650 SpiCreatePortConfig(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
5651 PHW_INITIALIZATION_DATA HwInitData
,
5652 PCONFIGURATION_INFO InternalConfigInfo
,
5653 PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
5656 UNICODE_STRING UnicodeString
;
5657 OBJECT_ATTRIBUTES ObjectAttributes
;
5658 PCONFIGURATION_INFORMATION DdkConfigInformation
;
5659 HANDLE RootKey
, Key
;
5661 WCHAR DeviceBuffer
[16];
5662 WCHAR StrBuffer
[512];
5666 /* Zero out the struct if told so */
5669 /* First zero the portconfig */
5670 RtlZeroMemory(ConfigInfo
, sizeof(PORT_CONFIGURATION_INFORMATION
));
5672 /* Then access ranges */
5673 RtlZeroMemory(InternalConfigInfo
->AccessRanges
,
5674 HwInitData
->NumberOfAccessRanges
* sizeof(ACCESS_RANGE
));
5676 /* Initialize the struct */
5677 ConfigInfo
->Length
= sizeof(PORT_CONFIGURATION_INFORMATION
);
5678 ConfigInfo
->AdapterInterfaceType
= HwInitData
->AdapterInterfaceType
;
5679 ConfigInfo
->InterruptMode
= Latched
;
5680 ConfigInfo
->DmaChannel
= SP_UNINITIALIZED_VALUE
;
5681 ConfigInfo
->DmaPort
= SP_UNINITIALIZED_VALUE
;
5682 ConfigInfo
->MaximumTransferLength
= SP_UNINITIALIZED_VALUE
;
5683 ConfigInfo
->NumberOfAccessRanges
= HwInitData
->NumberOfAccessRanges
;
5684 ConfigInfo
->MaximumNumberOfTargets
= 8;
5686 /* Store parameters */
5687 ConfigInfo
->NeedPhysicalAddresses
= HwInitData
->NeedPhysicalAddresses
;
5688 ConfigInfo
->MapBuffers
= HwInitData
->MapBuffers
;
5689 ConfigInfo
->AutoRequestSense
= HwInitData
->AutoRequestSense
;
5690 ConfigInfo
->ReceiveEvent
= HwInitData
->ReceiveEvent
;
5691 ConfigInfo
->TaggedQueuing
= HwInitData
->TaggedQueuing
;
5692 ConfigInfo
->MultipleRequestPerLu
= HwInitData
->MultipleRequestPerLu
;
5694 /* Get the disk usage */
5695 DdkConfigInformation
= IoGetConfigurationInformation();
5696 ConfigInfo
->AtdiskPrimaryClaimed
= DdkConfigInformation
->AtDiskPrimaryAddressClaimed
;
5697 ConfigInfo
->AtdiskSecondaryClaimed
= DdkConfigInformation
->AtDiskSecondaryAddressClaimed
;
5699 /* Initiator bus id is not set */
5700 for (Bus
= 0; Bus
< 8; Bus
++)
5701 ConfigInfo
->InitiatorBusId
[Bus
] = (CCHAR
)SP_UNINITIALIZED_VALUE
;
5704 ConfigInfo
->NumberOfPhysicalBreaks
= 17;
5706 /* Clear this information */
5707 InternalConfigInfo
->DisableTaggedQueueing
= FALSE
;
5708 InternalConfigInfo
->DisableMultipleLun
= FALSE
;
5710 /* Store Bus Number */
5711 ConfigInfo
->SystemIoBusNumber
= InternalConfigInfo
->BusNumber
;
5715 if (ConfigInfo
->AdapterInterfaceType
== Internal
)
5717 /* Open registry key for HW database */
5718 InitializeObjectAttributes(&ObjectAttributes
,
5719 DeviceExtension
->DeviceObject
->DriverObject
->HardwareDatabase
,
5720 OBJ_CASE_INSENSITIVE
,
5724 Status
= ZwOpenKey(&RootKey
,
5728 if (NT_SUCCESS(Status
))
5730 /* Create name for it */
5731 swprintf(StrBuffer
, L
"ScsiAdapter\\%lu",
5732 InternalConfigInfo
->AdapterNumber
);
5734 RtlInitUnicodeString(&UnicodeString
, StrBuffer
);
5736 /* Open device key */
5737 InitializeObjectAttributes(&ObjectAttributes
,
5739 OBJ_CASE_INSENSITIVE
,
5743 Status
= ZwOpenKey(&Key
,
5749 if (NT_SUCCESS(Status
))
5751 if (InternalConfigInfo
->LastAdapterNumber
!= InternalConfigInfo
->AdapterNumber
)
5753 DPRINT("Hardware info found at %S\n", StrBuffer
);
5756 SpiParseDeviceInfo(DeviceExtension
,
5762 InternalConfigInfo
->BusNumber
= 0;
5766 /* Try the next adapter */
5767 InternalConfigInfo
->AdapterNumber
++;
5773 /* Info was not found, exit */
5774 DPRINT1("ZwOpenKey() failed with Status=0x%08X\n", Status
);
5775 return STATUS_DEVICE_DOES_NOT_EXIST
;
5780 DPRINT1("ZwOpenKey() failed with Status=0x%08X\n", Status
);
5784 /* Look at device params */
5786 if (InternalConfigInfo
->Parameter
)
5788 ExFreePool(InternalConfigInfo
->Parameter
);
5789 InternalConfigInfo
->Parameter
= NULL
;
5792 if (InternalConfigInfo
->ServiceKey
!= NULL
)
5794 swprintf(DeviceBuffer
, L
"Device%lu", InternalConfigInfo
->AdapterNumber
);
5795 RtlInitUnicodeString(&UnicodeString
, DeviceBuffer
);
5797 /* Open the service key */
5798 InitializeObjectAttributes(&ObjectAttributes
,
5800 OBJ_CASE_INSENSITIVE
,
5801 InternalConfigInfo
->ServiceKey
,
5804 Status
= ZwOpenKey(&Key
,
5809 /* Parse device key */
5810 if (InternalConfigInfo
->DeviceKey
!= NULL
)
5812 SpiParseDeviceInfo(DeviceExtension
,
5813 InternalConfigInfo
->DeviceKey
,
5819 /* Then parse hw info */
5822 if (InternalConfigInfo
->LastAdapterNumber
!= InternalConfigInfo
->AdapterNumber
)
5824 SpiParseDeviceInfo(DeviceExtension
,
5835 /* Adapter not found, go try the next one */
5836 InternalConfigInfo
->AdapterNumber
++;
5845 /* Update the last adapter number */
5846 InternalConfigInfo
->LastAdapterNumber
= InternalConfigInfo
->AdapterNumber
;
5848 /* Do we have this kind of bus at all? */
5850 Status
= IoQueryDeviceDescription(&HwInitData
->AdapterInterfaceType
,
5851 &InternalConfigInfo
->BusNumber
,
5856 SpQueryDeviceCallout
,
5859 /* This bus was not found */
5862 INTERFACE_TYPE InterfaceType
= Eisa
;
5864 /* Check for EISA */
5865 if (HwInitData
->AdapterInterfaceType
== Isa
)
5867 Status
= IoQueryDeviceDescription(&InterfaceType
,
5868 &InternalConfigInfo
->BusNumber
,
5873 SpQueryDeviceCallout
,
5876 /* Return respectively */
5878 return STATUS_SUCCESS
;
5880 return STATUS_DEVICE_DOES_NOT_EXIST
;
5884 return STATUS_DEVICE_DOES_NOT_EXIST
;
5889 return STATUS_SUCCESS
;
5894 SpiParseDeviceInfo(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
5896 IN PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
5897 IN PCONFIGURATION_INFO InternalConfigInfo
,
5900 PKEY_VALUE_FULL_INFORMATION KeyValueInformation
;
5901 PCM_FULL_RESOURCE_DESCRIPTOR FullResource
;
5902 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
5903 PCM_SCSI_DEVICE_DATA ScsiDeviceData
;
5904 ULONG Length
, Count
;
5905 ULONG Index
= 0, RangeCount
= 0;
5906 UNICODE_STRING UnicodeString
;
5907 ANSI_STRING AnsiString
;
5908 NTSTATUS Status
= STATUS_SUCCESS
;
5910 KeyValueInformation
= (PKEY_VALUE_FULL_INFORMATION
) Buffer
;
5912 /* Loop through all values in the device node */
5915 Status
= ZwEnumerateValueKey(Key
,
5917 KeyValueFullInformation
,
5922 if (!NT_SUCCESS(Status
))
5927 /* Length for DWORD is ok? */
5928 if (KeyValueInformation
->Type
== REG_DWORD
&&
5929 KeyValueInformation
->DataLength
!= sizeof(ULONG
))
5934 /* Get MaximumLogicalUnit */
5935 if (_wcsnicmp(KeyValueInformation
->Name
, L
"MaximumLogicalUnit",
5936 KeyValueInformation
->NameLength
/2) == 0)
5939 if (KeyValueInformation
->Type
!= REG_DWORD
)
5941 DPRINT("Bad data type for MaximumLogicalUnit\n");
5945 DeviceExtension
->MaxLunCount
= *((PUCHAR
)
5946 (Buffer
+ KeyValueInformation
->DataOffset
));
5948 /* Check / reset if needed */
5949 if (DeviceExtension
->MaxLunCount
> SCSI_MAXIMUM_LOGICAL_UNITS
)
5950 DeviceExtension
->MaxLunCount
= SCSI_MAXIMUM_LOGICAL_UNITS
;
5952 DPRINT("MaximumLogicalUnit = %d\n", DeviceExtension
->MaxLunCount
);
5955 /* Get InitiatorTargetId */
5956 if (_wcsnicmp(KeyValueInformation
->Name
, L
"InitiatorTargetId",
5957 KeyValueInformation
->NameLength
/ 2) == 0)
5960 if (KeyValueInformation
->Type
!= REG_DWORD
)
5962 DPRINT("Bad data type for InitiatorTargetId\n");
5966 ConfigInfo
->InitiatorBusId
[0] = *((PUCHAR
)
5967 (Buffer
+ KeyValueInformation
->DataOffset
));
5969 /* Check / reset if needed */
5970 if (ConfigInfo
->InitiatorBusId
[0] > ConfigInfo
->MaximumNumberOfTargets
- 1)
5971 ConfigInfo
->InitiatorBusId
[0] = (CCHAR
)-1;
5973 DPRINT("InitiatorTargetId = %d\n", ConfigInfo
->InitiatorBusId
[0]);
5977 if (_wcsnicmp(KeyValueInformation
->Name
, L
"ScsiDebug",
5978 KeyValueInformation
->NameLength
/2) == 0)
5980 DPRINT("ScsiDebug key not supported\n");
5983 /* Check for a breakpoint */
5984 if (_wcsnicmp(KeyValueInformation
->Name
, L
"BreakPointOnEntry",
5985 KeyValueInformation
->NameLength
/2) == 0)
5987 DPRINT1("Breakpoint on entry requested!\n");
5991 /* Get DisableSynchronousTransfers */
5992 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DisableSynchronousTransfers",
5993 KeyValueInformation
->NameLength
/2) == 0)
5995 DeviceExtension
->SrbFlags
|= SRB_FLAGS_DISABLE_SYNCH_TRANSFER
;
5996 DPRINT("Synch transfers disabled\n");
5999 /* Get DisableDisconnects */
6000 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DisableDisconnects",
6001 KeyValueInformation
->NameLength
/2) == 0)
6003 DeviceExtension
->SrbFlags
|= SRB_FLAGS_DISABLE_DISCONNECT
;
6004 DPRINT("Disconnects disabled\n");
6007 /* Get DisableTaggedQueuing */
6008 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DisableTaggedQueuing",
6009 KeyValueInformation
->NameLength
/2) == 0)
6011 InternalConfigInfo
->DisableTaggedQueueing
= TRUE
;
6012 DPRINT("Tagged queueing disabled\n");
6015 /* Get DisableMultipleRequests */
6016 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DisableMultipleRequests",
6017 KeyValueInformation
->NameLength
/2) == 0)
6019 InternalConfigInfo
->DisableMultipleLun
= TRUE
;
6020 DPRINT("Multiple requests disabled\n");
6023 /* Get DriverParameters */
6024 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DriverParameters",
6025 KeyValueInformation
->NameLength
/2) == 0)
6027 /* Skip if nothing */
6028 if (KeyValueInformation
->DataLength
== 0)
6031 /* If there was something previously allocated - free it */
6032 if (InternalConfigInfo
->Parameter
!= NULL
)
6033 ExFreePool(InternalConfigInfo
->Parameter
);
6036 InternalConfigInfo
->Parameter
= ExAllocatePoolWithTag(NonPagedPool
,
6037 KeyValueInformation
->DataLength
, TAG_SCSIPORT
);
6039 if (InternalConfigInfo
->Parameter
!= NULL
)
6041 if (KeyValueInformation
->Type
!= REG_SZ
)
6045 InternalConfigInfo
->Parameter
,
6046 (PCCHAR
)KeyValueInformation
+ KeyValueInformation
->DataOffset
,
6047 KeyValueInformation
->DataLength
);
6051 /* If it's a unicode string, convert it to ansi */
6052 UnicodeString
.Length
= (USHORT
)KeyValueInformation
->DataLength
;
6053 UnicodeString
.MaximumLength
= (USHORT
)KeyValueInformation
->DataLength
;
6054 UnicodeString
.Buffer
=
6055 (PWSTR
)((PCCHAR
)KeyValueInformation
+ KeyValueInformation
->DataOffset
);
6057 AnsiString
.Length
= 0;
6058 AnsiString
.MaximumLength
= (USHORT
)KeyValueInformation
->DataLength
;
6059 AnsiString
.Buffer
= (PCHAR
)InternalConfigInfo
->Parameter
;
6061 Status
= RtlUnicodeStringToAnsiString(&AnsiString
,
6065 /* In case of error, free the allocated space */
6066 if (!NT_SUCCESS(Status
))
6068 ExFreePool(InternalConfigInfo
->Parameter
);
6069 InternalConfigInfo
->Parameter
= NULL
;
6075 DPRINT("Found driver parameter\n");
6078 /* Get MaximumSGList */
6079 if (_wcsnicmp(KeyValueInformation
->Name
, L
"MaximumSGList",
6080 KeyValueInformation
->NameLength
/2) == 0)
6082 if (KeyValueInformation
->Type
!= REG_DWORD
)
6084 DPRINT("Bad data type for MaximumSGList\n");
6088 ConfigInfo
->NumberOfPhysicalBreaks
= *((PUCHAR
)(Buffer
+ KeyValueInformation
->DataOffset
));
6091 if (ConfigInfo
->NumberOfPhysicalBreaks
> SCSI_MAXIMUM_PHYSICAL_BREAKS
)
6093 ConfigInfo
->NumberOfPhysicalBreaks
= SCSI_MAXIMUM_PHYSICAL_BREAKS
;
6095 else if (ConfigInfo
->NumberOfPhysicalBreaks
< SCSI_MINIMUM_PHYSICAL_BREAKS
)
6097 ConfigInfo
->NumberOfPhysicalBreaks
= SCSI_MINIMUM_PHYSICAL_BREAKS
;
6100 DPRINT("MaximumSGList = %d\n", ConfigInfo
->NumberOfPhysicalBreaks
);
6103 /* Get NumberOfRequests */
6104 if (_wcsnicmp(KeyValueInformation
->Name
, L
"NumberOfRequests",
6105 KeyValueInformation
->NameLength
/2) == 0)
6107 if (KeyValueInformation
->Type
!= REG_DWORD
)
6109 DPRINT("NumberOfRequests has wrong data type\n");
6113 DeviceExtension
->RequestsNumber
= *((PUCHAR
)(Buffer
+ KeyValueInformation
->DataOffset
));
6116 if (DeviceExtension
->RequestsNumber
< 16)
6118 DeviceExtension
->RequestsNumber
= 16;
6120 else if (DeviceExtension
->RequestsNumber
> 512)
6122 DeviceExtension
->RequestsNumber
= 512;
6125 DPRINT("Number Of Requests = %d\n", DeviceExtension
->RequestsNumber
);
6128 /* Get resource list */
6129 if (_wcsnicmp(KeyValueInformation
->Name
, L
"ResourceList",
6130 KeyValueInformation
->NameLength
/2) == 0 ||
6131 _wcsnicmp(KeyValueInformation
->Name
, L
"Configuration Data",
6132 KeyValueInformation
->NameLength
/2) == 0 )
6134 if (KeyValueInformation
->Type
!= REG_FULL_RESOURCE_DESCRIPTOR
||
6135 KeyValueInformation
->DataLength
< sizeof(REG_FULL_RESOURCE_DESCRIPTOR
))
6137 DPRINT("Bad data type for ResourceList\n");
6142 DPRINT("Found ResourceList\n");
6145 FullResource
= (PCM_FULL_RESOURCE_DESCRIPTOR
)(Buffer
+ KeyValueInformation
->DataOffset
);
6147 /* Copy some info from it */
6148 InternalConfigInfo
->BusNumber
= FullResource
->BusNumber
;
6149 ConfigInfo
->SystemIoBusNumber
= FullResource
->BusNumber
;
6151 /* Loop through it */
6152 for (Count
= 0; Count
< FullResource
->PartialResourceList
.Count
; Count
++)
6154 /* Get partial descriptor */
6156 &FullResource
->PartialResourceList
.PartialDescriptors
[Count
];
6158 /* Check datalength */
6159 if ((ULONG
)((PCHAR
)(PartialDescriptor
+ 1) -
6160 (PCHAR
)FullResource
) > KeyValueInformation
->DataLength
)
6162 DPRINT("Resource data is of incorrect size\n");
6166 switch (PartialDescriptor
->Type
)
6168 case CmResourceTypePort
:
6169 if (RangeCount
>= ConfigInfo
->NumberOfAccessRanges
)
6171 DPRINT("Too many access ranges\n");
6175 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeInMemory
= FALSE
;
6176 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeStart
= PartialDescriptor
->u
.Port
.Start
;
6177 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeLength
= PartialDescriptor
->u
.Port
.Length
;
6182 case CmResourceTypeMemory
:
6183 if (RangeCount
>= ConfigInfo
->NumberOfAccessRanges
)
6185 DPRINT("Too many access ranges\n");
6189 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeInMemory
= TRUE
;
6190 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeStart
= PartialDescriptor
->u
.Memory
.Start
;
6191 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeLength
= PartialDescriptor
->u
.Memory
.Length
;
6196 case CmResourceTypeInterrupt
:
6197 ConfigInfo
->BusInterruptLevel
=
6198 PartialDescriptor
->u
.Interrupt
.Level
;
6200 ConfigInfo
->BusInterruptVector
=
6201 PartialDescriptor
->u
.Interrupt
.Vector
;
6204 case CmResourceTypeDma
:
6205 ConfigInfo
->DmaChannel
= PartialDescriptor
->u
.Dma
.Channel
;
6206 ConfigInfo
->DmaPort
= PartialDescriptor
->u
.Dma
.Port
;
6209 case CmResourceTypeDeviceSpecific
:
6210 if (PartialDescriptor
->u
.DeviceSpecificData
.DataSize
<
6211 sizeof(CM_SCSI_DEVICE_DATA
) ||
6212 (PCHAR
) (PartialDescriptor
+ 1) - (PCHAR
)FullResource
+
6213 PartialDescriptor
->u
.DeviceSpecificData
.DataSize
>
6214 KeyValueInformation
->DataLength
)
6216 DPRINT("Resource data length is incorrect");
6220 /* Set only one field from it */
6221 ScsiDeviceData
= (PCM_SCSI_DEVICE_DATA
) (PartialDescriptor
+1);
6222 ConfigInfo
->InitiatorBusId
[0] = ScsiDeviceData
->HostIdentifier
;
6232 SpQueryDeviceCallout(IN PVOID Context
,
6233 IN PUNICODE_STRING PathName
,
6234 IN INTERFACE_TYPE BusType
,
6236 IN PKEY_VALUE_FULL_INFORMATION
*BusInformation
,
6237 IN CONFIGURATION_TYPE ControllerType
,
6238 IN ULONG ControllerNumber
,
6239 IN PKEY_VALUE_FULL_INFORMATION
*ControllerInformation
,
6240 IN CONFIGURATION_TYPE PeripheralType
,
6241 IN ULONG PeripheralNumber
,
6242 IN PKEY_VALUE_FULL_INFORMATION
*PeripheralInformation
)
6244 PBOOLEAN Found
= (PBOOLEAN
)Context
;
6245 /* We just set our Found variable to TRUE */
6248 return STATUS_SUCCESS
;
6251 IO_ALLOCATION_ACTION
6253 ScsiPortAllocateAdapterChannel(IN PDEVICE_OBJECT DeviceObject
,
6255 IN PVOID MapRegisterBase
,
6259 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
= DeviceObject
->DeviceExtension
;
6261 /* Guard access with the spinlock */
6262 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
6264 /* Save MapRegisterBase we've got here */
6265 DeviceExtension
->MapRegisterBase
= MapRegisterBase
;
6267 /* Start pending request */
6268 KeSynchronizeExecution(DeviceExtension
->Interrupt
,
6269 ScsiPortStartPacket
, DeviceObject
);
6271 /* Release spinlock we took */
6272 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
6279 SpiStatusSrbToNt(UCHAR SrbStatus
)
6281 switch (SRB_STATUS(SrbStatus
))
6283 case SRB_STATUS_TIMEOUT
:
6284 case SRB_STATUS_COMMAND_TIMEOUT
:
6285 return STATUS_IO_TIMEOUT
;
6287 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH
:
6288 case SRB_STATUS_BAD_FUNCTION
:
6289 return STATUS_INVALID_DEVICE_REQUEST
;
6291 case SRB_STATUS_NO_DEVICE
:
6292 case SRB_STATUS_INVALID_LUN
:
6293 case SRB_STATUS_INVALID_TARGET_ID
:
6294 case SRB_STATUS_NO_HBA
:
6295 return STATUS_DEVICE_DOES_NOT_EXIST
;
6297 case SRB_STATUS_DATA_OVERRUN
:
6298 return STATUS_BUFFER_OVERFLOW
;
6300 case SRB_STATUS_SELECTION_TIMEOUT
:
6301 return STATUS_DEVICE_NOT_CONNECTED
;
6304 return STATUS_IO_DEVICE_ERROR
;
6307 return STATUS_IO_DEVICE_ERROR
;
6311 #undef ScsiPortConvertPhysicalAddressToUlong
6316 ScsiPortConvertPhysicalAddressToUlong(IN SCSI_PHYSICAL_ADDRESS Address
)
6318 DPRINT("ScsiPortConvertPhysicalAddressToUlong()\n");
6319 return(Address
.u
.LowPart
);