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: 7691421 Return-Path: X-Original-To: patchwork-kvm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id E54209F2E9 for ; Tue, 24 Nov 2015 13:54:50 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 00C30207E5 for ; Tue, 24 Nov 2015 13:54:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 049B4207D7 for ; Tue, 24 Nov 2015 13:54:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754225AbbKXNxa (ORCPT ); Tue, 24 Nov 2015 08:53:30 -0500 Received: from mga11.intel.com ([192.55.52.93]:37055 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752920AbbKXNx2 (ORCPT ); Tue, 24 Nov 2015 08:53:28 -0500 Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga102.fm.intel.com with ESMTP; 24 Nov 2015 05:53:27 -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 Subject: [RFC PATCH V2 1/3] VFIO: Add new ioctl cmd VFIO_GET_PCI_CAP_INFO 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> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@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 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 */