^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright IBM Corp. 2001, 2018
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Author(s): Robert Burroughs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Eric Rossman (edrossma@us.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Cornelia Huck <cornelia.huck@de.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Ralph Wuerthner <rwuerthn@de.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Multiple device nodes: Harald Freudenberger <freude@linux.ibm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/hw_random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/cdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <asm/debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <asm/trace/zcrypt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include "zcrypt_api.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include "zcrypt_debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include "zcrypt_msgtype6.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include "zcrypt_msgtype50.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include "zcrypt_ccamisc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include "zcrypt_ep11misc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * Module description.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) MODULE_AUTHOR("IBM Corporation");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) MODULE_DESCRIPTION("Cryptographic Coprocessor interface, " \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) "Copyright IBM Corp. 2001, 2012");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * zcrypt tracepoint functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) EXPORT_TRACEPOINT_SYMBOL(s390_zcrypt_rep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static int zcrypt_hwrng_seed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) module_param_named(hwrng_seed, zcrypt_hwrng_seed, int, 0440);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) MODULE_PARM_DESC(hwrng_seed, "Turn on/off hwrng auto seed, default is 1 (on).");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) DEFINE_SPINLOCK(zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) LIST_HEAD(zcrypt_card_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int zcrypt_device_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static atomic_t zcrypt_open_count = ATOMIC_INIT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static atomic_t zcrypt_rescan_count = ATOMIC_INIT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) atomic_t zcrypt_rescan_req = ATOMIC_INIT(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) EXPORT_SYMBOL(zcrypt_rescan_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static LIST_HEAD(zcrypt_ops_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) /* Zcrypt related debug feature stuff. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) debug_info_t *zcrypt_dbf_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * Process a rescan of the transport layer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * Returns 1, if the rescan has been processed, otherwise 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static inline int zcrypt_process_rescan(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (atomic_read(&zcrypt_rescan_req)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) atomic_set(&zcrypt_rescan_req, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) atomic_inc(&zcrypt_rescan_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) ap_bus_force_rescan();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) ZCRYPT_DBF(DBF_INFO, "rescan count=%07d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) atomic_inc_return(&zcrypt_rescan_count));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) void zcrypt_msgtype_register(struct zcrypt_ops *zops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) list_add_tail(&zops->list, &zcrypt_ops_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) void zcrypt_msgtype_unregister(struct zcrypt_ops *zops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) list_del_init(&zops->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct zcrypt_ops *zcrypt_msgtype(unsigned char *name, int variant)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct zcrypt_ops *zops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) list_for_each_entry(zops, &zcrypt_ops_list, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if ((zops->variant == variant) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) (!strncmp(zops->name, name, sizeof(zops->name))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return zops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) EXPORT_SYMBOL(zcrypt_msgtype);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * Multi device nodes extension functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct zcdn_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static struct class *zcrypt_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static dev_t zcrypt_devt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static struct cdev zcrypt_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct zcdn_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct device device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct ap_perms perms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) #define to_zcdn_dev(x) container_of((x), struct zcdn_device, device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) #define ZCDN_MAX_NAME 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static int zcdn_create(const char *name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static int zcdn_destroy(const char *name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * Find zcdn device by name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * Returns reference to the zcdn device which needs to be released
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * with put_device() after use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) static inline struct zcdn_device *find_zcdndev_by_name(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) struct device *dev = class_find_device_by_name(zcrypt_class, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return dev ? to_zcdn_dev(dev) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * Find zcdn device by devt value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * Returns reference to the zcdn device which needs to be released
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * with put_device() after use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static inline struct zcdn_device *find_zcdndev_by_devt(dev_t devt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct device *dev = class_find_device_by_devt(zcrypt_class, devt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return dev ? to_zcdn_dev(dev) : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static ssize_t ioctlmask_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) int i, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) struct zcdn_device *zcdndev = to_zcdn_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (mutex_lock_interruptible(&ap_perms_mutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) buf[0] = '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) buf[1] = 'x';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) for (i = 0; i < sizeof(zcdndev->perms.ioctlm) / sizeof(long); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) snprintf(buf + 2 + 2 * i * sizeof(long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) PAGE_SIZE - 2 - 2 * i * sizeof(long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) "%016lx", zcdndev->perms.ioctlm[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) buf[2 + 2 * i * sizeof(long)] = '\n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) buf[2 + 2 * i * sizeof(long) + 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) rc = 2 + 2 * i * sizeof(long) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) mutex_unlock(&ap_perms_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static ssize_t ioctlmask_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) struct zcdn_device *zcdndev = to_zcdn_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) rc = ap_parse_mask_str(buf, zcdndev->perms.ioctlm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) AP_IOCTLS, &ap_perms_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static DEVICE_ATTR_RW(ioctlmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static ssize_t apmask_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) int i, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct zcdn_device *zcdndev = to_zcdn_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (mutex_lock_interruptible(&ap_perms_mutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) buf[0] = '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) buf[1] = 'x';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) for (i = 0; i < sizeof(zcdndev->perms.apm) / sizeof(long); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) snprintf(buf + 2 + 2 * i * sizeof(long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) PAGE_SIZE - 2 - 2 * i * sizeof(long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) "%016lx", zcdndev->perms.apm[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) buf[2 + 2 * i * sizeof(long)] = '\n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) buf[2 + 2 * i * sizeof(long) + 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) rc = 2 + 2 * i * sizeof(long) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) mutex_unlock(&ap_perms_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static ssize_t apmask_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) struct zcdn_device *zcdndev = to_zcdn_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) rc = ap_parse_mask_str(buf, zcdndev->perms.apm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) AP_DEVICES, &ap_perms_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static DEVICE_ATTR_RW(apmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) static ssize_t aqmask_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) int i, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) struct zcdn_device *zcdndev = to_zcdn_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (mutex_lock_interruptible(&ap_perms_mutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) buf[0] = '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) buf[1] = 'x';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) for (i = 0; i < sizeof(zcdndev->perms.aqm) / sizeof(long); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) snprintf(buf + 2 + 2 * i * sizeof(long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) PAGE_SIZE - 2 - 2 * i * sizeof(long),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) "%016lx", zcdndev->perms.aqm[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) buf[2 + 2 * i * sizeof(long)] = '\n';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) buf[2 + 2 * i * sizeof(long) + 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) rc = 2 + 2 * i * sizeof(long) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) mutex_unlock(&ap_perms_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static ssize_t aqmask_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) struct zcdn_device *zcdndev = to_zcdn_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) rc = ap_parse_mask_str(buf, zcdndev->perms.aqm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) AP_DOMAINS, &ap_perms_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) static DEVICE_ATTR_RW(aqmask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) static struct attribute *zcdn_dev_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) &dev_attr_ioctlmask.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) &dev_attr_apmask.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) &dev_attr_aqmask.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) static struct attribute_group zcdn_dev_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) .attrs = zcdn_dev_attrs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static const struct attribute_group *zcdn_dev_attr_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) &zcdn_dev_attr_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) static ssize_t zcdn_create_store(struct class *class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) struct class_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) char name[ZCDN_MAX_NAME];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) strncpy(name, skip_spaces(buf), sizeof(name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) name[sizeof(name) - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) rc = zcdn_create(strim(name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return rc ? rc : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) static const struct class_attribute class_attr_zcdn_create =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) __ATTR(create, 0600, NULL, zcdn_create_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) static ssize_t zcdn_destroy_store(struct class *class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) struct class_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) char name[ZCDN_MAX_NAME];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) strncpy(name, skip_spaces(buf), sizeof(name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) name[sizeof(name) - 1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) rc = zcdn_destroy(strim(name));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) return rc ? rc : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) static const struct class_attribute class_attr_zcdn_destroy =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) __ATTR(destroy, 0600, NULL, zcdn_destroy_store);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static void zcdn_device_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) struct zcdn_device *zcdndev = to_zcdn_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) ZCRYPT_DBF(DBF_INFO, "releasing zcdn device %d:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) MAJOR(dev->devt), MINOR(dev->devt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) kfree(zcdndev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) static int zcdn_create(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) dev_t devt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) int i, rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) char nodename[ZCDN_MAX_NAME];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) struct zcdn_device *zcdndev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (mutex_lock_interruptible(&ap_perms_mutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) /* check if device node with this name already exists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (name[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) zcdndev = find_zcdndev_by_name(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (zcdndev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) put_device(&zcdndev->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) rc = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) goto unlockout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) /* find an unused minor number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) zcdndev = find_zcdndev_by_devt(devt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (zcdndev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) put_device(&zcdndev->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (i == ZCRYPT_MAX_MINOR_NODES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) rc = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) goto unlockout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) /* alloc and prepare a new zcdn device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) zcdndev = kzalloc(sizeof(*zcdndev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (!zcdndev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) goto unlockout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) zcdndev->device.release = zcdn_device_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) zcdndev->device.class = zcrypt_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) zcdndev->device.devt = devt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) zcdndev->device.groups = zcdn_dev_attr_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (name[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) strncpy(nodename, name, sizeof(nodename));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) snprintf(nodename, sizeof(nodename),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) ZCRYPT_NAME "_%d", (int) MINOR(devt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) nodename[sizeof(nodename)-1] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) if (dev_set_name(&zcdndev->device, nodename)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) goto unlockout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) rc = device_register(&zcdndev->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) put_device(&zcdndev->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) goto unlockout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) ZCRYPT_DBF(DBF_INFO, "created zcdn device %d:%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) MAJOR(devt), MINOR(devt));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) unlockout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) mutex_unlock(&ap_perms_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) static int zcdn_destroy(const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) struct zcdn_device *zcdndev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) if (mutex_lock_interruptible(&ap_perms_mutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) /* try to find this zcdn device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) zcdndev = find_zcdndev_by_name(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) if (!zcdndev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) rc = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) goto unlockout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) * The zcdn device is not hard destroyed. It is subject to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) * reference counting and thus just needs to be unregistered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) put_device(&zcdndev->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) device_unregister(&zcdndev->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) unlockout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) mutex_unlock(&ap_perms_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) static void zcdn_destroy_all(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) dev_t devt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) struct zcdn_device *zcdndev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) mutex_lock(&ap_perms_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) for (i = 0; i < ZCRYPT_MAX_MINOR_NODES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) devt = MKDEV(MAJOR(zcrypt_devt), MINOR(zcrypt_devt) + i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) zcdndev = find_zcdndev_by_devt(devt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) if (zcdndev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) put_device(&zcdndev->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) device_unregister(&zcdndev->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) mutex_unlock(&ap_perms_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) * zcrypt_read (): Not supported beyond zcrypt 1.3.1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) * This function is not supported beyond zcrypt 1.3.1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) static ssize_t zcrypt_read(struct file *filp, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) size_t count, loff_t *f_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) * zcrypt_write(): Not allowed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) * Write is is not allowed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) static ssize_t zcrypt_write(struct file *filp, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) size_t count, loff_t *f_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) * zcrypt_open(): Count number of users.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) * Device open function to count number of users.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) static int zcrypt_open(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) struct ap_perms *perms = &ap_perms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) if (filp->f_inode->i_cdev == &zcrypt_cdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) struct zcdn_device *zcdndev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) if (mutex_lock_interruptible(&ap_perms_mutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) return -ERESTARTSYS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) /* find returns a reference, no get_device() needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) mutex_unlock(&ap_perms_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if (zcdndev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) perms = &zcdndev->perms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) filp->private_data = (void *) perms;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) atomic_inc(&zcrypt_open_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) return stream_open(inode, filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) * zcrypt_release(): Count number of users.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) * Device close function to count number of users.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) static int zcrypt_release(struct inode *inode, struct file *filp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) if (filp->f_inode->i_cdev == &zcrypt_cdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) struct zcdn_device *zcdndev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) mutex_lock(&ap_perms_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) zcdndev = find_zcdndev_by_devt(filp->f_inode->i_rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) mutex_unlock(&ap_perms_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (zcdndev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) /* 2 puts here: one for find, one for open */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) put_device(&zcdndev->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) put_device(&zcdndev->device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) atomic_dec(&zcrypt_open_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) static inline int zcrypt_check_ioctl(struct ap_perms *perms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) unsigned int cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) int rc = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) int ioctlnr = (cmd & _IOC_NRMASK) >> _IOC_NRSHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) if (ioctlnr > 0 && ioctlnr < AP_IOCTLS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) if (test_bit_inv(ioctlnr, perms->ioctlm))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) ZCRYPT_DBF(DBF_WARN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) "ioctl check failed: ioctlnr=0x%04x rc=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) ioctlnr, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) static inline bool zcrypt_check_card(struct ap_perms *perms, int card)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) return test_bit_inv(card, perms->apm) ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) static inline bool zcrypt_check_queue(struct ap_perms *perms, int queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) return test_bit_inv(queue, perms->aqm) ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) static inline struct zcrypt_queue *zcrypt_pick_queue(struct zcrypt_card *zc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) struct zcrypt_queue *zq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) struct module **pmod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) unsigned int weight)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) if (!zq || !try_module_get(zq->queue->ap_dev.drv->driver.owner))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) zcrypt_queue_get(zq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) get_device(&zq->queue->ap_dev.device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) atomic_add(weight, &zc->load);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) atomic_add(weight, &zq->load);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) zq->request_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) *pmod = zq->queue->ap_dev.drv->driver.owner;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) return zq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) static inline void zcrypt_drop_queue(struct zcrypt_card *zc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) struct zcrypt_queue *zq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) struct module *mod,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) unsigned int weight)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) zq->request_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) atomic_sub(weight, &zc->load);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) atomic_sub(weight, &zq->load);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) put_device(&zq->queue->ap_dev.device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) zcrypt_queue_put(zq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) module_put(mod);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) static inline bool zcrypt_card_compare(struct zcrypt_card *zc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) struct zcrypt_card *pref_zc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) unsigned int weight,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) unsigned int pref_weight)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) if (!pref_zc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) weight += atomic_read(&zc->load);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) pref_weight += atomic_read(&pref_zc->load);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) if (weight == pref_weight)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) return atomic64_read(&zc->card->total_request_count) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) atomic64_read(&pref_zc->card->total_request_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) return weight < pref_weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) static inline bool zcrypt_queue_compare(struct zcrypt_queue *zq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) struct zcrypt_queue *pref_zq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) unsigned int weight,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) unsigned int pref_weight)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) if (!pref_zq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) weight += atomic_read(&zq->load);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) pref_weight += atomic_read(&pref_zq->load);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) if (weight == pref_weight)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) return zq->queue->total_request_count <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) pref_zq->queue->total_request_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) return weight < pref_weight;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) * zcrypt ioctls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) static long zcrypt_rsa_modexpo(struct ap_perms *perms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) struct zcrypt_track *tr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) struct ica_rsa_modexpo *mex)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) struct zcrypt_card *zc, *pref_zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) struct zcrypt_queue *zq, *pref_zq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) struct ap_message ap_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) unsigned int wgt = 0, pref_wgt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) unsigned int func_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) int cpen, qpen, qid = 0, rc = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) trace_s390_zcrypt_req(mex, TP_ICARSAMODEXPO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) ap_init_message(&ap_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) #ifdef CONFIG_ZCRYPT_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) if (tr && tr->fi.cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) ap_msg.fi.cmd = tr->fi.cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) if (mex->outputdatalength < mex->inputdatalength) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) func_code = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) * As long as outputdatalength is big enough, we can set the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) * outputdatalength equal to the inputdatalength, since that is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) * number of bytes we will copy in any case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) mex->outputdatalength = mex->inputdatalength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) rc = get_rsa_modex_fc(mex, &func_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) pref_zc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) pref_zq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) spin_lock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) for_each_zcrypt_card(zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) /* Check for useable accelarator or CCA card */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) if (!zc->online || !zc->card->config ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) !(zc->card->functions & 0x18000000))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) /* Check for size limits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) if (zc->min_mod_size > mex->inputdatalength ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) zc->max_mod_size < mex->inputdatalength)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) /* check if device node has admission for this card */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) if (!zcrypt_check_card(perms, zc->card->id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) /* get weight index of the card device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) wgt = zc->speed_rating[func_code];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) /* penalty if this msg was previously sent via this card */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) cpen = (tr && tr->again_counter && tr->last_qid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) AP_QID_CARD(tr->last_qid) == zc->card->id) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) for_each_zcrypt_queue(zq, zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) /* check if device is useable and eligible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) if (!zq->online || !zq->ops->rsa_modexpo ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) !zq->queue->config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) /* check if device node has admission for this queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) if (!zcrypt_check_queue(perms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) AP_QID_QUEUE(zq->queue->qid)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) /* penalty if the msg was previously sent at this qid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) qpen = (tr && tr->again_counter && tr->last_qid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) tr->last_qid == zq->queue->qid) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) if (!zcrypt_queue_compare(zq, pref_zq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) wgt + cpen + qpen, pref_wgt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) pref_zc = zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) pref_zq = zq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) pref_wgt = wgt + cpen + qpen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) if (!pref_zq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) rc = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) qid = pref_zq->queue->qid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) rc = pref_zq->ops->rsa_modexpo(pref_zq, mex, &ap_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) spin_lock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) ap_release_message(&ap_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) if (tr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) tr->last_rc = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) tr->last_qid = qid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) trace_s390_zcrypt_rep(mex, func_code, rc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) AP_QID_CARD(qid), AP_QID_QUEUE(qid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) static long zcrypt_rsa_crt(struct ap_perms *perms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) struct zcrypt_track *tr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) struct ica_rsa_modexpo_crt *crt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) struct zcrypt_card *zc, *pref_zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) struct zcrypt_queue *zq, *pref_zq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) struct ap_message ap_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) unsigned int wgt = 0, pref_wgt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) unsigned int func_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) int cpen, qpen, qid = 0, rc = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) trace_s390_zcrypt_req(crt, TP_ICARSACRT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) ap_init_message(&ap_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) #ifdef CONFIG_ZCRYPT_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) if (tr && tr->fi.cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) ap_msg.fi.cmd = tr->fi.cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) if (crt->outputdatalength < crt->inputdatalength) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) func_code = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) * As long as outputdatalength is big enough, we can set the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) * outputdatalength equal to the inputdatalength, since that is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) * number of bytes we will copy in any case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) crt->outputdatalength = crt->inputdatalength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) rc = get_rsa_crt_fc(crt, &func_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) pref_zc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) pref_zq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) spin_lock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) for_each_zcrypt_card(zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) /* Check for useable accelarator or CCA card */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) if (!zc->online || !zc->card->config ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) !(zc->card->functions & 0x18000000))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) /* Check for size limits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) if (zc->min_mod_size > crt->inputdatalength ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) zc->max_mod_size < crt->inputdatalength)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) /* check if device node has admission for this card */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) if (!zcrypt_check_card(perms, zc->card->id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) /* get weight index of the card device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) wgt = zc->speed_rating[func_code];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) /* penalty if this msg was previously sent via this card */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) cpen = (tr && tr->again_counter && tr->last_qid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) AP_QID_CARD(tr->last_qid) == zc->card->id) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) for_each_zcrypt_queue(zq, zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) /* check if device is useable and eligible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) if (!zq->online || !zq->ops->rsa_modexpo_crt ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) !zq->queue->config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) /* check if device node has admission for this queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) if (!zcrypt_check_queue(perms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) AP_QID_QUEUE(zq->queue->qid)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) /* penalty if the msg was previously sent at this qid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) qpen = (tr && tr->again_counter && tr->last_qid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) tr->last_qid == zq->queue->qid) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) if (!zcrypt_queue_compare(zq, pref_zq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) wgt + cpen + qpen, pref_wgt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) pref_zc = zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) pref_zq = zq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) pref_wgt = wgt + cpen + qpen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) if (!pref_zq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) rc = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) qid = pref_zq->queue->qid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) rc = pref_zq->ops->rsa_modexpo_crt(pref_zq, crt, &ap_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) spin_lock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) ap_release_message(&ap_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) if (tr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) tr->last_rc = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) tr->last_qid = qid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) trace_s390_zcrypt_rep(crt, func_code, rc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) AP_QID_CARD(qid), AP_QID_QUEUE(qid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) static long _zcrypt_send_cprb(bool userspace, struct ap_perms *perms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) struct zcrypt_track *tr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) struct ica_xcRB *xcRB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) struct zcrypt_card *zc, *pref_zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) struct zcrypt_queue *zq, *pref_zq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) struct ap_message ap_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) unsigned int wgt = 0, pref_wgt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) unsigned int func_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) unsigned short *domain, tdom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) int cpen, qpen, qid = 0, rc = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) trace_s390_zcrypt_req(xcRB, TB_ZSECSENDCPRB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) xcRB->status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) ap_init_message(&ap_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) #ifdef CONFIG_ZCRYPT_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) if (tr && tr->fi.cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) ap_msg.fi.cmd = tr->fi.cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) if (tr && tr->fi.action == AP_FI_ACTION_CCA_AGENT_FF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) ZCRYPT_DBF_WARN("%s fi cmd 0x%04x: forcing invalid agent_ID 'FF'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) __func__, tr->fi.cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) xcRB->agent_ID = 0x4646;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) rc = get_cprb_fc(userspace, xcRB, &ap_msg, &func_code, &domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) * If a valid target domain is set and this domain is NOT a usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) * domain but a control only domain, use the default domain as target.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) tdom = *domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) if (tdom < AP_DOMAINS &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) !ap_test_config_usage_domain(tdom) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) ap_test_config_ctrl_domain(tdom) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) ap_domain_index >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) tdom = ap_domain_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) pref_zc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) pref_zq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) spin_lock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) for_each_zcrypt_card(zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) /* Check for useable CCA card */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) if (!zc->online || !zc->card->config ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) !(zc->card->functions & 0x10000000))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) /* Check for user selected CCA card */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) if (xcRB->user_defined != AUTOSELECT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) xcRB->user_defined != zc->card->id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) /* check if device node has admission for this card */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) if (!zcrypt_check_card(perms, zc->card->id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) /* get weight index of the card device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) wgt = speed_idx_cca(func_code) * zc->speed_rating[SECKEY];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) /* penalty if this msg was previously sent via this card */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) cpen = (tr && tr->again_counter && tr->last_qid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) AP_QID_CARD(tr->last_qid) == zc->card->id) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) for_each_zcrypt_queue(zq, zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) /* check for device useable and eligible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) if (!zq->online ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) !zq->ops->send_cprb ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) !zq->queue->config ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) (tdom != AUTOSEL_DOM &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) tdom != AP_QID_QUEUE(zq->queue->qid)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) /* check if device node has admission for this queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) if (!zcrypt_check_queue(perms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) AP_QID_QUEUE(zq->queue->qid)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) /* penalty if the msg was previously sent at this qid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) qpen = (tr && tr->again_counter && tr->last_qid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) tr->last_qid == zq->queue->qid) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) if (!zcrypt_queue_compare(zq, pref_zq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) wgt + cpen + qpen, pref_wgt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) pref_zc = zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) pref_zq = zq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) pref_wgt = wgt + cpen + qpen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) if (!pref_zq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) rc = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) /* in case of auto select, provide the correct domain */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) qid = pref_zq->queue->qid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) if (*domain == AUTOSEL_DOM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) *domain = AP_QID_QUEUE(qid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) #ifdef CONFIG_ZCRYPT_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) if (tr && tr->fi.action == AP_FI_ACTION_CCA_DOM_INVAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) ZCRYPT_DBF_WARN("%s fi cmd 0x%04x: forcing invalid domain\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) __func__, tr->fi.cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) *domain = 99;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) rc = pref_zq->ops->send_cprb(userspace, pref_zq, xcRB, &ap_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) spin_lock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) ap_release_message(&ap_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) if (tr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) tr->last_rc = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) tr->last_qid = qid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) trace_s390_zcrypt_rep(xcRB, func_code, rc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) AP_QID_CARD(qid), AP_QID_QUEUE(qid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) long zcrypt_send_cprb(struct ica_xcRB *xcRB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) return _zcrypt_send_cprb(false, &ap_perms, NULL, xcRB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) EXPORT_SYMBOL(zcrypt_send_cprb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) static bool is_desired_ep11_card(unsigned int dev_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) unsigned short target_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) struct ep11_target_dev *targets)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) while (target_num-- > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) if (targets->ap_id == dev_id || targets->ap_id == AUTOSEL_AP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) targets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) static bool is_desired_ep11_queue(unsigned int dev_qid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) unsigned short target_num,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) struct ep11_target_dev *targets)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) int card = AP_QID_CARD(dev_qid), dom = AP_QID_QUEUE(dev_qid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) while (target_num-- > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) if ((targets->ap_id == card || targets->ap_id == AUTOSEL_AP) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) (targets->dom_id == dom || targets->dom_id == AUTOSEL_DOM))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) targets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) static long _zcrypt_send_ep11_cprb(bool userspace, struct ap_perms *perms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) struct zcrypt_track *tr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) struct ep11_urb *xcrb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) struct zcrypt_card *zc, *pref_zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) struct zcrypt_queue *zq, *pref_zq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) struct ep11_target_dev *targets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) unsigned short target_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) unsigned int wgt = 0, pref_wgt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) unsigned int func_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) struct ap_message ap_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) int cpen, qpen, qid = 0, rc = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) trace_s390_zcrypt_req(xcrb, TP_ZSENDEP11CPRB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) ap_init_message(&ap_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) #ifdef CONFIG_ZCRYPT_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) if (tr && tr->fi.cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) ap_msg.fi.cmd = tr->fi.cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) target_num = (unsigned short) xcrb->targets_num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) /* empty list indicates autoselect (all available targets) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) targets = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) if (target_num != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) struct ep11_target_dev __user *uptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) targets = kcalloc(target_num, sizeof(*targets), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) if (!targets) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) func_code = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) uptr = (struct ep11_target_dev __force __user *) xcrb->targets;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) if (z_copy_from_user(userspace, targets, uptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) target_num * sizeof(*targets))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) func_code = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) rc = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) rc = get_ep11cprb_fc(userspace, xcrb, &ap_msg, &func_code);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) pref_zc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) pref_zq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) spin_lock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) for_each_zcrypt_card(zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) /* Check for useable EP11 card */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) if (!zc->online || !zc->card->config ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) !(zc->card->functions & 0x04000000))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) /* Check for user selected EP11 card */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) if (targets &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) !is_desired_ep11_card(zc->card->id, target_num, targets))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) /* check if device node has admission for this card */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) if (!zcrypt_check_card(perms, zc->card->id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) /* get weight index of the card device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) wgt = speed_idx_ep11(func_code) * zc->speed_rating[SECKEY];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) /* penalty if this msg was previously sent via this card */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) cpen = (tr && tr->again_counter && tr->last_qid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) AP_QID_CARD(tr->last_qid) == zc->card->id) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) TRACK_AGAIN_CARD_WEIGHT_PENALTY : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) if (!zcrypt_card_compare(zc, pref_zc, wgt + cpen, pref_wgt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) for_each_zcrypt_queue(zq, zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) /* check if device is useable and eligible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) if (!zq->online ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) !zq->ops->send_ep11_cprb ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) !zq->queue->config ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) (targets &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) !is_desired_ep11_queue(zq->queue->qid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) target_num, targets)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) /* check if device node has admission for this queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) if (!zcrypt_check_queue(perms,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) AP_QID_QUEUE(zq->queue->qid)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) /* penalty if the msg was previously sent at this qid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) qpen = (tr && tr->again_counter && tr->last_qid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) tr->last_qid == zq->queue->qid) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) TRACK_AGAIN_QUEUE_WEIGHT_PENALTY : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) if (!zcrypt_queue_compare(zq, pref_zq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) wgt + cpen + qpen, pref_wgt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) pref_zc = zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) pref_zq = zq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) pref_wgt = wgt + cpen + qpen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) if (!pref_zq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) rc = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) qid = pref_zq->queue->qid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) rc = pref_zq->ops->send_ep11_cprb(userspace, pref_zq, xcrb, &ap_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) spin_lock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) kfree(targets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) ap_release_message(&ap_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) if (tr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) tr->last_rc = rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) tr->last_qid = qid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) trace_s390_zcrypt_rep(xcrb, func_code, rc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) AP_QID_CARD(qid), AP_QID_QUEUE(qid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) long zcrypt_send_ep11_cprb(struct ep11_urb *xcrb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) return _zcrypt_send_ep11_cprb(false, &ap_perms, NULL, xcrb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) EXPORT_SYMBOL(zcrypt_send_ep11_cprb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) static long zcrypt_rng(char *buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) struct zcrypt_card *zc, *pref_zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) struct zcrypt_queue *zq, *pref_zq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) unsigned int wgt = 0, pref_wgt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) unsigned int func_code;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) struct ap_message ap_msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) unsigned int domain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) int qid = 0, rc = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) struct module *mod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) trace_s390_zcrypt_req(buffer, TP_HWRNGCPRB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) ap_init_message(&ap_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) rc = get_rng_fc(&ap_msg, &func_code, &domain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) pref_zc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) pref_zq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) spin_lock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) for_each_zcrypt_card(zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) /* Check for useable CCA card */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) if (!zc->online || !zc->card->config ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) !(zc->card->functions & 0x10000000))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) /* get weight index of the card device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) wgt = zc->speed_rating[func_code];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) if (!zcrypt_card_compare(zc, pref_zc, wgt, pref_wgt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) for_each_zcrypt_queue(zq, zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) /* check if device is useable and eligible */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) if (!zq->online || !zq->ops->rng ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) !zq->queue->config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) if (!zcrypt_queue_compare(zq, pref_zq, wgt, pref_wgt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) pref_zc = zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) pref_zq = zq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) pref_wgt = wgt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) pref_zq = zcrypt_pick_queue(pref_zc, pref_zq, &mod, wgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) if (!pref_zq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) rc = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) qid = pref_zq->queue->qid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) rc = pref_zq->ops->rng(pref_zq, buffer, &ap_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) spin_lock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) zcrypt_drop_queue(pref_zc, pref_zq, mod, wgt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) ap_release_message(&ap_msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) trace_s390_zcrypt_rep(buffer, func_code, rc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) AP_QID_CARD(qid), AP_QID_QUEUE(qid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) static void zcrypt_device_status_mask(struct zcrypt_device_status *devstatus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) struct zcrypt_card *zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) struct zcrypt_queue *zq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) struct zcrypt_device_status *stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) int card, queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) memset(devstatus, 0, MAX_ZDEV_ENTRIES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) * sizeof(struct zcrypt_device_status));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) spin_lock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) for_each_zcrypt_card(zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) for_each_zcrypt_queue(zq, zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) card = AP_QID_CARD(zq->queue->qid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) if (card >= MAX_ZDEV_CARDIDS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) queue = AP_QID_QUEUE(zq->queue->qid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) stat = &devstatus[card * AP_DOMAINS + queue];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) stat->hwtype = zc->card->ap_dev.device_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) stat->functions = zc->card->functions >> 26;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) stat->qid = zq->queue->qid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) stat->online = zq->online ? 0x01 : 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) void zcrypt_device_status_mask_ext(struct zcrypt_device_status_ext *devstatus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) struct zcrypt_card *zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) struct zcrypt_queue *zq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) struct zcrypt_device_status_ext *stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) int card, queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) memset(devstatus, 0, MAX_ZDEV_ENTRIES_EXT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) * sizeof(struct zcrypt_device_status_ext));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) spin_lock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) for_each_zcrypt_card(zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) for_each_zcrypt_queue(zq, zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) card = AP_QID_CARD(zq->queue->qid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) queue = AP_QID_QUEUE(zq->queue->qid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) stat = &devstatus[card * AP_DOMAINS + queue];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) stat->hwtype = zc->card->ap_dev.device_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) stat->functions = zc->card->functions >> 26;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) stat->qid = zq->queue->qid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) stat->online = zq->online ? 0x01 : 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) EXPORT_SYMBOL(zcrypt_device_status_mask_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) int zcrypt_device_status_ext(int card, int queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) struct zcrypt_device_status_ext *devstat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) struct zcrypt_card *zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) struct zcrypt_queue *zq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) memset(devstat, 0, sizeof(*devstat));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) spin_lock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) for_each_zcrypt_card(zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) for_each_zcrypt_queue(zq, zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) if (card == AP_QID_CARD(zq->queue->qid) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) queue == AP_QID_QUEUE(zq->queue->qid)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) devstat->hwtype = zc->card->ap_dev.device_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) devstat->functions = zc->card->functions >> 26;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) devstat->qid = zq->queue->qid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) devstat->online = zq->online ? 0x01 : 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) EXPORT_SYMBOL(zcrypt_device_status_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) static void zcrypt_status_mask(char status[], size_t max_adapters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) struct zcrypt_card *zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) struct zcrypt_queue *zq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) int card;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) memset(status, 0, max_adapters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) spin_lock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) for_each_zcrypt_card(zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) for_each_zcrypt_queue(zq, zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) card = AP_QID_CARD(zq->queue->qid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) || card >= max_adapters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) status[card] = zc->online ? zc->user_space_type : 0x0d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) static void zcrypt_qdepth_mask(char qdepth[], size_t max_adapters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) struct zcrypt_card *zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) struct zcrypt_queue *zq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) int card;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) memset(qdepth, 0, max_adapters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) spin_lock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) local_bh_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) for_each_zcrypt_card(zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) for_each_zcrypt_queue(zq, zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) card = AP_QID_CARD(zq->queue->qid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) || card >= max_adapters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) spin_lock(&zq->queue->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) qdepth[card] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) zq->queue->pendingq_count +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) zq->queue->requestq_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) spin_unlock(&zq->queue->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) local_bh_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) static void zcrypt_perdev_reqcnt(u32 reqcnt[], size_t max_adapters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) struct zcrypt_card *zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) struct zcrypt_queue *zq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) int card;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) u64 cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) memset(reqcnt, 0, sizeof(int) * max_adapters);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) spin_lock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) local_bh_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) for_each_zcrypt_card(zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) for_each_zcrypt_queue(zq, zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) card = AP_QID_CARD(zq->queue->qid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) || card >= max_adapters)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) spin_lock(&zq->queue->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) cnt = zq->queue->total_request_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) spin_unlock(&zq->queue->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) reqcnt[card] = (cnt < UINT_MAX) ? (u32) cnt : UINT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) local_bh_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) static int zcrypt_pendingq_count(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) struct zcrypt_card *zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) struct zcrypt_queue *zq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) int pendingq_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) pendingq_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) spin_lock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) local_bh_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) for_each_zcrypt_card(zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) for_each_zcrypt_queue(zq, zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) spin_lock(&zq->queue->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) pendingq_count += zq->queue->pendingq_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) spin_unlock(&zq->queue->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) local_bh_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) return pendingq_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) static int zcrypt_requestq_count(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) struct zcrypt_card *zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) struct zcrypt_queue *zq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) int requestq_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) requestq_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) spin_lock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) local_bh_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) for_each_zcrypt_card(zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) for_each_zcrypt_queue(zq, zc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) if (AP_QID_QUEUE(zq->queue->qid) != ap_domain_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) spin_lock(&zq->queue->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) requestq_count += zq->queue->requestq_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) spin_unlock(&zq->queue->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) local_bh_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) return requestq_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) static int icarsamodexpo_ioctl(struct ap_perms *perms, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) struct zcrypt_track tr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) struct ica_rsa_modexpo mex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) struct ica_rsa_modexpo __user *umex = (void __user *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) memset(&tr, 0, sizeof(tr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) if (copy_from_user(&mex, umex, sizeof(mex)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) #ifdef CONFIG_ZCRYPT_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) if (mex.inputdatalength & (1U << 31)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) tr.fi.cmd = (u16)(mex.inputdatalength >> 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) mex.inputdatalength &= 0x0000FFFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) rc = zcrypt_rsa_modexpo(perms, &tr, &mex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) if (rc == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) tr.again_counter++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) #ifdef CONFIG_ZCRYPT_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) if (rc == -EAGAIN && (tr.fi.flags & AP_FI_FLAG_NO_RETRY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) /* on failure: retry once again after a requested rescan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) if ((rc == -ENODEV) && (zcrypt_process_rescan()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) rc = zcrypt_rsa_modexpo(perms, &tr, &mex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) if (rc == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) tr.again_counter++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSAMODEXPO rc=%d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) return put_user(mex.outputdatalength, &umex->outputdatalength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) static int icarsacrt_ioctl(struct ap_perms *perms, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) struct zcrypt_track tr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) struct ica_rsa_modexpo_crt crt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) struct ica_rsa_modexpo_crt __user *ucrt = (void __user *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) memset(&tr, 0, sizeof(tr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) if (copy_from_user(&crt, ucrt, sizeof(crt)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) #ifdef CONFIG_ZCRYPT_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) if (crt.inputdatalength & (1U << 31)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) tr.fi.cmd = (u16)(crt.inputdatalength >> 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) crt.inputdatalength &= 0x0000FFFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) rc = zcrypt_rsa_crt(perms, &tr, &crt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) if (rc == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) tr.again_counter++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) #ifdef CONFIG_ZCRYPT_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) if (rc == -EAGAIN && (tr.fi.flags & AP_FI_FLAG_NO_RETRY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) /* on failure: retry once again after a requested rescan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) if ((rc == -ENODEV) && (zcrypt_process_rescan()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) rc = zcrypt_rsa_crt(perms, &tr, &crt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) if (rc == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) tr.again_counter++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) ZCRYPT_DBF(DBF_DEBUG, "ioctl ICARSACRT rc=%d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) return put_user(crt.outputdatalength, &ucrt->outputdatalength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) static int zsecsendcprb_ioctl(struct ap_perms *perms, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) struct ica_xcRB xcRB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) struct zcrypt_track tr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) struct ica_xcRB __user *uxcRB = (void __user *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) memset(&tr, 0, sizeof(tr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) if (copy_from_user(&xcRB, uxcRB, sizeof(xcRB)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) #ifdef CONFIG_ZCRYPT_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) if (xcRB.status & (1U << 31)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) tr.fi.cmd = (u16)(xcRB.status >> 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) xcRB.status &= 0x0000FFFF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) if (rc == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) tr.again_counter++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) #ifdef CONFIG_ZCRYPT_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) if (rc == -EAGAIN && (tr.fi.flags & AP_FI_FLAG_NO_RETRY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) /* on failure: retry once again after a requested rescan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) if ((rc == -ENODEV) && (zcrypt_process_rescan()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) if (rc == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) tr.again_counter++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDCPRB rc=%d status=0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) rc, xcRB.status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) if (copy_to_user(uxcRB, &xcRB, sizeof(xcRB)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) static int zsendep11cprb_ioctl(struct ap_perms *perms, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) struct ep11_urb xcrb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) struct zcrypt_track tr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) struct ep11_urb __user *uxcrb = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) memset(&tr, 0, sizeof(tr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) if (copy_from_user(&xcrb, uxcrb, sizeof(xcrb)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) #ifdef CONFIG_ZCRYPT_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) if (xcrb.req_len & (1ULL << 63)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) if (!capable(CAP_SYS_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) tr.fi.cmd = (u16)(xcrb.req_len >> 48);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) xcrb.req_len &= 0x0000FFFFFFFFFFFFULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) rc = _zcrypt_send_ep11_cprb(true, perms, &tr, &xcrb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) if (rc == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) tr.again_counter++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) #ifdef CONFIG_ZCRYPT_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) if (rc == -EAGAIN && (tr.fi.flags & AP_FI_FLAG_NO_RETRY))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) /* on failure: retry once again after a requested rescan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) if ((rc == -ENODEV) && (zcrypt_process_rescan()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) rc = _zcrypt_send_ep11_cprb(true, perms, &tr, &xcrb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) if (rc == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) tr.again_counter++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) ZCRYPT_DBF(DBF_DEBUG, "ioctl ZSENDEP11CPRB rc=%d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) if (copy_to_user(uxcrb, &xcrb, sizeof(xcrb)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) static long zcrypt_unlocked_ioctl(struct file *filp, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) struct ap_perms *perms =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) (struct ap_perms *) filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) rc = zcrypt_check_ioctl(perms, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) case ICARSAMODEXPO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) return icarsamodexpo_ioctl(perms, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) case ICARSACRT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) return icarsacrt_ioctl(perms, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) case ZSECSENDCPRB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) return zsecsendcprb_ioctl(perms, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) case ZSENDEP11CPRB:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) return zsendep11cprb_ioctl(perms, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) case ZCRYPT_DEVICE_STATUS: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) struct zcrypt_device_status_ext *device_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) size_t total_size = MAX_ZDEV_ENTRIES_EXT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) * sizeof(struct zcrypt_device_status_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) device_status = kzalloc(total_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) if (!device_status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) zcrypt_device_status_mask_ext(device_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) if (copy_to_user((char __user *) arg, device_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) total_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) rc = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) kfree(device_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) case ZCRYPT_STATUS_MASK: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) char status[AP_DEVICES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) zcrypt_status_mask(status, AP_DEVICES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) if (copy_to_user((char __user *) arg, status, sizeof(status)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) case ZCRYPT_QDEPTH_MASK: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) char qdepth[AP_DEVICES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) zcrypt_qdepth_mask(qdepth, AP_DEVICES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) case ZCRYPT_PERDEV_REQCNT: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) u32 *reqcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) reqcnt = kcalloc(AP_DEVICES, sizeof(u32), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) if (!reqcnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) zcrypt_perdev_reqcnt(reqcnt, AP_DEVICES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) if (copy_to_user((int __user *) arg, reqcnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) sizeof(u32) * AP_DEVICES))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) rc = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) kfree(reqcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) case Z90STAT_REQUESTQ_COUNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) return put_user(zcrypt_requestq_count(), (int __user *) arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) case Z90STAT_PENDINGQ_COUNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) return put_user(zcrypt_pendingq_count(), (int __user *) arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) case Z90STAT_TOTALOPEN_COUNT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) return put_user(atomic_read(&zcrypt_open_count),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) (int __user *) arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) case Z90STAT_DOMAIN_INDEX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) return put_user(ap_domain_index, (int __user *) arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) * Deprecated ioctls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) case ZDEVICESTATUS: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) /* the old ioctl supports only 64 adapters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) struct zcrypt_device_status *device_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) size_t total_size = MAX_ZDEV_ENTRIES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) * sizeof(struct zcrypt_device_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) device_status = kzalloc(total_size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) if (!device_status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) zcrypt_device_status_mask(device_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) if (copy_to_user((char __user *) arg, device_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) total_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) rc = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) kfree(device_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) case Z90STAT_STATUS_MASK: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) /* the old ioctl supports only 64 adapters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) char status[MAX_ZDEV_CARDIDS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) zcrypt_status_mask(status, MAX_ZDEV_CARDIDS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) if (copy_to_user((char __user *) arg, status, sizeof(status)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) case Z90STAT_QDEPTH_MASK: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) /* the old ioctl supports only 64 adapters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) char qdepth[MAX_ZDEV_CARDIDS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) zcrypt_qdepth_mask(qdepth, MAX_ZDEV_CARDIDS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) if (copy_to_user((char __user *) arg, qdepth, sizeof(qdepth)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) case Z90STAT_PERDEV_REQCNT: {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) /* the old ioctl supports only 64 adapters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) u32 reqcnt[MAX_ZDEV_CARDIDS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) zcrypt_perdev_reqcnt(reqcnt, MAX_ZDEV_CARDIDS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) if (copy_to_user((int __user *) arg, reqcnt, sizeof(reqcnt)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) /* unknown ioctl number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) ZCRYPT_DBF(DBF_DEBUG, "unknown ioctl 0x%08x\n", cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) return -ENOIOCTLCMD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) * ioctl32 conversion routines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) struct compat_ica_rsa_modexpo {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) compat_uptr_t inputdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) unsigned int inputdatalength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) compat_uptr_t outputdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) unsigned int outputdatalength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) compat_uptr_t b_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) compat_uptr_t n_modulus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) static long trans_modexpo32(struct ap_perms *perms, struct file *filp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) struct compat_ica_rsa_modexpo __user *umex32 = compat_ptr(arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) struct compat_ica_rsa_modexpo mex32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) struct ica_rsa_modexpo mex64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) struct zcrypt_track tr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) long rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) memset(&tr, 0, sizeof(tr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) if (copy_from_user(&mex32, umex32, sizeof(mex32)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) mex64.inputdata = compat_ptr(mex32.inputdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) mex64.inputdatalength = mex32.inputdatalength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) mex64.outputdata = compat_ptr(mex32.outputdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) mex64.outputdatalength = mex32.outputdatalength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) mex64.b_key = compat_ptr(mex32.b_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) mex64.n_modulus = compat_ptr(mex32.n_modulus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) rc = zcrypt_rsa_modexpo(perms, &tr, &mex64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) if (rc == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) tr.again_counter++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) /* on failure: retry once again after a requested rescan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) if ((rc == -ENODEV) && (zcrypt_process_rescan()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) rc = zcrypt_rsa_modexpo(perms, &tr, &mex64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) if (rc == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) tr.again_counter++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) return put_user(mex64.outputdatalength,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) &umex32->outputdatalength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) struct compat_ica_rsa_modexpo_crt {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) compat_uptr_t inputdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) unsigned int inputdatalength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) compat_uptr_t outputdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) unsigned int outputdatalength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) compat_uptr_t bp_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) compat_uptr_t bq_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) compat_uptr_t np_prime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) compat_uptr_t nq_prime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) compat_uptr_t u_mult_inv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) static long trans_modexpo_crt32(struct ap_perms *perms, struct file *filp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) struct compat_ica_rsa_modexpo_crt __user *ucrt32 = compat_ptr(arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) struct compat_ica_rsa_modexpo_crt crt32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) struct ica_rsa_modexpo_crt crt64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) struct zcrypt_track tr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) long rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) memset(&tr, 0, sizeof(tr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) if (copy_from_user(&crt32, ucrt32, sizeof(crt32)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) crt64.inputdata = compat_ptr(crt32.inputdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) crt64.inputdatalength = crt32.inputdatalength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) crt64.outputdata = compat_ptr(crt32.outputdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) crt64.outputdatalength = crt32.outputdatalength;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) crt64.bp_key = compat_ptr(crt32.bp_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) crt64.bq_key = compat_ptr(crt32.bq_key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) crt64.np_prime = compat_ptr(crt32.np_prime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) crt64.nq_prime = compat_ptr(crt32.nq_prime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) crt64.u_mult_inv = compat_ptr(crt32.u_mult_inv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) rc = zcrypt_rsa_crt(perms, &tr, &crt64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) if (rc == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) tr.again_counter++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) /* on failure: retry once again after a requested rescan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) if ((rc == -ENODEV) && (zcrypt_process_rescan()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) rc = zcrypt_rsa_crt(perms, &tr, &crt64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) if (rc == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) tr.again_counter++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) return put_user(crt64.outputdatalength,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) &ucrt32->outputdatalength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) struct compat_ica_xcRB {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) unsigned short agent_ID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) unsigned int user_defined;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) unsigned short request_ID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) unsigned int request_control_blk_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) unsigned char padding1[16 - sizeof(compat_uptr_t)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) compat_uptr_t request_control_blk_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) unsigned int request_data_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) char padding2[16 - sizeof(compat_uptr_t)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) compat_uptr_t request_data_address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) unsigned int reply_control_blk_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) char padding3[16 - sizeof(compat_uptr_t)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) compat_uptr_t reply_control_blk_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) unsigned int reply_data_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) char padding4[16 - sizeof(compat_uptr_t)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) compat_uptr_t reply_data_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) unsigned short priority_window;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) unsigned int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) static long trans_xcRB32(struct ap_perms *perms, struct file *filp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) struct compat_ica_xcRB __user *uxcRB32 = compat_ptr(arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) struct compat_ica_xcRB xcRB32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) struct zcrypt_track tr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) struct ica_xcRB xcRB64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) long rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) memset(&tr, 0, sizeof(tr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) if (copy_from_user(&xcRB32, uxcRB32, sizeof(xcRB32)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) xcRB64.agent_ID = xcRB32.agent_ID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) xcRB64.user_defined = xcRB32.user_defined;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) xcRB64.request_ID = xcRB32.request_ID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) xcRB64.request_control_blk_length =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) xcRB32.request_control_blk_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) xcRB64.request_control_blk_addr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) compat_ptr(xcRB32.request_control_blk_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) xcRB64.request_data_length =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) xcRB32.request_data_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) xcRB64.request_data_address =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) compat_ptr(xcRB32.request_data_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) xcRB64.reply_control_blk_length =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) xcRB32.reply_control_blk_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) xcRB64.reply_control_blk_addr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) compat_ptr(xcRB32.reply_control_blk_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) xcRB64.reply_data_length = xcRB32.reply_data_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) xcRB64.reply_data_addr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) compat_ptr(xcRB32.reply_data_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) xcRB64.priority_window = xcRB32.priority_window;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) xcRB64.status = xcRB32.status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) if (rc == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) tr.again_counter++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) /* on failure: retry once again after a requested rescan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) if ((rc == -ENODEV) && (zcrypt_process_rescan()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) rc = _zcrypt_send_cprb(true, perms, &tr, &xcRB64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) if (rc == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) tr.again_counter++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) } while (rc == -EAGAIN && tr.again_counter < TRACK_AGAIN_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) if (rc == -EAGAIN && tr.again_counter >= TRACK_AGAIN_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) xcRB32.reply_control_blk_length = xcRB64.reply_control_blk_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) xcRB32.reply_data_length = xcRB64.reply_data_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) xcRB32.status = xcRB64.status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) if (copy_to_user(uxcRB32, &xcRB32, sizeof(xcRB32)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) static long zcrypt_compat_ioctl(struct file *filp, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) struct ap_perms *perms =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) (struct ap_perms *) filp->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) rc = zcrypt_check_ioctl(perms, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) if (cmd == ICARSAMODEXPO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) return trans_modexpo32(perms, filp, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) if (cmd == ICARSACRT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) return trans_modexpo_crt32(perms, filp, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) if (cmd == ZSECSENDCPRB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) return trans_xcRB32(perms, filp, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) return zcrypt_unlocked_ioctl(filp, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) * Misc device file operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) static const struct file_operations zcrypt_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) .read = zcrypt_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) .write = zcrypt_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) .unlocked_ioctl = zcrypt_unlocked_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) .compat_ioctl = zcrypt_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) .open = zcrypt_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) .release = zcrypt_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) .llseek = no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) * Misc device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) static struct miscdevice zcrypt_misc_device = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) .minor = MISC_DYNAMIC_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) .name = "z90crypt",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) .fops = &zcrypt_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) static int zcrypt_rng_device_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) static u32 *zcrypt_rng_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) static int zcrypt_rng_buffer_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) static DEFINE_MUTEX(zcrypt_rng_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) static int zcrypt_rng_data_read(struct hwrng *rng, u32 *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) * We don't need locking here because the RNG API guarantees serialized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) * read method calls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) if (zcrypt_rng_buffer_index == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) rc = zcrypt_rng((char *) zcrypt_rng_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) /* on failure: retry once again after a requested rescan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) if ((rc == -ENODEV) && (zcrypt_process_rescan()))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) rc = zcrypt_rng((char *) zcrypt_rng_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) zcrypt_rng_buffer_index = rc / sizeof(*data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) *data = zcrypt_rng_buffer[--zcrypt_rng_buffer_index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) return sizeof(*data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) static struct hwrng zcrypt_rng_dev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) .name = "zcrypt",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) .data_read = zcrypt_rng_data_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) .quality = 990,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) int zcrypt_rng_device_add(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) mutex_lock(&zcrypt_rng_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) if (zcrypt_rng_device_count == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) zcrypt_rng_buffer = (u32 *) get_zeroed_page(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) if (!zcrypt_rng_buffer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) zcrypt_rng_buffer_index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) if (!zcrypt_hwrng_seed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) zcrypt_rng_dev.quality = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) rc = hwrng_register(&zcrypt_rng_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) zcrypt_rng_device_count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) zcrypt_rng_device_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) mutex_unlock(&zcrypt_rng_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) free_page((unsigned long) zcrypt_rng_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) mutex_unlock(&zcrypt_rng_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) void zcrypt_rng_device_remove(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) mutex_lock(&zcrypt_rng_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) zcrypt_rng_device_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) if (zcrypt_rng_device_count == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) hwrng_unregister(&zcrypt_rng_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) free_page((unsigned long) zcrypt_rng_buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) mutex_unlock(&zcrypt_rng_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) int __init zcrypt_debug_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) zcrypt_dbf_info = debug_register("zcrypt", 1, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) DBF_MAX_SPRINTF_ARGS * sizeof(long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) debug_register_view(zcrypt_dbf_info, &debug_sprintf_view);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) debug_set_level(zcrypt_dbf_info, DBF_ERR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) void zcrypt_debug_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) debug_unregister(zcrypt_dbf_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) static int __init zcdn_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) /* create a new class 'zcrypt' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) zcrypt_class = class_create(THIS_MODULE, ZCRYPT_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) if (IS_ERR(zcrypt_class)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) rc = PTR_ERR(zcrypt_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) goto out_class_create_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) zcrypt_class->dev_release = zcdn_device_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) /* alloc device minor range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) rc = alloc_chrdev_region(&zcrypt_devt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 0, ZCRYPT_MAX_MINOR_NODES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) ZCRYPT_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) goto out_alloc_chrdev_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) cdev_init(&zcrypt_cdev, &zcrypt_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) zcrypt_cdev.owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) rc = cdev_add(&zcrypt_cdev, zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) goto out_cdev_add_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) /* need some class specific sysfs attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) rc = class_create_file(zcrypt_class, &class_attr_zcdn_create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) goto out_class_create_file_1_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) rc = class_create_file(zcrypt_class, &class_attr_zcdn_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) goto out_class_create_file_2_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) out_class_create_file_2_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) class_remove_file(zcrypt_class, &class_attr_zcdn_create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) out_class_create_file_1_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) cdev_del(&zcrypt_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) out_cdev_add_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) out_alloc_chrdev_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) class_destroy(zcrypt_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) out_class_create_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) static void zcdn_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) class_remove_file(zcrypt_class, &class_attr_zcdn_create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) class_remove_file(zcrypt_class, &class_attr_zcdn_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) zcdn_destroy_all();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) cdev_del(&zcrypt_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) unregister_chrdev_region(zcrypt_devt, ZCRYPT_MAX_MINOR_NODES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) class_destroy(zcrypt_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) * zcrypt_api_init(): Module initialization.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) * The module initialization code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) int __init zcrypt_api_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) rc = zcrypt_debug_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) rc = zcdn_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) /* Register the request sprayer. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) rc = misc_register(&zcrypt_misc_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) if (rc < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) goto out_misc_register_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) zcrypt_msgtype6_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) zcrypt_msgtype50_init();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) out_misc_register_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) zcdn_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) zcrypt_debug_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) * zcrypt_api_exit(): Module termination.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) * The module termination code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) void __exit zcrypt_api_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) #ifdef CONFIG_ZCRYPT_MULTIDEVNODES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) zcdn_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) misc_deregister(&zcrypt_misc_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) zcrypt_msgtype6_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) zcrypt_msgtype50_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) zcrypt_ccamisc_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) zcrypt_ep11misc_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) zcrypt_debug_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) module_init(zcrypt_api_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) module_exit(zcrypt_api_exit);