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/init.h>
17 #include <linux/err.h>
18 #include <linux/delay.h>
19 #include <linux/slab.h>
20 #include <linux/atomic.h>
21 #include <linux/uaccess.h>
22 #include <linux/mod_devicetable.h>
25 #include "zcrypt_api.h"
26 #include "zcrypt_error.h"
27 #include "zcrypt_msgtype6.h"
28 #include "zcrypt_pcixcc.h"
29 #include "zcrypt_cca_key.h"
31 #define PCIXCC_MIN_MOD_SIZE 16 /* 128 bits */
32 #define PCIXCC_MIN_MOD_SIZE_OLD 64 /* 512 bits */
33 #define PCIXCC_MAX_MOD_SIZE 256 /* 2048 bits */
34 #define CEX3C_MIN_MOD_SIZE PCIXCC_MIN_MOD_SIZE
35 #define CEX3C_MAX_MOD_SIZE 512 /* 4096 bits */
37 #define PCIXCC_MAX_ICA_MESSAGE_SIZE 0x77c /* max size type6 v2 crt message */
38 #define PCIXCC_MAX_ICA_RESPONSE_SIZE 0x77c /* max size type86 v2 reply */
40 #define PCIXCC_MAX_XCRB_MESSAGE_SIZE (12*1024)
42 #define PCIXCC_CLEANUP_TIME (15*HZ)
44 #define CEIL4(x) ((((x)+3)/4)*4)
46 struct response_type
{
47 struct completion work
;
50 #define PCIXCC_RESPONSE_TYPE_ICA 0
51 #define PCIXCC_RESPONSE_TYPE_XCRB 1
53 MODULE_AUTHOR("IBM Corporation");
54 MODULE_DESCRIPTION("PCIXCC Cryptographic Coprocessor device driver, " \
55 "Copyright IBM Corp. 2001, 2012");
56 MODULE_LICENSE("GPL");
58 static struct ap_device_id zcrypt_pcixcc_card_ids
[] = {
59 { .dev_type
= AP_DEVICE_TYPE_PCIXCC
,
60 .match_flags
= AP_DEVICE_ID_MATCH_CARD_TYPE
},
61 { .dev_type
= AP_DEVICE_TYPE_CEX2C
,
62 .match_flags
= AP_DEVICE_ID_MATCH_CARD_TYPE
},
63 { .dev_type
= AP_DEVICE_TYPE_CEX3C
,
64 .match_flags
= AP_DEVICE_ID_MATCH_CARD_TYPE
},
65 { /* end of list */ },
68 MODULE_DEVICE_TABLE(ap
, zcrypt_pcixcc_card_ids
);
70 static struct ap_device_id zcrypt_pcixcc_queue_ids
[] = {
71 { .dev_type
= AP_DEVICE_TYPE_PCIXCC
,
72 .match_flags
= AP_DEVICE_ID_MATCH_QUEUE_TYPE
},
73 { .dev_type
= AP_DEVICE_TYPE_CEX2C
,
74 .match_flags
= AP_DEVICE_ID_MATCH_QUEUE_TYPE
},
75 { .dev_type
= AP_DEVICE_TYPE_CEX3C
,
76 .match_flags
= AP_DEVICE_ID_MATCH_QUEUE_TYPE
},
77 { /* end of list */ },
80 MODULE_DEVICE_TABLE(ap
, zcrypt_pcixcc_queue_ids
);
83 * Large random number detection function. Its sends a message to a pcixcc
84 * card to find out if large random numbers are supported.
85 * @ap_dev: pointer to the AP device.
87 * Returns 1 if large random numbers are supported, 0 if not and < 0 on error.
89 static int zcrypt_pcixcc_rng_supported(struct ap_queue
*aq
)
91 struct ap_message ap_msg
;
92 unsigned long long psmid
;
95 struct type86_hdr hdr
;
96 struct type86_fmt2_ext fmt2
;
98 } __attribute__((packed
)) *reply
;
100 struct type6_hdr hdr
;
102 char function_code
[2];
103 short int rule_length
;
105 short int verb_length
;
106 short int key_length
;
110 ap_init_message(&ap_msg
);
111 ap_msg
.message
= (void *) get_zeroed_page(GFP_KERNEL
);
115 rng_type6CPRB_msgX(&ap_msg
, 4, &domain
);
117 msg
= ap_msg
.message
;
118 msg
->cprbx
.domain
= AP_QID_QUEUE(aq
->qid
);
120 rc
= ap_send(aq
->qid
, 0x0102030405060708ULL
, ap_msg
.message
,
125 /* Wait for the test message to complete. */
126 for (i
= 0; i
< 2 * HZ
; i
++) {
128 rc
= ap_recv(aq
->qid
, &psmid
, ap_msg
.message
, 4096);
129 if (rc
== 0 && psmid
== 0x0102030405060708ULL
)
139 reply
= ap_msg
.message
;
140 if (reply
->cprbx
.ccp_rtcode
== 0 && reply
->cprbx
.ccp_rscode
== 0)
145 free_page((unsigned long) ap_msg
.message
);
150 * Probe function for PCIXCC/CEX2C card devices. It always accepts the
151 * AP device since the bus_match already checked the hardware type. The
152 * PCIXCC cards come in two flavours: micro code level 2 and micro code
153 * level 3. This is checked by sending a test message to the device.
154 * @ap_dev: pointer to the AP card device.
156 static int zcrypt_pcixcc_card_probe(struct ap_device
*ap_dev
)
159 * Normalized speed ratings per crypto adapter
160 * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
162 static const int CEX2C_SPEED_IDX
[] = {
163 1000, 1400, 2400, 1100, 1500, 2600, 100, 12};
164 static const int CEX3C_SPEED_IDX
[] = {
165 500, 700, 1400, 550, 800, 1500, 80, 10};
167 struct ap_card
*ac
= to_ap_card(&ap_dev
->device
);
168 struct zcrypt_card
*zc
;
171 zc
= zcrypt_card_alloc();
176 switch (ac
->ap_dev
.device_type
) {
177 case AP_DEVICE_TYPE_CEX2C
:
178 zc
->user_space_type
= ZCRYPT_CEX2C
;
179 zc
->type_string
= "CEX2C";
180 memcpy(zc
->speed_rating
, CEX2C_SPEED_IDX
,
181 sizeof(CEX2C_SPEED_IDX
));
182 zc
->min_mod_size
= PCIXCC_MIN_MOD_SIZE
;
183 zc
->max_mod_size
= PCIXCC_MAX_MOD_SIZE
;
184 zc
->max_exp_bit_length
= PCIXCC_MAX_MOD_SIZE
;
186 case AP_DEVICE_TYPE_CEX3C
:
187 zc
->user_space_type
= ZCRYPT_CEX3C
;
188 zc
->type_string
= "CEX3C";
189 memcpy(zc
->speed_rating
, CEX3C_SPEED_IDX
,
190 sizeof(CEX3C_SPEED_IDX
));
191 zc
->min_mod_size
= CEX3C_MIN_MOD_SIZE
;
192 zc
->max_mod_size
= CEX3C_MAX_MOD_SIZE
;
193 zc
->max_exp_bit_length
= CEX3C_MAX_MOD_SIZE
;
196 zcrypt_card_free(zc
);
201 rc
= zcrypt_card_register(zc
);
204 zcrypt_card_free(zc
);
211 * This is called to remove the PCIXCC/CEX2C card driver information
212 * if an AP card device is removed.
214 static void zcrypt_pcixcc_card_remove(struct ap_device
*ap_dev
)
216 struct zcrypt_card
*zc
= to_ap_card(&ap_dev
->device
)->private;
219 zcrypt_card_unregister(zc
);
222 static struct ap_driver zcrypt_pcixcc_card_driver
= {
223 .probe
= zcrypt_pcixcc_card_probe
,
224 .remove
= zcrypt_pcixcc_card_remove
,
225 .ids
= zcrypt_pcixcc_card_ids
,
229 * Probe function for PCIXCC/CEX2C queue devices. It always accepts the
230 * AP device since the bus_match already checked the hardware type. The
231 * PCIXCC cards come in two flavours: micro code level 2 and micro code
232 * level 3. This is checked by sending a test message to the device.
233 * @ap_dev: pointer to the AP card device.
235 static int zcrypt_pcixcc_queue_probe(struct ap_device
*ap_dev
)
237 struct ap_queue
*aq
= to_ap_queue(&ap_dev
->device
);
238 struct zcrypt_queue
*zq
;
241 zq
= zcrypt_queue_alloc(PCIXCC_MAX_XCRB_MESSAGE_SIZE
);
246 atomic_set(&zq
->load
, 0);
247 rc
= zcrypt_pcixcc_rng_supported(aq
);
249 zcrypt_queue_free(zq
);
253 zq
->ops
= zcrypt_msgtype(MSGTYPE06_NAME
,
254 MSGTYPE06_VARIANT_DEFAULT
);
256 zq
->ops
= zcrypt_msgtype(MSGTYPE06_NAME
,
257 MSGTYPE06_VARIANT_NORNG
);
258 ap_queue_init_reply(aq
, &zq
->reply
);
259 aq
->request_timeout
= PCIXCC_CLEANUP_TIME
,
261 rc
= zcrypt_queue_register(zq
);
264 zcrypt_queue_free(zq
);
270 * This is called to remove the PCIXCC/CEX2C queue driver information
271 * if an AP queue device is removed.
273 static void zcrypt_pcixcc_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_pcixcc_queue_driver
= {
284 .probe
= zcrypt_pcixcc_queue_probe
,
285 .remove
= zcrypt_pcixcc_queue_remove
,
286 .suspend
= ap_queue_suspend
,
287 .resume
= ap_queue_resume
,
288 .ids
= zcrypt_pcixcc_queue_ids
,
291 int __init
zcrypt_pcixcc_init(void)
295 rc
= ap_driver_register(&zcrypt_pcixcc_card_driver
,
296 THIS_MODULE
, "pcixcccard");
300 rc
= ap_driver_register(&zcrypt_pcixcc_queue_driver
,
301 THIS_MODULE
, "pcixccqueue");
303 ap_driver_unregister(&zcrypt_pcixcc_card_driver
);
308 void zcrypt_pcixcc_exit(void)
310 ap_driver_unregister(&zcrypt_pcixcc_queue_driver
);
311 ap_driver_unregister(&zcrypt_pcixcc_card_driver
);
314 module_init(zcrypt_pcixcc_init
);
315 module_exit(zcrypt_pcixcc_exit
);