2 * Copyright IBM Corp. 2012
3 * Author(s): Holger Dengler <hd@linux.vnet.ibm.com>
6 #include <linux/module.h>
7 #include <linux/slab.h>
8 #include <linux/init.h>
10 #include <linux/atomic.h>
11 #include <linux/uaccess.h>
12 #include <linux/mod_devicetable.h>
15 #include "zcrypt_api.h"
16 #include "zcrypt_msgtype6.h"
17 #include "zcrypt_msgtype50.h"
18 #include "zcrypt_error.h"
19 #include "zcrypt_cex4.h"
21 #define CEX4A_MIN_MOD_SIZE 1 /* 8 bits */
22 #define CEX4A_MAX_MOD_SIZE_2K 256 /* 2048 bits */
23 #define CEX4A_MAX_MOD_SIZE_4K 512 /* 4096 bits */
25 #define CEX4C_MIN_MOD_SIZE 16 /* 256 bits */
26 #define CEX4C_MAX_MOD_SIZE 512 /* 4096 bits */
28 #define CEX4A_MAX_MESSAGE_SIZE MSGTYPE50_CRB3_MAX_MSG_SIZE
29 #define CEX4C_MAX_MESSAGE_SIZE MSGTYPE06_MAX_MSG_SIZE
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.
36 #define CEX4_CLEANUP_TIME (900*HZ)
38 MODULE_AUTHOR("IBM Corporation");
39 MODULE_DESCRIPTION("CEX4 Cryptographic Card device driver, " \
40 "Copyright IBM Corp. 2012");
41 MODULE_LICENSE("GPL");
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 { /* end of list */ },
53 MODULE_DEVICE_TABLE(ap
, zcrypt_cex4_card_ids
);
55 static struct ap_device_id zcrypt_cex4_queue_ids
[] = {
56 { .dev_type
= AP_DEVICE_TYPE_CEX4
,
57 .match_flags
= AP_DEVICE_ID_MATCH_QUEUE_TYPE
},
58 { .dev_type
= AP_DEVICE_TYPE_CEX5
,
59 .match_flags
= AP_DEVICE_ID_MATCH_QUEUE_TYPE
},
60 { .dev_type
= AP_DEVICE_TYPE_CEX6
,
61 .match_flags
= AP_DEVICE_ID_MATCH_QUEUE_TYPE
},
62 { /* end of list */ },
65 MODULE_DEVICE_TABLE(ap
, zcrypt_cex4_queue_ids
);
68 * Probe function for CEX4 card device. It always accepts the AP device
69 * since the bus_match already checked the hardware type.
70 * @ap_dev: pointer to the AP device.
72 static int zcrypt_cex4_card_probe(struct ap_device
*ap_dev
)
75 * Normalized speed ratings per crypto adapter
76 * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
78 static const int CEX4A_SPEED_IDX
[] = {
79 14, 19, 249, 42, 228, 1458, 0, 0};
80 static const int CEX5A_SPEED_IDX
[] = {
81 8, 9, 20, 18, 66, 458, 0, 0};
82 static const int CEX6A_SPEED_IDX
[] = {
83 6, 9, 20, 17, 65, 438, 0, 0};
85 static const int CEX4C_SPEED_IDX
[] = {
86 59, 69, 308, 83, 278, 2204, 209, 40};
87 static const int CEX5C_SPEED_IDX
[] = {
88 24, 31, 50, 37, 90, 479, 27, 10};
89 static const int CEX6C_SPEED_IDX
[] = {
90 16, 20, 32, 27, 77, 455, 23, 9};
92 static const int CEX4P_SPEED_IDX
[] = {
93 224, 313, 3560, 359, 605, 2827, 0, 50};
94 static const int CEX5P_SPEED_IDX
[] = {
95 63, 84, 156, 83, 142, 533, 0, 10};
96 static const int CEX6P_SPEED_IDX
[] = {
97 55, 70, 121, 73, 129, 522, 0, 9};
99 struct ap_card
*ac
= to_ap_card(&ap_dev
->device
);
100 struct zcrypt_card
*zc
;
103 zc
= zcrypt_card_alloc();
108 if (ap_test_bit(&ac
->functions
, AP_FUNC_ACCEL
)) {
109 if (ac
->ap_dev
.device_type
== AP_DEVICE_TYPE_CEX4
) {
110 zc
->type_string
= "CEX4A";
111 zc
->user_space_type
= ZCRYPT_CEX4
;
112 memcpy(zc
->speed_rating
, CEX4A_SPEED_IDX
,
113 sizeof(CEX4A_SPEED_IDX
));
114 } else if (ac
->ap_dev
.device_type
== AP_DEVICE_TYPE_CEX5
) {
115 zc
->type_string
= "CEX5A";
116 zc
->user_space_type
= ZCRYPT_CEX5
;
117 memcpy(zc
->speed_rating
, CEX5A_SPEED_IDX
,
118 sizeof(CEX5A_SPEED_IDX
));
120 zc
->type_string
= "CEX6A";
121 zc
->user_space_type
= ZCRYPT_CEX6
;
122 memcpy(zc
->speed_rating
, CEX6A_SPEED_IDX
,
123 sizeof(CEX6A_SPEED_IDX
));
125 zc
->min_mod_size
= CEX4A_MIN_MOD_SIZE
;
126 if (ap_test_bit(&ac
->functions
, AP_FUNC_MEX4K
) &&
127 ap_test_bit(&ac
->functions
, AP_FUNC_CRT4K
)) {
128 zc
->max_mod_size
= CEX4A_MAX_MOD_SIZE_4K
;
129 zc
->max_exp_bit_length
=
130 CEX4A_MAX_MOD_SIZE_4K
;
132 zc
->max_mod_size
= CEX4A_MAX_MOD_SIZE_2K
;
133 zc
->max_exp_bit_length
=
134 CEX4A_MAX_MOD_SIZE_2K
;
136 } else if (ap_test_bit(&ac
->functions
, AP_FUNC_COPRO
)) {
137 if (ac
->ap_dev
.device_type
== AP_DEVICE_TYPE_CEX4
) {
138 zc
->type_string
= "CEX4C";
139 /* wrong user space type, must be CEX4
140 * just keep it for cca compatibility
142 zc
->user_space_type
= ZCRYPT_CEX3C
;
143 memcpy(zc
->speed_rating
, CEX4C_SPEED_IDX
,
144 sizeof(CEX4C_SPEED_IDX
));
145 } else if (ac
->ap_dev
.device_type
== AP_DEVICE_TYPE_CEX5
) {
146 zc
->type_string
= "CEX5C";
147 /* wrong user space type, must be CEX5
148 * just keep it for cca compatibility
150 zc
->user_space_type
= ZCRYPT_CEX3C
;
151 memcpy(zc
->speed_rating
, CEX5C_SPEED_IDX
,
152 sizeof(CEX5C_SPEED_IDX
));
154 zc
->type_string
= "CEX6C";
155 /* wrong user space type, must be CEX6
156 * just keep it for cca compatibility
158 zc
->user_space_type
= ZCRYPT_CEX3C
;
159 memcpy(zc
->speed_rating
, CEX6C_SPEED_IDX
,
160 sizeof(CEX6C_SPEED_IDX
));
162 zc
->min_mod_size
= CEX4C_MIN_MOD_SIZE
;
163 zc
->max_mod_size
= CEX4C_MAX_MOD_SIZE
;
164 zc
->max_exp_bit_length
= CEX4C_MAX_MOD_SIZE
;
165 } else if (ap_test_bit(&ac
->functions
, AP_FUNC_EP11
)) {
166 if (ac
->ap_dev
.device_type
== AP_DEVICE_TYPE_CEX4
) {
167 zc
->type_string
= "CEX4P";
168 zc
->user_space_type
= ZCRYPT_CEX4
;
169 memcpy(zc
->speed_rating
, CEX4P_SPEED_IDX
,
170 sizeof(CEX4P_SPEED_IDX
));
171 } else if (ac
->ap_dev
.device_type
== AP_DEVICE_TYPE_CEX5
) {
172 zc
->type_string
= "CEX5P";
173 zc
->user_space_type
= ZCRYPT_CEX5
;
174 memcpy(zc
->speed_rating
, CEX5P_SPEED_IDX
,
175 sizeof(CEX5P_SPEED_IDX
));
177 zc
->type_string
= "CEX6P";
178 zc
->user_space_type
= ZCRYPT_CEX6
;
179 memcpy(zc
->speed_rating
, CEX6P_SPEED_IDX
,
180 sizeof(CEX6P_SPEED_IDX
));
182 zc
->min_mod_size
= CEX4C_MIN_MOD_SIZE
;
183 zc
->max_mod_size
= CEX4C_MAX_MOD_SIZE
;
184 zc
->max_exp_bit_length
= CEX4C_MAX_MOD_SIZE
;
186 zcrypt_card_free(zc
);
191 rc
= zcrypt_card_register(zc
);
194 zcrypt_card_free(zc
);
201 * This is called to remove the CEX4 card driver information
202 * if an AP card device is removed.
204 static void zcrypt_cex4_card_remove(struct ap_device
*ap_dev
)
206 struct zcrypt_card
*zc
= to_ap_card(&ap_dev
->device
)->private;
209 zcrypt_card_unregister(zc
);
212 static struct ap_driver zcrypt_cex4_card_driver
= {
213 .probe
= zcrypt_cex4_card_probe
,
214 .remove
= zcrypt_cex4_card_remove
,
215 .ids
= zcrypt_cex4_card_ids
,
219 * Probe function for CEX4 queue device. It always accepts the AP device
220 * since the bus_match already checked the hardware type.
221 * @ap_dev: pointer to the AP device.
223 static int zcrypt_cex4_queue_probe(struct ap_device
*ap_dev
)
225 struct ap_queue
*aq
= to_ap_queue(&ap_dev
->device
);
226 struct zcrypt_queue
*zq
;
229 if (ap_test_bit(&aq
->card
->functions
, AP_FUNC_ACCEL
)) {
230 zq
= zcrypt_queue_alloc(CEX4A_MAX_MESSAGE_SIZE
);
233 zq
->ops
= zcrypt_msgtype(MSGTYPE50_NAME
,
234 MSGTYPE50_VARIANT_DEFAULT
);
235 } else if (ap_test_bit(&aq
->card
->functions
, AP_FUNC_COPRO
)) {
236 zq
= zcrypt_queue_alloc(CEX4C_MAX_MESSAGE_SIZE
);
239 zq
->ops
= zcrypt_msgtype(MSGTYPE06_NAME
,
240 MSGTYPE06_VARIANT_DEFAULT
);
241 } else if (ap_test_bit(&aq
->card
->functions
, AP_FUNC_EP11
)) {
242 zq
= zcrypt_queue_alloc(CEX4C_MAX_MESSAGE_SIZE
);
245 zq
->ops
= zcrypt_msgtype(MSGTYPE06_NAME
,
246 MSGTYPE06_VARIANT_EP11
);
252 atomic_set(&zq
->load
, 0);
253 ap_queue_init_reply(aq
, &zq
->reply
);
254 aq
->request_timeout
= CEX4_CLEANUP_TIME
,
256 rc
= zcrypt_queue_register(zq
);
259 zcrypt_queue_free(zq
);
266 * This is called to remove the CEX4 queue driver information
267 * if an AP queue device is removed.
269 static void zcrypt_cex4_queue_remove(struct ap_device
*ap_dev
)
271 struct ap_queue
*aq
= to_ap_queue(&ap_dev
->device
);
272 struct zcrypt_queue
*zq
= aq
->private;
276 zcrypt_queue_unregister(zq
);
279 static struct ap_driver zcrypt_cex4_queue_driver
= {
280 .probe
= zcrypt_cex4_queue_probe
,
281 .remove
= zcrypt_cex4_queue_remove
,
282 .suspend
= ap_queue_suspend
,
283 .resume
= ap_queue_resume
,
284 .ids
= zcrypt_cex4_queue_ids
,
287 int __init
zcrypt_cex4_init(void)
291 rc
= ap_driver_register(&zcrypt_cex4_card_driver
,
292 THIS_MODULE
, "cex4card");
296 rc
= ap_driver_register(&zcrypt_cex4_queue_driver
,
297 THIS_MODULE
, "cex4queue");
299 ap_driver_unregister(&zcrypt_cex4_card_driver
);
304 void __exit
zcrypt_cex4_exit(void)
306 ap_driver_unregister(&zcrypt_cex4_queue_driver
);
307 ap_driver_unregister(&zcrypt_cex4_card_driver
);
310 module_init(zcrypt_cex4_init
);
311 module_exit(zcrypt_cex4_exit
);