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