channel_mgmt.c 32 KB

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