Commit | Line | Data |
---|---|---|
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 | 35 | struct 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 | |
245ba56a KS |
40 | #define MAX_MSG_TYPES 4 |
41 | #define MAX_NUM_DEVICE_CLASSES_SUPPORTED 8 | |
c88c4e4c | 42 | |
358d2ee2 | 43 | static const uuid_le |
50b03266 | 44 | supported_device_classes[MAX_NUM_DEVICE_CLASSES_SUPPORTED] = { |
454f18a9 | 45 | /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */ |
caf26a31 GKH |
46 | /* Storage - SCSI */ |
47 | { | |
358d2ee2 | 48 | .b = { |
caf26a31 GKH |
49 | 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d, |
50 | 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f | |
51 | } | |
52 | }, | |
53 | ||
454f18a9 | 54 | /* {F8615163-DF3E-46c5-913F-F2D2F965ED0E} */ |
caf26a31 GKH |
55 | /* Network */ |
56 | { | |
358d2ee2 | 57 | .b = { |
caf26a31 GKH |
58 | 0x63, 0x51, 0x61, 0xF8, 0x3E, 0xDF, 0xc5, 0x46, |
59 | 0x91, 0x3F, 0xF2, 0xD2, 0xF9, 0x65, 0xED, 0x0E | |
60 | } | |
61 | }, | |
62 | ||
454f18a9 | 63 | /* {CFA8B69E-5B4A-4cc0-B98B-8BA1A1F3F95A} */ |
caf26a31 GKH |
64 | /* Input */ |
65 | { | |
358d2ee2 | 66 | .b = { |
caf26a31 GKH |
67 | 0x9E, 0xB6, 0xA8, 0xCF, 0x4A, 0x5B, 0xc0, 0x4c, |
68 | 0xB9, 0x8B, 0x8B, 0xA1, 0xA1, 0xF3, 0xF9, 0x5A | |
69 | } | |
70 | }, | |
3e7ee490 | 71 | |
caf26a31 GKH |
72 | /* {32412632-86cb-44a2-9b5c-50d1417354f5} */ |
73 | /* IDE */ | |
74 | { | |
358d2ee2 | 75 | .b = { |
caf26a31 GKH |
76 | 0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44, |
77 | 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5 | |
78 | } | |
79 | }, | |
c88c4e4c HJ |
80 | /* 0E0B6031-5213-4934-818B-38D90CED39DB */ |
81 | /* Shutdown */ | |
82 | { | |
358d2ee2 | 83 | .b = { |
c88c4e4c HJ |
84 | 0x31, 0x60, 0x0B, 0X0E, 0x13, 0x52, 0x34, 0x49, |
85 | 0x81, 0x8B, 0x38, 0XD9, 0x0C, 0xED, 0x39, 0xDB | |
86 | } | |
87 | }, | |
39c4e9c3 HZ |
88 | /* {9527E630-D0AE-497b-ADCE-E80AB0175CAF} */ |
89 | /* TimeSync */ | |
90 | { | |
358d2ee2 | 91 | .b = { |
39c4e9c3 HZ |
92 | 0x30, 0xe6, 0x27, 0x95, 0xae, 0xd0, 0x7b, 0x49, |
93 | 0xad, 0xce, 0xe8, 0x0a, 0xb0, 0x17, 0x5c, 0xaf | |
94 | } | |
95 | }, | |
9153f7b9 HJ |
96 | /* {57164f39-9115-4e78-ab55-382f3bd5422d} */ |
97 | /* Heartbeat */ | |
98 | { | |
358d2ee2 | 99 | .b = { |
9153f7b9 HJ |
100 | 0x39, 0x4f, 0x16, 0x57, 0x15, 0x91, 0x78, 0x4e, |
101 | 0xab, 0x55, 0x38, 0x2f, 0x3b, 0xd5, 0x42, 0x2d | |
102 | } | |
103 | }, | |
245ba56a KS |
104 | /* {A9A0F4E7-5A45-4d96-B827-8A841E8C03E6} */ |
105 | /* KVP */ | |
106 | { | |
358d2ee2 | 107 | .b = { |
245ba56a KS |
108 | 0xe7, 0xf4, 0xa0, 0xa9, 0x45, 0x5a, 0x96, 0x4d, |
109 | 0xb8, 0x27, 0x8a, 0x84, 0x1e, 0x8c, 0x3, 0xe6 | |
110 | } | |
111 | }, | |
112 | ||
3e7ee490 HJ |
113 | }; |
114 | ||
c88c4e4c HJ |
115 | |
116 | /** | |
da0e9631 | 117 | * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message |
c88c4e4c HJ |
118 | * @icmsghdrp: Pointer to msg header structure |
119 | * @icmsg_negotiate: Pointer to negotiate message structure | |
120 | * @buf: Raw buffer channel data | |
121 | * | |
122 | * @icmsghdrp is of type &struct icmsg_hdr. | |
123 | * @negop is of type &struct icmsg_negotiate. | |
124 | * Set up and fill in default negotiate response message. This response can | |
125 | * come from both the vmbus driver and the hv_utils driver. The current api | |
126 | * will respond properly to both Windows 2008 and Windows 2008-R2 operating | |
127 | * systems. | |
128 | * | |
129 | * Mainly used by Hyper-V drivers. | |
130 | */ | |
da0e9631 GKH |
131 | void vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp, |
132 | struct icmsg_negotiate *negop, u8 *buf) | |
c88c4e4c HJ |
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 | } | |
da0e9631 | 158 | EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp); |
c88c4e4c | 159 | |
3e189519 | 160 | /* |
e98cb276 | 161 | * alloc_channel - Allocate and initialize a vmbus channel object |
bd60c33e | 162 | */ |
50fe56d2 | 163 | static struct vmbus_channel *alloc_channel(void) |
3e7ee490 | 164 | { |
aded7165 | 165 | struct vmbus_channel *channel; |
3e7ee490 | 166 | |
aded7165 | 167 | channel = kzalloc(sizeof(*channel), GFP_ATOMIC); |
3e7ee490 | 168 | if (!channel) |
3e7ee490 | 169 | return NULL; |
3e7ee490 | 170 | |
54411c42 | 171 | spin_lock_init(&channel->inbound_lock); |
3e7ee490 | 172 | |
c50f7fb2 HZ |
173 | channel->controlwq = create_workqueue("hv_vmbus_ctl"); |
174 | if (!channel->controlwq) { | |
8c69f52a | 175 | kfree(channel); |
3e7ee490 HJ |
176 | return NULL; |
177 | } | |
178 | ||
179 | return channel; | |
180 | } | |
181 | ||
3e189519 | 182 | /* |
e98cb276 | 183 | * release_hannel - Release the vmbus channel object itself |
bd60c33e | 184 | */ |
4b2f9abe | 185 | static void release_channel(struct work_struct *work) |
3e7ee490 | 186 | { |
4b2f9abe TT |
187 | struct vmbus_channel *channel = container_of(work, |
188 | struct vmbus_channel, | |
189 | work); | |
3e7ee490 | 190 | |
c50f7fb2 | 191 | destroy_workqueue(channel->controlwq); |
3e7ee490 | 192 | |
8c69f52a | 193 | kfree(channel); |
3e7ee490 HJ |
194 | } |
195 | ||
3e189519 | 196 | /* |
e98cb276 | 197 | * free_channel - Release the resources used by the vmbus channel object |
bd60c33e | 198 | */ |
9f3e28e3 | 199 | static void free_channel(struct vmbus_channel *channel) |
3e7ee490 | 200 | { |
3e7ee490 | 201 | |
bd60c33e GKH |
202 | /* |
203 | * We have to release the channel's workqueue/thread in the vmbus's | |
204 | * workqueue/thread context | |
205 | * ie we can't destroy ourselves. | |
206 | */ | |
4b2f9abe | 207 | INIT_WORK(&channel->work, release_channel); |
da9fcb72 | 208 | queue_work(vmbus_connection.work_queue, &channel->work); |
3e7ee490 HJ |
209 | } |
210 | ||
8b5d6d3b | 211 | |
8b5d6d3b | 212 | |
4b2f9abe TT |
213 | /* |
214 | * vmbus_process_rescind_offer - | |
215 | * Rescind the offer by initiating a device removal | |
216 | */ | |
217 | static void vmbus_process_rescind_offer(struct work_struct *work) | |
218 | { | |
219 | struct vmbus_channel *channel = container_of(work, | |
220 | struct vmbus_channel, | |
221 | work); | |
222 | ||
696453ba | 223 | vmbus_device_unregister(channel->device_obj); |
4b2f9abe | 224 | } |
8b5d6d3b | 225 | |
3e189519 | 226 | /* |
e98cb276 | 227 | * vmbus_process_offer - Process the offer by creating a channel/device |
c88c4e4c | 228 | * associated with this offer |
bd60c33e | 229 | */ |
4b2f9abe | 230 | static void vmbus_process_offer(struct work_struct *work) |
3e7ee490 | 231 | { |
4b2f9abe TT |
232 | struct vmbus_channel *newchannel = container_of(work, |
233 | struct vmbus_channel, | |
234 | work); | |
aded7165 | 235 | struct vmbus_channel *channel; |
188963ec | 236 | bool fnew = true; |
bd60c33e | 237 | int ret; |
0f5e44ca | 238 | unsigned long flags; |
3e7ee490 | 239 | |
4b2f9abe TT |
240 | /* The next possible work is rescind handling */ |
241 | INIT_WORK(&newchannel->work, vmbus_process_rescind_offer); | |
242 | ||
454f18a9 | 243 | /* Make sure this is a new offer */ |
15b2f647 | 244 | spin_lock_irqsave(&vmbus_connection.channel_lock, flags); |
3e7ee490 | 245 | |
da9fcb72 | 246 | list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) { |
358d2ee2 S |
247 | if (!uuid_le_cmp(channel->offermsg.offer.if_type, |
248 | newchannel->offermsg.offer.if_type) && | |
249 | !uuid_le_cmp(channel->offermsg.offer.if_instance, | |
250 | newchannel->offermsg.offer.if_instance)) { | |
188963ec | 251 | fnew = false; |
3e7ee490 HJ |
252 | break; |
253 | } | |
254 | } | |
255 | ||
188963ec | 256 | if (fnew) |
c50f7fb2 | 257 | list_add_tail(&newchannel->listentry, |
da9fcb72 | 258 | &vmbus_connection.chn_list); |
bd60c33e | 259 | |
15b2f647 | 260 | spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags); |
3e7ee490 | 261 | |
188963ec | 262 | if (!fnew) { |
e98cb276 | 263 | free_channel(newchannel); |
3e7ee490 HJ |
264 | return; |
265 | } | |
266 | ||
bd60c33e GKH |
267 | /* |
268 | * Start the process of binding this offer to the driver | |
269 | * We need to set the DeviceObject field before calling | |
646f1ea3 | 270 | * vmbus_child_dev_add() |
bd60c33e | 271 | */ |
f2c73011 | 272 | newchannel->device_obj = vmbus_device_create( |
767dff68 HZ |
273 | &newchannel->offermsg.offer.if_type, |
274 | &newchannel->offermsg.offer.if_instance, | |
188963ec | 275 | newchannel); |
3e7ee490 | 276 | |
454f18a9 BP |
277 | /* |
278 | * Add the new device to the bus. This will kick off device-driver | |
279 | * binding which eventually invokes the device driver's AddDevice() | |
280 | * method. | |
281 | */ | |
22794281 | 282 | ret = vmbus_device_register(newchannel->device_obj); |
bd60c33e | 283 | if (ret != 0) { |
0a46618d | 284 | pr_err("unable to add child device object (relid %d)\n", |
c50f7fb2 | 285 | newchannel->offermsg.child_relid); |
3e7ee490 | 286 | |
15b2f647 | 287 | spin_lock_irqsave(&vmbus_connection.channel_lock, flags); |
c50f7fb2 | 288 | list_del(&newchannel->listentry); |
15b2f647 | 289 | spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags); |
3e7ee490 | 290 | |
e98cb276 | 291 | free_channel(newchannel); |
bd60c33e | 292 | } else { |
454f18a9 BP |
293 | /* |
294 | * This state is used to indicate a successful open | |
295 | * so that when we do close the channel normally, we | |
296 | * can cleanup properly | |
297 | */ | |
c50f7fb2 | 298 | newchannel->state = CHANNEL_OPEN_STATE; |
3e7ee490 | 299 | } |
3e7ee490 HJ |
300 | } |
301 | ||
3e189519 | 302 | /* |
e98cb276 | 303 | * vmbus_onoffer - Handler for channel offers from vmbus in parent partition. |
bd60c33e | 304 | * |
bd60c33e | 305 | */ |
e98cb276 | 306 | static void vmbus_onoffer(struct vmbus_channel_message_header *hdr) |
3e7ee490 | 307 | { |
bd60c33e | 308 | struct vmbus_channel_offer_channel *offer; |
188963ec | 309 | struct vmbus_channel *newchannel; |
358d2ee2 S |
310 | uuid_le *guidtype; |
311 | uuid_le *guidinstance; | |
3e7ee490 | 312 | int i; |
188963ec | 313 | int fsupported = 0; |
3e7ee490 | 314 | |
bd60c33e GKH |
315 | offer = (struct vmbus_channel_offer_channel *)hdr; |
316 | for (i = 0; i < MAX_NUM_DEVICE_CLASSES_SUPPORTED; i++) { | |
358d2ee2 S |
317 | if (!uuid_le_cmp(offer->offer.if_type, |
318 | supported_device_classes[i])) { | |
188963ec | 319 | fsupported = 1; |
3e7ee490 HJ |
320 | break; |
321 | } | |
322 | } | |
323 | ||
98e08702 | 324 | if (!fsupported) |
3e7ee490 | 325 | return; |
3e7ee490 | 326 | |
767dff68 HZ |
327 | guidtype = &offer->offer.if_type; |
328 | guidinstance = &offer->offer.if_instance; | |
3e7ee490 | 329 | |
454f18a9 | 330 | /* Allocate the channel object and save this offer. */ |
e98cb276 | 331 | newchannel = alloc_channel(); |
188963ec | 332 | if (!newchannel) { |
0a46618d | 333 | pr_err("Unable to allocate channel object\n"); |
3e7ee490 HJ |
334 | return; |
335 | } | |
336 | ||
c50f7fb2 | 337 | memcpy(&newchannel->offermsg, offer, |
bd60c33e | 338 | sizeof(struct vmbus_channel_offer_channel)); |
c50f7fb2 HZ |
339 | newchannel->monitor_grp = (u8)offer->monitorid / 32; |
340 | newchannel->monitor_bit = (u8)offer->monitorid % 32; | |
3e7ee490 | 341 | |
4b2f9abe TT |
342 | INIT_WORK(&newchannel->work, vmbus_process_offer); |
343 | queue_work(newchannel->controlwq, &newchannel->work); | |
3e7ee490 HJ |
344 | } |
345 | ||
3e189519 | 346 | /* |
e98cb276 | 347 | * vmbus_onoffer_rescind - Rescind offer handler. |
bd60c33e GKH |
348 | * |
349 | * We queue a work item to process this offer synchronously | |
350 | */ | |
e98cb276 | 351 | static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr) |
3e7ee490 | 352 | { |
bd60c33e | 353 | struct vmbus_channel_rescind_offer *rescind; |
aded7165 | 354 | struct vmbus_channel *channel; |
3e7ee490 | 355 | |
bd60c33e | 356 | rescind = (struct vmbus_channel_rescind_offer *)hdr; |
c6977677 | 357 | channel = relid2channel(rescind->child_relid); |
98e08702 HJ |
358 | |
359 | if (channel == NULL) | |
360 | /* Just return here, no channel found */ | |
3e7ee490 | 361 | return; |
3e7ee490 | 362 | |
4b2f9abe TT |
363 | /* work is initialized for vmbus_process_rescind_offer() from |
364 | * vmbus_process_offer() where the channel got created */ | |
365 | queue_work(channel->controlwq, &channel->work); | |
3e7ee490 HJ |
366 | } |
367 | ||
3e189519 | 368 | /* |
e98cb276 HZ |
369 | * vmbus_onoffers_delivered - |
370 | * This is invoked when all offers have been delivered. | |
bd60c33e GKH |
371 | * |
372 | * Nothing to do here. | |
373 | */ | |
e98cb276 | 374 | static void vmbus_onoffers_delivered( |
bd60c33e | 375 | struct vmbus_channel_message_header *hdr) |
3e7ee490 | 376 | { |
3e7ee490 HJ |
377 | } |
378 | ||
3e189519 | 379 | /* |
e98cb276 | 380 | * vmbus_onopen_result - Open result handler. |
bd60c33e GKH |
381 | * |
382 | * This is invoked when we received a response to our channel open request. | |
383 | * Find the matching request, copy the response and signal the requesting | |
384 | * thread. | |
385 | */ | |
e98cb276 | 386 | static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr) |
3e7ee490 | 387 | { |
bd60c33e | 388 | struct vmbus_channel_open_result *result; |
188963ec HZ |
389 | struct vmbus_channel_msginfo *msginfo; |
390 | struct vmbus_channel_message_header *requestheader; | |
391 | struct vmbus_channel_open_channel *openmsg; | |
dd0813b6 | 392 | unsigned long flags; |
3e7ee490 | 393 | |
bd60c33e | 394 | result = (struct vmbus_channel_open_result *)hdr; |
3e7ee490 | 395 | |
bd60c33e GKH |
396 | /* |
397 | * Find the open msg, copy the result and signal/unblock the wait event | |
398 | */ | |
15b2f647 | 399 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 | 400 | |
ebb61e5f HJ |
401 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
402 | msglistentry) { | |
188963ec | 403 | requestheader = |
c50f7fb2 | 404 | (struct vmbus_channel_message_header *)msginfo->msg; |
188963ec | 405 | |
c50f7fb2 | 406 | if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) { |
188963ec | 407 | openmsg = |
c50f7fb2 HZ |
408 | (struct vmbus_channel_open_channel *)msginfo->msg; |
409 | if (openmsg->child_relid == result->child_relid && | |
410 | openmsg->openid == result->openid) { | |
411 | memcpy(&msginfo->response.open_result, | |
bd60c33e | 412 | result, |
9568a193 S |
413 | sizeof( |
414 | struct vmbus_channel_open_result)); | |
415 | complete(&msginfo->waitevent); | |
3e7ee490 HJ |
416 | break; |
417 | } | |
418 | } | |
419 | } | |
15b2f647 | 420 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 HJ |
421 | } |
422 | ||
3e189519 | 423 | /* |
e98cb276 | 424 | * vmbus_ongpadl_created - GPADL created handler. |
bd60c33e GKH |
425 | * |
426 | * This is invoked when we received a response to our gpadl create request. | |
427 | * Find the matching request, copy the response and signal the requesting | |
428 | * thread. | |
429 | */ | |
e98cb276 | 430 | static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr) |
3e7ee490 | 431 | { |
188963ec | 432 | struct vmbus_channel_gpadl_created *gpadlcreated; |
188963ec HZ |
433 | struct vmbus_channel_msginfo *msginfo; |
434 | struct vmbus_channel_message_header *requestheader; | |
435 | struct vmbus_channel_gpadl_header *gpadlheader; | |
dd0813b6 | 436 | unsigned long flags; |
3e7ee490 | 437 | |
188963ec | 438 | gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr; |
3e7ee490 | 439 | |
bd60c33e GKH |
440 | /* |
441 | * Find the establish msg, copy the result and signal/unblock the wait | |
442 | * event | |
443 | */ | |
15b2f647 | 444 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 | 445 | |
ebb61e5f HJ |
446 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
447 | msglistentry) { | |
188963ec | 448 | requestheader = |
c50f7fb2 | 449 | (struct vmbus_channel_message_header *)msginfo->msg; |
188963ec | 450 | |
c50f7fb2 | 451 | if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) { |
188963ec HZ |
452 | gpadlheader = |
453 | (struct vmbus_channel_gpadl_header *)requestheader; | |
454 | ||
c50f7fb2 HZ |
455 | if ((gpadlcreated->child_relid == |
456 | gpadlheader->child_relid) && | |
457 | (gpadlcreated->gpadl == gpadlheader->gpadl)) { | |
458 | memcpy(&msginfo->response.gpadl_created, | |
188963ec | 459 | gpadlcreated, |
9568a193 S |
460 | sizeof( |
461 | struct vmbus_channel_gpadl_created)); | |
462 | complete(&msginfo->waitevent); | |
3e7ee490 HJ |
463 | break; |
464 | } | |
465 | } | |
466 | } | |
15b2f647 | 467 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 HJ |
468 | } |
469 | ||
3e189519 | 470 | /* |
e98cb276 | 471 | * vmbus_ongpadl_torndown - GPADL torndown handler. |
bd60c33e GKH |
472 | * |
473 | * This is invoked when we received a response to our gpadl teardown request. | |
474 | * Find the matching request, copy the response and signal the requesting | |
475 | * thread. | |
476 | */ | |
e98cb276 | 477 | static void vmbus_ongpadl_torndown( |
bd60c33e | 478 | struct vmbus_channel_message_header *hdr) |
3e7ee490 | 479 | { |
188963ec | 480 | struct vmbus_channel_gpadl_torndown *gpadl_torndown; |
188963ec HZ |
481 | struct vmbus_channel_msginfo *msginfo; |
482 | struct vmbus_channel_message_header *requestheader; | |
483 | struct vmbus_channel_gpadl_teardown *gpadl_teardown; | |
dd0813b6 | 484 | unsigned long flags; |
3e7ee490 | 485 | |
188963ec | 486 | gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr; |
bd60c33e GKH |
487 | |
488 | /* | |
489 | * Find the open msg, copy the result and signal/unblock the wait event | |
490 | */ | |
15b2f647 | 491 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 | 492 | |
ebb61e5f HJ |
493 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
494 | msglistentry) { | |
188963ec | 495 | requestheader = |
c50f7fb2 | 496 | (struct vmbus_channel_message_header *)msginfo->msg; |
3e7ee490 | 497 | |
c50f7fb2 | 498 | if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) { |
188963ec HZ |
499 | gpadl_teardown = |
500 | (struct vmbus_channel_gpadl_teardown *)requestheader; | |
3e7ee490 | 501 | |
c50f7fb2 HZ |
502 | if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) { |
503 | memcpy(&msginfo->response.gpadl_torndown, | |
188963ec | 504 | gpadl_torndown, |
9568a193 S |
505 | sizeof( |
506 | struct vmbus_channel_gpadl_torndown)); | |
507 | complete(&msginfo->waitevent); | |
3e7ee490 HJ |
508 | break; |
509 | } | |
510 | } | |
511 | } | |
15b2f647 | 512 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 HJ |
513 | } |
514 | ||
3e189519 | 515 | /* |
e98cb276 | 516 | * vmbus_onversion_response - Version response handler |
bd60c33e GKH |
517 | * |
518 | * This is invoked when we received a response to our initiate contact request. | |
519 | * Find the matching request, copy the response and signal the requesting | |
520 | * thread. | |
521 | */ | |
e98cb276 | 522 | static void vmbus_onversion_response( |
bd60c33e | 523 | struct vmbus_channel_message_header *hdr) |
3e7ee490 | 524 | { |
188963ec HZ |
525 | struct vmbus_channel_msginfo *msginfo; |
526 | struct vmbus_channel_message_header *requestheader; | |
82250213 | 527 | struct vmbus_channel_initiate_contact *initiate; |
188963ec | 528 | struct vmbus_channel_version_response *version_response; |
dd0813b6 | 529 | unsigned long flags; |
3e7ee490 | 530 | |
188963ec | 531 | version_response = (struct vmbus_channel_version_response *)hdr; |
15b2f647 | 532 | spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 | 533 | |
ebb61e5f HJ |
534 | list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list, |
535 | msglistentry) { | |
188963ec | 536 | requestheader = |
c50f7fb2 | 537 | (struct vmbus_channel_message_header *)msginfo->msg; |
3e7ee490 | 538 | |
c50f7fb2 HZ |
539 | if (requestheader->msgtype == |
540 | CHANNELMSG_INITIATE_CONTACT) { | |
188963ec HZ |
541 | initiate = |
542 | (struct vmbus_channel_initiate_contact *)requestheader; | |
c50f7fb2 | 543 | memcpy(&msginfo->response.version_response, |
188963ec | 544 | version_response, |
bd60c33e | 545 | sizeof(struct vmbus_channel_version_response)); |
9568a193 | 546 | complete(&msginfo->waitevent); |
3e7ee490 HJ |
547 | } |
548 | } | |
15b2f647 | 549 | spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags); |
3e7ee490 HJ |
550 | } |
551 | ||
c8212f04 GKH |
552 | /* Channel message dispatch table */ |
553 | static struct vmbus_channel_message_table_entry | |
b7c6b02f | 554 | channel_message_table[CHANNELMSG_COUNT] = { |
c50f7fb2 HZ |
555 | {CHANNELMSG_INVALID, NULL}, |
556 | {CHANNELMSG_OFFERCHANNEL, vmbus_onoffer}, | |
557 | {CHANNELMSG_RESCIND_CHANNELOFFER, vmbus_onoffer_rescind}, | |
558 | {CHANNELMSG_REQUESTOFFERS, NULL}, | |
559 | {CHANNELMSG_ALLOFFERS_DELIVERED, vmbus_onoffers_delivered}, | |
560 | {CHANNELMSG_OPENCHANNEL, NULL}, | |
561 | {CHANNELMSG_OPENCHANNEL_RESULT, vmbus_onopen_result}, | |
562 | {CHANNELMSG_CLOSECHANNEL, NULL}, | |
563 | {CHANNELMSG_GPADL_HEADER, NULL}, | |
564 | {CHANNELMSG_GPADL_BODY, NULL}, | |
565 | {CHANNELMSG_GPADL_CREATED, vmbus_ongpadl_created}, | |
566 | {CHANNELMSG_GPADL_TEARDOWN, NULL}, | |
567 | {CHANNELMSG_GPADL_TORNDOWN, vmbus_ongpadl_torndown}, | |
568 | {CHANNELMSG_RELID_RELEASED, NULL}, | |
569 | {CHANNELMSG_INITIATE_CONTACT, NULL}, | |
570 | {CHANNELMSG_VERSION_RESPONSE, vmbus_onversion_response}, | |
571 | {CHANNELMSG_UNLOAD, NULL}, | |
c8212f04 GKH |
572 | }; |
573 | ||
3e189519 | 574 | /* |
e98cb276 | 575 | * vmbus_onmessage - Handler for channel protocol messages. |
bd60c33e GKH |
576 | * |
577 | * This is invoked in the vmbus worker thread context. | |
578 | */ | |
e98cb276 | 579 | void vmbus_onmessage(void *context) |
3e7ee490 | 580 | { |
188963ec | 581 | struct hv_message *msg = context; |
82250213 | 582 | struct vmbus_channel_message_header *hdr; |
3e7ee490 HJ |
583 | int size; |
584 | ||
f6feebe0 HZ |
585 | hdr = (struct vmbus_channel_message_header *)msg->u.payload; |
586 | size = msg->header.payload_size; | |
3e7ee490 | 587 | |
c50f7fb2 | 588 | if (hdr->msgtype >= CHANNELMSG_COUNT) { |
0a46618d | 589 | pr_err("Received invalid channel message type %d size %d\n", |
c50f7fb2 | 590 | hdr->msgtype, size); |
04f50c4d | 591 | print_hex_dump_bytes("", DUMP_PREFIX_NONE, |
f6feebe0 | 592 | (unsigned char *)msg->u.payload, size); |
3e7ee490 HJ |
593 | return; |
594 | } | |
595 | ||
b7c6b02f S |
596 | if (channel_message_table[hdr->msgtype].message_handler) |
597 | channel_message_table[hdr->msgtype].message_handler(hdr); | |
3e7ee490 | 598 | else |
0a46618d | 599 | pr_err("Unhandled channel message type %d\n", hdr->msgtype); |
3e7ee490 HJ |
600 | } |
601 | ||
3e189519 | 602 | /* |
e98cb276 | 603 | * vmbus_request_offers - Send a request to get all our pending offers. |
bd60c33e | 604 | */ |
e98cb276 | 605 | int vmbus_request_offers(void) |
3e7ee490 | 606 | { |
82250213 | 607 | struct vmbus_channel_message_header *msg; |
188963ec | 608 | struct vmbus_channel_msginfo *msginfo; |
9568a193 | 609 | int ret, t; |
3e7ee490 | 610 | |
188963ec | 611 | msginfo = kmalloc(sizeof(*msginfo) + |
bd60c33e GKH |
612 | sizeof(struct vmbus_channel_message_header), |
613 | GFP_KERNEL); | |
188963ec | 614 | if (!msginfo) |
75910f23 | 615 | return -ENOMEM; |
3e7ee490 | 616 | |
9568a193 | 617 | init_completion(&msginfo->waitevent); |
75910f23 | 618 | |
c50f7fb2 | 619 | msg = (struct vmbus_channel_message_header *)msginfo->msg; |
3e7ee490 | 620 | |
c50f7fb2 | 621 | msg->msgtype = CHANNELMSG_REQUESTOFFERS; |
3e7ee490 | 622 | |
3e7ee490 | 623 | |
c6977677 | 624 | ret = vmbus_post_msg(msg, |
bd60c33e GKH |
625 | sizeof(struct vmbus_channel_message_header)); |
626 | if (ret != 0) { | |
0a46618d | 627 | pr_err("Unable to request offers - %d\n", ret); |
3e7ee490 | 628 | |
0c3b7b2f S |
629 | goto cleanup; |
630 | } | |
3e7ee490 | 631 | |
2dfde964 | 632 | t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ); |
9568a193 | 633 | if (t == 0) { |
0c3b7b2f S |
634 | ret = -ETIMEDOUT; |
635 | goto cleanup; | |
3e7ee490 | 636 | } |
3e7ee490 | 637 | |
3e7ee490 HJ |
638 | |
639 | ||
0c3b7b2f | 640 | cleanup: |
dd9b15dc | 641 | kfree(msginfo); |
3e7ee490 | 642 | |
3e7ee490 HJ |
643 | return ret; |
644 | } | |
645 | ||
454f18a9 | 646 | /* eof */ |