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