1 // SPDX-License-Identifier: GPL-2.0+
5 * Copyright IBM Corp. 2001, 2012
6 * Author(s): Robert Burroughs
7 * Eric Rossman (edrossma@us.ibm.com)
9 * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
10 * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
11 * Ralph Wuerthner <rwuerthn@de.ibm.com>
12 * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/init.h>
18 #include <linux/err.h>
19 #include <linux/atomic.h>
20 #include <linux/uaccess.h>
21 #include <linux/mod_devicetable.h>
24 #include "zcrypt_api.h"
25 #include "zcrypt_error.h"
26 #include "zcrypt_cex2a.h"
27 #include "zcrypt_msgtype50.h"
29 #define CEX2A_MIN_MOD_SIZE 1 /* 8 bits */
30 #define CEX2A_MAX_MOD_SIZE 256 /* 2048 bits */
31 #define CEX3A_MIN_MOD_SIZE CEX2A_MIN_MOD_SIZE
32 #define CEX3A_MAX_MOD_SIZE 512 /* 4096 bits */
34 #define CEX2A_MAX_MESSAGE_SIZE 0x390 /* sizeof(struct type50_crb2_msg) */
35 #define CEX2A_MAX_RESPONSE_SIZE 0x110 /* max outputdatalength + type80_hdr */
37 #define CEX3A_MAX_RESPONSE_SIZE 0x210 /* 512 bit modulus
38 * (max outputdatalength) +
40 #define CEX3A_MAX_MESSAGE_SIZE sizeof(struct type50_crb3_msg)
42 #define CEX2A_CLEANUP_TIME (15*HZ)
43 #define CEX3A_CLEANUP_TIME CEX2A_CLEANUP_TIME
45 MODULE_AUTHOR("IBM Corporation");
46 MODULE_DESCRIPTION("CEX2A Cryptographic Coprocessor device driver, " \
47 "Copyright IBM Corp. 2001, 2012");
48 MODULE_LICENSE("GPL");
50 static struct ap_device_id zcrypt_cex2a_card_ids
[] = {
51 { .dev_type
= AP_DEVICE_TYPE_CEX2A
,
52 .match_flags
= AP_DEVICE_ID_MATCH_CARD_TYPE
},
53 { .dev_type
= AP_DEVICE_TYPE_CEX3A
,
54 .match_flags
= AP_DEVICE_ID_MATCH_CARD_TYPE
},
55 { /* end of list */ },
58 MODULE_DEVICE_TABLE(ap
, zcrypt_cex2a_card_ids
);
60 static struct ap_device_id zcrypt_cex2a_queue_ids
[] = {
61 { .dev_type
= AP_DEVICE_TYPE_CEX2A
,
62 .match_flags
= AP_DEVICE_ID_MATCH_QUEUE_TYPE
},
63 { .dev_type
= AP_DEVICE_TYPE_CEX3A
,
64 .match_flags
= AP_DEVICE_ID_MATCH_QUEUE_TYPE
},
65 { /* end of list */ },
68 MODULE_DEVICE_TABLE(ap
, zcrypt_cex2a_queue_ids
);
71 * Probe function for CEX2A card devices. It always accepts the AP device
72 * since the bus_match already checked the card type.
73 * @ap_dev: pointer to the AP device.
75 static int zcrypt_cex2a_card_probe(struct ap_device
*ap_dev
)
78 * Normalized speed ratings per crypto adapter
79 * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
81 static const int CEX2A_SPEED_IDX
[] = {
82 800, 1000, 2000, 900, 1200, 2400, 0, 0};
83 static const int CEX3A_SPEED_IDX
[] = {
84 400, 500, 1000, 450, 550, 1200, 0, 0};
86 struct ap_card
*ac
= to_ap_card(&ap_dev
->device
);
87 struct zcrypt_card
*zc
;
90 zc
= zcrypt_card_alloc();
96 if (ac
->ap_dev
.device_type
== AP_DEVICE_TYPE_CEX2A
) {
97 zc
->min_mod_size
= CEX2A_MIN_MOD_SIZE
;
98 zc
->max_mod_size
= CEX2A_MAX_MOD_SIZE
;
99 memcpy(zc
->speed_rating
, CEX2A_SPEED_IDX
,
100 sizeof(CEX2A_SPEED_IDX
));
101 zc
->max_exp_bit_length
= CEX2A_MAX_MOD_SIZE
;
102 zc
->type_string
= "CEX2A";
103 zc
->user_space_type
= ZCRYPT_CEX2A
;
104 } else if (ac
->ap_dev
.device_type
== AP_DEVICE_TYPE_CEX3A
) {
105 zc
->min_mod_size
= CEX2A_MIN_MOD_SIZE
;
106 zc
->max_mod_size
= CEX2A_MAX_MOD_SIZE
;
107 zc
->max_exp_bit_length
= CEX2A_MAX_MOD_SIZE
;
108 if (ap_test_bit(&ac
->functions
, AP_FUNC_MEX4K
) &&
109 ap_test_bit(&ac
->functions
, AP_FUNC_CRT4K
)) {
110 zc
->max_mod_size
= CEX3A_MAX_MOD_SIZE
;
111 zc
->max_exp_bit_length
= CEX3A_MAX_MOD_SIZE
;
113 memcpy(zc
->speed_rating
, CEX3A_SPEED_IDX
,
114 sizeof(CEX3A_SPEED_IDX
));
115 zc
->type_string
= "CEX3A";
116 zc
->user_space_type
= ZCRYPT_CEX3A
;
118 zcrypt_card_free(zc
);
123 rc
= zcrypt_card_register(zc
);
126 zcrypt_card_free(zc
);
133 * This is called to remove the CEX2A card driver information
134 * if an AP card device is removed.
136 static void zcrypt_cex2a_card_remove(struct ap_device
*ap_dev
)
138 struct zcrypt_card
*zc
= to_ap_card(&ap_dev
->device
)->private;
141 zcrypt_card_unregister(zc
);
144 static struct ap_driver zcrypt_cex2a_card_driver
= {
145 .probe
= zcrypt_cex2a_card_probe
,
146 .remove
= zcrypt_cex2a_card_remove
,
147 .ids
= zcrypt_cex2a_card_ids
,
148 .flags
= AP_DRIVER_FLAG_DEFAULT
,
152 * Probe function for CEX2A queue devices. It always accepts the AP device
153 * since the bus_match already checked the queue type.
154 * @ap_dev: pointer to the AP device.
156 static int zcrypt_cex2a_queue_probe(struct ap_device
*ap_dev
)
158 struct ap_queue
*aq
= to_ap_queue(&ap_dev
->device
);
159 struct zcrypt_queue
*zq
= NULL
;
162 switch (ap_dev
->device_type
) {
163 case AP_DEVICE_TYPE_CEX2A
:
164 zq
= zcrypt_queue_alloc(CEX2A_MAX_RESPONSE_SIZE
);
168 case AP_DEVICE_TYPE_CEX3A
:
169 zq
= zcrypt_queue_alloc(CEX3A_MAX_RESPONSE_SIZE
);
176 zq
->ops
= zcrypt_msgtype(MSGTYPE50_NAME
, MSGTYPE50_VARIANT_DEFAULT
);
179 atomic_set(&zq
->load
, 0);
180 ap_queue_init_reply(aq
, &zq
->reply
);
181 aq
->request_timeout
= CEX2A_CLEANUP_TIME
,
183 rc
= zcrypt_queue_register(zq
);
186 zcrypt_queue_free(zq
);
193 * This is called to remove the CEX2A queue driver information
194 * if an AP queue device is removed.
196 static void zcrypt_cex2a_queue_remove(struct ap_device
*ap_dev
)
198 struct ap_queue
*aq
= to_ap_queue(&ap_dev
->device
);
199 struct zcrypt_queue
*zq
= aq
->private;
202 zcrypt_queue_unregister(zq
);
205 static struct ap_driver zcrypt_cex2a_queue_driver
= {
206 .probe
= zcrypt_cex2a_queue_probe
,
207 .remove
= zcrypt_cex2a_queue_remove
,
208 .suspend
= ap_queue_suspend
,
209 .resume
= ap_queue_resume
,
210 .ids
= zcrypt_cex2a_queue_ids
,
211 .flags
= AP_DRIVER_FLAG_DEFAULT
,
214 int __init
zcrypt_cex2a_init(void)
218 rc
= ap_driver_register(&zcrypt_cex2a_card_driver
,
219 THIS_MODULE
, "cex2acard");
223 rc
= ap_driver_register(&zcrypt_cex2a_queue_driver
,
224 THIS_MODULE
, "cex2aqueue");
226 ap_driver_unregister(&zcrypt_cex2a_card_driver
);
231 void __exit
zcrypt_cex2a_exit(void)
233 ap_driver_unregister(&zcrypt_cex2a_queue_driver
);
234 ap_driver_unregister(&zcrypt_cex2a_card_driver
);
237 module_init(zcrypt_cex2a_init
);
238 module_exit(zcrypt_cex2a_exit
);