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 { /* end of list */ },
51 MODULE_DEVICE_TABLE(ap
, zcrypt_cex4_card_ids
);
53 static struct ap_device_id zcrypt_cex4_queue_ids
[] = {
54 { .dev_type
= AP_DEVICE_TYPE_CEX4
,
55 .match_flags
= AP_DEVICE_ID_MATCH_QUEUE_TYPE
},
56 { .dev_type
= AP_DEVICE_TYPE_CEX5
,
57 .match_flags
= AP_DEVICE_ID_MATCH_QUEUE_TYPE
},
58 { /* end of list */ },
61 MODULE_DEVICE_TABLE(ap
, zcrypt_cex4_queue_ids
);
64 * Probe function for CEX4 card device. It always accepts the AP device
65 * since the bus_match already checked the hardware type.
66 * @ap_dev: pointer to the AP device.
68 static int zcrypt_cex4_card_probe(struct ap_device
*ap_dev
)
71 * Normalized speed ratings per crypto adapter
72 * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
74 static const int CEX4A_SPEED_IDX
[] = {
75 5, 6, 59, 20, 115, 581, 0, 0};
76 static const int CEX5A_SPEED_IDX
[] = {
77 3, 3, 6, 8, 32, 218, 0, 0};
78 static const int CEX4C_SPEED_IDX
[] = {
79 24, 25, 82, 41, 138, 1111, 79, 8};
80 static const int CEX5C_SPEED_IDX
[] = {
81 10, 14, 23, 17, 45, 242, 63, 4};
82 static const int CEX4P_SPEED_IDX
[] = {
83 142, 198, 1852, 203, 331, 1563, 0, 8};
84 static const int CEX5P_SPEED_IDX
[] = {
85 49, 67, 131, 52, 85, 287, 0, 4};
87 struct ap_card
*ac
= to_ap_card(&ap_dev
->device
);
88 struct zcrypt_card
*zc
;
91 zc
= zcrypt_card_alloc();
96 if (ap_test_bit(&ac
->functions
, AP_FUNC_ACCEL
)) {
97 if (ac
->ap_dev
.device_type
== AP_DEVICE_TYPE_CEX4
) {
98 zc
->type_string
= "CEX4A";
99 zc
->user_space_type
= ZCRYPT_CEX4
;
100 memcpy(zc
->speed_rating
, CEX4A_SPEED_IDX
,
101 sizeof(CEX4A_SPEED_IDX
));
103 zc
->type_string
= "CEX5A";
104 zc
->user_space_type
= ZCRYPT_CEX5
;
105 memcpy(zc
->speed_rating
, CEX5A_SPEED_IDX
,
106 sizeof(CEX5A_SPEED_IDX
));
108 zc
->min_mod_size
= CEX4A_MIN_MOD_SIZE
;
109 if (ap_test_bit(&ac
->functions
, AP_FUNC_MEX4K
) &&
110 ap_test_bit(&ac
->functions
, AP_FUNC_CRT4K
)) {
111 zc
->max_mod_size
= CEX4A_MAX_MOD_SIZE_4K
;
112 zc
->max_exp_bit_length
=
113 CEX4A_MAX_MOD_SIZE_4K
;
115 zc
->max_mod_size
= CEX4A_MAX_MOD_SIZE_2K
;
116 zc
->max_exp_bit_length
=
117 CEX4A_MAX_MOD_SIZE_2K
;
119 } else if (ap_test_bit(&ac
->functions
, AP_FUNC_COPRO
)) {
120 if (ac
->ap_dev
.device_type
== AP_DEVICE_TYPE_CEX4
) {
121 zc
->type_string
= "CEX4C";
122 /* wrong user space type, must be CEX4
123 * just keep it for cca compatibility
125 zc
->user_space_type
= ZCRYPT_CEX3C
;
126 memcpy(zc
->speed_rating
, CEX4C_SPEED_IDX
,
127 sizeof(CEX4C_SPEED_IDX
));
129 zc
->type_string
= "CEX5C";
130 /* wrong user space type, must be CEX5
131 * just keep it for cca compatibility
133 zc
->user_space_type
= ZCRYPT_CEX3C
;
134 memcpy(zc
->speed_rating
, CEX5C_SPEED_IDX
,
135 sizeof(CEX5C_SPEED_IDX
));
137 zc
->min_mod_size
= CEX4C_MIN_MOD_SIZE
;
138 zc
->max_mod_size
= CEX4C_MAX_MOD_SIZE
;
139 zc
->max_exp_bit_length
= CEX4C_MAX_MOD_SIZE
;
140 } else if (ap_test_bit(&ac
->functions
, AP_FUNC_EP11
)) {
141 if (ac
->ap_dev
.device_type
== AP_DEVICE_TYPE_CEX4
) {
142 zc
->type_string
= "CEX4P";
143 zc
->user_space_type
= ZCRYPT_CEX4
;
144 memcpy(zc
->speed_rating
, CEX4P_SPEED_IDX
,
145 sizeof(CEX4P_SPEED_IDX
));
147 zc
->type_string
= "CEX5P";
148 zc
->user_space_type
= ZCRYPT_CEX5
;
149 memcpy(zc
->speed_rating
, CEX5P_SPEED_IDX
,
150 sizeof(CEX5P_SPEED_IDX
));
152 zc
->min_mod_size
= CEX4C_MIN_MOD_SIZE
;
153 zc
->max_mod_size
= CEX4C_MAX_MOD_SIZE
;
154 zc
->max_exp_bit_length
= CEX4C_MAX_MOD_SIZE
;
156 zcrypt_card_free(zc
);
161 rc
= zcrypt_card_register(zc
);
164 zcrypt_card_free(zc
);
171 * This is called to remove the CEX4 card driver information
172 * if an AP card device is removed.
174 static void zcrypt_cex4_card_remove(struct ap_device
*ap_dev
)
176 struct zcrypt_card
*zc
= to_ap_card(&ap_dev
->device
)->private;
179 zcrypt_card_unregister(zc
);
182 static struct ap_driver zcrypt_cex4_card_driver
= {
183 .probe
= zcrypt_cex4_card_probe
,
184 .remove
= zcrypt_cex4_card_remove
,
185 .ids
= zcrypt_cex4_card_ids
,
189 * Probe function for CEX4 queue device. It always accepts the AP device
190 * since the bus_match already checked the hardware type.
191 * @ap_dev: pointer to the AP device.
193 static int zcrypt_cex4_queue_probe(struct ap_device
*ap_dev
)
195 struct ap_queue
*aq
= to_ap_queue(&ap_dev
->device
);
196 struct zcrypt_queue
*zq
;
199 if (ap_test_bit(&aq
->card
->functions
, AP_FUNC_ACCEL
)) {
200 zq
= zcrypt_queue_alloc(CEX4A_MAX_MESSAGE_SIZE
);
203 zq
->ops
= zcrypt_msgtype(MSGTYPE50_NAME
,
204 MSGTYPE50_VARIANT_DEFAULT
);
205 } else if (ap_test_bit(&aq
->card
->functions
, AP_FUNC_COPRO
)) {
206 zq
= zcrypt_queue_alloc(CEX4C_MAX_MESSAGE_SIZE
);
209 zq
->ops
= zcrypt_msgtype(MSGTYPE06_NAME
,
210 MSGTYPE06_VARIANT_DEFAULT
);
211 } else if (ap_test_bit(&aq
->card
->functions
, AP_FUNC_EP11
)) {
212 zq
= zcrypt_queue_alloc(CEX4C_MAX_MESSAGE_SIZE
);
215 zq
->ops
= zcrypt_msgtype(MSGTYPE06_NAME
,
216 MSGTYPE06_VARIANT_EP11
);
222 atomic_set(&zq
->load
, 0);
223 ap_queue_init_reply(aq
, &zq
->reply
);
224 aq
->request_timeout
= CEX4_CLEANUP_TIME
,
226 rc
= zcrypt_queue_register(zq
);
229 zcrypt_queue_free(zq
);
236 * This is called to remove the CEX4 queue driver information
237 * if an AP queue device is removed.
239 static void zcrypt_cex4_queue_remove(struct ap_device
*ap_dev
)
241 struct ap_queue
*aq
= to_ap_queue(&ap_dev
->device
);
242 struct zcrypt_queue
*zq
= aq
->private;
246 zcrypt_queue_unregister(zq
);
249 static struct ap_driver zcrypt_cex4_queue_driver
= {
250 .probe
= zcrypt_cex4_queue_probe
,
251 .remove
= zcrypt_cex4_queue_remove
,
252 .suspend
= ap_queue_suspend
,
253 .resume
= ap_queue_resume
,
254 .ids
= zcrypt_cex4_queue_ids
,
257 int __init
zcrypt_cex4_init(void)
261 rc
= ap_driver_register(&zcrypt_cex4_card_driver
,
262 THIS_MODULE
, "cex4card");
266 rc
= ap_driver_register(&zcrypt_cex4_queue_driver
,
267 THIS_MODULE
, "cex4queue");
269 ap_driver_unregister(&zcrypt_cex4_card_driver
);
274 void __exit
zcrypt_cex4_exit(void)
276 ap_driver_unregister(&zcrypt_cex4_queue_driver
);
277 ap_driver_unregister(&zcrypt_cex4_card_driver
);
280 module_init(zcrypt_cex4_init
);
281 module_exit(zcrypt_cex4_exit
);