channel_mgmt.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  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. #define UNLOAD_DELAY_UNIT_MS 10 /* 10 milliseconds */
  655. #define UNLOAD_WAIT_MS (100*1000) /* 100 seconds */
  656. #define UNLOAD_WAIT_LOOPS (UNLOAD_WAIT_MS/UNLOAD_DELAY_UNIT_MS)
  657. #define UNLOAD_MSG_MS (5*1000) /* Every 5 seconds */
  658. #define UNLOAD_MSG_LOOPS (UNLOAD_MSG_MS/UNLOAD_DELAY_UNIT_MS)
  659. static void vmbus_wait_for_unload(void)
  660. {
  661. int cpu;
  662. void *page_addr;
  663. struct hv_message *msg;
  664. struct vmbus_channel_message_header *hdr;
  665. u32 message_type, i;
  666. /*
  667. * CHANNELMSG_UNLOAD_RESPONSE is always delivered to the CPU which was
  668. * used for initial contact or to CPU0 depending on host version. When
  669. * we're crashing on a different CPU let's hope that IRQ handler on
  670. * the cpu which receives CHANNELMSG_UNLOAD_RESPONSE is still
  671. * functional and vmbus_unload_response() will complete
  672. * vmbus_connection.unload_event. If not, the last thing we can do is
  673. * read message pages for all CPUs directly.
  674. *
  675. * Wait up to 100 seconds since an Azure host must writeback any dirty
  676. * data in its disk cache before the VMbus UNLOAD request will
  677. * complete. This flushing has been empirically observed to take up
  678. * to 50 seconds in cases with a lot of dirty data, so allow additional
  679. * leeway and for inaccuracies in mdelay(). But eventually time out so
  680. * that the panic path can't get hung forever in case the response
  681. * message isn't seen.
  682. */
  683. for (i = 1; i <= UNLOAD_WAIT_LOOPS; i++) {
  684. if (completion_done(&vmbus_connection.unload_event))
  685. goto completed;
  686. for_each_online_cpu(cpu) {
  687. struct hv_per_cpu_context *hv_cpu
  688. = per_cpu_ptr(hv_context.cpu_context, cpu);
  689. page_addr = hv_cpu->synic_message_page;
  690. msg = (struct hv_message *)page_addr
  691. + VMBUS_MESSAGE_SINT;
  692. message_type = READ_ONCE(msg->header.message_type);
  693. if (message_type == HVMSG_NONE)
  694. continue;
  695. hdr = (struct vmbus_channel_message_header *)
  696. msg->u.payload;
  697. if (hdr->msgtype == CHANNELMSG_UNLOAD_RESPONSE)
  698. complete(&vmbus_connection.unload_event);
  699. vmbus_signal_eom(msg, message_type);
  700. }
  701. /*
  702. * Give a notice periodically so someone watching the
  703. * serial output won't think it is completely hung.
  704. */
  705. if (!(i % UNLOAD_MSG_LOOPS))
  706. pr_notice("Waiting for VMBus UNLOAD to complete\n");
  707. mdelay(UNLOAD_DELAY_UNIT_MS);
  708. }
  709. pr_err("Continuing even though VMBus UNLOAD did not complete\n");
  710. completed:
  711. /*
  712. * We're crashing and already got the UNLOAD_RESPONSE, cleanup all
  713. * maybe-pending messages on all CPUs to be able to receive new
  714. * messages after we reconnect.
  715. */
  716. for_each_online_cpu(cpu) {
  717. struct hv_per_cpu_context *hv_cpu
  718. = per_cpu_ptr(hv_context.cpu_context, cpu);
  719. page_addr = hv_cpu->synic_message_page;
  720. msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
  721. msg->header.message_type = HVMSG_NONE;
  722. }
  723. }
  724. /*
  725. * vmbus_unload_response - Handler for the unload response.
  726. */
  727. static void vmbus_unload_response(struct vmbus_channel_message_header *hdr)
  728. {
  729. /*
  730. * This is a global event; just wakeup the waiting thread.
  731. * Once we successfully unload, we can cleanup the monitor state.
  732. */
  733. complete(&vmbus_connection.unload_event);
  734. }
  735. void vmbus_initiate_unload(bool crash)
  736. {
  737. struct vmbus_channel_message_header hdr;
  738. if (xchg(&vmbus_connection.conn_state, DISCONNECTED) == DISCONNECTED)
  739. return;
  740. /* Pre-Win2012R2 hosts don't support reconnect */
  741. if (vmbus_proto_version < VERSION_WIN8_1)
  742. return;
  743. init_completion(&vmbus_connection.unload_event);
  744. memset(&hdr, 0, sizeof(struct vmbus_channel_message_header));
  745. hdr.msgtype = CHANNELMSG_UNLOAD;
  746. vmbus_post_msg(&hdr, sizeof(struct vmbus_channel_message_header),
  747. !crash);
  748. /*
  749. * vmbus_initiate_unload() is also called on crash and the crash can be
  750. * happening in an interrupt context, where scheduling is impossible.
  751. */
  752. if (!crash)
  753. wait_for_completion(&vmbus_connection.unload_event);
  754. else
  755. vmbus_wait_for_unload();
  756. }
  757. /*
  758. * vmbus_onoffer - Handler for channel offers from vmbus in parent partition.
  759. *
  760. */
  761. static void vmbus_onoffer(struct vmbus_channel_message_header *hdr)
  762. {
  763. struct vmbus_channel_offer_channel *offer;
  764. struct vmbus_channel *newchannel;
  765. offer = (struct vmbus_channel_offer_channel *)hdr;
  766. trace_vmbus_onoffer(offer);
  767. /* Allocate the channel object and save this offer. */
  768. newchannel = alloc_channel();
  769. if (!newchannel) {
  770. vmbus_release_relid(offer->child_relid);
  771. atomic_dec(&vmbus_connection.offer_in_progress);
  772. pr_err("Unable to allocate channel object\n");
  773. return;
  774. }
  775. /*
  776. * Setup state for signalling the host.
  777. */
  778. newchannel->sig_event = VMBUS_EVENT_CONNECTION_ID;
  779. if (vmbus_proto_version != VERSION_WS2008) {
  780. newchannel->is_dedicated_interrupt =
  781. (offer->is_dedicated_interrupt != 0);
  782. newchannel->sig_event = offer->connection_id;
  783. }
  784. memcpy(&newchannel->offermsg, offer,
  785. sizeof(struct vmbus_channel_offer_channel));
  786. newchannel->monitor_grp = (u8)offer->monitorid / 32;
  787. newchannel->monitor_bit = (u8)offer->monitorid % 32;
  788. vmbus_process_offer(newchannel);
  789. }
  790. /*
  791. * vmbus_onoffer_rescind - Rescind offer handler.
  792. *
  793. * We queue a work item to process this offer synchronously
  794. */
  795. static void vmbus_onoffer_rescind(struct vmbus_channel_message_header *hdr)
  796. {
  797. struct vmbus_channel_rescind_offer *rescind;
  798. struct vmbus_channel *channel;
  799. struct device *dev;
  800. rescind = (struct vmbus_channel_rescind_offer *)hdr;
  801. trace_vmbus_onoffer_rescind(rescind);
  802. /*
  803. * The offer msg and the corresponding rescind msg
  804. * from the host are guranteed to be ordered -
  805. * offer comes in first and then the rescind.
  806. * Since we process these events in work elements,
  807. * and with preemption, we may end up processing
  808. * the events out of order. Given that we handle these
  809. * work elements on the same CPU, this is possible only
  810. * in the case of preemption. In any case wait here
  811. * until the offer processing has moved beyond the
  812. * point where the channel is discoverable.
  813. */
  814. while (atomic_read(&vmbus_connection.offer_in_progress) != 0) {
  815. /*
  816. * We wait here until any channel offer is currently
  817. * being processed.
  818. */
  819. msleep(1);
  820. }
  821. mutex_lock(&vmbus_connection.channel_mutex);
  822. channel = relid2channel(rescind->child_relid);
  823. mutex_unlock(&vmbus_connection.channel_mutex);
  824. if (channel == NULL) {
  825. /*
  826. * We failed in processing the offer message;
  827. * we would have cleaned up the relid in that
  828. * failure path.
  829. */
  830. return;
  831. }
  832. /*
  833. * Before setting channel->rescind in vmbus_rescind_cleanup(), we
  834. * should make sure the channel callback is not running any more.
  835. */
  836. vmbus_reset_channel_cb(channel);
  837. /*
  838. * Now wait for offer handling to complete.
  839. */
  840. vmbus_rescind_cleanup(channel);
  841. while (READ_ONCE(channel->probe_done) == false) {
  842. /*
  843. * We wait here until any channel offer is currently
  844. * being processed.
  845. */
  846. msleep(1);
  847. }
  848. /*
  849. * At this point, the rescind handling can proceed safely.
  850. */
  851. if (channel->device_obj) {
  852. if (channel->chn_rescind_callback) {
  853. channel->chn_rescind_callback(channel);
  854. return;
  855. }
  856. /*
  857. * We will have to unregister this device from the
  858. * driver core.
  859. */
  860. dev = get_device(&channel->device_obj->device);
  861. if (dev) {
  862. vmbus_device_unregister(channel->device_obj);
  863. put_device(dev);
  864. }
  865. } else if (channel->primary_channel != NULL) {
  866. /*
  867. * Sub-channel is being rescinded. Following is the channel
  868. * close sequence when initiated from the driveri (refer to
  869. * vmbus_close() for details):
  870. * 1. Close all sub-channels first
  871. * 2. Then close the primary channel.
  872. */
  873. mutex_lock(&vmbus_connection.channel_mutex);
  874. if (channel->state == CHANNEL_OPEN_STATE) {
  875. /*
  876. * The channel is currently not open;
  877. * it is safe for us to cleanup the channel.
  878. */
  879. hv_process_channel_removal(rescind->child_relid);
  880. } else {
  881. complete(&channel->rescind_event);
  882. }
  883. mutex_unlock(&vmbus_connection.channel_mutex);
  884. }
  885. }
  886. void vmbus_hvsock_device_unregister(struct vmbus_channel *channel)
  887. {
  888. BUG_ON(!is_hvsock_channel(channel));
  889. /* We always get a rescind msg when a connection is closed. */
  890. while (!READ_ONCE(channel->probe_done) || !READ_ONCE(channel->rescind))
  891. msleep(1);
  892. vmbus_device_unregister(channel->device_obj);
  893. }
  894. EXPORT_SYMBOL_GPL(vmbus_hvsock_device_unregister);
  895. /*
  896. * vmbus_onoffers_delivered -
  897. * This is invoked when all offers have been delivered.
  898. *
  899. * Nothing to do here.
  900. */
  901. static void vmbus_onoffers_delivered(
  902. struct vmbus_channel_message_header *hdr)
  903. {
  904. }
  905. /*
  906. * vmbus_onopen_result - Open result handler.
  907. *
  908. * This is invoked when we received a response to our channel open request.
  909. * Find the matching request, copy the response and signal the requesting
  910. * thread.
  911. */
  912. static void vmbus_onopen_result(struct vmbus_channel_message_header *hdr)
  913. {
  914. struct vmbus_channel_open_result *result;
  915. struct vmbus_channel_msginfo *msginfo;
  916. struct vmbus_channel_message_header *requestheader;
  917. struct vmbus_channel_open_channel *openmsg;
  918. unsigned long flags;
  919. result = (struct vmbus_channel_open_result *)hdr;
  920. trace_vmbus_onopen_result(result);
  921. /*
  922. * Find the open msg, copy the result and signal/unblock the wait event
  923. */
  924. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  925. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  926. msglistentry) {
  927. requestheader =
  928. (struct vmbus_channel_message_header *)msginfo->msg;
  929. if (requestheader->msgtype == CHANNELMSG_OPENCHANNEL) {
  930. openmsg =
  931. (struct vmbus_channel_open_channel *)msginfo->msg;
  932. if (openmsg->child_relid == result->child_relid &&
  933. openmsg->openid == result->openid) {
  934. memcpy(&msginfo->response.open_result,
  935. result,
  936. sizeof(
  937. struct vmbus_channel_open_result));
  938. complete(&msginfo->waitevent);
  939. break;
  940. }
  941. }
  942. }
  943. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  944. }
  945. /*
  946. * vmbus_ongpadl_created - GPADL created handler.
  947. *
  948. * This is invoked when we received a response to our gpadl create request.
  949. * Find the matching request, copy the response and signal the requesting
  950. * thread.
  951. */
  952. static void vmbus_ongpadl_created(struct vmbus_channel_message_header *hdr)
  953. {
  954. struct vmbus_channel_gpadl_created *gpadlcreated;
  955. struct vmbus_channel_msginfo *msginfo;
  956. struct vmbus_channel_message_header *requestheader;
  957. struct vmbus_channel_gpadl_header *gpadlheader;
  958. unsigned long flags;
  959. gpadlcreated = (struct vmbus_channel_gpadl_created *)hdr;
  960. trace_vmbus_ongpadl_created(gpadlcreated);
  961. /*
  962. * Find the establish msg, copy the result and signal/unblock the wait
  963. * event
  964. */
  965. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  966. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  967. msglistentry) {
  968. requestheader =
  969. (struct vmbus_channel_message_header *)msginfo->msg;
  970. if (requestheader->msgtype == CHANNELMSG_GPADL_HEADER) {
  971. gpadlheader =
  972. (struct vmbus_channel_gpadl_header *)requestheader;
  973. if ((gpadlcreated->child_relid ==
  974. gpadlheader->child_relid) &&
  975. (gpadlcreated->gpadl == gpadlheader->gpadl)) {
  976. memcpy(&msginfo->response.gpadl_created,
  977. gpadlcreated,
  978. sizeof(
  979. struct vmbus_channel_gpadl_created));
  980. complete(&msginfo->waitevent);
  981. break;
  982. }
  983. }
  984. }
  985. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  986. }
  987. /*
  988. * vmbus_ongpadl_torndown - GPADL torndown handler.
  989. *
  990. * This is invoked when we received a response to our gpadl teardown request.
  991. * Find the matching request, copy the response and signal the requesting
  992. * thread.
  993. */
  994. static void vmbus_ongpadl_torndown(
  995. struct vmbus_channel_message_header *hdr)
  996. {
  997. struct vmbus_channel_gpadl_torndown *gpadl_torndown;
  998. struct vmbus_channel_msginfo *msginfo;
  999. struct vmbus_channel_message_header *requestheader;
  1000. struct vmbus_channel_gpadl_teardown *gpadl_teardown;
  1001. unsigned long flags;
  1002. gpadl_torndown = (struct vmbus_channel_gpadl_torndown *)hdr;
  1003. trace_vmbus_ongpadl_torndown(gpadl_torndown);
  1004. /*
  1005. * Find the open msg, copy the result and signal/unblock the wait event
  1006. */
  1007. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  1008. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  1009. msglistentry) {
  1010. requestheader =
  1011. (struct vmbus_channel_message_header *)msginfo->msg;
  1012. if (requestheader->msgtype == CHANNELMSG_GPADL_TEARDOWN) {
  1013. gpadl_teardown =
  1014. (struct vmbus_channel_gpadl_teardown *)requestheader;
  1015. if (gpadl_torndown->gpadl == gpadl_teardown->gpadl) {
  1016. memcpy(&msginfo->response.gpadl_torndown,
  1017. gpadl_torndown,
  1018. sizeof(
  1019. struct vmbus_channel_gpadl_torndown));
  1020. complete(&msginfo->waitevent);
  1021. break;
  1022. }
  1023. }
  1024. }
  1025. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  1026. }
  1027. /*
  1028. * vmbus_onversion_response - Version response handler
  1029. *
  1030. * This is invoked when we received a response to our initiate contact request.
  1031. * Find the matching request, copy the response and signal the requesting
  1032. * thread.
  1033. */
  1034. static void vmbus_onversion_response(
  1035. struct vmbus_channel_message_header *hdr)
  1036. {
  1037. struct vmbus_channel_msginfo *msginfo;
  1038. struct vmbus_channel_message_header *requestheader;
  1039. struct vmbus_channel_version_response *version_response;
  1040. unsigned long flags;
  1041. version_response = (struct vmbus_channel_version_response *)hdr;
  1042. trace_vmbus_onversion_response(version_response);
  1043. spin_lock_irqsave(&vmbus_connection.channelmsg_lock, flags);
  1044. list_for_each_entry(msginfo, &vmbus_connection.chn_msg_list,
  1045. msglistentry) {
  1046. requestheader =
  1047. (struct vmbus_channel_message_header *)msginfo->msg;
  1048. if (requestheader->msgtype ==
  1049. CHANNELMSG_INITIATE_CONTACT) {
  1050. memcpy(&msginfo->response.version_response,
  1051. version_response,
  1052. sizeof(struct vmbus_channel_version_response));
  1053. complete(&msginfo->waitevent);
  1054. }
  1055. }
  1056. spin_unlock_irqrestore(&vmbus_connection.channelmsg_lock, flags);
  1057. }
  1058. /* Channel message dispatch table */
  1059. const struct vmbus_channel_message_table_entry
  1060. channel_message_table[CHANNELMSG_COUNT] = {
  1061. { CHANNELMSG_INVALID, 0, NULL },
  1062. { CHANNELMSG_OFFERCHANNEL, 0, vmbus_onoffer },
  1063. { CHANNELMSG_RESCIND_CHANNELOFFER, 0, vmbus_onoffer_rescind },
  1064. { CHANNELMSG_REQUESTOFFERS, 0, NULL },
  1065. { CHANNELMSG_ALLOFFERS_DELIVERED, 1, vmbus_onoffers_delivered },
  1066. { CHANNELMSG_OPENCHANNEL, 0, NULL },
  1067. { CHANNELMSG_OPENCHANNEL_RESULT, 1, vmbus_onopen_result },
  1068. { CHANNELMSG_CLOSECHANNEL, 0, NULL },
  1069. { CHANNELMSG_GPADL_HEADER, 0, NULL },
  1070. { CHANNELMSG_GPADL_BODY, 0, NULL },
  1071. { CHANNELMSG_GPADL_CREATED, 1, vmbus_ongpadl_created },
  1072. { CHANNELMSG_GPADL_TEARDOWN, 0, NULL },
  1073. { CHANNELMSG_GPADL_TORNDOWN, 1, vmbus_ongpadl_torndown },
  1074. { CHANNELMSG_RELID_RELEASED, 0, NULL },
  1075. { CHANNELMSG_INITIATE_CONTACT, 0, NULL },
  1076. { CHANNELMSG_VERSION_RESPONSE, 1, vmbus_onversion_response },
  1077. { CHANNELMSG_UNLOAD, 0, NULL },
  1078. { CHANNELMSG_UNLOAD_RESPONSE, 1, vmbus_unload_response },
  1079. { CHANNELMSG_18, 0, NULL },
  1080. { CHANNELMSG_19, 0, NULL },
  1081. { CHANNELMSG_20, 0, NULL },
  1082. { CHANNELMSG_TL_CONNECT_REQUEST, 0, NULL },
  1083. { CHANNELMSG_22, 0, NULL },
  1084. { CHANNELMSG_TL_CONNECT_RESULT, 0, NULL },
  1085. };
  1086. /*
  1087. * vmbus_onmessage - Handler for channel protocol messages.
  1088. *
  1089. * This is invoked in the vmbus worker thread context.
  1090. */
  1091. void vmbus_onmessage(void *context)
  1092. {
  1093. struct hv_message *msg = context;
  1094. struct vmbus_channel_message_header *hdr;
  1095. hdr = (struct vmbus_channel_message_header *)msg->u.payload;
  1096. trace_vmbus_on_message(hdr);
  1097. /*
  1098. * vmbus_on_msg_dpc() makes sure the hdr->msgtype here can not go
  1099. * out of bound and the message_handler pointer can not be NULL.
  1100. */
  1101. channel_message_table[hdr->msgtype].message_handler(hdr);
  1102. }
  1103. /*
  1104. * vmbus_request_offers - Send a request to get all our pending offers.
  1105. */
  1106. int vmbus_request_offers(void)
  1107. {
  1108. struct vmbus_channel_message_header *msg;
  1109. struct vmbus_channel_msginfo *msginfo;
  1110. int ret;
  1111. msginfo = kmalloc(sizeof(*msginfo) +
  1112. sizeof(struct vmbus_channel_message_header),
  1113. GFP_KERNEL);
  1114. if (!msginfo)
  1115. return -ENOMEM;
  1116. msg = (struct vmbus_channel_message_header *)msginfo->msg;
  1117. msg->msgtype = CHANNELMSG_REQUESTOFFERS;
  1118. ret = vmbus_post_msg(msg, sizeof(struct vmbus_channel_message_header),
  1119. true);
  1120. trace_vmbus_request_offers(ret);
  1121. if (ret != 0) {
  1122. pr_err("Unable to request offers - %d\n", ret);
  1123. goto cleanup;
  1124. }
  1125. cleanup:
  1126. kfree(msginfo);
  1127. return ret;
  1128. }
  1129. /*
  1130. * Retrieve the (sub) channel on which to send an outgoing request.
  1131. * When a primary channel has multiple sub-channels, we try to
  1132. * distribute the load equally amongst all available channels.
  1133. */
  1134. struct vmbus_channel *vmbus_get_outgoing_channel(struct vmbus_channel *primary)
  1135. {
  1136. struct list_head *cur, *tmp;
  1137. int cur_cpu;
  1138. struct vmbus_channel *cur_channel;
  1139. struct vmbus_channel *outgoing_channel = primary;
  1140. int next_channel;
  1141. int i = 1;
  1142. if (list_empty(&primary->sc_list))
  1143. return outgoing_channel;
  1144. next_channel = primary->next_oc++;
  1145. if (next_channel > (primary->num_sc)) {
  1146. primary->next_oc = 0;
  1147. return outgoing_channel;
  1148. }
  1149. cur_cpu = hv_cpu_number_to_vp_number(smp_processor_id());
  1150. list_for_each_safe(cur, tmp, &primary->sc_list) {
  1151. cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
  1152. if (cur_channel->state != CHANNEL_OPENED_STATE)
  1153. continue;
  1154. if (cur_channel->target_vp == cur_cpu)
  1155. return cur_channel;
  1156. if (i == next_channel)
  1157. return cur_channel;
  1158. i++;
  1159. }
  1160. return outgoing_channel;
  1161. }
  1162. EXPORT_SYMBOL_GPL(vmbus_get_outgoing_channel);
  1163. static void invoke_sc_cb(struct vmbus_channel *primary_channel)
  1164. {
  1165. struct list_head *cur, *tmp;
  1166. struct vmbus_channel *cur_channel;
  1167. if (primary_channel->sc_creation_callback == NULL)
  1168. return;
  1169. list_for_each_safe(cur, tmp, &primary_channel->sc_list) {
  1170. cur_channel = list_entry(cur, struct vmbus_channel, sc_list);
  1171. primary_channel->sc_creation_callback(cur_channel);
  1172. }
  1173. }
  1174. void vmbus_set_sc_create_callback(struct vmbus_channel *primary_channel,
  1175. void (*sc_cr_cb)(struct vmbus_channel *new_sc))
  1176. {
  1177. primary_channel->sc_creation_callback = sc_cr_cb;
  1178. }
  1179. EXPORT_SYMBOL_GPL(vmbus_set_sc_create_callback);
  1180. bool vmbus_are_subchannels_present(struct vmbus_channel *primary)
  1181. {
  1182. bool ret;
  1183. ret = !list_empty(&primary->sc_list);
  1184. if (ret) {
  1185. /*
  1186. * Invoke the callback on sub-channel creation.
  1187. * This will present a uniform interface to the
  1188. * clients.
  1189. */
  1190. invoke_sc_cb(primary);
  1191. }
  1192. return ret;
  1193. }
  1194. EXPORT_SYMBOL_GPL(vmbus_are_subchannels_present);
  1195. void vmbus_set_chn_rescind_callback(struct vmbus_channel *channel,
  1196. void (*chn_rescind_cb)(struct vmbus_channel *))
  1197. {
  1198. channel->chn_rescind_callback = chn_rescind_cb;
  1199. }
  1200. EXPORT_SYMBOL_GPL(vmbus_set_chn_rescind_callback);