vmci_guest.c 21 KB

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