hv: channel: match var type to return type of wait_for_completion
[deliverable/linux.git] / drivers / hv / channel_mgmt.c
CommitLineData
3e7ee490 1/*
3e7ee490
HJ
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>
3e7ee490 20 */
0a46618d
HJ
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
a0086dc5 23#include <linux/kernel.h>
0c3b7b2f
S
24#include <linux/sched.h>
25#include <linux/wait.h>
a0086dc5 26#include <linux/mm.h>
5a0e3ad6 27#include <linux/slab.h>
53af545b 28#include <linux/list.h>
c88c4e4c 29#include <linux/module.h>
8b5d6d3b 30#include <linux/completion.h>
46a97191 31#include <linux/hyperv.h>
3f335ea2 32
0f2a6619 33#include "hyperv_vmbus.h"
3e7ee490 34
1d7e907f 35struct vmbus_channel_message_table_entry {
cf9dcba7 36 enum vmbus_channel_message_type message_type;
fa90f1de 37 void (*message_handler)(struct vmbus_channel_message_header *msg);
1d7e907f 38};
3e7ee490 39
c88c4e4c
HJ
40
41/**
da0e9631 42 * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message
c88c4e4c
HJ
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.
c836d0ab
S
49 * Set up and fill in default negotiate response message.
50 *
6741335b
S
51 * The fw_version specifies the framework version that
52 * we can support and srv_version specifies the service
53 * version we can support.
c88c4e4c
HJ
54 *
55 * Mainly used by Hyper-V drivers.
56 */
6741335b 57bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,
c836d0ab 58 struct icmsg_negotiate *negop, u8 *buf,
6741335b 59 int fw_version, int srv_version)
c88c4e4c 60{
6741335b
S
61 int icframe_major, icframe_minor;
62 int icmsg_major, icmsg_minor;
63 int fw_major, fw_minor;
64 int srv_major, srv_minor;
c836d0ab 65 int i;
6741335b 66 bool found_match = false;
c836d0ab 67
a3605300 68 icmsghdrp->icmsgsize = 0x10;
6741335b
S
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);
c88c4e4c 74
a3605300
S
75 negop = (struct icmsg_negotiate *)&buf[
76 sizeof(struct vmbuspipe_hdr) +
77 sizeof(struct icmsg_hdr)];
c88c4e4c 78
6741335b
S
79 icframe_major = negop->icframe_vercnt;
80 icframe_minor = 0;
81
82 icmsg_major = negop->icmsg_vercnt;
83 icmsg_minor = 0;
c836d0ab
S
84
85 /*
86 * Select the framework version number we will
87 * support.
88 */
89
90 for (i = 0; i < negop->icframe_vercnt; i++) {
6741335b
S
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 }
c836d0ab
S
97 }
98
6741335b
S
99 if (!found_match)
100 goto fw_error;
101
102 found_match = false;
103
c836d0ab
S
104 for (i = negop->icframe_vercnt;
105 (i < negop->icframe_vercnt + negop->icmsg_vercnt); i++) {
6741335b
S
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 }
c88c4e4c 112 }
a3605300 113
c836d0ab 114 /*
6741335b 115 * Respond with the framework and service
c836d0ab
S
116 * version numbers we can support.
117 */
6741335b
S
118
119fw_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;
c88c4e4c 133}
a3605300 134
da0e9631 135EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp);
c88c4e4c 136
3e189519 137/*
e98cb276 138 * alloc_channel - Allocate and initialize a vmbus channel object
bd60c33e 139 */
50fe56d2 140static struct vmbus_channel *alloc_channel(void)
3e7ee490 141{
bc63b6f6 142 static atomic_t chan_num = ATOMIC_INIT(0);
aded7165 143 struct vmbus_channel *channel;
3e7ee490 144
aded7165 145 channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
3e7ee490 146 if (!channel)
3e7ee490 147 return NULL;
3e7ee490 148
bc63b6f6 149 channel->id = atomic_inc_return(&chan_num);
54411c42 150 spin_lock_init(&channel->inbound_lock);
67fae053 151 spin_lock_init(&channel->lock);
e68d2971
S
152
153 INIT_LIST_HEAD(&channel->sc_list);
3a28fa35 154 INIT_LIST_HEAD(&channel->percpu_list);
3e7ee490 155
bc63b6f6
VK
156 channel->controlwq = alloc_workqueue("hv_vmbus_ctl/%d", WQ_MEM_RECLAIM,
157 1, channel->id);
c50f7fb2 158 if (!channel->controlwq) {
8c69f52a 159 kfree(channel);
3e7ee490
HJ
160 return NULL;
161 }
162
163 return channel;
164}
165
3e189519 166/*
e98cb276 167 * release_hannel - Release the vmbus channel object itself
bd60c33e 168 */
4b2f9abe 169static void release_channel(struct work_struct *work)
3e7ee490 170{
4b2f9abe
TT
171 struct vmbus_channel *channel = container_of(work,
172 struct vmbus_channel,
173 work);
3e7ee490 174
c50f7fb2 175 destroy_workqueue(channel->controlwq);
3e7ee490 176
8c69f52a 177 kfree(channel);
3e7ee490
HJ
178}
179
3e189519 180/*
e98cb276 181 * free_channel - Release the resources used by the vmbus channel object
bd60c33e 182 */
9f3e28e3 183static void free_channel(struct vmbus_channel *channel)
3e7ee490 184{
3e7ee490 185
bd60c33e
GKH
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 */
4b2f9abe 191 INIT_WORK(&channel->work, release_channel);
da9fcb72 192 queue_work(vmbus_connection.work_queue, &channel->work);
3e7ee490
HJ
193}
194
3a28fa35
S
195static 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}
8b5d6d3b 202
3a28fa35
S
203static void percpu_channel_deq(void *arg)
204{
205 struct vmbus_channel *channel = arg;
206
207 list_del(&channel->percpu_list);
208}
8b5d6d3b 209
4b2f9abe
TT
210/*
211 * vmbus_process_rescind_offer -
212 * Rescind the offer by initiating a device removal
213 */
214static void vmbus_process_rescind_offer(struct work_struct *work)
215{
216 struct vmbus_channel *channel = container_of(work,
217 struct vmbus_channel,
218 work);
c8705979 219 unsigned long flags;
e68d2971 220 struct vmbus_channel *primary_channel;
c8705979 221 struct vmbus_channel_relid_released msg;
04a258c1
VK
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 }
4b2f9abe 231
c8705979
S
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
2115b561
S
237 if (channel->target_cpu != get_cpu()) {
238 put_cpu();
3a28fa35
S
239 smp_call_function_single(channel->target_cpu,
240 percpu_channel_deq, channel, true);
2115b561 241 } else {
3a28fa35 242 percpu_channel_deq(channel);
2115b561
S
243 put_cpu();
244 }
3a28fa35 245
e68d2971
S
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;
67fae053 252 spin_lock_irqsave(&primary_channel->lock, flags);
565ce642 253 list_del(&channel->sc_list);
67fae053 254 spin_unlock_irqrestore(&primary_channel->lock, flags);
e68d2971 255 }
c8705979 256 free_channel(channel);
4b2f9abe 257}
8b5d6d3b 258
93e5bd06
S
259void 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);
93e5bd06
S
265 free_channel(channel);
266 }
267}
268
3e189519 269/*
e98cb276 270 * vmbus_process_offer - Process the offer by creating a channel/device
c88c4e4c 271 * associated with this offer
bd60c33e 272 */
4b2f9abe 273static void vmbus_process_offer(struct work_struct *work)
3e7ee490 274{
4b2f9abe
TT
275 struct vmbus_channel *newchannel = container_of(work,
276 struct vmbus_channel,
277 work);
aded7165 278 struct vmbus_channel *channel;
188963ec 279 bool fnew = true;
3a28fa35 280 bool enq = false;
bd60c33e 281 int ret;
0f5e44ca 282 unsigned long flags;
3e7ee490 283
454f18a9 284 /* Make sure this is a new offer */
15b2f647 285 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
3e7ee490 286
da9fcb72 287 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
358d2ee2
S
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)) {
188963ec 292 fnew = false;
3e7ee490
HJ
293 break;
294 }
295 }
296
3a28fa35 297 if (fnew) {
c50f7fb2 298 list_add_tail(&newchannel->listentry,
da9fcb72 299 &vmbus_connection.chn_list);
3a28fa35
S
300 enq = true;
301 }
bd60c33e 302
15b2f647 303 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
3e7ee490 304
3a28fa35 305 if (enq) {
2115b561
S
306 if (newchannel->target_cpu != get_cpu()) {
307 put_cpu();
3a28fa35
S
308 smp_call_function_single(newchannel->target_cpu,
309 percpu_channel_enq,
310 newchannel, true);
2115b561 311 } else {
3a28fa35 312 percpu_channel_enq(newchannel);
2115b561
S
313 put_cpu();
314 }
3a28fa35 315 }
188963ec 316 if (!fnew) {
e68d2971
S
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;
67fae053 325 spin_lock_irqsave(&channel->lock, flags);
e68d2971 326 list_add_tail(&newchannel->sc_list, &channel->sc_list);
67fae053 327 spin_unlock_irqrestore(&channel->lock, flags);
3a28fa35 328
2115b561
S
329 if (newchannel->target_cpu != get_cpu()) {
330 put_cpu();
3a28fa35
S
331 smp_call_function_single(newchannel->target_cpu,
332 percpu_channel_enq,
333 newchannel, true);
2115b561 334 } else {
3a28fa35 335 percpu_channel_enq(newchannel);
2115b561
S
336 put_cpu();
337 }
3a28fa35 338
e68d2971
S
339 newchannel->state = CHANNEL_OPEN_STATE;
340 if (channel->sc_creation_callback != NULL)
341 channel->sc_creation_callback(newchannel);
342
d7f2fbaf 343 goto done_init_rescind;
e68d2971
S
344 }
345
9c3a6f7e 346 goto err_free_chan;
3e7ee490
HJ
347 }
348
42dceebe
S
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
bd60c33e
GKH
356 /*
357 * Start the process of binding this offer to the driver
358 * We need to set the DeviceObject field before calling
646f1ea3 359 * vmbus_child_dev_add()
bd60c33e 360 */
f2c73011 361 newchannel->device_obj = vmbus_device_create(
767dff68
HZ
362 &newchannel->offermsg.offer.if_type,
363 &newchannel->offermsg.offer.if_instance,
188963ec 364 newchannel);
9c3a6f7e
VK
365 if (!newchannel->device_obj)
366 goto err_free_chan;
3e7ee490 367
454f18a9
BP
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 */
22794281 373 ret = vmbus_device_register(newchannel->device_obj);
bd60c33e 374 if (ret != 0) {
0a46618d 375 pr_err("unable to add child device object (relid %d)\n",
c50f7fb2 376 newchannel->offermsg.child_relid);
3e7ee490 377
15b2f647 378 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
c50f7fb2 379 list_del(&newchannel->listentry);
15b2f647 380 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
8b8ee675 381 kfree(newchannel->device_obj);
9c3a6f7e 382 goto err_free_chan;
3e7ee490 383 }
d7f2fbaf
VK
384done_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);
9c3a6f7e
VK
392 return;
393err_free_chan:
394 free_channel(newchannel);
3e7ee490
HJ
395}
396
a119845f
S
397enum {
398 IDE = 0,
399 SCSI,
400 NIC,
401 MAX_PERF_CHN,
402};
403
404/*
7fb96565 405 * This is an array of device_ids (device types) that are performance critical.
a119845f
S
406 * We attempt to distribute the interrupt load for these devices across
407 * all available CPUs.
408 */
7fb96565 409static const struct hv_vmbus_device_id hp_devs[] = {
a119845f 410 /* IDE */
7fb96565 411 { HV_IDE_GUID, },
a119845f 412 /* Storage - SCSI */
7fb96565 413 { HV_SCSI_GUID, },
a119845f 414 /* Network */
7fb96565 415 { HV_NIC_GUID, },
a119845f
S
416};
417
418
419/*
420 * We use this state to statically distribute the channel interrupt load.
421 */
422static u32 next_vp;
423
424/*
425 * Starting with Win8, we can statically distribute the incoming
426 * channel interrupt load by binding a channel to VCPU. We
427 * implement here a simple round robin scheme for distributing
428 * the interrupt load.
429 * We will bind channels that are not performance critical to cpu 0 and
430 * performance critical channels (IDE, SCSI and Network) will be uniformly
431 * distributed across all available CPUs.
432 */
f9da455b 433static void init_vp_index(struct vmbus_channel *channel, const uuid_le *type_guid)
a119845f
S
434{
435 u32 cur_cpu;
436 int i;
437 bool perf_chn = false;
438 u32 max_cpus = num_online_cpus();
439
440 for (i = IDE; i < MAX_PERF_CHN; i++) {
7fb96565 441 if (!memcmp(type_guid->b, hp_devs[i].guid,
a119845f
S
442 sizeof(uuid_le))) {
443 perf_chn = true;
444 break;
445 }
446 }
447 if ((vmbus_proto_version == VERSION_WS2008) ||
448 (vmbus_proto_version == VERSION_WIN7) || (!perf_chn)) {
449 /*
450 * Prior to win8, all channel interrupts are
451 * delivered on cpu 0.
452 * Also if the channel is not a performance critical
453 * channel, bind it to cpu 0.
454 */
d3ba720d
S
455 channel->target_cpu = 0;
456 channel->target_vp = 0;
457 return;
a119845f
S
458 }
459 cur_cpu = (++next_vp % max_cpus);
d3ba720d
S
460 channel->target_cpu = cur_cpu;
461 channel->target_vp = hv_context.vp_index[cur_cpu];
a119845f
S
462}
463
3e189519 464/*
e98cb276 465 * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
bd60c33e 466 *
bd60c33e 467 */
e98cb276 468static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
3e7ee490 469{
bd60c33e 470 struct vmbus_channel_offer_channel *offer;
188963ec 471 struct vmbus_channel *newchannel;
3e7ee490 472
bd60c33e 473 offer = (struct vmbus_channel_offer_channel *)hdr;
3e7ee490 474
454f18a9 475 /* Allocate the channel object and save this offer. */
e98cb276 476 newchannel = alloc_channel();
188963ec 477 if (!newchannel) {
0a46618d 478 pr_err("Unable to allocate channel object\n");
3e7ee490
HJ
479 return;
480 }
481
132368bd
S
482 /*
483 * By default we setup state to enable batched
484 * reading. A specific service can choose to
485 * disable this prior to opening the channel.
486 */
487 newchannel->batched_reading = true;
488
b3bf60c7
S
489 /*
490 * Setup state for signalling the host.
491 */
492 newchannel->sig_event = (struct hv_input_signal_event *)
493 (ALIGN((unsigned long)
494 &newchannel->sig_buf,
495 HV_HYPERCALL_PARAM_ALIGN));
496
497 newchannel->sig_event->connectionid.asu32 = 0;
498 newchannel->sig_event->connectionid.u.id = VMBUS_EVENT_CONNECTION_ID;
499 newchannel->sig_event->flag_number = 0;
500 newchannel->sig_event->rsvdz = 0;
501
502 if (vmbus_proto_version != VERSION_WS2008) {
503 newchannel->is_dedicated_interrupt =
504 (offer->is_dedicated_interrupt != 0);
505 newchannel->sig_event->connectionid.u.id =
506 offer->connection_id;
507 }
508
d3ba720d 509 init_vp_index(newchannel, &offer->offer.if_type);
abbf3b2a 510
c50f7fb2 511 memcpy(&newchannel->offermsg, offer,
bd60c33e 512 sizeof(struct vmbus_channel_offer_channel));
c50f7fb2
HZ
513 newchannel->monitor_grp = (u8)offer->monitorid / 32;
514 newchannel->monitor_bit = (u8)offer->monitorid % 32;
3e7ee490 515
4b2f9abe
TT
516 INIT_WORK(&newchannel->work, vmbus_process_offer);
517 queue_work(newchannel->controlwq, &newchannel->work);
3e7ee490
HJ
518}
519
3e189519 520/*
e98cb276 521 * vmbus_onoffer_rescind - Rescind offer handler.
bd60c33e
GKH
522 *
523 * We queue a work item to process this offer synchronously
524 */
e98cb276 525static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
3e7ee490 526{
bd60c33e 527 struct vmbus_channel_rescind_offer *rescind;
aded7165 528 struct vmbus_channel *channel;
d7f2fbaf 529 unsigned long flags;
3e7ee490 530
bd60c33e 531 rescind = (struct vmbus_channel_rescind_offer *)hdr;
c6977677 532 channel = relid2channel(rescind->child_relid);
98e08702
HJ
533
534 if (channel == NULL)
535 /* Just return here, no channel found */
3e7ee490 536 return;
3e7ee490 537
d7f2fbaf 538 spin_lock_irqsave(&channel->lock, flags);
c3582a2c 539 channel->rescind = true;
d7f2fbaf
VK
540 /*
541 * channel->work.func != vmbus_process_rescind_offer means we are still
542 * processing offer request and the rescind offer processing should be
543 * postponed. It will be done at the very end of vmbus_process_offer()
544 * as rescind flag is being checked there.
545 */
546 if (channel->work.func == vmbus_process_rescind_offer)
547 /* work is initialized for vmbus_process_rescind_offer() from
548 * vmbus_process_offer() where the channel got created */
549 queue_work(channel->controlwq, &channel->work);
c3582a2c 550
d7f2fbaf 551 spin_unlock_irqrestore(&channel->lock, flags);
3e7ee490
HJ
552}
553
3e189519 554/*
e98cb276
HZ
555 * vmbus_onoffers_delivered -
556 * This is invoked when all offers have been delivered.
bd60c33e
GKH
557 *
558 * Nothing to do here.
559 */
e98cb276 560static void vmbus_onoffers_delivered(
bd60c33e 561 struct vmbus_channel_message_header *hdr)
3e7ee490 562{
3e7ee490
HJ
563}
564
3e189519 565/*
e98cb276 566 * vmbus_onopen_result - Open result handler.
bd60c33e
GKH
567 *
568 * This is invoked when we received a response to our channel open request.
569 * Find the matching request, copy the response and signal the requesting
570 * thread.
571 */
e98cb276 572static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
3e7ee490 573{
bd60c33e 574 struct vmbus_channel_open_result *result;
188963ec
HZ
575 struct vmbus_channel_msginfo *msginfo;
576 struct vmbus_channel_message_header *requestheader;
577 struct vmbus_channel_open_channel *openmsg;
dd0813b6 578 unsigned long flags;
3e7ee490 579
bd60c33e 580 result = (struct vmbus_channel_open_result *)hdr;
3e7ee490 581
bd60c33e
GKH
582 /*
583 * Find the open msg, copy the result and signal/unblock the wait event
584 */
15b2f647 585 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
3e7ee490 586
ebb61e5f
HJ
587 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
588 msglistentry) {
188963ec 589 requestheader =
c50f7fb2 590 (struct vmbus_channel_message_header *)msginfo->msg;
188963ec 591
c50f7fb2 592 if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
188963ec 593 openmsg =
c50f7fb2
HZ
594 (struct vmbus_channel_open_channel *)msginfo->msg;
595 if (openmsg->child_relid == result->child_relid &&
596 openmsg->openid == result->openid) {
597 memcpy(&msginfo->response.open_result,
bd60c33e 598 result,
9568a193
S
599 sizeof(
600 struct vmbus_channel_open_result));
601 complete(&msginfo->waitevent);
3e7ee490
HJ
602 break;
603 }
604 }
605 }
15b2f647 606 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
3e7ee490
HJ
607}
608
3e189519 609/*
e98cb276 610 * vmbus_ongpadl_created - GPADL created handler.
bd60c33e
GKH
611 *
612 * This is invoked when we received a response to our gpadl create request.
613 * Find the matching request, copy the response and signal the requesting
614 * thread.
615 */
e98cb276 616static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
3e7ee490 617{
188963ec 618 struct vmbus_channel_gpadl_created *gpadlcreated;
188963ec
HZ
619 struct vmbus_channel_msginfo *msginfo;
620 struct vmbus_channel_message_header *requestheader;
621 struct vmbus_channel_gpadl_header *gpadlheader;
dd0813b6 622 unsigned long flags;
3e7ee490 623
188963ec 624 gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
3e7ee490 625
bd60c33e
GKH
626 /*
627 * Find the establish msg, copy the result and signal/unblock the wait
628 * event
629 */
15b2f647 630 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
3e7ee490 631
ebb61e5f
HJ
632 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
633 msglistentry) {
188963ec 634 requestheader =
c50f7fb2 635 (struct vmbus_channel_message_header *)msginfo->msg;
188963ec 636
c50f7fb2 637 if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
188963ec
HZ
638 gpadlheader =
639 (struct vmbus_channel_gpadl_header *)requestheader;
640
c50f7fb2
HZ
641 if ((gpadlcreated->child_relid ==
642 gpadlheader->child_relid) &&
643 (gpadlcreated->gpadl == gpadlheader->gpadl)) {
644 memcpy(&msginfo->response.gpadl_created,
188963ec 645 gpadlcreated,
9568a193
S
646 sizeof(
647 struct vmbus_channel_gpadl_created));
648 complete(&msginfo->waitevent);
3e7ee490
HJ
649 break;
650 }
651 }
652 }
15b2f647 653 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
3e7ee490
HJ
654}
655
3e189519 656/*
e98cb276 657 * vmbus_ongpadl_torndown - GPADL torndown handler.
bd60c33e
GKH
658 *
659 * This is invoked when we received a response to our gpadl teardown request.
660 * Find the matching request, copy the response and signal the requesting
661 * thread.
662 */
e98cb276 663static void vmbus_ongpadl_torndown(
bd60c33e 664 struct vmbus_channel_message_header *hdr)
3e7ee490 665{
188963ec 666 struct vmbus_channel_gpadl_torndown *gpadl_torndown;
188963ec
HZ
667 struct vmbus_channel_msginfo *msginfo;
668 struct vmbus_channel_message_header *requestheader;
669 struct vmbus_channel_gpadl_teardown *gpadl_teardown;
dd0813b6 670 unsigned long flags;
3e7ee490 671
188963ec 672 gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
bd60c33e
GKH
673
674 /*
675 * Find the open msg, copy the result and signal/unblock the wait event
676 */
15b2f647 677 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
3e7ee490 678
ebb61e5f
HJ
679 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
680 msglistentry) {
188963ec 681 requestheader =
c50f7fb2 682 (struct vmbus_channel_message_header *)msginfo->msg;
3e7ee490 683
c50f7fb2 684 if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
188963ec
HZ
685 gpadl_teardown =
686 (struct vmbus_channel_gpadl_teardown *)requestheader;
3e7ee490 687
c50f7fb2
HZ
688 if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
689 memcpy(&msginfo->response.gpadl_torndown,
188963ec 690 gpadl_torndown,
9568a193
S
691 sizeof(
692 struct vmbus_channel_gpadl_torndown));
693 complete(&msginfo->waitevent);
3e7ee490
HJ
694 break;
695 }
696 }
697 }
15b2f647 698 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
3e7ee490
HJ
699}
700
3e189519 701/*
e98cb276 702 * vmbus_onversion_response - Version response handler
bd60c33e
GKH
703 *
704 * This is invoked when we received a response to our initiate contact request.
705 * Find the matching request, copy the response and signal the requesting
706 * thread.
707 */
e98cb276 708static void vmbus_onversion_response(
bd60c33e 709 struct vmbus_channel_message_header *hdr)
3e7ee490 710{
188963ec
HZ
711 struct vmbus_channel_msginfo *msginfo;
712 struct vmbus_channel_message_header *requestheader;
188963ec 713 struct vmbus_channel_version_response *version_response;
dd0813b6 714 unsigned long flags;
3e7ee490 715
188963ec 716 version_response = (struct vmbus_channel_version_response *)hdr;
15b2f647 717 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
3e7ee490 718
ebb61e5f
HJ
719 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
720 msglistentry) {
188963ec 721 requestheader =
c50f7fb2 722 (struct vmbus_channel_message_header *)msginfo->msg;
3e7ee490 723
c50f7fb2
HZ
724 if (requestheader->msgtype ==
725 CHANNELMSG_INITIATE_CONTACT) {
c50f7fb2 726 memcpy(&msginfo->response.version_response,
188963ec 727 version_response,
bd60c33e 728 sizeof(struct vmbus_channel_version_response));
9568a193 729 complete(&msginfo->waitevent);
3e7ee490
HJ
730 }
731 }
15b2f647 732 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
3e7ee490
HJ
733}
734
c8212f04
GKH
735/* Channel message dispatch table */
736static struct vmbus_channel_message_table_entry
b7c6b02f 737 channel_message_table[CHANNELMSG_COUNT] = {
c50f7fb2
HZ
738 {CHANNELMSG_INVALID, NULL},
739 {CHANNELMSG_OFFERCHANNEL, vmbus_onoffer},
740 {CHANNELMSG_RESCIND_CHANNELOFFER, vmbus_onoffer_rescind},
741 {CHANNELMSG_REQUESTOFFERS, NULL},
742 {CHANNELMSG_ALLOFFERS_DELIVERED, vmbus_onoffers_delivered},
743 {CHANNELMSG_OPENCHANNEL, NULL},
744 {CHANNELMSG_OPENCHANNEL_RESULT, vmbus_onopen_result},
745 {CHANNELMSG_CLOSECHANNEL, NULL},
746 {CHANNELMSG_GPADL_HEADER, NULL},
747 {CHANNELMSG_GPADL_BODY, NULL},
748 {CHANNELMSG_GPADL_CREATED, vmbus_ongpadl_created},
749 {CHANNELMSG_GPADL_TEARDOWN, NULL},
750 {CHANNELMSG_GPADL_TORNDOWN, vmbus_ongpadl_torndown},
751 {CHANNELMSG_RELID_RELEASED, NULL},
752 {CHANNELMSG_INITIATE_CONTACT, NULL},
753 {CHANNELMSG_VERSION_RESPONSE, vmbus_onversion_response},
754 {CHANNELMSG_UNLOAD, NULL},
c8212f04
GKH
755};
756
3e189519 757/*
e98cb276 758 * vmbus_onmessage - Handler for channel protocol messages.
bd60c33e
GKH
759 *
760 * This is invoked in the vmbus worker thread context.
761 */
e98cb276 762void vmbus_onmessage(void *context)
3e7ee490 763{
188963ec 764 struct hv_message *msg = context;
82250213 765 struct vmbus_channel_message_header *hdr;
3e7ee490
HJ
766 int size;
767
f6feebe0
HZ
768 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
769 size = msg->header.payload_size;
3e7ee490 770
c50f7fb2 771 if (hdr->msgtype >= CHANNELMSG_COUNT) {
0a46618d 772 pr_err("Received invalid channel message type %d size %d\n",
c50f7fb2 773 hdr->msgtype, size);
04f50c4d 774 print_hex_dump_bytes("", DUMP_PREFIX_NONE,
f6feebe0 775 (unsigned char *)msg->u.payload, size);
3e7ee490
HJ
776 return;
777 }
778
b7c6b02f
S
779 if (channel_message_table[hdr->msgtype].message_handler)
780 channel_message_table[hdr->msgtype].message_handler(hdr);
3e7ee490 781 else
0a46618d 782 pr_err("Unhandled channel message type %d\n", hdr->msgtype);
3e7ee490
HJ
783}
784
3e189519 785/*
e98cb276 786 * vmbus_request_offers - Send a request to get all our pending offers.
bd60c33e 787 */
e98cb276 788int vmbus_request_offers(void)
3e7ee490 789{
82250213 790 struct vmbus_channel_message_header *msg;
188963ec 791 struct vmbus_channel_msginfo *msginfo;
9568a193 792 int ret, t;
3e7ee490 793
188963ec 794 msginfo = kmalloc(sizeof(*msginfo) +
bd60c33e
GKH
795 sizeof(struct vmbus_channel_message_header),
796 GFP_KERNEL);
188963ec 797 if (!msginfo)
75910f23 798 return -ENOMEM;
3e7ee490 799
9568a193 800 init_completion(&msginfo->waitevent);
75910f23 801
c50f7fb2 802 msg = (struct vmbus_channel_message_header *)msginfo->msg;
3e7ee490 803
c50f7fb2 804 msg->msgtype = CHANNELMSG_REQUESTOFFERS;
3e7ee490 805
3e7ee490 806
c6977677 807 ret = vmbus_post_msg(msg,
bd60c33e
GKH
808 sizeof(struct vmbus_channel_message_header));
809 if (ret != 0) {
0a46618d 810 pr_err("Unable to request offers - %d\n", ret);
3e7ee490 811
0c3b7b2f
S
812 goto cleanup;
813 }
3e7ee490 814
2dfde964 815 t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
9568a193 816 if (t == 0) {
0c3b7b2f
S
817 ret = -ETIMEDOUT;
818 goto cleanup;
3e7ee490 819 }
3e7ee490 820
3e7ee490
HJ
821
822
0c3b7b2f 823cleanup:
dd9b15dc 824 kfree(msginfo);
3e7ee490 825
3e7ee490
HJ
826 return ret;
827}
828
e68d2971
S
829/*
830 * Retrieve the (sub) channel on which to send an outgoing request.
831 * When a primary channel has multiple sub-channels, we choose a
832 * channel whose VCPU binding is closest to the VCPU on which
833 * this call is being made.
834 */
835struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary)
836{
837 struct list_head *cur, *tmp;
87712bf8 838 int cur_cpu;
e68d2971
S
839 struct vmbus_channel *cur_channel;
840 struct vmbus_channel *outgoing_channel = primary;
841 int cpu_distance, new_cpu_distance;
842
843 if (list_empty(&primary->sc_list))
844 return outgoing_channel;
845
87712bf8
S
846 cur_cpu = hv_context.vp_index[get_cpu()];
847 put_cpu();
e68d2971
S
848 list_for_each_safe(cur, tmp, &primary->sc_list) {
849 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
850 if (cur_channel->state != CHANNEL_OPENED_STATE)
851 continue;
852
853 if (cur_channel->target_vp == cur_cpu)
854 return cur_channel;
855
856 cpu_distance = ((outgoing_channel->target_vp > cur_cpu) ?
857 (outgoing_channel->target_vp - cur_cpu) :
858 (cur_cpu - outgoing_channel->target_vp));
859
860 new_cpu_distance = ((cur_channel->target_vp > cur_cpu) ?
861 (cur_channel->target_vp - cur_cpu) :
862 (cur_cpu - cur_channel->target_vp));
863
864 if (cpu_distance < new_cpu_distance)
865 continue;
866
867 outgoing_channel = cur_channel;
868 }
869
870 return outgoing_channel;
871}
872EXPORT_SYMBOL_GPL(vmbus_get_outgoing_channel);
873
874static void invoke_sc_cb(struct vmbus_channel *primary_channel)
875{
876 struct list_head *cur, *tmp;
877 struct vmbus_channel *cur_channel;
878
879 if (primary_channel->sc_creation_callback == NULL)
880 return;
881
882 list_for_each_safe(cur, tmp, &primary_channel->sc_list) {
883 cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
884
885 primary_channel->sc_creation_callback(cur_channel);
886 }
887}
888
889void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
890 void (*sc_cr_cb)(struct vmbus_channel *new_sc))
891{
892 primary_channel->sc_creation_callback = sc_cr_cb;
893}
894EXPORT_SYMBOL_GPL(vmbus_set_sc_create_callback);
895
896bool vmbus_are_subchannels_present(struct vmbus_channel *primary)
897{
898 bool ret;
899
900 ret = !list_empty(&primary->sc_list);
901
902 if (ret) {
903 /*
904 * Invoke the callback on sub-channel creation.
905 * This will present a uniform interface to the
906 * clients.
907 */
908 invoke_sc_cb(primary);
909 }
910
911 return ret;
912}
913EXPORT_SYMBOL_GPL(vmbus_are_subchannels_present);
This page took 0.461488 seconds and 5 git commands to generate.