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