[SCSIPORT]
[reactos.git] / reactos / drivers / storage / scsiport / scsiport.c
index 5b360f0..0db7e81 100644 (file)
 
 #include "precomp.h"
 
-#ifndef NDEBUG
+#include <ntddk.h>
+#include <stdio.h>
+#include <scsi.h>
+#include <ntddscsi.h>
+#include <ntdddisk.h>
+
 #define NDEBUG
-#endif
 #include <debug.h>
 
+#include "scsiport_int.h"
+
 ULONG InternalDebugLevel = 0x00;
 
 #undef ScsiPortMoveMemory
 
-/* TYPES *********************************************************************/
-
 /* GLOBALS *******************************************************************/
 
 static BOOLEAN
@@ -580,37 +584,35 @@ ScsiPortGetLogicalUnit(IN PVOID HwDeviceExtension,
                       IN UCHAR TargetId,
                       IN UCHAR Lun)
 {
-    UNIMPLEMENTED;
-#if 0
-  PSCSI_PORT_DEVICE_EXTENSION DeviceExtension;
-  PSCSI_PORT_LUN_EXTENSION LunExtension;
-  PLIST_ENTRY Entry;
+    PSCSI_PORT_DEVICE_EXTENSION DeviceExtension;
+    PSCSI_PORT_LUN_EXTENSION LunExtension;
 
-  DPRINT("ScsiPortGetLogicalUnit() called\n");
+    DPRINT("ScsiPortGetLogicalUnit() called\n");
 
-  DeviceExtension = CONTAINING_RECORD(HwDeviceExtension,
-                                     SCSI_PORT_DEVICE_EXTENSION,
-                                     MiniPortDeviceExtension);
-  if (IsListEmpty(&DeviceExtension->LunExtensionListHead))
-    return NULL;
+    DeviceExtension = CONTAINING_RECORD(HwDeviceExtension,
+                                        SCSI_PORT_DEVICE_EXTENSION,
+                                        MiniPortDeviceExtension);
 
-  Entry = DeviceExtension->LunExtensionListHead.Flink;
-  while (Entry != &DeviceExtension->LunExtensionListHead)
+    /* Check the extension size */
+    if (!DeviceExtension->LunExtensionSize)
     {
-      LunExtension = CONTAINING_RECORD(Entry,
-                                      SCSI_PORT_LUN_EXTENSION,
-                                      List);
-      if (LunExtension->PathId == PathId &&
-         LunExtension->TargetId == TargetId &&
-         LunExtension->Lun == Lun)
-       {
-         return (PVOID)&LunExtension->MiniportLunExtension;
-       }
+        /* They didn't want one */
+        return NULL;
+    }
 
-      Entry = Entry->Flink;
+    LunExtension = SpiGetLunExtension(DeviceExtension,
+                                      PathId,
+                                      TargetId,
+                                      Lun);
+    /* Check that the logical unit exists */
+    if (!LunExtension)
+    {
+        /* Nope, return NULL */
+        return NULL;
     }
-#endif
-  return NULL;
+
+    /* Return the logical unit miniport extension */
+    return (LunExtension + 1);
 }
 
 
