win-pvdrivers

annotate 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
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@1005 77 xvdd->device_state = DEVICE_STATE_INACTIVE;
james@1005 78 /* still need to call StartRing here */
james@1005 79 XenVbd_StartRing(xvdd, suspend);
james@1005 80 return STATUS_SUCCESS;
james@1005 81 }
james@1005 82 status = XnBindEvent(xvdd->handle, &xvdd->event_channel, XenVbd_HandleEventDIRQL, xvdd);
james@1005 83 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "event-channel", xvdd->event_channel);
james@1005 84 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "ring-ref", xvdd->sring_gref);
james@1005 85 status = XnWriteString(xvdd->handle, XN_BASE_FRONTEND, "protocol", ABI_PROTOCOL);
james@1005 86 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "state", XenbusStateInitialised);
james@1005 87
james@1005 88 while (xvdd->backend_state != XenbusStateConnected) {
james@1005 89 FUNCTION_MSG("waiting for XenbusStateConnected, backend_state = %d\n", xvdd->backend_state);
james@1005 90 KeWaitForSingleObject(&xvdd->backend_event, Executive, KernelMode, FALSE, NULL);
james@1005 91 }
james@1005 92
james@1005 93 // TODO: some of this stuff should be read on first connect only, then only verified on resume
james@1005 94 status = XnReadInt64(xvdd->handle, XN_BASE_BACKEND, "sectors", &xvdd->total_sectors);
james@1005 95 status = XnReadInt32(xvdd->handle, XN_BASE_BACKEND, "sector-size", &xvdd->hw_bytes_per_sector);
james@1005 96 if (xvdd->device_type == XENVBD_DEVICETYPE_CDROM) {
james@1005 97 /* CD/DVD drives must have bytes_per_sector = 2048. */
james@1005 98 xvdd->bytes_per_sector = 2048;
james@1005 99 xvdd->hw_bytes_per_sector = 2048;
james@1005 100 } else {
james@1005 101 xvdd->bytes_per_sector = 512;
james@1005 102 }
james@1005 103 /* for some reason total_sectors is measured in 512 byte sectors always, so correct this to be in bytes_per_sectors */
james@1005 104 xvdd->total_sectors /= xvdd->bytes_per_sector / 512;
james@1005 105 status = XnReadInt32(xvdd->handle, XN_BASE_BACKEND, "feature-barrier", &xvdd->feature_barrier);
james@1005 106 status = XnReadInt32(xvdd->handle, XN_BASE_BACKEND, "feature-discard", &xvdd->feature_discard);
james@1005 107 status = XnReadInt32(xvdd->handle, XN_BASE_BACKEND, "feature-flush-cache", &xvdd->feature_flush_cache);
james@1005 108 status = XnReadString(xvdd->handle, XN_BASE_BACKEND, "mode", &mode);
james@1005 109 if (strncmp(mode, "r", 1) == 0) {
james@1005 110 FUNCTION_MSG("mode = r\n");
james@1005 111 xvdd->device_mode = XENVBD_DEVICEMODE_READ;
james@1005 112 } else if (strncmp(mode, "w", 1) == 0) {
james@1005 113 FUNCTION_MSG("mode = w\n");
james@1005 114 xvdd->device_mode = XENVBD_DEVICEMODE_WRITE;
james@1005 115 } else {
james@1005 116 FUNCTION_MSG("mode = unknown\n");
james@1005 117 xvdd->device_mode = XENVBD_DEVICEMODE_UNKNOWN;
james@1005 118 }
james@1005 119 XnFreeMem(xvdd->handle, mode);
james@1005 120
james@1005 121 // read device-type
james@1005 122 status = XnReadString(xvdd->handle, XN_BASE_FRONTEND, "device-type", &device_type);
james@1005 123 if (strcmp(device_type, "disk") == 0) {
james@1005 124 FUNCTION_MSG("device-type = Disk\n");
james@1005 125 xvdd->device_type = XENVBD_DEVICETYPE_DISK;
james@1005 126 } else if (strcmp(device_type, "cdrom") == 0) {
james@1005 127 FUNCTION_MSG("device-type = CDROM\n");
james@1005 128 xvdd->device_type = XENVBD_DEVICETYPE_CDROM;
james@1005 129 } else {
james@1005 130 FUNCTION_MSG("device-type = %s (This probably won't work!)\n", device_type);
james@1005 131 xvdd->device_type = XENVBD_DEVICETYPE_UNKNOWN;
james@1005 132 }
james@1005 133 XnFreeMem(xvdd->handle, device_type);
james@1005 134 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "state", XenbusStateConnected);
james@1005 135
james@1005 136 if (xvdd->device_type == XENVBD_DEVICETYPE_UNKNOWN
james@1005 137 || xvdd->sring == NULL
james@1005 138 || xvdd->event_channel == 0
james@1005 139 || xvdd->total_sectors == 0
james@1005 140 || xvdd->hw_bytes_per_sector == 0) {
james@1005 141 FUNCTION_MSG("Missing settings\n");
james@1005 142 // TODO: fail how?
james@1005 143 }
james@1005 144
james@1005 145 xvdd->device_state = DEVICE_STATE_ACTIVE;
james@1005 146 XenVbd_StartRing(xvdd, suspend);
james@1005 147 FUNCTION_EXIT();
james@1005 148 return STATUS_SUCCESS;
james@1005 149 }
james@1005 150
james@1005 151 static NTSTATUS
james@1005 152 XenVbd_Disconnect(PVOID DeviceExtension, BOOLEAN suspend) {
james@1005 153 NTSTATUS status;
james@1005 154 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)DeviceExtension;
james@1005 155 PFN_NUMBER pfn;
james@1005 156
james@1005 157 if (xvdd->device_state != DEVICE_STATE_ACTIVE) {
james@1005 158 FUNCTION_MSG("state not DEVICE_STATE_ACTIVE, is %d instead\n", xvdd->device_state);
james@1005 159 FUNCTION_EXIT();
james@1005 160 return STATUS_SUCCESS;
james@1005 161 }
james@1005 162
james@1005 163 /* StopRing must set XenbusStateClosing when ring is paused */
james@1005 164 XenVbd_StopRing(xvdd, suspend);
james@1005 165
james@1005 166 while (xvdd->backend_state != XenbusStateClosing) {
james@1005 167 FUNCTION_MSG("waiting for XenbusStateClosing, backend_state = %d\n", xvdd->backend_state);
james@1005 168 KeWaitForSingleObject(&xvdd->backend_event, Executive, KernelMode, FALSE, NULL);
james@1005 169 }
james@1005 170 status = XnWriteInt32(xvdd->handle, XN_BASE_FRONTEND, "state", XenbusStateClosed);
james@1005 171 while (xvdd->backend_state != XenbusStateClosed) {
james@1005 172 FUNCTION_MSG("waiting for XenbusStateClosed, backend_state = %d\n", xvdd->backend_state);
james@1005 173 KeWaitForSingleObject(&xvdd->backend_event, Executive, KernelMode, FALSE, NULL);
james@1005 174 }
james@1005 175 XnUnbindEvent(xvdd->handle, xvdd->event_channel);
james@1005 176 pfn = (PFN_NUMBER)MmGetPhysicalAddress(xvdd->sring).QuadPart >> PAGE_SHIFT;
james@1005 177 XnEndAccess(xvdd->handle, xvdd->sring_gref, FALSE, xvdd->grant_tag);
james@1005 178 ExFreePoolWithTag(xvdd->sring, XENVBD_POOL_TAG);
james@1005 179
james@1005 180 if (!suspend) {
james@1005 181 XnCloseDevice(xvdd->handle);
james@1005 182 }
james@1005 183 xvdd->device_state = DEVICE_STATE_DISCONNECTED;
james@1005 184 return STATUS_SUCCESS;
james@1005 185 }
james@1005 186
james@1005 187 static VOID
james@1005 188 XenVbd_DeviceCallback(PVOID context, ULONG callback_type, PVOID value) {
james@1005 189 PXENVBD_DEVICE_DATA xvdd = (PXENVBD_DEVICE_DATA)context;
james@1005 190 ULONG state;
james@1005 191
james@1005 192 FUNCTION_ENTER();
james@1005 193
james@1005 194 switch (callback_type) {
james@1005 195 case XN_DEVICE_CALLBACK_BACKEND_STATE:
james@1005 196 state = (ULONG)(ULONG_PTR)value;
james@1005 197 if (state == xvdd->backend_state) {
james@1005 198 FUNCTION_MSG("same state %d\n", state);
james@1005 199 FUNCTION_EXIT();
james@1005 200 }
james@1005 201 FUNCTION_MSG("XenBusState = %d -> %d\n", xvdd->backend_state, state);
james@1005 202 xvdd->backend_state = state;
james@1005 203 KeMemoryBarrier();
james@1005 204 KeSetEvent(&xvdd->backend_event, 0, FALSE);
james@1005 205 break;
james@1005 206 case XN_DEVICE_CALLBACK_SUSPEND:
james@1005 207 FUNCTION_MSG("XN_DEVICE_CALLBACK_SUSPEND\n");
james@1005 208 XenVbd_Disconnect(xvdd, TRUE);
james@1005 209 break;
james@1005 210 case XN_DEVICE_CALLBACK_RESUME:
james@1005 211 FUNCTION_MSG("XN_DEVICE_CALLBACK_RESUME\n");
james@1005 212 XenVbd_Connect(xvdd, TRUE);
james@1005 213 break;
james@1005 214 }
james@1005 215 FUNCTION_EXIT();
james@1005 216 }