vmci_guest.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /*
  2. * VMware VMCI Driver
  3. *
  4. * Copyright (C) 2012 VMware, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation version 2 and no later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include <linux/vmw_vmci_defs.h>
  16. #include <linux/vmw_vmci_api.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/highmem.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mm.h>
  22. #include <linux/module.h>
  23. #include <linux/sched.h>
  24. #include <linux/slab.h>
  25. #include <linux/init.h>
  26. #include <linux/pci.h>
  27. #include <linux/smp.h>
  28. #include <linux/io.h>
  29. #include <linux/vmalloc.h>
  30. #include "vmci_datagram.h"
  31. #include "vmci_doorbell.h"
  32. #include "vmci_context.h"
  33. #include "vmci_driver.h"
  34. #include "vmci_event.h"
  35. #define PCI_DEVICE_ID_VMWARE_VMCI 0x0740
  36. #define VMCI_UTIL_NUM_RESOURCES 1
  37. static bool vmci_disable_msi;
  38. module_param_named(disable_msi, vmci_disable_msi, bool, 0);
  39. MODULE_PARM_DESC(disable_msi, "Disable MSI use in driver - (default=0)");
  40. static bool vmci_disable_msix;
  41. module_param_named(disable_msix, vmci_disable_msix, bool, 0);
  42. MODULE_PARM_DESC(disable_msix, "Disable MSI-X use in driver - (default=0)");
  43. static u32 ctx_update_sub_id = VMCI_INVALID_ID;
  44. static u32 vm_context_id = VMCI_INVALID_ID;
  45. struct vmci_guest_device {
  46. struct device *dev; /* PCI device we are attached to */
  47. void __iomem *iobase;
  48. bool exclusive_vectors;
  49. struct tasklet_struct datagram_tasklet;
  50. struct tasklet_struct bm_tasklet;
  51. void *data_buffer;
  52. void *notification_bitmap;
  53. dma_addr_t notification_base;
  54. };
  55. /* vmci_dev singleton device and supporting data*/
  56. struct pci_dev *vmci_pdev;
  57. static struct vmci_guest_device *vmci_dev_g;
  58. static DEFINE_SPINLOCK(vmci_dev_spinlock);
  59. static atomic_t vmci_num_guest_devices = ATOMIC_INIT(0);
  60. bool vmci_guest_code_active(void)
  61. {
  62. return atomic_read(&vmci_num_guest_devices) != 0;
  63. }
  64. u32 vmci_get_vm_context_id(void)
  65. {
  66. if (vm_context_id == VMCI_INVALID_ID) {
  67. struct vmci_datagram get_cid_msg;
  68. get_cid_msg.dst =
  69. vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
  70. VMCI_GET_CONTEXT_ID);
  71. get_cid_msg.src = VMCI_ANON_SRC_HANDLE;
  72. get_cid_msg.payload_size = 0;
  73. vm_context_id = vmci_send_datagram(&get_cid_msg);
  74. }
  75. return vm_context_id;
  76. }
  77. /*
  78. * VM to hypervisor call mechanism. We use the standard VMware naming
  79. * convention since shared code is calling this function as well.
  80. */
  81. int vmci_send_datagram(struct vmci_datagram *dg)
  82. {
  83. unsigned long flags;
  84. int result;
  85. /* Check args. */
  86. if (dg == NULL)
  87. return VMCI_ERROR_INVALID_ARGS;
  88. /*
  89. * Need to acquire spinlock on the device because the datagram
  90. * data may be spread over multiple pages and the monitor may
  91. * interleave device user rpc calls from multiple
  92. * VCPUs. Acquiring the spinlock precludes that
  93. * possibility. Disabling interrupts to avoid incoming
  94. * datagrams during a "rep out" and possibly landing up in
  95. * this function.
  96. */
  97. spin_lock_irqsave(&vmci_dev_spinlock, flags);
  98. if (vmci_dev_g) {
  99. iowrite8_rep(vmci_dev_g->iobase + VMCI_DATA_OUT_ADDR,
  100. dg, VMCI_DG_SIZE(dg));
  101. result = ioread32(vmci_dev_g->iobase + VMCI_RESULT_LOW_ADDR);
  102. } else {
  103. result = VMCI_ERROR_UNAVAILABLE;
  104. }
  105. spin_unlock_irqrestore(&vmci_dev_spinlock, flags);
  106. return result;
  107. }
  108. EXPORT_SYMBOL_GPL(vmci_send_datagram);
  109. /*
  110. * Gets called with the new context id if updated or resumed.
  111. * Context id.
  112. */
  113. static void vmci_guest_cid_update(u32 sub_id,
  114. const struct vmci_event_data *event_data,
  115. void *client_data)
  116. {
  117. const struct vmci_event_payld_ctx *ev_payload =
  118. vmci_event_data_const_payload(event_data);
  119. if (sub_id != ctx_update_sub_id) {
  120. pr_devel("Invalid subscriber (ID=0x%x)\n", sub_id);
  121. return;
  122. }
  123. if (!event_data || ev_payload->context_id == VMCI_INVALID_ID) {
  124. pr_devel("Invalid event data\n");
  125. return;
  126. }
  127. pr_devel("Updating context from (ID=0x%x) to (ID=0x%x) on event (type=%d)\n",
  128. vm_context_id, ev_payload->context_id, event_data->event);
  129. vm_context_id = ev_payload->context_id;
  130. }
  131. /*
  132. * Verify that the host supports the hypercalls we need. If it does not,
  133. * try to find fallback hypercalls and use those instead. Returns
  134. * true if required hypercalls (or fallback hypercalls) are
  135. * supported by the host, false otherwise.
  136. */
  137. static int vmci_check_host_caps(struct pci_dev *pdev)
  138. {
  139. bool result;
  140. struct vmci_resource_query_msg *msg;
  141. u32 msg_size = sizeof(struct vmci_resource_query_hdr) +
  142. VMCI_UTIL_NUM_RESOURCES * sizeof(u32);
  143. struct vmci_datagram *check_msg;
  144. check_msg = kzalloc(msg_size, GFP_KERNEL);
  145. if (!check_msg) {
  146. dev_err(&pdev->dev, "%s: Insufficient memory\n", __func__);
  147. return -ENOMEM;
  148. }
  149. check_msg->dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
  150. VMCI_RESOURCES_QUERY);
  151. check_msg->src = VMCI_ANON_SRC_HANDLE;
  152. check_msg->payload_size = msg_size - VMCI_DG_HEADERSIZE;
  153. msg = (struct vmci_resource_query_msg *)VMCI_DG_PAYLOAD(check_msg);
  154. msg->num_resources = VMCI_UTIL_NUM_RESOURCES;
  155. msg->resources[0] = VMCI_GET_CONTEXT_ID;
  156. /* Checks that hyper calls are supported */
  157. result = vmci_send_datagram(check_msg) == 0x01;
  158. kfree(check_msg);
  159. dev_dbg(&pdev->dev, "%s: Host capability check: %s\n",
  160. __func__, result ? "PASSED" : "FAILED");
  161. /* We need the vector. There are no fallbacks. */
  162. return result ? 0 : -ENXIO;
  163. }
  164. /*
  165. * Reads datagrams from the data in port and dispatches them. We
  166. * always start reading datagrams into only the first page of the
  167. * datagram buffer. If the datagrams don't fit into one page, we
  168. * use the maximum datagram buffer size for the remainder of the
  169. * invocation. This is a simple heuristic for not penalizing
  170. * small datagrams.
  171. *
  172. * This function assumes that it has exclusive access to the data
  173. * in port for the duration of the call.
  174. */
  175. static void vmci_dispatch_dgs(unsigned long data)
  176. {
  177. struct vmci_guest_device *vmci_dev = (struct vmci_guest_device *)data;
  178. u8 *dg_in_buffer = vmci_dev->data_buffer;
  179. struct vmci_datagram *dg;
  180. size_t dg_in_buffer_size = VMCI_MAX_DG_SIZE;
  181. size_t current_dg_in_buffer_size = PAGE_SIZE;
  182. size_t remaining_bytes;
  183. BUILD_BUG_ON(VMCI_MAX_DG_SIZE < PAGE_SIZE);
  184. ioread8_rep(vmci_dev->iobase + VMCI_DATA_IN_ADDR,
  185. vmci_dev->data_buffer, current_dg_in_buffer_size);
  186. dg = (struct vmci_datagram *)dg_in_buffer;
  187. remaining_bytes = current_dg_in_buffer_size;
  188. while (dg->dst.resource != VMCI_INVALID_ID ||
  189. remaining_bytes > PAGE_SIZE) {
  190. unsigned dg_in_size;
  191. /*
  192. * When the input buffer spans multiple pages, a datagram can
  193. * start on any page boundary in the buffer.
  194. */
  195. if (dg->dst.resource == VMCI_INVALID_ID) {
  196. dg = (struct vmci_datagram *)roundup(
  197. (uintptr_t)dg + 1, PAGE_SIZE);
  198. remaining_bytes =
  199. (size_t)(dg_in_buffer +
  200. current_dg_in_buffer_size -
  201. (u8 *)dg);
  202. continue;
  203. }
  204. dg_in_size = VMCI_DG_SIZE_ALIGNED(dg);
  205. if (dg_in_size <= dg_in_buffer_size) {
  206. int result;
  207. /*
  208. * If the remaining bytes in the datagram
  209. * buffer doesn't contain the complete
  210. * datagram, we first make sure we have enough
  211. * room for it and then we read the reminder
  212. * of the datagram and possibly any following
  213. * datagrams.
  214. */
  215. if (dg_in_size > remaining_bytes) {
  216. if (remaining_bytes !=
  217. current_dg_in_buffer_size) {
  218. /*
  219. * We move the partial
  220. * datagram to the front and
  221. * read the reminder of the
  222. * datagram and possibly
  223. * following calls into the
  224. * following bytes.
  225. */
  226. memmove(dg_in_buffer, dg_in_buffer +
  227. current_dg_in_buffer_size -
  228. remaining_bytes,
  229. remaining_bytes);
  230. dg = (struct vmci_datagram *)
  231. dg_in_buffer;
  232. }
  233. if (current_dg_in_buffer_size !=
  234. dg_in_buffer_size)
  235. current_dg_in_buffer_size =
  236. dg_in_buffer_size;
  237. ioread8_rep(vmci_dev->iobase +
  238. VMCI_DATA_IN_ADDR,
  239. vmci_dev->data_buffer +
  240. remaining_bytes,
  241. current_dg_in_buffer_size -
  242. remaining_bytes);
  243. }
  244. /*
  245. * We special case event datagrams from the
  246. * hypervisor.
  247. */
  248. if (dg->src.context == VMCI_HYPERVISOR_CONTEXT_ID &&
  249. dg->dst.resource == VMCI_EVENT_HANDLER) {
  250. result = vmci_event_dispatch(dg);
  251. } else {
  252. result = vmci_datagram_invoke_guest_handler(dg);
  253. }
  254. if (result < VMCI_SUCCESS)
  255. dev_dbg(vmci_dev->dev,
  256. "Datagram with resource (ID=0x%x) failed (err=%d)\n",
  257. dg->dst.resource, result);
  258. /* On to the next datagram. */
  259. dg = (struct vmci_datagram *)((u8 *)dg +
  260. dg_in_size);
  261. } else {
  262. size_t bytes_to_skip;
  263. /*
  264. * Datagram doesn't fit in datagram buffer of maximal
  265. * size. We drop it.
  266. */
  267. dev_dbg(vmci_dev->dev,
  268. "Failed to receive datagram (size=%u bytes)\n",
  269. dg_in_size);
  270. bytes_to_skip = dg_in_size - remaining_bytes;
  271. if (current_dg_in_buffer_size != dg_in_buffer_size)
  272. current_dg_in_buffer_size = dg_in_buffer_size;
  273. for (;;) {
  274. ioread8_rep(vmci_dev->iobase +
  275. VMCI_DATA_IN_ADDR,
  276. vmci_dev->data_buffer,
  277. current_dg_in_buffer_size);
  278. if (bytes_to_skip <= current_dg_in_buffer_size)
  279. break;
  280. bytes_to_skip -= current_dg_in_buffer_size;
  281. }
  282. dg = (struct vmci_datagram *)(dg_in_buffer +
  283. bytes_to_skip);
  284. }
  285. remaining_bytes =
  286. (size_t) (dg_in_buffer + current_dg_in_buffer_size -
  287. (u8 *)dg);
  288. if (remaining_bytes < VMCI_DG_HEADERSIZE) {
  289. /* Get the next batch of datagrams. */
  290. ioread8_rep(vmci_dev->iobase + VMCI_DATA_IN_ADDR,
  291. vmci_dev->data_buffer,
  292. current_dg_in_buffer_size);
  293. dg = (struct vmci_datagram *)dg_in_buffer;
  294. remaining_bytes = current_dg_in_buffer_size;
  295. }
  296. }
  297. }
  298. /*
  299. * Scans the notification bitmap for raised flags, clears them
  300. * and handles the notifications.
  301. */
  302. static void vmci_process_bitmap(unsigned long data)
  303. {
  304. struct vmci_guest_device *dev = (struct vmci_guest_device *)data;
  305. if (!dev->notification_bitmap) {
  306. dev_dbg(dev->dev, "No bitmap present in %s\n", __func__);
  307. return;
  308. }
  309. vmci_dbell_scan_notification_entries(dev->notification_bitmap);
  310. }
  311. /*
  312. * Interrupt handler for legacy or MSI interrupt, or for first MSI-X
  313. * interrupt (vector VMCI_INTR_DATAGRAM).
  314. */
  315. static irqreturn_t vmci_interrupt(int irq, void *_dev)
  316. {
  317. struct vmci_guest_device *dev = _dev;
  318. /*
  319. * If we are using MSI-X with exclusive vectors then we simply schedule
  320. * the datagram tasklet, since we know the interrupt was meant for us.
  321. * Otherwise we must read the ICR to determine what to do.
  322. */
  323. if (dev->exclusive_vectors) {
  324. tasklet_schedule(&dev->datagram_tasklet);
  325. } else {
  326. unsigned int icr;
  327. /* Acknowledge interrupt and determine what needs doing. */
  328. icr = ioread32(dev->iobase + VMCI_ICR_ADDR);
  329. if (icr == 0 || icr == ~0)
  330. return IRQ_NONE;
  331. if (icr & VMCI_ICR_DATAGRAM) {
  332. tasklet_schedule(&dev->datagram_tasklet);
  333. icr &= ~VMCI_ICR_DATAGRAM;
  334. }
  335. if (icr & VMCI_ICR_NOTIFICATION) {
  336. tasklet_schedule(&dev->bm_tasklet);
  337. icr &= ~VMCI_ICR_NOTIFICATION;
  338. }
  339. if (icr != 0)
  340. dev_warn(dev->dev,
  341. "Ignoring unknown interrupt cause (%d)\n",
  342. icr);
  343. }
  344. return IRQ_HANDLED;
  345. }
  346. /*
  347. * Interrupt handler for MSI-X interrupt vector VMCI_INTR_NOTIFICATION,
  348. * which is for the notification bitmap. Will only get called if we are
  349. * using MSI-X with exclusive vectors.
  350. */
  351. static irqreturn_t vmci_interrupt_bm(int irq, void *_dev)
  352. {
  353. struct vmci_guest_device *dev = _dev;
  354. /* For MSI-X we can just assume it was meant for us. */
  355. tasklet_schedule(&dev->bm_tasklet);
  356. return IRQ_HANDLED;
  357. }
  358. /*
  359. * Most of the initialization at module load time is done here.
  360. */
  361. static int vmci_guest_probe_device(struct pci_dev *pdev,
  362. const struct pci_device_id *id)
  363. {
  364. struct vmci_guest_device *vmci_dev;
  365. void __iomem *iobase;
  366. unsigned int capabilities;
  367. unsigned long cmd;
  368. int vmci_err;
  369. int error;
  370. dev_dbg(&pdev->dev, "Probing for vmci/PCI guest device\n");
  371. error = pcim_enable_device(pdev);
  372. if (error) {
  373. dev_err(&pdev->dev,
  374. "Failed to enable VMCI device: %d\n", error);
  375. return error;
  376. }
  377. error = pcim_iomap_regions(pdev, 1 << 0, KBUILD_MODNAME);
  378. if (error) {
  379. dev_err(&pdev->dev, "Failed to reserve/map IO regions\n");
  380. return error;
  381. }
  382. iobase = pcim_iomap_table(pdev)[0];
  383. dev_info(&pdev->dev, "Found VMCI PCI device at %#lx, irq %u\n",
  384. (unsigned long)iobase, pdev->irq);
  385. vmci_dev = devm_kzalloc(&pdev->dev, sizeof(*vmci_dev), GFP_KERNEL);
  386. if (!vmci_dev) {
  387. dev_err(&pdev->dev,
  388. "Can't allocate memory for VMCI device\n");
  389. return -ENOMEM;
  390. }
  391. vmci_dev->dev = &pdev->dev;
  392. vmci_dev->exclusive_vectors = false;
  393. vmci_dev->iobase = iobase;
  394. tasklet_init(&vmci_dev->datagram_tasklet,
  395. vmci_dispatch_dgs, (unsigned long)vmci_dev);
  396. tasklet_init(&vmci_dev->bm_tasklet,
  397. vmci_process_bitmap, (unsigned long)vmci_dev);
  398. vmci_dev->data_buffer = vmalloc(VMCI_MAX_DG_SIZE);
  399. if (!vmci_dev->data_buffer) {
  400. dev_err(&pdev->dev,
  401. "Can't allocate memory for datagram buffer\n");
  402. return -ENOMEM;
  403. }
  404. pci_set_master(pdev); /* To enable queue_pair functionality. */
  405. /*
  406. * Verify that the VMCI Device supports the capabilities that
  407. * we need. If the device is missing capabilities that we would
  408. * like to use, check for fallback capabilities and use those
  409. * instead (so we can run a new VM on old hosts). Fail the load if
  410. * a required capability is missing and there is no fallback.
  411. *
  412. * Right now, we need datagrams. There are no fallbacks.
  413. */
  414. capabilities = ioread32(vmci_dev->iobase + VMCI_CAPS_ADDR);
  415. if (!(capabilities & VMCI_CAPS_DATAGRAM)) {
  416. dev_err(&pdev->dev, "Device does not support datagrams\n");
  417. error = -ENXIO;
  418. goto err_free_data_buffer;
  419. }
  420. /*
  421. * If the hardware supports notifications, we will use that as
  422. * well.
  423. */
  424. if (capabilities & VMCI_CAPS_NOTIFICATIONS) {
  425. vmci_dev->notification_bitmap = dma_alloc_coherent(
  426. &pdev->dev, PAGE_SIZE, &vmci_dev->notification_base,
  427. GFP_KERNEL);
  428. if (!vmci_dev->notification_bitmap) {
  429. dev_warn(&pdev->dev,
  430. "Unable to allocate notification bitmap\n");
  431. } else {
  432. memset(vmci_dev->notification_bitmap, 0, PAGE_SIZE);
  433. capabilities |= VMCI_CAPS_NOTIFICATIONS;
  434. }
  435. }
  436. dev_info(&pdev->dev, "Using capabilities 0x%x\n", capabilities);
  437. /* Let the host know which capabilities we intend to use. */
  438. iowrite32(capabilities, vmci_dev->iobase + VMCI_CAPS_ADDR);
  439. /* Set up global device so that we can start sending datagrams */
  440. spin_lock_irq(&vmci_dev_spinlock);
  441. vmci_dev_g = vmci_dev;
  442. vmci_pdev = pdev;
  443. spin_unlock_irq(&vmci_dev_spinlock);
  444. /*
  445. * Register notification bitmap with device if that capability is
  446. * used.
  447. */
  448. if (capabilities & VMCI_CAPS_NOTIFICATIONS) {
  449. unsigned long bitmap_ppn =
  450. vmci_dev->notification_base >> PAGE_SHIFT;
  451. if (!vmci_dbell_register_notification_bitmap(bitmap_ppn)) {
  452. dev_warn(&pdev->dev,
  453. "VMCI device unable to register notification bitmap with PPN 0x%x\n",
  454. (u32) bitmap_ppn);
  455. error = -ENXIO;
  456. goto err_remove_vmci_dev_g;
  457. }
  458. }
  459. /* Check host capabilities. */
  460. error = vmci_check_host_caps(pdev);
  461. if (error)
  462. goto err_remove_bitmap;
  463. /* Enable device. */
  464. /*
  465. * We subscribe to the VMCI_EVENT_CTX_ID_UPDATE here so we can
  466. * update the internal context id when needed.
  467. */
  468. vmci_err = vmci_event_subscribe(VMCI_EVENT_CTX_ID_UPDATE,
  469. vmci_guest_cid_update, NULL,
  470. &ctx_update_sub_id);
  471. if (vmci_err < VMCI_SUCCESS)
  472. dev_warn(&pdev->dev,
  473. "Failed to subscribe to event (type=%d): %d\n",
  474. VMCI_EVENT_CTX_ID_UPDATE, vmci_err);
  475. /*
  476. * Enable interrupts. Try MSI-X first, then MSI, and then fallback on
  477. * legacy interrupts.
  478. */
  479. error = pci_alloc_irq_vectors(pdev, VMCI_MAX_INTRS, VMCI_MAX_INTRS,
  480. PCI_IRQ_MSIX);
  481. if (error < 0) {
  482. error = pci_alloc_irq_vectors(pdev, 1, 1,
  483. PCI_IRQ_MSIX | PCI_IRQ_MSI | PCI_IRQ_LEGACY);
  484. if (error < 0)
  485. goto err_remove_bitmap;
  486. } else {
  487. vmci_dev->exclusive_vectors = true;
  488. }
  489. /*
  490. * Request IRQ for legacy or MSI interrupts, or for first
  491. * MSI-X vector.
  492. */
  493. error = request_irq(pci_irq_vector(pdev, 0), vmci_interrupt,
  494. IRQF_SHARED, KBUILD_MODNAME, vmci_dev);
  495. if (error) {
  496. dev_err(&pdev->dev, "Irq %u in use: %d\n",
  497. pci_irq_vector(pdev, 0), error);
  498. goto err_disable_msi;
  499. }
  500. /*
  501. * For MSI-X with exclusive vectors we need to request an
  502. * interrupt for each vector so that we get a separate
  503. * interrupt handler routine. This allows us to distinguish
  504. * between the vectors.
  505. */
  506. if (vmci_dev->exclusive_vectors) {
  507. error = request_irq(pci_irq_vector(pdev, 1),
  508. vmci_interrupt_bm, 0, KBUILD_MODNAME,
  509. vmci_dev);
  510. if (error) {
  511. dev_err(&pdev->dev,
  512. "Failed to allocate irq %u: %d\n",
  513. pci_irq_vector(pdev, 1), error);
  514. goto err_free_irq;
  515. }
  516. }
  517. dev_dbg(&pdev->dev, "Registered device\n");
  518. atomic_inc(&vmci_num_guest_devices);
  519. /* Enable specific interrupt bits. */
  520. cmd = VMCI_IMR_DATAGRAM;
  521. if (capabilities & VMCI_CAPS_NOTIFICATIONS)
  522. cmd |= VMCI_IMR_NOTIFICATION;
  523. iowrite32(cmd, vmci_dev->iobase + VMCI_IMR_ADDR);
  524. /* Enable interrupts. */
  525. iowrite32(VMCI_CONTROL_INT_ENABLE,
  526. vmci_dev->iobase + VMCI_CONTROL_ADDR);
  527. pci_set_drvdata(pdev, vmci_dev);
  528. return 0;
  529. err_free_irq:
  530. free_irq(pci_irq_vector(pdev, 0), vmci_dev);
  531. tasklet_kill(&vmci_dev->datagram_tasklet);
  532. tasklet_kill(&vmci_dev->bm_tasklet);
  533. err_disable_msi:
  534. pci_free_irq_vectors(pdev);
  535. vmci_err = vmci_event_unsubscribe(ctx_update_sub_id);
  536. if (vmci_err < VMCI_SUCCESS)
  537. dev_warn(&pdev->dev,
  538. "Failed to unsubscribe from event (type=%d) with subscriber (ID=0x%x): %d\n",
  539. VMCI_EVENT_CTX_ID_UPDATE, ctx_update_sub_id, vmci_err);
  540. err_remove_bitmap:
  541. if (vmci_dev->notification_bitmap) {
  542. iowrite32(VMCI_CONTROL_RESET,
  543. vmci_dev->iobase + VMCI_CONTROL_ADDR);
  544. dma_free_coherent(&pdev->dev, PAGE_SIZE,
  545. vmci_dev->notification_bitmap,
  546. vmci_dev->notification_base);
  547. }
  548. err_remove_vmci_dev_g:
  549. spin_lock_irq(&vmci_dev_spinlock);
  550. vmci_pdev = NULL;
  551. vmci_dev_g = NULL;
  552. spin_unlock_irq(&vmci_dev_spinlock);
  553. err_free_data_buffer:
  554. vfree(vmci_dev->data_buffer);
  555. /* The rest are managed resources and will be freed by PCI core */
  556. return error;
  557. }
  558. static void vmci_guest_remove_device(struct pci_dev *pdev)
  559. {
  560. struct vmci_guest_device *vmci_dev = pci_get_drvdata(pdev);
  561. int vmci_err;
  562. dev_dbg(&pdev->dev, "Removing device\n");
  563. atomic_dec(&vmci_num_guest_devices);
  564. vmci_qp_guest_endpoints_exit();
  565. vmci_err = vmci_event_unsubscribe(ctx_update_sub_id);
  566. if (vmci_err < VMCI_SUCCESS)
  567. dev_warn(&pdev->dev,
  568. "Failed to unsubscribe from event (type=%d) with subscriber (ID=0x%x): %d\n",
  569. VMCI_EVENT_CTX_ID_UPDATE, ctx_update_sub_id, vmci_err);
  570. spin_lock_irq(&vmci_dev_spinlock);
  571. vmci_dev_g = NULL;
  572. vmci_pdev = NULL;
  573. spin_unlock_irq(&vmci_dev_spinlock);
  574. dev_dbg(&pdev->dev, "Resetting vmci device\n");
  575. iowrite32(VMCI_CONTROL_RESET, vmci_dev->iobase + VMCI_CONTROL_ADDR);
  576. /*
  577. * Free IRQ and then disable MSI/MSI-X as appropriate. For
  578. * MSI-X, we might have multiple vectors, each with their own
  579. * IRQ, which we must free too.
  580. */
  581. if (vmci_dev->exclusive_vectors)
  582. free_irq(pci_irq_vector(pdev, 1), vmci_dev);
  583. free_irq(pci_irq_vector(pdev, 0), vmci_dev);
  584. pci_free_irq_vectors(pdev);
  585. tasklet_kill(&vmci_dev->datagram_tasklet);
  586. tasklet_kill(&vmci_dev->bm_tasklet);
  587. if (vmci_dev->notification_bitmap) {
  588. /*
  589. * The device reset above cleared the bitmap state of the
  590. * device, so we can safely free it here.
  591. */
  592. dma_free_coherent(&pdev->dev, PAGE_SIZE,
  593. vmci_dev->notification_bitmap,
  594. vmci_dev->notification_base);
  595. }
  596. vfree(vmci_dev->data_buffer);
  597. /* The rest are managed resources and will be freed by PCI core */
  598. }
  599. static const struct pci_device_id vmci_ids[] = {
  600. { PCI_DEVICE(PCI_VENDOR_ID_VMWARE, PCI_DEVICE_ID_VMWARE_VMCI), },
  601. { 0 },
  602. };
  603. MODULE_DEVICE_TABLE(pci, vmci_ids);
  604. static struct pci_driver vmci_guest_driver = {
  605. .name = KBUILD_MODNAME,
  606. .id_table = vmci_ids,
  607. .probe = vmci_guest_probe_device,
  608. .remove = vmci_guest_remove_device,
  609. };
  610. int __init vmci_guest_init(void)
  611. {
  612. return pci_register_driver(&vmci_guest_driver);
  613. }
  614. void __exit vmci_guest_exit(void)
  615. {
  616. pci_unregister_driver(&vmci_guest_driver);
  617. }