vmbus_drv.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  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 <[email protected]>
  19. * Hank Janssen <[email protected]>
  20. * K. Y. Srinivasan <[email protected]>
  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 <asm/hyperv.h>
  37. #include <asm/hypervisor.h>
  38. #include <asm/mshyperv.h>
  39. #include <linux/notifier.h>
  40. #include <linux/ptrace.h>
  41. #include <linux/screen_info.h>
  42. #include <linux/kdebug.h>
  43. #include <linux/efi.h>
  44. #include <linux/random.h>
  45. #include "hyperv_vmbus.h"
  46. static struct acpi_device *hv_acpi_dev;
  47. static struct completion probe_event;
  48. static void hyperv_report_panic(struct pt_regs *regs)
  49. {
  50. static bool panic_reported;
  51. /*
  52. * We prefer to report panic on 'die' chain as we have proper
  53. * registers to report, but if we miss it (e.g. on BUG()) we need
  54. * to report it on 'panic'.
  55. */
  56. if (panic_reported)
  57. return;
  58. panic_reported = true;
  59. wrmsrl(HV_X64_MSR_CRASH_P0, regs->ip);
  60. wrmsrl(HV_X64_MSR_CRASH_P1, regs->ax);
  61. wrmsrl(HV_X64_MSR_CRASH_P2, regs->bx);
  62. wrmsrl(HV_X64_MSR_CRASH_P3, regs->cx);
  63. wrmsrl(HV_X64_MSR_CRASH_P4, regs->dx);
  64. /*
  65. * Let Hyper-V know there is crash data available
  66. */
  67. wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
  68. }
  69. static int hyperv_panic_event(struct notifier_block *nb, unsigned long val,
  70. void *args)
  71. {
  72. struct pt_regs *regs;
  73. regs = current_pt_regs();
  74. hyperv_report_panic(regs);
  75. return NOTIFY_DONE;
  76. }
  77. static int hyperv_die_event(struct notifier_block *nb, unsigned long val,
  78. void *args)
  79. {
  80. struct die_args *die = (struct die_args *)args;
  81. struct pt_regs *regs = die->regs;
  82. hyperv_report_panic(regs);
  83. return NOTIFY_DONE;
  84. }
  85. static struct notifier_block hyperv_die_block = {
  86. .notifier_call = hyperv_die_event,
  87. };
  88. static struct notifier_block hyperv_panic_block = {
  89. .notifier_call = hyperv_panic_event,
  90. };
  91. static const char *fb_mmio_name = "fb_range";
  92. static struct resource *fb_mmio;
  93. static struct resource *hyperv_mmio;
  94. static DEFINE_SEMAPHORE(hyperv_mmio_lock);
  95. static int vmbus_exists(void)
  96. {
  97. if (hv_acpi_dev == NULL)
  98. return -ENODEV;
  99. return 0;
  100. }
  101. #define VMBUS_ALIAS_LEN ((sizeof((struct hv_vmbus_device_id *)0)->guid) * 2)
  102. static void print_alias_name(struct hv_device *hv_dev, char *alias_name)
  103. {
  104. int i;
  105. for (i = 0; i < VMBUS_ALIAS_LEN; i += 2)
  106. sprintf(&alias_name[i], "%02x", hv_dev->dev_type.b[i/2]);
  107. }
  108. static u8 channel_monitor_group(struct vmbus_channel *channel)
  109. {
  110. return (u8)channel->offermsg.monitorid / 32;
  111. }
  112. static u8 channel_monitor_offset(struct vmbus_channel *channel)
  113. {
  114. return (u8)channel->offermsg.monitorid % 32;
  115. }
  116. static u32 channel_pending(struct vmbus_channel *channel,
  117. struct hv_monitor_page *monitor_page)
  118. {
  119. u8 monitor_group = channel_monitor_group(channel);
  120. return monitor_page->trigger_group[monitor_group].pending;
  121. }
  122. static u32 channel_latency(struct vmbus_channel *channel,
  123. struct hv_monitor_page *monitor_page)
  124. {
  125. u8 monitor_group = channel_monitor_group(channel);
  126. u8 monitor_offset = channel_monitor_offset(channel);
  127. return monitor_page->latency[monitor_group][monitor_offset];
  128. }
  129. static u32 channel_conn_id(struct vmbus_channel *channel,
  130. struct hv_monitor_page *monitor_page)
  131. {
  132. u8 monitor_group = channel_monitor_group(channel);
  133. u8 monitor_offset = channel_monitor_offset(channel);
  134. return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
  135. }
  136. static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
  137. char *buf)
  138. {
  139. struct hv_device *hv_dev = device_to_hv_device(dev);
  140. if (!hv_dev->channel)
  141. return -ENODEV;
  142. return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
  143. }
  144. static DEVICE_ATTR_RO(id);
  145. static ssize_t state_show(struct device *dev, struct device_attribute *dev_attr,
  146. char *buf)
  147. {
  148. struct hv_device *hv_dev = device_to_hv_device(dev);
  149. if (!hv_dev->channel)
  150. return -ENODEV;
  151. return sprintf(buf, "%d\n", hv_dev->channel->state);
  152. }
  153. static DEVICE_ATTR_RO(state);
  154. static ssize_t monitor_id_show(struct device *dev,
  155. struct device_attribute *dev_attr, char *buf)
  156. {
  157. struct hv_device *hv_dev = device_to_hv_device(dev);
  158. if (!hv_dev->channel)
  159. return -ENODEV;
  160. return sprintf(buf, "%d\n", hv_dev->channel->offermsg.monitorid);
  161. }
  162. static DEVICE_ATTR_RO(monitor_id);
  163. static ssize_t class_id_show(struct device *dev,
  164. struct device_attribute *dev_attr, char *buf)
  165. {
  166. struct hv_device *hv_dev = device_to_hv_device(dev);
  167. if (!hv_dev->channel)
  168. return -ENODEV;
  169. return sprintf(buf, "{%pUl}\n",
  170. hv_dev->channel->offermsg.offer.if_type.b);
  171. }
  172. static DEVICE_ATTR_RO(class_id);
  173. static ssize_t device_id_show(struct device *dev,
  174. struct device_attribute *dev_attr, char *buf)
  175. {
  176. struct hv_device *hv_dev = device_to_hv_device(dev);
  177. if (!hv_dev->channel)
  178. return -ENODEV;
  179. return sprintf(buf, "{%pUl}\n",
  180. hv_dev->channel->offermsg.offer.if_instance.b);
  181. }
  182. static DEVICE_ATTR_RO(device_id);
  183. static ssize_t modalias_show(struct device *dev,
  184. struct device_attribute *dev_attr, char *buf)
  185. {
  186. struct hv_device *hv_dev = device_to_hv_device(dev);
  187. char alias_name[VMBUS_ALIAS_LEN + 1];
  188. print_alias_name(hv_dev, alias_name);
  189. return sprintf(buf, "vmbus:%s\n", alias_name);
  190. }
  191. static DEVICE_ATTR_RO(modalias);
  192. static ssize_t server_monitor_pending_show(struct device *dev,
  193. struct device_attribute *dev_attr,
  194. char *buf)
  195. {
  196. struct hv_device *hv_dev = device_to_hv_device(dev);
  197. if (!hv_dev->channel)
  198. return -ENODEV;
  199. return sprintf(buf, "%d\n",
  200. channel_pending(hv_dev->channel,
  201. vmbus_connection.monitor_pages[1]));
  202. }
  203. static DEVICE_ATTR_RO(server_monitor_pending);
  204. static ssize_t client_monitor_pending_show(struct device *dev,
  205. struct device_attribute *dev_attr,
  206. char *buf)
  207. {
  208. struct hv_device *hv_dev = device_to_hv_device(dev);
  209. if (!hv_dev->channel)
  210. return -ENODEV;
  211. return sprintf(buf, "%d\n",
  212. channel_pending(hv_dev->channel,
  213. vmbus_connection.monitor_pages[1]));
  214. }
  215. static DEVICE_ATTR_RO(client_monitor_pending);
  216. static ssize_t server_monitor_latency_show(struct device *dev,
  217. struct device_attribute *dev_attr,
  218. char *buf)
  219. {
  220. struct hv_device *hv_dev = device_to_hv_device(dev);
  221. if (!hv_dev->channel)
  222. return -ENODEV;
  223. return sprintf(buf, "%d\n",
  224. channel_latency(hv_dev->channel,
  225. vmbus_connection.monitor_pages[0]));
  226. }
  227. static DEVICE_ATTR_RO(server_monitor_latency);
  228. static ssize_t client_monitor_latency_show(struct device *dev,
  229. struct device_attribute *dev_attr,
  230. char *buf)
  231. {
  232. struct hv_device *hv_dev = device_to_hv_device(dev);
  233. if (!hv_dev->channel)
  234. return -ENODEV;
  235. return sprintf(buf, "%d\n",
  236. channel_latency(hv_dev->channel,
  237. vmbus_connection.monitor_pages[1]));
  238. }
  239. static DEVICE_ATTR_RO(client_monitor_latency);
  240. static ssize_t server_monitor_conn_id_show(struct device *dev,
  241. struct device_attribute *dev_attr,
  242. char *buf)
  243. {
  244. struct hv_device *hv_dev = device_to_hv_device(dev);
  245. if (!hv_dev->channel)
  246. return -ENODEV;
  247. return sprintf(buf, "%d\n",
  248. channel_conn_id(hv_dev->channel,
  249. vmbus_connection.monitor_pages[0]));
  250. }
  251. static DEVICE_ATTR_RO(server_monitor_conn_id);
  252. static ssize_t client_monitor_conn_id_show(struct device *dev,
  253. struct device_attribute *dev_attr,
  254. char *buf)
  255. {
  256. struct hv_device *hv_dev = device_to_hv_device(dev);
  257. if (!hv_dev->channel)
  258. return -ENODEV;
  259. return sprintf(buf, "%d\n",
  260. channel_conn_id(hv_dev->channel,
  261. vmbus_connection.monitor_pages[1]));
  262. }
  263. static DEVICE_ATTR_RO(client_monitor_conn_id);
  264. static ssize_t out_intr_mask_show(struct device *dev,
  265. struct device_attribute *dev_attr, char *buf)
  266. {
  267. struct hv_device *hv_dev = device_to_hv_device(dev);
  268. struct hv_ring_buffer_debug_info outbound;
  269. if (!hv_dev->channel)
  270. return -ENODEV;
  271. if (hv_dev->channel->state != CHANNEL_OPENED_STATE)
  272. return -EINVAL;
  273. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  274. return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
  275. }
  276. static DEVICE_ATTR_RO(out_intr_mask);
  277. static ssize_t out_read_index_show(struct device *dev,
  278. struct device_attribute *dev_attr, char *buf)
  279. {
  280. struct hv_device *hv_dev = device_to_hv_device(dev);
  281. struct hv_ring_buffer_debug_info outbound;
  282. if (!hv_dev->channel)
  283. return -ENODEV;
  284. if (hv_dev->channel->state != CHANNEL_OPENED_STATE)
  285. return -EINVAL;
  286. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  287. return sprintf(buf, "%d\n", outbound.current_read_index);
  288. }
  289. static DEVICE_ATTR_RO(out_read_index);
  290. static ssize_t out_write_index_show(struct device *dev,
  291. struct device_attribute *dev_attr,
  292. char *buf)
  293. {
  294. struct hv_device *hv_dev = device_to_hv_device(dev);
  295. struct hv_ring_buffer_debug_info outbound;
  296. if (!hv_dev->channel)
  297. return -ENODEV;
  298. if (hv_dev->channel->state != CHANNEL_OPENED_STATE)
  299. return -EINVAL;
  300. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  301. return sprintf(buf, "%d\n", outbound.current_write_index);
  302. }
  303. static DEVICE_ATTR_RO(out_write_index);
  304. static ssize_t out_read_bytes_avail_show(struct device *dev,
  305. struct device_attribute *dev_attr,
  306. char *buf)
  307. {
  308. struct hv_device *hv_dev = device_to_hv_device(dev);
  309. struct hv_ring_buffer_debug_info outbound;
  310. if (!hv_dev->channel)
  311. return -ENODEV;
  312. if (hv_dev->channel->state != CHANNEL_OPENED_STATE)
  313. return -EINVAL;
  314. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  315. return sprintf(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. if (!hv_dev->channel)
  325. return -ENODEV;
  326. if (hv_dev->channel->state != CHANNEL_OPENED_STATE)
  327. return -EINVAL;
  328. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  329. return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
  330. }
  331. static DEVICE_ATTR_RO(out_write_bytes_avail);
  332. static ssize_t in_intr_mask_show(struct device *dev,
  333. struct device_attribute *dev_attr, char *buf)
  334. {
  335. struct hv_device *hv_dev = device_to_hv_device(dev);
  336. struct hv_ring_buffer_debug_info inbound;
  337. if (!hv_dev->channel)
  338. return -ENODEV;
  339. if (hv_dev->channel->state != CHANNEL_OPENED_STATE)
  340. return -EINVAL;
  341. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  342. return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
  343. }
  344. static DEVICE_ATTR_RO(in_intr_mask);
  345. static ssize_t in_read_index_show(struct device *dev,
  346. struct device_attribute *dev_attr, char *buf)
  347. {
  348. struct hv_device *hv_dev = device_to_hv_device(dev);
  349. struct hv_ring_buffer_debug_info inbound;
  350. if (!hv_dev->channel)
  351. return -ENODEV;
  352. if (hv_dev->channel->state != CHANNEL_OPENED_STATE)
  353. return -EINVAL;
  354. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  355. return sprintf(buf, "%d\n", inbound.current_read_index);
  356. }
  357. static DEVICE_ATTR_RO(in_read_index);
  358. static ssize_t in_write_index_show(struct device *dev,
  359. struct device_attribute *dev_attr, char *buf)
  360. {
  361. struct hv_device *hv_dev = device_to_hv_device(dev);
  362. struct hv_ring_buffer_debug_info inbound;
  363. if (!hv_dev->channel)
  364. return -ENODEV;
  365. if (hv_dev->channel->state != CHANNEL_OPENED_STATE)
  366. return -EINVAL;
  367. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  368. return sprintf(buf, "%d\n", inbound.current_write_index);
  369. }
  370. static DEVICE_ATTR_RO(in_write_index);
  371. static ssize_t in_read_bytes_avail_show(struct device *dev,
  372. struct device_attribute *dev_attr,
  373. char *buf)
  374. {
  375. struct hv_device *hv_dev = device_to_hv_device(dev);
  376. struct hv_ring_buffer_debug_info inbound;
  377. if (!hv_dev->channel)
  378. return -ENODEV;
  379. if (hv_dev->channel->state != CHANNEL_OPENED_STATE)
  380. return -EINVAL;
  381. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  382. return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
  383. }
  384. static DEVICE_ATTR_RO(in_read_bytes_avail);
  385. static ssize_t in_write_bytes_avail_show(struct device *dev,
  386. struct device_attribute *dev_attr,
  387. char *buf)
  388. {
  389. struct hv_device *hv_dev = device_to_hv_device(dev);
  390. struct hv_ring_buffer_debug_info inbound;
  391. if (!hv_dev->channel)
  392. return -ENODEV;
  393. if (hv_dev->channel->state != CHANNEL_OPENED_STATE)
  394. return -EINVAL;
  395. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  396. return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
  397. }
  398. static DEVICE_ATTR_RO(in_write_bytes_avail);
  399. static ssize_t channel_vp_mapping_show(struct device *dev,
  400. struct device_attribute *dev_attr,
  401. char *buf)
  402. {
  403. struct hv_device *hv_dev = device_to_hv_device(dev);
  404. struct vmbus_channel *channel = hv_dev->channel, *cur_sc;
  405. unsigned long flags;
  406. int buf_size = PAGE_SIZE, n_written, tot_written;
  407. struct list_head *cur;
  408. if (!channel)
  409. return -ENODEV;
  410. tot_written = snprintf(buf, buf_size, "%u:%u\n",
  411. channel->offermsg.child_relid, channel->target_cpu);
  412. spin_lock_irqsave(&channel->lock, flags);
  413. list_for_each(cur, &channel->sc_list) {
  414. if (tot_written >= buf_size - 1)
  415. break;
  416. cur_sc = list_entry(cur, struct vmbus_channel, sc_list);
  417. n_written = scnprintf(buf + tot_written,
  418. buf_size - tot_written,
  419. "%u:%u\n",
  420. cur_sc->offermsg.child_relid,
  421. cur_sc->target_cpu);
  422. tot_written += n_written;
  423. }
  424. spin_unlock_irqrestore(&channel->lock, flags);
  425. return tot_written;
  426. }
  427. static DEVICE_ATTR_RO(channel_vp_mapping);
  428. static ssize_t vendor_show(struct device *dev,
  429. struct device_attribute *dev_attr,
  430. char *buf)
  431. {
  432. struct hv_device *hv_dev = device_to_hv_device(dev);
  433. return sprintf(buf, "0x%x\n", hv_dev->vendor_id);
  434. }
  435. static DEVICE_ATTR_RO(vendor);
  436. static ssize_t device_show(struct device *dev,
  437. struct device_attribute *dev_attr,
  438. char *buf)
  439. {
  440. struct hv_device *hv_dev = device_to_hv_device(dev);
  441. return sprintf(buf, "0x%x\n", hv_dev->device_id);
  442. }
  443. static DEVICE_ATTR_RO(device);
  444. /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
  445. static struct attribute *vmbus_attrs[] = {
  446. &dev_attr_id.attr,
  447. &dev_attr_state.attr,
  448. &dev_attr_monitor_id.attr,
  449. &dev_attr_class_id.attr,
  450. &dev_attr_device_id.attr,
  451. &dev_attr_modalias.attr,
  452. &dev_attr_server_monitor_pending.attr,
  453. &dev_attr_client_monitor_pending.attr,
  454. &dev_attr_server_monitor_latency.attr,
  455. &dev_attr_client_monitor_latency.attr,
  456. &dev_attr_server_monitor_conn_id.attr,
  457. &dev_attr_client_monitor_conn_id.attr,
  458. &dev_attr_out_intr_mask.attr,
  459. &dev_attr_out_read_index.attr,
  460. &dev_attr_out_write_index.attr,
  461. &dev_attr_out_read_bytes_avail.attr,
  462. &dev_attr_out_write_bytes_avail.attr,
  463. &dev_attr_in_intr_mask.attr,
  464. &dev_attr_in_read_index.attr,
  465. &dev_attr_in_write_index.attr,
  466. &dev_attr_in_read_bytes_avail.attr,
  467. &dev_attr_in_write_bytes_avail.attr,
  468. &dev_attr_channel_vp_mapping.attr,
  469. &dev_attr_vendor.attr,
  470. &dev_attr_device.attr,
  471. NULL,
  472. };
  473. ATTRIBUTE_GROUPS(vmbus);
  474. /*
  475. * vmbus_uevent - add uevent for our device
  476. *
  477. * This routine is invoked when a device is added or removed on the vmbus to
  478. * generate a uevent to udev in the userspace. The udev will then look at its
  479. * rule and the uevent generated here to load the appropriate driver
  480. *
  481. * The alias string will be of the form vmbus:guid where guid is the string
  482. * representation of the device guid (each byte of the guid will be
  483. * represented with two hex characters.
  484. */
  485. static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
  486. {
  487. struct hv_device *dev = device_to_hv_device(device);
  488. int ret;
  489. char alias_name[VMBUS_ALIAS_LEN + 1];
  490. print_alias_name(dev, alias_name);
  491. ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name);
  492. return ret;
  493. }
  494. static const uuid_le null_guid;
  495. static inline bool is_null_guid(const uuid_le *guid)
  496. {
  497. if (uuid_le_cmp(*guid, null_guid))
  498. return false;
  499. return true;
  500. }
  501. /*
  502. * Return a matching hv_vmbus_device_id pointer.
  503. * If there is no match, return NULL.
  504. */
  505. static const struct hv_vmbus_device_id *hv_vmbus_get_id(
  506. const struct hv_vmbus_device_id *id,
  507. const uuid_le *guid)
  508. {
  509. for (; !is_null_guid(&id->guid); id++)
  510. if (!uuid_le_cmp(id->guid, *guid))
  511. return id;
  512. return NULL;
  513. }
  514. /*
  515. * vmbus_match - Attempt to match the specified device to the specified driver
  516. */
  517. static int vmbus_match(struct device *device, struct device_driver *driver)
  518. {
  519. struct hv_driver *drv = drv_to_hv_drv(driver);
  520. struct hv_device *hv_dev = device_to_hv_device(device);
  521. /* The hv_sock driver handles all hv_sock offers. */
  522. if (is_hvsock_channel(hv_dev->channel))
  523. return drv->hvsock;
  524. if (hv_vmbus_get_id(drv->id_table, &hv_dev->dev_type))
  525. return 1;
  526. return 0;
  527. }
  528. /*
  529. * vmbus_probe - Add the new vmbus's child device
  530. */
  531. static int vmbus_probe(struct device *child_device)
  532. {
  533. int ret = 0;
  534. struct hv_driver *drv =
  535. drv_to_hv_drv(child_device->driver);
  536. struct hv_device *dev = device_to_hv_device(child_device);
  537. const struct hv_vmbus_device_id *dev_id;
  538. dev_id = hv_vmbus_get_id(drv->id_table, &dev->dev_type);
  539. if (drv->probe) {
  540. ret = drv->probe(dev, dev_id);
  541. if (ret != 0)
  542. pr_err("probe failed for device %s (%d)\n",
  543. dev_name(child_device), ret);
  544. } else {
  545. pr_err("probe not set for driver %s\n",
  546. dev_name(child_device));
  547. ret = -ENODEV;
  548. }
  549. return ret;
  550. }
  551. /*
  552. * vmbus_remove - Remove a vmbus device
  553. */
  554. static int vmbus_remove(struct device *child_device)
  555. {
  556. struct hv_driver *drv;
  557. struct hv_device *dev = device_to_hv_device(child_device);
  558. if (child_device->driver) {
  559. drv = drv_to_hv_drv(child_device->driver);
  560. if (drv->remove)
  561. drv->remove(dev);
  562. }
  563. return 0;
  564. }
  565. /*
  566. * vmbus_shutdown - Shutdown a vmbus device
  567. */
  568. static void vmbus_shutdown(struct device *child_device)
  569. {
  570. struct hv_driver *drv;
  571. struct hv_device *dev = device_to_hv_device(child_device);
  572. /* The device may not be attached yet */
  573. if (!child_device->driver)
  574. return;
  575. drv = drv_to_hv_drv(child_device->driver);
  576. if (drv->shutdown)
  577. drv->shutdown(dev);
  578. return;
  579. }
  580. /*
  581. * vmbus_device_release - Final callback release of the vmbus child device
  582. */
  583. static void vmbus_device_release(struct device *device)
  584. {
  585. struct hv_device *hv_dev = device_to_hv_device(device);
  586. struct vmbus_channel *channel = hv_dev->channel;
  587. hv_process_channel_removal(channel,
  588. channel->offermsg.child_relid);
  589. kfree(hv_dev);
  590. }
  591. /* The one and only one */
  592. static struct bus_type hv_bus = {
  593. .name = "vmbus",
  594. .match = vmbus_match,
  595. .shutdown = vmbus_shutdown,
  596. .remove = vmbus_remove,
  597. .probe = vmbus_probe,
  598. .uevent = vmbus_uevent,
  599. .dev_groups = vmbus_groups,
  600. };
  601. struct onmessage_work_context {
  602. struct work_struct work;
  603. struct hv_message msg;
  604. };
  605. static void vmbus_onmessage_work(struct work_struct *work)
  606. {
  607. struct onmessage_work_context *ctx;
  608. /* Do not process messages if we're in DISCONNECTED state */
  609. if (vmbus_connection.conn_state == DISCONNECTED)
  610. return;
  611. ctx = container_of(work, struct onmessage_work_context,
  612. work);
  613. vmbus_onmessage(&ctx->msg);
  614. kfree(ctx);
  615. }
  616. static void hv_process_timer_expiration(struct hv_message *msg, int cpu)
  617. {
  618. struct clock_event_device *dev = hv_context.clk_evt[cpu];
  619. if (dev->event_handler)
  620. dev->event_handler(dev);
  621. vmbus_signal_eom(msg, HVMSG_TIMER_EXPIRED);
  622. }
  623. void vmbus_on_msg_dpc(unsigned long data)
  624. {
  625. int cpu = smp_processor_id();
  626. void *page_addr = hv_context.synic_message_page[cpu];
  627. struct hv_message *msg = (struct hv_message *)page_addr +
  628. VMBUS_MESSAGE_SINT;
  629. struct vmbus_channel_message_header *hdr;
  630. struct vmbus_channel_message_table_entry *entry;
  631. struct onmessage_work_context *ctx;
  632. u32 message_type = msg->header.message_type;
  633. if (message_type == HVMSG_NONE)
  634. /* no msg */
  635. return;
  636. hdr = (struct vmbus_channel_message_header *)msg->u.payload;
  637. if (hdr->msgtype >= CHANNELMSG_COUNT) {
  638. WARN_ONCE(1, "unknown msgtype=%d\n", hdr->msgtype);
  639. goto msg_handled;
  640. }
  641. entry = &channel_message_table[hdr->msgtype];
  642. if (entry->handler_type == VMHT_BLOCKING) {
  643. ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
  644. if (ctx == NULL)
  645. return;
  646. INIT_WORK(&ctx->work, vmbus_onmessage_work);
  647. memcpy(&ctx->msg, msg, sizeof(*msg));
  648. queue_work(vmbus_connection.work_queue, &ctx->work);
  649. } else
  650. entry->message_handler(hdr);
  651. msg_handled:
  652. vmbus_signal_eom(msg, message_type);
  653. }
  654. static void vmbus_isr(void)
  655. {
  656. int cpu = smp_processor_id();
  657. void *page_addr;
  658. struct hv_message *msg;
  659. union hv_synic_event_flags *event;
  660. bool handled = false;
  661. page_addr = hv_context.synic_event_page[cpu];
  662. if (page_addr == NULL)
  663. return;
  664. event = (union hv_synic_event_flags *)page_addr +
  665. VMBUS_MESSAGE_SINT;
  666. /*
  667. * Check for events before checking for messages. This is the order
  668. * in which events and messages are checked in Windows guests on
  669. * Hyper-V, and the Windows team suggested we do the same.
  670. */
  671. if ((vmbus_proto_version == VERSION_WS2008) ||
  672. (vmbus_proto_version == VERSION_WIN7)) {
  673. /* Since we are a child, we only need to check bit 0 */
  674. if (sync_test_and_clear_bit(0,
  675. (unsigned long *) &event->flags32[0])) {
  676. handled = true;
  677. }
  678. } else {
  679. /*
  680. * Our host is win8 or above. The signaling mechanism
  681. * has changed and we can directly look at the event page.
  682. * If bit n is set then we have an interrup on the channel
  683. * whose id is n.
  684. */
  685. handled = true;
  686. }
  687. if (handled)
  688. tasklet_schedule(hv_context.event_dpc[cpu]);
  689. page_addr = hv_context.synic_message_page[cpu];
  690. msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
  691. /* Check if there are actual msgs to be processed */
  692. if (msg->header.message_type != HVMSG_NONE) {
  693. if (msg->header.message_type == HVMSG_TIMER_EXPIRED)
  694. hv_process_timer_expiration(msg, cpu);
  695. else
  696. tasklet_schedule(hv_context.msg_dpc[cpu]);
  697. }
  698. add_interrupt_randomness(HYPERVISOR_CALLBACK_VECTOR, 0);
  699. }
  700. /*
  701. * vmbus_bus_init -Main vmbus driver initialization routine.
  702. *
  703. * Here, we
  704. * - initialize the vmbus driver context
  705. * - invoke the vmbus hv main init routine
  706. * - retrieve the channel offers
  707. */
  708. static int vmbus_bus_init(void)
  709. {
  710. int ret;
  711. /* Hypervisor initialization...setup hypercall page..etc */
  712. ret = hv_init();
  713. if (ret != 0) {
  714. pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
  715. return ret;
  716. }
  717. ret = bus_register(&hv_bus);
  718. if (ret)
  719. goto err_cleanup;
  720. hv_setup_vmbus_irq(vmbus_isr);
  721. ret = hv_synic_alloc();
  722. if (ret)
  723. goto err_alloc;
  724. /*
  725. * Initialize the per-cpu interrupt state and
  726. * connect to the host.
  727. */
  728. on_each_cpu(hv_synic_init, NULL, 1);
  729. ret = vmbus_connect();
  730. if (ret)
  731. goto err_connect;
  732. if (vmbus_proto_version > VERSION_WIN7)
  733. cpu_hotplug_disable();
  734. /*
  735. * Only register if the crash MSRs are available
  736. */
  737. if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
  738. register_die_notifier(&hyperv_die_block);
  739. atomic_notifier_chain_register(&panic_notifier_list,
  740. &hyperv_panic_block);
  741. }
  742. vmbus_request_offers();
  743. return 0;
  744. err_connect:
  745. on_each_cpu(hv_synic_cleanup, NULL, 1);
  746. err_alloc:
  747. hv_synic_free();
  748. hv_remove_vmbus_irq();
  749. bus_unregister(&hv_bus);
  750. err_cleanup:
  751. hv_cleanup(false);
  752. return ret;
  753. }
  754. /**
  755. * __vmbus_child_driver_register() - Register a vmbus's driver
  756. * @hv_driver: Pointer to driver structure you want to register
  757. * @owner: owner module of the drv
  758. * @mod_name: module name string
  759. *
  760. * Registers the given driver with Linux through the 'driver_register()' call
  761. * and sets up the hyper-v vmbus handling for this driver.
  762. * It will return the state of the 'driver_register()' call.
  763. *
  764. */
  765. int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
  766. {
  767. int ret;
  768. pr_info("registering driver %s\n", hv_driver->name);
  769. ret = vmbus_exists();
  770. if (ret < 0)
  771. return ret;
  772. hv_driver->driver.name = hv_driver->name;
  773. hv_driver->driver.owner = owner;
  774. hv_driver->driver.mod_name = mod_name;
  775. hv_driver->driver.bus = &hv_bus;
  776. ret = driver_register(&hv_driver->driver);
  777. return ret;
  778. }
  779. EXPORT_SYMBOL_GPL(__vmbus_driver_register);
  780. /**
  781. * vmbus_driver_unregister() - Unregister a vmbus's driver
  782. * @hv_driver: Pointer to driver structure you want to
  783. * un-register
  784. *
  785. * Un-register the given driver that was previous registered with a call to
  786. * vmbus_driver_register()
  787. */
  788. void vmbus_driver_unregister(struct hv_driver *hv_driver)
  789. {
  790. pr_info("unregistering driver %s\n", hv_driver->name);
  791. if (!vmbus_exists())
  792. driver_unregister(&hv_driver->driver);
  793. }
  794. EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
  795. /*
  796. * vmbus_device_create - Creates and registers a new child device
  797. * on the vmbus.
  798. */
  799. struct hv_device *vmbus_device_create(const uuid_le *type,
  800. const uuid_le *instance,
  801. struct vmbus_channel *channel)
  802. {
  803. struct hv_device *child_device_obj;
  804. child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
  805. if (!child_device_obj) {
  806. pr_err("Unable to allocate device object for child device\n");
  807. return NULL;
  808. }
  809. child_device_obj->channel = channel;
  810. memcpy(&child_device_obj->dev_type, type, sizeof(uuid_le));
  811. memcpy(&child_device_obj->dev_instance, instance,
  812. sizeof(uuid_le));
  813. child_device_obj->vendor_id = 0x1414; /* MSFT vendor ID */
  814. return child_device_obj;
  815. }
  816. /*
  817. * vmbus_device_register - Register the child device
  818. */
  819. int vmbus_device_register(struct hv_device *child_device_obj)
  820. {
  821. int ret = 0;
  822. dev_set_name(&child_device_obj->device, "%pUl",
  823. child_device_obj->channel->offermsg.offer.if_instance.b);
  824. child_device_obj->device.bus = &hv_bus;
  825. child_device_obj->device.parent = &hv_acpi_dev->dev;
  826. child_device_obj->device.release = vmbus_device_release;
  827. /*
  828. * Register with the LDM. This will kick off the driver/device
  829. * binding...which will eventually call vmbus_match() and vmbus_probe()
  830. */
  831. ret = device_register(&child_device_obj->device);
  832. if (ret)
  833. pr_err("Unable to register child device\n");
  834. else
  835. pr_debug("child device %s registered\n",
  836. dev_name(&child_device_obj->device));
  837. return ret;
  838. }
  839. /*
  840. * vmbus_device_unregister - Remove the specified child device
  841. * from the vmbus.
  842. */
  843. void vmbus_device_unregister(struct hv_device *device_obj)
  844. {
  845. pr_debug("child device %s unregistered\n",
  846. dev_name(&device_obj->device));
  847. /*
  848. * Kick off the process of unregistering the device.
  849. * This will call vmbus_remove() and eventually vmbus_device_release()
  850. */
  851. device_unregister(&device_obj->device);
  852. }
  853. /*
  854. * VMBUS is an acpi enumerated device. Get the information we
  855. * need from DSDT.
  856. */
  857. #define VTPM_BASE_ADDRESS 0xfed40000
  858. static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
  859. {
  860. resource_size_t start = 0;
  861. resource_size_t end = 0;
  862. struct resource *new_res;
  863. struct resource **old_res = &hyperv_mmio;
  864. struct resource **prev_res = NULL;
  865. switch (res->type) {
  866. /*
  867. * "Address" descriptors are for bus windows. Ignore
  868. * "memory" descriptors, which are for registers on
  869. * devices.
  870. */
  871. case ACPI_RESOURCE_TYPE_ADDRESS32:
  872. start = res->data.address32.address.minimum;
  873. end = res->data.address32.address.maximum;
  874. break;
  875. case ACPI_RESOURCE_TYPE_ADDRESS64:
  876. start = res->data.address64.address.minimum;
  877. end = res->data.address64.address.maximum;
  878. break;
  879. default:
  880. /* Unused resource type */
  881. return AE_OK;
  882. }
  883. /*
  884. * Ignore ranges that are below 1MB, as they're not
  885. * necessary or useful here.
  886. */
  887. if (end < 0x100000)
  888. return AE_OK;
  889. new_res = kzalloc(sizeof(*new_res), GFP_ATOMIC);
  890. if (!new_res)
  891. return AE_NO_MEMORY;
  892. /* If this range overlaps the virtual TPM, truncate it. */
  893. if (end > VTPM_BASE_ADDRESS && start < VTPM_BASE_ADDRESS)
  894. end = VTPM_BASE_ADDRESS;
  895. new_res->name = "hyperv mmio";
  896. new_res->flags = IORESOURCE_MEM;
  897. new_res->start = start;
  898. new_res->end = end;
  899. /*
  900. * If two ranges are adjacent, merge them.
  901. */
  902. do {
  903. if (!*old_res) {
  904. *old_res = new_res;
  905. break;
  906. }
  907. if (((*old_res)->end + 1) == new_res->start) {
  908. (*old_res)->end = new_res->end;
  909. kfree(new_res);
  910. break;
  911. }
  912. if ((*old_res)->start == new_res->end + 1) {
  913. (*old_res)->start = new_res->start;
  914. kfree(new_res);
  915. break;
  916. }
  917. if ((*old_res)->start > new_res->end) {
  918. new_res->sibling = *old_res;
  919. if (prev_res)
  920. (*prev_res)->sibling = new_res;
  921. *old_res = new_res;
  922. break;
  923. }
  924. prev_res = old_res;
  925. old_res = &(*old_res)->sibling;
  926. } while (1);
  927. return AE_OK;
  928. }
  929. static int vmbus_acpi_remove(struct acpi_device *device)
  930. {
  931. struct resource *cur_res;
  932. struct resource *next_res;
  933. if (hyperv_mmio) {
  934. if (fb_mmio) {
  935. __release_region(hyperv_mmio, fb_mmio->start,
  936. resource_size(fb_mmio));
  937. fb_mmio = NULL;
  938. }
  939. for (cur_res = hyperv_mmio; cur_res; cur_res = next_res) {
  940. next_res = cur_res->sibling;
  941. kfree(cur_res);
  942. }
  943. }
  944. return 0;
  945. }
  946. static void vmbus_reserve_fb(void)
  947. {
  948. int size;
  949. /*
  950. * Make a claim for the frame buffer in the resource tree under the
  951. * first node, which will be the one below 4GB. The length seems to
  952. * be underreported, particularly in a Generation 1 VM. So start out
  953. * reserving a larger area and make it smaller until it succeeds.
  954. */
  955. if (screen_info.lfb_base) {
  956. if (efi_enabled(EFI_BOOT))
  957. size = max_t(__u32, screen_info.lfb_size, 0x800000);
  958. else
  959. size = max_t(__u32, screen_info.lfb_size, 0x4000000);
  960. for (; !fb_mmio && (size >= 0x100000); size >>= 1) {
  961. fb_mmio = __request_region(hyperv_mmio,
  962. screen_info.lfb_base, size,
  963. fb_mmio_name, 0);
  964. }
  965. }
  966. }
  967. /**
  968. * vmbus_allocate_mmio() - Pick a memory-mapped I/O range.
  969. * @new: If successful, supplied a pointer to the
  970. * allocated MMIO space.
  971. * @device_obj: Identifies the caller
  972. * @min: Minimum guest physical address of the
  973. * allocation
  974. * @max: Maximum guest physical address
  975. * @size: Size of the range to be allocated
  976. * @align: Alignment of the range to be allocated
  977. * @fb_overlap_ok: Whether this allocation can be allowed
  978. * to overlap the video frame buffer.
  979. *
  980. * This function walks the resources granted to VMBus by the
  981. * _CRS object in the ACPI namespace underneath the parent
  982. * "bridge" whether that's a root PCI bus in the Generation 1
  983. * case or a Module Device in the Generation 2 case. It then
  984. * attempts to allocate from the global MMIO pool in a way that
  985. * matches the constraints supplied in these parameters and by
  986. * that _CRS.
  987. *
  988. * Return: 0 on success, -errno on failure
  989. */
  990. int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,
  991. resource_size_t min, resource_size_t max,
  992. resource_size_t size, resource_size_t align,
  993. bool fb_overlap_ok)
  994. {
  995. struct resource *iter, *shadow;
  996. resource_size_t range_min, range_max, start;
  997. const char *dev_n = dev_name(&device_obj->device);
  998. int retval;
  999. retval = -ENXIO;
  1000. down(&hyperv_mmio_lock);
  1001. /*
  1002. * If overlaps with frame buffers are allowed, then first attempt to
  1003. * make the allocation from within the reserved region. Because it
  1004. * is already reserved, no shadow allocation is necessary.
  1005. */
  1006. if (fb_overlap_ok && fb_mmio && !(min > fb_mmio->end) &&
  1007. !(max < fb_mmio->start)) {
  1008. range_min = fb_mmio->start;
  1009. range_max = fb_mmio->end;
  1010. start = (range_min + align - 1) & ~(align - 1);
  1011. for (; start + size - 1 <= range_max; start += align) {
  1012. *new = request_mem_region_exclusive(start, size, dev_n);
  1013. if (*new) {
  1014. retval = 0;
  1015. goto exit;
  1016. }
  1017. }
  1018. }
  1019. for (iter = hyperv_mmio; iter; iter = iter->sibling) {
  1020. if ((iter->start >= max) || (iter->end <= min))
  1021. continue;
  1022. range_min = iter->start;
  1023. range_max = iter->end;
  1024. start = (range_min + align - 1) & ~(align - 1);
  1025. for (; start + size - 1 <= range_max; start += align) {
  1026. shadow = __request_region(iter, start, size, NULL,
  1027. IORESOURCE_BUSY);
  1028. if (!shadow)
  1029. continue;
  1030. *new = request_mem_region_exclusive(start, size, dev_n);
  1031. if (*new) {
  1032. shadow->name = (char *)*new;
  1033. retval = 0;
  1034. goto exit;
  1035. }
  1036. __release_region(iter, start, size);
  1037. }
  1038. }
  1039. exit:
  1040. up(&hyperv_mmio_lock);
  1041. return retval;
  1042. }
  1043. EXPORT_SYMBOL_GPL(vmbus_allocate_mmio);
  1044. /**
  1045. * vmbus_free_mmio() - Free a memory-mapped I/O range.
  1046. * @start: Base address of region to release.
  1047. * @size: Size of the range to be allocated
  1048. *
  1049. * This function releases anything requested by
  1050. * vmbus_mmio_allocate().
  1051. */
  1052. void vmbus_free_mmio(resource_size_t start, resource_size_t size)
  1053. {
  1054. struct resource *iter;
  1055. down(&hyperv_mmio_lock);
  1056. for (iter = hyperv_mmio; iter; iter = iter->sibling) {
  1057. if ((iter->start >= start + size) || (iter->end <= start))
  1058. continue;
  1059. __release_region(iter, start, size);
  1060. }
  1061. release_mem_region(start, size);
  1062. up(&hyperv_mmio_lock);
  1063. }
  1064. EXPORT_SYMBOL_GPL(vmbus_free_mmio);
  1065. /**
  1066. * vmbus_cpu_number_to_vp_number() - Map CPU to VP.
  1067. * @cpu_number: CPU number in Linux terms
  1068. *
  1069. * This function returns the mapping between the Linux processor
  1070. * number and the hypervisor's virtual processor number, useful
  1071. * in making hypercalls and such that talk about specific
  1072. * processors.
  1073. *
  1074. * Return: Virtual processor number in Hyper-V terms
  1075. */
  1076. int vmbus_cpu_number_to_vp_number(int cpu_number)
  1077. {
  1078. return hv_context.vp_index[cpu_number];
  1079. }
  1080. EXPORT_SYMBOL_GPL(vmbus_cpu_number_to_vp_number);
  1081. static int vmbus_acpi_add(struct acpi_device *device)
  1082. {
  1083. acpi_status result;
  1084. int ret_val = -ENODEV;
  1085. struct acpi_device *ancestor;
  1086. hv_acpi_dev = device;
  1087. result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  1088. vmbus_walk_resources, NULL);
  1089. if (ACPI_FAILURE(result))
  1090. goto acpi_walk_err;
  1091. /*
  1092. * Some ancestor of the vmbus acpi device (Gen1 or Gen2
  1093. * firmware) is the VMOD that has the mmio ranges. Get that.
  1094. */
  1095. for (ancestor = device->parent; ancestor; ancestor = ancestor->parent) {
  1096. result = acpi_walk_resources(ancestor->handle, METHOD_NAME__CRS,
  1097. vmbus_walk_resources, NULL);
  1098. if (ACPI_FAILURE(result))
  1099. continue;
  1100. if (hyperv_mmio) {
  1101. vmbus_reserve_fb();
  1102. break;
  1103. }
  1104. }
  1105. ret_val = 0;
  1106. acpi_walk_err:
  1107. complete(&probe_event);
  1108. if (ret_val)
  1109. vmbus_acpi_remove(device);
  1110. return ret_val;
  1111. }
  1112. static const struct acpi_device_id vmbus_acpi_device_ids[] = {
  1113. {"VMBUS", 0},
  1114. {"VMBus", 0},
  1115. {"", 0},
  1116. };
  1117. MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
  1118. static struct acpi_driver vmbus_acpi_driver = {
  1119. .name = "vmbus",
  1120. .ids = vmbus_acpi_device_ids,
  1121. .ops = {
  1122. .add = vmbus_acpi_add,
  1123. .remove = vmbus_acpi_remove,
  1124. },
  1125. };
  1126. static void hv_kexec_handler(void)
  1127. {
  1128. int cpu;
  1129. hv_synic_clockevents_cleanup();
  1130. vmbus_initiate_unload(false);
  1131. for_each_online_cpu(cpu)
  1132. smp_call_function_single(cpu, hv_synic_cleanup, NULL, 1);
  1133. hv_cleanup(false);
  1134. };
  1135. static void hv_crash_handler(struct pt_regs *regs)
  1136. {
  1137. vmbus_initiate_unload(true);
  1138. /*
  1139. * In crash handler we can't schedule synic cleanup for all CPUs,
  1140. * doing the cleanup for current CPU only. This should be sufficient
  1141. * for kdump.
  1142. */
  1143. hv_synic_cleanup(NULL);
  1144. hv_cleanup(true);
  1145. };
  1146. static int __init hv_acpi_init(void)
  1147. {
  1148. int ret, t;
  1149. if (x86_hyper != &x86_hyper_ms_hyperv)
  1150. return -ENODEV;
  1151. init_completion(&probe_event);
  1152. /*
  1153. * Get ACPI resources first.
  1154. */
  1155. ret = acpi_bus_register_driver(&vmbus_acpi_driver);
  1156. if (ret)
  1157. return ret;
  1158. t = wait_for_completion_timeout(&probe_event, 5*HZ);
  1159. if (t == 0) {
  1160. ret = -ETIMEDOUT;
  1161. goto cleanup;
  1162. }
  1163. ret = vmbus_bus_init();
  1164. if (ret)
  1165. goto cleanup;
  1166. hv_setup_kexec_handler(hv_kexec_handler);
  1167. hv_setup_crash_handler(hv_crash_handler);
  1168. return 0;
  1169. cleanup:
  1170. acpi_bus_unregister_driver(&vmbus_acpi_driver);
  1171. hv_acpi_dev = NULL;
  1172. return ret;
  1173. }
  1174. static void __exit vmbus_exit(void)
  1175. {
  1176. int cpu;
  1177. hv_remove_kexec_handler();
  1178. hv_remove_crash_handler();
  1179. vmbus_connection.conn_state = DISCONNECTED;
  1180. hv_synic_clockevents_cleanup();
  1181. vmbus_disconnect();
  1182. hv_remove_vmbus_irq();
  1183. for_each_online_cpu(cpu)
  1184. tasklet_kill(hv_context.msg_dpc[cpu]);
  1185. vmbus_free_channels();
  1186. if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
  1187. unregister_die_notifier(&hyperv_die_block);
  1188. atomic_notifier_chain_unregister(&panic_notifier_list,
  1189. &hyperv_panic_block);
  1190. }
  1191. bus_unregister(&hv_bus);
  1192. hv_cleanup(false);
  1193. for_each_online_cpu(cpu) {
  1194. tasklet_kill(hv_context.event_dpc[cpu]);
  1195. smp_call_function_single(cpu, hv_synic_cleanup, NULL, 1);
  1196. }
  1197. hv_synic_free();
  1198. acpi_bus_unregister_driver(&vmbus_acpi_driver);
  1199. if (vmbus_proto_version > VERSION_WIN7)
  1200. cpu_hotplug_enable();
  1201. }
  1202. MODULE_LICENSE("GPL");
  1203. subsys_initcall(hv_acpi_init);
  1204. module_exit(vmbus_exit);