2 * Copyright (c) 2009, Microsoft Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
22 #include <linux/kernel.h>
24 #include <linux/slab.h>
27 #include "version_info.h"
28 #include "vmbus_private.h"
30 static const char *gDriverName
= "vmbus";
33 * Windows vmbus does not defined this.
34 * We defined this to be consistent with other devices
36 /* {c5295816-f63a-4d5f-8d1a-4daf999ca185} */
37 static const struct hv_guid gVmbusDeviceType
= {
39 0x16, 0x58, 0x29, 0xc5, 0x3a, 0xf6, 0x5f, 0x4d,
40 0x8d, 0x1a, 0x4d, 0xaf, 0x99, 0x9c, 0xa1, 0x85
44 /* {ac3760fc-9adf-40aa-9427-a70ed6de95c5} */
45 static const struct hv_guid gVmbusDeviceId
= {
47 0xfc, 0x60, 0x37, 0xac, 0xdf, 0x9a, 0xaa, 0x40,
48 0x94, 0x27, 0xa7, 0x0e, 0xd6, 0xde, 0x95, 0xc5
52 static struct hv_driver
*gDriver
; /* vmbus driver object */
53 static struct hv_device
*gDevice
; /* vmbus root device */
56 * VmbusChildDeviceAdd - Registers the child device with the vmbus
58 int VmbusChildDeviceAdd(struct hv_device
*ChildDevice
)
60 struct vmbus_driver
*vmbusDriver
= (struct vmbus_driver
*)gDriver
;
62 return vmbusDriver
->OnChildDeviceAdd(gDevice
, ChildDevice
);
66 * VmbusOnDeviceAdd - Callback when the root bus device is added
68 static int VmbusOnDeviceAdd(struct hv_device
*dev
, void *AdditionalInfo
)
70 u32
*irqvector
= AdditionalInfo
;
75 memcpy(&gDevice
->deviceType
, &gVmbusDeviceType
, sizeof(struct hv_guid
));
76 memcpy(&gDevice
->deviceInstance
, &gVmbusDeviceId
,
77 sizeof(struct hv_guid
));
79 /* strcpy(dev->name, "vmbus"); */
81 on_each_cpu(hv_synic_init
, (void *)irqvector
, 1);
83 /* Connect to VMBus in the root partition */
86 /* VmbusSendEvent(device->localPortId+1); */
91 * VmbusOnDeviceRemove - Callback when the root bus device is removed
93 static int VmbusOnDeviceRemove(struct hv_device
*dev
)
97 vmbus_release_unattached_channels();
99 on_each_cpu(hv_synic_cleanup
, NULL
, 1);
104 * VmbusOnCleanup - Perform any cleanup when the driver is removed
106 static void VmbusOnCleanup(struct hv_driver
*drv
)
108 /* struct vmbus_driver *driver = (struct vmbus_driver *)drv; */
114 * vmbus_on_msg_dpc - DPC routine to handle messages from the hypervisior
116 void vmbus_on_msg_dpc(struct hv_driver
*drv
)
118 int cpu
= smp_processor_id();
119 void *page_addr
= hv_context
.synic_message_page
[cpu
];
120 struct hv_message
*msg
= (struct hv_message
*)page_addr
+
122 struct hv_message
*copied
;
125 if (msg
->header
.message_type
== HVMSG_NONE
) {
129 copied
= kmemdup(msg
, sizeof(*copied
), GFP_ATOMIC
);
133 osd_schedule_callback(gVmbusConnection
.WorkQueue
,
138 msg
->header
.message_type
= HVMSG_NONE
;
141 * Make sure the write to MessageType (ie set to
142 * HVMSG_NONE) happens before we read the
143 * MessagePending and EOMing. Otherwise, the EOMing
144 * will not deliver any more messages since there is
149 if (msg
->header
.message_flags
.msg_pending
) {
151 * This will cause message queue rescan to
152 * possibly deliver another msg from the
155 wrmsrl(HV_X64_MSR_EOM
, 0);
161 * vmbus_on_event_dpc - DPC routine to handle events from the hypervisior
163 void vmbus_on_event_dpc(struct hv_driver
*drv
)
165 /* TODO: Process any events */
170 * vmbus_on_isr - ISR routine
172 int vmbus_on_isr(struct hv_driver
*drv
)
175 int cpu
= smp_processor_id();
177 struct hv_message
*msg
;
178 union hv_synic_event_flags
*event
;
180 page_addr
= hv_context
.synic_message_page
[cpu
];
181 msg
= (struct hv_message
*)page_addr
+ VMBUS_MESSAGE_SINT
;
183 /* Check if there are actual msgs to be process */
184 if (msg
->header
.message_type
!= HVMSG_NONE
) {
185 DPRINT_DBG(VMBUS
, "received msg type %d size %d",
186 msg
->header
.message_type
,
187 msg
->header
.payload_size
);
191 /* TODO: Check if there are events to be process */
192 page_addr
= hv_context
.synic_event_page
[cpu
];
193 event
= (union hv_synic_event_flags
*)page_addr
+ VMBUS_MESSAGE_SINT
;
195 /* Since we are a child, we only need to check bit 0 */
196 if (test_and_clear_bit(0, (unsigned long *) &event
->flags32
[0])) {
197 DPRINT_DBG(VMBUS
, "received event %d", event
->flags32
[0]);
205 * VmbusInitialize - Main entry point
207 int VmbusInitialize(struct hv_driver
*drv
)
209 struct vmbus_driver
*driver
= (struct vmbus_driver
*)drv
;
212 DPRINT_INFO(VMBUS
, "+++++++ HV Driver version = %s +++++++",
214 DPRINT_INFO(VMBUS
, "+++++++ Vmbus supported version = %d +++++++",
215 VMBUS_REVISION_NUMBER
);
216 DPRINT_INFO(VMBUS
, "+++++++ Vmbus using SINT %d +++++++",
218 DPRINT_DBG(VMBUS
, "sizeof(vmbus_channel_packet_page_buffer)=%zd, "
219 "sizeof(VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER)=%zd",
220 sizeof(struct vmbus_channel_packet_page_buffer
),
221 sizeof(struct vmbus_channel_packet_multipage_buffer
));
223 drv
->name
= gDriverName
;
224 memcpy(&drv
->deviceType
, &gVmbusDeviceType
, sizeof(struct hv_guid
));
226 /* Setup dispatch table */
227 driver
->Base
.OnDeviceAdd
= VmbusOnDeviceAdd
;
228 driver
->Base
.OnDeviceRemove
= VmbusOnDeviceRemove
;
229 driver
->Base
.OnCleanup
= VmbusOnCleanup
;
231 /* Hypervisor initialization...setup hypercall page..etc */
234 DPRINT_ERR(VMBUS
, "Unable to initialize the hypervisor - 0x%x",