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>
27 #include "include/logging.h"
28 #include "VersionInfo.h"
29 #include "VmbusPrivate.h"
34 static const char* gDriverName
="vmbus";
36 /* Windows vmbus does not defined this.
37 * We defined this to be consistent with other devices
39 /* {c5295816-f63a-4d5f-8d1a-4daf999ca185} */
40 static const struct hv_guid gVmbusDeviceType
= {
42 0x16, 0x58, 0x29, 0xc5, 0x3a, 0xf6, 0x5f, 0x4d,
43 0x8d, 0x1a, 0x4d, 0xaf, 0x99, 0x9c, 0xa1, 0x85
47 /* {ac3760fc-9adf-40aa-9427-a70ed6de95c5} */
48 static const struct hv_guid gVmbusDeviceId
= {
50 0xfc, 0x60, 0x37, 0xac, 0xdf, 0x9a, 0xaa, 0x40,
51 0x94, 0x27, 0xa7, 0x0e, 0xd6, 0xde, 0x95, 0xc5
55 static struct hv_driver
*gDriver
; /* vmbus driver object */
56 static struct hv_device
* gDevice
; /* vmbus root device */
60 /* Internal routines */
64 VmbusGetChannelInterface(
65 VMBUS_CHANNEL_INTERFACE
*Interface
70 struct hv_device
*DeviceObject
,
71 DEVICE_INFO
*DeviceInfo
75 VmbusGetChannelOffers(
81 struct hv_device
*Device
,
102 struct hv_driver
*drv
107 struct hv_driver
*drv
121 struct hv_driver
*drv
124 VMBUS_DRIVER_OBJECT
* driver
= (VMBUS_DRIVER_OBJECT
*)drv
;
129 DPRINT_INFO(VMBUS
, "+++++++ Build Date=%s %s +++++++", VersionDate
, VersionTime
);
130 DPRINT_INFO(VMBUS
, "+++++++ Build Description=%s +++++++", VersionDesc
);
132 DPRINT_INFO(VMBUS
, "+++++++ Vmbus supported version = %d +++++++", VMBUS_REVISION_NUMBER
);
133 DPRINT_INFO(VMBUS
, "+++++++ Vmbus using SINT %d +++++++", VMBUS_MESSAGE_SINT
);
135 DPRINT_DBG(VMBUS
, "sizeof(VMBUS_CHANNEL_PACKET_PAGE_BUFFER)=%zd, sizeof(VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER)=%zd",
136 sizeof(struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER
), sizeof(struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER
));
138 drv
->name
= gDriverName
;
139 memcpy(&drv
->deviceType
, &gVmbusDeviceType
, sizeof(struct hv_guid
));
141 /* Setup dispatch table */
142 driver
->Base
.OnDeviceAdd
= VmbusOnDeviceAdd
;
143 driver
->Base
.OnDeviceRemove
= VmbusOnDeviceRemove
;
144 driver
->Base
.OnCleanup
= VmbusOnCleanup
;
145 driver
->OnIsr
= VmbusOnISR
;
146 driver
->OnMsgDpc
= VmbusOnMsgDPC
;
147 driver
->OnEventDpc
= VmbusOnEventDPC
;
148 driver
->GetChannelOffers
= VmbusGetChannelOffers
;
149 driver
->GetChannelInterface
= VmbusGetChannelInterface
;
150 driver
->GetChannelInfo
= VmbusGetChannelInfo
;
152 /* Hypervisor initialization...setup hypercall page..etc */
156 DPRINT_ERR(VMBUS
, "Unable to initialize the hypervisor - 0x%x", ret
);
170 VmbusGetChannelOffers()
173 Retrieve the channel offers from the parent partition
178 VmbusGetChannelOffers(void)
181 VmbusChannelRequestOffers();
189 VmbusGetChannelInterface()
192 Get the channel interface
196 VmbusGetChannelInterface(
197 VMBUS_CHANNEL_INTERFACE
*Interface
200 GetChannelInterface(Interface
);
207 VmbusGetChannelInterface()
210 Get the device info for the specified device object
215 struct hv_device
*DeviceObject
,
216 DEVICE_INFO
*DeviceInfo
219 GetChannelInfo(DeviceObject
, DeviceInfo
);
227 VmbusCreateChildDevice()
230 Creates the child device on the bus that represents the channel offer
234 struct hv_device
*VmbusChildDeviceCreate(struct hv_guid
*DeviceType
,
235 struct hv_guid
*DeviceInstance
,
238 VMBUS_DRIVER_OBJECT
* vmbusDriver
= (VMBUS_DRIVER_OBJECT
*)gDriver
;
240 return vmbusDriver
->OnChildDeviceCreate(
250 VmbusChildDeviceAdd()
253 Registers the child device with the vmbus
256 int VmbusChildDeviceAdd(struct hv_device
*ChildDevice
)
258 VMBUS_DRIVER_OBJECT
* vmbusDriver
= (VMBUS_DRIVER_OBJECT
*)gDriver
;
260 return vmbusDriver
->OnChildDeviceAdd(gDevice
, ChildDevice
);
267 VmbusChildDeviceRemove()
270 Unregisters the child device from the vmbus
273 void VmbusChildDeviceRemove(struct hv_device
*ChildDevice
)
275 VMBUS_DRIVER_OBJECT
* vmbusDriver
= (VMBUS_DRIVER_OBJECT
*)gDriver
;
277 vmbusDriver
->OnChildDeviceRemove(ChildDevice
);
283 VmbusChildDeviceDestroy()
286 Release the child device from the vmbus
292 VmbusChildDeviceDestroy(
293 struct hv_device *ChildDevice
296 VMBUS_DRIVER_OBJECT* vmbusDriver = (VMBUS_DRIVER_OBJECT*)gDriver;
298 vmbusDriver->OnChildDeviceDestroy(ChildDevice);
308 Callback when the root bus device is added
313 struct hv_device
*dev
,
317 u32
*irqvector
= (u32
*) AdditionalInfo
;
324 memcpy(&gDevice
->deviceType
, &gVmbusDeviceType
, sizeof(struct hv_guid
));
325 memcpy(&gDevice
->deviceInstance
, &gVmbusDeviceId
, sizeof(struct hv_guid
));
327 /* strcpy(dev->name, "vmbus"); */
329 ret
= HvSynicInit(*irqvector
);
331 /* Connect to VMBus in the root partition */
332 ret
= VmbusConnect();
334 /* VmbusSendEvent(device->localPortId+1); */
344 VmbusOnDeviceRemove()
347 Callback when the root bus device is removed
350 static int VmbusOnDeviceRemove(
351 struct hv_device
*dev
358 VmbusChannelReleaseUnattachedChannels();
376 Perform any cleanup when the driver is removed
381 struct hv_driver
*drv
384 /* VMBUS_DRIVER_OBJECT* driver = (VMBUS_DRIVER_OBJECT*)drv; */
400 DPC routine to handle messages from the hypervisior
405 struct hv_driver
*drv
408 void *page_addr
= gHvContext
.synICMessagePage
[0];
410 HV_MESSAGE
* msg
= (HV_MESSAGE
*)page_addr
+ VMBUS_MESSAGE_SINT
;
414 if (msg
->Header
.MessageType
== HvMessageTypeNone
) /* no msg */
420 copied
= kmalloc(sizeof(HV_MESSAGE
), GFP_ATOMIC
);
426 memcpy(copied
, msg
, sizeof(HV_MESSAGE
));
427 osd_schedule_callback(gVmbusConnection
.WorkQueue
,
428 VmbusOnChannelMessage
,
432 msg
->Header
.MessageType
= HvMessageTypeNone
;
435 * Make sure the write to MessageType (ie set to
436 * HvMessageTypeNone) happens before we read the
437 * MessagePending and EOMing. Otherwise, the EOMing
438 * will not deliver any more messages since there is
443 if (msg
->Header
.MessageFlags
.MessagePending
)
446 * This will cause message queue rescan to
447 * possibly deliver another msg from the
450 wrmsrl(HV_X64_MSR_EOM
, 0);
461 DPC routine to handle events from the hypervisior
466 struct hv_driver
* drv
469 /* TODO: Process any events */
485 struct hv_driver
*drv
488 /* VMBUS_DRIVER_OBJECT* driver = (VMBUS_DRIVER_OBJECT*)drv; */
491 /* struct page* page; */
494 HV_SYNIC_EVENT_FLAGS
* event
;
496 /* page = SynICMessagePage[0]; */
497 /* page_addr = page_address(page); */
498 page_addr
= gHvContext
.synICMessagePage
[0];
499 msg
= (HV_MESSAGE
*)page_addr
+ VMBUS_MESSAGE_SINT
;
503 /* Check if there are actual msgs to be process */
504 if (msg
->Header
.MessageType
!= HvMessageTypeNone
)
506 DPRINT_DBG(VMBUS
, "received msg type %d size %d", msg
->Header
.MessageType
, msg
->Header
.PayloadSize
);
510 /* TODO: Check if there are events to be process */
511 page_addr
= gHvContext
.synICEventPage
[0];
512 event
= (HV_SYNIC_EVENT_FLAGS
*)page_addr
+ VMBUS_MESSAGE_SINT
;
514 /* Since we are a child, we only need to check bit 0 */
515 if (test_and_clear_bit(0, (unsigned long *) &event
->Flags32
[0]))
517 DPRINT_DBG(VMBUS
, "received event %d", event
->Flags32
[0]);