Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3827764pxj; Mon, 7 Jun 2021 22:51:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJysTWa578B3UxxqofdD62EghqHkc26Bhk2aouNKblkqtlJkDsuDx2nn8Yli1SR/sj+haSFu X-Received: by 2002:aa7:d555:: with SMTP id u21mr15534873edr.84.1623131497683; Mon, 07 Jun 2021 22:51:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623131497; cv=none; d=google.com; s=arc-20160816; b=JP46GxxT7sajxFb6/4ZMwzj0VkOKqxqkZclLgKV29JTN9vAqmQLJRh2YqutxxQliEI ec9sfIw/IedS8DgdHv92NvIvqot9FQvTHNsuobm/NEc0iAMSwY0f3aiOcL+iU0s4vXtc DfEOxw0wt6nkc3wFsCnu1jkY/CwHrZJxRlq4j6VJfaKS0RzujWPyS9RqVP9vY5vdX5PO 9Bza8BgUoMjcCOCJcG8te2XLBO0t6u5qwuMti1SUQZxhyuDw+8041mlSOjrhQbXjrZAq VH4K5bYh3h/Oa5QsW5CgdH4kECCCIELefPJb1kQYP4hybTx3pi1yXlczTRl61TSmwE4z ZB6A== 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=2GwU7xVOO3aHdi2ZowTA3j4dg4Dx7tydRNZ94ujj3N0=; b=USTiVInuroD86oow9raEfpaMVbtLbKdCVoHHA/ZY3UNYPDBY+vpokmuevBb/gl27yX dFS1NwY6yFgGyTmIl8mRsubjCPPtXlufEOeCbmoxRdclyYlL1mMEEJUg/JSs7atBKZaY c3SoT3QXkoqBAo48n3oLJhetdRijbNJXbYH4oWjbuJpfuWkyh994Xl91TmTFaIOVeo5h s8zmmh/8CFxkRE5z6E7Ib5yFi01tayWdq2KxS5WU6+XWnL8LfCYO4xUbTCuwdJy3QIDL TPoYmtzpze8C1EW6Nls6mh+6K6oBf1oDX+W7Fe3iDfLez8330DqW07+YwrmjzdhOUe9V dExQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=SAutUqtz; 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 b14si14100141edx.150.2021.06.07.22.51.14; Mon, 07 Jun 2021 22:51:37 -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=SAutUqtz; 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 S229659AbhFHFve (ORCPT + 99 others); Tue, 8 Jun 2021 01:51:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229507AbhFHFvd (ORCPT ); Tue, 8 Jun 2021 01:51:33 -0400 Received: from mail-oi1-x234.google.com (mail-oi1-x234.google.com [IPv6:2607:f8b0:4864:20::234]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7D2F9C061574; Mon, 7 Jun 2021 22:49:26 -0700 (PDT) Received: by mail-oi1-x234.google.com with SMTP id u11so20502353oiv.1; Mon, 07 Jun 2021 22:49:26 -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=2GwU7xVOO3aHdi2ZowTA3j4dg4Dx7tydRNZ94ujj3N0=; b=SAutUqtz+4NRqO7uYwZ+5el2wu14Cw3paIbS7yvlhgtFT6x8jnYfInvAofMUlGEzIE v+Pw0Zu9Pepp8qDh25bqkM1l6aCYDmxyOfidJLB8yz33giAh/BNwJCPQDcGTtvEbvtY7 kZHUcWvbDbPtOp8YleZdi51lr3FOwXIpUEJUPOi1NtiW6wW8EvxxlGHGa/EpJ2Tt3Lfs VvdHRzIVfxA6yUuYc936dr0rtpLh2k1tm6ZSl4NA1ctrQf8ZegU1AzhqwWkFiZoW6Fx2 CxPNom4vRpKX1/vhmn3FbdvC2mendID5OuHhxJ5LgfqzVxpBmcm1amEtX96RmpF8OY+M vN2g== 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=2GwU7xVOO3aHdi2ZowTA3j4dg4Dx7tydRNZ94ujj3N0=; b=Je0eFZxKrQZt1CbmaFCia1yrw5z4DTYiYynfoGjmifvjDR2vOgBo03ZEKssxgKKDJM FRjn/Cucyj+oOgSs2mNrFTjC0LBPxVitZWsMtze/2PE/C+UT2laXMTRIVdiwl2kRgGuf STUgF/KKACmIQAIeb4Wwtgw6sTmNtXblAj/F9DUagV1C2BTP5m8cGLbAYsIxoH4DNFuZ i7n919EelH/GHnfTVdI2F/jNWPDeQfIOq4ym6H6oGPdKRY60dZbO42j8pz+dw3IClI6s h3ABubmADaIauVevZyx130Wpbwe+byTu3IdhNwRvyV1veB8gIR7Vf8WL+aHpRg2In701 fN0g== X-Gm-Message-State: AOAM531CQ5gAXlFsHmhwGROVyl3HUR4/r0iqctV00czBIFobD/LwXSC8 LODjM/q+UWtOFrTFbmX+dJ8= X-Received: by 2002:aca:d0a:: with SMTP id 10mr1758233oin.40.1623131365782; Mon, 07 Jun 2021 22:49:25 -0700 (PDT) Received: from localhost.localdomain (static-198-54-128-46.cust.tzulo.com. [198.54.128.46]) by smtp.googlemail.com with ESMTPSA id o2sm2489730oom.26.2021.06.07.22.49.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Jun 2021 22:49:25 -0700 (PDT) From: Amey Narkhede To: Bjorn Helgaas Cc: alex.williamson@redhat.com, Raphael Norwitz , linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, kw@linux.com, Shanker Donthineni , Sinan Kaya , Len Brown , "Rafael J . Wysocki" , Amey Narkhede Subject: [PATCH v7 4/8] PCI/sysfs: Allow userspace to query and set device reset mechanism Date: Tue, 8 Jun 2021 11:18:53 +0530 Message-Id: <20210608054857.18963-5-ameynarkhede03@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210608054857.18963-1-ameynarkhede03@gmail.com> References: <20210608054857.18963-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. 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 | 118 ++++++++++++++++++++++++ 2 files changed, 134 insertions(+) diff --git a/Documentation/ABI/testing/sysfs-bus-pci b/Documentation/ABI/testing/sysfs-bus-pci index ef00fada2..cf6dbbb3c 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 316f70c3e..52def79aa 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -1334,6 +1334,123 @@ static const struct attribute_group pci_dev_rom_attr_group = { .is_bin_visible = pci_dev_rom_attr_is_visible, }; +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_METHODS_NUM; prio; prio--) { + for (i = 0; i < PCI_RESET_METHODS_NUM; 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_METHODS_NUM) + break; + } + + if (len) + len += sysfs_emit_at(buf, len, "\n"); + + 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_METHODS_NUM]; + struct pci_dev *pdev = to_pci_dev(dev); + u8 prio = PCI_RESET_METHODS_NUM; + char *name, *options; + int i; + + if (count >= (PAGE_SIZE - 1)) + return -EINVAL; + + options = kstrndup(buf, count, GFP_KERNEL); + if (!options) + return -ENOMEM; + + /* + * 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_METHODS_NUM; i++) + reset_methods[i] = pdev->reset_methods[i] ? 0xff : 0; + + if (sysfs_streq(options, "")) { + pci_warn(pdev, "All device reset methods disabled by user"); + goto set_reset_methods; + } + + if (sysfs_streq(options, "default")) { + for (i = 0; i < PCI_RESET_METHODS_NUM; i++) + reset_methods[i] = reset_methods[i] ? prio-- : 0; + goto set_reset_methods; + } + + while ((name = strsep(&options, ",")) != NULL) { + if (sysfs_streq(name, "")) + continue; + + name = strim(name); + + for (i = 0; i < PCI_RESET_METHODS_NUM; i++) { + if (reset_methods[i] && + sysfs_streq(name, pci_reset_fn_methods[i].name)) { + reset_methods[i] = prio--; + break; + } + } + + if (i == PCI_RESET_METHODS_NUM) { + kfree(options); + return -EINVAL; + } + } + + if (reset_methods[0] && + reset_methods[0] != PCI_RESET_METHODS_NUM) + pci_warn(pdev, "Device specific reset disabled/de-prioritized by user"); + +set_reset_methods: + kfree(options); + memcpy(pdev->reset_methods, reset_methods, sizeof(reset_methods)); + return count; +} +static DEVICE_ATTR_RW(reset_method); + +static struct attribute *pci_dev_reset_method_attrs[] = { + &dev_attr_reset_method.attr, + NULL, +}; + +static umode_t pci_dev_reset_method_attr_is_visible(struct kobject *kobj, + struct attribute *a, int n) +{ + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); + + if (!pci_reset_supported(pdev)) + return 0; + + return a->mode; +} + +static const struct attribute_group pci_dev_reset_method_attr_group = { + .attrs = pci_dev_reset_method_attrs, + .is_visible = pci_dev_reset_method_attr_is_visible, +}; + static ssize_t reset_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { @@ -1491,6 +1608,7 @@ const struct attribute_group *pci_dev_groups[] = { &pci_dev_config_attr_group, &pci_dev_rom_attr_group, &pci_dev_reset_attr_group, + &pci_dev_reset_method_attr_group, &pci_dev_vpd_attr_group, #ifdef CONFIG_DMI &pci_dev_smbios_attr_group, -- 2.31.1