win-pvdrivers

view xenvbd_scsiport/xenvbd.c @ 1032:306c10cdcba1

Added online resize. Untested under storport yet (>= Vista)
author James Harper <james.harper@bendigoit.com.au>
date Sun Mar 03 12:19:31 2013 +1100 (2013-03-03)
parents 0f40ce5cb467
children 9f476ac2fbe1
line source
1 /*
2 PV Drivers for Windows Xen HVM Domains
3 Copyright (C) 2007 James Harper
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (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
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
20 #define INITGUID
22 #include "xenvbd.h"
24 #pragma warning(disable: 4127)
26 /* Not really necessary but keeps PREfast happy */
27 DRIVER_INITIALIZE DriverEntry;
28 static IO_WORKITEM_ROUTINE XenVbd_DisconnectWorkItem;
30 static VOID XenVbd_ProcessSrbList(PXENVBD_DEVICE_DATA xvdd);
31 static BOOLEAN XenVbd_ResetBus(PXENVBD_DEVICE_DATA xvdd, ULONG PathId);
32 static VOID XenVbd_CompleteDisconnect(PXENVBD_DEVICE_DATA xvdd);
34 static BOOLEAN dump_mode = FALSE;
35 #define DUMP_MODE_ERROR_LIMIT 64
36 static ULONG dump_mode_errors = 0;
38 #define StorPortAcquireSpinLock(...) {}
39 #define StorPortReleaseSpinLock(...) {}
41 static ULONG
42 SxxxPortGetSystemAddress(PVOID device_extension, PSCSI_REQUEST_BLOCK srb, PVOID *system_address) {
43 UNREFERENCED_PARAMETER(device_extension);
44 *system_address = (PUCHAR)srb->DataBuffer;
45 return STATUS_SUCCESS;
46 }
48 static PHYSICAL_ADDRESS
49 SxxxPortGetPhysicalAddress(PVOID device_extension, PSCSI_REQUEST_BLOCK srb, PVOID virtual_address, ULONG *length) {
50 UNREFERENCED_PARAMETER(device_extension);
51 UNREFERENCED_PARAMETER(srb);
52 UNREFERENCED_PARAMETER(length);
53 return MmGetPhysicalAddress(virtual_address);
54 }
56 #define SxxxPortNotification(NotificationType, DeviceExtension, ...) XenVbd_Notification##NotificationType(DeviceExtension, __VA_ARGS__)
58 static VOID
59 XenVbd_NotificationRequestComplete(PXENVBD_DEVICE_DATA xvdd, PSCSI_REQUEST_BLOCK srb) {
60 PXENVBD_SCSIPORT_DATA xvsd = (PXENVBD_SCSIPORT_DATA)xvdd->xvsd;
61 srb_list_entry_t *srb_entry = srb->SrbExtension;
62 if (srb_entry->outstanding_requests != 0) {
63 FUNCTION_MSG("srb outstanding_requests = %d\n", srb_entry->outstanding_requests);
64 }
65 xvsd->outstanding--;
66 ScsiPortNotification(RequestComplete, xvsd, srb);
67 }
69 VOID
70 XenVbd_NotificationNextLuRequest(PXENVBD_DEVICE_DATA xvdd, UCHAR PathId, UCHAR TargetId, UCHAR Lun) {
71 ScsiPortNotification(NextLuRequest, xvdd->xvsd, PathId, TargetId, Lun);
72 }
74 VOID
75 XenVbd_NotificationBusChangeDetected(PXENVBD_DEVICE_DATA xvdd, UCHAR PathId) {
76 ScsiPortNotification(BusChangeDetected, xvdd->xvsd, PathId);
77 }
79 #include "..\xenvbd_common\common_miniport.h"
82 /* called in non-dump mode */
83 static ULONG
84 XenVbd_HwScsiFindAdapter(PVOID DeviceExtension, PVOID HwContext, PVOID BusInformation, PCHAR ArgumentString, PPORT_CONFIGURATION_INFORMATION ConfigInfo, PBOOLEAN Again) {
85 PXENVBD_SCSIPORT_DATA xvsd = (PXENVBD_SCSIPORT_DATA)DeviceExtension;
86 PXENVBD_DEVICE_DATA xvdd;
87 PACCESS_RANGE access_range;
89 UNREFERENCED_PARAMETER(HwContext);
90 UNREFERENCED_PARAMETER(BusInformation);
91 UNREFERENCED_PARAMETER(ArgumentString);
93 FUNCTION_ENTER();
94 FUNCTION_MSG("IRQL = %d\n", KeGetCurrentIrql());
95 FUNCTION_MSG("xvsd = %p\n", xvsd);
97 if (ConfigInfo->NumberOfAccessRanges != 1) {
98 FUNCTION_MSG("NumberOfAccessRanges wrong\n");
99 FUNCTION_EXIT();
100 return SP_RETURN_BAD_CONFIG;
101 }
102 if (XnGetVersion() != 1) {
103 FUNCTION_MSG("Wrong XnGetVersion\n");
104 FUNCTION_EXIT();
105 return SP_RETURN_BAD_CONFIG;
106 }
107 RtlZeroMemory(xvsd, FIELD_OFFSET(XENVBD_SCSIPORT_DATA, aligned_buffer_data));
109 access_range = &((*(ConfigInfo->AccessRanges))[0]);
111 if (!dump_mode) {
112 xvdd = (PXENVBD_DEVICE_DATA)(ULONG_PTR)access_range->RangeStart.QuadPart;
113 xvsd->xvdd = xvdd;
114 xvdd->xvsd = xvsd;
115 xvdd->aligned_buffer = (PVOID)((ULONG_PTR)((PUCHAR)xvsd->aligned_buffer_data + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1));
116 } else {
117 /* make a copy of xvdd and use that copy */
118 xvdd = (PXENVBD_DEVICE_DATA)xvsd->aligned_buffer_data;
119 memcpy(xvdd, (PVOID)(ULONG_PTR)access_range->RangeStart.QuadPart, sizeof(XENVBD_DEVICE_DATA));
120 /* make sure original xvdd is set to DISCONNECTED or resume will not work */
121 ((PXENVBD_DEVICE_DATA)(ULONG_PTR)access_range->RangeStart.QuadPart)->device_state = DEVICE_STATE_DISCONNECTED;
122 xvsd->xvdd = xvdd;
123 xvdd->xvsd = xvsd;
124 xvdd->aligned_buffer = (PVOID)((ULONG_PTR)((PUCHAR)xvsd->aligned_buffer_data + sizeof(XENVBD_DEVICE_DATA) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1));
125 if (xvsd->xvdd->device_state != DEVICE_STATE_ACTIVE) {
126 /* if we are not connected to the ring when we start dump mode then there is nothing we can do */
127 FUNCTION_MSG("Cannot connect backend in dump mode - state = %d\n", xvsd->xvdd->device_state);
128 return SP_RETURN_ERROR;
129 }
130 }
131 FUNCTION_MSG("aligned_buffer_data = %p\n", xvsd->aligned_buffer_data);
132 FUNCTION_MSG("aligned_buffer = %p\n", xvdd->aligned_buffer);
134 InitializeListHead(&xvdd->srb_list);
135 xvdd->aligned_buffer_in_use = FALSE;
136 /* align the buffer to PAGE_SIZE */
138 ConfigInfo->MaximumTransferLength = 4 * 1024 * 1024; //BLKIF_MAX_SEGMENTS_PER_REQUEST * PAGE_SIZE;
139 ConfigInfo->NumberOfPhysicalBreaks = ConfigInfo->MaximumTransferLength >> PAGE_SHIFT; //BLKIF_MAX_SEGMENTS_PER_REQUEST - 1;
140 FUNCTION_MSG("ConfigInfo->MaximumTransferLength = %d\n", ConfigInfo->MaximumTransferLength);
141 FUNCTION_MSG("ConfigInfo->NumberOfPhysicalBreaks = %d\n", ConfigInfo->NumberOfPhysicalBreaks);
142 if (!dump_mode) {
143 xvdd->aligned_buffer_size = BLKIF_MAX_SEGMENTS_PER_REQUEST * PAGE_SIZE;
144 } else {
145 xvdd->aligned_buffer_size = DUMP_MODE_UNALIGNED_PAGES * PAGE_SIZE;
146 }
148 FUNCTION_MSG("MultipleRequestPerLu = %d\n", ConfigInfo->MultipleRequestPerLu);
149 FUNCTION_MSG("TaggedQueuing = %d\n", ConfigInfo->TaggedQueuing);
150 FUNCTION_MSG("AutoRequestSense = %d\n", ConfigInfo->AutoRequestSense );
151 ConfigInfo->ScatterGather = TRUE;
152 ConfigInfo->Master = TRUE;
153 ConfigInfo->CachesData = FALSE;
154 ConfigInfo->MapBuffers = TRUE;
155 ConfigInfo->AlignmentMask = 0;
156 ConfigInfo->NumberOfBuses = 1;
157 ConfigInfo->InitiatorBusId[0] = 1;
158 ConfigInfo->MaximumNumberOfLogicalUnits = 1;
159 ConfigInfo->MaximumNumberOfTargets = 2;
160 if (ConfigInfo->Dma64BitAddresses == SCSI_DMA64_SYSTEM_SUPPORTED) {
161 ConfigInfo->Dma64BitAddresses = SCSI_DMA64_MINIPORT_SUPPORTED;
162 FUNCTION_MSG("Dma64BitAddresses supported\n");
163 } else {
164 FUNCTION_MSG("Dma64BitAddresses not supported\n");
165 }
166 *Again = FALSE;
168 FUNCTION_EXIT();
170 return SP_RETURN_FOUND;
171 }
173 /* Called at PASSIVE_LEVEL for non-dump mode */
174 static BOOLEAN
175 XenVbd_HwScsiInitialize(PVOID DeviceExtension) {
176 PXENVBD_SCSIPORT_DATA xvsd = (PXENVBD_SCSIPORT_DATA)DeviceExtension;
177 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)xvsd->xvdd;
178 ULONG i;
180 FUNCTION_ENTER();
181 FUNCTION_MSG("IRQL = %d\n", KeGetCurrentIrql());
182 FUNCTION_MSG("dump_mode = %d\n", dump_mode);
184 xvdd->shadow_free = 0;
185 memset(xvdd->shadows, 0, sizeof(blkif_shadow_t) * SHADOW_ENTRIES);
186 for (i = 0; i < SHADOW_ENTRIES; i++) {
187 xvdd->shadows[i].req.id = i;
188 /* make sure leftover real requests's are never confused with dump mode requests */
189 if (dump_mode)
190 xvdd->shadows[i].req.id |= SHADOW_ID_DUMP_FLAG;
191 put_shadow_on_freelist(xvdd, &xvdd->shadows[i]);
192 }
194 if (!dump_mode) {
195 /* nothing */
196 } else {
197 xvdd->grant_tag = (ULONG)'DUMP';
198 }
200 FUNCTION_EXIT();
202 return TRUE;
203 }
205 /* this is only used during hiber and dump */
206 static BOOLEAN
207 XenVbd_HwScsiInterrupt(PVOID DeviceExtension)
208 {
209 PXENVBD_SCSIPORT_DATA xvsd = DeviceExtension;
210 XenVbd_HandleEvent(xvsd->xvdd);
211 //SxxxPortNotification(NextLuRequest, xvdd, 0, 0, 0);
212 return TRUE;
213 }
215 static BOOLEAN
216 XenVbd_HwScsiResetBus(PVOID DeviceExtension, ULONG PathId)
217 {
218 PXENVBD_SCSIPORT_DATA xvsd = DeviceExtension;
219 return XenVbd_ResetBus(xvsd->xvdd, PathId);
220 }
222 static VOID
223 XenVbd_CompleteDisconnect(PXENVBD_DEVICE_DATA xvdd) {
224 PXENVBD_SCSIPORT_DATA xvsd = (PXENVBD_SCSIPORT_DATA)xvdd->xvsd;
225 PSCSI_REQUEST_BLOCK srb;
227 if (xvsd->stop_srb) {
228 srb = xvsd->stop_srb;
229 xvsd->stop_srb = NULL;
230 ScsiPortNotification(RequestComplete, xvsd, srb);
231 }
232 }
234 static BOOLEAN
235 XenVbd_HwScsiStartIo(PVOID DeviceExtension, PSCSI_REQUEST_BLOCK srb) {
236 PXENVBD_SCSIPORT_DATA xvsd = DeviceExtension;
237 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)xvsd->xvdd;
238 PSRB_IO_CONTROL sic;
240 if ((LONG)xvsd->outstanding < 0) {
241 FUNCTION_MSG("HwScsiStartIo outstanding = %d\n", xvsd->outstanding);
242 }
243 if (srb->PathId != 0 || srb->TargetId != 0 || srb->Lun != 0) {
244 FUNCTION_MSG("HwScsiStartIo (Out of bounds - PathId = %d, TargetId = %d, Lun = %d)\n", srb->PathId, srb->TargetId, srb->Lun);
245 srb->SrbStatus = SRB_STATUS_NO_DEVICE;
246 ScsiPortNotification(RequestComplete, xvsd, srb);
247 } else if (srb->Function == SRB_FUNCTION_IO_CONTROL && memcmp(((PSRB_IO_CONTROL)srb->DataBuffer)->Signature, XENVBD_CONTROL_SIG, 8) == 0) {
248 sic = srb->DataBuffer;
249 switch(sic->ControlCode) {
250 case XENVBD_CONTROL_EVENT:
251 srb->SrbStatus = SRB_STATUS_SUCCESS;
252 ScsiPortNotification(RequestComplete, xvsd, srb);
253 break;
254 case XENVBD_CONTROL_STOP:
255 if (xvdd->shadow_free == SHADOW_ENTRIES) {
256 srb->SrbStatus = SRB_STATUS_SUCCESS;
257 ScsiPortNotification(RequestComplete, xvsd, srb);
258 FUNCTION_MSG("CONTROL_STOP done\n");
259 } else {
260 xvsd->stop_srb = srb;
261 FUNCTION_MSG("CONTROL_STOP pended\n");
262 }
263 break;
264 case XENVBD_CONTROL_START:
265 // we might need to reload a few things here...
266 ScsiPortNotification(RequestComplete, xvsd, srb);
267 break;
268 default:
269 FUNCTION_MSG("XENVBD_CONTROL_%d\n", sic->ControlCode);
270 srb->SrbStatus = SRB_STATUS_ERROR;
271 ScsiPortNotification(RequestComplete, xvsd, srb);
272 break;
273 }
274 } else if (xvdd->device_state == DEVICE_STATE_INACTIVE) {
275 FUNCTION_MSG("HwScsiStartIo Inactive Device (in StartIo)\n");
276 srb->SrbStatus = SRB_STATUS_NO_DEVICE;
277 ScsiPortNotification(RequestComplete, xvsd, srb);
278 } else {
279 xvsd->outstanding++;
280 XenVbd_PutSrbOnList(xvdd, srb);
281 }
282 /* HandleEvent also puts queued SRB's on the ring */
283 XenVbd_HandleEvent(xvdd);
284 /* need 2 spare slots - 1 for EVENT and 1 for STOP/START */
285 if (xvsd->outstanding < 30) {
286 ScsiPortNotification(NextLuRequest, xvsd, 0, 0, 0);
287 } else {
288 ScsiPortNotification(NextRequest, xvsd);
289 }
290 return TRUE;
291 }
293 static SCSI_ADAPTER_CONTROL_STATUS
294 XenVbd_HwScsiAdapterControl(PVOID DeviceExtension, SCSI_ADAPTER_CONTROL_TYPE ControlType, PVOID Parameters) {
295 PXENVBD_SCSIPORT_DATA xvsd = DeviceExtension;
296 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)xvsd->xvdd;
297 SCSI_ADAPTER_CONTROL_STATUS Status = ScsiAdapterControlSuccess;
298 PSCSI_SUPPORTED_CONTROL_TYPE_LIST SupportedControlTypeList;
300 FUNCTION_ENTER();
301 FUNCTION_MSG("IRQL = %d\n", KeGetCurrentIrql());
302 FUNCTION_MSG("xvsd = %p\n", xvsd);
304 switch (ControlType) {
305 case ScsiQuerySupportedControlTypes:
306 SupportedControlTypeList = (PSCSI_SUPPORTED_CONTROL_TYPE_LIST)Parameters;
307 FUNCTION_MSG("ScsiQuerySupportedControlTypes (Max = %d)\n", SupportedControlTypeList->MaxControlType);
308 SupportedControlTypeList->SupportedTypeList[ScsiQuerySupportedControlTypes] = TRUE;
309 SupportedControlTypeList->SupportedTypeList[ScsiStopAdapter] = TRUE;
310 SupportedControlTypeList->SupportedTypeList[ScsiRestartAdapter] = TRUE;
311 break;
312 case ScsiStopAdapter:
313 FUNCTION_MSG("ScsiStopAdapter\n");
314 if (xvdd->device_state == DEVICE_STATE_INACTIVE) {
315 FUNCTION_MSG("inactive - nothing to do\n");
316 break;
317 }
318 XN_ASSERT(IsListEmpty(&xvdd->srb_list));
319 XN_ASSERT(xvdd->shadow_free == SHADOW_ENTRIES);
320 break;
321 case ScsiRestartAdapter:
322 FUNCTION_MSG("ScsiRestartAdapter\n");
323 if (xvdd->device_state == DEVICE_STATE_INACTIVE) {
324 FUNCTION_MSG("inactive - nothing to do\n");
325 break;
326 }
327 /* increase the tag every time we stop/start to track where the gref's came from */
328 xvdd->grant_tag++;
329 break;
330 case ScsiSetBootConfig:
331 FUNCTION_MSG("ScsiSetBootConfig\n");
332 break;
333 case ScsiSetRunningConfig:
334 FUNCTION_MSG("ScsiSetRunningConfig\n");
335 break;
336 default:
337 FUNCTION_MSG("UNKNOWN\n");
338 break;
339 }
341 FUNCTION_EXIT();
343 return Status;
344 }
346 NTSTATUS
347 DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath) {
348 ULONG status;
349 HW_INITIALIZATION_DATA HwInitializationData;
351 /* RegistryPath == NULL when we are invoked as a crash dump driver */
352 if (!RegistryPath) {
353 dump_mode = TRUE;
354 XnPrintDump();
355 }
357 FUNCTION_ENTER();
358 FUNCTION_MSG("IRQL = %d\n", KeGetCurrentIrql());
359 FUNCTION_MSG("DriverObject = %p, RegistryPath = %p\n", DriverObject, RegistryPath);
361 RtlZeroMemory(&HwInitializationData, sizeof(HW_INITIALIZATION_DATA));
362 HwInitializationData.HwInitializationDataSize = sizeof(HW_INITIALIZATION_DATA);
363 HwInitializationData.AdapterInterfaceType = PNPBus; /* not Internal */
364 HwInitializationData.SrbExtensionSize = sizeof(srb_list_entry_t);
365 HwInitializationData.NumberOfAccessRanges = 1;
366 HwInitializationData.MapBuffers = TRUE;
367 HwInitializationData.NeedPhysicalAddresses = FALSE;
368 HwInitializationData.TaggedQueuing = TRUE;
369 HwInitializationData.AutoRequestSense = TRUE;
370 HwInitializationData.MultipleRequestPerLu = TRUE;
371 HwInitializationData.ReceiveEvent = FALSE;
372 HwInitializationData.HwInitialize = XenVbd_HwScsiInitialize;
373 HwInitializationData.HwStartIo = XenVbd_HwScsiStartIo;
374 HwInitializationData.HwFindAdapter = XenVbd_HwScsiFindAdapter;
375 HwInitializationData.HwResetBus = XenVbd_HwScsiResetBus;
376 HwInitializationData.HwAdapterControl = XenVbd_HwScsiAdapterControl;
377 if (!dump_mode) {
378 HwInitializationData.DeviceExtensionSize = FIELD_OFFSET(XENVBD_SCSIPORT_DATA, aligned_buffer_data) + UNALIGNED_BUFFER_DATA_SIZE;
379 } else {
380 HwInitializationData.HwInterrupt = XenVbd_HwScsiInterrupt;
381 HwInitializationData.DeviceExtensionSize = FIELD_OFFSET(XENVBD_SCSIPORT_DATA, aligned_buffer_data) + sizeof(XENVBD_DEVICE_DATA) + UNALIGNED_BUFFER_DATA_SIZE_DUMP_MODE;
382 }
383 status = ScsiPortInitialize(DriverObject, RegistryPath, &HwInitializationData, NULL);
385 if(!NT_SUCCESS(status)) {
386 FUNCTION_MSG("ScsiPortInitialize failed with status 0x%08x\n", status);
387 }
389 FUNCTION_EXIT();
391 return status;
392 }