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. We defined this to be consistent with other devices
36 //{c5295816-f63a-4d5f-8d1a-4daf999ca185}
37 static const GUID gVmbusDeviceType
={
38 .Data
= {0x16, 0x58, 0x29, 0xc5, 0x3a, 0xf6, 0x5f, 0x4d, 0x8d, 0x1a, 0x4d, 0xaf, 0x99, 0x9c, 0xa1, 0x85}
41 //{ac3760fc-9adf-40aa-9427-a70ed6de95c5}
42 static const GUID gVmbusDeviceId
={
43 .Data
= {0xfc, 0x60, 0x37, 0xac, 0xdf, 0x9a, 0xaa, 0x40, 0x94, 0x27, 0xa7, 0x0e, 0xd6, 0xde, 0x95, 0xc5}
46 static DRIVER_OBJECT
* gDriver
; // vmbus driver object
47 static DEVICE_OBJECT
* gDevice
; // vmbus root device
55 VmbusGetChannelInterface(
56 VMBUS_CHANNEL_INTERFACE
*Interface
61 DEVICE_OBJECT
*DeviceObject
,
62 DEVICE_INFO
*DeviceInfo
66 VmbusGetChannelOffers(
72 DEVICE_OBJECT
*Device
,
115 VMBUS_DRIVER_OBJECT
* driver
= (VMBUS_DRIVER_OBJECT
*)drv
;
120 DPRINT_INFO(VMBUS
, "+++++++ Build Date=%s %s +++++++", VersionDate
, VersionTime
);
121 DPRINT_INFO(VMBUS
, "+++++++ Build Description=%s +++++++", VersionDesc
);
123 DPRINT_INFO(VMBUS
, "+++++++ Vmbus supported version = %d +++++++", VMBUS_REVISION_NUMBER
);
124 DPRINT_INFO(VMBUS
, "+++++++ Vmbus using SINT %d +++++++", VMBUS_MESSAGE_SINT
);
126 DPRINT_DBG(VMBUS
, "sizeof(VMBUS_CHANNEL_PACKET_PAGE_BUFFER)=%ld, sizeof(VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER)=%ld",
127 sizeof(VMBUS_CHANNEL_PACKET_PAGE_BUFFER
), sizeof(VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER
));
129 drv
->name
= gDriverName
;
130 memcpy(&drv
->deviceType
, &gVmbusDeviceType
, sizeof(GUID
));
132 // Setup dispatch table
133 driver
->Base
.OnDeviceAdd
= VmbusOnDeviceAdd
;
134 driver
->Base
.OnDeviceRemove
= VmbusOnDeviceRemove
;
135 driver
->Base
.OnCleanup
= VmbusOnCleanup
;
136 driver
->OnIsr
= VmbusOnISR
;
137 driver
->OnMsgDpc
= VmbusOnMsgDPC
;
138 driver
->OnEventDpc
= VmbusOnEventDPC
;
139 driver
->GetChannelOffers
= VmbusGetChannelOffers
;
140 driver
->GetChannelInterface
= VmbusGetChannelInterface
;
141 driver
->GetChannelInfo
= VmbusGetChannelInfo
;
143 // Hypervisor initialization...setup hypercall page..etc
147 DPRINT_ERR(VMBUS
, "Unable to initialize the hypervisor - 0x%x", ret
);
161 VmbusGetChannelOffers()
164 Retrieve the channel offers from the parent partition
169 VmbusGetChannelOffers(void)
172 VmbusChannelRequestOffers();
180 VmbusGetChannelInterface()
183 Get the channel interface
187 VmbusGetChannelInterface(
188 VMBUS_CHANNEL_INTERFACE
*Interface
191 GetChannelInterface(Interface
);
198 VmbusGetChannelInterface()
201 Get the device info for the specified device object
206 DEVICE_OBJECT
*DeviceObject
,
207 DEVICE_INFO
*DeviceInfo
210 GetChannelInfo(DeviceObject
, DeviceInfo
);
218 VmbusCreateChildDevice()
221 Creates the child device on the bus that represents the channel offer
226 VmbusChildDeviceCreate(
231 VMBUS_DRIVER_OBJECT
* vmbusDriver
= (VMBUS_DRIVER_OBJECT
*)gDriver
;
233 return vmbusDriver
->OnChildDeviceCreate(
243 VmbusChildDeviceAdd()
246 Registers the child device with the vmbus
251 DEVICE_OBJECT
* ChildDevice
)
253 VMBUS_DRIVER_OBJECT
* vmbusDriver
= (VMBUS_DRIVER_OBJECT
*)gDriver
;
255 return vmbusDriver
->OnChildDeviceAdd(gDevice
, ChildDevice
);
262 VmbusChildDeviceRemove()
265 Unregisters the child device from the vmbus
269 VmbusChildDeviceRemove(
270 DEVICE_OBJECT
* ChildDevice
)
272 VMBUS_DRIVER_OBJECT
* vmbusDriver
= (VMBUS_DRIVER_OBJECT
*)gDriver
;
274 vmbusDriver
->OnChildDeviceRemove(ChildDevice
);
280 VmbusChildDeviceDestroy()
283 Release the child device from the vmbus
287 //VmbusChildDeviceDestroy(
288 // DEVICE_OBJECT* ChildDevice
291 // VMBUS_DRIVER_OBJECT* vmbusDriver = (VMBUS_DRIVER_OBJECT*)gDriver;
293 // vmbusDriver->OnChildDeviceDestroy(ChildDevice);
302 Callback when the root bus device is added
311 u32
*irqvector
= (u32
*) AdditionalInfo
;
318 memcpy(&gDevice
->deviceType
, &gVmbusDeviceType
, sizeof(GUID
));
319 memcpy(&gDevice
->deviceInstance
, &gVmbusDeviceId
, sizeof(GUID
));
321 //strcpy(dev->name, "vmbus");
323 ret
= HvSynicInit(*irqvector
);
325 // Connect to VMBus in the root partition
326 ret
= VmbusConnect();
328 //VmbusSendEvent(device->localPortId+1);
338 VmbusOnDeviceRemove()
341 Callback when the root bus device is removed
344 int VmbusOnDeviceRemove(
352 VmbusChannelReleaseUnattachedChannels();
370 Perform any cleanup when the driver is removed
378 //VMBUS_DRIVER_OBJECT* driver = (VMBUS_DRIVER_OBJECT*)drv;
394 DPC routine to handle messages from the hypervisior
402 void *page_addr
= gHvContext
.synICMessagePage
[0];
404 HV_MESSAGE
* msg
= (HV_MESSAGE
*)page_addr
+ VMBUS_MESSAGE_SINT
;
408 if (msg
->Header
.MessageType
== HvMessageTypeNone
) // no msg
414 copied
= kmalloc(sizeof(HV_MESSAGE
), GFP_ATOMIC
);
420 memcpy(copied
, msg
, sizeof(HV_MESSAGE
));
421 WorkQueueQueueWorkItem(gVmbusConnection
.WorkQueue
, VmbusOnChannelMessage
, (void*)copied
);
424 msg
->Header
.MessageType
= HvMessageTypeNone
;
426 // Make sure the write to MessageType (ie set to HvMessageTypeNone) happens
427 // before we read the MessagePending and EOMing. Otherwise, the EOMing will not deliver
428 // any more messages since there is no empty slot
431 if (msg
->Header
.MessageFlags
.MessagePending
)
433 // This will cause message queue rescan to possibly deliver another msg from the hypervisor
434 WriteMsr(HV_X64_MSR_EOM
, 0);
445 DPC routine to handle events from the hypervisior
453 // TODO: Process any events
472 //VMBUS_DRIVER_OBJECT* driver = (VMBUS_DRIVER_OBJECT*)drv;
478 HV_SYNIC_EVENT_FLAGS
* event
;
480 //page = SynICMessagePage[0];
481 //page_addr = page_address(page);
482 page_addr
= gHvContext
.synICMessagePage
[0];
483 msg
= (HV_MESSAGE
*)page_addr
+ VMBUS_MESSAGE_SINT
;
487 // Check if there are actual msgs to be process
488 if (msg
->Header
.MessageType
!= HvMessageTypeNone
)
490 DPRINT_DBG(VMBUS
, "received msg type %d size %d", msg
->Header
.MessageType
, msg
->Header
.PayloadSize
);
494 // TODO: Check if there are events to be process
495 page_addr
= gHvContext
.synICEventPage
[0];
496 event
= (HV_SYNIC_EVENT_FLAGS
*)page_addr
+ VMBUS_MESSAGE_SINT
;
498 // Since we are a child, we only need to check bit 0
499 if (BitTestAndClear(&event
->Flags32
[0], 0))
501 DPRINT_DBG(VMBUS
, "received event %d", event
->Flags32
[0]);