xref: /openbmc/linux/drivers/hv/channel_mgmt.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
13b20eb23SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
246a97191SGreg Kroah-Hartman /*
346a97191SGreg Kroah-Hartman  * Copyright (c) 2009, Microsoft Corporation.
446a97191SGreg Kroah-Hartman  *
546a97191SGreg Kroah-Hartman  * Authors:
646a97191SGreg Kroah-Hartman  *   Haiyang Zhang <haiyangz@microsoft.com>
746a97191SGreg Kroah-Hartman  *   Hank Janssen  <hjanssen@microsoft.com>
846a97191SGreg Kroah-Hartman  */
946a97191SGreg Kroah-Hartman #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1046a97191SGreg Kroah-Hartman 
1146a97191SGreg Kroah-Hartman #include <linux/kernel.h>
12638fea33SDexuan Cui #include <linux/interrupt.h>
1346a97191SGreg Kroah-Hartman #include <linux/sched.h>
1446a97191SGreg Kroah-Hartman #include <linux/wait.h>
1546a97191SGreg Kroah-Hartman #include <linux/mm.h>
1646a97191SGreg Kroah-Hartman #include <linux/slab.h>
1746a97191SGreg Kroah-Hartman #include <linux/list.h>
1846a97191SGreg Kroah-Hartman #include <linux/module.h>
1946a97191SGreg Kroah-Hartman #include <linux/completion.h>
2041571916SVitaly Kuznetsov #include <linux/delay.h>
21d570aec0SAndrea Parri (Microsoft) #include <linux/cpu.h>
2246a97191SGreg Kroah-Hartman #include <linux/hyperv.h>
23e810e48cSK. Y. Srinivasan #include <asm/mshyperv.h>
246640b5dfSSaurabh Sengar #include <linux/sched/isolation.h>
2546a97191SGreg Kroah-Hartman 
2646a97191SGreg Kroah-Hartman #include "hyperv_vmbus.h"
2746a97191SGreg Kroah-Hartman 
28afaa33daSAndrea Parri (Microsoft) static void init_vp_index(struct vmbus_channel *channel);
297047f17dSK. Y. Srinivasan 
30afaa33daSAndrea Parri (Microsoft) const struct vmbus_device vmbus_devs[] = {
317047f17dSK. Y. Srinivasan 	/* IDE */
327047f17dSK. Y. Srinivasan 	{ .dev_type = HV_IDE,
337047f17dSK. Y. Srinivasan 	  HV_IDE_GUID,
347047f17dSK. Y. Srinivasan 	  .perf_device = true,
3521a4e356SAndrea Parri (Microsoft) 	  .allowed_in_isolated = false,
367047f17dSK. Y. Srinivasan 	},
377047f17dSK. Y. Srinivasan 
387047f17dSK. Y. Srinivasan 	/* SCSI */
397047f17dSK. Y. Srinivasan 	{ .dev_type = HV_SCSI,
407047f17dSK. Y. Srinivasan 	  HV_SCSI_GUID,
417047f17dSK. Y. Srinivasan 	  .perf_device = true,
4221a4e356SAndrea Parri (Microsoft) 	  .allowed_in_isolated = true,
437047f17dSK. Y. Srinivasan 	},
447047f17dSK. Y. Srinivasan 
457047f17dSK. Y. Srinivasan 	/* Fibre Channel */
467047f17dSK. Y. Srinivasan 	{ .dev_type = HV_FC,
477047f17dSK. Y. Srinivasan 	  HV_SYNTHFC_GUID,
487047f17dSK. Y. Srinivasan 	  .perf_device = true,
4921a4e356SAndrea Parri (Microsoft) 	  .allowed_in_isolated = false,
507047f17dSK. Y. Srinivasan 	},
517047f17dSK. Y. Srinivasan 
527047f17dSK. Y. Srinivasan 	/* Synthetic NIC */
537047f17dSK. Y. Srinivasan 	{ .dev_type = HV_NIC,
547047f17dSK. Y. Srinivasan 	  HV_NIC_GUID,
557047f17dSK. Y. Srinivasan 	  .perf_device = true,
5621a4e356SAndrea Parri (Microsoft) 	  .allowed_in_isolated = true,
577047f17dSK. Y. Srinivasan 	},
587047f17dSK. Y. Srinivasan 
597047f17dSK. Y. Srinivasan 	/* Network Direct */
607047f17dSK. Y. Srinivasan 	{ .dev_type = HV_ND,
617047f17dSK. Y. Srinivasan 	  HV_ND_GUID,
627047f17dSK. Y. Srinivasan 	  .perf_device = true,
6321a4e356SAndrea Parri (Microsoft) 	  .allowed_in_isolated = false,
647047f17dSK. Y. Srinivasan 	},
657047f17dSK. Y. Srinivasan 
667047f17dSK. Y. Srinivasan 	/* PCIE */
677047f17dSK. Y. Srinivasan 	{ .dev_type = HV_PCIE,
687047f17dSK. Y. Srinivasan 	  HV_PCIE_GUID,
69238064f1SDexuan Cui 	  .perf_device = false,
702c6ba421SMichael Kelley 	  .allowed_in_isolated = true,
717047f17dSK. Y. Srinivasan 	},
727047f17dSK. Y. Srinivasan 
737047f17dSK. Y. Srinivasan 	/* Synthetic Frame Buffer */
747047f17dSK. Y. Srinivasan 	{ .dev_type = HV_FB,
757047f17dSK. Y. Srinivasan 	  HV_SYNTHVID_GUID,
767047f17dSK. Y. Srinivasan 	  .perf_device = false,
7721a4e356SAndrea Parri (Microsoft) 	  .allowed_in_isolated = false,
787047f17dSK. Y. Srinivasan 	},
797047f17dSK. Y. Srinivasan 
807047f17dSK. Y. Srinivasan 	/* Synthetic Keyboard */
817047f17dSK. Y. Srinivasan 	{ .dev_type = HV_KBD,
827047f17dSK. Y. Srinivasan 	  HV_KBD_GUID,
837047f17dSK. Y. Srinivasan 	  .perf_device = false,
8421a4e356SAndrea Parri (Microsoft) 	  .allowed_in_isolated = false,
857047f17dSK. Y. Srinivasan 	},
867047f17dSK. Y. Srinivasan 
877047f17dSK. Y. Srinivasan 	/* Synthetic MOUSE */
887047f17dSK. Y. Srinivasan 	{ .dev_type = HV_MOUSE,
897047f17dSK. Y. Srinivasan 	  HV_MOUSE_GUID,
907047f17dSK. Y. Srinivasan 	  .perf_device = false,
9121a4e356SAndrea Parri (Microsoft) 	  .allowed_in_isolated = false,
927047f17dSK. Y. Srinivasan 	},
937047f17dSK. Y. Srinivasan 
947047f17dSK. Y. Srinivasan 	/* KVP */
957047f17dSK. Y. Srinivasan 	{ .dev_type = HV_KVP,
967047f17dSK. Y. Srinivasan 	  HV_KVP_GUID,
977047f17dSK. Y. Srinivasan 	  .perf_device = false,
9821a4e356SAndrea Parri (Microsoft) 	  .allowed_in_isolated = false,
997047f17dSK. Y. Srinivasan 	},
1007047f17dSK. Y. Srinivasan 
1017047f17dSK. Y. Srinivasan 	/* Time Synch */
1027047f17dSK. Y. Srinivasan 	{ .dev_type = HV_TS,
1037047f17dSK. Y. Srinivasan 	  HV_TS_GUID,
1047047f17dSK. Y. Srinivasan 	  .perf_device = false,
10521a4e356SAndrea Parri (Microsoft) 	  .allowed_in_isolated = true,
1067047f17dSK. Y. Srinivasan 	},
1077047f17dSK. Y. Srinivasan 
1087047f17dSK. Y. Srinivasan 	/* Heartbeat */
1097047f17dSK. Y. Srinivasan 	{ .dev_type = HV_HB,
1107047f17dSK. Y. Srinivasan 	  HV_HEART_BEAT_GUID,
1117047f17dSK. Y. Srinivasan 	  .perf_device = false,
11221a4e356SAndrea Parri (Microsoft) 	  .allowed_in_isolated = true,
1137047f17dSK. Y. Srinivasan 	},
1147047f17dSK. Y. Srinivasan 
1157047f17dSK. Y. Srinivasan 	/* Shutdown */
1167047f17dSK. Y. Srinivasan 	{ .dev_type = HV_SHUTDOWN,
1177047f17dSK. Y. Srinivasan 	  HV_SHUTDOWN_GUID,
1187047f17dSK. Y. Srinivasan 	  .perf_device = false,
11921a4e356SAndrea Parri (Microsoft) 	  .allowed_in_isolated = true,
1207047f17dSK. Y. Srinivasan 	},
1217047f17dSK. Y. Srinivasan 
1227047f17dSK. Y. Srinivasan 	/* File copy */
1237047f17dSK. Y. Srinivasan 	{ .dev_type = HV_FCOPY,
1247047f17dSK. Y. Srinivasan 	  HV_FCOPY_GUID,
1257047f17dSK. Y. Srinivasan 	  .perf_device = false,
12621a4e356SAndrea Parri (Microsoft) 	  .allowed_in_isolated = false,
1277047f17dSK. Y. Srinivasan 	},
1287047f17dSK. Y. Srinivasan 
1297047f17dSK. Y. Srinivasan 	/* Backup */
1307047f17dSK. Y. Srinivasan 	{ .dev_type = HV_BACKUP,
1317047f17dSK. Y. Srinivasan 	  HV_VSS_GUID,
1327047f17dSK. Y. Srinivasan 	  .perf_device = false,
13321a4e356SAndrea Parri (Microsoft) 	  .allowed_in_isolated = false,
1347047f17dSK. Y. Srinivasan 	},
1357047f17dSK. Y. Srinivasan 
1367047f17dSK. Y. Srinivasan 	/* Dynamic Memory */
1377047f17dSK. Y. Srinivasan 	{ .dev_type = HV_DM,
1387047f17dSK. Y. Srinivasan 	  HV_DM_GUID,
1397047f17dSK. Y. Srinivasan 	  .perf_device = false,
14021a4e356SAndrea Parri (Microsoft) 	  .allowed_in_isolated = false,
1417047f17dSK. Y. Srinivasan 	},
1427047f17dSK. Y. Srinivasan 
1437047f17dSK. Y. Srinivasan 	/* Unknown GUID */
144f45be72cSHaiyang Zhang 	{ .dev_type = HV_UNKNOWN,
1457047f17dSK. Y. Srinivasan 	  .perf_device = false,
14621a4e356SAndrea Parri (Microsoft) 	  .allowed_in_isolated = false,
1477047f17dSK. Y. Srinivasan 	},
1487047f17dSK. Y. Srinivasan };
1497047f17dSK. Y. Srinivasan 
1500f98829aSDexuan Cui static const struct {
151593db803SAndy Shevchenko 	guid_t guid;
1520f98829aSDexuan Cui } vmbus_unsupported_devs[] = {
1530f98829aSDexuan Cui 	{ HV_AVMA1_GUID },
1540f98829aSDexuan Cui 	{ HV_AVMA2_GUID },
1550f98829aSDexuan Cui 	{ HV_RDV_GUID	},
15666200bbcSMichael Kelley 	{ HV_IMC_GUID	},
1570f98829aSDexuan Cui };
1580f98829aSDexuan Cui 
159ccb61f8aSK. Y. Srinivasan /*
160ccb61f8aSK. Y. Srinivasan  * The rescinded channel may be blocked waiting for a response from the host;
161ccb61f8aSK. Y. Srinivasan  * take care of that.
162ccb61f8aSK. Y. Srinivasan  */
vmbus_rescind_cleanup(struct vmbus_channel * channel)163ccb61f8aSK. Y. Srinivasan static void vmbus_rescind_cleanup(struct vmbus_channel *channel)
164ccb61f8aSK. Y. Srinivasan {
165ccb61f8aSK. Y. Srinivasan 	struct vmbus_channel_msginfo *msginfo;
166ccb61f8aSK. Y. Srinivasan 	unsigned long flags;
167ccb61f8aSK. Y. Srinivasan 
168ccb61f8aSK. Y. Srinivasan 
169ccb61f8aSK. Y. Srinivasan 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
170192b2d78SK. Y. Srinivasan 	channel->rescind = true;
171ccb61f8aSK. Y. Srinivasan 	list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
172ccb61f8aSK. Y. Srinivasan 				msglistentry) {
173ccb61f8aSK. Y. Srinivasan 
174ccb61f8aSK. Y. Srinivasan 		if (msginfo->waiting_channel == channel) {
175ccb61f8aSK. Y. Srinivasan 			complete(&msginfo->waitevent);
176ccb61f8aSK. Y. Srinivasan 			break;
177ccb61f8aSK. Y. Srinivasan 		}
178ccb61f8aSK. Y. Srinivasan 	}
179ccb61f8aSK. Y. Srinivasan 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
180ccb61f8aSK. Y. Srinivasan }
181ccb61f8aSK. Y. Srinivasan 
is_unsupported_vmbus_devs(const guid_t * guid)182593db803SAndy Shevchenko static bool is_unsupported_vmbus_devs(const guid_t *guid)
1837047f17dSK. Y. Srinivasan {
1840f98829aSDexuan Cui 	int i;
1850f98829aSDexuan Cui 
1860f98829aSDexuan Cui 	for (i = 0; i < ARRAY_SIZE(vmbus_unsupported_devs); i++)
187593db803SAndy Shevchenko 		if (guid_equal(guid, &vmbus_unsupported_devs[i].guid))
1880f98829aSDexuan Cui 			return true;
1890f98829aSDexuan Cui 	return false;
1900f98829aSDexuan Cui }
1910f98829aSDexuan Cui 
hv_get_dev_type(const struct vmbus_channel * channel)1920f98829aSDexuan Cui static u16 hv_get_dev_type(const struct vmbus_channel *channel)
1930f98829aSDexuan Cui {
194593db803SAndy Shevchenko 	const guid_t *guid = &channel->offermsg.offer.if_type;
1957047f17dSK. Y. Srinivasan 	u16 i;
1967047f17dSK. Y. Srinivasan 
1970f98829aSDexuan Cui 	if (is_hvsock_channel(channel) || is_unsupported_vmbus_devs(guid))
198f45be72cSHaiyang Zhang 		return HV_UNKNOWN;
1990f98829aSDexuan Cui 
200f45be72cSHaiyang Zhang 	for (i = HV_IDE; i < HV_UNKNOWN; i++) {
201593db803SAndy Shevchenko 		if (guid_equal(guid, &vmbus_devs[i].guid))
2027047f17dSK. Y. Srinivasan 			return i;
2037047f17dSK. Y. Srinivasan 	}
2047047f17dSK. Y. Srinivasan 	pr_info("Unknown GUID: %pUl\n", guid);
2057047f17dSK. Y. Srinivasan 	return i;
2067047f17dSK. Y. Srinivasan }
207f38e7dd7SVitaly Kuznetsov 
20846a97191SGreg Kroah-Hartman /**
209fe857bb4SDexuan Cui  * vmbus_prep_negotiate_resp() - Create default response for Negotiate message
21046a97191SGreg Kroah-Hartman  * @icmsghdrp: Pointer to msg header structure
21146a97191SGreg Kroah-Hartman  * @buf: Raw buffer channel data
21206caa778SAndres Beltran  * @buflen: Length of the raw buffer channel data.
213fe857bb4SDexuan Cui  * @fw_version: The framework versions we can support.
214fe857bb4SDexuan Cui  * @fw_vercnt: The size of @fw_version.
215fe857bb4SDexuan Cui  * @srv_version: The service versions we can support.
216fe857bb4SDexuan Cui  * @srv_vercnt: The size of @srv_version.
217fe857bb4SDexuan Cui  * @nego_fw_version: The selected framework version.
218fe857bb4SDexuan Cui  * @nego_srv_version: The selected service version.
21946a97191SGreg Kroah-Hartman  *
220fe857bb4SDexuan Cui  * Note: Versions are given in decreasing order.
221fe857bb4SDexuan Cui  *
222c836d0abSK. Y. Srinivasan  * Set up and fill in default negotiate response message.
22346a97191SGreg Kroah-Hartman  * Mainly used by Hyper-V drivers.
22446a97191SGreg Kroah-Hartman  */
vmbus_prep_negotiate_resp(struct icmsg_hdr * icmsghdrp,u8 * buf,u32 buflen,const int * fw_version,int fw_vercnt,const int * srv_version,int srv_vercnt,int * nego_fw_version,int * nego_srv_version)22506caa778SAndres Beltran bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp, u8 *buf,
22606caa778SAndres Beltran 				u32 buflen, const int *fw_version, int fw_vercnt,
227a1656454SAlex Ng 				const int *srv_version, int srv_vercnt,
228a1656454SAlex Ng 				int *nego_fw_version, int *nego_srv_version)
22946a97191SGreg Kroah-Hartman {
2306741335bSK. Y. Srinivasan 	int icframe_major, icframe_minor;
2316741335bSK. Y. Srinivasan 	int icmsg_major, icmsg_minor;
2326741335bSK. Y. Srinivasan 	int fw_major, fw_minor;
2336741335bSK. Y. Srinivasan 	int srv_major, srv_minor;
234a1656454SAlex Ng 	int i, j;
2356741335bSK. Y. Srinivasan 	bool found_match = false;
236a1656454SAlex Ng 	struct icmsg_negotiate *negop;
237c836d0abSK. Y. Srinivasan 
23806caa778SAndres Beltran 	/* Check that there's enough space for icframe_vercnt, icmsg_vercnt */
23906caa778SAndres Beltran 	if (buflen < ICMSG_HDR + offsetof(struct icmsg_negotiate, reserved)) {
24006caa778SAndres Beltran 		pr_err_ratelimited("Invalid icmsg negotiate\n");
24106caa778SAndres Beltran 		return false;
24206caa778SAndres Beltran 	}
24306caa778SAndres Beltran 
24446a97191SGreg Kroah-Hartman 	icmsghdrp->icmsgsize = 0x10;
24506caa778SAndres Beltran 	negop = (struct icmsg_negotiate *)&buf[ICMSG_HDR];
24646a97191SGreg Kroah-Hartman 
2476741335bSK. Y. Srinivasan 	icframe_major = negop->icframe_vercnt;
2486741335bSK. Y. Srinivasan 	icframe_minor = 0;
2496741335bSK. Y. Srinivasan 
2506741335bSK. Y. Srinivasan 	icmsg_major = negop->icmsg_vercnt;
2516741335bSK. Y. Srinivasan 	icmsg_minor = 0;
252c836d0abSK. Y. Srinivasan 
25306caa778SAndres Beltran 	/* Validate negop packet */
25406caa778SAndres Beltran 	if (icframe_major > IC_VERSION_NEGOTIATION_MAX_VER_COUNT ||
25506caa778SAndres Beltran 	    icmsg_major > IC_VERSION_NEGOTIATION_MAX_VER_COUNT ||
25606caa778SAndres Beltran 	    ICMSG_NEGOTIATE_PKT_SIZE(icframe_major, icmsg_major) > buflen) {
25706caa778SAndres Beltran 		pr_err_ratelimited("Invalid icmsg negotiate - icframe_major: %u, icmsg_major: %u\n",
25806caa778SAndres Beltran 				   icframe_major, icmsg_major);
25906caa778SAndres Beltran 		goto fw_error;
26006caa778SAndres Beltran 	}
26106caa778SAndres Beltran 
262c836d0abSK. Y. Srinivasan 	/*
263c836d0abSK. Y. Srinivasan 	 * Select the framework version number we will
264c836d0abSK. Y. Srinivasan 	 * support.
265c836d0abSK. Y. Srinivasan 	 */
266c836d0abSK. Y. Srinivasan 
267a1656454SAlex Ng 	for (i = 0; i < fw_vercnt; i++) {
268a1656454SAlex Ng 		fw_major = (fw_version[i] >> 16);
269a1656454SAlex Ng 		fw_minor = (fw_version[i] & 0xFFFF);
270a1656454SAlex Ng 
271a1656454SAlex Ng 		for (j = 0; j < negop->icframe_vercnt; j++) {
272a1656454SAlex Ng 			if ((negop->icversion_data[j].major == fw_major) &&
273a1656454SAlex Ng 			    (negop->icversion_data[j].minor == fw_minor)) {
274a1656454SAlex Ng 				icframe_major = negop->icversion_data[j].major;
275a1656454SAlex Ng 				icframe_minor = negop->icversion_data[j].minor;
2766741335bSK. Y. Srinivasan 				found_match = true;
277a1656454SAlex Ng 				break;
27846a97191SGreg Kroah-Hartman 			}
2796741335bSK. Y. Srinivasan 		}
2806741335bSK. Y. Srinivasan 
281a1656454SAlex Ng 		if (found_match)
282a1656454SAlex Ng 			break;
283a1656454SAlex Ng 	}
284a1656454SAlex Ng 
2856741335bSK. Y. Srinivasan 	if (!found_match)
2866741335bSK. Y. Srinivasan 		goto fw_error;
2876741335bSK. Y. Srinivasan 
2886741335bSK. Y. Srinivasan 	found_match = false;
28946a97191SGreg Kroah-Hartman 
290a1656454SAlex Ng 	for (i = 0; i < srv_vercnt; i++) {
291a1656454SAlex Ng 		srv_major = (srv_version[i] >> 16);
292a1656454SAlex Ng 		srv_minor = (srv_version[i] & 0xFFFF);
293a1656454SAlex Ng 
294a1656454SAlex Ng 		for (j = negop->icframe_vercnt;
295a1656454SAlex Ng 			(j < negop->icframe_vercnt + negop->icmsg_vercnt);
296a1656454SAlex Ng 			j++) {
297a1656454SAlex Ng 
298a1656454SAlex Ng 			if ((negop->icversion_data[j].major == srv_major) &&
299a1656454SAlex Ng 				(negop->icversion_data[j].minor == srv_minor)) {
300a1656454SAlex Ng 
301a1656454SAlex Ng 				icmsg_major = negop->icversion_data[j].major;
302a1656454SAlex Ng 				icmsg_minor = negop->icversion_data[j].minor;
3036741335bSK. Y. Srinivasan 				found_match = true;
304a1656454SAlex Ng 				break;
3056741335bSK. Y. Srinivasan 			}
306c836d0abSK. Y. Srinivasan 		}
307c836d0abSK. Y. Srinivasan 
308a1656454SAlex Ng 		if (found_match)
309a1656454SAlex Ng 			break;
310a1656454SAlex Ng 	}
311a1656454SAlex Ng 
312c836d0abSK. Y. Srinivasan 	/*
3136741335bSK. Y. Srinivasan 	 * Respond with the framework and service
314c836d0abSK. Y. Srinivasan 	 * version numbers we can support.
315c836d0abSK. Y. Srinivasan 	 */
3166741335bSK. Y. Srinivasan 
3176741335bSK. Y. Srinivasan fw_error:
3186741335bSK. Y. Srinivasan 	if (!found_match) {
3196741335bSK. Y. Srinivasan 		negop->icframe_vercnt = 0;
3206741335bSK. Y. Srinivasan 		negop->icmsg_vercnt = 0;
3216741335bSK. Y. Srinivasan 	} else {
32246a97191SGreg Kroah-Hartman 		negop->icframe_vercnt = 1;
32346a97191SGreg Kroah-Hartman 		negop->icmsg_vercnt = 1;
3246741335bSK. Y. Srinivasan 	}
3256741335bSK. Y. Srinivasan 
326a1656454SAlex Ng 	if (nego_fw_version)
327a1656454SAlex Ng 		*nego_fw_version = (icframe_major << 16) | icframe_minor;
328a1656454SAlex Ng 
329a1656454SAlex Ng 	if (nego_srv_version)
330a1656454SAlex Ng 		*nego_srv_version = (icmsg_major << 16) | icmsg_minor;
331a1656454SAlex Ng 
3326741335bSK. Y. Srinivasan 	negop->icversion_data[0].major = icframe_major;
3336741335bSK. Y. Srinivasan 	negop->icversion_data[0].minor = icframe_minor;
3346741335bSK. Y. Srinivasan 	negop->icversion_data[1].major = icmsg_major;
3356741335bSK. Y. Srinivasan 	negop->icversion_data[1].minor = icmsg_minor;
3366741335bSK. Y. Srinivasan 	return found_match;
33746a97191SGreg Kroah-Hartman }
338da0e9631SGreg Kroah-Hartman EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp);
33946a97191SGreg Kroah-Hartman 
34046a97191SGreg Kroah-Hartman /*
34146a97191SGreg Kroah-Hartman  * alloc_channel - Allocate and initialize a vmbus channel object
34246a97191SGreg Kroah-Hartman  */
alloc_channel(void)34346a97191SGreg Kroah-Hartman static struct vmbus_channel *alloc_channel(void)
34446a97191SGreg Kroah-Hartman {
34546a97191SGreg Kroah-Hartman 	struct vmbus_channel *channel;
34646a97191SGreg Kroah-Hartman 
34746a97191SGreg Kroah-Hartman 	channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
34846a97191SGreg Kroah-Hartman 	if (!channel)
34946a97191SGreg Kroah-Hartman 		return NULL;
35046a97191SGreg Kroah-Hartman 
3519403b66eSAndrea Parri (Microsoft) 	spin_lock_init(&channel->sched_lock);
3527fa32e5eSK. Y. Srinivasan 	init_completion(&channel->rescind_event);
353e68d2971SK. Y. Srinivasan 
354e68d2971SK. Y. Srinivasan 	INIT_LIST_HEAD(&channel->sc_list);
35546a97191SGreg Kroah-Hartman 
356631e63a9SStephen Hemminger 	tasklet_init(&channel->callback_event,
357631e63a9SStephen Hemminger 		     vmbus_on_event, (unsigned long)channel);
358631e63a9SStephen Hemminger 
35914948e39SKimberly Brown 	hv_ringbuffer_pre_init(channel);
36014948e39SKimberly Brown 
36146a97191SGreg Kroah-Hartman 	return channel;
36246a97191SGreg Kroah-Hartman }
36346a97191SGreg Kroah-Hartman 
36446a97191SGreg Kroah-Hartman /*
36546a97191SGreg Kroah-Hartman  * free_channel - Release the resources used by the vmbus channel object
36646a97191SGreg Kroah-Hartman  */
free_channel(struct vmbus_channel * channel)3679f3e28e3SGreg Kroah-Hartman static void free_channel(struct vmbus_channel *channel)
36846a97191SGreg Kroah-Hartman {
369631e63a9SStephen Hemminger 	tasklet_kill(&channel->callback_event);
37046fc1548SKimberly Brown 	vmbus_remove_channel_attr_group(channel);
3718200f208SStephen Hemminger 
372c2e5df61SStephen Hemminger 	kobject_put(&channel->kobj);
37346a97191SGreg Kroah-Hartman }
37446a97191SGreg Kroah-Hartman 
vmbus_channel_map_relid(struct vmbus_channel * channel)3758b6a877cSAndrea Parri (Microsoft) void vmbus_channel_map_relid(struct vmbus_channel *channel)
3763a28fa35SK. Y. Srinivasan {
3778b6a877cSAndrea Parri (Microsoft) 	if (WARN_ON(channel->offermsg.child_relid >= MAX_CHANNEL_RELIDS))
3788b6a877cSAndrea Parri (Microsoft) 		return;
3798b6a877cSAndrea Parri (Microsoft) 	/*
3808b6a877cSAndrea Parri (Microsoft) 	 * The mapping of the channel's relid is visible from the CPUs that
3818b6a877cSAndrea Parri (Microsoft) 	 * execute vmbus_chan_sched() by the time that vmbus_chan_sched() will
3828b6a877cSAndrea Parri (Microsoft) 	 * execute:
3838b6a877cSAndrea Parri (Microsoft) 	 *
3848b6a877cSAndrea Parri (Microsoft) 	 *  (a) In the "normal (i.e., not resuming from hibernation)" path,
385eaa03d34SAndrea Parri (Microsoft) 	 *      the full barrier in virt_store_mb() guarantees that the store
3868b6a877cSAndrea Parri (Microsoft) 	 *      is propagated to all CPUs before the add_channel_work work
3878b6a877cSAndrea Parri (Microsoft) 	 *      is queued.  In turn, add_channel_work is queued before the
3888b6a877cSAndrea Parri (Microsoft) 	 *      channel's ring buffer is allocated/initialized and the
3898b6a877cSAndrea Parri (Microsoft) 	 *      OPENCHANNEL message for the channel is sent in vmbus_open().
3908b6a877cSAndrea Parri (Microsoft) 	 *      Hyper-V won't start sending the interrupts for the channel
3918b6a877cSAndrea Parri (Microsoft) 	 *      before the OPENCHANNEL message is acked.  The memory barrier
3928b6a877cSAndrea Parri (Microsoft) 	 *      in vmbus_chan_sched() -> sync_test_and_clear_bit() ensures
3938b6a877cSAndrea Parri (Microsoft) 	 *      that vmbus_chan_sched() must find the channel's relid in
3948b6a877cSAndrea Parri (Microsoft) 	 *      recv_int_page before retrieving the channel pointer from the
3958b6a877cSAndrea Parri (Microsoft) 	 *      array of channels.
3968b6a877cSAndrea Parri (Microsoft) 	 *
397eaa03d34SAndrea Parri (Microsoft) 	 *  (b) In the "resuming from hibernation" path, the virt_store_mb()
3988b6a877cSAndrea Parri (Microsoft) 	 *      guarantees that the store is propagated to all CPUs before
3998b6a877cSAndrea Parri (Microsoft) 	 *      the VMBus connection is marked as ready for the resume event
4008b6a877cSAndrea Parri (Microsoft) 	 *      (cf. check_ready_for_resume_event()).  The interrupt handler
4018b6a877cSAndrea Parri (Microsoft) 	 *      of the VMBus driver and vmbus_chan_sched() can not run before
4028b6a877cSAndrea Parri (Microsoft) 	 *      vmbus_bus_resume() has completed execution (cf. resume_noirq).
4038b6a877cSAndrea Parri (Microsoft) 	 */
404eaa03d34SAndrea Parri (Microsoft) 	virt_store_mb(
4058b6a877cSAndrea Parri (Microsoft) 		vmbus_connection.channels[channel->offermsg.child_relid],
4068b6a877cSAndrea Parri (Microsoft) 		channel);
4073a28fa35SK. Y. Srinivasan }
4083a28fa35SK. Y. Srinivasan 
vmbus_channel_unmap_relid(struct vmbus_channel * channel)4098b6a877cSAndrea Parri (Microsoft) void vmbus_channel_unmap_relid(struct vmbus_channel *channel)
4103a28fa35SK. Y. Srinivasan {
4118b6a877cSAndrea Parri (Microsoft) 	if (WARN_ON(channel->offermsg.child_relid >= MAX_CHANNEL_RELIDS))
4128b6a877cSAndrea Parri (Microsoft) 		return;
4138b6a877cSAndrea Parri (Microsoft) 	WRITE_ONCE(
4148b6a877cSAndrea Parri (Microsoft) 		vmbus_connection.channels[channel->offermsg.child_relid],
4158b6a877cSAndrea Parri (Microsoft) 		NULL);
4163a28fa35SK. Y. Srinivasan }
41746a97191SGreg Kroah-Hartman 
vmbus_release_relid(u32 relid)418f52078cfSDexuan Cui static void vmbus_release_relid(u32 relid)
41946a97191SGreg Kroah-Hartman {
420ed6cfcc5SK. Y. Srinivasan 	struct vmbus_channel_relid_released msg;
421259cc665SVitaly Kuznetsov 	int ret;
42204a258c1SVitaly Kuznetsov 
423c8705979SK. Y. Srinivasan 	memset(&msg, 0, sizeof(struct vmbus_channel_relid_released));
424ed6cfcc5SK. Y. Srinivasan 	msg.child_relid = relid;
425c8705979SK. Y. Srinivasan 	msg.header.msgtype = CHANNELMSG_RELID_RELEASED;
426259cc665SVitaly Kuznetsov 	ret = vmbus_post_msg(&msg, sizeof(struct vmbus_channel_relid_released),
427c0bb0392SVitaly Kuznetsov 			     true);
428259cc665SVitaly Kuznetsov 
429259cc665SVitaly Kuznetsov 	trace_vmbus_release_relid(&msg, ret);
430f52078cfSDexuan Cui }
431c8705979SK. Y. Srinivasan 
hv_process_channel_removal(struct vmbus_channel * channel)432800b9329SStephen Hemminger void hv_process_channel_removal(struct vmbus_channel *channel)
433f52078cfSDexuan Cui {
4348b6a877cSAndrea Parri (Microsoft) 	lockdep_assert_held(&vmbus_connection.channel_mutex);
435192b2d78SK. Y. Srinivasan 	BUG_ON(!channel->rescind);
436800b9329SStephen Hemminger 
4378b6a877cSAndrea Parri (Microsoft) 	/*
4388b6a877cSAndrea Parri (Microsoft) 	 * hv_process_channel_removal() could find INVALID_RELID only for
4398b6a877cSAndrea Parri (Microsoft) 	 * hv_sock channels.  See the inline comments in vmbus_onoffer().
4408b6a877cSAndrea Parri (Microsoft) 	 */
4418b6a877cSAndrea Parri (Microsoft) 	WARN_ON(channel->offermsg.child_relid == INVALID_RELID &&
4428b6a877cSAndrea Parri (Microsoft) 		!is_hvsock_channel(channel));
4438b6a877cSAndrea Parri (Microsoft) 
4448b6a877cSAndrea Parri (Microsoft) 	/*
4458b6a877cSAndrea Parri (Microsoft) 	 * Upon suspend, an in-use hv_sock channel is removed from the array of
4468b6a877cSAndrea Parri (Microsoft) 	 * channels and the relid is invalidated.  After hibernation, when the
4471940f9f8SJulia Lawall 	 * user-space application destroys the channel, it's unnecessary and
4488b6a877cSAndrea Parri (Microsoft) 	 * unsafe to remove the channel from the array of channels.  See also
4498b6a877cSAndrea Parri (Microsoft) 	 * the inline comments before the call of vmbus_release_relid() below.
4508b6a877cSAndrea Parri (Microsoft) 	 */
4518b6a877cSAndrea Parri (Microsoft) 	if (channel->offermsg.child_relid != INVALID_RELID)
4528b6a877cSAndrea Parri (Microsoft) 		vmbus_channel_unmap_relid(channel);
4533a28fa35SK. Y. Srinivasan 
4548a99e501SAndrea Parri (Microsoft) 	if (channel->primary_channel == NULL)
455c8705979SK. Y. Srinivasan 		list_del(&channel->listentry);
4568a99e501SAndrea Parri (Microsoft) 	else
457565ce642SK. Y. Srinivasan 		list_del(&channel->sc_list);
458ca1c4b74SDexuan Cui 
459ca1c4b74SDexuan Cui 	/*
460afaa33daSAndrea Parri (Microsoft) 	 * If this is a "perf" channel, updates the hv_numa_map[] masks so that
461afaa33daSAndrea Parri (Microsoft) 	 * init_vp_index() can (re-)use the CPU.
462afaa33daSAndrea Parri (Microsoft) 	 */
463afaa33daSAndrea Parri (Microsoft) 	if (hv_is_perf_channel(channel))
464de96e8a0SVitaly Kuznetsov 		hv_clear_allocated_cpu(channel->target_cpu);
465afaa33daSAndrea Parri (Microsoft) 
466afaa33daSAndrea Parri (Microsoft) 	/*
467d8bd2d44SDexuan Cui 	 * Upon suspend, an in-use hv_sock channel is marked as "rescinded" and
468d8bd2d44SDexuan Cui 	 * the relid is invalidated; after hibernation, when the user-space app
469d8bd2d44SDexuan Cui 	 * destroys the channel, the relid is INVALID_RELID, and in this case
470d8bd2d44SDexuan Cui 	 * it's unnecessary and unsafe to release the old relid, since the same
471d8bd2d44SDexuan Cui 	 * relid can refer to a completely different channel now.
472d8bd2d44SDexuan Cui 	 */
473d8bd2d44SDexuan Cui 	if (channel->offermsg.child_relid != INVALID_RELID)
474800b9329SStephen Hemminger 		vmbus_release_relid(channel->offermsg.child_relid);
475638fea33SDexuan Cui 
476c8705979SK. Y. Srinivasan 	free_channel(channel);
47746a97191SGreg Kroah-Hartman }
47846a97191SGreg Kroah-Hartman 
vmbus_free_channels(void)47993e5bd06SK. Y. Srinivasan void vmbus_free_channels(void)
48093e5bd06SK. Y. Srinivasan {
481813c5b79SDexuan Cui 	struct vmbus_channel *channel, *tmp;
48293e5bd06SK. Y. Srinivasan 
483813c5b79SDexuan Cui 	list_for_each_entry_safe(channel, tmp, &vmbus_connection.chn_list,
484813c5b79SDexuan Cui 		listentry) {
48534c6801eSDexuan Cui 		/* hv_process_channel_removal() needs this */
486813c5b79SDexuan Cui 		channel->rescind = true;
487813c5b79SDexuan Cui 
48893e5bd06SK. Y. Srinivasan 		vmbus_device_unregister(channel->device_obj);
48993e5bd06SK. Y. Srinivasan 	}
49093e5bd06SK. Y. Srinivasan }
49193e5bd06SK. Y. Srinivasan 
49237c2578cSDexuan Cui /* Note: the function can run concurrently for primary/sub channels. */
vmbus_add_channel_work(struct work_struct * work)49337c2578cSDexuan Cui static void vmbus_add_channel_work(struct work_struct *work)
49446a97191SGreg Kroah-Hartman {
49537c2578cSDexuan Cui 	struct vmbus_channel *newchannel =
49637c2578cSDexuan Cui 		container_of(work, struct vmbus_channel, add_channel_work);
49737c2578cSDexuan Cui 	struct vmbus_channel *primary_channel = newchannel->primary_channel;
49885d9aa70SDexuan Cui 	int ret;
49946a97191SGreg Kroah-Hartman 
50046a97191SGreg Kroah-Hartman 	/*
50142dceebeSK. Y. Srinivasan 	 * This state is used to indicate a successful open
50242dceebeSK. Y. Srinivasan 	 * so that when we do close the channel normally, we
50337c2578cSDexuan Cui 	 * can cleanup properly.
50442dceebeSK. Y. Srinivasan 	 */
50542dceebeSK. Y. Srinivasan 	newchannel->state = CHANNEL_OPEN_STATE;
50642dceebeSK. Y. Srinivasan 
50737c2578cSDexuan Cui 	if (primary_channel != NULL) {
50837c2578cSDexuan Cui 		/* newchannel is a sub-channel. */
50937c2578cSDexuan Cui 		struct hv_device *dev = primary_channel->device_obj;
510c2e5df61SStephen Hemminger 
51150229128SDexuan Cui 		if (vmbus_add_channel_kobj(dev, newchannel))
51237c2578cSDexuan Cui 			goto err_deq_chan;
513c2e5df61SStephen Hemminger 
51437c2578cSDexuan Cui 		if (primary_channel->sc_creation_callback != NULL)
51537c2578cSDexuan Cui 			primary_channel->sc_creation_callback(newchannel);
51637c2578cSDexuan Cui 
517192b2d78SK. Y. Srinivasan 		newchannel->probe_done = true;
5188dfd3326SVitaly Kuznetsov 		return;
5198dfd3326SVitaly Kuznetsov 	}
5208dfd3326SVitaly Kuznetsov 
52142dceebeSK. Y. Srinivasan 	/*
52237c2578cSDexuan Cui 	 * Start the process of binding the primary channel to the driver
52346a97191SGreg Kroah-Hartman 	 */
52446a97191SGreg Kroah-Hartman 	newchannel->device_obj = vmbus_device_create(
52546a97191SGreg Kroah-Hartman 		&newchannel->offermsg.offer.if_type,
52646a97191SGreg Kroah-Hartman 		&newchannel->offermsg.offer.if_instance,
52746a97191SGreg Kroah-Hartman 		newchannel);
5289c3a6f7eSVitaly Kuznetsov 	if (!newchannel->device_obj)
5295b1e5b53SK. Y. Srinivasan 		goto err_deq_chan;
53046a97191SGreg Kroah-Hartman 
531afaa33daSAndrea Parri (Microsoft) 	newchannel->device_obj->device_id = newchannel->device_id;
53246a97191SGreg Kroah-Hartman 	/*
53346a97191SGreg Kroah-Hartman 	 * Add the new device to the bus. This will kick off device-driver
53446a97191SGreg Kroah-Hartman 	 * binding which eventually invokes the device driver's AddDevice()
53546a97191SGreg Kroah-Hartman 	 * method.
536f92a4b50SYang Yingliang 	 *
537f92a4b50SYang Yingliang 	 * If vmbus_device_register() fails, the 'device_obj' is freed in
538f92a4b50SYang Yingliang 	 * vmbus_device_release() as called by device_unregister() in the
539f92a4b50SYang Yingliang 	 * error path of vmbus_device_register(). In the outside error
540f92a4b50SYang Yingliang 	 * path, there's no need to free it.
54146a97191SGreg Kroah-Hartman 	 */
54285d9aa70SDexuan Cui 	ret = vmbus_device_register(newchannel->device_obj);
54385d9aa70SDexuan Cui 
54485d9aa70SDexuan Cui 	if (ret != 0) {
545d43e2fe7SDexuan Cui 		pr_err("unable to add child device object (relid %d)\n",
546d43e2fe7SDexuan Cui 			newchannel->offermsg.child_relid);
547d43e2fe7SDexuan Cui 		goto err_deq_chan;
548d43e2fe7SDexuan Cui 	}
54954a66265SK. Y. Srinivasan 
5506f3d791fSK. Y. Srinivasan 	newchannel->probe_done = true;
5519c3a6f7eSVitaly Kuznetsov 	return;
5522dd37cb8SK. Y. Srinivasan 
5535b1e5b53SK. Y. Srinivasan err_deq_chan:
554d6f591e3SDexuan Cui 	mutex_lock(&vmbus_connection.channel_mutex);
55537c2578cSDexuan Cui 
55637c2578cSDexuan Cui 	/*
55737c2578cSDexuan Cui 	 * We need to set the flag, otherwise
55837c2578cSDexuan Cui 	 * vmbus_onoffer_rescind() can be blocked.
55937c2578cSDexuan Cui 	 */
56037c2578cSDexuan Cui 	newchannel->probe_done = true;
56137c2578cSDexuan Cui 
5628a99e501SAndrea Parri (Microsoft) 	if (primary_channel == NULL)
5635b1e5b53SK. Y. Srinivasan 		list_del(&newchannel->listentry);
5648a99e501SAndrea Parri (Microsoft) 	else
56537c2578cSDexuan Cui 		list_del(&newchannel->sc_list);
56637c2578cSDexuan Cui 
5678b6a877cSAndrea Parri (Microsoft) 	/* vmbus_process_offer() has mapped the channel. */
5688b6a877cSAndrea Parri (Microsoft) 	vmbus_channel_unmap_relid(newchannel);
5695b1e5b53SK. Y. Srinivasan 
5708b6a877cSAndrea Parri (Microsoft) 	mutex_unlock(&vmbus_connection.channel_mutex);
571638fea33SDexuan Cui 
572638fea33SDexuan Cui 	vmbus_release_relid(newchannel->offermsg.child_relid);
5735b1e5b53SK. Y. Srinivasan 
5749c3a6f7eSVitaly Kuznetsov 	free_channel(newchannel);
57546a97191SGreg Kroah-Hartman }
57646a97191SGreg Kroah-Hartman 
577a119845fSK. Y. Srinivasan /*
57837c2578cSDexuan Cui  * vmbus_process_offer - Process the offer by creating a channel/device
57937c2578cSDexuan Cui  * associated with this offer
58037c2578cSDexuan Cui  */
vmbus_process_offer(struct vmbus_channel * newchannel)58137c2578cSDexuan Cui static void vmbus_process_offer(struct vmbus_channel *newchannel)
58237c2578cSDexuan Cui {
58337c2578cSDexuan Cui 	struct vmbus_channel *channel;
58437c2578cSDexuan Cui 	struct workqueue_struct *wq;
58537c2578cSDexuan Cui 	bool fnew = true;
58637c2578cSDexuan Cui 
587d570aec0SAndrea Parri (Microsoft) 	/*
588a949e86cSAndrea Parri (Microsoft) 	 * Synchronize vmbus_process_offer() and CPU hotplugging:
589d570aec0SAndrea Parri (Microsoft) 	 *
590d570aec0SAndrea Parri (Microsoft) 	 * CPU1				CPU2
591d570aec0SAndrea Parri (Microsoft) 	 *
592a949e86cSAndrea Parri (Microsoft) 	 * [vmbus_process_offer()]	[Hot removal of the CPU]
593d570aec0SAndrea Parri (Microsoft) 	 *
594a949e86cSAndrea Parri (Microsoft) 	 * CPU_READ_LOCK		CPUS_WRITE_LOCK
595a949e86cSAndrea Parri (Microsoft) 	 * LOAD cpu_online_mask		SEARCH chn_list
596a949e86cSAndrea Parri (Microsoft) 	 * STORE target_cpu		LOAD target_cpu
597a949e86cSAndrea Parri (Microsoft) 	 * INSERT chn_list		STORE cpu_online_mask
598a949e86cSAndrea Parri (Microsoft) 	 * CPUS_READ_UNLOCK		CPUS_WRITE_UNLOCK
599a949e86cSAndrea Parri (Microsoft) 	 *
600a949e86cSAndrea Parri (Microsoft) 	 * Forbids: CPU1's LOAD from *not* seing CPU2's STORE &&
601a949e86cSAndrea Parri (Microsoft) 	 *              CPU2's SEARCH from *not* seeing CPU1's INSERT
602d570aec0SAndrea Parri (Microsoft) 	 *
603d570aec0SAndrea Parri (Microsoft) 	 * Forbids: CPU2's SEARCH from seeing CPU1's INSERT &&
604d570aec0SAndrea Parri (Microsoft) 	 *              CPU2's LOAD from *not* seing CPU1's STORE
605d570aec0SAndrea Parri (Microsoft) 	 */
606a949e86cSAndrea Parri (Microsoft) 	cpus_read_lock();
607d570aec0SAndrea Parri (Microsoft) 
608a949e86cSAndrea Parri (Microsoft) 	/*
609a949e86cSAndrea Parri (Microsoft) 	 * Serializes the modifications of the chn_list list as well as
610a949e86cSAndrea Parri (Microsoft) 	 * the accesses to next_numa_node_id in init_vp_index().
611a949e86cSAndrea Parri (Microsoft) 	 */
61237c2578cSDexuan Cui 	mutex_lock(&vmbus_connection.channel_mutex);
61337c2578cSDexuan Cui 
6147c9ff3deSHaiyang Zhang 	list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
6157c9ff3deSHaiyang Zhang 		if (guid_equal(&channel->offermsg.offer.if_type,
6167c9ff3deSHaiyang Zhang 			       &newchannel->offermsg.offer.if_type) &&
6177c9ff3deSHaiyang Zhang 		    guid_equal(&channel->offermsg.offer.if_instance,
6187c9ff3deSHaiyang Zhang 			       &newchannel->offermsg.offer.if_instance)) {
6197c9ff3deSHaiyang Zhang 			fnew = false;
6207c9ff3deSHaiyang Zhang 			newchannel->primary_channel = channel;
6217c9ff3deSHaiyang Zhang 			break;
6227c9ff3deSHaiyang Zhang 		}
6237c9ff3deSHaiyang Zhang 	}
6247c9ff3deSHaiyang Zhang 
625afaa33daSAndrea Parri (Microsoft) 	init_vp_index(newchannel);
626a949e86cSAndrea Parri (Microsoft) 
627b307b389SDexuan Cui 	/* Remember the channels that should be cleaned up upon suspend. */
628b307b389SDexuan Cui 	if (is_hvsock_channel(newchannel) || is_sub_channel(newchannel))
629b307b389SDexuan Cui 		atomic_inc(&vmbus_connection.nr_chan_close_on_suspend);
630b307b389SDexuan Cui 
63137c2578cSDexuan Cui 	/*
63237c2578cSDexuan Cui 	 * Now that we have acquired the channel_mutex,
63337c2578cSDexuan Cui 	 * we can release the potentially racing rescind thread.
63437c2578cSDexuan Cui 	 */
63537c2578cSDexuan Cui 	atomic_dec(&vmbus_connection.offer_in_progress);
63637c2578cSDexuan Cui 
6378a99e501SAndrea Parri (Microsoft) 	if (fnew) {
63837c2578cSDexuan Cui 		list_add_tail(&newchannel->listentry,
63937c2578cSDexuan Cui 			      &vmbus_connection.chn_list);
6408a99e501SAndrea Parri (Microsoft) 	} else {
64137c2578cSDexuan Cui 		/*
64237c2578cSDexuan Cui 		 * Check to see if this is a valid sub-channel.
64337c2578cSDexuan Cui 		 */
64437c2578cSDexuan Cui 		if (newchannel->offermsg.offer.sub_channel_index == 0) {
64537c2578cSDexuan Cui 			mutex_unlock(&vmbus_connection.channel_mutex);
646656c5ba5SSaurabh Sengar 			cpus_read_unlock();
64737c2578cSDexuan Cui 			/*
64837c2578cSDexuan Cui 			 * Don't call free_channel(), because newchannel->kobj
64937c2578cSDexuan Cui 			 * is not initialized yet.
65037c2578cSDexuan Cui 			 */
65137c2578cSDexuan Cui 			kfree(newchannel);
65237c2578cSDexuan Cui 			WARN_ON_ONCE(1);
65337c2578cSDexuan Cui 			return;
65437c2578cSDexuan Cui 		}
65537c2578cSDexuan Cui 		/*
65637c2578cSDexuan Cui 		 * Process the sub-channel.
65737c2578cSDexuan Cui 		 */
65837c2578cSDexuan Cui 		list_add_tail(&newchannel->sc_list, &channel->sc_list);
65937c2578cSDexuan Cui 	}
66037c2578cSDexuan Cui 
6618b6a877cSAndrea Parri (Microsoft) 	vmbus_channel_map_relid(newchannel);
6628b6a877cSAndrea Parri (Microsoft) 
66337c2578cSDexuan Cui 	mutex_unlock(&vmbus_connection.channel_mutex);
664a949e86cSAndrea Parri (Microsoft) 	cpus_read_unlock();
66537c2578cSDexuan Cui 
66637c2578cSDexuan Cui 	/*
66737c2578cSDexuan Cui 	 * vmbus_process_offer() mustn't call channel->sc_creation_callback()
66837c2578cSDexuan Cui 	 * directly for sub-channels, because sc_creation_callback() ->
66937c2578cSDexuan Cui 	 * vmbus_open() may never get the host's response to the
67037c2578cSDexuan Cui 	 * OPEN_CHANNEL message (the host may rescind a channel at any time,
67137c2578cSDexuan Cui 	 * e.g. in the case of hot removing a NIC), and vmbus_onoffer_rescind()
67237c2578cSDexuan Cui 	 * may not wake up the vmbus_open() as it's blocked due to a non-zero
67337c2578cSDexuan Cui 	 * vmbus_connection.offer_in_progress, and finally we have a deadlock.
67437c2578cSDexuan Cui 	 *
67537c2578cSDexuan Cui 	 * The above is also true for primary channels, if the related device
67637c2578cSDexuan Cui 	 * drivers use sync probing mode by default.
67737c2578cSDexuan Cui 	 *
67837c2578cSDexuan Cui 	 * And, usually the handling of primary channels and sub-channels can
67937c2578cSDexuan Cui 	 * depend on each other, so we should offload them to different
68037c2578cSDexuan Cui 	 * workqueues to avoid possible deadlock, e.g. in sync-probing mode,
68137c2578cSDexuan Cui 	 * NIC1's netvsc_subchan_work() can race with NIC2's netvsc_probe() ->
68237c2578cSDexuan Cui 	 * rtnl_lock(), and causes deadlock: the former gets the rtnl_lock
68337c2578cSDexuan Cui 	 * and waits for all the sub-channels to appear, but the latter
68437c2578cSDexuan Cui 	 * can't get the rtnl_lock and this blocks the handling of
68537c2578cSDexuan Cui 	 * sub-channels.
68637c2578cSDexuan Cui 	 */
68737c2578cSDexuan Cui 	INIT_WORK(&newchannel->add_channel_work, vmbus_add_channel_work);
68837c2578cSDexuan Cui 	wq = fnew ? vmbus_connection.handle_primary_chan_wq :
68937c2578cSDexuan Cui 		    vmbus_connection.handle_sub_chan_wq;
69037c2578cSDexuan Cui 	queue_work(wq, &newchannel->add_channel_work);
69137c2578cSDexuan Cui }
69237c2578cSDexuan Cui 
69337c2578cSDexuan Cui /*
6947c9ff3deSHaiyang Zhang  * Check if CPUs used by other channels of the same device.
6957c9ff3deSHaiyang Zhang  * It should only be called by init_vp_index().
6967c9ff3deSHaiyang Zhang  */
hv_cpuself_used(u32 cpu,struct vmbus_channel * chn)6977c9ff3deSHaiyang Zhang static bool hv_cpuself_used(u32 cpu, struct vmbus_channel *chn)
6987c9ff3deSHaiyang Zhang {
6997c9ff3deSHaiyang Zhang 	struct vmbus_channel *primary = chn->primary_channel;
7007c9ff3deSHaiyang Zhang 	struct vmbus_channel *sc;
7017c9ff3deSHaiyang Zhang 
7027c9ff3deSHaiyang Zhang 	lockdep_assert_held(&vmbus_connection.channel_mutex);
7037c9ff3deSHaiyang Zhang 
7047c9ff3deSHaiyang Zhang 	if (!primary)
7057c9ff3deSHaiyang Zhang 		return false;
7067c9ff3deSHaiyang Zhang 
7077c9ff3deSHaiyang Zhang 	if (primary->target_cpu == cpu)
7087c9ff3deSHaiyang Zhang 		return true;
7097c9ff3deSHaiyang Zhang 
7107c9ff3deSHaiyang Zhang 	list_for_each_entry(sc, &primary->sc_list, sc_list)
7117c9ff3deSHaiyang Zhang 		if (sc != chn && sc->target_cpu == cpu)
7127c9ff3deSHaiyang Zhang 			return true;
7137c9ff3deSHaiyang Zhang 
7147c9ff3deSHaiyang Zhang 	return false;
7157c9ff3deSHaiyang Zhang }
7167c9ff3deSHaiyang Zhang 
7177c9ff3deSHaiyang Zhang /*
718a119845fSK. Y. Srinivasan  * We use this state to statically distribute the channel interrupt load.
719a119845fSK. Y. Srinivasan  */
7201f656ff3SK. Y. Srinivasan static int next_numa_node_id;
721a119845fSK. Y. Srinivasan 
722a119845fSK. Y. Srinivasan /*
723a6b94c6bSMichael Kelley  * We can statically distribute the incoming channel interrupt load
724a6b94c6bSMichael Kelley  * by binding a channel to VCPU.
7251f656ff3SK. Y. Srinivasan  *
726a6b94c6bSMichael Kelley  * For non-performance critical channels we assign the VMBUS_CONNECT_CPU.
727a6b94c6bSMichael Kelley  * Performance critical channels will be distributed evenly among all
728a6b94c6bSMichael Kelley  * the available NUMA nodes.  Once the node is assigned, we will assign
729a6b94c6bSMichael Kelley  * the CPU based on a simple round robin scheme.
730a119845fSK. Y. Srinivasan  */
init_vp_index(struct vmbus_channel * channel)731afaa33daSAndrea Parri (Microsoft) static void init_vp_index(struct vmbus_channel *channel)
732a119845fSK. Y. Srinivasan {
733afaa33daSAndrea Parri (Microsoft) 	bool perf_chn = hv_is_perf_channel(channel);
7347c9ff3deSHaiyang Zhang 	u32 i, ncpu = num_online_cpus();
73525355252SDexuan Cui 	cpumask_var_t available_mask;
736de96e8a0SVitaly Kuznetsov 	struct cpumask *allocated_mask;
7376640b5dfSSaurabh Sengar 	const struct cpumask *hk_mask = housekeeping_cpumask(HK_TYPE_MANAGED_IRQ);
7388ef4c4abSAndrea Parri (Microsoft) 	u32 target_cpu;
7398ef4c4abSAndrea Parri (Microsoft) 	int numa_node;
740a119845fSK. Y. Srinivasan 
741a6b94c6bSMichael Kelley 	if (!perf_chn ||
7426640b5dfSSaurabh Sengar 	    !alloc_cpumask_var(&available_mask, GFP_KERNEL) ||
7436640b5dfSSaurabh Sengar 	    cpumask_empty(hk_mask)) {
744a119845fSK. Y. Srinivasan 		/*
745a6b94c6bSMichael Kelley 		 * If the channel is not a performance critical
746d570aec0SAndrea Parri (Microsoft) 		 * channel, bind it to VMBUS_CONNECT_CPU.
747d570aec0SAndrea Parri (Microsoft) 		 * In case alloc_cpumask_var() fails, bind it to
748d570aec0SAndrea Parri (Microsoft) 		 * VMBUS_CONNECT_CPU.
7496640b5dfSSaurabh Sengar 		 * If all the cpus are isolated, bind it to
7506640b5dfSSaurabh Sengar 		 * VMBUS_CONNECT_CPU.
751a119845fSK. Y. Srinivasan 		 */
752d570aec0SAndrea Parri (Microsoft) 		channel->target_cpu = VMBUS_CONNECT_CPU;
753afaa33daSAndrea Parri (Microsoft) 		if (perf_chn)
754de96e8a0SVitaly Kuznetsov 			hv_set_allocated_cpu(VMBUS_CONNECT_CPU);
755d3ba720dSK. Y. Srinivasan 		return;
756a119845fSK. Y. Srinivasan 	}
757ce59fec8SVitaly Kuznetsov 
7587c9ff3deSHaiyang Zhang 	for (i = 1; i <= ncpu + 1; i++) {
7591f656ff3SK. Y. Srinivasan 		while (true) {
7608ef4c4abSAndrea Parri (Microsoft) 			numa_node = next_numa_node_id++;
7618ef4c4abSAndrea Parri (Microsoft) 			if (numa_node == nr_node_ids) {
7628ef4c4abSAndrea Parri (Microsoft) 				next_numa_node_id = 0;
763509879bdSK. Y. Srinivasan 				continue;
764509879bdSK. Y. Srinivasan 			}
7658ef4c4abSAndrea Parri (Microsoft) 			if (cpumask_empty(cpumask_of_node(numa_node)))
7661f656ff3SK. Y. Srinivasan 				continue;
7671f656ff3SK. Y. Srinivasan 			break;
7681f656ff3SK. Y. Srinivasan 		}
769de96e8a0SVitaly Kuznetsov 		allocated_mask = &hv_context.hv_numa_map[numa_node];
7701f656ff3SK. Y. Srinivasan 
7716640b5dfSSaurabh Sengar retry:
7726640b5dfSSaurabh Sengar 		cpumask_xor(available_mask, allocated_mask, cpumask_of_node(numa_node));
7736640b5dfSSaurabh Sengar 		cpumask_and(available_mask, available_mask, hk_mask);
7746640b5dfSSaurabh Sengar 
7756640b5dfSSaurabh Sengar 		if (cpumask_empty(available_mask)) {
776ce59fec8SVitaly Kuznetsov 			/*
7771f656ff3SK. Y. Srinivasan 			 * We have cycled through all the CPUs in the node;
778de96e8a0SVitaly Kuznetsov 			 * reset the allocated map.
779ce59fec8SVitaly Kuznetsov 			 */
780de96e8a0SVitaly Kuznetsov 			cpumask_clear(allocated_mask);
7816640b5dfSSaurabh Sengar 			goto retry;
782ce59fec8SVitaly Kuznetsov 		}
7831f656ff3SK. Y. Srinivasan 
7848ef4c4abSAndrea Parri (Microsoft) 		target_cpu = cpumask_first(available_mask);
785de96e8a0SVitaly Kuznetsov 		cpumask_set_cpu(target_cpu, allocated_mask);
78679fd8e70SVitaly Kuznetsov 
7877c9ff3deSHaiyang Zhang 		if (channel->offermsg.offer.sub_channel_index >= ncpu ||
7887c9ff3deSHaiyang Zhang 		    i > ncpu || !hv_cpuself_used(target_cpu, channel))
7897c9ff3deSHaiyang Zhang 			break;
7907c9ff3deSHaiyang Zhang 	}
7917c9ff3deSHaiyang Zhang 
7928ef4c4abSAndrea Parri (Microsoft) 	channel->target_cpu = target_cpu;
79325355252SDexuan Cui 
79425355252SDexuan Cui 	free_cpumask_var(available_mask);
795a119845fSK. Y. Srinivasan }
796a119845fSK. Y. Srinivasan 
79777db0ec8SMichael Kelley #define UNLOAD_DELAY_UNIT_MS	10		/* 10 milliseconds */
79877db0ec8SMichael Kelley #define UNLOAD_WAIT_MS		(100*1000)	/* 100 seconds */
79977db0ec8SMichael Kelley #define UNLOAD_WAIT_LOOPS	(UNLOAD_WAIT_MS/UNLOAD_DELAY_UNIT_MS)
80077db0ec8SMichael Kelley #define UNLOAD_MSG_MS		(5*1000)	/* Every 5 seconds */
80177db0ec8SMichael Kelley #define UNLOAD_MSG_LOOPS	(UNLOAD_MSG_MS/UNLOAD_DELAY_UNIT_MS)
80277db0ec8SMichael Kelley 
vmbus_wait_for_unload(void)80341571916SVitaly Kuznetsov static void vmbus_wait_for_unload(void)
80441571916SVitaly Kuznetsov {
805cd95aad5SVitaly Kuznetsov 	int cpu;
806cd95aad5SVitaly Kuznetsov 	void *page_addr;
807cd95aad5SVitaly Kuznetsov 	struct hv_message *msg;
80841571916SVitaly Kuznetsov 	struct vmbus_channel_message_header *hdr;
809911e1987SMichael Kelley 	u32 message_type, i;
81041571916SVitaly Kuznetsov 
811cd95aad5SVitaly Kuznetsov 	/*
812cd95aad5SVitaly Kuznetsov 	 * CHANNELMSG_UNLOAD_RESPONSE is always delivered to the CPU which was
813cd95aad5SVitaly Kuznetsov 	 * used for initial contact or to CPU0 depending on host version. When
814cd95aad5SVitaly Kuznetsov 	 * we're crashing on a different CPU let's hope that IRQ handler on
815cd95aad5SVitaly Kuznetsov 	 * the cpu which receives CHANNELMSG_UNLOAD_RESPONSE is still
816cd95aad5SVitaly Kuznetsov 	 * functional and vmbus_unload_response() will complete
817cd95aad5SVitaly Kuznetsov 	 * vmbus_connection.unload_event. If not, the last thing we can do is
818cd95aad5SVitaly Kuznetsov 	 * read message pages for all CPUs directly.
819911e1987SMichael Kelley 	 *
82077db0ec8SMichael Kelley 	 * Wait up to 100 seconds since an Azure host must writeback any dirty
82177db0ec8SMichael Kelley 	 * data in its disk cache before the VMbus UNLOAD request will
82277db0ec8SMichael Kelley 	 * complete. This flushing has been empirically observed to take up
82377db0ec8SMichael Kelley 	 * to 50 seconds in cases with a lot of dirty data, so allow additional
82477db0ec8SMichael Kelley 	 * leeway and for inaccuracies in mdelay(). But eventually time out so
82577db0ec8SMichael Kelley 	 * that the panic path can't get hung forever in case the response
82677db0ec8SMichael Kelley 	 * message isn't seen.
827cd95aad5SVitaly Kuznetsov 	 */
82877db0ec8SMichael Kelley 	for (i = 1; i <= UNLOAD_WAIT_LOOPS; i++) {
829cd95aad5SVitaly Kuznetsov 		if (completion_done(&vmbus_connection.unload_event))
83077db0ec8SMichael Kelley 			goto completed;
831cd95aad5SVitaly Kuznetsov 
832*320805abSMichael Kelley 		for_each_present_cpu(cpu) {
83337cdd991SStephen Hemminger 			struct hv_per_cpu_context *hv_cpu
83437cdd991SStephen Hemminger 				= per_cpu_ptr(hv_context.cpu_context, cpu);
83537cdd991SStephen Hemminger 
836*320805abSMichael Kelley 			/*
837*320805abSMichael Kelley 			 * In a CoCo VM the synic_message_page is not allocated
838*320805abSMichael Kelley 			 * in hv_synic_alloc(). Instead it is set/cleared in
839*320805abSMichael Kelley 			 * hv_synic_enable_regs() and hv_synic_disable_regs()
840*320805abSMichael Kelley 			 * such that it is set only when the CPU is online. If
841*320805abSMichael Kelley 			 * not all present CPUs are online, the message page
842*320805abSMichael Kelley 			 * might be NULL, so skip such CPUs.
843*320805abSMichael Kelley 			 */
84437cdd991SStephen Hemminger 			page_addr = hv_cpu->synic_message_page;
845*320805abSMichael Kelley 			if (!page_addr)
846*320805abSMichael Kelley 				continue;
847*320805abSMichael Kelley 
84837cdd991SStephen Hemminger 			msg = (struct hv_message *)page_addr
84937cdd991SStephen Hemminger 				+ VMBUS_MESSAGE_SINT;
850cd95aad5SVitaly Kuznetsov 
851cd95aad5SVitaly Kuznetsov 			message_type = READ_ONCE(msg->header.message_type);
852cd95aad5SVitaly Kuznetsov 			if (message_type == HVMSG_NONE)
85341571916SVitaly Kuznetsov 				continue;
854cd95aad5SVitaly Kuznetsov 
855cd95aad5SVitaly Kuznetsov 			hdr = (struct vmbus_channel_message_header *)
856cd95aad5SVitaly Kuznetsov 				msg->u.payload;
857cd95aad5SVitaly Kuznetsov 
858cd95aad5SVitaly Kuznetsov 			if (hdr->msgtype == CHANNELMSG_UNLOAD_RESPONSE)
859cd95aad5SVitaly Kuznetsov 				complete(&vmbus_connection.unload_event);
860cd95aad5SVitaly Kuznetsov 
861cd95aad5SVitaly Kuznetsov 			vmbus_signal_eom(msg, message_type);
86241571916SVitaly Kuznetsov 		}
86341571916SVitaly Kuznetsov 
86477db0ec8SMichael Kelley 		/*
86577db0ec8SMichael Kelley 		 * Give a notice periodically so someone watching the
86677db0ec8SMichael Kelley 		 * serial output won't think it is completely hung.
86777db0ec8SMichael Kelley 		 */
86877db0ec8SMichael Kelley 		if (!(i % UNLOAD_MSG_LOOPS))
86977db0ec8SMichael Kelley 			pr_notice("Waiting for VMBus UNLOAD to complete\n");
87041571916SVitaly Kuznetsov 
87177db0ec8SMichael Kelley 		mdelay(UNLOAD_DELAY_UNIT_MS);
87277db0ec8SMichael Kelley 	}
87377db0ec8SMichael Kelley 	pr_err("Continuing even though VMBus UNLOAD did not complete\n");
87477db0ec8SMichael Kelley 
87577db0ec8SMichael Kelley completed:
876cd95aad5SVitaly Kuznetsov 	/*
877cd95aad5SVitaly Kuznetsov 	 * We're crashing and already got the UNLOAD_RESPONSE, cleanup all
878cd95aad5SVitaly Kuznetsov 	 * maybe-pending messages on all CPUs to be able to receive new
879cd95aad5SVitaly Kuznetsov 	 * messages after we reconnect.
880cd95aad5SVitaly Kuznetsov 	 */
881*320805abSMichael Kelley 	for_each_present_cpu(cpu) {
88237cdd991SStephen Hemminger 		struct hv_per_cpu_context *hv_cpu
88337cdd991SStephen Hemminger 			= per_cpu_ptr(hv_context.cpu_context, cpu);
88437cdd991SStephen Hemminger 
88537cdd991SStephen Hemminger 		page_addr = hv_cpu->synic_message_page;
886*320805abSMichael Kelley 		if (!page_addr)
887*320805abSMichael Kelley 			continue;
888*320805abSMichael Kelley 
889cd95aad5SVitaly Kuznetsov 		msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
890cd95aad5SVitaly Kuznetsov 		msg->header.message_type = HVMSG_NONE;
89141571916SVitaly Kuznetsov 	}
89241571916SVitaly Kuznetsov }
89341571916SVitaly Kuznetsov 
89446a97191SGreg Kroah-Hartman /*
8952db84effSK. Y. Srinivasan  * vmbus_unload_response - Handler for the unload response.
8962db84effSK. Y. Srinivasan  */
vmbus_unload_response(struct vmbus_channel_message_header * hdr)8972db84effSK. Y. Srinivasan static void vmbus_unload_response(struct vmbus_channel_message_header *hdr)
8982db84effSK. Y. Srinivasan {
8992db84effSK. Y. Srinivasan 	/*
9002db84effSK. Y. Srinivasan 	 * This is a global event; just wakeup the waiting thread.
9012db84effSK. Y. Srinivasan 	 * Once we successfully unload, we can cleanup the monitor state.
9028c2d5e06SAndrea Parri (Microsoft) 	 *
9038c2d5e06SAndrea Parri (Microsoft) 	 * NB.  A malicious or compromised Hyper-V could send a spurious
9048c2d5e06SAndrea Parri (Microsoft) 	 * message of type CHANNELMSG_UNLOAD_RESPONSE, and trigger a call
9058c2d5e06SAndrea Parri (Microsoft) 	 * of the complete() below.  Make sure that unload_event has been
9068c2d5e06SAndrea Parri (Microsoft) 	 * initialized by the time this complete() is executed.
9072db84effSK. Y. Srinivasan 	 */
9082db84effSK. Y. Srinivasan 	complete(&vmbus_connection.unload_event);
9092db84effSK. Y. Srinivasan }
9102db84effSK. Y. Srinivasan 
vmbus_initiate_unload(bool crash)91175ff3a8aSVitaly Kuznetsov void vmbus_initiate_unload(bool crash)
9122db84effSK. Y. Srinivasan {
9132db84effSK. Y. Srinivasan 	struct vmbus_channel_message_header hdr;
9142db84effSK. Y. Srinivasan 
91574347a99STianyu Lan 	if (xchg(&vmbus_connection.conn_state, DISCONNECTED) == DISCONNECTED)
91674347a99STianyu Lan 		return;
91774347a99STianyu Lan 
9184a54243fSVitaly Kuznetsov 	/* Pre-Win2012R2 hosts don't support reconnect */
9194a54243fSVitaly Kuznetsov 	if (vmbus_proto_version < VERSION_WIN8_1)
9204a54243fSVitaly Kuznetsov 		return;
9214a54243fSVitaly Kuznetsov 
9228c2d5e06SAndrea Parri (Microsoft) 	reinit_completion(&vmbus_connection.unload_event);
9232db84effSK. Y. Srinivasan 	memset(&hdr, 0, sizeof(struct vmbus_channel_message_header));
9242db84effSK. Y. Srinivasan 	hdr.msgtype = CHANNELMSG_UNLOAD;
925c0bb0392SVitaly Kuznetsov 	vmbus_post_msg(&hdr, sizeof(struct vmbus_channel_message_header),
926c0bb0392SVitaly Kuznetsov 		       !crash);
9272db84effSK. Y. Srinivasan 
92841571916SVitaly Kuznetsov 	/*
92941571916SVitaly Kuznetsov 	 * vmbus_initiate_unload() is also called on crash and the crash can be
93041571916SVitaly Kuznetsov 	 * happening in an interrupt context, where scheduling is impossible.
93141571916SVitaly Kuznetsov 	 */
93275ff3a8aSVitaly Kuznetsov 	if (!crash)
9332db84effSK. Y. Srinivasan 		wait_for_completion(&vmbus_connection.unload_event);
93441571916SVitaly Kuznetsov 	else
93541571916SVitaly Kuznetsov 		vmbus_wait_for_unload();
9362db84effSK. Y. Srinivasan }
9372db84effSK. Y. Srinivasan 
check_ready_for_resume_event(void)938d8bd2d44SDexuan Cui static void check_ready_for_resume_event(void)
939d8bd2d44SDexuan Cui {
940d8bd2d44SDexuan Cui 	/*
941d8bd2d44SDexuan Cui 	 * If all the old primary channels have been fixed up, then it's safe
942d8bd2d44SDexuan Cui 	 * to resume.
943d8bd2d44SDexuan Cui 	 */
944d8bd2d44SDexuan Cui 	if (atomic_dec_and_test(&vmbus_connection.nr_chan_fixup_on_resume))
945d8bd2d44SDexuan Cui 		complete(&vmbus_connection.ready_for_resume_event);
946d8bd2d44SDexuan Cui }
947d8bd2d44SDexuan Cui 
vmbus_setup_channel_state(struct vmbus_channel * channel,struct vmbus_channel_offer_channel * offer)948d8bd2d44SDexuan Cui static void vmbus_setup_channel_state(struct vmbus_channel *channel,
949d8bd2d44SDexuan Cui 				      struct vmbus_channel_offer_channel *offer)
950d8bd2d44SDexuan Cui {
951d8bd2d44SDexuan Cui 	/*
952d8bd2d44SDexuan Cui 	 * Setup state for signalling the host.
953d8bd2d44SDexuan Cui 	 */
954d8bd2d44SDexuan Cui 	channel->sig_event = VMBUS_EVENT_CONNECTION_ID;
955d8bd2d44SDexuan Cui 
956d8bd2d44SDexuan Cui 	channel->is_dedicated_interrupt =
957d8bd2d44SDexuan Cui 			(offer->is_dedicated_interrupt != 0);
958d8bd2d44SDexuan Cui 	channel->sig_event = offer->connection_id;
959d8bd2d44SDexuan Cui 
960d8bd2d44SDexuan Cui 	memcpy(&channel->offermsg, offer,
961d8bd2d44SDexuan Cui 	       sizeof(struct vmbus_channel_offer_channel));
962d8bd2d44SDexuan Cui 	channel->monitor_grp = (u8)offer->monitorid / 32;
963d8bd2d44SDexuan Cui 	channel->monitor_bit = (u8)offer->monitorid % 32;
964afaa33daSAndrea Parri (Microsoft) 	channel->device_id = hv_get_dev_type(channel);
965d8bd2d44SDexuan Cui }
966d8bd2d44SDexuan Cui 
9672db84effSK. Y. Srinivasan /*
968e3ede02aSDexuan Cui  * find_primary_channel_by_offer - Get the channel object given the new offer.
969e3ede02aSDexuan Cui  * This is only used in the resume path of hibernation.
970e3ede02aSDexuan Cui  */
971e3ede02aSDexuan Cui static struct vmbus_channel *
find_primary_channel_by_offer(const struct vmbus_channel_offer_channel * offer)972e3ede02aSDexuan Cui find_primary_channel_by_offer(const struct vmbus_channel_offer_channel *offer)
973e3ede02aSDexuan Cui {
974e3ede02aSDexuan Cui 	struct vmbus_channel *channel = NULL, *iter;
975e3ede02aSDexuan Cui 	const guid_t *inst1, *inst2;
976e3ede02aSDexuan Cui 
977e3ede02aSDexuan Cui 	/* Ignore sub-channel offers. */
978e3ede02aSDexuan Cui 	if (offer->offer.sub_channel_index != 0)
979e3ede02aSDexuan Cui 		return NULL;
980e3ede02aSDexuan Cui 
981e3ede02aSDexuan Cui 	mutex_lock(&vmbus_connection.channel_mutex);
982e3ede02aSDexuan Cui 
983e3ede02aSDexuan Cui 	list_for_each_entry(iter, &vmbus_connection.chn_list, listentry) {
984e3ede02aSDexuan Cui 		inst1 = &iter->offermsg.offer.if_instance;
985e3ede02aSDexuan Cui 		inst2 = &offer->offer.if_instance;
986e3ede02aSDexuan Cui 
987e3ede02aSDexuan Cui 		if (guid_equal(inst1, inst2)) {
988e3ede02aSDexuan Cui 			channel = iter;
989e3ede02aSDexuan Cui 			break;
990e3ede02aSDexuan Cui 		}
991e3ede02aSDexuan Cui 	}
992e3ede02aSDexuan Cui 
993e3ede02aSDexuan Cui 	mutex_unlock(&vmbus_connection.channel_mutex);
994e3ede02aSDexuan Cui 
995e3ede02aSDexuan Cui 	return channel;
996e3ede02aSDexuan Cui }
997e3ede02aSDexuan Cui 
vmbus_is_valid_offer(const struct vmbus_channel_offer_channel * offer)998da795eb2SAndrea Parri (Microsoft) static bool vmbus_is_valid_offer(const struct vmbus_channel_offer_channel *offer)
99921a4e356SAndrea Parri (Microsoft) {
1000da795eb2SAndrea Parri (Microsoft) 	const guid_t *guid = &offer->offer.if_type;
100121a4e356SAndrea Parri (Microsoft) 	u16 i;
100221a4e356SAndrea Parri (Microsoft) 
100321a4e356SAndrea Parri (Microsoft) 	if (!hv_is_isolation_supported())
100421a4e356SAndrea Parri (Microsoft) 		return true;
100521a4e356SAndrea Parri (Microsoft) 
1006da795eb2SAndrea Parri (Microsoft) 	if (is_hvsock_offer(offer))
1007da795eb2SAndrea Parri (Microsoft) 		return true;
1008da795eb2SAndrea Parri (Microsoft) 
100921a4e356SAndrea Parri (Microsoft) 	for (i = 0; i < ARRAY_SIZE(vmbus_devs); i++) {
101021a4e356SAndrea Parri (Microsoft) 		if (guid_equal(guid, &vmbus_devs[i].guid))
101121a4e356SAndrea Parri (Microsoft) 			return vmbus_devs[i].allowed_in_isolated;
101221a4e356SAndrea Parri (Microsoft) 	}
101321a4e356SAndrea Parri (Microsoft) 	return false;
101421a4e356SAndrea Parri (Microsoft) }
101521a4e356SAndrea Parri (Microsoft) 
1016e3ede02aSDexuan Cui /*
101746a97191SGreg Kroah-Hartman  * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
101846a97191SGreg Kroah-Hartman  *
101946a97191SGreg Kroah-Hartman  */
vmbus_onoffer(struct vmbus_channel_message_header * hdr)102046a97191SGreg Kroah-Hartman static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
102146a97191SGreg Kroah-Hartman {
102246a97191SGreg Kroah-Hartman 	struct vmbus_channel_offer_channel *offer;
1023e3ede02aSDexuan Cui 	struct vmbus_channel *oldchannel, *newchannel;
1024e3ede02aSDexuan Cui 	size_t offer_sz;
102546a97191SGreg Kroah-Hartman 
102646a97191SGreg Kroah-Hartman 	offer = (struct vmbus_channel_offer_channel *)hdr;
102746a97191SGreg Kroah-Hartman 
102803b81e66SVitaly Kuznetsov 	trace_vmbus_onoffer(offer);
102903b81e66SVitaly Kuznetsov 
1030da795eb2SAndrea Parri (Microsoft) 	if (!vmbus_is_valid_offer(offer)) {
103121a4e356SAndrea Parri (Microsoft) 		pr_err_ratelimited("Invalid offer %d from the host supporting isolation\n",
103221a4e356SAndrea Parri (Microsoft) 				   offer->child_relid);
103321a4e356SAndrea Parri (Microsoft) 		atomic_dec(&vmbus_connection.offer_in_progress);
103421a4e356SAndrea Parri (Microsoft) 		return;
103521a4e356SAndrea Parri (Microsoft) 	}
103621a4e356SAndrea Parri (Microsoft) 
1037e3ede02aSDexuan Cui 	oldchannel = find_primary_channel_by_offer(offer);
1038e3ede02aSDexuan Cui 
1039e3ede02aSDexuan Cui 	if (oldchannel != NULL) {
1040e3ede02aSDexuan Cui 		/*
1041d8bd2d44SDexuan Cui 		 * We're resuming from hibernation: all the sub-channel and
1042d8bd2d44SDexuan Cui 		 * hv_sock channels we had before the hibernation should have
1043d8bd2d44SDexuan Cui 		 * been cleaned up, and now we must be seeing a re-offered
1044d8bd2d44SDexuan Cui 		 * primary channel that we had before the hibernation.
1045e3ede02aSDexuan Cui 		 */
1046e3ede02aSDexuan Cui 
10478b6a877cSAndrea Parri (Microsoft) 		/*
10488b6a877cSAndrea Parri (Microsoft) 		 * { Initially: channel relid = INVALID_RELID,
10498b6a877cSAndrea Parri (Microsoft) 		 *		channels[valid_relid] = NULL }
10508b6a877cSAndrea Parri (Microsoft) 		 *
10518b6a877cSAndrea Parri (Microsoft) 		 * CPU1					CPU2
10528b6a877cSAndrea Parri (Microsoft) 		 *
10538b6a877cSAndrea Parri (Microsoft) 		 * [vmbus_onoffer()]			[vmbus_device_release()]
10548b6a877cSAndrea Parri (Microsoft) 		 *
10558b6a877cSAndrea Parri (Microsoft) 		 * LOCK channel_mutex			LOCK channel_mutex
10568b6a877cSAndrea Parri (Microsoft) 		 * STORE channel relid = valid_relid	LOAD r1 = channel relid
10578b6a877cSAndrea Parri (Microsoft) 		 * MAP_RELID channel			if (r1 != INVALID_RELID)
10588b6a877cSAndrea Parri (Microsoft) 		 * UNLOCK channel_mutex			  UNMAP_RELID channel
10598b6a877cSAndrea Parri (Microsoft) 		 *					UNLOCK channel_mutex
10608b6a877cSAndrea Parri (Microsoft) 		 *
10618b6a877cSAndrea Parri (Microsoft) 		 * Forbids: r1 == valid_relid &&
10628b6a877cSAndrea Parri (Microsoft) 		 *              channels[valid_relid] == channel
10638b6a877cSAndrea Parri (Microsoft) 		 *
10648b6a877cSAndrea Parri (Microsoft) 		 * Note.  r1 can be INVALID_RELID only for an hv_sock channel.
10658b6a877cSAndrea Parri (Microsoft) 		 * None of the hv_sock channels which were present before the
10668b6a877cSAndrea Parri (Microsoft) 		 * suspend are re-offered upon the resume.  See the WARN_ON()
10678b6a877cSAndrea Parri (Microsoft) 		 * in hv_process_channel_removal().
10688b6a877cSAndrea Parri (Microsoft) 		 */
10698b6a877cSAndrea Parri (Microsoft) 		mutex_lock(&vmbus_connection.channel_mutex);
10708b6a877cSAndrea Parri (Microsoft) 
10718b6a877cSAndrea Parri (Microsoft) 		atomic_dec(&vmbus_connection.offer_in_progress);
10728b6a877cSAndrea Parri (Microsoft) 
1073d8bd2d44SDexuan Cui 		WARN_ON(oldchannel->offermsg.child_relid != INVALID_RELID);
1074d8bd2d44SDexuan Cui 		/* Fix up the relid. */
1075d8bd2d44SDexuan Cui 		oldchannel->offermsg.child_relid = offer->child_relid;
1076d8bd2d44SDexuan Cui 
1077d8bd2d44SDexuan Cui 		offer_sz = sizeof(*offer);
10788b6a877cSAndrea Parri (Microsoft) 		if (memcmp(offer, &oldchannel->offermsg, offer_sz) != 0) {
1079d8bd2d44SDexuan Cui 			/*
10808b6a877cSAndrea Parri (Microsoft) 			 * This is not an error, since the host can also change
10818b6a877cSAndrea Parri (Microsoft) 			 * the other field(s) of the offer, e.g. on WS RS5
10828b6a877cSAndrea Parri (Microsoft) 			 * (Build 17763), the offer->connection_id of the
10838b6a877cSAndrea Parri (Microsoft) 			 * Mellanox VF vmbus device can change when the host
10848b6a877cSAndrea Parri (Microsoft) 			 * reoffers the device upon resume.
1085d8bd2d44SDexuan Cui 			 */
1086d8bd2d44SDexuan Cui 			pr_debug("vmbus offer changed: relid=%d\n",
1087e3ede02aSDexuan Cui 				 offer->child_relid);
1088e3ede02aSDexuan Cui 
10898b6a877cSAndrea Parri (Microsoft) 			print_hex_dump_debug("Old vmbus offer: ",
10908b6a877cSAndrea Parri (Microsoft) 					     DUMP_PREFIX_OFFSET, 16, 4,
10918b6a877cSAndrea Parri (Microsoft) 					     &oldchannel->offermsg, offer_sz,
1092e3ede02aSDexuan Cui 					     false);
10938b6a877cSAndrea Parri (Microsoft) 			print_hex_dump_debug("New vmbus offer: ",
10948b6a877cSAndrea Parri (Microsoft) 					     DUMP_PREFIX_OFFSET, 16, 4,
10958b6a877cSAndrea Parri (Microsoft) 					     offer, offer_sz, false);
1096d8bd2d44SDexuan Cui 
1097d8bd2d44SDexuan Cui 			/* Fix up the old channel. */
1098d8bd2d44SDexuan Cui 			vmbus_setup_channel_state(oldchannel, offer);
10998b6a877cSAndrea Parri (Microsoft) 		}
1100d8bd2d44SDexuan Cui 
11018b6a877cSAndrea Parri (Microsoft) 		/* Add the channel back to the array of channels. */
11028b6a877cSAndrea Parri (Microsoft) 		vmbus_channel_map_relid(oldchannel);
1103d8bd2d44SDexuan Cui 		check_ready_for_resume_event();
1104d8bd2d44SDexuan Cui 
11058b6a877cSAndrea Parri (Microsoft) 		mutex_unlock(&vmbus_connection.channel_mutex);
1106e3ede02aSDexuan Cui 		return;
1107e3ede02aSDexuan Cui 	}
1108e3ede02aSDexuan Cui 
110946a97191SGreg Kroah-Hartman 	/* Allocate the channel object and save this offer. */
111046a97191SGreg Kroah-Hartman 	newchannel = alloc_channel();
111146a97191SGreg Kroah-Hartman 	if (!newchannel) {
11129a547602SK. Y. Srinivasan 		vmbus_release_relid(offer->child_relid);
111354a66265SK. Y. Srinivasan 		atomic_dec(&vmbus_connection.offer_in_progress);
111446a97191SGreg Kroah-Hartman 		pr_err("Unable to allocate channel object\n");
111546a97191SGreg Kroah-Hartman 		return;
111646a97191SGreg Kroah-Hartman 	}
111746a97191SGreg Kroah-Hartman 
1118d8bd2d44SDexuan Cui 	vmbus_setup_channel_state(newchannel, offer);
111946a97191SGreg Kroah-Hartman 
11202dd37cb8SK. Y. Srinivasan 	vmbus_process_offer(newchannel);
112146a97191SGreg Kroah-Hartman }
112246a97191SGreg Kroah-Hartman 
check_ready_for_suspend_event(void)1123b307b389SDexuan Cui static void check_ready_for_suspend_event(void)
1124b307b389SDexuan Cui {
1125b307b389SDexuan Cui 	/*
1126b307b389SDexuan Cui 	 * If all the sub-channels or hv_sock channels have been cleaned up,
1127b307b389SDexuan Cui 	 * then it's safe to suspend.
1128b307b389SDexuan Cui 	 */
1129b307b389SDexuan Cui 	if (atomic_dec_and_test(&vmbus_connection.nr_chan_close_on_suspend))
1130b307b389SDexuan Cui 		complete(&vmbus_connection.ready_for_suspend_event);
1131b307b389SDexuan Cui }
1132b307b389SDexuan Cui 
113346a97191SGreg Kroah-Hartman /*
113446a97191SGreg Kroah-Hartman  * vmbus_onoffer_rescind - Rescind offer handler.
113546a97191SGreg Kroah-Hartman  *
113646a97191SGreg Kroah-Hartman  * We queue a work item to process this offer synchronously
113746a97191SGreg Kroah-Hartman  */
vmbus_onoffer_rescind(struct vmbus_channel_message_header * hdr)113846a97191SGreg Kroah-Hartman static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
113946a97191SGreg Kroah-Hartman {
114046a97191SGreg Kroah-Hartman 	struct vmbus_channel_rescind_offer *rescind;
114146a97191SGreg Kroah-Hartman 	struct vmbus_channel *channel;
1142d43e2fe7SDexuan Cui 	struct device *dev;
1143b307b389SDexuan Cui 	bool clean_up_chan_for_suspend;
114446a97191SGreg Kroah-Hartman 
114546a97191SGreg Kroah-Hartman 	rescind = (struct vmbus_channel_rescind_offer *)hdr;
114685d9aa70SDexuan Cui 
11475175167cSVitaly Kuznetsov 	trace_vmbus_onoffer_rescind(rescind);
11485175167cSVitaly Kuznetsov 
114954a66265SK. Y. Srinivasan 	/*
115054a66265SK. Y. Srinivasan 	 * The offer msg and the corresponding rescind msg
115154a66265SK. Y. Srinivasan 	 * from the host are guranteed to be ordered -
115254a66265SK. Y. Srinivasan 	 * offer comes in first and then the rescind.
115354a66265SK. Y. Srinivasan 	 * Since we process these events in work elements,
115454a66265SK. Y. Srinivasan 	 * and with preemption, we may end up processing
1155b9fa1b87SAndrea Parri (Microsoft) 	 * the events out of order.  We rely on the synchronization
1156b9fa1b87SAndrea Parri (Microsoft) 	 * provided by offer_in_progress and by channel_mutex for
1157b9fa1b87SAndrea Parri (Microsoft) 	 * ordering these events:
1158b9fa1b87SAndrea Parri (Microsoft) 	 *
1159b9fa1b87SAndrea Parri (Microsoft) 	 * { Initially: offer_in_progress = 1 }
1160b9fa1b87SAndrea Parri (Microsoft) 	 *
1161b9fa1b87SAndrea Parri (Microsoft) 	 * CPU1				CPU2
1162b9fa1b87SAndrea Parri (Microsoft) 	 *
11638b6a877cSAndrea Parri (Microsoft) 	 * [vmbus_onoffer()]		[vmbus_onoffer_rescind()]
1164b9fa1b87SAndrea Parri (Microsoft) 	 *
1165b9fa1b87SAndrea Parri (Microsoft) 	 * LOCK channel_mutex		WAIT_ON offer_in_progress == 0
1166b9fa1b87SAndrea Parri (Microsoft) 	 * DECREMENT offer_in_progress	LOCK channel_mutex
11678b6a877cSAndrea Parri (Microsoft) 	 * STORE channels[]		LOAD channels[]
1168b9fa1b87SAndrea Parri (Microsoft) 	 * UNLOCK channel_mutex		UNLOCK channel_mutex
1169b9fa1b87SAndrea Parri (Microsoft) 	 *
11708b6a877cSAndrea Parri (Microsoft) 	 * Forbids: CPU2's LOAD from *not* seeing CPU1's STORE
117154a66265SK. Y. Srinivasan 	 */
117254a66265SK. Y. Srinivasan 
117354a66265SK. Y. Srinivasan 	while (atomic_read(&vmbus_connection.offer_in_progress) != 0) {
117454a66265SK. Y. Srinivasan 		/*
117554a66265SK. Y. Srinivasan 		 * We wait here until any channel offer is currently
117654a66265SK. Y. Srinivasan 		 * being processed.
117754a66265SK. Y. Srinivasan 		 */
117854a66265SK. Y. Srinivasan 		msleep(1);
117954a66265SK. Y. Srinivasan 	}
118054a66265SK. Y. Srinivasan 
118185d9aa70SDexuan Cui 	mutex_lock(&vmbus_connection.channel_mutex);
1182d43e2fe7SDexuan Cui 	channel = relid2channel(rescind->child_relid);
1183e4d221b4SAndrea Parri (Microsoft) 	if (channel != NULL) {
1184e4d221b4SAndrea Parri (Microsoft) 		/*
1185e4d221b4SAndrea Parri (Microsoft) 		 * Guarantee that no other instance of vmbus_onoffer_rescind()
1186e4d221b4SAndrea Parri (Microsoft) 		 * has got a reference to the channel object.  Synchronize on
1187e4d221b4SAndrea Parri (Microsoft) 		 * &vmbus_connection.channel_mutex.
1188e4d221b4SAndrea Parri (Microsoft) 		 */
1189e4d221b4SAndrea Parri (Microsoft) 		if (channel->rescind_ref) {
1190e4d221b4SAndrea Parri (Microsoft) 			mutex_unlock(&vmbus_connection.channel_mutex);
1191e4d221b4SAndrea Parri (Microsoft) 			return;
1192e4d221b4SAndrea Parri (Microsoft) 		}
1193e4d221b4SAndrea Parri (Microsoft) 		channel->rescind_ref = true;
1194e4d221b4SAndrea Parri (Microsoft) 	}
119554a66265SK. Y. Srinivasan 	mutex_unlock(&vmbus_connection.channel_mutex);
119646a97191SGreg Kroah-Hartman 
11972dd37cb8SK. Y. Srinivasan 	if (channel == NULL) {
1198f52078cfSDexuan Cui 		/*
119954a66265SK. Y. Srinivasan 		 * We failed in processing the offer message;
120054a66265SK. Y. Srinivasan 		 * we would have cleaned up the relid in that
120154a66265SK. Y. Srinivasan 		 * failure path.
1202f52078cfSDexuan Cui 		 */
120354a66265SK. Y. Srinivasan 		return;
12042dd37cb8SK. Y. Srinivasan 	}
120546a97191SGreg Kroah-Hartman 
1206b307b389SDexuan Cui 	clean_up_chan_for_suspend = is_hvsock_channel(channel) ||
1207b307b389SDexuan Cui 				    is_sub_channel(channel);
12086f3d791fSK. Y. Srinivasan 	/*
1209d3b26dd7SDexuan Cui 	 * Before setting channel->rescind in vmbus_rescind_cleanup(), we
1210d3b26dd7SDexuan Cui 	 * should make sure the channel callback is not running any more.
1211d3b26dd7SDexuan Cui 	 */
1212d3b26dd7SDexuan Cui 	vmbus_reset_channel_cb(channel);
1213d3b26dd7SDexuan Cui 
1214d3b26dd7SDexuan Cui 	/*
12156f3d791fSK. Y. Srinivasan 	 * Now wait for offer handling to complete.
12166f3d791fSK. Y. Srinivasan 	 */
12177fa32e5eSK. Y. Srinivasan 	vmbus_rescind_cleanup(channel);
12186f3d791fSK. Y. Srinivasan 	while (READ_ONCE(channel->probe_done) == false) {
12196f3d791fSK. Y. Srinivasan 		/*
12206f3d791fSK. Y. Srinivasan 		 * We wait here until any channel offer is currently
12216f3d791fSK. Y. Srinivasan 		 * being processed.
12226f3d791fSK. Y. Srinivasan 		 */
12236f3d791fSK. Y. Srinivasan 		msleep(1);
12246f3d791fSK. Y. Srinivasan 	}
12256f3d791fSK. Y. Srinivasan 
12266f3d791fSK. Y. Srinivasan 	/*
12276f3d791fSK. Y. Srinivasan 	 * At this point, the rescind handling can proceed safely.
12286f3d791fSK. Y. Srinivasan 	 */
12296f3d791fSK. Y. Srinivasan 
1230d43e2fe7SDexuan Cui 	if (channel->device_obj) {
1231499e8401SDexuan Cui 		if (channel->chn_rescind_callback) {
1232499e8401SDexuan Cui 			channel->chn_rescind_callback(channel);
1233b307b389SDexuan Cui 
1234b307b389SDexuan Cui 			if (clean_up_chan_for_suspend)
1235b307b389SDexuan Cui 				check_ready_for_suspend_event();
1236b307b389SDexuan Cui 
123754a66265SK. Y. Srinivasan 			return;
1238499e8401SDexuan Cui 		}
12392dd37cb8SK. Y. Srinivasan 		/*
1240d43e2fe7SDexuan Cui 		 * We will have to unregister this device from the
1241d43e2fe7SDexuan Cui 		 * driver core.
12422dd37cb8SK. Y. Srinivasan 		 */
1243d43e2fe7SDexuan Cui 		dev = get_device(&channel->device_obj->device);
1244d43e2fe7SDexuan Cui 		if (dev) {
1245d43e2fe7SDexuan Cui 			vmbus_device_unregister(channel->device_obj);
1246d43e2fe7SDexuan Cui 			put_device(dev);
12472dd37cb8SK. Y. Srinivasan 		}
1248e3fa4b74SAndrea Parri (Microsoft) 	} else if (channel->primary_channel != NULL) {
124954a66265SK. Y. Srinivasan 		/*
125054a66265SK. Y. Srinivasan 		 * Sub-channel is being rescinded. Following is the channel
125154a66265SK. Y. Srinivasan 		 * close sequence when initiated from the driveri (refer to
125254a66265SK. Y. Srinivasan 		 * vmbus_close() for details):
125354a66265SK. Y. Srinivasan 		 * 1. Close all sub-channels first
125454a66265SK. Y. Srinivasan 		 * 2. Then close the primary channel.
125554a66265SK. Y. Srinivasan 		 */
1256192b2d78SK. Y. Srinivasan 		mutex_lock(&vmbus_connection.channel_mutex);
125754a66265SK. Y. Srinivasan 		if (channel->state == CHANNEL_OPEN_STATE) {
125854a66265SK. Y. Srinivasan 			/*
125954a66265SK. Y. Srinivasan 			 * The channel is currently not open;
126054a66265SK. Y. Srinivasan 			 * it is safe for us to cleanup the channel.
126154a66265SK. Y. Srinivasan 			 */
1262800b9329SStephen Hemminger 			hv_process_channel_removal(channel);
12637fa32e5eSK. Y. Srinivasan 		} else {
12647fa32e5eSK. Y. Srinivasan 			complete(&channel->rescind_event);
126546a97191SGreg Kroah-Hartman 		}
1266192b2d78SK. Y. Srinivasan 		mutex_unlock(&vmbus_connection.channel_mutex);
126754a66265SK. Y. Srinivasan 	}
1268b307b389SDexuan Cui 
1269b307b389SDexuan Cui 	/* The "channel" may have been freed. Do not access it any longer. */
1270b307b389SDexuan Cui 
1271b307b389SDexuan Cui 	if (clean_up_chan_for_suspend)
1272b307b389SDexuan Cui 		check_ready_for_suspend_event();
127354a66265SK. Y. Srinivasan }
127446a97191SGreg Kroah-Hartman 
vmbus_hvsock_device_unregister(struct vmbus_channel * channel)127585d9aa70SDexuan Cui void vmbus_hvsock_device_unregister(struct vmbus_channel *channel)
127685d9aa70SDexuan Cui {
127785d9aa70SDexuan Cui 	BUG_ON(!is_hvsock_channel(channel));
127885d9aa70SDexuan Cui 
12799d35593bSDexuan Cui 	/* We always get a rescind msg when a connection is closed. */
12809d35593bSDexuan Cui 	while (!READ_ONCE(channel->probe_done) || !READ_ONCE(channel->rescind))
12819d35593bSDexuan Cui 		msleep(1);
12829d35593bSDexuan Cui 
128385d9aa70SDexuan Cui 	vmbus_device_unregister(channel->device_obj);
128485d9aa70SDexuan Cui }
128585d9aa70SDexuan Cui EXPORT_SYMBOL_GPL(vmbus_hvsock_device_unregister);
128685d9aa70SDexuan Cui 
128785d9aa70SDexuan Cui 
128846a97191SGreg Kroah-Hartman /*
128946a97191SGreg Kroah-Hartman  * vmbus_onoffers_delivered -
129046a97191SGreg Kroah-Hartman  * This is invoked when all offers have been delivered.
129146a97191SGreg Kroah-Hartman  *
129246a97191SGreg Kroah-Hartman  * Nothing to do here.
129346a97191SGreg Kroah-Hartman  */
vmbus_onoffers_delivered(struct vmbus_channel_message_header * hdr)129446a97191SGreg Kroah-Hartman static void vmbus_onoffers_delivered(
129546a97191SGreg Kroah-Hartman 			struct vmbus_channel_message_header *hdr)
129646a97191SGreg Kroah-Hartman {
129746a97191SGreg Kroah-Hartman }
129846a97191SGreg Kroah-Hartman 
129946a97191SGreg Kroah-Hartman /*
130046a97191SGreg Kroah-Hartman  * vmbus_onopen_result - Open result handler.
130146a97191SGreg Kroah-Hartman  *
130246a97191SGreg Kroah-Hartman  * This is invoked when we received a response to our channel open request.
130346a97191SGreg Kroah-Hartman  * Find the matching request, copy the response and signal the requesting
130446a97191SGreg Kroah-Hartman  * thread.
130546a97191SGreg Kroah-Hartman  */
vmbus_onopen_result(struct vmbus_channel_message_header * hdr)130646a97191SGreg Kroah-Hartman static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
130746a97191SGreg Kroah-Hartman {
130846a97191SGreg Kroah-Hartman 	struct vmbus_channel_open_result *result;
130946a97191SGreg Kroah-Hartman 	struct vmbus_channel_msginfo *msginfo;
131046a97191SGreg Kroah-Hartman 	struct vmbus_channel_message_header *requestheader;
131146a97191SGreg Kroah-Hartman 	struct vmbus_channel_open_channel *openmsg;
131246a97191SGreg Kroah-Hartman 	unsigned long flags;
131346a97191SGreg Kroah-Hartman 
131446a97191SGreg Kroah-Hartman 	result = (struct vmbus_channel_open_result *)hdr;
131546a97191SGreg Kroah-Hartman 
13169f23ce1aSVitaly Kuznetsov 	trace_vmbus_onopen_result(result);
13179f23ce1aSVitaly Kuznetsov 
131846a97191SGreg Kroah-Hartman 	/*
131946a97191SGreg Kroah-Hartman 	 * Find the open msg, copy the result and signal/unblock the wait event
132046a97191SGreg Kroah-Hartman 	 */
132146a97191SGreg Kroah-Hartman 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
132246a97191SGreg Kroah-Hartman 
132346a97191SGreg Kroah-Hartman 	list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
132446a97191SGreg Kroah-Hartman 				msglistentry) {
132546a97191SGreg Kroah-Hartman 		requestheader =
132646a97191SGreg Kroah-Hartman 			(struct vmbus_channel_message_header *)msginfo->msg;
132746a97191SGreg Kroah-Hartman 
132846a97191SGreg Kroah-Hartman 		if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
132946a97191SGreg Kroah-Hartman 			openmsg =
133046a97191SGreg Kroah-Hartman 			(struct vmbus_channel_open_channel *)msginfo->msg;
133146a97191SGreg Kroah-Hartman 			if (openmsg->child_relid == result->child_relid &&
133246a97191SGreg Kroah-Hartman 			    openmsg->openid == result->openid) {
133346a97191SGreg Kroah-Hartman 				memcpy(&msginfo->response.open_result,
133446a97191SGreg Kroah-Hartman 				       result,
133546a97191SGreg Kroah-Hartman 				       sizeof(
133646a97191SGreg Kroah-Hartman 					struct vmbus_channel_open_result));
133746a97191SGreg Kroah-Hartman 				complete(&msginfo->waitevent);
133846a97191SGreg Kroah-Hartman 				break;
133946a97191SGreg Kroah-Hartman 			}
134046a97191SGreg Kroah-Hartman 		}
134146a97191SGreg Kroah-Hartman 	}
134246a97191SGreg Kroah-Hartman 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
134346a97191SGreg Kroah-Hartman }
134446a97191SGreg Kroah-Hartman 
134546a97191SGreg Kroah-Hartman /*
134646a97191SGreg Kroah-Hartman  * vmbus_ongpadl_created - GPADL created handler.
134746a97191SGreg Kroah-Hartman  *
134846a97191SGreg Kroah-Hartman  * This is invoked when we received a response to our gpadl create request.
134946a97191SGreg Kroah-Hartman  * Find the matching request, copy the response and signal the requesting
135046a97191SGreg Kroah-Hartman  * thread.
135146a97191SGreg Kroah-Hartman  */
vmbus_ongpadl_created(struct vmbus_channel_message_header * hdr)135246a97191SGreg Kroah-Hartman static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
135346a97191SGreg Kroah-Hartman {
135446a97191SGreg Kroah-Hartman 	struct vmbus_channel_gpadl_created *gpadlcreated;
135546a97191SGreg Kroah-Hartman 	struct vmbus_channel_msginfo *msginfo;
135646a97191SGreg Kroah-Hartman 	struct vmbus_channel_message_header *requestheader;
135746a97191SGreg Kroah-Hartman 	struct vmbus_channel_gpadl_header *gpadlheader;
135846a97191SGreg Kroah-Hartman 	unsigned long flags;
135946a97191SGreg Kroah-Hartman 
136046a97191SGreg Kroah-Hartman 	gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
136146a97191SGreg Kroah-Hartman 
1362a13bf0eaSVitaly Kuznetsov 	trace_vmbus_ongpadl_created(gpadlcreated);
1363a13bf0eaSVitaly Kuznetsov 
136446a97191SGreg Kroah-Hartman 	/*
136546a97191SGreg Kroah-Hartman 	 * Find the establish msg, copy the result and signal/unblock the wait
136646a97191SGreg Kroah-Hartman 	 * event
136746a97191SGreg Kroah-Hartman 	 */
136846a97191SGreg Kroah-Hartman 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
136946a97191SGreg Kroah-Hartman 
137046a97191SGreg Kroah-Hartman 	list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
137146a97191SGreg Kroah-Hartman 				msglistentry) {
137246a97191SGreg Kroah-Hartman 		requestheader =
137346a97191SGreg Kroah-Hartman 			(struct vmbus_channel_message_header *)msginfo->msg;
137446a97191SGreg Kroah-Hartman 
137546a97191SGreg Kroah-Hartman 		if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
137646a97191SGreg Kroah-Hartman 			gpadlheader =
137746a97191SGreg Kroah-Hartman 			(struct vmbus_channel_gpadl_header *)requestheader;
137846a97191SGreg Kroah-Hartman 
137946a97191SGreg Kroah-Hartman 			if ((gpadlcreated->child_relid ==
138046a97191SGreg Kroah-Hartman 			     gpadlheader->child_relid) &&
138146a97191SGreg Kroah-Hartman 			    (gpadlcreated->gpadl == gpadlheader->gpadl)) {
138246a97191SGreg Kroah-Hartman 				memcpy(&msginfo->response.gpadl_created,
138346a97191SGreg Kroah-Hartman 				       gpadlcreated,
138446a97191SGreg Kroah-Hartman 				       sizeof(
138546a97191SGreg Kroah-Hartman 					struct vmbus_channel_gpadl_created));
138646a97191SGreg Kroah-Hartman 				complete(&msginfo->waitevent);
138746a97191SGreg Kroah-Hartman 				break;
138846a97191SGreg Kroah-Hartman 			}
138946a97191SGreg Kroah-Hartman 		}
139046a97191SGreg Kroah-Hartman 	}
139146a97191SGreg Kroah-Hartman 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
139246a97191SGreg Kroah-Hartman }
139346a97191SGreg Kroah-Hartman 
139446a97191SGreg Kroah-Hartman /*
1395870ced05SAndrea Parri (Microsoft)  * vmbus_onmodifychannel_response - Modify Channel response handler.
1396870ced05SAndrea Parri (Microsoft)  *
1397870ced05SAndrea Parri (Microsoft)  * This is invoked when we received a response to our channel modify request.
1398870ced05SAndrea Parri (Microsoft)  * Find the matching request, copy the response and signal the requesting thread.
1399870ced05SAndrea Parri (Microsoft)  */
vmbus_onmodifychannel_response(struct vmbus_channel_message_header * hdr)1400870ced05SAndrea Parri (Microsoft) static void vmbus_onmodifychannel_response(struct vmbus_channel_message_header *hdr)
1401870ced05SAndrea Parri (Microsoft) {
1402870ced05SAndrea Parri (Microsoft) 	struct vmbus_channel_modifychannel_response *response;
1403870ced05SAndrea Parri (Microsoft) 	struct vmbus_channel_msginfo *msginfo;
1404870ced05SAndrea Parri (Microsoft) 	unsigned long flags;
1405870ced05SAndrea Parri (Microsoft) 
1406870ced05SAndrea Parri (Microsoft) 	response = (struct vmbus_channel_modifychannel_response *)hdr;
1407870ced05SAndrea Parri (Microsoft) 
1408870ced05SAndrea Parri (Microsoft) 	trace_vmbus_onmodifychannel_response(response);
1409870ced05SAndrea Parri (Microsoft) 
1410870ced05SAndrea Parri (Microsoft) 	/*
1411870ced05SAndrea Parri (Microsoft) 	 * Find the modify msg, copy the response and signal/unblock the wait event.
1412870ced05SAndrea Parri (Microsoft) 	 */
1413870ced05SAndrea Parri (Microsoft) 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
1414870ced05SAndrea Parri (Microsoft) 
1415870ced05SAndrea Parri (Microsoft) 	list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, msglistentry) {
1416870ced05SAndrea Parri (Microsoft) 		struct vmbus_channel_message_header *responseheader =
1417870ced05SAndrea Parri (Microsoft) 				(struct vmbus_channel_message_header *)msginfo->msg;
1418870ced05SAndrea Parri (Microsoft) 
1419870ced05SAndrea Parri (Microsoft) 		if (responseheader->msgtype == CHANNELMSG_MODIFYCHANNEL) {
1420870ced05SAndrea Parri (Microsoft) 			struct vmbus_channel_modifychannel *modifymsg;
1421870ced05SAndrea Parri (Microsoft) 
1422870ced05SAndrea Parri (Microsoft) 			modifymsg = (struct vmbus_channel_modifychannel *)msginfo->msg;
1423870ced05SAndrea Parri (Microsoft) 			if (modifymsg->child_relid == response->child_relid) {
1424870ced05SAndrea Parri (Microsoft) 				memcpy(&msginfo->response.modify_response, response,
1425870ced05SAndrea Parri (Microsoft) 				       sizeof(*response));
1426870ced05SAndrea Parri (Microsoft) 				complete(&msginfo->waitevent);
1427870ced05SAndrea Parri (Microsoft) 				break;
1428870ced05SAndrea Parri (Microsoft) 			}
1429870ced05SAndrea Parri (Microsoft) 		}
1430870ced05SAndrea Parri (Microsoft) 	}
1431870ced05SAndrea Parri (Microsoft) 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
1432870ced05SAndrea Parri (Microsoft) }
1433870ced05SAndrea Parri (Microsoft) 
1434870ced05SAndrea Parri (Microsoft) /*
143546a97191SGreg Kroah-Hartman  * vmbus_ongpadl_torndown - GPADL torndown handler.
143646a97191SGreg Kroah-Hartman  *
143746a97191SGreg Kroah-Hartman  * This is invoked when we received a response to our gpadl teardown request.
143846a97191SGreg Kroah-Hartman  * Find the matching request, copy the response and signal the requesting
143946a97191SGreg Kroah-Hartman  * thread.
144046a97191SGreg Kroah-Hartman  */
vmbus_ongpadl_torndown(struct vmbus_channel_message_header * hdr)144146a97191SGreg Kroah-Hartman static void vmbus_ongpadl_torndown(
144246a97191SGreg Kroah-Hartman 			struct vmbus_channel_message_header *hdr)
144346a97191SGreg Kroah-Hartman {
144446a97191SGreg Kroah-Hartman 	struct vmbus_channel_gpadl_torndown *gpadl_torndown;
144546a97191SGreg Kroah-Hartman 	struct vmbus_channel_msginfo *msginfo;
144646a97191SGreg Kroah-Hartman 	struct vmbus_channel_message_header *requestheader;
144746a97191SGreg Kroah-Hartman 	struct vmbus_channel_gpadl_teardown *gpadl_teardown;
144846a97191SGreg Kroah-Hartman 	unsigned long flags;
144946a97191SGreg Kroah-Hartman 
145046a97191SGreg Kroah-Hartman 	gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
145146a97191SGreg Kroah-Hartman 
1452602a8c52SVitaly Kuznetsov 	trace_vmbus_ongpadl_torndown(gpadl_torndown);
1453602a8c52SVitaly Kuznetsov 
145446a97191SGreg Kroah-Hartman 	/*
145546a97191SGreg Kroah-Hartman 	 * Find the open msg, copy the result and signal/unblock the wait event
145646a97191SGreg Kroah-Hartman 	 */
145746a97191SGreg Kroah-Hartman 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
145846a97191SGreg Kroah-Hartman 
145946a97191SGreg Kroah-Hartman 	list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
146046a97191SGreg Kroah-Hartman 				msglistentry) {
146146a97191SGreg Kroah-Hartman 		requestheader =
146246a97191SGreg Kroah-Hartman 			(struct vmbus_channel_message_header *)msginfo->msg;
146346a97191SGreg Kroah-Hartman 
146446a97191SGreg Kroah-Hartman 		if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
146546a97191SGreg Kroah-Hartman 			gpadl_teardown =
146646a97191SGreg Kroah-Hartman 			(struct vmbus_channel_gpadl_teardown *)requestheader;
146746a97191SGreg Kroah-Hartman 
146846a97191SGreg Kroah-Hartman 			if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
146946a97191SGreg Kroah-Hartman 				memcpy(&msginfo->response.gpadl_torndown,
147046a97191SGreg Kroah-Hartman 				       gpadl_torndown,
147146a97191SGreg Kroah-Hartman 				       sizeof(
147246a97191SGreg Kroah-Hartman 					struct vmbus_channel_gpadl_torndown));
147346a97191SGreg Kroah-Hartman 				complete(&msginfo->waitevent);
147446a97191SGreg Kroah-Hartman 				break;
147546a97191SGreg Kroah-Hartman 			}
147646a97191SGreg Kroah-Hartman 		}
147746a97191SGreg Kroah-Hartman 	}
147846a97191SGreg Kroah-Hartman 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
147946a97191SGreg Kroah-Hartman }
148046a97191SGreg Kroah-Hartman 
148146a97191SGreg Kroah-Hartman /*
148246a97191SGreg Kroah-Hartman  * vmbus_onversion_response - Version response handler
148346a97191SGreg Kroah-Hartman  *
148446a97191SGreg Kroah-Hartman  * This is invoked when we received a response to our initiate contact request.
148546a97191SGreg Kroah-Hartman  * Find the matching request, copy the response and signal the requesting
148646a97191SGreg Kroah-Hartman  * thread.
148746a97191SGreg Kroah-Hartman  */
vmbus_onversion_response(struct vmbus_channel_message_header * hdr)148846a97191SGreg Kroah-Hartman static void vmbus_onversion_response(
148946a97191SGreg Kroah-Hartman 		struct vmbus_channel_message_header *hdr)
149046a97191SGreg Kroah-Hartman {
149146a97191SGreg Kroah-Hartman 	struct vmbus_channel_msginfo *msginfo;
149246a97191SGreg Kroah-Hartman 	struct vmbus_channel_message_header *requestheader;
149346a97191SGreg Kroah-Hartman 	struct vmbus_channel_version_response *version_response;
149446a97191SGreg Kroah-Hartman 	unsigned long flags;
149546a97191SGreg Kroah-Hartman 
149646a97191SGreg Kroah-Hartman 	version_response = (struct vmbus_channel_version_response *)hdr;
1497c7924fb0SVitaly Kuznetsov 
1498c7924fb0SVitaly Kuznetsov 	trace_vmbus_onversion_response(version_response);
1499c7924fb0SVitaly Kuznetsov 
150046a97191SGreg Kroah-Hartman 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
150146a97191SGreg Kroah-Hartman 
150246a97191SGreg Kroah-Hartman 	list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
150346a97191SGreg Kroah-Hartman 				msglistentry) {
150446a97191SGreg Kroah-Hartman 		requestheader =
150546a97191SGreg Kroah-Hartman 			(struct vmbus_channel_message_header *)msginfo->msg;
150646a97191SGreg Kroah-Hartman 
150746a97191SGreg Kroah-Hartman 		if (requestheader->msgtype ==
150846a97191SGreg Kroah-Hartman 		    CHANNELMSG_INITIATE_CONTACT) {
150946a97191SGreg Kroah-Hartman 			memcpy(&msginfo->response.version_response,
151046a97191SGreg Kroah-Hartman 			      version_response,
151146a97191SGreg Kroah-Hartman 			      sizeof(struct vmbus_channel_version_response));
151246a97191SGreg Kroah-Hartman 			complete(&msginfo->waitevent);
151346a97191SGreg Kroah-Hartman 		}
151446a97191SGreg Kroah-Hartman 	}
151546a97191SGreg Kroah-Hartman 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
151646a97191SGreg Kroah-Hartman }
151746a97191SGreg Kroah-Hartman 
151846a97191SGreg Kroah-Hartman /* Channel message dispatch table */
1519e6242fa0SStephen Hemminger const struct vmbus_channel_message_table_entry
152046a97191SGreg Kroah-Hartman channel_message_table[CHANNELMSG_COUNT] = {
152152c7803fSVitaly Kuznetsov 	{ CHANNELMSG_INVALID,			0, NULL, 0},
152252c7803fSVitaly Kuznetsov 	{ CHANNELMSG_OFFERCHANNEL,		0, vmbus_onoffer,
152352c7803fSVitaly Kuznetsov 		sizeof(struct vmbus_channel_offer_channel)},
152452c7803fSVitaly Kuznetsov 	{ CHANNELMSG_RESCIND_CHANNELOFFER,	0, vmbus_onoffer_rescind,
152552c7803fSVitaly Kuznetsov 		sizeof(struct vmbus_channel_rescind_offer) },
152652c7803fSVitaly Kuznetsov 	{ CHANNELMSG_REQUESTOFFERS,		0, NULL, 0},
152752c7803fSVitaly Kuznetsov 	{ CHANNELMSG_ALLOFFERS_DELIVERED,	1, vmbus_onoffers_delivered, 0},
152852c7803fSVitaly Kuznetsov 	{ CHANNELMSG_OPENCHANNEL,		0, NULL, 0},
152952c7803fSVitaly Kuznetsov 	{ CHANNELMSG_OPENCHANNEL_RESULT,	1, vmbus_onopen_result,
153052c7803fSVitaly Kuznetsov 		sizeof(struct vmbus_channel_open_result)},
153152c7803fSVitaly Kuznetsov 	{ CHANNELMSG_CLOSECHANNEL,		0, NULL, 0},
153252c7803fSVitaly Kuznetsov 	{ CHANNELMSG_GPADL_HEADER,		0, NULL, 0},
153352c7803fSVitaly Kuznetsov 	{ CHANNELMSG_GPADL_BODY,		0, NULL, 0},
153452c7803fSVitaly Kuznetsov 	{ CHANNELMSG_GPADL_CREATED,		1, vmbus_ongpadl_created,
153552c7803fSVitaly Kuznetsov 		sizeof(struct vmbus_channel_gpadl_created)},
153652c7803fSVitaly Kuznetsov 	{ CHANNELMSG_GPADL_TEARDOWN,		0, NULL, 0},
153752c7803fSVitaly Kuznetsov 	{ CHANNELMSG_GPADL_TORNDOWN,		1, vmbus_ongpadl_torndown,
153852c7803fSVitaly Kuznetsov 		sizeof(struct vmbus_channel_gpadl_torndown) },
153952c7803fSVitaly Kuznetsov 	{ CHANNELMSG_RELID_RELEASED,		0, NULL, 0},
154052c7803fSVitaly Kuznetsov 	{ CHANNELMSG_INITIATE_CONTACT,		0, NULL, 0},
154152c7803fSVitaly Kuznetsov 	{ CHANNELMSG_VERSION_RESPONSE,		1, vmbus_onversion_response,
154252c7803fSVitaly Kuznetsov 		sizeof(struct vmbus_channel_version_response)},
154352c7803fSVitaly Kuznetsov 	{ CHANNELMSG_UNLOAD,			0, NULL, 0},
154452c7803fSVitaly Kuznetsov 	{ CHANNELMSG_UNLOAD_RESPONSE,		1, vmbus_unload_response, 0},
154552c7803fSVitaly Kuznetsov 	{ CHANNELMSG_18,			0, NULL, 0},
154652c7803fSVitaly Kuznetsov 	{ CHANNELMSG_19,			0, NULL, 0},
154752c7803fSVitaly Kuznetsov 	{ CHANNELMSG_20,			0, NULL, 0},
154852c7803fSVitaly Kuznetsov 	{ CHANNELMSG_TL_CONNECT_REQUEST,	0, NULL, 0},
154975278105SAndrea Parri (Microsoft) 	{ CHANNELMSG_MODIFYCHANNEL,		0, NULL, 0},
155052c7803fSVitaly Kuznetsov 	{ CHANNELMSG_TL_CONNECT_RESULT,		0, NULL, 0},
1551870ced05SAndrea Parri (Microsoft) 	{ CHANNELMSG_MODIFYCHANNEL_RESPONSE,	1, vmbus_onmodifychannel_response,
1552870ced05SAndrea Parri (Microsoft) 		sizeof(struct vmbus_channel_modifychannel_response)},
155346a97191SGreg Kroah-Hartman };
155446a97191SGreg Kroah-Hartman 
155546a97191SGreg Kroah-Hartman /*
155646a97191SGreg Kroah-Hartman  * vmbus_onmessage - Handler for channel protocol messages.
155746a97191SGreg Kroah-Hartman  *
155846a97191SGreg Kroah-Hartman  * This is invoked in the vmbus worker thread context.
155946a97191SGreg Kroah-Hartman  */
vmbus_onmessage(struct vmbus_channel_message_header * hdr)15605cc41500SVitaly Kuznetsov void vmbus_onmessage(struct vmbus_channel_message_header *hdr)
156146a97191SGreg Kroah-Hartman {
1562716fa52fSVitaly Kuznetsov 	trace_vmbus_on_message(hdr);
1563716fa52fSVitaly Kuznetsov 
1564ddc9d357SDexuan Cui 	/*
1565ddc9d357SDexuan Cui 	 * vmbus_on_msg_dpc() makes sure the hdr->msgtype here can not go
1566ddc9d357SDexuan Cui 	 * out of bound and the message_handler pointer can not be NULL.
1567ddc9d357SDexuan Cui 	 */
156846a97191SGreg Kroah-Hartman 	channel_message_table[hdr->msgtype].message_handler(hdr);
156946a97191SGreg Kroah-Hartman }
157046a97191SGreg Kroah-Hartman 
157146a97191SGreg Kroah-Hartman /*
157246a97191SGreg Kroah-Hartman  * vmbus_request_offers - Send a request to get all our pending offers.
157346a97191SGreg Kroah-Hartman  */
vmbus_request_offers(void)157446a97191SGreg Kroah-Hartman int vmbus_request_offers(void)
157546a97191SGreg Kroah-Hartman {
157646a97191SGreg Kroah-Hartman 	struct vmbus_channel_message_header *msg;
157746a97191SGreg Kroah-Hartman 	struct vmbus_channel_msginfo *msginfo;
157851e5181dSNicholas Mc Guire 	int ret;
157946a97191SGreg Kroah-Hartman 
15806a27e396SJuan Vazquez 	msginfo = kzalloc(sizeof(*msginfo) +
158146a97191SGreg Kroah-Hartman 			  sizeof(struct vmbus_channel_message_header),
158246a97191SGreg Kroah-Hartman 			  GFP_KERNEL);
158346a97191SGreg Kroah-Hartman 	if (!msginfo)
158446a97191SGreg Kroah-Hartman 		return -ENOMEM;
158546a97191SGreg Kroah-Hartman 
158646a97191SGreg Kroah-Hartman 	msg = (struct vmbus_channel_message_header *)msginfo->msg;
158746a97191SGreg Kroah-Hartman 
158846a97191SGreg Kroah-Hartman 	msg->msgtype = CHANNELMSG_REQUESTOFFERS;
158946a97191SGreg Kroah-Hartman 
1590c0bb0392SVitaly Kuznetsov 	ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_message_header),
1591c0bb0392SVitaly Kuznetsov 			     true);
1592d6706e63SVitaly Kuznetsov 
1593d6706e63SVitaly Kuznetsov 	trace_vmbus_request_offers(ret);
1594d6706e63SVitaly Kuznetsov 
159546a97191SGreg Kroah-Hartman 	if (ret != 0) {
159646a97191SGreg Kroah-Hartman 		pr_err("Unable to request offers - %d\n", ret);
159746a97191SGreg Kroah-Hartman 
159846a97191SGreg Kroah-Hartman 		goto cleanup;
159946a97191SGreg Kroah-Hartman 	}
160046a97191SGreg Kroah-Hartman 
160146a97191SGreg Kroah-Hartman cleanup:
160246a97191SGreg Kroah-Hartman 	kfree(msginfo);
160346a97191SGreg Kroah-Hartman 
160446a97191SGreg Kroah-Hartman 	return ret;
160546a97191SGreg Kroah-Hartman }
160646a97191SGreg Kroah-Hartman 
vmbus_set_sc_create_callback(struct vmbus_channel * primary_channel,void (* sc_cr_cb)(struct vmbus_channel * new_sc))1607e68d2971SK. Y. Srinivasan void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
1608e68d2971SK. Y. Srinivasan 				void (*sc_cr_cb)(struct vmbus_channel *new_sc))
1609e68d2971SK. Y. Srinivasan {
1610e68d2971SK. Y. Srinivasan 	primary_channel->sc_creation_callback = sc_cr_cb;
1611e68d2971SK. Y. Srinivasan }
1612e68d2971SK. Y. Srinivasan EXPORT_SYMBOL_GPL(vmbus_set_sc_create_callback);
1613e68d2971SK. Y. Srinivasan 
vmbus_set_chn_rescind_callback(struct vmbus_channel * channel,void (* chn_rescind_cb)(struct vmbus_channel *))1614499e8401SDexuan Cui void vmbus_set_chn_rescind_callback(struct vmbus_channel *channel,
1615499e8401SDexuan Cui 		void (*chn_rescind_cb)(struct vmbus_channel *))
1616499e8401SDexuan Cui {
1617499e8401SDexuan Cui 	channel->chn_rescind_callback = chn_rescind_cb;
1618499e8401SDexuan Cui }
1619499e8401SDexuan Cui EXPORT_SYMBOL_GPL(vmbus_set_chn_rescind_callback);
1620