From 91ee1603e1e5df75294fa5a49f61b37ef9079148 Mon Sep 17 00:00:00 2001
From: Alex Williamson <alex.williamson@redhat.com>
Date: Tue, 7 Dec 2010 20:50:16 -0200
Subject: [RHEL6 qemu-kvm PATCH 04/18] pci: add API to add capability at a known offset

RH-Author: Alex Williamson <alex.williamson@redhat.com>
Message-id: <20101207205016.4625.33240.stgit@s20.home>
Patchwork-id: 14371
O-Subject: [RHEL6.1 qemu-kvm PATCH 04/18] pci: add API to add capability at a
	known offset
Bugzilla: 624790
RH-Acked-by: Don Dutile <ddutile@redhat.com>
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
RH-Acked-by: Jes Sorensen <Jes.Sorensen@redhat.com>

From: Michael S. Tsirkin <mst@redhat.com>

Upstream commit: 1db5a3aad3c689be1255ed850ef8e3515ab461e8
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=624790
Brew build: http://brewweb.devel.redhat.com/brew/taskinfo?taskID=2948901

Unlike virtio, device emulations need to add pci capabilities
at known offsets to match real hardware. Make this possible
by adding an appropriate API.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---

 hw/pci.c |   17 +++++++++++++----
 hw/pci.h |    2 ++
 2 files changed, 15 insertions(+), 4 deletions(-)

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 hw/pci.c |   17 +++++++++++++----
 hw/pci.h |    2 ++
 2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/hw/pci.c b/hw/pci.c
index 5361bcc..ef49115 100644
--- a/hw/pci.c
+++ b/hw/pci.c
@@ -1683,12 +1683,10 @@ static void pci_del_option_rom(PCIDevice *pdev)
 }
 
 /* Reserve space and add capability to the linked list in pci config space */
-int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
+int pci_add_capability_at_offset(PCIDevice *pdev, uint8_t cap_id,
+                                 uint8_t offset, uint8_t size)
 {
-    uint8_t offset = pci_find_space(pdev, size);
     uint8_t *config = pdev->config + offset;
-    if (!offset)
-        return -ENOSPC;
     config[PCI_CAP_LIST_ID] = cap_id;
     config[PCI_CAP_LIST_NEXT] = pdev->config[PCI_CAPABILITY_LIST];
     pdev->config[PCI_CAPABILITY_LIST] = offset;
@@ -1701,6 +1699,17 @@ int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
     return offset;
 }
 
+/* Find and reserve space and add capability to the linked list
+ * in pci config space */
+int pci_add_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
+{
+    uint8_t offset = pci_find_space(pdev, size);
+    if (!offset) {
+        return -ENOSPC;
+    }
+    return pci_add_capability_at_offset(pdev, cap_id, offset, size);
+}
+
 /* Unlink capability from the pci config space. */
 void pci_del_capability(PCIDevice *pdev, uint8_t cap_id, uint8_t size)
 {
diff --git a/hw/pci.h b/hw/pci.h
index 77c2083..91f5137 100644
--- a/hw/pci.h
+++ b/hw/pci.h
@@ -239,6 +239,8 @@ int pci_enable_capability_support(PCIDevice *pci_dev,
 int pci_map_irq(PCIDevice *pci_dev, int pin);
 
 int pci_add_capability(PCIDevice *pci_dev, uint8_t cap_id, uint8_t cap_size);
+int pci_add_capability_at_offset(PCIDevice *pci_dev, uint8_t cap_id,
+                                 uint8_t cap_offset, uint8_t cap_size);
 
 void pci_del_capability(PCIDevice *pci_dev, uint8_t cap_id, uint8_t cap_size);
 
-- 
1.7.3.2

