win-pvdrivers

view xenvbd_scsiport/xenvbd.c @ 1027:0f40ce5cb467

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