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