]>
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 | { | |
e8e27047 | 110 | struct hv_device *hv_dev = device_to_hv_device(dev); |
98db4335 | 111 | struct hv_device_info device_info; |
3e7ee490 | 112 | |
98db4335 | 113 | memset(&device_info, 0, sizeof(struct hv_device_info)); |
3e7ee490 | 114 | |
e8e27047 | 115 | get_channel_info(hv_dev, &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); | |
e8e27047 | 303 | struct hv_device *hv_dev = device_to_hv_device(device); |
b7fc147b S |
304 | |
305 | /* We found our driver ? */ | |
e8e27047 | 306 | if (memcmp(&hv_dev->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)); | |
6de925b1 | 332 | ret = -ENODEV; |
f1f0d67b S |
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)); | |
6de925b1 | 355 | ret = -ENODEV; |
c5dce3db S |
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 | { | |
e8e27047 | 390 | struct hv_device *hv_dev = device_to_hv_device(device); |
086e7a56 | 391 | |
e8e27047 | 392 | kfree(hv_dev); |
086e7a56 S |
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 | */ | |
e826f1d5 | 458 | smp_mb(); |
36199a99 GKH |
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 | 488 | |
36199a99 GKH |
489 | page_addr = hv_context.synic_event_page[cpu]; |
490 | event = (union hv_synic_event_flags *)page_addr + VMBUS_MESSAGE_SINT; | |
491 | ||
492 | /* Since we are a child, we only need to check bit 0 */ | |
32235b07 | 493 | if (sync_test_and_clear_bit(0, (unsigned long *) &event->flags32[0])) |
36199a99 | 494 | ret |= 0x2; |
36199a99 GKH |
495 | |
496 | return ret; | |
497 | } | |
498 | ||
793be9c7 S |
499 | |
500 | static irqreturn_t vmbus_isr(int irq, void *dev_id) | |
501 | { | |
502 | int ret; | |
503 | ||
504 | ret = vmbus_on_isr(); | |
505 | ||
506 | /* Schedules a dpc if necessary */ | |
507 | if (ret > 0) { | |
508 | if (test_bit(0, (unsigned long *)&ret)) | |
59c0e4f0 | 509 | tasklet_schedule(&msg_dpc); |
793be9c7 S |
510 | |
511 | if (test_bit(1, (unsigned long *)&ret)) | |
66d9229c | 512 | tasklet_schedule(&event_dpc); |
793be9c7 S |
513 | |
514 | return IRQ_HANDLED; | |
515 | } else { | |
516 | return IRQ_NONE; | |
517 | } | |
518 | } | |
519 | ||
3e189519 | 520 | /* |
90c9960e GKH |
521 | * vmbus_bus_init -Main vmbus driver initialization routine. |
522 | * | |
523 | * Here, we | |
0686e4f4 | 524 | * - initialize the vmbus driver context |
0686e4f4 LL |
525 | * - invoke the vmbus hv main init routine |
526 | * - get the irq resource | |
0686e4f4 | 527 | * - retrieve the channel offers |
90c9960e | 528 | */ |
9aaa995e | 529 | static int vmbus_bus_init(int irq) |
3e7ee490 | 530 | { |
90c9960e GKH |
531 | int ret; |
532 | unsigned int vector; | |
3e7ee490 | 533 | |
6d26e38f GKH |
534 | /* Hypervisor initialization...setup hypercall page..etc */ |
535 | ret = hv_init(); | |
90c9960e | 536 | if (ret != 0) { |
0a46618d | 537 | pr_err("Unable to initialize the hypervisor - 0x%x\n", ret); |
d6c1c5de | 538 | return ret; |
3e7ee490 HJ |
539 | } |
540 | ||
454f18a9 | 541 | /* Initialize the bus context */ |
59c0e4f0 | 542 | tasklet_init(&msg_dpc, vmbus_on_msg_dpc, 0); |
66d9229c | 543 | tasklet_init(&event_dpc, vmbus_on_event, 0); |
3e7ee490 | 544 | |
a6e4d8e3 | 545 | /* Now, register the bus with LDM */ |
9adcac5c | 546 | ret = bus_register(&hv_bus); |
d6c1c5de S |
547 | if (ret) |
548 | return ret; | |
3e7ee490 | 549 | |
454f18a9 | 550 | /* Get the interrupt resource */ |
3d2de267 | 551 | ret = request_irq(irq, vmbus_isr, IRQF_SAMPLE_RANDOM, |
607c1a11 | 552 | driver_name, hv_acpi_dev); |
3e7ee490 | 553 | |
90c9960e | 554 | if (ret != 0) { |
0a46618d | 555 | pr_err("Unable to request IRQ %d\n", |
9aaa995e | 556 | irq); |
3e7ee490 | 557 | |
9adcac5c | 558 | bus_unregister(&hv_bus); |
3e7ee490 | 559 | |
d6c1c5de | 560 | return ret; |
3e7ee490 | 561 | } |
3e7ee490 | 562 | |
9aaa995e | 563 | vector = IRQ0_VECTOR + irq; |
3e7ee490 | 564 | |
800b6902 S |
565 | /* |
566 | * Notify the hypervisor of our irq and | |
567 | * connect to the host. | |
568 | */ | |
569 | on_each_cpu(hv_synic_init, (void *)&vector, 1); | |
570 | ret = vmbus_connect(); | |
90c9960e | 571 | if (ret) { |
607c1a11 | 572 | free_irq(irq, hv_acpi_dev); |
9adcac5c | 573 | bus_unregister(&hv_bus); |
d6c1c5de | 574 | return ret; |
5d48a1c2 BP |
575 | } |
576 | ||
800b6902 | 577 | |
2d6e882b | 578 | vmbus_request_offers(); |
8b5d6d3b | 579 | |
d6c1c5de | 580 | return 0; |
3e7ee490 HJ |
581 | } |
582 | ||
90c9960e | 583 | /** |
3e189519 | 584 | * vmbus_child_driver_register() - Register a vmbus's child driver |
c643269d | 585 | * @drv: Pointer to driver structure you want to register |
3e189519 | 586 | * |
3e189519 HJ |
587 | * |
588 | * Registers the given driver with Linux through the 'driver_register()' call | |
589 | * And sets up the hyper-v vmbus handling for this driver. | |
590 | * It will return the state of the 'driver_register()' call. | |
591 | * | |
592 | * Mainly used by Hyper-V drivers. | |
90c9960e | 593 | */ |
c643269d | 594 | int vmbus_child_driver_register(struct device_driver *drv) |
3e7ee490 | 595 | { |
5d48a1c2 | 596 | int ret; |
3e7ee490 | 597 | |
0a46618d | 598 | pr_info("child driver registering - name %s\n", drv->name); |
3e7ee490 | 599 | |
454f18a9 | 600 | /* The child driver on this vmbus */ |
9adcac5c | 601 | drv->bus = &hv_bus; |
3e7ee490 | 602 | |
c643269d | 603 | ret = driver_register(drv); |
3e7ee490 | 604 | |
2d6e882b | 605 | vmbus_request_offers(); |
3e7ee490 | 606 | |
5d48a1c2 | 607 | return ret; |
3e7ee490 | 608 | } |
3e7ee490 HJ |
609 | EXPORT_SYMBOL(vmbus_child_driver_register); |
610 | ||
90c9960e | 611 | /** |
3e189519 | 612 | * vmbus_child_driver_unregister() - Unregister a vmbus's child driver |
06de23f7 | 613 | * @drv: Pointer to driver structure you want to un-register |
3e189519 | 614 | * |
3e189519 HJ |
615 | * |
616 | * Un-register the given driver with Linux through the 'driver_unregister()' | |
617 | * call. And ungegisters the driver from the Hyper-V vmbus handler. | |
618 | * | |
619 | * Mainly used by Hyper-V drivers. | |
90c9960e | 620 | */ |
06de23f7 | 621 | void vmbus_child_driver_unregister(struct device_driver *drv) |
3e7ee490 | 622 | { |
0a46618d | 623 | pr_info("child driver unregistering - name %s\n", drv->name); |
3e7ee490 | 624 | |
06de23f7 | 625 | driver_unregister(drv); |
3e7ee490 | 626 | |
3e7ee490 | 627 | } |
3e7ee490 HJ |
628 | EXPORT_SYMBOL(vmbus_child_driver_unregister); |
629 | ||
3e189519 HJ |
630 | /* |
631 | * vmbus_child_device_create - Creates and registers a new child device | |
632 | * on the vmbus. | |
90c9960e | 633 | */ |
89733aa9 GKH |
634 | struct hv_device *vmbus_child_device_create(struct hv_guid *type, |
635 | struct hv_guid *instance, | |
636 | struct vmbus_channel *channel) | |
3e7ee490 | 637 | { |
3d3b5518 | 638 | struct hv_device *child_device_obj; |
3e7ee490 | 639 | |
454f18a9 | 640 | /* Allocate the new child device */ |
6bad88da S |
641 | child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL); |
642 | if (!child_device_obj) { | |
0a46618d | 643 | pr_err("Unable to allocate device object for child device\n"); |
3e7ee490 HJ |
644 | return NULL; |
645 | } | |
646 | ||
cae5b843 | 647 | child_device_obj->channel = channel; |
ca623ad3 HZ |
648 | memcpy(&child_device_obj->dev_type, type, sizeof(struct hv_guid)); |
649 | memcpy(&child_device_obj->dev_instance, instance, | |
90c9960e | 650 | sizeof(struct hv_guid)); |
3e7ee490 | 651 | |
3e7ee490 | 652 | |
3e7ee490 HJ |
653 | return child_device_obj; |
654 | } | |
655 | ||
3e189519 | 656 | /* |
e13a0b5a | 657 | * vmbus_child_device_register - Register the child device |
90c9960e | 658 | */ |
3ca07cb0 | 659 | int vmbus_child_device_register(struct hv_device *child_device_obj) |
3e7ee490 | 660 | { |
90c9960e | 661 | int ret = 0; |
6bad88da | 662 | |
f4888417 | 663 | static atomic_t device_num = ATOMIC_INIT(0); |
3e7ee490 | 664 | |
1bb40a25 | 665 | /* Set the device name. Otherwise, device_register() will fail. */ |
6bad88da | 666 | dev_set_name(&child_device_obj->device, "vmbus_0_%d", |
90c9960e | 667 | atomic_inc_return(&device_num)); |
3e7ee490 | 668 | |
454f18a9 | 669 | /* The new device belongs to this bus */ |
9adcac5c | 670 | child_device_obj->device.bus = &hv_bus; /* device->dev.bus; */ |
607c1a11 | 671 | child_device_obj->device.parent = &hv_acpi_dev->dev; |
6bad88da | 672 | child_device_obj->device.release = vmbus_device_release; |
3e7ee490 | 673 | |
90c9960e GKH |
674 | /* |
675 | * Register with the LDM. This will kick off the driver/device | |
676 | * binding...which will eventually call vmbus_match() and vmbus_probe() | |
677 | */ | |
6bad88da | 678 | ret = device_register(&child_device_obj->device); |
3e7ee490 | 679 | |
3e7ee490 | 680 | if (ret) |
0a46618d | 681 | pr_err("Unable to register child device\n"); |
3e7ee490 | 682 | else |
0a46618d HJ |
683 | pr_info("child device %s registered\n", |
684 | dev_name(&child_device_obj->device)); | |
3e7ee490 | 685 | |
3e7ee490 HJ |
686 | return ret; |
687 | } | |
688 | ||
3e189519 HJ |
689 | /* |
690 | * vmbus_child_device_unregister - Remove the specified child device | |
691 | * from the vmbus. | |
90c9960e | 692 | */ |
9d8bd71a | 693 | void vmbus_child_device_unregister(struct hv_device *device_obj) |
3e7ee490 | 694 | { |
90c9960e GKH |
695 | /* |
696 | * Kick off the process of unregistering the device. | |
697 | * This will call vmbus_remove() and eventually vmbus_device_release() | |
698 | */ | |
6bad88da | 699 | device_unregister(&device_obj->device); |
3e7ee490 | 700 | |
0a46618d HJ |
701 | pr_info("child device %s unregistered\n", |
702 | dev_name(&device_obj->device)); | |
3e7ee490 HJ |
703 | } |
704 | ||
3e7ee490 | 705 | |
b0069f43 S |
706 | /* |
707 | * VMBUS is an acpi enumerated device. Get the the IRQ information | |
708 | * from DSDT. | |
709 | */ | |
710 | ||
711 | static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *irq) | |
712 | { | |
713 | ||
714 | if (res->type == ACPI_RESOURCE_TYPE_IRQ) { | |
715 | struct acpi_resource_irq *irqp; | |
716 | irqp = &res->data.irq; | |
717 | ||
718 | *((unsigned int *)irq) = irqp->interrupts[0]; | |
719 | } | |
720 | ||
721 | return AE_OK; | |
722 | } | |
723 | ||
724 | static int vmbus_acpi_add(struct acpi_device *device) | |
725 | { | |
726 | acpi_status result; | |
727 | ||
607c1a11 S |
728 | hv_acpi_dev = device; |
729 | ||
b0069f43 S |
730 | result = |
731 | acpi_walk_resources(device->handle, METHOD_NAME__CRS, | |
732 | vmbus_walk_resources, &irq); | |
733 | ||
734 | if (ACPI_FAILURE(result)) { | |
735 | complete(&probe_event); | |
736 | return -ENODEV; | |
737 | } | |
738 | complete(&probe_event); | |
739 | return 0; | |
740 | } | |
741 | ||
742 | static const struct acpi_device_id vmbus_acpi_device_ids[] = { | |
743 | {"VMBUS", 0}, | |
9d7b18d1 | 744 | {"VMBus", 0}, |
b0069f43 S |
745 | {"", 0}, |
746 | }; | |
747 | MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids); | |
748 | ||
749 | static struct acpi_driver vmbus_acpi_driver = { | |
750 | .name = "vmbus", | |
751 | .ids = vmbus_acpi_device_ids, | |
752 | .ops = { | |
753 | .add = vmbus_acpi_add, | |
754 | }, | |
755 | }; | |
756 | ||
9a775dbd GKH |
757 | /* |
758 | * We use a PCI table to determine if we should autoload this driver This is | |
759 | * needed by distro tools to determine if the hyperv drivers should be | |
760 | * installed and/or configured. We don't do anything else with the table, but | |
761 | * it needs to be present. | |
9a775dbd | 762 | */ |
15f0beb1 | 763 | static const struct pci_device_id microsoft_hv_pci_table[] = { |
9a775dbd GKH |
764 | { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */ |
765 | { 0 } | |
766 | }; | |
767 | MODULE_DEVICE_TABLE(pci, microsoft_hv_pci_table); | |
768 | ||
607c1a11 | 769 | static int __init hv_acpi_init(void) |
1168ac22 | 770 | { |
71a6655d | 771 | int ret; |
b0069f43 S |
772 | |
773 | init_completion(&probe_event); | |
774 | ||
775 | /* | |
776 | * Get irq resources first. | |
777 | */ | |
778 | ||
0246604c S |
779 | ret = acpi_bus_register_driver(&vmbus_acpi_driver); |
780 | ||
b0069f43 S |
781 | if (ret) |
782 | return ret; | |
783 | ||
784 | wait_for_completion(&probe_event); | |
785 | ||
786 | if (irq <= 0) { | |
2da9e1d6 | 787 | acpi_bus_unregister_driver(&vmbus_acpi_driver); |
b0069f43 S |
788 | return -ENODEV; |
789 | } | |
790 | ||
91fd799e S |
791 | ret = vmbus_bus_init(irq); |
792 | if (ret) | |
793 | acpi_bus_unregister_driver(&vmbus_acpi_driver); | |
794 | return ret; | |
1168ac22 S |
795 | } |
796 | ||
1168ac22 | 797 | |
90c9960e | 798 | MODULE_LICENSE("GPL"); |
26c14cc1 | 799 | MODULE_VERSION(HV_DRV_VERSION); |
13399cba | 800 | module_param(vmbus_loglevel, int, S_IRUGO|S_IWUSR); |
3e7ee490 | 801 | |
607c1a11 | 802 | module_init(hv_acpi_init); |