Linux 4.16.11
[linux/fpc-iii.git] / drivers / s390 / crypto / zcrypt_cex2a.c
blobe701194d36115c06d4435df2b1dcc4dbc9cc103e
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/slab.h>
17 #include <linux/init.h>
18 #include <linux/err.h>
19 #include <linux/atomic.h>
20 #include <linux/uaccess.h>
21 #include <linux/mod_devicetable.h>
23 #include "ap_bus.h"
24 #include "zcrypt_api.h"
25 #include "zcrypt_error.h"
26 #include "zcrypt_cex2a.h"
27 #include "zcrypt_msgtype50.h"
29 #define CEX2A_MIN_MOD_SIZE 1 /* 8 bits */
30 #define CEX2A_MAX_MOD_SIZE 256 /* 2048 bits */
31 #define CEX3A_MIN_MOD_SIZE CEX2A_MIN_MOD_SIZE
32 #define CEX3A_MAX_MOD_SIZE 512 /* 4096 bits */
34 #define CEX2A_MAX_MESSAGE_SIZE 0x390 /* sizeof(struct type50_crb2_msg) */
35 #define CEX2A_MAX_RESPONSE_SIZE 0x110 /* max outputdatalength + type80_hdr */
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)
42 #define CEX2A_CLEANUP_TIME (15*HZ)
43 #define CEX3A_CLEANUP_TIME CEX2A_CLEANUP_TIME
45 MODULE_AUTHOR("IBM Corporation");
46 MODULE_DESCRIPTION("CEX2A Cryptographic Coprocessor device driver, " \
47 "Copyright IBM Corp. 2001, 2012");
48 MODULE_LICENSE("GPL");
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 */ },
58 MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_card_ids);
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 */ },
68 MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_queue_ids);
70 /**
71 * Probe function for CEX2A card devices. It always accepts the AP device
72 * since the bus_match already checked the card type.
73 * @ap_dev: pointer to the AP device.
75 static int zcrypt_cex2a_card_probe(struct ap_device *ap_dev)
78 * Normalized speed ratings per crypto adapter
79 * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
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};
86 struct ap_card *ac = to_ap_card(&ap_dev->device);
87 struct zcrypt_card *zc;
88 int rc = 0;
90 zc = zcrypt_card_alloc();
91 if (!zc)
92 return -ENOMEM;
93 zc->card = ac;
94 ac->private = zc;
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;
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;
121 zc->online = 1;
123 rc = zcrypt_card_register(zc);
124 if (rc) {
125 ac->private = NULL;
126 zcrypt_card_free(zc);
129 return rc;
133 * This is called to remove the CEX2A card driver information
134 * if an AP card device is removed.
136 static void zcrypt_cex2a_card_remove(struct ap_device *ap_dev)
138 struct zcrypt_card *zc = to_ap_card(&ap_dev->device)->private;
140 if (zc)
141 zcrypt_card_unregister(zc);
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,
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.
155 static int zcrypt_cex2a_queue_probe(struct ap_device *ap_dev)
157 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
158 struct zcrypt_queue *zq = NULL;
159 int rc;
161 switch (ap_dev->device_type) {
162 case AP_DEVICE_TYPE_CEX2A:
163 zq = zcrypt_queue_alloc(CEX2A_MAX_RESPONSE_SIZE);
164 if (!zq)
165 return -ENOMEM;
166 break;
167 case AP_DEVICE_TYPE_CEX3A:
168 zq = zcrypt_queue_alloc(CEX3A_MAX_RESPONSE_SIZE);
169 if (!zq)
170 return -ENOMEM;
171 break;
173 if (!zq)
174 return -ENODEV;
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);
183 if (rc) {
184 aq->private = NULL;
185 zcrypt_queue_free(zq);
188 return rc;
192 * This is called to remove the CEX2A queue driver information
193 * if an AP queue device is removed.
195 static void zcrypt_cex2a_queue_remove(struct ap_device *ap_dev)
197 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
198 struct zcrypt_queue *zq = aq->private;
200 ap_queue_remove(aq);
201 if (zq)
202 zcrypt_queue_unregister(zq);
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,
213 int __init zcrypt_cex2a_init(void)
215 int rc;
217 rc = ap_driver_register(&zcrypt_cex2a_card_driver,
218 THIS_MODULE, "cex2acard");
219 if (rc)
220 return rc;
222 rc = ap_driver_register(&zcrypt_cex2a_queue_driver,
223 THIS_MODULE, "cex2aqueue");
224 if (rc)
225 ap_driver_unregister(&zcrypt_cex2a_card_driver);
227 return rc;
230 void __exit zcrypt_cex2a_exit(void)
232 ap_driver_unregister(&zcrypt_cex2a_queue_driver);
233 ap_driver_unregister(&zcrypt_cex2a_card_driver);
236 module_init(zcrypt_cex2a_init);
237 module_exit(zcrypt_cex2a_exit);