win-pvdrivers

view xenpci/xenpci.h @ 537:2a74ac2f43bb

more big updates
dma now working under xp
author James Harper <james.harper@bendigoit.com.au>
date Wed Feb 18 22:18:23 2009 +1100 (2009-02-18)
parents 1d39de3ab8d6
children e75bb8d68370
line source
1 /*
2 PV Drivers for Windows Xen HVM Domains
3 Copyright (C) 2007 James Harper
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
20 #if !defined(_XENPCI_H_)
21 #define _XENPCI_H_
23 #define __attribute__(arg) /* empty */
24 #define EISCONN 127
26 #include <ntifs.h>
27 #include <ntddk.h>
29 #define DDKAPI
30 //#include <wdm.h>
31 #include <wdf.h>
32 #include <initguid.h>
33 #include <wdmguid.h>
34 #include <errno.h>
35 #define NTSTRSAFE_LIB
36 #include <ntstrsafe.h>
38 #define __DRIVER_NAME "XenPCI"
40 #include <xen_windows.h>
41 #include <memory.h>
42 #include <grant_table.h>
43 #include <event_channel.h>
44 #include <hvm/params.h>
45 #include <hvm/hvm_op.h>
46 #include <sched.h>
47 #include <io/xenbus.h>
48 #include <io/xs_wire.h>
50 #include <xen_public.h>
52 //{C828ABE9-14CA-4445-BAA6-82C2376C6518}
53 DEFINE_GUID( GUID_XENPCI_DEVCLASS, 0xC828ABE9, 0x14CA, 0x4445, 0xBA, 0xA6, 0x82, 0xC2, 0x37, 0x6C, 0x65, 0x18);
55 #define XENPCI_POOL_TAG (ULONG) 'XenP'
57 #define NR_RESERVED_ENTRIES 8
58 #define NR_GRANT_FRAMES 32
59 #define NR_GRANT_ENTRIES (NR_GRANT_FRAMES * PAGE_SIZE / sizeof(grant_entry_t))
61 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
63 #define EVT_ACTION_TYPE_EMPTY 0
64 #define EVT_ACTION_TYPE_NORMAL 1
65 #define EVT_ACTION_TYPE_DPC 2
66 #define EVT_ACTION_TYPE_IRQ 3
67 #define EVT_ACTION_TYPE_SUSPEND 4
69 #define XEN_PV_PRODUCT_NUMBER 0x0002
70 #define XEN_PV_PRODUCT_BUILD 0x00000001
72 extern ULONG qemu_filtered;
73 extern ULONG qemu_protocol_version;
75 typedef struct _ev_action_t {
76 PXEN_EVTCHN_SERVICE_ROUTINE ServiceRoutine;
77 PVOID ServiceContext;
78 CHAR description[128];
79 ULONG type; /* EVT_ACTION_TYPE_* */
80 KDPC Dpc;
81 ULONG vector;
82 ULONG count;
83 PVOID xpdd;
84 } ev_action_t;
86 typedef struct _XENBUS_WATCH_RING
87 {
88 char Path[128];
89 char Token[10];
90 } XENBUS_WATCH_RING;
92 typedef struct _XENBUS_WATCH_ENTRY {
93 char Path[128];
94 PXENBUS_WATCH_CALLBACK ServiceRoutine;
95 PVOID ServiceContext;
96 int Count;
97 int Active;
98 //int RemovePending;
99 //int Running;
100 //KEVENT CompleteEvent;
101 } XENBUS_WATCH_ENTRY, *PXENBUS_WATCH_ENTRY;
103 #define NR_EVENTS 1024
104 #define WATCH_RING_SIZE 128
105 #define NR_XB_REQS 32
106 #define MAX_WATCH_ENTRIES 128
108 #define CHILD_STATE_EMPTY 0
109 #define CHILD_STATE_DELETED 1
110 #define CHILD_STATE_ADDED 2
112 #define SUSPEND_STATE_NONE 0 /* no suspend in progress */
113 #define SUSPEND_STATE_SCHEDULED 1 /* suspend scheduled */
114 #define SUSPEND_STATE_HIGH_IRQL 2 /* all processors are at high IRQL and spinning */
115 #define SUSPEND_STATE_RESUMING 3 /* we are the other side of the suspend and things are starting to get back to normal */
117 typedef struct {
118 //XENPCI_COMMON common;
120 BOOLEAN XenBus_ShuttingDown;
122 BOOLEAN tpr_patched;
124 WDFINTERRUPT interrupt;
125 ULONG irq_number;
126 ULONG irq_vector;
127 KIRQL irq_level;
128 KINTERRUPT_MODE irq_mode;
129 KAFFINITY irq_affinity;
131 PHYSICAL_ADDRESS shared_info_area_unmapped;
132 shared_info_t *shared_info_area;
133 xen_ulong_t evtchn_pending_pvt[sizeof(xen_ulong_t) * 8];
134 xen_ulong_t evtchn_pending_suspend[sizeof(xen_ulong_t) * 8];
135 evtchn_port_t pdo_event_channel;
136 KEVENT pdo_suspend_event;
137 BOOLEAN interrupts_masked;
139 PHYSICAL_ADDRESS platform_mmio_addr;
140 ULONG platform_mmio_orig_len;
141 ULONG platform_mmio_len;
142 ULONG platform_mmio_alloc;
143 USHORT platform_mmio_flags;
145 ULONG platform_ioport_addr;
146 ULONG platform_ioport_len;
148 char *hypercall_stubs;
150 evtchn_port_t xen_store_evtchn;
152 /* grant related */
153 grant_entry_t *gnttab_table;
154 grant_entry_t *gnttab_table_copy;
155 PHYSICAL_ADDRESS gnttab_table_physical;
156 grant_ref_t *gnttab_list;
157 int gnttab_list_free;
158 KSPIN_LOCK grant_lock;
159 ULONG grant_frames;
161 ev_action_t ev_actions[NR_EVENTS];
162 // unsigned long bound_ports[NR_EVENTS/(8*sizeof(unsigned long))];
164 PKTHREAD XenBus_ReadThread;
165 KEVENT XenBus_ReadThreadEvent;
166 PKTHREAD XenBus_WatchThread;
167 KEVENT XenBus_WatchThreadEvent;
169 XENBUS_WATCH_RING XenBus_WatchRing[WATCH_RING_SIZE];
170 int XenBus_WatchRingReadIndex;
171 int XenBus_WatchRingWriteIndex;
173 struct xenstore_domain_interface *xen_store_interface;
175 /* xenbus related */
176 XENBUS_WATCH_ENTRY XenBus_WatchEntries[MAX_WATCH_ENTRIES];
177 FAST_MUTEX xb_watch_mutex;
178 FAST_MUTEX xb_request_mutex;
179 KEVENT xb_request_complete_event;
180 struct xsd_sockmsg *xb_reply;
182 WDFCHILDLIST child_list;
184 int suspend_state;
186 UNICODE_STRING legacy_interface_name;
187 UNICODE_STRING interface_name;
188 BOOLEAN interface_open;
190 BOOLEAN removable;
192 WDFQUEUE io_queue;
194 #if 0
195 KSPIN_LOCK mmio_freelist_lock;
196 PPFN_NUMBER mmio_freelist_base;
197 ULONG mmio_freelist_free;
198 #endif
200 } XENPCI_DEVICE_DATA, *PXENPCI_DEVICE_DATA;
202 WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(XENPCI_DEVICE_DATA, GetXpdd)
204 typedef struct {
205 // XENPCI_COMMON common;
206 WDFDEVICE wdf_device;
207 WDFDEVICE wdf_device_bus_fdo;
208 //PDEVICE_OBJECT bus_pdo;
209 //PDEVICE_OBJECT bus_fdo;
210 BOOLEAN reported_missing;
211 char path[128];
212 char device[128];
213 ULONG index;
214 ULONG irq_number;
215 ULONG irq_vector;
216 KIRQL irq_level;
217 char backend_path[128];
218 //PVOID xenbus_request;
219 KEVENT backend_state_event;
220 ULONG backend_state;
221 PHYSICAL_ADDRESS config_page_phys;
222 ULONG config_page_length;
223 PUCHAR requested_resources_start;
224 PUCHAR requested_resources_ptr;
225 PUCHAR assigned_resources_start;
226 PUCHAR assigned_resources_ptr;
227 XENPCI_DEVICE_STATE device_state;
228 BOOLEAN restart_on_resume;
230 } XENPCI_PDO_DEVICE_DATA, *PXENPCI_PDO_DEVICE_DATA;
232 WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(XENPCI_PDO_DEVICE_DATA, GetXppdd)
234 typedef struct {
235 WDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER header;
236 CHAR path[128];
237 CHAR device[128];
238 ULONG index;
239 } XENPCI_PDO_IDENTIFICATION_DESCRIPTION, *PXENPCI_PDO_IDENTIFICATION_DESCRIPTION;
241 typedef struct {
242 DMA_ADAPTER dma_adapter;
243 PXENPCI_PDO_DEVICE_DATA xppdd;
244 dma_driver_extension_t *dma_extension;
245 PDRIVER_OBJECT dma_extension_driver; /* to deference it */
246 } xen_dma_adapter_t;
248 #if 0
249 typedef struct
250 {
251 LIST_ENTRY entry;
252 int state;
253 PXENPCI_PDO_DEVICE_DATA context;
254 } XEN_CHILD, *PXEN_CHILD;
255 #endif
257 #define XEN_INTERFACE_VERSION 1
259 #define DEVICE_INTERFACE_TYPE_LEGACY 0
260 #define DEVICE_INTERFACE_TYPE_XENBUS 1
262 typedef struct {
263 ULONG type; /* must be the first member */
264 KSPIN_LOCK lock;
265 ULONG len;
266 union {
267 struct xsd_sockmsg msg;
268 UCHAR buffer[PAGE_SIZE];
269 } u;
270 LIST_ENTRY read_list_head;
271 LIST_ENTRY watch_list_head;
272 WDFQUEUE io_queue;
274 //PIRP pending_read_irp;
275 } XENPCI_DEVICE_INTERFACE_DATA, *PXENPCI_DEVICE_INTERFACE_DATA;
277 WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(XENPCI_DEVICE_INTERFACE_DATA, GetXpdid)
279 EVT_WDF_DEVICE_FILE_CREATE XenPci_EvtDeviceFileCreate;
280 EVT_WDF_FILE_CLOSE XenPci_EvtFileClose;
281 EVT_WDF_FILE_CLEANUP XenPci_EvtFileCleanup;
282 EVT_WDF_IO_QUEUE_IO_READ XenPci_EvtIoRead;
283 EVT_WDF_IO_QUEUE_IO_WRITE XenPci_EvtIoWrite;
285 #include "hypercall.h"
287 #define XBT_NIL ((xenbus_transaction_t)0)
289 NTSTATUS
290 hvm_get_stubs(PXENPCI_DEVICE_DATA xpdd);
291 NTSTATUS
292 hvm_free_stubs(PXENPCI_DEVICE_DATA xpdd);
294 EVT_WDF_DEVICE_PREPARE_HARDWARE XenPci_EvtDevicePrepareHardware;
295 EVT_WDF_DEVICE_RELEASE_HARDWARE XenPci_EvtDeviceReleaseHardware;
296 EVT_WDF_DEVICE_D0_ENTRY XenPci_EvtDeviceD0Entry;
297 EVT_WDF_DEVICE_D0_ENTRY_POST_INTERRUPTS_ENABLED XenPci_EvtDeviceD0EntryPostInterruptsEnabled;
298 EVT_WDF_DEVICE_D0_EXIT XenPci_EvtDeviceD0Exit;
299 EVT_WDF_DEVICE_D0_EXIT_PRE_INTERRUPTS_DISABLED XenPci_EvtDeviceD0ExitPreInterruptsDisabled;
300 EVT_WDF_DEVICE_QUERY_REMOVE XenPci_EvtDeviceQueryRemove;
302 NTSTATUS
303 XenPci_EvtChildListCreateDevice(WDFCHILDLIST child_list, PWDF_CHILD_IDENTIFICATION_DESCRIPTION_HEADER description_header, PWDFDEVICE_INIT child_init);
304 VOID
305 XenPci_EvtChildListScanForChildren(WDFCHILDLIST child_list);
307 VOID
308 XenPci_HideQemuDevices();
309 extern ULONG qemu_filtered_by_qemu;
311 #if 0
312 NTSTATUS
313 XenPci_Power_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
314 NTSTATUS
315 XenPci_Dummy_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
316 NTSTATUS
317 XenPci_Pnp_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
318 NTSTATUS
319 XenPci_Irp_Create_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
320 NTSTATUS
321 XenPci_Irp_Close_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
322 NTSTATUS
323 XenPci_Irp_Read_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
324 NTSTATUS
325 XenPci_Irp_Write_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
326 NTSTATUS
327 XenPci_Irp_Cleanup_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
328 NTSTATUS
329 XenPci_SystemControl_Fdo(PDEVICE_OBJECT device_object, PIRP irp);
331 NTSTATUS
332 XenPci_Irp_Create_XenBus(PDEVICE_OBJECT device_object, PIRP irp);
333 NTSTATUS
334 XenPci_Irp_Close_XenBus(PDEVICE_OBJECT device_object, PIRP irp);
335 NTSTATUS
336 XenPci_Irp_Read_XenBus(PDEVICE_OBJECT device_object, PIRP irp);
337 NTSTATUS
338 XenPci_Irp_Write_XenBus(PDEVICE_OBJECT device_object, PIRP irp);
339 NTSTATUS
340 XenPci_Irp_Cleanup_XenBus(PDEVICE_OBJECT device_object, PIRP irp);
342 NTSTATUS
343 XenPci_Power_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
344 //NTSTATUS
345 //XenPci_Dummy_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
346 NTSTATUS
347 XenPci_Pnp_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
348 NTSTATUS
349 XenPci_Irp_Create_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
350 NTSTATUS
351 XenPci_Irp_Close_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
352 NTSTATUS
353 XenPci_Irp_Read_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
354 NTSTATUS
355 XenPci_Irp_Write_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
356 NTSTATUS
357 XenPci_Irp_Cleanup_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
358 NTSTATUS
359 XenPci_SystemControl_Pdo(PDEVICE_OBJECT device_object, PIRP irp);
360 #endif
362 NTSTATUS
363 XenPci_Pdo_Suspend(WDFDEVICE device);
364 NTSTATUS
365 XenPci_Pdo_Resume(WDFDEVICE device);
367 VOID
368 XenPci_DumpPdoConfig(PDEVICE_OBJECT device_object);
370 typedef VOID
371 (*PXENPCI_HIGHSYNC_FUNCTION)(PVOID context);
373 VOID
374 XenPci_HighSync(PXENPCI_HIGHSYNC_FUNCTION function0, PXENPCI_HIGHSYNC_FUNCTION functionN, PVOID context);
376 VOID
377 XenPci_PatchKernel(PXENPCI_DEVICE_DATA xpdd, PVOID base, ULONG length);
379 struct xsd_sockmsg *
380 XenBus_Raw(PXENPCI_DEVICE_DATA xpdd, struct xsd_sockmsg *msg);
381 char *
382 XenBus_Read(PVOID Context, xenbus_transaction_t xbt, char *path, char **value);
383 char *
384 XenBus_Write(PVOID Context, xenbus_transaction_t xbt, char *path, char *value);
385 char *
386 XenBus_Printf(PVOID Context, xenbus_transaction_t xbt, char *path, char *fmt, ...);
387 char *
388 XenBus_StartTransaction(PVOID Context, xenbus_transaction_t *xbt);
389 char *
390 XenBus_EndTransaction(PVOID Context, xenbus_transaction_t t, int abort, int *retry);
391 char *
392 XenBus_List(PVOID Context, xenbus_transaction_t xbt, char *prefix, char ***contents);
393 char *
394 XenBus_AddWatch(PVOID Context, xenbus_transaction_t xbt, char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext);
395 char *
396 XenBus_RemWatch(PVOID Context, xenbus_transaction_t xbt, char *Path, PXENBUS_WATCH_CALLBACK ServiceRoutine, PVOID ServiceContext);
397 //VOID
398 //XenBus_ThreadProc(PVOID StartContext);
399 NTSTATUS
400 XenBus_Init(PXENPCI_DEVICE_DATA xpdd);
401 NTSTATUS
402 XenBus_Close(PXENPCI_DEVICE_DATA xpdd);
403 NTSTATUS
404 XenBus_Start(PXENPCI_DEVICE_DATA xpdd);
405 NTSTATUS
406 XenBus_Stop(PXENPCI_DEVICE_DATA xpdd);
407 NTSTATUS
408 XenBus_Suspend(PXENPCI_DEVICE_DATA xpdd);
409 NTSTATUS
410 XenBus_Resume(PXENPCI_DEVICE_DATA xpdd);
411 NTSTATUS
412 XenBus_StopThreads(PXENPCI_DEVICE_DATA xpdd);
414 PHYSICAL_ADDRESS
415 XenPci_AllocMMIO(PXENPCI_DEVICE_DATA xpdd, ULONG len);
417 EVT_WDF_INTERRUPT_ISR EvtChn_EvtInterruptIsr;
418 EVT_WDF_INTERRUPT_ENABLE EvtChn_EvtInterruptEnable;
419 EVT_WDF_INTERRUPT_DISABLE EvtChn_EvtInterruptDisable;
421 NTSTATUS
422 EvtChn_Init(PXENPCI_DEVICE_DATA xpdd);
423 NTSTATUS
424 EvtChn_Suspend(PXENPCI_DEVICE_DATA xpdd);
425 NTSTATUS
426 EvtChn_Resume(PXENPCI_DEVICE_DATA xpdd);
428 NTSTATUS
429 EvtChn_Mask(PVOID Context, evtchn_port_t Port);
430 NTSTATUS
431 EvtChn_Unmask(PVOID Context, evtchn_port_t Port);
432 NTSTATUS
433 EvtChn_Bind(PVOID Context, evtchn_port_t Port, PXEN_EVTCHN_SERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext);
434 NTSTATUS
435 EvtChn_BindDpc(PVOID Context, evtchn_port_t Port, PXEN_EVTCHN_SERVICE_ROUTINE ServiceRoutine, PVOID ServiceContext);
436 NTSTATUS
437 EvtChn_BindIrq(PVOID Context, evtchn_port_t Port, ULONG vector, PCHAR description);
438 evtchn_port_t
439 EvtChn_AllocIpi(PVOID context, ULONG vcpu);
440 NTSTATUS
441 EvtChn_Unbind(PVOID Context, evtchn_port_t Port);
442 NTSTATUS
443 EvtChn_Notify(PVOID Context, evtchn_port_t Port);
444 VOID
445 EvtChn_Close(PVOID Context, evtchn_port_t Port);
446 evtchn_port_t
447 EvtChn_AllocUnbound(PVOID Context, domid_t Domain);
448 BOOLEAN
449 EvtChn_AckEvent(PVOID context, evtchn_port_t port);
451 VOID
452 GntTbl_Init(PXENPCI_DEVICE_DATA xpdd);
453 VOID
454 GntTbl_Suspend(PXENPCI_DEVICE_DATA xpdd);
455 VOID
456 GntTbl_Resume(PXENPCI_DEVICE_DATA xpdd);
457 grant_ref_t
458 GntTbl_GrantAccess(PVOID Context, domid_t domid, uint32_t, int readonly, grant_ref_t ref);
459 BOOLEAN
460 GntTbl_EndAccess(PVOID Context, grant_ref_t ref, BOOLEAN keepref);
461 VOID
462 GntTbl_PutRef(PVOID Context, grant_ref_t ref);
463 grant_ref_t
464 GntTbl_GetRef(PVOID Context);
465 #if 0
466 int
467 GntTbl_Map(PVOID Context, unsigned int start_idx, unsigned int end_idx);
468 #endif
470 #endif