1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright IBM Corp. 2001, 2018
4 * Author(s): Robert Burroughs
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/err.h>
16 #include <linux/delay.h>
17 #include <linux/slab.h>
18 #include <linux/atomic.h>
19 #include <linux/uaccess.h>
20 #include <linux/mod_devicetable.h>
23 #include "zcrypt_api.h"
24 #include "zcrypt_error.h"
25 #include "zcrypt_msgtype6.h"
26 #include "zcrypt_cex2c.h"
27 #include "zcrypt_cca_key.h"
29 #define CEX2C_MIN_MOD_SIZE 16 /* 128 bits */
30 #define CEX2C_MAX_MOD_SIZE 256 /* 2048 bits */
31 #define CEX3C_MIN_MOD_SIZE 16 /* 128 bits */
32 #define CEX3C_MAX_MOD_SIZE 512 /* 4096 bits */
33 #define CEX2C_MAX_XCRB_MESSAGE_SIZE (12*1024)
34 #define CEX2C_CLEANUP_TIME (15*HZ)
36 MODULE_AUTHOR("IBM Corporation");
37 MODULE_DESCRIPTION("CEX2C/CEX3C Cryptographic Coprocessor device driver, " \
38 "Copyright IBM Corp. 2001, 2018");
39 MODULE_LICENSE("GPL");
41 static struct ap_device_id zcrypt_cex2c_card_ids[] = {
42 { .dev_type = AP_DEVICE_TYPE_CEX2C,
43 .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
44 { .dev_type = AP_DEVICE_TYPE_CEX3C,
45 .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
46 { /* end of list */ },
49 MODULE_DEVICE_TABLE(ap, zcrypt_cex2c_card_ids);
51 static struct ap_device_id zcrypt_cex2c_queue_ids[] = {
52 { .dev_type = AP_DEVICE_TYPE_CEX2C,
53 .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
54 { .dev_type = AP_DEVICE_TYPE_CEX3C,
55 .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
56 { /* end of list */ },
59 MODULE_DEVICE_TABLE(ap, zcrypt_cex2c_queue_ids);
62 * Large random number detection function. Its sends a message to a CEX2C/CEX3C
63 * card to find out if large random numbers are supported.
64 * @ap_dev: pointer to the AP device.
66 * Returns 1 if large random numbers are supported, 0 if not and < 0 on error.
68 static int zcrypt_cex2c_rng_supported(struct ap_queue *aq)
70 struct ap_message ap_msg;
71 unsigned long long psmid;
74 struct type86_hdr hdr;
75 struct type86_fmt2_ext fmt2;
81 char function_code[2];
82 short int rule_length;
84 short int verb_length;
89 ap_init_message(&ap_msg);
90 ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
94 rng_type6CPRB_msgX(&ap_msg, 4, &domain);
97 msg->cprbx.domain = AP_QID_QUEUE(aq->qid);
99 rc = ap_send(aq->qid, 0x0102030405060708ULL, ap_msg.message,
104 /* Wait for the test message to complete. */
105 for (i = 0; i < 2 * HZ; i++) {
107 rc = ap_recv(aq->qid, &psmid, ap_msg.message, 4096);
108 if (rc == 0 && psmid == 0x0102030405060708ULL)
118 reply = ap_msg.message;
119 if (reply->cprbx.ccp_rtcode == 0 && reply->cprbx.ccp_rscode == 0)
124 free_page((unsigned long) ap_msg.message);
129 * Probe function for CEX2C/CEX3C card devices. It always accepts the
130 * AP device since the bus_match already checked the hardware type.
131 * @ap_dev: pointer to the AP card device.
133 static int zcrypt_cex2c_card_probe(struct ap_device *ap_dev)
136 * Normalized speed ratings per crypto adapter
137 * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
139 static const int CEX2C_SPEED_IDX[] = {
140 1000, 1400, 2400, 1100, 1500, 2600, 100, 12};
141 static const int CEX3C_SPEED_IDX[] = {
142 500, 700, 1400, 550, 800, 1500, 80, 10};
144 struct ap_card *ac = to_ap_card(&ap_dev->device);
145 struct zcrypt_card *zc;
148 zc = zcrypt_card_alloc();
153 switch (ac->ap_dev.device_type) {
154 case AP_DEVICE_TYPE_CEX2C:
155 zc->user_space_type = ZCRYPT_CEX2C;
156 zc->type_string = "CEX2C";
157 memcpy(zc->speed_rating, CEX2C_SPEED_IDX,
158 sizeof(CEX2C_SPEED_IDX));
159 zc->min_mod_size = CEX2C_MIN_MOD_SIZE;
160 zc->max_mod_size = CEX2C_MAX_MOD_SIZE;
161 zc->max_exp_bit_length = CEX2C_MAX_MOD_SIZE;
163 case AP_DEVICE_TYPE_CEX3C:
164 zc->user_space_type = ZCRYPT_CEX3C;
165 zc->type_string = "CEX3C";
166 memcpy(zc->speed_rating, CEX3C_SPEED_IDX,
167 sizeof(CEX3C_SPEED_IDX));
168 zc->min_mod_size = CEX3C_MIN_MOD_SIZE;
169 zc->max_mod_size = CEX3C_MAX_MOD_SIZE;
170 zc->max_exp_bit_length = CEX3C_MAX_MOD_SIZE;
173 zcrypt_card_free(zc);
178 rc = zcrypt_card_register(zc);
181 zcrypt_card_free(zc);
188 * This is called to remove the CEX2C/CEX3C card driver information
189 * if an AP card device is removed.
191 static void zcrypt_cex2c_card_remove(struct ap_device *ap_dev)
193 struct zcrypt_card *zc = to_ap_card(&ap_dev->device)->private;
196 zcrypt_card_unregister(zc);
199 static struct ap_driver zcrypt_cex2c_card_driver = {
200 .probe = zcrypt_cex2c_card_probe,
201 .remove = zcrypt_cex2c_card_remove,
202 .ids = zcrypt_cex2c_card_ids,
203 .flags = AP_DRIVER_FLAG_DEFAULT,
207 * Probe function for CEX2C/CEX3C queue devices. It always accepts the
208 * AP device since the bus_match already checked the hardware type.
209 * @ap_dev: pointer to the AP card device.
211 static int zcrypt_cex2c_queue_probe(struct ap_device *ap_dev)
213 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
214 struct zcrypt_queue *zq;
217 zq = zcrypt_queue_alloc(CEX2C_MAX_XCRB_MESSAGE_SIZE);
222 atomic_set(&zq->load, 0);
223 rc = zcrypt_cex2c_rng_supported(aq);
225 zcrypt_queue_free(zq);
229 zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
230 MSGTYPE06_VARIANT_DEFAULT);
232 zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
233 MSGTYPE06_VARIANT_NORNG);
234 ap_queue_init_reply(aq, &zq->reply);
235 aq->request_timeout = CEX2C_CLEANUP_TIME;
237 rc = zcrypt_queue_register(zq);
240 zcrypt_queue_free(zq);
246 * This is called to remove the CEX2C/CEX3C queue driver information
247 * if an AP queue device is removed.
249 static void zcrypt_cex2c_queue_remove(struct ap_device *ap_dev)
251 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
252 struct zcrypt_queue *zq = aq->private;
255 zcrypt_queue_unregister(zq);
258 static struct ap_driver zcrypt_cex2c_queue_driver = {
259 .probe = zcrypt_cex2c_queue_probe,
260 .remove = zcrypt_cex2c_queue_remove,
261 .suspend = ap_queue_suspend,
262 .resume = ap_queue_resume,
263 .ids = zcrypt_cex2c_queue_ids,
264 .flags = AP_DRIVER_FLAG_DEFAULT,
267 int __init zcrypt_cex2c_init(void)
271 rc = ap_driver_register(&zcrypt_cex2c_card_driver,
272 THIS_MODULE, "cex2card");
276 rc = ap_driver_register(&zcrypt_cex2c_queue_driver,
277 THIS_MODULE, "cex2cqueue");
279 ap_driver_unregister(&zcrypt_cex2c_card_driver);
284 void zcrypt_cex2c_exit(void)
286 ap_driver_unregister(&zcrypt_cex2c_queue_driver);
287 ap_driver_unregister(&zcrypt_cex2c_card_driver);
290 module_init(zcrypt_cex2c_init);
291 module_exit(zcrypt_cex2c_exit);