From cf9ca9693fa72e444815b6226b942489da2dd9dd Mon Sep 17 00:00:00 2001
From: Andrea Bolognani <abologna@redhat.com>
Date: Tue, 16 Aug 2016 18:18:03 +0200
Subject: [PATCH] qemu: Simplify virDomainPCIConnectFlags handling

---
 src/qemu/qemu_domain_address.c | 62 ++++++++++++++++++++++--------------------
 1 file changed, 32 insertions(+), 30 deletions(-)

diff --git a/src/qemu/qemu_domain_address.c b/src/qemu/qemu_domain_address.c
index 49181d1..108c311 100644
--- a/src/qemu/qemu_domain_address.c
+++ b/src/qemu/qemu_domain_address.c
@@ -996,7 +996,6 @@ qemuDomainAssignDevicePCISlots(virDomainDefPtr def,
                                virDomainPCIAddressSetPtr addrs)
 {
     size_t i, j;
-    virDomainPCIConnectFlags flags = 0; /* initialize to quiet gcc warning */
     virDomainPCIConnectFlags virtioFlags;
     virDomainPCIConnectFlags pciFlags;
     virDomainPCIConnectFlags pcieFlags;
@@ -1007,6 +1006,9 @@ qemuDomainAssignDevicePCISlots(virDomainDefPtr def,
     for (i = 0; i < def->ncontrollers; i++) {
         if (def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_PCI) {
             virDomainControllerModelPCI model = def->controllers[i]->model;
+            virDomainPCIConnectFlags flags;
+
+            flags = virDomainPCIControllerModelToConnectType(model);
 
             if (model == VIR_DOMAIN_CONTROLLER_MODEL_PCIE_ROOT) {
                 havePCIeRoot = true;
@@ -1020,7 +1022,6 @@ qemuDomainAssignDevicePCISlots(virDomainDefPtr def,
              * flag to use when searching for the proper
              * controller/bus to connect it to on the upstream side.
              */
-            flags = virDomainPCIControllerModelToConnectType(model);
             if (virDomainPCIAddressReserveNextSlot(addrs,
                                                    &def->controllers[i]->info,
                                                    flags) < 0)
@@ -1043,11 +1044,10 @@ qemuDomainAssignDevicePCISlots(virDomainDefPtr def,
         if (!virDeviceInfoPCIAddressWanted(&def->fss[i]->info))
             continue;
 
-        flags = virtioFlags;
         /* Only support VirtIO-9p-pci so far. If that changes,
          * we might need to skip devices here */
         if (virDomainPCIAddressReserveNextSlot(addrs, &def->fss[i]->info,
-                                               flags) < 0)
+                                               virtioFlags) < 0)
             goto error;
     }
 
@@ -1058,6 +1058,7 @@ qemuDomainAssignDevicePCISlots(virDomainDefPtr def,
          * instead of here.
          */
         virDomainNetDefPtr net = def->nets[i];
+        virDomainPCIConnectFlags flags;
 
         if ((net->type == VIR_DOMAIN_NET_TYPE_HOSTDEV) ||
             !virDeviceInfoPCIAddressWanted(&net->info)) {
@@ -1082,9 +1083,8 @@ qemuDomainAssignDevicePCISlots(virDomainDefPtr def,
             def->sounds[i]->model == VIR_DOMAIN_SOUND_MODEL_USB)
             continue;
 
-        flags = pciFlags;
         if (virDomainPCIAddressReserveNextSlot(addrs, &def->sounds[i]->info,
-                                               flags) < 0)
+                                               pciFlags) < 0)
             goto error;
     }
 
@@ -1113,7 +1113,6 @@ qemuDomainAssignDevicePCISlots(virDomainDefPtr def,
         if (!virDeviceInfoPCIAddressWanted(&def->controllers[i]->info))
             continue;
 
-        flags = pciFlags;
         /* USB2 needs special handling to put all companions in the same slot */
         if (IS_USB2_CONTROLLER(def->controllers[i])) {
             virPCIDeviceAddress addr = { 0, 0, 0, 0, false };
@@ -1152,7 +1151,8 @@ qemuDomainAssignDevicePCISlots(virDomainDefPtr def,
             if (!foundAddr) {
                 /* This is the first part of the controller, so need
                  * to find a free slot & then reserve a function */
-                if (virDomainPCIAddressGetNextSlot(addrs, &tmp_addr, flags) < 0)
+                if (virDomainPCIAddressGetNextSlot(addrs, &tmp_addr,
+                                                   pciFlags) < 0)
                     goto error;
 
                 addr.bus = tmp_addr.bus;
@@ -1163,19 +1163,24 @@ qemuDomainAssignDevicePCISlots(virDomainDefPtr def,
                 addrs->lastaddr.multi = VIR_TRISTATE_SWITCH_ABSENT;
             }
             /* Finally we can reserve the slot+function */
-            if (virDomainPCIAddressReserveAddr(addrs, &addr, flags,
+            if (virDomainPCIAddressReserveAddr(addrs, &addr, pciFlags,
                                                false, foundAddr) < 0)
                 goto error;
 
             def->controllers[i]->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI;
             def->controllers[i]->info.addr.pci = addr;
         } else {
+            virDomainPCIConnectFlags flags;
+
             if ((def->controllers[i]->type == VIR_DOMAIN_CONTROLLER_TYPE_SCSI &&
                  def->controllers[i]->model ==
                  VIR_DOMAIN_CONTROLLER_MODEL_SCSI_VIRTIO_SCSI) ||
                 (def->controllers[i]->type ==
                  VIR_DOMAIN_CONTROLLER_TYPE_VIRTIO_SERIAL))
                 flags = virtioFlags;
+            else
+                flags = pciFlags;
+
             if (virDomainPCIAddressReserveNextSlot(addrs,
                                                    &def->controllers[i]->info,
                                                    flags) < 0)
@@ -1210,9 +1215,8 @@ qemuDomainAssignDevicePCISlots(virDomainDefPtr def,
             goto error;
         }
 
-        flags = virtioFlags;
         if (virDomainPCIAddressReserveNextSlot(addrs, &def->disks[i]->info,
-                                               flags) < 0)
+                                               virtioFlags) < 0)
             goto error;
     }
 
@@ -1224,10 +1228,9 @@ qemuDomainAssignDevicePCISlots(virDomainDefPtr def,
             def->hostdevs[i]->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
             continue;
 
-        flags = pciFlags;
         if (virDomainPCIAddressReserveNextSlot(addrs,
                                                def->hostdevs[i]->info,
-                                               flags) < 0)
+                                               pciFlags) < 0)
             goto error;
     }
 
@@ -1235,10 +1238,10 @@ qemuDomainAssignDevicePCISlots(virDomainDefPtr def,
     if (def->memballoon &&
         def->memballoon->model == VIR_DOMAIN_MEMBALLOON_MODEL_VIRTIO &&
         virDeviceInfoPCIAddressWanted(&def->memballoon->info)) {
-        flags = virtioFlags;
+
         if (virDomainPCIAddressReserveNextSlot(addrs,
                                                &def->memballoon->info,
-                                               flags) < 0)
+                                               virtioFlags) < 0)
             goto error;
     }
 
@@ -1248,9 +1251,8 @@ qemuDomainAssignDevicePCISlots(virDomainDefPtr def,
             !virDeviceInfoPCIAddressWanted(&def->rngs[i]->info))
             continue;
 
-        flags = virtioFlags;
-        if (virDomainPCIAddressReserveNextSlot(addrs,
-                                               &def->rngs[i]->info, flags) < 0)
+        if (virDomainPCIAddressReserveNextSlot(addrs, &def->rngs[i]->info,
+                                               virtioFlags) < 0)
             goto error;
     }
 
@@ -1258,9 +1260,9 @@ qemuDomainAssignDevicePCISlots(virDomainDefPtr def,
     if (def->watchdog &&
         def->watchdog->model == VIR_DOMAIN_WATCHDOG_MODEL_I6300ESB &&
         virDeviceInfoPCIAddressWanted(&def->watchdog->info)) {
-        flags = pciFlags;
+
         if (virDomainPCIAddressReserveNextSlot(addrs, &def->watchdog->info,
-                                               flags) < 0)
+                                               pciFlags) < 0)
             goto error;
     }
 
@@ -1268,10 +1270,13 @@ qemuDomainAssignDevicePCISlots(virDomainDefPtr def,
      * assigned address. */
     if (def->nvideos > 0 &&
         virDeviceInfoPCIAddressWanted(&def->videos[0]->info)) {
+        virDomainPCIConnectFlags flags;
+
         if (def->videos[0]->type == VIR_DOMAIN_VIDEO_TYPE_VIRTIO)
             flags = virtioFlags;
         else
             flags = pciFlags;
+
         if (virDomainPCIAddressReserveNextSlot(addrs, &def->videos[0]->info,
                                                flags) < 0)
             goto error;
@@ -1287,9 +1292,8 @@ qemuDomainAssignDevicePCISlots(virDomainDefPtr def,
         if (!virDeviceInfoPCIAddressWanted(&def->videos[i]->info))
             continue;
 
-        flags = pciFlags;
         if (virDomainPCIAddressReserveNextSlot(addrs, &def->videos[i]->info,
-                                               flags) < 0)
+                                               pciFlags) < 0)
             goto error;
     }
 
@@ -1298,9 +1302,8 @@ qemuDomainAssignDevicePCISlots(virDomainDefPtr def,
         if (!virDeviceInfoPCIAddressWanted(&def->shmems[i]->info))
             continue;
 
-        flags = pciFlags;
-        if (virDomainPCIAddressReserveNextSlot(addrs,
-                                               &def->shmems[i]->info, flags) < 0)
+        if (virDomainPCIAddressReserveNextSlot(addrs, &def->shmems[i]->info,
+                                               pciFlags) < 0)
             goto error;
     }
     for (i = 0; i < def->ninputs; i++) {
@@ -1308,9 +1311,8 @@ qemuDomainAssignDevicePCISlots(virDomainDefPtr def,
             !virDeviceInfoPCIAddressWanted(&def->inputs[i]->info))
             continue;
 
-        flags = virtioFlags;
-        if (virDomainPCIAddressReserveNextSlot(addrs,
-                                               &def->inputs[i]->info, flags) < 0)
+        if (virDomainPCIAddressReserveNextSlot(addrs, &def->inputs[i]->info,
+                                               virtioFlags) < 0)
             goto error;
     }
     for (i = 0; i < def->nparallels; i++) {
@@ -1323,8 +1325,8 @@ qemuDomainAssignDevicePCISlots(virDomainDefPtr def,
             !virDeviceInfoPCIAddressWanted(&chr->info))
             continue;
 
-        flags = pciFlags;
-        if (virDomainPCIAddressReserveNextSlot(addrs, &chr->info, flags) < 0)
+        if (virDomainPCIAddressReserveNextSlot(addrs, &chr->info,
+                                               pciFlags) < 0)
             goto error;
     }
     for (i = 0; i < def->nchannels; i++) {
-- 
2.7.4

