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/sched.h>
17 #include <linux/slab.h>
18 #include <linux/init.h>
19 #include <linux/pci.h>
20 #include <linux/smp.h>
22 #include <linux/vmalloc.h>
24 #include "vmci_datagram.h"
25 #include "vmci_doorbell.h"
26 #include "vmci_context.h"
27 #include "vmci_driver.h"
28 #include "vmci_event.h"
30 #define PCI_DEVICE_ID_VMWARE_VMCI 0x0740
32 #define VMCI_UTIL_NUM_RESOURCES 1
34 static bool vmci_disable_msi
;
35 module_param_named(disable_msi
, vmci_disable_msi
, bool, 0);
36 MODULE_PARM_DESC(disable_msi
, "Disable MSI use in driver - (default=0)");
38 static bool vmci_disable_msix
;
39 module_param_named(disable_msix
, vmci_disable_msix
, bool, 0);
40 MODULE_PARM_DESC(disable_msix
, "Disable MSI-X use in driver - (default=0)");
42 static u32 ctx_update_sub_id
= VMCI_INVALID_ID
;
43 static u32 vm_context_id
= VMCI_INVALID_ID
;
45 struct vmci_guest_device
{
46 struct device
*dev
; /* PCI device we are attached to */
48 void __iomem
*mmio_base
;
50 bool exclusive_vectors
;
52 struct tasklet_struct datagram_tasklet
;
53 struct tasklet_struct bm_tasklet
;
56 void *notification_bitmap
;
57 dma_addr_t notification_base
;
60 static bool use_ppn64
;
62 bool vmci_use_ppn64(void)
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
);
93 static unsigned int vmci_read_reg(struct vmci_guest_device
*dev
, u32 reg
)
95 if (dev
->mmio_base
!= NULL
)
96 return readl(dev
->mmio_base
+ reg
);
97 return ioread32(dev
->iobase
+ reg
);
100 static void vmci_write_reg(struct vmci_guest_device
*dev
, u32 val
, u32 reg
)
102 if (dev
->mmio_base
!= NULL
)
103 writel(val
, dev
->mmio_base
+ reg
);
105 iowrite32(val
, dev
->iobase
+ reg
);
109 * VM to hypervisor call mechanism. We use the standard VMware naming
110 * convention since shared code is calling this function as well.
112 int vmci_send_datagram(struct vmci_datagram
*dg
)
119 return VMCI_ERROR_INVALID_ARGS
;
122 * Need to acquire spinlock on the device because the datagram
123 * data may be spread over multiple pages and the monitor may
124 * interleave device user rpc calls from multiple
125 * VCPUs. Acquiring the spinlock precludes that
126 * possibility. Disabling interrupts to avoid incoming
127 * datagrams during a "rep out" and possibly landing up in
130 spin_lock_irqsave(&vmci_dev_spinlock
, flags
);
133 iowrite8_rep(vmci_dev_g
->iobase
+ VMCI_DATA_OUT_ADDR
,
134 dg
, VMCI_DG_SIZE(dg
));
135 result
= vmci_read_reg(vmci_dev_g
, VMCI_RESULT_LOW_ADDR
);
137 result
= VMCI_ERROR_UNAVAILABLE
;
140 spin_unlock_irqrestore(&vmci_dev_spinlock
, flags
);
144 EXPORT_SYMBOL_GPL(vmci_send_datagram
);
147 * Gets called with the new context id if updated or resumed.
150 static void vmci_guest_cid_update(u32 sub_id
,
151 const struct vmci_event_data
*event_data
,
154 const struct vmci_event_payld_ctx
*ev_payload
=
155 vmci_event_data_const_payload(event_data
);
157 if (sub_id
!= ctx_update_sub_id
) {
158 pr_devel("Invalid subscriber (ID=0x%x)\n", sub_id
);
162 if (!event_data
|| ev_payload
->context_id
== VMCI_INVALID_ID
) {
163 pr_devel("Invalid event data\n");
167 pr_devel("Updating context from (ID=0x%x) to (ID=0x%x) on event (type=%d)\n",
168 vm_context_id
, ev_payload
->context_id
, event_data
->event
);
170 vm_context_id
= ev_payload
->context_id
;
174 * Verify that the host supports the hypercalls we need. If it does not,
175 * try to find fallback hypercalls and use those instead. Returns
176 * true if required hypercalls (or fallback hypercalls) are
177 * supported by the host, false otherwise.
179 static int vmci_check_host_caps(struct pci_dev
*pdev
)
182 struct vmci_resource_query_msg
*msg
;
183 u32 msg_size
= sizeof(struct vmci_resource_query_hdr
) +
184 VMCI_UTIL_NUM_RESOURCES
* sizeof(u32
);
185 struct vmci_datagram
*check_msg
;
187 check_msg
= kzalloc(msg_size
, GFP_KERNEL
);
189 dev_err(&pdev
->dev
, "%s: Insufficient memory\n", __func__
);
193 check_msg
->dst
= vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID
,
194 VMCI_RESOURCES_QUERY
);
195 check_msg
->src
= VMCI_ANON_SRC_HANDLE
;
196 check_msg
->payload_size
= msg_size
- VMCI_DG_HEADERSIZE
;
197 msg
= (struct vmci_resource_query_msg
*)VMCI_DG_PAYLOAD(check_msg
);
199 msg
->num_resources
= VMCI_UTIL_NUM_RESOURCES
;
200 msg
->resources
[0] = VMCI_GET_CONTEXT_ID
;
202 /* Checks that hyper calls are supported */
203 result
= vmci_send_datagram(check_msg
) == 0x01;
206 dev_dbg(&pdev
->dev
, "%s: Host capability check: %s\n",
207 __func__
, result
? "PASSED" : "FAILED");
209 /* We need the vector. There are no fallbacks. */
210 return result
? 0 : -ENXIO
;
214 * Reads datagrams from the data in port and dispatches them. We
215 * always start reading datagrams into only the first page of the
216 * datagram buffer. If the datagrams don't fit into one page, we
217 * use the maximum datagram buffer size for the remainder of the
218 * invocation. This is a simple heuristic for not penalizing
221 * This function assumes that it has exclusive access to the data
222 * in port for the duration of the call.
224 static void vmci_dispatch_dgs(unsigned long data
)
226 struct vmci_guest_device
*vmci_dev
= (struct vmci_guest_device
*)data
;
227 u8
*dg_in_buffer
= vmci_dev
->data_buffer
;
228 struct vmci_datagram
*dg
;
229 size_t dg_in_buffer_size
= VMCI_MAX_DG_SIZE
;
230 size_t current_dg_in_buffer_size
= PAGE_SIZE
;
231 size_t remaining_bytes
;
233 BUILD_BUG_ON(VMCI_MAX_DG_SIZE
< PAGE_SIZE
);
235 ioread8_rep(vmci_dev
->iobase
+ VMCI_DATA_IN_ADDR
,
236 vmci_dev
->data_buffer
, current_dg_in_buffer_size
);
237 dg
= (struct vmci_datagram
*)dg_in_buffer
;
238 remaining_bytes
= current_dg_in_buffer_size
;
240 while (dg
->dst
.resource
!= VMCI_INVALID_ID
||
241 remaining_bytes
> PAGE_SIZE
) {
245 * When the input buffer spans multiple pages, a datagram can
246 * start on any page boundary in the buffer.
248 if (dg
->dst
.resource
== VMCI_INVALID_ID
) {
249 dg
= (struct vmci_datagram
*)roundup(
250 (uintptr_t)dg
+ 1, PAGE_SIZE
);
252 (size_t)(dg_in_buffer
+
253 current_dg_in_buffer_size
-
258 dg_in_size
= VMCI_DG_SIZE_ALIGNED(dg
);
260 if (dg_in_size
<= dg_in_buffer_size
) {
264 * If the remaining bytes in the datagram
265 * buffer doesn't contain the complete
266 * datagram, we first make sure we have enough
267 * room for it and then we read the reminder
268 * of the datagram and possibly any following
271 if (dg_in_size
> remaining_bytes
) {
272 if (remaining_bytes
!=
273 current_dg_in_buffer_size
) {
276 * We move the partial
277 * datagram to the front and
278 * read the reminder of the
279 * datagram and possibly
280 * following calls into the
283 memmove(dg_in_buffer
, dg_in_buffer
+
284 current_dg_in_buffer_size
-
287 dg
= (struct vmci_datagram
*)
291 if (current_dg_in_buffer_size
!=
293 current_dg_in_buffer_size
=
296 ioread8_rep(vmci_dev
->iobase
+
298 vmci_dev
->data_buffer
+
300 current_dg_in_buffer_size
-
305 * We special case event datagrams from the
308 if (dg
->src
.context
== VMCI_HYPERVISOR_CONTEXT_ID
&&
309 dg
->dst
.resource
== VMCI_EVENT_HANDLER
) {
310 result
= vmci_event_dispatch(dg
);
312 result
= vmci_datagram_invoke_guest_handler(dg
);
314 if (result
< VMCI_SUCCESS
)
315 dev_dbg(vmci_dev
->dev
,
316 "Datagram with resource (ID=0x%x) failed (err=%d)\n",
317 dg
->dst
.resource
, result
);
319 /* On to the next datagram. */
320 dg
= (struct vmci_datagram
*)((u8
*)dg
+
323 size_t bytes_to_skip
;
326 * Datagram doesn't fit in datagram buffer of maximal
329 dev_dbg(vmci_dev
->dev
,
330 "Failed to receive datagram (size=%u bytes)\n",
333 bytes_to_skip
= dg_in_size
- remaining_bytes
;
334 if (current_dg_in_buffer_size
!= dg_in_buffer_size
)
335 current_dg_in_buffer_size
= dg_in_buffer_size
;
338 ioread8_rep(vmci_dev
->iobase
+
340 vmci_dev
->data_buffer
,
341 current_dg_in_buffer_size
);
342 if (bytes_to_skip
<= current_dg_in_buffer_size
)
345 bytes_to_skip
-= current_dg_in_buffer_size
;
347 dg
= (struct vmci_datagram
*)(dg_in_buffer
+
352 (size_t) (dg_in_buffer
+ current_dg_in_buffer_size
-
355 if (remaining_bytes
< VMCI_DG_HEADERSIZE
) {
356 /* Get the next batch of datagrams. */
358 ioread8_rep(vmci_dev
->iobase
+ VMCI_DATA_IN_ADDR
,
359 vmci_dev
->data_buffer
,
360 current_dg_in_buffer_size
);
361 dg
= (struct vmci_datagram
*)dg_in_buffer
;
362 remaining_bytes
= current_dg_in_buffer_size
;
368 * Scans the notification bitmap for raised flags, clears them
369 * and handles the notifications.
371 static void vmci_process_bitmap(unsigned long data
)
373 struct vmci_guest_device
*dev
= (struct vmci_guest_device
*)data
;
375 if (!dev
->notification_bitmap
) {
376 dev_dbg(dev
->dev
, "No bitmap present in %s\n", __func__
);
380 vmci_dbell_scan_notification_entries(dev
->notification_bitmap
);
384 * Interrupt handler for legacy or MSI interrupt, or for first MSI-X
385 * interrupt (vector VMCI_INTR_DATAGRAM).
387 static irqreturn_t
vmci_interrupt(int irq
, void *_dev
)
389 struct vmci_guest_device
*dev
= _dev
;
392 * If we are using MSI-X with exclusive vectors then we simply schedule
393 * the datagram tasklet, since we know the interrupt was meant for us.
394 * Otherwise we must read the ICR to determine what to do.
397 if (dev
->exclusive_vectors
) {
398 tasklet_schedule(&dev
->datagram_tasklet
);
402 /* Acknowledge interrupt and determine what needs doing. */
403 icr
= vmci_read_reg(dev
, VMCI_ICR_ADDR
);
404 if (icr
== 0 || icr
== ~0)
407 if (icr
& VMCI_ICR_DATAGRAM
) {
408 tasklet_schedule(&dev
->datagram_tasklet
);
409 icr
&= ~VMCI_ICR_DATAGRAM
;
412 if (icr
& VMCI_ICR_NOTIFICATION
) {
413 tasklet_schedule(&dev
->bm_tasklet
);
414 icr
&= ~VMCI_ICR_NOTIFICATION
;
417 if (icr
& VMCI_ICR_DMA_DATAGRAM
)
418 icr
&= ~VMCI_ICR_DMA_DATAGRAM
;
422 "Ignoring unknown interrupt cause (%d)\n",
430 * Interrupt handler for MSI-X interrupt vector VMCI_INTR_NOTIFICATION,
431 * which is for the notification bitmap. Will only get called if we are
432 * using MSI-X with exclusive vectors.
434 static irqreturn_t
vmci_interrupt_bm(int irq
, void *_dev
)
436 struct vmci_guest_device
*dev
= _dev
;
438 /* For MSI-X we can just assume it was meant for us. */
439 tasklet_schedule(&dev
->bm_tasklet
);
445 * Interrupt handler for MSI-X interrupt vector VMCI_INTR_DMA_DATAGRAM,
446 * which is for the completion of a DMA datagram send or receive operation.
447 * Will only get called if we are using MSI-X with exclusive vectors.
449 static irqreturn_t
vmci_interrupt_dma_datagram(int irq
, void *_dev
)
455 * Most of the initialization at module load time is done here.
457 static int vmci_guest_probe_device(struct pci_dev
*pdev
,
458 const struct pci_device_id
*id
)
460 struct vmci_guest_device
*vmci_dev
;
461 void __iomem
*iobase
= NULL
;
462 void __iomem
*mmio_base
= NULL
;
463 unsigned int num_irq_vectors
;
464 unsigned int capabilities
;
465 unsigned int caps_in_use
;
470 dev_dbg(&pdev
->dev
, "Probing for vmci/PCI guest device\n");
472 error
= pcim_enable_device(pdev
);
475 "Failed to enable VMCI device: %d\n", error
);
480 * The VMCI device with mmio access to registers requests 256KB
481 * for BAR1. If present, driver will use new VMCI device
482 * functionality for register access and datagram send/recv.
485 if (pci_resource_len(pdev
, 1) == VMCI_WITH_MMIO_ACCESS_BAR_SIZE
) {
486 dev_info(&pdev
->dev
, "MMIO register access is available\n");
487 mmio_base
= pci_iomap_range(pdev
, 1, VMCI_MMIO_ACCESS_OFFSET
,
488 VMCI_MMIO_ACCESS_SIZE
);
489 /* If the map fails, we fall back to IOIO access. */
491 dev_warn(&pdev
->dev
, "Failed to map MMIO register access\n");
495 error
= pcim_iomap_regions(pdev
, BIT(0), KBUILD_MODNAME
);
497 dev_err(&pdev
->dev
, "Failed to reserve/map IO regions\n");
500 iobase
= pcim_iomap_table(pdev
)[0];
503 vmci_dev
= devm_kzalloc(&pdev
->dev
, sizeof(*vmci_dev
), GFP_KERNEL
);
506 "Can't allocate memory for VMCI device\n");
510 vmci_dev
->dev
= &pdev
->dev
;
511 vmci_dev
->exclusive_vectors
= false;
512 vmci_dev
->iobase
= iobase
;
513 vmci_dev
->mmio_base
= mmio_base
;
515 tasklet_init(&vmci_dev
->datagram_tasklet
,
516 vmci_dispatch_dgs
, (unsigned long)vmci_dev
);
517 tasklet_init(&vmci_dev
->bm_tasklet
,
518 vmci_process_bitmap
, (unsigned long)vmci_dev
);
520 vmci_dev
->data_buffer
= vmalloc(VMCI_MAX_DG_SIZE
);
521 if (!vmci_dev
->data_buffer
) {
523 "Can't allocate memory for datagram buffer\n");
527 pci_set_master(pdev
); /* To enable queue_pair functionality. */
530 * Verify that the VMCI Device supports the capabilities that
531 * we need. If the device is missing capabilities that we would
532 * like to use, check for fallback capabilities and use those
533 * instead (so we can run a new VM on old hosts). Fail the load if
534 * a required capability is missing and there is no fallback.
536 * Right now, we need datagrams. There are no fallbacks.
538 capabilities
= vmci_read_reg(vmci_dev
, VMCI_CAPS_ADDR
);
539 if (!(capabilities
& VMCI_CAPS_DATAGRAM
)) {
540 dev_err(&pdev
->dev
, "Device does not support datagrams\n");
542 goto err_free_data_buffer
;
544 caps_in_use
= VMCI_CAPS_DATAGRAM
;
547 * Use 64-bit PPNs if the device supports.
549 * There is no check for the return value of dma_set_mask_and_coherent
550 * since this driver can handle the default mask values if
551 * dma_set_mask_and_coherent fails.
553 if (capabilities
& VMCI_CAPS_PPN64
) {
554 dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(64));
556 caps_in_use
|= VMCI_CAPS_PPN64
;
558 dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(44));
563 * If the hardware supports notifications, we will use that as
566 if (capabilities
& VMCI_CAPS_NOTIFICATIONS
) {
567 vmci_dev
->notification_bitmap
= dma_alloc_coherent(
568 &pdev
->dev
, PAGE_SIZE
, &vmci_dev
->notification_base
,
570 if (!vmci_dev
->notification_bitmap
) {
572 "Unable to allocate notification bitmap\n");
574 memset(vmci_dev
->notification_bitmap
, 0, PAGE_SIZE
);
575 caps_in_use
|= VMCI_CAPS_NOTIFICATIONS
;
579 if (mmio_base
!= NULL
) {
580 if (capabilities
& VMCI_CAPS_DMA_DATAGRAM
) {
581 caps_in_use
|= VMCI_CAPS_DMA_DATAGRAM
;
584 "Missing capability: VMCI_CAPS_DMA_DATAGRAM\n");
586 goto err_free_data_buffer
;
590 dev_info(&pdev
->dev
, "Using capabilities 0x%x\n", caps_in_use
);
592 /* Let the host know which capabilities we intend to use. */
593 vmci_write_reg(vmci_dev
, caps_in_use
, VMCI_CAPS_ADDR
);
595 /* Let the device know the size for pages passed down. */
596 if (caps_in_use
& VMCI_CAPS_DMA_DATAGRAM
)
597 vmci_write_reg(vmci_dev
, PAGE_SHIFT
, VMCI_GUEST_PAGE_SHIFT
);
599 /* Set up global device so that we can start sending datagrams */
600 spin_lock_irq(&vmci_dev_spinlock
);
601 vmci_dev_g
= vmci_dev
;
603 spin_unlock_irq(&vmci_dev_spinlock
);
606 * Register notification bitmap with device if that capability is
609 if (caps_in_use
& VMCI_CAPS_NOTIFICATIONS
) {
610 unsigned long bitmap_ppn
=
611 vmci_dev
->notification_base
>> PAGE_SHIFT
;
612 if (!vmci_dbell_register_notification_bitmap(bitmap_ppn
)) {
614 "VMCI device unable to register notification bitmap with PPN 0x%lx\n",
617 goto err_remove_vmci_dev_g
;
621 /* Check host capabilities. */
622 error
= vmci_check_host_caps(pdev
);
624 goto err_remove_bitmap
;
629 * We subscribe to the VMCI_EVENT_CTX_ID_UPDATE here so we can
630 * update the internal context id when needed.
632 vmci_err
= vmci_event_subscribe(VMCI_EVENT_CTX_ID_UPDATE
,
633 vmci_guest_cid_update
, NULL
,
635 if (vmci_err
< VMCI_SUCCESS
)
637 "Failed to subscribe to event (type=%d): %d\n",
638 VMCI_EVENT_CTX_ID_UPDATE
, vmci_err
);
641 * Enable interrupts. Try MSI-X first, then MSI, and then fallback on
644 if (vmci_dev
->mmio_base
!= NULL
)
645 num_irq_vectors
= VMCI_MAX_INTRS
;
647 num_irq_vectors
= VMCI_MAX_INTRS_NOTIFICATION
;
648 error
= pci_alloc_irq_vectors(pdev
, num_irq_vectors
, num_irq_vectors
,
651 error
= pci_alloc_irq_vectors(pdev
, 1, 1,
652 PCI_IRQ_MSIX
| PCI_IRQ_MSI
| PCI_IRQ_LEGACY
);
654 goto err_remove_bitmap
;
656 vmci_dev
->exclusive_vectors
= true;
660 * Request IRQ for legacy or MSI interrupts, or for first
663 error
= request_irq(pci_irq_vector(pdev
, 0), vmci_interrupt
,
664 IRQF_SHARED
, KBUILD_MODNAME
, vmci_dev
);
666 dev_err(&pdev
->dev
, "Irq %u in use: %d\n",
667 pci_irq_vector(pdev
, 0), error
);
668 goto err_disable_msi
;
672 * For MSI-X with exclusive vectors we need to request an
673 * interrupt for each vector so that we get a separate
674 * interrupt handler routine. This allows us to distinguish
675 * between the vectors.
677 if (vmci_dev
->exclusive_vectors
) {
678 error
= request_irq(pci_irq_vector(pdev
, 1),
679 vmci_interrupt_bm
, 0, KBUILD_MODNAME
,
683 "Failed to allocate irq %u: %d\n",
684 pci_irq_vector(pdev
, 1), error
);
687 if (caps_in_use
& VMCI_CAPS_DMA_DATAGRAM
) {
688 error
= request_irq(pci_irq_vector(pdev
, 2),
689 vmci_interrupt_dma_datagram
,
690 0, KBUILD_MODNAME
, vmci_dev
);
693 "Failed to allocate irq %u: %d\n",
694 pci_irq_vector(pdev
, 2), error
);
695 goto err_free_bm_irq
;
700 dev_dbg(&pdev
->dev
, "Registered device\n");
702 atomic_inc(&vmci_num_guest_devices
);
704 /* Enable specific interrupt bits. */
705 cmd
= VMCI_IMR_DATAGRAM
;
706 if (caps_in_use
& VMCI_CAPS_NOTIFICATIONS
)
707 cmd
|= VMCI_IMR_NOTIFICATION
;
708 if (caps_in_use
& VMCI_CAPS_DMA_DATAGRAM
)
709 cmd
|= VMCI_IMR_DMA_DATAGRAM
;
710 vmci_write_reg(vmci_dev
, cmd
, VMCI_IMR_ADDR
);
712 /* Enable interrupts. */
713 vmci_write_reg(vmci_dev
, VMCI_CONTROL_INT_ENABLE
, VMCI_CONTROL_ADDR
);
715 pci_set_drvdata(pdev
, vmci_dev
);
717 vmci_call_vsock_callback(false);
721 free_irq(pci_irq_vector(pdev
, 1), vmci_dev
);
723 free_irq(pci_irq_vector(pdev
, 0), vmci_dev
);
724 tasklet_kill(&vmci_dev
->datagram_tasklet
);
725 tasklet_kill(&vmci_dev
->bm_tasklet
);
728 pci_free_irq_vectors(pdev
);
730 vmci_err
= vmci_event_unsubscribe(ctx_update_sub_id
);
731 if (vmci_err
< VMCI_SUCCESS
)
733 "Failed to unsubscribe from event (type=%d) with subscriber (ID=0x%x): %d\n",
734 VMCI_EVENT_CTX_ID_UPDATE
, ctx_update_sub_id
, vmci_err
);
737 if (vmci_dev
->notification_bitmap
) {
738 vmci_write_reg(vmci_dev
, VMCI_CONTROL_RESET
, VMCI_CONTROL_ADDR
);
739 dma_free_coherent(&pdev
->dev
, PAGE_SIZE
,
740 vmci_dev
->notification_bitmap
,
741 vmci_dev
->notification_base
);
744 err_remove_vmci_dev_g
:
745 spin_lock_irq(&vmci_dev_spinlock
);
748 spin_unlock_irq(&vmci_dev_spinlock
);
750 err_free_data_buffer
:
751 vfree(vmci_dev
->data_buffer
);
753 /* The rest are managed resources and will be freed by PCI core */
757 static void vmci_guest_remove_device(struct pci_dev
*pdev
)
759 struct vmci_guest_device
*vmci_dev
= pci_get_drvdata(pdev
);
762 dev_dbg(&pdev
->dev
, "Removing device\n");
764 atomic_dec(&vmci_num_guest_devices
);
766 vmci_qp_guest_endpoints_exit();
768 vmci_err
= vmci_event_unsubscribe(ctx_update_sub_id
);
769 if (vmci_err
< VMCI_SUCCESS
)
771 "Failed to unsubscribe from event (type=%d) with subscriber (ID=0x%x): %d\n",
772 VMCI_EVENT_CTX_ID_UPDATE
, ctx_update_sub_id
, vmci_err
);
774 spin_lock_irq(&vmci_dev_spinlock
);
777 spin_unlock_irq(&vmci_dev_spinlock
);
779 dev_dbg(&pdev
->dev
, "Resetting vmci device\n");
780 vmci_write_reg(vmci_dev
, VMCI_CONTROL_RESET
, VMCI_CONTROL_ADDR
);
783 * Free IRQ and then disable MSI/MSI-X as appropriate. For
784 * MSI-X, we might have multiple vectors, each with their own
785 * IRQ, which we must free too.
787 if (vmci_dev
->exclusive_vectors
) {
788 free_irq(pci_irq_vector(pdev
, 1), vmci_dev
);
789 if (vmci_dev
->mmio_base
!= NULL
)
790 free_irq(pci_irq_vector(pdev
, 2), vmci_dev
);
792 free_irq(pci_irq_vector(pdev
, 0), vmci_dev
);
793 pci_free_irq_vectors(pdev
);
795 tasklet_kill(&vmci_dev
->datagram_tasklet
);
796 tasklet_kill(&vmci_dev
->bm_tasklet
);
798 if (vmci_dev
->notification_bitmap
) {
800 * The device reset above cleared the bitmap state of the
801 * device, so we can safely free it here.
804 dma_free_coherent(&pdev
->dev
, PAGE_SIZE
,
805 vmci_dev
->notification_bitmap
,
806 vmci_dev
->notification_base
);
809 vfree(vmci_dev
->data_buffer
);
811 /* The rest are managed resources and will be freed by PCI core */
814 static const struct pci_device_id vmci_ids
[] = {
815 { PCI_DEVICE(PCI_VENDOR_ID_VMWARE
, PCI_DEVICE_ID_VMWARE_VMCI
), },
818 MODULE_DEVICE_TABLE(pci
, vmci_ids
);
820 static struct pci_driver vmci_guest_driver
= {
821 .name
= KBUILD_MODNAME
,
822 .id_table
= vmci_ids
,
823 .probe
= vmci_guest_probe_device
,
824 .remove
= vmci_guest_remove_device
,
827 int __init
vmci_guest_init(void)
829 return pci_register_driver(&vmci_guest_driver
);
832 void __exit
vmci_guest_exit(void)
834 pci_unregister_driver(&vmci_guest_driver
);