All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jike Song <jike.song@intel.com>
To: Kirti Wankhede <kwankhede@nvidia.com>
Cc: alex.williamson@redhat.com, pbonzini@redhat.com,
	kraxel@redhat.com, cjia@nvidia.com, qemu-devel@nongnu.org,
	kvm@vger.kernel.org, kevin.tian@intel.com,
	bjsdjshi@linux.vnet.ibm.com
Subject: Re: [PATCH v7 2/4] vfio: VFIO driver for mediated devices
Date: Tue, 20 Sep 2016 20:53:48 +0800	[thread overview]
Message-ID: <57E1315C.5070304@intel.com> (raw)
In-Reply-To: <1472097235-6332-3-git-send-email-kwankhede@nvidia.com>

On 08/25/2016 11:53 AM, Kirti Wankhede wrote:
/* {snip} */

To show another possible implementation of vfio-mdev, which provides
the thinnest framework and let the vendor physical drivers do
whatever they want to.

Again, it is diff-ed against Kirti's version 7, for demonstration only.

--
Thanks,
Jike


diff --git a/drivers/vfio/mdev/Kconfig b/drivers/vfio/mdev/Kconfig
index d25439f..b2fe0c6 100644
--- a/drivers/vfio/mdev/Kconfig
+++ b/drivers/vfio/mdev/Kconfig
@@ -8,3 +8,11 @@ config MDEV
 	See Documentation/vfio-mediated-device.txt for more details.
 
         If you don't know what do here, say N.
+
+config VFIO_MDEV
+    tristate "VFIO Bus driver for Mediated devices"
+    depends on VFIO && MDEV
+    default n
+    help
+        VFIO Bus driver for mediated devices.
+
diff --git a/drivers/vfio/mdev/Makefile b/drivers/vfio/mdev/Makefile
index 8bd78b5..ee9f89f 100644
--- a/drivers/vfio/mdev/Makefile
+++ b/drivers/vfio/mdev/Makefile
@@ -2,3 +2,4 @@
 mdev-y := mdev_core.o mdev_sysfs.o mdev_driver.o
 
 obj-$(CONFIG_MDEV) += mdev.o
