vmbus_drv.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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/irq.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/sysctl.h>
  30. #include <linux/slab.h>
  31. #include <linux/acpi.h>
  32. #include <acpi/acpi_bus.h>
  33. #include <linux/completion.h>
  34. #include <linux/hyperv.h>
  35. #include <asm/hyperv.h>
  36. #include <asm/hypervisor.h>
  37. #include "hyperv_vmbus.h"
  38. static struct acpi_device *hv_acpi_dev;
  39. static struct tasklet_struct msg_dpc;
  40. static struct tasklet_struct event_dpc;
  41. static struct completion probe_event;
  42. static int irq;
  43. struct hv_device_info {
  44. u32 chn_id;
  45. u32 chn_state;
  46. uuid_le chn_type;
  47. uuid_le chn_instance;
  48. u32 monitor_id;
  49. u32 server_monitor_pending;
  50. u32 server_monitor_latency;
  51. u32 server_monitor_conn_id;
  52. u32 client_monitor_pending;
  53. u32 client_monitor_latency;
  54. u32 client_monitor_conn_id;
  55. struct hv_dev_port_info inbound;
  56. struct hv_dev_port_info outbound;
  57. };
  58. static int vmbus_exists(void)
  59. {
  60. if (hv_acpi_dev == NULL)
  61. return -ENODEV;
  62. return 0;
  63. }
  64. static void get_channel_info(struct hv_device *device,
  65. struct hv_device_info *info)
  66. {
  67. struct vmbus_channel_debug_info debug_info;
  68. if (!device->channel)
  69. return;
  70. vmbus_get_debug_info(device->channel, &debug_info);
  71. info->chn_id = debug_info.relid;
  72. info->chn_state = debug_info.state;
  73. memcpy(&info->chn_type, &debug_info.interfacetype,
  74. sizeof(uuid_le));
  75. memcpy(&info->chn_instance, &debug_info.interface_instance,
  76. sizeof(uuid_le));
  77. info->monitor_id = debug_info.monitorid;
  78. info->server_monitor_pending = debug_info.servermonitor_pending;
  79. info->server_monitor_latency = debug_info.servermonitor_latency;
  80. info->server_monitor_conn_id = debug_info.servermonitor_connectionid;
  81. info->client_monitor_pending = debug_info.clientmonitor_pending;
  82. info->client_monitor_latency = debug_info.clientmonitor_latency;
  83. info->client_monitor_conn_id = debug_info.clientmonitor_connectionid;
  84. info->inbound.int_mask = debug_info.inbound.current_interrupt_mask;
  85. info->inbound.read_idx = debug_info.inbound.current_read_index;
  86. info->inbound.write_idx = debug_info.inbound.current_write_index;
  87. info->inbound.bytes_avail_toread =
  88. debug_info.inbound.bytes_avail_toread;
  89. info->inbound.bytes_avail_towrite =
  90. debug_info.inbound.bytes_avail_towrite;
  91. info->outbound.int_mask =
  92. debug_info.outbound.current_interrupt_mask;
  93. info->outbound.read_idx = debug_info.outbound.current_read_index;
  94. info->outbound.write_idx = debug_info.outbound.current_write_index;
  95. info->outbound.bytes_avail_toread =
  96. debug_info.outbound.bytes_avail_toread;
  97. info->outbound.bytes_avail_towrite =
  98. debug_info.outbound.bytes_avail_towrite;
  99. }
  100. #define VMBUS_ALIAS_LEN ((sizeof((struct hv_vmbus_device_id *)0)->guid) * 2)
  101. static void print_alias_name(struct hv_device *hv_dev, char *alias_name)
  102. {
  103. int i;
  104. for (i = 0; i < VMBUS_ALIAS_LEN; i += 2)
  105. sprintf(&alias_name[i], "%02x", hv_dev->dev_type.b[i/2]);
  106. }
  107. /*
  108. * vmbus_show_device_attr - Show the device attribute in sysfs.
  109. *
  110. * This is invoked when user does a
  111. * "cat /sys/bus/vmbus/devices/<busdevice>/<attr name>"
  112. */
  113. static ssize_t vmbus_show_device_attr(struct device *dev,
  114. struct device_attribute *dev_attr,
  115. char *buf)
  116. {
  117. struct hv_device *hv_dev = device_to_hv_device(dev);
  118. struct hv_device_info *device_info;
  119. char alias_name[VMBUS_ALIAS_LEN + 1];
  120. int ret = 0;
  121. device_info = kzalloc(sizeof(struct hv_device_info), GFP_KERNEL);
  122. if (!device_info)
  123. return ret;
  124. get_channel_info(hv_dev, device_info);
  125. if (!strcmp(dev_attr->attr.name, "class_id")) {
  126. ret = sprintf(buf, "{%pUl}\n", device_info->chn_type.b);
  127. } else if (!strcmp(dev_attr->attr.name, "device_id")) {
  128. ret = sprintf(buf, "{%pUl}\n", device_info->chn_instance.b);
  129. } else if (!strcmp(dev_attr->attr.name, "modalias")) {
  130. print_alias_name(hv_dev, alias_name);
  131. ret = sprintf(buf, "vmbus:%s\n", alias_name);
  132. } else if (!strcmp(dev_attr->attr.name, "state")) {
  133. ret = sprintf(buf, "%d\n", device_info->chn_state);
  134. } else if (!strcmp(dev_attr->attr.name, "id")) {
  135. ret = sprintf(buf, "%d\n", device_info->chn_id);
  136. } else if (!strcmp(dev_attr->attr.name, "out_intr_mask")) {
  137. ret = sprintf(buf, "%d\n", device_info->outbound.int_mask);
  138. } else if (!strcmp(dev_attr->attr.name, "out_read_index")) {
  139. ret = sprintf(buf, "%d\n", device_info->outbound.read_idx);
  140. } else if (!strcmp(dev_attr->attr.name, "out_write_index")) {
  141. ret = sprintf(buf, "%d\n", device_info->outbound.write_idx);
  142. } else if (!strcmp(dev_attr->attr.name, "out_read_bytes_avail")) {
  143. ret = sprintf(buf, "%d\n",
  144. device_info->outbound.bytes_avail_toread);
  145. } else if (!strcmp(dev_attr->attr.name, "out_write_bytes_avail")) {
  146. ret = sprintf(buf, "%d\n",
  147. device_info->outbound.bytes_avail_towrite);
  148. } else if (!strcmp(dev_attr->attr.name, "in_intr_mask")) {
  149. ret = sprintf(buf, "%d\n", device_info->inbound.int_mask);
  150. } else if (!strcmp(dev_attr->attr.name, "in_read_index")) {
  151. ret = sprintf(buf, "%d\n", device_info->inbound.read_idx);
  152. } else if (!strcmp(dev_attr->attr.name, "in_write_index")) {
  153. ret = sprintf(buf, "%d\n", device_info->inbound.write_idx);
  154. } else if (!strcmp(dev_attr->attr.name, "in_read_bytes_avail")) {
  155. ret = sprintf(buf, "%d\n",
  156. device_info->inbound.bytes_avail_toread);
  157. } else if (!strcmp(dev_attr->attr.name, "in_write_bytes_avail")) {
  158. ret = sprintf(buf, "%d\n",
  159. device_info->inbound.bytes_avail_towrite);
  160. } else if (!strcmp(dev_attr->attr.name, "monitor_id")) {
  161. ret = sprintf(buf, "%d\n", device_info->monitor_id);
  162. } else if (!strcmp(dev_attr->attr.name, "server_monitor_pending")) {
  163. ret = sprintf(buf, "%d\n", device_info->server_monitor_pending);
  164. } else if (!strcmp(dev_attr->attr.name, "server_monitor_latency")) {
  165. ret = sprintf(buf, "%d\n", device_info->server_monitor_latency);
  166. } else if (!strcmp(dev_attr->attr.name, "server_monitor_conn_id")) {
  167. ret = sprintf(buf, "%d\n",
  168. device_info->server_monitor_conn_id);
  169. } else if (!strcmp(dev_attr->attr.name, "client_monitor_pending")) {
  170. ret = sprintf(buf, "%d\n", device_info->client_monitor_pending);
  171. } else if (!strcmp(dev_attr->attr.name, "client_monitor_latency")) {
  172. ret = sprintf(buf, "%d\n", device_info->client_monitor_latency);
  173. } else if (!strcmp(dev_attr->attr.name, "client_monitor_conn_id")) {
  174. ret = sprintf(buf, "%d\n",
  175. device_info->client_monitor_conn_id);
  176. }
  177. kfree(device_info);
  178. return ret;
  179. }
  180. /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
  181. static struct device_attribute vmbus_device_attrs[] = {
  182. __ATTR(id, S_IRUGO, vmbus_show_device_attr, NULL),
  183. __ATTR(state, S_IRUGO, vmbus_show_device_attr, NULL),
  184. __ATTR(class_id, S_IRUGO, vmbus_show_device_attr, NULL),
  185. __ATTR(device_id, S_IRUGO, vmbus_show_device_attr, NULL),
  186. __ATTR(monitor_id, S_IRUGO, vmbus_show_device_attr, NULL),
  187. __ATTR(modalias, S_IRUGO, vmbus_show_device_attr, NULL),
  188. __ATTR(server_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL),
  189. __ATTR(server_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL),
  190. __ATTR(server_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL),
  191. __ATTR(client_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL),
  192. __ATTR(client_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL),
  193. __ATTR(client_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL),
  194. __ATTR(out_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
  195. __ATTR(out_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
  196. __ATTR(out_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
  197. __ATTR(out_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
  198. __ATTR(out_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
  199. __ATTR(in_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
  200. __ATTR(in_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
  201. __ATTR(in_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
  202. __ATTR(in_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
  203. __ATTR(in_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
  204. __ATTR_NULL
  205. };
  206. /*
  207. * vmbus_uevent - add uevent for our device
  208. *
  209. * This routine is invoked when a device is added or removed on the vmbus to
  210. * generate a uevent to udev in the userspace. The udev will then look at its
  211. * rule and the uevent generated here to load the appropriate driver
  212. *
  213. * The alias string will be of the form vmbus:guid where guid is the string
  214. * representation of the device guid (each byte of the guid will be
  215. * represented with two hex characters.
  216. */
  217. static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
  218. {
  219. struct hv_device *dev = device_to_hv_device(device);
  220. int ret;
  221. char alias_name[VMBUS_ALIAS_LEN + 1];
  222. print_alias_name(dev, alias_name);
  223. ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name);
  224. return ret;
  225. }
  226. static uuid_le null_guid;
  227. static inline bool is_null_guid(const __u8 *guid)
  228. {
  229. if (memcmp(guid, &null_guid, sizeof(uuid_le)))
  230. return false;
  231. return true;
  232. }
  233. /*
  234. * Return a matching hv_vmbus_device_id pointer.
  235. * If there is no match, return NULL.
  236. */
  237. static const struct hv_vmbus_device_id *hv_vmbus_get_id(
  238. const struct hv_vmbus_device_id *id,
  239. __u8 *guid)
  240. {
  241. for (; !is_null_guid(id->guid); id++)
  242. if (!memcmp(&id->guid, guid, sizeof(uuid_le)))
  243. return id;
  244. return NULL;
  245. }
  246. /*
  247. * vmbus_match - Attempt to match the specified device to the specified driver
  248. */
  249. static int vmbus_match(struct device *device, struct device_driver *driver)
  250. {
  251. struct hv_driver *drv = drv_to_hv_drv(driver);
  252. struct hv_device *hv_dev = device_to_hv_device(device);
  253. if (hv_vmbus_get_id(drv->id_table, hv_dev->dev_type.b))
  254. return 1;
  255. return 0;
  256. }
  257. /*
  258. * vmbus_probe - Add the new vmbus's child device
  259. */
  260. static int vmbus_probe(struct device *child_device)
  261. {
  262. int ret = 0;
  263. struct hv_driver *drv =
  264. drv_to_hv_drv(child_device->driver);
  265. struct hv_device *dev = device_to_hv_device(child_device);
  266. const struct hv_vmbus_device_id *dev_id;
  267. dev_id = hv_vmbus_get_id(drv->id_table, dev->dev_type.b);
  268. if (drv->probe) {
  269. ret = drv->probe(dev, dev_id);
  270. if (ret != 0)
  271. pr_err("probe failed for device %s (%d)\n",
  272. dev_name(child_device), ret);
  273. } else {
  274. pr_err("probe not set for driver %s\n",
  275. dev_name(child_device));
  276. ret = -ENODEV;
  277. }
  278. return ret;
  279. }
  280. /*
  281. * vmbus_remove - Remove a vmbus device
  282. */
  283. static int vmbus_remove(struct device *child_device)
  284. {
  285. struct hv_driver *drv = drv_to_hv_drv(child_device->driver);
  286. struct hv_device *dev = device_to_hv_device(child_device);
  287. if (drv->remove)
  288. drv->remove(dev);
  289. else
  290. pr_err("remove not set for driver %s\n",
  291. dev_name(child_device));
  292. return 0;
  293. }
  294. /*
  295. * vmbus_shutdown - Shutdown a vmbus device
  296. */
  297. static void vmbus_shutdown(struct device *child_device)
  298. {
  299. struct hv_driver *drv;
  300. struct hv_device *dev = device_to_hv_device(child_device);
  301. /* The device may not be attached yet */
  302. if (!child_device->driver)
  303. return;
  304. drv = drv_to_hv_drv(child_device->driver);
  305. if (drv->shutdown)
  306. drv->shutdown(dev);
  307. return;
  308. }
  309. /*
  310. * vmbus_device_release - Final callback release of the vmbus child device
  311. */
  312. static void vmbus_device_release(struct device *device)
  313. {
  314. struct hv_device *hv_dev = device_to_hv_device(device);
  315. kfree(hv_dev);
  316. }
  317. /* The one and only one */
  318. static struct bus_type hv_bus = {
  319. .name = "vmbus",
  320. .match = vmbus_match,
  321. .shutdown = vmbus_shutdown,
  322. .remove = vmbus_remove,
  323. .probe = vmbus_probe,
  324. .uevent = vmbus_uevent,
  325. .dev_attrs = vmbus_device_attrs,
  326. };
  327. static const char *driver_name = "hyperv";
  328. struct onmessage_work_context {
  329. struct work_struct work;
  330. struct hv_message msg;
  331. };
  332. static void vmbus_onmessage_work(struct work_struct *work)
  333. {
  334. struct onmessage_work_context *ctx;
  335. ctx = container_of(work, struct onmessage_work_context,
  336. work);
  337. vmbus_onmessage(&ctx->msg);
  338. kfree(ctx);
  339. }
  340. static void vmbus_on_msg_dpc(unsigned long data)
  341. {
  342. int cpu = smp_processor_id();
  343. void *page_addr = hv_context.synic_message_page[cpu];
  344. struct hv_message *msg = (struct hv_message *)page_addr +
  345. VMBUS_MESSAGE_SINT;
  346. struct onmessage_work_context *ctx;
  347. while (1) {
  348. if (msg->header.message_type == HVMSG_NONE) {
  349. /* no msg */
  350. break;
  351. } else {
  352. ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
  353. if (ctx == NULL)
  354. continue;
  355. INIT_WORK(&ctx->work, vmbus_onmessage_work);
  356. memcpy(&ctx->msg, msg, sizeof(*msg));
  357. queue_work(vmbus_connection.work_queue, &ctx->work);
  358. }
  359. msg->header.message_type = HVMSG_NONE;
  360. /*
  361. * Make sure the write to MessageType (ie set to
  362. * HVMSG_NONE) happens before we read the
  363. * MessagePending and EOMing. Otherwise, the EOMing
  364. * will not deliver any more messages since there is
  365. * no empty slot
  366. */
  367. smp_mb();
  368. if (msg->header.message_flags.msg_pending) {
  369. /*
  370. * This will cause message queue rescan to
  371. * possibly deliver another msg from the
  372. * hypervisor
  373. */
  374. wrmsrl(HV_X64_MSR_EOM, 0);
  375. }
  376. }
  377. }
  378. static irqreturn_t vmbus_isr(int irq, void *dev_id)
  379. {
  380. int cpu = smp_processor_id();
  381. void *page_addr;
  382. struct hv_message *msg;
  383. union hv_synic_event_flags *event;
  384. bool handled = false;
  385. /*
  386. * Check for events before checking for messages. This is the order
  387. * in which events and messages are checked in Windows guests on
  388. * Hyper-V, and the Windows team suggested we do the same.
  389. */
  390. page_addr = hv_context.synic_event_page[cpu];
  391. event = (union hv_synic_event_flags *)page_addr + VMBUS_MESSAGE_SINT;
  392. /* Since we are a child, we only need to check bit 0 */
  393. if (sync_test_and_clear_bit(0, (unsigned long *) &event->flags32[0])) {
  394. handled = true;
  395. tasklet_schedule(&event_dpc);
  396. }
  397. page_addr = hv_context.synic_message_page[cpu];
  398. msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
  399. /* Check if there are actual msgs to be processed */
  400. if (msg->header.message_type != HVMSG_NONE) {
  401. handled = true;
  402. tasklet_schedule(&msg_dpc);
  403. }
  404. if (handled)
  405. return IRQ_HANDLED;
  406. else
  407. return IRQ_NONE;
  408. }
  409. /*
  410. * vmbus_bus_init -Main vmbus driver initialization routine.
  411. *
  412. * Here, we
  413. * - initialize the vmbus driver context
  414. * - invoke the vmbus hv main init routine
  415. * - get the irq resource
  416. * - retrieve the channel offers
  417. */
  418. static int vmbus_bus_init(int irq)
  419. {
  420. int ret;
  421. unsigned int vector;
  422. /* Hypervisor initialization...setup hypercall page..etc */
  423. ret = hv_init();
  424. if (ret != 0) {
  425. pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
  426. return ret;
  427. }
  428. tasklet_init(&msg_dpc, vmbus_on_msg_dpc, 0);
  429. tasklet_init(&event_dpc, vmbus_on_event, 0);
  430. ret = bus_register(&hv_bus);
  431. if (ret)
  432. goto err_cleanup;
  433. ret = request_irq(irq, vmbus_isr, 0, driver_name, hv_acpi_dev);
  434. if (ret != 0) {
  435. pr_err("Unable to request IRQ %d\n",
  436. irq);
  437. goto err_unregister;
  438. }
  439. vector = IRQ0_VECTOR + irq;
  440. /*
  441. * Notify the hypervisor of our irq and
  442. * connect to the host.
  443. */
  444. on_each_cpu(hv_synic_init, (void *)&vector, 1);
  445. ret = vmbus_connect();
  446. if (ret)
  447. goto err_irq;
  448. vmbus_request_offers();
  449. return 0;
  450. err_irq:
  451. free_irq(irq, hv_acpi_dev);
  452. err_unregister:
  453. bus_unregister(&hv_bus);
  454. err_cleanup:
  455. hv_cleanup();
  456. return ret;
  457. }
  458. /**
  459. * __vmbus_child_driver_register - Register a vmbus's driver
  460. * @drv: Pointer to driver structure you want to register
  461. * @owner: owner module of the drv
  462. * @mod_name: module name string
  463. *
  464. * Registers the given driver with Linux through the 'driver_register()' call
  465. * and sets up the hyper-v vmbus handling for this driver.
  466. * It will return the state of the 'driver_register()' call.
  467. *
  468. */
  469. int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
  470. {
  471. int ret;
  472. pr_info("registering driver %s\n", hv_driver->name);
  473. ret = vmbus_exists();
  474. if (ret < 0)
  475. return ret;
  476. hv_driver->driver.name = hv_driver->name;
  477. hv_driver->driver.owner = owner;
  478. hv_driver->driver.mod_name = mod_name;
  479. hv_driver->driver.bus = &hv_bus;
  480. ret = driver_register(&hv_driver->driver);
  481. vmbus_request_offers();
  482. return ret;
  483. }
  484. EXPORT_SYMBOL_GPL(__vmbus_driver_register);
  485. /**
  486. * vmbus_driver_unregister() - Unregister a vmbus's driver
  487. * @drv: Pointer to driver structure you want to un-register
  488. *
  489. * Un-register the given driver that was previous registered with a call to
  490. * vmbus_driver_register()
  491. */
  492. void vmbus_driver_unregister(struct hv_driver *hv_driver)
  493. {
  494. pr_info("unregistering driver %s\n", hv_driver->name);
  495. if (!vmbus_exists())
  496. driver_unregister(&hv_driver->driver);
  497. }
  498. EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
  499. /*
  500. * vmbus_device_create - Creates and registers a new child device
  501. * on the vmbus.
  502. */
  503. struct hv_device *vmbus_device_create(uuid_le *type,
  504. uuid_le *instance,
  505. struct vmbus_channel *channel)
  506. {
  507. struct hv_device *child_device_obj;
  508. child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
  509. if (!child_device_obj) {
  510. pr_err("Unable to allocate device object for child device\n");
  511. return NULL;
  512. }
  513. child_device_obj->channel = channel;
  514. memcpy(&child_device_obj->dev_type, type, sizeof(uuid_le));
  515. memcpy(&child_device_obj->dev_instance, instance,
  516. sizeof(uuid_le));
  517. return child_device_obj;
  518. }
  519. /*
  520. * vmbus_device_register - Register the child device
  521. */
  522. int vmbus_device_register(struct hv_device *child_device_obj)
  523. {
  524. int ret = 0;
  525. static atomic_t device_num = ATOMIC_INIT(0);
  526. dev_set_name(&child_device_obj->device, "vmbus_0_%d",
  527. atomic_inc_return(&device_num));
  528. child_device_obj->device.bus = &hv_bus;
  529. child_device_obj->device.parent = &hv_acpi_dev->dev;
  530. child_device_obj->device.release = vmbus_device_release;
  531. /*
  532. * Register with the LDM. This will kick off the driver/device
  533. * binding...which will eventually call vmbus_match() and vmbus_probe()
  534. */
  535. ret = device_register(&child_device_obj->device);
  536. if (ret)
  537. pr_err("Unable to register child device\n");
  538. else
  539. pr_info("child device %s registered\n",
  540. dev_name(&child_device_obj->device));
  541. return ret;
  542. }
  543. /*
  544. * vmbus_device_unregister - Remove the specified child device
  545. * from the vmbus.
  546. */
  547. void vmbus_device_unregister(struct hv_device *device_obj)
  548. {
  549. /*
  550. * Kick off the process of unregistering the device.
  551. * This will call vmbus_remove() and eventually vmbus_device_release()
  552. */
  553. device_unregister(&device_obj->device);
  554. pr_info("child device %s unregistered\n",
  555. dev_name(&device_obj->device));
  556. }
  557. /*
  558. * VMBUS is an acpi enumerated device. Get the the IRQ information
  559. * from DSDT.
  560. */
  561. static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *irq)
  562. {
  563. if (res->type == ACPI_RESOURCE_TYPE_IRQ) {
  564. struct acpi_resource_irq *irqp;
  565. irqp = &res->data.irq;
  566. *((unsigned int *)irq) = irqp->interrupts[0];
  567. }
  568. return AE_OK;
  569. }
  570. static int vmbus_acpi_add(struct acpi_device *device)
  571. {
  572. acpi_status result;
  573. hv_acpi_dev = device;
  574. result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  575. vmbus_walk_resources, &irq);
  576. if (ACPI_FAILURE(result)) {
  577. complete(&probe_event);
  578. return -ENODEV;
  579. }
  580. complete(&probe_event);
  581. return 0;
  582. }
  583. static const struct acpi_device_id vmbus_acpi_device_ids[] = {
  584. {"VMBUS", 0},
  585. {"VMBus", 0},
  586. {"", 0},
  587. };
  588. MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
  589. static struct acpi_driver vmbus_acpi_driver = {
  590. .name = "vmbus",
  591. .ids = vmbus_acpi_device_ids,
  592. .ops = {
  593. .add = vmbus_acpi_add,
  594. },
  595. };
  596. static int __init hv_acpi_init(void)
  597. {
  598. int ret, t;
  599. if (x86_hyper != &x86_hyper_ms_hyperv)
  600. return -ENODEV;
  601. init_completion(&probe_event);
  602. /*
  603. * Get irq resources first.
  604. */
  605. ret = acpi_bus_register_driver(&vmbus_acpi_driver);
  606. if (ret)
  607. return ret;
  608. t = wait_for_completion_timeout(&probe_event, 5*HZ);
  609. if (t == 0) {
  610. ret = -ETIMEDOUT;
  611. goto cleanup;
  612. }
  613. if (irq <= 0) {
  614. ret = -ENODEV;
  615. goto cleanup;
  616. }
  617. ret = vmbus_bus_init(irq);
  618. if (ret)
  619. goto cleanup;
  620. return 0;
  621. cleanup:
  622. acpi_bus_unregister_driver(&vmbus_acpi_driver);
  623. hv_acpi_dev = NULL;
  624. return ret;
  625. }
  626. static void __exit vmbus_exit(void)
  627. {
  628. free_irq(irq, hv_acpi_dev);
  629. vmbus_free_channels();
  630. bus_unregister(&hv_bus);
  631. hv_cleanup();
  632. acpi_bus_unregister_driver(&vmbus_acpi_driver);
  633. }
  634. MODULE_LICENSE("GPL");
  635. MODULE_VERSION(HV_DRV_VERSION);
  636. subsys_initcall(hv_acpi_init);
  637. module_exit(vmbus_exit);