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