GNU Linux-libre 5.19-rc6-gnu
[releases.git] / drivers / s390 / crypto / zcrypt_cex2a.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Copyright IBM Corp. 2001, 2012
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/slab.h>
15 #include <linux/init.h>
16 #include <linux/err.h>
17 #include <linux/atomic.h>
18 #include <linux/uaccess.h>
19 #include <linux/mod_devicetable.h>
20
21 #include "ap_bus.h"
22 #include "zcrypt_api.h"
23 #include "zcrypt_error.h"
24 #include "zcrypt_cex2a.h"
25 #include "zcrypt_msgtype50.h"
26
27 #define CEX2A_MIN_MOD_SIZE        1     /*    8 bits    */
28 #define CEX2A_MAX_MOD_SIZE      256     /* 2048 bits    */
29 #define CEX3A_MIN_MOD_SIZE      CEX2A_MIN_MOD_SIZE
30 #define CEX3A_MAX_MOD_SIZE      512     /* 4096 bits    */
31
32 #define CEX2A_MAX_MESSAGE_SIZE  0x390   /* sizeof(struct type50_crb2_msg)    */
33 #define CEX2A_MAX_RESPONSE_SIZE 0x110   /* max outputdatalength + type80_hdr */
34
35 #define CEX3A_MAX_RESPONSE_SIZE 0x210   /* 512 bit modulus
36                                          * (max outputdatalength) +
37                                          * type80_hdr
38                                          */
39 #define CEX3A_MAX_MESSAGE_SIZE  sizeof(struct type50_crb3_msg)
40
41 #define CEX2A_CLEANUP_TIME      (15 * HZ)
42 #define CEX3A_CLEANUP_TIME      CEX2A_CLEANUP_TIME
43
44 MODULE_AUTHOR("IBM Corporation");
45 MODULE_DESCRIPTION("CEX2A/CEX3A Cryptographic Coprocessor device driver, " \
46                    "Copyright IBM Corp. 2001, 2018");
47 MODULE_LICENSE("GPL");
48
49 static struct ap_device_id zcrypt_cex2a_card_ids[] = {
50         { .dev_type = AP_DEVICE_TYPE_CEX2A,
51           .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
52         { .dev_type = AP_DEVICE_TYPE_CEX3A,
53           .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
54         { /* end of list */ },
55 };
56
57 MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_card_ids);
58
59 static struct ap_device_id zcrypt_cex2a_queue_ids[] = {
60         { .dev_type = AP_DEVICE_TYPE_CEX2A,
61           .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
62         { .dev_type = AP_DEVICE_TYPE_CEX3A,
63           .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
64         { /* end of list */ },
65 };
66
67 MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_queue_ids);
68
69 /*
70  * Probe function for CEX2A card devices. It always accepts the AP device
71  * since the bus_match already checked the card type.
72  * @ap_dev: pointer to the AP device.
73  */
74 static int zcrypt_cex2a_card_probe(struct ap_device *ap_dev)
75 {
76         /*
77          * Normalized speed ratings per crypto adapter
78          * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
79          */
80         static const int CEX2A_SPEED_IDX[] = {
81                 800, 1000, 2000,  900, 1200, 2400, 0, 0};
82         static const int CEX3A_SPEED_IDX[] = {
83                 400,  500, 1000,  450,  550, 1200, 0, 0};
84
85         struct ap_card *ac = to_ap_card(&ap_dev->device);
86         struct zcrypt_card *zc;
87         int rc = 0;
88
89         zc = zcrypt_card_alloc();
90         if (!zc)
91                 return -ENOMEM;
92         zc->card = ac;
93         dev_set_drvdata(&ap_dev->device, zc);
94
95         if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX2A) {
96                 zc->min_mod_size = CEX2A_MIN_MOD_SIZE;
97                 zc->max_mod_size = CEX2A_MAX_MOD_SIZE;
98                 zc->speed_rating = CEX2A_SPEED_IDX;
99                 zc->max_exp_bit_length = CEX2A_MAX_MOD_SIZE;
100                 zc->type_string = "CEX2A";
101                 zc->user_space_type = ZCRYPT_CEX2A;
102         } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX3A) {
103                 zc->min_mod_size = CEX2A_MIN_MOD_SIZE;
104                 zc->max_mod_size = CEX2A_MAX_MOD_SIZE;
105                 zc->max_exp_bit_length = CEX2A_MAX_MOD_SIZE;
106                 if (ap_test_bit(&ac->functions, AP_FUNC_MEX4K) &&
107                     ap_test_bit(&ac->functions, AP_FUNC_CRT4K)) {
108                         zc->max_mod_size = CEX3A_MAX_MOD_SIZE;
109                         zc->max_exp_bit_length = CEX3A_MAX_MOD_SIZE;
110                 }
111                 zc->speed_rating = CEX3A_SPEED_IDX;
112                 zc->type_string = "CEX3A";
113                 zc->user_space_type = ZCRYPT_CEX3A;
114         } else {
115                 zcrypt_card_free(zc);
116                 return -ENODEV;
117         }
118         zc->online = 1;
119
120         rc = zcrypt_card_register(zc);
121         if (rc)
122                 zcrypt_card_free(zc);
123
124         return rc;
125 }
126
127 /*
128  * This is called to remove the CEX2A card driver information
129  * if an AP card device is removed.
130  */
131 static void zcrypt_cex2a_card_remove(struct ap_device *ap_dev)
132 {
133         struct zcrypt_card *zc = dev_get_drvdata(&ap_dev->device);
134
135         zcrypt_card_unregister(zc);
136 }
137
138 static struct ap_driver zcrypt_cex2a_card_driver = {
139         .probe = zcrypt_cex2a_card_probe,
140         .remove = zcrypt_cex2a_card_remove,
141         .ids = zcrypt_cex2a_card_ids,
142         .flags = AP_DRIVER_FLAG_DEFAULT,
143 };
144
145 /*
146  * Probe function for CEX2A queue devices. It always accepts the AP device
147  * since the bus_match already checked the queue type.
148  * @ap_dev: pointer to the AP device.
149  */
150 static int zcrypt_cex2a_queue_probe(struct ap_device *ap_dev)
151 {
152         struct ap_queue *aq = to_ap_queue(&ap_dev->device);
153         struct zcrypt_queue *zq = NULL;
154         int rc;
155
156         switch (ap_dev->device_type) {
157         case AP_DEVICE_TYPE_CEX2A:
158                 zq = zcrypt_queue_alloc(CEX2A_MAX_RESPONSE_SIZE);
159                 if (!zq)
160                         return -ENOMEM;
161                 break;
162         case AP_DEVICE_TYPE_CEX3A:
163                 zq = zcrypt_queue_alloc(CEX3A_MAX_RESPONSE_SIZE);
164                 if (!zq)
165                         return -ENOMEM;
166                 break;
167         }
168         if (!zq)
169                 return -ENODEV;
170         zq->ops = zcrypt_msgtype(MSGTYPE50_NAME, MSGTYPE50_VARIANT_DEFAULT);
171         zq->queue = aq;
172         zq->online = 1;
173         atomic_set(&zq->load, 0);
174         ap_queue_init_state(aq);
175         ap_queue_init_reply(aq, &zq->reply);
176         aq->request_timeout = CEX2A_CLEANUP_TIME;
177         dev_set_drvdata(&ap_dev->device, zq);
178         rc = zcrypt_queue_register(zq);
179         if (rc)
180                 zcrypt_queue_free(zq);
181
182         return rc;
183 }
184
185 /*
186  * This is called to remove the CEX2A queue driver information
187  * if an AP queue device is removed.
188  */
189 static void zcrypt_cex2a_queue_remove(struct ap_device *ap_dev)
190 {
191         struct zcrypt_queue *zq = dev_get_drvdata(&ap_dev->device);
192
193         zcrypt_queue_unregister(zq);
194 }
195
196 static struct ap_driver zcrypt_cex2a_queue_driver = {
197         .probe = zcrypt_cex2a_queue_probe,
198         .remove = zcrypt_cex2a_queue_remove,
199         .ids = zcrypt_cex2a_queue_ids,
200         .flags = AP_DRIVER_FLAG_DEFAULT,
201 };
202
203 int __init zcrypt_cex2a_init(void)
204 {
205         int rc;
206
207         rc = ap_driver_register(&zcrypt_cex2a_card_driver,
208                                 THIS_MODULE, "cex2acard");
209         if (rc)
210                 return rc;
211
212         rc = ap_driver_register(&zcrypt_cex2a_queue_driver,
213                                 THIS_MODULE, "cex2aqueue");
214         if (rc)
215                 ap_driver_unregister(&zcrypt_cex2a_card_driver);
216
217         return rc;
218 }
219
220 void __exit zcrypt_cex2a_exit(void)
221 {
222         ap_driver_unregister(&zcrypt_cex2a_queue_driver);
223         ap_driver_unregister(&zcrypt_cex2a_card_driver);
224 }
225
226 module_init(zcrypt_cex2a_init);
227 module_exit(zcrypt_cex2a_exit);