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