vmbus_drv.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  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 <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 "hyperv_vmbus.h"
  42. static struct acpi_device *hv_acpi_dev;
  43. static struct tasklet_struct msg_dpc;
  44. static struct completion probe_event;
  45. static int irq;
  46. static int hyperv_panic_event(struct notifier_block *nb,
  47. unsigned long event, void *ptr)
  48. {
  49. struct pt_regs *regs;
  50. regs = current_pt_regs();
  51. wrmsrl(HV_X64_MSR_CRASH_P0, regs->ip);
  52. wrmsrl(HV_X64_MSR_CRASH_P1, regs->ax);
  53. wrmsrl(HV_X64_MSR_CRASH_P2, regs->bx);
  54. wrmsrl(HV_X64_MSR_CRASH_P3, regs->cx);
  55. wrmsrl(HV_X64_MSR_CRASH_P4, regs->dx);
  56. /*
  57. * Let Hyper-V know there is crash data available
  58. */
  59. wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
  60. return NOTIFY_DONE;
  61. }
  62. static struct notifier_block hyperv_panic_block = {
  63. .notifier_call = hyperv_panic_event,
  64. };
  65. struct resource hyperv_mmio = {
  66. .name = "hyperv mmio",
  67. .flags = IORESOURCE_MEM,
  68. };
  69. EXPORT_SYMBOL_GPL(hyperv_mmio);
  70. static int vmbus_exists(void)
  71. {
  72. if (hv_acpi_dev == NULL)
  73. return -ENODEV;
  74. return 0;
  75. }
  76. #define VMBUS_ALIAS_LEN ((sizeof((struct hv_vmbus_device_id *)0)->guid) * 2)
  77. static void print_alias_name(struct hv_device *hv_dev, char *alias_name)
  78. {
  79. int i;
  80. for (i = 0; i < VMBUS_ALIAS_LEN; i += 2)
  81. sprintf(&alias_name[i], "%02x", hv_dev->dev_type.b[i/2]);
  82. }
  83. static u8 channel_monitor_group(struct vmbus_channel *channel)
  84. {
  85. return (u8)channel->offermsg.monitorid / 32;
  86. }
  87. static u8 channel_monitor_offset(struct vmbus_channel *channel)
  88. {
  89. return (u8)channel->offermsg.monitorid % 32;
  90. }
  91. static u32 channel_pending(struct vmbus_channel *channel,
  92. struct hv_monitor_page *monitor_page)
  93. {
  94. u8 monitor_group = channel_monitor_group(channel);
  95. return monitor_page->trigger_group[monitor_group].pending;
  96. }
  97. static u32 channel_latency(struct vmbus_channel *channel,
  98. struct hv_monitor_page *monitor_page)
  99. {
  100. u8 monitor_group = channel_monitor_group(channel);
  101. u8 monitor_offset = channel_monitor_offset(channel);
  102. return monitor_page->latency[monitor_group][monitor_offset];
  103. }
  104. static u32 channel_conn_id(struct vmbus_channel *channel,
  105. struct hv_monitor_page *monitor_page)
  106. {
  107. u8 monitor_group = channel_monitor_group(channel);
  108. u8 monitor_offset = channel_monitor_offset(channel);
  109. return monitor_page->parameter[monitor_group][monitor_offset].connectionid.u.id;
  110. }
  111. static ssize_t id_show(struct device *dev, struct device_attribute *dev_attr,
  112. char *buf)
  113. {
  114. struct hv_device *hv_dev = device_to_hv_device(dev);
  115. if (!hv_dev->channel)
  116. return -ENODEV;
  117. return sprintf(buf, "%d\n", hv_dev->channel->offermsg.child_relid);
  118. }
  119. static DEVICE_ATTR_RO(id);
  120. static ssize_t state_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->state);
  127. }
  128. static DEVICE_ATTR_RO(state);
  129. static ssize_t monitor_id_show(struct device *dev,
  130. struct device_attribute *dev_attr, 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->offermsg.monitorid);
  136. }
  137. static DEVICE_ATTR_RO(monitor_id);
  138. static ssize_t class_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, "{%pUl}\n",
  145. hv_dev->channel->offermsg.offer.if_type.b);
  146. }
  147. static DEVICE_ATTR_RO(class_id);
  148. static ssize_t device_id_show(struct device *dev,
  149. struct device_attribute *dev_attr, char *buf)
  150. {
  151. struct hv_device *hv_dev = device_to_hv_device(dev);
  152. if (!hv_dev->channel)
  153. return -ENODEV;
  154. return sprintf(buf, "{%pUl}\n",
  155. hv_dev->channel->offermsg.offer.if_instance.b);
  156. }
  157. static DEVICE_ATTR_RO(device_id);
  158. static ssize_t modalias_show(struct device *dev,
  159. struct device_attribute *dev_attr, char *buf)
  160. {
  161. struct hv_device *hv_dev = device_to_hv_device(dev);
  162. char alias_name[VMBUS_ALIAS_LEN + 1];
  163. print_alias_name(hv_dev, alias_name);
  164. return sprintf(buf, "vmbus:%s\n", alias_name);
  165. }
  166. static DEVICE_ATTR_RO(modalias);
  167. static ssize_t server_monitor_pending_show(struct device *dev,
  168. struct device_attribute *dev_attr,
  169. char *buf)
  170. {
  171. struct hv_device *hv_dev = device_to_hv_device(dev);
  172. if (!hv_dev->channel)
  173. return -ENODEV;
  174. return sprintf(buf, "%d\n",
  175. channel_pending(hv_dev->channel,
  176. vmbus_connection.monitor_pages[1]));
  177. }
  178. static DEVICE_ATTR_RO(server_monitor_pending);
  179. static ssize_t client_monitor_pending_show(struct device *dev,
  180. struct device_attribute *dev_attr,
  181. char *buf)
  182. {
  183. struct hv_device *hv_dev = device_to_hv_device(dev);
  184. if (!hv_dev->channel)
  185. return -ENODEV;
  186. return sprintf(buf, "%d\n",
  187. channel_pending(hv_dev->channel,
  188. vmbus_connection.monitor_pages[1]));
  189. }
  190. static DEVICE_ATTR_RO(client_monitor_pending);
  191. static ssize_t server_monitor_latency_show(struct device *dev,
  192. struct device_attribute *dev_attr,
  193. char *buf)
  194. {
  195. struct hv_device *hv_dev = device_to_hv_device(dev);
  196. if (!hv_dev->channel)
  197. return -ENODEV;
  198. return sprintf(buf, "%d\n",
  199. channel_latency(hv_dev->channel,
  200. vmbus_connection.monitor_pages[0]));
  201. }
  202. static DEVICE_ATTR_RO(server_monitor_latency);
  203. static ssize_t client_monitor_latency_show(struct device *dev,
  204. struct device_attribute *dev_attr,
  205. char *buf)
  206. {
  207. struct hv_device *hv_dev = device_to_hv_device(dev);
  208. if (!hv_dev->channel)
  209. return -ENODEV;
  210. return sprintf(buf, "%d\n",
  211. channel_latency(hv_dev->channel,
  212. vmbus_connection.monitor_pages[1]));
  213. }
  214. static DEVICE_ATTR_RO(client_monitor_latency);
  215. static ssize_t server_monitor_conn_id_show(struct device *dev,
  216. struct device_attribute *dev_attr,
  217. char *buf)
  218. {
  219. struct hv_device *hv_dev = device_to_hv_device(dev);
  220. if (!hv_dev->channel)
  221. return -ENODEV;
  222. return sprintf(buf, "%d\n",
  223. channel_conn_id(hv_dev->channel,
  224. vmbus_connection.monitor_pages[0]));
  225. }
  226. static DEVICE_ATTR_RO(server_monitor_conn_id);
  227. static ssize_t client_monitor_conn_id_show(struct device *dev,
  228. struct device_attribute *dev_attr,
  229. char *buf)
  230. {
  231. struct hv_device *hv_dev = device_to_hv_device(dev);
  232. if (!hv_dev->channel)
  233. return -ENODEV;
  234. return sprintf(buf, "%d\n",
  235. channel_conn_id(hv_dev->channel,
  236. vmbus_connection.monitor_pages[1]));
  237. }
  238. static DEVICE_ATTR_RO(client_monitor_conn_id);
  239. static ssize_t out_intr_mask_show(struct device *dev,
  240. struct device_attribute *dev_attr, char *buf)
  241. {
  242. struct hv_device *hv_dev = device_to_hv_device(dev);
  243. struct hv_ring_buffer_debug_info outbound;
  244. if (!hv_dev->channel)
  245. return -ENODEV;
  246. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  247. return sprintf(buf, "%d\n", outbound.current_interrupt_mask);
  248. }
  249. static DEVICE_ATTR_RO(out_intr_mask);
  250. static ssize_t out_read_index_show(struct device *dev,
  251. struct device_attribute *dev_attr, char *buf)
  252. {
  253. struct hv_device *hv_dev = device_to_hv_device(dev);
  254. struct hv_ring_buffer_debug_info outbound;
  255. if (!hv_dev->channel)
  256. return -ENODEV;
  257. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  258. return sprintf(buf, "%d\n", outbound.current_read_index);
  259. }
  260. static DEVICE_ATTR_RO(out_read_index);
  261. static ssize_t out_write_index_show(struct device *dev,
  262. struct device_attribute *dev_attr,
  263. char *buf)
  264. {
  265. struct hv_device *hv_dev = device_to_hv_device(dev);
  266. struct hv_ring_buffer_debug_info outbound;
  267. if (!hv_dev->channel)
  268. return -ENODEV;
  269. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  270. return sprintf(buf, "%d\n", outbound.current_write_index);
  271. }
  272. static DEVICE_ATTR_RO(out_write_index);
  273. static ssize_t out_read_bytes_avail_show(struct device *dev,
  274. struct device_attribute *dev_attr,
  275. char *buf)
  276. {
  277. struct hv_device *hv_dev = device_to_hv_device(dev);
  278. struct hv_ring_buffer_debug_info outbound;
  279. if (!hv_dev->channel)
  280. return -ENODEV;
  281. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  282. return sprintf(buf, "%d\n", outbound.bytes_avail_toread);
  283. }
  284. static DEVICE_ATTR_RO(out_read_bytes_avail);
  285. static ssize_t out_write_bytes_avail_show(struct device *dev,
  286. struct device_attribute *dev_attr,
  287. char *buf)
  288. {
  289. struct hv_device *hv_dev = device_to_hv_device(dev);
  290. struct hv_ring_buffer_debug_info outbound;
  291. if (!hv_dev->channel)
  292. return -ENODEV;
  293. hv_ringbuffer_get_debuginfo(&hv_dev->channel->outbound, &outbound);
  294. return sprintf(buf, "%d\n", outbound.bytes_avail_towrite);
  295. }
  296. static DEVICE_ATTR_RO(out_write_bytes_avail);
  297. static ssize_t in_intr_mask_show(struct device *dev,
  298. struct device_attribute *dev_attr, char *buf)
  299. {
  300. struct hv_device *hv_dev = device_to_hv_device(dev);
  301. struct hv_ring_buffer_debug_info inbound;
  302. if (!hv_dev->channel)
  303. return -ENODEV;
  304. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  305. return sprintf(buf, "%d\n", inbound.current_interrupt_mask);
  306. }
  307. static DEVICE_ATTR_RO(in_intr_mask);
  308. static ssize_t in_read_index_show(struct device *dev,
  309. struct device_attribute *dev_attr, char *buf)
  310. {
  311. struct hv_device *hv_dev = device_to_hv_device(dev);
  312. struct hv_ring_buffer_debug_info inbound;
  313. if (!hv_dev->channel)
  314. return -ENODEV;
  315. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  316. return sprintf(buf, "%d\n", inbound.current_read_index);
  317. }
  318. static DEVICE_ATTR_RO(in_read_index);
  319. static ssize_t in_write_index_show(struct device *dev,
  320. struct device_attribute *dev_attr, char *buf)
  321. {
  322. struct hv_device *hv_dev = device_to_hv_device(dev);
  323. struct hv_ring_buffer_debug_info inbound;
  324. if (!hv_dev->channel)
  325. return -ENODEV;
  326. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  327. return sprintf(buf, "%d\n", inbound.current_write_index);
  328. }
  329. static DEVICE_ATTR_RO(in_write_index);
  330. static ssize_t in_read_bytes_avail_show(struct device *dev,
  331. struct device_attribute *dev_attr,
  332. char *buf)
  333. {
  334. struct hv_device *hv_dev = device_to_hv_device(dev);
  335. struct hv_ring_buffer_debug_info inbound;
  336. if (!hv_dev->channel)
  337. return -ENODEV;
  338. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  339. return sprintf(buf, "%d\n", inbound.bytes_avail_toread);
  340. }
  341. static DEVICE_ATTR_RO(in_read_bytes_avail);
  342. static ssize_t in_write_bytes_avail_show(struct device *dev,
  343. struct device_attribute *dev_attr,
  344. char *buf)
  345. {
  346. struct hv_device *hv_dev = device_to_hv_device(dev);
  347. struct hv_ring_buffer_debug_info inbound;
  348. if (!hv_dev->channel)
  349. return -ENODEV;
  350. hv_ringbuffer_get_debuginfo(&hv_dev->channel->inbound, &inbound);
  351. return sprintf(buf, "%d\n", inbound.bytes_avail_towrite);
  352. }
  353. static DEVICE_ATTR_RO(in_write_bytes_avail);
  354. /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
  355. static struct attribute *vmbus_attrs[] = {
  356. &dev_attr_id.attr,
  357. &dev_attr_state.attr,
  358. &dev_attr_monitor_id.attr,
  359. &dev_attr_class_id.attr,
  360. &dev_attr_device_id.attr,
  361. &dev_attr_modalias.attr,
  362. &dev_attr_server_monitor_pending.attr,
  363. &dev_attr_client_monitor_pending.attr,
  364. &dev_attr_server_monitor_latency.attr,
  365. &dev_attr_client_monitor_latency.attr,
  366. &dev_attr_server_monitor_conn_id.attr,
  367. &dev_attr_client_monitor_conn_id.attr,
  368. &dev_attr_out_intr_mask.attr,
  369. &dev_attr_out_read_index.attr,
  370. &dev_attr_out_write_index.attr,
  371. &dev_attr_out_read_bytes_avail.attr,
  372. &dev_attr_out_write_bytes_avail.attr,
  373. &dev_attr_in_intr_mask.attr,
  374. &dev_attr_in_read_index.attr,
  375. &dev_attr_in_write_index.attr,
  376. &dev_attr_in_read_bytes_avail.attr,
  377. &dev_attr_in_write_bytes_avail.attr,
  378. NULL,
  379. };
  380. ATTRIBUTE_GROUPS(vmbus);
  381. /*
  382. * vmbus_uevent - add uevent for our device
  383. *
  384. * This routine is invoked when a device is added or removed on the vmbus to
  385. * generate a uevent to udev in the userspace. The udev will then look at its
  386. * rule and the uevent generated here to load the appropriate driver
  387. *
  388. * The alias string will be of the form vmbus:guid where guid is the string
  389. * representation of the device guid (each byte of the guid will be
  390. * represented with two hex characters.
  391. */
  392. static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
  393. {
  394. struct hv_device *dev = device_to_hv_device(device);
  395. int ret;
  396. char alias_name[VMBUS_ALIAS_LEN + 1];
  397. print_alias_name(dev, alias_name);
  398. ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name);
  399. return ret;
  400. }
  401. static const uuid_le null_guid;
  402. static inline bool is_null_guid(const __u8 *guid)
  403. {
  404. if (memcmp(guid, &null_guid, sizeof(uuid_le)))
  405. return false;
  406. return true;
  407. }
  408. /*
  409. * Return a matching hv_vmbus_device_id pointer.
  410. * If there is no match, return NULL.
  411. */
  412. static const struct hv_vmbus_device_id *hv_vmbus_get_id(
  413. const struct hv_vmbus_device_id *id,
  414. const __u8 *guid)
  415. {
  416. for (; !is_null_guid(id->guid); id++)
  417. if (!memcmp(&id->guid, guid, sizeof(uuid_le)))
  418. return id;
  419. return NULL;
  420. }
  421. /*
  422. * vmbus_match - Attempt to match the specified device to the specified driver
  423. */
  424. static int vmbus_match(struct device *device, struct device_driver *driver)
  425. {
  426. struct hv_driver *drv = drv_to_hv_drv(driver);
  427. struct hv_device *hv_dev = device_to_hv_device(device);
  428. if (hv_vmbus_get_id(drv->id_table, hv_dev->dev_type.b))
  429. return 1;
  430. return 0;
  431. }
  432. /*
  433. * vmbus_probe - Add the new vmbus's child device
  434. */
  435. static int vmbus_probe(struct device *child_device)
  436. {
  437. int ret = 0;
  438. struct hv_driver *drv =
  439. drv_to_hv_drv(child_device->driver);
  440. struct hv_device *dev = device_to_hv_device(child_device);
  441. const struct hv_vmbus_device_id *dev_id;
  442. dev_id = hv_vmbus_get_id(drv->id_table, dev->dev_type.b);
  443. if (drv->probe) {
  444. ret = drv->probe(dev, dev_id);
  445. if (ret != 0)
  446. pr_err("probe failed for device %s (%d)\n",
  447. dev_name(child_device), ret);
  448. } else {
  449. pr_err("probe not set for driver %s\n",
  450. dev_name(child_device));
  451. ret = -ENODEV;
  452. }
  453. return ret;
  454. }
  455. /*
  456. * vmbus_remove - Remove a vmbus device
  457. */
  458. static int vmbus_remove(struct device *child_device)
  459. {
  460. struct hv_driver *drv;
  461. struct hv_device *dev = device_to_hv_device(child_device);
  462. u32 relid = dev->channel->offermsg.child_relid;
  463. if (child_device->driver) {
  464. drv = drv_to_hv_drv(child_device->driver);
  465. if (drv->remove)
  466. drv->remove(dev);
  467. else {
  468. hv_process_channel_removal(dev->channel, relid);
  469. pr_err("remove not set for driver %s\n",
  470. dev_name(child_device));
  471. }
  472. } else {
  473. /*
  474. * We don't have a driver for this device; deal with the
  475. * rescind message by removing the channel.
  476. */
  477. hv_process_channel_removal(dev->channel, relid);
  478. }
  479. return 0;
  480. }
  481. /*
  482. * vmbus_shutdown - Shutdown a vmbus device
  483. */
  484. static void vmbus_shutdown(struct device *child_device)
  485. {
  486. struct hv_driver *drv;
  487. struct hv_device *dev = device_to_hv_device(child_device);
  488. /* The device may not be attached yet */
  489. if (!child_device->driver)
  490. return;
  491. drv = drv_to_hv_drv(child_device->driver);
  492. if (drv->shutdown)
  493. drv->shutdown(dev);
  494. return;
  495. }
  496. /*
  497. * vmbus_device_release - Final callback release of the vmbus child device
  498. */
  499. static void vmbus_device_release(struct device *device)
  500. {
  501. struct hv_device *hv_dev = device_to_hv_device(device);
  502. kfree(hv_dev);
  503. }
  504. /* The one and only one */
  505. static struct bus_type hv_bus = {
  506. .name = "vmbus",
  507. .match = vmbus_match,
  508. .shutdown = vmbus_shutdown,
  509. .remove = vmbus_remove,
  510. .probe = vmbus_probe,
  511. .uevent = vmbus_uevent,
  512. .dev_groups = vmbus_groups,
  513. };
  514. struct onmessage_work_context {
  515. struct work_struct work;
  516. struct hv_message msg;
  517. };
  518. static void vmbus_onmessage_work(struct work_struct *work)
  519. {
  520. struct onmessage_work_context *ctx;
  521. /* Do not process messages if we're in DISCONNECTED state */
  522. if (vmbus_connection.conn_state == DISCONNECTED)
  523. return;
  524. ctx = container_of(work, struct onmessage_work_context,
  525. work);
  526. vmbus_onmessage(&ctx->msg);
  527. kfree(ctx);
  528. }
  529. static void hv_process_timer_expiration(struct hv_message *msg, int cpu)
  530. {
  531. struct clock_event_device *dev = hv_context.clk_evt[cpu];
  532. if (dev->event_handler)
  533. dev->event_handler(dev);
  534. msg->header.message_type = HVMSG_NONE;
  535. /*
  536. * Make sure the write to MessageType (ie set to
  537. * HVMSG_NONE) happens before we read the
  538. * MessagePending and EOMing. Otherwise, the EOMing
  539. * will not deliver any more messages since there is
  540. * no empty slot
  541. */
  542. mb();
  543. if (msg->header.message_flags.msg_pending) {
  544. /*
  545. * This will cause message queue rescan to
  546. * possibly deliver another msg from the
  547. * hypervisor
  548. */
  549. wrmsrl(HV_X64_MSR_EOM, 0);
  550. }
  551. }
  552. static void vmbus_on_msg_dpc(unsigned long data)
  553. {
  554. int cpu = smp_processor_id();
  555. void *page_addr = hv_context.synic_message_page[cpu];
  556. struct hv_message *msg = (struct hv_message *)page_addr +
  557. VMBUS_MESSAGE_SINT;
  558. struct vmbus_channel_message_header *hdr;
  559. struct vmbus_channel_message_table_entry *entry;
  560. struct onmessage_work_context *ctx;
  561. while (1) {
  562. if (msg->header.message_type == HVMSG_NONE)
  563. /* no msg */
  564. break;
  565. hdr = (struct vmbus_channel_message_header *)msg->u.payload;
  566. if (hdr->msgtype >= CHANNELMSG_COUNT) {
  567. WARN_ONCE(1, "unknown msgtype=%d\n", hdr->msgtype);
  568. goto msg_handled;
  569. }
  570. entry = &channel_message_table[hdr->msgtype];
  571. if (entry->handler_type == VMHT_BLOCKING) {
  572. ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
  573. if (ctx == NULL)
  574. continue;
  575. INIT_WORK(&ctx->work, vmbus_onmessage_work);
  576. memcpy(&ctx->msg, msg, sizeof(*msg));
  577. queue_work(vmbus_connection.work_queue, &ctx->work);
  578. } else
  579. entry->message_handler(hdr);
  580. msg_handled:
  581. msg->header.message_type = HVMSG_NONE;
  582. /*
  583. * Make sure the write to MessageType (ie set to
  584. * HVMSG_NONE) happens before we read the
  585. * MessagePending and EOMing. Otherwise, the EOMing
  586. * will not deliver any more messages since there is
  587. * no empty slot
  588. */
  589. mb();
  590. if (msg->header.message_flags.msg_pending) {
  591. /*
  592. * This will cause message queue rescan to
  593. * possibly deliver another msg from the
  594. * hypervisor
  595. */
  596. wrmsrl(HV_X64_MSR_EOM, 0);
  597. }
  598. }
  599. }
  600. static void vmbus_isr(void)
  601. {
  602. int cpu = smp_processor_id();
  603. void *page_addr;
  604. struct hv_message *msg;
  605. union hv_synic_event_flags *event;
  606. bool handled = false;
  607. page_addr = hv_context.synic_event_page[cpu];
  608. if (page_addr == NULL)
  609. return;
  610. event = (union hv_synic_event_flags *)page_addr +
  611. VMBUS_MESSAGE_SINT;
  612. /*
  613. * Check for events before checking for messages. This is the order
  614. * in which events and messages are checked in Windows guests on
  615. * Hyper-V, and the Windows team suggested we do the same.
  616. */
  617. if ((vmbus_proto_version == VERSION_WS2008) ||
  618. (vmbus_proto_version == VERSION_WIN7)) {
  619. /* Since we are a child, we only need to check bit 0 */
  620. if (sync_test_and_clear_bit(0,
  621. (unsigned long *) &event->flags32[0])) {
  622. handled = true;
  623. }
  624. } else {
  625. /*
  626. * Our host is win8 or above. The signaling mechanism
  627. * has changed and we can directly look at the event page.
  628. * If bit n is set then we have an interrup on the channel
  629. * whose id is n.
  630. */
  631. handled = true;
  632. }
  633. if (handled)
  634. tasklet_schedule(hv_context.event_dpc[cpu]);
  635. page_addr = hv_context.synic_message_page[cpu];
  636. msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
  637. /* Check if there are actual msgs to be processed */
  638. if (msg->header.message_type != HVMSG_NONE) {
  639. if (msg->header.message_type == HVMSG_TIMER_EXPIRED)
  640. hv_process_timer_expiration(msg, cpu);
  641. else
  642. tasklet_schedule(&msg_dpc);
  643. }
  644. }
  645. #ifdef CONFIG_HOTPLUG_CPU
  646. static int hyperv_cpu_disable(void)
  647. {
  648. return -ENOSYS;
  649. }
  650. static void hv_cpu_hotplug_quirk(bool vmbus_loaded)
  651. {
  652. static void *previous_cpu_disable;
  653. /*
  654. * Offlining a CPU when running on newer hypervisors (WS2012R2, Win8,
  655. * ...) is not supported at this moment as channel interrupts are
  656. * distributed across all of them.
  657. */
  658. if ((vmbus_proto_version == VERSION_WS2008) ||
  659. (vmbus_proto_version == VERSION_WIN7))
  660. return;
  661. if (vmbus_loaded) {
  662. previous_cpu_disable = smp_ops.cpu_disable;
  663. smp_ops.cpu_disable = hyperv_cpu_disable;
  664. pr_notice("CPU offlining is not supported by hypervisor\n");
  665. } else if (previous_cpu_disable)
  666. smp_ops.cpu_disable = previous_cpu_disable;
  667. }
  668. #else
  669. static void hv_cpu_hotplug_quirk(bool vmbus_loaded)
  670. {
  671. }
  672. #endif
  673. /*
  674. * vmbus_bus_init -Main vmbus driver initialization routine.
  675. *
  676. * Here, we
  677. * - initialize the vmbus driver context
  678. * - invoke the vmbus hv main init routine
  679. * - get the irq resource
  680. * - retrieve the channel offers
  681. */
  682. static int vmbus_bus_init(int irq)
  683. {
  684. int ret;
  685. /* Hypervisor initialization...setup hypercall page..etc */
  686. ret = hv_init();
  687. if (ret != 0) {
  688. pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
  689. return ret;
  690. }
  691. tasklet_init(&msg_dpc, vmbus_on_msg_dpc, 0);
  692. ret = bus_register(&hv_bus);
  693. if (ret)
  694. goto err_cleanup;
  695. hv_setup_vmbus_irq(vmbus_isr);
  696. ret = hv_synic_alloc();
  697. if (ret)
  698. goto err_alloc;
  699. /*
  700. * Initialize the per-cpu interrupt state and
  701. * connect to the host.
  702. */
  703. on_each_cpu(hv_synic_init, NULL, 1);
  704. ret = vmbus_connect();
  705. if (ret)
  706. goto err_alloc;
  707. hv_cpu_hotplug_quirk(true);
  708. /*
  709. * Only register if the crash MSRs are available
  710. */
  711. if (ms_hyperv.features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) {
  712. atomic_notifier_chain_register(&panic_notifier_list,
  713. &hyperv_panic_block);
  714. }
  715. vmbus_request_offers();
  716. return 0;
  717. err_alloc:
  718. hv_synic_free();
  719. hv_remove_vmbus_irq();
  720. bus_unregister(&hv_bus);
  721. err_cleanup:
  722. hv_cleanup();
  723. return ret;
  724. }
  725. /**
  726. * __vmbus_child_driver_register - Register a vmbus's driver
  727. * @drv: Pointer to driver structure you want to register
  728. * @owner: owner module of the drv
  729. * @mod_name: module name string
  730. *
  731. * Registers the given driver with Linux through the 'driver_register()' call
  732. * and sets up the hyper-v vmbus handling for this driver.
  733. * It will return the state of the 'driver_register()' call.
  734. *
  735. */
  736. int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
  737. {
  738. int ret;
  739. pr_info("registering driver %s\n", hv_driver->name);
  740. ret = vmbus_exists();
  741. if (ret < 0)
  742. return ret;
  743. hv_driver->driver.name = hv_driver->name;
  744. hv_driver->driver.owner = owner;
  745. hv_driver->driver.mod_name = mod_name;
  746. hv_driver->driver.bus = &hv_bus;
  747. ret = driver_register(&hv_driver->driver);
  748. return ret;
  749. }
  750. EXPORT_SYMBOL_GPL(__vmbus_driver_register);
  751. /**
  752. * vmbus_driver_unregister() - Unregister a vmbus's driver
  753. * @drv: Pointer to driver structure you want to un-register
  754. *
  755. * Un-register the given driver that was previous registered with a call to
  756. * vmbus_driver_register()
  757. */
  758. void vmbus_driver_unregister(struct hv_driver *hv_driver)
  759. {
  760. pr_info("unregistering driver %s\n", hv_driver->name);
  761. if (!vmbus_exists())
  762. driver_unregister(&hv_driver->driver);
  763. }
  764. EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
  765. /*
  766. * vmbus_device_create - Creates and registers a new child device
  767. * on the vmbus.
  768. */
  769. struct hv_device *vmbus_device_create(const uuid_le *type,
  770. const uuid_le *instance,
  771. struct vmbus_channel *channel)
  772. {
  773. struct hv_device *child_device_obj;
  774. child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
  775. if (!child_device_obj) {
  776. pr_err("Unable to allocate device object for child device\n");
  777. return NULL;
  778. }
  779. child_device_obj->channel = channel;
  780. memcpy(&child_device_obj->dev_type, type, sizeof(uuid_le));
  781. memcpy(&child_device_obj->dev_instance, instance,
  782. sizeof(uuid_le));
  783. return child_device_obj;
  784. }
  785. /*
  786. * vmbus_device_register - Register the child device
  787. */
  788. int vmbus_device_register(struct hv_device *child_device_obj)
  789. {
  790. int ret = 0;
  791. dev_set_name(&child_device_obj->device, "vmbus_%d",
  792. child_device_obj->channel->id);
  793. child_device_obj->device.bus = &hv_bus;
  794. child_device_obj->device.parent = &hv_acpi_dev->dev;
  795. child_device_obj->device.release = vmbus_device_release;
  796. /*
  797. * Register with the LDM. This will kick off the driver/device
  798. * binding...which will eventually call vmbus_match() and vmbus_probe()
  799. */
  800. ret = device_register(&child_device_obj->device);
  801. if (ret)
  802. pr_err("Unable to register child device\n");
  803. else
  804. pr_debug("child device %s registered\n",
  805. dev_name(&child_device_obj->device));
  806. return ret;
  807. }
  808. /*
  809. * vmbus_device_unregister - Remove the specified child device
  810. * from the vmbus.
  811. */
  812. void vmbus_device_unregister(struct hv_device *device_obj)
  813. {
  814. pr_debug("child device %s unregistered\n",
  815. dev_name(&device_obj->device));
  816. /*
  817. * Kick off the process of unregistering the device.
  818. * This will call vmbus_remove() and eventually vmbus_device_release()
  819. */
  820. device_unregister(&device_obj->device);
  821. }
  822. /*
  823. * VMBUS is an acpi enumerated device. Get the the information we
  824. * need from DSDT.
  825. */
  826. static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *ctx)
  827. {
  828. switch (res->type) {
  829. case ACPI_RESOURCE_TYPE_IRQ:
  830. irq = res->data.irq.interrupts[0];
  831. break;
  832. case ACPI_RESOURCE_TYPE_ADDRESS64:
  833. hyperv_mmio.start = res->data.address64.address.minimum;
  834. hyperv_mmio.end = res->data.address64.address.maximum;
  835. break;
  836. }
  837. return AE_OK;
  838. }
  839. static int vmbus_acpi_add(struct acpi_device *device)
  840. {
  841. acpi_status result;
  842. int ret_val = -ENODEV;
  843. hv_acpi_dev = device;
  844. result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  845. vmbus_walk_resources, NULL);
  846. if (ACPI_FAILURE(result))
  847. goto acpi_walk_err;
  848. /*
  849. * The parent of the vmbus acpi device (Gen2 firmware) is the VMOD that
  850. * has the mmio ranges. Get that.
  851. */
  852. if (device->parent) {
  853. result = acpi_walk_resources(device->parent->handle,
  854. METHOD_NAME__CRS,
  855. vmbus_walk_resources, NULL);
  856. if (ACPI_FAILURE(result))
  857. goto acpi_walk_err;
  858. if (hyperv_mmio.start && hyperv_mmio.end)
  859. request_resource(&iomem_resource, &hyperv_mmio);
  860. }
  861. ret_val = 0;
  862. acpi_walk_err:
  863. complete(&probe_event);
  864. return ret_val;
  865. }
  866. static const struct acpi_device_id vmbus_acpi_device_ids[] = {
  867. {"VMBUS", 0},
  868. {"VMBus", 0},
  869. {"", 0},
  870. };
  871. MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
  872. static struct acpi_driver vmbus_acpi_driver = {
  873. .name = "vmbus",
  874. .ids = vmbus_acpi_device_ids,
  875. .ops = {
  876. .add = vmbus_acpi_add,
  877. },
  878. };
  879. static int __init hv_acpi_init(void)
  880. {
  881. int ret, t;
  882. if (x86_hyper != &x86_hyper_ms_hyperv)
  883. return -ENODEV;
  884. init_completion(&probe_event);
  885. /*
  886. * Get irq resources first.
  887. */
  888. ret = acpi_bus_register_driver(&vmbus_acpi_driver);
  889. if (ret)
  890. return ret;
  891. t = wait_for_completion_timeout(&probe_event, 5*HZ);
  892. if (t == 0) {
  893. ret = -ETIMEDOUT;
  894. goto cleanup;
  895. }
  896. if (irq <= 0) {
  897. ret = -ENODEV;
  898. goto cleanup;
  899. }
  900. ret = vmbus_bus_init(irq);
  901. if (ret)
  902. goto cleanup;
  903. return 0;
  904. cleanup:
  905. acpi_bus_unregister_driver(&vmbus_acpi_driver);
  906. hv_acpi_dev = NULL;
  907. return ret;
  908. }
  909. static void __exit vmbus_exit(void)
  910. {
  911. int cpu;
  912. vmbus_connection.conn_state = DISCONNECTED;
  913. hv_synic_clockevents_cleanup();
  914. hv_remove_vmbus_irq();
  915. vmbus_free_channels();
  916. bus_unregister(&hv_bus);
  917. hv_cleanup();
  918. for_each_online_cpu(cpu)
  919. smp_call_function_single(cpu, hv_synic_cleanup, NULL, 1);
  920. acpi_bus_unregister_driver(&vmbus_acpi_driver);
  921. hv_cpu_hotplug_quirk(false);
  922. vmbus_disconnect();
  923. }
  924. MODULE_LICENSE("GPL");
  925. subsys_initcall(hv_acpi_init);
  926. module_exit(vmbus_exit);