From fe5c9b7f6e268b46e68beb77ce051290ad39c851 Mon Sep 17 00:00:00 2001
From: Alex Williamson <alex.williamson@redhat.com>
Date: Mon, 14 Jun 2010 21:31:04 -0300
Subject: [PATCH 2/2] device-assignment: Cleanup on exit

RH-Author: Alex Williamson <alex.williamson@redhat.com>
Message-id: <20100614213053.9655.97090.stgit@localhost.localdomain>
Patchwork-id: 9910
O-Subject: [RHEL6.0 qemu-kvm PATCH 2/2] device-assignment: Cleanup on exit
Bugzilla: 572043
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
RH-Acked-by: Juan Quintela <quintela@redhat.com>
RH-Acked-by: Marcelo Tosatti <mtosatti@redhat.com>

Bugzilla: 572043
Upstream status: applied, unknown commit id

close() the resource fd when we're done with it.  Unregister and munmap the
anonymous memory for the MSIX table.

Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
---

 hw/device-assignment.c |   21 +++++++++++++++++++++
 1 files changed, 21 insertions(+), 0 deletions(-)

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/device-assignment.c |   21 +++++++++++++++++++++
 1 files changed, 21 insertions(+), 0 deletions(-)

diff --git a/hw/device-assignment.c b/hw/device-assignment.c
index 12ddac3..0180e3d 100644
--- a/hw/device-assignment.c
+++ b/hw/device-assignment.c
@@ -60,6 +60,8 @@
 
 static void assigned_dev_load_option_rom(AssignedDevice *dev);
 
+static void assigned_dev_unregister_msix_mmio(AssignedDevice *dev);
+
 static uint32_t guest_to_host_ioport(AssignedDevRegion *region, uint32_t addr)
 {
     return region->u.r_baseport + (addr - region->e_physbase);
@@ -706,10 +708,14 @@ static void free_assigned_device(AssignedDevice *dev)
                         fprintf(stderr,
 				"Failed to unmap assigned device region: %s\n",
 				strerror(errno));
+                    close(pci_region->resource_fd);
                 }
 	    }
         }
 
+        if (dev->cap.available & ASSIGNED_DEVICE_CAP_MSIX)
+            assigned_dev_unregister_msix_mmio(dev);
+
         if (dev->real_device.config_fd) {
             close(dev->real_device.config_fd);
             dev->real_device.config_fd = 0;
@@ -1242,6 +1248,21 @@ static int assigned_dev_register_msix_mmio(AssignedDevice *dev)
     return 0;
 }
 
+static void assigned_dev_unregister_msix_mmio(AssignedDevice *dev)
+{
+    if (!dev->msix_table_page)
+        return;
+
+    cpu_unregister_io_memory(dev->mmio_index);
+    dev->mmio_index = 0;
+
+    if (munmap(dev->msix_table_page, 0x1000) == -1) {
+        fprintf(stderr, "error unmapping msix_table_page! %s\n",
+                strerror(errno));
+    }
+    dev->msix_table_page = NULL;
+}
+
 static int assigned_initfn(struct PCIDevice *pci_dev)
 {
     AssignedDevice *dev = DO_UPCAST(AssignedDevice, dev, pci_dev);
-- 
1.7.0.3

