]>
Commit | Line | Data |
---|---|---|
3e7ee490 | 1 | /* |
3e7ee490 HJ |
2 | * Copyright (c) 2009, Microsoft Corporation. |
3 | * | |
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. | |
7 | * | |
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 | |
11 | * more details. | |
12 | * | |
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. | |
16 | * | |
17 | * Authors: | |
18 | * Haiyang Zhang <haiyangz@microsoft.com> | |
19 | * Hank Janssen <hjanssen@microsoft.com> | |
20 | * | |
21 | */ | |
5654e932 | 22 | #include <linux/kernel.h> |
0ffa63b0 | 23 | #include <linux/mm.h> |
5a0e3ad6 | 24 | #include <linux/slab.h> |
4983b39a | 25 | #include "osd.h" |
645954c5 | 26 | #include "logging.h" |
3e7ee490 HJ |
27 | #include "VersionInfo.h" |
28 | #include "VmbusPrivate.h" | |
29 | ||
772ec4e2 | 30 | static const char *gDriverName = "vmbus"; |
454f18a9 | 31 | |
772ec4e2 GKH |
32 | /* |
33 | * Windows vmbus does not defined this. | |
454f18a9 BP |
34 | * We defined this to be consistent with other devices |
35 | */ | |
36 | /* {c5295816-f63a-4d5f-8d1a-4daf999ca185} */ | |
caf26a31 GKH |
37 | static const struct hv_guid gVmbusDeviceType = { |
38 | .data = { | |
39 | 0x16, 0x58, 0x29, 0xc5, 0x3a, 0xf6, 0x5f, 0x4d, | |
40 | 0x8d, 0x1a, 0x4d, 0xaf, 0x99, 0x9c, 0xa1, 0x85 | |
41 | } | |
3e7ee490 HJ |
42 | }; |
43 | ||
454f18a9 | 44 | /* {ac3760fc-9adf-40aa-9427-a70ed6de95c5} */ |
caf26a31 GKH |
45 | static const struct hv_guid gVmbusDeviceId = { |
46 | .data = { | |
47 | 0xfc, 0x60, 0x37, 0xac, 0xdf, 0x9a, 0xaa, 0x40, | |
48 | 0x94, 0x27, 0xa7, 0x0e, 0xd6, 0xde, 0x95, 0xc5 | |
49 | } | |
3e7ee490 HJ |
50 | }; |
51 | ||
775ef25e | 52 | static struct hv_driver *gDriver; /* vmbus driver object */ |
772ec4e2 | 53 | static struct hv_device *gDevice; /* vmbus root device */ |
3e7ee490 | 54 | |
772ec4e2 GKH |
55 | /** |
56 | * VmbusGetChannelOffers - Retrieve the channel offers from the parent partition | |
57 | */ | |
58 | static void VmbusGetChannelOffers(void) | |
3e7ee490 HJ |
59 | { |
60 | DPRINT_ENTER(VMBUS); | |
61 | VmbusChannelRequestOffers(); | |
62 | DPRINT_EXIT(VMBUS); | |
63 | } | |
64 | ||
772ec4e2 GKH |
65 | /** |
66 | * VmbusGetChannelInterface - Get the channel interface | |
67 | */ | |
ee3d7ddf | 68 | static void VmbusGetChannelInterface(struct vmbus_channel_interface *Interface) |
3e7ee490 HJ |
69 | { |
70 | GetChannelInterface(Interface); | |
71 | } | |
72 | ||
772ec4e2 GKH |
73 | /** |
74 | * VmbusGetChannelInfo - Get the device info for the specified device object | |
75 | */ | |
76 | static void VmbusGetChannelInfo(struct hv_device *DeviceObject, | |
77 | struct hv_device_info *DeviceInfo) | |
3e7ee490 HJ |
78 | { |
79 | GetChannelInfo(DeviceObject, DeviceInfo); | |
80 | } | |
81 | ||
772ec4e2 GKH |
82 | /** |
83 | * VmbusCreateChildDevice - Creates the child device on the bus that represents the channel offer | |
84 | */ | |
daaa8cc3 GKH |
85 | struct hv_device *VmbusChildDeviceCreate(struct hv_guid *DeviceType, |
86 | struct hv_guid *DeviceInstance, | |
f346fdc2 | 87 | void *Context) |
3e7ee490 | 88 | { |
ee3d7ddf | 89 | struct vmbus_driver *vmbusDriver = (struct vmbus_driver *)gDriver; |
3e7ee490 | 90 | |
772ec4e2 GKH |
91 | return vmbusDriver->OnChildDeviceCreate(DeviceType, DeviceInstance, |
92 | Context); | |
3e7ee490 HJ |
93 | } |
94 | ||
772ec4e2 GKH |
95 | /** |
96 | * VmbusChildDeviceAdd - Registers the child device with the vmbus | |
97 | */ | |
f346fdc2 | 98 | int VmbusChildDeviceAdd(struct hv_device *ChildDevice) |
3e7ee490 | 99 | { |
ee3d7ddf | 100 | struct vmbus_driver *vmbusDriver = (struct vmbus_driver *)gDriver; |
3e7ee490 HJ |
101 | |
102 | return vmbusDriver->OnChildDeviceAdd(gDevice, ChildDevice); | |
103 | } | |
104 | ||
772ec4e2 GKH |
105 | /** |
106 | * VmbusChildDeviceRemove Unregisters the child device from the vmbus | |
107 | */ | |
f346fdc2 | 108 | void VmbusChildDeviceRemove(struct hv_device *ChildDevice) |
3e7ee490 | 109 | { |
ee3d7ddf | 110 | struct vmbus_driver *vmbusDriver = (struct vmbus_driver *)gDriver; |
3e7ee490 HJ |
111 | |
112 | vmbusDriver->OnChildDeviceRemove(ChildDevice); | |
113 | } | |
114 | ||
772ec4e2 GKH |
115 | /** |
116 | * VmbusOnDeviceAdd - Callback when the root bus device is added | |
117 | */ | |
118 | static int VmbusOnDeviceAdd(struct hv_device *dev, void *AdditionalInfo) | |
3e7ee490 | 119 | { |
772ec4e2 GKH |
120 | u32 *irqvector = AdditionalInfo; |
121 | int ret; | |
3e7ee490 HJ |
122 | |
123 | DPRINT_ENTER(VMBUS); | |
124 | ||
125 | gDevice = dev; | |
126 | ||
caf26a31 | 127 | memcpy(&gDevice->deviceType, &gVmbusDeviceType, sizeof(struct hv_guid)); |
772ec4e2 GKH |
128 | memcpy(&gDevice->deviceInstance, &gVmbusDeviceId, |
129 | sizeof(struct hv_guid)); | |
3e7ee490 | 130 | |
454f18a9 BP |
131 | /* strcpy(dev->name, "vmbus"); */ |
132 | /* SynIC setup... */ | |
7692fd4d | 133 | on_each_cpu(HvSynicInit, (void *)irqvector, 1); |
3e7ee490 | 134 | |
454f18a9 | 135 | /* Connect to VMBus in the root partition */ |
3e7ee490 HJ |
136 | ret = VmbusConnect(); |
137 | ||
454f18a9 | 138 | /* VmbusSendEvent(device->localPortId+1); */ |
3e7ee490 HJ |
139 | DPRINT_EXIT(VMBUS); |
140 | ||
141 | return ret; | |
142 | } | |
143 | ||
772ec4e2 GKH |
144 | /** |
145 | * VmbusOnDeviceRemove - Callback when the root bus device is removed | |
146 | */ | |
147 | static int VmbusOnDeviceRemove(struct hv_device *dev) | |
3e7ee490 | 148 | { |
772ec4e2 | 149 | int ret = 0; |
3e7ee490 HJ |
150 | |
151 | DPRINT_ENTER(VMBUS); | |
3e7ee490 | 152 | VmbusChannelReleaseUnattachedChannels(); |
3e7ee490 | 153 | VmbusDisconnect(); |
7692fd4d | 154 | on_each_cpu(HvSynicCleanup, NULL, 1); |
3e7ee490 HJ |
155 | DPRINT_EXIT(VMBUS); |
156 | ||
157 | return ret; | |
158 | } | |
159 | ||
772ec4e2 GKH |
160 | /** |
161 | * VmbusOnCleanup - Perform any cleanup when the driver is removed | |
162 | */ | |
163 | static void VmbusOnCleanup(struct hv_driver *drv) | |
3e7ee490 | 164 | { |
ee3d7ddf | 165 | /* struct vmbus_driver *driver = (struct vmbus_driver *)drv; */ |
3e7ee490 HJ |
166 | |
167 | DPRINT_ENTER(VMBUS); | |
3e7ee490 | 168 | HvCleanup(); |
3e7ee490 HJ |
169 | DPRINT_EXIT(VMBUS); |
170 | } | |
171 | ||
772ec4e2 GKH |
172 | /** |
173 | * VmbusOnMsgDPC - DPC routine to handle messages from the hypervisior | |
174 | */ | |
175 | static void VmbusOnMsgDPC(struct hv_driver *drv) | |
3e7ee490 | 176 | { |
7692fd4d GKH |
177 | int cpu = smp_processor_id(); |
178 | void *page_addr = gHvContext.synICMessagePage[cpu]; | |
772ec4e2 GKH |
179 | struct hv_message *msg = (struct hv_message *)page_addr + |
180 | VMBUS_MESSAGE_SINT; | |
eacb1b4d | 181 | struct hv_message *copied; |
772ec4e2 GKH |
182 | |
183 | while (1) { | |
184 | if (msg->Header.MessageType == HvMessageTypeNone) { | |
185 | /* no msg */ | |
3e7ee490 | 186 | break; |
772ec4e2 | 187 | } else { |
eacb1b4d | 188 | copied = kmalloc(sizeof(*copied), GFP_ATOMIC); |
3e7ee490 | 189 | if (copied == NULL) |
3e7ee490 | 190 | continue; |
3e7ee490 | 191 | |
eacb1b4d | 192 | memcpy(copied, msg, sizeof(*copied)); |
de65a384 BP |
193 | osd_schedule_callback(gVmbusConnection.WorkQueue, |
194 | VmbusOnChannelMessage, | |
195 | (void *)copied); | |
3e7ee490 HJ |
196 | } |
197 | ||
198 | msg->Header.MessageType = HvMessageTypeNone; | |
199 | ||
454f18a9 BP |
200 | /* |
201 | * Make sure the write to MessageType (ie set to | |
202 | * HvMessageTypeNone) happens before we read the | |
203 | * MessagePending and EOMing. Otherwise, the EOMing | |
204 | * will not deliver any more messages since there is | |
205 | * no empty slot | |
206 | */ | |
28b6ca9c | 207 | mb(); |
3e7ee490 | 208 | |
772ec4e2 | 209 | if (msg->Header.MessageFlags.MessagePending) { |
454f18a9 BP |
210 | /* |
211 | * This will cause message queue rescan to | |
212 | * possibly deliver another msg from the | |
213 | * hypervisor | |
214 | */ | |
a51ed7d6 | 215 | wrmsrl(HV_X64_MSR_EOM, 0); |
3e7ee490 HJ |
216 | } |
217 | } | |
218 | } | |
219 | ||
772ec4e2 GKH |
220 | /** |
221 | * VmbusOnEventDPC - DPC routine to handle events from the hypervisior | |
222 | */ | |
223 | static void VmbusOnEventDPC(struct hv_driver *drv) | |
3e7ee490 | 224 | { |
454f18a9 | 225 | /* TODO: Process any events */ |
3e7ee490 HJ |
226 | VmbusOnEvents(); |
227 | } | |
228 | ||
772ec4e2 GKH |
229 | /** |
230 | * VmbusOnISR - ISR routine | |
231 | */ | |
232 | static int VmbusOnISR(struct hv_driver *drv) | |
3e7ee490 | 233 | { |
772ec4e2 | 234 | int ret = 0; |
7692fd4d | 235 | int cpu = smp_processor_id(); |
3e7ee490 | 236 | void *page_addr; |
eacb1b4d GKH |
237 | struct hv_message *msg; |
238 | union hv_synic_event_flags *event; | |
3e7ee490 | 239 | |
7692fd4d | 240 | page_addr = gHvContext.synICMessagePage[cpu]; |
eacb1b4d | 241 | msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT; |
3e7ee490 HJ |
242 | |
243 | DPRINT_ENTER(VMBUS); | |
244 | ||
454f18a9 | 245 | /* Check if there are actual msgs to be process */ |
772ec4e2 GKH |
246 | if (msg->Header.MessageType != HvMessageTypeNone) { |
247 | DPRINT_DBG(VMBUS, "received msg type %d size %d", | |
248 | msg->Header.MessageType, | |
249 | msg->Header.PayloadSize); | |
3e7ee490 | 250 | ret |= 0x1; |
772ec4e2 | 251 | } |
3e7ee490 | 252 | |
454f18a9 | 253 | /* TODO: Check if there are events to be process */ |
7692fd4d | 254 | page_addr = gHvContext.synICEventPage[cpu]; |
eacb1b4d | 255 | event = (union hv_synic_event_flags *)page_addr + VMBUS_MESSAGE_SINT; |
3e7ee490 | 256 | |
454f18a9 | 257 | /* Since we are a child, we only need to check bit 0 */ |
772ec4e2 | 258 | if (test_and_clear_bit(0, (unsigned long *) &event->Flags32[0])) { |
3e7ee490 HJ |
259 | DPRINT_DBG(VMBUS, "received event %d", event->Flags32[0]); |
260 | ret |= 0x2; | |
261 | } | |
262 | ||
263 | DPRINT_EXIT(VMBUS); | |
264 | return ret; | |
265 | } | |
b3bfb3ce GKH |
266 | |
267 | /** | |
268 | * VmbusInitialize - Main entry point | |
269 | */ | |
270 | int VmbusInitialize(struct hv_driver *drv) | |
271 | { | |
272 | struct vmbus_driver *driver = (struct vmbus_driver *)drv; | |
273 | int ret; | |
274 | ||
275 | DPRINT_ENTER(VMBUS); | |
276 | ||
26c14cc1 HJ |
277 | DPRINT_INFO(VMBUS, "+++++++ HV Driver version = %s +++++++", |
278 | HV_DRV_VERSION); | |
b3bfb3ce GKH |
279 | DPRINT_INFO(VMBUS, "+++++++ Vmbus supported version = %d +++++++", |
280 | VMBUS_REVISION_NUMBER); | |
281 | DPRINT_INFO(VMBUS, "+++++++ Vmbus using SINT %d +++++++", | |
282 | VMBUS_MESSAGE_SINT); | |
283 | DPRINT_DBG(VMBUS, "sizeof(VMBUS_CHANNEL_PACKET_PAGE_BUFFER)=%zd, " | |
284 | "sizeof(VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER)=%zd", | |
285 | sizeof(struct VMBUS_CHANNEL_PACKET_PAGE_BUFFER), | |
286 | sizeof(struct VMBUS_CHANNEL_PACKET_MULITPAGE_BUFFER)); | |
287 | ||
288 | drv->name = gDriverName; | |
289 | memcpy(&drv->deviceType, &gVmbusDeviceType, sizeof(struct hv_guid)); | |
290 | ||
291 | /* Setup dispatch table */ | |
292 | driver->Base.OnDeviceAdd = VmbusOnDeviceAdd; | |
293 | driver->Base.OnDeviceRemove = VmbusOnDeviceRemove; | |
294 | driver->Base.OnCleanup = VmbusOnCleanup; | |
295 | driver->OnIsr = VmbusOnISR; | |
296 | driver->OnMsgDpc = VmbusOnMsgDPC; | |
297 | driver->OnEventDpc = VmbusOnEventDPC; | |
298 | driver->GetChannelOffers = VmbusGetChannelOffers; | |
299 | driver->GetChannelInterface = VmbusGetChannelInterface; | |
300 | driver->GetChannelInfo = VmbusGetChannelInfo; | |
301 | ||
302 | /* Hypervisor initialization...setup hypercall page..etc */ | |
303 | ret = HvInit(); | |
304 | if (ret != 0) | |
305 | DPRINT_ERR(VMBUS, "Unable to initialize the hypervisor - 0x%x", | |
306 | ret); | |
307 | gDriver = drv; | |
308 | ||
309 | DPRINT_EXIT(VMBUS); | |
310 | ||
311 | return ret; | |
312 | } |