win-pvdrivers

view xenvbd/xenvbd.c @ 310:60372bd2582d

First cut of putting xenbus config details in the .inf file - xenvbd may yet pass WHQL
author James Harper <james.harper@bendigoit.com.au>
date Fri Jun 13 14:16:50 2008 +1000 (2008-06-13)
parents b4f7d75fbe24
children 0488ef11be09
line source
1 /*
2 PV Drivers for Windows Xen HVM Domains
3 Copyright (C) 2007 James Harper
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
20 #include "xenvbd.h"
21 #include <io/blkif.h>
22 #include <scsi.h>
23 #include <ntddscsi.h>
24 #include <ntdddisk.h>
25 #include <stdlib.h>
26 #include <xen_public.h>
27 #include <io/xenbus.h>
28 #include <io/protocols.h>
30 #pragma warning(disable: 4127)
32 DRIVER_INITIALIZE DriverEntry;
34 #ifdef ALLOC_PRAGMA
35 #pragma alloc_text (INIT, DriverEntry)
36 #endif
38 #if 0
39 static PDRIVER_DISPATCH XenVbd_Pnp_Original;
41 static NTSTATUS
42 XenVbd_Pnp(PDEVICE_OBJECT device_object, PIRP irp)
43 {
44 PIO_STACK_LOCATION stack;
45 NTSTATUS status;
46 PCM_RESOURCE_LIST old_crl, new_crl;
47 ULONG i;
48 PCM_PARTIAL_RESOURCE_LIST prl;
49 PCM_PARTIAL_RESOURCE_DESCRIPTOR prd;
50 ULONG old_length, new_length;
51 PMDL mdl;
52 PUCHAR start, ptr;
54 KdPrint((__DRIVER_NAME " --> " __FUNCTION__ "\n"));
56 stack = IoGetCurrentIrpStackLocation(irp);
58 // check if the Irp is meant for us... maybe the stack->DeviceObject field?
60 switch (stack->MinorFunction)
61 {
62 case IRP_MN_START_DEVICE:
63 KdPrint((__DRIVER_NAME " IRP_MN_START_DEVICE - DeviceObject = %p\n", stack->DeviceObject));
64 old_crl = stack->Parameters.StartDevice.AllocatedResourcesTranslated;
65 if (old_crl != NULL)
66 {
67 mdl = AllocateUncachedPage();
68 old_length = FIELD_OFFSET(CM_RESOURCE_LIST, List) +
69 FIELD_OFFSET(CM_FULL_RESOURCE_DESCRIPTOR, PartialResourceList) +
70 FIELD_OFFSET(CM_PARTIAL_RESOURCE_LIST, PartialDescriptors) +
71 sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) * old_crl->List[0].PartialResourceList.Count;
72 new_length = old_length + sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) * 1;
73 new_crl = ExAllocatePoolWithTag(PagedPool, new_length, XENVBD_POOL_TAG);
74 memcpy(new_crl, old_crl, old_length);
75 prl = &new_crl->List[0].PartialResourceList;
76 prd = &prl->PartialDescriptors[prl->Count++];
77 prd->Type = CmResourceTypeMemory;
78 prd->ShareDisposition = CmResourceShareDeviceExclusive;
79 prd->Flags = CM_RESOURCE_MEMORY_READ_WRITE; //|CM_RESOURCE_MEMORY_PREFETCHABLE; //|CM_RESOURCE_MEMORY_CACHEABLE;
80 KdPrint((__DRIVER_NAME " PFN[0] = %p\n", MmGetMdlPfnArray(mdl)[0]));
81 prd->u.Memory.Start.QuadPart = MmGetMdlPfnArray(mdl)[0] << PAGE_SHIFT;
82 prd->u.Memory.Length = PAGE_SIZE;
83 KdPrint((__DRIVER_NAME " Start = %08x:%08x, Length = %d\n", prd->u.Memory.Start.HighPart, prd->u.Memory.Start.LowPart, prd->u.Memory.Length));
84 ptr = start = MmGetMdlVirtualAddress(mdl);
85 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_RING, "ring-ref", NULL);
86 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_EVENT_CHANNEL_IRQ, "event-channel", NULL);
87 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_FRONT, "device-type", NULL);
88 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "sectors", NULL);
89 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_READ_STRING_BACK, "sector-size", NULL);
90 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_VECTORS, NULL, NULL);
91 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_GRANT_ENTRIES, NULL, UlongToPtr(GRANT_ENTRIES));
92 ADD_XEN_INIT_REQ(&ptr, XEN_INIT_TYPE_END, NULL, NULL);
94 stack->Parameters.StartDevice.AllocatedResourcesTranslated = new_crl;
96 old_crl = stack->Parameters.StartDevice.AllocatedResources;
97 new_crl = ExAllocatePoolWithTag(PagedPool, new_length, XENVBD_POOL_TAG);
98 memcpy(new_crl, old_crl, old_length);
99 prl = &new_crl->List[0].PartialResourceList;
100 prd = &prl->PartialDescriptors[prl->Count++];
101 prd->Type = CmResourceTypeMemory;
102 prd->ShareDisposition = CmResourceShareDeviceExclusive;
103 prd->Flags = CM_RESOURCE_MEMORY_READ_WRITE|CM_RESOURCE_MEMORY_PREFETCHABLE|CM_RESOURCE_MEMORY_CACHEABLE;
104 prd->u.Memory.Start.QuadPart = MmGetMdlPfnArray(mdl)[0] << PAGE_SHIFT;
105 prd->u.Memory.Length = PAGE_SIZE;
106 stack->Parameters.StartDevice.AllocatedResources = new_crl;
107 IoCopyCurrentIrpStackLocationToNext(irp);
108 }
109 status = XenVbd_Pnp_Original(device_object, irp);
111 break;
112 #if 0
113 case IRP_MN_QUERY_STOP_DEVICE:
114 KdPrint((__DRIVER_NAME " IRP_MN_QUERY_STOP_DEVICE\n"));
115 status = XenVbd_Pnp_Original(device_object, irp);
116 break;
118 case IRP_MN_STOP_DEVICE:
119 KdPrint((__DRIVER_NAME " IRP_MN_STOP_DEVICE\n"));
120 status = XenVbd_Pnp_Original(device_object, irp);
121 break;
123 case IRP_MN_CANCEL_STOP_DEVICE:
124 KdPrint((__DRIVER_NAME " IRP_MN_CANCEL_STOP_DEVICE\n"));
125 status = XenVbd_Pnp_Original(device_object, irp);
126 break;
128 case IRP_MN_QUERY_REMOVE_DEVICE:
129 KdPrint((__DRIVER_NAME " IRP_MN_QUERY_REMOVE_DEVICE\n"));
130 status = XenVbd_Pnp_Original(device_object, irp);
131 break;
133 case IRP_MN_REMOVE_DEVICE:
134 KdPrint((__DRIVER_NAME " IRP_MN_REMOVE_DEVICE\n"));
135 status = XenVbd_Pnp_Original(device_object, irp);
136 break;
138 case IRP_MN_CANCEL_REMOVE_DEVICE:
139 KdPrint((__DRIVER_NAME " IRP_MN_CANCEL_REMOVE_DEVICE\n"));
140 status = XenVbd_Pnp_Original(device_object, irp);
141 break;
143 case IRP_MN_SURPRISE_REMOVAL:
144 KdPrint((__DRIVER_NAME " IRP_MN_SURPRISE_REMOVAL\n"));
145 status = XenVbd_Pnp_Original(device_object, irp);
146 break;
147 #endif
149 default:
150 //KdPrint((__DRIVER_NAME " Unknown Minor = %d\n", stack->MinorFunction));
151 status = XenVbd_Pnp_Original(device_object, irp);
152 break;
153 }
155 KdPrint((__DRIVER_NAME " <-- " __FUNCTION__"\n"));
157 return status;
158 }
159 #endif
161 NTSTATUS
162 DriverEntry(PDRIVER_OBJECT DriverObject, PUNICODE_STRING RegistryPath)
163 {
164 ULONG status;
165 HW_INITIALIZATION_DATA HwInitializationData;
167 KdPrint((__DRIVER_NAME " --> "__FUNCTION__ "\n"));
168 KdPrint((__DRIVER_NAME " IRQL = %d\n", KeGetCurrentIrql()));
170 RtlZeroMemory(&HwInitializationData, sizeof(HW_INITIALIZATION_DATA));
172 HwInitializationData.HwInitializationDataSize = sizeof(HW_INITIALIZATION_DATA);
173 HwInitializationData.AdapterInterfaceType = Internal;
174 HwInitializationData.DeviceExtensionSize = sizeof(XENVBD_DEVICE_DATA);
175 HwInitializationData.SpecificLuExtensionSize = 0;
176 /* SrbExtension is not always aligned to a page boundary, so we add PAGE_SIZE-1 to it to make sure we have at least UNALIGNED_DOUBLE_BUFFER_SIZE bytes of page aligned memory */
177 HwInitializationData.SrbExtensionSize = UNALIGNED_DOUBLE_BUFFER_SIZE + PAGE_SIZE - 1;
178 HwInitializationData.NumberOfAccessRanges = 1;
179 HwInitializationData.MapBuffers = TRUE;
180 HwInitializationData.NeedPhysicalAddresses = FALSE;
181 HwInitializationData.TaggedQueuing = FALSE;
182 HwInitializationData.AutoRequestSense = TRUE;
183 HwInitializationData.MultipleRequestPerLu = TRUE;
184 HwInitializationData.ReceiveEvent = FALSE;
185 HwInitializationData.VendorIdLength = 0;
186 HwInitializationData.VendorId = NULL;
187 HwInitializationData.DeviceIdLength = 0;
188 HwInitializationData.DeviceId = NULL;
190 XenVbd_FillInitCallbacks(&HwInitializationData);
192 status = ScsiPortInitialize(DriverObject, RegistryPath, &HwInitializationData, NULL);
195 #if 0
196 /* DriverObject will be NULL if we are being called in dump mode */
197 if (DriverObject != NULL)
198 {
199 /* this is a bit naughty... */
200 XenVbd_Pnp_Original = DriverObject->MajorFunction[IRP_MJ_PNP];
201 DriverObject->MajorFunction[IRP_MJ_PNP] = XenVbd_Pnp;
202 }
203 else
204 XenVbd_Pnp_Original = NULL;
205 #endif
207 if(!NT_SUCCESS(status))
208 {
209 KdPrint((__DRIVER_NAME " ScsiPortInitialize failed with status 0x%08x\n", status));
210 }
212 KdPrint((__DRIVER_NAME " <-- " __FUNCTION__ "\n"));
214 return status;
215 }