vmbus_drv.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2009, Microsoft Corporation.
  4. *
  5. * Authors:
  6. * Haiyang Zhang <haiyangz@microsoft.com>
  7. * Hank Janssen <hjanssen@microsoft.com>
  8. * K. Y. Srinivasan <kys@microsoft.com>
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/device.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/sysctl.h>
  17. #include <linux/slab.h>
  18. #include <linux/acpi.h>
  19. #include <linux/completion.h>
  20. #include <linux/hyperv.h>
  21. #include <linux/kernel_stat.h>
  22. #include <linux/of_address.h>
  23. #include <linux/clockchips.h>
  24. #include <linux/cpu.h>
  25. #include <linux/sched/isolation.h>
  26. #include <linux/sched/task_stack.h>
  27. #include <linux/delay.h>
  28. #include <linux/panic_notifier.h>
  29. #include <linux/ptrace.h>
  30. #include <linux/screen_info.h>
  31. #include <linux/efi.h>
  32. #include <linux/random.h>
  33. #include <linux/kernel.h>
  34. #include <linux/syscore_ops.h>
  35. #include <linux/dma-map-ops.h>
  36. #include <linux/pci.h>
  37. #include <clocksource/hyperv_timer.h>
  38. #include <asm/mshyperv.h>
  39. #include "hyperv_vmbus.h"
  40. struct vmbus_dynid {
  41. struct list_head node;
  42. struct hv_vmbus_device_id id;
  43. };
  44. static struct device *hv_dev;
  45. static int hyperv_cpuhp_online;
  46. static long __percpu *vmbus_evt;
  47. /* Values parsed from ACPI DSDT */
  48. int vmbus_irq;
  49. int vmbus_interrupt;
  50. /*
  51. * The panic notifier below is responsible solely for unloading the
  52. * vmbus connection, which is necessary in a panic event.
  53. *
  54. * Notice an intrincate relation of this notifier with Hyper-V
  55. * framebuffer panic notifier exists - we need vmbus connection alive
  56. * there in order to succeed, so we need to order both with each other
  57. * [see hvfb_on_panic()] - this is done using notifiers' priorities.
  58. */
  59. static int hv_panic_vmbus_unload(struct notifier_block *nb, unsigned long val,
  60. void *args)
  61. {
  62. vmbus_initiate_unload(true);
  63. return NOTIFY_DONE;
  64. }
  65. static struct notifier_block hyperv_panic_vmbus_unload_block = {
  66. .notifier_call = hv_panic_vmbus_unload,
  67. .priority = INT_MIN + 1, /* almost the latest one to execute */
  68. };
  69. static const char *fb_mmio_name = "fb_range";
  70. static struct resource *fb_mmio;
  71. static struct resource *hyperv_mmio;
  72. static DEFINE_MUTEX(hyperv_mmio_lock);
  73. static int vmbus_exists(void)
  74. {
  75. if (hv_dev == NULL)
  76. return -ENODEV;
  77. return 0;
  78. }
  79. static u8 channel_monitor_group(const struct vmbus_channel *channel)
  80. {
  81. return (u8)channel->offermsg.monitorid / 32;
  82. }
  83. static u8 channel_monitor_offset(const struct vmbus_channel *channel)
  84. {
  85. return (u8)channel->offermsg.monitorid % 32;
  86. }
  87. static u32 channel_pending(const struct vmbus_channel *channel,
  88. const struct hv_monitor_page *monitor_page)
  89. {
  90. u8 monitor_group = channel_monitor_group(channel);
  91. return monitor_page->trigger_group[monitor_group].pending;
  92. }
  93. static u32 channel_latency(const struct vmbus_channel *channel,
  94. const struct hv_monitor_page *monitor_page)
  95. {
  96. u8 monitor_group = channel_monitor_group(channel);
  97. u8 monitor_offset = channel_monitor_offset(channel);
  98. return monitor_page->latency[monitor_group][monitor_offset];
  99. }
  100. static u32 channel_conn_id(struct vmbus_channel *channel,
  101. struct hv_monitor_page *monitor_page)
  102. {
  103. u8 monitor_group = channel_monitor_group(channel);
  104. u8 monitor_offset = channel_monitor_offset(channel);
  105. return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
  106. }
  107. static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
  108. char *buf)
  109. {
  110. struct hv_device *hv_dev = device_to_hv_device(dev);
  111. if (!hv_dev->channel)
  112. return -ENODEV;
  113. return sysfs_emit(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
  114. }
  115. static DEVICE_ATTR_RO(id);
  116. static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
  117. char *buf)
  118. {
  119. struct hv_device *hv_dev = device_to_hv_device(dev);
  120. if (!hv_dev->channel)
  121. return -ENODEV;
  122. return sysfs_emit(buf, "%d\n", hv_dev->channel->state);
  123. }
  124. static DEVICE_ATTR_RO(state);
  125. static ssize_t monitor_id_show(struct device *dev,
  126. struct device_attribute *dev_attr, char *buf)
  127. {
  128. struct hv_device *hv_dev = device_to_hv_device(dev);
  129. if (!hv_dev->channel)
  130. return -ENODEV;
  131. return sysfs_emit(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
  132. }
  133. static DEVICE_ATTR_RO(monitor_id);
  134. static ssize_t class_id_show(struct device *dev,
  135. struct device_attribute *dev_attr, char *buf)
  136. {
  137. struct hv_device *hv_dev = device_to_hv_device(dev);
  138. if (!hv_dev->channel)
  139. return -ENODEV;
  140. return sysfs_emit(buf, "{%pUl}\n",
  141. &hv_dev->channel->offermsg.offer.if_type);
  142. }
  143. static DEVICE_ATTR_RO(class_id);
  144. static ssize_t device_id_show(struct device *dev,
  145. struct device_attribute *dev_attr, char *buf)
  146. {
  147. struct hv_device *hv_dev = device_to_hv_device(dev);
  148. if (!hv_dev->channel)
  149. return -ENODEV;
  150. return sysfs_emit(buf, "{%pUl}\n",
  151. &hv_dev->channel->offermsg.offer.if_instance);
  152. }
  153. static DEVICE_ATTR_RO(device_id);
  154. static ssize_t modalias_show(struct device *dev,
  155. struct device_attribute *dev_attr, char *buf)
  156. {
  157. struct hv_device *hv_dev = device_to_hv_device(dev);
  158. return sysfs_emit(buf, "vmbus:%*phN\n", UUID_SIZE, &hv_dev->dev_type);
  159. }
  160. static DEVICE_ATTR_RO(modalias);
  161. #ifdef CONFIG_NUMA
  162. static ssize_t numa_node_show(struct device *dev,
  163. struct device_attribute *attr, char *buf)
  164. {
  165. struct hv_device *hv_dev = device_to_hv_device(dev);
  166. if (!hv_dev->channel)
  167. return -ENODEV;
  168. return sysfs_emit(buf, "%d\n", cpu_to_node(hv_dev->channel->target_cpu));
  169. }
  170. static DEVICE_ATTR_RO(numa_node);
  171. #endif
  172. static ssize_t server_monitor_pending_show(struct device *dev,
  173. struct device_attribute *dev_attr,
  174. char *buf)
  175. {
  176. struct hv_device *hv_dev = device_to_hv_device(dev);
  177. if (!hv_dev->channel)
  178. return -ENODEV;
  179. return sysfs_emit(buf, "%d\n", channel_pending(hv_dev->channel,
  180. vmbus_connection.monitor_pages[0]));
  181. }
  182. static DEVICE_ATTR_RO(server_monitor_pending);
  183. static ssize_t client_monitor_pending_show(struct device *dev,
  184. struct device_attribute *dev_attr,
  185. char *buf)
  186. {
  187. struct hv_device *hv_dev = device_to_hv_device(dev);
  188. if (!hv_dev->channel)
  189. return -ENODEV;
  190. return sysfs_emit(buf, "%d\n", channel_pending(hv_dev->channel,
  191. vmbus_connection.monitor_pages[1]));
  192. }
  193. static DEVICE_ATTR_RO(client_monitor_pending);
  194. static ssize_t server_monitor_latency_show(struct device *dev,
  195. struct device_attribute *dev_attr,
  196. char *buf)
  197. {
  198. struct hv_device *hv_dev = device_to_hv_device(dev);
  199. if (!hv_dev->channel)
  200. return -ENODEV;
  201. return sysfs_emit(buf, "%d\n", channel_latency(hv_dev->channel,
  202. vmbus_connection.monitor_pages[0]));
  203. }
  204. static DEVICE_ATTR_RO(server_monitor_latency);
  205. static ssize_t client_monitor_latency_show(struct device *dev,
  206. struct device_attribute *dev_attr,
  207. char *buf)
  208. {
  209. struct hv_device *hv_dev = device_to_hv_device(dev);
  210. if (!hv_dev->channel)
  211. return -ENODEV;
  212. return sysfs_emit(buf, "%d\n", channel_latency(hv_dev->channel,
  213. vmbus_connection.monitor_pages[1]));
  214. }
  215. static DEVICE_ATTR_RO(client_monitor_latency);
  216. static ssize_t server_monitor_conn_id_show(struct device *dev,
  217. struct device_attribute *dev_attr,
  218. char *buf)
  219. {
  220. struct hv_device *hv_dev = device_to_hv_device(dev);
  221. if (!hv_dev->channel)
  222. return -ENODEV;
  223. return sysfs_emit(buf, "%d\n", channel_conn_id(hv_dev->channel,
  224. vmbus_connection.monitor_pages[0]));
  225. }
  226. static DEVICE_ATTR_RO(server_monitor_conn_id);
  227. static ssize_t client_monitor_conn_id_show(struct device *dev,
  228. struct device_attribute *dev_attr,
  229. char *buf)
  230. {
  231. struct hv_device *hv_dev = device_to_hv_device(dev);
  232. if (!hv_dev->channel)
  233. return -ENODEV;
  234. return sysfs_emit(buf, "%d\n", channel_conn_id(hv_dev->channel,
  235. vmbus_connection.monitor_pages[1]));
  236. }
  237. static DEVICE_ATTR_RO(client_monitor_conn_id);
  238. static ssize_t out_intr_mask_show(struct device *dev,
  239. struct device_attribute *dev_attr, char *buf)
  240. {
  241. struct hv_device *hv_dev = device_to_hv_device(dev);
  242. struct hv_ring_buffer_debug_info outbound;
  243. int ret;
  244. if (!hv_dev->channel)
  245. return -ENODEV;
  246. ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
  247. &outbound);
  248. if (ret < 0)
  249. return ret;
  250. return sysfs_emit(buf, "%d\n", outbound.current_interrupt_mask);
  251. }
  252. static DEVICE_ATTR_RO(out_intr_mask);
  253. static ssize_t out_read_index_show(struct device *dev,
  254. struct device_attribute *dev_attr, char *buf)
  255. {
  256. struct hv_device *hv_dev = device_to_hv_device(dev);
  257. struct hv_ring_buffer_debug_info outbound;
  258. int ret;
  259. if (!hv_dev->channel)
  260. return -ENODEV;
  261. ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
  262. &outbound);
  263. if (ret < 0)
  264. return ret;
  265. return sysfs_emit(buf, "%d\n", outbound.current_read_index);
  266. }
  267. static DEVICE_ATTR_RO(out_read_index);
  268. static ssize_t out_write_index_show(struct device *dev,
  269. struct device_attribute *dev_attr,
  270. char *buf)
  271. {
  272. struct hv_device *hv_dev = device_to_hv_device(dev);
  273. struct hv_ring_buffer_debug_info outbound;
  274. int ret;
  275. if (!hv_dev->channel)
  276. return -ENODEV;
  277. ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
  278. &outbound);
  279. if (ret < 0)
  280. return ret;
  281. return sysfs_emit(buf, "%d\n", outbound.current_write_index);
  282. }
  283. static DEVICE_ATTR_RO(out_write_index);
  284. static ssize_t out_read_bytes_avail_show(struct device *dev,
  285. struct device_attribute *dev_attr,
  286. char *buf)
  287. {
  288. struct hv_device *hv_dev = device_to_hv_device(dev);
  289. struct hv_ring_buffer_debug_info outbound;
  290. int ret;
  291. if (!hv_dev->channel)
  292. return -ENODEV;
  293. ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
  294. &outbound);
  295. if (ret < 0)
  296. return ret;
  297. return sysfs_emit(buf, "%d\n", outbound.bytes_avail_toread);
  298. }
  299. static DEVICE_ATTR_RO(out_read_bytes_avail);
  300. static ssize_t out_write_bytes_avail_show(struct device *dev,
  301. struct device_attribute *dev_attr,
  302. char *buf)
  303. {
  304. struct hv_device *hv_dev = device_to_hv_device(dev);
  305. struct hv_ring_buffer_debug_info outbound;
  306. int ret;
  307. if (!hv_dev->channel)
  308. return -ENODEV;
  309. ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound,
  310. &outbound);
  311. if (ret < 0)
  312. return ret;
  313. return sysfs_emit(buf, "%d\n", outbound.bytes_avail_towrite);
  314. }
  315. static DEVICE_ATTR_RO(out_write_bytes_avail);
  316. static ssize_t in_intr_mask_show(struct device *dev,
  317. struct device_attribute *dev_attr, char *buf)
  318. {
  319. struct hv_device *hv_dev = device_to_hv_device(dev);
  320. struct hv_ring_buffer_debug_info inbound;
  321. int ret;
  322. if (!hv_dev->channel)
  323. return -ENODEV;
  324. ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  325. if (ret < 0)
  326. return ret;
  327. return sysfs_emit(buf, "%d\n", inbound.current_interrupt_mask);
  328. }
  329. static DEVICE_ATTR_RO(in_intr_mask);
  330. static ssize_t in_read_index_show(struct device *dev,
  331. struct device_attribute *dev_attr, char *buf)
  332. {
  333. struct hv_device *hv_dev = device_to_hv_device(dev);
  334. struct hv_ring_buffer_debug_info inbound;
  335. int ret;
  336. if (!hv_dev->channel)
  337. return -ENODEV;
  338. ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  339. if (ret < 0)
  340. return ret;
  341. return sysfs_emit(buf, "%d\n", inbound.current_read_index);
  342. }
  343. static DEVICE_ATTR_RO(in_read_index);
  344. static ssize_t in_write_index_show(struct device *dev,
  345. struct device_attribute *dev_attr, char *buf)
  346. {
  347. struct hv_device *hv_dev = device_to_hv_device(dev);
  348. struct hv_ring_buffer_debug_info inbound;
  349. int ret;
  350. if (!hv_dev->channel)
  351. return -ENODEV;
  352. ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  353. if (ret < 0)
  354. return ret;
  355. return sysfs_emit(buf, "%d\n", inbound.current_write_index);
  356. }
  357. static DEVICE_ATTR_RO(in_write_index);
  358. static ssize_t in_read_bytes_avail_show(struct device *dev,
  359. struct device_attribute *dev_attr,
  360. char *buf)
  361. {
  362. struct hv_device *hv_dev = device_to_hv_device(dev);
  363. struct hv_ring_buffer_debug_info inbound;
  364. int ret;
  365. if (!hv_dev->channel)
  366. return -ENODEV;
  367. ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  368. if (ret < 0)
  369. return ret;
  370. return sysfs_emit(buf, "%d\n", inbound.bytes_avail_toread);
  371. }
  372. static DEVICE_ATTR_RO(in_read_bytes_avail);
  373. static ssize_t in_write_bytes_avail_show(struct device *dev,
  374. struct device_attribute *dev_attr,
  375. char *buf)
  376. {
  377. struct hv_device *hv_dev = device_to_hv_device(dev);
  378. struct hv_ring_buffer_debug_info inbound;
  379. int ret;
  380. if (!hv_dev->channel)
  381. return -ENODEV;
  382. ret = hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  383. if (ret < 0)
  384. return ret;
  385. return sysfs_emit(buf, "%d\n", inbound.bytes_avail_towrite);
  386. }
  387. static DEVICE_ATTR_RO(in_write_bytes_avail);
  388. static ssize_t channel_vp_mapping_show(struct device *dev,
  389. struct device_attribute *dev_attr,
  390. char *buf)
  391. {
  392. struct hv_device *hv_dev = device_to_hv_device(dev);
  393. struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
  394. int n_written;
  395. struct list_head *cur;
  396. if (!channel)
  397. return -ENODEV;
  398. mutex_lock(&vmbus_connection.channel_mutex);
  399. n_written = sysfs_emit(buf, "%u:%u\n",
  400. channel->offermsg.child_relid,
  401. channel->target_cpu);
  402. list_for_each(cur, &channel->sc_list) {
  403. cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
  404. n_written += sysfs_emit_at(buf, n_written, "%u:%u\n",
  405. cur_sc->offermsg.child_relid,
  406. cur_sc->target_cpu);
  407. }
  408. mutex_unlock(&vmbus_connection.channel_mutex);
  409. return n_written;
  410. }
  411. static DEVICE_ATTR_RO(channel_vp_mapping);
  412. static ssize_t vendor_show(struct device *dev,
  413. struct device_attribute *dev_attr,
  414. char *buf)
  415. {
  416. struct hv_device *hv_dev = device_to_hv_device(dev);
  417. return sysfs_emit(buf, "0x%x\n", hv_dev->vendor_id);
  418. }
  419. static DEVICE_ATTR_RO(vendor);
  420. static ssize_t device_show(struct device *dev,
  421. struct device_attribute *dev_attr,
  422. char *buf)
  423. {
  424. struct hv_device *hv_dev = device_to_hv_device(dev);
  425. return sysfs_emit(buf, "0x%x\n", hv_dev->device_id);
  426. }
  427. static DEVICE_ATTR_RO(device);
  428. static ssize_t driver_override_store(struct device *dev,
  429. struct device_attribute *attr,
  430. const char *buf, size_t count)
  431. {
  432. struct hv_device *hv_dev = device_to_hv_device(dev);
  433. int ret;
  434. ret = driver_set_override(dev, &hv_dev->driver_override, buf, count);
  435. if (ret)
  436. return ret;
  437. return count;
  438. }
  439. static ssize_t driver_override_show(struct device *dev,
  440. struct device_attribute *attr, char *buf)
  441. {
  442. struct hv_device *hv_dev = device_to_hv_device(dev);
  443. ssize_t len;
  444. device_lock(dev);
  445. len = sysfs_emit(buf, "%s\n", hv_dev->driver_override);
  446. device_unlock(dev);
  447. return len;
  448. }
  449. static DEVICE_ATTR_RW(driver_override);
  450. /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
  451. static struct attribute *vmbus_dev_attrs[] = {
  452. &dev_attr_id.attr,
  453. &dev_attr_state.attr,
  454. &dev_attr_monitor_id.attr,
  455. &dev_attr_class_id.attr,
  456. &dev_attr_device_id.attr,
  457. &dev_attr_modalias.attr,
  458. #ifdef CONFIG_NUMA
  459. &dev_attr_numa_node.attr,
  460. #endif
  461. &dev_attr_server_monitor_pending.attr,
  462. &dev_attr_client_monitor_pending.attr,
  463. &dev_attr_server_monitor_latency.attr,
  464. &dev_attr_client_monitor_latency.attr,
  465. &dev_attr_server_monitor_conn_id.attr,
  466. &dev_attr_client_monitor_conn_id.attr,
  467. &dev_attr_out_intr_mask.attr,
  468. &dev_attr_out_read_index.attr,
  469. &dev_attr_out_write_index.attr,
  470. &dev_attr_out_read_bytes_avail.attr,
  471. &dev_attr_out_write_bytes_avail.attr,
  472. &dev_attr_in_intr_mask.attr,
  473. &dev_attr_in_read_index.attr,
  474. &dev_attr_in_write_index.attr,
  475. &dev_attr_in_read_bytes_avail.attr,
  476. &dev_attr_in_write_bytes_avail.attr,
  477. &dev_attr_channel_vp_mapping.attr,
  478. &dev_attr_vendor.attr,
  479. &dev_attr_device.attr,
  480. &dev_attr_driver_override.attr,
  481. NULL,
  482. };
  483. /*
  484. * Device-level attribute_group callback function. Returns the permission for
  485. * each attribute, and returns 0 if an attribute is not visible.
  486. */
  487. static umode_t vmbus_dev_attr_is_visible(struct kobject *kobj,
  488. struct attribute *attr, int idx)
  489. {
  490. struct device *dev = kobj_to_dev(kobj);
  491. const struct hv_device *hv_dev = device_to_hv_device(dev);
  492. /* Hide the monitor attributes if the monitor mechanism is not used. */
  493. if (!hv_dev->channel->offermsg.monitor_allocated &&
  494. (attr == &dev_attr_monitor_id.attr ||
  495. attr == &dev_attr_server_monitor_pending.attr ||
  496. attr == &dev_attr_client_monitor_pending.attr ||
  497. attr == &dev_attr_server_monitor_latency.attr ||
  498. attr == &dev_attr_client_monitor_latency.attr ||
  499. attr == &dev_attr_server_monitor_conn_id.attr ||
  500. attr == &dev_attr_client_monitor_conn_id.attr))
  501. return 0;
  502. return attr->mode;
  503. }
  504. static const struct attribute_group vmbus_dev_group = {
  505. .attrs = vmbus_dev_attrs,
  506. .is_visible = vmbus_dev_attr_is_visible
  507. };
  508. __ATTRIBUTE_GROUPS(vmbus_dev);
  509. /* Set up the attribute for /sys/bus/vmbus/hibernation */
  510. static ssize_t hibernation_show(const struct bus_type *bus, char *buf)
  511. {
  512. return sprintf(buf, "%d\n", !!hv_is_hibernation_supported());
  513. }
  514. static BUS_ATTR_RO(hibernation);
  515. static struct attribute *vmbus_bus_attrs[] = {
  516. &bus_attr_hibernation.attr,
  517. NULL,
  518. };
  519. static const struct attribute_group vmbus_bus_group = {
  520. .attrs = vmbus_bus_attrs,
  521. };
  522. __ATTRIBUTE_GROUPS(vmbus_bus);
  523. /*
  524. * vmbus_uevent - add uevent for our device
  525. *
  526. * This routine is invoked when a device is added or removed on the vmbus to
  527. * generate a uevent to udev in the userspace. The udev will then look at its
  528. * rule and the uevent generated here to load the appropriate driver
  529. *
  530. * The alias string will be of the form vmbus:guid where guid is the string
  531. * representation of the device guid (each byte of the guid will be
  532. * represented with two hex characters.
  533. */
  534. static int vmbus_uevent(const struct device *device, struct kobj_uevent_env *env)
  535. {
  536. const struct hv_device *dev = device_to_hv_device(device);
  537. const char *format = "MODALIAS=vmbus:%*phN";
  538. return add_uevent_var(env, format, UUID_SIZE, &dev->dev_type);
  539. }
  540. static const struct hv_vmbus_device_id *
  541. hv_vmbus_dev_match(const struct hv_vmbus_device_id *id, const guid_t *guid)
  542. {
  543. if (id == NULL)
  544. return NULL; /* empty device table */
  545. for (; !guid_is_null(&id->guid); id++)
  546. if (guid_equal(&id->guid, guid))
  547. return id;
  548. return NULL;
  549. }
  550. static const struct hv_vmbus_device_id *
  551. hv_vmbus_dynid_match(struct hv_driver *drv, const guid_t *guid)
  552. {
  553. const struct hv_vmbus_device_id *id = NULL;
  554. struct vmbus_dynid *dynid;
  555. spin_lock(&drv->dynids.lock);
  556. list_for_each_entry(dynid, &drv->dynids.list, node) {
  557. if (guid_equal(&dynid->id.guid, guid)) {
  558. id = &dynid->id;
  559. break;
  560. }
  561. }
  562. spin_unlock(&drv->dynids.lock);
  563. return id;
  564. }
  565. static const struct hv_vmbus_device_id vmbus_device_null;
  566. /*
  567. * Return a matching hv_vmbus_device_id pointer.
  568. * If there is no match, return NULL.
  569. */
  570. static const struct hv_vmbus_device_id *hv_vmbus_get_id(const struct hv_driver *drv,
  571. struct hv_device *dev)
  572. {
  573. const guid_t *guid = &dev->dev_type;
  574. const struct hv_vmbus_device_id *id;
  575. /* When driver_override is set, only bind to the matching driver */
  576. if (dev->driver_override && strcmp(dev->driver_override, drv->name))
  577. return NULL;
  578. /* Look at the dynamic ids first, before the static ones */
  579. id = hv_vmbus_dynid_match((struct hv_driver *)drv, guid);
  580. if (!id)
  581. id = hv_vmbus_dev_match(drv->id_table, guid);
  582. /* driver_override will always match, send a dummy id */
  583. if (!id && dev->driver_override)
  584. id = &vmbus_device_null;
  585. return id;
  586. }
  587. /* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
  588. static int vmbus_add_dynid(struct hv_driver *drv, guid_t *guid)
  589. {
  590. struct vmbus_dynid *dynid;
  591. dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
  592. if (!dynid)
  593. return -ENOMEM;
  594. dynid->id.guid = *guid;
  595. spin_lock(&drv->dynids.lock);
  596. list_add_tail(&dynid->node, &drv->dynids.list);
  597. spin_unlock(&drv->dynids.lock);
  598. return driver_attach(&drv->driver);
  599. }
  600. static void vmbus_free_dynids(struct hv_driver *drv)
  601. {
  602. struct vmbus_dynid *dynid, *n;
  603. spin_lock(&drv->dynids.lock);
  604. list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
  605. list_del(&dynid->node);
  606. kfree(dynid);
  607. }
  608. spin_unlock(&drv->dynids.lock);
  609. }
  610. /*
  611. * store_new_id - sysfs frontend to vmbus_add_dynid()
  612. *
  613. * Allow GUIDs to be added to an existing driver via sysfs.
  614. */
  615. static ssize_t new_id_store(struct device_driver *driver, const char *buf,
  616. size_t count)
  617. {
  618. struct hv_driver *drv = drv_to_hv_drv(driver);
  619. guid_t guid;
  620. ssize_t retval;
  621. retval = guid_parse(buf, &guid);
  622. if (retval)
  623. return retval;
  624. if (hv_vmbus_dynid_match(drv, &guid))
  625. return -EEXIST;
  626. retval = vmbus_add_dynid(drv, &guid);
  627. if (retval)
  628. return retval;
  629. return count;
  630. }
  631. static DRIVER_ATTR_WO(new_id);
  632. /*
  633. * store_remove_id - remove a PCI device ID from this driver
  634. *
  635. * Removes a dynamic pci device ID to this driver.
  636. */
  637. static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
  638. size_t count)
  639. {
  640. struct hv_driver *drv = drv_to_hv_drv(driver);
  641. struct vmbus_dynid *dynid, *n;
  642. guid_t guid;
  643. ssize_t retval;
  644. retval = guid_parse(buf, &guid);
  645. if (retval)
  646. return retval;
  647. retval = -ENODEV;
  648. spin_lock(&drv->dynids.lock);
  649. list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
  650. struct hv_vmbus_device_id *id = &dynid->id;
  651. if (guid_equal(&id->guid, &guid)) {
  652. list_del(&dynid->node);
  653. kfree(dynid);
  654. retval = count;
  655. break;
  656. }
  657. }
  658. spin_unlock(&drv->dynids.lock);
  659. return retval;
  660. }
  661. static DRIVER_ATTR_WO(remove_id);
  662. static struct attribute *vmbus_drv_attrs[] = {
  663. &driver_attr_new_id.attr,
  664. &driver_attr_remove_id.attr,
  665. NULL,
  666. };
  667. ATTRIBUTE_GROUPS(vmbus_drv);
  668. /*
  669. * vmbus_match - Attempt to match the specified device to the specified driver
  670. */
  671. static int vmbus_match(struct device *device, const struct device_driver *driver)
  672. {
  673. const struct hv_driver *drv = drv_to_hv_drv(driver);
  674. struct hv_device *hv_dev = device_to_hv_device(device);
  675. /* The hv_sock driver handles all hv_sock offers. */
  676. if (is_hvsock_channel(hv_dev->channel))
  677. return drv->hvsock;
  678. if (hv_vmbus_get_id(drv, hv_dev))
  679. return 1;
  680. return 0;
  681. }
  682. /*
  683. * vmbus_probe - Add the new vmbus's child device
  684. */
  685. static int vmbus_probe(struct device *child_device)
  686. {
  687. int ret = 0;
  688. struct hv_driver *drv =
  689. drv_to_hv_drv(child_device->driver);
  690. struct hv_device *dev = device_to_hv_device(child_device);
  691. const struct hv_vmbus_device_id *dev_id;
  692. dev_id = hv_vmbus_get_id(drv, dev);
  693. if (drv->probe) {
  694. ret = drv->probe(dev, dev_id);
  695. if (ret != 0)
  696. pr_err("probe failed for device %s (%d)\n",
  697. dev_name(child_device), ret);
  698. } else {
  699. pr_err("probe not set for driver %s\n",
  700. dev_name(child_device));
  701. ret = -ENODEV;
  702. }
  703. return ret;
  704. }
  705. /*
  706. * vmbus_dma_configure -- Configure DMA coherence for VMbus device
  707. */
  708. static int vmbus_dma_configure(struct device *child_device)
  709. {
  710. /*
  711. * On ARM64, propagate the DMA coherence setting from the top level
  712. * VMbus ACPI device to the child VMbus device being added here.
  713. * On x86/x64 coherence is assumed and these calls have no effect.
  714. */
  715. hv_setup_dma_ops(child_device,
  716. device_get_dma_attr(hv_dev) == DEV_DMA_COHERENT);
  717. return 0;
  718. }
  719. /*
  720. * vmbus_remove - Remove a vmbus device
  721. */
  722. static void vmbus_remove(struct device *child_device)
  723. {
  724. struct hv_driver *drv;
  725. struct hv_device *dev = device_to_hv_device(child_device);
  726. if (child_device->driver) {
  727. drv = drv_to_hv_drv(child_device->driver);
  728. if (drv->remove)
  729. drv->remove(dev);
  730. }
  731. }
  732. /*
  733. * vmbus_shutdown - Shutdown a vmbus device
  734. */
  735. static void vmbus_shutdown(struct device *child_device)
  736. {
  737. struct hv_driver *drv;
  738. struct hv_device *dev = device_to_hv_device(child_device);
  739. /* The device may not be attached yet */
  740. if (!child_device->driver)
  741. return;
  742. drv = drv_to_hv_drv(child_device->driver);
  743. if (drv->shutdown)
  744. drv->shutdown(dev);
  745. }
  746. #ifdef CONFIG_PM_SLEEP
  747. /*
  748. * vmbus_suspend - Suspend a vmbus device
  749. */
  750. static int vmbus_suspend(struct device *child_device)
  751. {
  752. struct hv_driver *drv;
  753. struct hv_device *dev = device_to_hv_device(child_device);
  754. /* The device may not be attached yet */
  755. if (!child_device->driver)
  756. return 0;
  757. drv = drv_to_hv_drv(child_device->driver);
  758. if (!drv->suspend)
  759. return -EOPNOTSUPP;
  760. return drv->suspend(dev);
  761. }
  762. /*
  763. * vmbus_resume - Resume a vmbus device
  764. */
  765. static int vmbus_resume(struct device *child_device)
  766. {
  767. struct hv_driver *drv;
  768. struct hv_device *dev = device_to_hv_device(child_device);
  769. /* The device may not be attached yet */
  770. if (!child_device->driver)
  771. return 0;
  772. drv = drv_to_hv_drv(child_device->driver);
  773. if (!drv->resume)
  774. return -EOPNOTSUPP;
  775. return drv->resume(dev);
  776. }
  777. #else
  778. #define vmbus_suspend NULL
  779. #define vmbus_resume NULL
  780. #endif /* CONFIG_PM_SLEEP */
  781. /*
  782. * vmbus_device_release - Final callback release of the vmbus child device
  783. */
  784. static void vmbus_device_release(struct device *device)
  785. {
  786. struct hv_device *hv_dev = device_to_hv_device(device);
  787. struct vmbus_channel *channel = hv_dev->channel;
  788. hv_debug_rm_dev_dir(hv_dev);
  789. mutex_lock(&vmbus_connection.channel_mutex);
  790. hv_process_channel_removal(channel);
  791. mutex_unlock(&vmbus_connection.channel_mutex);
  792. kfree(hv_dev);
  793. }
  794. /*
  795. * Note: we must use the "noirq" ops: see the comment before vmbus_bus_pm.
  796. *
  797. * suspend_noirq/resume_noirq are set to NULL to support Suspend-to-Idle: we
  798. * shouldn't suspend the vmbus devices upon Suspend-to-Idle, otherwise there
  799. * is no way to wake up a Generation-2 VM.
  800. *
  801. * The other 4 ops are for hibernation.
  802. */
  803. static const struct dev_pm_ops vmbus_pm = {
  804. .suspend_noirq = NULL,
  805. .resume_noirq = NULL,
  806. .freeze_noirq = vmbus_suspend,
  807. .thaw_noirq = vmbus_resume,
  808. .poweroff_noirq = vmbus_suspend,
  809. .restore_noirq = vmbus_resume,
  810. };
  811. /* The one and only one */
  812. static const struct bus_type hv_bus = {
  813. .name = "vmbus",
  814. .match = vmbus_match,
  815. .shutdown = vmbus_shutdown,
  816. .remove = vmbus_remove,
  817. .probe = vmbus_probe,
  818. .uevent = vmbus_uevent,
  819. .dma_configure = vmbus_dma_configure,
  820. .dev_groups = vmbus_dev_groups,
  821. .drv_groups = vmbus_drv_groups,
  822. .bus_groups = vmbus_bus_groups,
  823. .pm = &vmbus_pm,
  824. };
  825. struct onmessage_work_context {
  826. struct work_struct work;
  827. struct {
  828. struct hv_message_header header;
  829. u8 payload[];
  830. } msg;
  831. };
  832. static void vmbus_onmessage_work(struct work_struct *work)
  833. {
  834. struct onmessage_work_context *ctx;
  835. /* Do not process messages if we're in DISCONNECTED state */
  836. if (vmbus_connection.conn_state == DISCONNECTED)
  837. return;
  838. ctx = container_of(work, struct onmessage_work_context,
  839. work);
  840. vmbus_onmessage((struct vmbus_channel_message_header *)
  841. &ctx->msg.payload);
  842. kfree(ctx);
  843. }
  844. void vmbus_on_msg_dpc(unsigned long data)
  845. {
  846. struct hv_per_cpu_context *hv_cpu = (void *)data;
  847. void *page_addr = hv_cpu->synic_message_page;
  848. struct hv_message msg_copy, *msg = (struct hv_message *)page_addr +
  849. VMBUS_MESSAGE_SINT;
  850. struct vmbus_channel_message_header *hdr;
  851. enum vmbus_channel_message_type msgtype;
  852. const struct vmbus_channel_message_table_entry *entry;
  853. struct onmessage_work_context *ctx;
  854. __u8 payload_size;
  855. u32 message_type;
  856. /*
  857. * 'enum vmbus_channel_message_type' is supposed to always be 'u32' as
  858. * it is being used in 'struct vmbus_channel_message_header' definition
  859. * which is supposed to match hypervisor ABI.
  860. */
  861. BUILD_BUG_ON(sizeof(enum vmbus_channel_message_type) != sizeof(u32));
  862. /*
  863. * Since the message is in memory shared with the host, an erroneous or
  864. * malicious Hyper-V could modify the message while vmbus_on_msg_dpc()
  865. * or individual message handlers are executing; to prevent this, copy
  866. * the message into private memory.
  867. */
  868. memcpy(&msg_copy, msg, sizeof(struct hv_message));
  869. message_type = msg_copy.header.message_type;
  870. if (message_type == HVMSG_NONE)
  871. /* no msg */
  872. return;
  873. hdr = (struct vmbus_channel_message_header *)msg_copy.u.payload;
  874. msgtype = hdr->msgtype;
  875. trace_vmbus_on_msg_dpc(hdr);
  876. if (msgtype >= CHANNELMSG_COUNT) {
  877. WARN_ONCE(1, "unknown msgtype=%d\n", msgtype);
  878. goto msg_handled;
  879. }
  880. payload_size = msg_copy.header.payload_size;
  881. if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT) {
  882. WARN_ONCE(1, "payload size is too large (%d)\n", payload_size);
  883. goto msg_handled;
  884. }
  885. entry = &channel_message_table[msgtype];
  886. if (!entry->message_handler)
  887. goto msg_handled;
  888. if (payload_size < entry->min_payload_len) {
  889. WARN_ONCE(1, "message too short: msgtype=%d len=%d\n", msgtype, payload_size);
  890. goto msg_handled;
  891. }
  892. if (entry->handler_type == VMHT_BLOCKING) {
  893. ctx = kmalloc(struct_size(ctx, msg.payload, payload_size), GFP_ATOMIC);
  894. if (ctx == NULL)
  895. return;
  896. INIT_WORK(&ctx->work, vmbus_onmessage_work);
  897. ctx->msg.header = msg_copy.header;
  898. memcpy(&ctx->msg.payload, msg_copy.u.payload, payload_size);
  899. /*
  900. * The host can generate a rescind message while we
  901. * may still be handling the original offer. We deal with
  902. * this condition by relying on the synchronization provided
  903. * by offer_in_progress and by channel_mutex. See also the
  904. * inline comments in vmbus_onoffer_rescind().
  905. */
  906. switch (msgtype) {
  907. case CHANNELMSG_RESCIND_CHANNELOFFER:
  908. /*
  909. * If we are handling the rescind message;
  910. * schedule the work on the global work queue.
  911. *
  912. * The OFFER message and the RESCIND message should
  913. * not be handled by the same serialized work queue,
  914. * because the OFFER handler may call vmbus_open(),
  915. * which tries to open the channel by sending an
  916. * OPEN_CHANNEL message to the host and waits for
  917. * the host's response; however, if the host has
  918. * rescinded the channel before it receives the
  919. * OPEN_CHANNEL message, the host just silently
  920. * ignores the OPEN_CHANNEL message; as a result,
  921. * the guest's OFFER handler hangs for ever, if we
  922. * handle the RESCIND message in the same serialized
  923. * work queue: the RESCIND handler can not start to
  924. * run before the OFFER handler finishes.
  925. */
  926. if (vmbus_connection.ignore_any_offer_msg)
  927. break;
  928. queue_work(vmbus_connection.rescind_work_queue, &ctx->work);
  929. break;
  930. case CHANNELMSG_OFFERCHANNEL:
  931. /*
  932. * The host sends the offer message of a given channel
  933. * before sending the rescind message of the same
  934. * channel. These messages are sent to the guest's
  935. * connect CPU; the guest then starts processing them
  936. * in the tasklet handler on this CPU:
  937. *
  938. * VMBUS_CONNECT_CPU
  939. *
  940. * [vmbus_on_msg_dpc()]
  941. * atomic_inc() // CHANNELMSG_OFFERCHANNEL
  942. * queue_work()
  943. * ...
  944. * [vmbus_on_msg_dpc()]
  945. * schedule_work() // CHANNELMSG_RESCIND_CHANNELOFFER
  946. *
  947. * We rely on the memory-ordering properties of the
  948. * queue_work() and schedule_work() primitives, which
  949. * guarantee that the atomic increment will be visible
  950. * to the CPUs which will execute the offer & rescind
  951. * works by the time these works will start execution.
  952. */
  953. if (vmbus_connection.ignore_any_offer_msg)
  954. break;
  955. atomic_inc(&vmbus_connection.offer_in_progress);
  956. fallthrough;
  957. default:
  958. queue_work(vmbus_connection.work_queue, &ctx->work);
  959. }
  960. } else
  961. entry->message_handler(hdr);
  962. msg_handled:
  963. vmbus_signal_eom(msg, message_type);
  964. }
  965. #ifdef CONFIG_PM_SLEEP
  966. /*
  967. * Fake RESCIND_CHANNEL messages to clean up hv_sock channels by force for
  968. * hibernation, because hv_sock connections can not persist across hibernation.
  969. */
  970. static void vmbus_force_channel_rescinded(struct vmbus_channel *channel)
  971. {
  972. struct onmessage_work_context *ctx;
  973. struct vmbus_channel_rescind_offer *rescind;
  974. WARN_ON(!is_hvsock_channel(channel));
  975. /*
  976. * Allocation size is small and the allocation should really not fail,
  977. * otherwise the state of the hv_sock connections ends up in limbo.
  978. */
  979. ctx = kzalloc(sizeof(*ctx) + sizeof(*rescind),
  980. GFP_KERNEL | __GFP_NOFAIL);
  981. /*
  982. * So far, these are not really used by Linux. Just set them to the
  983. * reasonable values conforming to the definitions of the fields.
  984. */
  985. ctx->msg.header.message_type = 1;
  986. ctx->msg.header.payload_size = sizeof(*rescind);
  987. /* These values are actually used by Linux. */
  988. rescind = (struct vmbus_channel_rescind_offer *)ctx->msg.payload;
  989. rescind->header.msgtype = CHANNELMSG_RESCIND_CHANNELOFFER;
  990. rescind->child_relid = channel->offermsg.child_relid;
  991. INIT_WORK(&ctx->work, vmbus_onmessage_work);
  992. queue_work(vmbus_connection.work_queue, &ctx->work);
  993. }
  994. #endif /* CONFIG_PM_SLEEP */
  995. /*
  996. * Schedule all channels with events pending
  997. */
  998. static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
  999. {
  1000. unsigned long *recv_int_page;
  1001. u32 maxbits, relid;
  1002. /*
  1003. * The event page can be directly checked to get the id of
  1004. * the channel that has the interrupt pending.
  1005. */
  1006. void *page_addr = hv_cpu->synic_event_page;
  1007. union hv_synic_event_flags *event
  1008. = (union hv_synic_event_flags *)page_addr +
  1009. VMBUS_MESSAGE_SINT;
  1010. maxbits = HV_EVENT_FLAGS_COUNT;
  1011. recv_int_page = event->flags;
  1012. if (unlikely(!recv_int_page))
  1013. return;
  1014. for_each_set_bit(relid, recv_int_page, maxbits) {
  1015. void (*callback_fn)(void *context);
  1016. struct vmbus_channel *channel;
  1017. if (!sync_test_and_clear_bit(relid, recv_int_page))
  1018. continue;
  1019. /* Special case - vmbus channel protocol msg */
  1020. if (relid == 0)
  1021. continue;
  1022. /*
  1023. * Pairs with the kfree_rcu() in vmbus_chan_release().
  1024. * Guarantees that the channel data structure doesn't
  1025. * get freed while the channel pointer below is being
  1026. * dereferenced.
  1027. */
  1028. rcu_read_lock();
  1029. /* Find channel based on relid */
  1030. channel = relid2channel(relid);
  1031. if (channel == NULL)
  1032. goto sched_unlock_rcu;
  1033. if (channel->rescind)
  1034. goto sched_unlock_rcu;
  1035. /*
  1036. * Make sure that the ring buffer data structure doesn't get
  1037. * freed while we dereference the ring buffer pointer. Test
  1038. * for the channel's onchannel_callback being NULL within a
  1039. * sched_lock critical section. See also the inline comments
  1040. * in vmbus_reset_channel_cb().
  1041. */
  1042. spin_lock(&channel->sched_lock);
  1043. callback_fn = channel->onchannel_callback;
  1044. if (unlikely(callback_fn == NULL))
  1045. goto sched_unlock;
  1046. trace_vmbus_chan_sched(channel);
  1047. ++channel->interrupts;
  1048. switch (channel->callback_mode) {
  1049. case HV_CALL_ISR:
  1050. (*callback_fn)(channel->channel_callback_context);
  1051. break;
  1052. case HV_CALL_BATCHED:
  1053. hv_begin_read(&channel->inbound);
  1054. fallthrough;
  1055. case HV_CALL_DIRECT:
  1056. tasklet_schedule(&channel->callback_event);
  1057. }
  1058. sched_unlock:
  1059. spin_unlock(&channel->sched_lock);
  1060. sched_unlock_rcu:
  1061. rcu_read_unlock();
  1062. }
  1063. }
  1064. static void vmbus_isr(void)
  1065. {
  1066. struct hv_per_cpu_context *hv_cpu
  1067. = this_cpu_ptr(hv_context.cpu_context);
  1068. void *page_addr;
  1069. struct hv_message *msg;
  1070. vmbus_chan_sched(hv_cpu);
  1071. page_addr = hv_cpu->synic_message_page;
  1072. msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
  1073. /* Check if there are actual msgs to be processed */
  1074. if (msg->header.message_type != HVMSG_NONE) {
  1075. if (msg->header.message_type == HVMSG_TIMER_EXPIRED) {
  1076. hv_stimer0_isr();
  1077. vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
  1078. } else
  1079. tasklet_schedule(&hv_cpu->msg_dpc);
  1080. }
  1081. add_interrupt_randomness(vmbus_interrupt);
  1082. }
  1083. static irqreturn_t vmbus_percpu_isr(int irq, void *dev_id)
  1084. {
  1085. vmbus_isr();
  1086. return IRQ_HANDLED;
  1087. }
  1088. static void vmbus_percpu_work(struct work_struct *work)
  1089. {
  1090. unsigned int cpu = smp_processor_id();
  1091. hv_synic_init(cpu);
  1092. }
  1093. /*
  1094. * vmbus_bus_init -Main vmbus driver initialization routine.
  1095. *
  1096. * Here, we
  1097. * - initialize the vmbus driver context
  1098. * - invoke the vmbus hv main init routine
  1099. * - retrieve the channel offers
  1100. */
  1101. static int vmbus_bus_init(void)
  1102. {
  1103. int ret, cpu;
  1104. struct work_struct __percpu *works;
  1105. ret = hv_init();
  1106. if (ret != 0) {
  1107. pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
  1108. return ret;
  1109. }
  1110. ret = bus_register(&hv_bus);
  1111. if (ret)
  1112. return ret;
  1113. /*
  1114. * VMbus interrupts are best modeled as per-cpu interrupts. If
  1115. * on an architecture with support for per-cpu IRQs (e.g. ARM64),
  1116. * allocate a per-cpu IRQ using standard Linux kernel functionality.
  1117. * If not on such an architecture (e.g., x86/x64), then rely on
  1118. * code in the arch-specific portion of the code tree to connect
  1119. * the VMbus interrupt handler.
  1120. */
  1121. if (vmbus_irq == -1) {
  1122. hv_setup_vmbus_handler(vmbus_isr);
  1123. } else {
  1124. vmbus_evt = alloc_percpu(long);
  1125. ret = request_percpu_irq(vmbus_irq, vmbus_percpu_isr,
  1126. "Hyper-V VMbus", vmbus_evt);
  1127. if (ret) {
  1128. pr_err("Can't request Hyper-V VMbus IRQ %d, Err %d",
  1129. vmbus_irq, ret);
  1130. free_percpu(vmbus_evt);
  1131. goto err_setup;
  1132. }
  1133. }
  1134. ret = hv_synic_alloc();
  1135. if (ret)
  1136. goto err_alloc;
  1137. works = alloc_percpu(struct work_struct);
  1138. if (!works) {
  1139. ret = -ENOMEM;
  1140. goto err_alloc;
  1141. }
  1142. /*
  1143. * Initialize the per-cpu interrupt state and stimer state.
  1144. * Then connect to the host.
  1145. */
  1146. cpus_read_lock();
  1147. for_each_online_cpu(cpu) {
  1148. struct work_struct *work = per_cpu_ptr(works, cpu);
  1149. INIT_WORK(work, vmbus_percpu_work);
  1150. schedule_work_on(cpu, work);
  1151. }
  1152. for_each_online_cpu(cpu)
  1153. flush_work(per_cpu_ptr(works, cpu));
  1154. /* Register the callbacks for possible CPU online/offline'ing */
  1155. ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online",
  1156. hv_synic_init, hv_synic_cleanup);
  1157. cpus_read_unlock();
  1158. free_percpu(works);
  1159. if (ret < 0)
  1160. goto err_alloc;
  1161. hyperv_cpuhp_online = ret;
  1162. ret = vmbus_connect();
  1163. if (ret)
  1164. goto err_connect;
  1165. /*
  1166. * Always register the vmbus unload panic notifier because we
  1167. * need to shut the VMbus channel connection on panic.
  1168. */
  1169. atomic_notifier_chain_register(&panic_notifier_list,
  1170. &hyperv_panic_vmbus_unload_block);
  1171. vmbus_request_offers();
  1172. return 0;
  1173. err_connect:
  1174. cpuhp_remove_state(hyperv_cpuhp_online);
  1175. err_alloc:
  1176. hv_synic_free();
  1177. if (vmbus_irq == -1) {
  1178. hv_remove_vmbus_handler();
  1179. } else {
  1180. free_percpu_irq(vmbus_irq, vmbus_evt);
  1181. free_percpu(vmbus_evt);
  1182. }
  1183. err_setup:
  1184. bus_unregister(&hv_bus);
  1185. return ret;
  1186. }
  1187. /**
  1188. * __vmbus_driver_register() - Register a vmbus's driver
  1189. * @hv_driver: Pointer to driver structure you want to register
  1190. * @owner: owner module of the drv
  1191. * @mod_name: module name string
  1192. *
  1193. * Registers the given driver with Linux through the 'driver_register()' call
  1194. * and sets up the hyper-v vmbus handling for this driver.
  1195. * It will return the state of the 'driver_register()' call.
  1196. *
  1197. */
  1198. int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
  1199. {
  1200. int ret;
  1201. pr_info("registering driver %s\n", hv_driver->name);
  1202. ret = vmbus_exists();
  1203. if (ret < 0)
  1204. return ret;
  1205. hv_driver->driver.name = hv_driver->name;
  1206. hv_driver->driver.owner = owner;
  1207. hv_driver->driver.mod_name = mod_name;
  1208. hv_driver->driver.bus = &hv_bus;
  1209. spin_lock_init(&hv_driver->dynids.lock);
  1210. INIT_LIST_HEAD(&hv_driver->dynids.list);
  1211. ret = driver_register(&hv_driver->driver);
  1212. return ret;
  1213. }
  1214. EXPORT_SYMBOL_GPL(__vmbus_driver_register);
  1215. /**
  1216. * vmbus_driver_unregister() - Unregister a vmbus's driver
  1217. * @hv_driver: Pointer to driver structure you want to
  1218. * un-register
  1219. *
  1220. * Un-register the given driver that was previous registered with a call to
  1221. * vmbus_driver_register()
  1222. */
  1223. void vmbus_driver_unregister(struct hv_driver *hv_driver)
  1224. {
  1225. pr_info("unregistering driver %s\n", hv_driver->name);
  1226. if (!vmbus_exists()) {
  1227. driver_unregister(&hv_driver->driver);
  1228. vmbus_free_dynids(hv_driver);
  1229. }
  1230. }
  1231. EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
  1232. /*
  1233. * Called when last reference to channel is gone.
  1234. */
  1235. static void vmbus_chan_release(struct kobject *kobj)
  1236. {
  1237. struct vmbus_channel *channel
  1238. = container_of(kobj, struct vmbus_channel, kobj);
  1239. kfree_rcu(channel, rcu);
  1240. }
  1241. struct vmbus_chan_attribute {
  1242. struct attribute attr;
  1243. ssize_t (*show)(struct vmbus_channel *chan, char *buf);
  1244. ssize_t (*store)(struct vmbus_channel *chan,
  1245. const char *buf, size_t count);
  1246. };
  1247. #define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \
  1248. struct vmbus_chan_attribute chan_attr_##_name \
  1249. = __ATTR(_name, _mode, _show, _store)
  1250. #define VMBUS_CHAN_ATTR_RW(_name) \
  1251. struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name)
  1252. #define VMBUS_CHAN_ATTR_RO(_name) \
  1253. struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name)
  1254. #define VMBUS_CHAN_ATTR_WO(_name) \
  1255. struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name)
  1256. static ssize_t vmbus_chan_attr_show(struct kobject *kobj,
  1257. struct attribute *attr, char *buf)
  1258. {
  1259. const struct vmbus_chan_attribute *attribute
  1260. = container_of(attr, struct vmbus_chan_attribute, attr);
  1261. struct vmbus_channel *chan
  1262. = container_of(kobj, struct vmbus_channel, kobj);
  1263. if (!attribute->show)
  1264. return -EIO;
  1265. return attribute->show(chan, buf);
  1266. }
  1267. static ssize_t vmbus_chan_attr_store(struct kobject *kobj,
  1268. struct attribute *attr, const char *buf,
  1269. size_t count)
  1270. {
  1271. const struct vmbus_chan_attribute *attribute
  1272. = container_of(attr, struct vmbus_chan_attribute, attr);
  1273. struct vmbus_channel *chan
  1274. = container_of(kobj, struct vmbus_channel, kobj);
  1275. if (!attribute->store)
  1276. return -EIO;
  1277. return attribute->store(chan, buf, count);
  1278. }
  1279. static const struct sysfs_ops vmbus_chan_sysfs_ops = {
  1280. .show = vmbus_chan_attr_show,
  1281. .store = vmbus_chan_attr_store,
  1282. };
  1283. static ssize_t out_mask_show(struct vmbus_channel *channel, char *buf)
  1284. {
  1285. struct hv_ring_buffer_info *rbi = &channel->outbound;
  1286. ssize_t ret;
  1287. mutex_lock(&rbi->ring_buffer_mutex);
  1288. if (!rbi->ring_buffer) {
  1289. mutex_unlock(&rbi->ring_buffer_mutex);
  1290. return -EINVAL;
  1291. }
  1292. ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
  1293. mutex_unlock(&rbi->ring_buffer_mutex);
  1294. return ret;
  1295. }
  1296. static VMBUS_CHAN_ATTR_RO(out_mask);
  1297. static ssize_t in_mask_show(struct vmbus_channel *channel, char *buf)
  1298. {
  1299. struct hv_ring_buffer_info *rbi = &channel->inbound;
  1300. ssize_t ret;
  1301. mutex_lock(&rbi->ring_buffer_mutex);
  1302. if (!rbi->ring_buffer) {
  1303. mutex_unlock(&rbi->ring_buffer_mutex);
  1304. return -EINVAL;
  1305. }
  1306. ret = sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
  1307. mutex_unlock(&rbi->ring_buffer_mutex);
  1308. return ret;
  1309. }
  1310. static VMBUS_CHAN_ATTR_RO(in_mask);
  1311. static ssize_t read_avail_show(struct vmbus_channel *channel, char *buf)
  1312. {
  1313. struct hv_ring_buffer_info *rbi = &channel->inbound;
  1314. ssize_t ret;
  1315. mutex_lock(&rbi->ring_buffer_mutex);
  1316. if (!rbi->ring_buffer) {
  1317. mutex_unlock(&rbi->ring_buffer_mutex);
  1318. return -EINVAL;
  1319. }
  1320. ret = sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
  1321. mutex_unlock(&rbi->ring_buffer_mutex);
  1322. return ret;
  1323. }
  1324. static VMBUS_CHAN_ATTR_RO(read_avail);
  1325. static ssize_t write_avail_show(struct vmbus_channel *channel, char *buf)
  1326. {
  1327. struct hv_ring_buffer_info *rbi = &channel->outbound;
  1328. ssize_t ret;
  1329. mutex_lock(&rbi->ring_buffer_mutex);
  1330. if (!rbi->ring_buffer) {
  1331. mutex_unlock(&rbi->ring_buffer_mutex);
  1332. return -EINVAL;
  1333. }
  1334. ret = sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
  1335. mutex_unlock(&rbi->ring_buffer_mutex);
  1336. return ret;
  1337. }
  1338. static VMBUS_CHAN_ATTR_RO(write_avail);
  1339. static ssize_t target_cpu_show(struct vmbus_channel *channel, char *buf)
  1340. {
  1341. return sprintf(buf, "%u\n", channel->target_cpu);
  1342. }
  1343. static ssize_t target_cpu_store(struct vmbus_channel *channel,
  1344. const char *buf, size_t count)
  1345. {
  1346. u32 target_cpu, origin_cpu;
  1347. ssize_t ret = count;
  1348. if (vmbus_proto_version < VERSION_WIN10_V4_1)
  1349. return -EIO;
  1350. if (sscanf(buf, "%uu", &target_cpu) != 1)
  1351. return -EIO;
  1352. /* Validate target_cpu for the cpumask_test_cpu() operation below. */
  1353. if (target_cpu >= nr_cpumask_bits)
  1354. return -EINVAL;
  1355. if (!cpumask_test_cpu(target_cpu, housekeeping_cpumask(HK_TYPE_MANAGED_IRQ)))
  1356. return -EINVAL;
  1357. /* No CPUs should come up or down during this. */
  1358. cpus_read_lock();
  1359. if (!cpu_online(target_cpu)) {
  1360. cpus_read_unlock();
  1361. return -EINVAL;
  1362. }
  1363. /*
  1364. * Synchronizes target_cpu_store() and channel closure:
  1365. *
  1366. * { Initially: state = CHANNEL_OPENED }
  1367. *
  1368. * CPU1 CPU2
  1369. *
  1370. * [target_cpu_store()] [vmbus_disconnect_ring()]
  1371. *
  1372. * LOCK channel_mutex LOCK channel_mutex
  1373. * LOAD r1 = state LOAD r2 = state
  1374. * IF (r1 == CHANNEL_OPENED) IF (r2 == CHANNEL_OPENED)
  1375. * SEND MODIFYCHANNEL STORE state = CHANNEL_OPEN
  1376. * [...] SEND CLOSECHANNEL
  1377. * UNLOCK channel_mutex UNLOCK channel_mutex
  1378. *
  1379. * Forbids: r1 == r2 == CHANNEL_OPENED (i.e., CPU1's LOCK precedes
  1380. * CPU2's LOCK) && CPU2's SEND precedes CPU1's SEND
  1381. *
  1382. * Note. The host processes the channel messages "sequentially", in
  1383. * the order in which they are received on a per-partition basis.
  1384. */
  1385. mutex_lock(&vmbus_connection.channel_mutex);
  1386. /*
  1387. * Hyper-V will ignore MODIFYCHANNEL messages for "non-open" channels;
  1388. * avoid sending the message and fail here for such channels.
  1389. */
  1390. if (channel->state != CHANNEL_OPENED_STATE) {
  1391. ret = -EIO;
  1392. goto cpu_store_unlock;
  1393. }
  1394. origin_cpu = channel->target_cpu;
  1395. if (target_cpu == origin_cpu)
  1396. goto cpu_store_unlock;
  1397. if (vmbus_send_modifychannel(channel,
  1398. hv_cpu_number_to_vp_number(target_cpu))) {
  1399. ret = -EIO;
  1400. goto cpu_store_unlock;
  1401. }
  1402. /*
  1403. * For version before VERSION_WIN10_V5_3, the following warning holds:
  1404. *
  1405. * Warning. At this point, there is *no* guarantee that the host will
  1406. * have successfully processed the vmbus_send_modifychannel() request.
  1407. * See the header comment of vmbus_send_modifychannel() for more info.
  1408. *
  1409. * Lags in the processing of the above vmbus_send_modifychannel() can
  1410. * result in missed interrupts if the "old" target CPU is taken offline
  1411. * before Hyper-V starts sending interrupts to the "new" target CPU.
  1412. * But apart from this offlining scenario, the code tolerates such
  1413. * lags. It will function correctly even if a channel interrupt comes
  1414. * in on a CPU that is different from the channel target_cpu value.
  1415. */
  1416. channel->target_cpu = target_cpu;
  1417. /* See init_vp_index(). */
  1418. if (hv_is_perf_channel(channel))
  1419. hv_update_allocated_cpus(origin_cpu, target_cpu);
  1420. /* Currently set only for storvsc channels. */
  1421. if (channel->change_target_cpu_callback) {
  1422. (*channel->change_target_cpu_callback)(channel,
  1423. origin_cpu, target_cpu);
  1424. }
  1425. cpu_store_unlock:
  1426. mutex_unlock(&vmbus_connection.channel_mutex);
  1427. cpus_read_unlock();
  1428. return ret;
  1429. }
  1430. static VMBUS_CHAN_ATTR(cpu, 0644, target_cpu_show, target_cpu_store);
  1431. static ssize_t channel_pending_show(struct vmbus_channel *channel,
  1432. char *buf)
  1433. {
  1434. return sprintf(buf, "%d\n",
  1435. channel_pending(channel,
  1436. vmbus_connection.monitor_pages[1]));
  1437. }
  1438. static VMBUS_CHAN_ATTR(pending, 0444, channel_pending_show, NULL);
  1439. static ssize_t channel_latency_show(struct vmbus_channel *channel,
  1440. char *buf)
  1441. {
  1442. return sprintf(buf, "%d\n",
  1443. channel_latency(channel,
  1444. vmbus_connection.monitor_pages[1]));
  1445. }
  1446. static VMBUS_CHAN_ATTR(latency, 0444, channel_latency_show, NULL);
  1447. static ssize_t channel_interrupts_show(struct vmbus_channel *channel, char *buf)
  1448. {
  1449. return sprintf(buf, "%llu\n", channel->interrupts);
  1450. }
  1451. static VMBUS_CHAN_ATTR(interrupts, 0444, channel_interrupts_show, NULL);
  1452. static ssize_t channel_events_show(struct vmbus_channel *channel, char *buf)
  1453. {
  1454. return sprintf(buf, "%llu\n", channel->sig_events);
  1455. }
  1456. static VMBUS_CHAN_ATTR(events, 0444, channel_events_show, NULL);
  1457. static ssize_t channel_intr_in_full_show(struct vmbus_channel *channel,
  1458. char *buf)
  1459. {
  1460. return sprintf(buf, "%llu\n",
  1461. (unsigned long long)channel->intr_in_full);
  1462. }
  1463. static VMBUS_CHAN_ATTR(intr_in_full, 0444, channel_intr_in_full_show, NULL);
  1464. static ssize_t channel_intr_out_empty_show(struct vmbus_channel *channel,
  1465. char *buf)
  1466. {
  1467. return sprintf(buf, "%llu\n",
  1468. (unsigned long long)channel->intr_out_empty);
  1469. }
  1470. static VMBUS_CHAN_ATTR(intr_out_empty, 0444, channel_intr_out_empty_show, NULL);
  1471. static ssize_t channel_out_full_first_show(struct vmbus_channel *channel,
  1472. char *buf)
  1473. {
  1474. return sprintf(buf, "%llu\n",
  1475. (unsigned long long)channel->out_full_first);
  1476. }
  1477. static VMBUS_CHAN_ATTR(out_full_first, 0444, channel_out_full_first_show, NULL);
  1478. static ssize_t channel_out_full_total_show(struct vmbus_channel *channel,
  1479. char *buf)
  1480. {
  1481. return sprintf(buf, "%llu\n",
  1482. (unsigned long long)channel->out_full_total);
  1483. }
  1484. static VMBUS_CHAN_ATTR(out_full_total, 0444, channel_out_full_total_show, NULL);
  1485. static ssize_t subchannel_monitor_id_show(struct vmbus_channel *channel,
  1486. char *buf)
  1487. {
  1488. return sprintf(buf, "%u\n", channel->offermsg.monitorid);
  1489. }
  1490. static VMBUS_CHAN_ATTR(monitor_id, 0444, subchannel_monitor_id_show, NULL);
  1491. static ssize_t subchannel_id_show(struct vmbus_channel *channel,
  1492. char *buf)
  1493. {
  1494. return sprintf(buf, "%u\n",
  1495. channel->offermsg.offer.sub_channel_index);
  1496. }
  1497. static VMBUS_CHAN_ATTR_RO(subchannel_id);
  1498. static struct attribute *vmbus_chan_attrs[] = {
  1499. &chan_attr_out_mask.attr,
  1500. &chan_attr_in_mask.attr,
  1501. &chan_attr_read_avail.attr,
  1502. &chan_attr_write_avail.attr,
  1503. &chan_attr_cpu.attr,
  1504. &chan_attr_pending.attr,
  1505. &chan_attr_latency.attr,
  1506. &chan_attr_interrupts.attr,
  1507. &chan_attr_events.attr,
  1508. &chan_attr_intr_in_full.attr,
  1509. &chan_attr_intr_out_empty.attr,
  1510. &chan_attr_out_full_first.attr,
  1511. &chan_attr_out_full_total.attr,
  1512. &chan_attr_monitor_id.attr,
  1513. &chan_attr_subchannel_id.attr,
  1514. NULL
  1515. };
  1516. /*
  1517. * Channel-level attribute_group callback function. Returns the permission for
  1518. * each attribute, and returns 0 if an attribute is not visible.
  1519. */
  1520. static umode_t vmbus_chan_attr_is_visible(struct kobject *kobj,
  1521. struct attribute *attr, int idx)
  1522. {
  1523. const struct vmbus_channel *channel =
  1524. container_of(kobj, struct vmbus_channel, kobj);
  1525. /* Hide the monitor attributes if the monitor mechanism is not used. */
  1526. if (!channel->offermsg.monitor_allocated &&
  1527. (attr == &chan_attr_pending.attr ||
  1528. attr == &chan_attr_latency.attr ||
  1529. attr == &chan_attr_monitor_id.attr))
  1530. return 0;
  1531. return attr->mode;
  1532. }
  1533. static const struct attribute_group vmbus_chan_group = {
  1534. .attrs = vmbus_chan_attrs,
  1535. .is_visible = vmbus_chan_attr_is_visible
  1536. };
  1537. static const struct kobj_type vmbus_chan_ktype = {
  1538. .sysfs_ops = &vmbus_chan_sysfs_ops,
  1539. .release = vmbus_chan_release,
  1540. };
  1541. /*
  1542. * vmbus_add_channel_kobj - setup a sub-directory under device/channels
  1543. */
  1544. int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
  1545. {
  1546. const struct device *device = &dev->device;
  1547. struct kobject *kobj = &channel->kobj;
  1548. u32 relid = channel->offermsg.child_relid;
  1549. int ret;
  1550. kobj->kset = dev->channels_kset;
  1551. ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
  1552. "%u", relid);
  1553. if (ret) {
  1554. kobject_put(kobj);
  1555. return ret;
  1556. }
  1557. ret = sysfs_create_group(kobj, &vmbus_chan_group);
  1558. if (ret) {
  1559. /*
  1560. * The calling functions' error handling paths will cleanup the
  1561. * empty channel directory.
  1562. */
  1563. kobject_put(kobj);
  1564. dev_err(device, "Unable to set up channel sysfs files\n");
  1565. return ret;
  1566. }
  1567. kobject_uevent(kobj, KOBJ_ADD);
  1568. return 0;
  1569. }
  1570. /*
  1571. * vmbus_remove_channel_attr_group - remove the channel's attribute group
  1572. */
  1573. void vmbus_remove_channel_attr_group(struct vmbus_channel *channel)
  1574. {
  1575. sysfs_remove_group(&channel->kobj, &vmbus_chan_group);
  1576. }
  1577. /*
  1578. * vmbus_device_create - Creates and registers a new child device
  1579. * on the vmbus.
  1580. */
  1581. struct hv_device *vmbus_device_create(const guid_t *type,
  1582. const guid_t *instance,
  1583. struct vmbus_channel *channel)
  1584. {
  1585. struct hv_device *child_device_obj;
  1586. child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
  1587. if (!child_device_obj) {
  1588. pr_err("Unable to allocate device object for child device\n");
  1589. return NULL;
  1590. }
  1591. child_device_obj->channel = channel;
  1592. guid_copy(&child_device_obj->dev_type, type);
  1593. guid_copy(&child_device_obj->dev_instance, instance);
  1594. child_device_obj->vendor_id = PCI_VENDOR_ID_MICROSOFT;
  1595. return child_device_obj;
  1596. }
  1597. /*
  1598. * vmbus_device_register - Register the child device
  1599. */
  1600. int vmbus_device_register(struct hv_device *child_device_obj)
  1601. {
  1602. struct kobject *kobj = &child_device_obj->device.kobj;
  1603. int ret;
  1604. dev_set_name(&child_device_obj->device, "%pUl",
  1605. &child_device_obj->channel->offermsg.offer.if_instance);
  1606. child_device_obj->device.bus = &hv_bus;
  1607. child_device_obj->device.parent = hv_dev;
  1608. child_device_obj->device.release = vmbus_device_release;
  1609. child_device_obj->device.dma_parms = &child_device_obj->dma_parms;
  1610. child_device_obj->device.dma_mask = &child_device_obj->dma_mask;
  1611. dma_set_mask(&child_device_obj->device, DMA_BIT_MASK(64));
  1612. /*
  1613. * Register with the LDM. This will kick off the driver/device
  1614. * binding...which will eventually call vmbus_match() and vmbus_probe()
  1615. */
  1616. ret = device_register(&child_device_obj->device);
  1617. if (ret) {
  1618. pr_err("Unable to register child device\n");
  1619. put_device(&child_device_obj->device);
  1620. return ret;
  1621. }
  1622. child_device_obj->channels_kset = kset_create_and_add("channels",
  1623. NULL, kobj);
  1624. if (!child_device_obj->channels_kset) {
  1625. ret = -ENOMEM;
  1626. goto err_dev_unregister;
  1627. }
  1628. ret = vmbus_add_channel_kobj(child_device_obj,
  1629. child_device_obj->channel);
  1630. if (ret) {
  1631. pr_err("Unable to register primary channeln");
  1632. goto err_kset_unregister;
  1633. }
  1634. hv_debug_add_dev_dir(child_device_obj);
  1635. return 0;
  1636. err_kset_unregister:
  1637. kset_unregister(child_device_obj->channels_kset);
  1638. err_dev_unregister:
  1639. device_unregister(&child_device_obj->device);
  1640. return ret;
  1641. }
  1642. /*
  1643. * vmbus_device_unregister - Remove the specified child device
  1644. * from the vmbus.
  1645. */
  1646. void vmbus_device_unregister(struct hv_device *device_obj)
  1647. {
  1648. pr_debug("child device %s unregistered\n",
  1649. dev_name(&device_obj->device));
  1650. kset_unregister(device_obj->channels_kset);
  1651. /*
  1652. * Kick off the process of unregistering the device.
  1653. * This will call vmbus_remove() and eventually vmbus_device_release()
  1654. */
  1655. device_unregister(&device_obj->device);
  1656. }
  1657. EXPORT_SYMBOL_GPL(vmbus_device_unregister);
  1658. #ifdef CONFIG_ACPI
  1659. /*
  1660. * VMBUS is an acpi enumerated device. Get the information we
  1661. * need from DSDT.
  1662. */
  1663. static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
  1664. {
  1665. resource_size_t start = 0;
  1666. resource_size_t end = 0;
  1667. struct resource *new_res;
  1668. struct resource **old_res = &hyperv_mmio;
  1669. struct resource **prev_res = NULL;
  1670. struct resource r;
  1671. switch (res->type) {
  1672. /*
  1673. * "Address" descriptors are for bus windows. Ignore
  1674. * "memory" descriptors, which are for registers on
  1675. * devices.
  1676. */
  1677. case ACPI_RESOURCE_TYPE_ADDRESS32:
  1678. start = res->data.address32.address.minimum;
  1679. end = res->data.address32.address.maximum;
  1680. break;
  1681. case ACPI_RESOURCE_TYPE_ADDRESS64:
  1682. start = res->data.address64.address.minimum;
  1683. end = res->data.address64.address.maximum;
  1684. break;
  1685. /*
  1686. * The IRQ information is needed only on ARM64, which Hyper-V
  1687. * sets up in the extended format. IRQ information is present
  1688. * on x86/x64 in the non-extended format but it is not used by
  1689. * Linux. So don't bother checking for the non-extended format.
  1690. */
  1691. case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
  1692. if (!acpi_dev_resource_interrupt(res, 0, &r)) {
  1693. pr_err("Unable to parse Hyper-V ACPI interrupt\n");
  1694. return AE_ERROR;
  1695. }
  1696. /* ARM64 INTID for VMbus */
  1697. vmbus_interrupt = res->data.extended_irq.interrupts[0];
  1698. /* Linux IRQ number */
  1699. vmbus_irq = r.start;
  1700. return AE_OK;
  1701. default:
  1702. /* Unused resource type */
  1703. return AE_OK;
  1704. }
  1705. /*
  1706. * Ignore ranges that are below 1MB, as they're not
  1707. * necessary or useful here.
  1708. */
  1709. if (end < 0x100000)
  1710. return AE_OK;
  1711. new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
  1712. if (!new_res)
  1713. return AE_NO_MEMORY;
  1714. /* If this range overlaps the virtual TPM, truncate it. */
  1715. if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
  1716. end = VTPM_BASE_ADDRESS;
  1717. new_res->name = "hyperv mmio";
  1718. new_res->flags = IORESOURCE_MEM;
  1719. new_res->start = start;
  1720. new_res->end = end;
  1721. /*
  1722. * If two ranges are adjacent, merge them.
  1723. */
  1724. do {
  1725. if (!*old_res) {
  1726. *old_res = new_res;
  1727. break;
  1728. }
  1729. if (((*old_res)->end + 1) == new_res->start) {
  1730. (*old_res)->end = new_res->end;
  1731. kfree(new_res);
  1732. break;
  1733. }
  1734. if ((*old_res)->start == new_res->end + 1) {
  1735. (*old_res)->start = new_res->start;
  1736. kfree(new_res);
  1737. break;
  1738. }
  1739. if ((*old_res)->start > new_res->end) {
  1740. new_res->sibling = *old_res;
  1741. if (prev_res)
  1742. (*prev_res)->sibling = new_res;
  1743. *old_res = new_res;
  1744. break;
  1745. }
  1746. prev_res = old_res;
  1747. old_res = &(*old_res)->sibling;
  1748. } while (1);
  1749. return AE_OK;
  1750. }
  1751. #endif
  1752. static void vmbus_mmio_remove(void)
  1753. {
  1754. struct resource *cur_res;
  1755. struct resource *next_res;
  1756. if (hyperv_mmio) {
  1757. if (fb_mmio) {
  1758. __release_region(hyperv_mmio, fb_mmio->start,
  1759. resource_size(fb_mmio));
  1760. fb_mmio = NULL;
  1761. }
  1762. for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
  1763. next_res = cur_res->sibling;
  1764. kfree(cur_res);
  1765. }
  1766. }
  1767. }
  1768. static void __maybe_unused vmbus_reserve_fb(void)
  1769. {
  1770. resource_size_t start = 0, size;
  1771. struct pci_dev *pdev;
  1772. if (efi_enabled(EFI_BOOT)) {
  1773. /* Gen2 VM: get FB base from EFI framebuffer */
  1774. if (IS_ENABLED(CONFIG_SYSFB)) {
  1775. start = screen_info.lfb_base;
  1776. size = max_t(__u32, screen_info.lfb_size, 0x800000);
  1777. }
  1778. } else {
  1779. /* Gen1 VM: get FB base from PCI */
  1780. pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
  1781. PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
  1782. if (!pdev)
  1783. return;
  1784. if (pdev->resource[0].flags & IORESOURCE_MEM) {
  1785. start = pci_resource_start(pdev, 0);
  1786. size = pci_resource_len(pdev, 0);
  1787. }
  1788. /*
  1789. * Release the PCI device so hyperv_drm or hyperv_fb driver can
  1790. * grab it later.
  1791. */
  1792. pci_dev_put(pdev);
  1793. }
  1794. if (!start)
  1795. return;
  1796. /*
  1797. * Make a claim for the frame buffer in the resource tree under the
  1798. * first node, which will be the one below 4GB. The length seems to
  1799. * be underreported, particularly in a Generation 1 VM. So start out
  1800. * reserving a larger area and make it smaller until it succeeds.
  1801. */
  1802. for (; !fb_mmio && (size >= 0x100000); size >>= 1)
  1803. fb_mmio = __request_region(hyperv_mmio, start, size, fb_mmio_name, 0);
  1804. }
  1805. /**
  1806. * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
  1807. * @new: If successful, supplied a pointer to the
  1808. * allocated MMIO space.
  1809. * @device_obj: Identifies the caller
  1810. * @min: Minimum guest physical address of the
  1811. * allocation
  1812. * @max: Maximum guest physical address
  1813. * @size: Size of the range to be allocated
  1814. * @align: Alignment of the range to be allocated
  1815. * @fb_overlap_ok: Whether this allocation can be allowed
  1816. * to overlap the video frame buffer.
  1817. *
  1818. * This function walks the resources granted to VMBus by the
  1819. * _CRS object in the ACPI namespace underneath the parent
  1820. * "bridge" whether that's a root PCI bus in the Generation 1
  1821. * case or a Module Device in the Generation 2 case. It then
  1822. * attempts to allocate from the global MMIO pool in a way that
  1823. * matches the constraints supplied in these parameters and by
  1824. * that _CRS.
  1825. *
  1826. * Return: 0 on success, -errno on failure
  1827. */
  1828. int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
  1829. resource_size_t min, resource_size_t max,
  1830. resource_size_t size, resource_size_t align,
  1831. bool fb_overlap_ok)
  1832. {
  1833. struct resource *iter, *shadow;
  1834. resource_size_t range_min, range_max, start, end;
  1835. const char *dev_n = dev_name(&device_obj->device);
  1836. int retval;
  1837. retval = -ENXIO;
  1838. mutex_lock(&hyperv_mmio_lock);
  1839. /*
  1840. * If overlaps with frame buffers are allowed, then first attempt to
  1841. * make the allocation from within the reserved region. Because it
  1842. * is already reserved, no shadow allocation is necessary.
  1843. */
  1844. if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
  1845. !(max < fb_mmio->start)) {
  1846. range_min = fb_mmio->start;
  1847. range_max = fb_mmio->end;
  1848. start = (range_min + align - 1) & ~(align - 1);
  1849. for (; start + size - 1 <= range_max; start += align) {
  1850. *new = request_mem_region_exclusive(start, size, dev_n);
  1851. if (*new) {
  1852. retval = 0;
  1853. goto exit;
  1854. }
  1855. }
  1856. }
  1857. for (iter = hyperv_mmio; iter; iter = iter->sibling) {
  1858. if ((iter->start >= max) || (iter->end <= min))
  1859. continue;
  1860. range_min = iter->start;
  1861. range_max = iter->end;
  1862. start = (range_min + align - 1) & ~(align - 1);
  1863. for (; start + size - 1 <= range_max; start += align) {
  1864. end = start + size - 1;
  1865. /* Skip the whole fb_mmio region if not fb_overlap_ok */
  1866. if (!fb_overlap_ok && fb_mmio &&
  1867. (((start >= fb_mmio->start) && (start <= fb_mmio->end)) ||
  1868. ((end >= fb_mmio->start) && (end <= fb_mmio->end))))
  1869. continue;
  1870. shadow = __request_region(iter, start, size, NULL,
  1871. IORESOURCE_BUSY);
  1872. if (!shadow)
  1873. continue;
  1874. *new = request_mem_region_exclusive(start, size, dev_n);
  1875. if (*new) {
  1876. shadow->name = (char *)*new;
  1877. retval = 0;
  1878. goto exit;
  1879. }
  1880. __release_region(iter, start, size);
  1881. }
  1882. }
  1883. exit:
  1884. mutex_unlock(&hyperv_mmio_lock);
  1885. return retval;
  1886. }
  1887. EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
  1888. /**
  1889. * vmbus_free_mmio() - Free a memory-mapped I/O range.
  1890. * @start: Base address of region to release.
  1891. * @size: Size of the range to be allocated
  1892. *
  1893. * This function releases anything requested by
  1894. * vmbus_mmio_allocate().
  1895. */
  1896. void vmbus_free_mmio(resource_size_t start, resource_size_t size)
  1897. {
  1898. struct resource *iter;
  1899. mutex_lock(&hyperv_mmio_lock);
  1900. for (iter = hyperv_mmio; iter; iter = iter->sibling) {
  1901. if ((iter->start >= start + size) || (iter->end <= start))
  1902. continue;
  1903. __release_region(iter, start, size);
  1904. }
  1905. release_mem_region(start, size);
  1906. mutex_unlock(&hyperv_mmio_lock);
  1907. }
  1908. EXPORT_SYMBOL_GPL(vmbus_free_mmio);
  1909. #ifdef CONFIG_ACPI
  1910. static int vmbus_acpi_add(struct platform_device *pdev)
  1911. {
  1912. acpi_status result;
  1913. int ret_val = -ENODEV;
  1914. struct acpi_device *ancestor;
  1915. struct acpi_device *device = ACPI_COMPANION(&pdev->dev);
  1916. hv_dev = &device->dev;
  1917. /*
  1918. * Older versions of Hyper-V for ARM64 fail to include the _CCA
  1919. * method on the top level VMbus device in the DSDT. But devices
  1920. * are hardware coherent in all current Hyper-V use cases, so fix
  1921. * up the ACPI device to behave as if _CCA is present and indicates
  1922. * hardware coherence.
  1923. */
  1924. ACPI_COMPANION_SET(&device->dev, device);
  1925. if (IS_ENABLED(CONFIG_ACPI_CCA_REQUIRED) &&
  1926. device_get_dma_attr(&device->dev) == DEV_DMA_NOT_SUPPORTED) {
  1927. pr_info("No ACPI _CCA found; assuming coherent device I/O\n");
  1928. device->flags.cca_seen = true;
  1929. device->flags.coherent_dma = true;
  1930. }
  1931. result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  1932. vmbus_walk_resources, NULL);
  1933. if (ACPI_FAILURE(result))
  1934. goto acpi_walk_err;
  1935. /*
  1936. * Some ancestor of the vmbus acpi device (Gen1 or Gen2
  1937. * firmware) is the VMOD that has the mmio ranges. Get that.
  1938. */
  1939. for (ancestor = acpi_dev_parent(device);
  1940. ancestor && ancestor->handle != ACPI_ROOT_OBJECT;
  1941. ancestor = acpi_dev_parent(ancestor)) {
  1942. result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
  1943. vmbus_walk_resources, NULL);
  1944. if (ACPI_FAILURE(result))
  1945. continue;
  1946. if (hyperv_mmio) {
  1947. vmbus_reserve_fb();
  1948. break;
  1949. }
  1950. }
  1951. ret_val = 0;
  1952. acpi_walk_err:
  1953. if (ret_val)
  1954. vmbus_mmio_remove();
  1955. return ret_val;
  1956. }
  1957. #else
  1958. static int vmbus_acpi_add(struct platform_device *pdev)
  1959. {
  1960. return 0;
  1961. }
  1962. #endif
  1963. static int vmbus_device_add(struct platform_device *pdev)
  1964. {
  1965. struct resource **cur_res = &hyperv_mmio;
  1966. struct of_range range;
  1967. struct of_range_parser parser;
  1968. struct device_node *np = pdev->dev.of_node;
  1969. int ret;
  1970. hv_dev = &pdev->dev;
  1971. ret = of_range_parser_init(&parser, np);
  1972. if (ret)
  1973. return ret;
  1974. for_each_of_range(&parser, &range) {
  1975. struct resource *res;
  1976. res = kzalloc(sizeof(*res), GFP_KERNEL);
  1977. if (!res) {
  1978. vmbus_mmio_remove();
  1979. return -ENOMEM;
  1980. }
  1981. res->name = "hyperv mmio";
  1982. res->flags = range.flags;
  1983. res->start = range.cpu_addr;
  1984. res->end = range.cpu_addr + range.size;
  1985. *cur_res = res;
  1986. cur_res = &res->sibling;
  1987. }
  1988. return ret;
  1989. }
  1990. static int vmbus_platform_driver_probe(struct platform_device *pdev)
  1991. {
  1992. if (acpi_disabled)
  1993. return vmbus_device_add(pdev);
  1994. else
  1995. return vmbus_acpi_add(pdev);
  1996. }
  1997. static void vmbus_platform_driver_remove(struct platform_device *pdev)
  1998. {
  1999. vmbus_mmio_remove();
  2000. }
  2001. #ifdef CONFIG_PM_SLEEP
  2002. static int vmbus_bus_suspend(struct device *dev)
  2003. {
  2004. struct hv_per_cpu_context *hv_cpu = per_cpu_ptr(
  2005. hv_context.cpu_context, VMBUS_CONNECT_CPU);
  2006. struct vmbus_channel *channel, *sc;
  2007. tasklet_disable(&hv_cpu->msg_dpc);
  2008. vmbus_connection.ignore_any_offer_msg = true;
  2009. /* The tasklet_enable() takes care of providing a memory barrier */
  2010. tasklet_enable(&hv_cpu->msg_dpc);
  2011. /* Drain all the workqueues as we are in suspend */
  2012. drain_workqueue(vmbus_connection.rescind_work_queue);
  2013. drain_workqueue(vmbus_connection.work_queue);
  2014. drain_workqueue(vmbus_connection.handle_primary_chan_wq);
  2015. drain_workqueue(vmbus_connection.handle_sub_chan_wq);
  2016. mutex_lock(&vmbus_connection.channel_mutex);
  2017. list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
  2018. if (!is_hvsock_channel(channel))
  2019. continue;
  2020. vmbus_force_channel_rescinded(channel);
  2021. }
  2022. mutex_unlock(&vmbus_connection.channel_mutex);
  2023. /*
  2024. * Wait until all the sub-channels and hv_sock channels have been
  2025. * cleaned up. Sub-channels should be destroyed upon suspend, otherwise
  2026. * they would conflict with the new sub-channels that will be created
  2027. * in the resume path. hv_sock channels should also be destroyed, but
  2028. * a hv_sock channel of an established hv_sock connection can not be
  2029. * really destroyed since it may still be referenced by the userspace
  2030. * application, so we just force the hv_sock channel to be rescinded
  2031. * by vmbus_force_channel_rescinded(), and the userspace application
  2032. * will thoroughly destroy the channel after hibernation.
  2033. *
  2034. * Note: the counter nr_chan_close_on_suspend may never go above 0 if
  2035. * the VM has no sub-channel and hv_sock channel, e.g. a 1-vCPU VM.
  2036. */
  2037. if (atomic_read(&vmbus_connection.nr_chan_close_on_suspend) > 0)
  2038. wait_for_completion(&vmbus_connection.ready_for_suspend_event);
  2039. if (atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) != 0) {
  2040. pr_err("Can not suspend due to a previous failed resuming\n");
  2041. return -EBUSY;
  2042. }
  2043. mutex_lock(&vmbus_connection.channel_mutex);
  2044. list_for_each_entry(channel, &vmbus_connection.chn_list, listentry) {
  2045. /*
  2046. * Remove the channel from the array of channels and invalidate
  2047. * the channel's relid. Upon resume, vmbus_onoffer() will fix
  2048. * up the relid (and other fields, if necessary) and add the
  2049. * channel back to the array.
  2050. */
  2051. vmbus_channel_unmap_relid(channel);
  2052. channel->offermsg.child_relid = INVALID_RELID;
  2053. if (is_hvsock_channel(channel)) {
  2054. if (!channel->rescind) {
  2055. pr_err("hv_sock channel not rescinded!\n");
  2056. WARN_ON_ONCE(1);
  2057. }
  2058. continue;
  2059. }
  2060. list_for_each_entry(sc, &channel->sc_list, sc_list) {
  2061. pr_err("Sub-channel not deleted!\n");
  2062. WARN_ON_ONCE(1);
  2063. }
  2064. atomic_inc(&vmbus_connection.nr_chan_fixup_on_resume);
  2065. }
  2066. mutex_unlock(&vmbus_connection.channel_mutex);
  2067. vmbus_initiate_unload(false);
  2068. /* Reset the event for the next resume. */
  2069. reinit_completion(&vmbus_connection.ready_for_resume_event);
  2070. return 0;
  2071. }
  2072. static int vmbus_bus_resume(struct device *dev)
  2073. {
  2074. struct vmbus_channel_msginfo *msginfo;
  2075. size_t msgsize;
  2076. int ret;
  2077. vmbus_connection.ignore_any_offer_msg = false;
  2078. /*
  2079. * We only use the 'vmbus_proto_version', which was in use before
  2080. * hibernation, to re-negotiate with the host.
  2081. */
  2082. if (!vmbus_proto_version) {
  2083. pr_err("Invalid proto version = 0x%x\n", vmbus_proto_version);
  2084. return -EINVAL;
  2085. }
  2086. msgsize = sizeof(*msginfo) +
  2087. sizeof(struct vmbus_channel_initiate_contact);
  2088. msginfo = kzalloc(msgsize, GFP_KERNEL);
  2089. if (msginfo == NULL)
  2090. return -ENOMEM;
  2091. ret = vmbus_negotiate_version(msginfo, vmbus_proto_version);
  2092. kfree(msginfo);
  2093. if (ret != 0)
  2094. return ret;
  2095. WARN_ON(atomic_read(&vmbus_connection.nr_chan_fixup_on_resume) == 0);
  2096. vmbus_request_offers();
  2097. if (wait_for_completion_timeout(
  2098. &vmbus_connection.ready_for_resume_event, 10 * HZ) == 0)
  2099. pr_err("Some vmbus device is missing after suspending?\n");
  2100. /* Reset the event for the next suspend. */
  2101. reinit_completion(&vmbus_connection.ready_for_suspend_event);
  2102. return 0;
  2103. }
  2104. #else
  2105. #define vmbus_bus_suspend NULL
  2106. #define vmbus_bus_resume NULL
  2107. #endif /* CONFIG_PM_SLEEP */
  2108. static const __maybe_unused struct of_device_id vmbus_of_match[] = {
  2109. {
  2110. .compatible = "microsoft,vmbus",
  2111. },
  2112. {
  2113. /* sentinel */
  2114. },
  2115. };
  2116. MODULE_DEVICE_TABLE(of, vmbus_of_match);
  2117. static const __maybe_unused struct acpi_device_id vmbus_acpi_device_ids[] = {
  2118. {"VMBUS", 0},
  2119. {"VMBus", 0},
  2120. {"", 0},
  2121. };
  2122. MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
  2123. /*
  2124. * Note: we must use the "no_irq" ops, otherwise hibernation can not work with
  2125. * PCI device assignment, because "pci_dev_pm_ops" uses the "noirq" ops: in
  2126. * the resume path, the pci "noirq" restore op runs before "non-noirq" op (see
  2127. * resume_target_kernel() -> dpm_resume_start(), and hibernation_restore() ->
  2128. * dpm_resume_end()). This means vmbus_bus_resume() and the pci-hyperv's
  2129. * resume callback must also run via the "noirq" ops.
  2130. *
  2131. * Set suspend_noirq/resume_noirq to NULL for Suspend-to-Idle: see the comment
  2132. * earlier in this file before vmbus_pm.
  2133. */
  2134. static const struct dev_pm_ops vmbus_bus_pm = {
  2135. .suspend_noirq = NULL,
  2136. .resume_noirq = NULL,
  2137. .freeze_noirq = vmbus_bus_suspend,
  2138. .thaw_noirq = vmbus_bus_resume,
  2139. .poweroff_noirq = vmbus_bus_suspend,
  2140. .restore_noirq = vmbus_bus_resume
  2141. };
  2142. static struct platform_driver vmbus_platform_driver = {
  2143. .probe = vmbus_platform_driver_probe,
  2144. .remove_new = vmbus_platform_driver_remove,
  2145. .driver = {
  2146. .name = "vmbus",
  2147. .acpi_match_table = ACPI_PTR(vmbus_acpi_device_ids),
  2148. .of_match_table = of_match_ptr(vmbus_of_match),
  2149. .pm = &vmbus_bus_pm,
  2150. .probe_type = PROBE_FORCE_SYNCHRONOUS,
  2151. }
  2152. };
  2153. static void hv_kexec_handler(void)
  2154. {
  2155. hv_stimer_global_cleanup();
  2156. vmbus_initiate_unload(false);
  2157. /* Make sure conn_state is set as hv_synic_cleanup checks for it */
  2158. mb();
  2159. cpuhp_remove_state(hyperv_cpuhp_online);
  2160. };
  2161. static void hv_crash_handler(struct pt_regs *regs)
  2162. {
  2163. int cpu;
  2164. vmbus_initiate_unload(true);
  2165. /*
  2166. * In crash handler we can't schedule synic cleanup for all CPUs,
  2167. * doing the cleanup for current CPU only. This should be sufficient
  2168. * for kdump.
  2169. */
  2170. cpu = smp_processor_id();
  2171. hv_stimer_cleanup(cpu);
  2172. hv_synic_disable_regs(cpu);
  2173. };
  2174. static int hv_synic_suspend(void)
  2175. {
  2176. /*
  2177. * When we reach here, all the non-boot CPUs have been offlined.
  2178. * If we're in a legacy configuration where stimer Direct Mode is
  2179. * not enabled, the stimers on the non-boot CPUs have been unbound
  2180. * in hv_synic_cleanup() -> hv_stimer_legacy_cleanup() ->
  2181. * hv_stimer_cleanup() -> clockevents_unbind_device().
  2182. *
  2183. * hv_synic_suspend() only runs on CPU0 with interrupts disabled.
  2184. * Here we do not call hv_stimer_legacy_cleanup() on CPU0 because:
  2185. * 1) it's unnecessary as interrupts remain disabled between
  2186. * syscore_suspend() and syscore_resume(): see create_image() and
  2187. * resume_target_kernel()
  2188. * 2) the stimer on CPU0 is automatically disabled later by
  2189. * syscore_suspend() -> timekeeping_suspend() -> tick_suspend() -> ...
  2190. * -> clockevents_shutdown() -> ... -> hv_ce_shutdown()
  2191. * 3) a warning would be triggered if we call
  2192. * clockevents_unbind_device(), which may sleep, in an
  2193. * interrupts-disabled context.
  2194. */
  2195. hv_synic_disable_regs(0);
  2196. return 0;
  2197. }
  2198. static void hv_synic_resume(void)
  2199. {
  2200. hv_synic_enable_regs(0);
  2201. /*
  2202. * Note: we don't need to call hv_stimer_init(0), because the timer
  2203. * on CPU0 is not unbound in hv_synic_suspend(), and the timer is
  2204. * automatically re-enabled in timekeeping_resume().
  2205. */
  2206. }
  2207. /* The callbacks run only on CPU0, with irqs_disabled. */
  2208. static struct syscore_ops hv_synic_syscore_ops = {
  2209. .suspend = hv_synic_suspend,
  2210. .resume = hv_synic_resume,
  2211. };
  2212. static int __init hv_acpi_init(void)
  2213. {
  2214. int ret;
  2215. if (!hv_is_hyperv_initialized())
  2216. return -ENODEV;
  2217. if (hv_root_partition && !hv_nested)
  2218. return 0;
  2219. /*
  2220. * Get ACPI resources first.
  2221. */
  2222. ret = platform_driver_register(&vmbus_platform_driver);
  2223. if (ret)
  2224. return ret;
  2225. if (!hv_dev) {
  2226. ret = -ENODEV;
  2227. goto cleanup;
  2228. }
  2229. /*
  2230. * If we're on an architecture with a hardcoded hypervisor
  2231. * vector (i.e. x86/x64), override the VMbus interrupt found
  2232. * in the ACPI tables. Ensure vmbus_irq is not set since the
  2233. * normal Linux IRQ mechanism is not used in this case.
  2234. */
  2235. #ifdef HYPERVISOR_CALLBACK_VECTOR
  2236. vmbus_interrupt = HYPERVISOR_CALLBACK_VECTOR;
  2237. vmbus_irq = -1;
  2238. #endif
  2239. hv_debug_init();
  2240. ret = vmbus_bus_init();
  2241. if (ret)
  2242. goto cleanup;
  2243. hv_setup_kexec_handler(hv_kexec_handler);
  2244. hv_setup_crash_handler(hv_crash_handler);
  2245. register_syscore_ops(&hv_synic_syscore_ops);
  2246. return 0;
  2247. cleanup:
  2248. platform_driver_unregister(&vmbus_platform_driver);
  2249. hv_dev = NULL;
  2250. return ret;
  2251. }
  2252. static void __exit vmbus_exit(void)
  2253. {
  2254. int cpu;
  2255. unregister_syscore_ops(&hv_synic_syscore_ops);
  2256. hv_remove_kexec_handler();
  2257. hv_remove_crash_handler();
  2258. vmbus_connection.conn_state = DISCONNECTED;
  2259. hv_stimer_global_cleanup();
  2260. vmbus_disconnect();
  2261. if (vmbus_irq == -1) {
  2262. hv_remove_vmbus_handler();
  2263. } else {
  2264. free_percpu_irq(vmbus_irq, vmbus_evt);
  2265. free_percpu(vmbus_evt);
  2266. }
  2267. for_each_online_cpu(cpu) {
  2268. struct hv_per_cpu_context *hv_cpu
  2269. = per_cpu_ptr(hv_context.cpu_context, cpu);
  2270. tasklet_kill(&hv_cpu->msg_dpc);
  2271. }
  2272. hv_debug_rm_all_dir();
  2273. vmbus_free_channels();
  2274. kfree(vmbus_connection.channels);
  2275. /*
  2276. * The vmbus panic notifier is always registered, hence we should
  2277. * also unconditionally unregister it here as well.
  2278. */
  2279. atomic_notifier_chain_unregister(&panic_notifier_list,
  2280. &hyperv_panic_vmbus_unload_block);
  2281. bus_unregister(&hv_bus);
  2282. cpuhp_remove_state(hyperv_cpuhp_online);
  2283. hv_synic_free();
  2284. platform_driver_unregister(&vmbus_platform_driver);
  2285. }
  2286. MODULE_LICENSE("GPL");
  2287. MODULE_DESCRIPTION("Microsoft Hyper-V VMBus Driver");
  2288. subsys_initcall(hv_acpi_init);
  2289. module_exit(vmbus_exit);