vmbus_drv.c 82 KB

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