channel_mgmt.c 31 KB

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