]>
Commit | Line | Data |
---|---|---|
812141a9 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
963ed931 | 2 | /* |
5432114b | 3 | * zcrypt 2.1.0 |
963ed931 | 4 | * |
5e55a488 | 5 | * Copyright IBM Corp. 2001, 2012 |
963ed931 MS |
6 | * Author(s): Robert Burroughs |
7 | * Eric Rossman (edrossma@us.ibm.com) | |
8 | * | |
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> | |
5e55a488 | 12 | * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com> |
963ed931 MS |
13 | */ |
14 | ||
15 | #include <linux/module.h> | |
5a0e3ad6 | 16 | #include <linux/slab.h> |
963ed931 MS |
17 | #include <linux/init.h> |
18 | #include <linux/err.h> | |
60063497 | 19 | #include <linux/atomic.h> |
7c0f6ba6 | 20 | #include <linux/uaccess.h> |
e28d2af4 | 21 | #include <linux/mod_devicetable.h> |
963ed931 MS |
22 | |
23 | #include "ap_bus.h" | |
24 | #include "zcrypt_api.h" | |
25 | #include "zcrypt_error.h" | |
26 | #include "zcrypt_cex2a.h" | |
5e55a488 | 27 | #include "zcrypt_msgtype50.h" |
963ed931 MS |
28 | |
29 | #define CEX2A_MIN_MOD_SIZE 1 /* 8 bits */ | |
30 | #define CEX2A_MAX_MOD_SIZE 256 /* 2048 bits */ | |
8e89b6be | 31 | #define CEX3A_MIN_MOD_SIZE CEX2A_MIN_MOD_SIZE |
3e309a66 | 32 | #define CEX3A_MAX_MOD_SIZE 512 /* 4096 bits */ |
963ed931 | 33 | |
963ed931 MS |
34 | #define CEX2A_MAX_MESSAGE_SIZE 0x390 /* sizeof(struct type50_crb2_msg) */ |
35 | #define CEX2A_MAX_RESPONSE_SIZE 0x110 /* max outputdatalength + type80_hdr */ | |
36 | ||
3e309a66 FB |
37 | #define CEX3A_MAX_RESPONSE_SIZE 0x210 /* 512 bit modulus |
38 | * (max outputdatalength) + | |
39 | * type80_hdr*/ | |
40 | #define CEX3A_MAX_MESSAGE_SIZE sizeof(struct type50_crb3_msg) | |
8e89b6be | 41 | |
963ed931 | 42 | #define CEX2A_CLEANUP_TIME (15*HZ) |
8e89b6be | 43 | #define CEX3A_CLEANUP_TIME CEX2A_CLEANUP_TIME |
963ed931 | 44 | |
963ed931 | 45 | MODULE_AUTHOR("IBM Corporation"); |
5e55a488 HD |
46 | MODULE_DESCRIPTION("CEX2A Cryptographic Coprocessor device driver, " \ |
47 | "Copyright IBM Corp. 2001, 2012"); | |
963ed931 | 48 | MODULE_LICENSE("GPL"); |
963ed931 | 49 | |
e28d2af4 IT |
50 | static struct ap_device_id zcrypt_cex2a_card_ids[] = { |
51 | { .dev_type = AP_DEVICE_TYPE_CEX2A, | |
52 | .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE }, | |
53 | { .dev_type = AP_DEVICE_TYPE_CEX3A, | |
54 | .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE }, | |
55 | { /* end of list */ }, | |
56 | }; | |
57 | ||
58 | MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_card_ids); | |
963ed931 | 59 | |
e28d2af4 IT |
60 | static struct ap_device_id zcrypt_cex2a_queue_ids[] = { |
61 | { .dev_type = AP_DEVICE_TYPE_CEX2A, | |
62 | .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE }, | |
63 | { .dev_type = AP_DEVICE_TYPE_CEX3A, | |
64 | .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE }, | |
65 | { /* end of list */ }, | |
963ed931 MS |
66 | }; |
67 | ||
e28d2af4 IT |
68 | MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_queue_ids); |
69 | ||
963ed931 | 70 | /** |
e28d2af4 IT |
71 | * Probe function for CEX2A card devices. It always accepts the AP device |
72 | * since the bus_match already checked the card type. | |
963ed931 MS |
73 | * @ap_dev: pointer to the AP device. |
74 | */ | |
e28d2af4 | 75 | static int zcrypt_cex2a_card_probe(struct ap_device *ap_dev) |
963ed931 | 76 | { |
e28d2af4 IT |
77 | /* |
78 | * Normalized speed ratings per crypto adapter | |
79 | * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY | |
80 | */ | |
81 | static const int CEX2A_SPEED_IDX[] = { | |
82 | 800, 1000, 2000, 900, 1200, 2400, 0, 0}; | |
83 | static const int CEX3A_SPEED_IDX[] = { | |
84 | 400, 500, 1000, 450, 550, 1200, 0, 0}; | |
85 | ||
86 | struct ap_card *ac = to_ap_card(&ap_dev->device); | |
87 | struct zcrypt_card *zc; | |
8e89b6be FB |
88 | int rc = 0; |
89 | ||
e28d2af4 IT |
90 | zc = zcrypt_card_alloc(); |
91 | if (!zc) | |
92 | return -ENOMEM; | |
93 | zc->card = ac; | |
94 | ac->private = zc; | |
95 | ||
96 | if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX2A) { | |
97 | zc->min_mod_size = CEX2A_MIN_MOD_SIZE; | |
98 | zc->max_mod_size = CEX2A_MAX_MOD_SIZE; | |
99 | memcpy(zc->speed_rating, CEX2A_SPEED_IDX, | |
100 | sizeof(CEX2A_SPEED_IDX)); | |
101 | zc->max_exp_bit_length = CEX2A_MAX_MOD_SIZE; | |
102 | zc->type_string = "CEX2A"; | |
103 | zc->user_space_type = ZCRYPT_CEX2A; | |
104 | } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX3A) { | |
105 | zc->min_mod_size = CEX2A_MIN_MOD_SIZE; | |
106 | zc->max_mod_size = CEX2A_MAX_MOD_SIZE; | |
107 | zc->max_exp_bit_length = CEX2A_MAX_MOD_SIZE; | |
108 | if (ap_test_bit(&ac->functions, AP_FUNC_MEX4K) && | |
109 | ap_test_bit(&ac->functions, AP_FUNC_CRT4K)) { | |
110 | zc->max_mod_size = CEX3A_MAX_MOD_SIZE; | |
111 | zc->max_exp_bit_length = CEX3A_MAX_MOD_SIZE; | |
112 | } | |
113 | memcpy(zc->speed_rating, CEX3A_SPEED_IDX, | |
114 | sizeof(CEX3A_SPEED_IDX)); | |
115 | zc->type_string = "CEX3A"; | |
116 | zc->user_space_type = ZCRYPT_CEX3A; | |
117 | } else { | |
118 | zcrypt_card_free(zc); | |
119 | return -ENODEV; | |
120 | } | |
121 | zc->online = 1; | |
122 | ||
123 | rc = zcrypt_card_register(zc); | |
124 | if (rc) { | |
125 | ac->private = NULL; | |
126 | zcrypt_card_free(zc); | |
127 | } | |
128 | ||
129 | return rc; | |
130 | } | |
131 | ||
132 | /** | |
133 | * This is called to remove the CEX2A card driver information | |
134 | * if an AP card device is removed. | |
135 | */ | |
136 | static void zcrypt_cex2a_card_remove(struct ap_device *ap_dev) | |
137 | { | |
138 | struct zcrypt_card *zc = to_ap_card(&ap_dev->device)->private; | |
139 | ||
140 | if (zc) | |
141 | zcrypt_card_unregister(zc); | |
142 | } | |
143 | ||
144 | static struct ap_driver zcrypt_cex2a_card_driver = { | |
145 | .probe = zcrypt_cex2a_card_probe, | |
146 | .remove = zcrypt_cex2a_card_remove, | |
147 | .ids = zcrypt_cex2a_card_ids, | |
148 | }; | |
149 | ||
150 | /** | |
151 | * Probe function for CEX2A queue devices. It always accepts the AP device | |
152 | * since the bus_match already checked the queue type. | |
153 | * @ap_dev: pointer to the AP device. | |
154 | */ | |
155 | static int zcrypt_cex2a_queue_probe(struct ap_device *ap_dev) | |
156 | { | |
157 | struct ap_queue *aq = to_ap_queue(&ap_dev->device); | |
158 | struct zcrypt_queue *zq = NULL; | |
159 | int rc; | |
160 | ||
8e89b6be FB |
161 | switch (ap_dev->device_type) { |
162 | case AP_DEVICE_TYPE_CEX2A: | |
e28d2af4 IT |
163 | zq = zcrypt_queue_alloc(CEX2A_MAX_RESPONSE_SIZE); |
164 | if (!zq) | |
8e89b6be | 165 | return -ENOMEM; |
8e89b6be FB |
166 | break; |
167 | case AP_DEVICE_TYPE_CEX3A: | |
e28d2af4 IT |
168 | zq = zcrypt_queue_alloc(CEX3A_MAX_RESPONSE_SIZE); |
169 | if (!zq) | |
8e89b6be | 170 | return -ENOMEM; |
8e89b6be FB |
171 | break; |
172 | } | |
e28d2af4 | 173 | if (!zq) |
5e55a488 | 174 | return -ENODEV; |
e28d2af4 IT |
175 | zq->ops = zcrypt_msgtype(MSGTYPE50_NAME, MSGTYPE50_VARIANT_DEFAULT); |
176 | zq->queue = aq; | |
177 | zq->online = 1; | |
178 | atomic_set(&zq->load, 0); | |
179 | ap_queue_init_reply(aq, &zq->reply); | |
180 | aq->request_timeout = CEX2A_CLEANUP_TIME, | |
181 | aq->private = zq; | |
182 | rc = zcrypt_queue_register(zq); | |
8e89b6be | 183 | if (rc) { |
e28d2af4 IT |
184 | aq->private = NULL; |
185 | zcrypt_queue_free(zq); | |
8e89b6be | 186 | } |
e28d2af4 | 187 | |
963ed931 MS |
188 | return rc; |
189 | } | |
190 | ||
191 | /** | |
e28d2af4 IT |
192 | * This is called to remove the CEX2A queue driver information |
193 | * if an AP queue device is removed. | |
963ed931 | 194 | */ |
e28d2af4 | 195 | static void zcrypt_cex2a_queue_remove(struct ap_device *ap_dev) |
963ed931 | 196 | { |
e28d2af4 IT |
197 | struct ap_queue *aq = to_ap_queue(&ap_dev->device); |
198 | struct zcrypt_queue *zq = aq->private; | |
963ed931 | 199 | |
e28d2af4 IT |
200 | ap_queue_remove(aq); |
201 | if (zq) | |
202 | zcrypt_queue_unregister(zq); | |
963ed931 MS |
203 | } |
204 | ||
e28d2af4 IT |
205 | static struct ap_driver zcrypt_cex2a_queue_driver = { |
206 | .probe = zcrypt_cex2a_queue_probe, | |
207 | .remove = zcrypt_cex2a_queue_remove, | |
208 | .suspend = ap_queue_suspend, | |
209 | .resume = ap_queue_resume, | |
210 | .ids = zcrypt_cex2a_queue_ids, | |
211 | }; | |
212 | ||
963ed931 MS |
213 | int __init zcrypt_cex2a_init(void) |
214 | { | |
e28d2af4 IT |
215 | int rc; |
216 | ||
217 | rc = ap_driver_register(&zcrypt_cex2a_card_driver, | |
218 | THIS_MODULE, "cex2acard"); | |
219 | if (rc) | |
220 | return rc; | |
221 | ||
222 | rc = ap_driver_register(&zcrypt_cex2a_queue_driver, | |
223 | THIS_MODULE, "cex2aqueue"); | |
224 | if (rc) | |
225 | ap_driver_unregister(&zcrypt_cex2a_card_driver); | |
226 | ||
227 | return rc; | |
963ed931 MS |
228 | } |
229 | ||
230 | void __exit zcrypt_cex2a_exit(void) | |
231 | { | |
e28d2af4 IT |
232 | ap_driver_unregister(&zcrypt_cex2a_queue_driver); |
233 | ap_driver_unregister(&zcrypt_cex2a_card_driver); | |
963ed931 MS |
234 | } |
235 | ||
963ed931 MS |
236 | module_init(zcrypt_cex2a_init); |
237 | module_exit(zcrypt_cex2a_exit); |