vmbus_drv.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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. smp_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. /*
  475. * Initialize the per-cpu interrupt state and
  476. * connect to the host.
  477. */
  478. on_each_cpu(hv_synic_init, NULL, 1);
  479. ret = vmbus_connect();
  480. if (ret)
  481. goto err_irq;
  482. vmbus_request_offers();
  483. return 0;
  484. err_irq:
  485. free_irq(irq, hv_acpi_dev);
  486. err_unregister:
  487. bus_unregister(&hv_bus);
  488. err_cleanup:
  489. hv_cleanup();
  490. return ret;
  491. }
  492. /**
  493. * __vmbus_child_driver_register - Register a vmbus's driver
  494. * @drv: Pointer to driver structure you want to register
  495. * @owner: owner module of the drv
  496. * @mod_name: module name string
  497. *
  498. * Registers the given driver with Linux through the 'driver_register()' call
  499. * and sets up the hyper-v vmbus handling for this driver.
  500. * It will return the state of the 'driver_register()' call.
  501. *
  502. */
  503. int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
  504. {
  505. int ret;
  506. pr_info("registering driver %s\n", hv_driver->name);
  507. ret = vmbus_exists();
  508. if (ret < 0)
  509. return ret;
  510. hv_driver->driver.name = hv_driver->name;
  511. hv_driver->driver.owner = owner;
  512. hv_driver->driver.mod_name = mod_name;
  513. hv_driver->driver.bus = &hv_bus;
  514. ret = driver_register(&hv_driver->driver);
  515. return ret;
  516. }
  517. EXPORT_SYMBOL_GPL(__vmbus_driver_register);
  518. /**
  519. * vmbus_driver_unregister() - Unregister a vmbus's driver
  520. * @drv: Pointer to driver structure you want to un-register
  521. *
  522. * Un-register the given driver that was previous registered with a call to
  523. * vmbus_driver_register()
  524. */
  525. void vmbus_driver_unregister(struct hv_driver *hv_driver)
  526. {
  527. pr_info("unregistering driver %s\n", hv_driver->name);
  528. if (!vmbus_exists())
  529. driver_unregister(&hv_driver->driver);
  530. }
  531. EXPORT_SYMBOL_GPL(vmbus_driver_unregister);
  532. /*
  533. * vmbus_device_create - Creates and registers a new child device
  534. * on the vmbus.
  535. */
  536. struct hv_device *vmbus_device_create(uuid_le *type,
  537. uuid_le *instance,
  538. struct vmbus_channel *channel)
  539. {
  540. struct hv_device *child_device_obj;
  541. child_device_obj = kzalloc(sizeof(struct hv_device), GFP_KERNEL);
  542. if (!child_device_obj) {
  543. pr_err("Unable to allocate device object for child device\n");
  544. return NULL;
  545. }
  546. child_device_obj->channel = channel;
  547. memcpy(&child_device_obj->dev_type, type, sizeof(uuid_le));
  548. memcpy(&child_device_obj->dev_instance, instance,
  549. sizeof(uuid_le));
  550. return child_device_obj;
  551. }
  552. /*
  553. * vmbus_device_register - Register the child device
  554. */
  555. int vmbus_device_register(struct hv_device *child_device_obj)
  556. {
  557. int ret = 0;
  558. static atomic_t device_num = ATOMIC_INIT(0);
  559. dev_set_name(&child_device_obj->device, "vmbus_0_%d",
  560. atomic_inc_return(&device_num));
  561. child_device_obj->device.bus = &hv_bus;
  562. child_device_obj->device.parent = &hv_acpi_dev->dev;
  563. child_device_obj->device.release = vmbus_device_release;
  564. /*
  565. * Register with the LDM. This will kick off the driver/device
  566. * binding...which will eventually call vmbus_match() and vmbus_probe()
  567. */
  568. ret = device_register(&child_device_obj->device);
  569. if (ret)
  570. pr_err("Unable to register child device\n");
  571. else
  572. pr_info("child device %s registered\n",
  573. dev_name(&child_device_obj->device));
  574. return ret;
  575. }
  576. /*
  577. * vmbus_device_unregister - Remove the specified child device
  578. * from the vmbus.
  579. */
  580. void vmbus_device_unregister(struct hv_device *device_obj)
  581. {
  582. /*
  583. * Kick off the process of unregistering the device.
  584. * This will call vmbus_remove() and eventually vmbus_device_release()
  585. */
  586. device_unregister(&device_obj->device);
  587. pr_info("child device %s unregistered\n",
  588. dev_name(&device_obj->device));
  589. }
  590. /*
  591. * VMBUS is an acpi enumerated device. Get the the IRQ information
  592. * from DSDT.
  593. */
  594. static acpi_status vmbus_walk_resources(struct acpi_resource *res, void *irq)
  595. {
  596. if (res->type == ACPI_RESOURCE_TYPE_IRQ) {
  597. struct acpi_resource_irq *irqp;
  598. irqp = &res->data.irq;
  599. *((unsigned int *)irq) = irqp->interrupts[0];
  600. }
  601. return AE_OK;
  602. }
  603. static int vmbus_acpi_add(struct acpi_device *device)
  604. {
  605. acpi_status result;
  606. hv_acpi_dev = device;
  607. result = acpi_walk_resources(device->handle, METHOD_NAME__CRS,
  608. vmbus_walk_resources, &irq);
  609. if (ACPI_FAILURE(result)) {
  610. complete(&probe_event);
  611. return -ENODEV;
  612. }
  613. complete(&probe_event);
  614. return 0;
  615. }
  616. static const struct acpi_device_id vmbus_acpi_device_ids[] = {
  617. {"VMBUS", 0},
  618. {"VMBus", 0},
  619. {"", 0},
  620. };
  621. MODULE_DEVICE_TABLE(acpi, vmbus_acpi_device_ids);
  622. static struct acpi_driver vmbus_acpi_driver = {
  623. .name = "vmbus",
  624. .ids = vmbus_acpi_device_ids,
  625. .ops = {
  626. .add = vmbus_acpi_add,
  627. },
  628. };
  629. static int __init hv_acpi_init(void)
  630. {
  631. int ret, t;
  632. if (x86_hyper != &x86_hyper_ms_hyperv)
  633. return -ENODEV;
  634. init_completion(&probe_event);
  635. /*
  636. * Get irq resources first.
  637. */
  638. ret = acpi_bus_register_driver(&vmbus_acpi_driver);
  639. if (ret)
  640. return ret;
  641. t = wait_for_completion_timeout(&probe_event, 5*HZ);
  642. if (t == 0) {
  643. ret = -ETIMEDOUT;
  644. goto cleanup;
  645. }
  646. if (irq <= 0) {
  647. ret = -ENODEV;
  648. goto cleanup;
  649. }
  650. ret = vmbus_bus_init(irq);
  651. if (ret)
  652. goto cleanup;
  653. return 0;
  654. cleanup:
  655. acpi_bus_unregister_driver(&vmbus_acpi_driver);
  656. hv_acpi_dev = NULL;
  657. return ret;
  658. }
  659. static void __exit vmbus_exit(void)
  660. {
  661. free_irq(irq, hv_acpi_dev);
  662. vmbus_free_channels();
  663. bus_unregister(&hv_bus);
  664. hv_cleanup();
  665. acpi_bus_unregister_driver(&vmbus_acpi_driver);
  666. }
  667. MODULE_LICENSE("GPL");
  668. MODULE_VERSION(HV_DRV_VERSION);
  669. subsys_initcall(hv_acpi_init);
  670. module_exit(vmbus_exit);