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>
25 #include "include/logging.h"
26 #include "VersionInfo.h"
27 #include "VmbusPrivate.h"
32 static const char* gDriverName="vmbus";
34 // Windows vmbus does not defined this. We defined this to be consistent with other devices
35 //{c5295816-f63a-4d5f-8d1a-4daf999ca185}
36 static const GUID gVmbusDeviceType={
37 .Data = {0x16, 0x58, 0x29, 0xc5, 0x3a, 0xf6, 0x5f, 0x4d, 0x8d, 0x1a, 0x4d, 0xaf, 0x99, 0x9c, 0xa1, 0x85}
40 //{ac3760fc-9adf-40aa-9427-a70ed6de95c5}
41 static const GUID gVmbusDeviceId={
42 .Data = {0xfc, 0x60, 0x37, 0xac, 0xdf, 0x9a, 0xaa, 0x40, 0x94, 0x27, 0xa7, 0x0e, 0xd6, 0xde, 0x95, 0xc5}
45 static DRIVER_OBJECT* gDriver; // vmbus driver object
46 static DEVICE_OBJECT* gDevice; // vmbus root device
54 VmbusGetChannelInterface(
55 VMBUS_CHANNEL_INTERFACE *Interface
60 DEVICE_OBJECT *DeviceObject,
61 DEVICE_INFO *DeviceInfo
65 VmbusGetChannelOffers(
71 DEVICE_OBJECT *Device,
114 VMBUS_DRIVER_OBJECT* driver = (VMBUS_DRIVER_OBJECT*)drv;
119 DPRINT_INFO(VMBUS, "+++++++ Build Date=%s %s +++++++", VersionDate, VersionTime);
120 DPRINT_INFO(VMBUS, "+++++++ Build Description=%s +++++++", VersionDesc);
122 DPRINT_INFO(VMBUS, "+++++++ Vmbus supported version = %d +++++++", VMBUS_REVISION_NUMBER);
123 DPRINT_INFO(VMBUS, "+++++++ Vmbus using SINT %d +++++++", VMBUS_MESSAGE_SINT);
125 DPRINT_DBG(VMBUS, "sizeof(VMBUS_CHANNEL_PACKET_PAGE_BUFFER)=%d, sizeof(VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER)=%d",
126 sizeof(VMBUS_CHANNEL_PACKET_PAGE_BUFFER), sizeof(VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER));
128 drv->name = gDriverName;
129 memcpy(&drv->deviceType, &gVmbusDeviceType, sizeof(GUID));
131 // Setup dispatch table
132 driver->Base.OnDeviceAdd = VmbusOnDeviceAdd;
133 driver->Base.OnDeviceRemove = VmbusOnDeviceRemove;
134 driver->Base.OnCleanup = VmbusOnCleanup;
135 driver->OnIsr = VmbusOnISR;
136 driver->OnMsgDpc = VmbusOnMsgDPC;
137 driver->OnEventDpc = VmbusOnEventDPC;
138 driver->GetChannelOffers = VmbusGetChannelOffers;
139 driver->GetChannelInterface = VmbusGetChannelInterface;
140 driver->GetChannelInfo = VmbusGetChannelInfo;
142 // Hypervisor initialization...setup hypercall page..etc
146 DPRINT_ERR(VMBUS, "Unable to initialize the hypervisor - 0x%x", ret);
160 VmbusGetChannelOffers()
163 Retrieve the channel offers from the parent partition
168 VmbusGetChannelOffers(void)
171 VmbusChannelRequestOffers();
179 VmbusGetChannelInterface()
182 Get the channel interface
186 VmbusGetChannelInterface(
187 VMBUS_CHANNEL_INTERFACE *Interface
190 GetChannelInterface(Interface);
197 VmbusGetChannelInterface()
200 Get the device info for the specified device object
205 DEVICE_OBJECT *DeviceObject,
206 DEVICE_INFO *DeviceInfo
209 GetChannelInfo(DeviceObject, DeviceInfo);
217 VmbusCreateChildDevice()
220 Creates the child device on the bus that represents the channel offer
225 VmbusChildDeviceCreate(
230 VMBUS_DRIVER_OBJECT* vmbusDriver = (VMBUS_DRIVER_OBJECT*)gDriver;
232 return vmbusDriver->OnChildDeviceCreate(
242 VmbusChildDeviceAdd()
245 Registers the child device with the vmbus
250 DEVICE_OBJECT* ChildDevice)
252 VMBUS_DRIVER_OBJECT* vmbusDriver = (VMBUS_DRIVER_OBJECT*)gDriver;
254 return vmbusDriver->OnChildDeviceAdd(gDevice, ChildDevice);
261 VmbusChildDeviceRemove()
264 Unregisters the child device from the vmbus
268 VmbusChildDeviceRemove(
269 DEVICE_OBJECT* ChildDevice)
271 VMBUS_DRIVER_OBJECT* vmbusDriver = (VMBUS_DRIVER_OBJECT*)gDriver;
273 vmbusDriver->OnChildDeviceRemove(ChildDevice);
279 VmbusChildDeviceDestroy()
282 Release the child device from the vmbus
286 //VmbusChildDeviceDestroy(
287 // DEVICE_OBJECT* ChildDevice
290 // VMBUS_DRIVER_OBJECT* vmbusDriver = (VMBUS_DRIVER_OBJECT*)gDriver;
292 // vmbusDriver->OnChildDeviceDestroy(ChildDevice);
301 Callback when the root bus device is added
310 UINT32 *irqvector = (UINT32*) AdditionalInfo;
317 memcpy(&gDevice->deviceType, &gVmbusDeviceType, sizeof(GUID));
318 memcpy(&gDevice->deviceInstance, &gVmbusDeviceId, sizeof(GUID));
320 //strcpy(dev->name, "vmbus");
322 ret = HvSynicInit(*irqvector);
324 // Connect to VMBus in the root partition
325 ret = VmbusConnect();
327 //VmbusSendEvent(device->localPortId+1);
337 VmbusOnDeviceRemove()
340 Callback when the root bus device is removed
343 int VmbusOnDeviceRemove(
351 VmbusChannelReleaseUnattachedChannels();
369 Perform any cleanup when the driver is removed
377 //VMBUS_DRIVER_OBJECT* driver = (VMBUS_DRIVER_OBJECT*)drv;
393 DPC routine to handle messages from the hypervisior
401 void *page_addr = gHvContext.synICMessagePage[0];
403 HV_MESSAGE* msg = (HV_MESSAGE*)page_addr + VMBUS_MESSAGE_SINT;
407 if (msg->Header.MessageType == HvMessageTypeNone) // no msg
413 copied = MemAllocAtomic(sizeof(HV_MESSAGE));
419 memcpy(copied, msg, sizeof(HV_MESSAGE));
420 WorkQueueQueueWorkItem(gVmbusConnection.WorkQueue, VmbusOnChannelMessage, (void*)copied);
423 msg->Header.MessageType = HvMessageTypeNone;
425 // Make sure the write to MessageType (ie set to HvMessageTypeNone) happens
426 // before we read the MessagePending and EOMing. Otherwise, the EOMing will not deliver
427 // any more messages since there is no empty slot
430 if (msg->Header.MessageFlags.MessagePending)
432 // This will cause message queue rescan to possibly deliver another msg from the hypervisor
433 WriteMsr(HV_X64_MSR_EOM, 0);
444 DPC routine to handle events from the hypervisior
452 // TODO: Process any events
471 //VMBUS_DRIVER_OBJECT* driver = (VMBUS_DRIVER_OBJECT*)drv;
477 HV_SYNIC_EVENT_FLAGS* event;
479 //page = SynICMessagePage[0];
480 //page_addr = page_address(page);
481 page_addr = gHvContext.synICMessagePage[0];
482 msg = (HV_MESSAGE*)page_addr + VMBUS_MESSAGE_SINT;
486 // Check if there are actual msgs to be process
487 if (msg->Header.MessageType != HvMessageTypeNone)
489 DPRINT_DBG(VMBUS, "received msg type %d size %d", msg->Header.MessageType, msg->Header.PayloadSize);
493 // TODO: Check if there are events to be process
494 page_addr = gHvContext.synICEventPage[0];
495 event = (HV_SYNIC_EVENT_FLAGS*)page_addr + VMBUS_MESSAGE_SINT;
497 // Since we are a child, we only need to check bit 0
498 if (BitTestAndClear(&event->Flags32[0], 0))
500 DPRINT_DBG(VMBUS, "received event %d", event->Flags32[0]);