123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677 |
- /*
- * Copyright (c) 2009, Microsoft Corporation.
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms and conditions of the GNU General Public License,
- * version 2, as published by the Free Software Foundation.
- *
- * This program is distributed in the hope it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place - Suite 330, Boston, MA 02111-1307 USA.
- *
- * Authors:
- * Haiyang Zhang <haiyangz@microsoft.com>
- * Hank Janssen <hjanssen@microsoft.com>
- */
- #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
- #include <linux/kernel.h>
- #include <linux/sched.h>
- #include <linux/wait.h>
- #include <linux/mm.h>
- #include <linux/slab.h>
- #include <linux/list.h>
- #include <linux/module.h>
- #include <linux/completion.h>
- #include <linux/hyperv.h>
- #include "hyperv_vmbus.h"
- struct vmbus_channel_message_table_entry {
- enum vmbus_channel_message_type message_type;
- void (*message_handler)(struct vmbus_channel_message_header *msg);
- };
- /**
- * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message
- * @icmsghdrp: Pointer to msg header structure
- * @icmsg_negotiate: Pointer to negotiate message structure
- * @buf: Raw buffer channel data
- *
- * @icmsghdrp is of type &struct icmsg_hdr.
- * @negop is of type &struct icmsg_negotiate.
- * Set up and fill in default negotiate response message.
- *
- * The max_fw_version specifies the maximum framework version that
- * we can support and max _srv_version specifies the maximum service
- * version we can support. A special value MAX_SRV_VER can be
- * specified to indicate that we can handle the maximum version
- * exposed by the host.
- *
- * Mainly used by Hyper-V drivers.
- */
- void vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,
- struct icmsg_negotiate *negop, u8 *buf,
- int max_fw_version, int max_srv_version)
- {
- int icframe_vercnt;
- int icmsg_vercnt;
- int i;
- icmsghdrp->icmsgsize = 0x10;
- negop = (struct icmsg_negotiate *)&buf[
- sizeof(struct vmbuspipe_hdr) +
- sizeof(struct icmsg_hdr)];
- icframe_vercnt = negop->icframe_vercnt;
- icmsg_vercnt = negop->icmsg_vercnt;
- /*
- * Select the framework version number we will
- * support.
- */
- for (i = 0; i < negop->icframe_vercnt; i++) {
- if (negop->icversion_data[i].major <= max_fw_version)
- icframe_vercnt = negop->icversion_data[i].major;
- }
- for (i = negop->icframe_vercnt;
- (i < negop->icframe_vercnt + negop->icmsg_vercnt); i++) {
- if (negop->icversion_data[i].major <= max_srv_version)
- icmsg_vercnt = negop->icversion_data[i].major;
- }
- /*
- * Respond with the maximum framework and service
- * version numbers we can support.
- */
- negop->icframe_vercnt = 1;
- negop->icmsg_vercnt = 1;
- negop->icversion_data[0].major = icframe_vercnt;
- negop->icversion_data[0].minor = 0;
- negop->icversion_data[1].major = icmsg_vercnt;
- negop->icversion_data[1].minor = 0;
- }
- EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp);
- /*
- * alloc_channel - Allocate and initialize a vmbus channel object
- */
- static struct vmbus_channel *alloc_channel(void)
- {
- struct vmbus_channel *channel;
- channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
- if (!channel)
- return NULL;
- spin_lock_init(&channel->inbound_lock);
- channel->controlwq = create_workqueue("hv_vmbus_ctl");
- if (!channel->controlwq) {
- kfree(channel);
- return NULL;
- }
- return channel;
- }
- /*
- * release_hannel - Release the vmbus channel object itself
- */
- static void release_channel(struct work_struct *work)
- {
- struct vmbus_channel *channel = container_of(work,
- struct vmbus_channel,
- work);
- destroy_workqueue(channel->controlwq);
- kfree(channel);
- }
- /*
- * free_channel - Release the resources used by the vmbus channel object
- */
- static void free_channel(struct vmbus_channel *channel)
- {
- /*
- * We have to release the channel's workqueue/thread in the vmbus's
- * workqueue/thread context
- * ie we can't destroy ourselves.
- */
- INIT_WORK(&channel->work, release_channel);
- queue_work(vmbus_connection.work_queue, &channel->work);
- }
- /*
- * vmbus_process_rescind_offer -
- * Rescind the offer by initiating a device removal
- */
- static void vmbus_process_rescind_offer(struct work_struct *work)
- {
- struct vmbus_channel *channel = container_of(work,
- struct vmbus_channel,
- work);
- vmbus_device_unregister(channel->device_obj);
- }
- void vmbus_free_channels(void)
- {
- struct vmbus_channel *channel;
- list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
- vmbus_device_unregister(channel->device_obj);
- kfree(channel->device_obj);
- free_channel(channel);
- }
- }
- /*
- * vmbus_process_offer - Process the offer by creating a channel/device
- * associated with this offer
- */
- static void vmbus_process_offer(struct work_struct *work)
- {
- struct vmbus_channel *newchannel = container_of(work,
- struct vmbus_channel,
- work);
- struct vmbus_channel *channel;
- bool fnew = true;
- int ret;
- unsigned long flags;
- /* The next possible work is rescind handling */
- INIT_WORK(&newchannel->work, vmbus_process_rescind_offer);
- /* Make sure this is a new offer */
- spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
- list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
- if (!uuid_le_cmp(channel->offermsg.offer.if_type,
- newchannel->offermsg.offer.if_type) &&
- !uuid_le_cmp(channel->offermsg.offer.if_instance,
- newchannel->offermsg.offer.if_instance)) {
- fnew = false;
- break;
- }
- }
- if (fnew)
- list_add_tail(&newchannel->listentry,
- &vmbus_connection.chn_list);
- spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
- if (!fnew) {
- free_channel(newchannel);
- return;
- }
- /*
- * Start the process of binding this offer to the driver
- * We need to set the DeviceObject field before calling
- * vmbus_child_dev_add()
- */
- newchannel->device_obj = vmbus_device_create(
- &newchannel->offermsg.offer.if_type,
- &newchannel->offermsg.offer.if_instance,
- newchannel);
- /*
- * Add the new device to the bus. This will kick off device-driver
- * binding which eventually invokes the device driver's AddDevice()
- * method.
- */
- ret = vmbus_device_register(newchannel->device_obj);
- if (ret != 0) {
- pr_err("unable to add child device object (relid %d)\n",
- newchannel->offermsg.child_relid);
- spin_lock_irqsave(&vmbus_connection.channel_lock, flags);
- list_del(&newchannel->listentry);
- spin_unlock_irqrestore(&vmbus_connection.channel_lock, flags);
- kfree(newchannel->device_obj);
- free_channel(newchannel);
- } else {
- /*
- * This state is used to indicate a successful open
- * so that when we do close the channel normally, we
- * can cleanup properly
- */
- newchannel->state = CHANNEL_OPEN_STATE;
- }
- }
- enum {
- IDE = 0,
- SCSI,
- NIC,
- MAX_PERF_CHN,
- };
- /*
- * This is an array of device_ids (device types) that are performance critical.
- * We attempt to distribute the interrupt load for these devices across
- * all available CPUs.
- */
- static const struct hv_vmbus_device_id hp_devs[] = {
- /* IDE */
- { HV_IDE_GUID, },
- /* Storage - SCSI */
- { HV_SCSI_GUID, },
- /* Network */
- { HV_NIC_GUID, },
- };
- /*
- * We use this state to statically distribute the channel interrupt load.
- */
- static u32 next_vp;
- /*
- * Starting with Win8, we can statically distribute the incoming
- * channel interrupt load by binding a channel to VCPU. We
- * implement here a simple round robin scheme for distributing
- * the interrupt load.
- * We will bind channels that are not performance critical to cpu 0 and
- * performance critical channels (IDE, SCSI and Network) will be uniformly
- * distributed across all available CPUs.
- */
- static u32 get_vp_index(uuid_le *type_guid)
- {
- u32 cur_cpu;
- int i;
- bool perf_chn = false;
- u32 max_cpus = num_online_cpus();
- for (i = IDE; i < MAX_PERF_CHN; i++) {
- if (!memcmp(type_guid->b, hp_devs[i].guid,
- sizeof(uuid_le))) {
- perf_chn = true;
- break;
- }
- }
- if ((vmbus_proto_version == VERSION_WS2008) ||
- (vmbus_proto_version == VERSION_WIN7) || (!perf_chn)) {
- /*
- * Prior to win8, all channel interrupts are
- * delivered on cpu 0.
- * Also if the channel is not a performance critical
- * channel, bind it to cpu 0.
- */
- return 0;
- }
- cur_cpu = (++next_vp % max_cpus);
- return cur_cpu;
- }
- /*
- * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
- *
- */
- static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
- {
- struct vmbus_channel_offer_channel *offer;
- struct vmbus_channel *newchannel;
- offer = (struct vmbus_channel_offer_channel *)hdr;
- /* Allocate the channel object and save this offer. */
- newchannel = alloc_channel();
- if (!newchannel) {
- pr_err("Unable to allocate channel object\n");
- return;
- }
- /*
- * By default we setup state to enable batched
- * reading. A specific service can choose to
- * disable this prior to opening the channel.
- */
- newchannel->batched_reading = true;
- /*
- * Setup state for signalling the host.
- */
- newchannel->sig_event = (struct hv_input_signal_event *)
- (ALIGN((unsigned long)
- &newchannel->sig_buf,
- HV_HYPERCALL_PARAM_ALIGN));
- newchannel->sig_event->connectionid.asu32 = 0;
- newchannel->sig_event->connectionid.u.id = VMBUS_EVENT_CONNECTION_ID;
- newchannel->sig_event->flag_number = 0;
- newchannel->sig_event->rsvdz = 0;
- if (vmbus_proto_version != VERSION_WS2008) {
- newchannel->is_dedicated_interrupt =
- (offer->is_dedicated_interrupt != 0);
- newchannel->sig_event->connectionid.u.id =
- offer->connection_id;
- }
- newchannel->target_vp = get_vp_index(&offer->offer.if_type);
- memcpy(&newchannel->offermsg, offer,
- sizeof(struct vmbus_channel_offer_channel));
- newchannel->monitor_grp = (u8)offer->monitorid / 32;
- newchannel->monitor_bit = (u8)offer->monitorid % 32;
- INIT_WORK(&newchannel->work, vmbus_process_offer);
- queue_work(newchannel->controlwq, &newchannel->work);
- }
- /*
- * vmbus_onoffer_rescind - Rescind offer handler.
- *
- * We queue a work item to process this offer synchronously
- */
- static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
- {
- struct vmbus_channel_rescind_offer *rescind;
- struct vmbus_channel *channel;
- rescind = (struct vmbus_channel_rescind_offer *)hdr;
- channel = relid2channel(rescind->child_relid);
- if (channel == NULL)
- /* Just return here, no channel found */
- return;
- /* work is initialized for vmbus_process_rescind_offer() from
- * vmbus_process_offer() where the channel got created */
- queue_work(channel->controlwq, &channel->work);
- }
- /*
- * vmbus_onoffers_delivered -
- * This is invoked when all offers have been delivered.
- *
- * Nothing to do here.
- */
- static void vmbus_onoffers_delivered(
- struct vmbus_channel_message_header *hdr)
- {
- }
- /*
- * vmbus_onopen_result - Open result handler.
- *
- * This is invoked when we received a response to our channel open request.
- * Find the matching request, copy the response and signal the requesting
- * thread.
- */
- static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
- {
- struct vmbus_channel_open_result *result;
- struct vmbus_channel_msginfo *msginfo;
- struct vmbus_channel_message_header *requestheader;
- struct vmbus_channel_open_channel *openmsg;
- unsigned long flags;
- result = (struct vmbus_channel_open_result *)hdr;
- /*
- * Find the open msg, copy the result and signal/unblock the wait event
- */
- spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
- list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
- msglistentry) {
- requestheader =
- (struct vmbus_channel_message_header *)msginfo->msg;
- if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
- openmsg =
- (struct vmbus_channel_open_channel *)msginfo->msg;
- if (openmsg->child_relid == result->child_relid &&
- openmsg->openid == result->openid) {
- memcpy(&msginfo->response.open_result,
- result,
- sizeof(
- struct vmbus_channel_open_result));
- complete(&msginfo->waitevent);
- break;
- }
- }
- }
- spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
- }
- /*
- * vmbus_ongpadl_created - GPADL created handler.
- *
- * This is invoked when we received a response to our gpadl create request.
- * Find the matching request, copy the response and signal the requesting
- * thread.
- */
- static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
- {
- struct vmbus_channel_gpadl_created *gpadlcreated;
- struct vmbus_channel_msginfo *msginfo;
- struct vmbus_channel_message_header *requestheader;
- struct vmbus_channel_gpadl_header *gpadlheader;
- unsigned long flags;
- gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
- /*
- * Find the establish msg, copy the result and signal/unblock the wait
- * event
- */
- spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
- list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
- msglistentry) {
- requestheader =
- (struct vmbus_channel_message_header *)msginfo->msg;
- if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
- gpadlheader =
- (struct vmbus_channel_gpadl_header *)requestheader;
- if ((gpadlcreated->child_relid ==
- gpadlheader->child_relid) &&
- (gpadlcreated->gpadl == gpadlheader->gpadl)) {
- memcpy(&msginfo->response.gpadl_created,
- gpadlcreated,
- sizeof(
- struct vmbus_channel_gpadl_created));
- complete(&msginfo->waitevent);
- break;
- }
- }
- }
- spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
- }
- /*
- * vmbus_ongpadl_torndown - GPADL torndown handler.
- *
- * This is invoked when we received a response to our gpadl teardown request.
- * Find the matching request, copy the response and signal the requesting
- * thread.
- */
- static void vmbus_ongpadl_torndown(
- struct vmbus_channel_message_header *hdr)
- {
- struct vmbus_channel_gpadl_torndown *gpadl_torndown;
- struct vmbus_channel_msginfo *msginfo;
- struct vmbus_channel_message_header *requestheader;
- struct vmbus_channel_gpadl_teardown *gpadl_teardown;
- unsigned long flags;
- gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
- /*
- * Find the open msg, copy the result and signal/unblock the wait event
- */
- spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
- list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
- msglistentry) {
- requestheader =
- (struct vmbus_channel_message_header *)msginfo->msg;
- if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
- gpadl_teardown =
- (struct vmbus_channel_gpadl_teardown *)requestheader;
- if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
- memcpy(&msginfo->response.gpadl_torndown,
- gpadl_torndown,
- sizeof(
- struct vmbus_channel_gpadl_torndown));
- complete(&msginfo->waitevent);
- break;
- }
- }
- }
- spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
- }
- /*
- * vmbus_onversion_response - Version response handler
- *
- * This is invoked when we received a response to our initiate contact request.
- * Find the matching request, copy the response and signal the requesting
- * thread.
- */
- static void vmbus_onversion_response(
- struct vmbus_channel_message_header *hdr)
- {
- struct vmbus_channel_msginfo *msginfo;
- struct vmbus_channel_message_header *requestheader;
- struct vmbus_channel_version_response *version_response;
- unsigned long flags;
- version_response = (struct vmbus_channel_version_response *)hdr;
- spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
- list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
- msglistentry) {
- requestheader =
- (struct vmbus_channel_message_header *)msginfo->msg;
- if (requestheader->msgtype ==
- CHANNELMSG_INITIATE_CONTACT) {
- memcpy(&msginfo->response.version_response,
- version_response,
- sizeof(struct vmbus_channel_version_response));
- complete(&msginfo->waitevent);
- }
- }
- spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
- }
- /* Channel message dispatch table */
- static struct vmbus_channel_message_table_entry
- channel_message_table[CHANNELMSG_COUNT] = {
- {CHANNELMSG_INVALID, NULL},
- {CHANNELMSG_OFFERCHANNEL, vmbus_onoffer},
- {CHANNELMSG_RESCIND_CHANNELOFFER, vmbus_onoffer_rescind},
- {CHANNELMSG_REQUESTOFFERS, NULL},
- {CHANNELMSG_ALLOFFERS_DELIVERED, vmbus_onoffers_delivered},
- {CHANNELMSG_OPENCHANNEL, NULL},
- {CHANNELMSG_OPENCHANNEL_RESULT, vmbus_onopen_result},
- {CHANNELMSG_CLOSECHANNEL, NULL},
- {CHANNELMSG_GPADL_HEADER, NULL},
- {CHANNELMSG_GPADL_BODY, NULL},
- {CHANNELMSG_GPADL_CREATED, vmbus_ongpadl_created},
- {CHANNELMSG_GPADL_TEARDOWN, NULL},
- {CHANNELMSG_GPADL_TORNDOWN, vmbus_ongpadl_torndown},
- {CHANNELMSG_RELID_RELEASED, NULL},
- {CHANNELMSG_INITIATE_CONTACT, NULL},
- {CHANNELMSG_VERSION_RESPONSE, vmbus_onversion_response},
- {CHANNELMSG_UNLOAD, NULL},
- };
- /*
- * vmbus_onmessage - Handler for channel protocol messages.
- *
- * This is invoked in the vmbus worker thread context.
- */
- void vmbus_onmessage(void *context)
- {
- struct hv_message *msg = context;
- struct vmbus_channel_message_header *hdr;
- int size;
- hdr = (struct vmbus_channel_message_header *)msg->u.payload;
- size = msg->header.payload_size;
- if (hdr->msgtype >= CHANNELMSG_COUNT) {
- pr_err("Received invalid channel message type %d size %d\n",
- hdr->msgtype, size);
- print_hex_dump_bytes("", DUMP_PREFIX_NONE,
- (unsigned char *)msg->u.payload, size);
- return;
- }
- if (channel_message_table[hdr->msgtype].message_handler)
- channel_message_table[hdr->msgtype].message_handler(hdr);
- else
- pr_err("Unhandled channel message type %d\n", hdr->msgtype);
- }
- /*
- * vmbus_request_offers - Send a request to get all our pending offers.
- */
- int vmbus_request_offers(void)
- {
- struct vmbus_channel_message_header *msg;
- struct vmbus_channel_msginfo *msginfo;
- int ret, t;
- msginfo = kmalloc(sizeof(*msginfo) +
- sizeof(struct vmbus_channel_message_header),
- GFP_KERNEL);
- if (!msginfo)
- return -ENOMEM;
- init_completion(&msginfo->waitevent);
- msg = (struct vmbus_channel_message_header *)msginfo->msg;
- msg->msgtype = CHANNELMSG_REQUESTOFFERS;
- ret = vmbus_post_msg(msg,
- sizeof(struct vmbus_channel_message_header));
- if (ret != 0) {
- pr_err("Unable to request offers - %d\n", ret);
- goto cleanup;
- }
- t = wait_for_completion_timeout(&msginfo->waitevent, 5*HZ);
- if (t == 0) {
- ret = -ETIMEDOUT;
- goto cleanup;
- }
- cleanup:
- kfree(msginfo);
- return ret;
- }
- /* eof */
|