win-pvdrivers

view xenvbd_common/common_xen.h @ 1025:aa2e51f67f7c

Fix hibernate under Win8. Change debugprints.
author James Harper <james.harper@bendigoit.com.au>
date Tue Feb 19 15:14:53 2013 +1100 (2013-02-19)
parents 6547b07f3236
children 306c10cdcba1
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 status = XnReadInt64(xvdd->handle, XN_BASE_BACKEND, "sectors", &xvdd->total_sectors);
95 status = XnReadInt32(xvdd->handle, XN_BASE_BACKEND, "sector-size", &xvdd->hw_bytes_per_sector);
96 if (xvdd->device_type == XENVBD_DEVICETYPE_CDROM) {
97 /* CD/DVD drives must have bytes_per_sector = 2048. */
98 xvdd->bytes_per_sector = 2048;
99 xvdd->hw_bytes_per_sector = 2048;
100 } else {
101 xvdd->bytes_per_sector = 512;
102 }
103 /* for some reason total_sectors is measured in 512 byte sectors always, so correct this to be in bytes_per_sectors */
104 xvdd->total_sectors /= xvdd->bytes_per_sector / 512;
105 status = XnReadInt32(xvdd->handle, XN_BASE_BACKEND, "feature-barrier", &xvdd->feature_barrier);
106 status = XnReadInt32(xvdd->handle, XN_BASE_BACKEND, "feature-discard", &xvdd->feature_discard);
107 status = XnReadInt32(xvdd->handle, XN_BASE_BACKEND, "feature-flush-cache", &xvdd->feature_flush_cache);
108 status = XnReadString(xvdd->handle, XN_BASE_BACKEND, "mode", &mode);
109 if (strncmp(mode, "r", 1) == 0) {
110 FUNCTION_MSG("mode = r\n");
111 xvdd->device_mode = XENVBD_DEVICEMODE_READ;
112 } else if (strncmp(mode, "w", 1) == 0) {
113 FUNCTION_MSG("mode = w\n");
114 xvdd->device_mode = XENVBD_DEVICEMODE_WRITE;
115 } else {
116 FUNCTION_MSG("mode = unknown\n");
117 xvdd->device_mode = XENVBD_DEVICEMODE_UNKNOWN;
118 }
119 XnFreeMem(xvdd->handle, mode);
121 // read device-type
122 status = XnReadString(xvdd->handle, XN_BASE_FRONTEND, "device-type", &device_type);
123 if (strcmp(device_type, "disk") == 0) {
124 FUNCTION_MSG("device-type = Disk\n");
125 xvdd->device_type = XENVBD_DEVICETYPE_DISK;
126 } else if (strcmp(device_type, "cdrom") == 0) {
127 FUNCTION_MSG("device-type = CDROM\n");
128 xvdd->device_type = XENVBD_DEVICETYPE_CDROM;
129 } else {
130 FUNCTION_MSG("device-type = %s (This probably won't work!)\n", device_type);
131 xvdd->device_type = XENVBD_DEVICETYPE_UNKNOWN;
132 }
134 status = XnReadString(xvdd->handle, XN_BASE_FRONTEND, "device-type", &uuid);
135 if (status == STATUS_SUCCESS) {
136 RtlStringCbCopyA(xvdd->serial_number, ARRAY_SIZE(xvdd->serial_number), uuid);
137 XnFreeMem(xvdd->handle, uuid);
138 } else {
139 RtlStringCbCopyA(xvdd->serial_number, ARRAY_SIZE(xvdd->serial_number), " ");
140 }
142 XnFreeMem(xvdd->handle, device_type);
143 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "state", XenbusStateConnected);
145 if (xvdd->device_type == XENVBD_DEVICETYPE_UNKNOWN
146 || xvdd->sring == NULL
147 || xvdd->event_channel == 0
148 || xvdd->total_sectors == 0
149 || xvdd->hw_bytes_per_sector == 0) {
150 FUNCTION_MSG("Missing settings\n");
151 // TODO: fail how?
152 }
154 xvdd->device_state = DEVICE_STATE_ACTIVE;
155 XenVbd_StartRing(xvdd, suspend);
156 FUNCTION_EXIT();
157 return STATUS_SUCCESS;
158 }
160 static NTSTATUS
161 XenVbd_Disconnect(PVOID DeviceExtension, BOOLEAN suspend) {
162 NTSTATUS status;
163 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
165 if (xvdd->device_state == DEVICE_STATE_INACTIVE) {
166 /* state stays INACTIVE */
167 return STATUS_SUCCESS;
168 }
170 if (xvdd->device_state != DEVICE_STATE_ACTIVE) {
171 FUNCTION_MSG("state not DEVICE_STATE_ACTIVE, is %d instead\n", xvdd->device_state);
172 FUNCTION_EXIT();
173 return STATUS_SUCCESS;
174 }
176 /* StopRing must set XenbusStateClosing when ring is paused */
177 XenVbd_StopRing(xvdd, suspend);
179 while (xvdd->backend_state != XenbusStateClosing) {
180 FUNCTION_MSG("waiting for XenbusStateClosing, backend_state = %d\n", xvdd->backend_state);
181 KeWaitForSingleObject(&xvdd->backend_event, Executive, KernelMode, FALSE, NULL);
182 }
183 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "state", XenbusStateClosed);
184 while (xvdd->backend_state != XenbusStateClosed) {
185 FUNCTION_MSG("waiting for XenbusStateClosed, backend_state = %d\n", xvdd->backend_state);
186 KeWaitForSingleObject(&xvdd->backend_event, Executive, KernelMode, FALSE, NULL);
187 }
188 XnUnbindEvent(xvdd->handle, xvdd->event_channel);
189 XnEndAccess(xvdd->handle, xvdd->sring_gref, FALSE, xvdd->grant_tag);
190 ExFreePoolWithTag(xvdd->sring, XENVBD_POOL_TAG);
192 if (!suspend) {
193 XnCloseDevice(xvdd->handle);
194 }
195 xvdd->device_state = DEVICE_STATE_DISCONNECTED;
196 return STATUS_SUCCESS;
197 }
199 static VOID
200 XenVbd_DeviceCallback(PVOID context, ULONG callback_type, PVOID value) {
201 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)context;
202 ULONG state;
204 FUNCTION_ENTER();
206 switch (callback_type) {
207 case XN_DEVICE_CALLBACK_BACKEND_STATE:
208 state = (ULONG)(ULONG_PTR)value;
209 if (state == xvdd->backend_state) {
210 FUNCTION_MSG("same state %d\n", state);
211 FUNCTION_EXIT();
212 }
213 FUNCTION_MSG("XenBusState = %d -> %d\n", xvdd->backend_state, state);
214 xvdd->backend_state = state;
215 KeMemoryBarrier();
216 KeSetEvent(&xvdd->backend_event, 0, FALSE);
217 break;
218 case XN_DEVICE_CALLBACK_SUSPEND:
219 FUNCTION_MSG("XN_DEVICE_CALLBACK_SUSPEND\n");
220 XenVbd_Disconnect(xvdd, TRUE);
221 break;
222 case XN_DEVICE_CALLBACK_RESUME:
223 FUNCTION_MSG("XN_DEVICE_CALLBACK_RESUME\n");
224 XenVbd_Connect(xvdd, TRUE);
225 break;
226 }
227 FUNCTION_EXIT();
228 }