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