win-pvdrivers

annotate xenpci/xenbus.c @ 267:f157f82e0293

Continued porting to WDM. vbd is almost working.
author James Harper <james.harper@bendigoit.com.au>
date Mon May 12 00:20:02 2008 +1000 (2008-05-12)
parents 9b16ad37af17
children da9b1e17fbc0
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@258 247 rep = xenbus_msg_reply(xpdd, XS_READ, xbt, req, ARRAY_SIZE(req));
andy@13 248 msg = errmsg(rep);
andy@13 249 if (msg) {
andy@13 250 *value = NULL;
andy@13 251 return msg;
andy@13 252 }
andy@13 253 res = ExAllocatePoolWithTag(NonPagedPool, rep->len + 1, XENPCI_POOL_TAG);
andy@13 254 memcpy(res, rep + 1, rep->len);
andy@13 255 res[rep->len] = 0;
andy@13 256 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
andy@13 257 *value = res;
james@95 258
james@134 259 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 260
andy@13 261 return NULL;
james@0 262 }
james@0 263
james@0 264 char *
andy@13 265 XenBus_Write(
andy@13 266 PVOID Context,
andy@13 267 xenbus_transaction_t xbt,
andy@13 268 const char *path,
andy@13 269 const char *value)
james@0 270 {
james@258 271 PXENPCI_DEVICE_DATA xpdd = Context;
andy@13 272 struct write_req req[] = {
james@145 273 {path, (ULONG)strlen(path) + 1},
james@145 274 {value, (ULONG)strlen(value) + 1},
andy@13 275 };
andy@13 276 struct xsd_sockmsg *rep;
andy@13 277 char *msg;
james@0 278
james@134 279 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@95 280
james@258 281 rep = xenbus_msg_reply(xpdd, XS_WRITE, xbt, req, ARRAY_SIZE(req));
andy@13 282 msg = errmsg(rep);
andy@13 283 if (msg)
andy@13 284 return msg;
andy@13 285 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@95 286
james@134 287 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 288
andy@13 289 return NULL;
james@0 290 }
james@0 291
james@0 292 NTSTATUS
james@258 293 XenBus_Init(PXENPCI_DEVICE_DATA xpdd)
james@0 294 {
james@258 295 PHYSICAL_ADDRESS pa_xen_store_interface;
james@258 296 xen_ulong_t xen_store_mfn;
james@35 297 NTSTATUS Status;
james@35 298 int i;
andy@37 299
james@267 300 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@35 301
james@128 302 KeInitializeSpinLock(&xpdd->WatchLock);
james@128 303
james@258 304 xpdd->xen_store_evtchn = (evtchn_port_t)hvm_get_parameter(xpdd, HVM_PARAM_STORE_EVTCHN);
james@258 305
james@258 306 xen_store_mfn = (xen_ulong_t)hvm_get_parameter(xpdd, HVM_PARAM_STORE_PFN);
james@258 307 pa_xen_store_interface.QuadPart = xen_store_mfn << PAGE_SHIFT;
james@267 308 xpdd->xen_store_interface = MmMapIoSpace(pa_xen_store_interface, PAGE_SIZE, MmNonCached);
james@0 309
james@35 310 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@128 311 {
andy@38 312 xpdd->XenBus_WatchEntries[i].Active = 0;
james@128 313 xpdd->XenBus_WatchEntries[i].Running = 0;
james@128 314 KeInitializeEvent(&xpdd->XenBus_WatchEntries[i].CompleteEvent, SynchronizationEvent, FALSE);
james@128 315 }
james@0 316
andy@15 317 KeInitializeEvent(&xpdd->XenBus_ReadThreadEvent, SynchronizationEvent, FALSE);
andy@15 318 KeInitializeEvent(&xpdd->XenBus_WatchThreadEvent, SynchronizationEvent, FALSE);
andy@38 319 xpdd->XenBus_ShuttingDown = FALSE;
james@0 320
james@258 321 Status = PsCreateSystemThread(&xpdd->XenBus_ReadThreadHandle, THREAD_ALL_ACCESS, NULL, NULL, NULL, XenBus_ReadThreadProc, xpdd);
andy@101 322 if (!NT_SUCCESS(Status))
andy@101 323 {
andy@101 324 KdPrint((__DRIVER_NAME " Could not start read thread\n"));
andy@101 325 return STATUS_UNSUCCESSFUL;
andy@101 326 }
james@35 327
james@258 328 Status = PsCreateSystemThread(&xpdd->XenBus_WatchThreadHandle, THREAD_ALL_ACCESS, NULL, NULL, NULL, XenBus_WatchThreadProc, xpdd);
andy@101 329 if (!NT_SUCCESS(Status))
andy@101 330 {
andy@101 331 KdPrint((__DRIVER_NAME " Could not start watch thread\n"));
andy@101 332 return STATUS_UNSUCCESSFUL;
andy@101 333 }
james@35 334
james@258 335 EvtChn_BindDpc(xpdd, xpdd->xen_store_evtchn, XenBus_Interrupt, xpdd);
james@258 336
james@267 337 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 338
james@0 339 return STATUS_SUCCESS;
james@0 340 }
james@0 341
james@0 342 NTSTATUS
james@258 343 XenBus_Stop(PXENPCI_DEVICE_DATA xpdd)
james@0 344 {
james@258 345 int i;
andy@38 346
james@267 347 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 348
james@258 349 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
james@258 350 {
james@258 351 if (xpdd->XenBus_WatchEntries[i].Active)
james@258 352 XenBus_RemWatch(xpdd, XBT_NIL, xpdd->XenBus_WatchEntries[i].Path,
james@258 353 xpdd->XenBus_WatchEntries[i].ServiceRoutine,
james@258 354 xpdd->XenBus_WatchEntries[i].ServiceContext);
james@258 355 }
james@258 356
james@258 357 EvtChn_Unbind(xpdd, xpdd->xen_store_evtchn);
james@0 358
james@267 359 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 360
james@0 361 return STATUS_SUCCESS;
james@0 362 }
james@0 363
james@0 364 NTSTATUS
james@258 365 XenBus_Close(PXENPCI_DEVICE_DATA xpdd)
james@0 366 {
andy@56 367 //KWAIT_BLOCK WaitBlockArray[2];
james@35 368 PVOID WaitArray[2];
james@35 369
andy@38 370 xpdd->XenBus_ShuttingDown = TRUE;
james@35 371
andy@38 372 KeSetEvent(&xpdd->XenBus_ReadThreadEvent, 1, FALSE);
andy@38 373 KeSetEvent(&xpdd->XenBus_WatchThreadEvent, 1, FALSE);
andy@38 374 ObReferenceObjectByHandle(xpdd->XenBus_ReadThreadHandle, THREAD_ALL_ACCESS, NULL, KernelMode, &WaitArray[0], NULL);
andy@38 375 ObReferenceObjectByHandle(xpdd->XenBus_WatchThreadHandle, THREAD_ALL_ACCESS, NULL, KernelMode, &WaitArray[1], NULL);
james@48 376 KeWaitForSingleObject(WaitArray[0], Executive, KernelMode, FALSE, NULL);
james@48 377 KeWaitForSingleObject(WaitArray[1], Executive, KernelMode, FALSE, NULL);
andy@38 378 xpdd->XenBus_ShuttingDown = FALSE;
james@35 379
andy@15 380 ZwClose(xpdd->XenBus_WatchThreadHandle);
andy@15 381 ZwClose(xpdd->XenBus_ReadThreadHandle);
james@0 382
james@267 383 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@35 384
james@0 385 return STATUS_SUCCESS;
james@0 386 }
james@0 387
james@0 388 char *
andy@13 389 XenBus_List(
andy@13 390 PVOID Context,
andy@13 391 xenbus_transaction_t xbt,
andy@13 392 const char *pre,
andy@13 393 char ***contents)
james@0 394 {
james@258 395 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 396 struct xsd_sockmsg *reply, *repmsg;
james@145 397 struct write_req req[] = { { pre, (ULONG)strlen(pre)+1 } };
james@0 398 ULONG nr_elems, x, i;
james@0 399 char **res;
james@0 400 char *msg;
james@0 401
james@134 402 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 403
james@258 404 repmsg = xenbus_msg_reply(xpdd, XS_DIRECTORY, xbt, req, ARRAY_SIZE(req));
james@0 405 msg = errmsg(repmsg);
andy@15 406 if (msg)
andy@15 407 {
james@0 408 *contents = NULL;
james@134 409 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 410 return msg;
james@0 411 }
james@0 412 reply = repmsg + 1;
james@0 413 for (x = nr_elems = 0; x < repmsg->len; x++)
andy@15 414 {
james@0 415 nr_elems += (((char *)reply)[x] == 0);
andy@15 416 }
andy@15 417 res = ExAllocatePoolWithTag(NonPagedPool, sizeof(res[0]) * (nr_elems + 1),
andy@15 418 XENPCI_POOL_TAG);
andy@15 419 for (x = i = 0; i < nr_elems; i++)
andy@15 420 {
james@145 421 int l = (int)strlen((char *)reply + x);
james@0 422 res[i] = ExAllocatePoolWithTag(NonPagedPool, l + 1, XENPCI_POOL_TAG);
james@0 423 memcpy(res[i], (char *)reply + x, l + 1);
james@0 424 x += l + 1;
james@0 425 }
james@0 426 res[i] = NULL;
james@0 427 ExFreePoolWithTag(repmsg, XENPCI_POOL_TAG);
james@0 428 *contents = res;
james@134 429 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 430 return NULL;
james@0 431 }
james@0 432
andy@15 433 static void
andy@15 434 XenBus_ReadThreadProc(PVOID StartContext)
james@0 435 {
james@0 436 int NewWriteIndex;
james@0 437 struct xsd_sockmsg msg;
james@0 438 char *payload;
james@0 439 char *path, *token;
james@258 440 PXENPCI_DEVICE_DATA xpdd = StartContext;
james@0 441
james@0 442 for(;;)
james@0 443 {
andy@15 444 KeWaitForSingleObject(&xpdd->XenBus_ReadThreadEvent, Executive, KernelMode, FALSE, NULL);
andy@38 445 if (xpdd->XenBus_ShuttingDown)
james@35 446 {
james@35 447 KdPrint((__DRIVER_NAME " Shutdown detected in ReadThreadProc\n"));
james@35 448 PsTerminateSystemThread(0);
james@35 449 }
andy@15 450 while (xpdd->xen_store_interface->rsp_prod != xpdd->xen_store_interface->rsp_cons)
james@0 451 {
james@0 452 //KdPrint((__DRIVER_NAME " a - Rsp_cons %d, rsp_prod %d.\n", xen_store_interface->rsp_cons, xen_store_interface->rsp_prod));
andy@15 453 if (xpdd->xen_store_interface->rsp_prod - xpdd->xen_store_interface->rsp_cons < sizeof(msg))
james@0 454 {
james@0 455 //KdPrint((__DRIVER_NAME " +++ Message incomplete (not even a full header)\n"));
james@0 456 break;
james@0 457 }
james@0 458 //_ReadBarrier();
james@0 459 KeMemoryBarrier();
andy@15 460 memcpy_from_ring(xpdd->xen_store_interface->rsp, &msg,
andy@15 461 MASK_XENSTORE_IDX(xpdd->xen_store_interface->rsp_cons), sizeof(msg));
andy@15 462 if (xpdd->xen_store_interface->rsp_prod - xpdd->xen_store_interface->rsp_cons < sizeof(msg) + msg.len)
james@0 463 {
james@0 464 //KdPrint((__DRIVER_NAME " +++ Message incomplete (header but not full body)\n"));
james@0 465 break;
james@0 466 }
james@0 467
andy@20 468 if (msg.type != XS_WATCH_EVENT)
andy@20 469 {
andy@20 470 xpdd->req_info[msg.req_id].Reply = ExAllocatePoolWithTag(NonPagedPool, sizeof(msg) + msg.len, XENPCI_POOL_TAG);
andy@20 471 memcpy_from_ring(xpdd->xen_store_interface->rsp,
andy@20 472 xpdd->req_info[msg.req_id].Reply,
andy@20 473 MASK_XENSTORE_IDX(xpdd->xen_store_interface->rsp_cons),
andy@20 474 msg.len + sizeof(msg));
andy@20 475 xpdd->xen_store_interface->rsp_cons += msg.len + sizeof(msg);
andy@20 476 KeSetEvent(&xpdd->req_info[msg.req_id].WaitEvent, 1, FALSE);
andy@20 477 }
andy@20 478 else // a watch: add to watch ring and signal watch thread
james@0 479 {
james@0 480 payload = ExAllocatePoolWithTag(NonPagedPool, sizeof(msg) + msg.len, XENPCI_POOL_TAG);
andy@15 481 memcpy_from_ring(xpdd->xen_store_interface->rsp, payload,
andy@15 482 MASK_XENSTORE_IDX(xpdd->xen_store_interface->rsp_cons), msg.len + sizeof(msg));
andy@15 483 xpdd->xen_store_interface->rsp_cons += msg.len + sizeof(msg);
james@0 484 path = payload + sizeof(msg);
james@0 485 token = path + strlen(path) + 1;
james@0 486
andy@15 487 NewWriteIndex = (xpdd->XenBus_WatchRingWriteIndex + 1) & 127;
andy@15 488 if (NewWriteIndex != xpdd->XenBus_WatchRingReadIndex)
james@0 489 {
andy@15 490 strncpy(xpdd->XenBus_WatchRing[NewWriteIndex].Path, path, 128);
andy@15 491 strncpy(xpdd->XenBus_WatchRing[NewWriteIndex].Token, token, 10);
andy@15 492 xpdd->XenBus_WatchRingWriteIndex = NewWriteIndex;
james@0 493 }
james@0 494 else
james@0 495 {
james@0 496 KdPrint((__DRIVER_NAME " +++ Queue full Path = %s Token = %s\n", path, token));
james@0 497 // drop the message on the floor
james@0 498 continue;
james@0 499 }
james@0 500
james@0 501 ExFreePoolWithTag(payload, XENPCI_POOL_TAG);
james@0 502 //KdPrint((__DRIVER_NAME " +++ Watch Path = %s Token = %s\n", path, token));
andy@15 503 KeSetEvent(&xpdd->XenBus_WatchThreadEvent, 1, FALSE);
james@0 504 }
james@0 505 }
james@0 506 }
james@0 507 }
james@0 508
james@0 509 static void
james@0 510 XenBus_WatchThreadProc(PVOID StartContext)
james@0 511 {
james@0 512 int index;
james@0 513 PXENBUS_WATCH_ENTRY entry;
james@258 514 PXENPCI_DEVICE_DATA xpdd = StartContext;
james@128 515 KIRQL OldIrql;
james@0 516
james@0 517 for(;;)
james@0 518 {
andy@37 519 KeWaitForSingleObject(&xpdd->XenBus_WatchThreadEvent, Executive, KernelMode, FALSE, NULL);
andy@38 520 if (xpdd->XenBus_ShuttingDown)
james@35 521 {
james@35 522 KdPrint((__DRIVER_NAME " Shutdown detected in WatchThreadProc\n"));
james@35 523 PsTerminateSystemThread(0);
james@35 524 }
andy@38 525 while (xpdd->XenBus_WatchRingReadIndex != xpdd->XenBus_WatchRingWriteIndex)
james@0 526 {
andy@15 527 xpdd->XenBus_WatchRingReadIndex =
andy@15 528 (xpdd->XenBus_WatchRingReadIndex + 1) % WATCH_RING_SIZE;
andy@15 529 index = atoi(xpdd->XenBus_WatchRing[xpdd->XenBus_WatchRingReadIndex].Token);
james@0 530
andy@15 531 entry = &xpdd->XenBus_WatchEntries[index];
james@128 532 KeAcquireSpinLock(&xpdd->WatchLock, &OldIrql);
james@128 533 if (!entry->Active || !entry->ServiceRoutine)
james@0 534 {
james@128 535 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@138 536 KdPrint((__DRIVER_NAME " No watch for index %d\n", index));
james@0 537 continue;
james@0 538 }
james@138 539 if (entry->RemovePending)
james@138 540 {
james@138 541 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@138 542 KdPrint((__DRIVER_NAME " Not calling watch - remove is pending\n"));
james@138 543 continue;
james@138 544 }
james@128 545 entry->Running = 1;
james@128 546 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@0 547 entry->Count++;
james@138 548 KdPrint((__DRIVER_NAME " --- for %s\n", xpdd->XenBus_WatchRing[xpdd->XenBus_WatchRingReadIndex].Path));
andy@15 549 entry->ServiceRoutine(xpdd->XenBus_WatchRing[xpdd->XenBus_WatchRingReadIndex].Path, entry->ServiceContext);
james@128 550 entry->Running = 0;
james@128 551 KeSetEvent(&entry->CompleteEvent, 1, FALSE);
james@0 552 }
james@0 553 }
james@0 554 }
james@0 555
james@0 556 char *
andy@15 557 XenBus_AddWatch(
andy@13 558 PVOID Context,
andy@13 559 xenbus_transaction_t xbt,
andy@13 560 const char *Path,
andy@13 561 PXENBUS_WATCH_CALLBACK ServiceRoutine,
andy@13 562 PVOID ServiceContext)
james@0 563 {
james@258 564 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 565 struct xsd_sockmsg *rep;
james@0 566 char *msg;
james@0 567 int i;
james@0 568 char Token[20];
james@0 569 struct write_req req[2];
andy@94 570 PXENBUS_WATCH_ENTRY w_entry;
james@128 571 KIRQL OldIrql;
james@0 572
james@267 573 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 574
andy@94 575 ASSERT(strlen(Path) < ARRAY_SIZE(w_entry->Path));
james@0 576
james@128 577 KeAcquireSpinLock(&xpdd->WatchLock, &OldIrql);
james@128 578
james@0 579 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
andy@15 580 if (xpdd->XenBus_WatchEntries[i].Active == 0)
james@0 581 break;
james@0 582
james@0 583 if (i == MAX_WATCH_ENTRIES)
james@0 584 {
james@0 585 KdPrint((__DRIVER_NAME " +++ No more watch slots left\n"));
james@128 586 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@0 587 return NULL;
james@0 588 }
james@0 589
andy@94 590 /* must init watchentry before starting watch */
james@128 591
andy@94 592 w_entry = &xpdd->XenBus_WatchEntries[i];
andy@94 593 strncpy(w_entry->Path, Path, ARRAY_SIZE(w_entry->Path));
andy@94 594 w_entry->ServiceRoutine = ServiceRoutine;
andy@94 595 w_entry->ServiceContext = ServiceContext;
andy@94 596 w_entry->Count = 0;
andy@94 597 w_entry->Active = 1;
andy@94 598
james@128 599 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@128 600
james@0 601 req[0].data = Path;
james@145 602 req[0].len = (ULONG)strlen(Path) + 1;
james@0 603
james@0 604 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", i);
james@0 605 req[1].data = Token;
james@145 606 req[1].len = (ULONG)strlen(Token) + 1;
james@0 607
james@258 608 rep = xenbus_msg_reply(xpdd, XS_WATCH, xbt, req, ARRAY_SIZE(req));
andy@93 609
andy@93 610 msg = errmsg(rep);
andy@93 611 if (msg)
andy@93 612 {
andy@93 613 xpdd->XenBus_WatchEntries[i].Active = 0;
james@96 614 KdPrint((__DRIVER_NAME " <-- XenBus_AddWatch (%s)\n", msg));
andy@93 615 return msg;
andy@93 616 }
james@157 617 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
andy@93 618
james@153 619 KdPrint((__DRIVER_NAME " <-- XenBus_AddWatch\n"));
james@0 620
james@0 621 return NULL;
james@0 622 }
james@0 623
james@0 624 char *
andy@13 625 XenBus_RemWatch(
andy@13 626 PVOID Context,
andy@13 627 xenbus_transaction_t xbt,
andy@13 628 const char *Path,
andy@13 629 PXENBUS_WATCH_CALLBACK ServiceRoutine,
andy@13 630 PVOID ServiceContext)
james@0 631 {
james@258 632 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 633 struct xsd_sockmsg *rep;
james@0 634 char *msg;
james@0 635 int i;
james@0 636 char Token[20];
james@0 637 struct write_req req[2];
james@128 638 KIRQL OldIrql;
james@0 639
james@267 640 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 641
james@128 642 KeAcquireSpinLock(&xpdd->WatchLock, &OldIrql);
james@128 643
james@0 644 // check that Path < 128 chars
james@0 645
james@0 646 for (i = 0; i < MAX_WATCH_ENTRIES; i++) {
andy@15 647 if (xpdd->XenBus_WatchEntries[i].Active == 1
andy@15 648 && strcmp(xpdd->XenBus_WatchEntries[i].Path, Path) == 0
andy@15 649 && xpdd->XenBus_WatchEntries[i].ServiceRoutine == ServiceRoutine
andy@15 650 && xpdd->XenBus_WatchEntries[i].ServiceContext == ServiceContext)
james@0 651 break;
james@0 652 }
james@0 653
james@0 654 if (i == MAX_WATCH_ENTRIES)
james@0 655 {
james@128 656 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@0 657 KdPrint((__DRIVER_NAME " Watch not set - can't remove\n"));
james@0 658 return NULL;
james@0 659 }
james@0 660
james@138 661 if (xpdd->XenBus_WatchEntries[i].RemovePending)
james@128 662 {
james@132 663 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@138 664 KdPrint((__DRIVER_NAME " Remove already pending - can't remove\n"));
james@138 665 return NULL;
james@138 666 }
james@138 667 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@138 668
james@138 669 while (xpdd->XenBus_WatchEntries[i].Running)
james@128 670 KeWaitForSingleObject(&xpdd->XenBus_WatchEntries[i].CompleteEvent, Executive, KernelMode, FALSE, NULL);
james@138 671
james@138 672 KeAcquireSpinLock(&xpdd->WatchLock, &OldIrql);
james@128 673
james@128 674 xpdd->XenBus_WatchEntries[i].Active = 0;
james@138 675 xpdd->XenBus_WatchEntries[i].RemovePending = 0;
james@128 676 xpdd->XenBus_WatchEntries[i].Path[0] = 0;
james@128 677
james@128 678 KeReleaseSpinLock(&xpdd->WatchLock, OldIrql);
james@128 679
james@0 680 req[0].data = Path;
james@145 681 req[0].len = (ULONG)strlen(Path) + 1;
james@0 682
james@0 683 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", i);
james@0 684 req[1].data = Token;
james@145 685 req[1].len = (ULONG)strlen(Token) + 1;
james@0 686
james@258 687 rep = xenbus_msg_reply(xpdd, XS_UNWATCH, xbt, req, ARRAY_SIZE(req));
james@0 688
james@0 689 msg = errmsg(rep);
james@0 690 if (msg)
james@128 691 {
james@0 692 return msg;
james@128 693 }
james@0 694
james@0 695 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@0 696
james@267 697 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 698
james@0 699 return NULL;
james@0 700 }
james@0 701
james@0 702
james@0 703 char *
andy@13 704 XenBus_StartTransaction(PVOID Context, xenbus_transaction_t *xbt)
james@0 705 {
james@258 706 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 707 /* xenstored becomes angry if you send a length 0 message, so just
james@0 708 shove a nul terminator on the end */
james@0 709 struct write_req req = { "", 1};
james@0 710 struct xsd_sockmsg *rep;
james@0 711 char *err;
james@0 712
james@134 713 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@95 714
james@258 715 rep = xenbus_msg_reply(xpdd, XS_TRANSACTION_START, 0, &req, 1);
james@0 716 err = errmsg(rep);
james@0 717 if (err)
james@0 718 return err;
james@0 719 *xbt = atoi((char *)(rep + 1));
james@0 720 //sscanf((char *)(rep + 1), "%u", xbt);
james@0 721 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@95 722
james@134 723 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 724
james@0 725 return NULL;
james@0 726 }
james@0 727
james@0 728 char *
andy@15 729 XenBus_EndTransaction(
andy@15 730 PVOID Context,
andy@15 731 xenbus_transaction_t t,
andy@15 732 int abort,
andy@15 733 int *retry)
james@0 734 {
james@258 735 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 736 struct xsd_sockmsg *rep;
james@0 737 struct write_req req;
james@0 738 char *err;
james@0 739
james@134 740 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@95 741
james@0 742 *retry = 0;
james@0 743
james@0 744 req.data = abort ? "F" : "T";
james@0 745 req.len = 2;
james@258 746 rep = xenbus_msg_reply(xpdd, XS_TRANSACTION_END, t, &req, 1);
james@0 747 err = errmsg(rep);
james@0 748 if (err) {
james@0 749 if (!strcmp(err, "EAGAIN")) {
james@0 750 *retry = 1;
james@0 751 ExFreePoolWithTag(err, XENPCI_POOL_TAG);
james@0 752 return NULL;
james@0 753 } else {
james@0 754 return err;
james@0 755 }
james@0 756 }
james@0 757 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
james@95 758
james@134 759 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 760
james@0 761 return NULL;
james@0 762 }
james@0 763
james@0 764 static BOOLEAN
james@0 765 XenBus_Interrupt(PKINTERRUPT Interrupt, PVOID ServiceContext)
james@0 766 {
james@258 767 PXENPCI_DEVICE_DATA xpdd = ServiceContext;
andy@15 768
james@0 769 UNREFERENCED_PARAMETER(Interrupt);
james@0 770
james@134 771 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 772
andy@15 773 KeSetEvent(&xpdd->XenBus_ReadThreadEvent, 1, FALSE);
james@0 774
james@134 775 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@0 776
james@0 777 return TRUE;
james@0 778 }
james@0 779
james@0 780 char *
andy@15 781 XenBus_Printf(
andy@15 782 PVOID Context,
andy@15 783 xenbus_transaction_t xbt,
andy@15 784 const char *path,
andy@15 785 const char *fmt,
andy@15 786 ...)
james@0 787 {
james@258 788 PXENPCI_DEVICE_DATA xpdd = Context;
james@0 789 va_list ap;
andy@101 790 char buf[512];
james@95 791 char *retval;
james@95 792
james@134 793 // KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
james@0 794
james@0 795 va_start(ap, fmt);
james@0 796 RtlStringCbVPrintfA(buf, ARRAY_SIZE(buf), fmt, ap);
james@0 797 va_end(ap);
james@258 798 retval = XenBus_Write(xpdd, xbt, path, buf);
james@95 799
james@134 800 // KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
james@95 801
james@95 802 return retval;
andy@37 803 }