Merge tag 'pci-v5.18-changes-2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / drivers / misc / vmw_vmci / vmci_guest.c
index 1018dc77269d47b3079b0c9be6426295b5489fc5..57a6157209a19f039cc87bbd6a0870ea92b24b46 100644 (file)
@@ -13,6 +13,7 @@
 #include <linux/kernel.h>
 #include <linux/mm.h>
 #include <linux/module.h>
+#include <linux/processor.h>
 #include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/init.h>
 
 #define VMCI_UTIL_NUM_RESOURCES 1
 
+/*
+ * Datagram buffers for DMA send/receive must accommodate at least
+ * a maximum sized datagram and the header.
+ */
+#define VMCI_DMA_DG_BUFFER_SIZE (VMCI_MAX_DG_SIZE + PAGE_SIZE)
+
 static bool vmci_disable_msi;
 module_param_named(disable_msi, vmci_disable_msi, bool, 0);
 MODULE_PARM_DESC(disable_msi, "Disable MSI use in driver - (default=0)");
@@ -45,13 +52,18 @@ static u32 vm_context_id = VMCI_INVALID_ID;
 struct vmci_guest_device {
        struct device *dev;     /* PCI device we are attached to */
        void __iomem *iobase;
+       void __iomem *mmio_base;
 
        bool exclusive_vectors;
 
        struct tasklet_struct datagram_tasklet;
        struct tasklet_struct bm_tasklet;
+       struct wait_queue_head inout_wq;
 
        void *data_buffer;
+       dma_addr_t data_buffer_base;
+       void *tx_buffer;
+       dma_addr_t tx_buffer_base;
        void *notification_bitmap;
        dma_addr_t notification_base;
 };
@@ -89,6 +101,92 @@ u32 vmci_get_vm_context_id(void)
        return vm_context_id;
 }
 
