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> | |
5a0e3ad6 | 31 | #include <linux/slab.h> |
b0069f43 S |
32 | #include <linux/acpi.h> |
33 | #include <acpi/acpi_bus.h> | |
8b5d6d3b | 34 | #include <linux/completion.h> |
46a97191 | 35 | #include <linux/hyperv.h> |
b0209501 | 36 | #include <linux/kernel_stat.h> |
407dd164 | 37 | #include <asm/hyperv.h> |
1f94ea81 | 38 | #include <asm/hypervisor.h> |
302a3c0f | 39 | #include <asm/mshyperv.h> |
0f2a6619 | 40 | #include "hyperv_vmbus.h" |
3e7ee490 | 41 | |
3e7ee490 | 42 | |
607c1a11 | 43 | static struct acpi_device *hv_acpi_dev; |
1168ac22 | 44 | |
59c0e4f0 | 45 | static struct tasklet_struct msg_dpc; |
71a6655d | 46 | static struct completion probe_event; |
b0069f43 | 47 | static int irq; |
98db4335 | 48 | |
15b80d64 | 49 | struct hv_device_info { |
15b80d64 GKH |
50 | uuid_le chn_type; |
51 | uuid_le chn_instance; | |
52 | ||
53 | u32 monitor_id; | |
54 | u32 server_monitor_pending; | |
55 | u32 server_monitor_latency; | |
56 | u32 server_monitor_conn_id; | |
57 | u32 client_monitor_pending; | |
58 | u32 client_monitor_latency; | |
59 | u32 client_monitor_conn_id; | |
60 | ||
61 | struct hv_dev_port_info inbound; | |
62 | struct hv_dev_port_info outbound; | |
63 | }; | |
64 | ||
cf6a2eac S |
65 | static int vmbus_exists(void) |
66 | { | |
67 | if (hv_acpi_dev == NULL) | |
68 | return -ENODEV; | |
69 | ||
70 | return 0; | |
71 | } | |
72 | ||
15b80d64 | 73 | |
98db4335 S |
74 | static void get_channel_info(struct hv_device *device, |
75 | struct hv_device_info *info) | |
76 | { | |
77 | struct vmbus_channel_debug_info debug_info; | |
78 | ||
79 | if (!device->channel) | |
80 | return; | |
81 | ||
82 | vmbus_get_debug_info(device->channel, &debug_info); | |
83 | ||
98db4335 | 84 | memcpy(&info->chn_type, &debug_info.interfacetype, |
358d2ee2 | 85 | sizeof(uuid_le)); |
98db4335 | 86 | memcpy(&info->chn_instance, &debug_info.interface_instance, |
358d2ee2 | 87 | sizeof(uuid_le)); |
98db4335 S |
88 | |
89 | info->monitor_id = debug_info.monitorid; | |
90 | ||
91 | info->server_monitor_pending = debug_info.servermonitor_pending; | |
92 | info->server_monitor_latency = debug_info.servermonitor_latency; | |
93 | info->server_monitor_conn_id = debug_info.servermonitor_connectionid; | |
94 | ||
95 | info->client_monitor_pending = debug_info.clientmonitor_pending; | |
96 | info->client_monitor_latency = debug_info.clientmonitor_latency; | |
97 | info->client_monitor_conn_id = debug_info.clientmonitor_connectionid; | |
98 | ||
99 | info->inbound.int_mask = debug_info.inbound.current_interrupt_mask; | |
100 | info->inbound.read_idx = debug_info.inbound.current_read_index; | |
101 | info->inbound.write_idx = debug_info.inbound.current_write_index; | |
102 | info->inbound.bytes_avail_toread = | |
103 | debug_info.inbound.bytes_avail_toread; | |
104 | info->inbound.bytes_avail_towrite = | |
105 | debug_info.inbound.bytes_avail_towrite; | |
3e7ee490 | 106 | |
98db4335 S |
107 | info->outbound.int_mask = |
108 | debug_info.outbound.current_interrupt_mask; | |
109 | info->outbound.read_idx = debug_info.outbound.current_read_index; | |
110 | info->outbound.write_idx = debug_info.outbound.current_write_index; | |
111 | info->outbound.bytes_avail_toread = | |
112 | debug_info.outbound.bytes_avail_toread; | |
113 | info->outbound.bytes_avail_towrite = | |
114 | debug_info.outbound.bytes_avail_towrite; | |
115 | } | |
3e7ee490 | 116 | |
fd776ba9 OH |
117 | #define VMBUS_ALIAS_LEN ((sizeof((struct hv_vmbus_device_id *)0)->guid) * 2) |
118 | static void print_alias_name(struct hv_device *hv_dev, char *alias_name) | |
119 | { | |
120 | int i; | |
121 | for (i = 0; i < VMBUS_ALIAS_LEN; i += 2) | |
122 | sprintf(&alias_name[i], "%02x", hv_dev->dev_type.b[i/2]); | |
123 | } | |
124 | ||
98db4335 S |
125 | /* |
126 | * vmbus_show_device_attr - Show the device attribute in sysfs. | |
127 | * | |
128 | * This is invoked when user does a | |
129 | * "cat /sys/bus/vmbus/devices/<busdevice>/<attr name>" | |
130 | */ | |
90c9960e GKH |
131 | static ssize_t vmbus_show_device_attr(struct device *dev, |
132 | struct device_attribute *dev_attr, | |
98db4335 S |
133 | char *buf) |
134 | { | |
e8e27047 | 135 | struct hv_device *hv_dev = device_to_hv_device(dev); |
7bb52384 | 136 | struct hv_device_info *device_info; |
fd776ba9 | 137 | char alias_name[VMBUS_ALIAS_LEN + 1]; |
7bb52384 | 138 | int ret = 0; |
3e7ee490 | 139 | |
7bb52384 S |
140 | device_info = kzalloc(sizeof(struct hv_device_info), GFP_KERNEL); |
141 | if (!device_info) | |
142 | return ret; | |
3e7ee490 | 143 | |
7bb52384 | 144 | get_channel_info(hv_dev, device_info); |
3e7ee490 | 145 | |
98db4335 | 146 | if (!strcmp(dev_attr->attr.name, "class_id")) { |
2221f6ef | 147 | ret = sprintf(buf, "{%pUl}\n", device_info->chn_type.b); |
98db4335 | 148 | } else if (!strcmp(dev_attr->attr.name, "device_id")) { |
2221f6ef | 149 | ret = sprintf(buf, "{%pUl}\n", device_info->chn_instance.b); |
fd776ba9 OH |
150 | } else if (!strcmp(dev_attr->attr.name, "modalias")) { |
151 | print_alias_name(hv_dev, alias_name); | |
7bb52384 | 152 | ret = sprintf(buf, "vmbus:%s\n", alias_name); |
98db4335 | 153 | } else if (!strcmp(dev_attr->attr.name, "out_intr_mask")) { |
7bb52384 | 154 | ret = sprintf(buf, "%d\n", device_info->outbound.int_mask); |
98db4335 | 155 | } else if (!strcmp(dev_attr->attr.name, "out_read_index")) { |
7bb52384 | 156 | ret = sprintf(buf, "%d\n", device_info->outbound.read_idx); |
98db4335 | 157 | } else if (!strcmp(dev_attr->attr.name, "out_write_index")) { |
7bb52384 | 158 | ret = sprintf(buf, "%d\n", device_info->outbound.write_idx); |
98db4335 | 159 | } else if (!strcmp(dev_attr->attr.name, "out_read_bytes_avail")) { |
7bb52384 S |
160 | ret = sprintf(buf, "%d\n", |
161 | device_info->outbound.bytes_avail_toread); | |
98db4335 | 162 | } else if (!strcmp(dev_attr->attr.name, "out_write_bytes_avail")) { |
7bb52384 S |
163 | ret = sprintf(buf, "%d\n", |
164 | device_info->outbound.bytes_avail_towrite); | |
98db4335 | 165 | } else if (!strcmp(dev_attr->attr.name, "in_intr_mask")) { |
7bb52384 | 166 | ret = sprintf(buf, "%d\n", device_info->inbound.int_mask); |
98db4335 | 167 | } else if (!strcmp(dev_attr->attr.name, "in_read_index")) { |
7bb52384 | 168 | ret = sprintf(buf, "%d\n", device_info->inbound.read_idx); |
98db4335 | 169 | } else if (!strcmp(dev_attr->attr.name, "in_write_index")) { |
7bb52384 | 170 | ret = sprintf(buf, "%d\n", device_info->inbound.write_idx); |
98db4335 | 171 | } else if (!strcmp(dev_attr->attr.name, "in_read_bytes_avail")) { |
7bb52384 S |
172 | ret = sprintf(buf, "%d\n", |
173 | device_info->inbound.bytes_avail_toread); | |
98db4335 | 174 | } else if (!strcmp(dev_attr->attr.name, "in_write_bytes_avail")) { |
7bb52384 S |
175 | ret = sprintf(buf, "%d\n", |
176 | device_info->inbound.bytes_avail_towrite); | |
98db4335 | 177 | } else if (!strcmp(dev_attr->attr.name, "monitor_id")) { |
7bb52384 | 178 | ret = sprintf(buf, "%d\n", device_info->monitor_id); |
98db4335 | 179 | } else if (!strcmp(dev_attr->attr.name, "server_monitor_pending")) { |
7bb52384 | 180 | ret = sprintf(buf, "%d\n", device_info->server_monitor_pending); |
98db4335 | 181 | } else if (!strcmp(dev_attr->attr.name, "server_monitor_latency")) { |
7bb52384 | 182 | ret = sprintf(buf, "%d\n", device_info->server_monitor_latency); |
98db4335 | 183 | } else if (!strcmp(dev_attr->attr.name, "server_monitor_conn_id")) { |
7bb52384 S |
184 | ret = sprintf(buf, "%d\n", |
185 | device_info->server_monitor_conn_id); | |
98db4335 | 186 | } else if (!strcmp(dev_attr->attr.name, "client_monitor_pending")) { |
7bb52384 | 187 | ret = sprintf(buf, "%d\n", device_info->client_monitor_pending); |
98db4335 | 188 | } else if (!strcmp(dev_attr->attr.name, "client_monitor_latency")) { |
7bb52384 | 189 | ret = sprintf(buf, "%d\n", device_info->client_monitor_latency); |
98db4335 | 190 | } else if (!strcmp(dev_attr->attr.name, "client_monitor_conn_id")) { |
7bb52384 S |
191 | ret = sprintf(buf, "%d\n", |
192 | device_info->client_monitor_conn_id); | |
98db4335 | 193 | } |
7bb52384 S |
194 | |
195 | kfree(device_info); | |
196 | return ret; | |
98db4335 | 197 | } |
3e7ee490 | 198 | |
03f3a910 GKH |
199 | static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr, |
200 | char *buf) | |
201 | { | |
202 | struct hv_device *hv_dev = device_to_hv_device(dev); | |
203 | ||
204 | if (!hv_dev->channel) | |
205 | return -ENODEV; | |
206 | return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid); | |
207 | } | |
208 | static DEVICE_ATTR_RO(id); | |
209 | ||
a8fb5f3d GKH |
210 | static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr, |
211 | char *buf) | |
212 | { | |
213 | struct hv_device *hv_dev = device_to_hv_device(dev); | |
214 | ||
215 | if (!hv_dev->channel) | |
216 | return -ENODEV; | |
217 | return sprintf(buf, "%d\n", hv_dev->channel->state); | |
218 | } | |
219 | static DEVICE_ATTR_RO(state); | |
220 | ||
03f3a910 GKH |
221 | static struct attribute *vmbus_attrs[] = { |
222 | &dev_attr_id.attr, | |
a8fb5f3d | 223 | &dev_attr_state.attr, |
03f3a910 GKH |
224 | NULL, |
225 | }; | |
226 | ATTRIBUTE_GROUPS(vmbus); | |
227 | ||
454f18a9 | 228 | /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */ |
3e7ee490 | 229 | static struct device_attribute vmbus_device_attrs[] = { |
3e7ee490 HJ |
230 | __ATTR(class_id, S_IRUGO, vmbus_show_device_attr, NULL), |
231 | __ATTR(device_id, S_IRUGO, vmbus_show_device_attr, NULL), | |
232 | __ATTR(monitor_id, S_IRUGO, vmbus_show_device_attr, NULL), | |
fd776ba9 | 233 | __ATTR(modalias, S_IRUGO, vmbus_show_device_attr, NULL), |
3e7ee490 HJ |
234 | |
235 | __ATTR(server_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL), | |
236 | __ATTR(server_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL), | |
237 | __ATTR(server_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL), | |
238 | ||
239 | __ATTR(client_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL), | |
240 | __ATTR(client_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL), | |
241 | __ATTR(client_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL), | |
242 | ||
243 | __ATTR(out_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL), | |
244 | __ATTR(out_read_index, S_IRUGO, vmbus_show_device_attr, NULL), | |
245 | __ATTR(out_write_index, S_IRUGO, vmbus_show_device_attr, NULL), | |
246 | __ATTR(out_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL), | |
247 | __ATTR(out_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL), | |
248 | ||
249 | __ATTR(in_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL), | |
250 | __ATTR(in_read_index, S_IRUGO, vmbus_show_device_attr, NULL), | |
251 | __ATTR(in_write_index, S_IRUGO, vmbus_show_device_attr, NULL), | |
252 | __ATTR(in_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL), | |
253 | __ATTR(in_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL), | |
254 | __ATTR_NULL | |
255 | }; | |
3e7ee490 | 256 | |
793be9c7 | 257 | |
adde2487 S |
258 | /* |
259 | * vmbus_uevent - add uevent for our device | |
260 | * | |
261 | * This routine is invoked when a device is added or removed on the vmbus to | |
262 | * generate a uevent to udev in the userspace. The udev will then look at its | |
263 | * rule and the uevent generated here to load the appropriate driver | |
0ddda660 S |
264 | * |
265 | * The alias string will be of the form vmbus:guid where guid is the string | |
266 | * representation of the device guid (each byte of the guid will be | |
267 | * represented with two hex characters. | |
adde2487 S |
268 | */ |
269 | static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env) | |
270 | { | |
271 | struct hv_device *dev = device_to_hv_device(device); | |
fd776ba9 OH |
272 | int ret; |
273 | char alias_name[VMBUS_ALIAS_LEN + 1]; | |
0ddda660 | 274 | |
fd776ba9 | 275 | print_alias_name(dev, alias_name); |
0ddda660 S |
276 | ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name); |
277 | return ret; | |
adde2487 S |
278 | } |
279 | ||
5841a829 S |
280 | static uuid_le null_guid; |
281 | ||
282 | static inline bool is_null_guid(const __u8 *guid) | |
283 | { | |
284 | if (memcmp(guid, &null_guid, sizeof(uuid_le))) | |
285 | return false; | |
286 | return true; | |
287 | } | |
288 | ||
3037a7b6 S |
289 | /* |
290 | * Return a matching hv_vmbus_device_id pointer. | |
291 | * If there is no match, return NULL. | |
292 | */ | |
293 | static const struct hv_vmbus_device_id *hv_vmbus_get_id( | |
294 | const struct hv_vmbus_device_id *id, | |
295 | __u8 *guid) | |
296 | { | |
297 | for (; !is_null_guid(id->guid); id++) | |
298 | if (!memcmp(&id->guid, guid, sizeof(uuid_le))) | |
299 | return id; | |
300 | ||
301 | return NULL; | |
302 | } | |
303 | ||
304 | ||
b7fc147b S |
305 | |
306 | /* | |
307 | * vmbus_match - Attempt to match the specified device to the specified driver | |
308 | */ | |
309 | static int vmbus_match(struct device *device, struct device_driver *driver) | |
310 | { | |
b7fc147b | 311 | struct hv_driver *drv = drv_to_hv_drv(driver); |
e8e27047 | 312 | struct hv_device *hv_dev = device_to_hv_device(device); |
b7fc147b | 313 | |
3037a7b6 S |
314 | if (hv_vmbus_get_id(drv->id_table, hv_dev->dev_type.b)) |
315 | return 1; | |
de632a2b | 316 | |
5841a829 | 317 | return 0; |
b7fc147b S |
318 | } |
319 | ||
f1f0d67b S |
320 | /* |
321 | * vmbus_probe - Add the new vmbus's child device | |
322 | */ | |
323 | static int vmbus_probe(struct device *child_device) | |
324 | { | |
325 | int ret = 0; | |
326 | struct hv_driver *drv = | |
327 | drv_to_hv_drv(child_device->driver); | |
9efd21e1 | 328 | struct hv_device *dev = device_to_hv_device(child_device); |
84946899 | 329 | const struct hv_vmbus_device_id *dev_id; |
f1f0d67b | 330 | |
84946899 | 331 | dev_id = hv_vmbus_get_id(drv->id_table, dev->dev_type.b); |
9efd21e1 | 332 | if (drv->probe) { |
84946899 | 333 | ret = drv->probe(dev, dev_id); |
b14a7b30 | 334 | if (ret != 0) |
0a46618d HJ |
335 | pr_err("probe failed for device %s (%d)\n", |
336 | dev_name(child_device), ret); | |
f1f0d67b | 337 | |
f1f0d67b | 338 | } else { |
0a46618d HJ |
339 | pr_err("probe not set for driver %s\n", |
340 | dev_name(child_device)); | |
6de925b1 | 341 | ret = -ENODEV; |
f1f0d67b S |
342 | } |
343 | return ret; | |
344 | } | |
345 | ||
c5dce3db S |
346 | /* |
347 | * vmbus_remove - Remove a vmbus device | |
348 | */ | |
349 | static int vmbus_remove(struct device *child_device) | |
350 | { | |
d4372179 | 351 | struct hv_driver *drv = drv_to_hv_drv(child_device->driver); |
415b023a | 352 | struct hv_device *dev = device_to_hv_device(child_device); |
c5dce3db | 353 | |
d4372179 S |
354 | if (drv->remove) |
355 | drv->remove(dev); | |
356 | else | |
357 | pr_err("remove not set for driver %s\n", | |
358 | dev_name(child_device)); | |
c5dce3db S |
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 | { | |
e8e27047 | 391 | struct hv_device *hv_dev = device_to_hv_device(device); |
086e7a56 | 392 | |
e8e27047 | 393 | kfree(hv_dev); |
086e7a56 S |
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, | |
03f3a910 | 406 | .dev_groups = vmbus_groups, |
3e7ee490 HJ |
407 | }; |
408 | ||
adf874cb | 409 | static const char *driver_name = "hyperv"; |
36199a99 | 410 | |
36199a99 | 411 | |
bf6506f6 TT |
412 | struct onmessage_work_context { |
413 | struct work_struct work; | |
414 | struct hv_message msg; | |
415 | }; | |
416 | ||
417 | static void vmbus_onmessage_work(struct work_struct *work) | |
418 | { | |
419 | struct onmessage_work_context *ctx; | |
420 | ||
421 | ctx = container_of(work, struct onmessage_work_context, | |
422 | work); | |
423 | vmbus_onmessage(&ctx->msg); | |
424 | kfree(ctx); | |
425 | } | |
426 | ||
62c1059d | 427 | static void vmbus_on_msg_dpc(unsigned long data) |
36199a99 GKH |
428 | { |
429 | int cpu = smp_processor_id(); | |
430 | void *page_addr = hv_context.synic_message_page[cpu]; | |
431 | struct hv_message *msg = (struct hv_message *)page_addr + | |
432 | VMBUS_MESSAGE_SINT; | |
bf6506f6 | 433 | struct onmessage_work_context *ctx; |
36199a99 GKH |
434 | |
435 | while (1) { | |
436 | if (msg->header.message_type == HVMSG_NONE) { | |
437 | /* no msg */ | |
438 | break; | |
439 | } else { | |
bf6506f6 TT |
440 | ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC); |
441 | if (ctx == NULL) | |
36199a99 | 442 | continue; |
bf6506f6 TT |
443 | INIT_WORK(&ctx->work, vmbus_onmessage_work); |
444 | memcpy(&ctx->msg, msg, sizeof(*msg)); | |
da9fcb72 | 445 | queue_work(vmbus_connection.work_queue, &ctx->work); |
36199a99 GKH |
446 | } |
447 | ||
448 | msg->header.message_type = HVMSG_NONE; | |
449 | ||
450 | /* | |
451 | * Make sure the write to MessageType (ie set to | |
452 | * HVMSG_NONE) happens before we read the | |
453 | * MessagePending and EOMing. Otherwise, the EOMing | |
454 | * will not deliver any more messages since there is | |
455 | * no empty slot | |
456 | */ | |
35848f68 | 457 | mb(); |
36199a99 GKH |
458 | |
459 | if (msg->header.message_flags.msg_pending) { | |
460 | /* | |
461 | * This will cause message queue rescan to | |
462 | * possibly deliver another msg from the | |
463 | * hypervisor | |
464 | */ | |
465 | wrmsrl(HV_X64_MSR_EOM, 0); | |
466 | } | |
467 | } | |
468 | } | |
469 | ||
ae4636e6 | 470 | static irqreturn_t vmbus_isr(int irq, void *dev_id) |
36199a99 | 471 | { |
36199a99 GKH |
472 | int cpu = smp_processor_id(); |
473 | void *page_addr; | |
474 | struct hv_message *msg; | |
475 | union hv_synic_event_flags *event; | |
ae4636e6 | 476 | bool handled = false; |
36199a99 | 477 | |
5ab05951 S |
478 | page_addr = hv_context.synic_event_page[cpu]; |
479 | if (page_addr == NULL) | |
480 | return IRQ_NONE; | |
481 | ||
482 | event = (union hv_synic_event_flags *)page_addr + | |
483 | VMBUS_MESSAGE_SINT; | |
7341d908 S |
484 | /* |
485 | * Check for events before checking for messages. This is the order | |
486 | * in which events and messages are checked in Windows guests on | |
487 | * Hyper-V, and the Windows team suggested we do the same. | |
488 | */ | |
36199a99 | 489 | |
6552ecd7 S |
490 | if ((vmbus_proto_version == VERSION_WS2008) || |
491 | (vmbus_proto_version == VERSION_WIN7)) { | |
36199a99 | 492 | |
6552ecd7 S |
493 | /* Since we are a child, we only need to check bit 0 */ |
494 | if (sync_test_and_clear_bit(0, | |
495 | (unsigned long *) &event->flags32[0])) { | |
496 | handled = true; | |
497 | } | |
498 | } else { | |
499 | /* | |
500 | * Our host is win8 or above. The signaling mechanism | |
501 | * has changed and we can directly look at the event page. | |
502 | * If bit n is set then we have an interrup on the channel | |
503 | * whose id is n. | |
504 | */ | |
ae4636e6 | 505 | handled = true; |
ae4636e6 | 506 | } |
793be9c7 | 507 | |
6552ecd7 | 508 | if (handled) |
db11f12a | 509 | tasklet_schedule(hv_context.event_dpc[cpu]); |
6552ecd7 S |
510 | |
511 | ||
7341d908 S |
512 | page_addr = hv_context.synic_message_page[cpu]; |
513 | msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT; | |
514 | ||
515 | /* Check if there are actual msgs to be processed */ | |
516 | if (msg->header.message_type != HVMSG_NONE) { | |
517 | handled = true; | |
518 | tasklet_schedule(&msg_dpc); | |
519 | } | |
520 | ||
ae4636e6 | 521 | if (handled) |
793be9c7 | 522 | return IRQ_HANDLED; |
ae4636e6 | 523 | else |
793be9c7 | 524 | return IRQ_NONE; |
793be9c7 S |
525 | } |
526 | ||
b0209501 S |
527 | /* |
528 | * vmbus interrupt flow handler: | |
529 | * vmbus interrupts can concurrently occur on multiple CPUs and | |
530 | * can be handled concurrently. | |
531 | */ | |
532 | ||
83ebf6e5 | 533 | static void vmbus_flow_handler(unsigned int irq, struct irq_desc *desc) |
b0209501 S |
534 | { |
535 | kstat_incr_irqs_this_cpu(irq, desc); | |
536 | ||
537 | desc->action->handler(irq, desc->action->dev_id); | |
538 | } | |
539 | ||
3e189519 | 540 | /* |
90c9960e GKH |
541 | * vmbus_bus_init -Main vmbus driver initialization routine. |
542 | * | |
543 | * Here, we | |
0686e4f4 | 544 | * - initialize the vmbus driver context |
0686e4f4 LL |
545 | * - invoke the vmbus hv main init routine |
546 | * - get the irq resource | |
0686e4f4 | 547 | * - retrieve the channel offers |
90c9960e | 548 | */ |
9aaa995e | 549 | static int vmbus_bus_init(int irq) |
3e7ee490 | 550 | { |
90c9960e | 551 | int ret; |
3e7ee490 | 552 | |
6d26e38f GKH |
553 | /* Hypervisor initialization...setup hypercall page..etc */ |
554 | ret = hv_init(); | |
90c9960e | 555 | if (ret != 0) { |
0a46618d | 556 | pr_err("Unable to initialize the hypervisor - 0x%x\n", ret); |
d6c1c5de | 557 | return ret; |
3e7ee490 HJ |
558 | } |
559 | ||
59c0e4f0 | 560 | tasklet_init(&msg_dpc, vmbus_on_msg_dpc, 0); |
3e7ee490 | 561 | |
9adcac5c | 562 | ret = bus_register(&hv_bus); |
d6c1c5de | 563 | if (ret) |
8b9987e9 | 564 | goto err_cleanup; |
3e7ee490 | 565 | |
20ed3ef7 | 566 | ret = request_irq(irq, vmbus_isr, 0, driver_name, hv_acpi_dev); |
3e7ee490 | 567 | |
90c9960e | 568 | if (ret != 0) { |
0a46618d | 569 | pr_err("Unable to request IRQ %d\n", |
9aaa995e | 570 | irq); |
8b9987e9 | 571 | goto err_unregister; |
3e7ee490 | 572 | } |
3e7ee490 | 573 | |
b0209501 S |
574 | /* |
575 | * Vmbus interrupts can be handled concurrently on | |
576 | * different CPUs. Establish an appropriate interrupt flow | |
577 | * handler that can support this model. | |
578 | */ | |
579 | irq_set_handler(irq, vmbus_flow_handler); | |
580 | ||
302a3c0f S |
581 | /* |
582 | * Register our interrupt handler. | |
583 | */ | |
584 | hv_register_vmbus_handler(irq, vmbus_isr); | |
3e7ee490 | 585 | |
2608fb65 JW |
586 | ret = hv_synic_alloc(); |
587 | if (ret) | |
588 | goto err_alloc; | |
800b6902 | 589 | /* |
302a3c0f | 590 | * Initialize the per-cpu interrupt state and |
800b6902 S |
591 | * connect to the host. |
592 | */ | |
302a3c0f | 593 | on_each_cpu(hv_synic_init, NULL, 1); |
800b6902 | 594 | ret = vmbus_connect(); |
8b9987e9 | 595 | if (ret) |
2608fb65 | 596 | goto err_alloc; |
800b6902 | 597 | |
2d6e882b | 598 | vmbus_request_offers(); |
8b5d6d3b | 599 | |
d6c1c5de | 600 | return 0; |
8b9987e9 | 601 | |
2608fb65 JW |
602 | err_alloc: |
603 | hv_synic_free(); | |
8b9987e9 S |
604 | free_irq(irq, hv_acpi_dev); |
605 | ||
606 | err_unregister: | |
607 | bus_unregister(&hv_bus); | |
608 | ||
609 | err_cleanup: | |
610 | hv_cleanup(); | |
611 | ||
612 | return ret; | |
3e7ee490 HJ |
613 | } |
614 | ||
90c9960e | 615 | /** |
768fa219 GKH |
616 | * __vmbus_child_driver_register - Register a vmbus's driver |
617 | * @drv: Pointer to driver structure you want to register | |
618 | * @owner: owner module of the drv | |
619 | * @mod_name: module name string | |
3e189519 HJ |
620 | * |
621 | * Registers the given driver with Linux through the 'driver_register()' call | |
768fa219 | 622 | * and sets up the hyper-v vmbus handling for this driver. |
3e189519 HJ |
623 | * It will return the state of the 'driver_register()' call. |
624 | * | |
90c9960e | 625 | */ |
768fa219 | 626 | int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name) |
3e7ee490 | 627 | { |
5d48a1c2 | 628 | int ret; |
3e7ee490 | 629 | |
768fa219 | 630 | pr_info("registering driver %s\n", hv_driver->name); |
3e7ee490 | 631 | |
cf6a2eac S |
632 | ret = vmbus_exists(); |
633 | if (ret < 0) | |
634 | return ret; | |
635 | ||
768fa219 GKH |
636 | hv_driver->driver.name = hv_driver->name; |
637 | hv_driver->driver.owner = owner; | |
638 | hv_driver->driver.mod_name = mod_name; | |
639 | hv_driver->driver.bus = &hv_bus; | |
3e7ee490 | 640 | |
768fa219 | 641 | ret = driver_register(&hv_driver->driver); |
3e7ee490 | 642 | |
5d48a1c2 | 643 | return ret; |
3e7ee490 | 644 | } |
768fa219 | 645 | EXPORT_SYMBOL_GPL(__vmbus_driver_register); |
3e7ee490 | 646 | |
90c9960e | 647 | /** |
768fa219 GKH |
648 | * vmbus_driver_unregister() - Unregister a vmbus's driver |
649 | * @drv: Pointer to driver structure you want to un-register | |
3e189519 | 650 | * |
768fa219 GKH |
651 | * Un-register the given driver that was previous registered with a call to |
652 | * vmbus_driver_register() | |
90c9960e | 653 | */ |
768fa219 | 654 | void vmbus_driver_unregister(struct hv_driver *hv_driver) |
3e7ee490 | 655 | { |
768fa219 | 656 | pr_info("unregistering driver %s\n", hv_driver->name); |
3e7ee490 | 657 | |
cf6a2eac | 658 | if (!vmbus_exists()) |
8f257a14 | 659 | driver_unregister(&hv_driver->driver); |
3e7ee490 | 660 | } |
768fa219 | 661 | EXPORT_SYMBOL_GPL(vmbus_driver_unregister); |
3e7ee490 | 662 | |
3e189519 | 663 | /* |
f2c73011 | 664 | * vmbus_device_create - Creates and registers a new child device |
3e189519 | 665 | * on the vmbus. |
90c9960e | 666 | */ |
f2c73011 | 667 | struct hv_device *vmbus_device_create(uuid_le *type, |
358d2ee2 | 668 | uuid_le *instance, |
89733aa9 | 669 | struct vmbus_channel *channel) |
3e7ee490 | 670 | { |
3d3b5518 | 671 | struct hv_device *child_device_obj; |
3e7ee490 | 672 | |
6bad88da S |
673 | child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL); |
674 | if (!child_device_obj) { | |
0a46618d | 675 | pr_err("Unable to allocate device object for child device\n"); |
3e7ee490 HJ |
676 | return NULL; |
677 | } | |
678 | ||
cae5b843 | 679 | child_device_obj->channel = channel; |
358d2ee2 | 680 | memcpy(&child_device_obj->dev_type, type, sizeof(uuid_le)); |
ca623ad3 | 681 | memcpy(&child_device_obj->dev_instance, instance, |
358d2ee2 | 682 | sizeof(uuid_le)); |
3e7ee490 | 683 | |
3e7ee490 | 684 | |
3e7ee490 HJ |
685 | return child_device_obj; |
686 | } | |
687 | ||
3e189519 | 688 | /* |
22794281 | 689 | * vmbus_device_register - Register the child device |
90c9960e | 690 | */ |
22794281 | 691 | int vmbus_device_register(struct hv_device *child_device_obj) |
3e7ee490 | 692 | { |
90c9960e | 693 | int ret = 0; |
6bad88da | 694 | |
f4888417 | 695 | static atomic_t device_num = ATOMIC_INIT(0); |
3e7ee490 | 696 | |
6bad88da | 697 | dev_set_name(&child_device_obj->device, "vmbus_0_%d", |
90c9960e | 698 | atomic_inc_return(&device_num)); |
3e7ee490 | 699 | |
0bce28b6 | 700 | child_device_obj->device.bus = &hv_bus; |
607c1a11 | 701 | child_device_obj->device.parent = &hv_acpi_dev->dev; |
6bad88da | 702 | child_device_obj->device.release = vmbus_device_release; |
3e7ee490 | 703 | |
90c9960e GKH |
704 | /* |
705 | * Register with the LDM. This will kick off the driver/device | |
706 | * binding...which will eventually call vmbus_match() and vmbus_probe() | |
707 | */ | |
6bad88da | 708 | ret = device_register(&child_device_obj->device); |
3e7ee490 | 709 | |
3e7ee490 | 710 | if (ret) |
0a46618d | 711 | pr_err("Unable to register child device\n"); |
3e7ee490 | 712 | else |
84672369 | 713 | pr_debug("child device %s registered\n", |
0a46618d | 714 | dev_name(&child_device_obj->device)); |
3e7ee490 | 715 | |
3e7ee490 HJ |
716 | return ret; |
717 | } | |
718 | ||
3e189519 | 719 | /* |
696453ba | 720 | * vmbus_device_unregister - Remove the specified child device |
3e189519 | 721 | * from the vmbus. |
90c9960e | 722 | */ |
696453ba | 723 | void vmbus_device_unregister(struct hv_device *device_obj) |
3e7ee490 | 724 | { |
84672369 FS |
725 | pr_debug("child device %s unregistered\n", |
726 | dev_name(&device_obj->device)); | |
727 | ||
90c9960e GKH |
728 | /* |
729 | * Kick off the process of unregistering the device. | |
730 | * This will call vmbus_remove() and eventually vmbus_device_release() | |
731 | */ | |
6bad88da | 732 | device_unregister(&device_obj->device); |
3e7ee490 HJ |
733 | } |
734 | ||
3e7ee490 | 735 | |
b0069f43 S |
736 | /* |
737 | * VMBUS is an acpi enumerated device. Get the the IRQ information | |
738 | * from DSDT. | |
739 | */ | |
740 | ||
741 | static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *irq) | |
742 | { | |
743 | ||
744 | if (res->type == ACPI_RESOURCE_TYPE_IRQ) { | |
745 | struct acpi_resource_irq *irqp; | |
746 | irqp = &res->data.irq; | |
747 | ||
748 | *((unsigned int *)irq) = irqp->interrupts[0]; | |
749 | } | |
750 | ||
751 | return AE_OK; | |
752 | } | |
753 | ||
754 | static int vmbus_acpi_add(struct acpi_device *device) | |
755 | { | |
756 | acpi_status result; | |
757 | ||
607c1a11 S |
758 | hv_acpi_dev = device; |
759 | ||
0a4425b6 S |
760 | result = acpi_walk_resources(device->handle, METHOD_NAME__CRS, |
761 | vmbus_walk_resources, &irq); | |
b0069f43 S |
762 | |
763 | if (ACPI_FAILURE(result)) { | |
764 | complete(&probe_event); | |
765 | return -ENODEV; | |
766 | } | |
767 | complete(&probe_event); | |
768 | return 0; | |
769 | } | |
770 | ||
771 | static const struct acpi_device_id vmbus_acpi_device_ids[] = { | |
772 | {"VMBUS", 0}, | |
9d7b18d1 | 773 | {"VMBus", 0}, |
b0069f43 S |
774 | {"", 0}, |
775 | }; | |
776 | MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids); | |
777 | ||
778 | static struct acpi_driver vmbus_acpi_driver = { | |
779 | .name = "vmbus", | |
780 | .ids = vmbus_acpi_device_ids, | |
781 | .ops = { | |
782 | .add = vmbus_acpi_add, | |
783 | }, | |
784 | }; | |
785 | ||
607c1a11 | 786 | static int __init hv_acpi_init(void) |
1168ac22 | 787 | { |
2dda95f8 | 788 | int ret, t; |
b0069f43 | 789 | |
1f94ea81 | 790 | if (x86_hyper != &x86_hyper_ms_hyperv) |
0592969e JW |
791 | return -ENODEV; |
792 | ||
b0069f43 S |
793 | init_completion(&probe_event); |
794 | ||
795 | /* | |
796 | * Get irq resources first. | |
797 | */ | |
798 | ||
0246604c S |
799 | ret = acpi_bus_register_driver(&vmbus_acpi_driver); |
800 | ||
b0069f43 S |
801 | if (ret) |
802 | return ret; | |
803 | ||
2dda95f8 S |
804 | t = wait_for_completion_timeout(&probe_event, 5*HZ); |
805 | if (t == 0) { | |
806 | ret = -ETIMEDOUT; | |
807 | goto cleanup; | |
808 | } | |
b0069f43 S |
809 | |
810 | if (irq <= 0) { | |
2dda95f8 S |
811 | ret = -ENODEV; |
812 | goto cleanup; | |
b0069f43 S |
813 | } |
814 | ||
91fd799e S |
815 | ret = vmbus_bus_init(irq); |
816 | if (ret) | |
2dda95f8 S |
817 | goto cleanup; |
818 | ||
819 | return 0; | |
820 | ||
821 | cleanup: | |
822 | acpi_bus_unregister_driver(&vmbus_acpi_driver); | |
cf6a2eac | 823 | hv_acpi_dev = NULL; |
91fd799e | 824 | return ret; |
1168ac22 S |
825 | } |
826 | ||
93e5bd06 S |
827 | static void __exit vmbus_exit(void) |
828 | { | |
829 | ||
830 | free_irq(irq, hv_acpi_dev); | |
831 | vmbus_free_channels(); | |
832 | bus_unregister(&hv_bus); | |
833 | hv_cleanup(); | |
834 | acpi_bus_unregister_driver(&vmbus_acpi_driver); | |
835 | } | |
836 | ||
1168ac22 | 837 | |
90c9960e | 838 | MODULE_LICENSE("GPL"); |
3e7ee490 | 839 | |
43d4e119 | 840 | subsys_initcall(hv_acpi_init); |
93e5bd06 | 841 | module_exit(vmbus_exit); |