vmbus_drv.c 21 KB

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