vmbus_drv.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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 "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 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. page_addr = hv_context.synic_event_page[cpu];
  386. if (page_addr == NULL)
  387. return IRQ_NONE;
  388. event = (union hv_synic_event_flags *)page_addr +
  389. VMBUS_MESSAGE_SINT;
  390. /*
  391. * Check for events before checking for messages. This is the order
  392. * in which events and messages are checked in Windows guests on
  393. * Hyper-V, and the Windows team suggested we do the same.
  394. */
  395. if ((vmbus_proto_version == VERSION_WS2008) ||
  396. (vmbus_proto_version == VERSION_WIN7)) {
  397. /* Since we are a child, we only need to check bit 0 */
  398. if (sync_test_and_clear_bit(0,
  399. (unsigned long *) &event->flags32[0])) {
  400. handled = true;
  401. }
  402. } else {
  403. /*
  404. * Our host is win8 or above. The signaling mechanism
  405. * has changed and we can directly look at the event page.
  406. * If bit n is set then we have an interrup on the channel
  407. * whose id is n.
  408. */
  409. handled = true;
  410. }
  411. if (handled)
  412. tasklet_schedule(hv_context.event_dpc[cpu]);
  413. page_addr = hv_context.synic_message_page[cpu];
  414. msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
  415. /* Check if there are actual msgs to be processed */
  416. if (msg->header.message_type != HVMSG_NONE) {
  417. handled = true;
  418. tasklet_schedule(&msg_dpc);
  419. }
  420. if (handled)
  421. return IRQ_HANDLED;
  422. else
  423. return IRQ_NONE;
  424. }
  425. /*
  426. * vmbus interrupt flow handler:
  427. * vmbus interrupts can concurrently occur on multiple CPUs and
  428. * can be handled concurrently.
  429. */
  430. void vmbus_flow_handler(unsigned int irq, struct irq_desc *desc)
  431. {
  432. kstat_incr_irqs_this_cpu(irq, desc);
  433. desc->action->handler(irq, desc->action->dev_id);
  434. }
  435. /*
  436. * vmbus_bus_init -Main vmbus driver initialization routine.
  437. *
  438. * Here, we
  439. * - initialize the vmbus driver context
  440. * - invoke the vmbus hv main init routine
  441. * - get the irq resource
  442. * - retrieve the channel offers
  443. */
  444. static int vmbus_bus_init(int irq)
  445. {
  446. int ret;
  447. unsigned int vector;
  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. vector = IRQ0_VECTOR + irq;
  471. /*
  472. * Notify the hypervisor of our irq and
  473. * connect to the host.
  474. */
  475. on_each_cpu(hv_synic_init, (void *)&vector, 1);
  476. ret = vmbus_connect();
  477. if (ret)
  478. goto err_irq;
  479. vmbus_request_offers();
  480. return 0;
  481. err_irq:
  482. free_irq(irq, hv_acpi_dev);
  483. err_unregister:
  484. bus_unregister(&hv_bus);
  485. err_cleanup:
  486. hv_cleanup();
  487. return ret;
  488. }
  489. /**
  490. * __vmbus_child_driver_register - Register a vmbus's driver
  491. * @drv: Pointer to driver structure you want to register
  492. * @owner: owner module of the drv
  493. * @mod_name: module name string
  494. *
  495. * Registers the given driver with Linux through the 'driver_register()' call
  496. * and sets up the hyper-v vmbus handling for this driver.
  497. * It will return the state of the 'driver_register()' call.
  498. *
  499. */
  500. int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
  501. {
  502. int ret;
  503. pr_info("registering driver %s\n", hv_driver->name);
  504. ret = vmbus_exists();
  505. if (ret < 0)
  506. return ret;
  507. hv_driver->driver.name = hv_driver->name;
  508. hv_driver->driver.owner = owner;
  509. hv_driver->driver.mod_name = mod_name;
  510. hv_driver->driver.bus = &hv_bus;
  511. ret = driver_register(&hv_driver->driver);
  512. return ret;
  513. }
  514. EXPORT_SYMBOL_GPL(__vmbus_driver_register);
  515. /**
  516. * vmbus_driver_unregister() - Unregister a vmbus's driver
  517. * @drv: Pointer to driver structure you want to un-register
  518. *
  519. * Un-register the given driver that was previous registered with a call to
  520. * vmbus_driver_register()
  521. */
  522. void vmbus_driver_unregister(struct hv_driver *hv_driver)
  523. {
  524. pr_info("unregistering driver %s\n", hv_driver->name);
  525. if (!vmbus_exists())
  526. driver_unregister(&hv_driver->driver);
  527. }
  528. EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
  529. /*
  530. * vmbus_device_create - Creates and registers a new child device
  531. * on the vmbus.
  532. */
  533. struct hv_device *vmbus_device_create(uuid_le *type,
  534. uuid_le *instance,
  535. struct vmbus_channel *channel)
  536. {
  537. struct hv_device *child_device_obj;
  538. child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
  539. if (!child_device_obj) {
  540. pr_err("Unable to allocate device object for child device\n");
  541. return NULL;
  542. }
  543. child_device_obj->channel = channel;
  544. memcpy(&child_device_obj->dev_type, type, sizeof(uuid_le));
  545. memcpy(&child_device_obj->dev_instance, instance,
  546. sizeof(uuid_le));
  547. return child_device_obj;
  548. }
  549. /*
  550. * vmbus_device_register - Register the child device
  551. */
  552. int vmbus_device_register(struct hv_device *child_device_obj)
  553. {
  554. int ret = 0;
  555. static atomic_t device_num = ATOMIC_INIT(0);
  556. dev_set_name(&child_device_obj->device, "vmbus_0_%d",
  557. atomic_inc_return(&device_num));
  558. child_device_obj->device.bus = &hv_bus;
  559. child_device_obj->device.parent = &hv_acpi_dev->dev;
  560. child_device_obj->device.release = vmbus_device_release;
  561. /*
  562. * Register with the LDM. This will kick off the driver/device
  563. * binding...which will eventually call vmbus_match() and vmbus_probe()
  564. */
  565. ret = device_register(&child_device_obj->device);
  566. if (ret)
  567. pr_err("Unable to register child device\n");
  568. else
  569. pr_info("child device %s registered\n",
  570. dev_name(&child_device_obj->device));
  571. return ret;
  572. }
  573. /*
  574. * vmbus_device_unregister - Remove the specified child device
  575. * from the vmbus.
  576. */
  577. void vmbus_device_unregister(struct hv_device *device_obj)
  578. {
  579. /*
  580. * Kick off the process of unregistering the device.
  581. * This will call vmbus_remove() and eventually vmbus_device_release()
  582. */
  583. device_unregister(&device_obj->device);
  584. pr_info("child device %s unregistered\n",
  585. dev_name(&device_obj->device));
  586. }
  587. /*
  588. * VMBUS is an acpi enumerated device. Get the the IRQ information
  589. * from DSDT.
  590. */
  591. static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *irq)
  592. {
  593. if (res->type == ACPI_RESOURCE_TYPE_IRQ) {
  594. struct acpi_resource_irq *irqp;
  595. irqp = &res->data.irq;
  596. *((unsigned int *)irq) = irqp->interrupts[0];
  597. }
  598. return AE_OK;
  599. }
  600. static int vmbus_acpi_add(struct acpi_device *device)
  601. {
  602. acpi_status result;
  603. hv_acpi_dev = device;
  604. result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  605. vmbus_walk_resources, &irq);
  606. if (ACPI_FAILURE(result)) {
  607. complete(&probe_event);
  608. return -ENODEV;
  609. }
  610. complete(&probe_event);
  611. return 0;
  612. }
  613. static const struct acpi_device_id vmbus_acpi_device_ids[] = {
  614. {"VMBUS", 0},
  615. {"VMBus", 0},
  616. {"", 0},
  617. };
  618. MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
  619. static struct acpi_driver vmbus_acpi_driver = {
  620. .name = "vmbus",
  621. .ids = vmbus_acpi_device_ids,
  622. .ops = {
  623. .add = vmbus_acpi_add,
  624. },
  625. };
  626. static int __init hv_acpi_init(void)
  627. {
  628. int ret, t;
  629. if (x86_hyper != &x86_hyper_ms_hyperv)
  630. return -ENODEV;
  631. init_completion(&probe_event);
  632. /*
  633. * Get irq resources first.
  634. */
  635. ret = acpi_bus_register_driver(&vmbus_acpi_driver);
  636. if (ret)
  637. return ret;
  638. t = wait_for_completion_timeout(&probe_event, 5*HZ);
  639. if (t == 0) {
  640. ret = -ETIMEDOUT;
  641. goto cleanup;
  642. }
  643. if (irq <= 0) {
  644. ret = -ENODEV;
  645. goto cleanup;
  646. }
  647. ret = vmbus_bus_init(irq);
  648. if (ret)
  649. goto cleanup;
  650. return 0;
  651. cleanup:
  652. acpi_bus_unregister_driver(&vmbus_acpi_driver);
  653. hv_acpi_dev = NULL;
  654. return ret;
  655. }
  656. static void __exit vmbus_exit(void)
  657. {
  658. free_irq(irq, hv_acpi_dev);
  659. vmbus_free_channels();
  660. bus_unregister(&hv_bus);
  661. hv_cleanup();
  662. acpi_bus_unregister_driver(&vmbus_acpi_driver);
  663. }
  664. MODULE_LICENSE("GPL");
  665. MODULE_VERSION(HV_DRV_VERSION);
  666. subsys_initcall(hv_acpi_init);
  667. module_exit(vmbus_exit);