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_SCSI
] = ScsiPortDispatchScsi
;
1060 /* Obtain configuration information */
1061 SystemConfig
= IoGetConfigurationInformation();
1063 /* Zero the internal configuration info structure */
1064 RtlZeroMemory(&ConfigInfo
, sizeof(CONFIGURATION_INFO
));
1066 /* Zero starting slot number */
1067 SlotNumber
.u
.AsULONG
= 0;
1069 /* Allocate space for access ranges */
1070 if (HwInitializationData
->NumberOfAccessRanges
)
1072 ConfigInfo
.AccessRanges
=
1073 ExAllocatePoolWithTag(PagedPool
,
1074 HwInitializationData
->NumberOfAccessRanges
* sizeof(ACCESS_RANGE
), TAG_SCSIPORT
);
1076 /* Fail if failed */
1077 if (ConfigInfo
.AccessRanges
== NULL
)
1078 return STATUS_INSUFFICIENT_RESOURCES
;
1081 /* Open registry keys */
1082 SpiInitOpenKeys(&ConfigInfo
, (PUNICODE_STRING
)Argument2
);
1084 /* Last adapter number = not known */
1085 ConfigInfo
.LastAdapterNumber
= SP_UNINITIALIZED_VALUE
;
1087 /* Calculate sizes of DeviceExtension and PortConfig */
1088 DeviceExtensionSize
= sizeof(SCSI_PORT_DEVICE_EXTENSION
) +
1089 HwInitializationData
->DeviceExtensionSize
;
1091 MaxBus
= (HwInitializationData
->AdapterInterfaceType
== PCIBus
) ? 8 : 1;
1092 DPRINT("MaxBus: %lu\n", MaxBus
);
1096 /* Create a unicode device name */
1097 swprintf(NameBuffer
,
1098 L
"\\Device\\ScsiPort%lu",
1099 SystemConfig
->ScsiPortCount
);
1100 RtlInitUnicodeString(&DeviceName
, NameBuffer
);
1102 DPRINT("Creating device: %wZ\n", &DeviceName
);
1104 /* Create the port device */
1105 Status
= IoCreateDevice(DriverObject
,
1106 DeviceExtensionSize
,
1108 FILE_DEVICE_CONTROLLER
,
1113 if (!NT_SUCCESS(Status
))
1115 DPRINT1("IoCreateDevice call failed! (Status 0x%lX)\n", Status
);
1116 PortDeviceObject
= NULL
;
1120 DPRINT ("Created device: %wZ (%p)\n", &DeviceName
, PortDeviceObject
);
1122 /* Set the buffering strategy here... */
1123 PortDeviceObject
->Flags
|= DO_DIRECT_IO
;
1124 PortDeviceObject
->AlignmentRequirement
= FILE_WORD_ALIGNMENT
; /* FIXME: Is this really needed? */
1126 /* Fill Device Extension */
1127 DeviceExtension
= PortDeviceObject
->DeviceExtension
;
1128 RtlZeroMemory(DeviceExtension
, DeviceExtensionSize
);
1129 DeviceExtension
->Length
= DeviceExtensionSize
;
1130 DeviceExtension
->DeviceObject
= PortDeviceObject
;
1131 DeviceExtension
->PortNumber
= SystemConfig
->ScsiPortCount
;
1133 /* Driver's routines... */
1134 DeviceExtension
->HwInitialize
= HwInitializationData
->HwInitialize
;
1135 DeviceExtension
->HwStartIo
= HwInitializationData
->HwStartIo
;
1136 DeviceExtension
->HwInterrupt
= HwInitializationData
->HwInterrupt
;
1137 DeviceExtension
->HwResetBus
= HwInitializationData
->HwResetBus
;
1138 DeviceExtension
->HwDmaStarted
= HwInitializationData
->HwDmaStarted
;
1140 /* Extensions sizes */
1141 DeviceExtension
->MiniPortExtensionSize
= HwInitializationData
->DeviceExtensionSize
;
1142 DeviceExtension
->LunExtensionSize
= HwInitializationData
->SpecificLuExtensionSize
;
1143 DeviceExtension
->SrbExtensionSize
= HwInitializationData
->SrbExtensionSize
;
1145 /* Round Srb extension size to the quadword */
1146 DeviceExtension
->SrbExtensionSize
=
1147 ~(sizeof(LONGLONG
) - 1) & (DeviceExtension
->SrbExtensionSize
+
1148 sizeof(LONGLONG
) - 1);
1150 /* Fill some numbers (bus count, lun count, etc) */
1151 DeviceExtension
->MaxLunCount
= SCSI_MAXIMUM_LOGICAL_UNITS
;
1152 DeviceExtension
->RequestsNumber
= 16;
1154 /* Initialize the spin lock in the controller extension */
1155 KeInitializeSpinLock(&DeviceExtension
->IrqLock
);
1156 KeInitializeSpinLock(&DeviceExtension
->SpinLock
);
1158 /* Initialize the DPC object */
1159 IoInitializeDpcRequest(PortDeviceObject
,
1162 /* Initialize the device timer */
1163 DeviceExtension
->TimerCount
= -1;
1164 IoInitializeTimer(PortDeviceObject
,
1168 /* Initialize miniport timer */
1169 KeInitializeTimer(&DeviceExtension
->MiniportTimer
);
1170 KeInitializeDpc(&DeviceExtension
->MiniportTimerDpc
,
1171 SpiMiniportTimerDpc
,
1176 Status
= SpiCreatePortConfig(DeviceExtension
,
1177 HwInitializationData
,
1182 if (!NT_SUCCESS(Status
))
1184 DPRINT("SpiCreatePortConfig() failed with Status 0x%08X\n", Status
);
1188 /* Allocate and initialize port configuration info */
1189 PortConfigSize
= (sizeof(PORT_CONFIGURATION_INFORMATION
) +
1190 HwInitializationData
->NumberOfAccessRanges
*
1191 sizeof(ACCESS_RANGE
) + 7) & ~7;
1192 DeviceExtension
->PortConfig
= ExAllocatePoolWithTag(NonPagedPool
, PortConfigSize
, TAG_SCSIPORT
);
1194 /* Fail if failed */
1195 if (DeviceExtension
->PortConfig
== NULL
)
1197 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1201 PortConfig
= DeviceExtension
->PortConfig
;
1203 /* Copy information here */
1204 RtlCopyMemory(PortConfig
,
1206 sizeof(PORT_CONFIGURATION_INFORMATION
));
1209 /* Copy extension sizes into the PortConfig */
1210 PortConfig
->SpecificLuExtensionSize
= DeviceExtension
->LunExtensionSize
;
1211 PortConfig
->SrbExtensionSize
= DeviceExtension
->SrbExtensionSize
;
1213 /* Initialize Access ranges */
1214 if (HwInitializationData
->NumberOfAccessRanges
!= 0)
1216 PortConfig
->AccessRanges
= (PVOID
)(PortConfig
+1);
1218 /* Align to LONGLONG */
1219 PortConfig
->AccessRanges
= (PVOID
)((ULONG_PTR
)(PortConfig
->AccessRanges
) + 7);
1220 PortConfig
->AccessRanges
= (PVOID
)((ULONG_PTR
)(PortConfig
->AccessRanges
) & ~7);
1223 RtlCopyMemory(PortConfig
->AccessRanges
,
1224 ConfigInfo
.AccessRanges
,
1225 HwInitializationData
->NumberOfAccessRanges
* sizeof(ACCESS_RANGE
));
1228 /* Search for matching PCI device */
1229 if ((HwInitializationData
->AdapterInterfaceType
== PCIBus
) &&
1230 (HwInitializationData
->VendorIdLength
> 0) &&
1231 (HwInitializationData
->VendorId
!= NULL
) &&
1232 (HwInitializationData
->DeviceIdLength
> 0) &&
1233 (HwInitializationData
->DeviceId
!= NULL
))
1235 PortConfig
->BusInterruptLevel
= 0;
1237 /* Get PCI device data */
1238 DPRINT("VendorId '%.*s' DeviceId '%.*s'\n",
1239 HwInitializationData
->VendorIdLength
,
1240 HwInitializationData
->VendorId
,
1241 HwInitializationData
->DeviceIdLength
,
1242 HwInitializationData
->DeviceId
);
1244 if (!SpiGetPciConfigData(DriverObject
,
1246 HwInitializationData
,
1249 ConfigInfo
.BusNumber
,
1252 /* Continue to the next bus, nothing here */
1253 ConfigInfo
.BusNumber
++;
1254 DeviceExtension
->PortConfig
= NULL
;
1255 ExFreePool(PortConfig
);
1257 goto CreatePortConfig
;
1260 if (!PortConfig
->BusInterruptLevel
)
1262 /* Bypass this slot, because no interrupt was assigned */
1263 DeviceExtension
->PortConfig
= NULL
;
1264 ExFreePool(PortConfig
);
1265 goto CreatePortConfig
;
1270 DPRINT("Non-pci bus\n");
1273 /* Note: HwFindAdapter is called once for each bus */
1275 DPRINT("Calling HwFindAdapter() for Bus %lu\n", PortConfig
->SystemIoBusNumber
);
1276 Result
= (HwInitializationData
->HwFindAdapter
)(&DeviceExtension
->MiniPortDeviceExtension
,
1278 0, /* BusInformation */
1279 ConfigInfo
.Parameter
, /* ArgumentString */
1283 DPRINT("HwFindAdapter() Result: %lu Again: %s\n",
1284 Result
, (Again
) ? "True" : "False");
1286 /* Free MapRegisterBase, it's not needed anymore */
1287 if (DeviceExtension
->MapRegisterBase
!= NULL
)
1289 ExFreePool(DeviceExtension
->MapRegisterBase
);
1290 DeviceExtension
->MapRegisterBase
= NULL
;
1293 /* If result is nothing good... */
1294 if (Result
!= SP_RETURN_FOUND
)
1296 DPRINT("HwFindAdapter() Result: %lu\n", Result
);
1298 if (Result
== SP_RETURN_NOT_FOUND
)
1300 /* We can continue on the next bus */
1301 ConfigInfo
.BusNumber
++;
1304 DeviceExtension
->PortConfig
= NULL
;
1305 ExFreePool(PortConfig
);
1306 goto CreatePortConfig
;
1309 /* Otherwise, break */
1310 Status
= STATUS_INTERNAL_ERROR
;
1314 DPRINT("ScsiPortInitialize(): Found HBA! (%x), adapter Id %d\n",
1315 PortConfig
->BusInterruptVector
, PortConfig
->InitiatorBusId
[0]);
1317 /* If the SRB extension size was updated */
1318 if (!DeviceExtension
->NonCachedExtension
&&
1319 (PortConfig
->SrbExtensionSize
!= DeviceExtension
->SrbExtensionSize
))
1321 /* Set it (rounding to LONGLONG again) */
1322 DeviceExtension
->SrbExtensionSize
=
1323 (PortConfig
->SrbExtensionSize
+
1324 sizeof(LONGLONG
)) & ~(sizeof(LONGLONG
) - 1);
1327 /* The same with LUN extension size */
1328 if (PortConfig
->SpecificLuExtensionSize
!= DeviceExtension
->LunExtensionSize
)
1329 DeviceExtension
->LunExtensionSize
= PortConfig
->SpecificLuExtensionSize
;
1332 if (!((HwInitializationData
->AdapterInterfaceType
== PCIBus
) &&
1333 (HwInitializationData
->VendorIdLength
> 0) &&
1334 (HwInitializationData
->VendorId
!= NULL
) &&
1335 (HwInitializationData
->DeviceIdLength
> 0) &&
1336 (HwInitializationData
->DeviceId
!= NULL
)))
1338 /* Construct a resource list */
1339 ResourceList
= SpiConfigToResource(DeviceExtension
,
1344 UNICODE_STRING UnicodeString
;
1345 RtlInitUnicodeString(&UnicodeString
, L
"ScsiAdapter");
1346 DPRINT("Reporting resources\n");
1347 Status
= IoReportResourceUsage(&UnicodeString
,
1353 FIELD_OFFSET(CM_RESOURCE_LIST
,
1354 List
[0].PartialResourceList
.PartialDescriptors
) +
1355 ResourceList
->List
[0].PartialResourceList
.Count
1356 * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
),
1359 ExFreePool(ResourceList
);
1361 /* In case of a failure or a conflict, break */
1362 if (Conflict
|| (!NT_SUCCESS(Status
)))
1365 Status
= STATUS_CONFLICTING_ADDRESSES
;
1371 /* Reset the Conflict var */
1374 /* Copy all stuff which we ever need from PortConfig to the DeviceExtension */
1375 if (PortConfig
->MaximumNumberOfTargets
> SCSI_MAXIMUM_TARGETS_PER_BUS
)
1376 DeviceExtension
->MaxTargedIds
= SCSI_MAXIMUM_TARGETS_PER_BUS
;
1378 DeviceExtension
->MaxTargedIds
= PortConfig
->MaximumNumberOfTargets
;
1380 DeviceExtension
->BusNum
= PortConfig
->NumberOfBuses
;
1381 DeviceExtension
->CachesData
= PortConfig
->CachesData
;
1382 DeviceExtension
->ReceiveEvent
= PortConfig
->ReceiveEvent
;
1383 DeviceExtension
->SupportsTaggedQueuing
= PortConfig
->TaggedQueuing
;
1384 DeviceExtension
->MultipleReqsPerLun
= PortConfig
->MultipleRequestPerLu
;
1386 /* If something was disabled via registry - apply it */
1387 if (ConfigInfo
.DisableMultipleLun
)
1388 DeviceExtension
->MultipleReqsPerLun
= PortConfig
->MultipleRequestPerLu
= FALSE
;
1390 if (ConfigInfo
.DisableTaggedQueueing
)
1391 DeviceExtension
->SupportsTaggedQueuing
= PortConfig
->MultipleRequestPerLu
= FALSE
;
1393 /* Check if we need to alloc SRB data */
1394 if (DeviceExtension
->SupportsTaggedQueuing
||
1395 DeviceExtension
->MultipleReqsPerLun
)
1397 DeviceExtension
->NeedSrbDataAlloc
= TRUE
;
1401 DeviceExtension
->NeedSrbDataAlloc
= FALSE
;
1404 /* Get a pointer to the port capabilities */
1405 PortCapabilities
= &DeviceExtension
->PortCapabilities
;
1407 /* Copy one field there */
1408 DeviceExtension
->MapBuffers
= PortConfig
->MapBuffers
;
1409 PortCapabilities
->AdapterUsesPio
= PortConfig
->MapBuffers
;
1411 if (DeviceExtension
->AdapterObject
== NULL
&&
1412 (PortConfig
->DmaChannel
!= SP_UNINITIALIZED_VALUE
|| PortConfig
->Master
))
1414 DPRINT1("DMA is not supported yet\n");
1418 if (DeviceExtension
->SrbExtensionBuffer
== NULL
&&
1419 (DeviceExtension
->SrbExtensionSize
!= 0 ||
1420 PortConfig
->AutoRequestSense
))
1422 DeviceExtension
->SupportsAutoSense
= PortConfig
->AutoRequestSense
;
1423 DeviceExtension
->NeedSrbExtensionAlloc
= TRUE
;
1425 /* Allocate common buffer */
1426 Status
= SpiAllocateCommonBuffer(DeviceExtension
, 0);
1428 /* Check for failure */
1429 if (!NT_SUCCESS(Status
))
1433 /* Allocate SrbData, if needed */
1434 if (DeviceExtension
->NeedSrbDataAlloc
)
1437 PSCSI_REQUEST_BLOCK_INFO SrbData
;
1439 if (DeviceExtension
->SrbDataCount
!= 0)
1440 Count
= DeviceExtension
->SrbDataCount
;
1442 Count
= DeviceExtension
->RequestsNumber
* 2;
1444 /* Allocate the data */
1445 SrbData
= ExAllocatePoolWithTag(NonPagedPool
, Count
* sizeof(SCSI_REQUEST_BLOCK_INFO
), TAG_SCSIPORT
);
1446 if (SrbData
== NULL
)
1447 return STATUS_INSUFFICIENT_RESOURCES
;
1449 RtlZeroMemory(SrbData
, Count
* sizeof(SCSI_REQUEST_BLOCK_INFO
));
1451 DeviceExtension
->SrbInfo
= SrbData
;
1452 DeviceExtension
->FreeSrbInfo
= SrbData
;
1453 DeviceExtension
->SrbDataCount
= Count
;
1455 /* Link it to the list */
1458 SrbData
->Requests
.Flink
= (PLIST_ENTRY
)(SrbData
+ 1);
1463 /* Mark the last entry of the list */
1465 SrbData
->Requests
.Flink
= NULL
;
1468 /* Initialize port capabilities */
1469 PortCapabilities
= &DeviceExtension
->PortCapabilities
;
1470 PortCapabilities
->Length
= sizeof(IO_SCSI_CAPABILITIES
);
1471 PortCapabilities
->MaximumTransferLength
= PortConfig
->MaximumTransferLength
;
1473 if (PortConfig
->ReceiveEvent
)
1474 PortCapabilities
->SupportedAsynchronousEvents
|= SRBEV_SCSI_ASYNC_NOTIFICATION
;
1476 PortCapabilities
->TaggedQueuing
= DeviceExtension
->SupportsTaggedQueuing
;
1477 PortCapabilities
->AdapterScansDown
= PortConfig
->AdapterScansDown
;
1479 if (PortConfig
->AlignmentMask
> PortDeviceObject
->AlignmentRequirement
)
1480 PortDeviceObject
->AlignmentRequirement
= PortConfig
->AlignmentMask
;
1482 PortCapabilities
->AlignmentMask
= PortDeviceObject
->AlignmentRequirement
;
1484 if (PortCapabilities
->MaximumPhysicalPages
== 0)
1486 PortCapabilities
->MaximumPhysicalPages
=
1487 BYTES_TO_PAGES(PortCapabilities
->MaximumTransferLength
);
1489 /* Apply miniport's limits */
1490 if (PortConfig
->NumberOfPhysicalBreaks
< PortCapabilities
->MaximumPhysicalPages
)
1492 PortCapabilities
->MaximumPhysicalPages
= PortConfig
->NumberOfPhysicalBreaks
;
1496 /* Deal with interrupts */
1497 if (DeviceExtension
->HwInterrupt
== NULL
||
1498 (PortConfig
->BusInterruptLevel
== 0 && PortConfig
->BusInterruptVector
== 0))
1501 DeviceExtension
->InterruptCount
= 0;
1503 DPRINT1("Interrupt Count: 0\n");
1507 /* This code path will ALWAYS crash so stop it now */
1512 BOOLEAN InterruptShareable
;
1513 KINTERRUPT_MODE InterruptMode
[2];
1514 ULONG InterruptVector
[2], i
, MappedIrq
[2];
1515 KIRQL Dirql
[2], MaxDirql
;
1516 KAFFINITY Affinity
[2];
1518 DeviceExtension
->InterruptLevel
[0] = PortConfig
->BusInterruptLevel
;
1519 DeviceExtension
->InterruptLevel
[1] = PortConfig
->BusInterruptLevel2
;
1521 InterruptVector
[0] = PortConfig
->BusInterruptVector
;
1522 InterruptVector
[1] = PortConfig
->BusInterruptVector2
;
1524 InterruptMode
[0] = PortConfig
->InterruptMode
;
1525 InterruptMode
[1] = PortConfig
->InterruptMode2
;
1527 DeviceExtension
->InterruptCount
= (PortConfig
->BusInterruptLevel2
!= 0 || PortConfig
->BusInterruptVector2
!= 0) ? 2 : 1;
1529 for (i
= 0; i
< DeviceExtension
->InterruptCount
; i
++)
1531 /* Register an interrupt handler for this device */
1532 MappedIrq
[i
] = HalGetInterruptVector(PortConfig
->AdapterInterfaceType
,
1533 PortConfig
->SystemIoBusNumber
,
1534 DeviceExtension
->InterruptLevel
[i
],
1540 if (DeviceExtension
->InterruptCount
== 1 || Dirql
[0] > Dirql
[1])
1541 MaxDirql
= Dirql
[0];
1543 MaxDirql
= Dirql
[1];
1545 for (i
= 0; i
< DeviceExtension
->InterruptCount
; i
++)
1547 /* Determing IRQ sharability as usual */
1548 if (PortConfig
->AdapterInterfaceType
== MicroChannel
||
1549 InterruptMode
[i
] == LevelSensitive
)
1551 InterruptShareable
= TRUE
;
1555 InterruptShareable
= FALSE
;
1558 Status
= IoConnectInterrupt(&DeviceExtension
->Interrupt
[i
],
1559 (PKSERVICE_ROUTINE
)ScsiPortIsr
,
1561 &DeviceExtension
->IrqLock
,
1570 if (!(NT_SUCCESS(Status
)))
1572 DPRINT1("Could not connect interrupt %d\n",
1573 InterruptVector
[i
]);
1574 DeviceExtension
->Interrupt
[i
] = NULL
;
1579 if (!NT_SUCCESS(Status
))
1583 /* Save IoAddress (from access ranges) */
1584 if (HwInitializationData
->NumberOfAccessRanges
!= 0)
1586 DeviceExtension
->IoAddress
=
1587 ((*(PortConfig
->AccessRanges
))[0]).RangeStart
.LowPart
;
1589 DPRINT("Io Address %x\n", DeviceExtension
->IoAddress
);
1592 /* Set flag that it's allowed to disconnect during this command */
1593 DeviceExtension
->Flags
|= SCSI_PORT_DISCONNECT_ALLOWED
;
1595 /* Initialize counter of active requests (-1 means there are none) */
1596 DeviceExtension
->ActiveRequestCounter
= -1;
1598 /* Analyze what we have about DMA */
1599 if (DeviceExtension
->AdapterObject
!= NULL
&&
1600 PortConfig
->Master
&&
1601 PortConfig
->NeedPhysicalAddresses
)
1603 DeviceExtension
->MapRegisters
= TRUE
;
1607 DeviceExtension
->MapRegisters
= FALSE
;
1610 /* Call HwInitialize at DISPATCH_LEVEL */
1611 KeRaiseIrql(DISPATCH_LEVEL
, &OldIrql
);
1613 if (!KeSynchronizeExecution(DeviceExtension
->Interrupt
[0],
1614 DeviceExtension
->HwInitialize
,
1615 DeviceExtension
->MiniPortDeviceExtension
))
1617 DPRINT1("HwInitialize() failed!\n");
1618 KeLowerIrql(OldIrql
);
1619 Status
= STATUS_ADAPTER_HARDWARE_ERROR
;
1623 /* Check if a notification is needed */
1624 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
1626 /* Call DPC right away, because we're already at DISPATCH_LEVEL */
1627 ScsiPortDpcForIsr(NULL
,
1628 DeviceExtension
->DeviceObject
,
1633 /* Lower irql back to what it was */
1634 KeLowerIrql(OldIrql
);
1636 /* Start our timer */
1637 IoStartTimer(PortDeviceObject
);
1639 /* Initialize bus scanning information */
1640 DeviceExtension
->BusesConfig
= ExAllocatePoolWithTag(PagedPool
,
1641 sizeof(PVOID
) * DeviceExtension
->PortConfig
->NumberOfBuses
1642 + sizeof(ULONG
), TAG_SCSIPORT
);
1644 if (!DeviceExtension
->BusesConfig
)
1646 DPRINT1("Out of resources!\n");
1647 Status
= STATUS_INSUFFICIENT_RESOURCES
;
1652 RtlZeroMemory(DeviceExtension
->BusesConfig
,
1653 sizeof(PVOID
) * DeviceExtension
->PortConfig
->NumberOfBuses
1656 /* Store number of buses there */
1657 DeviceExtension
->BusesConfig
->NumberOfBuses
= (UCHAR
)DeviceExtension
->BusNum
;
1659 /* Scan the adapter for devices */
1660 SpiScanAdapter(DeviceExtension
);
1662 /* Build the registry device map */
1663 SpiBuildDeviceMap(DeviceExtension
,
1664 (PUNICODE_STRING
)Argument2
);
1666 /* Create the dos device link */
1667 swprintf(DosNameBuffer
,
1669 SystemConfig
->ScsiPortCount
);
1670 RtlInitUnicodeString(&DosDeviceName
, DosNameBuffer
);
1671 IoCreateSymbolicLink(&DosDeviceName
, &DeviceName
);
1673 /* Increase the port count */
1674 SystemConfig
->ScsiPortCount
++;
1675 FirstConfigCall
= FALSE
;
1677 /* Increase adapter number and bus number respectively */
1678 ConfigInfo
.AdapterNumber
++;
1681 ConfigInfo
.BusNumber
++;
1683 DPRINT("Bus: %lu MaxBus: %lu\n", BusNumber
, MaxBus
);
1688 /* Clean up the mess */
1689 SpiCleanupAfterInit(DeviceExtension
);
1691 /* Close registry keys */
1692 if (ConfigInfo
.ServiceKey
!= NULL
)
1693 ZwClose(ConfigInfo
.ServiceKey
);
1695 if (ConfigInfo
.DeviceKey
!= NULL
)
1696 ZwClose(ConfigInfo
.DeviceKey
);
1698 if (ConfigInfo
.BusKey
!= NULL
)
1699 ZwClose(ConfigInfo
.BusKey
);
1701 if (ConfigInfo
.AccessRanges
!= NULL
)
1702 ExFreePool(ConfigInfo
.AccessRanges
);
1704 if (ConfigInfo
.Parameter
!= NULL
)
1705 ExFreePool(ConfigInfo
.Parameter
);
1707 DPRINT("ScsiPortInitialize() done, Status = 0x%08X, DeviceFound = %d!\n",
1708 Status
, DeviceFound
);
1710 return (DeviceFound
== FALSE
) ? Status
: STATUS_SUCCESS
;
1714 SpiCleanupAfterInit(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
)
1716 PSCSI_LUN_INFO LunInfo
;
1720 /* Check if we have something to clean up */
1721 if (DeviceExtension
== NULL
)
1724 /* Stop the timer */
1725 IoStopTimer(DeviceExtension
->DeviceObject
);
1727 /* Disconnect the interrupts */
1728 while (DeviceExtension
->InterruptCount
)
1730 if (DeviceExtension
->Interrupt
[--DeviceExtension
->InterruptCount
])
1731 IoDisconnectInterrupt(DeviceExtension
->Interrupt
[DeviceExtension
->InterruptCount
]);
1734 /* Delete ConfigInfo */
1735 if (DeviceExtension
->BusesConfig
)
1737 for (Bus
= 0; Bus
< DeviceExtension
->BusNum
; Bus
++)
1739 if (!DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
])
1742 LunInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LunInfo
;
1746 /* Free current, but save pointer to the next one */
1747 Ptr
= LunInfo
->Next
;
1748 ExFreePool(LunInfo
);
1752 ExFreePool(DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]);
1755 ExFreePool(DeviceExtension
->BusesConfig
);
1758 /* Free PortConfig */
1759 if (DeviceExtension
->PortConfig
)
1760 ExFreePool(DeviceExtension
->PortConfig
);
1763 for(Lun
= 0; Lun
< LUS_NUMBER
; Lun
++)
1765 while (DeviceExtension
->LunExtensionList
[Lun
])
1767 Ptr
= DeviceExtension
->LunExtensionList
[Lun
];
1768 DeviceExtension
->LunExtensionList
[Lun
] = DeviceExtension
->LunExtensionList
[Lun
]->Next
;
1774 /* Free common buffer (if it exists) */
1775 if (DeviceExtension
->SrbExtensionBuffer
!= NULL
&&
1776 DeviceExtension
->CommonBufferLength
!= 0)
1778 if (!DeviceExtension
->AdapterObject
)
1780 ExFreePool(DeviceExtension
->SrbExtensionBuffer
);
1784 HalFreeCommonBuffer(DeviceExtension
->AdapterObject
,
1785 DeviceExtension
->CommonBufferLength
,
1786 DeviceExtension
->PhysicalAddress
,
1787 DeviceExtension
->SrbExtensionBuffer
,
1793 if (DeviceExtension
->SrbInfo
!= NULL
)
1794 ExFreePool(DeviceExtension
->SrbInfo
);
1796 /* Unmap mapped addresses */
1797 while (DeviceExtension
->MappedAddressList
!= NULL
)
1799 MmUnmapIoSpace(DeviceExtension
->MappedAddressList
->MappedAddress
,
1800 DeviceExtension
->MappedAddressList
->NumberOfBytes
);
1802 Ptr
= DeviceExtension
->MappedAddressList
;
1803 DeviceExtension
->MappedAddressList
= DeviceExtension
->MappedAddressList
->NextMappedAddress
;
1808 /* Finally delete the device object */
1809 DPRINT("Deleting device %p\n", DeviceExtension
->DeviceObject
);
1810 IoDeleteDevice(DeviceExtension
->DeviceObject
);
1817 ScsiPortIoMapTransfer(IN PVOID HwDeviceExtension
,
1818 IN PSCSI_REQUEST_BLOCK Srb
,
1819 IN PVOID LogicalAddress
,
1822 DPRINT1("ScsiPortIoMapTransfer()\n");
1830 ScsiPortLogError(IN PVOID HwDeviceExtension
,
1831 IN PSCSI_REQUEST_BLOCK Srb OPTIONAL
,
1838 //PSCSI_PORT_DEVICE_EXTENSION DeviceExtension;
1840 DPRINT1("ScsiPortLogError() called\n");
1842 //DeviceExtension = CONTAINING_RECORD(HwDeviceExtension, SCSI_PORT_DEVICE_EXTENSION, MiniPortDeviceExtension);
1845 DPRINT("ScsiPortLogError() done\n");
1852 ScsiPortMoveMemory(OUT PVOID Destination
,
1856 RtlMoveMemory(Destination
,
1866 ScsiPortNotification(IN SCSI_NOTIFICATION_TYPE NotificationType
,
1867 IN PVOID HwDeviceExtension
,
1870 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
1873 DPRINT("ScsiPortNotification() called\n");
1875 DeviceExtension
= CONTAINING_RECORD(HwDeviceExtension
,
1876 SCSI_PORT_DEVICE_EXTENSION
,
1877 MiniPortDeviceExtension
);
1879 DPRINT("DeviceExtension %p\n", DeviceExtension
);
1881 va_start(ap
, HwDeviceExtension
);
1883 switch (NotificationType
)
1885 case RequestComplete
:
1887 PSCSI_REQUEST_BLOCK Srb
;
1888 PSCSI_REQUEST_BLOCK_INFO SrbData
;
1890 Srb
= (PSCSI_REQUEST_BLOCK
) va_arg (ap
, PSCSI_REQUEST_BLOCK
);
1892 DPRINT("Notify: RequestComplete (Srb %p)\n", Srb
);
1894 /* Make sure Srb is allright */
1895 ASSERT(Srb
->SrbStatus
!= SRB_STATUS_PENDING
);
1896 ASSERT(Srb
->Function
!= SRB_FUNCTION_EXECUTE_SCSI
|| Srb
->SrbStatus
!= SRB_STATUS_SUCCESS
|| Srb
->ScsiStatus
== SCSISTAT_GOOD
);
1898 if (!(Srb
->SrbFlags
& SRB_FLAGS_IS_ACTIVE
))
1900 /* It's been already completed */
1905 /* It's not active anymore */
1906 Srb
->SrbFlags
&= ~SRB_FLAGS_IS_ACTIVE
;
1908 if (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
)
1910 /* TODO: Treat it specially */
1915 /* Get the SRB data */
1916 SrbData
= SpiGetSrbData(DeviceExtension
,
1922 /* Make sure there are no CompletedRequests and there is a Srb */
1923 ASSERT(SrbData
->CompletedRequests
== NULL
&& SrbData
->Srb
!= NULL
);
1925 /* If it's a read/write request, make sure it has data inside it */
1926 if ((Srb
->SrbStatus
== SRB_STATUS_SUCCESS
) &&
1927 ((Srb
->Cdb
[0] == SCSIOP_READ
) || (Srb
->Cdb
[0] == SCSIOP_WRITE
)))
1929 ASSERT(Srb
->DataTransferLength
);
1932 SrbData
->CompletedRequests
= DeviceExtension
->InterruptData
.CompletedRequests
;
1933 DeviceExtension
->InterruptData
.CompletedRequests
= SrbData
;
1939 DPRINT("Notify: NextRequest\n");
1940 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_NEXT_REQUEST_READY
;
1948 PSCSI_PORT_LUN_EXTENSION LunExtension
;
1950 PathId
= (UCHAR
) va_arg (ap
, int);
1951 TargetId
= (UCHAR
) va_arg (ap
, int);
1952 Lun
= (UCHAR
) va_arg (ap
, int);
1954 DPRINT("Notify: NextLuRequest(PathId %u TargetId %u Lun %u)\n",
1955 PathId
, TargetId
, Lun
);
1957 /* Mark it in the flags field */
1958 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_NEXT_REQUEST_READY
;
1960 /* Get the LUN extension */
1961 LunExtension
= SpiGetLunExtension(DeviceExtension
,
1966 /* If returned LunExtension is NULL, break out */
1967 if (!LunExtension
) break;
1969 /* This request should not be processed if */
1970 if ((LunExtension
->ReadyLun
) ||
1971 (LunExtension
->SrbInfo
.Srb
))
1973 /* Nothing to do here */
1977 /* Add this LUN to the list */
1978 LunExtension
->ReadyLun
= DeviceExtension
->InterruptData
.ReadyLun
;
1979 DeviceExtension
->InterruptData
.ReadyLun
= LunExtension
;
1984 DPRINT("Notify: ResetDetected\n");
1985 /* Add RESET flags */
1986 DeviceExtension
->InterruptData
.Flags
|=
1987 SCSI_PORT_RESET
| SCSI_PORT_RESET_REPORTED
;
1990 case CallDisableInterrupts
:
1991 DPRINT1("UNIMPLEMENTED SCSI Notification called: CallDisableInterrupts!\n");
1994 case CallEnableInterrupts
:
1995 DPRINT1("UNIMPLEMENTED SCSI Notification called: CallEnableInterrupts!\n");
1998 case RequestTimerCall
:
1999 DPRINT("Notify: RequestTimerCall\n");
2000 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_TIMER_NEEDED
;
2001 DeviceExtension
->InterruptData
.HwScsiTimer
= (PHW_TIMER
)va_arg(ap
, PHW_TIMER
);
2002 DeviceExtension
->InterruptData
.MiniportTimerValue
= (ULONG
)va_arg(ap
, ULONG
);
2005 case BusChangeDetected
:
2006 DPRINT1("UNIMPLEMENTED SCSI Notification called: BusChangeDetected!\n");
2010 DPRINT1 ("Unsupported notification from WMI: %lu\n", NotificationType
);
2016 /* Request a DPC after we're done with the interrupt */
2017 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_NOTIFICATION_NEEDED
;
2024 ScsiPortValidateRange(IN PVOID HwDeviceExtension
,
2025 IN INTERFACE_TYPE BusType
,
2026 IN ULONG SystemIoBusNumber
,
2027 IN SCSI_PHYSICAL_ADDRESS IoAddress
,
2028 IN ULONG NumberOfBytes
,
2029 IN BOOLEAN InIoSpace
)
2031 DPRINT("ScsiPortValidateRange()\n");
2036 /* INTERNAL FUNCTIONS ********************************************************/
2039 SpiResourceToConfig(IN PHW_INITIALIZATION_DATA HwInitializationData
,
2040 IN PCM_FULL_RESOURCE_DESCRIPTOR ResourceDescriptor
,
2041 IN PPORT_CONFIGURATION_INFORMATION PortConfig
)
2043 PACCESS_RANGE AccessRange
;
2044 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialData
;
2047 ULONG Interrupt
= 0;
2052 /* Loop through all entries */
2053 for (Index
= 0; Index
< ResourceDescriptor
->PartialResourceList
.Count
; Index
++)
2055 PartialData
= &ResourceDescriptor
->PartialResourceList
.PartialDescriptors
[Index
];
2057 switch (PartialData
->Type
)
2059 case CmResourceTypePort
:
2060 /* Copy access ranges */
2061 if (RangeNumber
< HwInitializationData
->NumberOfAccessRanges
)
2063 AccessRange
= &((*(PortConfig
->AccessRanges
))[RangeNumber
]);
2065 AccessRange
->RangeStart
= PartialData
->u
.Port
.Start
;
2066 AccessRange
->RangeLength
= PartialData
->u
.Port
.Length
;
2068 AccessRange
->RangeInMemory
= FALSE
;
2073 case CmResourceTypeMemory
:
2074 /* Copy access ranges */
2075 if (RangeNumber
< HwInitializationData
->NumberOfAccessRanges
)
2077 AccessRange
= &((*(PortConfig
->AccessRanges
))[RangeNumber
]);
2079 AccessRange
->RangeStart
= PartialData
->u
.Memory
.Start
;
2080 AccessRange
->RangeLength
= PartialData
->u
.Memory
.Length
;
2082 AccessRange
->RangeInMemory
= TRUE
;
2087 case CmResourceTypeInterrupt
:
2091 /* Copy interrupt data */
2092 PortConfig
->BusInterruptLevel
= PartialData
->u
.Interrupt
.Level
;
2093 PortConfig
->BusInterruptVector
= PartialData
->u
.Interrupt
.Vector
;
2095 /* Set interrupt mode accordingly to the resource */
2096 if (PartialData
->Flags
== CM_RESOURCE_INTERRUPT_LATCHED
)
2098 PortConfig
->InterruptMode
= Latched
;
2100 else if (PartialData
->Flags
== CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE
)
2102 PortConfig
->InterruptMode
= LevelSensitive
;
2105 else if (Interrupt
== 1)
2107 /* Copy interrupt data */
2108 PortConfig
->BusInterruptLevel2
= PartialData
->u
.Interrupt
.Level
;
2109 PortConfig
->BusInterruptVector2
= PartialData
->u
.Interrupt
.Vector
;
2111 /* Set interrupt mode accordingly to the resource */
2112 if (PartialData
->Flags
== CM_RESOURCE_INTERRUPT_LATCHED
)
2114 PortConfig
->InterruptMode2
= Latched
;
2116 else if (PartialData
->Flags
== CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE
)
2118 PortConfig
->InterruptMode2
= LevelSensitive
;
2125 case CmResourceTypeDma
:
2129 PortConfig
->DmaChannel
= PartialData
->u
.Dma
.Channel
;
2130 PortConfig
->DmaPort
= PartialData
->u
.Dma
.Port
;
2132 if (PartialData
->Flags
& CM_RESOURCE_DMA_8
)
2133 PortConfig
->DmaWidth
= Width8Bits
;
2134 else if ((PartialData
->Flags
& CM_RESOURCE_DMA_16
) ||
2135 (PartialData
->Flags
& CM_RESOURCE_DMA_8_AND_16
)) //???
2136 PortConfig
->DmaWidth
= Width16Bits
;
2137 else if (PartialData
->Flags
& CM_RESOURCE_DMA_32
)
2138 PortConfig
->DmaWidth
= Width32Bits
;
2142 PortConfig
->DmaChannel2
= PartialData
->u
.Dma
.Channel
;
2143 PortConfig
->DmaPort2
= PartialData
->u
.Dma
.Port
;
2145 if (PartialData
->Flags
& CM_RESOURCE_DMA_8
)
2146 PortConfig
->DmaWidth2
= Width8Bits
;
2147 else if ((PartialData
->Flags
& CM_RESOURCE_DMA_16
) ||
2148 (PartialData
->Flags
& CM_RESOURCE_DMA_8_AND_16
)) //???
2149 PortConfig
->DmaWidth2
= Width16Bits
;
2150 else if (PartialData
->Flags
& CM_RESOURCE_DMA_32
)
2151 PortConfig
->DmaWidth2
= Width32Bits
;
2158 static PCM_RESOURCE_LIST
2159 SpiConfigToResource(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
2160 PPORT_CONFIGURATION_INFORMATION PortConfig
)
2162 PCONFIGURATION_INFORMATION ConfigInfo
;
2163 PCM_RESOURCE_LIST ResourceList
;
2164 PCM_PARTIAL_RESOURCE_DESCRIPTOR ResourceDescriptor
;
2165 PACCESS_RANGE AccessRange
;
2166 ULONG ListLength
= 0, i
, FullSize
;
2167 ULONG Interrupt
, Dma
;
2169 /* Get current Atdisk usage from the system */
2170 ConfigInfo
= IoGetConfigurationInformation();
2172 if (PortConfig
->AtdiskPrimaryClaimed
)
2173 ConfigInfo
->AtDiskPrimaryAddressClaimed
= TRUE
;
2175 if (PortConfig
->AtdiskSecondaryClaimed
)
2176 ConfigInfo
->AtDiskSecondaryAddressClaimed
= TRUE
;
2178 /* Do we use DMA? */
2179 if (PortConfig
->DmaChannel
!= SP_UNINITIALIZED_VALUE
||
2180 PortConfig
->DmaPort
!= SP_UNINITIALIZED_VALUE
)
2184 if (PortConfig
->DmaChannel2
!= SP_UNINITIALIZED_VALUE
||
2185 PortConfig
->DmaPort2
!= SP_UNINITIALIZED_VALUE
)
2194 /* How many interrupts to we have? */
2195 Interrupt
= DeviceExtension
->InterruptCount
;
2196 ListLength
+= Interrupt
;
2198 /* How many access ranges do we use? */
2199 AccessRange
= &((*(PortConfig
->AccessRanges
))[0]);
2200 for (i
= 0; i
< PortConfig
->NumberOfAccessRanges
; i
++)
2202 if (AccessRange
->RangeLength
!= 0)
2208 /* Allocate the resource list, since we know its size now */
2209 FullSize
= sizeof(CM_RESOURCE_LIST
) + (ListLength
- 1) *
2210 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR
);
2212 ResourceList
= (PCM_RESOURCE_LIST
)ExAllocatePoolWithTag(PagedPool
, FullSize
, TAG_SCSIPORT
);
2218 RtlZeroMemory(ResourceList
, FullSize
);
2221 ResourceList
->Count
= 1;
2222 ResourceList
->List
[0].InterfaceType
= PortConfig
->AdapterInterfaceType
;
2223 ResourceList
->List
[0].BusNumber
= PortConfig
->SystemIoBusNumber
;
2224 ResourceList
->List
[0].PartialResourceList
.Count
= ListLength
;
2225 ResourceDescriptor
= ResourceList
->List
[0].PartialResourceList
.PartialDescriptors
;
2227 /* Copy access ranges array over */
2228 for (i
= 0; i
< PortConfig
->NumberOfAccessRanges
; i
++)
2230 AccessRange
= &((*(PortConfig
->AccessRanges
))[i
]);
2232 /* If the range is empty - skip it */
2233 if (AccessRange
->RangeLength
== 0)
2236 if (AccessRange
->RangeInMemory
)
2238 ResourceDescriptor
->Type
= CmResourceTypeMemory
;
2239 ResourceDescriptor
->Flags
= CM_RESOURCE_MEMORY_READ_WRITE
;
2243 ResourceDescriptor
->Type
= CmResourceTypePort
;
2244 ResourceDescriptor
->Flags
= CM_RESOURCE_PORT_IO
;
2247 ResourceDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
2249 ResourceDescriptor
->u
.Memory
.Start
= AccessRange
->RangeStart
;
2250 ResourceDescriptor
->u
.Memory
.Length
= AccessRange
->RangeLength
;
2252 ResourceDescriptor
++;
2255 /* If we use interrupt(s), copy them */
2258 ResourceDescriptor
->Type
= CmResourceTypeInterrupt
;
2260 if (PortConfig
->AdapterInterfaceType
== MicroChannel
||
2261 ((Interrupt
== 2) ? PortConfig
->InterruptMode2
: PortConfig
->InterruptMode
) == LevelSensitive
)
2263 ResourceDescriptor
->ShareDisposition
= CmResourceShareShared
;
2264 ResourceDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LEVEL_SENSITIVE
;
2268 ResourceDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
2269 ResourceDescriptor
->Flags
= CM_RESOURCE_INTERRUPT_LATCHED
;
2272 ResourceDescriptor
->u
.Interrupt
.Level
= (Interrupt
== 2) ? PortConfig
->BusInterruptLevel2
: PortConfig
->BusInterruptLevel
;
2273 ResourceDescriptor
->u
.Interrupt
.Vector
= (Interrupt
== 2) ? PortConfig
->BusInterruptVector2
: PortConfig
->BusInterruptVector
;
2274 ResourceDescriptor
->u
.Interrupt
.Affinity
= 0;
2276 ResourceDescriptor
++;
2283 ResourceDescriptor
->Type
= CmResourceTypeDma
;
2284 ResourceDescriptor
->ShareDisposition
= CmResourceShareDeviceExclusive
;
2285 ResourceDescriptor
->u
.Dma
.Channel
= (Dma
== 2) ? PortConfig
->DmaChannel2
: PortConfig
->DmaChannel
;
2286 ResourceDescriptor
->u
.Dma
.Port
= (Dma
== 2) ? PortConfig
->DmaPort2
: PortConfig
->DmaPort
;
2287 ResourceDescriptor
->Flags
= 0;
2289 if (((Dma
== 2) ? PortConfig
->DmaWidth2
: PortConfig
->DmaWidth
) == Width8Bits
)
2290 ResourceDescriptor
->Flags
|= CM_RESOURCE_DMA_8
;
2291 else if (((Dma
== 2) ? PortConfig
->DmaWidth2
: PortConfig
->DmaWidth
) == Width16Bits
)
2292 ResourceDescriptor
->Flags
|= CM_RESOURCE_DMA_16
;
2294 ResourceDescriptor
->Flags
|= CM_RESOURCE_DMA_32
;
2296 if (((Dma
== 2) ? PortConfig
->DmaChannel2
: PortConfig
->DmaChannel
) == SP_UNINITIALIZED_VALUE
)
2297 ResourceDescriptor
->u
.Dma
.Channel
= 0;
2299 if (((Dma
== 2) ? PortConfig
->DmaPort2
: PortConfig
->DmaPort
) == SP_UNINITIALIZED_VALUE
)
2300 ResourceDescriptor
->u
.Dma
.Port
= 0;
2302 ResourceDescriptor
++;
2306 return ResourceList
;
2311 SpiGetPciConfigData(IN PDRIVER_OBJECT DriverObject
,
2312 IN PDEVICE_OBJECT DeviceObject
,
2313 IN
struct _HW_INITIALIZATION_DATA
*HwInitializationData
,
2314 IN OUT PPORT_CONFIGURATION_INFORMATION PortConfig
,
2315 IN PUNICODE_STRING RegistryPath
,
2317 IN OUT PPCI_SLOT_NUMBER NextSlotNumber
)
2319 PCI_COMMON_CONFIG PciConfig
;
2320 PCI_SLOT_NUMBER SlotNumber
;
2323 ULONG FunctionNumber
;
2324 CHAR VendorIdString
[8];
2325 CHAR DeviceIdString
[8];
2326 UNICODE_STRING UnicodeStr
;
2327 PCM_RESOURCE_LIST ResourceList
= NULL
;
2330 DPRINT ("SpiGetPciConfiguration() called\n");
2332 SlotNumber
.u
.AsULONG
= 0;
2334 /* Loop through all devices */
2335 for (DeviceNumber
= NextSlotNumber
->u
.bits
.DeviceNumber
; DeviceNumber
< PCI_MAX_DEVICES
; DeviceNumber
++)
2337 SlotNumber
.u
.bits
.DeviceNumber
= DeviceNumber
;
2339 /* Loop through all functions */
2340 for (FunctionNumber
= NextSlotNumber
->u
.bits
.FunctionNumber
; FunctionNumber
< PCI_MAX_FUNCTION
; FunctionNumber
++)
2342 SlotNumber
.u
.bits
.FunctionNumber
= FunctionNumber
;
2344 /* Get PCI config bytes */
2345 DataSize
= HalGetBusData(PCIConfiguration
,
2347 SlotNumber
.u
.AsULONG
,
2351 /* If result of HalGetBusData is 0, then the bus is wrong */
2355 /* If result is PCI_INVALID_VENDORID, then this device has no more
2357 if (PciConfig
.VendorID
== PCI_INVALID_VENDORID
)
2360 sprintf (VendorIdString
, "%04hx", PciConfig
.VendorID
);
2361 sprintf (DeviceIdString
, "%04hx", PciConfig
.DeviceID
);
2363 if (_strnicmp(VendorIdString
, HwInitializationData
->VendorId
, HwInitializationData
->VendorIdLength
) ||
2364 _strnicmp(DeviceIdString
, HwInitializationData
->DeviceId
, HwInitializationData
->DeviceIdLength
))
2366 /* It is not our device */
2370 DPRINT("Found device 0x%04hx 0x%04hx at %1lu %2lu %1lu\n",
2374 SlotNumber
.u
.bits
.DeviceNumber
,
2375 SlotNumber
.u
.bits
.FunctionNumber
);
2378 RtlInitUnicodeString(&UnicodeStr
, L
"ScsiAdapter");
2379 Status
= HalAssignSlotResources(RegistryPath
,
2385 SlotNumber
.u
.AsULONG
,
2388 if (!NT_SUCCESS(Status
))
2391 /* Create configuration information */
2392 SpiResourceToConfig(HwInitializationData
,
2396 /* Free the resource list */
2397 ExFreePool(ResourceList
);
2399 /* Set dev & fn numbers */
2400 NextSlotNumber
->u
.bits
.DeviceNumber
= DeviceNumber
;
2401 NextSlotNumber
->u
.bits
.FunctionNumber
= FunctionNumber
+ 1;
2403 /* Save the slot number */
2404 PortConfig
->SlotNumber
= SlotNumber
.u
.AsULONG
;
2408 NextSlotNumber
->u
.bits
.FunctionNumber
= 0;
2411 NextSlotNumber
->u
.bits
.DeviceNumber
= 0;
2412 DPRINT ("No device found\n");
2419 /**********************************************************************
2421 * ScsiPortCreateClose
2424 * Answer requests for Create/Close calls: a null operation.
2431 * Pointer to a device object.
2434 * Pointer to an IRP.
2440 static NTSTATUS NTAPI
2441 ScsiPortCreateClose(IN PDEVICE_OBJECT DeviceObject
,
2444 DPRINT("ScsiPortCreateClose()\n");
2446 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
2447 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2449 return STATUS_SUCCESS
;
2453 SpiHandleAttachRelease(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
2456 PSCSI_LUN_INFO LunInfo
;
2457 PIO_STACK_LOCATION IrpStack
;
2458 PDEVICE_OBJECT DeviceObject
;
2459 PSCSI_REQUEST_BLOCK Srb
;
2462 /* Get pointer to the SRB */
2463 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
2464 Srb
= (PSCSI_REQUEST_BLOCK
)IrpStack
->Parameters
.Others
.Argument1
;
2466 /* Check if PathId matches number of buses */
2467 if (DeviceExtension
->BusesConfig
== NULL
||
2468 DeviceExtension
->BusesConfig
->NumberOfBuses
<= Srb
->PathId
)
2470 Srb
->SrbStatus
= SRB_STATUS_NO_DEVICE
;
2471 return STATUS_DEVICE_DOES_NOT_EXIST
;
2474 /* Get pointer to LunInfo */
2475 LunInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Srb
->PathId
]->LunInfo
;
2477 /* Find matching LunInfo */
2480 if (LunInfo
->PathId
== Srb
->PathId
&&
2481 LunInfo
->TargetId
== Srb
->TargetId
&&
2482 LunInfo
->Lun
== Srb
->Lun
)
2487 LunInfo
= LunInfo
->Next
;
2490 /* If we couldn't find it - exit */
2491 if (LunInfo
== NULL
)
2492 return STATUS_DEVICE_DOES_NOT_EXIST
;
2496 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
2498 /* Release, if asked */
2499 if (Srb
->Function
== SRB_FUNCTION_RELEASE_DEVICE
)
2501 LunInfo
->DeviceClaimed
= FALSE
;
2502 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2503 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2505 return STATUS_SUCCESS
;
2508 /* Attach, if not already claimed */
2509 if (LunInfo
->DeviceClaimed
)
2511 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2512 Srb
->SrbStatus
= SRB_STATUS_BUSY
;
2514 return STATUS_DEVICE_BUSY
;
2517 /* Save the device object */
2518 DeviceObject
= LunInfo
->DeviceObject
;
2520 if (Srb
->Function
== SRB_FUNCTION_CLAIM_DEVICE
)
2521 LunInfo
->DeviceClaimed
= TRUE
;
2523 if (Srb
->Function
== SRB_FUNCTION_ATTACH_DEVICE
)
2524 LunInfo
->DeviceObject
= Srb
->DataBuffer
;
2526 Srb
->DataBuffer
= DeviceObject
;
2528 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2529 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2531 return STATUS_SUCCESS
;
2535 /**********************************************************************
2537 * ScsiPortDispatchScsi
2540 * Answer requests for SCSI calls
2546 * Standard dispatch arguments
2552 static NTSTATUS NTAPI
2553 ScsiPortDispatchScsi(IN PDEVICE_OBJECT DeviceObject
,
2556 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
2557 PSCSI_PORT_LUN_EXTENSION LunExtension
;
2558 PIO_STACK_LOCATION Stack
;
2559 PSCSI_REQUEST_BLOCK Srb
;
2561 NTSTATUS Status
= STATUS_SUCCESS
;
2562 PIRP NextIrp
, IrpList
;
2563 PKDEVICE_QUEUE_ENTRY Entry
;
2565 DPRINT("ScsiPortDispatchScsi(DeviceObject %p Irp %p)\n",
2568 DeviceExtension
= DeviceObject
->DeviceExtension
;
2569 Stack
= IoGetCurrentIrpStackLocation(Irp
);
2571 Srb
= Stack
->Parameters
.Scsi
.Srb
;
2574 DPRINT1("ScsiPortDispatchScsi() called with Srb = NULL!\n");
2575 Status
= STATUS_UNSUCCESSFUL
;
2577 Irp
->IoStatus
.Status
= Status
;
2578 Irp
->IoStatus
.Information
= 0;
2580 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2585 DPRINT("Srb: %p\n", Srb
);
2586 DPRINT("Srb->Function: %lu\n", Srb
->Function
);
2587 DPRINT("PathId: %lu TargetId: %lu Lun: %lu\n", Srb
->PathId
, Srb
->TargetId
, Srb
->Lun
);
2589 LunExtension
= SpiGetLunExtension(DeviceExtension
,
2593 if (LunExtension
== NULL
)
2595 DPRINT("ScsiPortDispatchScsi() called with an invalid LUN\n");
2596 Status
= STATUS_NO_SUCH_DEVICE
;
2598 Srb
->SrbStatus
= SRB_STATUS_NO_DEVICE
;
2599 Irp
->IoStatus
.Status
= Status
;
2600 Irp
->IoStatus
.Information
= 0;
2602 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2607 switch (Srb
->Function
)
2609 case SRB_FUNCTION_SHUTDOWN
:
2610 case SRB_FUNCTION_FLUSH
:
2611 DPRINT (" SRB_FUNCTION_SHUTDOWN or FLUSH\n");
2612 if (DeviceExtension
->CachesData
== FALSE
)
2614 /* All success here */
2615 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2616 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
2617 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2618 return STATUS_SUCCESS
;
2620 /* Fall through to a usual execute operation */
2622 case SRB_FUNCTION_EXECUTE_SCSI
:
2623 case SRB_FUNCTION_IO_CONTROL
:
2624 /* Mark IRP as pending in all cases */
2625 IoMarkIrpPending(Irp
);
2627 if (Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
)
2629 /* Start IO directly */
2630 IoStartPacket(DeviceObject
, Irp
, NULL
, NULL
);
2636 /* We need to be at DISPATCH_LEVEL */
2637 KeRaiseIrql (DISPATCH_LEVEL
, &oldIrql
);
2639 /* Insert IRP into the queue */
2640 if (!KeInsertByKeyDeviceQueue(&LunExtension
->DeviceQueue
,
2641 &Irp
->Tail
.Overlay
.DeviceQueueEntry
,
2644 /* It means the queue is empty, and we just start this request */
2645 IoStartPacket(DeviceObject
, Irp
, NULL
, NULL
);
2648 /* Back to the old IRQL */
2649 KeLowerIrql (oldIrql
);
2651 return STATUS_PENDING
;
2653 case SRB_FUNCTION_CLAIM_DEVICE
:
2654 case SRB_FUNCTION_ATTACH_DEVICE
:
2655 DPRINT (" SRB_FUNCTION_CLAIM_DEVICE or ATTACH\n");
2657 /* Reference device object and keep the device object */
2658 Status
= SpiHandleAttachRelease(DeviceExtension
, Irp
);
2661 case SRB_FUNCTION_RELEASE_DEVICE
:
2662 DPRINT (" SRB_FUNCTION_RELEASE_DEVICE\n");
2664 /* Dereference device object and clear the device object */
2665 Status
= SpiHandleAttachRelease(DeviceExtension
, Irp
);
2668 case SRB_FUNCTION_RELEASE_QUEUE
:
2669 DPRINT(" SRB_FUNCTION_RELEASE_QUEUE\n");
2671 /* Guard with the spinlock */
2672 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
2674 if (!(LunExtension
->Flags
& LUNEX_FROZEN_QUEUE
))
2676 DPRINT("Queue is not frozen really\n");
2678 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2679 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2680 Status
= STATUS_SUCCESS
;
2685 /* Unfreeze the queue */
2686 LunExtension
->Flags
&= ~LUNEX_FROZEN_QUEUE
;
2688 if (LunExtension
->SrbInfo
.Srb
== NULL
)
2690 /* Get next logical unit request */
2691 SpiGetNextRequestFromLun(DeviceExtension
, LunExtension
);
2693 /* SpiGetNextRequestFromLun() releases the spinlock */
2698 DPRINT("The queue has active request\n");
2699 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2703 Srb
->SrbStatus
= SRB_STATUS_SUCCESS
;
2704 Status
= STATUS_SUCCESS
;
2707 case SRB_FUNCTION_FLUSH_QUEUE
:
2708 DPRINT(" SRB_FUNCTION_FLUSH_QUEUE\n");
2710 /* Guard with the spinlock */
2711 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
2713 if (!(LunExtension
->Flags
& LUNEX_FROZEN_QUEUE
))
2715 DPRINT("Queue is not frozen really\n");
2717 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2718 Status
= STATUS_INVALID_DEVICE_REQUEST
;
2722 /* Make sure there is no active request */
2723 ASSERT(LunExtension
->SrbInfo
.Srb
== NULL
);
2725 /* Compile a list from the device queue */
2727 while ((Entry
= KeRemoveDeviceQueue(&LunExtension
->DeviceQueue
)) != NULL
)
2729 NextIrp
= CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.DeviceQueueEntry
);
2732 Stack
= IoGetCurrentIrpStackLocation(NextIrp
);
2733 Srb
= Stack
->Parameters
.Scsi
.Srb
;
2736 Srb
->SrbStatus
= SRB_STATUS_REQUEST_FLUSHED
;
2737 NextIrp
->IoStatus
.Status
= STATUS_UNSUCCESSFUL
;
2739 /* Add then to the list */
2740 NextIrp
->Tail
.Overlay
.ListEntry
.Flink
= (PLIST_ENTRY
)IrpList
;
2744 /* Unfreeze the queue */
2745 LunExtension
->Flags
&= ~LUNEX_FROZEN_QUEUE
;
2747 /* Release the spinlock */
2748 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
2750 /* Complete those requests */
2754 IrpList
= (PIRP
)NextIrp
->Tail
.Overlay
.ListEntry
.Flink
;
2756 IoCompleteRequest(NextIrp
, 0);
2759 Status
= STATUS_SUCCESS
;
2763 DPRINT1("SRB function not implemented (Function %lu)\n", Srb
->Function
);
2764 Status
= STATUS_NOT_IMPLEMENTED
;
2768 Irp
->IoStatus
.Status
= Status
;
2770 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2776 /**********************************************************************
2778 * ScsiPortDeviceControl
2781 * Answer requests for device control calls
2787 * Standard dispatch arguments
2793 static NTSTATUS NTAPI
2794 ScsiPortDeviceControl(IN PDEVICE_OBJECT DeviceObject
,
2797 PIO_STACK_LOCATION Stack
;
2798 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
2799 PDUMP_POINTERS DumpPointers
;
2802 DPRINT("ScsiPortDeviceControl()\n");
2804 Irp
->IoStatus
.Information
= 0;
2806 Stack
= IoGetCurrentIrpStackLocation(Irp
);
2807 DeviceExtension
= DeviceObject
->DeviceExtension
;
2809 switch (Stack
->Parameters
.DeviceIoControl
.IoControlCode
)
2811 case IOCTL_SCSI_GET_DUMP_POINTERS
:
2812 DPRINT(" IOCTL_SCSI_GET_DUMP_POINTERS\n");
2814 if (Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(DUMP_POINTERS
))
2816 Status
= STATUS_BUFFER_OVERFLOW
;
2817 Irp
->IoStatus
.Information
= sizeof(DUMP_POINTERS
);
2821 DumpPointers
= Irp
->AssociatedIrp
.SystemBuffer
;
2822 DumpPointers
->DeviceObject
= DeviceObject
;
2825 Status
= STATUS_SUCCESS
;
2826 Irp
->IoStatus
.Information
= sizeof(DUMP_POINTERS
);
2829 case IOCTL_SCSI_GET_CAPABILITIES
:
2830 DPRINT(" IOCTL_SCSI_GET_CAPABILITIES\n");
2831 if (Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
== sizeof(PVOID
))
2833 *((PVOID
*)Irp
->AssociatedIrp
.SystemBuffer
) = &DeviceExtension
->PortCapabilities
;
2835 Irp
->IoStatus
.Information
= sizeof(PVOID
);
2836 Status
= STATUS_SUCCESS
;
2840 if (Stack
->Parameters
.DeviceIoControl
.OutputBufferLength
< sizeof(IO_SCSI_CAPABILITIES
))
2842 Status
= STATUS_BUFFER_TOO_SMALL
;
2846 RtlCopyMemory(Irp
->AssociatedIrp
.SystemBuffer
,
2847 &DeviceExtension
->PortCapabilities
,
2848 sizeof(IO_SCSI_CAPABILITIES
));
2850 Irp
->IoStatus
.Information
= sizeof(IO_SCSI_CAPABILITIES
);
2851 Status
= STATUS_SUCCESS
;
2854 case IOCTL_SCSI_GET_INQUIRY_DATA
:
2855 DPRINT(" IOCTL_SCSI_GET_INQUIRY_DATA\n");
2857 /* Copy inquiry data to the port device extension */
2858 Status
= SpiGetInquiryData(DeviceExtension
, Irp
);
2861 case IOCTL_SCSI_MINIPORT
:
2862 DPRINT1("IOCTL_SCSI_MINIPORT unimplemented!\n");
2863 Status
= STATUS_NOT_IMPLEMENTED
;
2866 case IOCTL_SCSI_PASS_THROUGH
:
2867 DPRINT1("IOCTL_SCSI_PASS_THROUGH unimplemented!\n");
2868 Status
= STATUS_NOT_IMPLEMENTED
;
2872 DPRINT1(" unknown ioctl code: 0x%lX\n", Stack
->Parameters
.DeviceIoControl
.IoControlCode
);
2873 Status
= STATUS_NOT_IMPLEMENTED
;
2877 /* Complete the request with the given status */
2878 Irp
->IoStatus
.Status
= Status
;
2879 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
2886 ScsiPortStartIo(IN PDEVICE_OBJECT DeviceObject
,
2889 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
2890 PSCSI_PORT_LUN_EXTENSION LunExtension
;
2891 PIO_STACK_LOCATION IrpStack
;
2892 PSCSI_REQUEST_BLOCK Srb
;
2893 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
2897 DPRINT("ScsiPortStartIo() called!\n");
2899 DeviceExtension
= DeviceObject
->DeviceExtension
;
2900 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
2902 DPRINT("DeviceExtension %p\n", DeviceExtension
);
2904 Srb
= IrpStack
->Parameters
.Scsi
.Srb
;
2906 /* Apply "default" flags */
2907 Srb
->SrbFlags
|= DeviceExtension
->SrbFlags
;
2909 /* Get LUN extension */
2910 LunExtension
= SpiGetLunExtension(DeviceExtension
,
2915 if (DeviceExtension
->NeedSrbDataAlloc
||
2916 DeviceExtension
->NeedSrbExtensionAlloc
)
2919 SrbInfo
= SpiAllocateSrbStructures(DeviceExtension
,
2923 /* Couldn't alloc one or both data structures, return */
2924 if (SrbInfo
== NULL
)
2926 /* We have to call IoStartNextPacket, because this request
2928 if (LunExtension
->Flags
& LUNEX_REQUEST_PENDING
)
2929 IoStartNextPacket(DeviceObject
, FALSE
);
2936 /* No allocations are needed */
2937 SrbInfo
= &LunExtension
->SrbInfo
;
2938 Srb
->SrbExtension
= NULL
;
2939 Srb
->QueueTag
= SP_UNTAGGED
;
2942 /* Increase sequence number of SRB */
2943 if (!SrbInfo
->SequenceNumber
)
2945 /* Increase global sequence number */
2946 DeviceExtension
->SequenceNumber
++;
2949 SrbInfo
->SequenceNumber
= DeviceExtension
->SequenceNumber
;
2952 /* Check some special SRBs */
2953 if (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
)
2955 /* Some special handling */
2956 DPRINT1("Abort command! Unimplemented now\n");
2963 if (Srb
->SrbFlags
& SRB_FLAGS_UNSPECIFIED_DIRECTION
)
2965 // Store the MDL virtual address in SrbInfo structure
2966 SrbInfo
->DataOffset
= MmGetMdlVirtualAddress(Irp
->MdlAddress
);
2968 if (DeviceExtension
->MapBuffers
)
2970 /* Calculate offset within DataBuffer */
2971 SrbInfo
->DataOffset
= MmGetSystemAddressForMdl(Irp
->MdlAddress
);
2972 Srb
->DataBuffer
= SrbInfo
->DataOffset
+
2973 (ULONG
)((PUCHAR
)Srb
->DataBuffer
-
2974 (PUCHAR
)MmGetMdlVirtualAddress(Irp
->MdlAddress
));
2977 if (DeviceExtension
->AdapterObject
)
2980 KeFlushIoBuffers(Irp
->MdlAddress
,
2981 Srb
->SrbFlags
& SRB_FLAGS_DATA_IN
? TRUE
: FALSE
,
2985 if (DeviceExtension
->MapRegisters
)
2987 /* Calculate number of needed map registers */
2988 SrbInfo
->NumberOfMapRegisters
= ADDRESS_AND_SIZE_TO_SPAN_PAGES(
2990 Srb
->DataTransferLength
);
2992 /* Allocate adapter channel */
2993 Status
= IoAllocateAdapterChannel(DeviceExtension
->AdapterObject
,
2994 DeviceExtension
->DeviceObject
,
2995 SrbInfo
->NumberOfMapRegisters
,
2999 if (!NT_SUCCESS(Status
))
3001 DPRINT1("IoAllocateAdapterChannel() failed!\n");
3003 Srb
->SrbStatus
= SRB_STATUS_INVALID_REQUEST
;
3004 ScsiPortNotification(RequestComplete
,
3005 DeviceExtension
+ 1,
3008 ScsiPortNotification(NextRequest
,
3009 DeviceExtension
+ 1);
3011 /* Request DPC for that work */
3012 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
3015 /* Control goes to SpiAdapterControl */
3020 /* Increase active request counter */
3021 CounterResult
= InterlockedIncrement(&DeviceExtension
->ActiveRequestCounter
);
3023 if (CounterResult
== 0 &&
3024 DeviceExtension
->AdapterObject
!= NULL
&&
3025 !DeviceExtension
->MapRegisters
)
3027 IoAllocateAdapterChannel(
3028 DeviceExtension
->AdapterObject
,
3030 DeviceExtension
->PortCapabilities
.MaximumPhysicalPages
,
3031 ScsiPortAllocateAdapterChannel
,
3038 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
3040 if (!KeSynchronizeExecution(DeviceExtension
->Interrupt
[0],
3041 ScsiPortStartPacket
,
3044 DPRINT("Synchronization failed!\n");
3046 Irp
->IoStatus
.Status
= STATUS_UNSUCCESSFUL
;
3047 Irp
->IoStatus
.Information
= 0;
3048 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3050 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
3054 /* Release the spinlock only */
3055 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3059 DPRINT("ScsiPortStartIo() done\n");
3063 static BOOLEAN NTAPI
3064 ScsiPortStartPacket(IN OUT PVOID Context
)
3066 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
3067 PIO_STACK_LOCATION IrpStack
;
3068 PSCSI_REQUEST_BLOCK Srb
;
3069 PDEVICE_OBJECT DeviceObject
= (PDEVICE_OBJECT
)Context
;
3070 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3071 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
3075 DPRINT("ScsiPortStartPacket() called\n");
3077 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
3079 IrpStack
= IoGetCurrentIrpStackLocation(DeviceObject
->CurrentIrp
);
3080 Srb
= IrpStack
->Parameters
.Scsi
.Srb
;
3082 /* Get LUN extension */
3083 LunExtension
= SpiGetLunExtension(DeviceExtension
,
3088 /* Check if we are in a reset state */
3089 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_RESET
)
3091 /* Mark the we've got requests while being in the reset state */
3092 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_RESET_REQUEST
;
3096 /* Set the time out value */
3097 DeviceExtension
->TimerCount
= Srb
->TimeOutValue
;
3100 DeviceExtension
->Flags
|= SCSI_PORT_DEVICE_BUSY
;
3102 if (LunExtension
->RequestTimeout
!= -1)
3104 /* Timer already active */
3109 /* It hasn't been initialized yet */
3110 LunExtension
->RequestTimeout
= Srb
->TimeOutValue
;
3114 if (Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
)
3116 /* Handle bypass-requests */
3118 /* Is this an abort request? */
3119 if (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
)
3121 /* Get pointer to SRB info structure */
3122 SrbInfo
= SpiGetSrbData(DeviceExtension
,
3128 /* Check if the request is still "active" */
3129 if (SrbInfo
== NULL
||
3130 SrbInfo
->Srb
== NULL
||
3131 !(SrbInfo
->Srb
->SrbFlags
& SRB_FLAGS_IS_ACTIVE
))
3133 /* It's not, mark it as active then */
3134 Srb
->SrbFlags
|= SRB_FLAGS_IS_ACTIVE
;
3137 LunExtension
->RequestTimeout
= -1;
3139 DPRINT("Request has been already completed, but abort request came\n");
3140 Srb
->SrbStatus
= SRB_STATUS_ABORT_FAILED
;
3142 /* Notify about request complete */
3143 ScsiPortNotification(RequestComplete
,
3144 DeviceExtension
->MiniPortDeviceExtension
,
3147 /* and about readiness for the next request */
3148 ScsiPortNotification(NextRequest
,
3149 DeviceExtension
->MiniPortDeviceExtension
);
3151 /* They might ask for some work, so queue the DPC for them */
3152 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
3154 /* We're done in this branch */
3160 /* Add number of queued requests */
3161 LunExtension
->QueueCount
++;
3164 /* Bypass requests don't need request sense */
3165 LunExtension
->Flags
&= ~LUNEX_NEED_REQUEST_SENSE
;
3167 /* Is disconnect disabled for this request? */
3168 if (Srb
->SrbFlags
& SRB_FLAGS_DISABLE_DISCONNECT
)
3170 /* Set the corresponding flag */
3171 DeviceExtension
->Flags
&= ~SCSI_PORT_DISCONNECT_ALLOWED
;
3174 /* Transfer timeout value from Srb to Lun */
3175 LunExtension
->RequestTimeout
= Srb
->TimeOutValue
;
3179 if (Srb
->SrbFlags
& SRB_FLAGS_DISABLE_DISCONNECT
)
3181 /* It's a disconnect, so no more requests can go */
3182 DeviceExtension
->Flags
&= ~SCSI_PORT_DISCONNECT_ALLOWED
;
3185 LunExtension
->Flags
|= SCSI_PORT_LU_ACTIVE
;
3187 /* Increment queue count */
3188 LunExtension
->QueueCount
++;
3190 /* If it's tagged - special thing */
3191 if (Srb
->QueueTag
!= SP_UNTAGGED
)
3193 SrbInfo
= &DeviceExtension
->SrbInfo
[Srb
->QueueTag
- 1];
3195 /* Chek for consistency */
3196 ASSERT(SrbInfo
->Requests
.Blink
== NULL
);
3198 /* Insert it into the list of requests */
3199 InsertTailList(&LunExtension
->SrbInfo
.Requests
, &SrbInfo
->Requests
);
3203 /* Mark this Srb active */
3204 Srb
->SrbFlags
|= SRB_FLAGS_IS_ACTIVE
;
3206 /* Call HwStartIo routine */
3207 Result
= DeviceExtension
->HwStartIo(&DeviceExtension
->MiniPortDeviceExtension
,
3210 /* If notification is needed, then request a DPC */
3211 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
3212 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
3217 IO_ALLOCATION_ACTION
3219 SpiAdapterControl(PDEVICE_OBJECT DeviceObject
,
3221 PVOID MapRegisterBase
,
3224 PSCSI_REQUEST_BLOCK Srb
;
3225 PSCSI_SG_ADDRESS ScatterGatherList
;
3227 PIO_STACK_LOCATION IrpStack
;
3228 ULONG TotalLength
= 0;
3229 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
3230 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
3232 BOOLEAN WriteToDevice
;
3234 /* Get pointers to SrbInfo and DeviceExtension */
3235 SrbInfo
= (PSCSI_REQUEST_BLOCK_INFO
)Context
;
3236 DeviceExtension
= DeviceObject
->DeviceExtension
;
3238 /* Get pointer to SRB */
3239 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
3240 Srb
= (PSCSI_REQUEST_BLOCK
)IrpStack
->Parameters
.Others
.Argument1
;
3242 /* Depending on the map registers number, we allocate
3243 either from NonPagedPool, or from our static list */
3244 if (SrbInfo
->NumberOfMapRegisters
> MAX_SG_LIST
)
3246 SrbInfo
->ScatterGather
= ExAllocatePoolWithTag(
3247 NonPagedPool
, SrbInfo
->NumberOfMapRegisters
* sizeof(SCSI_SG_ADDRESS
), TAG_SCSIPORT
);
3249 if (SrbInfo
->ScatterGather
== NULL
)
3252 Srb
->SrbFlags
|= SRB_FLAGS_SGLIST_FROM_POOL
;
3256 SrbInfo
->ScatterGather
= SrbInfo
->ScatterGatherList
;
3259 /* Use chosen SG list source */
3260 ScatterGatherList
= SrbInfo
->ScatterGather
;
3262 /* Save map registers base */
3263 SrbInfo
->BaseOfMapRegister
= MapRegisterBase
;
3265 /* Determine WriteToDevice flag */
3266 WriteToDevice
= Srb
->SrbFlags
& SRB_FLAGS_DATA_OUT
? TRUE
: FALSE
;
3268 /* Get virtual address of the data buffer */
3269 DataVA
= (PUCHAR
)MmGetMdlVirtualAddress(Irp
->MdlAddress
) +
3270 ((PCHAR
)Srb
->DataBuffer
- SrbInfo
->DataOffset
);
3272 /* Build the actual SG list */
3273 while (TotalLength
< Srb
->DataTransferLength
)
3275 if (!ScatterGatherList
)
3278 ScatterGatherList
->Length
= Srb
->DataTransferLength
- TotalLength
;
3279 ScatterGatherList
->PhysicalAddress
= IoMapTransfer(NULL
,
3282 DataVA
+ TotalLength
,
3283 &ScatterGatherList
->Length
,
3286 TotalLength
+= ScatterGatherList
->Length
;
3287 ScatterGatherList
++;
3290 /* Schedule an active request */
3291 InterlockedIncrement(&DeviceExtension
->ActiveRequestCounter
);
3292 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &CurrentIrql
);
3293 KeSynchronizeExecution(DeviceExtension
->Interrupt
[0],
3294 ScsiPortStartPacket
,
3296 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, CurrentIrql
);
3298 return DeallocateObjectKeepRegisters
;
3301 static PSCSI_PORT_LUN_EXTENSION
3302 SpiAllocateLunExtension (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
)
3304 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3305 ULONG LunExtensionSize
;
3307 DPRINT("SpiAllocateLunExtension (%p)\n",
3310 /* Round LunExtensionSize first to the sizeof LONGLONG */
3311 LunExtensionSize
= (DeviceExtension
->LunExtensionSize
+
3312 sizeof(LONGLONG
) - 1) & ~(sizeof(LONGLONG
) - 1);
3314 LunExtensionSize
+= sizeof(SCSI_PORT_LUN_EXTENSION
);
3315 DPRINT("LunExtensionSize %lu\n", LunExtensionSize
);
3317 LunExtension
= ExAllocatePoolWithTag(NonPagedPool
, LunExtensionSize
, TAG_SCSIPORT
);
3318 if (LunExtension
== NULL
)
3320 DPRINT1("Out of resources!\n");
3324 /* Zero everything */
3325 RtlZeroMemory(LunExtension
, LunExtensionSize
);
3327 /* Initialize a list of requests */
3328 InitializeListHead(&LunExtension
->SrbInfo
.Requests
);
3330 /* Initialize timeout counter */
3331 LunExtension
->RequestTimeout
= -1;
3333 /* Set maximum queue size */
3334 LunExtension
->MaxQueueCount
= 256;
3336 /* Initialize request queue */
3337 KeInitializeDeviceQueue (&LunExtension
->DeviceQueue
);
3339 return LunExtension
;
3342 static PSCSI_PORT_LUN_EXTENSION
3343 SpiGetLunExtension (IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
3348 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3350 DPRINT("SpiGetLunExtension(%p %u %u %u) called\n",
3351 DeviceExtension
, PathId
, TargetId
, Lun
);
3353 /* Get appropriate list */
3354 LunExtension
= DeviceExtension
->LunExtensionList
[(TargetId
+ Lun
) % LUS_NUMBER
];
3356 /* Iterate it until we find what we need */
3357 while (LunExtension
)
3359 if (LunExtension
->TargetId
== TargetId
&&
3360 LunExtension
->Lun
== Lun
&&
3361 LunExtension
->PathId
== PathId
)
3363 /* All matches, return */
3364 return LunExtension
;
3367 /* Advance to the next item */
3368 LunExtension
= LunExtension
->Next
;
3371 /* We did not find anything */
3372 DPRINT("Nothing found\n");
3376 static PSCSI_REQUEST_BLOCK_INFO
3377 SpiAllocateSrbStructures(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
3378 PSCSI_PORT_LUN_EXTENSION LunExtension
,
3379 PSCSI_REQUEST_BLOCK Srb
)
3382 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
3384 /* Spinlock must be held while this function executes */
3385 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
3387 /* Allocate SRB data structure */
3388 if (DeviceExtension
->NeedSrbDataAlloc
)
3390 /* Treat the abort request in a special way */
3391 if (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
)
3393 SrbInfo
= SpiGetSrbData(DeviceExtension
,
3399 else if (Srb
->SrbFlags
&
3400 (SRB_FLAGS_QUEUE_ACTION_ENABLE
| SRB_FLAGS_NO_QUEUE_FREEZE
) &&
3401 !(Srb
->SrbFlags
& SRB_FLAGS_DISABLE_DISCONNECT
)
3404 /* Do not process tagged commands if need request sense is set */
3405 if (LunExtension
->Flags
& LUNEX_NEED_REQUEST_SENSE
)
3407 ASSERT(!(LunExtension
->Flags
& LUNEX_REQUEST_PENDING
));
3409 LunExtension
->PendingRequest
= Srb
->OriginalRequest
;
3410 LunExtension
->Flags
|= LUNEX_REQUEST_PENDING
| SCSI_PORT_LU_ACTIVE
;
3412 /* Relese the spinlock and return */
3413 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3417 ASSERT(LunExtension
->SrbInfo
.Srb
== NULL
);
3418 SrbInfo
= DeviceExtension
->FreeSrbInfo
;
3420 if (SrbInfo
== NULL
)
3422 /* No SRB structures left in the list. We have to leave
3423 and wait while we are called again */
3425 DeviceExtension
->Flags
|= SCSI_PORT_REQUEST_PENDING
;
3426 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3430 DeviceExtension
->FreeSrbInfo
= (PSCSI_REQUEST_BLOCK_INFO
)SrbInfo
->Requests
.Flink
;
3432 /* QueueTag must never be 0, so +1 to it */
3433 Srb
->QueueTag
= (UCHAR
)(SrbInfo
- DeviceExtension
->SrbInfo
) + 1;
3437 /* Usual untagged command */
3439 (!IsListEmpty(&LunExtension
->SrbInfo
.Requests
) ||
3440 LunExtension
->Flags
& LUNEX_NEED_REQUEST_SENSE
) &&
3441 !(Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
)
3444 /* Mark it as pending and leave */
3445 ASSERT(!(LunExtension
->Flags
& LUNEX_REQUEST_PENDING
));
3446 LunExtension
->Flags
|= LUNEX_REQUEST_PENDING
| SCSI_PORT_LU_ACTIVE
;
3447 LunExtension
->PendingRequest
= Srb
->OriginalRequest
;
3449 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3453 Srb
->QueueTag
= SP_UNTAGGED
;
3454 SrbInfo
= &LunExtension
->SrbInfo
;
3459 Srb
->QueueTag
= SP_UNTAGGED
;
3460 SrbInfo
= &LunExtension
->SrbInfo
;
3463 /* Allocate SRB extension structure */
3464 if (DeviceExtension
->NeedSrbExtensionAlloc
)
3466 /* Check the list of free extensions */
3467 SrbExtension
= DeviceExtension
->FreeSrbExtensions
;
3469 /* If no free extensions... */
3470 if (SrbExtension
== NULL
)
3473 if (Srb
->Function
!= SRB_FUNCTION_ABORT_COMMAND
&&
3474 Srb
->QueueTag
!= SP_UNTAGGED
)
3476 SrbInfo
->Requests
.Blink
= NULL
;
3477 SrbInfo
->Requests
.Flink
= (PLIST_ENTRY
)DeviceExtension
->FreeSrbInfo
;
3478 DeviceExtension
->FreeSrbInfo
= SrbInfo
;
3481 /* Return, in order to be called again later */
3482 DeviceExtension
->Flags
|= SCSI_PORT_REQUEST_PENDING
;
3483 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3487 /* Remove that free SRB extension from the list (since
3488 we're going to use it) */
3489 DeviceExtension
->FreeSrbExtensions
= *((PVOID
*)SrbExtension
);
3491 /* Spinlock can be released now */
3492 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3494 Srb
->SrbExtension
= SrbExtension
;
3496 if (Srb
->SenseInfoBuffer
!= NULL
&&
3497 DeviceExtension
->SupportsAutoSense
)
3499 /* Store pointer to the SenseInfo buffer */
3500 SrbInfo
->SaveSenseRequest
= Srb
->SenseInfoBuffer
;
3502 /* Does data fit the buffer? */
3503 if (Srb
->SenseInfoBufferLength
> sizeof(SENSE_DATA
))
3505 /* No, disabling autosense at all */
3506 Srb
->SrbFlags
|= SRB_FLAGS_DISABLE_AUTOSENSE
;
3510 /* Yes, update the buffer pointer */
3511 Srb
->SenseInfoBuffer
= SrbExtension
+ DeviceExtension
->SrbExtensionSize
;
3518 Srb
->SrbExtension
= NULL
;
3519 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
3527 SpiSendInquiry (IN PDEVICE_OBJECT DeviceObject
,
3528 IN PSCSI_LUN_INFO LunInfo
)
3530 IO_STATUS_BLOCK IoStatusBlock
;
3531 PIO_STACK_LOCATION IrpStack
;
3536 PINQUIRYDATA InquiryBuffer
;
3537 PSENSE_DATA SenseBuffer
;
3538 BOOLEAN KeepTrying
= TRUE
;
3539 ULONG RetryCount
= 0;
3540 SCSI_REQUEST_BLOCK Srb
;
3542 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3543 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
3545 DPRINT ("SpiSendInquiry() called\n");
3547 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
3549 InquiryBuffer
= ExAllocatePoolWithTag (NonPagedPool
, INQUIRYDATABUFFERSIZE
, TAG_SCSIPORT
);
3550 if (InquiryBuffer
== NULL
)
3551 return STATUS_INSUFFICIENT_RESOURCES
;
3553 SenseBuffer
= ExAllocatePoolWithTag (NonPagedPool
, SENSE_BUFFER_SIZE
, TAG_SCSIPORT
);
3554 if (SenseBuffer
== NULL
)
3556 ExFreePool(InquiryBuffer
);
3557 return STATUS_INSUFFICIENT_RESOURCES
;
3562 /* Initialize event for waiting */
3563 KeInitializeEvent(&Event
,
3568 Irp
= IoBuildDeviceIoControlRequest(IOCTL_SCSI_EXECUTE_IN
,
3573 INQUIRYDATABUFFERSIZE
,
3579 DPRINT("IoBuildDeviceIoControlRequest() failed\n");
3580 return STATUS_INSUFFICIENT_RESOURCES
;
3584 RtlZeroMemory(&Srb
, sizeof(SCSI_REQUEST_BLOCK
));
3586 Srb
.Length
= sizeof(SCSI_REQUEST_BLOCK
);
3587 Srb
.OriginalRequest
= Irp
;
3588 Srb
.PathId
= LunInfo
->PathId
;
3589 Srb
.TargetId
= LunInfo
->TargetId
;
3590 Srb
.Lun
= LunInfo
->Lun
;
3591 Srb
.Function
= SRB_FUNCTION_EXECUTE_SCSI
;
3592 Srb
.SrbFlags
= SRB_FLAGS_DATA_IN
| SRB_FLAGS_DISABLE_SYNCH_TRANSFER
;
3593 Srb
.TimeOutValue
= 4;
3596 Srb
.SenseInfoBuffer
= SenseBuffer
;
3597 Srb
.SenseInfoBufferLength
= SENSE_BUFFER_SIZE
;
3599 Srb
.DataBuffer
= InquiryBuffer
;
3600 Srb
.DataTransferLength
= INQUIRYDATABUFFERSIZE
;
3602 /* Attach Srb to the Irp */
3603 IrpStack
= IoGetNextIrpStackLocation (Irp
);
3604 IrpStack
->Parameters
.Scsi
.Srb
= &Srb
;
3607 Cdb
= (PCDB
)Srb
.Cdb
;
3608 Cdb
->CDB6INQUIRY
.OperationCode
= SCSIOP_INQUIRY
;
3609 Cdb
->CDB6INQUIRY
.LogicalUnitNumber
= LunInfo
->Lun
;
3610 Cdb
->CDB6INQUIRY
.AllocationLength
= INQUIRYDATABUFFERSIZE
;
3612 /* Call the driver */
3613 Status
= IoCallDriver(DeviceObject
, Irp
);
3615 /* Wait for it to complete */
3616 if (Status
== STATUS_PENDING
)
3618 DPRINT("SpiSendInquiry(): Waiting for the driver to process request...\n");
3619 KeWaitForSingleObject(&Event
,
3624 Status
= IoStatusBlock
.Status
;
3627 DPRINT("SpiSendInquiry(): Request processed by driver, status = 0x%08X\n", Status
);
3629 if (SRB_STATUS(Srb
.SrbStatus
) == SRB_STATUS_SUCCESS
)
3631 /* All fine, copy data over */
3632 RtlCopyMemory(LunInfo
->InquiryData
,
3634 INQUIRYDATABUFFERSIZE
);
3636 Status
= STATUS_SUCCESS
;
3641 DPRINT("Inquiry SRB failed with SrbStatus 0x%08X\n", Srb
.SrbStatus
);
3642 /* Check if the queue is frozen */
3643 if (Srb
.SrbStatus
& SRB_STATUS_QUEUE_FROZEN
)
3645 /* Something weird happened, deal with it (unfreeze the queue) */
3648 DPRINT("SpiSendInquiry(): the queue is frozen at TargetId %d\n", Srb
.TargetId
);
3650 LunExtension
= SpiGetLunExtension(DeviceExtension
,
3655 /* Clear frozen flag */
3656 LunExtension
->Flags
&= ~LUNEX_FROZEN_QUEUE
;
3658 /* Acquire the spinlock */
3659 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
3661 /* Process the request */
3662 SpiGetNextRequestFromLun(DeviceObject
->DeviceExtension
, LunExtension
);
3664 /* SpiGetNextRequestFromLun() releases the spinlock,
3665 so we just lower irql back to what it was before */
3669 /* Check if data overrun happened */
3670 if (SRB_STATUS(Srb
.SrbStatus
) == SRB_STATUS_DATA_OVERRUN
)
3672 DPRINT("Data overrun at TargetId %d\n", LunInfo
->TargetId
);
3673 /* Nothing dramatic, just copy data, but limiting the size */
3674 RtlCopyMemory(LunInfo
->InquiryData
,
3676 (Srb
.DataTransferLength
> INQUIRYDATABUFFERSIZE
) ?
3677 INQUIRYDATABUFFERSIZE
: Srb
.DataTransferLength
);
3679 Status
= STATUS_SUCCESS
;
3682 else if ((Srb
.SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
) &&
3683 SenseBuffer
->SenseKey
== SCSI_SENSE_ILLEGAL_REQUEST
)
3685 /* LUN is not valid, but some device responds there.
3686 Mark it as invalid anyway */
3688 Status
= STATUS_INVALID_DEVICE_REQUEST
;
3693 /* Retry a couple of times if no timeout happened */
3694 if ((RetryCount
< 2) &&
3695 (SRB_STATUS(Srb
.SrbStatus
) != SRB_STATUS_NO_DEVICE
) &&
3696 (SRB_STATUS(Srb
.SrbStatus
) != SRB_STATUS_SELECTION_TIMEOUT
))
3703 /* That's all, go to exit */
3706 /* Set status according to SRB status */
3707 if (SRB_STATUS(Srb
.SrbStatus
) == SRB_STATUS_BAD_FUNCTION
||
3708 SRB_STATUS(Srb
.SrbStatus
) == SRB_STATUS_BAD_SRB_BLOCK_LENGTH
)
3710 Status
= STATUS_INVALID_DEVICE_REQUEST
;
3714 Status
= STATUS_IO_DEVICE_ERROR
;
3722 ExFreePool(InquiryBuffer
);
3723 ExFreePool(SenseBuffer
);
3725 DPRINT("SpiSendInquiry() done with Status 0x%08X\n", Status
);
3731 /* Scans all SCSI buses */
3733 SpiScanAdapter(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
)
3735 PSCSI_PORT_LUN_EXTENSION LunExtension
;
3739 PSCSI_BUS_SCAN_INFO BusScanInfo
;
3740 PSCSI_LUN_INFO LastLunInfo
, LunInfo
, LunInfoExists
;
3741 BOOLEAN DeviceExists
;
3746 DPRINT("SpiScanAdapter() called\n");
3748 /* Scan all buses */
3749 for (Bus
= 0; Bus
< DeviceExtension
->BusNum
; Bus
++)
3751 DPRINT(" Scanning bus %d\n", Bus
);
3754 /* Get pointer to the scan information */
3755 BusScanInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
];
3759 /* Find the last LUN info in the list */
3760 LunInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LunInfo
;
3761 LastLunInfo
= LunInfo
;
3763 while (LunInfo
!= NULL
)
3765 LastLunInfo
= LunInfo
;
3766 LunInfo
= LunInfo
->Next
;
3771 /* We need to allocate this buffer */
3772 BusScanInfo
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(SCSI_BUS_SCAN_INFO
), TAG_SCSIPORT
);
3776 DPRINT1("Out of resources!\n");
3780 /* Store the pointer in the BusScanInfo array */
3781 DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
] = BusScanInfo
;
3783 /* Fill this struct (length and bus ids for now) */
3784 BusScanInfo
->Length
= sizeof(SCSI_BUS_SCAN_INFO
);
3785 BusScanInfo
->LogicalUnitsCount
= 0;
3786 BusScanInfo
->BusIdentifier
= DeviceExtension
->PortConfig
->InitiatorBusId
[Bus
];
3787 BusScanInfo
->LunInfo
= NULL
;
3789 /* Set pointer to the last LUN info to NULL */
3793 /* Create LUN information structure */
3794 LunInfo
= ExAllocatePoolWithTag(PagedPool
, sizeof(SCSI_LUN_INFO
), TAG_SCSIPORT
);
3796 if (LunInfo
== NULL
)
3798 DPRINT1("Out of resources!\n");
3802 RtlZeroMemory(LunInfo
, sizeof(SCSI_LUN_INFO
));
3804 /* Create LunExtension */
3805 LunExtension
= SpiAllocateLunExtension (DeviceExtension
);
3807 /* And send INQUIRY to every target */
3808 for (Target
= 0; Target
< DeviceExtension
->PortConfig
->MaximumNumberOfTargets
; Target
++)
3810 /* TODO: Support scan bottom-up */
3812 /* Skip if it's the same address */
3813 if (Target
== BusScanInfo
->BusIdentifier
)
3816 /* Try to find an existing device here */
3817 DeviceExists
= FALSE
;
3818 LunInfoExists
= BusScanInfo
->LunInfo
;
3820 /* Find matching address on this bus */
3821 while (LunInfoExists
)
3823 if (LunInfoExists
->TargetId
== Target
)
3825 DeviceExists
= TRUE
;
3829 /* Advance to the next one */
3830 LunInfoExists
= LunInfoExists
->Next
;
3833 /* No need to bother rescanning, since we already did that before */
3837 /* Scan all logical units */
3838 for (Lun
= 0; Lun
< SCSI_MAXIMUM_LOGICAL_UNITS
; Lun
++)
3840 if ((!LunExtension
) || (!LunInfo
))
3843 /* Add extension to the list */
3844 Hint
= (Target
+ Lun
) % LUS_NUMBER
;
3845 LunExtension
->Next
= DeviceExtension
->LunExtensionList
[Hint
];
3846 DeviceExtension
->LunExtensionList
[Hint
] = LunExtension
;
3848 /* Fill Path, Target, Lun fields */
3849 LunExtension
->PathId
= LunInfo
->PathId
= (UCHAR
)Bus
;
3850 LunExtension
->TargetId
= LunInfo
->TargetId
= (UCHAR
) Target
;
3851 LunExtension
->Lun
= LunInfo
->Lun
= (UCHAR
)Lun
;
3853 /* Set flag to prevent race conditions */
3854 LunExtension
->Flags
|= SCSI_PORT_SCAN_IN_PROGRESS
;
3856 /* Zero LU extension contents */
3857 if (DeviceExtension
->LunExtensionSize
)
3859 RtlZeroMemory(LunExtension
+ 1,
3860 DeviceExtension
->LunExtensionSize
);
3863 /* Finally send the inquiry command */
3864 Status
= SpiSendInquiry(DeviceExtension
->DeviceObject
, LunInfo
);
3866 if (NT_SUCCESS(Status
))
3868 /* Let's see if we really found a device */
3869 PINQUIRYDATA InquiryData
= (PINQUIRYDATA
)LunInfo
->InquiryData
;
3871 /* Check if this device is unsupported */
3872 if (InquiryData
->DeviceTypeQualifier
== DEVICE_QUALIFIER_NOT_SUPPORTED
)
3874 DeviceExtension
->LunExtensionList
[Hint
] =
3875 DeviceExtension
->LunExtensionList
[Hint
]->Next
;
3880 /* Clear the "in scan" flag */
3881 LunExtension
->Flags
&= ~SCSI_PORT_SCAN_IN_PROGRESS
;
3883 DPRINT("SpiScanAdapter(): Found device of type %d at bus %d tid %d lun %d\n",
3884 InquiryData
->DeviceType
, Bus
, Target
, Lun
);
3886 /* Add this info to the linked list */
3887 LunInfo
->Next
= NULL
;
3889 LastLunInfo
->Next
= LunInfo
;
3891 BusScanInfo
->LunInfo
= LunInfo
;
3893 /* Store the last LUN info */
3894 LastLunInfo
= LunInfo
;
3896 /* Store DeviceObject */
3897 LunInfo
->DeviceObject
= DeviceExtension
->DeviceObject
;
3899 /* Allocate another buffer */
3900 LunInfo
= ExAllocatePoolWithTag(PagedPool
, sizeof(SCSI_LUN_INFO
), TAG_SCSIPORT
);
3904 DPRINT1("Out of resources!\n");
3908 RtlZeroMemory(LunInfo
, sizeof(SCSI_LUN_INFO
));
3910 /* Create a new LU extension */
3911 LunExtension
= SpiAllocateLunExtension(DeviceExtension
);
3917 /* Remove this LUN from the list */
3918 DeviceExtension
->LunExtensionList
[Hint
] =
3919 DeviceExtension
->LunExtensionList
[Hint
]->Next
;
3921 /* Decide whether we are continuing or not */
3922 if (Status
== STATUS_INVALID_DEVICE_REQUEST
)
3930 /* Free allocated buffers */
3932 ExFreePool(LunExtension
);
3935 ExFreePool(LunInfo
);
3937 /* Sum what we found */
3938 BusScanInfo
->LogicalUnitsCount
+= (UCHAR
) DevicesFound
;
3939 DPRINT(" Found %d devices on bus %d\n", DevicesFound
, Bus
);
3942 DPRINT ("SpiScanAdapter() done\n");
3947 SpiGetInquiryData(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
3950 ULONG InquiryDataSize
;
3951 PSCSI_LUN_INFO LunInfo
;
3952 ULONG BusCount
, LunCount
, Length
;
3953 PIO_STACK_LOCATION IrpStack
;
3954 PSCSI_ADAPTER_BUS_INFO AdapterBusInfo
;
3955 PSCSI_INQUIRY_DATA InquiryData
;
3956 PSCSI_BUS_DATA BusData
;
3960 DPRINT("SpiGetInquiryData() called\n");
3962 /* Get pointer to the buffer */
3963 IrpStack
= IoGetCurrentIrpStackLocation(Irp
);
3964 Buffer
= Irp
->AssociatedIrp
.SystemBuffer
;
3966 /* Initialize bus and LUN counters */
3967 BusCount
= DeviceExtension
->BusesConfig
->NumberOfBuses
;
3970 /* Calculate total number of LUNs */
3971 for (Bus
= 0; Bus
< BusCount
; Bus
++)
3972 LunCount
+= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LogicalUnitsCount
;
3974 /* Calculate size of inquiry data, rounding up to sizeof(ULONG) */
3976 ((sizeof(SCSI_INQUIRY_DATA
) - 1 + INQUIRYDATABUFFERSIZE
+
3977 sizeof(ULONG
) - 1) & ~(sizeof(ULONG
) - 1));
3979 /* Calculate data size */
3980 Length
= sizeof(SCSI_ADAPTER_BUS_INFO
) + (BusCount
- 1) *
3981 sizeof(SCSI_BUS_DATA
);
3983 Length
+= InquiryDataSize
* LunCount
;
3985 /* Check, if all data is going to fit into provided buffer */
3986 if (IrpStack
->Parameters
.DeviceIoControl
.OutputBufferLength
< Length
)
3988 Irp
->IoStatus
.Status
= STATUS_BUFFER_TOO_SMALL
;
3989 return STATUS_BUFFER_TOO_SMALL
;
3992 /* Store data size in the IRP */
3993 Irp
->IoStatus
.Information
= Length
;
3995 DPRINT("Data size: %lu\n", Length
);
3997 AdapterBusInfo
= (PSCSI_ADAPTER_BUS_INFO
)Buffer
;
3999 AdapterBusInfo
->NumberOfBuses
= (UCHAR
)BusCount
;
4001 /* Point InquiryData to the corresponding place inside Buffer */
4002 InquiryData
= (PSCSI_INQUIRY_DATA
)(Buffer
+ sizeof(SCSI_ADAPTER_BUS_INFO
) +
4003 (BusCount
- 1) * sizeof(SCSI_BUS_DATA
));
4006 for (Bus
= 0; Bus
< BusCount
; Bus
++)
4008 BusData
= &AdapterBusInfo
->BusData
[Bus
];
4010 /* Calculate and save an offset of the inquiry data */
4011 BusData
->InquiryDataOffset
= (PUCHAR
)InquiryData
- Buffer
;
4013 /* Get a pointer to the LUN information structure */
4014 LunInfo
= DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LunInfo
;
4016 /* Store Initiator Bus Id */
4017 BusData
->InitiatorBusId
=
4018 DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->BusIdentifier
;
4020 /* Store LUN count */
4021 BusData
->NumberOfLogicalUnits
=
4022 DeviceExtension
->BusesConfig
->BusScanInfo
[Bus
]->LogicalUnitsCount
;
4025 while (LunInfo
!= NULL
)
4027 DPRINT("(Bus %lu Target %lu Lun %lu)\n",
4028 Bus
, LunInfo
->TargetId
, LunInfo
->Lun
);
4030 /* Fill InquiryData with values */
4031 InquiryData
->PathId
= LunInfo
->PathId
;
4032 InquiryData
->TargetId
= LunInfo
->TargetId
;
4033 InquiryData
->Lun
= LunInfo
->Lun
;
4034 InquiryData
->InquiryDataLength
= INQUIRYDATABUFFERSIZE
;
4035 InquiryData
->DeviceClaimed
= LunInfo
->DeviceClaimed
;
4036 InquiryData
->NextInquiryDataOffset
=
4037 (PUCHAR
)InquiryData
+ InquiryDataSize
- Buffer
;
4039 /* Copy data in it */
4040 RtlCopyMemory(InquiryData
->InquiryData
,
4041 LunInfo
->InquiryData
,
4042 INQUIRYDATABUFFERSIZE
);
4044 /* Move to the next LUN */
4045 LunInfo
= LunInfo
->Next
;
4046 InquiryData
= (PSCSI_INQUIRY_DATA
) ((PCHAR
)InquiryData
+ InquiryDataSize
);
4049 /* Either mark the end, or set offset to 0 */
4050 if (BusData
->NumberOfLogicalUnits
!= 0)
4051 ((PSCSI_INQUIRY_DATA
) ((PCHAR
) InquiryData
- InquiryDataSize
))->NextInquiryDataOffset
= 0;
4053 BusData
->InquiryDataOffset
= 0;
4056 /* Finish with success */
4057 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
4058 return STATUS_SUCCESS
;
4061 static PSCSI_REQUEST_BLOCK_INFO
4062 SpiGetSrbData(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
4068 PSCSI_PORT_LUN_EXTENSION LunExtension
;
4070 if (QueueTag
== SP_UNTAGGED
)
4072 /* Untagged request, get LU and return pointer to SrbInfo */
4073 LunExtension
= SpiGetLunExtension(DeviceExtension
,
4078 /* Return NULL in case of error */
4082 /* Return the pointer to SrbInfo */
4083 return &LunExtension
->SrbInfo
;
4087 /* Make sure the tag is valid, if it is - return the data */
4088 if (QueueTag
> DeviceExtension
->SrbDataCount
|| QueueTag
< 1)
4091 return &DeviceExtension
->SrbInfo
[QueueTag
-1];
4096 SpiSendRequestSense(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
4097 IN PSCSI_REQUEST_BLOCK InitialSrb
)
4099 PSCSI_REQUEST_BLOCK Srb
;
4102 PIO_STACK_LOCATION IrpStack
;
4103 LARGE_INTEGER LargeInt
;
4106 DPRINT("SpiSendRequestSense() entered, InitialSrb %p\n", InitialSrb
);
4109 Srb
= ExAllocatePoolWithTag(NonPagedPool
, sizeof(SCSI_REQUEST_BLOCK
) + sizeof(PVOID
), TAG_SCSIPORT
);
4110 RtlZeroMemory(Srb
, sizeof(SCSI_REQUEST_BLOCK
));
4113 LargeInt
.QuadPart
= (LONGLONG
) 1;
4114 Irp
= IoBuildAsynchronousFsdRequest(IRP_MJ_READ
,
4115 DeviceExtension
->DeviceObject
,
4116 InitialSrb
->SenseInfoBuffer
,
4117 InitialSrb
->SenseInfoBufferLength
,
4121 IoSetCompletionRoutine(Irp
,
4122 (PIO_COMPLETION_ROUTINE
)SpiCompletionRoutine
,
4130 DPRINT("SpiSendRequestSense() failed, Srb %p\n", Srb
);
4134 IrpStack
= IoGetNextIrpStackLocation(Irp
);
4135 IrpStack
->MajorFunction
= IRP_MJ_SCSI
;
4137 /* Put Srb address into Irp... */
4138 IrpStack
->Parameters
.Others
.Argument1
= (PVOID
)Srb
;
4140 /* ...and vice versa */
4141 Srb
->OriginalRequest
= Irp
;
4144 Ptr
= (PVOID
*)(Srb
+1);
4147 /* Build CDB for REQUEST SENSE */
4149 Cdb
= (PCDB
)Srb
->Cdb
;
4151 Cdb
->CDB6INQUIRY
.OperationCode
= SCSIOP_REQUEST_SENSE
;
4152 Cdb
->CDB6INQUIRY
.LogicalUnitNumber
= 0;
4153 Cdb
->CDB6INQUIRY
.Reserved1
= 0;
4154 Cdb
->CDB6INQUIRY
.PageCode
= 0;
4155 Cdb
->CDB6INQUIRY
.IReserved
= 0;
4156 Cdb
->CDB6INQUIRY
.AllocationLength
= (UCHAR
)InitialSrb
->SenseInfoBufferLength
;
4157 Cdb
->CDB6INQUIRY
.Control
= 0;
4160 Srb
->TargetId
= InitialSrb
->TargetId
;
4161 Srb
->Lun
= InitialSrb
->Lun
;
4162 Srb
->PathId
= InitialSrb
->PathId
;
4164 Srb
->Function
= SRB_FUNCTION_EXECUTE_SCSI
;
4165 Srb
->Length
= sizeof(SCSI_REQUEST_BLOCK
);
4167 /* Timeout will be 2 seconds */
4168 Srb
->TimeOutValue
= 2;
4170 /* No auto request sense */
4171 Srb
->SenseInfoBufferLength
= 0;
4172 Srb
->SenseInfoBuffer
= NULL
;
4174 /* Set necessary flags */
4175 Srb
->SrbFlags
= SRB_FLAGS_DATA_IN
| SRB_FLAGS_BYPASS_FROZEN_QUEUE
|
4176 SRB_FLAGS_DISABLE_DISCONNECT
;
4178 /* Transfer disable synch transfer flag */
4179 if (InitialSrb
->SrbFlags
& SRB_FLAGS_DISABLE_SYNCH_TRANSFER
)
4180 Srb
->SrbFlags
|= SRB_FLAGS_DISABLE_SYNCH_TRANSFER
;
4182 Srb
->DataBuffer
= InitialSrb
->SenseInfoBuffer
;
4184 /* Fill the transfer length */
4185 Srb
->DataTransferLength
= InitialSrb
->SenseInfoBufferLength
;
4187 /* Clear statuses */
4188 Srb
->ScsiStatus
= Srb
->SrbStatus
= 0;
4191 /* Call the driver */
4192 (VOID
)IoCallDriver(DeviceExtension
->DeviceObject
, Irp
);
4194 DPRINT("SpiSendRequestSense() done\n");
4201 SpiProcessCompletedRequest(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
4202 IN PSCSI_REQUEST_BLOCK_INFO SrbInfo
,
4203 OUT PBOOLEAN NeedToCallStartIo
)
4205 PSCSI_REQUEST_BLOCK Srb
;
4206 PSCSI_PORT_LUN_EXTENSION LunExtension
;
4209 //ULONG SequenceNumber;
4212 Irp
= Srb
->OriginalRequest
;
4214 /* Get Lun extension */
4215 LunExtension
= SpiGetLunExtension(DeviceExtension
,
4220 if (Srb
->SrbFlags
& SRB_FLAGS_UNSPECIFIED_DIRECTION
&&
4221 DeviceExtension
->MapBuffers
&&
4224 /* MDL is shared if transfer is broken into smaller parts */
4225 Srb
->DataBuffer
= (PCCHAR
)MmGetMdlVirtualAddress(Irp
->MdlAddress
) +
4226 ((PCCHAR
)Srb
->DataBuffer
- SrbInfo
->DataOffset
);
4228 /* In case of data going in, flush the buffers */
4229 if (Srb
->SrbFlags
& SRB_FLAGS_DATA_IN
)
4231 KeFlushIoBuffers(Irp
->MdlAddress
,
4237 /* Flush adapter if needed */
4238 if (SrbInfo
->BaseOfMapRegister
)
4240 /* TODO: Implement */
4244 /* Clear the request */
4245 SrbInfo
->Srb
= NULL
;
4247 /* If disconnect is disabled... */
4248 if (Srb
->SrbFlags
& SRB_FLAGS_DISABLE_DISCONNECT
)
4250 /* Acquire the spinlock since we mess with flags */
4251 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4253 /* Set corresponding flag */
4254 DeviceExtension
->Flags
|= SCSI_PORT_DISCONNECT_ALLOWED
;
4256 /* Clear the timer if needed */
4257 if (!(DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_RESET
))
4258 DeviceExtension
->TimerCount
= -1;
4260 /* Spinlock is not needed anymore */
4261 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4263 if (!(DeviceExtension
->Flags
& SCSI_PORT_REQUEST_PENDING
) &&
4264 !(DeviceExtension
->Flags
& SCSI_PORT_DEVICE_BUSY
) &&
4265 !(*NeedToCallStartIo
))
4267 /* We're not busy, but we have a request pending */
4268 IoStartNextPacket(DeviceExtension
->DeviceObject
, FALSE
);
4272 /* Scatter/gather */
4273 if (Srb
->SrbFlags
& SRB_FLAGS_SGLIST_FROM_POOL
)
4275 /* TODO: Implement */
4279 /* Acquire spinlock (we're freeing SrbExtension) */
4280 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4282 /* Free it (if needed) */
4283 if (Srb
->SrbExtension
)
4285 if (Srb
->SenseInfoBuffer
!= NULL
&& DeviceExtension
->SupportsAutoSense
)
4287 ASSERT(Srb
->SenseInfoBuffer
== NULL
|| SrbInfo
->SaveSenseRequest
!= NULL
);
4289 if (Srb
->SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
)
4291 /* Copy sense data to the buffer */
4292 RtlCopyMemory(SrbInfo
->SaveSenseRequest
,
4293 Srb
->SenseInfoBuffer
,
4294 Srb
->SenseInfoBufferLength
);
4297 /* And restore the pointer */
4298 Srb
->SenseInfoBuffer
= SrbInfo
->SaveSenseRequest
;
4301 /* Put it into the free srb extensions list */
4302 *((PVOID
*)Srb
->SrbExtension
) = DeviceExtension
->FreeSrbExtensions
;
4303 DeviceExtension
->FreeSrbExtensions
= Srb
->SrbExtension
;
4306 /* Save transfer length in the IRP */
4307 Irp
->IoStatus
.Information
= Srb
->DataTransferLength
;
4309 //SequenceNumber = SrbInfo->SequenceNumber;
4310 SrbInfo
->SequenceNumber
= 0;
4312 /* Decrement the queue count */
4313 LunExtension
->QueueCount
--;
4315 /* Free Srb, if needed*/
4316 if (Srb
->QueueTag
!= SP_UNTAGGED
)
4318 /* Put it into the free list */
4319 SrbInfo
->Requests
.Blink
= NULL
;
4320 SrbInfo
->Requests
.Flink
= (PLIST_ENTRY
)DeviceExtension
->FreeSrbInfo
;
4321 DeviceExtension
->FreeSrbInfo
= SrbInfo
;
4324 /* SrbInfo is not used anymore */
4327 if (DeviceExtension
->Flags
& SCSI_PORT_REQUEST_PENDING
)
4329 /* Clear the flag */
4330 DeviceExtension
->Flags
&= ~SCSI_PORT_REQUEST_PENDING
;
4332 /* Note the caller about StartIo */
4333 *NeedToCallStartIo
= TRUE
;
4336 if (SRB_STATUS(Srb
->SrbStatus
) == SRB_STATUS_SUCCESS
)
4338 /* Start the packet */
4339 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
4341 if (!(Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
) &&
4342 LunExtension
->RequestTimeout
== -1)
4344 /* Start the next packet */
4345 SpiGetNextRequestFromLun(DeviceExtension
, LunExtension
);
4349 /* Release the spinlock */
4350 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4353 DPRINT("IoCompleting request IRP 0x%p\n", Irp
);
4355 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
4357 /* Decrement number of active requests, and analyze the result */
4358 Result
= InterlockedDecrement(&DeviceExtension
->ActiveRequestCounter
);
4361 !DeviceExtension
->MapRegisters
&&
4362 DeviceExtension
->AdapterObject
!= NULL
)
4364 /* Nullify map registers */
4365 DeviceExtension
->MapRegisterBase
= NULL
;
4366 IoFreeAdapterChannel(DeviceExtension
->AdapterObject
);
4369 /* Exit, we're done */
4373 /* Decrement number of active requests, and analyze the result */
4374 Result
= InterlockedDecrement(&DeviceExtension
->ActiveRequestCounter
);
4377 !DeviceExtension
->MapRegisters
&&
4378 DeviceExtension
->AdapterObject
!= NULL
)
4380 /* Result is negative, so this is a slave, free map registers */
4381 DeviceExtension
->MapRegisterBase
= NULL
;
4382 IoFreeAdapterChannel(DeviceExtension
->AdapterObject
);
4385 /* Convert status */
4386 Irp
->IoStatus
.Status
= SpiStatusSrbToNt(Srb
->SrbStatus
);
4388 /* It's not a bypass, it's busy or the queue is full? */
4389 if ((Srb
->ScsiStatus
== SCSISTAT_BUSY
||
4390 Srb
->SrbStatus
== SRB_STATUS_BUSY
||
4391 Srb
->ScsiStatus
== SCSISTAT_QUEUE_FULL
) &&
4392 !(Srb
->SrbFlags
& SRB_FLAGS_BYPASS_FROZEN_QUEUE
))
4395 DPRINT("Busy SRB status %x\n", Srb
->SrbStatus
);
4397 /* Requeu, if needed */
4398 if (LunExtension
->Flags
& (LUNEX_FROZEN_QUEUE
| LUNEX_BUSY
))
4400 DPRINT("it's being requeued\n");
4402 Srb
->SrbStatus
= SRB_STATUS_PENDING
;
4403 Srb
->ScsiStatus
= 0;
4405 if (!KeInsertByKeyDeviceQueue(&LunExtension
->DeviceQueue
,
4406 &Irp
->Tail
.Overlay
.DeviceQueueEntry
,
4409 /* It's a big f.ck up if we got here */
4410 Srb
->SrbStatus
= SRB_STATUS_ERROR
;
4411 Srb
->ScsiStatus
= SCSISTAT_BUSY
;
4417 /* Release the spinlock */
4418 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4421 else if (LunExtension
->AttemptCount
++ < 20)
4423 /* LUN is still busy */
4424 Srb
->ScsiStatus
= 0;
4425 Srb
->SrbStatus
= SRB_STATUS_PENDING
;
4427 LunExtension
->BusyRequest
= Irp
;
4428 LunExtension
->Flags
|= LUNEX_BUSY
;
4430 /* Release the spinlock */
4431 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4436 /* Freeze the queue*/
4437 Srb
->SrbStatus
|= SRB_STATUS_QUEUE_FROZEN
;
4438 LunExtension
->Flags
|= LUNEX_FROZEN_QUEUE
;
4440 /* "Unfull" the queue */
4441 LunExtension
->Flags
&= ~LUNEX_FULL_QUEUE
;
4443 /* Release the spinlock */
4444 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4446 /* Return status that the device is not ready */
4447 Irp
->IoStatus
.Status
= STATUS_DEVICE_NOT_READY
;
4448 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
4454 /* Start the next request, if LUN is idle, and this is sense request */
4455 if (((Srb
->ScsiStatus
!= SCSISTAT_CHECK_CONDITION
) ||
4456 (Srb
->SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
) ||
4457 !Srb
->SenseInfoBuffer
|| !Srb
->SenseInfoBufferLength
)
4458 && (Srb
->SrbFlags
& SRB_FLAGS_NO_QUEUE_FREEZE
))
4460 if (LunExtension
->RequestTimeout
== -1)
4461 SpiGetNextRequestFromLun(DeviceExtension
, LunExtension
);
4463 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4467 /* Freeze the queue */
4468 Srb
->SrbStatus
|= SRB_STATUS_QUEUE_FROZEN
;
4469 LunExtension
->Flags
|= LUNEX_FROZEN_QUEUE
;
4471 /* Do we need a request sense? */
4472 if (Srb
->ScsiStatus
== SCSISTAT_CHECK_CONDITION
&&
4473 !(Srb
->SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
) &&
4474 Srb
->SenseInfoBuffer
&& Srb
->SenseInfoBufferLength
)
4476 /* If LUN is busy, we have to requeue it in order to allow request sense */
4477 if (LunExtension
->Flags
& LUNEX_BUSY
)
4479 DPRINT("Requeueing busy request to allow request sense\n");
4481 if (!KeInsertByKeyDeviceQueue(&LunExtension
->DeviceQueue
,
4482 &LunExtension
->BusyRequest
->Tail
.Overlay
.DeviceQueueEntry
,
4485 /* We should never get here */
4488 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4489 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
4494 /* Clear busy flags */
4495 LunExtension
->Flags
&= ~(LUNEX_FULL_QUEUE
| LUNEX_BUSY
);
4498 /* Release the spinlock */
4499 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4501 /* Send RequestSense */
4502 SpiSendRequestSense(DeviceExtension
, Srb
);
4508 /* Release the spinlock */
4509 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4512 /* Complete the request */
4513 IoCompleteRequest(Irp
, IO_DISK_INCREMENT
);
4518 SpiCompletionRoutine(PDEVICE_OBJECT DeviceObject
,
4522 PSCSI_REQUEST_BLOCK Srb
= (PSCSI_REQUEST_BLOCK
)Context
;
4523 PSCSI_REQUEST_BLOCK InitialSrb
;
4526 DPRINT("SpiCompletionRoutine() entered, IRP %p \n", Irp
);
4528 if ((Srb
->Function
== SRB_FUNCTION_RESET_BUS
) ||
4529 (Srb
->Function
== SRB_FUNCTION_ABORT_COMMAND
))
4531 /* Deallocate SRB and IRP and exit */
4535 return STATUS_MORE_PROCESSING_REQUIRED
;
4538 /* Get a pointer to the SRB and IRP which were initially sent */
4539 InitialSrb
= *((PVOID
*)(Srb
+1));
4540 InitialIrp
= InitialSrb
->OriginalRequest
;
4542 if ((SRB_STATUS(Srb
->SrbStatus
) == SRB_STATUS_SUCCESS
) ||
4543 (SRB_STATUS(Srb
->SrbStatus
) == SRB_STATUS_DATA_OVERRUN
))
4545 /* Sense data is OK */
4546 InitialSrb
->SrbStatus
|= SRB_STATUS_AUTOSENSE_VALID
;
4548 /* Set length to be the same */
4549 InitialSrb
->SenseInfoBufferLength
= (UCHAR
)Srb
->DataTransferLength
;
4552 /* Make sure initial SRB's queue is frozen */
4553 ASSERT(InitialSrb
->SrbStatus
& SRB_STATUS_QUEUE_FROZEN
);
4555 /* Complete this request */
4556 IoCompleteRequest(InitialIrp
, IO_DISK_INCREMENT
);
4558 /* Deallocate everything (internal) */
4561 if (Irp
->MdlAddress
!= NULL
)
4563 MmUnlockPages(Irp
->MdlAddress
);
4564 IoFreeMdl(Irp
->MdlAddress
);
4565 Irp
->MdlAddress
= NULL
;
4569 return STATUS_MORE_PROCESSING_REQUIRED
;
4572 static BOOLEAN NTAPI
4573 ScsiPortIsr(IN PKINTERRUPT Interrupt
,
4574 IN PVOID ServiceContext
)
4576 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
4578 DPRINT("ScsiPortIsr() called!\n");
4580 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)ServiceContext
;
4582 /* If interrupts are disabled - we don't expect any */
4583 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_DISABLE_INTERRUPTS
)
4586 /* Call miniport's HwInterrupt routine */
4587 DeviceExtension
->HwInterrupt(&DeviceExtension
->MiniPortDeviceExtension
);
4589 /* If flag of notification is set - queue a DPC */
4590 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
4592 IoRequestDpc(DeviceExtension
->DeviceObject
,
4593 DeviceExtension
->CurrentIrp
,
4602 SpiSaveInterruptData(IN PVOID Context
)
4604 PSCSI_PORT_SAVE_INTERRUPT InterruptContext
= Context
;
4605 PSCSI_PORT_LUN_EXTENSION LunExtension
;
4606 PSCSI_REQUEST_BLOCK Srb
;
4607 PSCSI_REQUEST_BLOCK_INFO SrbInfo
, NextSrbInfo
;
4608 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
4611 /* Get pointer to the device extension */
4612 DeviceExtension
= InterruptContext
->DeviceExtension
;
4614 /* If we don't have anything pending - return */
4615 if (!(DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
))
4618 /* Actually save the interrupt data */
4619 *InterruptContext
->InterruptData
= DeviceExtension
->InterruptData
;
4621 /* Clear the data stored in the device extension */
4622 DeviceExtension
->InterruptData
.Flags
&=
4623 (SCSI_PORT_RESET
| SCSI_PORT_RESET_REQUEST
| SCSI_PORT_DISABLE_INTERRUPTS
);
4624 DeviceExtension
->InterruptData
.CompletedAbort
= NULL
;
4625 DeviceExtension
->InterruptData
.ReadyLun
= NULL
;
4626 DeviceExtension
->InterruptData
.CompletedRequests
= NULL
;
4628 /* Loop through the list of completed requests */
4629 SrbInfo
= InterruptContext
->InterruptData
->CompletedRequests
;
4633 /* Make sure we have SRV */
4634 ASSERT(SrbInfo
->Srb
);
4636 /* Get SRB and LunExtension */
4639 LunExtension
= SpiGetLunExtension(DeviceExtension
,
4644 /* We have to check special cases if request is unsuccessfull*/
4645 if (Srb
->SrbStatus
!= SRB_STATUS_SUCCESS
)
4647 /* Check if we need request sense by a few conditions */
4648 if (Srb
->SenseInfoBuffer
&& Srb
->SenseInfoBufferLength
&&
4649 Srb
->ScsiStatus
== SCSISTAT_CHECK_CONDITION
&&
4650 !(Srb
->SrbStatus
& SRB_STATUS_AUTOSENSE_VALID
))
4652 if (LunExtension
->Flags
& LUNEX_NEED_REQUEST_SENSE
)
4654 /* It means: we tried to send REQUEST SENSE, but failed */
4656 Srb
->ScsiStatus
= 0;
4657 Srb
->SrbStatus
= SRB_STATUS_REQUEST_SENSE_FAILED
;
4661 /* Set the corresponding flag, so that REQUEST SENSE
4663 LunExtension
->Flags
|= LUNEX_NEED_REQUEST_SENSE
;
4668 /* Check for a full queue */
4669 if (Srb
->ScsiStatus
== SCSISTAT_QUEUE_FULL
)
4671 /* TODO: Implement when it's encountered */
4676 /* Let's decide if we need to watch timeout or not */
4677 if (Srb
->QueueTag
== SP_UNTAGGED
)
4683 if (LunExtension
->SrbInfo
.Requests
.Flink
== &SrbInfo
->Requests
)
4688 /* Remove it from the queue */
4689 RemoveEntryList(&SrbInfo
->Requests
);
4694 /* We have to maintain timeout counter */
4695 if (IsListEmpty(&LunExtension
->SrbInfo
.Requests
))
4697 LunExtension
->RequestTimeout
= -1;
4701 NextSrbInfo
= CONTAINING_RECORD(LunExtension
->SrbInfo
.Requests
.Flink
,
4702 SCSI_REQUEST_BLOCK_INFO
,
4705 Srb
= NextSrbInfo
->Srb
;
4707 /* Update timeout counter */
4708 LunExtension
->RequestTimeout
= Srb
->TimeOutValue
;
4712 SrbInfo
= SrbInfo
->CompletedRequests
;
4720 SpiGetNextRequestFromLun(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
4721 IN PSCSI_PORT_LUN_EXTENSION LunExtension
)
4723 PIO_STACK_LOCATION IrpStack
;
4725 PKDEVICE_QUEUE_ENTRY Entry
;
4726 PSCSI_REQUEST_BLOCK Srb
;
4729 /* If LUN is not active or queue is more than maximum allowed */
4730 if (LunExtension
->QueueCount
>= LunExtension
->MaxQueueCount
||
4731 !(LunExtension
->Flags
& SCSI_PORT_LU_ACTIVE
))
4733 /* Release the spinlock and exit */
4734 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4738 /* Check if we can get a next request */
4739 if (LunExtension
->Flags
&
4740 (LUNEX_NEED_REQUEST_SENSE
| LUNEX_BUSY
|
4741 LUNEX_FULL_QUEUE
| LUNEX_FROZEN_QUEUE
| LUNEX_REQUEST_PENDING
))
4743 /* Pending requests can only be started if the queue is empty */
4744 if (IsListEmpty(&LunExtension
->SrbInfo
.Requests
) &&
4745 !(LunExtension
->Flags
&
4746 (LUNEX_BUSY
| LUNEX_FROZEN_QUEUE
| LUNEX_FULL_QUEUE
| LUNEX_NEED_REQUEST_SENSE
)))
4748 /* Make sure we have SRB */
4749 ASSERT(LunExtension
->SrbInfo
.Srb
== NULL
);
4751 /* Clear active and pending flags */
4752 LunExtension
->Flags
&= ~(LUNEX_REQUEST_PENDING
| SCSI_PORT_LU_ACTIVE
);
4754 /* Get next Irp, and clear pending requests list */
4755 NextIrp
= LunExtension
->PendingRequest
;
4756 LunExtension
->PendingRequest
= NULL
;
4758 /* Set attempt counter to zero */
4759 LunExtension
->AttemptCount
= 0;
4761 /* Release the spinlock */
4762 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4764 /* Start the next pending request */
4765 IoStartPacket(DeviceExtension
->DeviceObject
, NextIrp
, (PULONG
)NULL
, NULL
);
4771 /* Release the spinlock, without clearing any flags and exit */
4772 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4778 /* Reset active flag */
4779 LunExtension
->Flags
&= ~SCSI_PORT_LU_ACTIVE
;
4781 /* Set attempt counter to zero */
4782 LunExtension
->AttemptCount
= 0;
4784 /* Remove packet from the device queue */
4785 Entry
= KeRemoveByKeyDeviceQueue(&LunExtension
->DeviceQueue
, LunExtension
->SortKey
);
4789 /* Get pointer to the next irp */
4790 NextIrp
= CONTAINING_RECORD(Entry
, IRP
, Tail
.Overlay
.DeviceQueueEntry
);
4792 /* Get point to the SRB */
4793 IrpStack
= IoGetCurrentIrpStackLocation(NextIrp
);
4794 Srb
= (PSCSI_REQUEST_BLOCK
)IrpStack
->Parameters
.Others
.Argument1
;
4797 LunExtension
->SortKey
= Srb
->QueueSortKey
;
4798 LunExtension
->SortKey
++;
4800 /* Release the spinlock */
4801 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4803 /* Start the next pending request */
4804 IoStartPacket(DeviceExtension
->DeviceObject
, NextIrp
, (PULONG
)NULL
, NULL
);
4808 /* Release the spinlock */
4809 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4815 // ScsiPortDpcForIsr
4822 // IN PDEVICE_OBJECT DpcDeviceObject
4824 // IN PVOID DpcContext
4827 ScsiPortDpcForIsr(IN PKDPC Dpc
,
4828 IN PDEVICE_OBJECT DpcDeviceObject
,
4830 IN PVOID DpcContext
)
4832 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
= DpcDeviceObject
->DeviceExtension
;
4833 SCSI_PORT_INTERRUPT_DATA InterruptData
;
4834 SCSI_PORT_SAVE_INTERRUPT Context
;
4835 PSCSI_PORT_LUN_EXTENSION LunExtension
;
4836 BOOLEAN NeedToStartIo
;
4837 PSCSI_REQUEST_BLOCK_INFO SrbInfo
;
4838 LARGE_INTEGER TimerValue
;
4840 DPRINT("ScsiPortDpcForIsr(Dpc %p DpcDeviceObject %p DpcIrp %p DpcContext %p)\n",
4841 Dpc
, DpcDeviceObject
, DpcIrp
, DpcContext
);
4843 /* We need to acquire spinlock */
4844 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4846 RtlZeroMemory(&InterruptData
, sizeof(SCSI_PORT_INTERRUPT_DATA
));
4850 /* Interrupt structure must be snapshotted, and only then analyzed */
4851 Context
.InterruptData
= &InterruptData
;
4852 Context
.DeviceExtension
= DeviceExtension
;
4854 if (!KeSynchronizeExecution(DeviceExtension
->Interrupt
[0],
4855 SpiSaveInterruptData
,
4858 /* Nothing - just return (don't forget to release the spinlock */
4859 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4860 DPRINT("ScsiPortDpcForIsr() done\n");
4864 /* If flush of adapters is needed - do it */
4865 if (InterruptData
.Flags
& SCSI_PORT_FLUSH_ADAPTERS
)
4867 /* TODO: Implement */
4871 /* Check for IoMapTransfer */
4872 if (InterruptData
.Flags
& SCSI_PORT_MAP_TRANSFER
)
4874 /* TODO: Implement */
4878 /* Check if timer is needed */
4879 if (InterruptData
.Flags
& SCSI_PORT_TIMER_NEEDED
)
4881 /* Save the timer routine */
4882 DeviceExtension
->HwScsiTimer
= InterruptData
.HwScsiTimer
;
4884 if (InterruptData
.MiniportTimerValue
== 0)
4886 /* Cancel the timer */
4887 KeCancelTimer(&DeviceExtension
->MiniportTimer
);
4891 /* Convert timer value */
4892 TimerValue
.QuadPart
= Int32x32To64(InterruptData
.MiniportTimerValue
, -10);
4895 KeSetTimer(&DeviceExtension
->MiniportTimer
,
4897 &DeviceExtension
->MiniportTimerDpc
);
4901 /* If it's ready for the next request */
4902 if (InterruptData
.Flags
& SCSI_PORT_NEXT_REQUEST_READY
)
4904 /* Check for a duplicate request (NextRequest+NextLuRequest) */
4905 if ((DeviceExtension
->Flags
&
4906 (SCSI_PORT_DEVICE_BUSY
| SCSI_PORT_DISCONNECT_ALLOWED
)) ==
4907 (SCSI_PORT_DEVICE_BUSY
| SCSI_PORT_DISCONNECT_ALLOWED
))
4909 /* Clear busy flag set by ScsiPortStartPacket() */
4910 DeviceExtension
->Flags
&= ~SCSI_PORT_DEVICE_BUSY
;
4912 if (!(InterruptData
.Flags
& SCSI_PORT_RESET
))
4914 /* Ready for next, and no reset is happening */
4915 DeviceExtension
->TimerCount
= -1;
4920 /* Not busy, but not ready for the next request */
4921 DeviceExtension
->Flags
&= ~SCSI_PORT_DEVICE_BUSY
;
4922 InterruptData
.Flags
&= ~SCSI_PORT_NEXT_REQUEST_READY
;
4927 if (InterruptData
.Flags
& SCSI_PORT_RESET_REPORTED
)
4929 /* Hold for a bit */
4930 DeviceExtension
->TimerCount
= 4;
4933 /* Any ready LUN? */
4934 if (InterruptData
.ReadyLun
!= NULL
)
4937 /* Process all LUNs from the list*/
4940 /* Remove it from the list first (as processed) */
4941 LunExtension
= InterruptData
.ReadyLun
;
4942 InterruptData
.ReadyLun
= LunExtension
->ReadyLun
;
4943 LunExtension
->ReadyLun
= NULL
;
4945 /* Get next request for this LUN */
4946 SpiGetNextRequestFromLun(DeviceExtension
, LunExtension
);
4948 /* Still ready requests exist?
4949 If yes - get spinlock, if no - stop here */
4950 if (InterruptData
.ReadyLun
!= NULL
)
4951 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4958 /* Release the spinlock */
4959 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
4962 /* If we ready for next packet, start it */
4963 if (InterruptData
.Flags
& SCSI_PORT_NEXT_REQUEST_READY
)
4964 IoStartNextPacket(DeviceExtension
->DeviceObject
, FALSE
);
4966 NeedToStartIo
= FALSE
;
4968 /* Loop the completed request list */
4969 while (InterruptData
.CompletedRequests
)
4971 /* Remove the request */
4972 SrbInfo
= InterruptData
.CompletedRequests
;
4973 InterruptData
.CompletedRequests
= SrbInfo
->CompletedRequests
;
4974 SrbInfo
->CompletedRequests
= NULL
;
4977 SpiProcessCompletedRequest(DeviceExtension
,
4982 /* Loop abort request list */
4983 while (InterruptData
.CompletedAbort
)
4985 LunExtension
= InterruptData
.CompletedAbort
;
4987 /* Remove the request */
4988 InterruptData
.CompletedAbort
= LunExtension
->CompletedAbortRequests
;
4990 /* Get spinlock since we're going to change flags */
4991 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
4993 /* TODO: Put SrbExtension to the list of free extensions */
4997 /* If we need - call StartIo routine */
5000 /* Make sure CurrentIrp is not null! */
5001 ASSERT(DpcDeviceObject
->CurrentIrp
!= NULL
);
5002 ScsiPortStartIo(DpcDeviceObject
, DpcDeviceObject
->CurrentIrp
);
5005 /* Everything has been done, check */
5006 if (InterruptData
.Flags
& SCSI_PORT_ENABLE_INT_REQUEST
)
5008 /* Synchronize using spinlock */
5009 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
5011 /* Request an interrupt */
5012 DeviceExtension
->HwInterrupt(DeviceExtension
->MiniPortDeviceExtension
);
5014 ASSERT(DeviceExtension
->Flags
& SCSI_PORT_DISABLE_INT_REQUESET
);
5016 /* Should interrupts be enabled again? */
5017 if (DeviceExtension
->Flags
& SCSI_PORT_DISABLE_INT_REQUESET
)
5019 /* Clear this flag */
5020 DeviceExtension
->Flags
&= ~SCSI_PORT_DISABLE_INT_REQUESET
;
5022 /* Call a special routine to do this */
5025 KeSynchronizeExecution(DeviceExtension
->Interrupt
,
5026 SpiEnableInterrupts
,
5031 /* If we need a notification again - loop */
5032 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
5035 /* Release the spinlock */
5036 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
5039 DPRINT("ScsiPortDpcForIsr() done\n");
5044 SpiProcessTimeout(PVOID ServiceContext
)
5046 PDEVICE_OBJECT DeviceObject
= (PDEVICE_OBJECT
)ServiceContext
;
5047 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
= DeviceObject
->DeviceExtension
;
5050 DPRINT("SpiProcessTimeout() entered\n");
5052 DeviceExtension
->TimerCount
= -1;
5054 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_RESET
)
5056 DeviceExtension
->InterruptData
.Flags
&= ~SCSI_PORT_RESET
;
5058 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_RESET_REQUEST
)
5060 DeviceExtension
->InterruptData
.Flags
&= ~SCSI_PORT_RESET_REQUEST
;
5061 ScsiPortStartPacket(ServiceContext
);
5068 DPRINT("Resetting the bus\n");
5070 for (Bus
= 0; Bus
< DeviceExtension
->BusNum
; Bus
++)
5072 DeviceExtension
->HwResetBus(DeviceExtension
->MiniPortDeviceExtension
, Bus
);
5074 /* Reset flags and set reset timeout to 4 seconds */
5075 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_RESET
;
5076 DeviceExtension
->TimerCount
= 4;
5079 /* If miniport requested - request a dpc for it */
5080 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
5081 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
5090 SpiResetBus(PVOID ServiceContext
)
5092 PRESETBUS_PARAMS ResetParams
= (PRESETBUS_PARAMS
)ServiceContext
;
5093 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
5095 /* Perform the bus reset */
5096 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)ResetParams
->DeviceExtension
;
5097 DeviceExtension
->HwResetBus(DeviceExtension
->MiniPortDeviceExtension
,
5098 ResetParams
->PathId
);
5100 /* Set flags and start the timer */
5101 DeviceExtension
->InterruptData
.Flags
|= SCSI_PORT_RESET
;
5102 DeviceExtension
->TimerCount
= 4;
5104 /* If miniport requested - give him a DPC */
5105 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
5106 IoRequestDpc(DeviceExtension
->DeviceObject
, NULL
, NULL
);
5113 // This function handles timeouts and other time delayed processing
5118 // IN PDEVICE_OBJECT DeviceObject Device object registered with timer
5119 // IN PVOID Context the Controller extension for the
5120 // controller the device is on
5123 ScsiPortIoTimer(PDEVICE_OBJECT DeviceObject
,
5126 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
5127 PSCSI_PORT_LUN_EXTENSION LunExtension
;
5131 DPRINT("ScsiPortIoTimer()\n");
5133 DeviceExtension
= (PSCSI_PORT_DEVICE_EXTENSION
)DeviceObject
->DeviceExtension
;
5135 /* Protect with the spinlock */
5136 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
5138 /* Check timeouts */
5139 if (DeviceExtension
->TimerCount
> 0)
5141 /* Decrease the timeout counter */
5142 DeviceExtension
->TimerCount
--;
5144 if (DeviceExtension
->TimerCount
== 0)
5146 /* Timeout, process it */
5147 if (KeSynchronizeExecution(DeviceExtension
->Interrupt
[0],
5149 DeviceExtension
->DeviceObject
))
5151 DPRINT("Error happened during processing timeout, but nothing critical\n");
5155 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
5157 /* We should exit now, since timeout is processed */
5161 /* Per-Lun scanning of timeouts is needed... */
5162 for (Lun
= 0; Lun
< LUS_NUMBER
; Lun
++)
5164 LunExtension
= DeviceExtension
->LunExtensionList
[Lun
];
5166 while (LunExtension
)
5168 if (LunExtension
->Flags
& LUNEX_BUSY
)
5170 if (!(LunExtension
->Flags
&
5171 (LUNEX_NEED_REQUEST_SENSE
| LUNEX_FROZEN_QUEUE
)))
5173 DPRINT("Retrying busy request\n");
5175 /* Clear flags, and retry busy request */
5176 LunExtension
->Flags
&= ~(LUNEX_BUSY
| LUNEX_FULL_QUEUE
);
5177 Irp
= LunExtension
->BusyRequest
;
5179 /* Clearing busy request */
5180 LunExtension
->BusyRequest
= NULL
;
5182 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
5184 IoStartPacket(DeviceObject
, Irp
, (PULONG
)NULL
, NULL
);
5186 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
5189 else if (LunExtension
->RequestTimeout
== 0)
5191 RESETBUS_PARAMS ResetParams
;
5193 LunExtension
->RequestTimeout
= -1;
5195 DPRINT("Request timed out, resetting bus\n");
5197 /* Pass params to the bus reset routine */
5198 ResetParams
.PathId
= LunExtension
->PathId
;
5199 ResetParams
.DeviceExtension
= DeviceExtension
;
5201 if (!KeSynchronizeExecution(DeviceExtension
->Interrupt
[0],
5205 DPRINT1("Reset failed\n");
5208 else if (LunExtension
->RequestTimeout
> 0)
5210 /* Decrement the timeout counter */
5211 LunExtension
->RequestTimeout
--;
5214 LunExtension
= LunExtension
->Next
;
5218 /* Release the spinlock */
5219 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
5222 /**********************************************************************
5227 * Builds the registry device map of all device which are attached
5228 * to the given SCSI HBA port. The device map is located at:
5229 * \Registry\Machine\DeviceMap\Scsi
5239 * Name of registry driver service key.
5246 SpiBuildDeviceMap (PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
5247 PUNICODE_STRING RegistryPath
)
5249 PSCSI_PORT_LUN_EXTENSION LunExtension
;
5250 OBJECT_ATTRIBUTES ObjectAttributes
;
5251 UNICODE_STRING KeyName
;
5252 UNICODE_STRING ValueName
;
5253 WCHAR NameBuffer
[64];
5256 HANDLE ScsiPortKey
= NULL
;
5257 HANDLE ScsiBusKey
= NULL
;
5258 HANDLE ScsiInitiatorKey
= NULL
;
5259 HANDLE ScsiTargetKey
= NULL
;
5260 HANDLE ScsiLunKey
= NULL
;
5263 ULONG CurrentTarget
;
5270 DPRINT("SpiBuildDeviceMap() called\n");
5272 if (DeviceExtension
== NULL
|| RegistryPath
== NULL
)
5274 DPRINT1("Invalid parameter\n");
5275 return(STATUS_INVALID_PARAMETER
);
5278 /* Open or create the 'Scsi' subkey */
5279 RtlInitUnicodeString(&KeyName
,
5280 L
"\\Registry\\Machine\\Hardware\\DeviceMap\\Scsi");
5281 InitializeObjectAttributes(&ObjectAttributes
,
5283 OBJ_CASE_INSENSITIVE
| OBJ_OPENIF
,
5286 Status
= ZwCreateKey(&ScsiKey
,
5291 REG_OPTION_VOLATILE
,
5293 if (!NT_SUCCESS(Status
))
5295 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5299 /* Create new 'Scsi Port X' subkey */
5300 DPRINT("Scsi Port %lu\n",
5301 DeviceExtension
->PortNumber
);
5303 swprintf(NameBuffer
,
5305 DeviceExtension
->PortNumber
);
5306 RtlInitUnicodeString(&KeyName
,
5308 InitializeObjectAttributes(&ObjectAttributes
,
5313 Status
= ZwCreateKey(&ScsiPortKey
,
5318 REG_OPTION_VOLATILE
,
5321 if (!NT_SUCCESS(Status
))
5323 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5328 * Create port-specific values
5331 /* Set 'DMA Enabled' (REG_DWORD) value */
5332 UlongData
= (ULONG
)!DeviceExtension
->PortCapabilities
.AdapterUsesPio
;
5333 DPRINT(" DMA Enabled = %s\n", (UlongData
) ? "TRUE" : "FALSE");
5334 RtlInitUnicodeString(&ValueName
,
5336 Status
= ZwSetValueKey(ScsiPortKey
,
5342 if (!NT_SUCCESS(Status
))
5344 DPRINT("ZwSetValueKey('DMA Enabled') failed (Status %lx)\n", Status
);
5345 ZwClose(ScsiPortKey
);
5349 /* Set 'Driver' (REG_SZ) value */
5350 DriverName
= wcsrchr(RegistryPath
->Buffer
, L
'\\') + 1;
5351 RtlInitUnicodeString(&ValueName
,
5353 Status
= ZwSetValueKey(ScsiPortKey
,
5358 (wcslen(DriverName
) + 1) * sizeof(WCHAR
));
5359 if (!NT_SUCCESS(Status
))
5361 DPRINT("ZwSetValueKey('Driver') failed (Status %lx)\n", Status
);
5362 ZwClose(ScsiPortKey
);
5366 /* Set 'Interrupt' (REG_DWORD) value (NT4 only) */
5367 UlongData
= (ULONG
)DeviceExtension
->PortConfig
->BusInterruptLevel
;
5368 DPRINT(" Interrupt = %lu\n", UlongData
);
5369 RtlInitUnicodeString(&ValueName
,
5371 Status
= ZwSetValueKey(ScsiPortKey
,
5377 if (!NT_SUCCESS(Status
))
5379 DPRINT("ZwSetValueKey('Interrupt') failed (Status %lx)\n", Status
);
5380 ZwClose(ScsiPortKey
);
5384 /* Set 'IOAddress' (REG_DWORD) value (NT4 only) */
5385 UlongData
= ScsiPortConvertPhysicalAddressToUlong((*DeviceExtension
->PortConfig
->AccessRanges
)[0].RangeStart
);
5386 DPRINT(" IOAddress = %lx\n", UlongData
);
5387 RtlInitUnicodeString(&ValueName
,
5389 Status
= ZwSetValueKey(ScsiPortKey
,
5395 if (!NT_SUCCESS(Status
))
5397 DPRINT("ZwSetValueKey('IOAddress') failed (Status %lx)\n", Status
);
5398 ZwClose(ScsiPortKey
);
5402 /* Enumerate buses */
5403 for (BusNumber
= 0; BusNumber
< DeviceExtension
->PortConfig
->NumberOfBuses
; BusNumber
++)
5405 /* Create 'Scsi Bus X' key */
5406 DPRINT(" Scsi Bus %lu\n", BusNumber
);
5407 swprintf(NameBuffer
,
5410 RtlInitUnicodeString(&KeyName
,
5412 InitializeObjectAttributes(&ObjectAttributes
,
5417 Status
= ZwCreateKey(&ScsiBusKey
,
5422 REG_OPTION_VOLATILE
,
5424 if (!NT_SUCCESS(Status
))
5426 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5430 /* Create 'Initiator Id X' key */
5431 DPRINT(" Initiator Id %u\n",
5432 DeviceExtension
->PortConfig
->InitiatorBusId
[BusNumber
]);
5433 swprintf(NameBuffer
,
5435 (unsigned int)(UCHAR
)DeviceExtension
->PortConfig
->InitiatorBusId
[BusNumber
]);
5436 RtlInitUnicodeString(&KeyName
,
5438 InitializeObjectAttributes(&ObjectAttributes
,
5443 Status
= ZwCreateKey(&ScsiInitiatorKey
,
5448 REG_OPTION_VOLATILE
,
5450 if (!NT_SUCCESS(Status
))
5452 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5456 /* FIXME: Are there any initiator values (??) */
5458 ZwClose(ScsiInitiatorKey
);
5459 ScsiInitiatorKey
= NULL
;
5462 /* Enumerate targets */
5463 CurrentTarget
= (ULONG
)-1;
5464 ScsiTargetKey
= NULL
;
5465 for (Target
= 0; Target
< DeviceExtension
->PortConfig
->MaximumNumberOfTargets
; Target
++)
5467 for (Lun
= 0; Lun
< SCSI_MAXIMUM_LOGICAL_UNITS
; Lun
++)
5469 LunExtension
= SpiGetLunExtension(DeviceExtension
,
5473 if (LunExtension
!= NULL
)
5475 if (Target
!= CurrentTarget
)
5477 /* Close old target key */
5478 if (ScsiTargetKey
!= NULL
)
5480 ZwClose(ScsiTargetKey
);
5481 ScsiTargetKey
= NULL
;
5484 /* Create 'Target Id X' key */
5485 DPRINT(" Target Id %lu\n", Target
);
5486 swprintf(NameBuffer
,
5489 RtlInitUnicodeString(&KeyName
,
5491 InitializeObjectAttributes(&ObjectAttributes
,
5496 Status
= ZwCreateKey(&ScsiTargetKey
,
5501 REG_OPTION_VOLATILE
,
5503 if (!NT_SUCCESS(Status
))
5505 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5509 CurrentTarget
= Target
;
5512 /* Create 'Logical Unit Id X' key */
5513 DPRINT(" Logical Unit Id %lu\n", Lun
);
5514 swprintf(NameBuffer
,
5515 L
"Logical Unit Id %lu",
5517 RtlInitUnicodeString(&KeyName
,
5519 InitializeObjectAttributes(&ObjectAttributes
,
5524 Status
= ZwCreateKey(&ScsiLunKey
,
5529 REG_OPTION_VOLATILE
,
5531 if (!NT_SUCCESS(Status
))
5533 DPRINT("ZwCreateKey() failed (Status %lx)\n", Status
);
5537 /* Set 'Identifier' (REG_SZ) value */
5538 swprintf(NameBuffer
,
5540 LunExtension
->InquiryData
.VendorId
,
5541 LunExtension
->InquiryData
.ProductId
,
5542 LunExtension
->InquiryData
.ProductRevisionLevel
);
5543 DPRINT(" Identifier = '%S'\n", NameBuffer
);
5544 RtlInitUnicodeString(&ValueName
,
5546 Status
= ZwSetValueKey(ScsiLunKey
,
5551 (wcslen(NameBuffer
) + 1) * sizeof(WCHAR
));
5552 if (!NT_SUCCESS(Status
))
5554 DPRINT("ZwSetValueKey('Identifier') failed (Status %lx)\n", Status
);
5558 /* Set 'Type' (REG_SZ) value */
5559 switch (LunExtension
->InquiryData
.DeviceType
)
5562 TypeName
= L
"DiskPeripheral";
5565 TypeName
= L
"TapePeripheral";
5568 TypeName
= L
"PrinterPeripheral";
5571 TypeName
= L
"WormPeripheral";
5574 TypeName
= L
"CdRomPeripheral";
5577 TypeName
= L
"ScannerPeripheral";
5580 TypeName
= L
"OpticalDiskPeripheral";
5583 TypeName
= L
"MediumChangerPeripheral";
5586 TypeName
= L
"CommunicationPeripheral";
5589 TypeName
= L
"OtherPeripheral";
5592 DPRINT(" Type = '%S'\n", TypeName
);
5593 RtlInitUnicodeString(&ValueName
,
5595 Status
= ZwSetValueKey(ScsiLunKey
,
5600 (wcslen(TypeName
) + 1) * sizeof(WCHAR
));
5601 if (!NT_SUCCESS(Status
))
5603 DPRINT("ZwSetValueKey('Type') failed (Status %lx)\n", Status
);
5607 ZwClose(ScsiLunKey
);
5612 /* Close old target key */
5613 if (ScsiTargetKey
!= NULL
)
5615 ZwClose(ScsiTargetKey
);
5616 ScsiTargetKey
= NULL
;
5620 ZwClose(ScsiBusKey
);
5625 if (ScsiLunKey
!= NULL
)
5626 ZwClose (ScsiLunKey
);
5628 if (ScsiInitiatorKey
!= NULL
)
5629 ZwClose (ScsiInitiatorKey
);
5631 if (ScsiTargetKey
!= NULL
)
5632 ZwClose (ScsiTargetKey
);
5634 if (ScsiBusKey
!= NULL
)
5635 ZwClose (ScsiBusKey
);
5637 if (ScsiPortKey
!= NULL
)
5638 ZwClose (ScsiPortKey
);
5640 DPRINT("SpiBuildDeviceMap() done\n");
5647 SpiMiniportTimerDpc(IN
struct _KDPC
*Dpc
,
5648 IN PVOID DeviceObject
,
5649 IN PVOID SystemArgument1
,
5650 IN PVOID SystemArgument2
)
5652 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
;
5654 DPRINT("Miniport timer DPC\n");
5656 DeviceExtension
= ((PDEVICE_OBJECT
)DeviceObject
)->DeviceExtension
;
5658 /* Acquire the spinlock */
5659 KeAcquireSpinLockAtDpcLevel(&DeviceExtension
->SpinLock
);
5661 /* Call the timer routine */
5662 if (DeviceExtension
->HwScsiTimer
!= NULL
)
5664 DeviceExtension
->HwScsiTimer(&DeviceExtension
->MiniPortDeviceExtension
);
5667 /* Release the spinlock */
5668 KeReleaseSpinLockFromDpcLevel(&DeviceExtension
->SpinLock
);
5670 if (DeviceExtension
->InterruptData
.Flags
& SCSI_PORT_NOTIFICATION_NEEDED
)
5672 ScsiPortDpcForIsr(NULL
,
5673 DeviceExtension
->DeviceObject
,
5680 SpiCreatePortConfig(PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
5681 PHW_INITIALIZATION_DATA HwInitData
,
5682 PCONFIGURATION_INFO InternalConfigInfo
,
5683 PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
5686 UNICODE_STRING UnicodeString
;
5687 OBJECT_ATTRIBUTES ObjectAttributes
;
5688 PCONFIGURATION_INFORMATION DdkConfigInformation
;
5689 HANDLE RootKey
, Key
;
5691 WCHAR DeviceBuffer
[16];
5692 WCHAR StrBuffer
[512];
5696 /* Zero out the struct if told so */
5699 /* First zero the portconfig */
5700 RtlZeroMemory(ConfigInfo
, sizeof(PORT_CONFIGURATION_INFORMATION
));
5702 /* Then access ranges */
5703 RtlZeroMemory(InternalConfigInfo
->AccessRanges
,
5704 HwInitData
->NumberOfAccessRanges
* sizeof(ACCESS_RANGE
));
5706 /* Initialize the struct */
5707 ConfigInfo
->Length
= sizeof(PORT_CONFIGURATION_INFORMATION
);
5708 ConfigInfo
->AdapterInterfaceType
= HwInitData
->AdapterInterfaceType
;
5709 ConfigInfo
->InterruptMode
= Latched
;
5710 ConfigInfo
->DmaChannel
= SP_UNINITIALIZED_VALUE
;
5711 ConfigInfo
->DmaPort
= SP_UNINITIALIZED_VALUE
;
5712 ConfigInfo
->DmaChannel2
= SP_UNINITIALIZED_VALUE
;
5713 ConfigInfo
->DmaPort2
= SP_UNINITIALIZED_VALUE
;
5714 ConfigInfo
->MaximumTransferLength
= SP_UNINITIALIZED_VALUE
;
5715 ConfigInfo
->NumberOfAccessRanges
= HwInitData
->NumberOfAccessRanges
;
5716 ConfigInfo
->MaximumNumberOfTargets
= 8;
5718 /* Store parameters */
5719 ConfigInfo
->NeedPhysicalAddresses
= HwInitData
->NeedPhysicalAddresses
;
5720 ConfigInfo
->MapBuffers
= HwInitData
->MapBuffers
;
5721 ConfigInfo
->AutoRequestSense
= HwInitData
->AutoRequestSense
;
5722 ConfigInfo
->ReceiveEvent
= HwInitData
->ReceiveEvent
;
5723 ConfigInfo
->TaggedQueuing
= HwInitData
->TaggedQueuing
;
5724 ConfigInfo
->MultipleRequestPerLu
= HwInitData
->MultipleRequestPerLu
;
5726 /* Get the disk usage */
5727 DdkConfigInformation
= IoGetConfigurationInformation();
5728 ConfigInfo
->AtdiskPrimaryClaimed
= DdkConfigInformation
->AtDiskPrimaryAddressClaimed
;
5729 ConfigInfo
->AtdiskSecondaryClaimed
= DdkConfigInformation
->AtDiskSecondaryAddressClaimed
;
5731 /* Initiator bus id is not set */
5732 for (Bus
= 0; Bus
< 8; Bus
++)
5733 ConfigInfo
->InitiatorBusId
[Bus
] = (CCHAR
)SP_UNINITIALIZED_VALUE
;
5736 ConfigInfo
->NumberOfPhysicalBreaks
= 17;
5738 /* Clear this information */
5739 InternalConfigInfo
->DisableTaggedQueueing
= FALSE
;
5740 InternalConfigInfo
->DisableMultipleLun
= FALSE
;
5742 /* Store Bus Number */
5743 ConfigInfo
->SystemIoBusNumber
= InternalConfigInfo
->BusNumber
;
5747 if (ConfigInfo
->AdapterInterfaceType
== Internal
)
5749 /* Open registry key for HW database */
5750 InitializeObjectAttributes(&ObjectAttributes
,
5751 DeviceExtension
->DeviceObject
->DriverObject
->HardwareDatabase
,
5752 OBJ_CASE_INSENSITIVE
,
5756 Status
= ZwOpenKey(&RootKey
,
5760 if (NT_SUCCESS(Status
))
5762 /* Create name for it */
5763 swprintf(StrBuffer
, L
"ScsiAdapter\\%lu",
5764 InternalConfigInfo
->AdapterNumber
);
5766 RtlInitUnicodeString(&UnicodeString
, StrBuffer
);
5768 /* Open device key */
5769 InitializeObjectAttributes(&ObjectAttributes
,
5771 OBJ_CASE_INSENSITIVE
,
5775 Status
= ZwOpenKey(&Key
,
5781 if (NT_SUCCESS(Status
))
5783 if (InternalConfigInfo
->LastAdapterNumber
!= InternalConfigInfo
->AdapterNumber
)
5785 DPRINT("Hardware info found at %S\n", StrBuffer
);
5788 SpiParseDeviceInfo(DeviceExtension
,
5794 InternalConfigInfo
->BusNumber
= 0;
5798 /* Try the next adapter */
5799 InternalConfigInfo
->AdapterNumber
++;
5805 /* Info was not found, exit */
5806 DPRINT1("ZwOpenKey() failed with Status=0x%08X\n", Status
);
5807 return STATUS_DEVICE_DOES_NOT_EXIST
;
5812 DPRINT1("ZwOpenKey() failed with Status=0x%08X\n", Status
);
5816 /* Look at device params */
5818 if (InternalConfigInfo
->Parameter
)
5820 ExFreePool(InternalConfigInfo
->Parameter
);
5821 InternalConfigInfo
->Parameter
= NULL
;
5824 if (InternalConfigInfo
->ServiceKey
!= NULL
)
5826 swprintf(DeviceBuffer
, L
"Device%lu", InternalConfigInfo
->AdapterNumber
);
5827 RtlInitUnicodeString(&UnicodeString
, DeviceBuffer
);
5829 /* Open the service key */
5830 InitializeObjectAttributes(&ObjectAttributes
,
5832 OBJ_CASE_INSENSITIVE
,
5833 InternalConfigInfo
->ServiceKey
,
5836 Status
= ZwOpenKey(&Key
,
5841 /* Parse device key */
5842 if (InternalConfigInfo
->DeviceKey
!= NULL
)
5844 SpiParseDeviceInfo(DeviceExtension
,
5845 InternalConfigInfo
->DeviceKey
,
5851 /* Then parse hw info */
5854 if (InternalConfigInfo
->LastAdapterNumber
!= InternalConfigInfo
->AdapterNumber
)
5856 SpiParseDeviceInfo(DeviceExtension
,
5867 /* Adapter not found, go try the next one */
5868 InternalConfigInfo
->AdapterNumber
++;
5877 /* Update the last adapter number */
5878 InternalConfigInfo
->LastAdapterNumber
= InternalConfigInfo
->AdapterNumber
;
5880 /* Do we have this kind of bus at all? */
5882 Status
= IoQueryDeviceDescription(&HwInitData
->AdapterInterfaceType
,
5883 &InternalConfigInfo
->BusNumber
,
5888 SpQueryDeviceCallout
,
5891 /* This bus was not found */
5894 INTERFACE_TYPE InterfaceType
= Eisa
;
5896 /* Check for EISA */
5897 if (HwInitData
->AdapterInterfaceType
== Isa
)
5899 Status
= IoQueryDeviceDescription(&InterfaceType
,
5900 &InternalConfigInfo
->BusNumber
,
5905 SpQueryDeviceCallout
,
5908 /* Return respectively */
5910 return STATUS_SUCCESS
;
5912 return STATUS_DEVICE_DOES_NOT_EXIST
;
5916 return STATUS_DEVICE_DOES_NOT_EXIST
;
5921 return STATUS_SUCCESS
;
5926 SpiParseDeviceInfo(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
,
5928 IN PPORT_CONFIGURATION_INFORMATION ConfigInfo
,
5929 IN PCONFIGURATION_INFO InternalConfigInfo
,
5932 PKEY_VALUE_FULL_INFORMATION KeyValueInformation
;
5933 PCM_FULL_RESOURCE_DESCRIPTOR FullResource
;
5934 PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor
;
5935 PCM_SCSI_DEVICE_DATA ScsiDeviceData
;
5936 ULONG Length
, Count
, Dma
= 0, Interrupt
= 0;
5937 ULONG Index
= 0, RangeCount
= 0;
5938 UNICODE_STRING UnicodeString
;
5939 ANSI_STRING AnsiString
;
5940 NTSTATUS Status
= STATUS_SUCCESS
;
5943 KeyValueInformation
= (PKEY_VALUE_FULL_INFORMATION
) Buffer
;
5945 /* Loop through all values in the device node */
5948 Status
= ZwEnumerateValueKey(Key
,
5950 KeyValueFullInformation
,
5955 if (!NT_SUCCESS(Status
))
5960 /* Length for DWORD is ok? */
5961 if (KeyValueInformation
->Type
== REG_DWORD
&&
5962 KeyValueInformation
->DataLength
!= sizeof(ULONG
))
5967 /* Get MaximumLogicalUnit */
5968 if (_wcsnicmp(KeyValueInformation
->Name
, L
"MaximumLogicalUnit",
5969 KeyValueInformation
->NameLength
/2) == 0)
5972 if (KeyValueInformation
->Type
!= REG_DWORD
)
5974 DPRINT("Bad data type for MaximumLogicalUnit\n");
5978 DeviceExtension
->MaxLunCount
= *((PUCHAR
)
5979 (Buffer
+ KeyValueInformation
->DataOffset
));
5981 /* Check / reset if needed */
5982 if (DeviceExtension
->MaxLunCount
> SCSI_MAXIMUM_LOGICAL_UNITS
)
5983 DeviceExtension
->MaxLunCount
= SCSI_MAXIMUM_LOGICAL_UNITS
;
5985 DPRINT("MaximumLogicalUnit = %d\n", DeviceExtension
->MaxLunCount
);
5988 /* Get InitiatorTargetId */
5989 if (_wcsnicmp(KeyValueInformation
->Name
, L
"InitiatorTargetId",
5990 KeyValueInformation
->NameLength
/ 2) == 0)
5993 if (KeyValueInformation
->Type
!= REG_DWORD
)
5995 DPRINT("Bad data type for InitiatorTargetId\n");
5999 ConfigInfo
->InitiatorBusId
[0] = *((PUCHAR
)
6000 (Buffer
+ KeyValueInformation
->DataOffset
));
6002 /* Check / reset if needed */
6003 if (ConfigInfo
->InitiatorBusId
[0] > ConfigInfo
->MaximumNumberOfTargets
- 1)
6004 ConfigInfo
->InitiatorBusId
[0] = (CCHAR
)-1;
6006 DPRINT("InitiatorTargetId = %d\n", ConfigInfo
->InitiatorBusId
[0]);
6010 if (_wcsnicmp(KeyValueInformation
->Name
, L
"ScsiDebug",
6011 KeyValueInformation
->NameLength
/2) == 0)
6013 DPRINT("ScsiDebug key not supported\n");
6016 /* Check for a breakpoint */
6017 if (_wcsnicmp(KeyValueInformation
->Name
, L
"BreakPointOnEntry",
6018 KeyValueInformation
->NameLength
/2) == 0)
6020 DPRINT1("Breakpoint on entry requested!\n");
6024 /* Get DisableSynchronousTransfers */
6025 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DisableSynchronousTransfers",
6026 KeyValueInformation
->NameLength
/2) == 0)
6028 DeviceExtension
->SrbFlags
|= SRB_FLAGS_DISABLE_SYNCH_TRANSFER
;
6029 DPRINT("Synch transfers disabled\n");
6032 /* Get DisableDisconnects */
6033 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DisableDisconnects",
6034 KeyValueInformation
->NameLength
/2) == 0)
6036 DeviceExtension
->SrbFlags
|= SRB_FLAGS_DISABLE_DISCONNECT
;
6037 DPRINT("Disconnects disabled\n");
6040 /* Get DisableTaggedQueuing */
6041 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DisableTaggedQueuing",
6042 KeyValueInformation
->NameLength
/2) == 0)
6044 InternalConfigInfo
->DisableTaggedQueueing
= TRUE
;
6045 DPRINT("Tagged queueing disabled\n");
6048 /* Get DisableMultipleRequests */
6049 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DisableMultipleRequests",
6050 KeyValueInformation
->NameLength
/2) == 0)
6052 InternalConfigInfo
->DisableMultipleLun
= TRUE
;
6053 DPRINT("Multiple requests disabled\n");
6056 /* Get DriverParameters */
6057 if (_wcsnicmp(KeyValueInformation
->Name
, L
"DriverParameters",
6058 KeyValueInformation
->NameLength
/2) == 0)
6060 /* Skip if nothing */
6061 if (KeyValueInformation
->DataLength
== 0)
6064 /* If there was something previously allocated - free it */
6065 if (InternalConfigInfo
->Parameter
!= NULL
)
6066 ExFreePool(InternalConfigInfo
->Parameter
);
6069 InternalConfigInfo
->Parameter
= ExAllocatePoolWithTag(NonPagedPool
,
6070 KeyValueInformation
->DataLength
, TAG_SCSIPORT
);
6072 if (InternalConfigInfo
->Parameter
!= NULL
)
6074 if (KeyValueInformation
->Type
!= REG_SZ
)
6078 InternalConfigInfo
->Parameter
,
6079 (PCCHAR
)KeyValueInformation
+ KeyValueInformation
->DataOffset
,
6080 KeyValueInformation
->DataLength
);
6084 /* If it's a unicode string, convert it to ansi */
6085 UnicodeString
.Length
= (USHORT
)KeyValueInformation
->DataLength
;
6086 UnicodeString
.MaximumLength
= (USHORT
)KeyValueInformation
->DataLength
;
6087 UnicodeString
.Buffer
=
6088 (PWSTR
)((PCCHAR
)KeyValueInformation
+ KeyValueInformation
->DataOffset
);
6090 AnsiString
.Length
= 0;
6091 AnsiString
.MaximumLength
= (USHORT
)KeyValueInformation
->DataLength
;
6092 AnsiString
.Buffer
= (PCHAR
)InternalConfigInfo
->Parameter
;
6094 Status
= RtlUnicodeStringToAnsiString(&AnsiString
,
6098 /* In case of error, free the allocated space */
6099 if (!NT_SUCCESS(Status
))
6101 ExFreePool(InternalConfigInfo
->Parameter
);
6102 InternalConfigInfo
->Parameter
= NULL
;
6108 DPRINT("Found driver parameter\n");
6111 /* Get MaximumSGList */
6112 if (_wcsnicmp(KeyValueInformation
->Name
, L
"MaximumSGList",
6113 KeyValueInformation
->NameLength
/2) == 0)
6115 if (KeyValueInformation
->Type
!= REG_DWORD
)
6117 DPRINT("Bad data type for MaximumSGList\n");
6121 ConfigInfo
->NumberOfPhysicalBreaks
= *((PUCHAR
)(Buffer
+ KeyValueInformation
->DataOffset
));
6124 if (ConfigInfo
->NumberOfPhysicalBreaks
> SCSI_MAXIMUM_PHYSICAL_BREAKS
)
6126 ConfigInfo
->NumberOfPhysicalBreaks
= SCSI_MAXIMUM_PHYSICAL_BREAKS
;
6128 else if (ConfigInfo
->NumberOfPhysicalBreaks
< SCSI_MINIMUM_PHYSICAL_BREAKS
)
6130 ConfigInfo
->NumberOfPhysicalBreaks
= SCSI_MINIMUM_PHYSICAL_BREAKS
;
6133 DPRINT("MaximumSGList = %d\n", ConfigInfo
->NumberOfPhysicalBreaks
);
6136 /* Get NumberOfRequests */
6137 if (_wcsnicmp(KeyValueInformation
->Name
, L
"NumberOfRequests",
6138 KeyValueInformation
->NameLength
/2) == 0)
6140 if (KeyValueInformation
->Type
!= REG_DWORD
)
6142 DPRINT("NumberOfRequests has wrong data type\n");
6146 DeviceExtension
->RequestsNumber
= *((PUCHAR
)(Buffer
+ KeyValueInformation
->DataOffset
));
6149 if (DeviceExtension
->RequestsNumber
< 16)
6151 DeviceExtension
->RequestsNumber
= 16;
6153 else if (DeviceExtension
->RequestsNumber
> 512)
6155 DeviceExtension
->RequestsNumber
= 512;
6158 DPRINT("Number Of Requests = %d\n", DeviceExtension
->RequestsNumber
);
6161 /* Get resource list */
6162 if (_wcsnicmp(KeyValueInformation
->Name
, L
"ResourceList",
6163 KeyValueInformation
->NameLength
/2) == 0 ||
6164 _wcsnicmp(KeyValueInformation
->Name
, L
"Configuration Data",
6165 KeyValueInformation
->NameLength
/2) == 0 )
6167 if (KeyValueInformation
->Type
!= REG_FULL_RESOURCE_DESCRIPTOR
||
6168 KeyValueInformation
->DataLength
< sizeof(REG_FULL_RESOURCE_DESCRIPTOR
))
6170 DPRINT("Bad data type for ResourceList\n");
6175 DPRINT("Found ResourceList\n");
6178 FullResource
= (PCM_FULL_RESOURCE_DESCRIPTOR
)(Buffer
+ KeyValueInformation
->DataOffset
);
6180 /* Copy some info from it */
6181 InternalConfigInfo
->BusNumber
= FullResource
->BusNumber
;
6182 ConfigInfo
->SystemIoBusNumber
= FullResource
->BusNumber
;
6184 /* Loop through it */
6185 for (Count
= 0; Count
< FullResource
->PartialResourceList
.Count
; Count
++)
6187 /* Get partial descriptor */
6189 &FullResource
->PartialResourceList
.PartialDescriptors
[Count
];
6191 /* Check datalength */
6192 if ((ULONG
)((PCHAR
)(PartialDescriptor
+ 1) -
6193 (PCHAR
)FullResource
) > KeyValueInformation
->DataLength
)
6195 DPRINT("Resource data is of incorrect size\n");
6199 switch (PartialDescriptor
->Type
)
6201 case CmResourceTypePort
:
6202 if (RangeCount
>= ConfigInfo
->NumberOfAccessRanges
)
6204 DPRINT("Too many access ranges\n");
6208 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeInMemory
= FALSE
;
6209 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeStart
= PartialDescriptor
->u
.Port
.Start
;
6210 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeLength
= PartialDescriptor
->u
.Port
.Length
;
6215 case CmResourceTypeMemory
:
6216 if (RangeCount
>= ConfigInfo
->NumberOfAccessRanges
)
6218 DPRINT("Too many access ranges\n");
6222 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeInMemory
= TRUE
;
6223 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeStart
= PartialDescriptor
->u
.Memory
.Start
;
6224 InternalConfigInfo
->AccessRanges
[RangeCount
].RangeLength
= PartialDescriptor
->u
.Memory
.Length
;
6229 case CmResourceTypeInterrupt
:
6233 ConfigInfo
->BusInterruptLevel
=
6234 PartialDescriptor
->u
.Interrupt
.Level
;
6236 ConfigInfo
->BusInterruptVector
=
6237 PartialDescriptor
->u
.Interrupt
.Vector
;
6239 ConfigInfo
->InterruptMode
= (PartialDescriptor
->Flags
& CM_RESOURCE_INTERRUPT_LATCHED
) ? Latched
: LevelSensitive
;
6241 else if (Interrupt
== 1)
6243 ConfigInfo
->BusInterruptLevel2
=
6244 PartialDescriptor
->u
.Interrupt
.Level
;
6246 ConfigInfo
->BusInterruptVector2
=
6247 PartialDescriptor
->u
.Interrupt
.Vector
;
6249 ConfigInfo
->InterruptMode2
= (PartialDescriptor
->Flags
& CM_RESOURCE_INTERRUPT_LATCHED
) ? Latched
: LevelSensitive
;
6255 case CmResourceTypeDma
:
6259 ConfigInfo
->DmaChannel
= PartialDescriptor
->u
.Dma
.Channel
;
6260 ConfigInfo
->DmaPort
= PartialDescriptor
->u
.Dma
.Port
;
6262 if (PartialDescriptor
->Flags
& CM_RESOURCE_DMA_8
)
6263 ConfigInfo
->DmaWidth
= Width8Bits
;
6264 else if ((PartialDescriptor
->Flags
& CM_RESOURCE_DMA_16
) ||
6265 (PartialDescriptor
->Flags
& CM_RESOURCE_DMA_8_AND_16
)) //???
6266 ConfigInfo
->DmaWidth
= Width16Bits
;
6267 else if (PartialDescriptor
->Flags
& CM_RESOURCE_DMA_32
)
6268 ConfigInfo
->DmaWidth
= Width32Bits
;
6272 ConfigInfo
->DmaChannel2
= PartialDescriptor
->u
.Dma
.Channel
;
6273 ConfigInfo
->DmaPort2
= PartialDescriptor
->u
.Dma
.Port
;
6275 if (PartialDescriptor
->Flags
& CM_RESOURCE_DMA_8
)
6276 ConfigInfo
->DmaWidth2
= Width8Bits
;
6277 else if ((PartialDescriptor
->Flags
& CM_RESOURCE_DMA_16
) ||
6278 (PartialDescriptor
->Flags
& CM_RESOURCE_DMA_8_AND_16
)) //???
6279 ConfigInfo
->DmaWidth2
= Width16Bits
;
6280 else if (PartialDescriptor
->Flags
& CM_RESOURCE_DMA_32
)
6281 ConfigInfo
->DmaWidth2
= Width32Bits
;
6287 case CmResourceTypeDeviceSpecific
:
6288 if (PartialDescriptor
->u
.DeviceSpecificData
.DataSize
<
6289 sizeof(CM_SCSI_DEVICE_DATA
) ||
6290 (PCHAR
) (PartialDescriptor
+ 1) - (PCHAR
)FullResource
+
6291 PartialDescriptor
->u
.DeviceSpecificData
.DataSize
>
6292 KeyValueInformation
->DataLength
)
6294 DPRINT("Resource data length is incorrect");
6298 /* Set only one field from it */
6299 ScsiDeviceData
= (PCM_SCSI_DEVICE_DATA
) (PartialDescriptor
+1);
6300 ConfigInfo
->InitiatorBusId
[0] = ScsiDeviceData
->HostIdentifier
;
6310 SpQueryDeviceCallout(IN PVOID Context
,
6311 IN PUNICODE_STRING PathName
,
6312 IN INTERFACE_TYPE BusType
,
6314 IN PKEY_VALUE_FULL_INFORMATION
*BusInformation
,
6315 IN CONFIGURATION_TYPE ControllerType
,
6316 IN ULONG ControllerNumber
,
6317 IN PKEY_VALUE_FULL_INFORMATION
*ControllerInformation
,
6318 IN CONFIGURATION_TYPE PeripheralType
,
6319 IN ULONG PeripheralNumber
,
6320 IN PKEY_VALUE_FULL_INFORMATION
*PeripheralInformation
)
6322 PBOOLEAN Found
= (PBOOLEAN
)Context
;
6323 /* We just set our Found variable to TRUE */
6326 return STATUS_SUCCESS
;
6329 IO_ALLOCATION_ACTION
6331 ScsiPortAllocateAdapterChannel(IN PDEVICE_OBJECT DeviceObject
,
6333 IN PVOID MapRegisterBase
,
6337 PSCSI_PORT_DEVICE_EXTENSION DeviceExtension
= DeviceObject
->DeviceExtension
;
6339 /* Guard access with the spinlock */
6340 KeAcquireSpinLock(&DeviceExtension
->SpinLock
, &Irql
);
6342 /* Save MapRegisterBase we've got here */
6343 DeviceExtension
->MapRegisterBase
= MapRegisterBase
;
6345 /* Start pending request */
6346 KeSynchronizeExecution(DeviceExtension
->Interrupt
[0],
6347 ScsiPortStartPacket
, DeviceObject
);
6349 /* Release spinlock we took */
6350 KeReleaseSpinLock(&DeviceExtension
->SpinLock
, Irql
);
6357 SpiStatusSrbToNt(UCHAR SrbStatus
)
6359 switch (SRB_STATUS(SrbStatus
))
6361 case SRB_STATUS_TIMEOUT
:
6362 case SRB_STATUS_COMMAND_TIMEOUT
:
6363 return STATUS_IO_TIMEOUT
;
6365 case SRB_STATUS_BAD_SRB_BLOCK_LENGTH
:
6366 case SRB_STATUS_BAD_FUNCTION
:
6367 return STATUS_INVALID_DEVICE_REQUEST
;
6369 case SRB_STATUS_NO_DEVICE
:
6370 case SRB_STATUS_INVALID_LUN
:
6371 case SRB_STATUS_INVALID_TARGET_ID
:
6372 case SRB_STATUS_NO_HBA
:
6373 return STATUS_DEVICE_DOES_NOT_EXIST
;
6375 case SRB_STATUS_DATA_OVERRUN
:
6376 return STATUS_BUFFER_OVERFLOW
;
6378 case SRB_STATUS_SELECTION_TIMEOUT
:
6379 return STATUS_DEVICE_NOT_CONNECTED
;
6382 return STATUS_IO_DEVICE_ERROR
;
6385 return STATUS_IO_DEVICE_ERROR
;
6389 #undef ScsiPortConvertPhysicalAddressToUlong
6394 ScsiPortConvertPhysicalAddressToUlong(IN SCSI_PHYSICAL_ADDRESS Address
)
6396 DPRINT("ScsiPortConvertPhysicalAddressToUlong()\n");
6397 return(Address
.u
.LowPart
);