vmci_guest.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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 int 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 -ENOMEM;
  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 ? 0 : -ENXIO;
  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_exact(pdev,
  328. vmci_dev->msix_entries, VMCI_MAX_INTRS);
  329. if (result == 0)
  330. vmci_dev->exclusive_vectors = true;
  331. else if (result == -ENOSPC)
  332. result = pci_enable_msix_exact(pdev, vmci_dev->msix_entries, 1);
  333. return result;
  334. }
  335. /*
  336. * Interrupt handler for legacy or MSI interrupt, or for first MSI-X
  337. * interrupt (vector VMCI_INTR_DATAGRAM).
  338. */
  339. static irqreturn_t vmci_interrupt(int irq, void *_dev)
  340. {
  341. struct vmci_guest_device *dev = _dev;
  342. /*
  343. * If we are using MSI-X with exclusive vectors then we simply schedule
  344. * the datagram tasklet, since we know the interrupt was meant for us.
  345. * Otherwise we must read the ICR to determine what to do.
  346. */
  347. if (dev->intr_type == VMCI_INTR_TYPE_MSIX && dev->exclusive_vectors) {
  348. tasklet_schedule(&dev->datagram_tasklet);
  349. } else {
  350. unsigned int icr;
  351. /* Acknowledge interrupt and determine what needs doing. */
  352. icr = ioread32(dev->iobase + VMCI_ICR_ADDR);
  353. if (icr == 0 || icr == ~0)
  354. return IRQ_NONE;
  355. if (icr & VMCI_ICR_DATAGRAM) {
  356. tasklet_schedule(&dev->datagram_tasklet);
  357. icr &= ~VMCI_ICR_DATAGRAM;
  358. }
  359. if (icr & VMCI_ICR_NOTIFICATION) {
  360. tasklet_schedule(&dev->bm_tasklet);
  361. icr &= ~VMCI_ICR_NOTIFICATION;
  362. }
  363. if (icr != 0)
  364. dev_warn(dev->dev,
  365. "Ignoring unknown interrupt cause (%d)\n",
  366. icr);
  367. }
  368. return IRQ_HANDLED;
  369. }
  370. /*
  371. * Interrupt handler for MSI-X interrupt vector VMCI_INTR_NOTIFICATION,
  372. * which is for the notification bitmap. Will only get called if we are
  373. * using MSI-X with exclusive vectors.
  374. */
  375. static irqreturn_t vmci_interrupt_bm(int irq, void *_dev)
  376. {
  377. struct vmci_guest_device *dev = _dev;
  378. /* For MSI-X we can just assume it was meant for us. */
  379. tasklet_schedule(&dev->bm_tasklet);
  380. return IRQ_HANDLED;
  381. }
  382. /*
  383. * Most of the initialization at module load time is done here.
  384. */
  385. static int vmci_guest_probe_device(struct pci_dev *pdev,
  386. const struct pci_device_id *id)
  387. {
  388. struct vmci_guest_device *vmci_dev;
  389. void __iomem *iobase;
  390. unsigned int capabilities;
  391. unsigned long cmd;
  392. int vmci_err;
  393. int error;
  394. dev_dbg(&pdev->dev, "Probing for vmci/PCI guest device\n");
  395. error = pcim_enable_device(pdev);
  396. if (error) {
  397. dev_err(&pdev->dev,
  398. "Failed to enable VMCI device: %d\n", error);
  399. return error;
  400. }
  401. error = pcim_iomap_regions(pdev, 1 << 0, KBUILD_MODNAME);
  402. if (error) {
  403. dev_err(&pdev->dev, "Failed to reserve/map IO regions\n");
  404. return error;
  405. }
  406. iobase = pcim_iomap_table(pdev)[0];
  407. dev_info(&pdev->dev, "Found VMCI PCI device at %#lx, irq %u\n",
  408. (unsigned long)iobase, pdev->irq);
  409. vmci_dev = devm_kzalloc(&pdev->dev, sizeof(*vmci_dev), GFP_KERNEL);
  410. if (!vmci_dev) {
  411. dev_err(&pdev->dev,
  412. "Can't allocate memory for VMCI device\n");
  413. return -ENOMEM;
  414. }
  415. vmci_dev->dev = &pdev->dev;
  416. vmci_dev->intr_type = VMCI_INTR_TYPE_INTX;
  417. vmci_dev->exclusive_vectors = false;
  418. vmci_dev->iobase = iobase;
  419. tasklet_init(&vmci_dev->datagram_tasklet,
  420. vmci_dispatch_dgs, (unsigned long)vmci_dev);
  421. tasklet_init(&vmci_dev->bm_tasklet,
  422. vmci_process_bitmap, (unsigned long)vmci_dev);
  423. vmci_dev->data_buffer = vmalloc(VMCI_MAX_DG_SIZE);
  424. if (!vmci_dev->data_buffer) {
  425. dev_err(&pdev->dev,
  426. "Can't allocate memory for datagram buffer\n");
  427. return -ENOMEM;
  428. }
  429. pci_set_master(pdev); /* To enable queue_pair functionality. */
  430. /*
  431. * Verify that the VMCI Device supports the capabilities that
  432. * we need. If the device is missing capabilities that we would
  433. * like to use, check for fallback capabilities and use those
  434. * instead (so we can run a new VM on old hosts). Fail the load if
  435. * a required capability is missing and there is no fallback.
  436. *
  437. * Right now, we need datagrams. There are no fallbacks.
  438. */
  439. capabilities = ioread32(vmci_dev->iobase + VMCI_CAPS_ADDR);
  440. if (!(capabilities & VMCI_CAPS_DATAGRAM)) {
  441. dev_err(&pdev->dev, "Device does not support datagrams\n");
  442. error = -ENXIO;
  443. goto err_free_data_buffer;
  444. }
  445. /*
  446. * If the hardware supports notifications, we will use that as
  447. * well.
  448. */
  449. if (capabilities & VMCI_CAPS_NOTIFICATIONS) {
  450. vmci_dev->notification_bitmap = dma_alloc_coherent(
  451. &pdev->dev, PAGE_SIZE, &vmci_dev->notification_base,
  452. GFP_KERNEL);
  453. if (!vmci_dev->notification_bitmap) {
  454. dev_warn(&pdev->dev,
  455. "Unable to allocate notification bitmap\n");
  456. } else {
  457. memset(vmci_dev->notification_bitmap, 0, PAGE_SIZE);
  458. capabilities |= VMCI_CAPS_NOTIFICATIONS;
  459. }
  460. }
  461. dev_info(&pdev->dev, "Using capabilities 0x%x\n", capabilities);
  462. /* Let the host know which capabilities we intend to use. */
  463. iowrite32(capabilities, vmci_dev->iobase + VMCI_CAPS_ADDR);
  464. /* Set up global device so that we can start sending datagrams */
  465. spin_lock_irq(&vmci_dev_spinlock);
  466. vmci_dev_g = vmci_dev;
  467. vmci_pdev = pdev;
  468. spin_unlock_irq(&vmci_dev_spinlock);
  469. /*
  470. * Register notification bitmap with device if that capability is
  471. * used.
  472. */
  473. if (capabilities & VMCI_CAPS_NOTIFICATIONS) {
  474. unsigned long bitmap_ppn =
  475. vmci_dev->notification_base >> PAGE_SHIFT;
  476. if (!vmci_dbell_register_notification_bitmap(bitmap_ppn)) {
  477. dev_warn(&pdev->dev,
  478. "VMCI device unable to register notification bitmap with PPN 0x%x\n",
  479. (u32) bitmap_ppn);
  480. error = -ENXIO;
  481. goto err_remove_vmci_dev_g;
  482. }
  483. }
  484. /* Check host capabilities. */
  485. error = vmci_check_host_caps(pdev);
  486. if (error)
  487. goto err_remove_bitmap;
  488. /* Enable device. */
  489. /*
  490. * We subscribe to the VMCI_EVENT_CTX_ID_UPDATE here so we can
  491. * update the internal context id when needed.
  492. */
  493. vmci_err = vmci_event_subscribe(VMCI_EVENT_CTX_ID_UPDATE,
  494. vmci_guest_cid_update, NULL,
  495. &ctx_update_sub_id);
  496. if (vmci_err < VMCI_SUCCESS)
  497. dev_warn(&pdev->dev,
  498. "Failed to subscribe to event (type=%d): %d\n",
  499. VMCI_EVENT_CTX_ID_UPDATE, vmci_err);
  500. /*
  501. * Enable interrupts. Try MSI-X first, then MSI, and then fallback on
  502. * legacy interrupts.
  503. */
  504. if (!vmci_disable_msix && !vmci_enable_msix(pdev, vmci_dev)) {
  505. vmci_dev->intr_type = VMCI_INTR_TYPE_MSIX;
  506. vmci_dev->irq = vmci_dev->msix_entries[0].vector;
  507. } else if (!vmci_disable_msi && !pci_enable_msi(pdev)) {
  508. vmci_dev->intr_type = VMCI_INTR_TYPE_MSI;
  509. vmci_dev->irq = pdev->irq;
  510. } else {
  511. vmci_dev->intr_type = VMCI_INTR_TYPE_INTX;
  512. vmci_dev->irq = pdev->irq;
  513. }
  514. /*
  515. * Request IRQ for legacy or MSI interrupts, or for first
  516. * MSI-X vector.
  517. */
  518. error = request_irq(vmci_dev->irq, vmci_interrupt, IRQF_SHARED,
  519. KBUILD_MODNAME, vmci_dev);
  520. if (error) {
  521. dev_err(&pdev->dev, "Irq %u in use: %d\n",
  522. vmci_dev->irq, error);
  523. goto err_disable_msi;
  524. }
  525. /*
  526. * For MSI-X with exclusive vectors we need to request an
  527. * interrupt for each vector so that we get a separate
  528. * interrupt handler routine. This allows us to distinguish
  529. * between the vectors.
  530. */
  531. if (vmci_dev->exclusive_vectors) {
  532. error = request_irq(vmci_dev->msix_entries[1].vector,
  533. vmci_interrupt_bm, 0, KBUILD_MODNAME,
  534. vmci_dev);
  535. if (error) {
  536. dev_err(&pdev->dev,
  537. "Failed to allocate irq %u: %d\n",
  538. vmci_dev->msix_entries[1].vector, error);
  539. goto err_free_irq;
  540. }
  541. }
  542. dev_dbg(&pdev->dev, "Registered device\n");
  543. atomic_inc(&vmci_num_guest_devices);
  544. /* Enable specific interrupt bits. */
  545. cmd = VMCI_IMR_DATAGRAM;
  546. if (capabilities & VMCI_CAPS_NOTIFICATIONS)
  547. cmd |= VMCI_IMR_NOTIFICATION;
  548. iowrite32(cmd, vmci_dev->iobase + VMCI_IMR_ADDR);
  549. /* Enable interrupts. */
  550. iowrite32(VMCI_CONTROL_INT_ENABLE,
  551. vmci_dev->iobase + VMCI_CONTROL_ADDR);
  552. pci_set_drvdata(pdev, vmci_dev);
  553. return 0;
  554. err_free_irq:
  555. free_irq(vmci_dev->irq, vmci_dev);
  556. tasklet_kill(&vmci_dev->datagram_tasklet);
  557. tasklet_kill(&vmci_dev->bm_tasklet);
  558. err_disable_msi:
  559. if (vmci_dev->intr_type == VMCI_INTR_TYPE_MSIX)
  560. pci_disable_msix(pdev);
  561. else if (vmci_dev->intr_type == VMCI_INTR_TYPE_MSI)
  562. pci_disable_msi(pdev);
  563. vmci_err = vmci_event_unsubscribe(ctx_update_sub_id);
  564. if (vmci_err < VMCI_SUCCESS)
  565. dev_warn(&pdev->dev,
  566. "Failed to unsubscribe from event (type=%d) with subscriber (ID=0x%x): %d\n",
  567. VMCI_EVENT_CTX_ID_UPDATE, ctx_update_sub_id, vmci_err);
  568. err_remove_bitmap:
  569. if (vmci_dev->notification_bitmap) {
  570. iowrite32(VMCI_CONTROL_RESET,
  571. vmci_dev->iobase + VMCI_CONTROL_ADDR);
  572. dma_free_coherent(&pdev->dev, PAGE_SIZE,
  573. vmci_dev->notification_bitmap,
  574. vmci_dev->notification_base);
  575. }
  576. err_remove_vmci_dev_g:
  577. spin_lock_irq(&vmci_dev_spinlock);
  578. vmci_pdev = NULL;
  579. vmci_dev_g = NULL;
  580. spin_unlock_irq(&vmci_dev_spinlock);
  581. err_free_data_buffer:
  582. vfree(vmci_dev->data_buffer);
  583. /* The rest are managed resources and will be freed by PCI core */
  584. return error;
  585. }
  586. static void vmci_guest_remove_device(struct pci_dev *pdev)
  587. {
  588. struct vmci_guest_device *vmci_dev = pci_get_drvdata(pdev);
  589. int vmci_err;
  590. dev_dbg(&pdev->dev, "Removing device\n");
  591. atomic_dec(&vmci_num_guest_devices);
  592. vmci_qp_guest_endpoints_exit();
  593. vmci_err = vmci_event_unsubscribe(ctx_update_sub_id);
  594. if (vmci_err < VMCI_SUCCESS)
  595. dev_warn(&pdev->dev,
  596. "Failed to unsubscribe from event (type=%d) with subscriber (ID=0x%x): %d\n",
  597. VMCI_EVENT_CTX_ID_UPDATE, ctx_update_sub_id, vmci_err);
  598. spin_lock_irq(&vmci_dev_spinlock);
  599. vmci_dev_g = NULL;
  600. vmci_pdev = NULL;
  601. spin_unlock_irq(&vmci_dev_spinlock);
  602. dev_dbg(&pdev->dev, "Resetting vmci device\n");
  603. iowrite32(VMCI_CONTROL_RESET, vmci_dev->iobase + VMCI_CONTROL_ADDR);
  604. /*
  605. * Free IRQ and then disable MSI/MSI-X as appropriate. For
  606. * MSI-X, we might have multiple vectors, each with their own
  607. * IRQ, which we must free too.
  608. */
  609. free_irq(vmci_dev->irq, vmci_dev);
  610. if (vmci_dev->intr_type == VMCI_INTR_TYPE_MSIX) {
  611. if (vmci_dev->exclusive_vectors)
  612. free_irq(vmci_dev->msix_entries[1].vector, vmci_dev);
  613. pci_disable_msix(pdev);
  614. } else if (vmci_dev->intr_type == VMCI_INTR_TYPE_MSI) {
  615. pci_disable_msi(pdev);
  616. }
  617. tasklet_kill(&vmci_dev->datagram_tasklet);
  618. tasklet_kill(&vmci_dev->bm_tasklet);
  619. if (vmci_dev->notification_bitmap) {
  620. /*
  621. * The device reset above cleared the bitmap state of the
  622. * device, so we can safely free it here.
  623. */
  624. dma_free_coherent(&pdev->dev, PAGE_SIZE,
  625. vmci_dev->notification_bitmap,
  626. vmci_dev->notification_base);
  627. }
  628. vfree(vmci_dev->data_buffer);
  629. /* The rest are managed resources and will be freed by PCI core */
  630. }
  631. static DEFINE_PCI_DEVICE_TABLE(vmci_ids) = {
  632. { PCI_DEVICE(PCI_VENDOR_ID_VMWARE, PCI_DEVICE_ID_VMWARE_VMCI), },
  633. { 0 },
  634. };
  635. MODULE_DEVICE_TABLE(pci, vmci_ids);
  636. static struct pci_driver vmci_guest_driver = {
  637. .name = KBUILD_MODNAME,
  638. .id_table = vmci_ids,
  639. .probe = vmci_guest_probe_device,
  640. .remove = vmci_guest_remove_device,
  641. };
  642. int __init vmci_guest_init(void)
  643. {
  644. return pci_register_driver(&vmci_guest_driver);
  645. }
  646. void __exit vmci_guest_exit(void)
  647. {
  648. pci_unregister_driver(&vmci_guest_driver);
  649. }