+obj-$(CONFIG_VFIO_MDEV) += vfio_mdev.o
diff --git a/drivers/vfio/mdev/vfio_mdev.c b/drivers/vfio/mdev/vfio_mdev.c
index 28f13ae..c22ebd8 100644
--- a/drivers/vfio/mdev/vfio_mdev.c
+++ b/drivers/vfio/mdev/vfio_mdev.c
@@ -1,10 +1,15 @@
 /*
- * VFIO based Mediated PCI device driver
+ * VFIO Bus driver for Mediated device
  *
  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  *     Author: Neo Jia <cjia@nvidia.com>
  *	       Kirti Wankhede <kwankhede@nvidia.com>
  *
+ * Copyright (c) 2016 Intel Corporation.
+ * Author:
+ *	Xiao Guangrong <guangrong.xiao@linux.intel.com>
+ *	Jike Song <jike.song@intel.com>
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
@@ -22,24 +27,21 @@
 
 #include "mdev_private.h"
 
-#define DRIVER_VERSION  "0.1"
+#define DRIVER_VERSION  "0.2"
 #define DRIVER_AUTHOR   "NVIDIA Corporation"
-#define DRIVER_DESC     "VFIO based Mediated PCI device driver"
+#define DRIVER_DESC     "VFIO Bus driver for Mediated device"
 
 struct vfio_mdev {
 	struct iommu_group *group;
 	struct mdev_device *mdev;
-	struct vfio_device_info dev_info;
 };
 
 static int vfio_mdev_open(void *device_data)
 {
-	int ret = 0;
-
 	if (!try_module_get(THIS_MODULE))
 		return -ENODEV;
 
-	return ret;
+	return 0;
 }
 
 static void vfio_mdev_close(void *device_data)
@@ -47,220 +49,17 @@ static void vfio_mdev_close(void *device_data)
 	module_put(THIS_MODULE);
 }
 
-static int sparse_mmap_cap(struct vfio_info_cap *caps, void *cap_type)
-{
-	struct vfio_info_cap_header *header;
-	struct vfio_region_info_cap_sparse_mmap *sparse_cap, *sparse = cap_type;
-	size_t size;
-
-	size = sizeof(*sparse) + sparse->nr_areas *  sizeof(*sparse->areas);
-	header = vfio_info_cap_add(caps, size,
-				   VFIO_REGION_INFO_CAP_SPARSE_MMAP, 1);
-	if (IS_ERR(header))
-		return PTR_ERR(header);
-
-	sparse_cap = container_of(header,
-			struct vfio_region_info_cap_sparse_mmap, header);
-	sparse_cap->nr_areas = sparse->nr_areas;
-	memcpy(sparse_cap->areas, sparse->areas,
-	       sparse->nr_areas * sizeof(*sparse->areas));
-	return 0;
-}
-
-static int region_type_cap(struct vfio_info_cap *caps, void *cap_type)
-{
-	struct vfio_info_cap_header *header;
-	struct vfio_region_info_cap_type *type_cap, *cap = cap_type;
-
-	header = vfio_info_cap_add(caps, sizeof(*cap),
-				   VFIO_REGION_INFO_CAP_TYPE, 1);
-	if (IS_ERR(header))
-		return PTR_ERR(header);
-
-	type_cap = container_of(header, struct vfio_region_info_cap_type,
-				header);
-	type_cap->type = cap->type;
-	type_cap->subtype = cap->type;
-	return 0;
-}
-
 static long vfio_mdev_unlocked_ioctl(void *device_data,
 				     unsigned int cmd, unsigned long arg)
 {
-	int ret = 0;
 	struct vfio_mdev *vmdev = device_data;
-	struct parent_device *parent = vmdev->mdev->parent;
-	unsigned long minsz;
-
-	switch (cmd) {
-	case VFIO_DEVICE_GET_INFO:
-	{
-		struct vfio_device_info info;
-
-		minsz = offsetofend(struct vfio_device_info, num_irqs);
-
-		if (copy_from_user(&info, (void __user *)arg, minsz))
-			return -EFAULT;
-
-		if (info.argsz < minsz)
-			return -EINVAL;
-
-		if (parent->ops->get_device_info)
-			ret = parent->ops->get_device_info(vmdev->mdev, &info);
-		else
-			return -EINVAL;
-
-		if (ret)
-			return ret;
-
-		if (parent->ops->reset)
-			info.flags |= VFIO_DEVICE_FLAGS_RESET;
-
-		memcpy(&vmdev->dev_info, &info, sizeof(info));
-
-		return copy_to_user((void __user *)arg, &info, minsz);
-	}
-	case VFIO_DEVICE_GET_REGION_INFO:
-	{
-		struct vfio_region_info info;
-		struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
-		u16 cap_type_id = 0;
-		void *cap_type = NULL;
-
-		minsz = offsetofend(struct vfio_region_info, offset);
-
-		if (copy_from_user(&info, (void __user *)arg, minsz))
-			return -EFAULT;
-
-		if (info.argsz < minsz)
-			return -EINVAL;
-
-		if (parent->ops->get_region_info)
-			ret = parent->ops->get_region_info(vmdev->mdev, &info,
-						       &cap_type_id, &cap_type);
-		else
-			return -EINVAL;
-
-		if (ret)
-			return ret;
-
-		if ((info.flags & VFIO_REGION_INFO_FLAG_CAPS) && cap_type) {
-			switch (cap_type_id) {
-			case VFIO_REGION_INFO_CAP_SPARSE_MMAP:
-				ret = sparse_mmap_cap(&caps, cap_type);
-				if (ret)
-					return ret;
-				break;
-
-			case VFIO_REGION_INFO_CAP_TYPE:
-				ret = region_type_cap(&caps, cap_type);
-				if (ret)
-					return ret;
-				break;
-			default:
-				return -EINVAL;
-			}
-		}
-
-		if (caps.size) {
-			if (info.argsz < sizeof(info) + caps.size) {
-				info.argsz = sizeof(info) + caps.size;
-				info.cap_offset = 0;
-			} else {
-				vfio_info_cap_shift(&caps, sizeof(info));
-				if (copy_to_user((void __user *)arg +
-							sizeof(info), caps.buf,
-							caps.size)) {
-					kfree(caps.buf);
-					return -EFAULT;
-				}
-				info.cap_offset = sizeof(info);
-			}
-			kfree(caps.buf);
-		}
-
-		return copy_to_user((void __user *)arg, &info, minsz);
-	}
-	case VFIO_DEVICE_GET_IRQ_INFO:
-	{
-		struct vfio_irq_info info;
-
-		minsz = offsetofend(struct vfio_irq_info, count);
-
-		if (copy_from_user(&info, (void __user *)arg, minsz))
-			return -EFAULT;
-
-		if ((info.argsz < minsz) ||
-		    (info.index >= vmdev->dev_info.num_irqs))
-			return -EINVAL;
-
-		if (parent->ops->get_irq_info)
-			ret = parent->ops->get_irq_info(vmdev->mdev, &info);
-		else
-			return -EINVAL;
-
-		if (ret)
-			return ret;
-
-		if (info.count == -1)
-			return -EINVAL;
-
-		return copy_to_user((void __user *)arg, &info, minsz);
-	}
-	case VFIO_DEVICE_SET_IRQS:
-	{
-		struct vfio_irq_set hdr;
-		u8 *data = NULL, *ptr = NULL;
-
-		minsz = offsetofend(struct vfio_irq_set, count);
-
-		if (copy_from_user(&hdr, (void __user *)arg, minsz))
-			return -EFAULT;
-
-		if ((hdr.argsz < minsz) ||
-		    (hdr.index >= vmdev->dev_info.num_irqs) ||
-		    (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
-				  VFIO_IRQ_SET_ACTION_TYPE_MASK)))
-			return -EINVAL;
-
-		if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
-			size_t size;
-
-			if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
-				size = sizeof(uint8_t);
-			else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
-				size = sizeof(int32_t);
-			else
-				return -EINVAL;
-
-			if (hdr.argsz - minsz < hdr.count * size)
-				return -EINVAL;
-
-			ptr = data = memdup_user((void __user *)(arg + minsz),
-						 hdr.count * size);
-			if (IS_ERR(data))
-				return PTR_ERR(data);
-		}
-
-		if (parent->ops->set_irqs)
-			ret = parent->ops->set_irqs(vmdev->mdev, hdr.flags,
-						    hdr.index, hdr.start,
-						    hdr.count, data);
-		else
-			ret = -EINVAL;
-
-		kfree(ptr);
-		return ret;
-	}
-	case VFIO_DEVICE_RESET:
-	{
-		if (parent->ops->reset)
-			return parent->ops->reset(vmdev->mdev);
-
-		return -EINVAL;
-	}
-	}
-	return -ENOTTY;
+	struct mdev_device *mdev = vmdev->mdev;
+	struct mdev_host *host = dev_to_host(mdev->dev.parent);
+
+	if (host->ops->ioctl)
+		return host->ops->ioctl(mdev, cmd, arg);
+
+	return -ENODEV;
 }
 
 static ssize_t vfio_mdev_read(void *device_data, char __user *buf,
@@ -268,63 +67,12 @@ static ssize_t vfio_mdev_read(void *device_data, char __user *buf,
 {
 	struct vfio_mdev *vmdev = device_data;
 	struct mdev_device *mdev = vmdev->mdev;
-	struct parent_device *parent = mdev->parent;
-	unsigned int done = 0;
-	int ret;
-
-	if (!parent->ops->read)
-		return -EINVAL;
-
-	while (count) {
-		size_t filled;
-
-		if (count >= 4 && !(*ppos % 4)) {
-			u32 val;
-
-			ret = parent->ops->read(mdev, (char *)&val, sizeof(val),
-						*ppos);
-			if (ret <= 0)
-				goto read_err;
-
-			if (copy_to_user(buf, &val, sizeof(val)))
-				goto read_err;
-
-			filled = 4;
-		} else if (count >= 2 && !(*ppos % 2)) {
-			u16 val;
-
-			ret = parent->ops->read(mdev, (char *)&val, sizeof(val),
-						*ppos);
-			if (ret <= 0)
-				goto read_err;
-
-			if (copy_to_user(buf, &val, sizeof(val)))
-				goto read_err;
-
-			filled = 2;
-		} else {
-			u8 val;
-
-			ret = parent->ops->read(mdev, &val, sizeof(val), *ppos);
-			if (ret <= 0)
-				goto read_err;
+	struct mdev_host *host = dev_to_host(mdev->dev.parent);
 
-			if (copy_to_user(buf, &val, sizeof(val)))
-				goto read_err;
+	if (host->ops->read)
+		return host->ops->read(mdev, buf, count, ppos);
 
-			filled = 1;
-		}
-
-		count -= filled;
-		done += filled;
-		*ppos += filled;
-		buf += filled;
-	}
-
-	return done;
-
-read_err:
-	return -EFAULT;
+	return -ENODEV;
 }
 
 static ssize_t vfio_mdev_write(void *device_data, const char __user *buf,
@@ -332,75 +80,24 @@ static ssize_t vfio_mdev_write(void *device_data, const char __user *buf,
 {
 	struct vfio_mdev *vmdev = device_data;
 	struct mdev_device *mdev = vmdev->mdev;
-	struct parent_device *parent = mdev->parent;
-	unsigned int done = 0;
-	int ret;
-
-	if (!parent->ops->write)
-		return -EINVAL;
-
-	while (count) {
-		size_t filled;
-
-		if (count >= 4 && !(*ppos % 4)) {
-			u32 val;
-
-			if (copy_from_user(&val, buf, sizeof(val)))
-				goto write_err;
-
-			ret = parent->ops->write(mdev, (char *)&val,
-						 sizeof(val), *ppos);
-			if (ret <= 0)
-				goto write_err;
-
-			filled = 4;
-		} else if (count >= 2 && !(*ppos % 2)) {
-			u16 val;
-
-			if (copy_from_user(&val, buf, sizeof(val)))
-				goto write_err;
-
-			ret = parent->ops->write(mdev, (char *)&val,
-						 sizeof(val), *ppos);
-			if (ret <= 0)
-				goto write_err;
-
-			filled = 2;
-		} else {
-			u8 val;
+	struct mdev_host *host = dev_to_host(mdev->dev.parent);
 
-			if (copy_from_user(&val, buf, sizeof(val)))
-				goto write_err;
+	if (host->ops->write)
+		return host->ops->write(mdev, buf, count, ppos);
 
-			ret = parent->ops->write(mdev, &val, sizeof(val),
-						 *ppos);
-			if (ret <= 0)
-				goto write_err;
-
-			filled = 1;
-		}
-
-		count -= filled;
-		done += filled;
-		*ppos += filled;
-		buf += filled;
-	}
-
-	return done;
-write_err:
-	return -EFAULT;
+	return -ENODEV;
 }
 
 static int vfio_mdev_mmap(void *device_data, struct vm_area_struct *vma)
 {
 	struct vfio_mdev *vmdev = device_data;
 	struct mdev_device *mdev = vmdev->mdev;
-	struct parent_device *parent = mdev->parent;
+	struct mdev_host *host = dev_to_host(mdev->dev.parent);
 
-	if (parent->ops->mmap)
-		return parent->ops->mmap(mdev, vma);
+	if (host->ops->mmap)
+		return host->ops->mmap(mdev, vma);
 
-	return -EINVAL;
+	return -ENODEV;
 }
 
 static const struct vfio_device_ops vfio_mdev_dev_ops = {
@@ -413,28 +110,27 @@ static const struct vfio_device_ops vfio_mdev_dev_ops = {
 	.mmap		= vfio_mdev_mmap,
 };
 
-int vfio_mdev_probe(struct device *dev)
+static int vfio_mdev_probe(struct device *dev)
 {
 	struct vfio_mdev *vmdev;
-	struct mdev_device *mdev = to_mdev_device(dev);
+	struct mdev_device *mdev = dev_to_mdev(dev);
 	int ret;
 
 	vmdev = kzalloc(sizeof(*vmdev), GFP_KERNEL);
 	if (IS_ERR(vmdev))
 		return PTR_ERR(vmdev);
 
-	vmdev->mdev = mdev_get_device(mdev);
+	vmdev->mdev = mdev;
 	vmdev->group = mdev->group;
 
 	ret = vfio_add_group_dev(dev, &vfio_mdev_dev_ops, vmdev);
 	if (ret)
 		kfree(vmdev);
 
-	mdev_put_device(mdev);
 	return ret;
 }
 
-void vfio_mdev_remove(struct device *dev)
+static void vfio_mdev_remove(struct device *dev)
 {
 	struct vfio_mdev *vmdev;
 
@@ -442,10 +138,34 @@ void vfio_mdev_remove(struct device *dev)
 	kfree(vmdev);
 }
 
-struct mdev_driver vfio_mdev_driver = {
-	.name	= "vfio_mdev",
-	.probe	= vfio_mdev_probe,
-	.remove	= vfio_mdev_remove,
+static int vfio_mdev_online(struct device *dev)
+{
+	struct mdev_device *mdev = dev_to_mdev(dev);
+	struct mdev_host *host = dev_to_host(mdev->dev.parent);
+
+	if (host->ops->start)
+		return host->ops->start(mdev);
+
+	return -ENOTSUPP;
+}
+
+static int vfio_mdev_offline(struct device *dev)
+{
+	struct mdev_device *mdev = dev_to_mdev(dev);
+	struct mdev_host *host = dev_to_host(mdev->dev.parent);
+
+	if (host->ops->stop)
+		return host->ops->stop(mdev);
+
+	return -ENOTSUPP;
+}
+
+static struct mdev_driver vfio_mdev_driver = {
+	.name		= "vfio_mdev",
+	.probe		= vfio_mdev_probe,
+	.remove		= vfio_mdev_remove,
+	.online		= vfio_mdev_online,
+	.offline	= vfio_mdev_offline,
 };
 
 static int __init vfio_mdev_init(void)

WARNING: multiple messages have this Message-ID (diff)
From: Jike Song <jike.song@intel.com>
To: Kirti Wankhede <kwankhede@nvidia.com>
Cc: alex.williamson@redhat.com, pbonzini@redhat.com,
	kraxel@redhat.com, cjia@nvidia.com, qemu-devel@nongnu.org,
	kvm@vger.kernel.org, kevin.tian@intel.com,
	bjsdjshi@linux.vnet.ibm.com
Subject: Re: [Qemu-devel] [PATCH v7 2/4] vfio: VFIO driver for mediated devices
Date: Tue, 20 Sep 2016 20:53:48 +0800	[thread overview]
Message-ID: <57E1315C.5070304@intel.com> (raw)
In-Reply-To: <1472097235-6332-3-git-send-email-kwankhede@nvidia.com>

On 08/25/2016 11:53 AM, Kirti Wankhede wrote:
/* {snip} */

