From patchwork Sun Jul 26 17:11:39 2009 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Michael S. Tsirkin" X-Patchwork-Id: 37430 Received: from vger.kernel.org (vger.kernel.org [209.132.176.167]) by demeter.kernel.org (8.14.2/8.14.2) with ESMTP id n6QHCgCX015835 for ; Sun, 26 Jul 2009 17:12:43 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753782AbZGZRMi (ORCPT ); Sun, 26 Jul 2009 13:12:38 -0400 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1753839AbZGZRMi (ORCPT ); Sun, 26 Jul 2009 13:12:38 -0400 Received: from mx2.redhat.com ([66.187.237.31]:60014 "EHLO mx2.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753802AbZGZRMh (ORCPT ); Sun, 26 Jul 2009 13:12:37 -0400 Received: from int-mx2.corp.redhat.com (int-mx2.corp.redhat.com [172.16.27.26]) by mx2.redhat.com (8.13.8/8.13.8) with ESMTP id n6QHCbeo030800; Sun, 26 Jul 2009 13:12:37 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx2.corp.redhat.com (8.13.1/8.13.1) with ESMTP id n6QHCa2r012563; Sun, 26 Jul 2009 13:12:36 -0400 Received: from redhat.com (vpn-6-213.tlv.redhat.com [10.35.6.213]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id n6QHCYjg008029; Sun, 26 Jul 2009 13:12:35 -0400 Date: Sun, 26 Jul 2009 20:11:39 +0300 From: "Michael S. Tsirkin" To: jbarnes@virtuousgeek.org, linux-pci@vger.kernel.org, kvm@vger.kernel.org Subject: [PATCH RFC] pci: expose function reset capability in sysfs Message-ID: <20090726171139.GA22202@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.19 (2009-01-05) X-Scanned-By: MIMEDefang 2.58 on 172.16.27.26 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Some devices allow an individual function to be reset without affecting other functions in the same device: that's what pci_reset_function does. For devices that have this support, expose reset attribite in sysfs. This is useful e.g. for virtualization, where a qemu userspace process wants to reset the device when the guest is started/reset, to emulate machine reboot as closely as possible. Signed-off-by: Michael S. Tsirkin --- Jesse, all, could you please comment on whether the following approach looks sane? Compile-tested only at this point. I'm also not sure whether the CAP_SYS_ADMIN check is necessary: maybe 400 permissions on the sysfs file are sufficient? drivers/pci/pci-sysfs.c | 37 +++++++++++++++++++++++++++++++++++++ drivers/pci/pci.c | 16 ++++++++++++++++ drivers/pci/pci.h | 1 + include/linux/kvm_host.h | 1 + virt/kvm/irq_comm.c | 4 +++- 5 files changed, 58 insertions(+), 1 deletions(-) diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c index 85ebd02..92805e8 100644 --- a/drivers/pci/pci-sysfs.c +++ b/drivers/pci/pci-sysfs.c @@ -916,6 +916,28 @@ int __attribute__ ((weak)) pcibios_add_platform_entries(struct pci_dev *dev) return 0; } +static ssize_t reset_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 = strict_strtoul(buf, 0, &val); + + if (result < 0) + return result; + + /* this can crash the machine when done on the "wrong" device */ + if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + + if (val != 1) + return -EINVAL; + return pci_reset_function(pdev); +} + +static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store); + static int pci_create_capabilities_sysfs(struct pci_dev *dev) { int retval; @@ -943,7 +965,21 @@ static int pci_create_capabilities_sysfs(struct pci_dev *dev) /* Active State Power Management */ pcie_aspm_create_sysfs_dev_files(dev); + if (!pci_probe_reset_function(dev)) { + retval = device_create_file(&dev->dev, &reset_attr); + if (retval) + goto error; + } return 0; + +error: + pcie_aspm_remove_sysfs_dev_files(dev); + if (dev->vpd && dev->vpd->attr) { + sysfs_remove_bin_file(&dev->dev.kobj, dev->vpd->attr); + kfree(dev->vpd->attr); + } + + return retval; } int __must_check pci_create_sysfs_dev_files (struct pci_dev *pdev) @@ -1037,6 +1073,7 @@ static void pci_remove_capabilities_sysfs(struct pci_dev *dev) } pcie_aspm_remove_sysfs_dev_files(dev); + device_remove_file(&dev->dev, &reset_attr); } /** diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index dbd0f94..f6d1c6c 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -2260,6 +2260,22 @@ int __pci_reset_function(struct pci_dev *dev) EXPORT_SYMBOL_GPL(__pci_reset_function); /** + * pci_probe_reset_function - check whether the device can be safely reset + * @dev: PCI device to reset + * + * Some devices allow an individual function to be reset without affecting + * other functions in the same device. The PCI device must be responsive + * to PCI config space in order to use this function. + * + * Returns 0 if the device function can be reset or negative if the + * device doesn't support resetting a single function. + */ +int pci_probe_reset_function(struct pci_dev *dev) +{ + return pci_dev_reset(dev, 1); +} + +/** * pci_reset_function - quiesce and reset a PCI device function * @dev: PCI device to reset * diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index f73bcbe..60a3811 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -16,6 +16,7 @@ extern void pci_cleanup_rom(struct pci_dev *dev); extern int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma); #endif +int pci_probe_reset_function(struct pci_dev *dev); /** * struct pci_platform_pm_ops - Firmware PM callbacks