Merge tag 's390-6.10-1' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux
[sfrench/cifs-2.6.git] / drivers / s390 / crypto / vfio_ap_ops.c
index fc169bc61593955525368f56f5eb1bf3caa414b7..9f76f2d7b66e5854ff42b3ba30aedc1eab5aa864 100644 (file)
@@ -794,10 +794,11 @@ err_put_vdev:
 static void vfio_ap_mdev_link_queue(struct ap_matrix_mdev *matrix_mdev,
                                    struct vfio_ap_queue *q)
 {
-       if (q) {
-               q->matrix_mdev = matrix_mdev;
-               hash_add(matrix_mdev->qtable.queues, &q->mdev_qnode, q->apqn);
-       }
+       if (!q || vfio_ap_mdev_get_queue(matrix_mdev, q->apqn))
+               return;
+
+       q->matrix_mdev = matrix_mdev;
+       hash_add(matrix_mdev->qtable.queues, &q->mdev_qnode, q->apqn);
 }
 
 static void vfio_ap_mdev_link_apqn(struct ap_matrix_mdev *matrix_mdev, int apqn)
@@ -1118,20 +1119,29 @@ static void vfio_ap_mdev_unlink_adapter(struct ap_matrix_mdev *matrix_mdev,
        }
 }
 
-static void vfio_ap_mdev_hot_unplug_adapter(struct ap_matrix_mdev *matrix_mdev,
-                                           unsigned long apid)
+static void vfio_ap_mdev_hot_unplug_adapters(struct ap_matrix_mdev *matrix_mdev,
+                                            unsigned long *apids)
 {
        struct vfio_ap_queue *q, *tmpq;
        struct list_head qlist;
+       unsigned long apid;
+       bool apcb_update = false;
 
        INIT_LIST_HEAD(&qlist);
-       vfio_ap_mdev_unlink_adapter(matrix_mdev, apid, &qlist);
 
-       if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm)) {
-               clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
-               vfio_ap_mdev_update_guest_apcb(matrix_mdev);
+       for_each_set_bit_inv(apid, apids, AP_DEVICES) {
+               vfio_ap_mdev_unlink_adapter(matrix_mdev, apid, &qlist);
+
+               if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm)) {
+                       clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
+                       apcb_update = true;
+               }
        }
 
+       /* Only update apcb if needed to avoid impacting guest */
+       if (apcb_update)
+               vfio_ap_mdev_update_guest_apcb(matrix_mdev);
+
        vfio_ap_mdev_reset_qlist(&qlist);
 
        list_for_each_entry_safe(q, tmpq, &qlist, reset_qnode) {
@@ -1140,6 +1150,16 @@ static void vfio_ap_mdev_hot_unplug_adapter(struct ap_matrix_mdev *matrix_mdev,
        }
 }
 
+static void vfio_ap_mdev_hot_unplug_adapter(struct ap_matrix_mdev *matrix_mdev,
+                                           unsigned long apid)
+{
+       DECLARE_BITMAP(apids, AP_DEVICES);
+
+       bitmap_zero(apids, AP_DEVICES);
+       set_bit_inv(apid, apids);
+       vfio_ap_mdev_hot_unplug_adapters(matrix_mdev, apids);
+}
+
 /**
  * unassign_adapter_store - parses the APID from @buf and clears the
  * corresponding bit in the mediated matrix device's APM
@@ -1300,20 +1320,29 @@ static void vfio_ap_mdev_unlink_domain(struct ap_matrix_mdev *matrix_mdev,
        }
 }
 
-static void vfio_ap_mdev_hot_unplug_domain(struct ap_matrix_mdev *matrix_mdev,
-                                          unsigned long apqi)
+static void vfio_ap_mdev_hot_unplug_domains(struct ap_matrix_mdev *matrix_mdev,
+                                           unsigned long *apqis)
 {
        struct vfio_ap_queue *q, *tmpq;
        struct list_head qlist;
+       unsigned long apqi;
+       bool apcb_update = false;
 
        INIT_LIST_HEAD(&qlist);
-       vfio_ap_mdev_unlink_domain(matrix_mdev, apqi, &qlist);
 
-       if (test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
-               clear_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm);
-               vfio_ap_mdev_update_guest_apcb(matrix_mdev);
+       for_each_set_bit_inv(apqi, apqis, AP_DOMAINS) {
+               vfio_ap_mdev_unlink_domain(matrix_mdev, apqi, &qlist);
+
+               if (test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
+                       clear_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm);
+                       apcb_update = true;
+               }
        }
 
+       /* Only update apcb if needed to avoid impacting guest */
+       if (apcb_update)
+               vfio_ap_mdev_update_guest_apcb(matrix_mdev);
+
        vfio_ap_mdev_reset_qlist(&qlist);
 
        list_for_each_entry_safe(q, tmpq, &qlist, reset_qnode) {
@@ -1322,6 +1351,16 @@ static void vfio_ap_mdev_hot_unplug_domain(struct ap_matrix_mdev *matrix_mdev,
        }
 }
 
