win-pvdrivers

view xenvbd_common/common_xen.h @ 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 aa2e51f67f7c
children e86ea7c3d8a4
line source
1 /*
2 PV Drivers for Windows Xen HVM Domains
3 Copyright (C) 2013 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 static NTSTATUS
21 XenVbd_Connect(PXENVBD_DEVICE_DATA xvdd, BOOLEAN suspend) {
22 BOOLEAN qemu_hide_filter = FALSE;
23 ULONG qemu_hide_flags_value = 0;
24 PCHAR device_type;
25 //BOOLEAN active = FALSE;
26 NTSTATUS status;
27 PCHAR mode;
28 PCHAR uuid;
29 PFN_NUMBER pfn;
31 FUNCTION_ENTER();
33 if (xvdd->device_state != DEVICE_STATE_DISCONNECTED) {
34 FUNCTION_MSG("state not DEVICE_STATE_DISCONNECTED, is %d instead\n", xvdd->device_state);
35 FUNCTION_EXIT();
36 return STATUS_SUCCESS;
37 }
38 if (!suspend) {
39 xvdd->backend_state = XenbusStateUnknown;
40 if ((xvdd->handle = XnOpenDevice(xvdd->pdo, XenVbd_DeviceCallback, xvdd)) == NULL) {
41 FUNCTION_MSG("Failed to open\n");
42 return STATUS_UNSUCCESSFUL;
43 }
44 }
45 xvdd->sring = (blkif_sring_t *)ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENVBD_POOL_TAG);
46 if (!xvdd->sring) {
47 FUNCTION_MSG("Failed to allocate sring\n");
48 return STATUS_UNSUCCESSFUL;
49 }
50 pfn = (PFN_NUMBER)(MmGetPhysicalAddress(xvdd->sring).QuadPart >> PAGE_SHIFT);
51 xvdd->sring_gref = XnGrantAccess(xvdd->handle, (ULONG)pfn, FALSE, INVALID_GRANT_REF, xvdd->grant_tag);
52 SHARED_RING_INIT(xvdd->sring);
53 FRONT_RING_INIT(&xvdd->ring, xvdd->sring, PAGE_SIZE);
55 while (xvdd->backend_state != XenbusStateInitialising &&
56 xvdd->backend_state != XenbusStateInitWait &&
57 xvdd->backend_state != XenbusStateInitialised) {
58 FUNCTION_MSG("waiting for XenbusStateInitXxx, backend_state = %d\n", xvdd->backend_state);
59 KeWaitForSingleObject(&xvdd->backend_event, Executive, KernelMode, FALSE, NULL);
60 }
61 XnGetValue(xvdd->handle, XN_VALUE_TYPE_QEMU_HIDE_FLAGS, &qemu_hide_flags_value);
62 XnGetValue(xvdd->handle, XN_VALUE_TYPE_QEMU_FILTER, &qemu_hide_filter);
63 status = XnReadString(xvdd->handle, XN_BASE_FRONTEND, "device-type", &device_type);
64 if (strcmp(device_type, "disk") == 0) {
65 FUNCTION_MSG("device-type = Disk\n");
66 xvdd->device_type = XENVBD_DEVICETYPE_DISK;
67 } else if (strcmp(device_type, "cdrom") == 0) {
68 FUNCTION_MSG("device-type = CDROM\n");
69 xvdd->device_type = XENVBD_DEVICETYPE_CDROM;
70 } else {
71 FUNCTION_MSG("device-type = %s (This probably won't work!)\n", device_type);
72 xvdd->device_type = XENVBD_DEVICETYPE_UNKNOWN;
73 }
74 XnFreeMem(xvdd->handle, device_type);
75 if (!(((qemu_hide_flags_value & QEMU_UNPLUG_ALL_IDE_DISKS) && xvdd->device_type != XENVBD_DEVICETYPE_CDROM) || qemu_hide_filter)) {
76 /* we never set backend state active if we are inactive */
77 FUNCTION_MSG("Inactive\n");
78 XnCloseDevice(xvdd->handle);
79 xvdd->device_state = DEVICE_STATE_INACTIVE;
80 return STATUS_SUCCESS;
81 }
82 status = XnBindEvent(xvdd->handle, &xvdd->event_channel, XenVbd_HandleEventDIRQL, xvdd);
83 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "event-channel", xvdd->event_channel);
84 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "ring-ref", xvdd->sring_gref);
85 status = XnWriteString(xvdd->handle, XN_BASE_FRONTEND, "protocol", ABI_PROTOCOL);
86 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "state", XenbusStateInitialised);
88 while (xvdd->backend_state != XenbusStateConnected) {
89 FUNCTION_MSG("waiting for XenbusStateConnected, backend_state = %d\n", xvdd->backend_state);
90 KeWaitForSingleObject(&xvdd->backend_event, Executive, KernelMode, FALSE, NULL);
91 }
93 // TODO: some of this stuff should be read on first connect only, then only verified on resume
94 xvdd->new_total_sectors = (ULONGLONG)-1L;
95 status = XnReadInt64(xvdd->handle, XN_BASE_BACKEND, "sectors", &xvdd->total_sectors);
96 status = XnReadInt32(xvdd->handle, XN_BASE_BACKEND, "sector-size", &xvdd->hw_bytes_per_sector);
97 if (xvdd->device_type == XENVBD_DEVICETYPE_CDROM) {
98 /* CD/DVD drives must have bytes_per_sector = 2048. */
99 xvdd->bytes_per_sector = 2048;
100 xvdd->hw_bytes_per_sector = 2048;
101 } else {
102 xvdd->bytes_per_sector = 512;
103 }
104 /* for some reason total_sectors is measured in 512 byte sectors always, so correct this to be in bytes_per_sectors */
105 xvdd->total_sectors /= xvdd->bytes_per_sector / 512;
106 status = XnReadInt32(xvdd->handle, XN_BASE_BACKEND, "feature-barrier", &xvdd->feature_barrier);
107 status = XnReadInt32(xvdd->handle, XN_BASE_BACKEND, "feature-discard", &xvdd->feature_discard);
108 status = XnReadInt32(xvdd->handle, XN_BASE_BACKEND, "feature-flush-cache", &xvdd->feature_flush_cache);
109 status = XnReadString(xvdd->handle, XN_BASE_BACKEND, "mode", &mode);
110 if (strncmp(mode, "r", 1) == 0) {
111 FUNCTION_MSG("mode = r\n");
112 xvdd->device_mode = XENVBD_DEVICEMODE_READ;
113 } else if (strncmp(mode, "w", 1) == 0) {
114 FUNCTION_MSG("mode = w\n");
115 xvdd->device_mode = XENVBD_DEVICEMODE_WRITE;
116 } else {
117 FUNCTION_MSG("mode = unknown\n");
118 xvdd->device_mode = XENVBD_DEVICEMODE_UNKNOWN;
119 }
120 XnFreeMem(xvdd->handle, mode);
122 // read device-type
123 status = XnReadString(xvdd->handle, XN_BASE_FRONTEND, "device-type", &device_type);
124 if (strcmp(device_type, "disk") == 0) {
125 FUNCTION_MSG("device-type = Disk\n");
126 xvdd->device_type = XENVBD_DEVICETYPE_DISK;
127 } else if (strcmp(device_type, "cdrom") == 0) {
128 FUNCTION_MSG("device-type = CDROM\n");
129 xvdd->device_type = XENVBD_DEVICETYPE_CDROM;
130 } else {
131 FUNCTION_MSG("device-type = %s (This probably won't work!)\n", device_type);
132 xvdd->device_type = XENVBD_DEVICETYPE_UNKNOWN;
133 }
135 status = XnReadString(xvdd->handle, XN_BASE_FRONTEND, "device-type", &uuid);
136 if (status == STATUS_SUCCESS) {
137 RtlStringCbCopyA(xvdd->serial_number, ARRAY_SIZE(xvdd->serial_number), uuid);
138 XnFreeMem(xvdd->handle, uuid);
139 } else {
140 RtlStringCbCopyA(xvdd->serial_number, ARRAY_SIZE(xvdd->serial_number), " ");
141 }
143 XnFreeMem(xvdd->handle, device_type);
144 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "state", XenbusStateConnected);
146 if (xvdd->device_type == XENVBD_DEVICETYPE_UNKNOWN
147 || xvdd->sring == NULL
148 || xvdd->event_channel == 0
149 || xvdd->total_sectors == 0
150 || xvdd->hw_bytes_per_sector == 0) {
151 FUNCTION_MSG("Missing settings\n");
152 // TODO: fail how?
153 }
155 xvdd->device_state = DEVICE_STATE_ACTIVE;
156 XenVbd_StartRing(xvdd, suspend);
157 FUNCTION_EXIT();
158 return STATUS_SUCCESS;
159 }
161 static NTSTATUS
162 XenVbd_Disconnect(PVOID DeviceExtension, BOOLEAN suspend) {
163 NTSTATUS status;
164 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
166 if (xvdd->device_state == DEVICE_STATE_INACTIVE) {
167 /* state stays INACTIVE */
168 return STATUS_SUCCESS;
169 }
171 if (xvdd->device_state != DEVICE_STATE_ACTIVE) {
172 FUNCTION_MSG("state not DEVICE_STATE_ACTIVE, is %d instead\n", xvdd->device_state);
173 FUNCTION_EXIT();
174 return STATUS_SUCCESS;
175 }
177 /* StopRing must set XenbusStateClosing when ring is paused */
178 XenVbd_StopRing(xvdd, suspend);
180 while (xvdd->backend_state != XenbusStateClosing) {
181 FUNCTION_MSG("waiting for XenbusStateClosing, backend_state = %d\n", xvdd->backend_state);
182 KeWaitForSingleObject(&xvdd->backend_event, Executive, KernelMode, FALSE, NULL);
183 }
184 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "state", XenbusStateClosed);
185 while (xvdd->backend_state != XenbusStateClosed) {
186 FUNCTION_MSG("waiting for XenbusStateClosed, backend_state = %d\n", xvdd->backend_state);
187 KeWaitForSingleObject(&xvdd->backend_event, Executive, KernelMode, FALSE, NULL);
188 }
189 XnUnbindEvent(xvdd->handle, xvdd->event_channel);
190 XnEndAccess(xvdd->handle, xvdd->sring_gref, FALSE, xvdd->grant_tag);
191 ExFreePoolWithTag(xvdd->sring, XENVBD_POOL_TAG);
193 if (!suspend) {
194 XnCloseDevice(xvdd->handle);
195 }
196 xvdd->device_state = DEVICE_STATE_DISCONNECTED;
197 return STATUS_SUCCESS;
198 }
201 static VOID
202 XenVbd_DeviceCallback(PVOID context, ULONG callback_type, PVOID value) {
203 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)context;
204 ULONG state;
206 FUNCTION_ENTER();
208 switch (callback_type) {
209 case XN_DEVICE_CALLBACK_BACKEND_STATE:
210 state = (ULONG)(ULONG_PTR)value;
211 if (state == xvdd->backend_state) {
212 FUNCTION_MSG("same state %d\n", state);
213 /* could be rewriting same state because of size change */
214 if (xvdd->backend_state == XenbusStateConnected) {
215 /* just set the new value - it will be noticed sooner or later */
216 XnReadInt64(xvdd->handle, XN_BASE_BACKEND, "sectors", &xvdd->new_total_sectors);
217 }
218 FUNCTION_EXIT();
219 }
220 FUNCTION_MSG("XenBusState = %d -> %d\n", xvdd->backend_state, state);
221 xvdd->backend_state = state;
222 KeMemoryBarrier();
223 KeSetEvent(&xvdd->backend_event, 0, FALSE);
224 break;
225 case XN_DEVICE_CALLBACK_SUSPEND:
226 FUNCTION_MSG("XN_DEVICE_CALLBACK_SUSPEND\n");
227 XenVbd_Disconnect(xvdd, TRUE);
228 break;
229 case XN_DEVICE_CALLBACK_RESUME:
230 FUNCTION_MSG("XN_DEVICE_CALLBACK_RESUME\n");
231 XenVbd_Connect(xvdd, TRUE);
232 break;
233 }
234 FUNCTION_EXIT();
235 }