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