win-pvdrivers

annotate xenpci/xenbus.c @ 624:0b55299418ce

Found some bugs caused by the unavailability of the page file during restore from suspend.
author James Harper <james.harper@bendigoit.com.au>
date Sun Aug 09 00:30:57 2009 +1000 (2009-08-09)
parents f905eb3f0545
children 4c16d7438efd
rev   line source
james@0 1 /*
james@0 2 PV Drivers for Windows Xen HVM Domains
james@0 3 Copyright (C) 2007 James Harper
james@0 4
james@0 5 This program is free software; you can redistribute it and/or
james@0 6 modify it under the terms of the GNU General Public License
james@0 7 as published by the Free Software Foundation; either version 2
james@0 8 of the License, or (at your option) any later version.
james@0 9
james@0 10 This program is distributed in the hope that it will be useful,
james@0 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
james@0 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
james@0 13 GNU General Public License for more details.
james@0 14
james@0 15 You should have received a copy of the GNU General Public License
james@0 16 along with this program; if not, write to the Free Software
james@0 17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
james@0 18 */
james@0 19
james@0 20 #include "xenpci.h"
james@0 21 #include <stdlib.h>
james@0 22
james@0 23 #pragma warning( disable : 4204 )
james@0 24 #pragma warning( disable : 4221 )
james@0 25
james@624 26 WDF_DECLARE_CONTEXT_TYPE(xsd_sockmsg_t)
james@624 27
andy@15 28 struct write_req {
james@504 29 void *data;
andy@15 30 unsigned len;
james@0 31 };
james@0 32
james@157 33 // This routine free's the rep structure if there was an error!!!
james@0 34 static char *errmsg(struct xsd_sockmsg *rep)
james@0 35 {
james@0 36 char *res;
andy@15 37
james@0 38 if (!rep) {
james@0 39 char msg[] = "No reply";
james@0 40 size_t len = strlen(msg) + 1;
james@0 41 return memcpy(ExAllocatePoolWithTag(NonPagedPool, len, XENPCI_POOL_TAG), msg, len);
james@0 42 }
james@0 43 if (rep->type != XS_ERROR)
james@0 44 return NULL;
james@0 45 res = ExAllocatePoolWithTag(NonPagedPool, rep->len + 1, XENPCI_POOL_TAG);
james@0 46 memcpy(res, rep + 1, rep->len);
james@0 47 res[rep->len] = 0;
james@0 48 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@0 49 return res;
james@0 50 }
james@0 51
james@504 52 static void memcpy_from_ring(void *Ring,
james@0 53 void *Dest,
james@0 54 int off,
james@0 55 int len)
james@0 56 {
james@0 57 int c1, c2;
james@504 58 char *ring = Ring;
james@0 59 char *dest = Dest;
james@0 60 c1 = min(len, XENSTORE_RING_SIZE - off);
james@0 61 c2 = len - c1;
james@0 62 memcpy(dest, ring + off, c1);
james@0 63 memcpy(dest + c1, ring, c2);
james@0 64 }
james@0 65
james@404 66 /* called with xenbus_mutex held */
andy@15 67 static void xb_write(
james@258 68 PXENPCI_DEVICE_DATA xpdd,
james@504 69 PVOID data,
james@504 70 ULONG len
james@504 71 )
james@0 72 {
james@0 73 XENSTORE_RING_IDX prod;
james@504 74 ULONG copy_len;
james@504 75 PUCHAR ptr;
james@504 76 ULONG remaining;
james@504 77
james@523 78 //FUNCTION_ENTER();
james@0 79
james@390 80 ASSERT(len <= XENSTORE_RING_SIZE);
andy@15 81 prod = xpdd->xen_store_interface->req_prod;
james@504 82 ptr = data;
james@504 83 remaining = len;
james@504 84 while (remaining)
james@0 85 {
james@504 86 copy_len = min(remaining, XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod));
james@504 87 memcpy((PUCHAR)xpdd->xen_store_interface->req + MASK_XENSTORE_IDX(prod), ptr, copy_len);
james@504 88 prod += (XENSTORE_RING_IDX)copy_len;
james@504 89 ptr += copy_len;
james@504 90 remaining -= copy_len;
james@0 91 }
james@0 92 /* Remote must see entire message before updating indexes */
james@0 93 KeMemoryBarrier();
james@504 94 xpdd->xen_store_interface->req_prod = prod;
james@258 95 EvtChn_Notify(xpdd, xpdd->xen_store_evtchn);
james@0 96
james@523 97 //FUNCTION_EXIT();
james@0 98 }
james@0 99
james@504 100 /* takes and releases xb_request_mutex */
james@0 101 static struct xsd_sockmsg *
james@504 102 xenbus_format_msg_reply(
james@258 103 PXENPCI_DEVICE_DATA xpdd,
andy@13 104 int type,
james@504 105 xenbus_transaction_t trans_id,
james@504 106 struct write_req *req,
andy@13 107 int nr_reqs)
james@0 108 {
james@504 109 struct xsd_sockmsg msg;
james@504 110 struct xsd_sockmsg *reply;
james@504 111 int i;
andy@94 112
james@529 113 //FUNCTION_ENTER();
james@504 114
james@504 115 msg.type = type;
james@504 116 msg.req_id = 0;
james@504 117 msg.tx_id = trans_id;
james@504 118 msg.len = 0;
james@504 119 for (i = 0; i < nr_reqs; i++)
james@515 120 msg.len += req[i].len;
james@95 121
james@504 122 ExAcquireFastMutex(&xpdd->xb_request_mutex);
james@504 123 xb_write(xpdd, &msg, sizeof(msg));
james@504 124 for (i = 0; i < nr_reqs; i++)
james@504 125 xb_write(xpdd, req[i].data, req[i].len);
james@0 126
james@504 127 KeWaitForSingleObject(&xpdd->xb_request_complete_event, Executive, KernelMode, FALSE, NULL);
james@504 128 reply = xpdd->xb_reply;
james@504 129 xpdd->xb_reply = NULL;
james@512 130 ExReleaseFastMutex(&xpdd->xb_request_mutex);
james@96 131
james@529 132 //FUNCTION_EXIT();
james@504 133
james@504 134 return reply;
james@504 135 }
james@504 136
james@504 137 /* takes and releases xb_request_mutex */
james@504 138 struct xsd_sockmsg *
james@504 139 XenBus_Raw(
james@504 140 PXENPCI_DEVICE_DATA xpdd,
james@504 141 struct xsd_sockmsg *msg)
james@504 142 {
james@504 143 struct xsd_sockmsg *reply;
james@536 144
james@624 145 //FUNCTION_ENTER();
james@504 146
james@504 147 ExAcquireFastMutex(&xpdd->xb_request_mutex);
james@504 148 xb_write(xpdd, msg, sizeof(struct xsd_sockmsg) + msg->len);
james@504 149 KeWaitForSingleObject(&xpdd->xb_request_complete_event, Executive, KernelMode, FALSE, NULL);
james@504 150 reply = xpdd->xb_reply;
james@504 151 xpdd->xb_reply = NULL;
james@504 152 ExReleaseFastMutex(&xpdd->xb_request_mutex);
james@504 153
james@624 154 //FUNCTION_EXIT();
james@536 155
james@504 156 return reply;
james@0 157 }
james@0 158
james@624 159 /* Called at PASSIVE_LEVEL */
james@0 160 char *
andy@15 161 XenBus_Read(
andy@13 162 PVOID Context,
andy@13 163 xenbus_transaction_t xbt,
james@504 164 char *path,
andy@13 165 char **value)
james@0 166 {
james@258 167 PXENPCI_DEVICE_DATA xpdd = Context;
james@145 168 struct write_req req[] = { {path, (ULONG)strlen(path) + 1} };
andy@13 169 struct xsd_sockmsg *rep;
andy@13 170 char *res;
andy@13 171 char *msg;
james@0 172
james@342 173 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@95 174
james@272 175 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 176
james@504 177 rep = xenbus_format_msg_reply(xpdd, XS_READ, xbt, req, ARRAY_SIZE(req));
andy@13 178 msg = errmsg(rep);
andy@13 179 if (msg) {
andy@13 180 *value = NULL;
andy@13 181 return msg;
andy@13 182 }
andy@13 183 res = ExAllocatePoolWithTag(NonPagedPool, rep->len + 1, XENPCI_POOL_TAG);
andy@13 184 memcpy(res, rep + 1, rep->len);
andy@13 185 res[rep->len] = 0;
andy@13 186 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
andy@13 187 *value = res;
james@95 188
james@342 189 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 190
andy@13 191 return NULL;
james@0 192 }
james@0 193
james@624 194 /* Called at PASSIVE_LEVEL */
james@0 195 char *
andy@13 196 XenBus_Write(
andy@13 197 PVOID Context,
andy@13 198 xenbus_transaction_t xbt,
james@504 199 char *path,
james@504 200 char *value)
james@0 201 {
james@258 202 PXENPCI_DEVICE_DATA xpdd = Context;
andy@13 203 struct write_req req[] = {
james@145 204 {path, (ULONG)strlen(path) + 1},
andy@264 205 {value, (ULONG)strlen(value)},
andy@13 206 };
andy@13 207 struct xsd_sockmsg *rep;
andy@13 208 char *msg;
james@0 209
james@342 210 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@95 211
james@272 212 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 213
james@504 214 rep = xenbus_format_msg_reply(xpdd, XS_WRITE, xbt, req, ARRAY_SIZE(req));
andy@13 215 msg = errmsg(rep);
andy@13 216 if (msg)
andy@13 217 return msg;
andy@13 218 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@95 219
james@342 220 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 221
andy@13 222 return NULL;
james@0 223 }
james@0 224
james@624 225 /* Called at PASSIVE_LEVEL */
james@624 226 static VOID
james@624 227 XenBus_WatchWorkItemProc(WDFWORKITEM workitem)
james@624 228 {
james@624 229 WDFDEVICE device = WdfWorkItemGetParentObject(workitem);
james@624 230 PXENPCI_DEVICE_DATA xpdd = GetXpdd(device);
james@624 231 xsd_sockmsg_t *msg;
james@624 232 PCHAR path;
james@624 233 int index;
james@624 234 PXENBUS_WATCH_ENTRY entry;
james@624 235
james@624 236 //FUNCTION_ENTER();
james@624 237 msg = WdfObjectGetTypedContext(workitem, xsd_sockmsg_t);
james@624 238 path = (PCHAR)msg + sizeof(xsd_sockmsg_t);
james@624 239 index = atoi(path + strlen(path) + 1);
james@624 240 ExAcquireFastMutex(&xpdd->xb_watch_mutex);
james@624 241 entry = &xpdd->XenBus_WatchEntries[index];
james@624 242 if (!entry->Active || !entry->ServiceRoutine)
james@624 243 {
james@624 244 KdPrint((__DRIVER_NAME " No watch for index %d\n", index));
james@624 245 WdfObjectDelete(workitem);
james@624 246 return;
james@624 247 }
james@624 248 entry->Count++;
james@624 249 entry->ServiceRoutine(path, entry->ServiceContext);
james@624 250 ExReleaseFastMutex(&xpdd->xb_watch_mutex);
james@624 251 WdfObjectDelete(workitem);
james@624 252 //FUNCTION_ENTER();
james@624 253 }
james@624 254
james@624 255 /* Called at DISPATCH_LEVEL */
james@515 256 static VOID
james@515 257 XenBus_Dpc(PVOID ServiceContext)
james@515 258 {
james@624 259 NTSTATUS status;
james@515 260 PXENPCI_DEVICE_DATA xpdd = ServiceContext;
james@624 261 xsd_sockmsg_t msg;
james@624 262 WDF_WORKITEM_CONFIG workitem_config;
james@624 263 WDF_OBJECT_ATTRIBUTES workitem_attributes;
james@624 264 WDFWORKITEM workitem;
james@515 265
james@624 266 //FUNCTION_ENTER();
james@515 267
james@624 268 //KdPrint((__DRIVER_NAME " rsp_prod = %d, rsp_cons = %d\n", xpdd->xen_store_interface->rsp_prod, xpdd->xen_store_interface->rsp_cons));
james@624 269 while (xpdd->xen_store_interface->rsp_prod != xpdd->xen_store_interface->rsp_cons)
james@624 270 {
james@624 271 if (xpdd->xen_store_interface->rsp_prod - xpdd->xen_store_interface->rsp_cons < sizeof(msg))
james@624 272 {
james@624 273 KdPrint((__DRIVER_NAME " +++ Message incomplete (not even a full header)\n"));
james@624 274 break;
james@624 275 }
james@624 276 KeMemoryBarrier();
james@624 277 memcpy_from_ring(xpdd->xen_store_interface->rsp, &msg,
james@624 278 MASK_XENSTORE_IDX(xpdd->xen_store_interface->rsp_cons), sizeof(msg));
james@624 279 if (xpdd->xen_store_interface->rsp_prod - xpdd->xen_store_interface->rsp_cons < sizeof(msg) + msg.len)
james@624 280 {
james@624 281 KdPrint((__DRIVER_NAME " +++ Message incomplete (header but not full body)\n"));
james@624 282 break;
james@624 283 }
james@515 284
james@624 285 if (msg.type != XS_WATCH_EVENT)
james@624 286 {
james@624 287 /* process reply - only ever one outstanding */
james@624 288 xpdd->xb_reply = ExAllocatePoolWithTag(NonPagedPool, sizeof(msg) + msg.len, XENPCI_POOL_TAG);
james@624 289 memcpy_from_ring(xpdd->xen_store_interface->rsp,
james@624 290 xpdd->xb_reply,
james@624 291 MASK_XENSTORE_IDX(xpdd->xen_store_interface->rsp_cons),
james@624 292 msg.len + sizeof(msg));
james@624 293 xpdd->xen_store_interface->rsp_cons += msg.len + sizeof(msg);
james@624 294 KeSetEvent(&xpdd->xb_request_complete_event, IO_NO_INCREMENT, FALSE);
james@624 295 }
james@624 296 else
james@624 297 {
james@624 298 /* process watch */
james@624 299 WDF_WORKITEM_CONFIG_INIT(&workitem_config, XenBus_WatchWorkItemProc);
james@624 300 WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&workitem_attributes, xsd_sockmsg_t);
james@624 301 workitem_attributes.ParentObject = xpdd->wdf_device;
james@624 302 workitem_attributes.ContextSizeOverride = sizeof(msg) + msg.len;
james@624 303 status = WdfWorkItemCreate(&workitem_config, &workitem_attributes, &workitem);
james@624 304 if (!NT_SUCCESS(status))
james@624 305 {
james@624 306 KdPrint((__DRIVER_NAME " Failed to create work item for watch\n"));
james@624 307 xpdd->xen_store_interface->rsp_cons += msg.len + sizeof(msg);
james@624 308 continue;
james@624 309 }
james@624 310 memcpy_from_ring(xpdd->xen_store_interface->rsp,
james@624 311 WdfObjectGetTypedContext(workitem, xsd_sockmsg_t),
james@624 312 MASK_XENSTORE_IDX(xpdd->xen_store_interface->rsp_cons), msg.len + sizeof(msg));
james@624 313 xpdd->xen_store_interface->rsp_cons += msg.len + sizeof(msg);
james@624 314 WdfWorkItemEnqueue(workitem);
james@624 315 }
james@624 316 }
james@624 317
james@624 318 //FUNCTION_EXIT();
james@515 319 }
james@515 320
james@624 321 static NTSTATUS
james@342 322 XenBus_Connect(PXENPCI_DEVICE_DATA xpdd)
james@342 323 {
james@342 324 PHYSICAL_ADDRESS pa_xen_store_interface;
james@342 325 xen_ulong_t xen_store_mfn;
james@342 326
james@342 327 xpdd->xen_store_evtchn = (evtchn_port_t)hvm_get_parameter(xpdd, HVM_PARAM_STORE_EVTCHN);
james@342 328 xen_store_mfn = (xen_ulong_t)hvm_get_parameter(xpdd, HVM_PARAM_STORE_PFN);
james@342 329 pa_xen_store_interface.QuadPart = (ULONGLONG)xen_store_mfn << PAGE_SHIFT;
james@342 330 xpdd->xen_store_interface = MmMapIoSpace(pa_xen_store_interface, PAGE_SIZE, MmNonCached);
james@342 331
james@624 332 EvtChn_BindDpc(xpdd, xpdd->xen_store_evtchn, XenBus_Dpc, xpdd);
james@624 333
james@624 334 return STATUS_SUCCESS;
james@624 335 }
james@624 336
james@624 337 static NTSTATUS
james@624 338 XenBus_Disconnect(PXENPCI_DEVICE_DATA xpdd)
james@624 339 {
james@624 340 EvtChn_Unbind(xpdd, xpdd->xen_store_evtchn);
james@624 341
james@624 342 MmUnmapIoSpace(xpdd->xen_store_interface, PAGE_SIZE);
james@390 343
james@390 344 return STATUS_SUCCESS;
james@342 345 }
james@342 346
james@0 347 NTSTATUS
james@258 348 XenBus_Init(PXENPCI_DEVICE_DATA xpdd)
james@0 349 {
james@390 350 NTSTATUS status;
james@35 351 int i;
andy@37 352
james@624 353 FUNCTION_ENTER();
james@35 354
james@622 355 ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL);
james@35 356
james@504 357 ExInitializeFastMutex(&xpdd->xb_request_mutex);
james@504 358 ExInitializeFastMutex(&xpdd->xb_watch_mutex);
james@128 359
james@35 360 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@128 361 {
andy@38 362 xpdd->XenBus_WatchEntries[i].Active = 0;
james@128 363 }
james@0 364
james@504 365 KeInitializeEvent(&xpdd->xb_request_complete_event, SynchronizationEvent, FALSE);
james@504 366
james@390 367 status = XenBus_Connect(xpdd);
james@390 368 if (!NT_SUCCESS(status))
andy@101 369 {
james@390 370 return status;
andy@101 371 }
james@342 372
james@624 373 FUNCTION_EXIT();
james@0 374
james@0 375 return STATUS_SUCCESS;
james@0 376 }
james@0 377
james@406 378 char *
james@406 379 XenBus_SendRemWatch(
james@406 380 PVOID context,
james@406 381 xenbus_transaction_t xbt,
james@504 382 char *path,
james@504 383 int index)
james@406 384 {
james@406 385 struct xsd_sockmsg *rep;
james@406 386 char *msg;
james@406 387 char Token[20];
james@406 388 struct write_req req[2];
james@406 389
james@406 390 req[0].data = path;
james@406 391 req[0].len = (ULONG)strlen(path) + 1;
james@406 392
james@406 393 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", index);
james@406 394 req[1].data = Token;
james@406 395 req[1].len = (ULONG)strlen(Token) + 1;
james@406 396
james@504 397 rep = xenbus_format_msg_reply(context, XS_UNWATCH, xbt, req, ARRAY_SIZE(req));
james@406 398
james@406 399 msg = errmsg(rep);
james@406 400 if (msg)
james@406 401 return msg;
james@406 402
james@406 403 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@406 404
james@406 405 return NULL;
james@406 406 }
james@406 407
james@35 408 NTSTATUS
james@622 409 XenBus_Halt(PXENPCI_DEVICE_DATA xpdd)
james@35 410 {
james@406 411 int i;
james@35 412
james@406 413 FUNCTION_ENTER();
james@406 414
james@272 415 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 416
james@406 417 /* we need to remove the watches as a watch firing could lead to a XenBus_Read/Write/Printf */
james@624 418 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@624 419 {
james@406 420 if (xpdd->XenBus_WatchEntries[i].Active)
james@624 421 {
james@624 422 xpdd->XenBus_WatchEntries[i].Active = 0;
james@406 423 XenBus_SendRemWatch(xpdd, XBT_NIL, xpdd->XenBus_WatchEntries[i].Path, i);
james@624 424 }
james@406 425 }
james@406 426
james@624 427 XenBus_Disconnect(xpdd);
james@35 428
james@406 429 FUNCTION_EXIT();
james@35 430
james@0 431 return STATUS_SUCCESS;
james@0 432 }
james@0 433
james@0 434 char *
andy@13 435 XenBus_List(
andy@13 436 PVOID Context,
andy@13 437 xenbus_transaction_t xbt,
james@504 438 char *pre,
andy@13 439 char ***contents)
james@0 440 {
james@258 441 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 442 struct xsd_sockmsg *reply, *repmsg;
james@145 443 struct write_req req[] = { { pre, (ULONG)strlen(pre)+1 } };
james@0 444 ULONG nr_elems, x, i;
james@0 445 char **res;
james@0 446 char *msg;
james@0 447
james@272 448 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 449
james@504 450 repmsg = xenbus_format_msg_reply(xpdd, XS_DIRECTORY, xbt, req, ARRAY_SIZE(req));
james@0 451 msg = errmsg(repmsg);
andy@15 452 if (msg)
andy@15 453 {
james@0 454 *contents = NULL;
james@0 455 return msg;
james@0 456 }
james@0 457 reply = repmsg + 1;
james@0 458 for (x = nr_elems = 0; x < repmsg->len; x++)
andy@15 459 {
james@0 460 nr_elems += (((char *)reply)[x] == 0);
andy@15 461 }
andy@15 462 res = ExAllocatePoolWithTag(NonPagedPool, sizeof(res[0]) * (nr_elems + 1),
andy@15 463 XENPCI_POOL_TAG);
andy@15 464 for (x = i = 0; i < nr_elems; i++)
andy@15 465 {
james@145 466 int l = (int)strlen((char *)reply + x);
james@0 467 res[i] = ExAllocatePoolWithTag(NonPagedPool, l + 1, XENPCI_POOL_TAG);
james@0 468 memcpy(res[i], (char *)reply + x, l + 1);
james@0 469 x += l + 1;
james@0 470 }
james@0 471 res[i] = NULL;
james@0 472 ExFreePoolWithTag(repmsg, XENPCI_POOL_TAG);
james@0 473 *contents = res;
james@624 474
james@0 475 return NULL;
james@0 476 }
james@0 477
james@624 478 /* Called at PASSIVE_LEVEL */
james@279 479 static char *
james@279 480 XenBus_SendAddWatch(
james@279 481 PVOID Context,
james@279 482 xenbus_transaction_t xbt,
james@504 483 char *Path,
james@279 484 int slot)
james@279 485 {
james@279 486 PXENPCI_DEVICE_DATA xpdd = Context;
james@279 487 struct xsd_sockmsg *rep;
james@279 488 char *msg;
james@279 489 char Token[20];
james@279 490 struct write_req req[2];
james@279 491
james@279 492 req[0].data = Path;
james@279 493 req[0].len = (ULONG)strlen(Path) + 1;
james@279 494
james@279 495 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", slot);
james@279 496 req[1].data = Token;
james@279 497 req[1].len = (ULONG)strlen(Token) + 1;
james@279 498
james@504 499 rep = xenbus_format_msg_reply(xpdd, XS_WATCH, xbt, req, ARRAY_SIZE(req));
james@404 500
james@279 501 msg = errmsg(rep);
james@283 502 if (!msg)
james@283 503 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@279 504
james@279 505 return msg;
james@279 506 }
james@279 507
james@342 508 /* called at PASSIVE_LEVEL */
james@390 509 NTSTATUS
james@409 510 XenBus_Suspend(PXENPCI_DEVICE_DATA xpdd)
james@409 511 {
james@409 512 int i;
james@409 513
james@409 514 /* we need to remove the watches as a watch firing could lead to a XenBus_Read/Write/Printf */
james@409 515 for (i = 0; i < MAX_WATCH_ENTRIES; i++) {
james@409 516 if (xpdd->XenBus_WatchEntries[i].Active)
james@409 517 XenBus_SendRemWatch(xpdd, XBT_NIL, xpdd->XenBus_WatchEntries[i].Path, i);
james@409 518 }
james@624 519 XenBus_Disconnect(xpdd);
james@624 520
james@409 521 return STATUS_SUCCESS;
james@409 522 }
james@409 523
james@409 524 /* called at PASSIVE_LEVEL */
james@409 525 NTSTATUS
james@279 526 XenBus_Resume(PXENPCI_DEVICE_DATA xpdd)
james@279 527 {
james@390 528 NTSTATUS status;
james@279 529 int i;
james@341 530
andy@398 531 FUNCTION_ENTER();
james@342 532
james@390 533 status = XenBus_Connect(xpdd);
james@390 534 if (!NT_SUCCESS(status))
james@390 535 {
james@390 536 return status;
james@390 537 }
james@279 538
james@279 539 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@341 540 {
james@279 541 if (xpdd->XenBus_WatchEntries[i].Active)
james@341 542 {
james@536 543 //KdPrint((__DRIVER_NAME " Adding watch for path = %s\n", xpdd->XenBus_WatchEntries[i].Path));
james@279 544 XenBus_SendAddWatch(xpdd, XBT_NIL, xpdd->XenBus_WatchEntries[i].Path, i);
james@341 545 }
james@341 546 }
andy@398 547 FUNCTION_EXIT();
james@390 548
james@390 549 return STATUS_SUCCESS;
james@279 550 }
james@279 551
james@0 552 char *
andy@15 553 XenBus_AddWatch(
andy@13 554 PVOID Context,
andy@13 555 xenbus_transaction_t xbt,
james@504 556 char *Path,
andy@13 557 PXENBUS_WATCH_CALLBACK ServiceRoutine,
andy@13 558 PVOID ServiceContext)
james@0 559 {
james@258 560 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 561 char *msg;
james@0 562 int i;
andy@94 563 PXENBUS_WATCH_ENTRY w_entry;
james@0 564
james@272 565 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@0 566
andy@94 567 ASSERT(strlen(Path) < ARRAY_SIZE(w_entry->Path));
james@0 568
james@504 569 ExAcquireFastMutex(&xpdd->xb_watch_mutex);
james@128 570
james@0 571 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
andy@15 572 if (xpdd->XenBus_WatchEntries[i].Active == 0)
james@0 573 break;
james@0 574
james@0 575 if (i == MAX_WATCH_ENTRIES)
james@0 576 {
james@0 577 KdPrint((__DRIVER_NAME " +++ No more watch slots left\n"));
james@504 578 ExReleaseFastMutex(&xpdd->xb_watch_mutex);
james@0 579 return NULL;
james@0 580 }
james@0 581
andy@94 582 /* must init watchentry before starting watch */
james@128 583
andy@94 584 w_entry = &xpdd->XenBus_WatchEntries[i];
andy@94 585 strncpy(w_entry->Path, Path, ARRAY_SIZE(w_entry->Path));
andy@94 586 w_entry->ServiceRoutine = ServiceRoutine;
andy@94 587 w_entry->ServiceContext = ServiceContext;
andy@94 588 w_entry->Count = 0;
andy@94 589 w_entry->Active = 1;
andy@94 590
james@504 591 ExReleaseFastMutex(&xpdd->xb_watch_mutex);
james@128 592
james@279 593 msg = XenBus_SendAddWatch(xpdd, xbt, Path, i);
james@0 594
andy@93 595 if (msg)
andy@93 596 {
andy@93 597 xpdd->XenBus_WatchEntries[i].Active = 0;
andy@93 598 return msg;
andy@93 599 }
andy@93 600
james@0 601 return NULL;
james@0 602 }
james@0 603
james@0 604 char *
andy@13 605 XenBus_RemWatch(
andy@13 606 PVOID Context,
andy@13 607 xenbus_transaction_t xbt,
james@504 608 char *Path,
andy@13 609 PXENBUS_WATCH_CALLBACK ServiceRoutine,
andy@13 610 PVOID ServiceContext)
james@0 611 {
james@258 612 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 613 char *msg;
james@0 614 int i;
james@0 615
james@272 616 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@0 617
james@504 618 ExAcquireFastMutex(&xpdd->xb_watch_mutex);
james@128 619
james@0 620 // check that Path < 128 chars
james@0 621
james@409 622 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@409 623 {
james@538 624 if (xpdd->XenBus_WatchEntries[i].Active
james@538 625 && !strcmp(xpdd->XenBus_WatchEntries[i].Path, Path)
andy@15 626 && xpdd->XenBus_WatchEntries[i].ServiceRoutine == ServiceRoutine
andy@15 627 && xpdd->XenBus_WatchEntries[i].ServiceContext == ServiceContext)
james@409 628 {
james@409 629 KdPrint((__DRIVER_NAME " Match\n"));
james@0 630 break;
james@409 631 }
james@0 632 }
james@0 633
james@0 634 if (i == MAX_WATCH_ENTRIES)
james@0 635 {
james@504 636 ExReleaseFastMutex(&xpdd->xb_watch_mutex);
james@0 637 KdPrint((__DRIVER_NAME " Watch not set - can't remove\n"));
james@0 638 return NULL;
james@0 639 }
james@0 640
james@128 641 xpdd->XenBus_WatchEntries[i].Active = 0;
james@128 642 xpdd->XenBus_WatchEntries[i].Path[0] = 0;
james@128 643
james@504 644 ExReleaseFastMutex(&xpdd->xb_watch_mutex);
james@0 645
james@406 646 msg = XenBus_SendRemWatch(Context, xbt, Path, i);
james@406 647
james@406 648 return msg;
james@0 649 }
james@0 650
james@0 651 char *
andy@13 652 XenBus_StartTransaction(PVOID Context, xenbus_transaction_t *xbt)
james@0 653 {
james@258 654 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 655 /* xenstored becomes angry if you send a length 0 message, so just
james@0 656 shove a nul terminator on the end */
james@0 657 struct write_req req = { "", 1};
james@0 658 struct xsd_sockmsg *rep;
james@0 659 char *err;
james@0 660
james@272 661 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@95 662
james@504 663 rep = xenbus_format_msg_reply(xpdd, XS_TRANSACTION_START, 0, &req, 1);
james@0 664 err = errmsg(rep);
james@0 665 if (err)
james@0 666 return err;
james@0 667 *xbt = atoi((char *)(rep + 1));
james@0 668 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@95 669
james@0 670 return NULL;
james@0 671 }
james@0 672
james@0 673 char *
andy@15 674 XenBus_EndTransaction(
andy@15 675 PVOID Context,
andy@15 676 xenbus_transaction_t t,
andy@15 677 int abort,
andy@15 678 int *retry)
james@0 679 {
james@258 680 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 681 struct xsd_sockmsg *rep;
james@0 682 struct write_req req;
james@0 683 char *err;
james@0 684
james@0 685 *retry = 0;
james@0 686
james@0 687 req.data = abort ? "F" : "T";
james@0 688 req.len = 2;
james@504 689 rep = xenbus_format_msg_reply(xpdd, XS_TRANSACTION_END, t, &req, 1);
james@0 690 err = errmsg(rep);
james@0 691 if (err) {
james@0 692 if (!strcmp(err, "EAGAIN")) {
james@0 693 *retry = 1;
james@0 694 ExFreePoolWithTag(err, XENPCI_POOL_TAG);
james@0 695 return NULL;
james@0 696 } else {
james@0 697 return err;
james@0 698 }
james@0 699 }
james@0 700 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@95 701
james@0 702 return NULL;
james@0 703 }
james@0 704
james@0 705 char *
andy@15 706 XenBus_Printf(
andy@15 707 PVOID Context,
andy@15 708 xenbus_transaction_t xbt,
james@504 709 char *path,
james@504 710 char *fmt,
andy@15 711 ...)
james@0 712 {
james@258 713 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 714 va_list ap;
andy@101 715 char buf[512];
james@95 716 char *retval;
james@95 717
james@272 718 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@0 719
james@0 720 va_start(ap, fmt);
james@0 721 RtlStringCbVPrintfA(buf, ARRAY_SIZE(buf), fmt, ap);
james@0 722 va_end(ap);
james@258 723 retval = XenBus_Write(xpdd, xbt, path, buf);
james@95 724
james@95 725 return retval;
andy@37 726 }