Commit | Line | Data |
---|---|---|
6684af1a | 1 | /* |
5432114b | 2 | * zcrypt 2.1.0 |
6684af1a | 3 | * |
5e55a488 | 4 | * Copyright IBM Corp. 2001, 2012 |
6684af1a MS |
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> | |
5e55a488 | 11 | * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com> |
6684af1a MS |
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> | |
5a0e3ad6 | 32 | #include <linux/slab.h> |
60063497 | 33 | #include <linux/atomic.h> |
6684af1a MS |
34 | #include <asm/uaccess.h> |
35 | ||
36 | #include "ap_bus.h" | |
37 | #include "zcrypt_api.h" | |
38 | #include "zcrypt_error.h" | |
5e55a488 | 39 | #include "zcrypt_msgtype6.h" |
6684af1a MS |
40 | #include "zcrypt_pcixcc.h" |
41 | #include "zcrypt_cca_key.h" | |
42 | ||
43 | #define PCIXCC_MIN_MOD_SIZE 16 /* 128 bits */ | |
44 | #define PCIXCC_MIN_MOD_SIZE_OLD 64 /* 512 bits */ | |
45 | #define PCIXCC_MAX_MOD_SIZE 256 /* 2048 bits */ | |
8e89b6be | 46 | #define CEX3C_MIN_MOD_SIZE PCIXCC_MIN_MOD_SIZE |
2ade1fab | 47 | #define CEX3C_MAX_MOD_SIZE 512 /* 4096 bits */ |
6684af1a | 48 | |
6684af1a MS |
49 | #define PCIXCC_MAX_ICA_MESSAGE_SIZE 0x77c /* max size type6 v2 crt message */ |
50 | #define PCIXCC_MAX_ICA_RESPONSE_SIZE 0x77c /* max size type86 v2 reply */ | |
51 | ||
52 | #define PCIXCC_MAX_XCRB_MESSAGE_SIZE (12*1024) | |
6684af1a MS |
53 | |
54 | #define PCIXCC_CLEANUP_TIME (15*HZ) | |
55 | ||
5432114b RW |
56 | #define CEIL4(x) ((((x)+3)/4)*4) |
57 | ||
58 | struct response_type { | |
59 | struct completion work; | |
60 | int type; | |
61 | }; | |
62 | #define PCIXCC_RESPONSE_TYPE_ICA 0 | |
63 | #define PCIXCC_RESPONSE_TYPE_XCRB 1 | |
64 | ||
6684af1a MS |
65 | static struct ap_device_id zcrypt_pcixcc_ids[] = { |
66 | { AP_DEVICE(AP_DEVICE_TYPE_PCIXCC) }, | |
67 | { AP_DEVICE(AP_DEVICE_TYPE_CEX2C) }, | |
ffda4f71 | 68 | { AP_DEVICE(AP_DEVICE_TYPE_CEX3C) }, |
6684af1a MS |
69 | { /* end of list */ }, |
70 | }; | |
71 | ||
6684af1a MS |
72 | MODULE_DEVICE_TABLE(ap, zcrypt_pcixcc_ids); |
73 | MODULE_AUTHOR("IBM Corporation"); | |
5e55a488 HD |
74 | MODULE_DESCRIPTION("PCIXCC Cryptographic Coprocessor device driver, " \ |
75 | "Copyright IBM Corp. 2001, 2012"); | |
6684af1a | 76 | MODULE_LICENSE("GPL"); |
6684af1a MS |
77 | |
78 | static int zcrypt_pcixcc_probe(struct ap_device *ap_dev); | |
79 | static void zcrypt_pcixcc_remove(struct ap_device *ap_dev); | |
6684af1a MS |
80 | |
81 | static struct ap_driver zcrypt_pcixcc_driver = { | |
82 | .probe = zcrypt_pcixcc_probe, | |
83 | .remove = zcrypt_pcixcc_remove, | |
6684af1a | 84 | .ids = zcrypt_pcixcc_ids, |
af512ed0 | 85 | .request_timeout = PCIXCC_CLEANUP_TIME, |
6684af1a MS |
86 | }; |
87 | ||
6684af1a MS |
88 | /** |
89 | * Micro-code detection function. Its sends a message to a pcixcc card | |
90 | * to find out the microcode level. | |
91 | * @ap_dev: pointer to the AP device. | |
92 | */ | |
93 | static int zcrypt_pcixcc_mcl(struct ap_device *ap_dev) | |
94 | { | |
95 | static unsigned char msg[] = { | |
96 | 0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00, | |
97 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
98 | 0x00,0x00,0x00,0x58,0x00,0x00,0x00,0x00, | |
99 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
100 | 0x43,0x41,0x00,0x00,0x00,0x00,0x00,0x00, | |
101 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
102 | 0x00,0x00,0x00,0x00,0x50,0x4B,0x00,0x00, | |
103 | 0x00,0x00,0x01,0xC4,0x00,0x00,0x00,0x00, | |
104 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
105 | 0x00,0x00,0x07,0x24,0x00,0x00,0x00,0x00, | |
106 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
107 | 0x00,0xDC,0x02,0x00,0x00,0x00,0x54,0x32, | |
108 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE8, | |
109 | 0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x24, | |
110 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
111 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
112 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
113 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
114 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
115 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
116 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
117 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
118 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
119 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
120 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
121 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
122 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
123 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
124 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
125 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
126 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
127 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
128 | 0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x00, | |
129 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
130 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
131 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
132 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
133 | 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
134 | 0x00,0x00,0x00,0x00,0x50,0x4B,0x00,0x0A, | |
135 | 0x4D,0x52,0x50,0x20,0x20,0x20,0x20,0x20, | |
136 | 0x00,0x42,0x00,0x01,0x02,0x03,0x04,0x05, | |
137 | 0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D, | |
138 | 0x0E,0x0F,0x00,0x11,0x22,0x33,0x44,0x55, | |
139 | 0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD, | |
140 | 0xEE,0xFF,0xFF,0xEE,0xDD,0xCC,0xBB,0xAA, | |
141 | 0x99,0x88,0x77,0x66,0x55,0x44,0x33,0x22, | |
142 | 0x11,0x00,0x01,0x23,0x45,0x67,0x89,0xAB, | |
143 | 0xCD,0xEF,0xFE,0xDC,0xBA,0x98,0x76,0x54, | |
144 | 0x32,0x10,0x00,0x9A,0x00,0x98,0x00,0x00, | |
145 | 0x1E,0x00,0x00,0x94,0x00,0x00,0x00,0x00, | |
146 | 0x04,0x00,0x00,0x8C,0x00,0x00,0x00,0x40, | |
147 | 0x02,0x00,0x00,0x40,0xBA,0xE8,0x23,0x3C, | |
148 | 0x75,0xF3,0x91,0x61,0xD6,0x73,0x39,0xCF, | |
149 | 0x7B,0x6D,0x8E,0x61,0x97,0x63,0x9E,0xD9, | |
150 | 0x60,0x55,0xD6,0xC7,0xEF,0xF8,0x1E,0x63, | |
151 | 0x95,0x17,0xCC,0x28,0x45,0x60,0x11,0xC5, | |
152 | 0xC4,0x4E,0x66,0xC6,0xE6,0xC3,0xDE,0x8A, | |
153 | 0x19,0x30,0xCF,0x0E,0xD7,0xAA,0xDB,0x01, | |
154 | 0xD8,0x00,0xBB,0x8F,0x39,0x9F,0x64,0x28, | |
155 | 0xF5,0x7A,0x77,0x49,0xCC,0x6B,0xA3,0x91, | |
156 | 0x97,0x70,0xE7,0x60,0x1E,0x39,0xE1,0xE5, | |
157 | 0x33,0xE1,0x15,0x63,0x69,0x08,0x80,0x4C, | |
158 | 0x67,0xC4,0x41,0x8F,0x48,0xDF,0x26,0x98, | |
159 | 0xF1,0xD5,0x8D,0x88,0xD9,0x6A,0xA4,0x96, | |
160 | 0xC5,0x84,0xD9,0x30,0x49,0x67,0x7D,0x19, | |
161 | 0xB1,0xB3,0x45,0x4D,0xB2,0x53,0x9A,0x47, | |
162 | 0x3C,0x7C,0x55,0xBF,0xCC,0x85,0x00,0x36, | |
163 | 0xF1,0x3D,0x93,0x53 | |
164 | }; | |
165 | unsigned long long psmid; | |
166 | struct CPRBX *cprbx; | |
167 | char *reply; | |
168 | int rc, i; | |
169 | ||
170 | reply = (void *) get_zeroed_page(GFP_KERNEL); | |
171 | if (!reply) | |
172 | return -ENOMEM; | |
173 | ||
174 | rc = ap_send(ap_dev->qid, 0x0102030405060708ULL, msg, sizeof(msg)); | |
175 | if (rc) | |
176 | goto out_free; | |
177 | ||
178 | /* Wait for the test message to complete. */ | |
179 | for (i = 0; i < 6; i++) { | |
e4e1899a | 180 | msleep(300); |
6684af1a MS |
181 | rc = ap_recv(ap_dev->qid, &psmid, reply, 4096); |
182 | if (rc == 0 && psmid == 0x0102030405060708ULL) | |
183 | break; | |
184 | } | |
185 | ||
186 | if (i >= 6) { | |
187 | /* Got no answer. */ | |
188 | rc = -ENODEV; | |
189 | goto out_free; | |
190 | } | |
191 | ||
192 | cprbx = (struct CPRBX *) (reply + 48); | |
193 | if (cprbx->ccp_rtcode == 8 && cprbx->ccp_rscode == 33) | |
194 | rc = ZCRYPT_PCIXCC_MCL2; | |
195 | else | |
196 | rc = ZCRYPT_PCIXCC_MCL3; | |
197 | out_free: | |
198 | free_page((unsigned long) reply); | |
199 | return rc; | |
200 | } | |
201 | ||
2f7c8bd6 RW |
202 | /** |
203 | * Large random number detection function. Its sends a message to a pcixcc | |
204 | * card to find out if large random numbers are supported. | |
205 | * @ap_dev: pointer to the AP device. | |
206 | * | |
207 | * Returns 1 if large random numbers are supported, 0 if not and < 0 on error. | |
208 | */ | |
209 | static int zcrypt_pcixcc_rng_supported(struct ap_device *ap_dev) | |
210 | { | |
211 | struct ap_message ap_msg; | |
212 | unsigned long long psmid; | |
213 | struct { | |
214 | struct type86_hdr hdr; | |
215 | struct type86_fmt2_ext fmt2; | |
216 | struct CPRBX cprbx; | |
217 | } __attribute__((packed)) *reply; | |
34a15167 IT |
218 | struct { |
219 | struct type6_hdr hdr; | |
220 | struct CPRBX cprbx; | |
221 | char function_code[2]; | |
222 | short int rule_length; | |
223 | char rule[8]; | |
224 | short int verb_length; | |
225 | short int key_length; | |
226 | } __packed * msg; | |
2f7c8bd6 RW |
227 | int rc, i; |
228 | ||
468ffddf | 229 | ap_init_message(&ap_msg); |
2f7c8bd6 RW |
230 | ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL); |
231 | if (!ap_msg.message) | |
232 | return -ENOMEM; | |
233 | ||
34a15167 IT |
234 | rng_type6CPRB_msgX(&ap_msg, 4); |
235 | ||
236 | msg = ap_msg.message; | |
237 | msg->cprbx.domain = AP_QID_QUEUE(ap_dev->qid); | |
238 | ||
2f7c8bd6 RW |
239 | rc = ap_send(ap_dev->qid, 0x0102030405060708ULL, ap_msg.message, |
240 | ap_msg.length); | |
241 | if (rc) | |
242 | goto out_free; | |
243 | ||
244 | /* Wait for the test message to complete. */ | |
245 | for (i = 0; i < 2 * HZ; i++) { | |
246 | msleep(1000 / HZ); | |
247 | rc = ap_recv(ap_dev->qid, &psmid, ap_msg.message, 4096); | |
248 | if (rc == 0 && psmid == 0x0102030405060708ULL) | |
249 | break; | |
250 | } | |
251 | ||
252 | if (i >= 2 * HZ) { | |
253 | /* Got no answer. */ | |
254 | rc = -ENODEV; | |
255 | goto out_free; | |
256 | } | |
257 | ||
258 | reply = ap_msg.message; | |
259 | if (reply->cprbx.ccp_rtcode == 0 && reply->cprbx.ccp_rscode == 0) | |
260 | rc = 1; | |
261 | else | |
262 | rc = 0; | |
263 | out_free: | |
264 | free_page((unsigned long) ap_msg.message); | |
265 | return rc; | |
266 | } | |
267 | ||
6684af1a MS |
268 | /** |
269 | * Probe function for PCIXCC/CEX2C cards. It always accepts the AP device | |
270 | * since the bus_match already checked the hardware type. The PCIXCC | |
271 | * cards come in two flavours: micro code level 2 and micro code level 3. | |
272 | * This is checked by sending a test message to the device. | |
273 | * @ap_dev: pointer to the AP device. | |
274 | */ | |
275 | static int zcrypt_pcixcc_probe(struct ap_device *ap_dev) | |
276 | { | |
277 | struct zcrypt_device *zdev; | |
34a15167 IT |
278 | /* |
279 | * Normalized speed ratings per crypto adapter | |
280 | * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY | |
281 | */ | |
282 | int PCIXCC_MCL2_SPEED_IDX[] = {10, 10, 10, 10, 10, 10, 10, 10}; | |
283 | int PCIXCC_MCL3_SPEED_IDX[] = { 8, 8, 8, 8, 8, 8, 8, 8}; | |
284 | int CEX2C_SPEED_IDX[] = {1000, 1400, 2400, 1100, 1500, 2600, 100, 12}; | |
285 | int CEX3C_SPEED_IDX[] = { 500, 700, 1400, 550, 800, 1500, 80, 10}; | |
8e89b6be | 286 | int rc = 0; |
6684af1a | 287 | |
7fe6f097 | 288 | zdev = zcrypt_device_alloc(PCIXCC_MAX_XCRB_MESSAGE_SIZE); |
6684af1a MS |
289 | if (!zdev) |
290 | return -ENOMEM; | |
291 | zdev->ap_dev = ap_dev; | |
6684af1a | 292 | zdev->online = 1; |
8e89b6be FB |
293 | switch (ap_dev->device_type) { |
294 | case AP_DEVICE_TYPE_PCIXCC: | |
6684af1a MS |
295 | rc = zcrypt_pcixcc_mcl(ap_dev); |
296 | if (rc < 0) { | |
297 | zcrypt_device_free(zdev); | |
298 | return rc; | |
299 | } | |
300 | zdev->user_space_type = rc; | |
301 | if (rc == ZCRYPT_PCIXCC_MCL2) { | |
302 | zdev->type_string = "PCIXCC_MCL2"; | |
34a15167 IT |
303 | memcpy(zdev->speed_rating, PCIXCC_MCL2_SPEED_IDX, |
304 | sizeof(PCIXCC_MCL2_SPEED_IDX)); | |
6684af1a MS |
305 | zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE_OLD; |
306 | zdev->max_mod_size = PCIXCC_MAX_MOD_SIZE; | |
c2567f8f | 307 | zdev->max_exp_bit_length = PCIXCC_MAX_MOD_SIZE; |
6684af1a MS |
308 | } else { |
309 | zdev->type_string = "PCIXCC_MCL3"; | |
34a15167 IT |
310 | memcpy(zdev->speed_rating, PCIXCC_MCL3_SPEED_IDX, |
311 | sizeof(PCIXCC_MCL3_SPEED_IDX)); | |
6684af1a MS |
312 | zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE; |
313 | zdev->max_mod_size = PCIXCC_MAX_MOD_SIZE; | |
c2567f8f | 314 | zdev->max_exp_bit_length = PCIXCC_MAX_MOD_SIZE; |
6684af1a | 315 | } |
8e89b6be FB |
316 | break; |
317 | case AP_DEVICE_TYPE_CEX2C: | |
6684af1a MS |
318 | zdev->user_space_type = ZCRYPT_CEX2C; |
319 | zdev->type_string = "CEX2C"; | |
34a15167 IT |
320 | memcpy(zdev->speed_rating, CEX2C_SPEED_IDX, |
321 | sizeof(CEX2C_SPEED_IDX)); | |
6684af1a MS |
322 | zdev->min_mod_size = PCIXCC_MIN_MOD_SIZE; |
323 | zdev->max_mod_size = PCIXCC_MAX_MOD_SIZE; | |
c2567f8f | 324 | zdev->max_exp_bit_length = PCIXCC_MAX_MOD_SIZE; |
8e89b6be FB |
325 | break; |
326 | case AP_DEVICE_TYPE_CEX3C: | |
327 | zdev->user_space_type = ZCRYPT_CEX3C; | |
328 | zdev->type_string = "CEX3C"; | |
34a15167 IT |
329 | memcpy(zdev->speed_rating, CEX3C_SPEED_IDX, |
330 | sizeof(CEX3C_SPEED_IDX)); | |
8e89b6be FB |
331 | zdev->min_mod_size = CEX3C_MIN_MOD_SIZE; |
332 | zdev->max_mod_size = CEX3C_MAX_MOD_SIZE; | |
c2567f8f | 333 | zdev->max_exp_bit_length = CEX3C_MAX_MOD_SIZE; |
8e89b6be FB |
334 | break; |
335 | default: | |
336 | goto out_free; | |
6684af1a | 337 | } |
34a15167 | 338 | zdev->load = zdev->speed_rating[0]; |
8e89b6be | 339 | |
2f7c8bd6 RW |
340 | rc = zcrypt_pcixcc_rng_supported(ap_dev); |
341 | if (rc < 0) { | |
342 | zcrypt_device_free(zdev); | |
343 | return rc; | |
344 | } | |
345 | if (rc) | |
236fb2ab MS |
346 | zdev->ops = zcrypt_msgtype(MSGTYPE06_NAME, |
347 | MSGTYPE06_VARIANT_DEFAULT); | |
2f7c8bd6 | 348 | else |
236fb2ab MS |
349 | zdev->ops = zcrypt_msgtype(MSGTYPE06_NAME, |
350 | MSGTYPE06_VARIANT_NORNG); | |
d6d86c57 | 351 | ap_device_init_reply(ap_dev, &zdev->reply); |
6684af1a MS |
352 | ap_dev->private = zdev; |
353 | rc = zcrypt_device_register(zdev); | |
354 | if (rc) | |
355 | goto out_free; | |
356 | return 0; | |
357 | ||
358 | out_free: | |
359 | ap_dev->private = NULL; | |
360 | zcrypt_device_free(zdev); | |
361 | return rc; | |
362 | } | |
363 | ||
364 | /** | |
365 | * This is called to remove the extended PCIXCC/CEX2C driver information | |
366 | * if an AP device is removed. | |
367 | */ | |
368 | static void zcrypt_pcixcc_remove(struct ap_device *ap_dev) | |
369 | { | |
370 | struct zcrypt_device *zdev = ap_dev->private; | |
371 | ||
372 | zcrypt_device_unregister(zdev); | |
373 | } | |
374 | ||
375 | int __init zcrypt_pcixcc_init(void) | |
376 | { | |
377 | return ap_driver_register(&zcrypt_pcixcc_driver, THIS_MODULE, "pcixcc"); | |
378 | } | |
379 | ||
380 | void zcrypt_pcixcc_exit(void) | |
381 | { | |
382 | ap_driver_unregister(&zcrypt_pcixcc_driver); | |
383 | } | |
384 | ||
6684af1a MS |
385 | module_init(zcrypt_pcixcc_init); |
386 | module_exit(zcrypt_pcixcc_exit); |