win-pvdrivers

annotate xenpci/xenbus.c @ 272:909b775a891f

Fixed a few bugs in shutdown handler. Drivers appear to be working now. Shutdown not so much yet...
author James Harper <james.harper@bendigoit.com.au>
date Fri May 16 23:56:57 2008 +1000 (2008-05-16)
parents da9b1e17fbc0
children 685399b9adb0
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 "io/xs_wire.h"
james@0 22 #include <stdlib.h>
james@0 23
james@0 24 #pragma warning( disable : 4204 )
james@0 25 #pragma warning( disable : 4221 )
james@0 26
andy@15 27 struct write_req {
andy@15 28 const void *data;
andy@15 29 unsigned len;
james@0 30 };
james@0 31
james@0 32 static void
james@0 33 XenBus_ReadThreadProc(PVOID StartContext);
james@0 34 static void
james@0 35 XenBus_WatchThreadProc(PVOID StartContext);
james@0 36 static BOOLEAN
james@0 37 XenBus_Interrupt(PKINTERRUPT Interrupt, PVOID ServiceContext);
james@0 38
james@258 39 static int allocate_xenbus_id(PXENPCI_DEVICE_DATA xpdd)
james@0 40 {
james@0 41 static int probe;
james@0 42 int o_probe;
james@0 43
james@134 44 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 45
james@0 46 for (;;)
james@0 47 {
james@0 48 // spin_lock(&req_lock);
andy@15 49 if (xpdd->nr_live_reqs < NR_XB_REQS)
james@0 50 break;
james@0 51 // spin_unlock(&req_lock);
james@0 52 // wait_event(req_wq, (nr_live_reqs < NR_REQS));
james@0 53 }
james@0 54
james@0 55 o_probe = probe;
james@0 56
james@0 57 for (;;)
james@0 58 {
andy@15 59 if (!xpdd->req_info[o_probe].In_Use)
james@0 60 break;
andy@15 61 o_probe = (o_probe + 1) % NR_XB_REQS;
james@0 62 // BUG_ON(o_probe == probe);
james@0 63 }
andy@15 64 xpdd->nr_live_reqs++;
andy@15 65 xpdd->req_info[o_probe].In_Use = 1;
andy@15 66 probe = (o_probe + 1) % NR_XB_REQS;
james@0 67 //spin_unlock(&req_lock);
james@0 68 //init_waitqueue_head(&req_info[o_probe].waitq);
andy@15 69 KeInitializeEvent(&xpdd->req_info[o_probe].WaitEvent, SynchronizationEvent, FALSE);
james@0 70
james@134 71 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 72
james@0 73 return o_probe;
james@0 74 }
james@0 75
james@258 76 static void release_xenbus_id(PXENPCI_DEVICE_DATA xpdd, int id)
james@0 77 {
james@0 78 // BUG_ON(!req_info[id].in_use);
james@0 79 // spin_lock(&req_lock);
andy@15 80 xpdd->req_info[id].In_Use = 0;
andy@15 81 xpdd->nr_live_reqs--;
andy@15 82 xpdd->req_info[id].In_Use = 0;
james@0 83 // if (nr_live_reqs == NR_REQS - 1)
james@0 84 // wake_up(&req_wq);
james@0 85 // spin_unlock(&req_lock);
james@0 86 }
james@0 87
james@157 88 // This routine free's the rep structure if there was an error!!!
james@0 89 static char *errmsg(struct xsd_sockmsg *rep)
james@0 90 {
james@0 91 char *res;
andy@15 92
james@0 93 if (!rep) {
james@0 94 char msg[] = "No reply";
james@0 95 size_t len = strlen(msg) + 1;
james@0 96 return memcpy(ExAllocatePoolWithTag(NonPagedPool, len, XENPCI_POOL_TAG), msg, len);
james@0 97 }
james@0 98 if (rep->type != XS_ERROR)
james@0 99 return NULL;
james@0 100 res = ExAllocatePoolWithTag(NonPagedPool, rep->len + 1, XENPCI_POOL_TAG);
james@0 101 memcpy(res, rep + 1, rep->len);
james@0 102 res[rep->len] = 0;
james@0 103 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@0 104 return res;
james@0 105 }
james@0 106
james@0 107 static void memcpy_from_ring(const void *Ring,
james@0 108 void *Dest,
james@0 109 int off,
james@0 110 int len)
james@0 111 {
james@0 112 int c1, c2;
james@0 113 const char *ring = Ring;
james@0 114 char *dest = Dest;
james@0 115 c1 = min(len, XENSTORE_RING_SIZE - off);
james@0 116 c2 = len - c1;
james@0 117 memcpy(dest, ring + off, c1);
james@0 118 memcpy(dest + c1, ring, c2);
james@0 119 }
james@0 120
andy@15 121 static void xb_write(
james@258 122 PXENPCI_DEVICE_DATA xpdd,
andy@13 123 int type,
andy@13 124 int req_id,
andy@13 125 xenbus_transaction_t trans_id,
andy@13 126 const struct write_req *req,
andy@13 127 int nr_reqs)
james@0 128 {
james@0 129 XENSTORE_RING_IDX prod;
james@0 130 int r;
james@0 131 size_t len = 0;
james@0 132 const struct write_req *cur_req;
james@0 133 size_t req_off;
james@0 134 size_t total_off;
james@0 135 size_t this_chunk;
james@0 136 struct xsd_sockmsg m = {type, req_id, trans_id };
james@0 137 struct write_req header_req = { &m, sizeof(m) };
james@0 138
james@134 139 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 140
james@0 141 for (r = 0; r < nr_reqs; r++)
james@145 142 len += (size_t)req[r].len;
james@145 143 m.len = (ULONG)len;
james@0 144 len += sizeof(m);
james@0 145
james@0 146 cur_req = &header_req;
james@0 147
james@0 148 // BUG_ON(len > XENSTORE_RING_SIZE);
james@0 149 /* Wait for the ring to drain to the point where we can send the
james@0 150 message. */
andy@15 151 prod = xpdd->xen_store_interface->req_prod;
james@0 152
james@0 153 //KdPrint((__DRIVER_NAME " prod = %08x\n", prod));
james@0 154
andy@15 155 if (prod + len - xpdd->xen_store_interface->req_cons > XENSTORE_RING_SIZE)
james@0 156 {
james@0 157 /* Wait for there to be space on the ring */
james@0 158 //KdPrint((__DRIVER_NAME " prod %d, len %d, cons %d, size %d; waiting.\n", prod, len, xen_store_interface->req_cons, XENSTORE_RING_SIZE));
james@0 159 // wait_event(xb_waitq, xen_store_interface->req_prod + len - xen_store_interface->req_cons <= XENSTORE_RING_SIZE);
james@0 160 //KdPrint((__DRIVER_NAME " Back from wait.\n"));
andy@15 161 prod = xpdd->xen_store_interface->req_prod;
james@0 162 }
james@0 163
james@0 164 /* We're now guaranteed to be able to send the message without
james@0 165 overflowing the ring. Do so. */
james@0 166
james@0 167 total_off = 0;
james@0 168 req_off = 0;
james@0 169
james@0 170 while (total_off < len)
james@0 171 {
james@0 172 this_chunk = min(cur_req->len - req_off,XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod));
andy@15 173 memcpy((char *)xpdd->xen_store_interface->req + MASK_XENSTORE_IDX(prod), (char *)cur_req->data + req_off, this_chunk);
james@145 174 prod += (XENSTORE_RING_IDX)this_chunk;
james@0 175 req_off += this_chunk;
james@0 176 total_off += this_chunk;
james@0 177 if (req_off == cur_req->len)
james@0 178 {
james@0 179 req_off = 0;
james@0 180 if (cur_req == &header_req)
james@0 181 cur_req = req;
james@0 182 else
james@0 183 cur_req++;
james@0 184 }
james@0 185 }
james@0 186
james@0 187 //KdPrint((__DRIVER_NAME " Complete main loop of xb_write.\n"));
james@0 188
james@0 189 // BUG_ON(req_off != 0);
james@0 190 // BUG_ON(total_off != len);
james@0 191 // BUG_ON(prod > xen_store_interface->req_cons + XENSTORE_RING_SIZE);
james@0 192
james@0 193 /* Remote must see entire message before updating indexes */
james@0 194 //_WriteBarrier();
james@0 195 KeMemoryBarrier();
james@0 196
james@145 197 xpdd->xen_store_interface->req_prod += (XENSTORE_RING_IDX)len;
james@0 198
james@0 199 //KdPrint((__DRIVER_NAME " prod = %08x\n", xen_store_interface->req_prod));
james@0 200
james@0 201 /* Send evtchn to notify remote */
james@258 202 EvtChn_Notify(xpdd, xpdd->xen_store_evtchn);
james@0 203
james@134 204 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 205 }
james@0 206
james@0 207 static struct xsd_sockmsg *
andy@13 208 xenbus_msg_reply(
james@258 209 PXENPCI_DEVICE_DATA xpdd,
andy@13 210 int type,
andy@13 211 xenbus_transaction_t trans,
andy@13 212 struct write_req *io,
andy@13 213 int nr_reqs)
james@0 214 {
james@0 215 int id;
james@0 216
james@134 217 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 218
james@258 219 id = allocate_xenbus_id(xpdd);
andy@94 220
james@258 221 xb_write(xpdd, type, id, trans, io, nr_reqs);
james@95 222
andy@15 223 KeWaitForSingleObject(&xpdd->req_info[id].WaitEvent, Executive, KernelMode, FALSE, NULL);
james@95 224
james@258 225 release_xenbus_id(xpdd, id);
james@0 226
james@134 227 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@96 228
andy@94 229 return xpdd->req_info[id].Reply;
james@0 230 }
james@0 231
james@0 232 char *
andy@15 233 XenBus_Read(
andy@13 234 PVOID Context,
andy@13 235 xenbus_transaction_t xbt,
andy@13 236 const char *path,
andy@13 237 char **value)
james@0 238 {
james@258 239 PXENPCI_DEVICE_DATA xpdd = Context;
james@145 240 struct write_req req[] = { {path, (ULONG)strlen(path) + 1} };
andy@13 241 struct xsd_sockmsg *rep;
andy@13 242 char *res;
andy@13 243 char *msg;
james@0 244
james@134 245 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@95 246
james@272 247 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 248
james@258 249 rep = xenbus_msg_reply(xpdd, XS_READ, xbt, req, ARRAY_SIZE(req));
andy@13 250 msg = errmsg(rep);
andy@13 251 if (msg) {
andy@13 252 *value = NULL;
andy@13 253 return msg;
andy@13 254 }
andy@13 255 res = ExAllocatePoolWithTag(NonPagedPool, rep->len + 1, XENPCI_POOL_TAG);
andy@13 256 memcpy(res, rep + 1, rep->len);
andy@13 257 res[rep->len] = 0;
andy@13 258 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
andy@13 259 *value = res;
james@95 260
james@134 261 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 262
andy@13 263 return NULL;
james@0 264 }
james@0 265
james@0 266 char *
andy@13 267 XenBus_Write(
andy@13 268 PVOID Context,
andy@13 269 xenbus_transaction_t xbt,
andy@13 270 const char *path,
andy@13 271 const char *value)
james@0 272 {
james@258 273 PXENPCI_DEVICE_DATA xpdd = Context;
andy@13 274 struct write_req req[] = {
james@145 275 {path, (ULONG)strlen(path) + 1},
james@145 276 {value, (ULONG)strlen(value) + 1},
james@271 277 // {path, (ULONG)strlen(path)},
james@271 278 // {value, (ULONG)strlen(value)},
andy@13 279 };
andy@13 280 struct xsd_sockmsg *rep;
andy@13 281 char *msg;
james@0 282
james@134 283 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@95 284
james@272 285 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 286
james@258 287 rep = xenbus_msg_reply(xpdd, XS_WRITE, xbt, req, ARRAY_SIZE(req));
andy@13 288 msg = errmsg(rep);
andy@13 289 if (msg)
andy@13 290 return msg;
andy@13 291 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@95 292
james@134 293 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 294
andy@13 295 return NULL;
james@0 296 }
james@0 297
james@0 298 NTSTATUS
james@258 299 XenBus_Init(PXENPCI_DEVICE_DATA xpdd)
james@0 300 {
james@258 301 PHYSICAL_ADDRESS pa_xen_store_interface;
james@258 302 xen_ulong_t xen_store_mfn;
james@35 303 NTSTATUS Status;
james@35 304 int i;
andy@37 305
james@267 306 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@35 307
james@272 308 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 309
james@128 310 KeInitializeSpinLock(&xpdd->WatchLock);
james@128 311
james@258 312 xpdd->xen_store_evtchn = (evtchn_port_t)hvm_get_parameter(xpdd, HVM_PARAM_STORE_EVTCHN);
james@258 313
james@258 314 xen_store_mfn = (xen_ulong_t)hvm_get_parameter(xpdd, HVM_PARAM_STORE_PFN);
james@258 315 pa_xen_store_interface.QuadPart = xen_store_mfn << PAGE_SHIFT;
james@267 316 xpdd->xen_store_interface = MmMapIoSpace(pa_xen_store_interface, PAGE_SIZE, MmNonCached);
james@0 317
james@35 318 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@128 319 {
andy@38 320 xpdd->XenBus_WatchEntries[i].Active = 0;
james@128 321 xpdd->XenBus_WatchEntries[i].Running = 0;
james@128 322 KeInitializeEvent(&xpdd->XenBus_WatchEntries[i].CompleteEvent, SynchronizationEvent, FALSE);
james@128 323 }
james@0 324
andy@15 325 KeInitializeEvent(&xpdd->XenBus_ReadThreadEvent, SynchronizationEvent, FALSE);
andy@15 326 KeInitializeEvent(&xpdd->XenBus_WatchThreadEvent, SynchronizationEvent, FALSE);
andy@38 327 xpdd->XenBus_ShuttingDown = FALSE;
james@0 328
james@258 329 Status = PsCreateSystemThread(&xpdd->XenBus_ReadThreadHandle, THREAD_ALL_ACCESS, NULL, NULL, NULL, XenBus_ReadThreadProc, xpdd);
andy@101 330 if (!NT_SUCCESS(Status))
andy@101 331 {
andy@101 332 KdPrint((__DRIVER_NAME " Could not start read thread\n"));
andy@101 333 return STATUS_UNSUCCESSFUL;
andy@101 334 }
james@35 335
james@258 336 Status = PsCreateSystemThread(&xpdd->XenBus_WatchThreadHandle, THREAD_ALL_ACCESS, NULL, NULL, NULL, XenBus_WatchThreadProc, xpdd);
andy@101 337 if (!NT_SUCCESS(Status))
andy@101 338 {
andy@101 339 KdPrint((__DRIVER_NAME " Could not start watch thread\n"));
andy@101 340 return STATUS_UNSUCCESSFUL;
andy@101 341 }
james@35 342
james@258 343 EvtChn_BindDpc(xpdd, xpdd->xen_store_evtchn, XenBus_Interrupt, xpdd);
james@258 344
james@267 345 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 346
james@0 347 return STATUS_SUCCESS;
james@0 348 }
james@0 349
james@0 350 NTSTATUS
james@258 351 XenBus_Stop(PXENPCI_DEVICE_DATA xpdd)
james@0 352 {
james@258 353 int i;
andy@38 354
james@267 355 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 356
james@258 357 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@258 358 {
james@258 359 if (xpdd->XenBus_WatchEntries[i].Active)
james@258 360 XenBus_RemWatch(xpdd, XBT_NIL, xpdd->XenBus_WatchEntries[i].Path,
james@258 361 xpdd->XenBus_WatchEntries[i].ServiceRoutine,
james@258 362 xpdd->XenBus_WatchEntries[i].ServiceContext);
james@258 363 }
james@258 364
james@258 365 EvtChn_Unbind(xpdd, xpdd->xen_store_evtchn);
james@0 366
james@267 367 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 368
james@0 369 return STATUS_SUCCESS;
james@0 370 }
james@0 371
james@0 372 NTSTATUS
james@258 373 XenBus_Close(PXENPCI_DEVICE_DATA xpdd)
james@0 374 {
andy@56 375 //KWAIT_BLOCK WaitBlockArray[2];
james@35 376 PVOID WaitArray[2];
james@35 377
james@272 378 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 379
andy@38 380 xpdd->XenBus_ShuttingDown = TRUE;
james@35 381
andy@38 382 KeSetEvent(&xpdd->XenBus_ReadThreadEvent, 1, FALSE);
andy@38 383 KeSetEvent(&xpdd->XenBus_WatchThreadEvent, 1, FALSE);
andy@38 384 ObReferenceObjectByHandle(xpdd->XenBus_ReadThreadHandle, THREAD_ALL_ACCESS, NULL, KernelMode, &WaitArray[0], NULL);
andy@38 385 ObReferenceObjectByHandle(xpdd->XenBus_WatchThreadHandle, THREAD_ALL_ACCESS, NULL, KernelMode, &WaitArray[1], NULL);
james@48 386 KeWaitForSingleObject(WaitArray[0], Executive, KernelMode, FALSE, NULL);
james@48 387 KeWaitForSingleObject(WaitArray[1], Executive, KernelMode, FALSE, NULL);
andy@38 388 xpdd->XenBus_ShuttingDown = FALSE;
james@35 389
andy@15 390 ZwClose(xpdd->XenBus_WatchThreadHandle);
andy@15 391 ZwClose(xpdd->XenBus_ReadThreadHandle);
james@0 392
james@267 393 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@35 394
james@0 395 return STATUS_SUCCESS;
james@0 396 }
james@0 397
james@0 398 char *
andy@13 399 XenBus_List(
andy@13 400 PVOID Context,
andy@13 401 xenbus_transaction_t xbt,
andy@13 402 const char *pre,
andy@13 403 char ***contents)
james@0 404 {
james@258 405 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 406 struct xsd_sockmsg *reply, *repmsg;
james@145 407 struct write_req req[] = { { pre, (ULONG)strlen(pre)+1 } };
james@0 408 ULONG nr_elems, x, i;
james@0 409 char **res;
james@0 410 char *msg;
james@0 411
james@134 412 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 413
james@272 414 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 415
james@258 416 repmsg = xenbus_msg_reply(xpdd, XS_DIRECTORY, xbt, req, ARRAY_SIZE(req));
james@0 417 msg = errmsg(repmsg);
andy@15 418 if (msg)
andy@15 419 {
james@0 420 *contents = NULL;
james@134 421 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 422 return msg;
james@0 423 }
james@0 424 reply = repmsg + 1;
james@0 425 for (x = nr_elems = 0; x < repmsg->len; x++)
andy@15 426 {
james@0 427 nr_elems += (((char *)reply)[x] == 0);
andy@15 428 }
andy@15 429 res = ExAllocatePoolWithTag(NonPagedPool, sizeof(res[0]) * (nr_elems + 1),
andy@15 430 XENPCI_POOL_TAG);
andy@15 431 for (x = i = 0; i < nr_elems; i++)
andy@15 432 {
james@145 433 int l = (int)strlen((char *)reply + x);
james@0 434 res[i] = ExAllocatePoolWithTag(NonPagedPool, l + 1, XENPCI_POOL_TAG);
james@0 435 memcpy(res[i], (char *)reply + x, l + 1);
james@0 436 x += l + 1;
james@0 437 }
james@0 438 res[i] = NULL;
james@0 439 ExFreePoolWithTag(repmsg, XENPCI_POOL_TAG);
james@0 440 *contents = res;
james@134 441 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 442 return NULL;
james@0 443 }
james@0 444
andy@15 445 static void
andy@15 446 XenBus_ReadThreadProc(PVOID StartContext)
james@0 447 {
james@0 448 int NewWriteIndex;
james@0 449 struct xsd_sockmsg msg;
james@0 450 char *payload;
james@0 451 char *path, *token;
james@258 452 PXENPCI_DEVICE_DATA xpdd = StartContext;
james@0 453
james@0 454 for(;;)
james@0 455 {
andy@15 456 KeWaitForSingleObject(&xpdd->XenBus_ReadThreadEvent, Executive, KernelMode, FALSE, NULL);
andy@38 457 if (xpdd->XenBus_ShuttingDown)
james@35 458 {
james@35 459 KdPrint((__DRIVER_NAME " Shutdown detected in ReadThreadProc\n"));
james@35 460 PsTerminateSystemThread(0);
james@35 461 }
andy@15 462 while (xpdd->xen_store_interface->rsp_prod != xpdd->xen_store_interface->rsp_cons)
james@0 463 {
james@0 464 //KdPrint((__DRIVER_NAME " a - Rsp_cons %d, rsp_prod %d.\n", xen_store_interface->rsp_cons, xen_store_interface->rsp_prod));
andy@15 465 if (xpdd->xen_store_interface->rsp_prod - xpdd->xen_store_interface->rsp_cons < sizeof(msg))
james@0 466 {
james@0 467 //KdPrint((__DRIVER_NAME " +++ Message incomplete (not even a full header)\n"));
james@0 468 break;
james@0 469 }
james@0 470 //_ReadBarrier();
james@0 471 KeMemoryBarrier();
andy@15 472 memcpy_from_ring(xpdd->xen_store_interface->rsp, &msg,
andy@15 473 MASK_XENSTORE_IDX(xpdd->xen_store_interface->rsp_cons), sizeof(msg));
andy@15 474 if (xpdd->xen_store_interface->rsp_prod - xpdd->xen_store_interface->rsp_cons < sizeof(msg) + msg.len)
james@0 475 {
james@0 476 //KdPrint((__DRIVER_NAME " +++ Message incomplete (header but not full body)\n"));
james@0 477 break;
james@0 478 }
james@0 479
andy@20 480 if (msg.type != XS_WATCH_EVENT)
andy@20 481 {
andy@20 482 xpdd->req_info[msg.req_id].Reply = ExAllocatePoolWithTag(NonPagedPool, sizeof(msg) + msg.len, XENPCI_POOL_TAG);
andy@20 483 memcpy_from_ring(xpdd->xen_store_interface->rsp,
andy@20 484 xpdd->req_info[msg.req_id].Reply,
andy@20 485 MASK_XENSTORE_IDX(xpdd->xen_store_interface->rsp_cons),
andy@20 486 msg.len + sizeof(msg));
andy@20 487 xpdd->xen_store_interface->rsp_cons += msg.len + sizeof(msg);
andy@20 488 KeSetEvent(&xpdd->req_info[msg.req_id].WaitEvent, 1, FALSE);
andy@20 489 }
andy@20 490 else // a watch: add to watch ring and signal watch thread
james@0 491 {
james@0 492 payload = ExAllocatePoolWithTag(NonPagedPool, sizeof(msg) + msg.len, XENPCI_POOL_TAG);
andy@15 493 memcpy_from_ring(xpdd->xen_store_interface->rsp, payload,
andy@15 494 MASK_XENSTORE_IDX(xpdd->xen_store_interface->rsp_cons), msg.len + sizeof(msg));
andy@15 495 xpdd->xen_store_interface->rsp_cons += msg.len + sizeof(msg);
james@0 496 path = payload + sizeof(msg);
james@0 497 token = path + strlen(path) + 1;
james@0 498
andy@15 499 NewWriteIndex = (xpdd->XenBus_WatchRingWriteIndex + 1) & 127;
andy@15 500 if (NewWriteIndex != xpdd->XenBus_WatchRingReadIndex)
james@0 501 {
andy@15 502 strncpy(xpdd->XenBus_WatchRing[NewWriteIndex].Path, path, 128);
andy@15 503 strncpy(xpdd->XenBus_WatchRing[NewWriteIndex].Token, token, 10);
andy@15 504 xpdd->XenBus_WatchRingWriteIndex = NewWriteIndex;
james@0 505 }
james@0 506 else
james@0 507 {
james@0 508 KdPrint((__DRIVER_NAME " +++ Queue full Path = %s Token = %s\n", path, token));
james@0 509 // drop the message on the floor
james@0 510 continue;
james@0 511 }
james@0 512
james@0 513 ExFreePoolWithTag(payload, XENPCI_POOL_TAG);
james@0 514 //KdPrint((__DRIVER_NAME " +++ Watch Path = %s Token = %s\n", path, token));
andy@15 515 KeSetEvent(&xpdd->XenBus_WatchThreadEvent, 1, FALSE);
james@0 516 }
james@0 517 }
james@0 518 }
james@0 519 }
james@0 520
james@0 521 static void
james@0 522 XenBus_WatchThreadProc(PVOID StartContext)
james@0 523 {
james@0 524 int index;
james@0 525 PXENBUS_WATCH_ENTRY entry;
james@258 526 PXENPCI_DEVICE_DATA xpdd = StartContext;
james@128 527 KIRQL OldIrql;
james@0 528
james@0 529 for(;;)
james@0 530 {
andy@37 531 KeWaitForSingleObject(&xpdd->XenBus_WatchThreadEvent, Executive, KernelMode, FALSE, NULL);
andy@38 532 if (xpdd->XenBus_ShuttingDown)
james@35 533 {
james@35 534 KdPrint((__DRIVER_NAME " Shutdown detected in WatchThreadProc\n"));
james@35 535 PsTerminateSystemThread(0);
james@35 536 }
andy@38 537 while (xpdd->XenBus_WatchRingReadIndex != xpdd->XenBus_WatchRingWriteIndex)
james@0 538 {
andy@15 539 xpdd->XenBus_WatchRingReadIndex =
andy@15 540 (xpdd->XenBus_WatchRingReadIndex + 1) % WATCH_RING_SIZE;
andy@15 541 index = atoi(xpdd->XenBus_WatchRing[xpdd->XenBus_WatchRingReadIndex].Token);
james@0 542
andy@15 543 entry = &xpdd->XenBus_WatchEntries[index];
james@128 544 KeAcquireSpinLock(&xpdd->WatchLock, &OldIrql);
james@128 545 if (!entry->Active || !entry->ServiceRoutine)
james@0 546 {
james@128 547 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@138 548 KdPrint((__DRIVER_NAME " No watch for index %d\n", index));
james@0 549 continue;
james@0 550 }
james@138 551 if (entry->RemovePending)
james@138 552 {
james@138 553 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@138 554 KdPrint((__DRIVER_NAME " Not calling watch - remove is pending\n"));
james@138 555 continue;
james@138 556 }
james@128 557 entry->Running = 1;
james@128 558 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@0 559 entry->Count++;
james@138 560 KdPrint((__DRIVER_NAME " --- for %s\n", xpdd->XenBus_WatchRing[xpdd->XenBus_WatchRingReadIndex].Path));
andy@15 561 entry->ServiceRoutine(xpdd->XenBus_WatchRing[xpdd->XenBus_WatchRingReadIndex].Path, entry->ServiceContext);
james@128 562 entry->Running = 0;
james@128 563 KeSetEvent(&entry->CompleteEvent, 1, FALSE);
james@0 564 }
james@0 565 }
james@0 566 }
james@0 567
james@0 568 char *
andy@15 569 XenBus_AddWatch(
andy@13 570 PVOID Context,
andy@13 571 xenbus_transaction_t xbt,
andy@13 572 const char *Path,
andy@13 573 PXENBUS_WATCH_CALLBACK ServiceRoutine,
andy@13 574 PVOID ServiceContext)
james@0 575 {
james@258 576 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 577 struct xsd_sockmsg *rep;
james@0 578 char *msg;
james@0 579 int i;
james@0 580 char Token[20];
james@0 581 struct write_req req[2];
andy@94 582 PXENBUS_WATCH_ENTRY w_entry;
james@128 583 KIRQL OldIrql;
james@0 584
james@267 585 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 586
james@272 587 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 588
andy@94 589 ASSERT(strlen(Path) < ARRAY_SIZE(w_entry->Path));
james@0 590
james@128 591 KeAcquireSpinLock(&xpdd->WatchLock, &OldIrql);
james@128 592
james@0 593 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
andy@15 594 if (xpdd->XenBus_WatchEntries[i].Active == 0)
james@0 595 break;
james@0 596
james@0 597 if (i == MAX_WATCH_ENTRIES)
james@0 598 {
james@0 599 KdPrint((__DRIVER_NAME " +++ No more watch slots left\n"));
james@128 600 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@0 601 return NULL;
james@0 602 }
james@0 603
andy@94 604 /* must init watchentry before starting watch */
james@128 605
andy@94 606 w_entry = &xpdd->XenBus_WatchEntries[i];
andy@94 607 strncpy(w_entry->Path, Path, ARRAY_SIZE(w_entry->Path));
andy@94 608 w_entry->ServiceRoutine = ServiceRoutine;
andy@94 609 w_entry->ServiceContext = ServiceContext;
andy@94 610 w_entry->Count = 0;
andy@94 611 w_entry->Active = 1;
andy@94 612
james@128 613 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@128 614
james@0 615 req[0].data = Path;
james@145 616 req[0].len = (ULONG)strlen(Path) + 1;
james@0 617
james@0 618 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", i);
james@0 619 req[1].data = Token;
james@145 620 req[1].len = (ULONG)strlen(Token) + 1;
james@0 621
james@258 622 rep = xenbus_msg_reply(xpdd, XS_WATCH, xbt, req, ARRAY_SIZE(req));
andy@93 623
andy@93 624 msg = errmsg(rep);
andy@93 625 if (msg)
andy@93 626 {
andy@93 627 xpdd->XenBus_WatchEntries[i].Active = 0;
james@96 628 KdPrint((__DRIVER_NAME " <-- XenBus_AddWatch (%s)\n", msg));
andy@93 629 return msg;
andy@93 630 }
james@157 631 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
andy@93 632
james@153 633 KdPrint((__DRIVER_NAME " <-- XenBus_AddWatch\n"));
james@0 634
james@0 635 return NULL;
james@0 636 }
james@0 637
james@0 638 char *
andy@13 639 XenBus_RemWatch(
andy@13 640 PVOID Context,
andy@13 641 xenbus_transaction_t xbt,
andy@13 642 const char *Path,
andy@13 643 PXENBUS_WATCH_CALLBACK ServiceRoutine,
andy@13 644 PVOID ServiceContext)
james@0 645 {
james@258 646 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 647 struct xsd_sockmsg *rep;
james@0 648 char *msg;
james@0 649 int i;
james@0 650 char Token[20];
james@0 651 struct write_req req[2];
james@128 652 KIRQL OldIrql;
james@0 653
james@267 654 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@272 655 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@0 656
james@128 657 KeAcquireSpinLock(&xpdd->WatchLock, &OldIrql);
james@128 658
james@0 659 // check that Path < 128 chars
james@0 660
james@0 661 for (i = 0; i < MAX_WATCH_ENTRIES; i++) {
andy@15 662 if (xpdd->XenBus_WatchEntries[i].Active == 1
andy@15 663 && strcmp(xpdd->XenBus_WatchEntries[i].Path, Path) == 0
andy@15 664 && xpdd->XenBus_WatchEntries[i].ServiceRoutine == ServiceRoutine
andy@15 665 && xpdd->XenBus_WatchEntries[i].ServiceContext == ServiceContext)
james@0 666 break;
james@0 667 }
james@0 668
james@0 669 if (i == MAX_WATCH_ENTRIES)
james@0 670 {
james@128 671 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@0 672 KdPrint((__DRIVER_NAME " Watch not set - can't remove\n"));
james@0 673 return NULL;
james@0 674 }
james@0 675
james@138 676 if (xpdd->XenBus_WatchEntries[i].RemovePending)
james@128 677 {
james@132 678 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@138 679 KdPrint((__DRIVER_NAME " Remove already pending - can't remove\n"));
james@138 680 return NULL;
james@138 681 }
james@138 682 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@138 683
james@138 684 while (xpdd->XenBus_WatchEntries[i].Running)
james@128 685 KeWaitForSingleObject(&xpdd->XenBus_WatchEntries[i].CompleteEvent, Executive, KernelMode, FALSE, NULL);
james@138 686
james@138 687 KeAcquireSpinLock(&xpdd->WatchLock, &OldIrql);
james@128 688
james@128 689 xpdd->XenBus_WatchEntries[i].Active = 0;
james@138 690 xpdd->XenBus_WatchEntries[i].RemovePending = 0;
james@128 691 xpdd->XenBus_WatchEntries[i].Path[0] = 0;
james@128 692
james@128 693 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@128 694
james@0 695 req[0].data = Path;
james@145 696 req[0].len = (ULONG)strlen(Path) + 1;
james@0 697
james@0 698 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", i);
james@0 699 req[1].data = Token;
james@145 700 req[1].len = (ULONG)strlen(Token) + 1;
james@0 701
james@258 702 rep = xenbus_msg_reply(xpdd, XS_UNWATCH, xbt, req, ARRAY_SIZE(req));
james@0 703
james@0 704 msg = errmsg(rep);
james@0 705 if (msg)
james@128 706 {
james@0 707 return msg;
james@128 708 }
james@0 709
james@0 710 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@0 711
james@267 712 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 713
james@0 714 return NULL;
james@0 715 }
james@0 716
james@0 717
james@0 718 char *
andy@13 719 XenBus_StartTransaction(PVOID Context, xenbus_transaction_t *xbt)
james@0 720 {
james@258 721 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 722 /* xenstored becomes angry if you send a length 0 message, so just
james@0 723 shove a nul terminator on the end */
james@0 724 struct write_req req = { "", 1};
james@0 725 struct xsd_sockmsg *rep;
james@0 726 char *err;
james@0 727
james@134 728 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@272 729 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@95 730
james@258 731 rep = xenbus_msg_reply(xpdd, XS_TRANSACTION_START, 0, &req, 1);
james@0 732 err = errmsg(rep);
james@0 733 if (err)
james@0 734 return err;
james@0 735 *xbt = atoi((char *)(rep + 1));
james@0 736 //sscanf((char *)(rep + 1), "%u", xbt);
james@0 737 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@95 738
james@134 739 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 740
james@0 741 return NULL;
james@0 742 }
james@0 743
james@0 744 char *
andy@15 745 XenBus_EndTransaction(
andy@15 746 PVOID Context,
andy@15 747 xenbus_transaction_t t,
andy@15 748 int abort,
andy@15 749 int *retry)
james@0 750 {
james@258 751 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 752 struct xsd_sockmsg *rep;
james@0 753 struct write_req req;
james@0 754 char *err;
james@0 755
james@134 756 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@95 757
james@0 758 *retry = 0;
james@0 759
james@0 760 req.data = abort ? "F" : "T";
james@0 761 req.len = 2;
james@258 762 rep = xenbus_msg_reply(xpdd, XS_TRANSACTION_END, t, &req, 1);
james@0 763 err = errmsg(rep);
james@0 764 if (err) {
james@0 765 if (!strcmp(err, "EAGAIN")) {
james@0 766 *retry = 1;
james@0 767 ExFreePoolWithTag(err, XENPCI_POOL_TAG);
james@0 768 return NULL;
james@0 769 } else {
james@0 770 return err;
james@0 771 }
james@0 772 }
james@0 773 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@95 774
james@134 775 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 776
james@0 777 return NULL;
james@0 778 }
james@0 779
james@0 780 static BOOLEAN
james@0 781 XenBus_Interrupt(PKINTERRUPT Interrupt, PVOID ServiceContext)
james@0 782 {
james@258 783 PXENPCI_DEVICE_DATA xpdd = ServiceContext;
andy@15 784
james@0 785 UNREFERENCED_PARAMETER(Interrupt);
james@0 786
james@134 787 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 788
andy@15 789 KeSetEvent(&xpdd->XenBus_ReadThreadEvent, 1, FALSE);
james@0 790
james@134 791 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 792
james@0 793 return TRUE;
james@0 794 }
james@0 795
james@0 796 char *
andy@15 797 XenBus_Printf(
andy@15 798 PVOID Context,
andy@15 799 xenbus_transaction_t xbt,
andy@15 800 const char *path,
andy@15 801 const char *fmt,
andy@15 802 ...)
james@0 803 {
james@258 804 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 805 va_list ap;
andy@101 806 char buf[512];
james@95 807 char *retval;
james@95 808
james@134 809 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@272 810 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@0 811
james@0 812 va_start(ap, fmt);
james@0 813 RtlStringCbVPrintfA(buf, ARRAY_SIZE(buf), fmt, ap);
james@0 814 va_end(ap);
james@258 815 retval = XenBus_Write(xpdd, xbt, path, buf);
james@95 816
james@134 817 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 818
james@95 819 return retval;
andy@37 820 }