win-pvdrivers

changeset 13:be8c09632f31

fixes to xenpci

Move xenpci globals into XENPCI_DEVICE_DATA struct
External interfaces take a PVOID Context field
Use __writemsr intrinsic
Some lines broken for length
Dead (?) io queue code removed
XEN_IFACE_XEN removed, no one seems to use it?
Use case 3: in XenPCI_XenBusWatchHandler instead of default:
Xenvbd and xennet changed to pass back context field when calling xenpci interfaces
author Andy Grover <andy@groveronline.com>
date Wed Nov 28 14:51:17 2007 -0800 (2007-11-28)
parents 5712dede5a1b
children bde45c4eca0c
files common/include/evtchn_public.h common/include/gnttbl_public.h common/include/hypercall.h common/include/xenbus_public.h xennet/xennet.c xenpci/evtchn.c xenpci/gnttbl.c xenpci/xenbus.c xenpci/xenbus.h xenpci/xenpci.c xenpci/xenpci.h xenvbd/xenvbd.c xenvbd/xenvbd.h
line diff
     1.1 --- a/common/include/evtchn_public.h	Wed Nov 28 14:44:10 2007 -0800
     1.2 +++ b/common/include/evtchn_public.h	Wed Nov 28 14:51:17 2007 -0800
     1.3 @@ -20,23 +20,23 @@ Foundation, Inc., 51 Franklin Street, Fi
     1.4  DEFINE_GUID( GUID_XEN_IFACE_EVTCHN, 0xD2D20756, 0xDE69, 0x4447, 0x8A, 0x7D, 0x98, 0x37, 0x19, 0x7D, 0x61, 0x66);
     1.5  //{D2D20756-DE69-4447-8A7D-9837197D6166}
     1.6  
     1.7 -typedef evtchn_port_t
     1.8 -(*PXEN_EVTCHN_ALLOCUNBOUND)(domid_t Domain);
     1.9 -
    1.10  typedef NTSTATUS
    1.11 -(*PXEN_EVTCHN_BIND)(evtchn_port_t Port, PKSERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext);
    1.12 +(*PXEN_EVTCHN_BIND)(PVOID Context, evtchn_port_t Port, PKSERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext);
    1.13  
    1.14  typedef NTSTATUS
    1.15 -(*PXEN_EVTCHN_UNBIND)(evtchn_port_t Port);
    1.16 +(*PXEN_EVTCHN_UNBIND)(PVOID Context, evtchn_port_t Port);
    1.17  
    1.18  typedef NTSTATUS
    1.19 -(*PXEN_EVTCHN_MASK)(evtchn_port_t Port);
    1.20 +(*PXEN_EVTCHN_MASK)(PVOID Context, evtchn_port_t Port);
    1.21  
    1.22  typedef NTSTATUS
    1.23 -(*PXEN_EVTCHN_UNMASK)(evtchn_port_t Port);
    1.24 +(*PXEN_EVTCHN_UNMASK)(PVOID Context, evtchn_port_t Port);
    1.25  
    1.26  typedef NTSTATUS
    1.27 -(*PXEN_EVTCHN_NOTIFY)(evtchn_port_t Port);
    1.28 +(*PXEN_EVTCHN_NOTIFY)(PVOID Context, evtchn_port_t Port);
    1.29 +
    1.30 +typedef evtchn_port_t
    1.31 +(*PXEN_EVTCHN_ALLOCUNBOUND)(PVOID Context, domid_t Domain);
    1.32  
    1.33  typedef struct _XENBUS_IFACE_EVTCHN {
    1.34    INTERFACE InterfaceHeader;
     2.1 --- a/common/include/gnttbl_public.h	Wed Nov 28 14:44:10 2007 -0800
     2.2 +++ b/common/include/gnttbl_public.h	Wed Nov 28 14:51:17 2007 -0800
     2.3 @@ -21,9 +21,9 @@ Foundation, Inc., 51 Franklin Street, Fi
     2.4  DEFINE_GUID(GUID_XEN_IFACE_GNTTBL, 0x6a71acf8, 0xf6d, 0x4022, 0xba, 0x60, 0x19, 0x98, 0x6e, 0xbe, 0xea, 0x73);
     2.5  
     2.6  typedef grant_ref_t
     2.7 -(*PXEN_GNTTBL_GRANTACCESS)(domid_t domid, unsigned long frame, int readonly);
     2.8 +(*PXEN_GNTTBL_GRANTACCESS)(WDFDEVICE Device, domid_t domid, unsigned long frame, int readonly);
     2.9  typedef BOOLEAN
    2.10 -(*PXEN_GNTTBL_ENDACCESS)(grant_ref_t ref);
    2.11 +(*PXEN_GNTTBL_ENDACCESS)(WDFDEVICE Device, grant_ref_t ref);
    2.12  
    2.13  typedef struct _XEN_IFACE_GNTTBL {
    2.14    INTERFACE InterfaceHeader;
     3.1 --- a/common/include/hypercall.h	Wed Nov 28 14:44:10 2007 -0800
     3.2 +++ b/common/include/hypercall.h	Wed Nov 28 14:51:17 2007 -0800
     3.3 @@ -19,11 +19,10 @@ Foundation, Inc., 51 Franklin Street, Fi
     3.4  
     3.5  #include "xenpci.h"
     3.6  
     3.7 -char *hypercall_stubs;
     3.8 -
     3.9  static __inline int
    3.10 -HYPERVISOR_memory_op(int cmd, void *arg)
    3.11 +HYPERVISOR_memory_op(WDFDEVICE Device, int cmd, void *arg)
    3.12  {
    3.13 +  char *hypercall_stubs = GetDeviceData(Device)->hypercall_stubs;
    3.14    long __res;
    3.15    __asm {
    3.16      mov ebx, cmd
    3.17 @@ -37,8 +36,9 @@ HYPERVISOR_memory_op(int cmd, void *arg)
    3.18  }
    3.19  
    3.20  static __inline int
    3.21 -HYPERVISOR_xen_version(int cmd, void *arg)
    3.22 +HYPERVISOR_xen_version(WDFDEVICE Device, int cmd, void *arg)
    3.23  {
    3.24 +  char *hypercall_stubs = GetDeviceData(Device)->hypercall_stubs;
    3.25    long __res;
    3.26    __asm {
    3.27      mov ebx, cmd
    3.28 @@ -52,8 +52,9 @@ HYPERVISOR_xen_version(int cmd, void *ar
    3.29  }
    3.30  
    3.31  static __inline int
    3.32 -HYPERVISOR_grant_table_op(int cmd, void *uop, unsigned int count)
    3.33 +HYPERVISOR_grant_table_op(WDFDEVICE Device, int cmd, void *uop, unsigned int count)
    3.34  {
    3.35 +  char *hypercall_stubs = GetDeviceData(Device)->hypercall_stubs;
    3.36    long __res;
    3.37    __asm {
    3.38      mov ebx, cmd
    3.39 @@ -68,8 +69,9 @@ HYPERVISOR_grant_table_op(int cmd, void 
    3.40  }
    3.41  
    3.42  static __inline int
    3.43 -HYPERVISOR_mmu_update(mmu_update_t *req, int count, int *success_count, domid_t domid)
    3.44 +HYPERVISOR_mmu_update(WDFDEVICE Device, mmu_update_t *req, int count, int *success_count, domid_t domid)
    3.45  {
    3.46 +  char *hypercall_stubs = GetDeviceData(Device)->hypercall_stubs;
    3.47    long __res;
    3.48    long _domid = (long)domid;
    3.49    __asm {
    3.50 @@ -86,8 +88,9 @@ HYPERVISOR_mmu_update(mmu_update_t *req,
    3.51  }
    3.52  
    3.53  static __inline int
    3.54 -HYPERVISOR_console_io(int cmd, int count, char *string)
    3.55 +HYPERVISOR_console_io(WDFDEVICE Device, int cmd, int count, char *string)
    3.56  {
    3.57 +  char *hypercall_stubs = GetDeviceData(Device)->hypercall_stubs;
    3.58    long __res;
    3.59    __asm {
    3.60      mov ebx, cmd
    3.61 @@ -102,8 +105,9 @@ HYPERVISOR_console_io(int cmd, int count
    3.62  }
    3.63  
    3.64  static __inline int
    3.65 -HYPERVISOR_hvm_op(int op, struct xen_hvm_param *arg)
    3.66 +HYPERVISOR_hvm_op(WDFDEVICE Device, int op, struct xen_hvm_param *arg)
    3.67  {
    3.68 +  char *hypercall_stubs = GetDeviceData(Device)->hypercall_stubs;
    3.69    long __res;
    3.70    __asm {
    3.71      mov ebx, op
    3.72 @@ -117,8 +121,9 @@ HYPERVISOR_hvm_op(int op, struct xen_hvm
    3.73  }
    3.74  
    3.75  static __inline int
    3.76 -HYPERVISOR_event_channel_op(int cmd, void *op)
    3.77 +HYPERVISOR_event_channel_op(WDFDEVICE Device, int cmd, void *op)
    3.78  {
    3.79 +  char *hypercall_stubs = GetDeviceData(Device)->hypercall_stubs;
    3.80    long __res;
    3.81    __asm {
    3.82      mov ebx, cmd
    3.83 @@ -132,7 +137,7 @@ HYPERVISOR_event_channel_op(int cmd, voi
    3.84  }
    3.85  
    3.86  static __inline ULONGLONG
    3.87 -hvm_get_parameter(int hvm_param)
    3.88 +hvm_get_parameter(WDFDEVICE Device, int hvm_param)
    3.89  {
    3.90    struct xen_hvm_param a;
    3.91    int retval;
    3.92 @@ -141,7 +146,7 @@ hvm_get_parameter(int hvm_param)
    3.93    a.domid = DOMID_SELF;
    3.94    a.index = hvm_param;
    3.95    //a.value = via;
    3.96 -  retval = HYPERVISOR_hvm_op(HVMOP_get_param, &a);
    3.97 +  retval = HYPERVISOR_hvm_op(Device, HVMOP_get_param, &a);
    3.98    KdPrint((__DRIVER_NAME " hvm_get_parameter retval = %d\n", retval));
    3.99    KdPrint((__DRIVER_NAME " <-- hvm_get_parameter\n"));
   3.100    return a.value;
     4.1 --- a/common/include/xenbus_public.h	Wed Nov 28 14:44:10 2007 -0800
     4.2 +++ b/common/include/xenbus_public.h	Wed Nov 28 14:51:17 2007 -0800
     4.3 @@ -24,21 +24,21 @@ typedef VOID
     4.4  (*PXENBUS_WATCH_CALLBACK)(char *Path, PVOID ServiceContext);
     4.5  
     4.6  typedef char *
     4.7 -(*PXEN_XENBUS_READ)(xenbus_transaction_t xbt, const char *path, char **value);
     4.8 -typedef char *
     4.9 -(*PXEN_XENBUS_WRITE)(xenbus_transaction_t xbt, const char *path, const char *value);
    4.10 -typedef char *
    4.11 -(*PXEN_XENBUS_PRINTF)(xenbus_transaction_t xbt, const char *path, const char *fmt, ...);
    4.12 +(*PXEN_XENBUS_READ)(PVOID Context, xenbus_transaction_t xbt, const char *path, char **value);
    4.13  typedef char *
    4.14 -(*PXEN_XENBUS_STARTTRANSACTION)(xenbus_transaction_t *xbt);
    4.15 -typedef char *
    4.16 -(*PXEN_XENBUS_ENDTRANSACTION)(xenbus_transaction_t t, int abort, int *retry);
    4.17 +(*PXEN_XENBUS_WRITE)(PVOID Context, xenbus_transaction_t xbt, const char *path, const char *value);
    4.18  typedef char *
    4.19 -(*PXEN_XENBUS_LIST)(xenbus_transaction_t xbt, const char *prefix, char ***contents);
    4.20 +(*PXEN_XENBUS_PRINTF)(PVOID Context, xenbus_transaction_t xbt, const char *path, const char *fmt, ...);
    4.21  typedef char *
    4.22 -(*PXEN_XENBUS_ADDWATCH)(xenbus_transaction_t xbt, const char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext);
    4.23 +(*PXEN_XENBUS_STARTTRANSACTION)(PVOID Context, xenbus_transaction_t *xbt);
    4.24  typedef char *
    4.25 -(*PXEN_XENBUS_REMWATCH)(xenbus_transaction_t xbt, const char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext);
    4.26 +(*PXEN_XENBUS_ENDTRANSACTION)(PVOID Context, xenbus_transaction_t t, int abort, int *retry);
    4.27 +typedef char *
    4.28 +(*PXEN_XENBUS_LIST)(PVOID Context, xenbus_transaction_t xbt, const char *prefix, char ***contents);
    4.29 +typedef char *
    4.30 +(*PXEN_XENBUS_ADDWATCH)(PVOID Context, xenbus_transaction_t xbt, const char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext);
    4.31 +typedef char *
    4.32 +(*PXEN_XENBUS_REMWATCH)(PVOID Context, xenbus_transaction_t xbt, const char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext);
    4.33  
    4.34  typedef struct _XEN_IFACE_XENBUS {
    4.35    INTERFACE InterfaceHeader;
     5.1 --- a/xennet/xennet.c	Wed Nov 28 14:44:10 2007 -0800
     5.2 +++ b/xennet/xennet.c	Wed Nov 28 14:51:17 2007 -0800
     5.3 @@ -220,6 +220,7 @@ XenNet_Init(
     5.4      goto err;
     5.5    }
     5.6  
     5.7 +#if 0
     5.8    status = WdfFdoQueryForInterface(xi->wdf_device, &GUID_XEN_IFACE_XEN,
     5.9      (PINTERFACE) &xi->XenInterface, sizeof(XEN_IFACE_XEN), 1, NULL);
    5.10    if(!NT_SUCCESS(status))
    5.11 @@ -228,6 +229,7 @@ XenNet_Init(
    5.12      status = NDIS_STATUS_FAILURE;
    5.13      goto err;
    5.14    }
    5.15 +#endif
    5.16  
    5.17    status = WdfFdoQueryForInterface(xi->wdf_device, &GUID_XEN_IFACE_GNTTBL,
    5.18      (PINTERFACE) &xi->GntTblInterface, sizeof(XEN_IFACE_GNTTBL), 1, NULL);
    5.19 @@ -238,8 +240,10 @@ XenNet_Init(
    5.20      goto err;
    5.21    }
    5.22  
    5.23 -  xi->event_channel = xi->EvtChnInterface.AllocUnbound(0);  
    5.24 -  xi->EvtChnInterface.Bind(xi->event_channel, XenNet_Interrupt, xi);
    5.25 +  xi->event_channel = xi->EvtChnInterface.AllocUnbound(
    5.26 +    xi->EvtChnInterface.InterfaceHeader.Context, 0);  
    5.27 +  xi->EvtChnInterface.Bind(xi->EvtChnInterface.InterfaceHeader.Context,
    5.28 +    xi->event_channel, XenNet_Interrupt, xi);
    5.29  
    5.30    /* TODO: must free pages in MDL as well as MDL using MmFreePagesFromMdl and ExFreePool */
    5.31    // or, allocate mem and then get mdl, then free mdl
    5.32 @@ -248,21 +252,24 @@ XenNet_Init(
    5.33    xi->txs = MmMapLockedPages(mdl, KernelMode);
    5.34    SHARED_RING_INIT(xi->txs);
    5.35    FRONT_RING_INIT(&xi->tx, xi->txs, PAGE_SIZE);
    5.36 -  xi->tx_ring_ref = xi->GntTblInterface.GrantAccess(0, pfn, FALSE);
    5.37 +  xi->tx_ring_ref = xi->GntTblInterface.GrantAccess(
    5.38 +    xi->GntTblInterface.InterfaceHeader.Context, 0, pfn, FALSE);
    5.39  
    5.40    mdl = AllocatePage();
    5.41    pfn = *MmGetMdlPfnArray(mdl);
    5.42    xi->rxs = MmMapLockedPages(mdl, KernelMode);
    5.43    SHARED_RING_INIT(xi->rxs);
    5.44    FRONT_RING_INIT(&xi->rx, xi->rxs, PAGE_SIZE);
    5.45 -  xi->rx_ring_ref = xi->GntTblInterface.GrantAccess(0, pfn, FALSE);
    5.46 +  xi->rx_ring_ref = xi->GntTblInterface.GrantAccess(
    5.47 +    xi->GntTblInterface.InterfaceHeader.Context, 0, pfn, FALSE);
    5.48  
    5.49    {
    5.50    char *msg;
    5.51    char **vif_devs;
    5.52    char buffer[128];
    5.53    // get mac addr
    5.54 -  msg = xi->XenBusInterface.List(XBT_NIL, "device/vif", &vif_devs);
    5.55 +  msg = xi->XenBusInterface.List(xi->EvtChnInterface.InterfaceHeader.Context,
    5.56 +    XBT_NIL, "device/vif", &vif_devs);
    5.57    if (!msg)
    5.58    {
    5.59      for (i = 0; vif_devs[i]; i++)
    5.60 @@ -448,7 +455,7 @@ XenNet_SetInformation(
    5.61    OUT PULONG BytesNeeded
    5.62    )
    5.63  {
    5.64 -  KdPrint((__FUNCTION__ " called\n"));
    5.65 +  KdPrint((__FUNCTION__ " called with OID=0x%x\n", Oid));
    5.66    return NDIS_STATUS_SUCCESS;
    5.67  }
    5.68  
     6.1 --- a/xenpci/evtchn.c	Wed Nov 28 14:44:10 2007 -0800
     6.2 +++ b/xenpci/evtchn.c	Wed Nov 28 14:51:17 2007 -0800
     6.3 @@ -20,28 +20,17 @@ Foundation, Inc., 51 Franklin Street, Fi
     6.4  #include "xenpci.h"
     6.5  #include "hypercall.h"
     6.6  
     6.7 -#define NR_EVENTS 1024
     6.8 -
     6.9 -typedef struct _ev_action_t {
    6.10 -  PKSERVICE_ROUTINE ServiceRoutine;
    6.11 -  PVOID ServiceContext;
    6.12 -  ULONG Count;
    6.13 -} ev_action_t;
    6.14 -
    6.15 -static ev_action_t ev_actions[NR_EVENTS];
    6.16 -
    6.17 -static unsigned long bound_ports[NR_EVENTS/(8*sizeof(unsigned long))];
    6.18 -
    6.19  BOOLEAN
    6.20  EvtChn_Interrupt(WDFINTERRUPT Interrupt, ULONG MessageID)
    6.21  {
    6.22    int cpu = 0;
    6.23    vcpu_info_t *vcpu_info;
    6.24 -//  int i;
    6.25    unsigned long evt_words, evt_word;
    6.26    unsigned long evt_bit;
    6.27    unsigned long port;
    6.28    ev_action_t *ev_action;
    6.29 +  PXENPCI_DEVICE_DATA deviceData = GetDeviceData(WdfInterruptGetDevice(Interrupt));
    6.30 +  shared_info_t *shared_info_area = deviceData->shared_info_area;
    6.31  
    6.32    UNREFERENCED_PARAMETER(Interrupt);
    6.33    UNREFERENCED_PARAMETER(MessageID);
    6.34 @@ -61,7 +50,7 @@ EvtChn_Interrupt(WDFINTERRUPT Interrupt,
    6.35      while (_BitScanForward(&evt_bit, shared_info_area->evtchn_pending[evt_word] & ~shared_info_area->evtchn_mask[evt_word]))
    6.36      {
    6.37        port = (evt_word << 5) + evt_bit;
    6.38 -      ev_action = &ev_actions[port];
    6.39 +      ev_action = &deviceData->ev_actions[port];
    6.40        if (ev_action->ServiceRoutine == NULL)
    6.41        {
    6.42          KdPrint((__DRIVER_NAME "     Unhandled Event!!!\n"));
    6.43 @@ -82,38 +71,24 @@ EvtChn_Interrupt(WDFINTERRUPT Interrupt,
    6.44    return TRUE;
    6.45  }
    6.46  
    6.47 -evtchn_port_t
    6.48 -EvtChn_AllocUnbound(domid_t Domain)
    6.49 +NTSTATUS
    6.50 +EvtChn_Bind(PVOID Context, evtchn_port_t Port, PKSERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext)
    6.51  {
    6.52 -  evtchn_alloc_unbound_t op;
    6.53 -
    6.54 -  //KdPrint((__DRIVER_NAME " --> AllocUnbound\n"));
    6.55 +  WDFDEVICE Device = Context;
    6.56 +  PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);
    6.57  
    6.58 -  op.dom = DOMID_SELF;
    6.59 -  op.remote_dom = Domain;
    6.60 -  HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, &op);
    6.61 -
    6.62 -  //KdPrint((__DRIVER_NAME " <-- AllocUnbound\n"));
    6.63 -
    6.64 -  return op.port;
    6.65 -}
    6.66 -
    6.67 -
    6.68 -NTSTATUS
    6.69 -EvtChn_Bind(evtchn_port_t Port, PKSERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext)
    6.70 -{
    6.71    KdPrint((__DRIVER_NAME " --> EvtChn_Bind\n"));
    6.72  
    6.73 -  if(ev_actions[Port].ServiceRoutine != NULL)
    6.74 +  if(deviceData->ev_actions[Port].ServiceRoutine != NULL)
    6.75    {
    6.76      KdPrint((__DRIVER_NAME " Handler for port %d already registered, replacing\n", Port));
    6.77    }
    6.78  
    6.79 -  ev_actions[Port].ServiceContext = ServiceContext;
    6.80 +  deviceData->ev_actions[Port].ServiceContext = ServiceContext;
    6.81    KeMemoryBarrier();
    6.82 -  ev_actions[Port].ServiceRoutine = ServiceRoutine;
    6.83 +  deviceData->ev_actions[Port].ServiceRoutine = ServiceRoutine;
    6.84  
    6.85 -  EvtChn_Unmask(Port);
    6.86 +  EvtChn_Unmask(Device, Port);
    6.87  
    6.88    KdPrint((__DRIVER_NAME " <-- EvtChn_Bind\n"));
    6.89  
    6.90 @@ -121,13 +96,14 @@ EvtChn_Bind(evtchn_port_t Port, PKSERVIC
    6.91  }
    6.92  
    6.93  NTSTATUS
    6.94 -EvtChn_Unbind(evtchn_port_t Port)
    6.95 +EvtChn_Unbind(PVOID Context, evtchn_port_t Port)
    6.96  {
    6.97 -  //KdPrint((__DRIVER_NAME " --> EvtChn_UnBind\n"));
    6.98 +  WDFDEVICE Device = Context;
    6.99 +  PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);
   6.100  
   6.101 -  EvtChn_Mask(Port);
   6.102 -  ev_actions[Port].ServiceContext = NULL;
   6.103 -  ev_actions[Port].ServiceRoutine = NULL;
   6.104 +  EvtChn_Mask(Context, Port);
   6.105 +  deviceData->ev_actions[Port].ServiceContext = NULL;
   6.106 +  deviceData->ev_actions[Port].ServiceRoutine = NULL;
   6.107  
   6.108    //KdPrint((__DRIVER_NAME " <-- EvtChn_UnBind\n"));
   6.109  
   6.110 @@ -135,11 +111,14 @@ EvtChn_Unbind(evtchn_port_t Port)
   6.111  }
   6.112  
   6.113  NTSTATUS
   6.114 -EvtChn_Mask(evtchn_port_t Port)
   6.115 +EvtChn_Mask(PVOID Context, evtchn_port_t Port)
   6.116  {
   6.117 +  WDFDEVICE Device = Context;
   6.118 +  PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);
   6.119    //KdPrint((__DRIVER_NAME " --> EvtChn_Mask\n"));
   6.120  
   6.121 -  _interlockedbittestandset((volatile LONG *)&shared_info_area->evtchn_mask[0], Port);
   6.122 +  _interlockedbittestandset(
   6.123 +    (volatile LONG *)&deviceData->shared_info_area->evtchn_mask[0], Port);
   6.124  
   6.125    //KdPrint((__DRIVER_NAME " <-- EvtChn_Mask\n"));
   6.126  
   6.127 @@ -147,11 +126,14 @@ EvtChn_Mask(evtchn_port_t Port)
   6.128  }
   6.129  
   6.130  NTSTATUS
   6.131 -EvtChn_Unmask(evtchn_port_t Port)
   6.132 +EvtChn_Unmask(PVOID Context, evtchn_port_t Port)
   6.133  {
   6.134 +  WDFDEVICE Device = Context;
   6.135 +  PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);
   6.136    //KdPrint((__DRIVER_NAME " --> EvtChn_Unmask\n"));
   6.137  
   6.138 -  _interlockedbittestandreset((volatile LONG *)&shared_info_area->evtchn_mask[0], Port);
   6.139 +  _interlockedbittestandreset(
   6.140 +    (volatile LONG *)&deviceData->shared_info_area->evtchn_mask[0], Port);
   6.141    // should we kick off pending interrupts here too???
   6.142  
   6.143    //KdPrint((__DRIVER_NAME " <-- EvtChn_Unmask\n"));
   6.144 @@ -160,7 +142,7 @@ EvtChn_Unmask(evtchn_port_t Port)
   6.145  }
   6.146  
   6.147  NTSTATUS
   6.148 -EvtChn_Notify(evtchn_port_t Port)
   6.149 +EvtChn_Notify(PVOID Context, evtchn_port_t Port)
   6.150  {
   6.151    struct evtchn_send send;
   6.152  
   6.153 @@ -168,22 +150,39 @@ EvtChn_Notify(evtchn_port_t Port)
   6.154  
   6.155    send.port = Port;
   6.156  
   6.157 -  (void)HYPERVISOR_event_channel_op(EVTCHNOP_send, &send);
   6.158 +  (void)HYPERVISOR_event_channel_op(Context, EVTCHNOP_send, &send);
   6.159  
   6.160    //KdPrint((__DRIVER_NAME " <-- EvtChn_Notify\n"));
   6.161  
   6.162    return STATUS_SUCCESS;
   6.163  }
   6.164  
   6.165 +evtchn_port_t
   6.166 +EvtChn_AllocUnbound(PVOID Context, domid_t Domain)
   6.167 +{
   6.168 +  evtchn_alloc_unbound_t op;
   6.169 +
   6.170 +  UNREFERENCED_PARAMETER(Context);
   6.171 +
   6.172 +  //KdPrint((__DRIVER_NAME " --> AllocUnbound\n"));
   6.173 +
   6.174 +  op.dom = DOMID_SELF;
   6.175 +  op.remote_dom = Domain;
   6.176 +  HYPERVISOR_event_channel_op(Context, EVTCHNOP_alloc_unbound, &op);
   6.177 +
   6.178 +  //KdPrint((__DRIVER_NAME " <-- AllocUnbound\n"));
   6.179 +
   6.180 +  return op.port;
   6.181 +}
   6.182  
   6.183  evtchn_port_t
   6.184 -EvtChn_GetXenStorePort()
   6.185 +EvtChn_GetXenStorePort(WDFDEVICE Device)
   6.186  {
   6.187    evtchn_port_t Port;  
   6.188  
   6.189    //KdPrint((__DRIVER_NAME " --> EvtChn_GetStorePort\n"));
   6.190  
   6.191 -  Port = (evtchn_port_t)hvm_get_parameter(HVM_PARAM_STORE_EVTCHN);
   6.192 +  Port = (evtchn_port_t)hvm_get_parameter(Device, HVM_PARAM_STORE_EVTCHN);
   6.193  
   6.194    //KdPrint((__DRIVER_NAME " <-- EvtChn_GetStorePort\n"));
   6.195  
   6.196 @@ -191,7 +190,7 @@ EvtChn_GetXenStorePort()
   6.197  }
   6.198  
   6.199  PVOID
   6.200 -EvtChn_GetXenStoreRingAddr()
   6.201 +EvtChn_GetXenStoreRingAddr(WDFDEVICE Device)
   6.202  {
   6.203    PHYSICAL_ADDRESS pa_xen_store_interface;
   6.204    PVOID xen_store_interface;
   6.205 @@ -200,7 +199,7 @@ EvtChn_GetXenStoreRingAddr()
   6.206  
   6.207    //KdPrint((__DRIVER_NAME " --> EvtChn_GetRingAddr\n"));
   6.208  
   6.209 -  xen_store_mfn = (ULONG)hvm_get_parameter(HVM_PARAM_STORE_PFN);
   6.210 +  xen_store_mfn = (ULONG)hvm_get_parameter(Device, HVM_PARAM_STORE_PFN);
   6.211  
   6.212    pa_xen_store_interface.QuadPart = xen_store_mfn << PAGE_SHIFT;
   6.213    xen_store_interface = MmMapIoSpace(pa_xen_store_interface, PAGE_SIZE, MmNonCached);
   6.214 @@ -217,25 +216,25 @@ EvtChn_GetXenStoreRingAddr()
   6.215  }
   6.216  
   6.217  NTSTATUS
   6.218 -EvtChn_Init()
   6.219 +EvtChn_Init(WDFDEVICE Device)
   6.220  {
   6.221 +  PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);
   6.222    int i;
   6.223  
   6.224    for (i = 0; i < NR_EVENTS; i++)
   6.225    {
   6.226 -    EvtChn_Mask(i);
   6.227 -    ev_actions[i].ServiceRoutine = NULL;
   6.228 -    ev_actions[i].ServiceContext = NULL;
   6.229 -    ev_actions[i].Count = 0;
   6.230 +    EvtChn_Mask(Device, i);
   6.231 +    deviceData->ev_actions[i].ServiceRoutine = NULL;
   6.232 +    deviceData->ev_actions[i].ServiceContext = NULL;
   6.233 +    deviceData->ev_actions[i].Count = 0;
   6.234    }
   6.235  
   6.236 -  for (i = 0; i < 8; i++) {
   6.237 -    shared_info_area->evtchn_pending[i] = 0;
   6.238 +  for (i = 0; i < 8; i++)
   6.239 +  {
   6.240 +    deviceData->shared_info_area->evtchn_pending[i] = 0;
   6.241    }
   6.242 -  shared_info_area->vcpu_info[0].evtchn_upcall_pending = 0;
   6.243 -  shared_info_area->vcpu_info[0].evtchn_pending_sel = 0;
   6.244 +  deviceData->shared_info_area->vcpu_info[0].evtchn_upcall_pending = 0;
   6.245 +  deviceData->shared_info_area->vcpu_info[0].evtchn_pending_sel = 0;
   6.246  
   6.247    return STATUS_SUCCESS;
   6.248  }
   6.249 -
   6.250 -static ev_action_t ev_actions[NR_EVENTS];
     7.1 --- a/xenpci/gnttbl.c	Wed Nov 28 14:44:10 2007 -0800
     7.2 +++ b/xenpci/gnttbl.c	Wed Nov 28 14:51:17 2007 -0800
     7.3 @@ -20,24 +20,23 @@ Foundation, Inc., 51 Franklin Street, Fi
     7.4  #include "xenpci.h"
     7.5  #include <hypercall.h>
     7.6  
     7.7 -static grant_entry_t *gnttab_table;
     7.8 -static PHYSICAL_ADDRESS gnttab_table_physical;
     7.9 -static grant_ref_t gnttab_list[NR_GRANT_ENTRIES];
    7.10 -
    7.11  static void
    7.12 -put_free_entry(grant_ref_t ref)
    7.13 +put_free_entry(WDFDEVICE Device, grant_ref_t ref)
    7.14  {
    7.15 -    gnttab_list[ref] = gnttab_list[0];
    7.16 -    gnttab_list[0]  = ref;
    7.17 +  PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);
    7.18  
    7.19 +  deviceData->gnttab_list[ref] = deviceData->gnttab_list[0];
    7.20 +  deviceData->gnttab_list[0]  = ref;
    7.21  }
    7.22  
    7.23  static grant_ref_t
    7.24 -get_free_entry()
    7.25 +get_free_entry(WDFDEVICE Device)
    7.26  {
    7.27 -    unsigned int ref = gnttab_list[0];
    7.28 -    gnttab_list[0] = gnttab_list[ref];
    7.29 -    return ref;
    7.30 +  PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);
    7.31 +  unsigned int ref = deviceData->gnttab_list[0];
    7.32 +
    7.33 +  deviceData->gnttab_list[0] = deviceData->gnttab_list[ref];
    7.34 +  return ref;
    7.35  }
    7.36  
    7.37  /*
    7.38 @@ -50,8 +49,9 @@ typedef struct grant_entry grant_entry_t
    7.39  */
    7.40  
    7.41  static int 
    7.42 -GntTab_Map(unsigned int start_idx, unsigned int end_idx)
    7.43 +GntTab_Map(WDFDEVICE Device, unsigned int start_idx, unsigned int end_idx)
    7.44  {
    7.45 +  PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);
    7.46    struct xen_add_to_physmap xatp;
    7.47    unsigned int i = end_idx;
    7.48  
    7.49 @@ -63,8 +63,8 @@ GntTab_Map(unsigned int start_idx, unsig
    7.50      xatp.domid = DOMID_SELF;
    7.51      xatp.idx = i;
    7.52      xatp.space = XENMAPSPACE_grant_table;
    7.53 -    xatp.gpfn = (xen_pfn_t)(gnttab_table_physical.QuadPart >> PAGE_SHIFT) + i;
    7.54 -    if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
    7.55 +    xatp.gpfn = (xen_pfn_t)(deviceData->gnttab_table_physical.QuadPart >> PAGE_SHIFT) + i;
    7.56 +    if (HYPERVISOR_memory_op(Device, XENMEM_add_to_physmap, &xatp))
    7.57      {
    7.58        KdPrint((__DRIVER_NAME "     ***ERROR MAPPING FRAME***\n"));
    7.59      }
    7.60 @@ -74,41 +74,50 @@ GntTab_Map(unsigned int start_idx, unsig
    7.61  }
    7.62  
    7.63  VOID
    7.64 -GntTbl_Init()
    7.65 +GntTbl_Init(WDFDEVICE Device)
    7.66  {
    7.67 +  PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);
    7.68    int i;
    7.69  
    7.70    //KdPrint((__DRIVER_NAME " --> GntTbl_Init\n"));
    7.71  
    7.72    for (i = NR_RESERVED_ENTRIES; i < NR_GRANT_ENTRIES; i++)
    7.73 -    put_free_entry(i);
    7.74 +    put_free_entry(Device, i);
    7.75  
    7.76 -  gnttab_table_physical = XenPCI_AllocMMIO(PAGE_SIZE * NR_GRANT_FRAMES);
    7.77 -  gnttab_table = MmMapIoSpace(gnttab_table_physical, PAGE_SIZE * NR_GRANT_FRAMES, MmNonCached);
    7.78 -  if (gnttab_table == NULL)
    7.79 +  deviceData->gnttab_table_physical = XenPCI_AllocMMIO(Device,
    7.80 +    PAGE_SIZE * NR_GRANT_FRAMES);
    7.81 +  deviceData->gnttab_table = MmMapIoSpace(deviceData->gnttab_table_physical,
    7.82 +    PAGE_SIZE * NR_GRANT_FRAMES, MmNonCached);
    7.83 +  if (!deviceData->gnttab_table)
    7.84    {
    7.85      KdPrint((__DRIVER_NAME "     Error Mapping Grant Table Shared Memory\n"));
    7.86      return;
    7.87    }
    7.88 -  GntTab_Map(0, NR_GRANT_FRAMES - 1);
    7.89 +  GntTab_Map(Device, 0, NR_GRANT_FRAMES - 1);
    7.90  
    7.91    //KdPrint((__DRIVER_NAME " <-- GntTbl_Init table mapped at %p\n", gnttab_table));
    7.92  }
    7.93  
    7.94  grant_ref_t
    7.95 -GntTbl_GrantAccess(domid_t domid, unsigned long frame, int readonly)
    7.96 +GntTbl_GrantAccess(
    7.97 +  WDFDEVICE Device,
    7.98 +  domid_t domid,
    7.99 +  unsigned long frame,
   7.100 +  int readonly)
   7.101  {
   7.102 +  PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);
   7.103    grant_ref_t ref;
   7.104  
   7.105    //KdPrint((__DRIVER_NAME " --> GntTbl_GrantAccess\n"));
   7.106  
   7.107 -  ref = get_free_entry();
   7.108 -  gnttab_table[ref].frame = frame;
   7.109 -  gnttab_table[ref].domid = domid;
   7.110 +  /* TODO: locking? */
   7.111 +  ref = get_free_entry(Device);
   7.112 +  deviceData->gnttab_table[ref].frame = frame;
   7.113 +  deviceData->gnttab_table[ref].domid = domid;
   7.114    //_WriteBarrier();
   7.115    KeMemoryBarrier();
   7.116    readonly *= GTF_readonly;
   7.117 -  gnttab_table[ref].flags = GTF_permit_access | (uint16_t)readonly;
   7.118 +  deviceData->gnttab_table[ref].flags = GTF_permit_access | (uint16_t)readonly;
   7.119  
   7.120    //KdPrint((__DRIVER_NAME " <-- GntTbl_GrantAccess (ref = %d)\n", ref));
   7.121  
   7.122 @@ -116,22 +125,26 @@ GntTbl_GrantAccess(domid_t domid, unsign
   7.123  }
   7.124  
   7.125  BOOLEAN
   7.126 -GntTbl_EndAccess(grant_ref_t ref)
   7.127 +GntTbl_EndAccess(
   7.128 +  WDFDEVICE Device,
   7.129 +  grant_ref_t ref)
   7.130  {
   7.131 +  PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);
   7.132    unsigned short flags, nflags;
   7.133  
   7.134    //KdPrint((__DRIVER_NAME " --> GntTbl_EndAccess\n"));
   7.135  
   7.136 -  nflags = gnttab_table[ref].flags;
   7.137 +  nflags = deviceData->gnttab_table[ref].flags;
   7.138    do {
   7.139      if ((flags = nflags) & (GTF_reading|GTF_writing))
   7.140      {
   7.141        KdPrint((__DRIVER_NAME "WARNING: g.e. still in use!\n"));
   7.142        return FALSE;
   7.143      }
   7.144 -  } while ((nflags = InterlockedCompareExchange16((volatile SHORT *)&gnttab_table[ref].flags, flags, 0)) != flags);
   7.145 +  } while ((nflags = InterlockedCompareExchange16(
   7.146 +    (volatile SHORT *)&deviceData->gnttab_table[ref].flags, flags, 0)) != flags);
   7.147  
   7.148 -  put_free_entry(ref);
   7.149 +  put_free_entry(Device, ref);
   7.150    //KdPrint((__DRIVER_NAME " <-- GntTbl_EndAccess\n"));
   7.151    return TRUE;
   7.152  }
     8.1 --- a/xenpci/xenbus.c	Wed Nov 28 14:44:10 2007 -0800
     8.2 +++ b/xenpci/xenbus.c	Wed Nov 28 14:51:17 2007 -0800
     8.3 @@ -31,7 +31,7 @@ struct xenbus_req_info
     8.4    void *Reply;
     8.5  };
     8.6  
     8.7 -typedef struct {
     8.8 +typedef struct _XENBUS_WATCH_ENTRY {
     8.9    char Path[128];
    8.10    PXENBUS_WATCH_CALLBACK ServiceRoutine;
    8.11    PVOID ServiceContext;
    8.12 @@ -39,7 +39,8 @@ typedef struct {
    8.13    int Active;
    8.14  } XENBUS_WATCH_ENTRY, *PXENBUS_WATCH_ENTRY;
    8.15  
    8.16 -typedef struct {
    8.17 +typedef struct _XENBUS_WATCH_RING
    8.18 +{
    8.19    char Path[128];
    8.20    char Token[10];
    8.21  } XENBUS_WATCH_RING;
    8.22 @@ -130,7 +131,6 @@ static void release_xenbus_id(int id)
    8.23  //    spin_unlock(&req_lock);
    8.24  }
    8.25  
    8.26 -
    8.27  static char *errmsg(struct xsd_sockmsg *rep)
    8.28  {
    8.29    char *res;
    8.30 @@ -176,11 +176,15 @@ struct write_req {
    8.31      unsigned len;
    8.32  };
    8.33  
    8.34 -static evtchn_port_t xen_store_evtchn;
    8.35 -
    8.36 -static void xb_write(int type, int req_id, xenbus_transaction_t trans_id,
    8.37 -                     const struct write_req *req, int nr_reqs)
    8.38 +static void xb_write (
    8.39 +  WDFDEVICE Device,
    8.40 +  int type,
    8.41 +  int req_id,
    8.42 +  xenbus_transaction_t trans_id,
    8.43 +  const struct write_req *req,
    8.44 +  int nr_reqs)
    8.45  {
    8.46 +  PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);
    8.47    XENSTORE_RING_IDX prod;
    8.48    int r;
    8.49    size_t len = 0;
    8.50 @@ -254,13 +258,18 @@ static void xb_write(int type, int req_i
    8.51    //KdPrint((__DRIVER_NAME " prod = %08x\n", xen_store_interface->req_prod));
    8.52  
    8.53    /* Send evtchn to notify remote */
    8.54 -  EvtChn_Notify(xen_store_evtchn);
    8.55 +  EvtChn_Notify(Device, deviceData->xen_store_evtchn);
    8.56  
    8.57    //KdPrint((__DRIVER_NAME " <-- xb_write\n"));
    8.58  }
    8.59  
    8.60  static struct xsd_sockmsg *
    8.61 -xenbus_msg_reply(int type, xenbus_transaction_t trans, struct write_req *io, int nr_reqs)
    8.62 +xenbus_msg_reply(
    8.63 +  WDFDEVICE Device,
    8.64 +  int type,
    8.65 +  xenbus_transaction_t trans,
    8.66 +  struct write_req *io,
    8.67 +  int nr_reqs)
    8.68  {
    8.69    int id;
    8.70  //  DEFINE_WAIT(w);
    8.71 @@ -271,7 +280,7 @@ xenbus_msg_reply(int type, xenbus_transa
    8.72    id = allocate_xenbus_id();
    8.73  //  add_waiter(w, req_info[id].waitq);
    8.74  
    8.75 -  xb_write(type, id, trans, io, nr_reqs);
    8.76 +  xb_write(Device, type, id, trans, io, nr_reqs);
    8.77  //
    8.78  //  schedule();
    8.79  //  remove_waiter(w);
    8.80 @@ -291,43 +300,53 @@ xenbus_msg_reply(int type, xenbus_transa
    8.81  }
    8.82  
    8.83  char *
    8.84 -XenBus_Read(xenbus_transaction_t xbt, const char *path, char **value)
    8.85 +XenBus_Read (
    8.86 +  PVOID Context,
    8.87 +  xenbus_transaction_t xbt,
    8.88 +  const char *path,
    8.89 +  char **value)
    8.90  {
    8.91 -    struct write_req req[] = { {path, strlen(path) + 1} };
    8.92 -    struct xsd_sockmsg *rep;
    8.93 -    char *res;
    8.94 -    char *msg;
    8.95 +  WDFDEVICE Device = Context;
    8.96 +  struct write_req req[] = { {path, strlen(path) + 1} };
    8.97 +  struct xsd_sockmsg *rep;
    8.98 +  char *res;
    8.99 +  char *msg;
   8.100  
   8.101 -    rep = xenbus_msg_reply(XS_READ, xbt, req, ARRAY_SIZE(req));
   8.102 -    msg = errmsg(rep);
   8.103 -    if (msg) {
   8.104 -      *value = NULL;
   8.105 -      return msg;
   8.106 -    }
   8.107 -    res = ExAllocatePoolWithTag(NonPagedPool, rep->len + 1, XENPCI_POOL_TAG);
   8.108 -    memcpy(res, rep + 1, rep->len);
   8.109 -    res[rep->len] = 0;
   8.110 -    ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
   8.111 -    *value = res;
   8.112 -    return NULL;
   8.113 +  rep = xenbus_msg_reply(Device, XS_READ, xbt, req, ARRAY_SIZE(req));
   8.114 +  msg = errmsg(rep);
   8.115 +  if (msg) {
   8.116 +    *value = NULL;
   8.117 +    return msg;
   8.118 +  }
   8.119 +  res = ExAllocatePoolWithTag(NonPagedPool, rep->len + 1, XENPCI_POOL_TAG);
   8.120 +  memcpy(res, rep + 1, rep->len);
   8.121 +  res[rep->len] = 0;
   8.122 +  ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
   8.123 +  *value = res;
   8.124 +  return NULL;
   8.125  }
   8.126  
   8.127  char *
   8.128 -XenBus_Write(xenbus_transaction_t xbt, const char *path, const char *value)
   8.129 +XenBus_Write(
   8.130 +  PVOID Context,
   8.131 +  xenbus_transaction_t xbt,
   8.132 +  const char *path,
   8.133 +  const char *value)
   8.134  {
   8.135 -    struct write_req req[] = {
   8.136 -        {path, strlen(path) + 1},
   8.137 -        {value, strlen(value) + 1},
   8.138 -    };
   8.139 -    struct xsd_sockmsg *rep;
   8.140 -    char *msg;
   8.141 +  WDFDEVICE Device = Context;
   8.142 +  struct write_req req[] = {
   8.143 +    {path, strlen(path) + 1},
   8.144 +    {value, strlen(value) + 1},
   8.145 +  };
   8.146 +  struct xsd_sockmsg *rep;
   8.147 +  char *msg;
   8.148  
   8.149 -    rep = xenbus_msg_reply(XS_WRITE, xbt, req, ARRAY_SIZE(req));
   8.150 -    msg = errmsg(rep);
   8.151 -    if (msg)
   8.152 -      return msg;
   8.153 -    ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
   8.154 -    return NULL;
   8.155 +  rep = xenbus_msg_reply(Device, XS_WRITE, xbt, req, ARRAY_SIZE(req));
   8.156 +  msg = errmsg(rep);
   8.157 +  if (msg)
   8.158 +    return msg;
   8.159 +  ExFreePoolWithTag(rep, XENPCI_POOL_TAG);
   8.160 +  return NULL;
   8.161  }
   8.162  
   8.163  char* xenbus_wait_for_value(const char* path,const char* value)
   8.164 @@ -354,12 +373,13 @@ char* xenbus_wait_for_value(const char* 
   8.165  }
   8.166  
   8.167  NTSTATUS
   8.168 -XenBus_Init()
   8.169 +XenBus_Init(WDFDEVICE Device)
   8.170  {
   8.171 +  PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);  
   8.172    //KdPrint((__DRIVER_NAME " --> XenBus_Init\n"));
   8.173  
   8.174 -  xen_store_evtchn = EvtChn_GetXenStorePort();
   8.175 -  xen_store_interface = EvtChn_GetXenStoreRingAddr();
   8.176 +  deviceData->xen_store_evtchn = EvtChn_GetXenStorePort(Device);
   8.177 +  xen_store_interface = EvtChn_GetXenStoreRingAddr(Device);
   8.178  
   8.179    //KdPrint((__DRIVER_NAME "     xen_store_evtchn = %08x\n", xen_store_evtchn));
   8.180    //KdPrint((__DRIVER_NAME "     xen_store_interface = %08x\n", xen_store_interface));
   8.181 @@ -373,8 +393,9 @@ XenBus_Init()
   8.182  }
   8.183  
   8.184  NTSTATUS
   8.185 -XenBus_Start()
   8.186 +XenBus_Start(WDFDEVICE Device)
   8.187  {
   8.188 +  PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);
   8.189    OBJECT_ATTRIBUTES oa;
   8.190    NTSTATUS status;
   8.191    int i;
   8.192 @@ -392,7 +413,7 @@ XenBus_Start()
   8.193    InitializeObjectAttributes(&oa, NULL, OBJ_KERNEL_HANDLE, NULL, NULL);
   8.194    status = PsCreateSystemThread(&XenBus_WatchThreadHandle, THREAD_ALL_ACCESS, &oa, NULL, NULL, XenBus_WatchThreadProc, NULL);
   8.195  
   8.196 -  EvtChn_Bind(xen_store_evtchn, XenBus_Interrupt, NULL);
   8.197 +  EvtChn_Bind(Device, deviceData->xen_store_evtchn, XenBus_Interrupt, NULL);
   8.198  
   8.199    KdPrint((__DRIVER_NAME " <-- XenBus_Start\n"));
   8.200  
   8.201 @@ -400,18 +421,19 @@ XenBus_Start()
   8.202  }
   8.203  
   8.204  NTSTATUS
   8.205 -XenBus_Stop()
   8.206 +XenBus_Stop(WDFDEVICE Device)
   8.207  {
   8.208 +  PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);
   8.209    int i;
   8.210  
   8.211    for (i = 0; i < MAX_WATCH_ENTRIES; i++)
   8.212    {
   8.213      if (!XenBus_WatchEntries[i].Active)
   8.214        continue;
   8.215 -    XenBus_RemWatch(XBT_NIL, XenBus_WatchEntries[i].Path, XenBus_WatchEntries[i].ServiceRoutine, XenBus_WatchEntries[i].ServiceContext);
   8.216 +    XenBus_RemWatch(Device, XBT_NIL, XenBus_WatchEntries[i].Path, XenBus_WatchEntries[i].ServiceRoutine, XenBus_WatchEntries[i].ServiceContext);
   8.217    }
   8.218  
   8.219 -  EvtChn_Unbind(xen_store_evtchn);
   8.220 +  EvtChn_Unbind(Device, deviceData->xen_store_evtchn);
   8.221  
   8.222    // Does this actually stop the threads???
   8.223    ZwClose(XenBus_WatchThreadHandle);
   8.224 @@ -421,8 +443,13 @@ XenBus_Stop()
   8.225  }
   8.226  
   8.227  char *
   8.228 -XenBus_List(xenbus_transaction_t xbt, const char *pre, char ***contents)
   8.229 +XenBus_List(
   8.230 +  PVOID Context,
   8.231 +  xenbus_transaction_t xbt,
   8.232 +  const char *pre,
   8.233 +  char ***contents)
   8.234  {
   8.235 +  WDFDEVICE Device = Context;
   8.236    struct xsd_sockmsg *reply, *repmsg;
   8.237    struct write_req req[] = { { pre, strlen(pre)+1 } };
   8.238    ULONG nr_elems, x, i;
   8.239 @@ -431,7 +458,7 @@ XenBus_List(xenbus_transaction_t xbt, co
   8.240  
   8.241    //KdPrint((__DRIVER_NAME " --> xenbus_ls\n"));
   8.242  
   8.243 -  repmsg = xenbus_msg_reply(XS_DIRECTORY, xbt, req, ARRAY_SIZE(req));
   8.244 +  repmsg = xenbus_msg_reply(Device, XS_DIRECTORY, xbt, req, ARRAY_SIZE(req));
   8.245    msg = errmsg(repmsg);
   8.246    if (msg) {
   8.247      *contents = NULL;
   8.248 @@ -455,6 +482,7 @@ XenBus_List(xenbus_transaction_t xbt, co
   8.249    return NULL;
   8.250  }
   8.251  
   8.252 +#if 0 // test code
   8.253  void
   8.254  do_ls_test(const char *pre)
   8.255  {
   8.256 @@ -479,6 +507,7 @@ do_ls_test(const char *pre)
   8.257    ExFreePoolWithTag(dirs, XENPCI_POOL_TAG);
   8.258    //KdPrint((__DRIVER_NAME " --> do_ls_test\n"));
   8.259  }
   8.260 +#endif
   8.261  
   8.262  int ReadThreadSetCount;
   8.263  int ReadThreadWaitCount;
   8.264 @@ -593,8 +622,14 @@ XenBus_WatchThreadProc(PVOID StartContex
   8.265  }    
   8.266  
   8.267  char *
   8.268 -XenBus_AddWatch(xenbus_transaction_t xbt, const char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext)
   8.269 +XenBus_AddWatch (
   8.270 +  PVOID Context,
   8.271 +  xenbus_transaction_t xbt,
   8.272 +  const char *Path,
   8.273 +  PXENBUS_WATCH_CALLBACK ServiceRoutine,
   8.274 +  PVOID ServiceContext)
   8.275  {
   8.276 +  WDFDEVICE Device = Context;
   8.277    struct xsd_sockmsg *rep;
   8.278    char *msg;
   8.279    int i;
   8.280 @@ -622,7 +657,7 @@ XenBus_AddWatch(xenbus_transaction_t xbt
   8.281    req[1].data = Token;
   8.282    req[1].len = strlen(Token) + 1;
   8.283  
   8.284 -  rep = xenbus_msg_reply(XS_WATCH, xbt, req, ARRAY_SIZE(req));
   8.285 +  rep = xenbus_msg_reply(Device, XS_WATCH, xbt, req, ARRAY_SIZE(req));
   8.286  
   8.287    msg = errmsg(rep);
   8.288    if (msg)
   8.289 @@ -642,8 +677,14 @@ XenBus_AddWatch(xenbus_transaction_t xbt
   8.290  }
   8.291  
   8.292  char *
   8.293 -XenBus_RemWatch(xenbus_transaction_t xbt, const char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext)
   8.294 +XenBus_RemWatch(
   8.295 +  PVOID Context,
   8.296 +  xenbus_transaction_t xbt,
   8.297 +  const char *Path,
   8.298 +  PXENBUS_WATCH_CALLBACK ServiceRoutine,
   8.299 +  PVOID ServiceContext)
   8.300  {
   8.301 +  WDFDEVICE Device = Context;
   8.302    struct xsd_sockmsg *rep;
   8.303    char *msg;
   8.304    int i;
   8.305 @@ -672,7 +713,7 @@ XenBus_RemWatch(xenbus_transaction_t xbt
   8.306    req[1].data = Token;
   8.307    req[1].len = strlen(Token) + 1;
   8.308  
   8.309 -  rep = xenbus_msg_reply(XS_UNWATCH, xbt, req, ARRAY_SIZE(req));
   8.310 +  rep = xenbus_msg_reply(Device, XS_UNWATCH, xbt, req, ARRAY_SIZE(req));
   8.311  
   8.312    msg = errmsg(rep);
   8.313    if (msg)
   8.314 @@ -689,15 +730,16 @@ XenBus_RemWatch(xenbus_transaction_t xbt
   8.315  
   8.316  
   8.317  char *
   8.318 -XenBus_StartTransaction(xenbus_transaction_t *xbt)
   8.319 +XenBus_StartTransaction(PVOID Context, xenbus_transaction_t *xbt)
   8.320  {
   8.321 +  WDFDEVICE Device = Context;
   8.322    /* xenstored becomes angry if you send a length 0 message, so just
   8.323       shove a nul terminator on the end */
   8.324    struct write_req req = { "", 1};
   8.325    struct xsd_sockmsg *rep;
   8.326    char *err;
   8.327  
   8.328 -  rep = xenbus_msg_reply(XS_TRANSACTION_START, 0, &req, 1);
   8.329 +  rep = xenbus_msg_reply(Device, XS_TRANSACTION_START, 0, &req, 1);
   8.330    err = errmsg(rep);
   8.331    if (err)
   8.332      return err;
   8.333 @@ -708,8 +750,9 @@ XenBus_StartTransaction(xenbus_transacti
   8.334  }
   8.335  
   8.336  char *
   8.337 -XenBus_EndTransaction(xenbus_transaction_t t, int abort, int *retry)
   8.338 +XenBus_EndTransaction(PVOID Context, xenbus_transaction_t t, int abort, int *retry)
   8.339  {
   8.340 +  WDFDEVICE Device = Context;
   8.341    struct xsd_sockmsg *rep;
   8.342    struct write_req req;
   8.343    char *err;
   8.344 @@ -718,7 +761,7 @@ XenBus_EndTransaction(xenbus_transaction
   8.345  
   8.346    req.data = abort ? "F" : "T";
   8.347    req.len = 2;
   8.348 -  rep = xenbus_msg_reply(XS_TRANSACTION_END, t, &req, 1);
   8.349 +  rep = xenbus_msg_reply(Device, XS_TRANSACTION_END, t, &req, 1);
   8.350    err = errmsg(rep);
   8.351    if (err) {
   8.352      if (!strcmp(err, "EAGAIN")) {
   8.353 @@ -772,13 +815,14 @@ XenBus_Interrupt(PKINTERRUPT Interrupt, 
   8.354  }
   8.355  
   8.356  char *
   8.357 -XenBus_Printf(xenbus_transaction_t xbt, const char *path, const char *fmt, ...)
   8.358 +XenBus_Printf(PVOID Context, xenbus_transaction_t xbt, const char *path, const char *fmt, ...)
   8.359  {
   8.360 +  WDFDEVICE Device = Context;
   8.361    va_list ap;
   8.362    char buf[1024];
   8.363  
   8.364    va_start(ap, fmt);
   8.365    RtlStringCbVPrintfA(buf, ARRAY_SIZE(buf), fmt, ap);
   8.366    va_end(ap);
   8.367 -  return XenBus_Write(xbt, path, buf);
   8.368 +  return XenBus_Write(Device, xbt, path, buf);
   8.369  }
   8.370 \ No newline at end of file
     9.1 --- a/xenpci/xenbus.h	Wed Nov 28 14:44:10 2007 -0800
     9.2 +++ b/xenpci/xenbus.h	Wed Nov 28 14:51:17 2007 -0800
     9.3 @@ -132,13 +132,13 @@ struct xsd_sockmsg
     9.4  
     9.5  #define XBT_NIL ((xenbus_transaction_t)0)
     9.6  char *
     9.7 -XenBus_Read(xenbus_transaction_t xbt, const char *path, char **value);
     9.8 +XenBus_Read(PVOID Context, xenbus_transaction_t xbt, const char *path, char **value);
     9.9  char *
    9.10 -XenBus_Write(xenbus_transaction_t xbt, const char *path, const char *value);
    9.11 +XenBus_Write(PVOID Context, xenbus_transaction_t xbt, const char *path, const char *value);
    9.12  char *
    9.13 -XenBus_StartTransaction(xenbus_transaction_t *xbt);
    9.14 +XenBus_StartTransaction(PVOID Context, xenbus_transaction_t *xbt);
    9.15  char *
    9.16 -XenBus_EndTransaction(xenbus_transaction_t t, int abort, int *retry);
    9.17 +XenBus_EndTransaction(PVOID Context, xenbus_transaction_t t, int abort, int *retry);
    9.18  
    9.19  
    9.20  char *
    10.1 --- a/xenpci/xenpci.c	Wed Nov 28 14:44:10 2007 -0800
    10.2 +++ b/xenpci/xenpci.c	Wed Nov 28 14:51:17 2007 -0800
    10.3 @@ -68,13 +68,9 @@ XenPCI_XenBusWatchHandler(char *Path, PV
    10.4  #pragma alloc_text (PAGE, XenPCI_AddDevice)
    10.5  #endif
    10.6  
    10.7 -shared_info_t *shared_info_area;
    10.8 -static ULONG irqNumber = 0;
    10.9 -
   10.10 +/* Global (driver-wide) variables */
   10.11  static BOOLEAN AutoEnumerate;
   10.12  
   10.13 -static WDFDEVICE Device;
   10.14 -
   10.15  NTSTATUS
   10.16  DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
   10.17  {
   10.18 @@ -189,22 +185,10 @@ DriverEntry(PDRIVER_OBJECT DriverObject,
   10.19    return status;
   10.20  }
   10.21  
   10.22 -static void WRMSR(unsigned int msr, ULONGLONG val)
   10.23 +static NTSTATUS
   10.24 +get_hypercall_stubs(WDFDEVICE Device)
   10.25  {
   10.26 -  ULONG lo, hi;
   10.27 -  lo = (ULONG)val;
   10.28 -  hi = (ULONG)(val >> 32);
   10.29 -  __asm  {
   10.30 -    mov ecx, msr
   10.31 -    mov eax, lo
   10.32 -    mov edx, hi
   10.33 -    wrmsr
   10.34 -  }
   10.35 -}
   10.36 -
   10.37 -static int
   10.38 -get_hypercall_stubs()
   10.39 -{
   10.40 +  PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);
   10.41    DWORD32 cpuid_output[4];
   10.42    char xensig[13];
   10.43    ULONG i;
   10.44 @@ -223,47 +207,39 @@ get_hypercall_stubs()
   10.45    msr = cpuid_output[1];
   10.46    //KdPrint((__DRIVER_NAME " Hypercall area is %u pages.\n", pages));
   10.47  
   10.48 -  hypercall_stubs = ExAllocatePoolWithTag(NonPagedPool, pages * PAGE_SIZE, XENPCI_POOL_TAG);
   10.49 +  deviceData->hypercall_stubs = ExAllocatePoolWithTag(NonPagedPool, pages * PAGE_SIZE, XENPCI_POOL_TAG);
   10.50    //KdPrint((__DRIVER_NAME " Hypercall area at %08x\n", hypercall_stubs));
   10.51  
   10.52 -  if (hypercall_stubs == NULL)
   10.53 +  if (!deviceData->hypercall_stubs)
   10.54      return 1;
   10.55    for (i = 0; i < pages; i++) {
   10.56      ULONG pfn;
   10.57      //pfn = vmalloc_to_pfn((char *)hypercall_stubs + i * PAGE_SIZE);
   10.58 -    pfn = (ULONG)(MmGetPhysicalAddress(hypercall_stubs + i * PAGE_SIZE).QuadPart >> PAGE_SHIFT);
   10.59 +    pfn = (ULONG)(MmGetPhysicalAddress(deviceData->hypercall_stubs + i * PAGE_SIZE).QuadPart >> PAGE_SHIFT);
   10.60      //KdPrint((__DRIVER_NAME " pfn = %08X\n", pfn));
   10.61 -    WRMSR(msr, ((ULONGLONG)pfn << PAGE_SHIFT) + i);
   10.62 +    __writemsr(msr, ((ULONGLONG)pfn << PAGE_SHIFT) + i);
   10.63    }
   10.64    return STATUS_SUCCESS;
   10.65  }
   10.66  
   10.67 -static ULONG PciBusNumber;
   10.68 -static USHORT PciFunctionNumber, PciDeviceNumber;
   10.69 -static USHORT PciPinNumber;
   10.70 -
   10.71 -static WDFINTERRUPT XenInterrupt;
   10.72 +PHYSICAL_ADDRESS
   10.73 +XenPCI_AllocMMIO(WDFDEVICE Device, ULONG len)
   10.74 +{
   10.75 +  PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);
   10.76  
   10.77 -static PHYSICAL_ADDRESS platform_mmio_addr;
   10.78 -static ULONG platform_mmio_orig_len;
   10.79 -static ULONG platform_mmio_len;
   10.80 -static ULONG platform_mmio_alloc;
   10.81 -
   10.82 -PHYSICAL_ADDRESS
   10.83 -XenPCI_AllocMMIO(ULONG len)
   10.84 -{
   10.85    PHYSICAL_ADDRESS addr;
   10.86  
   10.87 -  addr = platform_mmio_addr;
   10.88 -  addr.QuadPart += platform_mmio_alloc;
   10.89 -  platform_mmio_alloc += len;
   10.90 +  addr = deviceData->platform_mmio_addr;
   10.91 +  addr.QuadPart += deviceData->platform_mmio_alloc;
   10.92 +  deviceData->platform_mmio_alloc += len;
   10.93  
   10.94    return addr;
   10.95  }
   10.96  
   10.97  static int
   10.98 -init_xen_info()
   10.99 +init_xen_info(WDFDEVICE Device)
  10.100  {
  10.101 +  PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);
  10.102    struct xen_add_to_physmap xatp;
  10.103    int ret;
  10.104    PHYSICAL_ADDRESS shared_info_area_unmapped;
  10.105 @@ -271,23 +247,22 @@ init_xen_info()
  10.106    //setup_xen_features();
  10.107    //KdPrint((__DRIVER_NAME " init_xen_info Hypercall area at %08x\n", hypercall_stubs));
  10.108  
  10.109 -  shared_info_area_unmapped = XenPCI_AllocMMIO(PAGE_SIZE);
  10.110 +  shared_info_area_unmapped = XenPCI_AllocMMIO(Device, PAGE_SIZE);
  10.111    xatp.domid = DOMID_SELF;
  10.112    xatp.idx = 0;
  10.113    xatp.space = XENMAPSPACE_shared_info;
  10.114    xatp.gpfn = (xen_pfn_t)(shared_info_area_unmapped.QuadPart >> PAGE_SHIFT);
  10.115 -  ret = HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp);
  10.116 +  ret = HYPERVISOR_memory_op(Device, XENMEM_add_to_physmap, &xatp);
  10.117    //KdPrint((__DRIVER_NAME " ret = %d\n", ret));
  10.118  
  10.119 -  shared_info_area = (shared_info_t *)MmMapIoSpace(shared_info_area_unmapped, PAGE_SIZE, MmNonCached);
  10.120 +  deviceData->shared_info_area = MmMapIoSpace(shared_info_area_unmapped,
  10.121 +    PAGE_SIZE, MmNonCached);
  10.122  
  10.123    return 0;
  10.124  }
  10.125  
  10.126 -static PKINTERRUPT interrupt;
  10.127 -
  10.128  static int
  10.129 -set_callback_irq(ULONGLONG irq)
  10.130 +set_callback_irq(WDFDEVICE Device, ULONGLONG irq)
  10.131  {
  10.132    struct xen_hvm_param a;
  10.133    int retval;
  10.134 @@ -296,7 +271,7 @@ set_callback_irq(ULONGLONG irq)
  10.135    a.domid = DOMID_SELF;
  10.136    a.index = HVM_PARAM_CALLBACK_IRQ;
  10.137    a.value = irq;
  10.138 -  retval = HYPERVISOR_hvm_op(HVMOP_set_param, &a);
  10.139 +  retval = HYPERVISOR_hvm_op(Device, HVMOP_set_param, &a);
  10.140    //KdPrint((__DRIVER_NAME " HYPERVISOR_hvm_op retval = %d\n", retval));
  10.141    //KdPrint((__DRIVER_NAME " <-- set_callback_irq\n"));
  10.142    return retval;
  10.143 @@ -310,17 +285,13 @@ XenPCI_AddDevice(
  10.144  {
  10.145    NTSTATUS status;
  10.146    WDF_CHILD_LIST_CONFIG config;
  10.147 -  PXENPCI_DEVICE_DATA devData = NULL;
  10.148    WDF_OBJECT_ATTRIBUTES attributes;
  10.149    WDF_PNPPOWER_EVENT_CALLBACKS pnpPowerCallbacks;
  10.150 -  WDF_IO_QUEUE_CONFIG ioQConfig;
  10.151    WDF_INTERRUPT_CONFIG interruptConfig;
  10.152    PNP_BUS_INFORMATION busInfo;
  10.153    BUS_INTERFACE_STANDARD BusInterface;
  10.154 -  //WDFDEVICE Parent;
  10.155 -
  10.156 -  //PDEVICE_OBJECT pdo;
  10.157 -  //ULONG propertyAddress, length;
  10.158 +  WDFDEVICE Device;
  10.159 +  PXENPCI_DEVICE_DATA deviceData;
  10.160  
  10.161    UNREFERENCED_PARAMETER(Driver);
  10.162  
  10.163 @@ -344,8 +315,10 @@ XenPCI_AddDevice(
  10.164    pnpPowerCallbacks.EvtDevicePrepareHardware = XenPCI_PrepareHardware;
  10.165    pnpPowerCallbacks.EvtDeviceReleaseHardware = XenPCI_ReleaseHardware;
  10.166    pnpPowerCallbacks.EvtDeviceD0Entry = XenPCI_D0Entry;
  10.167 -  pnpPowerCallbacks.EvtDeviceD0EntryPostInterruptsEnabled = XenPCI_D0EntryPostInterruptsEnabled;
  10.168 -  pnpPowerCallbacks.EvtDeviceD0ExitPreInterruptsDisabled = XenPCI_D0ExitPreInterruptsDisabled;
  10.169 +  pnpPowerCallbacks.EvtDeviceD0EntryPostInterruptsEnabled
  10.170 +    = XenPCI_D0EntryPostInterruptsEnabled;
  10.171 +  pnpPowerCallbacks.EvtDeviceD0ExitPreInterruptsDisabled
  10.172 +    = XenPCI_D0ExitPreInterruptsDisabled;
  10.173    pnpPowerCallbacks.EvtDeviceD0Exit = XenPCI_D0Exit;
  10.174    WdfDeviceInitSetPnpPowerEventCallbacks(DeviceInit, &pnpPowerCallbacks);
  10.175  
  10.176 @@ -358,9 +331,11 @@ XenPCI_AddDevice(
  10.177    status = WdfDeviceCreate(&DeviceInit, &attributes, &Device);  
  10.178    if(!NT_SUCCESS(status))
  10.179    {
  10.180 -    KdPrint((__DRIVER_NAME "     WdfDeviceCreate failed with status 0x%08x\n", status));
  10.181 +    KdPrint((__DRIVER_NAME " WdfDeviceCreate failed with status 0x%08x\n", status));
  10.182      return status;
  10.183    }
  10.184 +  deviceData = GetDeviceData(Device);
  10.185 +  deviceData->Device = Device;
  10.186  
  10.187    WdfDeviceSetSpecialFileSupport(Device, WdfSpecialFilePaging, TRUE);
  10.188    WdfDeviceSetSpecialFileSupport(Device, WdfSpecialFileHibernation, TRUE);
  10.189 @@ -369,46 +344,19 @@ XenPCI_AddDevice(
  10.190    status = WdfFdoQueryForInterface(Device, &GUID_BUS_INTERFACE_STANDARD, (PINTERFACE) &BusInterface, sizeof(BUS_INTERFACE_STANDARD), 1, NULL);
  10.191    if(!NT_SUCCESS(status))
  10.192    {
  10.193 -    KdPrint((__DRIVER_NAME "     WdfFdoQueryForInterface (BusInterface) failed with status 0x%08x\n", status));
  10.194 +    KdPrint((__DRIVER_NAME " WdfFdoQueryForInterface (BusInterface) failed with status 0x%08x\n", status));
  10.195    }
  10.196  
  10.197 -  //BusInterface.GetBusData(NULL, PCI_WHICHSPACE_CONFIG, buf, 
  10.198 -
  10.199 -  
  10.200    busInfo.BusTypeGuid = GUID_XENPCI_DEVCLASS;
  10.201    busInfo.LegacyBusType = PNPBus;
  10.202    busInfo.BusNumber = 0;
  10.203  
  10.204    WdfDeviceSetBusInformationForChildren(Device, &busInfo);
  10.205  
  10.206 -  /*create the default IO queue. this one will 
  10.207 -  be used for all requests*/
  10.208 -/*
  10.209 -  WDF_IO_QUEUE_CONFIG_INIT_DEFAULT_QUEUE(&ioQConfig,
  10.210 -                                WdfIoQueueDispatchSequential);
  10.211 -  ioQConfig.EvtIoDefault = XenPCI_IoDefault;
  10.212 -  status = WdfIoQueueCreate(Device, &ioQConfig,
  10.213 -                            WDF_NO_OBJECT_ATTRIBUTES,
  10.214 -                            &devData->IoDefaultQueue);
  10.215 -  if(!NT_SUCCESS(status))
  10.216 -  {
  10.217 -    KdPrint((__DRIVER_NAME "     WdfIoQueueCreate failed with status 0x%08x\n", status));
  10.218 -    return status;
  10.219 -  }
  10.220 -*/
  10.221 -/*
  10.222 -  status = WdfDeviceCreateDeviceInterface(Device, &GUID_INTERFACE_XENPCI, NULL);
  10.223 -  if(!NT_SUCCESS(status))
  10.224 -  {
  10.225 -    KdPrint((__DRIVER_NAME "WdfDeviceCreateDeviceInterface failed with status 0x%08x\n", status));
  10.226 -    return status;
  10.227 -  }
  10.228 -*/
  10.229 -
  10.230 -  WDF_INTERRUPT_CONFIG_INIT(&interruptConfig, EvtChn_Interrupt, NULL); //EvtChn_InterruptDpc);
  10.231 +  WDF_INTERRUPT_CONFIG_INIT(&interruptConfig, EvtChn_Interrupt, NULL);
  10.232    interruptConfig.EvtInterruptEnable = XenPCI_InterruptEnable;
  10.233    interruptConfig.EvtInterruptDisable = XenPCI_InterruptDisable;
  10.234 -  status = WdfInterruptCreate(Device, &interruptConfig, WDF_NO_OBJECT_ATTRIBUTES, &XenInterrupt);
  10.235 +  status = WdfInterruptCreate(Device, &interruptConfig, WDF_NO_OBJECT_ATTRIBUTES, &deviceData->XenInterrupt);
  10.236    if (!NT_SUCCESS (status))
  10.237    {
  10.238      KdPrint((__DRIVER_NAME "WdfInterruptCreate failed 0x%08x\n", status));
  10.239 @@ -427,12 +375,13 @@ XenPCI_PrepareHardware(
  10.240  {
  10.241    NTSTATUS status = STATUS_SUCCESS;
  10.242    PCM_PARTIAL_RESOURCE_DESCRIPTOR descriptor;
  10.243 -  ULONG i;  
  10.244 -
  10.245 -  UNREFERENCED_PARAMETER(Device);
  10.246 +  ULONG i;
  10.247 +  PXENPCI_DEVICE_DATA deviceData;
  10.248  
  10.249    KdPrint((__DRIVER_NAME " --> EvtDevicePrepareHardware\n"));
  10.250  
  10.251 +  deviceData = GetDeviceData(Device);
  10.252 +
  10.253    for (i = 0; i < WdfCmResourceListGetCount(ResourceList); i++)
  10.254    {
  10.255      descriptor = WdfCmResourceListGetDescriptor(ResourceList, i);
  10.256 @@ -441,7 +390,7 @@ XenPCI_PrepareHardware(
  10.257      switch (descriptor->Type)
  10.258      {
  10.259      case CmResourceTypeInterrupt:
  10.260 -      irqNumber = descriptor->u.Interrupt.Vector;
  10.261 +      deviceData->irqNumber = descriptor->u.Interrupt.Vector;
  10.262        break;
  10.263      }
  10.264    }
  10.265 @@ -465,9 +414,9 @@ XenPCI_PrepareHardware(
  10.266        break;
  10.267      case CmResourceTypeMemory:
  10.268        KdPrint((__DRIVER_NAME "     Memory mapped CSR:(%x:%x) Length:(%d)\n", descriptor->u.Memory.Start.LowPart, descriptor->u.Memory.Start.HighPart, descriptor->u.Memory.Length));
  10.269 -      platform_mmio_addr = descriptor->u.Memory.Start; //(ULONG)MmMapIoSpace(descriptor->u.Memory.Start, descriptor->u.Memory.Length, MmNonCached);
  10.270 -      platform_mmio_len = descriptor->u.Memory.Length;
  10.271 -      platform_mmio_alloc = 0;
  10.272 +      deviceData->platform_mmio_addr = descriptor->u.Memory.Start; //(ULONG)MmMapIoSpace(descriptor->u.Memory.Start, descriptor->u.Memory.Length, MmNonCached);
  10.273 +      deviceData->platform_mmio_len = descriptor->u.Memory.Length;
  10.274 +      deviceData->platform_mmio_alloc = 0;
  10.275        break;
  10.276      case CmResourceTypeInterrupt:
  10.277        //KdPrint((__DRIVER_NAME "     Interrupt level: 0x%0x, Vector: 0x%0x\n", descriptor->u.Interrupt.Level, descriptor->u.Interrupt.Vector));
  10.278 @@ -481,21 +430,21 @@ XenPCI_PrepareHardware(
  10.279      }
  10.280    }
  10.281  
  10.282 -  get_hypercall_stubs();
  10.283 -
  10.284 -  init_xen_info();
  10.285 +  get_hypercall_stubs(Device);
  10.286  
  10.287 -  GntTbl_Init();
  10.288 +  init_xen_info(Device);
  10.289  
  10.290 -  EvtChn_Init();
  10.291 +  GntTbl_Init(Device);
  10.292  
  10.293 -  set_callback_irq(irqNumber);
  10.294 +  EvtChn_Init(Device);
  10.295  
  10.296 -  XenBus_Init();
  10.297 +  set_callback_irq(Device, deviceData->irqNumber);
  10.298 +
  10.299 +  XenBus_Init(Device);
  10.300  
  10.301    //KdPrint((__DRIVER_NAME " upcall_pending = %d\n", shared_info_area->vcpu_info[0].evtchn_upcall_pending));
  10.302  
  10.303 -  shared_info_area->vcpu_info[0].evtchn_upcall_mask = 0;
  10.304 +  deviceData->shared_info_area->vcpu_info[0].evtchn_upcall_mask = 0;
  10.305  
  10.306    //xen_reboot_init();
  10.307  
  10.308 @@ -531,10 +480,8 @@ XenPCI_D0Entry(
  10.309    return status;
  10.310  }
  10.311  
  10.312 -static HANDLE  ThreadHandle;
  10.313 -
  10.314  static NTSTATUS
  10.315 -XenPCI_D0EntryPostInterruptsEnabled(WDFDEVICE  Device, WDF_POWER_DEVICE_STATE PreviousState)
  10.316 +XenPCI_D0EntryPostInterruptsEnabled(WDFDEVICE Device, WDF_POWER_DEVICE_STATE PreviousState)
  10.317  {
  10.318    NTSTATUS status = STATUS_SUCCESS;
  10.319    //OBJECT_ATTRIBUTES oa;
  10.320 @@ -544,31 +491,30 @@ XenPCI_D0EntryPostInterruptsEnabled(WDFD
  10.321    int i;
  10.322    char buffer[128];
  10.323  
  10.324 -  UNREFERENCED_PARAMETER(Device);
  10.325    UNREFERENCED_PARAMETER(PreviousState);
  10.326  
  10.327    KdPrint((__DRIVER_NAME " --> EvtDeviceD0EntryPostInterruptsEnabled\n"));
  10.328  
  10.329 -  XenBus_Start();
  10.330 +  XenBus_Start(Device);
  10.331  
  10.332    KdPrint((__DRIVER_NAME "     A\n"));
  10.333    
  10.334 -  response = XenBus_AddWatch(XBT_NIL, SHUTDOWN_PATH, XenBus_ShutdownHandler, NULL);
  10.335 +  response = XenBus_AddWatch(Device, XBT_NIL, SHUTDOWN_PATH, XenBus_ShutdownHandler, Device);
  10.336    KdPrint((__DRIVER_NAME "     shutdown watch response = '%s'\n", response)); 
  10.337  
  10.338 -  response = XenBus_AddWatch(XBT_NIL, BALLOON_PATH, XenBus_BalloonHandler, NULL);
  10.339 +  response = XenBus_AddWatch(Device, XBT_NIL, BALLOON_PATH, XenBus_BalloonHandler, Device);
  10.340    KdPrint((__DRIVER_NAME "     shutdown watch response = '%s'\n", response)); 
  10.341  
  10.342 -  response = XenBus_AddWatch(XBT_NIL, "device", XenPCI_XenBusWatchHandler, NULL);
  10.343 +  response = XenBus_AddWatch(Device, XBT_NIL, "device", XenPCI_XenBusWatchHandler, Device);
  10.344    KdPrint((__DRIVER_NAME "     device watch response = '%s'\n", response)); 
  10.345  
  10.346 -  msgTypes = XenBus_List(XBT_NIL, "device", &Types);
  10.347 +  msgTypes = XenBus_List(Device, XBT_NIL, "device", &Types);
  10.348    if (!msgTypes) {
  10.349      for (i = 0; Types[i]; i++)
  10.350      {
  10.351        RtlStringCbPrintfA(buffer, ARRAY_SIZE(buffer), "device/%s", Types[i]);
  10.352        //KdPrint((__DRIVER_NAME "     ls device[%d] -> %s\n", i, Types[i]));
  10.353 -      XenPCI_XenBusWatchHandler(buffer, NULL);
  10.354 +      XenPCI_XenBusWatchHandler(buffer, Device);
  10.355        ExFreePoolWithTag(Types[i], XENPCI_POOL_TAG);
  10.356      }
  10.357    }
  10.358 @@ -582,10 +528,9 @@ XenPCI_D0ExitPreInterruptsDisabled(WDFDE
  10.359  {
  10.360    NTSTATUS status = STATUS_SUCCESS;
  10.361  
  10.362 -  UNREFERENCED_PARAMETER(Device);
  10.363    UNREFERENCED_PARAMETER(TargetState);
  10.364  
  10.365 -  XenBus_Stop();
  10.366 +  XenBus_Stop(Device);
  10.367  
  10.368    return status;
  10.369  }
  10.370 @@ -623,12 +568,12 @@ XenPCI_IoDefault(
  10.371  static NTSTATUS
  10.372  XenPCI_InterruptEnable(WDFINTERRUPT Interrupt, WDFDEVICE AssociatedDevice)
  10.373  {
  10.374 +  PXENPCI_DEVICE_DATA deviceData = GetDeviceData(AssociatedDevice);
  10.375    UNREFERENCED_PARAMETER(Interrupt);
  10.376 -  UNREFERENCED_PARAMETER(AssociatedDevice);
  10.377  
  10.378    KdPrint((__DRIVER_NAME " --> EvtInterruptEnable\n"));
  10.379  
  10.380 -  shared_info_area->vcpu_info[0].evtchn_upcall_mask = 0;
  10.381 +  deviceData->shared_info_area->vcpu_info[0].evtchn_upcall_mask = 0;
  10.382  
  10.383    KdPrint((__DRIVER_NAME " <-- EvtInterruptEnable\n"));
  10.384  
  10.385 @@ -638,12 +583,12 @@ XenPCI_InterruptEnable(WDFINTERRUPT Inte
  10.386  static NTSTATUS
  10.387  XenPCI_InterruptDisable(WDFINTERRUPT Interrupt, WDFDEVICE AssociatedDevice)
  10.388  {
  10.389 +  PXENPCI_DEVICE_DATA deviceData = GetDeviceData(AssociatedDevice);
  10.390    UNREFERENCED_PARAMETER(Interrupt);
  10.391 -  UNREFERENCED_PARAMETER(AssociatedDevice);
  10.392  
  10.393    //KdPrint((__DRIVER_NAME " --> EvtInterruptDisable\n"));
  10.394  
  10.395 -  shared_info_area->vcpu_info[0].evtchn_upcall_mask = 1;
  10.396 +  deviceData->shared_info_area->vcpu_info[0].evtchn_upcall_mask = 1;
  10.397    // should we kick off any pending interrupts here?
  10.398  
  10.399    //KdPrint((__DRIVER_NAME " <-- EvtInterruptDisable\n"));
  10.400 @@ -652,23 +597,23 @@ XenPCI_InterruptDisable(WDFINTERRUPT Int
  10.401  }
  10.402  
  10.403  static NTSTATUS
  10.404 -XenPCI_ChildListCreateDevice(WDFCHILDLIST ChildList, PWDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER IdentificationDescription, PWDFDEVICE_INIT ChildInit)
  10.405 +XenPCI_ChildListCreateDevice(
  10.406 +  WDFCHILDLIST ChildList,
  10.407 +  PWDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER IdentificationDescription,
  10.408 +  PWDFDEVICE_INIT ChildInit)
  10.409  {
  10.410    NTSTATUS status;
  10.411    WDFDEVICE ChildDevice = NULL;
  10.412    PXENPCI_IDENTIFICATION_DESCRIPTION XenIdentificationDesc;
  10.413    XEN_IFACE_EVTCHN EvtChnInterface;
  10.414    XEN_IFACE_XENBUS XenBusInterface;
  10.415 -  XEN_IFACE_XEN XenInterface;
  10.416 +  //XEN_IFACE_XEN XenInterface;
  10.417    XEN_IFACE_GNTTBL GntTblInterface;
  10.418 -  //UNICODE_STRING DeviceId;
  10.419    DECLARE_UNICODE_STRING_SIZE(buffer, 20);
  10.420    WDF_OBJECT_ATTRIBUTES PdoAttributes;
  10.421    DECLARE_CONST_UNICODE_STRING(DeviceLocation, L"Xen Bus");
  10.422    WDF_QUERY_INTERFACE_CONFIG  qiConfig;
  10.423 -  //WDF_PDO_EVENT_CALLBACKS PdoCallbacks;
  10.424    PXENPCI_XEN_DEVICE_DATA ChildDeviceData = NULL;
  10.425 -  //size_t path_len;
  10.426  
  10.427    UNREFERENCED_PARAMETER(ChildList);
  10.428  
  10.429 @@ -712,11 +657,11 @@ XenPCI_ChildListCreateDevice(WDFCHILDLIS
  10.430  
  10.431    ChildDeviceData = GetXenDeviceData(ChildDevice);
  10.432    strncpy(ChildDeviceData->BasePath, XenIdentificationDesc->Path, 128);
  10.433 -  
  10.434 +
  10.435    RtlZeroMemory(&EvtChnInterface, sizeof(EvtChnInterface));
  10.436    EvtChnInterface.InterfaceHeader.Size = sizeof(EvtChnInterface);
  10.437    EvtChnInterface.InterfaceHeader.Version = 1;
  10.438 -  EvtChnInterface.InterfaceHeader.Context = NULL;
  10.439 +  EvtChnInterface.InterfaceHeader.Context = WdfPdoGetParent(ChildDevice);
  10.440    EvtChnInterface.InterfaceHeader.InterfaceReference = WdfDeviceInterfaceReferenceNoOp;
  10.441    EvtChnInterface.InterfaceHeader.InterfaceDereference = WdfDeviceInterfaceDereferenceNoOp;
  10.442    EvtChnInterface.Bind = EvtChn_Bind;
  10.443 @@ -732,10 +677,12 @@ XenPCI_ChildListCreateDevice(WDFCHILDLIS
  10.444      return status;
  10.445    }
  10.446  
  10.447 +  /* nobody uses this yet, maybe it isn't needed? */
  10.448 +#if 0
  10.449    RtlZeroMemory(&XenInterface, sizeof(XenInterface));
  10.450    XenInterface.InterfaceHeader.Size = sizeof(XenInterface);
  10.451    XenInterface.InterfaceHeader.Version = 1;
  10.452 -  XenInterface.InterfaceHeader.Context = NULL;
  10.453 +  XenInterface.InterfaceHeader.Context = WdfPdoGetParent(ChildDevice);
  10.454    XenInterface.InterfaceHeader.InterfaceReference = WdfDeviceInterfaceReferenceNoOp;
  10.455    XenInterface.InterfaceHeader.InterfaceDereference = WdfDeviceInterfaceDereferenceNoOp;
  10.456    XenInterface.AllocMMIO = XenPCI_AllocMMIO;
  10.457 @@ -744,11 +691,12 @@ XenPCI_ChildListCreateDevice(WDFCHILDLIS
  10.458    if (!NT_SUCCESS(status)) {
  10.459      return status;
  10.460    }
  10.461 +#endif
  10.462  
  10.463    RtlZeroMemory(&GntTblInterface, sizeof(GntTblInterface));
  10.464    GntTblInterface.InterfaceHeader.Size = sizeof(GntTblInterface);
  10.465    GntTblInterface.InterfaceHeader.Version = 1;
  10.466 -  GntTblInterface.InterfaceHeader.Context = NULL;
  10.467 +  GntTblInterface.InterfaceHeader.Context = WdfPdoGetParent(ChildDevice);
  10.468    GntTblInterface.InterfaceHeader.InterfaceReference = WdfDeviceInterfaceReferenceNoOp;
  10.469    GntTblInterface.InterfaceHeader.InterfaceDereference = WdfDeviceInterfaceDereferenceNoOp;
  10.470    GntTblInterface.GrantAccess = GntTbl_GrantAccess;
  10.471 @@ -763,7 +711,7 @@ XenPCI_ChildListCreateDevice(WDFCHILDLIS
  10.472  
  10.473    XenBusInterface.InterfaceHeader.Size = sizeof(XenBusInterface);
  10.474    XenBusInterface.InterfaceHeader.Version = 1;
  10.475 -  XenBusInterface.InterfaceHeader.Context = NULL;
  10.476 +  XenBusInterface.InterfaceHeader.Context = WdfPdoGetParent(ChildDevice);
  10.477    //XenBusInterface.InterfaceHeader.Context = ExAllocatePoolWithTag(NonPagedPool, (strlen(XenIdentificationDesc->Path) + 1), XENPCI_POOL_TAG);
  10.478    //strcpy(XenBusInterface.InterfaceHeader.Context, XenIdentificationDesc->Path);
  10.479    XenBusInterface.Read = XenBus_Read;
  10.480 @@ -792,6 +740,7 @@ XenPCI_XenBusWatchHandler(char *Path, PV
  10.481    NTSTATUS status;
  10.482    char **Bits;
  10.483    int Count;
  10.484 +  WDFDEVICE Device = Data;
  10.485    WDFCHILDLIST ChildList;
  10.486    WDF_CHILD_LIST_ITERATOR ChildIterator;
  10.487    WDFDEVICE ChildDevice;
  10.488 @@ -822,14 +771,17 @@ XenPCI_XenBusWatchHandler(char *Path, PV
  10.489        RtlAnsiStringToUnicodeString(&description.DeviceType, &AnsiBuf, TRUE);
  10.490        status = WdfChildListAddOrUpdateChildDescriptionAsPresent(ChildList, &description.Header, NULL);
  10.491        break;
  10.492 -    default:
  10.493 +    case 3:
  10.494        WDF_CHILD_LIST_ITERATOR_INIT(&ChildIterator, WdfRetrievePresentChildren);
  10.495        WdfChildListBeginIteration(ChildList, &ChildIterator);
  10.496 -      while(NT_SUCCESS(WdfChildListRetrieveNextDevice(ChildList, &ChildIterator, &ChildDevice, NULL)))
  10.497 +      while (NT_SUCCESS(WdfChildListRetrieveNextDevice(ChildList, &ChildIterator, &ChildDevice, NULL)))
  10.498        {
  10.499 -	ChildDeviceData = GetXenDeviceData(ChildDevice);
  10.500 +        ChildDeviceData = GetXenDeviceData(ChildDevice);
  10.501          if (!ChildDeviceData)
  10.502 +        {
  10.503 +          KdPrint((__FUNCTION__ " No child device data, should never happen\n"));
  10.504            continue;
  10.505 +        }
  10.506          if (strncmp(ChildDeviceData->BasePath, Path, strlen(ChildDeviceData->BasePath)) == 0 && Path[strlen(ChildDeviceData->BasePath)] == '/')
  10.507          {
  10.508            //KdPrint((__DRIVER_NAME "     Child Path = %s (Match - WatchHandler = %08x)\n", ChildDeviceData->BasePath, ChildDeviceData->WatchHandler));
  10.509 @@ -843,6 +795,9 @@ XenPCI_XenBusWatchHandler(char *Path, PV
  10.510        }
  10.511        WdfChildListEndIteration(ChildList, &ChildIterator);
  10.512        break;
  10.513 +    default:
  10.514 +      KdPrint((__FUNCTION__ ": Unknown case %d\n", Count));
  10.515 +      break;
  10.516    }
  10.517  
  10.518    FreeSplitString(Bits, Count);
  10.519 @@ -853,26 +808,26 @@ XenPCI_XenBusWatchHandler(char *Path, PV
  10.520  static void
  10.521  XenBus_ShutdownHandler(char *Path, PVOID Data)
  10.522  {
  10.523 +  WDFDEVICE Device = Data;
  10.524    char *value;
  10.525    xenbus_transaction_t xbt;
  10.526    int retry;
  10.527  
  10.528    UNREFERENCED_PARAMETER(Path);
  10.529 -  UNREFERENCED_PARAMETER(Data);
  10.530  
  10.531    KdPrint((__DRIVER_NAME " --> XenBus_ShutdownHandler\n"));
  10.532  
  10.533 -  XenBus_StartTransaction(&xbt);
  10.534 +  XenBus_StartTransaction(Device, &xbt);
  10.535  
  10.536 -  XenBus_Read(XBT_NIL, SHUTDOWN_PATH, &value);
  10.537 +  XenBus_Read(Device, XBT_NIL, SHUTDOWN_PATH, &value);
  10.538  
  10.539    KdPrint((__DRIVER_NAME "     Shutdown Value = %s\n", value));
  10.540  
  10.541    // should check for error here... but why have we been called at all???
  10.542    if (value != NULL && strlen(value) != 0)
  10.543 -    XenBus_Write(XBT_NIL, SHUTDOWN_PATH, "");
  10.544 +    XenBus_Write(Device, XBT_NIL, SHUTDOWN_PATH, "");
  10.545  
  10.546 -  XenBus_EndTransaction(xbt, 0, &retry);
  10.547 +  XenBus_EndTransaction(Device, xbt, 0, &retry);
  10.548    
  10.549    KdPrint((__DRIVER_NAME " <-- XenBus_ShutdownHandler\n"));
  10.550  }
  10.551 @@ -880,25 +835,25 @@ XenBus_ShutdownHandler(char *Path, PVOID
  10.552  static VOID
  10.553  XenBus_BalloonHandler(char *Path, PVOID Data)
  10.554  {
  10.555 +  WDFDEVICE Device = Data;
  10.556    char *value;
  10.557    xenbus_transaction_t xbt;
  10.558    int retry;
  10.559  
  10.560    UNREFERENCED_PARAMETER(Path);
  10.561 -  UNREFERENCED_PARAMETER(Data);
  10.562  
  10.563    KdPrint((__DRIVER_NAME " --> XenBus_BalloonHandler\n"));
  10.564  
  10.565 -  XenBus_StartTransaction(&xbt);
  10.566 +  XenBus_StartTransaction(Device, &xbt);
  10.567  
  10.568 -  XenBus_Read(XBT_NIL, BALLOON_PATH, &value);
  10.569 +  XenBus_Read(Device, XBT_NIL, BALLOON_PATH, &value);
  10.570  
  10.571    KdPrint((__DRIVER_NAME "     Balloon Value = %s\n", value));
  10.572  
  10.573    // use the memory_op(unsigned int op, void *arg) hypercall to adjust this
  10.574    // use XENMEM_increase_reservation and XENMEM_decrease_reservation
  10.575  
  10.576 -  XenBus_EndTransaction(xbt, 0, &retry);
  10.577 +  XenBus_EndTransaction(Device, xbt, 0, &retry);
  10.578    
  10.579    KdPrint((__DRIVER_NAME " <-- XenBus_BalloonHandler\n"));
  10.580  }
  10.581 @@ -1042,8 +997,7 @@ XenPCI_DeviceResourceRequirementsQuery(W
  10.582    NTSTATUS  status;
  10.583    WDFIORESLIST resourceList;
  10.584    IO_RESOURCE_DESCRIPTOR descriptor;
  10.585 -
  10.586 -  UNREFERENCED_PARAMETER(Device);
  10.587 +  PXENPCI_DEVICE_DATA deviceData = GetDeviceData(Device);
  10.588  
  10.589    //KdPrint((__DRIVER_NAME " --> DeviceResourceRequirementsQuery\n"));
  10.590  
  10.591 @@ -1076,8 +1030,10 @@ XenPCI_DeviceResourceRequirementsQuery(W
  10.592    descriptor.Flags = CM_RESOURCE_MEMORY_READ_WRITE;
  10.593    descriptor.u.Memory.Length = PAGE_SIZE;
  10.594    descriptor.u.Memory.Alignment = PAGE_SIZE;
  10.595 -  descriptor.u.Memory.MinimumAddress.QuadPart = platform_mmio_addr.QuadPart + PAGE_SIZE;
  10.596 -  descriptor.u.Memory.MaximumAddress.QuadPart = platform_mmio_addr.QuadPart + platform_mmio_len - 1;
  10.597 +  descriptor.u.Memory.MinimumAddress.QuadPart
  10.598 +    = deviceData->platform_mmio_addr.QuadPart + PAGE_SIZE;
  10.599 +  descriptor.u.Memory.MaximumAddress.QuadPart
  10.600 +    = deviceData->platform_mmio_addr.QuadPart + deviceData->platform_mmio_len - 1;
  10.601  
  10.602    //KdPrint((__DRIVER_NAME "     MinimumAddress = %08x, MaximumAddress = %08X\n", descriptor.u.Memory.MinimumAddress.LowPart, descriptor.u.Memory.MaximumAddress.LowPart));
  10.603  
    11.1 --- a/xenpci/xenpci.h	Wed Nov 28 14:44:10 2007 -0800
    11.2 +++ b/xenpci/xenpci.h	Wed Nov 28 14:51:17 2007 -0800
    11.3 @@ -79,8 +79,6 @@ DEFINE_GUID( GUID_XENPCI_DEVCLASS, 0xC82
    11.4  
    11.5  #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
    11.6  
    11.7 -extern char *hypercall_stubs;
    11.8 -
    11.9  typedef struct _XENPCI_IDENTIFICATION_DESCRIPTION
   11.10  {
   11.11    WDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER Header;
   11.12 @@ -89,28 +87,38 @@ typedef struct _XENPCI_IDENTIFICATION_DE
   11.13    char Path[128];
   11.14  } XENPCI_IDENTIFICATION_DESCRIPTION, *PXENPCI_IDENTIFICATION_DESCRIPTION;
   11.15  
   11.16 +typedef struct _ev_action_t {
   11.17 +  PKSERVICE_ROUTINE ServiceRoutine;
   11.18 +  PVOID ServiceContext;
   11.19 +  ULONG Count;
   11.20 +} ev_action_t;
   11.21 +
   11.22 +#define NR_EVENTS 1024
   11.23  
   11.24  typedef struct {
   11.25 -  WDFQUEUE          IoDefaultQueue;
   11.26 -
   11.27 -  // Resources
   11.28 -  //WDFINTERRUPT      Interrupt;
   11.29 -  //PULONG            PhysAddress;
   11.30 -
   11.31 -  //ULONG platform_mmio_addr;
   11.32 -  //ULONG platform_mmio_len;
   11.33 -  //ULONG platform_mmio_alloc;
   11.34  
   11.35 -  //ULONG shared_info_frame;
   11.36 -  //char *hypercall_stubs;
   11.37 +  WDFDEVICE Device;
   11.38  
   11.39 -  //PULONG            IoBaseAddress;
   11.40 -  //ULONG             IoRange;
   11.41 +  WDFINTERRUPT XenInterrupt;
   11.42 +  ULONG irqNumber;
   11.43  
   11.44 -  // Grant Table stuff
   11.45 +  shared_info_t *shared_info_area;
   11.46  
   11.47 -  //grant_entry_t *gnttab_table;
   11.48 -  //grant_ref_t gnttab_list[NR_GRANT_ENTRIES];
   11.49 +  PHYSICAL_ADDRESS platform_mmio_addr;
   11.50 +  ULONG platform_mmio_orig_len;
   11.51 +  ULONG platform_mmio_len;
   11.52 +  ULONG platform_mmio_alloc;
   11.53 +
   11.54 +  char *hypercall_stubs;
   11.55 +
   11.56 +  evtchn_port_t xen_store_evtchn;
   11.57 +
   11.58 +  grant_entry_t *gnttab_table;
   11.59 +  PHYSICAL_ADDRESS gnttab_table_physical;
   11.60 +  grant_ref_t gnttab_list[NR_GRANT_ENTRIES];
   11.61 +
   11.62 +  ev_action_t ev_actions[NR_EVENTS];
   11.63 +  unsigned long bound_ports[NR_EVENTS/(8*sizeof(unsigned long))];
   11.64  
   11.65  } XENPCI_DEVICE_DATA, *PXENPCI_DEVICE_DATA;
   11.66  
   11.67 @@ -142,17 +150,17 @@ typedef uint32_t XENSTORE_RING_IDX;
   11.68  #include <gnttbl_public.h>
   11.69  
   11.70  char *
   11.71 -XenBus_Read(xenbus_transaction_t xbt, const char *path, char **value);
   11.72 -char *
   11.73 -XenBus_Write(xenbus_transaction_t xbt, const char *path, const char *value);
   11.74 +XenBus_Read(PVOID Context, xenbus_transaction_t xbt, const char *path, char **value);
   11.75  char *
   11.76 -XenBus_Printf(xenbus_transaction_t xbt, const char *path, const char *fmt, ...);
   11.77 +XenBus_Write(PVOID Context, xenbus_transaction_t xbt, const char *path, const char *value);
   11.78  char *
   11.79 -XenBus_StartTransaction(xenbus_transaction_t *xbt);
   11.80 +XenBus_Printf(PVOID Context, xenbus_transaction_t xbt, const char *path, const char *fmt, ...);
   11.81  char *
   11.82 -XenBus_EndTransaction(xenbus_transaction_t t, int abort, int *retry);
   11.83 +XenBus_StartTransaction(PVOID Context, xenbus_transaction_t *xbt);
   11.84  char *
   11.85 -XenBus_List(xenbus_transaction_t xbt, const char *prefix, char ***contents);
   11.86 +XenBus_EndTransaction(PVOID Context, xenbus_transaction_t t, int abort, int *retry);
   11.87 +char *
   11.88 +XenBus_List(PVOID Context, xenbus_transaction_t xbt, const char *prefix, char ***contents);
   11.89  NTSTATUS
   11.90  XenBus_Init();
   11.91  NTSTATUS
   11.92 @@ -164,16 +172,16 @@ XenBus_Stop();
   11.93  //(*PXENBUS_WATCH_CALLBACK)(char *Path, PVOID ServiceContext);
   11.94  
   11.95  char *
   11.96 -XenBus_AddWatch(xenbus_transaction_t xbt, const char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext);
   11.97 +XenBus_AddWatch(PVOID Context, xenbus_transaction_t xbt, const char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext);
   11.98  char *
   11.99 -XenBus_RemWatch(xenbus_transaction_t xbt, const char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext);
  11.100 +XenBus_RemWatch(PVOID Context, xenbus_transaction_t xbt, const char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext);
  11.101  
  11.102  
  11.103  VOID
  11.104  XenBus_ThreadProc(PVOID StartContext);
  11.105  
  11.106  PHYSICAL_ADDRESS
  11.107 -XenPCI_AllocMMIO(ULONG len);
  11.108 +XenPCI_AllocMMIO(WDFDEVICE Device, ULONG len);
  11.109  
  11.110  //PVOID
  11.111  //map_frames(PULONG f, ULONG n);
  11.112 @@ -186,28 +194,28 @@ EvtChn_Interrupt(WDFINTERRUPT Interrupt,
  11.113  BOOLEAN
  11.114  EvtChn_InterruptDpc(WDFINTERRUPT Interrupt, WDFOBJECT AssociatedObject);
  11.115  NTSTATUS
  11.116 -EvtChn_Mask(evtchn_port_t Port);
  11.117 -NTSTATUS
  11.118 -EvtChn_Unmask(evtchn_port_t Port);
  11.119 -NTSTATUS
  11.120 -EvtChn_Bind(evtchn_port_t Port, PKSERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext);
  11.121 +EvtChn_Mask(PVOID Context, evtchn_port_t Port);
  11.122  NTSTATUS
  11.123 -EvtChn_Unbind(evtchn_port_t Port);
  11.124 +EvtChn_Unmask(PVOID Context, evtchn_port_t Port);
  11.125  NTSTATUS
  11.126 -EvtChn_Notify(evtchn_port_t Port);
  11.127 +EvtChn_Bind(PVOID Context, evtchn_port_t Port, PKSERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext);
  11.128 +NTSTATUS
  11.129 +EvtChn_Unbind(PVOID Context, evtchn_port_t Port);
  11.130 +NTSTATUS
  11.131 +EvtChn_Notify(PVOID Context, evtchn_port_t Port);
  11.132  evtchn_port_t
  11.133 -EvtChn_AllocUnbound(domid_t Domain);
  11.134 +EvtChn_AllocUnbound(PVOID Context, domid_t Domain);
  11.135  NTSTATUS
  11.136 -EvtChn_Init();
  11.137 +EvtChn_Init(WDFDEVICE Device);
  11.138  
  11.139  grant_ref_t
  11.140 -GntTbl_GrantAccess(domid_t domid, unsigned long frame, int readonly);
  11.141 +GntTbl_GrantAccess(WDFDEVICE Device, domid_t domid, unsigned long frame, int readonly);
  11.142  BOOLEAN
  11.143 -GntTbl_EndAccess(grant_ref_t ref);
  11.144 +GntTbl_EndAccess(WDFDEVICE Device, grant_ref_t ref);
  11.145  
  11.146  evtchn_port_t
  11.147 -EvtChn_GetXenStorePort();
  11.148 +EvtChn_GetXenStorePort(WDFDEVICE Device);
  11.149  PVOID
  11.150 -EvtChn_GetXenStoreRingAddr();
  11.151 +EvtChn_GetXenStoreRingAddr(WDFDEVICE Device);
  11.152  
  11.153  #endif
    12.1 --- a/xenvbd/xenvbd.c	Wed Nov 28 14:44:10 2007 -0800
    12.2 +++ b/xenvbd/xenvbd.c	Wed Nov 28 14:51:17 2007 -0800
    12.3 @@ -270,11 +270,13 @@ XenVbd_PrepareHardware(
    12.4      KdPrint((__DRIVER_NAME "     WdfFdoQueryForInterface (XenBus) failed with status 0x%08x\n", status));
    12.5    }
    12.6  
    12.7 +#if 0 // not used yet
    12.8    status = WdfFdoQueryForInterface(Device, &GUID_XEN_IFACE_XEN, (PINTERFACE)&XenInterface, sizeof(XEN_IFACE_XEN), 1, NULL);
    12.9    if(!NT_SUCCESS(status))
   12.10    {
   12.11      KdPrint((__DRIVER_NAME "     WdfFdoQueryForInterface (Xen) failed with status 0x%08x\n", status));
   12.12    }
   12.13 +#endif
   12.14  
   12.15    status = WdfFdoQueryForInterface(Device, &GUID_XEN_IFACE_GNTTBL, (PINTERFACE)&GntTblInterface, sizeof(XEN_IFACE_GNTTBL), 1, NULL);
   12.16    if(!NT_SUCCESS(status))
   12.17 @@ -353,7 +355,8 @@ XenVbd_D0EntryPostInterruptsEnabled(WDFD
   12.18    // TODO: Should probably do this in an EvtChildListScanForChildren
   12.19    if (AutoEnumerate)
   12.20    {
   12.21 -    msg = XenBusInterface.List(XBT_NIL, "device/vbd", &VbdDevices);
   12.22 +    msg = XenBusInterface.List(XenBusInterface.InterfaceHeader.Context,
   12.23 +      XBT_NIL, "device/vbd", &VbdDevices);
   12.24      if (!msg) {
   12.25        for (i = 0; VbdDevices[i]; i++)
   12.26        {
   12.27 @@ -565,7 +568,8 @@ XenVbd_DpcThreadProc(WDFDPC Dpc)
   12.28        }
   12.29        for (j = 0; j < ChildDeviceData->shadow[rep->id].req.nr_segments; j++)
   12.30        {
   12.31 -        GntTblInterface.EndAccess(ChildDeviceData->shadow[rep->id].req.seg[j].gref);
   12.32 +        GntTblInterface.EndAccess(GntTblInterface.InterfaceHeader.Context,
   12.33 +          ChildDeviceData->shadow[rep->id].req.seg[j].gref);
   12.34        }
   12.35        BlockCount = (Srb->Cdb[7] << 8) | Srb->Cdb[8];
   12.36        if (ChildDeviceData->shadow[rep->id].Buf != NULL)
   12.37 @@ -613,7 +617,8 @@ XenVbd_DpcThreadProc(WDFDPC Dpc)
   12.38    {
   12.39      RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&ChildDeviceData->Ring, notify);
   12.40      if (notify)
   12.41 -      EvtChnInterface.Notify(ChildDeviceData->EventChannel);
   12.42 +      EvtChnInterface.Notify(EvtChnInterface.InterfaceHeader.Context,
   12.43 +        ChildDeviceData->EventChannel);
   12.44    }
   12.45    KeReleaseSpinLock(&ChildDeviceData->Lock, KIrql);
   12.46  
   12.47 @@ -663,9 +668,11 @@ XenVbd_BackEndStateHandler(char *Path, P
   12.48  
   12.49    DeviceData = (PXENVBD_CHILD_DEVICE_DATA)Data;
   12.50  
   12.51 -  XenBusInterface.Read(XBT_NIL, Path, &Value);
   12.52 +  XenBusInterface.Read(XenBusInterface.InterfaceHeader.Context,
   12.53 +    XBT_NIL, Path, &Value);
   12.54 +  NewState = atoi(Value);
   12.55 +  ExFreePool(Value);
   12.56  
   12.57 -  NewState = atoi(Value);
   12.58    switch (NewState)
   12.59    {
   12.60    case XenbusStateUnknown:
   12.61 @@ -679,14 +686,17 @@ XenVbd_BackEndStateHandler(char *Path, P
   12.62    case XenbusStateInitWait:
   12.63      KdPrint((__DRIVER_NAME "     Backend State Changed to InitWait\n"));  
   12.64  
   12.65 -    DeviceData->EventChannel = EvtChnInterface.AllocUnbound(0);
   12.66 -    EvtChnInterface.Bind(DeviceData->EventChannel, XenVbd_Interrupt, DeviceData);
   12.67 +    DeviceData->EventChannel = EvtChnInterface.AllocUnbound(
   12.68 +      EvtChnInterface.InterfaceHeader.Context, 0);
   12.69 +    EvtChnInterface.Bind(EvtChnInterface.InterfaceHeader.Context,
   12.70 +      DeviceData->EventChannel, XenVbd_Interrupt, DeviceData);
   12.71      Mdl = AllocatePage();
   12.72      PFN = *MmGetMdlPfnArray(Mdl);
   12.73      SharedRing = (blkif_sring_t *)MmGetMdlVirtualAddress(Mdl);
   12.74      SHARED_RING_INIT(SharedRing);
   12.75      FRONT_RING_INIT(&DeviceData->Ring, SharedRing, PAGE_SIZE);
   12.76 -    ref = GntTblInterface.GrantAccess(0, PFN, FALSE);
   12.77 +    ref = GntTblInterface.GrantAccess(GntTblInterface.InterfaceHeader.Context,
   12.78 +      0, PFN, FALSE);
   12.79  
   12.80      RtlStringCbCopyA(TmpPath, 128, DeviceData->Path);
   12.81      RtlStringCbCatA(TmpPath, 128, "/ring-ref");
   12.82 @@ -717,7 +727,8 @@ XenVbd_BackEndStateHandler(char *Path, P
   12.83  
   12.84      RtlStringCbCopyA(TmpPath, 128, DeviceData->Path);
   12.85      RtlStringCbCatA(TmpPath, 128, "/device-type");
   12.86 -    XenBusInterface.Read(XBT_NIL, TmpPath, &Value);
   12.87 +    XenBusInterface.Read(XenBusInterface.InterfaceHeader.Context,
   12.88 +      XBT_NIL, TmpPath, &Value);
   12.89      if (strcmp(Value, "disk") == 0)
   12.90      {
   12.91        KdPrint((__DRIVER_NAME "     DeviceType = Disk\n"));    
   12.92 @@ -735,27 +746,35 @@ XenVbd_BackEndStateHandler(char *Path, P
   12.93  
   12.94      RtlStringCbCopyA(TmpPath, 128, DeviceData->BackendPath);
   12.95      RtlStringCbCatA(TmpPath, 128, "/type"); // should probably check that this is 'phy' or 'file' or at least not ''
   12.96 -    XenBusInterface.Read(XBT_NIL, TmpPath, &Value);
   12.97 +    XenBusInterface.Read(XenBusInterface.InterfaceHeader.Context,
   12.98 +      XBT_NIL, TmpPath, &Value);
   12.99      KdPrint((__DRIVER_NAME "     Backend Type = %s\n", Value));
  12.100 +    ExFreePool(Value);
  12.101  
  12.102      RtlStringCbCopyA(TmpPath, 128, DeviceData->BackendPath);
  12.103      RtlStringCbCatA(TmpPath, 128, "/mode"); // should store this...
  12.104 -    XenBusInterface.Read(XBT_NIL, TmpPath, &Value);
  12.105 +    XenBusInterface.Read(XenBusInterface.InterfaceHeader.Context,
  12.106 +      XBT_NIL, TmpPath, &Value);
  12.107      KdPrint((__DRIVER_NAME "     Backend Mode = %s\n", Value));
  12.108 +    ExFreePool(Value);
  12.109  
  12.110      RtlStringCbCopyA(TmpPath, 128, DeviceData->BackendPath);
  12.111      RtlStringCbCatA(TmpPath, 128, "/sector-size");
  12.112 -    XenBusInterface.Read(XBT_NIL, TmpPath, &Value);
  12.113 +    XenBusInterface.Read(XenBusInterface.InterfaceHeader.Context,
  12.114 +      XBT_NIL, TmpPath, &Value);
  12.115      // should complain if Value == NULL
  12.116      DeviceData->BytesPerSector = atoi(Value);
  12.117 +    ExFreePool(Value);
  12.118  
  12.119      KdPrint((__DRIVER_NAME "     BytesPerSector = %d\n", DeviceData->BytesPerSector));    
  12.120  
  12.121      RtlStringCbCopyA(TmpPath, 128, DeviceData->BackendPath);
  12.122      RtlStringCbCatA(TmpPath, 128, "/sectors");
  12.123 -    XenBusInterface.Read(XBT_NIL, TmpPath, &Value);
  12.124 +    XenBusInterface.Read(XenBusInterface.InterfaceHeader.Context,
  12.125 +      XBT_NIL, TmpPath, &Value);
  12.126      // should complain if Value == NULL
  12.127      DeviceData->TotalSectors = (ULONGLONG)atol(Value);
  12.128 +    ExFreePool(Value);
  12.129  
  12.130      KdPrint((__DRIVER_NAME "     TotalSectors = %d\n", DeviceData->TotalSectors));    
  12.131  
  12.132 @@ -854,7 +873,8 @@ XenVbd_HotPlugHandler(char *Path, PVOID 
  12.133  
  12.134        RtlStringCbCopyA(TmpPath, 128, DeviceData->Path);
  12.135        RtlStringCbCatA(TmpPath, 128, "/backend");
  12.136 -      XenBusInterface.Read(XBT_NIL, TmpPath, &Value);
  12.137 +      XenBusInterface.Read(XenBusInterface.InterfaceHeader.Context,
  12.138 +        XBT_NIL, TmpPath, &Value);
  12.139        if (Value == NULL)
  12.140        {
  12.141          KdPrint((__DRIVER_NAME "     Read Failed\n"));
  12.142 @@ -863,9 +883,11 @@ XenVbd_HotPlugHandler(char *Path, PVOID 
  12.143        {
  12.144          RtlStringCbCopyA(DeviceData->BackendPath, 128, Value);
  12.145        }
  12.146 +      ExFreePool(Value);
  12.147        RtlStringCbCopyA(TmpPath, 128, DeviceData->BackendPath);
  12.148        RtlStringCbCatA(TmpPath, 128, "/state");
  12.149 -      XenBusInterface.AddWatch(XBT_NIL, TmpPath, XenVbd_BackEndStateHandler, DeviceData);
  12.150 +      XenBusInterface.AddWatch(XenBusInterface.InterfaceHeader.Context,
  12.151 +        XBT_NIL, TmpPath, XenVbd_BackEndStateHandler, DeviceData);
  12.152      }
  12.153      break;
  12.154    }
  12.155 @@ -873,8 +895,6 @@ XenVbd_HotPlugHandler(char *Path, PVOID 
  12.156    FreeSplitString(Bits, Count);
  12.157  
  12.158    //KdPrint((__DRIVER_NAME " <-- HotPlugHandler\n"));  
  12.159 -
  12.160 -  return;
  12.161  }
  12.162  
  12.163  static NTSTATUS
  12.164 @@ -1108,7 +1128,11 @@ XenVbd_PutIrpOnRing(WDFDEVICE Device, PI
  12.165    sect_offset = MmGetMdlByteOffset(ChildDeviceData->shadow[req->id].Mdl) >> 9;
  12.166    for (i = 0, req->nr_segments = 0; i < BlockCount; req->nr_segments++)
  12.167    {
  12.168 -    req->seg[req->nr_segments].gref = GntTblInterface.GrantAccess(0, MmGetMdlPfnArray(ChildDeviceData->shadow[req->id].Mdl)[req->nr_segments], FALSE);
  12.169 +    req->seg[req->nr_segments].gref = GntTblInterface.GrantAccess(
  12.170 +      GntTblInterface.InterfaceHeader.Context,
  12.171 +      0,
  12.172 +      MmGetMdlPfnArray(ChildDeviceData->shadow[req->id].Mdl)[req->nr_segments],
  12.173 +      FALSE);
  12.174      req->seg[req->nr_segments].first_sect = sect_offset;
  12.175      for (j = sect_offset; i < BlockCount && j < PAGE_SIZE / ChildDeviceData->BytesPerSector; j++, i++)
  12.176        req->seg[req->nr_segments].last_sect = (uint8_t)j;
  12.177 @@ -1324,7 +1348,8 @@ XenVbd_Child_PreprocessWdmIrpSCSI(WDFDEV
  12.178          XenVbd_PutIrpOnRing(Device, Irp);
  12.179          RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&ChildDeviceData->Ring, notify);
  12.180          if (notify)
  12.181 -          EvtChnInterface.Notify(ChildDeviceData->EventChannel);
  12.182 +          EvtChnInterface.Notify(EvtChnInterface.InterfaceHeader.Context,
  12.183 +            ChildDeviceData->EventChannel);
  12.184          //KdPrint((__DRIVER_NAME "     WdmIrpPreprocessSCSI (AddedToList = %d, RemovedFromList = %d, AddedToRing = %d, AddedToRingAtLastNotify = %d, AddedToRingAtLastInterrupt = %d, AddedToRingAtLastDpc = %d, RemovedFromRing = %d, IrpCompleted = %d)\n", ChildDeviceData->IrpAddedToList, ChildDeviceData->IrpRemovedFromList, ChildDeviceData->IrpAddedToRing, ChildDeviceData->IrpAddedToRingAtLastNotify, ChildDeviceData->IrpAddedToRingAtLastInterrupt, ChildDeviceData->IrpAddedToRingAtLastDpc, ChildDeviceData->IrpRemovedFromRing, ChildDeviceData->IrpCompleted));
  12.185        }
  12.186        KeReleaseSpinLock(&ChildDeviceData->Lock, KIrql);
    13.1 --- a/xenvbd/xenvbd.h	Wed Nov 28 14:44:10 2007 -0800
    13.2 +++ b/xenvbd/xenvbd.h	Wed Nov 28 14:51:17 2007 -0800
    13.3 @@ -61,7 +61,7 @@ typedef enum {
    13.4    XENVBD_DEVICETYPE_CDROM
    13.5  } XENVBD_DEVICETYPE;
    13.6  
    13.7 -struct {
    13.8 +typedef struct _XENVBD_CHILD_DEVICE_DATA {
    13.9    LIST_ENTRY Entry;
   13.10    KSPIN_LOCK Lock;
   13.11    WDFQUEUE IoDefaultQueue;
   13.12 @@ -100,7 +100,7 @@ struct {
   13.13    int FastPathUsed;
   13.14    int SlowPathUsed;
   13.15  
   13.16 -} typedef XENVBD_CHILD_DEVICE_DATA, *PXENVBD_CHILD_DEVICE_DATA, **PPXENVBD_CHILD_DEVICE_DATA;
   13.17 +} XENVBD_CHILD_DEVICE_DATA, *PXENVBD_CHILD_DEVICE_DATA;
   13.18  
   13.19  WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(PXENVBD_CHILD_DEVICE_DATA, GetChildDeviceData);
   13.20