vmbus_drv.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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 int vmbus_exists(void)
  58. {
  59. if (hv_acpi_dev == NULL)
  60. return -ENODEV;
  61. return 0;
  62. }
  63. static void get_channel_info(struct hv_device *device,
  64. struct hv_device_info *info)
  65. {
  66. struct vmbus_channel_debug_info debug_info;
  67. if (!device->channel)
  68. return;
  69. vmbus_get_debug_info(device->channel, &debug_info);
  70. info->chn_id = debug_info.relid;
  71. info->chn_state = debug_info.state;
  72. memcpy(&info->chn_type, &debug_info.interfacetype,
  73. sizeof(uuid_le));
  74. memcpy(&info->chn_instance, &debug_info.interface_instance,
  75. sizeof(uuid_le));
  76. info->monitor_id = debug_info.monitorid;
  77. info->server_monitor_pending = debug_info.servermonitor_pending;
  78. info->server_monitor_latency = debug_info.servermonitor_latency;
  79. info->server_monitor_conn_id = debug_info.servermonitor_connectionid;
  80. info->client_monitor_pending = debug_info.clientmonitor_pending;
  81. info->client_monitor_latency = debug_info.clientmonitor_latency;
  82. info->client_monitor_conn_id = debug_info.clientmonitor_connectionid;
  83. info->inbound.int_mask = debug_info.inbound.current_interrupt_mask;
  84. info->inbound.read_idx = debug_info.inbound.current_read_index;
  85. info->inbound.write_idx = debug_info.inbound.current_write_index;
  86. info->inbound.bytes_avail_toread =
  87. debug_info.inbound.bytes_avail_toread;
  88. info->inbound.bytes_avail_towrite =
  89. debug_info.inbound.bytes_avail_towrite;
  90. info->outbound.int_mask =
  91. debug_info.outbound.current_interrupt_mask;
  92. info->outbound.read_idx = debug_info.outbound.current_read_index;
  93. info->outbound.write_idx = debug_info.outbound.current_write_index;
  94. info->outbound.bytes_avail_toread =
  95. debug_info.outbound.bytes_avail_toread;
  96. info->outbound.bytes_avail_towrite =
  97. debug_info.outbound.bytes_avail_towrite;
  98. }
  99. #define VMBUS_ALIAS_LEN ((sizeof((struct hv_vmbus_device_id *)0)->guid) * 2)
  100. static void print_alias_name(struct hv_device *hv_dev, char *alias_name)
  101. {
  102. int i;
  103. for (i = 0; i < VMBUS_ALIAS_LEN; i += 2)
  104. sprintf(&alias_name[i], "%02x", hv_dev->dev_type.b[i/2]);
  105. }
  106. /*
  107. * vmbus_show_device_attr - Show the device attribute in sysfs.
  108. *
  109. * This is invoked when user does a
  110. * "cat /sys/bus/vmbus/devices/<busdevice>/<attr name>"
  111. */
  112. static ssize_t vmbus_show_device_attr(struct device *dev,
  113. struct device_attribute *dev_attr,
  114. char *buf)
  115. {
  116. struct hv_device *hv_dev = device_to_hv_device(dev);
  117. struct hv_device_info *device_info;
  118. char alias_name[VMBUS_ALIAS_LEN + 1];
  119. int ret = 0;
  120. device_info = kzalloc(sizeof(struct hv_device_info), GFP_KERNEL);
  121. if (!device_info)
  122. return ret;
  123. get_channel_info(hv_dev, device_info);
  124. if (!strcmp(dev_attr->attr.name, "class_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_type.b[3],
  128. device_info->chn_type.b[2],
  129. device_info->chn_type.b[1],
  130. device_info->chn_type.b[0],
  131. device_info->chn_type.b[5],
  132. device_info->chn_type.b[4],
  133. device_info->chn_type.b[7],
  134. device_info->chn_type.b[6],
  135. device_info->chn_type.b[8],
  136. device_info->chn_type.b[9],
  137. device_info->chn_type.b[10],
  138. device_info->chn_type.b[11],
  139. device_info->chn_type.b[12],
  140. device_info->chn_type.b[13],
  141. device_info->chn_type.b[14],
  142. device_info->chn_type.b[15]);
  143. } else if (!strcmp(dev_attr->attr.name, "device_id")) {
  144. ret = sprintf(buf, "{%02x%02x%02x%02x-%02x%02x-%02x%02x-"
  145. "%02x%02x%02x%02x%02x%02x%02x%02x}\n",
  146. device_info->chn_instance.b[3],
  147. device_info->chn_instance.b[2],
  148. device_info->chn_instance.b[1],
  149. device_info->chn_instance.b[0],
  150. device_info->chn_instance.b[5],
  151. device_info->chn_instance.b[4],
  152. device_info->chn_instance.b[7],
  153. device_info->chn_instance.b[6],
  154. device_info->chn_instance.b[8],
  155. device_info->chn_instance.b[9],
  156. device_info->chn_instance.b[10],
  157. device_info->chn_instance.b[11],
  158. device_info->chn_instance.b[12],
  159. device_info->chn_instance.b[13],
  160. device_info->chn_instance.b[14],
  161. device_info->chn_instance.b[15]);
  162. } else if (!strcmp(dev_attr->attr.name, "modalias")) {
  163. print_alias_name(hv_dev, alias_name);
  164. ret = sprintf(buf, "vmbus:%s\n", alias_name);
  165. } else if (!strcmp(dev_attr->attr.name, "state")) {
  166. ret = sprintf(buf, "%d\n", device_info->chn_state);
  167. } else if (!strcmp(dev_attr->attr.name, "id")) {
  168. ret = sprintf(buf, "%d\n", device_info->chn_id);
  169. } else if (!strcmp(dev_attr->attr.name, "out_intr_mask")) {
  170. ret = sprintf(buf, "%d\n", device_info->outbound.int_mask);
  171. } else if (!strcmp(dev_attr->attr.name, "out_read_index")) {
  172. ret = sprintf(buf, "%d\n", device_info->outbound.read_idx);
  173. } else if (!strcmp(dev_attr->attr.name, "out_write_index")) {
  174. ret = sprintf(buf, "%d\n", device_info->outbound.write_idx);
  175. } else if (!strcmp(dev_attr->attr.name, "out_read_bytes_avail")) {
  176. ret = sprintf(buf, "%d\n",
  177. device_info->outbound.bytes_avail_toread);
  178. } else if (!strcmp(dev_attr->attr.name, "out_write_bytes_avail")) {
  179. ret = sprintf(buf, "%d\n",
  180. device_info->outbound.bytes_avail_towrite);
  181. } else if (!strcmp(dev_attr->attr.name, "in_intr_mask")) {
  182. ret = sprintf(buf, "%d\n", device_info->inbound.int_mask);
  183. } else if (!strcmp(dev_attr->attr.name, "in_read_index")) {
  184. ret = sprintf(buf, "%d\n", device_info->inbound.read_idx);
  185. } else if (!strcmp(dev_attr->attr.name, "in_write_index")) {
  186. ret = sprintf(buf, "%d\n", device_info->inbound.write_idx);
  187. } else if (!strcmp(dev_attr->attr.name, "in_read_bytes_avail")) {
  188. ret = sprintf(buf, "%d\n",
  189. device_info->inbound.bytes_avail_toread);
  190. } else if (!strcmp(dev_attr->attr.name, "in_write_bytes_avail")) {
  191. ret = sprintf(buf, "%d\n",
  192. device_info->inbound.bytes_avail_towrite);
  193. } else if (!strcmp(dev_attr->attr.name, "monitor_id")) {
  194. ret = sprintf(buf, "%d\n", device_info->monitor_id);
  195. } else if (!strcmp(dev_attr->attr.name, "server_monitor_pending")) {
  196. ret = sprintf(buf, "%d\n", device_info->server_monitor_pending);
  197. } else if (!strcmp(dev_attr->attr.name, "server_monitor_latency")) {
  198. ret = sprintf(buf, "%d\n", device_info->server_monitor_latency);
  199. } else if (!strcmp(dev_attr->attr.name, "server_monitor_conn_id")) {
  200. ret = sprintf(buf, "%d\n",
  201. device_info->server_monitor_conn_id);
  202. } else if (!strcmp(dev_attr->attr.name, "client_monitor_pending")) {
  203. ret = sprintf(buf, "%d\n", device_info->client_monitor_pending);
  204. } else if (!strcmp(dev_attr->attr.name, "client_monitor_latency")) {
  205. ret = sprintf(buf, "%d\n", device_info->client_monitor_latency);
  206. } else if (!strcmp(dev_attr->attr.name, "client_monitor_conn_id")) {
  207. ret = sprintf(buf, "%d\n",
  208. device_info->client_monitor_conn_id);
  209. }
  210. kfree(device_info);
  211. return ret;
  212. }
  213. /* Set up per device attributes in /sys/bus/vmbus/devices/<bus device> */
  214. static struct device_attribute vmbus_device_attrs[] = {
  215. __ATTR(id, S_IRUGO, vmbus_show_device_attr, NULL),
  216. __ATTR(state, S_IRUGO, vmbus_show_device_attr, NULL),
  217. __ATTR(class_id, S_IRUGO, vmbus_show_device_attr, NULL),
  218. __ATTR(device_id, S_IRUGO, vmbus_show_device_attr, NULL),
  219. __ATTR(monitor_id, S_IRUGO, vmbus_show_device_attr, NULL),
  220. __ATTR(modalias, S_IRUGO, vmbus_show_device_attr, NULL),
  221. __ATTR(server_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL),
  222. __ATTR(server_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL),
  223. __ATTR(server_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL),
  224. __ATTR(client_monitor_pending, S_IRUGO, vmbus_show_device_attr, NULL),
  225. __ATTR(client_monitor_latency, S_IRUGO, vmbus_show_device_attr, NULL),
  226. __ATTR(client_monitor_conn_id, S_IRUGO, vmbus_show_device_attr, NULL),
  227. __ATTR(out_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
  228. __ATTR(out_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
  229. __ATTR(out_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
  230. __ATTR(out_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
  231. __ATTR(out_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
  232. __ATTR(in_intr_mask, S_IRUGO, vmbus_show_device_attr, NULL),
  233. __ATTR(in_read_index, S_IRUGO, vmbus_show_device_attr, NULL),
  234. __ATTR(in_write_index, S_IRUGO, vmbus_show_device_attr, NULL),
  235. __ATTR(in_read_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
  236. __ATTR(in_write_bytes_avail, S_IRUGO, vmbus_show_device_attr, NULL),
  237. __ATTR_NULL
  238. };
  239. /*
  240. * vmbus_uevent - add uevent for our device
  241. *
  242. * This routine is invoked when a device is added or removed on the vmbus to
  243. * generate a uevent to udev in the userspace. The udev will then look at its
  244. * rule and the uevent generated here to load the appropriate driver
  245. *
  246. * The alias string will be of the form vmbus:guid where guid is the string
  247. * representation of the device guid (each byte of the guid will be
  248. * represented with two hex characters.
  249. */
  250. static int vmbus_uevent(struct device *device, struct kobj_uevent_env *env)
  251. {
  252. struct hv_device *dev = device_to_hv_device(device);
  253. int ret;
  254. char alias_name[VMBUS_ALIAS_LEN + 1];
  255. print_alias_name(dev, alias_name);
  256. ret = add_uevent_var(env, "MODALIAS=vmbus:%s", alias_name);
  257. return ret;
  258. }
  259. static uuid_le null_guid;
  260. static inline bool is_null_guid(const __u8 *guid)
  261. {
  262. if (memcmp(guid, &null_guid, sizeof(uuid_le)))
  263. return false;
  264. return true;
  265. }
  266. /*
  267. * Return a matching hv_vmbus_device_id pointer.
  268. * If there is no match, return NULL.
  269. */
  270. static const struct hv_vmbus_device_id *hv_vmbus_get_id(
  271. const struct hv_vmbus_device_id *id,
  272. __u8 *guid)
  273. {
  274. for (; !is_null_guid(id->guid); id++)
  275. if (!memcmp(&id->guid, guid, sizeof(uuid_le)))
  276. return id;
  277. return NULL;
  278. }
  279. /*
  280. * vmbus_match - Attempt to match the specified device to the specified driver
  281. */
  282. static int vmbus_match(struct device *device, struct device_driver *driver)
  283. {
  284. struct hv_driver *drv = drv_to_hv_drv(driver);
  285. struct hv_device *hv_dev = device_to_hv_device(device);
  286. if (hv_vmbus_get_id(drv->id_table, hv_dev->dev_type.b))
  287. return 1;
  288. return 0;
  289. }
  290. /*
  291. * vmbus_probe - Add the new vmbus's child device
  292. */
  293. static int vmbus_probe(struct device *child_device)
  294. {
  295. int ret = 0;
  296. struct hv_driver *drv =
  297. drv_to_hv_drv(child_device->driver);
  298. struct hv_device *dev = device_to_hv_device(child_device);
  299. const struct hv_vmbus_device_id *dev_id;
  300. dev_id = hv_vmbus_get_id(drv->id_table, dev->dev_type.b);
  301. if (drv->probe) {
  302. ret = drv->probe(dev, dev_id);
  303. if (ret != 0)
  304. pr_err("probe failed for device %s (%d)\n",
  305. dev_name(child_device), ret);
  306. } else {
  307. pr_err("probe not set for driver %s\n",
  308. dev_name(child_device));
  309. ret = -ENODEV;
  310. }
  311. return ret;
  312. }
  313. /*
  314. * vmbus_remove - Remove a vmbus device
  315. */
  316. static int vmbus_remove(struct device *child_device)
  317. {
  318. struct hv_driver *drv = drv_to_hv_drv(child_device->driver);
  319. struct hv_device *dev = device_to_hv_device(child_device);
  320. if (drv->remove)
  321. drv->remove(dev);
  322. else
  323. pr_err("remove not set for driver %s\n",
  324. dev_name(child_device));
  325. return 0;
  326. }
  327. /*
  328. * vmbus_shutdown - Shutdown a vmbus device
  329. */
  330. static void vmbus_shutdown(struct device *child_device)
  331. {
  332. struct hv_driver *drv;
  333. struct hv_device *dev = device_to_hv_device(child_device);
  334. /* The device may not be attached yet */
  335. if (!child_device->driver)
  336. return;
  337. drv = drv_to_hv_drv(child_device->driver);
  338. if (drv->shutdown)
  339. drv->shutdown(dev);
  340. return;
  341. }
  342. /*
  343. * vmbus_device_release - Final callback release of the vmbus child device
  344. */
  345. static void vmbus_device_release(struct device *device)
  346. {
  347. struct hv_device *hv_dev = device_to_hv_device(device);
  348. kfree(hv_dev);
  349. }
  350. /* The one and only one */
  351. static struct bus_type hv_bus = {
  352. .name = "vmbus",
  353. .match = vmbus_match,
  354. .shutdown = vmbus_shutdown,
  355. .remove = vmbus_remove,
  356. .probe = vmbus_probe,
  357. .uevent = vmbus_uevent,
  358. .dev_attrs = vmbus_device_attrs,
  359. };
  360. static const char *driver_name = "hyperv";
  361. struct onmessage_work_context {
  362. struct work_struct work;
  363. struct hv_message msg;
  364. };
  365. static void vmbus_onmessage_work(struct work_struct *work)
  366. {
  367. struct onmessage_work_context *ctx;
  368. ctx = container_of(work, struct onmessage_work_context,
  369. work);
  370. vmbus_onmessage(&ctx->msg);
  371. kfree(ctx);
  372. }
  373. static void vmbus_on_msg_dpc(unsigned long data)
  374. {
  375. int cpu = smp_processor_id();
  376. void *page_addr = hv_context.synic_message_page[cpu];
  377. struct hv_message *msg = (struct hv_message *)page_addr +
  378. VMBUS_MESSAGE_SINT;
  379. struct onmessage_work_context *ctx;
  380. while (1) {
  381. if (msg->header.message_type == HVMSG_NONE) {
  382. /* no msg */
  383. break;
  384. } else {
  385. ctx = kmalloc(sizeof(*ctx), GFP_ATOMIC);
  386. if (ctx == NULL)
  387. continue;
  388. INIT_WORK(&ctx->work, vmbus_onmessage_work);
  389. memcpy(&ctx->msg, msg, sizeof(*msg));
  390. queue_work(vmbus_connection.work_queue, &ctx->work);
  391. }
  392. msg->header.message_type = HVMSG_NONE;
  393. /*
  394. * Make sure the write to MessageType (ie set to
  395. * HVMSG_NONE) happens before we read the
  396. * MessagePending and EOMing. Otherwise, the EOMing
  397. * will not deliver any more messages since there is
  398. * no empty slot
  399. */
  400. smp_mb();
  401. if (msg->header.message_flags.msg_pending) {
  402. /*
  403. * This will cause message queue rescan to
  404. * possibly deliver another msg from the
  405. * hypervisor
  406. */
  407. wrmsrl(HV_X64_MSR_EOM, 0);
  408. }
  409. }
  410. }
  411. static irqreturn_t vmbus_isr(int irq, void *dev_id)
  412. {
  413. int cpu = smp_processor_id();
  414. void *page_addr;
  415. struct hv_message *msg;
  416. union hv_synic_event_flags *event;
  417. bool handled = false;
  418. /*
  419. * Check for events before checking for messages. This is the order
  420. * in which events and messages are checked in Windows guests on
  421. * Hyper-V, and the Windows team suggested we do the same.
  422. */
  423. page_addr = hv_context.synic_event_page[cpu];
  424. event = (union hv_synic_event_flags *)page_addr + VMBUS_MESSAGE_SINT;
  425. /* Since we are a child, we only need to check bit 0 */
  426. if (sync_test_and_clear_bit(0, (unsigned long *) &event->flags32[0])) {
  427. handled = true;
  428. tasklet_schedule(&event_dpc);
  429. }
  430. page_addr = hv_context.synic_message_page[cpu];
  431. msg = (struct hv_message *)page_addr + VMBUS_MESSAGE_SINT;
  432. /* Check if there are actual msgs to be processed */
  433. if (msg->header.message_type != HVMSG_NONE) {
  434. handled = true;
  435. tasklet_schedule(&msg_dpc);
  436. }
  437. if (handled)
  438. return IRQ_HANDLED;
  439. else
  440. return IRQ_NONE;
  441. }
  442. /*
  443. * vmbus_bus_init -Main vmbus driver initialization routine.
  444. *
  445. * Here, we
  446. * - initialize the vmbus driver context
  447. * - invoke the vmbus hv main init routine
  448. * - get the irq resource
  449. * - retrieve the channel offers
  450. */
  451. static int vmbus_bus_init(int irq)
  452. {
  453. int ret;
  454. unsigned int vector;
  455. /* Hypervisor initialization...setup hypercall page..etc */
  456. ret = hv_init();
  457. if (ret != 0) {
  458. pr_err("Unable to initialize the hypervisor - 0x%x\n", ret);
  459. return ret;
  460. }
  461. tasklet_init(&msg_dpc, vmbus_on_msg_dpc, 0);
  462. tasklet_init(&event_dpc, vmbus_on_event, 0);
  463. ret = bus_register(&hv_bus);
  464. if (ret)
  465. goto err_cleanup;
  466. ret = request_irq(irq, vmbus_isr, 0, driver_name, hv_acpi_dev);
  467. if (ret != 0) {
  468. pr_err("Unable to request IRQ %d\n",
  469. irq);
  470. goto err_unregister;
  471. }
  472. vector = IRQ0_VECTOR + irq;
  473. /*
  474. * Notify the hypervisor of our irq and
  475. * connect to the host.
  476. */
  477. on_each_cpu(hv_synic_init, (void *)&vector, 1);
  478. ret = vmbus_connect();
  479. if (ret)
  480. goto err_irq;
  481. vmbus_request_offers();
  482. return 0;
  483. err_irq:
  484. free_irq(irq, hv_acpi_dev);
  485. err_unregister:
  486. bus_unregister(&hv_bus);
  487. err_cleanup:
  488. hv_cleanup();
  489. return ret;
  490. }
  491. /**
  492. * __vmbus_child_driver_register - Register a vmbus's driver
  493. * @drv: Pointer to driver structure you want to register
  494. * @owner: owner module of the drv
  495. * @mod_name: module name string
  496. *
  497. * Registers the given driver with Linux through the 'driver_register()' call
  498. * and sets up the hyper-v vmbus handling for this driver.
  499. * It will return the state of the 'driver_register()' call.
  500. *
  501. */
  502. int __vmbus_driver_register(struct hv_driver *hv_driver, struct module *owner, const char *mod_name)
  503. {
  504. int ret;
  505. pr_info("registering driver %s\n", hv_driver->name);
  506. ret = vmbus_exists();
  507. if (ret < 0)
  508. return ret;
  509. hv_driver->driver.name = hv_driver->name;
  510. hv_driver->driver.owner = owner;
  511. hv_driver->driver.mod_name = mod_name;
  512. hv_driver->driver.bus = &hv_bus;
  513. ret = driver_register(&hv_driver->driver);
  514. vmbus_request_offers();
  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. init_completion(&probe_event);
  633. /*
  634. * Get irq resources first.
  635. */
  636. ret = acpi_bus_register_driver(&vmbus_acpi_driver);
  637. if (ret)
  638. return ret;
  639. t = wait_for_completion_timeout(&probe_event, 5*HZ);
  640. if (t == 0) {
  641. ret = -ETIMEDOUT;
  642. goto cleanup;
  643. }
  644. if (irq <= 0) {
  645. ret = -ENODEV;
  646. goto cleanup;
  647. }
  648. ret = vmbus_bus_init(irq);
  649. if (ret)
  650. goto cleanup;
  651. return 0;
  652. cleanup:
  653. acpi_bus_unregister_driver(&vmbus_acpi_driver);
  654. hv_acpi_dev = NULL;
  655. return ret;
  656. }
  657. static void __exit vmbus_exit(void)
  658. {
  659. free_irq(irq, hv_acpi_dev);
  660. vmbus_free_channels();
  661. bus_unregister(&hv_bus);
  662. hv_cleanup();
  663. acpi_bus_unregister_driver(&vmbus_acpi_driver);
  664. }
  665. MODULE_LICENSE("GPL");
  666. MODULE_VERSION(HV_DRV_VERSION);
  667. subsys_initcall(hv_acpi_init);
  668. module_exit(vmbus_exit);