win-pvdrivers

annotate xenpci/xenbus.c @ 646:84e37987cdbc

xenbus dpc could be scheduled on multiple cpu's simultaneously. protect with a spinlock.
author James Harper <james.harper@bendigoit.com.au>
date Tue Aug 25 21:44:08 2009 +1000 (2009-08-25)
parents 51012d703b75
children e5522de3be37
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@630 245 ExReleaseFastMutex(&xpdd->xb_watch_mutex);
james@624 246 WdfObjectDelete(workitem);
james@624 247 return;
james@624 248 }
james@624 249 entry->Count++;
james@624 250 entry->ServiceRoutine(path, entry->ServiceContext);
james@624 251 ExReleaseFastMutex(&xpdd->xb_watch_mutex);
james@624 252 WdfObjectDelete(workitem);
james@624 253 //FUNCTION_ENTER();
james@624 254 }
james@624 255
james@624 256 /* Called at DISPATCH_LEVEL */
james@515 257 static VOID
james@515 258 XenBus_Dpc(PVOID ServiceContext)
james@515 259 {
james@624 260 NTSTATUS status;
james@515 261 PXENPCI_DEVICE_DATA xpdd = ServiceContext;
james@624 262 xsd_sockmsg_t msg;
james@624 263 WDF_WORKITEM_CONFIG workitem_config;
james@624 264 WDF_OBJECT_ATTRIBUTES workitem_attributes;
james@624 265 WDFWORKITEM workitem;
james@515 266
james@624 267 //FUNCTION_ENTER();
james@646 268
james@646 269 KeAcquireSpinLockAtDpcLevel(&xpdd->xb_ring_spinlock);
james@515 270
james@624 271 //KdPrint((__DRIVER_NAME " rsp_prod = %d, rsp_cons = %d\n", xpdd->xen_store_interface->rsp_prod, xpdd->xen_store_interface->rsp_cons));
james@624 272 while (xpdd->xen_store_interface->rsp_prod != xpdd->xen_store_interface->rsp_cons)
james@624 273 {
james@624 274 if (xpdd->xen_store_interface->rsp_prod - xpdd->xen_store_interface->rsp_cons < sizeof(msg))
james@624 275 {
james@624 276 KdPrint((__DRIVER_NAME " +++ Message incomplete (not even a full header)\n"));
james@624 277 break;
james@624 278 }
james@624 279 KeMemoryBarrier();
james@624 280 memcpy_from_ring(xpdd->xen_store_interface->rsp, &msg,
james@624 281 MASK_XENSTORE_IDX(xpdd->xen_store_interface->rsp_cons), sizeof(msg));
james@624 282 if (xpdd->xen_store_interface->rsp_prod - xpdd->xen_store_interface->rsp_cons < sizeof(msg) + msg.len)
james@624 283 {
james@629 284 //KdPrint((__DRIVER_NAME " +++ Message incomplete (header but not full body)\n"));
james@624 285 break;
james@624 286 }
james@515 287
james@624 288 if (msg.type != XS_WATCH_EVENT)
james@624 289 {
james@624 290 /* process reply - only ever one outstanding */
james@624 291 xpdd->xb_reply = ExAllocatePoolWithTag(NonPagedPool, sizeof(msg) + msg.len, XENPCI_POOL_TAG);
james@624 292 memcpy_from_ring(xpdd->xen_store_interface->rsp,
james@624 293 xpdd->xb_reply,
james@624 294 MASK_XENSTORE_IDX(xpdd->xen_store_interface->rsp_cons),
james@624 295 msg.len + sizeof(msg));
james@624 296 xpdd->xen_store_interface->rsp_cons += msg.len + sizeof(msg);
james@624 297 KeSetEvent(&xpdd->xb_request_complete_event, IO_NO_INCREMENT, FALSE);
james@624 298 }
james@624 299 else
james@624 300 {
james@624 301 /* process watch */
james@624 302 WDF_WORKITEM_CONFIG_INIT(&workitem_config, XenBus_WatchWorkItemProc);
james@624 303 WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&workitem_attributes, xsd_sockmsg_t);
james@624 304 workitem_attributes.ParentObject = xpdd->wdf_device;
james@624 305 workitem_attributes.ContextSizeOverride = sizeof(msg) + msg.len;
james@624 306 status = WdfWorkItemCreate(&workitem_config, &workitem_attributes, &workitem);
james@624 307 if (!NT_SUCCESS(status))
james@624 308 {
james@624 309 KdPrint((__DRIVER_NAME " Failed to create work item for watch\n"));
james@624 310 xpdd->xen_store_interface->rsp_cons += msg.len + sizeof(msg);
james@624 311 continue;
james@624 312 }
james@624 313 memcpy_from_ring(xpdd->xen_store_interface->rsp,
james@624 314 WdfObjectGetTypedContext(workitem, xsd_sockmsg_t),
james@624 315 MASK_XENSTORE_IDX(xpdd->xen_store_interface->rsp_cons), msg.len + sizeof(msg));
james@624 316 xpdd->xen_store_interface->rsp_cons += msg.len + sizeof(msg);
james@624 317 WdfWorkItemEnqueue(workitem);
james@624 318 }
james@624 319 }
james@646 320 KeReleaseSpinLockFromDpcLevel(&xpdd->xb_ring_spinlock);
james@624 321
james@624 322 //FUNCTION_EXIT();
james@515 323 }
james@515 324
james@624 325 static NTSTATUS
james@342 326 XenBus_Connect(PXENPCI_DEVICE_DATA xpdd)
james@342 327 {
james@342 328 PHYSICAL_ADDRESS pa_xen_store_interface;
james@342 329 xen_ulong_t xen_store_mfn;
james@342 330
james@342 331 xpdd->xen_store_evtchn = (evtchn_port_t)hvm_get_parameter(xpdd, HVM_PARAM_STORE_EVTCHN);
james@342 332 xen_store_mfn = (xen_ulong_t)hvm_get_parameter(xpdd, HVM_PARAM_STORE_PFN);
james@342 333 pa_xen_store_interface.QuadPart = (ULONGLONG)xen_store_mfn << PAGE_SHIFT;
james@342 334 xpdd->xen_store_interface = MmMapIoSpace(pa_xen_store_interface, PAGE_SIZE, MmNonCached);
james@342 335
james@624 336 EvtChn_BindDpc(xpdd, xpdd->xen_store_evtchn, XenBus_Dpc, xpdd);
james@624 337
james@624 338 return STATUS_SUCCESS;
james@624 339 }
james@624 340
james@624 341 static NTSTATUS
james@624 342 XenBus_Disconnect(PXENPCI_DEVICE_DATA xpdd)
james@624 343 {
james@624 344 EvtChn_Unbind(xpdd, xpdd->xen_store_evtchn);
james@624 345
james@624 346 MmUnmapIoSpace(xpdd->xen_store_interface, PAGE_SIZE);
james@390 347
james@390 348 return STATUS_SUCCESS;
james@342 349 }
james@342 350
james@0 351 NTSTATUS
james@258 352 XenBus_Init(PXENPCI_DEVICE_DATA xpdd)
james@0 353 {
james@390 354 NTSTATUS status;
james@35 355 int i;
andy@37 356
james@624 357 FUNCTION_ENTER();
james@35 358
james@622 359 ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL);
james@35 360
james@646 361 KeInitializeSpinLock(&xpdd->xb_ring_spinlock);
james@504 362 ExInitializeFastMutex(&xpdd->xb_request_mutex);
james@504 363 ExInitializeFastMutex(&xpdd->xb_watch_mutex);
james@128 364
james@35 365 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@128 366 {
andy@38 367 xpdd->XenBus_WatchEntries[i].Active = 0;
james@128 368 }
james@0 369
james@504 370 KeInitializeEvent(&xpdd->xb_request_complete_event, SynchronizationEvent, FALSE);
james@504 371
james@390 372 status = XenBus_Connect(xpdd);
james@390 373 if (!NT_SUCCESS(status))
andy@101 374 {
james@390 375 return status;
andy@101 376 }
james@342 377
james@624 378 FUNCTION_EXIT();
james@0 379
james@0 380 return STATUS_SUCCESS;
james@0 381 }
james@0 382
james@406 383 char *
james@406 384 XenBus_SendRemWatch(
james@406 385 PVOID context,
james@406 386 xenbus_transaction_t xbt,
james@504 387 char *path,
james@504 388 int index)
james@406 389 {
james@406 390 struct xsd_sockmsg *rep;
james@406 391 char *msg;
james@406 392 char Token[20];
james@406 393 struct write_req req[2];
james@406 394
james@406 395 req[0].data = path;
james@406 396 req[0].len = (ULONG)strlen(path) + 1;
james@406 397
james@406 398 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", index);
james@406 399 req[1].data = Token;
james@406 400 req[1].len = (ULONG)strlen(Token) + 1;
james@406 401
james@504 402 rep = xenbus_format_msg_reply(context, XS_UNWATCH, xbt, req, ARRAY_SIZE(req));
james@406 403
james@406 404 msg = errmsg(rep);
james@406 405 if (msg)
james@406 406 return msg;
james@406 407
james@406 408 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@406 409
james@406 410 return NULL;
james@406 411 }
james@406 412
james@35 413 NTSTATUS
james@622 414 XenBus_Halt(PXENPCI_DEVICE_DATA xpdd)
james@35 415 {
james@406 416 int i;
james@35 417
james@406 418 FUNCTION_ENTER();
james@406 419
james@272 420 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 421
james@406 422 /* we need to remove the watches as a watch firing could lead to a XenBus_Read/Write/Printf */
james@624 423 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@624 424 {
james@406 425 if (xpdd->XenBus_WatchEntries[i].Active)
james@624 426 {
james@624 427 xpdd->XenBus_WatchEntries[i].Active = 0;
james@406 428 XenBus_SendRemWatch(xpdd, XBT_NIL, xpdd->XenBus_WatchEntries[i].Path, i);
james@624 429 }
james@406 430 }
james@406 431
james@624 432 XenBus_Disconnect(xpdd);
james@35 433
james@406 434 FUNCTION_EXIT();
james@35 435
james@0 436 return STATUS_SUCCESS;
james@0 437 }
james@0 438
james@0 439 char *
andy@13 440 XenBus_List(
andy@13 441 PVOID Context,
andy@13 442 xenbus_transaction_t xbt,
james@504 443 char *pre,
andy@13 444 char ***contents)
james@0 445 {
james@258 446 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 447 struct xsd_sockmsg *reply, *repmsg;
james@145 448 struct write_req req[] = { { pre, (ULONG)strlen(pre)+1 } };
james@0 449 ULONG nr_elems, x, i;
james@0 450 char **res;
james@0 451 char *msg;
james@0 452
james@272 453 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 454
james@504 455 repmsg = xenbus_format_msg_reply(xpdd, XS_DIRECTORY, xbt, req, ARRAY_SIZE(req));
james@0 456 msg = errmsg(repmsg);
andy@15 457 if (msg)
andy@15 458 {
james@0 459 *contents = NULL;
james@0 460 return msg;
james@0 461 }
james@0 462 reply = repmsg + 1;
james@0 463 for (x = nr_elems = 0; x < repmsg->len; x++)
andy@15 464 {
james@0 465 nr_elems += (((char *)reply)[x] == 0);
andy@15 466 }
andy@15 467 res = ExAllocatePoolWithTag(NonPagedPool, sizeof(res[0]) * (nr_elems + 1),
andy@15 468 XENPCI_POOL_TAG);
andy@15 469 for (x = i = 0; i < nr_elems; i++)
andy@15 470 {
james@145 471 int l = (int)strlen((char *)reply + x);
james@0 472 res[i] = ExAllocatePoolWithTag(NonPagedPool, l + 1, XENPCI_POOL_TAG);
james@0 473 memcpy(res[i], (char *)reply + x, l + 1);
james@0 474 x += l + 1;
james@0 475 }
james@0 476 res[i] = NULL;
james@0 477 ExFreePoolWithTag(repmsg, XENPCI_POOL_TAG);
james@0 478 *contents = res;
james@624 479
james@0 480 return NULL;
james@0 481 }
james@0 482
james@624 483 /* Called at PASSIVE_LEVEL */
james@279 484 static char *
james@279 485 XenBus_SendAddWatch(
james@279 486 PVOID Context,
james@279 487 xenbus_transaction_t xbt,
james@504 488 char *Path,
james@279 489 int slot)
james@279 490 {
james@279 491 PXENPCI_DEVICE_DATA xpdd = Context;
james@279 492 struct xsd_sockmsg *rep;
james@279 493 char *msg;
james@279 494 char Token[20];
james@279 495 struct write_req req[2];
james@279 496
james@279 497 req[0].data = Path;
james@279 498 req[0].len = (ULONG)strlen(Path) + 1;
james@279 499
james@279 500 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", slot);
james@279 501 req[1].data = Token;
james@279 502 req[1].len = (ULONG)strlen(Token) + 1;
james@279 503
james@504 504 rep = xenbus_format_msg_reply(xpdd, XS_WATCH, xbt, req, ARRAY_SIZE(req));
james@404 505
james@279 506 msg = errmsg(rep);
james@283 507 if (!msg)
james@283 508 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@279 509
james@279 510 return msg;
james@279 511 }
james@279 512
james@342 513 /* called at PASSIVE_LEVEL */
james@390 514 NTSTATUS
james@409 515 XenBus_Suspend(PXENPCI_DEVICE_DATA xpdd)
james@409 516 {
james@409 517 int i;
james@409 518
james@409 519 /* we need to remove the watches as a watch firing could lead to a XenBus_Read/Write/Printf */
james@409 520 for (i = 0; i < MAX_WATCH_ENTRIES; i++) {
james@409 521 if (xpdd->XenBus_WatchEntries[i].Active)
james@409 522 XenBus_SendRemWatch(xpdd, XBT_NIL, xpdd->XenBus_WatchEntries[i].Path, i);
james@409 523 }
james@624 524 XenBus_Disconnect(xpdd);
james@624 525
james@409 526 return STATUS_SUCCESS;
james@409 527 }
james@409 528
james@409 529 /* called at PASSIVE_LEVEL */
james@409 530 NTSTATUS
james@279 531 XenBus_Resume(PXENPCI_DEVICE_DATA xpdd)
james@279 532 {
james@390 533 NTSTATUS status;
james@279 534 int i;
james@341 535
andy@398 536 FUNCTION_ENTER();
james@342 537
james@390 538 status = XenBus_Connect(xpdd);
james@390 539 if (!NT_SUCCESS(status))
james@390 540 {
james@390 541 return status;
james@390 542 }
james@279 543
james@279 544 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@341 545 {
james@279 546 if (xpdd->XenBus_WatchEntries[i].Active)
james@341 547 {
james@536 548 //KdPrint((__DRIVER_NAME " Adding watch for path = %s\n", xpdd->XenBus_WatchEntries[i].Path));
james@279 549 XenBus_SendAddWatch(xpdd, XBT_NIL, xpdd->XenBus_WatchEntries[i].Path, i);
james@341 550 }
james@341 551 }
andy@398 552 FUNCTION_EXIT();
james@390 553
james@390 554 return STATUS_SUCCESS;
james@279 555 }
james@279 556
james@0 557 char *
andy@15 558 XenBus_AddWatch(
andy@13 559 PVOID Context,
andy@13 560 xenbus_transaction_t xbt,
james@504 561 char *Path,
andy@13 562 PXENBUS_WATCH_CALLBACK ServiceRoutine,
andy@13 563 PVOID ServiceContext)
james@0 564 {
james@258 565 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 566 char *msg;
james@0 567 int i;
andy@94 568 PXENBUS_WATCH_ENTRY w_entry;
james@0 569
james@272 570 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@0 571
andy@94 572 ASSERT(strlen(Path) < ARRAY_SIZE(w_entry->Path));
james@0 573
james@504 574 ExAcquireFastMutex(&xpdd->xb_watch_mutex);
james@128 575
james@0 576 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
andy@15 577 if (xpdd->XenBus_WatchEntries[i].Active == 0)
james@0 578 break;
james@0 579
james@0 580 if (i == MAX_WATCH_ENTRIES)
james@0 581 {
james@0 582 KdPrint((__DRIVER_NAME " +++ No more watch slots left\n"));
james@504 583 ExReleaseFastMutex(&xpdd->xb_watch_mutex);
james@0 584 return NULL;
james@0 585 }
james@0 586
andy@94 587 /* must init watchentry before starting watch */
james@128 588
andy@94 589 w_entry = &xpdd->XenBus_WatchEntries[i];
andy@94 590 strncpy(w_entry->Path, Path, ARRAY_SIZE(w_entry->Path));
andy@94 591 w_entry->ServiceRoutine = ServiceRoutine;
andy@94 592 w_entry->ServiceContext = ServiceContext;
andy@94 593 w_entry->Count = 0;
andy@94 594 w_entry->Active = 1;
andy@94 595
james@504 596 ExReleaseFastMutex(&xpdd->xb_watch_mutex);
james@128 597
james@279 598 msg = XenBus_SendAddWatch(xpdd, xbt, Path, i);
james@0 599
andy@93 600 if (msg)
andy@93 601 {
andy@93 602 xpdd->XenBus_WatchEntries[i].Active = 0;
andy@93 603 return msg;
andy@93 604 }
andy@93 605
james@0 606 return NULL;
james@0 607 }
james@0 608
james@0 609 char *
andy@13 610 XenBus_RemWatch(
andy@13 611 PVOID Context,
andy@13 612 xenbus_transaction_t xbt,
james@504 613 char *Path,
andy@13 614 PXENBUS_WATCH_CALLBACK ServiceRoutine,
andy@13 615 PVOID ServiceContext)
james@0 616 {
james@258 617 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 618 char *msg;
james@0 619 int i;
james@0 620
james@272 621 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@0 622
james@504 623 ExAcquireFastMutex(&xpdd->xb_watch_mutex);
james@128 624
james@0 625 // check that Path < 128 chars
james@0 626
james@409 627 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@409 628 {
james@538 629 if (xpdd->XenBus_WatchEntries[i].Active
james@538 630 && !strcmp(xpdd->XenBus_WatchEntries[i].Path, Path)
andy@15 631 && xpdd->XenBus_WatchEntries[i].ServiceRoutine == ServiceRoutine
andy@15 632 && xpdd->XenBus_WatchEntries[i].ServiceContext == ServiceContext)
james@409 633 {
james@409 634 KdPrint((__DRIVER_NAME " Match\n"));
james@0 635 break;
james@409 636 }
james@0 637 }
james@0 638
james@0 639 if (i == MAX_WATCH_ENTRIES)
james@0 640 {
james@504 641 ExReleaseFastMutex(&xpdd->xb_watch_mutex);
james@0 642 KdPrint((__DRIVER_NAME " Watch not set - can't remove\n"));
james@0 643 return NULL;
james@0 644 }
james@0 645
james@128 646 xpdd->XenBus_WatchEntries[i].Active = 0;
james@128 647 xpdd->XenBus_WatchEntries[i].Path[0] = 0;
james@128 648
james@504 649 ExReleaseFastMutex(&xpdd->xb_watch_mutex);
james@0 650
james@406 651 msg = XenBus_SendRemWatch(Context, xbt, Path, i);
james@406 652
james@406 653 return msg;
james@0 654 }
james@0 655
james@0 656 char *
andy@13 657 XenBus_StartTransaction(PVOID Context, xenbus_transaction_t *xbt)
james@0 658 {
james@258 659 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 660 /* xenstored becomes angry if you send a length 0 message, so just
james@0 661 shove a nul terminator on the end */
james@0 662 struct write_req req = { "", 1};
james@0 663 struct xsd_sockmsg *rep;
james@0 664 char *err;
james@0 665
james@272 666 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@95 667
james@504 668 rep = xenbus_format_msg_reply(xpdd, XS_TRANSACTION_START, 0, &req, 1);
james@0 669 err = errmsg(rep);
james@0 670 if (err)
james@0 671 return err;
james@0 672 *xbt = atoi((char *)(rep + 1));
james@0 673 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@95 674
james@0 675 return NULL;
james@0 676 }
james@0 677
james@0 678 char *
andy@15 679 XenBus_EndTransaction(
andy@15 680 PVOID Context,
andy@15 681 xenbus_transaction_t t,
andy@15 682 int abort,
andy@15 683 int *retry)
james@0 684 {
james@258 685 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 686 struct xsd_sockmsg *rep;
james@0 687 struct write_req req;
james@0 688 char *err;
james@0 689
james@0 690 *retry = 0;
james@0 691
james@0 692 req.data = abort ? "F" : "T";
james@0 693 req.len = 2;
james@504 694 rep = xenbus_format_msg_reply(xpdd, XS_TRANSACTION_END, t, &req, 1);
james@0 695 err = errmsg(rep);
james@0 696 if (err) {
james@0 697 if (!strcmp(err, "EAGAIN")) {
james@0 698 *retry = 1;
james@0 699 ExFreePoolWithTag(err, XENPCI_POOL_TAG);
james@0 700 return NULL;
james@0 701 } else {
james@0 702 return err;
james@0 703 }
james@0 704 }
james@0 705 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@95 706
james@0 707 return NULL;
james@0 708 }
james@0 709
james@0 710 char *
andy@15 711 XenBus_Printf(
andy@15 712 PVOID Context,
andy@15 713 xenbus_transaction_t xbt,
james@504 714 char *path,
james@504 715 char *fmt,
andy@15 716 ...)
james@0 717 {
james@258 718 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 719 va_list ap;
andy@101 720 char buf[512];
james@95 721 char *retval;
james@95 722
james@272 723 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@0 724
james@0 725 va_start(ap, fmt);
james@0 726 RtlStringCbVPrintfA(buf, ARRAY_SIZE(buf), fmt, ap);
james@0 727 va_end(ap);
james@258 728 retval = XenBus_Write(xpdd, xbt, path, buf);
james@95 729
james@95 730 return retval;
andy@37 731 }