+static void vfio_ap_mdev_hot_unplug_domain(struct ap_matrix_mdev *matrix_mdev,
+                                          unsigned long apqi)
+{
+       DECLARE_BITMAP(apqis, AP_DOMAINS);
+
+       bitmap_zero(apqis, AP_DEVICES);
+       set_bit_inv(apqi, apqis);
+       vfio_ap_mdev_hot_unplug_domains(matrix_mdev, apqis);
+}
+
 /**
  * unassign_domain_store - parses the APQI from @buf and clears the
  * corresponding bit in the mediated matrix device's AQM
@@ -1570,6 +1609,158 @@ static ssize_t guest_matrix_show(struct device *dev,
 }
 static DEVICE_ATTR_RO(guest_matrix);
 
+static ssize_t write_ap_bitmap(unsigned long *bitmap, char *buf, int offset, char sep)
+{
+       return sysfs_emit_at(buf, offset, "0x%016lx%016lx%016lx%016lx%c",
+                        bitmap[0], bitmap[1], bitmap[2], bitmap[3], sep);
+}
+
+static ssize_t ap_config_show(struct device *dev, struct device_attribute *attr,
+                             char *buf)
+{
+       struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
+       int idx = 0;
+
+       idx += write_ap_bitmap(matrix_mdev->matrix.apm, buf, idx, ',');
+       idx += write_ap_bitmap(matrix_mdev->matrix.aqm, buf, idx, ',');
+       idx += write_ap_bitmap(matrix_mdev->matrix.adm, buf, idx, '\n');
+
+       return idx;
+}
+
+/* Number of characters needed for a complete hex mask representing the bits in ..  */
+#define AP_DEVICES_STRLEN      (AP_DEVICES / 4 + 3)
+#define AP_DOMAINS_STRLEN      (AP_DOMAINS / 4 + 3)
+#define AP_CONFIG_STRLEN       (AP_DEVICES_STRLEN + 2 * AP_DOMAINS_STRLEN)
+
+static int parse_bitmap(char **strbufptr, unsigned long *bitmap, int nbits)
+{
+       char *curmask;
+
+       curmask = strsep(strbufptr, ",\n");
+       if (!curmask)
+               return -EINVAL;
+
+       bitmap_clear(bitmap, 0, nbits);
+       return ap_hex2bitmap(curmask, bitmap, nbits);
+}
+
+static int ap_matrix_overflow_check(struct ap_matrix_mdev *matrix_mdev)
+{
+       unsigned long bit;
+
+       for_each_set_bit_inv(bit, matrix_mdev->matrix.apm, AP_DEVICES) {
+               if (bit > matrix_mdev->matrix.apm_max)
+                       return -ENODEV;
+       }
+
+       for_each_set_bit_inv(bit, matrix_mdev->matrix.aqm, AP_DOMAINS) {
+               if (bit > matrix_mdev->matrix.aqm_max)
+                       return -ENODEV;
+       }
+
+       for_each_set_bit_inv(bit, matrix_mdev->matrix.adm, AP_DOMAINS) {
+               if (bit > matrix_mdev->matrix.adm_max)
+                       return -ENODEV;
+       }
+
+       return 0;
+}
+
+static void ap_matrix_copy(struct ap_matrix *dst, struct ap_matrix *src)
+{
+       /* This check works around false positive gcc -Wstringop-overread */
+       if (!src)
+               return;
+
+       bitmap_copy(dst->apm, src->apm, AP_DEVICES);
+       bitmap_copy(dst->aqm, src->aqm, AP_DOMAINS);
+       bitmap_copy(dst->adm, src->adm, AP_DOMAINS);
+}
+
+static ssize_t ap_config_store(struct device *dev, struct device_attribute *attr,
+                              const char *buf, size_t count)
+{
+       struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
+       struct ap_matrix m_new, m_old, m_added, m_removed;
+       DECLARE_BITMAP(apm_filtered, AP_DEVICES);
+       unsigned long newbit;
+       char *newbuf, *rest;
+       int rc = count;
+       bool do_update;
+
+       newbuf = kstrndup(buf, AP_CONFIG_STRLEN, GFP_KERNEL);
+       if (!newbuf)
+               return -ENOMEM;
+       rest = newbuf;
+
+       mutex_lock(&ap_perms_mutex);
+       get_update_locks_for_mdev(matrix_mdev);
+
+       /* Save old state */
+       ap_matrix_copy(&m_old, &matrix_mdev->matrix);
+       if (parse_bitmap(&rest, m_new.apm, AP_DEVICES) ||
+           parse_bitmap(&rest, m_new.aqm, AP_DOMAINS) ||
+           parse_bitmap(&rest, m_new.adm, AP_DOMAINS)) {
+               rc = -EINVAL;
+               goto out;
+       }
+
+       bitmap_andnot(m_removed.apm, m_old.apm, m_new.apm, AP_DEVICES);
+       bitmap_andnot(m_removed.aqm, m_old.aqm, m_new.aqm, AP_DOMAINS);
+       bitmap_andnot(m_added.apm, m_new.apm, m_old.apm, AP_DEVICES);
+       bitmap_andnot(m_added.aqm, m_new.aqm, m_old.aqm, AP_DOMAINS);
+
+       /* Need new bitmaps in matrix_mdev for validation */
+       ap_matrix_copy(&matrix_mdev->matrix, &m_new);
+
+       /* Ensure new state is valid, else undo new state */
+       rc = vfio_ap_mdev_validate_masks(matrix_mdev);
+       if (rc) {
+               ap_matrix_copy(&matrix_mdev->matrix, &m_old);
+               goto out;
+       }
+       rc = ap_matrix_overflow_check(matrix_mdev);
+       if (rc) {
+               ap_matrix_copy(&matrix_mdev->matrix, &m_old);
+               goto out;
+       }
+       rc = count;
+
+       /* Need old bitmaps in matrix_mdev for unplug/unlink */
+       ap_matrix_copy(&matrix_mdev->matrix, &m_old);
+
+       /* Unlink removed adapters/domains */
+       vfio_ap_mdev_hot_unplug_adapters(matrix_mdev, m_removed.apm);
+       vfio_ap_mdev_hot_unplug_domains(matrix_mdev, m_removed.aqm);
+
+       /* Need new bitmaps in matrix_mdev for linking new adapters/domains */
+       ap_matrix_copy(&matrix_mdev->matrix, &m_new);
+
+       /* Link newly added adapters */
+       for_each_set_bit_inv(newbit, m_added.apm, AP_DEVICES)
+               vfio_ap_mdev_link_adapter(matrix_mdev, newbit);
+
+       for_each_set_bit_inv(newbit, m_added.aqm, AP_DOMAINS)
+               vfio_ap_mdev_link_domain(matrix_mdev, newbit);
+
+       /* filter resources not bound to vfio-ap */
+       do_update = vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered);
+       do_update |= vfio_ap_mdev_filter_cdoms(matrix_mdev);
+
+       /* Apply changes to shadow apbc if things changed */
+       if (do_update) {
+               vfio_ap_mdev_update_guest_apcb(matrix_mdev);
+               reset_queues_for_apids(matrix_mdev, apm_filtered);
+       }
+out:
+       release_update_locks_for_mdev(matrix_mdev);
+       mutex_unlock(&ap_perms_mutex);
+       kfree(newbuf);
+       return rc;
+}
+static DEVICE_ATTR_RW(ap_config);
+
 static struct attribute *vfio_ap_mdev_attrs[] = {
        &dev_attr_assign_adapter.attr,
        &dev_attr_unassign_adapter.attr,
@@ -1577,6 +1768,7 @@ static struct attribute *vfio_ap_mdev_attrs[] = {
        &dev_attr_unassign_domain.attr,
        &dev_attr_assign_control_domain.attr,
        &dev_attr_unassign_control_domain.attr,
+       &dev_attr_ap_config.attr,
        &dev_attr_control_domains.attr,
        &dev_attr_matrix.attr,
        &dev_attr_guest_matrix.attr,