win-pvdrivers

view xenvbd_scsiport/xenvbd.c @ 1049:4de2eb034713

Fix xenvbd under xp
author James Harper <james.harper@bendigoit.com.au>
date Mon May 13 21:19:23 2013 +1000 (2013-05-13)
parents 9f476ac2fbe1
children ca993d6b5539
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_NotificationNextRequest(PXENVBD_DEVICE_DATA xvdd) {
76 ScsiPortNotification(NextRequest, xvdd->xvsd);
77 }
80 VOID
81 XenVbd_NotificationBusChangeDetected(PXENVBD_DEVICE_DATA xvdd, UCHAR PathId) {
82 ScsiPortNotification(BusChangeDetected, xvdd->xvsd, PathId);
83 }
85 #include "..\xenvbd_common\common_miniport.h"
88 /* called in non-dump mode */
89 static ULONG
90 XenVbd_HwScsiFindAdapter(PVOID DeviceExtension, PVOID HwContext, PVOID BusInformation, PCHAR ArgumentString, PPORT_CONFIGURATION_INFORMATION ConfigInfo, PBOOLEAN Again) {
91 PXENVBD_SCSIPORT_DATA xvsd = (PXENVBD_SCSIPORT_DATA)DeviceExtension;
92 PXENVBD_DEVICE_DATA xvdd;
93 PACCESS_RANGE access_range;
95 UNREFERENCED_PARAMETER(HwContext);
96 UNREFERENCED_PARAMETER(BusInformation);
97 UNREFERENCED_PARAMETER(ArgumentString);
99 FUNCTION_ENTER();
100 FUNCTION_MSG("IRQL = %d\n", KeGetCurrentIrql());
101 FUNCTION_MSG("xvsd = %p\n", xvsd);
103 if (ConfigInfo->NumberOfAccessRanges != 1) {
104 FUNCTION_MSG("NumberOfAccessRanges wrong\n");
105 FUNCTION_EXIT();
106 return SP_RETURN_BAD_CONFIG;
107 }
108 if (XnGetVersion() != 1) {
109 FUNCTION_MSG("Wrong XnGetVersion\n");
110 FUNCTION_EXIT();
111 return SP_RETURN_BAD_CONFIG;
112 }
113 RtlZeroMemory(xvsd, FIELD_OFFSET(XENVBD_SCSIPORT_DATA, aligned_buffer_data));
115 access_range = &((*(ConfigInfo->AccessRanges))[0]);
117 if (!dump_mode) {
118 xvdd = (PXENVBD_DEVICE_DATA)(ULONG_PTR)access_range->RangeStart.QuadPart;
119 xvsd->xvdd = xvdd;
120 xvdd->xvsd = xvsd;
121 xvdd->aligned_buffer = (PVOID)((ULONG_PTR)((PUCHAR)xvsd->aligned_buffer_data + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1));
122 } else {
123 /* make a copy of xvdd and use that copy */
124 xvdd = (PXENVBD_DEVICE_DATA)xvsd->aligned_buffer_data;
125 memcpy(xvdd, (PVOID)(ULONG_PTR)access_range->RangeStart.QuadPart, sizeof(XENVBD_DEVICE_DATA));
126 /* make sure original xvdd is set to DISCONNECTED or resume will not work */
127 ((PXENVBD_DEVICE_DATA)(ULONG_PTR)access_range->RangeStart.QuadPart)->device_state = DEVICE_STATE_DISCONNECTED;
128 xvsd->xvdd = xvdd;
129 xvdd->xvsd = xvsd;
130 xvdd->aligned_buffer = (PVOID)((ULONG_PTR)((PUCHAR)xvsd->aligned_buffer_data + sizeof(XENVBD_DEVICE_DATA) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1));
131 if (xvsd->xvdd->device_state != DEVICE_STATE_ACTIVE) {
132 /* if we are not connected to the ring when we start dump mode then there is nothing we can do */
133 FUNCTION_MSG("Cannot connect backend in dump mode - state = %d\n", xvsd->xvdd->device_state);
134 return SP_RETURN_ERROR;
135 }
136 }
137 FUNCTION_MSG("aligned_buffer_data = %p\n", xvsd->aligned_buffer_data);
138 FUNCTION_MSG("aligned_buffer = %p\n", xvdd->aligned_buffer);
140 InitializeListHead(&xvdd->srb_list);
141 xvdd->aligned_buffer_in_use = FALSE;
142 /* align the buffer to PAGE_SIZE */
144 ConfigInfo->MaximumTransferLength = 4 * 1024 * 1024; //BLKIF_MAX_SEGMENTS_PER_REQUEST * PAGE_SIZE;
145 ConfigInfo->NumberOfPhysicalBreaks = ConfigInfo->MaximumTransferLength >> PAGE_SHIFT; //BLKIF_MAX_SEGMENTS_PER_REQUEST - 1;
146 FUNCTION_MSG("ConfigInfo->MaximumTransferLength = %d\n", ConfigInfo->MaximumTransferLength);
147 FUNCTION_MSG("ConfigInfo->NumberOfPhysicalBreaks = %d\n", ConfigInfo->NumberOfPhysicalBreaks);
148 if (!dump_mode) {
149 xvdd->aligned_buffer_size = BLKIF_MAX_SEGMENTS_PER_REQUEST * PAGE_SIZE;
150 } else {
151 xvdd->aligned_buffer_size = DUMP_MODE_UNALIGNED_PAGES * PAGE_SIZE;
152 }
154 FUNCTION_MSG("MultipleRequestPerLu = %d\n", ConfigInfo->MultipleRequestPerLu);
155 FUNCTION_MSG("TaggedQueuing = %d\n", ConfigInfo->TaggedQueuing);
156 FUNCTION_MSG("AutoRequestSense = %d\n", ConfigInfo->AutoRequestSense);
157 ConfigInfo->ScatterGather = FALSE; //TRUE;
158 ConfigInfo->Master = FALSE; //TRUE;
159 ConfigInfo->CachesData = FALSE;
160 ConfigInfo->MapBuffers = TRUE;
161 ConfigInfo->AlignmentMask = 0;
162 ConfigInfo->NumberOfBuses = 1;
163 ConfigInfo->InitiatorBusId[0] = 1;
164 ConfigInfo->MaximumNumberOfLogicalUnits = 1;
165 ConfigInfo->MaximumNumberOfTargets = 2;
166 #if 0
167 if (ConfigInfo->Dma64BitAddresses == SCSI_DMA64_SYSTEM_SUPPORTED) {
168 ConfigInfo->Dma64BitAddresses = SCSI_DMA64_MINIPORT_SUPPORTED;
169 FUNCTION_MSG("Dma64BitAddresses supported\n");
170 } else {
171 FUNCTION_MSG("Dma64BitAddresses not supported\n");
172 }
173 #endif
174 *Again = FALSE;
176 FUNCTION_EXIT();
178 return SP_RETURN_FOUND;
179 }
181 /* Called at PASSIVE_LEVEL for non-dump mode */
182 static BOOLEAN
183 XenVbd_HwScsiInitialize(PVOID DeviceExtension) {
184 PXENVBD_SCSIPORT_DATA xvsd = (PXENVBD_SCSIPORT_DATA)DeviceExtension;
185 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)xvsd->xvdd;
186 ULONG i;
188 FUNCTION_ENTER();
189 FUNCTION_MSG("IRQL = %d\n", KeGetCurrentIrql());
190 FUNCTION_MSG("dump_mode = %d\n", dump_mode);
192 xvdd->shadow_free = 0;
193 memset(xvdd->shadows, 0, sizeof(blkif_shadow_t) * SHADOW_ENTRIES);
194 for (i = 0; i < SHADOW_ENTRIES; i++) {
195 xvdd->shadows[i].req.id = i;
196 /* make sure leftover real requests's are never confused with dump mode requests */
197 if (dump_mode)
198 xvdd->shadows[i].req.id |= SHADOW_ID_DUMP_FLAG;
199 put_shadow_on_freelist(xvdd, &xvdd->shadows[i]);
200 }
202 if (!dump_mode) {
203 /* nothing */
204 } else {
205 xvdd->grant_tag = (ULONG)'DUMP';
206 }
208 FUNCTION_EXIT();
210 return TRUE;
211 }
213 /* this is only used during hiber and dump */
214 static BOOLEAN
215 XenVbd_HwScsiInterrupt(PVOID DeviceExtension)
216 {
217 PXENVBD_SCSIPORT_DATA xvsd = DeviceExtension;
218 XenVbd_HandleEvent(xvsd->xvdd);
219 //SxxxPortNotification(NextLuRequest, xvdd, 0, 0, 0);
220 return TRUE;
221 }
223 static BOOLEAN
224 XenVbd_HwScsiResetBus(PVOID DeviceExtension, ULONG PathId)
225 {
226 PXENVBD_SCSIPORT_DATA xvsd = DeviceExtension;
227 return XenVbd_ResetBus(xvsd->xvdd, PathId);
228 }
230 static VOID
231 XenVbd_CompleteDisconnect(PXENVBD_DEVICE_DATA xvdd) {
232 PXENVBD_SCSIPORT_DATA xvsd = (PXENVBD_SCSIPORT_DATA)xvdd->xvsd;
233 PSCSI_REQUEST_BLOCK srb;
235 if (xvsd->stop_srb) {
236 srb = xvsd->stop_srb;
237 xvsd->stop_srb = NULL;
238 ScsiPortNotification(RequestComplete, xvsd, srb);
239 }
240 }
242 static BOOLEAN
243 XenVbd_HwScsiStartIo(PVOID DeviceExtension, PSCSI_REQUEST_BLOCK srb) {
244 PXENVBD_SCSIPORT_DATA xvsd = DeviceExtension;
245 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)xvsd->xvdd;
246 PSRB_IO_CONTROL sic;
248 if ((LONG)xvsd->outstanding < 0) {
249 FUNCTION_MSG("HwScsiStartIo outstanding = %d\n", xvsd->outstanding);
250 }
251 if (srb->PathId != 0 || srb->TargetId != 0 || srb->Lun != 0) {
252 FUNCTION_MSG("HwScsiStartIo (Out of bounds - PathId = %d, TargetId = %d, Lun = %d)\n", srb->PathId, srb->TargetId, srb->Lun);
253 srb->SrbStatus = SRB_STATUS_NO_DEVICE;
254 ScsiPortNotification(RequestComplete, xvsd, srb);
255 } else if (srb->Function == SRB_FUNCTION_IO_CONTROL && memcmp(((PSRB_IO_CONTROL)srb->DataBuffer)->Signature, XENVBD_CONTROL_SIG, 8) == 0) {
256 sic = srb->DataBuffer;
257 switch(sic->ControlCode) {
258 case XENVBD_CONTROL_EVENT:
259 srb->SrbStatus = SRB_STATUS_SUCCESS;
260 ScsiPortNotification(RequestComplete, xvsd, srb);
261 break;
262 case XENVBD_CONTROL_STOP:
263 if (xvdd->shadow_free == SHADOW_ENTRIES) {
264 srb->SrbStatus = SRB_STATUS_SUCCESS;
265 ScsiPortNotification(RequestComplete, xvsd, srb);
266 FUNCTION_MSG("CONTROL_STOP done\n");
267 } else {
268 xvsd->stop_srb = srb;
269 FUNCTION_MSG("CONTROL_STOP pended\n");
270 }
271 break;
272 case XENVBD_CONTROL_START:
273 // we might need to reload a few things here...
274 ScsiPortNotification(RequestComplete, xvsd, srb);
275 break;
276 default:
277 FUNCTION_MSG("XENVBD_CONTROL_%d\n", sic->ControlCode);
278 srb->SrbStatus = SRB_STATUS_ERROR;
279 ScsiPortNotification(RequestComplete, xvsd, srb);
280 break;
281 }
282 } else if (xvdd->device_state == DEVICE_STATE_INACTIVE) {
283 FUNCTION_MSG("HwScsiStartIo Inactive Device (in StartIo)\n");
284 srb->SrbStatus = SRB_STATUS_NO_DEVICE;
285 ScsiPortNotification(RequestComplete, xvsd, srb);
286 } else {
287 xvsd->outstanding++;
288 XenVbd_PutSrbOnList(xvdd, srb);
289 }
290 /* HandleEvent also puts queued SRB's on the ring */
291 XenVbd_HandleEvent(xvdd);
292 /* need 2 spare slots - 1 for EVENT and 1 for STOP/START */
293 if (xvsd->outstanding < 30) {
294 ScsiPortNotification(NextLuRequest, xvsd, 0, 0, 0);
295 } else {
296 ScsiPortNotification(NextRequest, xvsd);
297 }
298 return TRUE;
299 }
301 static SCSI_ADAPTER_CONTROL_STATUS
302 XenVbd_HwScsiAdapterControl(PVOID DeviceExtension, SCSI_ADAPTER_CONTROL_TYPE ControlType, PVOID Parameters) {
303 PXENVBD_SCSIPORT_DATA xvsd = DeviceExtension;
304 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)xvsd->xvdd;
305 SCSI_ADAPTER_CONTROL_STATUS Status = ScsiAdapterControlSuccess;
306 PSCSI_SUPPORTED_CONTROL_TYPE_LIST SupportedControlTypeList;
308 FUNCTION_ENTER();
309 FUNCTION_MSG("IRQL = %d\n", KeGetCurrentIrql());
310 FUNCTION_MSG("xvsd = %p\n", xvsd);
312 switch (ControlType) {
313 case ScsiQuerySupportedControlTypes:
314 SupportedControlTypeList = (PSCSI_SUPPORTED_CONTROL_TYPE_LIST)Parameters;
315 FUNCTION_MSG("ScsiQuerySupportedControlTypes (Max = %d)\n", SupportedControlTypeList->MaxControlType);
316 SupportedControlTypeList->SupportedTypeList[ScsiQuerySupportedControlTypes] = TRUE;
317 SupportedControlTypeList->SupportedTypeList[ScsiStopAdapter] = TRUE;
318 SupportedControlTypeList->SupportedTypeList[ScsiRestartAdapter] = TRUE;
319 break;
320 case ScsiStopAdapter:
321 FUNCTION_MSG("ScsiStopAdapter\n");
322 if (xvdd->device_state == DEVICE_STATE_INACTIVE) {
323 FUNCTION_MSG("inactive - nothing to do\n");
324 break;
325 }
326 XN_ASSERT(IsListEmpty(&xvdd->srb_list));
327 XN_ASSERT(xvdd->shadow_free == SHADOW_ENTRIES);
328 break;
329 case ScsiRestartAdapter:
330 FUNCTION_MSG("ScsiRestartAdapter\n");
331 if (xvdd->device_state == DEVICE_STATE_INACTIVE) {
332 FUNCTION_MSG("inactive - nothing to do\n");
333 break;
334 }
335 /* increase the tag every time we stop/start to track where the gref's came from */
336 xvdd->grant_tag++;
337 break;
338 case ScsiSetBootConfig:
339 FUNCTION_MSG("ScsiSetBootConfig\n");
340 break;
341 case ScsiSetRunningConfig:
342 FUNCTION_MSG("ScsiSetRunningConfig\n");
343 break;
344 default:
345 FUNCTION_MSG("UNKNOWN\n");
346 break;
347 }
349 FUNCTION_EXIT();
351 return Status;
352 }
354 NTSTATUS
355 DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath) {
356 ULONG status;
357 HW_INITIALIZATION_DATA HwInitializationData;
359 /* RegistryPath == NULL when we are invoked as a crash dump driver */
360 if (!RegistryPath) {
361 dump_mode = TRUE;
362 XnPrintDump();
363 }
365 FUNCTION_ENTER();
366 FUNCTION_MSG("IRQL = %d\n", KeGetCurrentIrql());
367 FUNCTION_MSG("DriverObject = %p, RegistryPath = %p\n", DriverObject, RegistryPath);
369 RtlZeroMemory(&HwInitializationData, sizeof(HW_INITIALIZATION_DATA));
370 HwInitializationData.HwInitializationDataSize = sizeof(HW_INITIALIZATION_DATA);
371 HwInitializationData.AdapterInterfaceType = PNPBus; /* not Internal */
372 HwInitializationData.SrbExtensionSize = sizeof(srb_list_entry_t);
373 HwInitializationData.NumberOfAccessRanges = 1;
374 HwInitializationData.MapBuffers = TRUE;
375 HwInitializationData.NeedPhysicalAddresses = FALSE;
376 HwInitializationData.TaggedQueuing = TRUE;
377 HwInitializationData.AutoRequestSense = TRUE;
378 HwInitializationData.MultipleRequestPerLu = TRUE;
379 HwInitializationData.ReceiveEvent = FALSE;
380 HwInitializationData.HwInitialize = XenVbd_HwScsiInitialize;
381 HwInitializationData.HwStartIo = XenVbd_HwScsiStartIo;
382 HwInitializationData.HwFindAdapter = XenVbd_HwScsiFindAdapter;
383 HwInitializationData.HwResetBus = XenVbd_HwScsiResetBus;
384 HwInitializationData.HwAdapterControl = XenVbd_HwScsiAdapterControl;
385 if (!dump_mode) {
386 HwInitializationData.DeviceExtensionSize = FIELD_OFFSET(XENVBD_SCSIPORT_DATA, aligned_buffer_data) + UNALIGNED_BUFFER_DATA_SIZE;
387 } else {
388 HwInitializationData.HwInterrupt = XenVbd_HwScsiInterrupt;
389 HwInitializationData.DeviceExtensionSize = FIELD_OFFSET(XENVBD_SCSIPORT_DATA, aligned_buffer_data) + sizeof(XENVBD_DEVICE_DATA) + UNALIGNED_BUFFER_DATA_SIZE_DUMP_MODE;
390 }
391 status = ScsiPortInitialize(DriverObject, RegistryPath, &HwInitializationData, NULL);
393 if(!NT_SUCCESS(status)) {
394 FUNCTION_MSG("ScsiPortInitialize failed with status 0x%08x\n", status);
395 }
397 FUNCTION_EXIT();
399 return status;
400 }