win-pvdrivers

view xenvbd_scsiport/xenvbd.c @ 1044:9f476ac2fbe1

Add proper support for vbd resize using unit attention.
author James Harper <james.harper@bendigoit.com.au>
date Mon Mar 04 14:26:41 2013 +1100 (2013-03-04)
parents 306c10cdcba1
children 4de2eb034713
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(NextLuRequest, 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 = TRUE;
158 ConfigInfo->Master = 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 (ConfigInfo->Dma64BitAddresses == SCSI_DMA64_SYSTEM_SUPPORTED) {
167 ConfigInfo->Dma64BitAddresses = SCSI_DMA64_MINIPORT_SUPPORTED;
168 FUNCTION_MSG("Dma64BitAddresses supported\n");
169 } else {
170 FUNCTION_MSG("Dma64BitAddresses not supported\n");
171 }
172 *Again = FALSE;
174 FUNCTION_EXIT();
176 return SP_RETURN_FOUND;
177 }
179 /* Called at PASSIVE_LEVEL for non-dump mode */
180 static BOOLEAN
181 XenVbd_HwScsiInitialize(PVOID DeviceExtension) {
182 PXENVBD_SCSIPORT_DATA xvsd = (PXENVBD_SCSIPORT_DATA)DeviceExtension;
183 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)xvsd->xvdd;
184 ULONG i;
186 FUNCTION_ENTER();
187 FUNCTION_MSG("IRQL = %d\n", KeGetCurrentIrql());
188 FUNCTION_MSG("dump_mode = %d\n", dump_mode);
190 xvdd->shadow_free = 0;
191 memset(xvdd->shadows, 0, sizeof(blkif_shadow_t) * SHADOW_ENTRIES);
192 for (i = 0; i < SHADOW_ENTRIES; i++) {
193 xvdd->shadows[i].req.id = i;
194 /* make sure leftover real requests's are never confused with dump mode requests */
195 if (dump_mode)
196 xvdd->shadows[i].req.id |= SHADOW_ID_DUMP_FLAG;
197 put_shadow_on_freelist(xvdd, &xvdd->shadows[i]);
198 }
200 if (!dump_mode) {
201 /* nothing */
202 } else {
203 xvdd->grant_tag = (ULONG)'DUMP';
204 }
206 FUNCTION_EXIT();
208 return TRUE;
209 }
211 /* this is only used during hiber and dump */
212 static BOOLEAN
213 XenVbd_HwScsiInterrupt(PVOID DeviceExtension)
214 {
215 PXENVBD_SCSIPORT_DATA xvsd = DeviceExtension;
216 XenVbd_HandleEvent(xvsd->xvdd);
217 //SxxxPortNotification(NextLuRequest, xvdd, 0, 0, 0);
218 return TRUE;
219 }
221 static BOOLEAN
222 XenVbd_HwScsiResetBus(PVOID DeviceExtension, ULONG PathId)
223 {
224 PXENVBD_SCSIPORT_DATA xvsd = DeviceExtension;
225 return XenVbd_ResetBus(xvsd->xvdd, PathId);
226 }
228 static VOID
229 XenVbd_CompleteDisconnect(PXENVBD_DEVICE_DATA xvdd) {
230 PXENVBD_SCSIPORT_DATA xvsd = (PXENVBD_SCSIPORT_DATA)xvdd->xvsd;
231 PSCSI_REQUEST_BLOCK srb;
233 if (xvsd->stop_srb) {
234 srb = xvsd->stop_srb;
235 xvsd->stop_srb = NULL;
236 ScsiPortNotification(RequestComplete, xvsd, srb);
237 }
238 }
240 static BOOLEAN
241 XenVbd_HwScsiStartIo(PVOID DeviceExtension, PSCSI_REQUEST_BLOCK srb) {
242 PXENVBD_SCSIPORT_DATA xvsd = DeviceExtension;
243 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)xvsd->xvdd;
244 PSRB_IO_CONTROL sic;
246 if ((LONG)xvsd->outstanding < 0) {
247 FUNCTION_MSG("HwScsiStartIo outstanding = %d\n", xvsd->outstanding);
248 }
249 if (srb->PathId != 0 || srb->TargetId != 0 || srb->Lun != 0) {
250 FUNCTION_MSG("HwScsiStartIo (Out of bounds - PathId = %d, TargetId = %d, Lun = %d)\n", srb->PathId, srb->TargetId, srb->Lun);
251 srb->SrbStatus = SRB_STATUS_NO_DEVICE;
252 ScsiPortNotification(RequestComplete, xvsd, srb);
253 } else if (srb->Function == SRB_FUNCTION_IO_CONTROL && memcmp(((PSRB_IO_CONTROL)srb->DataBuffer)->Signature, XENVBD_CONTROL_SIG, 8) == 0) {
254 sic = srb->DataBuffer;
255 switch(sic->ControlCode) {
256 case XENVBD_CONTROL_EVENT:
257 srb->SrbStatus = SRB_STATUS_SUCCESS;
258 ScsiPortNotification(RequestComplete, xvsd, srb);
259 break;
260 case XENVBD_CONTROL_STOP:
261 if (xvdd->shadow_free == SHADOW_ENTRIES) {
262 srb->SrbStatus = SRB_STATUS_SUCCESS;
263 ScsiPortNotification(RequestComplete, xvsd, srb);
264 FUNCTION_MSG("CONTROL_STOP done\n");
265 } else {
266 xvsd->stop_srb = srb;
267 FUNCTION_MSG("CONTROL_STOP pended\n");
268 }
269 break;
270 case XENVBD_CONTROL_START:
271 // we might need to reload a few things here...
272 ScsiPortNotification(RequestComplete, xvsd, srb);
273 break;
274 default:
275 FUNCTION_MSG("XENVBD_CONTROL_%d\n", sic->ControlCode);
276 srb->SrbStatus = SRB_STATUS_ERROR;
277 ScsiPortNotification(RequestComplete, xvsd, srb);
278 break;
279 }
280 } else if (xvdd->device_state == DEVICE_STATE_INACTIVE) {
281 FUNCTION_MSG("HwScsiStartIo Inactive Device (in StartIo)\n");
282 srb->SrbStatus = SRB_STATUS_NO_DEVICE;
283 ScsiPortNotification(RequestComplete, xvsd, srb);
284 } else {
285 xvsd->outstanding++;
286 XenVbd_PutSrbOnList(xvdd, srb);
287 }
288 /* HandleEvent also puts queued SRB's on the ring */
289 XenVbd_HandleEvent(xvdd);
290 /* need 2 spare slots - 1 for EVENT and 1 for STOP/START */
291 if (xvsd->outstanding < 30) {
292 ScsiPortNotification(NextLuRequest, xvsd, 0, 0, 0);
293 } else {
294 ScsiPortNotification(NextRequest, xvsd);
295 }
296 return TRUE;
297 }
299 static SCSI_ADAPTER_CONTROL_STATUS
300 XenVbd_HwScsiAdapterControl(PVOID DeviceExtension, SCSI_ADAPTER_CONTROL_TYPE ControlType, PVOID Parameters) {
301 PXENVBD_SCSIPORT_DATA xvsd = DeviceExtension;
302 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)xvsd->xvdd;
303 SCSI_ADAPTER_CONTROL_STATUS Status = ScsiAdapterControlSuccess;
304 PSCSI_SUPPORTED_CONTROL_TYPE_LIST SupportedControlTypeList;
306 FUNCTION_ENTER();
307 FUNCTION_MSG("IRQL = %d\n", KeGetCurrentIrql());
308 FUNCTION_MSG("xvsd = %p\n", xvsd);
310 switch (ControlType) {
311 case ScsiQuerySupportedControlTypes:
312 SupportedControlTypeList = (PSCSI_SUPPORTED_CONTROL_TYPE_LIST)Parameters;
313 FUNCTION_MSG("ScsiQuerySupportedControlTypes (Max = %d)\n", SupportedControlTypeList->MaxControlType);
314 SupportedControlTypeList->SupportedTypeList[ScsiQuerySupportedControlTypes] = TRUE;
315 SupportedControlTypeList->SupportedTypeList[ScsiStopAdapter] = TRUE;
316 SupportedControlTypeList->SupportedTypeList[ScsiRestartAdapter] = TRUE;
317 break;
318 case ScsiStopAdapter:
319 FUNCTION_MSG("ScsiStopAdapter\n");
320 if (xvdd->device_state == DEVICE_STATE_INACTIVE) {
321 FUNCTION_MSG("inactive - nothing to do\n");
322 break;
323 }
324 XN_ASSERT(IsListEmpty(&xvdd->srb_list));
325 XN_ASSERT(xvdd->shadow_free == SHADOW_ENTRIES);
326 break;
327 case ScsiRestartAdapter:
328 FUNCTION_MSG("ScsiRestartAdapter\n");
329 if (xvdd->device_state == DEVICE_STATE_INACTIVE) {
330 FUNCTION_MSG("inactive - nothing to do\n");
331 break;
332 }
333 /* increase the tag every time we stop/start to track where the gref's came from */
334 xvdd->grant_tag++;
335 break;
336 case ScsiSetBootConfig:
337 FUNCTION_MSG("ScsiSetBootConfig\n");
338 break;
339 case ScsiSetRunningConfig:
340 FUNCTION_MSG("ScsiSetRunningConfig\n");
341 break;
342 default:
343 FUNCTION_MSG("UNKNOWN\n");
344 break;
345 }
347 FUNCTION_EXIT();
349 return Status;
350 }
352 NTSTATUS
353 DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath) {
354 ULONG status;
355 HW_INITIALIZATION_DATA HwInitializationData;
357 /* RegistryPath == NULL when we are invoked as a crash dump driver */
358 if (!RegistryPath) {
359 dump_mode = TRUE;
360 XnPrintDump();
361 }
363 FUNCTION_ENTER();
364 FUNCTION_MSG("IRQL = %d\n", KeGetCurrentIrql());
365 FUNCTION_MSG("DriverObject = %p, RegistryPath = %p\n", DriverObject, RegistryPath);
367 RtlZeroMemory(&HwInitializationData, sizeof(HW_INITIALIZATION_DATA));
368 HwInitializationData.HwInitializationDataSize = sizeof(HW_INITIALIZATION_DATA);
369 HwInitializationData.AdapterInterfaceType = PNPBus; /* not Internal */
370 HwInitializationData.SrbExtensionSize = sizeof(srb_list_entry_t);
371 HwInitializationData.NumberOfAccessRanges = 1;
372 HwInitializationData.MapBuffers = TRUE;
373 HwInitializationData.NeedPhysicalAddresses = FALSE;
374 HwInitializationData.TaggedQueuing = TRUE;
375 HwInitializationData.AutoRequestSense = TRUE;
376 HwInitializationData.MultipleRequestPerLu = TRUE;
377 HwInitializationData.ReceiveEvent = FALSE;
378 HwInitializationData.HwInitialize = XenVbd_HwScsiInitialize;
379 HwInitializationData.HwStartIo = XenVbd_HwScsiStartIo;
380 HwInitializationData.HwFindAdapter = XenVbd_HwScsiFindAdapter;
381 HwInitializationData.HwResetBus = XenVbd_HwScsiResetBus;
382 HwInitializationData.HwAdapterControl = XenVbd_HwScsiAdapterControl;
383 if (!dump_mode) {
384 HwInitializationData.DeviceExtensionSize = FIELD_OFFSET(XENVBD_SCSIPORT_DATA, aligned_buffer_data) + UNALIGNED_BUFFER_DATA_SIZE;
385 } else {
386 HwInitializationData.HwInterrupt = XenVbd_HwScsiInterrupt;
387 HwInitializationData.DeviceExtensionSize = FIELD_OFFSET(XENVBD_SCSIPORT_DATA, aligned_buffer_data) + sizeof(XENVBD_DEVICE_DATA) + UNALIGNED_BUFFER_DATA_SIZE_DUMP_MODE;
388 }
389 status = ScsiPortInitialize(DriverObject, RegistryPath, &HwInitializationData, NULL);
391 if(!NT_SUCCESS(status)) {
392 FUNCTION_MSG("ScsiPortInitialize failed with status 0x%08x\n", status);
393 }
395 FUNCTION_EXIT();
397 return status;
398 }