From 3fedeb0f9c045c8fb71e4016f28b13df8da32b24 Mon Sep 17 00:00:00 2001
Message-Id: <3fedeb0f9c045c8fb71e4016f28b13df8da32b24.1357660440.git.minovotn@redhat.com>
In-Reply-To: <ffc7768db990f0587b84e08e06095ed26f095fde.1357660440.git.minovotn@redhat.com>
References: <ffc7768db990f0587b84e08e06095ed26f095fde.1357660440.git.minovotn@redhat.com>
From: Alex Williamson <alex.williamson@redhat.com>
Date: Mon, 7 Jan 2013 17:14:24 +0100
Subject: [PATCH 4/4] pci-assign: Enable MSIX on device to match guest

RH-Author: Alex Williamson <alex.williamson@redhat.com>
Message-id: <20130107170918.32146.20266.stgit@bling.home>
Patchwork-id: 45618
O-Subject: [RHEL6.4 qemu-kvm PATCH] pci-assign: Enable MSIX on device to match guest
Bugzilla: 886410
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
RH-Acked-by: Don Dutile <ddutile@redhat.com>
RH-Acked-by: Amos Kong <akong@redhat.com>

Bugzilla: bz886410
Brew: http://brewweb.devel.redhat.com/brew/taskinfo?taskID=5243200
Upstream: Posted & Ack'd by MST

When a guest enables MSIX on a device we evaluate the MSIX vector
table, typically find no unmasked vectors and don't switch the device
to MSIX mode.  This generally works fine and the device will be
switched once the guest enables and therefore unmasks a vector.
Unfortunately some drivers enable MSIX, then use interfaces to send
commands between VF & PF or PF & firmware that act based on the host
state of the device.  These therefore may break when MSIX is managed
lazily.  This change re-enables the previous test used to enable MSIX
(see qemu-kvm a6b402c9), which basically guesses whether a vector
will be used based on the data field of the vector table.

Cc: qemu-stable@nongnu.org
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
Acked-by: Michael S. Tsirkin <mst@redhat.com>
---

Testing: Tested with igbvf & e1000e devices under RHEL6, FreeBSD9,
         and Windows guests.

The equivalent RHEL patch to the referenced commit is bd93301f.

 hw/device-assignment.c |   17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 hw/device-assignment.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/hw/device-assignment.c b/hw/device-assignment.c
index b2540a8..589e8d0 100644
--- a/hw/device-assignment.c
+++ b/hw/device-assignment.c
@@ -1139,6 +1139,19 @@ static bool msix_masked(MSIXTableEntry *entry)
     return (entry->ctrl & cpu_to_le32(0x1)) != 0;
 }
 
+/*
+ * When MSI-X is first enabled the vector table typically has all the
+ * vectors masked, so we can't use that as the obvious test to figure out
+ * how many vectors to initially enable.  Instead we look at the data field
+ * because this is what worked for pci-assign for a long time.  This makes
+ * sure the physical MSI-X state tracks the guest's view, which is important
+ * for some VF/PF and PF/fw communication channels.
+ */
+static bool msix_skipped(MSIXTableEntry *entry)
+{
+    return !entry->data;
+}
+
 static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
 {
     AssignedDevice *adev = container_of(pci_dev, AssignedDevice, dev);
@@ -1150,7 +1163,7 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
 
     /* Get the usable entry number for allocating */
     for (i = 0; i < adev->msix_max; i++, entry++) {
-        if (msix_masked(entry)) {
+        if (msix_skipped(entry)) {
             continue;
         }
         entries_nr++;
@@ -1185,7 +1198,7 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
     msix_entry.assigned_dev_id = msix_nr.assigned_dev_id;
     entry = adev->msix_table;
     for (i = 0; i < adev->msix_max; i++, entry++) {
-        if (msix_masked(entry)) {
+        if (msix_skipped(entry)) {
             continue;
         }
 
-- 
1.7.11.7

