^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, 2012
^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) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/hw_random.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <asm/debug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include "zcrypt_debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include "zcrypt_api.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include "zcrypt_msgtype6.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include "zcrypt_msgtype50.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * Device attributes common for all crypto card devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static ssize_t type_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct zcrypt_card *zc = to_ap_card(dev)->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return scnprintf(buf, PAGE_SIZE, "%s\n", zc->type_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static DEVICE_ATTR_RO(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static ssize_t online_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct ap_card *ac = to_ap_card(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct zcrypt_card *zc = ac->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) int online = ac->config && zc->online ? 1 : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return scnprintf(buf, PAGE_SIZE, "%d\n", online);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static ssize_t online_store(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct ap_card *ac = to_ap_card(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct zcrypt_card *zc = ac->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct zcrypt_queue *zq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) int online, id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (online && !ac->config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) zc->online = online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) id = zc->card->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) ZCRYPT_DBF(DBF_INFO, "card=%02x online=%d\n", id, online);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) spin_lock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) list_for_each_entry(zq, &zc->zqueues, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) zcrypt_queue_force_online(zq, online);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static DEVICE_ATTR_RW(online);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static ssize_t load_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) struct zcrypt_card *zc = to_ap_card(dev)->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return scnprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&zc->load));
^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) static DEVICE_ATTR_RO(load);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) static struct attribute *zcrypt_card_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) &dev_attr_type.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) &dev_attr_online.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) &dev_attr_load.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static const struct attribute_group zcrypt_card_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) .attrs = zcrypt_card_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct zcrypt_card *zcrypt_card_alloc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct zcrypt_card *zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) zc = kzalloc(sizeof(struct zcrypt_card), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if (!zc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) INIT_LIST_HEAD(&zc->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) INIT_LIST_HEAD(&zc->zqueues);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) kref_init(&zc->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return zc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) EXPORT_SYMBOL(zcrypt_card_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) void zcrypt_card_free(struct zcrypt_card *zc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) kfree(zc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) EXPORT_SYMBOL(zcrypt_card_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static void zcrypt_card_release(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct zcrypt_card *zdev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) container_of(kref, struct zcrypt_card, refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) zcrypt_card_free(zdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) void zcrypt_card_get(struct zcrypt_card *zc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) kref_get(&zc->refcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) EXPORT_SYMBOL(zcrypt_card_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) int zcrypt_card_put(struct zcrypt_card *zc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return kref_put(&zc->refcount, zcrypt_card_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) EXPORT_SYMBOL(zcrypt_card_put);
^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) * zcrypt_card_register() - Register a crypto card device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * @zc: Pointer to a crypto card device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * Register a crypto card device. Returns 0 if successful.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) int zcrypt_card_register(struct zcrypt_card *zc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) spin_lock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) list_add_tail(&zc->list, &zcrypt_card_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) zc->online = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) ZCRYPT_DBF(DBF_INFO, "card=%02x register online=1\n", zc->card->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) rc = sysfs_create_group(&zc->card->ap_dev.device.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) &zcrypt_card_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) spin_lock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) list_del_init(&zc->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) EXPORT_SYMBOL(zcrypt_card_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * zcrypt_card_unregister(): Unregister a crypto card device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * @zc: Pointer to crypto card device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * Unregister a crypto card device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) void zcrypt_card_unregister(struct zcrypt_card *zc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) ZCRYPT_DBF(DBF_INFO, "card=%02x unregister\n", zc->card->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) spin_lock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) list_del_init(&zc->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) spin_unlock(&zcrypt_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) sysfs_remove_group(&zc->card->ap_dev.device.kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) &zcrypt_card_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) zcrypt_card_put(zc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) EXPORT_SYMBOL(zcrypt_card_unregister);