channel_mgmt.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. /*
  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>
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <linux/kernel.h>
  23. #include <linux/sched.h>
  24. #include <linux/wait.h>
  25. #include <linux/mm.h>
  26. #include <linux/slab.h>
  27. #include <linux/list.h>
  28. #include <linux/module.h>
  29. #include <linux/completion.h>
  30. #include <linux/delay.h>
  31. #include <linux/hyperv.h>
  32. #include "hyperv_vmbus.h"
  33. static void init_vp_index(struct vmbus_channel *channel, u16 dev_type);
  34. static const struct vmbus_device vmbus_devs[] = {
  35. /* IDE */
  36. { .dev_type = HV_IDE,
  37. HV_IDE_GUID,
  38. .perf_device = true,
  39. },
  40. /* SCSI */
  41. { .dev_type = HV_SCSI,
  42. HV_SCSI_GUID,
  43. .perf_device = true,
  44. },
  45. /* Fibre Channel */
  46. { .dev_type = HV_FC,
  47. HV_SYNTHFC_GUID,
  48. .perf_device = true,
  49. },
  50. /* Synthetic NIC */
  51. { .dev_type = HV_NIC,
  52. HV_NIC_GUID,
  53. .perf_device = true,
  54. },
  55. /* Network Direct */
  56. { .dev_type = HV_ND,
  57. HV_ND_GUID,
  58. .perf_device = true,
  59. },
  60. /* PCIE */
  61. { .dev_type = HV_PCIE,
  62. HV_PCIE_GUID,
  63. .perf_device = true,
  64. },
  65. /* Synthetic Frame Buffer */
  66. { .dev_type = HV_FB,
  67. HV_SYNTHVID_GUID,
  68. .perf_device = false,
  69. },
  70. /* Synthetic Keyboard */
  71. { .dev_type = HV_KBD,
  72. HV_KBD_GUID,
  73. .perf_device = false,
  74. },
  75. /* Synthetic MOUSE */
  76. { .dev_type = HV_MOUSE,
  77. HV_MOUSE_GUID,
  78. .perf_device = false,
  79. },
  80. /* KVP */
  81. { .dev_type = HV_KVP,
  82. HV_KVP_GUID,
  83. .perf_device = false,
  84. },
  85. /* Time Synch */
  86. { .dev_type = HV_TS,
  87. HV_TS_GUID,
  88. .perf_device = false,
  89. },
  90. /* Heartbeat */
  91. { .dev_type = HV_HB,
  92. HV_HEART_BEAT_GUID,
  93. .perf_device = false,
  94. },
  95. /* Shutdown */
  96. { .dev_type = HV_SHUTDOWN,
  97. HV_SHUTDOWN_GUID,
  98. .perf_device = false,
  99. },
  100. /* File copy */
  101. { .dev_type = HV_FCOPY,
  102. HV_FCOPY_GUID,
  103. .perf_device = false,
  104. },
  105. /* Backup */
  106. { .dev_type = HV_BACKUP,
  107. HV_VSS_GUID,
  108. .perf_device = false,
  109. },
  110. /* Dynamic Memory */
  111. { .dev_type = HV_DM,
  112. HV_DM_GUID,
  113. .perf_device = false,
  114. },
  115. /* Unknown GUID */
  116. { .dev_type = HV_UNKOWN,
  117. .perf_device = false,
  118. },
  119. };
  120. static u16 hv_get_dev_type(const uuid_le *guid)
  121. {
  122. u16 i;
  123. for (i = HV_IDE; i < HV_UNKOWN; i++) {
  124. if (!uuid_le_cmp(*guid, vmbus_devs[i].guid))
  125. return i;
  126. }
  127. pr_info("Unknown GUID: %pUl\n", guid);
  128. return i;
  129. }
  130. /**
  131. * vmbus_prep_negotiate_resp() - Create default response for Hyper-V Negotiate message
  132. * @icmsghdrp: Pointer to msg header structure
  133. * @icmsg_negotiate: Pointer to negotiate message structure
  134. * @buf: Raw buffer channel data
  135. *
  136. * @icmsghdrp is of type &struct icmsg_hdr.
  137. * @negop is of type &struct icmsg_negotiate.
  138. * Set up and fill in default negotiate response message.
  139. *
  140. * The fw_version specifies the framework version that
  141. * we can support and srv_version specifies the service
  142. * version we can support.
  143. *
  144. * Mainly used by Hyper-V drivers.
  145. */
  146. bool vmbus_prep_negotiate_resp(struct icmsg_hdr *icmsghdrp,
  147. struct icmsg_negotiate *negop, u8 *buf,
  148. int fw_version, int srv_version)
  149. {
  150. int icframe_major, icframe_minor;
  151. int icmsg_major, icmsg_minor;
  152. int fw_major, fw_minor;
  153. int srv_major, srv_minor;
  154. int i;
  155. bool found_match = false;
  156. icmsghdrp->icmsgsize = 0x10;
  157. fw_major = (fw_version >> 16);
  158. fw_minor = (fw_version & 0xFFFF);
  159. srv_major = (srv_version >> 16);
  160. srv_minor = (srv_version & 0xFFFF);
  161. negop = (struct icmsg_negotiate *)&buf[
  162. sizeof(struct vmbuspipe_hdr) +
  163. sizeof(struct icmsg_hdr)];
  164. icframe_major = negop->icframe_vercnt;
  165. icframe_minor = 0;
  166. icmsg_major = negop->icmsg_vercnt;
  167. icmsg_minor = 0;
  168. /*
  169. * Select the framework version number we will
  170. * support.
  171. */
  172. for (i = 0; i < negop->icframe_vercnt; i++) {
  173. if ((negop->icversion_data[i].major == fw_major) &&
  174. (negop->icversion_data[i].minor == fw_minor)) {
  175. icframe_major = negop->icversion_data[i].major;
  176. icframe_minor = negop->icversion_data[i].minor;
  177. found_match = true;
  178. }
  179. }
  180. if (!found_match)
  181. goto fw_error;
  182. found_match = false;
  183. for (i = negop->icframe_vercnt;
  184. (i < negop->icframe_vercnt + negop->icmsg_vercnt); i++) {
  185. if ((negop->icversion_data[i].major == srv_major) &&
  186. (negop->icversion_data[i].minor == srv_minor)) {
  187. icmsg_major = negop->icversion_data[i].major;
  188. icmsg_minor = negop->icversion_data[i].minor;
  189. found_match = true;
  190. }
  191. }
  192. /*
  193. * Respond with the framework and service
  194. * version numbers we can support.
  195. */
  196. fw_error:
  197. if (!found_match) {
  198. negop->icframe_vercnt = 0;
  199. negop->icmsg_vercnt = 0;
  200. } else {
  201. negop->icframe_vercnt = 1;
  202. negop->icmsg_vercnt = 1;
  203. }
  204. negop->icversion_data[0].major = icframe_major;
  205. negop->icversion_data[0].minor = icframe_minor;
  206. negop->icversion_data[1].major = icmsg_major;
  207. negop->icversion_data[1].minor = icmsg_minor;
  208. return found_match;
  209. }
  210. EXPORT_SYMBOL_GPL(vmbus_prep_negotiate_resp);
  211. /*
  212. * alloc_channel - Allocate and initialize a vmbus channel object
  213. */
  214. static struct vmbus_channel *alloc_channel(void)
  215. {
  216. static atomic_t chan_num = ATOMIC_INIT(0);
  217. struct vmbus_channel *channel;
  218. channel = kzalloc(sizeof(*channel), GFP_ATOMIC);
  219. if (!channel)
  220. return NULL;
  221. channel->id = atomic_inc_return(&chan_num);
  222. channel->acquire_ring_lock = true;
  223. spin_lock_init(&channel->inbound_lock);
  224. spin_lock_init(&channel->lock);
  225. INIT_LIST_HEAD(&channel->sc_list);
  226. INIT_LIST_HEAD(&channel->percpu_list);
  227. return channel;
  228. }
  229. /*
  230. * free_channel - Release the resources used by the vmbus channel object
  231. */
  232. static void free_channel(struct vmbus_channel *channel)
  233. {
  234. kfree(channel);
  235. }
  236. static void percpu_channel_enq(void *arg)
  237. {
  238. struct vmbus_channel *channel = arg;
  239. int cpu = smp_processor_id();
  240. list_add_tail(&channel->percpu_list, &hv_context.percpu_list[cpu]);
  241. }
  242. static void percpu_channel_deq(void *arg)
  243. {
  244. struct vmbus_channel *channel = arg;
  245. list_del(&channel->percpu_list);
  246. }
  247. static void vmbus_release_relid(u32 relid)
  248. {
  249. struct vmbus_channel_relid_released msg;
  250. memset(&msg, 0, sizeof(struct vmbus_channel_relid_released));
  251. msg.child_relid = relid;
  252. msg.header.msgtype = CHANNELMSG_RELID_RELEASED;
  253. vmbus_post_msg(&msg, sizeof(struct vmbus_channel_relid_released));
  254. }
  255. void hv_process_channel_removal(struct vmbus_channel *channel, u32 relid)
  256. {
  257. unsigned long flags;
  258. struct vmbus_channel *primary_channel;
  259. vmbus_release_relid(relid);
  260. BUG_ON(!channel->rescind);
  261. BUG_ON(!mutex_is_locked(&vmbus_connection.channel_mutex));
  262. if (channel->target_cpu != get_cpu()) {
  263. put_cpu();
  264. smp_call_function_single(channel->target_cpu,
  265. percpu_channel_deq, channel, true);
  266. } else {
  267. percpu_channel_deq(channel);
  268. put_cpu();
  269. }
  270. if (channel->primary_channel == NULL) {
  271. list_del(&channel->listentry);
  272. primary_channel = channel;
  273. } else {
  274. primary_channel = channel->primary_channel;
  275. spin_lock_irqsave(&primary_channel->lock, flags);
  276. list_del(&channel->sc_list);
  277. primary_channel->num_sc--;
  278. spin_unlock_irqrestore(&primary_channel->lock, flags);
  279. }
  280. /*
  281. * We need to free the bit for init_vp_index() to work in the case
  282. * of sub-channel, when we reload drivers like hv_netvsc.
  283. */
  284. cpumask_clear_cpu(channel->target_cpu,
  285. &primary_channel->alloced_cpus_in_node);
  286. free_channel(channel);
  287. }
  288. void vmbus_free_channels(void)
  289. {
  290. struct vmbus_channel *channel, *tmp;
  291. list_for_each_entry_safe(channel, tmp, &vmbus_connection.chn_list,
  292. listentry) {
  293. /* hv_process_channel_removal() needs this */
  294. channel->rescind = true;
  295. vmbus_device_unregister(channel->device_obj);
  296. }
  297. }
  298. /*
  299. * vmbus_process_offer - Process the offer by creating a channel/device
  300. * associated with this offer
  301. */
  302. static void vmbus_process_offer(struct vmbus_channel *newchannel)
  303. {
  304. struct vmbus_channel *channel;
  305. bool fnew = true;
  306. unsigned long flags;
  307. u16 dev_type;
  308. int ret;
  309. /* Make sure this is a new offer */
  310. mutex_lock(&vmbus_connection.channel_mutex);
  311. list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
  312. if (!uuid_le_cmp(channel->offermsg.offer.if_type,
  313. newchannel->offermsg.offer.if_type) &&
  314. !uuid_le_cmp(channel->offermsg.offer.if_instance,
  315. newchannel->offermsg.offer.if_instance)) {
  316. fnew = false;
  317. break;
  318. }
  319. }
  320. if (fnew)
  321. list_add_tail(&newchannel->listentry,
  322. &vmbus_connection.chn_list);
  323. mutex_unlock(&vmbus_connection.channel_mutex);
  324. if (!fnew) {
  325. /*
  326. * Check to see if this is a sub-channel.
  327. */
  328. if (newchannel->offermsg.offer.sub_channel_index != 0) {
  329. /*
  330. * Process the sub-channel.
  331. */
  332. newchannel->primary_channel = channel;
  333. spin_lock_irqsave(&channel->lock, flags);
  334. list_add_tail(&newchannel->sc_list, &channel->sc_list);
  335. channel->num_sc++;
  336. spin_unlock_irqrestore(&channel->lock, flags);
  337. } else
  338. goto err_free_chan;
  339. }
  340. dev_type = hv_get_dev_type(&newchannel->offermsg.offer.if_type);
  341. init_vp_index(newchannel, dev_type);
  342. if (newchannel->target_cpu != get_cpu()) {
  343. put_cpu();
  344. smp_call_function_single(newchannel->target_cpu,
  345. percpu_channel_enq,
  346. newchannel, true);
  347. } else {
  348. percpu_channel_enq(newchannel);
  349. put_cpu();
  350. }
  351. /*
  352. * This state is used to indicate a successful open
  353. * so that when we do close the channel normally, we
  354. * can cleanup properly
  355. */
  356. newchannel->state = CHANNEL_OPEN_STATE;
  357. if (!fnew) {
  358. if (channel->sc_creation_callback != NULL)
  359. channel->sc_creation_callback(newchannel);
  360. return;
  361. }
  362. /*
  363. * Start the process of binding this offer to the driver
  364. * We need to set the DeviceObject field before calling
  365. * vmbus_child_dev_add()
  366. */
  367. newchannel->device_obj = vmbus_device_create(
  368. &newchannel->offermsg.offer.if_type,
  369. &newchannel->offermsg.offer.if_instance,
  370. newchannel);
  371. if (!newchannel->device_obj)
  372. goto err_deq_chan;
  373. newchannel->device_obj->device_id = dev_type;
  374. /*
  375. * Add the new device to the bus. This will kick off device-driver
  376. * binding which eventually invokes the device driver's AddDevice()
  377. * method.
  378. */
  379. mutex_lock(&vmbus_connection.channel_mutex);
  380. ret = vmbus_device_register(newchannel->device_obj);
  381. mutex_unlock(&vmbus_connection.channel_mutex);
  382. if (ret != 0) {
  383. pr_err("unable to add child device object (relid %d)\n",
  384. newchannel->offermsg.child_relid);
  385. kfree(newchannel->device_obj);
  386. goto err_deq_chan;
  387. }
  388. return;
  389. err_deq_chan:
  390. vmbus_release_relid(newchannel->offermsg.child_relid);
  391. mutex_lock(&vmbus_connection.channel_mutex);
  392. list_del(&newchannel->listentry);
  393. mutex_unlock(&vmbus_connection.channel_mutex);
  394. if (newchannel->target_cpu != get_cpu()) {
  395. put_cpu();
  396. smp_call_function_single(newchannel->target_cpu,
  397. percpu_channel_deq, newchannel, true);
  398. } else {
  399. percpu_channel_deq(newchannel);
  400. put_cpu();
  401. }
  402. err_free_chan:
  403. free_channel(newchannel);
  404. }
  405. /*
  406. * We use this state to statically distribute the channel interrupt load.
  407. */
  408. static int next_numa_node_id;
  409. /*
  410. * Starting with Win8, we can statically distribute the incoming
  411. * channel interrupt load by binding a channel to VCPU.
  412. * We do this in a hierarchical fashion:
  413. * First distribute the primary channels across available NUMA nodes
  414. * and then distribute the subchannels amongst the CPUs in the NUMA
  415. * node assigned to the primary channel.
  416. *
  417. * For pre-win8 hosts or non-performance critical channels we assign the
  418. * first CPU in the first NUMA node.
  419. */
  420. static void init_vp_index(struct vmbus_channel *channel, u16 dev_type)
  421. {
  422. u32 cur_cpu;
  423. bool perf_chn = vmbus_devs[dev_type].perf_device;
  424. struct vmbus_channel *primary = channel->primary_channel;
  425. int next_node;
  426. struct cpumask available_mask;
  427. struct cpumask *alloced_mask;
  428. if ((vmbus_proto_version == VERSION_WS2008) ||
  429. (vmbus_proto_version == VERSION_WIN7) || (!perf_chn)) {
  430. /*
  431. * Prior to win8, all channel interrupts are
  432. * delivered on cpu 0.
  433. * Also if the channel is not a performance critical
  434. * channel, bind it to cpu 0.
  435. */
  436. channel->numa_node = 0;
  437. channel->target_cpu = 0;
  438. channel->target_vp = hv_context.vp_index[0];
  439. return;
  440. }
  441. /*
  442. * We distribute primary channels evenly across all the available
  443. * NUMA nodes and within the assigned NUMA node we will assign the
  444. * first available CPU to the primary channel.
  445. * The sub-channels will be assigned to the CPUs available in the
  446. * NUMA node evenly.
  447. */
  448. if (!primary) {
  449. while (true) {
  450. next_node = next_numa_node_id++;
  451. if (next_node == nr_node_ids)
  452. next_node = next_numa_node_id = 0;
  453. if (cpumask_empty(cpumask_of_node(next_node)))
  454. continue;
  455. break;
  456. }
  457. channel->numa_node = next_node;
  458. primary = channel;
  459. }
  460. alloced_mask = &hv_context.hv_numa_map[primary->numa_node];
  461. if (cpumask_weight(alloced_mask) ==
  462. cpumask_weight(cpumask_of_node(primary->numa_node))) {
  463. /*
  464. * We have cycled through all the CPUs in the node;
  465. * reset the alloced map.
  466. */
  467. cpumask_clear(alloced_mask);
  468. }
  469. cpumask_xor(&available_mask, alloced_mask,
  470. cpumask_of_node(primary->numa_node));
  471. cur_cpu = -1;
  472. /*
  473. * Normally Hyper-V host doesn't create more subchannels than there
  474. * are VCPUs on the node but it is possible when not all present VCPUs
  475. * on the node are initialized by guest. Clear the alloced_cpus_in_node
  476. * to start over.
  477. */
  478. if (cpumask_equal(&primary->alloced_cpus_in_node,
  479. cpumask_of_node(primary->numa_node)))
  480. cpumask_clear(&primary->alloced_cpus_in_node);
  481. while (true) {
  482. cur_cpu = cpumask_next(cur_cpu, &available_mask);
  483. if (cur_cpu >= nr_cpu_ids) {
  484. cur_cpu = -1;
  485. cpumask_copy(&available_mask,
  486. cpumask_of_node(primary->numa_node));
  487. continue;
  488. }
  489. /*
  490. * NOTE: in the case of sub-channel, we clear the sub-channel
  491. * related bit(s) in primary->alloced_cpus_in_node in
  492. * hv_process_channel_removal(), so when we reload drivers
  493. * like hv_netvsc in SMP guest, here we're able to re-allocate
  494. * bit from primary->alloced_cpus_in_node.
  495. */
  496. if (!cpumask_test_cpu(cur_cpu,
  497. &primary->alloced_cpus_in_node)) {
  498. cpumask_set_cpu(cur_cpu,
  499. &primary->alloced_cpus_in_node);
  500. cpumask_set_cpu(cur_cpu, alloced_mask);
  501. break;
  502. }
  503. }
  504. channel->target_cpu = cur_cpu;
  505. channel->target_vp = hv_context.vp_index[cur_cpu];
  506. }
  507. static void vmbus_wait_for_unload(void)
  508. {
  509. int cpu = smp_processor_id();
  510. void *page_addr = hv_context.synic_message_page[cpu];
  511. struct hv_message *msg = (struct hv_message *)page_addr +
  512. VMBUS_MESSAGE_SINT;
  513. struct vmbus_channel_message_header *hdr;
  514. bool unloaded = false;
  515. while (1) {
  516. if (READ_ONCE(msg->header.message_type) == HVMSG_NONE) {
  517. mdelay(10);
  518. continue;
  519. }
  520. hdr = (struct vmbus_channel_message_header *)msg->u.payload;
  521. if (hdr->msgtype == CHANNELMSG_UNLOAD_RESPONSE)
  522. unloaded = true;
  523. vmbus_signal_eom(msg);
  524. if (unloaded)
  525. break;
  526. }
  527. }
  528. /*
  529. * vmbus_unload_response - Handler for the unload response.
  530. */
  531. static void vmbus_unload_response(struct vmbus_channel_message_header *hdr)
  532. {
  533. /*
  534. * This is a global event; just wakeup the waiting thread.
  535. * Once we successfully unload, we can cleanup the monitor state.
  536. */
  537. complete(&vmbus_connection.unload_event);
  538. }
  539. void vmbus_initiate_unload(bool crash)
  540. {
  541. struct vmbus_channel_message_header hdr;
  542. /* Pre-Win2012R2 hosts don't support reconnect */
  543. if (vmbus_proto_version < VERSION_WIN8_1)
  544. return;
  545. init_completion(&vmbus_connection.unload_event);
  546. memset(&hdr, 0, sizeof(struct vmbus_channel_message_header));
  547. hdr.msgtype = CHANNELMSG_UNLOAD;
  548. vmbus_post_msg(&hdr, sizeof(struct vmbus_channel_message_header));
  549. /*
  550. * vmbus_initiate_unload() is also called on crash and the crash can be
  551. * happening in an interrupt context, where scheduling is impossible.
  552. */
  553. if (!crash)
  554. wait_for_completion(&vmbus_connection.unload_event);
  555. else
  556. vmbus_wait_for_unload();
  557. }
  558. /*
  559. * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
  560. *
  561. */
  562. static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
  563. {
  564. struct vmbus_channel_offer_channel *offer;
  565. struct vmbus_channel *newchannel;
  566. offer = (struct vmbus_channel_offer_channel *)hdr;
  567. /* Allocate the channel object and save this offer. */
  568. newchannel = alloc_channel();
  569. if (!newchannel) {
  570. pr_err("Unable to allocate channel object\n");
  571. return;
  572. }
  573. /*
  574. * By default we setup state to enable batched
  575. * reading. A specific service can choose to
  576. * disable this prior to opening the channel.
  577. */
  578. newchannel->batched_reading = true;
  579. /*
  580. * Setup state for signalling the host.
  581. */
  582. newchannel->sig_event = (struct hv_input_signal_event *)
  583. (ALIGN((unsigned long)
  584. &newchannel->sig_buf,
  585. HV_HYPERCALL_PARAM_ALIGN));
  586. newchannel->sig_event->connectionid.asu32 = 0;
  587. newchannel->sig_event->connectionid.u.id = VMBUS_EVENT_CONNECTION_ID;
  588. newchannel->sig_event->flag_number = 0;
  589. newchannel->sig_event->rsvdz = 0;
  590. if (vmbus_proto_version != VERSION_WS2008) {
  591. newchannel->is_dedicated_interrupt =
  592. (offer->is_dedicated_interrupt != 0);
  593. newchannel->sig_event->connectionid.u.id =
  594. offer->connection_id;
  595. }
  596. memcpy(&newchannel->offermsg, offer,
  597. sizeof(struct vmbus_channel_offer_channel));
  598. newchannel->monitor_grp = (u8)offer->monitorid / 32;
  599. newchannel->monitor_bit = (u8)offer->monitorid % 32;
  600. vmbus_process_offer(newchannel);
  601. }
  602. /*
  603. * vmbus_onoffer_rescind - Rescind offer handler.
  604. *
  605. * We queue a work item to process this offer synchronously
  606. */
  607. static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
  608. {
  609. struct vmbus_channel_rescind_offer *rescind;
  610. struct vmbus_channel *channel;
  611. unsigned long flags;
  612. struct device *dev;
  613. rescind = (struct vmbus_channel_rescind_offer *)hdr;
  614. mutex_lock(&vmbus_connection.channel_mutex);
  615. channel = relid2channel(rescind->child_relid);
  616. if (channel == NULL) {
  617. /*
  618. * This is very impossible, because in
  619. * vmbus_process_offer(), we have already invoked
  620. * vmbus_release_relid() on error.
  621. */
  622. goto out;
  623. }
  624. spin_lock_irqsave(&channel->lock, flags);
  625. channel->rescind = true;
  626. spin_unlock_irqrestore(&channel->lock, flags);
  627. if (channel->device_obj) {
  628. if (channel->chn_rescind_callback) {
  629. channel->chn_rescind_callback(channel);
  630. goto out;
  631. }
  632. /*
  633. * We will have to unregister this device from the
  634. * driver core.
  635. */
  636. dev = get_device(&channel->device_obj->device);
  637. if (dev) {
  638. vmbus_device_unregister(channel->device_obj);
  639. put_device(dev);
  640. }
  641. } else {
  642. hv_process_channel_removal(channel,
  643. channel->offermsg.child_relid);
  644. }
  645. out:
  646. mutex_unlock(&vmbus_connection.channel_mutex);
  647. }
  648. void vmbus_hvsock_device_unregister(struct vmbus_channel *channel)
  649. {
  650. mutex_lock(&vmbus_connection.channel_mutex);
  651. BUG_ON(!is_hvsock_channel(channel));
  652. channel->rescind = true;
  653. vmbus_device_unregister(channel->device_obj);
  654. mutex_unlock(&vmbus_connection.channel_mutex);
  655. }
  656. EXPORT_SYMBOL_GPL(vmbus_hvsock_device_unregister);
  657. /*
  658. * vmbus_onoffers_delivered -
  659. * This is invoked when all offers have been delivered.
  660. *
  661. * Nothing to do here.
  662. */
  663. static void vmbus_onoffers_delivered(
  664. struct vmbus_channel_message_header *hdr)
  665. {
  666. }
  667. /*
  668. * vmbus_onopen_result - Open result handler.
  669. *
  670. * This is invoked when we received a response to our channel open request.
  671. * Find the matching request, copy the response and signal the requesting
  672. * thread.
  673. */
  674. static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
  675. {
  676. struct vmbus_channel_open_result *result;
  677. struct vmbus_channel_msginfo *msginfo;
  678. struct vmbus_channel_message_header *requestheader;
  679. struct vmbus_channel_open_channel *openmsg;
  680. unsigned long flags;
  681. result = (struct vmbus_channel_open_result *)hdr;
  682. /*
  683. * Find the open msg, copy the result and signal/unblock the wait event
  684. */
  685. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  686. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  687. msglistentry) {
  688. requestheader =
  689. (struct vmbus_channel_message_header *)msginfo->msg;
  690. if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
  691. openmsg =
  692. (struct vmbus_channel_open_channel *)msginfo->msg;
  693. if (openmsg->child_relid == result->child_relid &&
  694. openmsg->openid == result->openid) {
  695. memcpy(&msginfo->response.open_result,
  696. result,
  697. sizeof(
  698. struct vmbus_channel_open_result));
  699. complete(&msginfo->waitevent);
  700. break;
  701. }
  702. }
  703. }
  704. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  705. }
  706. /*
  707. * vmbus_ongpadl_created - GPADL created handler.
  708. *
  709. * This is invoked when we received a response to our gpadl create request.
  710. * Find the matching request, copy the response and signal the requesting
  711. * thread.
  712. */
  713. static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
  714. {
  715. struct vmbus_channel_gpadl_created *gpadlcreated;
  716. struct vmbus_channel_msginfo *msginfo;
  717. struct vmbus_channel_message_header *requestheader;
  718. struct vmbus_channel_gpadl_header *gpadlheader;
  719. unsigned long flags;
  720. gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
  721. /*
  722. * Find the establish msg, copy the result and signal/unblock the wait
  723. * event
  724. */
  725. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  726. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  727. msglistentry) {
  728. requestheader =
  729. (struct vmbus_channel_message_header *)msginfo->msg;
  730. if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
  731. gpadlheader =
  732. (struct vmbus_channel_gpadl_header *)requestheader;
  733. if ((gpadlcreated->child_relid ==
  734. gpadlheader->child_relid) &&
  735. (gpadlcreated->gpadl == gpadlheader->gpadl)) {
  736. memcpy(&msginfo->response.gpadl_created,
  737. gpadlcreated,
  738. sizeof(
  739. struct vmbus_channel_gpadl_created));
  740. complete(&msginfo->waitevent);
  741. break;
  742. }
  743. }
  744. }
  745. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  746. }
  747. /*
  748. * vmbus_ongpadl_torndown - GPADL torndown handler.
  749. *
  750. * This is invoked when we received a response to our gpadl teardown request.
  751. * Find the matching request, copy the response and signal the requesting
  752. * thread.
  753. */
  754. static void vmbus_ongpadl_torndown(
  755. struct vmbus_channel_message_header *hdr)
  756. {
  757. struct vmbus_channel_gpadl_torndown *gpadl_torndown;
  758. struct vmbus_channel_msginfo *msginfo;
  759. struct vmbus_channel_message_header *requestheader;
  760. struct vmbus_channel_gpadl_teardown *gpadl_teardown;
  761. unsigned long flags;
  762. gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
  763. /*
  764. * Find the open msg, copy the result and signal/unblock the wait event
  765. */
  766. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  767. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  768. msglistentry) {
  769. requestheader =
  770. (struct vmbus_channel_message_header *)msginfo->msg;
  771. if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
  772. gpadl_teardown =
  773. (struct vmbus_channel_gpadl_teardown *)requestheader;
  774. if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
  775. memcpy(&msginfo->response.gpadl_torndown,
  776. gpadl_torndown,
  777. sizeof(
  778. struct vmbus_channel_gpadl_torndown));
  779. complete(&msginfo->waitevent);
  780. break;
  781. }
  782. }
  783. }
  784. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  785. }
  786. /*
  787. * vmbus_onversion_response - Version response handler
  788. *
  789. * This is invoked when we received a response to our initiate contact request.
  790. * Find the matching request, copy the response and signal the requesting
  791. * thread.
  792. */
  793. static void vmbus_onversion_response(
  794. struct vmbus_channel_message_header *hdr)
  795. {
  796. struct vmbus_channel_msginfo *msginfo;
  797. struct vmbus_channel_message_header *requestheader;
  798. struct vmbus_channel_version_response *version_response;
  799. unsigned long flags;
  800. version_response = (struct vmbus_channel_version_response *)hdr;
  801. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  802. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  803. msglistentry) {
  804. requestheader =
  805. (struct vmbus_channel_message_header *)msginfo->msg;
  806. if (requestheader->msgtype ==
  807. CHANNELMSG_INITIATE_CONTACT) {
  808. memcpy(&msginfo->response.version_response,
  809. version_response,
  810. sizeof(struct vmbus_channel_version_response));
  811. complete(&msginfo->waitevent);
  812. }
  813. }
  814. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  815. }
  816. /* Channel message dispatch table */
  817. struct vmbus_channel_message_table_entry
  818. channel_message_table[CHANNELMSG_COUNT] = {
  819. {CHANNELMSG_INVALID, 0, NULL},
  820. {CHANNELMSG_OFFERCHANNEL, 0, vmbus_onoffer},
  821. {CHANNELMSG_RESCIND_CHANNELOFFER, 0, vmbus_onoffer_rescind},
  822. {CHANNELMSG_REQUESTOFFERS, 0, NULL},
  823. {CHANNELMSG_ALLOFFERS_DELIVERED, 1, vmbus_onoffers_delivered},
  824. {CHANNELMSG_OPENCHANNEL, 0, NULL},
  825. {CHANNELMSG_OPENCHANNEL_RESULT, 1, vmbus_onopen_result},
  826. {CHANNELMSG_CLOSECHANNEL, 0, NULL},
  827. {CHANNELMSG_GPADL_HEADER, 0, NULL},
  828. {CHANNELMSG_GPADL_BODY, 0, NULL},
  829. {CHANNELMSG_GPADL_CREATED, 1, vmbus_ongpadl_created},
  830. {CHANNELMSG_GPADL_TEARDOWN, 0, NULL},
  831. {CHANNELMSG_GPADL_TORNDOWN, 1, vmbus_ongpadl_torndown},
  832. {CHANNELMSG_RELID_RELEASED, 0, NULL},
  833. {CHANNELMSG_INITIATE_CONTACT, 0, NULL},
  834. {CHANNELMSG_VERSION_RESPONSE, 1, vmbus_onversion_response},
  835. {CHANNELMSG_UNLOAD, 0, NULL},
  836. {CHANNELMSG_UNLOAD_RESPONSE, 1, vmbus_unload_response},
  837. {CHANNELMSG_18, 0, NULL},
  838. {CHANNELMSG_19, 0, NULL},
  839. {CHANNELMSG_20, 0, NULL},
  840. {CHANNELMSG_TL_CONNECT_REQUEST, 0, NULL},
  841. };
  842. /*
  843. * vmbus_onmessage - Handler for channel protocol messages.
  844. *
  845. * This is invoked in the vmbus worker thread context.
  846. */
  847. void vmbus_onmessage(void *context)
  848. {
  849. struct hv_message *msg = context;
  850. struct vmbus_channel_message_header *hdr;
  851. int size;
  852. hdr = (struct vmbus_channel_message_header *)msg->u.payload;
  853. size = msg->header.payload_size;
  854. if (hdr->msgtype >= CHANNELMSG_COUNT) {
  855. pr_err("Received invalid channel message type %d size %d\n",
  856. hdr->msgtype, size);
  857. print_hex_dump_bytes("", DUMP_PREFIX_NONE,
  858. (unsigned char *)msg->u.payload, size);
  859. return;
  860. }
  861. if (channel_message_table[hdr->msgtype].message_handler)
  862. channel_message_table[hdr->msgtype].message_handler(hdr);
  863. else
  864. pr_err("Unhandled channel message type %d\n", hdr->msgtype);
  865. }
  866. /*
  867. * vmbus_request_offers - Send a request to get all our pending offers.
  868. */
  869. int vmbus_request_offers(void)
  870. {
  871. struct vmbus_channel_message_header *msg;
  872. struct vmbus_channel_msginfo *msginfo;
  873. int ret;
  874. msginfo = kmalloc(sizeof(*msginfo) +
  875. sizeof(struct vmbus_channel_message_header),
  876. GFP_KERNEL);
  877. if (!msginfo)
  878. return -ENOMEM;
  879. msg = (struct vmbus_channel_message_header *)msginfo->msg;
  880. msg->msgtype = CHANNELMSG_REQUESTOFFERS;
  881. ret = vmbus_post_msg(msg,
  882. sizeof(struct vmbus_channel_message_header));
  883. if (ret != 0) {
  884. pr_err("Unable to request offers - %d\n", ret);
  885. goto cleanup;
  886. }
  887. cleanup:
  888. kfree(msginfo);
  889. return ret;
  890. }
  891. /*
  892. * Retrieve the (sub) channel on which to send an outgoing request.
  893. * When a primary channel has multiple sub-channels, we try to
  894. * distribute the load equally amongst all available channels.
  895. */
  896. struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary)
  897. {
  898. struct list_head *cur, *tmp;
  899. int cur_cpu;
  900. struct vmbus_channel *cur_channel;
  901. struct vmbus_channel *outgoing_channel = primary;
  902. int next_channel;
  903. int i = 1;
  904. if (list_empty(&primary->sc_list))
  905. return outgoing_channel;
  906. next_channel = primary->next_oc++;
  907. if (next_channel > (primary->num_sc)) {
  908. primary->next_oc = 0;
  909. return outgoing_channel;
  910. }
  911. cur_cpu = hv_context.vp_index[get_cpu()];
  912. put_cpu();
  913. list_for_each_safe(cur, tmp, &primary->sc_list) {
  914. cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
  915. if (cur_channel->state != CHANNEL_OPENED_STATE)
  916. continue;
  917. if (cur_channel->target_vp == cur_cpu)
  918. return cur_channel;
  919. if (i == next_channel)
  920. return cur_channel;
  921. i++;
  922. }
  923. return outgoing_channel;
  924. }
  925. EXPORT_SYMBOL_GPL(vmbus_get_outgoing_channel);
  926. static void invoke_sc_cb(struct vmbus_channel *primary_channel)
  927. {
  928. struct list_head *cur, *tmp;
  929. struct vmbus_channel *cur_channel;
  930. if (primary_channel->sc_creation_callback == NULL)
  931. return;
  932. list_for_each_safe(cur, tmp, &primary_channel->sc_list) {
  933. cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
  934. primary_channel->sc_creation_callback(cur_channel);
  935. }
  936. }
  937. void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
  938. void (*sc_cr_cb)(struct vmbus_channel *new_sc))
  939. {
  940. primary_channel->sc_creation_callback = sc_cr_cb;
  941. }
  942. EXPORT_SYMBOL_GPL(vmbus_set_sc_create_callback);
  943. bool vmbus_are_subchannels_present(struct vmbus_channel *primary)
  944. {
  945. bool ret;
  946. ret = !list_empty(&primary->sc_list);
  947. if (ret) {
  948. /*
  949. * Invoke the callback on sub-channel creation.
  950. * This will present a uniform interface to the
  951. * clients.
  952. */
  953. invoke_sc_cb(primary);
  954. }
  955. return ret;
  956. }
  957. EXPORT_SYMBOL_GPL(vmbus_are_subchannels_present);
  958. void vmbus_set_chn_rescind_callback(struct vmbus_channel *channel,
  959. void (*chn_rescind_cb)(struct vmbus_channel *))
  960. {
  961. channel->chn_rescind_callback = chn_rescind_cb;
  962. }
  963. EXPORT_SYMBOL_GPL(vmbus_set_chn_rescind_callback);