vmbus_drv.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000
  1. /*
  2. * Copyright (c) 2009, Microsoft Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  15. * Place - Suite 330, Boston, MA 02111-1307 USA.
  16. *
  17. * Authors:
  18. * Haiyang Zhang <haiyangz@microsoft.com>
  19. * Hank Janssen <hjanssen@microsoft.com>
  20. * K. Y. Srinivasan <kys@microsoft.com>
  21. *
  22. */
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/device.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/sysctl.h>
  29. #include <linux/slab.h>
  30. #include <linux/acpi.h>
  31. #include <linux/completion.h>
  32. #include <linux/hyperv.h>
  33. #include <linux/kernel_stat.h>
  34. #include <linux/clockchips.h>
  35. #include <linux/cpu.h>
  36. #include <linux/sched/task_stack.h>
  37. #include <asm/mshyperv.h>
  38. #include <linux/notifier.h>
  39. #include <linux/ptrace.h>
  40. #include <linux/screen_info.h>
  41. #include <linux/kdebug.h>
  42. #include <linux/efi.h>
  43. #include <linux/random.h>
  44. #include "hyperv_vmbus.h"
  45. struct vmbus_dynid {
  46. struct list_head node;
  47. struct hv_vmbus_device_id id;
  48. };
  49. static struct acpi_device *hv_acpi_dev;
  50. static struct completion probe_event;
  51. static int hyperv_cpuhp_online;
  52. static void *hv_panic_page;
  53. static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
  54. void *args)
  55. {
  56. struct pt_regs *regs;
  57. regs = current_pt_regs();
  58. hyperv_report_panic(regs, val);
  59. return NOTIFY_DONE;
  60. }
  61. static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
  62. void *args)
  63. {
  64. struct die_args *die = (struct die_args *)args;
  65. struct pt_regs *regs = die->regs;
  66. hyperv_report_panic(regs, val);
  67. return NOTIFY_DONE;
  68. }
  69. static struct notifier_block hyperv_die_block = {
  70. .notifier_call = hyperv_die_event,
  71. };
  72. static struct notifier_block hyperv_panic_block = {
  73. .notifier_call = hyperv_panic_event,
  74. };
  75. static const char *fb_mmio_name = "fb_range";
  76. static struct resource *fb_mmio;
  77. static struct resource *hyperv_mmio;
  78. static DEFINE_SEMAPHORE(hyperv_mmio_lock);
  79. static int vmbus_exists(void)
  80. {
  81. if (hv_acpi_dev == NULL)
  82. return -ENODEV;
  83. return 0;
  84. }
  85. #define VMBUS_ALIAS_LEN ((sizeof((struct hv_vmbus_device_id *)0)->guid) * 2)
  86. static void print_alias_name(struct hv_device *hv_dev, char *alias_name)
  87. {
  88. int i;
  89. for (i = 0; i < VMBUS_ALIAS_LEN; i += 2)
  90. sprintf(&alias_name[i], "%02x", hv_dev->dev_type.b[i/2]);
  91. }
  92. static u8 channel_monitor_group(const struct vmbus_channel *channel)
  93. {
  94. return (u8)channel->offermsg.monitorid / 32;
  95. }
  96. static u8 channel_monitor_offset(const struct vmbus_channel *channel)
  97. {
  98. return (u8)channel->offermsg.monitorid % 32;
  99. }
  100. static u32 channel_pending(const struct vmbus_channel *channel,
  101. const struct hv_monitor_page *monitor_page)
  102. {
  103. u8 monitor_group = channel_monitor_group(channel);
  104. return monitor_page->trigger_group[monitor_group].pending;
  105. }
  106. static u32 channel_latency(const struct vmbus_channel *channel,
  107. const struct hv_monitor_page *monitor_page)
  108. {
  109. u8 monitor_group = channel_monitor_group(channel);
  110. u8 monitor_offset = channel_monitor_offset(channel);
  111. return monitor_page->latency[monitor_group][monitor_offset];
  112. }
  113. static u32 channel_conn_id(struct vmbus_channel *channel,
  114. struct hv_monitor_page *monitor_page)
  115. {
  116. u8 monitor_group = channel_monitor_group(channel);
  117. u8 monitor_offset = channel_monitor_offset(channel);
  118. return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
  119. }
  120. static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
  121. char *buf)
  122. {
  123. struct hv_device *hv_dev = device_to_hv_device(dev);
  124. if (!hv_dev->channel)
  125. return -ENODEV;
  126. return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
  127. }
  128. static DEVICE_ATTR_RO(id);
  129. static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
  130. char *buf)
  131. {
  132. struct hv_device *hv_dev = device_to_hv_device(dev);
  133. if (!hv_dev->channel)
  134. return -ENODEV;
  135. return sprintf(buf, "%d\n", hv_dev->channel->state);
  136. }
  137. static DEVICE_ATTR_RO(state);
  138. static ssize_t monitor_id_show(struct device *dev,
  139. struct device_attribute *dev_attr, char *buf)
  140. {
  141. struct hv_device *hv_dev = device_to_hv_device(dev);
  142. if (!hv_dev->channel)
  143. return -ENODEV;
  144. return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
  145. }
  146. static DEVICE_ATTR_RO(monitor_id);
  147. static ssize_t class_id_show(struct device *dev,
  148. struct device_attribute *dev_attr, char *buf)
  149. {
  150. struct hv_device *hv_dev = device_to_hv_device(dev);
  151. if (!hv_dev->channel)
  152. return -ENODEV;
  153. return sprintf(buf, "{%pUl}\n",
  154. hv_dev->channel->offermsg.offer.if_type.b);
  155. }
  156. static DEVICE_ATTR_RO(class_id);
  157. static ssize_t device_id_show(struct device *dev,
  158. struct device_attribute *dev_attr, char *buf)
  159. {
  160. struct hv_device *hv_dev = device_to_hv_device(dev);
  161. if (!hv_dev->channel)
  162. return -ENODEV;
  163. return sprintf(buf, "{%pUl}\n",
  164. hv_dev->channel->offermsg.offer.if_instance.b);
  165. }
  166. static DEVICE_ATTR_RO(device_id);
  167. static ssize_t modalias_show(struct device *dev,
  168. struct device_attribute *dev_attr, char *buf)
  169. {
  170. struct hv_device *hv_dev = device_to_hv_device(dev);
  171. char alias_name[VMBUS_ALIAS_LEN + 1];
  172. print_alias_name(hv_dev, alias_name);
  173. return sprintf(buf, "vmbus:%s\n", alias_name);
  174. }
  175. static DEVICE_ATTR_RO(modalias);
  176. #ifdef CONFIG_NUMA
  177. static ssize_t numa_node_show(struct device *dev,
  178. struct device_attribute *attr, char *buf)
  179. {
  180. struct hv_device *hv_dev = device_to_hv_device(dev);
  181. if (!hv_dev->channel)
  182. return -ENODEV;
  183. return sprintf(buf, "%d\n", hv_dev->channel->numa_node);
  184. }
  185. static DEVICE_ATTR_RO(numa_node);
  186. #endif
  187. static ssize_t server_monitor_pending_show(struct device *dev,
  188. struct device_attribute *dev_attr,
  189. char *buf)
  190. {
  191. struct hv_device *hv_dev = device_to_hv_device(dev);
  192. if (!hv_dev->channel)
  193. return -ENODEV;
  194. return sprintf(buf, "%d\n",
  195. channel_pending(hv_dev->channel,
  196. vmbus_connection.monitor_pages[1]));
  197. }
  198. static DEVICE_ATTR_RO(server_monitor_pending);
  199. static ssize_t client_monitor_pending_show(struct device *dev,
  200. struct device_attribute *dev_attr,
  201. char *buf)
  202. {
  203. struct hv_device *hv_dev = device_to_hv_device(dev);
  204. if (!hv_dev->channel)
  205. return -ENODEV;
  206. return sprintf(buf, "%d\n",
  207. channel_pending(hv_dev->channel,
  208. vmbus_connection.monitor_pages[1]));
  209. }
  210. static DEVICE_ATTR_RO(client_monitor_pending);
  211. static ssize_t server_monitor_latency_show(struct device *dev,
  212. struct device_attribute *dev_attr,
  213. char *buf)
  214. {
  215. struct hv_device *hv_dev = device_to_hv_device(dev);
  216. if (!hv_dev->channel)
  217. return -ENODEV;
  218. return sprintf(buf, "%d\n",
  219. 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 sprintf(buf, "%d\n",
  231. channel_latency(hv_dev->channel,
  232. vmbus_connection.monitor_pages[1]));
  233. }
  234. static DEVICE_ATTR_RO(client_monitor_latency);
  235. static ssize_t server_monitor_conn_id_show(struct device *dev,
  236. struct device_attribute *dev_attr,
  237. char *buf)
  238. {
  239. struct hv_device *hv_dev = device_to_hv_device(dev);
  240. if (!hv_dev->channel)
  241. return -ENODEV;
  242. return sprintf(buf, "%d\n",
  243. channel_conn_id(hv_dev->channel,
  244. vmbus_connection.monitor_pages[0]));
  245. }
  246. static DEVICE_ATTR_RO(server_monitor_conn_id);
  247. static ssize_t client_monitor_conn_id_show(struct device *dev,
  248. struct device_attribute *dev_attr,
  249. char *buf)
  250. {
  251. struct hv_device *hv_dev = device_to_hv_device(dev);
  252. if (!hv_dev->channel)
  253. return -ENODEV;
  254. return sprintf(buf, "%d\n",
  255. channel_conn_id(hv_dev->channel,
  256. vmbus_connection.monitor_pages[1]));
  257. }
  258. static DEVICE_ATTR_RO(client_monitor_conn_id);
  259. static ssize_t out_intr_mask_show(struct device *dev,
  260. struct device_attribute *dev_attr, char *buf)
  261. {
  262. struct hv_device *hv_dev = device_to_hv_device(dev);
  263. struct hv_ring_buffer_debug_info outbound;
  264. if (!hv_dev->channel)
  265. return -ENODEV;
  266. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  267. return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
  268. }
  269. static DEVICE_ATTR_RO(out_intr_mask);
  270. static ssize_t out_read_index_show(struct device *dev,
  271. struct device_attribute *dev_attr, char *buf)
  272. {
  273. struct hv_device *hv_dev = device_to_hv_device(dev);
  274. struct hv_ring_buffer_debug_info outbound;
  275. if (!hv_dev->channel)
  276. return -ENODEV;
  277. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  278. return sprintf(buf, "%d\n", outbound.current_read_index);
  279. }
  280. static DEVICE_ATTR_RO(out_read_index);
  281. static ssize_t out_write_index_show(struct device *dev,
  282. struct device_attribute *dev_attr,
  283. char *buf)
  284. {
  285. struct hv_device *hv_dev = device_to_hv_device(dev);
  286. struct hv_ring_buffer_debug_info outbound;
  287. if (!hv_dev->channel)
  288. return -ENODEV;
  289. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  290. return sprintf(buf, "%d\n", outbound.current_write_index);
  291. }
  292. static DEVICE_ATTR_RO(out_write_index);
  293. static ssize_t out_read_bytes_avail_show(struct device *dev,
  294. struct device_attribute *dev_attr,
  295. char *buf)
  296. {
  297. struct hv_device *hv_dev = device_to_hv_device(dev);
  298. struct hv_ring_buffer_debug_info outbound;
  299. if (!hv_dev->channel)
  300. return -ENODEV;
  301. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  302. return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
  303. }
  304. static DEVICE_ATTR_RO(out_read_bytes_avail);
  305. static ssize_t out_write_bytes_avail_show(struct device *dev,
  306. struct device_attribute *dev_attr,
  307. char *buf)
  308. {
  309. struct hv_device *hv_dev = device_to_hv_device(dev);
  310. struct hv_ring_buffer_debug_info outbound;
  311. if (!hv_dev->channel)
  312. return -ENODEV;
  313. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  314. return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
  315. }
  316. static DEVICE_ATTR_RO(out_write_bytes_avail);
  317. static ssize_t in_intr_mask_show(struct device *dev,
  318. struct device_attribute *dev_attr, char *buf)
  319. {
  320. struct hv_device *hv_dev = device_to_hv_device(dev);
  321. struct hv_ring_buffer_debug_info inbound;
  322. if (!hv_dev->channel)
  323. return -ENODEV;
  324. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  325. return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
  326. }
  327. static DEVICE_ATTR_RO(in_intr_mask);
  328. static ssize_t in_read_index_show(struct device *dev,
  329. struct device_attribute *dev_attr, char *buf)
  330. {
  331. struct hv_device *hv_dev = device_to_hv_device(dev);
  332. struct hv_ring_buffer_debug_info inbound;
  333. if (!hv_dev->channel)
  334. return -ENODEV;
  335. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  336. return sprintf(buf, "%d\n", inbound.current_read_index);
  337. }
  338. static DEVICE_ATTR_RO(in_read_index);
  339. static ssize_t in_write_index_show(struct device *dev,
  340. struct device_attribute *dev_attr, char *buf)
  341. {
  342. struct hv_device *hv_dev = device_to_hv_device(dev);
  343. struct hv_ring_buffer_debug_info inbound;
  344. if (!hv_dev->channel)
  345. return -ENODEV;
  346. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  347. return sprintf(buf, "%d\n", inbound.current_write_index);
  348. }
  349. static DEVICE_ATTR_RO(in_write_index);
  350. static ssize_t in_read_bytes_avail_show(struct device *dev,
  351. struct device_attribute *dev_attr,
  352. char *buf)
  353. {
  354. struct hv_device *hv_dev = device_to_hv_device(dev);
  355. struct hv_ring_buffer_debug_info inbound;
  356. if (!hv_dev->channel)
  357. return -ENODEV;
  358. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  359. return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
  360. }
  361. static DEVICE_ATTR_RO(in_read_bytes_avail);
  362. static ssize_t in_write_bytes_avail_show(struct device *dev,
  363. struct device_attribute *dev_attr,
  364. char *buf)
  365. {
  366. struct hv_device *hv_dev = device_to_hv_device(dev);
  367. struct hv_ring_buffer_debug_info inbound;
  368. if (!hv_dev->channel)
  369. return -ENODEV;
  370. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  371. return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
  372. }
  373. static DEVICE_ATTR_RO(in_write_bytes_avail);
  374. static ssize_t channel_vp_mapping_show(struct device *dev,
  375. struct device_attribute *dev_attr,
  376. char *buf)
  377. {
  378. struct hv_device *hv_dev = device_to_hv_device(dev);
  379. struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
  380. unsigned long flags;
  381. int buf_size = PAGE_SIZE, n_written, tot_written;
  382. struct list_head *cur;
  383. if (!channel)
  384. return -ENODEV;
  385. tot_written = snprintf(buf, buf_size, "%u:%u\n",
  386. channel->offermsg.child_relid, channel->target_cpu);
  387. spin_lock_irqsave(&channel->lock, flags);
  388. list_for_each(cur, &channel->sc_list) {
  389. if (tot_written >= buf_size - 1)
  390. break;
  391. cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
  392. n_written = scnprintf(buf + tot_written,
  393. buf_size - tot_written,
  394. "%u:%u\n",
  395. cur_sc->offermsg.child_relid,
  396. cur_sc->target_cpu);
  397. tot_written += n_written;
  398. }
  399. spin_unlock_irqrestore(&channel->lock, flags);
  400. return tot_written;
  401. }
  402. static DEVICE_ATTR_RO(channel_vp_mapping);
  403. static ssize_t vendor_show(struct device *dev,
  404. struct device_attribute *dev_attr,
  405. char *buf)
  406. {
  407. struct hv_device *hv_dev = device_to_hv_device(dev);
  408. return sprintf(buf, "0x%x\n", hv_dev->vendor_id);
  409. }
  410. static DEVICE_ATTR_RO(vendor);
  411. static ssize_t device_show(struct device *dev,
  412. struct device_attribute *dev_attr,
  413. char *buf)
  414. {
  415. struct hv_device *hv_dev = device_to_hv_device(dev);
  416. return sprintf(buf, "0x%x\n", hv_dev->device_id);
  417. }
  418. static DEVICE_ATTR_RO(device);
  419. static ssize_t driver_override_store(struct device *dev,
  420. struct device_attribute *attr,
  421. const char *buf, size_t count)
  422. {
  423. struct hv_device *hv_dev = device_to_hv_device(dev);
  424. char *driver_override, *old, *cp;
  425. /* We need to keep extra room for a newline */
  426. if (count >= (PAGE_SIZE - 1))
  427. return -EINVAL;
  428. driver_override = kstrndup(buf, count, GFP_KERNEL);
  429. if (!driver_override)
  430. return -ENOMEM;
  431. cp = strchr(driver_override, '\n');
  432. if (cp)
  433. *cp = '\0';
  434. device_lock(dev);
  435. old = hv_dev->driver_override;
  436. if (strlen(driver_override)) {
  437. hv_dev->driver_override = driver_override;
  438. } else {
  439. kfree(driver_override);
  440. hv_dev->driver_override = NULL;
  441. }
  442. device_unlock(dev);
  443. kfree(old);
  444. return count;
  445. }
  446. static ssize_t driver_override_show(struct device *dev,
  447. struct device_attribute *attr, char *buf)
  448. {
  449. struct hv_device *hv_dev = device_to_hv_device(dev);
  450. ssize_t len;
  451. device_lock(dev);
  452. len = snprintf(buf, PAGE_SIZE, "%s\n", hv_dev->driver_override);
  453. device_unlock(dev);
  454. return len;
  455. }
  456. static DEVICE_ATTR_RW(driver_override);
  457. /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
  458. static struct attribute *vmbus_dev_attrs[] = {
  459. &dev_attr_id.attr,
  460. &dev_attr_state.attr,
  461. &dev_attr_monitor_id.attr,
  462. &dev_attr_class_id.attr,
  463. &dev_attr_device_id.attr,
  464. &dev_attr_modalias.attr,
  465. #ifdef CONFIG_NUMA
  466. &dev_attr_numa_node.attr,
  467. #endif
  468. &dev_attr_server_monitor_pending.attr,
  469. &dev_attr_client_monitor_pending.attr,
  470. &dev_attr_server_monitor_latency.attr,
  471. &dev_attr_client_monitor_latency.attr,
  472. &dev_attr_server_monitor_conn_id.attr,
  473. &dev_attr_client_monitor_conn_id.attr,
  474. &dev_attr_out_intr_mask.attr,
  475. &dev_attr_out_read_index.attr,
  476. &dev_attr_out_write_index.attr,
  477. &dev_attr_out_read_bytes_avail.attr,
  478. &dev_attr_out_write_bytes_avail.attr,
  479. &dev_attr_in_intr_mask.attr,
  480. &dev_attr_in_read_index.attr,
  481. &dev_attr_in_write_index.attr,
  482. &dev_attr_in_read_bytes_avail.attr,
  483. &dev_attr_in_write_bytes_avail.attr,
  484. &dev_attr_channel_vp_mapping.attr,
  485. &dev_attr_vendor.attr,
  486. &dev_attr_device.attr,
  487. &dev_attr_driver_override.attr,
  488. NULL,
  489. };
  490. ATTRIBUTE_GROUPS(vmbus_dev);
  491. /*
  492. * vmbus_uevent - add uevent for our device
  493. *
  494. * This routine is invoked when a device is added or removed on the vmbus to
  495. * generate a uevent to udev in the userspace. The udev will then look at its
  496. * rule and the uevent generated here to load the appropriate driver
  497. *
  498. * The alias string will be of the form vmbus:guid where guid is the string
  499. * representation of the device guid (each byte of the guid will be
  500. * represented with two hex characters.
  501. */
  502. static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
  503. {
  504. struct hv_device *dev = device_to_hv_device(device);
  505. int ret;
  506. char alias_name[VMBUS_ALIAS_LEN + 1];
  507. print_alias_name(dev, alias_name);
  508. ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name);
  509. return ret;
  510. }
  511. static const uuid_le null_guid;
  512. static inline bool is_null_guid(const uuid_le *guid)
  513. {
  514. if (uuid_le_cmp(*guid, null_guid))
  515. return false;
  516. return true;
  517. }
  518. static const struct hv_vmbus_device_id *
  519. hv_vmbus_dev_match(const struct hv_vmbus_device_id *id, const uuid_le *guid)
  520. {
  521. if (id == NULL)
  522. return NULL; /* empty device table */
  523. for (; !is_null_guid(&id->guid); id++)
  524. if (!uuid_le_cmp(id->guid, *guid))
  525. return id;
  526. return NULL;
  527. }
  528. static const struct hv_vmbus_device_id *
  529. hv_vmbus_dynid_match(struct hv_driver *drv, const uuid_le *guid)
  530. {
  531. const struct hv_vmbus_device_id *id = NULL;
  532. struct vmbus_dynid *dynid;
  533. spin_lock(&drv->dynids.lock);
  534. list_for_each_entry(dynid, &drv->dynids.list, node) {
  535. if (!uuid_le_cmp(dynid->id.guid, *guid)) {
  536. id = &dynid->id;
  537. break;
  538. }
  539. }
  540. spin_unlock(&drv->dynids.lock);
  541. return id;
  542. }
  543. static const struct hv_vmbus_device_id vmbus_device_null = {
  544. .guid = NULL_UUID_LE,
  545. };
  546. /*
  547. * Return a matching hv_vmbus_device_id pointer.
  548. * If there is no match, return NULL.
  549. */
  550. static const struct hv_vmbus_device_id *hv_vmbus_get_id(struct hv_driver *drv,
  551. struct hv_device *dev)
  552. {
  553. const uuid_le *guid = &dev->dev_type;
  554. const struct hv_vmbus_device_id *id;
  555. /* When driver_override is set, only bind to the matching driver */
  556. if (dev->driver_override && strcmp(dev->driver_override, drv->name))
  557. return NULL;
  558. /* Look at the dynamic ids first, before the static ones */
  559. id = hv_vmbus_dynid_match(drv, guid);
  560. if (!id)
  561. id = hv_vmbus_dev_match(drv->id_table, guid);
  562. /* driver_override will always match, send a dummy id */
  563. if (!id && dev->driver_override)
  564. id = &vmbus_device_null;
  565. return id;
  566. }
  567. /* vmbus_add_dynid - add a new device ID to this driver and re-probe devices */
  568. static int vmbus_add_dynid(struct hv_driver *drv, uuid_le *guid)
  569. {
  570. struct vmbus_dynid *dynid;
  571. dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
  572. if (!dynid)
  573. return -ENOMEM;
  574. dynid->id.guid = *guid;
  575. spin_lock(&drv->dynids.lock);
  576. list_add_tail(&dynid->node, &drv->dynids.list);
  577. spin_unlock(&drv->dynids.lock);
  578. return driver_attach(&drv->driver);
  579. }
  580. static void vmbus_free_dynids(struct hv_driver *drv)
  581. {
  582. struct vmbus_dynid *dynid, *n;
  583. spin_lock(&drv->dynids.lock);
  584. list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
  585. list_del(&dynid->node);
  586. kfree(dynid);
  587. }
  588. spin_unlock(&drv->dynids.lock);
  589. }
  590. /*
  591. * store_new_id - sysfs frontend to vmbus_add_dynid()
  592. *
  593. * Allow GUIDs to be added to an existing driver via sysfs.
  594. */
  595. static ssize_t new_id_store(struct device_driver *driver, const char *buf,
  596. size_t count)
  597. {
  598. struct hv_driver *drv = drv_to_hv_drv(driver);
  599. uuid_le guid;
  600. ssize_t retval;
  601. retval = uuid_le_to_bin(buf, &guid);
  602. if (retval)
  603. return retval;
  604. if (hv_vmbus_dynid_match(drv, &guid))
  605. return -EEXIST;
  606. retval = vmbus_add_dynid(drv, &guid);
  607. if (retval)
  608. return retval;
  609. return count;
  610. }
  611. static DRIVER_ATTR_WO(new_id);
  612. /*
  613. * store_remove_id - remove a PCI device ID from this driver
  614. *
  615. * Removes a dynamic pci device ID to this driver.
  616. */
  617. static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
  618. size_t count)
  619. {
  620. struct hv_driver *drv = drv_to_hv_drv(driver);
  621. struct vmbus_dynid *dynid, *n;
  622. uuid_le guid;
  623. ssize_t retval;
  624. retval = uuid_le_to_bin(buf, &guid);
  625. if (retval)
  626. return retval;
  627. retval = -ENODEV;
  628. spin_lock(&drv->dynids.lock);
  629. list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
  630. struct hv_vmbus_device_id *id = &dynid->id;
  631. if (!uuid_le_cmp(id->guid, guid)) {
  632. list_del(&dynid->node);
  633. kfree(dynid);
  634. retval = count;
  635. break;
  636. }
  637. }
  638. spin_unlock(&drv->dynids.lock);
  639. return retval;
  640. }
  641. static DRIVER_ATTR_WO(remove_id);
  642. static struct attribute *vmbus_drv_attrs[] = {
  643. &driver_attr_new_id.attr,
  644. &driver_attr_remove_id.attr,
  645. NULL,
  646. };
  647. ATTRIBUTE_GROUPS(vmbus_drv);
  648. /*
  649. * vmbus_match - Attempt to match the specified device to the specified driver
  650. */
  651. static int vmbus_match(struct device *device, struct device_driver *driver)
  652. {
  653. struct hv_driver *drv = drv_to_hv_drv(driver);
  654. struct hv_device *hv_dev = device_to_hv_device(device);
  655. /* The hv_sock driver handles all hv_sock offers. */
  656. if (is_hvsock_channel(hv_dev->channel))
  657. return drv->hvsock;
  658. if (hv_vmbus_get_id(drv, hv_dev))
  659. return 1;
  660. return 0;
  661. }
  662. /*
  663. * vmbus_probe - Add the new vmbus's child device
  664. */
  665. static int vmbus_probe(struct device *child_device)
  666. {
  667. int ret = 0;
  668. struct hv_driver *drv =
  669. drv_to_hv_drv(child_device->driver);
  670. struct hv_device *dev = device_to_hv_device(child_device);
  671. const struct hv_vmbus_device_id *dev_id;
  672. dev_id = hv_vmbus_get_id(drv, dev);
  673. if (drv->probe) {
  674. ret = drv->probe(dev, dev_id);
  675. if (ret != 0)
  676. pr_err("probe failed for device %s (%d)\n",
  677. dev_name(child_device), ret);
  678. } else {
  679. pr_err("probe not set for driver %s\n",
  680. dev_name(child_device));
  681. ret = -ENODEV;
  682. }
  683. return ret;
  684. }
  685. /*
  686. * vmbus_remove - Remove a vmbus device
  687. */
  688. static int vmbus_remove(struct device *child_device)
  689. {
  690. struct hv_driver *drv;
  691. struct hv_device *dev = device_to_hv_device(child_device);
  692. if (child_device->driver) {
  693. drv = drv_to_hv_drv(child_device->driver);
  694. if (drv->remove)
  695. drv->remove(dev);
  696. }
  697. return 0;
  698. }
  699. /*
  700. * vmbus_shutdown - Shutdown a vmbus device
  701. */
  702. static void vmbus_shutdown(struct device *child_device)
  703. {
  704. struct hv_driver *drv;
  705. struct hv_device *dev = device_to_hv_device(child_device);
  706. /* The device may not be attached yet */
  707. if (!child_device->driver)
  708. return;
  709. drv = drv_to_hv_drv(child_device->driver);
  710. if (drv->shutdown)
  711. drv->shutdown(dev);
  712. }
  713. /*
  714. * vmbus_device_release - Final callback release of the vmbus child device
  715. */
  716. static void vmbus_device_release(struct device *device)
  717. {
  718. struct hv_device *hv_dev = device_to_hv_device(device);
  719. struct vmbus_channel *channel = hv_dev->channel;
  720. mutex_lock(&vmbus_connection.channel_mutex);
  721. hv_process_channel_removal(channel);
  722. mutex_unlock(&vmbus_connection.channel_mutex);
  723. kfree(hv_dev);
  724. }
  725. /* The one and only one */
  726. static struct bus_type hv_bus = {
  727. .name = "vmbus",
  728. .match = vmbus_match,
  729. .shutdown = vmbus_shutdown,
  730. .remove = vmbus_remove,
  731. .probe = vmbus_probe,
  732. .uevent = vmbus_uevent,
  733. .dev_groups = vmbus_dev_groups,
  734. .drv_groups = vmbus_drv_groups,
  735. };
  736. struct onmessage_work_context {
  737. struct work_struct work;
  738. struct hv_message msg;
  739. };
  740. static void vmbus_onmessage_work(struct work_struct *work)
  741. {
  742. struct onmessage_work_context *ctx;
  743. /* Do not process messages if we're in DISCONNECTED state */
  744. if (vmbus_connection.conn_state == DISCONNECTED)
  745. return;
  746. ctx = container_of(work, struct onmessage_work_context,
  747. work);
  748. vmbus_onmessage(&ctx->msg);
  749. kfree(ctx);
  750. }
  751. static void hv_process_timer_expiration(struct hv_message *msg,
  752. struct hv_per_cpu_context *hv_cpu)
  753. {
  754. struct clock_event_device *dev = hv_cpu->clk_evt;
  755. if (dev->event_handler)
  756. dev->event_handler(dev);
  757. vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
  758. }
  759. void vmbus_on_msg_dpc(unsigned long data)
  760. {
  761. struct hv_per_cpu_context *hv_cpu = (void *)data;
  762. void *page_addr = hv_cpu->synic_message_page;
  763. struct hv_message *msg = (struct hv_message *)page_addr +
  764. VMBUS_MESSAGE_SINT;
  765. struct vmbus_channel_message_header *hdr;
  766. const struct vmbus_channel_message_table_entry *entry;
  767. struct onmessage_work_context *ctx;
  768. u32 message_type = msg->header.message_type;
  769. if (message_type == HVMSG_NONE)
  770. /* no msg */
  771. return;
  772. hdr = (struct vmbus_channel_message_header *)msg->u.payload;
  773. trace_vmbus_on_msg_dpc(hdr);
  774. if (hdr->msgtype >= CHANNELMSG_COUNT) {
  775. WARN_ONCE(1, "unknown msgtype=%d\n", hdr->msgtype);
  776. goto msg_handled;
  777. }
  778. entry = &channel_message_table[hdr->msgtype];
  779. if (entry->handler_type == VMHT_BLOCKING) {
  780. ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
  781. if (ctx == NULL)
  782. return;
  783. INIT_WORK(&ctx->work, vmbus_onmessage_work);
  784. memcpy(&ctx->msg, msg, sizeof(*msg));
  785. /*
  786. * The host can generate a rescind message while we
  787. * may still be handling the original offer. We deal with
  788. * this condition by ensuring the processing is done on the
  789. * same CPU.
  790. */
  791. switch (hdr->msgtype) {
  792. case CHANNELMSG_RESCIND_CHANNELOFFER:
  793. /*
  794. * If we are handling the rescind message;
  795. * schedule the work on the global work queue.
  796. */
  797. schedule_work_on(vmbus_connection.connect_cpu,
  798. &ctx->work);
  799. break;
  800. case CHANNELMSG_OFFERCHANNEL:
  801. atomic_inc(&vmbus_connection.offer_in_progress);
  802. queue_work_on(vmbus_connection.connect_cpu,
  803. vmbus_connection.work_queue,
  804. &ctx->work);
  805. break;
  806. default:
  807. queue_work(vmbus_connection.work_queue, &ctx->work);
  808. }
  809. } else
  810. entry->message_handler(hdr);
  811. msg_handled:
  812. vmbus_signal_eom(msg, message_type);
  813. }
  814. /*
  815. * Direct callback for channels using other deferred processing
  816. */
  817. static void vmbus_channel_isr(struct vmbus_channel *channel)
  818. {
  819. void (*callback_fn)(void *);
  820. callback_fn = READ_ONCE(channel->onchannel_callback);
  821. if (likely(callback_fn != NULL))
  822. (*callback_fn)(channel->channel_callback_context);
  823. }
  824. /*
  825. * Schedule all channels with events pending
  826. */
  827. static void vmbus_chan_sched(struct hv_per_cpu_context *hv_cpu)
  828. {
  829. unsigned long *recv_int_page;
  830. u32 maxbits, relid;
  831. if (vmbus_proto_version < VERSION_WIN8) {
  832. maxbits = MAX_NUM_CHANNELS_SUPPORTED;
  833. recv_int_page = vmbus_connection.recv_int_page;
  834. } else {
  835. /*
  836. * When the host is win8 and beyond, the event page
  837. * can be directly checked to get the id of the channel
  838. * that has the interrupt pending.
  839. */
  840. void *page_addr = hv_cpu->synic_event_page;
  841. union hv_synic_event_flags *event
  842. = (union hv_synic_event_flags *)page_addr +
  843. VMBUS_MESSAGE_SINT;
  844. maxbits = HV_EVENT_FLAGS_COUNT;
  845. recv_int_page = event->flags;
  846. }
  847. if (unlikely(!recv_int_page))
  848. return;
  849. for_each_set_bit(relid, recv_int_page, maxbits) {
  850. struct vmbus_channel *channel;
  851. if (!sync_test_and_clear_bit(relid, recv_int_page))
  852. continue;
  853. /* Special case - vmbus channel protocol msg */
  854. if (relid == 0)
  855. continue;
  856. rcu_read_lock();
  857. /* Find channel based on relid */
  858. list_for_each_entry_rcu(channel, &hv_cpu->chan_list, percpu_list) {
  859. if (channel->offermsg.child_relid != relid)
  860. continue;
  861. if (channel->rescind)
  862. continue;
  863. trace_vmbus_chan_sched(channel);
  864. ++channel->interrupts;
  865. switch (channel->callback_mode) {
  866. case HV_CALL_ISR:
  867. vmbus_channel_isr(channel);
  868. break;
  869. case HV_CALL_BATCHED:
  870. hv_begin_read(&channel->inbound);
  871. /* fallthrough */
  872. case HV_CALL_DIRECT:
  873. tasklet_schedule(&channel->callback_event);
  874. }
  875. }
  876. rcu_read_unlock();
  877. }
  878. }
  879. static void vmbus_isr(void)
  880. {
  881. struct hv_per_cpu_context *hv_cpu
  882. = this_cpu_ptr(hv_context.cpu_context);
  883. void *page_addr = hv_cpu->synic_event_page;
  884. struct hv_message *msg;
  885. union hv_synic_event_flags *event;
  886. bool handled = false;
  887. if (unlikely(page_addr == NULL))
  888. return;
  889. event = (union hv_synic_event_flags *)page_addr +
  890. VMBUS_MESSAGE_SINT;
  891. /*
  892. * Check for events before checking for messages. This is the order
  893. * in which events and messages are checked in Windows guests on
  894. * Hyper-V, and the Windows team suggested we do the same.
  895. */
  896. if ((vmbus_proto_version == VERSION_WS2008) ||
  897. (vmbus_proto_version == VERSION_WIN7)) {
  898. /* Since we are a child, we only need to check bit 0 */
  899. if (sync_test_and_clear_bit(0, event->flags))
  900. handled = true;
  901. } else {
  902. /*
  903. * Our host is win8 or above. The signaling mechanism
  904. * has changed and we can directly look at the event page.
  905. * If bit n is set then we have an interrup on the channel
  906. * whose id is n.
  907. */
  908. handled = true;
  909. }
  910. if (handled)
  911. vmbus_chan_sched(hv_cpu);
  912. page_addr = hv_cpu->synic_message_page;
  913. msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
  914. /* Check if there are actual msgs to be processed */
  915. if (msg->header.message_type != HVMSG_NONE) {
  916. if (msg->header.message_type == HVMSG_TIMER_EXPIRED)
  917. hv_process_timer_expiration(msg, hv_cpu);
  918. else
  919. tasklet_schedule(&hv_cpu->msg_dpc);
  920. }
  921. add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0);
  922. }
  923. /*
  924. * Boolean to control whether to report panic messages over Hyper-V.
  925. *
  926. * It can be set via /proc/sys/kernel/hyperv/record_panic_msg
  927. */
  928. static int sysctl_record_panic_msg = 1;
  929. /*
  930. * Callback from kmsg_dump. Grab as much as possible from the end of the kmsg
  931. * buffer and call into Hyper-V to transfer the data.
  932. */
  933. static void hv_kmsg_dump(struct kmsg_dumper *dumper,
  934. enum kmsg_dump_reason reason)
  935. {
  936. size_t bytes_written;
  937. phys_addr_t panic_pa;
  938. /* We are only interested in panics. */
  939. if ((reason != KMSG_DUMP_PANIC) || (!sysctl_record_panic_msg))
  940. return;
  941. panic_pa = virt_to_phys(hv_panic_page);
  942. /*
  943. * Write dump contents to the page. No need to synchronize; panic should
  944. * be single-threaded.
  945. */
  946. kmsg_dump_get_buffer(dumper, true, hv_panic_page, PAGE_SIZE,
  947. &bytes_written);
  948. if (bytes_written)
  949. hyperv_report_panic_msg(panic_pa, bytes_written);
  950. }
  951. static struct kmsg_dumper hv_kmsg_dumper = {
  952. .dump = hv_kmsg_dump,
  953. };
  954. static struct ctl_table_header *hv_ctl_table_hdr;
  955. static int zero;
  956. static int one = 1;
  957. /*
  958. * sysctl option to allow the user to control whether kmsg data should be
  959. * reported to Hyper-V on panic.
  960. */
  961. static struct ctl_table hv_ctl_table[] = {
  962. {
  963. .procname = "hyperv_record_panic_msg",
  964. .data = &sysctl_record_panic_msg,
  965. .maxlen = sizeof(int),
  966. .mode = 0644,
  967. .proc_handler = proc_dointvec_minmax,
  968. .extra1 = &zero,
  969. .extra2 = &one
  970. },
  971. {}
  972. };
  973. static struct ctl_table hv_root_table[] = {
  974. {
  975. .procname = "kernel",
  976. .mode = 0555,
  977. .child = hv_ctl_table
  978. },
  979. {}
  980. };
  981. /*
  982. * vmbus_bus_init -Main vmbus driver initialization routine.
  983. *
  984. * Here, we
  985. * - initialize the vmbus driver context
  986. * - invoke the vmbus hv main init routine
  987. * - retrieve the channel offers
  988. */
  989. static int vmbus_bus_init(void)
  990. {
  991. int ret;
  992. /* Hypervisor initialization...setup hypercall page..etc */
  993. ret = hv_init();
  994. if (ret != 0) {
  995. pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
  996. return ret;
  997. }
  998. ret = bus_register(&hv_bus);
  999. if (ret)
  1000. return ret;
  1001. hv_setup_vmbus_irq(vmbus_isr);
  1002. ret = hv_synic_alloc();
  1003. if (ret)
  1004. goto err_alloc;
  1005. /*
  1006. * Initialize the per-cpu interrupt state and
  1007. * connect to the host.
  1008. */
  1009. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "hyperv/vmbus:online",
  1010. hv_synic_init, hv_synic_cleanup);
  1011. if (ret < 0)
  1012. goto err_alloc;
  1013. hyperv_cpuhp_online = ret;
  1014. ret = vmbus_connect();
  1015. if (ret)
  1016. goto err_connect;
  1017. /*
  1018. * Only register if the crash MSRs are available
  1019. */
  1020. if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
  1021. u64 hyperv_crash_ctl;
  1022. /*
  1023. * Sysctl registration is not fatal, since by default
  1024. * reporting is enabled.
  1025. */
  1026. hv_ctl_table_hdr = register_sysctl_table(hv_root_table);
  1027. if (!hv_ctl_table_hdr)
  1028. pr_err("Hyper-V: sysctl table register error");
  1029. /*
  1030. * Register for panic kmsg callback only if the right
  1031. * capability is supported by the hypervisor.
  1032. */
  1033. hv_get_crash_ctl(hyperv_crash_ctl);
  1034. if (hyperv_crash_ctl & HV_CRASH_CTL_CRASH_NOTIFY_MSG) {
  1035. hv_panic_page = (void *)get_zeroed_page(GFP_KERNEL);
  1036. if (hv_panic_page) {
  1037. ret = kmsg_dump_register(&hv_kmsg_dumper);
  1038. if (ret)
  1039. pr_err("Hyper-V: kmsg dump register "
  1040. "error 0x%x\n", ret);
  1041. } else
  1042. pr_err("Hyper-V: panic message page memory "
  1043. "allocation failed");
  1044. }
  1045. register_die_notifier(&hyperv_die_block);
  1046. atomic_notifier_chain_register(&panic_notifier_list,
  1047. &hyperv_panic_block);
  1048. }
  1049. vmbus_request_offers();
  1050. return 0;
  1051. err_connect:
  1052. cpuhp_remove_state(hyperv_cpuhp_online);
  1053. err_alloc:
  1054. hv_synic_free();
  1055. hv_remove_vmbus_irq();
  1056. bus_unregister(&hv_bus);
  1057. free_page((unsigned long)hv_panic_page);
  1058. unregister_sysctl_table(hv_ctl_table_hdr);
  1059. hv_ctl_table_hdr = NULL;
  1060. return ret;
  1061. }
  1062. /**
  1063. * __vmbus_child_driver_register() - Register a vmbus's driver
  1064. * @hv_driver: Pointer to driver structure you want to register
  1065. * @owner: owner module of the drv
  1066. * @mod_name: module name string
  1067. *
  1068. * Registers the given driver with Linux through the 'driver_register()' call
  1069. * and sets up the hyper-v vmbus handling for this driver.
  1070. * It will return the state of the 'driver_register()' call.
  1071. *
  1072. */
  1073. int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
  1074. {
  1075. int ret;
  1076. pr_info("registering driver %s\n", hv_driver->name);
  1077. ret = vmbus_exists();
  1078. if (ret < 0)
  1079. return ret;
  1080. hv_driver->driver.name = hv_driver->name;
  1081. hv_driver->driver.owner = owner;
  1082. hv_driver->driver.mod_name = mod_name;
  1083. hv_driver->driver.bus = &hv_bus;
  1084. spin_lock_init(&hv_driver->dynids.lock);
  1085. INIT_LIST_HEAD(&hv_driver->dynids.list);
  1086. ret = driver_register(&hv_driver->driver);
  1087. return ret;
  1088. }
  1089. EXPORT_SYMBOL_GPL(__vmbus_driver_register);
  1090. /**
  1091. * vmbus_driver_unregister() - Unregister a vmbus's driver
  1092. * @hv_driver: Pointer to driver structure you want to
  1093. * un-register
  1094. *
  1095. * Un-register the given driver that was previous registered with a call to
  1096. * vmbus_driver_register()
  1097. */
  1098. void vmbus_driver_unregister(struct hv_driver *hv_driver)
  1099. {
  1100. pr_info("unregistering driver %s\n", hv_driver->name);
  1101. if (!vmbus_exists()) {
  1102. driver_unregister(&hv_driver->driver);
  1103. vmbus_free_dynids(hv_driver);
  1104. }
  1105. }
  1106. EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
  1107. /*
  1108. * Called when last reference to channel is gone.
  1109. */
  1110. static void vmbus_chan_release(struct kobject *kobj)
  1111. {
  1112. struct vmbus_channel *channel
  1113. = container_of(kobj, struct vmbus_channel, kobj);
  1114. kfree_rcu(channel, rcu);
  1115. }
  1116. struct vmbus_chan_attribute {
  1117. struct attribute attr;
  1118. ssize_t (*show)(const struct vmbus_channel *chan, char *buf);
  1119. ssize_t (*store)(struct vmbus_channel *chan,
  1120. const char *buf, size_t count);
  1121. };
  1122. #define VMBUS_CHAN_ATTR(_name, _mode, _show, _store) \
  1123. struct vmbus_chan_attribute chan_attr_##_name \
  1124. = __ATTR(_name, _mode, _show, _store)
  1125. #define VMBUS_CHAN_ATTR_RW(_name) \
  1126. struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RW(_name)
  1127. #define VMBUS_CHAN_ATTR_RO(_name) \
  1128. struct vmbus_chan_attribute chan_attr_##_name = __ATTR_RO(_name)
  1129. #define VMBUS_CHAN_ATTR_WO(_name) \
  1130. struct vmbus_chan_attribute chan_attr_##_name = __ATTR_WO(_name)
  1131. static ssize_t vmbus_chan_attr_show(struct kobject *kobj,
  1132. struct attribute *attr, char *buf)
  1133. {
  1134. const struct vmbus_chan_attribute *attribute
  1135. = container_of(attr, struct vmbus_chan_attribute, attr);
  1136. const struct vmbus_channel *chan
  1137. = container_of(kobj, struct vmbus_channel, kobj);
  1138. if (!attribute->show)
  1139. return -EIO;
  1140. if (chan->state != CHANNEL_OPENED_STATE)
  1141. return -EINVAL;
  1142. return attribute->show(chan, buf);
  1143. }
  1144. static const struct sysfs_ops vmbus_chan_sysfs_ops = {
  1145. .show = vmbus_chan_attr_show,
  1146. };
  1147. static ssize_t out_mask_show(const struct vmbus_channel *channel, char *buf)
  1148. {
  1149. const struct hv_ring_buffer_info *rbi = &channel->outbound;
  1150. return sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
  1151. }
  1152. static VMBUS_CHAN_ATTR_RO(out_mask);
  1153. static ssize_t in_mask_show(const struct vmbus_channel *channel, char *buf)
  1154. {
  1155. const struct hv_ring_buffer_info *rbi = &channel->inbound;
  1156. return sprintf(buf, "%u\n", rbi->ring_buffer->interrupt_mask);
  1157. }
  1158. static VMBUS_CHAN_ATTR_RO(in_mask);
  1159. static ssize_t read_avail_show(const struct vmbus_channel *channel, char *buf)
  1160. {
  1161. const struct hv_ring_buffer_info *rbi = &channel->inbound;
  1162. return sprintf(buf, "%u\n", hv_get_bytes_to_read(rbi));
  1163. }
  1164. static VMBUS_CHAN_ATTR_RO(read_avail);
  1165. static ssize_t write_avail_show(const struct vmbus_channel *channel, char *buf)
  1166. {
  1167. const struct hv_ring_buffer_info *rbi = &channel->outbound;
  1168. return sprintf(buf, "%u\n", hv_get_bytes_to_write(rbi));
  1169. }
  1170. static VMBUS_CHAN_ATTR_RO(write_avail);
  1171. static ssize_t show_target_cpu(const struct vmbus_channel *channel, char *buf)
  1172. {
  1173. return sprintf(buf, "%u\n", channel->target_cpu);
  1174. }
  1175. static VMBUS_CHAN_ATTR(cpu, S_IRUGO, show_target_cpu, NULL);
  1176. static ssize_t channel_pending_show(const struct vmbus_channel *channel,
  1177. char *buf)
  1178. {
  1179. return sprintf(buf, "%d\n",
  1180. channel_pending(channel,
  1181. vmbus_connection.monitor_pages[1]));
  1182. }
  1183. static VMBUS_CHAN_ATTR(pending, S_IRUGO, channel_pending_show, NULL);
  1184. static ssize_t channel_latency_show(const struct vmbus_channel *channel,
  1185. char *buf)
  1186. {
  1187. return sprintf(buf, "%d\n",
  1188. channel_latency(channel,
  1189. vmbus_connection.monitor_pages[1]));
  1190. }
  1191. static VMBUS_CHAN_ATTR(latency, S_IRUGO, channel_latency_show, NULL);
  1192. static ssize_t channel_interrupts_show(const struct vmbus_channel *channel, char *buf)
  1193. {
  1194. return sprintf(buf, "%llu\n", channel->interrupts);
  1195. }
  1196. static VMBUS_CHAN_ATTR(interrupts, S_IRUGO, channel_interrupts_show, NULL);
  1197. static ssize_t channel_events_show(const struct vmbus_channel *channel, char *buf)
  1198. {
  1199. return sprintf(buf, "%llu\n", channel->sig_events);
  1200. }
  1201. static VMBUS_CHAN_ATTR(events, S_IRUGO, channel_events_show, NULL);
  1202. static ssize_t subchannel_monitor_id_show(const struct vmbus_channel *channel,
  1203. char *buf)
  1204. {
  1205. return sprintf(buf, "%u\n", channel->offermsg.monitorid);
  1206. }
  1207. static VMBUS_CHAN_ATTR(monitor_id, S_IRUGO, subchannel_monitor_id_show, NULL);
  1208. static ssize_t subchannel_id_show(const struct vmbus_channel *channel,
  1209. char *buf)
  1210. {
  1211. return sprintf(buf, "%u\n",
  1212. channel->offermsg.offer.sub_channel_index);
  1213. }
  1214. static VMBUS_CHAN_ATTR_RO(subchannel_id);
  1215. static struct attribute *vmbus_chan_attrs[] = {
  1216. &chan_attr_out_mask.attr,
  1217. &chan_attr_in_mask.attr,
  1218. &chan_attr_read_avail.attr,
  1219. &chan_attr_write_avail.attr,
  1220. &chan_attr_cpu.attr,
  1221. &chan_attr_pending.attr,
  1222. &chan_attr_latency.attr,
  1223. &chan_attr_interrupts.attr,
  1224. &chan_attr_events.attr,
  1225. &chan_attr_monitor_id.attr,
  1226. &chan_attr_subchannel_id.attr,
  1227. NULL
  1228. };
  1229. static struct kobj_type vmbus_chan_ktype = {
  1230. .sysfs_ops = &vmbus_chan_sysfs_ops,
  1231. .release = vmbus_chan_release,
  1232. .default_attrs = vmbus_chan_attrs,
  1233. };
  1234. /*
  1235. * vmbus_add_channel_kobj - setup a sub-directory under device/channels
  1236. */
  1237. int vmbus_add_channel_kobj(struct hv_device *dev, struct vmbus_channel *channel)
  1238. {
  1239. struct kobject *kobj = &channel->kobj;
  1240. u32 relid = channel->offermsg.child_relid;
  1241. int ret;
  1242. kobj->kset = dev->channels_kset;
  1243. ret = kobject_init_and_add(kobj, &vmbus_chan_ktype, NULL,
  1244. "%u", relid);
  1245. if (ret)
  1246. return ret;
  1247. kobject_uevent(kobj, KOBJ_ADD);
  1248. return 0;
  1249. }
  1250. /*
  1251. * vmbus_device_create - Creates and registers a new child device
  1252. * on the vmbus.
  1253. */
  1254. struct hv_device *vmbus_device_create(const uuid_le *type,
  1255. const uuid_le *instance,
  1256. struct vmbus_channel *channel)
  1257. {
  1258. struct hv_device *child_device_obj;
  1259. child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
  1260. if (!child_device_obj) {
  1261. pr_err("Unable to allocate device object for child device\n");
  1262. return NULL;
  1263. }
  1264. child_device_obj->channel = channel;
  1265. memcpy(&child_device_obj->dev_type, type, sizeof(uuid_le));
  1266. memcpy(&child_device_obj->dev_instance, instance,
  1267. sizeof(uuid_le));
  1268. child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */
  1269. return child_device_obj;
  1270. }
  1271. /*
  1272. * vmbus_device_register - Register the child device
  1273. */
  1274. int vmbus_device_register(struct hv_device *child_device_obj)
  1275. {
  1276. struct kobject *kobj = &child_device_obj->device.kobj;
  1277. int ret;
  1278. dev_set_name(&child_device_obj->device, "%pUl",
  1279. child_device_obj->channel->offermsg.offer.if_instance.b);
  1280. child_device_obj->device.bus = &hv_bus;
  1281. child_device_obj->device.parent = &hv_acpi_dev->dev;
  1282. child_device_obj->device.release = vmbus_device_release;
  1283. /*
  1284. * Register with the LDM. This will kick off the driver/device
  1285. * binding...which will eventually call vmbus_match() and vmbus_probe()
  1286. */
  1287. ret = device_register(&child_device_obj->device);
  1288. if (ret) {
  1289. pr_err("Unable to register child device\n");
  1290. return ret;
  1291. }
  1292. child_device_obj->channels_kset = kset_create_and_add("channels",
  1293. NULL, kobj);
  1294. if (!child_device_obj->channels_kset) {
  1295. ret = -ENOMEM;
  1296. goto err_dev_unregister;
  1297. }
  1298. ret = vmbus_add_channel_kobj(child_device_obj,
  1299. child_device_obj->channel);
  1300. if (ret) {
  1301. pr_err("Unable to register primary channeln");
  1302. goto err_kset_unregister;
  1303. }
  1304. return 0;
  1305. err_kset_unregister:
  1306. kset_unregister(child_device_obj->channels_kset);
  1307. err_dev_unregister:
  1308. device_unregister(&child_device_obj->device);
  1309. return ret;
  1310. }
  1311. /*
  1312. * vmbus_device_unregister - Remove the specified child device
  1313. * from the vmbus.
  1314. */
  1315. void vmbus_device_unregister(struct hv_device *device_obj)
  1316. {
  1317. pr_debug("child device %s unregistered\n",
  1318. dev_name(&device_obj->device));
  1319. kset_unregister(device_obj->channels_kset);
  1320. /*
  1321. * Kick off the process of unregistering the device.
  1322. * This will call vmbus_remove() and eventually vmbus_device_release()
  1323. */
  1324. device_unregister(&device_obj->device);
  1325. }
  1326. /*
  1327. * VMBUS is an acpi enumerated device. Get the information we
  1328. * need from DSDT.
  1329. */
  1330. #define VTPM_BASE_ADDRESS 0xfed40000
  1331. static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
  1332. {
  1333. resource_size_t start = 0;
  1334. resource_size_t end = 0;
  1335. struct resource *new_res;
  1336. struct resource **old_res = &hyperv_mmio;
  1337. struct resource **prev_res = NULL;
  1338. switch (res->type) {
  1339. /*
  1340. * "Address" descriptors are for bus windows. Ignore
  1341. * "memory" descriptors, which are for registers on
  1342. * devices.
  1343. */
  1344. case ACPI_RESOURCE_TYPE_ADDRESS32:
  1345. start = res->data.address32.address.minimum;
  1346. end = res->data.address32.address.maximum;
  1347. break;
  1348. case ACPI_RESOURCE_TYPE_ADDRESS64:
  1349. start = res->data.address64.address.minimum;
  1350. end = res->data.address64.address.maximum;
  1351. break;
  1352. default:
  1353. /* Unused resource type */
  1354. return AE_OK;
  1355. }
  1356. /*
  1357. * Ignore ranges that are below 1MB, as they're not
  1358. * necessary or useful here.
  1359. */
  1360. if (end < 0x100000)
  1361. return AE_OK;
  1362. new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
  1363. if (!new_res)
  1364. return AE_NO_MEMORY;
  1365. /* If this range overlaps the virtual TPM, truncate it. */
  1366. if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
  1367. end = VTPM_BASE_ADDRESS;
  1368. new_res->name = "hyperv mmio";
  1369. new_res->flags = IORESOURCE_MEM;
  1370. new_res->start = start;
  1371. new_res->end = end;
  1372. /*
  1373. * If two ranges are adjacent, merge them.
  1374. */
  1375. do {
  1376. if (!*old_res) {
  1377. *old_res = new_res;
  1378. break;
  1379. }
  1380. if (((*old_res)->end + 1) == new_res->start) {
  1381. (*old_res)->end = new_res->end;
  1382. kfree(new_res);
  1383. break;
  1384. }
  1385. if ((*old_res)->start == new_res->end + 1) {
  1386. (*old_res)->start = new_res->start;
  1387. kfree(new_res);
  1388. break;
  1389. }
  1390. if ((*old_res)->start > new_res->end) {
  1391. new_res->sibling = *old_res;
  1392. if (prev_res)
  1393. (*prev_res)->sibling = new_res;
  1394. *old_res = new_res;
  1395. break;
  1396. }
  1397. prev_res = old_res;
  1398. old_res = &(*old_res)->sibling;
  1399. } while (1);
  1400. return AE_OK;
  1401. }
  1402. static int vmbus_acpi_remove(struct acpi_device *device)
  1403. {
  1404. struct resource *cur_res;
  1405. struct resource *next_res;
  1406. if (hyperv_mmio) {
  1407. if (fb_mmio) {
  1408. __release_region(hyperv_mmio, fb_mmio->start,
  1409. resource_size(fb_mmio));
  1410. fb_mmio = NULL;
  1411. }
  1412. for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
  1413. next_res = cur_res->sibling;
  1414. kfree(cur_res);
  1415. }
  1416. }
  1417. return 0;
  1418. }
  1419. static void vmbus_reserve_fb(void)
  1420. {
  1421. int size;
  1422. /*
  1423. * Make a claim for the frame buffer in the resource tree under the
  1424. * first node, which will be the one below 4GB. The length seems to
  1425. * be underreported, particularly in a Generation 1 VM. So start out
  1426. * reserving a larger area and make it smaller until it succeeds.
  1427. */
  1428. if (screen_info.lfb_base) {
  1429. if (efi_enabled(EFI_BOOT))
  1430. size = max_t(__u32, screen_info.lfb_size, 0x800000);
  1431. else
  1432. size = max_t(__u32, screen_info.lfb_size, 0x4000000);
  1433. for (; !fb_mmio && (size >= 0x100000); size >>= 1) {
  1434. fb_mmio = __request_region(hyperv_mmio,
  1435. screen_info.lfb_base, size,
  1436. fb_mmio_name, 0);
  1437. }
  1438. }
  1439. }
  1440. /**
  1441. * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
  1442. * @new: If successful, supplied a pointer to the
  1443. * allocated MMIO space.
  1444. * @device_obj: Identifies the caller
  1445. * @min: Minimum guest physical address of the
  1446. * allocation
  1447. * @max: Maximum guest physical address
  1448. * @size: Size of the range to be allocated
  1449. * @align: Alignment of the range to be allocated
  1450. * @fb_overlap_ok: Whether this allocation can be allowed
  1451. * to overlap the video frame buffer.
  1452. *
  1453. * This function walks the resources granted to VMBus by the
  1454. * _CRS object in the ACPI namespace underneath the parent
  1455. * "bridge" whether that's a root PCI bus in the Generation 1
  1456. * case or a Module Device in the Generation 2 case. It then
  1457. * attempts to allocate from the global MMIO pool in a way that
  1458. * matches the constraints supplied in these parameters and by
  1459. * that _CRS.
  1460. *
  1461. * Return: 0 on success, -errno on failure
  1462. */
  1463. int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
  1464. resource_size_t min, resource_size_t max,
  1465. resource_size_t size, resource_size_t align,
  1466. bool fb_overlap_ok)
  1467. {
  1468. struct resource *iter, *shadow;
  1469. resource_size_t range_min, range_max, start;
  1470. const char *dev_n = dev_name(&device_obj->device);
  1471. int retval;
  1472. retval = -ENXIO;
  1473. down(&hyperv_mmio_lock);
  1474. /*
  1475. * If overlaps with frame buffers are allowed, then first attempt to
  1476. * make the allocation from within the reserved region. Because it
  1477. * is already reserved, no shadow allocation is necessary.
  1478. */
  1479. if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
  1480. !(max < fb_mmio->start)) {
  1481. range_min = fb_mmio->start;
  1482. range_max = fb_mmio->end;
  1483. start = (range_min + align - 1) & ~(align - 1);
  1484. for (; start + size - 1 <= range_max; start += align) {
  1485. *new = request_mem_region_exclusive(start, size, dev_n);
  1486. if (*new) {
  1487. retval = 0;
  1488. goto exit;
  1489. }
  1490. }
  1491. }
  1492. for (iter = hyperv_mmio; iter; iter = iter->sibling) {
  1493. if ((iter->start >= max) || (iter->end <= min))
  1494. continue;
  1495. range_min = iter->start;
  1496. range_max = iter->end;
  1497. start = (range_min + align - 1) & ~(align - 1);
  1498. for (; start + size - 1 <= range_max; start += align) {
  1499. shadow = __request_region(iter, start, size, NULL,
  1500. IORESOURCE_BUSY);
  1501. if (!shadow)
  1502. continue;
  1503. *new = request_mem_region_exclusive(start, size, dev_n);
  1504. if (*new) {
  1505. shadow->name = (char *)*new;
  1506. retval = 0;
  1507. goto exit;
  1508. }
  1509. __release_region(iter, start, size);
  1510. }
  1511. }
  1512. exit:
  1513. up(&hyperv_mmio_lock);
  1514. return retval;
  1515. }
  1516. EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
  1517. /**
  1518. * vmbus_free_mmio() - Free a memory-mapped I/O range.
  1519. * @start: Base address of region to release.
  1520. * @size: Size of the range to be allocated
  1521. *
  1522. * This function releases anything requested by
  1523. * vmbus_mmio_allocate().
  1524. */
  1525. void vmbus_free_mmio(resource_size_t start, resource_size_t size)
  1526. {
  1527. struct resource *iter;
  1528. down(&hyperv_mmio_lock);
  1529. for (iter = hyperv_mmio; iter; iter = iter->sibling) {
  1530. if ((iter->start >= start + size) || (iter->end <= start))
  1531. continue;
  1532. __release_region(iter, start, size);
  1533. }
  1534. release_mem_region(start, size);
  1535. up(&hyperv_mmio_lock);
  1536. }
  1537. EXPORT_SYMBOL_GPL(vmbus_free_mmio);
  1538. static int vmbus_acpi_add(struct acpi_device *device)
  1539. {
  1540. acpi_status result;
  1541. int ret_val = -ENODEV;
  1542. struct acpi_device *ancestor;
  1543. hv_acpi_dev = device;
  1544. result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  1545. vmbus_walk_resources, NULL);
  1546. if (ACPI_FAILURE(result))
  1547. goto acpi_walk_err;
  1548. /*
  1549. * Some ancestor of the vmbus acpi device (Gen1 or Gen2
  1550. * firmware) is the VMOD that has the mmio ranges. Get that.
  1551. */
  1552. for (ancestor = device->parent; ancestor; ancestor = ancestor->parent) {
  1553. result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
  1554. vmbus_walk_resources, NULL);
  1555. if (ACPI_FAILURE(result))
  1556. continue;
  1557. if (hyperv_mmio) {
  1558. vmbus_reserve_fb();
  1559. break;
  1560. }
  1561. }
  1562. ret_val = 0;
  1563. acpi_walk_err:
  1564. complete(&probe_event);
  1565. if (ret_val)
  1566. vmbus_acpi_remove(device);
  1567. return ret_val;
  1568. }
  1569. static const struct acpi_device_id vmbus_acpi_device_ids[] = {
  1570. {"VMBUS", 0},
  1571. {"VMBus", 0},
  1572. {"", 0},
  1573. };
  1574. MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
  1575. static struct acpi_driver vmbus_acpi_driver = {
  1576. .name = "vmbus",
  1577. .ids = vmbus_acpi_device_ids,
  1578. .ops = {
  1579. .add = vmbus_acpi_add,
  1580. .remove = vmbus_acpi_remove,
  1581. },
  1582. };
  1583. static void hv_kexec_handler(void)
  1584. {
  1585. hv_synic_clockevents_cleanup();
  1586. vmbus_initiate_unload(false);
  1587. vmbus_connection.conn_state = DISCONNECTED;
  1588. /* Make sure conn_state is set as hv_synic_cleanup checks for it */
  1589. mb();
  1590. cpuhp_remove_state(hyperv_cpuhp_online);
  1591. hyperv_cleanup();
  1592. };
  1593. static void hv_crash_handler(struct pt_regs *regs)
  1594. {
  1595. vmbus_initiate_unload(true);
  1596. /*
  1597. * In crash handler we can't schedule synic cleanup for all CPUs,
  1598. * doing the cleanup for current CPU only. This should be sufficient
  1599. * for kdump.
  1600. */
  1601. vmbus_connection.conn_state = DISCONNECTED;
  1602. hv_synic_cleanup(smp_processor_id());
  1603. hyperv_cleanup();
  1604. };
  1605. static int __init hv_acpi_init(void)
  1606. {
  1607. int ret, t;
  1608. if (!hv_is_hyperv_initialized())
  1609. return -ENODEV;
  1610. init_completion(&probe_event);
  1611. /*
  1612. * Get ACPI resources first.
  1613. */
  1614. ret = acpi_bus_register_driver(&vmbus_acpi_driver);
  1615. if (ret)
  1616. return ret;
  1617. t = wait_for_completion_timeout(&probe_event, 5*HZ);
  1618. if (t == 0) {
  1619. ret = -ETIMEDOUT;
  1620. goto cleanup;
  1621. }
  1622. ret = vmbus_bus_init();
  1623. if (ret)
  1624. goto cleanup;
  1625. hv_setup_kexec_handler(hv_kexec_handler);
  1626. hv_setup_crash_handler(hv_crash_handler);
  1627. return 0;
  1628. cleanup:
  1629. acpi_bus_unregister_driver(&vmbus_acpi_driver);
  1630. hv_acpi_dev = NULL;
  1631. return ret;
  1632. }
  1633. static void __exit vmbus_exit(void)
  1634. {
  1635. int cpu;
  1636. hv_remove_kexec_handler();
  1637. hv_remove_crash_handler();
  1638. vmbus_connection.conn_state = DISCONNECTED;
  1639. hv_synic_clockevents_cleanup();
  1640. vmbus_disconnect();
  1641. hv_remove_vmbus_irq();
  1642. for_each_online_cpu(cpu) {
  1643. struct hv_per_cpu_context *hv_cpu
  1644. = per_cpu_ptr(hv_context.cpu_context, cpu);
  1645. tasklet_kill(&hv_cpu->msg_dpc);
  1646. }
  1647. vmbus_free_channels();
  1648. if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
  1649. kmsg_dump_unregister(&hv_kmsg_dumper);
  1650. unregister_die_notifier(&hyperv_die_block);
  1651. atomic_notifier_chain_unregister(&panic_notifier_list,
  1652. &hyperv_panic_block);
  1653. }
  1654. free_page((unsigned long)hv_panic_page);
  1655. unregister_sysctl_table(hv_ctl_table_hdr);
  1656. hv_ctl_table_hdr = NULL;
  1657. bus_unregister(&hv_bus);
  1658. cpuhp_remove_state(hyperv_cpuhp_online);
  1659. hv_synic_free();
  1660. acpi_bus_unregister_driver(&vmbus_acpi_driver);
  1661. }
  1662. MODULE_LICENSE("GPL");
  1663. subsys_initcall(hv_acpi_init);
  1664. module_exit(vmbus_exit);