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