21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
25 #include <linux/wait.h>
27 #include <linux/slab.h>
28 #include <linux/list.h>
29 #include <linux/module.h>
60 struct icmsg_negotiate *negop,
u8 *
buf,
61 int max_fw_version,
int max_srv_version)
67 icmsghdrp->icmsgsize = 0x10;
69 negop = (
struct icmsg_negotiate *)&buf[
70 sizeof(
struct vmbuspipe_hdr) +
71 sizeof(
struct icmsg_hdr)];
73 icframe_vercnt = negop->icframe_vercnt;
74 icmsg_vercnt = negop->icmsg_vercnt;
81 for (i = 0; i < negop->icframe_vercnt; i++) {
82 if (negop->icversion_data[i].major <= max_fw_version)
83 icframe_vercnt = negop->icversion_data[
i].major;
86 for (i = negop->icframe_vercnt;
87 (i < negop->icframe_vercnt + negop->icmsg_vercnt); i++) {
88 if (negop->icversion_data[i].major <= max_srv_version)
89 icmsg_vercnt = negop->icversion_data[
i].major;
96 negop->icframe_vercnt = 1;
97 negop->icmsg_vercnt = 1;
98 negop->icversion_data[0].major = icframe_vercnt;
99 negop->icversion_data[0].minor = 0;
100 negop->icversion_data[1].major = icmsg_vercnt;
101 negop->icversion_data[1].minor = 0;
109 static struct vmbus_channel *alloc_channel(
void)
113 channel = kzalloc(
sizeof(*channel),
GFP_ATOMIC);
120 if (!channel->controlwq) {
134 struct vmbus_channel,
145 static void free_channel(
struct vmbus_channel *channel)
153 INIT_WORK(&channel->work, release_channel);
166 struct vmbus_channel,
178 kfree(channel->device_obj);
179 free_channel(channel);
190 struct vmbus_channel,
198 INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
204 if (!uuid_le_cmp(channel->offermsg.offer.if_type,
205 newchannel->offermsg.offer.if_type) &&
206 !uuid_le_cmp(channel->offermsg.offer.if_instance,
207 newchannel->offermsg.offer.if_instance)) {
220 free_channel(newchannel);
230 &newchannel->offermsg.offer.if_type,
231 &newchannel->offermsg.offer.if_instance,
241 pr_err(
"unable to add child device object (relid %d)\n",
242 newchannel->offermsg.child_relid);
247 kfree(newchannel->device_obj);
249 free_channel(newchannel);
256 newchannel->state = CHANNEL_OPEN_STATE;
264 static void vmbus_onoffer(
struct vmbus_channel_message_header *
hdr)
266 struct vmbus_channel_offer_channel *offer;
267 struct vmbus_channel *newchannel;
271 offer = (
struct vmbus_channel_offer_channel *)hdr;
273 guidtype = &offer->offer.if_type;
274 guidinstance = &offer->offer.if_instance;
277 newchannel = alloc_channel();
279 pr_err(
"Unable to allocate channel object\n");
283 memcpy(&newchannel->offermsg, offer,
284 sizeof(
struct vmbus_channel_offer_channel));
285 newchannel->monitor_grp = (
u8)offer->monitorid / 32;
286 newchannel->monitor_bit = (
u8)offer->monitorid % 32;
288 INIT_WORK(&newchannel->work, vmbus_process_offer);
289 queue_work(newchannel->controlwq, &newchannel->work);
297 static void vmbus_onoffer_rescind(
struct vmbus_channel_message_header *hdr)
299 struct vmbus_channel_rescind_offer *rescind;
302 rescind = (
struct vmbus_channel_rescind_offer *)hdr;
311 queue_work(channel->controlwq, &channel->work);
320 static void vmbus_onoffers_delivered(
321 struct vmbus_channel_message_header *hdr)
332 static void vmbus_onopen_result(
struct vmbus_channel_message_header *hdr)
334 struct vmbus_channel_open_result *
result;
335 struct vmbus_channel_msginfo *
msginfo;
336 struct vmbus_channel_message_header *requestheader;
337 struct vmbus_channel_open_channel *openmsg;
340 result = (
struct vmbus_channel_open_result *)hdr;
350 (
struct vmbus_channel_message_header *)msginfo->msg;
352 if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
354 (
struct vmbus_channel_open_channel *)msginfo->msg;
355 if (openmsg->child_relid == result->child_relid &&
356 openmsg->openid == result->openid) {
357 memcpy(&msginfo->response.open_result,
360 struct vmbus_channel_open_result));
376 static void vmbus_ongpadl_created(
struct vmbus_channel_message_header *hdr)
378 struct vmbus_channel_gpadl_created *gpadlcreated;
379 struct vmbus_channel_msginfo *msginfo;
380 struct vmbus_channel_message_header *requestheader;
381 struct vmbus_channel_gpadl_header *gpadlheader;
384 gpadlcreated = (
struct vmbus_channel_gpadl_created *)hdr;
395 (
struct vmbus_channel_message_header *)msginfo->msg;
397 if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
399 (
struct vmbus_channel_gpadl_header *)requestheader;
401 if ((gpadlcreated->child_relid ==
402 gpadlheader->child_relid) &&
403 (gpadlcreated->gpadl == gpadlheader->gpadl)) {
404 memcpy(&msginfo->response.gpadl_created,
407 struct vmbus_channel_gpadl_created));
423 static void vmbus_ongpadl_torndown(
424 struct vmbus_channel_message_header *hdr)
426 struct vmbus_channel_gpadl_torndown *gpadl_torndown;
427 struct vmbus_channel_msginfo *msginfo;
428 struct vmbus_channel_message_header *requestheader;
429 struct vmbus_channel_gpadl_teardown *gpadl_teardown;
432 gpadl_torndown = (
struct vmbus_channel_gpadl_torndown *)hdr;
442 (
struct vmbus_channel_message_header *)msginfo->msg;
444 if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
446 (
struct vmbus_channel_gpadl_teardown *)requestheader;
448 if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
449 memcpy(&msginfo->response.gpadl_torndown,
452 struct vmbus_channel_gpadl_torndown));
468 static void vmbus_onversion_response(
469 struct vmbus_channel_message_header *hdr)
471 struct vmbus_channel_msginfo *msginfo;
472 struct vmbus_channel_message_header *requestheader;
473 struct vmbus_channel_initiate_contact *initiate;
474 struct vmbus_channel_version_response *version_response;
477 version_response = (
struct vmbus_channel_version_response *)hdr;
483 (
struct vmbus_channel_message_header *)msginfo->msg;
485 if (requestheader->msgtype ==
486 CHANNELMSG_INITIATE_CONTACT) {
488 (
struct vmbus_channel_initiate_contact *)requestheader;
489 memcpy(&msginfo->response.version_response,
491 sizeof(
struct vmbus_channel_version_response));
500 channel_message_table[CHANNELMSG_COUNT] = {
501 {CHANNELMSG_INVALID,
NULL},
502 {CHANNELMSG_OFFERCHANNEL, vmbus_onoffer},
503 {CHANNELMSG_RESCIND_CHANNELOFFER, vmbus_onoffer_rescind},
504 {CHANNELMSG_REQUESTOFFERS,
NULL},
505 {CHANNELMSG_ALLOFFERS_DELIVERED, vmbus_onoffers_delivered},
506 {CHANNELMSG_OPENCHANNEL,
NULL},
507 {CHANNELMSG_OPENCHANNEL_RESULT, vmbus_onopen_result},
508 {CHANNELMSG_CLOSECHANNEL,
NULL},
509 {CHANNELMSG_GPADL_HEADER,
NULL},
510 {CHANNELMSG_GPADL_BODY,
NULL},
511 {CHANNELMSG_GPADL_CREATED, vmbus_ongpadl_created},
512 {CHANNELMSG_GPADL_TEARDOWN,
NULL},
513 {CHANNELMSG_GPADL_TORNDOWN, vmbus_ongpadl_torndown},
514 {CHANNELMSG_RELID_RELEASED,
NULL},
515 {CHANNELMSG_INITIATE_CONTACT,
NULL},
516 {CHANNELMSG_VERSION_RESPONSE, vmbus_onversion_response},
517 {CHANNELMSG_UNLOAD,
NULL},
528 struct vmbus_channel_message_header *
hdr;
531 hdr = (
struct vmbus_channel_message_header *)msg->
u.
payload;
532 size = msg->
header.payload_size;
534 if (hdr->msgtype >= CHANNELMSG_COUNT) {
535 pr_err(
"Received invalid channel message type %d size %d\n",
538 (
unsigned char *)msg->
u.
payload, size);
545 pr_err(
"Unhandled channel message type %d\n", hdr->msgtype);
553 struct vmbus_channel_message_header *
msg;
554 struct vmbus_channel_msginfo *msginfo;
557 msginfo =
kmalloc(
sizeof(*msginfo) +
558 sizeof(
struct vmbus_channel_message_header),
563 init_completion(&msginfo->waitevent);
565 msg = (
struct vmbus_channel_message_header *)msginfo->msg;
567 msg->msgtype = CHANNELMSG_REQUESTOFFERS;
571 sizeof(
struct vmbus_channel_message_header));
573 pr_err(
"Unable to request offers - %d\n", ret);