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/slab.h>
30 #include <linux/init.h>
31 #include <linux/err.h>
32 #include <linux/atomic.h>
33 #include <linux/uaccess.h>
34 #include <linux/mod_devicetable.h>
37 #include "zcrypt_api.h"
38 #include "zcrypt_error.h"
39 #include "zcrypt_cex2a.h"
40 #include "zcrypt_msgtype50.h"
42 #define CEX2A_MIN_MOD_SIZE 1 /* 8 bits */
43 #define CEX2A_MAX_MOD_SIZE 256 /* 2048 bits */
44 #define CEX3A_MIN_MOD_SIZE CEX2A_MIN_MOD_SIZE
45 #define CEX3A_MAX_MOD_SIZE 512 /* 4096 bits */
47 #define CEX2A_MAX_MESSAGE_SIZE 0x390 /* sizeof(struct type50_crb2_msg) */
48 #define CEX2A_MAX_RESPONSE_SIZE 0x110 /* max outputdatalength + type80_hdr */
50 #define CEX3A_MAX_RESPONSE_SIZE 0x210 /* 512 bit modulus
51 * (max outputdatalength) +
53 #define CEX3A_MAX_MESSAGE_SIZE sizeof(struct type50_crb3_msg)
55 #define CEX2A_CLEANUP_TIME (15*HZ)
56 #define CEX3A_CLEANUP_TIME CEX2A_CLEANUP_TIME
58 MODULE_AUTHOR("IBM Corporation");
59 MODULE_DESCRIPTION("CEX2A Cryptographic Coprocessor device driver, " \
60 "Copyright IBM Corp. 2001, 2012");
61 MODULE_LICENSE("GPL");
63 static struct ap_device_id zcrypt_cex2a_card_ids
[] = {
64 { .dev_type
= AP_DEVICE_TYPE_CEX2A
,
65 .match_flags
= AP_DEVICE_ID_MATCH_CARD_TYPE
},
66 { .dev_type
= AP_DEVICE_TYPE_CEX3A
,
67 .match_flags
= AP_DEVICE_ID_MATCH_CARD_TYPE
},
68 { /* end of list */ },
71 MODULE_DEVICE_TABLE(ap
, zcrypt_cex2a_card_ids
);
73 static struct ap_device_id zcrypt_cex2a_queue_ids
[] = {
74 { .dev_type
= AP_DEVICE_TYPE_CEX2A
,
75 .match_flags
= AP_DEVICE_ID_MATCH_QUEUE_TYPE
},
76 { .dev_type
= AP_DEVICE_TYPE_CEX3A
,
77 .match_flags
= AP_DEVICE_ID_MATCH_QUEUE_TYPE
},
78 { /* end of list */ },
81 MODULE_DEVICE_TABLE(ap
, zcrypt_cex2a_queue_ids
);
84 * Probe function for CEX2A card devices. It always accepts the AP device
85 * since the bus_match already checked the card type.
86 * @ap_dev: pointer to the AP device.
88 static int zcrypt_cex2a_card_probe(struct ap_device
*ap_dev
)
91 * Normalized speed ratings per crypto adapter
92 * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
94 static const int CEX2A_SPEED_IDX
[] = {
95 800, 1000, 2000, 900, 1200, 2400, 0, 0};
96 static const int CEX3A_SPEED_IDX
[] = {
97 400, 500, 1000, 450, 550, 1200, 0, 0};
99 struct ap_card
*ac
= to_ap_card(&ap_dev
->device
);
100 struct zcrypt_card
*zc
;
103 zc
= zcrypt_card_alloc();
109 if (ac
->ap_dev
.device_type
== AP_DEVICE_TYPE_CEX2A
) {
110 zc
->min_mod_size
= CEX2A_MIN_MOD_SIZE
;
111 zc
->max_mod_size
= CEX2A_MAX_MOD_SIZE
;
112 memcpy(zc
->speed_rating
, CEX2A_SPEED_IDX
,
113 sizeof(CEX2A_SPEED_IDX
));
114 zc
->max_exp_bit_length
= CEX2A_MAX_MOD_SIZE
;
115 zc
->type_string
= "CEX2A";
116 zc
->user_space_type
= ZCRYPT_CEX2A
;
117 } else if (ac
->ap_dev
.device_type
== AP_DEVICE_TYPE_CEX3A
) {
118 zc
->min_mod_size
= CEX2A_MIN_MOD_SIZE
;
119 zc
->max_mod_size
= CEX2A_MAX_MOD_SIZE
;
120 zc
->max_exp_bit_length
= CEX2A_MAX_MOD_SIZE
;
121 if (ap_test_bit(&ac
->functions
, AP_FUNC_MEX4K
) &&
122 ap_test_bit(&ac
->functions
, AP_FUNC_CRT4K
)) {
123 zc
->max_mod_size
= CEX3A_MAX_MOD_SIZE
;
124 zc
->max_exp_bit_length
= CEX3A_MAX_MOD_SIZE
;
126 memcpy(zc
->speed_rating
, CEX3A_SPEED_IDX
,
127 sizeof(CEX3A_SPEED_IDX
));
128 zc
->type_string
= "CEX3A";
129 zc
->user_space_type
= ZCRYPT_CEX3A
;
131 zcrypt_card_free(zc
);
136 rc
= zcrypt_card_register(zc
);
139 zcrypt_card_free(zc
);
146 * This is called to remove the CEX2A card driver information
147 * if an AP card device is removed.
149 static void zcrypt_cex2a_card_remove(struct ap_device
*ap_dev
)
151 struct zcrypt_card
*zc
= to_ap_card(&ap_dev
->device
)->private;
154 zcrypt_card_unregister(zc
);
157 static struct ap_driver zcrypt_cex2a_card_driver
= {
158 .probe
= zcrypt_cex2a_card_probe
,
159 .remove
= zcrypt_cex2a_card_remove
,
160 .ids
= zcrypt_cex2a_card_ids
,
164 * Probe function for CEX2A queue devices. It always accepts the AP device
165 * since the bus_match already checked the queue type.
166 * @ap_dev: pointer to the AP device.
168 static int zcrypt_cex2a_queue_probe(struct ap_device
*ap_dev
)
170 struct ap_queue
*aq
= to_ap_queue(&ap_dev
->device
);
171 struct zcrypt_queue
*zq
= NULL
;
174 switch (ap_dev
->device_type
) {
175 case AP_DEVICE_TYPE_CEX2A
:
176 zq
= zcrypt_queue_alloc(CEX2A_MAX_RESPONSE_SIZE
);
180 case AP_DEVICE_TYPE_CEX3A
:
181 zq
= zcrypt_queue_alloc(CEX3A_MAX_RESPONSE_SIZE
);
188 zq
->ops
= zcrypt_msgtype(MSGTYPE50_NAME
, MSGTYPE50_VARIANT_DEFAULT
);
191 atomic_set(&zq
->load
, 0);
192 ap_queue_init_reply(aq
, &zq
->reply
);
193 aq
->request_timeout
= CEX2A_CLEANUP_TIME
,
195 rc
= zcrypt_queue_register(zq
);
198 zcrypt_queue_free(zq
);
205 * This is called to remove the CEX2A queue driver information
206 * if an AP queue device is removed.
208 static void zcrypt_cex2a_queue_remove(struct ap_device
*ap_dev
)
210 struct ap_queue
*aq
= to_ap_queue(&ap_dev
->device
);
211 struct zcrypt_queue
*zq
= aq
->private;
215 zcrypt_queue_unregister(zq
);
218 static struct ap_driver zcrypt_cex2a_queue_driver
= {
219 .probe
= zcrypt_cex2a_queue_probe
,
220 .remove
= zcrypt_cex2a_queue_remove
,
221 .suspend
= ap_queue_suspend
,
222 .resume
= ap_queue_resume
,
223 .ids
= zcrypt_cex2a_queue_ids
,
226 int __init
zcrypt_cex2a_init(void)
230 rc
= ap_driver_register(&zcrypt_cex2a_card_driver
,
231 THIS_MODULE
, "cex2acard");
235 rc
= ap_driver_register(&zcrypt_cex2a_queue_driver
,
236 THIS_MODULE
, "cex2aqueue");
238 ap_driver_unregister(&zcrypt_cex2a_card_driver
);
243 void __exit
zcrypt_cex2a_exit(void)
245 ap_driver_unregister(&zcrypt_cex2a_queue_driver
);
246 ap_driver_unregister(&zcrypt_cex2a_card_driver
);
249 module_init(zcrypt_cex2a_init
);
250 module_exit(zcrypt_cex2a_exit
);