1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2012 VMware, Inc. All rights reserved.
8 #include <linux/vmw_vmci_defs.h>
9 #include <linux/vmw_vmci_api.h>
10 #include <linux/moduleparam.h>
11 #include <linux/interrupt.h>
12 #include <linux/highmem.h>
13 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/processor.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/pci.h>
21 #include <linux/smp.h>
23 #include <linux/vmalloc.h>
25 #include "vmci_datagram.h"
26 #include "vmci_doorbell.h"
27 #include "vmci_context.h"
28 #include "vmci_driver.h"
29 #include "vmci_event.h"
31 #define PCI_DEVICE_ID_VMWARE_VMCI 0x0740
33 #define VMCI_UTIL_NUM_RESOURCES 1
36 * Datagram buffers for DMA send/receive must accommodate at least
37 * a maximum sized datagram and the header.
39 #define VMCI_DMA_DG_BUFFER_SIZE (VMCI_MAX_DG_SIZE + PAGE_SIZE)
41 static bool vmci_disable_msi;
42 module_param_named(disable_msi, vmci_disable_msi, bool, 0);
43 MODULE_PARM_DESC(disable_msi, "Disable MSI use in driver - (default=0)");
45 static bool vmci_disable_msix;
46 module_param_named(disable_msix, vmci_disable_msix, bool, 0);
47 MODULE_PARM_DESC(disable_msix, "Disable MSI-X use in driver - (default=0)");
49 static u32 ctx_update_sub_id = VMCI_INVALID_ID;
50 static u32 vm_context_id = VMCI_INVALID_ID;
52 struct vmci_guest_device {
53 struct device *dev; /* PCI device we are attached to */
55 void __iomem *mmio_base;
57 bool exclusive_vectors;
59 struct tasklet_struct datagram_tasklet;
60 struct tasklet_struct bm_tasklet;
61 struct wait_queue_head inout_wq;
64 dma_addr_t data_buffer_base;
66 dma_addr_t tx_buffer_base;
67 void *notification_bitmap;
68 dma_addr_t notification_base;
71 static bool use_ppn64;
73 bool vmci_use_ppn64(void)
78 /* vmci_dev singleton device and supporting data*/
79 struct pci_dev *vmci_pdev;
80 static struct vmci_guest_device *vmci_dev_g;
81 static DEFINE_SPINLOCK(vmci_dev_spinlock);
83 static atomic_t vmci_num_guest_devices = ATOMIC_INIT(0);
85 bool vmci_guest_code_active(void)
87 return atomic_read(&vmci_num_guest_devices) != 0;
90 u32 vmci_get_vm_context_id(void)
92 if (vm_context_id == VMCI_INVALID_ID) {
93 struct vmci_datagram get_cid_msg;
95 vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
97 get_cid_msg.src = VMCI_ANON_SRC_HANDLE;
98 get_cid_msg.payload_size = 0;
99 vm_context_id = vmci_send_datagram(&get_cid_msg);
101 return vm_context_id;
104 static unsigned int vmci_read_reg(struct vmci_guest_device *dev, u32 reg)
106 if (dev->mmio_base != NULL)
107 return readl(dev->mmio_base + reg);
108 return ioread32(dev->iobase + reg);
111 static void vmci_write_reg(struct vmci_guest_device *dev, u32 val, u32 reg)
113 if (dev->mmio_base != NULL)
114 writel(val, dev->mmio_base + reg);
116 iowrite32(val, dev->iobase + reg);
119 static void vmci_read_data(struct vmci_guest_device *vmci_dev,
120 void *dest, size_t size)
122 if (vmci_dev->mmio_base == NULL)
123 ioread8_rep(vmci_dev->iobase + VMCI_DATA_IN_ADDR,
127 * For DMA datagrams, the data_buffer will contain the header on the
128 * first page, followed by the incoming datagram(s) on the following
129 * pages. The header uses an S/G element immediately following the
130 * header on the first page to point to the data area.
132 struct vmci_data_in_out_header *buffer_header = vmci_dev->data_buffer;
133 struct vmci_sg_elem *sg_array = (struct vmci_sg_elem *)(buffer_header + 1);
134 size_t buffer_offset = dest - vmci_dev->data_buffer;
136 buffer_header->opcode = 1;
137 buffer_header->size = 1;
138 buffer_header->busy = 0;
139 sg_array[0].addr = vmci_dev->data_buffer_base + buffer_offset;
140 sg_array[0].size = size;
142 vmci_write_reg(vmci_dev, lower_32_bits(vmci_dev->data_buffer_base),
143 VMCI_DATA_IN_LOW_ADDR);
145 wait_event(vmci_dev->inout_wq, buffer_header->busy == 1);
149 static int vmci_write_data(struct vmci_guest_device *dev,
150 struct vmci_datagram *dg)
154 if (dev->mmio_base != NULL) {
155 struct vmci_data_in_out_header *buffer_header = dev->tx_buffer;
156 u8 *dg_out_buffer = (u8 *)(buffer_header + 1);
158 if (VMCI_DG_SIZE(dg) > VMCI_MAX_DG_SIZE)
159 return VMCI_ERROR_INVALID_ARGS;
162 * Initialize send buffer with outgoing datagram
163 * and set up header for inline data. Device will
164 * not access buffer asynchronously - only after
165 * the write to VMCI_DATA_OUT_LOW_ADDR.
167 memcpy(dg_out_buffer, dg, VMCI_DG_SIZE(dg));
168 buffer_header->opcode = 0;
169 buffer_header->size = VMCI_DG_SIZE(dg);
170 buffer_header->busy = 1;
172 vmci_write_reg(dev, lower_32_bits(dev->tx_buffer_base),
173 VMCI_DATA_OUT_LOW_ADDR);
175 /* Caller holds a spinlock, so cannot block. */
176 spin_until_cond(buffer_header->busy == 0);
178 result = vmci_read_reg(vmci_dev_g, VMCI_RESULT_LOW_ADDR);
179 if (result == VMCI_SUCCESS)
180 result = (int)buffer_header->result;
182 iowrite8_rep(dev->iobase + VMCI_DATA_OUT_ADDR,
183 dg, VMCI_DG_SIZE(dg));
184 result = vmci_read_reg(vmci_dev_g, VMCI_RESULT_LOW_ADDR);
191 * VM to hypervisor call mechanism. We use the standard VMware naming
192 * convention since shared code is calling this function as well.
194 int vmci_send_datagram(struct vmci_datagram *dg)
201 return VMCI_ERROR_INVALID_ARGS;
204 * Need to acquire spinlock on the device because the datagram
205 * data may be spread over multiple pages and the monitor may
206 * interleave device user rpc calls from multiple
207 * VCPUs. Acquiring the spinlock precludes that
208 * possibility. Disabling interrupts to avoid incoming
209 * datagrams during a "rep out" and possibly landing up in
212 spin_lock_irqsave(&vmci_dev_spinlock, flags);
215 vmci_write_data(vmci_dev_g, dg);
216 result = vmci_read_reg(vmci_dev_g, VMCI_RESULT_LOW_ADDR);
218 result = VMCI_ERROR_UNAVAILABLE;
221 spin_unlock_irqrestore(&vmci_dev_spinlock, flags);
225 EXPORT_SYMBOL_GPL(vmci_send_datagram);
228 * Gets called with the new context id if updated or resumed.
231 static void vmci_guest_cid_update(u32 sub_id,
232 const struct vmci_event_data *event_data,
235 const struct vmci_event_payld_ctx *ev_payload =
236 vmci_event_data_const_payload(event_data);
238 if (sub_id != ctx_update_sub_id) {
239 pr_devel("Invalid subscriber (ID=0x%x)\n", sub_id);
243 if (!event_data || ev_payload->context_id == VMCI_INVALID_ID) {
244 pr_devel("Invalid event data\n");
248 pr_devel("Updating context from (ID=0x%x) to (ID=0x%x) on event (type=%d)\n",
249 vm_context_id, ev_payload->context_id, event_data->event);
251 vm_context_id = ev_payload->context_id;
255 * Verify that the host supports the hypercalls we need. If it does not,
256 * try to find fallback hypercalls and use those instead. Returns 0 if
257 * required hypercalls (or fallback hypercalls) are supported by the host,
258 * an error code otherwise.
260 static int vmci_check_host_caps(struct pci_dev *pdev)
263 struct vmci_resource_query_msg *msg;
264 u32 msg_size = sizeof(struct vmci_resource_query_hdr) +
265 VMCI_UTIL_NUM_RESOURCES * sizeof(u32);
266 struct vmci_datagram *check_msg;
268 check_msg = kzalloc(msg_size, GFP_KERNEL);
270 dev_err(&pdev->dev, "%s: Insufficient memory\n", __func__);
274 check_msg->dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
275 VMCI_RESOURCES_QUERY);
276 check_msg->src = VMCI_ANON_SRC_HANDLE;
277 check_msg->payload_size = msg_size - VMCI_DG_HEADERSIZE;
278 msg = (struct vmci_resource_query_msg *)VMCI_DG_PAYLOAD(check_msg);
280 msg->num_resources = VMCI_UTIL_NUM_RESOURCES;
281 msg->resources[0] = VMCI_GET_CONTEXT_ID;
283 /* Checks that hyper calls are supported */
284 result = vmci_send_datagram(check_msg) == 0x01;
287 dev_dbg(&pdev->dev, "%s: Host capability check: %s\n",
288 __func__, result ? "PASSED" : "FAILED");
290 /* We need the vector. There are no fallbacks. */
291 return result ? 0 : -ENXIO;
295 * Reads datagrams from the device and dispatches them. For IO port
296 * based access to the device, we always start reading datagrams into
297 * only the first page of the datagram buffer. If the datagrams don't
298 * fit into one page, we use the maximum datagram buffer size for the
299 * remainder of the invocation. This is a simple heuristic for not
300 * penalizing small datagrams. For DMA-based datagrams, we always
301 * use the maximum datagram buffer size, since there is no performance
302 * penalty for doing so.
304 * This function assumes that it has exclusive access to the data
305 * in register(s) for the duration of the call.
307 static void vmci_dispatch_dgs(unsigned long data)
309 struct vmci_guest_device *vmci_dev = (struct vmci_guest_device *)data;
310 u8 *dg_in_buffer = vmci_dev->data_buffer;
311 struct vmci_datagram *dg;
312 size_t dg_in_buffer_size = VMCI_MAX_DG_SIZE;
313 size_t current_dg_in_buffer_size;
314 size_t remaining_bytes;
315 bool is_io_port = vmci_dev->mmio_base == NULL;
317 BUILD_BUG_ON(VMCI_MAX_DG_SIZE < PAGE_SIZE);
320 /* For mmio, the first page is used for the header. */
321 dg_in_buffer += PAGE_SIZE;
324 * For DMA-based datagram operations, there is no performance
325 * penalty for reading the maximum buffer size.
327 current_dg_in_buffer_size = VMCI_MAX_DG_SIZE;
329 current_dg_in_buffer_size = PAGE_SIZE;
331 vmci_read_data(vmci_dev, dg_in_buffer, current_dg_in_buffer_size);
332 dg = (struct vmci_datagram *)dg_in_buffer;
333 remaining_bytes = current_dg_in_buffer_size;
336 * Read through the buffer until an invalid datagram header is
337 * encountered. The exit condition for datagrams read through
338 * VMCI_DATA_IN_ADDR is a bit more complicated, since a datagram
339 * can start on any page boundary in the buffer.
341 while (dg->dst.resource != VMCI_INVALID_ID ||
342 (is_io_port && remaining_bytes > PAGE_SIZE)) {
346 * If using VMCI_DATA_IN_ADDR, skip to the next page
347 * as a datagram can start on any page boundary.
349 if (dg->dst.resource == VMCI_INVALID_ID) {
350 dg = (struct vmci_datagram *)roundup(
351 (uintptr_t)dg + 1, PAGE_SIZE);
353 (size_t)(dg_in_buffer +
354 current_dg_in_buffer_size -
359 dg_in_size = VMCI_DG_SIZE_ALIGNED(dg);
361 if (dg_in_size <= dg_in_buffer_size) {
365 * If the remaining bytes in the datagram
366 * buffer doesn't contain the complete
367 * datagram, we first make sure we have enough
368 * room for it and then we read the reminder
369 * of the datagram and possibly any following
372 if (dg_in_size > remaining_bytes) {
373 if (remaining_bytes !=
374 current_dg_in_buffer_size) {
377 * We move the partial
378 * datagram to the front and
379 * read the reminder of the
380 * datagram and possibly
381 * following calls into the
384 memmove(dg_in_buffer, dg_in_buffer +
385 current_dg_in_buffer_size -
388 dg = (struct vmci_datagram *)
392 if (current_dg_in_buffer_size !=
394 current_dg_in_buffer_size =
397 vmci_read_data(vmci_dev,
400 current_dg_in_buffer_size -
405 * We special case event datagrams from the
408 if (dg->src.context == VMCI_HYPERVISOR_CONTEXT_ID &&
409 dg->dst.resource == VMCI_EVENT_HANDLER) {
410 result = vmci_event_dispatch(dg);
412 result = vmci_datagram_invoke_guest_handler(dg);
414 if (result < VMCI_SUCCESS)
415 dev_dbg(vmci_dev->dev,
416 "Datagram with resource (ID=0x%x) failed (err=%d)\n",
417 dg->dst.resource, result);
419 /* On to the next datagram. */
420 dg = (struct vmci_datagram *)((u8 *)dg +
423 size_t bytes_to_skip;
426 * Datagram doesn't fit in datagram buffer of maximal
429 dev_dbg(vmci_dev->dev,
430 "Failed to receive datagram (size=%u bytes)\n",
433 bytes_to_skip = dg_in_size - remaining_bytes;
434 if (current_dg_in_buffer_size != dg_in_buffer_size)
435 current_dg_in_buffer_size = dg_in_buffer_size;
438 vmci_read_data(vmci_dev, dg_in_buffer,
439 current_dg_in_buffer_size);
440 if (bytes_to_skip <= current_dg_in_buffer_size)
443 bytes_to_skip -= current_dg_in_buffer_size;
445 dg = (struct vmci_datagram *)(dg_in_buffer +
450 (size_t) (dg_in_buffer + current_dg_in_buffer_size -
453 if (remaining_bytes < VMCI_DG_HEADERSIZE) {
454 /* Get the next batch of datagrams. */
456 vmci_read_data(vmci_dev, dg_in_buffer,
457 current_dg_in_buffer_size);
458 dg = (struct vmci_datagram *)dg_in_buffer;
459 remaining_bytes = current_dg_in_buffer_size;
465 * Scans the notification bitmap for raised flags, clears them
466 * and handles the notifications.
468 static void vmci_process_bitmap(unsigned long data)
470 struct vmci_guest_device *dev = (struct vmci_guest_device *)data;
472 if (!dev->notification_bitmap) {
473 dev_dbg(dev->dev, "No bitmap present in %s\n", __func__);
477 vmci_dbell_scan_notification_entries(dev->notification_bitmap);
481 * Interrupt handler for legacy or MSI interrupt, or for first MSI-X
482 * interrupt (vector VMCI_INTR_DATAGRAM).
484 static irqreturn_t vmci_interrupt(int irq, void *_dev)
486 struct vmci_guest_device *dev = _dev;
489 * If we are using MSI-X with exclusive vectors then we simply schedule
490 * the datagram tasklet, since we know the interrupt was meant for us.
491 * Otherwise we must read the ICR to determine what to do.
494 if (dev->exclusive_vectors) {
495 tasklet_schedule(&dev->datagram_tasklet);
499 /* Acknowledge interrupt and determine what needs doing. */
500 icr = vmci_read_reg(dev, VMCI_ICR_ADDR);
501 if (icr == 0 || icr == ~0)
504 if (icr & VMCI_ICR_DATAGRAM) {
505 tasklet_schedule(&dev->datagram_tasklet);
506 icr &= ~VMCI_ICR_DATAGRAM;
509 if (icr & VMCI_ICR_NOTIFICATION) {
510 tasklet_schedule(&dev->bm_tasklet);
511 icr &= ~VMCI_ICR_NOTIFICATION;
515 if (icr & VMCI_ICR_DMA_DATAGRAM) {
516 wake_up_all(&dev->inout_wq);
517 icr &= ~VMCI_ICR_DMA_DATAGRAM;
522 "Ignoring unknown interrupt cause (%d)\n",
530 * Interrupt handler for MSI-X interrupt vector VMCI_INTR_NOTIFICATION,
531 * which is for the notification bitmap. Will only get called if we are
532 * using MSI-X with exclusive vectors.
534 static irqreturn_t vmci_interrupt_bm(int irq, void *_dev)
536 struct vmci_guest_device *dev = _dev;
538 /* For MSI-X we can just assume it was meant for us. */
539 tasklet_schedule(&dev->bm_tasklet);
545 * Interrupt handler for MSI-X interrupt vector VMCI_INTR_DMA_DATAGRAM,
546 * which is for the completion of a DMA datagram send or receive operation.
547 * Will only get called if we are using MSI-X with exclusive vectors.
549 static irqreturn_t vmci_interrupt_dma_datagram(int irq, void *_dev)
551 struct vmci_guest_device *dev = _dev;
553 wake_up_all(&dev->inout_wq);
558 static void vmci_free_dg_buffers(struct vmci_guest_device *vmci_dev)
560 if (vmci_dev->mmio_base != NULL) {
561 if (vmci_dev->tx_buffer != NULL)
562 dma_free_coherent(vmci_dev->dev,
563 VMCI_DMA_DG_BUFFER_SIZE,
565 vmci_dev->tx_buffer_base);
566 if (vmci_dev->data_buffer != NULL)
567 dma_free_coherent(vmci_dev->dev,
568 VMCI_DMA_DG_BUFFER_SIZE,
569 vmci_dev->data_buffer,
570 vmci_dev->data_buffer_base);
572 vfree(vmci_dev->data_buffer);
577 * Most of the initialization at module load time is done here.
579 static int vmci_guest_probe_device(struct pci_dev *pdev,
580 const struct pci_device_id *id)
582 struct vmci_guest_device *vmci_dev;
583 void __iomem *iobase = NULL;
584 void __iomem *mmio_base = NULL;
585 unsigned int num_irq_vectors;
586 unsigned int capabilities;
587 unsigned int caps_in_use;
592 dev_dbg(&pdev->dev, "Probing for vmci/PCI guest device\n");
594 error = pcim_enable_device(pdev);
597 "Failed to enable VMCI device: %d\n", error);
602 * The VMCI device with mmio access to registers requests 256KB
603 * for BAR1. If present, driver will use new VMCI device
604 * functionality for register access and datagram send/recv.
607 if (pci_resource_len(pdev, 1) == VMCI_WITH_MMIO_ACCESS_BAR_SIZE) {
608 dev_info(&pdev->dev, "MMIO register access is available\n");
609 mmio_base = pci_iomap_range(pdev, 1, VMCI_MMIO_ACCESS_OFFSET,
610 VMCI_MMIO_ACCESS_SIZE);
611 /* If the map fails, we fall back to IOIO access. */
613 dev_warn(&pdev->dev, "Failed to map MMIO register access\n");
617 if (IS_ENABLED(CONFIG_ARM64)) {
618 dev_err(&pdev->dev, "MMIO base is invalid\n");
621 error = pcim_iomap_regions(pdev, BIT(0), KBUILD_MODNAME);
623 dev_err(&pdev->dev, "Failed to reserve/map IO regions\n");
626 iobase = pcim_iomap_table(pdev)[0];
629 vmci_dev = devm_kzalloc(&pdev->dev, sizeof(*vmci_dev), GFP_KERNEL);
632 "Can't allocate memory for VMCI device\n");
636 vmci_dev->dev = &pdev->dev;
637 vmci_dev->exclusive_vectors = false;
638 vmci_dev->iobase = iobase;
639 vmci_dev->mmio_base = mmio_base;
641 tasklet_init(&vmci_dev->datagram_tasklet,
642 vmci_dispatch_dgs, (unsigned long)vmci_dev);
643 tasklet_init(&vmci_dev->bm_tasklet,
644 vmci_process_bitmap, (unsigned long)vmci_dev);
645 init_waitqueue_head(&vmci_dev->inout_wq);
647 if (mmio_base != NULL) {
648 vmci_dev->tx_buffer = dma_alloc_coherent(&pdev->dev, VMCI_DMA_DG_BUFFER_SIZE,
649 &vmci_dev->tx_buffer_base,
651 if (!vmci_dev->tx_buffer) {
653 "Can't allocate memory for datagram tx buffer\n");
657 vmci_dev->data_buffer = dma_alloc_coherent(&pdev->dev, VMCI_DMA_DG_BUFFER_SIZE,
658 &vmci_dev->data_buffer_base,
661 vmci_dev->data_buffer = vmalloc(VMCI_MAX_DG_SIZE);
663 if (!vmci_dev->data_buffer) {
665 "Can't allocate memory for datagram buffer\n");
667 goto err_free_data_buffers;
670 pci_set_master(pdev); /* To enable queue_pair functionality. */
673 * Verify that the VMCI Device supports the capabilities that
674 * we need. If the device is missing capabilities that we would
675 * like to use, check for fallback capabilities and use those
676 * instead (so we can run a new VM on old hosts). Fail the load if
677 * a required capability is missing and there is no fallback.
679 * Right now, we need datagrams. There are no fallbacks.
681 capabilities = vmci_read_reg(vmci_dev, VMCI_CAPS_ADDR);
682 if (!(capabilities & VMCI_CAPS_DATAGRAM)) {
683 dev_err(&pdev->dev, "Device does not support datagrams\n");
685 goto err_free_data_buffers;
687 caps_in_use = VMCI_CAPS_DATAGRAM;
690 * Use 64-bit PPNs if the device supports.
692 * There is no check for the return value of dma_set_mask_and_coherent
693 * since this driver can handle the default mask values if
694 * dma_set_mask_and_coherent fails.
696 if (capabilities & VMCI_CAPS_PPN64) {
697 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
699 caps_in_use |= VMCI_CAPS_PPN64;
701 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(44));
706 * If the hardware supports notifications, we will use that as
709 if (capabilities & VMCI_CAPS_NOTIFICATIONS) {
710 vmci_dev->notification_bitmap = dma_alloc_coherent(
711 &pdev->dev, PAGE_SIZE, &vmci_dev->notification_base,
713 if (!vmci_dev->notification_bitmap)
715 "Unable to allocate notification bitmap\n");
717 caps_in_use |= VMCI_CAPS_NOTIFICATIONS;
720 if (mmio_base != NULL) {
721 if (capabilities & VMCI_CAPS_DMA_DATAGRAM) {
722 caps_in_use |= VMCI_CAPS_DMA_DATAGRAM;
725 "Missing capability: VMCI_CAPS_DMA_DATAGRAM\n");
727 goto err_free_notification_bitmap;
731 dev_info(&pdev->dev, "Using capabilities 0x%x\n", caps_in_use);
733 /* Let the host know which capabilities we intend to use. */
734 vmci_write_reg(vmci_dev, caps_in_use, VMCI_CAPS_ADDR);
736 if (caps_in_use & VMCI_CAPS_DMA_DATAGRAM) {
737 /* Let the device know the size for pages passed down. */
738 vmci_write_reg(vmci_dev, PAGE_SHIFT, VMCI_GUEST_PAGE_SHIFT);
740 /* Configure the high order parts of the data in/out buffers. */
741 vmci_write_reg(vmci_dev, upper_32_bits(vmci_dev->data_buffer_base),
742 VMCI_DATA_IN_HIGH_ADDR);
743 vmci_write_reg(vmci_dev, upper_32_bits(vmci_dev->tx_buffer_base),
744 VMCI_DATA_OUT_HIGH_ADDR);
747 /* Set up global device so that we can start sending datagrams */
748 spin_lock_irq(&vmci_dev_spinlock);
749 vmci_dev_g = vmci_dev;
751 spin_unlock_irq(&vmci_dev_spinlock);
754 * Register notification bitmap with device if that capability is
757 if (caps_in_use & VMCI_CAPS_NOTIFICATIONS) {
758 unsigned long bitmap_ppn =
759 vmci_dev->notification_base >> PAGE_SHIFT;
760 if (!vmci_dbell_register_notification_bitmap(bitmap_ppn)) {
762 "VMCI device unable to register notification bitmap with PPN 0x%lx\n",
765 goto err_remove_vmci_dev_g;
769 /* Check host capabilities. */
770 error = vmci_check_host_caps(pdev);
772 goto err_remove_vmci_dev_g;
777 * We subscribe to the VMCI_EVENT_CTX_ID_UPDATE here so we can
778 * update the internal context id when needed.
780 vmci_err = vmci_event_subscribe(VMCI_EVENT_CTX_ID_UPDATE,
781 vmci_guest_cid_update, NULL,
783 if (vmci_err < VMCI_SUCCESS)
785 "Failed to subscribe to event (type=%d): %d\n",
786 VMCI_EVENT_CTX_ID_UPDATE, vmci_err);
789 * Enable interrupts. Try MSI-X first, then MSI, and then fallback on
792 if (vmci_dev->mmio_base != NULL)
793 num_irq_vectors = VMCI_MAX_INTRS;
795 num_irq_vectors = VMCI_MAX_INTRS_NOTIFICATION;
796 error = pci_alloc_irq_vectors(pdev, num_irq_vectors, num_irq_vectors,
799 error = pci_alloc_irq_vectors(pdev, 1, 1,
800 PCI_IRQ_MSIX | PCI_IRQ_MSI | PCI_IRQ_LEGACY);
802 goto err_unsubscribe_event;
804 vmci_dev->exclusive_vectors = true;
808 * Request IRQ for legacy or MSI interrupts, or for first
811 error = request_irq(pci_irq_vector(pdev, 0), vmci_interrupt,
812 IRQF_SHARED, KBUILD_MODNAME, vmci_dev);
814 dev_err(&pdev->dev, "Irq %u in use: %d\n",
815 pci_irq_vector(pdev, 0), error);
816 goto err_disable_msi;
820 * For MSI-X with exclusive vectors we need to request an
821 * interrupt for each vector so that we get a separate
822 * interrupt handler routine. This allows us to distinguish
823 * between the vectors.
825 if (vmci_dev->exclusive_vectors) {
826 error = request_irq(pci_irq_vector(pdev, 1),
827 vmci_interrupt_bm, 0, KBUILD_MODNAME,
831 "Failed to allocate irq %u: %d\n",
832 pci_irq_vector(pdev, 1), error);
835 if (caps_in_use & VMCI_CAPS_DMA_DATAGRAM) {
836 error = request_irq(pci_irq_vector(pdev, 2),
837 vmci_interrupt_dma_datagram,
838 0, KBUILD_MODNAME, vmci_dev);
841 "Failed to allocate irq %u: %d\n",
842 pci_irq_vector(pdev, 2), error);
843 goto err_free_bm_irq;
848 dev_dbg(&pdev->dev, "Registered device\n");
850 atomic_inc(&vmci_num_guest_devices);
852 /* Enable specific interrupt bits. */
853 cmd = VMCI_IMR_DATAGRAM;
854 if (caps_in_use & VMCI_CAPS_NOTIFICATIONS)
855 cmd |= VMCI_IMR_NOTIFICATION;
856 if (caps_in_use & VMCI_CAPS_DMA_DATAGRAM)
857 cmd |= VMCI_IMR_DMA_DATAGRAM;
858 vmci_write_reg(vmci_dev, cmd, VMCI_IMR_ADDR);
860 /* Enable interrupts. */
861 vmci_write_reg(vmci_dev, VMCI_CONTROL_INT_ENABLE, VMCI_CONTROL_ADDR);
863 pci_set_drvdata(pdev, vmci_dev);
865 vmci_call_vsock_callback(false);
869 if (vmci_dev->exclusive_vectors)
870 free_irq(pci_irq_vector(pdev, 1), vmci_dev);
873 free_irq(pci_irq_vector(pdev, 0), vmci_dev);
874 tasklet_kill(&vmci_dev->datagram_tasklet);
875 tasklet_kill(&vmci_dev->bm_tasklet);
878 pci_free_irq_vectors(pdev);
880 err_unsubscribe_event:
881 vmci_err = vmci_event_unsubscribe(ctx_update_sub_id);
882 if (vmci_err < VMCI_SUCCESS)
884 "Failed to unsubscribe from event (type=%d) with subscriber (ID=0x%x): %d\n",
885 VMCI_EVENT_CTX_ID_UPDATE, ctx_update_sub_id, vmci_err);
887 err_remove_vmci_dev_g:
888 spin_lock_irq(&vmci_dev_spinlock);
891 spin_unlock_irq(&vmci_dev_spinlock);
893 err_free_notification_bitmap:
894 if (vmci_dev->notification_bitmap) {
895 vmci_write_reg(vmci_dev, VMCI_CONTROL_RESET, VMCI_CONTROL_ADDR);
896 dma_free_coherent(&pdev->dev, PAGE_SIZE,
897 vmci_dev->notification_bitmap,
898 vmci_dev->notification_base);
901 err_free_data_buffers:
902 vmci_free_dg_buffers(vmci_dev);
904 /* The rest are managed resources and will be freed by PCI core */
908 static void vmci_guest_remove_device(struct pci_dev *pdev)
910 struct vmci_guest_device *vmci_dev = pci_get_drvdata(pdev);
913 dev_dbg(&pdev->dev, "Removing device\n");
915 atomic_dec(&vmci_num_guest_devices);
917 vmci_qp_guest_endpoints_exit();
919 vmci_err = vmci_event_unsubscribe(ctx_update_sub_id);
920 if (vmci_err < VMCI_SUCCESS)
922 "Failed to unsubscribe from event (type=%d) with subscriber (ID=0x%x): %d\n",
923 VMCI_EVENT_CTX_ID_UPDATE, ctx_update_sub_id, vmci_err);
925 spin_lock_irq(&vmci_dev_spinlock);
928 spin_unlock_irq(&vmci_dev_spinlock);
930 dev_dbg(&pdev->dev, "Resetting vmci device\n");
931 vmci_write_reg(vmci_dev, VMCI_CONTROL_RESET, VMCI_CONTROL_ADDR);
934 * Free IRQ and then disable MSI/MSI-X as appropriate. For
935 * MSI-X, we might have multiple vectors, each with their own
936 * IRQ, which we must free too.
938 if (vmci_dev->exclusive_vectors) {
939 free_irq(pci_irq_vector(pdev, 1), vmci_dev);
940 if (vmci_dev->mmio_base != NULL)
941 free_irq(pci_irq_vector(pdev, 2), vmci_dev);
943 free_irq(pci_irq_vector(pdev, 0), vmci_dev);
944 pci_free_irq_vectors(pdev);
946 tasklet_kill(&vmci_dev->datagram_tasklet);
947 tasklet_kill(&vmci_dev->bm_tasklet);
949 if (vmci_dev->notification_bitmap) {
951 * The device reset above cleared the bitmap state of the
952 * device, so we can safely free it here.
955 dma_free_coherent(&pdev->dev, PAGE_SIZE,
956 vmci_dev->notification_bitmap,
957 vmci_dev->notification_base);
960 vmci_free_dg_buffers(vmci_dev);
962 if (vmci_dev->mmio_base != NULL)
963 pci_iounmap(pdev, vmci_dev->mmio_base);
965 /* The rest are managed resources and will be freed by PCI core */
968 static const struct pci_device_id vmci_ids[] = {
969 { PCI_DEVICE(PCI_VENDOR_ID_VMWARE, PCI_DEVICE_ID_VMWARE_VMCI), },
972 MODULE_DEVICE_TABLE(pci, vmci_ids);
974 static struct pci_driver vmci_guest_driver = {
975 .name = KBUILD_MODNAME,
976 .id_table = vmci_ids,
977 .probe = vmci_guest_probe_device,
978 .remove = vmci_guest_remove_device,
981 int __init vmci_guest_init(void)
983 return pci_register_driver(&vmci_guest_driver);
986 void __exit vmci_guest_exit(void)
988 pci_unregister_driver(&vmci_guest_driver);