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