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