xref: /openbmc/linux/drivers/hv/channel_mgmt.c (revision 04653a00)
1 /*
2  * Copyright (c) 2009, Microsoft Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15  * Place - Suite 330, Boston, MA 02111-1307 USA.
16  *
17  * Authors:
18  *   Haiyang Zhang <haiyangz@microsoft.com>
19  *   Hank Janssen  <hjanssen@microsoft.com>
20  */
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22 
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/wait.h>
26 #include <linux/mm.h>
27 #include <linux/slab.h>
28 #include <linux/list.h>
29 #include <linux/module.h>
30 #include <linux/completion.h>
31 #include <linux/hyperv.h>
32 
33 #include "hyperv_vmbus.h"
34 
35 struct vmbus_channel_message_table_entry {
36 	enum vmbus_channel_message_type message_type;
37 	void (*message_handler)(struct vmbus_channel_message_header *msg);
38 };
39 
40 
41 /**
42  * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message
43  * @icmsghdrp: Pointer to msg header structure
44  * @icmsg_negotiate: Pointer to negotiate message structure
45  * @buf: Raw buffer channel data
46  *
47  * @icmsghdrp is of type &struct icmsg_hdr.
48  * @negop is of type &struct icmsg_negotiate.
49  * Set up and fill in default negotiate response message.
50  *
51  * The fw_version specifies the  framework version that
52  * we can support and srv_version specifies the service
53  * version we can support.
54  *
55  * Mainly used by Hyper-V drivers.
56  */
57 bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,
58 				struct icmsg_negotiate *negop, u8 *buf,
59 				int fw_version, int srv_version)
60 {
61 	int icframe_major, icframe_minor;
62 	int icmsg_major, icmsg_minor;
63 	int fw_major, fw_minor;
64 	int srv_major, srv_minor;
65 	int i;
66 	bool found_match = false;
67 
68 	icmsghdrp->icmsgsize = 0x10;
69 	fw_major = (fw_version >> 16);
70 	fw_minor = (fw_version & 0xFFFF);
71 
72 	srv_major = (srv_version >> 16);
73 	srv_minor = (srv_version & 0xFFFF);
74 
75 	negop = (struct icmsg_negotiate *)&buf[
76 		sizeof(struct vmbuspipe_hdr) +
77 		sizeof(struct icmsg_hdr)];
78 
79 	icframe_major = negop->icframe_vercnt;
80 	icframe_minor = 0;
81 
82 	icmsg_major = negop->icmsg_vercnt;
83 	icmsg_minor = 0;
84 
85 	/*
86 	 * Select the framework version number we will
87 	 * support.
88 	 */
89 
90 	for (i = 0; i < negop->icframe_vercnt; i++) {
91 		if ((negop->icversion_data[i].major == fw_major) &&
92 		   (negop->icversion_data[i].minor == fw_minor)) {
93 			icframe_major = negop->icversion_data[i].major;
94 			icframe_minor = negop->icversion_data[i].minor;
95 			found_match = true;
96 		}
97 	}
98 
99 	if (!found_match)
100 		goto fw_error;
101 
102 	found_match = false;
103 
104 	for (i = negop->icframe_vercnt;
105 		 (i < negop->icframe_vercnt + negop->icmsg_vercnt); i++) {
106 		if ((negop->icversion_data[i].major == srv_major) &&
107 		   (negop->icversion_data[i].minor == srv_minor)) {
108 			icmsg_major = negop->icversion_data[i].major;
109 			icmsg_minor = negop->icversion_data[i].minor;
110 			found_match = true;
111 		}
112 	}
113 
114 	/*
115 	 * Respond with the framework and service
116 	 * version numbers we can support.
117 	 */
118 
119 fw_error:
120 	if (!found_match) {
121 		negop->icframe_vercnt = 0;
122 		negop->icmsg_vercnt = 0;
123 	} else {
124 		negop->icframe_vercnt = 1;
125 		negop->icmsg_vercnt = 1;
126 	}
127 
128 	negop->icversion_data[0].major = icframe_major;
129 	negop->icversion_data[0].minor = icframe_minor;
130 	negop->icversion_data[1].major = icmsg_major;
131 	negop->icversion_data[1].minor = icmsg_minor;
132 	return found_match;
133 }
134 
135 EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp);
136 
137 /*
138  * alloc_channel - Allocate and initialize a vmbus channel object
139  */
140 static struct vmbus_channel *alloc_channel(void)
141 {
142 	static atomic_t chan_num = ATOMIC_INIT(0);
143 	struct vmbus_channel *channel;
144 
145 	channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
146 	if (!channel)
147 		return NULL;
148 
149 	channel->id = atomic_inc_return(&chan_num);
150 	spin_lock_init(&channel->inbound_lock);
151 	spin_lock_init(&channel->lock);
152 
153 	INIT_LIST_HEAD(&channel->sc_list);
154 	INIT_LIST_HEAD(&channel->percpu_list);
155 
156 	channel->controlwq = alloc_workqueue("hv_vmbus_ctl/%d", WQ_MEM_RECLAIM,
157 					     1, channel->id);
158 	if (!channel->controlwq) {
159 		kfree(channel);
160 		return NULL;
161 	}
162 
163 	return channel;
164 }
165 
166 /*
167  * release_hannel - Release the vmbus channel object itself
168  */
169 static void release_channel(struct work_struct *work)
170 {
171 	struct vmbus_channel *channel = container_of(work,
172 						     struct vmbus_channel,
173 						     work);
174 
175 	destroy_workqueue(channel->controlwq);
176 
177 	kfree(channel);
178 }
179 
180 /*
181  * free_channel - Release the resources used by the vmbus channel object
182  */
183 static void free_channel(struct vmbus_channel *channel)
184 {
185 
186 	/*
187 	 * We have to release the channel's workqueue/thread in the vmbus's
188 	 * workqueue/thread context
189 	 * ie we can't destroy ourselves.
190 	 */
191 	INIT_WORK(&channel->work, release_channel);
192 	queue_work(vmbus_connection.work_queue, &channel->work);
193 }
194 
195 static void percpu_channel_enq(void *arg)
196 {
197 	struct vmbus_channel *channel = arg;
198 	int cpu = smp_processor_id();
199 
200 	list_add_tail(&channel->percpu_list, &hv_context.percpu_list[cpu]);
201 }
202 
203 static void percpu_channel_deq(void *arg)
204 {
205 	struct vmbus_channel *channel = arg;
206 
207 	list_del(&channel->percpu_list);
208 }
209 
210 /*
211  * vmbus_process_rescind_offer -
212  * Rescind the offer by initiating a device removal
213  */
214 static void vmbus_process_rescind_offer(struct work_struct *work)
215 {
216 	struct vmbus_channel *channel = container_of(work,
217 						     struct vmbus_channel,
218 						     work);
219 	unsigned long flags;
220 	struct vmbus_channel *primary_channel;
221 	struct vmbus_channel_relid_released msg;
222 	struct device *dev;
223 
224 	if (channel->device_obj) {
225 		dev = get_device(&channel->device_obj->device);
226 		if (dev) {
227 			vmbus_device_unregister(channel->device_obj);
228 			put_device(dev);
229 		}
230 	}
231 
232 	memset(&msg, 0, sizeof(struct vmbus_channel_relid_released));
233 	msg.child_relid = channel->offermsg.child_relid;
234 	msg.header.msgtype = CHANNELMSG_RELID_RELEASED;
235 	vmbus_post_msg(&msg, sizeof(struct vmbus_channel_relid_released));
236 
237 	if (channel->target_cpu != get_cpu()) {
238 		put_cpu();
239 		smp_call_function_single(channel->target_cpu,
240 					 percpu_channel_deq, channel, true);
241 	} else {
242 		percpu_channel_deq(channel);
243 		put_cpu();
244 	}
245 
246 	if (channel->primary_channel == NULL) {
247 		spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
248 		list_del(&channel->listentry);
249 		spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
250 	} else {
251 		primary_channel = channel->primary_channel;
252 		spin_lock_irqsave(&primary_channel->lock, flags);
253 		list_del(&channel->sc_list);
254 		spin_unlock_irqrestore(&primary_channel->lock, flags);
255 	}
256 	free_channel(channel);
257 }
258 
259 void vmbus_free_channels(void)
260 {
261 	struct vmbus_channel *channel;
262 
263 	list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
264 		vmbus_device_unregister(channel->device_obj);
265 		free_channel(channel);
266 	}
267 }
268 
269 /*
270  * vmbus_process_offer - Process the offer by creating a channel/device
271  * associated with this offer
272  */
273 static void vmbus_process_offer(struct work_struct *work)
274 {
275 	struct vmbus_channel *newchannel = container_of(work,
276 							struct vmbus_channel,
277 							work);
278 	struct vmbus_channel *channel;
279 	bool fnew = true;
280 	bool enq = false;
281 	int ret;
282 	unsigned long flags;
283 
284 	/* Make sure this is a new offer */
285 	spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
286 
287 	list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
288 		if (!uuid_le_cmp(channel->offermsg.offer.if_type,
289 			newchannel->offermsg.offer.if_type) &&
290 			!uuid_le_cmp(channel->offermsg.offer.if_instance,
291 				newchannel->offermsg.offer.if_instance)) {
292 			fnew = false;
293 			break;
294 		}
295 	}
296 
297 	if (fnew) {
298 		list_add_tail(&newchannel->listentry,
299 			      &vmbus_connection.chn_list);
300 		enq = true;
301 	}
302 
303 	spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
304 
305 	if (enq) {
306 		if (newchannel->target_cpu != get_cpu()) {
307 			put_cpu();
308 			smp_call_function_single(newchannel->target_cpu,
309 						 percpu_channel_enq,
310 						 newchannel, true);
311 		} else {
312 			percpu_channel_enq(newchannel);
313 			put_cpu();
314 		}
315 	}
316 	if (!fnew) {
317 		/*
318 		 * Check to see if this is a sub-channel.
319 		 */
320 		if (newchannel->offermsg.offer.sub_channel_index != 0) {
321 			/*
322 			 * Process the sub-channel.
323 			 */
324 			newchannel->primary_channel = channel;
325 			spin_lock_irqsave(&channel->lock, flags);
326 			list_add_tail(&newchannel->sc_list, &channel->sc_list);
327 			spin_unlock_irqrestore(&channel->lock, flags);
328 
329 			if (newchannel->target_cpu != get_cpu()) {
330 				put_cpu();
331 				smp_call_function_single(newchannel->target_cpu,
332 							 percpu_channel_enq,
333 							 newchannel, true);
334 			} else {
335 				percpu_channel_enq(newchannel);
336 				put_cpu();
337 			}
338 
339 			newchannel->state = CHANNEL_OPEN_STATE;
340 			if (channel->sc_creation_callback != NULL)
341 				channel->sc_creation_callback(newchannel);
342 
343 			goto done_init_rescind;
344 		}
345 
346 		goto err_free_chan;
347 	}
348 
349 	/*
350 	 * This state is used to indicate a successful open
351 	 * so that when we do close the channel normally, we
352 	 * can cleanup properly
353 	 */
354 	newchannel->state = CHANNEL_OPEN_STATE;
355 
356 	/*
357 	 * Start the process of binding this offer to the driver
358 	 * We need to set the DeviceObject field before calling
359 	 * vmbus_child_dev_add()
360 	 */
361 	newchannel->device_obj = vmbus_device_create(
362 		&newchannel->offermsg.offer.if_type,
363 		&newchannel->offermsg.offer.if_instance,
364 		newchannel);
365 	if (!newchannel->device_obj)
366 		goto err_free_chan;
367 
368 	/*
369 	 * Add the new device to the bus. This will kick off device-driver
370 	 * binding which eventually invokes the device driver's AddDevice()
371 	 * method.
372 	 */
373 	ret = vmbus_device_register(newchannel->device_obj);
374 	if (ret != 0) {
375 		pr_err("unable to add child device object (relid %d)\n",
376 			   newchannel->offermsg.child_relid);
377 
378 		spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
379 		list_del(&newchannel->listentry);
380 		spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
381 		kfree(newchannel->device_obj);
382 		goto err_free_chan;
383 	}
384 done_init_rescind:
385 	spin_lock_irqsave(&newchannel->lock, flags);
386 	/* The next possible work is rescind handling */
387 	INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
388 	/* Check if rescind offer was already received */
389 	if (newchannel->rescind)
390 		queue_work(newchannel->controlwq, &newchannel->work);
391 	spin_unlock_irqrestore(&newchannel->lock, flags);
392 	return;
393 err_free_chan:
394 	free_channel(newchannel);
395 }
396 
397 enum {
398 	IDE = 0,
399 	SCSI,
400 	NIC,
401 	MAX_PERF_CHN,
402 };
403 
404 /*
405  * This is an array of device_ids (device types) that are performance critical.
406  * We attempt to distribute the interrupt load for these devices across
407  * all available CPUs.
408  */
409 static const struct hv_vmbus_device_id hp_devs[] = {
410 	/* IDE */
411 	{ HV_IDE_GUID, },
412 	/* Storage - SCSI */
413 	{ HV_SCSI_GUID, },
414 	/* Network */
415 	{ HV_NIC_GUID, },
416 	/* NetworkDirect Guest RDMA */
417 	{ HV_ND_GUID, },
418 };
419 
420 
421 /*
422  * We use this state to statically distribute the channel interrupt load.
423  */
424 static u32  next_vp;
425 
426 /*
427  * Starting with Win8, we can statically distribute the incoming
428  * channel interrupt load by binding a channel to VCPU. We
429  * implement here a simple round robin scheme for distributing
430  * the interrupt load.
431  * We will bind channels that are not performance critical to cpu 0 and
432  * performance critical channels (IDE, SCSI and Network) will be uniformly
433  * distributed across all available CPUs.
434  */
435 static void init_vp_index(struct vmbus_channel *channel, const uuid_le *type_guid)
436 {
437 	u32 cur_cpu;
438 	int i;
439 	bool perf_chn = false;
440 	u32 max_cpus = num_online_cpus();
441 
442 	for (i = IDE; i < MAX_PERF_CHN; i++) {
443 		if (!memcmp(type_guid->b, hp_devs[i].guid,
444 				 sizeof(uuid_le))) {
445 			perf_chn = true;
446 			break;
447 		}
448 	}
449 	if ((vmbus_proto_version == VERSION_WS2008) ||
450 	    (vmbus_proto_version == VERSION_WIN7) || (!perf_chn)) {
451 		/*
452 		 * Prior to win8, all channel interrupts are
453 		 * delivered on cpu 0.
454 		 * Also if the channel is not a performance critical
455 		 * channel, bind it to cpu 0.
456 		 */
457 		channel->target_cpu = 0;
458 		channel->target_vp = 0;
459 		return;
460 	}
461 	cur_cpu = (++next_vp % max_cpus);
462 	channel->target_cpu = cur_cpu;
463 	channel->target_vp = hv_context.vp_index[cur_cpu];
464 }
465 
466 /*
467  * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
468  *
469  */
470 static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
471 {
472 	struct vmbus_channel_offer_channel *offer;
473 	struct vmbus_channel *newchannel;
474 
475 	offer = (struct vmbus_channel_offer_channel *)hdr;
476 
477 	/* Allocate the channel object and save this offer. */
478 	newchannel = alloc_channel();
479 	if (!newchannel) {
480 		pr_err("Unable to allocate channel object\n");
481 		return;
482 	}
483 
484 	/*
485 	 * By default we setup state to enable batched
486 	 * reading. A specific service can choose to
487 	 * disable this prior to opening the channel.
488 	 */
489 	newchannel->batched_reading = true;
490 
491 	/*
492 	 * Setup state for signalling the host.
493 	 */
494 	newchannel->sig_event = (struct hv_input_signal_event *)
495 				(ALIGN((unsigned long)
496 				&newchannel->sig_buf,
497 				HV_HYPERCALL_PARAM_ALIGN));
498 
499 	newchannel->sig_event->connectionid.asu32 = 0;
500 	newchannel->sig_event->connectionid.u.id = VMBUS_EVENT_CONNECTION_ID;
501 	newchannel->sig_event->flag_number = 0;
502 	newchannel->sig_event->rsvdz = 0;
503 
504 	if (vmbus_proto_version != VERSION_WS2008) {
505 		newchannel->is_dedicated_interrupt =
506 				(offer->is_dedicated_interrupt != 0);
507 		newchannel->sig_event->connectionid.u.id =
508 				offer->connection_id;
509 	}
510 
511 	init_vp_index(newchannel, &offer->offer.if_type);
512 
513 	memcpy(&newchannel->offermsg, offer,
514 	       sizeof(struct vmbus_channel_offer_channel));
515 	newchannel->monitor_grp = (u8)offer->monitorid / 32;
516 	newchannel->monitor_bit = (u8)offer->monitorid % 32;
517 
518 	INIT_WORK(&newchannel->work, vmbus_process_offer);
519 	queue_work(newchannel->controlwq, &newchannel->work);
520 }
521 
522 /*
523  * vmbus_onoffer_rescind - Rescind offer handler.
524  *
525  * We queue a work item to process this offer synchronously
526  */
527 static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
528 {
529 	struct vmbus_channel_rescind_offer *rescind;
530 	struct vmbus_channel *channel;
531 	unsigned long flags;
532 
533 	rescind = (struct vmbus_channel_rescind_offer *)hdr;
534 	channel = relid2channel(rescind->child_relid);
535 
536 	if (channel == NULL)
537 		/* Just return here, no channel found */
538 		return;
539 
540 	spin_lock_irqsave(&channel->lock, flags);
541 	channel->rescind = true;
542 	/*
543 	 * channel->work.func != vmbus_process_rescind_offer means we are still
544 	 * processing offer request and the rescind offer processing should be
545 	 * postponed. It will be done at the very end of vmbus_process_offer()
546 	 * as rescind flag is being checked there.
547 	 */
548 	if (channel->work.func == vmbus_process_rescind_offer)
549 		/* work is initialized for vmbus_process_rescind_offer() from
550 		 * vmbus_process_offer() where the channel got created */
551 		queue_work(channel->controlwq, &channel->work);
552 
553 	spin_unlock_irqrestore(&channel->lock, flags);
554 }
555 
556 /*
557  * vmbus_onoffers_delivered -
558  * This is invoked when all offers have been delivered.
559  *
560  * Nothing to do here.
561  */
562 static void vmbus_onoffers_delivered(
563 			struct vmbus_channel_message_header *hdr)
564 {
565 }
566 
567 /*
568  * vmbus_onopen_result - Open result handler.
569  *
570  * This is invoked when we received a response to our channel open request.
571  * Find the matching request, copy the response and signal the requesting
572  * thread.
573  */
574 static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
575 {
576 	struct vmbus_channel_open_result *result;
577 	struct vmbus_channel_msginfo *msginfo;
578 	struct vmbus_channel_message_header *requestheader;
579 	struct vmbus_channel_open_channel *openmsg;
580 	unsigned long flags;
581 
582 	result = (struct vmbus_channel_open_result *)hdr;
583 
584 	/*
585 	 * Find the open msg, copy the result and signal/unblock the wait event
586 	 */
587 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
588 
589 	list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
590 				msglistentry) {
591 		requestheader =
592 			(struct vmbus_channel_message_header *)msginfo->msg;
593 
594 		if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
595 			openmsg =
596 			(struct vmbus_channel_open_channel *)msginfo->msg;
597 			if (openmsg->child_relid == result->child_relid &&
598 			    openmsg->openid == result->openid) {
599 				memcpy(&msginfo->response.open_result,
600 				       result,
601 				       sizeof(
602 					struct vmbus_channel_open_result));
603 				complete(&msginfo->waitevent);
604 				break;
605 			}
606 		}
607 	}
608 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
609 }
610 
611 /*
612  * vmbus_ongpadl_created - GPADL created handler.
613  *
614  * This is invoked when we received a response to our gpadl create request.
615  * Find the matching request, copy the response and signal the requesting
616  * thread.
617  */
618 static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
619 {
620 	struct vmbus_channel_gpadl_created *gpadlcreated;
621 	struct vmbus_channel_msginfo *msginfo;
622 	struct vmbus_channel_message_header *requestheader;
623 	struct vmbus_channel_gpadl_header *gpadlheader;
624 	unsigned long flags;
625 
626 	gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
627 
628 	/*
629 	 * Find the establish msg, copy the result and signal/unblock the wait
630 	 * event
631 	 */
632 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
633 
634 	list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
635 				msglistentry) {
636 		requestheader =
637 			(struct vmbus_channel_message_header *)msginfo->msg;
638 
639 		if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
640 			gpadlheader =
641 			(struct vmbus_channel_gpadl_header *)requestheader;
642 
643 			if ((gpadlcreated->child_relid ==
644 			     gpadlheader->child_relid) &&
645 			    (gpadlcreated->gpadl == gpadlheader->gpadl)) {
646 				memcpy(&msginfo->response.gpadl_created,
647 				       gpadlcreated,
648 				       sizeof(
649 					struct vmbus_channel_gpadl_created));
650 				complete(&msginfo->waitevent);
651 				break;
652 			}
653 		}
654 	}
655 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
656 }
657 
658 /*
659  * vmbus_ongpadl_torndown - GPADL torndown handler.
660  *
661  * This is invoked when we received a response to our gpadl teardown request.
662  * Find the matching request, copy the response and signal the requesting
663  * thread.
664  */
665 static void vmbus_ongpadl_torndown(
666 			struct vmbus_channel_message_header *hdr)
667 {
668 	struct vmbus_channel_gpadl_torndown *gpadl_torndown;
669 	struct vmbus_channel_msginfo *msginfo;
670 	struct vmbus_channel_message_header *requestheader;
671 	struct vmbus_channel_gpadl_teardown *gpadl_teardown;
672 	unsigned long flags;
673 
674 	gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
675 
676 	/*
677 	 * Find the open msg, copy the result and signal/unblock the wait event
678 	 */
679 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
680 
681 	list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
682 				msglistentry) {
683 		requestheader =
684 			(struct vmbus_channel_message_header *)msginfo->msg;
685 
686 		if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
687 			gpadl_teardown =
688 			(struct vmbus_channel_gpadl_teardown *)requestheader;
689 
690 			if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
691 				memcpy(&msginfo->response.gpadl_torndown,
692 				       gpadl_torndown,
693 				       sizeof(
694 					struct vmbus_channel_gpadl_torndown));
695 				complete(&msginfo->waitevent);
696 				break;
697 			}
698 		}
699 	}
700 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
701 }
702 
703 /*
704  * vmbus_onversion_response - Version response handler
705  *
706  * This is invoked when we received a response to our initiate contact request.
707  * Find the matching request, copy the response and signal the requesting
708  * thread.
709  */
710 static void vmbus_onversion_response(
711 		struct vmbus_channel_message_header *hdr)
712 {
713 	struct vmbus_channel_msginfo *msginfo;
714 	struct vmbus_channel_message_header *requestheader;
715 	struct vmbus_channel_version_response *version_response;
716 	unsigned long flags;
717 
718 	version_response = (struct vmbus_channel_version_response *)hdr;
719 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
720 
721 	list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
722 				msglistentry) {
723 		requestheader =
724 			(struct vmbus_channel_message_header *)msginfo->msg;
725 
726 		if (requestheader->msgtype ==
727 		    CHANNELMSG_INITIATE_CONTACT) {
728 			memcpy(&msginfo->response.version_response,
729 			      version_response,
730 			      sizeof(struct vmbus_channel_version_response));
731 			complete(&msginfo->waitevent);
732 		}
733 	}
734 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
735 }
736 
737 /* Channel message dispatch table */
738 static struct vmbus_channel_message_table_entry
739 	channel_message_table[CHANNELMSG_COUNT] = {
740 	{CHANNELMSG_INVALID,			NULL},
741 	{CHANNELMSG_OFFERCHANNEL,		vmbus_onoffer},
742 	{CHANNELMSG_RESCIND_CHANNELOFFER,	vmbus_onoffer_rescind},
743 	{CHANNELMSG_REQUESTOFFERS,		NULL},
744 	{CHANNELMSG_ALLOFFERS_DELIVERED,	vmbus_onoffers_delivered},
745 	{CHANNELMSG_OPENCHANNEL,		NULL},
746 	{CHANNELMSG_OPENCHANNEL_RESULT,	vmbus_onopen_result},
747 	{CHANNELMSG_CLOSECHANNEL,		NULL},
748 	{CHANNELMSG_GPADL_HEADER,		NULL},
749 	{CHANNELMSG_GPADL_BODY,		NULL},
750 	{CHANNELMSG_GPADL_CREATED,		vmbus_ongpadl_created},
751 	{CHANNELMSG_GPADL_TEARDOWN,		NULL},
752 	{CHANNELMSG_GPADL_TORNDOWN,		vmbus_ongpadl_torndown},
753 	{CHANNELMSG_RELID_RELEASED,		NULL},
754 	{CHANNELMSG_INITIATE_CONTACT,		NULL},
755 	{CHANNELMSG_VERSION_RESPONSE,		vmbus_onversion_response},
756 	{CHANNELMSG_UNLOAD,			NULL},
757 };
758 
759 /*
760  * vmbus_onmessage - Handler for channel protocol messages.
761  *
762  * This is invoked in the vmbus worker thread context.
763  */
764 void vmbus_onmessage(void *context)
765 {
766 	struct hv_message *msg = context;
767 	struct vmbus_channel_message_header *hdr;
768 	int size;
769 
770 	hdr = (struct vmbus_channel_message_header *)msg->u.payload;
771 	size = msg->header.payload_size;
772 
773 	if (hdr->msgtype >= CHANNELMSG_COUNT) {
774 		pr_err("Received invalid channel message type %d size %d\n",
775 			   hdr->msgtype, size);
776 		print_hex_dump_bytes("", DUMP_PREFIX_NONE,
777 				     (unsigned char *)msg->u.payload, size);
778 		return;
779 	}
780 
781 	if (channel_message_table[hdr->msgtype].message_handler)
782 		channel_message_table[hdr->msgtype].message_handler(hdr);
783 	else
784 		pr_err("Unhandled channel message type %d\n", hdr->msgtype);
785 }
786 
787 /*
788  * vmbus_request_offers - Send a request to get all our pending offers.
789  */
790 int vmbus_request_offers(void)
791 {
792 	struct vmbus_channel_message_header *msg;
793 	struct vmbus_channel_msginfo *msginfo;
794 	int ret;
795 	unsigned long t;
796 
797 	msginfo = kmalloc(sizeof(*msginfo) +
798 			  sizeof(struct vmbus_channel_message_header),
799 			  GFP_KERNEL);
800 	if (!msginfo)
801 		return -ENOMEM;
802 
803 	init_completion(&msginfo->waitevent);
804 
805 	msg = (struct vmbus_channel_message_header *)msginfo->msg;
806 
807 	msg->msgtype = CHANNELMSG_REQUESTOFFERS;
808 
809 
810 	ret = vmbus_post_msg(msg,
811 			       sizeof(struct vmbus_channel_message_header));
812 	if (ret != 0) {
813 		pr_err("Unable to request offers - %d\n", ret);
814 
815 		goto cleanup;
816 	}
817 
818 	t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
819 	if (t == 0) {
820 		ret = -ETIMEDOUT;
821 		goto cleanup;
822 	}
823 
824 
825 
826 cleanup:
827 	kfree(msginfo);
828 
829 	return ret;
830 }
831 
832 /*
833  * Retrieve the (sub) channel on which to send an outgoing request.
834  * When a primary channel has multiple sub-channels, we choose a
835  * channel whose VCPU binding is closest to the VCPU on which
836  * this call is being made.
837  */
838 struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary)
839 {
840 	struct list_head *cur, *tmp;
841 	int cur_cpu;
842 	struct vmbus_channel *cur_channel;
843 	struct vmbus_channel *outgoing_channel = primary;
844 	int cpu_distance, new_cpu_distance;
845 
846 	if (list_empty(&primary->sc_list))
847 		return outgoing_channel;
848 
849 	cur_cpu = hv_context.vp_index[get_cpu()];
850 	put_cpu();
851 	list_for_each_safe(cur, tmp, &primary->sc_list) {
852 		cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
853 		if (cur_channel->state != CHANNEL_OPENED_STATE)
854 			continue;
855 
856 		if (cur_channel->target_vp == cur_cpu)
857 			return cur_channel;
858 
859 		cpu_distance = ((outgoing_channel->target_vp > cur_cpu) ?
860 				(outgoing_channel->target_vp - cur_cpu) :
861 				(cur_cpu - outgoing_channel->target_vp));
862 
863 		new_cpu_distance = ((cur_channel->target_vp > cur_cpu) ?
864 				(cur_channel->target_vp - cur_cpu) :
865 				(cur_cpu - cur_channel->target_vp));
866 
867 		if (cpu_distance < new_cpu_distance)
868 			continue;
869 
870 		outgoing_channel = cur_channel;
871 	}
872 
873 	return outgoing_channel;
874 }
875 EXPORT_SYMBOL_GPL(vmbus_get_outgoing_channel);
876 
877 static void invoke_sc_cb(struct vmbus_channel *primary_channel)
878 {
879 	struct list_head *cur, *tmp;
880 	struct vmbus_channel *cur_channel;
881 
882 	if (primary_channel->sc_creation_callback == NULL)
883 		return;
884 
885 	list_for_each_safe(cur, tmp, &primary_channel->sc_list) {
886 		cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
887 
888 		primary_channel->sc_creation_callback(cur_channel);
889 	}
890 }
891 
892 void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
893 				void (*sc_cr_cb)(struct vmbus_channel *new_sc))
894 {
895 	primary_channel->sc_creation_callback = sc_cr_cb;
896 }
897 EXPORT_SYMBOL_GPL(vmbus_set_sc_create_callback);
898 
899 bool vmbus_are_subchannels_present(struct vmbus_channel *primary)
900 {
901 	bool ret;
902 
903 	ret = !list_empty(&primary->sc_list);
904 
905 	if (ret) {
906 		/*
907 		 * Invoke the callback on sub-channel creation.
908 		 * This will present a uniform interface to the
909 		 * clients.
910 		 */
911 		invoke_sc_cb(primary);
912 	}
913 
914 	return ret;
915 }
916 EXPORT_SYMBOL_GPL(vmbus_are_subchannels_present);
917