4 * Copyright (C) 2012 VMware, Inc. All rights reserved.
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.
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
16 #include <linux/vmw_vmci_defs.h>
17 #include <linux/vmw_vmci_api.h>
18 #include <linux/moduleparam.h>
19 #include <linux/interrupt.h>
20 #include <linux/highmem.h>
21 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/sched.h>
25 #include <linux/slab.h>
26 #include <linux/init.h>
27 #include <linux/pci.h>
28 #include <linux/smp.h>
30 #include <linux/vmalloc.h>
32 #include "vmci_datagram.h"
33 #include "vmci_doorbell.h"
34 #include "vmci_context.h"
35 #include "vmci_driver.h"
36 #include "vmci_event.h"
38 #define PCI_DEVICE_ID_VMWARE_VMCI 0x0740
40 #define VMCI_UTIL_NUM_RESOURCES 1
42 static bool vmci_disable_msi;
43 module_param_named(disable_msi, vmci_disable_msi, bool, 0);
44 MODULE_PARM_DESC(disable_msi, "Disable MSI use in driver - (default=0)");
46 static bool vmci_disable_msix;
47 module_param_named(disable_msix, vmci_disable_msix, bool, 0);
48 MODULE_PARM_DESC(disable_msix, "Disable MSI-X use in driver - (default=0)");
50 static u32 ctx_update_sub_id = VMCI_INVALID_ID;
51 static u32 vm_context_id = VMCI_INVALID_ID;
53 struct vmci_guest_device {
54 struct device *dev; /* PCI device we are attached to */
57 bool exclusive_vectors;
59 struct tasklet_struct datagram_tasklet;
60 struct tasklet_struct bm_tasklet;
63 void *notification_bitmap;
64 dma_addr_t notification_base;
67 /* vmci_dev singleton device and supporting data*/
68 struct pci_dev *vmci_pdev;
69 static struct vmci_guest_device *vmci_dev_g;
70 static DEFINE_SPINLOCK(vmci_dev_spinlock);
72 static atomic_t vmci_num_guest_devices = ATOMIC_INIT(0);
74 bool vmci_guest_code_active(void)
76 return atomic_read(&vmci_num_guest_devices) != 0;
79 u32 vmci_get_vm_context_id(void)
81 if (vm_context_id == VMCI_INVALID_ID) {
82 struct vmci_datagram get_cid_msg;
84 vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
86 get_cid_msg.src = VMCI_ANON_SRC_HANDLE;
87 get_cid_msg.payload_size = 0;
88 vm_context_id = vmci_send_datagram(&get_cid_msg);
94 * VM to hypervisor call mechanism. We use the standard VMware naming
95 * convention since shared code is calling this function as well.
97 int vmci_send_datagram(struct vmci_datagram *dg)
104 return VMCI_ERROR_INVALID_ARGS;
107 * Need to acquire spinlock on the device because the datagram
108 * data may be spread over multiple pages and the monitor may
109 * interleave device user rpc calls from multiple
110 * VCPUs. Acquiring the spinlock precludes that
111 * possibility. Disabling interrupts to avoid incoming
112 * datagrams during a "rep out" and possibly landing up in
115 spin_lock_irqsave(&vmci_dev_spinlock, flags);
118 iowrite8_rep(vmci_dev_g->iobase + VMCI_DATA_OUT_ADDR,
119 dg, VMCI_DG_SIZE(dg));
120 result = ioread32(vmci_dev_g->iobase + VMCI_RESULT_LOW_ADDR);
122 result = VMCI_ERROR_UNAVAILABLE;
125 spin_unlock_irqrestore(&vmci_dev_spinlock, flags);
129 EXPORT_SYMBOL_GPL(vmci_send_datagram);
132 * Gets called with the new context id if updated or resumed.
135 static void vmci_guest_cid_update(u32 sub_id,
136 const struct vmci_event_data *event_data,
139 const struct vmci_event_payld_ctx *ev_payload =
140 vmci_event_data_const_payload(event_data);
142 if (sub_id != ctx_update_sub_id) {
143 pr_devel("Invalid subscriber (ID=0x%x)\n", sub_id);
147 if (!event_data || ev_payload->context_id == VMCI_INVALID_ID) {
148 pr_devel("Invalid event data\n");
152 pr_devel("Updating context from (ID=0x%x) to (ID=0x%x) on event (type=%d)\n",
153 vm_context_id, ev_payload->context_id, event_data->event);
155 vm_context_id = ev_payload->context_id;
159 * Verify that the host supports the hypercalls we need. If it does not,
160 * try to find fallback hypercalls and use those instead. Returns
161 * true if required hypercalls (or fallback hypercalls) are
162 * supported by the host, false otherwise.
164 static int vmci_check_host_caps(struct pci_dev *pdev)
167 struct vmci_resource_query_msg *msg;
168 u32 msg_size = sizeof(struct vmci_resource_query_hdr) +
169 VMCI_UTIL_NUM_RESOURCES * sizeof(u32);
170 struct vmci_datagram *check_msg;
172 check_msg = kmalloc(msg_size, GFP_KERNEL);
174 dev_err(&pdev->dev, "%s: Insufficient memory\n", __func__);
178 check_msg->dst = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
179 VMCI_RESOURCES_QUERY);
180 check_msg->src = VMCI_ANON_SRC_HANDLE;
181 check_msg->payload_size = msg_size - VMCI_DG_HEADERSIZE;
182 msg = (struct vmci_resource_query_msg *)VMCI_DG_PAYLOAD(check_msg);
184 msg->num_resources = VMCI_UTIL_NUM_RESOURCES;
185 msg->resources[0] = VMCI_GET_CONTEXT_ID;
187 /* Checks that hyper calls are supported */
188 result = vmci_send_datagram(check_msg) == 0x01;
191 dev_dbg(&pdev->dev, "%s: Host capability check: %s\n",
192 __func__, result ? "PASSED" : "FAILED");
194 /* We need the vector. There are no fallbacks. */
195 return result ? 0 : -ENXIO;
199 * Reads datagrams from the data in port and dispatches them. We
200 * always start reading datagrams into only the first page of the
201 * datagram buffer. If the datagrams don't fit into one page, we
202 * use the maximum datagram buffer size for the remainder of the
203 * invocation. This is a simple heuristic for not penalizing
206 * This function assumes that it has exclusive access to the data
207 * in port for the duration of the call.
209 static void vmci_dispatch_dgs(unsigned long data)
211 struct vmci_guest_device *vmci_dev = (struct vmci_guest_device *)data;
212 u8 *dg_in_buffer = vmci_dev->data_buffer;
213 struct vmci_datagram *dg;
214 size_t dg_in_buffer_size = VMCI_MAX_DG_SIZE;
215 size_t current_dg_in_buffer_size = PAGE_SIZE;
216 size_t remaining_bytes;
218 BUILD_BUG_ON(VMCI_MAX_DG_SIZE < PAGE_SIZE);
220 ioread8_rep(vmci_dev->iobase + VMCI_DATA_IN_ADDR,
221 vmci_dev->data_buffer, current_dg_in_buffer_size);
222 dg = (struct vmci_datagram *)dg_in_buffer;
223 remaining_bytes = current_dg_in_buffer_size;
225 while (dg->dst.resource != VMCI_INVALID_ID ||
226 remaining_bytes > PAGE_SIZE) {
230 * When the input buffer spans multiple pages, a datagram can
231 * start on any page boundary in the buffer.
233 if (dg->dst.resource == VMCI_INVALID_ID) {
234 dg = (struct vmci_datagram *)roundup(
235 (uintptr_t)dg + 1, PAGE_SIZE);
237 (size_t)(dg_in_buffer +
238 current_dg_in_buffer_size -
243 dg_in_size = VMCI_DG_SIZE_ALIGNED(dg);
245 if (dg_in_size <= dg_in_buffer_size) {
249 * If the remaining bytes in the datagram
250 * buffer doesn't contain the complete
251 * datagram, we first make sure we have enough
252 * room for it and then we read the reminder
253 * of the datagram and possibly any following
256 if (dg_in_size > remaining_bytes) {
257 if (remaining_bytes !=
258 current_dg_in_buffer_size) {
261 * We move the partial
262 * datagram to the front and
263 * read the reminder of the
264 * datagram and possibly
265 * following calls into the
268 memmove(dg_in_buffer, dg_in_buffer +
269 current_dg_in_buffer_size -
272 dg = (struct vmci_datagram *)
276 if (current_dg_in_buffer_size !=
278 current_dg_in_buffer_size =
281 ioread8_rep(vmci_dev->iobase +
283 vmci_dev->data_buffer +
285 current_dg_in_buffer_size -
290 * We special case event datagrams from the
293 if (dg->src.context == VMCI_HYPERVISOR_CONTEXT_ID &&
294 dg->dst.resource == VMCI_EVENT_HANDLER) {
295 result = vmci_event_dispatch(dg);
297 result = vmci_datagram_invoke_guest_handler(dg);
299 if (result < VMCI_SUCCESS)
300 dev_dbg(vmci_dev->dev,
301 "Datagram with resource (ID=0x%x) failed (err=%d)\n",
302 dg->dst.resource, result);
304 /* On to the next datagram. */
305 dg = (struct vmci_datagram *)((u8 *)dg +
308 size_t bytes_to_skip;
311 * Datagram doesn't fit in datagram buffer of maximal
314 dev_dbg(vmci_dev->dev,
315 "Failed to receive datagram (size=%u bytes)\n",
318 bytes_to_skip = dg_in_size - remaining_bytes;
319 if (current_dg_in_buffer_size != dg_in_buffer_size)
320 current_dg_in_buffer_size = dg_in_buffer_size;
323 ioread8_rep(vmci_dev->iobase +
325 vmci_dev->data_buffer,
326 current_dg_in_buffer_size);
327 if (bytes_to_skip <= current_dg_in_buffer_size)
330 bytes_to_skip -= current_dg_in_buffer_size;
332 dg = (struct vmci_datagram *)(dg_in_buffer +
337 (size_t) (dg_in_buffer + current_dg_in_buffer_size -
340 if (remaining_bytes < VMCI_DG_HEADERSIZE) {
341 /* Get the next batch of datagrams. */
343 ioread8_rep(vmci_dev->iobase + VMCI_DATA_IN_ADDR,
344 vmci_dev->data_buffer,
345 current_dg_in_buffer_size);
346 dg = (struct vmci_datagram *)dg_in_buffer;
347 remaining_bytes = current_dg_in_buffer_size;
353 * Scans the notification bitmap for raised flags, clears them
354 * and handles the notifications.
356 static void vmci_process_bitmap(unsigned long data)
358 struct vmci_guest_device *dev = (struct vmci_guest_device *)data;
360 if (!dev->notification_bitmap) {
361 dev_dbg(dev->dev, "No bitmap present in %s\n", __func__);
365 vmci_dbell_scan_notification_entries(dev->notification_bitmap);
369 * Interrupt handler for legacy or MSI interrupt, or for first MSI-X
370 * interrupt (vector VMCI_INTR_DATAGRAM).
372 static irqreturn_t vmci_interrupt(int irq, void *_dev)
374 struct vmci_guest_device *dev = _dev;
377 * If we are using MSI-X with exclusive vectors then we simply schedule
378 * the datagram tasklet, since we know the interrupt was meant for us.
379 * Otherwise we must read the ICR to determine what to do.
382 if (dev->exclusive_vectors) {
383 tasklet_schedule(&dev->datagram_tasklet);
387 /* Acknowledge interrupt and determine what needs doing. */
388 icr = ioread32(dev->iobase + VMCI_ICR_ADDR);
389 if (icr == 0 || icr == ~0)
392 if (icr & VMCI_ICR_DATAGRAM) {
393 tasklet_schedule(&dev->datagram_tasklet);
394 icr &= ~VMCI_ICR_DATAGRAM;
397 if (icr & VMCI_ICR_NOTIFICATION) {
398 tasklet_schedule(&dev->bm_tasklet);
399 icr &= ~VMCI_ICR_NOTIFICATION;
404 "Ignoring unknown interrupt cause (%d)\n",
412 * Interrupt handler for MSI-X interrupt vector VMCI_INTR_NOTIFICATION,
413 * which is for the notification bitmap. Will only get called if we are
414 * using MSI-X with exclusive vectors.
416 static irqreturn_t vmci_interrupt_bm(int irq, void *_dev)
418 struct vmci_guest_device *dev = _dev;
420 /* For MSI-X we can just assume it was meant for us. */
421 tasklet_schedule(&dev->bm_tasklet);
427 * Most of the initialization at module load time is done here.
429 static int vmci_guest_probe_device(struct pci_dev *pdev,
430 const struct pci_device_id *id)
432 struct vmci_guest_device *vmci_dev;
433 void __iomem *iobase;
434 unsigned int capabilities;
439 dev_dbg(&pdev->dev, "Probing for vmci/PCI guest device\n");
441 error = pcim_enable_device(pdev);
444 "Failed to enable VMCI device: %d\n", error);
448 error = pcim_iomap_regions(pdev, 1 << 0, KBUILD_MODNAME);
450 dev_err(&pdev->dev, "Failed to reserve/map IO regions\n");
454 iobase = pcim_iomap_table(pdev)[0];
456 dev_info(&pdev->dev, "Found VMCI PCI device at %#lx, irq %u\n",
457 (unsigned long)iobase, pdev->irq);
459 vmci_dev = devm_kzalloc(&pdev->dev, sizeof(*vmci_dev), GFP_KERNEL);
462 "Can't allocate memory for VMCI device\n");
466 vmci_dev->dev = &pdev->dev;
467 vmci_dev->exclusive_vectors = false;
468 vmci_dev->iobase = iobase;
470 tasklet_init(&vmci_dev->datagram_tasklet,
471 vmci_dispatch_dgs, (unsigned long)vmci_dev);
472 tasklet_init(&vmci_dev->bm_tasklet,
473 vmci_process_bitmap, (unsigned long)vmci_dev);
475 vmci_dev->data_buffer = vmalloc(VMCI_MAX_DG_SIZE);
476 if (!vmci_dev->data_buffer) {
478 "Can't allocate memory for datagram buffer\n");
482 pci_set_master(pdev); /* To enable queue_pair functionality. */
485 * Verify that the VMCI Device supports the capabilities that
486 * we need. If the device is missing capabilities that we would
487 * like to use, check for fallback capabilities and use those
488 * instead (so we can run a new VM on old hosts). Fail the load if
489 * a required capability is missing and there is no fallback.
491 * Right now, we need datagrams. There are no fallbacks.
493 capabilities = ioread32(vmci_dev->iobase + VMCI_CAPS_ADDR);
494 if (!(capabilities & VMCI_CAPS_DATAGRAM)) {
495 dev_err(&pdev->dev, "Device does not support datagrams\n");
497 goto err_free_data_buffer;
501 * If the hardware supports notifications, we will use that as
504 if (capabilities & VMCI_CAPS_NOTIFICATIONS) {
505 vmci_dev->notification_bitmap = dma_alloc_coherent(
506 &pdev->dev, PAGE_SIZE, &vmci_dev->notification_base,
508 if (!vmci_dev->notification_bitmap) {
510 "Unable to allocate notification bitmap\n");
512 memset(vmci_dev->notification_bitmap, 0, PAGE_SIZE);
513 capabilities |= VMCI_CAPS_NOTIFICATIONS;
517 dev_info(&pdev->dev, "Using capabilities 0x%x\n", capabilities);
519 /* Let the host know which capabilities we intend to use. */
520 iowrite32(capabilities, vmci_dev->iobase + VMCI_CAPS_ADDR);
522 /* Set up global device so that we can start sending datagrams */
523 spin_lock_irq(&vmci_dev_spinlock);
524 vmci_dev_g = vmci_dev;
526 spin_unlock_irq(&vmci_dev_spinlock);
529 * Register notification bitmap with device if that capability is
532 if (capabilities & VMCI_CAPS_NOTIFICATIONS) {
533 unsigned long bitmap_ppn =
534 vmci_dev->notification_base >> PAGE_SHIFT;
535 if (!vmci_dbell_register_notification_bitmap(bitmap_ppn)) {
537 "VMCI device unable to register notification bitmap with PPN 0x%x\n",
540 goto err_remove_vmci_dev_g;
544 /* Check host capabilities. */
545 error = vmci_check_host_caps(pdev);
547 goto err_remove_bitmap;
552 * We subscribe to the VMCI_EVENT_CTX_ID_UPDATE here so we can
553 * update the internal context id when needed.
555 vmci_err = vmci_event_subscribe(VMCI_EVENT_CTX_ID_UPDATE,
556 vmci_guest_cid_update, NULL,
558 if (vmci_err < VMCI_SUCCESS)
560 "Failed to subscribe to event (type=%d): %d\n",
561 VMCI_EVENT_CTX_ID_UPDATE, vmci_err);
564 * Enable interrupts. Try MSI-X first, then MSI, and then fallback on
567 error = pci_alloc_irq_vectors(pdev, VMCI_MAX_INTRS, VMCI_MAX_INTRS,
570 error = pci_alloc_irq_vectors(pdev, 1, 1,
571 PCI_IRQ_MSIX | PCI_IRQ_MSI | PCI_IRQ_LEGACY);
573 goto err_remove_bitmap;
575 vmci_dev->exclusive_vectors = true;
579 * Request IRQ for legacy or MSI interrupts, or for first
582 error = request_irq(pci_irq_vector(pdev, 0), vmci_interrupt,
583 IRQF_SHARED, KBUILD_MODNAME, vmci_dev);
585 dev_err(&pdev->dev, "Irq %u in use: %d\n",
586 pci_irq_vector(pdev, 0), error);
587 goto err_disable_msi;
591 * For MSI-X with exclusive vectors we need to request an
592 * interrupt for each vector so that we get a separate
593 * interrupt handler routine. This allows us to distinguish
594 * between the vectors.
596 if (vmci_dev->exclusive_vectors) {
597 error = request_irq(pci_irq_vector(pdev, 1),
598 vmci_interrupt_bm, 0, KBUILD_MODNAME,
602 "Failed to allocate irq %u: %d\n",
603 pci_irq_vector(pdev, 1), error);
608 dev_dbg(&pdev->dev, "Registered device\n");
610 atomic_inc(&vmci_num_guest_devices);
612 /* Enable specific interrupt bits. */
613 cmd = VMCI_IMR_DATAGRAM;
614 if (capabilities & VMCI_CAPS_NOTIFICATIONS)
615 cmd |= VMCI_IMR_NOTIFICATION;
616 iowrite32(cmd, vmci_dev->iobase + VMCI_IMR_ADDR);
618 /* Enable interrupts. */
619 iowrite32(VMCI_CONTROL_INT_ENABLE,
620 vmci_dev->iobase + VMCI_CONTROL_ADDR);
622 pci_set_drvdata(pdev, vmci_dev);
626 free_irq(pci_irq_vector(pdev, 0), vmci_dev);
627 tasklet_kill(&vmci_dev->datagram_tasklet);
628 tasklet_kill(&vmci_dev->bm_tasklet);
631 pci_free_irq_vectors(pdev);
633 vmci_err = vmci_event_unsubscribe(ctx_update_sub_id);
634 if (vmci_err < VMCI_SUCCESS)
636 "Failed to unsubscribe from event (type=%d) with subscriber (ID=0x%x): %d\n",
637 VMCI_EVENT_CTX_ID_UPDATE, ctx_update_sub_id, vmci_err);
640 if (vmci_dev->notification_bitmap) {
641 iowrite32(VMCI_CONTROL_RESET,
642 vmci_dev->iobase + VMCI_CONTROL_ADDR);
643 dma_free_coherent(&pdev->dev, PAGE_SIZE,
644 vmci_dev->notification_bitmap,
645 vmci_dev->notification_base);
648 err_remove_vmci_dev_g:
649 spin_lock_irq(&vmci_dev_spinlock);
652 spin_unlock_irq(&vmci_dev_spinlock);
654 err_free_data_buffer:
655 vfree(vmci_dev->data_buffer);
657 /* The rest are managed resources and will be freed by PCI core */
661 static void vmci_guest_remove_device(struct pci_dev *pdev)
663 struct vmci_guest_device *vmci_dev = pci_get_drvdata(pdev);
666 dev_dbg(&pdev->dev, "Removing device\n");
668 atomic_dec(&vmci_num_guest_devices);
670 vmci_qp_guest_endpoints_exit();
672 vmci_err = vmci_event_unsubscribe(ctx_update_sub_id);
673 if (vmci_err < VMCI_SUCCESS)
675 "Failed to unsubscribe from event (type=%d) with subscriber (ID=0x%x): %d\n",
676 VMCI_EVENT_CTX_ID_UPDATE, ctx_update_sub_id, vmci_err);
678 spin_lock_irq(&vmci_dev_spinlock);
681 spin_unlock_irq(&vmci_dev_spinlock);
683 dev_dbg(&pdev->dev, "Resetting vmci device\n");
684 iowrite32(VMCI_CONTROL_RESET, vmci_dev->iobase + VMCI_CONTROL_ADDR);
687 * Free IRQ and then disable MSI/MSI-X as appropriate. For
688 * MSI-X, we might have multiple vectors, each with their own
689 * IRQ, which we must free too.
691 if (vmci_dev->exclusive_vectors)
692 free_irq(pci_irq_vector(pdev, 1), vmci_dev);
693 free_irq(pci_irq_vector(pdev, 0), vmci_dev);
694 pci_free_irq_vectors(pdev);
696 tasklet_kill(&vmci_dev->datagram_tasklet);
697 tasklet_kill(&vmci_dev->bm_tasklet);
699 if (vmci_dev->notification_bitmap) {
701 * The device reset above cleared the bitmap state of the
702 * device, so we can safely free it here.
705 dma_free_coherent(&pdev->dev, PAGE_SIZE,
706 vmci_dev->notification_bitmap,
707 vmci_dev->notification_base);
710 vfree(vmci_dev->data_buffer);
712 /* The rest are managed resources and will be freed by PCI core */
715 static const struct pci_device_id vmci_ids[] = {
716 { PCI_DEVICE(PCI_VENDOR_ID_VMWARE, PCI_DEVICE_ID_VMWARE_VMCI), },
719 MODULE_DEVICE_TABLE(pci, vmci_ids);
721 static struct pci_driver vmci_guest_driver = {
722 .name = KBUILD_MODNAME,
723 .id_table = vmci_ids,
724 .probe = vmci_guest_probe_device,
725 .remove = vmci_guest_remove_device,
728 int __init vmci_guest_init(void)
730 return pci_register_driver(&vmci_guest_driver);
733 void __exit vmci_guest_exit(void)
735 pci_unregister_driver(&vmci_guest_driver);