From patchwork Tue Nov 24 13:38:16 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lan Tianyu X-Patchwork-Id: 548087 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 30B34140319 for ; Wed, 25 Nov 2015 00:54:17 +1100 (AEDT) Received: from localhost ([::1]:38635 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a1E2x-0005Op-2k for incoming@patchwork.ozlabs.org; Tue, 24 Nov 2015 08:54:15 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34979) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a1E2F-0004rG-Uo for qemu-devel@nongnu.org; Tue, 24 Nov 2015 08:53:33 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1a1E2C-00024P-OE for qemu-devel@nongnu.org; Tue, 24 Nov 2015 08:53:31 -0500 Received: from mga02.intel.com ([134.134.136.20]:61493) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a1E2C-000247-D7 for qemu-devel@nongnu.org; Tue, 24 Nov 2015 08:53:28 -0500 Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga101.jf.intel.com with ESMTP; 24 Nov 2015 05:53:28 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.20,338,1444719600"; d="scan'208";a="606321728" Received: from lantianyu-ws.sh.intel.com (HELO localhost) ([10.239.159.159]) by FMSMGA003.fm.intel.com with ESMTP; 24 Nov 2015 05:53:23 -0800 From: Lan Tianyu To: a.motakis@virtualopensystems.com, alex.williamson@redhat.com, b.reynal@virtualopensystems.com, bhelgaas@google.com, carolyn.wyborny@intel.com, donald.c.skidmore@intel.com, eddie.dong@intel.com, nrupal.jani@intel.com, agraf@suse.de, kvm@vger.kernel.org, pbonzini@redhat.com, qemu-devel@nongnu.org, emil.s.tantilov@intel.com, gerlitz.or@gmail.com, mark.d.rustad@intel.com, mst@redhat.com, eric.auger@linaro.org, intel-wired-lan@lists.osuosl.org, jeffrey.t.kirsher@intel.com, jesse.brandeburg@intel.com, john.ronciak@intel.com, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, matthew.vick@intel.com, mitch.a.williams@intel.com, netdev@vger.kernel.org, shannon.nelson@intel.com, tianyu.lan@intel.com, weiyang@linux.vnet.ibm.com, zajec5@gmail.com Date: Tue, 24 Nov 2015 21:38:16 +0800 Message-Id: <1448372298-28386-2-git-send-email-tianyu.lan@intel.com> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: <1448372298-28386-1-git-send-email-tianyu.lan@intel.com> References: <1448372298-28386-1-git-send-email-tianyu.lan@intel.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 134.134.136.20 Subject: [Qemu-devel] [RFC PATCH V2 1/3] VFIO: Add new ioctl cmd VFIO_GET_PCI_CAP_INFO X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This patch is to add new ioctl cmd VFIO_GET_PCI_CAP_INFO to get PCI cap table size and get free PCI config space regs according pos and size. Qemu will add faked PCI capability for migration and need such info. Signed-off-by: Lan Tianyu --- drivers/vfio/pci/vfio_pci.c | 21 ++++++++++++++++++++ drivers/vfio/pci/vfio_pci_config.c | 38 +++++++++++++++++++++++++++++++------ drivers/vfio/pci/vfio_pci_private.h | 5 +++++ include/uapi/linux/vfio.h | 12 ++++++++++++ 4 files changed, 70 insertions(+), 6 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c index 69fab0f..2e42de0 100644 --- a/drivers/vfio/pci/vfio_pci.c +++ b/drivers/vfio/pci/vfio_pci.c @@ -784,6 +784,27 @@ hot_reset_release: kfree(groups); return ret; + } else if (cmd == VFIO_GET_PCI_CAP_INFO) { + struct vfio_pci_cap_info info; + int offset; + + if (copy_from_user(&info, (void __user *)arg, sizeof(info))) + return -EFAULT; + + switch (info.index) { + case VFIO_PCI_CAP_GET_SIZE: + info.size = vfio_get_cap_size(vdev, info.cap, info.offset); + break; + case VFIO_PCI_CAP_GET_FREE_REGION: + offset = vfio_find_free_pci_config_reg(vdev, + info.offset, info.size); + info.offset = offset; + break; + default: + return -EINVAL; + } + + return copy_to_user((void __user *)arg, &info, sizeof(info)); } return -ENOTTY; diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c index ff75ca3..8afbda4 100644 --- a/drivers/vfio/pci/vfio_pci_config.c +++ b/drivers/vfio/pci/vfio_pci_config.c @@ -841,6 +841,21 @@ static int vfio_find_cap_start(struct vfio_pci_device *vdev, int pos) return pos; } +int vfio_find_free_pci_config_reg(struct vfio_pci_device *vdev, + int pos, int size) +{ + int i, offset = pos; + + for (i = pos; i < PCI_CFG_SPACE_SIZE; i++) { + if (vdev->pci_config_map[i] != PCI_CAP_ID_INVALID) + offset = i + 1; + else if (i - offset + 1 == size) + return offset; + } + + return 0; +} + static int vfio_msi_config_read(struct vfio_pci_device *vdev, int pos, int count, struct perm_bits *perm, int offset, __le32 *val) @@ -1199,6 +1214,20 @@ static int vfio_fill_vconfig_bytes(struct vfio_pci_device *vdev, return ret; } +int vfio_get_cap_size(struct vfio_pci_device *vdev, u8 cap, int pos) +{ + int len; + + len = pci_cap_length[cap]; + if (len == 0xFF) { /* Variable length */ + len = vfio_cap_len(vdev, cap, pos); + if (len < 0) + return len; + } + + return len; +} + static int vfio_cap_init(struct vfio_pci_device *vdev) { struct pci_dev *pdev = vdev->pdev; @@ -1238,12 +1267,9 @@ static int vfio_cap_init(struct vfio_pci_device *vdev) return ret; if (cap <= PCI_CAP_ID_MAX) { - len = pci_cap_length[cap]; - if (len == 0xFF) { /* Variable length */ - len = vfio_cap_len(vdev, cap, pos); - if (len < 0) - return len; - } + len = vfio_get_cap_size(vdev, cap, pos); + if (len < 0) + return len; } if (!len) { diff --git a/drivers/vfio/pci/vfio_pci_private.h b/drivers/vfio/pci/vfio_pci_private.h index ae0e1b4..91b4f9b 100644 --- a/drivers/vfio/pci/vfio_pci_private.h +++ b/drivers/vfio/pci/vfio_pci_private.h @@ -89,4 +89,9 @@ extern void vfio_pci_uninit_perm_bits(void); extern int vfio_config_init(struct vfio_pci_device *vdev); extern void vfio_config_free(struct vfio_pci_device *vdev); +extern int vfio_find_free_pci_config_reg(struct vfio_pci_device *vdev, + int pos, int size); +extern int vfio_get_cap_size(struct vfio_pci_device *vdev, + u8 cap, int pos); + #endif /* VFIO_PCI_PRIVATE_H */ diff --git a/include/uapi/linux/vfio.h b/include/uapi/linux/vfio.h index b57b750..dfa7023 100644 --- a/include/uapi/linux/vfio.h +++ b/include/uapi/linux/vfio.h @@ -495,6 +495,18 @@ struct vfio_eeh_pe_op { #define VFIO_EEH_PE_OP _IO(VFIO_TYPE, VFIO_BASE + 21) +#define VFIO_GET_PCI_CAP_INFO _IO(VFIO_TYPE, VFIO_BASE + 22) +struct vfio_pci_cap_info { + __u32 argsz; + __u32 flags; +#define VFIO_PCI_CAP_GET_SIZE (1 << 0) +#define VFIO_PCI_CAP_GET_FREE_REGION (1 << 1) + __u32 index; + __u32 offset; + __u32 size; + __u8 cap; +}; + /* ***************************************************************** */ #endif /* _UAPIVFIO_H */