[STORPORT] Initialize the port configuration and pass it to the HwFindAdapter routine.
[reactos.git] / drivers / storage / port / storport / miniport.c
1 /*
2 * PROJECT: ReactOS Storport Driver
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: Miniport interface code
5 * COPYRIGHT: Copyright 2017 Eric Kohl (eric.kohl@reactos.org)
6 */
7
8 /* INCLUDES *******************************************************************/
9
10 #include "precomp.h"
11
12 #define NDEBUG
13 #include <debug.h>
14
15
16 /* FUNCTIONS ******************************************************************/
17
18 static
19 NTSTATUS
20 InitializeConfiguration(
21 _In_ PPORT_CONFIGURATION_INFORMATION PortConfig,
22 _In_ PHW_INITIALIZATION_DATA InitData,
23 _In_ ULONG BusNumber,
24 _In_ ULONG SlotNumber)
25 {
26 PCONFIGURATION_INFORMATION ConfigInfo;
27 ULONG i;
28
29 DPRINT1("InitializeConfiguration(%p %p %lu %lu)\n",
30 PortConfig, InitData, BusNumber, SlotNumber);
31
32 /* Get the configurration information */
33 ConfigInfo = IoGetConfigurationInformation();
34
35 /* Initialize the port configuration */
36 RtlZeroMemory(PortConfig,
37 sizeof(PORT_CONFIGURATION_INFORMATION));
38
39 PortConfig->Length = sizeof(PORT_CONFIGURATION_INFORMATION);
40 PortConfig->SystemIoBusNumber = BusNumber;
41 PortConfig->SlotNumber = SlotNumber;
42 PortConfig->AdapterInterfaceType = InitData->AdapterInterfaceType;
43
44 PortConfig->MaximumTransferLength = -1; //SP_UNINITIALIZED_VALUE;
45 PortConfig->DmaChannel = -1; //SP_UNINITIALIZED_VALUE;
46 PortConfig->DmaPort = -1; //SP_UNINITIALIZED_VALUE;
47
48 PortConfig->InterruptMode = LevelSensitive;
49
50 PortConfig->Master = TRUE;
51 PortConfig->AtdiskPrimaryClaimed = ConfigInfo->AtDiskPrimaryAddressClaimed;
52 PortConfig->AtdiskSecondaryClaimed = ConfigInfo->AtDiskSecondaryAddressClaimed;
53 PortConfig->Dma32BitAddresses = TRUE;
54 PortConfig->DemandMode = FALSE;
55 PortConfig->MapBuffers = InitData->MapBuffers;
56
57 PortConfig->NeedPhysicalAddresses = TRUE;
58 PortConfig->TaggedQueuing = TRUE;
59 PortConfig->AutoRequestSense = TRUE;
60 PortConfig->MultipleRequestPerLu = TRUE;
61 PortConfig->ReceiveEvent = InitData->ReceiveEvent;
62 PortConfig->RealModeInitialized = FALSE;
63 PortConfig->BufferAccessScsiPortControlled = TRUE;
64 PortConfig->MaximumNumberOfTargets = 128;
65
66 PortConfig->SpecificLuExtensionSize = InitData->SpecificLuExtensionSize;
67 PortConfig->SrbExtensionSize = InitData->SrbExtensionSize;
68 PortConfig->MaximumNumberOfLogicalUnits = 1;
69 PortConfig->WmiDataProvider = TRUE;
70
71 PortConfig->NumberOfAccessRanges = InitData->NumberOfAccessRanges;
72 DPRINT1("NumberOfAccessRanges: %lu\n", PortConfig->NumberOfAccessRanges);
73 if (PortConfig->NumberOfAccessRanges != 0)
74 {
75 PortConfig->AccessRanges = ExAllocatePoolWithTag(NonPagedPool,
76 PortConfig->NumberOfAccessRanges * sizeof(ACCESS_RANGE),
77 TAG_ACCRESS_RANGE);
78 if (PortConfig->AccessRanges == NULL)
79 return STATUS_NO_MEMORY;
80
81 RtlZeroMemory(PortConfig->AccessRanges,
82 PortConfig->NumberOfAccessRanges * sizeof(ACCESS_RANGE));
83 }
84
85 for (i = 0; i < 7; i++)
86 PortConfig->InitiatorBusId[i] = 0xff;
87
88 return STATUS_SUCCESS;
89 }
90
91
92 NTSTATUS
93 MiniportInitialize(
94 _In_ PMINIPORT Miniport,
95 _In_ PFDO_DEVICE_EXTENSION DeviceExtension,
96 _In_ PHW_INITIALIZATION_DATA InitData)
97 {
98 PMINIPORT_DEVICE_EXTENSION MiniportExtension;
99 ULONG Size;
100 NTSTATUS Status;
101
102 DPRINT1("MiniportInitialize(%p %p %p)\n",
103 Miniport, DeviceExtension, InitData);
104
105 Miniport->DeviceExtension = DeviceExtension;
106 Miniport->InitData = InitData;
107
108 /* Calculate the miniport device extension size */
109 Size = sizeof(MINIPORT_DEVICE_EXTENSION) +
110 Miniport->InitData->DeviceExtensionSize;
111
112 /* Allocate and initialize the miniport device extension */
113 MiniportExtension = ExAllocatePoolWithTag(NonPagedPool,
114 Size,
115 TAG_MINIPORT_DATA);
116 if (MiniportExtension == NULL)
117 return STATUS_NO_MEMORY;
118
119 RtlZeroMemory(MiniportExtension, Size);
120
121 MiniportExtension->Miniport = Miniport;
122 Miniport->MiniportExtension = MiniportExtension;
123
124 /* Initialize the port configuration */
125 Status = InitializeConfiguration(&Miniport->PortConfig,
126 InitData,
127 DeviceExtension->BusNumber,
128 DeviceExtension->SlotNumber);
129
130 return Status;
131 }
132
133
134 NTSTATUS
135 MiniportFindAdapter(
136 _In_ PMINIPORT Miniport)
137 {
138 BOOLEAN Reserved = FALSE;
139 ULONG Result;
140 NTSTATUS Status;
141
142 DPRINT1("MiniportFindAdapter(%p)\n", Miniport);
143
144 /* Call the miniport HwFindAdapter routine */
145 Result = Miniport->InitData->HwFindAdapter(&Miniport->MiniportExtension->HwDeviceExtension,
146 NULL,
147 NULL,
148 NULL,
149 &Miniport->PortConfig,
150 &Reserved);
151 DPRINT1("HwFindAdapter() returned %lu\n", Result);
152
153 switch (Result)
154 {
155 case SP_RETURN_NOT_FOUND:
156 DPRINT1("SP_RETURN_NOT_FOUND\n");
157 Status = STATUS_NOT_FOUND;
158 break;
159
160 case SP_RETURN_FOUND:
161 DPRINT1("SP_RETURN_FOUND\n");
162 Status = STATUS_SUCCESS;
163 break;
164
165 case SP_RETURN_ERROR:
166 DPRINT1("SP_RETURN_ERROR\n");
167 Status = STATUS_ADAPTER_HARDWARE_ERROR;
168 break;
169
170 case SP_RETURN_BAD_CONFIG:
171 DPRINT1("SP_RETURN_BAD_CONFIG\n");
172 Status = STATUS_DEVICE_CONFIGURATION_ERROR;
173 break;
174
175 default:
176 DPRINT1("Unknown result: %lu\n", Result);
177 Status = STATUS_INTERNAL_ERROR;
178 break;
179 }
180
181 return Status;
182 }
183
184
185 NTSTATUS
186 MiniportHwInitialize(
187 _In_ PMINIPORT Miniport)
188 {
189 NTSTATUS Status;
190
191 DPRINT1("MiniportHwInitialize(%p)\n", Miniport);
192
193 /* Call the miniport HwInitialize routine */
194 Status = Miniport->InitData->HwInitialize(&Miniport->MiniportExtension->HwDeviceExtension);
195 DPRINT1("HwInitialize() returned 0x%08lx\n", Status);
196
197 return Status;
198 }
199
200 /* EOF */