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