]>
Commit | Line | Data |
---|---|---|
963ed931 | 1 | /* |
5432114b | 2 | * zcrypt 2.1.0 |
963ed931 | 3 | * |
5e55a488 | 4 | * Copyright IBM Corp. 2001, 2012 |
963ed931 MS |
5 | * Author(s): Robert Burroughs |
6 | * Eric Rossman (edrossma@us.ibm.com) | |
7 | * | |
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> | |
5e55a488 | 11 | * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com> |
963ed931 MS |
12 | * |
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) | |
16 | * any later version. | |
17 | * | |
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. | |
22 | * | |
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. | |
26 | */ | |
27 | ||
28 | #include <linux/module.h> | |
5a0e3ad6 | 29 | #include <linux/slab.h> |
963ed931 MS |
30 | #include <linux/init.h> |
31 | #include <linux/err.h> | |
60063497 | 32 | #include <linux/atomic.h> |
7c0f6ba6 | 33 | #include <linux/uaccess.h> |
e28d2af4 | 34 | #include <linux/mod_devicetable.h> |
963ed931 MS |
35 | |
36 | #include "ap_bus.h" | |
37 | #include "zcrypt_api.h" | |
38 | #include "zcrypt_error.h" | |
39 | #include "zcrypt_cex2a.h" | |
5e55a488 | 40 | #include "zcrypt_msgtype50.h" |
963ed931 MS |
41 | |
42 | #define CEX2A_MIN_MOD_SIZE 1 /* 8 bits */ | |
43 | #define CEX2A_MAX_MOD_SIZE 256 /* 2048 bits */ | |
8e89b6be | 44 | #define CEX3A_MIN_MOD_SIZE CEX2A_MIN_MOD_SIZE |
3e309a66 | 45 | #define CEX3A_MAX_MOD_SIZE 512 /* 4096 bits */ |
963ed931 | 46 | |
963ed931 MS |
47 | #define CEX2A_MAX_MESSAGE_SIZE 0x390 /* sizeof(struct type50_crb2_msg) */ |
48 | #define CEX2A_MAX_RESPONSE_SIZE 0x110 /* max outputdatalength + type80_hdr */ | |
49 | ||
3e309a66 FB |
50 | #define CEX3A_MAX_RESPONSE_SIZE 0x210 /* 512 bit modulus |
51 | * (max outputdatalength) + | |
52 | * type80_hdr*/ | |
53 | #define CEX3A_MAX_MESSAGE_SIZE sizeof(struct type50_crb3_msg) | |
8e89b6be | 54 | |
963ed931 | 55 | #define CEX2A_CLEANUP_TIME (15*HZ) |
8e89b6be | 56 | #define CEX3A_CLEANUP_TIME CEX2A_CLEANUP_TIME |
963ed931 | 57 | |
963ed931 | 58 | MODULE_AUTHOR("IBM Corporation"); |
5e55a488 HD |
59 | MODULE_DESCRIPTION("CEX2A Cryptographic Coprocessor device driver, " \ |
60 | "Copyright IBM Corp. 2001, 2012"); | |
963ed931 | 61 | MODULE_LICENSE("GPL"); |
963ed931 | 62 | |
e28d2af4 IT |
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 */ }, | |
69 | }; | |
70 | ||
71 | MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_card_ids); | |
963ed931 | 72 | |
e28d2af4 IT |
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 */ }, | |
963ed931 MS |
79 | }; |
80 | ||
e28d2af4 IT |
81 | MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_queue_ids); |
82 | ||
963ed931 | 83 | /** |
e28d2af4 IT |
84 | * Probe function for CEX2A card devices. It always accepts the AP device |
85 | * since the bus_match already checked the card type. | |
963ed931 MS |
86 | * @ap_dev: pointer to the AP device. |
87 | */ | |
e28d2af4 | 88 | static int zcrypt_cex2a_card_probe(struct ap_device *ap_dev) |
963ed931 | 89 | { |
e28d2af4 IT |
90 | /* |
91 | * Normalized speed ratings per crypto adapter | |
92 | * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY | |
93 | */ | |
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}; | |
98 | ||
99 | struct ap_card *ac = to_ap_card(&ap_dev->device); | |
100 | struct zcrypt_card *zc; | |
8e89b6be FB |
101 | int rc = 0; |
102 | ||
e28d2af4 IT |
103 | zc = zcrypt_card_alloc(); |
104 | if (!zc) | |
105 | return -ENOMEM; | |
106 | zc->card = ac; | |
107 | ac->private = zc; | |
108 | ||
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; | |
125 | } | |
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; | |
130 | } else { | |
131 | zcrypt_card_free(zc); | |
132 | return -ENODEV; | |
133 | } | |
134 | zc->online = 1; | |
135 | ||
136 | rc = zcrypt_card_register(zc); | |
137 | if (rc) { | |
138 | ac->private = NULL; | |
139 | zcrypt_card_free(zc); | |
140 | } | |
141 | ||
142 | return rc; | |
143 | } | |
144 | ||
145 | /** | |
146 | * This is called to remove the CEX2A card driver information | |
147 | * if an AP card device is removed. | |
148 | */ | |
149 | static void zcrypt_cex2a_card_remove(struct ap_device *ap_dev) | |
150 | { | |
151 | struct zcrypt_card *zc = to_ap_card(&ap_dev->device)->private; | |
152 | ||
153 | if (zc) | |
154 | zcrypt_card_unregister(zc); | |
155 | } | |
156 | ||
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, | |
161 | }; | |
162 | ||
163 | /** | |
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. | |
167 | */ | |
168 | static int zcrypt_cex2a_queue_probe(struct ap_device *ap_dev) | |
169 | { | |
170 | struct ap_queue *aq = to_ap_queue(&ap_dev->device); | |
171 | struct zcrypt_queue *zq = NULL; | |
172 | int rc; | |
173 | ||
8e89b6be FB |
174 | switch (ap_dev->device_type) { |
175 | case AP_DEVICE_TYPE_CEX2A: | |
e28d2af4 IT |
176 | zq = zcrypt_queue_alloc(CEX2A_MAX_RESPONSE_SIZE); |
177 | if (!zq) | |
8e89b6be | 178 | return -ENOMEM; |
8e89b6be FB |
179 | break; |
180 | case AP_DEVICE_TYPE_CEX3A: | |
e28d2af4 IT |
181 | zq = zcrypt_queue_alloc(CEX3A_MAX_RESPONSE_SIZE); |
182 | if (!zq) | |
8e89b6be | 183 | return -ENOMEM; |
8e89b6be FB |
184 | break; |
185 | } | |
e28d2af4 | 186 | if (!zq) |
5e55a488 | 187 | return -ENODEV; |
e28d2af4 IT |
188 | zq->ops = zcrypt_msgtype(MSGTYPE50_NAME, MSGTYPE50_VARIANT_DEFAULT); |
189 | zq->queue = aq; | |
190 | zq->online = 1; | |
191 | atomic_set(&zq->load, 0); | |
192 | ap_queue_init_reply(aq, &zq->reply); | |
193 | aq->request_timeout = CEX2A_CLEANUP_TIME, | |
194 | aq->private = zq; | |
195 | rc = zcrypt_queue_register(zq); | |
8e89b6be | 196 | if (rc) { |
e28d2af4 IT |
197 | aq->private = NULL; |
198 | zcrypt_queue_free(zq); | |
8e89b6be | 199 | } |
e28d2af4 | 200 | |
963ed931 MS |
201 | return rc; |
202 | } | |
203 | ||
204 | /** | |
e28d2af4 IT |
205 | * This is called to remove the CEX2A queue driver information |
206 | * if an AP queue device is removed. | |
963ed931 | 207 | */ |
e28d2af4 | 208 | static void zcrypt_cex2a_queue_remove(struct ap_device *ap_dev) |
963ed931 | 209 | { |
e28d2af4 IT |
210 | struct ap_queue *aq = to_ap_queue(&ap_dev->device); |
211 | struct zcrypt_queue *zq = aq->private; | |
963ed931 | 212 | |
e28d2af4 IT |
213 | ap_queue_remove(aq); |
214 | if (zq) | |
215 | zcrypt_queue_unregister(zq); | |
963ed931 MS |
216 | } |
217 | ||
e28d2af4 IT |
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, | |
224 | }; | |
225 | ||
963ed931 MS |
226 | int __init zcrypt_cex2a_init(void) |
227 | { | |
e28d2af4 IT |
228 | int rc; |
229 | ||
230 | rc = ap_driver_register(&zcrypt_cex2a_card_driver, | |
231 | THIS_MODULE, "cex2acard"); | |
232 | if (rc) | |
233 | return rc; | |
234 | ||
235 | rc = ap_driver_register(&zcrypt_cex2a_queue_driver, | |
236 | THIS_MODULE, "cex2aqueue"); | |
237 | if (rc) | |
238 | ap_driver_unregister(&zcrypt_cex2a_card_driver); | |
239 | ||
240 | return rc; | |
963ed931 MS |
241 | } |
242 | ||
243 | void __exit zcrypt_cex2a_exit(void) | |
244 | { | |
e28d2af4 IT |
245 | ap_driver_unregister(&zcrypt_cex2a_queue_driver); |
246 | ap_driver_unregister(&zcrypt_cex2a_card_driver); | |
963ed931 MS |
247 | } |
248 | ||
963ed931 MS |
249 | module_init(zcrypt_cex2a_init); |
250 | module_exit(zcrypt_cex2a_exit); |