From nobody Mon Feb 9 06:34:13 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; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1675958224; cv=none; d=zohomail.com; s=zohoarc; b=mTyc3swuLTir2Pnai8uQ3FxGmPNrZdfuO7uWtT5SeXwcEHnApnDZXY5tGHodTPw7tMdfpgQevfovV76JoxN6m5+XLQdive8psLAtWgbu305pJ63+mVED9Fkj65O09ltWol5K/+zzHC47SD1Ag/wn8EdQzH70vVPaw8ZYKY5opWE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1675958224; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=0HDRmSfELdHFsxew+hy0RMD843CMREA2B14by0pRDE0=; b=H1dQFxJ3UPtKgkwxvP7BvR5LsfTH2oWauL4+nKCSLgjrvmcM6iI1wtsz/kayIr0f6b757k/IHN3M1H56RUu4lpQPTEObQoBmQ32xbrHM+i3vWLInyg8FLwQ0uaft9n5zgHo8FDGGmUG/6CHB+2WhwQaeFcgnYQ36RTc+re6/0So= 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1675958224916658.6738664365149; Thu, 9 Feb 2023 07:57:04 -0800 (PST) Received: from list by lists.xenproject.org with outflank-mailman.492680.762406 (Exim 4.92) (envelope-from ) id 1pQ9Hs-0000SI-EL; Thu, 09 Feb 2023 15:56:40 +0000 Received: by outflank-mailman (output) from mailman id 492680.762406; Thu, 09 Feb 2023 15:56:40 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pQ9Hs-0000R2-07; Thu, 09 Feb 2023 15:56:40 +0000 Received: by outflank-mailman (input) for mailman id 492680; Thu, 09 Feb 2023 15:56:38 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1pQ9Hq-0006cl-Gy for xen-devel@lists.xenproject.org; Thu, 09 Feb 2023 15:56:38 +0000 Received: from mail-ej1-x62b.google.com (mail-ej1-x62b.google.com [2a00:1450:4864:20::62b]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 553d0ea0-a892-11ed-93b5-47a8fe42b414; Thu, 09 Feb 2023 16:56:36 +0100 (CET) Received: by mail-ej1-x62b.google.com with SMTP id gr7so7725460ejb.5 for ; Thu, 09 Feb 2023 07:56:36 -0800 (PST) Received: from dsemenets-HP-EliteBook-850-G8-Notebook-PC.. ([91.219.254.72]) by smtp.gmail.com with ESMTPSA id v1-20020a170906b00100b0087862f45a29sm1027594ejy.174.2023.02.09.07.56.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Feb 2023 07:56:35 -0800 (PST) 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: 553d0ea0-a892-11ed-93b5-47a8fe42b414 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0HDRmSfELdHFsxew+hy0RMD843CMREA2B14by0pRDE0=; b=eYe8PdysANwGLi6ir/z7GNejeEdFb/yKqwstsPaWymqLfX8vtQr7IqGQgI+gOfY3LH VFCyJ076kspR56gqmhLKKIexWkAtpnhoAsFtVTMLLaSjNDSMMmlbVpFOoZEqPP7dDTwn n0hq+80Ud/gDYmdBAbtJmjl9OMJoMOGuX5z8l5UQcn1cZ7QmuJQt1wTY+MkEn5Zmfce7 ScyGnS4TCH8ZBXIC/R/AUI3WBf5Kd4k8dZfpPHMQg77sf84sHq7tsYReNfVibj+oWI2r 4mgN8ftv+I0Fq7kJvv+oWrDTOxiWRKDgAvUVS9/SXzN7fz56PHvagVejcYsWTzpjuNpj ig2w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0HDRmSfELdHFsxew+hy0RMD843CMREA2B14by0pRDE0=; b=ngxWWP4/ok8nVlqCjcUPrlwN7/Ncnc2BTxCi8hnT0/QaonyDToM41XWe1D8sPEeYG4 6OhT+QlAptCSS6RW7OQYuvafdIuVc3nqOr/MbCvPdLy/GdcDP5TknxGfHID8gwg3/ZnF uQmvfgPvx3qkg2XUcM3Y1j465mj/YOq0bw6a71tFB8yYP0uhDqYANPUsoGvpITEFVaNG cMZTXQvSO5knLI7VBtcTkNNINLLmJRzDFZf9TY2vTQ861b1R8WHs6yab8N1RHLFFyTpt kwS+FQgYwIMpBE/Do/gS3mrWc2160rYQmJQMYBei2MhYsfY8xEedX9yPXS71L/+qCFpU T7QA== X-Gm-Message-State: AO0yUKWU6z9yQnGVe5Ae6fhxFj/ZN8ahHMP917dzP8uphL0frQJVbGVN roW+zNgv83X6g8EiljTa7og6n6Z02KwpTQ== X-Google-Smtp-Source: AK7set8PttQf/A23scnNgYDmyUMC5SpM1LhW3fDa0espq2v56go0uO1k4JI44QLi/+dq2VClCOZYPA== X-Received: by 2002:a17:906:74ca:b0:8a9:e5c2:e3b8 with SMTP id z10-20020a17090674ca00b008a9e5c2e3b8mr10166753ejl.2.1675958196068; Thu, 09 Feb 2023 07:56:36 -0800 (PST) From: dmitry.semenets@gmail.com To: xen-devel@lists.xenproject.org Cc: Dmytro Semenets , Wei Liu , Anthony PERARD , Juergen Gross Subject: [RFC PATCH v4 9/9] tools/libs/light: pcid: implement write_bdf command Date: Thu, 9 Feb 2023 17:56:03 +0200 Message-Id: <20230209155604.2753219-10-dmitry.semenets@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230209155604.2753219-1-dmitry.semenets@gmail.com> References: <20230209155604.2753219-1-dmitry.semenets@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1675958225546100002 Content-Type: text/plain; charset="utf-8" From: Dmytro Semenets Signed-off-by: Dmytro Semenets --- tools/include/xen-pcid.h | 17 +++++++++ tools/libs/light/libxl_pci.c | 67 ++++++++++++++++++----------------- tools/libs/light/libxl_pcid.c | 38 ++++++++++++++++++++ 3 files changed, 90 insertions(+), 32 deletions(-) diff --git a/tools/include/xen-pcid.h b/tools/include/xen-pcid.h index ad2dec80df..e731340653 100644 --- a/tools/include/xen-pcid.h +++ b/tools/include/xen-pcid.h @@ -182,6 +182,23 @@ #define PCID_RESULT_KEY_IOMEM "iomem" #define PCID_RESULT_KEY_IRQS "irqs" =20 +/* + *************************************************************************= ****** + * Write BDF values to the pciback sysfs path + * + * This command resets PCI device + * + * Request (see other mandatory fields above): + * - "cmd" field of the request must be set to "write_bdf". + * - "sbdf" SBDF of the device in format defined by PCID_SBDF_FMT. + * - "name" name of sysfs file of pciback driver + * + * Response (see other mandatory fields above): + * - "resp" field of the response must be set to "write_bdf". + */ +#define PCID_CMD_WRITE_BDF "write_bdf" +#define PCID_MSG_FIELD_NAME "name" + /* *************************************************************************= ****** * Reset PCI device diff --git a/tools/libs/light/libxl_pci.c b/tools/libs/light/libxl_pci.c index 955635125f..d771f587c5 100644 --- a/tools/libs/light/libxl_pci.c +++ b/tools/libs/light/libxl_pci.c @@ -94,6 +94,8 @@ static int pci_handle_response(libxl__gc *gc, else if (strcmp(command_name, PCID_CMD_RESOURCE_LIST) =3D=3D 0) *result =3D (libxl__json_object *)libxl__json_map_get(PCID_MSG_FIE= LD_RESOURCES, response, JSON_MAP); + else if (strcmp(command_name, PCID_CMD_WRITE_BDF) =3D=3D 0) + *result =3D libxl__json_object_alloc(gc, JSON_NULL); return ret; } =20 @@ -511,33 +513,6 @@ static bool is_pci_in_array(libxl_device_pci *pcis, in= t num, return i < 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) -{ - int rc, fd; - char *buf; - - fd =3D open(sysfs_path, O_WRONLY); - if (fd < 0) { - LOGE(ERROR, "Couldn't open %s", sysfs_path); - return ERROR_FAIL; - } - - buf =3D GCSPRINTF(PCI_BDF, pci->domain, pci->bus, - pci->dev, pci->func); - rc =3D write(fd, buf, strlen(buf)); - /* Annoying to have two if's, but we need the errno */ - if (rc < 0) - LOGE(ERROR, "write to %s returned %d", sysfs_path, rc); - close(fd); - - if (rc < 0) - return ERROR_FAIL; - - return 0; -} - #define PCI_INFO_PATH "/libxl/pci" =20 static char *pci_info_xs_path(libxl__gc *gc, libxl_device_pci *pci, @@ -1384,6 +1359,36 @@ static bool pci_supp_legacy_irq(void) #endif } =20 +static int pciback_write_bdf(libxl__gc *gc, char *name, libxl_device_pci *= pci) +{ + struct vchan_info *vchan; + int rc; + libxl__json_object *args, *result; + + vchan =3D pci_vchan_get_client(gc); + if (!vchan) { + rc =3D ERROR_NOT_READY; + goto out; + } + + args =3D libxl__vchan_start_args(gc); + + libxl__vchan_arg_add_string(gc, args, PCID_MSG_FIELD_SBDF, + GCSPRINTF(PCID_SBDF_FMT, pci->domain, + pci->bus, pci->dev, pci->func)); + libxl__vchan_arg_add_string(gc, args, PCID_MSG_FIELD_NAME, name); + + result =3D vchan_send_command(gc, vchan, PCID_CMD_WRITE_BDF, args); + if (!result) { + rc =3D ERROR_FAIL; + goto vchan_free; + } +vchan_free: + pci_vchan_free(gc, vchan); +out: + return rc; +} + static void pci_add_dm_done(libxl__egc *egc, pci_add_state *pas, int rc) @@ -1421,8 +1426,9 @@ static void pci_add_dm_done(libxl__egc *egc, libxl__vchan_arg_add_integer(gc, args, PCID_MSG_FIELD_DOMID, domid); =20 result =3D vchan_send_command(gc, vchan, PCID_CMD_RESOURCE_LIST, args); + pci_vchan_free(gc, vchan); if (!result) - goto vchan_free; + goto out; value =3D libxl__json_map_get(PCID_RESULT_KEY_IOMEM, result, JSON_ARRA= Y); =20 /* stubdomain is always running by now, even at create time */ @@ -1483,8 +1489,7 @@ static void pci_add_dm_done(libxl__egc *egc, =20 /* 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 ) { + if (pciback_write_bdf(gc, "permissive", pci)) { LOGD(ERROR, domainid, "Setting permissive for device"); rc =3D ERROR_FAIL; goto out; @@ -1512,8 +1517,6 @@ out_no_irq: rc =3D libxl__device_pci_add_xenstore(gc, domid, pci, starting); else rc =3D 0; -vchan_free: - pci_vchan_free(gc, vchan); out: libxl__ev_time_deregister(gc, &pas->timeout); libxl__ev_time_deregister(gc, &pas->timeout_retries); diff --git a/tools/libs/light/libxl_pcid.c b/tools/libs/light/libxl_pcid.c index 0befb1999d..9368ba6c73 100644 --- a/tools/libs/light/libxl_pcid.c +++ b/tools/libs/light/libxl_pcid.c @@ -257,6 +257,41 @@ static int pciback_dev_is_assigned(libxl__gc *gc, unsi= gned int domain, return 0; } =20 +static int process_pciback_write_bdf(libxl__gc *gc, yajl_gen gen, + char *command_name, + const struct libxl__json_object *reques= t, + struct libxl__json_object **response) +{ + const struct libxl__json_object *json_o; + unsigned int dom, bus, dev, func; + int rc =3D 0; + const char *name; + char *spath; + + json_o =3D libxl__json_map_get(PCID_MSG_FIELD_SBDF, request, JSON_STRI= NG); + if (!json_o) { + make_error_reply(gc, gen, "No mandatory parameter 'sbdf'", command= _name); + return ERROR_FAIL; + } + + if (sscanf(libxl__json_object_get_string(json_o), PCID_SBDF_FMT, + &dom, &bus, &dev, &func) !=3D 4) { + make_error_reply(gc, gen, "Can't parse SBDF", command_name); + return ERROR_FAIL; + } + + json_o =3D libxl__json_map_get(PCID_MSG_FIELD_NAME, request, JSON_STRI= NG); + if (!json_o) { + make_error_reply(gc, gen, "No mandatory parameter 'rebind'", comma= nd_name); + return ERROR_FAIL; + } + + name =3D libxl__json_object_get_string(json_o); + spath =3D GCSPRINTF("%s/%s", SYSFS_PCIBACK_DRIVER, name); + LOG(WARN, "sysf_write_bdf(%s, %d, %d, %d, %d)", spath, dom, bus, dev,f= unc); + return rc; +} + #define PCID_INFO_PATH "pcid" #define PCID_BDF_XSPATH "%04x-%02x-%02x-%01x" =20 @@ -746,6 +781,9 @@ static int pcid_handle_request(libxl__gc *gc, yajl_gen = gen, else if (strcmp(command_name, PCID_CMD_RESOURCE_LIST) =3D=3D 0) ret =3D process_list_resources(gc, gen, command_name, request, &command_response); + else if (strcmp(command_name, PCID_CMD_WRITE_BDF) =3D=3D 0) + ret =3D process_pciback_write_bdf(gc, gen, command_name, + request, &command_response); else { /* * This is an unsupported command: make a reply and proceed over --=20 2.34.1