sctp: walk the list of asoc safely
[linux/fpc-iii.git] / drivers / s390 / crypto / zcrypt_pcixcc.c
blob94d9f7224aea3acbb394c424a88ed2e6ba867c04
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * zcrypt 2.1.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>
24 #include "ap_bus.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;
48 int type;
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);
82 /**
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;
93 unsigned int domain;
94 struct {
95 struct type86_hdr hdr;
96 struct type86_fmt2_ext fmt2;
97 struct CPRBX cprbx;
98 } __packed *reply;
99 struct {
100 struct type6_hdr hdr;
101 struct CPRBX cprbx;
102 char function_code[2];
103 short int rule_length;
104 char rule[8];
105 short int verb_length;
106 short int key_length;
107 } __packed *msg;
108 int rc, i;
110 ap_init_message(&ap_msg);
111 ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
112 if (!ap_msg.message)
113 return -ENOMEM;
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,
121 ap_msg.length);
122 if (rc)
123 goto out_free;
125 /* Wait for the test message to complete. */
126 for (i = 0; i < 2 * HZ; i++) {
127 msleep(1000 / HZ);
128 rc = ap_recv(aq->qid, &psmid, ap_msg.message, 4096);
129 if (rc == 0 && psmid == 0x0102030405060708ULL)
130 break;
133 if (i >= 2 * HZ) {
134 /* Got no answer. */
135 rc = -ENODEV;
136 goto out_free;
139 reply = ap_msg.message;
140 if (reply->cprbx.ccp_rtcode == 0 && reply->cprbx.ccp_rscode == 0)
141 rc = 1;
142 else
143 rc = 0;
144 out_free:
145 free_page((unsigned long) ap_msg.message);
146 return rc;
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;
169 int rc = 0;
171 zc = zcrypt_card_alloc();
172 if (!zc)
173 return -ENOMEM;
174 zc->card = ac;
175 ac->private = zc;
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;
185 break;
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;
194 break;
195 default:
196 zcrypt_card_free(zc);
197 return -ENODEV;
199 zc->online = 1;
201 rc = zcrypt_card_register(zc);
202 if (rc) {
203 ac->private = NULL;
204 zcrypt_card_free(zc);
207 return rc;
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;
218 if (zc)
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,
226 .flags = AP_DRIVER_FLAG_DEFAULT,
230 * Probe function for PCIXCC/CEX2C queue devices. It always accepts the
231 * AP device since the bus_match already checked the hardware type. The
232 * PCIXCC cards come in two flavours: micro code level 2 and micro code
233 * level 3. This is checked by sending a test message to the device.
234 * @ap_dev: pointer to the AP card device.
236 static int zcrypt_pcixcc_queue_probe(struct ap_device *ap_dev)
238 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
239 struct zcrypt_queue *zq;
240 int rc;
242 zq = zcrypt_queue_alloc(PCIXCC_MAX_XCRB_MESSAGE_SIZE);
243 if (!zq)
244 return -ENOMEM;
245 zq->queue = aq;
246 zq->online = 1;
247 atomic_set(&zq->load, 0);
248 rc = zcrypt_pcixcc_rng_supported(aq);
249 if (rc < 0) {
250 zcrypt_queue_free(zq);
251 return rc;
253 if (rc)
254 zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
255 MSGTYPE06_VARIANT_DEFAULT);
256 else
257 zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
258 MSGTYPE06_VARIANT_NORNG);
259 ap_queue_init_reply(aq, &zq->reply);
260 aq->request_timeout = PCIXCC_CLEANUP_TIME,
261 aq->private = zq;
262 rc = zcrypt_queue_register(zq);
263 if (rc) {
264 aq->private = NULL;
265 zcrypt_queue_free(zq);
267 return rc;
271 * This is called to remove the PCIXCC/CEX2C queue driver information
272 * if an AP queue device is removed.
274 static void zcrypt_pcixcc_queue_remove(struct ap_device *ap_dev)
276 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
277 struct zcrypt_queue *zq = aq->private;
279 ap_queue_remove(aq);
280 if (zq)
281 zcrypt_queue_unregister(zq);
284 static struct ap_driver zcrypt_pcixcc_queue_driver = {
285 .probe = zcrypt_pcixcc_queue_probe,
286 .remove = zcrypt_pcixcc_queue_remove,
287 .suspend = ap_queue_suspend,
288 .resume = ap_queue_resume,
289 .ids = zcrypt_pcixcc_queue_ids,
290 .flags = AP_DRIVER_FLAG_DEFAULT,
293 int __init zcrypt_pcixcc_init(void)
295 int rc;
297 rc = ap_driver_register(&zcrypt_pcixcc_card_driver,
298 THIS_MODULE, "pcixcccard");
299 if (rc)
300 return rc;
302 rc = ap_driver_register(&zcrypt_pcixcc_queue_driver,
303 THIS_MODULE, "pcixccqueue");
304 if (rc)
305 ap_driver_unregister(&zcrypt_pcixcc_card_driver);
307 return rc;
310 void zcrypt_pcixcc_exit(void)
312 ap_driver_unregister(&zcrypt_pcixcc_queue_driver);
313 ap_driver_unregister(&zcrypt_pcixcc_card_driver);
316 module_init(zcrypt_pcixcc_init);
317 module_exit(zcrypt_pcixcc_exit);