win-pvdrivers

annotate xenpci/xenbus.c @ 673:e5522de3be37

Fix a problem where a zero length sg buffer might be present
author James Harper <james.harper@bendigoit.com.au>
date Wed Oct 07 14:16:21 2009 +1100 (2009-10-07)
parents 84e37987cdbc
children 4baaaaa23235
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@673 375 FUNCTION_EXIT();
james@390 376 return status;
andy@101 377 }
james@342 378
james@624 379 FUNCTION_EXIT();
james@0 380
james@0 381 return STATUS_SUCCESS;
james@0 382 }
james@0 383
james@406 384 char *
james@406 385 XenBus_SendRemWatch(
james@406 386 PVOID context,
james@406 387 xenbus_transaction_t xbt,
james@504 388 char *path,
james@504 389 int index)
james@406 390 {
james@406 391 struct xsd_sockmsg *rep;
james@406 392 char *msg;
james@406 393 char Token[20];
james@406 394 struct write_req req[2];
james@406 395
james@406 396 req[0].data = path;
james@406 397 req[0].len = (ULONG)strlen(path) + 1;
james@406 398
james@406 399 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", index);
james@406 400 req[1].data = Token;
james@406 401 req[1].len = (ULONG)strlen(Token) + 1;
james@406 402
james@504 403 rep = xenbus_format_msg_reply(context, XS_UNWATCH, xbt, req, ARRAY_SIZE(req));
james@406 404
james@406 405 msg = errmsg(rep);
james@406 406 if (msg)
james@406 407 return msg;
james@406 408
james@406 409 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@406 410
james@406 411 return NULL;
james@406 412 }
james@406 413
james@35 414 NTSTATUS
james@622 415 XenBus_Halt(PXENPCI_DEVICE_DATA xpdd)
james@35 416 {
james@406 417 int i;
james@35 418
james@406 419 FUNCTION_ENTER();
james@406 420
james@272 421 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 422
james@406 423 /* we need to remove the watches as a watch firing could lead to a XenBus_Read/Write/Printf */
james@624 424 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@624 425 {
james@406 426 if (xpdd->XenBus_WatchEntries[i].Active)
james@624 427 {
james@624 428 xpdd->XenBus_WatchEntries[i].Active = 0;
james@406 429 XenBus_SendRemWatch(xpdd, XBT_NIL, xpdd->XenBus_WatchEntries[i].Path, i);
james@624 430 }
james@406 431 }
james@406 432
james@624 433 XenBus_Disconnect(xpdd);
james@35 434
james@406 435 FUNCTION_EXIT();
james@35 436
james@0 437 return STATUS_SUCCESS;
james@0 438 }
james@0 439
james@0 440 char *
andy@13 441 XenBus_List(
andy@13 442 PVOID Context,
andy@13 443 xenbus_transaction_t xbt,
james@504 444 char *pre,
andy@13 445 char ***contents)
james@0 446 {
james@258 447 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 448 struct xsd_sockmsg *reply, *repmsg;
james@145 449 struct write_req req[] = { { pre, (ULONG)strlen(pre)+1 } };
james@0 450 ULONG nr_elems, x, i;
james@0 451 char **res;
james@0 452 char *msg;
james@0 453
james@272 454 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 455
james@504 456 repmsg = xenbus_format_msg_reply(xpdd, XS_DIRECTORY, xbt, req, ARRAY_SIZE(req));
james@0 457 msg = errmsg(repmsg);
andy@15 458 if (msg)
andy@15 459 {
james@0 460 *contents = NULL;
james@0 461 return msg;
james@0 462 }
james@0 463 reply = repmsg + 1;
james@0 464 for (x = nr_elems = 0; x < repmsg->len; x++)
andy@15 465 {
james@0 466 nr_elems += (((char *)reply)[x] == 0);
andy@15 467 }
andy@15 468 res = ExAllocatePoolWithTag(NonPagedPool, sizeof(res[0]) * (nr_elems + 1),
andy@15 469 XENPCI_POOL_TAG);
andy@15 470 for (x = i = 0; i < nr_elems; i++)
andy@15 471 {
james@145 472 int l = (int)strlen((char *)reply + x);
james@0 473 res[i] = ExAllocatePoolWithTag(NonPagedPool, l + 1, XENPCI_POOL_TAG);
james@0 474 memcpy(res[i], (char *)reply + x, l + 1);
james@0 475 x += l + 1;
james@0 476 }
james@0 477 res[i] = NULL;
james@0 478 ExFreePoolWithTag(repmsg, XENPCI_POOL_TAG);
james@0 479 *contents = res;
james@624 480
james@0 481 return NULL;
james@0 482 }
james@0 483
james@624 484 /* Called at PASSIVE_LEVEL */
james@279 485 static char *
james@279 486 XenBus_SendAddWatch(
james@279 487 PVOID Context,
james@279 488 xenbus_transaction_t xbt,
james@504 489 char *Path,
james@279 490 int slot)
james@279 491 {
james@279 492 PXENPCI_DEVICE_DATA xpdd = Context;
james@279 493 struct xsd_sockmsg *rep;
james@279 494 char *msg;
james@279 495 char Token[20];
james@279 496 struct write_req req[2];
james@279 497
james@279 498 req[0].data = Path;
james@279 499 req[0].len = (ULONG)strlen(Path) + 1;
james@279 500
james@279 501 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", slot);
james@279 502 req[1].data = Token;
james@279 503 req[1].len = (ULONG)strlen(Token) + 1;
james@279 504
james@504 505 rep = xenbus_format_msg_reply(xpdd, XS_WATCH, xbt, req, ARRAY_SIZE(req));
james@404 506
james@279 507 msg = errmsg(rep);
james@283 508 if (!msg)
james@283 509 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@279 510
james@279 511 return msg;
james@279 512 }
james@279 513
james@342 514 /* called at PASSIVE_LEVEL */
james@390 515 NTSTATUS
james@409 516 XenBus_Suspend(PXENPCI_DEVICE_DATA xpdd)
james@409 517 {
james@409 518 int i;
james@409 519
james@409 520 /* we need to remove the watches as a watch firing could lead to a XenBus_Read/Write/Printf */
james@409 521 for (i = 0; i < MAX_WATCH_ENTRIES; i++) {
james@409 522 if (xpdd->XenBus_WatchEntries[i].Active)
james@409 523 XenBus_SendRemWatch(xpdd, XBT_NIL, xpdd->XenBus_WatchEntries[i].Path, i);
james@409 524 }
james@624 525 XenBus_Disconnect(xpdd);
james@624 526
james@409 527 return STATUS_SUCCESS;
james@409 528 }
james@409 529
james@409 530 /* called at PASSIVE_LEVEL */
james@409 531 NTSTATUS
james@279 532 XenBus_Resume(PXENPCI_DEVICE_DATA xpdd)
james@279 533 {
james@390 534 NTSTATUS status;
james@279 535 int i;
james@341 536
andy@398 537 FUNCTION_ENTER();
james@342 538
james@390 539 status = XenBus_Connect(xpdd);
james@390 540 if (!NT_SUCCESS(status))
james@390 541 {
james@390 542 return status;
james@390 543 }
james@279 544
james@279 545 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@341 546 {
james@279 547 if (xpdd->XenBus_WatchEntries[i].Active)
james@341 548 {
james@536 549 //KdPrint((__DRIVER_NAME " Adding watch for path = %s\n", xpdd->XenBus_WatchEntries[i].Path));
james@279 550 XenBus_SendAddWatch(xpdd, XBT_NIL, xpdd->XenBus_WatchEntries[i].Path, i);
james@341 551 }
james@341 552 }
andy@398 553 FUNCTION_EXIT();
james@390 554
james@390 555 return STATUS_SUCCESS;
james@279 556 }
james@279 557
james@0 558 char *
andy@15 559 XenBus_AddWatch(
andy@13 560 PVOID Context,
andy@13 561 xenbus_transaction_t xbt,
james@504 562 char *Path,
andy@13 563 PXENBUS_WATCH_CALLBACK ServiceRoutine,
andy@13 564 PVOID ServiceContext)
james@0 565 {
james@258 566 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 567 char *msg;
james@0 568 int i;
andy@94 569 PXENBUS_WATCH_ENTRY w_entry;
james@0 570
james@272 571 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@0 572
andy@94 573 ASSERT(strlen(Path) < ARRAY_SIZE(w_entry->Path));
james@0 574
james@504 575 ExAcquireFastMutex(&xpdd->xb_watch_mutex);
james@128 576
james@0 577 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
andy@15 578 if (xpdd->XenBus_WatchEntries[i].Active == 0)
james@0 579 break;
james@0 580
james@0 581 if (i == MAX_WATCH_ENTRIES)
james@0 582 {
james@0 583 KdPrint((__DRIVER_NAME " +++ No more watch slots left\n"));
james@504 584 ExReleaseFastMutex(&xpdd->xb_watch_mutex);
james@0 585 return NULL;
james@0 586 }
james@0 587
andy@94 588 /* must init watchentry before starting watch */
james@128 589
andy@94 590 w_entry = &xpdd->XenBus_WatchEntries[i];
andy@94 591 strncpy(w_entry->Path, Path, ARRAY_SIZE(w_entry->Path));
andy@94 592 w_entry->ServiceRoutine = ServiceRoutine;
andy@94 593 w_entry->ServiceContext = ServiceContext;
andy@94 594 w_entry->Count = 0;
andy@94 595 w_entry->Active = 1;
andy@94 596
james@504 597 ExReleaseFastMutex(&xpdd->xb_watch_mutex);
james@128 598
james@279 599 msg = XenBus_SendAddWatch(xpdd, xbt, Path, i);
james@0 600
andy@93 601 if (msg)
andy@93 602 {
andy@93 603 xpdd->XenBus_WatchEntries[i].Active = 0;
andy@93 604 return msg;
andy@93 605 }
andy@93 606
james@0 607 return NULL;
james@0 608 }
james@0 609
james@0 610 char *
andy@13 611 XenBus_RemWatch(
andy@13 612 PVOID Context,
andy@13 613 xenbus_transaction_t xbt,
james@504 614 char *Path,
andy@13 615 PXENBUS_WATCH_CALLBACK ServiceRoutine,
andy@13 616 PVOID ServiceContext)
james@0 617 {
james@258 618 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 619 char *msg;
james@0 620 int i;
james@0 621
james@272 622 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@0 623
james@504 624 ExAcquireFastMutex(&xpdd->xb_watch_mutex);
james@128 625
james@0 626 // check that Path < 128 chars
james@0 627
james@409 628 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@409 629 {
james@538 630 if (xpdd->XenBus_WatchEntries[i].Active
james@538 631 && !strcmp(xpdd->XenBus_WatchEntries[i].Path, Path)
andy@15 632 && xpdd->XenBus_WatchEntries[i].ServiceRoutine == ServiceRoutine
andy@15 633 && xpdd->XenBus_WatchEntries[i].ServiceContext == ServiceContext)
james@409 634 {
james@409 635 KdPrint((__DRIVER_NAME " Match\n"));
james@0 636 break;
james@409 637 }
james@0 638 }
james@0 639
james@0 640 if (i == MAX_WATCH_ENTRIES)
james@0 641 {
james@504 642 ExReleaseFastMutex(&xpdd->xb_watch_mutex);
james@0 643 KdPrint((__DRIVER_NAME " Watch not set - can't remove\n"));
james@0 644 return NULL;
james@0 645 }
james@0 646
james@128 647 xpdd->XenBus_WatchEntries[i].Active = 0;
james@128 648 xpdd->XenBus_WatchEntries[i].Path[0] = 0;
james@128 649
james@504 650 ExReleaseFastMutex(&xpdd->xb_watch_mutex);
james@0 651
james@406 652 msg = XenBus_SendRemWatch(Context, xbt, Path, i);
james@406 653
james@406 654 return msg;
james@0 655 }
james@0 656
james@0 657 char *
andy@13 658 XenBus_StartTransaction(PVOID Context, xenbus_transaction_t *xbt)
james@0 659 {
james@258 660 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 661 /* xenstored becomes angry if you send a length 0 message, so just
james@0 662 shove a nul terminator on the end */
james@0 663 struct write_req req = { "", 1};
james@0 664 struct xsd_sockmsg *rep;
james@0 665 char *err;
james@0 666
james@272 667 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@95 668
james@504 669 rep = xenbus_format_msg_reply(xpdd, XS_TRANSACTION_START, 0, &req, 1);
james@0 670 err = errmsg(rep);
james@0 671 if (err)
james@0 672 return err;
james@0 673 *xbt = atoi((char *)(rep + 1));
james@0 674 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@95 675
james@0 676 return NULL;
james@0 677 }
james@0 678
james@0 679 char *
andy@15 680 XenBus_EndTransaction(
andy@15 681 PVOID Context,
andy@15 682 xenbus_transaction_t t,
andy@15 683 int abort,
andy@15 684 int *retry)
james@0 685 {
james@258 686 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 687 struct xsd_sockmsg *rep;
james@0 688 struct write_req req;
james@0 689 char *err;
james@0 690
james@0 691 *retry = 0;
james@0 692
james@0 693 req.data = abort ? "F" : "T";
james@0 694 req.len = 2;
james@504 695 rep = xenbus_format_msg_reply(xpdd, XS_TRANSACTION_END, t, &req, 1);
james@0 696 err = errmsg(rep);
james@0 697 if (err) {
james@0 698 if (!strcmp(err, "EAGAIN")) {
james@0 699 *retry = 1;
james@0 700 ExFreePoolWithTag(err, XENPCI_POOL_TAG);
james@0 701 return NULL;
james@0 702 } else {
james@0 703 return err;
james@0 704 }
james@0 705 }
james@0 706 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@95 707
james@0 708 return NULL;
james@0 709 }
james@0 710
james@0 711 char *
andy@15 712 XenBus_Printf(
andy@15 713 PVOID Context,
andy@15 714 xenbus_transaction_t xbt,
james@504 715 char *path,
james@504 716 char *fmt,
andy@15 717 ...)
james@0 718 {
james@258 719 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 720 va_list ap;
andy@101 721 char buf[512];
james@95 722 char *retval;
james@95 723
james@272 724 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@0 725
james@0 726 va_start(ap, fmt);
james@0 727 RtlStringCbVPrintfA(buf, ARRAY_SIZE(buf), fmt, ap);
james@0 728 va_end(ap);
james@258 729 retval = XenBus_Write(xpdd, xbt, path, buf);
james@95 730
james@95 731 return retval;
andy@37 732 }