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