vmci_guest.c 21 KB

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