Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1891781pxj; Wed, 19 May 2021 16:56:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwxifx10v68myhjIl68D4Hraw7T6CjW3GtJmzgXvSFCGSx6whGOs/3xDXxKoBS8yg9HOeu3 X-Received: by 2002:a02:4844:: with SMTP id p65mr2217699jaa.102.1621468574520; Wed, 19 May 2021 16:56:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621468574; cv=none; d=google.com; s=arc-20160816; b=UgEo1ymdYRC3YxeXYRKEIY85fgHE7teWv36v4P085+QSmlc2hxuzguN6fiJgqqU0h0 jCzIOiGVsWz1qNjFNIhcOAyuK83ASnKUbgVXzdQcT2iT0EuVlYRiiE5DmHVH+DSSkdee Q602S6+W7Z0Vy4qOnGzFuR9RZ/zoMLQShC5uxnmDXd47p1RcG2wV/DNBi6onlb83fWxF 4yNqzF/xZ9WSemxNcOEfwnTYEP4KjiBfxyd/v6pzWu/KdXkpX9sO70MxDSvlwOFKFfrw 8j1O38YFx3WHXvwQrDGVn+hfn7hPYwaReru6bzXCebz6L2D9OC/T02DBFAQi5rc+hFuM YHCQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=lJOBwH8PLMV6va8nHZHAu/lGBTZiJMwsNPKzTsCadho=; b=sxCYkt3cuzzCDNtt/QtQQ9z3C2d9h1ZNiayG2GUpiKRZb7+c9fN03G8Fr0VN35jGzH fEojcuhvQXGVT8AGGOBKNQxm/MA3Y+BciHbcmF+6dx3OlD+Eb9V8OFIeBaeZFBJEbOGb jnGTU0ZM49Nzpai0AolLGT9DGBFzQxHn172YVFaleq274r7x90/bqgUM6TI1CtjqeRfP bMMYreFjcFkvCrxnLW2wiqPjycYKh0p4gS4k2DMRu3POUqCQzjAcw2UBIdiB8Zt9Ebur cmOrRXASr/REXAZAZWFEXbvcg2crRSgCwuJ7RF2/vGG8wLSiuFE3TWRM+osl8WjP6gWf vrbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=shlelyBt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id s8si845207iop.88.2021.05.19.16.56.01; Wed, 19 May 2021 16:56:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=shlelyBt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230188AbhESX4S (ORCPT + 99 others); Wed, 19 May 2021 19:56:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230149AbhESX4Q (ORCPT ); Wed, 19 May 2021 19:56:16 -0400 Received: from mail-pf1-x42a.google.com (mail-pf1-x42a.google.com [IPv6:2607:f8b0:4864:20::42a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 585B8C061574; Wed, 19 May 2021 16:54:56 -0700 (PDT) Received: by mail-pf1-x42a.google.com with SMTP id e19so11047765pfv.3; Wed, 19 May 2021 16:54:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=lJOBwH8PLMV6va8nHZHAu/lGBTZiJMwsNPKzTsCadho=; b=shlelyBt1qer5wpAW1LO1ffaKwlZNc+NTVk/TDOT5Veelln89KUemjua0v5IN+I8Pw xCJMCWCPYNDdV5YtaCNkkuDKOPBWvdeQwBUfWoBmoOXoqmNfSFMvqcck/wnG+CiklpPa 2qpUxmNrLT6UqQkG20zR0hhAer7qvJPUKGbQe5zPZyuxLsXASkifoaeXz+qXtWWnpmdx Kq6+Vrht1mT6Wij6555fgo659jwoRcJAh70yeO1grR+BBDsp+GmroNpVVFFdRawyStdr ABfNXZJDYXwZE38wxiPkr48YWMQLU7NcV8ILOi7RF72IhTAIJOxA4jldzaDmdrf/Vyvm O2UA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=lJOBwH8PLMV6va8nHZHAu/lGBTZiJMwsNPKzTsCadho=; b=hKaGiJA7EaWs0Ao6+LADli4/aSaqXLFih3EiHwXSJiB2YN/VtL6tzO0Gg1HqVfIDbY yY5pebyM3Vpj3MRZuQ8aLcBW5TKpe4BpUNSBrlZPxMVCsjjMFP5VwerMkkAj2II4lCDK iaxvfEPl0mbHqEiNTWUI4bobb6K5LlsPWfdvy9zNJie7ugq9SPycxhIaDT9kVLMlKjxP IFkcUBVPdepQf6a8pPTEXXZyP19/HErIWO5YA1DPFsbGfSQbaOWuXNNi9x0W96FxFIpW aMgSiX5HJ2JdkdxtzO29dPDjLY+D04RMhbRkqSnW2LThyQJwLp74PkQ3c+KGXj1vuf8H jRCg== X-Gm-Message-State: AOAM531HNZziPGq3TjwZMciqkcFsgSXJl6qakCAvTWcYyGCTkPA7zwJq rZWP4z9zlSRS0ZZUSeWd8No= X-Received: by 2002:aa7:8d5a:0:b029:227:7b07:7d8b with SMTP id s26-20020aa78d5a0000b02902277b077d8bmr1594306pfe.26.1621468495847; Wed, 19 May 2021 16:54:55 -0700 (PDT) Received: from localhost.localdomain ([94.140.8.39]) by smtp.googlemail.com with ESMTPSA id z12sm397670pfk.45.2021.05.19.16.54.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 May 2021 16:54:55 -0700 (PDT) From: Amey Narkhede To: Bjorn Helgaas Cc: alex.williamson@redhat.com, raphael.norwitz@nutanix.com, linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, kw@linux.com, Amey Narkhede Subject: [PATCH RESEND v2 5/7] PCI/sysfs: Allow userspace to query and set device reset mechanism Date: Thu, 20 May 2021 05:24:24 +0530 Message-Id: <20210519235426.99728-6-ameynarkhede03@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210519235426.99728-1-ameynarkhede03@gmail.com> References: <20210519235426.99728-1-ameynarkhede03@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add reset_method sysfs attribute to enable user to query and set user preferred device reset methods and their ordering. Reviewed-by: Alex Williamson Reviewed-by: Raphael Norwitz Co-developed-by: Alex Williamson Signed-off-by: Alex Williamson Signed-off-by: Amey Narkhede --- Documentation/ABI/testing/sysfs-bus-pci | 16 +++++ drivers/pci/pci-sysfs.c | 91 ++++++++++++++++++++++++- 2 files changed, 104 insertions(+), 3 deletions(-) diff --git a/Documentation/ABI/testing/sysfs-bus-pci b/Documentation/ABI/testing/sysfs-bus-pci index 25c9c3977..36fba7ebf 100644 --- a/Documentation/ABI/testing/sysfs-bus-pci +++ b/Documentation/ABI/testing/sysfs-bus-pci @@ -121,6 +121,22 @@ Description: child buses, and re-discover devices removed earlier from this part of the device tree. +What: /sys/bus/pci/devices/.../reset_method +Date: March 2021 +Contact: Amey Narkhede +Description: + Some devices allow an individual function to be reset + without affecting other functions in the same slot. + For devices that have this support, a file named reset_method + will be present in sysfs. Reading this file will give names + of the device supported reset methods and their ordering. + Writing the name or comma separated list of names of any of + the device supported reset methods to this file will set the + reset methods and their ordering to be used when resetting + the device. Writing empty string to this file will disable + ability to reset the device and writing "default" will return + to the original value. + What: /sys/bus/pci/devices/.../reset Date: July 2009 Contact: Michael S. Tsirkin diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index 388895099..cf2f66270 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -1304,6 +1304,84 @@ static const struct bin_attribute pcie_config_attr = { .write = pci_write_config, }; +static ssize_t reset_method_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct pci_dev *pdev = to_pci_dev(dev); + ssize_t len = 0; + int i, prio; + + for (prio = PCI_RESET_FN_METHODS; prio; prio--) { + for (i = 0; i < PCI_RESET_FN_METHODS; i++) { + if (prio == pdev->reset_methods[i]) { + len += sysfs_emit_at(buf, len, "%s%s", + len ? "," : "", + pci_reset_fn_methods[i].name); + break; + } + } + + if (i == PCI_RESET_FN_METHODS) + break; + } + + return len; +} + +static ssize_t reset_method_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + u8 reset_methods[PCI_RESET_FN_METHODS]; + struct pci_dev *pdev = to_pci_dev(dev); + u8 prio = PCI_RESET_FN_METHODS; + char *name; + int i; + + /* + * Initialize reset_method such that 0xff indicates + * supported but not currently enabled reset methods + * as we only use priority values which are within + * the range of PCI_RESET_FN_METHODS array size + */ + for (i = 0; i < PCI_RESET_FN_METHODS; i++) + reset_methods[i] = pdev->reset_methods[i] ? 0xff : 0; + + if (sysfs_streq(buf, "")) { + pci_warn(pdev, "All device reset methods disabled by user"); + goto set_reset_methods; + } + + if (sysfs_streq(buf, "default")) { + for (i = 0; i < PCI_RESET_FN_METHODS; i++) + reset_methods[i] = reset_methods[i] ? prio-- : 0; + goto set_reset_methods; + } + + while ((name = strsep((char **)&buf, ",")) != NULL) { + for (i = 0; i < PCI_RESET_FN_METHODS; i++) { + if (reset_methods[i] && + sysfs_streq(name, pci_reset_fn_methods[i].name)) { + reset_methods[i] = prio--; + break; + } + } + if (i == PCI_RESET_FN_METHODS) + return -EINVAL; + } + + if (reset_methods[0] && + reset_methods[0] != PCI_RESET_FN_METHODS) + pci_warn(pdev, "Device specific reset disabled/de-prioritized by user"); + +set_reset_methods: + memcpy(pdev->reset_methods, reset_methods, sizeof(reset_methods)); + return count; +} + +static DEVICE_ATTR_RW(reset_method); + static ssize_t reset_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { @@ -1337,11 +1415,16 @@ static int pci_create_capabilities_sysfs(struct pci_dev *dev) if (pci_reset_supported(dev)) { retval = device_create_file(&dev->dev, &dev_attr_reset); if (retval) - goto error; + goto err_reset; + retval = device_create_file(&dev->dev, &dev_attr_reset_method); + if (retval) + goto err_method; } return 0; -error: +err_method: + device_remove_file(&dev->dev, &dev_attr_reset); +err_reset: pcie_vpd_remove_sysfs_dev_files(dev); return retval; } @@ -1417,8 +1500,10 @@ int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev) static void pci_remove_capabilities_sysfs(struct pci_dev *dev) { pcie_vpd_remove_sysfs_dev_files(dev); - if (pci_reset_supported(dev)) + if (pci_reset_supported(dev)) { device_remove_file(&dev->dev, &dev_attr_reset); + device_remove_file(&dev->dev, &dev_attr_reset_method); + } } /** -- 2.31.1