From patchwork Thu May 23 05:29:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Gibson X-Patchwork-Id: 10956903 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 699D515A6 for ; Thu, 23 May 2019 05:31:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 44EC827FC0 for ; Thu, 23 May 2019 05:31:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2FDC227FA9; Thu, 23 May 2019 05:31:49 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.0 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 99B1C27FB7 for ; Thu, 23 May 2019 05:31:48 +0000 (UTC) Received: from localhost ([127.0.0.1]:57930 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hTgKR-0001Ub-T4 for patchwork-qemu-devel@patchwork.kernel.org; Thu, 23 May 2019 01:31:47 -0400 Received: from eggs.gnu.org ([209.51.188.92]:41345) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hTgIQ-00080l-GW for qemu-devel@nongnu.org; Thu, 23 May 2019 01:29:43 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hTgIN-0005kK-Ea for qemu-devel@nongnu.org; Thu, 23 May 2019 01:29:42 -0400 Received: from ozlabs.org ([203.11.71.1]:55457) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hTgIK-0005hr-QO; Thu, 23 May 2019 01:29:38 -0400 Received: by ozlabs.org (Postfix, from userid 1007) id 458dNM2dFzz9sBK; Thu, 23 May 2019 15:29:23 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gibson.dropbear.id.au; s=201602; t=1558589363; bh=ClDTaKnH+f6JP4SlotEDr0uf2Ec7eFg7MaLVeZqMCSw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ThgBYcANrgD5S92UOI4LzaKOWnrq7xfAhmIFmQgbocJG7c3FNZgCZuAVzZCNtSnSt sq1y8FpEm4nPpkDF+UmQ/6dcBW1LRcAsDN1m2e4+qw9mePZoY64THUT2UElu4XDLD/ XE3+3E9y9b0S+94IdWYMD9dh3lQva7HEvBawk8Xk= From: David Gibson To: qemu-ppc@nongnu.org Date: Thu, 23 May 2019 15:29:16 +1000 Message-Id: <20190523052918.1129-6-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190523052918.1129-1-david@gibson.dropbear.id.au> References: <20190523052918.1129-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 203.11.71.1 Subject: [Qemu-devel] [PATCH 6/8] spapr: Don't use bus number for building DRC ids X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mst@redhat.com, qemu-devel@nongnu.org, groug@kaod.org, clg@kaod.org, mdroth@linux.ibm.com, David Gibson Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP DRC ids are more or less arbitrary, as long as they're consistent. For PCI, we notionally build them from the phb's index along with PCI bus number, slot and function number. Using bus number is broken, however, because it can change if the guest re-enumerates the PCI topology for whatever reason (e.g. due to hotplug of a bridge, which we don't support yet but want to). Fortunately, there's an alternative. Bridges are required to have a unique non-zero "chassis number" that we can use instead. Adjust the code to use that instead. This looks like it would introduce a guest visible breaking change, but in fact it does not because we don't yet ever use non-zero bus numbers. Both chassis and bus number are always 0 for the root bus, so there's no change for the existing cases. Signed-off-by: David Gibson --- hw/ppc/spapr_pci.c | 54 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/hw/ppc/spapr_pci.c b/hw/ppc/spapr_pci.c index f799f8b423..94691fcfc2 100644 --- a/hw/ppc/spapr_pci.c +++ b/hw/ppc/spapr_pci.c @@ -1221,23 +1221,40 @@ static const char *dt_name_from_class(uint8_t class, uint8_t subclass, */ static uint32_t drc_id_from_devfn(SpaprPhbState *phb, - uint32_t busnr, - int32_t devfn) + uint8_t chassis, int32_t devfn) { - return (phb->index << 16) | (busnr << 8) | devfn; + return (phb->index << 16) | (chassis << 8) | devfn; } static SpaprDrc *drc_from_devfn(SpaprPhbState *phb, - uint32_t busnr, int32_t devfn) + uint8_t chassis, int32_t devfn) { return spapr_drc_by_id(TYPE_SPAPR_DRC_PCI, - drc_id_from_devfn(phb, busnr, devfn)); + drc_id_from_devfn(phb, chassis, devfn)); +} + +static uint8_t chassis_from_bus(PCIBus *bus, Error **errp) +{ + if (pci_bus_is_root(bus)) { + return 0; + } else { + PCIDevice *bridge = pci_bridge_get_device(bus); + + return object_property_get_uint(OBJECT(bridge), "chassis_nr", errp); + } } static SpaprDrc *drc_from_dev(SpaprPhbState *phb, PCIDevice *dev) { - uint32_t busnr = pci_bus_num(PCI_BUS(qdev_get_parent_bus(DEVICE(dev)))); - return drc_from_devfn(phb, busnr, dev->devfn); + Error *local_err = NULL; + uint8_t chassis = chassis_from_bus(pci_get_bus(dev), &local_err); + + if (local_err) { + error_report_err(local_err); + return NULL; + } + + return drc_from_devfn(phb, chassis, dev->devfn); } static void add_drcs(SpaprPhbState *phb) @@ -1516,14 +1533,19 @@ static void spapr_pci_plug(HotplugHandler *plug_handler, spapr_drc_reset(drc); } else if (PCI_FUNC(pdev->devfn) == 0) { int i; + uint8_t chassis = chassis_from_bus(pci_get_bus(pdev), &local_err); + + if (local_err) { + error_propagate(errp, local_err); + return; + } for (i = 0; i < 8; i++) { SpaprDrc *func_drc; SpaprDrcClass *func_drck; SpaprDREntitySense state; - func_drc = drc_from_devfn(phb, pci_bus_num(bus), - PCI_DEVFN(slotnr, i)); + func_drc = drc_from_devfn(phb, chassis, PCI_DEVFN(slotnr, i)); func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc); state = func_drck->dr_entity_sense(func_drc); @@ -1571,18 +1593,23 @@ static void spapr_pci_unplug_request(HotplugHandler *plug_handler, g_assert(drc->dev == plugged_dev); if (!spapr_drc_unplug_requested(drc)) { - PCIBus *bus = PCI_BUS(qdev_get_parent_bus(DEVICE(pdev))); uint32_t slotnr = PCI_SLOT(pdev->devfn); SpaprDrc *func_drc; SpaprDrcClass *func_drck; SpaprDREntitySense state; int i; + Error *local_err = NULL; + uint8_t chassis = chassis_from_bus(pci_get_bus(pdev), &local_err); + + if (local_err) { + error_propagate(errp, local_err); + return; + } /* ensure any other present functions are pending unplug */ if (PCI_FUNC(pdev->devfn) == 0) { for (i = 1; i < 8; i++) { - func_drc = drc_from_devfn(phb, pci_bus_num(bus), - PCI_DEVFN(slotnr, i)); + func_drc = drc_from_devfn(phb, chassis, PCI_DEVFN(slotnr, i)); func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc); state = func_drck->dr_entity_sense(func_drc); if (state == SPAPR_DR_ENTITY_SENSE_PRESENT @@ -1603,8 +1630,7 @@ static void spapr_pci_unplug_request(HotplugHandler *plug_handler, */ if (PCI_FUNC(pdev->devfn) == 0) { for (i = 7; i >= 0; i--) { - func_drc = drc_from_devfn(phb, pci_bus_num(bus), - PCI_DEVFN(slotnr, i)); + func_drc = drc_from_devfn(phb, chassis, PCI_DEVFN(slotnr, i)); func_drck = SPAPR_DR_CONNECTOR_GET_CLASS(func_drc); state = func_drck->dr_entity_sense(func_drc); if (state == SPAPR_DR_ENTITY_SENSE_PRESENT) {