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