To show another possible implementation of vfio-mdev, which provides
the thinnest framework and let the vendor physical drivers do
whatever they want to.

Again, it is diff-ed against Kirti's version 7, for demonstration only.

--
Thanks,
Jike


diff --git a/drivers/vfio/mdev/Kconfig b/drivers/vfio/mdev/Kconfig
index d25439f..b2fe0c6 100644
--- a/drivers/vfio/mdev/Kconfig
+++ b/drivers/vfio/mdev/Kconfig
@@ -8,3 +8,11 @@ config MDEV
 	See Documentation/vfio-mediated-device.txt for more details.
 
         If you don't know what do here, say N.
+
+config VFIO_MDEV
+    tristate "VFIO Bus driver for Mediated devices"
+    depends on VFIO && MDEV
+    default n
+    help
+        VFIO Bus driver for mediated devices.
+
diff --git a/drivers/vfio/mdev/Makefile b/drivers/vfio/mdev/Makefile
index 8bd78b5..ee9f89f 100644
--- a/drivers/vfio/mdev/Makefile
+++ b/drivers/vfio/mdev/Makefile
@@ -2,3 +2,4 @@
 mdev-y := mdev_core.o mdev_sysfs.o mdev_driver.o
 
 obj-$(CONFIG_MDEV) += mdev.o
