From patchwork Thu Sep 21 12:40:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yishai Hadas X-Patchwork-Id: 13394047 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CD5B3E71094 for ; Thu, 21 Sep 2023 17:03:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229627AbjIURD7 (ORCPT ); Thu, 21 Sep 2023 13:03:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230029AbjIURDa (ORCPT ); Thu, 21 Sep 2023 13:03:30 -0400 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1nam02on2040.outbound.protection.outlook.com [40.107.96.40]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10A05182 for ; Thu, 21 Sep 2023 10:02:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=N+z4s/cEK/1doRmsNqukFbh88GsQGAKeQkgwfeWf2m0ywIoYL8BE4txkFSmTAwofeRFFL4f2ZiGPdJzdPQHEZUExtGFBBVw8VKe0hHjeMES66p1X22tr/4cAeD1k0iumf0zmYDt11yokR6el2Kee+mf16iICHxoQVWcNXQlgY/pymWFB5T/DlEo8ek8nLBdg3GF6gr+aPUR5M+7KO9Xq76D5Kg3TFzri/COpgRcVugABTlKUyF/bFgtWTH3CT88uTlV7AAo6YWwq/jLyfHbZuYFusv4mD0eQAn+8FWyppmPbj5BIMRD7VGK6/XNM9CDvlI9UID9n0q+CVt5WzdGXlg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=8W3p89QAltRZ1zWi8LhOhIMZebEugb+OQMBXR017KHs=; b=IfX3QX6Mp5WwfIPi8mMpAvdF23c1DfAUBs/cwxRMMDsn0ZSVJDPe+RjFRVCHjY2FBidXIvARhZ64zHDKGYdcZzG1/QVT9Fbx27JwcDQSuaoaN3t0LyDWlSUys1J3z7Dwib8YxHe7+tnmngpjvLsNUsFbGLQrZWEBDhJBjo6yu0E1TO/v4tph1TxOEeNxh5MQQypaTxXDbtpmwUeScJwgg1elOR5drsYBkoLhtPnh3MaBtOVCA5qUeXt1negQ/TS+DaL9M31oT6fGzPSZmxI3GJ3nCJtGR+nsfafUvMfUNexaULQWZbzw1Kgni5/Q8Yx9xKhW1MXf2bDUlhqtza6B0Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=redhat.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=8W3p89QAltRZ1zWi8LhOhIMZebEugb+OQMBXR017KHs=; b=fD+/evmOFbw3UDjcHgAz8FBpo6vO7QII015XfF2QB4xc6tO/g15EeGkEZ1e2/gKFhVw+EyvfkjLnMoIx4mQhX++IhLT+ByEhfms7gxsvXAe+Nw4ziIpMF5R5NaezHqropSvUQtxeOZvcvpxyJYZtLkAtDTUcZEnt6+LjOz9hmZqzcn85+jQLvlHCujvdT/uViaH5ByeS7N7kkvAAVIycUWZhKkPkUHEgnArVDvisX1VtbaUmdI/99QBKI2zxbDOeAAZVO6dctmOP+a3o5ojHRz90A0pV8PGPxxBRu6o1BousuT+CQbjH4u5valq/jVXiX18Dc9ZRe5a+ZjvfIN1Dhg== Received: from DM6PR06CA0065.namprd06.prod.outlook.com (2603:10b6:5:54::42) by SA1PR12MB6871.namprd12.prod.outlook.com (2603:10b6:806:25f::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6813.20; Thu, 21 Sep 2023 12:41:35 +0000 Received: from DS1PEPF00017094.namprd03.prod.outlook.com (2603:10b6:5:54:cafe::2e) by DM6PR06CA0065.outlook.office365.com (2603:10b6:5:54::42) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.31 via Frontend Transport; Thu, 21 Sep 2023 12:41:35 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by DS1PEPF00017094.mail.protection.outlook.com (10.167.17.137) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.20 via Frontend Transport; Thu, 21 Sep 2023 12:41:35 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 21 Sep 2023 05:41:27 -0700 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail203.nvidia.com (10.129.68.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 21 Sep 2023 05:41:27 -0700 Received: from vdi.nvidia.com (10.127.8.10) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.986.41 via Frontend Transport; Thu, 21 Sep 2023 05:41:23 -0700 From: Yishai Hadas To: , , , CC: , , , , , , , , , Subject: [PATCH vfio 01/11] virtio-pci: Use virtio pci device layer vq info instead of generic one Date: Thu, 21 Sep 2023 15:40:30 +0300 Message-ID: <20230921124040.145386-2-yishaih@nvidia.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20230921124040.145386-1-yishaih@nvidia.com> References: <20230921124040.145386-1-yishaih@nvidia.com> MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS1PEPF00017094:EE_|SA1PR12MB6871:EE_ X-MS-Office365-Filtering-Correlation-Id: 07c5cbf7-7fb1-46c7-2357-08dbbaa01759 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: iTdgzAGZ28BlQJupUVeTATPuV7piZgos0i6euYxKAIDCFH88L+9wjgAqb+guE4HIvFyIz6QPNkkXPk7nivOSGkpLz4nzH2TKekuPaRfIlH5W/RjiYOsyYL7GYErzGWWXbLoiFvDQ6emL05feNLTzVJbctmt/FMUtzIUO0Qa0i4Zz2BIKaUlP3Nhj/tzDxnLXDImr9HxmZ9DtGlDBDvpg9PUNI78/6U10ocFYqSiD3qWPTuIX/gWq5doGrr2K8a7ToQH6SCxybsKt3+t2TvDREv9tyFHSgaSgWkXj8fDruBFW5trK9CPzLSfJPVfzG+kWjsTJ51UPIU1S2CfO6VZqQdPfVVhajq4QazhuqQvXI0xldZP3PAPtmc62sJI/O31YskgESqspLRiyNJZaeXRoX4aGSNPcggtdF0l53FJa+6UbHi0yHbLOqoyRbJQvVO2ScRPGtsr7vll28f5Pox+csXHK727kFYIY4tAfP37lQPs4AzFU2oeXLXfqHFusL1duycbA0hOFS1u3pW6YlTdXDnHSv0dMuzUdLsCUx4/lTH0dcm3t74SYTlO6h3sK4fH2paw3rvLYfCz5mnbRnoVY55y5UhRcqoSUKqG7Po4qERJQc4bhrLQQ1tv9CSg+ysDDfqL8b9H4IudgREQdqMSwzJJxKtFoejrPu5BvndRAGZLTEDrZTfAu2J61XPfyFIuQyk8jEfYZP6qQaoBOyrXSZEmdDNOMOqk+WCCf/O5MLWgVKKq+kaigeXGSYP+030quuvKLOSaQmyipk4CuWjo05aEWsPiBFZ8l9zqfLUuw0E0= X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230031)(4636009)(396003)(376002)(346002)(136003)(39860400002)(451199024)(1800799009)(186009)(82310400011)(36840700001)(40470700004)(46966006)(2906002)(47076005)(83380400001)(7636003)(40480700001)(82740400003)(6666004)(356005)(5660300002)(426003)(336012)(8676002)(8936002)(4326008)(86362001)(36860700001)(1076003)(26005)(6636002)(316002)(40460700003)(70206006)(110136005)(478600001)(7696005)(36756003)(2616005)(41300700001)(107886003)(70586007)(54906003)(41533002)(2101003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2023 12:41:35.0902 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 07c5cbf7-7fb1-46c7-2357-08dbbaa01759 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DS1PEPF00017094.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB6871 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Feng Liu Currently VQ deletion callback vp_del_vqs() processes generic virtio_device level VQ list instead of VQ information available at PCI layer. To adhere to the layering, use the pci device level VQ information stored in the virtqueues or vqs. This also prepares the code to handle PCI layer admin vq life cycle to be managed within the pci layer and thereby avoid undesired deletion of admin vq by upper layer drivers (net, console, vfio), in the del_vqs() callback. Signed-off-by: Feng Liu Reviewed-by: Parav Pandit Reviewed-by: Jiri Pirko Signed-off-by: Yishai Hadas --- drivers/virtio/virtio_pci_common.c | 12 +++++++++--- drivers/virtio/virtio_pci_common.h | 1 + 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/drivers/virtio/virtio_pci_common.c b/drivers/virtio/virtio_pci_common.c index c2524a7207cf..7a3e6edc4dd6 100644 --- a/drivers/virtio/virtio_pci_common.c +++ b/drivers/virtio/virtio_pci_common.c @@ -232,12 +232,16 @@ static void vp_del_vq(struct virtqueue *vq) void vp_del_vqs(struct virtio_device *vdev) { struct virtio_pci_device *vp_dev = to_vp_device(vdev); - struct virtqueue *vq, *n; + struct virtqueue *vq; int i; - list_for_each_entry_safe(vq, n, &vdev->vqs, list) { + for (i = 0; i < vp_dev->nvqs; i++) { + if (!vp_dev->vqs[i]) + continue; + + vq = vp_dev->vqs[i]->vq; if (vp_dev->per_vq_vectors) { - int v = vp_dev->vqs[vq->index]->msix_vector; + int v = vp_dev->vqs[i]->msix_vector; if (v != VIRTIO_MSI_NO_VECTOR) { int irq = pci_irq_vector(vp_dev->pci_dev, v); @@ -294,6 +298,7 @@ static int vp_find_vqs_msix(struct virtio_device *vdev, unsigned int nvqs, vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL); if (!vp_dev->vqs) return -ENOMEM; + vp_dev->nvqs = nvqs; if (per_vq_vectors) { /* Best option: one for change interrupt, one per vq. */ @@ -365,6 +370,7 @@ static int vp_find_vqs_intx(struct virtio_device *vdev, unsigned int nvqs, vp_dev->vqs = kcalloc(nvqs, sizeof(*vp_dev->vqs), GFP_KERNEL); if (!vp_dev->vqs) return -ENOMEM; + vp_dev->nvqs = nvqs; err = request_irq(vp_dev->pci_dev->irq, vp_interrupt, IRQF_SHARED, dev_name(&vdev->dev), vp_dev); diff --git a/drivers/virtio/virtio_pci_common.h b/drivers/virtio/virtio_pci_common.h index 4b773bd7c58c..602021967aaa 100644 --- a/drivers/virtio/virtio_pci_common.h +++ b/drivers/virtio/virtio_pci_common.h @@ -60,6 +60,7 @@ struct virtio_pci_device { /* array of all queues for house-keeping */ struct virtio_pci_vq_info **vqs; + u32 nvqs; /* MSI-X support */ int msix_enabled; From patchwork Thu Sep 21 12:40:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yishai Hadas X-Patchwork-Id: 13394211 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AA40FE7D0A2 for ; Thu, 21 Sep 2023 17:51:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230128AbjIURvT (ORCPT ); Thu, 21 Sep 2023 13:51:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49146 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229846AbjIURvG (ORCPT ); Thu, 21 Sep 2023 13:51:06 -0400 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2041.outbound.protection.outlook.com [40.107.236.41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3871B3C12 for ; Thu, 21 Sep 2023 10:09:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=leUTKgPmd/NGH2faN8OGhj6BSrZsKZa1zMBYG/Z9cvCml25lUIZtyDd8ViAT0tbOxqdNGP4UQbLIXbLfTaFs42nLm2UXdpvWSdzIRTPumk/fVx1Sm2AWWKYQAMWb5JY/mENgwCCBSlnlma6kBq6/TcsBXyxDfS4qMThcKQV3H6Dh+/eVz6F9l8dAThBoqaMTWmVhIdFvKcPpDPFAu1RXMOJKX/7s+sK4EMAZf/m6qRK9T3SRCb0g68whXSLEE6B0Id0PDuymL7LqSmR3E2T1ZM9enxH7Yr98ZeQpO/IcKNYz+5GDGvIjIJZ0k6byrED/7n6MZdczxk+1mdQc+2mIxQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=ZK8oVP00vpc6UQUa3hMRt2UjQkZntth7M63FfSSA9Po=; b=gOU7tT+BIblKzGey+GuFQn/TqBfgNtQ8IpoG+jQYX+a2tSwDCSi7XsoPne0qS+ej0JDQwLazQP8r5gF5ZrKL07atNr6311eLo30JNqXHlXyIAaUrHdSy2zNVbgCzWY50reOFdPySMHuKfBlyN00yx55cS8IVO9XkrIzlBocoO7VOXSk8vLe4O59rVvnO8o3uTxzAI5xJXn/Rctlxy0sE+v9+KLrtHfllS1dxuCjsXbACDsNatc7znjOyxK75wKLeGtYf1GUfqVguuQmzFxD6yjImAg5h0X/7uWAr9Vrn7nGvXa0+Eoc8BMXtTaYbb8tNwVmINtdt3hSnn5SsShDwOA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=redhat.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ZK8oVP00vpc6UQUa3hMRt2UjQkZntth7M63FfSSA9Po=; b=rkO5UX950UB17sJeJ7AJ08FXQ/MIc9+eLT1nmqFz0rLZqbxJuH1shOb9N8q+oxBFebHgnMHBDGU6n5i00u/BKQiigM/Uz6k7Hg4aCs06V09D06FimiTjiAP02iVFRhuAK6f+HlmjKOZAZ7EoSCW8BqsluI12Ryf92SMd9AmjaubRnvhjuqItw6wSDRh2sRGr0qzW0IOU/koUvh6LQGlNLd4ov9Y64vh4QnbOX4n0wjEfh1hmD1T3VRfNPlVEe3UY0NKu05S4k8awG2CRdRDLtKKdfr2OjOzlbvalcUS7eey2OMKhzPwiZwUqhhzR/JNYlhlzXORvmUt0H9+jjFRfzw== Received: from DM6PR07CA0094.namprd07.prod.outlook.com (2603:10b6:5:337::27) by IA0PR12MB8205.namprd12.prod.outlook.com (2603:10b6:208:400::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.24; Thu, 21 Sep 2023 12:41:44 +0000 Received: from DS3PEPF000099D3.namprd04.prod.outlook.com (2603:10b6:5:337:cafe::11) by DM6PR07CA0094.outlook.office365.com (2603:10b6:5:337::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.31 via Frontend Transport; Thu, 21 Sep 2023 12:41:44 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by DS3PEPF000099D3.mail.protection.outlook.com (10.167.17.4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.20 via Frontend Transport; Thu, 21 Sep 2023 12:41:43 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 21 Sep 2023 05:41:31 -0700 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail203.nvidia.com (10.129.68.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 21 Sep 2023 05:41:31 -0700 Received: from vdi.nvidia.com (10.127.8.10) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.986.41 via Frontend Transport; Thu, 21 Sep 2023 05:41:27 -0700 From: Yishai Hadas To: , , , CC: , , , , , , , , , Subject: [PATCH vfio 02/11] virtio: Define feature bit for administration virtqueue Date: Thu, 21 Sep 2023 15:40:31 +0300 Message-ID: <20230921124040.145386-3-yishaih@nvidia.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20230921124040.145386-1-yishaih@nvidia.com> References: <20230921124040.145386-1-yishaih@nvidia.com> MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS3PEPF000099D3:EE_|IA0PR12MB8205:EE_ X-MS-Office365-Filtering-Correlation-Id: b9cd67ab-82a6-4857-d536-08dbbaa01cac X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2uQMAaRT65k8HW2Xn6W7ojGMvBkxIJ4C473ybO0I5bA6khl1N8cPQUJCxomhbLHubWZzAVVUggsZqk7V2RAWcSGJHNnz5vGPwhkN8LpVECC5Iin5dFUj2aX53h7IwD+o1AKDS+G1KOkFG+Z/UvcxntnZ79iSrQNLNbUrumxMsvbmZtWsEF5yRxvY1azZ7loymYyyJ1MpjmXIqqhd0E94fquNoKxDY3tmF2HDdXFORF1bcinNMvsC+7QgsXZPzMcfPA1afBXGTitJjzcziJiLMX16TRkXnFnLIIjlvZrAgeQvRe60o8i84QyxBalVTY2BfD7XoiGU/ay/aYWYsX0K3x8GZYls1C4ljOfULojSq3ojpzEZC67YOTKRYalmDZGSLBNXm+xknX+p/WSEmtHGBqYb3X77ZAgT04bxn4ZGCI+1SUsnjov/HCZsAl2/ofbaGsLzgyGh8R4SGV0q//dIf2iBAHQd63QykNuZbcJ0TsbWQSEZzzmSG4PEn+ECufr6dy5v0mPn19wgBSHCCgkWn1Zti0owMJq2SbrUvjbnSdrujRzwvHIKLKmiTsHwwz936FWnNboY8P2lpW2wSSHU7XAQpCeFkvlCq72UcD5+4dbUhsgqZB+hMAcaR177QDdD1K8rVp3vwSpjKVhdO779s9hmxRklXTrtglc5otgkPUgkp02zY7jkkoLf6o2UAfS035thfzP0GGQDZelN0fb8CR7KF4wX1wA0l9CwAjYWBbvLr0GDf+qcdQ710wpcrBFT30afRhmcJJBltnxEFP6/yQ== X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230031)(4636009)(136003)(346002)(396003)(376002)(39860400002)(82310400011)(186009)(1800799009)(451199024)(46966006)(36840700001)(40470700004)(7636003)(356005)(82740400003)(8936002)(8676002)(36860700001)(26005)(2616005)(336012)(40460700003)(1076003)(4326008)(83380400001)(107886003)(2906002)(426003)(36756003)(47076005)(40480700001)(86362001)(7696005)(478600001)(6666004)(5660300002)(6636002)(54906003)(316002)(110136005)(41300700001)(70586007)(70206006)(2101003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2023 12:41:43.9960 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: b9cd67ab-82a6-4857-d536-08dbbaa01cac X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DS3PEPF000099D3.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB8205 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Feng Liu Introduce VIRTIO_F_ADMIN_VQ which is used for administration virtqueue support. Signed-off-by: Feng Liu Reviewed-by: Parav Pandit Reviewed-by: Jiri Pirko Signed-off-by: Yishai Hadas --- include/uapi/linux/virtio_config.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/include/uapi/linux/virtio_config.h b/include/uapi/linux/virtio_config.h index 2c712c654165..09d694968b14 100644 --- a/include/uapi/linux/virtio_config.h +++ b/include/uapi/linux/virtio_config.h @@ -52,7 +52,7 @@ * rest are per-device feature bits. */ #define VIRTIO_TRANSPORT_F_START 28 -#define VIRTIO_TRANSPORT_F_END 41 +#define VIRTIO_TRANSPORT_F_END 42 #ifndef VIRTIO_CONFIG_NO_LEGACY /* Do we get callbacks when the ring is completely used, even if we've @@ -109,4 +109,10 @@ * This feature indicates that the driver can reset a queue individually. */ #define VIRTIO_F_RING_RESET 40 + +/* + * This feature indicates that the device support administration virtqueues. + */ +#define VIRTIO_F_ADMIN_VQ 41 + #endif /* _UAPI_LINUX_VIRTIO_CONFIG_H */ From patchwork Thu Sep 21 12:40:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yishai Hadas X-Patchwork-Id: 13394817 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3FE67E7D0AF for ; Thu, 21 Sep 2023 22:25:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232383AbjIUWZg (ORCPT ); Thu, 21 Sep 2023 18:25:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59524 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233065AbjIUWZ0 (ORCPT ); Thu, 21 Sep 2023 18:25:26 -0400 Received: from NAM02-DM3-obe.outbound.protection.outlook.com (mail-dm3nam02on2089.outbound.protection.outlook.com [40.107.95.89]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8F8C87ED9 for ; Thu, 21 Sep 2023 10:10:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=E+1UZzgEmwdCfBRz13Xtucxt2yUOT7xswz1fkF+KqAg1ZK7vzFl49xKmd1koPgjuA0Ccz5qpMd7jqEMQyQzW/8MHUmbbFIQCKf43ZeNtCYJiB17AqFTEFRMUG97pu4JEa/AzHY1BV40iNsX9EEe2X6pPFNrM+Gbo0cnu6jfee4s2pKPG84yGYxCjJnf5bRjpKUv7KY6z6mv7lRLdbYn6n1s/cSNJtq/eq8Fa8jg1EzpFsQOhqpDdgOs1eQ1/zSJ4lMEEy8+7QuUI3Moz3+CZhUeFEzYoFtgbb6/eu1FNUKycqrZvEWrPyz5GZfwT3KWtfGyloIAHQ5SoYM6McFJlcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=JK2zh36R8kbTFn+NXcxUpq2M2SXdLc9/Z6K659NDZ8s=; b=jctt/o2gs/RjDTkcc/DwbuXkDjAwVSo2BzQWHJs92upUBFyqN92VTxNFxQ+E7Ug0wfFL2Qd1JxiNF8evoaaGhfyNq0kGejUMxVmKbK5Q+e/J0KA7iP2KrXi9vYmFfCgkw08L/zFXDi9ZP9ojUPzqLU0bLrMJKyZOh+XrnciYof9ooZ3WVONVkl/IKA1EPMUhjbFy80reSCQUol5fIDFqNC2bmknS6QuZgH0GYMXX9JEzG73Q3Tojohbpe3i6kRTJGfgRkv3iaim9drYbMDdCcLJgCMeGU64CpDe5Zfqp0qssil0wU0dQUwhmF4McwYfV2bSSDOLutpif6VYGEeGOOQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=redhat.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=JK2zh36R8kbTFn+NXcxUpq2M2SXdLc9/Z6K659NDZ8s=; b=eFDQ75uEfRnLNXG5f3wzGtNHKSTNY/JANb3BnChuru71oK/Rgpr51dF7aXmghYedkP5afO1SFmzEUi6mYHpQdC8rpqJFogNamIIZNJqP7ZUGNaxsiFQT0R8slssGpx9osTAZjOMXBTPIF6owaES9GnUcZeCTGgBXXz6HJ5HIMFA28adekUwKnmNDlJxXGDSlZ5sueB1TGh/XOVB+wnzg90vWiYSUqc1+K+brLMS7ksEr/6EKt8CrTnBGAzbbpI1jV5lXv4efkbFuf1YroPb4AKvU7ZXCpVyUZeuouCQ/3L1SR+zKKCgvBXG79XTqnwvetNUFkdEalC+RTEgmPy0VAw== Received: from CH0PR03CA0295.namprd03.prod.outlook.com (2603:10b6:610:e6::30) by LV8PR12MB9406.namprd12.prod.outlook.com (2603:10b6:408:20b::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.28; Thu, 21 Sep 2023 12:41:47 +0000 Received: from DS3PEPF000099D8.namprd04.prod.outlook.com (2603:10b6:610:e6:cafe::a6) by CH0PR03CA0295.outlook.office365.com (2603:10b6:610:e6::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.30 via Frontend Transport; Thu, 21 Sep 2023 12:41:47 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by DS3PEPF000099D8.mail.protection.outlook.com (10.167.17.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.20 via Frontend Transport; Thu, 21 Sep 2023 12:41:47 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 21 Sep 2023 05:41:35 -0700 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail202.nvidia.com (10.129.68.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 21 Sep 2023 05:41:35 -0700 Received: from vdi.nvidia.com (10.127.8.10) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.986.41 via Frontend Transport; Thu, 21 Sep 2023 05:41:31 -0700 From: Yishai Hadas To: , , , CC: , , , , , , , , , Subject: [PATCH vfio 03/11] virtio-pci: Introduce admin virtqueue Date: Thu, 21 Sep 2023 15:40:32 +0300 Message-ID: <20230921124040.145386-4-yishaih@nvidia.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20230921124040.145386-1-yishaih@nvidia.com> References: <20230921124040.145386-1-yishaih@nvidia.com> MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS3PEPF000099D8:EE_|LV8PR12MB9406:EE_ X-MS-Office365-Filtering-Correlation-Id: 13a745e0-97d3-400b-960e-08dbbaa01e7e X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: VONSuuVb6yccA92UtoSfoc2Sh0v+8QsEKD3ednqGgHyFlLn08RPSX2CYgw1UbSrPPbMHWPDEf72oS2HALsbHbQldxlX/gS5MbNOetd1MUiXrHbrq6Lfyj1p52pMt3GxlKBi6Hr1/t0uQaARhmkADB2gehJoWyuAwXMU65+6ZB5qISAJVjnFzAm9aFtnuRW0pWyoPCrxmqAFtZdVrxBMNegUq9HgNaK/w4sgnZZVWbkraMRcAT/EeepZrlszG3IdpAWHOgxA93m/Wz/czoetQACGDRx8UHuJ6njL1QWjdZ4g1QmVf32vETBwXJtGrQsJTL1oVHNlS0NOR23isHUxkejVTEAw9yc3+bgNcGuKe/GtVVck2l8GKLudFjmUWXj7DR2wfj4y4tE8WBJvLt9eEadC7mhLoJDYUkgABYtPC3xXEZvZVR3Ii/3gBT4GUrfq96CY8UG96eDig/jRdoeMyFjyX+/8vPux5M0FBA350GCN0i48fjB+gxzLaTcXBnyWILjZ5BeogoJsPaZeebO4mmEc7ZK4F7QLpWty9IvRcAywAqfmwsnEL7ykM+LLdTy5QX3OKl703uXf79Y0ru51TUW1kQ6yp2il4fIi++XSZPVYsYaum9aEi6lu2stKxu5q2fP2VasK29FFUK4YxXgf4ss8+QLRnF28zTaXStyulE6Ou2QhP57mvfSWy84lds2ZNSu/CWMmTeQTuMW4e4PqeZTc3odBxlEjvbb9v8B8r+BE2ninwd6SC9ZJ2DZKECWCeWHZZAQenCeg/3nX/OHsoXA== X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230031)(4636009)(136003)(376002)(396003)(39860400002)(346002)(82310400011)(186009)(1800799009)(451199024)(46966006)(36840700001)(40470700004)(70206006)(7696005)(36860700001)(82740400003)(86362001)(70586007)(41300700001)(356005)(110136005)(54906003)(6636002)(478600001)(316002)(7636003)(107886003)(1076003)(8936002)(4326008)(8676002)(36756003)(40460700003)(26005)(47076005)(30864003)(336012)(426003)(5660300002)(40480700001)(2906002)(2616005)(83380400001)(2101003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2023 12:41:47.0484 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 13a745e0-97d3-400b-960e-08dbbaa01e7e X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DS3PEPF000099D8.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV8PR12MB9406 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Feng Liu Introduce support for the admin virtqueue. By negotiating VIRTIO_F_ADMIN_VQ feature, driver detects capability and creates one administration virtqueue. Administration virtqueue implementation in virtio pci generic layer, enables multiple types of upper layer drivers such as vfio, net, blk to utilize it. Signed-off-by: Feng Liu Reviewed-by: Parav Pandit Reviewed-by: Jiri Pirko Signed-off-by: Yishai Hadas --- drivers/virtio/Makefile | 2 +- drivers/virtio/virtio.c | 37 +++++++++++++-- drivers/virtio/virtio_pci_common.h | 15 +++++- drivers/virtio/virtio_pci_modern.c | 10 +++- drivers/virtio/virtio_pci_modern_avq.c | 65 ++++++++++++++++++++++++++ include/linux/virtio_config.h | 4 ++ include/linux/virtio_pci_modern.h | 3 ++ 7 files changed, 129 insertions(+), 7 deletions(-) create mode 100644 drivers/virtio/virtio_pci_modern_avq.c diff --git a/drivers/virtio/Makefile b/drivers/virtio/Makefile index 8e98d24917cc..dcc535b5b4d9 100644 --- a/drivers/virtio/Makefile +++ b/drivers/virtio/Makefile @@ -5,7 +5,7 @@ obj-$(CONFIG_VIRTIO_PCI_LIB) += virtio_pci_modern_dev.o obj-$(CONFIG_VIRTIO_PCI_LIB_LEGACY) += virtio_pci_legacy_dev.o obj-$(CONFIG_VIRTIO_MMIO) += virtio_mmio.o obj-$(CONFIG_VIRTIO_PCI) += virtio_pci.o -virtio_pci-y := virtio_pci_modern.o virtio_pci_common.o +virtio_pci-y := virtio_pci_modern.o virtio_pci_common.o virtio_pci_modern_avq.o virtio_pci-$(CONFIG_VIRTIO_PCI_LEGACY) += virtio_pci_legacy.o obj-$(CONFIG_VIRTIO_BALLOON) += virtio_balloon.o obj-$(CONFIG_VIRTIO_INPUT) += virtio_input.o diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index 3893dc29eb26..f4080692b351 100644 --- a/drivers/virtio/virtio.c +++ b/drivers/virtio/virtio.c @@ -302,9 +302,15 @@ static int virtio_dev_probe(struct device *_d) if (err) goto err; + if (dev->config->create_avq) { + err = dev->config->create_avq(dev); + if (err) + goto err; + } + err = drv->probe(dev); if (err) - goto err; + goto err_probe; /* If probe didn't do it, mark device DRIVER_OK ourselves. */ if (!(dev->config->get_status(dev) & VIRTIO_CONFIG_S_DRIVER_OK)) @@ -316,6 +322,10 @@ static int virtio_dev_probe(struct device *_d) virtio_config_enable(dev); return 0; + +err_probe: + if (dev->config->destroy_avq) + dev->config->destroy_avq(dev); err: virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED); return err; @@ -331,6 +341,9 @@ static void virtio_dev_remove(struct device *_d) drv->remove(dev); + if (dev->config->destroy_avq) + dev->config->destroy_avq(dev); + /* Driver should have reset device. */ WARN_ON_ONCE(dev->config->get_status(dev)); @@ -489,13 +502,20 @@ EXPORT_SYMBOL_GPL(unregister_virtio_device); int virtio_device_freeze(struct virtio_device *dev) { struct virtio_driver *drv = drv_to_virtio(dev->dev.driver); + int ret; virtio_config_disable(dev); dev->failed = dev->config->get_status(dev) & VIRTIO_CONFIG_S_FAILED; - if (drv && drv->freeze) - return drv->freeze(dev); + if (drv && drv->freeze) { + ret = drv->freeze(dev); + if (ret) + return ret; + } + + if (dev->config->destroy_avq) + dev->config->destroy_avq(dev); return 0; } @@ -532,10 +552,16 @@ int virtio_device_restore(struct virtio_device *dev) if (ret) goto err; + if (dev->config->create_avq) { + ret = dev->config->create_avq(dev); + if (ret) + goto err; + } + if (drv->restore) { ret = drv->restore(dev); if (ret) - goto err; + goto err_restore; } /* If restore didn't do it, mark device DRIVER_OK ourselves. */ @@ -546,6 +572,9 @@ int virtio_device_restore(struct virtio_device *dev) return 0; +err_restore: + if (dev->config->destroy_avq) + dev->config->destroy_avq(dev); err: virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED); return ret; diff --git a/drivers/virtio/virtio_pci_common.h b/drivers/virtio/virtio_pci_common.h index 602021967aaa..9bffa95274b6 100644 --- a/drivers/virtio/virtio_pci_common.h +++ b/drivers/virtio/virtio_pci_common.h @@ -41,6 +41,14 @@ struct virtio_pci_vq_info { unsigned int msix_vector; }; +struct virtio_avq { + /* Virtqueue info associated with this admin queue. */ + struct virtio_pci_vq_info info; + /* Name of the admin queue: avq.$index. */ + char name[10]; + u16 vq_index; +}; + /* Our device structure */ struct virtio_pci_device { struct virtio_device vdev; @@ -58,10 +66,13 @@ struct virtio_pci_device { spinlock_t lock; struct list_head virtqueues; - /* array of all queues for house-keeping */ + /* Array of all virtqueues reported in the + * PCI common config num_queues field + */ struct virtio_pci_vq_info **vqs; u32 nvqs; + struct virtio_avq *admin; /* MSI-X support */ int msix_enabled; int intx_enabled; @@ -115,6 +126,8 @@ int vp_find_vqs(struct virtio_device *vdev, unsigned int nvqs, const char * const names[], const bool *ctx, struct irq_affinity *desc); const char *vp_bus_name(struct virtio_device *vdev); +void vp_destroy_avq(struct virtio_device *vdev); +int vp_create_avq(struct virtio_device *vdev); /* Setup the affinity for a virtqueue: * - force the affinity for per vq vector diff --git a/drivers/virtio/virtio_pci_modern.c b/drivers/virtio/virtio_pci_modern.c index d6bb68ba84e5..a72c87687196 100644 --- a/drivers/virtio/virtio_pci_modern.c +++ b/drivers/virtio/virtio_pci_modern.c @@ -37,6 +37,9 @@ static void vp_transport_features(struct virtio_device *vdev, u64 features) if (features & BIT_ULL(VIRTIO_F_RING_RESET)) __virtio_set_bit(vdev, VIRTIO_F_RING_RESET); + + if (features & BIT_ULL(VIRTIO_F_ADMIN_VQ)) + __virtio_set_bit(vdev, VIRTIO_F_ADMIN_VQ); } /* virtio config->finalize_features() implementation */ @@ -317,7 +320,8 @@ static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev, else notify = vp_notify; - if (index >= vp_modern_get_num_queues(mdev)) + if (!((index < vp_modern_get_num_queues(mdev) || + (vp_dev->admin && vp_dev->admin->vq_index == index)))) return ERR_PTR(-EINVAL); /* Check if queue is either not available or already active. */ @@ -509,6 +513,8 @@ static const struct virtio_config_ops virtio_pci_config_nodev_ops = { .get_shm_region = vp_get_shm_region, .disable_vq_and_reset = vp_modern_disable_vq_and_reset, .enable_vq_after_reset = vp_modern_enable_vq_after_reset, + .create_avq = vp_create_avq, + .destroy_avq = vp_destroy_avq, }; static const struct virtio_config_ops virtio_pci_config_ops = { @@ -529,6 +535,8 @@ static const struct virtio_config_ops virtio_pci_config_ops = { .get_shm_region = vp_get_shm_region, .disable_vq_and_reset = vp_modern_disable_vq_and_reset, .enable_vq_after_reset = vp_modern_enable_vq_after_reset, + .create_avq = vp_create_avq, + .destroy_avq = vp_destroy_avq, }; /* the PCI probing function */ diff --git a/drivers/virtio/virtio_pci_modern_avq.c b/drivers/virtio/virtio_pci_modern_avq.c new file mode 100644 index 000000000000..114579ad788f --- /dev/null +++ b/drivers/virtio/virtio_pci_modern_avq.c @@ -0,0 +1,65 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include "virtio_pci_common.h" + +static u16 vp_modern_avq_num(struct virtio_pci_modern_device *mdev) +{ + struct virtio_pci_modern_common_cfg __iomem *cfg; + + cfg = (struct virtio_pci_modern_common_cfg __iomem *)mdev->common; + return vp_ioread16(&cfg->admin_queue_num); +} + +static u16 vp_modern_avq_index(struct virtio_pci_modern_device *mdev) +{ + struct virtio_pci_modern_common_cfg __iomem *cfg; + + cfg = (struct virtio_pci_modern_common_cfg __iomem *)mdev->common; + return vp_ioread16(&cfg->admin_queue_index); +} + +int vp_create_avq(struct virtio_device *vdev) +{ + struct virtio_pci_device *vp_dev = to_vp_device(vdev); + struct virtio_avq *avq; + struct virtqueue *vq; + u16 admin_q_num; + + if (!virtio_has_feature(vdev, VIRTIO_F_ADMIN_VQ)) + return 0; + + admin_q_num = vp_modern_avq_num(&vp_dev->mdev); + if (!admin_q_num) + return -EINVAL; + + vp_dev->admin = kzalloc(sizeof(*vp_dev->admin), GFP_KERNEL); + if (!vp_dev->admin) + return -ENOMEM; + + avq = vp_dev->admin; + avq->vq_index = vp_modern_avq_index(&vp_dev->mdev); + sprintf(avq->name, "avq.%u", avq->vq_index); + vq = vp_dev->setup_vq(vp_dev, &vp_dev->admin->info, avq->vq_index, NULL, + avq->name, NULL, VIRTIO_MSI_NO_VECTOR); + if (IS_ERR(vq)) { + dev_err(&vdev->dev, "failed to setup admin virtqueue"); + kfree(vp_dev->admin); + return PTR_ERR(vq); + } + + vp_dev->admin->info.vq = vq; + vp_modern_set_queue_enable(&vp_dev->mdev, avq->info.vq->index, true); + return 0; +} + +void vp_destroy_avq(struct virtio_device *vdev) +{ + struct virtio_pci_device *vp_dev = to_vp_device(vdev); + + if (!vp_dev->admin) + return; + + vp_dev->del_vq(&vp_dev->admin->info); + kfree(vp_dev->admin); +} diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h index 2b3438de2c4d..028c51ea90ee 100644 --- a/include/linux/virtio_config.h +++ b/include/linux/virtio_config.h @@ -93,6 +93,8 @@ typedef void vq_callback_t(struct virtqueue *); * Returns 0 on success or error status * If disable_vq_and_reset is set, then enable_vq_after_reset must also be * set. + * @create_avq: initialize admin virtqueue resource. + * @destroy_avq: destroy admin virtqueue resource. */ struct virtio_config_ops { void (*get)(struct virtio_device *vdev, unsigned offset, @@ -120,6 +122,8 @@ struct virtio_config_ops { struct virtio_shm_region *region, u8 id); int (*disable_vq_and_reset)(struct virtqueue *vq); int (*enable_vq_after_reset)(struct virtqueue *vq); + int (*create_avq)(struct virtio_device *vdev); + void (*destroy_avq)(struct virtio_device *vdev); }; /* If driver didn't advertise the feature, it will never appear. */ diff --git a/include/linux/virtio_pci_modern.h b/include/linux/virtio_pci_modern.h index 067ac1d789bc..f6cb13d858fd 100644 --- a/include/linux/virtio_pci_modern.h +++ b/include/linux/virtio_pci_modern.h @@ -10,6 +10,9 @@ struct virtio_pci_modern_common_cfg { __le16 queue_notify_data; /* read-write */ __le16 queue_reset; /* read-write */ + + __le16 admin_queue_index; /* read-only */ + __le16 admin_queue_num; /* read-only */ }; struct virtio_pci_modern_device { From patchwork Thu Sep 21 12:40:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yishai Hadas X-Patchwork-Id: 13394043 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E6485C88CB9 for ; Thu, 21 Sep 2023 17:02:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229913AbjIURCR (ORCPT ); Thu, 21 Sep 2023 13:02:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45094 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229799AbjIURCE (ORCPT ); Thu, 21 Sep 2023 13:02:04 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2062a.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e88::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 242201B1 for ; Thu, 21 Sep 2023 10:01:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Lc/S+Ixx7nz3e0e7nefBpI7X3UVxXHN5NV3rEf9zhC3+PfryDrdgZSxhOGFcXU/2G+Lp64RbCNMMsJdevvtbsUrkaZFrDFIUgSdvbMEIZPsaFm079C8yCqeZffAWodSd2xf2TIRePJx3AKFS0LiFZHOon+KCXAYMcoDJwuUoC/x+gVVrn+aLwM/Us236DvywjPiiCs7WpIU79K44QxKGHd6VtDl0wzDQIS89V+BKACncllMIUM7YKy1zFZRwDHhvlI/mp32fiH5WoibSkKZ2W+UgHPkVMj3ErtIJblS6YMAsBZau8Jp8B+PQhap70HWEBJQOrtUlZaRIWVxhd/v/+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=PC/Q/vB60/w2vwe0f3rUOMWf2K9QuBGjKjXbMP01yqA=; b=XfhJ/9h2hiV6uYIyFSmYr539X3xVRvdUyB7KNCo1N9FfLeILJq6+hx+JyIyrylY78Wnjh1VL6BhGzYmAdPgfPCPec7C0HnX79kEKqfp24RRA5pmUi01bBczdMU0KN5WYBeYG4MAS7/RzjhZh54mYo2oP1XaCxoZ9BWfV5dUWQgw6CexQ0EobjwdRtoUdU9Xr0BK7vQZYYIrsG2I5/SPm+IHHYm5i/tm1LXLeiGiQ/w8IQ1XYoVm0mhs9GYViY+ukXFXzyQqsJnb384cZsYgv6i+sZ6xKSPMuf+vQJafuN1hmctRto8aJog2ZqYeZUWu4Jli5hTw3WnpfNzfoYLzKUQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=redhat.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=PC/Q/vB60/w2vwe0f3rUOMWf2K9QuBGjKjXbMP01yqA=; b=ay5bBY/XYoMm5m4v3qVCwYfoFzYlQrHZsXc9k8UZLppM5oD8gI+U2qzi9jK8IWqtVA3u2ssRXfk5o8UyNsa5o/I3qm6wconP/MLTQWbvqF+OWtOHinSBPBUWYsJVT8oltSy73zh15s0OUh+Td/fUmQLZch6kQlr04STWFZ/YTaWJzIdW1UPmpgNyBO/KjNH1V4s8mWFMt53fZqeD/zJV1wFgUv3K7fRgohbA+S39nVHSeSIKzmF57CVds7y3pkTx97GTY+zB1mtKWr7bZ8pGyvllWoZZP88wQ+OND1o2O9ze4E0d9YVY9Xmd02Xth9hD2R+xZ7F5x5vH5GTpEBv0sw== Received: from CH0PR03CA0292.namprd03.prod.outlook.com (2603:10b6:610:e6::27) by DM4PR12MB5037.namprd12.prod.outlook.com (2603:10b6:5:39a::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6813.20; Thu, 21 Sep 2023 12:41:51 +0000 Received: from DS3PEPF000099D8.namprd04.prod.outlook.com (2603:10b6:610:e6:cafe::e) by CH0PR03CA0292.outlook.office365.com (2603:10b6:610:e6::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.31 via Frontend Transport; Thu, 21 Sep 2023 12:41:51 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by DS3PEPF000099D8.mail.protection.outlook.com (10.167.17.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.20 via Frontend Transport; Thu, 21 Sep 2023 12:41:51 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 21 Sep 2023 05:41:39 -0700 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail202.nvidia.com (10.129.68.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 21 Sep 2023 05:41:39 -0700 Received: from vdi.nvidia.com (10.127.8.10) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.986.41 via Frontend Transport; Thu, 21 Sep 2023 05:41:35 -0700 From: Yishai Hadas To: , , , CC: , , , , , , , , , Subject: [PATCH vfio 04/11] virtio: Expose the synchronous command helper function Date: Thu, 21 Sep 2023 15:40:33 +0300 Message-ID: <20230921124040.145386-5-yishaih@nvidia.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20230921124040.145386-1-yishaih@nvidia.com> References: <20230921124040.145386-1-yishaih@nvidia.com> MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS3PEPF000099D8:EE_|DM4PR12MB5037:EE_ X-MS-Office365-Filtering-Correlation-Id: 97d8b5ff-4d91-4838-4f2a-08dbbaa02121 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: szFZp33zsuDI+boqjF+ECppqyNHdca38KKRWFdUIzWIKOyFwHpgE42qhezgep4xhSBRvtr9oCK0BX05Q/ajXQqQHxnfdfO7WcsQhKckfkfwlKR7kYx+dIjeoDrR2upr8X2DZt92H7/ch35LMDblUxg7pUrxSU4aLhXZROHAavXnXmblep2DGg3S2SgkjswNjtOeXKEip5KD7MqJEavDAFn+Sb6sWuWcFShEAQ/+63JCIAZNfivLSpLOfxt9Bmi2tAk1DLzXc8Y5kdW7S3IbYfAPHqD5pEdd8orwCabD0v3oBAmufOF6In6X4nFfR94K+/VWoG/AgFlXZbe2nNdLS4X35mq/TJWYkyHhQxymqzi6NRRAzgyJGbTSBEujHFaRFTr0GSRU5D6vqOLnW8M25krWBkZp+Wfdt9F3yznY4wBI8zeyZlZFy1sJarYZQbMH/XbxNsB9nfkOMY3tcfX9l7sstRp3DbXQbI0gcJ1SV3Owc712byQWtbj7jBjXaesUmsj6nSvYoK6sCpIvOTt/m+btqdyK9SqYrrDAzhf+ERsFqTxi9gBldlHQZeAqS6RTFzdIlOkVz4y868o3RmL9nCWfkfJfG3usfgj/gJVtYPAg+6rw5RXoArv4AOjlxzODkRKBXnKkkB7/+VNt4hbQtjS7e/kE5NqqOHJ4KYndj+5DwVNgVA4zBlgxDBkuUWe5wpVh51d2Wr64pfRbWn0H5fjhJuBwwXTRe2zHMujMdhSqVok0J00+Y0KUl6nqLBWiqTB2SiSzipvSn9ykvGrQJ2A== X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230031)(4636009)(396003)(39860400002)(376002)(136003)(346002)(82310400011)(1800799009)(186009)(451199024)(40470700004)(46966006)(36840700001)(83380400001)(107886003)(316002)(54906003)(40480700001)(6636002)(70206006)(70586007)(7696005)(336012)(2616005)(7636003)(356005)(1076003)(426003)(26005)(36756003)(478600001)(47076005)(40460700003)(110136005)(5660300002)(2906002)(82740400003)(8936002)(4326008)(8676002)(36860700001)(86362001)(41300700001)(2101003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2023 12:41:51.4859 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 97d8b5ff-4d91-4838-4f2a-08dbbaa02121 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DS3PEPF000099D8.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR12MB5037 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Feng Liu Synchronous command helper function is exposed at virtio layer, so that ctrl virtqueue and admin virtqueues can reuse this helper function to send synchronous commands. Signed-off-by: Feng Liu Reviewed-by: Parav Pandit Reviewed-by: Jiri Pirko Signed-off-by: Yishai Hadas --- drivers/net/virtio_net.c | 21 ++++++--------------- drivers/virtio/virtio_ring.c | 27 +++++++++++++++++++++++++++ include/linux/virtio.h | 7 +++++++ 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c index fe7f314d65c9..65c210b0fb9e 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -2451,7 +2451,7 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, struct scatterlist *out) { struct scatterlist *sgs[4], hdr, stat; - unsigned out_num = 0, tmp; + unsigned int out_num = 0; int ret; /* Caller should know better */ @@ -2472,23 +2472,14 @@ static bool virtnet_send_command(struct virtnet_info *vi, u8 class, u8 cmd, sgs[out_num] = &stat; BUG_ON(out_num + 1 > ARRAY_SIZE(sgs)); - ret = virtqueue_add_sgs(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC); - if (ret < 0) { - dev_warn(&vi->vdev->dev, - "Failed to add sgs for command vq: %d\n.", ret); + ret = virtqueue_exec_cmd(vi->cvq, sgs, out_num, 1, vi, GFP_ATOMIC); + if (ret) { + dev_err(&vi->vdev->dev, + "Failed to exec command vq(%s,%d): %d\n", + vi->cvq->name, vi->cvq->index, ret); return false; } - if (unlikely(!virtqueue_kick(vi->cvq))) - return vi->ctrl->status == VIRTIO_NET_OK; - - /* Spin for a response, the kick causes an ioport write, trapping - * into the hypervisor, so the request should be handled immediately. - */ - while (!virtqueue_get_buf(vi->cvq, &tmp) && - !virtqueue_is_broken(vi->cvq)) - cpu_relax(); - return vi->ctrl->status == VIRTIO_NET_OK; } diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 51d8f3299c10..253905c0b008 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -3251,4 +3251,31 @@ void virtqueue_dma_sync_single_range_for_device(struct virtqueue *_vq, } EXPORT_SYMBOL_GPL(virtqueue_dma_sync_single_range_for_device); +int virtqueue_exec_cmd(struct virtqueue *vq, + struct scatterlist **sgs, + unsigned int out_num, + unsigned int in_num, + void *data, + gfp_t gfp) +{ + int ret, len; + + ret = virtqueue_add_sgs(vq, sgs, out_num, in_num, data, gfp); + if (ret < 0) + return ret; + + if (unlikely(!virtqueue_kick(vq))) + return -EIO; + + /* Spin for a response, the kick causes an ioport write, trapping + * into the hypervisor, so the request should be handled immediately. + */ + while (!virtqueue_get_buf(vq, &len) && + !virtqueue_is_broken(vq)) + cpu_relax(); + + return 0; +} +EXPORT_SYMBOL_GPL(virtqueue_exec_cmd); + MODULE_LICENSE("GPL"); diff --git a/include/linux/virtio.h b/include/linux/virtio.h index 4cc614a38376..9d39706bed10 100644 --- a/include/linux/virtio.h +++ b/include/linux/virtio.h @@ -103,6 +103,13 @@ int virtqueue_resize(struct virtqueue *vq, u32 num, int virtqueue_reset(struct virtqueue *vq, void (*recycle)(struct virtqueue *vq, void *buf)); +int virtqueue_exec_cmd(struct virtqueue *vq, + struct scatterlist **sgs, + unsigned int out_num, + unsigned int in_num, + void *data, + gfp_t gfp); + /** * struct virtio_device - representation of a device using virtio * @index: unique position on the virtio bus From patchwork Thu Sep 21 12:40:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yishai Hadas X-Patchwork-Id: 13394130 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 444F9E7D0A0 for ; Thu, 21 Sep 2023 17:27:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229603AbjIUR1H (ORCPT ); Thu, 21 Sep 2023 13:27:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54970 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229901AbjIUR0r (ORCPT ); Thu, 21 Sep 2023 13:26:47 -0400 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on20626.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe59::626]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B4551FE7 for ; Thu, 21 Sep 2023 10:01:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cLEezKFixK5Br/JxQwlfxDQHi3991kA6X9ngB463NfN/tmqL3VoZc8DGtbpHVwclksXEPtcZ6LQAjAWWuHxt2eMxiYW/lec1Mz7QPRKnsAaX4CaXrUYYpDUJKSW4p8vPEa8Rx8PA4LZ8k2G+ma/eIikDZvRe7bYE5biCO/pNX5XRS9dRto695ssigT5RY55OMuymQlcBchY8TJw6Kad4QwmujHlCVziVmWxQ+YpGA3dvvHpuPzocfVfnKrCGKwwWOS9fgXvO3FmrlZPdH5Qhl8fjpYIMxz4aJLPNmh+auLdvLhEoQsH1zkGMdoZQYQgPLRmLmpXbPcvQZR4pjQACog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=vs0v/NljeSeqjShQU8l7ntCvJ7v12eFf8Z064gNRmlU=; b=kaZXwxchqlU3B5xSY8O1yytuLZwkqSDPQ/gVqG+isH6ReCao4TbkjAcgqCDWgYMPoiCYQoGcZh+Fy84vJmueb6gwIbTlVzbxgbSrwExl0EXOcjT7DLV6UNL/VAbAk6WQbalvEKI0rqkJppCRB89LouelnTxyN2gGf68XZF6+8Q9bCHT7WoT1aIv3s2wvpBUd0k/twZ9IWCDp36GBkkNGmp/WJcUs+nuWCX0kEPLyhPYCPLgE/fwn4vnsA2fn7t6FjmXxV5VUo98k5XujLu4X8tR+eFLVVTI4LonJBoTi6ZL5fFVDa+jI8agCggKne19ALC2mBKSXcV5aBvLECE6nBg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=redhat.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=vs0v/NljeSeqjShQU8l7ntCvJ7v12eFf8Z064gNRmlU=; b=hKIuaWmJ61Qsr+dMRITkT7Lj4dxOmKkU07GPyO/5eaYgLf3ey2ss4IostbQ9fhY84R4a0xW/2QdpKT9Cmph3a1XX1PLiM6olECQ+qu0YILPyUi7JTzR0jQJdzOwl5rAy5yrvmLDV53cdE7qz2XuaOmaev771beqH3QJ+cfkzfIJhVokWCp3zUg1nhhyGKHpjHBmljkyKCq0zFa+UurKAZcYN/yxxw7GYSt0Ga+66i3dw8wAhHzclG0+bhmz74+oG0ESf9pCkGxu3mO48srJ2TLigBYUw75QpkpKNZdjowmHJf8rAelx7Sx0DgruA/NReMiFxUQ8WYS/lq7XzuqkvCw== Received: from DM6PR02CA0132.namprd02.prod.outlook.com (2603:10b6:5:1b4::34) by PH8PR12MB7325.namprd12.prod.outlook.com (2603:10b6:510:217::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.28; Thu, 21 Sep 2023 12:41:53 +0000 Received: from DS1PEPF00017090.namprd03.prod.outlook.com (2603:10b6:5:1b4:cafe::ce) by DM6PR02CA0132.outlook.office365.com (2603:10b6:5:1b4::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.31 via Frontend Transport; Thu, 21 Sep 2023 12:41:52 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by DS1PEPF00017090.mail.protection.outlook.com (10.167.17.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.20 via Frontend Transport; Thu, 21 Sep 2023 12:41:52 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 21 Sep 2023 05:41:43 -0700 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail203.nvidia.com (10.129.68.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 21 Sep 2023 05:41:42 -0700 Received: from vdi.nvidia.com (10.127.8.10) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.986.41 via Frontend Transport; Thu, 21 Sep 2023 05:41:39 -0700 From: Yishai Hadas To: , , , CC: , , , , , , , , , Subject: [PATCH vfio 05/11] virtio-pci: Introduce admin command sending function Date: Thu, 21 Sep 2023 15:40:34 +0300 Message-ID: <20230921124040.145386-6-yishaih@nvidia.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20230921124040.145386-1-yishaih@nvidia.com> References: <20230921124040.145386-1-yishaih@nvidia.com> MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS1PEPF00017090:EE_|PH8PR12MB7325:EE_ X-MS-Office365-Filtering-Correlation-Id: 058fab27-fb7f-4339-ca9d-08dbbaa021ce X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: kLOfhF6rek/57gYUa/oZzlxT8dXMIGIU3BXGxPiIc6GEsjV791lne1gUfWFdZZT5SP5zKNCSgU8qDF8ZjiA36FO5OUj+L3J2MvrPTPtWgcdpDS8mYPBhO9JHbrFxD4TiShN577y7DFvH4dMG/CEGYh/QG0EtLfIkjFy1oKKdOClL0UUurSdV1Q83lwjU2zSan+glmxK2NriTpJK68bEUhoX4IT9bQ9mET4eMX8GdPLXIFPqwShf+bre+qw2GZftulJ8Ytt37TxtNib1fX6/2m29m7F5lhwFLlPrUBzCVi0zNi0YDWH4zIkgwJZFiD8t6tkNqWZrK5v03SkFoCaWVD5vRRrEWh+eONUvm2WH+9/tRPCCWlurMP1v5X0kYAIJdWp5Zw+Tlhjm2jH34ZZRDxgP1PkLq9v6islUJmo1TR8FEotN8fhaKGYIPxgPuT20HVx28gEn3XP3/O5P1q+ecBoA91g1baz9KWl42NkqXXJLRrfxR1dbGRxnGBBVA1TgZVrp7lYDYD50rLtGA/WRIqGl4LKfkKVOR+SqyYP/yWr4OHbuctEwPaW8FBZVhRkKfJuuef6hyx0C1IYZThWnpynBH1G1t1I05BI7sFgI9K/GqwFGzDW/aqSgqpH1JEyB/n2jUKq7CFF93wIdW3Kc4IKZuq9MgLD9ebxKlypn4SmsrLnGViyIpZD/nddmZFtnQw8UdaN1n99enu9CaMBJMNL6Kh8lzt0v4HGKIlgrQMgnTsY5cv0YgNJHgRyxTCGU/ML+b2pIcu9Ji9kVaRGLmZQ== X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230031)(4636009)(136003)(39860400002)(346002)(396003)(376002)(451199024)(1800799009)(186009)(82310400011)(36840700001)(46966006)(40470700004)(36756003)(40460700003)(83380400001)(8676002)(40480700001)(36860700001)(26005)(86362001)(356005)(4326008)(8936002)(41300700001)(336012)(7636003)(107886003)(426003)(1076003)(82740400003)(2616005)(7696005)(6666004)(47076005)(5660300002)(2906002)(70586007)(70206006)(316002)(54906003)(6636002)(110136005)(478600001)(2101003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2023 12:41:52.6385 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 058fab27-fb7f-4339-ca9d-08dbbaa021ce X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DS1PEPF00017090.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH8PR12MB7325 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Feng Liu Add support for sending admin command through admin virtqueue interface, and expose generic API to execute virtio admin command. Reuse the send synchronous command helper function at virtio transport layer. In addition, add new result state of admin command and admin commands range definitions. Signed-off-by: Feng Liu Reviewed-by: Parav Pandit Reviewed-by: Jiri Pirko Signed-off-by: Yishai Hadas --- drivers/virtio/virtio.c | 7 +++ drivers/virtio/virtio_pci_common.h | 1 + drivers/virtio/virtio_pci_modern.c | 2 + drivers/virtio/virtio_pci_modern_avq.c | 73 ++++++++++++++++++++++++++ include/linux/virtio.h | 11 ++++ include/linux/virtio_config.h | 3 ++ include/uapi/linux/virtio_pci.h | 22 ++++++++ 7 files changed, 119 insertions(+) diff --git a/drivers/virtio/virtio.c b/drivers/virtio/virtio.c index f4080692b351..dd71f584a1bd 100644 --- a/drivers/virtio/virtio.c +++ b/drivers/virtio/virtio.c @@ -582,6 +582,13 @@ int virtio_device_restore(struct virtio_device *dev) EXPORT_SYMBOL_GPL(virtio_device_restore); #endif +int virtio_admin_cmd_exec(struct virtio_device *vdev, + struct virtio_admin_cmd *cmd) +{ + return vdev->config->exec_admin_cmd(vdev, cmd); +} +EXPORT_SYMBOL_GPL(virtio_admin_cmd_exec); + static int virtio_init(void) { if (bus_register(&virtio_bus) != 0) diff --git a/drivers/virtio/virtio_pci_common.h b/drivers/virtio/virtio_pci_common.h index 9bffa95274b6..a579f1338263 100644 --- a/drivers/virtio/virtio_pci_common.h +++ b/drivers/virtio/virtio_pci_common.h @@ -128,6 +128,7 @@ int vp_find_vqs(struct virtio_device *vdev, unsigned int nvqs, const char *vp_bus_name(struct virtio_device *vdev); void vp_destroy_avq(struct virtio_device *vdev); int vp_create_avq(struct virtio_device *vdev); +int vp_avq_cmd_exec(struct virtio_device *vdev, struct virtio_admin_cmd *cmd); /* Setup the affinity for a virtqueue: * - force the affinity for per vq vector diff --git a/drivers/virtio/virtio_pci_modern.c b/drivers/virtio/virtio_pci_modern.c index a72c87687196..cac18872b088 100644 --- a/drivers/virtio/virtio_pci_modern.c +++ b/drivers/virtio/virtio_pci_modern.c @@ -515,6 +515,7 @@ static const struct virtio_config_ops virtio_pci_config_nodev_ops = { .enable_vq_after_reset = vp_modern_enable_vq_after_reset, .create_avq = vp_create_avq, .destroy_avq = vp_destroy_avq, + .exec_admin_cmd = vp_avq_cmd_exec, }; static const struct virtio_config_ops virtio_pci_config_ops = { @@ -537,6 +538,7 @@ static const struct virtio_config_ops virtio_pci_config_ops = { .enable_vq_after_reset = vp_modern_enable_vq_after_reset, .create_avq = vp_create_avq, .destroy_avq = vp_destroy_avq, + .exec_admin_cmd = vp_avq_cmd_exec, }; /* the PCI probing function */ diff --git a/drivers/virtio/virtio_pci_modern_avq.c b/drivers/virtio/virtio_pci_modern_avq.c index 114579ad788f..ca3fe10f616d 100644 --- a/drivers/virtio/virtio_pci_modern_avq.c +++ b/drivers/virtio/virtio_pci_modern_avq.c @@ -19,6 +19,79 @@ static u16 vp_modern_avq_index(struct virtio_pci_modern_device *mdev) return vp_ioread16(&cfg->admin_queue_index); } +#define VIRTIO_AVQ_SGS_MAX 4 + +int vp_avq_cmd_exec(struct virtio_device *vdev, struct virtio_admin_cmd *cmd) +{ + struct scatterlist *sgs[VIRTIO_AVQ_SGS_MAX], hdr, stat; + struct virtio_pci_device *vp_dev = to_vp_device(vdev); + struct virtio_admin_cmd_status *va_status; + unsigned int out_num = 0, in_num = 0; + struct virtio_admin_cmd_hdr *va_hdr; + struct virtqueue *avq; + u16 status; + int ret; + + avq = vp_dev->admin ? vp_dev->admin->info.vq : NULL; + if (!avq) + return -EOPNOTSUPP; + + va_status = kzalloc(sizeof(*va_status), GFP_KERNEL); + if (!va_status) + return -ENOMEM; + + va_hdr = kzalloc(sizeof(*va_hdr), GFP_KERNEL); + if (!va_hdr) { + ret = -ENOMEM; + goto err_alloc; + } + + va_hdr->opcode = cmd->opcode; + va_hdr->group_type = cmd->group_type; + va_hdr->group_member_id = cmd->group_member_id; + + /* Add header */ + sg_init_one(&hdr, va_hdr, sizeof(*va_hdr)); + sgs[out_num] = &hdr; + out_num++; + + if (cmd->data_sg) { + sgs[out_num] = cmd->data_sg; + out_num++; + } + + /* Add return status */ + sg_init_one(&stat, va_status, sizeof(*va_status)); + sgs[out_num + in_num] = &stat; + in_num++; + + if (cmd->result_sg) { + sgs[out_num + in_num] = cmd->result_sg; + in_num++; + } + + ret = virtqueue_exec_cmd(avq, sgs, out_num, in_num, sgs, GFP_KERNEL); + if (ret) { + dev_err(&vdev->dev, + "Failed to execute command on admin vq: %d\n.", ret); + goto err_cmd_exec; + } + + status = le16_to_cpu(va_status->status); + if (status != VIRTIO_ADMIN_STATUS_OK) { + dev_err(&vdev->dev, + "admin command error: status(%#x) qualifier(%#x)\n", + status, le16_to_cpu(va_status->status_qualifier)); + ret = -status; + } + +err_cmd_exec: + kfree(va_hdr); +err_alloc: + kfree(va_status); + return ret; +} + int vp_create_avq(struct virtio_device *vdev) { struct virtio_pci_device *vp_dev = to_vp_device(vdev); diff --git a/include/linux/virtio.h b/include/linux/virtio.h index 9d39706bed10..094a2ef1c8b8 100644 --- a/include/linux/virtio.h +++ b/include/linux/virtio.h @@ -110,6 +110,14 @@ int virtqueue_exec_cmd(struct virtqueue *vq, void *data, gfp_t gfp); +struct virtio_admin_cmd { + __le16 opcode; + __le16 group_type; + __le64 group_member_id; + struct scatterlist *data_sg; + struct scatterlist *result_sg; +}; + /** * struct virtio_device - representation of a device using virtio * @index: unique position on the virtio bus @@ -207,6 +215,9 @@ static inline struct virtio_driver *drv_to_virtio(struct device_driver *drv) return container_of(drv, struct virtio_driver, driver); } +int virtio_admin_cmd_exec(struct virtio_device *vdev, + struct virtio_admin_cmd *cmd); + int register_virtio_driver(struct virtio_driver *drv); void unregister_virtio_driver(struct virtio_driver *drv); diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h index 028c51ea90ee..e213173e1291 100644 --- a/include/linux/virtio_config.h +++ b/include/linux/virtio_config.h @@ -95,6 +95,7 @@ typedef void vq_callback_t(struct virtqueue *); * set. * @create_avq: initialize admin virtqueue resource. * @destroy_avq: destroy admin virtqueue resource. + * @exec_admin_cmd: Send admin command and get result. */ struct virtio_config_ops { void (*get)(struct virtio_device *vdev, unsigned offset, @@ -124,6 +125,8 @@ struct virtio_config_ops { int (*enable_vq_after_reset)(struct virtqueue *vq); int (*create_avq)(struct virtio_device *vdev); void (*destroy_avq)(struct virtio_device *vdev); + int (*exec_admin_cmd)(struct virtio_device *vdev, + struct virtio_admin_cmd *cmd); }; /* If driver didn't advertise the feature, it will never appear. */ diff --git a/include/uapi/linux/virtio_pci.h b/include/uapi/linux/virtio_pci.h index f703afc7ad31..1f1ac6ac07df 100644 --- a/include/uapi/linux/virtio_pci.h +++ b/include/uapi/linux/virtio_pci.h @@ -207,4 +207,26 @@ struct virtio_pci_cfg_cap { #endif /* VIRTIO_PCI_NO_MODERN */ +/* Admin command status. */ +#define VIRTIO_ADMIN_STATUS_OK 0 + +struct virtio_admin_cmd_hdr { + __le16 opcode; + /* + * 1 - SR-IOV + * 2-65535 - reserved + */ + __le16 group_type; + /* Unused, reserved for future extensions. */ + __u8 reserved1[12]; + __le64 group_member_id; +} __packed; + +struct virtio_admin_cmd_status { + __le16 status; + __le16 status_qualifier; + /* Unused, reserved for future extensions. */ + __u8 reserved2[4]; +} __packed; + #endif From patchwork Thu Sep 21 12:40:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yishai Hadas X-Patchwork-Id: 13394111 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1AD4FE7D0A1 for ; Thu, 21 Sep 2023 17:23:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229843AbjIURX0 (ORCPT ); Thu, 21 Sep 2023 13:23:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38652 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229580AbjIURXF (ORCPT ); Thu, 21 Sep 2023 13:23:05 -0400 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2070.outbound.protection.outlook.com [40.107.237.70]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 07B917A89 for ; Thu, 21 Sep 2023 10:10:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=g3Qc2NWQ4tWV+7KGEDCUnaoknarsd+kYJJChrsAiHYZ4Mpg7YHc3xIM5fbsDOSgZ7+51ppoH5Cj1mGG5sqQTGSYHomq/+8G1rNrmQCNGSonqiq73VJICxP0YlQ7K/GGJa3BCrXyG+MefD9/LadT5/o5g/powyjOm8uAwRdSJghQHvpqA/KdK4+VHXCsPvvmMsQQsVn7bUIBn7ZeD9g1aXOxX61MS9TKZbqte8vN9ja7vx9CgKrTB5OL9kfaeoiann7iDtET9AiTrDRQHytUYyAH1tYl3YE6u3uZOhZIWXP4gU/21VyAypCs1y9tG+nQSE8SlmhFFjOTPkUw0OZbI5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=PgDMGFjt7FhY5awHmR/4HjyxidJdDGmuEBy/dRx1OYw=; b=Q2C26n/iedINiZ9hR7g+zpMa+1mvF5negyDXsHySGV15Ci7mDyjPE8VCtuBpLfd6/q9ekv7iWc9yrOnLoY066G8Y+lGPDvlx9WS9GBGCMZA1BfWfU2oQabL6Dib/ItVPlXVlUp1tM+ckehCyu2xtE1pr+c3SarV3hO0zuQQHBg8Aw0mpZYucK0b1kNXAVaI9k9C0ZaQqyMzM+pgGcT335CCtPFe71eZIYcM7/iGGcjG59ttnFdIBufZbvswPmhH4vDRnnGITln5fAUziJSrQ13byg9JAL6w4qwuEFgLcRDUI1XI0tN02WUdoLnanOOQ7qXka3zelHHqrvNuoPeYRnw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=redhat.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=PgDMGFjt7FhY5awHmR/4HjyxidJdDGmuEBy/dRx1OYw=; b=KKpMjvPSkhyi7ODFJ1WeKb8x3lJAQeJBI8GJOkL2g+/pUcxoUNGp5vwmbpZcfMR7rOAhr8A/vcRk3HgiGBFIcMZ9jPTR4y70m1GgVe7eT5D9JGtROd9khK3iq3pWZf3YwlCr8WMuiCw1oCP1cwEmE1NPVMW95Dznf4DQZaPY7SWa9fWl7zPUVVuO3Pn9gJmTgqbnbVKbiZPTM71CPM8Ls26B3qgeLAAiYVP2gMwYSkwpzkn4SN9RhOX+WNDgg6en8uKWEnursoSipl6oHgmhFSQBxoyDvInebdcribINDBuI30Ns9tdgdoqQJFe2MItda727CHPt8sKkQoFgYOnTJg== Received: from DS7PR06CA0008.namprd06.prod.outlook.com (2603:10b6:8:2a::15) by BL1PR12MB5756.namprd12.prod.outlook.com (2603:10b6:208:393::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.28; Thu, 21 Sep 2023 12:42:00 +0000 Received: from DS3PEPF000099D4.namprd04.prod.outlook.com (2603:10b6:8:2a:cafe::a0) by DS7PR06CA0008.outlook.office365.com (2603:10b6:8:2a::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.31 via Frontend Transport; Thu, 21 Sep 2023 12:42:00 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by DS3PEPF000099D4.mail.protection.outlook.com (10.167.17.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.20 via Frontend Transport; Thu, 21 Sep 2023 12:42:00 +0000 Received: from rnnvmail203.nvidia.com (10.129.68.9) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 21 Sep 2023 05:41:47 -0700 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail203.nvidia.com (10.129.68.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 21 Sep 2023 05:41:46 -0700 Received: from vdi.nvidia.com (10.127.8.10) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.986.41 via Frontend Transport; Thu, 21 Sep 2023 05:41:43 -0700 From: Yishai Hadas To: , , , CC: , , , , , , , , , Subject: [PATCH vfio 06/11] virtio-pci: Introduce API to get PF virtio device from VF PCI device Date: Thu, 21 Sep 2023 15:40:35 +0300 Message-ID: <20230921124040.145386-7-yishaih@nvidia.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20230921124040.145386-1-yishaih@nvidia.com> References: <20230921124040.145386-1-yishaih@nvidia.com> MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS3PEPF000099D4:EE_|BL1PR12MB5756:EE_ X-MS-Office365-Filtering-Correlation-Id: 1dd09c3d-c23d-4a87-13e1-08dbbaa02664 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: RoTGVpt1x/4ODigQwlrMsf7wE/JvBuAb3pYxs08kskZ+DcrIIO1jRXz+3k0WHYe+jr2fyE44DqgC9m+rdBozPB5Qzg9Cg++BVXc+sl/fZeRAKik7/CUYR0ZduORRm0EktpDrBtAQwCRrH/B5mL1MmybJDjYkkM3Y3Gmxq6R1JtQBA6SpzMZwbjlqGMiLWpXzehDiqo99jIMc68degbHJyQwXdtaNiz0b3xYAIy+jf4v+siH5ig3viqSRIqIefY5qijFW9Dd+iuFaZda+nyccmPz87oieobWce3g+lLjim+V6k7f1hI/rxUDZOpsUo320kKpSJnhlo8nipbyhXhPV4eZpKheZU556ddIVpvD4k2Hh3wfganiIu9gPxNPDGYT8ye1OseML4+sI39Tq605Q0HsH3vtzXPY3tj0Ze/x3f89o9GWWxmeRAA9B/hppt0csqWbifQETfb9LTl/BYc0MzrrhQEQco1Yv50OpwZzpgzj/JSSsvkDC88BH5EGYzM3ouSFzaSshGQBxAhBJAEdtejoqTIPIX8fGuY17RtR0fBncgGfG7gzhUEDt0WA/YWB7OcKrZq5aYz5a3GSt/mMtr7S/W9D8dPr5//5IzJUVbIWYgsVRl9n9ftmdI3JIMUdeVbJ041wDOi+hY3HthPxxt3XhFcYESsXjRA0L+uf50W6QGvF+ZFF1YTggIgAS3ggZw45BdwpACvYl+ZlBlrlIe+/dKZ/BumUmZV78XIbD2+JbzSAP2euF8cLhWeFklWtR3+MqVY5SCTsZuAzuXIKDfg== X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230031)(4636009)(396003)(346002)(376002)(136003)(39860400002)(451199024)(186009)(1800799009)(82310400011)(36840700001)(40470700004)(46966006)(6666004)(7696005)(426003)(40480700001)(356005)(40460700003)(36756003)(82740400003)(7636003)(26005)(36860700001)(86362001)(107886003)(2906002)(478600001)(83380400001)(47076005)(70586007)(6636002)(70206006)(8936002)(2616005)(41300700001)(5660300002)(4326008)(8676002)(1076003)(316002)(336012)(110136005)(54906003)(2101003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2023 12:42:00.3277 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1dd09c3d-c23d-4a87-13e1-08dbbaa02664 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DS3PEPF000099D4.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5756 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Feng Liu Introduce API to get PF virtio device from the given VF PCI device so that other modules such as vfio in subsequent patch can use it. Signed-off-by: Feng Liu Reviewed-by: Parav Pandit Reviewed-by: Jiri Pirko Signed-off-by: Yishai Hadas --- drivers/virtio/virtio_pci_common.c | 12 ++++++++++++ include/linux/virtio.h | 1 + 2 files changed, 13 insertions(+) diff --git a/drivers/virtio/virtio_pci_common.c b/drivers/virtio/virtio_pci_common.c index 7a3e6edc4dd6..c64484cd5b13 100644 --- a/drivers/virtio/virtio_pci_common.c +++ b/drivers/virtio/virtio_pci_common.c @@ -648,6 +648,18 @@ static struct pci_driver virtio_pci_driver = { .sriov_configure = virtio_pci_sriov_configure, }; +struct virtio_device *virtio_pci_vf_get_pf_dev(struct pci_dev *pdev) +{ + struct virtio_pci_device *pf_vp_dev; + + pf_vp_dev = pci_iov_get_pf_drvdata(pdev, &virtio_pci_driver); + if (IS_ERR(pf_vp_dev)) + return NULL; + + return &pf_vp_dev->vdev; +} +EXPORT_SYMBOL_GPL(virtio_pci_vf_get_pf_dev); + module_pci_driver(virtio_pci_driver); MODULE_AUTHOR("Anthony Liguori "); diff --git a/include/linux/virtio.h b/include/linux/virtio.h index 094a2ef1c8b8..4ae088ea9299 100644 --- a/include/linux/virtio.h +++ b/include/linux/virtio.h @@ -217,6 +217,7 @@ static inline struct virtio_driver *drv_to_virtio(struct device_driver *drv) int virtio_admin_cmd_exec(struct virtio_device *vdev, struct virtio_admin_cmd *cmd); +struct virtio_device *virtio_pci_vf_get_pf_dev(struct pci_dev *pdev); int register_virtio_driver(struct virtio_driver *drv); void unregister_virtio_driver(struct virtio_driver *drv); From patchwork Thu Sep 21 12:40:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yishai Hadas X-Patchwork-Id: 13394044 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A583DE71096 for ; Thu, 21 Sep 2023 17:02:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229989AbjIURCW (ORCPT ); Thu, 21 Sep 2023 13:02:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45138 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229869AbjIURCF (ORCPT ); Thu, 21 Sep 2023 13:02:05 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2061b.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e89::61b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1DF00182 for ; Thu, 21 Sep 2023 10:01:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=l6EAL8IqdhZTB0L9Ci9Ku1TPfACxaDoNqRQvlbzwXoWZKjsa98OfdUmxhzxy8bUC4WrZ+O2DPHYroXOXJArL7zQaiOSqm1sS7RLGUB+TQxpReRUy15DZOLlU327YJufSykEM4PDP0SVfcZtOiLT48iOvia7yWP7TkbDOtXfyI3uCBWvWpSHC9xATUTbM/FGiQstvA9khAENL1cSM3+4z2MjNsr0VGJdO3Wmbw6C5w9gP1Sc/2nIh5yth1Jc2B+MJziuvO0Ft0beDWi+QWJdHFQw1rmAesVyiLE8nQ8xHwRdpfJZNzC7p67Cb1952f5HJlHWxXe5Ucdr6AnIxYRrBkw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=/S0ZFJNHbAruE1hYwcCiAQ+pYhNwNj5wHSYQXqHBcrk=; b=fu2FdjYMhBlHRiucm5n83e0IOCkh3/241oOm1j5iOLh8R5mb9AP5xqf6gLYnvgXIXtqCUG3NOiMukwTnPR8nfEafVmQsFn6b1tlkBW9lFHqqYYbPXlHYdUqnI0f9nDiL/tny1cG0keDkQdKkEQXtn4g/31KfGVRr3poSDh7wQvyALNmJCKzstGmTUW/pU30VzcGh+TqMzAuu/jv/zJgGsGeZ9tbDDHjZljXljIh7dx1ZUmMRYmLa2nFcIKwIWCS1n7R/EJ2MSLIXhEcKSmWSgBBXzYswVTPoM4q3ZPmEyz1+ShzDYaaUAayvI+mSJc8JS4tvOuxtyQyg1tUduAFIPA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=redhat.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=/S0ZFJNHbAruE1hYwcCiAQ+pYhNwNj5wHSYQXqHBcrk=; b=R1qHNpWNQOwXMtSmbplvTAOPn5MKda1GCgjWwu55iQLTqeZpLvrj7Y5nHEe/zH87W5qMaiGeJAORNOcJOXUomW45Gmzz/ZKg/SWYm7yWdOmyCsOzxfbfj52qAZyEl++zBM4Hyf33FQIjUbGo3KTw4NNhu1N/mBjkabM9UFAaRvPr5JvY/pJ+yWucvfYwD2fGmhPRJvU/FnqldxAOowsTEcrTnjAhOMh0RuErklvaTkDXBpa1FLOW85yDnbQ89DvjRHna2jQ9AImD/qd2f/iIiTCoMPBe7zKEm41ehVSuHkl/tDvwyw4HoHm5MCNldZqd/6PWKG8jg9xBj3vJDHLRmA== Received: from DM6PR02CA0128.namprd02.prod.outlook.com (2603:10b6:5:1b4::30) by BL1PR12MB5176.namprd12.prod.outlook.com (2603:10b6:208:311::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.28; Thu, 21 Sep 2023 12:42:01 +0000 Received: from DS1PEPF00017090.namprd03.prod.outlook.com (2603:10b6:5:1b4:cafe::2d) by DM6PR02CA0128.outlook.office365.com (2603:10b6:5:1b4::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.31 via Frontend Transport; Thu, 21 Sep 2023 12:42:01 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by DS1PEPF00017090.mail.protection.outlook.com (10.167.17.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.20 via Frontend Transport; Thu, 21 Sep 2023 12:42:01 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 21 Sep 2023 05:41:51 -0700 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 21 Sep 2023 05:41:50 -0700 Received: from vdi.nvidia.com (10.127.8.10) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.986.41 via Frontend Transport; Thu, 21 Sep 2023 05:41:47 -0700 From: Yishai Hadas To: , , , CC: , , , , , , , , , Subject: [PATCH vfio 07/11] virtio-pci: Introduce admin commands Date: Thu, 21 Sep 2023 15:40:36 +0300 Message-ID: <20230921124040.145386-8-yishaih@nvidia.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20230921124040.145386-1-yishaih@nvidia.com> References: <20230921124040.145386-1-yishaih@nvidia.com> MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS1PEPF00017090:EE_|BL1PR12MB5176:EE_ X-MS-Office365-Filtering-Correlation-Id: e909e10b-5652-445d-c6bf-08dbbaa026df X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: s/f3GZJzflzutcOlycHFomKkrPLZg0yhFTue6vOLl6ypYN4wrLFKkd8YNizkatJ7oJgvIsB3KgdoyNBR48TheVaBWndwvoYvH9Gbpw1drMl6/5h7aZNq1Kc/KcNuhrqy9+Ns6LKxj1eyRx9XsGruVZ+wEyzJFPkUmTu+vi4QqAkReW3FOZDN1k2aA9roqhbydXGQ+my8c7zStJ4JYMtcG+3GPmPG3idxEf7w0CY2/AeV1db7XQ4WmTzbCdyozuTkdErRF2Pv6+eLY271jP4DkQeI8g9ZSIOm8UMtxY2QW2vp8EvtJSN/GSLyaXqlS6DadONqrz5qNdYUYr6PXbSmipRXYzsfrspQMdvhvjHZzD9MRi3oVnN+6atYNLxctvOPDIBhGojFFJWcBIsAKS2TTfcIhZp/aufnZuNTnAuCuiDjsgFQeMPVGj9txzDr1d1Q7rGk0opGIajyENwu9xNjNJzwl7oyl+Hui/itE54TTPlrITI6OUQZREsyYM0HkjyJcDI2Bouwv+tQAdy1/hc9gm/fzZDv3ynIVHyQLCsR7Ax1Rh9x2JiS6BYZLCn1IoSfEXiMNleR9CypNE25xf1O8etRoE5scaVd0/8VNN+pjGzBOsSWr2afp0jxtbkNncKPh3BH7ZRK+WSBk+jUlJDrVdA0ahnq61pnhqLJ1Hb0UoKAhFlCduvUyQhs7OJZdxP8nyZsIhwZNrJkWWFI+EFCOrEIYFuyt42uBqI1PR44e3UUmJPVk/6ngtqychDGag1FhosbnKhq8fOpEtCnHJhIVA== X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230031)(4636009)(136003)(376002)(39860400002)(396003)(346002)(82310400011)(186009)(1800799009)(451199024)(40470700004)(36840700001)(46966006)(6666004)(7696005)(36860700001)(82740400003)(86362001)(70586007)(70206006)(41300700001)(110136005)(54906003)(6636002)(316002)(478600001)(356005)(7636003)(107886003)(8676002)(8936002)(1076003)(4326008)(36756003)(40460700003)(26005)(47076005)(336012)(40480700001)(5660300002)(426003)(2616005)(83380400001)(2906002)(2101003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2023 12:42:01.1384 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: e909e10b-5652-445d-c6bf-08dbbaa026df X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DS1PEPF00017090.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: BL1PR12MB5176 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Feng Liu Introduces admin commands, as follow: The "list query" command can be used by the driver to query the set of admin commands supported by the virtio device. The "list use" command is used to inform the virtio device which admin commands the driver will use. The "legacy common cfg rd/wr" commands are used to read from/write into the legacy common configuration structure. The "legacy dev cfg rd/wr" commands are used to read from/write into the legacy device configuration structure. The "notify info" command is used to query the notification region information. Signed-off-by: Feng Liu Reviewed-by: Parav Pandit Reviewed-by: Jiri Pirko Signed-off-by: Yishai Hadas --- include/uapi/linux/virtio_pci.h | 44 +++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/include/uapi/linux/virtio_pci.h b/include/uapi/linux/virtio_pci.h index 1f1ac6ac07df..2bf275ad0f20 100644 --- a/include/uapi/linux/virtio_pci.h +++ b/include/uapi/linux/virtio_pci.h @@ -210,6 +210,23 @@ struct virtio_pci_cfg_cap { /* Admin command status. */ #define VIRTIO_ADMIN_STATUS_OK 0 +/* Admin command opcode. */ +#define VIRTIO_ADMIN_CMD_LIST_QUERY 0x0 +#define VIRTIO_ADMIN_CMD_LIST_USE 0x1 + +/* Admin command group type. */ +#define VIRTIO_ADMIN_GROUP_TYPE_SRIOV 0x1 + +/* Transitional device admin command. */ +#define VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_WRITE 0x2 +#define VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_READ 0x3 +#define VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_WRITE 0x4 +#define VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_READ 0x5 +#define VIRTIO_ADMIN_CMD_LEGACY_NOTIFY_INFO 0x6 + +/* Increment MAX_OPCODE to next value when new opcode is added */ +#define VIRTIO_ADMIN_MAX_CMD_OPCODE 0x6 + struct virtio_admin_cmd_hdr { __le16 opcode; /* @@ -229,4 +246,31 @@ struct virtio_admin_cmd_status { __u8 reserved2[4]; } __packed; +struct virtio_admin_cmd_legacy_wr_data { + u8 offset; /* Starting offset of the register(s) to write. */ + u8 reserved[7]; + u8 registers[]; +} __packed; + +struct virtio_admin_cmd_legacy_rd_data { + u8 offset; /* Starting offset of the register(s) to read. */ +} __packed; + +#define VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_END 0 +#define VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_OWNER_DEV 0x1 +#define VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_OWNER_MEM 0x2 + +#define VIRTIO_ADMIN_CMD_MAX_NOTIFY_INFO 4 + +struct virtio_admin_cmd_notify_info_data { + u8 flags; /* 0 = end of list, 1 = owner device, 2 = member device */ + u8 bar; /* BAR of the member or the owner device */ + u8 padding[6]; + __le64 offset; /* Offset within bar. */ +}; __packed + +struct virtio_admin_cmd_notify_info_result { + struct virtio_admin_cmd_notify_info_data entries[VIRTIO_ADMIN_CMD_MAX_NOTIFY_INFO]; +}; + #endif From patchwork Thu Sep 21 12:40:37 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yishai Hadas X-Patchwork-Id: 13394042 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 79ED1E71096 for ; Thu, 21 Sep 2023 17:01:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229783AbjIURB5 (ORCPT ); Thu, 21 Sep 2023 13:01:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229924AbjIURBX (ORCPT ); Thu, 21 Sep 2023 13:01:23 -0400 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on20611.outbound.protection.outlook.com [IPv6:2a01:111:f400:7eaa::611]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ADDBF10E4 for ; Thu, 21 Sep 2023 10:00:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=h0P3gujRrdEg/YgG0J8AAr20h6WlO0eX8hRTgAf/1FI9lg/d4gyowktJNYl0q8W308hfb45LexsE/wUHcNYkPR3OPa0D3kG4btbepg5h0wbkGaz22vCb/y0qLmXO0XHjrjFwcr9EF+4fjfGYRZiVoS9xgcsKL3k8qfKg6YdGXcnQ5CeG+QYBt3SbzBaHLaanoegkMNHpEHoId+PvLDR6nZ4KXiYhQlbleCE5e3mgmLzLr2wdwMGj56XSoecy2OstPIoHuMMdvpwzaW+MP0PeE710ePca4kJ2yLsct93Xby0sQ5P0tTDoLyp5T1lv7jCofJ+J+RTewlNXpOttRxx+8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=DoCuwgDC8nSkydt1xw3EhuUYLdSoxtsTl5j142Bf8Z8=; b=i6ynx/KFXPdl7CWnHOr0Y+0PdaZfp8Yk2D6L+DL7VwbocTGRjIEsmcbxMreErIOFKopyXPNbWThT397on4t7hrBwIyt7m4XNd97oVWdrAut4YK6SmybG6B4U+4ZqUzOksIN3AUfWxQfgbB0XuVPZfwMtLni2w7cYoEKnyfx/t8xS6f1OUJcMnDs/0g0m4LMT+hLzYWGj/9lAn+aZkTrl+pTFkaCr6lnXKuZ8DbiYy7amXA1E4aPjwKKe9nwwPjil5/HJ0TE9s2eawJyoDeHxvuQ3aVOL2gqhWb+IIpMBVDfvNp/QuCwx5cr93QsknuspvJolZRDCAwlJtXz7Fms+FQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=redhat.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=DoCuwgDC8nSkydt1xw3EhuUYLdSoxtsTl5j142Bf8Z8=; b=k4CMP9clZ3uKO0MJQ+tZ/i4cQYUsOT8MFxy9R/hi7Xt/QKi3mCS+pL+85tJq4kgWMR/+NQ//JclTzhU3P87+C/QRv76iM86Q2xNwCU2pKgaJcISU7AkRAMuvwimbfoHNc2A8pCR+EHzv26RGNA1gpLCLkArNvSVwhnvBwQpLLP/Y8ZY4HmipCoGCHASnPSxHNQOww5SqTJae9VS2ywdlMzQ9v4ktESKH5zvsUFc3Ur9R6dJYTPFVq7ERBwWAOidEUHBeQL2ZtkSW3nH2FGKAqqTh8f8shYgQMHUOW/T41h4BU9JUXm7GDGRtY+eds/shsNein53QkbH4s+JzXuEP2g== Received: from DS7PR05CA0034.namprd05.prod.outlook.com (2603:10b6:8:2f::35) by MW6PR12MB7072.namprd12.prod.outlook.com (2603:10b6:303:238::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.28; Thu, 21 Sep 2023 12:42:06 +0000 Received: from DS1PEPF0001708F.namprd03.prod.outlook.com (2603:10b6:8:2f:cafe::5a) by DS7PR05CA0034.outlook.office365.com (2603:10b6:8:2f::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6838.9 via Frontend Transport; Thu, 21 Sep 2023 12:42:06 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by DS1PEPF0001708F.mail.protection.outlook.com (10.167.17.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.20 via Frontend Transport; Thu, 21 Sep 2023 12:42:06 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 21 Sep 2023 05:41:55 -0700 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail202.nvidia.com (10.129.68.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 21 Sep 2023 05:41:54 -0700 Received: from vdi.nvidia.com (10.127.8.10) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.986.41 via Frontend Transport; Thu, 21 Sep 2023 05:41:51 -0700 From: Yishai Hadas To: , , , CC: , , , , , , , , , Subject: [PATCH vfio 08/11] vfio/pci: Expose vfio_pci_core_setup_barmap() Date: Thu, 21 Sep 2023 15:40:37 +0300 Message-ID: <20230921124040.145386-9-yishaih@nvidia.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20230921124040.145386-1-yishaih@nvidia.com> References: <20230921124040.145386-1-yishaih@nvidia.com> MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS1PEPF0001708F:EE_|MW6PR12MB7072:EE_ X-MS-Office365-Filtering-Correlation-Id: 6fa6021e-cda3-43f0-515f-08dbbaa029d3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PfBSQPeNA8ov5FmvqUU4SxIphmZKKim+Lq1r6rg6ZoyBYrSc8ievKDkWu/0dVtOOLoG0vE8RmsIb+GdKv60KGfHLRFdxvXFJehlSKvmEM3LrJVyJADwUAW0BY1WOhrjlNl3N1mBWeIWf25eaQ+ib6id12sGN00aijhjPUPgocrr0oQuKADG4RJZCpDmNDaIt9OYIqSJiCM+1gMoHyj6LvGcJKu34LBKI5yjsf1PHnvX3x0BzDXXXA/ETpvTQ+UZUQxoSZVVdIqIkb9pOukvaDn4LBD2LgxqmrQCSqN0YWF3zdDchi9y2ai1NLsSmxdNJhLJCqN5/X4jm6rzVhigqZdBb00DXVH8MSWhY3dG9Qg4GRbnbny8ZguQ6qPVj+J8gxkQGyVhB9c+1zcAviV6iqSYCEZk3x1zhCsHik69bT+GfriWSvoWZoToVB6dpHLkmkBgthW9hryZYg6Pe9JgSkIgOjuRTIUZbpBfczHUEcl/vohHgd2cKY8h7L+13906Yeub0OiH/JsBpSsU5SUb0RWARdUxV9h1yTGuGCtTZqEtZxoDb8JceydbBydWPglWNtZZMNQHhC2E6nqkgPTXiw2OpCBAd9dXPVmxWYiTE8HXMk8QxK7+65eS5YkAZOQ9tSQmxqp5ZX1qxSGqD48XFllozFo4N8MIFDatT/TEBGoG3vX/UEVfbcJDiOWUvOiK/l/KRAwJ5iYr3l3/TZWuhweVNJhxpHcxmVrF2A1Em62DfpLUg+uS9vpg3GQyuPMD2DoKAcwVn+IUczUfYzQFKMw== X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230031)(4636009)(346002)(396003)(136003)(39860400002)(376002)(82310400011)(451199024)(1800799009)(186009)(40470700004)(36840700001)(46966006)(2906002)(5660300002)(4326008)(8676002)(41300700001)(8936002)(6636002)(316002)(54906003)(70206006)(26005)(70586007)(110136005)(336012)(426003)(1076003)(478600001)(7636003)(40460700003)(7696005)(6666004)(2616005)(107886003)(83380400001)(36756003)(356005)(82740400003)(47076005)(36860700001)(40480700001)(86362001)(2101003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2023 12:42:06.0899 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 6fa6021e-cda3-43f0-515f-08dbbaa029d3 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DS1PEPF0001708F.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW6PR12MB7072 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Expose vfio_pci_core_setup_barmap() to be used by drivers. This will let drivers to mmap a BAR and re-use it from both vfio and the driver when it's applicable. This API will be used in the next patches by the vfio/virtio coming driver. Signed-off-by: Yishai Hadas --- drivers/vfio/pci/vfio_pci_core.c | 25 +++++++++++++++++++++++++ drivers/vfio/pci/vfio_pci_rdwr.c | 28 ++-------------------------- include/linux/vfio_pci_core.h | 1 + 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c index 1929103ee59a..b56111ed8a8c 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -684,6 +684,31 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev) } EXPORT_SYMBOL_GPL(vfio_pci_core_disable); +int vfio_pci_core_setup_barmap(struct vfio_pci_core_device *vdev, int bar) +{ + struct pci_dev *pdev = vdev->pdev; + void __iomem *io; + int ret; + + if (vdev->barmap[bar]) + return 0; + + ret = pci_request_selected_regions(pdev, 1 << bar, "vfio"); + if (ret) + return ret; + + io = pci_iomap(pdev, bar, 0); + if (!io) { + pci_release_selected_regions(pdev, 1 << bar); + return -ENOMEM; + } + + vdev->barmap[bar] = io; + + return 0; +} +EXPORT_SYMBOL(vfio_pci_core_setup_barmap); + void vfio_pci_core_close_device(struct vfio_device *core_vdev) { struct vfio_pci_core_device *vdev = diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c index e27de61ac9fe..6f08b3ecbb89 100644 --- a/drivers/vfio/pci/vfio_pci_rdwr.c +++ b/drivers/vfio/pci/vfio_pci_rdwr.c @@ -200,30 +200,6 @@ static ssize_t do_io_rw(struct vfio_pci_core_device *vdev, bool test_mem, return done; } -static int vfio_pci_setup_barmap(struct vfio_pci_core_device *vdev, int bar) -{ - struct pci_dev *pdev = vdev->pdev; - int ret; - void __iomem *io; - - if (vdev->barmap[bar]) - return 0; - - ret = pci_request_selected_regions(pdev, 1 << bar, "vfio"); - if (ret) - return ret; - - io = pci_iomap(pdev, bar, 0); - if (!io) { - pci_release_selected_regions(pdev, 1 << bar); - return -ENOMEM; - } - - vdev->barmap[bar] = io; - - return 0; -} - ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf, size_t count, loff_t *ppos, bool iswrite) { @@ -262,7 +238,7 @@ ssize_t vfio_pci_bar_rw(struct vfio_pci_core_device *vdev, char __user *buf, } x_end = end; } else { - int ret = vfio_pci_setup_barmap(vdev, bar); + int ret = vfio_pci_core_setup_barmap(vdev, bar); if (ret) { done = ret; goto out; @@ -438,7 +414,7 @@ int vfio_pci_ioeventfd(struct vfio_pci_core_device *vdev, loff_t offset, return -EINVAL; #endif - ret = vfio_pci_setup_barmap(vdev, bar); + ret = vfio_pci_core_setup_barmap(vdev, bar); if (ret) return ret; diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h index 562e8754869d..67ac58e20e1d 100644 --- a/include/linux/vfio_pci_core.h +++ b/include/linux/vfio_pci_core.h @@ -127,6 +127,7 @@ int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf); int vfio_pci_core_enable(struct vfio_pci_core_device *vdev); void vfio_pci_core_disable(struct vfio_pci_core_device *vdev); void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev); +int vfio_pci_core_setup_barmap(struct vfio_pci_core_device *vdev, int bar); pci_ers_result_t vfio_pci_core_aer_err_detected(struct pci_dev *pdev, pci_channel_state_t state); From patchwork Thu Sep 21 12:40:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yishai Hadas X-Patchwork-Id: 13394131 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7B77BE7D0A4 for ; Thu, 21 Sep 2023 17:27:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230200AbjIUR1q (ORCPT ); Thu, 21 Sep 2023 13:27:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55122 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229790AbjIUR1K (ORCPT ); Thu, 21 Sep 2023 13:27:10 -0400 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on20625.outbound.protection.outlook.com [IPv6:2a01:111:f400:fe5a::625]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46178CDA for ; Thu, 21 Sep 2023 10:01:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CskpOzTrBeA3lBGkmgyJG/q3CFBrVvri/t9NCxq9WJLR/a+S9VKY+3G6R9NrPJZoGfyCwEJFOu5harSrwYL26Jo/epBG+WNjvdkNN22LpYTVEMkMbD2wYgfUt7iHKnweaRumZYTEzh9NI9Wr1+jBN53V5C1WAfrZttCuhcko2D07vokqIfolMA4DSeJWWjE5x71uVfwJOzgqOiNeJINhpyIQGz7aCM9PX58VQ+JhmhcRvP+wWlAJ5Oh44w6BUXRDWAiM2z6mVxZtcjbPJpKZBYC3etykA1JvZpZj7aPXaszI2mDpzpXIznhOeR2GVcbXhWbYaIXrlhi6BcW2x1dPSQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=EJelJnn/JyXTdadjpJZBsKAweBrMvLSpqlwHLntWW68=; b=IfH2bm7lwlS+7YNvsfvxahGPUWAoFI8X3SYVu0UX/qu2cKjpmx5TjcC0awR9ttZLQmyPEWJQvneCimdsvM8jLUvgROym1+PiQps3Q7uILP12CTwIJFne9T+dKf7jMkHvwBCMECAwJXfnViYZLzhbFVDSawi5whoBlvIbsgR7bqBi82zgZKEfl1xHOm7ws4RIvDKiZQmhavuZdCukyvtpsmyHd4PRH87z99SHe7fe7tAUrFfnXC2JGgsB1HyqdTiv/wJRYdhNYTjvHdKpdSm6Lr9KS7k+jgpYAe+5w5xyG8mKJqK3Q/XffYg333nb6ePQQcYFLIoAorL7hK8eyX2/mw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=redhat.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=EJelJnn/JyXTdadjpJZBsKAweBrMvLSpqlwHLntWW68=; b=Lt2YkDrBi9OUx8AVqgHl2HHqi0DqV708IYDz5ShUfbAAUSgynR/8gTBIC5aVu+GG04Z+4seZ9uzmItHlMybwgBUUXcT9BxS88Lunhx4d4H+MiGv61x0xrZtxeuu4eNYN09VQf0zudroj7xMYmdNcUEL464/EqK9jwgQqzsXrxDRHdh+8WNurS6zG5/MRwqh5P3q+mSwryb1g1LoMB9NwuD2QvWXCTzFe/tRJjrTk4JQRmWimhYpQ6vwb9/qatHB6/33ASVF4tORov+2llc8hMplB9oZNb2qaWcv8rrUqYaro4abf6Fko58DwB/IVuDhxaUzbK9RjDIyGLRo/9+iRFg== Received: from CH0PR03CA0208.namprd03.prod.outlook.com (2603:10b6:610:e4::33) by MN0PR12MB5979.namprd12.prod.outlook.com (2603:10b6:208:37e::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6813.19; Thu, 21 Sep 2023 12:42:11 +0000 Received: from DS3PEPF000099D9.namprd04.prod.outlook.com (2603:10b6:610:e4:cafe::d3) by CH0PR03CA0208.outlook.office365.com (2603:10b6:610:e4::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.31 via Frontend Transport; Thu, 21 Sep 2023 12:42:11 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by DS3PEPF000099D9.mail.protection.outlook.com (10.167.17.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.19 via Frontend Transport; Thu, 21 Sep 2023 12:42:11 +0000 Received: from rnnvmail205.nvidia.com (10.129.68.10) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 21 Sep 2023 05:41:59 -0700 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail205.nvidia.com (10.129.68.10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 21 Sep 2023 05:41:58 -0700 Received: from vdi.nvidia.com (10.127.8.10) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.986.41 via Frontend Transport; Thu, 21 Sep 2023 05:41:55 -0700 From: Yishai Hadas To: , , , CC: , , , , , , , , , Subject: [PATCH vfio 09/11] vfio/pci: Expose vfio_pci_iowrite/read##size() Date: Thu, 21 Sep 2023 15:40:38 +0300 Message-ID: <20230921124040.145386-10-yishaih@nvidia.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20230921124040.145386-1-yishaih@nvidia.com> References: <20230921124040.145386-1-yishaih@nvidia.com> MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS3PEPF000099D9:EE_|MN0PR12MB5979:EE_ X-MS-Office365-Filtering-Correlation-Id: 0f24e8f8-d919-44f6-9938-08dbbaa02cda X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Wz13dtetZ1aRPZGvRfvyGrnaeLodu6N4FZZFSL8v4MDX2woKieMMM8xwjyDkwk4j+7iFnh2PQYTOY9/WvhHpRNLysIL/6BkuQYKsFYFdb3Zs0QgTqFbx882rwdGkphzZULC/CUOUs8onKhYmaXITssx6bprDN52ao3m79ATbMj4jsFouxkYTVkjG7NW/D+Mf9d72NlqG3ZFTEzUkGHxtiPZJvRoVxp091s73FEXBXLGdcs/7JL+Z+Khu36Cbvyo5KETlbCnhJVHhSZOOrbtjp1Y9T9mnFQMb2dAxcZ3Y9QoWYVtNdL/OMOYMIpNZpkoL6j0eagaJ1WY4yhtkeWbrf6z9Wb+7YfSuPQL7NHw+Z9BSAOg7mYFYTrx7grHqc5rzZ0XSfO8DGnUecKIttkWxo3MLaS2oCGy//+6BgbZ+Oo9bnEbMnOJNnRKkAkN9gZue0AIrz5KVErzQz2oZIXJ8EoEfJLm9Feqp4iJFINePKNnY25uAdNS+GQsDAgVJpq5Yl7i/f4Xy536iHD2cqNfUOBCeVfbjo3kmemrTF4evwz50hjpQhH6zLllc2l8WgrTxD2rk6/iGomPXMfosdOBYiqvdjo+CP4OuhAKM1zI+Fg4ZE4V70k0K8LuzTaF3zM5QNOsiGI4SQa/eksknI2l5Fco61DjRoFXwJjUCTGRvenOCLldMw1NKOj8u61HCEwBzsr2husT7LjFg/iKpSfWdcli63vqTQIa/KZIMhik1i/wc/G8Ah8cp3tGtqI2NIASARPsGWlPRgVj2jxPTgXaVIw== X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230031)(4636009)(136003)(39860400002)(396003)(346002)(376002)(1800799009)(82310400011)(186009)(451199024)(46966006)(36840700001)(40470700004)(478600001)(6666004)(83380400001)(8936002)(8676002)(7696005)(70586007)(70206006)(54906003)(6636002)(316002)(110136005)(5660300002)(40460700003)(107886003)(1076003)(2616005)(36756003)(36860700001)(82740400003)(86362001)(40480700001)(4326008)(336012)(2906002)(47076005)(426003)(26005)(41300700001)(356005)(7636003)(2101003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2023 12:42:11.1708 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 0f24e8f8-d919-44f6-9938-08dbbaa02cda X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DS3PEPF000099D9.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB5979 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Expose vfio_pci_iowrite/read##size() to let it be used by drivers. This functionality is needed to enable direct access to some physical BAR of the device with the proper locks/checks in place. The next patches from this series will use this functionality on a data path flow when a direct access to the BAR is needed. Signed-off-by: Yishai Hadas --- drivers/vfio/pci/vfio_pci_rdwr.c | 10 ++++++---- include/linux/vfio_pci_core.h | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_rdwr.c b/drivers/vfio/pci/vfio_pci_rdwr.c index 6f08b3ecbb89..5d84bad7d30c 100644 --- a/drivers/vfio/pci/vfio_pci_rdwr.c +++ b/drivers/vfio/pci/vfio_pci_rdwr.c @@ -38,7 +38,7 @@ #define vfio_iowrite8 iowrite8 #define VFIO_IOWRITE(size) \ -static int vfio_pci_iowrite##size(struct vfio_pci_core_device *vdev, \ +int vfio_pci_iowrite##size(struct vfio_pci_core_device *vdev, \ bool test_mem, u##size val, void __iomem *io) \ { \ if (test_mem) { \ @@ -55,7 +55,8 @@ static int vfio_pci_iowrite##size(struct vfio_pci_core_device *vdev, \ up_read(&vdev->memory_lock); \ \ return 0; \ -} +} \ +EXPORT_SYMBOL(vfio_pci_iowrite##size); VFIO_IOWRITE(8) VFIO_IOWRITE(16) @@ -65,7 +66,7 @@ VFIO_IOWRITE(64) #endif #define VFIO_IOREAD(size) \ -static int vfio_pci_ioread##size(struct vfio_pci_core_device *vdev, \ +int vfio_pci_ioread##size(struct vfio_pci_core_device *vdev, \ bool test_mem, u##size *val, void __iomem *io) \ { \ if (test_mem) { \ @@ -82,7 +83,8 @@ static int vfio_pci_ioread##size(struct vfio_pci_core_device *vdev, \ up_read(&vdev->memory_lock); \ \ return 0; \ -} +} \ +EXPORT_SYMBOL(vfio_pci_ioread##size); VFIO_IOREAD(8) VFIO_IOREAD(16) diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h index 67ac58e20e1d..22c915317788 100644 --- a/include/linux/vfio_pci_core.h +++ b/include/linux/vfio_pci_core.h @@ -131,4 +131,23 @@ int vfio_pci_core_setup_barmap(struct vfio_pci_core_device *vdev, int bar); pci_ers_result_t vfio_pci_core_aer_err_detected(struct pci_dev *pdev, pci_channel_state_t state); +#define VFIO_IOWRITE_DECLATION(size) \ +int vfio_pci_iowrite##size(struct vfio_pci_core_device *vdev, \ + bool test_mem, u##size val, void __iomem *io); + +VFIO_IOWRITE_DECLATION(8) +VFIO_IOWRITE_DECLATION(16) +VFIO_IOWRITE_DECLATION(32) +#ifdef iowrite64 +VFIO_IOWRITE_DECLATION(64) +#endif + +#define VFIO_IOREAD_DECLATION(size) \ +int vfio_pci_ioread##size(struct vfio_pci_core_device *vdev, \ + bool test_mem, u##size *val, void __iomem *io); + +VFIO_IOREAD_DECLATION(8) +VFIO_IOREAD_DECLATION(16) +VFIO_IOREAD_DECLATION(32) + #endif /* VFIO_PCI_CORE_H */ From patchwork Thu Sep 21 12:40:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yishai Hadas X-Patchwork-Id: 13394132 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B965FE7D0A4 for ; Thu, 21 Sep 2023 17:27:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230269AbjIUR2A (ORCPT ); Thu, 21 Sep 2023 13:28:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54856 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230076AbjIUR1g (ORCPT ); Thu, 21 Sep 2023 13:27:36 -0400 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam04on2060c.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e8d::60c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 604522122 for ; Thu, 21 Sep 2023 10:01:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=obs13qzRLqEp2cvKb7XAL3FTvpzxqO20AiQrfkzNfGp0QGTqbi+ze6ZL+wUCiOSeEcK6LFhYGZSQZdOzQmLwkm4yScuLD65Q+75m8wo8AwnLYZB1BUBokeNK4YT53SeWuS1KdqvRJoWVHiCGp3ZcfOvJONVOdoxQ9VcFKk8xVM4HCzjLTTexFlPP09VujhlrriMDde+q9ZywmxQPi4cJcJpcjJ4dhyHctYlRAFBzy313UGVla+tn7JFroSUk8SGs7aKKd6gfNruarg1ClMCkdjY3zkOK1G6yxVJ9e/VK5jQYaksw0Y88FUoFRz23WQ/AMsQbYup04VoQPPuTlLRvbQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=wo6WmwIdSkX0NeDL2rVYm1D/kPxM7/2CfouItdS7Ck8=; b=M3m7oUDuQq/IIaNXAXJb30uY7MDND6cTasFNBLL4YrmffUGYJ/Rn97lHZbSrm+pZQUfTvj8HO4wcU02H768emfiHQxLriGwN6SEJh9or5DeYKLGl8mnYxrqwvsNDoRQSJINRUfDIr2jcRY1chLAlP84WpM+laNeNJk1FOn/oN49Xt9LUrqbwJkHtnxXwxG8gN34v1GJOyw5g2KQpVcSxEZQ0Bfb6uhX6PjXojrQdaXW3Df6lPDugSjbbjJDcXztjhBqLA4uGlzhky35s8OYt7JWkS8dRX3+rmHW0Yu7ZPUVvnLNv9BtheDwy5q6H3rc2Y2JEQBglbQEHVhGXXaSphg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=redhat.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=wo6WmwIdSkX0NeDL2rVYm1D/kPxM7/2CfouItdS7Ck8=; b=GfYy9s7iUOVw4+oAT/ZhUGJcJpR4oY4ev/PDIjjX2eN7SmVMAAB4FVH9K7FEg+IKFwqSppXeNwA0iOxq8YlQYM/+SLcFiK/wED6ULzfQ+YHT4qOIfZnbxx5cFNuLk+rmC+1jMyL5xty9upK5Y5sut58aKPc1kTuxPsu/NM/K8VUKmju8WWDGWzbQEBEJZYAW5uxUCYlQ7TXGfiB5RUzFagorUhCTfDBf6okd9rKahZ62Jn8O3j1tzODgY8QVZvmj7EJtdiGn6d0nOUEMtUG71QL0OxmAE9EiKZR5TFkS2f+PXzigeMPN4eduE8Z6JwkVE8gj6ifW3q5/nH+DQz9a1w== Received: from BYAPR11CA0041.namprd11.prod.outlook.com (2603:10b6:a03:80::18) by DS0PR12MB8501.namprd12.prod.outlook.com (2603:10b6:8:15d::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.30; Thu, 21 Sep 2023 12:42:16 +0000 Received: from DS3PEPF000099D5.namprd04.prod.outlook.com (2603:10b6:a03:80:cafe::a0) by BYAPR11CA0041.outlook.office365.com (2603:10b6:a03:80::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.30 via Frontend Transport; Thu, 21 Sep 2023 12:42:15 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by DS3PEPF000099D5.mail.protection.outlook.com (10.167.17.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.20 via Frontend Transport; Thu, 21 Sep 2023 12:42:15 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 21 Sep 2023 05:42:03 -0700 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail202.nvidia.com (10.129.68.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 21 Sep 2023 05:42:02 -0700 Received: from vdi.nvidia.com (10.127.8.10) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.986.41 via Frontend Transport; Thu, 21 Sep 2023 05:41:59 -0700 From: Yishai Hadas To: , , , CC: , , , , , , , , , Subject: [PATCH vfio 10/11] vfio/virtio: Expose admin commands over virtio device Date: Thu, 21 Sep 2023 15:40:39 +0300 Message-ID: <20230921124040.145386-11-yishaih@nvidia.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20230921124040.145386-1-yishaih@nvidia.com> References: <20230921124040.145386-1-yishaih@nvidia.com> MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS3PEPF000099D5:EE_|DS0PR12MB8501:EE_ X-MS-Office365-Filtering-Correlation-Id: 26ef6772-5ae2-4483-1404-08dbbaa02f5a X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Koq1JKg6gKopejYPxqvIjWdkwKr4lBUruFca9uQxAXl5vRHaMeMvkJyRBgXLP0gQkIV0SSTAI7smPqE7dgey98JAh/u/lMbPZo7wz/AqqCIoOwZrtbfRYJTKeB06B8HbveLeTPadapqQwWq8YPQx/FyjJ2HWtTizTd1e8+W59LIqMHYvxfSkZ0SyU+UTsxv1eEkoknlBYpxxgKuBcw6K8PXzWEb1SQp+TE5UafiAqIWMrdp3GGSkNgDzIhvJH7MQzC5lAO9c9GT+6bAdzmJ2t31pcJI6JwNWxamcpIRlVxZKVTdv/YfU2zrXm3FFOwhvYv0PM+14PWWW51jLzb4QopNRonRWZA+HmeUmMDfSH+jFanpqEGOSQ1RTLyidc4aChkowalIu3LQ4LRRwBNVilPHavasqw+yKixCc00wS9Sunnj8adHS4rJf3WmVAVpXYVedQeBX6V4oxMUDGRLz9wxNgB8Cf2+UYzQJ5e2BRdP/qESpkiNNUBV1139+f3R+OotXqIy8RGdE8DTo7B6lwOh8CirBUusIqFLWT7DyRlrpFfS9yAaMK0DKXXd70kntbAUTPYBYWR6EsQbUm4sYJ2iGiSPx0F43oGlWNpnnygEixRsOQA5NMJT/I4+OVl4C2GV57R6yaowjIhGv5XYcm4cyx1XVqO1b2OyT2DnSLxrrdFHZ5BklE8SqHNk9VhHLMSN4tBibFeKRcuu79GuWgf3QEpYKk+mw1GcS/rjpQGo4CEo483I19ZhyqIQ7c6MizFUJ/S/FuMX/oI0pLK7Wk4g== X-Forefront-Antispam-Report: CIP:216.228.117.161;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge2.nvidia.com;CAT:NONE;SFS:(13230031)(4636009)(136003)(346002)(396003)(376002)(39860400002)(451199024)(1800799009)(186009)(82310400011)(36840700001)(40470700004)(46966006)(7696005)(6666004)(40460700003)(36756003)(40480700001)(7636003)(356005)(86362001)(82740400003)(36860700001)(1076003)(2906002)(426003)(47076005)(336012)(26005)(8936002)(478600001)(83380400001)(5660300002)(110136005)(4326008)(54906003)(41300700001)(316002)(70586007)(6636002)(2616005)(70206006)(107886003)(8676002)(2101003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2023 12:42:15.3460 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 26ef6772-5ae2-4483-1404-08dbbaa02f5a X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.161];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: DS3PEPF000099D5.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR12MB8501 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Expose admin commands over the virtio device, to be used by the vfio-virtio driver in the next patches. It includes: list query/use, legacy write/read, read notify_info. Signed-off-by: Yishai Hadas --- drivers/vfio/pci/virtio/cmd.c | 146 ++++++++++++++++++++++++++++++++++ drivers/vfio/pci/virtio/cmd.h | 27 +++++++ 2 files changed, 173 insertions(+) create mode 100644 drivers/vfio/pci/virtio/cmd.c create mode 100644 drivers/vfio/pci/virtio/cmd.h diff --git a/drivers/vfio/pci/virtio/cmd.c b/drivers/vfio/pci/virtio/cmd.c new file mode 100644 index 000000000000..f068239cdbb0 --- /dev/null +++ b/drivers/vfio/pci/virtio/cmd.c @@ -0,0 +1,146 @@ +// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB +/* + * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved + */ + +#include "cmd.h" + +int virtiovf_cmd_list_query(struct pci_dev *pdev, u8 *buf, int buf_size) +{ + struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev); + struct scatterlist out_sg; + struct virtio_admin_cmd cmd = {}; + + if (!virtio_dev) + return -ENOTCONN; + + sg_init_one(&out_sg, buf, buf_size); + cmd.opcode = VIRTIO_ADMIN_CMD_LIST_QUERY; + cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV; + cmd.result_sg = &out_sg; + + return virtio_admin_cmd_exec(virtio_dev, &cmd); +} + +int virtiovf_cmd_list_use(struct pci_dev *pdev, u8 *buf, int buf_size) +{ + struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(pdev); + struct scatterlist in_sg; + struct virtio_admin_cmd cmd = {}; + + if (!virtio_dev) + return -ENOTCONN; + + sg_init_one(&in_sg, buf, buf_size); + cmd.opcode = VIRTIO_ADMIN_CMD_LIST_USE; + cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV; + cmd.data_sg = &in_sg; + + return virtio_admin_cmd_exec(virtio_dev, &cmd); +} + +int virtiovf_cmd_lr_write(struct virtiovf_pci_core_device *virtvdev, u16 opcode, + u8 offset, u8 size, u8 *buf) +{ + struct virtio_device *virtio_dev = + virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev); + struct virtio_admin_cmd_data_lr_write *in; + struct scatterlist in_sg; + struct virtio_admin_cmd cmd = {}; + int ret; + + if (!virtio_dev) + return -ENOTCONN; + + in = kzalloc(sizeof(*in) + size, GFP_KERNEL); + if (!in) + return -ENOMEM; + + in->offset = offset; + memcpy(in->registers, buf, size); + sg_init_one(&in_sg, in, sizeof(*in) + size); + cmd.opcode = opcode; + cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV; + cmd.group_member_id = virtvdev->vf_id + 1; + cmd.data_sg = &in_sg; + ret = virtio_admin_cmd_exec(virtio_dev, &cmd); + + kfree(in); + return ret; +} + +int virtiovf_cmd_lr_read(struct virtiovf_pci_core_device *virtvdev, u16 opcode, + u8 offset, u8 size, u8 *buf) +{ + struct virtio_device *virtio_dev = + virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev); + struct virtio_admin_cmd_data_lr_read *in; + struct scatterlist in_sg, out_sg; + struct virtio_admin_cmd cmd = {}; + int ret; + + if (!virtio_dev) + return -ENOTCONN; + + in = kzalloc(sizeof(*in), GFP_KERNEL); + if (!in) + return -ENOMEM; + + in->offset = offset; + sg_init_one(&in_sg, in, sizeof(*in)); + sg_init_one(&out_sg, buf, size); + cmd.opcode = opcode; + cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV; + cmd.data_sg = &in_sg; + cmd.result_sg = &out_sg; + cmd.group_member_id = virtvdev->vf_id + 1; + ret = virtio_admin_cmd_exec(virtio_dev, &cmd); + + kfree(in); + return ret; +} + +int virtiovf_cmd_lq_read_notify(struct virtiovf_pci_core_device *virtvdev, + u8 req_bar_flags, u8 *bar, u64 *bar_offset) +{ + struct virtio_device *virtio_dev = + virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev); + struct virtio_admin_cmd_notify_info_result *out; + struct scatterlist out_sg; + struct virtio_admin_cmd cmd = {}; + int ret; + + if (!virtio_dev) + return -ENOTCONN; + + out = kzalloc(sizeof(*out), GFP_KERNEL); + if (!out) + return -ENOMEM; + + sg_init_one(&out_sg, out, sizeof(*out)); + cmd.opcode = VIRTIO_ADMIN_CMD_LEGACY_NOTIFY_INFO; + cmd.group_type = VIRTIO_ADMIN_GROUP_TYPE_SRIOV; + cmd.result_sg = &out_sg; + cmd.group_member_id = virtvdev->vf_id + 1; + ret = virtio_admin_cmd_exec(virtio_dev, &cmd); + if (!ret) { + struct virtio_admin_cmd_notify_info_data *entry; + int i; + + ret = -ENOENT; + for (i = 0; i < VIRTIO_ADMIN_CMD_MAX_NOTIFY_INFO; i++) { + entry = &out->entries[i]; + if (entry->flags == VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_END) + break; + if (entry->flags != req_bar_flags) + continue; + *bar = entry->bar; + *bar_offset = le64_to_cpu(entry->offset); + ret = 0; + break; + } + } + + kfree(out); + return ret; +} diff --git a/drivers/vfio/pci/virtio/cmd.h b/drivers/vfio/pci/virtio/cmd.h new file mode 100644 index 000000000000..c2a3645f4b90 --- /dev/null +++ b/drivers/vfio/pci/virtio/cmd.h @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB +/* + * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. + */ + +#ifndef VIRTIO_VFIO_CMD_H +#define VIRTIO_VFIO_CMD_H + +#include +#include +#include +#include + +struct virtiovf_pci_core_device { + struct vfio_pci_core_device core_device; + int vf_id; +}; + +int virtiovf_cmd_list_query(struct pci_dev *pdev, u8 *buf, int buf_size); +int virtiovf_cmd_list_use(struct pci_dev *pdev, u8 *buf, int buf_size); +int virtiovf_cmd_lr_write(struct virtiovf_pci_core_device *virtvdev, u16 opcode, + u8 offset, u8 size, u8 *buf); +int virtiovf_cmd_lr_read(struct virtiovf_pci_core_device *virtvdev, u16 opcode, + u8 offset, u8 size, u8 *buf); +int virtiovf_cmd_lq_read_notify(struct virtiovf_pci_core_device *virtvdev, + u8 req_bar_flags, u8 *bar, u64 *bar_offset); +#endif /* VIRTIO_VFIO_CMD_H */ From patchwork Thu Sep 21 12:40:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yishai Hadas X-Patchwork-Id: 13394129 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9D20FE7D0A2 for ; Thu, 21 Sep 2023 17:26:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230114AbjIUR06 (ORCPT ); Thu, 21 Sep 2023 13:26:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229927AbjIUR0p (ORCPT ); Thu, 21 Sep 2023 13:26:45 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on20606.outbound.protection.outlook.com [IPv6:2a01:111:f400:7e88::606]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A61E3E79 for ; Thu, 21 Sep 2023 10:01:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CvNbD6ifnX1MS323COsH2yopHxL2g8uPfu7twWdVuGH0qpdjIU/FL5TxJhBw1IEn71PFFsL3vmxwJrfj0nHKmKawM6PeUoUJbnq13ZEclixj7BhAiHBXx1/fGE68Gk3cHEHGAkHD8sWkkBMCDsucNUOOdkVNItDG4TI+WrCc948B4hp/kpBKtn8+jI5YLjt45tq2ztIyzJMgRbxUrsEvUuf3gHz2qLBR+/k2cRhNtthiWMMUx2yru1PzXlvVV4Zj7brvaQqRgo88HHu6yp+BCF7tZnt730Q90O53gevm2N3Gmb31Vnm6Cwq9PYaODWWzDzBi5dkt+CCvC5W39o8hAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=EB2UvYTxW9xlyOHlPqx5ftuoz9voE97yJ7MC6k+z6uI=; b=a7tHbKrSEtS2lvf7kkS+HOVrB3E2m2mHeF49tsSHl19wwu3SVt46Qr9GeIa12yvRMQYKlhtZSmyWRGDgXKOClRqSnd4pdtM00U5pNd+oG3suZ7EO/oGmk1L+yWAtr7C1oZRFLJhZm36MntCw6JcLYxViPg4i8CYLfXffWwKioiL9gQCAwQci19mHK61cWAqIit6YqkmzzyEdkQHbnrPvy3GJBnQmYCAfEGOQrcIz4TaxfKnB6t0FEVpfQiDQKpon5LOfmLXeVpuN/dMtYs7VzW8Lfvoc3WwWCgwbt1GG0SMPBezYS4VfDgJq5QFni0UU1rF7vnkmjNj1c7jXHvK6Zw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.160) smtp.rcpttodomain=redhat.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=EB2UvYTxW9xlyOHlPqx5ftuoz9voE97yJ7MC6k+z6uI=; b=lhdPQ3XHUPnTeE6UmI1sEpFYmxQRYb/vO6XnHvj/+k8N6jxVupt6sXbLukHRf0G7ul7FKabm58wGoU0eANzIn7I1qcYPHmnDlY3MAeQattGJlckzCwGtDUZdhu+zfixCDdiPeZxWWiqlw53eQ7K+y1jE2qxs7aDQA2eOiDJNlQ/L0yr+MfCDpBN9HS2FZcDIOqZCuIesVvAg7DTnjJsvCk+JYwlintW5r4/Wb6bGICArONltFq1dcl05p3RZRbumRHisk3EDEHs9HdJD4auRoi3GTnNgabs5SkSSyWyeqNX3oMCV20MCIBkgqnFFYzy7YHp9BBx4BWaNqJ3zTngIeQ== Received: from MW2PR16CA0061.namprd16.prod.outlook.com (2603:10b6:907:1::38) by PH7PR12MB5782.namprd12.prod.outlook.com (2603:10b6:510:1d1::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.28; Thu, 21 Sep 2023 12:42:17 +0000 Received: from MWH0EPF000989EB.namprd02.prod.outlook.com (2603:10b6:907:1:cafe::19) by MW2PR16CA0061.outlook.office365.com (2603:10b6:907:1::38) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.30 via Frontend Transport; Thu, 21 Sep 2023 12:42:17 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.160) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.160 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.160; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.160) by MWH0EPF000989EB.mail.protection.outlook.com (10.167.241.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.20 via Frontend Transport; Thu, 21 Sep 2023 12:42:16 +0000 Received: from rnnvmail202.nvidia.com (10.129.68.7) by mail.nvidia.com (10.129.200.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 21 Sep 2023 05:42:07 -0700 Received: from rnnvmail204.nvidia.com (10.129.68.6) by rnnvmail202.nvidia.com (10.129.68.7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Thu, 21 Sep 2023 05:42:06 -0700 Received: from vdi.nvidia.com (10.127.8.10) by mail.nvidia.com (10.129.68.6) with Microsoft SMTP Server id 15.2.986.41 via Frontend Transport; Thu, 21 Sep 2023 05:42:03 -0700 From: Yishai Hadas To: , , , CC: , , , , , , , , , Subject: [PATCH vfio 11/11] vfio/virtio: Introduce a vfio driver over virtio devices Date: Thu, 21 Sep 2023 15:40:40 +0300 Message-ID: <20230921124040.145386-12-yishaih@nvidia.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20230921124040.145386-1-yishaih@nvidia.com> References: <20230921124040.145386-1-yishaih@nvidia.com> MIME-Version: 1.0 X-NV-OnPremToCloud: ExternallySecured X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MWH0EPF000989EB:EE_|PH7PR12MB5782:EE_ X-MS-Office365-Filtering-Correlation-Id: 24cc4425-8fa0-4ff7-be0d-08dbbaa03052 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PWNItZj+prBjw4OSMhMHflBUaoUbxhMKLn/92aXt//rzjiuUw+UZkvGvDcG8zsDXSt+gjT4oyODn8TqmtrgeMikhEzSTH9D4QA5DcC5+AfUZWx3EtWWXF6BoXMdaikuherc/2e1oTPCDf2EAHLGN4MQ69PNhgNOA7ewdUxgwenHJMJuDR7b91LRf7D5N+peJeWDnGTgvaq7lLTPZt++SQFRWVs0qt7XTKp9hco5bdvpF3Jrrp1xK23e8JQMheJVz3ao7nEc+6eIG1s3PvEC7dLNVtgYvNWaJpuK+YqKfMOp1wX9gvRNGNsSiGFHIkqdLav38xJLNbUwiZxs/L8vpwEgGe/ZJcNvDwXlTcaFTQ7V9L+yf+m/TjTql247cI6zRmRN2SpoOTVKUOIoX6/Eu7XS9jToo8KMi9fnZ1L92I41KskBCMUO3pH++4ti9v0ZPtd+MA6OMuEhEboqIbLnve0HV87l7oh/9nlsX5cx2rSNa9zWbZopBDjjveBzWHUXZlmYwP4WgBJg6mSAyVq2k/69RYqfbbQvoGa8E6L/67jyb6pnQjoVOm8U8/kbDpAvkQYa8X1x2sMQv/xDMhk4KjNZfT1asezT4zxybOFYtZVWFANxgJlNZU5aq0NgeBpq2in4ekFgjCVFCTIqc6WxmrIN4xNu7s3GlG4M71EqujPH9dVjYCnfs4UzR+OJ0aDItqIidvzJ5ZBiCAAboKkEbkQYIFbSQRx6tLlQ1Rmlq5DyBVt/AmdABHXOisOAbvv0cnnakdSJGx2Hxwx2bubAzs6swLfoPUHbAzhZ0gKMKCYeIclDl/bCyKBvX3f+yEASv X-Forefront-Antispam-Report: CIP:216.228.117.160;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:mail.nvidia.com;PTR:dc6edge1.nvidia.com;CAT:NONE;SFS:(13230031)(4636009)(39860400002)(376002)(136003)(396003)(346002)(186009)(1800799009)(82310400011)(451199024)(36840700001)(40470700004)(46966006)(41300700001)(26005)(47076005)(8676002)(4326008)(70586007)(54906003)(110136005)(70206006)(30864003)(7636003)(356005)(82740400003)(5660300002)(6636002)(36860700001)(8936002)(316002)(336012)(2906002)(426003)(40460700003)(86362001)(966005)(6666004)(478600001)(36756003)(40480700001)(83380400001)(2616005)(7696005)(107886003)(1076003)(2101003);DIR:OUT;SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Sep 2023 12:42:16.9913 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 24cc4425-8fa0-4ff7-be0d-08dbbaa03052 X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a;Ip=[216.228.117.160];Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: MWH0EPF000989EB.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH7PR12MB5782 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Introduce a vfio driver over virtio devices to support the legacy interface functionality for VFs. Background, from the virtio spec [1]. -------------------------------------------------------------------- In some systems, there is a need to support a virtio legacy driver with a device that does not directly support the legacy interface. In such scenarios, a group owner device can provide the legacy interface functionality for the group member devices. The driver of the owner device can then access the legacy interface of a member device on behalf of the legacy member device driver. For example, with the SR-IOV group type, group members (VFs) can not present the legacy interface in an I/O BAR in BAR0 as expected by the legacy pci driver. If the legacy driver is running inside a virtual machine, the hypervisor executing the virtual machine can present a virtual device with an I/O BAR in BAR0. The hypervisor intercepts the legacy driver accesses to this I/O BAR and forwards them to the group owner device (PF) using group administration commands. -------------------------------------------------------------------- Specifically, this driver adds support for a virtio-net VF to be exposed as a transitional device to a guest driver and allows the legacy IO BAR functionality on top. This allows a VM which uses a legacy virtio-net driver in the guest to work transparently over a VF which its driver in the host is that new driver. The driver can be extended easily to support some other types of virtio devices (e.g virtio-blk), by adding in a few places the specific type properties as was done for virtio-net. For now, only the virtio-net use case was tested and as such we introduce the support only for such a device. Practically, Upon probing a VF for a virtio-net device, in case its PF supports legacy access over the virtio admin commands and the VF doesn't have BAR 0, we set some specific 'vfio_device_ops' to be able to simulate in SW a transitional device with I/O BAR in BAR 0. The existence of the simulated I/O bar is reported later on by overwriting the VFIO_DEVICE_GET_REGION_INFO command and the device exposes itself as a transitional device by overwriting some properties upon reading its config space. Once we report the existence of I/O BAR as BAR 0 a legacy driver in the guest may use it via read/write calls according to the virtio specification. Any read/write towards the control parts of the BAR will be captured by the new driver and will be translated into admin commands towards the device. Any data path read/write access (i.e. virtio driver notifications) will be forwarded to the physical BAR which its properties were supplied by the command VIRTIO_PCI_QUEUE_NOTIFY upon the probing/init flow. With that code in place a legacy driver in the guest has the look and feel as if having a transitional device with legacy support for both its control and data path flows. [1] https://github.com/oasis-tcs/virtio-spec/commit/03c2d32e5093ca9f2a17797242fbef88efe94b8c Signed-off-by: Yishai Hadas --- MAINTAINERS | 6 + drivers/vfio/pci/Kconfig | 2 + drivers/vfio/pci/Makefile | 2 + drivers/vfio/pci/virtio/Kconfig | 15 + drivers/vfio/pci/virtio/Makefile | 4 + drivers/vfio/pci/virtio/cmd.c | 4 +- drivers/vfio/pci/virtio/cmd.h | 8 + drivers/vfio/pci/virtio/main.c | 546 +++++++++++++++++++++++++++++++ 8 files changed, 585 insertions(+), 2 deletions(-) create mode 100644 drivers/vfio/pci/virtio/Kconfig create mode 100644 drivers/vfio/pci/virtio/Makefile create mode 100644 drivers/vfio/pci/virtio/main.c diff --git a/MAINTAINERS b/MAINTAINERS index bf0f54c24f81..5098418c8389 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -22624,6 +22624,12 @@ L: kvm@vger.kernel.org S: Maintained F: drivers/vfio/pci/mlx5/ +VFIO VIRTIO PCI DRIVER +M: Yishai Hadas +L: kvm@vger.kernel.org +S: Maintained +F: drivers/vfio/pci/virtio + VFIO PCI DEVICE SPECIFIC DRIVERS R: Jason Gunthorpe R: Yishai Hadas diff --git a/drivers/vfio/pci/Kconfig b/drivers/vfio/pci/Kconfig index 8125e5f37832..18c397df566d 100644 --- a/drivers/vfio/pci/Kconfig +++ b/drivers/vfio/pci/Kconfig @@ -65,4 +65,6 @@ source "drivers/vfio/pci/hisilicon/Kconfig" source "drivers/vfio/pci/pds/Kconfig" +source "drivers/vfio/pci/virtio/Kconfig" + endmenu diff --git a/drivers/vfio/pci/Makefile b/drivers/vfio/pci/Makefile index 45167be462d8..046139a4eca5 100644 --- a/drivers/vfio/pci/Makefile +++ b/drivers/vfio/pci/Makefile @@ -13,3 +13,5 @@ obj-$(CONFIG_MLX5_VFIO_PCI) += mlx5/ obj-$(CONFIG_HISI_ACC_VFIO_PCI) += hisilicon/ obj-$(CONFIG_PDS_VFIO_PCI) += pds/ + +obj-$(CONFIG_VIRTIO_VFIO_PCI) += virtio/ diff --git a/drivers/vfio/pci/virtio/Kconfig b/drivers/vfio/pci/virtio/Kconfig new file mode 100644 index 000000000000..89eddce8b1bd --- /dev/null +++ b/drivers/vfio/pci/virtio/Kconfig @@ -0,0 +1,15 @@ +# SPDX-License-Identifier: GPL-2.0-only +config VIRTIO_VFIO_PCI + tristate "VFIO support for VIRTIO PCI devices" + depends on VIRTIO_PCI + select VFIO_PCI_CORE + help + This provides support for exposing VIRTIO VF devices using the VFIO + framework that can work with a legacy virtio driver in the guest. + Based on PCIe spec, VFs do not support I/O Space; thus, VF BARs shall + not indicate I/O Space. + As of that this driver emulated I/O BAR in software to let a VF be + seen as a transitional device in the guest and let it work with + a legacy driver. + + If you don't know what to do here, say N. diff --git a/drivers/vfio/pci/virtio/Makefile b/drivers/vfio/pci/virtio/Makefile new file mode 100644 index 000000000000..584372648a03 --- /dev/null +++ b/drivers/vfio/pci/virtio/Makefile @@ -0,0 +1,4 @@ +# SPDX-License-Identifier: GPL-2.0-only +obj-$(CONFIG_VIRTIO_VFIO_PCI) += virtio-vfio-pci.o +virtio-vfio-pci-y := main.o cmd.o + diff --git a/drivers/vfio/pci/virtio/cmd.c b/drivers/vfio/pci/virtio/cmd.c index f068239cdbb0..aea9d25fbf1d 100644 --- a/drivers/vfio/pci/virtio/cmd.c +++ b/drivers/vfio/pci/virtio/cmd.c @@ -44,7 +44,7 @@ int virtiovf_cmd_lr_write(struct virtiovf_pci_core_device *virtvdev, u16 opcode, { struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev); - struct virtio_admin_cmd_data_lr_write *in; + struct virtio_admin_cmd_legacy_wr_data *in; struct scatterlist in_sg; struct virtio_admin_cmd cmd = {}; int ret; @@ -74,7 +74,7 @@ int virtiovf_cmd_lr_read(struct virtiovf_pci_core_device *virtvdev, u16 opcode, { struct virtio_device *virtio_dev = virtio_pci_vf_get_pf_dev(virtvdev->core_device.pdev); - struct virtio_admin_cmd_data_lr_read *in; + struct virtio_admin_cmd_legacy_rd_data *in; struct scatterlist in_sg, out_sg; struct virtio_admin_cmd cmd = {}; int ret; diff --git a/drivers/vfio/pci/virtio/cmd.h b/drivers/vfio/pci/virtio/cmd.h index c2a3645f4b90..347b1dc85570 100644 --- a/drivers/vfio/pci/virtio/cmd.h +++ b/drivers/vfio/pci/virtio/cmd.h @@ -13,7 +13,15 @@ struct virtiovf_pci_core_device { struct vfio_pci_core_device core_device; + u8 bar0_virtual_buf_size; + u8 *bar0_virtual_buf; + /* synchronize access to the virtual buf */ + struct mutex bar_mutex; int vf_id; + void __iomem *notify_addr; + u32 notify_offset; + u8 notify_bar; + u8 pci_cmd_io :1; }; int virtiovf_cmd_list_query(struct pci_dev *pdev, u8 *buf, int buf_size); diff --git a/drivers/vfio/pci/virtio/main.c b/drivers/vfio/pci/virtio/main.c new file mode 100644 index 000000000000..2486991c49f3 --- /dev/null +++ b/drivers/vfio/pci/virtio/main.c @@ -0,0 +1,546 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cmd.h" + +#define VIRTIO_LEGACY_IO_BAR_HEADER_LEN 20 +#define VIRTIO_LEGACY_IO_BAR_MSIX_HEADER_LEN 4 + +static int virtiovf_issue_lr_cmd(struct virtiovf_pci_core_device *virtvdev, + loff_t pos, char __user *buf, + size_t count, bool read) +{ + u8 *bar0_buf = virtvdev->bar0_virtual_buf; + u16 opcode; + int ret; + + mutex_lock(&virtvdev->bar_mutex); + if (read) { + opcode = (pos < VIRTIO_PCI_CONFIG_OFF(true)) ? + VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_READ : + VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_READ; + ret = virtiovf_cmd_lr_read(virtvdev, opcode, pos, + count, bar0_buf + pos); + if (ret) + goto out; + if (copy_to_user(buf, bar0_buf + pos, count)) + ret = -EFAULT; + goto out; + } + + if (copy_from_user(bar0_buf + pos, buf, count)) { + ret = -EFAULT; + goto out; + } + + opcode = (pos < VIRTIO_PCI_CONFIG_OFF(true)) ? + VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_WRITE : + VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_WRITE; + ret = virtiovf_cmd_lr_write(virtvdev, opcode, pos, count, + bar0_buf + pos); +out: + mutex_unlock(&virtvdev->bar_mutex); + return ret; +} + +static int +translate_io_bar_to_mem_bar(struct virtiovf_pci_core_device *virtvdev, + loff_t pos, char __user *buf, + size_t count, bool read) +{ + struct vfio_pci_core_device *core_device = &virtvdev->core_device; + u16 queue_notify; + int ret; + + if (pos + count > virtvdev->bar0_virtual_buf_size) + return -EINVAL; + + switch (pos) { + case VIRTIO_PCI_QUEUE_NOTIFY: + if (count != sizeof(queue_notify)) + return -EINVAL; + if (read) { + ret = vfio_pci_ioread16(core_device, true, &queue_notify, + virtvdev->notify_addr); + if (ret) + return ret; + if (copy_to_user(buf, &queue_notify, + sizeof(queue_notify))) + return -EFAULT; + break; + } + + if (copy_from_user(&queue_notify, buf, count)) + return -EFAULT; + + ret = vfio_pci_iowrite16(core_device, true, queue_notify, + virtvdev->notify_addr); + break; + default: + ret = virtiovf_issue_lr_cmd(virtvdev, pos, buf, count, read); + } + + return ret ? ret : count; +} + +static bool range_contains_range(loff_t range1_start, size_t count1, + loff_t range2_start, size_t count2, + loff_t *start_offset) +{ + if (range1_start <= range2_start && + range1_start + count1 >= range2_start + count2) { + *start_offset = range2_start - range1_start; + return true; + } + return false; +} + +static ssize_t virtiovf_pci_read_config(struct vfio_device *core_vdev, + char __user *buf, size_t count, + loff_t *ppos) +{ + struct virtiovf_pci_core_device *virtvdev = container_of( + core_vdev, struct virtiovf_pci_core_device, core_device.vdev); + loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK; + loff_t copy_offset; + __le32 val32; + __le16 val16; + u8 val8; + int ret; + + ret = vfio_pci_core_read(core_vdev, buf, count, ppos); + if (ret < 0) + return ret; + + if (range_contains_range(pos, count, PCI_DEVICE_ID, sizeof(val16), + ©_offset)) { + val16 = cpu_to_le16(0x1000); + if (copy_to_user(buf + copy_offset, &val16, sizeof(val16))) + return -EFAULT; + } + + if (virtvdev->pci_cmd_io && + range_contains_range(pos, count, PCI_COMMAND, sizeof(val16), + ©_offset)) { + if (copy_from_user(&val16, buf, sizeof(val16))) + return -EFAULT; + val16 |= cpu_to_le16(PCI_COMMAND_IO); + if (copy_to_user(buf + copy_offset, &val16, sizeof(val16))) + return -EFAULT; + } + + if (range_contains_range(pos, count, PCI_REVISION_ID, sizeof(val8), + ©_offset)) { + /* Transional needs to have revision 0 */ + val8 = 0; + if (copy_to_user(buf + copy_offset, &val8, sizeof(val8))) + return -EFAULT; + } + + if (range_contains_range(pos, count, PCI_BASE_ADDRESS_0, sizeof(val32), + ©_offset)) { + val32 = cpu_to_le32(PCI_BASE_ADDRESS_SPACE_IO); + if (copy_to_user(buf + copy_offset, &val32, sizeof(val32))) + return -EFAULT; + } + + if (range_contains_range(pos, count, PCI_SUBSYSTEM_ID, sizeof(val16), + ©_offset)) { + /* Transitional devices use the PCI subsystem device id as + * virtio device id, same as legacy driver always did. + */ + val16 = cpu_to_le16(VIRTIO_ID_NET); + if (copy_to_user(buf + copy_offset, &val16, sizeof(val16))) + return -EFAULT; + } + + return count; +} + +static ssize_t +virtiovf_pci_core_read(struct vfio_device *core_vdev, char __user *buf, + size_t count, loff_t *ppos) +{ + struct virtiovf_pci_core_device *virtvdev = container_of( + core_vdev, struct virtiovf_pci_core_device, core_device.vdev); + struct pci_dev *pdev = virtvdev->core_device.pdev; + unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos); + loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK; + int ret; + + if (!count) + return 0; + + if (index == VFIO_PCI_CONFIG_REGION_INDEX) + return virtiovf_pci_read_config(core_vdev, buf, count, ppos); + + if (index != VFIO_PCI_BAR0_REGION_INDEX) + return vfio_pci_core_read(core_vdev, buf, count, ppos); + + ret = pm_runtime_resume_and_get(&pdev->dev); + if (ret) { + pci_info_ratelimited(pdev, "runtime resume failed %d\n", + ret); + return -EIO; + } + + ret = translate_io_bar_to_mem_bar(virtvdev, pos, buf, count, true); + pm_runtime_put(&pdev->dev); + return ret; +} + +static ssize_t +virtiovf_pci_core_write(struct vfio_device *core_vdev, const char __user *buf, + size_t count, loff_t *ppos) +{ + struct virtiovf_pci_core_device *virtvdev = container_of( + core_vdev, struct virtiovf_pci_core_device, core_device.vdev); + struct pci_dev *pdev = virtvdev->core_device.pdev; + unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos); + loff_t pos = *ppos & VFIO_PCI_OFFSET_MASK; + int ret; + + if (!count) + return 0; + + if (index == VFIO_PCI_CONFIG_REGION_INDEX) { + loff_t copy_offset; + u16 cmd; + + if (range_contains_range(pos, count, PCI_COMMAND, sizeof(cmd), + ©_offset)) { + if (copy_from_user(&cmd, buf + copy_offset, sizeof(cmd))) + return -EFAULT; + virtvdev->pci_cmd_io = (cmd & PCI_COMMAND_IO); + } + } + + if (index != VFIO_PCI_BAR0_REGION_INDEX) + return vfio_pci_core_write(core_vdev, buf, count, ppos); + + ret = pm_runtime_resume_and_get(&pdev->dev); + if (ret) { + pci_info_ratelimited(pdev, "runtime resume failed %d\n", ret); + return -EIO; + } + + ret = translate_io_bar_to_mem_bar(virtvdev, pos, (char __user *)buf, count, false); + pm_runtime_put(&pdev->dev); + return ret; +} + +static int +virtiovf_pci_ioctl_get_region_info(struct vfio_device *core_vdev, + unsigned int cmd, unsigned long arg) +{ + struct virtiovf_pci_core_device *virtvdev = container_of( + core_vdev, struct virtiovf_pci_core_device, core_device.vdev); + unsigned long minsz = offsetofend(struct vfio_region_info, offset); + void __user *uarg = (void __user *)arg; + struct vfio_region_info info = {}; + + if (copy_from_user(&info, uarg, minsz)) + return -EFAULT; + + if (info.argsz < minsz) + return -EINVAL; + + switch (info.index) { + case VFIO_PCI_BAR0_REGION_INDEX: + info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); + info.size = virtvdev->bar0_virtual_buf_size; + info.flags = VFIO_REGION_INFO_FLAG_READ | + VFIO_REGION_INFO_FLAG_WRITE; + return copy_to_user(uarg, &info, minsz) ? -EFAULT : 0; + default: + return vfio_pci_core_ioctl(core_vdev, cmd, arg); + } +} + +static long +virtiovf_vfio_pci_core_ioctl(struct vfio_device *core_vdev, unsigned int cmd, + unsigned long arg) +{ + switch (cmd) { + case VFIO_DEVICE_GET_REGION_INFO: + return virtiovf_pci_ioctl_get_region_info(core_vdev, cmd, arg); + default: + return vfio_pci_core_ioctl(core_vdev, cmd, arg); + } +} + +static int +virtiovf_set_notify_addr(struct virtiovf_pci_core_device *virtvdev) +{ + struct vfio_pci_core_device *core_device = &virtvdev->core_device; + int ret; + + /* Setup the BAR where the 'notify' exists to be used by vfio as well + * This will let us mmap it only once and use it when needed. + */ + ret = vfio_pci_core_setup_barmap(core_device, + virtvdev->notify_bar); + if (ret) + return ret; + + virtvdev->notify_addr = core_device->barmap[virtvdev->notify_bar] + + virtvdev->notify_offset; + return 0; +} + +static int virtiovf_pci_open_device(struct vfio_device *core_vdev) +{ + struct virtiovf_pci_core_device *virtvdev = container_of( + core_vdev, struct virtiovf_pci_core_device, core_device.vdev); + struct vfio_pci_core_device *vdev = &virtvdev->core_device; + int ret; + + ret = vfio_pci_core_enable(vdev); + if (ret) + return ret; + + if (virtvdev->bar0_virtual_buf) { + /* upon close_device() the vfio_pci_core_disable() is called + * and will close all the previous mmaps, so it seems that the + * valid life cycle for the 'notify' addr is per open/close. + */ + ret = virtiovf_set_notify_addr(virtvdev); + if (ret) { + vfio_pci_core_disable(vdev); + return ret; + } + } + + vfio_pci_core_finish_enable(vdev); + return 0; +} + +static void virtiovf_pci_close_device(struct vfio_device *core_vdev) +{ + vfio_pci_core_close_device(core_vdev); +} + +static int virtiovf_get_device_config_size(unsigned short device) +{ + switch (device) { + case 0x1041: + /* network card */ + return offsetofend(struct virtio_net_config, status); + default: + return 0; + } +} + +static int virtiovf_read_notify_info(struct virtiovf_pci_core_device *virtvdev) +{ + u64 offset; + int ret; + u8 bar; + + ret = virtiovf_cmd_lq_read_notify(virtvdev, + VIRTIO_ADMIN_CMD_NOTIFY_INFO_FLAGS_OWNER_MEM, + &bar, &offset); + if (ret) + return ret; + + virtvdev->notify_bar = bar; + virtvdev->notify_offset = offset; + return 0; +} + +static int virtiovf_pci_init_device(struct vfio_device *core_vdev) +{ + struct virtiovf_pci_core_device *virtvdev = container_of( + core_vdev, struct virtiovf_pci_core_device, core_device.vdev); + struct pci_dev *pdev; + int ret; + + ret = vfio_pci_core_init_dev(core_vdev); + if (ret) + return ret; + + pdev = virtvdev->core_device.pdev; + virtvdev->vf_id = pci_iov_vf_id(pdev); + if (virtvdev->vf_id < 0) + return -EINVAL; + + ret = virtiovf_read_notify_info(virtvdev); + if (ret) + return ret; + + virtvdev->bar0_virtual_buf_size = VIRTIO_LEGACY_IO_BAR_HEADER_LEN + + VIRTIO_LEGACY_IO_BAR_MSIX_HEADER_LEN + + virtiovf_get_device_config_size(pdev->device); + virtvdev->bar0_virtual_buf = kzalloc(virtvdev->bar0_virtual_buf_size, + GFP_KERNEL); + if (!virtvdev->bar0_virtual_buf) + return -ENOMEM; + mutex_init(&virtvdev->bar_mutex); + return 0; +} + +static void virtiovf_pci_core_release_dev(struct vfio_device *core_vdev) +{ + struct virtiovf_pci_core_device *virtvdev = container_of( + core_vdev, struct virtiovf_pci_core_device, core_device.vdev); + + kfree(virtvdev->bar0_virtual_buf); + vfio_pci_core_release_dev(core_vdev); +} + +static const struct vfio_device_ops virtiovf_acc_vfio_pci_tran_ops = { + .name = "virtio-transitional-vfio-pci", + .init = virtiovf_pci_init_device, + .release = virtiovf_pci_core_release_dev, + .open_device = virtiovf_pci_open_device, + .close_device = virtiovf_pci_close_device, + .ioctl = virtiovf_vfio_pci_core_ioctl, + .read = virtiovf_pci_core_read, + .write = virtiovf_pci_core_write, + .mmap = vfio_pci_core_mmap, + .request = vfio_pci_core_request, + .match = vfio_pci_core_match, + .bind_iommufd = vfio_iommufd_physical_bind, + .unbind_iommufd = vfio_iommufd_physical_unbind, + .attach_ioas = vfio_iommufd_physical_attach_ioas, +}; + +static const struct vfio_device_ops virtiovf_acc_vfio_pci_ops = { + .name = "virtio-acc-vfio-pci", + .init = vfio_pci_core_init_dev, + .release = vfio_pci_core_release_dev, + .open_device = virtiovf_pci_open_device, + .close_device = virtiovf_pci_close_device, + .ioctl = vfio_pci_core_ioctl, + .device_feature = vfio_pci_core_ioctl_feature, + .read = vfio_pci_core_read, + .write = vfio_pci_core_write, + .mmap = vfio_pci_core_mmap, + .request = vfio_pci_core_request, + .match = vfio_pci_core_match, + .bind_iommufd = vfio_iommufd_physical_bind, + .unbind_iommufd = vfio_iommufd_physical_unbind, + .attach_ioas = vfio_iommufd_physical_attach_ioas, +}; + +static bool virtiovf_bar0_exists(struct pci_dev *pdev) +{ + struct resource *res = pdev->resource; + + return res->flags ? true : false; +} + +#define VIRTIOVF_USE_ADMIN_CMD_BITMAP \ + (BIT_ULL(VIRTIO_ADMIN_CMD_LIST_QUERY) | \ + BIT_ULL(VIRTIO_ADMIN_CMD_LIST_USE) | \ + BIT_ULL(VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_WRITE) | \ + BIT_ULL(VIRTIO_ADMIN_CMD_LEGACY_COMMON_CFG_READ) | \ + BIT_ULL(VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_WRITE) | \ + BIT_ULL(VIRTIO_ADMIN_CMD_LEGACY_DEV_CFG_READ) | \ + BIT_ULL(VIRTIO_ADMIN_CMD_LEGACY_NOTIFY_INFO)) + +static bool virtiovf_support_legacy_access(struct pci_dev *pdev) +{ + int buf_size = DIV_ROUND_UP(VIRTIO_ADMIN_MAX_CMD_OPCODE, 64) * 8; + u8 *buf; + int ret; + + /* Only virtio-net is supported/tested so far */ + if (pdev->device != 0x1041) + return false; + + buf = kzalloc(buf_size, GFP_KERNEL); + if (!buf) + return false; + + ret = virtiovf_cmd_list_query(pdev, buf, buf_size); + if (ret) + goto end; + + if ((le64_to_cpup((__le64 *)buf) & VIRTIOVF_USE_ADMIN_CMD_BITMAP) != + VIRTIOVF_USE_ADMIN_CMD_BITMAP) { + ret = -EOPNOTSUPP; + goto end; + } + + /* confirm the used commands */ + memset(buf, 0, buf_size); + *(__le64 *)buf = cpu_to_le64(VIRTIOVF_USE_ADMIN_CMD_BITMAP); + ret = virtiovf_cmd_list_use(pdev, buf, buf_size); + +end: + kfree(buf); + return ret ? false : true; +} + +static int virtiovf_pci_probe(struct pci_dev *pdev, + const struct pci_device_id *id) +{ + const struct vfio_device_ops *ops = &virtiovf_acc_vfio_pci_ops; + struct virtiovf_pci_core_device *virtvdev; + int ret; + + if (pdev->is_virtfn && virtiovf_support_legacy_access(pdev) && + !virtiovf_bar0_exists(pdev) && pdev->msix_cap) + ops = &virtiovf_acc_vfio_pci_tran_ops; + + virtvdev = vfio_alloc_device(virtiovf_pci_core_device, core_device.vdev, + &pdev->dev, ops); + if (IS_ERR(virtvdev)) + return PTR_ERR(virtvdev); + + dev_set_drvdata(&pdev->dev, &virtvdev->core_device); + ret = vfio_pci_core_register_device(&virtvdev->core_device); + if (ret) + goto out; + return 0; +out: + vfio_put_device(&virtvdev->core_device.vdev); + return ret; +} + +static void virtiovf_pci_remove(struct pci_dev *pdev) +{ + struct virtiovf_pci_core_device *virtvdev = dev_get_drvdata(&pdev->dev); + + vfio_pci_core_unregister_device(&virtvdev->core_device); + vfio_put_device(&virtvdev->core_device.vdev); +} + +static const struct pci_device_id virtiovf_pci_table[] = { + { PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_VENDOR_ID_REDHAT_QUMRANET, PCI_ANY_ID) }, + {} +}; + +MODULE_DEVICE_TABLE(pci, virtiovf_pci_table); + +static struct pci_driver virtiovf_pci_driver = { + .name = KBUILD_MODNAME, + .id_table = virtiovf_pci_table, + .probe = virtiovf_pci_probe, + .remove = virtiovf_pci_remove, + .err_handler = &vfio_pci_core_err_handlers, + .driver_managed_dma = true, +}; + +module_pci_driver(virtiovf_pci_driver); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Yishai Hadas "); +MODULE_DESCRIPTION( + "VIRTIO VFIO PCI - User Level meta-driver for VIRTIO device family");