Staging: hv: vmbus_drv: Rename the variable gChannelMessageTable
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / staging / 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>
e3fe0bb6 31#include "hv_api.h"
645954c5 32#include "logging.h"
72daf320 33#include "vmbus_private.h"
c88c4e4c 34#include "utils.h"
3e7ee490 35
1d7e907f 36struct vmbus_channel_message_table_entry {
cf9dcba7 37 enum vmbus_channel_message_type message_type;
fa90f1de 38 void (*message_handler)(struct vmbus_channel_message_header *msg);
1d7e907f 39};
3e7ee490 40
245ba56a
KS
41#define MAX_MSG_TYPES 4
42#define MAX_NUM_DEVICE_CLASSES_SUPPORTED 8
c88c4e4c 43
bd60c33e 44static const struct hv_guid
50b03266 45 supported_device_classes[MAX_NUM_DEVICE_CLASSES_SUPPORTED] = {
454f18a9 46 /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */
caf26a31
GKH
47 /* Storage - SCSI */
48 {
49 .data = {
50 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d,
51 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f
52 }
53 },
54
454f18a9 55 /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */
caf26a31
GKH
56 /* Network */
57 {
58 .data = {
59 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46,
60 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E
61 }
62 },
63
454f18a9 64 /* {CFA8B69E-5B4A-4cc0-B98B-8BA1A1F3F95A} */
caf26a31
GKH
65 /* Input */
66 {
67 .data = {
68 0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c,
69 0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A
70 }
71 },
3e7ee490 72
caf26a31
GKH
73 /* {32412632-86cb-44a2-9b5c-50d1417354f5} */
74 /* IDE */
75 {
76 .data = {
77 0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44,
78 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5
79 }
80 },
c88c4e4c
HJ
81 /* 0E0B6031-5213-4934-818B-38D90CED39DB */
82 /* Shutdown */
83 {
84 .data = {
85 0x31, 0x60, 0x0B, 0X0E, 0x13, 0x52, 0x34, 0x49,
86 0x81, 0x8B, 0x38, 0XD9, 0x0C, 0xED, 0x39, 0xDB
87 }
88 },
39c4e9c3
HZ
89 /* {9527E630-D0AE-497b-ADCE-E80AB0175CAF} */
90 /* TimeSync */
91 {
92 .data = {
93 0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
94 0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf
95 }
96 },
9153f7b9
HJ
97 /* {57164f39-9115-4e78-ab55-382f3bd5422d} */
98 /* Heartbeat */
99 {
100 .data = {
101 0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e,
102 0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d
103 }
104 },
245ba56a
KS
105 /* {A9A0F4E7-5A45-4d96-B827-8A841E8C03E6} */
106 /* KVP */
107 {
108 .data = {
109 0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d,
110 0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x3, 0xe6
111 }
112 },
113
3e7ee490
HJ
114};
115
c88c4e4c
HJ
116
117/**
118 * prep_negotiate_resp() - Create default response for Hyper-V Negotiate message
119 * @icmsghdrp: Pointer to msg header structure
120 * @icmsg_negotiate: Pointer to negotiate message structure
121 * @buf: Raw buffer channel data
122 *
123 * @icmsghdrp is of type &struct icmsg_hdr.
124 * @negop is of type &struct icmsg_negotiate.
125 * Set up and fill in default negotiate response message. This response can
126 * come from both the vmbus driver and the hv_utils driver. The current api
127 * will respond properly to both Windows 2008 and Windows 2008-R2 operating
128 * systems.
129 *
130 * Mainly used by Hyper-V drivers.
131 */
132void prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,
133 struct icmsg_negotiate *negop,
134 u8 *buf)
135{
136 if (icmsghdrp->icmsgtype == ICMSGTYPE_NEGOTIATE) {
137 icmsghdrp->icmsgsize = 0x10;
138
139 negop = (struct icmsg_negotiate *)&buf[
140 sizeof(struct vmbuspipe_hdr) +
141 sizeof(struct icmsg_hdr)];
142
143 if (negop->icframe_vercnt == 2 &&
144 negop->icversion_data[1].major == 3) {
145 negop->icversion_data[0].major = 3;
146 negop->icversion_data[0].minor = 0;
147 negop->icversion_data[1].major = 3;
148 negop->icversion_data[1].minor = 0;
149 } else {
150 negop->icversion_data[0].major = 1;
151 negop->icversion_data[0].minor = 0;
152 negop->icversion_data[1].major = 1;
153 negop->icversion_data[1].minor = 0;
154 }
155
156 negop->icframe_vercnt = 1;
157 negop->icmsg_vercnt = 1;
158 }
159}
160EXPORT_SYMBOL(prep_negotiate_resp);
161
162/**
163 * chn_cb_negotiate() - Default handler for non IDE/SCSI/NETWORK
164 * Hyper-V requests
165 * @context: Pointer to argument structure.
166 *
167 * Set up the default handler for non device driver specific requests
168 * from Hyper-V. This stub responds to the default negotiate messages
169 * that come in for every non IDE/SCSI/Network request.
170 * This behavior is normally overwritten in the hv_utils driver. That
25985edc 171 * driver handles requests like graceful shutdown, heartbeats etc.
c88c4e4c
HJ
172 *
173 * Mainly used by Hyper-V drivers.
174 */
175void chn_cb_negotiate(void *context)
176{
177 struct vmbus_channel *channel = context;
178 u8 *buf;
179 u32 buflen, recvlen;
180 u64 requestid;
181
182 struct icmsg_hdr *icmsghdrp;
183 struct icmsg_negotiate *negop = NULL;
184
185 buflen = PAGE_SIZE;
186 buf = kmalloc(buflen, GFP_ATOMIC);
187
fff41b2e 188 vmbus_recvpacket(channel, buf, buflen, &recvlen, &requestid);
c88c4e4c
HJ
189
190 if (recvlen > 0) {
191 icmsghdrp = (struct icmsg_hdr *)&buf[
192 sizeof(struct vmbuspipe_hdr)];
193
194 prep_negotiate_resp(icmsghdrp, negop, buf);
195
196 icmsghdrp->icflags = ICMSGHDRFLAG_TRANSACTION
197 | ICMSGHDRFLAG_RESPONSE;
198
fff41b2e 199 vmbus_sendpacket(channel, buf,
c88c4e4c 200 recvlen, requestid,
415f2287 201 VM_PKT_DATA_INBAND, 0);
c88c4e4c
HJ
202 }
203
204 kfree(buf);
205}
206EXPORT_SYMBOL(chn_cb_negotiate);
207
208/*
209 * Function table used for message responses for non IDE/SCSI/Network type
210 * messages. (Such as KVP/Shutdown etc)
211 */
212struct hyperv_service_callback hv_cb_utils[MAX_MSG_TYPES] = {
213 /* 0E0B6031-5213-4934-818B-38D90CED39DB */
214 /* Shutdown */
215 {
216 .msg_type = HV_SHUTDOWN_MSG,
217 .data = {
218 0x31, 0x60, 0x0B, 0X0E, 0x13, 0x52, 0x34, 0x49,
219 0x81, 0x8B, 0x38, 0XD9, 0x0C, 0xED, 0x39, 0xDB
220 },
221 .callback = chn_cb_negotiate,
222 .log_msg = "Shutdown channel functionality initialized"
223 },
39c4e9c3
HZ
224
225 /* {9527E630-D0AE-497b-ADCE-E80AB0175CAF} */
226 /* TimeSync */
227 {
228 .msg_type = HV_TIMESYNC_MSG,
229 .data = {
230 0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49,
231 0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf
232 },
233 .callback = chn_cb_negotiate,
234 .log_msg = "Timesync channel functionality initialized"
235 },
9153f7b9
HJ
236 /* {57164f39-9115-4e78-ab55-382f3bd5422d} */
237 /* Heartbeat */
238 {
239 .msg_type = HV_HEARTBEAT_MSG,
240 .data = {
241 0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e,
242 0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d
243 },
244 .callback = chn_cb_negotiate,
245 .log_msg = "Heartbeat channel functionality initialized"
246 },
245ba56a
KS
247 /* {A9A0F4E7-5A45-4d96-B827-8A841E8C03E6} */
248 /* KVP */
249 {
250 .data = {
251 0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d,
252 0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x3, 0xe6
253 },
254 .callback = chn_cb_negotiate,
255 .log_msg = "KVP channel functionality initialized"
256 },
c88c4e4c
HJ
257};
258EXPORT_SYMBOL(hv_cb_utils);
259
3e189519 260/*
e98cb276 261 * alloc_channel - Allocate and initialize a vmbus channel object
bd60c33e 262 */
50fe56d2 263static struct vmbus_channel *alloc_channel(void)
3e7ee490 264{
aded7165 265 struct vmbus_channel *channel;
3e7ee490 266
aded7165 267 channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
3e7ee490 268 if (!channel)
3e7ee490 269 return NULL;
3e7ee490 270
54411c42 271 spin_lock_init(&channel->inbound_lock);
3e7ee490 272
c8a429a4
GKH
273 init_timer(&channel->poll_timer);
274 channel->poll_timer.data = (unsigned long)channel;
fff41b2e 275 channel->poll_timer.function = vmbus_ontimer;
3e7ee490 276
c50f7fb2
HZ
277 channel->controlwq = create_workqueue("hv_vmbus_ctl");
278 if (!channel->controlwq) {
8c69f52a 279 kfree(channel);
3e7ee490
HJ
280 return NULL;
281 }
282
283 return channel;
284}
285
3e189519 286/*
e98cb276 287 * release_hannel - Release the vmbus channel object itself
bd60c33e 288 */
4b2f9abe 289static void release_channel(struct work_struct *work)
3e7ee490 290{
4b2f9abe
TT
291 struct vmbus_channel *channel = container_of(work,
292 struct vmbus_channel,
293 work);
3e7ee490 294
c50f7fb2 295 destroy_workqueue(channel->controlwq);
3e7ee490 296
8c69f52a 297 kfree(channel);
3e7ee490
HJ
298}
299
3e189519 300/*
e98cb276 301 * free_channel - Release the resources used by the vmbus channel object
bd60c33e 302 */
e98cb276 303void free_channel(struct vmbus_channel *channel)
3e7ee490 304{
188963ec 305 del_timer_sync(&channel->poll_timer);
3e7ee490 306
bd60c33e
GKH
307 /*
308 * We have to release the channel's workqueue/thread in the vmbus's
309 * workqueue/thread context
310 * ie we can't destroy ourselves.
311 */
4b2f9abe 312 INIT_WORK(&channel->work, release_channel);
da9fcb72 313 queue_work(vmbus_connection.work_queue, &channel->work);
3e7ee490
HJ
314}
315
8b5d6d3b
HZ
316
317DECLARE_COMPLETION(hv_channel_ready);
318
319/*
320 * Count initialized channels, and ensure all channels are ready when hv_vmbus
321 * module loading completes.
322 */
323static void count_hv_channel(void)
324{
325 static int counter;
326 unsigned long flags;
327
15b2f647 328 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
8b5d6d3b
HZ
329 if (++counter == MAX_MSG_TYPES)
330 complete(&hv_channel_ready);
15b2f647 331 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
8b5d6d3b
HZ
332}
333
4b2f9abe
TT
334/*
335 * vmbus_process_rescind_offer -
336 * Rescind the offer by initiating a device removal
337 */
338static void vmbus_process_rescind_offer(struct work_struct *work)
339{
340 struct vmbus_channel *channel = container_of(work,
341 struct vmbus_channel,
342 work);
343
344 vmbus_child_device_unregister(channel->device_obj);
345}
8b5d6d3b 346
3e189519 347/*
e98cb276 348 * vmbus_process_offer - Process the offer by creating a channel/device
c88c4e4c 349 * associated with this offer
bd60c33e 350 */
4b2f9abe 351static void vmbus_process_offer(struct work_struct *work)
3e7ee490 352{
4b2f9abe
TT
353 struct vmbus_channel *newchannel = container_of(work,
354 struct vmbus_channel,
355 work);
aded7165 356 struct vmbus_channel *channel;
188963ec 357 bool fnew = true;
bd60c33e 358 int ret;
c88c4e4c 359 int cnt;
0f5e44ca 360 unsigned long flags;
3e7ee490 361
4b2f9abe
TT
362 /* The next possible work is rescind handling */
363 INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
364
454f18a9 365 /* Make sure this is a new offer */
15b2f647 366 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
3e7ee490 367
da9fcb72 368 list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
767dff68
HZ
369 if (!memcmp(&channel->offermsg.offer.if_type,
370 &newchannel->offermsg.offer.if_type,
bd60c33e 371 sizeof(struct hv_guid)) &&
767dff68
HZ
372 !memcmp(&channel->offermsg.offer.if_instance,
373 &newchannel->offermsg.offer.if_instance,
bd60c33e 374 sizeof(struct hv_guid))) {
188963ec 375 fnew = false;
3e7ee490
HJ
376 break;
377 }
378 }
379
188963ec 380 if (fnew)
c50f7fb2 381 list_add_tail(&newchannel->listentry,
da9fcb72 382 &vmbus_connection.chn_list);
bd60c33e 383
15b2f647 384 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
3e7ee490 385
188963ec 386 if (!fnew) {
e98cb276 387 free_channel(newchannel);
3e7ee490
HJ
388 return;
389 }
390
bd60c33e
GKH
391 /*
392 * Start the process of binding this offer to the driver
393 * We need to set the DeviceObject field before calling
646f1ea3 394 * vmbus_child_dev_add()
bd60c33e 395 */
89733aa9 396 newchannel->device_obj = vmbus_child_device_create(
767dff68
HZ
397 &newchannel->offermsg.offer.if_type,
398 &newchannel->offermsg.offer.if_instance,
188963ec 399 newchannel);
3e7ee490 400
454f18a9
BP
401 /*
402 * Add the new device to the bus. This will kick off device-driver
403 * binding which eventually invokes the device driver's AddDevice()
404 * method.
405 */
e13a0b5a 406 ret = vmbus_child_device_register(newchannel->device_obj);
bd60c33e 407 if (ret != 0) {
0a46618d 408 pr_err("unable to add child device object (relid %d)\n",
c50f7fb2 409 newchannel->offermsg.child_relid);
3e7ee490 410
15b2f647 411 spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
c50f7fb2 412 list_del(&newchannel->listentry);
15b2f647 413 spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
3e7ee490 414
e98cb276 415 free_channel(newchannel);
bd60c33e 416 } else {
454f18a9
BP
417 /*
418 * This state is used to indicate a successful open
419 * so that when we do close the channel normally, we
420 * can cleanup properly
421 */
c50f7fb2 422 newchannel->state = CHANNEL_OPEN_STATE;
c88c4e4c 423
8b5d6d3b
HZ
424 /* Open IC channels */
425 for (cnt = 0; cnt < MAX_MSG_TYPES; cnt++) {
767dff68 426 if (memcmp(&newchannel->offermsg.offer.if_type,
c88c4e4c 427 &hv_cb_utils[cnt].data,
8b5d6d3b 428 sizeof(struct hv_guid)) == 0 &&
188963ec 429 vmbus_open(newchannel, 2 * PAGE_SIZE,
8b5d6d3b
HZ
430 2 * PAGE_SIZE, NULL, 0,
431 hv_cb_utils[cnt].callback,
188963ec
HZ
432 newchannel) == 0) {
433 hv_cb_utils[cnt].channel = newchannel;
0a46618d
HJ
434
435 pr_info("%s\n", hv_cb_utils[cnt].log_msg);
436
8b5d6d3b 437 count_hv_channel();
c88c4e4c 438 }
c88c4e4c 439 }
3e7ee490 440 }
3e7ee490
HJ
441}
442
3e189519 443/*
e98cb276 444 * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
bd60c33e
GKH
445 *
446 * We ignore all offers except network and storage offers. For each network and
447 * storage offers, we create a channel object and queue a work item to the
448 * channel object to process the offer synchronously
449 */
e98cb276 450static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
3e7ee490 451{
bd60c33e 452 struct vmbus_channel_offer_channel *offer;
188963ec
HZ
453 struct vmbus_channel *newchannel;
454 struct hv_guid *guidtype;
455 struct hv_guid *guidinstance;
3e7ee490 456 int i;
188963ec 457 int fsupported = 0;
3e7ee490 458
bd60c33e
GKH
459 offer = (struct vmbus_channel_offer_channel *)hdr;
460 for (i = 0; i < MAX_NUM_DEVICE_CLASSES_SUPPORTED; i++) {
767dff68 461 if (memcmp(&offer->offer.if_type,
50b03266
S
462 &supported_device_classes[i],
463 sizeof(struct hv_guid)) == 0) {
188963ec 464 fsupported = 1;
3e7ee490
HJ
465 break;
466 }
467 }
468
98e08702 469 if (!fsupported)
3e7ee490 470 return;
3e7ee490 471
767dff68
HZ
472 guidtype = &offer->offer.if_type;
473 guidinstance = &offer->offer.if_instance;
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
c50f7fb2 482 memcpy(&newchannel->offermsg, offer,
bd60c33e 483 sizeof(struct vmbus_channel_offer_channel));
c50f7fb2
HZ
484 newchannel->monitor_grp = (u8)offer->monitorid / 32;
485 newchannel->monitor_bit = (u8)offer->monitorid % 32;
3e7ee490 486
454f18a9 487 /* TODO: Make sure the offer comes from our parent partition */
4b2f9abe
TT
488 INIT_WORK(&newchannel->work, vmbus_process_offer);
489 queue_work(newchannel->controlwq, &newchannel->work);
3e7ee490
HJ
490}
491
3e189519 492/*
e98cb276 493 * vmbus_onoffer_rescind - Rescind offer handler.
bd60c33e
GKH
494 *
495 * We queue a work item to process this offer synchronously
496 */
e98cb276 497static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
3e7ee490 498{
bd60c33e 499 struct vmbus_channel_rescind_offer *rescind;
aded7165 500 struct vmbus_channel *channel;
3e7ee490 501
bd60c33e 502 rescind = (struct vmbus_channel_rescind_offer *)hdr;
c6977677 503 channel = relid2channel(rescind->child_relid);
98e08702
HJ
504
505 if (channel == NULL)
506 /* Just return here, no channel found */
3e7ee490 507 return;
3e7ee490 508
4b2f9abe
TT
509 /* work is initialized for vmbus_process_rescind_offer() from
510 * vmbus_process_offer() where the channel got created */
511 queue_work(channel->controlwq, &channel->work);
3e7ee490
HJ
512}
513
3e189519 514/*
e98cb276
HZ
515 * vmbus_onoffers_delivered -
516 * This is invoked when all offers have been delivered.
bd60c33e
GKH
517 *
518 * Nothing to do here.
519 */
e98cb276 520static void vmbus_onoffers_delivered(
bd60c33e 521 struct vmbus_channel_message_header *hdr)
3e7ee490 522{
3e7ee490
HJ
523}
524
3e189519 525/*
e98cb276 526 * vmbus_onopen_result - Open result handler.
bd60c33e
GKH
527 *
528 * This is invoked when we received a response to our channel open request.
529 * Find the matching request, copy the response and signal the requesting
530 * thread.
531 */
e98cb276 532static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
3e7ee490 533{
bd60c33e 534 struct vmbus_channel_open_result *result;
188963ec
HZ
535 struct vmbus_channel_msginfo *msginfo;
536 struct vmbus_channel_message_header *requestheader;
537 struct vmbus_channel_open_channel *openmsg;
dd0813b6 538 unsigned long flags;
3e7ee490 539
bd60c33e 540 result = (struct vmbus_channel_open_result *)hdr;
3e7ee490 541
bd60c33e
GKH
542 /*
543 * Find the open msg, copy the result and signal/unblock the wait event
544 */
15b2f647 545 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
3e7ee490 546
ebb61e5f
HJ
547 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
548 msglistentry) {
188963ec 549 requestheader =
c50f7fb2 550 (struct vmbus_channel_message_header *)msginfo->msg;
188963ec 551
c50f7fb2 552 if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
188963ec 553 openmsg =
c50f7fb2
HZ
554 (struct vmbus_channel_open_channel *)msginfo->msg;
555 if (openmsg->child_relid == result->child_relid &&
556 openmsg->openid == result->openid) {
557 memcpy(&msginfo->response.open_result,
bd60c33e
GKH
558 result,
559 sizeof(struct vmbus_channel_open_result));
0c3b7b2f
S
560 msginfo->wait_condition = 1;
561 wake_up(&msginfo->waitevent);
3e7ee490
HJ
562 break;
563 }
564 }
565 }
15b2f647 566 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
3e7ee490
HJ
567}
568
3e189519 569/*
e98cb276 570 * vmbus_ongpadl_created - GPADL created handler.
bd60c33e
GKH
571 *
572 * This is invoked when we received a response to our gpadl create request.
573 * Find the matching request, copy the response and signal the requesting
574 * thread.
575 */
e98cb276 576static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
3e7ee490 577{
188963ec 578 struct vmbus_channel_gpadl_created *gpadlcreated;
188963ec
HZ
579 struct vmbus_channel_msginfo *msginfo;
580 struct vmbus_channel_message_header *requestheader;
581 struct vmbus_channel_gpadl_header *gpadlheader;
dd0813b6 582 unsigned long flags;
3e7ee490 583
188963ec 584 gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
3e7ee490 585
bd60c33e
GKH
586 /*
587 * Find the establish msg, copy the result and signal/unblock the wait
588 * event
589 */
15b2f647 590 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
3e7ee490 591
ebb61e5f
HJ
592 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
593 msglistentry) {
188963ec 594 requestheader =
c50f7fb2 595 (struct vmbus_channel_message_header *)msginfo->msg;
188963ec 596
c50f7fb2 597 if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
188963ec
HZ
598 gpadlheader =
599 (struct vmbus_channel_gpadl_header *)requestheader;
600
c50f7fb2
HZ
601 if ((gpadlcreated->child_relid ==
602 gpadlheader->child_relid) &&
603 (gpadlcreated->gpadl == gpadlheader->gpadl)) {
604 memcpy(&msginfo->response.gpadl_created,
188963ec 605 gpadlcreated,
bd60c33e 606 sizeof(struct vmbus_channel_gpadl_created));
0c3b7b2f
S
607 msginfo->wait_condition = 1;
608 wake_up(&msginfo->waitevent);
3e7ee490
HJ
609 break;
610 }
611 }
612 }
15b2f647 613 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
3e7ee490
HJ
614}
615
3e189519 616/*
e98cb276 617 * vmbus_ongpadl_torndown - GPADL torndown handler.
bd60c33e
GKH
618 *
619 * This is invoked when we received a response to our gpadl teardown request.
620 * Find the matching request, copy the response and signal the requesting
621 * thread.
622 */
e98cb276 623static void vmbus_ongpadl_torndown(
bd60c33e 624 struct vmbus_channel_message_header *hdr)
3e7ee490 625{
188963ec 626 struct vmbus_channel_gpadl_torndown *gpadl_torndown;
188963ec
HZ
627 struct vmbus_channel_msginfo *msginfo;
628 struct vmbus_channel_message_header *requestheader;
629 struct vmbus_channel_gpadl_teardown *gpadl_teardown;
dd0813b6 630 unsigned long flags;
3e7ee490 631
188963ec 632 gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
bd60c33e
GKH
633
634 /*
635 * Find the open msg, copy the result and signal/unblock the wait event
636 */
15b2f647 637 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
3e7ee490 638
ebb61e5f
HJ
639 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
640 msglistentry) {
188963ec 641 requestheader =
c50f7fb2 642 (struct vmbus_channel_message_header *)msginfo->msg;
3e7ee490 643
c50f7fb2 644 if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
188963ec
HZ
645 gpadl_teardown =
646 (struct vmbus_channel_gpadl_teardown *)requestheader;
3e7ee490 647
c50f7fb2
HZ
648 if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
649 memcpy(&msginfo->response.gpadl_torndown,
188963ec 650 gpadl_torndown,
bd60c33e 651 sizeof(struct vmbus_channel_gpadl_torndown));
0c3b7b2f
S
652 msginfo->wait_condition = 1;
653 wake_up(&msginfo->waitevent);
3e7ee490
HJ
654 break;
655 }
656 }
657 }
15b2f647 658 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
3e7ee490
HJ
659}
660
3e189519 661/*
e98cb276 662 * vmbus_onversion_response - Version response handler
bd60c33e
GKH
663 *
664 * This is invoked when we received a response to our initiate contact request.
665 * Find the matching request, copy the response and signal the requesting
666 * thread.
667 */
e98cb276 668static void vmbus_onversion_response(
bd60c33e 669 struct vmbus_channel_message_header *hdr)
3e7ee490 670{
188963ec
HZ
671 struct vmbus_channel_msginfo *msginfo;
672 struct vmbus_channel_message_header *requestheader;
82250213 673 struct vmbus_channel_initiate_contact *initiate;
188963ec 674 struct vmbus_channel_version_response *version_response;
dd0813b6 675 unsigned long flags;
3e7ee490 676
188963ec 677 version_response = (struct vmbus_channel_version_response *)hdr;
15b2f647 678 spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
3e7ee490 679
ebb61e5f
HJ
680 list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
681 msglistentry) {
188963ec 682 requestheader =
c50f7fb2 683 (struct vmbus_channel_message_header *)msginfo->msg;
3e7ee490 684
c50f7fb2
HZ
685 if (requestheader->msgtype ==
686 CHANNELMSG_INITIATE_CONTACT) {
188963ec
HZ
687 initiate =
688 (struct vmbus_channel_initiate_contact *)requestheader;
c50f7fb2 689 memcpy(&msginfo->response.version_response,
188963ec 690 version_response,
bd60c33e 691 sizeof(struct vmbus_channel_version_response));
0c3b7b2f
S
692 msginfo->wait_condition = 1;
693 wake_up(&msginfo->waitevent);
3e7ee490
HJ
694 }
695 }
15b2f647 696 spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
3e7ee490
HJ
697}
698
c8212f04
GKH
699/* Channel message dispatch table */
700static struct vmbus_channel_message_table_entry
b7c6b02f 701 channel_message_table[CHANNELMSG_COUNT] = {
c50f7fb2
HZ
702 {CHANNELMSG_INVALID, NULL},
703 {CHANNELMSG_OFFERCHANNEL, vmbus_onoffer},
704 {CHANNELMSG_RESCIND_CHANNELOFFER, vmbus_onoffer_rescind},
705 {CHANNELMSG_REQUESTOFFERS, NULL},
706 {CHANNELMSG_ALLOFFERS_DELIVERED, vmbus_onoffers_delivered},
707 {CHANNELMSG_OPENCHANNEL, NULL},
708 {CHANNELMSG_OPENCHANNEL_RESULT, vmbus_onopen_result},
709 {CHANNELMSG_CLOSECHANNEL, NULL},
710 {CHANNELMSG_GPADL_HEADER, NULL},
711 {CHANNELMSG_GPADL_BODY, NULL},
712 {CHANNELMSG_GPADL_CREATED, vmbus_ongpadl_created},
713 {CHANNELMSG_GPADL_TEARDOWN, NULL},
714 {CHANNELMSG_GPADL_TORNDOWN, vmbus_ongpadl_torndown},
715 {CHANNELMSG_RELID_RELEASED, NULL},
716 {CHANNELMSG_INITIATE_CONTACT, NULL},
717 {CHANNELMSG_VERSION_RESPONSE, vmbus_onversion_response},
718 {CHANNELMSG_UNLOAD, NULL},
c8212f04
GKH
719};
720
3e189519 721/*
e98cb276 722 * vmbus_onmessage - Handler for channel protocol messages.
bd60c33e
GKH
723 *
724 * This is invoked in the vmbus worker thread context.
725 */
e98cb276 726void vmbus_onmessage(void *context)
3e7ee490 727{
188963ec 728 struct hv_message *msg = context;
82250213 729 struct vmbus_channel_message_header *hdr;
3e7ee490
HJ
730 int size;
731
f6feebe0
HZ
732 hdr = (struct vmbus_channel_message_header *)msg->u.payload;
733 size = msg->header.payload_size;
3e7ee490 734
c50f7fb2 735 if (hdr->msgtype >= CHANNELMSG_COUNT) {
0a46618d 736 pr_err("Received invalid channel message type %d size %d\n",
c50f7fb2 737 hdr->msgtype, size);
04f50c4d 738 print_hex_dump_bytes("", DUMP_PREFIX_NONE,
f6feebe0 739 (unsigned char *)msg->u.payload, size);
3e7ee490
HJ
740 return;
741 }
742
b7c6b02f
S
743 if (channel_message_table[hdr->msgtype].message_handler)
744 channel_message_table[hdr->msgtype].message_handler(hdr);
3e7ee490 745 else
0a46618d 746 pr_err("Unhandled channel message type %d\n", hdr->msgtype);
3e7ee490
HJ
747}
748
3e189519 749/*
e98cb276 750 * vmbus_request_offers - Send a request to get all our pending offers.
bd60c33e 751 */
e98cb276 752int vmbus_request_offers(void)
3e7ee490 753{
82250213 754 struct vmbus_channel_message_header *msg;
188963ec 755 struct vmbus_channel_msginfo *msginfo;
bd60c33e 756 int ret;
3e7ee490 757
188963ec 758 msginfo = kmalloc(sizeof(*msginfo) +
bd60c33e
GKH
759 sizeof(struct vmbus_channel_message_header),
760 GFP_KERNEL);
188963ec 761 if (!msginfo)
75910f23 762 return -ENOMEM;
3e7ee490 763
0c3b7b2f 764 init_waitqueue_head(&msginfo->waitevent);
75910f23 765
c50f7fb2 766 msg = (struct vmbus_channel_message_header *)msginfo->msg;
3e7ee490 767
c50f7fb2 768 msg->msgtype = CHANNELMSG_REQUESTOFFERS;
3e7ee490 769
3e7ee490 770
c6977677 771 ret = vmbus_post_msg(msg,
bd60c33e
GKH
772 sizeof(struct vmbus_channel_message_header));
773 if (ret != 0) {
0a46618d 774 pr_err("Unable to request offers - %d\n", ret);
3e7ee490 775
0c3b7b2f
S
776 goto cleanup;
777 }
3e7ee490 778
0c3b7b2f
S
779 msginfo->wait_condition = 0;
780 wait_event_timeout(msginfo->waitevent, msginfo->wait_condition,
781 msecs_to_jiffies(1000));
782 if (msginfo->wait_condition == 0) {
783 ret = -ETIMEDOUT;
784 goto cleanup;
3e7ee490 785 }
3e7ee490 786
3e7ee490
HJ
787
788
0c3b7b2f 789cleanup:
dd9b15dc 790 kfree(msginfo);
3e7ee490 791
3e7ee490
HJ
792 return ret;
793}
794
454f18a9 795/* eof */