From: Shunsuke Mie <mie@igel.co.jp>
To: Lorenzo Pieralisi <lpieralisi@kernel.org>
Cc: "Krzysztof Wilczyński" <kw@linux.com>,
"Manivannan Sadhasivam" <mani@kernel.org>,
"Kishon Vijay Abraham I" <kishon@kernel.org>,
"Bjorn Helgaas" <bhelgaas@google.com>,
"Michael S. Tsirkin" <mst@redhat.com>,
"Jason Wang" <jasowang@redhat.com>,
"Shunsuke Mie" <mie@igel.co.jp>, "Frank Li" <Frank.Li@nxp.com>,
"Jon Mason" <jdmason@kudzu.us>,
"Ren Zhijie" <renzhijie2@huawei.com>,
"Takanari Hayama" <taki@igel.co.jp>,
linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org,
virtualization@lists.linux-foundation.org
Subject: [RFC PATCH 3/4] PCI: endpoint: Introduce virtio library for EP functions
Date: Fri, 3 Feb 2023 19:04:17 +0900 [thread overview]
Message-ID: <20230203100418.2981144-4-mie@igel.co.jp> (raw)
In-Reply-To: <20230203100418.2981144-1-mie@igel.co.jp>
Add a new library to access a virtio ring located on PCIe host memory. The
library generates struct pci_epf_vringh that is introduced in this patch.
The struct has a vringh member, so vringh APIs can be used to access the
virtio ring.
Signed-off-by: Shunsuke Mie <mie@igel.co.jp>
Signed-off-by: Takanari Hayama <taki@igel.co.jp>
---
drivers/pci/endpoint/Kconfig | 7 ++
drivers/pci/endpoint/Makefile | 1 +
drivers/pci/endpoint/pci-epf-virtio.c | 113 ++++++++++++++++++++++++++
include/linux/pci-epf-virtio.h | 25 ++++++
4 files changed, 146 insertions(+)
create mode 100644 drivers/pci/endpoint/pci-epf-virtio.c
create mode 100644 include/linux/pci-epf-virtio.h
diff --git a/drivers/pci/endpoint/Kconfig b/drivers/pci/endpoint/Kconfig
index 17bbdc9bbde0..07276dcc43c8 100644
--- a/drivers/pci/endpoint/Kconfig
+++ b/drivers/pci/endpoint/Kconfig
@@ -28,6 +28,13 @@ config PCI_ENDPOINT_CONFIGFS
configure the endpoint function and used to bind the
function with a endpoint controller.
+config PCI_ENDPOINT_VIRTIO
+ tristate
+ depends on PCI_ENDPOINT
+ select VHOST_IOMEM
+ help
+ TODO update this comment
+
source "drivers/pci/endpoint/functions/Kconfig"
endmenu
diff --git a/drivers/pci/endpoint/Makefile b/drivers/pci/endpoint/Makefile
index 95b2fe47e3b0..95712f0a13d1 100644
--- a/drivers/pci/endpoint/Makefile
+++ b/drivers/pci/endpoint/Makefile
@@ -4,5 +4,6 @@
#
obj-$(CONFIG_PCI_ENDPOINT_CONFIGFS) += pci-ep-cfs.o
+obj-$(CONFIG_PCI_ENDPOINT_VIRTIO) += pci-epf-virtio.o
obj-$(CONFIG_PCI_ENDPOINT) += pci-epc-core.o pci-epf-core.o\
pci-epc-mem.o functions/
diff --git a/drivers/pci/endpoint/pci-epf-virtio.c b/drivers/pci/endpoint/pci-epf-virtio.c
new file mode 100644
index 000000000000..7134ca407a03
--- /dev/null
+++ b/drivers/pci/endpoint/pci-epf-virtio.c
@@ -0,0 +1,113 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Virtio library for PCI Endpoint function
+ */
+#include <linux/kernel.h>
+#include <linux/pci-epf-virtio.h>
+#include <linux/pci-epc.h>
+#include <linux/virtio_pci.h>
+
+static void __iomem *epf_virtio_map_vq(struct pci_epf *epf, u32 pfn,
+ size_t size, phys_addr_t *vq_phys)
+{
+ int err;
+ phys_addr_t vq_addr;
+ size_t vq_size;
+ void __iomem *vq_virt;
+
+ vq_addr = (phys_addr_t)pfn << VIRTIO_PCI_QUEUE_ADDR_SHIFT;
+
+ vq_size = vring_size(size, VIRTIO_PCI_VRING_ALIGN) + 100;
+
+ vq_virt = pci_epc_mem_alloc_addr(epf->epc, vq_phys, vq_size);
+ if (!vq_virt) {
+ pr_err("Failed to allocate epc memory\n");
+ return ERR_PTR(-ENOMEM);
+ }
+
+ err = pci_epc_map_addr(epf->epc, epf->func_no, epf->vfunc_no, *vq_phys,
+ vq_addr, vq_size);
+ if (err) {
+ pr_err("Failed to map virtuqueue to local");
+ goto err_free;
+ }
+
+ return vq_virt;
+
+err_free:
+ pci_epc_mem_free_addr(epf->epc, *vq_phys, vq_virt, vq_size);
+
+ return ERR_PTR(err);
+}
+
+static void epf_virtio_unmap_vq(struct pci_epf *epf, void __iomem *vq_virt,
+ phys_addr_t vq_phys, size_t size)
+{
+ pci_epc_unmap_addr(epf->epc, epf->func_no, epf->vfunc_no, vq_phys);
+ pci_epc_mem_free_addr(epf->epc, vq_phys, vq_virt,
+ vring_size(size, VIRTIO_PCI_VRING_ALIGN));
+}
+
+/**
+ * pci_epf_virtio_alloc_vringh() - allocate epf vringh from @pfn
+ * @epf: the EPF device that communicates to host virtio dirver
+ * @features: the virtio features of device
+ * @pfn: page frame number of virtqueue located on host memory. It is
+ * passed during virtqueue negotiation.
+ * @size: a length of virtqueue
+ */
+struct pci_epf_vringh *pci_epf_virtio_alloc_vringh(struct pci_epf *epf,
+ u64 features, u32 pfn,
+ size_t size)
+{
+ int err;
+ struct vring vring;
+ struct pci_epf_vringh *evrh;
+
+ evrh = kmalloc(sizeof(*evrh), GFP_KERNEL);
+ if (!evrh) {
+ err = -ENOMEM;
+ goto err_unmap_vq;
+ }
+
+ evrh->size = size;
+
+ evrh->virt = epf_virtio_map_vq(epf, pfn, size, &evrh->phys);
+ if (IS_ERR(evrh->virt))
+ return evrh->virt;
+
+ vring_init(&vring, size, evrh->virt, VIRTIO_PCI_VRING_ALIGN);
+
+ err = vringh_init_iomem(&evrh->vrh, features, size, false, GFP_KERNEL,
+ vring.desc, vring.avail, vring.used);
+ if (err)
+ goto err_free_epf_vq;
+
+ return evrh;
+
+err_free_epf_vq:
+ kfree(evrh);
+
+err_unmap_vq:
+ epf_virtio_unmap_vq(epf, evrh->virt, evrh->phys, evrh->size);
+
+ return ERR_PTR(err);
+}
+EXPORT_SYMBOL_GPL(pci_epf_virtio_alloc_vringh);
+
+/**
+ * pci_epf_virtio_free_vringh() - release allocated epf vring
+ * @epf: the EPF device that communicates to host virtio dirver
+ * @evrh: epf vringh to free
+ */
+void pci_epf_virtio_free_vringh(struct pci_epf *epf,
+ struct pci_epf_vringh *evrh)
+{
+ epf_virtio_unmap_vq(epf, evrh->virt, evrh->phys, evrh->size);
+ kfree(evrh);
+}
+EXPORT_SYMBOL_GPL(pci_epf_virtio_free_vringh);
+
+MODULE_DESCRIPTION("PCI EP Virtio Library");
+MODULE_AUTHOR("Shunsuke Mie <mie@igel.co.jp>");
+MODULE_LICENSE("GPL");
diff --git a/include/linux/pci-epf-virtio.h b/include/linux/pci-epf-virtio.h
new file mode 100644
index 000000000000..ae09087919a9
--- /dev/null
+++ b/include/linux/pci-epf-virtio.h
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * PCI Endpoint Function (EPF) for virtio definitions
+ */
+#ifndef __LINUX_PCI_EPF_VIRTIO_H
+#define __LINUX_PCI_EPF_VIRTIO_H
+
+#include <linux/types.h>
+#include <linux/vringh.h>
+#include <linux/pci-epf.h>
+
+struct pci_epf_vringh {
+ struct vringh vrh;
+ void __iomem *virt;
+ phys_addr_t phys;
+ size_t size;
+};
+
+struct pci_epf_vringh *pci_epf_virtio_alloc_vringh(struct pci_epf *epf,
+ u64 features, u32 pfn,
+ size_t size);
+void pci_epf_virtio_free_vringh(struct pci_epf *epf,
+ struct pci_epf_vringh *evrh);
+
+#endif // __LINUX_PCI_EPF_VIRTIO_H
--
2.25.1
next prev parent reply other threads:[~2023-02-03 10:05 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-03 10:04 [RFC PATCH 0/4] PCI: endpoint: Introduce a virtio-net EP function Shunsuke Mie
2023-02-03 10:04 ` [RFC PATCH 1/4] virtio_pci: add a definition of queue flag in ISR Shunsuke Mie
2023-02-03 10:16 ` Michael S. Tsirkin
2023-02-07 10:06 ` Shunsuke Mie
2023-02-03 10:04 ` [RFC PATCH 2/4] virtio_ring: remove const from vring getter Shunsuke Mie
2023-02-03 10:04 ` Shunsuke Mie [this message]
2023-02-03 10:20 ` [RFC PATCH 3/4] PCI: endpoint: Introduce virtio library for EP functions Michael S. Tsirkin
2023-02-07 11:05 ` Shunsuke Mie
2023-02-03 10:04 ` [RFC PATCH 4/4] PCI: endpoint: function: Add EP function driver to provide virtio net device Shunsuke Mie
2023-02-03 10:22 ` Michael S. Tsirkin
2023-02-03 22:15 ` [EXT] " Frank Li
2023-02-07 10:56 ` Shunsuke Mie
2023-02-07 15:37 ` Frank Li
2023-02-08 5:46 ` Shunsuke Mie
2023-02-07 10:47 ` Shunsuke Mie
2023-02-03 16:45 ` [EXT] [RFC PATCH 0/4] PCI: endpoint: Introduce a virtio-net EP function Frank Li
2023-02-07 10:29 ` Shunsuke Mie
2023-02-07 16:02 ` Frank Li
2023-02-14 3:27 ` Shunsuke Mie
2023-03-29 16:46 ` Frank Li
2023-04-05 1:22 ` Shunsuke Mie
2023-04-11 10:22 ` Shunsuke Mie
2023-02-03 21:48 ` Frank Li
2023-02-07 1:43 ` Shunsuke Mie
2023-02-07 3:27 ` Shunsuke Mie
2023-02-05 10:01 ` Michael S. Tsirkin
2023-02-07 10:17 ` Shunsuke Mie
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230203100418.2981144-4-mie@igel.co.jp \
--to=mie@igel.co.jp \
--cc=Frank.Li@nxp.com \
--cc=bhelgaas@google.com \
--cc=jasowang@redhat.com \
--cc=jdmason@kudzu.us \
--cc=kishon@kernel.org \
--cc=kw@linux.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=lpieralisi@kernel.org \
--cc=mani@kernel.org \
--cc=mst@redhat.com \
--cc=renzhijie2@huawei.com \
--cc=taki@igel.co.jp \
--cc=virtualization@lists.linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®