Hi all:
There are hardwares that can do virtio datapath offloading while
having its own control path. This path tries to implement a mdev based
unified API to support using kernel virtio driver to drive those
devices. This is done by introducing a new mdev transport for virtio
(virtio_mdev) and register itself as a new kind of mdev driver. Then
it provides a unified way for kernel virtio driver to talk with mdev
device implementation.
Though the series only contains kernel driver support, the goal is to
make the transport generic enough to support userspace drivers. This
means vhost-mdev[1] could be built on top as well by resuing the
transport.
A sample driver is also implemented which simulate a virito-net
loopback ethernet device on top of vringh + workqueue. This could be
used as a reference implementation for real hardware driver.
Also a real IFC VF driver was also posted here[2] which is a good
reference for vendors who is interested in their own virtio datapath
offloading product.
Consider mdev framework only support VFIO device and driver right now,
this series also extend it to support other types. This is done
through introducing class id to the device and pairing it with
id_talbe claimed by the driver. On top, this seris also decouple
device specific ops out of the common ones for implementing class
specific operations over mdev bus.
Pktgen test was done with virito-net + mvnet loop back device.
Please review.
[1] https://lkml.org/lkml/2019/11/5/424
[2] https://lkml.org/lkml/2019/11/5/227
Changes from V10:
- rename mvnet to mvnet_loopback
- fix typo in the help text for sample Kconfig
Changes from V9:
- Tweak the help text for virito-mdev kconfig
Changes from V8:
- try silent checkpatch, some are still there becuase they were inherited
from virtio_config_ops which needs to be resolved in an independent series
- tweak on the comment and doc
- remove VIRTIO_MDEV_F_VERSION_1 completely
- rename CONFIG_VIRTIO_MDEV_DEVICE to CONFIG_VIRTIO_MDEV
Changes from V7:
- drop {set|get}_mdev_features for virtio
- typo and comment style fixes
Changes from V6:
- rename ops files and compile guard
Changes from V5:
- use dev_warn() instead of WARN(1) when class id is not set
- validate id_table before trying to do matching between device and
driver
- add wildcard for modpost script
- use unique name for id_table
- move get_mdev_features() to be the first member of virtio_device_ops
and more comments for it
- typo fixes for the comments above virtio_mdev_ops
Changes from V4:
- keep mdev_set_class() for the device that doesn't use device ops
- use union for device ops pointer in mdev_device
- introduce class specific helper for getting is device ops
- use WARN_ON instead of BUG_ON in mdev_set_virtio_ops
- explain details of get_mdev_features() and get_vendor_id()
- distinguish the optional virito device ops from mandatory ones and
make get_generation() optional
- rename vfio_mdev.h to vfio_mdev_ops.h, rename virito_mdev.h to
virtio_mdev_ops.h
- don't abuse version fileds in virtio_mdev structure, use features
instead
- fix warning during device remove
- style & docs tweaks and typo fixes
Changes from V3:
- document that class id (device ops) must be specified in create()
- add WARN() when trying to set class_id when it has already set
- add WARN() when class_id is not specified in create() and correctly
return an error in this case
- correct the prototype of mdev_set_class() in the doc
- add documention of mdev_set_class()
- remove the unnecessary "class_id_fail" label when class id is not
specified in create()
- convert id_table in vfio_mdev to const
- move mdev_set_class and its friends after mdev_uuid()
- suqash the patch of bus uevent into patch of introducing class id
- tweak the words in the docs per Cornelia suggestion
- tie class_id and device ops through class specific initialization
routine like mdev_set_vfio_ops()
- typos fixes in the docs of virtio-mdev callbacks
- document the usage of virtqueues in struct virtio_mdev_device
- remove the useless vqs array in struct virtio_mdev_device
- rename MDEV_ID_XXX to MDEV_CLASS_ID_XXX
Changes from V2:
- fail when class_id is not specified
- drop the vringh patch
- match the doc to the code
- tweak the commit log
- move device_ops from parent to mdev device
- remove the unused MDEV_ID_VHOST
Changes from V1:
- move virtio_mdev.c to drivers/virtio
- store class_id in mdev_device instead of mdev_parent
- store device_ops in mdev_device instead of mdev_parent
- reorder the patch, vringh fix comes first
- really silent compiling warnings
- really switch to use u16 for class_id
- uevent and modpost support for mdev class_id
- vraious tweaks per comments from Parav
Changes from RFC-V2:
- silent compile warnings on some specific configuration
- use u16 instead u8 for class id
- reseve MDEV_ID_VHOST for future vhost-mdev work
- introduce "virtio" type for mvnet and make "vhost" type for future
work
- add entries in MAINTAINER
- tweak and typos fixes in commit log
Changes from RFC-V1:
- rename device id to class id
- add docs for class id and device specific ops (device_ops)
- split device_ops into seperate headers
- drop the mdev_set_dma_ops()
- use device_ops to implement the transport API, then it's not a part
of UAPI any more
- use GFP_ATOMIC in mvnet sample device and other tweaks
- set_vring_base/get_vring_base support for mvnet device
Jason Wang (6):
mdev: class id support
modpost: add support for mdev class id
mdev: introduce device specific ops
mdev: introduce virtio device and its device ops
virtio: introduce a mdev based transport
docs: sample driver to demonstrate how to implement virtio-mdev
framework
.../driver-api/vfio-mediated-device.rst | 38 +-
MAINTAINERS | 3 +
drivers/gpu/drm/i915/gvt/kvmgt.c | 17 +-
drivers/s390/cio/vfio_ccw_ops.c | 17 +-
drivers/s390/crypto/vfio_ap_ops.c | 13 +-
drivers/vfio/mdev/mdev_core.c | 60 ++
drivers/vfio/mdev/mdev_driver.c | 25 +
drivers/vfio/mdev/mdev_private.h | 8 +
drivers/vfio/mdev/vfio_mdev.c | 45 +-
drivers/virtio/Kconfig | 13 +
drivers/virtio/Makefile | 1 +
drivers/virtio/virtio_mdev.c | 406 +++++++++++
include/linux/mdev.h | 57 +-
include/linux/mdev_vfio_ops.h | 52 ++
include/linux/mdev_virtio_ops.h | 147 ++++
include/linux/mod_devicetable.h | 8 +
samples/Kconfig | 10 +
samples/vfio-mdev/Makefile | 1 +
samples/vfio-mdev/mbochs.c | 19 +-
samples/vfio-mdev/mdpy.c | 19 +-
samples/vfio-mdev/mtty.c | 17 +-
samples/vfio-mdev/mvnet_loopback.c | 687 ++++++++++++++++++
scripts/mod/devicetable-offsets.c | 3 +
scripts/mod/file2alias.c | 11 +
24 files changed, 1586 insertions(+), 91 deletions(-)
create mode 100644 drivers/virtio/virtio_mdev.c
create mode 100644 include/linux/mdev_vfio_ops.h
create mode 100644 include/linux/mdev_virtio_ops.h
create mode 100644 samples/vfio-mdev/mvnet_loopback.c
--
2.19.1
This patch implements basic support for mdev driver that supports
virtio transport for kernel virtio driver.
Reviewed-by: Cornelia Huck <[email protected]>
Signed-off-by: Jason Wang <[email protected]>
---
MAINTAINERS | 1 +
drivers/vfio/mdev/mdev_core.c | 21 +++++
drivers/vfio/mdev/mdev_private.h | 2 +
include/linux/mdev.h | 6 ++
include/linux/mdev_virtio_ops.h | 147 +++++++++++++++++++++++++++++++
5 files changed, 177 insertions(+)
create mode 100644 include/linux/mdev_virtio_ops.h
diff --git a/MAINTAINERS b/MAINTAINERS
index f661d13344d6..4997957443df 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -17248,6 +17248,7 @@ F: include/linux/virtio*.h
F: include/uapi/linux/virtio_*.h
F: drivers/crypto/virtio/
F: mm/balloon_compaction.c
+F: include/linux/mdev_virtio_ops.h
VIRTIO BLOCK AND SCSI DRIVERS
M: "Michael S. Tsirkin" <[email protected]>
diff --git a/drivers/vfio/mdev/mdev_core.c b/drivers/vfio/mdev/mdev_core.c
index 4e70f19ac145..c58253404ed5 100644
--- a/drivers/vfio/mdev/mdev_core.c
+++ b/drivers/vfio/mdev/mdev_core.c
@@ -78,6 +78,27 @@ const struct mdev_vfio_device_ops *mdev_get_vfio_ops(struct mdev_device *mdev)
}
EXPORT_SYMBOL(mdev_get_vfio_ops);
+/*
+ * Specify the virtio device ops for the mdev device, this
+ * must be called during create() callback for virtio mdev device.
+ */
+void mdev_set_virtio_ops(struct mdev_device *mdev,
+ const struct mdev_virtio_device_ops *virtio_ops)
+{
+ mdev_set_class(mdev, MDEV_CLASS_ID_VIRTIO);
+ mdev->virtio_ops = virtio_ops;
+}
+EXPORT_SYMBOL(mdev_set_virtio_ops);
+
+/* Get the virtio device ops for the mdev device. */
+const struct mdev_virtio_device_ops *
+mdev_get_virtio_ops(struct mdev_device *mdev)
+{
+ WARN_ON(mdev->class_id != MDEV_CLASS_ID_VIRTIO);
+ return mdev->virtio_ops;
+}
+EXPORT_SYMBOL(mdev_get_virtio_ops);
+
struct device *mdev_dev(struct mdev_device *mdev)
{
return &mdev->dev;
diff --git a/drivers/vfio/mdev/mdev_private.h b/drivers/vfio/mdev/mdev_private.h
index 411227373625..2c74dd032409 100644
--- a/drivers/vfio/mdev/mdev_private.h
+++ b/drivers/vfio/mdev/mdev_private.h
@@ -11,6 +11,7 @@
#define MDEV_PRIVATE_H
#include <linux/mdev_vfio_ops.h>
+#include <linux/mdev_virtio_ops.h>
int mdev_bus_register(void);
void mdev_bus_unregister(void);
@@ -38,6 +39,7 @@ struct mdev_device {
u16 class_id;
union {
const struct mdev_vfio_device_ops *vfio_ops;
+ const struct mdev_virtio_device_ops *virtio_ops;
};
};
diff --git a/include/linux/mdev.h b/include/linux/mdev.h
index 9e37506d1987..f3d75a60c2b5 100644
--- a/include/linux/mdev.h
+++ b/include/linux/mdev.h
@@ -17,6 +17,7 @@
struct mdev_device;
struct mdev_vfio_device_ops;
+struct mdev_virtio_device_ops;
/*
* Called by the parent device driver to set the device which represents
@@ -112,6 +113,10 @@ void mdev_set_class(struct mdev_device *mdev, u16 id);
void mdev_set_vfio_ops(struct mdev_device *mdev,
const struct mdev_vfio_device_ops *vfio_ops);
const struct mdev_vfio_device_ops *mdev_get_vfio_ops(struct mdev_device *mdev);
+void mdev_set_virtio_ops(struct mdev_device *mdev,
+ const struct mdev_virtio_device_ops *virtio_ops);
+const struct mdev_virtio_device_ops *
+mdev_get_virtio_ops(struct mdev_device *mdev);
extern struct bus_type mdev_bus_type;
@@ -127,6 +132,7 @@ struct mdev_device *mdev_from_dev(struct device *dev);
enum {
MDEV_CLASS_ID_VFIO = 1,
+ MDEV_CLASS_ID_VIRTIO = 2,
/* New entries must be added here */
};
diff --git a/include/linux/mdev_virtio_ops.h b/include/linux/mdev_virtio_ops.h
new file mode 100644
index 000000000000..8951331c6629
--- /dev/null
+++ b/include/linux/mdev_virtio_ops.h
@@ -0,0 +1,147 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Virtio mediated device driver
+ *
+ * Copyright 2019, Red Hat Corp.
+ * Author: Jason Wang <[email protected]>
+ */
+#ifndef MDEV_VIRTIO_OPS_H
+#define MDEV_VIRTIO_OPS_H
+
+#include <linux/interrupt.h>
+#include <linux/mdev.h>
+#include <uapi/linux/vhost.h>
+
+#define VIRTIO_MDEV_DEVICE_API_STRING "virtio-mdev"
+
+struct virtio_mdev_callback {
+ irqreturn_t (*callback)(void *data);
+ void *private;
+};
+
+/**
+ * struct mdev_virtio_device_ops - Structure to be registered for each
+ * mdev device to register the device for virtio/vhost drivers.
+ *
+ * The callbacks are mandatory unless explicitly mentioned.
+ *
+ * @set_vq_address: Set the address of virtqueue
+ * @mdev: mediated device
+ * @idx: virtqueue index
+ * @desc_area: address of desc area
+ * @driver_area: address of driver area
+ * @device_area: address of device area
+ * Returns integer: success (0) or error (< 0)
+ * @set_vq_num: Set the size of virtqueue
+ * @mdev: mediated device
+ * @idx: virtqueue index
+ * @num: the size of virtqueue
+ * @kick_vq: Kick the virtqueue
+ * @mdev: mediated device
+ * @idx: virtqueue index
+ * @set_vq_cb: Set the interrupt callback function for
+ * a virtqueue
+ * @mdev: mediated device
+ * @idx: virtqueue index
+ * @cb: virtio-mdev interrupt callback structure
+ * @set_vq_ready: Set ready status for a virtqueue
+ * @mdev: mediated device
+ * @idx: virtqueue index
+ * @ready: ready (true) not ready(false)
+ * @get_vq_ready: Get ready status for a virtqueue
+ * @mdev: mediated device
+ * @idx: virtqueue index
+ * Returns boolean: ready (true) or not (false)
+ * @set_vq_state: Set the state for a virtqueue
+ * @mdev: mediated device
+ * @idx: virtqueue index
+ * @state: virtqueue state (last_avail_idx)
+ * Returns integer: success (0) or error (< 0)
+ * @get_vq_state: Get the state for a virtqueue
+ * @mdev: mediated device
+ * @idx: virtqueue index
+ * Returns virtqueue state (last_avail_idx)
+ * @get_vq_align: Get the virtqueue align requirement
+ * for the device
+ * @mdev: mediated device
+ * Returns virtqueue algin requirement
+ * @get_features: Get virtio features supported by the device
+ * @mdev: mediated device
+ * Returns the virtio features support by the
+ * device
+ * @set_features: Set virtio features supported by the driver
+ * @mdev: mediated device
+ * @features: feature support by the driver
+ * Returns integer: success (0) or error (< 0)
+ * @set_config_cb: Set the config interrupt callback
+ * @mdev: mediated device
+ * @cb: virtio-mdev interrupt callback structure
+ * @get_vq_num_max: Get the max size of virtqueue
+ * @mdev: mediated device
+ * Returns u16: max size of virtqueue
+ * @get_device_id: Get virtio device id
+ * @mdev: mediated device
+ * Returns u32: virtio device id
+ * @get_vendor_id: Get id for the vendor that provides this device
+ * @mdev: mediated device
+ * Returns u32: virtio vendor id
+ * @get_status: Get the device status
+ * @mdev: mediated device
+ * Returns u8: virtio device status
+ * @set_status: Set the device status
+ * @mdev: mediated device
+ * @status: virtio device status
+ * @get_config: Read from device specific configuration space
+ * @mdev: mediated device
+ * @offset: offset from the beginning of
+ * configuration space
+ * @buf: buffer used to read to
+ * @len: the length to read from
+ * configration space
+ * @set_config: Write to device specific configuration space
+ * @mdev: mediated device
+ * @offset: offset from the beginning of
+ * configuration space
+ * @buf: buffer used to write from
+ * @len: the length to write to
+ * configration space
+ * @get_generation: Get device config generaton (optional)
+ * @mdev: mediated device
+ * Returns u32: device generation
+ */
+struct mdev_virtio_device_ops {
+ /* Virtqueue ops */
+ int (*set_vq_address)(struct mdev_device *mdev,
+ u16 idx, u64 desc_area, u64 driver_area,
+ u64 device_area);
+ void (*set_vq_num)(struct mdev_device *mdev, u16 idx, u32 num);
+ void (*kick_vq)(struct mdev_device *mdev, u16 idx);
+ void (*set_vq_cb)(struct mdev_device *mdev, u16 idx,
+ struct virtio_mdev_callback *cb);
+ void (*set_vq_ready)(struct mdev_device *mdev, u16 idx, bool ready);
+ bool (*get_vq_ready)(struct mdev_device *mdev, u16 idx);
+ int (*set_vq_state)(struct mdev_device *mdev, u16 idx, u64 state);
+ u64 (*get_vq_state)(struct mdev_device *mdev, u16 idx);
+
+ /* Virtio device ops */
+ u16 (*get_vq_align)(struct mdev_device *mdev);
+ u64 (*get_features)(struct mdev_device *mdev);
+ int (*set_features)(struct mdev_device *mdev, u64 features);
+ void (*set_config_cb)(struct mdev_device *mdev,
+ struct virtio_mdev_callback *cb);
+ u16 (*get_vq_num_max)(struct mdev_device *mdev);
+ u32 (*get_device_id)(struct mdev_device *mdev);
+ u32 (*get_vendor_id)(struct mdev_device *mdev);
+ u8 (*get_status)(struct mdev_device *mdev);
+ void (*set_status)(struct mdev_device *mdev, u8 status);
+ void (*get_config)(struct mdev_device *mdev, unsigned int offset,
+ void *buf, unsigned int len);
+ void (*set_config)(struct mdev_device *mdev, unsigned int offset,
+ const void *buf, unsigned int len);
+ u32 (*get_generation)(struct mdev_device *mdev);
+};
+
+void mdev_set_virtio_ops(struct mdev_device *mdev,
+ const struct mdev_virtio_device_ops *virtio_ops);
+
+#endif
--
2.19.1
This sample driver creates mdev device that simulate virtio net device
over virtio mdev transport. The device is implemented through vringh
and workqueue. A device specific dma ops is to make sure HVA is used
directly as the IOVA. This should be sufficient for kernel virtio
driver to work.
Only 'virtio' type is supported right now. I plan to add 'vhost' type
on top which requires some virtual IOMMU implemented in this sample
driver.
Signed-off-by: Jason Wang <[email protected]>
---
MAINTAINERS | 1 +
samples/Kconfig | 10 +
samples/vfio-mdev/Makefile | 1 +
samples/vfio-mdev/mvnet_loopback.c | 687 +++++++++++++++++++++++++++++
4 files changed, 699 insertions(+)
create mode 100644 samples/vfio-mdev/mvnet_loopback.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 4997957443df..62c3eafa99b0 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -17249,6 +17249,7 @@ F: include/uapi/linux/virtio_*.h
F: drivers/crypto/virtio/
F: mm/balloon_compaction.c
F: include/linux/mdev_virtio_ops.h
+F: samples/vfio-mdev/mvnet_loopback.c
VIRTIO BLOCK AND SCSI DRIVERS
M: "Michael S. Tsirkin" <[email protected]>
diff --git a/samples/Kconfig b/samples/Kconfig
index c8dacb4dda80..5b347e90fd20 100644
--- a/samples/Kconfig
+++ b/samples/Kconfig
@@ -131,6 +131,16 @@ config SAMPLE_VFIO_MDEV_MDPY
mediated device. It is a simple framebuffer and supports
the region display interface (VFIO_GFX_PLANE_TYPE_REGION).
+config SAMPLE_VIRTIO_MDEV_NET_LOOPBACK
+ tristate "Build loopback VIRTIO net example mediated device sample code -- loadable modules only"
+ depends on VIRTIO_MDEV && VHOST_RING && m
+ help
+ Build a networking sample device for use as a virtio
+ mediated device. The device cooperates with virtio-mdev bus
+ driver to present an virtio ethernet driver for
+ kernel. It simply loopbacks all packets from its TX
+ virtqueue to its RX virtqueue.
+
config SAMPLE_VFIO_MDEV_MDPY_FB
tristate "Build VFIO mdpy example guest fbdev driver -- loadable module only"
depends on FB && m
diff --git a/samples/vfio-mdev/Makefile b/samples/vfio-mdev/Makefile
index 10d179c4fdeb..817618569848 100644
--- a/samples/vfio-mdev/Makefile
+++ b/samples/vfio-mdev/Makefile
@@ -3,3 +3,4 @@ obj-$(CONFIG_SAMPLE_VFIO_MDEV_MTTY) += mtty.o
obj-$(CONFIG_SAMPLE_VFIO_MDEV_MDPY) += mdpy.o
obj-$(CONFIG_SAMPLE_VFIO_MDEV_MDPY_FB) += mdpy-fb.o
obj-$(CONFIG_SAMPLE_VFIO_MDEV_MBOCHS) += mbochs.o
+obj-$(CONFIG_SAMPLE_VIRTIO_MDEV_NET_LOOPBACK) += mvnet_loopback.o
diff --git a/samples/vfio-mdev/mvnet_loopback.c b/samples/vfio-mdev/mvnet_loopback.c
new file mode 100644
index 000000000000..e790004ddeb9
--- /dev/null
+++ b/samples/vfio-mdev/mvnet_loopback.c
@@ -0,0 +1,687 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Mediated virtual virtio-net device driver.
+ *
+ * Copyright (c) 2019, Red Hat Inc. All rights reserved.
+ * Author: Jason Wang <[email protected]>
+ *
+ * Sample driver that creates mdev device that simulates ethernet loopback
+ * device.
+ *
+ * Usage:
+ *
+ * # modprobe virtio_mdev
+ * # modprobe mvnet_loopback
+ * # cd /sys/devices/virtual/mvnet_loopback/mvnet_loopback/ \
+ * mdev_supported_types/mvnet_loopback-virtio
+ * # echo "83b8f4f2-509f-382f-3c1e-e6bfe0fa1001" > ./create
+ * # cd devices/83b8f4f2-509f-382f-3c1e-e6bfe0fa1001
+ * # ls -d virtio0
+ * virtio0
+ */
+
+#include <linux/init.h>
+#include <linux/module.h>
+#include <linux/device.h>
+#include <linux/kernel.h>
+#include <linux/fs.h>
+#include <linux/poll.h>
+#include <linux/slab.h>
+#include <linux/sched.h>
+#include <linux/wait.h>
+#include <linux/uuid.h>
+#include <linux/iommu.h>
+#include <linux/sysfs.h>
+#include <linux/file.h>
+#include <linux/etherdevice.h>
+#include <linux/mdev.h>
+#include <linux/vringh.h>
+#include <linux/mdev_virtio_ops.h>
+#include <uapi/linux/virtio_config.h>
+#include <uapi/linux/virtio_net.h>
+
+#define VERSION_STRING "0.1"
+#define DRIVER_AUTHOR "Red Hat Corporation"
+
+#define MVNET_CLASS_NAME "mvnet_loopback"
+#define MVNET_NAME "mvnet_loopback"
+
+/*
+ * Global Structures
+ */
+
+static struct mvnet_dev {
+ struct class *vd_class;
+ struct idr vd_idr;
+ struct device dev;
+} mvnet_dev;
+
+struct mvnet_virtqueue {
+ struct vringh vring;
+ struct vringh_kiov iov;
+ unsigned short head;
+ bool ready;
+ u64 desc_addr;
+ u64 device_addr;
+ u64 driver_addr;
+ u32 num;
+ void *private;
+ irqreturn_t (*cb)(void *data);
+};
+
+#define MVNET_QUEUE_ALIGN PAGE_SIZE
+#define MVNET_QUEUE_MAX 256
+#define MVNET_DEVICE_ID 0x1
+#define MVNET_VENDOR_ID 0
+
+u64 mvnet_features = (1ULL << VIRTIO_F_ANY_LAYOUT) |
+ (1ULL << VIRTIO_F_VERSION_1) |
+ (1ULL << VIRTIO_F_IOMMU_PLATFORM);
+
+/* State of each mdev device */
+struct mvnet_state {
+ struct mvnet_virtqueue vqs[2];
+ struct work_struct work;
+ /* spinlock to synchronize virtqueue state */
+ spinlock_t lock;
+ struct mdev_device *mdev;
+ struct virtio_net_config config;
+ void *buffer;
+ u32 status;
+ u32 generation;
+ u64 features;
+ struct list_head next;
+};
+
+static struct mutex mdev_list_lock;
+static struct list_head mdev_devices_list;
+
+static void mvnet_queue_ready(struct mvnet_state *mvnet, unsigned int idx)
+{
+ struct mvnet_virtqueue *vq = &mvnet->vqs[idx];
+ int ret;
+
+ ret = vringh_init_kern(&vq->vring, mvnet_features, MVNET_QUEUE_MAX,
+ false, (struct vring_desc *)vq->desc_addr,
+ (struct vring_avail *)vq->driver_addr,
+ (struct vring_used *)vq->device_addr);
+}
+
+static void mvnet_vq_reset(struct mvnet_virtqueue *vq)
+{
+ vq->ready = 0;
+ vq->desc_addr = 0;
+ vq->driver_addr = 0;
+ vq->device_addr = 0;
+ vq->cb = NULL;
+ vq->private = NULL;
+ vringh_init_kern(&vq->vring, mvnet_features, MVNET_QUEUE_MAX,
+ false, 0, 0, 0);
+}
+
+static void mvnet_reset(struct mvnet_state *mvnet)
+{
+ int i;
+
+ for (i = 0; i < 2; i++)
+ mvnet_vq_reset(&mvnet->vqs[i]);
+
+ mvnet->features = 0;
+ mvnet->status = 0;
+ ++mvnet->generation;
+}
+
+static void mvnet_work(struct work_struct *work)
+{
+ struct mvnet_state *mvnet = container_of(work, struct
+ mvnet_state, work);
+ struct mvnet_virtqueue *txq = &mvnet->vqs[1];
+ struct mvnet_virtqueue *rxq = &mvnet->vqs[0];
+ size_t read, write, total_write;
+ int err;
+ int pkts = 0;
+
+ spin_lock(&mvnet->lock);
+
+ if (!txq->ready || !rxq->ready)
+ goto out;
+
+ while (true) {
+ total_write = 0;
+ err = vringh_getdesc_kern(&txq->vring, &txq->iov, NULL,
+ &txq->head, GFP_ATOMIC);
+ if (err <= 0)
+ break;
+
+ err = vringh_getdesc_kern(&rxq->vring, NULL, &rxq->iov,
+ &rxq->head, GFP_ATOMIC);
+ if (err <= 0) {
+ vringh_complete_kern(&txq->vring, txq->head, 0);
+ break;
+ }
+
+ while (true) {
+ read = vringh_iov_pull_kern(&txq->iov, mvnet->buffer,
+ PAGE_SIZE);
+ if (read <= 0)
+ break;
+
+ write = vringh_iov_push_kern(&rxq->iov, mvnet->buffer,
+ read);
+ if (write <= 0)
+ break;
+
+ total_write += write;
+ }
+
+ /* Make sure data is wrote before advancing index */
+ smp_wmb();
+
+ vringh_complete_kern(&txq->vring, txq->head, 0);
+ vringh_complete_kern(&rxq->vring, rxq->head, total_write);
+
+ /* Make sure used is visible before rasing the interrupt. */
+ smp_wmb();
+
+ local_bh_disable();
+ if (txq->cb)
+ txq->cb(txq->private);
+ if (rxq->cb)
+ rxq->cb(rxq->private);
+ local_bh_enable();
+
+ if (++pkts > 4) {
+ schedule_work(&mvnet->work);
+ goto out;
+ }
+ }
+
+out:
+ spin_unlock(&mvnet->lock);
+}
+
+static dma_addr_t mvnet_map_page(struct device *dev, struct page *page,
+ unsigned long offset, size_t size,
+ enum dma_data_direction dir,
+ unsigned long attrs)
+{
+ /* Vringh can only use HVA */
+ return (dma_addr_t)(page_address(page) + offset);
+}
+
+static void mvnet_unmap_page(struct device *dev, dma_addr_t dma_addr,
+ size_t size, enum dma_data_direction dir,
+ unsigned long attrs)
+{
+}
+
+static void *mvnet_alloc_coherent(struct device *dev, size_t size,
+ dma_addr_t *dma_addr, gfp_t flag,
+ unsigned long attrs)
+{
+ void *addr = kmalloc(size, flag);
+
+ if (!addr)
+ *dma_addr = DMA_MAPPING_ERROR;
+ else
+ *dma_addr = (dma_addr_t)addr;
+
+ return addr;
+}
+
+static void mvnet_free_coherent(struct device *dev, size_t size,
+ void *vaddr, dma_addr_t dma_addr,
+ unsigned long attrs)
+{
+ kfree((void *)dma_addr);
+}
+
+static const struct dma_map_ops mvnet_dma_ops = {
+ .map_page = mvnet_map_page,
+ .unmap_page = mvnet_unmap_page,
+ .alloc = mvnet_alloc_coherent,
+ .free = mvnet_free_coherent,
+};
+
+static const struct mdev_virtio_device_ops mdev_virtio_ops;
+
+static int mvnet_create(struct kobject *kobj, struct mdev_device *mdev)
+{
+ struct mvnet_state *mvnet;
+ struct virtio_net_config *config;
+ struct device *dev = mdev_dev(mdev);
+
+ if (!mdev)
+ return -EINVAL;
+
+ mvnet = kzalloc(sizeof(*mvnet), GFP_KERNEL);
+ if (!mvnet)
+ return -ENOMEM;
+
+ mvnet->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
+ if (!mvnet->buffer) {
+ kfree(mvnet);
+ return -ENOMEM;
+ }
+
+ config = &mvnet->config;
+ config->mtu = 1500;
+ config->status = VIRTIO_NET_S_LINK_UP;
+ eth_random_addr(config->mac);
+
+ INIT_WORK(&mvnet->work, mvnet_work);
+
+ spin_lock_init(&mvnet->lock);
+ mvnet->mdev = mdev;
+ mdev_set_drvdata(mdev, mvnet);
+
+ mutex_lock(&mdev_list_lock);
+ list_add(&mvnet->next, &mdev_devices_list);
+ mutex_unlock(&mdev_list_lock);
+
+ dev->coherent_dma_mask = DMA_BIT_MASK(64);
+ set_dma_ops(dev, &mvnet_dma_ops);
+
+ mdev_set_virtio_ops(mdev, &mdev_virtio_ops);
+
+ return 0;
+}
+
+static int mvnet_remove(struct mdev_device *mdev)
+{
+ struct mvnet_state *mds, *tmp_mds;
+ struct mvnet_state *mvnet = mdev_get_drvdata(mdev);
+ int ret = -EINVAL;
+
+ mutex_lock(&mdev_list_lock);
+ list_for_each_entry_safe(mds, tmp_mds, &mdev_devices_list, next) {
+ if (mvnet == mds) {
+ list_del(&mvnet->next);
+ mdev_set_drvdata(mdev, NULL);
+ kfree(mvnet->buffer);
+ kfree(mvnet);
+ ret = 0;
+ break;
+ }
+ }
+ mutex_unlock(&mdev_list_lock);
+
+ return ret;
+}
+
+static ssize_t
+sample_mvnet_dev_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ if (mdev_from_dev(dev))
+ return sprintf(buf, "This is MDEV %s\n", dev_name(dev));
+
+ return sprintf(buf, "\n");
+}
+
+static DEVICE_ATTR_RO(sample_mvnet_dev);
+
+static struct attribute *mvnet_dev_attrs[] = {
+ &dev_attr_sample_mvnet_dev.attr,
+ NULL,
+};
+
+static const struct attribute_group mvnet_dev_group = {
+ .name = "mvnet_dev",
+ .attrs = mvnet_dev_attrs,
+};
+
+static const struct attribute_group *mvnet_dev_groups[] = {
+ &mvnet_dev_group,
+ NULL,
+};
+
+static ssize_t
+sample_mdev_dev_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ if (mdev_from_dev(dev))
+ return sprintf(buf, "This is MDEV %s\n", dev_name(dev));
+
+ return sprintf(buf, "\n");
+}
+
+static DEVICE_ATTR_RO(sample_mdev_dev);
+
+static struct attribute *mdev_dev_attrs[] = {
+ &dev_attr_sample_mdev_dev.attr,
+ NULL,
+};
+
+static const struct attribute_group mdev_dev_group = {
+ .name = "vendor",
+ .attrs = mdev_dev_attrs,
+};
+
+static const struct attribute_group *mdev_dev_groups[] = {
+ &mdev_dev_group,
+ NULL,
+};
+
+#define MVNET_STRING_LEN 16
+
+static ssize_t
+name_show(struct kobject *kobj, struct device *dev, char *buf)
+{
+ char name[MVNET_STRING_LEN];
+ const char *name_str = "virtio-net";
+
+ snprintf(name, MVNET_STRING_LEN, "%s", dev_driver_string(dev));
+ if (!strcmp(kobj->name, name))
+ return sprintf(buf, "%s\n", name_str);
+
+ return -EINVAL;
+}
+
+static MDEV_TYPE_ATTR_RO(name);
+
+static ssize_t
+available_instances_show(struct kobject *kobj, struct device *dev, char *buf)
+{
+ return sprintf(buf, "%d\n", INT_MAX);
+}
+
+static MDEV_TYPE_ATTR_RO(available_instances);
+
+static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
+ char *buf)
+{
+ return sprintf(buf, "%s\n", VIRTIO_MDEV_DEVICE_API_STRING);
+}
+
+static MDEV_TYPE_ATTR_RO(device_api);
+
+static struct attribute *mdev_types_attrs[] = {
+ &mdev_type_attr_name.attr,
+ &mdev_type_attr_device_api.attr,
+ &mdev_type_attr_available_instances.attr,
+ NULL,
+};
+
+static struct attribute_group mdev_type_group = {
+ .name = "virtio",
+ .attrs = mdev_types_attrs,
+};
+
+/* TBD: "vhost" type */
+
+static struct attribute_group *mdev_type_groups[] = {
+ &mdev_type_group,
+ NULL,
+};
+
+static int mvnet_set_vq_address(struct mdev_device *mdev, u16 idx,
+ u64 desc_area, u64 driver_area, u64 device_area)
+{
+ struct mvnet_state *mvnet = mdev_get_drvdata(mdev);
+ struct mvnet_virtqueue *vq = &mvnet->vqs[idx];
+
+ vq->desc_addr = desc_area;
+ vq->driver_addr = driver_area;
+ vq->device_addr = device_area;
+
+ return 0;
+}
+
+static void mvnet_set_vq_num(struct mdev_device *mdev, u16 idx, u32 num)
+{
+ struct mvnet_state *mvnet = mdev_get_drvdata(mdev);
+ struct mvnet_virtqueue *vq = &mvnet->vqs[idx];
+
+ vq->num = num;
+}
+
+static void mvnet_kick_vq(struct mdev_device *mdev, u16 idx)
+{
+ struct mvnet_state *mvnet = mdev_get_drvdata(mdev);
+ struct mvnet_virtqueue *vq = &mvnet->vqs[idx];
+
+ if (vq->ready)
+ schedule_work(&mvnet->work);
+}
+
+static void mvnet_set_vq_cb(struct mdev_device *mdev, u16 idx,
+ struct virtio_mdev_callback *cb)
+{
+ struct mvnet_state *mvnet = mdev_get_drvdata(mdev);
+ struct mvnet_virtqueue *vq = &mvnet->vqs[idx];
+
+ vq->cb = cb->callback;
+ vq->private = cb->private;
+}
+
+static void mvnet_set_vq_ready(struct mdev_device *mdev, u16 idx, bool ready)
+{
+ struct mvnet_state *mvnet = mdev_get_drvdata(mdev);
+ struct mvnet_virtqueue *vq = &mvnet->vqs[idx];
+
+ spin_lock(&mvnet->lock);
+ vq->ready = ready;
+ if (vq->ready)
+ mvnet_queue_ready(mvnet, idx);
+ spin_unlock(&mvnet->lock);
+}
+
+static bool mvnet_get_vq_ready(struct mdev_device *mdev, u16 idx)
+{
+ struct mvnet_state *mvnet = mdev_get_drvdata(mdev);
+ struct mvnet_virtqueue *vq = &mvnet->vqs[idx];
+
+ return vq->ready;
+}
+
+static int mvnet_set_vq_state(struct mdev_device *mdev, u16 idx, u64 state)
+{
+ struct mvnet_state *mvnet = mdev_get_drvdata(mdev);
+ struct mvnet_virtqueue *vq = &mvnet->vqs[idx];
+ struct vringh *vrh = &vq->vring;
+
+ spin_lock(&mvnet->lock);
+ vrh->last_avail_idx = state;
+ spin_unlock(&mvnet->lock);
+
+ return 0;
+}
+
+static u64 mvnet_get_vq_state(struct mdev_device *mdev, u16 idx)
+{
+ struct mvnet_state *mvnet = mdev_get_drvdata(mdev);
+ struct mvnet_virtqueue *vq = &mvnet->vqs[idx];
+ struct vringh *vrh = &vq->vring;
+
+ return vrh->last_avail_idx;
+}
+
+static u16 mvnet_get_vq_align(struct mdev_device *mdev)
+{
+ return MVNET_QUEUE_ALIGN;
+}
+
+static u64 mvnet_get_features(struct mdev_device *mdev)
+{
+ return mvnet_features;
+}
+
+static int mvnet_set_features(struct mdev_device *mdev, u64 features)
+{
+ struct mvnet_state *mvnet = mdev_get_drvdata(mdev);
+
+ /* DMA mapping must be done by driver */
+ if (!(features & (1ULL << VIRTIO_F_IOMMU_PLATFORM)))
+ return -EINVAL;
+
+ mvnet->features = features & mvnet_features;
+
+ return 0;
+}
+
+static void mvnet_set_config_cb(struct mdev_device *mdev,
+ struct virtio_mdev_callback *cb)
+{
+ /* We don't support config interrupt */
+}
+
+static u16 mvnet_get_vq_num_max(struct mdev_device *mdev)
+{
+ return MVNET_QUEUE_MAX;
+}
+
+static u32 mvnet_get_device_id(struct mdev_device *mdev)
+{
+ return MVNET_DEVICE_ID;
+}
+
+static u32 mvnet_get_vendor_id(struct mdev_device *mdev)
+{
+ return MVNET_VENDOR_ID;
+}
+
+static u8 mvnet_get_status(struct mdev_device *mdev)
+{
+ struct mvnet_state *mvnet = mdev_get_drvdata(mdev);
+
+ return mvnet->status;
+}
+
+static void mvnet_set_status(struct mdev_device *mdev, u8 status)
+{
+ struct mvnet_state *mvnet = mdev_get_drvdata(mdev);
+
+ mvnet->status = status;
+
+ if (status == 0) {
+ spin_lock(&mvnet->lock);
+ mvnet_reset(mvnet);
+ spin_unlock(&mvnet->lock);
+ }
+}
+
+static void mvnet_get_config(struct mdev_device *mdev, unsigned int offset,
+ void *buf, unsigned int len)
+{
+ struct mvnet_state *mvnet = mdev_get_drvdata(mdev);
+
+ if (offset + len < sizeof(struct virtio_net_config))
+ memcpy(buf, &mvnet->config + offset, len);
+}
+
+static void mvnet_set_config(struct mdev_device *mdev, unsigned int offset,
+ const void *buf, unsigned int len)
+{
+ /* No writable config supportted by mvnet */
+}
+
+static u32 mvnet_get_generation(struct mdev_device *mdev)
+{
+ struct mvnet_state *mvnet = mdev_get_drvdata(mdev);
+
+ return mvnet->generation;
+}
+
+static const struct mdev_virtio_device_ops mdev_virtio_ops = {
+ .set_vq_address = mvnet_set_vq_address,
+ .set_vq_num = mvnet_set_vq_num,
+ .kick_vq = mvnet_kick_vq,
+ .set_vq_cb = mvnet_set_vq_cb,
+ .set_vq_ready = mvnet_set_vq_ready,
+ .get_vq_ready = mvnet_get_vq_ready,
+ .set_vq_state = mvnet_set_vq_state,
+ .get_vq_state = mvnet_get_vq_state,
+ .get_vq_align = mvnet_get_vq_align,
+ .get_features = mvnet_get_features,
+ .set_features = mvnet_set_features,
+ .set_config_cb = mvnet_set_config_cb,
+ .get_vq_num_max = mvnet_get_vq_num_max,
+ .get_device_id = mvnet_get_device_id,
+ .get_vendor_id = mvnet_get_vendor_id,
+ .get_status = mvnet_get_status,
+ .set_status = mvnet_set_status,
+ .get_config = mvnet_get_config,
+ .set_config = mvnet_set_config,
+ .get_generation = mvnet_get_generation,
+};
+
+static const struct mdev_parent_ops mdev_fops = {
+ .owner = THIS_MODULE,
+ .dev_attr_groups = mvnet_dev_groups,
+ .mdev_attr_groups = mdev_dev_groups,
+ .supported_type_groups = mdev_type_groups,
+ .create = mvnet_create,
+ .remove = mvnet_remove,
+};
+
+static void mvnet_device_release(struct device *dev)
+{
+ dev_dbg(dev, "mvnet: released\n");
+}
+
+static int __init mvnet_dev_init(void)
+{
+ int ret = 0;
+
+ pr_info("mvnet_dev: %s\n", __func__);
+
+ memset(&mvnet_dev, 0, sizeof(mvnet_dev));
+
+ idr_init(&mvnet_dev.vd_idr);
+
+ mvnet_dev.vd_class = class_create(THIS_MODULE, MVNET_CLASS_NAME);
+
+ if (IS_ERR(mvnet_dev.vd_class)) {
+ pr_err("Error: failed to register mvnet_dev class\n");
+ ret = PTR_ERR(mvnet_dev.vd_class);
+ goto failed1;
+ }
+
+ mvnet_dev.dev.class = mvnet_dev.vd_class;
+ mvnet_dev.dev.release = mvnet_device_release;
+ dev_set_name(&mvnet_dev.dev, "%s", MVNET_NAME);
+
+ ret = device_register(&mvnet_dev.dev);
+ if (ret)
+ goto failed2;
+
+ ret = mdev_register_device(&mvnet_dev.dev, &mdev_fops);
+ if (ret)
+ goto failed3;
+
+ mutex_init(&mdev_list_lock);
+ INIT_LIST_HEAD(&mdev_devices_list);
+
+ goto all_done;
+
+failed3:
+
+ device_unregister(&mvnet_dev.dev);
+failed2:
+ class_destroy(mvnet_dev.vd_class);
+
+failed1:
+all_done:
+ return ret;
+}
+
+static void __exit mvnet_dev_exit(void)
+{
+ mvnet_dev.dev.bus = NULL;
+ mdev_unregister_device(&mvnet_dev.dev);
+
+ device_unregister(&mvnet_dev.dev);
+ idr_destroy(&mvnet_dev.vd_idr);
+ class_destroy(mvnet_dev.vd_class);
+ mvnet_dev.vd_class = NULL;
+ pr_info("mvnet_dev: Unloaded!\n");
+}
+
+module_init(mvnet_dev_init)
+module_exit(mvnet_dev_exit)
+
+MODULE_LICENSE("GPL v2");
+MODULE_INFO(supported, "Simulate loopback ethernet device over mdev");
+MODULE_VERSION(VERSION_STRING);
+MODULE_AUTHOR(DRIVER_AUTHOR);
--
2.19.1
On Thu, 7 Nov 2019 23:11:09 +0800
Jason Wang <[email protected]> wrote:
> This sample driver creates mdev device that simulate virtio net device
> over virtio mdev transport. The device is implemented through vringh
> and workqueue. A device specific dma ops is to make sure HVA is used
> directly as the IOVA. This should be sufficient for kernel virtio
> driver to work.
>
> Only 'virtio' type is supported right now. I plan to add 'vhost' type
> on top which requires some virtual IOMMU implemented in this sample
> driver.
>
> Signed-off-by: Jason Wang <[email protected]>
> ---
> MAINTAINERS | 1 +
> samples/Kconfig | 10 +
> samples/vfio-mdev/Makefile | 1 +
> samples/vfio-mdev/mvnet_loopback.c | 687 +++++++++++++++++++++++++++++
> 4 files changed, 699 insertions(+)
> create mode 100644 samples/vfio-mdev/mvnet_loopback.c
Acked-by: Cornelia Huck <[email protected]>
On Thu, Nov 07, 2019 at 11:11:03PM +0800, Jason Wang wrote:
> Hi all:
>
> There are hardwares that can do virtio datapath offloading while
> having its own control path. This path tries to implement a mdev based
> unified API to support using kernel virtio driver to drive those
> devices. This is done by introducing a new mdev transport for virtio
> (virtio_mdev) and register itself as a new kind of mdev driver. Then
> it provides a unified way for kernel virtio driver to talk with mdev
> device implementation.
>
> Though the series only contains kernel driver support, the goal is to
> make the transport generic enough to support userspace drivers. This
> means vhost-mdev[1] could be built on top as well by resuing the
> transport.
>
> A sample driver is also implemented which simulate a virito-net
> loopback ethernet device on top of vringh + workqueue. This could be
> used as a reference implementation for real hardware driver.
>
> Also a real IFC VF driver was also posted here[2] which is a good
> reference for vendors who is interested in their own virtio datapath
> offloading product.
>
> Consider mdev framework only support VFIO device and driver right now,
> this series also extend it to support other types. This is done
> through introducing class id to the device and pairing it with
> id_talbe claimed by the driver. On top, this seris also decouple
> device specific ops out of the common ones for implementing class
> specific operations over mdev bus.
>
> Pktgen test was done with virito-net + mvnet loop back device.
>
> Please review.
>
> [1] https://lkml.org/lkml/2019/11/5/424
> [2] https://lkml.org/lkml/2019/11/5/227
>
> Changes from V10:
> - rename mvnet to mvnet_loopback
> - fix typo in the help text for sample Kconfig
Acked-by: Michael S. Tsirkin <[email protected]>
> Changes from V9:
> - Tweak the help text for virito-mdev kconfig
>
> Changes from V8:
> - try silent checkpatch, some are still there becuase they were inherited
> from virtio_config_ops which needs to be resolved in an independent series
> - tweak on the comment and doc
> - remove VIRTIO_MDEV_F_VERSION_1 completely
> - rename CONFIG_VIRTIO_MDEV_DEVICE to CONFIG_VIRTIO_MDEV
>
> Changes from V7:
> - drop {set|get}_mdev_features for virtio
> - typo and comment style fixes
>
> Changes from V6:
> - rename ops files and compile guard
>
> Changes from V5:
> - use dev_warn() instead of WARN(1) when class id is not set
> - validate id_table before trying to do matching between device and
> driver
> - add wildcard for modpost script
> - use unique name for id_table
> - move get_mdev_features() to be the first member of virtio_device_ops
> and more comments for it
> - typo fixes for the comments above virtio_mdev_ops
>
> Changes from V4:
> - keep mdev_set_class() for the device that doesn't use device ops
> - use union for device ops pointer in mdev_device
> - introduce class specific helper for getting is device ops
> - use WARN_ON instead of BUG_ON in mdev_set_virtio_ops
> - explain details of get_mdev_features() and get_vendor_id()
> - distinguish the optional virito device ops from mandatory ones and
> make get_generation() optional
> - rename vfio_mdev.h to vfio_mdev_ops.h, rename virito_mdev.h to
> virtio_mdev_ops.h
> - don't abuse version fileds in virtio_mdev structure, use features
> instead
> - fix warning during device remove
> - style & docs tweaks and typo fixes
>
> Changes from V3:
> - document that class id (device ops) must be specified in create()
> - add WARN() when trying to set class_id when it has already set
> - add WARN() when class_id is not specified in create() and correctly
> return an error in this case
> - correct the prototype of mdev_set_class() in the doc
> - add documention of mdev_set_class()
> - remove the unnecessary "class_id_fail" label when class id is not
> specified in create()
> - convert id_table in vfio_mdev to const
> - move mdev_set_class and its friends after mdev_uuid()
> - suqash the patch of bus uevent into patch of introducing class id
> - tweak the words in the docs per Cornelia suggestion
> - tie class_id and device ops through class specific initialization
> routine like mdev_set_vfio_ops()
> - typos fixes in the docs of virtio-mdev callbacks
> - document the usage of virtqueues in struct virtio_mdev_device
> - remove the useless vqs array in struct virtio_mdev_device
> - rename MDEV_ID_XXX to MDEV_CLASS_ID_XXX
>
> Changes from V2:
> - fail when class_id is not specified
> - drop the vringh patch
> - match the doc to the code
> - tweak the commit log
> - move device_ops from parent to mdev device
> - remove the unused MDEV_ID_VHOST
>
> Changes from V1:
> - move virtio_mdev.c to drivers/virtio
> - store class_id in mdev_device instead of mdev_parent
> - store device_ops in mdev_device instead of mdev_parent
> - reorder the patch, vringh fix comes first
> - really silent compiling warnings
> - really switch to use u16 for class_id
> - uevent and modpost support for mdev class_id
> - vraious tweaks per comments from Parav
>
> Changes from RFC-V2:
> - silent compile warnings on some specific configuration
> - use u16 instead u8 for class id
> - reseve MDEV_ID_VHOST for future vhost-mdev work
> - introduce "virtio" type for mvnet and make "vhost" type for future
> work
> - add entries in MAINTAINER
> - tweak and typos fixes in commit log
>
> Changes from RFC-V1:
> - rename device id to class id
> - add docs for class id and device specific ops (device_ops)
> - split device_ops into seperate headers
> - drop the mdev_set_dma_ops()
> - use device_ops to implement the transport API, then it's not a part
> of UAPI any more
> - use GFP_ATOMIC in mvnet sample device and other tweaks
> - set_vring_base/get_vring_base support for mvnet device
>
> Jason Wang (6):
> mdev: class id support
> modpost: add support for mdev class id
> mdev: introduce device specific ops
> mdev: introduce virtio device and its device ops
> virtio: introduce a mdev based transport
> docs: sample driver to demonstrate how to implement virtio-mdev
> framework
>
> .../driver-api/vfio-mediated-device.rst | 38 +-
> MAINTAINERS | 3 +
> drivers/gpu/drm/i915/gvt/kvmgt.c | 17 +-
> drivers/s390/cio/vfio_ccw_ops.c | 17 +-
> drivers/s390/crypto/vfio_ap_ops.c | 13 +-
> drivers/vfio/mdev/mdev_core.c | 60 ++
> drivers/vfio/mdev/mdev_driver.c | 25 +
> drivers/vfio/mdev/mdev_private.h | 8 +
> drivers/vfio/mdev/vfio_mdev.c | 45 +-
> drivers/virtio/Kconfig | 13 +
> drivers/virtio/Makefile | 1 +
> drivers/virtio/virtio_mdev.c | 406 +++++++++++
> include/linux/mdev.h | 57 +-
> include/linux/mdev_vfio_ops.h | 52 ++
> include/linux/mdev_virtio_ops.h | 147 ++++
> include/linux/mod_devicetable.h | 8 +
> samples/Kconfig | 10 +
> samples/vfio-mdev/Makefile | 1 +
> samples/vfio-mdev/mbochs.c | 19 +-
> samples/vfio-mdev/mdpy.c | 19 +-
> samples/vfio-mdev/mtty.c | 17 +-
> samples/vfio-mdev/mvnet_loopback.c | 687 ++++++++++++++++++
> scripts/mod/devicetable-offsets.c | 3 +
> scripts/mod/file2alias.c | 11 +
> 24 files changed, 1586 insertions(+), 91 deletions(-)
> create mode 100644 drivers/virtio/virtio_mdev.c
> create mode 100644 include/linux/mdev_vfio_ops.h
> create mode 100644 include/linux/mdev_virtio_ops.h
> create mode 100644 samples/vfio-mdev/mvnet_loopback.c
>
> --
> 2.19.1
On 11/7/2019 8:41 PM, Jason Wang wrote:
> This patch implements basic support for mdev driver that supports
> virtio transport for kernel virtio driver.
>
> Reviewed-by: Cornelia Huck <[email protected]>
> Signed-off-by: Jason Wang <[email protected]>
I'm not expert on virtio part, my ack is from mdev perspective.
Reviewed-by: Kirti Wankhede <[email protected]>
Thanks,
Kirti
> ---
> MAINTAINERS | 1 +
> drivers/vfio/mdev/mdev_core.c | 21 +++++
> drivers/vfio/mdev/mdev_private.h | 2 +
> include/linux/mdev.h | 6 ++
> include/linux/mdev_virtio_ops.h | 147 +++++++++++++++++++++++++++++++
> 5 files changed, 177 insertions(+)
> create mode 100644 include/linux/mdev_virtio_ops.h
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index f661d13344d6..4997957443df 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -17248,6 +17248,7 @@ F: include/linux/virtio*.h
> F: include/uapi/linux/virtio_*.h
> F: drivers/crypto/virtio/
> F: mm/balloon_compaction.c
> +F: include/linux/mdev_virtio_ops.h
>
> VIRTIO BLOCK AND SCSI DRIVERS
> M: "Michael S. Tsirkin" <[email protected]>
> diff --git a/drivers/vfio/mdev/mdev_core.c b/drivers/vfio/mdev/mdev_core.c
> index 4e70f19ac145..c58253404ed5 100644
> --- a/drivers/vfio/mdev/mdev_core.c
> +++ b/drivers/vfio/mdev/mdev_core.c
> @@ -78,6 +78,27 @@ const struct mdev_vfio_device_ops *mdev_get_vfio_ops(struct mdev_device *mdev)
> }
> EXPORT_SYMBOL(mdev_get_vfio_ops);
>
> +/*
> + * Specify the virtio device ops for the mdev device, this
> + * must be called during create() callback for virtio mdev device.
> + */
> +void mdev_set_virtio_ops(struct mdev_device *mdev,
> + const struct mdev_virtio_device_ops *virtio_ops)
> +{
> + mdev_set_class(mdev, MDEV_CLASS_ID_VIRTIO);
> + mdev->virtio_ops = virtio_ops;
> +}
> +EXPORT_SYMBOL(mdev_set_virtio_ops);
> +
> +/* Get the virtio device ops for the mdev device. */
> +const struct mdev_virtio_device_ops *
> +mdev_get_virtio_ops(struct mdev_device *mdev)
> +{
> + WARN_ON(mdev->class_id != MDEV_CLASS_ID_VIRTIO);
> + return mdev->virtio_ops;
> +}
> +EXPORT_SYMBOL(mdev_get_virtio_ops);
> +
> struct device *mdev_dev(struct mdev_device *mdev)
> {
> return &mdev->dev;
> diff --git a/drivers/vfio/mdev/mdev_private.h b/drivers/vfio/mdev/mdev_private.h
> index 411227373625..2c74dd032409 100644
> --- a/drivers/vfio/mdev/mdev_private.h
> +++ b/drivers/vfio/mdev/mdev_private.h
> @@ -11,6 +11,7 @@
> #define MDEV_PRIVATE_H
>
> #include <linux/mdev_vfio_ops.h>
> +#include <linux/mdev_virtio_ops.h>
>
> int mdev_bus_register(void);
> void mdev_bus_unregister(void);
> @@ -38,6 +39,7 @@ struct mdev_device {
> u16 class_id;
> union {
> const struct mdev_vfio_device_ops *vfio_ops;
> + const struct mdev_virtio_device_ops *virtio_ops;
> };
> };
>
> diff --git a/include/linux/mdev.h b/include/linux/mdev.h
> index 9e37506d1987..f3d75a60c2b5 100644
> --- a/include/linux/mdev.h
> +++ b/include/linux/mdev.h
> @@ -17,6 +17,7 @@
>
> struct mdev_device;
> struct mdev_vfio_device_ops;
> +struct mdev_virtio_device_ops;
>
> /*
> * Called by the parent device driver to set the device which represents
> @@ -112,6 +113,10 @@ void mdev_set_class(struct mdev_device *mdev, u16 id);
> void mdev_set_vfio_ops(struct mdev_device *mdev,
> const struct mdev_vfio_device_ops *vfio_ops);
> const struct mdev_vfio_device_ops *mdev_get_vfio_ops(struct mdev_device *mdev);
> +void mdev_set_virtio_ops(struct mdev_device *mdev,
> + const struct mdev_virtio_device_ops *virtio_ops);
> +const struct mdev_virtio_device_ops *
> +mdev_get_virtio_ops(struct mdev_device *mdev);
>
> extern struct bus_type mdev_bus_type;
>
> @@ -127,6 +132,7 @@ struct mdev_device *mdev_from_dev(struct device *dev);
>
> enum {
> MDEV_CLASS_ID_VFIO = 1,
> + MDEV_CLASS_ID_VIRTIO = 2,
> /* New entries must be added here */
> };
>
> diff --git a/include/linux/mdev_virtio_ops.h b/include/linux/mdev_virtio_ops.h
> new file mode 100644
> index 000000000000..8951331c6629
> --- /dev/null
> +++ b/include/linux/mdev_virtio_ops.h
> @@ -0,0 +1,147 @@
> +/* SPDX-License-Identifier: GPL-2.0-only */
> +/*
> + * Virtio mediated device driver
> + *
> + * Copyright 2019, Red Hat Corp.
> + * Author: Jason Wang <[email protected]>
> + */
> +#ifndef MDEV_VIRTIO_OPS_H
> +#define MDEV_VIRTIO_OPS_H
> +
> +#include <linux/interrupt.h>
> +#include <linux/mdev.h>
> +#include <uapi/linux/vhost.h>
> +
> +#define VIRTIO_MDEV_DEVICE_API_STRING "virtio-mdev"
> +
> +struct virtio_mdev_callback {
> + irqreturn_t (*callback)(void *data);
> + void *private;
> +};
> +
> +/**
> + * struct mdev_virtio_device_ops - Structure to be registered for each
> + * mdev device to register the device for virtio/vhost drivers.
> + *
> + * The callbacks are mandatory unless explicitly mentioned.
> + *
> + * @set_vq_address: Set the address of virtqueue
> + * @mdev: mediated device
> + * @idx: virtqueue index
> + * @desc_area: address of desc area
> + * @driver_area: address of driver area
> + * @device_area: address of device area
> + * Returns integer: success (0) or error (< 0)
> + * @set_vq_num: Set the size of virtqueue
> + * @mdev: mediated device
> + * @idx: virtqueue index
> + * @num: the size of virtqueue
> + * @kick_vq: Kick the virtqueue
> + * @mdev: mediated device
> + * @idx: virtqueue index
> + * @set_vq_cb: Set the interrupt callback function for
> + * a virtqueue
> + * @mdev: mediated device
> + * @idx: virtqueue index
> + * @cb: virtio-mdev interrupt callback structure
> + * @set_vq_ready: Set ready status for a virtqueue
> + * @mdev: mediated device
> + * @idx: virtqueue index
> + * @ready: ready (true) not ready(false)
> + * @get_vq_ready: Get ready status for a virtqueue
> + * @mdev: mediated device
> + * @idx: virtqueue index
> + * Returns boolean: ready (true) or not (false)
> + * @set_vq_state: Set the state for a virtqueue
> + * @mdev: mediated device
> + * @idx: virtqueue index
> + * @state: virtqueue state (last_avail_idx)
> + * Returns integer: success (0) or error (< 0)
> + * @get_vq_state: Get the state for a virtqueue
> + * @mdev: mediated device
> + * @idx: virtqueue index
> + * Returns virtqueue state (last_avail_idx)
> + * @get_vq_align: Get the virtqueue align requirement
> + * for the device
> + * @mdev: mediated device
> + * Returns virtqueue algin requirement
> + * @get_features: Get virtio features supported by the device
> + * @mdev: mediated device
> + * Returns the virtio features support by the
> + * device
> + * @set_features: Set virtio features supported by the driver
> + * @mdev: mediated device
> + * @features: feature support by the driver
> + * Returns integer: success (0) or error (< 0)
> + * @set_config_cb: Set the config interrupt callback
> + * @mdev: mediated device
> + * @cb: virtio-mdev interrupt callback structure
> + * @get_vq_num_max: Get the max size of virtqueue
> + * @mdev: mediated device
> + * Returns u16: max size of virtqueue
> + * @get_device_id: Get virtio device id
> + * @mdev: mediated device
> + * Returns u32: virtio device id
> + * @get_vendor_id: Get id for the vendor that provides this device
> + * @mdev: mediated device
> + * Returns u32: virtio vendor id
> + * @get_status: Get the device status
> + * @mdev: mediated device
> + * Returns u8: virtio device status
> + * @set_status: Set the device status
> + * @mdev: mediated device
> + * @status: virtio device status
> + * @get_config: Read from device specific configuration space
> + * @mdev: mediated device
> + * @offset: offset from the beginning of
> + * configuration space
> + * @buf: buffer used to read to
> + * @len: the length to read from
> + * configration space
> + * @set_config: Write to device specific configuration space
> + * @mdev: mediated device
> + * @offset: offset from the beginning of
> + * configuration space
> + * @buf: buffer used to write from
> + * @len: the length to write to
> + * configration space
> + * @get_generation: Get device config generaton (optional)
> + * @mdev: mediated device
> + * Returns u32: device generation
> + */
> +struct mdev_virtio_device_ops {
> + /* Virtqueue ops */
> + int (*set_vq_address)(struct mdev_device *mdev,
> + u16 idx, u64 desc_area, u64 driver_area,
> + u64 device_area);
> + void (*set_vq_num)(struct mdev_device *mdev, u16 idx, u32 num);
> + void (*kick_vq)(struct mdev_device *mdev, u16 idx);
> + void (*set_vq_cb)(struct mdev_device *mdev, u16 idx,
> + struct virtio_mdev_callback *cb);
> + void (*set_vq_ready)(struct mdev_device *mdev, u16 idx, bool ready);
> + bool (*get_vq_ready)(struct mdev_device *mdev, u16 idx);
> + int (*set_vq_state)(struct mdev_device *mdev, u16 idx, u64 state);
> + u64 (*get_vq_state)(struct mdev_device *mdev, u16 idx);
> +
> + /* Virtio device ops */
> + u16 (*get_vq_align)(struct mdev_device *mdev);
> + u64 (*get_features)(struct mdev_device *mdev);
> + int (*set_features)(struct mdev_device *mdev, u64 features);
> + void (*set_config_cb)(struct mdev_device *mdev,
> + struct virtio_mdev_callback *cb);
> + u16 (*get_vq_num_max)(struct mdev_device *mdev);
> + u32 (*get_device_id)(struct mdev_device *mdev);
> + u32 (*get_vendor_id)(struct mdev_device *mdev);
> + u8 (*get_status)(struct mdev_device *mdev);
> + void (*set_status)(struct mdev_device *mdev, u8 status);
> + void (*get_config)(struct mdev_device *mdev, unsigned int offset,
> + void *buf, unsigned int len);
> + void (*set_config)(struct mdev_device *mdev, unsigned int offset,
> + const void *buf, unsigned int len);
> + u32 (*get_generation)(struct mdev_device *mdev);
> +};
> +
> +void mdev_set_virtio_ops(struct mdev_device *mdev,
> + const struct mdev_virtio_device_ops *virtio_ops);
> +
> +#endif
>