xref: /OK3568_Linux_fs/kernel/drivers/hv/vmbus_drv.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2009, Microsoft Corporation.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Authors:
6*4882a593Smuzhiyun  *   Haiyang Zhang <haiyangz@microsoft.com>
7*4882a593Smuzhiyun  *   Hank Janssen  <hjanssen@microsoft.com>
8*4882a593Smuzhiyun  *   K. Y. Srinivasan <kys@microsoft.com>
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/init.h>
13*4882a593Smuzhiyun #include <linux/module.h>
14*4882a593Smuzhiyun #include <linux/device.h>
15*4882a593Smuzhiyun #include <linux/interrupt.h>
16*4882a593Smuzhiyun #include <linux/sysctl.h>
17*4882a593Smuzhiyun #include <linux/slab.h>
18*4882a593Smuzhiyun #include <linux/acpi.h>
19*4882a593Smuzhiyun #include <linux/completion.h>
20*4882a593Smuzhiyun #include <linux/hyperv.h>
21*4882a593Smuzhiyun #include <linux/kernel_stat.h>
22*4882a593Smuzhiyun #include <linux/clockchips.h>
23*4882a593Smuzhiyun #include <linux/cpu.h>
24*4882a593Smuzhiyun #include <linux/sched/task_stack.h>
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #include <linux/delay.h>
27*4882a593Smuzhiyun #include <linux/notifier.h>
28*4882a593Smuzhiyun #include <linux/ptrace.h>
29*4882a593Smuzhiyun #include <linux/screen_info.h>
30*4882a593Smuzhiyun #include <linux/kdebug.h>
31*4882a593Smuzhiyun #include <linux/efi.h>
32*4882a593Smuzhiyun #include <linux/random.h>
33*4882a593Smuzhiyun #include <linux/kernel.h>
34*4882a593Smuzhiyun #include <linux/syscore_ops.h>
35*4882a593Smuzhiyun #include <clocksource/hyperv_timer.h>
36*4882a593Smuzhiyun #include "hyperv_vmbus.h"
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun struct vmbus_dynid {
39*4882a593Smuzhiyun 	struct list_head node;
40*4882a593Smuzhiyun 	struct hv_vmbus_device_id id;
41*4882a593Smuzhiyun };
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun static struct acpi_device  *hv_acpi_dev;
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun static struct completion probe_event;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun static int hyperv_cpuhp_online;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun static void *hv_panic_page;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun /* Values parsed from ACPI DSDT */
52*4882a593Smuzhiyun static int vmbus_irq;
53*4882a593Smuzhiyun int vmbus_interrupt;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun /*
56*4882a593Smuzhiyun  * Boolean to control whether to report panic messages over Hyper-V.
57*4882a593Smuzhiyun  *
58*4882a593Smuzhiyun  * It can be set via /proc/sys/kernel/hyperv/record_panic_msg
59*4882a593Smuzhiyun  */
60*4882a593Smuzhiyun static int sysctl_record_panic_msg = 1;
61*4882a593Smuzhiyun 
hyperv_report_reg(void)62*4882a593Smuzhiyun static int hyperv_report_reg(void)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	return !sysctl_record_panic_msg || !hv_panic_page;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun 
hyperv_panic_event(struct notifier_block * nb,unsigned long val,void * args)67*4882a593Smuzhiyun static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
68*4882a593Smuzhiyun 			      void *args)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	struct pt_regs *regs;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	vmbus_initiate_unload(true);
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	/*
75*4882a593Smuzhiyun 	 * Hyper-V should be notified only once about a panic.  If we will be
76*4882a593Smuzhiyun 	 * doing hyperv_report_panic_msg() later with kmsg data, don't do
77*4882a593Smuzhiyun 	 * the notification here.
78*4882a593Smuzhiyun 	 */
79*4882a593Smuzhiyun 	if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE
80*4882a593Smuzhiyun 	    && hyperv_report_reg()) {
81*4882a593Smuzhiyun 		regs = current_pt_regs();
82*4882a593Smuzhiyun 		hyperv_report_panic(regs, val, false);
83*4882a593Smuzhiyun 	}
84*4882a593Smuzhiyun 	return NOTIFY_DONE;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun 
hyperv_die_event(struct notifier_block * nb,unsigned long val,void * args)87*4882a593Smuzhiyun static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
88*4882a593Smuzhiyun 			    void *args)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	struct die_args *die = args;
91*4882a593Smuzhiyun 	struct pt_regs *regs = die->regs;
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	/* Don't notify Hyper-V if the die event is other than oops */
94*4882a593Smuzhiyun 	if (val != DIE_OOPS)
95*4882a593Smuzhiyun 		return NOTIFY_DONE;
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	/*
98*4882a593Smuzhiyun 	 * Hyper-V should be notified only once about a panic.  If we will be
99*4882a593Smuzhiyun 	 * doing hyperv_report_panic_msg() later with kmsg data, don't do
100*4882a593Smuzhiyun 	 * the notification here.
101*4882a593Smuzhiyun 	 */
102*4882a593Smuzhiyun 	if (hyperv_report_reg())
103*4882a593Smuzhiyun 		hyperv_report_panic(regs, val, true);
104*4882a593Smuzhiyun 	return NOTIFY_DONE;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun static struct notifier_block hyperv_die_block = {
108*4882a593Smuzhiyun 	.notifier_call = hyperv_die_event,
109*4882a593Smuzhiyun };
110*4882a593Smuzhiyun static struct notifier_block hyperv_panic_block = {
111*4882a593Smuzhiyun 	.notifier_call = hyperv_panic_event,
112*4882a593Smuzhiyun };
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun static const char *fb_mmio_name = "fb_range";
115*4882a593Smuzhiyun static struct resource *fb_mmio;
116*4882a593Smuzhiyun static struct resource *hyperv_mmio;
117*4882a593Smuzhiyun static DEFINE_MUTEX(hyperv_mmio_lock);
118*4882a593Smuzhiyun 
vmbus_exists(void)119*4882a593Smuzhiyun static int vmbus_exists(void)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun 	if (hv_acpi_dev == NULL)
122*4882a593Smuzhiyun 		return -ENODEV;
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	return 0;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun 
channel_monitor_group(const struct vmbus_channel * channel)127*4882a593Smuzhiyun static u8 channel_monitor_group(const struct vmbus_channel *channel)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun 	return (u8)channel->offermsg.monitorid / 32;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun 
channel_monitor_offset(const struct vmbus_channel * channel)132*4882a593Smuzhiyun static u8 channel_monitor_offset(const struct vmbus_channel *channel)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	return (u8)channel->offermsg.monitorid % 32;
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun 
channel_pending(const struct vmbus_channel * channel,const struct hv_monitor_page * monitor_page)137*4882a593Smuzhiyun static u32 channel_pending(const struct vmbus_channel *channel,
138*4882a593Smuzhiyun 			   const struct hv_monitor_page *monitor_page)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun 	u8 monitor_group = channel_monitor_group(channel);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	return monitor_page->trigger_group[monitor_group].pending;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun 
channel_latency(const struct vmbus_channel * channel,const struct hv_monitor_page * monitor_page)145*4882a593Smuzhiyun static u32 channel_latency(const struct vmbus_channel *channel,
146*4882a593Smuzhiyun 			   const struct hv_monitor_page *monitor_page)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun 	u8 monitor_group = channel_monitor_group(channel);
149*4882a593Smuzhiyun 	u8 monitor_offset = channel_monitor_offset(channel);
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	return monitor_page->latency[monitor_group][monitor_offset];
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun 
channel_conn_id(struct vmbus_channel * channel,struct hv_monitor_page * monitor_page)154*4882a593Smuzhiyun static u32 channel_conn_id(struct vmbus_channel *channel,
155*4882a593Smuzhiyun 			   struct hv_monitor_page *monitor_page)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun 	u8 monitor_group = channel_monitor_group(channel);
158*4882a593Smuzhiyun 	u8 monitor_offset = channel_monitor_offset(channel);
159*4882a593Smuzhiyun 	return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun 
id_show(struct device * dev,struct device_attribute * dev_attr,char * buf)162*4882a593Smuzhiyun static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
163*4882a593Smuzhiyun 		       char *buf)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	if (!hv_dev->channel)
168*4882a593Smuzhiyun 		return -ENODEV;
169*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun static DEVICE_ATTR_RO(id);
172*4882a593Smuzhiyun 
state_show(struct device * dev,struct device_attribute * dev_attr,char * buf)173*4882a593Smuzhiyun static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
174*4882a593Smuzhiyun 			  char *buf)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	if (!hv_dev->channel)
179*4882a593Smuzhiyun 		return -ENODEV;
180*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", hv_dev->channel->state);
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun static DEVICE_ATTR_RO(state);
183*4882a593Smuzhiyun 
monitor_id_show(struct device * dev,struct device_attribute * dev_attr,char * buf)184*4882a593Smuzhiyun static ssize_t monitor_id_show(struct device *dev,
185*4882a593Smuzhiyun 			       struct device_attribute *dev_attr, char *buf)
186*4882a593Smuzhiyun {
187*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	if (!hv_dev->channel)
190*4882a593Smuzhiyun 		return -ENODEV;
191*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
192*4882a593Smuzhiyun }
193*4882a593Smuzhiyun static DEVICE_ATTR_RO(monitor_id);
194*4882a593Smuzhiyun 
class_id_show(struct device * dev,struct device_attribute * dev_attr,char * buf)195*4882a593Smuzhiyun static ssize_t class_id_show(struct device *dev,
196*4882a593Smuzhiyun 			       struct device_attribute *dev_attr, char *buf)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	if (!hv_dev->channel)
201*4882a593Smuzhiyun 		return -ENODEV;
202*4882a593Smuzhiyun 	return sprintf(buf, "{%pUl}\n",
203*4882a593Smuzhiyun 		       &hv_dev->channel->offermsg.offer.if_type);
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun static DEVICE_ATTR_RO(class_id);
206*4882a593Smuzhiyun 
device_id_show(struct device * dev,struct device_attribute * dev_attr,char * buf)207*4882a593Smuzhiyun static ssize_t device_id_show(struct device *dev,
208*4882a593Smuzhiyun 			      struct device_attribute *dev_attr, char *buf)
209*4882a593Smuzhiyun {
210*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	if (!hv_dev->channel)
213*4882a593Smuzhiyun 		return -ENODEV;
214*4882a593Smuzhiyun 	return sprintf(buf, "{%pUl}\n",
215*4882a593Smuzhiyun 		       &hv_dev->channel->offermsg.offer.if_instance);
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun static DEVICE_ATTR_RO(device_id);
218*4882a593Smuzhiyun 
modalias_show(struct device * dev,struct device_attribute * dev_attr,char * buf)219*4882a593Smuzhiyun static ssize_t modalias_show(struct device *dev,
220*4882a593Smuzhiyun 			     struct device_attribute *dev_attr, char *buf)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 	return sprintf(buf, "vmbus:%*phN\n", UUID_SIZE, &hv_dev->dev_type);
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun static DEVICE_ATTR_RO(modalias);
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun #ifdef CONFIG_NUMA
numa_node_show(struct device * dev,struct device_attribute * attr,char * buf)229*4882a593Smuzhiyun static ssize_t numa_node_show(struct device *dev,
230*4882a593Smuzhiyun 			      struct device_attribute *attr, char *buf)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	if (!hv_dev->channel)
235*4882a593Smuzhiyun 		return -ENODEV;
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", cpu_to_node(hv_dev->channel->target_cpu));
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun static DEVICE_ATTR_RO(numa_node);
240*4882a593Smuzhiyun #endif
241*4882a593Smuzhiyun 
server_monitor_pending_show(struct device * dev,struct device_attribute * dev_attr,char * buf)242*4882a593Smuzhiyun static ssize_t server_monitor_pending_show(struct device *dev,
243*4882a593Smuzhiyun 					   struct device_attribute *dev_attr,
244*4882a593Smuzhiyun 					   char *buf)
245*4882a593Smuzhiyun {
246*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	if (!hv_dev->channel)
249*4882a593Smuzhiyun 		return -ENODEV;
250*4882a593Smuzhiyun 	return sprintf(buf, "%d\n",
251*4882a593Smuzhiyun 		       channel_pending(hv_dev->channel,
252*4882a593Smuzhiyun 				       vmbus_connection.monitor_pages[0]));
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun static DEVICE_ATTR_RO(server_monitor_pending);
255*4882a593Smuzhiyun 
client_monitor_pending_show(struct device * dev,struct device_attribute * dev_attr,char * buf)256*4882a593Smuzhiyun static ssize_t client_monitor_pending_show(struct device *dev,
257*4882a593Smuzhiyun 					   struct device_attribute *dev_attr,
258*4882a593Smuzhiyun 					   char *buf)
259*4882a593Smuzhiyun {
260*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	if (!hv_dev->channel)
263*4882a593Smuzhiyun 		return -ENODEV;
264*4882a593Smuzhiyun 	return sprintf(buf, "%d\n",
265*4882a593Smuzhiyun 		       channel_pending(hv_dev->channel,
266*4882a593Smuzhiyun 				       vmbus_connection.monitor_pages[1]));
267*4882a593Smuzhiyun }
268*4882a593Smuzhiyun static DEVICE_ATTR_RO(client_monitor_pending);
269*4882a593Smuzhiyun 
server_monitor_latency_show(struct device * dev,struct device_attribute * dev_attr,char * buf)270*4882a593Smuzhiyun static ssize_t server_monitor_latency_show(struct device *dev,
271*4882a593Smuzhiyun 					   struct device_attribute *dev_attr,
272*4882a593Smuzhiyun 					   char *buf)
273*4882a593Smuzhiyun {
274*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun 	if (!hv_dev->channel)
277*4882a593Smuzhiyun 		return -ENODEV;
278*4882a593Smuzhiyun 	return sprintf(buf, "%d\n",
279*4882a593Smuzhiyun 		       channel_latency(hv_dev->channel,
280*4882a593Smuzhiyun 				       vmbus_connection.monitor_pages[0]));
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun static DEVICE_ATTR_RO(server_monitor_latency);
283*4882a593Smuzhiyun 
client_monitor_latency_show(struct device * dev,struct device_attribute * dev_attr,char * buf)284*4882a593Smuzhiyun static ssize_t client_monitor_latency_show(struct device *dev,
285*4882a593Smuzhiyun 					   struct device_attribute *dev_attr,
286*4882a593Smuzhiyun 					   char *buf)
287*4882a593Smuzhiyun {
288*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	if (!hv_dev->channel)
291*4882a593Smuzhiyun 		return -ENODEV;
292*4882a593Smuzhiyun 	return sprintf(buf, "%d\n",
293*4882a593Smuzhiyun 		       channel_latency(hv_dev->channel,
294*4882a593Smuzhiyun 				       vmbus_connection.monitor_pages[1]));
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun static DEVICE_ATTR_RO(client_monitor_latency);
297*4882a593Smuzhiyun 
server_monitor_conn_id_show(struct device * dev,struct device_attribute * dev_attr,char * buf)298*4882a593Smuzhiyun static ssize_t server_monitor_conn_id_show(struct device *dev,
299*4882a593Smuzhiyun 					   struct device_attribute *dev_attr,
300*4882a593Smuzhiyun 					   char *buf)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	if (!hv_dev->channel)
305*4882a593Smuzhiyun 		return -ENODEV;
306*4882a593Smuzhiyun 	return sprintf(buf, "%d\n",
307*4882a593Smuzhiyun 		       channel_conn_id(hv_dev->channel,
308*4882a593Smuzhiyun 				       vmbus_connection.monitor_pages[0]));
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun static DEVICE_ATTR_RO(server_monitor_conn_id);
311*4882a593Smuzhiyun 
client_monitor_conn_id_show(struct device * dev,struct device_attribute * dev_attr,char * buf)312*4882a593Smuzhiyun static ssize_t client_monitor_conn_id_show(struct device *dev,
313*4882a593Smuzhiyun 					   struct device_attribute *dev_attr,
314*4882a593Smuzhiyun 					   char *buf)
315*4882a593Smuzhiyun {
316*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	if (!hv_dev->channel)
319*4882a593Smuzhiyun 		return -ENODEV;
320*4882a593Smuzhiyun 	return sprintf(buf, "%d\n",
321*4882a593Smuzhiyun 		       channel_conn_id(hv_dev->channel,
322*4882a593Smuzhiyun 				       vmbus_connection.monitor_pages[1]));
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun static DEVICE_ATTR_RO(client_monitor_conn_id);
325*4882a593Smuzhiyun 
out_intr_mask_show(struct device * dev,struct device_attribute * dev_attr,char * buf)326*4882a593Smuzhiyun static ssize_t out_intr_mask_show(struct device *dev,
327*4882a593Smuzhiyun 				  struct device_attribute *dev_attr, char *buf)
328*4882a593Smuzhiyun {
329*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
330*4882a593Smuzhiyun 	struct hv_ring_buffer_debug_info outbound;
331*4882a593Smuzhiyun 	int ret;
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	if (!hv_dev->channel)
334*4882a593Smuzhiyun 		return -ENODEV;
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
337*4882a593Smuzhiyun 					  &outbound);
338*4882a593Smuzhiyun 	if (ret < 0)
339*4882a593Smuzhiyun 		return ret;
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun static DEVICE_ATTR_RO(out_intr_mask);
344*4882a593Smuzhiyun 
out_read_index_show(struct device * dev,struct device_attribute * dev_attr,char * buf)345*4882a593Smuzhiyun static ssize_t out_read_index_show(struct device *dev,
346*4882a593Smuzhiyun 				   struct device_attribute *dev_attr, char *buf)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
349*4882a593Smuzhiyun 	struct hv_ring_buffer_debug_info outbound;
350*4882a593Smuzhiyun 	int ret;
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	if (!hv_dev->channel)
353*4882a593Smuzhiyun 		return -ENODEV;
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
356*4882a593Smuzhiyun 					  &outbound);
357*4882a593Smuzhiyun 	if (ret < 0)
358*4882a593Smuzhiyun 		return ret;
359*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", outbound.current_read_index);
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun static DEVICE_ATTR_RO(out_read_index);
362*4882a593Smuzhiyun 
out_write_index_show(struct device * dev,struct device_attribute * dev_attr,char * buf)363*4882a593Smuzhiyun static ssize_t out_write_index_show(struct device *dev,
364*4882a593Smuzhiyun 				    struct device_attribute *dev_attr,
365*4882a593Smuzhiyun 				    char *buf)
366*4882a593Smuzhiyun {
367*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
368*4882a593Smuzhiyun 	struct hv_ring_buffer_debug_info outbound;
369*4882a593Smuzhiyun 	int ret;
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	if (!hv_dev->channel)
372*4882a593Smuzhiyun 		return -ENODEV;
373*4882a593Smuzhiyun 
374*4882a593Smuzhiyun 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
375*4882a593Smuzhiyun 					  &outbound);
376*4882a593Smuzhiyun 	if (ret < 0)
377*4882a593Smuzhiyun 		return ret;
378*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", outbound.current_write_index);
379*4882a593Smuzhiyun }
380*4882a593Smuzhiyun static DEVICE_ATTR_RO(out_write_index);
381*4882a593Smuzhiyun 
out_read_bytes_avail_show(struct device * dev,struct device_attribute * dev_attr,char * buf)382*4882a593Smuzhiyun static ssize_t out_read_bytes_avail_show(struct device *dev,
383*4882a593Smuzhiyun 					 struct device_attribute *dev_attr,
384*4882a593Smuzhiyun 					 char *buf)
385*4882a593Smuzhiyun {
386*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
387*4882a593Smuzhiyun 	struct hv_ring_buffer_debug_info outbound;
388*4882a593Smuzhiyun 	int ret;
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	if (!hv_dev->channel)
391*4882a593Smuzhiyun 		return -ENODEV;
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
394*4882a593Smuzhiyun 					  &outbound);
395*4882a593Smuzhiyun 	if (ret < 0)
396*4882a593Smuzhiyun 		return ret;
397*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun static DEVICE_ATTR_RO(out_read_bytes_avail);
400*4882a593Smuzhiyun 
out_write_bytes_avail_show(struct device * dev,struct device_attribute * dev_attr,char * buf)401*4882a593Smuzhiyun static ssize_t out_write_bytes_avail_show(struct device *dev,
402*4882a593Smuzhiyun 					  struct device_attribute *dev_attr,
403*4882a593Smuzhiyun 					  char *buf)
404*4882a593Smuzhiyun {
405*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
406*4882a593Smuzhiyun 	struct hv_ring_buffer_debug_info outbound;
407*4882a593Smuzhiyun 	int ret;
408*4882a593Smuzhiyun 
409*4882a593Smuzhiyun 	if (!hv_dev->channel)
410*4882a593Smuzhiyun 		return -ENODEV;
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
413*4882a593Smuzhiyun 					  &outbound);
414*4882a593Smuzhiyun 	if (ret < 0)
415*4882a593Smuzhiyun 		return ret;
416*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun static DEVICE_ATTR_RO(out_write_bytes_avail);
419*4882a593Smuzhiyun 
in_intr_mask_show(struct device * dev,struct device_attribute * dev_attr,char * buf)420*4882a593Smuzhiyun static ssize_t in_intr_mask_show(struct device *dev,
421*4882a593Smuzhiyun 				 struct device_attribute *dev_attr, char *buf)
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
424*4882a593Smuzhiyun 	struct hv_ring_buffer_debug_info inbound;
425*4882a593Smuzhiyun 	int ret;
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	if (!hv_dev->channel)
428*4882a593Smuzhiyun 		return -ENODEV;
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
431*4882a593Smuzhiyun 	if (ret < 0)
432*4882a593Smuzhiyun 		return ret;
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun static DEVICE_ATTR_RO(in_intr_mask);
437*4882a593Smuzhiyun 
in_read_index_show(struct device * dev,struct device_attribute * dev_attr,char * buf)438*4882a593Smuzhiyun static ssize_t in_read_index_show(struct device *dev,
439*4882a593Smuzhiyun 				  struct device_attribute *dev_attr, char *buf)
440*4882a593Smuzhiyun {
441*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
442*4882a593Smuzhiyun 	struct hv_ring_buffer_debug_info inbound;
443*4882a593Smuzhiyun 	int ret;
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	if (!hv_dev->channel)
446*4882a593Smuzhiyun 		return -ENODEV;
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
449*4882a593Smuzhiyun 	if (ret < 0)
450*4882a593Smuzhiyun 		return ret;
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", inbound.current_read_index);
453*4882a593Smuzhiyun }
454*4882a593Smuzhiyun static DEVICE_ATTR_RO(in_read_index);
455*4882a593Smuzhiyun 
in_write_index_show(struct device * dev,struct device_attribute * dev_attr,char * buf)456*4882a593Smuzhiyun static ssize_t in_write_index_show(struct device *dev,
457*4882a593Smuzhiyun 				   struct device_attribute *dev_attr, char *buf)
458*4882a593Smuzhiyun {
459*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
460*4882a593Smuzhiyun 	struct hv_ring_buffer_debug_info inbound;
461*4882a593Smuzhiyun 	int ret;
462*4882a593Smuzhiyun 
463*4882a593Smuzhiyun 	if (!hv_dev->channel)
464*4882a593Smuzhiyun 		return -ENODEV;
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
467*4882a593Smuzhiyun 	if (ret < 0)
468*4882a593Smuzhiyun 		return ret;
469*4882a593Smuzhiyun 
470*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", inbound.current_write_index);
471*4882a593Smuzhiyun }
472*4882a593Smuzhiyun static DEVICE_ATTR_RO(in_write_index);
473*4882a593Smuzhiyun 
in_read_bytes_avail_show(struct device * dev,struct device_attribute * dev_attr,char * buf)474*4882a593Smuzhiyun static ssize_t in_read_bytes_avail_show(struct device *dev,
475*4882a593Smuzhiyun 					struct device_attribute *dev_attr,
476*4882a593Smuzhiyun 					char *buf)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
479*4882a593Smuzhiyun 	struct hv_ring_buffer_debug_info inbound;
480*4882a593Smuzhiyun 	int ret;
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	if (!hv_dev->channel)
483*4882a593Smuzhiyun 		return -ENODEV;
484*4882a593Smuzhiyun 
485*4882a593Smuzhiyun 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
486*4882a593Smuzhiyun 	if (ret < 0)
487*4882a593Smuzhiyun 		return ret;
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun static DEVICE_ATTR_RO(in_read_bytes_avail);
492*4882a593Smuzhiyun 
in_write_bytes_avail_show(struct device * dev,struct device_attribute * dev_attr,char * buf)493*4882a593Smuzhiyun static ssize_t in_write_bytes_avail_show(struct device *dev,
494*4882a593Smuzhiyun 					 struct device_attribute *dev_attr,
495*4882a593Smuzhiyun 					 char *buf)
496*4882a593Smuzhiyun {
497*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
498*4882a593Smuzhiyun 	struct hv_ring_buffer_debug_info inbound;
499*4882a593Smuzhiyun 	int ret;
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 	if (!hv_dev->channel)
502*4882a593Smuzhiyun 		return -ENODEV;
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 	ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
505*4882a593Smuzhiyun 	if (ret < 0)
506*4882a593Smuzhiyun 		return ret;
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun static DEVICE_ATTR_RO(in_write_bytes_avail);
511*4882a593Smuzhiyun 
channel_vp_mapping_show(struct device * dev,struct device_attribute * dev_attr,char * buf)512*4882a593Smuzhiyun static ssize_t channel_vp_mapping_show(struct device *dev,
513*4882a593Smuzhiyun 				       struct device_attribute *dev_attr,
514*4882a593Smuzhiyun 				       char *buf)
515*4882a593Smuzhiyun {
516*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
517*4882a593Smuzhiyun 	struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
518*4882a593Smuzhiyun 	int buf_size = PAGE_SIZE, n_written, tot_written;
519*4882a593Smuzhiyun 	struct list_head *cur;
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	if (!channel)
522*4882a593Smuzhiyun 		return -ENODEV;
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	mutex_lock(&vmbus_connection.channel_mutex);
525*4882a593Smuzhiyun 
526*4882a593Smuzhiyun 	tot_written = snprintf(buf, buf_size, "%u:%u\n",
527*4882a593Smuzhiyun 		channel->offermsg.child_relid, channel->target_cpu);
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun 	list_for_each(cur, &channel->sc_list) {
530*4882a593Smuzhiyun 		if (tot_written >= buf_size - 1)
531*4882a593Smuzhiyun 			break;
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 		cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
534*4882a593Smuzhiyun 		n_written = scnprintf(buf + tot_written,
535*4882a593Smuzhiyun 				     buf_size - tot_written,
536*4882a593Smuzhiyun 				     "%u:%u\n",
537*4882a593Smuzhiyun 				     cur_sc->offermsg.child_relid,
538*4882a593Smuzhiyun 				     cur_sc->target_cpu);
539*4882a593Smuzhiyun 		tot_written += n_written;
540*4882a593Smuzhiyun 	}
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 	mutex_unlock(&vmbus_connection.channel_mutex);
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun 	return tot_written;
545*4882a593Smuzhiyun }
546*4882a593Smuzhiyun static DEVICE_ATTR_RO(channel_vp_mapping);
547*4882a593Smuzhiyun 
vendor_show(struct device * dev,struct device_attribute * dev_attr,char * buf)548*4882a593Smuzhiyun static ssize_t vendor_show(struct device *dev,
549*4882a593Smuzhiyun 			   struct device_attribute *dev_attr,
550*4882a593Smuzhiyun 			   char *buf)
551*4882a593Smuzhiyun {
552*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
553*4882a593Smuzhiyun 	return sprintf(buf, "0x%x\n", hv_dev->vendor_id);
554*4882a593Smuzhiyun }
555*4882a593Smuzhiyun static DEVICE_ATTR_RO(vendor);
556*4882a593Smuzhiyun 
device_show(struct device * dev,struct device_attribute * dev_attr,char * buf)557*4882a593Smuzhiyun static ssize_t device_show(struct device *dev,
558*4882a593Smuzhiyun 			   struct device_attribute *dev_attr,
559*4882a593Smuzhiyun 			   char *buf)
560*4882a593Smuzhiyun {
561*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
562*4882a593Smuzhiyun 	return sprintf(buf, "0x%x\n", hv_dev->device_id);
563*4882a593Smuzhiyun }
564*4882a593Smuzhiyun static DEVICE_ATTR_RO(device);
565*4882a593Smuzhiyun 
driver_override_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)566*4882a593Smuzhiyun static ssize_t driver_override_store(struct device *dev,
567*4882a593Smuzhiyun 				     struct device_attribute *attr,
568*4882a593Smuzhiyun 				     const char *buf, size_t count)
569*4882a593Smuzhiyun {
570*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
571*4882a593Smuzhiyun 	char *driver_override, *old, *cp;
572*4882a593Smuzhiyun 
573*4882a593Smuzhiyun 	/* We need to keep extra room for a newline */
574*4882a593Smuzhiyun 	if (count >= (PAGE_SIZE - 1))
575*4882a593Smuzhiyun 		return -EINVAL;
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun 	driver_override = kstrndup(buf, count, GFP_KERNEL);
578*4882a593Smuzhiyun 	if (!driver_override)
579*4882a593Smuzhiyun 		return -ENOMEM;
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun 	cp = strchr(driver_override, '\n');
582*4882a593Smuzhiyun 	if (cp)
583*4882a593Smuzhiyun 		*cp = '\0';
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 	device_lock(dev);
586*4882a593Smuzhiyun 	old = hv_dev->driver_override;
587*4882a593Smuzhiyun 	if (strlen(driver_override)) {
588*4882a593Smuzhiyun 		hv_dev->driver_override = driver_override;
589*4882a593Smuzhiyun 	} else {
590*4882a593Smuzhiyun 		kfree(driver_override);
591*4882a593Smuzhiyun 		hv_dev->driver_override = NULL;
592*4882a593Smuzhiyun 	}
593*4882a593Smuzhiyun 	device_unlock(dev);
594*4882a593Smuzhiyun 
595*4882a593Smuzhiyun 	kfree(old);
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun 	return count;
598*4882a593Smuzhiyun }
599*4882a593Smuzhiyun 
driver_override_show(struct device * dev,struct device_attribute * attr,char * buf)600*4882a593Smuzhiyun static ssize_t driver_override_show(struct device *dev,
601*4882a593Smuzhiyun 				    struct device_attribute *attr, char *buf)
602*4882a593Smuzhiyun {
603*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(dev);
604*4882a593Smuzhiyun 	ssize_t len;
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 	device_lock(dev);
607*4882a593Smuzhiyun 	len = snprintf(buf, PAGE_SIZE, "%s\n", hv_dev->driver_override);
608*4882a593Smuzhiyun 	device_unlock(dev);
609*4882a593Smuzhiyun 
610*4882a593Smuzhiyun 	return len;
611*4882a593Smuzhiyun }
612*4882a593Smuzhiyun static DEVICE_ATTR_RW(driver_override);
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
615*4882a593Smuzhiyun static struct attribute *vmbus_dev_attrs[] = {
616*4882a593Smuzhiyun 	&dev_attr_id.attr,
617*4882a593Smuzhiyun 	&dev_attr_state.attr,
618*4882a593Smuzhiyun 	&dev_attr_monitor_id.attr,
619*4882a593Smuzhiyun 	&dev_attr_class_id.attr,
620*4882a593Smuzhiyun 	&dev_attr_device_id.attr,
621*4882a593Smuzhiyun 	&dev_attr_modalias.attr,
622*4882a593Smuzhiyun #ifdef CONFIG_NUMA
623*4882a593Smuzhiyun 	&dev_attr_numa_node.attr,
624*4882a593Smuzhiyun #endif
625*4882a593Smuzhiyun 	&dev_attr_server_monitor_pending.attr,
626*4882a593Smuzhiyun 	&dev_attr_client_monitor_pending.attr,
627*4882a593Smuzhiyun 	&dev_attr_server_monitor_latency.attr,
628*4882a593Smuzhiyun 	&dev_attr_client_monitor_latency.attr,
629*4882a593Smuzhiyun 	&dev_attr_server_monitor_conn_id.attr,
630*4882a593Smuzhiyun 	&dev_attr_client_monitor_conn_id.attr,
631*4882a593Smuzhiyun 	&dev_attr_out_intr_mask.attr,
632*4882a593Smuzhiyun 	&dev_attr_out_read_index.attr,
633*4882a593Smuzhiyun 	&dev_attr_out_write_index.attr,
634*4882a593Smuzhiyun 	&dev_attr_out_read_bytes_avail.attr,
635*4882a593Smuzhiyun 	&dev_attr_out_write_bytes_avail.attr,
636*4882a593Smuzhiyun 	&dev_attr_in_intr_mask.attr,
637*4882a593Smuzhiyun 	&dev_attr_in_read_index.attr,
638*4882a593Smuzhiyun 	&dev_attr_in_write_index.attr,
639*4882a593Smuzhiyun 	&dev_attr_in_read_bytes_avail.attr,
640*4882a593Smuzhiyun 	&dev_attr_in_write_bytes_avail.attr,
641*4882a593Smuzhiyun 	&dev_attr_channel_vp_mapping.attr,
642*4882a593Smuzhiyun 	&dev_attr_vendor.attr,
643*4882a593Smuzhiyun 	&dev_attr_device.attr,
644*4882a593Smuzhiyun 	&dev_attr_driver_override.attr,
645*4882a593Smuzhiyun 	NULL,
646*4882a593Smuzhiyun };
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun /*
649*4882a593Smuzhiyun  * Device-level attribute_group callback function. Returns the permission for
650*4882a593Smuzhiyun  * each attribute, and returns 0 if an attribute is not visible.
651*4882a593Smuzhiyun  */
vmbus_dev_attr_is_visible(struct kobject * kobj,struct attribute * attr,int idx)652*4882a593Smuzhiyun static umode_t vmbus_dev_attr_is_visible(struct kobject *kobj,
653*4882a593Smuzhiyun 					 struct attribute *attr, int idx)
654*4882a593Smuzhiyun {
655*4882a593Smuzhiyun 	struct device *dev = kobj_to_dev(kobj);
656*4882a593Smuzhiyun 	const struct hv_device *hv_dev = device_to_hv_device(dev);
657*4882a593Smuzhiyun 
658*4882a593Smuzhiyun 	/* Hide the monitor attributes if the monitor mechanism is not used. */
659*4882a593Smuzhiyun 	if (!hv_dev->channel->offermsg.monitor_allocated &&
660*4882a593Smuzhiyun 	    (attr == &dev_attr_monitor_id.attr ||
661*4882a593Smuzhiyun 	     attr == &dev_attr_server_monitor_pending.attr ||
662*4882a593Smuzhiyun 	     attr == &dev_attr_client_monitor_pending.attr ||
663*4882a593Smuzhiyun 	     attr == &dev_attr_server_monitor_latency.attr ||
664*4882a593Smuzhiyun 	     attr == &dev_attr_client_monitor_latency.attr ||
665*4882a593Smuzhiyun 	     attr == &dev_attr_server_monitor_conn_id.attr ||
666*4882a593Smuzhiyun 	     attr == &dev_attr_client_monitor_conn_id.attr))
667*4882a593Smuzhiyun 		return 0;
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	return attr->mode;
670*4882a593Smuzhiyun }
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun static const struct attribute_group vmbus_dev_group = {
673*4882a593Smuzhiyun 	.attrs = vmbus_dev_attrs,
674*4882a593Smuzhiyun 	.is_visible = vmbus_dev_attr_is_visible
675*4882a593Smuzhiyun };
676*4882a593Smuzhiyun __ATTRIBUTE_GROUPS(vmbus_dev);
677*4882a593Smuzhiyun 
678*4882a593Smuzhiyun /*
679*4882a593Smuzhiyun  * vmbus_uevent - add uevent for our device
680*4882a593Smuzhiyun  *
681*4882a593Smuzhiyun  * This routine is invoked when a device is added or removed on the vmbus to
682*4882a593Smuzhiyun  * generate a uevent to udev in the userspace. The udev will then look at its
683*4882a593Smuzhiyun  * rule and the uevent generated here to load the appropriate driver
684*4882a593Smuzhiyun  *
685*4882a593Smuzhiyun  * The alias string will be of the form vmbus:guid where guid is the string
686*4882a593Smuzhiyun  * representation of the device guid (each byte of the guid will be
687*4882a593Smuzhiyun  * represented with two hex characters.
688*4882a593Smuzhiyun  */
vmbus_uevent(struct device * device,struct kobj_uevent_env * env)689*4882a593Smuzhiyun static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
690*4882a593Smuzhiyun {
691*4882a593Smuzhiyun 	struct hv_device *dev = device_to_hv_device(device);
692*4882a593Smuzhiyun 	const char *format = "MODALIAS=vmbus:%*phN";
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun 	return add_uevent_var(env, format, UUID_SIZE, &dev->dev_type);
695*4882a593Smuzhiyun }
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun static const struct hv_vmbus_device_id *
hv_vmbus_dev_match(const struct hv_vmbus_device_id * id,const guid_t * guid)698*4882a593Smuzhiyun hv_vmbus_dev_match(const struct hv_vmbus_device_id *id, const guid_t *guid)
699*4882a593Smuzhiyun {
700*4882a593Smuzhiyun 	if (id == NULL)
701*4882a593Smuzhiyun 		return NULL; /* empty device table */
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 	for (; !guid_is_null(&id->guid); id++)
704*4882a593Smuzhiyun 		if (guid_equal(&id->guid, guid))
705*4882a593Smuzhiyun 			return id;
706*4882a593Smuzhiyun 
707*4882a593Smuzhiyun 	return NULL;
708*4882a593Smuzhiyun }
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun static const struct hv_vmbus_device_id *
hv_vmbus_dynid_match(struct hv_driver * drv,const guid_t * guid)711*4882a593Smuzhiyun hv_vmbus_dynid_match(struct hv_driver *drv, const guid_t *guid)
712*4882a593Smuzhiyun {
713*4882a593Smuzhiyun 	const struct hv_vmbus_device_id *id = NULL;
714*4882a593Smuzhiyun 	struct vmbus_dynid *dynid;
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	spin_lock(&drv->dynids.lock);
717*4882a593Smuzhiyun 	list_for_each_entry(dynid, &drv->dynids.list, node) {
718*4882a593Smuzhiyun 		if (guid_equal(&dynid->id.guid, guid)) {
719*4882a593Smuzhiyun 			id = &dynid->id;
720*4882a593Smuzhiyun 			break;
721*4882a593Smuzhiyun 		}
722*4882a593Smuzhiyun 	}
723*4882a593Smuzhiyun 	spin_unlock(&drv->dynids.lock);
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun 	return id;
726*4882a593Smuzhiyun }
727*4882a593Smuzhiyun 
728*4882a593Smuzhiyun static const struct hv_vmbus_device_id vmbus_device_null;
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun /*
731*4882a593Smuzhiyun  * Return a matching hv_vmbus_device_id pointer.
732*4882a593Smuzhiyun  * If there is no match, return NULL.
733*4882a593Smuzhiyun  */
hv_vmbus_get_id(struct hv_driver * drv,struct hv_device * dev)734*4882a593Smuzhiyun static const struct hv_vmbus_device_id *hv_vmbus_get_id(struct hv_driver *drv,
735*4882a593Smuzhiyun 							struct hv_device *dev)
736*4882a593Smuzhiyun {
737*4882a593Smuzhiyun 	const guid_t *guid = &dev->dev_type;
738*4882a593Smuzhiyun 	const struct hv_vmbus_device_id *id;
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun 	/* When driver_override is set, only bind to the matching driver */
741*4882a593Smuzhiyun 	if (dev->driver_override && strcmp(dev->driver_override, drv->name))
742*4882a593Smuzhiyun 		return NULL;
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun 	/* Look at the dynamic ids first, before the static ones */
745*4882a593Smuzhiyun 	id = hv_vmbus_dynid_match(drv, guid);
746*4882a593Smuzhiyun 	if (!id)
747*4882a593Smuzhiyun 		id = hv_vmbus_dev_match(drv->id_table, guid);
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun 	/* driver_override will always match, send a dummy id */
750*4882a593Smuzhiyun 	if (!id && dev->driver_override)
751*4882a593Smuzhiyun 		id = &vmbus_device_null;
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 	return id;
754*4882a593Smuzhiyun }
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun /* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
vmbus_add_dynid(struct hv_driver * drv,guid_t * guid)757*4882a593Smuzhiyun static int vmbus_add_dynid(struct hv_driver *drv, guid_t *guid)
758*4882a593Smuzhiyun {
759*4882a593Smuzhiyun 	struct vmbus_dynid *dynid;
760*4882a593Smuzhiyun 
761*4882a593Smuzhiyun 	dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
762*4882a593Smuzhiyun 	if (!dynid)
763*4882a593Smuzhiyun 		return -ENOMEM;
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	dynid->id.guid = *guid;
766*4882a593Smuzhiyun 
767*4882a593Smuzhiyun 	spin_lock(&drv->dynids.lock);
768*4882a593Smuzhiyun 	list_add_tail(&dynid->node, &drv->dynids.list);
769*4882a593Smuzhiyun 	spin_unlock(&drv->dynids.lock);
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	return driver_attach(&drv->driver);
772*4882a593Smuzhiyun }
773*4882a593Smuzhiyun 
vmbus_free_dynids(struct hv_driver * drv)774*4882a593Smuzhiyun static void vmbus_free_dynids(struct hv_driver *drv)
775*4882a593Smuzhiyun {
776*4882a593Smuzhiyun 	struct vmbus_dynid *dynid, *n;
777*4882a593Smuzhiyun 
778*4882a593Smuzhiyun 	spin_lock(&drv->dynids.lock);
779*4882a593Smuzhiyun 	list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
780*4882a593Smuzhiyun 		list_del(&dynid->node);
781*4882a593Smuzhiyun 		kfree(dynid);
782*4882a593Smuzhiyun 	}
783*4882a593Smuzhiyun 	spin_unlock(&drv->dynids.lock);
784*4882a593Smuzhiyun }
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun /*
787*4882a593Smuzhiyun  * store_new_id - sysfs frontend to vmbus_add_dynid()
788*4882a593Smuzhiyun  *
789*4882a593Smuzhiyun  * Allow GUIDs to be added to an existing driver via sysfs.
790*4882a593Smuzhiyun  */
new_id_store(struct device_driver * driver,const char * buf,size_t count)791*4882a593Smuzhiyun static ssize_t new_id_store(struct device_driver *driver, const char *buf,
792*4882a593Smuzhiyun 			    size_t count)
793*4882a593Smuzhiyun {
794*4882a593Smuzhiyun 	struct hv_driver *drv = drv_to_hv_drv(driver);
795*4882a593Smuzhiyun 	guid_t guid;
796*4882a593Smuzhiyun 	ssize_t retval;
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun 	retval = guid_parse(buf, &guid);
799*4882a593Smuzhiyun 	if (retval)
800*4882a593Smuzhiyun 		return retval;
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	if (hv_vmbus_dynid_match(drv, &guid))
803*4882a593Smuzhiyun 		return -EEXIST;
804*4882a593Smuzhiyun 
805*4882a593Smuzhiyun 	retval = vmbus_add_dynid(drv, &guid);
806*4882a593Smuzhiyun 	if (retval)
807*4882a593Smuzhiyun 		return retval;
808*4882a593Smuzhiyun 	return count;
809*4882a593Smuzhiyun }
810*4882a593Smuzhiyun static DRIVER_ATTR_WO(new_id);
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun /*
813*4882a593Smuzhiyun  * store_remove_id - remove a PCI device ID from this driver
814*4882a593Smuzhiyun  *
815*4882a593Smuzhiyun  * Removes a dynamic pci device ID to this driver.
816*4882a593Smuzhiyun  */
remove_id_store(struct device_driver * driver,const char * buf,size_t count)817*4882a593Smuzhiyun static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
818*4882a593Smuzhiyun 			       size_t count)
819*4882a593Smuzhiyun {
820*4882a593Smuzhiyun 	struct hv_driver *drv = drv_to_hv_drv(driver);
821*4882a593Smuzhiyun 	struct vmbus_dynid *dynid, *n;
822*4882a593Smuzhiyun 	guid_t guid;
823*4882a593Smuzhiyun 	ssize_t retval;
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 	retval = guid_parse(buf, &guid);
826*4882a593Smuzhiyun 	if (retval)
827*4882a593Smuzhiyun 		return retval;
828*4882a593Smuzhiyun 
829*4882a593Smuzhiyun 	retval = -ENODEV;
830*4882a593Smuzhiyun 	spin_lock(&drv->dynids.lock);
831*4882a593Smuzhiyun 	list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
832*4882a593Smuzhiyun 		struct hv_vmbus_device_id *id = &dynid->id;
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 		if (guid_equal(&id->guid, &guid)) {
835*4882a593Smuzhiyun 			list_del(&dynid->node);
836*4882a593Smuzhiyun 			kfree(dynid);
837*4882a593Smuzhiyun 			retval = count;
838*4882a593Smuzhiyun 			break;
839*4882a593Smuzhiyun 		}
840*4882a593Smuzhiyun 	}
841*4882a593Smuzhiyun 	spin_unlock(&drv->dynids.lock);
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun 	return retval;
844*4882a593Smuzhiyun }
845*4882a593Smuzhiyun static DRIVER_ATTR_WO(remove_id);
846*4882a593Smuzhiyun 
847*4882a593Smuzhiyun static struct attribute *vmbus_drv_attrs[] = {
848*4882a593Smuzhiyun 	&driver_attr_new_id.attr,
849*4882a593Smuzhiyun 	&driver_attr_remove_id.attr,
850*4882a593Smuzhiyun 	NULL,
851*4882a593Smuzhiyun };
852*4882a593Smuzhiyun ATTRIBUTE_GROUPS(vmbus_drv);
853*4882a593Smuzhiyun 
854*4882a593Smuzhiyun 
855*4882a593Smuzhiyun /*
856*4882a593Smuzhiyun  * vmbus_match - Attempt to match the specified device to the specified driver
857*4882a593Smuzhiyun  */
vmbus_match(struct device * device,struct device_driver * driver)858*4882a593Smuzhiyun static int vmbus_match(struct device *device, struct device_driver *driver)
859*4882a593Smuzhiyun {
860*4882a593Smuzhiyun 	struct hv_driver *drv = drv_to_hv_drv(driver);
861*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(device);
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun 	/* The hv_sock driver handles all hv_sock offers. */
864*4882a593Smuzhiyun 	if (is_hvsock_channel(hv_dev->channel))
865*4882a593Smuzhiyun 		return drv->hvsock;
866*4882a593Smuzhiyun 
867*4882a593Smuzhiyun 	if (hv_vmbus_get_id(drv, hv_dev))
868*4882a593Smuzhiyun 		return 1;
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun 	return 0;
871*4882a593Smuzhiyun }
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun /*
874*4882a593Smuzhiyun  * vmbus_probe - Add the new vmbus's child device
875*4882a593Smuzhiyun  */
vmbus_probe(struct device * child_device)876*4882a593Smuzhiyun static int vmbus_probe(struct device *child_device)
877*4882a593Smuzhiyun {
878*4882a593Smuzhiyun 	int ret = 0;
879*4882a593Smuzhiyun 	struct hv_driver *drv =
880*4882a593Smuzhiyun 			drv_to_hv_drv(child_device->driver);
881*4882a593Smuzhiyun 	struct hv_device *dev = device_to_hv_device(child_device);
882*4882a593Smuzhiyun 	const struct hv_vmbus_device_id *dev_id;
883*4882a593Smuzhiyun 
884*4882a593Smuzhiyun 	dev_id = hv_vmbus_get_id(drv, dev);
885*4882a593Smuzhiyun 	if (drv->probe) {
886*4882a593Smuzhiyun 		ret = drv->probe(dev, dev_id);
887*4882a593Smuzhiyun 		if (ret != 0)
888*4882a593Smuzhiyun 			pr_err("probe failed for device %s (%d)\n",
889*4882a593Smuzhiyun 			       dev_name(child_device), ret);
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun 	} else {
892*4882a593Smuzhiyun 		pr_err("probe not set for driver %s\n",
893*4882a593Smuzhiyun 		       dev_name(child_device));
894*4882a593Smuzhiyun 		ret = -ENODEV;
895*4882a593Smuzhiyun 	}
896*4882a593Smuzhiyun 	return ret;
897*4882a593Smuzhiyun }
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun /*
900*4882a593Smuzhiyun  * vmbus_remove - Remove a vmbus device
901*4882a593Smuzhiyun  */
vmbus_remove(struct device * child_device)902*4882a593Smuzhiyun static int vmbus_remove(struct device *child_device)
903*4882a593Smuzhiyun {
904*4882a593Smuzhiyun 	struct hv_driver *drv;
905*4882a593Smuzhiyun 	struct hv_device *dev = device_to_hv_device(child_device);
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 	if (child_device->driver) {
908*4882a593Smuzhiyun 		drv = drv_to_hv_drv(child_device->driver);
909*4882a593Smuzhiyun 		if (drv->remove)
910*4882a593Smuzhiyun 			drv->remove(dev);
911*4882a593Smuzhiyun 	}
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun 	return 0;
914*4882a593Smuzhiyun }
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun /*
918*4882a593Smuzhiyun  * vmbus_shutdown - Shutdown a vmbus device
919*4882a593Smuzhiyun  */
vmbus_shutdown(struct device * child_device)920*4882a593Smuzhiyun static void vmbus_shutdown(struct device *child_device)
921*4882a593Smuzhiyun {
922*4882a593Smuzhiyun 	struct hv_driver *drv;
923*4882a593Smuzhiyun 	struct hv_device *dev = device_to_hv_device(child_device);
924*4882a593Smuzhiyun 
925*4882a593Smuzhiyun 
926*4882a593Smuzhiyun 	/* The device may not be attached yet */
927*4882a593Smuzhiyun 	if (!child_device->driver)
928*4882a593Smuzhiyun 		return;
929*4882a593Smuzhiyun 
930*4882a593Smuzhiyun 	drv = drv_to_hv_drv(child_device->driver);
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun 	if (drv->shutdown)
933*4882a593Smuzhiyun 		drv->shutdown(dev);
934*4882a593Smuzhiyun }
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
937*4882a593Smuzhiyun /*
938*4882a593Smuzhiyun  * vmbus_suspend - Suspend a vmbus device
939*4882a593Smuzhiyun  */
vmbus_suspend(struct device * child_device)940*4882a593Smuzhiyun static int vmbus_suspend(struct device *child_device)
941*4882a593Smuzhiyun {
942*4882a593Smuzhiyun 	struct hv_driver *drv;
943*4882a593Smuzhiyun 	struct hv_device *dev = device_to_hv_device(child_device);
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun 	/* The device may not be attached yet */
946*4882a593Smuzhiyun 	if (!child_device->driver)
947*4882a593Smuzhiyun 		return 0;
948*4882a593Smuzhiyun 
949*4882a593Smuzhiyun 	drv = drv_to_hv_drv(child_device->driver);
950*4882a593Smuzhiyun 	if (!drv->suspend)
951*4882a593Smuzhiyun 		return -EOPNOTSUPP;
952*4882a593Smuzhiyun 
953*4882a593Smuzhiyun 	return drv->suspend(dev);
954*4882a593Smuzhiyun }
955*4882a593Smuzhiyun 
956*4882a593Smuzhiyun /*
957*4882a593Smuzhiyun  * vmbus_resume - Resume a vmbus device
958*4882a593Smuzhiyun  */
vmbus_resume(struct device * child_device)959*4882a593Smuzhiyun static int vmbus_resume(struct device *child_device)
960*4882a593Smuzhiyun {
961*4882a593Smuzhiyun 	struct hv_driver *drv;
962*4882a593Smuzhiyun 	struct hv_device *dev = device_to_hv_device(child_device);
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun 	/* The device may not be attached yet */
965*4882a593Smuzhiyun 	if (!child_device->driver)
966*4882a593Smuzhiyun 		return 0;
967*4882a593Smuzhiyun 
968*4882a593Smuzhiyun 	drv = drv_to_hv_drv(child_device->driver);
969*4882a593Smuzhiyun 	if (!drv->resume)
970*4882a593Smuzhiyun 		return -EOPNOTSUPP;
971*4882a593Smuzhiyun 
972*4882a593Smuzhiyun 	return drv->resume(dev);
973*4882a593Smuzhiyun }
974*4882a593Smuzhiyun #else
975*4882a593Smuzhiyun #define vmbus_suspend NULL
976*4882a593Smuzhiyun #define vmbus_resume NULL
977*4882a593Smuzhiyun #endif /* CONFIG_PM_SLEEP */
978*4882a593Smuzhiyun 
979*4882a593Smuzhiyun /*
980*4882a593Smuzhiyun  * vmbus_device_release - Final callback release of the vmbus child device
981*4882a593Smuzhiyun  */
vmbus_device_release(struct device * device)982*4882a593Smuzhiyun static void vmbus_device_release(struct device *device)
983*4882a593Smuzhiyun {
984*4882a593Smuzhiyun 	struct hv_device *hv_dev = device_to_hv_device(device);
985*4882a593Smuzhiyun 	struct vmbus_channel *channel = hv_dev->channel;
986*4882a593Smuzhiyun 
987*4882a593Smuzhiyun 	hv_debug_rm_dev_dir(hv_dev);
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun 	mutex_lock(&vmbus_connection.channel_mutex);
990*4882a593Smuzhiyun 	hv_process_channel_removal(channel);
991*4882a593Smuzhiyun 	mutex_unlock(&vmbus_connection.channel_mutex);
992*4882a593Smuzhiyun 	kfree(hv_dev);
993*4882a593Smuzhiyun }
994*4882a593Smuzhiyun 
995*4882a593Smuzhiyun /*
996*4882a593Smuzhiyun  * Note: we must use the "noirq" ops: see the comment before vmbus_bus_pm.
997*4882a593Smuzhiyun  *
998*4882a593Smuzhiyun  * suspend_noirq/resume_noirq are set to NULL to support Suspend-to-Idle: we
999*4882a593Smuzhiyun  * shouldn't suspend the vmbus devices upon Suspend-to-Idle, otherwise there
1000*4882a593Smuzhiyun  * is no way to wake up a Generation-2 VM.
1001*4882a593Smuzhiyun  *
1002*4882a593Smuzhiyun  * The other 4 ops are for hibernation.
1003*4882a593Smuzhiyun  */
1004*4882a593Smuzhiyun 
1005*4882a593Smuzhiyun static const struct dev_pm_ops vmbus_pm = {
1006*4882a593Smuzhiyun 	.suspend_noirq	= NULL,
1007*4882a593Smuzhiyun 	.resume_noirq	= NULL,
1008*4882a593Smuzhiyun 	.freeze_noirq	= vmbus_suspend,
1009*4882a593Smuzhiyun 	.thaw_noirq	= vmbus_resume,
1010*4882a593Smuzhiyun 	.poweroff_noirq	= vmbus_suspend,
1011*4882a593Smuzhiyun 	.restore_noirq	= vmbus_resume,
1012*4882a593Smuzhiyun };
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun /* The one and only one */
1015*4882a593Smuzhiyun static struct bus_type  hv_bus = {
1016*4882a593Smuzhiyun 	.name =		"vmbus",
1017*4882a593Smuzhiyun 	.match =		vmbus_match,
1018*4882a593Smuzhiyun 	.shutdown =		vmbus_shutdown,
1019*4882a593Smuzhiyun 	.remove =		vmbus_remove,
1020*4882a593Smuzhiyun 	.probe =		vmbus_probe,
1021*4882a593Smuzhiyun 	.uevent =		vmbus_uevent,
1022*4882a593Smuzhiyun 	.dev_groups =		vmbus_dev_groups,
1023*4882a593Smuzhiyun 	.drv_groups =		vmbus_drv_groups,
1024*4882a593Smuzhiyun 	.pm =			&vmbus_pm,
1025*4882a593Smuzhiyun };
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun struct onmessage_work_context {
1028*4882a593Smuzhiyun 	struct work_struct work;
1029*4882a593Smuzhiyun 	struct {
1030*4882a593Smuzhiyun 		struct hv_message_header header;
1031*4882a593Smuzhiyun 		u8 payload[];
1032*4882a593Smuzhiyun 	} msg;
1033*4882a593Smuzhiyun };
1034*4882a593Smuzhiyun 
vmbus_onmessage_work(struct work_struct * work)1035*4882a593Smuzhiyun static void vmbus_onmessage_work(struct work_struct *work)
1036*4882a593Smuzhiyun {
1037*4882a593Smuzhiyun 	struct onmessage_work_context *ctx;
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun 	/* Do not process messages if we're in DISCONNECTED state */
1040*4882a593Smuzhiyun 	if (vmbus_connection.conn_state == DISCONNECTED)
1041*4882a593Smuzhiyun 		return;
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun 	ctx = container_of(work, struct onmessage_work_context,
1044*4882a593Smuzhiyun 			   work);
1045*4882a593Smuzhiyun 	vmbus_onmessage((struct vmbus_channel_message_header *)
1046*4882a593Smuzhiyun 			&ctx->msg.payload);
1047*4882a593Smuzhiyun 	kfree(ctx);
1048*4882a593Smuzhiyun }
1049*4882a593Smuzhiyun 
vmbus_on_msg_dpc(unsigned long data)1050*4882a593Smuzhiyun void vmbus_on_msg_dpc(unsigned long data)
1051*4882a593Smuzhiyun {
1052*4882a593Smuzhiyun 	struct hv_per_cpu_context *hv_cpu = (void *)data;
1053*4882a593Smuzhiyun 	void *page_addr = hv_cpu->synic_message_page;
1054*4882a593Smuzhiyun 	struct hv_message *msg = (struct hv_message *)page_addr +
1055*4882a593Smuzhiyun 				  VMBUS_MESSAGE_SINT;
1056*4882a593Smuzhiyun 	struct vmbus_channel_message_header *hdr;
1057*4882a593Smuzhiyun 	const struct vmbus_channel_message_table_entry *entry;
1058*4882a593Smuzhiyun 	struct onmessage_work_context *ctx;
1059*4882a593Smuzhiyun 	u32 message_type = msg->header.message_type;
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun 	/*
1062*4882a593Smuzhiyun 	 * 'enum vmbus_channel_message_type' is supposed to always be 'u32' as
1063*4882a593Smuzhiyun 	 * it is being used in 'struct vmbus_channel_message_header' definition
1064*4882a593Smuzhiyun 	 * which is supposed to match hypervisor ABI.
1065*4882a593Smuzhiyun 	 */
1066*4882a593Smuzhiyun 	BUILD_BUG_ON(sizeof(enum vmbus_channel_message_type) != sizeof(u32));
1067*4882a593Smuzhiyun 
1068*4882a593Smuzhiyun 	if (message_type == HVMSG_NONE)
1069*4882a593Smuzhiyun 		/* no msg */
1070*4882a593Smuzhiyun 		return;
1071*4882a593Smuzhiyun 
1072*4882a593Smuzhiyun 	hdr = (struct vmbus_channel_message_header *)msg->u.payload;
1073*4882a593Smuzhiyun 
1074*4882a593Smuzhiyun 	trace_vmbus_on_msg_dpc(hdr);
1075*4882a593Smuzhiyun 
1076*4882a593Smuzhiyun 	if (hdr->msgtype >= CHANNELMSG_COUNT) {
1077*4882a593Smuzhiyun 		WARN_ONCE(1, "unknown msgtype=%d\n", hdr->msgtype);
1078*4882a593Smuzhiyun 		goto msg_handled;
1079*4882a593Smuzhiyun 	}
1080*4882a593Smuzhiyun 
1081*4882a593Smuzhiyun 	if (msg->header.payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT) {
1082*4882a593Smuzhiyun 		WARN_ONCE(1, "payload size is too large (%d)\n",
1083*4882a593Smuzhiyun 			  msg->header.payload_size);
1084*4882a593Smuzhiyun 		goto msg_handled;
1085*4882a593Smuzhiyun 	}
1086*4882a593Smuzhiyun 
1087*4882a593Smuzhiyun 	entry = &channel_message_table[hdr->msgtype];
1088*4882a593Smuzhiyun 
1089*4882a593Smuzhiyun 	if (!entry->message_handler)
1090*4882a593Smuzhiyun 		goto msg_handled;
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun 	if (msg->header.payload_size < entry->min_payload_len) {
1093*4882a593Smuzhiyun 		WARN_ONCE(1, "message too short: msgtype=%d len=%d\n",
1094*4882a593Smuzhiyun 			  hdr->msgtype, msg->header.payload_size);
1095*4882a593Smuzhiyun 		goto msg_handled;
1096*4882a593Smuzhiyun 	}
1097*4882a593Smuzhiyun 
1098*4882a593Smuzhiyun 	if (entry->handler_type	== VMHT_BLOCKING) {
1099*4882a593Smuzhiyun 		ctx = kmalloc(sizeof(*ctx) + msg->header.payload_size,
1100*4882a593Smuzhiyun 			      GFP_ATOMIC);
1101*4882a593Smuzhiyun 		if (ctx == NULL)
1102*4882a593Smuzhiyun 			return;
1103*4882a593Smuzhiyun 
1104*4882a593Smuzhiyun 		INIT_WORK(&ctx->work, vmbus_onmessage_work);
1105*4882a593Smuzhiyun 		memcpy(&ctx->msg, msg, sizeof(msg->header) +
1106*4882a593Smuzhiyun 		       msg->header.payload_size);
1107*4882a593Smuzhiyun 
1108*4882a593Smuzhiyun 		/*
1109*4882a593Smuzhiyun 		 * The host can generate a rescind message while we
1110*4882a593Smuzhiyun 		 * may still be handling the original offer. We deal with
1111*4882a593Smuzhiyun 		 * this condition by relying on the synchronization provided
1112*4882a593Smuzhiyun 		 * by offer_in_progress and by channel_mutex.  See also the
1113*4882a593Smuzhiyun 		 * inline comments in vmbus_onoffer_rescind().
1114*4882a593Smuzhiyun 		 */
1115*4882a593Smuzhiyun 		switch (hdr->msgtype) {
1116*4882a593Smuzhiyun 		case CHANNELMSG_RESCIND_CHANNELOFFER:
1117*4882a593Smuzhiyun 			/*
1118*4882a593Smuzhiyun 			 * If we are handling the rescind message;
1119*4882a593Smuzhiyun 			 * schedule the work on the global work queue.
1120*4882a593Smuzhiyun 			 *
1121*4882a593Smuzhiyun 			 * The OFFER message and the RESCIND message should
1122*4882a593Smuzhiyun 			 * not be handled by the same serialized work queue,
1123*4882a593Smuzhiyun 			 * because the OFFER handler may call vmbus_open(),
1124*4882a593Smuzhiyun 			 * which tries to open the channel by sending an
1125*4882a593Smuzhiyun 			 * OPEN_CHANNEL message to the host and waits for
1126*4882a593Smuzhiyun 			 * the host's response; however, if the host has
1127*4882a593Smuzhiyun 			 * rescinded the channel before it receives the
1128*4882a593Smuzhiyun 			 * OPEN_CHANNEL message, the host just silently
1129*4882a593Smuzhiyun 			 * ignores the OPEN_CHANNEL message; as a result,
1130*4882a593Smuzhiyun 			 * the guest's OFFER handler hangs for ever, if we
1131*4882a593Smuzhiyun 			 * handle the RESCIND message in the same serialized
1132*4882a593Smuzhiyun 			 * work queue: the RESCIND handler can not start to
1133*4882a593Smuzhiyun 			 * run before the OFFER handler finishes.
1134*4882a593Smuzhiyun 			 */
1135*4882a593Smuzhiyun 			schedule_work(&ctx->work);
1136*4882a593Smuzhiyun 			break;
1137*4882a593Smuzhiyun 
1138*4882a593Smuzhiyun 		case CHANNELMSG_OFFERCHANNEL:
1139*4882a593Smuzhiyun 			/*
1140*4882a593Smuzhiyun 			 * The host sends the offer message of a given channel
1141*4882a593Smuzhiyun 			 * before sending the rescind message of the same
1142*4882a593Smuzhiyun 			 * channel.  These messages are sent to the guest's
1143*4882a593Smuzhiyun 			 * connect CPU; the guest then starts processing them
1144*4882a593Smuzhiyun 			 * in the tasklet handler on this CPU:
1145*4882a593Smuzhiyun 			 *
1146*4882a593Smuzhiyun 			 * VMBUS_CONNECT_CPU
1147*4882a593Smuzhiyun 			 *
1148*4882a593Smuzhiyun 			 * [vmbus_on_msg_dpc()]
1149*4882a593Smuzhiyun 			 * atomic_inc()  // CHANNELMSG_OFFERCHANNEL
1150*4882a593Smuzhiyun 			 * queue_work()
1151*4882a593Smuzhiyun 			 * ...
1152*4882a593Smuzhiyun 			 * [vmbus_on_msg_dpc()]
1153*4882a593Smuzhiyun 			 * schedule_work()  // CHANNELMSG_RESCIND_CHANNELOFFER
1154*4882a593Smuzhiyun 			 *
1155*4882a593Smuzhiyun 			 * We rely on the memory-ordering properties of the
1156*4882a593Smuzhiyun 			 * queue_work() and schedule_work() primitives, which
1157*4882a593Smuzhiyun 			 * guarantee that the atomic increment will be visible
1158*4882a593Smuzhiyun 			 * to the CPUs which will execute the offer & rescind
1159*4882a593Smuzhiyun 			 * works by the time these works will start execution.
1160*4882a593Smuzhiyun 			 */
1161*4882a593Smuzhiyun 			atomic_inc(&vmbus_connection.offer_in_progress);
1162*4882a593Smuzhiyun 			fallthrough;
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun 		default:
1165*4882a593Smuzhiyun 			queue_work(vmbus_connection.work_queue, &ctx->work);
1166*4882a593Smuzhiyun 		}
1167*4882a593Smuzhiyun 	} else
1168*4882a593Smuzhiyun 		entry->message_handler(hdr);
1169*4882a593Smuzhiyun 
1170*4882a593Smuzhiyun msg_handled:
1171*4882a593Smuzhiyun 	vmbus_signal_eom(msg, message_type);
1172*4882a593Smuzhiyun }
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
1175*4882a593Smuzhiyun /*
1176*4882a593Smuzhiyun  * Fake RESCIND_CHANNEL messages to clean up hv_sock channels by force for
1177*4882a593Smuzhiyun  * hibernation, because hv_sock connections can not persist across hibernation.
1178*4882a593Smuzhiyun  */
vmbus_force_channel_rescinded(struct vmbus_channel * channel)1179*4882a593Smuzhiyun static void vmbus_force_channel_rescinded(struct vmbus_channel *channel)
1180*4882a593Smuzhiyun {
1181*4882a593Smuzhiyun 	struct onmessage_work_context *ctx;
1182*4882a593Smuzhiyun 	struct vmbus_channel_rescind_offer *rescind;
1183*4882a593Smuzhiyun 
1184*4882a593Smuzhiyun 	WARN_ON(!is_hvsock_channel(channel));
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun 	/*
1187*4882a593Smuzhiyun 	 * Allocation size is small and the allocation should really not fail,
1188*4882a593Smuzhiyun 	 * otherwise the state of the hv_sock connections ends up in limbo.
1189*4882a593Smuzhiyun 	 */
1190*4882a593Smuzhiyun 	ctx = kzalloc(sizeof(*ctx) + sizeof(*rescind),
1191*4882a593Smuzhiyun 		      GFP_KERNEL | __GFP_NOFAIL);
1192*4882a593Smuzhiyun 
1193*4882a593Smuzhiyun 	/*
1194*4882a593Smuzhiyun 	 * So far, these are not really used by Linux. Just set them to the
1195*4882a593Smuzhiyun 	 * reasonable values conforming to the definitions of the fields.
1196*4882a593Smuzhiyun 	 */
1197*4882a593Smuzhiyun 	ctx->msg.header.message_type = 1;
1198*4882a593Smuzhiyun 	ctx->msg.header.payload_size = sizeof(*rescind);
1199*4882a593Smuzhiyun 
1200*4882a593Smuzhiyun 	/* These values are actually used by Linux. */
1201*4882a593Smuzhiyun 	rescind = (struct vmbus_channel_rescind_offer *)ctx->msg.payload;
1202*4882a593Smuzhiyun 	rescind->header.msgtype = CHANNELMSG_RESCIND_CHANNELOFFER;
1203*4882a593Smuzhiyun 	rescind->child_relid = channel->offermsg.child_relid;
1204*4882a593Smuzhiyun 
1205*4882a593Smuzhiyun 	INIT_WORK(&ctx->work, vmbus_onmessage_work);
1206*4882a593Smuzhiyun 
1207*4882a593Smuzhiyun 	queue_work(vmbus_connection.work_queue, &ctx->work);
1208*4882a593Smuzhiyun }
1209*4882a593Smuzhiyun #endif /* CONFIG_PM_SLEEP */
1210*4882a593Smuzhiyun 
1211*4882a593Smuzhiyun /*
1212*4882a593Smuzhiyun  * Schedule all channels with events pending
1213*4882a593Smuzhiyun  */
vmbus_chan_sched(struct hv_per_cpu_context * hv_cpu)1214*4882a593Smuzhiyun static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
1215*4882a593Smuzhiyun {
1216*4882a593Smuzhiyun 	unsigned long *recv_int_page;
1217*4882a593Smuzhiyun 	u32 maxbits, relid;
1218*4882a593Smuzhiyun 
1219*4882a593Smuzhiyun 	if (vmbus_proto_version < VERSION_WIN8) {
1220*4882a593Smuzhiyun 		maxbits = MAX_NUM_CHANNELS_SUPPORTED;
1221*4882a593Smuzhiyun 		recv_int_page = vmbus_connection.recv_int_page;
1222*4882a593Smuzhiyun 	} else {
1223*4882a593Smuzhiyun 		/*
1224*4882a593Smuzhiyun 		 * When the host is win8 and beyond, the event page
1225*4882a593Smuzhiyun 		 * can be directly checked to get the id of the channel
1226*4882a593Smuzhiyun 		 * that has the interrupt pending.
1227*4882a593Smuzhiyun 		 */
1228*4882a593Smuzhiyun 		void *page_addr = hv_cpu->synic_event_page;
1229*4882a593Smuzhiyun 		union hv_synic_event_flags *event
1230*4882a593Smuzhiyun 			= (union hv_synic_event_flags *)page_addr +
1231*4882a593Smuzhiyun 						 VMBUS_MESSAGE_SINT;
1232*4882a593Smuzhiyun 
1233*4882a593Smuzhiyun 		maxbits = HV_EVENT_FLAGS_COUNT;
1234*4882a593Smuzhiyun 		recv_int_page = event->flags;
1235*4882a593Smuzhiyun 	}
1236*4882a593Smuzhiyun 
1237*4882a593Smuzhiyun 	if (unlikely(!recv_int_page))
1238*4882a593Smuzhiyun 		return;
1239*4882a593Smuzhiyun 
1240*4882a593Smuzhiyun 	for_each_set_bit(relid, recv_int_page, maxbits) {
1241*4882a593Smuzhiyun 		void (*callback_fn)(void *context);
1242*4882a593Smuzhiyun 		struct vmbus_channel *channel;
1243*4882a593Smuzhiyun 
1244*4882a593Smuzhiyun 		if (!sync_test_and_clear_bit(relid, recv_int_page))
1245*4882a593Smuzhiyun 			continue;
1246*4882a593Smuzhiyun 
1247*4882a593Smuzhiyun 		/* Special case - vmbus channel protocol msg */
1248*4882a593Smuzhiyun 		if (relid == 0)
1249*4882a593Smuzhiyun 			continue;
1250*4882a593Smuzhiyun 
1251*4882a593Smuzhiyun 		/*
1252*4882a593Smuzhiyun 		 * Pairs with the kfree_rcu() in vmbus_chan_release().
1253*4882a593Smuzhiyun 		 * Guarantees that the channel data structure doesn't
1254*4882a593Smuzhiyun 		 * get freed while the channel pointer below is being
1255*4882a593Smuzhiyun 		 * dereferenced.
1256*4882a593Smuzhiyun 		 */
1257*4882a593Smuzhiyun 		rcu_read_lock();
1258*4882a593Smuzhiyun 
1259*4882a593Smuzhiyun 		/* Find channel based on relid */
1260*4882a593Smuzhiyun 		channel = relid2channel(relid);
1261*4882a593Smuzhiyun 		if (channel == NULL)
1262*4882a593Smuzhiyun 			goto sched_unlock_rcu;
1263*4882a593Smuzhiyun 
1264*4882a593Smuzhiyun 		if (channel->rescind)
1265*4882a593Smuzhiyun 			goto sched_unlock_rcu;
1266*4882a593Smuzhiyun 
1267*4882a593Smuzhiyun 		/*
1268*4882a593Smuzhiyun 		 * Make sure that the ring buffer data structure doesn't get
1269*4882a593Smuzhiyun 		 * freed while we dereference the ring buffer pointer.  Test
1270*4882a593Smuzhiyun 		 * for the channel's onchannel_callback being NULL within a
1271*4882a593Smuzhiyun 		 * sched_lock critical section.  See also the inline comments
1272*4882a593Smuzhiyun 		 * in vmbus_reset_channel_cb().
1273*4882a593Smuzhiyun 		 */
1274*4882a593Smuzhiyun 		spin_lock(&channel->sched_lock);
1275*4882a593Smuzhiyun 
1276*4882a593Smuzhiyun 		callback_fn = channel->onchannel_callback;
1277*4882a593Smuzhiyun 		if (unlikely(callback_fn == NULL))
1278*4882a593Smuzhiyun 			goto sched_unlock;
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun 		trace_vmbus_chan_sched(channel);
1281*4882a593Smuzhiyun 
1282*4882a593Smuzhiyun 		++channel->interrupts;
1283*4882a593Smuzhiyun 
1284*4882a593Smuzhiyun 		switch (channel->callback_mode) {
1285*4882a593Smuzhiyun 		case HV_CALL_ISR:
1286*4882a593Smuzhiyun 			(*callback_fn)(channel->channel_callback_context);
1287*4882a593Smuzhiyun 			break;
1288*4882a593Smuzhiyun 
1289*4882a593Smuzhiyun 		case HV_CALL_BATCHED:
1290*4882a593Smuzhiyun 			hv_begin_read(&channel->inbound);
1291*4882a593Smuzhiyun 			fallthrough;
1292*4882a593Smuzhiyun 		case HV_CALL_DIRECT:
1293*4882a593Smuzhiyun 			tasklet_schedule(&channel->callback_event);
1294*4882a593Smuzhiyun 		}
1295*4882a593Smuzhiyun 
1296*4882a593Smuzhiyun sched_unlock:
1297*4882a593Smuzhiyun 		spin_unlock(&channel->sched_lock);
1298*4882a593Smuzhiyun sched_unlock_rcu:
1299*4882a593Smuzhiyun 		rcu_read_unlock();
1300*4882a593Smuzhiyun 	}
1301*4882a593Smuzhiyun }
1302*4882a593Smuzhiyun 
vmbus_isr(void)1303*4882a593Smuzhiyun static void vmbus_isr(void)
1304*4882a593Smuzhiyun {
1305*4882a593Smuzhiyun 	struct hv_per_cpu_context *hv_cpu
1306*4882a593Smuzhiyun 		= this_cpu_ptr(hv_context.cpu_context);
1307*4882a593Smuzhiyun 	void *page_addr = hv_cpu->synic_event_page;
1308*4882a593Smuzhiyun 	struct hv_message *msg;
1309*4882a593Smuzhiyun 	union hv_synic_event_flags *event;
1310*4882a593Smuzhiyun 	bool handled = false;
1311*4882a593Smuzhiyun 
1312*4882a593Smuzhiyun 	if (unlikely(page_addr == NULL))
1313*4882a593Smuzhiyun 		return;
1314*4882a593Smuzhiyun 
1315*4882a593Smuzhiyun 	event = (union hv_synic_event_flags *)page_addr +
1316*4882a593Smuzhiyun 					 VMBUS_MESSAGE_SINT;
1317*4882a593Smuzhiyun 	/*
1318*4882a593Smuzhiyun 	 * Check for events before checking for messages. This is the order
1319*4882a593Smuzhiyun 	 * in which events and messages are checked in Windows guests on
1320*4882a593Smuzhiyun 	 * Hyper-V, and the Windows team suggested we do the same.
1321*4882a593Smuzhiyun 	 */
1322*4882a593Smuzhiyun 
1323*4882a593Smuzhiyun 	if ((vmbus_proto_version == VERSION_WS2008) ||
1324*4882a593Smuzhiyun 		(vmbus_proto_version == VERSION_WIN7)) {
1325*4882a593Smuzhiyun 
1326*4882a593Smuzhiyun 		/* Since we are a child, we only need to check bit 0 */
1327*4882a593Smuzhiyun 		if (sync_test_and_clear_bit(0, event->flags))
1328*4882a593Smuzhiyun 			handled = true;
1329*4882a593Smuzhiyun 	} else {
1330*4882a593Smuzhiyun 		/*
1331*4882a593Smuzhiyun 		 * Our host is win8 or above. The signaling mechanism
1332*4882a593Smuzhiyun 		 * has changed and we can directly look at the event page.
1333*4882a593Smuzhiyun 		 * If bit n is set then we have an interrup on the channel
1334*4882a593Smuzhiyun 		 * whose id is n.
1335*4882a593Smuzhiyun 		 */
1336*4882a593Smuzhiyun 		handled = true;
1337*4882a593Smuzhiyun 	}
1338*4882a593Smuzhiyun 
1339*4882a593Smuzhiyun 	if (handled)
1340*4882a593Smuzhiyun 		vmbus_chan_sched(hv_cpu);
1341*4882a593Smuzhiyun 
1342*4882a593Smuzhiyun 	page_addr = hv_cpu->synic_message_page;
1343*4882a593Smuzhiyun 	msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun 	/* Check if there are actual msgs to be processed */
1346*4882a593Smuzhiyun 	if (msg->header.message_type != HVMSG_NONE) {
1347*4882a593Smuzhiyun 		if (msg->header.message_type == HVMSG_TIMER_EXPIRED) {
1348*4882a593Smuzhiyun 			hv_stimer0_isr();
1349*4882a593Smuzhiyun 			vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
1350*4882a593Smuzhiyun 		} else
1351*4882a593Smuzhiyun 			tasklet_schedule(&hv_cpu->msg_dpc);
1352*4882a593Smuzhiyun 	}
1353*4882a593Smuzhiyun 
1354*4882a593Smuzhiyun 	add_interrupt_randomness(hv_get_vector());
1355*4882a593Smuzhiyun }
1356*4882a593Smuzhiyun 
1357*4882a593Smuzhiyun /*
1358*4882a593Smuzhiyun  * Callback from kmsg_dump. Grab as much as possible from the end of the kmsg
1359*4882a593Smuzhiyun  * buffer and call into Hyper-V to transfer the data.
1360*4882a593Smuzhiyun  */
hv_kmsg_dump(struct kmsg_dumper * dumper,enum kmsg_dump_reason reason)1361*4882a593Smuzhiyun static void hv_kmsg_dump(struct kmsg_dumper *dumper,
1362*4882a593Smuzhiyun 			 enum kmsg_dump_reason reason)
1363*4882a593Smuzhiyun {
1364*4882a593Smuzhiyun 	size_t bytes_written;
1365*4882a593Smuzhiyun 	phys_addr_t panic_pa;
1366*4882a593Smuzhiyun 
1367*4882a593Smuzhiyun 	/* We are only interested in panics. */
1368*4882a593Smuzhiyun 	if ((reason != KMSG_DUMP_PANIC) || (!sysctl_record_panic_msg))
1369*4882a593Smuzhiyun 		return;
1370*4882a593Smuzhiyun 
1371*4882a593Smuzhiyun 	panic_pa = virt_to_phys(hv_panic_page);
1372*4882a593Smuzhiyun 
1373*4882a593Smuzhiyun 	/*
1374*4882a593Smuzhiyun 	 * Write dump contents to the page. No need to synchronize; panic should
1375*4882a593Smuzhiyun 	 * be single-threaded.
1376*4882a593Smuzhiyun 	 */
1377*4882a593Smuzhiyun 	kmsg_dump_get_buffer(dumper, false, hv_panic_page, HV_HYP_PAGE_SIZE,
1378*4882a593Smuzhiyun 			     &bytes_written);
1379*4882a593Smuzhiyun 	if (bytes_written)
1380*4882a593Smuzhiyun 		hyperv_report_panic_msg(panic_pa, bytes_written);
1381*4882a593Smuzhiyun }
1382*4882a593Smuzhiyun 
1383*4882a593Smuzhiyun static struct kmsg_dumper hv_kmsg_dumper = {
1384*4882a593Smuzhiyun 	.dump = hv_kmsg_dump,
1385*4882a593Smuzhiyun };
1386*4882a593Smuzhiyun 
1387*4882a593Smuzhiyun static struct ctl_table_header *hv_ctl_table_hdr;
1388*4882a593Smuzhiyun 
1389*4882a593Smuzhiyun /*
1390*4882a593Smuzhiyun  * sysctl option to allow the user to control whether kmsg data should be
1391*4882a593Smuzhiyun  * reported to Hyper-V on panic.
1392*4882a593Smuzhiyun  */
1393*4882a593Smuzhiyun static struct ctl_table hv_ctl_table[] = {
1394*4882a593Smuzhiyun 	{
1395*4882a593Smuzhiyun 		.procname       = "hyperv_record_panic_msg",
1396*4882a593Smuzhiyun 		.data           = &sysctl_record_panic_msg,
1397*4882a593Smuzhiyun 		.maxlen         = sizeof(int),
1398*4882a593Smuzhiyun 		.mode           = 0644,
1399*4882a593Smuzhiyun 		.proc_handler   = proc_dointvec_minmax,
1400*4882a593Smuzhiyun 		.extra1		= SYSCTL_ZERO,
1401*4882a593Smuzhiyun 		.extra2		= SYSCTL_ONE
1402*4882a593Smuzhiyun 	},
1403*4882a593Smuzhiyun 	{}
1404*4882a593Smuzhiyun };
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun static struct ctl_table hv_root_table[] = {
1407*4882a593Smuzhiyun 	{
1408*4882a593Smuzhiyun 		.procname	= "kernel",
1409*4882a593Smuzhiyun 		.mode		= 0555,
1410*4882a593Smuzhiyun 		.child		= hv_ctl_table
1411*4882a593Smuzhiyun 	},
1412*4882a593Smuzhiyun 	{}
1413*4882a593Smuzhiyun };
1414*4882a593Smuzhiyun 
1415*4882a593Smuzhiyun /*
1416*4882a593Smuzhiyun  * vmbus_bus_init -Main vmbus driver initialization routine.
1417*4882a593Smuzhiyun  *
1418*4882a593Smuzhiyun  * Here, we
1419*4882a593Smuzhiyun  *	- initialize the vmbus driver context
1420*4882a593Smuzhiyun  *	- invoke the vmbus hv main init routine
1421*4882a593Smuzhiyun  *	- retrieve the channel offers
1422*4882a593Smuzhiyun  */
vmbus_bus_init(void)1423*4882a593Smuzhiyun static int vmbus_bus_init(void)
1424*4882a593Smuzhiyun {
1425*4882a593Smuzhiyun 	int ret;
1426*4882a593Smuzhiyun 
1427*4882a593Smuzhiyun 	ret = hv_init();
1428*4882a593Smuzhiyun 	if (ret != 0) {
1429*4882a593Smuzhiyun 		pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
1430*4882a593Smuzhiyun 		return ret;
1431*4882a593Smuzhiyun 	}
1432*4882a593Smuzhiyun 
1433*4882a593Smuzhiyun 	ret = bus_register(&hv_bus);
1434*4882a593Smuzhiyun 	if (ret)
1435*4882a593Smuzhiyun 		return ret;
1436*4882a593Smuzhiyun 
1437*4882a593Smuzhiyun 	ret = hv_setup_vmbus_irq(vmbus_irq, vmbus_isr);
1438*4882a593Smuzhiyun 	if (ret)
1439*4882a593Smuzhiyun 		goto err_setup;
1440*4882a593Smuzhiyun 
1441*4882a593Smuzhiyun 	ret = hv_synic_alloc();
1442*4882a593Smuzhiyun 	if (ret)
1443*4882a593Smuzhiyun 		goto err_alloc;
1444*4882a593Smuzhiyun 
1445*4882a593Smuzhiyun 	/*
1446*4882a593Smuzhiyun 	 * Initialize the per-cpu interrupt state and stimer state.
1447*4882a593Smuzhiyun 	 * Then connect to the host.
1448*4882a593Smuzhiyun 	 */
1449*4882a593Smuzhiyun 	ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online",
1450*4882a593Smuzhiyun 				hv_synic_init, hv_synic_cleanup);
1451*4882a593Smuzhiyun 	if (ret < 0)
1452*4882a593Smuzhiyun 		goto err_cpuhp;
1453*4882a593Smuzhiyun 	hyperv_cpuhp_online = ret;
1454*4882a593Smuzhiyun 
1455*4882a593Smuzhiyun 	ret = vmbus_connect();
1456*4882a593Smuzhiyun 	if (ret)
1457*4882a593Smuzhiyun 		goto err_connect;
1458*4882a593Smuzhiyun 
1459*4882a593Smuzhiyun 	/*
1460*4882a593Smuzhiyun 	 * Only register if the crash MSRs are available
1461*4882a593Smuzhiyun 	 */
1462*4882a593Smuzhiyun 	if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
1463*4882a593Smuzhiyun 		u64 hyperv_crash_ctl;
1464*4882a593Smuzhiyun 		/*
1465*4882a593Smuzhiyun 		 * Sysctl registration is not fatal, since by default
1466*4882a593Smuzhiyun 		 * reporting is enabled.
1467*4882a593Smuzhiyun 		 */
1468*4882a593Smuzhiyun 		hv_ctl_table_hdr = register_sysctl_table(hv_root_table);
1469*4882a593Smuzhiyun 		if (!hv_ctl_table_hdr)
1470*4882a593Smuzhiyun 			pr_err("Hyper-V: sysctl table register error");
1471*4882a593Smuzhiyun 
1472*4882a593Smuzhiyun 		/*
1473*4882a593Smuzhiyun 		 * Register for panic kmsg callback only if the right
1474*4882a593Smuzhiyun 		 * capability is supported by the hypervisor.
1475*4882a593Smuzhiyun 		 */
1476*4882a593Smuzhiyun 		hv_get_crash_ctl(hyperv_crash_ctl);
1477*4882a593Smuzhiyun 		if (hyperv_crash_ctl & HV_CRASH_CTL_CRASH_NOTIFY_MSG) {
1478*4882a593Smuzhiyun 			hv_panic_page = (void *)hv_alloc_hyperv_zeroed_page();
1479*4882a593Smuzhiyun 			if (hv_panic_page) {
1480*4882a593Smuzhiyun 				ret = kmsg_dump_register(&hv_kmsg_dumper);
1481*4882a593Smuzhiyun 				if (ret) {
1482*4882a593Smuzhiyun 					pr_err("Hyper-V: kmsg dump register "
1483*4882a593Smuzhiyun 						"error 0x%x\n", ret);
1484*4882a593Smuzhiyun 					hv_free_hyperv_page(
1485*4882a593Smuzhiyun 					    (unsigned long)hv_panic_page);
1486*4882a593Smuzhiyun 					hv_panic_page = NULL;
1487*4882a593Smuzhiyun 				}
1488*4882a593Smuzhiyun 			} else
1489*4882a593Smuzhiyun 				pr_err("Hyper-V: panic message page memory "
1490*4882a593Smuzhiyun 					"allocation failed");
1491*4882a593Smuzhiyun 		}
1492*4882a593Smuzhiyun 
1493*4882a593Smuzhiyun 		register_die_notifier(&hyperv_die_block);
1494*4882a593Smuzhiyun 	}
1495*4882a593Smuzhiyun 
1496*4882a593Smuzhiyun 	/*
1497*4882a593Smuzhiyun 	 * Always register the panic notifier because we need to unload
1498*4882a593Smuzhiyun 	 * the VMbus channel connection to prevent any VMbus
1499*4882a593Smuzhiyun 	 * activity after the VM panics.
1500*4882a593Smuzhiyun 	 */
1501*4882a593Smuzhiyun 	atomic_notifier_chain_register(&panic_notifier_list,
1502*4882a593Smuzhiyun 			       &hyperv_panic_block);
1503*4882a593Smuzhiyun 
1504*4882a593Smuzhiyun 	vmbus_request_offers();
1505*4882a593Smuzhiyun 
1506*4882a593Smuzhiyun 	return 0;
1507*4882a593Smuzhiyun 
1508*4882a593Smuzhiyun err_connect:
1509*4882a593Smuzhiyun 	cpuhp_remove_state(hyperv_cpuhp_online);
1510*4882a593Smuzhiyun err_cpuhp:
1511*4882a593Smuzhiyun 	hv_synic_free();
1512*4882a593Smuzhiyun err_alloc:
1513*4882a593Smuzhiyun 	hv_remove_vmbus_irq();
1514*4882a593Smuzhiyun err_setup:
1515*4882a593Smuzhiyun 	bus_unregister(&hv_bus);
1516*4882a593Smuzhiyun 	unregister_sysctl_table(hv_ctl_table_hdr);
1517*4882a593Smuzhiyun 	hv_ctl_table_hdr = NULL;
1518*4882a593Smuzhiyun 	return ret;
1519*4882a593Smuzhiyun }
1520*4882a593Smuzhiyun 
1521*4882a593Smuzhiyun /**
1522*4882a593Smuzhiyun  * __vmbus_child_driver_register() - Register a vmbus's driver
1523*4882a593Smuzhiyun  * @hv_driver: Pointer to driver structure you want to register
1524*4882a593Smuzhiyun  * @owner: owner module of the drv
1525*4882a593Smuzhiyun  * @mod_name: module name string
1526*4882a593Smuzhiyun  *
1527*4882a593Smuzhiyun  * Registers the given driver with Linux through the 'driver_register()' call
1528*4882a593Smuzhiyun  * and sets up the hyper-v vmbus handling for this driver.
1529*4882a593Smuzhiyun  * It will return the state of the 'driver_register()' call.
1530*4882a593Smuzhiyun  *
1531*4882a593Smuzhiyun  */
__vmbus_driver_register(struct hv_driver * hv_driver,struct module * owner,const char * mod_name)1532*4882a593Smuzhiyun int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
1533*4882a593Smuzhiyun {
1534*4882a593Smuzhiyun 	int ret;
1535*4882a593Smuzhiyun 
1536*4882a593Smuzhiyun 	pr_info("registering driver %s\n", hv_driver->name);
1537*4882a593Smuzhiyun 
1538*4882a593Smuzhiyun 	ret = vmbus_exists();
1539*4882a593Smuzhiyun 	if (ret < 0)
1540*4882a593Smuzhiyun 		return ret;
1541*4882a593Smuzhiyun 
1542*4882a593Smuzhiyun 	hv_driver->driver.name = hv_driver->name;
1543*4882a593Smuzhiyun 	hv_driver->driver.owner = owner;
1544*4882a593Smuzhiyun 	hv_driver->driver.mod_name = mod_name;
1545*4882a593Smuzhiyun 	hv_driver->driver.bus = &hv_bus;
1546*4882a593Smuzhiyun 
1547*4882a593Smuzhiyun 	spin_lock_init(&hv_driver->dynids.lock);
1548*4882a593Smuzhiyun 	INIT_LIST_HEAD(&hv_driver->dynids.list);
1549*4882a593Smuzhiyun 
1550*4882a593Smuzhiyun 	ret = driver_register(&hv_driver->driver);
1551*4882a593Smuzhiyun 
1552*4882a593Smuzhiyun 	return ret;
1553*4882a593Smuzhiyun }
1554*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(__vmbus_driver_register);
1555*4882a593Smuzhiyun 
1556*4882a593Smuzhiyun /**
1557*4882a593Smuzhiyun  * vmbus_driver_unregister() - Unregister a vmbus's driver
1558*4882a593Smuzhiyun  * @hv_driver: Pointer to driver structure you want to
1559*4882a593Smuzhiyun  *             un-register
1560*4882a593Smuzhiyun  *
1561*4882a593Smuzhiyun  * Un-register the given driver that was previous registered with a call to
1562*4882a593Smuzhiyun  * vmbus_driver_register()
1563*4882a593Smuzhiyun  */
vmbus_driver_unregister(struct hv_driver * hv_driver)1564*4882a593Smuzhiyun void vmbus_driver_unregister(struct hv_driver *hv_driver)
1565*4882a593Smuzhiyun {
1566*4882a593Smuzhiyun 	pr_info("unregistering driver %s\n", hv_driver->name);
1567*4882a593Smuzhiyun 
1568*4882a593Smuzhiyun 	if (!vmbus_exists()) {
1569*4882a593Smuzhiyun 		driver_unregister(&hv_driver->driver);
1570*4882a593Smuzhiyun 		vmbus_free_dynids(hv_driver);
1571*4882a593Smuzhiyun 	}
1572*4882a593Smuzhiyun }
1573*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
1574*4882a593Smuzhiyun 
1575*4882a593Smuzhiyun 
1576*4882a593Smuzhiyun /*
1577*4882a593Smuzhiyun  * Called when last reference to channel is gone.
1578*4882a593Smuzhiyun  */
vmbus_chan_release(struct kobject * kobj)1579*4882a593Smuzhiyun static void vmbus_chan_release(struct kobject *kobj)
1580*4882a593Smuzhiyun {
1581*4882a593Smuzhiyun 	struct vmbus_channel *channel
1582*4882a593Smuzhiyun 		= container_of(kobj, struct vmbus_channel, kobj);
1583*4882a593Smuzhiyun 
1584*4882a593Smuzhiyun 	kfree_rcu(channel, rcu);
1585*4882a593Smuzhiyun }
1586*4882a593Smuzhiyun 
1587*4882a593Smuzhiyun struct vmbus_chan_attribute {
1588*4882a593Smuzhiyun 	struct attribute attr;
1589*4882a593Smuzhiyun 	ssize_t (*show)(struct vmbus_channel *chan, char *buf);
1590*4882a593Smuzhiyun 	ssize_t (*store)(struct vmbus_channel *chan,
1591*4882a593Smuzhiyun 			 const char *buf, size_t count);
1592*4882a593Smuzhiyun };
1593*4882a593Smuzhiyun #define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \
1594*4882a593Smuzhiyun 	struct vmbus_chan_attribute chan_attr_##_name \
1595*4882a593Smuzhiyun 		= __ATTR(_name, _mode, _show, _store)
1596*4882a593Smuzhiyun #define VMBUS_CHAN_ATTR_RW(_name) \
1597*4882a593Smuzhiyun 	struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name)
1598*4882a593Smuzhiyun #define VMBUS_CHAN_ATTR_RO(_name) \
1599*4882a593Smuzhiyun 	struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name)
1600*4882a593Smuzhiyun #define VMBUS_CHAN_ATTR_WO(_name) \
1601*4882a593Smuzhiyun 	struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name)
1602*4882a593Smuzhiyun 
vmbus_chan_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)1603*4882a593Smuzhiyun static ssize_t vmbus_chan_attr_show(struct kobject *kobj,
1604*4882a593Smuzhiyun 				    struct attribute *attr, char *buf)
1605*4882a593Smuzhiyun {
1606*4882a593Smuzhiyun 	const struct vmbus_chan_attribute *attribute
1607*4882a593Smuzhiyun 		= container_of(attr, struct vmbus_chan_attribute, attr);
1608*4882a593Smuzhiyun 	struct vmbus_channel *chan
1609*4882a593Smuzhiyun 		= container_of(kobj, struct vmbus_channel, kobj);
1610*4882a593Smuzhiyun 
1611*4882a593Smuzhiyun 	if (!attribute->show)
1612*4882a593Smuzhiyun 		return -EIO;
1613*4882a593Smuzhiyun 
1614*4882a593Smuzhiyun 	return attribute->show(chan, buf);
1615*4882a593Smuzhiyun }
1616*4882a593Smuzhiyun 
vmbus_chan_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)1617*4882a593Smuzhiyun static ssize_t vmbus_chan_attr_store(struct kobject *kobj,
1618*4882a593Smuzhiyun 				     struct attribute *attr, const char *buf,
1619*4882a593Smuzhiyun 				     size_t count)
1620*4882a593Smuzhiyun {
1621*4882a593Smuzhiyun 	const struct vmbus_chan_attribute *attribute
1622*4882a593Smuzhiyun 		= container_of(attr, struct vmbus_chan_attribute, attr);
1623*4882a593Smuzhiyun 	struct vmbus_channel *chan
1624*4882a593Smuzhiyun 		= container_of(kobj, struct vmbus_channel, kobj);
1625*4882a593Smuzhiyun 
1626*4882a593Smuzhiyun 	if (!attribute->store)
1627*4882a593Smuzhiyun 		return -EIO;
1628*4882a593Smuzhiyun 
1629*4882a593Smuzhiyun 	return attribute->store(chan, buf, count);
1630*4882a593Smuzhiyun }
1631*4882a593Smuzhiyun 
1632*4882a593Smuzhiyun static const struct sysfs_ops vmbus_chan_sysfs_ops = {
1633*4882a593Smuzhiyun 	.show = vmbus_chan_attr_show,
1634*4882a593Smuzhiyun 	.store = vmbus_chan_attr_store,
1635*4882a593Smuzhiyun };
1636*4882a593Smuzhiyun 
out_mask_show(struct vmbus_channel * channel,char * buf)1637*4882a593Smuzhiyun static ssize_t out_mask_show(struct vmbus_channel *channel, char *buf)
1638*4882a593Smuzhiyun {
1639*4882a593Smuzhiyun 	struct hv_ring_buffer_info *rbi = &channel->outbound;
1640*4882a593Smuzhiyun 	ssize_t ret;
1641*4882a593Smuzhiyun 
1642*4882a593Smuzhiyun 	mutex_lock(&rbi->ring_buffer_mutex);
1643*4882a593Smuzhiyun 	if (!rbi->ring_buffer) {
1644*4882a593Smuzhiyun 		mutex_unlock(&rbi->ring_buffer_mutex);
1645*4882a593Smuzhiyun 		return -EINVAL;
1646*4882a593Smuzhiyun 	}
1647*4882a593Smuzhiyun 
1648*4882a593Smuzhiyun 	ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1649*4882a593Smuzhiyun 	mutex_unlock(&rbi->ring_buffer_mutex);
1650*4882a593Smuzhiyun 	return ret;
1651*4882a593Smuzhiyun }
1652*4882a593Smuzhiyun static VMBUS_CHAN_ATTR_RO(out_mask);
1653*4882a593Smuzhiyun 
in_mask_show(struct vmbus_channel * channel,char * buf)1654*4882a593Smuzhiyun static ssize_t in_mask_show(struct vmbus_channel *channel, char *buf)
1655*4882a593Smuzhiyun {
1656*4882a593Smuzhiyun 	struct hv_ring_buffer_info *rbi = &channel->inbound;
1657*4882a593Smuzhiyun 	ssize_t ret;
1658*4882a593Smuzhiyun 
1659*4882a593Smuzhiyun 	mutex_lock(&rbi->ring_buffer_mutex);
1660*4882a593Smuzhiyun 	if (!rbi->ring_buffer) {
1661*4882a593Smuzhiyun 		mutex_unlock(&rbi->ring_buffer_mutex);
1662*4882a593Smuzhiyun 		return -EINVAL;
1663*4882a593Smuzhiyun 	}
1664*4882a593Smuzhiyun 
1665*4882a593Smuzhiyun 	ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
1666*4882a593Smuzhiyun 	mutex_unlock(&rbi->ring_buffer_mutex);
1667*4882a593Smuzhiyun 	return ret;
1668*4882a593Smuzhiyun }
1669*4882a593Smuzhiyun static VMBUS_CHAN_ATTR_RO(in_mask);
1670*4882a593Smuzhiyun 
read_avail_show(struct vmbus_channel * channel,char * buf)1671*4882a593Smuzhiyun static ssize_t read_avail_show(struct vmbus_channel *channel, char *buf)
1672*4882a593Smuzhiyun {
1673*4882a593Smuzhiyun 	struct hv_ring_buffer_info *rbi = &channel->inbound;
1674*4882a593Smuzhiyun 	ssize_t ret;
1675*4882a593Smuzhiyun 
1676*4882a593Smuzhiyun 	mutex_lock(&rbi->ring_buffer_mutex);
1677*4882a593Smuzhiyun 	if (!rbi->ring_buffer) {
1678*4882a593Smuzhiyun 		mutex_unlock(&rbi->ring_buffer_mutex);
1679*4882a593Smuzhiyun 		return -EINVAL;
1680*4882a593Smuzhiyun 	}
1681*4882a593Smuzhiyun 
1682*4882a593Smuzhiyun 	ret = sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
1683*4882a593Smuzhiyun 	mutex_unlock(&rbi->ring_buffer_mutex);
1684*4882a593Smuzhiyun 	return ret;
1685*4882a593Smuzhiyun }
1686*4882a593Smuzhiyun static VMBUS_CHAN_ATTR_RO(read_avail);
1687*4882a593Smuzhiyun 
write_avail_show(struct vmbus_channel * channel,char * buf)1688*4882a593Smuzhiyun static ssize_t write_avail_show(struct vmbus_channel *channel, char *buf)
1689*4882a593Smuzhiyun {
1690*4882a593Smuzhiyun 	struct hv_ring_buffer_info *rbi = &channel->outbound;
1691*4882a593Smuzhiyun 	ssize_t ret;
1692*4882a593Smuzhiyun 
1693*4882a593Smuzhiyun 	mutex_lock(&rbi->ring_buffer_mutex);
1694*4882a593Smuzhiyun 	if (!rbi->ring_buffer) {
1695*4882a593Smuzhiyun 		mutex_unlock(&rbi->ring_buffer_mutex);
1696*4882a593Smuzhiyun 		return -EINVAL;
1697*4882a593Smuzhiyun 	}
1698*4882a593Smuzhiyun 
1699*4882a593Smuzhiyun 	ret = sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
1700*4882a593Smuzhiyun 	mutex_unlock(&rbi->ring_buffer_mutex);
1701*4882a593Smuzhiyun 	return ret;
1702*4882a593Smuzhiyun }
1703*4882a593Smuzhiyun static VMBUS_CHAN_ATTR_RO(write_avail);
1704*4882a593Smuzhiyun 
target_cpu_show(struct vmbus_channel * channel,char * buf)1705*4882a593Smuzhiyun static ssize_t target_cpu_show(struct vmbus_channel *channel, char *buf)
1706*4882a593Smuzhiyun {
1707*4882a593Smuzhiyun 	return sprintf(buf, "%u\n", channel->target_cpu);
1708*4882a593Smuzhiyun }
target_cpu_store(struct vmbus_channel * channel,const char * buf,size_t count)1709*4882a593Smuzhiyun static ssize_t target_cpu_store(struct vmbus_channel *channel,
1710*4882a593Smuzhiyun 				const char *buf, size_t count)
1711*4882a593Smuzhiyun {
1712*4882a593Smuzhiyun 	u32 target_cpu, origin_cpu;
1713*4882a593Smuzhiyun 	ssize_t ret = count;
1714*4882a593Smuzhiyun 
1715*4882a593Smuzhiyun 	if (vmbus_proto_version < VERSION_WIN10_V4_1)
1716*4882a593Smuzhiyun 		return -EIO;
1717*4882a593Smuzhiyun 
1718*4882a593Smuzhiyun 	if (sscanf(buf, "%uu", &target_cpu) != 1)
1719*4882a593Smuzhiyun 		return -EIO;
1720*4882a593Smuzhiyun 
1721*4882a593Smuzhiyun 	/* Validate target_cpu for the cpumask_test_cpu() operation below. */
1722*4882a593Smuzhiyun 	if (target_cpu >= nr_cpumask_bits)
1723*4882a593Smuzhiyun 		return -EINVAL;
1724*4882a593Smuzhiyun 
1725*4882a593Smuzhiyun 	/* No CPUs should come up or down during this. */
1726*4882a593Smuzhiyun 	cpus_read_lock();
1727*4882a593Smuzhiyun 
1728*4882a593Smuzhiyun 	if (!cpu_online(target_cpu)) {
1729*4882a593Smuzhiyun 		cpus_read_unlock();
1730*4882a593Smuzhiyun 		return -EINVAL;
1731*4882a593Smuzhiyun 	}
1732*4882a593Smuzhiyun 
1733*4882a593Smuzhiyun 	/*
1734*4882a593Smuzhiyun 	 * Synchronizes target_cpu_store() and channel closure:
1735*4882a593Smuzhiyun 	 *
1736*4882a593Smuzhiyun 	 * { Initially: state = CHANNEL_OPENED }
1737*4882a593Smuzhiyun 	 *
1738*4882a593Smuzhiyun 	 * CPU1				CPU2
1739*4882a593Smuzhiyun 	 *
1740*4882a593Smuzhiyun 	 * [target_cpu_store()]		[vmbus_disconnect_ring()]
1741*4882a593Smuzhiyun 	 *
1742*4882a593Smuzhiyun 	 * LOCK channel_mutex		LOCK channel_mutex
1743*4882a593Smuzhiyun 	 * LOAD r1 = state		LOAD r2 = state
1744*4882a593Smuzhiyun 	 * IF (r1 == CHANNEL_OPENED)	IF (r2 == CHANNEL_OPENED)
1745*4882a593Smuzhiyun 	 *   SEND MODIFYCHANNEL		  STORE state = CHANNEL_OPEN
1746*4882a593Smuzhiyun 	 *   [...]			  SEND CLOSECHANNEL
1747*4882a593Smuzhiyun 	 * UNLOCK channel_mutex		UNLOCK channel_mutex
1748*4882a593Smuzhiyun 	 *
1749*4882a593Smuzhiyun 	 * Forbids: r1 == r2 == CHANNEL_OPENED (i.e., CPU1's LOCK precedes
1750*4882a593Smuzhiyun 	 * 		CPU2's LOCK) && CPU2's SEND precedes CPU1's SEND
1751*4882a593Smuzhiyun 	 *
1752*4882a593Smuzhiyun 	 * Note.  The host processes the channel messages "sequentially", in
1753*4882a593Smuzhiyun 	 * the order in which they are received on a per-partition basis.
1754*4882a593Smuzhiyun 	 */
1755*4882a593Smuzhiyun 	mutex_lock(&vmbus_connection.channel_mutex);
1756*4882a593Smuzhiyun 
1757*4882a593Smuzhiyun 	/*
1758*4882a593Smuzhiyun 	 * Hyper-V will ignore MODIFYCHANNEL messages for "non-open" channels;
1759*4882a593Smuzhiyun 	 * avoid sending the message and fail here for such channels.
1760*4882a593Smuzhiyun 	 */
1761*4882a593Smuzhiyun 	if (channel->state != CHANNEL_OPENED_STATE) {
1762*4882a593Smuzhiyun 		ret = -EIO;
1763*4882a593Smuzhiyun 		goto cpu_store_unlock;
1764*4882a593Smuzhiyun 	}
1765*4882a593Smuzhiyun 
1766*4882a593Smuzhiyun 	origin_cpu = channel->target_cpu;
1767*4882a593Smuzhiyun 	if (target_cpu == origin_cpu)
1768*4882a593Smuzhiyun 		goto cpu_store_unlock;
1769*4882a593Smuzhiyun 
1770*4882a593Smuzhiyun 	if (vmbus_send_modifychannel(channel->offermsg.child_relid,
1771*4882a593Smuzhiyun 				     hv_cpu_number_to_vp_number(target_cpu))) {
1772*4882a593Smuzhiyun 		ret = -EIO;
1773*4882a593Smuzhiyun 		goto cpu_store_unlock;
1774*4882a593Smuzhiyun 	}
1775*4882a593Smuzhiyun 
1776*4882a593Smuzhiyun 	/*
1777*4882a593Smuzhiyun 	 * Warning.  At this point, there is *no* guarantee that the host will
1778*4882a593Smuzhiyun 	 * have successfully processed the vmbus_send_modifychannel() request.
1779*4882a593Smuzhiyun 	 * See the header comment of vmbus_send_modifychannel() for more info.
1780*4882a593Smuzhiyun 	 *
1781*4882a593Smuzhiyun 	 * Lags in the processing of the above vmbus_send_modifychannel() can
1782*4882a593Smuzhiyun 	 * result in missed interrupts if the "old" target CPU is taken offline
1783*4882a593Smuzhiyun 	 * before Hyper-V starts sending interrupts to the "new" target CPU.
1784*4882a593Smuzhiyun 	 * But apart from this offlining scenario, the code tolerates such
1785*4882a593Smuzhiyun 	 * lags.  It will function correctly even if a channel interrupt comes
1786*4882a593Smuzhiyun 	 * in on a CPU that is different from the channel target_cpu value.
1787*4882a593Smuzhiyun 	 */
1788*4882a593Smuzhiyun 
1789*4882a593Smuzhiyun 	channel->target_cpu = target_cpu;
1790*4882a593Smuzhiyun 
1791*4882a593Smuzhiyun 	/* See init_vp_index(). */
1792*4882a593Smuzhiyun 	if (hv_is_perf_channel(channel))
1793*4882a593Smuzhiyun 		hv_update_alloced_cpus(origin_cpu, target_cpu);
1794*4882a593Smuzhiyun 
1795*4882a593Smuzhiyun 	/* Currently set only for storvsc channels. */
1796*4882a593Smuzhiyun 	if (channel->change_target_cpu_callback) {
1797*4882a593Smuzhiyun 		(*channel->change_target_cpu_callback)(channel,
1798*4882a593Smuzhiyun 				origin_cpu, target_cpu);
1799*4882a593Smuzhiyun 	}
1800*4882a593Smuzhiyun 
1801*4882a593Smuzhiyun cpu_store_unlock:
1802*4882a593Smuzhiyun 	mutex_unlock(&vmbus_connection.channel_mutex);
1803*4882a593Smuzhiyun 	cpus_read_unlock();
1804*4882a593Smuzhiyun 	return ret;
1805*4882a593Smuzhiyun }
1806*4882a593Smuzhiyun static VMBUS_CHAN_ATTR(cpu, 0644, target_cpu_show, target_cpu_store);
1807*4882a593Smuzhiyun 
channel_pending_show(struct vmbus_channel * channel,char * buf)1808*4882a593Smuzhiyun static ssize_t channel_pending_show(struct vmbus_channel *channel,
1809*4882a593Smuzhiyun 				    char *buf)
1810*4882a593Smuzhiyun {
1811*4882a593Smuzhiyun 	return sprintf(buf, "%d\n",
1812*4882a593Smuzhiyun 		       channel_pending(channel,
1813*4882a593Smuzhiyun 				       vmbus_connection.monitor_pages[1]));
1814*4882a593Smuzhiyun }
1815*4882a593Smuzhiyun static VMBUS_CHAN_ATTR(pending, S_IRUGO, channel_pending_show, NULL);
1816*4882a593Smuzhiyun 
channel_latency_show(struct vmbus_channel * channel,char * buf)1817*4882a593Smuzhiyun static ssize_t channel_latency_show(struct vmbus_channel *channel,
1818*4882a593Smuzhiyun 				    char *buf)
1819*4882a593Smuzhiyun {
1820*4882a593Smuzhiyun 	return sprintf(buf, "%d\n",
1821*4882a593Smuzhiyun 		       channel_latency(channel,
1822*4882a593Smuzhiyun 				       vmbus_connection.monitor_pages[1]));
1823*4882a593Smuzhiyun }
1824*4882a593Smuzhiyun static VMBUS_CHAN_ATTR(latency, S_IRUGO, channel_latency_show, NULL);
1825*4882a593Smuzhiyun 
channel_interrupts_show(struct vmbus_channel * channel,char * buf)1826*4882a593Smuzhiyun static ssize_t channel_interrupts_show(struct vmbus_channel *channel, char *buf)
1827*4882a593Smuzhiyun {
1828*4882a593Smuzhiyun 	return sprintf(buf, "%llu\n", channel->interrupts);
1829*4882a593Smuzhiyun }
1830*4882a593Smuzhiyun static VMBUS_CHAN_ATTR(interrupts, S_IRUGO, channel_interrupts_show, NULL);
1831*4882a593Smuzhiyun 
channel_events_show(struct vmbus_channel * channel,char * buf)1832*4882a593Smuzhiyun static ssize_t channel_events_show(struct vmbus_channel *channel, char *buf)
1833*4882a593Smuzhiyun {
1834*4882a593Smuzhiyun 	return sprintf(buf, "%llu\n", channel->sig_events);
1835*4882a593Smuzhiyun }
1836*4882a593Smuzhiyun static VMBUS_CHAN_ATTR(events, S_IRUGO, channel_events_show, NULL);
1837*4882a593Smuzhiyun 
channel_intr_in_full_show(struct vmbus_channel * channel,char * buf)1838*4882a593Smuzhiyun static ssize_t channel_intr_in_full_show(struct vmbus_channel *channel,
1839*4882a593Smuzhiyun 					 char *buf)
1840*4882a593Smuzhiyun {
1841*4882a593Smuzhiyun 	return sprintf(buf, "%llu\n",
1842*4882a593Smuzhiyun 		       (unsigned long long)channel->intr_in_full);
1843*4882a593Smuzhiyun }
1844*4882a593Smuzhiyun static VMBUS_CHAN_ATTR(intr_in_full, 0444, channel_intr_in_full_show, NULL);
1845*4882a593Smuzhiyun 
channel_intr_out_empty_show(struct vmbus_channel * channel,char * buf)1846*4882a593Smuzhiyun static ssize_t channel_intr_out_empty_show(struct vmbus_channel *channel,
1847*4882a593Smuzhiyun 					   char *buf)
1848*4882a593Smuzhiyun {
1849*4882a593Smuzhiyun 	return sprintf(buf, "%llu\n",
1850*4882a593Smuzhiyun 		       (unsigned long long)channel->intr_out_empty);
1851*4882a593Smuzhiyun }
1852*4882a593Smuzhiyun static VMBUS_CHAN_ATTR(intr_out_empty, 0444, channel_intr_out_empty_show, NULL);
1853*4882a593Smuzhiyun 
channel_out_full_first_show(struct vmbus_channel * channel,char * buf)1854*4882a593Smuzhiyun static ssize_t channel_out_full_first_show(struct vmbus_channel *channel,
1855*4882a593Smuzhiyun 					   char *buf)
1856*4882a593Smuzhiyun {
1857*4882a593Smuzhiyun 	return sprintf(buf, "%llu\n",
1858*4882a593Smuzhiyun 		       (unsigned long long)channel->out_full_first);
1859*4882a593Smuzhiyun }
1860*4882a593Smuzhiyun static VMBUS_CHAN_ATTR(out_full_first, 0444, channel_out_full_first_show, NULL);
1861*4882a593Smuzhiyun 
channel_out_full_total_show(struct vmbus_channel * channel,char * buf)1862*4882a593Smuzhiyun static ssize_t channel_out_full_total_show(struct vmbus_channel *channel,
1863*4882a593Smuzhiyun 					   char *buf)
1864*4882a593Smuzhiyun {
1865*4882a593Smuzhiyun 	return sprintf(buf, "%llu\n",
1866*4882a593Smuzhiyun 		       (unsigned long long)channel->out_full_total);
1867*4882a593Smuzhiyun }
1868*4882a593Smuzhiyun static VMBUS_CHAN_ATTR(out_full_total, 0444, channel_out_full_total_show, NULL);
1869*4882a593Smuzhiyun 
subchannel_monitor_id_show(struct vmbus_channel * channel,char * buf)1870*4882a593Smuzhiyun static ssize_t subchannel_monitor_id_show(struct vmbus_channel *channel,
1871*4882a593Smuzhiyun 					  char *buf)
1872*4882a593Smuzhiyun {
1873*4882a593Smuzhiyun 	return sprintf(buf, "%u\n", channel->offermsg.monitorid);
1874*4882a593Smuzhiyun }
1875*4882a593Smuzhiyun static VMBUS_CHAN_ATTR(monitor_id, S_IRUGO, subchannel_monitor_id_show, NULL);
1876*4882a593Smuzhiyun 
subchannel_id_show(struct vmbus_channel * channel,char * buf)1877*4882a593Smuzhiyun static ssize_t subchannel_id_show(struct vmbus_channel *channel,
1878*4882a593Smuzhiyun 				  char *buf)
1879*4882a593Smuzhiyun {
1880*4882a593Smuzhiyun 	return sprintf(buf, "%u\n",
1881*4882a593Smuzhiyun 		       channel->offermsg.offer.sub_channel_index);
1882*4882a593Smuzhiyun }
1883*4882a593Smuzhiyun static VMBUS_CHAN_ATTR_RO(subchannel_id);
1884*4882a593Smuzhiyun 
1885*4882a593Smuzhiyun static struct attribute *vmbus_chan_attrs[] = {
1886*4882a593Smuzhiyun 	&chan_attr_out_mask.attr,
1887*4882a593Smuzhiyun 	&chan_attr_in_mask.attr,
1888*4882a593Smuzhiyun 	&chan_attr_read_avail.attr,
1889*4882a593Smuzhiyun 	&chan_attr_write_avail.attr,
1890*4882a593Smuzhiyun 	&chan_attr_cpu.attr,
1891*4882a593Smuzhiyun 	&chan_attr_pending.attr,
1892*4882a593Smuzhiyun 	&chan_attr_latency.attr,
1893*4882a593Smuzhiyun 	&chan_attr_interrupts.attr,
1894*4882a593Smuzhiyun 	&chan_attr_events.attr,
1895*4882a593Smuzhiyun 	&chan_attr_intr_in_full.attr,
1896*4882a593Smuzhiyun 	&chan_attr_intr_out_empty.attr,
1897*4882a593Smuzhiyun 	&chan_attr_out_full_first.attr,
1898*4882a593Smuzhiyun 	&chan_attr_out_full_total.attr,
1899*4882a593Smuzhiyun 	&chan_attr_monitor_id.attr,
1900*4882a593Smuzhiyun 	&chan_attr_subchannel_id.attr,
1901*4882a593Smuzhiyun 	NULL
1902*4882a593Smuzhiyun };
1903*4882a593Smuzhiyun 
1904*4882a593Smuzhiyun /*
1905*4882a593Smuzhiyun  * Channel-level attribute_group callback function. Returns the permission for
1906*4882a593Smuzhiyun  * each attribute, and returns 0 if an attribute is not visible.
1907*4882a593Smuzhiyun  */
vmbus_chan_attr_is_visible(struct kobject * kobj,struct attribute * attr,int idx)1908*4882a593Smuzhiyun static umode_t vmbus_chan_attr_is_visible(struct kobject *kobj,
1909*4882a593Smuzhiyun 					  struct attribute *attr, int idx)
1910*4882a593Smuzhiyun {
1911*4882a593Smuzhiyun 	const struct vmbus_channel *channel =
1912*4882a593Smuzhiyun 		container_of(kobj, struct vmbus_channel, kobj);
1913*4882a593Smuzhiyun 
1914*4882a593Smuzhiyun 	/* Hide the monitor attributes if the monitor mechanism is not used. */
1915*4882a593Smuzhiyun 	if (!channel->offermsg.monitor_allocated &&
1916*4882a593Smuzhiyun 	    (attr == &chan_attr_pending.attr ||
1917*4882a593Smuzhiyun 	     attr == &chan_attr_latency.attr ||
1918*4882a593Smuzhiyun 	     attr == &chan_attr_monitor_id.attr))
1919*4882a593Smuzhiyun 		return 0;
1920*4882a593Smuzhiyun 
1921*4882a593Smuzhiyun 	return attr->mode;
1922*4882a593Smuzhiyun }
1923*4882a593Smuzhiyun 
1924*4882a593Smuzhiyun static struct attribute_group vmbus_chan_group = {
1925*4882a593Smuzhiyun 	.attrs = vmbus_chan_attrs,
1926*4882a593Smuzhiyun 	.is_visible = vmbus_chan_attr_is_visible
1927*4882a593Smuzhiyun };
1928*4882a593Smuzhiyun 
1929*4882a593Smuzhiyun static struct kobj_type vmbus_chan_ktype = {
1930*4882a593Smuzhiyun 	.sysfs_ops = &vmbus_chan_sysfs_ops,
1931*4882a593Smuzhiyun 	.release = vmbus_chan_release,
1932*4882a593Smuzhiyun };
1933*4882a593Smuzhiyun 
1934*4882a593Smuzhiyun /*
1935*4882a593Smuzhiyun  * vmbus_add_channel_kobj - setup a sub-directory under device/channels
1936*4882a593Smuzhiyun  */
vmbus_add_channel_kobj(struct hv_device * dev,struct vmbus_channel * channel)1937*4882a593Smuzhiyun int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
1938*4882a593Smuzhiyun {
1939*4882a593Smuzhiyun 	const struct device *device = &dev->device;
1940*4882a593Smuzhiyun 	struct kobject *kobj = &channel->kobj;
1941*4882a593Smuzhiyun 	u32 relid = channel->offermsg.child_relid;
1942*4882a593Smuzhiyun 	int ret;
1943*4882a593Smuzhiyun 
1944*4882a593Smuzhiyun 	kobj->kset = dev->channels_kset;
1945*4882a593Smuzhiyun 	ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
1946*4882a593Smuzhiyun 				   "%u", relid);
1947*4882a593Smuzhiyun 	if (ret) {
1948*4882a593Smuzhiyun 		kobject_put(kobj);
1949*4882a593Smuzhiyun 		return ret;
1950*4882a593Smuzhiyun 	}
1951*4882a593Smuzhiyun 
1952*4882a593Smuzhiyun 	ret = sysfs_create_group(kobj, &vmbus_chan_group);
1953*4882a593Smuzhiyun 
1954*4882a593Smuzhiyun 	if (ret) {
1955*4882a593Smuzhiyun 		/*
1956*4882a593Smuzhiyun 		 * The calling functions' error handling paths will cleanup the
1957*4882a593Smuzhiyun 		 * empty channel directory.
1958*4882a593Smuzhiyun 		 */
1959*4882a593Smuzhiyun 		kobject_put(kobj);
1960*4882a593Smuzhiyun 		dev_err(device, "Unable to set up channel sysfs files\n");
1961*4882a593Smuzhiyun 		return ret;
1962*4882a593Smuzhiyun 	}
1963*4882a593Smuzhiyun 
1964*4882a593Smuzhiyun 	kobject_uevent(kobj, KOBJ_ADD);
1965*4882a593Smuzhiyun 
1966*4882a593Smuzhiyun 	return 0;
1967*4882a593Smuzhiyun }
1968*4882a593Smuzhiyun 
1969*4882a593Smuzhiyun /*
1970*4882a593Smuzhiyun  * vmbus_remove_channel_attr_group - remove the channel's attribute group
1971*4882a593Smuzhiyun  */
vmbus_remove_channel_attr_group(struct vmbus_channel * channel)1972*4882a593Smuzhiyun void vmbus_remove_channel_attr_group(struct vmbus_channel *channel)
1973*4882a593Smuzhiyun {
1974*4882a593Smuzhiyun 	sysfs_remove_group(&channel->kobj, &vmbus_chan_group);
1975*4882a593Smuzhiyun }
1976*4882a593Smuzhiyun 
1977*4882a593Smuzhiyun /*
1978*4882a593Smuzhiyun  * vmbus_device_create - Creates and registers a new child device
1979*4882a593Smuzhiyun  * on the vmbus.
1980*4882a593Smuzhiyun  */
vmbus_device_create(const guid_t * type,const guid_t * instance,struct vmbus_channel * channel)1981*4882a593Smuzhiyun struct hv_device *vmbus_device_create(const guid_t *type,
1982*4882a593Smuzhiyun 				      const guid_t *instance,
1983*4882a593Smuzhiyun 				      struct vmbus_channel *channel)
1984*4882a593Smuzhiyun {
1985*4882a593Smuzhiyun 	struct hv_device *child_device_obj;
1986*4882a593Smuzhiyun 
1987*4882a593Smuzhiyun 	child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
1988*4882a593Smuzhiyun 	if (!child_device_obj) {
1989*4882a593Smuzhiyun 		pr_err("Unable to allocate device object for child device\n");
1990*4882a593Smuzhiyun 		return NULL;
1991*4882a593Smuzhiyun 	}
1992*4882a593Smuzhiyun 
1993*4882a593Smuzhiyun 	child_device_obj->channel = channel;
1994*4882a593Smuzhiyun 	guid_copy(&child_device_obj->dev_type, type);
1995*4882a593Smuzhiyun 	guid_copy(&child_device_obj->dev_instance, instance);
1996*4882a593Smuzhiyun 	child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */
1997*4882a593Smuzhiyun 
1998*4882a593Smuzhiyun 	return child_device_obj;
1999*4882a593Smuzhiyun }
2000*4882a593Smuzhiyun 
2001*4882a593Smuzhiyun /*
2002*4882a593Smuzhiyun  * vmbus_device_register - Register the child device
2003*4882a593Smuzhiyun  */
vmbus_device_register(struct hv_device * child_device_obj)2004*4882a593Smuzhiyun int vmbus_device_register(struct hv_device *child_device_obj)
2005*4882a593Smuzhiyun {
2006*4882a593Smuzhiyun 	struct kobject *kobj = &child_device_obj->device.kobj;
2007*4882a593Smuzhiyun 	int ret;
2008*4882a593Smuzhiyun 
2009*4882a593Smuzhiyun 	dev_set_name(&child_device_obj->device, "%pUl",
2010*4882a593Smuzhiyun 		     &child_device_obj->channel->offermsg.offer.if_instance);
2011*4882a593Smuzhiyun 
2012*4882a593Smuzhiyun 	child_device_obj->device.bus = &hv_bus;
2013*4882a593Smuzhiyun 	child_device_obj->device.parent = &hv_acpi_dev->dev;
2014*4882a593Smuzhiyun 	child_device_obj->device.release = vmbus_device_release;
2015*4882a593Smuzhiyun 
2016*4882a593Smuzhiyun 	/*
2017*4882a593Smuzhiyun 	 * Register with the LDM. This will kick off the driver/device
2018*4882a593Smuzhiyun 	 * binding...which will eventually call vmbus_match() and vmbus_probe()
2019*4882a593Smuzhiyun 	 */
2020*4882a593Smuzhiyun 	ret = device_register(&child_device_obj->device);
2021*4882a593Smuzhiyun 	if (ret) {
2022*4882a593Smuzhiyun 		pr_err("Unable to register child device\n");
2023*4882a593Smuzhiyun 		put_device(&child_device_obj->device);
2024*4882a593Smuzhiyun 		return ret;
2025*4882a593Smuzhiyun 	}
2026*4882a593Smuzhiyun 
2027*4882a593Smuzhiyun 	child_device_obj->channels_kset = kset_create_and_add("channels",
2028*4882a593Smuzhiyun 							      NULL, kobj);
2029*4882a593Smuzhiyun 	if (!child_device_obj->channels_kset) {
2030*4882a593Smuzhiyun 		ret = -ENOMEM;
2031*4882a593Smuzhiyun 		goto err_dev_unregister;
2032*4882a593Smuzhiyun 	}
2033*4882a593Smuzhiyun 
2034*4882a593Smuzhiyun 	ret = vmbus_add_channel_kobj(child_device_obj,
2035*4882a593Smuzhiyun 				     child_device_obj->channel);
2036*4882a593Smuzhiyun 	if (ret) {
2037*4882a593Smuzhiyun 		pr_err("Unable to register primary channeln");
2038*4882a593Smuzhiyun 		goto err_kset_unregister;
2039*4882a593Smuzhiyun 	}
2040*4882a593Smuzhiyun 	hv_debug_add_dev_dir(child_device_obj);
2041*4882a593Smuzhiyun 
2042*4882a593Smuzhiyun 	return 0;
2043*4882a593Smuzhiyun 
2044*4882a593Smuzhiyun err_kset_unregister:
2045*4882a593Smuzhiyun 	kset_unregister(child_device_obj->channels_kset);
2046*4882a593Smuzhiyun 
2047*4882a593Smuzhiyun err_dev_unregister:
2048*4882a593Smuzhiyun 	device_unregister(&child_device_obj->device);
2049*4882a593Smuzhiyun 	return ret;
2050*4882a593Smuzhiyun }
2051*4882a593Smuzhiyun 
2052*4882a593Smuzhiyun /*
2053*4882a593Smuzhiyun  * vmbus_device_unregister - Remove the specified child device
2054*4882a593Smuzhiyun  * from the vmbus.
2055*4882a593Smuzhiyun  */
vmbus_device_unregister(struct hv_device * device_obj)2056*4882a593Smuzhiyun void vmbus_device_unregister(struct hv_device *device_obj)
2057*4882a593Smuzhiyun {
2058*4882a593Smuzhiyun 	pr_debug("child device %s unregistered\n",
2059*4882a593Smuzhiyun 		dev_name(&device_obj->device));
2060*4882a593Smuzhiyun 
2061*4882a593Smuzhiyun 	kset_unregister(device_obj->channels_kset);
2062*4882a593Smuzhiyun 
2063*4882a593Smuzhiyun 	/*
2064*4882a593Smuzhiyun 	 * Kick off the process of unregistering the device.
2065*4882a593Smuzhiyun 	 * This will call vmbus_remove() and eventually vmbus_device_release()
2066*4882a593Smuzhiyun 	 */
2067*4882a593Smuzhiyun 	device_unregister(&device_obj->device);
2068*4882a593Smuzhiyun }
2069*4882a593Smuzhiyun 
2070*4882a593Smuzhiyun 
2071*4882a593Smuzhiyun /*
2072*4882a593Smuzhiyun  * VMBUS is an acpi enumerated device. Get the information we
2073*4882a593Smuzhiyun  * need from DSDT.
2074*4882a593Smuzhiyun  */
2075*4882a593Smuzhiyun #define VTPM_BASE_ADDRESS 0xfed40000
vmbus_walk_resources(struct acpi_resource * res,void * ctx)2076*4882a593Smuzhiyun static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
2077*4882a593Smuzhiyun {
2078*4882a593Smuzhiyun 	resource_size_t start = 0;
2079*4882a593Smuzhiyun 	resource_size_t end = 0;
2080*4882a593Smuzhiyun 	struct resource *new_res;
2081*4882a593Smuzhiyun 	struct resource **old_res = &hyperv_mmio;
2082*4882a593Smuzhiyun 	struct resource **prev_res = NULL;
2083*4882a593Smuzhiyun 	struct resource r;
2084*4882a593Smuzhiyun 
2085*4882a593Smuzhiyun 	switch (res->type) {
2086*4882a593Smuzhiyun 
2087*4882a593Smuzhiyun 	/*
2088*4882a593Smuzhiyun 	 * "Address" descriptors are for bus windows. Ignore
2089*4882a593Smuzhiyun 	 * "memory" descriptors, which are for registers on
2090*4882a593Smuzhiyun 	 * devices.
2091*4882a593Smuzhiyun 	 */
2092*4882a593Smuzhiyun 	case ACPI_RESOURCE_TYPE_ADDRESS32:
2093*4882a593Smuzhiyun 		start = res->data.address32.address.minimum;
2094*4882a593Smuzhiyun 		end = res->data.address32.address.maximum;
2095*4882a593Smuzhiyun 		break;
2096*4882a593Smuzhiyun 
2097*4882a593Smuzhiyun 	case ACPI_RESOURCE_TYPE_ADDRESS64:
2098*4882a593Smuzhiyun 		start = res->data.address64.address.minimum;
2099*4882a593Smuzhiyun 		end = res->data.address64.address.maximum;
2100*4882a593Smuzhiyun 		break;
2101*4882a593Smuzhiyun 
2102*4882a593Smuzhiyun 	/*
2103*4882a593Smuzhiyun 	 * The IRQ information is needed only on ARM64, which Hyper-V
2104*4882a593Smuzhiyun 	 * sets up in the extended format. IRQ information is present
2105*4882a593Smuzhiyun 	 * on x86/x64 in the non-extended format but it is not used by
2106*4882a593Smuzhiyun 	 * Linux. So don't bother checking for the non-extended format.
2107*4882a593Smuzhiyun 	 */
2108*4882a593Smuzhiyun 	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
2109*4882a593Smuzhiyun 		if (!acpi_dev_resource_interrupt(res, 0, &r)) {
2110*4882a593Smuzhiyun 			pr_err("Unable to parse Hyper-V ACPI interrupt\n");
2111*4882a593Smuzhiyun 			return AE_ERROR;
2112*4882a593Smuzhiyun 		}
2113*4882a593Smuzhiyun 		/* ARM64 INTID for VMbus */
2114*4882a593Smuzhiyun 		vmbus_interrupt = res->data.extended_irq.interrupts[0];
2115*4882a593Smuzhiyun 		/* Linux IRQ number */
2116*4882a593Smuzhiyun 		vmbus_irq = r.start;
2117*4882a593Smuzhiyun 		return AE_OK;
2118*4882a593Smuzhiyun 
2119*4882a593Smuzhiyun 	default:
2120*4882a593Smuzhiyun 		/* Unused resource type */
2121*4882a593Smuzhiyun 		return AE_OK;
2122*4882a593Smuzhiyun 
2123*4882a593Smuzhiyun 	}
2124*4882a593Smuzhiyun 	/*
2125*4882a593Smuzhiyun 	 * Ignore ranges that are below 1MB, as they're not
2126*4882a593Smuzhiyun 	 * necessary or useful here.
2127*4882a593Smuzhiyun 	 */
2128*4882a593Smuzhiyun 	if (end < 0x100000)
2129*4882a593Smuzhiyun 		return AE_OK;
2130*4882a593Smuzhiyun 
2131*4882a593Smuzhiyun 	new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
2132*4882a593Smuzhiyun 	if (!new_res)
2133*4882a593Smuzhiyun 		return AE_NO_MEMORY;
2134*4882a593Smuzhiyun 
2135*4882a593Smuzhiyun 	/* If this range overlaps the virtual TPM, truncate it. */
2136*4882a593Smuzhiyun 	if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
2137*4882a593Smuzhiyun 		end = VTPM_BASE_ADDRESS;
2138*4882a593Smuzhiyun 
2139*4882a593Smuzhiyun 	new_res->name = "hyperv mmio";
2140*4882a593Smuzhiyun 	new_res->flags = IORESOURCE_MEM;
2141*4882a593Smuzhiyun 	new_res->start = start;
2142*4882a593Smuzhiyun 	new_res->end = end;
2143*4882a593Smuzhiyun 
2144*4882a593Smuzhiyun 	/*
2145*4882a593Smuzhiyun 	 * If two ranges are adjacent, merge them.
2146*4882a593Smuzhiyun 	 */
2147*4882a593Smuzhiyun 	do {
2148*4882a593Smuzhiyun 		if (!*old_res) {
2149*4882a593Smuzhiyun 			*old_res = new_res;
2150*4882a593Smuzhiyun 			break;
2151*4882a593Smuzhiyun 		}
2152*4882a593Smuzhiyun 
2153*4882a593Smuzhiyun 		if (((*old_res)->end + 1) == new_res->start) {
2154*4882a593Smuzhiyun 			(*old_res)->end = new_res->end;
2155*4882a593Smuzhiyun 			kfree(new_res);
2156*4882a593Smuzhiyun 			break;
2157*4882a593Smuzhiyun 		}
2158*4882a593Smuzhiyun 
2159*4882a593Smuzhiyun 		if ((*old_res)->start == new_res->end + 1) {
2160*4882a593Smuzhiyun 			(*old_res)->start = new_res->start;
2161*4882a593Smuzhiyun 			kfree(new_res);
2162*4882a593Smuzhiyun 			break;
2163*4882a593Smuzhiyun 		}
2164*4882a593Smuzhiyun 
2165*4882a593Smuzhiyun 		if ((*old_res)->start > new_res->end) {
2166*4882a593Smuzhiyun 			new_res->sibling = *old_res;
2167*4882a593Smuzhiyun 			if (prev_res)
2168*4882a593Smuzhiyun 				(*prev_res)->sibling = new_res;
2169*4882a593Smuzhiyun 			*old_res = new_res;
2170*4882a593Smuzhiyun 			break;
2171*4882a593Smuzhiyun 		}
2172*4882a593Smuzhiyun 
2173*4882a593Smuzhiyun 		prev_res = old_res;
2174*4882a593Smuzhiyun 		old_res = &(*old_res)->sibling;
2175*4882a593Smuzhiyun 
2176*4882a593Smuzhiyun 	} while (1);
2177*4882a593Smuzhiyun 
2178*4882a593Smuzhiyun 	return AE_OK;
2179*4882a593Smuzhiyun }
2180*4882a593Smuzhiyun 
vmbus_acpi_remove(struct acpi_device * device)2181*4882a593Smuzhiyun static int vmbus_acpi_remove(struct acpi_device *device)
2182*4882a593Smuzhiyun {
2183*4882a593Smuzhiyun 	struct resource *cur_res;
2184*4882a593Smuzhiyun 	struct resource *next_res;
2185*4882a593Smuzhiyun 
2186*4882a593Smuzhiyun 	if (hyperv_mmio) {
2187*4882a593Smuzhiyun 		if (fb_mmio) {
2188*4882a593Smuzhiyun 			__release_region(hyperv_mmio, fb_mmio->start,
2189*4882a593Smuzhiyun 					 resource_size(fb_mmio));
2190*4882a593Smuzhiyun 			fb_mmio = NULL;
2191*4882a593Smuzhiyun 		}
2192*4882a593Smuzhiyun 
2193*4882a593Smuzhiyun 		for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
2194*4882a593Smuzhiyun 			next_res = cur_res->sibling;
2195*4882a593Smuzhiyun 			kfree(cur_res);
2196*4882a593Smuzhiyun 		}
2197*4882a593Smuzhiyun 	}
2198*4882a593Smuzhiyun 
2199*4882a593Smuzhiyun 	return 0;
2200*4882a593Smuzhiyun }
2201*4882a593Smuzhiyun 
vmbus_reserve_fb(void)2202*4882a593Smuzhiyun static void vmbus_reserve_fb(void)
2203*4882a593Smuzhiyun {
2204*4882a593Smuzhiyun 	int size;
2205*4882a593Smuzhiyun 	/*
2206*4882a593Smuzhiyun 	 * Make a claim for the frame buffer in the resource tree under the
2207*4882a593Smuzhiyun 	 * first node, which will be the one below 4GB.  The length seems to
2208*4882a593Smuzhiyun 	 * be underreported, particularly in a Generation 1 VM.  So start out
2209*4882a593Smuzhiyun 	 * reserving a larger area and make it smaller until it succeeds.
2210*4882a593Smuzhiyun 	 */
2211*4882a593Smuzhiyun 
2212*4882a593Smuzhiyun 	if (screen_info.lfb_base) {
2213*4882a593Smuzhiyun 		if (efi_enabled(EFI_BOOT))
2214*4882a593Smuzhiyun 			size = max_t(__u32, screen_info.lfb_size, 0x800000);
2215*4882a593Smuzhiyun 		else
2216*4882a593Smuzhiyun 			size = max_t(__u32, screen_info.lfb_size, 0x4000000);
2217*4882a593Smuzhiyun 
2218*4882a593Smuzhiyun 		for (; !fb_mmio && (size >= 0x100000); size >>= 1) {
2219*4882a593Smuzhiyun 			fb_mmio = __request_region(hyperv_mmio,
2220*4882a593Smuzhiyun 						   screen_info.lfb_base, size,
2221*4882a593Smuzhiyun 						   fb_mmio_name, 0);
2222*4882a593Smuzhiyun 		}
2223*4882a593Smuzhiyun 	}
2224*4882a593Smuzhiyun }
2225*4882a593Smuzhiyun 
2226*4882a593Smuzhiyun /**
2227*4882a593Smuzhiyun  * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
2228*4882a593Smuzhiyun  * @new:		If successful, supplied a pointer to the
2229*4882a593Smuzhiyun  *			allocated MMIO space.
2230*4882a593Smuzhiyun  * @device_obj:		Identifies the caller
2231*4882a593Smuzhiyun  * @min:		Minimum guest physical address of the
2232*4882a593Smuzhiyun  *			allocation
2233*4882a593Smuzhiyun  * @max:		Maximum guest physical address
2234*4882a593Smuzhiyun  * @size:		Size of the range to be allocated
2235*4882a593Smuzhiyun  * @align:		Alignment of the range to be allocated
2236*4882a593Smuzhiyun  * @fb_overlap_ok:	Whether this allocation can be allowed
2237*4882a593Smuzhiyun  *			to overlap the video frame buffer.
2238*4882a593Smuzhiyun  *
2239*4882a593Smuzhiyun  * This function walks the resources granted to VMBus by the
2240*4882a593Smuzhiyun  * _CRS object in the ACPI namespace underneath the parent
2241*4882a593Smuzhiyun  * "bridge" whether that's a root PCI bus in the Generation 1
2242*4882a593Smuzhiyun  * case or a Module Device in the Generation 2 case.  It then
2243*4882a593Smuzhiyun  * attempts to allocate from the global MMIO pool in a way that
2244*4882a593Smuzhiyun  * matches the constraints supplied in these parameters and by
2245*4882a593Smuzhiyun  * that _CRS.
2246*4882a593Smuzhiyun  *
2247*4882a593Smuzhiyun  * Return: 0 on success, -errno on failure
2248*4882a593Smuzhiyun  */
vmbus_allocate_mmio(struct resource ** new,struct hv_device * device_obj,resource_size_t min,resource_size_t max,resource_size_t size,resource_size_t align,bool fb_overlap_ok)2249*4882a593Smuzhiyun int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
2250*4882a593Smuzhiyun 			resource_size_t min, resource_size_t max,
2251*4882a593Smuzhiyun 			resource_size_t size, resource_size_t align,
2252*4882a593Smuzhiyun 			bool fb_overlap_ok)
2253*4882a593Smuzhiyun {
2254*4882a593Smuzhiyun 	struct resource *iter, *shadow;
2255*4882a593Smuzhiyun 	resource_size_t range_min, range_max, start, end;
2256*4882a593Smuzhiyun 	const char *dev_n = dev_name(&device_obj->device);
2257*4882a593Smuzhiyun 	int retval;
2258*4882a593Smuzhiyun 
2259*4882a593Smuzhiyun 	retval = -ENXIO;
2260*4882a593Smuzhiyun 	mutex_lock(&hyperv_mmio_lock);
2261*4882a593Smuzhiyun 
2262*4882a593Smuzhiyun 	/*
2263*4882a593Smuzhiyun 	 * If overlaps with frame buffers are allowed, then first attempt to
2264*4882a593Smuzhiyun 	 * make the allocation from within the reserved region.  Because it
2265*4882a593Smuzhiyun 	 * is already reserved, no shadow allocation is necessary.
2266*4882a593Smuzhiyun 	 */
2267*4882a593Smuzhiyun 	if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
2268*4882a593Smuzhiyun 	    !(max < fb_mmio->start)) {
2269*4882a593Smuzhiyun 
2270*4882a593Smuzhiyun 		range_min = fb_mmio->start;
2271*4882a593Smuzhiyun 		range_max = fb_mmio->end;
2272*4882a593Smuzhiyun 		start = (range_min + align - 1) & ~(align - 1);
2273*4882a593Smuzhiyun 		for (; start + size - 1 <= range_max; start += align) {
2274*4882a593Smuzhiyun 			*new = request_mem_region_exclusive(start, size, dev_n);
2275*4882a593Smuzhiyun 			if (*new) {
2276*4882a593Smuzhiyun 				retval = 0;
2277*4882a593Smuzhiyun 				goto exit;
2278*4882a593Smuzhiyun 			}
2279*4882a593Smuzhiyun 		}
2280*4882a593Smuzhiyun 	}
2281*4882a593Smuzhiyun 
2282*4882a593Smuzhiyun 	for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2283*4882a593Smuzhiyun 		if ((iter->start >= max) || (iter->end <= min))
2284*4882a593Smuzhiyun 			continue;
2285*4882a593Smuzhiyun 
2286*4882a593Smuzhiyun 		range_min = iter->start;
2287*4882a593Smuzhiyun 		range_max = iter->end;
2288*4882a593Smuzhiyun 		start = (range_min + align - 1) & ~(align - 1);
2289*4882a593Smuzhiyun 		for (; start + size - 1 <= range_max; start += align) {
2290*4882a593Smuzhiyun 			end = start + size - 1;
2291*4882a593Smuzhiyun 
2292*4882a593Smuzhiyun 			/* Skip the whole fb_mmio region if not fb_overlap_ok */
2293*4882a593Smuzhiyun 			if (!fb_overlap_ok && fb_mmio &&
2294*4882a593Smuzhiyun 			    (((start >= fb_mmio->start) && (start <= fb_mmio->end)) ||
2295*4882a593Smuzhiyun 			     ((end >= fb_mmio->start) && (end <= fb_mmio->end))))
2296*4882a593Smuzhiyun 				continue;
2297*4882a593Smuzhiyun 
2298*4882a593Smuzhiyun 			shadow = __request_region(iter, start, size, NULL,
2299*4882a593Smuzhiyun 						  IORESOURCE_BUSY);
2300*4882a593Smuzhiyun 			if (!shadow)
2301*4882a593Smuzhiyun 				continue;
2302*4882a593Smuzhiyun 
2303*4882a593Smuzhiyun 			*new = request_mem_region_exclusive(start, size, dev_n);
2304*4882a593Smuzhiyun 			if (*new) {
2305*4882a593Smuzhiyun 				shadow->name = (char *)*new;
2306*4882a593Smuzhiyun 				retval = 0;
2307*4882a593Smuzhiyun 				goto exit;
2308*4882a593Smuzhiyun 			}
2309*4882a593Smuzhiyun 
2310*4882a593Smuzhiyun 			__release_region(iter, start, size);
2311*4882a593Smuzhiyun 		}
2312*4882a593Smuzhiyun 	}
2313*4882a593Smuzhiyun 
2314*4882a593Smuzhiyun exit:
2315*4882a593Smuzhiyun 	mutex_unlock(&hyperv_mmio_lock);
2316*4882a593Smuzhiyun 	return retval;
2317*4882a593Smuzhiyun }
2318*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
2319*4882a593Smuzhiyun 
2320*4882a593Smuzhiyun /**
2321*4882a593Smuzhiyun  * vmbus_free_mmio() - Free a memory-mapped I/O range.
2322*4882a593Smuzhiyun  * @start:		Base address of region to release.
2323*4882a593Smuzhiyun  * @size:		Size of the range to be allocated
2324*4882a593Smuzhiyun  *
2325*4882a593Smuzhiyun  * This function releases anything requested by
2326*4882a593Smuzhiyun  * vmbus_mmio_allocate().
2327*4882a593Smuzhiyun  */
vmbus_free_mmio(resource_size_t start,resource_size_t size)2328*4882a593Smuzhiyun void vmbus_free_mmio(resource_size_t start, resource_size_t size)
2329*4882a593Smuzhiyun {
2330*4882a593Smuzhiyun 	struct resource *iter;
2331*4882a593Smuzhiyun 
2332*4882a593Smuzhiyun 	mutex_lock(&hyperv_mmio_lock);
2333*4882a593Smuzhiyun 	for (iter = hyperv_mmio; iter; iter = iter->sibling) {
2334*4882a593Smuzhiyun 		if ((iter->start >= start + size) || (iter->end <= start))
2335*4882a593Smuzhiyun 			continue;
2336*4882a593Smuzhiyun 
2337*4882a593Smuzhiyun 		__release_region(iter, start, size);
2338*4882a593Smuzhiyun 	}
2339*4882a593Smuzhiyun 	release_mem_region(start, size);
2340*4882a593Smuzhiyun 	mutex_unlock(&hyperv_mmio_lock);
2341*4882a593Smuzhiyun 
2342*4882a593Smuzhiyun }
2343*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(vmbus_free_mmio);
2344*4882a593Smuzhiyun 
vmbus_acpi_add(struct acpi_device * device)2345*4882a593Smuzhiyun static int vmbus_acpi_add(struct acpi_device *device)
2346*4882a593Smuzhiyun {
2347*4882a593Smuzhiyun 	acpi_status result;
2348*4882a593Smuzhiyun 	int ret_val = -ENODEV;
2349*4882a593Smuzhiyun 	struct acpi_device *ancestor;
2350*4882a593Smuzhiyun 
2351*4882a593Smuzhiyun 	hv_acpi_dev = device;
2352*4882a593Smuzhiyun 
2353*4882a593Smuzhiyun 	result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
2354*4882a593Smuzhiyun 					vmbus_walk_resources, NULL);
2355*4882a593Smuzhiyun 
2356*4882a593Smuzhiyun 	if (ACPI_FAILURE(result))
2357*4882a593Smuzhiyun 		goto acpi_walk_err;
2358*4882a593Smuzhiyun 	/*
2359*4882a593Smuzhiyun 	 * Some ancestor of the vmbus acpi device (Gen1 or Gen2
2360*4882a593Smuzhiyun 	 * firmware) is the VMOD that has the mmio ranges. Get that.
2361*4882a593Smuzhiyun 	 */
2362*4882a593Smuzhiyun 	for (ancestor = device->parent; ancestor; ancestor = ancestor->parent) {
2363*4882a593Smuzhiyun 		result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
2364*4882a593Smuzhiyun 					     vmbus_walk_resources, NULL);
2365*4882a593Smuzhiyun 
2366*4882a593Smuzhiyun 		if (ACPI_FAILURE(result))
2367*4882a593Smuzhiyun 			continue;
2368*4882a593Smuzhiyun 		if (hyperv_mmio) {
2369*4882a593Smuzhiyun 			vmbus_reserve_fb();
2370*4882a593Smuzhiyun 			break;
2371*4882a593Smuzhiyun 		}
2372*4882a593Smuzhiyun 	}
2373*4882a593Smuzhiyun 	ret_val = 0;
2374*4882a593Smuzhiyun 
2375*4882a593Smuzhiyun acpi_walk_err:
2376*4882a593Smuzhiyun 	complete(&probe_event);
2377*4882a593Smuzhiyun 	if (ret_val)
2378*4882a593Smuzhiyun 		vmbus_acpi_remove(device);
2379*4882a593Smuzhiyun 	return ret_val;
2380*4882a593Smuzhiyun }
2381*4882a593Smuzhiyun 
2382*4882a593Smuzhiyun #ifdef CONFIG_PM_SLEEP
vmbus_bus_suspend(struct device * dev)2383*4882a593Smuzhiyun static int vmbus_bus_suspend(struct device *dev)
2384*4882a593Smuzhiyun {
2385*4882a593Smuzhiyun 	struct vmbus_channel *channel, *sc;
2386*4882a593Smuzhiyun 
2387*4882a593Smuzhiyun 	while (atomic_read(&vmbus_connection.offer_in_progress) != 0) {
2388*4882a593Smuzhiyun 		/*
2389*4882a593Smuzhiyun 		 * We wait here until the completion of any channel
2390*4882a593Smuzhiyun 		 * offers that are currently in progress.
2391*4882a593Smuzhiyun 		 */
2392*4882a593Smuzhiyun 		msleep(1);
2393*4882a593Smuzhiyun 	}
2394*4882a593Smuzhiyun 
2395*4882a593Smuzhiyun 	mutex_lock(&vmbus_connection.channel_mutex);
2396*4882a593Smuzhiyun 	list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2397*4882a593Smuzhiyun 		if (!is_hvsock_channel(channel))
2398*4882a593Smuzhiyun 			continue;
2399*4882a593Smuzhiyun 
2400*4882a593Smuzhiyun 		vmbus_force_channel_rescinded(channel);
2401*4882a593Smuzhiyun 	}
2402*4882a593Smuzhiyun 	mutex_unlock(&vmbus_connection.channel_mutex);
2403*4882a593Smuzhiyun 
2404*4882a593Smuzhiyun 	/*
2405*4882a593Smuzhiyun 	 * Wait until all the sub-channels and hv_sock channels have been
2406*4882a593Smuzhiyun 	 * cleaned up. Sub-channels should be destroyed upon suspend, otherwise
2407*4882a593Smuzhiyun 	 * they would conflict with the new sub-channels that will be created
2408*4882a593Smuzhiyun 	 * in the resume path. hv_sock channels should also be destroyed, but
2409*4882a593Smuzhiyun 	 * a hv_sock channel of an established hv_sock connection can not be
2410*4882a593Smuzhiyun 	 * really destroyed since it may still be referenced by the userspace
2411*4882a593Smuzhiyun 	 * application, so we just force the hv_sock channel to be rescinded
2412*4882a593Smuzhiyun 	 * by vmbus_force_channel_rescinded(), and the userspace application
2413*4882a593Smuzhiyun 	 * will thoroughly destroy the channel after hibernation.
2414*4882a593Smuzhiyun 	 *
2415*4882a593Smuzhiyun 	 * Note: the counter nr_chan_close_on_suspend may never go above 0 if
2416*4882a593Smuzhiyun 	 * the VM has no sub-channel and hv_sock channel, e.g. a 1-vCPU VM.
2417*4882a593Smuzhiyun 	 */
2418*4882a593Smuzhiyun 	if (atomic_read(&vmbus_connection.nr_chan_close_on_suspend) > 0)
2419*4882a593Smuzhiyun 		wait_for_completion(&vmbus_connection.ready_for_suspend_event);
2420*4882a593Smuzhiyun 
2421*4882a593Smuzhiyun 	if (atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) != 0) {
2422*4882a593Smuzhiyun 		pr_err("Can not suspend due to a previous failed resuming\n");
2423*4882a593Smuzhiyun 		return -EBUSY;
2424*4882a593Smuzhiyun 	}
2425*4882a593Smuzhiyun 
2426*4882a593Smuzhiyun 	mutex_lock(&vmbus_connection.channel_mutex);
2427*4882a593Smuzhiyun 
2428*4882a593Smuzhiyun 	list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
2429*4882a593Smuzhiyun 		/*
2430*4882a593Smuzhiyun 		 * Remove the channel from the array of channels and invalidate
2431*4882a593Smuzhiyun 		 * the channel's relid.  Upon resume, vmbus_onoffer() will fix
2432*4882a593Smuzhiyun 		 * up the relid (and other fields, if necessary) and add the
2433*4882a593Smuzhiyun 		 * channel back to the array.
2434*4882a593Smuzhiyun 		 */
2435*4882a593Smuzhiyun 		vmbus_channel_unmap_relid(channel);
2436*4882a593Smuzhiyun 		channel->offermsg.child_relid = INVALID_RELID;
2437*4882a593Smuzhiyun 
2438*4882a593Smuzhiyun 		if (is_hvsock_channel(channel)) {
2439*4882a593Smuzhiyun 			if (!channel->rescind) {
2440*4882a593Smuzhiyun 				pr_err("hv_sock channel not rescinded!\n");
2441*4882a593Smuzhiyun 				WARN_ON_ONCE(1);
2442*4882a593Smuzhiyun 			}
2443*4882a593Smuzhiyun 			continue;
2444*4882a593Smuzhiyun 		}
2445*4882a593Smuzhiyun 
2446*4882a593Smuzhiyun 		list_for_each_entry(sc, &channel->sc_list, sc_list) {
2447*4882a593Smuzhiyun 			pr_err("Sub-channel not deleted!\n");
2448*4882a593Smuzhiyun 			WARN_ON_ONCE(1);
2449*4882a593Smuzhiyun 		}
2450*4882a593Smuzhiyun 
2451*4882a593Smuzhiyun 		atomic_inc(&vmbus_connection.nr_chan_fixup_on_resume);
2452*4882a593Smuzhiyun 	}
2453*4882a593Smuzhiyun 
2454*4882a593Smuzhiyun 	mutex_unlock(&vmbus_connection.channel_mutex);
2455*4882a593Smuzhiyun 
2456*4882a593Smuzhiyun 	vmbus_initiate_unload(false);
2457*4882a593Smuzhiyun 
2458*4882a593Smuzhiyun 	/* Reset the event for the next resume. */
2459*4882a593Smuzhiyun 	reinit_completion(&vmbus_connection.ready_for_resume_event);
2460*4882a593Smuzhiyun 
2461*4882a593Smuzhiyun 	return 0;
2462*4882a593Smuzhiyun }
2463*4882a593Smuzhiyun 
vmbus_bus_resume(struct device * dev)2464*4882a593Smuzhiyun static int vmbus_bus_resume(struct device *dev)
2465*4882a593Smuzhiyun {
2466*4882a593Smuzhiyun 	struct vmbus_channel_msginfo *msginfo;
2467*4882a593Smuzhiyun 	size_t msgsize;
2468*4882a593Smuzhiyun 	int ret;
2469*4882a593Smuzhiyun 
2470*4882a593Smuzhiyun 	/*
2471*4882a593Smuzhiyun 	 * We only use the 'vmbus_proto_version', which was in use before
2472*4882a593Smuzhiyun 	 * hibernation, to re-negotiate with the host.
2473*4882a593Smuzhiyun 	 */
2474*4882a593Smuzhiyun 	if (!vmbus_proto_version) {
2475*4882a593Smuzhiyun 		pr_err("Invalid proto version = 0x%x\n", vmbus_proto_version);
2476*4882a593Smuzhiyun 		return -EINVAL;
2477*4882a593Smuzhiyun 	}
2478*4882a593Smuzhiyun 
2479*4882a593Smuzhiyun 	msgsize = sizeof(*msginfo) +
2480*4882a593Smuzhiyun 		  sizeof(struct vmbus_channel_initiate_contact);
2481*4882a593Smuzhiyun 
2482*4882a593Smuzhiyun 	msginfo = kzalloc(msgsize, GFP_KERNEL);
2483*4882a593Smuzhiyun 
2484*4882a593Smuzhiyun 	if (msginfo == NULL)
2485*4882a593Smuzhiyun 		return -ENOMEM;
2486*4882a593Smuzhiyun 
2487*4882a593Smuzhiyun 	ret = vmbus_negotiate_version(msginfo, vmbus_proto_version);
2488*4882a593Smuzhiyun 
2489*4882a593Smuzhiyun 	kfree(msginfo);
2490*4882a593Smuzhiyun 
2491*4882a593Smuzhiyun 	if (ret != 0)
2492*4882a593Smuzhiyun 		return ret;
2493*4882a593Smuzhiyun 
2494*4882a593Smuzhiyun 	WARN_ON(atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) == 0);
2495*4882a593Smuzhiyun 
2496*4882a593Smuzhiyun 	vmbus_request_offers();
2497*4882a593Smuzhiyun 
2498*4882a593Smuzhiyun 	if (wait_for_completion_timeout(
2499*4882a593Smuzhiyun 		&vmbus_connection.ready_for_resume_event, 10 * HZ) == 0)
2500*4882a593Smuzhiyun 		pr_err("Some vmbus device is missing after suspending?\n");
2501*4882a593Smuzhiyun 
2502*4882a593Smuzhiyun 	/* Reset the event for the next suspend. */
2503*4882a593Smuzhiyun 	reinit_completion(&vmbus_connection.ready_for_suspend_event);
2504*4882a593Smuzhiyun 
2505*4882a593Smuzhiyun 	return 0;
2506*4882a593Smuzhiyun }
2507*4882a593Smuzhiyun #else
2508*4882a593Smuzhiyun #define vmbus_bus_suspend NULL
2509*4882a593Smuzhiyun #define vmbus_bus_resume NULL
2510*4882a593Smuzhiyun #endif /* CONFIG_PM_SLEEP */
2511*4882a593Smuzhiyun 
2512*4882a593Smuzhiyun static const struct acpi_device_id vmbus_acpi_device_ids[] = {
2513*4882a593Smuzhiyun 	{"VMBUS", 0},
2514*4882a593Smuzhiyun 	{"VMBus", 0},
2515*4882a593Smuzhiyun 	{"", 0},
2516*4882a593Smuzhiyun };
2517*4882a593Smuzhiyun MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
2518*4882a593Smuzhiyun 
2519*4882a593Smuzhiyun /*
2520*4882a593Smuzhiyun  * Note: we must use the "no_irq" ops, otherwise hibernation can not work with
2521*4882a593Smuzhiyun  * PCI device assignment, because "pci_dev_pm_ops" uses the "noirq" ops: in
2522*4882a593Smuzhiyun  * the resume path, the pci "noirq" restore op runs before "non-noirq" op (see
2523*4882a593Smuzhiyun  * resume_target_kernel() -> dpm_resume_start(), and hibernation_restore() ->
2524*4882a593Smuzhiyun  * dpm_resume_end()). This means vmbus_bus_resume() and the pci-hyperv's
2525*4882a593Smuzhiyun  * resume callback must also run via the "noirq" ops.
2526*4882a593Smuzhiyun  *
2527*4882a593Smuzhiyun  * Set suspend_noirq/resume_noirq to NULL for Suspend-to-Idle: see the comment
2528*4882a593Smuzhiyun  * earlier in this file before vmbus_pm.
2529*4882a593Smuzhiyun  */
2530*4882a593Smuzhiyun 
2531*4882a593Smuzhiyun static const struct dev_pm_ops vmbus_bus_pm = {
2532*4882a593Smuzhiyun 	.suspend_noirq	= NULL,
2533*4882a593Smuzhiyun 	.resume_noirq	= NULL,
2534*4882a593Smuzhiyun 	.freeze_noirq	= vmbus_bus_suspend,
2535*4882a593Smuzhiyun 	.thaw_noirq	= vmbus_bus_resume,
2536*4882a593Smuzhiyun 	.poweroff_noirq	= vmbus_bus_suspend,
2537*4882a593Smuzhiyun 	.restore_noirq	= vmbus_bus_resume
2538*4882a593Smuzhiyun };
2539*4882a593Smuzhiyun 
2540*4882a593Smuzhiyun static struct acpi_driver vmbus_acpi_driver = {
2541*4882a593Smuzhiyun 	.name = "vmbus",
2542*4882a593Smuzhiyun 	.ids = vmbus_acpi_device_ids,
2543*4882a593Smuzhiyun 	.ops = {
2544*4882a593Smuzhiyun 		.add = vmbus_acpi_add,
2545*4882a593Smuzhiyun 		.remove = vmbus_acpi_remove,
2546*4882a593Smuzhiyun 	},
2547*4882a593Smuzhiyun 	.drv.pm = &vmbus_bus_pm,
2548*4882a593Smuzhiyun };
2549*4882a593Smuzhiyun 
hv_kexec_handler(void)2550*4882a593Smuzhiyun static void hv_kexec_handler(void)
2551*4882a593Smuzhiyun {
2552*4882a593Smuzhiyun 	hv_stimer_global_cleanup();
2553*4882a593Smuzhiyun 	vmbus_initiate_unload(false);
2554*4882a593Smuzhiyun 	/* Make sure conn_state is set as hv_synic_cleanup checks for it */
2555*4882a593Smuzhiyun 	mb();
2556*4882a593Smuzhiyun 	cpuhp_remove_state(hyperv_cpuhp_online);
2557*4882a593Smuzhiyun };
2558*4882a593Smuzhiyun 
hv_crash_handler(struct pt_regs * regs)2559*4882a593Smuzhiyun static void hv_crash_handler(struct pt_regs *regs)
2560*4882a593Smuzhiyun {
2561*4882a593Smuzhiyun 	int cpu;
2562*4882a593Smuzhiyun 
2563*4882a593Smuzhiyun 	vmbus_initiate_unload(true);
2564*4882a593Smuzhiyun 	/*
2565*4882a593Smuzhiyun 	 * In crash handler we can't schedule synic cleanup for all CPUs,
2566*4882a593Smuzhiyun 	 * doing the cleanup for current CPU only. This should be sufficient
2567*4882a593Smuzhiyun 	 * for kdump.
2568*4882a593Smuzhiyun 	 */
2569*4882a593Smuzhiyun 	cpu = smp_processor_id();
2570*4882a593Smuzhiyun 	hv_stimer_cleanup(cpu);
2571*4882a593Smuzhiyun 	hv_synic_disable_regs(cpu);
2572*4882a593Smuzhiyun };
2573*4882a593Smuzhiyun 
hv_synic_suspend(void)2574*4882a593Smuzhiyun static int hv_synic_suspend(void)
2575*4882a593Smuzhiyun {
2576*4882a593Smuzhiyun 	/*
2577*4882a593Smuzhiyun 	 * When we reach here, all the non-boot CPUs have been offlined.
2578*4882a593Smuzhiyun 	 * If we're in a legacy configuration where stimer Direct Mode is
2579*4882a593Smuzhiyun 	 * not enabled, the stimers on the non-boot CPUs have been unbound
2580*4882a593Smuzhiyun 	 * in hv_synic_cleanup() -> hv_stimer_legacy_cleanup() ->
2581*4882a593Smuzhiyun 	 * hv_stimer_cleanup() -> clockevents_unbind_device().
2582*4882a593Smuzhiyun 	 *
2583*4882a593Smuzhiyun 	 * hv_synic_suspend() only runs on CPU0 with interrupts disabled.
2584*4882a593Smuzhiyun 	 * Here we do not call hv_stimer_legacy_cleanup() on CPU0 because:
2585*4882a593Smuzhiyun 	 * 1) it's unnecessary as interrupts remain disabled between
2586*4882a593Smuzhiyun 	 * syscore_suspend() and syscore_resume(): see create_image() and
2587*4882a593Smuzhiyun 	 * resume_target_kernel()
2588*4882a593Smuzhiyun 	 * 2) the stimer on CPU0 is automatically disabled later by
2589*4882a593Smuzhiyun 	 * syscore_suspend() -> timekeeping_suspend() -> tick_suspend() -> ...
2590*4882a593Smuzhiyun 	 * -> clockevents_shutdown() -> ... -> hv_ce_shutdown()
2591*4882a593Smuzhiyun 	 * 3) a warning would be triggered if we call
2592*4882a593Smuzhiyun 	 * clockevents_unbind_device(), which may sleep, in an
2593*4882a593Smuzhiyun 	 * interrupts-disabled context.
2594*4882a593Smuzhiyun 	 */
2595*4882a593Smuzhiyun 
2596*4882a593Smuzhiyun 	hv_synic_disable_regs(0);
2597*4882a593Smuzhiyun 
2598*4882a593Smuzhiyun 	return 0;
2599*4882a593Smuzhiyun }
2600*4882a593Smuzhiyun 
hv_synic_resume(void)2601*4882a593Smuzhiyun static void hv_synic_resume(void)
2602*4882a593Smuzhiyun {
2603*4882a593Smuzhiyun 	hv_synic_enable_regs(0);
2604*4882a593Smuzhiyun 
2605*4882a593Smuzhiyun 	/*
2606*4882a593Smuzhiyun 	 * Note: we don't need to call hv_stimer_init(0), because the timer
2607*4882a593Smuzhiyun 	 * on CPU0 is not unbound in hv_synic_suspend(), and the timer is
2608*4882a593Smuzhiyun 	 * automatically re-enabled in timekeeping_resume().
2609*4882a593Smuzhiyun 	 */
2610*4882a593Smuzhiyun }
2611*4882a593Smuzhiyun 
2612*4882a593Smuzhiyun /* The callbacks run only on CPU0, with irqs_disabled. */
2613*4882a593Smuzhiyun static struct syscore_ops hv_synic_syscore_ops = {
2614*4882a593Smuzhiyun 	.suspend = hv_synic_suspend,
2615*4882a593Smuzhiyun 	.resume = hv_synic_resume,
2616*4882a593Smuzhiyun };
2617*4882a593Smuzhiyun 
hv_acpi_init(void)2618*4882a593Smuzhiyun static int __init hv_acpi_init(void)
2619*4882a593Smuzhiyun {
2620*4882a593Smuzhiyun 	int ret, t;
2621*4882a593Smuzhiyun 
2622*4882a593Smuzhiyun 	if (!hv_is_hyperv_initialized())
2623*4882a593Smuzhiyun 		return -ENODEV;
2624*4882a593Smuzhiyun 
2625*4882a593Smuzhiyun 	init_completion(&probe_event);
2626*4882a593Smuzhiyun 
2627*4882a593Smuzhiyun 	/*
2628*4882a593Smuzhiyun 	 * Get ACPI resources first.
2629*4882a593Smuzhiyun 	 */
2630*4882a593Smuzhiyun 	ret = acpi_bus_register_driver(&vmbus_acpi_driver);
2631*4882a593Smuzhiyun 
2632*4882a593Smuzhiyun 	if (ret)
2633*4882a593Smuzhiyun 		return ret;
2634*4882a593Smuzhiyun 
2635*4882a593Smuzhiyun 	t = wait_for_completion_timeout(&probe_event, 5*HZ);
2636*4882a593Smuzhiyun 	if (t == 0) {
2637*4882a593Smuzhiyun 		ret = -ETIMEDOUT;
2638*4882a593Smuzhiyun 		goto cleanup;
2639*4882a593Smuzhiyun 	}
2640*4882a593Smuzhiyun 	hv_debug_init();
2641*4882a593Smuzhiyun 
2642*4882a593Smuzhiyun 	ret = vmbus_bus_init();
2643*4882a593Smuzhiyun 	if (ret)
2644*4882a593Smuzhiyun 		goto cleanup;
2645*4882a593Smuzhiyun 
2646*4882a593Smuzhiyun 	hv_setup_kexec_handler(hv_kexec_handler);
2647*4882a593Smuzhiyun 	hv_setup_crash_handler(hv_crash_handler);
2648*4882a593Smuzhiyun 
2649*4882a593Smuzhiyun 	register_syscore_ops(&hv_synic_syscore_ops);
2650*4882a593Smuzhiyun 
2651*4882a593Smuzhiyun 	return 0;
2652*4882a593Smuzhiyun 
2653*4882a593Smuzhiyun cleanup:
2654*4882a593Smuzhiyun 	acpi_bus_unregister_driver(&vmbus_acpi_driver);
2655*4882a593Smuzhiyun 	hv_acpi_dev = NULL;
2656*4882a593Smuzhiyun 	return ret;
2657*4882a593Smuzhiyun }
2658*4882a593Smuzhiyun 
vmbus_exit(void)2659*4882a593Smuzhiyun static void __exit vmbus_exit(void)
2660*4882a593Smuzhiyun {
2661*4882a593Smuzhiyun 	int cpu;
2662*4882a593Smuzhiyun 
2663*4882a593Smuzhiyun 	unregister_syscore_ops(&hv_synic_syscore_ops);
2664*4882a593Smuzhiyun 
2665*4882a593Smuzhiyun 	hv_remove_kexec_handler();
2666*4882a593Smuzhiyun 	hv_remove_crash_handler();
2667*4882a593Smuzhiyun 	vmbus_connection.conn_state = DISCONNECTED;
2668*4882a593Smuzhiyun 	hv_stimer_global_cleanup();
2669*4882a593Smuzhiyun 	vmbus_disconnect();
2670*4882a593Smuzhiyun 	hv_remove_vmbus_irq();
2671*4882a593Smuzhiyun 	for_each_online_cpu(cpu) {
2672*4882a593Smuzhiyun 		struct hv_per_cpu_context *hv_cpu
2673*4882a593Smuzhiyun 			= per_cpu_ptr(hv_context.cpu_context, cpu);
2674*4882a593Smuzhiyun 
2675*4882a593Smuzhiyun 		tasklet_kill(&hv_cpu->msg_dpc);
2676*4882a593Smuzhiyun 	}
2677*4882a593Smuzhiyun 	hv_debug_rm_all_dir();
2678*4882a593Smuzhiyun 
2679*4882a593Smuzhiyun 	vmbus_free_channels();
2680*4882a593Smuzhiyun 	kfree(vmbus_connection.channels);
2681*4882a593Smuzhiyun 
2682*4882a593Smuzhiyun 	if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
2683*4882a593Smuzhiyun 		kmsg_dump_unregister(&hv_kmsg_dumper);
2684*4882a593Smuzhiyun 		unregister_die_notifier(&hyperv_die_block);
2685*4882a593Smuzhiyun 	}
2686*4882a593Smuzhiyun 
2687*4882a593Smuzhiyun 	/*
2688*4882a593Smuzhiyun 	 * The panic notifier is always registered, hence we should
2689*4882a593Smuzhiyun 	 * also unconditionally unregister it here as well.
2690*4882a593Smuzhiyun 	 */
2691*4882a593Smuzhiyun 	atomic_notifier_chain_unregister(&panic_notifier_list,
2692*4882a593Smuzhiyun 					 &hyperv_panic_block);
2693*4882a593Smuzhiyun 
2694*4882a593Smuzhiyun 	free_page((unsigned long)hv_panic_page);
2695*4882a593Smuzhiyun 	unregister_sysctl_table(hv_ctl_table_hdr);
2696*4882a593Smuzhiyun 	hv_ctl_table_hdr = NULL;
2697*4882a593Smuzhiyun 	bus_unregister(&hv_bus);
2698*4882a593Smuzhiyun 
2699*4882a593Smuzhiyun 	cpuhp_remove_state(hyperv_cpuhp_online);
2700*4882a593Smuzhiyun 	hv_synic_free();
2701*4882a593Smuzhiyun 	acpi_bus_unregister_driver(&vmbus_acpi_driver);
2702*4882a593Smuzhiyun }
2703*4882a593Smuzhiyun 
2704*4882a593Smuzhiyun 
2705*4882a593Smuzhiyun MODULE_LICENSE("GPL");
2706*4882a593Smuzhiyun MODULE_DESCRIPTION("Microsoft Hyper-V VMBus Driver");
2707*4882a593Smuzhiyun 
2708*4882a593Smuzhiyun subsys_initcall(hv_acpi_init);
2709*4882a593Smuzhiyun module_exit(vmbus_exit);
2710