vmci_guest.c 21 KB

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