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 *****************************************************************/
37 ULONG InternalDebugLevel
= 0x00;
39 #undef ScsiPortMoveMemory
41 /* TYPES *********************************************************************/
43 /* GLOBALS *******************************************************************/
46 SpiGetPciConfigData(IN PDRIVER_OBJECT DriverObject
,
47 IN PDEVICE_OBJECT DeviceObject
,
48 IN
struct _HW_INITIALIZATION_DATA
*HwInitializationData
,
49 IN OUT PPORT_CONFIGURATION_INFORMATION PortConfig
,
50 IN PUNICODE_STRING RegistryPath
,
52 IN OUT PPCI_SLOT_NUMBER NextSlotNumber
);
55 ScsiPortCreateClose(IN PDEVICE_OBJECT DeviceObject
,
58 static DRIVER_DISPATCH ScsiPortDispatchScsi
;
60 ScsiPortDispatchScsi(IN PDEVICE_OBJECT DeviceObject
,
64 ScsiPortDeviceControl(IN PDEVICE_OBJECT DeviceObject
,
67 static DRIVER_STARTIO ScsiPortStartIo
;
69 ScsiPortStartIo(IN PDEVICE_OBJECT DeviceObject
,
73 ScsiPortStartPacket(IN OUT PVOID Context
);
77 SpiAdapterControl(PDEVICE_OBJECT DeviceObject
, PIRP Irp
,
78 PVOID MapRegisterBase
, PVOID Context
);
80 static PSCSI_PORT_LUN_EXTENSION
81 SpiAllocateLunExtension (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
);
83 static PSCSI_PORT_LUN_EXTENSION
84 SpiGetLunExtension (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
89 static PSCSI_REQUEST_BLOCK_INFO
90 SpiAllocateSrbStructures(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
91 PSCSI_PORT_LUN_EXTENSION LunExtension
,
92 PSCSI_REQUEST_BLOCK Srb
);
95 SpiSendInquiry (IN PDEVICE_OBJECT DeviceObject
,
96 IN PSCSI_LUN_INFO LunInfo
);
99 SpiScanAdapter (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
);
102 SpiGetInquiryData (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
105 static PSCSI_REQUEST_BLOCK_INFO
106 SpiGetSrbData(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
113 ScsiPortIsr(IN PKINTERRUPT Interrupt
,
114 IN PVOID ServiceContext
);
117 ScsiPortDpcForIsr(IN PKDPC Dpc
,
118 IN PDEVICE_OBJECT DpcDeviceObject
,
120 IN PVOID DpcContext
);
123 ScsiPortIoTimer(PDEVICE_OBJECT DeviceObject
,
128 ScsiPortAllocateAdapterChannel(IN PDEVICE_OBJECT DeviceObject
,
130 IN PVOID MapRegisterBase
,
134 SpiBuildDeviceMap (PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
135 PUNICODE_STRING RegistryPath
);
138 SpiStatusSrbToNt(UCHAR SrbStatus
);
141 SpiSendRequestSense(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
142 IN PSCSI_REQUEST_BLOCK Srb
);
144 static IO_COMPLETION_ROUTINE SpiCompletionRoutine
;
146 SpiCompletionRoutine(PDEVICE_OBJECT DeviceObject
,
152 SpiProcessCompletedRequest(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
153 IN PSCSI_REQUEST_BLOCK_INFO SrbInfo
,
154 OUT PBOOLEAN NeedToCallStartIo
);
157 SpiGetNextRequestFromLun(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
158 IN PSCSI_PORT_LUN_EXTENSION LunExtension
);
161 SpiMiniportTimerDpc(IN
struct _KDPC
*Dpc
,
162 IN PVOID DeviceObject
,
163 IN PVOID SystemArgument1
,
164 IN PVOID SystemArgument2
);
167 SpiCreatePortConfig(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
168 PHW_INITIALIZATION_DATA HwInitData
,
169 PCONFIGURATION_INFO InternalConfigInfo
,
170 PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
174 SpQueryDeviceCallout(IN PVOID Context
,
175 IN PUNICODE_STRING PathName
,
176 IN INTERFACE_TYPE BusType
,
178 IN PKEY_VALUE_FULL_INFORMATION
*BusInformation
,
179 IN CONFIGURATION_TYPE ControllerType
,
180 IN ULONG ControllerNumber
,
181 IN PKEY_VALUE_FULL_INFORMATION
*ControllerInformation
,
182 IN CONFIGURATION_TYPE PeripheralType
,
183 IN ULONG PeripheralNumber
,
184 IN PKEY_VALUE_FULL_INFORMATION
*PeripheralInformation
);
187 SpiParseDeviceInfo(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
189 IN PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
190 IN PCONFIGURATION_INFO InternalConfigInfo
,
194 SpiResourceToConfig(IN PHW_INITIALIZATION_DATA HwInitializationData
,
195 IN PCM_FULL_RESOURCE_DESCRIPTOR ResourceDescriptor
,
196 IN PPORT_CONFIGURATION_INFORMATION PortConfig
);
198 static PCM_RESOURCE_LIST
199 SpiConfigToResource(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
200 PPORT_CONFIGURATION_INFORMATION PortConfig
);
203 SpiCleanupAfterInit(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
);
206 SpiHandleAttachRelease(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
210 SpiAllocateCommonBuffer(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
, ULONG NonCachedSize
);
212 NTHALAPI ULONG NTAPI
HalGetBusData(BUS_DATA_TYPE
, ULONG
, ULONG
, PVOID
, ULONG
);
213 NTHALAPI ULONG NTAPI
HalGetInterruptVector(INTERFACE_TYPE
, ULONG
, ULONG
, ULONG
, PKIRQL
, PKAFFINITY
);
214 NTHALAPI NTSTATUS NTAPI
HalAssignSlotResources(PUNICODE_STRING
, PUNICODE_STRING
, PDRIVER_OBJECT
, PDEVICE_OBJECT
, INTERFACE_TYPE
, ULONG
, ULONG
, PCM_RESOURCE_LIST
*);
216 /* FUNCTIONS *****************************************************************/
218 /**********************************************************************
223 * This function initializes the driver.
230 * System allocated Driver Object for this driver.
233 * Name of registry driver service key.
240 DriverEntry(IN PDRIVER_OBJECT DriverObject
,
241 IN PUNICODE_STRING RegistryPath
)
243 DPRINT("ScsiPort Driver %s\n", VERSION
);
244 return(STATUS_SUCCESS
);
248 /**********************************************************************
253 * Prints debugging messages.
260 * Debug level of the given message.
263 * Pointer to printf()-compatible format string.
266 Additional output data (see printf()).
275 ScsiDebugPrint(IN ULONG DebugPrintLevel
,
276 IN PCHAR DebugMessage
,
282 if (DebugPrintLevel
> InternalDebugLevel
)
285 va_start(ap
, DebugMessage
);
286 vsprintf(Buffer
, DebugMessage
, ap
);
292 /* An internal helper function for ScsiPortCompleteRequest */
295 SpiCompleteRequest(IN PVOID HwDeviceExtension
,
296 IN PSCSI_REQUEST_BLOCK_INFO SrbInfo
,
299 PSCSI_REQUEST_BLOCK Srb
;
301 /* Get current SRB */
304 /* Return if there is no SRB or it is not active */
305 if (!Srb
|| !(Srb
->SrbFlags
& SRB_FLAGS_IS_ACTIVE
)) return;
308 Srb
->SrbStatus
= SrbStatus
;
310 /* Set data transfered to 0 */
311 Srb
->DataTransferLength
= 0;
314 ScsiPortNotification(RequestComplete
,
323 ScsiPortCompleteRequest(IN PVOID HwDeviceExtension
,
329 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
330 PSCSI_PORT_LUN_EXTENSION LunExtension
;
331 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
332 PLIST_ENTRY ListEntry
;
336 DPRINT("ScsiPortCompleteRequest() called\n");
338 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
339 SCSI_PORT_DEVICE_EXTENSION
,
340 MiniPortDeviceExtension
);
342 /* Go through all buses */
343 for (BusNumber
= 0; BusNumber
< 8; BusNumber
++)
345 /* Go through all targets */
346 for (Target
= 0; Target
< DeviceExtension
->MaxTargedIds
; Target
++)
348 /* Get logical unit list head */
349 LunExtension
= DeviceExtension
->LunExtensionList
[Target
% 8];
351 /* Go through all logical units */
354 /* Now match what caller asked with what we are at now */
355 if ((PathId
== SP_UNTAGGED
|| PathId
== LunExtension
->PathId
) &&
356 (TargetId
== SP_UNTAGGED
|| TargetId
== LunExtension
->TargetId
) &&
357 (Lun
== SP_UNTAGGED
|| Lun
== LunExtension
->Lun
))
359 /* Yes, that's what caller asked for. Complete abort requests */
360 if (LunExtension
->CompletedAbortRequests
)
362 /* TODO: Save SrbStatus in this request */
363 DPRINT1("Completing abort request without setting SrbStatus!\n");
365 /* Issue a notification request */
366 ScsiPortNotification(RequestComplete
,
368 LunExtension
->CompletedAbortRequests
);
371 /* Complete the request using our helper */
372 SpiCompleteRequest(HwDeviceExtension
,
373 &LunExtension
->SrbInfo
,
376 /* Go through the queue and complete everything there too */
377 ListEntry
= LunExtension
->SrbInfo
.Requests
.Flink
;
378 while (ListEntry
!= &LunExtension
->SrbInfo
.Requests
)
380 /* Get the actual SRB info entry */
381 SrbInfo
= CONTAINING_RECORD(ListEntry
,
382 SCSI_REQUEST_BLOCK_INFO
,
386 SpiCompleteRequest(HwDeviceExtension
,
390 /* Advance to the next request in queue */
391 ListEntry
= SrbInfo
->Requests
.Flink
;
395 /* Advance to the next one */
396 LunExtension
= LunExtension
->Next
;
406 ScsiPortFlushDma(IN PVOID HwDeviceExtension
)
408 DPRINT("ScsiPortFlushDma()\n");
417 ScsiPortFreeDeviceBase(IN PVOID HwDeviceExtension
,
418 IN PVOID MappedAddress
)
420 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
421 PMAPPED_ADDRESS NextMa
, LastMa
;
423 //DPRINT("ScsiPortFreeDeviceBase() called\n");
425 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
426 SCSI_PORT_DEVICE_EXTENSION
,
427 MiniPortDeviceExtension
);
429 /* Initialize our pointers */
430 NextMa
= DeviceExtension
->MappedAddressList
;
435 if (NextMa
->MappedAddress
== MappedAddress
)
438 MmUnmapIoSpace(MappedAddress
, NextMa
->NumberOfBytes
);
440 /* Remove it from the list */
441 if (NextMa
== DeviceExtension
->MappedAddressList
)
443 /* Remove the first entry */
444 DeviceExtension
->MappedAddressList
= NextMa
->NextMappedAddress
;
448 LastMa
->NextMappedAddress
= NextMa
->NextMappedAddress
;
451 /* Free the resources and quit */
459 NextMa
= NextMa
->NextMappedAddress
;
469 ScsiPortGetBusData(IN PVOID DeviceExtension
,
470 IN ULONG BusDataType
,
471 IN ULONG SystemIoBusNumber
,
476 DPRINT("ScsiPortGetBusData()\n");
480 /* If Length is non-zero, just forward the call to
481 HalGetBusData() function */
482 return HalGetBusData(BusDataType
,
489 /* We have a more complex case here */
498 ScsiPortSetBusDataByOffset(IN PVOID DeviceExtension
,
499 IN ULONG BusDataType
,
500 IN ULONG SystemIoBusNumber
,
506 DPRINT("ScsiPortSetBusDataByOffset()\n");
507 return HalSetBusDataByOffset(BusDataType
,
519 ScsiPortGetDeviceBase(IN PVOID HwDeviceExtension
,
520 IN INTERFACE_TYPE BusType
,
521 IN ULONG SystemIoBusNumber
,
522 IN SCSI_PHYSICAL_ADDRESS IoAddress
,
523 IN ULONG NumberOfBytes
,
524 IN BOOLEAN InIoSpace
)
526 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
527 PHYSICAL_ADDRESS TranslatedAddress
;
528 PMAPPED_ADDRESS DeviceBase
;
532 //DPRINT ("ScsiPortGetDeviceBase() called\n");
534 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
535 SCSI_PORT_DEVICE_EXTENSION
,
536 MiniPortDeviceExtension
);
538 AddressSpace
= (ULONG
)InIoSpace
;
539 if (HalTranslateBusAddress(BusType
,
543 &TranslatedAddress
) == FALSE
)
549 if (AddressSpace
!= 0)
550 return((PVOID
)(ULONG_PTR
)TranslatedAddress
.QuadPart
);
552 MappedAddress
= MmMapIoSpace(TranslatedAddress
,
556 DeviceBase
= ExAllocatePoolWithTag(NonPagedPool
,
557 sizeof(MAPPED_ADDRESS
), TAG_SCSIPORT
);
559 if (DeviceBase
== NULL
)
560 return MappedAddress
;
562 DeviceBase
->MappedAddress
= MappedAddress
;
563 DeviceBase
->NumberOfBytes
= NumberOfBytes
;
564 DeviceBase
->IoAddress
= IoAddress
;
565 DeviceBase
->BusNumber
= SystemIoBusNumber
;
567 /* Link it to the Device Extension list */
568 DeviceBase
->NextMappedAddress
= DeviceExtension
->MappedAddressList
;
569 DeviceExtension
->MappedAddressList
= DeviceBase
;
571 return MappedAddress
;
578 ScsiPortGetLogicalUnit(IN PVOID HwDeviceExtension
,
585 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
586 PSCSI_PORT_LUN_EXTENSION LunExtension
;
589 DPRINT("ScsiPortGetLogicalUnit() called\n");
591 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
592 SCSI_PORT_DEVICE_EXTENSION
,
593 MiniPortDeviceExtension
);
594 if (IsListEmpty(&DeviceExtension
->LunExtensionListHead
))
597 Entry
= DeviceExtension
->LunExtensionListHead
.Flink
;
598 while (Entry
!= &DeviceExtension
->LunExtensionListHead
)
600 LunExtension
= CONTAINING_RECORD(Entry
,
601 SCSI_PORT_LUN_EXTENSION
,
603 if (LunExtension
->PathId
== PathId
&&
604 LunExtension
->TargetId
== TargetId
&&
605 LunExtension
->Lun
== Lun
)
607 return (PVOID
)&LunExtension
->MiniportLunExtension
;
610 Entry
= Entry
->Flink
;
620 SCSI_PHYSICAL_ADDRESS NTAPI
621 ScsiPortGetPhysicalAddress(IN PVOID HwDeviceExtension
,
622 IN PSCSI_REQUEST_BLOCK Srb OPTIONAL
,
623 IN PVOID VirtualAddress
,
626 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
627 SCSI_PHYSICAL_ADDRESS PhysicalAddress
;
628 ULONG BufferLength
= 0;
630 PSCSI_SG_ADDRESS SGList
;
631 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
633 DPRINT("ScsiPortGetPhysicalAddress(%p %p %p %p)\n",
634 HwDeviceExtension
, Srb
, VirtualAddress
, Length
);
636 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
637 SCSI_PORT_DEVICE_EXTENSION
,
638 MiniPortDeviceExtension
);
640 if (Srb
== NULL
|| Srb
->SenseInfoBuffer
== VirtualAddress
)
642 /* Simply look it up in the allocated common buffer */
643 Offset
= (PUCHAR
)VirtualAddress
- (PUCHAR
)DeviceExtension
->SrbExtensionBuffer
;
645 BufferLength
= DeviceExtension
->CommonBufferLength
- Offset
;
646 PhysicalAddress
.QuadPart
= DeviceExtension
->PhysicalAddress
.QuadPart
+ Offset
;
648 else if (DeviceExtension
->MapRegisters
)
650 /* Scatter-gather list must be used */
651 SrbInfo
= SpiGetSrbData(DeviceExtension
,
657 SGList
= SrbInfo
->ScatterGather
;
659 /* Find needed item in the SG list */
660 Offset
= (PCHAR
)VirtualAddress
- (PCHAR
)Srb
->DataBuffer
;
661 while (Offset
>= SGList
->Length
)
663 Offset
-= SGList
->Length
;
667 /* We're done, store length and physical address */
668 BufferLength
= SGList
->Length
- Offset
;
669 PhysicalAddress
.QuadPart
= SGList
->PhysicalAddress
.QuadPart
+ Offset
;
675 PhysicalAddress
.QuadPart
= (LONGLONG
)(SP_UNINITIALIZED_VALUE
);
678 *Length
= BufferLength
;
679 return PhysicalAddress
;
686 PSCSI_REQUEST_BLOCK NTAPI
687 ScsiPortGetSrb(IN PVOID DeviceExtension
,
693 DPRINT1("ScsiPortGetSrb() unimplemented\n");
703 ScsiPortGetUncachedExtension(IN PVOID HwDeviceExtension
,
704 IN PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
705 IN ULONG NumberOfBytes
)
707 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
708 DEVICE_DESCRIPTION DeviceDescription
;
709 ULONG MapRegistersCount
;
712 DPRINT("ScsiPortGetUncachedExtension(%p %p %lu)\n",
713 HwDeviceExtension
, ConfigInfo
, NumberOfBytes
);
715 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
716 SCSI_PORT_DEVICE_EXTENSION
,
717 MiniPortDeviceExtension
);
719 /* Check for allocated common DMA buffer */
720 if (DeviceExtension
->SrbExtensionBuffer
!= NULL
)
722 DPRINT1("The HBA has already got a common DMA buffer!\n");
726 /* Check for DMA adapter object */
727 if (DeviceExtension
->AdapterObject
== NULL
)
729 /* Initialize DMA adapter description */
730 RtlZeroMemory(&DeviceDescription
, sizeof(DEVICE_DESCRIPTION
));
732 DeviceDescription
.Version
= DEVICE_DESCRIPTION_VERSION
;
733 DeviceDescription
.Master
= ConfigInfo
->Master
;
734 DeviceDescription
.ScatterGather
= ConfigInfo
->ScatterGather
;
735 DeviceDescription
.DemandMode
= ConfigInfo
->DemandMode
;
736 DeviceDescription
.Dma32BitAddresses
= ConfigInfo
->Dma32BitAddresses
;
737 DeviceDescription
.BusNumber
= ConfigInfo
->SystemIoBusNumber
;
738 DeviceDescription
.DmaChannel
= ConfigInfo
->DmaChannel
;
739 DeviceDescription
.InterfaceType
= ConfigInfo
->AdapterInterfaceType
;
740 DeviceDescription
.DmaWidth
= ConfigInfo
->DmaWidth
;
741 DeviceDescription
.DmaSpeed
= ConfigInfo
->DmaSpeed
;
742 DeviceDescription
.MaximumLength
= ConfigInfo
->MaximumTransferLength
;
743 DeviceDescription
.DmaPort
= ConfigInfo
->DmaPort
;
745 /* Get a DMA adapter object */
746 DeviceExtension
->AdapterObject
=
747 HalGetAdapter(&DeviceDescription
, &MapRegistersCount
);
749 /* Fail in case of error */
750 if (DeviceExtension
->AdapterObject
== NULL
)
752 DPRINT1("HalGetAdapter() failed\n");
756 /* Set number of physical breaks */
757 if (ConfigInfo
->NumberOfPhysicalBreaks
!= 0 &&
758 MapRegistersCount
> ConfigInfo
->NumberOfPhysicalBreaks
)
760 DeviceExtension
->PortCapabilities
.MaximumPhysicalPages
=
761 ConfigInfo
->NumberOfPhysicalBreaks
;
765 DeviceExtension
->PortCapabilities
.MaximumPhysicalPages
= MapRegistersCount
;
769 /* Update auto request sense feature */
770 DeviceExtension
->SupportsAutoSense
= ConfigInfo
->AutoRequestSense
;
772 /* Update Srb extension size */
773 if (DeviceExtension
->SrbExtensionSize
!= ConfigInfo
->SrbExtensionSize
)
774 DeviceExtension
->SrbExtensionSize
= ConfigInfo
->SrbExtensionSize
;
776 /* Update Srb extension alloc flag */
777 if (ConfigInfo
->AutoRequestSense
|| DeviceExtension
->SrbExtensionSize
)
778 DeviceExtension
->NeedSrbExtensionAlloc
= TRUE
;
780 /* Allocate a common DMA buffer */
781 Status
= SpiAllocateCommonBuffer(DeviceExtension
, NumberOfBytes
);
783 if (!NT_SUCCESS(Status
))
785 DPRINT1("SpiAllocateCommonBuffer() failed with Status = 0x%08X!\n", Status
);
789 return DeviceExtension
->NonCachedExtension
;
793 SpiAllocateCommonBuffer(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
, ULONG NonCachedSize
)
795 PVOID
*SrbExtension
, CommonBuffer
;
796 ULONG CommonBufferLength
, BufSize
;
798 /* If size is 0, set it to 16 */
799 if (!DeviceExtension
->SrbExtensionSize
)
800 DeviceExtension
->SrbExtensionSize
= 16;
803 BufSize
= DeviceExtension
->SrbExtensionSize
;
805 /* Add autosense data size if needed */
806 if (DeviceExtension
->SupportsAutoSense
)
807 BufSize
+= sizeof(SENSE_DATA
);
811 BufSize
= (BufSize
+ sizeof(LONGLONG
) - 1) & ~(sizeof(LONGLONG
) - 1);
813 /* Sum up into the total common buffer length, and round it to page size */
815 ROUND_TO_PAGES(NonCachedSize
+ BufSize
* DeviceExtension
->RequestsNumber
);
818 if (!DeviceExtension
->AdapterObject
)
820 /* From nonpaged pool if there is no DMA */
821 CommonBuffer
= ExAllocatePoolWithTag(NonPagedPool
, CommonBufferLength
, TAG_SCSIPORT
);
825 /* Perform a full request since we have a DMA adapter*/
826 CommonBuffer
= HalAllocateCommonBuffer(DeviceExtension
->AdapterObject
,
828 &DeviceExtension
->PhysicalAddress
,
832 /* Fail in case of error */
834 return STATUS_INSUFFICIENT_RESOURCES
;
837 RtlZeroMemory(CommonBuffer
, CommonBufferLength
);
839 /* Store its size in Device Extension */
840 DeviceExtension
->CommonBufferLength
= CommonBufferLength
;
842 /* SrbExtension buffer is located at the beginning of the buffer */
843 DeviceExtension
->SrbExtensionBuffer
= CommonBuffer
;
845 /* Non-cached extension buffer is located at the end of
849 CommonBufferLength
-= NonCachedSize
;
850 DeviceExtension
->NonCachedExtension
= (PUCHAR
)CommonBuffer
+ CommonBufferLength
;
854 DeviceExtension
->NonCachedExtension
= NULL
;
857 if (DeviceExtension
->NeedSrbExtensionAlloc
)
859 /* Look up how many SRB data structures we need */
860 DeviceExtension
->SrbDataCount
= CommonBufferLength
/ BufSize
;
862 /* Initialize the free SRB extensions list */
863 SrbExtension
= (PVOID
*)CommonBuffer
;
864 DeviceExtension
->FreeSrbExtensions
= SrbExtension
;
866 /* Fill the remainding pointers (if we have more than 1 SRB) */
867 while (CommonBufferLength
>= 2 * BufSize
)
869 *SrbExtension
= (PVOID
*)((PCHAR
)SrbExtension
+ BufSize
);
870 SrbExtension
= *SrbExtension
;
872 CommonBufferLength
-= BufSize
;
876 return STATUS_SUCCESS
;
885 ScsiPortGetVirtualAddress(IN PVOID HwDeviceExtension
,
886 IN SCSI_PHYSICAL_ADDRESS PhysicalAddress
)
888 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
891 DPRINT("ScsiPortGetVirtualAddress(%p %I64x)\n",
892 HwDeviceExtension
, PhysicalAddress
.QuadPart
);
894 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
895 SCSI_PORT_DEVICE_EXTENSION
,
896 MiniPortDeviceExtension
);
898 if (DeviceExtension
->PhysicalAddress
.QuadPart
> PhysicalAddress
.QuadPart
)
901 Offset
= (ULONG
)(PhysicalAddress
.QuadPart
- DeviceExtension
->PhysicalAddress
.QuadPart
);
903 if (Offset
>= DeviceExtension
->CommonBufferLength
)
906 return (PVOID
)((ULONG_PTR
)DeviceExtension
->SrbExtensionBuffer
+ Offset
);
910 SpiInitOpenKeys(PCONFIGURATION_INFO ConfigInfo
, PUNICODE_STRING RegistryPath
)
912 OBJECT_ATTRIBUTES ObjectAttributes
;
913 UNICODE_STRING KeyName
;
916 /* Open the service key */
917 InitializeObjectAttributes(&ObjectAttributes
,
919 OBJ_CASE_INSENSITIVE
,
923 Status
= ZwOpenKey(&ConfigInfo
->ServiceKey
,
927 if (!NT_SUCCESS(Status
))
929 DPRINT("Unable to open driver's registry key %wZ, status 0x%08x\n", RegistryPath
, Status
);
930 ConfigInfo
->ServiceKey
= NULL
;
933 /* If we could open driver's service key, then proceed to the Parameters key */
934 if (ConfigInfo
->ServiceKey
!= NULL
)
936 RtlInitUnicodeString(&KeyName
, L
"Parameters");
937 InitializeObjectAttributes(&ObjectAttributes
,
939 OBJ_CASE_INSENSITIVE
,
940 ConfigInfo
->ServiceKey
,
941 (PSECURITY_DESCRIPTOR
) NULL
);
944 Status
= ZwOpenKey(&ConfigInfo
->DeviceKey
,
948 if (NT_SUCCESS(Status
))
950 /* Yes, Parameters key exist, and it must be used instead of
952 ZwClose(ConfigInfo
->ServiceKey
);
953 ConfigInfo
->ServiceKey
= ConfigInfo
->DeviceKey
;
954 ConfigInfo
->DeviceKey
= NULL
;
958 if (ConfigInfo
->ServiceKey
!= NULL
)
960 /* Open the Device key */
961 RtlInitUnicodeString(&KeyName
, L
"Device");
962 InitializeObjectAttributes(&ObjectAttributes
,
964 OBJ_CASE_INSENSITIVE
,
965 ConfigInfo
->ServiceKey
,
968 /* We don't check for failure here - not needed */
969 ZwOpenKey(&ConfigInfo
->DeviceKey
,
976 /**********************************************************************
981 * Initializes SCSI port driver specific data.
988 * Pointer to the miniport driver's driver object.
991 * Pointer to the miniport driver's registry path.
993 * HwInitializationData
994 * Pointer to port driver specific configuration data.
997 Miniport driver specific context.
1006 ScsiPortInitialize(IN PVOID Argument1
,
1008 IN
struct _HW_INITIALIZATION_DATA
*HwInitializationData
,
1011 PDRIVER_OBJECT DriverObject
= (PDRIVER_OBJECT
)Argument1
;
1012 PUNICODE_STRING RegistryPath
= (PUNICODE_STRING
)Argument2
;
1013 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
= NULL
;
1014 PCONFIGURATION_INFORMATION SystemConfig
;
1015 PPORT_CONFIGURATION_INFORMATION PortConfig
;
1016 PORT_CONFIGURATION_INFORMATION InitialPortConfig
;
1017 CONFIGURATION_INFO ConfigInfo
;
1018 ULONG DeviceExtensionSize
;
1019 ULONG PortConfigSize
;
1021 BOOLEAN DeviceFound
= FALSE
;
1022 BOOLEAN FirstConfigCall
= TRUE
;
1026 ULONG BusNumber
= 0;
1027 PCI_SLOT_NUMBER SlotNumber
;
1029 PDEVICE_OBJECT PortDeviceObject
;
1030 WCHAR NameBuffer
[80];
1031 UNICODE_STRING DeviceName
;
1032 WCHAR DosNameBuffer
[80];
1033 UNICODE_STRING DosDeviceName
;
1034 PIO_SCSI_CAPABILITIES PortCapabilities
;
1037 PCM_RESOURCE_LIST ResourceList
;
1041 DPRINT ("ScsiPortInitialize() called!\n");
1043 /* Check params for validity */
1044 if ((HwInitializationData
->HwInitialize
== NULL
) ||
1045 (HwInitializationData
->HwStartIo
== NULL
) ||
1046 (HwInitializationData
->HwInterrupt
== NULL
) ||
1047 (HwInitializationData
->HwFindAdapter
== NULL
) ||
1048 (HwInitializationData
->HwResetBus
== NULL
))
1050 return STATUS_INVALID_PARAMETER
;
1054 DriverObject
->DriverStartIo
= ScsiPortStartIo
;
1055 DriverObject
->MajorFunction
[IRP_MJ_CREATE
] = ScsiPortCreateClose
;
1056 DriverObject
->MajorFunction
[IRP_MJ_CLOSE
] = ScsiPortCreateClose
;
1057 DriverObject
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = ScsiPortDeviceControl
;
1058 DriverObject
->MajorFunction
[IRP_MJ_INTERNAL_DEVICE_CONTROL
] = ScsiPortDeviceControl
;
1059 DriverObject
->MajorFunction
[IRP_MJ_SCSI
] = ScsiPortDispatchScsi
;
1061 /* Obtain configuration information */
1062 SystemConfig
= IoGetConfigurationInformation();
1064 /* Zero the internal configuration info structure */
1065 RtlZeroMemory(&ConfigInfo
, sizeof(CONFIGURATION_INFO
));
1067 /* Zero starting slot number */
1068 SlotNumber
.u
.AsULONG
= 0;
1070 /* Allocate space for access ranges */
1071 if (HwInitializationData
->NumberOfAccessRanges
)
1073 ConfigInfo
.AccessRanges
=
1074 ExAllocatePoolWithTag(PagedPool
,
1075 HwInitializationData
->NumberOfAccessRanges
* sizeof(ACCESS_RANGE
), TAG_SCSIPORT
);
1077 /* Fail if failed */
1078 if (ConfigInfo
.AccessRanges
== NULL
)
1079 return STATUS_INSUFFICIENT_RESOURCES
;
1082 /* Open registry keys */
1083 SpiInitOpenKeys(&ConfigInfo
, (PUNICODE_STRING
)Argument2
);
1085 /* Last adapter number = not known */
1086 ConfigInfo
.LastAdapterNumber
= SP_UNINITIALIZED_VALUE
;
1088 /* Calculate sizes of DeviceExtension and PortConfig */
1089 DeviceExtensionSize
= sizeof(SCSI_PORT_DEVICE_EXTENSION
) +
1090 HwInitializationData
->DeviceExtensionSize
;
1092 MaxBus
= (HwInitializationData
->AdapterInterfaceType
== PCIBus
) ? 8 : 1;
1093 DPRINT("MaxBus: %lu\n", MaxBus
);
1097 /* Create a unicode device name */
1098 swprintf(NameBuffer
,
1099 L
"\\Device\\ScsiPort%lu",
1100 SystemConfig
->ScsiPortCount
);
1101 RtlInitUnicodeString(&DeviceName
, NameBuffer
);
1103 DPRINT("Creating device: %wZ\n", &DeviceName
);
1105 /* Create the port device */
1106 Status
= IoCreateDevice(DriverObject
,
1107 DeviceExtensionSize
,
1109 FILE_DEVICE_CONTROLLER
,
1114 if (!NT_SUCCESS(Status
))
1116 DPRINT1("IoCreateDevice call failed! (Status 0x%lX)\n", Status
);
1117 PortDeviceObject
= NULL
;
1121 DPRINT ("Created device: %wZ (%p)\n", &DeviceName
, PortDeviceObject
);
1123 /* Set the buffering strategy here... */
1124 PortDeviceObject
->Flags
|= DO_DIRECT_IO
;
1125 PortDeviceObject
->AlignmentRequirement
= FILE_WORD_ALIGNMENT
; /* FIXME: Is this really needed? */
1127 /* Fill Device Extension */
1128 DeviceExtension
= PortDeviceObject
->DeviceExtension
;
1129 RtlZeroMemory(DeviceExtension
, DeviceExtensionSize
);
1130 DeviceExtension
->Length
= DeviceExtensionSize
;
1131 DeviceExtension
->DeviceObject
= PortDeviceObject
;
1132 DeviceExtension
->PortNumber
= SystemConfig
->ScsiPortCount
;
1134 /* Driver's routines... */
1135 DeviceExtension
->HwInitialize
= HwInitializationData
->HwInitialize
;
1136 DeviceExtension
->HwStartIo
= HwInitializationData
->HwStartIo
;
1137 DeviceExtension
->HwInterrupt
= HwInitializationData
->HwInterrupt
;
1138 DeviceExtension
->HwResetBus
= HwInitializationData
->HwResetBus
;
1139 DeviceExtension
->HwDmaStarted
= HwInitializationData
->HwDmaStarted
;
1141 /* Extensions sizes */
1142 DeviceExtension
->MiniPortExtensionSize
= HwInitializationData
->DeviceExtensionSize
;
1143 DeviceExtension
->LunExtensionSize
= HwInitializationData
->SpecificLuExtensionSize
;
1144 DeviceExtension
->SrbExtensionSize
= HwInitializationData
->SrbExtensionSize
;
1146 /* Round Srb extension size to the quadword */
1147 DeviceExtension
->SrbExtensionSize
=
1148 ~(sizeof(LONGLONG
) - 1) & (DeviceExtension
->SrbExtensionSize
+
1149 sizeof(LONGLONG
) - 1);
1151 /* Fill some numbers (bus count, lun count, etc) */
1152 DeviceExtension
->MaxLunCount
= SCSI_MAXIMUM_LOGICAL_UNITS
;
1153 DeviceExtension
->RequestsNumber
= 16;
1155 /* Initialize the spin lock in the controller extension */
1156 KeInitializeSpinLock(&DeviceExtension
->IrqLock
);
1157 KeInitializeSpinLock(&DeviceExtension
->SpinLock
);
1159 /* Initialize the DPC object */
1160 IoInitializeDpcRequest(PortDeviceObject
,
1163 /* Initialize the device timer */
1164 DeviceExtension
->TimerCount
= -1;
1165 IoInitializeTimer(PortDeviceObject
,
1169 /* Initialize miniport timer */
1170 KeInitializeTimer(&DeviceExtension
->MiniportTimer
);
1171 KeInitializeDpc(&DeviceExtension
->MiniportTimerDpc
,
1172 SpiMiniportTimerDpc
,
1177 Status
= SpiCreatePortConfig(DeviceExtension
,
1178 HwInitializationData
,
1183 if (!NT_SUCCESS(Status
))
1185 DPRINT("SpiCreatePortConfig() failed with Status 0x%08X\n", Status
);
1189 /* Allocate and initialize port configuration info */
1190 PortConfigSize
= (sizeof(PORT_CONFIGURATION_INFORMATION
) +
1191 HwInitializationData
->NumberOfAccessRanges
*
1192 sizeof(ACCESS_RANGE
) + 7) & ~7;
1193 DeviceExtension
->PortConfig
= ExAllocatePoolWithTag(NonPagedPool
, PortConfigSize
, TAG_SCSIPORT
);
1195 /* Fail if failed */
1196 if (DeviceExtension
->PortConfig
== NULL
)
1198 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1202 PortConfig
= DeviceExtension
->PortConfig
;
1204 /* Copy information here */
1205 RtlCopyMemory(PortConfig
,
1207 sizeof(PORT_CONFIGURATION_INFORMATION
));
1210 /* Copy extension sizes into the PortConfig */
1211 PortConfig
->SpecificLuExtensionSize
= DeviceExtension
->LunExtensionSize
;
1212 PortConfig
->SrbExtensionSize
= DeviceExtension
->SrbExtensionSize
;
1214 /* Initialize Access ranges */
1215 if (HwInitializationData
->NumberOfAccessRanges
!= 0)
1217 PortConfig
->AccessRanges
= (PVOID
)(PortConfig
+1);
1219 /* Align to LONGLONG */
1220 PortConfig
->AccessRanges
= (PVOID
)((ULONG_PTR
)(PortConfig
->AccessRanges
) + 7);
1221 PortConfig
->AccessRanges
= (PVOID
)((ULONG_PTR
)(PortConfig
->AccessRanges
) & ~7);
1224 RtlCopyMemory(PortConfig
->AccessRanges
,
1225 ConfigInfo
.AccessRanges
,
1226 HwInitializationData
->NumberOfAccessRanges
* sizeof(ACCESS_RANGE
));
1229 /* Search for matching PCI device */
1230 if ((HwInitializationData
->AdapterInterfaceType
== PCIBus
) &&
1231 (HwInitializationData
->VendorIdLength
> 0) &&
1232 (HwInitializationData
->VendorId
!= NULL
) &&
1233 (HwInitializationData
->DeviceIdLength
> 0) &&
1234 (HwInitializationData
->DeviceId
!= NULL
))
1236 PortConfig
->BusInterruptLevel
= 0;
1238 /* Get PCI device data */
1239 DPRINT("VendorId '%.*s' DeviceId '%.*s'\n",
1240 HwInitializationData
->VendorIdLength
,
1241 HwInitializationData
->VendorId
,
1242 HwInitializationData
->DeviceIdLength
,
1243 HwInitializationData
->DeviceId
);
1245 if (!SpiGetPciConfigData(DriverObject
,
1247 HwInitializationData
,
1250 ConfigInfo
.BusNumber
,
1253 /* Continue to the next bus, nothing here */
1254 ConfigInfo
.BusNumber
++;
1255 DeviceExtension
->PortConfig
= NULL
;
1256 ExFreePool(PortConfig
);
1258 goto CreatePortConfig
;
1261 if (!PortConfig
->BusInterruptLevel
)
1263 /* Bypass this slot, because no interrupt was assigned */
1264 DeviceExtension
->PortConfig
= NULL
;
1265 ExFreePool(PortConfig
);
1266 goto CreatePortConfig
;
1271 DPRINT("Non-pci bus\n");
1274 /* Note: HwFindAdapter is called once for each bus */
1276 DPRINT("Calling HwFindAdapter() for Bus %lu\n", PortConfig
->SystemIoBusNumber
);
1277 Result
= (HwInitializationData
->HwFindAdapter
)(&DeviceExtension
->MiniPortDeviceExtension
,
1279 0, /* BusInformation */
1280 ConfigInfo
.Parameter
, /* ArgumentString */
1284 DPRINT("HwFindAdapter() Result: %lu Again: %s\n",
1285 Result
, (Again
) ? "True" : "False");
1287 /* Free MapRegisterBase, it's not needed anymore */
1288 if (DeviceExtension
->MapRegisterBase
!= NULL
)
1290 ExFreePool(DeviceExtension
->MapRegisterBase
);
1291 DeviceExtension
->MapRegisterBase
= NULL
;
1294 /* If result is nothing good... */
1295 if (Result
!= SP_RETURN_FOUND
)
1297 DPRINT("HwFindAdapter() Result: %lu\n", Result
);
1299 if (Result
== SP_RETURN_NOT_FOUND
)
1301 /* We can continue on the next bus */
1302 ConfigInfo
.BusNumber
++;
1305 DeviceExtension
->PortConfig
= NULL
;
1306 ExFreePool(PortConfig
);
1307 goto CreatePortConfig
;
1310 /* Otherwise, break */
1311 Status
= STATUS_INTERNAL_ERROR
;
1315 DPRINT("ScsiPortInitialize(): Found HBA! (%x), adapter Id %d\n",
1316 PortConfig
->BusInterruptVector
, PortConfig
->InitiatorBusId
[0]);
1318 /* If the SRB extension size was updated */
1319 if (!DeviceExtension
->NonCachedExtension
&&
1320 (PortConfig
->SrbExtensionSize
!= DeviceExtension
->SrbExtensionSize
))
1322 /* Set it (rounding to LONGLONG again) */
1323 DeviceExtension
->SrbExtensionSize
=
1324 (PortConfig
->SrbExtensionSize
+
1325 sizeof(LONGLONG
)) & ~(sizeof(LONGLONG
) - 1);
1328 /* The same with LUN extension size */
1329 if (PortConfig
->SpecificLuExtensionSize
!= DeviceExtension
->LunExtensionSize
)
1330 DeviceExtension
->LunExtensionSize
= PortConfig
->SpecificLuExtensionSize
;
1333 if (!((HwInitializationData
->AdapterInterfaceType
== PCIBus
) &&
1334 (HwInitializationData
->VendorIdLength
> 0) &&
1335 (HwInitializationData
->VendorId
!= NULL
) &&
1336 (HwInitializationData
->DeviceIdLength
> 0) &&
1337 (HwInitializationData
->DeviceId
!= NULL
)))
1339 /* Construct a resource list */
1340 ResourceList
= SpiConfigToResource(DeviceExtension
,
1345 UNICODE_STRING UnicodeString
;
1346 RtlInitUnicodeString(&UnicodeString
, L
"ScsiAdapter");
1347 DPRINT("Reporting resources\n");
1348 Status
= IoReportResourceUsage(&UnicodeString
,
1354 FIELD_OFFSET(CM_RESOURCE_LIST
,
1355 List
[0].PartialResourceList
.PartialDescriptors
) +
1356 ResourceList
->List
[0].PartialResourceList
.Count
1357 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
),
1360 ExFreePool(ResourceList
);
1362 /* In case of a failure or a conflict, break */
1363 if (Conflict
|| (!NT_SUCCESS(Status
)))
1366 Status
= STATUS_CONFLICTING_ADDRESSES
;
1372 /* Reset the Conflict var */
1375 /* Copy all stuff which we ever need from PortConfig to the DeviceExtension */
1376 if (PortConfig
->MaximumNumberOfTargets
> SCSI_MAXIMUM_TARGETS_PER_BUS
)
1377 DeviceExtension
->MaxTargedIds
= SCSI_MAXIMUM_TARGETS_PER_BUS
;
1379 DeviceExtension
->MaxTargedIds
= PortConfig
->MaximumNumberOfTargets
;
1381 DeviceExtension
->BusNum
= PortConfig
->NumberOfBuses
;
1382 DeviceExtension
->CachesData
= PortConfig
->CachesData
;
1383 DeviceExtension
->ReceiveEvent
= PortConfig
->ReceiveEvent
;
1384 DeviceExtension
->SupportsTaggedQueuing
= PortConfig
->TaggedQueuing
;
1385 DeviceExtension
->MultipleReqsPerLun
= PortConfig
->MultipleRequestPerLu
;
1387 /* If something was disabled via registry - apply it */
1388 if (ConfigInfo
.DisableMultipleLun
)
1389 DeviceExtension
->MultipleReqsPerLun
= PortConfig
->MultipleRequestPerLu
= FALSE
;
1391 if (ConfigInfo
.DisableTaggedQueueing
)
1392 DeviceExtension
->SupportsTaggedQueuing
= PortConfig
->MultipleRequestPerLu
= FALSE
;
1394 /* Check if we need to alloc SRB data */
1395 if (DeviceExtension
->SupportsTaggedQueuing
||
1396 DeviceExtension
->MultipleReqsPerLun
)
1398 DeviceExtension
->NeedSrbDataAlloc
= TRUE
;
1402 DeviceExtension
->NeedSrbDataAlloc
= FALSE
;
1405 /* Get a pointer to the port capabilities */
1406 PortCapabilities
= &DeviceExtension
->PortCapabilities
;
1408 /* Copy one field there */
1409 DeviceExtension
->MapBuffers
= PortConfig
->MapBuffers
;
1410 PortCapabilities
->AdapterUsesPio
= PortConfig
->MapBuffers
;
1412 if (DeviceExtension
->AdapterObject
== NULL
&&
1413 (PortConfig
->DmaChannel
!= SP_UNINITIALIZED_VALUE
|| PortConfig
->Master
))
1415 DPRINT1("DMA is not supported yet\n");
1419 if (DeviceExtension
->SrbExtensionBuffer
== NULL
&&
1420 (DeviceExtension
->SrbExtensionSize
!= 0 ||
1421 PortConfig
->AutoRequestSense
))
1423 DeviceExtension
->SupportsAutoSense
= PortConfig
->AutoRequestSense
;
1424 DeviceExtension
->NeedSrbExtensionAlloc
= TRUE
;
1426 /* Allocate common buffer */
1427 Status
= SpiAllocateCommonBuffer(DeviceExtension
, 0);
1429 /* Check for failure */
1430 if (!NT_SUCCESS(Status
))
1434 /* Allocate SrbData, if needed */
1435 if (DeviceExtension
->NeedSrbDataAlloc
)
1438 PSCSI_REQUEST_BLOCK_INFO SrbData
;
1440 if (DeviceExtension
->SrbDataCount
!= 0)
1441 Count
= DeviceExtension
->SrbDataCount
;
1443 Count
= DeviceExtension
->RequestsNumber
* 2;
1445 /* Allocate the data */
1446 SrbData
= ExAllocatePoolWithTag(NonPagedPool
, Count
* sizeof(SCSI_REQUEST_BLOCK_INFO
), TAG_SCSIPORT
);
1447 if (SrbData
== NULL
)
1448 return STATUS_INSUFFICIENT_RESOURCES
;
1450 RtlZeroMemory(SrbData
, Count
* sizeof(SCSI_REQUEST_BLOCK_INFO
));
1452 DeviceExtension
->SrbInfo
= SrbData
;
1453 DeviceExtension
->FreeSrbInfo
= SrbData
;
1454 DeviceExtension
->SrbDataCount
= Count
;
1456 /* Link it to the list */
1459 SrbData
->Requests
.Flink
= (PLIST_ENTRY
)(SrbData
+ 1);
1464 /* Mark the last entry of the list */
1466 SrbData
->Requests
.Flink
= NULL
;
1469 /* Initialize port capabilities */
1470 PortCapabilities
= &DeviceExtension
->PortCapabilities
;
1471 PortCapabilities
->Length
= sizeof(IO_SCSI_CAPABILITIES
);
1472 PortCapabilities
->MaximumTransferLength
= PortConfig
->MaximumTransferLength
;
1474 if (PortConfig
->ReceiveEvent
)
1475 PortCapabilities
->SupportedAsynchronousEvents
|= SRBEV_SCSI_ASYNC_NOTIFICATION
;
1477 PortCapabilities
->TaggedQueuing
= DeviceExtension
->SupportsTaggedQueuing
;
1478 PortCapabilities
->AdapterScansDown
= PortConfig
->AdapterScansDown
;
1480 if (PortConfig
->AlignmentMask
> PortDeviceObject
->AlignmentRequirement
)
1481 PortDeviceObject
->AlignmentRequirement
= PortConfig
->AlignmentMask
;
1483 PortCapabilities
->AlignmentMask
= PortDeviceObject
->AlignmentRequirement
;
1485 if (PortCapabilities
->MaximumPhysicalPages
== 0)
1487 PortCapabilities
->MaximumPhysicalPages
=
1488 BYTES_TO_PAGES(PortCapabilities
->MaximumTransferLength
);
1490 /* Apply miniport's limits */
1491 if (PortConfig
->NumberOfPhysicalBreaks
< PortCapabilities
->MaximumPhysicalPages
)
1493 PortCapabilities
->MaximumPhysicalPages
= PortConfig
->NumberOfPhysicalBreaks
;
1497 /* Deal with interrupts */
1498 if (DeviceExtension
->HwInterrupt
== NULL
||
1499 (PortConfig
->BusInterruptLevel
== 0 && PortConfig
->BusInterruptVector
== 0))
1502 DeviceExtension
->InterruptCount
= 0;
1504 DPRINT1("Interrupt Count: 0\n");
1508 /* This code path will ALWAYS crash so stop it now */
1513 BOOLEAN InterruptShareable
;
1514 KINTERRUPT_MODE InterruptMode
[2];
1515 ULONG InterruptVector
[2], i
, MappedIrq
[2];
1516 KIRQL Dirql
[2], MaxDirql
;
1517 KAFFINITY Affinity
[2];
1519 DeviceExtension
->InterruptLevel
[0] = PortConfig
->BusInterruptLevel
;
1520 DeviceExtension
->InterruptLevel
[1] = PortConfig
->BusInterruptLevel2
;
1522 InterruptVector
[0] = PortConfig
->BusInterruptVector
;
1523 InterruptVector
[1] = PortConfig
->BusInterruptVector2
;
1525 InterruptMode
[0] = PortConfig
->InterruptMode
;
1526 InterruptMode
[1] = PortConfig
->InterruptMode2
;
1528 DeviceExtension
->InterruptCount
= (PortConfig
->BusInterruptLevel2
!= 0 || PortConfig
->BusInterruptVector2
!= 0) ? 2 : 1;
1530 for (i
= 0; i
< DeviceExtension
->InterruptCount
; i
++)
1532 /* Register an interrupt handler for this device */
1533 MappedIrq
[i
] = HalGetInterruptVector(PortConfig
->AdapterInterfaceType
,
1534 PortConfig
->SystemIoBusNumber
,
1535 DeviceExtension
->InterruptLevel
[i
],
1541 if (DeviceExtension
->InterruptCount
== 1 || Dirql
[0] > Dirql
[1])
1542 MaxDirql
= Dirql
[0];
1544 MaxDirql
= Dirql
[1];
1546 for (i
= 0; i
< DeviceExtension
->InterruptCount
; i
++)
1548 /* Determing IRQ sharability as usual */
1549 if (PortConfig
->AdapterInterfaceType
== MicroChannel
||
1550 InterruptMode
[i
] == LevelSensitive
)
1552 InterruptShareable
= TRUE
;
1556 InterruptShareable
= FALSE
;
1559 Status
= IoConnectInterrupt(&DeviceExtension
->Interrupt
[i
],
1560 (PKSERVICE_ROUTINE
)ScsiPortIsr
,
1562 &DeviceExtension
->IrqLock
,
1571 if (!(NT_SUCCESS(Status
)))
1573 DPRINT1("Could not connect interrupt %d\n",
1574 InterruptVector
[i
]);
1575 DeviceExtension
->Interrupt
[i
] = NULL
;
1580 if (!NT_SUCCESS(Status
))
1584 /* Save IoAddress (from access ranges) */
1585 if (HwInitializationData
->NumberOfAccessRanges
!= 0)
1587 DeviceExtension
->IoAddress
=
1588 ((*(PortConfig
->AccessRanges
))[0]).RangeStart
.LowPart
;
1590 DPRINT("Io Address %x\n", DeviceExtension
->IoAddress
);
1593 /* Set flag that it's allowed to disconnect during this command */
1594 DeviceExtension
->Flags
|= SCSI_PORT_DISCONNECT_ALLOWED
;
1596 /* Initialize counter of active requests (-1 means there are none) */
1597 DeviceExtension
->ActiveRequestCounter
= -1;
1599 /* Analyze what we have about DMA */
1600 if (DeviceExtension
->AdapterObject
!= NULL
&&
1601 PortConfig
->Master
&&
1602 PortConfig
->NeedPhysicalAddresses
)
1604 DeviceExtension
->MapRegisters
= TRUE
;
1608 DeviceExtension
->MapRegisters
= FALSE
;
1611 /* Call HwInitialize at DISPATCH_LEVEL */
1612 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
1614 if (!KeSynchronizeExecution(DeviceExtension
->Interrupt
[0],
1615 DeviceExtension
->HwInitialize
,
1616 DeviceExtension
->MiniPortDeviceExtension
))
1618 DPRINT1("HwInitialize() failed!\n");
1619 KeLowerIrql(OldIrql
);
1620 Status
= STATUS_ADAPTER_HARDWARE_ERROR
;
1624 /* Check if a notification is needed */
1625 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
1627 /* Call DPC right away, because we're already at DISPATCH_LEVEL */
1628 ScsiPortDpcForIsr(NULL
,
1629 DeviceExtension
->DeviceObject
,
1634 /* Lower irql back to what it was */
1635 KeLowerIrql(OldIrql
);
1637 /* Start our timer */
1638 IoStartTimer(PortDeviceObject
);
1640 /* Initialize bus scanning information */
1641 DeviceExtension
->BusesConfig
= ExAllocatePoolWithTag(PagedPool
,
1642 sizeof(PVOID
) * DeviceExtension
->PortConfig
->NumberOfBuses
1643 + sizeof(ULONG
), TAG_SCSIPORT
);
1645 if (!DeviceExtension
->BusesConfig
)
1647 DPRINT1("Out of resources!\n");
1648 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1653 RtlZeroMemory(DeviceExtension
->BusesConfig
,
1654 sizeof(PVOID
) * DeviceExtension
->PortConfig
->NumberOfBuses
1657 /* Store number of buses there */
1658 DeviceExtension
->BusesConfig
->NumberOfBuses
= (UCHAR
)DeviceExtension
->BusNum
;
1660 /* Scan the adapter for devices */
1661 SpiScanAdapter(DeviceExtension
);
1663 /* Build the registry device map */
1664 SpiBuildDeviceMap(DeviceExtension
,
1665 (PUNICODE_STRING
)Argument2
);
1667 /* Create the dos device link */
1668 swprintf(DosNameBuffer
,
1670 SystemConfig
->ScsiPortCount
);
1671 RtlInitUnicodeString(&DosDeviceName
, DosNameBuffer
);
1672 IoCreateSymbolicLink(&DosDeviceName
, &DeviceName
);
1674 /* Increase the port count */
1675 SystemConfig
->ScsiPortCount
++;
1676 FirstConfigCall
= FALSE
;
1678 /* Increase adapter number and bus number respectively */
1679 ConfigInfo
.AdapterNumber
++;
1682 ConfigInfo
.BusNumber
++;
1684 DPRINT("Bus: %lu MaxBus: %lu\n", BusNumber
, MaxBus
);
1689 /* Clean up the mess */
1690 SpiCleanupAfterInit(DeviceExtension
);
1692 /* Close registry keys */
1693 if (ConfigInfo
.ServiceKey
!= NULL
)
1694 ZwClose(ConfigInfo
.ServiceKey
);
1696 if (ConfigInfo
.DeviceKey
!= NULL
)
1697 ZwClose(ConfigInfo
.DeviceKey
);
1699 if (ConfigInfo
.BusKey
!= NULL
)
1700 ZwClose(ConfigInfo
.BusKey
);
1702 if (ConfigInfo
.AccessRanges
!= NULL
)
1703 ExFreePool(ConfigInfo
.AccessRanges
);
1705 if (ConfigInfo
.Parameter
!= NULL
)
1706 ExFreePool(ConfigInfo
.Parameter
);
1708 DPRINT("ScsiPortInitialize() done, Status = 0x%08X, DeviceFound = %d!\n",
1709 Status
, DeviceFound
);
1711 return (DeviceFound
== FALSE
) ? Status
: STATUS_SUCCESS
;
1715 SpiCleanupAfterInit(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
)
1717 PSCSI_LUN_INFO LunInfo
;
1721 /* Check if we have something to clean up */
1722 if (DeviceExtension
== NULL
)
1725 /* Stop the timer */
1726 IoStopTimer(DeviceExtension
->DeviceObject
);
1728 /* Disconnect the interrupts */
1729 while (DeviceExtension
->InterruptCount
)
1731 if (DeviceExtension
->Interrupt
[--DeviceExtension
->InterruptCount
])
1732 IoDisconnectInterrupt(DeviceExtension
->Interrupt
[DeviceExtension
->InterruptCount
]);
1735 /* Delete ConfigInfo */
1736 if (DeviceExtension
->BusesConfig
)
1738 for (Bus
= 0; Bus
< DeviceExtension
->BusNum
; Bus
++)
1740 if (!DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
])
1743 LunInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LunInfo
;
1747 /* Free current, but save pointer to the next one */
1748 Ptr
= LunInfo
->Next
;
1749 ExFreePool(LunInfo
);
1753 ExFreePool(DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]);
1756 ExFreePool(DeviceExtension
->BusesConfig
);
1759 /* Free PortConfig */
1760 if (DeviceExtension
->PortConfig
)
1761 ExFreePool(DeviceExtension
->PortConfig
);
1764 for(Lun
= 0; Lun
< LUS_NUMBER
; Lun
++)
1766 while (DeviceExtension
->LunExtensionList
[Lun
])
1768 Ptr
= DeviceExtension
->LunExtensionList
[Lun
];
1769 DeviceExtension
->LunExtensionList
[Lun
] = DeviceExtension
->LunExtensionList
[Lun
]->Next
;
1775 /* Free common buffer (if it exists) */
1776 if (DeviceExtension
->SrbExtensionBuffer
!= NULL
&&
1777 DeviceExtension
->CommonBufferLength
!= 0)
1779 if (!DeviceExtension
->AdapterObject
)
1781 ExFreePool(DeviceExtension
->SrbExtensionBuffer
);
1785 HalFreeCommonBuffer(DeviceExtension
->AdapterObject
,
1786 DeviceExtension
->CommonBufferLength
,
1787 DeviceExtension
->PhysicalAddress
,
1788 DeviceExtension
->SrbExtensionBuffer
,
1794 if (DeviceExtension
->SrbInfo
!= NULL
)
1795 ExFreePool(DeviceExtension
->SrbInfo
);
1797 /* Unmap mapped addresses */
1798 while (DeviceExtension
->MappedAddressList
!= NULL
)
1800 MmUnmapIoSpace(DeviceExtension
->MappedAddressList
->MappedAddress
,
1801 DeviceExtension
->MappedAddressList
->NumberOfBytes
);
1803 Ptr
= DeviceExtension
->MappedAddressList
;
1804 DeviceExtension
->MappedAddressList
= DeviceExtension
->MappedAddressList
->NextMappedAddress
;
1809 /* Finally delete the device object */
1810 DPRINT("Deleting device %p\n", DeviceExtension
->DeviceObject
);
1811 IoDeleteDevice(DeviceExtension
->DeviceObject
);
1818 ScsiPortIoMapTransfer(IN PVOID HwDeviceExtension
,
1819 IN PSCSI_REQUEST_BLOCK Srb
,
1820 IN PVOID LogicalAddress
,
1823 DPRINT1("ScsiPortIoMapTransfer()\n");
1831 ScsiPortLogError(IN PVOID HwDeviceExtension
,
1832 IN PSCSI_REQUEST_BLOCK Srb OPTIONAL
,
1839 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
1841 DPRINT1("ScsiPortLogError() called\n");
1843 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
1844 SCSI_PORT_DEVICE_EXTENSION
,
1845 MiniPortDeviceExtension
);
1848 DPRINT("ScsiPortLogError() done\n");
1855 ScsiPortMoveMemory(OUT PVOID Destination
,
1859 RtlMoveMemory(Destination
,
1869 ScsiPortNotification(IN SCSI_NOTIFICATION_TYPE NotificationType
,
1870 IN PVOID HwDeviceExtension
,
1873 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
1876 DPRINT("ScsiPortNotification() called\n");
1878 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
1879 SCSI_PORT_DEVICE_EXTENSION
,
1880 MiniPortDeviceExtension
);
1882 DPRINT("DeviceExtension %p\n", DeviceExtension
);
1884 va_start(ap
, HwDeviceExtension
);
1886 switch (NotificationType
)
1888 case RequestComplete
:
1890 PSCSI_REQUEST_BLOCK Srb
;
1891 PSCSI_REQUEST_BLOCK_INFO SrbData
;
1893 Srb
= (PSCSI_REQUEST_BLOCK
) va_arg (ap
, PSCSI_REQUEST_BLOCK
);
1895 DPRINT("Notify: RequestComplete (Srb %p)\n", Srb
);
1897 /* Make sure Srb is allright */
1898 ASSERT(Srb
->SrbStatus
!= SRB_STATUS_PENDING
);
1899 ASSERT(Srb
->Function
!= SRB_FUNCTION_EXECUTE_SCSI
|| Srb
->SrbStatus
!= SRB_STATUS_SUCCESS
|| Srb
->ScsiStatus
== SCSISTAT_GOOD
);
1901 if (!(Srb
->SrbFlags
& SRB_FLAGS_IS_ACTIVE
))
1903 /* It's been already completed */
1908 /* It's not active anymore */
1909 Srb
->SrbFlags
&= ~SRB_FLAGS_IS_ACTIVE
;
1911 if (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
)
1913 /* TODO: Treat it specially */
1918 /* Get the SRB data */
1919 SrbData
= SpiGetSrbData(DeviceExtension
,
1925 /* Make sure there are no CompletedRequests and there is a Srb */
1926 ASSERT(SrbData
->CompletedRequests
== NULL
&& SrbData
->Srb
!= NULL
);
1928 /* If it's a read/write request, make sure it has data inside it */
1929 if ((Srb
->SrbStatus
== SRB_STATUS_SUCCESS
) &&
1930 ((Srb
->Cdb
[0] == SCSIOP_READ
) || (Srb
->Cdb
[0] == SCSIOP_WRITE
)))
1932 ASSERT(Srb
->DataTransferLength
);
1935 SrbData
->CompletedRequests
= DeviceExtension
->InterruptData
.CompletedRequests
;
1936 DeviceExtension
->InterruptData
.CompletedRequests
= SrbData
;
1942 DPRINT("Notify: NextRequest\n");
1943 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_NEXT_REQUEST_READY
;
1951 PSCSI_PORT_LUN_EXTENSION LunExtension
;
1953 PathId
= (UCHAR
) va_arg (ap
, int);
1954 TargetId
= (UCHAR
) va_arg (ap
, int);
1955 Lun
= (UCHAR
) va_arg (ap
, int);
1957 DPRINT("Notify: NextLuRequest(PathId %u TargetId %u Lun %u)\n",
1958 PathId
, TargetId
, Lun
);
1960 /* Mark it in the flags field */
1961 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_NEXT_REQUEST_READY
;
1963 /* Get the LUN extension */
1964 LunExtension
= SpiGetLunExtension(DeviceExtension
,
1969 /* If returned LunExtension is NULL, break out */
1970 if (!LunExtension
) break;
1972 /* This request should not be processed if */
1973 if ((LunExtension
->ReadyLun
) ||
1974 (LunExtension
->SrbInfo
.Srb
))
1976 /* Nothing to do here */
1980 /* Add this LUN to the list */
1981 LunExtension
->ReadyLun
= DeviceExtension
->InterruptData
.ReadyLun
;
1982 DeviceExtension
->InterruptData
.ReadyLun
= LunExtension
;
1987 DPRINT("Notify: ResetDetected\n");
1988 /* Add RESET flags */
1989 DeviceExtension
->InterruptData
.Flags
|=
1990 SCSI_PORT_RESET
| SCSI_PORT_RESET_REPORTED
;
1993 case CallDisableInterrupts
:
1994 DPRINT1("UNIMPLEMENTED SCSI Notification called: CallDisableInterrupts!\n");
1997 case CallEnableInterrupts
:
1998 DPRINT1("UNIMPLEMENTED SCSI Notification called: CallEnableInterrupts!\n");
2001 case RequestTimerCall
:
2002 DPRINT("Notify: RequestTimerCall\n");
2003 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_TIMER_NEEDED
;
2004 DeviceExtension
->InterruptData
.HwScsiTimer
= (PHW_TIMER
)va_arg(ap
, PHW_TIMER
);
2005 DeviceExtension
->InterruptData
.MiniportTimerValue
= (ULONG
)va_arg(ap
, ULONG
);
2008 case BusChangeDetected
:
2009 DPRINT1("UNIMPLEMENTED SCSI Notification called: BusChangeDetected!\n");
2013 DPRINT1 ("Unsupported notification from WMI: %lu\n", NotificationType
);
2019 /* Request a DPC after we're done with the interrupt */
2020 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_NOTIFICATION_NEEDED
;
2027 ScsiPortValidateRange(IN PVOID HwDeviceExtension
,
2028 IN INTERFACE_TYPE BusType
,
2029 IN ULONG SystemIoBusNumber
,
2030 IN SCSI_PHYSICAL_ADDRESS IoAddress
,
2031 IN ULONG NumberOfBytes
,
2032 IN BOOLEAN InIoSpace
)
2034 DPRINT("ScsiPortValidateRange()\n");
2039 /* INTERNAL FUNCTIONS ********************************************************/
2042 SpiResourceToConfig(IN PHW_INITIALIZATION_DATA HwInitializationData
,
2043 IN PCM_FULL_RESOURCE_DESCRIPTOR ResourceDescriptor
,
2044 IN PPORT_CONFIGURATION_INFORMATION PortConfig
)
2046 PACCESS_RANGE AccessRange
;
2047 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialData
;
2050 ULONG Interrupt
= 0;
2055 /* Loop through all entries */
2056 for (Index
= 0; Index
< ResourceDescriptor
->PartialResourceList
.Count
; Index
++)
2058 PartialData
= &ResourceDescriptor
->PartialResourceList
.PartialDescriptors
[Index
];
2060 switch (PartialData
->Type
)
2062 case CmResourceTypePort
:
2063 /* Copy access ranges */
2064 if (RangeNumber
< HwInitializationData
->NumberOfAccessRanges
)
2066 AccessRange
= &((*(PortConfig
->AccessRanges
))[RangeNumber
]);
2068 AccessRange
->RangeStart
= PartialData
->u
.Port
.Start
;
2069 AccessRange
->RangeLength
= PartialData
->u
.Port
.Length
;
2071 AccessRange
->RangeInMemory
= FALSE
;
2076 case CmResourceTypeMemory
:
2077 /* Copy access ranges */
2078 if (RangeNumber
< HwInitializationData
->NumberOfAccessRanges
)
2080 AccessRange
= &((*(PortConfig
->AccessRanges
))[RangeNumber
]);
2082 AccessRange
->RangeStart
= PartialData
->u
.Memory
.Start
;
2083 AccessRange
->RangeLength
= PartialData
->u
.Memory
.Length
;
2085 AccessRange
->RangeInMemory
= TRUE
;
2090 case CmResourceTypeInterrupt
:
2094 /* Copy interrupt data */
2095 PortConfig
->BusInterruptLevel
= PartialData
->u
.Interrupt
.Level
;
2096 PortConfig
->BusInterruptVector
= PartialData
->u
.Interrupt
.Vector
;
2098 /* Set interrupt mode accordingly to the resource */
2099 if (PartialData
->Flags
== CM_RESOURCE_INTERRUPT_LATCHED
)
2101 PortConfig
->InterruptMode
= Latched
;
2103 else if (PartialData
->Flags
== CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE
)
2105 PortConfig
->InterruptMode
= LevelSensitive
;
2108 else if (Interrupt
== 1)
2110 /* Copy interrupt data */
2111 PortConfig
->BusInterruptLevel2
= PartialData
->u
.Interrupt
.Level
;
2112 PortConfig
->BusInterruptVector2
= PartialData
->u
.Interrupt
.Vector
;
2114 /* Set interrupt mode accordingly to the resource */
2115 if (PartialData
->Flags
== CM_RESOURCE_INTERRUPT_LATCHED
)
2117 PortConfig
->InterruptMode2
= Latched
;
2119 else if (PartialData
->Flags
== CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE
)
2121 PortConfig
->InterruptMode2
= LevelSensitive
;
2128 case CmResourceTypeDma
:
2132 PortConfig
->DmaChannel
= PartialData
->u
.Dma
.Channel
;
2133 PortConfig
->DmaPort
= PartialData
->u
.Dma
.Port
;
2135 if (PartialData
->Flags
& CM_RESOURCE_DMA_8
)
2136 PortConfig
->DmaWidth
= Width8Bits
;
2137 else if ((PartialData
->Flags
& CM_RESOURCE_DMA_16
) ||
2138 (PartialData
->Flags
& CM_RESOURCE_DMA_8_AND_16
)) //???
2139 PortConfig
->DmaWidth
= Width16Bits
;
2140 else if (PartialData
->Flags
& CM_RESOURCE_DMA_32
)
2141 PortConfig
->DmaWidth
= Width32Bits
;
2145 PortConfig
->DmaChannel2
= PartialData
->u
.Dma
.Channel
;
2146 PortConfig
->DmaPort2
= PartialData
->u
.Dma
.Port
;
2148 if (PartialData
->Flags
& CM_RESOURCE_DMA_8
)
2149 PortConfig
->DmaWidth2
= Width8Bits
;
2150 else if ((PartialData
->Flags
& CM_RESOURCE_DMA_16
) ||
2151 (PartialData
->Flags
& CM_RESOURCE_DMA_8_AND_16
)) //???
2152 PortConfig
->DmaWidth2
= Width16Bits
;
2153 else if (PartialData
->Flags
& CM_RESOURCE_DMA_32
)
2154 PortConfig
->DmaWidth2
= Width32Bits
;
2161 static PCM_RESOURCE_LIST
2162 SpiConfigToResource(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
2163 PPORT_CONFIGURATION_INFORMATION PortConfig
)
2165 PCONFIGURATION_INFORMATION ConfigInfo
;
2166 PCM_RESOURCE_LIST ResourceList
;
2167 PCM_PARTIAL_RESOURCE_DESCRIPTOR ResourceDescriptor
;
2168 PACCESS_RANGE AccessRange
;
2169 ULONG ListLength
= 0, i
, FullSize
;
2170 ULONG Interrupt
, Dma
;
2172 /* Get current Atdisk usage from the system */
2173 ConfigInfo
= IoGetConfigurationInformation();
2175 if (PortConfig
->AtdiskPrimaryClaimed
)
2176 ConfigInfo
->AtDiskPrimaryAddressClaimed
= TRUE
;
2178 if (PortConfig
->AtdiskSecondaryClaimed
)
2179 ConfigInfo
->AtDiskSecondaryAddressClaimed
= TRUE
;
2181 /* Do we use DMA? */
2182 if (PortConfig
->DmaChannel
!= SP_UNINITIALIZED_VALUE
||
2183 PortConfig
->DmaPort
!= SP_UNINITIALIZED_VALUE
)
2187 if (PortConfig
->DmaChannel2
!= SP_UNINITIALIZED_VALUE
||
2188 PortConfig
->DmaPort2
!= SP_UNINITIALIZED_VALUE
)
2197 /* How many interrupts to we have? */
2198 Interrupt
= DeviceExtension
->InterruptCount
;
2199 ListLength
+= Interrupt
;
2201 /* How many access ranges do we use? */
2202 AccessRange
= &((*(PortConfig
->AccessRanges
))[0]);
2203 for (i
= 0; i
< PortConfig
->NumberOfAccessRanges
; i
++)
2205 if (AccessRange
->RangeLength
!= 0)
2211 /* Allocate the resource list, since we know its size now */
2212 FullSize
= sizeof(CM_RESOURCE_LIST
) + (ListLength
- 1) *
2213 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
2215 ResourceList
= (PCM_RESOURCE_LIST
)ExAllocatePoolWithTag(PagedPool
, FullSize
, TAG_SCSIPORT
);
2221 RtlZeroMemory(ResourceList
, FullSize
);
2224 ResourceList
->Count
= 1;
2225 ResourceList
->List
[0].InterfaceType
= PortConfig
->AdapterInterfaceType
;
2226 ResourceList
->List
[0].BusNumber
= PortConfig
->SystemIoBusNumber
;
2227 ResourceList
->List
[0].PartialResourceList
.Count
= ListLength
;
2228 ResourceDescriptor
= ResourceList
->List
[0].PartialResourceList
.PartialDescriptors
;
2230 /* Copy access ranges array over */
2231 for (i
= 0; i
< PortConfig
->NumberOfAccessRanges
; i
++)
2233 AccessRange
= &((*(PortConfig
->AccessRanges
))[i
]);
2235 /* If the range is empty - skip it */
2236 if (AccessRange
->RangeLength
== 0)
2239 if (AccessRange
->RangeInMemory
)
2241 ResourceDescriptor
->Type
= CmResourceTypeMemory
;
2242 ResourceDescriptor
->Flags
= CM_RESOURCE_MEMORY_READ_WRITE
;
2246 ResourceDescriptor
->Type
= CmResourceTypePort
;
2247 ResourceDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
2250 ResourceDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
2252 ResourceDescriptor
->u
.Memory
.Start
= AccessRange
->RangeStart
;
2253 ResourceDescriptor
->u
.Memory
.Length
= AccessRange
->RangeLength
;
2255 ResourceDescriptor
++;
2258 /* If we use interrupt(s), copy them */
2261 ResourceDescriptor
->Type
= CmResourceTypeInterrupt
;
2263 if (PortConfig
->AdapterInterfaceType
== MicroChannel
||
2264 ((Interrupt
== 2) ? PortConfig
->InterruptMode2
: PortConfig
->InterruptMode
) == LevelSensitive
)
2266 ResourceDescriptor
->ShareDisposition
= CmResourceShareShared
;
2267 ResourceDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE
;
2271 ResourceDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
2272 ResourceDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
2275 ResourceDescriptor
->u
.Interrupt
.Level
= (Interrupt
== 2) ? PortConfig
->BusInterruptLevel2
: PortConfig
->BusInterruptLevel
;
2276 ResourceDescriptor
->u
.Interrupt
.Vector
= (Interrupt
== 2) ? PortConfig
->BusInterruptVector2
: PortConfig
->BusInterruptVector
;
2277 ResourceDescriptor
->u
.Interrupt
.Affinity
= 0;
2279 ResourceDescriptor
++;
2286 ResourceDescriptor
->Type
= CmResourceTypeDma
;
2287 ResourceDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
2288 ResourceDescriptor
->u
.Dma
.Channel
= (Dma
== 2) ? PortConfig
->DmaChannel2
: PortConfig
->DmaChannel
;
2289 ResourceDescriptor
->u
.Dma
.Port
= (Dma
== 2) ? PortConfig
->DmaPort2
: PortConfig
->DmaPort
;
2290 ResourceDescriptor
->Flags
= 0;
2292 if (((Dma
== 2) ? PortConfig
->DmaWidth2
: PortConfig
->DmaWidth
) == Width8Bits
)
2293 ResourceDescriptor
->Flags
|= CM_RESOURCE_DMA_8
;
2294 else if (((Dma
== 2) ? PortConfig
->DmaWidth2
: PortConfig
->DmaWidth
) == Width16Bits
)
2295 ResourceDescriptor
->Flags
|= CM_RESOURCE_DMA_16
;
2297 ResourceDescriptor
->Flags
|= CM_RESOURCE_DMA_32
;
2299 if (((Dma
== 2) ? PortConfig
->DmaChannel2
: PortConfig
->DmaChannel
) == SP_UNINITIALIZED_VALUE
)
2300 ResourceDescriptor
->u
.Dma
.Channel
= 0;
2302 if (((Dma
== 2) ? PortConfig
->DmaPort2
: PortConfig
->DmaPort
) == SP_UNINITIALIZED_VALUE
)
2303 ResourceDescriptor
->u
.Dma
.Port
= 0;
2305 ResourceDescriptor
++;
2309 return ResourceList
;
2314 SpiGetPciConfigData(IN PDRIVER_OBJECT DriverObject
,
2315 IN PDEVICE_OBJECT DeviceObject
,
2316 IN
struct _HW_INITIALIZATION_DATA
*HwInitializationData
,
2317 IN OUT PPORT_CONFIGURATION_INFORMATION PortConfig
,
2318 IN PUNICODE_STRING RegistryPath
,
2320 IN OUT PPCI_SLOT_NUMBER NextSlotNumber
)
2322 PCI_COMMON_CONFIG PciConfig
;
2323 PCI_SLOT_NUMBER SlotNumber
;
2326 ULONG FunctionNumber
;
2327 CHAR VendorIdString
[8];
2328 CHAR DeviceIdString
[8];
2329 UNICODE_STRING UnicodeStr
;
2330 PCM_RESOURCE_LIST ResourceList
= NULL
;
2333 DPRINT ("SpiGetPciConfiguration() called\n");
2335 SlotNumber
.u
.AsULONG
= 0;
2337 /* Loop through all devices */
2338 for (DeviceNumber
= NextSlotNumber
->u
.bits
.DeviceNumber
; DeviceNumber
< PCI_MAX_DEVICES
; DeviceNumber
++)
2340 SlotNumber
.u
.bits
.DeviceNumber
= DeviceNumber
;
2342 /* Loop through all functions */
2343 for (FunctionNumber
= NextSlotNumber
->u
.bits
.FunctionNumber
; FunctionNumber
< PCI_MAX_FUNCTION
; FunctionNumber
++)
2345 SlotNumber
.u
.bits
.FunctionNumber
= FunctionNumber
;
2347 /* Get PCI config bytes */
2348 DataSize
= HalGetBusData(PCIConfiguration
,
2350 SlotNumber
.u
.AsULONG
,
2354 /* If result of HalGetBusData is 0, then the bus is wrong */
2358 /* If result is PCI_INVALID_VENDORID, then this device has no more
2360 if (PciConfig
.VendorID
== PCI_INVALID_VENDORID
)
2363 sprintf (VendorIdString
, "%04hx", PciConfig
.VendorID
);
2364 sprintf (DeviceIdString
, "%04hx", PciConfig
.DeviceID
);
2366 if (_strnicmp(VendorIdString
, HwInitializationData
->VendorId
, HwInitializationData
->VendorIdLength
) ||
2367 _strnicmp(DeviceIdString
, HwInitializationData
->DeviceId
, HwInitializationData
->DeviceIdLength
))
2369 /* It is not our device */
2373 DPRINT("Found device 0x%04hx 0x%04hx at %1lu %2lu %1lu\n",
2377 SlotNumber
.u
.bits
.DeviceNumber
,
2378 SlotNumber
.u
.bits
.FunctionNumber
);
2381 RtlInitUnicodeString(&UnicodeStr
, L
"ScsiAdapter");
2382 Status
= HalAssignSlotResources(RegistryPath
,
2388 SlotNumber
.u
.AsULONG
,
2391 if (!NT_SUCCESS(Status
))
2394 /* Create configuration information */
2395 SpiResourceToConfig(HwInitializationData
,
2399 /* Free the resource list */
2400 ExFreePool(ResourceList
);
2402 /* Set dev & fn numbers */
2403 NextSlotNumber
->u
.bits
.DeviceNumber
= DeviceNumber
;
2404 NextSlotNumber
->u
.bits
.FunctionNumber
= FunctionNumber
+ 1;
2406 /* Save the slot number */
2407 PortConfig
->SlotNumber
= SlotNumber
.u
.AsULONG
;
2411 NextSlotNumber
->u
.bits
.FunctionNumber
= 0;
2414 NextSlotNumber
->u
.bits
.DeviceNumber
= 0;
2415 DPRINT ("No device found\n");
2422 /**********************************************************************
2424 * ScsiPortCreateClose
2427 * Answer requests for Create/Close calls: a null operation.
2434 * Pointer to a device object.
2437 * Pointer to an IRP.
2443 static NTSTATUS NTAPI
2444 ScsiPortCreateClose(IN PDEVICE_OBJECT DeviceObject
,
2447 DPRINT("ScsiPortCreateClose()\n");
2449 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
2450 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2452 return STATUS_SUCCESS
;
2456 SpiHandleAttachRelease(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
2459 PSCSI_LUN_INFO LunInfo
;
2460 PIO_STACK_LOCATION IrpStack
;
2461 PDEVICE_OBJECT DeviceObject
;
2462 PSCSI_REQUEST_BLOCK Srb
;
2465 /* Get pointer to the SRB */
2466 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
2467 Srb
= (PSCSI_REQUEST_BLOCK
)IrpStack
->Parameters
.Others
.Argument1
;
2469 /* Check if PathId matches number of buses */
2470 if (DeviceExtension
->BusesConfig
== NULL
||
2471 DeviceExtension
->BusesConfig
->NumberOfBuses
<= Srb
->PathId
)
2473 Srb
->SrbStatus
= SRB_STATUS_NO_DEVICE
;
2474 return STATUS_DEVICE_DOES_NOT_EXIST
;
2477 /* Get pointer to LunInfo */
2478 LunInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Srb
->PathId
]->LunInfo
;
2480 /* Find matching LunInfo */
2483 if (LunInfo
->PathId
== Srb
->PathId
&&
2484 LunInfo
->TargetId
== Srb
->TargetId
&&
2485 LunInfo
->Lun
== Srb
->Lun
)
2490 LunInfo
= LunInfo
->Next
;
2493 /* If we couldn't find it - exit */
2494 if (LunInfo
== NULL
)
2495 return STATUS_DEVICE_DOES_NOT_EXIST
;
2499 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
2501 /* Release, if asked */
2502 if (Srb
->Function
== SRB_FUNCTION_RELEASE_DEVICE
)
2504 LunInfo
->DeviceClaimed
= FALSE
;
2505 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2506 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2508 return STATUS_SUCCESS
;
2511 /* Attach, if not already claimed */
2512 if (LunInfo
->DeviceClaimed
)
2514 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2515 Srb
->SrbStatus
= SRB_STATUS_BUSY
;
2517 return STATUS_DEVICE_BUSY
;
2520 /* Save the device object */
2521 DeviceObject
= LunInfo
->DeviceObject
;
2523 if (Srb
->Function
== SRB_FUNCTION_CLAIM_DEVICE
)
2524 LunInfo
->DeviceClaimed
= TRUE
;
2526 if (Srb
->Function
== SRB_FUNCTION_ATTACH_DEVICE
)
2527 LunInfo
->DeviceObject
= Srb
->DataBuffer
;
2529 Srb
->DataBuffer
= DeviceObject
;
2531 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2532 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2534 return STATUS_SUCCESS
;
2538 /**********************************************************************
2540 * ScsiPortDispatchScsi
2543 * Answer requests for SCSI calls
2549 * Standard dispatch arguments
2555 static NTSTATUS NTAPI
2556 ScsiPortDispatchScsi(IN PDEVICE_OBJECT DeviceObject
,
2559 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
2560 PSCSI_PORT_LUN_EXTENSION LunExtension
;
2561 PIO_STACK_LOCATION Stack
;
2562 PSCSI_REQUEST_BLOCK Srb
;
2564 NTSTATUS Status
= STATUS_SUCCESS
;
2565 PIRP NextIrp
, IrpList
;
2566 PKDEVICE_QUEUE_ENTRY Entry
;
2568 DPRINT("ScsiPortDispatchScsi(DeviceObject %p Irp %p)\n",
2571 DeviceExtension
= DeviceObject
->DeviceExtension
;
2572 Stack
= IoGetCurrentIrpStackLocation(Irp
);
2574 Srb
= Stack
->Parameters
.Scsi
.Srb
;
2577 DPRINT1("ScsiPortDispatchScsi() called with Srb = NULL!\n");
2578 Status
= STATUS_UNSUCCESSFUL
;
2580 Irp
->IoStatus
.Status
= Status
;
2581 Irp
->IoStatus
.Information
= 0;
2583 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2588 DPRINT("Srb: %p\n", Srb
);
2589 DPRINT("Srb->Function: %lu\n", Srb
->Function
);
2590 DPRINT("PathId: %lu TargetId: %lu Lun: %lu\n", Srb
->PathId
, Srb
->TargetId
, Srb
->Lun
);
2592 LunExtension
= SpiGetLunExtension(DeviceExtension
,
2596 if (LunExtension
== NULL
)
2598 DPRINT("ScsiPortDispatchScsi() called with an invalid LUN\n");
2599 Status
= STATUS_NO_SUCH_DEVICE
;
2601 Srb
->SrbStatus
= SRB_STATUS_NO_DEVICE
;
2602 Irp
->IoStatus
.Status
= Status
;
2603 Irp
->IoStatus
.Information
= 0;
2605 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2610 switch (Srb
->Function
)
2612 case SRB_FUNCTION_SHUTDOWN
:
2613 case SRB_FUNCTION_FLUSH
:
2614 DPRINT (" SRB_FUNCTION_SHUTDOWN or FLUSH\n");
2615 if (DeviceExtension
->CachesData
== FALSE
)
2617 /* All success here */
2618 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2619 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
2620 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2621 return STATUS_SUCCESS
;
2623 /* Fall through to a usual execute operation */
2625 case SRB_FUNCTION_EXECUTE_SCSI
:
2626 case SRB_FUNCTION_IO_CONTROL
:
2627 /* Mark IRP as pending in all cases */
2628 IoMarkIrpPending(Irp
);
2630 if (Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
)
2632 /* Start IO directly */
2633 IoStartPacket(DeviceObject
, Irp
, NULL
, NULL
);
2639 /* We need to be at DISPATCH_LEVEL */
2640 KeRaiseIrql (DISPATCH_LEVEL
, &oldIrql
);
2642 /* Insert IRP into the queue */
2643 if (!KeInsertByKeyDeviceQueue(&LunExtension
->DeviceQueue
,
2644 &Irp
->Tail
.Overlay
.DeviceQueueEntry
,
2647 /* It means the queue is empty, and we just start this request */
2648 IoStartPacket(DeviceObject
, Irp
, NULL
, NULL
);
2651 /* Back to the old IRQL */
2652 KeLowerIrql (oldIrql
);
2654 return STATUS_PENDING
;
2656 case SRB_FUNCTION_CLAIM_DEVICE
:
2657 case SRB_FUNCTION_ATTACH_DEVICE
:
2658 DPRINT (" SRB_FUNCTION_CLAIM_DEVICE or ATTACH\n");
2660 /* Reference device object and keep the device object */
2661 Status
= SpiHandleAttachRelease(DeviceExtension
, Irp
);
2664 case SRB_FUNCTION_RELEASE_DEVICE
:
2665 DPRINT (" SRB_FUNCTION_RELEASE_DEVICE\n");
2667 /* Dereference device object and clear the device object */
2668 Status
= SpiHandleAttachRelease(DeviceExtension
, Irp
);
2671 case SRB_FUNCTION_RELEASE_QUEUE
:
2672 DPRINT(" SRB_FUNCTION_RELEASE_QUEUE\n");
2674 /* Guard with the spinlock */
2675 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
2677 if (!(LunExtension
->Flags
& LUNEX_FROZEN_QUEUE
))
2679 DPRINT("Queue is not frozen really\n");
2681 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2682 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2683 Status
= STATUS_SUCCESS
;
2688 /* Unfreeze the queue */
2689 LunExtension
->Flags
&= ~LUNEX_FROZEN_QUEUE
;
2691 if (LunExtension
->SrbInfo
.Srb
== NULL
)
2693 /* Get next logical unit request */
2694 SpiGetNextRequestFromLun(DeviceExtension
, LunExtension
);
2696 /* SpiGetNextRequestFromLun() releases the spinlock */
2701 DPRINT("The queue has active request\n");
2702 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2706 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2707 Status
= STATUS_SUCCESS
;
2710 case SRB_FUNCTION_FLUSH_QUEUE
:
2711 DPRINT(" SRB_FUNCTION_FLUSH_QUEUE\n");
2713 /* Guard with the spinlock */
2714 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
2716 if (!(LunExtension
->Flags
& LUNEX_FROZEN_QUEUE
))
2718 DPRINT("Queue is not frozen really\n");
2720 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2721 Status
= STATUS_INVALID_DEVICE_REQUEST
;
2725 /* Make sure there is no active request */
2726 ASSERT(LunExtension
->SrbInfo
.Srb
== NULL
);
2728 /* Compile a list from the device queue */
2730 while ((Entry
= KeRemoveDeviceQueue(&LunExtension
->DeviceQueue
)) != NULL
)
2732 NextIrp
= CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.DeviceQueueEntry
);
2735 Stack
= IoGetCurrentIrpStackLocation(NextIrp
);
2736 Srb
= Stack
->Parameters
.Scsi
.Srb
;
2739 Srb
->SrbStatus
= SRB_STATUS_REQUEST_FLUSHED
;
2740 NextIrp
->IoStatus
.Status
= STATUS_UNSUCCESSFUL
;
2742 /* Add then to the list */
2743 NextIrp
->Tail
.Overlay
.ListEntry
.Flink
= (PLIST_ENTRY
)IrpList
;
2747 /* Unfreeze the queue */
2748 LunExtension
->Flags
&= ~LUNEX_FROZEN_QUEUE
;
2750 /* Release the spinlock */
2751 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2753 /* Complete those requests */
2757 IrpList
= (PIRP
)NextIrp
->Tail
.Overlay
.ListEntry
.Flink
;
2759 IoCompleteRequest(NextIrp
, 0);
2762 Status
= STATUS_SUCCESS
;
2766 DPRINT1("SRB function not implemented (Function %lu)\n", Srb
->Function
);
2767 Status
= STATUS_NOT_IMPLEMENTED
;
2771 Irp
->IoStatus
.Status
= Status
;
2773 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2779 /**********************************************************************
2781 * ScsiPortDeviceControl
2784 * Answer requests for device control calls
2790 * Standard dispatch arguments
2796 static NTSTATUS NTAPI
2797 ScsiPortDeviceControl(IN PDEVICE_OBJECT DeviceObject
,
2800 PIO_STACK_LOCATION Stack
;
2801 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
2802 PDUMP_POINTERS DumpPointers
;
2805 DPRINT("ScsiPortDeviceControl()\n");
2807 Irp
->IoStatus
.Information
= 0;
2809 Stack
= IoGetCurrentIrpStackLocation(Irp
);
2810 DeviceExtension
= DeviceObject
->DeviceExtension
;
2812 switch (Stack
->Parameters
.DeviceIoControl
.IoControlCode
)
2814 case IOCTL_SCSI_GET_DUMP_POINTERS
:
2815 DPRINT(" IOCTL_SCSI_GET_DUMP_POINTERS\n");
2817 if (Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(DUMP_POINTERS
))
2819 Status
= STATUS_BUFFER_OVERFLOW
;
2820 Irp
->IoStatus
.Information
= sizeof(DUMP_POINTERS
);
2824 DumpPointers
= Irp
->AssociatedIrp
.SystemBuffer
;
2825 DumpPointers
->DeviceObject
= DeviceObject
;
2828 Status
= STATUS_SUCCESS
;
2829 Irp
->IoStatus
.Information
= sizeof(DUMP_POINTERS
);
2832 case IOCTL_SCSI_GET_CAPABILITIES
:
2833 DPRINT(" IOCTL_SCSI_GET_CAPABILITIES\n");
2834 if (Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
== sizeof(PVOID
))
2836 *((PVOID
*)Irp
->AssociatedIrp
.SystemBuffer
) = &DeviceExtension
->PortCapabilities
;
2838 Irp
->IoStatus
.Information
= sizeof(PVOID
);
2839 Status
= STATUS_SUCCESS
;
2843 if (Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(IO_SCSI_CAPABILITIES
))
2845 Status
= STATUS_BUFFER_TOO_SMALL
;
2849 RtlCopyMemory(Irp
->AssociatedIrp
.SystemBuffer
,
2850 &DeviceExtension
->PortCapabilities
,
2851 sizeof(IO_SCSI_CAPABILITIES
));
2853 Irp
->IoStatus
.Information
= sizeof(IO_SCSI_CAPABILITIES
);
2854 Status
= STATUS_SUCCESS
;
2857 case IOCTL_SCSI_GET_INQUIRY_DATA
:
2858 DPRINT(" IOCTL_SCSI_GET_INQUIRY_DATA\n");
2860 /* Copy inquiry data to the port device extension */
2861 Status
= SpiGetInquiryData(DeviceExtension
, Irp
);
2864 case IOCTL_SCSI_MINIPORT
:
2865 DPRINT1("IOCTL_SCSI_MINIPORT unimplemented!\n");
2866 Status
= STATUS_NOT_IMPLEMENTED
;
2869 case IOCTL_SCSI_PASS_THROUGH
:
2870 DPRINT1("IOCTL_SCSI_PASS_THROUGH unimplemented!\n");
2871 Status
= STATUS_NOT_IMPLEMENTED
;
2875 DPRINT1(" unknown ioctl code: 0x%lX\n", Stack
->Parameters
.DeviceIoControl
.IoControlCode
);
2876 Status
= STATUS_NOT_IMPLEMENTED
;
2880 /* Complete the request with the given status */
2881 Irp
->IoStatus
.Status
= Status
;
2882 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2889 ScsiPortStartIo(IN PDEVICE_OBJECT DeviceObject
,
2892 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
2893 PSCSI_PORT_LUN_EXTENSION LunExtension
;
2894 PIO_STACK_LOCATION IrpStack
;
2895 PSCSI_REQUEST_BLOCK Srb
;
2896 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
2900 DPRINT("ScsiPortStartIo() called!\n");
2902 DeviceExtension
= DeviceObject
->DeviceExtension
;
2903 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
2905 DPRINT("DeviceExtension %p\n", DeviceExtension
);
2907 Srb
= IrpStack
->Parameters
.Scsi
.Srb
;
2909 /* Apply "default" flags */
2910 Srb
->SrbFlags
|= DeviceExtension
->SrbFlags
;
2912 /* Get LUN extension */
2913 LunExtension
= SpiGetLunExtension(DeviceExtension
,
2918 if (DeviceExtension
->NeedSrbDataAlloc
||
2919 DeviceExtension
->NeedSrbExtensionAlloc
)
2922 SrbInfo
= SpiAllocateSrbStructures(DeviceExtension
,
2926 /* Couldn't alloc one or both data structures, return */
2927 if (SrbInfo
== NULL
)
2929 /* We have to call IoStartNextPacket, because this request
2931 if (LunExtension
->Flags
& LUNEX_REQUEST_PENDING
)
2932 IoStartNextPacket(DeviceObject
, FALSE
);
2939 /* No allocations are needed */
2940 SrbInfo
= &LunExtension
->SrbInfo
;
2941 Srb
->SrbExtension
= NULL
;
2942 Srb
->QueueTag
= SP_UNTAGGED
;
2945 /* Increase sequence number of SRB */
2946 if (!SrbInfo
->SequenceNumber
)
2948 /* Increase global sequence number */
2949 DeviceExtension
->SequenceNumber
++;
2952 SrbInfo
->SequenceNumber
= DeviceExtension
->SequenceNumber
;
2955 /* Check some special SRBs */
2956 if (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
)
2958 /* Some special handling */
2959 DPRINT1("Abort command! Unimplemented now\n");
2966 if (Srb
->SrbFlags
& SRB_FLAGS_UNSPECIFIED_DIRECTION
)
2968 // Store the MDL virtual address in SrbInfo structure
2969 SrbInfo
->DataOffset
= MmGetMdlVirtualAddress(Irp
->MdlAddress
);
2971 if (DeviceExtension
->MapBuffers
)
2973 /* Calculate offset within DataBuffer */
2974 SrbInfo
->DataOffset
= MmGetSystemAddressForMdl(Irp
->MdlAddress
);
2975 Srb
->DataBuffer
= SrbInfo
->DataOffset
+
2976 (ULONG
)((PUCHAR
)Srb
->DataBuffer
-
2977 (PUCHAR
)MmGetMdlVirtualAddress(Irp
->MdlAddress
));
2980 if (DeviceExtension
->AdapterObject
)
2983 KeFlushIoBuffers(Irp
->MdlAddress
,
2984 Srb
->SrbFlags
& SRB_FLAGS_DATA_IN
? TRUE
: FALSE
,
2988 if (DeviceExtension
->MapRegisters
)
2990 /* Calculate number of needed map registers */
2991 SrbInfo
->NumberOfMapRegisters
= ADDRESS_AND_SIZE_TO_SPAN_PAGES(
2993 Srb
->DataTransferLength
);
2995 /* Allocate adapter channel */
2996 Status
= IoAllocateAdapterChannel(DeviceExtension
->AdapterObject
,
2997 DeviceExtension
->DeviceObject
,
2998 SrbInfo
->NumberOfMapRegisters
,
3002 if (!NT_SUCCESS(Status
))
3004 DPRINT1("IoAllocateAdapterChannel() failed!\n");
3006 Srb
->SrbStatus
= SRB_STATUS_INVALID_REQUEST
;
3007 ScsiPortNotification(RequestComplete
,
3008 DeviceExtension
+ 1,
3011 ScsiPortNotification(NextRequest
,
3012 DeviceExtension
+ 1);
3014 /* Request DPC for that work */
3015 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
3018 /* Control goes to SpiAdapterControl */
3023 /* Increase active request counter */
3024 CounterResult
= InterlockedIncrement(&DeviceExtension
->ActiveRequestCounter
);
3026 if (CounterResult
== 0 &&
3027 DeviceExtension
->AdapterObject
!= NULL
&&
3028 !DeviceExtension
->MapRegisters
)
3030 IoAllocateAdapterChannel(
3031 DeviceExtension
->AdapterObject
,
3033 DeviceExtension
->PortCapabilities
.MaximumPhysicalPages
,
3034 ScsiPortAllocateAdapterChannel
,
3041 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
3043 if (!KeSynchronizeExecution(DeviceExtension
->Interrupt
[0],
3044 ScsiPortStartPacket
,
3047 DPRINT("Synchronization failed!\n");
3049 Irp
->IoStatus
.Status
= STATUS_UNSUCCESSFUL
;
3050 Irp
->IoStatus
.Information
= 0;
3051 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3053 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
3057 /* Release the spinlock only */
3058 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3062 DPRINT("ScsiPortStartIo() done\n");
3066 static BOOLEAN NTAPI
3067 ScsiPortStartPacket(IN OUT PVOID Context
)
3069 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
3070 PIO_STACK_LOCATION IrpStack
;
3071 PSCSI_REQUEST_BLOCK Srb
;
3072 PDEVICE_OBJECT DeviceObject
= (PDEVICE_OBJECT
)Context
;
3073 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3074 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
3078 DPRINT("ScsiPortStartPacket() called\n");
3080 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
3082 IrpStack
= IoGetCurrentIrpStackLocation(DeviceObject
->CurrentIrp
);
3083 Srb
= IrpStack
->Parameters
.Scsi
.Srb
;
3085 /* Get LUN extension */
3086 LunExtension
= SpiGetLunExtension(DeviceExtension
,
3091 /* Check if we are in a reset state */
3092 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_RESET
)
3094 /* Mark the we've got requests while being in the reset state */
3095 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_RESET_REQUEST
;
3099 /* Set the time out value */
3100 DeviceExtension
->TimerCount
= Srb
->TimeOutValue
;
3103 DeviceExtension
->Flags
|= SCSI_PORT_DEVICE_BUSY
;
3105 if (LunExtension
->RequestTimeout
!= -1)
3107 /* Timer already active */
3112 /* It hasn't been initialized yet */
3113 LunExtension
->RequestTimeout
= Srb
->TimeOutValue
;
3117 if (Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
)
3119 /* Handle bypass-requests */
3121 /* Is this an abort request? */
3122 if (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
)
3124 /* Get pointer to SRB info structure */
3125 SrbInfo
= SpiGetSrbData(DeviceExtension
,
3131 /* Check if the request is still "active" */
3132 if (SrbInfo
== NULL
||
3133 SrbInfo
->Srb
== NULL
||
3134 !(SrbInfo
->Srb
->SrbFlags
& SRB_FLAGS_IS_ACTIVE
))
3136 /* It's not, mark it as active then */
3137 Srb
->SrbFlags
|= SRB_FLAGS_IS_ACTIVE
;
3140 LunExtension
->RequestTimeout
= -1;
3142 DPRINT("Request has been already completed, but abort request came\n");
3143 Srb
->SrbStatus
= SRB_STATUS_ABORT_FAILED
;
3145 /* Notify about request complete */
3146 ScsiPortNotification(RequestComplete
,
3147 DeviceExtension
->MiniPortDeviceExtension
,
3150 /* and about readiness for the next request */
3151 ScsiPortNotification(NextRequest
,
3152 DeviceExtension
->MiniPortDeviceExtension
);
3154 /* They might ask for some work, so queue the DPC for them */
3155 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
3157 /* We're done in this branch */
3163 /* Add number of queued requests */
3164 LunExtension
->QueueCount
++;
3167 /* Bypass requests don't need request sense */
3168 LunExtension
->Flags
&= ~LUNEX_NEED_REQUEST_SENSE
;
3170 /* Is disconnect disabled for this request? */
3171 if (Srb
->SrbFlags
& SRB_FLAGS_DISABLE_DISCONNECT
)
3173 /* Set the corresponding flag */
3174 DeviceExtension
->Flags
&= ~SCSI_PORT_DISCONNECT_ALLOWED
;
3177 /* Transfer timeout value from Srb to Lun */
3178 LunExtension
->RequestTimeout
= Srb
->TimeOutValue
;
3182 if (Srb
->SrbFlags
& SRB_FLAGS_DISABLE_DISCONNECT
)
3184 /* It's a disconnect, so no more requests can go */
3185 DeviceExtension
->Flags
&= ~SCSI_PORT_DISCONNECT_ALLOWED
;
3188 LunExtension
->Flags
|= SCSI_PORT_LU_ACTIVE
;
3190 /* Increment queue count */
3191 LunExtension
->QueueCount
++;
3193 /* If it's tagged - special thing */
3194 if (Srb
->QueueTag
!= SP_UNTAGGED
)
3196 SrbInfo
= &DeviceExtension
->SrbInfo
[Srb
->QueueTag
- 1];
3198 /* Chek for consistency */
3199 ASSERT(SrbInfo
->Requests
.Blink
== NULL
);
3201 /* Insert it into the list of requests */
3202 InsertTailList(&LunExtension
->SrbInfo
.Requests
, &SrbInfo
->Requests
);
3206 /* Mark this Srb active */
3207 Srb
->SrbFlags
|= SRB_FLAGS_IS_ACTIVE
;
3209 /* Call HwStartIo routine */
3210 Result
= DeviceExtension
->HwStartIo(&DeviceExtension
->MiniPortDeviceExtension
,
3213 /* If notification is needed, then request a DPC */
3214 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
3215 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
3220 IO_ALLOCATION_ACTION
3222 SpiAdapterControl(PDEVICE_OBJECT DeviceObject
,
3224 PVOID MapRegisterBase
,
3227 PSCSI_REQUEST_BLOCK Srb
;
3228 PSCSI_SG_ADDRESS ScatterGatherList
;
3230 PIO_STACK_LOCATION IrpStack
;
3231 ULONG TotalLength
= 0;
3232 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
3233 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
3235 BOOLEAN WriteToDevice
;
3237 /* Get pointers to SrbInfo and DeviceExtension */
3238 SrbInfo
= (PSCSI_REQUEST_BLOCK_INFO
)Context
;
3239 DeviceExtension
= DeviceObject
->DeviceExtension
;
3241 /* Get pointer to SRB */
3242 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
3243 Srb
= (PSCSI_REQUEST_BLOCK
)IrpStack
->Parameters
.Others
.Argument1
;
3245 /* Depending on the map registers number, we allocate
3246 either from NonPagedPool, or from our static list */
3247 if (SrbInfo
->NumberOfMapRegisters
> MAX_SG_LIST
)
3249 SrbInfo
->ScatterGather
= ExAllocatePoolWithTag(
3250 NonPagedPool
, SrbInfo
->NumberOfMapRegisters
* sizeof(SCSI_SG_ADDRESS
), TAG_SCSIPORT
);
3252 if (SrbInfo
->ScatterGather
== NULL
)
3255 Srb
->SrbFlags
|= SRB_FLAGS_SGLIST_FROM_POOL
;
3259 SrbInfo
->ScatterGather
= SrbInfo
->ScatterGatherList
;
3262 /* Use chosen SG list source */
3263 ScatterGatherList
= SrbInfo
->ScatterGather
;
3265 /* Save map registers base */
3266 SrbInfo
->BaseOfMapRegister
= MapRegisterBase
;
3268 /* Determine WriteToDevice flag */
3269 WriteToDevice
= Srb
->SrbFlags
& SRB_FLAGS_DATA_OUT
? TRUE
: FALSE
;
3271 /* Get virtual address of the data buffer */
3272 DataVA
= (PUCHAR
)MmGetMdlVirtualAddress(Irp
->MdlAddress
) +
3273 ((PCHAR
)Srb
->DataBuffer
- SrbInfo
->DataOffset
);
3275 /* Build the actual SG list */
3276 while (TotalLength
< Srb
->DataTransferLength
)
3278 if (!ScatterGatherList
)
3281 ScatterGatherList
->Length
= Srb
->DataTransferLength
- TotalLength
;
3282 ScatterGatherList
->PhysicalAddress
= IoMapTransfer(NULL
,
3285 DataVA
+ TotalLength
,
3286 &ScatterGatherList
->Length
,
3289 TotalLength
+= ScatterGatherList
->Length
;
3290 ScatterGatherList
++;
3293 /* Schedule an active request */
3294 InterlockedIncrement(&DeviceExtension
->ActiveRequestCounter
);
3295 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &CurrentIrql
);
3296 KeSynchronizeExecution(DeviceExtension
->Interrupt
[0],
3297 ScsiPortStartPacket
,
3299 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, CurrentIrql
);
3301 return DeallocateObjectKeepRegisters
;
3304 static PSCSI_PORT_LUN_EXTENSION
3305 SpiAllocateLunExtension (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
)
3307 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3308 ULONG LunExtensionSize
;
3310 DPRINT("SpiAllocateLunExtension (%p)\n",
3313 /* Round LunExtensionSize first to the sizeof LONGLONG */
3314 LunExtensionSize
= (DeviceExtension
->LunExtensionSize
+
3315 sizeof(LONGLONG
) - 1) & ~(sizeof(LONGLONG
) - 1);
3317 LunExtensionSize
+= sizeof(SCSI_PORT_LUN_EXTENSION
);
3318 DPRINT("LunExtensionSize %lu\n", LunExtensionSize
);
3320 LunExtension
= ExAllocatePoolWithTag(NonPagedPool
, LunExtensionSize
, TAG_SCSIPORT
);
3321 if (LunExtension
== NULL
)
3323 DPRINT1("Out of resources!\n");
3327 /* Zero everything */
3328 RtlZeroMemory(LunExtension
, LunExtensionSize
);
3330 /* Initialize a list of requests */
3331 InitializeListHead(&LunExtension
->SrbInfo
.Requests
);
3333 /* Initialize timeout counter */
3334 LunExtension
->RequestTimeout
= -1;
3336 /* Set maximum queue size */
3337 LunExtension
->MaxQueueCount
= 256;
3339 /* Initialize request queue */
3340 KeInitializeDeviceQueue (&LunExtension
->DeviceQueue
);
3342 return LunExtension
;
3345 static PSCSI_PORT_LUN_EXTENSION
3346 SpiGetLunExtension (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
3351 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3353 DPRINT("SpiGetLunExtension(%p %u %u %u) called\n",
3354 DeviceExtension
, PathId
, TargetId
, Lun
);
3356 /* Get appropriate list */
3357 LunExtension
= DeviceExtension
->LunExtensionList
[(TargetId
+ Lun
) % LUS_NUMBER
];
3359 /* Iterate it until we find what we need */
3360 while (LunExtension
)
3362 if (LunExtension
->TargetId
== TargetId
&&
3363 LunExtension
->Lun
== Lun
&&
3364 LunExtension
->PathId
== PathId
)
3366 /* All matches, return */
3367 return LunExtension
;
3370 /* Advance to the next item */
3371 LunExtension
= LunExtension
->Next
;
3374 /* We did not find anything */
3375 DPRINT("Nothing found\n");
3379 static PSCSI_REQUEST_BLOCK_INFO
3380 SpiAllocateSrbStructures(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
3381 PSCSI_PORT_LUN_EXTENSION LunExtension
,
3382 PSCSI_REQUEST_BLOCK Srb
)
3385 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
3387 /* Spinlock must be held while this function executes */
3388 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
3390 /* Allocate SRB data structure */
3391 if (DeviceExtension
->NeedSrbDataAlloc
)
3393 /* Treat the abort request in a special way */
3394 if (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
)
3396 SrbInfo
= SpiGetSrbData(DeviceExtension
,
3402 else if (Srb
->SrbFlags
&
3403 (SRB_FLAGS_QUEUE_ACTION_ENABLE
| SRB_FLAGS_NO_QUEUE_FREEZE
) &&
3404 !(Srb
->SrbFlags
& SRB_FLAGS_DISABLE_DISCONNECT
)
3407 /* Do not process tagged commands if need request sense is set */
3408 if (LunExtension
->Flags
& LUNEX_NEED_REQUEST_SENSE
)
3410 ASSERT(!(LunExtension
->Flags
& LUNEX_REQUEST_PENDING
));
3412 LunExtension
->PendingRequest
= Srb
->OriginalRequest
;
3413 LunExtension
->Flags
|= LUNEX_REQUEST_PENDING
| SCSI_PORT_LU_ACTIVE
;
3415 /* Relese the spinlock and return */
3416 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3420 ASSERT(LunExtension
->SrbInfo
.Srb
== NULL
);
3421 SrbInfo
= DeviceExtension
->FreeSrbInfo
;
3423 if (SrbInfo
== NULL
)
3425 /* No SRB structures left in the list. We have to leave
3426 and wait while we are called again */
3428 DeviceExtension
->Flags
|= SCSI_PORT_REQUEST_PENDING
;
3429 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3433 DeviceExtension
->FreeSrbInfo
= (PSCSI_REQUEST_BLOCK_INFO
)SrbInfo
->Requests
.Flink
;
3435 /* QueueTag must never be 0, so +1 to it */
3436 Srb
->QueueTag
= (UCHAR
)(SrbInfo
- DeviceExtension
->SrbInfo
) + 1;
3440 /* Usual untagged command */
3442 (!IsListEmpty(&LunExtension
->SrbInfo
.Requests
) ||
3443 LunExtension
->Flags
& LUNEX_NEED_REQUEST_SENSE
) &&
3444 !(Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
)
3447 /* Mark it as pending and leave */
3448 ASSERT(!(LunExtension
->Flags
& LUNEX_REQUEST_PENDING
));
3449 LunExtension
->Flags
|= LUNEX_REQUEST_PENDING
| SCSI_PORT_LU_ACTIVE
;
3450 LunExtension
->PendingRequest
= Srb
->OriginalRequest
;
3452 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3456 Srb
->QueueTag
= SP_UNTAGGED
;
3457 SrbInfo
= &LunExtension
->SrbInfo
;
3462 Srb
->QueueTag
= SP_UNTAGGED
;
3463 SrbInfo
= &LunExtension
->SrbInfo
;
3466 /* Allocate SRB extension structure */
3467 if (DeviceExtension
->NeedSrbExtensionAlloc
)
3469 /* Check the list of free extensions */
3470 SrbExtension
= DeviceExtension
->FreeSrbExtensions
;
3472 /* If no free extensions... */
3473 if (SrbExtension
== NULL
)
3476 if (Srb
->Function
!= SRB_FUNCTION_ABORT_COMMAND
&&
3477 Srb
->QueueTag
!= SP_UNTAGGED
)
3479 SrbInfo
->Requests
.Blink
= NULL
;
3480 SrbInfo
->Requests
.Flink
= (PLIST_ENTRY
)DeviceExtension
->FreeSrbInfo
;
3481 DeviceExtension
->FreeSrbInfo
= SrbInfo
;
3484 /* Return, in order to be called again later */
3485 DeviceExtension
->Flags
|= SCSI_PORT_REQUEST_PENDING
;
3486 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3490 /* Remove that free SRB extension from the list (since
3491 we're going to use it) */
3492 DeviceExtension
->FreeSrbExtensions
= *((PVOID
*)SrbExtension
);
3494 /* Spinlock can be released now */
3495 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3497 Srb
->SrbExtension
= SrbExtension
;
3499 if (Srb
->SenseInfoBuffer
!= NULL
&&
3500 DeviceExtension
->SupportsAutoSense
)
3502 /* Store pointer to the SenseInfo buffer */
3503 SrbInfo
->SaveSenseRequest
= Srb
->SenseInfoBuffer
;
3505 /* Does data fit the buffer? */
3506 if (Srb
->SenseInfoBufferLength
> sizeof(SENSE_DATA
))
3508 /* No, disabling autosense at all */
3509 Srb
->SrbFlags
|= SRB_FLAGS_DISABLE_AUTOSENSE
;
3513 /* Yes, update the buffer pointer */
3514 Srb
->SenseInfoBuffer
= SrbExtension
+ DeviceExtension
->SrbExtensionSize
;
3521 Srb
->SrbExtension
= NULL
;
3522 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3530 SpiSendInquiry (IN PDEVICE_OBJECT DeviceObject
,
3531 IN PSCSI_LUN_INFO LunInfo
)
3533 IO_STATUS_BLOCK IoStatusBlock
;
3534 PIO_STACK_LOCATION IrpStack
;
3539 PINQUIRYDATA InquiryBuffer
;
3540 PSENSE_DATA SenseBuffer
;
3541 BOOLEAN KeepTrying
= TRUE
;
3542 ULONG RetryCount
= 0;
3543 SCSI_REQUEST_BLOCK Srb
;
3545 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3546 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
3548 DPRINT ("SpiSendInquiry() called\n");
3550 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
3552 InquiryBuffer
= ExAllocatePoolWithTag (NonPagedPool
, INQUIRYDATABUFFERSIZE
, TAG_SCSIPORT
);
3553 if (InquiryBuffer
== NULL
)
3554 return STATUS_INSUFFICIENT_RESOURCES
;
3556 SenseBuffer
= ExAllocatePoolWithTag (NonPagedPool
, SENSE_BUFFER_SIZE
, TAG_SCSIPORT
);
3557 if (SenseBuffer
== NULL
)
3559 ExFreePool(InquiryBuffer
);
3560 return STATUS_INSUFFICIENT_RESOURCES
;
3565 /* Initialize event for waiting */
3566 KeInitializeEvent(&Event
,
3571 Irp
= IoBuildDeviceIoControlRequest(IOCTL_SCSI_EXECUTE_IN
,
3576 INQUIRYDATABUFFERSIZE
,
3582 DPRINT("IoBuildDeviceIoControlRequest() failed\n");
3583 return STATUS_INSUFFICIENT_RESOURCES
;
3587 RtlZeroMemory(&Srb
, sizeof(SCSI_REQUEST_BLOCK
));
3589 Srb
.Length
= sizeof(SCSI_REQUEST_BLOCK
);
3590 Srb
.OriginalRequest
= Irp
;
3591 Srb
.PathId
= LunInfo
->PathId
;
3592 Srb
.TargetId
= LunInfo
->TargetId
;
3593 Srb
.Lun
= LunInfo
->Lun
;
3594 Srb
.Function
= SRB_FUNCTION_EXECUTE_SCSI
;
3595 Srb
.SrbFlags
= SRB_FLAGS_DATA_IN
| SRB_FLAGS_DISABLE_SYNCH_TRANSFER
;
3596 Srb
.TimeOutValue
= 4;
3599 Srb
.SenseInfoBuffer
= SenseBuffer
;
3600 Srb
.SenseInfoBufferLength
= SENSE_BUFFER_SIZE
;
3602 Srb
.DataBuffer
= InquiryBuffer
;
3603 Srb
.DataTransferLength
= INQUIRYDATABUFFERSIZE
;
3605 /* Attach Srb to the Irp */
3606 IrpStack
= IoGetNextIrpStackLocation (Irp
);
3607 IrpStack
->Parameters
.Scsi
.Srb
= &Srb
;
3610 Cdb
= (PCDB
)Srb
.Cdb
;
3611 Cdb
->CDB6INQUIRY
.OperationCode
= SCSIOP_INQUIRY
;
3612 Cdb
->CDB6INQUIRY
.LogicalUnitNumber
= LunInfo
->Lun
;
3613 Cdb
->CDB6INQUIRY
.AllocationLength
= INQUIRYDATABUFFERSIZE
;
3615 /* Call the driver */
3616 Status
= IoCallDriver(DeviceObject
, Irp
);
3618 /* Wait for it to complete */
3619 if (Status
== STATUS_PENDING
)
3621 DPRINT("SpiSendInquiry(): Waiting for the driver to process request...\n");
3622 KeWaitForSingleObject(&Event
,
3627 Status
= IoStatusBlock
.Status
;
3630 DPRINT("SpiSendInquiry(): Request processed by driver, status = 0x%08X\n", Status
);
3632 if (SRB_STATUS(Srb
.SrbStatus
) == SRB_STATUS_SUCCESS
)
3634 /* All fine, copy data over */
3635 RtlCopyMemory(LunInfo
->InquiryData
,
3637 INQUIRYDATABUFFERSIZE
);
3639 Status
= STATUS_SUCCESS
;
3644 DPRINT("Inquiry SRB failed with SrbStatus 0x%08X\n", Srb
.SrbStatus
);
3645 /* Check if the queue is frozen */
3646 if (Srb
.SrbStatus
& SRB_STATUS_QUEUE_FROZEN
)
3648 /* Something weird happened, deal with it (unfreeze the queue) */
3651 DPRINT("SpiSendInquiry(): the queue is frozen at TargetId %d\n", Srb
.TargetId
);
3653 LunExtension
= SpiGetLunExtension(DeviceExtension
,
3658 /* Clear frozen flag */
3659 LunExtension
->Flags
&= ~LUNEX_FROZEN_QUEUE
;
3661 /* Acquire the spinlock */
3662 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
3664 /* Process the request */
3665 SpiGetNextRequestFromLun(DeviceObject
->DeviceExtension
, LunExtension
);
3667 /* SpiGetNextRequestFromLun() releases the spinlock,
3668 so we just lower irql back to what it was before */
3672 /* Check if data overrun happened */
3673 if (SRB_STATUS(Srb
.SrbStatus
) == SRB_STATUS_DATA_OVERRUN
)
3675 DPRINT("Data overrun at TargetId %d\n", LunInfo
->TargetId
);
3676 /* Nothing dramatic, just copy data, but limiting the size */
3677 RtlCopyMemory(LunInfo
->InquiryData
,
3679 (Srb
.DataTransferLength
> INQUIRYDATABUFFERSIZE
) ?
3680 INQUIRYDATABUFFERSIZE
: Srb
.DataTransferLength
);
3682 Status
= STATUS_SUCCESS
;
3685 else if ((Srb
.SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
) &&
3686 SenseBuffer
->SenseKey
== SCSI_SENSE_ILLEGAL_REQUEST
)
3688 /* LUN is not valid, but some device responds there.
3689 Mark it as invalid anyway */
3691 Status
= STATUS_INVALID_DEVICE_REQUEST
;
3696 /* Retry a couple of times if no timeout happened */
3697 if ((RetryCount
< 2) &&
3698 (SRB_STATUS(Srb
.SrbStatus
) != SRB_STATUS_NO_DEVICE
) &&
3699 (SRB_STATUS(Srb
.SrbStatus
) != SRB_STATUS_SELECTION_TIMEOUT
))
3706 /* That's all, go to exit */
3709 /* Set status according to SRB status */
3710 if (SRB_STATUS(Srb
.SrbStatus
) == SRB_STATUS_BAD_FUNCTION
||
3711 SRB_STATUS(Srb
.SrbStatus
) == SRB_STATUS_BAD_SRB_BLOCK_LENGTH
)
3713 Status
= STATUS_INVALID_DEVICE_REQUEST
;
3717 Status
= STATUS_IO_DEVICE_ERROR
;
3725 ExFreePool(InquiryBuffer
);
3726 ExFreePool(SenseBuffer
);
3728 DPRINT("SpiSendInquiry() done with Status 0x%08X\n", Status
);
3734 /* Scans all SCSI buses */
3736 SpiScanAdapter(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
)
3738 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3742 PSCSI_BUS_SCAN_INFO BusScanInfo
;
3743 PSCSI_LUN_INFO LastLunInfo
, LunInfo
, LunInfoExists
;
3744 BOOLEAN DeviceExists
;
3749 DPRINT("SpiScanAdapter() called\n");
3751 /* Scan all buses */
3752 for (Bus
= 0; Bus
< DeviceExtension
->BusNum
; Bus
++)
3754 DPRINT(" Scanning bus %d\n", Bus
);
3757 /* Get pointer to the scan information */
3758 BusScanInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
];
3762 /* Find the last LUN info in the list */
3763 LunInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LunInfo
;
3764 LastLunInfo
= LunInfo
;
3766 while (LunInfo
!= NULL
)
3768 LastLunInfo
= LunInfo
;
3769 LunInfo
= LunInfo
->Next
;
3774 /* We need to allocate this buffer */
3775 BusScanInfo
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(SCSI_BUS_SCAN_INFO
), TAG_SCSIPORT
);
3779 DPRINT1("Out of resources!\n");
3783 /* Store the pointer in the BusScanInfo array */
3784 DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
] = BusScanInfo
;
3786 /* Fill this struct (length and bus ids for now) */
3787 BusScanInfo
->Length
= sizeof(SCSI_BUS_SCAN_INFO
);
3788 BusScanInfo
->LogicalUnitsCount
= 0;
3789 BusScanInfo
->BusIdentifier
= DeviceExtension
->PortConfig
->InitiatorBusId
[Bus
];
3790 BusScanInfo
->LunInfo
= NULL
;
3792 /* Set pointer to the last LUN info to NULL */
3796 /* Create LUN information structure */
3797 LunInfo
= ExAllocatePoolWithTag(PagedPool
, sizeof(SCSI_LUN_INFO
), TAG_SCSIPORT
);
3799 if (LunInfo
== NULL
)
3801 DPRINT1("Out of resources!\n");
3805 RtlZeroMemory(LunInfo
, sizeof(SCSI_LUN_INFO
));
3807 /* Create LunExtension */
3808 LunExtension
= SpiAllocateLunExtension (DeviceExtension
);
3810 /* And send INQUIRY to every target */
3811 for (Target
= 0; Target
< DeviceExtension
->PortConfig
->MaximumNumberOfTargets
; Target
++)
3813 /* TODO: Support scan bottom-up */
3815 /* Skip if it's the same address */
3816 if (Target
== BusScanInfo
->BusIdentifier
)
3819 /* Try to find an existing device here */
3820 DeviceExists
= FALSE
;
3821 LunInfoExists
= BusScanInfo
->LunInfo
;
3823 /* Find matching address on this bus */
3824 while (LunInfoExists
)
3826 if (LunInfoExists
->TargetId
== Target
)
3828 DeviceExists
= TRUE
;
3832 /* Advance to the next one */
3833 LunInfoExists
= LunInfoExists
->Next
;
3836 /* No need to bother rescanning, since we already did that before */
3840 /* Scan all logical units */
3841 for (Lun
= 0; Lun
< SCSI_MAXIMUM_LOGICAL_UNITS
; Lun
++)
3843 if ((!LunExtension
) || (!LunInfo
))
3846 /* Add extension to the list */
3847 Hint
= (Target
+ Lun
) % LUS_NUMBER
;
3848 LunExtension
->Next
= DeviceExtension
->LunExtensionList
[Hint
];
3849 DeviceExtension
->LunExtensionList
[Hint
] = LunExtension
;
3851 /* Fill Path, Target, Lun fields */
3852 LunExtension
->PathId
= LunInfo
->PathId
= (UCHAR
)Bus
;
3853 LunExtension
->TargetId
= LunInfo
->TargetId
= (UCHAR
) Target
;
3854 LunExtension
->Lun
= LunInfo
->Lun
= (UCHAR
)Lun
;
3856 /* Set flag to prevent race conditions */
3857 LunExtension
->Flags
|= SCSI_PORT_SCAN_IN_PROGRESS
;
3859 /* Zero LU extension contents */
3860 if (DeviceExtension
->LunExtensionSize
)
3862 RtlZeroMemory(LunExtension
+ 1,
3863 DeviceExtension
->LunExtensionSize
);
3866 /* Finally send the inquiry command */
3867 Status
= SpiSendInquiry(DeviceExtension
->DeviceObject
, LunInfo
);
3869 if (NT_SUCCESS(Status
))
3871 /* Let's see if we really found a device */
3872 PINQUIRYDATA InquiryData
= (PINQUIRYDATA
)LunInfo
->InquiryData
;
3874 /* Check if this device is unsupported */
3875 if (InquiryData
->DeviceTypeQualifier
== DEVICE_QUALIFIER_NOT_SUPPORTED
)
3877 DeviceExtension
->LunExtensionList
[Hint
] =
3878 DeviceExtension
->LunExtensionList
[Hint
]->Next
;
3883 /* Clear the "in scan" flag */
3884 LunExtension
->Flags
&= ~SCSI_PORT_SCAN_IN_PROGRESS
;
3886 DPRINT("SpiScanAdapter(): Found device of type %d at bus %d tid %d lun %d\n",
3887 InquiryData
->DeviceType
, Bus
, Target
, Lun
);
3889 /* Add this info to the linked list */
3890 LunInfo
->Next
= NULL
;
3892 LastLunInfo
->Next
= LunInfo
;
3894 BusScanInfo
->LunInfo
= LunInfo
;
3896 /* Store the last LUN info */
3897 LastLunInfo
= LunInfo
;
3899 /* Store DeviceObject */
3900 LunInfo
->DeviceObject
= DeviceExtension
->DeviceObject
;
3902 /* Allocate another buffer */
3903 LunInfo
= ExAllocatePoolWithTag(PagedPool
, sizeof(SCSI_LUN_INFO
), TAG_SCSIPORT
);
3907 DPRINT1("Out of resources!\n");
3911 RtlZeroMemory(LunInfo
, sizeof(SCSI_LUN_INFO
));
3913 /* Create a new LU extension */
3914 LunExtension
= SpiAllocateLunExtension(DeviceExtension
);
3920 /* Remove this LUN from the list */
3921 DeviceExtension
->LunExtensionList
[Hint
] =
3922 DeviceExtension
->LunExtensionList
[Hint
]->Next
;
3924 /* Decide whether we are continuing or not */
3925 if (Status
== STATUS_INVALID_DEVICE_REQUEST
)
3933 /* Free allocated buffers */
3935 ExFreePool(LunExtension
);
3938 ExFreePool(LunInfo
);
3940 /* Sum what we found */
3941 BusScanInfo
->LogicalUnitsCount
+= (UCHAR
) DevicesFound
;
3942 DPRINT(" Found %d devices on bus %d\n", DevicesFound
, Bus
);
3945 DPRINT ("SpiScanAdapter() done\n");
3950 SpiGetInquiryData(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
3953 ULONG InquiryDataSize
;
3954 PSCSI_LUN_INFO LunInfo
;
3955 ULONG BusCount
, LunCount
, Length
;
3956 PIO_STACK_LOCATION IrpStack
;
3957 PSCSI_ADAPTER_BUS_INFO AdapterBusInfo
;
3958 PSCSI_INQUIRY_DATA InquiryData
;
3959 PSCSI_BUS_DATA BusData
;
3963 DPRINT("SpiGetInquiryData() called\n");
3965 /* Get pointer to the buffer */
3966 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
3967 Buffer
= Irp
->AssociatedIrp
.SystemBuffer
;
3969 /* Initialize bus and LUN counters */
3970 BusCount
= DeviceExtension
->BusesConfig
->NumberOfBuses
;
3973 /* Calculate total number of LUNs */
3974 for (Bus
= 0; Bus
< BusCount
; Bus
++)
3975 LunCount
+= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LogicalUnitsCount
;
3977 /* Calculate size of inquiry data, rounding up to sizeof(ULONG) */
3979 ((sizeof(SCSI_INQUIRY_DATA
) - 1 + INQUIRYDATABUFFERSIZE
+
3980 sizeof(ULONG
) - 1) & ~(sizeof(ULONG
) - 1));
3982 /* Calculate data size */
3983 Length
= sizeof(SCSI_ADAPTER_BUS_INFO
) + (BusCount
- 1) *
3984 sizeof(SCSI_BUS_DATA
);
3986 Length
+= InquiryDataSize
* LunCount
;
3988 /* Check, if all data is going to fit into provided buffer */
3989 if (IrpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
< Length
)
3991 Irp
->IoStatus
.Status
= STATUS_BUFFER_TOO_SMALL
;
3992 return STATUS_BUFFER_TOO_SMALL
;
3995 /* Store data size in the IRP */
3996 Irp
->IoStatus
.Information
= Length
;
3998 DPRINT("Data size: %lu\n", Length
);
4000 AdapterBusInfo
= (PSCSI_ADAPTER_BUS_INFO
)Buffer
;
4002 AdapterBusInfo
->NumberOfBuses
= (UCHAR
)BusCount
;
4004 /* Point InquiryData to the corresponding place inside Buffer */
4005 InquiryData
= (PSCSI_INQUIRY_DATA
)(Buffer
+ sizeof(SCSI_ADAPTER_BUS_INFO
) +
4006 (BusCount
- 1) * sizeof(SCSI_BUS_DATA
));
4009 for (Bus
= 0; Bus
< BusCount
; Bus
++)
4011 BusData
= &AdapterBusInfo
->BusData
[Bus
];
4013 /* Calculate and save an offset of the inquiry data */
4014 BusData
->InquiryDataOffset
= (PUCHAR
)InquiryData
- Buffer
;
4016 /* Get a pointer to the LUN information structure */
4017 LunInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LunInfo
;
4019 /* Store Initiator Bus Id */
4020 BusData
->InitiatorBusId
=
4021 DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->BusIdentifier
;
4023 /* Store LUN count */
4024 BusData
->NumberOfLogicalUnits
=
4025 DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LogicalUnitsCount
;
4028 while (LunInfo
!= NULL
)
4030 DPRINT("(Bus %lu Target %lu Lun %lu)\n",
4031 Bus
, LunInfo
->TargetId
, LunInfo
->Lun
);
4033 /* Fill InquiryData with values */
4034 InquiryData
->PathId
= LunInfo
->PathId
;
4035 InquiryData
->TargetId
= LunInfo
->TargetId
;
4036 InquiryData
->Lun
= LunInfo
->Lun
;
4037 InquiryData
->InquiryDataLength
= INQUIRYDATABUFFERSIZE
;
4038 InquiryData
->DeviceClaimed
= LunInfo
->DeviceClaimed
;
4039 InquiryData
->NextInquiryDataOffset
=
4040 (PUCHAR
)InquiryData
+ InquiryDataSize
- Buffer
;
4042 /* Copy data in it */
4043 RtlCopyMemory(InquiryData
->InquiryData
,
4044 LunInfo
->InquiryData
,
4045 INQUIRYDATABUFFERSIZE
);
4047 /* Move to the next LUN */
4048 LunInfo
= LunInfo
->Next
;
4049 InquiryData
= (PSCSI_INQUIRY_DATA
) ((PCHAR
)InquiryData
+ InquiryDataSize
);
4052 /* Either mark the end, or set offset to 0 */
4053 if (BusData
->NumberOfLogicalUnits
!= 0)
4054 ((PSCSI_INQUIRY_DATA
) ((PCHAR
) InquiryData
- InquiryDataSize
))->NextInquiryDataOffset
= 0;
4056 BusData
->InquiryDataOffset
= 0;
4059 /* Finish with success */
4060 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
4061 return STATUS_SUCCESS
;
4064 static PSCSI_REQUEST_BLOCK_INFO
4065 SpiGetSrbData(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
4071 PSCSI_PORT_LUN_EXTENSION LunExtension
;
4073 if (QueueTag
== SP_UNTAGGED
)
4075 /* Untagged request, get LU and return pointer to SrbInfo */
4076 LunExtension
= SpiGetLunExtension(DeviceExtension
,
4081 /* Return NULL in case of error */
4085 /* Return the pointer to SrbInfo */
4086 return &LunExtension
->SrbInfo
;
4090 /* Make sure the tag is valid, if it is - return the data */
4091 if (QueueTag
> DeviceExtension
->SrbDataCount
|| QueueTag
< 1)
4094 return &DeviceExtension
->SrbInfo
[QueueTag
-1];
4099 SpiSendRequestSense(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
4100 IN PSCSI_REQUEST_BLOCK InitialSrb
)
4102 PSCSI_REQUEST_BLOCK Srb
;
4105 PIO_STACK_LOCATION IrpStack
;
4106 LARGE_INTEGER LargeInt
;
4109 DPRINT("SpiSendRequestSense() entered, InitialSrb %p\n", InitialSrb
);
4112 Srb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(SCSI_REQUEST_BLOCK
) + sizeof(PVOID
), TAG_SCSIPORT
);
4113 RtlZeroMemory(Srb
, sizeof(SCSI_REQUEST_BLOCK
));
4116 LargeInt
.QuadPart
= (LONGLONG
) 1;
4117 Irp
= IoBuildAsynchronousFsdRequest(IRP_MJ_READ
,
4118 DeviceExtension
->DeviceObject
,
4119 InitialSrb
->SenseInfoBuffer
,
4120 InitialSrb
->SenseInfoBufferLength
,
4124 IoSetCompletionRoutine(Irp
,
4125 (PIO_COMPLETION_ROUTINE
)SpiCompletionRoutine
,
4133 DPRINT("SpiSendRequestSense() failed, Srb %p\n", Srb
);
4137 IrpStack
= IoGetNextIrpStackLocation(Irp
);
4138 IrpStack
->MajorFunction
= IRP_MJ_SCSI
;
4140 /* Put Srb address into Irp... */
4141 IrpStack
->Parameters
.Others
.Argument1
= (PVOID
)Srb
;
4143 /* ...and vice versa */
4144 Srb
->OriginalRequest
= Irp
;
4147 Ptr
= (PVOID
*)(Srb
+1);
4150 /* Build CDB for REQUEST SENSE */
4152 Cdb
= (PCDB
)Srb
->Cdb
;
4154 Cdb
->CDB6INQUIRY
.OperationCode
= SCSIOP_REQUEST_SENSE
;
4155 Cdb
->CDB6INQUIRY
.LogicalUnitNumber
= 0;
4156 Cdb
->CDB6INQUIRY
.Reserved1
= 0;
4157 Cdb
->CDB6INQUIRY
.PageCode
= 0;
4158 Cdb
->CDB6INQUIRY
.IReserved
= 0;
4159 Cdb
->CDB6INQUIRY
.AllocationLength
= (UCHAR
)InitialSrb
->SenseInfoBufferLength
;
4160 Cdb
->CDB6INQUIRY
.Control
= 0;
4163 Srb
->TargetId
= InitialSrb
->TargetId
;
4164 Srb
->Lun
= InitialSrb
->Lun
;
4165 Srb
->PathId
= InitialSrb
->PathId
;
4167 Srb
->Function
= SRB_FUNCTION_EXECUTE_SCSI
;
4168 Srb
->Length
= sizeof(SCSI_REQUEST_BLOCK
);
4170 /* Timeout will be 2 seconds */
4171 Srb
->TimeOutValue
= 2;
4173 /* No auto request sense */
4174 Srb
->SenseInfoBufferLength
= 0;
4175 Srb
->SenseInfoBuffer
= NULL
;
4177 /* Set necessary flags */
4178 Srb
->SrbFlags
= SRB_FLAGS_DATA_IN
| SRB_FLAGS_BYPASS_FROZEN_QUEUE
|
4179 SRB_FLAGS_DISABLE_DISCONNECT
;
4181 /* Transfer disable synch transfer flag */
4182 if (InitialSrb
->SrbFlags
& SRB_FLAGS_DISABLE_SYNCH_TRANSFER
)
4183 Srb
->SrbFlags
|= SRB_FLAGS_DISABLE_SYNCH_TRANSFER
;
4185 Srb
->DataBuffer
= InitialSrb
->SenseInfoBuffer
;
4187 /* Fill the transfer length */
4188 Srb
->DataTransferLength
= InitialSrb
->SenseInfoBufferLength
;
4190 /* Clear statuses */
4191 Srb
->ScsiStatus
= Srb
->SrbStatus
= 0;
4194 /* Call the driver */
4195 (VOID
)IoCallDriver(DeviceExtension
->DeviceObject
, Irp
);
4197 DPRINT("SpiSendRequestSense() done\n");
4204 SpiProcessCompletedRequest(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
4205 IN PSCSI_REQUEST_BLOCK_INFO SrbInfo
,
4206 OUT PBOOLEAN NeedToCallStartIo
)
4208 PSCSI_REQUEST_BLOCK Srb
;
4209 PSCSI_PORT_LUN_EXTENSION LunExtension
;
4212 ULONG SequenceNumber
;
4215 Irp
= Srb
->OriginalRequest
;
4217 /* Get Lun extension */
4218 LunExtension
= SpiGetLunExtension(DeviceExtension
,
4223 if (Srb
->SrbFlags
& SRB_FLAGS_UNSPECIFIED_DIRECTION
&&
4224 DeviceExtension
->MapBuffers
&&
4227 /* MDL is shared if transfer is broken into smaller parts */
4228 Srb
->DataBuffer
= (PCCHAR
)MmGetMdlVirtualAddress(Irp
->MdlAddress
) +
4229 ((PCCHAR
)Srb
->DataBuffer
- SrbInfo
->DataOffset
);
4231 /* In case of data going in, flush the buffers */
4232 if (Srb
->SrbFlags
& SRB_FLAGS_DATA_IN
)
4234 KeFlushIoBuffers(Irp
->MdlAddress
,
4240 /* Flush adapter if needed */
4241 if (SrbInfo
->BaseOfMapRegister
)
4243 /* TODO: Implement */
4247 /* Clear the request */
4248 SrbInfo
->Srb
= NULL
;
4250 /* If disconnect is disabled... */
4251 if (Srb
->SrbFlags
& SRB_FLAGS_DISABLE_DISCONNECT
)
4253 /* Acquire the spinlock since we mess with flags */
4254 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4256 /* Set corresponding flag */
4257 DeviceExtension
->Flags
|= SCSI_PORT_DISCONNECT_ALLOWED
;
4259 /* Clear the timer if needed */
4260 if (!(DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_RESET
))
4261 DeviceExtension
->TimerCount
= -1;
4263 /* Spinlock is not needed anymore */
4264 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4266 if (!(DeviceExtension
->Flags
& SCSI_PORT_REQUEST_PENDING
) &&
4267 !(DeviceExtension
->Flags
& SCSI_PORT_DEVICE_BUSY
) &&
4268 !(*NeedToCallStartIo
))
4270 /* We're not busy, but we have a request pending */
4271 IoStartNextPacket(DeviceExtension
->DeviceObject
, FALSE
);
4275 /* Scatter/gather */
4276 if (Srb
->SrbFlags
& SRB_FLAGS_SGLIST_FROM_POOL
)
4278 /* TODO: Implement */
4282 /* Acquire spinlock (we're freeing SrbExtension) */
4283 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4285 /* Free it (if needed) */
4286 if (Srb
->SrbExtension
)
4288 if (Srb
->SenseInfoBuffer
!= NULL
&& DeviceExtension
->SupportsAutoSense
)
4290 ASSERT(Srb
->SenseInfoBuffer
== NULL
|| SrbInfo
->SaveSenseRequest
!= NULL
);
4292 if (Srb
->SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
)
4294 /* Copy sense data to the buffer */
4295 RtlCopyMemory(SrbInfo
->SaveSenseRequest
,
4296 Srb
->SenseInfoBuffer
,
4297 Srb
->SenseInfoBufferLength
);
4300 /* And restore the pointer */
4301 Srb
->SenseInfoBuffer
= SrbInfo
->SaveSenseRequest
;
4304 /* Put it into the free srb extensions list */
4305 *((PVOID
*)Srb
->SrbExtension
) = DeviceExtension
->FreeSrbExtensions
;
4306 DeviceExtension
->FreeSrbExtensions
= Srb
->SrbExtension
;
4309 /* Save transfer length in the IRP */
4310 Irp
->IoStatus
.Information
= Srb
->DataTransferLength
;
4312 SequenceNumber
= SrbInfo
->SequenceNumber
;
4313 SrbInfo
->SequenceNumber
= 0;
4315 /* Decrement the queue count */
4316 LunExtension
->QueueCount
--;
4318 /* Free Srb, if needed*/
4319 if (Srb
->QueueTag
!= SP_UNTAGGED
)
4321 /* Put it into the free list */
4322 SrbInfo
->Requests
.Blink
= NULL
;
4323 SrbInfo
->Requests
.Flink
= (PLIST_ENTRY
)DeviceExtension
->FreeSrbInfo
;
4324 DeviceExtension
->FreeSrbInfo
= SrbInfo
;
4327 /* SrbInfo is not used anymore */
4330 if (DeviceExtension
->Flags
& SCSI_PORT_REQUEST_PENDING
)
4332 /* Clear the flag */
4333 DeviceExtension
->Flags
&= ~SCSI_PORT_REQUEST_PENDING
;
4335 /* Note the caller about StartIo */
4336 *NeedToCallStartIo
= TRUE
;
4339 if (SRB_STATUS(Srb
->SrbStatus
) == SRB_STATUS_SUCCESS
)
4341 /* Start the packet */
4342 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
4344 if (!(Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
) &&
4345 LunExtension
->RequestTimeout
== -1)
4347 /* Start the next packet */
4348 SpiGetNextRequestFromLun(DeviceExtension
, LunExtension
);
4352 /* Release the spinlock */
4353 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4356 DPRINT("IoCompleting request IRP 0x%p\n", Irp
);
4358 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
4360 /* Decrement number of active requests, and analyze the result */
4361 Result
= InterlockedDecrement(&DeviceExtension
->ActiveRequestCounter
);
4364 !DeviceExtension
->MapRegisters
&&
4365 DeviceExtension
->AdapterObject
!= NULL
)
4367 /* Nullify map registers */
4368 DeviceExtension
->MapRegisterBase
= NULL
;
4369 IoFreeAdapterChannel(DeviceExtension
->AdapterObject
);
4372 /* Exit, we're done */
4376 /* Decrement number of active requests, and analyze the result */
4377 Result
= InterlockedDecrement(&DeviceExtension
->ActiveRequestCounter
);
4380 !DeviceExtension
->MapRegisters
&&
4381 DeviceExtension
->AdapterObject
!= NULL
)
4383 /* Result is negative, so this is a slave, free map registers */
4384 DeviceExtension
->MapRegisterBase
= NULL
;
4385 IoFreeAdapterChannel(DeviceExtension
->AdapterObject
);
4388 /* Convert status */
4389 Irp
->IoStatus
.Status
= SpiStatusSrbToNt(Srb
->SrbStatus
);
4391 /* It's not a bypass, it's busy or the queue is full? */
4392 if ((Srb
->ScsiStatus
== SCSISTAT_BUSY
||
4393 Srb
->SrbStatus
== SRB_STATUS_BUSY
||
4394 Srb
->ScsiStatus
== SCSISTAT_QUEUE_FULL
) &&
4395 !(Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
))
4398 DPRINT("Busy SRB status %x\n", Srb
->SrbStatus
);
4400 /* Requeu, if needed */
4401 if (LunExtension
->Flags
& (LUNEX_FROZEN_QUEUE
| LUNEX_BUSY
))
4403 DPRINT("it's being requeued\n");
4405 Srb
->SrbStatus
= SRB_STATUS_PENDING
;
4406 Srb
->ScsiStatus
= 0;
4408 if (!KeInsertByKeyDeviceQueue(&LunExtension
->DeviceQueue
,
4409 &Irp
->Tail
.Overlay
.DeviceQueueEntry
,
4412 /* It's a big f.ck up if we got here */
4413 Srb
->SrbStatus
= SRB_STATUS_ERROR
;
4414 Srb
->ScsiStatus
= SCSISTAT_BUSY
;
4420 /* Release the spinlock */
4421 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4424 else if (LunExtension
->AttemptCount
++ < 20)
4426 /* LUN is still busy */
4427 Srb
->ScsiStatus
= 0;
4428 Srb
->SrbStatus
= SRB_STATUS_PENDING
;
4430 LunExtension
->BusyRequest
= Irp
;
4431 LunExtension
->Flags
|= LUNEX_BUSY
;
4433 /* Release the spinlock */
4434 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4439 /* Freeze the queue*/
4440 Srb
->SrbStatus
|= SRB_STATUS_QUEUE_FROZEN
;
4441 LunExtension
->Flags
|= LUNEX_FROZEN_QUEUE
;
4443 /* "Unfull" the queue */
4444 LunExtension
->Flags
&= ~LUNEX_FULL_QUEUE
;
4446 /* Release the spinlock */
4447 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4449 /* Return status that the device is not ready */
4450 Irp
->IoStatus
.Status
= STATUS_DEVICE_NOT_READY
;
4451 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
4457 /* Start the next request, if LUN is idle, and this is sense request */
4458 if (((Srb
->ScsiStatus
!= SCSISTAT_CHECK_CONDITION
) ||
4459 (Srb
->SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
) ||
4460 !Srb
->SenseInfoBuffer
|| !Srb
->SenseInfoBufferLength
)
4461 && (Srb
->SrbFlags
& SRB_FLAGS_NO_QUEUE_FREEZE
))
4463 if (LunExtension
->RequestTimeout
== -1)
4464 SpiGetNextRequestFromLun(DeviceExtension
, LunExtension
);
4466 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4470 /* Freeze the queue */
4471 Srb
->SrbStatus
|= SRB_STATUS_QUEUE_FROZEN
;
4472 LunExtension
->Flags
|= LUNEX_FROZEN_QUEUE
;
4474 /* Do we need a request sense? */
4475 if (Srb
->ScsiStatus
== SCSISTAT_CHECK_CONDITION
&&
4476 !(Srb
->SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
) &&
4477 Srb
->SenseInfoBuffer
&& Srb
->SenseInfoBufferLength
)
4479 /* If LUN is busy, we have to requeue it in order to allow request sense */
4480 if (LunExtension
->Flags
& LUNEX_BUSY
)
4482 DPRINT("Requeueing busy request to allow request sense\n");
4484 if (!KeInsertByKeyDeviceQueue(&LunExtension
->DeviceQueue
,
4485 &LunExtension
->BusyRequest
->Tail
.Overlay
.DeviceQueueEntry
,
4488 /* We should never get here */
4491 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4492 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
4497 /* Clear busy flags */
4498 LunExtension
->Flags
&= ~(LUNEX_FULL_QUEUE
| LUNEX_BUSY
);
4501 /* Release the spinlock */
4502 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4504 /* Send RequestSense */
4505 SpiSendRequestSense(DeviceExtension
, Srb
);
4511 /* Release the spinlock */
4512 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4515 /* Complete the request */
4516 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
4521 SpiCompletionRoutine(PDEVICE_OBJECT DeviceObject
,
4525 PSCSI_REQUEST_BLOCK Srb
= (PSCSI_REQUEST_BLOCK
)Context
;
4526 PSCSI_REQUEST_BLOCK InitialSrb
;
4529 DPRINT("SpiCompletionRoutine() entered, IRP %p \n", Irp
);
4531 if ((Srb
->Function
== SRB_FUNCTION_RESET_BUS
) ||
4532 (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
))
4534 /* Deallocate SRB and IRP and exit */
4538 return STATUS_MORE_PROCESSING_REQUIRED
;
4541 /* Get a pointer to the SRB and IRP which were initially sent */
4542 InitialSrb
= *((PVOID
*)(Srb
+1));
4543 InitialIrp
= InitialSrb
->OriginalRequest
;
4545 if ((SRB_STATUS(Srb
->SrbStatus
) == SRB_STATUS_SUCCESS
) ||
4546 (SRB_STATUS(Srb
->SrbStatus
) == SRB_STATUS_DATA_OVERRUN
))
4548 /* Sense data is OK */
4549 InitialSrb
->SrbStatus
|= SRB_STATUS_AUTOSENSE_VALID
;
4551 /* Set length to be the same */
4552 InitialSrb
->SenseInfoBufferLength
= (UCHAR
)Srb
->DataTransferLength
;
4555 /* Make sure initial SRB's queue is frozen */
4556 ASSERT(InitialSrb
->SrbStatus
& SRB_STATUS_QUEUE_FROZEN
);
4558 /* Complete this request */
4559 IoCompleteRequest(InitialIrp
, IO_DISK_INCREMENT
);
4561 /* Deallocate everything (internal) */
4564 if (Irp
->MdlAddress
!= NULL
)
4566 MmUnlockPages(Irp
->MdlAddress
);
4567 IoFreeMdl(Irp
->MdlAddress
);
4568 Irp
->MdlAddress
= NULL
;
4572 return STATUS_MORE_PROCESSING_REQUIRED
;
4575 static BOOLEAN NTAPI
4576 ScsiPortIsr(IN PKINTERRUPT Interrupt
,
4577 IN PVOID ServiceContext
)
4579 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
4582 DPRINT("ScsiPortIsr() called!\n");
4584 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)ServiceContext
;
4586 /* If interrupts are disabled - we don't expect any */
4587 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_DISABLE_INTERRUPTS
)
4590 /* Call miniport's HwInterrupt routine */
4591 Result
= DeviceExtension
->HwInterrupt(&DeviceExtension
->MiniPortDeviceExtension
);
4593 /* If flag of notification is set - queue a DPC */
4594 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
4596 IoRequestDpc(DeviceExtension
->DeviceObject
,
4597 DeviceExtension
->CurrentIrp
,
4606 SpiSaveInterruptData(IN PVOID Context
)
4608 PSCSI_PORT_SAVE_INTERRUPT InterruptContext
= Context
;
4609 PSCSI_PORT_LUN_EXTENSION LunExtension
;
4610 PSCSI_REQUEST_BLOCK Srb
;
4611 PSCSI_REQUEST_BLOCK_INFO SrbInfo
, NextSrbInfo
;
4612 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
4615 /* Get pointer to the device extension */
4616 DeviceExtension
= InterruptContext
->DeviceExtension
;
4618 /* If we don't have anything pending - return */
4619 if (!(DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
))
4622 /* Actually save the interrupt data */
4623 *InterruptContext
->InterruptData
= DeviceExtension
->InterruptData
;
4625 /* Clear the data stored in the device extension */
4626 DeviceExtension
->InterruptData
.Flags
&=
4627 (SCSI_PORT_RESET
| SCSI_PORT_RESET_REQUEST
| SCSI_PORT_DISABLE_INTERRUPTS
);
4628 DeviceExtension
->InterruptData
.CompletedAbort
= NULL
;
4629 DeviceExtension
->InterruptData
.ReadyLun
= NULL
;
4630 DeviceExtension
->InterruptData
.CompletedRequests
= NULL
;
4632 /* Loop through the list of completed requests */
4633 SrbInfo
= InterruptContext
->InterruptData
->CompletedRequests
;
4637 /* Make sure we have SRV */
4638 ASSERT(SrbInfo
->Srb
);
4640 /* Get SRB and LunExtension */
4643 LunExtension
= SpiGetLunExtension(DeviceExtension
,
4648 /* We have to check special cases if request is unsuccessfull*/
4649 if (Srb
->SrbStatus
!= SRB_STATUS_SUCCESS
)
4651 /* Check if we need request sense by a few conditions */
4652 if (Srb
->SenseInfoBuffer
&& Srb
->SenseInfoBufferLength
&&
4653 Srb
->ScsiStatus
== SCSISTAT_CHECK_CONDITION
&&
4654 !(Srb
->SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
))
4656 if (LunExtension
->Flags
& LUNEX_NEED_REQUEST_SENSE
)
4658 /* It means: we tried to send REQUEST SENSE, but failed */
4660 Srb
->ScsiStatus
= 0;
4661 Srb
->SrbStatus
= SRB_STATUS_REQUEST_SENSE_FAILED
;
4665 /* Set the corresponding flag, so that REQUEST SENSE
4667 LunExtension
->Flags
|= LUNEX_NEED_REQUEST_SENSE
;
4672 /* Check for a full queue */
4673 if (Srb
->ScsiStatus
== SCSISTAT_QUEUE_FULL
)
4675 /* TODO: Implement when it's encountered */
4680 /* Let's decide if we need to watch timeout or not */
4681 if (Srb
->QueueTag
== SP_UNTAGGED
)
4687 if (LunExtension
->SrbInfo
.Requests
.Flink
== &SrbInfo
->Requests
)
4692 /* Remove it from the queue */
4693 RemoveEntryList(&SrbInfo
->Requests
);
4698 /* We have to maintain timeout counter */
4699 if (IsListEmpty(&LunExtension
->SrbInfo
.Requests
))
4701 LunExtension
->RequestTimeout
= -1;
4705 NextSrbInfo
= CONTAINING_RECORD(LunExtension
->SrbInfo
.Requests
.Flink
,
4706 SCSI_REQUEST_BLOCK_INFO
,
4709 Srb
= NextSrbInfo
->Srb
;
4711 /* Update timeout counter */
4712 LunExtension
->RequestTimeout
= Srb
->TimeOutValue
;
4716 SrbInfo
= SrbInfo
->CompletedRequests
;
4724 SpiGetNextRequestFromLun(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
4725 IN PSCSI_PORT_LUN_EXTENSION LunExtension
)
4727 PIO_STACK_LOCATION IrpStack
;
4729 PKDEVICE_QUEUE_ENTRY Entry
;
4730 PSCSI_REQUEST_BLOCK Srb
;
4733 /* If LUN is not active or queue is more than maximum allowed */
4734 if (LunExtension
->QueueCount
>= LunExtension
->MaxQueueCount
||
4735 !(LunExtension
->Flags
& SCSI_PORT_LU_ACTIVE
))
4737 /* Release the spinlock and exit */
4738 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4742 /* Check if we can get a next request */
4743 if (LunExtension
->Flags
&
4744 (LUNEX_NEED_REQUEST_SENSE
| LUNEX_BUSY
|
4745 LUNEX_FULL_QUEUE
| LUNEX_FROZEN_QUEUE
| LUNEX_REQUEST_PENDING
))
4747 /* Pending requests can only be started if the queue is empty */
4748 if (IsListEmpty(&LunExtension
->SrbInfo
.Requests
) &&
4749 !(LunExtension
->Flags
&
4750 (LUNEX_BUSY
| LUNEX_FROZEN_QUEUE
| LUNEX_FULL_QUEUE
| LUNEX_NEED_REQUEST_SENSE
)))
4752 /* Make sure we have SRB */
4753 ASSERT(LunExtension
->SrbInfo
.Srb
== NULL
);
4755 /* Clear active and pending flags */
4756 LunExtension
->Flags
&= ~(LUNEX_REQUEST_PENDING
| SCSI_PORT_LU_ACTIVE
);
4758 /* Get next Irp, and clear pending requests list */
4759 NextIrp
= LunExtension
->PendingRequest
;
4760 LunExtension
->PendingRequest
= NULL
;
4762 /* Set attempt counter to zero */
4763 LunExtension
->AttemptCount
= 0;
4765 /* Release the spinlock */
4766 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4768 /* Start the next pending request */
4769 IoStartPacket(DeviceExtension
->DeviceObject
, NextIrp
, (PULONG
)NULL
, NULL
);
4775 /* Release the spinlock, without clearing any flags and exit */
4776 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4782 /* Reset active flag */
4783 LunExtension
->Flags
&= ~SCSI_PORT_LU_ACTIVE
;
4785 /* Set attempt counter to zero */
4786 LunExtension
->AttemptCount
= 0;
4788 /* Remove packet from the device queue */
4789 Entry
= KeRemoveByKeyDeviceQueue(&LunExtension
->DeviceQueue
, LunExtension
->SortKey
);
4793 /* Get pointer to the next irp */
4794 NextIrp
= CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.DeviceQueueEntry
);
4796 /* Get point to the SRB */
4797 IrpStack
= IoGetCurrentIrpStackLocation(NextIrp
);
4798 Srb
= (PSCSI_REQUEST_BLOCK
)IrpStack
->Parameters
.Others
.Argument1
;
4801 LunExtension
->SortKey
= Srb
->QueueSortKey
;
4802 LunExtension
->SortKey
++;
4804 /* Release the spinlock */
4805 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4807 /* Start the next pending request */
4808 IoStartPacket(DeviceExtension
->DeviceObject
, NextIrp
, (PULONG
)NULL
, NULL
);
4812 /* Release the spinlock */
4813 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4819 // ScsiPortDpcForIsr
4826 // IN PDEVICE_OBJECT DpcDeviceObject
4828 // IN PVOID DpcContext
4831 ScsiPortDpcForIsr(IN PKDPC Dpc
,
4832 IN PDEVICE_OBJECT DpcDeviceObject
,
4834 IN PVOID DpcContext
)
4836 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
= DpcDeviceObject
->DeviceExtension
;
4837 SCSI_PORT_INTERRUPT_DATA InterruptData
;
4838 SCSI_PORT_SAVE_INTERRUPT Context
;
4839 PSCSI_PORT_LUN_EXTENSION LunExtension
;
4840 BOOLEAN NeedToStartIo
;
4841 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
4842 LARGE_INTEGER TimerValue
;
4844 DPRINT("ScsiPortDpcForIsr(Dpc %p DpcDeviceObject %p DpcIrp %p DpcContext %p)\n",
4845 Dpc
, DpcDeviceObject
, DpcIrp
, DpcContext
);
4847 /* We need to acquire spinlock */
4848 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4850 RtlZeroMemory(&InterruptData
, sizeof(SCSI_PORT_INTERRUPT_DATA
));
4854 /* Interrupt structure must be snapshotted, and only then analyzed */
4855 Context
.InterruptData
= &InterruptData
;
4856 Context
.DeviceExtension
= DeviceExtension
;
4858 if (!KeSynchronizeExecution(DeviceExtension
->Interrupt
[0],
4859 SpiSaveInterruptData
,
4862 /* Nothing - just return (don't forget to release the spinlock */
4863 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4864 DPRINT("ScsiPortDpcForIsr() done\n");
4868 /* If flush of adapters is needed - do it */
4869 if (InterruptData
.Flags
& SCSI_PORT_FLUSH_ADAPTERS
)
4871 /* TODO: Implement */
4875 /* Check for IoMapTransfer */
4876 if (InterruptData
.Flags
& SCSI_PORT_MAP_TRANSFER
)
4878 /* TODO: Implement */
4882 /* Check if timer is needed */
4883 if (InterruptData
.Flags
& SCSI_PORT_TIMER_NEEDED
)
4885 /* Save the timer routine */
4886 DeviceExtension
->HwScsiTimer
= InterruptData
.HwScsiTimer
;
4888 if (InterruptData
.MiniportTimerValue
== 0)
4890 /* Cancel the timer */
4891 KeCancelTimer(&DeviceExtension
->MiniportTimer
);
4895 /* Convert timer value */
4896 TimerValue
.QuadPart
= Int32x32To64(InterruptData
.MiniportTimerValue
, -10);
4899 KeSetTimer(&DeviceExtension
->MiniportTimer
,
4901 &DeviceExtension
->MiniportTimerDpc
);
4905 /* If it's ready for the next request */
4906 if (InterruptData
.Flags
& SCSI_PORT_NEXT_REQUEST_READY
)
4908 /* Check for a duplicate request (NextRequest+NextLuRequest) */
4909 if ((DeviceExtension
->Flags
&
4910 (SCSI_PORT_DEVICE_BUSY
| SCSI_PORT_DISCONNECT_ALLOWED
)) ==
4911 (SCSI_PORT_DEVICE_BUSY
| SCSI_PORT_DISCONNECT_ALLOWED
))
4913 /* Clear busy flag set by ScsiPortStartPacket() */
4914 DeviceExtension
->Flags
&= ~SCSI_PORT_DEVICE_BUSY
;
4916 if (!(InterruptData
.Flags
& SCSI_PORT_RESET
))
4918 /* Ready for next, and no reset is happening */
4919 DeviceExtension
->TimerCount
= -1;
4924 /* Not busy, but not ready for the next request */
4925 DeviceExtension
->Flags
&= ~SCSI_PORT_DEVICE_BUSY
;
4926 InterruptData
.Flags
&= ~SCSI_PORT_NEXT_REQUEST_READY
;
4931 if (InterruptData
.Flags
& SCSI_PORT_RESET_REPORTED
)
4933 /* Hold for a bit */
4934 DeviceExtension
->TimerCount
= 4;
4937 /* Any ready LUN? */
4938 if (InterruptData
.ReadyLun
!= NULL
)
4941 /* Process all LUNs from the list*/
4944 /* Remove it from the list first (as processed) */
4945 LunExtension
= InterruptData
.ReadyLun
;
4946 InterruptData
.ReadyLun
= LunExtension
->ReadyLun
;
4947 LunExtension
->ReadyLun
= NULL
;
4949 /* Get next request for this LUN */
4950 SpiGetNextRequestFromLun(DeviceExtension
, LunExtension
);
4952 /* Still ready requests exist?
4953 If yes - get spinlock, if no - stop here */
4954 if (InterruptData
.ReadyLun
!= NULL
)
4955 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4962 /* Release the spinlock */
4963 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4966 /* If we ready for next packet, start it */
4967 if (InterruptData
.Flags
& SCSI_PORT_NEXT_REQUEST_READY
)
4968 IoStartNextPacket(DeviceExtension
->DeviceObject
, FALSE
);
4970 NeedToStartIo
= FALSE
;
4972 /* Loop the completed request list */
4973 while (InterruptData
.CompletedRequests
)
4975 /* Remove the request */
4976 SrbInfo
= InterruptData
.CompletedRequests
;
4977 InterruptData
.CompletedRequests
= SrbInfo
->CompletedRequests
;
4978 SrbInfo
->CompletedRequests
= NULL
;
4981 SpiProcessCompletedRequest(DeviceExtension
,
4986 /* Loop abort request list */
4987 while (InterruptData
.CompletedAbort
)
4989 LunExtension
= InterruptData
.CompletedAbort
;
4991 /* Remove the request */
4992 InterruptData
.CompletedAbort
= LunExtension
->CompletedAbortRequests
;
4994 /* Get spinlock since we're going to change flags */
4995 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4997 /* TODO: Put SrbExtension to the list of free extensions */
5001 /* If we need - call StartIo routine */
5004 /* Make sure CurrentIrp is not null! */
5005 ASSERT(DpcDeviceObject
->CurrentIrp
!= NULL
);
5006 ScsiPortStartIo(DpcDeviceObject
, DpcDeviceObject
->CurrentIrp
);
5009 /* Everything has been done, check */
5010 if (InterruptData
.Flags
& SCSI_PORT_ENABLE_INT_REQUEST
)
5012 /* Synchronize using spinlock */
5013 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
5015 /* Request an interrupt */
5016 DeviceExtension
->HwInterrupt(DeviceExtension
->MiniPortDeviceExtension
);
5018 ASSERT(DeviceExtension
->Flags
& SCSI_PORT_DISABLE_INT_REQUESET
);
5020 /* Should interrupts be enabled again? */
5021 if (DeviceExtension
->Flags
& SCSI_PORT_DISABLE_INT_REQUESET
)
5023 /* Clear this flag */
5024 DeviceExtension
->Flags
&= ~SCSI_PORT_DISABLE_INT_REQUESET
;
5026 /* Call a special routine to do this */
5029 KeSynchronizeExecution(DeviceExtension
->Interrupt
,
5030 SpiEnableInterrupts
,
5035 /* If we need a notification again - loop */
5036 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
5039 /* Release the spinlock */
5040 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
5043 DPRINT("ScsiPortDpcForIsr() done\n");
5048 SpiProcessTimeout(PVOID ServiceContext
)
5050 PDEVICE_OBJECT DeviceObject
= (PDEVICE_OBJECT
)ServiceContext
;
5051 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
= DeviceObject
->DeviceExtension
;
5054 DPRINT("SpiProcessTimeout() entered\n");
5056 DeviceExtension
->TimerCount
= -1;
5058 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_RESET
)
5060 DeviceExtension
->InterruptData
.Flags
&= ~SCSI_PORT_RESET
;
5062 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_RESET_REQUEST
)
5064 DeviceExtension
->InterruptData
.Flags
&= ~SCSI_PORT_RESET_REQUEST
;
5065 ScsiPortStartPacket(ServiceContext
);
5072 DPRINT("Resetting the bus\n");
5074 for (Bus
= 0; Bus
< DeviceExtension
->BusNum
; Bus
++)
5076 DeviceExtension
->HwResetBus(DeviceExtension
->MiniPortDeviceExtension
, Bus
);
5078 /* Reset flags and set reset timeout to 4 seconds */
5079 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_RESET
;
5080 DeviceExtension
->TimerCount
= 4;
5083 /* If miniport requested - request a dpc for it */
5084 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
5085 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
5094 SpiResetBus(PVOID ServiceContext
)
5096 PRESETBUS_PARAMS ResetParams
= (PRESETBUS_PARAMS
)ServiceContext
;
5097 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
5099 /* Perform the bus reset */
5100 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)ResetParams
->DeviceExtension
;
5101 DeviceExtension
->HwResetBus(DeviceExtension
->MiniPortDeviceExtension
,
5102 ResetParams
->PathId
);
5104 /* Set flags and start the timer */
5105 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_RESET
;
5106 DeviceExtension
->TimerCount
= 4;
5108 /* If miniport requested - give him a DPC */
5109 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
5110 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
5117 // This function handles timeouts and other time delayed processing
5122 // IN PDEVICE_OBJECT DeviceObject Device object registered with timer
5123 // IN PVOID Context the Controller extension for the
5124 // controller the device is on
5127 ScsiPortIoTimer(PDEVICE_OBJECT DeviceObject
,
5130 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
5131 PSCSI_PORT_LUN_EXTENSION LunExtension
;
5135 DPRINT("ScsiPortIoTimer()\n");
5137 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
5139 /* Protect with the spinlock */
5140 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
5142 /* Check timeouts */
5143 if (DeviceExtension
->TimerCount
> 0)
5145 /* Decrease the timeout counter */
5146 DeviceExtension
->TimerCount
--;
5148 if (DeviceExtension
->TimerCount
== 0)
5150 /* Timeout, process it */
5151 if (KeSynchronizeExecution(DeviceExtension
->Interrupt
[0],
5153 DeviceExtension
->DeviceObject
))
5155 DPRINT("Error happened during processing timeout, but nothing critical\n");
5159 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
5161 /* We should exit now, since timeout is processed */
5165 /* Per-Lun scanning of timeouts is needed... */
5166 for (Lun
= 0; Lun
< LUS_NUMBER
; Lun
++)
5168 LunExtension
= DeviceExtension
->LunExtensionList
[Lun
];
5170 while (LunExtension
)
5172 if (LunExtension
->Flags
& LUNEX_BUSY
)
5174 if (!(LunExtension
->Flags
&
5175 (LUNEX_NEED_REQUEST_SENSE
| LUNEX_FROZEN_QUEUE
)))
5177 DPRINT("Retrying busy request\n");
5179 /* Clear flags, and retry busy request */
5180 LunExtension
->Flags
&= ~(LUNEX_BUSY
| LUNEX_FULL_QUEUE
);
5181 Irp
= LunExtension
->BusyRequest
;
5183 /* Clearing busy request */
5184 LunExtension
->BusyRequest
= NULL
;
5186 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
5188 IoStartPacket(DeviceObject
, Irp
, (PULONG
)NULL
, NULL
);
5190 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
5193 else if (LunExtension
->RequestTimeout
== 0)
5195 RESETBUS_PARAMS ResetParams
;
5197 LunExtension
->RequestTimeout
= -1;
5199 DPRINT("Request timed out, resetting bus\n");
5201 /* Pass params to the bus reset routine */
5202 ResetParams
.PathId
= LunExtension
->PathId
;
5203 ResetParams
.DeviceExtension
= DeviceExtension
;
5205 if (!KeSynchronizeExecution(DeviceExtension
->Interrupt
[0],
5209 DPRINT1("Reset failed\n");
5212 else if (LunExtension
->RequestTimeout
> 0)
5214 /* Decrement the timeout counter */
5215 LunExtension
->RequestTimeout
--;
5218 LunExtension
= LunExtension
->Next
;
5222 /* Release the spinlock */
5223 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
5226 /**********************************************************************
5231 * Builds the registry device map of all device which are attached
5232 * to the given SCSI HBA port. The device map is located at:
5233 * \Registry\Machine\DeviceMap\Scsi
5243 * Name of registry driver service key.
5250 SpiBuildDeviceMap (PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
5251 PUNICODE_STRING RegistryPath
)
5253 PSCSI_PORT_LUN_EXTENSION LunExtension
;
5254 OBJECT_ATTRIBUTES ObjectAttributes
;
5255 UNICODE_STRING KeyName
;
5256 UNICODE_STRING ValueName
;
5257 WCHAR NameBuffer
[64];
5260 HANDLE ScsiPortKey
= NULL
;
5261 HANDLE ScsiBusKey
= NULL
;
5262 HANDLE ScsiInitiatorKey
= NULL
;
5263 HANDLE ScsiTargetKey
= NULL
;
5264 HANDLE ScsiLunKey
= NULL
;
5267 ULONG CurrentTarget
;
5274 DPRINT("SpiBuildDeviceMap() called\n");
5276 if (DeviceExtension
== NULL
|| RegistryPath
== NULL
)
5278 DPRINT1("Invalid parameter\n");
5279 return(STATUS_INVALID_PARAMETER
);
5282 /* Open or create the 'Scsi' subkey */
5283 RtlInitUnicodeString(&KeyName
,
5284 L
"\\Registry\\Machine\\Hardware\\DeviceMap\\Scsi");
5285 InitializeObjectAttributes(&ObjectAttributes
,
5287 OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
,
5290 Status
= ZwCreateKey(&ScsiKey
,
5295 REG_OPTION_VOLATILE
,
5297 if (!NT_SUCCESS(Status
))
5299 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5303 /* Create new 'Scsi Port X' subkey */
5304 DPRINT("Scsi Port %lu\n",
5305 DeviceExtension
->PortNumber
);
5307 swprintf(NameBuffer
,
5309 DeviceExtension
->PortNumber
);
5310 RtlInitUnicodeString(&KeyName
,
5312 InitializeObjectAttributes(&ObjectAttributes
,
5317 Status
= ZwCreateKey(&ScsiPortKey
,
5322 REG_OPTION_VOLATILE
,
5325 if (!NT_SUCCESS(Status
))
5327 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5332 * Create port-specific values
5335 /* Set 'DMA Enabled' (REG_DWORD) value */
5336 UlongData
= (ULONG
)!DeviceExtension
->PortCapabilities
.AdapterUsesPio
;
5337 DPRINT(" DMA Enabled = %s\n", (UlongData
) ? "TRUE" : "FALSE");
5338 RtlInitUnicodeString(&ValueName
,
5340 Status
= ZwSetValueKey(ScsiPortKey
,
5346 if (!NT_SUCCESS(Status
))
5348 DPRINT("ZwSetValueKey('DMA Enabled') failed (Status %lx)\n", Status
);
5349 ZwClose(ScsiPortKey
);
5353 /* Set 'Driver' (REG_SZ) value */
5354 DriverName
= wcsrchr(RegistryPath
->Buffer
, L
'\\') + 1;
5355 RtlInitUnicodeString(&ValueName
,
5357 Status
= ZwSetValueKey(ScsiPortKey
,
5362 (wcslen(DriverName
) + 1) * sizeof(WCHAR
));
5363 if (!NT_SUCCESS(Status
))
5365 DPRINT("ZwSetValueKey('Driver') failed (Status %lx)\n", Status
);
5366 ZwClose(ScsiPortKey
);
5370 /* Set 'Interrupt' (REG_DWORD) value (NT4 only) */
5371 UlongData
= (ULONG
)DeviceExtension
->PortConfig
->BusInterruptLevel
;
5372 DPRINT(" Interrupt = %lu\n", UlongData
);
5373 RtlInitUnicodeString(&ValueName
,
5375 Status
= ZwSetValueKey(ScsiPortKey
,
5381 if (!NT_SUCCESS(Status
))
5383 DPRINT("ZwSetValueKey('Interrupt') failed (Status %lx)\n", Status
);
5384 ZwClose(ScsiPortKey
);
5388 /* Set 'IOAddress' (REG_DWORD) value (NT4 only) */
5389 UlongData
= ScsiPortConvertPhysicalAddressToUlong((*DeviceExtension
->PortConfig
->AccessRanges
)[0].RangeStart
);
5390 DPRINT(" IOAddress = %lx\n", UlongData
);
5391 RtlInitUnicodeString(&ValueName
,
5393 Status
= ZwSetValueKey(ScsiPortKey
,
5399 if (!NT_SUCCESS(Status
))
5401 DPRINT("ZwSetValueKey('IOAddress') failed (Status %lx)\n", Status
);
5402 ZwClose(ScsiPortKey
);
5406 /* Enumerate buses */
5407 for (BusNumber
= 0; BusNumber
< DeviceExtension
->PortConfig
->NumberOfBuses
; BusNumber
++)
5409 /* Create 'Scsi Bus X' key */
5410 DPRINT(" Scsi Bus %lu\n", BusNumber
);
5411 swprintf(NameBuffer
,
5414 RtlInitUnicodeString(&KeyName
,
5416 InitializeObjectAttributes(&ObjectAttributes
,
5421 Status
= ZwCreateKey(&ScsiBusKey
,
5426 REG_OPTION_VOLATILE
,
5428 if (!NT_SUCCESS(Status
))
5430 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5434 /* Create 'Initiator Id X' key */
5435 DPRINT(" Initiator Id %u\n",
5436 DeviceExtension
->PortConfig
->InitiatorBusId
[BusNumber
]);
5437 swprintf(NameBuffer
,
5439 (unsigned int)(UCHAR
)DeviceExtension
->PortConfig
->InitiatorBusId
[BusNumber
]);
5440 RtlInitUnicodeString(&KeyName
,
5442 InitializeObjectAttributes(&ObjectAttributes
,
5447 Status
= ZwCreateKey(&ScsiInitiatorKey
,
5452 REG_OPTION_VOLATILE
,
5454 if (!NT_SUCCESS(Status
))
5456 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5460 /* FIXME: Are there any initiator values (??) */
5462 ZwClose(ScsiInitiatorKey
);
5463 ScsiInitiatorKey
= NULL
;
5466 /* Enumerate targets */
5467 CurrentTarget
= (ULONG
)-1;
5468 ScsiTargetKey
= NULL
;
5469 for (Target
= 0; Target
< DeviceExtension
->PortConfig
->MaximumNumberOfTargets
; Target
++)
5471 for (Lun
= 0; Lun
< SCSI_MAXIMUM_LOGICAL_UNITS
; Lun
++)
5473 LunExtension
= SpiGetLunExtension(DeviceExtension
,
5477 if (LunExtension
!= NULL
)
5479 if (Target
!= CurrentTarget
)
5481 /* Close old target key */
5482 if (ScsiTargetKey
!= NULL
)
5484 ZwClose(ScsiTargetKey
);
5485 ScsiTargetKey
= NULL
;
5488 /* Create 'Target Id X' key */
5489 DPRINT(" Target Id %lu\n", Target
);
5490 swprintf(NameBuffer
,
5493 RtlInitUnicodeString(&KeyName
,
5495 InitializeObjectAttributes(&ObjectAttributes
,
5500 Status
= ZwCreateKey(&ScsiTargetKey
,
5505 REG_OPTION_VOLATILE
,
5507 if (!NT_SUCCESS(Status
))
5509 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5513 CurrentTarget
= Target
;
5516 /* Create 'Logical Unit Id X' key */
5517 DPRINT(" Logical Unit Id %lu\n", Lun
);
5518 swprintf(NameBuffer
,
5519 L
"Logical Unit Id %lu",
5521 RtlInitUnicodeString(&KeyName
,
5523 InitializeObjectAttributes(&ObjectAttributes
,
5528 Status
= ZwCreateKey(&ScsiLunKey
,
5533 REG_OPTION_VOLATILE
,
5535 if (!NT_SUCCESS(Status
))
5537 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5541 /* Set 'Identifier' (REG_SZ) value */
5542 swprintf(NameBuffer
,
5544 LunExtension
->InquiryData
.VendorId
,
5545 LunExtension
->InquiryData
.ProductId
,
5546 LunExtension
->InquiryData
.ProductRevisionLevel
);
5547 DPRINT(" Identifier = '%S'\n", NameBuffer
);
5548 RtlInitUnicodeString(&ValueName
,
5550 Status
= ZwSetValueKey(ScsiLunKey
,
5555 (wcslen(NameBuffer
) + 1) * sizeof(WCHAR
));
5556 if (!NT_SUCCESS(Status
))
5558 DPRINT("ZwSetValueKey('Identifier') failed (Status %lx)\n", Status
);
5562 /* Set 'Type' (REG_SZ) value */
5563 switch (LunExtension
->InquiryData
.DeviceType
)
5566 TypeName
= L
"DiskPeripheral";
5569 TypeName
= L
"TapePeripheral";
5572 TypeName
= L
"PrinterPeripheral";
5575 TypeName
= L
"WormPeripheral";
5578 TypeName
= L
"CdRomPeripheral";
5581 TypeName
= L
"ScannerPeripheral";
5584 TypeName
= L
"OpticalDiskPeripheral";
5587 TypeName
= L
"MediumChangerPeripheral";
5590 TypeName
= L
"CommunicationPeripheral";
5593 TypeName
= L
"OtherPeripheral";
5596 DPRINT(" Type = '%S'\n", TypeName
);
5597 RtlInitUnicodeString(&ValueName
,
5599 Status
= ZwSetValueKey(ScsiLunKey
,
5604 (wcslen(TypeName
) + 1) * sizeof(WCHAR
));
5605 if (!NT_SUCCESS(Status
))
5607 DPRINT("ZwSetValueKey('Type') failed (Status %lx)\n", Status
);
5611 ZwClose(ScsiLunKey
);
5616 /* Close old target key */
5617 if (ScsiTargetKey
!= NULL
)
5619 ZwClose(ScsiTargetKey
);
5620 ScsiTargetKey
= NULL
;
5624 ZwClose(ScsiBusKey
);
5629 if (ScsiLunKey
!= NULL
)
5630 ZwClose (ScsiLunKey
);
5632 if (ScsiInitiatorKey
!= NULL
)
5633 ZwClose (ScsiInitiatorKey
);
5635 if (ScsiTargetKey
!= NULL
)
5636 ZwClose (ScsiTargetKey
);
5638 if (ScsiBusKey
!= NULL
)
5639 ZwClose (ScsiBusKey
);
5641 if (ScsiPortKey
!= NULL
)
5642 ZwClose (ScsiPortKey
);
5644 DPRINT("SpiBuildDeviceMap() done\n");
5651 SpiMiniportTimerDpc(IN
struct _KDPC
*Dpc
,
5652 IN PVOID DeviceObject
,
5653 IN PVOID SystemArgument1
,
5654 IN PVOID SystemArgument2
)
5656 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
5658 DPRINT("Miniport timer DPC\n");
5660 DeviceExtension
= ((PDEVICE_OBJECT
)DeviceObject
)->DeviceExtension
;
5662 /* Acquire the spinlock */
5663 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
5665 /* Call the timer routine */
5666 if (DeviceExtension
->HwScsiTimer
!= NULL
)
5668 DeviceExtension
->HwScsiTimer(&DeviceExtension
->MiniPortDeviceExtension
);
5671 /* Release the spinlock */
5672 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
5674 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
5676 ScsiPortDpcForIsr(NULL
,
5677 DeviceExtension
->DeviceObject
,
5684 SpiCreatePortConfig(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
5685 PHW_INITIALIZATION_DATA HwInitData
,
5686 PCONFIGURATION_INFO InternalConfigInfo
,
5687 PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
5690 UNICODE_STRING UnicodeString
;
5691 OBJECT_ATTRIBUTES ObjectAttributes
;
5692 PCONFIGURATION_INFORMATION DdkConfigInformation
;
5693 HANDLE RootKey
, Key
;
5695 WCHAR DeviceBuffer
[16];
5696 WCHAR StrBuffer
[512];
5700 /* Zero out the struct if told so */
5703 /* First zero the portconfig */
5704 RtlZeroMemory(ConfigInfo
, sizeof(PORT_CONFIGURATION_INFORMATION
));
5706 /* Then access ranges */
5707 RtlZeroMemory(InternalConfigInfo
->AccessRanges
,
5708 HwInitData
->NumberOfAccessRanges
* sizeof(ACCESS_RANGE
));
5710 /* Initialize the struct */
5711 ConfigInfo
->Length
= sizeof(PORT_CONFIGURATION_INFORMATION
);
5712 ConfigInfo
->AdapterInterfaceType
= HwInitData
->AdapterInterfaceType
;
5713 ConfigInfo
->InterruptMode
= Latched
;
5714 ConfigInfo
->DmaChannel
= SP_UNINITIALIZED_VALUE
;
5715 ConfigInfo
->DmaPort
= SP_UNINITIALIZED_VALUE
;
5716 ConfigInfo
->DmaChannel2
= SP_UNINITIALIZED_VALUE
;
5717 ConfigInfo
->DmaPort2
= SP_UNINITIALIZED_VALUE
;
5718 ConfigInfo
->MaximumTransferLength
= SP_UNINITIALIZED_VALUE
;
5719 ConfigInfo
->NumberOfAccessRanges
= HwInitData
->NumberOfAccessRanges
;
5720 ConfigInfo
->MaximumNumberOfTargets
= 8;
5722 /* Store parameters */
5723 ConfigInfo
->NeedPhysicalAddresses
= HwInitData
->NeedPhysicalAddresses
;
5724 ConfigInfo
->MapBuffers
= HwInitData
->MapBuffers
;
5725 ConfigInfo
->AutoRequestSense
= HwInitData
->AutoRequestSense
;
5726 ConfigInfo
->ReceiveEvent
= HwInitData
->ReceiveEvent
;
5727 ConfigInfo
->TaggedQueuing
= HwInitData
->TaggedQueuing
;
5728 ConfigInfo
->MultipleRequestPerLu
= HwInitData
->MultipleRequestPerLu
;
5730 /* Get the disk usage */
5731 DdkConfigInformation
= IoGetConfigurationInformation();
5732 ConfigInfo
->AtdiskPrimaryClaimed
= DdkConfigInformation
->AtDiskPrimaryAddressClaimed
;
5733 ConfigInfo
->AtdiskSecondaryClaimed
= DdkConfigInformation
->AtDiskSecondaryAddressClaimed
;
5735 /* Initiator bus id is not set */
5736 for (Bus
= 0; Bus
< 8; Bus
++)
5737 ConfigInfo
->InitiatorBusId
[Bus
] = (CCHAR
)SP_UNINITIALIZED_VALUE
;
5740 ConfigInfo
->NumberOfPhysicalBreaks
= 17;
5742 /* Clear this information */
5743 InternalConfigInfo
->DisableTaggedQueueing
= FALSE
;
5744 InternalConfigInfo
->DisableMultipleLun
= FALSE
;
5746 /* Store Bus Number */
5747 ConfigInfo
->SystemIoBusNumber
= InternalConfigInfo
->BusNumber
;
5751 if (ConfigInfo
->AdapterInterfaceType
== Internal
)
5753 /* Open registry key for HW database */
5754 InitializeObjectAttributes(&ObjectAttributes
,
5755 DeviceExtension
->DeviceObject
->DriverObject
->HardwareDatabase
,
5756 OBJ_CASE_INSENSITIVE
,
5760 Status
= ZwOpenKey(&RootKey
,
5764 if (NT_SUCCESS(Status
))
5766 /* Create name for it */
5767 swprintf(StrBuffer
, L
"ScsiAdapter\\%lu",
5768 InternalConfigInfo
->AdapterNumber
);
5770 RtlInitUnicodeString(&UnicodeString
, StrBuffer
);
5772 /* Open device key */
5773 InitializeObjectAttributes(&ObjectAttributes
,
5775 OBJ_CASE_INSENSITIVE
,
5779 Status
= ZwOpenKey(&Key
,
5785 if (NT_SUCCESS(Status
))
5787 if (InternalConfigInfo
->LastAdapterNumber
!= InternalConfigInfo
->AdapterNumber
)
5789 DPRINT("Hardware info found at %S\n", StrBuffer
);
5792 SpiParseDeviceInfo(DeviceExtension
,
5798 InternalConfigInfo
->BusNumber
= 0;
5802 /* Try the next adapter */
5803 InternalConfigInfo
->AdapterNumber
++;
5809 /* Info was not found, exit */
5810 DPRINT1("ZwOpenKey() failed with Status=0x%08X\n", Status
);
5811 return STATUS_DEVICE_DOES_NOT_EXIST
;
5816 DPRINT1("ZwOpenKey() failed with Status=0x%08X\n", Status
);
5820 /* Look at device params */
5822 if (InternalConfigInfo
->Parameter
)
5824 ExFreePool(InternalConfigInfo
->Parameter
);
5825 InternalConfigInfo
->Parameter
= NULL
;
5828 if (InternalConfigInfo
->ServiceKey
!= NULL
)
5830 swprintf(DeviceBuffer
, L
"Device%lu", InternalConfigInfo
->AdapterNumber
);
5831 RtlInitUnicodeString(&UnicodeString
, DeviceBuffer
);
5833 /* Open the service key */
5834 InitializeObjectAttributes(&ObjectAttributes
,
5836 OBJ_CASE_INSENSITIVE
,
5837 InternalConfigInfo
->ServiceKey
,
5840 Status
= ZwOpenKey(&Key
,
5845 /* Parse device key */
5846 if (InternalConfigInfo
->DeviceKey
!= NULL
)
5848 SpiParseDeviceInfo(DeviceExtension
,
5849 InternalConfigInfo
->DeviceKey
,
5855 /* Then parse hw info */
5858 if (InternalConfigInfo
->LastAdapterNumber
!= InternalConfigInfo
->AdapterNumber
)
5860 SpiParseDeviceInfo(DeviceExtension
,
5871 /* Adapter not found, go try the next one */
5872 InternalConfigInfo
->AdapterNumber
++;
5881 /* Update the last adapter number */
5882 InternalConfigInfo
->LastAdapterNumber
= InternalConfigInfo
->AdapterNumber
;
5884 /* Do we have this kind of bus at all? */
5886 Status
= IoQueryDeviceDescription(&HwInitData
->AdapterInterfaceType
,
5887 &InternalConfigInfo
->BusNumber
,
5892 SpQueryDeviceCallout
,
5895 /* This bus was not found */
5898 INTERFACE_TYPE InterfaceType
= Eisa
;
5900 /* Check for EISA */
5901 if (HwInitData
->AdapterInterfaceType
== Isa
)
5903 Status
= IoQueryDeviceDescription(&InterfaceType
,
5904 &InternalConfigInfo
->BusNumber
,
5909 SpQueryDeviceCallout
,
5912 /* Return respectively */
5914 return STATUS_SUCCESS
;
5916 return STATUS_DEVICE_DOES_NOT_EXIST
;
5920 return STATUS_DEVICE_DOES_NOT_EXIST
;
5925 return STATUS_SUCCESS
;
5930 SpiParseDeviceInfo(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
5932 IN PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
5933 IN PCONFIGURATION_INFO InternalConfigInfo
,
5936 PKEY_VALUE_FULL_INFORMATION KeyValueInformation
;
5937 PCM_FULL_RESOURCE_DESCRIPTOR FullResource
;
5938 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
5939 PCM_SCSI_DEVICE_DATA ScsiDeviceData
;
5940 ULONG Length
, Count
, Dma
= 0, Interrupt
= 0;
5941 ULONG Index
= 0, RangeCount
= 0;
5942 UNICODE_STRING UnicodeString
;
5943 ANSI_STRING AnsiString
;
5944 NTSTATUS Status
= STATUS_SUCCESS
;
5947 KeyValueInformation
= (PKEY_VALUE_FULL_INFORMATION
) Buffer
;
5949 /* Loop through all values in the device node */
5952 Status
= ZwEnumerateValueKey(Key
,
5954 KeyValueFullInformation
,
5959 if (!NT_SUCCESS(Status
))
5964 /* Length for DWORD is ok? */
5965 if (KeyValueInformation
->Type
== REG_DWORD
&&
5966 KeyValueInformation
->DataLength
!= sizeof(ULONG
))
5971 /* Get MaximumLogicalUnit */
5972 if (_wcsnicmp(KeyValueInformation
->Name
, L
"MaximumLogicalUnit",
5973 KeyValueInformation
->NameLength
/2) == 0)
5976 if (KeyValueInformation
->Type
!= REG_DWORD
)
5978 DPRINT("Bad data type for MaximumLogicalUnit\n");
5982 DeviceExtension
->MaxLunCount
= *((PUCHAR
)
5983 (Buffer
+ KeyValueInformation
->DataOffset
));
5985 /* Check / reset if needed */
5986 if (DeviceExtension
->MaxLunCount
> SCSI_MAXIMUM_LOGICAL_UNITS
)
5987 DeviceExtension
->MaxLunCount
= SCSI_MAXIMUM_LOGICAL_UNITS
;
5989 DPRINT("MaximumLogicalUnit = %d\n", DeviceExtension
->MaxLunCount
);
5992 /* Get InitiatorTargetId */
5993 if (_wcsnicmp(KeyValueInformation
->Name
, L
"InitiatorTargetId",
5994 KeyValueInformation
->NameLength
/ 2) == 0)
5997 if (KeyValueInformation
->Type
!= REG_DWORD
)
5999 DPRINT("Bad data type for InitiatorTargetId\n");
6003 ConfigInfo
->InitiatorBusId
[0] = *((PUCHAR
)
6004 (Buffer
+ KeyValueInformation
->DataOffset
));
6006 /* Check / reset if needed */
6007 if (ConfigInfo
->InitiatorBusId
[0] > ConfigInfo
->MaximumNumberOfTargets
- 1)
6008 ConfigInfo
->InitiatorBusId
[0] = (CCHAR
)-1;
6010 DPRINT("InitiatorTargetId = %d\n", ConfigInfo
->InitiatorBusId
[0]);
6014 if (_wcsnicmp(KeyValueInformation
->Name
, L
"ScsiDebug",
6015 KeyValueInformation
->NameLength
/2) == 0)
6017 DPRINT("ScsiDebug key not supported\n");
6020 /* Check for a breakpoint */
6021 if (_wcsnicmp(KeyValueInformation
->Name
, L
"BreakPointOnEntry",
6022 KeyValueInformation
->NameLength
/2) == 0)
6024 DPRINT1("Breakpoint on entry requested!\n");
6028 /* Get DisableSynchronousTransfers */
6029 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DisableSynchronousTransfers",
6030 KeyValueInformation
->NameLength
/2) == 0)
6032 DeviceExtension
->SrbFlags
|= SRB_FLAGS_DISABLE_SYNCH_TRANSFER
;
6033 DPRINT("Synch transfers disabled\n");
6036 /* Get DisableDisconnects */
6037 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DisableDisconnects",
6038 KeyValueInformation
->NameLength
/2) == 0)
6040 DeviceExtension
->SrbFlags
|= SRB_FLAGS_DISABLE_DISCONNECT
;
6041 DPRINT("Disconnects disabled\n");
6044 /* Get DisableTaggedQueuing */
6045 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DisableTaggedQueuing",
6046 KeyValueInformation
->NameLength
/2) == 0)
6048 InternalConfigInfo
->DisableTaggedQueueing
= TRUE
;
6049 DPRINT("Tagged queueing disabled\n");
6052 /* Get DisableMultipleRequests */
6053 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DisableMultipleRequests",
6054 KeyValueInformation
->NameLength
/2) == 0)
6056 InternalConfigInfo
->DisableMultipleLun
= TRUE
;
6057 DPRINT("Multiple requests disabled\n");
6060 /* Get DriverParameters */
6061 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DriverParameters",
6062 KeyValueInformation
->NameLength
/2) == 0)
6064 /* Skip if nothing */
6065 if (KeyValueInformation
->DataLength
== 0)
6068 /* If there was something previously allocated - free it */
6069 if (InternalConfigInfo
->Parameter
!= NULL
)
6070 ExFreePool(InternalConfigInfo
->Parameter
);
6073 InternalConfigInfo
->Parameter
= ExAllocatePoolWithTag(NonPagedPool
,
6074 KeyValueInformation
->DataLength
, TAG_SCSIPORT
);
6076 if (InternalConfigInfo
->Parameter
!= NULL
)
6078 if (KeyValueInformation
->Type
!= REG_SZ
)
6082 InternalConfigInfo
->Parameter
,
6083 (PCCHAR
)KeyValueInformation
+ KeyValueInformation
->DataOffset
,
6084 KeyValueInformation
->DataLength
);
6088 /* If it's a unicode string, convert it to ansi */
6089 UnicodeString
.Length
= (USHORT
)KeyValueInformation
->DataLength
;
6090 UnicodeString
.MaximumLength
= (USHORT
)KeyValueInformation
->DataLength
;
6091 UnicodeString
.Buffer
=
6092 (PWSTR
)((PCCHAR
)KeyValueInformation
+ KeyValueInformation
->DataOffset
);
6094 AnsiString
.Length
= 0;
6095 AnsiString
.MaximumLength
= (USHORT
)KeyValueInformation
->DataLength
;
6096 AnsiString
.Buffer
= (PCHAR
)InternalConfigInfo
->Parameter
;
6098 Status
= RtlUnicodeStringToAnsiString(&AnsiString
,
6102 /* In case of error, free the allocated space */
6103 if (!NT_SUCCESS(Status
))
6105 ExFreePool(InternalConfigInfo
->Parameter
);
6106 InternalConfigInfo
->Parameter
= NULL
;
6112 DPRINT("Found driver parameter\n");
6115 /* Get MaximumSGList */
6116 if (_wcsnicmp(KeyValueInformation
->Name
, L
"MaximumSGList",
6117 KeyValueInformation
->NameLength
/2) == 0)
6119 if (KeyValueInformation
->Type
!= REG_DWORD
)
6121 DPRINT("Bad data type for MaximumSGList\n");
6125 ConfigInfo
->NumberOfPhysicalBreaks
= *((PUCHAR
)(Buffer
+ KeyValueInformation
->DataOffset
));
6128 if (ConfigInfo
->NumberOfPhysicalBreaks
> SCSI_MAXIMUM_PHYSICAL_BREAKS
)
6130 ConfigInfo
->NumberOfPhysicalBreaks
= SCSI_MAXIMUM_PHYSICAL_BREAKS
;
6132 else if (ConfigInfo
->NumberOfPhysicalBreaks
< SCSI_MINIMUM_PHYSICAL_BREAKS
)
6134 ConfigInfo
->NumberOfPhysicalBreaks
= SCSI_MINIMUM_PHYSICAL_BREAKS
;
6137 DPRINT("MaximumSGList = %d\n", ConfigInfo
->NumberOfPhysicalBreaks
);
6140 /* Get NumberOfRequests */
6141 if (_wcsnicmp(KeyValueInformation
->Name
, L
"NumberOfRequests",
6142 KeyValueInformation
->NameLength
/2) == 0)
6144 if (KeyValueInformation
->Type
!= REG_DWORD
)
6146 DPRINT("NumberOfRequests has wrong data type\n");
6150 DeviceExtension
->RequestsNumber
= *((PUCHAR
)(Buffer
+ KeyValueInformation
->DataOffset
));
6153 if (DeviceExtension
->RequestsNumber
< 16)
6155 DeviceExtension
->RequestsNumber
= 16;
6157 else if (DeviceExtension
->RequestsNumber
> 512)
6159 DeviceExtension
->RequestsNumber
= 512;
6162 DPRINT("Number Of Requests = %d\n", DeviceExtension
->RequestsNumber
);
6165 /* Get resource list */
6166 if (_wcsnicmp(KeyValueInformation
->Name
, L
"ResourceList",
6167 KeyValueInformation
->NameLength
/2) == 0 ||
6168 _wcsnicmp(KeyValueInformation
->Name
, L
"Configuration Data",
6169 KeyValueInformation
->NameLength
/2) == 0 )
6171 if (KeyValueInformation
->Type
!= REG_FULL_RESOURCE_DESCRIPTOR
||
6172 KeyValueInformation
->DataLength
< sizeof(REG_FULL_RESOURCE_DESCRIPTOR
))
6174 DPRINT("Bad data type for ResourceList\n");
6179 DPRINT("Found ResourceList\n");
6182 FullResource
= (PCM_FULL_RESOURCE_DESCRIPTOR
)(Buffer
+ KeyValueInformation
->DataOffset
);
6184 /* Copy some info from it */
6185 InternalConfigInfo
->BusNumber
= FullResource
->BusNumber
;
6186 ConfigInfo
->SystemIoBusNumber
= FullResource
->BusNumber
;
6188 /* Loop through it */
6189 for (Count
= 0; Count
< FullResource
->PartialResourceList
.Count
; Count
++)
6191 /* Get partial descriptor */
6193 &FullResource
->PartialResourceList
.PartialDescriptors
[Count
];
6195 /* Check datalength */
6196 if ((ULONG
)((PCHAR
)(PartialDescriptor
+ 1) -
6197 (PCHAR
)FullResource
) > KeyValueInformation
->DataLength
)
6199 DPRINT("Resource data is of incorrect size\n");
6203 switch (PartialDescriptor
->Type
)
6205 case CmResourceTypePort
:
6206 if (RangeCount
>= ConfigInfo
->NumberOfAccessRanges
)
6208 DPRINT("Too many access ranges\n");
6212 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeInMemory
= FALSE
;
6213 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeStart
= PartialDescriptor
->u
.Port
.Start
;
6214 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeLength
= PartialDescriptor
->u
.Port
.Length
;
6219 case CmResourceTypeMemory
:
6220 if (RangeCount
>= ConfigInfo
->NumberOfAccessRanges
)
6222 DPRINT("Too many access ranges\n");
6226 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeInMemory
= TRUE
;
6227 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeStart
= PartialDescriptor
->u
.Memory
.Start
;
6228 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeLength
= PartialDescriptor
->u
.Memory
.Length
;
6233 case CmResourceTypeInterrupt
:
6237 ConfigInfo
->BusInterruptLevel
=
6238 PartialDescriptor
->u
.Interrupt
.Level
;
6240 ConfigInfo
->BusInterruptVector
=
6241 PartialDescriptor
->u
.Interrupt
.Vector
;
6243 ConfigInfo
->InterruptMode
= (PartialDescriptor
->Flags
& CM_RESOURCE_INTERRUPT_LATCHED
) ? Latched
: LevelSensitive
;
6245 else if (Interrupt
== 1)
6247 ConfigInfo
->BusInterruptLevel2
=
6248 PartialDescriptor
->u
.Interrupt
.Level
;
6250 ConfigInfo
->BusInterruptVector2
=
6251 PartialDescriptor
->u
.Interrupt
.Vector
;
6253 ConfigInfo
->InterruptMode2
= (PartialDescriptor
->Flags
& CM_RESOURCE_INTERRUPT_LATCHED
) ? Latched
: LevelSensitive
;
6259 case CmResourceTypeDma
:
6263 ConfigInfo
->DmaChannel
= PartialDescriptor
->u
.Dma
.Channel
;
6264 ConfigInfo
->DmaPort
= PartialDescriptor
->u
.Dma
.Port
;
6266 if (PartialDescriptor
->Flags
& CM_RESOURCE_DMA_8
)
6267 ConfigInfo
->DmaWidth
= Width8Bits
;
6268 else if ((PartialDescriptor
->Flags
& CM_RESOURCE_DMA_16
) ||
6269 (PartialDescriptor
->Flags
& CM_RESOURCE_DMA_8_AND_16
)) //???
6270 ConfigInfo
->DmaWidth
= Width16Bits
;
6271 else if (PartialDescriptor
->Flags
& CM_RESOURCE_DMA_32
)
6272 ConfigInfo
->DmaWidth
= Width32Bits
;
6276 ConfigInfo
->DmaChannel2
= PartialDescriptor
->u
.Dma
.Channel
;
6277 ConfigInfo
->DmaPort2
= PartialDescriptor
->u
.Dma
.Port
;
6279 if (PartialDescriptor
->Flags
& CM_RESOURCE_DMA_8
)
6280 ConfigInfo
->DmaWidth2
= Width8Bits
;
6281 else if ((PartialDescriptor
->Flags
& CM_RESOURCE_DMA_16
) ||
6282 (PartialDescriptor
->Flags
& CM_RESOURCE_DMA_8_AND_16
)) //???
6283 ConfigInfo
->DmaWidth2
= Width16Bits
;
6284 else if (PartialDescriptor
->Flags
& CM_RESOURCE_DMA_32
)
6285 ConfigInfo
->DmaWidth2
= Width32Bits
;
6291 case CmResourceTypeDeviceSpecific
:
6292 if (PartialDescriptor
->u
.DeviceSpecificData
.DataSize
<
6293 sizeof(CM_SCSI_DEVICE_DATA
) ||
6294 (PCHAR
) (PartialDescriptor
+ 1) - (PCHAR
)FullResource
+
6295 PartialDescriptor
->u
.DeviceSpecificData
.DataSize
>
6296 KeyValueInformation
->DataLength
)
6298 DPRINT("Resource data length is incorrect");
6302 /* Set only one field from it */
6303 ScsiDeviceData
= (PCM_SCSI_DEVICE_DATA
) (PartialDescriptor
+1);
6304 ConfigInfo
->InitiatorBusId
[0] = ScsiDeviceData
->HostIdentifier
;
6314 SpQueryDeviceCallout(IN PVOID Context
,
6315 IN PUNICODE_STRING PathName
,
6316 IN INTERFACE_TYPE BusType
,
6318 IN PKEY_VALUE_FULL_INFORMATION
*BusInformation
,
6319 IN CONFIGURATION_TYPE ControllerType
,
6320 IN ULONG ControllerNumber
,
6321 IN PKEY_VALUE_FULL_INFORMATION
*ControllerInformation
,
6322 IN CONFIGURATION_TYPE PeripheralType
,
6323 IN ULONG PeripheralNumber
,
6324 IN PKEY_VALUE_FULL_INFORMATION
*PeripheralInformation
)
6326 PBOOLEAN Found
= (PBOOLEAN
)Context
;
6327 /* We just set our Found variable to TRUE */
6330 return STATUS_SUCCESS
;
6333 IO_ALLOCATION_ACTION
6335 ScsiPortAllocateAdapterChannel(IN PDEVICE_OBJECT DeviceObject
,
6337 IN PVOID MapRegisterBase
,
6341 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
= DeviceObject
->DeviceExtension
;
6343 /* Guard access with the spinlock */
6344 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
6346 /* Save MapRegisterBase we've got here */
6347 DeviceExtension
->MapRegisterBase
= MapRegisterBase
;
6349 /* Start pending request */
6350 KeSynchronizeExecution(DeviceExtension
->Interrupt
[0],
6351 ScsiPortStartPacket
, DeviceObject
);
6353 /* Release spinlock we took */
6354 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
6361 SpiStatusSrbToNt(UCHAR SrbStatus
)
6363 switch (SRB_STATUS(SrbStatus
))
6365 case SRB_STATUS_TIMEOUT
:
6366 case SRB_STATUS_COMMAND_TIMEOUT
:
6367 return STATUS_IO_TIMEOUT
;
6369 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH
:
6370 case SRB_STATUS_BAD_FUNCTION
:
6371 return STATUS_INVALID_DEVICE_REQUEST
;
6373 case SRB_STATUS_NO_DEVICE
:
6374 case SRB_STATUS_INVALID_LUN
:
6375 case SRB_STATUS_INVALID_TARGET_ID
:
6376 case SRB_STATUS_NO_HBA
:
6377 return STATUS_DEVICE_DOES_NOT_EXIST
;
6379 case SRB_STATUS_DATA_OVERRUN
:
6380 return STATUS_BUFFER_OVERFLOW
;
6382 case SRB_STATUS_SELECTION_TIMEOUT
:
6383 return STATUS_DEVICE_NOT_CONNECTED
;
6386 return STATUS_IO_DEVICE_ERROR
;
6389 return STATUS_IO_DEVICE_ERROR
;
6393 #undef ScsiPortConvertPhysicalAddressToUlong
6398 ScsiPortConvertPhysicalAddressToUlong(IN SCSI_PHYSICAL_ADDRESS Address
)
6400 DPRINT("ScsiPortConvertPhysicalAddressToUlong()\n");
6401 return(Address
.u
.LowPart
);