channel_mgmt.c 33 KB

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