vmbus_drv.c 24 KB

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