123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251 |
- #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
- #include <linux/kernel.h>
- #include <linux/interrupt.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/delay.h>
- #include <linux/hyperv.h>
- #include "hyperv_vmbus.h"
- static void init_vp_index(struct vmbus_channel *channel, u16 dev_type);
- static const struct vmbus_device vmbus_devs[] = {
-
- { .dev_type = HV_IDE,
- HV_IDE_GUID,
- .perf_device = true,
- },
-
- { .dev_type = HV_SCSI,
- HV_SCSI_GUID,
- .perf_device = true,
- },
-
- { .dev_type = HV_FC,
- HV_SYNTHFC_GUID,
- .perf_device = true,
- },
-
- { .dev_type = HV_NIC,
- HV_NIC_GUID,
- .perf_device = true,
- },
-
- { .dev_type = HV_ND,
- HV_ND_GUID,
- .perf_device = true,
- },
-
- { .dev_type = HV_PCIE,
- HV_PCIE_GUID,
- .perf_device = true,
- },
-
- { .dev_type = HV_FB,
- HV_SYNTHVID_GUID,
- .perf_device = false,
- },
-
- { .dev_type = HV_KBD,
- HV_KBD_GUID,
- .perf_device = false,
- },
-
- { .dev_type = HV_MOUSE,
- HV_MOUSE_GUID,
- .perf_device = false,
- },
-
- { .dev_type = HV_KVP,
- HV_KVP_GUID,
- .perf_device = false,
- },
-
- { .dev_type = HV_TS,
- HV_TS_GUID,
- .perf_device = false,
- },
-
- { .dev_type = HV_HB,
- HV_HEART_BEAT_GUID,
- .perf_device = false,
- },
-
- { .dev_type = HV_SHUTDOWN,
- HV_SHUTDOWN_GUID,
- .perf_device = false,
- },
-
- { .dev_type = HV_FCOPY,
- HV_FCOPY_GUID,
- .perf_device = false,
- },
-
- { .dev_type = HV_BACKUP,
- HV_VSS_GUID,
- .perf_device = false,
- },
-
- { .dev_type = HV_DM,
- HV_DM_GUID,
- .perf_device = false,
- },
-
- { .dev_type = HV_UNKOWN,
- .perf_device = false,
- },
- };
- static const struct {
- uuid_le guid;
- } vmbus_unsupported_devs[] = {
- { HV_AVMA1_GUID },
- { HV_AVMA2_GUID },
- { HV_RDV_GUID },
- };
- static void vmbus_rescind_cleanup(struct vmbus_channel *channel)
- {
- struct vmbus_channel_msginfo *msginfo;
- unsigned long flags;
- spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
- list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
- msglistentry) {
- if (msginfo->waiting_channel == channel) {
- complete(&msginfo->waitevent);
- break;
- }
- }
- spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
- }
- static bool is_unsupported_vmbus_devs(const uuid_le *guid)
- {
- int i;
- for (i = 0; i < ARRAY_SIZE(vmbus_unsupported_devs); i++)
- if (!uuid_le_cmp(*guid, vmbus_unsupported_devs[i].guid))
- return true;
- return false;
- }
- static u16 hv_get_dev_type(const struct vmbus_channel *channel)
- {
- const uuid_le *guid = &channel->offermsg.offer.if_type;
- u16 i;
- if (is_hvsock_channel(channel) || is_unsupported_vmbus_devs(guid))
- return HV_UNKOWN;
- for (i = HV_IDE; i < HV_UNKOWN; i++) {
- if (!uuid_le_cmp(*guid, vmbus_devs[i].guid))
- return i;
- }
- pr_info("Unknown GUID: %pUl\n", guid);
- return i;
- }
- bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,
- struct icmsg_negotiate *negop, u8 *buf,
- int fw_version, int srv_version)
- {
- int icframe_major, icframe_minor;
- int icmsg_major, icmsg_minor;
- int fw_major, fw_minor;
- int srv_major, srv_minor;
- int i;
- bool found_match = false;
- icmsghdrp->icmsgsize = 0x10;
- fw_major = (fw_version >> 16);
- fw_minor = (fw_version & 0xFFFF);
- srv_major = (srv_version >> 16);
- srv_minor = (srv_version & 0xFFFF);
- negop = (struct icmsg_negotiate *)&buf[
- sizeof(struct vmbuspipe_hdr) +
- sizeof(struct icmsg_hdr)];
- icframe_major = negop->icframe_vercnt;
- icframe_minor = 0;
- icmsg_major = negop->icmsg_vercnt;
- icmsg_minor = 0;
-
- for (i = 0; i < negop->icframe_vercnt; i++) {
- if ((negop->icversion_data[i].major == fw_major) &&
- (negop->icversion_data[i].minor == fw_minor)) {
- icframe_major = negop->icversion_data[i].major;
- icframe_minor = negop->icversion_data[i].minor;
- found_match = true;
- }
- }
- if (!found_match)
- goto fw_error;
- found_match = false;
- for (i = negop->icframe_vercnt;
- (i < negop->icframe_vercnt + negop->icmsg_vercnt); i++) {
- if ((negop->icversion_data[i].major == srv_major) &&
- (negop->icversion_data[i].minor == srv_minor)) {
- icmsg_major = negop->icversion_data[i].major;
- icmsg_minor = negop->icversion_data[i].minor;
- found_match = true;
- }
- }
-
- fw_error:
- if (!found_match) {
- negop->icframe_vercnt = 0;
- negop->icmsg_vercnt = 0;
- } else {
- negop->icframe_vercnt = 1;
- negop->icmsg_vercnt = 1;
- }
- negop->icversion_data[0].major = icframe_major;
- negop->icversion_data[0].minor = icframe_minor;
- negop->icversion_data[1].major = icmsg_major;
- negop->icversion_data[1].minor = icmsg_minor;
- return found_match;
- }
- EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp);
- static struct vmbus_channel *alloc_channel(void)
- {
- struct vmbus_channel *channel;
- channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
- if (!channel)
- return NULL;
- channel->acquire_ring_lock = true;
- spin_lock_init(&channel->inbound_lock);
- spin_lock_init(&channel->lock);
- INIT_LIST_HEAD(&channel->sc_list);
- INIT_LIST_HEAD(&channel->percpu_list);
- return channel;
- }
- static void free_channel(struct vmbus_channel *channel)
- {
- kfree(channel);
- }
- static void percpu_channel_enq(void *arg)
- {
- struct vmbus_channel *channel = arg;
- int cpu = smp_processor_id();
- list_add_tail(&channel->percpu_list, &hv_context.percpu_list[cpu]);
- }
- static void percpu_channel_deq(void *arg)
- {
- struct vmbus_channel *channel = arg;
- list_del(&channel->percpu_list);
- }
- static void vmbus_release_relid(u32 relid)
- {
- struct vmbus_channel_relid_released msg;
- memset(&msg, 0, sizeof(struct vmbus_channel_relid_released));
- msg.child_relid = relid;
- msg.header.msgtype = CHANNELMSG_RELID_RELEASED;
- vmbus_post_msg(&msg, sizeof(struct vmbus_channel_relid_released),
- true);
- }
- void hv_event_tasklet_disable(struct vmbus_channel *channel)
- {
- struct tasklet_struct *tasklet;
- tasklet = hv_context.event_dpc[channel->target_cpu];
- tasklet_disable(tasklet);
- }
- void hv_event_tasklet_enable(struct vmbus_channel *channel)
- {
- struct tasklet_struct *tasklet;
- tasklet = hv_context.event_dpc[channel->target_cpu];
- tasklet_enable(tasklet);
-
- tasklet_schedule(tasklet);
- }
- void hv_process_channel_removal(struct vmbus_channel *channel, u32 relid)
- {
- unsigned long flags;
- struct vmbus_channel *primary_channel;
- BUG_ON(!channel->rescind);
- BUG_ON(!mutex_is_locked(&vmbus_connection.channel_mutex));
- hv_event_tasklet_disable(channel);
- if (channel->target_cpu != get_cpu()) {
- put_cpu();
- smp_call_function_single(channel->target_cpu,
- percpu_channel_deq, channel, true);
- } else {
- percpu_channel_deq(channel);
- put_cpu();
- }
- hv_event_tasklet_enable(channel);
- if (channel->primary_channel == NULL) {
- list_del(&channel->listentry);
- primary_channel = channel;
- } else {
- primary_channel = channel->primary_channel;
- spin_lock_irqsave(&primary_channel->lock, flags);
- list_del(&channel->sc_list);
- primary_channel->num_sc--;
- spin_unlock_irqrestore(&primary_channel->lock, flags);
- }
-
- if (channel->affinity_policy == HV_LOCALIZED)
- cpumask_clear_cpu(channel->target_cpu,
- &primary_channel->alloced_cpus_in_node);
- vmbus_release_relid(relid);
- free_channel(channel);
- }
- void vmbus_free_channels(void)
- {
- struct vmbus_channel *channel, *tmp;
- mutex_lock(&vmbus_connection.channel_mutex);
- list_for_each_entry_safe(channel, tmp, &vmbus_connection.chn_list,
- listentry) {
-
- channel->rescind = true;
- vmbus_device_unregister(channel->device_obj);
- }
- mutex_unlock(&vmbus_connection.channel_mutex);
- }
- static void vmbus_process_offer(struct vmbus_channel *newchannel)
- {
- struct vmbus_channel *channel;
- bool fnew = true;
- unsigned long flags;
- u16 dev_type;
- int ret;
-
- mutex_lock(&vmbus_connection.channel_mutex);
- 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);
- mutex_unlock(&vmbus_connection.channel_mutex);
- if (!fnew) {
-
- if (newchannel->offermsg.offer.sub_channel_index != 0) {
-
- newchannel->primary_channel = channel;
- spin_lock_irqsave(&channel->lock, flags);
- list_add_tail(&newchannel->sc_list, &channel->sc_list);
- channel->num_sc++;
- spin_unlock_irqrestore(&channel->lock, flags);
- } else
- goto err_free_chan;
- }
- dev_type = hv_get_dev_type(newchannel);
- init_vp_index(newchannel, dev_type);
- hv_event_tasklet_disable(newchannel);
- if (newchannel->target_cpu != get_cpu()) {
- put_cpu();
- smp_call_function_single(newchannel->target_cpu,
- percpu_channel_enq,
- newchannel, true);
- } else {
- percpu_channel_enq(newchannel);
- put_cpu();
- }
- hv_event_tasklet_enable(newchannel);
-
- newchannel->state = CHANNEL_OPEN_STATE;
- if (!fnew) {
- if (channel->sc_creation_callback != NULL)
- channel->sc_creation_callback(newchannel);
- return;
- }
-
- newchannel->device_obj = vmbus_device_create(
- &newchannel->offermsg.offer.if_type,
- &newchannel->offermsg.offer.if_instance,
- newchannel);
- if (!newchannel->device_obj)
- goto err_deq_chan;
- newchannel->device_obj->device_id = dev_type;
-
- mutex_lock(&vmbus_connection.channel_mutex);
- ret = vmbus_device_register(newchannel->device_obj);
- mutex_unlock(&vmbus_connection.channel_mutex);
- if (ret != 0) {
- pr_err("unable to add child device object (relid %d)\n",
- newchannel->offermsg.child_relid);
- kfree(newchannel->device_obj);
- goto err_deq_chan;
- }
- return;
- err_deq_chan:
- mutex_lock(&vmbus_connection.channel_mutex);
- list_del(&newchannel->listentry);
- mutex_unlock(&vmbus_connection.channel_mutex);
- hv_event_tasklet_disable(newchannel);
- if (newchannel->target_cpu != get_cpu()) {
- put_cpu();
- smp_call_function_single(newchannel->target_cpu,
- percpu_channel_deq, newchannel, true);
- } else {
- percpu_channel_deq(newchannel);
- put_cpu();
- }
- hv_event_tasklet_enable(newchannel);
- vmbus_release_relid(newchannel->offermsg.child_relid);
- err_free_chan:
- free_channel(newchannel);
- }
- static int next_numa_node_id;
- static void init_vp_index(struct vmbus_channel *channel, u16 dev_type)
- {
- u32 cur_cpu;
- bool perf_chn = vmbus_devs[dev_type].perf_device;
- struct vmbus_channel *primary = channel->primary_channel;
- int next_node;
- struct cpumask available_mask;
- struct cpumask *alloced_mask;
- if ((vmbus_proto_version == VERSION_WS2008) ||
- (vmbus_proto_version == VERSION_WIN7) || (!perf_chn)) {
-
- channel->numa_node = 0;
- channel->target_cpu = 0;
- channel->target_vp = hv_context.vp_index[0];
- return;
- }
-
- if ((channel->affinity_policy == HV_BALANCED) || (!primary)) {
- while (true) {
- next_node = next_numa_node_id++;
- if (next_node == nr_node_ids) {
- next_node = next_numa_node_id = 0;
- continue;
- }
- if (cpumask_empty(cpumask_of_node(next_node)))
- continue;
- break;
- }
- channel->numa_node = next_node;
- primary = channel;
- }
- alloced_mask = &hv_context.hv_numa_map[primary->numa_node];
- if (cpumask_weight(alloced_mask) ==
- cpumask_weight(cpumask_of_node(primary->numa_node))) {
-
- cpumask_clear(alloced_mask);
- }
- cpumask_xor(&available_mask, alloced_mask,
- cpumask_of_node(primary->numa_node));
- cur_cpu = -1;
- if (primary->affinity_policy == HV_LOCALIZED) {
-
- if (cpumask_equal(&primary->alloced_cpus_in_node,
- cpumask_of_node(primary->numa_node)))
- cpumask_clear(&primary->alloced_cpus_in_node);
- }
- while (true) {
- cur_cpu = cpumask_next(cur_cpu, &available_mask);
- if (cur_cpu >= nr_cpu_ids) {
- cur_cpu = -1;
- cpumask_copy(&available_mask,
- cpumask_of_node(primary->numa_node));
- continue;
- }
- if (primary->affinity_policy == HV_LOCALIZED) {
-
- if (!cpumask_test_cpu(cur_cpu,
- &primary->alloced_cpus_in_node)) {
- cpumask_set_cpu(cur_cpu,
- &primary->alloced_cpus_in_node);
- cpumask_set_cpu(cur_cpu, alloced_mask);
- break;
- }
- } else {
- cpumask_set_cpu(cur_cpu, alloced_mask);
- break;
- }
- }
- channel->target_cpu = cur_cpu;
- channel->target_vp = hv_context.vp_index[cur_cpu];
- }
- static void vmbus_wait_for_unload(void)
- {
- int cpu;
- void *page_addr;
- struct hv_message *msg;
- struct vmbus_channel_message_header *hdr;
- u32 message_type;
-
- while (1) {
- if (completion_done(&vmbus_connection.unload_event))
- break;
- for_each_online_cpu(cpu) {
- page_addr = hv_context.synic_message_page[cpu];
- msg = (struct hv_message *)page_addr +
- VMBUS_MESSAGE_SINT;
- message_type = READ_ONCE(msg->header.message_type);
- if (message_type == HVMSG_NONE)
- continue;
- hdr = (struct vmbus_channel_message_header *)
- msg->u.payload;
- if (hdr->msgtype == CHANNELMSG_UNLOAD_RESPONSE)
- complete(&vmbus_connection.unload_event);
- vmbus_signal_eom(msg, message_type);
- }
- mdelay(10);
- }
-
- for_each_online_cpu(cpu) {
- page_addr = hv_context.synic_message_page[cpu];
- msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
- msg->header.message_type = HVMSG_NONE;
- }
- }
- static void vmbus_unload_response(struct vmbus_channel_message_header *hdr)
- {
-
- complete(&vmbus_connection.unload_event);
- }
- void vmbus_initiate_unload(bool crash)
- {
- struct vmbus_channel_message_header hdr;
-
- if (vmbus_proto_version < VERSION_WIN8_1)
- return;
- init_completion(&vmbus_connection.unload_event);
- memset(&hdr, 0, sizeof(struct vmbus_channel_message_header));
- hdr.msgtype = CHANNELMSG_UNLOAD;
- vmbus_post_msg(&hdr, sizeof(struct vmbus_channel_message_header),
- !crash);
-
- if (!crash)
- wait_for_completion(&vmbus_connection.unload_event);
- else
- vmbus_wait_for_unload();
- }
- 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;
-
- newchannel = alloc_channel();
- if (!newchannel) {
- vmbus_release_relid(offer->child_relid);
- pr_err("Unable to allocate channel object\n");
- return;
- }
-
- newchannel->batched_reading = true;
-
- 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;
- }
- memcpy(&newchannel->offermsg, offer,
- sizeof(struct vmbus_channel_offer_channel));
- newchannel->monitor_grp = (u8)offer->monitorid / 32;
- newchannel->monitor_bit = (u8)offer->monitorid % 32;
- vmbus_process_offer(newchannel);
- }
- static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
- {
- struct vmbus_channel_rescind_offer *rescind;
- struct vmbus_channel *channel;
- unsigned long flags;
- struct device *dev;
- rescind = (struct vmbus_channel_rescind_offer *)hdr;
- mutex_lock(&vmbus_connection.channel_mutex);
- channel = relid2channel(rescind->child_relid);
- if (channel == NULL) {
-
- goto out;
- }
- spin_lock_irqsave(&channel->lock, flags);
- channel->rescind = true;
- spin_unlock_irqrestore(&channel->lock, flags);
- vmbus_rescind_cleanup(channel);
- if (channel->device_obj) {
- if (channel->chn_rescind_callback) {
- channel->chn_rescind_callback(channel);
- goto out;
- }
-
- dev = get_device(&channel->device_obj->device);
- if (dev) {
- vmbus_device_unregister(channel->device_obj);
- put_device(dev);
- }
- } else {
- hv_process_channel_removal(channel,
- channel->offermsg.child_relid);
- }
- out:
- mutex_unlock(&vmbus_connection.channel_mutex);
- }
- void vmbus_hvsock_device_unregister(struct vmbus_channel *channel)
- {
- mutex_lock(&vmbus_connection.channel_mutex);
- BUG_ON(!is_hvsock_channel(channel));
- channel->rescind = true;
- vmbus_device_unregister(channel->device_obj);
- mutex_unlock(&vmbus_connection.channel_mutex);
- }
- EXPORT_SYMBOL_GPL(vmbus_hvsock_device_unregister);
- static void vmbus_onoffers_delivered(
- struct vmbus_channel_message_header *hdr)
- {
- }
- 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;
-
- 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);
- }
- 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;
-
- 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);
- }
- 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;
-
- 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);
- }
- 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);
- }
- struct vmbus_channel_message_table_entry
- channel_message_table[CHANNELMSG_COUNT] = {
- {CHANNELMSG_INVALID, 0, NULL},
- {CHANNELMSG_OFFERCHANNEL, 0, vmbus_onoffer},
- {CHANNELMSG_RESCIND_CHANNELOFFER, 0, vmbus_onoffer_rescind},
- {CHANNELMSG_REQUESTOFFERS, 0, NULL},
- {CHANNELMSG_ALLOFFERS_DELIVERED, 1, vmbus_onoffers_delivered},
- {CHANNELMSG_OPENCHANNEL, 0, NULL},
- {CHANNELMSG_OPENCHANNEL_RESULT, 1, vmbus_onopen_result},
- {CHANNELMSG_CLOSECHANNEL, 0, NULL},
- {CHANNELMSG_GPADL_HEADER, 0, NULL},
- {CHANNELMSG_GPADL_BODY, 0, NULL},
- {CHANNELMSG_GPADL_CREATED, 1, vmbus_ongpadl_created},
- {CHANNELMSG_GPADL_TEARDOWN, 0, NULL},
- {CHANNELMSG_GPADL_TORNDOWN, 1, vmbus_ongpadl_torndown},
- {CHANNELMSG_RELID_RELEASED, 0, NULL},
- {CHANNELMSG_INITIATE_CONTACT, 0, NULL},
- {CHANNELMSG_VERSION_RESPONSE, 1, vmbus_onversion_response},
- {CHANNELMSG_UNLOAD, 0, NULL},
- {CHANNELMSG_UNLOAD_RESPONSE, 1, vmbus_unload_response},
- {CHANNELMSG_18, 0, NULL},
- {CHANNELMSG_19, 0, NULL},
- {CHANNELMSG_20, 0, NULL},
- {CHANNELMSG_TL_CONNECT_REQUEST, 0, NULL},
- };
- 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);
- }
- int vmbus_request_offers(void)
- {
- struct vmbus_channel_message_header *msg;
- struct vmbus_channel_msginfo *msginfo;
- int ret;
- msginfo = kmalloc(sizeof(*msginfo) +
- sizeof(struct vmbus_channel_message_header),
- GFP_KERNEL);
- if (!msginfo)
- return -ENOMEM;
- msg = (struct vmbus_channel_message_header *)msginfo->msg;
- msg->msgtype = CHANNELMSG_REQUESTOFFERS;
- ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_message_header),
- true);
- if (ret != 0) {
- pr_err("Unable to request offers - %d\n", ret);
- goto cleanup;
- }
- cleanup:
- kfree(msginfo);
- return ret;
- }
- struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary)
- {
- struct list_head *cur, *tmp;
- int cur_cpu;
- struct vmbus_channel *cur_channel;
- struct vmbus_channel *outgoing_channel = primary;
- int next_channel;
- int i = 1;
- if (list_empty(&primary->sc_list))
- return outgoing_channel;
- next_channel = primary->next_oc++;
- if (next_channel > (primary->num_sc)) {
- primary->next_oc = 0;
- return outgoing_channel;
- }
- cur_cpu = hv_context.vp_index[get_cpu()];
- put_cpu();
- list_for_each_safe(cur, tmp, &primary->sc_list) {
- cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
- if (cur_channel->state != CHANNEL_OPENED_STATE)
- continue;
- if (cur_channel->target_vp == cur_cpu)
- return cur_channel;
- if (i == next_channel)
- return cur_channel;
- i++;
- }
- return outgoing_channel;
- }
- EXPORT_SYMBOL_GPL(vmbus_get_outgoing_channel);
- static void invoke_sc_cb(struct vmbus_channel *primary_channel)
- {
- struct list_head *cur, *tmp;
- struct vmbus_channel *cur_channel;
- if (primary_channel->sc_creation_callback == NULL)
- return;
- list_for_each_safe(cur, tmp, &primary_channel->sc_list) {
- cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
- primary_channel->sc_creation_callback(cur_channel);
- }
- }
- void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
- void (*sc_cr_cb)(struct vmbus_channel *new_sc))
- {
- primary_channel->sc_creation_callback = sc_cr_cb;
- }
- EXPORT_SYMBOL_GPL(vmbus_set_sc_create_callback);
- bool vmbus_are_subchannels_present(struct vmbus_channel *primary)
- {
- bool ret;
- ret = !list_empty(&primary->sc_list);
- if (ret) {
-
- invoke_sc_cb(primary);
- }
- return ret;
- }
- EXPORT_SYMBOL_GPL(vmbus_are_subchannels_present);
- void vmbus_set_chn_rescind_callback(struct vmbus_channel *channel,
- void (*chn_rescind_cb)(struct vmbus_channel *))
- {
- channel->chn_rescind_callback = chn_rescind_cb;
- }
- EXPORT_SYMBOL_GPL(vmbus_set_chn_rescind_callback);
|