4 * Copyright IBM Corp. 2001, 2012
5 * Author(s): Robert Burroughs
6 * Eric Rossman (edrossma@us.ibm.com)
8 * Hotplug & misc device support: Jochen Roehrig (roehrig@de.ibm.com)
9 * Major cleanup & driver split: Martin Schwidefsky <schwidefsky@de.ibm.com>
10 * Ralph Wuerthner <rwuerthn@de.ibm.com>
11 * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/err.h>
31 #include <linux/delay.h>
32 #include <linux/slab.h>
33 #include <linux/atomic.h>
34 #include <linux/uaccess.h>
35 #include <linux/mod_devicetable.h>
38 #include "zcrypt_api.h"
39 #include "zcrypt_error.h"
40 #include "zcrypt_msgtype6.h"
41 #include "zcrypt_pcixcc.h"
42 #include "zcrypt_cca_key.h"
44 #define PCIXCC_MIN_MOD_SIZE 16 /* 128 bits */
45 #define PCIXCC_MIN_MOD_SIZE_OLD 64 /* 512 bits */
46 #define PCIXCC_MAX_MOD_SIZE 256 /* 2048 bits */
47 #define CEX3C_MIN_MOD_SIZE PCIXCC_MIN_MOD_SIZE
48 #define CEX3C_MAX_MOD_SIZE 512 /* 4096 bits */
50 #define PCIXCC_MAX_ICA_MESSAGE_SIZE 0x77c /* max size type6 v2 crt message */
51 #define PCIXCC_MAX_ICA_RESPONSE_SIZE 0x77c /* max size type86 v2 reply */
53 #define PCIXCC_MAX_XCRB_MESSAGE_SIZE (12*1024)
55 #define PCIXCC_CLEANUP_TIME (15*HZ)
57 #define CEIL4(x) ((((x)+3)/4)*4)
59 struct response_type
{
60 struct completion work
;
63 #define PCIXCC_RESPONSE_TYPE_ICA 0
64 #define PCIXCC_RESPONSE_TYPE_XCRB 1
66 MODULE_AUTHOR("IBM Corporation");
67 MODULE_DESCRIPTION("PCIXCC Cryptographic Coprocessor device driver, " \
68 "Copyright IBM Corp. 2001, 2012");
69 MODULE_LICENSE("GPL");
71 static struct ap_device_id zcrypt_pcixcc_card_ids
[] = {
72 { .dev_type
= AP_DEVICE_TYPE_PCIXCC
,
73 .match_flags
= AP_DEVICE_ID_MATCH_CARD_TYPE
},
74 { .dev_type
= AP_DEVICE_TYPE_CEX2C
,
75 .match_flags
= AP_DEVICE_ID_MATCH_CARD_TYPE
},
76 { .dev_type
= AP_DEVICE_TYPE_CEX3C
,
77 .match_flags
= AP_DEVICE_ID_MATCH_CARD_TYPE
},
78 { /* end of list */ },
81 MODULE_DEVICE_TABLE(ap
, zcrypt_pcixcc_card_ids
);
83 static struct ap_device_id zcrypt_pcixcc_queue_ids
[] = {
84 { .dev_type
= AP_DEVICE_TYPE_PCIXCC
,
85 .match_flags
= AP_DEVICE_ID_MATCH_QUEUE_TYPE
},
86 { .dev_type
= AP_DEVICE_TYPE_CEX2C
,
87 .match_flags
= AP_DEVICE_ID_MATCH_QUEUE_TYPE
},
88 { .dev_type
= AP_DEVICE_TYPE_CEX3C
,
89 .match_flags
= AP_DEVICE_ID_MATCH_QUEUE_TYPE
},
90 { /* end of list */ },
93 MODULE_DEVICE_TABLE(ap
, zcrypt_pcixcc_queue_ids
);
96 * Large random number detection function. Its sends a message to a pcixcc
97 * card to find out if large random numbers are supported.
98 * @ap_dev: pointer to the AP device.
100 * Returns 1 if large random numbers are supported, 0 if not and < 0 on error.
102 static int zcrypt_pcixcc_rng_supported(struct ap_queue
*aq
)
104 struct ap_message ap_msg
;
105 unsigned long long psmid
;
108 struct type86_hdr hdr
;
109 struct type86_fmt2_ext fmt2
;
111 } __attribute__((packed
)) *reply
;
113 struct type6_hdr hdr
;
115 char function_code
[2];
116 short int rule_length
;
118 short int verb_length
;
119 short int key_length
;
123 ap_init_message(&ap_msg
);
124 ap_msg
.message
= (void *) get_zeroed_page(GFP_KERNEL
);
128 rng_type6CPRB_msgX(&ap_msg
, 4, &domain
);
130 msg
= ap_msg
.message
;
131 msg
->cprbx
.domain
= AP_QID_QUEUE(aq
->qid
);
133 rc
= ap_send(aq
->qid
, 0x0102030405060708ULL
, ap_msg
.message
,
138 /* Wait for the test message to complete. */
139 for (i
= 0; i
< 2 * HZ
; i
++) {
141 rc
= ap_recv(aq
->qid
, &psmid
, ap_msg
.message
, 4096);
142 if (rc
== 0 && psmid
== 0x0102030405060708ULL
)
152 reply
= ap_msg
.message
;
153 if (reply
->cprbx
.ccp_rtcode
== 0 && reply
->cprbx
.ccp_rscode
== 0)
158 free_page((unsigned long) ap_msg
.message
);
163 * Probe function for PCIXCC/CEX2C card devices. It always accepts the
164 * AP device since the bus_match already checked the hardware type. The
165 * PCIXCC cards come in two flavours: micro code level 2 and micro code
166 * level 3. This is checked by sending a test message to the device.
167 * @ap_dev: pointer to the AP card device.
169 static int zcrypt_pcixcc_card_probe(struct ap_device
*ap_dev
)
172 * Normalized speed ratings per crypto adapter
173 * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
175 static const int CEX2C_SPEED_IDX
[] = {
176 1000, 1400, 2400, 1100, 1500, 2600, 100, 12};
177 static const int CEX3C_SPEED_IDX
[] = {
178 500, 700, 1400, 550, 800, 1500, 80, 10};
180 struct ap_card
*ac
= to_ap_card(&ap_dev
->device
);
181 struct zcrypt_card
*zc
;
184 zc
= zcrypt_card_alloc();
189 switch (ac
->ap_dev
.device_type
) {
190 case AP_DEVICE_TYPE_CEX2C
:
191 zc
->user_space_type
= ZCRYPT_CEX2C
;
192 zc
->type_string
= "CEX2C";
193 memcpy(zc
->speed_rating
, CEX2C_SPEED_IDX
,
194 sizeof(CEX2C_SPEED_IDX
));
195 zc
->min_mod_size
= PCIXCC_MIN_MOD_SIZE
;
196 zc
->max_mod_size
= PCIXCC_MAX_MOD_SIZE
;
197 zc
->max_exp_bit_length
= PCIXCC_MAX_MOD_SIZE
;
199 case AP_DEVICE_TYPE_CEX3C
:
200 zc
->user_space_type
= ZCRYPT_CEX3C
;
201 zc
->type_string
= "CEX3C";
202 memcpy(zc
->speed_rating
, CEX3C_SPEED_IDX
,
203 sizeof(CEX3C_SPEED_IDX
));
204 zc
->min_mod_size
= CEX3C_MIN_MOD_SIZE
;
205 zc
->max_mod_size
= CEX3C_MAX_MOD_SIZE
;
206 zc
->max_exp_bit_length
= CEX3C_MAX_MOD_SIZE
;
209 zcrypt_card_free(zc
);
214 rc
= zcrypt_card_register(zc
);
217 zcrypt_card_free(zc
);
224 * This is called to remove the PCIXCC/CEX2C card driver information
225 * if an AP card device is removed.
227 static void zcrypt_pcixcc_card_remove(struct ap_device
*ap_dev
)
229 struct zcrypt_card
*zc
= to_ap_card(&ap_dev
->device
)->private;
232 zcrypt_card_unregister(zc
);
235 static struct ap_driver zcrypt_pcixcc_card_driver
= {
236 .probe
= zcrypt_pcixcc_card_probe
,
237 .remove
= zcrypt_pcixcc_card_remove
,
238 .ids
= zcrypt_pcixcc_card_ids
,
242 * Probe function for PCIXCC/CEX2C queue devices. It always accepts the
243 * AP device since the bus_match already checked the hardware type. The
244 * PCIXCC cards come in two flavours: micro code level 2 and micro code
245 * level 3. This is checked by sending a test message to the device.
246 * @ap_dev: pointer to the AP card device.
248 static int zcrypt_pcixcc_queue_probe(struct ap_device
*ap_dev
)
250 struct ap_queue
*aq
= to_ap_queue(&ap_dev
->device
);
251 struct zcrypt_queue
*zq
;
254 zq
= zcrypt_queue_alloc(PCIXCC_MAX_XCRB_MESSAGE_SIZE
);
259 atomic_set(&zq
->load
, 0);
260 rc
= zcrypt_pcixcc_rng_supported(aq
);
262 zcrypt_queue_free(zq
);
266 zq
->ops
= zcrypt_msgtype(MSGTYPE06_NAME
,
267 MSGTYPE06_VARIANT_DEFAULT
);
269 zq
->ops
= zcrypt_msgtype(MSGTYPE06_NAME
,
270 MSGTYPE06_VARIANT_NORNG
);
271 ap_queue_init_reply(aq
, &zq
->reply
);
272 aq
->request_timeout
= PCIXCC_CLEANUP_TIME
,
274 rc
= zcrypt_queue_register(zq
);
277 zcrypt_queue_free(zq
);
283 * This is called to remove the PCIXCC/CEX2C queue driver information
284 * if an AP queue device is removed.
286 static void zcrypt_pcixcc_queue_remove(struct ap_device
*ap_dev
)
288 struct ap_queue
*aq
= to_ap_queue(&ap_dev
->device
);
289 struct zcrypt_queue
*zq
= aq
->private;
293 zcrypt_queue_unregister(zq
);
296 static struct ap_driver zcrypt_pcixcc_queue_driver
= {
297 .probe
= zcrypt_pcixcc_queue_probe
,
298 .remove
= zcrypt_pcixcc_queue_remove
,
299 .suspend
= ap_queue_suspend
,
300 .resume
= ap_queue_resume
,
301 .ids
= zcrypt_pcixcc_queue_ids
,
304 int __init
zcrypt_pcixcc_init(void)
308 rc
= ap_driver_register(&zcrypt_pcixcc_card_driver
,
309 THIS_MODULE
, "pcixcccard");
313 rc
= ap_driver_register(&zcrypt_pcixcc_queue_driver
,
314 THIS_MODULE
, "pcixccqueue");
316 ap_driver_unregister(&zcrypt_pcixcc_card_driver
);
321 void zcrypt_pcixcc_exit(void)
323 ap_driver_unregister(&zcrypt_pcixcc_queue_driver
);
324 ap_driver_unregister(&zcrypt_pcixcc_card_driver
);
327 module_init(zcrypt_pcixcc_init
);
328 module_exit(zcrypt_pcixcc_exit
);