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> | |
b0069f43 | 20 | * K. Y. Srinivasan <kys@microsoft.com> |
52e5c1ce | 21 | * |
3e7ee490 | 22 | */ |
0a46618d HJ |
23 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
24 | ||
3e7ee490 HJ |
25 | #include <linux/init.h> |
26 | #include <linux/module.h> | |
27 | #include <linux/device.h> | |
28 | #include <linux/irq.h> | |
29 | #include <linux/interrupt.h> | |
30 | #include <linux/sysctl.h> | |
9a775dbd | 31 | #include <linux/pci.h> |
c22090fa | 32 | #include <linux/dmi.h> |
5a0e3ad6 | 33 | #include <linux/slab.h> |
b0069f43 S |
34 | #include <linux/acpi.h> |
35 | #include <acpi/acpi_bus.h> | |
8b5d6d3b | 36 | #include <linux/completion.h> |
3f335ea2 S |
37 | |
38 | #include "hyperv.h" | |
0f2a6619 | 39 | #include "hyperv_vmbus.h" |
3e7ee490 | 40 | |
3e7ee490 | 41 | |
607c1a11 | 42 | static struct acpi_device *hv_acpi_dev; |
1168ac22 | 43 | |
59c0e4f0 | 44 | static struct tasklet_struct msg_dpc; |
66d9229c | 45 | static struct tasklet_struct event_dpc; |
59c0e4f0 | 46 | |
98db4335 S |
47 | unsigned int vmbus_loglevel = (ALL_MODULES << 16 | INFO_LVL); |
48 | EXPORT_SYMBOL(vmbus_loglevel); | |
49 | /* (ALL_MODULES << 16 | DEBUG_LVL_ENTEREXIT); */ | |
50 | /* (((VMBUS | VMBUS_DRV)<<16) | DEBUG_LVL_ENTEREXIT); */ | |
51 | ||
71a6655d | 52 | static struct completion probe_event; |
b0069f43 | 53 | static int irq; |
98db4335 S |
54 | |
55 | static void get_channel_info(struct hv_device *device, | |
56 | struct hv_device_info *info) | |
57 | { | |
58 | struct vmbus_channel_debug_info debug_info; | |
59 | ||
60 | if (!device->channel) | |
61 | return; | |
62 | ||
63 | vmbus_get_debug_info(device->channel, &debug_info); | |
64 | ||
65 | info->chn_id = debug_info.relid; | |
66 | info->chn_state = debug_info.state; | |
67 | memcpy(&info->chn_type, &debug_info.interfacetype, | |
68 | sizeof(struct hv_guid)); | |
69 | memcpy(&info->chn_instance, &debug_info.interface_instance, | |
70 | sizeof(struct hv_guid)); | |
71 | ||
72 | info->monitor_id = debug_info.monitorid; | |
73 | ||
74 | info->server_monitor_pending = debug_info.servermonitor_pending; | |
75 | info->server_monitor_latency = debug_info.servermonitor_latency; | |
76 | info->server_monitor_conn_id = debug_info.servermonitor_connectionid; | |
77 | ||
78 | info->client_monitor_pending = debug_info.clientmonitor_pending; | |
79 | info->client_monitor_latency = debug_info.clientmonitor_latency; | |
80 | info->client_monitor_conn_id = debug_info.clientmonitor_connectionid; | |
81 | ||
82 | info->inbound.int_mask = debug_info.inbound.current_interrupt_mask; | |
83 | info->inbound.read_idx = debug_info.inbound.current_read_index; | |
84 | info->inbound.write_idx = debug_info.inbound.current_write_index; | |
85 | info->inbound.bytes_avail_toread = | |
86 | debug_info.inbound.bytes_avail_toread; | |
87 | info->inbound.bytes_avail_towrite = | |
88 | debug_info.inbound.bytes_avail_towrite; | |
3e7ee490 | 89 | |
98db4335 S |
90 | info->outbound.int_mask = |
91 | debug_info.outbound.current_interrupt_mask; | |
92 | info->outbound.read_idx = debug_info.outbound.current_read_index; | |
93 | info->outbound.write_idx = debug_info.outbound.current_write_index; | |
94 | info->outbound.bytes_avail_toread = | |
95 | debug_info.outbound.bytes_avail_toread; | |
96 | info->outbound.bytes_avail_towrite = | |
97 | debug_info.outbound.bytes_avail_towrite; | |
98 | } | |
3e7ee490 | 99 | |
98db4335 S |
100 | /* |
101 | * vmbus_show_device_attr - Show the device attribute in sysfs. | |
102 | * | |
103 | * This is invoked when user does a | |
104 | * "cat /sys/bus/vmbus/devices/<busdevice>/<attr name>" | |
105 | */ | |
90c9960e GKH |
106 | static ssize_t vmbus_show_device_attr(struct device *dev, |
107 | struct device_attribute *dev_attr, | |
98db4335 S |
108 | char *buf) |
109 | { | |
110 | struct hv_device *device_ctx = device_to_hv_device(dev); | |
111 | struct hv_device_info device_info; | |
3e7ee490 | 112 | |
98db4335 | 113 | memset(&device_info, 0, sizeof(struct hv_device_info)); |
3e7ee490 | 114 | |
98db4335 | 115 | get_channel_info(device_ctx, &device_info); |
3e7ee490 | 116 | |
98db4335 S |
117 | if (!strcmp(dev_attr->attr.name, "class_id")) { |
118 | return sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-" | |
119 | "%02x%02x%02x%02x%02x%02x%02x%02x}\n", | |
120 | device_info.chn_type.data[3], | |
121 | device_info.chn_type.data[2], | |
122 | device_info.chn_type.data[1], | |
123 | device_info.chn_type.data[0], | |
124 | device_info.chn_type.data[5], | |
125 | device_info.chn_type.data[4], | |
126 | device_info.chn_type.data[7], | |
127 | device_info.chn_type.data[6], | |
128 | device_info.chn_type.data[8], | |
129 | device_info.chn_type.data[9], | |
130 | device_info.chn_type.data[10], | |
131 | device_info.chn_type.data[11], | |
132 | device_info.chn_type.data[12], | |
133 | device_info.chn_type.data[13], | |
134 | device_info.chn_type.data[14], | |
135 | device_info.chn_type.data[15]); | |
136 | } else if (!strcmp(dev_attr->attr.name, "device_id")) { | |
137 | return sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-" | |
138 | "%02x%02x%02x%02x%02x%02x%02x%02x}\n", | |
139 | device_info.chn_instance.data[3], | |
140 | device_info.chn_instance.data[2], | |
141 | device_info.chn_instance.data[1], | |
142 | device_info.chn_instance.data[0], | |
143 | device_info.chn_instance.data[5], | |
144 | device_info.chn_instance.data[4], | |
145 | device_info.chn_instance.data[7], | |
146 | device_info.chn_instance.data[6], | |
147 | device_info.chn_instance.data[8], | |
148 | device_info.chn_instance.data[9], | |
149 | device_info.chn_instance.data[10], | |
150 | device_info.chn_instance.data[11], | |
151 | device_info.chn_instance.data[12], | |
152 | device_info.chn_instance.data[13], | |
153 | device_info.chn_instance.data[14], | |
154 | device_info.chn_instance.data[15]); | |
155 | } else if (!strcmp(dev_attr->attr.name, "state")) { | |
156 | return sprintf(buf, "%d\n", device_info.chn_state); | |
157 | } else if (!strcmp(dev_attr->attr.name, "id")) { | |
158 | return sprintf(buf, "%d\n", device_info.chn_id); | |
159 | } else if (!strcmp(dev_attr->attr.name, "out_intr_mask")) { | |
160 | return sprintf(buf, "%d\n", device_info.outbound.int_mask); | |
161 | } else if (!strcmp(dev_attr->attr.name, "out_read_index")) { | |
162 | return sprintf(buf, "%d\n", device_info.outbound.read_idx); | |
163 | } else if (!strcmp(dev_attr->attr.name, "out_write_index")) { | |
164 | return sprintf(buf, "%d\n", device_info.outbound.write_idx); | |
165 | } else if (!strcmp(dev_attr->attr.name, "out_read_bytes_avail")) { | |
166 | return sprintf(buf, "%d\n", | |
167 | device_info.outbound.bytes_avail_toread); | |
168 | } else if (!strcmp(dev_attr->attr.name, "out_write_bytes_avail")) { | |
169 | return sprintf(buf, "%d\n", | |
170 | device_info.outbound.bytes_avail_towrite); | |
171 | } else if (!strcmp(dev_attr->attr.name, "in_intr_mask")) { | |
172 | return sprintf(buf, "%d\n", device_info.inbound.int_mask); | |
173 | } else if (!strcmp(dev_attr->attr.name, "in_read_index")) { | |
174 | return sprintf(buf, "%d\n", device_info.inbound.read_idx); | |
175 | } else if (!strcmp(dev_attr->attr.name, "in_write_index")) { | |
176 | return sprintf(buf, "%d\n", device_info.inbound.write_idx); | |
177 | } else if (!strcmp(dev_attr->attr.name, "in_read_bytes_avail")) { | |
178 | return sprintf(buf, "%d\n", | |
179 | device_info.inbound.bytes_avail_toread); | |
180 | } else if (!strcmp(dev_attr->attr.name, "in_write_bytes_avail")) { | |
181 | return sprintf(buf, "%d\n", | |
182 | device_info.inbound.bytes_avail_towrite); | |
183 | } else if (!strcmp(dev_attr->attr.name, "monitor_id")) { | |
184 | return sprintf(buf, "%d\n", device_info.monitor_id); | |
185 | } else if (!strcmp(dev_attr->attr.name, "server_monitor_pending")) { | |
186 | return sprintf(buf, "%d\n", device_info.server_monitor_pending); | |
187 | } else if (!strcmp(dev_attr->attr.name, "server_monitor_latency")) { | |
188 | return sprintf(buf, "%d\n", device_info.server_monitor_latency); | |
189 | } else if (!strcmp(dev_attr->attr.name, "server_monitor_conn_id")) { | |
190 | return sprintf(buf, "%d\n", | |
191 | device_info.server_monitor_conn_id); | |
192 | } else if (!strcmp(dev_attr->attr.name, "client_monitor_pending")) { | |
193 | return sprintf(buf, "%d\n", device_info.client_monitor_pending); | |
194 | } else if (!strcmp(dev_attr->attr.name, "client_monitor_latency")) { | |
195 | return sprintf(buf, "%d\n", device_info.client_monitor_latency); | |
196 | } else if (!strcmp(dev_attr->attr.name, "client_monitor_conn_id")) { | |
197 | return sprintf(buf, "%d\n", | |
198 | device_info.client_monitor_conn_id); | |
199 | } else { | |
200 | return 0; | |
201 | } | |
202 | } | |
3e7ee490 | 203 | |
454f18a9 | 204 | /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */ |
3e7ee490 HJ |
205 | static struct device_attribute vmbus_device_attrs[] = { |
206 | __ATTR(id, S_IRUGO, vmbus_show_device_attr, NULL), | |
207 | __ATTR(state, S_IRUGO, vmbus_show_device_attr, NULL), | |
208 | __ATTR(class_id, S_IRUGO, vmbus_show_device_attr, NULL), | |
209 | __ATTR(device_id, S_IRUGO, vmbus_show_device_attr, NULL), | |
210 | __ATTR(monitor_id, S_IRUGO, vmbus_show_device_attr, NULL), | |
211 | ||
212 | __ATTR(server_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL), | |
213 | __ATTR(server_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL), | |
214 | __ATTR(server_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL), | |
215 | ||
216 | __ATTR(client_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL), | |
217 | __ATTR(client_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL), | |
218 | __ATTR(client_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL), | |
219 | ||
220 | __ATTR(out_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL), | |
221 | __ATTR(out_read_index, S_IRUGO, vmbus_show_device_attr, NULL), | |
222 | __ATTR(out_write_index, S_IRUGO, vmbus_show_device_attr, NULL), | |
223 | __ATTR(out_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL), | |
224 | __ATTR(out_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL), | |
225 | ||
226 | __ATTR(in_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL), | |
227 | __ATTR(in_read_index, S_IRUGO, vmbus_show_device_attr, NULL), | |
228 | __ATTR(in_write_index, S_IRUGO, vmbus_show_device_attr, NULL), | |
229 | __ATTR(in_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL), | |
230 | __ATTR(in_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL), | |
231 | __ATTR_NULL | |
232 | }; | |
3e7ee490 | 233 | |
793be9c7 | 234 | |
adde2487 S |
235 | /* |
236 | * vmbus_uevent - add uevent for our device | |
237 | * | |
238 | * This routine is invoked when a device is added or removed on the vmbus to | |
239 | * generate a uevent to udev in the userspace. The udev will then look at its | |
240 | * rule and the uevent generated here to load the appropriate driver | |
241 | */ | |
242 | static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env) | |
243 | { | |
244 | struct hv_device *dev = device_to_hv_device(device); | |
245 | int ret; | |
246 | ||
adde2487 S |
247 | ret = add_uevent_var(env, "VMBUS_DEVICE_CLASS_GUID={" |
248 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-" | |
249 | "%02x%02x%02x%02x%02x%02x%02x%02x}", | |
250 | dev->dev_type.data[3], | |
251 | dev->dev_type.data[2], | |
252 | dev->dev_type.data[1], | |
253 | dev->dev_type.data[0], | |
254 | dev->dev_type.data[5], | |
255 | dev->dev_type.data[4], | |
256 | dev->dev_type.data[7], | |
257 | dev->dev_type.data[6], | |
258 | dev->dev_type.data[8], | |
259 | dev->dev_type.data[9], | |
260 | dev->dev_type.data[10], | |
261 | dev->dev_type.data[11], | |
262 | dev->dev_type.data[12], | |
263 | dev->dev_type.data[13], | |
264 | dev->dev_type.data[14], | |
265 | dev->dev_type.data[15]); | |
266 | ||
267 | if (ret) | |
268 | return ret; | |
269 | ||
270 | ret = add_uevent_var(env, "VMBUS_DEVICE_DEVICE_GUID={" | |
271 | "%02x%02x%02x%02x-%02x%02x-%02x%02x-" | |
272 | "%02x%02x%02x%02x%02x%02x%02x%02x}", | |
273 | dev->dev_instance.data[3], | |
274 | dev->dev_instance.data[2], | |
275 | dev->dev_instance.data[1], | |
276 | dev->dev_instance.data[0], | |
277 | dev->dev_instance.data[5], | |
278 | dev->dev_instance.data[4], | |
279 | dev->dev_instance.data[7], | |
280 | dev->dev_instance.data[6], | |
281 | dev->dev_instance.data[8], | |
282 | dev->dev_instance.data[9], | |
283 | dev->dev_instance.data[10], | |
284 | dev->dev_instance.data[11], | |
285 | dev->dev_instance.data[12], | |
286 | dev->dev_instance.data[13], | |
287 | dev->dev_instance.data[14], | |
288 | dev->dev_instance.data[15]); | |
289 | if (ret) | |
290 | return ret; | |
291 | ||
292 | return 0; | |
293 | } | |
294 | ||
b7fc147b S |
295 | |
296 | /* | |
297 | * vmbus_match - Attempt to match the specified device to the specified driver | |
298 | */ | |
299 | static int vmbus_match(struct device *device, struct device_driver *driver) | |
300 | { | |
301 | int match = 0; | |
302 | struct hv_driver *drv = drv_to_hv_drv(driver); | |
303 | struct hv_device *device_ctx = device_to_hv_device(device); | |
304 | ||
305 | /* We found our driver ? */ | |
306 | if (memcmp(&device_ctx->dev_type, &drv->dev_type, | |
de632a2b | 307 | sizeof(struct hv_guid)) == 0) |
b7fc147b | 308 | match = 1; |
de632a2b | 309 | |
b7fc147b S |
310 | return match; |
311 | } | |
312 | ||
f1f0d67b S |
313 | /* |
314 | * vmbus_probe - Add the new vmbus's child device | |
315 | */ | |
316 | static int vmbus_probe(struct device *child_device) | |
317 | { | |
318 | int ret = 0; | |
319 | struct hv_driver *drv = | |
320 | drv_to_hv_drv(child_device->driver); | |
9efd21e1 | 321 | struct hv_device *dev = device_to_hv_device(child_device); |
f1f0d67b | 322 | |
9efd21e1 S |
323 | if (drv->probe) { |
324 | ret = drv->probe(dev); | |
b14a7b30 | 325 | if (ret != 0) |
0a46618d HJ |
326 | pr_err("probe failed for device %s (%d)\n", |
327 | dev_name(child_device), ret); | |
f1f0d67b | 328 | |
f1f0d67b | 329 | } else { |
0a46618d HJ |
330 | pr_err("probe not set for driver %s\n", |
331 | dev_name(child_device)); | |
f1f0d67b S |
332 | ret = -1; |
333 | } | |
334 | return ret; | |
335 | } | |
336 | ||
c5dce3db S |
337 | /* |
338 | * vmbus_remove - Remove a vmbus device | |
339 | */ | |
340 | static int vmbus_remove(struct device *child_device) | |
341 | { | |
342 | int ret; | |
343 | struct hv_driver *drv; | |
344 | ||
415b023a | 345 | struct hv_device *dev = device_to_hv_device(child_device); |
c5dce3db S |
346 | |
347 | if (child_device->driver) { | |
348 | drv = drv_to_hv_drv(child_device->driver); | |
349 | ||
415b023a S |
350 | if (drv->remove) { |
351 | ret = drv->remove(dev); | |
c5dce3db | 352 | } else { |
0a46618d HJ |
353 | pr_err("remove not set for driver %s\n", |
354 | dev_name(child_device)); | |
c5dce3db S |
355 | ret = -1; |
356 | } | |
357 | } | |
358 | ||
359 | return 0; | |
360 | } | |
361 | ||
eb1bb259 S |
362 | |
363 | /* | |
364 | * vmbus_shutdown - Shutdown a vmbus device | |
365 | */ | |
366 | static void vmbus_shutdown(struct device *child_device) | |
367 | { | |
368 | struct hv_driver *drv; | |
ca6887fb | 369 | struct hv_device *dev = device_to_hv_device(child_device); |
eb1bb259 S |
370 | |
371 | ||
372 | /* The device may not be attached yet */ | |
373 | if (!child_device->driver) | |
374 | return; | |
375 | ||
376 | drv = drv_to_hv_drv(child_device->driver); | |
377 | ||
ca6887fb S |
378 | if (drv->shutdown) |
379 | drv->shutdown(dev); | |
eb1bb259 S |
380 | |
381 | return; | |
382 | } | |
383 | ||
086e7a56 S |
384 | |
385 | /* | |
386 | * vmbus_device_release - Final callback release of the vmbus child device | |
387 | */ | |
388 | static void vmbus_device_release(struct device *device) | |
389 | { | |
390 | struct hv_device *device_ctx = device_to_hv_device(device); | |
391 | ||
392 | kfree(device_ctx); | |
393 | ||
394 | } | |
395 | ||
454f18a9 | 396 | /* The one and only one */ |
9adcac5c S |
397 | static struct bus_type hv_bus = { |
398 | .name = "vmbus", | |
399 | .match = vmbus_match, | |
400 | .shutdown = vmbus_shutdown, | |
401 | .remove = vmbus_remove, | |
402 | .probe = vmbus_probe, | |
403 | .uevent = vmbus_uevent, | |
404 | .dev_attrs = vmbus_device_attrs, | |
3e7ee490 HJ |
405 | }; |
406 | ||
adf874cb | 407 | static const char *driver_name = "hyperv"; |
36199a99 | 408 | |
36199a99 | 409 | |
bf6506f6 TT |
410 | struct onmessage_work_context { |
411 | struct work_struct work; | |
412 | struct hv_message msg; | |
413 | }; | |
414 | ||
415 | static void vmbus_onmessage_work(struct work_struct *work) | |
416 | { | |
417 | struct onmessage_work_context *ctx; | |
418 | ||
419 | ctx = container_of(work, struct onmessage_work_context, | |
420 | work); | |
421 | vmbus_onmessage(&ctx->msg); | |
422 | kfree(ctx); | |
423 | } | |
424 | ||
36199a99 GKH |
425 | /* |
426 | * vmbus_on_msg_dpc - DPC routine to handle messages from the hypervisior | |
427 | */ | |
62c1059d | 428 | static void vmbus_on_msg_dpc(unsigned long data) |
36199a99 GKH |
429 | { |
430 | int cpu = smp_processor_id(); | |
431 | void *page_addr = hv_context.synic_message_page[cpu]; | |
432 | struct hv_message *msg = (struct hv_message *)page_addr + | |
433 | VMBUS_MESSAGE_SINT; | |
bf6506f6 | 434 | struct onmessage_work_context *ctx; |
36199a99 GKH |
435 | |
436 | while (1) { | |
437 | if (msg->header.message_type == HVMSG_NONE) { | |
438 | /* no msg */ | |
439 | break; | |
440 | } else { | |
bf6506f6 TT |
441 | ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC); |
442 | if (ctx == NULL) | |
36199a99 | 443 | continue; |
bf6506f6 TT |
444 | INIT_WORK(&ctx->work, vmbus_onmessage_work); |
445 | memcpy(&ctx->msg, msg, sizeof(*msg)); | |
da9fcb72 | 446 | queue_work(vmbus_connection.work_queue, &ctx->work); |
36199a99 GKH |
447 | } |
448 | ||
449 | msg->header.message_type = HVMSG_NONE; | |
450 | ||
451 | /* | |
452 | * Make sure the write to MessageType (ie set to | |
453 | * HVMSG_NONE) happens before we read the | |
454 | * MessagePending and EOMing. Otherwise, the EOMing | |
455 | * will not deliver any more messages since there is | |
456 | * no empty slot | |
457 | */ | |
458 | mb(); | |
459 | ||
460 | if (msg->header.message_flags.msg_pending) { | |
461 | /* | |
462 | * This will cause message queue rescan to | |
463 | * possibly deliver another msg from the | |
464 | * hypervisor | |
465 | */ | |
466 | wrmsrl(HV_X64_MSR_EOM, 0); | |
467 | } | |
468 | } | |
469 | } | |
470 | ||
36199a99 GKH |
471 | /* |
472 | * vmbus_on_isr - ISR routine | |
473 | */ | |
480ae58d | 474 | static int vmbus_on_isr(void) |
36199a99 GKH |
475 | { |
476 | int ret = 0; | |
477 | int cpu = smp_processor_id(); | |
478 | void *page_addr; | |
479 | struct hv_message *msg; | |
480 | union hv_synic_event_flags *event; | |
481 | ||
482 | page_addr = hv_context.synic_message_page[cpu]; | |
483 | msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT; | |
484 | ||
485 | /* Check if there are actual msgs to be process */ | |
98e08702 | 486 | if (msg->header.message_type != HVMSG_NONE) |
36199a99 | 487 | ret |= 0x1; |
36199a99 GKH |
488 | |
489 | /* TODO: Check if there are events to be process */ | |
490 | page_addr = hv_context.synic_event_page[cpu]; | |
491 | event = (union hv_synic_event_flags *)page_addr + VMBUS_MESSAGE_SINT; | |
492 | ||
493 | /* Since we are a child, we only need to check bit 0 */ | |
32235b07 | 494 | if (sync_test_and_clear_bit(0, (unsigned long *) &event->flags32[0])) |
36199a99 | 495 | ret |= 0x2; |
36199a99 GKH |
496 | |
497 | return ret; | |
498 | } | |
499 | ||
793be9c7 S |
500 | |
501 | static irqreturn_t vmbus_isr(int irq, void *dev_id) | |
502 | { | |
503 | int ret; | |
504 | ||
505 | ret = vmbus_on_isr(); | |
506 | ||
507 | /* Schedules a dpc if necessary */ | |
508 | if (ret > 0) { | |
509 | if (test_bit(0, (unsigned long *)&ret)) | |
59c0e4f0 | 510 | tasklet_schedule(&msg_dpc); |
793be9c7 S |
511 | |
512 | if (test_bit(1, (unsigned long *)&ret)) | |
66d9229c | 513 | tasklet_schedule(&event_dpc); |
793be9c7 S |
514 | |
515 | return IRQ_HANDLED; | |
516 | } else { | |
517 | return IRQ_NONE; | |
518 | } | |
519 | } | |
520 | ||
3e189519 | 521 | /* |
90c9960e GKH |
522 | * vmbus_bus_init -Main vmbus driver initialization routine. |
523 | * | |
524 | * Here, we | |
0686e4f4 | 525 | * - initialize the vmbus driver context |
0686e4f4 LL |
526 | * - invoke the vmbus hv main init routine |
527 | * - get the irq resource | |
0686e4f4 | 528 | * - retrieve the channel offers |
90c9960e | 529 | */ |
9aaa995e | 530 | static int vmbus_bus_init(int irq) |
3e7ee490 | 531 | { |
90c9960e GKH |
532 | int ret; |
533 | unsigned int vector; | |
3e7ee490 | 534 | |
6d26e38f GKH |
535 | /* Hypervisor initialization...setup hypercall page..etc */ |
536 | ret = hv_init(); | |
90c9960e | 537 | if (ret != 0) { |
0a46618d | 538 | pr_err("Unable to initialize the hypervisor - 0x%x\n", ret); |
3e7ee490 HJ |
539 | goto cleanup; |
540 | } | |
541 | ||
454f18a9 | 542 | /* Initialize the bus context */ |
59c0e4f0 | 543 | tasklet_init(&msg_dpc, vmbus_on_msg_dpc, 0); |
66d9229c | 544 | tasklet_init(&event_dpc, vmbus_on_event, 0); |
3e7ee490 | 545 | |
a6e4d8e3 | 546 | /* Now, register the bus with LDM */ |
9adcac5c | 547 | ret = bus_register(&hv_bus); |
90c9960e | 548 | if (ret) { |
c19fbca3 BP |
549 | ret = -1; |
550 | goto cleanup; | |
551 | } | |
3e7ee490 | 552 | |
454f18a9 | 553 | /* Get the interrupt resource */ |
3d2de267 | 554 | ret = request_irq(irq, vmbus_isr, IRQF_SAMPLE_RANDOM, |
607c1a11 | 555 | driver_name, hv_acpi_dev); |
3e7ee490 | 556 | |
90c9960e | 557 | if (ret != 0) { |
0a46618d | 558 | pr_err("Unable to request IRQ %d\n", |
9aaa995e | 559 | irq); |
3e7ee490 | 560 | |
9adcac5c | 561 | bus_unregister(&hv_bus); |
3e7ee490 HJ |
562 | |
563 | ret = -1; | |
564 | goto cleanup; | |
565 | } | |
3e7ee490 | 566 | |
9aaa995e | 567 | vector = IRQ0_VECTOR + irq; |
3e7ee490 | 568 | |
800b6902 S |
569 | /* |
570 | * Notify the hypervisor of our irq and | |
571 | * connect to the host. | |
572 | */ | |
573 | on_each_cpu(hv_synic_init, (void *)&vector, 1); | |
574 | ret = vmbus_connect(); | |
90c9960e | 575 | if (ret) { |
607c1a11 | 576 | free_irq(irq, hv_acpi_dev); |
9adcac5c | 577 | bus_unregister(&hv_bus); |
5d48a1c2 BP |
578 | goto cleanup; |
579 | } | |
580 | ||
800b6902 | 581 | |
2d6e882b | 582 | vmbus_request_offers(); |
8b5d6d3b | 583 | |
3e7ee490 | 584 | cleanup: |
3e7ee490 HJ |
585 | return ret; |
586 | } | |
587 | ||
90c9960e | 588 | /** |
3e189519 | 589 | * vmbus_child_driver_register() - Register a vmbus's child driver |
c643269d | 590 | * @drv: Pointer to driver structure you want to register |
3e189519 | 591 | * |
3e189519 HJ |
592 | * |
593 | * Registers the given driver with Linux through the 'driver_register()' call | |
594 | * And sets up the hyper-v vmbus handling for this driver. | |
595 | * It will return the state of the 'driver_register()' call. | |
596 | * | |
597 | * Mainly used by Hyper-V drivers. | |
90c9960e | 598 | */ |
c643269d | 599 | int vmbus_child_driver_register(struct device_driver *drv) |
3e7ee490 | 600 | { |
5d48a1c2 | 601 | int ret; |
3e7ee490 | 602 | |
0a46618d | 603 | pr_info("child driver registering - name %s\n", drv->name); |
3e7ee490 | 604 | |
454f18a9 | 605 | /* The child driver on this vmbus */ |
9adcac5c | 606 | drv->bus = &hv_bus; |
3e7ee490 | 607 | |
c643269d | 608 | ret = driver_register(drv); |
3e7ee490 | 609 | |
2d6e882b | 610 | vmbus_request_offers(); |
3e7ee490 | 611 | |
5d48a1c2 | 612 | return ret; |
3e7ee490 | 613 | } |
3e7ee490 HJ |
614 | EXPORT_SYMBOL(vmbus_child_driver_register); |
615 | ||
90c9960e | 616 | /** |
3e189519 | 617 | * vmbus_child_driver_unregister() - Unregister a vmbus's child driver |
06de23f7 | 618 | * @drv: Pointer to driver structure you want to un-register |
3e189519 | 619 | * |
3e189519 HJ |
620 | * |
621 | * Un-register the given driver with Linux through the 'driver_unregister()' | |
622 | * call. And ungegisters the driver from the Hyper-V vmbus handler. | |
623 | * | |
624 | * Mainly used by Hyper-V drivers. | |
90c9960e | 625 | */ |
06de23f7 | 626 | void vmbus_child_driver_unregister(struct device_driver *drv) |
3e7ee490 | 627 | { |
0a46618d | 628 | pr_info("child driver unregistering - name %s\n", drv->name); |
3e7ee490 | 629 | |
06de23f7 | 630 | driver_unregister(drv); |
3e7ee490 | 631 | |
3e7ee490 | 632 | } |
3e7ee490 HJ |
633 | EXPORT_SYMBOL(vmbus_child_driver_unregister); |
634 | ||
3e189519 HJ |
635 | /* |
636 | * vmbus_child_device_create - Creates and registers a new child device | |
637 | * on the vmbus. | |
90c9960e | 638 | */ |
89733aa9 GKH |
639 | struct hv_device *vmbus_child_device_create(struct hv_guid *type, |
640 | struct hv_guid *instance, | |
641 | struct vmbus_channel *channel) | |
3e7ee490 | 642 | { |
3d3b5518 | 643 | struct hv_device *child_device_obj; |
3e7ee490 | 644 | |
454f18a9 | 645 | /* Allocate the new child device */ |
6bad88da S |
646 | child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL); |
647 | if (!child_device_obj) { | |
0a46618d | 648 | pr_err("Unable to allocate device object for child device\n"); |
3e7ee490 HJ |
649 | return NULL; |
650 | } | |
651 | ||
cae5b843 | 652 | child_device_obj->channel = channel; |
ca623ad3 HZ |
653 | memcpy(&child_device_obj->dev_type, type, sizeof(struct hv_guid)); |
654 | memcpy(&child_device_obj->dev_instance, instance, | |
90c9960e | 655 | sizeof(struct hv_guid)); |
3e7ee490 | 656 | |
3e7ee490 | 657 | |
3e7ee490 HJ |
658 | return child_device_obj; |
659 | } | |
660 | ||
3e189519 | 661 | /* |
e13a0b5a | 662 | * vmbus_child_device_register - Register the child device |
90c9960e | 663 | */ |
3ca07cb0 | 664 | int vmbus_child_device_register(struct hv_device *child_device_obj) |
3e7ee490 | 665 | { |
90c9960e | 666 | int ret = 0; |
6bad88da | 667 | |
f4888417 | 668 | static atomic_t device_num = ATOMIC_INIT(0); |
3e7ee490 | 669 | |
1bb40a25 | 670 | /* Set the device name. Otherwise, device_register() will fail. */ |
6bad88da | 671 | dev_set_name(&child_device_obj->device, "vmbus_0_%d", |
90c9960e | 672 | atomic_inc_return(&device_num)); |
3e7ee490 | 673 | |
454f18a9 | 674 | /* The new device belongs to this bus */ |
9adcac5c | 675 | child_device_obj->device.bus = &hv_bus; /* device->dev.bus; */ |
607c1a11 | 676 | child_device_obj->device.parent = &hv_acpi_dev->dev; |
6bad88da | 677 | child_device_obj->device.release = vmbus_device_release; |
3e7ee490 | 678 | |
90c9960e GKH |
679 | /* |
680 | * Register with the LDM. This will kick off the driver/device | |
681 | * binding...which will eventually call vmbus_match() and vmbus_probe() | |
682 | */ | |
6bad88da | 683 | ret = device_register(&child_device_obj->device); |
3e7ee490 | 684 | |
3e7ee490 | 685 | if (ret) |
0a46618d | 686 | pr_err("Unable to register child device\n"); |
3e7ee490 | 687 | else |
0a46618d HJ |
688 | pr_info("child device %s registered\n", |
689 | dev_name(&child_device_obj->device)); | |
3e7ee490 | 690 | |
3e7ee490 HJ |
691 | return ret; |
692 | } | |
693 | ||
3e189519 HJ |
694 | /* |
695 | * vmbus_child_device_unregister - Remove the specified child device | |
696 | * from the vmbus. | |
90c9960e | 697 | */ |
9d8bd71a | 698 | void vmbus_child_device_unregister(struct hv_device *device_obj) |
3e7ee490 | 699 | { |
90c9960e GKH |
700 | /* |
701 | * Kick off the process of unregistering the device. | |
702 | * This will call vmbus_remove() and eventually vmbus_device_release() | |
703 | */ | |
6bad88da | 704 | device_unregister(&device_obj->device); |
3e7ee490 | 705 | |
0a46618d HJ |
706 | pr_info("child device %s unregistered\n", |
707 | dev_name(&device_obj->device)); | |
3e7ee490 HJ |
708 | } |
709 | ||
3e7ee490 | 710 | |
b0069f43 S |
711 | /* |
712 | * VMBUS is an acpi enumerated device. Get the the IRQ information | |
713 | * from DSDT. | |
714 | */ | |
715 | ||
716 | static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *irq) | |
717 | { | |
718 | ||
719 | if (res->type == ACPI_RESOURCE_TYPE_IRQ) { | |
720 | struct acpi_resource_irq *irqp; | |
721 | irqp = &res->data.irq; | |
722 | ||
723 | *((unsigned int *)irq) = irqp->interrupts[0]; | |
724 | } | |
725 | ||
726 | return AE_OK; | |
727 | } | |
728 | ||
729 | static int vmbus_acpi_add(struct acpi_device *device) | |
730 | { | |
731 | acpi_status result; | |
732 | ||
607c1a11 S |
733 | hv_acpi_dev = device; |
734 | ||
b0069f43 S |
735 | result = |
736 | acpi_walk_resources(device->handle, METHOD_NAME__CRS, | |
737 | vmbus_walk_resources, &irq); | |
738 | ||
739 | if (ACPI_FAILURE(result)) { | |
740 | complete(&probe_event); | |
741 | return -ENODEV; | |
742 | } | |
743 | complete(&probe_event); | |
744 | return 0; | |
745 | } | |
746 | ||
747 | static const struct acpi_device_id vmbus_acpi_device_ids[] = { | |
748 | {"VMBUS", 0}, | |
749 | {"", 0}, | |
750 | }; | |
751 | MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids); | |
752 | ||
753 | static struct acpi_driver vmbus_acpi_driver = { | |
754 | .name = "vmbus", | |
755 | .ids = vmbus_acpi_device_ids, | |
756 | .ops = { | |
757 | .add = vmbus_acpi_add, | |
758 | }, | |
759 | }; | |
760 | ||
9a775dbd GKH |
761 | /* |
762 | * We use a PCI table to determine if we should autoload this driver This is | |
763 | * needed by distro tools to determine if the hyperv drivers should be | |
764 | * installed and/or configured. We don't do anything else with the table, but | |
765 | * it needs to be present. | |
9a775dbd | 766 | */ |
15f0beb1 | 767 | static const struct pci_device_id microsoft_hv_pci_table[] = { |
9a775dbd GKH |
768 | { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */ |
769 | { 0 } | |
770 | }; | |
771 | MODULE_DEVICE_TABLE(pci, microsoft_hv_pci_table); | |
772 | ||
607c1a11 | 773 | static int __init hv_acpi_init(void) |
1168ac22 | 774 | { |
71a6655d | 775 | int ret; |
b0069f43 S |
776 | |
777 | init_completion(&probe_event); | |
778 | ||
779 | /* | |
780 | * Get irq resources first. | |
781 | */ | |
782 | ||
0246604c S |
783 | ret = acpi_bus_register_driver(&vmbus_acpi_driver); |
784 | ||
b0069f43 S |
785 | if (ret) |
786 | return ret; | |
787 | ||
788 | wait_for_completion(&probe_event); | |
789 | ||
790 | if (irq <= 0) { | |
2da9e1d6 | 791 | acpi_bus_unregister_driver(&vmbus_acpi_driver); |
b0069f43 S |
792 | return -ENODEV; |
793 | } | |
794 | ||
607c1a11 | 795 | return vmbus_bus_init(irq); |
1168ac22 S |
796 | } |
797 | ||
1168ac22 | 798 | |
90c9960e | 799 | MODULE_LICENSE("GPL"); |
26c14cc1 | 800 | MODULE_VERSION(HV_DRV_VERSION); |
13399cba | 801 | module_param(vmbus_loglevel, int, S_IRUGO|S_IWUSR); |
3e7ee490 | 802 | |
607c1a11 | 803 | module_init(hv_acpi_init); |