channel_mgmt.c 36 KB

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