Drivers: hv: vmbus: prefer 'die' notification chain to 'panic'
[firefly-linux-kernel-4.4.55.git] / drivers / hv / vmbus_drv.c
1 /*
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>
20  *   K. Y. Srinivasan <kys@microsoft.com>
21  *
22  */
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/device.h>
28 #include <linux/interrupt.h>
29 #include <linux/sysctl.h>
30 #include <linux/slab.h>
31 #include <linux/acpi.h>
32 #include <linux/completion.h>
33 #include <linux/hyperv.h>
34 #include <linux/kernel_stat.h>
35 #include <linux/clockchips.h>
36 #include <linux/cpu.h>
37 #include <asm/hyperv.h>
38 #include <asm/hypervisor.h>
39 #include <asm/mshyperv.h>
40 #include <linux/notifier.h>
41 #include <linux/ptrace.h>
42 #include <linux/kdebug.h>
43 #include "hyperv_vmbus.h"
44
45 static struct acpi_device  *hv_acpi_dev;
46
47 static struct tasklet_struct msg_dpc;
48 static struct completion probe_event;
49 static int irq;
50
51
52 static void hyperv_report_panic(struct pt_regs *regs)
53 {
54         static bool panic_reported;
55
56         /*
57          * We prefer to report panic on 'die' chain as we have proper
58          * registers to report, but if we miss it (e.g. on BUG()) we need
59          * to report it on 'panic'.
60          */
61         if (panic_reported)
62                 return;
63         panic_reported = true;
64
65         wrmsrl(HV_X64_MSR_CRASH_P0, regs->ip);
66         wrmsrl(HV_X64_MSR_CRASH_P1, regs->ax);
67         wrmsrl(HV_X64_MSR_CRASH_P2, regs->bx);
68         wrmsrl(HV_X64_MSR_CRASH_P3, regs->cx);
69         wrmsrl(HV_X64_MSR_CRASH_P4, regs->dx);
70
71         /*
72          * Let Hyper-V know there is crash data available
73          */
74         wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
75 }
76
77 static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
78                               void *args)
79 {
80         struct pt_regs *regs;
81
82         regs = current_pt_regs();
83
84         hyperv_report_panic(regs);
85         return NOTIFY_DONE;
86 }
87
88 static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
89                             void *args)
90 {
91         struct die_args *die = (struct die_args *)args;
92         struct pt_regs *regs = die->regs;
93
94         hyperv_report_panic(regs);
95         return NOTIFY_DONE;
96 }
97
98 static struct notifier_block hyperv_die_block = {
99         .notifier_call = hyperv_die_event,
100 };
101 static struct notifier_block hyperv_panic_block = {
102         .notifier_call = hyperv_panic_event,
103 };
104
105 struct resource hyperv_mmio = {
106         .name  = "hyperv mmio",
107         .flags = IORESOURCE_MEM,
108 };
109 EXPORT_SYMBOL_GPL(hyperv_mmio);
110
111 static int vmbus_exists(void)
112 {
113         if (hv_acpi_dev == NULL)
114                 return -ENODEV;
115
116         return 0;
117 }
118
119 #define VMBUS_ALIAS_LEN ((sizeof((struct hv_vmbus_device_id *)0)->guid) * 2)
120 static void print_alias_name(struct hv_device *hv_dev, char *alias_name)
121 {
122         int i;
123         for (i = 0; i < VMBUS_ALIAS_LEN; i += 2)
124                 sprintf(&alias_name[i], "%02x", hv_dev->dev_type.b[i/2]);
125 }
126
127 static u8 channel_monitor_group(struct vmbus_channel *channel)
128 {
129         return (u8)channel->offermsg.monitorid / 32;
130 }
131
132 static u8 channel_monitor_offset(struct vmbus_channel *channel)
133 {
134         return (u8)channel->offermsg.monitorid % 32;
135 }
136
137 static u32 channel_pending(struct vmbus_channel *channel,
138                            struct hv_monitor_page *monitor_page)
139 {
140         u8 monitor_group = channel_monitor_group(channel);
141         return monitor_page->trigger_group[monitor_group].pending;
142 }
143
144 static u32 channel_latency(struct vmbus_channel *channel,
145                            struct hv_monitor_page *monitor_page)
146 {
147         u8 monitor_group = channel_monitor_group(channel);
148         u8 monitor_offset = channel_monitor_offset(channel);
149         return monitor_page->latency[monitor_group][monitor_offset];
150 }
151
152 static u32 channel_conn_id(struct vmbus_channel *channel,
153                            struct hv_monitor_page *monitor_page)
154 {
155         u8 monitor_group = channel_monitor_group(channel);
156         u8 monitor_offset = channel_monitor_offset(channel);
157         return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
158 }
159
160 static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
161                        char *buf)
162 {
163         struct hv_device *hv_dev = device_to_hv_device(dev);
164
165         if (!hv_dev->channel)
166                 return -ENODEV;
167         return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
168 }
169 static DEVICE_ATTR_RO(id);
170
171 static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
172                           char *buf)
173 {
174         struct hv_device *hv_dev = device_to_hv_device(dev);
175
176         if (!hv_dev->channel)
177                 return -ENODEV;
178         return sprintf(buf, "%d\n", hv_dev->channel->state);
179 }
180 static DEVICE_ATTR_RO(state);
181
182 static ssize_t monitor_id_show(struct device *dev,
183                                struct device_attribute *dev_attr, char *buf)
184 {
185         struct hv_device *hv_dev = device_to_hv_device(dev);
186
187         if (!hv_dev->channel)
188                 return -ENODEV;
189         return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
190 }
191 static DEVICE_ATTR_RO(monitor_id);
192
193 static ssize_t class_id_show(struct device *dev,
194                                struct device_attribute *dev_attr, char *buf)
195 {
196         struct hv_device *hv_dev = device_to_hv_device(dev);
197
198         if (!hv_dev->channel)
199                 return -ENODEV;
200         return sprintf(buf, "{%pUl}\n",
201                        hv_dev->channel->offermsg.offer.if_type.b);
202 }
203 static DEVICE_ATTR_RO(class_id);
204
205 static ssize_t device_id_show(struct device *dev,
206                               struct device_attribute *dev_attr, char *buf)
207 {
208         struct hv_device *hv_dev = device_to_hv_device(dev);
209
210         if (!hv_dev->channel)
211                 return -ENODEV;
212         return sprintf(buf, "{%pUl}\n",
213                        hv_dev->channel->offermsg.offer.if_instance.b);
214 }
215 static DEVICE_ATTR_RO(device_id);
216
217 static ssize_t modalias_show(struct device *dev,
218                              struct device_attribute *dev_attr, char *buf)
219 {
220         struct hv_device *hv_dev = device_to_hv_device(dev);
221         char alias_name[VMBUS_ALIAS_LEN + 1];
222
223         print_alias_name(hv_dev, alias_name);
224         return sprintf(buf, "vmbus:%s\n", alias_name);
225 }
226 static DEVICE_ATTR_RO(modalias);
227
228 static ssize_t server_monitor_pending_show(struct device *dev,
229                                            struct device_attribute *dev_attr,
230                                            char *buf)
231 {
232         struct hv_device *hv_dev = device_to_hv_device(dev);
233
234         if (!hv_dev->channel)
235                 return -ENODEV;
236         return sprintf(buf, "%d\n",
237                        channel_pending(hv_dev->channel,
238                                        vmbus_connection.monitor_pages[1]));
239 }
240 static DEVICE_ATTR_RO(server_monitor_pending);
241
242 static ssize_t client_monitor_pending_show(struct device *dev,
243                                            struct device_attribute *dev_attr,
244                                            char *buf)
245 {
246         struct hv_device *hv_dev = device_to_hv_device(dev);
247
248         if (!hv_dev->channel)
249                 return -ENODEV;
250         return sprintf(buf, "%d\n",
251                        channel_pending(hv_dev->channel,
252                                        vmbus_connection.monitor_pages[1]));
253 }
254 static DEVICE_ATTR_RO(client_monitor_pending);
255
256 static ssize_t server_monitor_latency_show(struct device *dev,
257                                            struct device_attribute *dev_attr,
258                                            char *buf)
259 {
260         struct hv_device *hv_dev = device_to_hv_device(dev);
261
262         if (!hv_dev->channel)
263                 return -ENODEV;
264         return sprintf(buf, "%d\n",
265                        channel_latency(hv_dev->channel,
266                                        vmbus_connection.monitor_pages[0]));
267 }
268 static DEVICE_ATTR_RO(server_monitor_latency);
269
270 static ssize_t client_monitor_latency_show(struct device *dev,
271                                            struct device_attribute *dev_attr,
272                                            char *buf)
273 {
274         struct hv_device *hv_dev = device_to_hv_device(dev);
275
276         if (!hv_dev->channel)
277                 return -ENODEV;
278         return sprintf(buf, "%d\n",
279                        channel_latency(hv_dev->channel,
280                                        vmbus_connection.monitor_pages[1]));
281 }
282 static DEVICE_ATTR_RO(client_monitor_latency);
283
284 static ssize_t server_monitor_conn_id_show(struct device *dev,
285                                            struct device_attribute *dev_attr,
286                                            char *buf)
287 {
288         struct hv_device *hv_dev = device_to_hv_device(dev);
289
290         if (!hv_dev->channel)
291                 return -ENODEV;
292         return sprintf(buf, "%d\n",
293                        channel_conn_id(hv_dev->channel,
294                                        vmbus_connection.monitor_pages[0]));
295 }
296 static DEVICE_ATTR_RO(server_monitor_conn_id);
297
298 static ssize_t client_monitor_conn_id_show(struct device *dev,
299                                            struct device_attribute *dev_attr,
300                                            char *buf)
301 {
302         struct hv_device *hv_dev = device_to_hv_device(dev);
303
304         if (!hv_dev->channel)
305                 return -ENODEV;
306         return sprintf(buf, "%d\n",
307                        channel_conn_id(hv_dev->channel,
308                                        vmbus_connection.monitor_pages[1]));
309 }
310 static DEVICE_ATTR_RO(client_monitor_conn_id);
311
312 static ssize_t out_intr_mask_show(struct device *dev,
313                                   struct device_attribute *dev_attr, char *buf)
314 {
315         struct hv_device *hv_dev = device_to_hv_device(dev);
316         struct hv_ring_buffer_debug_info outbound;
317
318         if (!hv_dev->channel)
319                 return -ENODEV;
320         hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
321         return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
322 }
323 static DEVICE_ATTR_RO(out_intr_mask);
324
325 static ssize_t out_read_index_show(struct device *dev,
326                                    struct device_attribute *dev_attr, char *buf)
327 {
328         struct hv_device *hv_dev = device_to_hv_device(dev);
329         struct hv_ring_buffer_debug_info outbound;
330
331         if (!hv_dev->channel)
332                 return -ENODEV;
333         hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
334         return sprintf(buf, "%d\n", outbound.current_read_index);
335 }
336 static DEVICE_ATTR_RO(out_read_index);
337
338 static ssize_t out_write_index_show(struct device *dev,
339                                     struct device_attribute *dev_attr,
340                                     char *buf)
341 {
342         struct hv_device *hv_dev = device_to_hv_device(dev);
343         struct hv_ring_buffer_debug_info outbound;
344
345         if (!hv_dev->channel)
346                 return -ENODEV;
347         hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
348         return sprintf(buf, "%d\n", outbound.current_write_index);
349 }
350 static DEVICE_ATTR_RO(out_write_index);
351
352 static ssize_t out_read_bytes_avail_show(struct device *dev,
353                                          struct device_attribute *dev_attr,
354                                          char *buf)
355 {
356         struct hv_device *hv_dev = device_to_hv_device(dev);
357         struct hv_ring_buffer_debug_info outbound;
358
359         if (!hv_dev->channel)
360                 return -ENODEV;
361         hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
362         return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
363 }
364 static DEVICE_ATTR_RO(out_read_bytes_avail);
365
366 static ssize_t out_write_bytes_avail_show(struct device *dev,
367                                           struct device_attribute *dev_attr,
368                                           char *buf)
369 {
370         struct hv_device *hv_dev = device_to_hv_device(dev);
371         struct hv_ring_buffer_debug_info outbound;
372
373         if (!hv_dev->channel)
374                 return -ENODEV;
375         hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
376         return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
377 }
378 static DEVICE_ATTR_RO(out_write_bytes_avail);
379
380 static ssize_t in_intr_mask_show(struct device *dev,
381                                  struct device_attribute *dev_attr, char *buf)
382 {
383         struct hv_device *hv_dev = device_to_hv_device(dev);
384         struct hv_ring_buffer_debug_info inbound;
385
386         if (!hv_dev->channel)
387                 return -ENODEV;
388         hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
389         return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
390 }
391 static DEVICE_ATTR_RO(in_intr_mask);
392
393 static ssize_t in_read_index_show(struct device *dev,
394                                   struct device_attribute *dev_attr, char *buf)
395 {
396         struct hv_device *hv_dev = device_to_hv_device(dev);
397         struct hv_ring_buffer_debug_info inbound;
398
399         if (!hv_dev->channel)
400                 return -ENODEV;
401         hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
402         return sprintf(buf, "%d\n", inbound.current_read_index);
403 }
404 static DEVICE_ATTR_RO(in_read_index);
405
406 static ssize_t in_write_index_show(struct device *dev,
407                                    struct device_attribute *dev_attr, char *buf)
408 {
409         struct hv_device *hv_dev = device_to_hv_device(dev);
410         struct hv_ring_buffer_debug_info inbound;
411
412         if (!hv_dev->channel)
413                 return -ENODEV;
414         hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
415         return sprintf(buf, "%d\n", inbound.current_write_index);
416 }
417 static DEVICE_ATTR_RO(in_write_index);
418
419 static ssize_t in_read_bytes_avail_show(struct device *dev,
420                                         struct device_attribute *dev_attr,
421                                         char *buf)
422 {
423         struct hv_device *hv_dev = device_to_hv_device(dev);
424         struct hv_ring_buffer_debug_info inbound;
425
426         if (!hv_dev->channel)
427                 return -ENODEV;
428         hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
429         return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
430 }
431 static DEVICE_ATTR_RO(in_read_bytes_avail);
432
433 static ssize_t in_write_bytes_avail_show(struct device *dev,
434                                          struct device_attribute *dev_attr,
435                                          char *buf)
436 {
437         struct hv_device *hv_dev = device_to_hv_device(dev);
438         struct hv_ring_buffer_debug_info inbound;
439
440         if (!hv_dev->channel)
441                 return -ENODEV;
442         hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
443         return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
444 }
445 static DEVICE_ATTR_RO(in_write_bytes_avail);
446
447 /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
448 static struct attribute *vmbus_attrs[] = {
449         &dev_attr_id.attr,
450         &dev_attr_state.attr,
451         &dev_attr_monitor_id.attr,
452         &dev_attr_class_id.attr,
453         &dev_attr_device_id.attr,
454         &dev_attr_modalias.attr,
455         &dev_attr_server_monitor_pending.attr,
456         &dev_attr_client_monitor_pending.attr,
457         &dev_attr_server_monitor_latency.attr,
458         &dev_attr_client_monitor_latency.attr,
459         &dev_attr_server_monitor_conn_id.attr,
460         &dev_attr_client_monitor_conn_id.attr,
461         &dev_attr_out_intr_mask.attr,
462         &dev_attr_out_read_index.attr,
463         &dev_attr_out_write_index.attr,
464         &dev_attr_out_read_bytes_avail.attr,
465         &dev_attr_out_write_bytes_avail.attr,
466         &dev_attr_in_intr_mask.attr,
467         &dev_attr_in_read_index.attr,
468         &dev_attr_in_write_index.attr,
469         &dev_attr_in_read_bytes_avail.attr,
470         &dev_attr_in_write_bytes_avail.attr,
471         NULL,
472 };
473 ATTRIBUTE_GROUPS(vmbus);
474
475 /*
476  * vmbus_uevent - add uevent for our device
477  *
478  * This routine is invoked when a device is added or removed on the vmbus to
479  * generate a uevent to udev in the userspace. The udev will then look at its
480  * rule and the uevent generated here to load the appropriate driver
481  *
482  * The alias string will be of the form vmbus:guid where guid is the string
483  * representation of the device guid (each byte of the guid will be
484  * represented with two hex characters.
485  */
486 static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
487 {
488         struct hv_device *dev = device_to_hv_device(device);
489         int ret;
490         char alias_name[VMBUS_ALIAS_LEN + 1];
491
492         print_alias_name(dev, alias_name);
493         ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name);
494         return ret;
495 }
496
497 static const uuid_le null_guid;
498
499 static inline bool is_null_guid(const __u8 *guid)
500 {
501         if (memcmp(guid, &null_guid, sizeof(uuid_le)))
502                 return false;
503         return true;
504 }
505
506 /*
507  * Return a matching hv_vmbus_device_id pointer.
508  * If there is no match, return NULL.
509  */
510 static const struct hv_vmbus_device_id *hv_vmbus_get_id(
511                                         const struct hv_vmbus_device_id *id,
512                                         const __u8 *guid)
513 {
514         for (; !is_null_guid(id->guid); id++)
515                 if (!memcmp(&id->guid, guid, sizeof(uuid_le)))
516                         return id;
517
518         return NULL;
519 }
520
521
522
523 /*
524  * vmbus_match - Attempt to match the specified device to the specified driver
525  */
526 static int vmbus_match(struct device *device, struct device_driver *driver)
527 {
528         struct hv_driver *drv = drv_to_hv_drv(driver);
529         struct hv_device *hv_dev = device_to_hv_device(device);
530
531         if (hv_vmbus_get_id(drv->id_table, hv_dev->dev_type.b))
532                 return 1;
533
534         return 0;
535 }
536
537 /*
538  * vmbus_probe - Add the new vmbus's child device
539  */
540 static int vmbus_probe(struct device *child_device)
541 {
542         int ret = 0;
543         struct hv_driver *drv =
544                         drv_to_hv_drv(child_device->driver);
545         struct hv_device *dev = device_to_hv_device(child_device);
546         const struct hv_vmbus_device_id *dev_id;
547
548         dev_id = hv_vmbus_get_id(drv->id_table, dev->dev_type.b);
549         if (drv->probe) {
550                 ret = drv->probe(dev, dev_id);
551                 if (ret != 0)
552                         pr_err("probe failed for device %s (%d)\n",
553                                dev_name(child_device), ret);
554
555         } else {
556                 pr_err("probe not set for driver %s\n",
557                        dev_name(child_device));
558                 ret = -ENODEV;
559         }
560         return ret;
561 }
562
563 /*
564  * vmbus_remove - Remove a vmbus device
565  */
566 static int vmbus_remove(struct device *child_device)
567 {
568         struct hv_driver *drv;
569         struct hv_device *dev = device_to_hv_device(child_device);
570         u32 relid = dev->channel->offermsg.child_relid;
571
572         if (child_device->driver) {
573                 drv = drv_to_hv_drv(child_device->driver);
574                 if (drv->remove)
575                         drv->remove(dev);
576                 else {
577                         hv_process_channel_removal(dev->channel, relid);
578                         pr_err("remove not set for driver %s\n",
579                                 dev_name(child_device));
580                 }
581         } else {
582                 /*
583                  * We don't have a driver for this device; deal with the
584                  * rescind message by removing the channel.
585                  */
586                 hv_process_channel_removal(dev->channel, relid);
587         }
588
589         return 0;
590 }
591
592
593 /*
594  * vmbus_shutdown - Shutdown a vmbus device
595  */
596 static void vmbus_shutdown(struct device *child_device)
597 {
598         struct hv_driver *drv;
599         struct hv_device *dev = device_to_hv_device(child_device);
600
601
602         /* The device may not be attached yet */
603         if (!child_device->driver)
604                 return;
605
606         drv = drv_to_hv_drv(child_device->driver);
607
608         if (drv->shutdown)
609                 drv->shutdown(dev);
610
611         return;
612 }
613
614
615 /*
616  * vmbus_device_release - Final callback release of the vmbus child device
617  */
618 static void vmbus_device_release(struct device *device)
619 {
620         struct hv_device *hv_dev = device_to_hv_device(device);
621
622         kfree(hv_dev);
623
624 }
625
626 /* The one and only one */
627 static struct bus_type  hv_bus = {
628         .name =         "vmbus",
629         .match =                vmbus_match,
630         .shutdown =             vmbus_shutdown,
631         .remove =               vmbus_remove,
632         .probe =                vmbus_probe,
633         .uevent =               vmbus_uevent,
634         .dev_groups =           vmbus_groups,
635 };
636
637 struct onmessage_work_context {
638         struct work_struct work;
639         struct hv_message msg;
640 };
641
642 static void vmbus_onmessage_work(struct work_struct *work)
643 {
644         struct onmessage_work_context *ctx;
645
646         /* Do not process messages if we're in DISCONNECTED state */
647         if (vmbus_connection.conn_state == DISCONNECTED)
648                 return;
649
650         ctx = container_of(work, struct onmessage_work_context,
651                            work);
652         vmbus_onmessage(&ctx->msg);
653         kfree(ctx);
654 }
655
656 static void hv_process_timer_expiration(struct hv_message *msg, int cpu)
657 {
658         struct clock_event_device *dev = hv_context.clk_evt[cpu];
659
660         if (dev->event_handler)
661                 dev->event_handler(dev);
662
663         msg->header.message_type = HVMSG_NONE;
664
665         /*
666          * Make sure the write to MessageType (ie set to
667          * HVMSG_NONE) happens before we read the
668          * MessagePending and EOMing. Otherwise, the EOMing
669          * will not deliver any more messages since there is
670          * no empty slot
671          */
672         mb();
673
674         if (msg->header.message_flags.msg_pending) {
675                 /*
676                  * This will cause message queue rescan to
677                  * possibly deliver another msg from the
678                  * hypervisor
679                  */
680                 wrmsrl(HV_X64_MSR_EOM, 0);
681         }
682 }
683
684 static void vmbus_on_msg_dpc(unsigned long data)
685 {
686         int cpu = smp_processor_id();
687         void *page_addr = hv_context.synic_message_page[cpu];
688         struct hv_message *msg = (struct hv_message *)page_addr +
689                                   VMBUS_MESSAGE_SINT;
690         struct vmbus_channel_message_header *hdr;
691         struct vmbus_channel_message_table_entry *entry;
692         struct onmessage_work_context *ctx;
693
694         while (1) {
695                 if (msg->header.message_type == HVMSG_NONE)
696                         /* no msg */
697                         break;
698
699                 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
700
701                 if (hdr->msgtype >= CHANNELMSG_COUNT) {
702                         WARN_ONCE(1, "unknown msgtype=%d\n", hdr->msgtype);
703                         goto msg_handled;
704                 }
705
706                 entry = &channel_message_table[hdr->msgtype];
707                 if (entry->handler_type == VMHT_BLOCKING) {
708                         ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
709                         if (ctx == NULL)
710                                 continue;
711
712                         INIT_WORK(&ctx->work, vmbus_onmessage_work);
713                         memcpy(&ctx->msg, msg, sizeof(*msg));
714
715                         queue_work(vmbus_connection.work_queue, &ctx->work);
716                 } else
717                         entry->message_handler(hdr);
718
719 msg_handled:
720                 msg->header.message_type = HVMSG_NONE;
721
722                 /*
723                  * Make sure the write to MessageType (ie set to
724                  * HVMSG_NONE) happens before we read the
725                  * MessagePending and EOMing. Otherwise, the EOMing
726                  * will not deliver any more messages since there is
727                  * no empty slot
728                  */
729                 mb();
730
731                 if (msg->header.message_flags.msg_pending) {
732                         /*
733                          * This will cause message queue rescan to
734                          * possibly deliver another msg from the
735                          * hypervisor
736                          */
737                         wrmsrl(HV_X64_MSR_EOM, 0);
738                 }
739         }
740 }
741
742 static void vmbus_isr(void)
743 {
744         int cpu = smp_processor_id();
745         void *page_addr;
746         struct hv_message *msg;
747         union hv_synic_event_flags *event;
748         bool handled = false;
749
750         page_addr = hv_context.synic_event_page[cpu];
751         if (page_addr == NULL)
752                 return;
753
754         event = (union hv_synic_event_flags *)page_addr +
755                                          VMBUS_MESSAGE_SINT;
756         /*
757          * Check for events before checking for messages. This is the order
758          * in which events and messages are checked in Windows guests on
759          * Hyper-V, and the Windows team suggested we do the same.
760          */
761
762         if ((vmbus_proto_version == VERSION_WS2008) ||
763                 (vmbus_proto_version == VERSION_WIN7)) {
764
765                 /* Since we are a child, we only need to check bit 0 */
766                 if (sync_test_and_clear_bit(0,
767                         (unsigned long *) &event->flags32[0])) {
768                         handled = true;
769                 }
770         } else {
771                 /*
772                  * Our host is win8 or above. The signaling mechanism
773                  * has changed and we can directly look at the event page.
774                  * If bit n is set then we have an interrup on the channel
775                  * whose id is n.
776                  */
777                 handled = true;
778         }
779
780         if (handled)
781                 tasklet_schedule(hv_context.event_dpc[cpu]);
782
783
784         page_addr = hv_context.synic_message_page[cpu];
785         msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
786
787         /* Check if there are actual msgs to be processed */
788         if (msg->header.message_type != HVMSG_NONE) {
789                 if (msg->header.message_type == HVMSG_TIMER_EXPIRED)
790                         hv_process_timer_expiration(msg, cpu);
791                 else
792                         tasklet_schedule(&msg_dpc);
793         }
794 }
795
796 #ifdef CONFIG_HOTPLUG_CPU
797 static int hyperv_cpu_disable(void)
798 {
799         return -ENOSYS;
800 }
801
802 static void hv_cpu_hotplug_quirk(bool vmbus_loaded)
803 {
804         static void *previous_cpu_disable;
805
806         /*
807          * Offlining a CPU when running on newer hypervisors (WS2012R2, Win8,
808          * ...) is not supported at this moment as channel interrupts are
809          * distributed across all of them.
810          */
811
812         if ((vmbus_proto_version == VERSION_WS2008) ||
813             (vmbus_proto_version == VERSION_WIN7))
814                 return;
815
816         if (vmbus_loaded) {
817                 previous_cpu_disable = smp_ops.cpu_disable;
818                 smp_ops.cpu_disable = hyperv_cpu_disable;
819                 pr_notice("CPU offlining is not supported by hypervisor\n");
820         } else if (previous_cpu_disable)
821                 smp_ops.cpu_disable = previous_cpu_disable;
822 }
823 #else
824 static void hv_cpu_hotplug_quirk(bool vmbus_loaded)
825 {
826 }
827 #endif
828
829 /*
830  * vmbus_bus_init -Main vmbus driver initialization routine.
831  *
832  * Here, we
833  *      - initialize the vmbus driver context
834  *      - invoke the vmbus hv main init routine
835  *      - get the irq resource
836  *      - retrieve the channel offers
837  */
838 static int vmbus_bus_init(int irq)
839 {
840         int ret;
841
842         /* Hypervisor initialization...setup hypercall page..etc */
843         ret = hv_init();
844         if (ret != 0) {
845                 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
846                 return ret;
847         }
848
849         tasklet_init(&msg_dpc, vmbus_on_msg_dpc, 0);
850
851         ret = bus_register(&hv_bus);
852         if (ret)
853                 goto err_cleanup;
854
855         hv_setup_vmbus_irq(vmbus_isr);
856
857         ret = hv_synic_alloc();
858         if (ret)
859                 goto err_alloc;
860         /*
861          * Initialize the per-cpu interrupt state and
862          * connect to the host.
863          */
864         on_each_cpu(hv_synic_init, NULL, 1);
865         ret = vmbus_connect();
866         if (ret)
867                 goto err_alloc;
868
869         hv_cpu_hotplug_quirk(true);
870
871         /*
872          * Only register if the crash MSRs are available
873          */
874         if (ms_hyperv.features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
875                 register_die_notifier(&hyperv_die_block);
876                 atomic_notifier_chain_register(&panic_notifier_list,
877                                                &hyperv_panic_block);
878         }
879
880         vmbus_request_offers();
881
882         return 0;
883
884 err_alloc:
885         hv_synic_free();
886         hv_remove_vmbus_irq();
887
888         bus_unregister(&hv_bus);
889
890 err_cleanup:
891         hv_cleanup();
892
893         return ret;
894 }
895
896 /**
897  * __vmbus_child_driver_register - Register a vmbus's driver
898  * @drv: Pointer to driver structure you want to register
899  * @owner: owner module of the drv
900  * @mod_name: module name string
901  *
902  * Registers the given driver with Linux through the 'driver_register()' call
903  * and sets up the hyper-v vmbus handling for this driver.
904  * It will return the state of the 'driver_register()' call.
905  *
906  */
907 int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
908 {
909         int ret;
910
911         pr_info("registering driver %s\n", hv_driver->name);
912
913         ret = vmbus_exists();
914         if (ret < 0)
915                 return ret;
916
917         hv_driver->driver.name = hv_driver->name;
918         hv_driver->driver.owner = owner;
919         hv_driver->driver.mod_name = mod_name;
920         hv_driver->driver.bus = &hv_bus;
921
922         ret = driver_register(&hv_driver->driver);
923
924         return ret;
925 }
926 EXPORT_SYMBOL_GPL(__vmbus_driver_register);
927
928 /**
929  * vmbus_driver_unregister() - Unregister a vmbus's driver
930  * @drv: Pointer to driver structure you want to un-register
931  *
932  * Un-register the given driver that was previous registered with a call to
933  * vmbus_driver_register()
934  */
935 void vmbus_driver_unregister(struct hv_driver *hv_driver)
936 {
937         pr_info("unregistering driver %s\n", hv_driver->name);
938
939         if (!vmbus_exists())
940                 driver_unregister(&hv_driver->driver);
941 }
942 EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
943
944 /*
945  * vmbus_device_create - Creates and registers a new child device
946  * on the vmbus.
947  */
948 struct hv_device *vmbus_device_create(const uuid_le *type,
949                                       const uuid_le *instance,
950                                       struct vmbus_channel *channel)
951 {
952         struct hv_device *child_device_obj;
953
954         child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
955         if (!child_device_obj) {
956                 pr_err("Unable to allocate device object for child device\n");
957                 return NULL;
958         }
959
960         child_device_obj->channel = channel;
961         memcpy(&child_device_obj->dev_type, type, sizeof(uuid_le));
962         memcpy(&child_device_obj->dev_instance, instance,
963                sizeof(uuid_le));
964
965
966         return child_device_obj;
967 }
968
969 /*
970  * vmbus_device_register - Register the child device
971  */
972 int vmbus_device_register(struct hv_device *child_device_obj)
973 {
974         int ret = 0;
975
976         dev_set_name(&child_device_obj->device, "vmbus_%d",
977                      child_device_obj->channel->id);
978
979         child_device_obj->device.bus = &hv_bus;
980         child_device_obj->device.parent = &hv_acpi_dev->dev;
981         child_device_obj->device.release = vmbus_device_release;
982
983         /*
984          * Register with the LDM. This will kick off the driver/device
985          * binding...which will eventually call vmbus_match() and vmbus_probe()
986          */
987         ret = device_register(&child_device_obj->device);
988
989         if (ret)
990                 pr_err("Unable to register child device\n");
991         else
992                 pr_debug("child device %s registered\n",
993                         dev_name(&child_device_obj->device));
994
995         return ret;
996 }
997
998 /*
999  * vmbus_device_unregister - Remove the specified child device
1000  * from the vmbus.
1001  */
1002 void vmbus_device_unregister(struct hv_device *device_obj)
1003 {
1004         pr_debug("child device %s unregistered\n",
1005                 dev_name(&device_obj->device));
1006
1007         /*
1008          * Kick off the process of unregistering the device.
1009          * This will call vmbus_remove() and eventually vmbus_device_release()
1010          */
1011         device_unregister(&device_obj->device);
1012 }
1013
1014
1015 /*
1016  * VMBUS is an acpi enumerated device. Get the the information we
1017  * need from DSDT.
1018  */
1019
1020 static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
1021 {
1022         switch (res->type) {
1023         case ACPI_RESOURCE_TYPE_IRQ:
1024                 irq = res->data.irq.interrupts[0];
1025                 break;
1026
1027         case ACPI_RESOURCE_TYPE_ADDRESS64:
1028                 hyperv_mmio.start = res->data.address64.address.minimum;
1029                 hyperv_mmio.end = res->data.address64.address.maximum;
1030                 break;
1031         }
1032
1033         return AE_OK;
1034 }
1035
1036 static int vmbus_acpi_add(struct acpi_device *device)
1037 {
1038         acpi_status result;
1039         int ret_val = -ENODEV;
1040
1041         hv_acpi_dev = device;
1042
1043         result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
1044                                         vmbus_walk_resources, NULL);
1045
1046         if (ACPI_FAILURE(result))
1047                 goto acpi_walk_err;
1048         /*
1049          * The parent of the vmbus acpi device (Gen2 firmware) is the VMOD that
1050          * has the mmio ranges. Get that.
1051          */
1052         if (device->parent) {
1053                 result = acpi_walk_resources(device->parent->handle,
1054                                         METHOD_NAME__CRS,
1055                                         vmbus_walk_resources, NULL);
1056
1057                 if (ACPI_FAILURE(result))
1058                         goto acpi_walk_err;
1059                 if (hyperv_mmio.start && hyperv_mmio.end)
1060                         request_resource(&iomem_resource, &hyperv_mmio);
1061         }
1062         ret_val = 0;
1063
1064 acpi_walk_err:
1065         complete(&probe_event);
1066         return ret_val;
1067 }
1068
1069 static int vmbus_acpi_remove(struct acpi_device *device)
1070 {
1071         int ret = 0;
1072
1073         if (hyperv_mmio.start && hyperv_mmio.end)
1074                 ret = release_resource(&hyperv_mmio);
1075         return ret;
1076 }
1077
1078 static const struct acpi_device_id vmbus_acpi_device_ids[] = {
1079         {"VMBUS", 0},
1080         {"VMBus", 0},
1081         {"", 0},
1082 };
1083 MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
1084
1085 static struct acpi_driver vmbus_acpi_driver = {
1086         .name = "vmbus",
1087         .ids = vmbus_acpi_device_ids,
1088         .ops = {
1089                 .add = vmbus_acpi_add,
1090                 .remove = vmbus_acpi_remove,
1091         },
1092 };
1093
1094 static void hv_kexec_handler(void)
1095 {
1096         int cpu;
1097
1098         hv_synic_clockevents_cleanup();
1099         vmbus_initiate_unload();
1100         for_each_online_cpu(cpu)
1101                 smp_call_function_single(cpu, hv_synic_cleanup, NULL, 1);
1102         hv_cleanup();
1103 };
1104
1105 static void hv_crash_handler(struct pt_regs *regs)
1106 {
1107         vmbus_initiate_unload();
1108         /*
1109          * In crash handler we can't schedule synic cleanup for all CPUs,
1110          * doing the cleanup for current CPU only. This should be sufficient
1111          * for kdump.
1112          */
1113         hv_synic_cleanup(NULL);
1114         hv_cleanup();
1115 };
1116
1117 static int __init hv_acpi_init(void)
1118 {
1119         int ret, t;
1120
1121         if (x86_hyper != &x86_hyper_ms_hyperv)
1122                 return -ENODEV;
1123
1124         init_completion(&probe_event);
1125
1126         /*
1127          * Get irq resources first.
1128          */
1129         ret = acpi_bus_register_driver(&vmbus_acpi_driver);
1130
1131         if (ret)
1132                 return ret;
1133
1134         t = wait_for_completion_timeout(&probe_event, 5*HZ);
1135         if (t == 0) {
1136                 ret = -ETIMEDOUT;
1137                 goto cleanup;
1138         }
1139
1140         if (irq <= 0) {
1141                 ret = -ENODEV;
1142                 goto cleanup;
1143         }
1144
1145         ret = vmbus_bus_init(irq);
1146         if (ret)
1147                 goto cleanup;
1148
1149         hv_setup_kexec_handler(hv_kexec_handler);
1150         hv_setup_crash_handler(hv_crash_handler);
1151
1152         return 0;
1153
1154 cleanup:
1155         acpi_bus_unregister_driver(&vmbus_acpi_driver);
1156         hv_acpi_dev = NULL;
1157         return ret;
1158 }
1159
1160 static void __exit vmbus_exit(void)
1161 {
1162         int cpu;
1163
1164         hv_remove_kexec_handler();
1165         hv_remove_crash_handler();
1166         vmbus_connection.conn_state = DISCONNECTED;
1167         hv_synic_clockevents_cleanup();
1168         vmbus_disconnect();
1169         hv_remove_vmbus_irq();
1170         tasklet_kill(&msg_dpc);
1171         vmbus_free_channels();
1172         if (ms_hyperv.features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
1173                 unregister_die_notifier(&hyperv_die_block);
1174                 atomic_notifier_chain_unregister(&panic_notifier_list,
1175                                                  &hyperv_panic_block);
1176         }
1177         bus_unregister(&hv_bus);
1178         hv_cleanup();
1179         for_each_online_cpu(cpu) {
1180                 tasklet_kill(hv_context.event_dpc[cpu]);
1181                 smp_call_function_single(cpu, hv_synic_cleanup, NULL, 1);
1182         }
1183         hv_synic_free();
1184         acpi_bus_unregister_driver(&vmbus_acpi_driver);
1185         hv_cpu_hotplug_quirk(false);
1186 }
1187
1188
1189 MODULE_LICENSE("GPL");
1190
1191 subsys_initcall(hv_acpi_init);
1192 module_exit(vmbus_exit);