win-pvdrivers

view xenvbd_common/common_xen.h @ 1005:4f7d5a8636bd

xenvbd rewrite. combined scsiport and storport code where possible.
author James Harper <james.harper@bendigoit.com.au>
date Sat Feb 02 18:51:07 2013 +1100 (2013-02-02)
parents
children 9c6af8d0bdb6
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 xvdd->device_state = DEVICE_STATE_INACTIVE;
78 /* still need to call StartRing here */
79 XenVbd_StartRing(xvdd, suspend);
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 }
133 XnFreeMem(xvdd->handle, device_type);
134 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "state", XenbusStateConnected);
136 if (xvdd->device_type == XENVBD_DEVICETYPE_UNKNOWN
137 || xvdd->sring == NULL
138 || xvdd->event_channel == 0
139 || xvdd->total_sectors == 0
140 || xvdd->hw_bytes_per_sector == 0) {
141 FUNCTION_MSG("Missing settings\n");
142 // TODO: fail how?
143 }
145 xvdd->device_state = DEVICE_STATE_ACTIVE;
146 XenVbd_StartRing(xvdd, suspend);
147 FUNCTION_EXIT();
148 return STATUS_SUCCESS;
149 }
151 static NTSTATUS
152 XenVbd_Disconnect(PVOID DeviceExtension, BOOLEAN suspend) {
153 NTSTATUS status;
154 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
155 PFN_NUMBER pfn;
157 if (xvdd->device_state != DEVICE_STATE_ACTIVE) {
158 FUNCTION_MSG("state not DEVICE_STATE_ACTIVE, is %d instead\n", xvdd->device_state);
159 FUNCTION_EXIT();
160 return STATUS_SUCCESS;
161 }
163 /* StopRing must set XenbusStateClosing when ring is paused */
164 XenVbd_StopRing(xvdd, suspend);
166 while (xvdd->backend_state != XenbusStateClosing) {
167 FUNCTION_MSG("waiting for XenbusStateClosing, backend_state = %d\n", xvdd->backend_state);
168 KeWaitForSingleObject(&xvdd->backend_event, Executive, KernelMode, FALSE, NULL);
169 }
170 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "state", XenbusStateClosed);
171 while (xvdd->backend_state != XenbusStateClosed) {
172 FUNCTION_MSG("waiting for XenbusStateClosed, backend_state = %d\n", xvdd->backend_state);
173 KeWaitForSingleObject(&xvdd->backend_event, Executive, KernelMode, FALSE, NULL);
174 }
175 XnUnbindEvent(xvdd->handle, xvdd->event_channel);
176 pfn = (PFN_NUMBER)MmGetPhysicalAddress(xvdd->sring).QuadPart >> PAGE_SHIFT;
177 XnEndAccess(xvdd->handle, xvdd->sring_gref, FALSE, xvdd->grant_tag);
178 ExFreePoolWithTag(xvdd->sring, XENVBD_POOL_TAG);
180 if (!suspend) {
181 XnCloseDevice(xvdd->handle);
182 }
183 xvdd->device_state = DEVICE_STATE_DISCONNECTED;
184 return STATUS_SUCCESS;
185 }
187 static VOID
188 XenVbd_DeviceCallback(PVOID context, ULONG callback_type, PVOID value) {
189 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)context;
190 ULONG state;
192 FUNCTION_ENTER();
194 switch (callback_type) {
195 case XN_DEVICE_CALLBACK_BACKEND_STATE:
196 state = (ULONG)(ULONG_PTR)value;
197 if (state == xvdd->backend_state) {
198 FUNCTION_MSG("same state %d\n", state);
199 FUNCTION_EXIT();
200 }
201 FUNCTION_MSG("XenBusState = %d -> %d\n", xvdd->backend_state, state);
202 xvdd->backend_state = state;
203 KeMemoryBarrier();
204 KeSetEvent(&xvdd->backend_event, 0, FALSE);
205 break;
206 case XN_DEVICE_CALLBACK_SUSPEND:
207 FUNCTION_MSG("XN_DEVICE_CALLBACK_SUSPEND\n");
208 XenVbd_Disconnect(xvdd, TRUE);
209 break;
210 case XN_DEVICE_CALLBACK_RESUME:
211 FUNCTION_MSG("XN_DEVICE_CALLBACK_RESUME\n");
212 XenVbd_Connect(xvdd, TRUE);
213 break;
214 }
215 FUNCTION_EXIT();
216 }