vmbus_drv.c 21 KB

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