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