1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2012
4 * Author(s): Holger Dengler <hd@linux.vnet.ibm.com>
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>
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"
23 #define CEX4A_MIN_MOD_SIZE 1 /* 8 bits */
24 #define CEX4A_MAX_MOD_SIZE_2K 256 /* 2048 bits */
25 #define CEX4A_MAX_MOD_SIZE_4K 512 /* 4096 bits */
27 #define CEX4C_MIN_MOD_SIZE 16 /* 256 bits */
28 #define CEX4C_MAX_MOD_SIZE 512 /* 4096 bits */
30 #define CEX4A_MAX_MESSAGE_SIZE MSGTYPE50_CRB3_MAX_MSG_SIZE
31 #define CEX4C_MAX_MESSAGE_SIZE MSGTYPE06_MAX_MSG_SIZE
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.
38 #define CEX4_CLEANUP_TIME (900*HZ)
40 MODULE_AUTHOR("IBM Corporation");
41 MODULE_DESCRIPTION("CEX4/CEX5/CEX6 Cryptographic Card device driver, " \
42 "Copyright IBM Corp. 2018");
43 MODULE_LICENSE("GPL");
45 static struct ap_device_id zcrypt_cex4_card_ids[] = {
46 { .dev_type = AP_DEVICE_TYPE_CEX4,
47 .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
48 { .dev_type = AP_DEVICE_TYPE_CEX5,
49 .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
50 { .dev_type = AP_DEVICE_TYPE_CEX6,
51 .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
52 { /* end of list */ },
55 MODULE_DEVICE_TABLE(ap, zcrypt_cex4_card_ids);
57 static struct ap_device_id zcrypt_cex4_queue_ids[] = {
58 { .dev_type = AP_DEVICE_TYPE_CEX4,
59 .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
60 { .dev_type = AP_DEVICE_TYPE_CEX5,
61 .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
62 { .dev_type = AP_DEVICE_TYPE_CEX6,
63 .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
64 { /* end of list */ },
67 MODULE_DEVICE_TABLE(ap, zcrypt_cex4_queue_ids);
70 * CCA card addditional device attributes
72 static ssize_t serialnr_show(struct device *dev,
73 struct device_attribute *attr,
77 struct ap_card *ac = to_ap_card(dev);
78 struct zcrypt_card *zc = ac->private;
80 memset(&ci, 0, sizeof(ci));
82 if (ap_domain_index >= 0)
83 cca_get_info(ac->id, ap_domain_index, &ci, zc->online);
85 return snprintf(buf, PAGE_SIZE, "%s\n", ci.serial);
87 static DEVICE_ATTR_RO(serialnr);
89 static struct attribute *cca_card_attrs[] = {
90 &dev_attr_serialnr.attr,
94 static const struct attribute_group cca_card_attr_group = {
95 .attrs = cca_card_attrs,
99 * CCA queue addditional device attributes
101 static ssize_t mkvps_show(struct device *dev,
102 struct device_attribute *attr,
107 struct zcrypt_queue *zq = to_ap_queue(dev)->private;
108 static const char * const cao_state[] = { "invalid", "valid" };
109 static const char * const new_state[] = { "empty", "partial", "full" };
111 memset(&ci, 0, sizeof(ci));
113 cca_get_info(AP_QID_CARD(zq->queue->qid),
114 AP_QID_QUEUE(zq->queue->qid),
117 if (ci.new_mk_state >= '1' && ci.new_mk_state <= '3')
118 n = snprintf(buf, PAGE_SIZE, "AES NEW: %s 0x%016llx\n",
119 new_state[ci.new_mk_state - '1'], ci.new_mkvp);
121 n = snprintf(buf, PAGE_SIZE, "AES NEW: - -\n");
123 if (ci.cur_mk_state >= '1' && ci.cur_mk_state <= '2')
124 n += snprintf(buf + n, PAGE_SIZE - n, "AES CUR: %s 0x%016llx\n",
125 cao_state[ci.cur_mk_state - '1'], ci.cur_mkvp);
127 n += snprintf(buf + n, PAGE_SIZE - n, "AES CUR: - -\n");
129 if (ci.old_mk_state >= '1' && ci.old_mk_state <= '2')
130 n += snprintf(buf + n, PAGE_SIZE - n, "AES OLD: %s 0x%016llx\n",
131 cao_state[ci.old_mk_state - '1'], ci.old_mkvp);
133 n += snprintf(buf + n, PAGE_SIZE - n, "AES OLD: - -\n");
137 static DEVICE_ATTR_RO(mkvps);
139 static struct attribute *cca_queue_attrs[] = {
140 &dev_attr_mkvps.attr,
144 static const struct attribute_group cca_queue_attr_group = {
145 .attrs = cca_queue_attrs,
149 * Probe function for CEX4/CEX5/CEX6 card device. It always
150 * accepts the AP device since the bus_match already checked
152 * @ap_dev: pointer to the AP device.
154 static int zcrypt_cex4_card_probe(struct ap_device *ap_dev)
157 * Normalized speed ratings per crypto adapter
158 * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
160 static const int CEX4A_SPEED_IDX[] = {
161 14, 19, 249, 42, 228, 1458, 0, 0};
162 static const int CEX5A_SPEED_IDX[] = {
163 8, 9, 20, 18, 66, 458, 0, 0};
164 static const int CEX6A_SPEED_IDX[] = {
165 6, 9, 20, 17, 65, 438, 0, 0};
167 static const int CEX4C_SPEED_IDX[] = {
168 59, 69, 308, 83, 278, 2204, 209, 40};
169 static const int CEX5C_SPEED_IDX[] = {
170 24, 31, 50, 37, 90, 479, 27, 10};
171 static const int CEX6C_SPEED_IDX[] = {
172 16, 20, 32, 27, 77, 455, 23, 9};
174 static const int CEX4P_SPEED_IDX[] = {
175 224, 313, 3560, 359, 605, 2827, 0, 50};
176 static const int CEX5P_SPEED_IDX[] = {
177 63, 84, 156, 83, 142, 533, 0, 10};
178 static const int CEX6P_SPEED_IDX[] = {
179 55, 70, 121, 73, 129, 522, 0, 9};
181 struct ap_card *ac = to_ap_card(&ap_dev->device);
182 struct zcrypt_card *zc;
185 zc = zcrypt_card_alloc();
190 if (ap_test_bit(&ac->functions, AP_FUNC_ACCEL)) {
191 if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
192 zc->type_string = "CEX4A";
193 zc->user_space_type = ZCRYPT_CEX4;
194 memcpy(zc->speed_rating, CEX4A_SPEED_IDX,
195 sizeof(CEX4A_SPEED_IDX));
196 } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) {
197 zc->type_string = "CEX5A";
198 zc->user_space_type = ZCRYPT_CEX5;
199 memcpy(zc->speed_rating, CEX5A_SPEED_IDX,
200 sizeof(CEX5A_SPEED_IDX));
202 zc->type_string = "CEX6A";
203 zc->user_space_type = ZCRYPT_CEX6;
204 memcpy(zc->speed_rating, CEX6A_SPEED_IDX,
205 sizeof(CEX6A_SPEED_IDX));
207 zc->min_mod_size = CEX4A_MIN_MOD_SIZE;
208 if (ap_test_bit(&ac->functions, AP_FUNC_MEX4K) &&
209 ap_test_bit(&ac->functions, AP_FUNC_CRT4K)) {
210 zc->max_mod_size = CEX4A_MAX_MOD_SIZE_4K;
211 zc->max_exp_bit_length =
212 CEX4A_MAX_MOD_SIZE_4K;
214 zc->max_mod_size = CEX4A_MAX_MOD_SIZE_2K;
215 zc->max_exp_bit_length =
216 CEX4A_MAX_MOD_SIZE_2K;
218 } else if (ap_test_bit(&ac->functions, AP_FUNC_COPRO)) {
219 if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
220 zc->type_string = "CEX4C";
221 /* wrong user space type, must be CEX4
222 * just keep it for cca compatibility
224 zc->user_space_type = ZCRYPT_CEX3C;
225 memcpy(zc->speed_rating, CEX4C_SPEED_IDX,
226 sizeof(CEX4C_SPEED_IDX));
227 } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) {
228 zc->type_string = "CEX5C";
229 /* wrong user space type, must be CEX5
230 * just keep it for cca compatibility
232 zc->user_space_type = ZCRYPT_CEX3C;
233 memcpy(zc->speed_rating, CEX5C_SPEED_IDX,
234 sizeof(CEX5C_SPEED_IDX));
236 zc->type_string = "CEX6C";
237 /* wrong user space type, must be CEX6
238 * just keep it for cca compatibility
240 zc->user_space_type = ZCRYPT_CEX3C;
241 memcpy(zc->speed_rating, CEX6C_SPEED_IDX,
242 sizeof(CEX6C_SPEED_IDX));
244 zc->min_mod_size = CEX4C_MIN_MOD_SIZE;
245 zc->max_mod_size = CEX4C_MAX_MOD_SIZE;
246 zc->max_exp_bit_length = CEX4C_MAX_MOD_SIZE;
247 } else if (ap_test_bit(&ac->functions, AP_FUNC_EP11)) {
248 if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
249 zc->type_string = "CEX4P";
250 zc->user_space_type = ZCRYPT_CEX4;
251 memcpy(zc->speed_rating, CEX4P_SPEED_IDX,
252 sizeof(CEX4P_SPEED_IDX));
253 } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) {
254 zc->type_string = "CEX5P";
255 zc->user_space_type = ZCRYPT_CEX5;
256 memcpy(zc->speed_rating, CEX5P_SPEED_IDX,
257 sizeof(CEX5P_SPEED_IDX));
259 zc->type_string = "CEX6P";
260 zc->user_space_type = ZCRYPT_CEX6;
261 memcpy(zc->speed_rating, CEX6P_SPEED_IDX,
262 sizeof(CEX6P_SPEED_IDX));
264 zc->min_mod_size = CEX4C_MIN_MOD_SIZE;
265 zc->max_mod_size = CEX4C_MAX_MOD_SIZE;
266 zc->max_exp_bit_length = CEX4C_MAX_MOD_SIZE;
268 zcrypt_card_free(zc);
273 rc = zcrypt_card_register(zc);
276 zcrypt_card_free(zc);
280 if (ap_test_bit(&ac->functions, AP_FUNC_COPRO)) {
281 rc = sysfs_create_group(&ap_dev->device.kobj,
282 &cca_card_attr_group);
284 zcrypt_card_unregister(zc);
292 * This is called to remove the CEX4/CEX5/CEX6 card driver information
293 * if an AP card device is removed.
295 static void zcrypt_cex4_card_remove(struct ap_device *ap_dev)
297 struct ap_card *ac = to_ap_card(&ap_dev->device);
298 struct zcrypt_card *zc = ac->private;
300 if (ap_test_bit(&ac->functions, AP_FUNC_COPRO))
301 sysfs_remove_group(&ap_dev->device.kobj, &cca_card_attr_group);
303 zcrypt_card_unregister(zc);
306 static struct ap_driver zcrypt_cex4_card_driver = {
307 .probe = zcrypt_cex4_card_probe,
308 .remove = zcrypt_cex4_card_remove,
309 .ids = zcrypt_cex4_card_ids,
310 .flags = AP_DRIVER_FLAG_DEFAULT,
314 * Probe function for CEX4/CEX5/CEX6 queue device. It always
315 * accepts the AP device since the bus_match already checked
317 * @ap_dev: pointer to the AP device.
319 static int zcrypt_cex4_queue_probe(struct ap_device *ap_dev)
321 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
322 struct zcrypt_queue *zq;
325 if (ap_test_bit(&aq->card->functions, AP_FUNC_ACCEL)) {
326 zq = zcrypt_queue_alloc(CEX4A_MAX_MESSAGE_SIZE);
329 zq->ops = zcrypt_msgtype(MSGTYPE50_NAME,
330 MSGTYPE50_VARIANT_DEFAULT);
331 } else if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO)) {
332 zq = zcrypt_queue_alloc(CEX4C_MAX_MESSAGE_SIZE);
335 zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
336 MSGTYPE06_VARIANT_DEFAULT);
337 } else if (ap_test_bit(&aq->card->functions, AP_FUNC_EP11)) {
338 zq = zcrypt_queue_alloc(CEX4C_MAX_MESSAGE_SIZE);
341 zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
342 MSGTYPE06_VARIANT_EP11);
349 atomic_set(&zq->load, 0);
350 ap_queue_init_reply(aq, &zq->reply);
351 aq->request_timeout = CEX4_CLEANUP_TIME,
353 rc = zcrypt_queue_register(zq);
356 zcrypt_queue_free(zq);
360 if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO)) {
361 rc = sysfs_create_group(&ap_dev->device.kobj,
362 &cca_queue_attr_group);
364 zcrypt_queue_unregister(zq);
372 * This is called to remove the CEX4/CEX5/CEX6 queue driver
373 * information if an AP queue device is removed.
375 static void zcrypt_cex4_queue_remove(struct ap_device *ap_dev)
377 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
378 struct zcrypt_queue *zq = aq->private;
380 if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO))
381 sysfs_remove_group(&ap_dev->device.kobj, &cca_queue_attr_group);
383 zcrypt_queue_unregister(zq);
386 static struct ap_driver zcrypt_cex4_queue_driver = {
387 .probe = zcrypt_cex4_queue_probe,
388 .remove = zcrypt_cex4_queue_remove,
389 .suspend = ap_queue_suspend,
390 .resume = ap_queue_resume,
391 .ids = zcrypt_cex4_queue_ids,
392 .flags = AP_DRIVER_FLAG_DEFAULT,
395 int __init zcrypt_cex4_init(void)
399 rc = ap_driver_register(&zcrypt_cex4_card_driver,
400 THIS_MODULE, "cex4card");
404 rc = ap_driver_register(&zcrypt_cex4_queue_driver,
405 THIS_MODULE, "cex4queue");
407 ap_driver_unregister(&zcrypt_cex4_card_driver);
412 void __exit zcrypt_cex4_exit(void)
414 ap_driver_unregister(&zcrypt_cex4_queue_driver);
415 ap_driver_unregister(&zcrypt_cex4_card_driver);
418 module_init(zcrypt_cex4_init);
419 module_exit(zcrypt_cex4_exit);