win-pvdrivers

view xenvbd_common/common_xen.h @ 1018:9c6af8d0bdb6

fix problem with stopping xenvbd. Tidyup.
author James Harper <james.harper@bendigoit.com.au>
date Tue Feb 12 20:45:08 2013 +1100 (2013-02-12)
parents 4f7d5a8636bd
children 6547b07f3236
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 PFN_NUMBER pfn;
30 FUNCTION_ENTER();
32 if (xvdd->device_state != DEVICE_STATE_DISCONNECTED) {
33 FUNCTION_MSG("state not DEVICE_STATE_DISCONNECTED, is %d instead\n", xvdd->device_state);
34 FUNCTION_EXIT();
35 return STATUS_SUCCESS;
36 }
38 xvdd->backend_state = XenbusStateUnknown;
39 if (!suspend) {
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 return STATUS_UNSUCCESSFUL;
48 }
49 pfn = (PFN_NUMBER)MmGetPhysicalAddress(xvdd->sring).QuadPart >> PAGE_SHIFT;
50 xvdd->sring_gref = XnGrantAccess(xvdd->handle, (ULONG)pfn, FALSE, INVALID_GRANT_REF, xvdd->grant_tag);
51 SHARED_RING_INIT(xvdd->sring);
52 FRONT_RING_INIT(&xvdd->ring, xvdd->sring, PAGE_SIZE);
54 while (xvdd->backend_state != XenbusStateInitialising &&
55 xvdd->backend_state != XenbusStateInitWait &&
56 xvdd->backend_state != XenbusStateInitialised) {
57 FUNCTION_MSG("waiting for XenbusStateInitXxx, backend_state = %d\n", xvdd->backend_state);
58 KeWaitForSingleObject(&xvdd->backend_event, Executive, KernelMode, FALSE, NULL);
59 }
60 XnGetValue(xvdd->handle, XN_VALUE_TYPE_QEMU_HIDE_FLAGS, &qemu_hide_flags_value);
61 XnGetValue(xvdd->handle, XN_VALUE_TYPE_QEMU_FILTER, &qemu_hide_filter);
62 status = XnReadString(xvdd->handle, XN_BASE_FRONTEND, "device-type", &device_type);
63 if (strcmp(device_type, "disk") == 0) {
64 FUNCTION_MSG("device-type = Disk\n");
65 xvdd->device_type = XENVBD_DEVICETYPE_DISK;
66 } else if (strcmp(device_type, "cdrom") == 0) {
67 FUNCTION_MSG("device-type = CDROM\n");
68 xvdd->device_type = XENVBD_DEVICETYPE_CDROM;
69 } else {
70 FUNCTION_MSG("device-type = %s (This probably won't work!)\n", device_type);
71 xvdd->device_type = XENVBD_DEVICETYPE_UNKNOWN;
72 }
73 XnFreeMem(xvdd->handle, device_type);
74 if (!(((qemu_hide_flags_value & QEMU_UNPLUG_ALL_IDE_DISKS) && xvdd->device_type != XENVBD_DEVICETYPE_CDROM) || qemu_hide_filter)) {
75 /* we never set backend state active if we are inactive */
76 FUNCTION_MSG("Inactive\n");
77 XnCloseDevice(xvdd->handle);
78 xvdd->device_state = DEVICE_STATE_INACTIVE;
79 return STATUS_SUCCESS;
80 }
81 status = XnBindEvent(xvdd->handle, &xvdd->event_channel, XenVbd_HandleEventDIRQL, xvdd);
82 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "event-channel", xvdd->event_channel);
83 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "ring-ref", xvdd->sring_gref);
84 status = XnWriteString(xvdd->handle, XN_BASE_FRONTEND, "protocol", ABI_PROTOCOL);
85 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "state", XenbusStateInitialised);
87 while (xvdd->backend_state != XenbusStateConnected) {
88 FUNCTION_MSG("waiting for XenbusStateConnected, backend_state = %d\n", xvdd->backend_state);
89 KeWaitForSingleObject(&xvdd->backend_event, Executive, KernelMode, FALSE, NULL);
90 }
92 // TODO: some of this stuff should be read on first connect only, then only verified on resume
93 status = XnReadInt64(xvdd->handle, XN_BASE_BACKEND, "sectors", &xvdd->total_sectors);
94 status = XnReadInt32(xvdd->handle, XN_BASE_BACKEND, "sector-size", &xvdd->hw_bytes_per_sector);
95 if (xvdd->device_type == XENVBD_DEVICETYPE_CDROM) {
96 /* CD/DVD drives must have bytes_per_sector = 2048. */
97 xvdd->bytes_per_sector = 2048;
98 xvdd->hw_bytes_per_sector = 2048;
99 } else {
100 xvdd->bytes_per_sector = 512;
101 }
102 /* for some reason total_sectors is measured in 512 byte sectors always, so correct this to be in bytes_per_sectors */
103 xvdd->total_sectors /= xvdd->bytes_per_sector / 512;
104 status = XnReadInt32(xvdd->handle, XN_BASE_BACKEND, "feature-barrier", &xvdd->feature_barrier);
105 status = XnReadInt32(xvdd->handle, XN_BASE_BACKEND, "feature-discard", &xvdd->feature_discard);
106 status = XnReadInt32(xvdd->handle, XN_BASE_BACKEND, "feature-flush-cache", &xvdd->feature_flush_cache);
107 status = XnReadString(xvdd->handle, XN_BASE_BACKEND, "mode", &mode);
108 if (strncmp(mode, "r", 1) == 0) {
109 FUNCTION_MSG("mode = r\n");
110 xvdd->device_mode = XENVBD_DEVICEMODE_READ;
111 } else if (strncmp(mode, "w", 1) == 0) {
112 FUNCTION_MSG("mode = w\n");
113 xvdd->device_mode = XENVBD_DEVICEMODE_WRITE;
114 } else {
115 FUNCTION_MSG("mode = unknown\n");
116 xvdd->device_mode = XENVBD_DEVICEMODE_UNKNOWN;
117 }
118 XnFreeMem(xvdd->handle, mode);
120 // read device-type
121 status = XnReadString(xvdd->handle, XN_BASE_FRONTEND, "device-type", &device_type);
122 if (strcmp(device_type, "disk") == 0) {
123 FUNCTION_MSG("device-type = Disk\n");
124 xvdd->device_type = XENVBD_DEVICETYPE_DISK;
125 } else if (strcmp(device_type, "cdrom") == 0) {
126 FUNCTION_MSG("device-type = CDROM\n");
127 xvdd->device_type = XENVBD_DEVICETYPE_CDROM;
128 } else {
129 FUNCTION_MSG("device-type = %s (This probably won't work!)\n", device_type);
130 xvdd->device_type = XENVBD_DEVICETYPE_UNKNOWN;
131 }
132 XnFreeMem(xvdd->handle, device_type);
133 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "state", XenbusStateConnected);
135 if (xvdd->device_type == XENVBD_DEVICETYPE_UNKNOWN
136 || xvdd->sring == NULL
137 || xvdd->event_channel == 0
138 || xvdd->total_sectors == 0
139 || xvdd->hw_bytes_per_sector == 0) {
140 FUNCTION_MSG("Missing settings\n");
141 // TODO: fail how?
142 }
144 xvdd->device_state = DEVICE_STATE_ACTIVE;
145 XenVbd_StartRing(xvdd, suspend);
146 FUNCTION_EXIT();
147 return STATUS_SUCCESS;
148 }
150 static NTSTATUS
151 XenVbd_Disconnect(PVOID DeviceExtension, BOOLEAN suspend) {
152 NTSTATUS status;
153 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
154 PFN_NUMBER pfn;
156 if (xvdd->device_state == DEVICE_STATE_INACTIVE) {
157 /* state stays INACTIVE */
158 return STATUS_SUCCESS;
159 }
161 if (xvdd->device_state != DEVICE_STATE_ACTIVE) {
162 FUNCTION_MSG("state not DEVICE_STATE_ACTIVE, is %d instead\n", xvdd->device_state);
163 FUNCTION_EXIT();
164 return STATUS_SUCCESS;
165 }
167 /* StopRing must set XenbusStateClosing when ring is paused */
168 XenVbd_StopRing(xvdd, suspend);
170 while (xvdd->backend_state != XenbusStateClosing) {
171 FUNCTION_MSG("waiting for XenbusStateClosing, backend_state = %d\n", xvdd->backend_state);
172 KeWaitForSingleObject(&xvdd->backend_event, Executive, KernelMode, FALSE, NULL);
173 }
174 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "state", XenbusStateClosed);
175 while (xvdd->backend_state != XenbusStateClosed) {
176 FUNCTION_MSG("waiting for XenbusStateClosed, backend_state = %d\n", xvdd->backend_state);
177 KeWaitForSingleObject(&xvdd->backend_event, Executive, KernelMode, FALSE, NULL);
178 }
179 XnUnbindEvent(xvdd->handle, xvdd->event_channel);
180 pfn = (PFN_NUMBER)MmGetPhysicalAddress(xvdd->sring).QuadPart >> PAGE_SHIFT;
181 XnEndAccess(xvdd->handle, xvdd->sring_gref, FALSE, xvdd->grant_tag);
182 ExFreePoolWithTag(xvdd->sring, XENVBD_POOL_TAG);
184 if (!suspend) {
185 XnCloseDevice(xvdd->handle);
186 }
187 xvdd->device_state = DEVICE_STATE_DISCONNECTED;
188 return STATUS_SUCCESS;
189 }
191 static VOID
192 XenVbd_DeviceCallback(PVOID context, ULONG callback_type, PVOID value) {
193 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)context;
194 ULONG state;
196 FUNCTION_ENTER();
198 switch (callback_type) {
199 case XN_DEVICE_CALLBACK_BACKEND_STATE:
200 state = (ULONG)(ULONG_PTR)value;
201 if (state == xvdd->backend_state) {
202 FUNCTION_MSG("same state %d\n", state);
203 FUNCTION_EXIT();
204 }
205 FUNCTION_MSG("XenBusState = %d -> %d\n", xvdd->backend_state, state);
206 xvdd->backend_state = state;
207 KeMemoryBarrier();
208 KeSetEvent(&xvdd->backend_event, 0, FALSE);
209 break;
210 case XN_DEVICE_CALLBACK_SUSPEND:
211 FUNCTION_MSG("XN_DEVICE_CALLBACK_SUSPEND\n");
212 XenVbd_Disconnect(xvdd, TRUE);
213 break;
214 case XN_DEVICE_CALLBACK_RESUME:
215 FUNCTION_MSG("XN_DEVICE_CALLBACK_RESUME\n");
216 XenVbd_Connect(xvdd, TRUE);
217 break;
218 }
219 FUNCTION_EXIT();
220 }