1812141a9SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
2e28d2af4SIngo Tuchscherer /*
3e28d2af4SIngo Tuchscherer * Copyright IBM Corp. 2001, 2012
4e28d2af4SIngo Tuchscherer * Author(s): Robert Burroughs
5e28d2af4SIngo Tuchscherer * Eric Rossman (edrossma@us.ibm.com)
6e28d2af4SIngo Tuchscherer * Cornelia Huck <cornelia.huck@de.ibm.com>
7e28d2af4SIngo Tuchscherer *
8e28d2af4SIngo Tuchscherer * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
9e28d2af4SIngo Tuchscherer * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
10e28d2af4SIngo Tuchscherer * Ralph Wuerthner <rwuerthn@de.ibm.com>
11e28d2af4SIngo Tuchscherer * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
12e28d2af4SIngo Tuchscherer */
13e28d2af4SIngo Tuchscherer
14e28d2af4SIngo Tuchscherer #include <linux/module.h>
15e28d2af4SIngo Tuchscherer #include <linux/init.h>
16e28d2af4SIngo Tuchscherer #include <linux/interrupt.h>
17e28d2af4SIngo Tuchscherer #include <linux/miscdevice.h>
18e28d2af4SIngo Tuchscherer #include <linux/fs.h>
19e28d2af4SIngo Tuchscherer #include <linux/proc_fs.h>
20e28d2af4SIngo Tuchscherer #include <linux/seq_file.h>
21e28d2af4SIngo Tuchscherer #include <linux/compat.h>
22e28d2af4SIngo Tuchscherer #include <linux/slab.h>
23e28d2af4SIngo Tuchscherer #include <linux/atomic.h>
24e28d2af4SIngo Tuchscherer #include <linux/uaccess.h>
25e28d2af4SIngo Tuchscherer #include <linux/hw_random.h>
26e28d2af4SIngo Tuchscherer #include <linux/debugfs.h>
27e28d2af4SIngo Tuchscherer #include <asm/debug.h>
28e28d2af4SIngo Tuchscherer
29e28d2af4SIngo Tuchscherer #include "zcrypt_debug.h"
30e28d2af4SIngo Tuchscherer #include "zcrypt_api.h"
31e28d2af4SIngo Tuchscherer
32e28d2af4SIngo Tuchscherer #include "zcrypt_msgtype6.h"
33e28d2af4SIngo Tuchscherer #include "zcrypt_msgtype50.h"
34e28d2af4SIngo Tuchscherer
35e28d2af4SIngo Tuchscherer /*
36e28d2af4SIngo Tuchscherer * Device attributes common for all crypto card devices.
37e28d2af4SIngo Tuchscherer */
38e28d2af4SIngo Tuchscherer
type_show(struct device * dev,struct device_attribute * attr,char * buf)39ac2b96f3SHarald Freudenberger static ssize_t type_show(struct device *dev,
40e28d2af4SIngo Tuchscherer struct device_attribute *attr, char *buf)
41e28d2af4SIngo Tuchscherer {
42b5adbbf8SJulian Wiedmann struct zcrypt_card *zc = dev_get_drvdata(dev);
43e28d2af4SIngo Tuchscherer
44964d581dSHarald Freudenberger return sysfs_emit(buf, "%s\n", zc->type_string);
45e28d2af4SIngo Tuchscherer }
46e28d2af4SIngo Tuchscherer
47ac2b96f3SHarald Freudenberger static DEVICE_ATTR_RO(type);
48e28d2af4SIngo Tuchscherer
online_show(struct device * dev,struct device_attribute * attr,char * buf)49ac2b96f3SHarald Freudenberger static ssize_t online_show(struct device *dev,
50e28d2af4SIngo Tuchscherer struct device_attribute *attr,
51e28d2af4SIngo Tuchscherer char *buf)
52e28d2af4SIngo Tuchscherer {
53b5adbbf8SJulian Wiedmann struct zcrypt_card *zc = dev_get_drvdata(dev);
544f2fcccdSHarald Freudenberger struct ap_card *ac = to_ap_card(dev);
55*cfaef651SIngo Franzki int online = ac->config && !ac->chkstop && zc->online ? 1 : 0;
56e28d2af4SIngo Tuchscherer
57964d581dSHarald Freudenberger return sysfs_emit(buf, "%d\n", online);
58e28d2af4SIngo Tuchscherer }
59e28d2af4SIngo Tuchscherer
online_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)60ac2b96f3SHarald Freudenberger static ssize_t online_store(struct device *dev,
61e28d2af4SIngo Tuchscherer struct device_attribute *attr,
62e28d2af4SIngo Tuchscherer const char *buf, size_t count)
63e28d2af4SIngo Tuchscherer {
64b5adbbf8SJulian Wiedmann struct zcrypt_card *zc = dev_get_drvdata(dev);
654f2fcccdSHarald Freudenberger struct ap_card *ac = to_ap_card(dev);
66e28d2af4SIngo Tuchscherer struct zcrypt_queue *zq;
67df6f508cSHarald Freudenberger int online, id, i = 0, maxzqs = 0;
68df6f508cSHarald Freudenberger struct zcrypt_queue **zq_uelist = NULL;
69e28d2af4SIngo Tuchscherer
70e28d2af4SIngo Tuchscherer if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
71e28d2af4SIngo Tuchscherer return -EINVAL;
72e28d2af4SIngo Tuchscherer
73*cfaef651SIngo Franzki if (online && (!ac->config || ac->chkstop))
744f2fcccdSHarald Freudenberger return -ENODEV;
754f2fcccdSHarald Freudenberger
76e28d2af4SIngo Tuchscherer zc->online = online;
77e28d2af4SIngo Tuchscherer id = zc->card->id;
78cccd85bfSHarald Freudenberger
793f74eb5fSHarald Freudenberger ZCRYPT_DBF_INFO("%s card=%02x online=%d\n", __func__, id, online);
80cccd85bfSHarald Freudenberger
81df6f508cSHarald Freudenberger ap_send_online_uevent(&ac->ap_dev, online);
82df6f508cSHarald Freudenberger
83e28d2af4SIngo Tuchscherer spin_lock(&zcrypt_list_lock);
84df6f508cSHarald Freudenberger /*
85df6f508cSHarald Freudenberger * As we are in atomic context here, directly sending uevents
86df6f508cSHarald Freudenberger * does not work. So collect the zqueues in a dynamic array
87df6f508cSHarald Freudenberger * and process them after zcrypt_list_lock release. As we get/put
88df6f508cSHarald Freudenberger * the zqueue objects, we make sure they exist after lock release.
89df6f508cSHarald Freudenberger */
90e28d2af4SIngo Tuchscherer list_for_each_entry(zq, &zc->zqueues, list)
91df6f508cSHarald Freudenberger maxzqs++;
92df6f508cSHarald Freudenberger if (maxzqs > 0)
9397f32e11SJakob Koschel zq_uelist = kcalloc(maxzqs + 1, sizeof(*zq_uelist), GFP_ATOMIC);
94df6f508cSHarald Freudenberger list_for_each_entry(zq, &zc->zqueues, list)
95df6f508cSHarald Freudenberger if (zcrypt_queue_force_online(zq, online))
96df6f508cSHarald Freudenberger if (zq_uelist) {
97df6f508cSHarald Freudenberger zcrypt_queue_get(zq);
98df6f508cSHarald Freudenberger zq_uelist[i++] = zq;
99df6f508cSHarald Freudenberger }
100e28d2af4SIngo Tuchscherer spin_unlock(&zcrypt_list_lock);
101df6f508cSHarald Freudenberger if (zq_uelist) {
102df6f508cSHarald Freudenberger for (i = 0; zq_uelist[i]; i++) {
103df6f508cSHarald Freudenberger zq = zq_uelist[i];
104df6f508cSHarald Freudenberger ap_send_online_uevent(&zq->queue->ap_dev, online);
105df6f508cSHarald Freudenberger zcrypt_queue_put(zq);
106df6f508cSHarald Freudenberger }
107df6f508cSHarald Freudenberger kfree(zq_uelist);
108df6f508cSHarald Freudenberger }
109df6f508cSHarald Freudenberger
110e28d2af4SIngo Tuchscherer return count;
111e28d2af4SIngo Tuchscherer }
112e28d2af4SIngo Tuchscherer
113ac2b96f3SHarald Freudenberger static DEVICE_ATTR_RW(online);
114e28d2af4SIngo Tuchscherer
load_show(struct device * dev,struct device_attribute * attr,char * buf)115ac2b96f3SHarald Freudenberger static ssize_t load_show(struct device *dev,
1164a07750bSHarald Freudenberger struct device_attribute *attr,
1174a07750bSHarald Freudenberger char *buf)
1184a07750bSHarald Freudenberger {
119b5adbbf8SJulian Wiedmann struct zcrypt_card *zc = dev_get_drvdata(dev);
1204a07750bSHarald Freudenberger
121964d581dSHarald Freudenberger return sysfs_emit(buf, "%d\n", atomic_read(&zc->load));
1224a07750bSHarald Freudenberger }
1234a07750bSHarald Freudenberger
124ac2b96f3SHarald Freudenberger static DEVICE_ATTR_RO(load);
1254a07750bSHarald Freudenberger
126e28d2af4SIngo Tuchscherer static struct attribute *zcrypt_card_attrs[] = {
127e28d2af4SIngo Tuchscherer &dev_attr_type.attr,
128e28d2af4SIngo Tuchscherer &dev_attr_online.attr,
1294a07750bSHarald Freudenberger &dev_attr_load.attr,
130e28d2af4SIngo Tuchscherer NULL,
131e28d2af4SIngo Tuchscherer };
132e28d2af4SIngo Tuchscherer
1339731c0a9SArvind Yadav static const struct attribute_group zcrypt_card_attr_group = {
134e28d2af4SIngo Tuchscherer .attrs = zcrypt_card_attrs,
135e28d2af4SIngo Tuchscherer };
136e28d2af4SIngo Tuchscherer
zcrypt_card_alloc(void)137e28d2af4SIngo Tuchscherer struct zcrypt_card *zcrypt_card_alloc(void)
138e28d2af4SIngo Tuchscherer {
139e28d2af4SIngo Tuchscherer struct zcrypt_card *zc;
140e28d2af4SIngo Tuchscherer
1412004b57cSHarald Freudenberger zc = kzalloc(sizeof(*zc), GFP_KERNEL);
142e28d2af4SIngo Tuchscherer if (!zc)
143e28d2af4SIngo Tuchscherer return NULL;
144e28d2af4SIngo Tuchscherer INIT_LIST_HEAD(&zc->list);
145e28d2af4SIngo Tuchscherer INIT_LIST_HEAD(&zc->zqueues);
146e28d2af4SIngo Tuchscherer kref_init(&zc->refcount);
147e28d2af4SIngo Tuchscherer return zc;
148e28d2af4SIngo Tuchscherer }
149e28d2af4SIngo Tuchscherer EXPORT_SYMBOL(zcrypt_card_alloc);
150e28d2af4SIngo Tuchscherer
zcrypt_card_free(struct zcrypt_card * zc)151e28d2af4SIngo Tuchscherer void zcrypt_card_free(struct zcrypt_card *zc)
152e28d2af4SIngo Tuchscherer {
153e28d2af4SIngo Tuchscherer kfree(zc);
154e28d2af4SIngo Tuchscherer }
155e28d2af4SIngo Tuchscherer EXPORT_SYMBOL(zcrypt_card_free);
156e28d2af4SIngo Tuchscherer
zcrypt_card_release(struct kref * kref)157e28d2af4SIngo Tuchscherer static void zcrypt_card_release(struct kref *kref)
158e28d2af4SIngo Tuchscherer {
159e28d2af4SIngo Tuchscherer struct zcrypt_card *zdev =
160e28d2af4SIngo Tuchscherer container_of(kref, struct zcrypt_card, refcount);
161e28d2af4SIngo Tuchscherer zcrypt_card_free(zdev);
162e28d2af4SIngo Tuchscherer }
163e28d2af4SIngo Tuchscherer
zcrypt_card_get(struct zcrypt_card * zc)164e28d2af4SIngo Tuchscherer void zcrypt_card_get(struct zcrypt_card *zc)
165e28d2af4SIngo Tuchscherer {
166e28d2af4SIngo Tuchscherer kref_get(&zc->refcount);
167e28d2af4SIngo Tuchscherer }
168e28d2af4SIngo Tuchscherer EXPORT_SYMBOL(zcrypt_card_get);
169e28d2af4SIngo Tuchscherer
zcrypt_card_put(struct zcrypt_card * zc)170e28d2af4SIngo Tuchscherer int zcrypt_card_put(struct zcrypt_card *zc)
171e28d2af4SIngo Tuchscherer {
172e28d2af4SIngo Tuchscherer return kref_put(&zc->refcount, zcrypt_card_release);
173e28d2af4SIngo Tuchscherer }
174e28d2af4SIngo Tuchscherer EXPORT_SYMBOL(zcrypt_card_put);
175e28d2af4SIngo Tuchscherer
176e28d2af4SIngo Tuchscherer /**
177e28d2af4SIngo Tuchscherer * zcrypt_card_register() - Register a crypto card device.
178e28d2af4SIngo Tuchscherer * @zc: Pointer to a crypto card device
179e28d2af4SIngo Tuchscherer *
180e28d2af4SIngo Tuchscherer * Register a crypto card device. Returns 0 if successful.
181e28d2af4SIngo Tuchscherer */
zcrypt_card_register(struct zcrypt_card * zc)182e28d2af4SIngo Tuchscherer int zcrypt_card_register(struct zcrypt_card *zc)
183e28d2af4SIngo Tuchscherer {
184e28d2af4SIngo Tuchscherer int rc;
185e28d2af4SIngo Tuchscherer
186e28d2af4SIngo Tuchscherer spin_lock(&zcrypt_list_lock);
187e28d2af4SIngo Tuchscherer list_add_tail(&zc->list, &zcrypt_card_list);
188e28d2af4SIngo Tuchscherer spin_unlock(&zcrypt_list_lock);
189e28d2af4SIngo Tuchscherer
190e28d2af4SIngo Tuchscherer zc->online = 1;
191cccd85bfSHarald Freudenberger
1923f74eb5fSHarald Freudenberger ZCRYPT_DBF_INFO("%s card=%02x register online=1\n",
1933f74eb5fSHarald Freudenberger __func__, zc->card->id);
194cccd85bfSHarald Freudenberger
19529c2680fSHarald Freudenberger rc = sysfs_create_group(&zc->card->ap_dev.device.kobj,
19629c2680fSHarald Freudenberger &zcrypt_card_attr_group);
19729c2680fSHarald Freudenberger if (rc) {
19829c2680fSHarald Freudenberger spin_lock(&zcrypt_list_lock);
19929c2680fSHarald Freudenberger list_del_init(&zc->list);
20029c2680fSHarald Freudenberger spin_unlock(&zcrypt_list_lock);
20129c2680fSHarald Freudenberger }
20229c2680fSHarald Freudenberger
203e28d2af4SIngo Tuchscherer return rc;
204e28d2af4SIngo Tuchscherer }
205e28d2af4SIngo Tuchscherer EXPORT_SYMBOL(zcrypt_card_register);
206e28d2af4SIngo Tuchscherer
207e28d2af4SIngo Tuchscherer /**
208e28d2af4SIngo Tuchscherer * zcrypt_card_unregister(): Unregister a crypto card device.
209e28d2af4SIngo Tuchscherer * @zc: Pointer to crypto card device
210e28d2af4SIngo Tuchscherer *
211e28d2af4SIngo Tuchscherer * Unregister a crypto card device.
212e28d2af4SIngo Tuchscherer */
zcrypt_card_unregister(struct zcrypt_card * zc)213e28d2af4SIngo Tuchscherer void zcrypt_card_unregister(struct zcrypt_card *zc)
214e28d2af4SIngo Tuchscherer {
2153f74eb5fSHarald Freudenberger ZCRYPT_DBF_INFO("%s card=%02x unregister\n",
2163f74eb5fSHarald Freudenberger __func__, zc->card->id);
217cccd85bfSHarald Freudenberger
218e28d2af4SIngo Tuchscherer spin_lock(&zcrypt_list_lock);
219e28d2af4SIngo Tuchscherer list_del_init(&zc->list);
220e28d2af4SIngo Tuchscherer spin_unlock(&zcrypt_list_lock);
221e28d2af4SIngo Tuchscherer sysfs_remove_group(&zc->card->ap_dev.device.kobj,
222e28d2af4SIngo Tuchscherer &zcrypt_card_attr_group);
22370fac808SHarald Freudenberger zcrypt_card_put(zc);
224e28d2af4SIngo Tuchscherer }
225e28d2af4SIngo Tuchscherer EXPORT_SYMBOL(zcrypt_card_unregister);
226