From patchwork Fri Apr 16 20:58:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?S3J6eXN6dG9mIFdpbGN6ee+/ve+/vXNraQ==?= X-Patchwork-Id: 12208789 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7A7D6C433ED for ; Fri, 16 Apr 2021 20:59:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 49121613E3 for ; Fri, 16 Apr 2021 20:59:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244362AbhDPU7Z (ORCPT ); Fri, 16 Apr 2021 16:59:25 -0400 Received: from mail-ed1-f46.google.com ([209.85.208.46]:44784 "EHLO mail-ed1-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244287AbhDPU7Y (ORCPT ); Fri, 16 Apr 2021 16:59:24 -0400 Received: by mail-ed1-f46.google.com with SMTP id f8so33828506edd.11 for ; Fri, 16 Apr 2021 13:58:59 -0700 (PDT) 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=IwJd4epxm5V24MvuZj0Uq0t+/qsTh7e7QJtSqD/5M9M=; b=Mbd6ujd5OAhjQHWkpoPTtSZ4BI4F1cmzW19rEZNO2rM6R1J89mdemXGaS5tWORc1+Y 4FPA47ORa6DQiaK2LPpblGpf/bbF6r5nT3Vej6QSFuIllnRojmvLPxohLWd9M9BXft1K BfFG0P2rya1ZX2SrPABSxzqeBtb7yZBweA6UkPLDZnkpupiw6R6o5kbvFeboZj7i63gT gc817+U39bZWITVnHQvZBOHebKq0CDsPFEwwnHGDx8jcQiGRsRgzeRgyy+bPDhNLqWeb EtpPE3ZpNzoeQfyHkccjT5aKS5WwWvihxI4cCoOh8JhXqnO20s3Gsmi7KA8peu8gsMvu +3BQ== X-Gm-Message-State: AOAM530/GWLZ8mJXMgNmWF36uGHyaz0StBzWG+T68nznyJP5tXBJBbUG NjMRo2a0FUuGZ+GapKdeXTU= X-Google-Smtp-Source: ABdhPJzvRd+WEBnOHpBx0ZGOb+mAL+HRhueUDohb36XD15szMDKSzfah37WE5ZAqYdsUGoLxrgypzA== X-Received: by 2002:a05:6402:cbb:: with SMTP id cn27mr12194009edb.296.1618606738949; Fri, 16 Apr 2021 13:58:58 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id n11sm5103864ejg.43.2021.04.16.13.58.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Apr 2021 13:58:58 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: =?utf-8?q?Pali_Roh=C3=A1r?= , "Oliver O'Halloran" , Greg Kroah-Hartman , Daniel Vetter , Joe Perches , Dan Williams , Mauro Carvalho Chehab , David Sterba , linux-pci@vger.kernel.org Subject: [PATCH 01/20] PCI: Convert dynamic "config" sysfs object into static Date: Fri, 16 Apr 2021 20:58:37 +0000 Message-Id: <20210416205856.3234481-2-kw@linux.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210416205856.3234481-1-kw@linux.com> References: <20210416205856.3234481-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org The "config" sysfs object, which is a binary read-and-write attribute, resides at the "/sys/bus/pci/devices/.../config" path and allows for accessing either the legacy (PCI and PCI-X Mode 1) or the extended (PCI-X Mode 2 and PCIe) device PCI configuration space. At the moment, the static "config" sysfs object is dynamically created when the pci_create_sysfs_dev_files() function executes that is part of the PCI sysfs objects initialisation and when a device is added: late_initcall() pci_sysfs_init() pci_create_sysfs_dev_files() sysfs_create_bin_file() pci_bus_add_devices() pci_bus_add_device() pci_create_sysfs_dev_files() ... And dynamically removed when the pci_remove_sysfs_dev_files() function executes when the PCI device is stopped and removed: pci_stop_bus_device() pci_stop_dev() pci_remove_sysfs_dev_files() sysfs_remove_bin_file() This attribute does not need to be created dynamically and removed dynamically, and thus there is no need to also manage its create and remove life cycle manually as the PCI driver core can do it when the device is either added or removed. Convert the "config" sysfs object created dynamically to static and use the .is_bin_visible callback to set the correct sysfs object size (either 256 bytes of 4 KiB) at runtime. Then, add a newly created attribute group to the existing group called "pci_dev_groups" to ensure that the "config" sysfs object would be automatically included when the PCI driver initializes. Suggested-by: Oliver O'Halloran Suggested-by: Bjorn Helgaas Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-sysfs.c | 64 ++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 39 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index f8afd54ca3e1..dc14daf404f5 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -808,6 +808,29 @@ static ssize_t pci_write_config(struct file *filp, struct kobject *kobj, return count; } +static BIN_ATTR(config, 0644, pci_read_config, pci_write_config, 0); + +static struct bin_attribute *pci_dev_config_attrs[] = { + &bin_attr_config, + NULL, +}; + +static umode_t pci_dev_config_attr_is_visible(struct kobject *kobj, + struct bin_attribute *a, int n) +{ + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); + + a->size = PCI_CFG_SPACE_SIZE; + if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) + a->size = PCI_CFG_SPACE_EXP_SIZE; + + return a->attr.mode; +} + +static const struct attribute_group pci_dev_config_attr_group = { + .bin_attrs = pci_dev_config_attrs, + .is_bin_visible = pci_dev_config_attr_is_visible, +}; #ifdef HAVE_PCI_LEGACY /** @@ -1284,26 +1307,6 @@ static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj, return count; } -static const struct bin_attribute pci_config_attr = { - .attr = { - .name = "config", - .mode = 0644, - }, - .size = PCI_CFG_SPACE_SIZE, - .read = pci_read_config, - .write = pci_write_config, -}; - -static const struct bin_attribute pcie_config_attr = { - .attr = { - .name = "config", - .mode = 0644, - }, - .size = PCI_CFG_SPACE_EXP_SIZE, - .read = pci_read_config, - .write = pci_write_config, -}; - static ssize_t reset_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { @@ -1355,16 +1358,9 @@ int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev) if (!sysfs_initialized) return -EACCES; - if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) - retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr); - else - retval = sysfs_create_bin_file(&pdev->dev.kobj, &pci_config_attr); - if (retval) - goto err; - retval = pci_create_resource_files(pdev); if (retval) - goto err_config_file; + goto err; /* If the device has a ROM, try to expose it in sysfs. */ rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE); @@ -1405,11 +1401,6 @@ int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev) } err_resource_files: pci_remove_resource_files(pdev); -err_config_file: - if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) - sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr); - else - sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr); err: return retval; } @@ -1435,12 +1426,6 @@ void pci_remove_sysfs_dev_files(struct pci_dev *pdev) return; pci_remove_capabilities_sysfs(pdev); - - if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) - sysfs_remove_bin_file(&pdev->dev.kobj, &pcie_config_attr); - else - sysfs_remove_bin_file(&pdev->dev.kobj, &pci_config_attr); - pci_remove_resource_files(pdev); if (pdev->rom_attr) { @@ -1540,6 +1525,7 @@ static const struct attribute_group pci_dev_group = { const struct attribute_group *pci_dev_groups[] = { &pci_dev_group, + &pci_dev_config_attr_group, NULL, }; From patchwork Fri Apr 16 20:58:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?S3J6eXN6dG9mIFdpbGN6ee+/ve+/vXNraQ==?= X-Patchwork-Id: 12208791 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8D964C433B4 for ; Fri, 16 Apr 2021 20:59:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5FEC3613E3 for ; Fri, 16 Apr 2021 20:59:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244438AbhDPU71 (ORCPT ); Fri, 16 Apr 2021 16:59:27 -0400 Received: from mail-ej1-f52.google.com ([209.85.218.52]:36380 "EHLO mail-ej1-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244245AbhDPU7Z (ORCPT ); Fri, 16 Apr 2021 16:59:25 -0400 Received: by mail-ej1-f52.google.com with SMTP id r9so44003144ejj.3 for ; Fri, 16 Apr 2021 13:59:00 -0700 (PDT) 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=ySAfFGrSk2CnpJnaNlZ6qM0Yy8d5ZV25TZSLynyZ05U=; b=cfqyAPEJX9EvG6x6uaZvppJAI9dJqTkZn9IWuaH3L8BMCJoOcxBBlTSweEqcPg1cGE 6fxRnvRIZZK+87dENE4/8N0LhsvUXUI3wKrq+CWlWgWWPF8Mk7h4fqXXXS9VRZ6NARV1 V2xTZ1A/S2j//zezVLcqHV/2Dv9zPHVGlKK2OIPxyUdZxeYk5NIhDFhiIBEps5g39buu twgIdj4S8V/p7yntGAfpEJU5XYGE4xolTENpe0/UBN41+aoyT2QJWE0yCfHWLrOJKZyG iUtirK9Mt5glbDmy8uB+nISG9E4/gl4r7TbTr1JLCBN1l0aeWYlaWZkqZXe15PUPOXtX 0EIg== X-Gm-Message-State: AOAM530kF9uGAgnCqau2EzZKhKvffIqtC6/j3XVbaC3HNz7r2q4EWydl ScvwlO79qqyMBj4QZ+8bWPo= X-Google-Smtp-Source: ABdhPJy3kKQ/5DJa9fJaYnlt6bjnqijuCBdieQzWps3E5fLrX9XASMsMPdDgKLTPD90Z3aCzYaADwA== X-Received: by 2002:a17:906:6d50:: with SMTP id a16mr4090016ejt.31.1618606739897; Fri, 16 Apr 2021 13:58:59 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id n11sm5103864ejg.43.2021.04.16.13.58.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Apr 2021 13:58:59 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: =?utf-8?q?Pali_Roh=C3=A1r?= , "Oliver O'Halloran" , Greg Kroah-Hartman , Daniel Vetter , Joe Perches , Dan Williams , Mauro Carvalho Chehab , David Sterba , linux-pci@vger.kernel.org Subject: [PATCH 02/20] PCI: Convert dynamic "rom" sysfs object into static Date: Fri, 16 Apr 2021 20:58:38 +0000 Message-Id: <20210416205856.3234481-3-kw@linux.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210416205856.3234481-1-kw@linux.com> References: <20210416205856.3234481-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org The "rom" sysfs object, which is a read-only binary attribute, resides at the "/sys/bus/pci/devices/.../rom" path and allows for accessing the PCI ROM if the device has one. At the moment, the "rom" sysfs object is dynamically created when the pci_create_sysfs_dev_files() function executes that is part of the PCI sysfs objects initialisation and when a device is added: late_initcall() pci_sysfs_init() pci_create_sysfs_dev_files() sysfs_bin_attr_init() sysfs_create_bin_file() pci_bus_add_devices() pci_bus_add_device() pci_create_sysfs_dev_files() ... And dynamically removed when the pci_remove_sysfs_dev_files() function executes when the PCI device is stopped and removed, as per: pci_stop_bus_device() pci_stop_dev() pci_remove_sysfs_dev_files() sysfs_remove_bin_file() This attribute does not need to be created dynamically and removed dynamically, and thus there is no need to also manage its create and remove life cycle manually as the PCI driver core can do it when the device is either added or removed. Convert the "rom" sysfs object created dynamically to static and use the .is_bin_visible callback to set the correct sysfs object size based on the PCI ROM size. Then, add a newly created attribute group to the existing group called "pci_dev_groups" to ensure that the "rom" sysfs object would be automatically included when the PCI driver initializes. Remove the no longer needed pointer "rom_attr" from the struct pci_dev that used to hold a reference to the dynamically created "rom" sysfs object. Suggested-by: Oliver O'Halloran Suggested-by: Bjorn Helgaas Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-sysfs.c | 67 ++++++++++++++++++----------------------- include/linux/pci.h | 1 - 2 files changed, 29 insertions(+), 39 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index dc14daf404f5..fa8373685140 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -1306,6 +1306,33 @@ static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj, return count; } +static BIN_ATTR(rom, 0600, pci_read_rom, pci_write_rom, 0); + +static struct bin_attribute *pci_dev_rom_attrs[] = { + &bin_attr_rom, + NULL, +}; + +static umode_t pci_dev_rom_attr_is_visible(struct kobject *kobj, + struct bin_attribute *a, int n) +{ + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); + size_t rom_size; + + /* If the device has a ROM, try to expose it in sysfs. */ + rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE); + if (!rom_size) + return 0; + + a->size = rom_size; + + return a->attr.mode; +} + +static const struct attribute_group pci_dev_rom_attr_group = { + .bin_attrs = pci_dev_rom_attrs, + .is_bin_visible = pci_dev_rom_attr_is_visible, +}; static ssize_t reset_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) @@ -1352,8 +1379,6 @@ static int pci_create_capabilities_sysfs(struct pci_dev *dev) int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev) { int retval; - int rom_size; - struct bin_attribute *attr; if (!sysfs_initialized) return -EACCES; @@ -1362,43 +1387,15 @@ int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev) if (retval) goto err; - /* If the device has a ROM, try to expose it in sysfs. */ - rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE); - if (rom_size) { - attr = kzalloc(sizeof(*attr), GFP_ATOMIC); - if (!attr) { - retval = -ENOMEM; - goto err_resource_files; - } - sysfs_bin_attr_init(attr); - attr->size = rom_size; - attr->attr.name = "rom"; - attr->attr.mode = 0600; - attr->read = pci_read_rom; - attr->write = pci_write_rom; - retval = sysfs_create_bin_file(&pdev->dev.kobj, attr); - if (retval) { - kfree(attr); - goto err_resource_files; - } - pdev->rom_attr = attr; - } - /* add sysfs entries for various capabilities */ retval = pci_create_capabilities_sysfs(pdev); if (retval) - goto err_rom_file; + goto err_resource_files; pci_create_firmware_label_files(pdev); return 0; -err_rom_file: - if (pdev->rom_attr) { - sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr); - kfree(pdev->rom_attr); - pdev->rom_attr = NULL; - } err_resource_files: pci_remove_resource_files(pdev); err: @@ -1427,13 +1424,6 @@ void pci_remove_sysfs_dev_files(struct pci_dev *pdev) pci_remove_capabilities_sysfs(pdev); pci_remove_resource_files(pdev); - - if (pdev->rom_attr) { - sysfs_remove_bin_file(&pdev->dev.kobj, pdev->rom_attr); - kfree(pdev->rom_attr); - pdev->rom_attr = NULL; - } - pci_remove_firmware_label_files(pdev); } @@ -1526,6 +1516,7 @@ static const struct attribute_group pci_dev_group = { const struct attribute_group *pci_dev_groups[] = { &pci_dev_group, &pci_dev_config_attr_group, + &pci_dev_rom_attr_group, NULL, }; diff --git a/include/linux/pci.h b/include/linux/pci.h index 86c799c97b77..45f1fef80b50 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -458,7 +458,6 @@ struct pci_dev { u32 saved_config_space[16]; /* Config space saved at suspend time */ struct hlist_head saved_cap_space; - struct bin_attribute *rom_attr; /* Attribute descriptor for sysfs ROM entry */ int rom_attr_enabled; /* Display of ROM attribute enabled? */ struct bin_attribute *res_attr[DEVICE_COUNT_RESOURCE]; /* sysfs file for resources */ struct bin_attribute *res_attr_wc[DEVICE_COUNT_RESOURCE]; /* sysfs file for WC mapping of resources */ From patchwork Fri Apr 16 20:58:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?S3J6eXN6dG9mIFdpbGN6ee+/ve+/vXNraQ==?= X-Patchwork-Id: 12208793 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 347A0C43461 for ; Fri, 16 Apr 2021 20:59:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1B335613E3 for ; Fri, 16 Apr 2021 20:59:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244245AbhDPU72 (ORCPT ); Fri, 16 Apr 2021 16:59:28 -0400 Received: from mail-ed1-f42.google.com ([209.85.208.42]:42918 "EHLO mail-ed1-f42.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244287AbhDPU70 (ORCPT ); Fri, 16 Apr 2021 16:59:26 -0400 Received: by mail-ed1-f42.google.com with SMTP id d21so13717923edv.9 for ; Fri, 16 Apr 2021 13:59:01 -0700 (PDT) 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=GfONP6K8RDBHL1QgHrnC3J9ZMGcg9Jlord5rI4F6ZB8=; b=GNzMy3eroSIJQvwLEdUeWNIPuVHTHHnR3f3pS8gTnSZZKnWNdFSkbMql+n65XLqIwS Or4pRwENqZe1xuwViaLgiR/vutaWESh2tBISsUX1AeH8Scu9AWAozTXN8MIzIyS0lUhc D92VAOb7GDIQpvgFaut7h8JO4c2rJjM9raAG30Z1Iv9HAB6Nllq9qgdI6//LdlV39vJw 1k/NPX+zIDDCpjF5OfUnJPkyXPN79mWaF2U9THCB0d5H3TJJfweRR5ceghsToyMxn0Kb wXYBTtytYZoxZJpn1Wg1+gId4vg/8jSeiKl1Lsl26PjvJbg3GRxcQ64/cf7SdSrQI+Lo U5nA== X-Gm-Message-State: AOAM53041UfoAXk+T9jbkG05PqA+TM/hGQmqXhbTs0qJ2zGRyhRdDa1p e8YpiWQtZfS6v0drUN3/OOE= X-Google-Smtp-Source: ABdhPJxxToO5ngxNP860ld5HXeJk7n59ZjptozTNcha06u25Aw4JdMdWzTlKjGBDWVqzm4D7/kxkmA== X-Received: by 2002:a05:6402:40c9:: with SMTP id z9mr12385865edb.24.1618606740845; Fri, 16 Apr 2021 13:59:00 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id n11sm5103864ejg.43.2021.04.16.13.58.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Apr 2021 13:59:00 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: =?utf-8?q?Pali_Roh=C3=A1r?= , "Oliver O'Halloran" , Greg Kroah-Hartman , Daniel Vetter , Joe Perches , Dan Williams , Mauro Carvalho Chehab , David Sterba , linux-pci@vger.kernel.org Subject: [PATCH 03/20] PCI: Convert dynamic "reset" sysfs object into static Date: Fri, 16 Apr 2021 20:58:39 +0000 Message-Id: <20210416205856.3234481-4-kw@linux.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210416205856.3234481-1-kw@linux.com> References: <20210416205856.3234481-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org The "reset" sysfs object, which is a write-only attribute, resides at the "/sys/bus/pci/devices/.../reset" path and allows for resetting the PCI function of a device if the device supports resetting a single function. At the moment, the static "reset" sysfs object is dynamically created when the pci_create_capabilities_sysfs() function executes that is part of the PCI sysfs objects initialisation and when a device is added: late_initcall() pci_sysfs_init() pci_create_sysfs_dev_files() pci_create_capabilities_sysfs() device_create_file() pci_bus_add_devices() pci_bus_add_device() pci_create_sysfs_dev_files() ... And dynamically removed when the pci_remove_capabilities_sysfs() function executes when the PCI device is stopped and removed: pci_stop_bus_device() pci_stop_dev() pci_remove_sysfs_dev_files() pci_remove_capabilities_sysfs() device_remove_file() This attribute does not need to be created dynamically and removed dynamically, and thus there is no need to also manage its create and remove life cycle manually as the PCI driver core can do it when the device is either added or removed. Convert the "reset" sysfs object created dynamically to static and use the .is_visible callback to check whether the device has a reset function capability and to decide if the sysfs object should be made available. Then, add a newly created attribute group to the existing group called "pci_dev_groups" to ensure that the "reset" sysfs object would be automatically included when the PCI driver initialises. Move to disable the "reset_fn" flag, and thus disabling the reset function capability of a device, to the pci_stop_dev() function from the pci_remove_capabilities_sysfs() function, as it makes more sense to disable the flag when the device is about to be stopped, rather than where sysfs objects are removed. Suggested-by: Oliver O'Halloran Suggested-by: Bjorn Helgaas Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-sysfs.c | 51 +++++++++++++++++++---------------------- drivers/pci/remove.c | 2 ++ 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index fa8373685140..c469d9cad0a8 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -1355,25 +1355,33 @@ static ssize_t reset_store(struct device *dev, struct device_attribute *attr, return count; } +static DEVICE_ATTR_WO(reset); -static DEVICE_ATTR(reset, 0200, NULL, reset_store); +static struct attribute *pci_dev_reset_attrs[] = { + &dev_attr_reset.attr, + NULL, +}; -static int pci_create_capabilities_sysfs(struct pci_dev *dev) +static umode_t pci_dev_reset_attr_is_visible(struct kobject *kobj, + struct attribute *a, int n) { - int retval; + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); - pcie_vpd_create_sysfs_dev_files(dev); + if (!pdev->reset_fn) + return 0; - if (dev->reset_fn) { - retval = device_create_file(&dev->dev, &dev_attr_reset); - if (retval) - goto error; - } - return 0; + return a->mode; +} -error: - pcie_vpd_remove_sysfs_dev_files(dev); - return retval; +static const struct attribute_group pci_dev_reset_attr_group = { + .attrs = pci_dev_reset_attrs, + .is_visible = pci_dev_reset_attr_is_visible, +}; + + +static void pci_create_capabilities_sysfs(struct pci_dev *dev) +{ + pcie_vpd_create_sysfs_dev_files(dev); } int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev) @@ -1385,30 +1393,18 @@ int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev) retval = pci_create_resource_files(pdev); if (retval) - goto err; + return retval; /* add sysfs entries for various capabilities */ - retval = pci_create_capabilities_sysfs(pdev); - if (retval) - goto err_resource_files; - + pci_create_capabilities_sysfs(pdev); pci_create_firmware_label_files(pdev); return 0; - -err_resource_files: - pci_remove_resource_files(pdev); -err: - return retval; } static void pci_remove_capabilities_sysfs(struct pci_dev *dev) { pcie_vpd_remove_sysfs_dev_files(dev); - if (dev->reset_fn) { - device_remove_file(&dev->dev, &dev_attr_reset); - dev->reset_fn = 0; - } } /** @@ -1517,6 +1513,7 @@ const struct attribute_group *pci_dev_groups[] = { &pci_dev_group, &pci_dev_config_attr_group, &pci_dev_rom_attr_group, + &pci_dev_reset_attr_group, NULL, }; diff --git a/drivers/pci/remove.c b/drivers/pci/remove.c index 95dec03d9f2a..dd12c2fcc7dc 100644 --- a/drivers/pci/remove.c +++ b/drivers/pci/remove.c @@ -19,6 +19,8 @@ static void pci_stop_dev(struct pci_dev *dev) pci_pme_active(dev, false); if (pci_dev_is_added(dev)) { + dev->reset_fn = 0; + device_release_driver(&dev->dev); pci_proc_detach_device(dev); pci_remove_sysfs_dev_files(dev); From patchwork Fri Apr 16 20:58:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?S3J6eXN6dG9mIFdpbGN6ee+/ve+/vXNraQ==?= X-Patchwork-Id: 12208795 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A7A96C43460 for ; Fri, 16 Apr 2021 20:59:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8BCFA613D2 for ; Fri, 16 Apr 2021 20:59:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244287AbhDPU73 (ORCPT ); Fri, 16 Apr 2021 16:59:29 -0400 Received: from mail-ej1-f45.google.com ([209.85.218.45]:34348 "EHLO mail-ej1-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244489AbhDPU72 (ORCPT ); Fri, 16 Apr 2021 16:59:28 -0400 Received: by mail-ej1-f45.google.com with SMTP id x12so23230464ejc.1 for ; Fri, 16 Apr 2021 13:59:02 -0700 (PDT) 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=WG/g5y0DViW7yjKYVrQwqq+4O3GQmP8ASlsa/r+oCP4=; b=Vfb0rgtpS4myg9Qvj0hubjteroAtyF1LgFRQrKfNUUaEuwEB8FUDkPK39Pk+NiAFZe iuvT5anEUh2mbJx8G4XwJwbZpprRG5kuKye9Pjtf5CR5XpiTbkUAJD6wjfJtl4MmIsMD 2+OruZn1JBHC/IOiv4VZCw8ZEUGoC8f7czsNTnX9Hri/K9jTK/J/mWzL+tNYm4BgAysf Xdf72/5ly736wluevB3odhC6nt5WtbmmdysEjweEAg3bzWq9kjubdhQ3JCm3E1yKpo0h E/eJxtRCtOt2O9dmJw6ry0RmdFUzaqS0dyKrbFQVMAHBuNxjdwvhNASiAesXXW3KJ7pI v7LA== X-Gm-Message-State: AOAM531nsCBzaWHWDtRbz4uGjqjE4j7cWGMwsceh5UIEJDftyWlEjPRc LYeG3wlvcSElsmClTo4L93o= X-Google-Smtp-Source: ABdhPJzBpLjV9gCEOH0OlyjqQJ8ozJaEZ29pPD7ptwU5rm3xbICJ4Zg+Hel6XM7We9cj/oa7b+dZ+Q== X-Received: by 2002:a17:906:2652:: with SMTP id i18mr10039217ejc.189.1618606741791; Fri, 16 Apr 2021 13:59:01 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id n11sm5103864ejg.43.2021.04.16.13.59.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Apr 2021 13:59:01 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: =?utf-8?q?Pali_Roh=C3=A1r?= , "Oliver O'Halloran" , Greg Kroah-Hartman , Daniel Vetter , Joe Perches , Dan Williams , Mauro Carvalho Chehab , David Sterba , linux-pci@vger.kernel.org Subject: [PATCH 04/20] PCI/VPD: Convert dynamic "vpd" sysfs object into static Date: Fri, 16 Apr 2021 20:58:40 +0000 Message-Id: <20210416205856.3234481-5-kw@linux.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210416205856.3234481-1-kw@linux.com> References: <20210416205856.3234481-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org The "vpd" sysfs object, which is a read-and-write binary attribute, resides at the "/sys/bus/pci/devices/.../vpd" path and allows for accessing the Vital Product Data (VPD) for a device. At the moment, the "vpd" sysfs object is dynamically created when the pcie_vpd_create_sysfs_dev_files() function executes that is part of the PCI sysfs objects initialisation and when a device is added: late_initcall() pci_sysfs_init() pci_create_sysfs_dev_files() pci_create_capabilities_sysfs() pcie_vpd_create_sysfs_dev_files() sysfs_bin_attr_init() sysfs_create_bin_file() pci_bus_add_devices() pci_bus_add_device() pci_create_sysfs_dev_files() ... And dynamically removed when the pcie_vpd_remove_sysfs_dev_files() function executes when the PCI device is stopped and removed: pci_stop_bus_device() pci_stop_dev() pci_remove_sysfs_dev_files() pci_remove_capabilities_sysfs() pcie_vpd_remove_sysfs_dev_files() sysfs_remove_bin_file() This attribute does not need to be created dynamically and removed dynamically, and thus there is no need to also manage its create and remove life cycle manually as the PCI driver core can do it when the device is either added or removed. Convert the "vpd" sysfs object created dynamically to static and use the .is_bin_visible callback to check whether the device supports VPD and to decide if the sysfs object should be made available. Then, add a newly created attribute group to the existing group called "pci_dev_groups" to ensure that the "vpd" sysfs object would be automatically included when the PCI driver initializes. Remove the no longer needed pointer "attr" from the struct pci_vpd that used to hold a reference to the dynamically created "vpd" sysfs object. And also remove functions such as pcie_vpd_create_sysfs_dev_files(), pcie_vpd_remove_sysfs_dev_files(), pci_create_capabilities_sysfs() and pci_create_capabilities_sysfs() that are no longer needed to be called from the pci_create_sysfs_dev_files() or pci_remove_sysfs_dev_files() functions. Suggested-by: Oliver O'Halloran Suggested-by: Bjorn Helgaas Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-sysfs.c | 15 +------------- drivers/pci/pci.h | 3 +-- drivers/pci/vpd.c | 46 ++++++++++++++--------------------------- 3 files changed, 18 insertions(+), 46 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index c469d9cad0a8..501d120e268b 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -1378,12 +1378,6 @@ static const struct attribute_group pci_dev_reset_attr_group = { .is_visible = pci_dev_reset_attr_is_visible, }; - -static void pci_create_capabilities_sysfs(struct pci_dev *dev) -{ - pcie_vpd_create_sysfs_dev_files(dev); -} - int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev) { int retval; @@ -1395,18 +1389,11 @@ int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev) if (retval) return retval; - /* add sysfs entries for various capabilities */ - pci_create_capabilities_sysfs(pdev); pci_create_firmware_label_files(pdev); return 0; } -static void pci_remove_capabilities_sysfs(struct pci_dev *dev) -{ - pcie_vpd_remove_sysfs_dev_files(dev); -} - /** * pci_remove_sysfs_dev_files - cleanup PCI specific sysfs files * @pdev: device whose entries we should free @@ -1418,7 +1405,6 @@ void pci_remove_sysfs_dev_files(struct pci_dev *pdev) if (!sysfs_initialized) return; - pci_remove_capabilities_sysfs(pdev); pci_remove_resource_files(pdev); pci_remove_firmware_label_files(pdev); } @@ -1514,6 +1500,7 @@ const struct attribute_group *pci_dev_groups[] = { &pci_dev_config_attr_group, &pci_dev_rom_attr_group, &pci_dev_reset_attr_group, + &vpd_attr_group, NULL, }; diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index ef7c4661314f..0dc52e60d03c 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -143,8 +143,7 @@ static inline bool pcie_downstream_port(const struct pci_dev *dev) int pci_vpd_init(struct pci_dev *dev); void pci_vpd_release(struct pci_dev *dev); -void pcie_vpd_create_sysfs_dev_files(struct pci_dev *dev); -void pcie_vpd_remove_sysfs_dev_files(struct pci_dev *dev); +extern const struct attribute_group vpd_attr_group; /* PCI Virtual Channel */ int pci_save_vc_state(struct pci_dev *dev); diff --git a/drivers/pci/vpd.c b/drivers/pci/vpd.c index 7915d10f9aa1..3707671bc2f1 100644 --- a/drivers/pci/vpd.c +++ b/drivers/pci/vpd.c @@ -21,7 +21,6 @@ struct pci_vpd_ops { struct pci_vpd { const struct pci_vpd_ops *ops; - struct bin_attribute *attr; /* Descriptor for sysfs VPD entry */ struct mutex lock; unsigned int len; u16 flag; @@ -428,41 +427,28 @@ static ssize_t write_vpd_attr(struct file *filp, struct kobject *kobj, return pci_write_vpd(dev, off, count, buf); } +static BIN_ATTR(vpd, 0600, read_vpd_attr, write_vpd_attr, 0); -void pcie_vpd_create_sysfs_dev_files(struct pci_dev *dev) -{ - int retval; - struct bin_attribute *attr; - - if (!dev->vpd) - return; +static struct bin_attribute *vpd_attrs[] = { + &bin_attr_vpd, + NULL, +}; - attr = kzalloc(sizeof(*attr), GFP_ATOMIC); - if (!attr) - return; +static umode_t vpd_attr_is_visible(struct kobject *kobj, + struct bin_attribute *a, int n) +{ + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); - sysfs_bin_attr_init(attr); - attr->size = 0; - attr->attr.name = "vpd"; - attr->attr.mode = S_IRUSR | S_IWUSR; - attr->read = read_vpd_attr; - attr->write = write_vpd_attr; - retval = sysfs_create_bin_file(&dev->dev.kobj, attr); - if (retval) { - kfree(attr); - return; - } + if (!pdev->vpd) + return 0; - dev->vpd->attr = attr; + return a->attr.mode; } -void pcie_vpd_remove_sysfs_dev_files(struct pci_dev *dev) -{ - if (dev->vpd && dev->vpd->attr) { - sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr); - kfree(dev->vpd->attr); - } -} +const struct attribute_group vpd_attr_group = { + .bin_attrs = vpd_attrs, + .is_bin_visible = vpd_attr_is_visible, +}; int pci_vpd_find_tag(const u8 *buf, unsigned int off, unsigned int len, u8 rdt) { From patchwork Fri Apr 16 20:58:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?S3J6eXN6dG9mIFdpbGN6ee+/ve+/vXNraQ==?= X-Patchwork-Id: 12208797 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C526DC433B4 for ; Fri, 16 Apr 2021 20:59:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A77EA613E8 for ; Fri, 16 Apr 2021 20:59:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244489AbhDPU73 (ORCPT ); Fri, 16 Apr 2021 16:59:29 -0400 Received: from mail-ej1-f50.google.com ([209.85.218.50]:42510 "EHLO mail-ej1-f50.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244516AbhDPU72 (ORCPT ); Fri, 16 Apr 2021 16:59:28 -0400 Received: by mail-ej1-f50.google.com with SMTP id w23so28189131ejb.9 for ; Fri, 16 Apr 2021 13:59:03 -0700 (PDT) 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=fyxBBbYogYtY/0SVOORvDrmKaBSe5CZpPNs55FdoAQc=; b=LvK1GngrT3koJuPdWXOH3uuKla9Ipw4w8d8gi8DnIzVRGeZtR8xVU/qCeGFiK4vaq2 9dKDhQAaKnjO0Q5OrBsbrYf0ViOWGFqKDKMsfwLrtAwnTN+llr7MCiGj4ybCI+tty08K cJ5fX/Wfwb8HZ5V+CEVyTtPPv4Qftbqc98eZOzeZcPUlqJORHRSY7qsk666A+axCLRUM D6ZXlF90eb9OpdUaaVa8LSGjL0z0kggqjsqaZsLXaAv1MuBs7D9t6Lj6t8wa7WxRqcIV 7IV3RXzevn9Zymz683qroj7dYlDprVhSdVLD6Npmp8KRpcDGrS7fdzJBDWdGabgW8mET bVkA== X-Gm-Message-State: AOAM530NkOd3Kaxtnx85UvmISn3bWBRJnidhTEgTpjuzFTaPzYVJgtHB A7twxiXVl7JEBBDRpEfYRVg= X-Google-Smtp-Source: ABdhPJzdtoQIys0WGOg+wUN2UYqcu1h5ushBqwM9Qwk661qBQK70axSFUptB6pZUIYbyLGUvK0+cog== X-Received: by 2002:a17:906:b156:: with SMTP id bt22mr6097642ejb.181.1618606742781; Fri, 16 Apr 2021 13:59:02 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id n11sm5103864ejg.43.2021.04.16.13.59.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Apr 2021 13:59:02 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: =?utf-8?q?Pali_Roh=C3=A1r?= , "Oliver O'Halloran" , Greg Kroah-Hartman , Daniel Vetter , Joe Perches , Dan Williams , Mauro Carvalho Chehab , David Sterba , linux-pci@vger.kernel.org Subject: [PATCH 05/20] PCI: Convert dynamic "index" and "label" sysfs objects into static Date: Fri, 16 Apr 2021 20:58:41 +0000 Message-Id: <20210416205856.3234481-6-kw@linux.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210416205856.3234481-1-kw@linux.com> References: <20210416205856.3234481-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org The "index" and "label" sysfs objects, which are read-only attributes, that reside at the "/sys/bus/pci/devices/.../index" (or "acpi_index" with the ACPI _DSM support) and "/sys/bus/pci/devices/.../label" (when using either SMBIOS or ACPI _DSM) paths and allow for accessing the PCI device label, either from the BIOS (SMBIOS) or through the ACPI _DSM (Device-Specific Methods), and the device function index. At the moment, both the "index" (or "acpi_index") and "label" static sysfs objects are dynamically created when the pci_create_sysfs_dev_files() function executes that is part of the PCI sysfs objects initialisation and when a device is added: late_initcall() pci_sysfs_init() pci_create_sysfs_dev_files() pci_create_firmware_label_files() pci_create_acpi_index_label_files() <--------------------------- Depending on what is supported. pci_create_smbiosname_file() pci_bus_add_devices() pci_bus_add_device() pci_create_sysfs_dev_files() ... And dynamically removed when the pci_remove_sysfs_dev_files() function executes when the PCI device is stopped and removed: pci_stop_bus_device() pci_stop_dev() pci_remove_sysfs_dev_files() pci_remove_firmware_label_files() pci_remove_acpi_index_label_files() <--------------------------- Depending on what is supported. pci_remove_smbiosname_file() These attributes do not need to be created dynamically and removed dynamically, and thus there is no need to also manage their create and remove life cycle manually as the PCI driver core can do it when the device is either added or removed. Convert the "index", "acpi_index" and "label" sysfs objects created dynamically to static and use the .is_visible callback to check whether the SMBIOS or ACPI _DSM is available to decide if and which of the sysfs objects should be made available. Then, add a newly created attribute group to the existing group called "pci_dev_groups" to ensure that the "config" sysfs object would be automatically included when the PCI driver initializes. And also rename the device_has_dsm() function to device_has_acpi_name() to better relfect its purpose. Suggested-by: Oliver O'Halloran Suggested-by: Bjorn Helgaas Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-label.c | 212 +++++++++++++--------------------------- drivers/pci/pci-sysfs.c | 17 ++-- drivers/pci/pci.h | 13 +-- 3 files changed, 80 insertions(+), 162 deletions(-) diff --git a/drivers/pci/pci-label.c b/drivers/pci/pci-label.c index 781e45cf60d1..31fedc4920bb 100644 --- a/drivers/pci/pci-label.c +++ b/drivers/pci/pci-label.c @@ -33,6 +33,22 @@ #include #include "pci.h" +static bool device_has_acpi_name(struct device *dev) +{ +#ifdef CONFIG_ACPI + acpi_handle handle; + + handle = ACPI_HANDLE(dev); + if (!handle) + return false; + + return !!acpi_check_dsm(handle, &pci_acpi_dsm_guid, 0x2, + 1 << DSM_PCI_DEVICE_NAME); +#else + return false; +#endif +} + #ifdef CONFIG_DMI enum smbios_attr_enum { SMBIOS_ATTR_NONE = 0, @@ -45,13 +61,9 @@ static size_t find_smbios_instance_string(struct pci_dev *pdev, char *buf, { const struct dmi_device *dmi; struct dmi_dev_onboard *donboard; - int domain_nr; - int bus; - int devfn; - - domain_nr = pci_domain_nr(pdev->bus); - bus = pdev->bus->number; - devfn = pdev->devfn; + int domain_nr = pci_domain_nr(pdev->bus); + int bus = pdev->bus->number; + int devfn = pdev->devfn; dmi = NULL; while ((dmi = dmi_find_device(DMI_DEV_TYPE_DEV_ONBOARD, @@ -73,81 +85,57 @@ static size_t find_smbios_instance_string(struct pci_dev *pdev, char *buf, return strlen(dmi->name); } } - return 0; -} - -static umode_t smbios_instance_string_exist(struct kobject *kobj, - struct attribute *attr, int n) -{ - struct device *dev; - struct pci_dev *pdev; - dev = kobj_to_dev(kobj); - pdev = to_pci_dev(dev); - - return find_smbios_instance_string(pdev, NULL, SMBIOS_ATTR_NONE) ? - S_IRUGO : 0; + return 0; } -static ssize_t smbioslabel_show(struct device *dev, - struct device_attribute *attr, char *buf) +static ssize_t smbios_label_show(struct device *dev, + struct device_attribute *attr, char *buf) { - struct pci_dev *pdev; - pdev = to_pci_dev(dev); + struct pci_dev *pdev = to_pci_dev(dev); return find_smbios_instance_string(pdev, buf, SMBIOS_ATTR_LABEL_SHOW); } +static struct device_attribute dev_attr_smbios_label = __ATTR(label, 0444, + smbios_label_show, + NULL); -static ssize_t smbiosinstance_show(struct device *dev, - struct device_attribute *attr, char *buf) +static ssize_t index_show(struct device *dev, + struct device_attribute *attr, char *buf) { - struct pci_dev *pdev; - pdev = to_pci_dev(dev); + struct pci_dev *pdev = to_pci_dev(dev); return find_smbios_instance_string(pdev, buf, SMBIOS_ATTR_INSTANCE_SHOW); } +static DEVICE_ATTR_RO(index); -static struct device_attribute smbios_attr_label = { - .attr = {.name = "label", .mode = 0444}, - .show = smbioslabel_show, -}; - -static struct device_attribute smbios_attr_instance = { - .attr = {.name = "index", .mode = 0444}, - .show = smbiosinstance_show, -}; - -static struct attribute *smbios_attributes[] = { - &smbios_attr_label.attr, - &smbios_attr_instance.attr, +static struct attribute *smbios_attrs[] = { + &dev_attr_smbios_label.attr, + &dev_attr_index.attr, NULL, }; -static const struct attribute_group smbios_attr_group = { - .attrs = smbios_attributes, - .is_visible = smbios_instance_string_exist, -}; - -static int pci_create_smbiosname_file(struct pci_dev *pdev) +static umode_t smbios_attr_is_visible(struct kobject *kobj, + struct attribute *a, int n) { - return sysfs_create_group(&pdev->dev.kobj, &smbios_attr_group); -} + struct device *dev = kobj_to_dev(kobj); + struct pci_dev *pdev = to_pci_dev(dev); -static void pci_remove_smbiosname_file(struct pci_dev *pdev) -{ - sysfs_remove_group(&pdev->dev.kobj, &smbios_attr_group); -} -#else -static inline int pci_create_smbiosname_file(struct pci_dev *pdev) -{ - return -1; -} + if (device_has_acpi_name(dev)) + return 0; -static inline void pci_remove_smbiosname_file(struct pci_dev *pdev) -{ + if (!find_smbios_instance_string(pdev, NULL, SMBIOS_ATTR_NONE)) + return 0; + + return a->mode; } + +const struct attribute_group smbios_attr_group = { + .attrs = smbios_attrs, + .is_visible = smbios_attr_is_visible, +}; #endif #ifdef CONFIG_ACPI @@ -209,103 +197,41 @@ static int dsm_get_label(struct device *dev, char *buf, return len; } -static bool device_has_dsm(struct device *dev) -{ - acpi_handle handle; - - handle = ACPI_HANDLE(dev); - if (!handle) - return false; - - return !!acpi_check_dsm(handle, &pci_acpi_dsm_guid, 0x2, - 1 << DSM_PCI_DEVICE_NAME); -} - -static umode_t acpi_index_string_exist(struct kobject *kobj, - struct attribute *attr, int n) -{ - struct device *dev; - - dev = kobj_to_dev(kobj); - - if (device_has_dsm(dev)) - return S_IRUGO; - - return 0; -} - -static ssize_t acpilabel_show(struct device *dev, - struct device_attribute *attr, char *buf) +static ssize_t acpi_label_show(struct device *dev, + struct device_attribute *attr, char *buf) { return dsm_get_label(dev, buf, ACPI_ATTR_LABEL_SHOW); } +static struct device_attribute dev_attr_acpi_label = __ATTR(label, 0444, + acpi_label_show, + NULL); -static ssize_t acpiindex_show(struct device *dev, - struct device_attribute *attr, char *buf) +static ssize_t acpi_index_show(struct device *dev, + struct device_attribute *attr, char *buf) { return dsm_get_label(dev, buf, ACPI_ATTR_INDEX_SHOW); } +static DEVICE_ATTR_RO(acpi_index); -static struct device_attribute acpi_attr_label = { - .attr = {.name = "label", .mode = 0444}, - .show = acpilabel_show, -}; - -static struct device_attribute acpi_attr_index = { - .attr = {.name = "acpi_index", .mode = 0444}, - .show = acpiindex_show, -}; - -static struct attribute *acpi_attributes[] = { - &acpi_attr_label.attr, - &acpi_attr_index.attr, +static struct attribute *acpi_attrs[] = { + &dev_attr_acpi_label.attr, + &dev_attr_acpi_index.attr, NULL, }; -static const struct attribute_group acpi_attr_group = { - .attrs = acpi_attributes, - .is_visible = acpi_index_string_exist, -}; - -static int pci_create_acpi_index_label_files(struct pci_dev *pdev) +static umode_t acpi_attr_is_visible(struct kobject *kobj, + struct attribute *a, int n) { - return sysfs_create_group(&pdev->dev.kobj, &acpi_attr_group); -} + struct device *dev = kobj_to_dev(kobj); -static int pci_remove_acpi_index_label_files(struct pci_dev *pdev) -{ - sysfs_remove_group(&pdev->dev.kobj, &acpi_attr_group); - return 0; -} -#else -static inline int pci_create_acpi_index_label_files(struct pci_dev *pdev) -{ - return -1; -} + if (!device_has_acpi_name(dev)) + return 0; -static inline int pci_remove_acpi_index_label_files(struct pci_dev *pdev) -{ - return -1; + return a->mode; } -static inline bool device_has_dsm(struct device *dev) -{ - return false; -} +const struct attribute_group acpi_attr_group = { + .attrs = acpi_attrs, + .is_visible = acpi_attr_is_visible, +}; #endif - -void pci_create_firmware_label_files(struct pci_dev *pdev) -{ - if (device_has_dsm(&pdev->dev)) - pci_create_acpi_index_label_files(pdev); - else - pci_create_smbiosname_file(pdev); -} - -void pci_remove_firmware_label_files(struct pci_dev *pdev) -{ - if (device_has_dsm(&pdev->dev)) - pci_remove_acpi_index_label_files(pdev); - else - pci_remove_smbiosname_file(pdev); -} diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index 501d120e268b..6a8a44d44127 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -1380,18 +1380,10 @@ static const struct attribute_group pci_dev_reset_attr_group = { int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev) { - int retval; - if (!sysfs_initialized) return -EACCES; - retval = pci_create_resource_files(pdev); - if (retval) - return retval; - - pci_create_firmware_label_files(pdev); - - return 0; + return pci_create_resource_files(pdev); } /** @@ -1406,7 +1398,6 @@ void pci_remove_sysfs_dev_files(struct pci_dev *pdev) return; pci_remove_resource_files(pdev); - pci_remove_firmware_label_files(pdev); } static int __init pci_sysfs_init(void) @@ -1501,6 +1492,12 @@ const struct attribute_group *pci_dev_groups[] = { &pci_dev_rom_attr_group, &pci_dev_reset_attr_group, &vpd_attr_group, +#ifdef CONFIG_DMI + &smbios_attr_group, +#endif +#ifdef CONFIG_ACPI + &acpi_attr_group, +#endif NULL, }; diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 0dc52e60d03c..d6678124e8ea 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -21,16 +21,10 @@ bool pcie_cap_has_rtctl(const struct pci_dev *dev); int pci_create_sysfs_dev_files(struct pci_dev *pdev); void pci_remove_sysfs_dev_files(struct pci_dev *pdev); -#if !defined(CONFIG_DMI) && !defined(CONFIG_ACPI) -static inline void pci_create_firmware_label_files(struct pci_dev *pdev) -{ return; } -static inline void pci_remove_firmware_label_files(struct pci_dev *pdev) -{ return; } -#else -void pci_create_firmware_label_files(struct pci_dev *pdev); -void pci_remove_firmware_label_files(struct pci_dev *pdev); -#endif void pci_cleanup_rom(struct pci_dev *dev); +#ifdef CONFIG_DMI +extern const struct attribute_group smbios_attr_group; +#endif enum pci_mmap_api { PCI_MMAP_SYSFS, /* mmap on /sys/bus/pci/devices//resource */ @@ -695,6 +689,7 @@ static inline int pci_aer_raw_clear_status(struct pci_dev *dev) { return -EINVAL #ifdef CONFIG_ACPI int pci_acpi_program_hp_params(struct pci_dev *dev); +extern const struct attribute_group acpi_attr_group; #else static inline int pci_acpi_program_hp_params(struct pci_dev *dev) { From patchwork Fri Apr 16 20:58:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?S3J6eXN6dG9mIFdpbGN6ee+/ve+/vXNraQ==?= X-Patchwork-Id: 12208799 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8295BC433ED for ; Fri, 16 Apr 2021 20:59:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5A1C8613CE for ; Fri, 16 Apr 2021 20:59:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343817AbhDPU7b (ORCPT ); Fri, 16 Apr 2021 16:59:31 -0400 Received: from mail-ed1-f54.google.com ([209.85.208.54]:40814 "EHLO mail-ed1-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235963AbhDPU73 (ORCPT ); Fri, 16 Apr 2021 16:59:29 -0400 Received: by mail-ed1-f54.google.com with SMTP id o20so7548371edc.7 for ; Fri, 16 Apr 2021 13:59:04 -0700 (PDT) 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=JeUgz7b7rXLxynuNSGprjbs9WcXU7kskGSfcqquriAo=; b=rW8f28aLJSUFhI+S10vhOWIbEnH3nEsI6CporQAeTj6XH24orW0al/xDw9ZaVU9Ria wvkUrix2bt5+K3OIWy22NMbXKHb/JMvHtT1fdNa7J5cPJaRXR0TjK0+2lZ2KiTbXGLJU 1TjvxRqLuBmmWZ7UCjTmF+J9pZ+UQzNgeRwVOkEkbDpwkiUkWGlmXj5PsYxHY37+3MTO lMZAL188gobWjDTlfz2wFjKlEriJu/HBBzkbUAK5xQjwl/jA+0fV6/wnR7FyLYWL7Zin BEJjW5FB5ZAdHN1S5MMJxi75RBY97HUmFnYzIM+H9j8qRFUOl30xUq/t9z9Y7epfsoc2 BG2Q== X-Gm-Message-State: AOAM532z5VXAN95MqTDlfGQhO1UQPL0c/9MLrtkHIa6JRNJLaaGMBwqN 3OUsLT8DJz381q9mhtthVKE= X-Google-Smtp-Source: ABdhPJwVOkc6xSk37T2eHXQV6vW3+g077Om5a1OPNiB2/FC1ntKWNRjN+pE8ReGeKIrjo0EMw9X4/w== X-Received: by 2002:aa7:c1c8:: with SMTP id d8mr12245446edp.236.1618606743761; Fri, 16 Apr 2021 13:59:03 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id n11sm5103864ejg.43.2021.04.16.13.59.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Apr 2021 13:59:03 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: =?utf-8?q?Pali_Roh=C3=A1r?= , "Oliver O'Halloran" , Greg Kroah-Hartman , Daniel Vetter , Joe Perches , Dan Williams , Mauro Carvalho Chehab , David Sterba , linux-pci@vger.kernel.org Subject: [PATCH 06/20] sysfs: Introduce BIN_ATTR_ADMIN_RO and BIN_ATTR_ADMIN_RW Date: Fri, 16 Apr 2021 20:58:42 +0000 Message-Id: <20210416205856.3234481-7-kw@linux.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210416205856.3234481-1-kw@linux.com> References: <20210416205856.3234481-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org A very common use case is to limit read and/or write access to certain sysfs objects to only root with the expectation that the CAP_SYS_ADMIN capability is needed to access sensitive data exposed through such sysfs objects. The existing macros such as BIN_ATTR_RO and BIN_ATTR_RW are sadly inadequate given the specific need to limit access only to the root user, as they offer permissions that are too open e.g., 0444 and 0644, thus a lot of users of binary attributes with this specific use case, for example, the PCI "config", "rom" and "vps" sysfs objects, would opt to use the BIN_ATTR macro directly specifying 0400 or 0600 as needed. Add a new set of macros with an explicit "ADMIN" identifier catering to this specific use case that also follows the semantic of other existing macros such as e.g., BIN_ATTR_RO, BIN_ATTR_RW, BIN_ATTR_WO, etc. No functional change intended. Related: commit 60d360acddc5 ("driver-core: Introduce DEVICE_ATTR_ADMIN_{RO,RW}") Signed-off-by: Krzysztof Wilczyński --- include/linux/sysfs.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/include/linux/sysfs.h b/include/linux/sysfs.h index d76a1ddf83a3..9f423dfa8494 100644 --- a/include/linux/sysfs.h +++ b/include/linux/sysfs.h @@ -205,6 +205,13 @@ struct bin_attribute { .size = _size, \ } +#define __BIN_ATTR_RO_MODE(_name, _mode, _size) { \ + .attr = { .name = __stringify(_name), \ + .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \ + .read = _name##_read, \ + .size = _size, \ +} + #define __BIN_ATTR_WO(_name, _size) { \ .attr = { .name = __stringify(_name), .mode = 0200 }, \ .write = _name##_write, \ @@ -214,6 +221,14 @@ struct bin_attribute { #define __BIN_ATTR_RW(_name, _size) \ __BIN_ATTR(_name, 0644, _name##_read, _name##_write, _size) +#define __BIN_ATTR_RW_MODE(_name, _mode, _size) { \ + .attr = { .name = __stringify(_name), \ + .mode = VERIFY_OCTAL_PERMISSIONS(_mode) }, \ + .read = _name##_read, \ + .write = _name##_write, \ + .size = _size, \ +} + #define __BIN_ATTR_NULL __ATTR_NULL #define BIN_ATTR(_name, _mode, _read, _write, _size) \ @@ -223,12 +238,20 @@ struct bin_attribute bin_attr_##_name = __BIN_ATTR(_name, _mode, _read, \ #define BIN_ATTR_RO(_name, _size) \ struct bin_attribute bin_attr_##_name = __BIN_ATTR_RO(_name, _size) +#define BIN_ATTR_ADMIN_RO(_name, _size) \ +struct bin_attribute bin_attr_##_name = __BIN_ATTR_RO_MODE(_name, 0400, \ + _size) + #define BIN_ATTR_WO(_name, _size) \ struct bin_attribute bin_attr_##_name = __BIN_ATTR_WO(_name, _size) #define BIN_ATTR_RW(_name, _size) \ struct bin_attribute bin_attr_##_name = __BIN_ATTR_RW(_name, _size) +#define BIN_ATTR_ADMIN_RW(_name, _size) \ +struct bin_attribute bin_attr_##_name = __BIN_ATTR_RW_MODE(_name, 0600, \ + _size) + struct sysfs_ops { ssize_t (*show)(struct kobject *, struct attribute *, char *); ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t); From patchwork Fri Apr 16 20:58:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?S3J6eXN6dG9mIFdpbGN6ee+/ve+/vXNraQ==?= X-Patchwork-Id: 12208801 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1D7C0C43461 for ; Fri, 16 Apr 2021 20:59:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EB826613DA for ; Fri, 16 Apr 2021 20:59:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235963AbhDPU7c (ORCPT ); Fri, 16 Apr 2021 16:59:32 -0400 Received: from mail-ej1-f45.google.com ([209.85.218.45]:45720 "EHLO mail-ej1-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343620AbhDPU7a (ORCPT ); Fri, 16 Apr 2021 16:59:30 -0400 Received: by mail-ej1-f45.google.com with SMTP id sd23so35350244ejb.12 for ; Fri, 16 Apr 2021 13:59:05 -0700 (PDT) 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=Z2TcPPTUR1JDJUVX5/dAzIrKyjR/9xMdAu1o7StmrtM=; b=ZpIQbGUwMQSEnkWPv3jChUVhEjP4vzuzCki8J6pVJgzh6kmIfwAkaVEgi2iAxEP9/b So0YUwGDfXVCjIt9E+UYWaJUhWOUpDBOAYXbiBwC8Z7s4JSF/MVnfte8whGvYeV0C5nf /bQbs91w1e3K0+hY+7Cus/rqzIy3aXwIhzfaPh4Ohz6tbDPKM8lsZINyvlT6Mnp4Ma2Z vUxMBwFMJRkJriANBRAJE9PdzfkhL3aH7r1xLotfx3qf73GL+yjJPurugcCK7EFT9FKf 2UjiGxtUA79aACOrJ/+o5IBrcrlKnIxisIPm+GeaqvMS87v7n3hQ0ZM5iF5xmF8/9dV6 iMFg== X-Gm-Message-State: AOAM531jMpMmsEnHsdmv5MbiDNge4Db4XGP7ZJTkBAyPtHkjir5FduJz oicI2tEmDeLmKTRDftL3ykI= X-Google-Smtp-Source: ABdhPJzTB7kg2S4c1K5HXNO0iB0LtElArHPUEk7sgTzDL6f3bT+kjTJu7JSQZlA97TqLIMoWpW/WIg== X-Received: by 2002:a17:906:5949:: with SMTP id g9mr4626721ejr.356.1618606744704; Fri, 16 Apr 2021 13:59:04 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id n11sm5103864ejg.43.2021.04.16.13.59.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Apr 2021 13:59:04 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: =?utf-8?q?Pali_Roh=C3=A1r?= , "Oliver O'Halloran" , Greg Kroah-Hartman , Daniel Vetter , Joe Perches , Dan Williams , Mauro Carvalho Chehab , David Sterba , linux-pci@vger.kernel.org Subject: [PATCH 07/20] PCI: Convert PCI sysfs objects to use BIN_ATTR_ADMIN_RW macro Date: Fri, 16 Apr 2021 20:58:43 +0000 Message-Id: <20210416205856.3234481-8-kw@linux.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210416205856.3234481-1-kw@linux.com> References: <20210416205856.3234481-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org Move the PCI "config", "rom" and "vpd" sysfs objects that currently use an open coded BIN_ATTR macro to hide sensitive information that should only be accessed by the root user to the new BIN_ATTR_ADMIN_RW macro, and the BIN_ATTR_RW macro where otherwise appropriate. Rename the "read" and "write" functions so that they are in the format that is expected when using new macros as these have specific naming requirements from the accessor functions. No functional change intended. Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-sysfs.c | 32 ++++++++++++++++---------------- drivers/pci/vpd.c | 14 +++++++------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index 6a8a44d44127..07c3ddd7877e 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -666,9 +666,9 @@ static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr, } static DEVICE_ATTR_RO(boot_vga); -static ssize_t pci_read_config(struct file *filp, struct kobject *kobj, - struct bin_attribute *bin_attr, char *buf, - loff_t off, size_t count) +static ssize_t config_read(struct file *filp, struct kobject *kobj, + struct bin_attribute *bin_attr, char *buf, + loff_t off, size_t count) { struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); unsigned int size = 64; @@ -743,9 +743,9 @@ static ssize_t pci_read_config(struct file *filp, struct kobject *kobj, return count; } -static ssize_t pci_write_config(struct file *filp, struct kobject *kobj, - struct bin_attribute *bin_attr, char *buf, - loff_t off, size_t count) +static ssize_t config_write(struct file *filp, struct kobject *kobj, + struct bin_attribute *bin_attr, char *buf, + loff_t off, size_t count) { struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); unsigned int size = count; @@ -808,7 +808,7 @@ static ssize_t pci_write_config(struct file *filp, struct kobject *kobj, return count; } -static BIN_ATTR(config, 0644, pci_read_config, pci_write_config, 0); +static BIN_ATTR_RW(config, 0); static struct bin_attribute *pci_dev_config_attrs[] = { &bin_attr_config, @@ -1243,7 +1243,7 @@ void __weak pci_remove_resource_files(struct pci_dev *dev) { return; } #endif /** - * pci_write_rom - used to enable access to the PCI ROM display + * rom_write - used to enable access to the PCI ROM display * @filp: sysfs file * @kobj: kernel object handle * @bin_attr: struct bin_attribute for this file @@ -1253,9 +1253,9 @@ void __weak pci_remove_resource_files(struct pci_dev *dev) { return; } * * writing anything except 0 enables it */ -static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj, - struct bin_attribute *bin_attr, char *buf, - loff_t off, size_t count) +static ssize_t rom_write(struct file *filp, struct kobject *kobj, + struct bin_attribute *bin_attr, char *buf, + loff_t off, size_t count) { struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); @@ -1268,7 +1268,7 @@ static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj, } /** - * pci_read_rom - read a PCI ROM + * rom_read - read a PCI ROM * @filp: sysfs file * @kobj: kernel object handle * @bin_attr: struct bin_attribute for this file @@ -1279,9 +1279,9 @@ static ssize_t pci_write_rom(struct file *filp, struct kobject *kobj, * Put @count bytes starting at @off into @buf from the ROM in the PCI * device corresponding to @kobj. */ -static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj, - struct bin_attribute *bin_attr, char *buf, - loff_t off, size_t count) +static ssize_t rom_read(struct file *filp, struct kobject *kobj, + struct bin_attribute *bin_attr, char *buf, + loff_t off, size_t count) { struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); void __iomem *rom; @@ -1306,7 +1306,7 @@ static ssize_t pci_read_rom(struct file *filp, struct kobject *kobj, return count; } -static BIN_ATTR(rom, 0600, pci_read_rom, pci_write_rom, 0); +static BIN_ATTR_ADMIN_RW(rom, 0); static struct bin_attribute *pci_dev_rom_attrs[] = { &bin_attr_rom, diff --git a/drivers/pci/vpd.c b/drivers/pci/vpd.c index 3707671bc2f1..c8efb9ebdfd7 100644 --- a/drivers/pci/vpd.c +++ b/drivers/pci/vpd.c @@ -396,9 +396,9 @@ void pci_vpd_release(struct pci_dev *dev) kfree(dev->vpd); } -static ssize_t read_vpd_attr(struct file *filp, struct kobject *kobj, - struct bin_attribute *bin_attr, char *buf, - loff_t off, size_t count) +static ssize_t vpd_read(struct file *filp, struct kobject *kobj, + struct bin_attribute *bin_attr, char *buf, + loff_t off, size_t count) { struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); @@ -412,9 +412,9 @@ static ssize_t read_vpd_attr(struct file *filp, struct kobject *kobj, return pci_read_vpd(dev, off, count, buf); } -static ssize_t write_vpd_attr(struct file *filp, struct kobject *kobj, - struct bin_attribute *bin_attr, char *buf, - loff_t off, size_t count) +static ssize_t vpd_write(struct file *filp, struct kobject *kobj, + struct bin_attribute *bin_attr, char *buf, + loff_t off, size_t count) { struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); @@ -427,7 +427,7 @@ static ssize_t write_vpd_attr(struct file *filp, struct kobject *kobj, return pci_write_vpd(dev, off, count, buf); } -static BIN_ATTR(vpd, 0600, read_vpd_attr, write_vpd_attr, 0); +static BIN_ATTR_ADMIN_RW(vpd, 0); static struct bin_attribute *vpd_attrs[] = { &bin_attr_vpd, From patchwork Fri Apr 16 20:58:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?S3J6eXN6dG9mIFdpbGN6ee+/ve+/vXNraQ==?= X-Patchwork-Id: 12208803 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9D222C433B4 for ; Fri, 16 Apr 2021 20:59:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7115C613DA for ; Fri, 16 Apr 2021 20:59:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343620AbhDPU7d (ORCPT ); Fri, 16 Apr 2021 16:59:33 -0400 Received: from mail-ed1-f43.google.com ([209.85.208.43]:36354 "EHLO mail-ed1-f43.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343757AbhDPU7b (ORCPT ); Fri, 16 Apr 2021 16:59:31 -0400 Received: by mail-ed1-f43.google.com with SMTP id j12so8810222edy.3 for ; Fri, 16 Apr 2021 13:59:06 -0700 (PDT) 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=fNBXyVO7q0K7664XA5cwoU96TPUWCzHYpT1Rz6FpGu0=; b=aIAAoalPL3dX6zW1yBauQfHEEAZnruTHft+0worcil2PC99pNmccyT1bOEUyjUx2wp gxOvDjEXo0ASqlcD5q90OZegglmtyAkucr0GJUQTyUAiRJRgmviAM9m5vvjyk5Lmsh9e jTn5wxRWeqEdauW4tsCzkroHTwb3Ji/zPE6flFbZHyZA5cg4fcwclsSAiy//oKSwobcA zerCON2IGvagK8uVVC3NaJS8zSZOafZwQDaf+CoZSuQSbON2KBwPGCUuJ4uVmtffM36n aLYj41zWoVkiUNUwbekjZMVeDWXzUTCg5XG9JZqixr0XeN38ktU/nb7UOeuEWhHPbKKR 169Q== X-Gm-Message-State: AOAM532V60j5rlo6n3EKO76/Zq/mAvVV5c63X1SkRgqBMvdKeZTYuzHP 0rQVVmzOf54ixakkbiN+LAY= X-Google-Smtp-Source: ABdhPJyF4PYmWa4qBKIdX1Q+o75HSu70b2u8x2ta+hPVn2632Q3ZnZAXF3RvVGnqqOA0JiB+JxVtKQ== X-Received: by 2002:aa7:d2cc:: with SMTP id k12mr12169348edr.374.1618606745677; Fri, 16 Apr 2021 13:59:05 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id n11sm5103864ejg.43.2021.04.16.13.59.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Apr 2021 13:59:05 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: =?utf-8?q?Pali_Roh=C3=A1r?= , "Oliver O'Halloran" , Greg Kroah-Hartman , Daniel Vetter , Joe Perches , Dan Williams , Mauro Carvalho Chehab , David Sterba , linux-pci@vger.kernel.org Subject: [PATCH 08/20] PCI: Move to kstrtobool() to handle user input Date: Fri, 16 Apr 2021 20:58:44 +0000 Message-Id: <20210416205856.3234481-9-kw@linux.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210416205856.3234481-1-kw@linux.com> References: <20210416205856.3234481-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org A common use case for many PCI sysfs objects is to either enable some functionality or trigger an action following a write to a given attribute where the value is written would be simply either "1" or "0" synonymous with either disabling or enabling something. Parsing and validation of the input values are currently done using the kstrtoul() function to convert anything in the string buffer into an integer value - anything non-zero would be accepted as "enable" and zero simply as "disable". For a while now, the kernel offers another function called kstrtobool() which was created to parse common user inputs into a boolean value, so that a range of values such as "y", "n", "1", "0", "on" and "off" handled in a case-insensitive manner would yield a boolean true or false result accordingly after the input string has been parsed. Thus, move to kstrtobool() over kstrtoul() as it's a better fit for parsing user input, and it also implicitly offers a range check as only a finite amount of possible input values will be considered as valid. Where appropriate, move the user input parsing after checking for whether the "CAP_SYS_ADMIN" flag is set, as it makes more sense to first check whether the current user has the right permissions before accepting any input from such user. Related: commit d0f1fed29e6e ("Add a strtobool function matching semantics of existing in kernel equivalents") commit ef951599074b ("lib: move strtobool() to kstrtobool()") commit a81a5a17d44b ("lib: add "on"/"off" support to kstrtobool") commit 1404297ebf76 ("lib: update single-char callers of strtobool()") Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-sysfs.c | 90 ++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index 07c3ddd7877e..9ee003cc4375 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -64,12 +64,12 @@ static ssize_t broken_parity_status_store(struct device *dev, const char *buf, size_t count) { struct pci_dev *pdev = to_pci_dev(dev); - unsigned long val; + bool broken; - if (kstrtoul(buf, 0, &val) < 0) + if (kstrtobool(buf, &broken) < 0) return -EINVAL; - pdev->broken_parity_status = !!val; + pdev->broken_parity_status = !!broken; return count; } @@ -272,20 +272,20 @@ static ssize_t enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { struct pci_dev *pdev = to_pci_dev(dev); - unsigned long val; - ssize_t result = kstrtoul(buf, 0, &val); - - if (result < 0) - return result; + bool enable; + ssize_t result = 0; /* this can crash the machine when done on the "wrong" device */ if (!capable(CAP_SYS_ADMIN)) return -EPERM; + if (kstrtobool(buf, &enable) < 0) + return -EINVAL; + device_lock(dev); if (dev->driver) result = -EBUSY; - else if (val) + else if (enable) result = pci_enable_device(pdev); else if (pci_is_enabled(pdev)) pci_disable_device(pdev); @@ -312,14 +312,13 @@ static ssize_t numa_node_store(struct device *dev, size_t count) { struct pci_dev *pdev = to_pci_dev(dev); - int node, ret; + int node; if (!capable(CAP_SYS_ADMIN)) return -EPERM; - ret = kstrtoint(buf, 0, &node); - if (ret) - return ret; + if (kstrtoint(buf, 0, &node) < 0) + return -EINVAL; if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES) return -EINVAL; @@ -376,46 +375,46 @@ static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr, { struct pci_dev *pdev = to_pci_dev(dev); struct pci_bus *subordinate = pdev->subordinate; - unsigned long val; - - if (kstrtoul(buf, 0, &val) < 0) - return -EINVAL; + bool allowed; if (!capable(CAP_SYS_ADMIN)) return -EPERM; + if (kstrtobool(buf, &allowed) < 0) + return -EINVAL; + /* * "no_msi" and "bus_flags" only affect what happens when a driver * requests MSI or MSI-X. They don't affect any drivers that have * already requested MSI or MSI-X. */ if (!subordinate) { - pdev->no_msi = !val; + pdev->no_msi = !allowed; pci_info(pdev, "MSI/MSI-X %s for future drivers\n", - val ? "allowed" : "disallowed"); + allowed ? "allowed" : "disallowed"); return count; } - if (val) + if (allowed) subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI; else subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI; dev_info(&subordinate->dev, "MSI/MSI-X %s for future drivers of devices on this bus\n", - val ? "allowed" : "disallowed"); + allowed ? "allowed" : "disallowed"); return count; } static DEVICE_ATTR_RW(msi_bus); static ssize_t rescan_store(struct bus_type *bus, const char *buf, size_t count) { - unsigned long val; + bool rescan; struct pci_bus *b = NULL; - if (kstrtoul(buf, 0, &val) < 0) + if (kstrtobool(buf, &rescan) < 0) return -EINVAL; - if (val) { + if (rescan) { pci_lock_rescan_remove(); while ((b = pci_find_next_bus(b)) != NULL) pci_rescan_bus(b); @@ -443,13 +442,13 @@ static ssize_t dev_rescan_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { - unsigned long val; + bool rescan; struct pci_dev *pdev = to_pci_dev(dev); - if (kstrtoul(buf, 0, &val) < 0) + if (kstrtobool(buf, &rescan) < 0) return -EINVAL; - if (val) { + if (rescan) { pci_lock_rescan_remove(); pci_rescan_bus(pdev->bus); pci_unlock_rescan_remove(); @@ -462,12 +461,12 @@ static struct device_attribute dev_attr_dev_rescan = __ATTR(rescan, 0200, NULL, static ssize_t remove_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { - unsigned long val; + bool remove; - if (kstrtoul(buf, 0, &val) < 0) + if (kstrtobool(buf, &remove) < 0) return -EINVAL; - if (val && device_remove_file_self(dev, attr)) + if (remove && device_remove_file_self(dev, attr)) pci_stop_and_remove_bus_device_locked(to_pci_dev(dev)); return count; } @@ -478,13 +477,13 @@ static ssize_t bus_rescan_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { - unsigned long val; + bool rescan; struct pci_bus *bus = to_pci_bus(dev); - if (kstrtoul(buf, 0, &val) < 0) + if (kstrtobool(buf, &rescan) < 0) return -EINVAL; - if (val) { + if (rescan) { pci_lock_rescan_remove(); if (!pci_is_root_bus(bus) && list_empty(&bus->devices)) pci_rescan_bus_bridge_resize(bus->self); @@ -503,12 +502,12 @@ static ssize_t d3cold_allowed_store(struct device *dev, const char *buf, size_t count) { struct pci_dev *pdev = to_pci_dev(dev); - unsigned long val; + bool allowed; - if (kstrtoul(buf, 0, &val) < 0) + if (kstrtobool(buf, &allowed) < 0) return -EINVAL; - pdev->d3cold_allowed = !!val; + pdev->d3cold_allowed = !!allowed; if (pdev->d3cold_allowed) pci_d3cold_enable(pdev); else @@ -1257,12 +1256,13 @@ static ssize_t rom_write(struct file *filp, struct kobject *kobj, struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count) { + bool allowed; struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); - if ((off == 0) && (*buf == '0') && (count == 2)) - pdev->rom_attr_enabled = 0; - else - pdev->rom_attr_enabled = 1; + if (kstrtobool(buf, &allowed) < 0) + return -EINVAL; + + pdev->rom_attr_enabled = !!allowed; return count; } @@ -1337,14 +1337,14 @@ static const struct attribute_group pci_dev_rom_attr_group = { static ssize_t reset_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { + bool reset; struct pci_dev *pdev = to_pci_dev(dev); - unsigned long val; - ssize_t result = kstrtoul(buf, 0, &val); + ssize_t result; - if (result < 0) - return result; + if (kstrtobool(buf, &reset) < 0) + return -EINVAL; - if (val != 1) + if (!reset) return -EINVAL; pm_runtime_get_sync(dev); From patchwork Fri Apr 16 20:58:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?S3J6eXN6dG9mIFdpbGN6ee+/ve+/vXNraQ==?= X-Patchwork-Id: 12208805 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C5AD3C43462 for ; Fri, 16 Apr 2021 20:59:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9F370613ED for ; Fri, 16 Apr 2021 20:59:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236201AbhDPU7d (ORCPT ); Fri, 16 Apr 2021 16:59:33 -0400 Received: from mail-ej1-f50.google.com ([209.85.218.50]:41625 "EHLO mail-ej1-f50.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244516AbhDPU7d (ORCPT ); Fri, 16 Apr 2021 16:59:33 -0400 Received: by mail-ej1-f50.google.com with SMTP id mh2so22452698ejb.8 for ; Fri, 16 Apr 2021 13:59:07 -0700 (PDT) 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=YQdDvvb/NUYE/KyNp9dJGeLDR6zwYttostGpiT5NeHA=; b=HG1gQ0ohFvMLGx/zSw7Vu8d4pKpf71bajKeol+/UuM7NI9HrudlL7XCv9GcJxfYj3V 0b3Sh5/JYdlxHKLeZ7UMmbVCmRBgB0lRP07f3cInIig4CJ/7/9XaWKSEhy909xW7DtBy ZPd175yy+b+wmy8Yhm5QJXNlM1n10PT9OEA9nlsW3TJr1xjbwRDCkRYuCDxLRzlxydo7 w0MvkaYI8Ibxf6z0lv+LDddQ4CjXmymkl+hDmdH4qvnzIuugFJLS0r/J5frHEj401rcy 1dDWix1u02q6/BbD0p/g1DFZYcACPmLGZprTJLJokM9VKi95uUVoryadvh8IytD4xrwr XHwA== X-Gm-Message-State: AOAM532MLWUCTH6FPBd4iSeftB/GWySHPkRUgSfwOhViAZpsjvdfGobk Kec3wjgm9iphQlP+FG4paSQ= X-Google-Smtp-Source: ABdhPJx8bamBJCIDwQBg4K0JfXb5oUdkc/gxlzchpkbCvcBb8AhvxoGUeZnsRA+abmS4xglGmSwlZg== X-Received: by 2002:a17:906:5fc2:: with SMTP id k2mr10338549ejv.354.1618606746711; Fri, 16 Apr 2021 13:59:06 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id n11sm5103864ejg.43.2021.04.16.13.59.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Apr 2021 13:59:06 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: =?utf-8?q?Pali_Roh=C3=A1r?= , "Oliver O'Halloran" , Greg Kroah-Hartman , Daniel Vetter , Joe Perches , Dan Williams , Mauro Carvalho Chehab , David Sterba , linux-pci@vger.kernel.org Subject: [PATCH 09/20] PCI: Use sysfs_emit() and sysfs_emit_at() in "show" functions Date: Fri, 16 Apr 2021 20:58:45 +0000 Message-Id: <20210416205856.3234481-10-kw@linux.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210416205856.3234481-1-kw@linux.com> References: <20210416205856.3234481-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org The sysfs_emit() and sysfs_emit_at() functions were introduced to make it less ambiguous which function is preferred when writing to the output buffer in a device attribute's "show" callback [1]. Convert the PCI sysfs object "show" functions from sprintf(), snprintf() and scnprintf() to sysfs_emit() and sysfs_emit_at() accordingly, as the latter is aware of the PAGE_SIZE buffer and correctly returns the number of bytes written into the buffer. No functional change intended. [1] Documentation/filesystems/sysfs.rst Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-label.c | 31 ++++++++------- drivers/pci/pci-sysfs.c | 88 ++++++++++++++++++----------------------- 2 files changed, 56 insertions(+), 63 deletions(-) diff --git a/drivers/pci/pci-label.c b/drivers/pci/pci-label.c index 31fedc4920bb..0ef863e598ad 100644 --- a/drivers/pci/pci-label.c +++ b/drivers/pci/pci-label.c @@ -74,13 +74,11 @@ static size_t find_smbios_instance_string(struct pci_dev *pdev, char *buf, donboard->devfn == devfn) { if (buf) { if (attribute == SMBIOS_ATTR_INSTANCE_SHOW) - return scnprintf(buf, PAGE_SIZE, - "%d\n", - donboard->instance); + return sysfs_emit(buf, "%d\n", + donboard->instance); else if (attribute == SMBIOS_ATTR_LABEL_SHOW) - return scnprintf(buf, PAGE_SIZE, - "%s\n", - dmi->name); + return sysfs_emit(buf, "%s\n", + dmi->name); } return strlen(dmi->name); } @@ -144,14 +142,17 @@ enum acpi_attr_enum { ACPI_ATTR_INDEX_SHOW, }; -static void dsm_label_utf16s_to_utf8s(union acpi_object *obj, char *buf) +static int dsm_label_utf16s_to_utf8s(union acpi_object *obj, char *buf) { int len; + len = utf16s_to_utf8s((const wchar_t *)obj->buffer.pointer, obj->buffer.length, UTF16_LITTLE_ENDIAN, buf, PAGE_SIZE); buf[len] = '\n'; + + return strlen(buf); } static int dsm_get_label(struct device *dev, char *buf, @@ -159,7 +160,7 @@ static int dsm_get_label(struct device *dev, char *buf, { acpi_handle handle; union acpi_object *obj, *tmp; - int len = -1; + int len = 0; handle = ACPI_HANDLE(dev); if (!handle) @@ -181,20 +182,22 @@ static int dsm_get_label(struct device *dev, char *buf, * this entry must return a null string. */ if (attr == ACPI_ATTR_INDEX_SHOW) { - scnprintf(buf, PAGE_SIZE, "%llu\n", tmp->integer.value); + len = sysfs_emit(buf, "%llu\n", tmp->integer.value); } else if (attr == ACPI_ATTR_LABEL_SHOW) { if (tmp[1].type == ACPI_TYPE_STRING) - scnprintf(buf, PAGE_SIZE, "%s\n", - tmp[1].string.pointer); + len = sysfs_emit(buf, "%s\n", + tmp[1].string.pointer); else if (tmp[1].type == ACPI_TYPE_BUFFER) - dsm_label_utf16s_to_utf8s(tmp + 1, buf); + len = dsm_label_utf16s_to_utf8s(tmp + 1, buf); } - len = strlen(buf) > 0 ? strlen(buf) : -1; } ACPI_FREE(obj); - return len; + if (len > 0) + return len; + + return -1; } static ssize_t acpi_label_show(struct device *dev, diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index 9ee003cc4375..c47c30cf063e 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -39,7 +39,7 @@ field##_show(struct device *dev, struct device_attribute *attr, char *buf) \ struct pci_dev *pdev; \ \ pdev = to_pci_dev(dev); \ - return sprintf(buf, format_string, pdev->field); \ + return sysfs_emit(buf, format_string, pdev->field); \ } \ static DEVICE_ATTR_RO(field) @@ -56,7 +56,7 @@ static ssize_t broken_parity_status_show(struct device *dev, char *buf) { struct pci_dev *pdev = to_pci_dev(dev); - return sprintf(buf, "%u\n", pdev->broken_parity_status); + return sysfs_emit(buf, "%u\n", pdev->broken_parity_status); } static ssize_t broken_parity_status_store(struct device *dev, @@ -129,7 +129,7 @@ static ssize_t power_state_show(struct device *dev, { struct pci_dev *pdev = to_pci_dev(dev); - return sprintf(buf, "%s\n", pci_power_name(pdev->current_state)); + return sysfs_emit(buf, "%s\n", pci_power_name(pdev->current_state)); } static DEVICE_ATTR_RO(power_state); @@ -138,10 +138,10 @@ static ssize_t resource_show(struct device *dev, struct device_attribute *attr, char *buf) { struct pci_dev *pci_dev = to_pci_dev(dev); - char *str = buf; int i; int max; resource_size_t start, end; + size_t len = 0; if (pci_dev->subordinate) max = DEVICE_COUNT_RESOURCE; @@ -151,12 +151,12 @@ static ssize_t resource_show(struct device *dev, struct device_attribute *attr, for (i = 0; i < max; i++) { struct resource *res = &pci_dev->resource[i]; pci_resource_to_user(pci_dev, i, res, &start, &end); - str += sprintf(str, "0x%016llx 0x%016llx 0x%016llx\n", - (unsigned long long)start, - (unsigned long long)end, - (unsigned long long)res->flags); + len += sysfs_emit_at(buf, len, "0x%016llx 0x%016llx 0x%016llx\n", + (unsigned long long)start, + (unsigned long long)end, + (unsigned long long)res->flags); } - return (str - buf); + return len; } static DEVICE_ATTR_RO(resource); @@ -165,8 +165,8 @@ static ssize_t max_link_speed_show(struct device *dev, { struct pci_dev *pdev = to_pci_dev(dev); - return sprintf(buf, "%s\n", - pci_speed_string(pcie_get_speed_cap(pdev))); + return sysfs_emit(buf, "%s\n", + pci_speed_string(pcie_get_speed_cap(pdev))); } static DEVICE_ATTR_RO(max_link_speed); @@ -175,7 +175,7 @@ static ssize_t max_link_width_show(struct device *dev, { struct pci_dev *pdev = to_pci_dev(dev); - return sprintf(buf, "%u\n", pcie_get_width_cap(pdev)); + return sysfs_emit(buf, "%u\n", pcie_get_width_cap(pdev)); } static DEVICE_ATTR_RO(max_link_width); @@ -183,17 +183,11 @@ static ssize_t current_link_speed_show(struct device *dev, struct device_attribute *attr, char *buf) { struct pci_dev *pci_dev = to_pci_dev(dev); - u16 linkstat; - int err; enum pci_bus_speed speed; - err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat); - if (err) - return -EINVAL; - - speed = pcie_link_speed[linkstat & PCI_EXP_LNKSTA_CLS]; + pcie_bandwidth_available(pci_dev, NULL, &speed, NULL); - return sprintf(buf, "%s\n", pci_speed_string(speed)); + return sysfs_emit(buf, "%s\n", pci_speed_string(speed)); } static DEVICE_ATTR_RO(current_link_speed); @@ -201,15 +195,11 @@ static ssize_t current_link_width_show(struct device *dev, struct device_attribute *attr, char *buf) { struct pci_dev *pci_dev = to_pci_dev(dev); - u16 linkstat; - int err; + enum pcie_link_width width; - err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat); - if (err) - return -EINVAL; + pcie_bandwidth_available(pci_dev, NULL, NULL, &width); - return sprintf(buf, "%u\n", - (linkstat & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT); + return sysfs_emit(buf, "%u\n", width); } static DEVICE_ATTR_RO(current_link_width); @@ -225,7 +215,7 @@ static ssize_t secondary_bus_number_show(struct device *dev, if (err) return -EINVAL; - return sprintf(buf, "%u\n", sec_bus); + return sysfs_emit(buf, "%u\n", sec_bus); } static DEVICE_ATTR_RO(secondary_bus_number); @@ -241,7 +231,7 @@ static ssize_t subordinate_bus_number_show(struct device *dev, if (err) return -EINVAL; - return sprintf(buf, "%u\n", sub_bus); + return sysfs_emit(buf, "%u\n", sub_bus); } static DEVICE_ATTR_RO(subordinate_bus_number); @@ -251,7 +241,7 @@ static ssize_t ari_enabled_show(struct device *dev, { struct pci_dev *pci_dev = to_pci_dev(dev); - return sprintf(buf, "%u\n", pci_ari_enabled(pci_dev->bus)); + return sysfs_emit(buf, "%u\n", pci_ari_enabled(pci_dev->bus)); } static DEVICE_ATTR_RO(ari_enabled); @@ -260,11 +250,11 @@ static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, { struct pci_dev *pci_dev = to_pci_dev(dev); - return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n", - pci_dev->vendor, pci_dev->device, - pci_dev->subsystem_vendor, pci_dev->subsystem_device, - (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8), - (u8)(pci_dev->class)); + return sysfs_emit(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n", + pci_dev->vendor, pci_dev->device, + pci_dev->subsystem_vendor, pci_dev->subsystem_device, + (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8), + (u8)(pci_dev->class)); } static DEVICE_ATTR_RO(modalias); @@ -302,7 +292,7 @@ static ssize_t enable_show(struct device *dev, struct device_attribute *attr, struct pci_dev *pdev; pdev = to_pci_dev(dev); - return sprintf(buf, "%u\n", atomic_read(&pdev->enable_cnt)); + return sysfs_emit(buf, "%u\n", atomic_read(&pdev->enable_cnt)); } static DEVICE_ATTR_RW(enable); @@ -337,7 +327,7 @@ static ssize_t numa_node_store(struct device *dev, static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr, char *buf) { - return sprintf(buf, "%d\n", dev->numa_node); + return sysfs_emit(buf, "%d\n", dev->numa_node); } static DEVICE_ATTR_RW(numa_node); #endif @@ -347,7 +337,7 @@ static ssize_t dma_mask_bits_show(struct device *dev, { struct pci_dev *pdev = to_pci_dev(dev); - return sprintf(buf, "%d\n", fls64(pdev->dma_mask)); + return sysfs_emit(buf, "%d\n", fls64(pdev->dma_mask)); } static DEVICE_ATTR_RO(dma_mask_bits); @@ -355,7 +345,7 @@ static ssize_t consistent_dma_mask_bits_show(struct device *dev, struct device_attribute *attr, char *buf) { - return sprintf(buf, "%d\n", fls64(dev->coherent_dma_mask)); + return sysfs_emit(buf, "%d\n", fls64(dev->coherent_dma_mask)); } static DEVICE_ATTR_RO(consistent_dma_mask_bits); @@ -365,9 +355,9 @@ static ssize_t msi_bus_show(struct device *dev, struct device_attribute *attr, struct pci_dev *pdev = to_pci_dev(dev); struct pci_bus *subordinate = pdev->subordinate; - return sprintf(buf, "%u\n", subordinate ? - !(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) - : !pdev->no_msi); + return sysfs_emit(buf, "%u\n", subordinate ? + !(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) + : !pdev->no_msi); } static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr, @@ -522,7 +512,7 @@ static ssize_t d3cold_allowed_show(struct device *dev, struct device_attribute *attr, char *buf) { struct pci_dev *pdev = to_pci_dev(dev); - return sprintf(buf, "%u\n", pdev->d3cold_allowed); + return sysfs_emit(buf, "%u\n", pdev->d3cold_allowed); } static DEVICE_ATTR_RW(d3cold_allowed); #endif @@ -536,7 +526,7 @@ static ssize_t devspec_show(struct device *dev, if (np == NULL) return 0; - return sprintf(buf, "%pOF", np); + return sysfs_emit(buf, "%pOF", np); } static DEVICE_ATTR_RO(devspec); #endif @@ -582,7 +572,7 @@ static ssize_t driver_override_show(struct device *dev, ssize_t len; device_lock(dev); - len = scnprintf(buf, PAGE_SIZE, "%s\n", pdev->driver_override); + len = sysfs_emit(buf, "%s\n", pdev->driver_override); device_unlock(dev); return len; } @@ -657,11 +647,11 @@ static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr, struct pci_dev *vga_dev = vga_default_device(); if (vga_dev) - return sprintf(buf, "%u\n", (pdev == vga_dev)); + return sysfs_emit(buf, "%u\n", (pdev == vga_dev)); - return sprintf(buf, "%u\n", - !!(pdev->resource[PCI_ROM_RESOURCE].flags & - IORESOURCE_ROM_SHADOW)); + return sysfs_emit(buf, "%u\n", + !!(pdev->resource[PCI_ROM_RESOURCE].flags & + IORESOURCE_ROM_SHADOW)); } static DEVICE_ATTR_RO(boot_vga); From patchwork Fri Apr 16 20:58:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?S3J6eXN6dG9mIFdpbGN6ee+/ve+/vXNraQ==?= X-Patchwork-Id: 12208811 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D42B4C43460 for ; Fri, 16 Apr 2021 20:59:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A33AA613EA for ; Fri, 16 Apr 2021 20:59:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244639AbhDPU7e (ORCPT ); Fri, 16 Apr 2021 16:59:34 -0400 Received: from mail-ed1-f41.google.com ([209.85.208.41]:34747 "EHLO mail-ed1-f41.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343707AbhDPU7e (ORCPT ); Fri, 16 Apr 2021 16:59:34 -0400 Received: by mail-ed1-f41.google.com with SMTP id i3so8148397edt.1 for ; Fri, 16 Apr 2021 13:59:08 -0700 (PDT) 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=chnXS5vlnNEAjx2DkDD/rfaMsl7xw9YzjK1dkczZPx8=; b=kH3Os25UUhdx11QsfHNxfOng7MmsXfFHE4OL8NrlaAxkRGctT5LfpiyZ2UogxWr5xR m1KPEdjMcmH2+QOlpUwN/R2yKeM3PnkvYgxHGKOM5suJfjASyR8bdDmWsSXENX+gK6zI aUN0s590gPEDBqzd6+GZ86TA5tKKjYp4B3Jzr2UO/BvvXHDFCoJHt5I1xkkKVjdG+gGp 9iEtlMmzEHMgnpPHjnJajobfELRWWAraMuuQLsl5h2OvFRJQIMT3UNraD8IYdxNZsBpD aZyD4DZpEskWpG+6XHkmHHALQrhjVrex5EDhE8kUmS/NbcQsY1MKKyKmb/1koP4N6ZxD L/Ig== X-Gm-Message-State: AOAM530iGN82cqT6XURr5A8t5/tLZZF7+i9DRSXeFo8ckC/zS0TdQmkI 1YmrVswMZ3U1IqDGD7+uLF4= X-Google-Smtp-Source: ABdhPJztiek1E12EuWfxeukL0dkQLP4boqPMtzKwltOjoIOU/t04ZB81cjksT3H1cgiDK34q4DQKug== X-Received: by 2002:a05:6402:40ca:: with SMTP id z10mr11995612edb.215.1618606747791; Fri, 16 Apr 2021 13:59:07 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id n11sm5103864ejg.43.2021.04.16.13.59.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Apr 2021 13:59:07 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: =?utf-8?q?Pali_Roh=C3=A1r?= , "Oliver O'Halloran" , Greg Kroah-Hartman , Daniel Vetter , Joe Perches , Dan Williams , Mauro Carvalho Chehab , David Sterba , linux-pci@vger.kernel.org Subject: [PATCH 10/20] PCI: Update style to be more consistent Date: Fri, 16 Apr 2021 20:58:46 +0000 Message-Id: <20210416205856.3234481-11-kw@linux.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210416205856.3234481-1-kw@linux.com> References: <20210416205856.3234481-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org The number of sysfs object added to the PCI device grew over time, and with each addition, the style of the code in the pci-sysfc.c file also changed. Thus, update to coding style in the file, so that it's consistent: - Update variable naming - Sort variables in the order of use - Update functions signatures to be more aligned - Correct white spaces and indentation - Remove ternary operator in favour of the if-else style - Update brackets and if-statements to better match kernel code style Also, rename the .is_visible and .bin_is_visible callbacks functions so that these end with the "_is_visible" suffix, rather than "_are_visible" to be consistent with rest of the code in the kernel. No functional change intended. Suggested-by: Bjorn Helgaas Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-sysfs.c | 470 +++++++++++++++++++++------------------- 1 file changed, 251 insertions(+), 219 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index c47c30cf063e..96302b63f6c5 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -12,7 +12,6 @@ * Modeled after usb's driverfs.c */ - #include #include #include @@ -36,9 +35,8 @@ static int sysfs_initialized; /* = 0 */ static ssize_t \ field##_show(struct device *dev, struct device_attribute *attr, char *buf) \ { \ - struct pci_dev *pdev; \ + struct pci_dev *pdev = to_pci_dev(dev); \ \ - pdev = to_pci_dev(dev); \ return sysfs_emit(buf, format_string, pdev->field); \ } \ static DEVICE_ATTR_RO(field) @@ -56,6 +54,7 @@ static ssize_t broken_parity_status_show(struct device *dev, char *buf) { struct pci_dev *pdev = to_pci_dev(dev); + return sysfs_emit(buf, "%u\n", pdev->broken_parity_status); } @@ -63,8 +62,8 @@ static ssize_t broken_parity_status_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { - struct pci_dev *pdev = to_pci_dev(dev); bool broken; + struct pci_dev *pdev = to_pci_dev(dev); if (kstrtobool(buf, &broken) < 0) return -EINVAL; @@ -79,12 +78,17 @@ static ssize_t pci_dev_show_local_cpu(struct device *dev, bool list, struct device_attribute *attr, char *buf) { const struct cpumask *mask; - #ifdef CONFIG_NUMA - mask = (dev_to_node(dev) == -1) ? cpu_online_mask : - cpumask_of_node(dev_to_node(dev)); + int node = dev_to_node(dev); + + if (node == NUMA_NO_NODE) + mask = cpu_online_mask; + else + mask = cpumask_of_node(node); #else - mask = cpumask_of_pcibus(to_pci_dev(dev)->bus); + struct pci_dev *pdev = to_pci_dev(dev); + + mask = cpumask_of_pcibus(pdev->bus); #endif return cpumap_print_to_pagebuf(list, buf, mask); } @@ -109,7 +113,8 @@ static DEVICE_ATTR_RO(local_cpulist); static ssize_t cpuaffinity_show(struct device *dev, struct device_attribute *attr, char *buf) { - const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev)); + struct pci_bus *bus = to_pci_bus(dev); + const struct cpumask *cpumask = cpumask_of_pcibus(bus); return cpumap_print_to_pagebuf(false, buf, cpumask); } @@ -118,7 +123,8 @@ static DEVICE_ATTR_RO(cpuaffinity); static ssize_t cpulistaffinity_show(struct device *dev, struct device_attribute *attr, char *buf) { - const struct cpumask *cpumask = cpumask_of_pcibus(to_pci_bus(dev)); + struct pci_bus *bus = to_pci_bus(dev); + const struct cpumask *cpumask = cpumask_of_pcibus(bus); return cpumap_print_to_pagebuf(true, buf, cpumask); } @@ -134,28 +140,28 @@ static ssize_t power_state_show(struct device *dev, static DEVICE_ATTR_RO(power_state); /* show resources */ -static ssize_t resource_show(struct device *dev, struct device_attribute *attr, - char *buf) +static ssize_t resource_show(struct device *dev, + struct device_attribute *attr, char *buf) { - struct pci_dev *pci_dev = to_pci_dev(dev); - int i; - int max; + int i, max; + struct pci_dev *pdev = to_pci_dev(dev); + struct resource *res; resource_size_t start, end; size_t len = 0; - if (pci_dev->subordinate) + max = PCI_BRIDGE_RESOURCES; + if (pdev->subordinate) max = DEVICE_COUNT_RESOURCE; - else - max = PCI_BRIDGE_RESOURCES; for (i = 0; i < max; i++) { - struct resource *res = &pci_dev->resource[i]; - pci_resource_to_user(pci_dev, i, res, &start, &end); + res = &pdev->resource[i]; + pci_resource_to_user(pdev, i, res, &start, &end); len += sysfs_emit_at(buf, len, "0x%016llx 0x%016llx 0x%016llx\n", (unsigned long long)start, (unsigned long long)end, (unsigned long long)res->flags); } + return len; } static DEVICE_ATTR_RO(resource); @@ -182,10 +188,10 @@ static DEVICE_ATTR_RO(max_link_width); static ssize_t current_link_speed_show(struct device *dev, struct device_attribute *attr, char *buf) { - struct pci_dev *pci_dev = to_pci_dev(dev); + struct pci_dev *pdev = to_pci_dev(dev); enum pci_bus_speed speed; - pcie_bandwidth_available(pci_dev, NULL, &speed, NULL); + pcie_bandwidth_available(pdev, NULL, &speed, NULL); return sysfs_emit(buf, "%s\n", pci_speed_string(speed)); } @@ -194,10 +200,10 @@ static DEVICE_ATTR_RO(current_link_speed); static ssize_t current_link_width_show(struct device *dev, struct device_attribute *attr, char *buf) { - struct pci_dev *pci_dev = to_pci_dev(dev); + struct pci_dev *pdev = to_pci_dev(dev); enum pcie_link_width width; - pcie_bandwidth_available(pci_dev, NULL, NULL, &width); + pcie_bandwidth_available(pdev, NULL, NULL, &width); return sysfs_emit(buf, "%u\n", width); } @@ -207,11 +213,11 @@ static ssize_t secondary_bus_number_show(struct device *dev, struct device_attribute *attr, char *buf) { - struct pci_dev *pci_dev = to_pci_dev(dev); + struct pci_dev *pdev = to_pci_dev(dev); u8 sec_bus; int err; - err = pci_read_config_byte(pci_dev, PCI_SECONDARY_BUS, &sec_bus); + err = pci_read_config_byte(pdev, PCI_SECONDARY_BUS, &sec_bus); if (err) return -EINVAL; @@ -223,11 +229,11 @@ static ssize_t subordinate_bus_number_show(struct device *dev, struct device_attribute *attr, char *buf) { - struct pci_dev *pci_dev = to_pci_dev(dev); + struct pci_dev *pdev = to_pci_dev(dev); u8 sub_bus; int err; - err = pci_read_config_byte(pci_dev, PCI_SUBORDINATE_BUS, &sub_bus); + err = pci_read_config_byte(pdev, PCI_SUBORDINATE_BUS, &sub_bus); if (err) return -EINVAL; @@ -236,34 +242,34 @@ static ssize_t subordinate_bus_number_show(struct device *dev, static DEVICE_ATTR_RO(subordinate_bus_number); static ssize_t ari_enabled_show(struct device *dev, - struct device_attribute *attr, - char *buf) + struct device_attribute *attr, char *buf) { - struct pci_dev *pci_dev = to_pci_dev(dev); + struct pci_dev *pdev = to_pci_dev(dev); - return sysfs_emit(buf, "%u\n", pci_ari_enabled(pci_dev->bus)); + return sysfs_emit(buf, "%u\n", pci_ari_enabled(pdev->bus)); } static DEVICE_ATTR_RO(ari_enabled); -static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, - char *buf) +static ssize_t modalias_show(struct device *dev, + struct device_attribute *attr, char *buf) { - struct pci_dev *pci_dev = to_pci_dev(dev); + struct pci_dev *pdev = to_pci_dev(dev); return sysfs_emit(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n", - pci_dev->vendor, pci_dev->device, - pci_dev->subsystem_vendor, pci_dev->subsystem_device, - (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8), - (u8)(pci_dev->class)); + pdev->vendor, pdev->device, + pdev->subsystem_vendor, pdev->subsystem_device, + (u8)(pdev->class >> 16), (u8)(pdev->class >> 8), + (u8)(pdev->class)); } static DEVICE_ATTR_RO(modalias); -static ssize_t enable_store(struct device *dev, struct device_attribute *attr, - const char *buf, size_t count) +static ssize_t enable_store(struct device *dev, + struct device_attribute *attr, const char *buf, + size_t count) { - struct pci_dev *pdev = to_pci_dev(dev); bool enable; - ssize_t result = 0; + struct pci_dev *pdev = to_pci_dev(dev); + ssize_t ret = 0; /* this can crash the machine when done on the "wrong" device */ if (!capable(CAP_SYS_ADMIN)) @@ -274,24 +280,26 @@ static ssize_t enable_store(struct device *dev, struct device_attribute *attr, device_lock(dev); if (dev->driver) - result = -EBUSY; + ret = -EBUSY; else if (enable) - result = pci_enable_device(pdev); + ret = pci_enable_device(pdev); else if (pci_is_enabled(pdev)) pci_disable_device(pdev); else - result = -EIO; + ret = -EIO; device_unlock(dev); - return result < 0 ? result : count; + if (ret < 0) + return ret; + + return count; } -static ssize_t enable_show(struct device *dev, struct device_attribute *attr, - char *buf) +static ssize_t enable_show(struct device *dev, + struct device_attribute *attr, char *buf) { - struct pci_dev *pdev; + struct pci_dev *pdev = to_pci_dev(dev); - pdev = to_pci_dev(dev); return sysfs_emit(buf, "%u\n", atomic_read(&pdev->enable_cnt)); } static DEVICE_ATTR_RW(enable); @@ -301,8 +309,8 @@ static ssize_t numa_node_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { - struct pci_dev *pdev = to_pci_dev(dev); int node; + struct pci_dev *pdev = to_pci_dev(dev); if (!capable(CAP_SYS_ADMIN)) return -EPERM; @@ -321,11 +329,12 @@ static ssize_t numa_node_store(struct device *dev, node); dev->numa_node = node; + return count; } -static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr, - char *buf) +static ssize_t numa_node_show(struct device *dev, + struct device_attribute *attr, char *buf) { return sysfs_emit(buf, "%d\n", dev->numa_node); } @@ -349,8 +358,8 @@ static ssize_t consistent_dma_mask_bits_show(struct device *dev, } static DEVICE_ATTR_RO(consistent_dma_mask_bits); -static ssize_t msi_bus_show(struct device *dev, struct device_attribute *attr, - char *buf) +static ssize_t msi_bus_show(struct device *dev, + struct device_attribute *attr, char *buf) { struct pci_dev *pdev = to_pci_dev(dev); struct pci_bus *subordinate = pdev->subordinate; @@ -360,12 +369,13 @@ static ssize_t msi_bus_show(struct device *dev, struct device_attribute *attr, : !pdev->no_msi); } -static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr, - const char *buf, size_t count) +static ssize_t msi_bus_store(struct device *dev, + struct device_attribute *attr, const char *buf, + size_t count) { + bool allowed; struct pci_dev *pdev = to_pci_dev(dev); struct pci_bus *subordinate = pdev->subordinate; - bool allowed; if (!capable(CAP_SYS_ADMIN)) return -EPERM; @@ -392,6 +402,7 @@ static ssize_t msi_bus_store(struct device *dev, struct device_attribute *attr, dev_info(&subordinate->dev, "MSI/MSI-X %s for future drivers of devices on this bus\n", allowed ? "allowed" : "disallowed"); + return count; } static DEVICE_ATTR_RW(msi_bus); @@ -410,6 +421,7 @@ static ssize_t rescan_store(struct bus_type *bus, const char *buf, size_t count) pci_rescan_bus(b); pci_unlock_rescan_remove(); } + return count; } static BUS_ATTR_WO(rescan); @@ -443,29 +455,32 @@ static ssize_t dev_rescan_store(struct device *dev, pci_rescan_bus(pdev->bus); pci_unlock_rescan_remove(); } + return count; } static struct device_attribute dev_attr_dev_rescan = __ATTR(rescan, 0200, NULL, dev_rescan_store); -static ssize_t remove_store(struct device *dev, struct device_attribute *attr, - const char *buf, size_t count) +static ssize_t remove_store(struct device *dev, + struct device_attribute *attr, const char *buf, + size_t count) { bool remove; + struct pci_dev *pdev = to_pci_dev(dev); if (kstrtobool(buf, &remove) < 0) return -EINVAL; if (remove && device_remove_file_self(dev, attr)) - pci_stop_and_remove_bus_device_locked(to_pci_dev(dev)); + pci_stop_and_remove_bus_device_locked(pdev); + return count; } -static DEVICE_ATTR_IGNORE_LOCKDEP(remove, 0220, NULL, - remove_store); +static DEVICE_ATTR_IGNORE_LOCKDEP(remove, 0220, NULL, remove_store); static ssize_t bus_rescan_store(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) + struct device_attribute *attr, const char *buf, + size_t count) { bool rescan; struct pci_bus *bus = to_pci_bus(dev); @@ -481,6 +496,7 @@ static ssize_t bus_rescan_store(struct device *dev, pci_rescan_bus(bus); pci_unlock_rescan_remove(); } + return count; } static struct device_attribute dev_attr_bus_rescan = __ATTR(rescan, 0200, NULL, @@ -491,8 +507,8 @@ static ssize_t d3cold_allowed_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { - struct pci_dev *pdev = to_pci_dev(dev); bool allowed; + struct pci_dev *pdev = to_pci_dev(dev); if (kstrtobool(buf, &allowed) < 0) return -EINVAL; @@ -512,6 +528,7 @@ static ssize_t d3cold_allowed_show(struct device *dev, struct device_attribute *attr, char *buf) { struct pci_dev *pdev = to_pci_dev(dev); + return sysfs_emit(buf, "%u\n", pdev->d3cold_allowed); } static DEVICE_ATTR_RW(d3cold_allowed); @@ -524,8 +541,9 @@ static ssize_t devspec_show(struct device *dev, struct pci_dev *pdev = to_pci_dev(dev); struct device_node *np = pci_device_to_OF_node(pdev); - if (np == NULL) + if (!np) return 0; + return sysfs_emit(buf, "%pOF", np); } static DEVICE_ATTR_RO(devspec); @@ -535,8 +553,8 @@ static ssize_t driver_override_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) { - struct pci_dev *pdev = to_pci_dev(dev); char *driver_override, *old, *cp; + struct pci_dev *pdev = to_pci_dev(dev); /* We need to keep extra room for a newline */ if (count >= (PAGE_SIZE - 1)) @@ -574,6 +592,7 @@ static ssize_t driver_override_show(struct device *dev, device_lock(dev); len = sysfs_emit(buf, "%s\n", pdev->driver_override); device_unlock(dev); + return len; } static DEVICE_ATTR_RW(driver_override); @@ -640,8 +659,8 @@ const struct attribute_group *pcibus_groups[] = { NULL, }; -static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr, - char *buf) +static ssize_t boot_vga_show(struct device *dev, + struct device_attribute *attr, char *buf) { struct pci_dev *pdev = to_pci_dev(dev); struct pci_dev *vga_dev = vga_default_device(); @@ -659,19 +678,20 @@ static ssize_t config_read(struct file *filp, struct kobject *kobj, struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count) { - struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); unsigned int size = 64; loff_t init_off = off; - u8 *data = (u8 *) buf; + u8 *data = (u8 *)buf; /* Several chips lock up trying to read undefined config space */ if (file_ns_capable(filp, &init_user_ns, CAP_SYS_ADMIN)) - size = dev->cfg_size; - else if (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS) + size = pdev->cfg_size; + else if (pdev->hdr_type == PCI_HEADER_TYPE_CARDBUS) size = 128; if (off > size) return 0; + if (off + count > size) { size -= off; count = size; @@ -679,11 +699,11 @@ static ssize_t config_read(struct file *filp, struct kobject *kobj, size = count; } - pci_config_pm_runtime_get(dev); + pci_config_pm_runtime_get(pdev); if ((off & 1) && size) { u8 val; - pci_user_read_config_byte(dev, off, &val); + pci_user_read_config_byte(pdev, off, &val); data[off - init_off] = val; off++; size--; @@ -691,7 +711,7 @@ static ssize_t config_read(struct file *filp, struct kobject *kobj, if ((off & 3) && size > 2) { u16 val; - pci_user_read_config_word(dev, off, &val); + pci_user_read_config_word(pdev, off, &val); data[off - init_off] = val & 0xff; data[off - init_off + 1] = (val >> 8) & 0xff; off += 2; @@ -700,7 +720,7 @@ static ssize_t config_read(struct file *filp, struct kobject *kobj, while (size > 3) { u32 val; - pci_user_read_config_dword(dev, off, &val); + pci_user_read_config_dword(pdev, off, &val); data[off - init_off] = val & 0xff; data[off - init_off + 1] = (val >> 8) & 0xff; data[off - init_off + 2] = (val >> 16) & 0xff; @@ -712,7 +732,7 @@ static ssize_t config_read(struct file *filp, struct kobject *kobj, if (size >= 2) { u16 val; - pci_user_read_config_word(dev, off, &val); + pci_user_read_config_word(pdev, off, &val); data[off - init_off] = val & 0xff; data[off - init_off + 1] = (val >> 8) & 0xff; off += 2; @@ -721,13 +741,13 @@ static ssize_t config_read(struct file *filp, struct kobject *kobj, if (size > 0) { u8 val; - pci_user_read_config_byte(dev, off, &val); + pci_user_read_config_byte(pdev, off, &val); data[off - init_off] = val; off++; --size; } - pci_config_pm_runtime_put(dev); + pci_config_pm_runtime_put(pdev); return count; } @@ -736,27 +756,28 @@ static ssize_t config_write(struct file *filp, struct kobject *kobj, struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count) { - struct pci_dev *dev = to_pci_dev(kobj_to_dev(kobj)); + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); unsigned int size = count; loff_t init_off = off; - u8 *data = (u8 *) buf; - int ret; + u8 *data = (u8 *)buf; + size_t ret; ret = security_locked_down(LOCKDOWN_PCI_ACCESS); if (ret) return ret; - if (off > dev->cfg_size) + if (off > pdev->cfg_size) return 0; - if (off + count > dev->cfg_size) { - size = dev->cfg_size - off; + + if (off + count > pdev->cfg_size) { + size = pdev->cfg_size - off; count = size; } - pci_config_pm_runtime_get(dev); + pci_config_pm_runtime_get(pdev); if ((off & 1) && size) { - pci_user_write_config_byte(dev, off, data[off - init_off]); + pci_user_write_config_byte(pdev, off, data[off - init_off]); off++; size--; } @@ -764,7 +785,7 @@ static ssize_t config_write(struct file *filp, struct kobject *kobj, if ((off & 3) && size > 2) { u16 val = data[off - init_off]; val |= (u16) data[off - init_off + 1] << 8; - pci_user_write_config_word(dev, off, val); + pci_user_write_config_word(pdev, off, val); off += 2; size -= 2; } @@ -774,7 +795,7 @@ static ssize_t config_write(struct file *filp, struct kobject *kobj, val |= (u32) data[off - init_off + 1] << 8; val |= (u32) data[off - init_off + 2] << 16; val |= (u32) data[off - init_off + 3] << 24; - pci_user_write_config_dword(dev, off, val); + pci_user_write_config_dword(pdev, off, val); off += 4; size -= 4; } @@ -782,18 +803,18 @@ static ssize_t config_write(struct file *filp, struct kobject *kobj, if (size >= 2) { u16 val = data[off - init_off]; val |= (u16) data[off - init_off + 1] << 8; - pci_user_write_config_word(dev, off, val); + pci_user_write_config_word(pdev, off, val); off += 2; size -= 2; } if (size) { - pci_user_write_config_byte(dev, off, data[off - init_off]); + pci_user_write_config_byte(pdev, off, data[off - init_off]); off++; --size; } - pci_config_pm_runtime_put(dev); + pci_config_pm_runtime_put(pdev); return count; } @@ -876,7 +897,7 @@ static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj, * pci_mmap_legacy_mem - map legacy PCI memory into user memory space * @filp: open sysfs file * @kobj: kobject corresponding to device to be mapped - * @attr: struct bin_attribute for this file + * @bin_attr: struct bin_attribute for this file * @vma: struct vm_area_struct passed to mmap * * Uses an arch specific callback, pci_mmap_legacy_mem_page_range, to mmap @@ -884,7 +905,7 @@ static ssize_t pci_write_legacy_io(struct file *filp, struct kobject *kobj, * memory space. */ static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj, - struct bin_attribute *attr, + struct bin_attribute *bin_attr, struct vm_area_struct *vma) { struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); @@ -896,7 +917,7 @@ static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj, * pci_mmap_legacy_io - map legacy PCI IO into user memory space * @filp: open sysfs file * @kobj: kobject corresponding to device to be mapped - * @attr: struct bin_attribute for this file + * @bin_attr: struct bin_attribute for this file * @vma: struct vm_area_struct passed to mmap * * Uses an arch specific callback, pci_mmap_legacy_io_page_range, to mmap @@ -904,7 +925,7 @@ static int pci_mmap_legacy_mem(struct file *filp, struct kobject *kobj, * memory space. Returns -ENOSYS if the operation isn't supported */ static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj, - struct bin_attribute *attr, + struct bin_attribute *bin_attr, struct vm_area_struct *vma) { struct pci_bus *bus = to_pci_bus(kobj_to_dev(kobj)); @@ -914,19 +935,19 @@ static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj, /** * pci_adjust_legacy_attr - adjustment of legacy file attributes - * @b: bus to create files under + * @bus: bus to create files under * @mmap_type: I/O port or memory * * Stub implementation. Can be overridden by arch if necessary. */ -void __weak pci_adjust_legacy_attr(struct pci_bus *b, +void __weak pci_adjust_legacy_attr(struct pci_bus *bus, enum pci_mmap_state mmap_type) { } /** * pci_create_legacy_files - create legacy I/O port and memory files - * @b: bus to create files under + * @bus: bus to create files under * * Some platforms allow access to legacy I/O port and ISA memory space on * a per-bus basis. This routine creates the files and ties them into @@ -935,67 +956,71 @@ void __weak pci_adjust_legacy_attr(struct pci_bus *b, * On error unwind, but don't propagate the error to the caller * as it is ok to set up the PCI bus without these files. */ -void pci_create_legacy_files(struct pci_bus *b) +void pci_create_legacy_files(struct pci_bus *bus) { - int error; + int ret; if (!sysfs_initialized) return; - b->legacy_io = kcalloc(2, sizeof(struct bin_attribute), - GFP_ATOMIC); - if (!b->legacy_io) + bus->legacy_io = kcalloc(2, sizeof(struct bin_attribute), + GFP_ATOMIC); + if (!bus->legacy_io) goto kzalloc_err; - sysfs_bin_attr_init(b->legacy_io); - b->legacy_io->attr.name = "legacy_io"; - b->legacy_io->size = 0xffff; - b->legacy_io->attr.mode = 0600; - b->legacy_io->read = pci_read_legacy_io; - b->legacy_io->write = pci_write_legacy_io; - b->legacy_io->mmap = pci_mmap_legacy_io; - b->legacy_io->mapping = iomem_get_mapping(); - pci_adjust_legacy_attr(b, pci_mmap_io); - error = device_create_bin_file(&b->dev, b->legacy_io); - if (error) + sysfs_bin_attr_init(bus->legacy_io); + bus->legacy_io->attr.name = "legacy_io"; + bus->legacy_io->size = 0xffff; + bus->legacy_io->attr.mode = 0600; + bus->legacy_io->read = pci_read_legacy_io; + bus->legacy_io->write = pci_write_legacy_io; + bus->legacy_io->mmap = pci_mmap_legacy_io; + bus->legacy_io->mapping = iomem_get_mapping(); + + pci_adjust_legacy_attr(bus, pci_mmap_io); + + ret = device_create_bin_file(&bus->dev, bus->legacy_io); + if (ret) goto legacy_io_err; /* Allocated above after the legacy_io struct */ - b->legacy_mem = b->legacy_io + 1; - sysfs_bin_attr_init(b->legacy_mem); - b->legacy_mem->attr.name = "legacy_mem"; - b->legacy_mem->size = 1024*1024; - b->legacy_mem->attr.mode = 0600; - b->legacy_mem->mmap = pci_mmap_legacy_mem; - b->legacy_io->mapping = iomem_get_mapping(); - pci_adjust_legacy_attr(b, pci_mmap_mem); - error = device_create_bin_file(&b->dev, b->legacy_mem); - if (error) + bus->legacy_mem = bus->legacy_io + 1; + + sysfs_bin_attr_init(bus->legacy_mem); + bus->legacy_mem->attr.name = "legacy_mem"; + bus->legacy_mem->size = 1024*1024; + bus->legacy_mem->attr.mode = 0600; + bus->legacy_mem->mmap = pci_mmap_legacy_mem; + bus->legacy_io->mapping = iomem_get_mapping(); + + pci_adjust_legacy_attr(bus, pci_mmap_mem); + + ret = device_create_bin_file(&bus->dev, bus->legacy_mem); + if (ret) goto legacy_mem_err; return; legacy_mem_err: - device_remove_bin_file(&b->dev, b->legacy_io); + device_remove_bin_file(&bus->dev, bus->legacy_io); legacy_io_err: - kfree(b->legacy_io); - b->legacy_io = NULL; + kfree(bus->legacy_io); + bus->legacy_io = NULL; kzalloc_err: - dev_warn(&b->dev, "could not create legacy I/O port and ISA memory resources in sysfs\n"); + dev_warn(&bus->dev, "could not create legacy I/O port and ISA memory resources in sysfs\n"); } -void pci_remove_legacy_files(struct pci_bus *b) +void pci_remove_legacy_files(struct pci_bus *bus) { - if (b->legacy_io) { - device_remove_bin_file(&b->dev, b->legacy_io); - device_remove_bin_file(&b->dev, b->legacy_mem); - kfree(b->legacy_io); /* both are allocated here */ + if (bus->legacy_io) { + device_remove_bin_file(&bus->dev, bus->legacy_io); + device_remove_bin_file(&bus->dev, bus->legacy_mem); + kfree(bus->legacy_io); /* both are allocated here */ } } #endif /* HAVE_PCI_LEGACY */ #if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE) - int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma, enum pci_mmap_api mmap_api) { @@ -1004,36 +1029,41 @@ int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma, if (pci_resource_len(pdev, resno) == 0) return 0; + nr = vma_pages(vma); start = vma->vm_pgoff; size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1; + if (mmap_api == PCI_MMAP_PROCFS) { pci_resource_to_user(pdev, resno, &pdev->resource[resno], &pci_start, &pci_end); pci_start >>= PAGE_SHIFT; } + if (start >= pci_start && start < pci_start + size && start + nr <= pci_start + size) return 1; + return 0; } /** * pci_mmap_resource - map a PCI resource into user memory space * @kobj: kobject for mapping - * @attr: struct bin_attribute for the file being mapped + * @bin_attr: struct bin_attribute for the file being mapped * @vma: struct vm_area_struct passed into the mmap * @write_combine: 1 for write_combine mapping * * Use the regular PCI mapping routines to map a PCI resource into userspace. */ -static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr, +static int pci_mmap_resource(struct kobject *kobj, + struct bin_attribute *bin_attr, struct vm_area_struct *vma, int write_combine) { struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); - int bar = (unsigned long)attr->private; - enum pci_mmap_state mmap_type; + int bar = (unsigned long)bin_attr->private; struct resource *res = &pdev->resource[bar]; + enum pci_mmap_state mmap_type; int ret; ret = security_locked_down(LOCKDOWN_PCI_ACCESS); @@ -1046,31 +1076,33 @@ static int pci_mmap_resource(struct kobject *kobj, struct bin_attribute *attr, if (!pci_mmap_fits(pdev, bar, vma, PCI_MMAP_SYSFS)) return -EINVAL; - mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io; + mmap_type = pci_mmap_io; + if (res->flags & IORESOURCE_MEM) + mmap_type = pci_mmap_mem; return pci_mmap_resource_range(pdev, bar, vma, mmap_type, write_combine); } static int pci_mmap_resource_uc(struct file *filp, struct kobject *kobj, - struct bin_attribute *attr, + struct bin_attribute *bin_attr, struct vm_area_struct *vma) { - return pci_mmap_resource(kobj, attr, vma, 0); + return pci_mmap_resource(kobj, bin_attr, vma, 0); } static int pci_mmap_resource_wc(struct file *filp, struct kobject *kobj, - struct bin_attribute *attr, + struct bin_attribute *bin_attr, struct vm_area_struct *vma) { - return pci_mmap_resource(kobj, attr, vma, 1); + return pci_mmap_resource(kobj, bin_attr, vma, 1); } static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj, - struct bin_attribute *attr, char *buf, + struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count, bool write) { struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); - int bar = (unsigned long)attr->private; + int bar = (unsigned long)bin_attr->private; unsigned long port = off; port += pci_resource_start(pdev, bar); @@ -1101,27 +1133,28 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj, *(u32 *)buf = inl(port); return 4; } + return -EINVAL; } static ssize_t pci_read_resource_io(struct file *filp, struct kobject *kobj, - struct bin_attribute *attr, char *buf, + struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count) { - return pci_resource_io(filp, kobj, attr, buf, off, count, false); + return pci_resource_io(filp, kobj, bin_attr, buf, off, count, false); } static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj, - struct bin_attribute *attr, char *buf, + struct bin_attribute *bin_attr, char *buf, loff_t off, size_t count) { - int ret; + size_t ret; ret = security_locked_down(LOCKDOWN_PCI_ACCESS); if (ret) return ret; - return pci_resource_io(filp, kobj, attr, buf, off, count, true); + return pci_resource_io(filp, kobj, bin_attr, buf, off, count, true); } /** @@ -1134,10 +1167,9 @@ static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj, static void pci_remove_resource_files(struct pci_dev *pdev) { int i; + struct bin_attribute *res_attr; for (i = 0; i < PCI_STD_NUM_BARS; i++) { - struct bin_attribute *res_attr; - res_attr = pdev->res_attr[i]; if (res_attr) { sysfs_remove_bin_file(&pdev->dev.kobj, res_attr); @@ -1152,13 +1184,13 @@ static void pci_remove_resource_files(struct pci_dev *pdev) } } -static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine) +static int pci_create_attr(struct pci_dev *pdev, int resno, int write_combine) { /* allocate attribute structure, piggyback attribute name */ int name_len = write_combine ? 13 : 10; struct bin_attribute *res_attr; char *res_attr_name; - int retval; + int ret; res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC); if (!res_attr) @@ -1168,13 +1200,13 @@ static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine) sysfs_bin_attr_init(res_attr); if (write_combine) { - pdev->res_attr_wc[num] = res_attr; - sprintf(res_attr_name, "resource%d_wc", num); + pdev->res_attr_wc[resno] = res_attr; + sprintf(res_attr_name, "resource%d_wc", resno); res_attr->mmap = pci_mmap_resource_wc; } else { - pdev->res_attr[num] = res_attr; - sprintf(res_attr_name, "resource%d", num); - if (pci_resource_flags(pdev, num) & IORESOURCE_IO) { + pdev->res_attr[resno] = res_attr; + sprintf(res_attr_name, "resource%d", resno); + if (pci_resource_flags(pdev, resno) & IORESOURCE_IO) { res_attr->read = pci_read_resource_io; res_attr->write = pci_write_resource_io; if (arch_can_pci_mmap_io()) @@ -1183,17 +1215,20 @@ static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine) res_attr->mmap = pci_mmap_resource_uc; } } + if (res_attr->mmap) res_attr->mapping = iomem_get_mapping(); + res_attr->attr.name = res_attr_name; res_attr->attr.mode = 0600; - res_attr->size = pci_resource_len(pdev, num); - res_attr->private = (void *)(unsigned long)num; - retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr); - if (retval) + res_attr->size = pci_resource_len(pdev, resno); + res_attr->private = (void *)(unsigned long)resno; + + ret = sysfs_create_bin_file(&pdev->dev.kobj, res_attr); + if (ret) kfree(res_attr); - return retval; + return ret; } /** @@ -1204,31 +1239,31 @@ static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine) */ static int pci_create_resource_files(struct pci_dev *pdev) { - int i; - int retval; + int i, ret; /* Expose the PCI resources from this device as files */ for (i = 0; i < PCI_STD_NUM_BARS; i++) { - /* skip empty resources */ if (!pci_resource_len(pdev, i)) continue; - retval = pci_create_attr(pdev, i, 0); + ret = pci_create_attr(pdev, i, 0); /* for prefetchable resources, create a WC mappable file */ - if (!retval && arch_can_pci_mmap_wc() && + if (!ret && arch_can_pci_mmap_wc() && pdev->resource[i].flags & IORESOURCE_PREFETCH) - retval = pci_create_attr(pdev, i, 1); - if (retval) { + ret = pci_create_attr(pdev, i, 1); + + if (ret) { pci_remove_resource_files(pdev); - return retval; + return ret; } } + return 0; } #else /* !(defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)) */ -int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; } -void __weak pci_remove_resource_files(struct pci_dev *dev) { return; } +int __weak pci_create_resource_files(struct pci_dev *pdev) { return 0; } +void __weak pci_remove_resource_files(struct pci_dev *pdev) { return; } #endif /** @@ -1284,9 +1319,9 @@ static ssize_t rom_read(struct file *filp, struct kobject *kobj, if (!rom || !size) return -EIO; - if (off >= size) + if (off >= size) { count = 0; - else { + } else { if (off + count > size) count = size - off; @@ -1324,12 +1359,13 @@ static const struct attribute_group pci_dev_rom_attr_group = { .is_bin_visible = pci_dev_rom_attr_is_visible, }; -static ssize_t reset_store(struct device *dev, struct device_attribute *attr, - const char *buf, size_t count) +static ssize_t reset_store(struct device *dev, + struct device_attribute *attr, const char *buf, + size_t count) { bool reset; struct pci_dev *pdev = to_pci_dev(dev); - ssize_t result; + ssize_t ret; if (kstrtobool(buf, &reset) < 0) return -EINVAL; @@ -1338,10 +1374,10 @@ static ssize_t reset_store(struct device *dev, struct device_attribute *attr, return -EINVAL; pm_runtime_get_sync(dev); - result = pci_reset_function(pdev); + ret = pci_reset_function(pdev); pm_runtime_put(dev); - if (result < 0) - return result; + if (ret < 0) + return ret; return count; } @@ -1393,20 +1429,20 @@ void pci_remove_sysfs_dev_files(struct pci_dev *pdev) static int __init pci_sysfs_init(void) { struct pci_dev *pdev = NULL; - struct pci_bus *pbus = NULL; - int retval; + struct pci_bus *bus = NULL; + int ret; sysfs_initialized = 1; for_each_pci_dev(pdev) { - retval = pci_create_sysfs_dev_files(pdev); - if (retval) { + ret = pci_create_sysfs_dev_files(pdev); + if (ret) { pci_dev_put(pdev); - return retval; + return ret; } } - while ((pbus = pci_find_next_bus(pbus))) - pci_create_legacy_files(pbus); + while ((bus = pci_find_next_bus(bus))) + pci_create_legacy_files(bus); return 0; } @@ -1417,11 +1453,10 @@ static struct attribute *pci_dev_dev_attrs[] = { NULL, }; -static umode_t pci_dev_attrs_are_visible(struct kobject *kobj, - struct attribute *a, int n) +static umode_t pci_dev_attr_is_visible(struct kobject *kobj, + struct attribute *a, int n) { - struct device *dev = kobj_to_dev(kobj); - struct pci_dev *pdev = to_pci_dev(dev); + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); if (a == &dev_attr_boot_vga.attr) if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA) @@ -1436,11 +1471,10 @@ static struct attribute *pci_dev_hp_attrs[] = { NULL, }; -static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj, - struct attribute *a, int n) +static umode_t pci_dev_hp_attr_is_visible(struct kobject *kobj, + struct attribute *a, int n) { - struct device *dev = kobj_to_dev(kobj); - struct pci_dev *pdev = to_pci_dev(dev); + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); if (pdev->is_virtfn) return 0; @@ -1448,28 +1482,26 @@ static umode_t pci_dev_hp_attrs_are_visible(struct kobject *kobj, return a->mode; } -static umode_t pci_bridge_attrs_are_visible(struct kobject *kobj, - struct attribute *a, int n) +static umode_t pci_bridge_attr_is_visible(struct kobject *kobj, + struct attribute *a, int n) { - struct device *dev = kobj_to_dev(kobj); - struct pci_dev *pdev = to_pci_dev(dev); + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); - if (pci_is_bridge(pdev)) - return a->mode; + if (!pci_is_bridge(pdev)) + return 0; - return 0; + return a->mode; } -static umode_t pcie_dev_attrs_are_visible(struct kobject *kobj, - struct attribute *a, int n) +static umode_t pcie_dev_attr_is_visible(struct kobject *kobj, + struct attribute *a, int n) { - struct device *dev = kobj_to_dev(kobj); - struct pci_dev *pdev = to_pci_dev(dev); + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); - if (pci_is_pcie(pdev)) - return a->mode; + if (!pci_is_pcie(pdev)) + return 0; - return 0; + return a->mode; } static const struct attribute_group pci_dev_group = { @@ -1493,22 +1525,22 @@ const struct attribute_group *pci_dev_groups[] = { static const struct attribute_group pci_dev_hp_attr_group = { .attrs = pci_dev_hp_attrs, - .is_visible = pci_dev_hp_attrs_are_visible, + .is_visible = pci_dev_hp_attr_is_visible, }; static const struct attribute_group pci_dev_attr_group = { .attrs = pci_dev_dev_attrs, - .is_visible = pci_dev_attrs_are_visible, + .is_visible = pci_dev_attr_is_visible, }; static const struct attribute_group pci_bridge_attr_group = { .attrs = pci_bridge_attrs, - .is_visible = pci_bridge_attrs_are_visible, + .is_visible = pci_bridge_attr_is_visible, }; static const struct attribute_group pcie_dev_attr_group = { .attrs = pcie_dev_attrs, - .is_visible = pcie_dev_attrs_are_visible, + .is_visible = pcie_dev_attr_is_visible, }; static const struct attribute_group *pci_dev_attr_groups[] = { From patchwork Fri Apr 16 20:58:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?S3J6eXN6dG9mIFdpbGN6ee+/ve+/vXNraQ==?= X-Patchwork-Id: 12208807 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B0B5BC43461 for ; Fri, 16 Apr 2021 20:59:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 83316613DA for ; Fri, 16 Apr 2021 20:59:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343757AbhDPU7f (ORCPT ); Fri, 16 Apr 2021 16:59:35 -0400 Received: from mail-ej1-f44.google.com ([209.85.218.44]:33522 "EHLO mail-ej1-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244516AbhDPU7f (ORCPT ); Fri, 16 Apr 2021 16:59:35 -0400 Received: by mail-ej1-f44.google.com with SMTP id g5so37295144ejx.0 for ; Fri, 16 Apr 2021 13:59:09 -0700 (PDT) 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=zEMt8zlMmYDSPAeCcBkze5c5Ax6jXwqdG0prTSbuHtc=; b=p6kP+vOgx5ltmAukT3Woqg7fHs+x3TsCeRuhMVNIDuDVqK0GTawbEsKjN2t6n/Sue9 CRay9q1L1oLxdaLSyS6v4b8W0okY3hbqya/TL7p+waba36cNxK0bqvZHjFqIM3vJYx1B wsJhy3W1S1GeG5KungCof3MOg9/j6wi5Hts0LUDMpeNuVfiwPJ8tRXycUOqFHTGU0A+G 3ni3iAr+2+vMXfIDSBUF9ScXGAYtsByPPub4hYBOSdWSe+AKSmF4MdTv1mW3QYRe0+Xk xcWcrJJt1OTKvsekOkC3I6q4tD8Qc24NAYma7mm16AvAednt1hXwe4VNwqHxZ/GLhSE/ mNZg== X-Gm-Message-State: AOAM531OciFaSAMWrNHM55XFbjmhxkqYRu8S7rYw33vCN+cDo3bSAkNU a8wqaoHcWRLApALvU6odEio= X-Google-Smtp-Source: ABdhPJw0Zgvsy83yPtRRCqR291wNUnO1PGIuY7JCVqj2VLmSA8IPlSiU2Y5Vce05spGiAwYDNTvyfw== X-Received: by 2002:a17:906:7842:: with SMTP id p2mr10614167ejm.87.1618606749009; Fri, 16 Apr 2021 13:59:09 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id n11sm5103864ejg.43.2021.04.16.13.59.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Apr 2021 13:59:08 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: =?utf-8?q?Pali_Roh=C3=A1r?= , "Oliver O'Halloran" , Greg Kroah-Hartman , Daniel Vetter , Joe Perches , Dan Williams , Mauro Carvalho Chehab , David Sterba , linux-pci@vger.kernel.org Subject: [PATCH 11/20] PCI: Rearrange attributes from the pci_dev_group Date: Fri, 16 Apr 2021 20:58:47 +0000 Message-Id: <20210416205856.3234481-12-kw@linux.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210416205856.3234481-1-kw@linux.com> References: <20210416205856.3234481-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org When new sysfs objects were added to the PCI device over time, the code that implemented new attributes has been added in many different places in the pci-sysfs.c file. This makes it hard to read and also hard to find relevant code. Thus, collect all the attributes that are part of the "pci_dev_group" attribute group together and move to the top of the file sorting everything attribute in the order of use. No functional change intended. Suggested-by: Bjorn Helgaas Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-sysfs.c | 695 +++++++++++++++++++++------------------- 1 file changed, 369 insertions(+), 326 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index 96302b63f6c5..5f83ff087f2c 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -30,50 +30,6 @@ static int sysfs_initialized; /* = 0 */ -/* show configuration fields */ -#define pci_config_attr(field, format_string) \ -static ssize_t \ -field##_show(struct device *dev, struct device_attribute *attr, char *buf) \ -{ \ - struct pci_dev *pdev = to_pci_dev(dev); \ - \ - return sysfs_emit(buf, format_string, pdev->field); \ -} \ -static DEVICE_ATTR_RO(field) - -pci_config_attr(vendor, "0x%04x\n"); -pci_config_attr(device, "0x%04x\n"); -pci_config_attr(subsystem_vendor, "0x%04x\n"); -pci_config_attr(subsystem_device, "0x%04x\n"); -pci_config_attr(revision, "0x%02x\n"); -pci_config_attr(class, "0x%06x\n"); -pci_config_attr(irq, "%u\n"); - -static ssize_t broken_parity_status_show(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct pci_dev *pdev = to_pci_dev(dev); - - return sysfs_emit(buf, "%u\n", pdev->broken_parity_status); -} - -static ssize_t broken_parity_status_store(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - bool broken; - struct pci_dev *pdev = to_pci_dev(dev); - - if (kstrtobool(buf, &broken) < 0) - return -EINVAL; - - pdev->broken_parity_status = !!broken; - - return count; -} -static DEVICE_ATTR_RW(broken_parity_status); - static ssize_t pci_dev_show_local_cpu(struct device *dev, bool list, struct device_attribute *attr, char *buf) { @@ -93,43 +49,6 @@ static ssize_t pci_dev_show_local_cpu(struct device *dev, bool list, return cpumap_print_to_pagebuf(list, buf, mask); } -static ssize_t local_cpus_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - return pci_dev_show_local_cpu(dev, false, attr, buf); -} -static DEVICE_ATTR_RO(local_cpus); - -static ssize_t local_cpulist_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - return pci_dev_show_local_cpu(dev, true, attr, buf); -} -static DEVICE_ATTR_RO(local_cpulist); - -/* - * PCI Bus Class Devices - */ -static ssize_t cpuaffinity_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct pci_bus *bus = to_pci_bus(dev); - const struct cpumask *cpumask = cpumask_of_pcibus(bus); - - return cpumap_print_to_pagebuf(false, buf, cpumask); -} -static DEVICE_ATTR_RO(cpuaffinity); - -static ssize_t cpulistaffinity_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct pci_bus *bus = to_pci_bus(dev); - const struct cpumask *cpumask = cpumask_of_pcibus(bus); - - return cpumap_print_to_pagebuf(true, buf, cpumask); -} -static DEVICE_ATTR_RO(cpulistaffinity); - static ssize_t power_state_show(struct device *dev, struct device_attribute *attr, char *buf) { @@ -139,7 +58,6 @@ static ssize_t power_state_show(struct device *dev, } static DEVICE_ATTR_RO(power_state); -/* show resources */ static ssize_t resource_show(struct device *dev, struct device_attribute *attr, char *buf) { @@ -166,89 +84,82 @@ static ssize_t resource_show(struct device *dev, } static DEVICE_ATTR_RO(resource); -static ssize_t max_link_speed_show(struct device *dev, - struct device_attribute *attr, char *buf) +static ssize_t vendor_show(struct device *dev, + struct device_attribute *attr, char *buf) { struct pci_dev *pdev = to_pci_dev(dev); - return sysfs_emit(buf, "%s\n", - pci_speed_string(pcie_get_speed_cap(pdev))); + return sysfs_emit(buf, "0x%04x\n", pdev->vendor); } -static DEVICE_ATTR_RO(max_link_speed); +static DEVICE_ATTR_RO(vendor); -static ssize_t max_link_width_show(struct device *dev, - struct device_attribute *attr, char *buf) +static ssize_t device_show(struct device *dev, + struct device_attribute *attr, char *buf) { struct pci_dev *pdev = to_pci_dev(dev); - return sysfs_emit(buf, "%u\n", pcie_get_width_cap(pdev)); + return sysfs_emit(buf, "0x%04x\n", pdev->device); } -static DEVICE_ATTR_RO(max_link_width); +static DEVICE_ATTR_RO(device); -static ssize_t current_link_speed_show(struct device *dev, - struct device_attribute *attr, char *buf) +static ssize_t subsystem_vendor_show(struct device *dev, + struct device_attribute *attr, char *buf) { struct pci_dev *pdev = to_pci_dev(dev); - enum pci_bus_speed speed; - - pcie_bandwidth_available(pdev, NULL, &speed, NULL); - return sysfs_emit(buf, "%s\n", pci_speed_string(speed)); + return sysfs_emit(buf, "0x%04x\n", pdev->subsystem_vendor); } -static DEVICE_ATTR_RO(current_link_speed); +static DEVICE_ATTR_RO(subsystem_vendor); -static ssize_t current_link_width_show(struct device *dev, - struct device_attribute *attr, char *buf) +static ssize_t subsystem_device_show(struct device *dev, + struct device_attribute *attr, char *buf) { struct pci_dev *pdev = to_pci_dev(dev); - enum pcie_link_width width; - pcie_bandwidth_available(pdev, NULL, NULL, &width); - - return sysfs_emit(buf, "%u\n", width); + return sysfs_emit(buf, "0x%04x\n", pdev->subsystem_device); } -static DEVICE_ATTR_RO(current_link_width); +static DEVICE_ATTR_RO(subsystem_device); -static ssize_t secondary_bus_number_show(struct device *dev, - struct device_attribute *attr, - char *buf) +static ssize_t revision_show(struct device *dev, + struct device_attribute *attr, char *buf) { struct pci_dev *pdev = to_pci_dev(dev); - u8 sec_bus; - int err; - - err = pci_read_config_byte(pdev, PCI_SECONDARY_BUS, &sec_bus); - if (err) - return -EINVAL; - return sysfs_emit(buf, "%u\n", sec_bus); + return sysfs_emit(buf, "0x%02x\n", pdev->revision); } -static DEVICE_ATTR_RO(secondary_bus_number); +static DEVICE_ATTR_RO(revision); -static ssize_t subordinate_bus_number_show(struct device *dev, - struct device_attribute *attr, - char *buf) +static ssize_t class_show(struct device *dev, + struct device_attribute *attr, char *buf) { struct pci_dev *pdev = to_pci_dev(dev); - u8 sub_bus; - int err; - err = pci_read_config_byte(pdev, PCI_SUBORDINATE_BUS, &sub_bus); - if (err) - return -EINVAL; - - return sysfs_emit(buf, "%u\n", sub_bus); + return sysfs_emit(buf, "0x%06x\n", pdev->class); } -static DEVICE_ATTR_RO(subordinate_bus_number); +static DEVICE_ATTR_RO(class); -static ssize_t ari_enabled_show(struct device *dev, - struct device_attribute *attr, char *buf) +static ssize_t irq_show(struct device *dev, + struct device_attribute *attr, char *buf) { struct pci_dev *pdev = to_pci_dev(dev); - return sysfs_emit(buf, "%u\n", pci_ari_enabled(pdev->bus)); + return sysfs_emit(buf, "%u\n", pdev->irq); } -static DEVICE_ATTR_RO(ari_enabled); +static DEVICE_ATTR_RO(irq); + +static ssize_t local_cpus_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + return pci_dev_show_local_cpu(dev, false, attr, buf); +} +static DEVICE_ATTR_RO(local_cpus); + +static ssize_t local_cpulist_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + return pci_dev_show_local_cpu(dev, true, attr, buf); +} +static DEVICE_ATTR_RO(local_cpulist); static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, char *buf) @@ -263,6 +174,68 @@ static ssize_t modalias_show(struct device *dev, } static DEVICE_ATTR_RO(modalias); +#ifdef CONFIG_NUMA +static ssize_t numa_node_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%d\n", dev->numa_node); +} + +static ssize_t numa_node_store(struct device *dev, + struct device_attribute *attr, const char *buf, + size_t count) +{ + int node; + struct pci_dev *pdev = to_pci_dev(dev); + + if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + + if (kstrtoint(buf, 0, &node) < 0) + return -EINVAL; + + if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES) + return -EINVAL; + + if (node != NUMA_NO_NODE && !node_online(node)) + return -EINVAL; + + add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK); + pci_alert(pdev, FW_BUG "Overriding NUMA node to %d. Contact your vendor for updates.", + node); + + dev->numa_node = node; + + return count; +} +static DEVICE_ATTR_RW(numa_node); +#endif + +static ssize_t dma_mask_bits_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct pci_dev *pdev = to_pci_dev(dev); + + return sysfs_emit(buf, "%d\n", fls64(pdev->dma_mask)); +} +static DEVICE_ATTR_RO(dma_mask_bits); + +static ssize_t consistent_dma_mask_bits_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + return sysfs_emit(buf, "%d\n", fls64(dev->coherent_dma_mask)); +} +static DEVICE_ATTR_RO(consistent_dma_mask_bits); + +static ssize_t enable_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct pci_dev *pdev = to_pci_dev(dev); + + return sysfs_emit(buf, "%u\n", atomic_read(&pdev->enable_cnt)); +} + static ssize_t enable_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) @@ -294,118 +267,319 @@ static ssize_t enable_store(struct device *dev, return count; } +static DEVICE_ATTR_RW(enable); -static ssize_t enable_show(struct device *dev, - struct device_attribute *attr, char *buf) +static ssize_t broken_parity_status_show(struct device *dev, + struct device_attribute *attr, + char *buf) { struct pci_dev *pdev = to_pci_dev(dev); - return sysfs_emit(buf, "%u\n", atomic_read(&pdev->enable_cnt)); + return sysfs_emit(buf, "%u\n", pdev->broken_parity_status); } -static DEVICE_ATTR_RW(enable); -#ifdef CONFIG_NUMA -static ssize_t numa_node_store(struct device *dev, - struct device_attribute *attr, const char *buf, - size_t count) +static ssize_t broken_parity_status_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + bool broken; + struct pci_dev *pdev = to_pci_dev(dev); + + if (kstrtobool(buf, &broken) < 0) + return -EINVAL; + + pdev->broken_parity_status = !!broken; + + return count; +} +static DEVICE_ATTR_RW(broken_parity_status); + +static ssize_t msi_bus_show(struct device *dev, + struct device_attribute *attr, char *buf) { - int node; struct pci_dev *pdev = to_pci_dev(dev); + struct pci_bus *subordinate = pdev->subordinate; + + return sysfs_emit(buf, "%u\n", subordinate ? + !(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) + : !pdev->no_msi); +} + +static ssize_t msi_bus_store(struct device *dev, + struct device_attribute *attr, const char *buf, + size_t count) +{ + bool allowed; + struct pci_dev *pdev = to_pci_dev(dev); + struct pci_bus *subordinate = pdev->subordinate; if (!capable(CAP_SYS_ADMIN)) return -EPERM; - if (kstrtoint(buf, 0, &node) < 0) + if (kstrtobool(buf, &allowed) < 0) return -EINVAL; - if ((node < 0 && node != NUMA_NO_NODE) || node >= MAX_NUMNODES) + /* + * "no_msi" and "bus_flags" only affect what happens when a driver + * requests MSI or MSI-X. They don't affect any drivers that have + * already requested MSI or MSI-X. + */ + if (!subordinate) { + pdev->no_msi = !allowed; + pci_info(pdev, "MSI/MSI-X %s for future drivers\n", + allowed ? "allowed" : "disallowed"); + return count; + } + + if (allowed) + subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI; + else + subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI; + + dev_info(&subordinate->dev, "MSI/MSI-X %s for future drivers of devices on this bus\n", + allowed ? "allowed" : "disallowed"); + + return count; +} +static DEVICE_ATTR_RW(msi_bus); + +#if defined(CONFIG_PM) && defined(CONFIG_ACPI) +static ssize_t d3cold_allowed_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct pci_dev *pdev = to_pci_dev(dev); + + return sysfs_emit(buf, "%u\n", pdev->d3cold_allowed); +} + +static ssize_t d3cold_allowed_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + bool allowed; + struct pci_dev *pdev = to_pci_dev(dev); + + if (kstrtobool(buf, &allowed) < 0) return -EINVAL; - if (node != NUMA_NO_NODE && !node_online(node)) + pdev->d3cold_allowed = !!allowed; + if (pdev->d3cold_allowed) + pci_d3cold_enable(pdev); + else + pci_d3cold_disable(pdev); + + pm_runtime_resume(dev); + + return count; +} +static DEVICE_ATTR_RW(d3cold_allowed); +#endif + +#ifdef CONFIG_OF +static ssize_t devspec_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct pci_dev *pdev = to_pci_dev(dev); + struct device_node *np = pci_device_to_OF_node(pdev); + + if (!np) + return 0; + + return sysfs_emit(buf, "%pOF", np); +} +static DEVICE_ATTR_RO(devspec); +#endif + +static ssize_t driver_override_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct pci_dev *pdev = to_pci_dev(dev); + ssize_t len; + + device_lock(dev); + len = sysfs_emit(buf, "%s\n", pdev->driver_override); + device_unlock(dev); + + return len; +} + +static ssize_t driver_override_store(struct device *dev, + struct device_attribute *attr, + const char *buf, size_t count) +{ + char *driver_override, *old, *cp; + struct pci_dev *pdev = to_pci_dev(dev); + + /* We need to keep extra room for a newline */ + if (count >= (PAGE_SIZE - 1)) return -EINVAL; - add_taint(TAINT_FIRMWARE_WORKAROUND, LOCKDEP_STILL_OK); - pci_alert(pdev, FW_BUG "Overriding NUMA node to %d. Contact your vendor for updates.", - node); + driver_override = kstrndup(buf, count, GFP_KERNEL); + if (!driver_override) + return -ENOMEM; + + cp = strchr(driver_override, '\n'); + if (cp) + *cp = '\0'; + + device_lock(dev); + old = pdev->driver_override; + if (strlen(driver_override)) { + pdev->driver_override = driver_override; + } else { + kfree(driver_override); + pdev->driver_override = NULL; + } + device_unlock(dev); + + kfree(old); + + return count; +} +static DEVICE_ATTR_RW(driver_override); + +static ssize_t ari_enabled_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct pci_dev *pdev = to_pci_dev(dev); + + return sysfs_emit(buf, "%u\n", pci_ari_enabled(pdev->bus)); +} +static DEVICE_ATTR_RO(ari_enabled); + +static struct attribute *pci_dev_attrs[] = { + &dev_attr_power_state.attr, + &dev_attr_resource.attr, + &dev_attr_vendor.attr, + &dev_attr_device.attr, + &dev_attr_subsystem_vendor.attr, + &dev_attr_subsystem_device.attr, + &dev_attr_revision.attr, + &dev_attr_class.attr, + &dev_attr_irq.attr, + &dev_attr_local_cpus.attr, + &dev_attr_local_cpulist.attr, + &dev_attr_modalias.attr, +#ifdef CONFIG_NUMA + &dev_attr_numa_node.attr, +#endif + &dev_attr_dma_mask_bits.attr, + &dev_attr_consistent_dma_mask_bits.attr, + &dev_attr_enable.attr, + &dev_attr_broken_parity_status.attr, + &dev_attr_msi_bus.attr, +#if defined(CONFIG_PM) && defined(CONFIG_ACPI) + &dev_attr_d3cold_allowed.attr, +#endif +#ifdef CONFIG_OF + &dev_attr_devspec.attr, +#endif + &dev_attr_driver_override.attr, + &dev_attr_ari_enabled.attr, + NULL, +}; + +static const struct attribute_group pci_dev_group = { + .attrs = pci_dev_attrs, +}; - dev->numa_node = node; +/* + * PCI Bus Class Devices + */ +static ssize_t cpuaffinity_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct pci_bus *bus = to_pci_bus(dev); + const struct cpumask *cpumask = cpumask_of_pcibus(bus); - return count; + return cpumap_print_to_pagebuf(false, buf, cpumask); } +static DEVICE_ATTR_RO(cpuaffinity); -static ssize_t numa_node_show(struct device *dev, - struct device_attribute *attr, char *buf) +static ssize_t cpulistaffinity_show(struct device *dev, + struct device_attribute *attr, char *buf) { - return sysfs_emit(buf, "%d\n", dev->numa_node); + struct pci_bus *bus = to_pci_bus(dev); + const struct cpumask *cpumask = cpumask_of_pcibus(bus); + + return cpumap_print_to_pagebuf(true, buf, cpumask); } -static DEVICE_ATTR_RW(numa_node); -#endif +static DEVICE_ATTR_RO(cpulistaffinity); -static ssize_t dma_mask_bits_show(struct device *dev, - struct device_attribute *attr, char *buf) +static ssize_t max_link_speed_show(struct device *dev, + struct device_attribute *attr, char *buf) { struct pci_dev *pdev = to_pci_dev(dev); - return sysfs_emit(buf, "%d\n", fls64(pdev->dma_mask)); + return sysfs_emit(buf, "%s\n", + pci_speed_string(pcie_get_speed_cap(pdev))); } -static DEVICE_ATTR_RO(dma_mask_bits); +static DEVICE_ATTR_RO(max_link_speed); -static ssize_t consistent_dma_mask_bits_show(struct device *dev, - struct device_attribute *attr, - char *buf) +static ssize_t max_link_width_show(struct device *dev, + struct device_attribute *attr, char *buf) { - return sysfs_emit(buf, "%d\n", fls64(dev->coherent_dma_mask)); + struct pci_dev *pdev = to_pci_dev(dev); + + return sysfs_emit(buf, "%u\n", pcie_get_width_cap(pdev)); } -static DEVICE_ATTR_RO(consistent_dma_mask_bits); +static DEVICE_ATTR_RO(max_link_width); -static ssize_t msi_bus_show(struct device *dev, - struct device_attribute *attr, char *buf) +static ssize_t current_link_speed_show(struct device *dev, + struct device_attribute *attr, char *buf) { struct pci_dev *pdev = to_pci_dev(dev); - struct pci_bus *subordinate = pdev->subordinate; + enum pci_bus_speed speed; - return sysfs_emit(buf, "%u\n", subordinate ? - !(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) - : !pdev->no_msi); + pcie_bandwidth_available(pdev, NULL, &speed, NULL); + + return sysfs_emit(buf, "%s\n", pci_speed_string(speed)); } +static DEVICE_ATTR_RO(current_link_speed); -static ssize_t msi_bus_store(struct device *dev, - struct device_attribute *attr, const char *buf, - size_t count) +static ssize_t current_link_width_show(struct device *dev, + struct device_attribute *attr, char *buf) { - bool allowed; struct pci_dev *pdev = to_pci_dev(dev); - struct pci_bus *subordinate = pdev->subordinate; + enum pcie_link_width width; - if (!capable(CAP_SYS_ADMIN)) - return -EPERM; + pcie_bandwidth_available(pdev, NULL, NULL, &width); - if (kstrtobool(buf, &allowed) < 0) + return sysfs_emit(buf, "%u\n", width); +} +static DEVICE_ATTR_RO(current_link_width); + +static ssize_t secondary_bus_number_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct pci_dev *pdev = to_pci_dev(dev); + u8 sec_bus; + int err; + + err = pci_read_config_byte(pdev, PCI_SECONDARY_BUS, &sec_bus); + if (err) return -EINVAL; - /* - * "no_msi" and "bus_flags" only affect what happens when a driver - * requests MSI or MSI-X. They don't affect any drivers that have - * already requested MSI or MSI-X. - */ - if (!subordinate) { - pdev->no_msi = !allowed; - pci_info(pdev, "MSI/MSI-X %s for future drivers\n", - allowed ? "allowed" : "disallowed"); - return count; - } + return sysfs_emit(buf, "%u\n", sec_bus); +} +static DEVICE_ATTR_RO(secondary_bus_number); - if (allowed) - subordinate->bus_flags &= ~PCI_BUS_FLAGS_NO_MSI; - else - subordinate->bus_flags |= PCI_BUS_FLAGS_NO_MSI; +static ssize_t subordinate_bus_number_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct pci_dev *pdev = to_pci_dev(dev); + u8 sub_bus; + int err; - dev_info(&subordinate->dev, "MSI/MSI-X %s for future drivers of devices on this bus\n", - allowed ? "allowed" : "disallowed"); + err = pci_read_config_byte(pdev, PCI_SUBORDINATE_BUS, &sub_bus); + if (err) + return -EINVAL; - return count; + return sysfs_emit(buf, "%u\n", sub_bus); } -static DEVICE_ATTR_RW(msi_bus); +static DEVICE_ATTR_RO(subordinate_bus_number); static ssize_t rescan_store(struct bus_type *bus, const char *buf, size_t count) { @@ -502,133 +676,6 @@ static ssize_t bus_rescan_store(struct device *dev, static struct device_attribute dev_attr_bus_rescan = __ATTR(rescan, 0200, NULL, bus_rescan_store); -#if defined(CONFIG_PM) && defined(CONFIG_ACPI) -static ssize_t d3cold_allowed_store(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - bool allowed; - struct pci_dev *pdev = to_pci_dev(dev); - - if (kstrtobool(buf, &allowed) < 0) - return -EINVAL; - - pdev->d3cold_allowed = !!allowed; - if (pdev->d3cold_allowed) - pci_d3cold_enable(pdev); - else - pci_d3cold_disable(pdev); - - pm_runtime_resume(dev); - - return count; -} - -static ssize_t d3cold_allowed_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct pci_dev *pdev = to_pci_dev(dev); - - return sysfs_emit(buf, "%u\n", pdev->d3cold_allowed); -} -static DEVICE_ATTR_RW(d3cold_allowed); -#endif - -#ifdef CONFIG_OF -static ssize_t devspec_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct pci_dev *pdev = to_pci_dev(dev); - struct device_node *np = pci_device_to_OF_node(pdev); - - if (!np) - return 0; - - return sysfs_emit(buf, "%pOF", np); -} -static DEVICE_ATTR_RO(devspec); -#endif - -static ssize_t driver_override_store(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t count) -{ - char *driver_override, *old, *cp; - struct pci_dev *pdev = to_pci_dev(dev); - - /* We need to keep extra room for a newline */ - if (count >= (PAGE_SIZE - 1)) - return -EINVAL; - - driver_override = kstrndup(buf, count, GFP_KERNEL); - if (!driver_override) - return -ENOMEM; - - cp = strchr(driver_override, '\n'); - if (cp) - *cp = '\0'; - - device_lock(dev); - old = pdev->driver_override; - if (strlen(driver_override)) { - pdev->driver_override = driver_override; - } else { - kfree(driver_override); - pdev->driver_override = NULL; - } - device_unlock(dev); - - kfree(old); - - return count; -} - -static ssize_t driver_override_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct pci_dev *pdev = to_pci_dev(dev); - ssize_t len; - - device_lock(dev); - len = sysfs_emit(buf, "%s\n", pdev->driver_override); - device_unlock(dev); - - return len; -} -static DEVICE_ATTR_RW(driver_override); - -static struct attribute *pci_dev_attrs[] = { - &dev_attr_power_state.attr, - &dev_attr_resource.attr, - &dev_attr_vendor.attr, - &dev_attr_device.attr, - &dev_attr_subsystem_vendor.attr, - &dev_attr_subsystem_device.attr, - &dev_attr_revision.attr, - &dev_attr_class.attr, - &dev_attr_irq.attr, - &dev_attr_local_cpus.attr, - &dev_attr_local_cpulist.attr, - &dev_attr_modalias.attr, -#ifdef CONFIG_NUMA - &dev_attr_numa_node.attr, -#endif - &dev_attr_dma_mask_bits.attr, - &dev_attr_consistent_dma_mask_bits.attr, - &dev_attr_enable.attr, - &dev_attr_broken_parity_status.attr, - &dev_attr_msi_bus.attr, -#if defined(CONFIG_PM) && defined(CONFIG_ACPI) - &dev_attr_d3cold_allowed.attr, -#endif -#ifdef CONFIG_OF - &dev_attr_devspec.attr, -#endif - &dev_attr_driver_override.attr, - &dev_attr_ari_enabled.attr, - NULL, -}; - static struct attribute *pci_bridge_attrs[] = { &dev_attr_subordinate_bus_number.attr, &dev_attr_secondary_bus_number.attr, @@ -1504,10 +1551,6 @@ static umode_t pcie_dev_attr_is_visible(struct kobject *kobj, return a->mode; } -static const struct attribute_group pci_dev_group = { - .attrs = pci_dev_attrs, -}; - const struct attribute_group *pci_dev_groups[] = { &pci_dev_group, &pci_dev_config_attr_group, From patchwork Fri Apr 16 20:58:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?S3J6eXN6dG9mIFdpbGN6ee+/ve+/vXNraQ==?= X-Patchwork-Id: 12208809 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.9 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNWANTED_LANGUAGE_BODY, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9C78AC433B4 for ; Fri, 16 Apr 2021 20:59:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6AB7A613E9 for ; Fri, 16 Apr 2021 20:59:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244516AbhDPU7g (ORCPT ); Fri, 16 Apr 2021 16:59:36 -0400 Received: from mail-ed1-f41.google.com ([209.85.208.41]:41687 "EHLO mail-ed1-f41.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343707AbhDPU7g (ORCPT ); Fri, 16 Apr 2021 16:59:36 -0400 Received: by mail-ed1-f41.google.com with SMTP id z1so33832404edb.8 for ; Fri, 16 Apr 2021 13:59:10 -0700 (PDT) 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=MVmWMVb6qko8ecvNCz9h65V4X7tSNjC2VksULNu6kzQ=; b=Q31QO6a0reOkrHfp/M0XTLuVyhBSQeCS76Y9RzITmaxhALUZ4GalFEjHSricoVuoSR 9U1bZI2Pdzeu2JJfJHvF6pvILEBPhR6YEJaDwhOevOGlarCGk3xo+vu9VS5UPQ+ImGCZ dEIpirnx/1cru6IsBPaO3wwlaGwD7rSRhh9kNcVUffZyZO4uV5WH+ixfoKMv/W7re2zW iZ1mYsg6Wx6sx5BvPleWFETuhAitsNT1ezUy2fptCPdwgRih3otNi1Z/ujwdi59+W2pE iHDOXB9khOjBv3kxGC8yIwP6zzY4QAiM4+H4OA2zAsmXfdjPHY6amzzGvy9TKuRyhjbf cQIw== X-Gm-Message-State: AOAM531/nJoDyprxFpKVO/LBtd9pH5JUzwUuKuJ5WT/OKVrtY8zQtCzT oGOTuxLnr9Wcmqw8vdcwnzI= X-Google-Smtp-Source: ABdhPJw8/3g+FUFbqjQOKdKJpIFFWKqGOdi+8SHbsmuggqq0meRiUek9aNNf20UsYfB804xJ3tjSKw== X-Received: by 2002:a05:6402:438f:: with SMTP id o15mr12206150edc.123.1618606750090; Fri, 16 Apr 2021 13:59:10 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id n11sm5103864ejg.43.2021.04.16.13.59.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Apr 2021 13:59:09 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: =?utf-8?q?Pali_Roh=C3=A1r?= , "Oliver O'Halloran" , Greg Kroah-Hartman , Daniel Vetter , Joe Perches , Dan Williams , Mauro Carvalho Chehab , David Sterba , linux-pci@vger.kernel.org Subject: [PATCH 12/20] PCI: Rearrange attributes from the pci_dev_config_attr_group Date: Fri, 16 Apr 2021 20:58:48 +0000 Message-Id: <20210416205856.3234481-13-kw@linux.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210416205856.3234481-1-kw@linux.com> References: <20210416205856.3234481-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org When new sysfs objects were added to the PCI device over time, the code that implemented new attributes has been added in many different places in the pci-sysfs.c file. This makes it hard to read and also hard to find relevant code. Thus, move attributes that are part of the "pci_dev_config_attr_group" attribute group to the top of the file. No functional change intended. Suggested-by: Bjorn Helgaas Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-sysfs.c | 336 ++++++++++++++++++++-------------------- 1 file changed, 168 insertions(+), 168 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index 5f83ff087f2c..513e19154a93 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -483,6 +483,174 @@ static const struct attribute_group pci_dev_group = { .attrs = pci_dev_attrs, }; +static ssize_t config_read(struct file *filp, struct kobject *kobj, + struct bin_attribute *bin_attr, char *buf, + loff_t off, size_t count) +{ + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); + unsigned int size = 64; + loff_t init_off = off; + u8 *data = (u8 *)buf; + + /* Several chips lock up trying to read undefined config space */ + if (file_ns_capable(filp, &init_user_ns, CAP_SYS_ADMIN)) + size = pdev->cfg_size; + else if (pdev->hdr_type == PCI_HEADER_TYPE_CARDBUS) + size = 128; + + if (off > size) + return 0; + + if (off + count > size) { + size -= off; + count = size; + } else { + size = count; + } + + pci_config_pm_runtime_get(pdev); + + if ((off & 1) && size) { + u8 val; + pci_user_read_config_byte(pdev, off, &val); + data[off - init_off] = val; + off++; + size--; + } + + if ((off & 3) && size > 2) { + u16 val; + pci_user_read_config_word(pdev, off, &val); + data[off - init_off] = val & 0xff; + data[off - init_off + 1] = (val >> 8) & 0xff; + off += 2; + size -= 2; + } + + while (size > 3) { + u32 val; + pci_user_read_config_dword(pdev, off, &val); + data[off - init_off] = val & 0xff; + data[off - init_off + 1] = (val >> 8) & 0xff; + data[off - init_off + 2] = (val >> 16) & 0xff; + data[off - init_off + 3] = (val >> 24) & 0xff; + off += 4; + size -= 4; + cond_resched(); + } + + if (size >= 2) { + u16 val; + pci_user_read_config_word(pdev, off, &val); + data[off - init_off] = val & 0xff; + data[off - init_off + 1] = (val >> 8) & 0xff; + off += 2; + size -= 2; + } + + if (size > 0) { + u8 val; + pci_user_read_config_byte(pdev, off, &val); + data[off - init_off] = val; + off++; + --size; + } + + pci_config_pm_runtime_put(pdev); + + return count; +} + +static ssize_t config_write(struct file *filp, struct kobject *kobj, + struct bin_attribute *bin_attr, char *buf, + loff_t off, size_t count) +{ + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); + unsigned int size = count; + loff_t init_off = off; + u8 *data = (u8 *)buf; + size_t ret; + + ret = security_locked_down(LOCKDOWN_PCI_ACCESS); + if (ret) + return ret; + + if (off > pdev->cfg_size) + return 0; + + if (off + count > pdev->cfg_size) { + size = pdev->cfg_size - off; + count = size; + } + + pci_config_pm_runtime_get(pdev); + + if ((off & 1) && size) { + pci_user_write_config_byte(pdev, off, data[off - init_off]); + off++; + size--; + } + + if ((off & 3) && size > 2) { + u16 val = data[off - init_off]; + val |= (u16) data[off - init_off + 1] << 8; + pci_user_write_config_word(pdev, off, val); + off += 2; + size -= 2; + } + + while (size > 3) { + u32 val = data[off - init_off]; + val |= (u32) data[off - init_off + 1] << 8; + val |= (u32) data[off - init_off + 2] << 16; + val |= (u32) data[off - init_off + 3] << 24; + pci_user_write_config_dword(pdev, off, val); + off += 4; + size -= 4; + } + + if (size >= 2) { + u16 val = data[off - init_off]; + val |= (u16) data[off - init_off + 1] << 8; + pci_user_write_config_word(pdev, off, val); + off += 2; + size -= 2; + } + + if (size) { + pci_user_write_config_byte(pdev, off, data[off - init_off]); + off++; + --size; + } + + pci_config_pm_runtime_put(pdev); + + return count; +} +static BIN_ATTR_RW(config, 0); + +static struct bin_attribute *pci_dev_config_attrs[] = { + &bin_attr_config, + NULL, +}; + +static umode_t pci_dev_config_attr_is_visible(struct kobject *kobj, + struct bin_attribute *a, int n) +{ + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); + + a->size = PCI_CFG_SPACE_SIZE; + if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) + a->size = PCI_CFG_SPACE_EXP_SIZE; + + return a->attr.mode; +} + +static const struct attribute_group pci_dev_config_attr_group = { + .bin_attrs = pci_dev_config_attrs, + .is_bin_visible = pci_dev_config_attr_is_visible, +}; + /* * PCI Bus Class Devices */ @@ -721,174 +889,6 @@ static ssize_t boot_vga_show(struct device *dev, } static DEVICE_ATTR_RO(boot_vga); -static ssize_t config_read(struct file *filp, struct kobject *kobj, - struct bin_attribute *bin_attr, char *buf, - loff_t off, size_t count) -{ - struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); - unsigned int size = 64; - loff_t init_off = off; - u8 *data = (u8 *)buf; - - /* Several chips lock up trying to read undefined config space */ - if (file_ns_capable(filp, &init_user_ns, CAP_SYS_ADMIN)) - size = pdev->cfg_size; - else if (pdev->hdr_type == PCI_HEADER_TYPE_CARDBUS) - size = 128; - - if (off > size) - return 0; - - if (off + count > size) { - size -= off; - count = size; - } else { - size = count; - } - - pci_config_pm_runtime_get(pdev); - - if ((off & 1) && size) { - u8 val; - pci_user_read_config_byte(pdev, off, &val); - data[off - init_off] = val; - off++; - size--; - } - - if ((off & 3) && size > 2) { - u16 val; - pci_user_read_config_word(pdev, off, &val); - data[off - init_off] = val & 0xff; - data[off - init_off + 1] = (val >> 8) & 0xff; - off += 2; - size -= 2; - } - - while (size > 3) { - u32 val; - pci_user_read_config_dword(pdev, off, &val); - data[off - init_off] = val & 0xff; - data[off - init_off + 1] = (val >> 8) & 0xff; - data[off - init_off + 2] = (val >> 16) & 0xff; - data[off - init_off + 3] = (val >> 24) & 0xff; - off += 4; - size -= 4; - cond_resched(); - } - - if (size >= 2) { - u16 val; - pci_user_read_config_word(pdev, off, &val); - data[off - init_off] = val & 0xff; - data[off - init_off + 1] = (val >> 8) & 0xff; - off += 2; - size -= 2; - } - - if (size > 0) { - u8 val; - pci_user_read_config_byte(pdev, off, &val); - data[off - init_off] = val; - off++; - --size; - } - - pci_config_pm_runtime_put(pdev); - - return count; -} - -static ssize_t config_write(struct file *filp, struct kobject *kobj, - struct bin_attribute *bin_attr, char *buf, - loff_t off, size_t count) -{ - struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); - unsigned int size = count; - loff_t init_off = off; - u8 *data = (u8 *)buf; - size_t ret; - - ret = security_locked_down(LOCKDOWN_PCI_ACCESS); - if (ret) - return ret; - - if (off > pdev->cfg_size) - return 0; - - if (off + count > pdev->cfg_size) { - size = pdev->cfg_size - off; - count = size; - } - - pci_config_pm_runtime_get(pdev); - - if ((off & 1) && size) { - pci_user_write_config_byte(pdev, off, data[off - init_off]); - off++; - size--; - } - - if ((off & 3) && size > 2) { - u16 val = data[off - init_off]; - val |= (u16) data[off - init_off + 1] << 8; - pci_user_write_config_word(pdev, off, val); - off += 2; - size -= 2; - } - - while (size > 3) { - u32 val = data[off - init_off]; - val |= (u32) data[off - init_off + 1] << 8; - val |= (u32) data[off - init_off + 2] << 16; - val |= (u32) data[off - init_off + 3] << 24; - pci_user_write_config_dword(pdev, off, val); - off += 4; - size -= 4; - } - - if (size >= 2) { - u16 val = data[off - init_off]; - val |= (u16) data[off - init_off + 1] << 8; - pci_user_write_config_word(pdev, off, val); - off += 2; - size -= 2; - } - - if (size) { - pci_user_write_config_byte(pdev, off, data[off - init_off]); - off++; - --size; - } - - pci_config_pm_runtime_put(pdev); - - return count; -} -static BIN_ATTR_RW(config, 0); - -static struct bin_attribute *pci_dev_config_attrs[] = { - &bin_attr_config, - NULL, -}; - -static umode_t pci_dev_config_attr_is_visible(struct kobject *kobj, - struct bin_attribute *a, int n) -{ - struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); - - a->size = PCI_CFG_SPACE_SIZE; - if (pdev->cfg_size > PCI_CFG_SPACE_SIZE) - a->size = PCI_CFG_SPACE_EXP_SIZE; - - return a->attr.mode; -} - -static const struct attribute_group pci_dev_config_attr_group = { - .bin_attrs = pci_dev_config_attrs, - .is_bin_visible = pci_dev_config_attr_is_visible, -}; - #ifdef HAVE_PCI_LEGACY /** * pci_read_legacy_io - read byte(s) from legacy I/O port space From patchwork Fri Apr 16 20:58:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?S3J6eXN6dG9mIFdpbGN6ee+/ve+/vXNraQ==?= X-Patchwork-Id: 12208813 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9DC1AC433ED for ; Fri, 16 Apr 2021 20:59:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 726B0613EA for ; Fri, 16 Apr 2021 20:59:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343707AbhDPU7h (ORCPT ); Fri, 16 Apr 2021 16:59:37 -0400 Received: from mail-ej1-f44.google.com ([209.85.218.44]:41629 "EHLO mail-ej1-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343791AbhDPU7h (ORCPT ); Fri, 16 Apr 2021 16:59:37 -0400 Received: by mail-ej1-f44.google.com with SMTP id mh2so22452907ejb.8 for ; Fri, 16 Apr 2021 13:59:11 -0700 (PDT) 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=xYXmofRcGdnImmAQP35u+j5nMlwM8TKpfiEH+XxcCUo=; b=YzpppqHtc7J4BuYlmAebR7fIiZ99rdhBVzoz1vey0j8w2O94Z9/5AC08RAhzwqNWhs ENdxhGJHGiTJi4FXygUqFWixikJimGYMlNBGrqSN49DVgD4Cq1Wnu4vv5Q2OLgO+wZ9Y 9EHTqwlFZ1WnptCDGKrRHF/NMWisa7E2o6FXnwGJFhfwlP4g6S0T4zPbAcXi7kMoSVUy +wKqM1978660Mg2EcjYqAWH+WvxxfXkSWSjD8Hz0UaokvZ7Ni6zMO0uwJgx4bo3pMLAN zWtmIOugeVYJ/BWdTTX352m/KYDnsN9cZgX/k89GUcd575+UZjWNJjYyaLJ+qRicDsM8 lL/Q== X-Gm-Message-State: AOAM530G/DJ7W43PsclDzD5YZaDwP2dWBL29u2q9WchM2Xj6SL9/FMj5 Z/MPC5aYEEd2VBxGNXI0yVU= X-Google-Smtp-Source: ABdhPJwpFWTXUCe/YqezHByd/SX4RShjdY88amiM8Mo1OdayvXj9e1A0nfVjo4R7cxM05XXVjbNd2g== X-Received: by 2002:a17:906:f6c1:: with SMTP id jo1mr9842089ejb.262.1618606751146; Fri, 16 Apr 2021 13:59:11 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id n11sm5103864ejg.43.2021.04.16.13.59.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Apr 2021 13:59:10 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: =?utf-8?q?Pali_Roh=C3=A1r?= , "Oliver O'Halloran" , Greg Kroah-Hartman , Daniel Vetter , Joe Perches , Dan Williams , Mauro Carvalho Chehab , David Sterba , linux-pci@vger.kernel.org Subject: [PATCH 13/20] PCI: Rearrange attributes from the pci_dev_rom_attr_group Date: Fri, 16 Apr 2021 20:58:49 +0000 Message-Id: <20210416205856.3234481-14-kw@linux.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210416205856.3234481-1-kw@linux.com> References: <20210416205856.3234481-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org When new sysfs objects were added to the PCI device over time, the code that implemented new attributes has been added in many different places in the pci-sysfs.c file. This makes it hard to read and also hard to find relevant code. Thus, move attributes that are part of the "pci_dev_rom_attr_group" attribute group to the top of the file. No functional change intended. Suggested-by: Bjorn Helgaas Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-sysfs.c | 186 ++++++++++++++++++++-------------------- 1 file changed, 93 insertions(+), 93 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index 513e19154a93..794c97424456 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -651,6 +651,99 @@ static const struct attribute_group pci_dev_config_attr_group = { .is_bin_visible = pci_dev_config_attr_is_visible, }; +/** + * rom_read - read a PCI ROM + * @filp: sysfs file + * @kobj: kernel object handle + * @bin_attr: struct bin_attribute for this file + * @buf: where to put the data we read from the ROM + * @off: file offset + * @count: number of bytes to read + * + * Put @count bytes starting at @off into @buf from the ROM in the PCI + * device corresponding to @kobj. + */ +static ssize_t rom_read(struct file *filp, struct kobject *kobj, + struct bin_attribute *bin_attr, char *buf, + loff_t off, size_t count) +{ + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); + void __iomem *rom; + size_t size; + + if (!pdev->rom_attr_enabled) + return -EINVAL; + + rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */ + if (!rom || !size) + return -EIO; + + if (off >= size) { + count = 0; + } else { + if (off + count > size) + count = size - off; + + memcpy_fromio(buf, rom + off, count); + } + pci_unmap_rom(pdev, rom); + + return count; +} + +/** + * rom_write - used to enable access to the PCI ROM display + * @filp: sysfs file + * @kobj: kernel object handle + * @bin_attr: struct bin_attribute for this file + * @buf: user input + * @off: file offset + * @count: number of byte in input + * + * writing anything except 0 enables it + */ +static ssize_t rom_write(struct file *filp, struct kobject *kobj, + struct bin_attribute *bin_attr, char *buf, + loff_t off, size_t count) +{ + bool allowed; + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); + + if (kstrtobool(buf, &allowed) < 0) + return -EINVAL; + + pdev->rom_attr_enabled = !!allowed; + + return count; +} +static BIN_ATTR_ADMIN_RW(rom, 0); + +static struct bin_attribute *pci_dev_rom_attrs[] = { + &bin_attr_rom, + NULL, +}; + +static umode_t pci_dev_rom_attr_is_visible(struct kobject *kobj, + struct bin_attribute *a, int n) +{ + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); + size_t rom_size; + + /* If the device has a ROM, try to expose it in sysfs. */ + rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE); + if (!rom_size) + return 0; + + a->size = rom_size; + + return a->attr.mode; +} + +static const struct attribute_group pci_dev_rom_attr_group = { + .bin_attrs = pci_dev_rom_attrs, + .is_bin_visible = pci_dev_rom_attr_is_visible, +}; + /* * PCI Bus Class Devices */ @@ -1313,99 +1406,6 @@ int __weak pci_create_resource_files(struct pci_dev *pdev) { return 0; } void __weak pci_remove_resource_files(struct pci_dev *pdev) { return; } #endif -/** - * rom_write - used to enable access to the PCI ROM display - * @filp: sysfs file - * @kobj: kernel object handle - * @bin_attr: struct bin_attribute for this file - * @buf: user input - * @off: file offset - * @count: number of byte in input - * - * writing anything except 0 enables it - */ -static ssize_t rom_write(struct file *filp, struct kobject *kobj, - struct bin_attribute *bin_attr, char *buf, - loff_t off, size_t count) -{ - bool allowed; - struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); - - if (kstrtobool(buf, &allowed) < 0) - return -EINVAL; - - pdev->rom_attr_enabled = !!allowed; - - return count; -} - -/** - * rom_read - read a PCI ROM - * @filp: sysfs file - * @kobj: kernel object handle - * @bin_attr: struct bin_attribute for this file - * @buf: where to put the data we read from the ROM - * @off: file offset - * @count: number of bytes to read - * - * Put @count bytes starting at @off into @buf from the ROM in the PCI - * device corresponding to @kobj. - */ -static ssize_t rom_read(struct file *filp, struct kobject *kobj, - struct bin_attribute *bin_attr, char *buf, - loff_t off, size_t count) -{ - struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); - void __iomem *rom; - size_t size; - - if (!pdev->rom_attr_enabled) - return -EINVAL; - - rom = pci_map_rom(pdev, &size); /* size starts out as PCI window size */ - if (!rom || !size) - return -EIO; - - if (off >= size) { - count = 0; - } else { - if (off + count > size) - count = size - off; - - memcpy_fromio(buf, rom + off, count); - } - pci_unmap_rom(pdev, rom); - - return count; -} -static BIN_ATTR_ADMIN_RW(rom, 0); - -static struct bin_attribute *pci_dev_rom_attrs[] = { - &bin_attr_rom, - NULL, -}; - -static umode_t pci_dev_rom_attr_is_visible(struct kobject *kobj, - struct bin_attribute *a, int n) -{ - struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); - size_t rom_size; - - /* If the device has a ROM, try to expose it in sysfs. */ - rom_size = pci_resource_len(pdev, PCI_ROM_RESOURCE); - if (!rom_size) - return 0; - - a->size = rom_size; - - return a->attr.mode; -} - -static const struct attribute_group pci_dev_rom_attr_group = { - .bin_attrs = pci_dev_rom_attrs, - .is_bin_visible = pci_dev_rom_attr_is_visible, -}; - static ssize_t reset_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) From patchwork Fri Apr 16 20:58:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?S3J6eXN6dG9mIFdpbGN6ee+/ve+/vXNraQ==?= X-Patchwork-Id: 12208815 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E33D4C433B4 for ; Fri, 16 Apr 2021 20:59:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B235F613DA for ; Fri, 16 Apr 2021 20:59:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343845AbhDPU7i (ORCPT ); Fri, 16 Apr 2021 16:59:38 -0400 Received: from mail-ed1-f50.google.com ([209.85.208.50]:46993 "EHLO mail-ed1-f50.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343791AbhDPU7i (ORCPT ); Fri, 16 Apr 2021 16:59:38 -0400 Received: by mail-ed1-f50.google.com with SMTP id h10so33797363edt.13 for ; Fri, 16 Apr 2021 13:59:12 -0700 (PDT) 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=RG9PYVKkM4vrPvILHcpTFnJdF4PMSdFWokhECO6VD/s=; b=XKwvUiezLeO7fhqJamC3n5KMYqpshbKI/ix1dCFNi2wrLhyNvEn6ErkdJnWcSWto6f hFhjgTXqQDSbIR2gyWiN8jD1jLFGjrnTWMC8AxV06SSRyELKG6RJfqf7j4Yqi61PI2VP AbnjhgXtCO5fdhDz0n3Hkuv7d0By8oI8IeQMachGiyP2qtzg95uzU5d1tJK/35SmK2Dk k+DoIbi8LEWVxkcwq9FziqA/ZVJnGVytmP3SHlRLmyWcdlNmWkAiR59eziIbeREu/pwh leKNBSs/CmYCrYRcRkLa2D5cd2dgK6mfTtYGRUS3bp//fXHmw3eWlqD8JK4YFls14/Ey aQpw== X-Gm-Message-State: AOAM531IwQdW6SYD2tRE4qsZLrpVDqSFBbE7eyMOMTimbN6EMYjNulGC 4VUyDXv4XUQh/pjY8zuDBPU= X-Google-Smtp-Source: ABdhPJwWWkOHsFvao3fgkHVFIwf4xBPY7JUbBSbKFP4MC35OQACrhvW8O78Vu16lblXG0oxcQtYXwg== X-Received: by 2002:a05:6402:26d3:: with SMTP id x19mr12258449edd.349.1618606752062; Fri, 16 Apr 2021 13:59:12 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id n11sm5103864ejg.43.2021.04.16.13.59.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Apr 2021 13:59:11 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: =?utf-8?q?Pali_Roh=C3=A1r?= , "Oliver O'Halloran" , Greg Kroah-Hartman , Daniel Vetter , Joe Perches , Dan Williams , Mauro Carvalho Chehab , David Sterba , linux-pci@vger.kernel.org Subject: [PATCH 14/20] PCI: Rearrange attributes from the pci_dev_reset_attr_group Date: Fri, 16 Apr 2021 20:58:50 +0000 Message-Id: <20210416205856.3234481-15-kw@linux.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210416205856.3234481-1-kw@linux.com> References: <20210416205856.3234481-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org When new sysfs objects were added to the PCI device over time, the code that implemented new attributes has been added in many different places in the pci-sysfs.c file. This makes it hard to read and also hard to find relevant code. Thus, move attributes that are part of the "pci_dev_reset_attr_group" attribute group to the top of the file. No functional change intended. Suggested-by: Bjorn Helgaas Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-sysfs.c | 90 ++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index 794c97424456..f18b1728aefa 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -744,6 +744,51 @@ static const struct attribute_group pci_dev_rom_attr_group = { .is_bin_visible = pci_dev_rom_attr_is_visible, }; +static ssize_t reset_store(struct device *dev, + struct device_attribute *attr, const char *buf, + size_t count) +{ + bool reset; + struct pci_dev *pdev = to_pci_dev(dev); + ssize_t ret; + + if (kstrtobool(buf, &reset) < 0) + return -EINVAL; + + if (!reset) + return -EINVAL; + + pm_runtime_get_sync(dev); + ret = pci_reset_function(pdev); + pm_runtime_put(dev); + if (ret < 0) + return ret; + + return count; +} +static DEVICE_ATTR_WO(reset); + +static struct attribute *pci_dev_reset_attrs[] = { + &dev_attr_reset.attr, + NULL, +}; + +static umode_t pci_dev_reset_attr_is_visible(struct kobject *kobj, + struct attribute *a, int n) +{ + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); + + if (!pdev->reset_fn) + return 0; + + return a->mode; +} + +static const struct attribute_group pci_dev_reset_attr_group = { + .attrs = pci_dev_reset_attrs, + .is_visible = pci_dev_reset_attr_is_visible, +}; + /* * PCI Bus Class Devices */ @@ -1406,51 +1451,6 @@ int __weak pci_create_resource_files(struct pci_dev *pdev) { return 0; } void __weak pci_remove_resource_files(struct pci_dev *pdev) { return; } #endif -static ssize_t reset_store(struct device *dev, - struct device_attribute *attr, const char *buf, - size_t count) -{ - bool reset; - struct pci_dev *pdev = to_pci_dev(dev); - ssize_t ret; - - if (kstrtobool(buf, &reset) < 0) - return -EINVAL; - - if (!reset) - return -EINVAL; - - pm_runtime_get_sync(dev); - ret = pci_reset_function(pdev); - pm_runtime_put(dev); - if (ret < 0) - return ret; - - return count; -} -static DEVICE_ATTR_WO(reset); - -static struct attribute *pci_dev_reset_attrs[] = { - &dev_attr_reset.attr, - NULL, -}; - -static umode_t pci_dev_reset_attr_is_visible(struct kobject *kobj, - struct attribute *a, int n) -{ - struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); - - if (!pdev->reset_fn) - return 0; - - return a->mode; -} - -static const struct attribute_group pci_dev_reset_attr_group = { - .attrs = pci_dev_reset_attrs, - .is_visible = pci_dev_reset_attr_is_visible, -}; - int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev) { if (!sysfs_initialized) From patchwork Fri Apr 16 20:58:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?S3J6eXN6dG9mIFdpbGN6ee+/ve+/vXNraQ==?= X-Patchwork-Id: 12208817 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E7F5AC433B4 for ; Fri, 16 Apr 2021 20:59:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A59C1613F0 for ; Fri, 16 Apr 2021 20:59:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344001AbhDPU7m (ORCPT ); Fri, 16 Apr 2021 16:59:42 -0400 Received: from mail-ej1-f51.google.com ([209.85.218.51]:46963 "EHLO mail-ej1-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343938AbhDPU7j (ORCPT ); Fri, 16 Apr 2021 16:59:39 -0400 Received: by mail-ej1-f51.google.com with SMTP id u21so44008800ejo.13 for ; Fri, 16 Apr 2021 13:59:13 -0700 (PDT) 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=+Fv8uv3c+thITNzU1vdfCbdGQyGnkllJf20yfYwGOy0=; b=QOVTgDMCjadqCPgX6Ht1/HTSo6AdCIlOvt0FH886uQUD5PAos3i0FUD55syAu8SLzp n/aSn58bDmxrZUnEd/TK9gHbR5XDRSUX8VJAfhGRWBnIFhCgeLKkLjqpBS8VN4QaOQ+E pmJJu2vgot/jQ6Zpm0XCHtXfnsbyceDftfzthqC4ILTV9EAVSkFbMhaUm1jpXFoaHYgx KrG84OCSHYy7aNThy9qrRx9s6VMMQ92k/HhKxOiA1lRuUoorPplVVSSIEEIqD7T3ELZq qNLPLvOpElwftH/17xJ6/VIfixpiWSw8kEP9g6l474x4RiAAY097SzuYUfmA/IGxo53i hM4Q== X-Gm-Message-State: AOAM531NlLHLg7V709+O9ctZDk8LLp7nlJRWaWm/35MYsyd5P49zUxEE CPxZXu7I6NoWDd4Kxa8GB7k= X-Google-Smtp-Source: ABdhPJz5dQbLqVcvRLHFH40AKIyLvxrXJLMjuZ1OzSLECwP+xMY4BaTiIGnLSTSoXYa4VY6+As5G/A== X-Received: by 2002:a17:906:fa07:: with SMTP id lo7mr10291431ejb.321.1618606752998; Fri, 16 Apr 2021 13:59:12 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id n11sm5103864ejg.43.2021.04.16.13.59.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Apr 2021 13:59:12 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: =?utf-8?q?Pali_Roh=C3=A1r?= , "Oliver O'Halloran" , Greg Kroah-Hartman , Daniel Vetter , Joe Perches , Dan Williams , Mauro Carvalho Chehab , David Sterba , linux-pci@vger.kernel.org Subject: [PATCH 15/20] PCI: Rearrange attributes from the pci_dev_attr_group Date: Fri, 16 Apr 2021 20:58:51 +0000 Message-Id: <20210416205856.3234481-16-kw@linux.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210416205856.3234481-1-kw@linux.com> References: <20210416205856.3234481-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org When new sysfs objects were added to the PCI device over time, the code that implemented new attributes has been added in many different places in the pci-sysfs.c file. This makes it hard to read and also hard to find relevant code. Thus, move attributes that are part of the "pci_dev_attr_group" attribute group to the top of the file. No functional change intended. Suggested-by: Bjorn Helgaas Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-sysfs.c | 74 ++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index f18b1728aefa..fd89a391b1c7 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -789,6 +789,43 @@ static const struct attribute_group pci_dev_reset_attr_group = { .is_visible = pci_dev_reset_attr_is_visible, }; +static ssize_t boot_vga_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct pci_dev *pdev = to_pci_dev(dev); + struct pci_dev *vga_dev = vga_default_device(); + + if (vga_dev) + return sysfs_emit(buf, "%u\n", (pdev == vga_dev)); + + return sysfs_emit(buf, "%u\n", + !!(pdev->resource[PCI_ROM_RESOURCE].flags & + IORESOURCE_ROM_SHADOW)); +} +static DEVICE_ATTR_RO(boot_vga); + +static struct attribute *pci_dev_dev_attrs[] = { + &dev_attr_boot_vga.attr, + NULL, +}; + +static umode_t pci_dev_attr_is_visible(struct kobject *kobj, + struct attribute *a, int n) +{ + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); + + if (a == &dev_attr_boot_vga.attr) + if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA) + return 0; + + return a->mode; +} + +static const struct attribute_group pci_dev_attr_group = { + .attrs = pci_dev_dev_attrs, + .is_visible = pci_dev_attr_is_visible, +}; + /* * PCI Bus Class Devices */ @@ -1012,21 +1049,6 @@ const struct attribute_group *pcibus_groups[] = { NULL, }; -static ssize_t boot_vga_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct pci_dev *pdev = to_pci_dev(dev); - struct pci_dev *vga_dev = vga_default_device(); - - if (vga_dev) - return sysfs_emit(buf, "%u\n", (pdev == vga_dev)); - - return sysfs_emit(buf, "%u\n", - !!(pdev->resource[PCI_ROM_RESOURCE].flags & - IORESOURCE_ROM_SHADOW)); -} -static DEVICE_ATTR_RO(boot_vga); - #ifdef HAVE_PCI_LEGACY /** * pci_read_legacy_io - read byte(s) from legacy I/O port space @@ -1495,23 +1517,6 @@ static int __init pci_sysfs_init(void) } late_initcall(pci_sysfs_init); -static struct attribute *pci_dev_dev_attrs[] = { - &dev_attr_boot_vga.attr, - NULL, -}; - -static umode_t pci_dev_attr_is_visible(struct kobject *kobj, - struct attribute *a, int n) -{ - struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); - - if (a == &dev_attr_boot_vga.attr) - if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA) - return 0; - - return a->mode; -} - static struct attribute *pci_dev_hp_attrs[] = { &dev_attr_remove.attr, &dev_attr_dev_rescan.attr, @@ -1571,11 +1576,6 @@ static const struct attribute_group pci_dev_hp_attr_group = { .is_visible = pci_dev_hp_attr_is_visible, }; -static const struct attribute_group pci_dev_attr_group = { - .attrs = pci_dev_dev_attrs, - .is_visible = pci_dev_attr_is_visible, -}; - static const struct attribute_group pci_bridge_attr_group = { .attrs = pci_bridge_attrs, .is_visible = pci_bridge_attr_is_visible, From patchwork Fri Apr 16 20:58:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?S3J6eXN6dG9mIFdpbGN6ee+/ve+/vXNraQ==?= X-Patchwork-Id: 12208819 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D47D2C43460 for ; Fri, 16 Apr 2021 20:59:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AC5B5613E0 for ; Fri, 16 Apr 2021 20:59:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343888AbhDPU7n (ORCPT ); Fri, 16 Apr 2021 16:59:43 -0400 Received: from mail-ed1-f52.google.com ([209.85.208.52]:38640 "EHLO mail-ed1-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343947AbhDPU7j (ORCPT ); Fri, 16 Apr 2021 16:59:39 -0400 Received: by mail-ed1-f52.google.com with SMTP id m3so33809395edv.5 for ; Fri, 16 Apr 2021 13:59:14 -0700 (PDT) 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=6qxNYzZBqS/3l+6iffEqt9nD120CmaQyJj6jPaGw3Nw=; b=RNAVIpVFt4KCa6oHr1MdBrxxUpZYMq608p9Kq8NT0zVexsFowsK7GEqodHk5wECLwd cw5YWoRdEZ5ETWPlYd6rCTOBapC/LRsMD1JwY6/EAojf3bQVGh853iCqXxPLVziGjcYD 0kHGL4uKHOuxGgX4Y+3MrFgh+XDhnXfS0DKtbiv4BcNrIiDn11HbduKVlcPEoDMtyey2 BfMEfw1LWwNAAV8MSYaMHWAs0Qf9wUV2H+GQ9MDta6IZwr72E+Ec4y7dPZUu1zsmmRvD RY3nnskWDCJH7TvHfyEW/VJsLvSqXV3F3Pj7rAqBYijddBzxhSCO30JJXY8h+MHdxXPD a5IA== X-Gm-Message-State: AOAM533XbXcRH2d7j52Kh6L5hJchk5kTreKX4maHs5/FeIA0rOWRCIqA OLQPNCGHWRxbjWljEEONIhc= X-Google-Smtp-Source: ABdhPJxSwupV8+2GHx4KMde0bKr/CYzofemZLeHLeCRkMLFoTx1PBB6mQjrW1lzjaFnSXEakAZpx5g== X-Received: by 2002:a05:6402:b66:: with SMTP id cb6mr11504723edb.248.1618606753911; Fri, 16 Apr 2021 13:59:13 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id n11sm5103864ejg.43.2021.04.16.13.59.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Apr 2021 13:59:13 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: =?utf-8?q?Pali_Roh=C3=A1r?= , "Oliver O'Halloran" , Greg Kroah-Hartman , Daniel Vetter , Joe Perches , Dan Williams , Mauro Carvalho Chehab , David Sterba , linux-pci@vger.kernel.org Subject: [PATCH 16/20] PCI: Rearrange attributes from the pci_dev_hp_attr_group Date: Fri, 16 Apr 2021 20:58:52 +0000 Message-Id: <20210416205856.3234481-17-kw@linux.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210416205856.3234481-1-kw@linux.com> References: <20210416205856.3234481-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org When new sysfs objects were added to the PCI device over time, the code that implemented new attributes has been added in many different places in the pci-sysfs.c file. This makes it hard to read and also hard to find relevant code. Thus, collect all the attributes that are part of the "pci_dev_hp_attr_group" attribute group together and move to the top of the file sorting everything attribute in the order of use. No functional change intended. Suggested-by: Bjorn Helgaas Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-sysfs.c | 120 ++++++++++++++++++++-------------------- 1 file changed, 60 insertions(+), 60 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index fd89a391b1c7..bf909e9a9528 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -826,6 +826,66 @@ static const struct attribute_group pci_dev_attr_group = { .is_visible = pci_dev_attr_is_visible, }; +static ssize_t remove_store(struct device *dev, + struct device_attribute *attr, const char *buf, + size_t count) +{ + bool remove; + struct pci_dev *pdev = to_pci_dev(dev); + + if (kstrtobool(buf, &remove) < 0) + return -EINVAL; + + if (remove && device_remove_file_self(dev, attr)) + pci_stop_and_remove_bus_device_locked(pdev); + + return count; +} +static DEVICE_ATTR_IGNORE_LOCKDEP(remove, 0220, NULL, remove_store); + +static ssize_t dev_rescan_store(struct device *dev, + struct device_attribute *attr, const char *buf, + size_t count) +{ + bool rescan; + struct pci_dev *pdev = to_pci_dev(dev); + + if (kstrtobool(buf, &rescan) < 0) + return -EINVAL; + + if (rescan) { + pci_lock_rescan_remove(); + pci_rescan_bus(pdev->bus); + pci_unlock_rescan_remove(); + } + + return count; +} +static struct device_attribute dev_attr_dev_rescan = __ATTR(rescan, 0200, NULL, + dev_rescan_store); + +static struct attribute *pci_dev_hp_attrs[] = { + &dev_attr_remove.attr, + &dev_attr_dev_rescan.attr, + NULL, +}; + +static umode_t pci_dev_hp_attr_is_visible(struct kobject *kobj, + struct attribute *a, int n) +{ + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); + + if (pdev->is_virtfn) + return 0; + + return a->mode; +} + +static const struct attribute_group pci_dev_hp_attr_group = { + .attrs = pci_dev_hp_attrs, + .is_visible = pci_dev_hp_attr_is_visible, +}; + /* * PCI Bus Class Devices */ @@ -957,44 +1017,6 @@ const struct attribute_group *pci_bus_groups[] = { NULL, }; -static ssize_t dev_rescan_store(struct device *dev, - struct device_attribute *attr, const char *buf, - size_t count) -{ - bool rescan; - struct pci_dev *pdev = to_pci_dev(dev); - - if (kstrtobool(buf, &rescan) < 0) - return -EINVAL; - - if (rescan) { - pci_lock_rescan_remove(); - pci_rescan_bus(pdev->bus); - pci_unlock_rescan_remove(); - } - - return count; -} -static struct device_attribute dev_attr_dev_rescan = __ATTR(rescan, 0200, NULL, - dev_rescan_store); - -static ssize_t remove_store(struct device *dev, - struct device_attribute *attr, const char *buf, - size_t count) -{ - bool remove; - struct pci_dev *pdev = to_pci_dev(dev); - - if (kstrtobool(buf, &remove) < 0) - return -EINVAL; - - if (remove && device_remove_file_self(dev, attr)) - pci_stop_and_remove_bus_device_locked(pdev); - - return count; -} -static DEVICE_ATTR_IGNORE_LOCKDEP(remove, 0220, NULL, remove_store); - static ssize_t bus_rescan_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) @@ -1517,23 +1539,6 @@ static int __init pci_sysfs_init(void) } late_initcall(pci_sysfs_init); -static struct attribute *pci_dev_hp_attrs[] = { - &dev_attr_remove.attr, - &dev_attr_dev_rescan.attr, - NULL, -}; - -static umode_t pci_dev_hp_attr_is_visible(struct kobject *kobj, - struct attribute *a, int n) -{ - struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); - - if (pdev->is_virtfn) - return 0; - - return a->mode; -} - static umode_t pci_bridge_attr_is_visible(struct kobject *kobj, struct attribute *a, int n) { @@ -1571,11 +1576,6 @@ const struct attribute_group *pci_dev_groups[] = { NULL, }; -static const struct attribute_group pci_dev_hp_attr_group = { - .attrs = pci_dev_hp_attrs, - .is_visible = pci_dev_hp_attr_is_visible, -}; - static const struct attribute_group pci_bridge_attr_group = { .attrs = pci_bridge_attrs, .is_visible = pci_bridge_attr_is_visible, From patchwork Fri Apr 16 20:58:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?S3J6eXN6dG9mIFdpbGN6ee+/ve+/vXNraQ==?= X-Patchwork-Id: 12208821 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 47CBEC433ED for ; Fri, 16 Apr 2021 20:59:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0D35A613E0 for ; Fri, 16 Apr 2021 20:59:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343947AbhDPU7o (ORCPT ); Fri, 16 Apr 2021 16:59:44 -0400 Received: from mail-ej1-f51.google.com ([209.85.218.51]:39509 "EHLO mail-ej1-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343954AbhDPU7k (ORCPT ); Fri, 16 Apr 2021 16:59:40 -0400 Received: by mail-ej1-f51.google.com with SMTP id v6so42706287ejo.6 for ; Fri, 16 Apr 2021 13:59:15 -0700 (PDT) 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=imUCWi5f0L0u44r4h5ePrxjMjkXDq587H6qCYb8kQvE=; b=PxclICk0vbACZjOXqP3DScizUQO0k0h26EHTBNUQZSDNYL7oUbQ9qK5GHZXmO9BLf1 bTj4VzYThLqdPctiwz9fAK8EfHajZV+sVUh26K5jOGmg0xJbm4WaWnUfwzPoIB02mFE+ 0BXe3jCvweO62zveJHRNejqOsLZ5vg9xXiVyO1rkVZ5BGSrLHw073VjlhdY0H7HcH/wR lSlyOIkkzOVWTuPDmbfbdB0h9djW9romjs+rQKubMS9YMTqZPpJgwaWLpmc3q/sZGaA9 aYKMi7kK/YclR/3wIXdncl6RpaBN8zLkayrcROV+QhjXrVJmvbs7YFyltAlQvslC5ZZI r1Jw== X-Gm-Message-State: AOAM530B+7SP3TODVxMf7nh642CKjHYAhnCyzZgzcga2v6a1S33ZvxEo M9JILZCCIuHLLPV5u7FQoas= X-Google-Smtp-Source: ABdhPJw5PQ1ZoXzIDd+BYON0gNtmEdml9gLAKllaEqpuZ/gPEM5mL2RMO9aizP/o61O5o4l6bky/Cg== X-Received: by 2002:a17:907:f93:: with SMTP id kb19mr9993175ejc.207.1618606754838; Fri, 16 Apr 2021 13:59:14 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id n11sm5103864ejg.43.2021.04.16.13.59.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Apr 2021 13:59:14 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: =?utf-8?q?Pali_Roh=C3=A1r?= , "Oliver O'Halloran" , Greg Kroah-Hartman , Daniel Vetter , Joe Perches , Dan Williams , Mauro Carvalho Chehab , David Sterba , linux-pci@vger.kernel.org Subject: [PATCH 17/20] PCI: Rearrange attributes from the pci_bridge_attr_group Date: Fri, 16 Apr 2021 20:58:53 +0000 Message-Id: <20210416205856.3234481-18-kw@linux.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210416205856.3234481-1-kw@linux.com> References: <20210416205856.3234481-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org When new sysfs objects were added to the PCI device over time, the code that implemented new attributes has been added in many different places in the pci-sysfs.c file. This makes it hard to read and also hard to find relevant code. Thus, collect all the attributes that are part of the "pci_bridge_attr_group" attribute group together and move to the top of the file sorting everything attribute in the order of use. No functional change intended. Suggested-by: Bjorn Helgaas Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-sysfs.c | 108 ++++++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index bf909e9a9528..1899c24081f7 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -886,6 +886,60 @@ static const struct attribute_group pci_dev_hp_attr_group = { .is_visible = pci_dev_hp_attr_is_visible, }; +static ssize_t subordinate_bus_number_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct pci_dev *pdev = to_pci_dev(dev); + u8 sub_bus; + int err; + + err = pci_read_config_byte(pdev, PCI_SUBORDINATE_BUS, &sub_bus); + if (err) + return -EINVAL; + + return sysfs_emit(buf, "%u\n", sub_bus); +} +static DEVICE_ATTR_RO(subordinate_bus_number); + +static ssize_t secondary_bus_number_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct pci_dev *pdev = to_pci_dev(dev); + u8 sec_bus; + int err; + + err = pci_read_config_byte(pdev, PCI_SECONDARY_BUS, &sec_bus); + if (err) + return -EINVAL; + + return sysfs_emit(buf, "%u\n", sec_bus); +} +static DEVICE_ATTR_RO(secondary_bus_number); + +static struct attribute *pci_bridge_attrs[] = { + &dev_attr_subordinate_bus_number.attr, + &dev_attr_secondary_bus_number.attr, + NULL, +}; + +static umode_t pci_bridge_attr_is_visible(struct kobject *kobj, + struct attribute *a, int n) +{ + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); + + if (!pci_is_bridge(pdev)) + return 0; + + return a->mode; +} + +static const struct attribute_group pci_bridge_attr_group = { + .attrs = pci_bridge_attrs, + .is_visible = pci_bridge_attr_is_visible, +}; + /* * PCI Bus Class Devices */ @@ -952,38 +1006,6 @@ static ssize_t current_link_width_show(struct device *dev, } static DEVICE_ATTR_RO(current_link_width); -static ssize_t secondary_bus_number_show(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct pci_dev *pdev = to_pci_dev(dev); - u8 sec_bus; - int err; - - err = pci_read_config_byte(pdev, PCI_SECONDARY_BUS, &sec_bus); - if (err) - return -EINVAL; - - return sysfs_emit(buf, "%u\n", sec_bus); -} -static DEVICE_ATTR_RO(secondary_bus_number); - -static ssize_t subordinate_bus_number_show(struct device *dev, - struct device_attribute *attr, - char *buf) -{ - struct pci_dev *pdev = to_pci_dev(dev); - u8 sub_bus; - int err; - - err = pci_read_config_byte(pdev, PCI_SUBORDINATE_BUS, &sub_bus); - if (err) - return -EINVAL; - - return sysfs_emit(buf, "%u\n", sub_bus); -} -static DEVICE_ATTR_RO(subordinate_bus_number); - static ssize_t rescan_store(struct bus_type *bus, const char *buf, size_t count) { bool rescan; @@ -1041,12 +1063,6 @@ static ssize_t bus_rescan_store(struct device *dev, static struct device_attribute dev_attr_bus_rescan = __ATTR(rescan, 0200, NULL, bus_rescan_store); -static struct attribute *pci_bridge_attrs[] = { - &dev_attr_subordinate_bus_number.attr, - &dev_attr_secondary_bus_number.attr, - NULL, -}; - static struct attribute *pcie_dev_attrs[] = { &dev_attr_current_link_speed.attr, &dev_attr_current_link_width.attr, @@ -1539,17 +1555,6 @@ static int __init pci_sysfs_init(void) } late_initcall(pci_sysfs_init); -static umode_t pci_bridge_attr_is_visible(struct kobject *kobj, - struct attribute *a, int n) -{ - struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); - - if (!pci_is_bridge(pdev)) - return 0; - - return a->mode; -} - static umode_t pcie_dev_attr_is_visible(struct kobject *kobj, struct attribute *a, int n) { @@ -1576,11 +1581,6 @@ const struct attribute_group *pci_dev_groups[] = { NULL, }; -static const struct attribute_group pci_bridge_attr_group = { - .attrs = pci_bridge_attrs, - .is_visible = pci_bridge_attr_is_visible, -}; - static const struct attribute_group pcie_dev_attr_group = { .attrs = pcie_dev_attrs, .is_visible = pcie_dev_attr_is_visible, From patchwork Fri Apr 16 20:58:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?S3J6eXN6dG9mIFdpbGN6ee+/ve+/vXNraQ==?= X-Patchwork-Id: 12208823 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AB77FC43462 for ; Fri, 16 Apr 2021 20:59:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7B9AD613E0 for ; Fri, 16 Apr 2021 20:59:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244280AbhDPU7o (ORCPT ); Fri, 16 Apr 2021 16:59:44 -0400 Received: from mail-ej1-f49.google.com ([209.85.218.49]:37626 "EHLO mail-ej1-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343968AbhDPU7l (ORCPT ); Fri, 16 Apr 2021 16:59:41 -0400 Received: by mail-ej1-f49.google.com with SMTP id w3so44033715ejc.4 for ; Fri, 16 Apr 2021 13:59:16 -0700 (PDT) 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=QuzMAqSHj63WrHC/FbcYb9iuqVgBJBCPeYJtkJ/Y10w=; b=SudTsEqxburb2Qju64+ohazDlqW7BsTaT2gVA9hAw/VFOJyoTHm2cE3FbsyOW1Zi/6 U5WKj97k1k0sAByZi+KlYDmMZlORp6rysBp3dF4G6XuaBYGUpw8Fl5dramR4GmQnvc4R NLm69YBK0Eh7Z7Vh/KJDVFJjN9LVuWxQ9VRNPiZCLaa0gS7O64yXqPl4RGEbo7JwSC3n YJfIPFwOpgnm2kzVEVQBr+Nb3xOkdXWT7VQxJWqP75it1FAyS8qaYDVvCqdziM2Bfwmh vuQkJaZVYLr5C4e9RjefgNcg/n1RWhYSC8rlCrZ1cyOk95vSV1KVuxnPcxaUuUFizX0j /F/g== X-Gm-Message-State: AOAM532K4U79PG26zqPFCdOkwXEYSyuROt8tlctQq60453mibihQ//Es TPT7c034kJ0ni6+6/fvwqew= X-Google-Smtp-Source: ABdhPJxshZ5evBhgHGTC7ixtmO+e6480PhIDTVhQdCLgaF2xBLD/C6VXm8PjgMBk0OLvr0tbhxNtsw== X-Received: by 2002:a17:906:9385:: with SMTP id l5mr10276852ejx.32.1618606755885; Fri, 16 Apr 2021 13:59:15 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id n11sm5103864ejg.43.2021.04.16.13.59.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Apr 2021 13:59:15 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: =?utf-8?q?Pali_Roh=C3=A1r?= , "Oliver O'Halloran" , Greg Kroah-Hartman , Daniel Vetter , Joe Perches , Dan Williams , Mauro Carvalho Chehab , David Sterba , linux-pci@vger.kernel.org Subject: [PATCH 18/20] PCI: Rearrange attributes from the pcie_dev_attr_group Date: Fri, 16 Apr 2021 20:58:54 +0000 Message-Id: <20210416205856.3234481-19-kw@linux.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210416205856.3234481-1-kw@linux.com> References: <20210416205856.3234481-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org When new sysfs objects were added to the PCI device over time, the code that implemented new attributes has been added in many different places in the pci-sysfs.c file. This makes it hard to read and also hard to find relevant code. Thus, collect all the attributes that are part of the "pcie_dev_attr_group" attribute group together and move to the top of the file sorting everything attribute in the order of use. No functional change intended. Suggested-by: Bjorn Helgaas Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-sysfs.c | 106 ++++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index 1899c24081f7..44ce65bcacba 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -940,28 +940,29 @@ static const struct attribute_group pci_bridge_attr_group = { .is_visible = pci_bridge_attr_is_visible, }; -/* - * PCI Bus Class Devices - */ -static ssize_t cpuaffinity_show(struct device *dev, - struct device_attribute *attr, char *buf) +static ssize_t current_link_speed_show(struct device *dev, + struct device_attribute *attr, char *buf) { - struct pci_bus *bus = to_pci_bus(dev); - const struct cpumask *cpumask = cpumask_of_pcibus(bus); + struct pci_dev *pdev = to_pci_dev(dev); + enum pci_bus_speed speed; - return cpumap_print_to_pagebuf(false, buf, cpumask); + pcie_bandwidth_available(pdev, NULL, &speed, NULL); + + return sysfs_emit(buf, "%s\n", pci_speed_string(speed)); } -static DEVICE_ATTR_RO(cpuaffinity); +static DEVICE_ATTR_RO(current_link_speed); -static ssize_t cpulistaffinity_show(struct device *dev, - struct device_attribute *attr, char *buf) +static ssize_t current_link_width_show(struct device *dev, + struct device_attribute *attr, char *buf) { - struct pci_bus *bus = to_pci_bus(dev); - const struct cpumask *cpumask = cpumask_of_pcibus(bus); + struct pci_dev *pdev = to_pci_dev(dev); + enum pcie_link_width width; - return cpumap_print_to_pagebuf(true, buf, cpumask); + pcie_bandwidth_available(pdev, NULL, NULL, &width); + + return sysfs_emit(buf, "%u\n", width); } -static DEVICE_ATTR_RO(cpulistaffinity); +static DEVICE_ATTR_RO(current_link_width); static ssize_t max_link_speed_show(struct device *dev, struct device_attribute *attr, char *buf) @@ -982,29 +983,52 @@ static ssize_t max_link_width_show(struct device *dev, } static DEVICE_ATTR_RO(max_link_width); -static ssize_t current_link_speed_show(struct device *dev, - struct device_attribute *attr, char *buf) +static struct attribute *pcie_dev_attrs[] = { + &dev_attr_current_link_speed.attr, + &dev_attr_current_link_width.attr, + &dev_attr_max_link_speed.attr, + &dev_attr_max_link_width.attr, + NULL, +}; + +static umode_t pcie_dev_attr_is_visible(struct kobject *kobj, + struct attribute *a, int n) { - struct pci_dev *pdev = to_pci_dev(dev); - enum pci_bus_speed speed; + struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); - pcie_bandwidth_available(pdev, NULL, &speed, NULL); + if (!pci_is_pcie(pdev)) + return 0; - return sysfs_emit(buf, "%s\n", pci_speed_string(speed)); + return a->mode; } -static DEVICE_ATTR_RO(current_link_speed); -static ssize_t current_link_width_show(struct device *dev, - struct device_attribute *attr, char *buf) +static const struct attribute_group pcie_dev_attr_group = { + .attrs = pcie_dev_attrs, + .is_visible = pcie_dev_attr_is_visible, +}; + +/* + * PCI Bus Class Devices + */ +static ssize_t cpuaffinity_show(struct device *dev, + struct device_attribute *attr, char *buf) { - struct pci_dev *pdev = to_pci_dev(dev); - enum pcie_link_width width; + struct pci_bus *bus = to_pci_bus(dev); + const struct cpumask *cpumask = cpumask_of_pcibus(bus); - pcie_bandwidth_available(pdev, NULL, NULL, &width); + return cpumap_print_to_pagebuf(false, buf, cpumask); +} +static DEVICE_ATTR_RO(cpuaffinity); - return sysfs_emit(buf, "%u\n", width); +static ssize_t cpulistaffinity_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct pci_bus *bus = to_pci_bus(dev); + const struct cpumask *cpumask = cpumask_of_pcibus(bus); + + return cpumap_print_to_pagebuf(true, buf, cpumask); } -static DEVICE_ATTR_RO(current_link_width); +static DEVICE_ATTR_RO(cpulistaffinity); static ssize_t rescan_store(struct bus_type *bus, const char *buf, size_t count) { @@ -1063,14 +1087,6 @@ static ssize_t bus_rescan_store(struct device *dev, static struct device_attribute dev_attr_bus_rescan = __ATTR(rescan, 0200, NULL, bus_rescan_store); -static struct attribute *pcie_dev_attrs[] = { - &dev_attr_current_link_speed.attr, - &dev_attr_current_link_width.attr, - &dev_attr_max_link_width.attr, - &dev_attr_max_link_speed.attr, - NULL, -}; - static struct attribute *pcibus_attrs[] = { &dev_attr_bus_rescan.attr, &dev_attr_cpuaffinity.attr, @@ -1555,17 +1571,6 @@ static int __init pci_sysfs_init(void) } late_initcall(pci_sysfs_init); -static umode_t pcie_dev_attr_is_visible(struct kobject *kobj, - struct attribute *a, int n) -{ - struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj)); - - if (!pci_is_pcie(pdev)) - return 0; - - return a->mode; -} - const struct attribute_group *pci_dev_groups[] = { &pci_dev_group, &pci_dev_config_attr_group, @@ -1581,11 +1586,6 @@ const struct attribute_group *pci_dev_groups[] = { NULL, }; -static const struct attribute_group pcie_dev_attr_group = { - .attrs = pcie_dev_attrs, - .is_visible = pcie_dev_attr_is_visible, -}; - static const struct attribute_group *pci_dev_attr_groups[] = { &pci_dev_attr_group, &pci_dev_hp_attr_group, From patchwork Fri Apr 16 20:58:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?S3J6eXN6dG9mIFdpbGN6ee+/ve+/vXNraQ==?= X-Patchwork-Id: 12208825 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D6F00C43461 for ; Fri, 16 Apr 2021 20:59:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 9266F613DE for ; Fri, 16 Apr 2021 20:59:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343968AbhDPU7o (ORCPT ); Fri, 16 Apr 2021 16:59:44 -0400 Received: from mail-ej1-f52.google.com ([209.85.218.52]:45753 "EHLO mail-ej1-f52.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343999AbhDPU7m (ORCPT ); Fri, 16 Apr 2021 16:59:42 -0400 Received: by mail-ej1-f52.google.com with SMTP id sd23so35350818ejb.12 for ; Fri, 16 Apr 2021 13:59:17 -0700 (PDT) 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=9LJehf4vVA9YmkyMHVQoHumdkMO50bcEMXHF3G09jEo=; b=g7QSe0a9UNShTdiEw0IXUGIAHkhM23fsogYiDTlRbfk5e+npo+w2duKW/IXBNX7pTI x3HC4KLXxRYGUeRv21kDkjTABgi27C4HVGtoTe8nXquveHlZj2uica++bYTEQjv2d9MD +qnnbECDuCK7KpeI3nrihsrxhYFeLQ6ZFeg6G9kTQoOEhdBqcEmGKCzTxnduTpgnbFOE L2LOdYrXsWer5yAqPxGKNHcRUqXWTGJ1kqrtiKVdUwnIgdm66JNCZCCQXq3UYyRNaJct 8HfDQtKuqqgIs227nRBB8s4t0mBeOpePFNTWlLwwhu3+3H9AFKDocPrb/1FQUVPobmP5 XTPw== X-Gm-Message-State: AOAM533kQeuju+OrsNV83/qohd9OTVXv4mhBCuzSdtX8ZmTbG7E3Vg8u xn42uM+e0wrUY2LAAWfqZuQ= X-Google-Smtp-Source: ABdhPJwEp02/z4i4YJg2d09g160vVZ3k65Zvvi4TIp7pnWAR4n2d1xCdaybQ2eDBpscGa7ffmmvKoQ== X-Received: by 2002:a17:906:c283:: with SMTP id r3mr10138830ejz.328.1618606756854; Fri, 16 Apr 2021 13:59:16 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id n11sm5103864ejg.43.2021.04.16.13.59.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Apr 2021 13:59:16 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: =?utf-8?q?Pali_Roh=C3=A1r?= , "Oliver O'Halloran" , Greg Kroah-Hartman , Daniel Vetter , Joe Perches , Dan Williams , Mauro Carvalho Chehab , David Sterba , linux-pci@vger.kernel.org Subject: [PATCH 19/20] PCI: Rearrange attributes from the pci_bus_group Date: Fri, 16 Apr 2021 20:58:55 +0000 Message-Id: <20210416205856.3234481-20-kw@linux.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210416205856.3234481-1-kw@linux.com> References: <20210416205856.3234481-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org When new sysfs objects were added to the PCI device over time, the code that implemented new attributes has been added in many different places in the pci-sysfs.c file. This makes it hard to read and also hard to find relevant code. Thus, collect all the attributes that are part of the "pci_bus_group" attribute group together and move to the top of the file sorting everything attribute in the order of use. No functional change intended. Suggested-by: Bjorn Helgaas Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-sysfs.c | 48 ++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index 44ce65bcacba..c29a781efe55 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -1010,26 +1010,6 @@ static const struct attribute_group pcie_dev_attr_group = { /* * PCI Bus Class Devices */ -static ssize_t cpuaffinity_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct pci_bus *bus = to_pci_bus(dev); - const struct cpumask *cpumask = cpumask_of_pcibus(bus); - - return cpumap_print_to_pagebuf(false, buf, cpumask); -} -static DEVICE_ATTR_RO(cpuaffinity); - -static ssize_t cpulistaffinity_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct pci_bus *bus = to_pci_bus(dev); - const struct cpumask *cpumask = cpumask_of_pcibus(bus); - - return cpumap_print_to_pagebuf(true, buf, cpumask); -} -static DEVICE_ATTR_RO(cpulistaffinity); - static ssize_t rescan_store(struct bus_type *bus, const char *buf, size_t count) { bool rescan; @@ -1058,10 +1038,25 @@ static const struct attribute_group pci_bus_group = { .attrs = pci_bus_attrs, }; -const struct attribute_group *pci_bus_groups[] = { - &pci_bus_group, - NULL, -}; +static ssize_t cpuaffinity_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct pci_bus *bus = to_pci_bus(dev); + const struct cpumask *cpumask = cpumask_of_pcibus(bus); + + return cpumap_print_to_pagebuf(false, buf, cpumask); +} +static DEVICE_ATTR_RO(cpuaffinity); + +static ssize_t cpulistaffinity_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct pci_bus *bus = to_pci_bus(dev); + const struct cpumask *cpumask = cpumask_of_pcibus(bus); + + return cpumap_print_to_pagebuf(true, buf, cpumask); +} +static DEVICE_ATTR_RO(cpulistaffinity); static ssize_t bus_rescan_store(struct device *dev, struct device_attribute *attr, const char *buf, @@ -1603,6 +1598,11 @@ static const struct attribute_group *pci_dev_attr_groups[] = { NULL, }; +const struct attribute_group *pci_bus_groups[] = { + &pci_bus_group, + NULL, +}; + const struct device_type pci_dev_type = { .groups = pci_dev_attr_groups, }; From patchwork Fri Apr 16 20:58:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?S3J6eXN6dG9mIFdpbGN6ee+/ve+/vXNraQ==?= X-Patchwork-Id: 12208827 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C9913C433B4 for ; Fri, 16 Apr 2021 20:59:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A607C613DC for ; Fri, 16 Apr 2021 20:59:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343791AbhDPU7p (ORCPT ); Fri, 16 Apr 2021 16:59:45 -0400 Received: from mail-ej1-f45.google.com ([209.85.218.45]:43977 "EHLO mail-ej1-f45.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343943AbhDPU7n (ORCPT ); Fri, 16 Apr 2021 16:59:43 -0400 Received: by mail-ej1-f45.google.com with SMTP id l4so43964659ejc.10 for ; Fri, 16 Apr 2021 13:59:18 -0700 (PDT) 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=t1bw/7s+hujLwzSHRsIAXEJYLbGFg3ol96I/CZkwRFU=; b=pWlvMP6XWozybLLmpATcGbozuosVb0DX9LG48a8spd2r/jNkaQBudCdCyUTgCfHHuB Uena1pqwUDJqL+A5AUg5R1puAXCiC58yRQUD7A27cu1Bl6E8fcN99YPRcuBLEcDzjqNR EdW5TrHxjp+OlxtcMi9EYIMi/lRlG0S7SqisS1lZUTF8PiTwLOKBfyMlHU2YjMXFk79X w+NhDrNjqlp2ERN3JSrVjgaaxnCbUp5hJ5yJ07vAzvbhQdX3UqsD7CP8f8FJ+cFNOrv5 2CPGs25T11mBh9Qcy/wFIYj+nPVsDOgL1mIJ+8p3YR0x+IAi4LdQiij5dnRFU+DtGpUg 5L4g== X-Gm-Message-State: AOAM5321uSWtYe8FDTZveeGR83uXbUvrWnakFnhw5Fb8fNztl7L/NLMe d5faYURnq/5fbsKWqsJ8YDY= X-Google-Smtp-Source: ABdhPJzxVDhmhGXzEtaWzdnO0cmQUalLpylWyAtN6fPPStU4g0jVDbUJOqusSQ9DG389QhD7RgPeZA== X-Received: by 2002:a17:906:4913:: with SMTP id b19mr10220540ejq.439.1618606757810; Fri, 16 Apr 2021 13:59:17 -0700 (PDT) Received: from workstation.lan ([95.155.85.46]) by smtp.gmail.com with ESMTPSA id n11sm5103864ejg.43.2021.04.16.13.59.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Apr 2021 13:59:17 -0700 (PDT) From: =?utf-8?q?Krzysztof_Wilczy=C5=84ski?= To: Bjorn Helgaas Cc: =?utf-8?q?Pali_Roh=C3=A1r?= , "Oliver O'Halloran" , Greg Kroah-Hartman , Daniel Vetter , Joe Perches , Dan Williams , Mauro Carvalho Chehab , David Sterba , linux-pci@vger.kernel.org Subject: [PATCH 20/20] PCI: Rearrange attributes from the pcibus_group Date: Fri, 16 Apr 2021 20:58:56 +0000 Message-Id: <20210416205856.3234481-21-kw@linux.com> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20210416205856.3234481-1-kw@linux.com> References: <20210416205856.3234481-1-kw@linux.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org When new sysfs objects were added to the PCI device over time, the code that implemented new attributes has been added in many different places in the pci-sysfs.c file. This makes it hard to read and also hard to find relevant code. Thus, collect all the attributes that are part of the "pcibus_group" attribute group together and move to the top of the file sorting everything attribute in the order of use. No functional change intended. Suggested-by: Bjorn Helgaas Signed-off-by: Krzysztof Wilczyński --- drivers/pci/pci-sysfs.c | 50 ++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index c29a781efe55..e2d3d214178a 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -1038,26 +1038,6 @@ static const struct attribute_group pci_bus_group = { .attrs = pci_bus_attrs, }; -static ssize_t cpuaffinity_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct pci_bus *bus = to_pci_bus(dev); - const struct cpumask *cpumask = cpumask_of_pcibus(bus); - - return cpumap_print_to_pagebuf(false, buf, cpumask); -} -static DEVICE_ATTR_RO(cpuaffinity); - -static ssize_t cpulistaffinity_show(struct device *dev, - struct device_attribute *attr, char *buf) -{ - struct pci_bus *bus = to_pci_bus(dev); - const struct cpumask *cpumask = cpumask_of_pcibus(bus); - - return cpumap_print_to_pagebuf(true, buf, cpumask); -} -static DEVICE_ATTR_RO(cpulistaffinity); - static ssize_t bus_rescan_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) @@ -1082,6 +1062,26 @@ static ssize_t bus_rescan_store(struct device *dev, static struct device_attribute dev_attr_bus_rescan = __ATTR(rescan, 0200, NULL, bus_rescan_store); +static ssize_t cpuaffinity_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct pci_bus *bus = to_pci_bus(dev); + const struct cpumask *cpumask = cpumask_of_pcibus(bus); + + return cpumap_print_to_pagebuf(false, buf, cpumask); +} +static DEVICE_ATTR_RO(cpuaffinity); + +static ssize_t cpulistaffinity_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct pci_bus *bus = to_pci_bus(dev); + const struct cpumask *cpumask = cpumask_of_pcibus(bus); + + return cpumap_print_to_pagebuf(true, buf, cpumask); +} +static DEVICE_ATTR_RO(cpulistaffinity); + static struct attribute *pcibus_attrs[] = { &dev_attr_bus_rescan.attr, &dev_attr_cpuaffinity.attr, @@ -1093,11 +1093,6 @@ static const struct attribute_group pcibus_group = { .attrs = pcibus_attrs, }; -const struct attribute_group *pcibus_groups[] = { - &pcibus_group, - NULL, -}; - #ifdef HAVE_PCI_LEGACY /** * pci_read_legacy_io - read byte(s) from legacy I/O port space @@ -1603,6 +1598,11 @@ const struct attribute_group *pci_bus_groups[] = { NULL, }; +const struct attribute_group *pcibus_groups[] = { + &pcibus_group, + NULL, +}; + const struct device_type pci_dev_type = { .groups = pci_dev_attr_groups, };