From patchwork Wed Feb 12 02:52:51 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yijing Wang X-Patchwork-Id: 3633881 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Original-To: patchwork-linux-pci@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id B8ACD9F1EE for ; Wed, 12 Feb 2014 02:55:00 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 83F5720203 for ; Wed, 12 Feb 2014 02:54:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 94930201F2 for ; Wed, 12 Feb 2014 02:54:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753197AbaBLCyY (ORCPT ); Tue, 11 Feb 2014 21:54:24 -0500 Received: from szxga02-in.huawei.com ([119.145.14.65]:13399 "EHLO szxga02-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753189AbaBLCyW (ORCPT ); Tue, 11 Feb 2014 21:54:22 -0500 Received: from 172.24.2.119 (EHLO szxeml208-edg.china.huawei.com) ([172.24.2.119]) by szxrg02-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id BPR85018; Wed, 12 Feb 2014 10:54:20 +0800 (CST) Received: from SZXEML457-HUB.china.huawei.com (10.82.67.200) by szxeml208-edg.china.huawei.com (172.24.2.57) with Microsoft SMTP Server (TLS) id 14.3.158.1; Wed, 12 Feb 2014 10:53:20 +0800 Received: from localhost (10.177.27.212) by szxeml457-hub.china.huawei.com (10.82.67.200) with Microsoft SMTP Server id 14.3.158.1; Wed, 12 Feb 2014 10:53:12 +0800 From: Yijing Wang To: Bjorn Helgaas CC: , , Yijing Wang , Hanjun Guo Subject: [PATCH part1 v6 5/7] PCI: Add pci_dummy_ops to isolate pci device temporarily Date: Wed, 12 Feb 2014 10:52:51 +0800 Message-ID: <1392173573-59844-6-git-send-email-wangyijing@huawei.com> X-Mailer: git-send-email 1.7.11.msysgit.1 In-Reply-To: <1392173573-59844-1-git-send-email-wangyijing@huawei.com> References: <1392173573-59844-1-git-send-email-wangyijing@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.27.212] X-CFilter-Loop: Reflected Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org X-Spam-Status: No, score=-7.5 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Pci_dummy_ops does nothing when we use it to read/write pci_device. So we can isolate pci device by replace its bus pci_ops by pci_dummy_ops. This is preparation for the later patch. Signed-off-by: Yijing Wang --- drivers/pci/pci.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/pci.h | 4 +++ 2 files changed, 66 insertions(+), 0 deletions(-) diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index af34057..283e82e 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -2226,6 +2226,68 @@ bool pci_serial_number_changed(struct pci_dev *pdev) } EXPORT_SYMBOL(pci_serial_number_changed); +static int pci_dummy_read(struct pci_bus *bus, unsigned int devfn, + int where, int size, u32 *val) +{ + return PCIBIOS_DEVICE_NOT_FOUND; +} + +static int pci_dummy_write(struct pci_bus *bus, unsigned int devfn, + int reg, int size, u32 val) +{ + return PCIBIOS_DEVICE_NOT_FOUND; +} + +static struct pci_ops pci_dummy_ops = { + .read = pci_dummy_read, + .write = pci_dummy_write, +}; + +static DEFINE_SPINLOCK(pci_freeze_lock); +/** + * pci_bus_freeze_device - freeze pci bus to access pci device + * @bus: the pci bus to freeze + * + * Replace pci bus ops by pci_dummy_ops, protect system from + * accessing pci devices. + */ +void pci_bus_freeze_device(struct pci_bus *bus) +{ + struct pci_ops *ops; + unsigned long flags; + + spin_lock_irqsave(&pci_freeze_lock, flags); + if (bus->is_frozen) + goto out; + bus->is_frozen = 1; + ops = pci_bus_set_ops(bus, &pci_dummy_ops); + bus->save_ops = ops; +out: + spin_unlock_irqrestore(&pci_freeze_lock, flags); +} + +/** + * pci_bus_unfreeze_device - unfreeze pci bus to acess pci devices + * @bus: the pci bus to unfreeze + * + * Restore pci bus original ops, so pci bus can access + * pci devices normally. + */ +void pci_bus_unfreeze_device(struct pci_bus *bus) +{ + unsigned long flags; + + BUG_ON(!bus->save_ops); + spin_lock_irqsave(&pci_freeze_lock, flags); + if (!bus->is_frozen) + goto out; + pci_bus_set_ops(bus, bus->save_ops); + bus->save_ops = NULL; + bus->is_frozen = 0; +out: + spin_unlock_irqrestore(&pci_freeze_lock, flags); +} + /** * pci_configure_ari - enable or disable ARI forwarding * @dev: the PCI device diff --git a/include/linux/pci.h b/include/linux/pci.h index d60c0b6..e3dd4ea 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -442,6 +442,7 @@ struct pci_bus { struct resource busn_res; /* bus numbers routed to this bus */ struct pci_ops *ops; /* configuration access functions */ + struct pci_ops *save_ops; /* save configuration access functions */ struct msi_chip *msi; /* MSI controller */ void *sysdata; /* hook for sys-specific extension */ struct proc_dir_entry *procdir; /* directory entry in /proc/bus/pci */ @@ -460,6 +461,7 @@ struct pci_bus { struct bin_attribute *legacy_io; /* legacy I/O for this bus */ struct bin_attribute *legacy_mem; /* legacy mem */ unsigned int is_added:1; + unsigned int is_frozen:1; }; #define pci_bus_b(n) list_entry(n, struct pci_bus, node) @@ -1026,6 +1028,8 @@ ssize_t pci_read_vpd(struct pci_dev *dev, loff_t pos, size_t count, void *buf); ssize_t pci_write_vpd(struct pci_dev *dev, loff_t pos, size_t count, const void *buf); bool pci_serial_number_changed(struct pci_dev *pdev); +void pci_bus_freeze_device(struct pci_bus *bus); +void pci_bus_unfreeze_device(struct pci_bus *bus); /* Helper functions for low-level code (drivers/pci/setup-[bus,res].c) */ resource_size_t pcibios_retrieve_fw_addr(struct pci_dev *dev, int idx);