vmci_guest.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /*
  2. * VMware VMCI Driver
  3. *
  4. * Copyright (C) 2012 VMware, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation version 2 and no later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include <linux/vmw_vmci_defs.h>
  16. #include <linux/vmw_vmci_api.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/highmem.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mm.h>
  22. #include <linux/module.h>
  23. #include <linux/sched.h>
  24. #include <linux/slab.h>
  25. #include <linux/init.h>
  26. #include <linux/pci.h>
  27. #include <linux/smp.h>
  28. #include <linux/io.h>
  29. #include <linux/vmalloc.h>
  30. #include "vmci_datagram.h"
  31. #include "vmci_doorbell.h"
  32. #include "vmci_context.h"
  33. #include "vmci_driver.h"
  34. #include "vmci_event.h"
  35. #define PCI_DEVICE_ID_VMWARE_VMCI 0x0740
  36. #define VMCI_UTIL_NUM_RESOURCES 1
  37. static bool vmci_disable_msi;
  38. module_param_named(disable_msi, vmci_disable_msi, bool, 0);
  39. MODULE_PARM_DESC(disable_msi, "Disable MSI use in driver - (default=0)");
  40. static bool vmci_disable_msix;
  41. module_param_named(disable_msix, vmci_disable_msix, bool, 0);
  42. MODULE_PARM_DESC(disable_msix, "Disable MSI-X use in driver - (default=0)");
  43. static u32 ctx_update_sub_id = VMCI_INVALID_ID;
  44. static u32 vm_context_id = VMCI_INVALID_ID;
  45. struct vmci_guest_device {
  46. struct device *dev; /* PCI device we are attached to */
  47. void __iomem *iobase;
  48. unsigned int irq;
  49. unsigned int intr_type;
  50. bool exclusive_vectors;
  51. struct msix_entry msix_entries[VMCI_MAX_INTRS];
  52. struct tasklet_struct datagram_tasklet;
  53. struct tasklet_struct bm_tasklet;
  54. void *data_buffer;
  55. void *notification_bitmap;
  56. dma_addr_t notification_base;
  57. };
  58. /* vmci_dev singleton device and supporting data*/
  59. struct pci_dev *vmci_pdev;
  60. static struct vmci_guest_device *vmci_dev_g;
  61. static DEFINE_SPINLOCK(vmci_dev_spinlock);
  62. static atomic_t vmci_num_guest_devices = ATOMIC_INIT(0);
  63. bool vmci_guest_code_active(void)
  64. {
  65. return atomic_read(&vmci_num_guest_devices) != 0;
  66. }
  67. u32 vmci_get_vm_context_id(void)
  68. {
  69. if (vm_context_id == VMCI_INVALID_ID) {
  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. vm_context_id = vmci_send_datagram(&get_cid_msg);
  77. }
  78. return vm_context_id;
  79. }
  80. /*
  81. * VM to hypervisor call mechanism. We use the standard VMware naming
  82. * convention since shared code is calling this function as well.
  83. */
  84. int vmci_send_datagram(struct vmci_datagram *dg)
  85. {
  86. unsigned long flags;
  87. int result;
  88. /* Check args. */
  89. if (dg == NULL)
  90. return VMCI_ERROR_INVALID_ARGS;
  91. /*
  92. * Need to acquire spinlock on the device because the datagram
  93. * data may be spread over multiple pages and the monitor may
  94. * interleave device user rpc calls from multiple
  95. * VCPUs. Acquiring the spinlock precludes that
  96. * possibility. Disabling interrupts to avoid incoming
  97. * datagrams during a "rep out" and possibly landing up in
  98. * this function.
  99. */
  100. spin_lock_irqsave(&vmci_dev_spinlock, flags);
  101. if (vmci_dev_g) {
  102. iowrite8_rep(vmci_dev_g->iobase + VMCI_DATA_OUT_ADDR,
  103. dg, VMCI_DG_SIZE(dg));
  104. result = ioread32(vmci_dev_g->iobase + VMCI_RESULT_LOW_ADDR);
  105. } else {
  106. result = VMCI_ERROR_UNAVAILABLE;
  107. }
  108. spin_unlock_irqrestore(&vmci_dev_spinlock, flags);
  109. return result;
  110. }
  111. EXPORT_SYMBOL_GPL(vmci_send_datagram);
  112. /*
  113. * Gets called with the new context id if updated or resumed.
  114. * Context id.
  115. */
  116. static void vmci_guest_cid_update(u32 sub_id,
  117. const struct vmci_event_data *event_data,
  118. void *client_data)
  119. {
  120. const struct vmci_event_payld_ctx *ev_payload =
  121. vmci_event_data_const_payload(event_data);
  122. if (sub_id != ctx_update_sub_id) {
  123. pr_devel("Invalid subscriber (ID=0x%x)\n", sub_id);
  124. return;
  125. }
  126. if (!event_data || ev_payload->context_id == VMCI_INVALID_ID) {
  127. pr_devel("Invalid event data\n");
  128. return;
  129. }
  130. pr_devel("Updating context from (ID=0x%x) to (ID=0x%x) on event (type=%d)\n",
  131. vm_context_id, ev_payload->context_id, event_data->event);
  132. vm_context_id = ev_payload->context_id;
  133. }
  134. /*
  135. * Verify that the host supports the hypercalls we need. If it does not,
  136. * try to find fallback hypercalls and use those instead. Returns
  137. * true if required hypercalls (or fallback hypercalls) are
  138. * supported by the host, false otherwise.
  139. */
  140. static int vmci_check_host_caps(struct pci_dev *pdev)
  141. {
  142. bool result;
  143. struct vmci_resource_query_msg *msg;
  144. u32 msg_size = sizeof(struct vmci_resource_query_hdr) +
  145. VMCI_UTIL_NUM_RESOURCES * sizeof(u32);
  146. struct vmci_datagram *check_msg;
  147. check_msg = kmalloc(msg_size, GFP_KERNEL);
  148. if (!check_msg) {
  149. dev_err(&pdev->dev, "%s: Insufficient memory\n", __func__);
  150. return -ENOMEM;
  151. }
  152. check_msg->dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
  153. VMCI_RESOURCES_QUERY);
  154. check_msg->src = VMCI_ANON_SRC_HANDLE;
  155. check_msg->payload_size = msg_size - VMCI_DG_HEADERSIZE;
  156. msg = (struct vmci_resource_query_msg *)VMCI_DG_PAYLOAD(check_msg);
  157. msg->num_resources = VMCI_UTIL_NUM_RESOURCES;
  158. msg->resources[0] = VMCI_GET_CONTEXT_ID;
  159. /* Checks that hyper calls are supported */
  160. result = vmci_send_datagram(check_msg) == 0x01;
  161. kfree(check_msg);
  162. dev_dbg(&pdev->dev, "%s: Host capability check: %s\n",
  163. __func__, result ? "PASSED" : "FAILED");
  164. /* We need the vector. There are no fallbacks. */
  165. return result ? 0 : -ENXIO;
  166. }
  167. /*
  168. * Reads datagrams from the data in port and dispatches them. We
  169. * always start reading datagrams into only the first page of the
  170. * datagram buffer. If the datagrams don't fit into one page, we
  171. * use the maximum datagram buffer size for the remainder of the
  172. * invocation. This is a simple heuristic for not penalizing
  173. * small datagrams.
  174. *
  175. * This function assumes that it has exclusive access to the data
  176. * in port for the duration of the call.
  177. */
  178. static void vmci_dispatch_dgs(unsigned long data)
  179. {
  180. struct vmci_guest_device *vmci_dev = (struct vmci_guest_device *)data;
  181. u8 *dg_in_buffer = vmci_dev->data_buffer;
  182. struct vmci_datagram *dg;
  183. size_t dg_in_buffer_size = VMCI_MAX_DG_SIZE;
  184. size_t current_dg_in_buffer_size = PAGE_SIZE;
  185. size_t remaining_bytes;
  186. BUILD_BUG_ON(VMCI_MAX_DG_SIZE < PAGE_SIZE);
  187. ioread8_rep(vmci_dev->iobase + VMCI_DATA_IN_ADDR,
  188. vmci_dev->data_buffer, current_dg_in_buffer_size);
  189. dg = (struct vmci_datagram *)dg_in_buffer;
  190. remaining_bytes = current_dg_in_buffer_size;
  191. while (dg->dst.resource != VMCI_INVALID_ID ||
  192. remaining_bytes > PAGE_SIZE) {
  193. unsigned dg_in_size;
  194. /*
  195. * When the input buffer spans multiple pages, a datagram can
  196. * start on any page boundary in the buffer.
  197. */
  198. if (dg->dst.resource == VMCI_INVALID_ID) {
  199. dg = (struct vmci_datagram *)roundup(
  200. (uintptr_t)dg + 1, PAGE_SIZE);
  201. remaining_bytes =
  202. (size_t)(dg_in_buffer +
  203. current_dg_in_buffer_size -
  204. (u8 *)dg);
  205. continue;
  206. }
  207. dg_in_size = VMCI_DG_SIZE_ALIGNED(dg);
  208. if (dg_in_size <= dg_in_buffer_size) {
  209. int result;
  210. /*
  211. * If the remaining bytes in the datagram
  212. * buffer doesn't contain the complete
  213. * datagram, we first make sure we have enough
  214. * room for it and then we read the reminder
  215. * of the datagram and possibly any following
  216. * datagrams.
  217. */
  218. if (dg_in_size > remaining_bytes) {
  219. if (remaining_bytes !=
  220. current_dg_in_buffer_size) {
  221. /*
  222. * We move the partial
  223. * datagram to the front and
  224. * read the reminder of the
  225. * datagram and possibly
  226. * following calls into the
  227. * following bytes.
  228. */
  229. memmove(dg_in_buffer, dg_in_buffer +
  230. current_dg_in_buffer_size -
  231. remaining_bytes,
  232. remaining_bytes);
  233. dg = (struct vmci_datagram *)
  234. dg_in_buffer;
  235. }
  236. if (current_dg_in_buffer_size !=
  237. dg_in_buffer_size)
  238. current_dg_in_buffer_size =
  239. dg_in_buffer_size;
  240. ioread8_rep(vmci_dev->iobase +
  241. VMCI_DATA_IN_ADDR,
  242. vmci_dev->data_buffer +
  243. remaining_bytes,
  244. current_dg_in_buffer_size -
  245. remaining_bytes);
  246. }
  247. /*
  248. * We special case event datagrams from the
  249. * hypervisor.
  250. */
  251. if (dg->src.context == VMCI_HYPERVISOR_CONTEXT_ID &&
  252. dg->dst.resource == VMCI_EVENT_HANDLER) {
  253. result = vmci_event_dispatch(dg);
  254. } else {
  255. result = vmci_datagram_invoke_guest_handler(dg);
  256. }
  257. if (result < VMCI_SUCCESS)
  258. dev_dbg(vmci_dev->dev,
  259. "Datagram with resource (ID=0x%x) failed (err=%d)\n",
  260. dg->dst.resource, result);
  261. /* On to the next datagram. */
  262. dg = (struct vmci_datagram *)((u8 *)dg +
  263. dg_in_size);
  264. } else {
  265. size_t bytes_to_skip;
  266. /*
  267. * Datagram doesn't fit in datagram buffer of maximal
  268. * size. We drop it.
  269. */
  270. dev_dbg(vmci_dev->dev,
  271. "Failed to receive datagram (size=%u bytes)\n",
  272. dg_in_size);
  273. bytes_to_skip = dg_in_size - remaining_bytes;
  274. if (current_dg_in_buffer_size != dg_in_buffer_size)
  275. current_dg_in_buffer_size = dg_in_buffer_size;
  276. for (;;) {
  277. ioread8_rep(vmci_dev->iobase +
  278. VMCI_DATA_IN_ADDR,
  279. vmci_dev->data_buffer,
  280. current_dg_in_buffer_size);
  281. if (bytes_to_skip <= current_dg_in_buffer_size)
  282. break;
  283. bytes_to_skip -= current_dg_in_buffer_size;
  284. }
  285. dg = (struct vmci_datagram *)(dg_in_buffer +
  286. bytes_to_skip);
  287. }
  288. remaining_bytes =
  289. (size_t) (dg_in_buffer + current_dg_in_buffer_size -
  290. (u8 *)dg);
  291. if (remaining_bytes < VMCI_DG_HEADERSIZE) {
  292. /* Get the next batch of datagrams. */
  293. ioread8_rep(vmci_dev->iobase + VMCI_DATA_IN_ADDR,
  294. vmci_dev->data_buffer,
  295. current_dg_in_buffer_size);
  296. dg = (struct vmci_datagram *)dg_in_buffer;
  297. remaining_bytes = current_dg_in_buffer_size;
  298. }
  299. }
  300. }
  301. /*
  302. * Scans the notification bitmap for raised flags, clears them
  303. * and handles the notifications.
  304. */
  305. static void vmci_process_bitmap(unsigned long data)
  306. {
  307. struct vmci_guest_device *dev = (struct vmci_guest_device *)data;
  308. if (!dev->notification_bitmap) {
  309. dev_dbg(dev->dev, "No bitmap present in %s\n", __func__);
  310. return;
  311. }
  312. vmci_dbell_scan_notification_entries(dev->notification_bitmap);
  313. }
  314. /*
  315. * Enable MSI-X. Try exclusive vectors first, then shared vectors.
  316. */
  317. static int vmci_enable_msix(struct pci_dev *pdev,
  318. struct vmci_guest_device *vmci_dev)
  319. {
  320. int i;
  321. int result;
  322. for (i = 0; i < VMCI_MAX_INTRS; ++i) {
  323. vmci_dev->msix_entries[i].entry = i;
  324. vmci_dev->msix_entries[i].vector = i;
  325. }
  326. result = pci_enable_msix_exact(pdev,
  327. vmci_dev->msix_entries, VMCI_MAX_INTRS);
  328. if (result == 0)
  329. vmci_dev->exclusive_vectors = true;
  330. else if (result == -ENOSPC)
  331. result = pci_enable_msix_exact(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. error = -ENXIO;
  480. goto err_remove_vmci_dev_g;
  481. }
  482. }
  483. /* Check host capabilities. */
  484. error = vmci_check_host_caps(pdev);
  485. if (error)
  486. goto err_remove_bitmap;
  487. /* Enable device. */
  488. /*
  489. * We subscribe to the VMCI_EVENT_CTX_ID_UPDATE here so we can
  490. * update the internal context id when needed.
  491. */
  492. vmci_err = vmci_event_subscribe(VMCI_EVENT_CTX_ID_UPDATE,
  493. vmci_guest_cid_update, NULL,
  494. &ctx_update_sub_id);
  495. if (vmci_err < VMCI_SUCCESS)
  496. dev_warn(&pdev->dev,
  497. "Failed to subscribe to event (type=%d): %d\n",
  498. VMCI_EVENT_CTX_ID_UPDATE, vmci_err);
  499. /*
  500. * Enable interrupts. Try MSI-X first, then MSI, and then fallback on
  501. * legacy interrupts.
  502. */
  503. if (!vmci_disable_msix && !vmci_enable_msix(pdev, vmci_dev)) {
  504. vmci_dev->intr_type = VMCI_INTR_TYPE_MSIX;
  505. vmci_dev->irq = vmci_dev->msix_entries[0].vector;
  506. } else if (!vmci_disable_msi && !pci_enable_msi(pdev)) {
  507. vmci_dev->intr_type = VMCI_INTR_TYPE_MSI;
  508. vmci_dev->irq = pdev->irq;
  509. } else {
  510. vmci_dev->intr_type = VMCI_INTR_TYPE_INTX;
  511. vmci_dev->irq = pdev->irq;
  512. }
  513. /*
  514. * Request IRQ for legacy or MSI interrupts, or for first
  515. * MSI-X vector.
  516. */
  517. error = request_irq(vmci_dev->irq, vmci_interrupt, IRQF_SHARED,
  518. KBUILD_MODNAME, vmci_dev);
  519. if (error) {
  520. dev_err(&pdev->dev, "Irq %u in use: %d\n",
  521. vmci_dev->irq, error);
  522. goto err_disable_msi;
  523. }
  524. /*
  525. * For MSI-X with exclusive vectors we need to request an
  526. * interrupt for each vector so that we get a separate
  527. * interrupt handler routine. This allows us to distinguish
  528. * between the vectors.
  529. */
  530. if (vmci_dev->exclusive_vectors) {
  531. error = request_irq(vmci_dev->msix_entries[1].vector,
  532. vmci_interrupt_bm, 0, KBUILD_MODNAME,
  533. vmci_dev);
  534. if (error) {
  535. dev_err(&pdev->dev,
  536. "Failed to allocate irq %u: %d\n",
  537. vmci_dev->msix_entries[1].vector, error);
  538. goto err_free_irq;
  539. }
  540. }
  541. dev_dbg(&pdev->dev, "Registered device\n");
  542. atomic_inc(&vmci_num_guest_devices);
  543. /* Enable specific interrupt bits. */
  544. cmd = VMCI_IMR_DATAGRAM;
  545. if (capabilities & VMCI_CAPS_NOTIFICATIONS)
  546. cmd |= VMCI_IMR_NOTIFICATION;
  547. iowrite32(cmd, vmci_dev->iobase + VMCI_IMR_ADDR);
  548. /* Enable interrupts. */
  549. iowrite32(VMCI_CONTROL_INT_ENABLE,
  550. vmci_dev->iobase + VMCI_CONTROL_ADDR);
  551. pci_set_drvdata(pdev, vmci_dev);
  552. return 0;
  553. err_free_irq:
  554. free_irq(vmci_dev->irq, vmci_dev);
  555. tasklet_kill(&vmci_dev->datagram_tasklet);
  556. tasklet_kill(&vmci_dev->bm_tasklet);
  557. err_disable_msi:
  558. if (vmci_dev->intr_type == VMCI_INTR_TYPE_MSIX)
  559. pci_disable_msix(pdev);
  560. else if (vmci_dev->intr_type == VMCI_INTR_TYPE_MSI)
  561. pci_disable_msi(pdev);
  562. vmci_err = vmci_event_unsubscribe(ctx_update_sub_id);
  563. if (vmci_err < VMCI_SUCCESS)
  564. dev_warn(&pdev->dev,
  565. "Failed to unsubscribe from event (type=%d) with subscriber (ID=0x%x): %d\n",
  566. VMCI_EVENT_CTX_ID_UPDATE, ctx_update_sub_id, vmci_err);
  567. err_remove_bitmap:
  568. if (vmci_dev->notification_bitmap) {
  569. iowrite32(VMCI_CONTROL_RESET,
  570. vmci_dev->iobase + VMCI_CONTROL_ADDR);
  571. dma_free_coherent(&pdev->dev, PAGE_SIZE,
  572. vmci_dev->notification_bitmap,
  573. vmci_dev->notification_base);
  574. }
  575. err_remove_vmci_dev_g:
  576. spin_lock_irq(&vmci_dev_spinlock);
  577. vmci_pdev = NULL;
  578. vmci_dev_g = NULL;
  579. spin_unlock_irq(&vmci_dev_spinlock);
  580. err_free_data_buffer:
  581. vfree(vmci_dev->data_buffer);
  582. /* The rest are managed resources and will be freed by PCI core */
  583. return error;
  584. }
  585. static void vmci_guest_remove_device(struct pci_dev *pdev)
  586. {
  587. struct vmci_guest_device *vmci_dev = pci_get_drvdata(pdev);
  588. int vmci_err;
  589. dev_dbg(&pdev->dev, "Removing device\n");
  590. atomic_dec(&vmci_num_guest_devices);
  591. vmci_qp_guest_endpoints_exit();
  592. vmci_err = vmci_event_unsubscribe(ctx_update_sub_id);
  593. if (vmci_err < VMCI_SUCCESS)
  594. dev_warn(&pdev->dev,
  595. "Failed to unsubscribe from event (type=%d) with subscriber (ID=0x%x): %d\n",
  596. VMCI_EVENT_CTX_ID_UPDATE, ctx_update_sub_id, vmci_err);
  597. spin_lock_irq(&vmci_dev_spinlock);
  598. vmci_dev_g = NULL;
  599. vmci_pdev = NULL;
  600. spin_unlock_irq(&vmci_dev_spinlock);
  601. dev_dbg(&pdev->dev, "Resetting vmci device\n");
  602. iowrite32(VMCI_CONTROL_RESET, vmci_dev->iobase + VMCI_CONTROL_ADDR);
  603. /*
  604. * Free IRQ and then disable MSI/MSI-X as appropriate. For
  605. * MSI-X, we might have multiple vectors, each with their own
  606. * IRQ, which we must free too.
  607. */
  608. free_irq(vmci_dev->irq, vmci_dev);
  609. if (vmci_dev->intr_type == VMCI_INTR_TYPE_MSIX) {
  610. if (vmci_dev->exclusive_vectors)
  611. free_irq(vmci_dev->msix_entries[1].vector, vmci_dev);
  612. pci_disable_msix(pdev);
  613. } else if (vmci_dev->intr_type == VMCI_INTR_TYPE_MSI) {
  614. pci_disable_msi(pdev);
  615. }
  616. tasklet_kill(&vmci_dev->datagram_tasklet);
  617. tasklet_kill(&vmci_dev->bm_tasklet);
  618. if (vmci_dev->notification_bitmap) {
  619. /*
  620. * The device reset above cleared the bitmap state of the
  621. * device, so we can safely free it here.
  622. */
  623. dma_free_coherent(&pdev->dev, PAGE_SIZE,
  624. vmci_dev->notification_bitmap,
  625. vmci_dev->notification_base);
  626. }
  627. vfree(vmci_dev->data_buffer);
  628. /* The rest are managed resources and will be freed by PCI core */
  629. }
  630. static const struct pci_device_id vmci_ids[] = {
  631. { PCI_DEVICE(PCI_VENDOR_ID_VMWARE, PCI_DEVICE_ID_VMWARE_VMCI), },
  632. { 0 },
  633. };
  634. MODULE_DEVICE_TABLE(pci, vmci_ids);
  635. static struct pci_driver vmci_guest_driver = {
  636. .name = KBUILD_MODNAME,
  637. .id_table = vmci_ids,
  638. .probe = vmci_guest_probe_device,
  639. .remove = vmci_guest_remove_device,
  640. };
  641. int __init vmci_guest_init(void)
  642. {
  643. return pci_register_driver(&vmci_guest_driver);
  644. }
  645. void __exit vmci_guest_exit(void)
  646. {
  647. pci_unregister_driver(&vmci_guest_driver);
  648. }