From nobody Tue Feb 10 00:40:02 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1603470434; cv=none; d=zohomail.com; s=zohoarc; b=A5YkPv0c0x0ZRDzbgGjaWDJcvrcjeRNU2lLlHsudFAmMtjbPua1v06kK1UGr5D4/xNQ8T6QUQvnkzkCdhg/eb+OGSJwFWSx8y6N8QuGW1Ki0hkgbpyC/gvJuKT0a7woZIf1VnM473kz6qsG53RcSwX2fyHFGUfJwMIoqAyfxW2E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1603470434; h=Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:Message-ID:References:Sender:Subject:To; bh=OvopI12UbRrhQAhqN7hvDR3hBLZ7P1RnY9jfoW3jfa8=; b=PoPRiFfqlvUkHWmNUzOzA0mDuTnF7pIybgkcZltPy+5cYxxAdhX/XAxNNmZQnHJhhVby2/sndObZF8zj6aN8Gn8F7GxXpZKLOWJkc14nK8jsbkPoB3SON6kC1x6SxbaCCfcopuqPEq2TFm18jhWOnjX+iN65y1u5XGXEBu+Nejk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1603470434312442.75978744785243; Fri, 23 Oct 2020 09:27:14 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.11215.29843 (Exim 4.92) (envelope-from ) id 1kVzu2-0001pG-L0; Fri, 23 Oct 2020 16:26:54 +0000 Received: by outflank-mailman (output) from mailman id 11215.29843; Fri, 23 Oct 2020 16:26:54 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kVzu2-0001op-92; Fri, 23 Oct 2020 16:26:54 +0000 Received: by outflank-mailman (input) for mailman id 11215; Fri, 23 Oct 2020 16:26:52 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kVzu0-0000v3-IR for xen-devel@lists.xenproject.org; Fri, 23 Oct 2020 16:26:52 +0000 Received: from mail.xenproject.org (unknown [104.130.215.37]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 7d654f5d-4e2a-4f1a-88ec-68c7ff500dd8; Fri, 23 Oct 2020 16:26:22 +0000 (UTC) Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kVztU-0008No-VR; Fri, 23 Oct 2020 16:26:20 +0000 Received: from ec2-18-200-132-236.eu-west-1.compute.amazonaws.com ([18.200.132.236] helo=ip-10-0-185-232.eu-west-1.compute.internal) by xenbits.xenproject.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kVzqs-000376-7I; Fri, 23 Oct 2020 16:23:38 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kVzu0-0000v3-IR for xen-devel@lists.xenproject.org; Fri, 23 Oct 2020 16:26:52 +0000 Received: from mail.xenproject.org (unknown [104.130.215.37]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 7d654f5d-4e2a-4f1a-88ec-68c7ff500dd8; Fri, 23 Oct 2020 16:26:22 +0000 (UTC) Received: from xenbits.xenproject.org ([104.239.192.120]) by mail.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1kVztU-0008No-VR; Fri, 23 Oct 2020 16:26:20 +0000 Received: from ec2-18-200-132-236.eu-west-1.compute.amazonaws.com ([18.200.132.236] helo=ip-10-0-185-232.eu-west-1.compute.internal) by xenbits.xenproject.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kVzqs-000376-7I; Fri, 23 Oct 2020 16:23:38 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 7d654f5d-4e2a-4f1a-88ec-68c7ff500dd8 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=xen.org; s=20200302mail; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=OvopI12UbRrhQAhqN7hvDR3hBLZ7P1RnY9jfoW3jfa8=; b=vi73Ud6zHzB7KkLW/mCTovsm/ d8gb0T8FrD/00Tem41RHymTesctebKGvTjQ7GjOgbnKfY56yvK0lLRBmKtVavlRrRU3QdXZxVOs0H hWzRBBndV3AyTGzmRwI0ObRWfFm5hCmhOIhrbZHoFLC9ziIEbxMdbUxs9ZOIFPCUcYt0g=; From: Paul Durrant To: xen-devel@lists.xenproject.org Cc: Paul Durrant , Ian Jackson , Wei Liu , Christian Lindig , David Scott , Anthony PERARD Subject: [PATCH 21/25] libxl: modify libxl_device_pci_assignable_add/remove/list/list_free()... Date: Fri, 23 Oct 2020 16:23:10 +0000 Message-Id: <20201023162314.2235-22-paul@xen.org> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20201023162314.2235-1-paul@xen.org> References: <20201023162314.2235-1-paul@xen.org> X-ZohoMail-DKIM: pass (identity @xen.org) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Paul Durrant ... to use 'libxl_pci_bdf' rather than 'libxl_device_pci'. This patch modifies the API and callers accordingly. It also modifies several internal functions in libxl_pci.c that support the API to also use 'libxl_pci_bdf'. NOTE: The OCaml bindings are adjusted to contain the interface change. It should therefore not affect compatibility with OCaml-based utilities. Signed-off-by: Paul Durrant --- Cc: Ian Jackson Cc: Wei Liu Cc: Christian Lindig Cc: David Scott Cc: Anthony PERARD --- tools/include/libxl.h | 15 ++- tools/libs/light/libxl_pci.c | 215 +++++++++++++++++++------------= ---- tools/ocaml/libs/xl/xenlight_stubs.c | 15 ++- tools/xl/xl_pci.c | 32 +++--- 4 files changed, 157 insertions(+), 120 deletions(-) diff --git a/tools/include/libxl.h b/tools/include/libxl.h index 5edacccbd1..5703fdf367 100644 --- a/tools/include/libxl.h +++ b/tools/include/libxl.h @@ -470,6 +470,13 @@ #define LIBXL_HAVE_PCI_BDF 1 =20 /* + * LIBXL_HAVE_PCI_ASSIGNABLE_BDF indicates that the + * libxl_device_pci_assignable_add/remove/list/list_free() functions all + * use the 'libxl_pci_bdf' type rather than 'libxl_device_pci' type. + */ +#define LIBXL_HAVE_PCI_ASSIGNABLE_BDF 1 + +/* * libxl ABI compatibility * * The only guarantee which libxl makes regarding ABI compatibility @@ -2378,10 +2385,10 @@ int libxl_device_events_handler(libxl_ctx *ctx, * added or is not bound, the functions will emit a warning but return * SUCCESS. */ -int libxl_device_pci_assignable_add(libxl_ctx *ctx, libxl_device_pci *pci,= int rebind); -int libxl_device_pci_assignable_remove(libxl_ctx *ctx, libxl_device_pci *p= ci, int rebind); -libxl_device_pci *libxl_device_pci_assignable_list(libxl_ctx *ctx, int *nu= m); -void libxl_device_pci_assignable_list_free(libxl_device_pci *list, int num= ); +int libxl_device_pci_assignable_add(libxl_ctx *ctx, libxl_pci_bdf *pcibdf,= int rebind); +int libxl_device_pci_assignable_remove(libxl_ctx *ctx, libxl_pci_bdf *pcib= df, int rebind); +libxl_pci_bdf *libxl_device_pci_assignable_list(libxl_ctx *ctx, int *num); +void libxl_device_pci_assignable_list_free(libxl_pci_bdf *list, int num); =20 /* CPUID handling */ int libxl_cpuid_parse_config(libxl_cpuid_policy_list *cpuid, const char* s= tr); diff --git a/tools/libs/light/libxl_pci.c b/tools/libs/light/libxl_pci.c index fec77dd270..5104f31448 100644 --- a/tools/libs/light/libxl_pci.c +++ b/tools/libs/light/libxl_pci.c @@ -25,26 +25,33 @@ #define PCI_BDF_XSPATH "%04x-%02x-%02x-%01x" #define PCI_PT_QDEV_ID "pci-pt-%02x_%02x.%01x" =20 -static unsigned int pci_encode_bdf(libxl_device_pci *pci) +static unsigned int pci_encode_bdf(libxl_pci_bdf *pcibdf) { unsigned int value; =20 - value =3D pci->bdf.domain << 16; - value |=3D (pci->bdf.bus & 0xff) << 8; - value |=3D (pci->bdf.dev & 0x1f) << 3; - value |=3D (pci->bdf.func & 0x7); + value =3D pcibdf->domain << 16; + value |=3D (pcibdf->bus & 0xff) << 8; + value |=3D (pcibdf->dev & 0x1f) << 3; + value |=3D (pcibdf->func & 0x7); =20 return value; } =20 +static void pcibdf_struct_fill(libxl_pci_bdf *pcibdf, unsigned int domain, + unsigned int bus, unsigned int dev, + unsigned int func) +{ + pcibdf->domain =3D domain; + pcibdf->bus =3D bus; + pcibdf->dev =3D dev; + pcibdf->func =3D func; +} + static void pci_struct_fill(libxl_device_pci *pci, unsigned int domain, unsigned int bus, unsigned int dev, unsigned int func, unsigned int vdevfn) { - pci->bdf.domain =3D domain; - pci->bdf.bus =3D bus; - pci->bdf.dev =3D dev; - pci->bdf.func =3D func; + pcibdf_struct_fill(&pci->bdf, domain, bus, dev, func); pci->vdevfn =3D vdevfn; } =20 @@ -318,8 +325,8 @@ static int is_pci_in_array(libxl_device_pci *pcis, int = num, } =20 /* Write the standard BDF into the sysfs path given by sysfs_path. */ -static int sysfs_write_bdf(libxl__gc *gc, const char * sysfs_path, - libxl_device_pci *pci) +static int sysfs_write_bdf(libxl__gc *gc, const char *sysfs_path, + libxl_pci_bdf *pcibdf) { int rc, fd; char *buf; @@ -330,8 +337,8 @@ static int sysfs_write_bdf(libxl__gc *gc, const char * = sysfs_path, return ERROR_FAIL; } =20 - buf =3D GCSPRINTF(PCI_BDF, pci->bdf.domain, pci->bdf.bus, - pci->bdf.dev, pci->bdf.func); + buf =3D GCSPRINTF(PCI_BDF, pcibdf->domain, pcibdf->bus, + pcibdf->dev, pcibdf->func); rc =3D write(fd, buf, strlen(buf)); /* Annoying to have two if's, but we need the errno */ if (rc < 0) @@ -346,22 +353,22 @@ static int sysfs_write_bdf(libxl__gc *gc, const char = * sysfs_path, =20 #define PCI_INFO_PATH "/libxl/pci" =20 -static char *pci_info_xs_path(libxl__gc *gc, libxl_device_pci *pci, +static char *pci_info_xs_path(libxl__gc *gc, libxl_pci_bdf *pcibdf, const char *node) { return node ? GCSPRINTF(PCI_INFO_PATH"/"PCI_BDF_XSPATH"/%s", - pci->bdf.domain, pci->bdf.bus, pci->bdf.dev, pci->bdf.fu= nc, + pcibdf->domain, pcibdf->bus, pcibdf->dev, pcibdf->func, node) : GCSPRINTF(PCI_INFO_PATH"/"PCI_BDF_XSPATH, - pci->bdf.domain, pci->bdf.bus, pci->bdf.dev, pci->bdf.fu= nc); + pcibdf->domain, pcibdf->bus, pcibdf->dev, pcibdf->func); } =20 =20 -static int pci_info_xs_write(libxl__gc *gc, libxl_device_pci *pci, +static int pci_info_xs_write(libxl__gc *gc, libxl_pci_bdf *pcibdf, const char *node, const char *val) { - char *path =3D pci_info_xs_path(gc, pci, node); + char *path =3D pci_info_xs_path(gc, pcibdf, node); int rc =3D libxl__xs_printf(gc, XBT_NULL, path, "%s", val); =20 if (rc) LOGE(WARN, "Write of %s to node %s failed.", val, path); @@ -369,28 +376,28 @@ static int pci_info_xs_write(libxl__gc *gc, libxl_dev= ice_pci *pci, return rc; } =20 -static char *pci_info_xs_read(libxl__gc *gc, libxl_device_pci *pci, +static char *pci_info_xs_read(libxl__gc *gc, libxl_pci_bdf *pcibdf, const char *node) { - char *path =3D pci_info_xs_path(gc, pci, node); + char *path =3D pci_info_xs_path(gc, pcibdf, node); =20 return libxl__xs_read(gc, XBT_NULL, path); } =20 -static void pci_info_xs_remove(libxl__gc *gc, libxl_device_pci *pci, +static void pci_info_xs_remove(libxl__gc *gc, libxl_pci_bdf *pcibdf, const char *node) { - char *path =3D pci_info_xs_path(gc, pci, node); + char *path =3D pci_info_xs_path(gc, pcibdf, node); libxl_ctx *ctx =3D libxl__gc_owner(gc); =20 /* Remove the xenstore entry */ xs_rm(ctx->xsh, XBT_NULL, path); } =20 -libxl_device_pci *libxl_device_pci_assignable_list(libxl_ctx *ctx, int *nu= m) +libxl_pci_bdf *libxl_device_pci_assignable_list(libxl_ctx *ctx, int *num) { GC_INIT(ctx); - libxl_device_pci *pcis =3D NULL, *new; + libxl_pci_bdf *pcibdfs =3D NULL, *new; struct dirent *de; DIR *dir; =20 @@ -411,15 +418,15 @@ libxl_device_pci *libxl_device_pci_assignable_list(li= bxl_ctx *ctx, int *num) if (sscanf(de->d_name, PCI_BDF, &dom, &bus, &dev, &func) !=3D 4) continue; =20 - new =3D realloc(pcis, ((*num) + 1) * sizeof(*new)); + new =3D realloc(pcibdfs, ((*num) + 1) * sizeof(*new)); if (NULL =3D=3D new) continue; =20 - pcis =3D new; - new =3D pcis + *num; + pcibdfs =3D new; + new =3D pcibdfs + *num; =20 - libxl_device_pci_init(new); - pci_struct_fill(new, dom, bus, dev, func, 0); + libxl_pci_bdf_init(new); + pcibdf_struct_fill(new, dom, bus, dev, func); =20 if (pci_info_xs_read(gc, new, "domid")) /* already assigned */ continue; @@ -430,32 +437,32 @@ libxl_device_pci *libxl_device_pci_assignable_list(li= bxl_ctx *ctx, int *num) closedir(dir); out: GC_FREE; - return pcis; + return pcibdfs; } =20 -void libxl_device_pci_assignable_list_free(libxl_device_pci *list, int num) +void libxl_device_pci_assignable_list_free(libxl_pci_bdf *list, int num) { int i; =20 for (i =3D 0; i < num; i++) - libxl_device_pci_dispose(&list[i]); + libxl_pci_bdf_dispose(&list[i]); =20 free(list); } =20 /* Unbind device from its current driver, if any. If driver_path is non-N= ULL, * store the path to the original driver in it. */ -static int sysfs_dev_unbind(libxl__gc *gc, libxl_device_pci *pci, +static int sysfs_dev_unbind(libxl__gc *gc, libxl_pci_bdf *pcibdf, char **driver_path) { char * spath, *dp =3D NULL; struct stat st; =20 spath =3D GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/driver", - pci->bdf.domain, - pci->bdf.bus, - pci->bdf.dev, - pci->bdf.func); + pcibdf->domain, + pcibdf->bus, + pcibdf->dev, + pcibdf->func); if ( !lstat(spath, &st) ) { /* Find the canonical path to the driver. */ dp =3D libxl__zalloc(gc, PATH_MAX); @@ -469,7 +476,7 @@ static int sysfs_dev_unbind(libxl__gc *gc, libxl_device= _pci *pci, =20 /* Unbind from the old driver */ spath =3D GCSPRINTF("%s/unbind", dp); - if ( sysfs_write_bdf(gc, spath, pci) < 0 ) { + if ( sysfs_write_bdf(gc, spath, pcibdf) < 0 ) { LOGE(ERROR, "Couldn't unbind device"); return -1; } @@ -607,8 +614,8 @@ bool libxl__is_igd_vga_passthru(libxl__gc *gc, * already exist. */ =20 -/* Scan through /sys/.../pciback/slots looking for pci's BDF */ -static int pciback_dev_has_slot(libxl__gc *gc, libxl_device_pci *pci) +/* Scan through /sys/.../pciback/slots looking for BDF */ +static int pciback_dev_has_slot(libxl__gc *gc, libxl_pci_bdf *pcibdf) { FILE *f; int rc =3D 0; @@ -621,11 +628,11 @@ static int pciback_dev_has_slot(libxl__gc *gc, libxl_= device_pci *pci) return ERROR_FAIL; } =20 - while (fscanf(f, "%x:%x:%x.%d\n", &dom, &bus, &dev, &func)=3D=3D4) { - if (dom =3D=3D pci->bdf.domain - && bus =3D=3D pci->bdf.bus - && dev =3D=3D pci->bdf.dev - && func =3D=3D pci->bdf.func) { + while (fscanf(f, "%x:%x:%x.%d\n", &dom, &bus, &dev, &func) =3D=3D 4) { + if (dom =3D=3D pcibdf->domain + && bus =3D=3D pcibdf->bus + && dev =3D=3D pcibdf->dev + && func =3D=3D pcibdf->func) { rc =3D 1; goto out; } @@ -635,7 +642,7 @@ out: return rc; } =20 -static int pciback_dev_is_assigned(libxl__gc *gc, libxl_device_pci *pci) +static int pciback_dev_is_assigned(libxl__gc *gc, libxl_pci_bdf *pcibdf) { char * spath; int rc; @@ -651,8 +658,8 @@ static int pciback_dev_is_assigned(libxl__gc *gc, libxl= _device_pci *pci) } =20 spath =3D GCSPRINTF(SYSFS_PCIBACK_DRIVER"/"PCI_BDF, - pci->bdf.domain, pci->bdf.bus, - pci->bdf.dev, pci->bdf.func); + pcibdf->domain, pcibdf->bus, + pcibdf->dev, pcibdf->func); rc =3D lstat(spath, &st); =20 if( rc =3D=3D 0 ) @@ -663,40 +670,40 @@ static int pciback_dev_is_assigned(libxl__gc *gc, lib= xl_device_pci *pci) return -1; } =20 -static int pciback_dev_assign(libxl__gc *gc, libxl_device_pci *pci) +static int pciback_dev_assign(libxl__gc *gc, libxl_pci_bdf *pcibdf) { int rc; =20 - if ( (rc =3D pciback_dev_has_slot(gc, pci)) < 0 ) { + if ( (rc =3D pciback_dev_has_slot(gc, pcibdf)) < 0 ) { LOGE(ERROR, "Error checking for pciback slot"); return ERROR_FAIL; } else if (rc =3D=3D 0) { if ( sysfs_write_bdf(gc, SYSFS_PCIBACK_DRIVER"/new_slot", - pci) < 0 ) { + pcibdf) < 0 ) { LOGE(ERROR, "Couldn't bind device to pciback!"); return ERROR_FAIL; } } =20 - if ( sysfs_write_bdf(gc, SYSFS_PCIBACK_DRIVER"/bind", pci) < 0 ) { + if ( sysfs_write_bdf(gc, SYSFS_PCIBACK_DRIVER"/bind", pcibdf) < 0 ) { LOGE(ERROR, "Couldn't bind device to pciback!"); return ERROR_FAIL; } return 0; } =20 -static int pciback_dev_unassign(libxl__gc *gc, libxl_device_pci *pci) +static int pciback_dev_unassign(libxl__gc *gc, libxl_pci_bdf *pcibdf) { /* Remove from pciback */ - if ( sysfs_dev_unbind(gc, pci, NULL) < 0 ) { + if ( sysfs_dev_unbind(gc, pcibdf, NULL) < 0 ) { LOG(ERROR, "Couldn't unbind device!"); return ERROR_FAIL; } =20 /* Remove slot if necessary */ - if ( pciback_dev_has_slot(gc, pci) > 0 ) { + if ( pciback_dev_has_slot(gc, pcibdf) > 0 ) { if ( sysfs_write_bdf(gc, SYSFS_PCIBACK_DRIVER"/remove_slot", - pci) < 0 ) { + pcibdf) < 0 ) { LOGE(ERROR, "Couldn't remove pciback slot"); return ERROR_FAIL; } @@ -705,7 +712,7 @@ static int pciback_dev_unassign(libxl__gc *gc, libxl_de= vice_pci *pci) } =20 static int libxl__device_pci_assignable_add(libxl__gc *gc, - libxl_device_pci *pci, + libxl_pci_bdf *pcibdf, int rebind) { libxl_ctx *ctx =3D libxl__gc_owner(gc); @@ -715,10 +722,10 @@ static int libxl__device_pci_assignable_add(libxl__gc= *gc, struct stat st; =20 /* Local copy for convenience */ - dom =3D pci->bdf.domain; - bus =3D pci->bdf.bus; - dev =3D pci->bdf.dev; - func =3D pci->bdf.func; + dom =3D pcibdf->domain; + bus =3D pcibdf->bus; + dev =3D pcibdf->dev; + func =3D pcibdf->func; =20 /* See if the device exists */ spath =3D GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF, dom, bus, dev, func); @@ -728,7 +735,7 @@ static int libxl__device_pci_assignable_add(libxl__gc *= gc, } =20 /* Check to see if it's already assigned to pciback */ - rc =3D pciback_dev_is_assigned(gc, pci); + rc =3D pciback_dev_is_assigned(gc, pcibdf); if ( rc < 0 ) { return ERROR_FAIL; } @@ -738,7 +745,7 @@ static int libxl__device_pci_assignable_add(libxl__gc *= gc, } =20 /* Check to see if there's already a driver that we need to unbind fro= m */ - if ( sysfs_dev_unbind(gc, pci, &driver_path ) ) { + if ( sysfs_dev_unbind(gc, pcibdf, &driver_path ) ) { LOG(ERROR, "Couldn't unbind "PCI_BDF" from driver", dom, bus, dev, func); return ERROR_FAIL; @@ -747,9 +754,9 @@ static int libxl__device_pci_assignable_add(libxl__gc *= gc, /* Store driver_path for rebinding to dom0 */ if ( rebind ) { if ( driver_path ) { - pci_info_xs_write(gc, pci, "driver_path", driver_path); + pci_info_xs_write(gc, pcibdf, "driver_path", driver_path); } else if ( (driver_path =3D - pci_info_xs_read(gc, pci, "driver_path")) !=3D NULL )= { + pci_info_xs_read(gc, pcibdf, "driver_path")) !=3D NUL= L ) { LOG(INFO, PCI_BDF" not bound to a driver, will be rebound to %= s", dom, bus, dev, func, driver_path); } else { @@ -757,10 +764,10 @@ static int libxl__device_pci_assignable_add(libxl__gc= *gc, dom, bus, dev, func); } } else { - pci_info_xs_remove(gc, pci, "driver_path"); + pci_info_xs_remove(gc, pcibdf, "driver_path"); } =20 - if ( pciback_dev_assign(gc, pci) ) { + if ( pciback_dev_assign(gc, pcibdf) ) { LOG(ERROR, "Couldn't bind device to pciback!"); return ERROR_FAIL; } @@ -771,7 +778,7 @@ quarantine: * so always pass XEN_DOMCTL_DEV_RDM_RELAXED to avoid assignment being * unnecessarily denied. */ - rc =3D xc_assign_device(ctx->xch, DOMID_IO, pci_encode_bdf(pci), + rc =3D xc_assign_device(ctx->xch, DOMID_IO, pci_encode_bdf(pcibdf), XEN_DOMCTL_DEV_RDM_RELAXED); if ( rc < 0 ) { LOG(ERROR, "failed to quarantine "PCI_BDF, dom, bus, dev, func); @@ -782,7 +789,7 @@ quarantine: } =20 static int libxl__device_pci_assignable_remove(libxl__gc *gc, - libxl_device_pci *pci, + libxl_pci_bdf *pcibdf, int rebind) { libxl_ctx *ctx =3D libxl__gc_owner(gc); @@ -790,24 +797,24 @@ static int libxl__device_pci_assignable_remove(libxl_= _gc *gc, char *driver_path; =20 /* De-quarantine */ - rc =3D xc_deassign_device(ctx->xch, DOMID_IO, pci_encode_bdf(pci)); + rc =3D xc_deassign_device(ctx->xch, DOMID_IO, pci_encode_bdf(pcibdf)); if ( rc < 0 ) { - LOG(ERROR, "failed to de-quarantine "PCI_BDF, pci->bdf.domain, pci= ->bdf.bus, - pci->bdf.dev, pci->bdf.func); + LOG(ERROR, "failed to de-quarantine "PCI_BDF, pcibdf->domain, + pcibdf->bus, pcibdf->dev, pcibdf->func); return ERROR_FAIL; } =20 /* Unbind from pciback */ - if ( (rc =3D pciback_dev_is_assigned(gc, pci)) < 0 ) { + if ( (rc =3D pciback_dev_is_assigned(gc, pcibdf)) < 0 ) { return ERROR_FAIL; } else if ( rc ) { - pciback_dev_unassign(gc, pci); + pciback_dev_unassign(gc, pcibdf); } else { LOG(WARN, "Not bound to pciback"); } =20 /* Rebind if necessary */ - driver_path =3D pci_info_xs_read(gc, pci, "driver_path"); + driver_path =3D pci_info_xs_read(gc, pcibdf, "driver_path"); =20 if ( driver_path ) { if ( rebind ) { @@ -815,12 +822,12 @@ static int libxl__device_pci_assignable_remove(libxl_= _gc *gc, =20 if ( sysfs_write_bdf(gc, GCSPRINTF("%s/bind", driver_path), - pci) < 0 ) { + pcibdf) < 0 ) { LOGE(ERROR, "Couldn't bind device to %s", driver_path); return -1; } =20 - pci_info_xs_remove(gc, pci, "driver_path"); + pci_info_xs_remove(gc, pcibdf, "driver_path"); } } else { if ( rebind ) { @@ -832,26 +839,26 @@ static int libxl__device_pci_assignable_remove(libxl_= _gc *gc, return 0; } =20 -int libxl_device_pci_assignable_add(libxl_ctx *ctx, libxl_device_pci *pci, +int libxl_device_pci_assignable_add(libxl_ctx *ctx, libxl_pci_bdf *pcibdf, int rebind) { GC_INIT(ctx); int rc; =20 - rc =3D libxl__device_pci_assignable_add(gc, pci, rebind); + rc =3D libxl__device_pci_assignable_add(gc, pcibdf, rebind); =20 GC_FREE; return rc; } =20 =20 -int libxl_device_pci_assignable_remove(libxl_ctx *ctx, libxl_device_pci *p= ci, +int libxl_device_pci_assignable_remove(libxl_ctx *ctx, libxl_pci_bdf *pcib= df, int rebind) { GC_INIT(ctx); int rc; =20 - rc =3D libxl__device_pci_assignable_remove(gc, pci, rebind); + rc =3D libxl__device_pci_assignable_remove(gc, pcibdf, rebind); =20 GC_FREE; return rc; @@ -1352,7 +1359,7 @@ static void pci_add_dm_done(libxl__egc *egc, /* Don't restrict writes to the PCI config space from this VM */ if (pci->permissive) { if ( sysfs_write_bdf(gc, SYSFS_PCIBACK_DRIVER"/permissive", - pci) < 0 ) { + &pci->bdf) < 0 ) { LOGD(ERROR, domid, "Setting permissive for device"); rc =3D ERROR_FAIL; goto out; @@ -1368,7 +1375,8 @@ out_no_irq: rc =3D ERROR_FAIL; goto out; } - r =3D xc_assign_device(ctx->xch, domid, pci_encode_bdf(pci), flag); + r =3D xc_assign_device(ctx->xch, domid, pci_encode_bdf(&pci->bdf), + flag); if (r < 0 && (hvm || errno !=3D ENOSYS)) { LOGED(ERROR, domid, "xc_assign_device failed"); rc =3D ERROR_FAIL; @@ -1447,15 +1455,28 @@ int libxl_device_pci_add(libxl_ctx *ctx, uint32_t d= omid, return AO_INPROGRESS; } =20 -static bool libxl_pci_assignable(libxl_ctx *ctx, libxl_device_pci *pci) +static int is_bdf_in_array(libxl_pci_bdf *pcibdfs, int num, + libxl_pci_bdf *pcibdf) { - libxl_device_pci *pcis; + int i; + + for(i =3D 0; i < num; i++) { + if (COMPARE_BDF(pcibdf, &pcibdfs[i])) + break; + } + + return i < num; +} + +static bool is_bdf_assignable(libxl_ctx *ctx, libxl_pci_bdf *pcibdf) +{ + libxl_pci_bdf *pcibdfs; int num; bool assignable; =20 - pcis =3D libxl_device_pci_assignable_list(ctx, &num); - assignable =3D is_pci_in_array(pcis, num, pci); - libxl_device_pci_assignable_list_free(pcis, num); + pcibdfs =3D libxl_device_pci_assignable_list(ctx, &num); + assignable =3D is_bdf_in_array(pcibdfs, num, pcibdf); + libxl_device_pci_assignable_list_free(pcibdfs, num); =20 return assignable; } @@ -1490,7 +1511,8 @@ void libxl__device_pci_add(libxl__egc *egc, uint32_t = domid, pas->callback =3D device_pci_add_stubdom_done; =20 if (libxl__domain_type(gc, domid) =3D=3D LIBXL_DOMAIN_TYPE_HVM) { - rc =3D xc_test_assign_device(ctx->xch, domid, pci_encode_bdf(pci)); + rc =3D xc_test_assign_device(ctx->xch, domid, + pci_encode_bdf(&pci->bdf)); if (rc) { LOGD(ERROR, domid, "PCI device %04x:%02x:%02x.%u %s?", @@ -1504,20 +1526,20 @@ void libxl__device_pci_add(libxl__egc *egc, uint32_= t domid, rc =3D libxl__device_pci_setdefault(gc, domid, pci, !starting); if (rc) goto out; =20 - if (pci->seize && !pciback_dev_is_assigned(gc, pci)) { - rc =3D libxl__device_pci_assignable_add(gc, pci, 1); + if (pci->seize && !pciback_dev_is_assigned(gc, &pci->bdf)) { + rc =3D libxl__device_pci_assignable_add(gc, &pci->bdf, 1); if ( rc ) goto out; } =20 - if (!libxl_pci_assignable(ctx, pci)) { + if (!is_bdf_assignable(ctx, &pci->bdf)) { LOGD(ERROR, domid, "PCI device %x:%x:%x.%x is not assignable", pci->bdf.domain, pci->bdf.bus, pci->bdf.dev, pci->bdf.func); rc =3D ERROR_FAIL; goto out; } =20 - rc =3D pci_info_xs_write(gc, pci, "domid", GCSPRINTF("%u", domid)); + rc =3D pci_info_xs_write(gc, &pci->bdf, "domid", GCSPRINTF("%u", domid= )); if (rc) goto out; =20 libxl__device_pci_reset(gc, pci->bdf.domain, pci->bdf.bus, pci->bdf.de= v, pci->bdf.func); @@ -1641,7 +1663,7 @@ static void device_pci_add_done(libxl__egc *egc, "PCI device %x:%x:%x.%x (rc %d)", pci->bdf.domain, pci->bdf.bus, pci->bdf.dev, pci->bdf.func, rc); - pci_info_xs_remove(gc, pci, "domid"); + pci_info_xs_remove(gc, &pci->bdf, "domid"); } libxl_device_pci_dispose(pci); aodev->rc =3D rc; @@ -2110,7 +2132,8 @@ static void pci_remove_detached(libxl__egc *egc, } =20 if (!isstubdom) { - rc =3D xc_deassign_device(CTX->xch, domid, pci_encode_bdf(pci)); + rc =3D xc_deassign_device(CTX->xch, domid, + pci_encode_bdf(&pci->bdf)); if (rc < 0 && (prs->hvm || errno !=3D ENOSYS)) LOGED(ERROR, domid, "xc_deassign_device failed"); } @@ -2239,7 +2262,7 @@ out: libxl__ev_time_deregister(gc, &prs->timeout); libxl__ev_time_deregister(gc, &prs->retry_timer); =20 - if (!rc) pci_info_xs_remove(gc, pci, "domid"); + if (!rc) pci_info_xs_remove(gc, &pci->bdf, "domid"); =20 libxl_device_pci_dispose(pci); aodev->rc =3D rc; diff --git a/tools/ocaml/libs/xl/xenlight_stubs.c b/tools/ocaml/libs/xl/xen= light_stubs.c index 352a00134d..2388f23869 100644 --- a/tools/ocaml/libs/xl/xenlight_stubs.c +++ b/tools/ocaml/libs/xl/xenlight_stubs.c @@ -840,7 +840,7 @@ value stub_xl_device_pci_assignable_add(value ctx, valu= e info, value rebind) device_pci_val(CTX, &c_info, info); =20 caml_enter_blocking_section(); - ret =3D libxl_device_pci_assignable_add(CTX, &c_info, c_rebind); + ret =3D libxl_device_pci_assignable_add(CTX, &c_info.bdf, c_rebind); caml_leave_blocking_section(); =20 libxl_device_pci_dispose(&c_info); @@ -861,7 +861,7 @@ value stub_xl_device_pci_assignable_remove(value ctx, v= alue info, value rebind) device_pci_val(CTX, &c_info, info); =20 caml_enter_blocking_section(); - ret =3D libxl_device_pci_assignable_remove(CTX, &c_info, c_rebind); + ret =3D libxl_device_pci_assignable_remove(CTX, &c_info.bdf, c_rebind); caml_leave_blocking_section(); =20 libxl_device_pci_dispose(&c_info); @@ -876,7 +876,7 @@ value stub_xl_device_pci_assignable_list(value ctx) { CAMLparam1(ctx); CAMLlocal2(list, temp); - libxl_device_pci *c_list; + libxl_pci_bdf *c_list; int i, nb; uint32_t c_domid; =20 @@ -889,11 +889,18 @@ value stub_xl_device_pci_assignable_list(value ctx) =20 list =3D temp =3D Val_emptylist; for (i =3D 0; i < nb; i++) { + libxl_device_pci pci; + + libxl_device_pci_init(&pci); + libxl_pci_bdf_copy(CTX, &pci.bdf, &c_list[i]); + list =3D caml_alloc_small(2, Tag_cons); Field(list, 0) =3D Val_int(0); Field(list, 1) =3D temp; temp =3D list; - Store_field(list, 0, Val_device_pci(&c_list[i])); + Store_field(list, 0, Val_device_pci(&pci)); + + libxl_device_pci_dispose(&pci); } libxl_device_pci_assignable_list_free(c_list, nb); =20 diff --git a/tools/xl/xl_pci.c b/tools/xl/xl_pci.c index 9c24496cb2..37708b4eb1 100644 --- a/tools/xl/xl_pci.c +++ b/tools/xl/xl_pci.c @@ -154,19 +154,19 @@ int main_pciattach(int argc, char **argv) =20 static void pciassignable_list(void) { - libxl_device_pci *pcis; + libxl_pci_bdf *pcibdfs; int num, i; =20 - pcis =3D libxl_device_pci_assignable_list(ctx, &num); + pcibdfs =3D libxl_device_pci_assignable_list(ctx, &num); =20 - if ( pcis =3D=3D NULL ) + if ( pcibdfs =3D=3D NULL ) return; for (i =3D 0; i < num; i++) { printf("%04x:%02x:%02x.%01x\n", - pcis[i].bdf.domain, pcis[i].bdf.bus, pcis[i].bdf.dev, - pcis[i].bdf.func); + pcibdfs[i].domain, pcibdfs[i].bus, pcibdfs[i].dev, + pcibdfs[i].func); } - libxl_device_pci_assignable_list_free(pcis, num); + libxl_device_pci_assignable_list_free(pcibdfs, num); } =20 int main_pciassignable_list(int argc, char **argv) @@ -183,24 +183,24 @@ int main_pciassignable_list(int argc, char **argv) =20 static int pciassignable_add(const char *bdf, int rebind) { - libxl_device_pci pci; + libxl_pci_bdf pcibdf; XLU_Config *config; int r =3D 0; =20 - libxl_device_pci_init(&pci); + libxl_pci_bdf_init(&pcibdf); =20 config =3D xlu_cfg_init(stderr, "command line"); if (!config) { perror("xlu_cfg_init"); exit(-1); } =20 - if (xlu_pci_parse_bdf(config, &pci.bdf, bdf)) { + if (xlu_pci_parse_bdf(config, &pcibdf, bdf)) { fprintf(stderr, "pci-assignable-add: malformed BDF \"%s\"\n", bdf); exit(2); } =20 - if (libxl_device_pci_assignable_add(ctx, &pci, rebind)) + if (libxl_device_pci_assignable_add(ctx, &pcibdf, rebind)) r =3D 1; =20 - libxl_device_pci_dispose(&pci); + libxl_pci_bdf_dispose(&pcibdf); xlu_cfg_destroy(config); =20 return r; @@ -225,24 +225,24 @@ int main_pciassignable_add(int argc, char **argv) =20 static int pciassignable_remove(const char *bdf, int rebind) { - libxl_device_pci pci; + libxl_pci_bdf pcibdf; XLU_Config *config; int r =3D 0; =20 - libxl_device_pci_init(&pci); + libxl_pci_bdf_init(&pcibdf); =20 config =3D xlu_cfg_init(stderr, "command line"); if (!config) { perror("xlu_cfg_init"); exit(-1); } =20 - if (xlu_pci_parse_bdf(config, &pci.bdf, bdf)) { + if (xlu_pci_parse_bdf(config, &pcibdf, bdf)) { fprintf(stderr, "pci-assignable-remove: malformed BDF \"%s\"\n", b= df); exit(2); } =20 - if (libxl_device_pci_assignable_remove(ctx, &pci, rebind)) + if (libxl_device_pci_assignable_remove(ctx, &pcibdf, rebind)) r =3D 1; =20 - libxl_device_pci_dispose(&pci); + libxl_pci_bdf_dispose(&pcibdf); xlu_cfg_destroy(config); =20 return r; --=20 2.11.0