3 * Copyright (c) 2009, Microsoft Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
24 #include <linux/kernel.h>
26 #include "include/logging.h"
27 #include "VersionInfo.h"
28 #include "VmbusPrivate.h"
33 static const char* gDriverName
="vmbus";
35 /* Windows vmbus does not defined this.
36 * We defined this to be consistent with other devices
38 /* {c5295816-f63a-4d5f-8d1a-4daf999ca185} */
39 static const GUID gVmbusDeviceType
={
40 .Data
= {0x16, 0x58, 0x29, 0xc5, 0x3a, 0xf6, 0x5f, 0x4d, 0x8d, 0x1a, 0x4d, 0xaf, 0x99, 0x9c, 0xa1, 0x85}
43 /* {ac3760fc-9adf-40aa-9427-a70ed6de95c5} */
44 static const GUID gVmbusDeviceId
={
45 .Data
= {0xfc, 0x60, 0x37, 0xac, 0xdf, 0x9a, 0xaa, 0x40, 0x94, 0x27, 0xa7, 0x0e, 0xd6, 0xde, 0x95, 0xc5}
48 static DRIVER_OBJECT
* gDriver
; /* vmbus driver object */
49 static struct hv_device
* gDevice
; /* vmbus root device */
53 /* Internal routines */
57 VmbusGetChannelInterface(
58 VMBUS_CHANNEL_INTERFACE
*Interface
63 struct hv_device
*DeviceObject
,
64 DEVICE_INFO
*DeviceInfo
68 VmbusGetChannelOffers(
74 struct hv_device
*Device
,
117 VMBUS_DRIVER_OBJECT
* driver
= (VMBUS_DRIVER_OBJECT
*)drv
;
122 DPRINT_INFO(VMBUS
, "+++++++ Build Date=%s %s +++++++", VersionDate
, VersionTime
);
123 DPRINT_INFO(VMBUS
, "+++++++ Build Description=%s +++++++", VersionDesc
);
125 DPRINT_INFO(VMBUS
, "+++++++ Vmbus supported version = %d +++++++", VMBUS_REVISION_NUMBER
);
126 DPRINT_INFO(VMBUS
, "+++++++ Vmbus using SINT %d +++++++", VMBUS_MESSAGE_SINT
);
128 DPRINT_DBG(VMBUS
, "sizeof(VMBUS_CHANNEL_PACKET_PAGE_BUFFER)=%d, sizeof(VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER)=%d",
129 sizeof(struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER
), sizeof(struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER
));
131 drv
->name
= gDriverName
;
132 memcpy(&drv
->deviceType
, &gVmbusDeviceType
, sizeof(GUID
));
134 /* Setup dispatch table */
135 driver
->Base
.OnDeviceAdd
= VmbusOnDeviceAdd
;
136 driver
->Base
.OnDeviceRemove
= VmbusOnDeviceRemove
;
137 driver
->Base
.OnCleanup
= VmbusOnCleanup
;
138 driver
->OnIsr
= VmbusOnISR
;
139 driver
->OnMsgDpc
= VmbusOnMsgDPC
;
140 driver
->OnEventDpc
= VmbusOnEventDPC
;
141 driver
->GetChannelOffers
= VmbusGetChannelOffers
;
142 driver
->GetChannelInterface
= VmbusGetChannelInterface
;
143 driver
->GetChannelInfo
= VmbusGetChannelInfo
;
145 /* Hypervisor initialization...setup hypercall page..etc */
149 DPRINT_ERR(VMBUS
, "Unable to initialize the hypervisor - 0x%x", ret
);
163 VmbusGetChannelOffers()
166 Retrieve the channel offers from the parent partition
171 VmbusGetChannelOffers(void)
174 VmbusChannelRequestOffers();
182 VmbusGetChannelInterface()
185 Get the channel interface
189 VmbusGetChannelInterface(
190 VMBUS_CHANNEL_INTERFACE
*Interface
193 GetChannelInterface(Interface
);
200 VmbusGetChannelInterface()
203 Get the device info for the specified device object
208 struct hv_device
*DeviceObject
,
209 DEVICE_INFO
*DeviceInfo
212 GetChannelInfo(DeviceObject
, DeviceInfo
);
220 VmbusCreateChildDevice()
223 Creates the child device on the bus that represents the channel offer
228 VmbusChildDeviceCreate(
233 VMBUS_DRIVER_OBJECT
* vmbusDriver
= (VMBUS_DRIVER_OBJECT
*)gDriver
;
235 return vmbusDriver
->OnChildDeviceCreate(
245 VmbusChildDeviceAdd()
248 Registers the child device with the vmbus
253 struct hv_device
*ChildDevice
)
255 VMBUS_DRIVER_OBJECT
* vmbusDriver
= (VMBUS_DRIVER_OBJECT
*)gDriver
;
257 return vmbusDriver
->OnChildDeviceAdd(gDevice
, ChildDevice
);
264 VmbusChildDeviceRemove()
267 Unregisters the child device from the vmbus
271 VmbusChildDeviceRemove(
272 struct hv_device
*ChildDevice
)
274 VMBUS_DRIVER_OBJECT
* vmbusDriver
= (VMBUS_DRIVER_OBJECT
*)gDriver
;
276 vmbusDriver
->OnChildDeviceRemove(ChildDevice
);
282 VmbusChildDeviceDestroy()
285 Release the child device from the vmbus
291 VmbusChildDeviceDestroy(
292 struct hv_device *ChildDevice
295 VMBUS_DRIVER_OBJECT* vmbusDriver = (VMBUS_DRIVER_OBJECT*)gDriver;
297 vmbusDriver->OnChildDeviceDestroy(ChildDevice);
307 Callback when the root bus device is added
312 struct hv_device
*dev
,
316 u32
*irqvector
= (u32
*) AdditionalInfo
;
323 memcpy(&gDevice
->deviceType
, &gVmbusDeviceType
, sizeof(GUID
));
324 memcpy(&gDevice
->deviceInstance
, &gVmbusDeviceId
, sizeof(GUID
));
326 /* strcpy(dev->name, "vmbus"); */
328 ret
= HvSynicInit(*irqvector
);
330 /* Connect to VMBus in the root partition */
331 ret
= VmbusConnect();
333 /* VmbusSendEvent(device->localPortId+1); */
343 VmbusOnDeviceRemove()
346 Callback when the root bus device is removed
349 int VmbusOnDeviceRemove(
350 struct hv_device
*dev
357 VmbusChannelReleaseUnattachedChannels();
375 Perform any cleanup when the driver is removed
383 /* VMBUS_DRIVER_OBJECT* driver = (VMBUS_DRIVER_OBJECT*)drv; */
399 DPC routine to handle messages from the hypervisior
407 void *page_addr
= gHvContext
.synICMessagePage
[0];
409 HV_MESSAGE
* msg
= (HV_MESSAGE
*)page_addr
+ VMBUS_MESSAGE_SINT
;
413 if (msg
->Header
.MessageType
== HvMessageTypeNone
) /* no msg */
419 copied
= kmalloc(sizeof(HV_MESSAGE
), GFP_ATOMIC
);
425 memcpy(copied
, msg
, sizeof(HV_MESSAGE
));
426 WorkQueueQueueWorkItem(gVmbusConnection
.WorkQueue
, VmbusOnChannelMessage
, (void*)copied
);
429 msg
->Header
.MessageType
= HvMessageTypeNone
;
432 * Make sure the write to MessageType (ie set to
433 * HvMessageTypeNone) happens before we read the
434 * MessagePending and EOMing. Otherwise, the EOMing
435 * will not deliver any more messages since there is
440 if (msg
->Header
.MessageFlags
.MessagePending
)
443 * This will cause message queue rescan to
444 * possibly deliver another msg from the
447 WriteMsr(HV_X64_MSR_EOM
, 0);
458 DPC routine to handle events from the hypervisior
466 /* TODO: Process any events */
485 /* VMBUS_DRIVER_OBJECT* driver = (VMBUS_DRIVER_OBJECT*)drv; */
488 /* struct page* page; */
491 HV_SYNIC_EVENT_FLAGS
* event
;
493 /* page = SynICMessagePage[0]; */
494 /* page_addr = page_address(page); */
495 page_addr
= gHvContext
.synICMessagePage
[0];
496 msg
= (HV_MESSAGE
*)page_addr
+ VMBUS_MESSAGE_SINT
;
500 /* Check if there are actual msgs to be process */
501 if (msg
->Header
.MessageType
!= HvMessageTypeNone
)
503 DPRINT_DBG(VMBUS
, "received msg type %d size %d", msg
->Header
.MessageType
, msg
->Header
.PayloadSize
);
507 /* TODO: Check if there are events to be process */
508 page_addr
= gHvContext
.synICEventPage
[0];
509 event
= (HV_SYNIC_EVENT_FLAGS
*)page_addr
+ VMBUS_MESSAGE_SINT
;
511 /* Since we are a child, we only need to check bit 0 */
512 if (BitTestAndClear(&event
->Flags32
[0], 0))
514 DPRINT_DBG(VMBUS
, "received event %d", event
->Flags32
[0]);