1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corp. 2012
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"
22 #define CEX4A_MIN_MOD_SIZE 1 /* 8 bits */
23 #define CEX4A_MAX_MOD_SIZE_2K 256 /* 2048 bits */
24 #define CEX4A_MAX_MOD_SIZE_4K 512 /* 4096 bits */
26 #define CEX4C_MIN_MOD_SIZE 16 /* 256 bits */
27 #define CEX4C_MAX_MOD_SIZE 512 /* 4096 bits */
29 #define CEX4A_MAX_MESSAGE_SIZE MSGTYPE50_CRB3_MAX_MSG_SIZE
30 #define CEX4C_MAX_MESSAGE_SIZE MSGTYPE06_MAX_MSG_SIZE
32 /* Waiting time for requests to be processed.
33 * Currently there are some types of request which are not deterministic.
34 * But the maximum time limit managed by the stomper code is set to 60sec.
35 * Hence we have to wait at least that time period.
37 #define CEX4_CLEANUP_TIME (900*HZ)
39 MODULE_AUTHOR("IBM Corporation");
40 MODULE_DESCRIPTION("CEX4/CEX5/CEX6 Cryptographic Card device driver, " \
41 "Copyright IBM Corp. 2018");
42 MODULE_LICENSE("GPL");
44 static struct ap_device_id zcrypt_cex4_card_ids[] = {
45 { .dev_type = AP_DEVICE_TYPE_CEX4,
46 .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
47 { .dev_type = AP_DEVICE_TYPE_CEX5,
48 .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
49 { .dev_type = AP_DEVICE_TYPE_CEX6,
50 .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
51 { /* end of list */ },
54 MODULE_DEVICE_TABLE(ap, zcrypt_cex4_card_ids);
56 static struct ap_device_id zcrypt_cex4_queue_ids[] = {
57 { .dev_type = AP_DEVICE_TYPE_CEX4,
58 .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
59 { .dev_type = AP_DEVICE_TYPE_CEX5,
60 .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
61 { .dev_type = AP_DEVICE_TYPE_CEX6,
62 .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
63 { /* end of list */ },
66 MODULE_DEVICE_TABLE(ap, zcrypt_cex4_queue_ids);
69 * Probe function for CEX4/CEX5/CEX6 card device. It always
70 * accepts the AP device since the bus_match already checked
72 * @ap_dev: pointer to the AP device.
74 static int zcrypt_cex4_card_probe(struct ap_device *ap_dev)
77 * Normalized speed ratings per crypto adapter
78 * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
80 static const int CEX4A_SPEED_IDX[] = {
81 14, 19, 249, 42, 228, 1458, 0, 0};
82 static const int CEX5A_SPEED_IDX[] = {
83 8, 9, 20, 18, 66, 458, 0, 0};
84 static const int CEX6A_SPEED_IDX[] = {
85 6, 9, 20, 17, 65, 438, 0, 0};
87 static const int CEX4C_SPEED_IDX[] = {
88 59, 69, 308, 83, 278, 2204, 209, 40};
89 static const int CEX5C_SPEED_IDX[] = {
90 24, 31, 50, 37, 90, 479, 27, 10};
91 static const int CEX6C_SPEED_IDX[] = {
92 16, 20, 32, 27, 77, 455, 23, 9};
94 static const int CEX4P_SPEED_IDX[] = {
95 224, 313, 3560, 359, 605, 2827, 0, 50};
96 static const int CEX5P_SPEED_IDX[] = {
97 63, 84, 156, 83, 142, 533, 0, 10};
98 static const int CEX6P_SPEED_IDX[] = {
99 55, 70, 121, 73, 129, 522, 0, 9};
101 struct ap_card *ac = to_ap_card(&ap_dev->device);
102 struct zcrypt_card *zc;
105 zc = zcrypt_card_alloc();
110 if (ap_test_bit(&ac->functions, AP_FUNC_ACCEL)) {
111 if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
112 zc->type_string = "CEX4A";
113 zc->user_space_type = ZCRYPT_CEX4;
114 memcpy(zc->speed_rating, CEX4A_SPEED_IDX,
115 sizeof(CEX4A_SPEED_IDX));
116 } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) {
117 zc->type_string = "CEX5A";
118 zc->user_space_type = ZCRYPT_CEX5;
119 memcpy(zc->speed_rating, CEX5A_SPEED_IDX,
120 sizeof(CEX5A_SPEED_IDX));
122 zc->type_string = "CEX6A";
123 zc->user_space_type = ZCRYPT_CEX6;
124 memcpy(zc->speed_rating, CEX6A_SPEED_IDX,
125 sizeof(CEX6A_SPEED_IDX));
127 zc->min_mod_size = CEX4A_MIN_MOD_SIZE;
128 if (ap_test_bit(&ac->functions, AP_FUNC_MEX4K) &&
129 ap_test_bit(&ac->functions, AP_FUNC_CRT4K)) {
130 zc->max_mod_size = CEX4A_MAX_MOD_SIZE_4K;
131 zc->max_exp_bit_length =
132 CEX4A_MAX_MOD_SIZE_4K;
134 zc->max_mod_size = CEX4A_MAX_MOD_SIZE_2K;
135 zc->max_exp_bit_length =
136 CEX4A_MAX_MOD_SIZE_2K;
138 } else if (ap_test_bit(&ac->functions, AP_FUNC_COPRO)) {
139 if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
140 zc->type_string = "CEX4C";
141 /* wrong user space type, must be CEX4
142 * just keep it for cca compatibility
144 zc->user_space_type = ZCRYPT_CEX3C;
145 memcpy(zc->speed_rating, CEX4C_SPEED_IDX,
146 sizeof(CEX4C_SPEED_IDX));
147 } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) {
148 zc->type_string = "CEX5C";
149 /* wrong user space type, must be CEX5
150 * just keep it for cca compatibility
152 zc->user_space_type = ZCRYPT_CEX3C;
153 memcpy(zc->speed_rating, CEX5C_SPEED_IDX,
154 sizeof(CEX5C_SPEED_IDX));
156 zc->type_string = "CEX6C";
157 /* wrong user space type, must be CEX6
158 * just keep it for cca compatibility
160 zc->user_space_type = ZCRYPT_CEX3C;
161 memcpy(zc->speed_rating, CEX6C_SPEED_IDX,
162 sizeof(CEX6C_SPEED_IDX));
164 zc->min_mod_size = CEX4C_MIN_MOD_SIZE;
165 zc->max_mod_size = CEX4C_MAX_MOD_SIZE;
166 zc->max_exp_bit_length = CEX4C_MAX_MOD_SIZE;
167 } else if (ap_test_bit(&ac->functions, AP_FUNC_EP11)) {
168 if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
169 zc->type_string = "CEX4P";
170 zc->user_space_type = ZCRYPT_CEX4;
171 memcpy(zc->speed_rating, CEX4P_SPEED_IDX,
172 sizeof(CEX4P_SPEED_IDX));
173 } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) {
174 zc->type_string = "CEX5P";
175 zc->user_space_type = ZCRYPT_CEX5;
176 memcpy(zc->speed_rating, CEX5P_SPEED_IDX,
177 sizeof(CEX5P_SPEED_IDX));
179 zc->type_string = "CEX6P";
180 zc->user_space_type = ZCRYPT_CEX6;
181 memcpy(zc->speed_rating, CEX6P_SPEED_IDX,
182 sizeof(CEX6P_SPEED_IDX));
184 zc->min_mod_size = CEX4C_MIN_MOD_SIZE;
185 zc->max_mod_size = CEX4C_MAX_MOD_SIZE;
186 zc->max_exp_bit_length = CEX4C_MAX_MOD_SIZE;
188 zcrypt_card_free(zc);
193 rc = zcrypt_card_register(zc);
196 zcrypt_card_free(zc);
203 * This is called to remove the CEX4/CEX5/CEX6 card driver information
204 * if an AP card device is removed.
206 static void zcrypt_cex4_card_remove(struct ap_device *ap_dev)
208 struct zcrypt_card *zc = to_ap_card(&ap_dev->device)->private;
211 zcrypt_card_unregister(zc);
214 static struct ap_driver zcrypt_cex4_card_driver = {
215 .probe = zcrypt_cex4_card_probe,
216 .remove = zcrypt_cex4_card_remove,
217 .ids = zcrypt_cex4_card_ids,
218 .flags = AP_DRIVER_FLAG_DEFAULT,
222 * Probe function for CEX4/CEX5/CEX6 queue device. It always
223 * accepts the AP device since the bus_match already checked
225 * @ap_dev: pointer to the AP device.
227 static int zcrypt_cex4_queue_probe(struct ap_device *ap_dev)
229 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
230 struct zcrypt_queue *zq;
233 if (ap_test_bit(&aq->card->functions, AP_FUNC_ACCEL)) {
234 zq = zcrypt_queue_alloc(CEX4A_MAX_MESSAGE_SIZE);
237 zq->ops = zcrypt_msgtype(MSGTYPE50_NAME,
238 MSGTYPE50_VARIANT_DEFAULT);
239 } else if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO)) {
240 zq = zcrypt_queue_alloc(CEX4C_MAX_MESSAGE_SIZE);
243 zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
244 MSGTYPE06_VARIANT_DEFAULT);
245 } else if (ap_test_bit(&aq->card->functions, AP_FUNC_EP11)) {
246 zq = zcrypt_queue_alloc(CEX4C_MAX_MESSAGE_SIZE);
249 zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
250 MSGTYPE06_VARIANT_EP11);
256 atomic_set(&zq->load, 0);
257 ap_queue_init_reply(aq, &zq->reply);
258 aq->request_timeout = CEX4_CLEANUP_TIME,
260 rc = zcrypt_queue_register(zq);
263 zcrypt_queue_free(zq);
270 * This is called to remove the CEX4/CEX5/CEX6 queue driver
271 * information if an AP queue device is removed.
273 static void zcrypt_cex4_queue_remove(struct ap_device *ap_dev)
275 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
276 struct zcrypt_queue *zq = aq->private;
280 zcrypt_queue_unregister(zq);
283 static struct ap_driver zcrypt_cex4_queue_driver = {
284 .probe = zcrypt_cex4_queue_probe,
285 .remove = zcrypt_cex4_queue_remove,
286 .suspend = ap_queue_suspend,
287 .resume = ap_queue_resume,
288 .ids = zcrypt_cex4_queue_ids,
289 .flags = AP_DRIVER_FLAG_DEFAULT,
292 int __init zcrypt_cex4_init(void)
296 rc = ap_driver_register(&zcrypt_cex4_card_driver,
297 THIS_MODULE, "cex4card");
301 rc = ap_driver_register(&zcrypt_cex4_queue_driver,
302 THIS_MODULE, "cex4queue");
304 ap_driver_unregister(&zcrypt_cex4_card_driver);
309 void __exit zcrypt_cex4_exit(void)
311 ap_driver_unregister(&zcrypt_cex4_queue_driver);
312 ap_driver_unregister(&zcrypt_cex4_card_driver);
315 module_init(zcrypt_cex4_init);
316 module_exit(zcrypt_cex4_exit);