GNU Linux-libre 4.14.328-gnu1
[releases.git] / drivers / s390 / crypto / zcrypt_pcixcc.c
1 /*
2  *  zcrypt 2.1.0
3  *
4  *  Copyright IBM Corp. 2001, 2012
5  *  Author(s): Robert Burroughs
6  *             Eric Rossman (edrossma@us.ibm.com)
7  *
8  *  Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
9  *  Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
10  *                                Ralph Wuerthner <rwuerthn@de.ibm.com>
11  *  MSGTYPE restruct:             Holger Dengler <hd@linux.vnet.ibm.com>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2, or (at your option)
16  * any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26  */
27
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/err.h>
31 #include <linux/delay.h>
32 #include <linux/slab.h>
33 #include <linux/atomic.h>
34 #include <linux/uaccess.h>
35 #include <linux/mod_devicetable.h>
36
37 #include "ap_bus.h"
38 #include "zcrypt_api.h"
39 #include "zcrypt_error.h"
40 #include "zcrypt_msgtype6.h"
41 #include "zcrypt_pcixcc.h"
42 #include "zcrypt_cca_key.h"
43
44 #define PCIXCC_MIN_MOD_SIZE      16     /*  128 bits    */
45 #define PCIXCC_MIN_MOD_SIZE_OLD  64     /*  512 bits    */
46 #define PCIXCC_MAX_MOD_SIZE     256     /* 2048 bits    */
47 #define CEX3C_MIN_MOD_SIZE      PCIXCC_MIN_MOD_SIZE
48 #define CEX3C_MAX_MOD_SIZE      512     /* 4096 bits    */
49
50 #define PCIXCC_MAX_ICA_MESSAGE_SIZE 0x77c  /* max size type6 v2 crt message */
51 #define PCIXCC_MAX_ICA_RESPONSE_SIZE 0x77c /* max size type86 v2 reply      */
52
53 #define PCIXCC_MAX_XCRB_MESSAGE_SIZE (12*1024)
54
55 #define PCIXCC_CLEANUP_TIME     (15*HZ)
56
57 #define CEIL4(x) ((((x)+3)/4)*4)
58
59 struct response_type {
60         struct completion work;
61         int type;
62 };
63 #define PCIXCC_RESPONSE_TYPE_ICA  0
64 #define PCIXCC_RESPONSE_TYPE_XCRB 1
65
66 MODULE_AUTHOR("IBM Corporation");
67 MODULE_DESCRIPTION("PCIXCC Cryptographic Coprocessor device driver, " \
68                    "Copyright IBM Corp. 2001, 2012");
69 MODULE_LICENSE("GPL");
70
71 static struct ap_device_id zcrypt_pcixcc_card_ids[] = {
72         { .dev_type = AP_DEVICE_TYPE_PCIXCC,
73           .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
74         { .dev_type = AP_DEVICE_TYPE_CEX2C,
75           .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
76         { .dev_type = AP_DEVICE_TYPE_CEX3C,
77           .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
78         { /* end of list */ },
79 };
80
81 MODULE_DEVICE_TABLE(ap, zcrypt_pcixcc_card_ids);
82
83 static struct ap_device_id zcrypt_pcixcc_queue_ids[] = {
84         { .dev_type = AP_DEVICE_TYPE_PCIXCC,
85           .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
86         { .dev_type = AP_DEVICE_TYPE_CEX2C,
87           .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
88         { .dev_type = AP_DEVICE_TYPE_CEX3C,
89           .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
90         { /* end of list */ },
91 };
92
93 MODULE_DEVICE_TABLE(ap, zcrypt_pcixcc_queue_ids);
94
95 /**
96  * Large random number detection function. Its sends a message to a pcixcc
97  * card to find out if large random numbers are supported.
98  * @ap_dev: pointer to the AP device.
99  *
100  * Returns 1 if large random numbers are supported, 0 if not and < 0 on error.
101  */
102 static int zcrypt_pcixcc_rng_supported(struct ap_queue *aq)
103 {
104         struct ap_message ap_msg;
105         unsigned long long psmid;
106         unsigned int domain;
107         struct {
108                 struct type86_hdr hdr;
109                 struct type86_fmt2_ext fmt2;
110                 struct CPRBX cprbx;
111         } __attribute__((packed)) *reply;
112         struct {
113                 struct type6_hdr hdr;
114                 struct CPRBX cprbx;
115                 char function_code[2];
116                 short int rule_length;
117                 char rule[8];
118                 short int verb_length;
119                 short int key_length;
120         } __packed * msg;
121         int rc, i;
122
123         ap_init_message(&ap_msg);
124         ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
125         if (!ap_msg.message)
126                 return -ENOMEM;
127
128         rng_type6CPRB_msgX(&ap_msg, 4, &domain);
129
130         msg = ap_msg.message;
131         msg->cprbx.domain = AP_QID_QUEUE(aq->qid);
132
133         rc = ap_send(aq->qid, 0x0102030405060708ULL, ap_msg.message,
134                      ap_msg.length);
135         if (rc)
136                 goto out_free;
137
138         /* Wait for the test message to complete. */
139         for (i = 0; i < 2 * HZ; i++) {
140                 msleep(1000 / HZ);
141                 rc = ap_recv(aq->qid, &psmid, ap_msg.message, 4096);
142                 if (rc == 0 && psmid == 0x0102030405060708ULL)
143                         break;
144         }
145
146         if (i >= 2 * HZ) {
147                 /* Got no answer. */
148                 rc = -ENODEV;
149                 goto out_free;
150         }
151
152         reply = ap_msg.message;
153         if (reply->cprbx.ccp_rtcode == 0 && reply->cprbx.ccp_rscode == 0)
154                 rc = 1;
155         else
156                 rc = 0;
157 out_free:
158         free_page((unsigned long) ap_msg.message);
159         return rc;
160 }
161
162 /**
163  * Probe function for PCIXCC/CEX2C card devices. It always accepts the
164  * AP device since the bus_match already checked the hardware type. The
165  * PCIXCC cards come in two flavours: micro code level 2 and micro code
166  * level 3. This is checked by sending a test message to the device.
167  * @ap_dev: pointer to the AP card device.
168  */
169 static int zcrypt_pcixcc_card_probe(struct ap_device *ap_dev)
170 {
171         /*
172          * Normalized speed ratings per crypto adapter
173          * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
174          */
175         static const int CEX2C_SPEED_IDX[] = {
176                 1000, 1400, 2400, 1100, 1500, 2600, 100, 12};
177         static const int CEX3C_SPEED_IDX[] = {
178                 500,  700, 1400,  550,  800, 1500,  80, 10};
179
180         struct ap_card *ac = to_ap_card(&ap_dev->device);
181         struct zcrypt_card *zc;
182         int rc = 0;
183
184         zc = zcrypt_card_alloc();
185         if (!zc)
186                 return -ENOMEM;
187         zc->card = ac;
188         ac->private = zc;
189         switch (ac->ap_dev.device_type) {
190         case AP_DEVICE_TYPE_CEX2C:
191                 zc->user_space_type = ZCRYPT_CEX2C;
192                 zc->type_string = "CEX2C";
193                 memcpy(zc->speed_rating, CEX2C_SPEED_IDX,
194                        sizeof(CEX2C_SPEED_IDX));
195                 zc->min_mod_size = PCIXCC_MIN_MOD_SIZE;
196                 zc->max_mod_size = PCIXCC_MAX_MOD_SIZE;
197                 zc->max_exp_bit_length = PCIXCC_MAX_MOD_SIZE;
198                 break;
199         case AP_DEVICE_TYPE_CEX3C:
200                 zc->user_space_type = ZCRYPT_CEX3C;
201                 zc->type_string = "CEX3C";
202                 memcpy(zc->speed_rating, CEX3C_SPEED_IDX,
203                        sizeof(CEX3C_SPEED_IDX));
204                 zc->min_mod_size = CEX3C_MIN_MOD_SIZE;
205                 zc->max_mod_size = CEX3C_MAX_MOD_SIZE;
206                 zc->max_exp_bit_length = CEX3C_MAX_MOD_SIZE;
207                 break;
208         default:
209                 zcrypt_card_free(zc);
210                 return -ENODEV;
211         }
212         zc->online = 1;
213
214         rc = zcrypt_card_register(zc);
215         if (rc) {
216                 ac->private = NULL;
217                 zcrypt_card_free(zc);
218         }
219
220         return rc;
221 }
222
223 /**
224  * This is called to remove the PCIXCC/CEX2C card driver information
225  * if an AP card device is removed.
226  */
227 static void zcrypt_pcixcc_card_remove(struct ap_device *ap_dev)
228 {
229         struct zcrypt_card *zc = to_ap_card(&ap_dev->device)->private;
230
231         if (zc)
232                 zcrypt_card_unregister(zc);
233 }
234
235 static struct ap_driver zcrypt_pcixcc_card_driver = {
236         .probe = zcrypt_pcixcc_card_probe,
237         .remove = zcrypt_pcixcc_card_remove,
238         .ids = zcrypt_pcixcc_card_ids,
239 };
240
241 /**
242  * Probe function for PCIXCC/CEX2C queue devices. It always accepts the
243  * AP device since the bus_match already checked the hardware type. The
244  * PCIXCC cards come in two flavours: micro code level 2 and micro code
245  * level 3. This is checked by sending a test message to the device.
246  * @ap_dev: pointer to the AP card device.
247  */
248 static int zcrypt_pcixcc_queue_probe(struct ap_device *ap_dev)
249 {
250         struct ap_queue *aq = to_ap_queue(&ap_dev->device);
251         struct zcrypt_queue *zq;
252         int rc;
253
254         zq = zcrypt_queue_alloc(PCIXCC_MAX_XCRB_MESSAGE_SIZE);
255         if (!zq)
256                 return -ENOMEM;
257         zq->queue = aq;
258         zq->online = 1;
259         atomic_set(&zq->load, 0);
260         rc = zcrypt_pcixcc_rng_supported(aq);
261         if (rc < 0) {
262                 zcrypt_queue_free(zq);
263                 return rc;
264         }
265         if (rc)
266                 zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
267                                          MSGTYPE06_VARIANT_DEFAULT);
268         else
269                 zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
270                                          MSGTYPE06_VARIANT_NORNG);
271         ap_queue_init_reply(aq, &zq->reply);
272         aq->request_timeout = PCIXCC_CLEANUP_TIME,
273         aq->private = zq;
274         rc = zcrypt_queue_register(zq);
275         if (rc) {
276                 aq->private = NULL;
277                 zcrypt_queue_free(zq);
278         }
279         return rc;
280 }
281
282 /**
283  * This is called to remove the PCIXCC/CEX2C queue driver information
284  * if an AP queue device is removed.
285  */
286 static void zcrypt_pcixcc_queue_remove(struct ap_device *ap_dev)
287 {
288         struct ap_queue *aq = to_ap_queue(&ap_dev->device);
289         struct zcrypt_queue *zq = aq->private;
290
291         ap_queue_remove(aq);
292         if (zq)
293                 zcrypt_queue_unregister(zq);
294 }
295
296 static struct ap_driver zcrypt_pcixcc_queue_driver = {
297         .probe = zcrypt_pcixcc_queue_probe,
298         .remove = zcrypt_pcixcc_queue_remove,
299         .suspend = ap_queue_suspend,
300         .resume = ap_queue_resume,
301         .ids = zcrypt_pcixcc_queue_ids,
302 };
303
304 int __init zcrypt_pcixcc_init(void)
305 {
306         int rc;
307
308         rc = ap_driver_register(&zcrypt_pcixcc_card_driver,
309                                 THIS_MODULE, "pcixcccard");
310         if (rc)
311                 return rc;
312
313         rc = ap_driver_register(&zcrypt_pcixcc_queue_driver,
314                                 THIS_MODULE, "pcixccqueue");
315         if (rc)
316                 ap_driver_unregister(&zcrypt_pcixcc_card_driver);
317
318         return rc;
319 }
320
321 void zcrypt_pcixcc_exit(void)
322 {
323         ap_driver_unregister(&zcrypt_pcixcc_queue_driver);
324         ap_driver_unregister(&zcrypt_pcixcc_card_driver);
325 }
326
327 module_init(zcrypt_pcixcc_init);
328 module_exit(zcrypt_pcixcc_exit);