blob: 600604782b65e972705d01984568134949e61039 [file] [log] [blame]
Martin Schwidefsky6684af12006-09-20 15:58:32 +02001/*
Ralph Wuerthner54321142006-09-20 15:58:36 +02002 * zcrypt 2.1.0
Martin Schwidefsky6684af12006-09-20 15:58:32 +02003 *
Holger Dengler5e55a482012-08-28 16:45:36 +02004 * Copyright IBM Corp. 2001, 2012
Martin Schwidefsky6684af12006-09-20 15:58:32 +02005 * 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>
Holger Dengler5e55a482012-08-28 16:45:36 +020011 * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com>
Martin Schwidefsky6684af12006-09-20 15:58:32 +020012 *
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>
29#include <linux/init.h>
30#include <linux/err.h>
31#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Arun Sharma600634972011-07-26 16:09:06 -070033#include <linux/atomic.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080034#include <linux/uaccess.h>
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +020035#include <linux/mod_devicetable.h>
Martin Schwidefsky6684af12006-09-20 15:58:32 +020036
37#include "ap_bus.h"
38#include "zcrypt_api.h"
39#include "zcrypt_error.h"
Holger Dengler5e55a482012-08-28 16:45:36 +020040#include "zcrypt_msgtype6.h"
Martin Schwidefsky6684af12006-09-20 15:58:32 +020041#include "zcrypt_pcixcc.h"
42#include "zcrypt_cca_key.h"
43
44#define PCIXCC_MIN_MOD_SIZE 16 /* 128 bits */
45#define PCIXCC_MIN_MOD_SIZE_OLD 64 /* 512 bits */
46#define PCIXCC_MAX_MOD_SIZE 256 /* 2048 bits */
Felix Beck8e89b6b2009-12-07 12:51:57 +010047#define CEX3C_MIN_MOD_SIZE PCIXCC_MIN_MOD_SIZE
Felix Beck2ade1fa2011-01-05 12:47:46 +010048#define CEX3C_MAX_MOD_SIZE 512 /* 4096 bits */
Martin Schwidefsky6684af12006-09-20 15:58:32 +020049
Martin Schwidefsky6684af12006-09-20 15:58:32 +020050#define PCIXCC_MAX_ICA_MESSAGE_SIZE 0x77c /* max size type6 v2 crt message */
51#define PCIXCC_MAX_ICA_RESPONSE_SIZE 0x77c /* max size type86 v2 reply */
52
53#define PCIXCC_MAX_XCRB_MESSAGE_SIZE (12*1024)
Martin Schwidefsky6684af12006-09-20 15:58:32 +020054
55#define PCIXCC_CLEANUP_TIME (15*HZ)
56
Ralph Wuerthner54321142006-09-20 15:58:36 +020057#define CEIL4(x) ((((x)+3)/4)*4)
58
59struct response_type {
60 struct completion work;
61 int type;
62};
63#define PCIXCC_RESPONSE_TYPE_ICA 0
64#define PCIXCC_RESPONSE_TYPE_XCRB 1
65
Martin Schwidefsky6684af12006-09-20 15:58:32 +020066MODULE_AUTHOR("IBM Corporation");
Holger Dengler5e55a482012-08-28 16:45:36 +020067MODULE_DESCRIPTION("PCIXCC Cryptographic Coprocessor device driver, " \
68 "Copyright IBM Corp. 2001, 2012");
Martin Schwidefsky6684af12006-09-20 15:58:32 +020069MODULE_LICENSE("GPL");
Martin Schwidefsky6684af12006-09-20 15:58:32 +020070
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +020071static struct ap_device_id zcrypt_pcixcc_card_ids[] = {
72 { .dev_type = AP_DEVICE_TYPE_PCIXCC,
73 .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
74 { .dev_type = AP_DEVICE_TYPE_CEX2C,
75 .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
76 { .dev_type = AP_DEVICE_TYPE_CEX3C,
77 .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
78 { /* end of list */ },
Martin Schwidefsky6684af12006-09-20 15:58:32 +020079};
80
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +020081MODULE_DEVICE_TABLE(ap, zcrypt_pcixcc_card_ids);
Martin Schwidefsky6684af12006-09-20 15:58:32 +020082
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +020083static struct ap_device_id zcrypt_pcixcc_queue_ids[] = {
84 { .dev_type = AP_DEVICE_TYPE_PCIXCC,
85 .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
86 { .dev_type = AP_DEVICE_TYPE_CEX2C,
87 .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
88 { .dev_type = AP_DEVICE_TYPE_CEX3C,
89 .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
90 { /* end of list */ },
91};
Martin Schwidefsky6684af12006-09-20 15:58:32 +020092
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +020093MODULE_DEVICE_TABLE(ap, zcrypt_pcixcc_queue_ids);
Martin Schwidefsky6684af12006-09-20 15:58:32 +020094
95/**
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +020096 * Large random number detection function. Its sends a message to a pcixcc
97 * card to find out if large random numbers are supported.
98 * @ap_dev: pointer to the AP device.
99 *
100 * Returns 1 if large random numbers are supported, 0 if not and < 0 on error.
101 */
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200102static int zcrypt_pcixcc_rng_supported(struct ap_queue *aq)
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +0200103{
104 struct ap_message ap_msg;
105 unsigned long long psmid;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200106 unsigned int domain;
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +0200107 struct {
108 struct type86_hdr hdr;
109 struct type86_fmt2_ext fmt2;
110 struct CPRBX cprbx;
111 } __attribute__((packed)) *reply;
Ingo Tuchscherer34a15162016-08-25 11:14:15 +0200112 struct {
113 struct type6_hdr hdr;
114 struct CPRBX cprbx;
115 char function_code[2];
116 short int rule_length;
117 char rule[8];
118 short int verb_length;
119 short int key_length;
120 } __packed * msg;
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +0200121 int rc, i;
122
Felix Beck468ffdd2009-12-07 12:51:54 +0100123 ap_init_message(&ap_msg);
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +0200124 ap_msg.message = (void *) get_zeroed_page(GFP_KERNEL);
125 if (!ap_msg.message)
126 return -ENOMEM;
127
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200128 rng_type6CPRB_msgX(&ap_msg, 4, &domain);
Ingo Tuchscherer34a15162016-08-25 11:14:15 +0200129
130 msg = ap_msg.message;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200131 msg->cprbx.domain = AP_QID_QUEUE(aq->qid);
Ingo Tuchscherer34a15162016-08-25 11:14:15 +0200132
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200133 rc = ap_send(aq->qid, 0x0102030405060708ULL, ap_msg.message,
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +0200134 ap_msg.length);
135 if (rc)
136 goto out_free;
137
138 /* Wait for the test message to complete. */
139 for (i = 0; i < 2 * HZ; i++) {
140 msleep(1000 / HZ);
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200141 rc = ap_recv(aq->qid, &psmid, ap_msg.message, 4096);
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +0200142 if (rc == 0 && psmid == 0x0102030405060708ULL)
143 break;
144 }
145
146 if (i >= 2 * HZ) {
147 /* Got no answer. */
148 rc = -ENODEV;
149 goto out_free;
150 }
151
152 reply = ap_msg.message;
153 if (reply->cprbx.ccp_rtcode == 0 && reply->cprbx.ccp_rscode == 0)
154 rc = 1;
155 else
156 rc = 0;
157out_free:
158 free_page((unsigned long) ap_msg.message);
159 return rc;
160}
161
162/**
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200163 * Probe function for PCIXCC/CEX2C card devices. It always accepts the
164 * AP device since the bus_match already checked the hardware type. The
165 * PCIXCC cards come in two flavours: micro code level 2 and micro code
166 * level 3. This is checked by sending a test message to the device.
167 * @ap_dev: pointer to the AP card device.
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200168 */
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200169static int zcrypt_pcixcc_card_probe(struct ap_device *ap_dev)
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200170{
Ingo Tuchscherer34a15162016-08-25 11:14:15 +0200171 /*
172 * Normalized speed ratings per crypto adapter
173 * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
174 */
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200175 static const int CEX2C_SPEED_IDX[] = {
176 1000, 1400, 2400, 1100, 1500, 2600, 100, 12};
177 static const int CEX3C_SPEED_IDX[] = {
178 500, 700, 1400, 550, 800, 1500, 80, 10};
179
180 struct ap_card *ac = to_ap_card(&ap_dev->device);
181 struct zcrypt_card *zc;
Felix Beck8e89b6b2009-12-07 12:51:57 +0100182 int rc = 0;
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200183
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200184 zc = zcrypt_card_alloc();
185 if (!zc)
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200186 return -ENOMEM;
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200187 zc->card = ac;
188 ac->private = zc;
189 switch (ac->ap_dev.device_type) {
Felix Beck8e89b6b2009-12-07 12:51:57 +0100190 case AP_DEVICE_TYPE_CEX2C:
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200191 zc->user_space_type = ZCRYPT_CEX2C;
192 zc->type_string = "CEX2C";
193 memcpy(zc->speed_rating, CEX2C_SPEED_IDX,
Ingo Tuchscherer34a15162016-08-25 11:14:15 +0200194 sizeof(CEX2C_SPEED_IDX));
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200195 zc->min_mod_size = PCIXCC_MIN_MOD_SIZE;
196 zc->max_mod_size = PCIXCC_MAX_MOD_SIZE;
197 zc->max_exp_bit_length = PCIXCC_MAX_MOD_SIZE;
Felix Beck8e89b6b2009-12-07 12:51:57 +0100198 break;
199 case AP_DEVICE_TYPE_CEX3C:
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200200 zc->user_space_type = ZCRYPT_CEX3C;
201 zc->type_string = "CEX3C";
202 memcpy(zc->speed_rating, CEX3C_SPEED_IDX,
Ingo Tuchscherer34a15162016-08-25 11:14:15 +0200203 sizeof(CEX3C_SPEED_IDX));
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200204 zc->min_mod_size = CEX3C_MIN_MOD_SIZE;
205 zc->max_mod_size = CEX3C_MAX_MOD_SIZE;
206 zc->max_exp_bit_length = CEX3C_MAX_MOD_SIZE;
Felix Beck8e89b6b2009-12-07 12:51:57 +0100207 break;
208 default:
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200209 zcrypt_card_free(zc);
210 return -ENODEV;
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200211 }
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200212 zc->online = 1;
Felix Beck8e89b6b2009-12-07 12:51:57 +0100213
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200214 rc = zcrypt_card_register(zc);
215 if (rc) {
216 ac->private = NULL;
217 zcrypt_card_free(zc);
Ralph Wuerthner2f7c8bd2008-04-17 07:46:15 +0200218 }
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200219
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200220 return rc;
221}
222
223/**
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200224 * This is called to remove the PCIXCC/CEX2C card driver information
225 * if an AP card device is removed.
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200226 */
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200227static void zcrypt_pcixcc_card_remove(struct ap_device *ap_dev)
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200228{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200229 struct zcrypt_card *zc = to_ap_card(&ap_dev->device)->private;
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200230
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200231 if (zc)
232 zcrypt_card_unregister(zc);
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200233}
234
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200235static struct ap_driver zcrypt_pcixcc_card_driver = {
236 .probe = zcrypt_pcixcc_card_probe,
237 .remove = zcrypt_pcixcc_card_remove,
238 .ids = zcrypt_pcixcc_card_ids,
239};
240
241/**
242 * Probe function for PCIXCC/CEX2C queue devices. It always accepts the
243 * AP device since the bus_match already checked the hardware type. The
244 * PCIXCC cards come in two flavours: micro code level 2 and micro code
245 * level 3. This is checked by sending a test message to the device.
246 * @ap_dev: pointer to the AP card device.
247 */
248static int zcrypt_pcixcc_queue_probe(struct ap_device *ap_dev)
249{
250 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
251 struct zcrypt_queue *zq;
252 int rc;
253
254 zq = zcrypt_queue_alloc(PCIXCC_MAX_XCRB_MESSAGE_SIZE);
255 if (!zq)
256 return -ENOMEM;
257 zq->queue = aq;
258 zq->online = 1;
259 atomic_set(&zq->load, 0);
260 rc = zcrypt_pcixcc_rng_supported(aq);
261 if (rc < 0) {
262 zcrypt_queue_free(zq);
263 return rc;
264 }
265 if (rc)
266 zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
267 MSGTYPE06_VARIANT_DEFAULT);
268 else
269 zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
270 MSGTYPE06_VARIANT_NORNG);
271 ap_queue_init_reply(aq, &zq->reply);
272 aq->request_timeout = PCIXCC_CLEANUP_TIME,
273 aq->private = zq;
274 rc = zcrypt_queue_register(zq);
275 if (rc) {
276 aq->private = NULL;
277 zcrypt_queue_free(zq);
278 }
279 return rc;
280}
281
282/**
283 * This is called to remove the PCIXCC/CEX2C queue driver information
284 * if an AP queue device is removed.
285 */
286static void zcrypt_pcixcc_queue_remove(struct ap_device *ap_dev)
287{
288 struct ap_queue *aq = to_ap_queue(&ap_dev->device);
289 struct zcrypt_queue *zq = aq->private;
290
291 ap_queue_remove(aq);
292 if (zq)
293 zcrypt_queue_unregister(zq);
294}
295
296static struct ap_driver zcrypt_pcixcc_queue_driver = {
297 .probe = zcrypt_pcixcc_queue_probe,
298 .remove = zcrypt_pcixcc_queue_remove,
299 .suspend = ap_queue_suspend,
300 .resume = ap_queue_resume,
301 .ids = zcrypt_pcixcc_queue_ids,
302};
303
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200304int __init zcrypt_pcixcc_init(void)
305{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200306 int rc;
307
308 rc = ap_driver_register(&zcrypt_pcixcc_card_driver,
309 THIS_MODULE, "pcixcccard");
310 if (rc)
311 return rc;
312
313 rc = ap_driver_register(&zcrypt_pcixcc_queue_driver,
314 THIS_MODULE, "pcixccqueue");
315 if (rc)
316 ap_driver_unregister(&zcrypt_pcixcc_card_driver);
317
318 return rc;
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200319}
320
321void zcrypt_pcixcc_exit(void)
322{
Ingo Tuchscherere28d2af2016-08-25 11:16:03 +0200323 ap_driver_unregister(&zcrypt_pcixcc_queue_driver);
324 ap_driver_unregister(&zcrypt_pcixcc_card_driver);
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200325}
326
Martin Schwidefsky6684af12006-09-20 15:58:32 +0200327module_init(zcrypt_pcixcc_init);
328module_exit(zcrypt_pcixcc_exit);