From nobody Thu Sep 19 01:42:19 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1719839999; cv=none; d=zohomail.com; s=zohoarc; b=F92/dmr81gOguWykMJ4EM4gG1GSnkPGoh0i/cqLij42WkM27SwPuD/PYuXKaWx2Y0s0qgGqcAA9IjzKixmLV2ZyEoGdVKFbF/aoD6dOjSJhXln+4ZiSo6POxHlz82hH0o7Zuo8FPMNrLaVggmqLBO3Bx9xY2hakLxvIdAlzCMC4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1719839999; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=xwhPXZgnf2ShtahzS2ZragTwFKjTepc0etNckiWQG/8=; b=J7dpeQjzJzAPIP9Aw3dNClbJaAAO5knUc2rUeF0UX2ue90QCle26uPgK1R2rpc3SwU5+0psXR+hVt7SOFP0rv5zwwJzOcW/jQwlHkipcKI0Ouhwbf1y/QDCjoTaIW+I4cobB+MKKL461WHZxMIENilFoZf0sgk1cBLwlDXdut0I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1719839999290256.016848045138; Mon, 1 Jul 2024 06:19:59 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sOGw4-0000K8-H9; Mon, 01 Jul 2024 09:19:12 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sOGvw-0000G1-Vv for qemu-devel@nongnu.org; Mon, 01 Jul 2024 09:19:05 -0400 Received: from mail.xenproject.org ([104.130.215.37]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sOGvd-0007ub-DO for qemu-devel@nongnu.org; Mon, 01 Jul 2024 09:19:04 -0400 Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1sOGva-0006nk-QH; Mon, 01 Jul 2024 13:18:42 +0000 Received: from lfbn-lyo-1-451-148.w2-7.abo.wanadoo.fr ([2.7.43.148] helo=l14.home) by xenbits.xenproject.org with esmtpsa (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1sOGva-0000WF-HL; Mon, 01 Jul 2024 13:18:42 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xenproject.org; s=20200302mail; h=Content-Transfer-Encoding:Content-Type: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=xwhPXZgnf2ShtahzS2ZragTwFKjTepc0etNckiWQG/8=; b=0FAej2CnIaurGLWoXc42EvwX4e KYgTnmGFwh7kQoO3zNpIKl2+Dv2atVZLculYNNscVXbJELPE74L/TYBk4lxxVfmT8TS8FqH7D9Pm5 8jEmLWSR0RjvU8sGY6YfbG95wBjnGJgxmSZNrPnFDrGiZaaRiROPh0W6dyi1/bsXDREg=; From: anthony@xenproject.org To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Subject: [PULL 2/3] xen: fix stubdom PCI addr Date: Mon, 1 Jul 2024 15:18:32 +0200 Message-Id: <20240701131833.29486-3-anthony@xenproject.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240701131833.29486-1-anthony@xenproject.org> References: <20240701131833.29486-1-anthony@xenproject.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=104.130.215.37; envelope-from=anthony@xenproject.org; helo=mail.xenproject.org X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @xenproject.org) X-ZM-MESSAGEID: 1719840000336100009 From: Marek Marczykowski-G=C3=B3recki When running in a stubdomain, the config space access via sysfs needs to use BDF as seen inside stubdomain (connected via xen-pcifront), which is different from the real BDF. For other purposes (hypercall parameters etc), the real BDF needs to be used. Get the in-stubdomain BDF by looking up relevant PV PCI xenstore entries. Signed-off-by: Marek Marczykowski-G=C3=B3recki Reviewed-by: Anthony PERARD Message-Id: <35049e99da634a74578a1ff2cb3ae4cc436ede33.1711506237.git-series= .marmarek@invisiblethingslab.com> Signed-off-by: Anthony PERARD --- hw/xen/xen-host-pci-device.c | 76 +++++++++++++++++++++++++++++++++++- hw/xen/xen-host-pci-device.h | 6 +++ 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/hw/xen/xen-host-pci-device.c b/hw/xen/xen-host-pci-device.c index 8c6e9a1716..eaf32f2710 100644 --- a/hw/xen/xen-host-pci-device.c +++ b/hw/xen/xen-host-pci-device.c @@ -9,6 +9,8 @@ #include "qemu/osdep.h" #include "qapi/error.h" #include "qemu/cutils.h" +#include "hw/xen/xen-legacy-backend.h" +#include "hw/xen/xen-bus-helper.h" #include "xen-host-pci-device.h" =20 #define XEN_HOST_PCI_MAX_EXT_CAP \ @@ -33,13 +35,73 @@ #define IORESOURCE_PREFETCH 0x00001000 /* No side effects */ #define IORESOURCE_MEM_64 0x00100000 =20 +/* + * Non-passthrough (dom0) accesses are local PCI devices and use the given= BDF + * Passthough (stubdom) accesses are through PV frontend PCI device. Those + * either have a BDF identical to the backend's BDF (xen-backend.passthrou= gh=3D1) + * or a local virtual BDF (xen-backend.passthrough=3D0) + * + * We are always given the backend's BDF and need to lookup the appropriate + * local BDF for sysfs access. + */ +static void xen_host_pci_fill_local_addr(XenHostPCIDevice *d, Error **errp) +{ + unsigned int num_devs, len, i; + unsigned int domain, bus, dev, func; + char *be_path =3D NULL; + char path[16]; + + be_path =3D qemu_xen_xs_read(xenstore, 0, "device/pci/0/backend", &len= ); + if (!be_path) { + error_setg(errp, "Failed to read device/pci/0/backend"); + goto out; + } + + if (xs_node_scanf(xenstore, 0, be_path, "num_devs", NULL, + "%d", &num_devs) !=3D 1) { + error_setg(errp, "Failed to read or parse %s/num_devs", be_path); + goto out; + } + + for (i =3D 0; i < num_devs; i++) { + snprintf(path, sizeof(path), "dev-%d", i); + if (xs_node_scanf(xenstore, 0, be_path, path, NULL, + "%x:%x:%x.%x", &domain, &bus, &dev, &func) !=3D = 4) { + error_setg(errp, "Failed to read or parse %s/%s", be_path, pat= h); + goto out; + } + if (domain !=3D d->domain || + bus !=3D d->bus || + dev !=3D d->dev || + func !=3D d->func) + continue; + snprintf(path, sizeof(path), "vdev-%d", i); + if (xs_node_scanf(xenstore, 0, be_path, path, NULL, + "%x:%x:%x.%x", &domain, &bus, &dev, &func) !=3D = 4) { + error_setg(errp, "Failed to read or parse %s/%s", be_path, pat= h); + goto out; + } + d->local_domain =3D domain; + d->local_bus =3D bus; + d->local_dev =3D dev; + d->local_func =3D func; + goto out; + } + error_setg(errp, "Failed to find PCI device %x:%x:%x.%x in xenstore", + d->domain, d->bus, d->dev, d->func); + +out: + free(be_path); +} + static void xen_host_pci_sysfs_path(const XenHostPCIDevice *d, const char *name, char *buf, ssize_t s= ize) { int rc; =20 rc =3D snprintf(buf, size, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/%s", - d->domain, d->bus, d->dev, d->func, name); + d->local_domain, d->local_bus, d->local_dev, d->local_fu= nc, + name); assert(rc >=3D 0 && rc < size); } =20 @@ -342,6 +404,18 @@ void xen_host_pci_device_get(XenHostPCIDevice *d, uint= 16_t domain, d->dev =3D dev; d->func =3D func; =20 + if (xen_is_stubdomain) { + xen_host_pci_fill_local_addr(d, errp); + if (*errp) { + goto error; + } + } else { + d->local_domain =3D d->domain; + d->local_bus =3D d->bus; + d->local_dev =3D d->dev; + d->local_func =3D d->func; + } + xen_host_pci_config_open(d, errp); if (*errp) { goto error; diff --git a/hw/xen/xen-host-pci-device.h b/hw/xen/xen-host-pci-device.h index 4d8d34ecb0..270dcb27f7 100644 --- a/hw/xen/xen-host-pci-device.h +++ b/hw/xen/xen-host-pci-device.h @@ -23,6 +23,12 @@ typedef struct XenHostPCIDevice { uint8_t dev; uint8_t func; =20 + /* different from the above in case of stubdomain */ + uint16_t local_domain; + uint8_t local_bus; + uint8_t local_dev; + uint8_t local_func; + uint16_t vendor_id; uint16_t device_id; uint32_t class_code; --=20 Anthony PERARD