win-pvdrivers

view xenpci/xenbus.c @ 841:2f4e64007b0f

Hibernation and suspend/resume fixes.
Storport now handles large requests and breaks them up
author James Harper <james.harper@bendigoit.com.au>
date Sat Feb 05 17:32:12 2011 +1100 (2011-02-05)
parents e5651aba4e03
children 0fd4e07ab4ae
line source
1 /*
2 PV Drivers for Windows Xen HVM Domains
3 Copyright (C) 2007 James Harper
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
20 #include "xenpci.h"
21 #include <stdlib.h>
23 #pragma warning( disable : 4204 )
24 #pragma warning( disable : 4221 )
26 /* Not really necessary but keeps PREfast happy */
27 static EVT_WDF_WORKITEM XenBus_WatchWorkItemProc;
29 WDF_DECLARE_CONTEXT_TYPE(xsd_sockmsg_t)
31 struct write_req {
32 void *data;
33 unsigned len;
34 };
36 // This routine free's the rep structure if there was an error!!!
37 static char *errmsg(struct xsd_sockmsg *rep)
38 {
39 char *res;
41 if (!rep) {
42 char msg[] = "No reply";
43 size_t len = strlen(msg) + 1;
44 return memcpy(ExAllocatePoolWithTag(NonPagedPool, len, XENPCI_POOL_TAG), msg, len);
45 }
46 if (rep->type != XS_ERROR)
47 return NULL;
48 res = ExAllocatePoolWithTag(NonPagedPool, rep->len + 1, XENPCI_POOL_TAG);
49 memcpy(res, rep + 1, rep->len);
50 res[rep->len] = 0;
51 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
52 return res;
53 }
55 static void memcpy_from_ring(void *Ring,
56 void *Dest,
57 int off,
58 int len)
59 {
60 int c1, c2;
61 char *ring = Ring;
62 char *dest = Dest;
63 c1 = min(len, XENSTORE_RING_SIZE - off);
64 c2 = len - c1;
65 memcpy(dest, ring + off, c1);
66 memcpy(dest + c1, ring, c2);
67 }
69 /* called with xenbus_mutex held */
70 static void xb_write(
71 PXENPCI_DEVICE_DATA xpdd,
72 PVOID data,
73 ULONG len
74 )
75 {
76 XENSTORE_RING_IDX prod;
77 ULONG copy_len;
78 PUCHAR ptr;
79 ULONG remaining;
81 //FUNCTION_ENTER();
83 ASSERT(len <= XENSTORE_RING_SIZE);
84 prod = xpdd->xen_store_interface->req_prod;
85 ptr = data;
86 remaining = len;
87 while (remaining)
88 {
89 copy_len = min(remaining, XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod));
90 memcpy((PUCHAR)xpdd->xen_store_interface->req + MASK_XENSTORE_IDX(prod), ptr, copy_len);
91 prod += (XENSTORE_RING_IDX)copy_len;
92 ptr += copy_len;
93 remaining -= copy_len;
94 }
95 /* Remote must see entire message before updating indexes */
96 KeMemoryBarrier();
97 xpdd->xen_store_interface->req_prod = prod;
98 EvtChn_Notify(xpdd, xpdd->xen_store_evtchn);
100 //FUNCTION_EXIT();
101 }
103 /* takes and releases xb_request_mutex */
104 static struct xsd_sockmsg *
105 xenbus_format_msg_reply(
106 PXENPCI_DEVICE_DATA xpdd,
107 int type,
108 xenbus_transaction_t trans_id,
109 struct write_req *req,
110 int nr_reqs)
111 {
112 struct xsd_sockmsg msg;
113 struct xsd_sockmsg *reply;
114 int i;
116 //FUNCTION_ENTER();
118 msg.type = type;
119 msg.req_id = 0;
120 msg.tx_id = trans_id;
121 msg.len = 0;
122 for (i = 0; i < nr_reqs; i++)
123 msg.len += req[i].len;
125 ExAcquireFastMutex(&xpdd->xb_request_mutex);
126 xb_write(xpdd, &msg, sizeof(msg));
127 for (i = 0; i < nr_reqs; i++)
128 xb_write(xpdd, req[i].data, req[i].len);
130 KeWaitForSingleObject(&xpdd->xb_request_complete_event, Executive, KernelMode, FALSE, NULL);
131 reply = xpdd->xb_reply;
132 xpdd->xb_reply = NULL;
133 ExReleaseFastMutex(&xpdd->xb_request_mutex);
135 //FUNCTION_EXIT();
137 return reply;
138 }
140 /* takes and releases xb_request_mutex */
141 struct xsd_sockmsg *
142 XenBus_Raw(
143 PXENPCI_DEVICE_DATA xpdd,
144 struct xsd_sockmsg *msg)
145 {
146 struct xsd_sockmsg *reply;
148 //FUNCTION_ENTER();
150 ExAcquireFastMutex(&xpdd->xb_request_mutex);
151 xb_write(xpdd, msg, sizeof(struct xsd_sockmsg) + msg->len);
152 KeWaitForSingleObject(&xpdd->xb_request_complete_event, Executive, KernelMode, FALSE, NULL);
153 reply = xpdd->xb_reply;
154 xpdd->xb_reply = NULL;
155 ExReleaseFastMutex(&xpdd->xb_request_mutex);
157 //FUNCTION_EXIT();
159 return reply;
160 }
162 /* Called at PASSIVE_LEVEL */
163 char *
164 XenBus_Read(
165 PVOID Context,
166 xenbus_transaction_t xbt,
167 char *path,
168 char **value)
169 {
170 PXENPCI_DEVICE_DATA xpdd = Context;
171 struct write_req req[] = { {path, (ULONG)strlen(path) + 1} };
172 struct xsd_sockmsg *rep;
173 char *res;
174 char *msg;
176 //KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
178 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
180 rep = xenbus_format_msg_reply(xpdd, XS_READ, xbt, req, ARRAY_SIZE(req));
181 msg = errmsg(rep);
182 if (msg) {
183 *value = NULL;
184 return msg;
185 }
186 res = ExAllocatePoolWithTag(NonPagedPool, rep->len + 1, XENPCI_POOL_TAG);
187 memcpy(res, rep + 1, rep->len);
188 res[rep->len] = 0;
189 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
190 *value = res;
192 //KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
194 return NULL;
195 }
197 /* Called at PASSIVE_LEVEL */
198 char *
199 XenBus_Write(
200 PVOID Context,
201 xenbus_transaction_t xbt,
202 char *path,
203 char *value)
204 {
205 PXENPCI_DEVICE_DATA xpdd = Context;
206 struct write_req req[] = {
207 {path, (ULONG)strlen(path) + 1},
208 {value, (ULONG)strlen(value)},
209 };
210 struct xsd_sockmsg *rep;
211 char *msg;
213 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
215 rep = xenbus_format_msg_reply(xpdd, XS_WRITE, xbt, req, ARRAY_SIZE(req));
216 msg = errmsg(rep);
217 if (msg)
218 return msg;
219 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
221 return NULL;
222 }
224 /* Called at PASSIVE_LEVEL */
225 static VOID
226 XenBus_WatchWorkItemProc(WDFWORKITEM workitem)
227 {
228 WDFDEVICE device = WdfWorkItemGetParentObject(workitem);
229 PXENPCI_DEVICE_DATA xpdd = GetXpdd(device);
230 xsd_sockmsg_t *msg;
231 PCHAR path;
232 int index;
233 PXENBUS_WATCH_ENTRY entry;
234 PXENBUS_WATCH_CALLBACK service_routine;
235 PVOID service_context;
237 //FUNCTION_ENTER();
238 msg = WdfObjectGetTypedContext(workitem, xsd_sockmsg_t);
239 path = (PCHAR)msg + sizeof(xsd_sockmsg_t);
240 index = atoi(path + strlen(path) + 1);
241 if (index < 0 || index >= MAX_WATCH_ENTRIES)
242 {
243 KdPrint((__DRIVER_NAME " Watch index %d out of range\n", index));
244 WdfObjectDelete(workitem);
245 //FUNCTION_ENTER();
246 return;
247 }
248 ExAcquireFastMutex(&xpdd->xb_watch_mutex);
249 entry = &xpdd->XenBus_WatchEntries[index];
250 if (!entry->Active || !entry->ServiceRoutine)
251 {
252 KdPrint((__DRIVER_NAME " No watch for index %d\n", index));
253 ExReleaseFastMutex(&xpdd->xb_watch_mutex);
254 WdfObjectDelete(workitem);
255 //FUNCTION_ENTER();
256 return;
257 }
258 entry->Count++;
259 service_routine = entry->ServiceRoutine;
260 service_context = entry->ServiceContext;
261 service_routine(path, service_context);
262 ExReleaseFastMutex(&xpdd->xb_watch_mutex);
263 WdfObjectDelete(workitem);
264 //FUNCTION_EXIT();
265 }
267 /* Called at DISPATCH_LEVEL */
268 static VOID
269 XenBus_Dpc(PVOID ServiceContext)
270 {
271 NTSTATUS status;
272 PXENPCI_DEVICE_DATA xpdd = ServiceContext;
273 xsd_sockmsg_t msg;
274 ULONG msg_len;
275 WDF_WORKITEM_CONFIG workitem_config;
276 WDF_OBJECT_ATTRIBUTES workitem_attributes;
277 WDFWORKITEM workitem;
279 //FUNCTION_ENTER();
281 KeAcquireSpinLockAtDpcLevel(&xpdd->xb_ring_spinlock);
283 while (xpdd->xen_store_interface->rsp_prod != xpdd->xen_store_interface->rsp_cons)
284 {
285 if (!xpdd->xb_msg)
286 {
287 if (xpdd->xen_store_interface->rsp_prod - xpdd->xen_store_interface->rsp_cons < sizeof(xsd_sockmsg_t))
288 {
289 //KdPrint((__DRIVER_NAME " +++ Message incomplete (not even a full header)\n"));
290 break;
291 }
292 KeMemoryBarrier();
293 memcpy_from_ring(xpdd->xen_store_interface->rsp, &msg,
294 MASK_XENSTORE_IDX(xpdd->xen_store_interface->rsp_cons), sizeof(xsd_sockmsg_t));
295 xpdd->xb_msg = ExAllocatePoolWithTag(NonPagedPool, sizeof(xsd_sockmsg_t) + msg.len, XENPCI_POOL_TAG);
296 memcpy(xpdd->xb_msg, &msg, sizeof(xsd_sockmsg_t));
297 xpdd->xb_msg_offset = sizeof(xsd_sockmsg_t);
298 xpdd->xen_store_interface->rsp_cons += sizeof(xsd_sockmsg_t);
299 }
301 msg_len = min(xpdd->xen_store_interface->rsp_prod - xpdd->xen_store_interface->rsp_cons, sizeof(xsd_sockmsg_t) + xpdd->xb_msg->len - xpdd->xb_msg_offset);
302 KeMemoryBarrier(); /* make sure the data in the ring is valid */
303 ASSERT(xpdd->xb_msg_offset + msg_len <= sizeof(xsd_sockmsg_t) + xpdd->xb_msg->len);
304 memcpy_from_ring(xpdd->xen_store_interface->rsp,
305 (PUCHAR)xpdd->xb_msg + xpdd->xb_msg_offset,
306 MASK_XENSTORE_IDX(xpdd->xen_store_interface->rsp_cons),
307 msg_len);
308 xpdd->xen_store_interface->rsp_cons += msg_len;
309 xpdd->xb_msg_offset += msg_len;
311 if (xpdd->xb_msg_offset < sizeof(xsd_sockmsg_t) + xpdd->xb_msg->len)
312 {
313 //KdPrint((__DRIVER_NAME " +++ Message incomplete (header but not full body)\n"));
314 EvtChn_Notify(xpdd, xpdd->xen_store_evtchn); /* there is room on the ring now */
315 break;
316 }
318 if (xpdd->xb_msg->type != XS_WATCH_EVENT)
319 {
320 /* process reply - only ever one outstanding */
321 ASSERT(xpdd->xb_reply == NULL);
322 xpdd->xb_reply = xpdd->xb_msg;
323 xpdd->xb_msg = NULL;
324 KeSetEvent(&xpdd->xb_request_complete_event, IO_NO_INCREMENT, FALSE);
325 }
326 else
327 {
328 /* process watch */
329 WDF_WORKITEM_CONFIG_INIT(&workitem_config, XenBus_WatchWorkItemProc);
330 WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&workitem_attributes, xsd_sockmsg_t);
331 workitem_attributes.ParentObject = xpdd->wdf_device;
332 workitem_attributes.ContextSizeOverride = xpdd->xb_msg_offset;
333 status = WdfWorkItemCreate(&workitem_config, &workitem_attributes, &workitem);
334 if (!NT_SUCCESS(status))
335 {
336 KdPrint((__DRIVER_NAME " Failed to create work item for watch\n"));
337 continue;
338 }
339 memcpy(WdfObjectGetTypedContext(workitem, xsd_sockmsg_t), xpdd->xb_msg, xpdd->xb_msg_offset);
340 xpdd->xb_msg = NULL;
341 WdfWorkItemEnqueue(workitem);
342 }
343 EvtChn_Notify(xpdd, xpdd->xen_store_evtchn); /* there is room on the ring now */
344 }
345 KeReleaseSpinLockFromDpcLevel(&xpdd->xb_ring_spinlock);
347 //FUNCTION_EXIT();
348 }
350 static NTSTATUS
351 XenBus_Connect(PXENPCI_DEVICE_DATA xpdd)
352 {
353 PHYSICAL_ADDRESS pa_xen_store_interface;
354 xen_ulong_t xen_store_mfn;
356 xpdd->xen_store_evtchn = (evtchn_port_t)hvm_get_parameter(xpdd, HVM_PARAM_STORE_EVTCHN);
357 xen_store_mfn = (xen_ulong_t)hvm_get_parameter(xpdd, HVM_PARAM_STORE_PFN);
358 pa_xen_store_interface.QuadPart = (ULONGLONG)xen_store_mfn << PAGE_SHIFT;
359 xpdd->xen_store_interface = MmMapIoSpace(pa_xen_store_interface, PAGE_SIZE, MmNonCached);
361 EvtChn_BindDpc(xpdd, xpdd->xen_store_evtchn, XenBus_Dpc, xpdd, EVT_ACTION_FLAGS_NO_SUSPEND);
363 return STATUS_SUCCESS;
364 }
366 static NTSTATUS
367 XenBus_Disconnect(PXENPCI_DEVICE_DATA xpdd)
368 {
369 EvtChn_Unbind(xpdd, xpdd->xen_store_evtchn);
371 MmUnmapIoSpace(xpdd->xen_store_interface, PAGE_SIZE);
373 return STATUS_SUCCESS;
374 }
376 NTSTATUS
377 XenBus_Init(PXENPCI_DEVICE_DATA xpdd)
378 {
379 NTSTATUS status;
380 int i;
382 FUNCTION_ENTER();
384 ASSERT(KeGetCurrentIrql() == PASSIVE_LEVEL);
386 KeInitializeSpinLock(&xpdd->xb_ring_spinlock);
387 ExInitializeFastMutex(&xpdd->xb_request_mutex);
388 ExInitializeFastMutex(&xpdd->xb_watch_mutex);
390 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
391 {
392 xpdd->XenBus_WatchEntries[i].Active = 0;
393 }
395 KeInitializeEvent(&xpdd->xb_request_complete_event, SynchronizationEvent, FALSE);
397 status = XenBus_Connect(xpdd);
398 if (!NT_SUCCESS(status))
399 {
400 FUNCTION_EXIT();
401 return status;
402 }
404 FUNCTION_EXIT();
406 return STATUS_SUCCESS;
407 }
409 char *
410 XenBus_SendRemWatch(
411 PVOID context,
412 xenbus_transaction_t xbt,
413 char *path,
414 int index)
415 {
416 struct xsd_sockmsg *rep;
417 char *msg;
418 char Token[20];
419 struct write_req req[2];
421 req[0].data = path;
422 req[0].len = (ULONG)strlen(path) + 1;
424 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", index);
425 req[1].data = Token;
426 req[1].len = (ULONG)strlen(Token) + 1;
428 rep = xenbus_format_msg_reply(context, XS_UNWATCH, xbt, req, ARRAY_SIZE(req));
430 msg = errmsg(rep);
431 if (msg)
432 return msg;
434 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
436 return NULL;
437 }
439 NTSTATUS
440 XenBus_Halt(PXENPCI_DEVICE_DATA xpdd)
441 {
442 int i;
444 FUNCTION_ENTER();
446 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
448 /* we need to remove the watches as a watch firing could lead to a XenBus_Read/Write/Printf */
449 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
450 {
451 if (xpdd->XenBus_WatchEntries[i].Active)
452 {
453 xpdd->XenBus_WatchEntries[i].Active = 0;
454 XenBus_SendRemWatch(xpdd, XBT_NIL, xpdd->XenBus_WatchEntries[i].Path, i);
455 }
456 }
458 XenBus_Disconnect(xpdd);
460 FUNCTION_EXIT();
462 return STATUS_SUCCESS;
463 }
465 char *
466 XenBus_List(
467 PVOID Context,
468 xenbus_transaction_t xbt,
469 char *pre,
470 char ***contents)
471 {
472 PXENPCI_DEVICE_DATA xpdd = Context;
473 struct xsd_sockmsg *reply, *repmsg;
474 struct write_req req[] = { { pre, (ULONG)strlen(pre)+1 } };
475 ULONG nr_elems, x, i;
476 char **res;
477 char *msg;
479 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
481 repmsg = xenbus_format_msg_reply(xpdd, XS_DIRECTORY, xbt, req, ARRAY_SIZE(req));
482 msg = errmsg(repmsg);
483 if (msg)
484 {
485 *contents = NULL;
486 return msg;
487 }
488 reply = repmsg + 1;
489 for (x = nr_elems = 0; x < repmsg->len; x++)
490 {
491 nr_elems += (((char *)reply)[x] == 0);
492 }
493 res = ExAllocatePoolWithTag(NonPagedPool, sizeof(res[0]) * (nr_elems + 1),
494 XENPCI_POOL_TAG);
495 for (x = i = 0; i < nr_elems; i++)
496 {
497 int l = (int)strlen((char *)reply + x);
498 res[i] = ExAllocatePoolWithTag(NonPagedPool, l + 1, XENPCI_POOL_TAG);
499 memcpy(res[i], (char *)reply + x, l + 1);
500 x += l + 1;
501 }
502 res[i] = NULL;
503 ExFreePoolWithTag(repmsg, XENPCI_POOL_TAG);
504 *contents = res;
506 return NULL;
507 }
509 /* Called at PASSIVE_LEVEL */
510 static char *
511 XenBus_SendAddWatch(
512 PVOID Context,
513 xenbus_transaction_t xbt,
514 char *Path,
515 int slot)
516 {
517 PXENPCI_DEVICE_DATA xpdd = Context;
518 struct xsd_sockmsg *rep;
519 char *msg;
520 char Token[20];
521 struct write_req req[2];
523 req[0].data = Path;
524 req[0].len = (ULONG)strlen(Path) + 1;
526 RtlStringCbPrintfA(Token, ARRAY_SIZE(Token), "%d", slot);
527 req[1].data = Token;
528 req[1].len = (ULONG)strlen(Token) + 1;
530 rep = xenbus_format_msg_reply(xpdd, XS_WATCH, xbt, req, ARRAY_SIZE(req));
532 msg = errmsg(rep);
533 if (!msg)
534 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
536 return msg;
537 }
539 /* called at PASSIVE_LEVEL */
540 NTSTATUS
541 XenBus_Suspend(PXENPCI_DEVICE_DATA xpdd)
542 {
543 int i;
545 /* we need to remove the watches as a watch firing could lead to a XenBus_Read/Write/Printf */
546 for (i = 0; i < MAX_WATCH_ENTRIES; i++) {
547 if (xpdd->XenBus_WatchEntries[i].Active)
548 XenBus_SendRemWatch(xpdd, XBT_NIL, xpdd->XenBus_WatchEntries[i].Path, i);
549 }
550 XenBus_Disconnect(xpdd);
552 return STATUS_SUCCESS;
553 }
555 /* called at PASSIVE_LEVEL */
556 NTSTATUS
557 XenBus_Resume(PXENPCI_DEVICE_DATA xpdd)
558 {
559 NTSTATUS status;
560 int i;
562 FUNCTION_ENTER();
564 status = XenBus_Connect(xpdd);
565 if (!NT_SUCCESS(status))
566 {
567 return status;
568 }
570 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
571 {
572 if (xpdd->XenBus_WatchEntries[i].Active)
573 {
574 KdPrint((__DRIVER_NAME " Adding watch for path = %s\n", xpdd->XenBus_WatchEntries[i].Path));
575 XenBus_SendAddWatch(xpdd, XBT_NIL, xpdd->XenBus_WatchEntries[i].Path, i);
576 }
577 }
579 FUNCTION_EXIT();
581 return STATUS_SUCCESS;
582 }
584 char *
585 XenBus_AddWatch(
586 PVOID Context,
587 xenbus_transaction_t xbt,
588 char *Path,
589 PXENBUS_WATCH_CALLBACK ServiceRoutine,
590 PVOID ServiceContext)
591 {
592 PXENPCI_DEVICE_DATA xpdd = Context;
593 char *msg;
594 int i;
595 PXENBUS_WATCH_ENTRY w_entry;
597 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
599 ASSERT(strlen(Path) < ARRAY_SIZE(w_entry->Path));
601 ExAcquireFastMutex(&xpdd->xb_watch_mutex);
603 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
604 if (xpdd->XenBus_WatchEntries[i].Active == 0)
605 break;
607 if (i == MAX_WATCH_ENTRIES)
608 {
609 KdPrint((__DRIVER_NAME " +++ No more watch slots left\n"));
610 ExReleaseFastMutex(&xpdd->xb_watch_mutex);
611 return NULL;
612 }
614 /* must init watchentry before starting watch */
616 w_entry = &xpdd->XenBus_WatchEntries[i];
617 RtlStringCbCopyA(w_entry->Path, ARRAY_SIZE(w_entry->Path), Path);
618 w_entry->ServiceRoutine = ServiceRoutine;
619 w_entry->ServiceContext = ServiceContext;
620 w_entry->Count = 0;
621 w_entry->Active = 1;
623 ExReleaseFastMutex(&xpdd->xb_watch_mutex);
625 msg = XenBus_SendAddWatch(xpdd, xbt, Path, i);
627 if (msg)
628 {
629 xpdd->XenBus_WatchEntries[i].Active = 0;
630 return msg;
631 }
633 return NULL;
634 }
636 char *
637 XenBus_RemWatch(
638 PVOID Context,
639 xenbus_transaction_t xbt,
640 char *Path,
641 PXENBUS_WATCH_CALLBACK ServiceRoutine,
642 PVOID ServiceContext)
643 {
644 PXENPCI_DEVICE_DATA xpdd = Context;
645 char *msg;
646 int i;
648 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
650 ExAcquireFastMutex(&xpdd->xb_watch_mutex);
652 // check that Path < 128 chars
654 for (i = 0; i < MAX_WATCH_ENTRIES; i++)
655 {
656 if (xpdd->XenBus_WatchEntries[i].Active
657 && !strcmp(xpdd->XenBus_WatchEntries[i].Path, Path)
658 && xpdd->XenBus_WatchEntries[i].ServiceRoutine == ServiceRoutine
659 && xpdd->XenBus_WatchEntries[i].ServiceContext == ServiceContext)
660 {
661 KdPrint((__DRIVER_NAME " Match\n"));
662 break;
663 }
664 }
666 if (i == MAX_WATCH_ENTRIES)
667 {
668 ExReleaseFastMutex(&xpdd->xb_watch_mutex);
669 KdPrint((__DRIVER_NAME " Watch not set for %s - can't remove\n", Path));
670 return NULL;
671 }
673 xpdd->XenBus_WatchEntries[i].Active = 0;
674 xpdd->XenBus_WatchEntries[i].Path[0] = 0;
676 ExReleaseFastMutex(&xpdd->xb_watch_mutex);
678 msg = XenBus_SendRemWatch(Context, xbt, Path, i);
680 return msg;
681 }
683 char *
684 XenBus_StartTransaction(PVOID Context, xenbus_transaction_t *xbt)
685 {
686 PXENPCI_DEVICE_DATA xpdd = Context;
687 /* xenstored becomes angry if you send a length 0 message, so just
688 shove a nul terminator on the end */
689 struct write_req req = { "", 1};
690 struct xsd_sockmsg *rep;
691 char *err;
693 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
695 rep = xenbus_format_msg_reply(xpdd, XS_TRANSACTION_START, 0, &req, 1);
696 err = errmsg(rep);
697 if (err)
698 return err;
699 *xbt = atoi((char *)(rep + 1));
700 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
702 return NULL;
703 }
705 char *
706 XenBus_EndTransaction(
707 PVOID Context,
708 xenbus_transaction_t t,
709 int abort,
710 int *retry)
711 {
712 PXENPCI_DEVICE_DATA xpdd = Context;
713 struct xsd_sockmsg *rep;
714 struct write_req req;
715 char *err;
717 *retry = 0;
719 req.data = abort ? "F" : "T";
720 req.len = 2;
721 rep = xenbus_format_msg_reply(xpdd, XS_TRANSACTION_END, t, &req, 1);
722 err = errmsg(rep);
723 if (err) {
724 if (!strcmp(err, "EAGAIN")) {
725 *retry = 1;
726 ExFreePoolWithTag(err, XENPCI_POOL_TAG);
727 return NULL;
728 } else {
729 return err;
730 }
731 }
732 ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
734 return NULL;
735 }
737 char *
738 XenBus_Printf(
739 PVOID Context,
740 xenbus_transaction_t xbt,
741 char *path,
742 char *fmt,
743 ...)
744 {
745 PXENPCI_DEVICE_DATA xpdd = Context;
746 va_list ap;
747 char buf[512];
748 char *retval;
750 ASSERT(KeGetCurrentIrql() < DISPATCH_LEVEL);
752 va_start(ap, fmt);
753 RtlStringCbVPrintfA(buf, ARRAY_SIZE(buf), fmt, ap);
754 va_end(ap);
755 retval = XenBus_Write(xpdd, xbt, path, buf);
757 return retval;
758 }