From patchwork Wed Aug 1 15:54:09 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiang Liu X-Patchwork-Id: 1265341 X-Patchwork-Delegate: bhelgaas@google.com Return-Path: X-Original-To: patchwork-linux-pci@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id 622FBDF215 for ; Wed, 1 Aug 2012 16:04:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755573Ab2HAPz5 (ORCPT ); Wed, 1 Aug 2012 11:55:57 -0400 Received: from mail-yw0-f46.google.com ([209.85.213.46]:47749 "EHLO mail-yw0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755113Ab2HAPzz (ORCPT ); Wed, 1 Aug 2012 11:55:55 -0400 Received: by yhmm54 with SMTP id m54so7456188yhm.19 for ; Wed, 01 Aug 2012 08:55:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=TsVNkTPIsr6n4ab53KcD4+YJWlnbVU66Bzp0xgVjycU=; b=TvtamWJX/bN10jKrfa5QW30VtWI1bYOzDtO152TnPypSd+R4JUYVe9n7PvyX4oUPSW zMsabpwHSqLxIGEKj4Pifu+FgraKIeAVuUM2k2IXAdOv3I9YtygqrY+yCDdVRehnDxnC HWbezf4wwpj76YZa6cK2ZB7DFm8ZlBKPQEKhEpuVC/9eaP0/NeJpKrjhgGjrkMdb4u84 uL6bxDtpn3NtkANSymWn3pCWVofyJDMMyWZG/NhD6Kn8EnyceOv9WL6r5BYgPztkTmUq BZZzlFpssHjZWsNhVljYu3DdqUvJK3NdZMj/l/btNxUxG41JGuB2VzSD10eCBO5mAnVC p1og== Received: by 10.66.88.40 with SMTP id bd8mr40911552pab.36.1343836554467; Wed, 01 Aug 2012 08:55:54 -0700 (PDT) Received: from localhost.localdomain ([58.250.81.2]) by mx.google.com with ESMTPS id pe8sm2816231pbc.76.2012.08.01.08.55.46 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 01 Aug 2012 08:55:52 -0700 (PDT) From: Jiang Liu To: Bjorn Helgaas , Don Dutile Cc: Jiang Liu , Yinghai Lu , Taku Izumi , "Rafael J . Wysocki" , Kenji Kaneshige , Yijing Wang , linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Jiang Liu Subject: [PATCH v3 04/32] PCI: add PCIe capabilities access functions to hide differences among PCIe specs Date: Wed, 1 Aug 2012 23:54:09 +0800 Message-Id: <1343836477-7287-5-git-send-email-jiang.liu@huawei.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1343836477-7287-1-git-send-email-jiang.liu@huawei.com> References: <1343836477-7287-1-git-send-email-jiang.liu@huawei.com> Sender: linux-pci-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pci@vger.kernel.org From: Jiang Liu Introduce several configuration access functions for PCIe capabilities registers to hide differences among PCIe Base Spec versions. Function pci_pcie_capability_read_word/dword() stores the PCIe Capabilities register value by the passed parameter val. If related PCIe Capabilities register is not implemented on the PCIe device, the passed parameter val will be set 0. Function pci_pcie_capability_write_word/dowrd() writes the value to PCIe Capability register. Function pci_pcie_capability_change_word sets and/or clears bits of a PCIe Capability register. Function pci_pcie_capability_reg_implemeneted() checks whether a Capabilities register is implemented by the PCIe device. Signed-off-by: Jiang Liu Signed-off-by: Yijing Wang --- drivers/pci/access.c | 195 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/pci.h | 10 +++ include/linux/pci_regs.h | 2 + 3 files changed, 207 insertions(+) diff --git a/drivers/pci/access.c b/drivers/pci/access.c index ba91a7e..1f8c9e8 100644 --- a/drivers/pci/access.c +++ b/drivers/pci/access.c @@ -469,3 +469,198 @@ void pci_cfg_access_unlock(struct pci_dev *dev) raw_spin_unlock_irqrestore(&pci_lock, flags); } EXPORT_SYMBOL_GPL(pci_cfg_access_unlock); + +static inline int pci_pcie_cap_version(const struct pci_dev *dev) +{ + return dev->pcie_flags_reg & PCI_EXP_FLAGS_VERS; +} + +static inline bool pci_pcie_cap_has_devctl(const struct pci_dev *dev) +{ + return true; +} + +static inline bool pci_pcie_cap_has_lnkctl(const struct pci_dev *dev) +{ + int type = pci_pcie_type(dev); + + return pci_pcie_cap_version(dev) > 1 || + type == PCI_EXP_TYPE_ROOT_PORT || + type == PCI_EXP_TYPE_ENDPOINT || + type == PCI_EXP_TYPE_LEG_END; +} + +static inline bool pci_pcie_cap_has_sltctl(const struct pci_dev *dev) +{ + int type = pci_pcie_type(dev); + + return pci_pcie_cap_version(dev) > 1 || + type == PCI_EXP_TYPE_ROOT_PORT || + (type == PCI_EXP_TYPE_DOWNSTREAM && + dev->pcie_flags_reg & PCI_EXP_FLAGS_SLOT); +} + +static inline bool pci_pcie_cap_has_rtctl(const struct pci_dev *dev) +{ + int type = pci_pcie_type(dev); + + return pci_pcie_cap_version(dev) > 1 || + type == PCI_EXP_TYPE_ROOT_PORT || + type == PCI_EXP_TYPE_RC_EC; +} + +bool pci_pcie_capability_reg_implemented(struct pci_dev *dev, int pos) +{ + if (!pci_is_pcie(dev)) + return false; + + switch (pos) { + case PCI_EXP_FLAGS_TYPE: + return true; + case PCI_EXP_DEVCAP: + case PCI_EXP_DEVCTL: + case PCI_EXP_DEVSTA: + return pci_pcie_cap_has_devctl(dev); + case PCI_EXP_LNKCAP: + case PCI_EXP_LNKCTL: + case PCI_EXP_LNKSTA: + return pci_pcie_cap_has_lnkctl(dev); + case PCI_EXP_SLTCAP: + case PCI_EXP_SLTCTL: + case PCI_EXP_SLTSTA: + return pci_pcie_cap_has_sltctl(dev); + case PCI_EXP_RTCTL: + case PCI_EXP_RTCAP: + case PCI_EXP_RTSTA: + return pci_pcie_cap_has_rtctl(dev); + case PCI_EXP_DEVCAP2: + case PCI_EXP_DEVCTL2: + case PCI_EXP_LNKCAP2: + case PCI_EXP_LNKCTL2: + case PCI_EXP_LNKSTA2: + return pci_pcie_cap_version(dev) > 1; + default: + return false; + } +} +EXPORT_SYMBOL(pci_pcie_capability_reg_implemented); + +/* + * Quotation from PCIe Base Spec 3.0: + * For Functions that do not implement the Slot Capabilities, + * Slot Status, and Slot Control registers, these spaces must + * be hardwired to 0b, with the exception of the Presence Detect + * State bit in the Slot Status register of Downstream Ports, + * which must be hardwired to 1b. + */ +int pci_pcie_capability_read_word(struct pci_dev *dev, int pos, u16 *val) +{ + int ret = 0; + + *val = 0; + if (pos & 1) + return -EINVAL; + + if (pci_pcie_capability_reg_implemented(dev, pos)) { + ret = pci_read_config_word(dev, pci_pcie_cap(dev) + pos, val); + /* + * Reset *val to 0 if pci_read_config_word() fails, it may + * have been written as 0xFFFF if hardware error happens + * during pci_read_config_word(). + */ + if (ret) + *val = 0; + } else if (pci_is_pcie(dev) && pos == PCI_EXP_SLTSTA && + pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM) { + *val = PCI_EXP_SLTSTA_PDS; + } + + return ret; +} +EXPORT_SYMBOL(pci_pcie_capability_read_word); + +int pci_pcie_capability_read_dword(struct pci_dev *dev, int pos, u32 *val) +{ + int ret = 0; + + *val = 0; + if (pos & 3) + return -EINVAL; + + if (pci_pcie_capability_reg_implemented(dev, pos)) { + ret = pci_read_config_dword(dev, pci_pcie_cap(dev) + pos, val); + /* + * Reset *val to 0 if pci_read_config_dword() fails, it may + * have been written as 0xFFFFFFFF if hardware error happens + * during pci_read_config_dword(). + */ + if (ret) + *val = 0; + } else if (pci_is_pcie(dev) && pos == PCI_EXP_SLTCTL && + pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM) { + *val = PCI_EXP_SLTSTA_PDS; + } + + return ret; +} +EXPORT_SYMBOL(pci_pcie_capability_read_dword); + +int pci_pcie_capability_write_word(struct pci_dev *dev, int pos, u16 val) +{ + if (pos & 1) + return -EINVAL; + else if (!pci_pcie_capability_reg_implemented(dev, pos)) + return 0; + + return pci_write_config_word(dev, pci_pcie_cap(dev) + pos, val); +} +EXPORT_SYMBOL(pci_pcie_capability_write_word); + +int pci_pcie_capability_write_dword(struct pci_dev *dev, int pos, u32 val) +{ + if (pos & 3) + return -EINVAL; + else if (!pci_pcie_capability_reg_implemented(dev, pos)) + return 0; + + return pci_write_config_dword(dev, pci_pcie_cap(dev) + pos, val); +} +EXPORT_SYMBOL(pci_pcie_capability_write_dword); + +int pci_pcie_capability_change_word(struct pci_dev *dev, int pos, + u16 set_bits, u16 clear_bits) +{ + int ret; + u16 val, old; + + ret = pci_pcie_capability_read_word(dev, pos, &val); + if (!ret) { + old = val; + val &= ~clear_bits; + val |= set_bits; + if (val != old) + ret = pci_pcie_capability_write_word(dev, pos, val); + } + + return ret; +} +EXPORT_SYMBOL(pci_pcie_capability_change_word); + +int pci_pcie_capability_change_dword(struct pci_dev *dev, int pos, + u32 set_bits, u32 clear_bits) +{ + int ret; + u32 val, old; + + ret = pci_pcie_capability_read_dword(dev, pos, &val); + if (!ret) { + old = val; + val &= ~clear_bits; + val |= set_bits; + if (val != old) + ret = pci_pcie_capability_write_dword(dev, pos, val); + } + + return ret; +} +EXPORT_SYMBOL(pci_pcie_capability_change_dword); diff --git a/include/linux/pci.h b/include/linux/pci.h index 9807da5..4fa3c99 100644 --- a/include/linux/pci.h +++ b/include/linux/pci.h @@ -816,6 +816,16 @@ static inline int pci_write_config_dword(const struct pci_dev *dev, int where, return pci_bus_write_config_dword(dev->bus, dev->devfn, where, val); } +int pci_pcie_capability_read_word(struct pci_dev *dev, int where, u16 *val); +int pci_pcie_capability_read_dword(struct pci_dev *dev, int where, u32 *val); +int pci_pcie_capability_write_word(struct pci_dev *dev, int where, u16 val); +int pci_pcie_capability_write_dword(struct pci_dev *dev, int where, u32 val); +int pci_pcie_capability_change_word(struct pci_dev *dev, int pos, + u16 set_bits, u16 clear_bits); +int pci_pcie_capability_change_dword(struct pci_dev *dev, int pos, + u32 set_bits, u32 clear_bits); +bool pci_pcie_capability_reg_implemented(struct pci_dev *dev, int where); + /* user-space driven config access */ int pci_user_read_config_byte(struct pci_dev *dev, int where, u8 *val); int pci_user_read_config_word(struct pci_dev *dev, int where, u16 *val); diff --git a/include/linux/pci_regs.h b/include/linux/pci_regs.h index 53274bf..5300fdf 100644 --- a/include/linux/pci_regs.h +++ b/include/linux/pci_regs.h @@ -543,7 +543,9 @@ #define PCI_EXP_OBFF_MSGB_EN 0x4000 /* OBFF enable with Message type B */ #define PCI_EXP_OBFF_WAKE_EN 0x6000 /* OBFF using WAKE# signaling */ #define PCI_CAP_EXP_ENDPOINT_SIZEOF_V2 44 /* v2 endpoints end here */ +#define PCI_EXP_LNKCAP2 44 /* Link Capabilities 2 */ #define PCI_EXP_LNKCTL2 48 /* Link Control 2 */ +#define PCI_EXP_LNKSTA2 50 /* Link Status 2 */ #define PCI_EXP_SLTCTL2 56 /* Slot Control 2 */ /* Extended Capabilities (PCI-X 2.0 and Express) */