+static unsigned int vmci_read_reg(struct vmci_guest_device *dev, u32 reg)
+{
+       if (dev->mmio_base != NULL)
+               return readl(dev->mmio_base + reg);
+       return ioread32(dev->iobase + reg);
+}
+
+static void vmci_write_reg(struct vmci_guest_device *dev, u32 val, u32 reg)
+{
+       if (dev->mmio_base != NULL)
+               writel(val, dev->mmio_base + reg);
+       else
+               iowrite32(val, dev->iobase + reg);
+}
+
+static void vmci_read_data(struct vmci_guest_device *vmci_dev,
+                          void *dest, size_t size)
+{
+       if (vmci_dev->mmio_base == NULL)
+               ioread8_rep(vmci_dev->iobase + VMCI_DATA_IN_ADDR,
+                           dest, size);
+       else {
+               /*
+                * For DMA datagrams, the data_buffer will contain the header on the
+                * first page, followed by the incoming datagram(s) on the following
+                * pages. The header uses an S/G element immediately following the
+                * header on the first page to point to the data area.
+                */
+               struct vmci_data_in_out_header *buffer_header = vmci_dev->data_buffer;
+               struct vmci_sg_elem *sg_array = (struct vmci_sg_elem *)(buffer_header + 1);
+               size_t buffer_offset = dest - vmci_dev->data_buffer;
+
+               buffer_header->opcode = 1;
+               buffer_header->size = 1;
+               buffer_header->busy = 0;
+               sg_array[0].addr = vmci_dev->data_buffer_base + buffer_offset;
+               sg_array[0].size = size;
+
+               vmci_write_reg(vmci_dev, lower_32_bits(vmci_dev->data_buffer_base),
+                              VMCI_DATA_IN_LOW_ADDR);
+
+               wait_event(vmci_dev->inout_wq, buffer_header->busy == 1);
+       }
+}
+
+static int vmci_write_data(struct vmci_guest_device *dev,
+                          struct vmci_datagram *dg)
+{
+       int result;
+
+       if (dev->mmio_base != NULL) {
+               struct vmci_data_in_out_header *buffer_header = dev->tx_buffer;
+               u8 *dg_out_buffer = (u8 *)(buffer_header + 1);
+
+               if (VMCI_DG_SIZE(dg) > VMCI_MAX_DG_SIZE)
+                       return VMCI_ERROR_INVALID_ARGS;
+
+               /*
+                * Initialize send buffer with outgoing datagram
+                * and set up header for inline data. Device will
+                * not access buffer asynchronously - only after
+                * the write to VMCI_DATA_OUT_LOW_ADDR.
+                */
+               memcpy(dg_out_buffer, dg, VMCI_DG_SIZE(dg));
+               buffer_header->opcode = 0;
+               buffer_header->size = VMCI_DG_SIZE(dg);
+               buffer_header->busy = 1;
+
+               vmci_write_reg(dev, lower_32_bits(dev->tx_buffer_base),
+                              VMCI_DATA_OUT_LOW_ADDR);
+
+               /* Caller holds a spinlock, so cannot block. */
+               spin_until_cond(buffer_header->busy == 0);
+
+               result = vmci_read_reg(vmci_dev_g, VMCI_RESULT_LOW_ADDR);
+               if (result == VMCI_SUCCESS)
+                       result = (int)buffer_header->result;
+       } else {
+               iowrite8_rep(dev->iobase + VMCI_DATA_OUT_ADDR,
+                            dg, VMCI_DG_SIZE(dg));
+               result = vmci_read_reg(vmci_dev_g, VMCI_RESULT_LOW_ADDR);
+       }
+
+       return result;
+}
+
 /*
  * VM to hypervisor call mechanism. We use the standard VMware naming
  * convention since shared code is calling this function as well.
@@ -114,9 +212,8 @@ int vmci_send_datagram(struct vmci_datagram *dg)
        spin_lock_irqsave(&vmci_dev_spinlock, flags);
 
        if (vmci_dev_g) {
-               iowrite8_rep(vmci_dev_g->iobase + VMCI_DATA_OUT_ADDR,
-                            dg, VMCI_DG_SIZE(dg));
-               result = ioread32(vmci_dev_g->iobase + VMCI_RESULT_LOW_ADDR);
+               vmci_write_data(vmci_dev_g, dg);
+               result = vmci_read_reg(vmci_dev_g, VMCI_RESULT_LOW_ADDR);
        } else {
                result = VMCI_ERROR_UNAVAILABLE;
        }
@@ -156,9 +253,9 @@ static void vmci_guest_cid_update(u32 sub_id,
 
 /*
  * Verify that the host supports the hypercalls we need. If it does not,
- * try to find fallback hypercalls and use those instead.  Returns
- * true if required hypercalls (or fallback hypercalls) are
- * supported by the host, false otherwise.
+ * try to find fallback hypercalls and use those instead.  Returns 0 if
+ * required hypercalls (or fallback hypercalls) are supported by the host,
+ * an error code otherwise.
  */
 static int vmci_check_host_caps(struct pci_dev *pdev)
 {
@@ -195,15 +292,17 @@ static int vmci_check_host_caps(struct pci_dev *pdev)
 }
 
 /*
- * Reads datagrams from the data in port and dispatches them. We
- * always start reading datagrams into only the first page of the
- * datagram buffer. If the datagrams don't fit into one page, we
- * use the maximum datagram buffer size for the remainder of the
- * invocation. This is a simple heuristic for not penalizing
- * small datagrams.
+ * Reads datagrams from the device and dispatches them. For IO port
+ * based access to the device, we always start reading datagrams into
+ * only the first page of the datagram buffer. If the datagrams don't
+ * fit into one page, we use the maximum datagram buffer size for the
+ * remainder of the invocation. This is a simple heuristic for not
+ * penalizing small datagrams. For DMA-based datagrams, we always
+ * use the maximum datagram buffer size, since there is no performance
+ * penalty for doing so.
  *
  * This function assumes that it has exclusive access to the data
- * in port for the duration of the call.
+ * in register(s) for the duration of the call.
  */
 static void vmci_dispatch_dgs(unsigned long data)
 {
@@ -211,23 +310,41 @@ static void vmci_dispatch_dgs(unsigned long data)
        u8 *dg_in_buffer = vmci_dev->data_buffer;
        struct vmci_datagram *dg;
        size_t dg_in_buffer_size = VMCI_MAX_DG_SIZE;
-       size_t current_dg_in_buffer_size = PAGE_SIZE;
+       size_t current_dg_in_buffer_size;
        size_t remaining_bytes;
+       bool is_io_port = vmci_dev->mmio_base == NULL;
 
        BUILD_BUG_ON(VMCI_MAX_DG_SIZE < PAGE_SIZE);
 
-       ioread8_rep(vmci_dev->iobase + VMCI_DATA_IN_ADDR,
-                   vmci_dev->data_buffer, current_dg_in_buffer_size);
+       if (!is_io_port) {
+               /* For mmio, the first page is used for the header. */
+               dg_in_buffer += PAGE_SIZE;
+
+               /*
+                * For DMA-based datagram operations, there is no performance
+                * penalty for reading the maximum buffer size.
+                */
+               current_dg_in_buffer_size = VMCI_MAX_DG_SIZE;
+       } else {
+               current_dg_in_buffer_size = PAGE_SIZE;
+       }
+       vmci_read_data(vmci_dev, dg_in_buffer, current_dg_in_buffer_size);
        dg = (struct vmci_datagram *)dg_in_buffer;
        remaining_bytes = current_dg_in_buffer_size;
 
+       /*
+        * Read through the buffer until an invalid datagram header is
+        * encountered. The exit condition for datagrams read through
+        * VMCI_DATA_IN_ADDR is a bit more complicated, since a datagram
+        * can start on any page boundary in the buffer.
+        */
        while (dg->dst.resource != VMCI_INVALID_ID ||
-              remaining_bytes > PAGE_SIZE) {
+              (is_io_port && remaining_bytes > PAGE_SIZE)) {
                unsigned dg_in_size;
 
                /*
-                * When the input buffer spans multiple pages, a datagram can
-                * start on any page boundary in the buffer.
+                * If using VMCI_DATA_IN_ADDR, skip to the next page
+                * as a datagram can start on any page boundary.
                 */
                if (dg->dst.resource == VMCI_INVALID_ID) {
                        dg = (struct vmci_datagram *)roundup(
@@ -277,11 +394,10 @@ static void vmci_dispatch_dgs(unsigned long data)
                                        current_dg_in_buffer_size =
                                            dg_in_buffer_size;
 
-                               ioread8_rep(vmci_dev->iobase +
-                                               VMCI_DATA_IN_ADDR,
-                                       vmci_dev->data_buffer +
+                               vmci_read_data(vmci_dev,
+                                              dg_in_buffer +
                                                remaining_bytes,
-                                       current_dg_in_buffer_size -
+                                              current_dg_in_buffer_size -
                                                remaining_bytes);
                        }
 
@@ -319,10 +435,8 @@ static void vmci_dispatch_dgs(unsigned long data)
                                current_dg_in_buffer_size = dg_in_buffer_size;
 
                        for (;;) {
-                               ioread8_rep(vmci_dev->iobase +
-                                               VMCI_DATA_IN_ADDR,
-                                       vmci_dev->data_buffer,
-                                       current_dg_in_buffer_size);
+                               vmci_read_data(vmci_dev, dg_in_buffer,
+                                              current_dg_in_buffer_size);
                                if (bytes_to_skip <= current_dg_in_buffer_size)
                                        break;
 
@@ -339,8 +453,7 @@ static void vmci_dispatch_dgs(unsigned long data)
                if (remaining_bytes < VMCI_DG_HEADERSIZE) {
                        /* Get the next batch of datagrams. */
 
-                       ioread8_rep(vmci_dev->iobase + VMCI_DATA_IN_ADDR,
-                                   vmci_dev->data_buffer,
+                       vmci_read_data(vmci_dev, dg_in_buffer,
                                    current_dg_in_buffer_size);
                        dg = (struct vmci_datagram *)dg_in_buffer;
                        remaining_bytes = current_dg_in_buffer_size;
@@ -384,7 +497,7 @@ static irqreturn_t vmci_interrupt(int irq, void *_dev)
                unsigned int icr;
 
                /* Acknowledge interrupt and determine what needs doing. */
-               icr = ioread32(dev->iobase + VMCI_ICR_ADDR);
+               icr = vmci_read_reg(dev, VMCI_ICR_ADDR);
                if (icr == 0 || icr == ~0)
                        return IRQ_NONE;
 
@@ -398,6 +511,12 @@ static irqreturn_t vmci_interrupt(int irq, void *_dev)
                        icr &= ~VMCI_ICR_NOTIFICATION;
                }
 
+
+               if (icr & VMCI_ICR_DMA_DATAGRAM) {
+                       wake_up_all(&dev->inout_wq);
+                       icr &= ~VMCI_ICR_DMA_DATAGRAM;
+               }
+
                if (icr != 0)
                        dev_warn(dev->dev,
                                 "Ignoring unknown interrupt cause (%d)\n",
@@ -422,6 +541,38 @@ static irqreturn_t vmci_interrupt_bm(int irq, void *_dev)
        return IRQ_HANDLED;
 }
 
+/*
+ * Interrupt handler for MSI-X interrupt vector VMCI_INTR_DMA_DATAGRAM,
+ * which is for the completion of a DMA datagram send or receive operation.
+ * Will only get called if we are using MSI-X with exclusive vectors.
+ */
+static irqreturn_t vmci_interrupt_dma_datagram(int irq, void *_dev)
+{
+       struct vmci_guest_device *dev = _dev;
+
+       wake_up_all(&dev->inout_wq);
+
+       return IRQ_HANDLED;
+}
+
+static void vmci_free_dg_buffers(struct vmci_guest_device *vmci_dev)
+{
+       if (vmci_dev->mmio_base != NULL) {
+               if (vmci_dev->tx_buffer != NULL)
+                       dma_free_coherent(vmci_dev->dev,
+                                         VMCI_DMA_DG_BUFFER_SIZE,
+                                         vmci_dev->tx_buffer,
+                                         vmci_dev->tx_buffer_base);
+               if (vmci_dev->data_buffer != NULL)
+                       dma_free_coherent(vmci_dev->dev,
+                                         VMCI_DMA_DG_BUFFER_SIZE,
+                                         vmci_dev->data_buffer,
+                                         vmci_dev->data_buffer_base);
+       } else {
+               vfree(vmci_dev->data_buffer);
+       }
+}
+
 /*
  * Most of the initialization at module load time is done here.
  */
@@ -429,7 +580,9 @@ static int vmci_guest_probe_device(struct pci_dev *pdev,
                                   const struct pci_device_id *id)
 {
        struct vmci_guest_device *vmci_dev;
-       void __iomem *iobase;
+       void __iomem *iobase = NULL;
+       void __iomem *mmio_base = NULL;
+       unsigned int num_irq_vectors;
        unsigned int capabilities;
        unsigned int caps_in_use;
        unsigned long cmd;
@@ -445,16 +598,29 @@ static int vmci_guest_probe_device(struct pci_dev *pdev,
                return error;
        }
 
-       error = pcim_iomap_regions(pdev, 1 << 0, KBUILD_MODNAME);
-       if (error) {
-               dev_err(&pdev->dev, "Failed to reserve/map IO regions\n");
-               return error;
-       }
+       /*
+        * The VMCI device with mmio access to registers requests 256KB
+        * for BAR1. If present, driver will use new VMCI device
+        * functionality for register access and datagram send/recv.
+        */
 
-       iobase = pcim_iomap_table(pdev)[0];
+       if (pci_resource_len(pdev, 1) == VMCI_WITH_MMIO_ACCESS_BAR_SIZE) {
+               dev_info(&pdev->dev, "MMIO register access is available\n");
+               mmio_base = pci_iomap_range(pdev, 1, VMCI_MMIO_ACCESS_OFFSET,
+                                           VMCI_MMIO_ACCESS_SIZE);
+               /* If the map fails, we fall back to IOIO access. */
+               if (!mmio_base)
+                       dev_warn(&pdev->dev, "Failed to map MMIO register access\n");
+       }
 
-       dev_info(&pdev->dev, "Found VMCI PCI device at %#lx, irq %u\n",
-                (unsigned long)iobase, pdev->irq);
+       if (!mmio_base) {
+               error = pcim_iomap_regions(pdev, BIT(0), KBUILD_MODNAME);
+               if (error) {
+                       dev_err(&pdev->dev, "Failed to reserve/map IO regions\n");
+                       return error;
+               }
+               iobase = pcim_iomap_table(pdev)[0];
+       }
 
        vmci_dev = devm_kzalloc(&pdev->dev, sizeof(*vmci_dev), GFP_KERNEL);
        if (!vmci_dev) {
@@ -466,17 +632,35 @@ static int vmci_guest_probe_device(struct pci_dev *pdev,
        vmci_dev->dev = &pdev->dev;
        vmci_dev->exclusive_vectors = false;
        vmci_dev->iobase = iobase;
+       vmci_dev->mmio_base = mmio_base;
 
        tasklet_init(&vmci_dev->datagram_tasklet,
                     vmci_dispatch_dgs, (unsigned long)vmci_dev);
        tasklet_init(&vmci_dev->bm_tasklet,
                     vmci_process_bitmap, (unsigned long)vmci_dev);
+       init_waitqueue_head(&vmci_dev->inout_wq);
 
-       vmci_dev->data_buffer = vmalloc(VMCI_MAX_DG_SIZE);
+       if (mmio_base != NULL) {
+               vmci_dev->tx_buffer = dma_alloc_coherent(&pdev->dev, VMCI_DMA_DG_BUFFER_SIZE,
+                                                        &vmci_dev->tx_buffer_base,
+                                                        GFP_KERNEL);
+               if (!vmci_dev->tx_buffer) {
+                       dev_err(&pdev->dev,
+                               "Can't allocate memory for datagram tx buffer\n");
+                       return -ENOMEM;
+               }
+
+               vmci_dev->data_buffer = dma_alloc_coherent(&pdev->dev, VMCI_DMA_DG_BUFFER_SIZE,
+                                                          &vmci_dev->data_buffer_base,
+                                                          GFP_KERNEL);
+       } else {
+               vmci_dev->data_buffer = vmalloc(VMCI_MAX_DG_SIZE);
+       }
        if (!vmci_dev->data_buffer) {
                dev_err(&pdev->dev,
                        "Can't allocate memory for datagram buffer\n");
-               return -ENOMEM;
+               error = -ENOMEM;
+               goto err_free_data_buffers;
        }
 
        pci_set_master(pdev);   /* To enable queue_pair functionality. */
@@ -490,11 +674,11 @@ static int vmci_guest_probe_device(struct pci_dev *pdev,
         *
         * Right now, we need datagrams. There are no fallbacks.
         */
-       capabilities = ioread32(vmci_dev->iobase + VMCI_CAPS_ADDR);
+       capabilities = vmci_read_reg(vmci_dev, VMCI_CAPS_ADDR);
        if (!(capabilities & VMCI_CAPS_DATAGRAM)) {
                dev_err(&pdev->dev, "Device does not support datagrams\n");
                error = -ENXIO;
-               goto err_free_data_buffer;
+               goto err_free_data_buffers;
        }
        caps_in_use = VMCI_CAPS_DATAGRAM;
 
@@ -522,19 +706,39 @@ static int vmci_guest_probe_device(struct pci_dev *pdev,
                vmci_dev->notification_bitmap = dma_alloc_coherent(
                        &pdev->dev, PAGE_SIZE, &vmci_dev->notification_base,
                        GFP_KERNEL);
-               if (!vmci_dev->notification_bitmap) {
+               if (!vmci_dev->notification_bitmap)
                        dev_warn(&pdev->dev,
                                 "Unable to allocate notification bitmap\n");
-               } else {
-                       memset(vmci_dev->notification_bitmap, 0, PAGE_SIZE);
+               else
                        caps_in_use |= VMCI_CAPS_NOTIFICATIONS;
+       }
+
+       if (mmio_base != NULL) {
+               if (capabilities & VMCI_CAPS_DMA_DATAGRAM) {
+                       caps_in_use |= VMCI_CAPS_DMA_DATAGRAM;
+               } else {
+                       dev_err(&pdev->dev,
+                               "Missing capability: VMCI_CAPS_DMA_DATAGRAM\n");
+                       error = -ENXIO;
+                       goto err_free_notification_bitmap;
                }
        }
 
        dev_info(&pdev->dev, "Using capabilities 0x%x\n", caps_in_use);
 
        /* Let the host know which capabilities we intend to use. */
-       iowrite32(caps_in_use, vmci_dev->iobase + VMCI_CAPS_ADDR);
+       vmci_write_reg(vmci_dev, caps_in_use, VMCI_CAPS_ADDR);
+
+       if (caps_in_use & VMCI_CAPS_DMA_DATAGRAM) {
+               /* Let the device know the size for pages passed down. */
+               vmci_write_reg(vmci_dev, PAGE_SHIFT, VMCI_GUEST_PAGE_SHIFT);
+
+               /* Configure the high order parts of the data in/out buffers. */
+               vmci_write_reg(vmci_dev, upper_32_bits(vmci_dev->data_buffer_base),
+                              VMCI_DATA_IN_HIGH_ADDR);
+               vmci_write_reg(vmci_dev, upper_32_bits(vmci_dev->tx_buffer_base),
+                              VMCI_DATA_OUT_HIGH_ADDR);
+       }
 
        /* Set up global device so that we can start sending datagrams */
        spin_lock_irq(&vmci_dev_spinlock);
@@ -561,7 +765,7 @@ static int vmci_guest_probe_device(struct pci_dev *pdev,
        /* Check host capabilities. */
        error = vmci_check_host_caps(pdev);
        if (error)
-               goto err_remove_bitmap;
+               goto err_remove_vmci_dev_g;
 
        /* Enable device. */
 
@@ -581,13 +785,17 @@ static int vmci_guest_probe_device(struct pci_dev *pdev,
         * Enable interrupts.  Try MSI-X first, then MSI, and then fallback on
         * legacy interrupts.
         */
-       error = pci_alloc_irq_vectors(pdev, VMCI_MAX_INTRS, VMCI_MAX_INTRS,
-                       PCI_IRQ_MSIX);
+       if (vmci_dev->mmio_base != NULL)
+               num_irq_vectors = VMCI_MAX_INTRS;
+       else
+               num_irq_vectors = VMCI_MAX_INTRS_NOTIFICATION;
+       error = pci_alloc_irq_vectors(pdev, num_irq_vectors, num_irq_vectors,
+                                     PCI_IRQ_MSIX);
        if (error < 0) {
                error = pci_alloc_irq_vectors(pdev, 1, 1,
                                PCI_IRQ_MSIX | PCI_IRQ_MSI | PCI_IRQ_LEGACY);
                if (error < 0)
-                       goto err_remove_bitmap;
+                       goto err_unsubscribe_event;
        } else {
                vmci_dev->exclusive_vectors = true;
        }
@@ -620,6 +828,17 @@ static int vmci_guest_probe_device(struct pci_dev *pdev,
                                pci_irq_vector(pdev, 1), error);
                        goto err_free_irq;
                }
+               if (caps_in_use & VMCI_CAPS_DMA_DATAGRAM) {
+                       error = request_irq(pci_irq_vector(pdev, 2),
+                                           vmci_interrupt_dma_datagram,
+                                           0, KBUILD_MODNAME, vmci_dev);
+                       if (error) {
+                               dev_err(&pdev->dev,
+                                       "Failed to allocate irq %u: %d\n",
+                                       pci_irq_vector(pdev, 2), error);
+                               goto err_free_bm_irq;
+                       }
+               }
        }
 
        dev_dbg(&pdev->dev, "Registered device\n");
@@ -630,17 +849,22 @@ static int vmci_guest_probe_device(struct pci_dev *pdev,
        cmd = VMCI_IMR_DATAGRAM;
        if (caps_in_use & VMCI_CAPS_NOTIFICATIONS)
                cmd |= VMCI_IMR_NOTIFICATION;
-       iowrite32(cmd, vmci_dev->iobase + VMCI_IMR_ADDR);
+       if (caps_in_use & VMCI_CAPS_DMA_DATAGRAM)
+               cmd |= VMCI_IMR_DMA_DATAGRAM;
+       vmci_write_reg(vmci_dev, cmd, VMCI_IMR_ADDR);
 
        /* Enable interrupts. */
-       iowrite32(VMCI_CONTROL_INT_ENABLE,
-                 vmci_dev->iobase + VMCI_CONTROL_ADDR);
+       vmci_write_reg(vmci_dev, VMCI_CONTROL_INT_ENABLE, VMCI_CONTROL_ADDR);
 
        pci_set_drvdata(pdev, vmci_dev);
 
        vmci_call_vsock_callback(false);
        return 0;
 
+err_free_bm_irq:
+       if (vmci_dev->exclusive_vectors)
+               free_irq(pci_irq_vector(pdev, 1), vmci_dev);
+
 err_free_irq:
        free_irq(pci_irq_vector(pdev, 0), vmci_dev);
        tasklet_kill(&vmci_dev->datagram_tasklet);
@@ -649,29 +873,29 @@ err_free_irq:
 err_disable_msi:
        pci_free_irq_vectors(pdev);
 
+err_unsubscribe_event:
        vmci_err = vmci_event_unsubscribe(ctx_update_sub_id);
        if (vmci_err < VMCI_SUCCESS)
                dev_warn(&pdev->dev,
                         "Failed to unsubscribe from event (type=%d) with subscriber (ID=0x%x): %d\n",
                         VMCI_EVENT_CTX_ID_UPDATE, ctx_update_sub_id, vmci_err);
 
-err_remove_bitmap:
-       if (vmci_dev->notification_bitmap) {
-               iowrite32(VMCI_CONTROL_RESET,
-                         vmci_dev->iobase + VMCI_CONTROL_ADDR);
-               dma_free_coherent(&pdev->dev, PAGE_SIZE,
-                                 vmci_dev->notification_bitmap,
-                                 vmci_dev->notification_base);
-       }
-
 err_remove_vmci_dev_g:
        spin_lock_irq(&vmci_dev_spinlock);
        vmci_pdev = NULL;
        vmci_dev_g = NULL;
        spin_unlock_irq(&vmci_dev_spinlock);
 
-err_free_data_buffer:
-       vfree(vmci_dev->data_buffer);
+err_free_notification_bitmap:
+       if (vmci_dev->notification_bitmap) {
+               vmci_write_reg(vmci_dev, VMCI_CONTROL_RESET, VMCI_CONTROL_ADDR);
+               dma_free_coherent(&pdev->dev, PAGE_SIZE,
+                                 vmci_dev->notification_bitmap,
+                                 vmci_dev->notification_base);
+       }
+
+err_free_data_buffers:
+       vmci_free_dg_buffers(vmci_dev);
 
        /* The rest are managed resources and will be freed by PCI core */
        return error;
@@ -700,15 +924,18 @@ static void vmci_guest_remove_device(struct pci_dev *pdev)
        spin_unlock_irq(&vmci_dev_spinlock);
 
        dev_dbg(&pdev->dev, "Resetting vmci device\n");
-       iowrite32(VMCI_CONTROL_RESET, vmci_dev->iobase + VMCI_CONTROL_ADDR);
+       vmci_write_reg(vmci_dev, VMCI_CONTROL_RESET, VMCI_CONTROL_ADDR);
 
        /*
         * Free IRQ and then disable MSI/MSI-X as appropriate.  For
         * MSI-X, we might have multiple vectors, each with their own
         * IRQ, which we must free too.
         */
-       if (vmci_dev->exclusive_vectors)
+       if (vmci_dev->exclusive_vectors) {
                free_irq(pci_irq_vector(pdev, 1), vmci_dev);
+               if (vmci_dev->mmio_base != NULL)
+                       free_irq(pci_irq_vector(pdev, 2), vmci_dev);
+       }
        free_irq(pci_irq_vector(pdev, 0), vmci_dev);
        pci_free_irq_vectors(pdev);
 
@@ -726,7 +953,10 @@ static void vmci_guest_remove_device(struct pci_dev *pdev)
                                  vmci_dev->notification_base);
        }
 
-       vfree(vmci_dev->data_buffer);
+       vmci_free_dg_buffers(vmci_dev);
+
+       if (vmci_dev->mmio_base != NULL)
+               pci_iounmap(pdev, vmci_dev->mmio_base);
 
        /* The rest are managed resources and will be freed by PCI core */
 }