1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Copyright IBM Corp. 2012
4  *  Author(s): Holger Dengler <hd@linux.vnet.ibm.com>
5  */
6 
7 #include <linux/module.h>
8 #include <linux/slab.h>
9 #include <linux/init.h>
10 #include <linux/err.h>
11 #include <linux/atomic.h>
12 #include <linux/uaccess.h>
13 #include <linux/mod_devicetable.h>
14 
15 #include "ap_bus.h"
16 #include "zcrypt_api.h"
17 #include "zcrypt_msgtype6.h"
18 #include "zcrypt_msgtype50.h"
19 #include "zcrypt_error.h"
20 #include "zcrypt_cex4.h"
21 #include "zcrypt_ccamisc.h"
22 
23 #define CEX4A_MIN_MOD_SIZE	  1	/*    8 bits	*/
24 #define CEX4A_MAX_MOD_SIZE_2K	256	/* 2048 bits	*/
25 #define CEX4A_MAX_MOD_SIZE_4K	512	/* 4096 bits	*/
26 
27 #define CEX4C_MIN_MOD_SIZE	 16	/*  256 bits	*/
28 #define CEX4C_MAX_MOD_SIZE	512	/* 4096 bits	*/
29 
30 #define CEX4A_MAX_MESSAGE_SIZE	MSGTYPE50_CRB3_MAX_MSG_SIZE
31 #define CEX4C_MAX_MESSAGE_SIZE	MSGTYPE06_MAX_MSG_SIZE
32 
33 /* Waiting time for requests to be processed.
34  * Currently there are some types of request which are not deterministic.
35  * But the maximum time limit managed by the stomper code is set to 60sec.
36  * Hence we have to wait at least that time period.
37  */
38 #define CEX4_CLEANUP_TIME	(900*HZ)
39 
40 MODULE_AUTHOR("IBM Corporation");
41 MODULE_DESCRIPTION("CEX4/CEX5/CEX6 Cryptographic Card device driver, " \
42 		   "Copyright IBM Corp. 2018");
43 MODULE_LICENSE("GPL");
44 
45 static struct ap_device_id zcrypt_cex4_card_ids[] = {
46 	{ .dev_type = AP_DEVICE_TYPE_CEX4,
47 	  .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
48 	{ .dev_type = AP_DEVICE_TYPE_CEX5,
49 	  .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
50 	{ .dev_type = AP_DEVICE_TYPE_CEX6,
51 	  .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
52 	{ /* end of list */ },
53 };
54 
55 MODULE_DEVICE_TABLE(ap, zcrypt_cex4_card_ids);
56 
57 static struct ap_device_id zcrypt_cex4_queue_ids[] = {
58 	{ .dev_type = AP_DEVICE_TYPE_CEX4,
59 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
60 	{ .dev_type = AP_DEVICE_TYPE_CEX5,
61 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
62 	{ .dev_type = AP_DEVICE_TYPE_CEX6,
63 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
64 	{ /* end of list */ },
65 };
66 
67 MODULE_DEVICE_TABLE(ap, zcrypt_cex4_queue_ids);
68 
69 /*
70  * CCA card addditional device attributes
71  */
72 static ssize_t serialnr_show(struct device *dev,
73 			     struct device_attribute *attr,
74 			     char *buf)
75 {
76 	struct cca_info ci;
77 	struct ap_card *ac = to_ap_card(dev);
78 	struct zcrypt_card *zc = ac->private;
79 
80 	memset(&ci, 0, sizeof(ci));
81 
82 	if (ap_domain_index >= 0)
83 		cca_get_info(ac->id, ap_domain_index, &ci, zc->online);
84 
85 	return snprintf(buf, PAGE_SIZE, "%s\n", ci.serial);
86 }
87 static DEVICE_ATTR_RO(serialnr);
88 
89 static struct attribute *cca_card_attrs[] = {
90 	&dev_attr_serialnr.attr,
91 	NULL,
92 };
93 
94 static const struct attribute_group cca_card_attr_group = {
95 	.attrs = cca_card_attrs,
96 };
97 
98 /*
99  * CCA queue addditional device attributes
100  */
101 static ssize_t mkvps_show(struct device *dev,
102 			  struct device_attribute *attr,
103 			  char *buf)
104 {
105 	int n = 0;
106 	struct cca_info ci;
107 	struct zcrypt_queue *zq = to_ap_queue(dev)->private;
108 	static const char * const cao_state[] = { "invalid", "valid" };
109 	static const char * const new_state[] = { "empty", "partial", "full" };
110 
111 	memset(&ci, 0, sizeof(ci));
112 
113 	cca_get_info(AP_QID_CARD(zq->queue->qid),
114 		     AP_QID_QUEUE(zq->queue->qid),
115 		     &ci, zq->online);
116 
117 	if (ci.new_mk_state >= '1' && ci.new_mk_state <= '3')
118 		n = snprintf(buf, PAGE_SIZE, "AES NEW: %s 0x%016llx\n",
119 			     new_state[ci.new_mk_state - '1'], ci.new_mkvp);
120 	else
121 		n = snprintf(buf, PAGE_SIZE, "AES NEW: - -\n");
122 
123 	if (ci.cur_mk_state >= '1' && ci.cur_mk_state <= '2')
124 		n += snprintf(buf + n, PAGE_SIZE - n, "AES CUR: %s 0x%016llx\n",
125 			      cao_state[ci.cur_mk_state - '1'], ci.cur_mkvp);
126 	else
127 		n += snprintf(buf + n, PAGE_SIZE - n, "AES CUR: - -\n");
128 
129 	if (ci.old_mk_state >= '1' && ci.old_mk_state <= '2')
130 		n += snprintf(buf + n, PAGE_SIZE - n, "AES OLD: %s 0x%016llx\n",
131 			      cao_state[ci.old_mk_state - '1'], ci.old_mkvp);
132 	else
133 		n += snprintf(buf + n, PAGE_SIZE - n, "AES OLD: - -\n");
134 
135 	return n;
136 }
137 static DEVICE_ATTR_RO(mkvps);
138 
139 static struct attribute *cca_queue_attrs[] = {
140 	&dev_attr_mkvps.attr,
141 	NULL,
142 };
143 
144 static const struct attribute_group cca_queue_attr_group = {
145 	.attrs = cca_queue_attrs,
146 };
147 
148 /**
149  * Probe function for CEX4/CEX5/CEX6 card device. It always
150  * accepts the AP device since the bus_match already checked
151  * the hardware type.
152  * @ap_dev: pointer to the AP device.
153  */
154 static int zcrypt_cex4_card_probe(struct ap_device *ap_dev)
155 {
156 	/*
157 	 * Normalized speed ratings per crypto adapter
158 	 * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
159 	 */
160 	static const int CEX4A_SPEED_IDX[] = {
161 		 14, 19, 249, 42, 228, 1458, 0, 0};
162 	static const int CEX5A_SPEED_IDX[] = {
163 		  8,  9,  20, 18,  66,	458, 0, 0};
164 	static const int CEX6A_SPEED_IDX[] = {
165 		  6,  9,  20, 17,  65,	438, 0, 0};
166 
167 	static const int CEX4C_SPEED_IDX[] = {
168 		 59,  69, 308, 83, 278, 2204, 209, 40};
169 	static const int CEX5C_SPEED_IDX[] = {
170 		 24,  31,  50, 37,  90,  479,  27, 10};
171 	static const int CEX6C_SPEED_IDX[] = {
172 		 16,  20,  32, 27,  77,  455,  23,  9};
173 
174 	static const int CEX4P_SPEED_IDX[] = {
175 		224, 313, 3560, 359, 605, 2827, 0, 50};
176 	static const int CEX5P_SPEED_IDX[] = {
177 		 63,  84,  156,  83, 142,  533, 0, 10};
178 	static const int CEX6P_SPEED_IDX[] = {
179 		 55,  70,  121,  73, 129,  522, 0,  9};
180 
181 	struct ap_card *ac = to_ap_card(&ap_dev->device);
182 	struct zcrypt_card *zc;
183 	int rc = 0;
184 
185 	zc = zcrypt_card_alloc();
186 	if (!zc)
187 		return -ENOMEM;
188 	zc->card = ac;
189 	ac->private = zc;
190 	if (ap_test_bit(&ac->functions, AP_FUNC_ACCEL)) {
191 		if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
192 			zc->type_string = "CEX4A";
193 			zc->user_space_type = ZCRYPT_CEX4;
194 			memcpy(zc->speed_rating, CEX4A_SPEED_IDX,
195 			       sizeof(CEX4A_SPEED_IDX));
196 		} else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) {
197 			zc->type_string = "CEX5A";
198 			zc->user_space_type = ZCRYPT_CEX5;
199 			memcpy(zc->speed_rating, CEX5A_SPEED_IDX,
200 			       sizeof(CEX5A_SPEED_IDX));
201 		} else {
202 			zc->type_string = "CEX6A";
203 			zc->user_space_type = ZCRYPT_CEX6;
204 			memcpy(zc->speed_rating, CEX6A_SPEED_IDX,
205 			       sizeof(CEX6A_SPEED_IDX));
206 		}
207 		zc->min_mod_size = CEX4A_MIN_MOD_SIZE;
208 		if (ap_test_bit(&ac->functions, AP_FUNC_MEX4K) &&
209 		    ap_test_bit(&ac->functions, AP_FUNC_CRT4K)) {
210 			zc->max_mod_size = CEX4A_MAX_MOD_SIZE_4K;
211 			zc->max_exp_bit_length =
212 				CEX4A_MAX_MOD_SIZE_4K;
213 		} else {
214 			zc->max_mod_size = CEX4A_MAX_MOD_SIZE_2K;
215 			zc->max_exp_bit_length =
216 				CEX4A_MAX_MOD_SIZE_2K;
217 		}
218 	} else if (ap_test_bit(&ac->functions, AP_FUNC_COPRO)) {
219 		if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
220 			zc->type_string = "CEX4C";
221 			/* wrong user space type, must be CEX4
222 			 * just keep it for cca compatibility
223 			 */
224 			zc->user_space_type = ZCRYPT_CEX3C;
225 			memcpy(zc->speed_rating, CEX4C_SPEED_IDX,
226 			       sizeof(CEX4C_SPEED_IDX));
227 		} else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) {
228 			zc->type_string = "CEX5C";
229 			/* wrong user space type, must be CEX5
230 			 * just keep it for cca compatibility
231 			 */
232 			zc->user_space_type = ZCRYPT_CEX3C;
233 			memcpy(zc->speed_rating, CEX5C_SPEED_IDX,
234 			       sizeof(CEX5C_SPEED_IDX));
235 		} else {
236 			zc->type_string = "CEX6C";
237 			/* wrong user space type, must be CEX6
238 			 * just keep it for cca compatibility
239 			 */
240 			zc->user_space_type = ZCRYPT_CEX3C;
241 			memcpy(zc->speed_rating, CEX6C_SPEED_IDX,
242 			       sizeof(CEX6C_SPEED_IDX));
243 		}
244 		zc->min_mod_size = CEX4C_MIN_MOD_SIZE;
245 		zc->max_mod_size = CEX4C_MAX_MOD_SIZE;
246 		zc->max_exp_bit_length = CEX4C_MAX_MOD_SIZE;
247 	} else if (ap_test_bit(&ac->functions, AP_FUNC_EP11)) {
248 		if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
249 			zc->type_string = "CEX4P";
250 			zc->user_space_type = ZCRYPT_CEX4;
251 			memcpy(zc->speed_rating, CEX4P_SPEED_IDX,
252 			       sizeof(CEX4P_SPEED_IDX));
253 		} else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) {
254 			zc->type_string = "CEX5P";
255 			zc->user_space_type = ZCRYPT_CEX5;
256 			memcpy(zc->speed_rating, CEX5P_SPEED_IDX,
257 			       sizeof(CEX5P_SPEED_IDX));
258 		} else {
259 			zc->type_string = "CEX6P";
260 			zc->user_space_type = ZCRYPT_CEX6;
261 			memcpy(zc->speed_rating, CEX6P_SPEED_IDX,
262 			       sizeof(CEX6P_SPEED_IDX));
263 		}
264 		zc->min_mod_size = CEX4C_MIN_MOD_SIZE;
265 		zc->max_mod_size = CEX4C_MAX_MOD_SIZE;
266 		zc->max_exp_bit_length = CEX4C_MAX_MOD_SIZE;
267 	} else {
268 		zcrypt_card_free(zc);
269 		return -ENODEV;
270 	}
271 	zc->online = 1;
272 
273 	rc = zcrypt_card_register(zc);
274 	if (rc) {
275 		ac->private = NULL;
276 		zcrypt_card_free(zc);
277 		goto out;
278 	}
279 
280 	if (ap_test_bit(&ac->functions, AP_FUNC_COPRO)) {
281 		rc = sysfs_create_group(&ap_dev->device.kobj,
282 					&cca_card_attr_group);
283 		if (rc)
284 			zcrypt_card_unregister(zc);
285 	}
286 
287 out:
288 	return rc;
289 }
290 
291 /**
292  * This is called to remove the CEX4/CEX5/CEX6 card driver information
293  * if an AP card device is removed.
294  */
295 static void zcrypt_cex4_card_remove(struct ap_device *ap_dev)
296 {
297 	struct ap_card *ac = to_ap_card(&ap_dev->device);
298 	struct zcrypt_card *zc = ac->private;
299 
300 	if (ap_test_bit(&ac->functions, AP_FUNC_COPRO))
301 		sysfs_remove_group(&ap_dev->device.kobj, &cca_card_attr_group);
302 	if (zc)
303 		zcrypt_card_unregister(zc);
304 }
305 
306 static struct ap_driver zcrypt_cex4_card_driver = {
307 	.probe = zcrypt_cex4_card_probe,
308 	.remove = zcrypt_cex4_card_remove,
309 	.ids = zcrypt_cex4_card_ids,
310 	.flags = AP_DRIVER_FLAG_DEFAULT,
311 };
312 
313 /**
314  * Probe function for CEX4/CEX5/CEX6 queue device. It always
315  * accepts the AP device since the bus_match already checked
316  * the hardware type.
317  * @ap_dev: pointer to the AP device.
318  */
319 static int zcrypt_cex4_queue_probe(struct ap_device *ap_dev)
320 {
321 	struct ap_queue *aq = to_ap_queue(&ap_dev->device);
322 	struct zcrypt_queue *zq;
323 	int rc;
324 
325 	if (ap_test_bit(&aq->card->functions, AP_FUNC_ACCEL)) {
326 		zq = zcrypt_queue_alloc(CEX4A_MAX_MESSAGE_SIZE);
327 		if (!zq)
328 			return -ENOMEM;
329 		zq->ops = zcrypt_msgtype(MSGTYPE50_NAME,
330 					 MSGTYPE50_VARIANT_DEFAULT);
331 	} else if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO)) {
332 		zq = zcrypt_queue_alloc(CEX4C_MAX_MESSAGE_SIZE);
333 		if (!zq)
334 			return -ENOMEM;
335 		zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
336 					 MSGTYPE06_VARIANT_DEFAULT);
337 	} else if (ap_test_bit(&aq->card->functions, AP_FUNC_EP11)) {
338 		zq = zcrypt_queue_alloc(CEX4C_MAX_MESSAGE_SIZE);
339 		if (!zq)
340 			return -ENOMEM;
341 		zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
342 					 MSGTYPE06_VARIANT_EP11);
343 	} else {
344 		return -ENODEV;
345 	}
346 
347 	zq->queue = aq;
348 	zq->online = 1;
349 	atomic_set(&zq->load, 0);
350 	ap_queue_init_reply(aq, &zq->reply);
351 	aq->request_timeout = CEX4_CLEANUP_TIME,
352 	aq->private = zq;
353 	rc = zcrypt_queue_register(zq);
354 	if (rc) {
355 		aq->private = NULL;
356 		zcrypt_queue_free(zq);
357 		goto out;
358 	}
359 
360 	if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO)) {
361 		rc = sysfs_create_group(&ap_dev->device.kobj,
362 					&cca_queue_attr_group);
363 		if (rc)
364 			zcrypt_queue_unregister(zq);
365 	}
366 
367 out:
368 	return rc;
369 }
370 
371 /**
372  * This is called to remove the CEX4/CEX5/CEX6 queue driver
373  * information if an AP queue device is removed.
374  */
375 static void zcrypt_cex4_queue_remove(struct ap_device *ap_dev)
376 {
377 	struct ap_queue *aq = to_ap_queue(&ap_dev->device);
378 	struct zcrypt_queue *zq = aq->private;
379 
380 	if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO))
381 		sysfs_remove_group(&ap_dev->device.kobj, &cca_queue_attr_group);
382 	if (zq)
383 		zcrypt_queue_unregister(zq);
384 }
385 
386 static struct ap_driver zcrypt_cex4_queue_driver = {
387 	.probe = zcrypt_cex4_queue_probe,
388 	.remove = zcrypt_cex4_queue_remove,
389 	.suspend = ap_queue_suspend,
390 	.resume = ap_queue_resume,
391 	.ids = zcrypt_cex4_queue_ids,
392 	.flags = AP_DRIVER_FLAG_DEFAULT,
393 };
394 
395 int __init zcrypt_cex4_init(void)
396 {
397 	int rc;
398 
399 	rc = ap_driver_register(&zcrypt_cex4_card_driver,
400 				THIS_MODULE, "cex4card");
401 	if (rc)
402 		return rc;
403 
404 	rc = ap_driver_register(&zcrypt_cex4_queue_driver,
405 				THIS_MODULE, "cex4queue");
406 	if (rc)
407 		ap_driver_unregister(&zcrypt_cex4_card_driver);
408 
409 	return rc;
410 }
411 
412 void __exit zcrypt_cex4_exit(void)
413 {
414 	ap_driver_unregister(&zcrypt_cex4_queue_driver);
415 	ap_driver_unregister(&zcrypt_cex4_card_driver);
416 }
417 
418 module_init(zcrypt_cex4_init);
419 module_exit(zcrypt_cex4_exit);
420