Merge branch 'for-4.16' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/percpu
[sfrench/cifs-2.6.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 <linux/sched/task_stack.h>
38
39 #include <asm/hyperv.h>
40 #include <asm/hypervisor.h>
41 #include <asm/mshyperv.h>
42 #include <linux/notifier.h>
43 #include <linux/ptrace.h>
44 #include <linux/screen_info.h>
45 #include <linux/kdebug.h>
46 #include <linux/efi.h>
47 #include <linux/random.h>
48 #include "hyperv_vmbus.h"
49
50 struct vmbus_dynid {
51         struct list_head node;
52         struct hv_vmbus_device_id id;
53 };
54
55 static struct acpi_device  *hv_acpi_dev;
56
57 static struct completion probe_event;
58
59 static int hyperv_cpuhp_online;
60
61 static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
62                               void *args)
63 {
64         struct pt_regs *regs;
65
66         regs = current_pt_regs();
67
68         hyperv_report_panic(regs, val);
69         return NOTIFY_DONE;
70 }
71
72 static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
73                             void *args)
74 {
75         struct die_args *die = (struct die_args *)args;
76         struct pt_regs *regs = die->regs;
77
78         hyperv_report_panic(regs, val);
79         return NOTIFY_DONE;
80 }
81
82 static struct notifier_block hyperv_die_block = {
83         .notifier_call = hyperv_die_event,
84 };
85 static struct notifier_block hyperv_panic_block = {
86         .notifier_call = hyperv_panic_event,
87 };
88
89 static const char *fb_mmio_name = "fb_range";
90 static struct resource *fb_mmio;
91 static struct resource *hyperv_mmio;
92 static DEFINE_SEMAPHORE(hyperv_mmio_lock);
93
94 static int vmbus_exists(void)
95 {
96         if (hv_acpi_dev == NULL)
97                 return -ENODEV;
98
99         return 0;
100 }
101
102 #define VMBUS_ALIAS_LEN ((sizeof((struct hv_vmbus_device_id *)0)->guid) * 2)
103 static void print_alias_name(struct hv_device *hv_dev, char *alias_name)
104 {
105         int i;
106         for (i = 0; i < VMBUS_ALIAS_LEN; i += 2)
107                 sprintf(&alias_name[i], "%02x", hv_dev->dev_type.b[i/2]);
108 }
109
110 static u8 channel_monitor_group(const struct vmbus_channel *channel)
111 {
112         return (u8)channel->offermsg.monitorid / 32;
113 }
114
115 static u8 channel_monitor_offset(const struct vmbus_channel *channel)
116 {
117         return (u8)channel->offermsg.monitorid % 32;
118 }
119
120 static u32 channel_pending(const struct vmbus_channel *channel,
121                            const struct hv_monitor_page *monitor_page)
122 {
123         u8 monitor_group = channel_monitor_group(channel);
124
125         return monitor_page->trigger_group[monitor_group].pending;
126 }
127
128 static u32 channel_latency(const struct vmbus_channel *channel,
129                            const struct hv_monitor_page *monitor_page)
130 {
131         u8 monitor_group = channel_monitor_group(channel);
132         u8 monitor_offset = channel_monitor_offset(channel);
133
134         return monitor_page->latency[monitor_group][monitor_offset];
135 }
136
137 static u32 channel_conn_id(struct vmbus_channel *channel,
138                            struct hv_monitor_page *monitor_page)
139 {
140         u8 monitor_group = channel_monitor_group(channel);
141         u8 monitor_offset = channel_monitor_offset(channel);
142         return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
143 }
144
145 static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
146                        char *buf)
147 {
148         struct hv_device *hv_dev = device_to_hv_device(dev);
149
150         if (!hv_dev->channel)
151                 return -ENODEV;
152         return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
153 }
154 static DEVICE_ATTR_RO(id);
155
156 static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
157                           char *buf)
158 {
159         struct hv_device *hv_dev = device_to_hv_device(dev);
160
161         if (!hv_dev->channel)
162                 return -ENODEV;
163         return sprintf(buf, "%d\n", hv_dev->channel->state);
164 }
165 static DEVICE_ATTR_RO(state);
166
167 static ssize_t monitor_id_show(struct device *dev,
168                                struct device_attribute *dev_attr, char *buf)
169 {
170         struct hv_device *hv_dev = device_to_hv_device(dev);
171
172         if (!hv_dev->channel)
173                 return -ENODEV;
174         return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
175 }
176 static DEVICE_ATTR_RO(monitor_id);
177
178 static ssize_t class_id_show(struct device *dev,
179                                struct device_attribute *dev_attr, char *buf)
180 {
181         struct hv_device *hv_dev = device_to_hv_device(dev);
182
183         if (!hv_dev->channel)
184                 return -ENODEV;
185         return sprintf(buf, "{%pUl}\n",
186                        hv_dev->channel->offermsg.offer.if_type.b);
187 }
188 static DEVICE_ATTR_RO(class_id);
189
190 static ssize_t device_id_show(struct device *dev,
191                               struct device_attribute *dev_attr, char *buf)
192 {
193         struct hv_device *hv_dev = device_to_hv_device(dev);
194
195         if (!hv_dev->channel)
196                 return -ENODEV;
197         return sprintf(buf, "{%pUl}\n",
198                        hv_dev->channel->offermsg.offer.if_instance.b);
199 }
200 static DEVICE_ATTR_RO(device_id);
201
202 static ssize_t modalias_show(struct device *dev,
203                              struct device_attribute *dev_attr, char *buf)
204 {
205         struct hv_device *hv_dev = device_to_hv_device(dev);
206         char alias_name[VMBUS_ALIAS_LEN + 1];
207
208         print_alias_name(hv_dev, alias_name);
209         return sprintf(buf, "vmbus:%s\n", alias_name);
210 }
211 static DEVICE_ATTR_RO(modalias);
212
213 static ssize_t server_monitor_pending_show(struct device *dev,
214                                            struct device_attribute *dev_attr,
215                                            char *buf)
216 {
217         struct hv_device *hv_dev = device_to_hv_device(dev);
218
219         if (!hv_dev->channel)
220                 return -ENODEV;
221         return sprintf(buf, "%d\n",
222                        channel_pending(hv_dev->channel,
223                                        vmbus_connection.monitor_pages[1]));
224 }
225 static DEVICE_ATTR_RO(server_monitor_pending);
226
227 static ssize_t client_monitor_pending_show(struct device *dev,
228                                            struct device_attribute *dev_attr,
229                                            char *buf)
230 {
231         struct hv_device *hv_dev = device_to_hv_device(dev);
232
233         if (!hv_dev->channel)
234                 return -ENODEV;
235         return sprintf(buf, "%d\n",
236                        channel_pending(hv_dev->channel,
237                                        vmbus_connection.monitor_pages[1]));
238 }
239 static DEVICE_ATTR_RO(client_monitor_pending);
240
241 static ssize_t server_monitor_latency_show(struct device *dev,
242                                            struct device_attribute *dev_attr,
243                                            char *buf)
244 {
245         struct hv_device *hv_dev = device_to_hv_device(dev);
246
247         if (!hv_dev->channel)
248                 return -ENODEV;
249         return sprintf(buf, "%d\n",
250                        channel_latency(hv_dev->channel,
251                                        vmbus_connection.monitor_pages[0]));
252 }
253 static DEVICE_ATTR_RO(server_monitor_latency);
254
255 static ssize_t client_monitor_latency_show(struct device *dev,
256                                            struct device_attribute *dev_attr,
257                                            char *buf)
258 {
259         struct hv_device *hv_dev = device_to_hv_device(dev);
260
261         if (!hv_dev->channel)
262                 return -ENODEV;
263         return sprintf(buf, "%d\n",
264                        channel_latency(hv_dev->channel,
265                                        vmbus_connection.monitor_pages[1]));
266 }
267 static DEVICE_ATTR_RO(client_monitor_latency);
268
269 static ssize_t server_monitor_conn_id_show(struct device *dev,
270                                            struct device_attribute *dev_attr,
271                                            char *buf)
272 {
273         struct hv_device *hv_dev = device_to_hv_device(dev);
274
275         if (!hv_dev->channel)
276                 return -ENODEV;
277         return sprintf(buf, "%d\n",
278                        channel_conn_id(hv_dev->channel,
279                                        vmbus_connection.monitor_pages[0]));
280 }
281 static DEVICE_ATTR_RO(server_monitor_conn_id);
282
283 static ssize_t client_monitor_conn_id_show(struct device *dev,
284                                            struct device_attribute *dev_attr,
285                                            char *buf)
286 {
287         struct hv_device *hv_dev = device_to_hv_device(dev);
288
289         if (!hv_dev->channel)
290                 return -ENODEV;
291         return sprintf(buf, "%d\n",
292                        channel_conn_id(hv_dev->channel,
293                                        vmbus_connection.monitor_pages[1]));
294 }
295 static DEVICE_ATTR_RO(client_monitor_conn_id);
296
297 static ssize_t out_intr_mask_show(struct device *dev,
298                                   struct device_attribute *dev_attr, char *buf)
299 {
300         struct hv_device *hv_dev = device_to_hv_device(dev);
301         struct hv_ring_buffer_debug_info outbound;
302
303         if (!hv_dev->channel)
304                 return -ENODEV;
305         hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
306         return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
307 }
308 static DEVICE_ATTR_RO(out_intr_mask);
309
310 static ssize_t out_read_index_show(struct device *dev,
311                                    struct device_attribute *dev_attr, char *buf)
312 {
313         struct hv_device *hv_dev = device_to_hv_device(dev);
314         struct hv_ring_buffer_debug_info outbound;
315
316         if (!hv_dev->channel)
317                 return -ENODEV;
318         hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
319         return sprintf(buf, "%d\n", outbound.current_read_index);
320 }
321 static DEVICE_ATTR_RO(out_read_index);
322
323 static ssize_t out_write_index_show(struct device *dev,
324                                     struct device_attribute *dev_attr,
325                                     char *buf)
326 {
327         struct hv_device *hv_dev = device_to_hv_device(dev);
328         struct hv_ring_buffer_debug_info outbound;
329
330         if (!hv_dev->channel)
331                 return -ENODEV;
332         hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
333         return sprintf(buf, "%d\n", outbound.current_write_index);
334 }
335 static DEVICE_ATTR_RO(out_write_index);
336
337 static ssize_t out_read_bytes_avail_show(struct device *dev,
338                                          struct device_attribute *dev_attr,
339                                          char *buf)
340 {
341         struct hv_device *hv_dev = device_to_hv_device(dev);
342         struct hv_ring_buffer_debug_info outbound;
343
344         if (!hv_dev->channel)
345                 return -ENODEV;
346         hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
347         return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
348 }
349 static DEVICE_ATTR_RO(out_read_bytes_avail);
350
351 static ssize_t out_write_bytes_avail_show(struct device *dev,
352                                           struct device_attribute *dev_attr,
353                                           char *buf)
354 {
355         struct hv_device *hv_dev = device_to_hv_device(dev);
356         struct hv_ring_buffer_debug_info outbound;
357
358         if (!hv_dev->channel)
359                 return -ENODEV;
360         hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
361         return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
362 }
363 static DEVICE_ATTR_RO(out_write_bytes_avail);
364
365 static ssize_t in_intr_mask_show(struct device *dev,
366                                  struct device_attribute *dev_attr, char *buf)
367 {
368         struct hv_device *hv_dev = device_to_hv_device(dev);
369         struct hv_ring_buffer_debug_info inbound;
370
371         if (!hv_dev->channel)
372                 return -ENODEV;
373         hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
374         return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
375 }
376 static DEVICE_ATTR_RO(in_intr_mask);
377
378 static ssize_t in_read_index_show(struct device *dev,
379                                   struct device_attribute *dev_attr, char *buf)
380 {
381         struct hv_device *hv_dev = device_to_hv_device(dev);
382         struct hv_ring_buffer_debug_info inbound;
383
384         if (!hv_dev->channel)
385                 return -ENODEV;
386         hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
387         return sprintf(buf, "%d\n", inbound.current_read_index);
388 }
389 static DEVICE_ATTR_RO(in_read_index);
390
391 static ssize_t in_write_index_show(struct device *dev,
392                                    struct device_attribute *dev_attr, char *buf)
393 {
394         struct hv_device *hv_dev = device_to_hv_device(dev);
395         struct hv_ring_buffer_debug_info inbound;
396
397         if (!hv_dev->channel)
398                 return -ENODEV;
399         hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
400         return sprintf(buf, "%d\n", inbound.current_write_index);
401 }
402 static DEVICE_ATTR_RO(in_write_index);
403
404 static ssize_t in_read_bytes_avail_show(struct device *dev,
405                                         struct device_attribute *dev_attr,
406                                         char *buf)
407 {
408         struct hv_device *hv_dev = device_to_hv_device(dev);
409         struct hv_ring_buffer_debug_info inbound;
410
411         if (!hv_dev->channel)
412                 return -ENODEV;
413         hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
414         return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
415 }
416 static DEVICE_ATTR_RO(in_read_bytes_avail);
417
418 static ssize_t in_write_bytes_avail_show(struct device *dev,
419                                          struct device_attribute *dev_attr,
420                                          char *buf)
421 {
422         struct hv_device *hv_dev = device_to_hv_device(dev);
423         struct hv_ring_buffer_debug_info inbound;
424
425         if (!hv_dev->channel)
426                 return -ENODEV;
427         hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
428         return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
429 }
430 static DEVICE_ATTR_RO(in_write_bytes_avail);
431
432 static ssize_t channel_vp_mapping_show(struct device *dev,
433                                        struct device_attribute *dev_attr,
434                                        char *buf)
435 {
436         struct hv_device *hv_dev = device_to_hv_device(dev);
437         struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
438         unsigned long flags;
439         int buf_size = PAGE_SIZE, n_written, tot_written;
440         struct list_head *cur;
441
442         if (!channel)
443                 return -ENODEV;
444
445         tot_written = snprintf(buf, buf_size, "%u:%u\n",
446                 channel->offermsg.child_relid, channel->target_cpu);
447
448         spin_lock_irqsave(&channel->lock, flags);
449
450         list_for_each(cur, &channel->sc_list) {
451                 if (tot_written >= buf_size - 1)
452                         break;
453
454                 cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
455                 n_written = scnprintf(buf + tot_written,
456                                      buf_size - tot_written,
457                                      "%u:%u\n",
458                                      cur_sc->offermsg.child_relid,
459                                      cur_sc->target_cpu);
460                 tot_written += n_written;
461         }
462
463         spin_unlock_irqrestore(&channel->lock, flags);
464
465         return tot_written;
466 }
467 static DEVICE_ATTR_RO(channel_vp_mapping);
468
469 static ssize_t vendor_show(struct device *dev,
470                            struct device_attribute *dev_attr,
471                            char *buf)
472 {
473         struct hv_device *hv_dev = device_to_hv_device(dev);
474         return sprintf(buf, "0x%x\n", hv_dev->vendor_id);
475 }
476 static DEVICE_ATTR_RO(vendor);
477
478 static ssize_t device_show(struct device *dev,
479                            struct device_attribute *dev_attr,
480                            char *buf)
481 {
482         struct hv_device *hv_dev = device_to_hv_device(dev);
483         return sprintf(buf, "0x%x\n", hv_dev->device_id);
484 }
485 static DEVICE_ATTR_RO(device);
486
487 /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
488 static struct attribute *vmbus_dev_attrs[] = {
489         &dev_attr_id.attr,
490         &dev_attr_state.attr,
491         &dev_attr_monitor_id.attr,
492         &dev_attr_class_id.attr,
493         &dev_attr_device_id.attr,
494         &dev_attr_modalias.attr,
495         &dev_attr_server_monitor_pending.attr,
496         &dev_attr_client_monitor_pending.attr,
497         &dev_attr_server_monitor_latency.attr,
498         &dev_attr_client_monitor_latency.attr,
499         &dev_attr_server_monitor_conn_id.attr,
500         &dev_attr_client_monitor_conn_id.attr,
501         &dev_attr_out_intr_mask.attr,
502         &dev_attr_out_read_index.attr,
503         &dev_attr_out_write_index.attr,
504         &dev_attr_out_read_bytes_avail.attr,
505         &dev_attr_out_write_bytes_avail.attr,
506         &dev_attr_in_intr_mask.attr,
507         &dev_attr_in_read_index.attr,
508         &dev_attr_in_write_index.attr,
509         &dev_attr_in_read_bytes_avail.attr,
510         &dev_attr_in_write_bytes_avail.attr,
511         &dev_attr_channel_vp_mapping.attr,
512         &dev_attr_vendor.attr,
513         &dev_attr_device.attr,
514         NULL,
515 };
516 ATTRIBUTE_GROUPS(vmbus_dev);
517
518 /*
519  * vmbus_uevent - add uevent for our device
520  *
521  * This routine is invoked when a device is added or removed on the vmbus to
522  * generate a uevent to udev in the userspace. The udev will then look at its
523  * rule and the uevent generated here to load the appropriate driver
524  *
525  * The alias string will be of the form vmbus:guid where guid is the string
526  * representation of the device guid (each byte of the guid will be
527  * represented with two hex characters.
528  */
529 static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
530 {
531         struct hv_device *dev = device_to_hv_device(device);
532         int ret;
533         char alias_name[VMBUS_ALIAS_LEN + 1];
534
535         print_alias_name(dev, alias_name);
536         ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name);
537         return ret;
538 }
539
540 static const uuid_le null_guid;
541
542 static inline bool is_null_guid(const uuid_le *guid)
543 {
544         if (uuid_le_cmp(*guid, null_guid))
545                 return false;
546         return true;
547 }
548
549 /*
550  * Return a matching hv_vmbus_device_id pointer.
551  * If there is no match, return NULL.
552  */
553 static const struct hv_vmbus_device_id *hv_vmbus_get_id(struct hv_driver *drv,
554                                         const uuid_le *guid)
555 {
556         const struct hv_vmbus_device_id *id = NULL;
557         struct vmbus_dynid *dynid;
558
559         /* Look at the dynamic ids first, before the static ones */
560         spin_lock(&drv->dynids.lock);
561         list_for_each_entry(dynid, &drv->dynids.list, node) {
562                 if (!uuid_le_cmp(dynid->id.guid, *guid)) {
563                         id = &dynid->id;
564                         break;
565                 }
566         }
567         spin_unlock(&drv->dynids.lock);
568
569         if (id)
570                 return id;
571
572         id = drv->id_table;
573         if (id == NULL)
574                 return NULL; /* empty device table */
575
576         for (; !is_null_guid(&id->guid); id++)
577                 if (!uuid_le_cmp(id->guid, *guid))
578                         return id;
579
580         return NULL;
581 }
582
583 /* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
584 static int vmbus_add_dynid(struct hv_driver *drv, uuid_le *guid)
585 {
586         struct vmbus_dynid *dynid;
587
588         dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
589         if (!dynid)
590                 return -ENOMEM;
591
592         dynid->id.guid = *guid;
593
594         spin_lock(&drv->dynids.lock);
595         list_add_tail(&dynid->node, &drv->dynids.list);
596         spin_unlock(&drv->dynids.lock);
597
598         return driver_attach(&drv->driver);
599 }
600
601 static void vmbus_free_dynids(struct hv_driver *drv)
602 {
603         struct vmbus_dynid *dynid, *n;
604
605         spin_lock(&drv->dynids.lock);
606         list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
607                 list_del(&dynid->node);
608                 kfree(dynid);
609         }
610         spin_unlock(&drv->dynids.lock);
611 }
612
613 /*
614  * store_new_id - sysfs frontend to vmbus_add_dynid()
615  *
616  * Allow GUIDs to be added to an existing driver via sysfs.
617  */
618 static ssize_t new_id_store(struct device_driver *driver, const char *buf,
619                             size_t count)
620 {
621         struct hv_driver *drv = drv_to_hv_drv(driver);
622         uuid_le guid;
623         ssize_t retval;
624
625         retval = uuid_le_to_bin(buf, &guid);
626         if (retval)
627                 return retval;
628
629         if (hv_vmbus_get_id(drv, &guid))
630                 return -EEXIST;
631
632         retval = vmbus_add_dynid(drv, &guid);
633         if (retval)
634                 return retval;
635         return count;
636 }
637 static DRIVER_ATTR_WO(new_id);
638
639 /*
640  * store_remove_id - remove a PCI device ID from this driver
641  *
642  * Removes a dynamic pci device ID to this driver.
643  */
644 static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
645                                size_t count)
646 {
647         struct hv_driver *drv = drv_to_hv_drv(driver);
648         struct vmbus_dynid *dynid, *n;
649         uuid_le guid;
650         ssize_t retval;
651
652         retval = uuid_le_to_bin(buf, &guid);
653         if (retval)
654                 return retval;
655
656         retval = -ENODEV;
657         spin_lock(&drv->dynids.lock);
658         list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
659                 struct hv_vmbus_device_id *id = &dynid->id;
660
661                 if (!uuid_le_cmp(id->guid, guid)) {
662                         list_del(&dynid->node);
663                         kfree(dynid);
664                         retval = count;
665                         break;
666                 }
667         }
668         spin_unlock(&drv->dynids.lock);
669
670         return retval;
671 }
672 static DRIVER_ATTR_WO(remove_id);
673
674 static struct attribute *vmbus_drv_attrs[] = {
675         &driver_attr_new_id.attr,
676         &driver_attr_remove_id.attr,
677         NULL,
678 };
679 ATTRIBUTE_GROUPS(vmbus_drv);
680
681
682 /*
683  * vmbus_match - Attempt to match the specified device to the specified driver
684  */
685 static int vmbus_match(struct device *device, struct device_driver *driver)
686 {
687         struct hv_driver *drv = drv_to_hv_drv(driver);
688         struct hv_device *hv_dev = device_to_hv_device(device);
689
690         /* The hv_sock driver handles all hv_sock offers. */
691         if (is_hvsock_channel(hv_dev->channel))
692                 return drv->hvsock;
693
694         if (hv_vmbus_get_id(drv, &hv_dev->dev_type))
695                 return 1;
696
697         return 0;
698 }
699
700 /*
701  * vmbus_probe - Add the new vmbus's child device
702  */
703 static int vmbus_probe(struct device *child_device)
704 {
705         int ret = 0;
706         struct hv_driver *drv =
707                         drv_to_hv_drv(child_device->driver);
708         struct hv_device *dev = device_to_hv_device(child_device);
709         const struct hv_vmbus_device_id *dev_id;
710
711         dev_id = hv_vmbus_get_id(drv, &dev->dev_type);
712         if (drv->probe) {
713                 ret = drv->probe(dev, dev_id);
714                 if (ret != 0)
715                         pr_err("probe failed for device %s (%d)\n",
716                                dev_name(child_device), ret);
717
718         } else {
719                 pr_err("probe not set for driver %s\n",
720                        dev_name(child_device));
721                 ret = -ENODEV;
722         }
723         return ret;
724 }
725
726 /*
727  * vmbus_remove - Remove a vmbus device
728  */
729 static int vmbus_remove(struct device *child_device)
730 {
731         struct hv_driver *drv;
732         struct hv_device *dev = device_to_hv_device(child_device);
733
734         if (child_device->driver) {
735                 drv = drv_to_hv_drv(child_device->driver);
736                 if (drv->remove)
737                         drv->remove(dev);
738         }
739
740         return 0;
741 }
742
743
744 /*
745  * vmbus_shutdown - Shutdown a vmbus device
746  */
747 static void vmbus_shutdown(struct device *child_device)
748 {
749         struct hv_driver *drv;
750         struct hv_device *dev = device_to_hv_device(child_device);
751
752
753         /* The device may not be attached yet */
754         if (!child_device->driver)
755                 return;
756
757         drv = drv_to_hv_drv(child_device->driver);
758
759         if (drv->shutdown)
760                 drv->shutdown(dev);
761 }
762
763
764 /*
765  * vmbus_device_release - Final callback release of the vmbus child device
766  */
767 static void vmbus_device_release(struct device *device)
768 {
769         struct hv_device *hv_dev = device_to_hv_device(device);
770         struct vmbus_channel *channel = hv_dev->channel;
771
772         mutex_lock(&vmbus_connection.channel_mutex);
773         hv_process_channel_removal(channel->offermsg.child_relid);
774         mutex_unlock(&vmbus_connection.channel_mutex);
775         kfree(hv_dev);
776
777 }
778
779 /* The one and only one */
780 static struct bus_type  hv_bus = {
781         .name =         "vmbus",
782         .match =                vmbus_match,
783         .shutdown =             vmbus_shutdown,
784         .remove =               vmbus_remove,
785         .probe =                vmbus_probe,
786         .uevent =               vmbus_uevent,
787         .dev_groups =           vmbus_dev_groups,
788         .drv_groups =           vmbus_drv_groups,
789 };
790
791 struct onmessage_work_context {
792         struct work_struct work;
793         struct hv_message msg;
794 };
795
796 static void vmbus_onmessage_work(struct work_struct *work)
797 {
798         struct onmessage_work_context *ctx;
799
800         /* Do not process messages if we're in DISCONNECTED state */
801         if (vmbus_connection.conn_state == DISCONNECTED)
802                 return;
803
804         ctx = container_of(work, struct onmessage_work_context,
805                            work);
806         vmbus_onmessage(&ctx->msg);
807         kfree(ctx);
808 }
809
810 static void hv_process_timer_expiration(struct hv_message *msg,
811                                         struct hv_per_cpu_context *hv_cpu)
812 {
813         struct clock_event_device *dev = hv_cpu->clk_evt;
814
815         if (dev->event_handler)
816                 dev->event_handler(dev);
817
818         vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
819 }
820
821 void vmbus_on_msg_dpc(unsigned long data)
822 {
823         struct hv_per_cpu_context *hv_cpu = (void *)data;
824         void *page_addr = hv_cpu->synic_message_page;
825         struct hv_message *msg = (struct hv_message *)page_addr +
826                                   VMBUS_MESSAGE_SINT;
827         struct vmbus_channel_message_header *hdr;
828         const struct vmbus_channel_message_table_entry *entry;
829         struct onmessage_work_context *ctx;
830         u32 message_type = msg->header.message_type;
831
832         if (message_type == HVMSG_NONE)
833                 /* no msg */
834                 return;
835
836         hdr = (struct vmbus_channel_message_header *)msg->u.payload;
837
838         trace_vmbus_on_msg_dpc(hdr);
839
840         if (hdr->msgtype >= CHANNELMSG_COUNT) {
841                 WARN_ONCE(1, "unknown msgtype=%d\n", hdr->msgtype);
842                 goto msg_handled;
843         }
844
845         entry = &channel_message_table[hdr->msgtype];
846         if (entry->handler_type == VMHT_BLOCKING) {
847                 ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
848                 if (ctx == NULL)
849                         return;
850
851                 INIT_WORK(&ctx->work, vmbus_onmessage_work);
852                 memcpy(&ctx->msg, msg, sizeof(*msg));
853
854                 /*
855                  * The host can generate a rescind message while we
856                  * may still be handling the original offer. We deal with
857                  * this condition by ensuring the processing is done on the
858                  * same CPU.
859                  */
860                 switch (hdr->msgtype) {
861                 case CHANNELMSG_RESCIND_CHANNELOFFER:
862                         /*
863                          * If we are handling the rescind message;
864                          * schedule the work on the global work queue.
865                          */
866                         schedule_work_on(vmbus_connection.connect_cpu,
867                                          &ctx->work);
868                         break;
869
870                 case CHANNELMSG_OFFERCHANNEL:
871                         atomic_inc(&vmbus_connection.offer_in_progress);
872                         queue_work_on(vmbus_connection.connect_cpu,
873                                       vmbus_connection.work_queue,
874                                       &ctx->work);
875                         break;
876
877                 default:
878                         queue_work(vmbus_connection.work_queue, &ctx->work);
879                 }
880         } else
881                 entry->message_handler(hdr);
882
883 msg_handled:
884         vmbus_signal_eom(msg, message_type);
885 }
886
887
888 /*
889  * Direct callback for channels using other deferred processing
890  */
891 static void vmbus_channel_isr(struct vmbus_channel *channel)
892 {
893         void (*callback_fn)(void *);
894
895         callback_fn = READ_ONCE(channel->onchannel_callback);
896         if (likely(callback_fn != NULL))
897                 (*callback_fn)(channel->channel_callback_context);
898 }
899
900 /*
901  * Schedule all channels with events pending
902  */
903 static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
904 {
905         unsigned long *recv_int_page;
906         u32 maxbits, relid;
907
908         if (vmbus_proto_version < VERSION_WIN8) {
909                 maxbits = MAX_NUM_CHANNELS_SUPPORTED;
910                 recv_int_page = vmbus_connection.recv_int_page;
911         } else {
912                 /*
913                  * When the host is win8 and beyond, the event page
914                  * can be directly checked to get the id of the channel
915                  * that has the interrupt pending.
916                  */
917                 void *page_addr = hv_cpu->synic_event_page;
918                 union hv_synic_event_flags *event
919                         = (union hv_synic_event_flags *)page_addr +
920                                                  VMBUS_MESSAGE_SINT;
921
922                 maxbits = HV_EVENT_FLAGS_COUNT;
923                 recv_int_page = event->flags;
924         }
925
926         if (unlikely(!recv_int_page))
927                 return;
928
929         for_each_set_bit(relid, recv_int_page, maxbits) {
930                 struct vmbus_channel *channel;
931
932                 if (!sync_test_and_clear_bit(relid, recv_int_page))
933                         continue;
934
935                 /* Special case - vmbus channel protocol msg */
936                 if (relid == 0)
937                         continue;
938
939                 rcu_read_lock();
940
941                 /* Find channel based on relid */
942                 list_for_each_entry_rcu(channel, &hv_cpu->chan_list, percpu_list) {
943                         if (channel->offermsg.child_relid != relid)
944                                 continue;
945
946                         if (channel->rescind)
947                                 continue;
948
949                         trace_vmbus_chan_sched(channel);
950
951                         ++channel->interrupts;
952
953                         switch (channel->callback_mode) {
954                         case HV_CALL_ISR:
955                                 vmbus_channel_isr(channel);
956                                 break;
957
958                         case HV_CALL_BATCHED:
959                                 hv_begin_read(&channel->inbound);
960                                 /* fallthrough */
961                         case HV_CALL_DIRECT:
962                                 tasklet_schedule(&channel->callback_event);
963                         }
964                 }
965
966                 rcu_read_unlock();
967         }
968 }
969
970 static void vmbus_isr(void)
971 {
972         struct hv_per_cpu_context *hv_cpu
973                 = this_cpu_ptr(hv_context.cpu_context);
974         void *page_addr = hv_cpu->synic_event_page;
975         struct hv_message *msg;
976         union hv_synic_event_flags *event;
977         bool handled = false;
978
979         if (unlikely(page_addr == NULL))
980                 return;
981
982         event = (union hv_synic_event_flags *)page_addr +
983                                          VMBUS_MESSAGE_SINT;
984         /*
985          * Check for events before checking for messages. This is the order
986          * in which events and messages are checked in Windows guests on
987          * Hyper-V, and the Windows team suggested we do the same.
988          */
989
990         if ((vmbus_proto_version == VERSION_WS2008) ||
991                 (vmbus_proto_version == VERSION_WIN7)) {
992
993                 /* Since we are a child, we only need to check bit 0 */
994                 if (sync_test_and_clear_bit(0, event->flags))
995                         handled = true;
996         } else {
997                 /*
998                  * Our host is win8 or above. The signaling mechanism
999                  * has changed and we can directly look at the event page.
1000                  * If bit n is set then we have an interrup on the channel
1001                  * whose id is n.
1002                  */
1003                 handled = true;
1004         }
1005
1006         if (handled)
1007                 vmbus_chan_sched(hv_cpu);
1008
1009         page_addr = hv_cpu->synic_message_page;
1010         msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
1011
1012         /* Check if there are actual msgs to be processed */
1013         if (msg->header.message_type != HVMSG_NONE) {
1014                 if (msg->header.message_type == HVMSG_TIMER_EXPIRED)
1015                         hv_process_timer_expiration(msg, hv_cpu);
1016                 else
1017                         tasklet_schedule(&hv_cpu->msg_dpc);
1018         }
1019
1020         add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0);
1021 }
1022
1023
1024 /*
1025  * vmbus_bus_init -Main vmbus driver initialization routine.
1026  *
1027  * Here, we
1028  *      - initialize the vmbus driver context
1029  *      - invoke the vmbus hv main init routine
1030  *      - retrieve the channel offers
1031  */
1032 static int vmbus_bus_init(void)
1033 {
1034         int ret;
1035
1036         /* Hypervisor initialization...setup hypercall page..etc */
1037         ret = hv_init();
1038         if (ret != 0) {
1039                 pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
1040                 return ret;
1041         }
1042
1043         ret = bus_register(&hv_bus);
1044         if (ret)
1045                 return ret;
1046
1047         hv_setup_vmbus_irq(vmbus_isr);
1048
1049         ret = hv_synic_alloc();
1050         if (ret)
1051                 goto err_alloc;
1052         /*
1053          * Initialize the per-cpu interrupt state and
1054          * connect to the host.
1055          */
1056         ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv:online",
1057                                 hv_synic_init, hv_synic_cleanup);
1058         if (ret < 0)
1059                 goto err_alloc;
1060         hyperv_cpuhp_online = ret;
1061
1062         ret = vmbus_connect();
1063         if (ret)
1064                 goto err_connect;
1065
1066         /*
1067          * Only register if the crash MSRs are available
1068          */
1069         if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
1070                 register_die_notifier(&hyperv_die_block);
1071                 atomic_notifier_chain_register(&panic_notifier_list,
1072                                                &hyperv_panic_block);
1073         }
1074
1075         vmbus_request_offers();
1076
1077         return 0;
1078
1079 err_connect:
1080         cpuhp_remove_state(hyperv_cpuhp_online);
1081 err_alloc:
1082         hv_synic_free();
1083         hv_remove_vmbus_irq();
1084
1085         bus_unregister(&hv_bus);
1086
1087         return ret;
1088 }
1089
1090 /**
1091  * __vmbus_child_driver_register() - Register a vmbus's driver
1092  * @hv_driver: Pointer to driver structure you want to register
1093  * @owner: owner module of the drv
1094  * @mod_name: module name string
1095  *
1096  * Registers the given driver with Linux through the 'driver_register()' call
1097  * and sets up the hyper-v vmbus handling for this driver.
1098  * It will return the state of the 'driver_register()' call.
1099  *
1100  */
1101 int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
1102 {
1103         int ret;
1104
1105         pr_info("registering driver %s\n", hv_driver->name);
1106
1107         ret = vmbus_exists();
1108         if (ret < 0)
1109                 return ret;
1110
1111         hv_driver->driver.name = hv_driver->name;
1112         hv_driver->driver.owner = owner;
1113         hv_driver->driver.mod_name = mod_name;
1114         hv_driver->driver.bus = &hv_bus;
1115
1116         spin_lock_init(&hv_driver->dynids.lock);
1117         INIT_LIST_HEAD(&hv_driver->dynids.list);
1118
1119         ret = driver_register(&hv_driver->driver);
1120
1121         return ret;
1122 }
1123 EXPORT_SYMBOL_GPL(__vmbus_driver_register);
1124
1125 /**
1126  * vmbus_driver_unregister() - Unregister a vmbus's driver
1127  * @hv_driver: Pointer to driver structure you want to
1128  *             un-register
1129  *
1130  * Un-register the given driver that was previous registered with a call to
1131  * vmbus_driver_register()
1132  */
1133 void vmbus_driver_unregister(struct hv_driver *hv_driver)
1134 {
1135         pr_info("unregistering driver %s\n", hv_driver->name);
1136
1137         if (!vmbus_exists()) {
1138                 driver_unregister(&hv_driver->driver);
1139                 vmbus_free_dynids(hv_driver);
1140         }
1141 }
1142 EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
1143
1144
1145 /*
1146  * Called when last reference to channel is gone.
1147  */
1148 static void vmbus_chan_release(struct kobject *kobj)
1149 {
1150         struct vmbus_channel *channel
1151                 = container_of(kobj, struct vmbus_channel, kobj);
1152
1153         kfree_rcu(channel, rcu);
1154 }
1155
1156 struct vmbus_chan_attribute {
1157         struct attribute attr;
1158         ssize_t (*show)(const struct vmbus_channel *chan, char *buf);
1159         ssize_t (*store)(struct vmbus_channel *chan,
1160                          const char *buf, size_t count);
1161 };
1162 #define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \
1163         struct vmbus_chan_attribute chan_attr_##_name \
1164                 = __ATTR(_name, _mode, _show, _store)
1165 #define VMBUS_CHAN_ATTR_RW(_name) \
1166         struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name)
1167 #define VMBUS_CHAN_ATTR_RO(_name) \
1168         struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name)
1169 #define VMBUS_CHAN_ATTR_WO(_name) \
1170         struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name)
1171
1172 static ssize_t vmbus_chan_attr_show(struct kobject *kobj,
1173                                     struct attribute *attr, char *buf)
1174 {
1175         const struct vmbus_chan_attribute *attribute
1176                 = container_of(attr, struct vmbus_chan_attribute, attr);
1177         const struct vmbus_channel *chan
1178                 = container_of(kobj, struct vmbus_channel, kobj);
1179
1180         if (!attribute->show)
1181                 return -EIO;
1182
1183         return attribute->show(chan, buf);
1184 }
1185
1186 static const struct sysfs_ops vmbus_chan_sysfs_ops = {
1187         .show = vmbus_chan_attr_show,
1188 };
1189
1190 static ssize_t out_mask_show(const struct vmbus_channel *channel, char *buf)
1191 {
1192         const struct hv_ring_buffer_info *rbi = &channel->outbound;
1193
1194         return sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1195 }
1196 VMBUS_CHAN_ATTR_RO(out_mask);
1197
1198 static ssize_t in_mask_show(const struct vmbus_channel *channel, char *buf)
1199 {
1200         const struct hv_ring_buffer_info *rbi = &channel->inbound;
1201
1202         return sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1203 }
1204 VMBUS_CHAN_ATTR_RO(in_mask);
1205
1206 static ssize_t read_avail_show(const struct vmbus_channel *channel, char *buf)
1207 {
1208         const struct hv_ring_buffer_info *rbi = &channel->inbound;
1209
1210         return sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
1211 }
1212 VMBUS_CHAN_ATTR_RO(read_avail);
1213
1214 static ssize_t write_avail_show(const struct vmbus_channel *channel, char *buf)
1215 {
1216         const struct hv_ring_buffer_info *rbi = &channel->outbound;
1217
1218         return sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
1219 }
1220 VMBUS_CHAN_ATTR_RO(write_avail);
1221
1222 static ssize_t show_target_cpu(const struct vmbus_channel *channel, char *buf)
1223 {
1224         return sprintf(buf, "%u\n", channel->target_cpu);
1225 }
1226 VMBUS_CHAN_ATTR(cpu, S_IRUGO, show_target_cpu, NULL);
1227
1228 static ssize_t channel_pending_show(const struct vmbus_channel *channel,
1229                                     char *buf)
1230 {
1231         return sprintf(buf, "%d\n",
1232                        channel_pending(channel,
1233                                        vmbus_connection.monitor_pages[1]));
1234 }
1235 VMBUS_CHAN_ATTR(pending, S_IRUGO, channel_pending_show, NULL);
1236
1237 static ssize_t channel_latency_show(const struct vmbus_channel *channel,
1238                                     char *buf)
1239 {
1240         return sprintf(buf, "%d\n",
1241                        channel_latency(channel,
1242                                        vmbus_connection.monitor_pages[1]));
1243 }
1244 VMBUS_CHAN_ATTR(latency, S_IRUGO, channel_latency_show, NULL);
1245
1246 static ssize_t channel_interrupts_show(const struct vmbus_channel *channel, char *buf)
1247 {
1248         return sprintf(buf, "%llu\n", channel->interrupts);
1249 }
1250 VMBUS_CHAN_ATTR(interrupts, S_IRUGO, channel_interrupts_show, NULL);
1251
1252 static ssize_t channel_events_show(const struct vmbus_channel *channel, char *buf)
1253 {
1254         return sprintf(buf, "%llu\n", channel->sig_events);
1255 }
1256 VMBUS_CHAN_ATTR(events, S_IRUGO, channel_events_show, NULL);
1257
1258 static struct attribute *vmbus_chan_attrs[] = {
1259         &chan_attr_out_mask.attr,
1260         &chan_attr_in_mask.attr,
1261         &chan_attr_read_avail.attr,
1262         &chan_attr_write_avail.attr,
1263         &chan_attr_cpu.attr,
1264         &chan_attr_pending.attr,
1265         &chan_attr_latency.attr,
1266         &chan_attr_interrupts.attr,
1267         &chan_attr_events.attr,
1268         NULL
1269 };
1270
1271 static struct kobj_type vmbus_chan_ktype = {
1272         .sysfs_ops = &vmbus_chan_sysfs_ops,
1273         .release = vmbus_chan_release,
1274         .default_attrs = vmbus_chan_attrs,
1275 };
1276
1277 /*
1278  * vmbus_add_channel_kobj - setup a sub-directory under device/channels
1279  */
1280 int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
1281 {
1282         struct kobject *kobj = &channel->kobj;
1283         u32 relid = channel->offermsg.child_relid;
1284         int ret;
1285
1286         kobj->kset = dev->channels_kset;
1287         ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
1288                                    "%u", relid);
1289         if (ret)
1290                 return ret;
1291
1292         kobject_uevent(kobj, KOBJ_ADD);
1293
1294         return 0;
1295 }
1296
1297 /*
1298  * vmbus_device_create - Creates and registers a new child device
1299  * on the vmbus.
1300  */
1301 struct hv_device *vmbus_device_create(const uuid_le *type,
1302                                       const uuid_le *instance,
1303                                       struct vmbus_channel *channel)
1304 {
1305         struct hv_device *child_device_obj;
1306
1307         child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
1308         if (!child_device_obj) {
1309                 pr_err("Unable to allocate device object for child device\n");
1310                 return NULL;
1311         }
1312
1313         child_device_obj->channel = channel;
1314         memcpy(&child_device_obj->dev_type, type, sizeof(uuid_le));
1315         memcpy(&child_device_obj->dev_instance, instance,
1316                sizeof(uuid_le));
1317         child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */
1318
1319
1320         return child_device_obj;
1321 }
1322
1323 /*
1324  * vmbus_device_register - Register the child device
1325  */
1326 int vmbus_device_register(struct hv_device *child_device_obj)
1327 {
1328         struct kobject *kobj = &child_device_obj->device.kobj;
1329         int ret;
1330
1331         dev_set_name(&child_device_obj->device, "%pUl",
1332                      child_device_obj->channel->offermsg.offer.if_instance.b);
1333
1334         child_device_obj->device.bus = &hv_bus;
1335         child_device_obj->device.parent = &hv_acpi_dev->dev;
1336         child_device_obj->device.release = vmbus_device_release;
1337
1338         /*
1339          * Register with the LDM. This will kick off the driver/device
1340          * binding...which will eventually call vmbus_match() and vmbus_probe()
1341          */
1342         ret = device_register(&child_device_obj->device);
1343         if (ret) {
1344                 pr_err("Unable to register child device\n");
1345                 return ret;
1346         }
1347
1348         child_device_obj->channels_kset = kset_create_and_add("channels",
1349                                                               NULL, kobj);
1350         if (!child_device_obj->channels_kset) {
1351                 ret = -ENOMEM;
1352                 goto err_dev_unregister;
1353         }
1354
1355         ret = vmbus_add_channel_kobj(child_device_obj,
1356                                      child_device_obj->channel);
1357         if (ret) {
1358                 pr_err("Unable to register primary channeln");
1359                 goto err_kset_unregister;
1360         }
1361
1362         return 0;
1363
1364 err_kset_unregister:
1365         kset_unregister(child_device_obj->channels_kset);
1366
1367 err_dev_unregister:
1368         device_unregister(&child_device_obj->device);
1369         return ret;
1370 }
1371
1372 /*
1373  * vmbus_device_unregister - Remove the specified child device
1374  * from the vmbus.
1375  */
1376 void vmbus_device_unregister(struct hv_device *device_obj)
1377 {
1378         pr_debug("child device %s unregistered\n",
1379                 dev_name(&device_obj->device));
1380
1381         kset_unregister(device_obj->channels_kset);
1382
1383         /*
1384          * Kick off the process of unregistering the device.
1385          * This will call vmbus_remove() and eventually vmbus_device_release()
1386          */
1387         device_unregister(&device_obj->device);
1388 }
1389
1390
1391 /*
1392  * VMBUS is an acpi enumerated device. Get the information we
1393  * need from DSDT.
1394  */
1395 #define VTPM_BASE_ADDRESS 0xfed40000
1396 static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
1397 {
1398         resource_size_t start = 0;
1399         resource_size_t end = 0;
1400         struct resource *new_res;
1401         struct resource **old_res = &hyperv_mmio;
1402         struct resource **prev_res = NULL;
1403
1404         switch (res->type) {
1405
1406         /*
1407          * "Address" descriptors are for bus windows. Ignore
1408          * "memory" descriptors, which are for registers on
1409          * devices.
1410          */
1411         case ACPI_RESOURCE_TYPE_ADDRESS32:
1412                 start = res->data.address32.address.minimum;
1413                 end = res->data.address32.address.maximum;
1414                 break;
1415
1416         case ACPI_RESOURCE_TYPE_ADDRESS64:
1417                 start = res->data.address64.address.minimum;
1418                 end = res->data.address64.address.maximum;
1419                 break;
1420
1421         default:
1422                 /* Unused resource type */
1423                 return AE_OK;
1424
1425         }
1426         /*
1427          * Ignore ranges that are below 1MB, as they're not
1428          * necessary or useful here.
1429          */
1430         if (end < 0x100000)
1431                 return AE_OK;
1432
1433         new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
1434         if (!new_res)
1435                 return AE_NO_MEMORY;
1436
1437         /* If this range overlaps the virtual TPM, truncate it. */
1438         if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
1439                 end = VTPM_BASE_ADDRESS;
1440
1441         new_res->name = "hyperv mmio";
1442         new_res->flags = IORESOURCE_MEM;
1443         new_res->start = start;
1444         new_res->end = end;
1445
1446         /*
1447          * If two ranges are adjacent, merge them.
1448          */
1449         do {
1450                 if (!*old_res) {
1451                         *old_res = new_res;
1452                         break;
1453                 }
1454
1455                 if (((*old_res)->end + 1) == new_res->start) {
1456                         (*old_res)->end = new_res->end;
1457                         kfree(new_res);
1458                         break;
1459                 }
1460
1461                 if ((*old_res)->start == new_res->end + 1) {
1462                         (*old_res)->start = new_res->start;
1463                         kfree(new_res);
1464                         break;
1465                 }
1466
1467                 if ((*old_res)->start > new_res->end) {
1468                         new_res->sibling = *old_res;
1469                         if (prev_res)
1470                                 (*prev_res)->sibling = new_res;
1471                         *old_res = new_res;
1472                         break;
1473                 }
1474
1475                 prev_res = old_res;
1476                 old_res = &(*old_res)->sibling;
1477
1478         } while (1);
1479
1480         return AE_OK;
1481 }
1482
1483 static int vmbus_acpi_remove(struct acpi_device *device)
1484 {
1485         struct resource *cur_res;
1486         struct resource *next_res;
1487
1488         if (hyperv_mmio) {
1489                 if (fb_mmio) {
1490                         __release_region(hyperv_mmio, fb_mmio->start,
1491                                          resource_size(fb_mmio));
1492                         fb_mmio = NULL;
1493                 }
1494
1495                 for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
1496                         next_res = cur_res->sibling;
1497                         kfree(cur_res);
1498                 }
1499         }
1500
1501         return 0;
1502 }
1503
1504 static void vmbus_reserve_fb(void)
1505 {
1506         int size;
1507         /*
1508          * Make a claim for the frame buffer in the resource tree under the
1509          * first node, which will be the one below 4GB.  The length seems to
1510          * be underreported, particularly in a Generation 1 VM.  So start out
1511          * reserving a larger area and make it smaller until it succeeds.
1512          */
1513
1514         if (screen_info.lfb_base) {
1515                 if (efi_enabled(EFI_BOOT))
1516                         size = max_t(__u32, screen_info.lfb_size, 0x800000);
1517                 else
1518                         size = max_t(__u32, screen_info.lfb_size, 0x4000000);
1519
1520                 for (; !fb_mmio && (size >= 0x100000); size >>= 1) {
1521                         fb_mmio = __request_region(hyperv_mmio,
1522                                                    screen_info.lfb_base, size,
1523                                                    fb_mmio_name, 0);
1524                 }
1525         }
1526 }
1527
1528 /**
1529  * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
1530  * @new:                If successful, supplied a pointer to the
1531  *                      allocated MMIO space.
1532  * @device_obj:         Identifies the caller
1533  * @min:                Minimum guest physical address of the
1534  *                      allocation
1535  * @max:                Maximum guest physical address
1536  * @size:               Size of the range to be allocated
1537  * @align:              Alignment of the range to be allocated
1538  * @fb_overlap_ok:      Whether this allocation can be allowed
1539  *                      to overlap the video frame buffer.
1540  *
1541  * This function walks the resources granted to VMBus by the
1542  * _CRS object in the ACPI namespace underneath the parent
1543  * "bridge" whether that's a root PCI bus in the Generation 1
1544  * case or a Module Device in the Generation 2 case.  It then
1545  * attempts to allocate from the global MMIO pool in a way that
1546  * matches the constraints supplied in these parameters and by
1547  * that _CRS.
1548  *
1549  * Return: 0 on success, -errno on failure
1550  */
1551 int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
1552                         resource_size_t min, resource_size_t max,
1553                         resource_size_t size, resource_size_t align,
1554                         bool fb_overlap_ok)
1555 {
1556         struct resource *iter, *shadow;
1557         resource_size_t range_min, range_max, start;
1558         const char *dev_n = dev_name(&device_obj->device);
1559         int retval;
1560
1561         retval = -ENXIO;
1562         down(&hyperv_mmio_lock);
1563
1564         /*
1565          * If overlaps with frame buffers are allowed, then first attempt to
1566          * make the allocation from within the reserved region.  Because it
1567          * is already reserved, no shadow allocation is necessary.
1568          */
1569         if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
1570             !(max < fb_mmio->start)) {
1571
1572                 range_min = fb_mmio->start;
1573                 range_max = fb_mmio->end;
1574                 start = (range_min + align - 1) & ~(align - 1);
1575                 for (; start + size - 1 <= range_max; start += align) {
1576                         *new = request_mem_region_exclusive(start, size, dev_n);
1577                         if (*new) {
1578                                 retval = 0;
1579                                 goto exit;
1580                         }
1581                 }
1582         }
1583
1584         for (iter = hyperv_mmio; iter; iter = iter->sibling) {
1585                 if ((iter->start >= max) || (iter->end <= min))
1586                         continue;
1587
1588                 range_min = iter->start;
1589                 range_max = iter->end;
1590                 start = (range_min + align - 1) & ~(align - 1);
1591                 for (; start + size - 1 <= range_max; start += align) {
1592                         shadow = __request_region(iter, start, size, NULL,
1593                                                   IORESOURCE_BUSY);
1594                         if (!shadow)
1595                                 continue;
1596
1597                         *new = request_mem_region_exclusive(start, size, dev_n);
1598                         if (*new) {
1599                                 shadow->name = (char *)*new;
1600                                 retval = 0;
1601                                 goto exit;
1602                         }
1603
1604                         __release_region(iter, start, size);
1605                 }
1606         }
1607
1608 exit:
1609         up(&hyperv_mmio_lock);
1610         return retval;
1611 }
1612 EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
1613
1614 /**
1615  * vmbus_free_mmio() - Free a memory-mapped I/O range.
1616  * @start:              Base address of region to release.
1617  * @size:               Size of the range to be allocated
1618  *
1619  * This function releases anything requested by
1620  * vmbus_mmio_allocate().
1621  */
1622 void vmbus_free_mmio(resource_size_t start, resource_size_t size)
1623 {
1624         struct resource *iter;
1625
1626         down(&hyperv_mmio_lock);
1627         for (iter = hyperv_mmio; iter; iter = iter->sibling) {
1628                 if ((iter->start >= start + size) || (iter->end <= start))
1629                         continue;
1630
1631                 __release_region(iter, start, size);
1632         }
1633         release_mem_region(start, size);
1634         up(&hyperv_mmio_lock);
1635
1636 }
1637 EXPORT_SYMBOL_GPL(vmbus_free_mmio);
1638
1639 static int vmbus_acpi_add(struct acpi_device *device)
1640 {
1641         acpi_status result;
1642         int ret_val = -ENODEV;
1643         struct acpi_device *ancestor;
1644
1645         hv_acpi_dev = device;
1646
1647         result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
1648                                         vmbus_walk_resources, NULL);
1649
1650         if (ACPI_FAILURE(result))
1651                 goto acpi_walk_err;
1652         /*
1653          * Some ancestor of the vmbus acpi device (Gen1 or Gen2
1654          * firmware) is the VMOD that has the mmio ranges. Get that.
1655          */
1656         for (ancestor = device->parent; ancestor; ancestor = ancestor->parent) {
1657                 result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
1658                                              vmbus_walk_resources, NULL);
1659
1660                 if (ACPI_FAILURE(result))
1661                         continue;
1662                 if (hyperv_mmio) {
1663                         vmbus_reserve_fb();
1664                         break;
1665                 }
1666         }
1667         ret_val = 0;
1668
1669 acpi_walk_err:
1670         complete(&probe_event);
1671         if (ret_val)
1672                 vmbus_acpi_remove(device);
1673         return ret_val;
1674 }
1675
1676 static const struct acpi_device_id vmbus_acpi_device_ids[] = {
1677         {"VMBUS", 0},
1678         {"VMBus", 0},
1679         {"", 0},
1680 };
1681 MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
1682
1683 static struct acpi_driver vmbus_acpi_driver = {
1684         .name = "vmbus",
1685         .ids = vmbus_acpi_device_ids,
1686         .ops = {
1687                 .add = vmbus_acpi_add,
1688                 .remove = vmbus_acpi_remove,
1689         },
1690 };
1691
1692 static void hv_kexec_handler(void)
1693 {
1694         hv_synic_clockevents_cleanup();
1695         vmbus_initiate_unload(false);
1696         vmbus_connection.conn_state = DISCONNECTED;
1697         /* Make sure conn_state is set as hv_synic_cleanup checks for it */
1698         mb();
1699         cpuhp_remove_state(hyperv_cpuhp_online);
1700         hyperv_cleanup();
1701 };
1702
1703 static void hv_crash_handler(struct pt_regs *regs)
1704 {
1705         vmbus_initiate_unload(true);
1706         /*
1707          * In crash handler we can't schedule synic cleanup for all CPUs,
1708          * doing the cleanup for current CPU only. This should be sufficient
1709          * for kdump.
1710          */
1711         vmbus_connection.conn_state = DISCONNECTED;
1712         hv_synic_cleanup(smp_processor_id());
1713         hyperv_cleanup();
1714 };
1715
1716 static int __init hv_acpi_init(void)
1717 {
1718         int ret, t;
1719
1720         if (x86_hyper_type != X86_HYPER_MS_HYPERV)
1721                 return -ENODEV;
1722
1723         init_completion(&probe_event);
1724
1725         /*
1726          * Get ACPI resources first.
1727          */
1728         ret = acpi_bus_register_driver(&vmbus_acpi_driver);
1729
1730         if (ret)
1731                 return ret;
1732
1733         t = wait_for_completion_timeout(&probe_event, 5*HZ);
1734         if (t == 0) {
1735                 ret = -ETIMEDOUT;
1736                 goto cleanup;
1737         }
1738
1739         ret = vmbus_bus_init();
1740         if (ret)
1741                 goto cleanup;
1742
1743         hv_setup_kexec_handler(hv_kexec_handler);
1744         hv_setup_crash_handler(hv_crash_handler);
1745
1746         return 0;
1747
1748 cleanup:
1749         acpi_bus_unregister_driver(&vmbus_acpi_driver);
1750         hv_acpi_dev = NULL;
1751         return ret;
1752 }
1753
1754 static void __exit vmbus_exit(void)
1755 {
1756         int cpu;
1757
1758         hv_remove_kexec_handler();
1759         hv_remove_crash_handler();
1760         vmbus_connection.conn_state = DISCONNECTED;
1761         hv_synic_clockevents_cleanup();
1762         vmbus_disconnect();
1763         hv_remove_vmbus_irq();
1764         for_each_online_cpu(cpu) {
1765                 struct hv_per_cpu_context *hv_cpu
1766                         = per_cpu_ptr(hv_context.cpu_context, cpu);
1767
1768                 tasklet_kill(&hv_cpu->msg_dpc);
1769         }
1770         vmbus_free_channels();
1771
1772         if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
1773                 unregister_die_notifier(&hyperv_die_block);
1774                 atomic_notifier_chain_unregister(&panic_notifier_list,
1775                                                  &hyperv_panic_block);
1776         }
1777         bus_unregister(&hv_bus);
1778
1779         cpuhp_remove_state(hyperv_cpuhp_online);
1780         hv_synic_free();
1781         acpi_bus_unregister_driver(&vmbus_acpi_driver);
1782 }
1783
1784
1785 MODULE_LICENSE("GPL");
1786
1787 subsys_initcall(hv_acpi_init);
1788 module_exit(vmbus_exit);