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