channel_mgmt.c 36 KB

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