vtd: avoid redundant context mapping
authorKeir Fraser <keir.fraser@citrix.com>
Mon, 5 Jan 2009 11:13:22 +0000 (11:13 +0000)
committerKeir Fraser <keir.fraser@citrix.com>
Mon, 5 Jan 2009 11:13:22 +0000 (11:13 +0000)
After changeset 18934 (VT-d: Fix PCI-X device assignment), my assigned
PCI E1000 NIC doesn't work in guest.

The NIC is 03:00.0. Its parent bridge is: 00:1e.0.
In domain_context_mapping():
   case DEV_TYPE_PCI:
   After we domain_context_mapping_one() 03:00.0 and 00:1e.0, the
   'secbus' is 3 and 'bus' is 0,  so we domain_context_mapping_one()
   03:00.0 again -- this redundant invocation returns -EINVAL because
   we have created the mapping but haven't changed pdev->domain from
   Dom0 to a new domain at this time and eventually the
   XEN_DOMCTL_assign_device hypercall returns a failure.

The attached patch detects this case and avoids the redundant
invocation.

Signed-off-by: Dexuan Cui <dexuan.cui@intel.com>
xen/drivers/passthrough/vtd/iommu.c

index d2f0522dda2c79d79cf2b38bcdc7651349d3cc24..ec9ef5f8589dfabbee34f45074ef5855771c3863 100644 (file)
@@ -1209,7 +1209,7 @@ static int domain_context_mapping(struct domain *domain, u8 bus, u8 devfn)
     int ret = 0;
     u16 sec_bus, sub_bus;
     u32 type;
-    u8 secbus;
+    u8 secbus, secdevfn;
 
     drhd = acpi_find_matched_drhd_unit(bus, devfn);
     if ( !drhd )
@@ -1256,10 +1256,12 @@ static int domain_context_mapping(struct domain *domain, u8 bus, u8 devfn)
            break;
 
         secbus = bus;
+        secdevfn = devfn;
         /* dependent devices mapping */
         while ( bus2bridge[bus].map )
         {
             secbus = bus;
+            secdevfn = devfn;
             devfn = bus2bridge[bus].devfn;
             bus = bus2bridge[bus].bus;
             ret = domain_context_mapping_one(domain, drhd->iommu, bus, devfn);
@@ -1267,7 +1269,7 @@ static int domain_context_mapping(struct domain *domain, u8 bus, u8 devfn)
                 return ret;
         }
 
-        if ( secbus != bus )
+        if ( (secbus != bus) && (secdevfn != 0) )
             /*
              * The source-id for transactions on non-PCIe buses seem
              * to originate from devfn=0 on the secondary bus behind
@@ -1333,7 +1335,7 @@ static int domain_context_unmap(struct domain *domain, u8 bus, u8 devfn)
     struct acpi_drhd_unit *drhd;
     int ret = 0;
     u32 type;
-    u8 secbus;
+    u8 secbus, secdevfn;
 
     drhd = acpi_find_matched_drhd_unit(bus, devfn);
     if ( !drhd )
@@ -1362,10 +1364,12 @@ static int domain_context_unmap(struct domain *domain, u8 bus, u8 devfn)
             break;
 
         secbus = bus;
+        secdevfn = devfn;
         /* dependent devices unmapping */
         while ( bus2bridge[bus].map )
         {
             secbus = bus;
+            secdevfn = devfn;
             devfn = bus2bridge[bus].devfn;
             bus = bus2bridge[bus].bus;
             ret = domain_context_unmap_one(domain, drhd->iommu, bus, devfn);
@@ -1373,7 +1377,7 @@ static int domain_context_unmap(struct domain *domain, u8 bus, u8 devfn)
                 return ret;
         }
 
-        if ( bus != secbus )
+        if ( (secbus != bus) && (secdevfn != 0) )
             ret = domain_context_unmap_one(domain, drhd->iommu, secbus, 0);
         break;