vmbus_drv.c 21 KB

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