vmbus_drv.c 25 KB

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