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