@@ -625,8 +627,8 @@ ScsiPortGetPhysicalAddress(IN PVOID HwDeviceExtension,
 {
     PSCSI_PORT_DEVICE_EXTENSION DeviceExtension;
     SCSI_PHYSICAL_ADDRESS PhysicalAddress;
-    ULONG BufferLength = 0;
-    ULONG Offset;
+    SIZE_T BufferLength = 0;
+    ULONG_PTR Offset;
     PSCSI_SG_ADDRESS SGList;
     PSCSI_REQUEST_BLOCK_INFO SrbInfo;
 
@@ -675,7 +677,7 @@ ScsiPortGetPhysicalAddress(IN PVOID HwDeviceExtension,
         PhysicalAddress.QuadPart = (LONGLONG)(SP_UNINITIALIZED_VALUE);
     }
 
-    *Length = BufferLength;
+    *Length = (ULONG)BufferLength;
     return PhysicalAddress;
 }
 
@@ -1036,6 +1038,7 @@ ScsiPortInitialize(IN PVOID Argument1,
     KIRQL OldIrql;
     PCM_RESOURCE_LIST ResourceList;
     BOOLEAN Conflict;
+    SIZE_T BusConfigSize;
 
 
     DPRINT ("ScsiPortInitialize() called!\n");
@@ -1637,10 +1640,11 @@ CreatePortConfig:
       IoStartTimer(PortDeviceObject);
 
       /* Initialize bus scanning information */
+      BusConfigSize = FIELD_OFFSET(BUSES_CONFIGURATION_INFORMATION,
+                                   BusScanInfo[DeviceExtension->PortConfig->NumberOfBuses]);
       DeviceExtension->BusesConfig = ExAllocatePoolWithTag(PagedPool,
-          sizeof(PVOID) * DeviceExtension->PortConfig->NumberOfBuses
-          + sizeof(ULONG), TAG_SCSIPORT);
-
+                                                           BusConfigSize,
+                                                           TAG_SCSIPORT);
       if (!DeviceExtension->BusesConfig)
       {
           DPRINT1("Out of resources!\n");
@@ -1649,9 +1653,7 @@ CreatePortConfig:
       }
 
       /* Zero it */
-      RtlZeroMemory(DeviceExtension->BusesConfig,
-          sizeof(PVOID) * DeviceExtension->PortConfig->NumberOfBuses
-          + sizeof(ULONG));
+      RtlZeroMemory(DeviceExtension->BusesConfig, BusConfigSize);
 
       /* Store number of buses there */
       DeviceExtension->BusesConfig->NumberOfBuses = (UCHAR)DeviceExtension->BusNum;
@@ -1838,6 +1840,8 @@ ScsiPortLogError(IN PVOID HwDeviceExtension,
   //PSCSI_PORT_DEVICE_EXTENSION DeviceExtension;
 
   DPRINT1("ScsiPortLogError() called\n");
+  DPRINT1("PathId: 0x%02x  TargetId: 0x%02x  Lun: 0x%02x  ErrorCode: 0x%08lx  UniqueId: 0x%08lx\n",
+          PathId, TargetId, Lun, ErrorCode, UniqueId);
 
   //DeviceExtension = CONTAINING_RECORD(HwDeviceExtension, SCSI_PORT_DEVICE_EXTENSION, MiniPortDeviceExtension);
 
@@ -2352,10 +2356,13 @@ SpiGetPciConfigData(IN PDRIVER_OBJECT DriverObject,
             if (DataSize == 0)
                 return FALSE;
 
-            /* If result is PCI_INVALID_VENDORID, then this device has no more
-               "Functions" */
-            if (PciConfig.VendorID == PCI_INVALID_VENDORID)
-                break;
+            /* Check if result is PCI_INVALID_VENDORID or too small */
+            if ((DataSize < sizeof(ULONG)) ||
+                (PciConfig.VendorID == PCI_INVALID_VENDORID))
+            {
+                /* Continue to try the next function */
+                continue;
+            }
 
             sprintf (VendorIdString, "%04hx", PciConfig.VendorID);
             sprintf (DeviceIdString, "%04hx", PciConfig.DeviceID);
@@ -2621,6 +2628,7 @@ ScsiPortDispatchScsi(IN PDEVICE_OBJECT DeviceObject,
 
     case SRB_FUNCTION_EXECUTE_SCSI:
     case SRB_FUNCTION_IO_CONTROL:
+        DPRINT("  SRB_FUNCTION_EXECUTE_SCSI or SRB_FUNCTION_IO_CONTROL\n");
         /* Mark IRP as pending in all cases */
         IoMarkIrpPending(Irp);
 
@@ -2869,7 +2877,11 @@ ScsiPortDeviceControl(IN PDEVICE_OBJECT DeviceObject,
           break;
 
       default:
-          DPRINT1("  unknown ioctl code: 0x%lX\n", Stack->Parameters.DeviceIoControl.IoControlCode);
+          if ('M' == (Stack->Parameters.DeviceIoControl.IoControlCode >> 16)) {
+            DPRINT1("  got ioctl intended for the mount manager: 0x%lX\n", Stack->Parameters.DeviceIoControl.IoControlCode);
+          } else {
+            DPRINT1("  unknown ioctl code: 0x%lX\n", Stack->Parameters.DeviceIoControl.IoControlCode);
+          }
           Status = STATUS_NOT_IMPLEMENTED;
           break;
     }
@@ -3276,7 +3288,7 @@ SpiAdapterControl(PDEVICE_OBJECT DeviceObject,
             break;
 
         ScatterGatherList->Length = Srb->DataTransferLength - TotalLength;
-        ScatterGatherList->PhysicalAddress = IoMapTransfer(NULL,
+        ScatterGatherList->PhysicalAddress = IoMapTransfer(DeviceExtension->AdapterObject,
                                                            Irp->MdlAddress,
                                                            MapRegisterBase,
                                                            DataVA + TotalLength,
@@ -4008,7 +4020,7 @@ SpiGetInquiryData(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension,
         BusData = &AdapterBusInfo->BusData[Bus];
 
         /* Calculate and save an offset of the inquiry data */
-        BusData->InquiryDataOffset = (PUCHAR)InquiryData - Buffer;
+        BusData->InquiryDataOffset = (ULONG)((PUCHAR)InquiryData - Buffer);
 
         /* Get a pointer to the LUN information structure */
         LunInfo = DeviceExtension->BusesConfig->BusScanInfo[Bus]->LunInfo;
@@ -4034,7 +4046,7 @@ SpiGetInquiryData(IN PSCSI_PORT_DEVICE_EXTENSION DeviceExtension,
             InquiryData->InquiryDataLength = INQUIRYDATABUFFERSIZE;
             InquiryData->DeviceClaimed = LunInfo->DeviceClaimed;
             InquiryData->NextInquiryDataOffset =
-                (PUCHAR)InquiryData + InquiryDataSize - Buffer;
+                (ULONG)((PUCHAR)InquiryData + InquiryDataSize - Buffer);
 
             /* Copy data in it */
             RtlCopyMemory(InquiryData->InquiryData,
@@ -4584,7 +4596,11 @@ ScsiPortIsr(IN PKINTERRUPT Interrupt,
         return FALSE;
 
     /* Call miniport's HwInterrupt routine */
-    DeviceExtension->HwInterrupt(&DeviceExtension->MiniPortDeviceExtension);
+    if (DeviceExtension->HwInterrupt(&DeviceExtension->MiniPortDeviceExtension) == FALSE)
+    {
+        /* This interrupt doesn't belong to us */
+        return FALSE;
+    }
 
     /* If flag of notification is set - queue a DPC */
     if (DeviceExtension->InterruptData.Flags & SCSI_PORT_NOTIFICATION_NEEDED)
@@ -5280,7 +5296,7 @@ SpiBuildDeviceMap (PSCSI_PORT_DEVICE_EXTENSION DeviceExtension,
                          L"\\Registry\\Machine\\Hardware\\DeviceMap\\Scsi");
   InitializeObjectAttributes(&ObjectAttributes,
                             &KeyName,
-                            OBJ_CASE_INSENSITIVE | OBJ_OPENIF,
+                            OBJ_CASE_INSENSITIVE | OBJ_OPENIF | OBJ_KERNEL_HANDLE,
                             0,
                             NULL);
   Status = ZwCreateKey(&ScsiKey,
@@ -5307,7 +5323,7 @@ SpiBuildDeviceMap (PSCSI_PORT_DEVICE_EXTENSION DeviceExtension,
                       NameBuffer);
   InitializeObjectAttributes(&ObjectAttributes,
                             &KeyName,
-                            0,
+                            OBJ_KERNEL_HANDLE,
                             ScsiKey,
                             NULL);
   Status = ZwCreateKey(&ScsiPortKey,
@@ -5355,7 +5371,7 @@ SpiBuildDeviceMap (PSCSI_PORT_DEVICE_EXTENSION DeviceExtension,
                         0,
                         REG_SZ,
                         DriverName,
-                        (wcslen(DriverName) + 1) * sizeof(WCHAR));
+                        (ULONG)((wcslen(DriverName) + 1) * sizeof(WCHAR)));
   if (!NT_SUCCESS(Status))
     {
       DPRINT("ZwSetValueKey('Driver') failed (Status %lx)\n", Status);
@@ -5548,7 +5564,7 @@ SpiBuildDeviceMap (PSCSI_PORT_DEVICE_EXTENSION DeviceExtension,
                                         0,
                                         REG_SZ,
                                         NameBuffer,
-                                        (wcslen(NameBuffer) + 1) * sizeof(WCHAR));
+                                        (ULONG)((wcslen(NameBuffer) + 1) * sizeof(WCHAR)));
                  if (!NT_SUCCESS(Status))
                    {
                      DPRINT("ZwSetValueKey('Identifier') failed (Status %lx)\n", Status);
@@ -5597,7 +5613,7 @@ SpiBuildDeviceMap (PSCSI_PORT_DEVICE_EXTENSION DeviceExtension,
                                         0,
                                         REG_SZ,
                                         TypeName,
-                                        (wcslen(TypeName) + 1) * sizeof(WCHAR));
+                                        (ULONG)((wcslen(TypeName) + 1) * sizeof(WCHAR)));
                  if (!NT_SUCCESS(Status))
                    {
                      DPRINT("ZwSetValueKey('Type') failed (Status %lx)\n", Status);
@@ -6397,5 +6413,4 @@ ScsiPortConvertPhysicalAddressToUlong(IN SCSI_PHYSICAL_ADDRESS Address)
   return(Address.u.LowPart);
 }
 
-
 /* EOF */