From nobody Mon Feb 9 04:39:18 2026 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 895FBC47090 for ; Tue, 6 Dec 2022 05:58:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232284AbiLFF6v (ORCPT ); Tue, 6 Dec 2022 00:58:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232399AbiLFF6n (ORCPT ); Tue, 6 Dec 2022 00:58:43 -0500 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6ABB226540; Mon, 5 Dec 2022 21:58:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1670306321; x=1701842321; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DUSArCj86hDjg2xcLGooiKx6M0pp7aVHzT7v3JaXbC4=; b=B3rS5+iHX+muKncdThZQBiUJQNFsAOofrJlOCSS9cI00IYyYWhGlsEcM 4rs862f5NXzuQY5laf24D82AXfJ5T019VcX8xfTSsSbZmy1gAlQcVBCHm 7EhIh247AG+HFCzRv5qViRElmYdLgtukepjwY9U7tAzsnYmGI6YQgT7sY 8It9e/BdY77IXPU1TO8Lb7YOgps9Fz/j/8h4i5qTtlGNiic8qrfW9giES sDeWgRPssitv47HC7GSJdl4uRTer/FvAvo7ruOOwZv6LPEw9hWQ8iakgy 6QkvQzUdDq1aGdM9SywClX/gUsnwOusUquP43MZLMLz+B/sGlXdevQDcQ g==; X-IronPort-AV: E=McAfee;i="6500,9779,10552"; a="378706658" X-IronPort-AV: E=Sophos;i="5.96,220,1665471600"; d="scan'208";a="378706658" Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Dec 2022 21:58:40 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10552"; a="648211382" X-IronPort-AV: E=Sophos;i="5.96,220,1665471600"; d="scan'208";a="648211382" Received: from leirao-pc.bj.intel.com ([10.238.156.101]) by fmsmga007.fm.intel.com with ESMTP; 05 Dec 2022 21:58:35 -0800 From: Lei Rao To: kbusch@kernel.org, axboe@fb.com, kch@nvidia.com, hch@lst.de, sagi@grimberg.me, alex.williamson@redhat.com, cohuck@redhat.com, jgg@ziepe.ca, yishaih@nvidia.com, shameerali.kolothum.thodi@huawei.com, kevin.tian@intel.com, mjrosato@linux.ibm.com, linux-kernel@vger.kernel.org, linux-nvme@lists.infradead.org, kvm@vger.kernel.org Cc: eddie.dong@intel.com, yadong.li@intel.com, yi.l.liu@intel.com, Konrad.wilk@oracle.com, stephen@eideticom.com, hang.yuan@intel.com, Lei Rao Subject: [RFC PATCH 2/5] nvme-vfio: add new vfio-pci driver for NVMe device Date: Tue, 6 Dec 2022 13:58:13 +0800 Message-Id: <20221206055816.292304-3-lei.rao@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221206055816.292304-1-lei.rao@intel.com> References: <20221206055816.292304-1-lei.rao@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" NVMe device has specific live migration implementation. Add an specific VFIO PCI driver for NVMe device. Its live migration support will be added in the subsequent patches. Signed-off-by: Lei Rao Signed-off-by: Yadong Li Signed-off-by: Chaitanya Kulkarni Reviewed-by: Eddie Dong Reviewed-by: Hang Yuan --- drivers/vfio/pci/Kconfig | 2 + drivers/vfio/pci/Makefile | 2 + drivers/vfio/pci/nvme/Kconfig | 9 ++++ drivers/vfio/pci/nvme/Makefile | 3 ++ drivers/vfio/pci/nvme/nvme.c | 99 ++++++++++++++++++++++++++++++++++ 5 files changed, 115 insertions(+) create mode 100644 drivers/vfio/pci/nvme/Kconfig create mode 100644 drivers/vfio/pci/nvme/Makefile create mode 100644 drivers/vfio/pci/nvme/nvme.c diff --git a/drivers/vfio/pci/Kconfig b/drivers/vfio/pci/Kconfig index f9d0c908e738..fcd45144d3e3 100644 --- a/drivers/vfio/pci/Kconfig +++ b/drivers/vfio/pci/Kconfig @@ -59,4 +59,6 @@ source "drivers/vfio/pci/mlx5/Kconfig" =20 source "drivers/vfio/pci/hisilicon/Kconfig" =20 +source "drivers/vfio/pci/nvme/Kconfig" + endif diff --git a/drivers/vfio/pci/Makefile b/drivers/vfio/pci/Makefile index 24c524224da5..eddc8e889726 100644 --- a/drivers/vfio/pci/Makefile +++ b/drivers/vfio/pci/Makefile @@ -11,3 +11,5 @@ obj-$(CONFIG_VFIO_PCI) +=3D vfio-pci.o obj-$(CONFIG_MLX5_VFIO_PCI) +=3D mlx5/ =20 obj-$(CONFIG_HISI_ACC_VFIO_PCI) +=3D hisilicon/ + +obj-$(CONFIG_NVME_VFIO_PCI) +=3D nvme/ diff --git a/drivers/vfio/pci/nvme/Kconfig b/drivers/vfio/pci/nvme/Kconfig new file mode 100644 index 000000000000..c281fe154007 --- /dev/null +++ b/drivers/vfio/pci/nvme/Kconfig @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: GPL-2.0-only +config NVME_VFIO_PCI + tristate "VFIO support for NVMe PCI devices" + depends on VFIO_PCI_CORE + help + This provides generic VFIO PCI support for NVMe device + using the VFIO framework. + + If you don't know what to do here, say N. diff --git a/drivers/vfio/pci/nvme/Makefile b/drivers/vfio/pci/nvme/Makefile new file mode 100644 index 000000000000..2f4a0ad3d9cf --- /dev/null +++ b/drivers/vfio/pci/nvme/Makefile @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0-only +obj-$(CONFIG_NVME_VFIO_PCI) +=3D nvme-vfio-pci.o +nvme-vfio-pci-y :=3D nvme.o diff --git a/drivers/vfio/pci/nvme/nvme.c b/drivers/vfio/pci/nvme/nvme.c new file mode 100644 index 000000000000..f1386d8a9287 --- /dev/null +++ b/drivers/vfio/pci/nvme/nvme.c @@ -0,0 +1,99 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (c) 2022, INTEL CORPORATION. All rights reserved + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static int nvmevf_pci_open_device(struct vfio_device *core_vdev) +{ + struct vfio_pci_core_device *vdev =3D + container_of(core_vdev, struct vfio_pci_core_device, vdev); + int ret; + + ret =3D vfio_pci_core_enable(vdev); + if (ret) + return ret; + + vfio_pci_core_finish_enable(vdev); + return 0; +} + +static const struct vfio_device_ops nvmevf_pci_ops =3D { + .name =3D "nvme-vfio-pci", + .init =3D vfio_pci_core_init_dev, + .release =3D vfio_pci_core_release_dev, + .open_device =3D nvmevf_pci_open_device, + .close_device =3D vfio_pci_core_close_device, + .ioctl =3D vfio_pci_core_ioctl, + .device_feature =3D vfio_pci_core_ioctl_feature, + .read =3D vfio_pci_core_read, + .write =3D vfio_pci_core_write, + .mmap =3D vfio_pci_core_mmap, + .request =3D vfio_pci_core_request, + .match =3D vfio_pci_core_match, +}; + +static int nvmevf_pci_probe(struct pci_dev *pdev, const struct pci_device_= id *id) +{ + struct vfio_pci_core_device *vdev; + int ret; + + vdev =3D vfio_alloc_device(vfio_pci_core_device, vdev, &pdev->dev, + &nvmevf_pci_ops); + if (IS_ERR(vdev)) + return PTR_ERR(vdev); + + dev_set_drvdata(&pdev->dev, vdev); + ret =3D vfio_pci_core_register_device(vdev); + if (ret) + goto out_put_dev; + + return 0; + +out_put_dev: + vfio_put_device(&vdev->vdev); + return ret; +} + +static void nvmevf_pci_remove(struct pci_dev *pdev) +{ + struct vfio_pci_core_device *vdev =3D dev_get_drvdata(&pdev->dev); + + vfio_pci_core_unregister_device(vdev); + vfio_put_device(&vdev->vdev); +} + +static const struct pci_device_id nvmevf_pci_table[] =3D { + /* Intel IPU NVMe Virtual Function */ + { PCI_DRIVER_OVERRIDE_DEVICE_VFIO(PCI_VENDOR_ID_INTEL, 0x1457) }, + {} +}; + +MODULE_DEVICE_TABLE(pci, nvmevf_pci_table); + +static struct pci_driver nvmevf_pci_driver =3D { + .name =3D KBUILD_MODNAME, + .id_table =3D nvmevf_pci_table, + .probe =3D nvmevf_pci_probe, + .remove =3D nvmevf_pci_remove, + .err_handler =3D &vfio_pci_core_err_handlers, + .driver_managed_dma =3D true, +}; + +module_pci_driver(nvmevf_pci_driver); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Lei Rao "); +MODULE_DESCRIPTION("NVMe VFIO PCI - Generic VFIO PCI driver for NVMe"); --=20 2.34.1