xref: /linux/drivers/s390/crypto/zcrypt_cex4.c (revision 95298d63c67673c654c08952672d016212b26054)
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 & (1 << 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 & (1 << 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 		goto out;
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 	} else if (ap_test_bit(&ac->functions, AP_FUNC_EP11)) {
541 		rc = sysfs_create_group(&ap_dev->device.kobj,
542 					&ep11_card_attr_grp);
543 		if (rc)
544 			zcrypt_card_unregister(zc);
545 	}
546 
547 out:
548 	return rc;
549 }
550 
551 /**
552  * This is called to remove the CEX4/CEX5/CEX6/CEX7 card driver
553  * information if an AP card device is removed.
554  */
555 static void zcrypt_cex4_card_remove(struct ap_device *ap_dev)
556 {
557 	struct ap_card *ac = to_ap_card(&ap_dev->device);
558 	struct zcrypt_card *zc = ac->private;
559 
560 	if (ap_test_bit(&ac->functions, AP_FUNC_COPRO))
561 		sysfs_remove_group(&ap_dev->device.kobj, &cca_card_attr_grp);
562 	else if (ap_test_bit(&ac->functions, AP_FUNC_EP11))
563 		sysfs_remove_group(&ap_dev->device.kobj, &ep11_card_attr_grp);
564 	if (zc)
565 		zcrypt_card_unregister(zc);
566 }
567 
568 static struct ap_driver zcrypt_cex4_card_driver = {
569 	.probe = zcrypt_cex4_card_probe,
570 	.remove = zcrypt_cex4_card_remove,
571 	.ids = zcrypt_cex4_card_ids,
572 	.flags = AP_DRIVER_FLAG_DEFAULT,
573 };
574 
575 /**
576  * Probe function for CEX4/CEX5/CEX6/CEX7 queue device. It always
577  * accepts the AP device since the bus_match already checked
578  * the hardware type.
579  * @ap_dev: pointer to the AP device.
580  */
581 static int zcrypt_cex4_queue_probe(struct ap_device *ap_dev)
582 {
583 	struct ap_queue *aq = to_ap_queue(&ap_dev->device);
584 	struct zcrypt_queue *zq;
585 	int rc;
586 
587 	if (ap_test_bit(&aq->card->functions, AP_FUNC_ACCEL)) {
588 		zq = zcrypt_queue_alloc(CEX4A_MAX_MESSAGE_SIZE);
589 		if (!zq)
590 			return -ENOMEM;
591 		zq->ops = zcrypt_msgtype(MSGTYPE50_NAME,
592 					 MSGTYPE50_VARIANT_DEFAULT);
593 	} else if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO)) {
594 		zq = zcrypt_queue_alloc(CEX4C_MAX_MESSAGE_SIZE);
595 		if (!zq)
596 			return -ENOMEM;
597 		zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
598 					 MSGTYPE06_VARIANT_DEFAULT);
599 	} else if (ap_test_bit(&aq->card->functions, AP_FUNC_EP11)) {
600 		zq = zcrypt_queue_alloc(CEX4C_MAX_MESSAGE_SIZE);
601 		if (!zq)
602 			return -ENOMEM;
603 		zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
604 					 MSGTYPE06_VARIANT_EP11);
605 	} else {
606 		return -ENODEV;
607 	}
608 
609 	zq->queue = aq;
610 	zq->online = 1;
611 	atomic_set(&zq->load, 0);
612 	ap_queue_init_state(aq);
613 	ap_queue_init_reply(aq, &zq->reply);
614 	aq->request_timeout = CEX4_CLEANUP_TIME,
615 	aq->private = zq;
616 	rc = zcrypt_queue_register(zq);
617 	if (rc) {
618 		aq->private = NULL;
619 		zcrypt_queue_free(zq);
620 		goto out;
621 	}
622 
623 	if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO)) {
624 		rc = sysfs_create_group(&ap_dev->device.kobj,
625 					&cca_queue_attr_grp);
626 		if (rc)
627 			zcrypt_queue_unregister(zq);
628 	} else if (ap_test_bit(&aq->card->functions, AP_FUNC_EP11)) {
629 		rc = sysfs_create_group(&ap_dev->device.kobj,
630 					&ep11_queue_attr_grp);
631 		if (rc)
632 			zcrypt_queue_unregister(zq);
633 	}
634 
635 out:
636 	return rc;
637 }
638 
639 /**
640  * This is called to remove the CEX4/CEX5/CEX6/CEX7 queue driver
641  * information if an AP queue device is removed.
642  */
643 static void zcrypt_cex4_queue_remove(struct ap_device *ap_dev)
644 {
645 	struct ap_queue *aq = to_ap_queue(&ap_dev->device);
646 	struct zcrypt_queue *zq = aq->private;
647 
648 	if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO))
649 		sysfs_remove_group(&ap_dev->device.kobj, &cca_queue_attr_grp);
650 	else if (ap_test_bit(&aq->card->functions, AP_FUNC_EP11))
651 		sysfs_remove_group(&ap_dev->device.kobj, &ep11_queue_attr_grp);
652 	if (zq)
653 		zcrypt_queue_unregister(zq);
654 }
655 
656 static struct ap_driver zcrypt_cex4_queue_driver = {
657 	.probe = zcrypt_cex4_queue_probe,
658 	.remove = zcrypt_cex4_queue_remove,
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