channel_mgmt.c 32 KB

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