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