win-pvdrivers

annotate xenpci/xenbus.c @ 279:685399b9adb0

Converted the suspend code to WDM. Still far from working properly but it's a starting point.
author James Harper <james.harper@bendigoit.com.au>
date Fri May 23 23:53:02 2008 +1000 (2008-05-23)
parents 909b775a891f
children 3c65d6c6453f
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@279 568 static char *
james@279 569 XenBus_SendAddWatch(
james@279 570 PVOID Context,
james@279 571 xenbus_transaction_t xbt,
james@279 572 const char *Path,
james@279 573 int slot)
james@279 574 {
james@279 575 PXENPCI_DEVICE_DATA xpdd = Context;
james@279 576 struct xsd_sockmsg *rep;
james@279 577 char *msg;
james@279 578 char Token[20];
james@279 579 struct write_req req[2];
james@279 580
james@279 581 req[0].data = Path;
james@279 582 req[0].len = (ULONG)strlen(Path) + 1;
james@279 583
james@279 584 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", slot);
james@279 585 req[1].data = Token;
james@279 586 req[1].len = (ULONG)strlen(Token) + 1;
james@279 587
james@279 588 rep = xenbus_msg_reply(xpdd, XS_WATCH, xbt, req, ARRAY_SIZE(req));
james@279 589 msg = errmsg(rep);
james@279 590
james@279 591 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@279 592
james@279 593 return msg;
james@279 594 }
james@279 595
james@279 596 VOID
james@279 597 XenBus_Resume(PXENPCI_DEVICE_DATA xpdd)
james@279 598 {
james@279 599 int i;
james@279 600
james@279 601 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@279 602 if (xpdd->XenBus_WatchEntries[i].Active)
james@279 603 XenBus_SendAddWatch(xpdd, XBT_NIL, xpdd->XenBus_WatchEntries[i].Path, i);
james@279 604 }
james@279 605
james@0 606 char *
andy@15 607 XenBus_AddWatch(
andy@13 608 PVOID Context,
andy@13 609 xenbus_transaction_t xbt,
andy@13 610 const char *Path,
andy@13 611 PXENBUS_WATCH_CALLBACK ServiceRoutine,
andy@13 612 PVOID ServiceContext)
james@0 613 {
james@258 614 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 615 char *msg;
james@0 616 int i;
andy@94 617 PXENBUS_WATCH_ENTRY w_entry;
james@128 618 KIRQL OldIrql;
james@0 619
james@267 620 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 621
james@272 622 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@272 623
andy@94 624 ASSERT(strlen(Path) < ARRAY_SIZE(w_entry->Path));
james@0 625
james@128 626 KeAcquireSpinLock(&xpdd->WatchLock, &OldIrql);
james@128 627
james@0 628 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
andy@15 629 if (xpdd->XenBus_WatchEntries[i].Active == 0)
james@0 630 break;
james@0 631
james@0 632 if (i == MAX_WATCH_ENTRIES)
james@0 633 {
james@0 634 KdPrint((__DRIVER_NAME " +++ No more watch slots left\n"));
james@128 635 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@0 636 return NULL;
james@0 637 }
james@0 638
andy@94 639 /* must init watchentry before starting watch */
james@128 640
andy@94 641 w_entry = &xpdd->XenBus_WatchEntries[i];
andy@94 642 strncpy(w_entry->Path, Path, ARRAY_SIZE(w_entry->Path));
andy@94 643 w_entry->ServiceRoutine = ServiceRoutine;
andy@94 644 w_entry->ServiceContext = ServiceContext;
andy@94 645 w_entry->Count = 0;
andy@94 646 w_entry->Active = 1;
andy@94 647
james@128 648 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@128 649
james@279 650 msg = XenBus_SendAddWatch(xpdd, xbt, Path, i);
james@0 651
andy@93 652 if (msg)
andy@93 653 {
andy@93 654 xpdd->XenBus_WatchEntries[i].Active = 0;
james@96 655 KdPrint((__DRIVER_NAME " <-- XenBus_AddWatch (%s)\n", msg));
andy@93 656 return msg;
andy@93 657 }
andy@93 658
james@153 659 KdPrint((__DRIVER_NAME " <-- XenBus_AddWatch\n"));
james@0 660
james@0 661 return NULL;
james@0 662 }
james@0 663
james@0 664 char *
andy@13 665 XenBus_RemWatch(
andy@13 666 PVOID Context,
andy@13 667 xenbus_transaction_t xbt,
andy@13 668 const char *Path,
andy@13 669 PXENBUS_WATCH_CALLBACK ServiceRoutine,
andy@13 670 PVOID ServiceContext)
james@0 671 {
james@258 672 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 673 struct xsd_sockmsg *rep;
james@0 674 char *msg;
james@0 675 int i;
james@0 676 char Token[20];
james@0 677 struct write_req req[2];
james@128 678 KIRQL OldIrql;
james@0 679
james@267 680 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@272 681 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@0 682
james@128 683 KeAcquireSpinLock(&xpdd->WatchLock, &OldIrql);
james@128 684
james@0 685 // check that Path < 128 chars
james@0 686
james@0 687 for (i = 0; i < MAX_WATCH_ENTRIES; i++) {
andy@15 688 if (xpdd->XenBus_WatchEntries[i].Active == 1
andy@15 689 && strcmp(xpdd->XenBus_WatchEntries[i].Path, Path) == 0
andy@15 690 && xpdd->XenBus_WatchEntries[i].ServiceRoutine == ServiceRoutine
andy@15 691 && xpdd->XenBus_WatchEntries[i].ServiceContext == ServiceContext)
james@0 692 break;
james@0 693 }
james@0 694
james@0 695 if (i == MAX_WATCH_ENTRIES)
james@0 696 {
james@128 697 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@0 698 KdPrint((__DRIVER_NAME " Watch not set - can't remove\n"));
james@0 699 return NULL;
james@0 700 }
james@0 701
james@138 702 if (xpdd->XenBus_WatchEntries[i].RemovePending)
james@128 703 {
james@132 704 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@138 705 KdPrint((__DRIVER_NAME " Remove already pending - can't remove\n"));
james@138 706 return NULL;
james@138 707 }
james@138 708 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@138 709
james@138 710 while (xpdd->XenBus_WatchEntries[i].Running)
james@128 711 KeWaitForSingleObject(&xpdd->XenBus_WatchEntries[i].CompleteEvent, Executive, KernelMode, FALSE, NULL);
james@138 712
james@138 713 KeAcquireSpinLock(&xpdd->WatchLock, &OldIrql);
james@128 714
james@128 715 xpdd->XenBus_WatchEntries[i].Active = 0;
james@138 716 xpdd->XenBus_WatchEntries[i].RemovePending = 0;
james@128 717 xpdd->XenBus_WatchEntries[i].Path[0] = 0;
james@128 718
james@128 719 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@128 720
james@0 721 req[0].data = Path;
james@145 722 req[0].len = (ULONG)strlen(Path) + 1;
james@0 723
james@0 724 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", i);
james@0 725 req[1].data = Token;
james@145 726 req[1].len = (ULONG)strlen(Token) + 1;
james@0 727
james@258 728 rep = xenbus_msg_reply(xpdd, XS_UNWATCH, xbt, req, ARRAY_SIZE(req));
james@0 729
james@0 730 msg = errmsg(rep);
james@0 731 if (msg)
james@128 732 {
james@0 733 return msg;
james@128 734 }
james@0 735
james@0 736 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@0 737
james@267 738 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 739
james@0 740 return NULL;
james@0 741 }
james@0 742
james@0 743
james@0 744 char *
andy@13 745 XenBus_StartTransaction(PVOID Context, xenbus_transaction_t *xbt)
james@0 746 {
james@258 747 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 748 /* xenstored becomes angry if you send a length 0 message, so just
james@0 749 shove a nul terminator on the end */
james@0 750 struct write_req req = { "", 1};
james@0 751 struct xsd_sockmsg *rep;
james@0 752 char *err;
james@0 753
james@134 754 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@272 755 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@95 756
james@258 757 rep = xenbus_msg_reply(xpdd, XS_TRANSACTION_START, 0, &req, 1);
james@0 758 err = errmsg(rep);
james@0 759 if (err)
james@0 760 return err;
james@0 761 *xbt = atoi((char *)(rep + 1));
james@0 762 //sscanf((char *)(rep + 1), "%u", xbt);
james@0 763 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@95 764
james@134 765 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 766
james@0 767 return NULL;
james@0 768 }
james@0 769
james@0 770 char *
andy@15 771 XenBus_EndTransaction(
andy@15 772 PVOID Context,
andy@15 773 xenbus_transaction_t t,
andy@15 774 int abort,
andy@15 775 int *retry)
james@0 776 {
james@258 777 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 778 struct xsd_sockmsg *rep;
james@0 779 struct write_req req;
james@0 780 char *err;
james@0 781
james@134 782 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@95 783
james@0 784 *retry = 0;
james@0 785
james@0 786 req.data = abort ? "F" : "T";
james@0 787 req.len = 2;
james@258 788 rep = xenbus_msg_reply(xpdd, XS_TRANSACTION_END, t, &req, 1);
james@0 789 err = errmsg(rep);
james@0 790 if (err) {
james@0 791 if (!strcmp(err, "EAGAIN")) {
james@0 792 *retry = 1;
james@0 793 ExFreePoolWithTag(err, XENPCI_POOL_TAG);
james@0 794 return NULL;
james@0 795 } else {
james@0 796 return err;
james@0 797 }
james@0 798 }
james@0 799 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@95 800
james@134 801 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 802
james@0 803 return NULL;
james@0 804 }
james@0 805
james@0 806 static BOOLEAN
james@0 807 XenBus_Interrupt(PKINTERRUPT Interrupt, PVOID ServiceContext)
james@0 808 {
james@258 809 PXENPCI_DEVICE_DATA xpdd = ServiceContext;
andy@15 810
james@0 811 UNREFERENCED_PARAMETER(Interrupt);
james@0 812
james@134 813 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 814
andy@15 815 KeSetEvent(&xpdd->XenBus_ReadThreadEvent, 1, FALSE);
james@0 816
james@134 817 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 818
james@0 819 return TRUE;
james@0 820 }
james@0 821
james@0 822 char *
andy@15 823 XenBus_Printf(
andy@15 824 PVOID Context,
andy@15 825 xenbus_transaction_t xbt,
andy@15 826 const char *path,
andy@15 827 const char *fmt,
andy@15 828 ...)
james@0 829 {
james@258 830 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 831 va_list ap;
andy@101 832 char buf[512];
james@95 833 char *retval;
james@95 834
james@134 835 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@272 836 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
james@0 837
james@0 838 va_start(ap, fmt);
james@0 839 RtlStringCbVPrintfA(buf, ARRAY_SIZE(buf), fmt, ap);
james@0 840 va_end(ap);
james@258 841 retval = XenBus_Write(xpdd, xbt, path, buf);
james@95 842
james@134 843 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 844
james@95 845 return retval;
andy@37 846 }