Merge remote-tracking branch 'spi/for-5.9' into spi-linus
[linux-2.6-microblaze.git] / drivers / s390 / crypto / zcrypt_cex2c.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Copyright IBM Corp. 2001, 2018
4  *  Author(s): Robert Burroughs
5  *             Eric Rossman (edrossma@us.ibm.com)
6  *
7  *  Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
8  *  Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
9  *                                Ralph Wuerthner <rwuerthn@de.ibm.com>
10  *  MSGTYPE restruct:             Holger Dengler <hd@linux.vnet.ibm.com>
11  */
12
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/err.h>
16 #include <linux/delay.h>
17 #include <linux/slab.h>
18 #include <linux/atomic.h>
19 #include <linux/uaccess.h>
20 #include <linux/mod_devicetable.h>
21
22 #include "ap_bus.h"
23 #include "zcrypt_api.h"
24 #include "zcrypt_error.h"
25 #include "zcrypt_msgtype6.h"
26 #include "zcrypt_cex2c.h"
27 #include "zcrypt_cca_key.h"
28 #include "zcrypt_ccamisc.h"
29
30 #define CEX2C_MIN_MOD_SIZE       16     /*  128 bits    */
31 #define CEX2C_MAX_MOD_SIZE      256     /* 2048 bits    */
32 #define CEX3C_MIN_MOD_SIZE       16     /*  128 bits    */
33 #define CEX3C_MAX_MOD_SIZE      512     /* 4096 bits    */
34 #define CEX2C_MAX_XCRB_MESSAGE_SIZE (12*1024)
35 #define CEX2C_CLEANUP_TIME      (15*HZ)
36
37 MODULE_AUTHOR("IBM Corporation");
38 MODULE_DESCRIPTION("CEX2C/CEX3C Cryptographic Coprocessor device driver, " \
39                    "Copyright IBM Corp. 2001, 2018");
40 MODULE_LICENSE("GPL");
41
42 static struct ap_device_id zcrypt_cex2c_card_ids[] = {
43         { .dev_type = AP_DEVICE_TYPE_CEX2C,
44           .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
45         { .dev_type = AP_DEVICE_TYPE_CEX3C,
46           .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
47         { /* end of list */ },
48 };
49
50 MODULE_DEVICE_TABLE(ap, zcrypt_cex2c_card_ids);
51
52 static struct ap_device_id zcrypt_cex2c_queue_ids[] = {
53         { .dev_type = AP_DEVICE_TYPE_CEX2C,
54           .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
55         { .dev_type = AP_DEVICE_TYPE_CEX3C,
56           .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
57         { /* end of list */ },
58 };
59
60 MODULE_DEVICE_TABLE(ap, zcrypt_cex2c_queue_ids);
61
62 /*
63  * CCA card additional device attributes
64  */
65 static ssize_t cca_serialnr_show(struct device *dev,
66                                  struct device_attribute *attr,
67                                  char *buf)
68 {
69         struct cca_info ci;
70         struct ap_card *ac = to_ap_card(dev);
71         struct zcrypt_card *zc = ac->private;
72
73         memset(&ci, 0, sizeof(ci));
74
75         if (ap_domain_index >= 0)
76                 cca_get_info(ac->id, ap_domain_index, &ci, zc->online);
77
78         return scnprintf(buf, PAGE_SIZE, "%s\n", ci.serial);
79 }
80
81 static struct device_attribute dev_attr_cca_serialnr =
82         __ATTR(serialnr, 0444, cca_serialnr_show, NULL);
83
84 static struct attribute *cca_card_attrs[] = {
85         &dev_attr_cca_serialnr.attr,
86         NULL,
87 };
88
89 static const struct attribute_group cca_card_attr_grp = {
90         .attrs = cca_card_attrs,
91 };
92
93  /*
94   * CCA queue additional device attributes
95   */
96 static ssize_t cca_mkvps_show(struct device *dev,
97                               struct device_attribute *attr,
98                               char *buf)
99 {
100         int n = 0;
101         struct cca_info ci;
102         struct zcrypt_queue *zq = to_ap_queue(dev)->private;
103         static const char * const cao_state[] = { "invalid", "valid" };
104         static const char * const new_state[] = { "empty", "partial", "full" };
105
106         memset(&ci, 0, sizeof(ci));
107
108         cca_get_info(AP_QID_CARD(zq->queue->qid),
109                      AP_QID_QUEUE(zq->queue->qid),
110                      &ci, zq->online);
111
112         if (ci.new_mk_state >= '1' && ci.new_mk_state <= '3')
113                 n = scnprintf(buf, PAGE_SIZE, "AES NEW: %s 0x%016llx\n",
114                               new_state[ci.new_mk_state - '1'], ci.new_mkvp);
115         else
116                 n = scnprintf(buf, PAGE_SIZE, "AES NEW: - -\n");
117
118         if (ci.cur_mk_state >= '1' && ci.cur_mk_state <= '2')
119                 n += scnprintf(buf + n, PAGE_SIZE - n,
120                                "AES CUR: %s 0x%016llx\n",
121                                cao_state[ci.cur_mk_state - '1'], ci.cur_mkvp);
122         else
123                 n += scnprintf(buf + n, PAGE_SIZE - n, "AES CUR: - -\n");
124
125         if (ci.old_mk_state >= '1' && ci.old_mk_state <= '2')
126                 n += scnprintf(buf + n, PAGE_SIZE - n,
127                                "AES OLD: %s 0x%016llx\n",
128                                cao_state[ci.old_mk_state - '1'], ci.old_mkvp);
129         else
130                 n += scnprintf(buf + n, PAGE_SIZE - n, "AES OLD: - -\n");
131
132         return n;
133 }
134
135 static struct device_attribute dev_attr_cca_mkvps =
136         __ATTR(mkvps, 0444, cca_mkvps_show, NULL);
137
138 static struct attribute *cca_queue_attrs[] = {
139         &dev_attr_cca_mkvps.attr,
140         NULL,
141 };
142
143 static const struct attribute_group cca_queue_attr_grp = {
144         .attrs = cca_queue_attrs,
145 };
146
147 /**
148  * Large random number detection function. Its sends a message to a CEX2C/CEX3C
149  * card to find out if large random numbers are supported.
150  * @ap_dev: pointer to the AP device.
151  *
152  * Returns 1 if large random numbers are supported, 0 if not and < 0 on error.
153  */
154 static int zcrypt_cex2c_rng_supported(struct ap_queue *aq)
155 {
156         struct ap_message ap_msg;
157         unsigned long long psmid;
158         unsigned int domain;
159         struct {
160                 struct type86_hdr hdr;
161                 struct type86_fmt2_ext fmt2;
162                 struct CPRBX cprbx;
163         } __packed *reply;
164         struct {
165                 struct type6_hdr hdr;
166                 struct CPRBX cprbx;
167                 char function_code[2];
168                 short int rule_length;
169                 char rule[8];
170                 short int verb_length;
171                 short int key_length;
172         } __packed *msg;
173         int rc, i;
174
175         ap_init_message(&ap_msg);
176         ap_msg.msg = (void *) get_zeroed_page(GFP_KERNEL);
177         if (!ap_msg.msg)
178                 return -ENOMEM;
179
180         rng_type6CPRB_msgX(&ap_msg, 4, &domain);
181
182         msg = ap_msg.msg;
183         msg->cprbx.domain = AP_QID_QUEUE(aq->qid);
184
185         rc = ap_send(aq->qid, 0x0102030405060708ULL, ap_msg.msg, ap_msg.len);
186         if (rc)
187                 goto out_free;
188
189         /* Wait for the test message to complete. */
190         for (i = 0; i < 2 * HZ; i++) {
191                 msleep(1000 / HZ);
192                 rc = ap_recv(aq->qid, &psmid, ap_msg.msg, 4096);
193                 if (rc == 0 && psmid == 0x0102030405060708ULL)
194                         break;
195         }
196
197         if (i >= 2 * HZ) {
198                 /* Got no answer. */
199                 rc = -ENODEV;
200                 goto out_free;
201         }
202
203         reply = ap_msg.msg;
204         if (reply->cprbx.ccp_rtcode == 0 && reply->cprbx.ccp_rscode == 0)
205                 rc = 1;
206         else
207                 rc = 0;
208 out_free:
209         free_page((unsigned long) ap_msg.msg);
210         return rc;
211 }
212
213 /**
214  * Probe function for CEX2C/CEX3C card devices. It always accepts the
215  * AP device since the bus_match already checked the hardware type.
216  * @ap_dev: pointer to the AP card device.
217  */
218 static int zcrypt_cex2c_card_probe(struct ap_device *ap_dev)
219 {
220         /*
221          * Normalized speed ratings per crypto adapter
222          * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
223          */
224         static const int CEX2C_SPEED_IDX[] = {
225                 1000, 1400, 2400, 1100, 1500, 2600, 100, 12};
226         static const int CEX3C_SPEED_IDX[] = {
227                 500,  700, 1400,  550,  800, 1500,  80, 10};
228
229         struct ap_card *ac = to_ap_card(&ap_dev->device);
230         struct zcrypt_card *zc;
231         int rc = 0;
232
233         zc = zcrypt_card_alloc();
234         if (!zc)
235                 return -ENOMEM;
236         zc->card = ac;
237         ac->private = zc;
238         switch (ac->ap_dev.device_type) {
239         case AP_DEVICE_TYPE_CEX2C:
240                 zc->user_space_type = ZCRYPT_CEX2C;
241                 zc->type_string = "CEX2C";
242                 memcpy(zc->speed_rating, CEX2C_SPEED_IDX,
243                        sizeof(CEX2C_SPEED_IDX));
244                 zc->min_mod_size = CEX2C_MIN_MOD_SIZE;
245                 zc->max_mod_size = CEX2C_MAX_MOD_SIZE;
246                 zc->max_exp_bit_length = CEX2C_MAX_MOD_SIZE;
247                 break;
248         case AP_DEVICE_TYPE_CEX3C:
249                 zc->user_space_type = ZCRYPT_CEX3C;
250                 zc->type_string = "CEX3C";
251                 memcpy(zc->speed_rating, CEX3C_SPEED_IDX,
252                        sizeof(CEX3C_SPEED_IDX));
253                 zc->min_mod_size = CEX3C_MIN_MOD_SIZE;
254                 zc->max_mod_size = CEX3C_MAX_MOD_SIZE;
255                 zc->max_exp_bit_length = CEX3C_MAX_MOD_SIZE;
256                 break;
257         default:
258                 zcrypt_card_free(zc);
259                 return -ENODEV;
260         }
261         zc->online = 1;
262
263         rc = zcrypt_card_register(zc);
264         if (rc) {
265                 ac->private = NULL;
266                 zcrypt_card_free(zc);
267                 return rc;
268         }
269
270         if (ap_test_bit(&ac->functions, AP_FUNC_COPRO)) {
271                 rc = sysfs_create_group(&ap_dev->device.kobj,
272                                         &cca_card_attr_grp);
273                 if (rc) {
274                         zcrypt_card_unregister(zc);
275                         ac->private = NULL;
276                         zcrypt_card_free(zc);
277                 }
278         }
279
280         return rc;
281 }
282
283 /**
284  * This is called to remove the CEX2C/CEX3C card driver information
285  * if an AP card device is removed.
286  */
287 static void zcrypt_cex2c_card_remove(struct ap_device *ap_dev)
288 {
289         struct ap_card *ac = to_ap_card(&ap_dev->device);
290         struct zcrypt_card *zc = to_ap_card(&ap_dev->device)->private;
291
292         if (ap_test_bit(&ac->functions, AP_FUNC_COPRO))
293                 sysfs_remove_group(&ap_dev->device.kobj, &cca_card_attr_grp);
294         if (zc)
295                 zcrypt_card_unregister(zc);
296 }
297
298 static struct ap_driver zcrypt_cex2c_card_driver = {
299         .probe = zcrypt_cex2c_card_probe,
300         .remove = zcrypt_cex2c_card_remove,
301         .ids = zcrypt_cex2c_card_ids,
302         .flags = AP_DRIVER_FLAG_DEFAULT,
303 };
304
305 /**
306  * Probe function for CEX2C/CEX3C queue devices. It always accepts the
307  * AP device since the bus_match already checked the hardware type.
308  * @ap_dev: pointer to the AP card device.
309  */
310 static int zcrypt_cex2c_queue_probe(struct ap_device *ap_dev)
311 {
312         struct ap_queue *aq = to_ap_queue(&ap_dev->device);
313         struct zcrypt_queue *zq;
314         int rc;
315
316         zq = zcrypt_queue_alloc(CEX2C_MAX_XCRB_MESSAGE_SIZE);
317         if (!zq)
318                 return -ENOMEM;
319         zq->queue = aq;
320         zq->online = 1;
321         atomic_set(&zq->load, 0);
322         ap_rapq(aq->qid);
323         rc = zcrypt_cex2c_rng_supported(aq);
324         if (rc < 0) {
325                 zcrypt_queue_free(zq);
326                 return rc;
327         }
328         if (rc)
329                 zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
330                                          MSGTYPE06_VARIANT_DEFAULT);
331         else
332                 zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
333                                          MSGTYPE06_VARIANT_NORNG);
334         ap_queue_init_state(aq);
335         ap_queue_init_reply(aq, &zq->reply);
336         aq->request_timeout = CEX2C_CLEANUP_TIME;
337         aq->private = zq;
338         rc = zcrypt_queue_register(zq);
339         if (rc) {
340                 aq->private = NULL;
341                 zcrypt_queue_free(zq);
342                 return rc;
343         }
344
345         if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO)) {
346                 rc = sysfs_create_group(&ap_dev->device.kobj,
347                                         &cca_queue_attr_grp);
348                 if (rc) {
349                         zcrypt_queue_unregister(zq);
350                         aq->private = NULL;
351                         zcrypt_queue_free(zq);
352                 }
353         }
354
355         return rc;
356 }
357
358 /**
359  * This is called to remove the CEX2C/CEX3C queue driver information
360  * if an AP queue device is removed.
361  */
362 static void zcrypt_cex2c_queue_remove(struct ap_device *ap_dev)
363 {
364         struct ap_queue *aq = to_ap_queue(&ap_dev->device);
365         struct zcrypt_queue *zq = aq->private;
366
367         if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO))
368                 sysfs_remove_group(&ap_dev->device.kobj, &cca_queue_attr_grp);
369         if (zq)
370                 zcrypt_queue_unregister(zq);
371 }
372
373 static struct ap_driver zcrypt_cex2c_queue_driver = {
374         .probe = zcrypt_cex2c_queue_probe,
375         .remove = zcrypt_cex2c_queue_remove,
376         .ids = zcrypt_cex2c_queue_ids,
377         .flags = AP_DRIVER_FLAG_DEFAULT,
378 };
379
380 int __init zcrypt_cex2c_init(void)
381 {
382         int rc;
383
384         rc = ap_driver_register(&zcrypt_cex2c_card_driver,
385                                 THIS_MODULE, "cex2card");
386         if (rc)
387                 return rc;
388
389         rc = ap_driver_register(&zcrypt_cex2c_queue_driver,
390                                 THIS_MODULE, "cex2cqueue");
391         if (rc)
392                 ap_driver_unregister(&zcrypt_cex2c_card_driver);
393
394         return rc;
395 }
396
397 void zcrypt_cex2c_exit(void)
398 {
399         ap_driver_unregister(&zcrypt_cex2c_queue_driver);
400         ap_driver_unregister(&zcrypt_cex2c_card_driver);
401 }
402
403 module_init(zcrypt_cex2c_init);
404 module_exit(zcrypt_cex2c_exit);