]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
s390/vfio-ap: realize the VFIO_DEVICE_SET_IRQS ioctl
authorTony Krowiak <akrowiak@linux.ibm.com>
Tue, 30 May 2023 22:35:37 +0000 (18:35 -0400)
committerAlexander Gordeev <agordeev@linux.ibm.com>
Tue, 6 Jun 2023 11:42:07 +0000 (13:42 +0200)
Realize the VFIO_DEVICE_SET_IRQS ioctl to set an eventfd file descriptor
to be used by the vfio_ap device driver to signal a device request to
userspace.

Signed-off-by: Tony Krowiak <akrowiak@linux.ibm.com>
Reviewed-by: Cédric Le Goater <clg@redhat.com>
Reviewed-by: Matthew Rosato <mjrosato@linux.ibm.com>
Tested-by: Matthew Rosato <mjrosato@linux.ibm.com>
Link: https://lore.kernel.org/r/20230530223538.279198-3-akrowiak@linux.ibm.com
Signed-off-by: Alexander Gordeev <agordeev@linux.ibm.com>
drivers/s390/crypto/vfio_ap_ops.c
drivers/s390/crypto/vfio_ap_private.h

index 35cd90eee93779ddb291eaa17c9f6cd9af31dc2c..44f159136891a381e9add27d76bad36941447b64 100644 (file)
@@ -716,6 +716,7 @@ static int vfio_ap_mdev_probe(struct mdev_device *mdev)
        ret = vfio_register_emulated_iommu_dev(&matrix_mdev->vdev);
        if (ret)
                goto err_put_vdev;
+       matrix_mdev->req_trigger = NULL;
        dev_set_drvdata(&mdev->dev, matrix_mdev);
        mutex_lock(&matrix_dev->mdevs_lock);
        list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
@@ -1780,6 +1781,85 @@ static ssize_t vfio_ap_get_irq_info(unsigned long arg)
        return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
 }
 
+static int vfio_ap_irq_set_init(struct vfio_irq_set *irq_set, unsigned long arg)
+{
+       int ret;
+       size_t data_size;
+       unsigned long minsz;
+
+       minsz = offsetofend(struct vfio_irq_set, count);
+
+       if (copy_from_user(irq_set, (void __user *)arg, minsz))
+               return -EFAULT;
+
+       ret = vfio_set_irqs_validate_and_prepare(irq_set, 1, VFIO_AP_NUM_IRQS,
+                                                &data_size);
+       if (ret)
+               return ret;
+
+       if (!(irq_set->flags & VFIO_IRQ_SET_ACTION_TRIGGER))
+               return -EINVAL;
+
+       return 0;
+}
+
+static int vfio_ap_set_request_irq(struct ap_matrix_mdev *matrix_mdev,
+                                  unsigned long arg)
+{
+       s32 fd;
+       void __user *data;
+       unsigned long minsz;
+       struct eventfd_ctx *req_trigger;
+
+       minsz = offsetofend(struct vfio_irq_set, count);
+       data = (void __user *)(arg + minsz);
+
+       if (get_user(fd, (s32 __user *)data))
+               return -EFAULT;
+
+       if (fd == -1) {
+               if (matrix_mdev->req_trigger)
+                       eventfd_ctx_put(matrix_mdev->req_trigger);
+               matrix_mdev->req_trigger = NULL;
+       } else if (fd >= 0) {
+               req_trigger = eventfd_ctx_fdget(fd);
+               if (IS_ERR(req_trigger))
+                       return PTR_ERR(req_trigger);
+
+               if (matrix_mdev->req_trigger)
+                       eventfd_ctx_put(matrix_mdev->req_trigger);
+
+               matrix_mdev->req_trigger = req_trigger;
+       } else {
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+static int vfio_ap_set_irqs(struct ap_matrix_mdev *matrix_mdev,
+                           unsigned long arg)
+{
+       int ret;
+       struct vfio_irq_set irq_set;
+
+       ret = vfio_ap_irq_set_init(&irq_set, arg);
+       if (ret)
+               return ret;
+
+       switch (irq_set.flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
+       case VFIO_IRQ_SET_DATA_EVENTFD:
+               switch (irq_set.index) {
+               case VFIO_AP_REQ_IRQ_INDEX:
+                       return vfio_ap_set_request_irq(matrix_mdev, arg);
+               default:
+                       return -EINVAL;
+               }
+       default:
+               return -EINVAL;
+       }
+}
+
 static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,
                                    unsigned int cmd, unsigned long arg)
 {
@@ -1798,6 +1878,9 @@ static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,
        case VFIO_DEVICE_GET_IRQ_INFO:
                        ret = vfio_ap_get_irq_info(arg);
                        break;
+       case VFIO_DEVICE_SET_IRQS:
+               ret = vfio_ap_set_irqs(matrix_mdev, arg);
+               break;
        default:
                ret = -EOPNOTSUPP;
                break;
index 976a65f32e7d10419131fe1e447b647e0569c069..4642bbdbd1b27d4cd48dfabef2e2bb512cbc38a2 100644 (file)
@@ -15,6 +15,7 @@
 #include <linux/types.h>
 #include <linux/mdev.h>
 #include <linux/delay.h>
+#include <linux/eventfd.h>
 #include <linux/mutex.h>
 #include <linux/kvm_host.h>
 #include <linux/vfio.h>
@@ -103,6 +104,7 @@ struct ap_queue_table {
  *             PQAP(AQIC) instruction.
  * @mdev:      the mediated device
  * @qtable:    table of queues (struct vfio_ap_queue) assigned to the mdev
+ * @req_trigger eventfd ctx for signaling userspace to return a device
  * @apm_add:   bitmap of APIDs added to the host's AP configuration
  * @aqm_add:   bitmap of APQIs added to the host's AP configuration
  * @adm_add:   bitmap of control domain numbers added to the host's AP
@@ -117,6 +119,7 @@ struct ap_matrix_mdev {
        crypto_hook pqap_hook;
        struct mdev_device *mdev;
        struct ap_queue_table qtable;
+       struct eventfd_ctx *req_trigger;
        DECLARE_BITMAP(apm_add, AP_DEVICES);
        DECLARE_BITMAP(aqm_add, AP_DOMAINS);
        DECLARE_BITMAP(adm_add, AP_DOMAINS);