win-pvdrivers

annotate 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
rev   line source
james@1005 1 /*
james@1005 2 PV Drivers for Windows Xen HVM Domains
james@1005 3 Copyright (C) 2013 James Harper
james@1005 4
james@1005 5 This program is free software; you can redistribute it and/or
james@1005 6 modify it under the terms of the GNU General Public License
james@1005 7 as published by the Free Software Foundation; either version 2
james@1005 8 of the License, or (at your option) any later version.
james@1005 9
james@1005 10 This program is distributed in the hope that it will be useful,
james@1005 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
james@1005 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
james@1005 13 GNU General Public License for more details.
james@1005 14
james@1005 15 You should have received a copy of the GNU General Public License
james@1005 16 along with this program; if not, write to the Free Software
james@1005 17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
james@1005 18 */
james@1005 19
james@1005 20 static NTSTATUS
james@1005 21 XenVbd_Connect(PXENVBD_DEVICE_DATA xvdd, BOOLEAN suspend) {
james@1005 22 BOOLEAN qemu_hide_filter = FALSE;
james@1005 23 ULONG qemu_hide_flags_value = 0;
james@1005 24 PCHAR device_type;
james@1005 25 //BOOLEAN active = FALSE;
james@1005 26 NTSTATUS status;
james@1005 27 PCHAR mode;
james@1005 28 PFN_NUMBER pfn;
james@1005 29
james@1005 30 FUNCTION_ENTER();
james@1005 31
james@1005 32 if (xvdd->device_state != DEVICE_STATE_DISCONNECTED) {
james@1005 33 FUNCTION_MSG("state not DEVICE_STATE_DISCONNECTED, is %d instead\n", xvdd->device_state);
james@1005 34 FUNCTION_EXIT();
james@1005 35 return STATUS_SUCCESS;
james@1005 36 }
james@1005 37
james@1005 38 xvdd->backend_state = XenbusStateUnknown;
james@1005 39 if (!suspend) {
james@1005 40 if ((xvdd->handle = XnOpenDevice(xvdd->pdo, XenVbd_DeviceCallback, xvdd)) == NULL) {
james@1005 41 FUNCTION_MSG("Failed to open\n");
james@1005 42 return STATUS_UNSUCCESSFUL;
james@1005 43 }
james@1005 44 }
james@1005 45 xvdd->sring = (blkif_sring_t *)ExAllocatePoolWithTag(NonPagedPool, PAGE_SIZE, XENVBD_POOL_TAG);
james@1005 46 if (!xvdd->sring) {
james@1005 47 return STATUS_UNSUCCESSFUL;
james@1005 48 }
james@1005 49 pfn = (PFN_NUMBER)MmGetPhysicalAddress(xvdd->sring).QuadPart >> PAGE_SHIFT;
james@1005 50 xvdd->sring_gref = XnGrantAccess(xvdd->handle, (ULONG)pfn, FALSE, INVALID_GRANT_REF, xvdd->grant_tag);
james@1005 51 SHARED_RING_INIT(xvdd->sring);
james@1005 52 FRONT_RING_INIT(&xvdd->ring, xvdd->sring, PAGE_SIZE);
james@1005 53
james@1005 54 while (xvdd->backend_state != XenbusStateInitialising &&
james@1005 55 xvdd->backend_state != XenbusStateInitWait &&
james@1005 56 xvdd->backend_state != XenbusStateInitialised) {
james@1005 57 FUNCTION_MSG("waiting for XenbusStateInitXxx, backend_state = %d\n", xvdd->backend_state);
james@1005 58 KeWaitForSingleObject(&xvdd->backend_event, Executive, KernelMode, FALSE, NULL);
james@1005 59 }
james@1005 60 XnGetValue(xvdd->handle, XN_VALUE_TYPE_QEMU_HIDE_FLAGS, &qemu_hide_flags_value);
james@1005 61 XnGetValue(xvdd->handle, XN_VALUE_TYPE_QEMU_FILTER, &qemu_hide_filter);
james@1005 62 status = XnReadString(xvdd->handle, XN_BASE_FRONTEND, "device-type", &device_type);
james@1005 63 if (strcmp(device_type, "disk") == 0) {
james@1005 64 FUNCTION_MSG("device-type = Disk\n");
james@1005 65 xvdd->device_type = XENVBD_DEVICETYPE_DISK;
james@1005 66 } else if (strcmp(device_type, "cdrom") == 0) {
james@1005 67 FUNCTION_MSG("device-type = CDROM\n");
james@1005 68 xvdd->device_type = XENVBD_DEVICETYPE_CDROM;
james@1005 69 } else {
james@1005 70 FUNCTION_MSG("device-type = %s (This probably won't work!)\n", device_type);
james@1005 71 xvdd->device_type = XENVBD_DEVICETYPE_UNKNOWN;
james@1005 72 }
james@1005 73 XnFreeMem(xvdd->handle, device_type);
james@1005 74 if (!(((qemu_hide_flags_value & QEMU_UNPLUG_ALL_IDE_DISKS) && xvdd->device_type != XENVBD_DEVICETYPE_CDROM) || qemu_hide_filter)) {
james@1005 75 /* we never set backend state active if we are inactive */
james@1005 76 FUNCTION_MSG("Inactive\n");
james@1018 77 XnCloseDevice(xvdd->handle);
james@1005 78 xvdd->device_state = DEVICE_STATE_INACTIVE;
james@1005 79 return STATUS_SUCCESS;
james@1005 80 }
james@1005 81 status = XnBindEvent(xvdd->handle, &xvdd->event_channel, XenVbd_HandleEventDIRQL, xvdd);
james@1005 82 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "event-channel", xvdd->event_channel);
james@1005 83 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "ring-ref", xvdd->sring_gref);
james@1005 84 status = XnWriteString(xvdd->handle, XN_BASE_FRONTEND, "protocol", ABI_PROTOCOL);
james@1005 85 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "state", XenbusStateInitialised);
james@1005 86
james@1005 87 while (xvdd->backend_state != XenbusStateConnected) {
james@1005 88 FUNCTION_MSG("waiting for XenbusStateConnected, backend_state = %d\n", xvdd->backend_state);
james@1005 89 KeWaitForSingleObject(&xvdd->backend_event, Executive, KernelMode, FALSE, NULL);
james@1005 90 }
james@1005 91
james@1005 92 // TODO: some of this stuff should be read on first connect only, then only verified on resume
james@1005 93 status = XnReadInt64(xvdd->handle, XN_BASE_BACKEND, "sectors", &xvdd->total_sectors);
james@1005 94 status = XnReadInt32(xvdd->handle, XN_BASE_BACKEND, "sector-size", &xvdd->hw_bytes_per_sector);
james@1005 95 if (xvdd->device_type == XENVBD_DEVICETYPE_CDROM) {
james@1005 96 /* CD/DVD drives must have bytes_per_sector = 2048. */
james@1005 97 xvdd->bytes_per_sector = 2048;
james@1005 98 xvdd->hw_bytes_per_sector = 2048;
james@1005 99 } else {
james@1005 100 xvdd->bytes_per_sector = 512;
james@1005 101 }
james@1005 102 /* for some reason total_sectors is measured in 512 byte sectors always, so correct this to be in bytes_per_sectors */
james@1005 103 xvdd->total_sectors /= xvdd->bytes_per_sector / 512;
james@1005 104 status = XnReadInt32(xvdd->handle, XN_BASE_BACKEND, "feature-barrier", &xvdd->feature_barrier);
james@1005 105 status = XnReadInt32(xvdd->handle, XN_BASE_BACKEND, "feature-discard", &xvdd->feature_discard);
james@1005 106 status = XnReadInt32(xvdd->handle, XN_BASE_BACKEND, "feature-flush-cache", &xvdd->feature_flush_cache);
james@1005 107 status = XnReadString(xvdd->handle, XN_BASE_BACKEND, "mode", &mode);
james@1005 108 if (strncmp(mode, "r", 1) == 0) {
james@1005 109 FUNCTION_MSG("mode = r\n");
james@1005 110 xvdd->device_mode = XENVBD_DEVICEMODE_READ;
james@1005 111 } else if (strncmp(mode, "w", 1) == 0) {
james@1005 112 FUNCTION_MSG("mode = w\n");
james@1005 113 xvdd->device_mode = XENVBD_DEVICEMODE_WRITE;
james@1005 114 } else {
james@1005 115 FUNCTION_MSG("mode = unknown\n");
james@1005 116 xvdd->device_mode = XENVBD_DEVICEMODE_UNKNOWN;
james@1005 117 }
james@1005 118 XnFreeMem(xvdd->handle, mode);
james@1005 119
james@1005 120 // read device-type
james@1005 121 status = XnReadString(xvdd->handle, XN_BASE_FRONTEND, "device-type", &device_type);
james@1005 122 if (strcmp(device_type, "disk") == 0) {
james@1005 123 FUNCTION_MSG("device-type = Disk\n");
james@1005 124 xvdd->device_type = XENVBD_DEVICETYPE_DISK;
james@1005 125 } else if (strcmp(device_type, "cdrom") == 0) {
james@1005 126 FUNCTION_MSG("device-type = CDROM\n");
james@1005 127 xvdd->device_type = XENVBD_DEVICETYPE_CDROM;
james@1005 128 } else {
james@1005 129 FUNCTION_MSG("device-type = %s (This probably won't work!)\n", device_type);
james@1005 130 xvdd->device_type = XENVBD_DEVICETYPE_UNKNOWN;
james@1005 131 }
james@1005 132 XnFreeMem(xvdd->handle, device_type);
james@1005 133 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "state", XenbusStateConnected);
james@1005 134
james@1005 135 if (xvdd->device_type == XENVBD_DEVICETYPE_UNKNOWN
james@1005 136 || xvdd->sring == NULL
james@1005 137 || xvdd->event_channel == 0
james@1005 138 || xvdd->total_sectors == 0
james@1005 139 || xvdd->hw_bytes_per_sector == 0) {
james@1005 140 FUNCTION_MSG("Missing settings\n");
james@1005 141 // TODO: fail how?
james@1005 142 }
james@1005 143
james@1005 144 xvdd->device_state = DEVICE_STATE_ACTIVE;
james@1005 145 XenVbd_StartRing(xvdd, suspend);
james@1005 146 FUNCTION_EXIT();
james@1005 147 return STATUS_SUCCESS;
james@1005 148 }
james@1005 149
james@1005 150 static NTSTATUS
james@1005 151 XenVbd_Disconnect(PVOID DeviceExtension, BOOLEAN suspend) {
james@1005 152 NTSTATUS status;
james@1005 153 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
james@1005 154 PFN_NUMBER pfn;
james@1005 155
james@1018 156 if (xvdd->device_state == DEVICE_STATE_INACTIVE) {
james@1018 157 /* state stays INACTIVE */
james@1018 158 return STATUS_SUCCESS;
james@1018 159 }
james@1018 160
james@1005 161 if (xvdd->device_state != DEVICE_STATE_ACTIVE) {
james@1005 162 FUNCTION_MSG("state not DEVICE_STATE_ACTIVE, is %d instead\n", xvdd->device_state);
james@1005 163 FUNCTION_EXIT();
james@1005 164 return STATUS_SUCCESS;
james@1005 165 }
james@1005 166
james@1005 167 /* StopRing must set XenbusStateClosing when ring is paused */
james@1005 168 XenVbd_StopRing(xvdd, suspend);
james@1005 169
james@1005 170 while (xvdd->backend_state != XenbusStateClosing) {
james@1005 171 FUNCTION_MSG("waiting for XenbusStateClosing, backend_state = %d\n", xvdd->backend_state);
james@1005 172 KeWaitForSingleObject(&xvdd->backend_event, Executive, KernelMode, FALSE, NULL);
james@1005 173 }
james@1005 174 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "state", XenbusStateClosed);
james@1005 175 while (xvdd->backend_state != XenbusStateClosed) {
james@1005 176 FUNCTION_MSG("waiting for XenbusStateClosed, backend_state = %d\n", xvdd->backend_state);
james@1005 177 KeWaitForSingleObject(&xvdd->backend_event, Executive, KernelMode, FALSE, NULL);
james@1005 178 }
james@1005 179 XnUnbindEvent(xvdd->handle, xvdd->event_channel);
james@1005 180 pfn = (PFN_NUMBER)MmGetPhysicalAddress(xvdd->sring).QuadPart >> PAGE_SHIFT;
james@1005 181 XnEndAccess(xvdd->handle, xvdd->sring_gref, FALSE, xvdd->grant_tag);
james@1005 182 ExFreePoolWithTag(xvdd->sring, XENVBD_POOL_TAG);
james@1005 183
james@1005 184 if (!suspend) {
james@1005 185 XnCloseDevice(xvdd->handle);
james@1005 186 }
james@1005 187 xvdd->device_state = DEVICE_STATE_DISCONNECTED;
james@1005 188 return STATUS_SUCCESS;
james@1005 189 }
james@1005 190
james@1005 191 static VOID
james@1005 192 XenVbd_DeviceCallback(PVOID context, ULONG callback_type, PVOID value) {
james@1005 193 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)context;
james@1005 194 ULONG state;
james@1005 195
james@1005 196 FUNCTION_ENTER();
james@1005 197
james@1005 198 switch (callback_type) {
james@1005 199 case XN_DEVICE_CALLBACK_BACKEND_STATE:
james@1005 200 state = (ULONG)(ULONG_PTR)value;
james@1005 201 if (state == xvdd->backend_state) {
james@1005 202 FUNCTION_MSG("same state %d\n", state);
james@1005 203 FUNCTION_EXIT();
james@1005 204 }
james@1005 205 FUNCTION_MSG("XenBusState = %d -> %d\n", xvdd->backend_state, state);
james@1005 206 xvdd->backend_state = state;
james@1005 207 KeMemoryBarrier();
james@1005 208 KeSetEvent(&xvdd->backend_event, 0, FALSE);
james@1005 209 break;
james@1005 210 case XN_DEVICE_CALLBACK_SUSPEND:
james@1005 211 FUNCTION_MSG("XN_DEVICE_CALLBACK_SUSPEND\n");
james@1005 212 XenVbd_Disconnect(xvdd, TRUE);
james@1005 213 break;
james@1005 214 case XN_DEVICE_CALLBACK_RESUME:
james@1005 215 FUNCTION_MSG("XN_DEVICE_CALLBACK_RESUME\n");
james@1005 216 XenVbd_Connect(xvdd, TRUE);
james@1005 217 break;
james@1005 218 }
james@1005 219 FUNCTION_EXIT();
james@1005 220 }