From 19e94db94935b6a5ecfd532cc890d9cc04a781dd Mon Sep 17 00:00:00 2001 From: Keir Fraser Date: Mon, 5 Jan 2009 11:13:22 +0000 Subject: [PATCH] vtd: avoid redundant context mapping 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 --- xen/drivers/passthrough/vtd/iommu.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/xen/drivers/passthrough/vtd/iommu.c b/xen/drivers/passthrough/vtd/iommu.c index d2f0522dda..ec9ef5f858 100644 --- a/xen/drivers/passthrough/vtd/iommu.c +++ b/xen/drivers/passthrough/vtd/iommu.c @@ -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; -- 2.30.2