win-pvdrivers

annotate xenpci/xenbus.c @ 271:da9b1e17fbc0

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