xref: /linux/drivers/s390/crypto/zcrypt_cex4.c (revision 2a4c0c11c0193889446cdb6f1540cc2b9aff97dd)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Copyright IBM Corp. 2012, 2022
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/hex.h>
10 #include <linux/init.h>
11 #include <linux/err.h>
12 #include <linux/atomic.h>
13 #include <linux/uaccess.h>
14 #include <linux/mod_devicetable.h>
15 
16 #include "ap_bus.h"
17 #include "zcrypt_api.h"
18 #include "zcrypt_msgtype6.h"
19 #include "zcrypt_msgtype50.h"
20 #include "zcrypt_error.h"
21 #include "zcrypt_cex4.h"
22 #include "zcrypt_ccamisc.h"
23 #include "zcrypt_ep11misc.h"
24 
25 #define CEX4A_MIN_MOD_SIZE	  1	/*    8 bits	*/
26 #define CEX4A_MAX_MOD_SIZE_2K	256	/* 2048 bits	*/
27 #define CEX4A_MAX_MOD_SIZE_4K	512	/* 4096 bits	*/
28 
29 #define CEX4C_MIN_MOD_SIZE	 16	/*  256 bits	*/
30 #define CEX4C_MAX_MOD_SIZE	512	/* 4096 bits	*/
31 
32 /* Waiting time for requests to be processed.
33  * Currently there are some types of request which are not deterministic.
34  * But the maximum time limit managed by the stomper code is set to 60sec.
35  * Hence we have to wait at least that time period.
36  */
37 #define CEX4_CLEANUP_TIME	(900 * HZ)
38 
39 MODULE_AUTHOR("IBM Corporation");
40 MODULE_DESCRIPTION("CEX[45678] Cryptographic Card device driver, " \
41 		   "Copyright IBM Corp. 2022");
42 MODULE_LICENSE("GPL");
43 
44 static struct ap_device_id zcrypt_cex4_card_ids[] = {
45 	{ .dev_type = AP_DEVICE_TYPE_CEX4,
46 	  .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
47 	{ .dev_type = AP_DEVICE_TYPE_CEX5,
48 	  .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
49 	{ .dev_type = AP_DEVICE_TYPE_CEX6,
50 	  .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
51 	{ .dev_type = AP_DEVICE_TYPE_CEX7,
52 	  .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
53 	{ .dev_type = AP_DEVICE_TYPE_CEX8,
54 	  .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
55 	{ /* end of list */ },
56 };
57 
58 MODULE_DEVICE_TABLE(ap, zcrypt_cex4_card_ids);
59 
60 static struct ap_device_id zcrypt_cex4_queue_ids[] = {
61 	{ .dev_type = AP_DEVICE_TYPE_CEX4,
62 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
63 	{ .dev_type = AP_DEVICE_TYPE_CEX5,
64 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
65 	{ .dev_type = AP_DEVICE_TYPE_CEX6,
66 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
67 	{ .dev_type = AP_DEVICE_TYPE_CEX7,
68 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
69 	{ .dev_type = AP_DEVICE_TYPE_CEX8,
70 	  .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
71 	{ /* end of list */ },
72 };
73 
74 MODULE_DEVICE_TABLE(ap, zcrypt_cex4_queue_ids);
75 
76 /*
77  * CCA card additional device attributes
78  */
cca_serialnr_show(struct device * dev,struct device_attribute * attr,char * buf)79 static ssize_t cca_serialnr_show(struct device *dev,
80 				 struct device_attribute *attr,
81 				 char *buf)
82 {
83 	struct ap_card *ac = to_ap_card(dev);
84 	struct cca_info ci;
85 
86 	memset(&ci, 0, sizeof(ci));
87 
88 	cca_get_info(ac->id, AUTOSEL_DOM, &ci, 0);
89 
90 	return sysfs_emit(buf, "%s\n", ci.serial);
91 }
92 
93 static struct device_attribute dev_attr_cca_serialnr =
94 	__ATTR(serialnr, 0444, cca_serialnr_show, NULL);
95 
96 static struct attribute *cca_card_attrs[] = {
97 	&dev_attr_cca_serialnr.attr,
98 	NULL,
99 };
100 
101 static const struct attribute_group cca_card_attr_grp = {
102 	.attrs = cca_card_attrs,
103 };
104 
105 /*
106  * Simple helper macro to format raw mkvp byte array into hex
107  */
108 #define MKVP_TO_HEXBUF(mkvp, buf) \
109 	do { \
110 		BUILD_BUG_ON(sizeof(buf) <= 2 * sizeof(mkvp)); \
111 		bin2hex(buf, mkvp, sizeof(mkvp)); \
112 		buf[2 * sizeof(mkvp)] = '\0'; \
113 	} while (0)
114 
115 /*
116  * CCA queue additional device attributes
117  */
cca_mkvps_show(struct device * dev,struct device_attribute * attr,char * buf)118 static ssize_t cca_mkvps_show(struct device *dev,
119 			      struct device_attribute *attr,
120 			      char *buf)
121 {
122 	static const char * const new_state[] = { "empty", "partial", "full" };
123 	static const char * const cao_state[] = { "invalid", "valid" };
124 	struct zcrypt_queue *zq = dev_get_drvdata(dev);
125 	struct cca_info ci;
126 	char hexbuf[2 * 16 + 1];
127 	int n = 0;
128 
129 	memset(&ci, 0, sizeof(ci));
130 
131 	cca_get_info(AP_QID_CARD(zq->queue->qid),
132 		     AP_QID_QUEUE(zq->queue->qid),
133 		     &ci, 0);
134 
135 	if (ci.new_aes_mk_state >= '1' && ci.new_aes_mk_state <= '3') {
136 		MKVP_TO_HEXBUF(ci.new_aes_mkvp, hexbuf);
137 		n += sysfs_emit_at(buf, n, "AES NEW: %s 0x%s\n",
138 				   new_state[ci.new_aes_mk_state - '1'],
139 				   hexbuf);
140 	} else {
141 		n += sysfs_emit_at(buf, n, "AES NEW: - -\n");
142 	}
143 
144 	if (ci.cur_aes_mk_state >= '1' && ci.cur_aes_mk_state <= '2') {
145 		MKVP_TO_HEXBUF(ci.cur_aes_mkvp, hexbuf);
146 		n += sysfs_emit_at(buf, n, "AES CUR: %s 0x%s\n",
147 				   cao_state[ci.cur_aes_mk_state - '1'],
148 				   hexbuf);
149 	} else {
150 		n += sysfs_emit_at(buf, n, "AES CUR: - -\n");
151 	}
152 
153 	if (ci.old_aes_mk_state >= '1' && ci.old_aes_mk_state <= '2') {
154 		MKVP_TO_HEXBUF(ci.old_aes_mkvp, hexbuf);
155 		n += sysfs_emit_at(buf, n, "AES OLD: %s 0x%s\n",
156 				   cao_state[ci.old_aes_mk_state - '1'],
157 				   hexbuf);
158 	} else {
159 		n += sysfs_emit_at(buf, n, "AES OLD: - -\n");
160 	}
161 
162 	if (ci.new_apka_mk_state >= '1' && ci.new_apka_mk_state <= '3') {
163 		MKVP_TO_HEXBUF(ci.new_apka_mkvp, hexbuf);
164 		n += sysfs_emit_at(buf, n, "APKA NEW: %s 0x%s\n",
165 				   new_state[ci.new_apka_mk_state - '1'],
166 				   hexbuf);
167 	} else {
168 		n += sysfs_emit_at(buf, n, "APKA NEW: - -\n");
169 	}
170 
171 	if (ci.cur_apka_mk_state >= '1' && ci.cur_apka_mk_state <= '2') {
172 		MKVP_TO_HEXBUF(ci.cur_apka_mkvp, hexbuf);
173 		n += sysfs_emit_at(buf, n, "APKA CUR: %s 0x%s\n",
174 				   cao_state[ci.cur_apka_mk_state - '1'],
175 				   hexbuf);
176 	} else {
177 		n += sysfs_emit_at(buf, n, "APKA CUR: - -\n");
178 	}
179 
180 	if (ci.old_apka_mk_state >= '1' && ci.old_apka_mk_state <= '2') {
181 		MKVP_TO_HEXBUF(ci.old_apka_mkvp, hexbuf);
182 		n += sysfs_emit_at(buf, n, "APKA OLD: %s 0x%s\n",
183 				   cao_state[ci.old_apka_mk_state - '1'],
184 				   hexbuf);
185 	} else {
186 		n += sysfs_emit_at(buf, n, "APKA OLD: - -\n");
187 	}
188 
189 	if (ci.new_asym_mk_state >= '1' && ci.new_asym_mk_state <= '3') {
190 		MKVP_TO_HEXBUF(ci.new_asym_mkvp, hexbuf);
191 		n += sysfs_emit_at(buf, n, "ASYM NEW: %s 0x%s\n",
192 				   new_state[ci.new_asym_mk_state - '1'],
193 				   hexbuf);
194 	} else {
195 		n += sysfs_emit_at(buf, n, "ASYM NEW: - -\n");
196 	}
197 
198 	if (ci.cur_asym_mk_state >= '1' && ci.cur_asym_mk_state <= '2') {
199 		MKVP_TO_HEXBUF(ci.cur_asym_mkvp, hexbuf);
200 		n += sysfs_emit_at(buf, n, "ASYM CUR: %s 0x%s\n",
201 				   cao_state[ci.cur_asym_mk_state - '1'],
202 				   hexbuf);
203 	} else {
204 		n += sysfs_emit_at(buf, n, "ASYM CUR: - -\n");
205 	}
206 
207 	if (ci.old_asym_mk_state >= '1' && ci.old_asym_mk_state <= '2') {
208 		MKVP_TO_HEXBUF(ci.old_asym_mkvp, hexbuf);
209 		n += sysfs_emit_at(buf, n, "ASYM OLD: %s 0x%s\n",
210 				   cao_state[ci.old_asym_mk_state - '1'],
211 				   hexbuf);
212 	} else {
213 		n += sysfs_emit_at(buf, n, "ASYM OLD: - -\n");
214 	}
215 
216 	return n;
217 }
218 
219 static struct device_attribute dev_attr_cca_mkvps =
220 	__ATTR(mkvps, 0444, cca_mkvps_show, NULL);
221 
222 static struct attribute *cca_queue_attrs[] = {
223 	&dev_attr_cca_mkvps.attr,
224 	NULL,
225 };
226 
227 static const struct attribute_group cca_queue_attr_grp = {
228 	.attrs = cca_queue_attrs,
229 };
230 
231 /*
232  * EP11 card additional device attributes
233  */
ep11_api_ordinalnr_show(struct device * dev,struct device_attribute * attr,char * buf)234 static ssize_t ep11_api_ordinalnr_show(struct device *dev,
235 				       struct device_attribute *attr,
236 				       char *buf)
237 {
238 	struct ap_card *ac = to_ap_card(dev);
239 	struct ep11_card_info ci;
240 
241 	memset(&ci, 0, sizeof(ci));
242 
243 	ep11_get_card_info(ac->id, &ci, 0);
244 
245 	if (ci.API_ord_nr > 0)
246 		return sysfs_emit(buf, "%u\n", ci.API_ord_nr);
247 	else
248 		return sysfs_emit(buf, "\n");
249 }
250 
251 static struct device_attribute dev_attr_ep11_api_ordinalnr =
252 	__ATTR(API_ordinalnr, 0444, ep11_api_ordinalnr_show, NULL);
253 
ep11_fw_version_show(struct device * dev,struct device_attribute * attr,char * buf)254 static ssize_t ep11_fw_version_show(struct device *dev,
255 				    struct device_attribute *attr,
256 				    char *buf)
257 {
258 	struct ap_card *ac = to_ap_card(dev);
259 	struct ep11_card_info ci;
260 
261 	memset(&ci, 0, sizeof(ci));
262 
263 	ep11_get_card_info(ac->id, &ci, 0);
264 
265 	if (ci.FW_version > 0)
266 		return sysfs_emit(buf, "%d.%d\n",
267 				  (int)(ci.FW_version >> 8),
268 				  (int)(ci.FW_version & 0xFF));
269 	else
270 		return sysfs_emit(buf, "\n");
271 }
272 
273 static struct device_attribute dev_attr_ep11_fw_version =
274 	__ATTR(FW_version, 0444, ep11_fw_version_show, NULL);
275 
ep11_serialnr_show(struct device * dev,struct device_attribute * attr,char * buf)276 static ssize_t ep11_serialnr_show(struct device *dev,
277 				  struct device_attribute *attr,
278 				  char *buf)
279 {
280 	struct ap_card *ac = to_ap_card(dev);
281 	struct ep11_card_info ci;
282 
283 	memset(&ci, 0, sizeof(ci));
284 
285 	ep11_get_card_info(ac->id, &ci, 0);
286 
287 	if (ci.serial[0])
288 		return sysfs_emit(buf, "%16.16s\n", ci.serial);
289 	else
290 		return sysfs_emit(buf, "\n");
291 }
292 
293 static struct device_attribute dev_attr_ep11_serialnr =
294 	__ATTR(serialnr, 0444, ep11_serialnr_show, NULL);
295 
296 static const struct {
297 	int	    mode_bit;
298 	const char *mode_txt;
299 } ep11_op_modes[] = {
300 	{ 0, "FIPS2009" },
301 	{ 1, "BSI2009" },
302 	{ 2, "FIPS2011" },
303 	{ 3, "BSI2011" },
304 	{ 4, "SIGG-IMPORT" },
305 	{ 5, "SIGG" },
306 	{ 6, "BSICC2017" },
307 	{ 7, "FIPS2021" },
308 	{ 8, "FIPS2024" },
309 	{ 0, NULL }
310 };
311 
ep11_card_op_modes_show(struct device * dev,struct device_attribute * attr,char * buf)312 static ssize_t ep11_card_op_modes_show(struct device *dev,
313 				       struct device_attribute *attr,
314 				       char *buf)
315 {
316 	struct ap_card *ac = to_ap_card(dev);
317 	struct ep11_card_info ci;
318 	int i, n = 0;
319 
320 	memset(&ci, 0, sizeof(ci));
321 
322 	ep11_get_card_info(ac->id, &ci, 0);
323 
324 	for (i = 0; ep11_op_modes[i].mode_txt; i++) {
325 		if (ci.op_mode & (1ULL << ep11_op_modes[i].mode_bit)) {
326 			if (n > 0)
327 				buf[n++] = ' ';
328 			n += sysfs_emit_at(buf, n, "%s",
329 					   ep11_op_modes[i].mode_txt);
330 		}
331 	}
332 	n += sysfs_emit_at(buf, n, "\n");
333 
334 	return n;
335 }
336 
337 static struct device_attribute dev_attr_ep11_card_op_modes =
338 	__ATTR(op_modes, 0444, ep11_card_op_modes_show, NULL);
339 
340 static struct attribute *ep11_card_attrs[] = {
341 	&dev_attr_ep11_api_ordinalnr.attr,
342 	&dev_attr_ep11_fw_version.attr,
343 	&dev_attr_ep11_serialnr.attr,
344 	&dev_attr_ep11_card_op_modes.attr,
345 	NULL,
346 };
347 
348 static const struct attribute_group ep11_card_attr_grp = {
349 	.attrs = ep11_card_attrs,
350 };
351 
352 /*
353  * EP11 queue additional device attributes
354  */
355 
ep11_mkvps_show(struct device * dev,struct device_attribute * attr,char * buf)356 static ssize_t ep11_mkvps_show(struct device *dev,
357 			       struct device_attribute *attr,
358 			       char *buf)
359 {
360 	struct zcrypt_queue *zq = dev_get_drvdata(dev);
361 	int n = 0;
362 	struct ep11_domain_info di;
363 	static const char * const cwk_state[] = { "invalid", "valid" };
364 	static const char * const nwk_state[] = { "empty", "uncommitted",
365 						  "committed" };
366 
367 	memset(&di, 0, sizeof(di));
368 
369 	if (zq->online)
370 		ep11_get_domain_info(AP_QID_CARD(zq->queue->qid),
371 				     AP_QID_QUEUE(zq->queue->qid),
372 				     &di, 0);
373 
374 	if (di.cur_wk_state == '0') {
375 		n = sysfs_emit(buf, "WK CUR: %s -\n",
376 			       cwk_state[di.cur_wk_state - '0']);
377 	} else if (di.cur_wk_state == '1') {
378 		n = sysfs_emit(buf, "WK CUR: %s 0x",
379 			       cwk_state[di.cur_wk_state - '0']);
380 		bin2hex(buf + n, di.cur_wkvp, sizeof(di.cur_wkvp));
381 		n += 2 * sizeof(di.cur_wkvp);
382 		n += sysfs_emit_at(buf, n, "\n");
383 	} else {
384 		n = sysfs_emit(buf, "WK CUR: - -\n");
385 	}
386 
387 	if (di.new_wk_state == '0') {
388 		n += sysfs_emit_at(buf, n, "WK NEW: %s -\n",
389 				   nwk_state[di.new_wk_state - '0']);
390 	} else if (di.new_wk_state >= '1' && di.new_wk_state <= '2') {
391 		n += sysfs_emit_at(buf, n, "WK NEW: %s 0x",
392 				   nwk_state[di.new_wk_state - '0']);
393 		bin2hex(buf + n, di.new_wkvp, sizeof(di.new_wkvp));
394 		n += 2 * sizeof(di.new_wkvp);
395 		n += sysfs_emit_at(buf, n, "\n");
396 	} else {
397 		n += sysfs_emit_at(buf, n, "WK NEW: - -\n");
398 	}
399 
400 	return n;
401 }
402 
403 static struct device_attribute dev_attr_ep11_mkvps =
404 	__ATTR(mkvps, 0444, ep11_mkvps_show, NULL);
405 
ep11_queue_op_modes_show(struct device * dev,struct device_attribute * attr,char * buf)406 static ssize_t ep11_queue_op_modes_show(struct device *dev,
407 					struct device_attribute *attr,
408 					char *buf)
409 {
410 	struct zcrypt_queue *zq = dev_get_drvdata(dev);
411 	int i, n = 0;
412 	struct ep11_domain_info di;
413 
414 	memset(&di, 0, sizeof(di));
415 
416 	if (zq->online)
417 		ep11_get_domain_info(AP_QID_CARD(zq->queue->qid),
418 				     AP_QID_QUEUE(zq->queue->qid),
419 				     &di, 0);
420 
421 	for (i = 0; ep11_op_modes[i].mode_txt; i++) {
422 		if (di.op_mode & (1ULL << ep11_op_modes[i].mode_bit)) {
423 			if (n > 0)
424 				buf[n++] = ' ';
425 			n += sysfs_emit_at(buf, n, "%s",
426 					   ep11_op_modes[i].mode_txt);
427 		}
428 	}
429 	n += sysfs_emit_at(buf, n, "\n");
430 
431 	return n;
432 }
433 
434 static struct device_attribute dev_attr_ep11_queue_op_modes =
435 	__ATTR(op_modes, 0444, ep11_queue_op_modes_show, NULL);
436 
437 static struct attribute *ep11_queue_attrs[] = {
438 	&dev_attr_ep11_mkvps.attr,
439 	&dev_attr_ep11_queue_op_modes.attr,
440 	NULL,
441 };
442 
443 static const struct attribute_group ep11_queue_attr_grp = {
444 	.attrs = ep11_queue_attrs,
445 };
446 
447 /*
448  * Probe function for CEX[45678] card device. It always
449  * accepts the AP device since the bus_match already checked
450  * the hardware type.
451  * @ap_dev: pointer to the AP device.
452  */
zcrypt_cex4_card_probe(struct ap_device * ap_dev)453 static int zcrypt_cex4_card_probe(struct ap_device *ap_dev)
454 {
455 	/*
456 	 * Normalized speed ratings per crypto adapter
457 	 * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
458 	 */
459 	static const int CEX4A_SPEED_IDX[NUM_OPS] = {
460 		 14,  19, 249, 42, 228, 1458, 0, 0};
461 	static const int CEX5A_SPEED_IDX[NUM_OPS] = {
462 		  8,   9,  20, 18,  66,	 458, 0, 0};
463 	static const int CEX6A_SPEED_IDX[NUM_OPS] = {
464 		  6,   9,  20, 17,  65,	 438, 0, 0};
465 	static const int CEX7A_SPEED_IDX[NUM_OPS] = {
466 		  6,   8,  17, 15,  54,	 362, 0, 0};
467 	static const int CEX8A_SPEED_IDX[NUM_OPS] = {
468 		  6,   8,  17, 15,  54,	 362, 0, 0};
469 
470 	static const int CEX4C_SPEED_IDX[NUM_OPS] = {
471 		 59,  69, 308, 83, 278, 2204, 209, 40};
472 	static const int CEX5C_SPEED_IDX[] = {
473 		 24,  31,  50, 37,  90,	 479,  27, 10};
474 	static const int CEX6C_SPEED_IDX[NUM_OPS] = {
475 		 16,  20,  32, 27,  77,	 455,  24,  9};
476 	static const int CEX7C_SPEED_IDX[NUM_OPS] = {
477 		 14,  16,  26, 23,  64,	 376,  23,  8};
478 	static const int CEX8C_SPEED_IDX[NUM_OPS] = {
479 		 14,  16,  26, 23,  64,	 376,  23,  8};
480 
481 	static const int CEX4P_SPEED_IDX[NUM_OPS] = {
482 		  0,   0,   0,	 0,   0,   0,	0,  50};
483 	static const int CEX5P_SPEED_IDX[NUM_OPS] = {
484 		  0,   0,   0,	 0,   0,   0,	0,  10};
485 	static const int CEX6P_SPEED_IDX[NUM_OPS] = {
486 		  0,   0,   0,	 0,   0,   0,	0,   9};
487 	static const int CEX7P_SPEED_IDX[NUM_OPS] = {
488 		  0,   0,   0,	 0,   0,   0,	0,   8};
489 	static const int CEX8P_SPEED_IDX[NUM_OPS] = {
490 		  0,   0,   0,	 0,   0,   0,	0,   8};
491 
492 	struct ap_card *ac = to_ap_card(&ap_dev->device);
493 	struct zcrypt_card *zc;
494 	int rc = 0;
495 
496 	zc = zcrypt_card_alloc();
497 	if (!zc)
498 		return -ENOMEM;
499 	zc->card = ac;
500 	dev_set_drvdata(&ap_dev->device, zc);
501 	if (ac->hwinfo.accel) {
502 		if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
503 			zc->type_string = "CEX4A";
504 			zc->user_space_type = ZCRYPT_CEX4;
505 			zc->speed_rating = CEX4A_SPEED_IDX;
506 		} else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) {
507 			zc->type_string = "CEX5A";
508 			zc->user_space_type = ZCRYPT_CEX5;
509 			zc->speed_rating = CEX5A_SPEED_IDX;
510 		} else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX6) {
511 			zc->type_string = "CEX6A";
512 			zc->user_space_type = ZCRYPT_CEX6;
513 			zc->speed_rating = CEX6A_SPEED_IDX;
514 		} else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX7) {
515 			zc->type_string = "CEX7A";
516 			zc->speed_rating = CEX7A_SPEED_IDX;
517 			/* wrong user space type, just for compatibility
518 			 * with the ZCRYPT_STATUS_MASK ioctl.
519 			 */
520 			zc->user_space_type = ZCRYPT_CEX6;
521 		} else {
522 			zc->type_string = "CEX8A";
523 			zc->speed_rating = CEX8A_SPEED_IDX;
524 			/* wrong user space type, just for compatibility
525 			 * with the ZCRYPT_STATUS_MASK ioctl.
526 			 */
527 			zc->user_space_type = ZCRYPT_CEX6;
528 		}
529 		zc->min_mod_size = CEX4A_MIN_MOD_SIZE;
530 		if (ac->hwinfo.mex4k && ac->hwinfo.crt4k) {
531 			zc->max_mod_size = CEX4A_MAX_MOD_SIZE_4K;
532 			zc->max_exp_bit_length =
533 				CEX4A_MAX_MOD_SIZE_4K;
534 		} else {
535 			zc->max_mod_size = CEX4A_MAX_MOD_SIZE_2K;
536 			zc->max_exp_bit_length =
537 				CEX4A_MAX_MOD_SIZE_2K;
538 		}
539 	} else if (ac->hwinfo.cca) {
540 		if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
541 			zc->type_string = "CEX4C";
542 			zc->speed_rating = CEX4C_SPEED_IDX;
543 			/* wrong user space type, must be CEX3C
544 			 * just keep it for cca compatibility
545 			 */
546 			zc->user_space_type = ZCRYPT_CEX3C;
547 		} else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) {
548 			zc->type_string = "CEX5C";
549 			zc->speed_rating = CEX5C_SPEED_IDX;
550 			/* wrong user space type, must be CEX3C
551 			 * just keep it for cca compatibility
552 			 */
553 			zc->user_space_type = ZCRYPT_CEX3C;
554 		} else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX6) {
555 			zc->type_string = "CEX6C";
556 			zc->speed_rating = CEX6C_SPEED_IDX;
557 			/* wrong user space type, must be CEX3C
558 			 * just keep it for cca compatibility
559 			 */
560 			zc->user_space_type = ZCRYPT_CEX3C;
561 		} else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX7) {
562 			zc->type_string = "CEX7C";
563 			zc->speed_rating = CEX7C_SPEED_IDX;
564 			/* wrong user space type, must be CEX3C
565 			 * just keep it for cca compatibility
566 			 */
567 			zc->user_space_type = ZCRYPT_CEX3C;
568 		} else {
569 			zc->type_string = "CEX8C";
570 			zc->speed_rating = CEX8C_SPEED_IDX;
571 			/* wrong user space type, must be CEX3C
572 			 * just keep it for cca compatibility
573 			 */
574 			zc->user_space_type = ZCRYPT_CEX3C;
575 		}
576 		zc->min_mod_size = CEX4C_MIN_MOD_SIZE;
577 		zc->max_mod_size = CEX4C_MAX_MOD_SIZE;
578 		zc->max_exp_bit_length = CEX4C_MAX_MOD_SIZE;
579 	} else if (ac->hwinfo.ep11) {
580 		if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
581 			zc->type_string = "CEX4P";
582 			zc->user_space_type = ZCRYPT_CEX4;
583 			zc->speed_rating = CEX4P_SPEED_IDX;
584 		} else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) {
585 			zc->type_string = "CEX5P";
586 			zc->user_space_type = ZCRYPT_CEX5;
587 			zc->speed_rating = CEX5P_SPEED_IDX;
588 		} else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX6) {
589 			zc->type_string = "CEX6P";
590 			zc->user_space_type = ZCRYPT_CEX6;
591 			zc->speed_rating = CEX6P_SPEED_IDX;
592 		} else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX7) {
593 			zc->type_string = "CEX7P";
594 			zc->speed_rating = CEX7P_SPEED_IDX;
595 			/* wrong user space type, just for compatibility
596 			 * with the ZCRYPT_STATUS_MASK ioctl.
597 			 */
598 			zc->user_space_type = ZCRYPT_CEX6;
599 		} else {
600 			zc->type_string = "CEX8P";
601 			zc->speed_rating = CEX8P_SPEED_IDX;
602 			/* wrong user space type, just for compatibility
603 			 * with the ZCRYPT_STATUS_MASK ioctl.
604 			 */
605 			zc->user_space_type = ZCRYPT_CEX6;
606 		}
607 		zc->min_mod_size = CEX4C_MIN_MOD_SIZE;
608 		zc->max_mod_size = CEX4C_MAX_MOD_SIZE;
609 		zc->max_exp_bit_length = CEX4C_MAX_MOD_SIZE;
610 	} else {
611 		zcrypt_card_free(zc);
612 		return -ENODEV;
613 	}
614 	zc->online = 1;
615 
616 	rc = zcrypt_card_register(zc);
617 	if (rc) {
618 		zcrypt_card_free(zc);
619 		return rc;
620 	}
621 
622 	if (ac->hwinfo.cca) {
623 		rc = sysfs_create_group(&ap_dev->device.kobj,
624 					&cca_card_attr_grp);
625 		if (rc) {
626 			zcrypt_card_unregister(zc);
627 			zcrypt_card_free(zc);
628 		}
629 	} else if (ac->hwinfo.ep11) {
630 		rc = sysfs_create_group(&ap_dev->device.kobj,
631 					&ep11_card_attr_grp);
632 		if (rc) {
633 			zcrypt_card_unregister(zc);
634 			zcrypt_card_free(zc);
635 		}
636 	}
637 
638 	return rc;
639 }
640 
641 /*
642  * This is called to remove the CEX[45678] card driver
643  * information if an AP card device is removed.
644  */
zcrypt_cex4_card_remove(struct ap_device * ap_dev)645 static void zcrypt_cex4_card_remove(struct ap_device *ap_dev)
646 {
647 	struct zcrypt_card *zc = dev_get_drvdata(&ap_dev->device);
648 	struct ap_card *ac = to_ap_card(&ap_dev->device);
649 
650 	if (ac->hwinfo.cca)
651 		sysfs_remove_group(&ap_dev->device.kobj, &cca_card_attr_grp);
652 	else if (ac->hwinfo.ep11)
653 		sysfs_remove_group(&ap_dev->device.kobj, &ep11_card_attr_grp);
654 
655 	zcrypt_card_unregister(zc);
656 }
657 
658 static struct ap_driver zcrypt_cex4_card_driver = {
659 	.probe = zcrypt_cex4_card_probe,
660 	.remove = zcrypt_cex4_card_remove,
661 	.ids = zcrypt_cex4_card_ids,
662 	.flags = AP_DRIVER_FLAG_DEFAULT,
663 };
664 
665 /*
666  * Probe function for CEX[45678] queue device. It always
667  * accepts the AP device since the bus_match already checked
668  * the hardware type.
669  * @ap_dev: pointer to the AP device.
670  */
zcrypt_cex4_queue_probe(struct ap_device * ap_dev)671 static int zcrypt_cex4_queue_probe(struct ap_device *ap_dev)
672 {
673 	struct ap_queue *aq = to_ap_queue(&ap_dev->device);
674 	struct zcrypt_queue *zq;
675 	int rc;
676 
677 	if (aq->card->hwinfo.accel) {
678 		zq = zcrypt_queue_alloc(aq->card->maxmsgsize);
679 		if (!zq)
680 			return -ENOMEM;
681 		zq->ops = zcrypt_msgtype(MSGTYPE50_NAME,
682 					 MSGTYPE50_VARIANT_DEFAULT);
683 	} else if (aq->card->hwinfo.cca) {
684 		zq = zcrypt_queue_alloc(aq->card->maxmsgsize);
685 		if (!zq)
686 			return -ENOMEM;
687 		zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
688 					 MSGTYPE06_VARIANT_DEFAULT);
689 	} else if (aq->card->hwinfo.ep11) {
690 		zq = zcrypt_queue_alloc(aq->card->maxmsgsize);
691 		if (!zq)
692 			return -ENOMEM;
693 		zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
694 					 MSGTYPE06_VARIANT_EP11);
695 	} else {
696 		return -ENODEV;
697 	}
698 
699 	zq->queue = aq;
700 	zq->online = 1;
701 	atomic_set(&zq->load, 0);
702 	ap_queue_init_state(aq);
703 	ap_queue_init_reply(aq, &zq->reply);
704 	aq->request_timeout = CEX4_CLEANUP_TIME;
705 	dev_set_drvdata(&ap_dev->device, zq);
706 	rc = zcrypt_queue_register(zq);
707 	if (rc) {
708 		zcrypt_queue_free(zq);
709 		return rc;
710 	}
711 
712 	if (aq->card->hwinfo.cca) {
713 		rc = sysfs_create_group(&ap_dev->device.kobj,
714 					&cca_queue_attr_grp);
715 		if (rc) {
716 			zcrypt_queue_unregister(zq);
717 			zcrypt_queue_free(zq);
718 		}
719 	} else if (aq->card->hwinfo.ep11) {
720 		rc = sysfs_create_group(&ap_dev->device.kobj,
721 					&ep11_queue_attr_grp);
722 		if (rc) {
723 			zcrypt_queue_unregister(zq);
724 			zcrypt_queue_free(zq);
725 		}
726 	}
727 
728 	return rc;
729 }
730 
731 /*
732  * This is called to remove the CEX[45678] queue driver
733  * information if an AP queue device is removed.
734  */
zcrypt_cex4_queue_remove(struct ap_device * ap_dev)735 static void zcrypt_cex4_queue_remove(struct ap_device *ap_dev)
736 {
737 	struct zcrypt_queue *zq = dev_get_drvdata(&ap_dev->device);
738 	struct ap_queue *aq = to_ap_queue(&ap_dev->device);
739 
740 	if (aq->card->hwinfo.cca)
741 		sysfs_remove_group(&ap_dev->device.kobj, &cca_queue_attr_grp);
742 	else if (aq->card->hwinfo.ep11)
743 		sysfs_remove_group(&ap_dev->device.kobj, &ep11_queue_attr_grp);
744 
745 	zcrypt_queue_unregister(zq);
746 }
747 
748 static struct ap_driver zcrypt_cex4_queue_driver = {
749 	.probe = zcrypt_cex4_queue_probe,
750 	.remove = zcrypt_cex4_queue_remove,
751 	.ids = zcrypt_cex4_queue_ids,
752 	.flags = AP_DRIVER_FLAG_DEFAULT,
753 };
754 
zcrypt_cex4_init(void)755 int __init zcrypt_cex4_init(void)
756 {
757 	int rc;
758 
759 	rc = ap_driver_register(&zcrypt_cex4_card_driver,
760 				THIS_MODULE, "cex4card");
761 	if (rc)
762 		return rc;
763 
764 	rc = ap_driver_register(&zcrypt_cex4_queue_driver,
765 				THIS_MODULE, "cex4queue");
766 	if (rc)
767 		ap_driver_unregister(&zcrypt_cex4_card_driver);
768 
769 	return rc;
770 }
771 
zcrypt_cex4_exit(void)772 void __exit zcrypt_cex4_exit(void)
773 {
774 	ap_driver_unregister(&zcrypt_cex4_queue_driver);
775 	ap_driver_unregister(&zcrypt_cex4_card_driver);
776 }
777 
778 module_init(zcrypt_cex4_init);
779 module_exit(zcrypt_cex4_exit);
780