xref: /openbmc/linux/drivers/hv/channel_mgmt.c (revision bc63b6f6)
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 		kfree(channel->device_obj);
266 		free_channel(channel);
267 	}
268 }
269 
270 /*
271  * vmbus_process_offer - Process the offer by creating a channel/device
272  * associated with this offer
273  */
274 static void vmbus_process_offer(struct work_struct *work)
275 {
276 	struct vmbus_channel *newchannel = container_of(work,
277 							struct vmbus_channel,
278 							work);
279 	struct vmbus_channel *channel;
280 	bool fnew = true;
281 	bool enq = false;
282 	int ret;
283 	unsigned long flags;
284 
285 	/* Make sure this is a new offer */
286 	spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
287 
288 	list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
289 		if (!uuid_le_cmp(channel->offermsg.offer.if_type,
290 			newchannel->offermsg.offer.if_type) &&
291 			!uuid_le_cmp(channel->offermsg.offer.if_instance,
292 				newchannel->offermsg.offer.if_instance)) {
293 			fnew = false;
294 			break;
295 		}
296 	}
297 
298 	if (fnew) {
299 		list_add_tail(&newchannel->listentry,
300 			      &vmbus_connection.chn_list);
301 		enq = true;
302 	}
303 
304 	spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
305 
306 	if (enq) {
307 		if (newchannel->target_cpu != get_cpu()) {
308 			put_cpu();
309 			smp_call_function_single(newchannel->target_cpu,
310 						 percpu_channel_enq,
311 						 newchannel, true);
312 		} else {
313 			percpu_channel_enq(newchannel);
314 			put_cpu();
315 		}
316 	}
317 	if (!fnew) {
318 		/*
319 		 * Check to see if this is a sub-channel.
320 		 */
321 		if (newchannel->offermsg.offer.sub_channel_index != 0) {
322 			/*
323 			 * Process the sub-channel.
324 			 */
325 			newchannel->primary_channel = channel;
326 			spin_lock_irqsave(&channel->lock, flags);
327 			list_add_tail(&newchannel->sc_list, &channel->sc_list);
328 			spin_unlock_irqrestore(&channel->lock, flags);
329 
330 			if (newchannel->target_cpu != get_cpu()) {
331 				put_cpu();
332 				smp_call_function_single(newchannel->target_cpu,
333 							 percpu_channel_enq,
334 							 newchannel, true);
335 			} else {
336 				percpu_channel_enq(newchannel);
337 				put_cpu();
338 			}
339 
340 			newchannel->state = CHANNEL_OPEN_STATE;
341 			if (channel->sc_creation_callback != NULL)
342 				channel->sc_creation_callback(newchannel);
343 
344 			goto done_init_rescind;
345 		}
346 
347 		goto err_free_chan;
348 	}
349 
350 	/*
351 	 * This state is used to indicate a successful open
352 	 * so that when we do close the channel normally, we
353 	 * can cleanup properly
354 	 */
355 	newchannel->state = CHANNEL_OPEN_STATE;
356 
357 	/*
358 	 * Start the process of binding this offer to the driver
359 	 * We need to set the DeviceObject field before calling
360 	 * vmbus_child_dev_add()
361 	 */
362 	newchannel->device_obj = vmbus_device_create(
363 		&newchannel->offermsg.offer.if_type,
364 		&newchannel->offermsg.offer.if_instance,
365 		newchannel);
366 	if (!newchannel->device_obj)
367 		goto err_free_chan;
368 
369 	/*
370 	 * Add the new device to the bus. This will kick off device-driver
371 	 * binding which eventually invokes the device driver's AddDevice()
372 	 * method.
373 	 */
374 	ret = vmbus_device_register(newchannel->device_obj);
375 	if (ret != 0) {
376 		pr_err("unable to add child device object (relid %d)\n",
377 			   newchannel->offermsg.child_relid);
378 
379 		spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
380 		list_del(&newchannel->listentry);
381 		spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
382 		kfree(newchannel->device_obj);
383 		goto err_free_chan;
384 	}
385 done_init_rescind:
386 	spin_lock_irqsave(&newchannel->lock, flags);
387 	/* The next possible work is rescind handling */
388 	INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
389 	/* Check if rescind offer was already received */
390 	if (newchannel->rescind)
391 		queue_work(newchannel->controlwq, &newchannel->work);
392 	spin_unlock_irqrestore(&newchannel->lock, flags);
393 	return;
394 err_free_chan:
395 	free_channel(newchannel);
396 }
397 
398 enum {
399 	IDE = 0,
400 	SCSI,
401 	NIC,
402 	MAX_PERF_CHN,
403 };
404 
405 /*
406  * This is an array of device_ids (device types) that are performance critical.
407  * We attempt to distribute the interrupt load for these devices across
408  * all available CPUs.
409  */
410 static const struct hv_vmbus_device_id hp_devs[] = {
411 	/* IDE */
412 	{ HV_IDE_GUID, },
413 	/* Storage - SCSI */
414 	{ HV_SCSI_GUID, },
415 	/* Network */
416 	{ HV_NIC_GUID, },
417 };
418 
419 
420 /*
421  * We use this state to statically distribute the channel interrupt load.
422  */
423 static u32  next_vp;
424 
425 /*
426  * Starting with Win8, we can statically distribute the incoming
427  * channel interrupt load by binding a channel to VCPU. We
428  * implement here a simple round robin scheme for distributing
429  * the interrupt load.
430  * We will bind channels that are not performance critical to cpu 0 and
431  * performance critical channels (IDE, SCSI and Network) will be uniformly
432  * distributed across all available CPUs.
433  */
434 static void init_vp_index(struct vmbus_channel *channel, const uuid_le *type_guid)
435 {
436 	u32 cur_cpu;
437 	int i;
438 	bool perf_chn = false;
439 	u32 max_cpus = num_online_cpus();
440 
441 	for (i = IDE; i < MAX_PERF_CHN; i++) {
442 		if (!memcmp(type_guid->b, hp_devs[i].guid,
443 				 sizeof(uuid_le))) {
444 			perf_chn = true;
445 			break;
446 		}
447 	}
448 	if ((vmbus_proto_version == VERSION_WS2008) ||
449 	    (vmbus_proto_version == VERSION_WIN7) || (!perf_chn)) {
450 		/*
451 		 * Prior to win8, all channel interrupts are
452 		 * delivered on cpu 0.
453 		 * Also if the channel is not a performance critical
454 		 * channel, bind it to cpu 0.
455 		 */
456 		channel->target_cpu = 0;
457 		channel->target_vp = 0;
458 		return;
459 	}
460 	cur_cpu = (++next_vp % max_cpus);
461 	channel->target_cpu = cur_cpu;
462 	channel->target_vp = hv_context.vp_index[cur_cpu];
463 }
464 
465 /*
466  * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
467  *
468  */
469 static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
470 {
471 	struct vmbus_channel_offer_channel *offer;
472 	struct vmbus_channel *newchannel;
473 
474 	offer = (struct vmbus_channel_offer_channel *)hdr;
475 
476 	/* Allocate the channel object and save this offer. */
477 	newchannel = alloc_channel();
478 	if (!newchannel) {
479 		pr_err("Unable to allocate channel object\n");
480 		return;
481 	}
482 
483 	/*
484 	 * By default we setup state to enable batched
485 	 * reading. A specific service can choose to
486 	 * disable this prior to opening the channel.
487 	 */
488 	newchannel->batched_reading = true;
489 
490 	/*
491 	 * Setup state for signalling the host.
492 	 */
493 	newchannel->sig_event = (struct hv_input_signal_event *)
494 				(ALIGN((unsigned long)
495 				&newchannel->sig_buf,
496 				HV_HYPERCALL_PARAM_ALIGN));
497 
498 	newchannel->sig_event->connectionid.asu32 = 0;
499 	newchannel->sig_event->connectionid.u.id = VMBUS_EVENT_CONNECTION_ID;
500 	newchannel->sig_event->flag_number = 0;
501 	newchannel->sig_event->rsvdz = 0;
502 
503 	if (vmbus_proto_version != VERSION_WS2008) {
504 		newchannel->is_dedicated_interrupt =
505 				(offer->is_dedicated_interrupt != 0);
506 		newchannel->sig_event->connectionid.u.id =
507 				offer->connection_id;
508 	}
509 
510 	init_vp_index(newchannel, &offer->offer.if_type);
511 
512 	memcpy(&newchannel->offermsg, offer,
513 	       sizeof(struct vmbus_channel_offer_channel));
514 	newchannel->monitor_grp = (u8)offer->monitorid / 32;
515 	newchannel->monitor_bit = (u8)offer->monitorid % 32;
516 
517 	INIT_WORK(&newchannel->work, vmbus_process_offer);
518 	queue_work(newchannel->controlwq, &newchannel->work);
519 }
520 
521 /*
522  * vmbus_onoffer_rescind - Rescind offer handler.
523  *
524  * We queue a work item to process this offer synchronously
525  */
526 static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
527 {
528 	struct vmbus_channel_rescind_offer *rescind;
529 	struct vmbus_channel *channel;
530 	unsigned long flags;
531 
532 	rescind = (struct vmbus_channel_rescind_offer *)hdr;
533 	channel = relid2channel(rescind->child_relid);
534 
535 	if (channel == NULL)
536 		/* Just return here, no channel found */
537 		return;
538 
539 	spin_lock_irqsave(&channel->lock, flags);
540 	channel->rescind = true;
541 	/*
542 	 * channel->work.func != vmbus_process_rescind_offer means we are still
543 	 * processing offer request and the rescind offer processing should be
544 	 * postponed. It will be done at the very end of vmbus_process_offer()
545 	 * as rescind flag is being checked there.
546 	 */
547 	if (channel->work.func == vmbus_process_rescind_offer)
548 		/* work is initialized for vmbus_process_rescind_offer() from
549 		 * vmbus_process_offer() where the channel got created */
550 		queue_work(channel->controlwq, &channel->work);
551 
552 	spin_unlock_irqrestore(&channel->lock, flags);
553 }
554 
555 /*
556  * vmbus_onoffers_delivered -
557  * This is invoked when all offers have been delivered.
558  *
559  * Nothing to do here.
560  */
561 static void vmbus_onoffers_delivered(
562 			struct vmbus_channel_message_header *hdr)
563 {
564 }
565 
566 /*
567  * vmbus_onopen_result - Open result handler.
568  *
569  * This is invoked when we received a response to our channel open request.
570  * Find the matching request, copy the response and signal the requesting
571  * thread.
572  */
573 static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
574 {
575 	struct vmbus_channel_open_result *result;
576 	struct vmbus_channel_msginfo *msginfo;
577 	struct vmbus_channel_message_header *requestheader;
578 	struct vmbus_channel_open_channel *openmsg;
579 	unsigned long flags;
580 
581 	result = (struct vmbus_channel_open_result *)hdr;
582 
583 	/*
584 	 * Find the open msg, copy the result and signal/unblock the wait event
585 	 */
586 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
587 
588 	list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
589 				msglistentry) {
590 		requestheader =
591 			(struct vmbus_channel_message_header *)msginfo->msg;
592 
593 		if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
594 			openmsg =
595 			(struct vmbus_channel_open_channel *)msginfo->msg;
596 			if (openmsg->child_relid == result->child_relid &&
597 			    openmsg->openid == result->openid) {
598 				memcpy(&msginfo->response.open_result,
599 				       result,
600 				       sizeof(
601 					struct vmbus_channel_open_result));
602 				complete(&msginfo->waitevent);
603 				break;
604 			}
605 		}
606 	}
607 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
608 }
609 
610 /*
611  * vmbus_ongpadl_created - GPADL created handler.
612  *
613  * This is invoked when we received a response to our gpadl create request.
614  * Find the matching request, copy the response and signal the requesting
615  * thread.
616  */
617 static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
618 {
619 	struct vmbus_channel_gpadl_created *gpadlcreated;
620 	struct vmbus_channel_msginfo *msginfo;
621 	struct vmbus_channel_message_header *requestheader;
622 	struct vmbus_channel_gpadl_header *gpadlheader;
623 	unsigned long flags;
624 
625 	gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
626 
627 	/*
628 	 * Find the establish msg, copy the result and signal/unblock the wait
629 	 * event
630 	 */
631 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
632 
633 	list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
634 				msglistentry) {
635 		requestheader =
636 			(struct vmbus_channel_message_header *)msginfo->msg;
637 
638 		if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
639 			gpadlheader =
640 			(struct vmbus_channel_gpadl_header *)requestheader;
641 
642 			if ((gpadlcreated->child_relid ==
643 			     gpadlheader->child_relid) &&
644 			    (gpadlcreated->gpadl == gpadlheader->gpadl)) {
645 				memcpy(&msginfo->response.gpadl_created,
646 				       gpadlcreated,
647 				       sizeof(
648 					struct vmbus_channel_gpadl_created));
649 				complete(&msginfo->waitevent);
650 				break;
651 			}
652 		}
653 	}
654 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
655 }
656 
657 /*
658  * vmbus_ongpadl_torndown - GPADL torndown handler.
659  *
660  * This is invoked when we received a response to our gpadl teardown request.
661  * Find the matching request, copy the response and signal the requesting
662  * thread.
663  */
664 static void vmbus_ongpadl_torndown(
665 			struct vmbus_channel_message_header *hdr)
666 {
667 	struct vmbus_channel_gpadl_torndown *gpadl_torndown;
668 	struct vmbus_channel_msginfo *msginfo;
669 	struct vmbus_channel_message_header *requestheader;
670 	struct vmbus_channel_gpadl_teardown *gpadl_teardown;
671 	unsigned long flags;
672 
673 	gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
674 
675 	/*
676 	 * Find the open msg, copy the result and signal/unblock the wait event
677 	 */
678 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
679 
680 	list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
681 				msglistentry) {
682 		requestheader =
683 			(struct vmbus_channel_message_header *)msginfo->msg;
684 
685 		if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
686 			gpadl_teardown =
687 			(struct vmbus_channel_gpadl_teardown *)requestheader;
688 
689 			if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
690 				memcpy(&msginfo->response.gpadl_torndown,
691 				       gpadl_torndown,
692 				       sizeof(
693 					struct vmbus_channel_gpadl_torndown));
694 				complete(&msginfo->waitevent);
695 				break;
696 			}
697 		}
698 	}
699 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
700 }
701 
702 /*
703  * vmbus_onversion_response - Version response handler
704  *
705  * This is invoked when we received a response to our initiate contact request.
706  * Find the matching request, copy the response and signal the requesting
707  * thread.
708  */
709 static void vmbus_onversion_response(
710 		struct vmbus_channel_message_header *hdr)
711 {
712 	struct vmbus_channel_msginfo *msginfo;
713 	struct vmbus_channel_message_header *requestheader;
714 	struct vmbus_channel_version_response *version_response;
715 	unsigned long flags;
716 
717 	version_response = (struct vmbus_channel_version_response *)hdr;
718 	spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
719 
720 	list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
721 				msglistentry) {
722 		requestheader =
723 			(struct vmbus_channel_message_header *)msginfo->msg;
724 
725 		if (requestheader->msgtype ==
726 		    CHANNELMSG_INITIATE_CONTACT) {
727 			memcpy(&msginfo->response.version_response,
728 			      version_response,
729 			      sizeof(struct vmbus_channel_version_response));
730 			complete(&msginfo->waitevent);
731 		}
732 	}
733 	spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
734 }
735 
736 /* Channel message dispatch table */
737 static struct vmbus_channel_message_table_entry
738 	channel_message_table[CHANNELMSG_COUNT] = {
739 	{CHANNELMSG_INVALID,			NULL},
740 	{CHANNELMSG_OFFERCHANNEL,		vmbus_onoffer},
741 	{CHANNELMSG_RESCIND_CHANNELOFFER,	vmbus_onoffer_rescind},
742 	{CHANNELMSG_REQUESTOFFERS,		NULL},
743 	{CHANNELMSG_ALLOFFERS_DELIVERED,	vmbus_onoffers_delivered},
744 	{CHANNELMSG_OPENCHANNEL,		NULL},
745 	{CHANNELMSG_OPENCHANNEL_RESULT,	vmbus_onopen_result},
746 	{CHANNELMSG_CLOSECHANNEL,		NULL},
747 	{CHANNELMSG_GPADL_HEADER,		NULL},
748 	{CHANNELMSG_GPADL_BODY,		NULL},
749 	{CHANNELMSG_GPADL_CREATED,		vmbus_ongpadl_created},
750 	{CHANNELMSG_GPADL_TEARDOWN,		NULL},
751 	{CHANNELMSG_GPADL_TORNDOWN,		vmbus_ongpadl_torndown},
752 	{CHANNELMSG_RELID_RELEASED,		NULL},
753 	{CHANNELMSG_INITIATE_CONTACT,		NULL},
754 	{CHANNELMSG_VERSION_RESPONSE,		vmbus_onversion_response},
755 	{CHANNELMSG_UNLOAD,			NULL},
756 };
757 
758 /*
759  * vmbus_onmessage - Handler for channel protocol messages.
760  *
761  * This is invoked in the vmbus worker thread context.
762  */
763 void vmbus_onmessage(void *context)
764 {
765 	struct hv_message *msg = context;
766 	struct vmbus_channel_message_header *hdr;
767 	int size;
768 
769 	hdr = (struct vmbus_channel_message_header *)msg->u.payload;
770 	size = msg->header.payload_size;
771 
772 	if (hdr->msgtype >= CHANNELMSG_COUNT) {
773 		pr_err("Received invalid channel message type %d size %d\n",
774 			   hdr->msgtype, size);
775 		print_hex_dump_bytes("", DUMP_PREFIX_NONE,
776 				     (unsigned char *)msg->u.payload, size);
777 		return;
778 	}
779 
780 	if (channel_message_table[hdr->msgtype].message_handler)
781 		channel_message_table[hdr->msgtype].message_handler(hdr);
782 	else
783 		pr_err("Unhandled channel message type %d\n", hdr->msgtype);
784 }
785 
786 /*
787  * vmbus_request_offers - Send a request to get all our pending offers.
788  */
789 int vmbus_request_offers(void)
790 {
791 	struct vmbus_channel_message_header *msg;
792 	struct vmbus_channel_msginfo *msginfo;
793 	int ret, t;
794 
795 	msginfo = kmalloc(sizeof(*msginfo) +
796 			  sizeof(struct vmbus_channel_message_header),
797 			  GFP_KERNEL);
798 	if (!msginfo)
799 		return -ENOMEM;
800 
801 	init_completion(&msginfo->waitevent);
802 
803 	msg = (struct vmbus_channel_message_header *)msginfo->msg;
804 
805 	msg->msgtype = CHANNELMSG_REQUESTOFFERS;
806 
807 
808 	ret = vmbus_post_msg(msg,
809 			       sizeof(struct vmbus_channel_message_header));
810 	if (ret != 0) {
811 		pr_err("Unable to request offers - %d\n", ret);
812 
813 		goto cleanup;
814 	}
815 
816 	t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
817 	if (t == 0) {
818 		ret = -ETIMEDOUT;
819 		goto cleanup;
820 	}
821 
822 
823 
824 cleanup:
825 	kfree(msginfo);
826 
827 	return ret;
828 }
829 
830 /*
831  * Retrieve the (sub) channel on which to send an outgoing request.
832  * When a primary channel has multiple sub-channels, we choose a
833  * channel whose VCPU binding is closest to the VCPU on which
834  * this call is being made.
835  */
836 struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary)
837 {
838 	struct list_head *cur, *tmp;
839 	int cur_cpu;
840 	struct vmbus_channel *cur_channel;
841 	struct vmbus_channel *outgoing_channel = primary;
842 	int cpu_distance, new_cpu_distance;
843 
844 	if (list_empty(&primary->sc_list))
845 		return outgoing_channel;
846 
847 	cur_cpu = hv_context.vp_index[get_cpu()];
848 	put_cpu();
849 	list_for_each_safe(cur, tmp, &primary->sc_list) {
850 		cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
851 		if (cur_channel->state != CHANNEL_OPENED_STATE)
852 			continue;
853 
854 		if (cur_channel->target_vp == cur_cpu)
855 			return cur_channel;
856 
857 		cpu_distance = ((outgoing_channel->target_vp > cur_cpu) ?
858 				(outgoing_channel->target_vp - cur_cpu) :
859 				(cur_cpu - outgoing_channel->target_vp));
860 
861 		new_cpu_distance = ((cur_channel->target_vp > cur_cpu) ?
862 				(cur_channel->target_vp - cur_cpu) :
863 				(cur_cpu - cur_channel->target_vp));
864 
865 		if (cpu_distance < new_cpu_distance)
866 			continue;
867 
868 		outgoing_channel = cur_channel;
869 	}
870 
871 	return outgoing_channel;
872 }
873 EXPORT_SYMBOL_GPL(vmbus_get_outgoing_channel);
874 
875 static void invoke_sc_cb(struct vmbus_channel *primary_channel)
876 {
877 	struct list_head *cur, *tmp;
878 	struct vmbus_channel *cur_channel;
879 
880 	if (primary_channel->sc_creation_callback == NULL)
881 		return;
882 
883 	list_for_each_safe(cur, tmp, &primary_channel->sc_list) {
884 		cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
885 
886 		primary_channel->sc_creation_callback(cur_channel);
887 	}
888 }
889 
890 void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
891 				void (*sc_cr_cb)(struct vmbus_channel *new_sc))
892 {
893 	primary_channel->sc_creation_callback = sc_cr_cb;
894 }
895 EXPORT_SYMBOL_GPL(vmbus_set_sc_create_callback);
896 
897 bool vmbus_are_subchannels_present(struct vmbus_channel *primary)
898 {
899 	bool ret;
900 
901 	ret = !list_empty(&primary->sc_list);
902 
903 	if (ret) {
904 		/*
905 		 * Invoke the callback on sub-channel creation.
906 		 * This will present a uniform interface to the
907 		 * clients.
908 		 */
909 		invoke_sc_cb(primary);
910 	}
911 
912 	return ret;
913 }
914 EXPORT_SYMBOL_GPL(vmbus_are_subchannels_present);
915