channel_mgmt.c 37 KB

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