xref: /openbmc/linux/drivers/s390/crypto/zcrypt_cex4.c (revision 7051924f771722c6dd235e693742cda6488ac700)
1 /*
2  *  Copyright IBM Corp. 2012
3  *  Author(s): Holger Dengler <hd@linux.vnet.ibm.com>
4  */
5 
6 #include <linux/module.h>
7 #include <linux/slab.h>
8 #include <linux/init.h>
9 #include <linux/err.h>
10 #include <linux/atomic.h>
11 #include <linux/uaccess.h>
12 
13 #include "ap_bus.h"
14 #include "zcrypt_api.h"
15 #include "zcrypt_msgtype6.h"
16 #include "zcrypt_msgtype50.h"
17 #include "zcrypt_error.h"
18 #include "zcrypt_cex4.h"
19 
20 #define CEX4A_MIN_MOD_SIZE	  1	/*    8 bits	*/
21 #define CEX4A_MAX_MOD_SIZE_2K	256	/* 2048 bits	*/
22 #define CEX4A_MAX_MOD_SIZE_4K	512	/* 4096 bits	*/
23 
24 #define CEX4C_MIN_MOD_SIZE	 16	/*  256 bits	*/
25 #define CEX4C_MAX_MOD_SIZE	512	/* 4096 bits	*/
26 
27 #define CEX4A_SPEED_RATING	900	 /* TODO new card, new speed rating */
28 #define CEX4C_SPEED_RATING	6500	 /* TODO new card, new speed rating */
29 
30 #define CEX4A_MAX_MESSAGE_SIZE	MSGTYPE50_CRB3_MAX_MSG_SIZE
31 #define CEX4C_MAX_MESSAGE_SIZE	MSGTYPE06_MAX_MSG_SIZE
32 
33 /* Waiting time for requests to be processed.
34  * Currently there are some types of request which are not deterministic.
35  * But the maximum time limit managed by the stomper code is set to 60sec.
36  * Hence we have to wait at least that time period.
37  */
38 #define CEX4_CLEANUP_TIME	(61*HZ)
39 
40 static struct ap_device_id zcrypt_cex4_ids[] = {
41 	{ AP_DEVICE(AP_DEVICE_TYPE_CEX4)  },
42 	{ /* end of list */ },
43 };
44 
45 MODULE_DEVICE_TABLE(ap, zcrypt_cex4_ids);
46 MODULE_AUTHOR("IBM Corporation");
47 MODULE_DESCRIPTION("CEX4 Cryptographic Card device driver, " \
48 		   "Copyright IBM Corp. 2012");
49 MODULE_LICENSE("GPL");
50 
51 static int zcrypt_cex4_probe(struct ap_device *ap_dev);
52 static void zcrypt_cex4_remove(struct ap_device *ap_dev);
53 
54 static struct ap_driver zcrypt_cex4_driver = {
55 	.probe = zcrypt_cex4_probe,
56 	.remove = zcrypt_cex4_remove,
57 	.ids = zcrypt_cex4_ids,
58 	.request_timeout = CEX4_CLEANUP_TIME,
59 };
60 
61 /**
62  * Probe function for CEX4 cards. It always accepts the AP device
63  * since the bus_match already checked the hardware type.
64  * @ap_dev: pointer to the AP device.
65  */
66 static int zcrypt_cex4_probe(struct ap_device *ap_dev)
67 {
68 	struct zcrypt_device *zdev = NULL;
69 	int rc = 0;
70 
71 	switch (ap_dev->device_type) {
72 	case AP_DEVICE_TYPE_CEX4:
73 		if (ap_test_bit(&ap_dev->functions, AP_FUNC_ACCEL)) {
74 			zdev = zcrypt_device_alloc(CEX4A_MAX_MESSAGE_SIZE);
75 			if (!zdev)
76 				return -ENOMEM;
77 			zdev->type_string = "CEX4A";
78 			zdev->user_space_type = ZCRYPT_CEX3A;
79 			zdev->min_mod_size = CEX4A_MIN_MOD_SIZE;
80 			if (ap_test_bit(&ap_dev->functions, AP_FUNC_MEX4K) &&
81 			    ap_test_bit(&ap_dev->functions, AP_FUNC_CRT4K)) {
82 				zdev->max_mod_size =
83 					CEX4A_MAX_MOD_SIZE_4K;
84 				zdev->max_exp_bit_length =
85 					CEX4A_MAX_MOD_SIZE_4K;
86 			} else {
87 				zdev->max_mod_size =
88 					CEX4A_MAX_MOD_SIZE_2K;
89 				zdev->max_exp_bit_length =
90 					CEX4A_MAX_MOD_SIZE_2K;
91 			}
92 			zdev->short_crt = 1;
93 			zdev->speed_rating = CEX4A_SPEED_RATING;
94 			zdev->ops = zcrypt_msgtype_request(MSGTYPE50_NAME,
95 							   MSGTYPE50_VARIANT_DEFAULT);
96 		} else if (ap_test_bit(&ap_dev->functions, AP_FUNC_COPRO)) {
97 			zdev = zcrypt_device_alloc(CEX4C_MAX_MESSAGE_SIZE);
98 			if (!zdev)
99 				return -ENOMEM;
100 			zdev->type_string = "CEX4C";
101 			zdev->user_space_type = ZCRYPT_CEX3C;
102 			zdev->min_mod_size = CEX4C_MIN_MOD_SIZE;
103 			zdev->max_mod_size = CEX4C_MAX_MOD_SIZE;
104 			zdev->max_exp_bit_length = CEX4C_MAX_MOD_SIZE;
105 			zdev->short_crt = 0;
106 			zdev->speed_rating = CEX4C_SPEED_RATING;
107 			zdev->ops = zcrypt_msgtype_request(MSGTYPE06_NAME,
108 							   MSGTYPE06_VARIANT_DEFAULT);
109 		} else if (ap_test_bit(&ap_dev->functions, AP_FUNC_EP11)) {
110 			zdev = zcrypt_device_alloc(CEX4C_MAX_MESSAGE_SIZE);
111 			if (!zdev)
112 				return -ENOMEM;
113 			zdev->type_string = "CEX4P";
114 			zdev->user_space_type = ZCRYPT_CEX4;
115 			zdev->min_mod_size = CEX4C_MIN_MOD_SIZE;
116 			zdev->max_mod_size = CEX4C_MAX_MOD_SIZE;
117 			zdev->max_exp_bit_length = CEX4C_MAX_MOD_SIZE;
118 			zdev->short_crt = 0;
119 			zdev->speed_rating = CEX4C_SPEED_RATING;
120 			zdev->ops = zcrypt_msgtype_request(MSGTYPE06_NAME,
121 							MSGTYPE06_VARIANT_EP11);
122 		}
123 		break;
124 	}
125 	if (!zdev)
126 		return -ENODEV;
127 	zdev->ap_dev = ap_dev;
128 	zdev->online = 1;
129 	ap_dev->reply = &zdev->reply;
130 	ap_dev->private = zdev;
131 	rc = zcrypt_device_register(zdev);
132 	if (rc) {
133 		zcrypt_msgtype_release(zdev->ops);
134 		ap_dev->private = NULL;
135 		zcrypt_device_free(zdev);
136 	}
137 	return rc;
138 }
139 
140 /**
141  * This is called to remove the extended CEX4 driver information
142  * if an AP device is removed.
143  */
144 static void zcrypt_cex4_remove(struct ap_device *ap_dev)
145 {
146 	struct zcrypt_device *zdev = ap_dev->private;
147 	struct zcrypt_ops *zops;
148 
149 	if (zdev) {
150 		zops = zdev->ops;
151 		zcrypt_device_unregister(zdev);
152 		zcrypt_msgtype_release(zops);
153 	}
154 }
155 
156 int __init zcrypt_cex4_init(void)
157 {
158 	return ap_driver_register(&zcrypt_cex4_driver, THIS_MODULE, "cex4");
159 }
160 
161 void __exit zcrypt_cex4_exit(void)
162 {
163 	ap_driver_unregister(&zcrypt_cex4_driver);
164 }
165 
166 module_init(zcrypt_cex4_init);
167 module_exit(zcrypt_cex4_exit);
168