+obj-$(CONFIG_VFIO_MDEV) += vfio_mdev.o
diff --git a/drivers/vfio/mdev/vfio_mdev.c b/drivers/vfio/mdev/vfio_mdev.c
index 28f13ae..c22ebd8 100644
--- a/drivers/vfio/mdev/vfio_mdev.c
+++ b/drivers/vfio/mdev/vfio_mdev.c
@@ -1,10 +1,15 @@
 /*
- * VFIO based Mediated PCI device driver
+ * VFIO Bus driver for Mediated device
  *
  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
  *     Author: Neo Jia <cjia@nvidia.com>
  *	       Kirti Wankhede <kwankhede@nvidia.com>
  *
+ * Copyright (c) 2016 Intel Corporation.
+ * Author:
+ *	Xiao Guangrong <guangrong.xiao@linux.intel.com>
+ *	Jike Song <jike.song@intel.com>
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
@@ -22,24 +27,21 @@
 
 #include "mdev_private.h"
 
-#define DRIVER_VERSION  "0.1"
+#define DRIVER_VERSION  "0.2"
 #define DRIVER_AUTHOR   "NVIDIA Corporation"
-#define DRIVER_DESC     "VFIO based Mediated PCI device driver"
+#define DRIVER_DESC     "VFIO Bus driver for Mediated device"
 
 struct vfio_mdev {
 	struct iommu_group *group;
 	struct mdev_device *mdev;
-	struct vfio_device_info dev_info;
 };
 
 static int vfio_mdev_open(void *device_data)
 {
-	int ret = 0;
-
 	if (!try_module_get(THIS_MODULE))
 		return -ENODEV;
 
-	return ret;
+	return 0;
 }
 
 static void vfio_mdev_close(void *device_data)
@@ -47,220 +49,17 @@ static void vfio_mdev_close(void *device_data)
 	module_put(THIS_MODULE);
 }
 
-static int sparse_mmap_cap(struct vfio_info_cap *caps, void *cap_type)
-{
-	struct vfio_info_cap_header *header;
-	struct vfio_region_info_cap_sparse_mmap *sparse_cap, *sparse = cap_type;
-	size_t size;
-
-	size = sizeof(*sparse) + sparse->nr_areas *  sizeof(*sparse->areas);
-	header = vfio_info_cap_add(caps, size,
-				   VFIO_REGION_INFO_CAP_SPARSE_MMAP, 1);
-	if (IS_ERR(header))
-		return PTR_ERR(header);
-
-	sparse_cap = container_of(header,
-			struct vfio_region_info_cap_sparse_mmap, header);
-	sparse_cap->nr_areas = sparse->nr_areas;
-	memcpy(sparse_cap->areas, sparse->areas,
-	       sparse->nr_areas * sizeof(*sparse->areas));
-	return 0;
-}
-
-static int region_type_cap(struct vfio_info_cap *caps, void *cap_type)
-{
-	struct vfio_info_cap_header *header;
-	struct vfio_region_info_cap_type *type_cap, *cap = cap_type;
-
-	header = vfio_info_cap_add(caps, sizeof(*cap),
-				   VFIO_REGION_INFO_CAP_TYPE, 1);
-	if (IS_ERR(header))
-		return PTR_ERR(header);
-
-	type_cap = container_of(header, struct vfio_region_info_cap_type,
-				header);
-	type_cap->type = cap->type;
-	type_cap->subtype = cap->type;
-	return 0;
-}
-
 static long vfio_mdev_unlocked_ioctl(void *device_data,
 				     unsigned int cmd, unsigned long arg)
 {
-	int ret = 0;
 	struct vfio_mdev *vmdev = device_data;
-	struct parent_device *parent = vmdev->mdev->parent;
-	unsigned long minsz;
-
-	switch (cmd) {
-	case VFIO_DEVICE_GET_INFO:
-	{
-		struct vfio_device_info info;
-
-		minsz = offsetofend(struct vfio_device_info, num_irqs);
-
-		if (copy_from_user(&info, (void __user *)arg, minsz))
-			return -EFAULT;
-
-		if (info.argsz < minsz)
-			return -EINVAL;
-
-		if (parent->ops->get_device_info)
-			ret = parent->ops->get_device_info(vmdev->mdev, &info);
-		else
-			return -EINVAL;
-
-		if (ret)
-			return ret;
-
-		if (parent->ops->reset)
-			info.flags |= VFIO_DEVICE_FLAGS_RESET;
-
-		memcpy(&vmdev->dev_info, &info, sizeof(info));
-
-		return copy_to_user((void __user *)arg, &info, minsz);
-	}
-	case VFIO_DEVICE_GET_REGION_INFO:
-	{
-		struct vfio_region_info info;
-		struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
-		u16 cap_type_id = 0;
-		void *cap_type = NULL;
-
-		minsz = offsetofend(struct vfio_region_info, offset);
-
-		if (copy_from_user(&info, (void __user *)arg, minsz))
-			return -EFAULT;
-
-		if (info.argsz < minsz)
-			return -EINVAL;
-
-		if (parent->ops->get_region_info)
-			ret = parent->ops->get_region_info(vmdev->mdev, &info,
-						       &cap_type_id, &cap_type);
-		else
-			return -EINVAL;
-
-		if (ret)
-			return ret;
-
-		if ((info.flags & VFIO_REGION_INFO_FLAG_CAPS) && cap_type) {
-			switch (cap_type_id) {
-			case VFIO_REGION_INFO_CAP_SPARSE_MMAP:
-				ret = sparse_mmap_cap(&caps, cap_type);
-				if (ret)
-					return ret;
-				break;
-
-			case VFIO_REGION_INFO_CAP_TYPE:
-				ret = region_type_cap(&caps, cap_type);
-				if (ret)
-					return ret;
-				break;
-			default:
-				return -EINVAL;
-			}
-		}
-
-		if (caps.size) {
-			if (info.argsz < sizeof(info) + caps.size) {
-				info.argsz = sizeof(info) + caps.size;
-				info.cap_offset = 0;
-			} else {
-				vfio_info_cap_shift(&caps, sizeof(info));
-				if (copy_to_user((void __user *)arg +
-							sizeof(info), caps.buf,
-							caps.size)) {
-					kfree(caps.buf);
-					return -EFAULT;
-				}
-				info.cap_offset = sizeof(info);
-			}
-			kfree(caps.buf);
-		}
-
-		return copy_to_user((void __user *)arg, &info, minsz);
-	}
-	case VFIO_DEVICE_GET_IRQ_INFO:
-	{
-		struct vfio_irq_info info;
-
-		minsz = offsetofend(struct vfio_irq_info, count);
-
-		if (copy_from_user(&info, (void __user *)arg, minsz))
-			return -EFAULT;
-
-		if ((info.argsz < minsz) ||
-		    (info.index >= vmdev->dev_info.num_irqs))
-			return -EINVAL;
-
-		if (parent->ops->get_irq_info)
-			ret = parent->ops->get_irq_info(vmdev->mdev, &info);
-		else
-			return -EINVAL;
-
-		if (ret)
-			return ret;
-
-		if (info.count == -1)
-			return -EINVAL;
-
-		return copy_to_user((void __user *)arg, &info, minsz);
-	}
-	case VFIO_DEVICE_SET_IRQS:
-	{
-		struct vfio_irq_set hdr;
-		u8 *data = NULL, *ptr = NULL;
-
-		minsz = offsetofend(struct vfio_irq_set, count);
-
-		if (copy_from_user(&hdr, (void __user *)arg, minsz))
-			return -EFAULT;
-
-		if ((hdr.argsz < minsz) ||
-		    (hdr.index >= vmdev->dev_info.num_irqs) ||
-		    (hdr.flags & ~(VFIO_IRQ_SET_DATA_TYPE_MASK |
-				  VFIO_IRQ_SET_ACTION_TYPE_MASK)))
-			return -EINVAL;
-
-		if (!(hdr.flags & VFIO_IRQ_SET_DATA_NONE)) {
-			size_t size;
-
-			if (hdr.flags & VFIO_IRQ_SET_DATA_BOOL)
-				size = sizeof(uint8_t);
-			else if (hdr.flags & VFIO_IRQ_SET_DATA_EVENTFD)
-				size = sizeof(int32_t);
-			else
-				return -EINVAL;
-
-			if (hdr.argsz - minsz < hdr.count * size)
-				return -EINVAL;
-
-			ptr = data = memdup_user((void __user *)(arg + minsz),
-						 hdr.count * size);
-			if (IS_ERR(data))
-				return PTR_ERR(data);
-		}
-
-		if (parent->ops->set_irqs)
-			ret = parent->ops->set_irqs(vmdev->mdev, hdr.flags,
-						    hdr.index, hdr.start,
-						    hdr.count, data);
-		else
-			ret = -EINVAL;
-
-		kfree(ptr);
-		return ret;
-	}
-	case VFIO_DEVICE_RESET:
-	{
-		if (parent->ops->reset)
-			return parent->ops->reset(vmdev->mdev);
-
-		return -EINVAL;
-	}
-	}
-	return -ENOTTY;
+	struct mdev_device *mdev = vmdev->mdev;
+	struct mdev_host *host = dev_to_host(mdev->dev.parent);
+
+	if (host->ops->ioctl)
+		return host->ops->ioctl(mdev, cmd, arg);
+
+	return -ENODEV;
 }
 
 static ssize_t vfio_mdev_read(void *device_data, char __user *buf,
@@ -268,63 +67,12 @@ static ssize_t vfio_mdev_read(void *device_data, char __user *buf,
 {
 	struct vfio_mdev *vmdev = device_data;
 	struct mdev_device *mdev = vmdev->mdev;
-	struct parent_device *parent = mdev->parent;
-	unsigned int done = 0;
-	int ret;
-
-	if (!parent->ops->read)
-		return -EINVAL;
-
-	while (count) {
-		size_t filled;
-
-		if (count >= 4 && !(*ppos % 4)) {
-			u32 val;
-
-			ret = parent->ops->read(mdev, (char *)&val, sizeof(val),
-						*ppos);
-			if (ret <= 0)
-				goto read_err;
-
-			if (copy_to_user(buf, &val, sizeof(val)))
-				goto read_err;
-
-			filled = 4;
-		} else if (count >= 2 && !(*ppos % 2)) {
-			u16 val;
-
-			ret = parent->ops->read(mdev, (char *)&val, sizeof(val),
-						*ppos);
-			if (ret <= 0)
-				goto read_err;
-
-			if (copy_to_user(buf, &val, sizeof(val)))
-				goto read_err;
-
-			filled = 2;
-		} else {
-			u8 val;
-
-			ret = parent->ops->read(mdev, &val, sizeof(val), *ppos);
-			if (ret <= 0)
-				goto read_err;
+	struct mdev_host *host = dev_to_host(mdev->dev.parent);
 
-			if (copy_to_user(buf, &val, sizeof(val)))
-				goto read_err;
+	if (host->ops->read)
+		return host->ops->read(mdev, buf, count, ppos);
 
-			filled = 1;
-		}
-
-		count -= filled;
-		done += filled;
-		*ppos += filled;
-		buf += filled;
-	}
-
-	return done;
-
-read_err:
-	return -EFAULT;
+	return -ENODEV;
 }
 
 static ssize_t vfio_mdev_write(void *device_data, const char __user *buf,
@@ -332,75 +80,24 @@ static ssize_t vfio_mdev_write(void *device_data, const char __user *buf,
 {
 	struct vfio_mdev *vmdev = device_data;
 	struct mdev_device *mdev = vmdev->mdev;
-	struct parent_device *parent = mdev->parent;
-	unsigned int done = 0;
-	int ret;
-
-	if (!parent->ops->write)
-		return -EINVAL;
-
-	while (count) {
-		size_t filled;
-
-		if (count >= 4 && !(*ppos % 4)) {
-			u32 val;
-
-			if (copy_from_user(&val, buf, sizeof(val)))
-				goto write_err;
-
-			ret = parent->ops->write(mdev, (char *)&val,
-						 sizeof(val), *ppos);
-			if (ret <= 0)
-				goto write_err;
-
-			filled = 4;
-		} else if (count >= 2 && !(*ppos % 2)) {
-			u16 val;
-
-			if (copy_from_user(&val, buf, sizeof(val)))
-				goto write_err;
-
-			ret = parent->ops->write(mdev, (char *)&val,
-						 sizeof(val), *ppos);
-			if (ret <= 0)
-				goto write_err;
-
-			filled = 2;
-		} else {
-			u8 val;
+	struct mdev_host *host = dev_to_host(mdev->dev.parent);
 
-			if (copy_from_user(&val, buf, sizeof(val)))
-				goto write_err;
+	if (host->ops->write)
+		return host->ops->write(mdev, buf, count, ppos);
 
-			ret = parent->ops->write(mdev, &val, sizeof(val),
-						 *ppos);
-			if (ret <= 0)
-				goto write_err;
-
-			filled = 1;
-		}
-
-		count -= filled;
-		done += filled;
-		*ppos += filled;
-		buf += filled;
-	}
-
-	return done;
-write_err:
-	return -EFAULT;
+	return -ENODEV;
 }
 
 static int vfio_mdev_mmap(void *device_data, struct vm_area_struct *vma)
 {
 	struct vfio_mdev *vmdev = device_data;
 	struct mdev_device *mdev = vmdev->mdev;
-	struct parent_device *parent = mdev->parent;
+	struct mdev_host *host = dev_to_host(mdev->dev.parent);
 
-	if (parent->ops->mmap)
-		return parent->ops->mmap(mdev, vma);
+	if (host->ops->mmap)
+		return host->ops->mmap(mdev, vma);
 
-	return -EINVAL;
+	return -ENODEV;
 }
 
 static const struct vfio_device_ops vfio_mdev_dev_ops = {
@@ -413,28 +110,27 @@ static const struct vfio_device_ops vfio_mdev_dev_ops = {
 	.mmap		= vfio_mdev_mmap,
 };
 
-int vfio_mdev_probe(struct device *dev)
+static int vfio_mdev_probe(struct device *dev)
 {
 	struct vfio_mdev *vmdev;
-	struct mdev_device *mdev = to_mdev_device(dev);
+	struct mdev_device *mdev = dev_to_mdev(dev);
 	int ret;
 
 	vmdev = kzalloc(sizeof(*vmdev), GFP_KERNEL);
 	if (IS_ERR(vmdev))
 		return PTR_ERR(vmdev);
 
-	vmdev->mdev = mdev_get_device(mdev);
+	vmdev->mdev = mdev;
 	vmdev->group = mdev->group;
 
 	ret = vfio_add_group_dev(dev, &vfio_mdev_dev_ops, vmdev);
 	if (ret)
 		kfree(vmdev);
 
-	mdev_put_device(mdev);
 	return ret;
 }
 
-void vfio_mdev_remove(struct device *dev)
+static void vfio_mdev_remove(struct device *dev)
 {
 	struct vfio_mdev *vmdev;
 
@@ -442,10 +138,34 @@ void vfio_mdev_remove(struct device *dev)
 	kfree(vmdev);
 }
 
-struct mdev_driver vfio_mdev_driver = {
-	.name	= "vfio_mdev",
-	.probe	= vfio_mdev_probe,
-	.remove	= vfio_mdev_remove,
+static int vfio_mdev_online(struct device *dev)
+{
+	struct mdev_device *mdev = dev_to_mdev(dev);
+	struct mdev_host *host = dev_to_host(mdev->dev.parent);
+
+	if (host->ops->start)
+		return host->ops->start(mdev);
+
+	return -ENOTSUPP;
+}
+
+static int vfio_mdev_offline(struct device *dev)
+{
+	struct mdev_device *mdev = dev_to_mdev(dev);
+	struct mdev_host *host = dev_to_host(mdev->dev.parent);
+
+	if (host->ops->stop)
+		return host->ops->stop(mdev);
+
+	return -ENOTSUPP;
+}
+
+static struct mdev_driver vfio_mdev_driver = {
+	.name		= "vfio_mdev",
+	.probe		= vfio_mdev_probe,
+	.remove		= vfio_mdev_remove,
+	.online		= vfio_mdev_online,
+	.offline	= vfio_mdev_offline,
 };
 
 static int __init vfio_mdev_init(void)

  parent reply	other threads:[~2016-09-20 12:56 UTC|newest]

Thread overview: 162+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-25  3:53 [PATCH v7 0/4] Add Mediated device support Kirti Wankhede
2016-08-25  3:53 ` [Qemu-devel] " Kirti Wankhede
2016-08-25  3:53 ` [PATCH v7 1/4] vfio: Mediated device Core driver Kirti Wankhede
2016-08-25  3:53   ` [Qemu-devel] " Kirti Wankhede
2016-09-08  8:09   ` Jike Song
2016-09-08  8:09     ` [Qemu-devel] " Jike Song
2016-09-08  9:38     ` Neo Jia
2016-09-08  9:38       ` [Qemu-devel] " Neo Jia
2016-09-09  6:26       ` Jike Song
2016-09-09  6:26         ` [Qemu-devel] " Jike Song
2016-09-09 17:48     ` Kirti Wankhede
2016-09-09 17:48       ` [Qemu-devel] " Kirti Wankhede
2016-09-09 18:42       ` Alex Williamson
2016-09-09 18:42         ` [Qemu-devel] " Alex Williamson
2016-09-09 19:55         ` Kirti Wankhede
2016-09-09 19:55           ` [Qemu-devel] " Kirti Wankhede
2016-09-12  5:10           ` Jike Song
2016-09-12  5:10             ` [Qemu-devel] " Jike Song
2016-09-12  7:49             ` Kirti Wankhede
2016-09-12  7:49               ` [Qemu-devel] " Kirti Wankhede
2016-09-12 15:53               ` Alex Williamson
2016-09-12 15:53                 ` [Qemu-devel] " Alex Williamson
2016-09-19  7:08                 ` Jike Song
2016-09-19  7:08                   ` [Qemu-devel] " Jike Song
2016-09-19 17:29                 ` Kirti Wankhede
2016-09-19 17:29                   ` [Qemu-devel] " Kirti Wankhede
2016-09-19 18:11                   ` Alex Williamson
2016-09-19 18:11                     ` [Qemu-devel] " Alex Williamson
2016-09-19 20:09                     ` Kirti Wankhede
2016-09-19 20:09                       ` [Qemu-devel] " Kirti Wankhede
2016-09-19 20:59                       ` Alex Williamson
2016-09-20 12:48   ` Jike Song
2016-09-20 12:48     ` [Qemu-devel] " Jike Song
2016-08-25  3:53 ` [PATCH v7 2/4] vfio: VFIO driver for mediated devices Kirti Wankhede
2016-08-25  3:53   ` [Qemu-devel] " Kirti Wankhede
2016-08-25  9:22   ` Dong Jia
2016-08-25  9:22     ` [Qemu-devel] " Dong Jia
2016-08-26 14:13     ` Kirti Wankhede
2016-08-26 14:13       ` [Qemu-devel] " Kirti Wankhede
2016-09-08  2:38       ` Jike Song
2016-09-08  2:38         ` [Qemu-devel] " Jike Song
2016-09-19 18:22       ` Kirti Wankhede
2016-09-19 18:22         ` Kirti Wankhede
2016-09-19 18:36         ` Alex Williamson
2016-09-19 18:36           ` Alex Williamson
2016-09-19 19:13           ` Kirti Wankhede
2016-09-19 19:13             ` Kirti Wankhede
2016-09-19 20:03             ` Alex Williamson
2016-09-19 20:03               ` Alex Williamson
2016-09-20  2:50               ` Jike Song
2016-09-20 16:24                 ` Alex Williamson
2016-09-21  3:19                   ` Jike Song
2016-09-21  4:51                     ` Alex Williamson
2016-09-21  5:02                       ` Jike Song
2016-09-08  2:45     ` Jike Song
2016-09-08  2:45       ` [Qemu-devel] " Jike Song
2016-09-13  2:35       ` Jike Song
2016-09-13  2:35         ` [Qemu-devel] " Jike Song
2016-09-20  5:48         ` Dong Jia Shi
2016-09-20  6:37           ` Jike Song
2016-09-20  6:37             ` [Qemu-devel] " Jike Song
2016-09-20  5:48         ` Dong Jia Shi
2016-09-20 12:53   ` Jike Song [this message]
2016-09-20 12:53     ` [Qemu-devel] " Jike Song
2016-08-25  3:53 ` [PATCH v7 3/4] vfio iommu: Add support " Kirti Wankhede
2016-08-25  3:53   ` [Qemu-devel] " Kirti Wankhede
2016-08-25  7:29   ` Dong Jia
2016-08-25  7:29     ` [Qemu-devel] " Dong Jia
2016-08-26 13:50     ` Kirti Wankhede
2016-08-26 13:50       ` [Qemu-devel] " Kirti Wankhede
2016-09-29  2:17   ` Jike Song
2016-09-29  2:17     ` [Qemu-devel] " Jike Song
2016-09-29 15:06     ` Kirti Wankhede
2016-09-29 15:06       ` [Qemu-devel] " Kirti Wankhede
2016-09-30  2:58       ` Jike Song
2016-09-30  2:58         ` [Qemu-devel] " Jike Song
2016-09-30  3:10         ` Jike Song
2016-09-30  3:10           ` [Qemu-devel] " Jike Song
2016-09-30 11:44           ` Kirti Wankhede
2016-09-30 11:44             ` [Qemu-devel] " Kirti Wankhede
2016-10-08  7:09             ` Jike Song
2016-10-08  7:09               ` [Qemu-devel] " Jike Song
2016-08-25  3:53 ` [PATCH v7 4/4] docs: Add Documentation for Mediated devices Kirti Wankhede
2016-08-25  3:53   ` [Qemu-devel] " Kirti Wankhede
2016-09-03 16:40   ` Kirti Wankhede
2016-09-03 16:40     ` [Qemu-devel] " Kirti Wankhede
2016-08-30 16:16 ` [PATCH v7 0/4] Add Mediated device support Alex Williamson
2016-08-30 16:16   ` [Qemu-devel] " Alex Williamson
2016-08-31  6:12   ` Tian, Kevin
2016-08-31  6:12     ` [Qemu-devel] " Tian, Kevin
2016-08-31  7:04     ` Jike Song
2016-08-31  7:04       ` [Qemu-devel] " Jike Song
2016-08-31 15:48       ` Alex Williamson
2016-08-31 15:48         ` [Qemu-devel] " Alex Williamson
2016-09-01  4:09         ` Tian, Kevin
2016-09-01  4:09           ` [Qemu-devel] " Tian, Kevin
2016-09-01  4:10         ` Tian, Kevin
2016-09-01  4:10           ` [Qemu-devel] " Tian, Kevin
2016-09-01 18:22         ` Kirti Wankhede
2016-09-01 18:22           ` [Qemu-devel] " Kirti Wankhede
2016-09-01 20:01           ` Alex Williamson
2016-09-01 20:01             ` [Qemu-devel] " Alex Williamson
2016-09-02  6:17             ` Kirti Wankhede
2016-09-02  6:17               ` [Qemu-devel] " Kirti Wankhede
2016-09-01 16:47     ` Michal Privoznik
2016-09-01 16:59       ` Alex Williamson
2016-09-01 16:59         ` [Qemu-devel] " Alex Williamson
2016-09-02  4:48         ` Michal Privoznik
2016-09-02  5:21           ` Kirti Wankhede
2016-09-02 10:05             ` Paolo Bonzini
2016-09-02 17:15               ` Kirti Wankhede
2016-09-02 17:25                 ` Paolo Bonzini
2016-09-02 18:33                   ` Kirti Wankhede
2016-09-02 20:29                     ` [libvirt] " John Ferlan
2016-09-02 20:29                       ` [Qemu-devel] [libvirt] " John Ferlan
2016-09-03 16:31                       ` Kirti Wankhede
2016-09-03 16:31                         ` [Qemu-devel] " Kirti Wankhede
2016-09-06 17:54                         ` [libvirt] [Qemu-devel] " Alex Williamson
2016-09-06 17:54                           ` [Qemu-devel] [libvirt] " Alex Williamson
2016-09-02 21:48                     ` [Qemu-devel] " Paolo Bonzini
2016-09-03 11:56                       ` [libvirt] " John Ferlan
2016-09-03 11:56                         ` [Qemu-devel] [libvirt] " John Ferlan
2016-09-03 13:07                         ` [libvirt] [Qemu-devel] " Paolo Bonzini
2016-09-03 13:07                           ` [Qemu-devel] [libvirt] " Paolo Bonzini
2016-09-03 17:47                           ` Kirti Wankhede
2016-09-03 17:47                             ` [Qemu-devel] " Kirti Wankhede
2016-09-03 16:34                       ` [Qemu-devel] " Kirti Wankhede
2016-09-06 17:40                         ` Alex Williamson
2016-09-06 19:35                           ` Kirti Wankhede
2016-09-06 21:28                             ` Alex Williamson
2016-09-07  8:22                               ` Tian, Kevin
2016-09-07  8:22                                 ` Tian, Kevin
2016-09-07 16:00                                 ` Alex Williamson
2016-09-07 16:15                               ` Kirti Wankhede
2016-09-07 16:44                                 ` Alex Williamson
2016-09-07 18:06                                   ` Kirti Wankhede
2016-09-07 22:13                                     ` Alex Williamson
2016-09-08 18:48                                       ` Kirti Wankhede
2016-09-08 20:51                                         ` Alex Williamson
2016-09-07 18:17                                   ` Neo Jia
2016-09-07 18:27                                     ` Daniel P. Berrange
2016-09-07 18:32                                       ` Neo Jia
2016-09-07  6:48                           ` Tian, Kevin
2016-09-07  6:48                             ` Tian, Kevin
2016-09-02 20:19               ` [libvirt] " John Ferlan
2016-09-02 20:19                 ` [Qemu-devel] [libvirt] " John Ferlan
2016-09-02 21:44                 ` [libvirt] [Qemu-devel] " Paolo Bonzini
2016-09-02 21:44                   ` [Qemu-devel] [libvirt] " Paolo Bonzini
2016-09-02 23:57                   ` [libvirt] [Qemu-devel] " Laine Stump
2016-09-02 23:57                     ` [Qemu-devel] [libvirt] " Laine Stump
2016-09-03 16:49                     ` [libvirt] [Qemu-devel] " Kirti Wankhede
2016-09-03 16:49                       ` [Qemu-devel] [libvirt] " Kirti Wankhede
2016-09-05  7:52                     ` [libvirt] [Qemu-devel] " Paolo Bonzini
2016-09-05  7:52                       ` [Qemu-devel] [libvirt] " Paolo Bonzini
2016-09-03 11:57                   ` [libvirt] [Qemu-devel] " John Ferlan
2016-09-03 11:57                     ` [Qemu-devel] [libvirt] " John Ferlan
2016-09-05  7:54                     ` [libvirt] [Qemu-devel] " Paolo Bonzini
2016-09-05  7:54                       ` [Qemu-devel] [libvirt] " Paolo Bonzini
2016-09-02 17:55         ` [libvirt] [Qemu-devel] " Laine Stump
2016-09-02 17:55           ` [Qemu-devel] [libvirt] " Laine Stump
2016-09-02 19:15           ` Alex Williamson
2016-09-02 19:15             ` [Qemu-devel] " Alex Williamson

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=57E1315C.5070304@intel.com \
    --to=jike.song@intel.com \
    --cc=alex.williamson@redhat.com \
    --cc=bjsdjshi@linux.vnet.ibm.com \
    --cc=cjia@nvidia.com \
    --cc=kevin.tian@intel.com \
    --cc=kraxel@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=kwankhede@nvidia.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.