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