win-pvdrivers

annotate xenpci/xenpci.h @ 272:909b775a891f

Fixed a few bugs in shutdown handler. Drivers appear to be working now. Shutdown not so much yet...
author James Harper <james.harper@bendigoit.com.au>
date Fri May 16 23:56:57 2008 +1000 (2008-05-16)
parents da9b1e17fbc0
children 874c3640830e
rev   line source
james@0 1 /*
james@0 2 PV Drivers for Windows Xen HVM Domains
james@0 3 Copyright (C) 2007 James Harper
james@0 4
james@0 5 This program is free software; you can redistribute it and/or
james@0 6 modify it under the terms of the GNU General Public License
james@0 7 as published by the Free Software Foundation; either version 2
james@0 8 of the License, or (at your option) any later version.
james@0 9
james@0 10 This program is distributed in the hope that it will be useful,
james@0 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
james@0 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
james@0 13 GNU General Public License for more details.
james@0 14
james@0 15 You should have received a copy of the GNU General Public License
james@0 16 along with this program; if not, write to the Free Software
james@0 17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
james@0 18 */
james@0 19
james@0 20 #if !defined(_XENPCI_H_)
james@0 21 #define _XENPCI_H_
james@0 22
james@0 23 #define __attribute__(arg) /* empty */
james@0 24 #define EISCONN 127
james@0 25
james@0 26 #include <ntddk.h>
james@0 27 #include <wdm.h>
james@258 28 //#include <wdf.h>
james@0 29 #include <initguid.h>
james@0 30 #include <wdmguid.h>
james@0 31 #include <errno.h>
james@0 32
james@0 33 #define NTSTRSAFE_LIB
james@0 34 #include <ntstrsafe.h>
james@0 35
james@74 36 #define __DRIVER_NAME "XenPCI"
james@74 37
james@0 38 #include <xen_windows.h>
james@0 39 #include <memory.h>
james@0 40 #include <grant_table.h>
james@0 41 #include <event_channel.h>
james@0 42 #include <hvm/params.h>
james@0 43 #include <hvm/hvm_op.h>
james@198 44 #include <sched.h>
james@267 45 #include <io/xenbus.h>
james@0 46
andy@15 47 #include <xen_public.h>
andy@15 48
james@0 49 //{C828ABE9-14CA-4445-BAA6-82C2376C6518}
james@0 50 DEFINE_GUID( GUID_XENPCI_DEVCLASS, 0xC828ABE9, 0x14CA, 0x4445, 0xBA, 0xA6, 0x82, 0xC2, 0x37, 0x6C, 0x65, 0x18);
james@0 51
james@0 52 #define XENPCI_POOL_TAG (ULONG) 'XenP'
james@0 53
james@0 54 #define NR_RESERVED_ENTRIES 8
james@0 55 #define NR_GRANT_FRAMES 4
james@0 56 #define NR_GRANT_ENTRIES (NR_GRANT_FRAMES * PAGE_SIZE / sizeof(grant_entry_t))
james@0 57
james@0 58 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
james@0 59
james@267 60 #define EVT_ACTION_TYPE_EMPTY 0
james@267 61 #define EVT_ACTION_TYPE_NORMAL 1
james@267 62 #define EVT_ACTION_TYPE_DPC 2
james@267 63 #define EVT_ACTION_TYPE_IRQ 3
james@267 64
andy@13 65 typedef struct _ev_action_t {
andy@13 66 PKSERVICE_ROUTINE ServiceRoutine;
andy@13 67 PVOID ServiceContext;
james@267 68 ULONG type; /* EVT_ACTION_TYPE_* */
james@258 69 KDPC Dpc;
james@267 70 ULONG vector;
andy@13 71 ULONG Count;
andy@13 72 } ev_action_t;
james@0 73
andy@15 74 typedef struct _XENBUS_WATCH_RING
andy@15 75 {
andy@15 76 char Path[128];
andy@15 77 char Token[10];
andy@15 78 } XENBUS_WATCH_RING;
james@0 79
andy@15 80 typedef struct _XENBUS_REQ_INFO
andy@15 81 {
andy@15 82 int In_Use:1;
andy@15 83 KEVENT WaitEvent;
andy@15 84 void *Reply;
andy@15 85 } XENBUS_REQ_INFO;
james@0 86
andy@15 87 typedef struct _XENBUS_WATCH_ENTRY {
andy@15 88 char Path[128];
andy@15 89 PXENBUS_WATCH_CALLBACK ServiceRoutine;
andy@15 90 PVOID ServiceContext;
andy@15 91 int Count;
andy@15 92 int Active;
james@138 93 int RemovePending;
james@128 94 int Running;
james@128 95 KEVENT CompleteEvent;
andy@15 96 } XENBUS_WATCH_ENTRY, *PXENBUS_WATCH_ENTRY;
james@0 97
andy@13 98 #define NR_EVENTS 1024
andy@15 99 #define WATCH_RING_SIZE 128
andy@15 100 #define NR_XB_REQS 32
andy@15 101 #define MAX_WATCH_ENTRIES 128
james@0 102
james@259 103 #define CHILD_STATE_EMPTY 0
james@259 104 #define CHILD_STATE_DELETED 1
james@259 105 #define CHILD_STATE_ADDED 2
james@259 106
james@259 107 // TODO: tidy up & organize this struct
james@258 108
james@258 109 typedef struct
james@258 110 {
james@258 111 PDEVICE_OBJECT fdo;
james@258 112 PDEVICE_OBJECT pdo;
james@258 113 PDEVICE_OBJECT lower_do;
james@259 114 } XENPCI_COMMON, *PXENPCI_COMMON;
james@259 115
james@272 116 #define SHUTDOWN_RING_SIZE 128
james@272 117
james@259 118 typedef struct {
james@259 119 XENPCI_COMMON common;
james@259 120
andy@38 121 BOOLEAN XenBus_ShuttingDown;
james@0 122
james@258 123 PKINTERRUPT interrupt;
james@258 124 ULONG irq_number;
james@258 125 ULONG irq_vector;
james@258 126 KIRQL irq_level;
james@258 127 KAFFINITY irq_affinity;
james@0 128
andy@13 129 shared_info_t *shared_info_area;
james@0 130
andy@13 131 PHYSICAL_ADDRESS platform_mmio_addr;
andy@13 132 ULONG platform_mmio_orig_len;
andy@13 133 ULONG platform_mmio_len;
andy@13 134 ULONG platform_mmio_alloc;
james@267 135 USHORT platform_mmio_flags;
andy@13 136
andy@13 137 char *hypercall_stubs;
andy@13 138
andy@13 139 evtchn_port_t xen_store_evtchn;
andy@13 140
andy@13 141 grant_entry_t *gnttab_table;
andy@13 142 PHYSICAL_ADDRESS gnttab_table_physical;
andy@13 143 grant_ref_t gnttab_list[NR_GRANT_ENTRIES];
andy@13 144
andy@13 145 ev_action_t ev_actions[NR_EVENTS];
james@258 146 // unsigned long bound_ports[NR_EVENTS/(8*sizeof(unsigned long))];
james@0 147
andy@15 148 HANDLE XenBus_ReadThreadHandle;
andy@15 149 KEVENT XenBus_ReadThreadEvent;
andy@15 150 HANDLE XenBus_WatchThreadHandle;
andy@15 151 KEVENT XenBus_WatchThreadEvent;
andy@15 152
andy@15 153 XENBUS_WATCH_RING XenBus_WatchRing[WATCH_RING_SIZE];
andy@15 154 int XenBus_WatchRingReadIndex;
andy@15 155 int XenBus_WatchRingWriteIndex;
andy@15 156
andy@15 157 struct xenstore_domain_interface *xen_store_interface;
andy@15 158
andy@15 159 XENBUS_REQ_INFO req_info[NR_XB_REQS];
andy@15 160 int nr_live_reqs;
andy@15 161 XENBUS_WATCH_ENTRY XenBus_WatchEntries[MAX_WATCH_ENTRIES];
james@0 162
james@128 163 KSPIN_LOCK WatchLock;
james@176 164 KSPIN_LOCK grant_lock;
james@191 165
james@191 166 KGUARDED_MUTEX WatchHandlerMutex;
james@198 167
james@259 168 LIST_ENTRY child_list;
james@258 169
james@198 170 int suspending;
james@271 171
james@271 172 UNICODE_STRING interface_name;
james@272 173 BOOLEAN interface_open;
james@272 174
james@272 175 KSPIN_LOCK shutdown_ring_lock;
james@272 176 CHAR shutdown_ring[SHUTDOWN_RING_SIZE];
james@272 177 ULONG shutdown_prod;
james@272 178 ULONG shutdown_cons;
james@272 179 ULONG shutdown_start; /* the start of the most recent message on the ring */
james@272 180 PIRP shutdown_irp;
james@0 181 } XENPCI_DEVICE_DATA, *PXENPCI_DEVICE_DATA;
james@0 182
james@259 183 typedef struct {
james@259 184 XENPCI_COMMON common;
james@261 185 PDEVICE_OBJECT bus_fdo;
james@259 186 char path[128];
james@267 187 char device[128];
james@259 188 ULONG index;
james@267 189 //PHYSICAL_ADDRESS mmio_phys;
james@267 190 ULONG irq_vector;
james@267 191 KIRQL irq_level;
james@267 192 char backend_path[128];
james@267 193 KEVENT backend_state_event;
james@267 194 ULONG backend_state;
james@259 195 } XENPCI_PDO_DEVICE_DATA, *PXENPCI_PDO_DEVICE_DATA;
james@0 196
james@259 197 typedef struct
james@259 198 {
james@259 199 LIST_ENTRY entry;
james@259 200 int state;
james@259 201 PXENPCI_PDO_DEVICE_DATA context;
james@259 202 } XEN_CHILD, *PXEN_CHILD;
james@267 203
james@267 204 #define SWINT(x) case x: __asm { int x } break;
james@267 205
james@267 206 static __inline VOID
james@267 207 sw_interrupt(UCHAR intno)
james@267 208 {
james@269 209 //KdPrint((__DRIVER_NAME " Calling interrupt %02X\n", intno));
james@267 210 switch (intno)
james@267 211 {
james@267 212 SWINT(0x10) SWINT(0x11) SWINT(0x12) SWINT(0x13) SWINT(0x14) SWINT(0x15) SWINT(0x16) SWINT(0x17)
james@267 213 SWINT(0x18) SWINT(0x19) SWINT(0x1A) SWINT(0x1B) SWINT(0x1C) SWINT(0x1D) SWINT(0x1E) SWINT(0x1F)
james@267 214 SWINT(0x20) SWINT(0x21) SWINT(0x22) SWINT(0x23) SWINT(0x24) SWINT(0x25) SWINT(0x26) SWINT(0x27)
james@267 215 SWINT(0x28) SWINT(0x29) SWINT(0x2A) SWINT(0x2B) SWINT(0x2C) SWINT(0x2D) SWINT(0x2E) SWINT(0x2F)
james@267 216 SWINT(0x30) SWINT(0x31) SWINT(0x32) SWINT(0x33) SWINT(0x34) SWINT(0x35) SWINT(0x36) SWINT(0x37)
james@267 217 SWINT(0x38) SWINT(0x39) SWINT(0x3A) SWINT(0x3B) SWINT(0x3C) SWINT(0x3D) SWINT(0x3E) SWINT(0x3F)
james@267 218 SWINT(0x40) SWINT(0x41) SWINT(0x42) SWINT(0x43) SWINT(0x44) SWINT(0x45) SWINT(0x46) SWINT(0x47)
james@267 219 SWINT(0x48) SWINT(0x49) SWINT(0x4A) SWINT(0x4B) SWINT(0x4C) SWINT(0x4D) SWINT(0x4E) SWINT(0x4F)
james@267 220
james@267 221 SWINT(0x80) SWINT(0x81) SWINT(0x82) SWINT(0x83) SWINT(0x84) SWINT(0x85) SWINT(0x86) SWINT(0x87)
james@267 222 SWINT(0x88) SWINT(0x89) SWINT(0x8A) SWINT(0x8B) SWINT(0x8C) SWINT(0x8D) SWINT(0x8E) SWINT(0x8F)
james@267 223
james@267 224 SWINT(0xB0) SWINT(0xB1) SWINT(0xB2) SWINT(0xB3) SWINT(0xB4) SWINT(0xB5) SWINT(0xB6) SWINT(0xB7)
james@267 225 SWINT(0xB8) SWINT(0xB9) SWINT(0xBA) SWINT(0xBB) SWINT(0xBC) SWINT(0xBD) SWINT(0xBE) SWINT(0xBF)
james@267 226
james@267 227 default:
james@267 228 KdPrint((__DRIVER_NAME " interrupt %02X not set up. Blame James.\n", intno));
james@267 229 KeBugCheckEx(('X' << 16)|('E' << 8)|('N'), 0x00000002, (ULONG)intno, 0x00000000, 0x00000000);
james@267 230 break;
james@267 231 }
james@267 232 }
james@259 233
james@258 234 #include "hypercall.h"
james@124 235
james@0 236 typedef unsigned long xenbus_transaction_t;
james@0 237 typedef uint32_t XENSTORE_RING_IDX;
james@0 238
james@0 239 #define XBT_NIL ((xenbus_transaction_t)0)
james@0 240
james@267 241 static __inline VOID
james@267 242 XenPci_FreeMem(PVOID Ptr)
james@267 243 {
james@267 244 ExFreePoolWithTag(Ptr, XENPCI_POOL_TAG);
james@267 245 }
james@267 246
james@259 247 NTSTATUS
james@259 248 XenPci_Power_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
james@259 249 NTSTATUS
james@259 250 XenPci_Dummy_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
james@259 251 NTSTATUS
james@259 252 XenPci_Pnp_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
james@272 253 NTSTATUS
james@272 254 XenPci_Irp_Create_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
james@272 255 NTSTATUS
james@272 256 XenPci_Irp_Close_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
james@272 257 NTSTATUS
james@272 258 XenPci_Irp_Read_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
james@272 259 NTSTATUS
james@272 260 XenPci_Irp_Cleanup_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
james@259 261
james@259 262 NTSTATUS
james@259 263 XenPci_Power_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
james@272 264 //NTSTATUS
james@272 265 //XenPci_Dummy_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
james@259 266 NTSTATUS
james@259 267 XenPci_Pnp_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
james@272 268 NTSTATUS
james@272 269 XenPci_Irp_Create_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
james@272 270 NTSTATUS
james@272 271 XenPci_Irp_Close_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
james@272 272 NTSTATUS
james@272 273 XenPci_Irp_Read_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
james@272 274 NTSTATUS
james@272 275 XenPci_Irp_Cleanup_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
james@259 276
james@259 277
james@259 278
james@0 279 char *
andy@13 280 XenBus_Read(PVOID Context, xenbus_transaction_t xbt, const char *path, char **value);
james@0 281 char *
andy@13 282 XenBus_Write(PVOID Context, xenbus_transaction_t xbt, const char *path, const char *value);
james@0 283 char *
andy@13 284 XenBus_Printf(PVOID Context, xenbus_transaction_t xbt, const char *path, const char *fmt, ...);
james@0 285 char *
andy@13 286 XenBus_StartTransaction(PVOID Context, xenbus_transaction_t *xbt);
james@0 287 char *
andy@13 288 XenBus_EndTransaction(PVOID Context, xenbus_transaction_t t, int abort, int *retry);
andy@13 289 char *
andy@13 290 XenBus_List(PVOID Context, xenbus_transaction_t xbt, const char *prefix, char ***contents);
james@0 291 char *
andy@13 292 XenBus_AddWatch(PVOID Context, xenbus_transaction_t xbt, const char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext);
james@0 293 char *
andy@13 294 XenBus_RemWatch(PVOID Context, xenbus_transaction_t xbt, const char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext);
james@0 295 VOID
james@0 296 XenBus_ThreadProc(PVOID StartContext);
james@258 297 NTSTATUS
james@258 298 XenBus_Init(PXENPCI_DEVICE_DATA xpdd);
james@258 299 NTSTATUS
james@258 300 XenBus_Close(PXENPCI_DEVICE_DATA xpdd);
james@258 301 NTSTATUS
james@258 302 XenBus_Start(PXENPCI_DEVICE_DATA xpdd);
james@258 303 NTSTATUS
james@258 304 XenBus_Stop(PXENPCI_DEVICE_DATA xpdd);
james@0 305
james@0 306 PHYSICAL_ADDRESS
james@258 307 XenPci_AllocMMIO(PXENPCI_DEVICE_DATA xpdd, ULONG len);
james@0 308
andy@15 309 NTSTATUS
james@258 310 EvtChn_Init(PXENPCI_DEVICE_DATA xpdd);
james@258 311 NTSTATUS
james@258 312 EvtChn_Shutdown(PXENPCI_DEVICE_DATA xpdd);
james@258 313
james@0 314 NTSTATUS
andy@13 315 EvtChn_Mask(PVOID Context, evtchn_port_t Port);
james@0 316 NTSTATUS
andy@13 317 EvtChn_Unmask(PVOID Context, evtchn_port_t Port);
james@0 318 NTSTATUS
andy@13 319 EvtChn_Bind(PVOID Context, evtchn_port_t Port, PKSERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext);
james@0 320 NTSTATUS
james@73 321 EvtChn_BindDpc(PVOID Context, evtchn_port_t Port, PKSERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext);
james@73 322 NTSTATUS
james@267 323 EvtChn_BindIrq(PVOID Context, evtchn_port_t Port, ULONG vector);
james@267 324 NTSTATUS
andy@13 325 EvtChn_Unbind(PVOID Context, evtchn_port_t Port);
andy@13 326 NTSTATUS
andy@13 327 EvtChn_Notify(PVOID Context, evtchn_port_t Port);
james@0 328 evtchn_port_t
andy@13 329 EvtChn_AllocUnbound(PVOID Context, domid_t Domain);
james@0 330
andy@15 331 VOID
james@258 332 GntTbl_Init(PXENPCI_DEVICE_DATA xpdd);
james@258 333
james@0 334 grant_ref_t
james@258 335 GntTbl_GrantAccess(PVOID Context, domid_t domid, uint32_t, int readonly, grant_ref_t ref);
james@0 336 BOOLEAN
james@258 337 GntTbl_EndAccess(PVOID Context, grant_ref_t ref, BOOLEAN keepref);
james@206 338 VOID
james@258 339 GntTbl_PutRef(PVOID Context, grant_ref_t ref);
james@206 340 grant_ref_t
james@258 341 GntTbl_GetRef(PVOID Context);
james@0 342
james@0 343 #endif