blob: 15104aaa075a41db8728f6bd412ff44636e6f6ee [file] [log] [blame]
Martin Schwidefsky963ed932006-09-20 15:58:29 +02001/*
Ralph Wuerthner54321142006-09-20 15:58:36 +02002 * zcrypt 2.1.0
Martin Schwidefsky963ed932006-09-20 15:58:29 +02003 *
Holger Dengler5e55a482012-08-28 16:45:36 +02004 * Copyright IBM Corp. 2001, 2012
Martin Schwidefsky963ed932006-09-20 15:58:29 +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 Schwidefsky963ed932006-09-20 15:58:29 +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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Martin Schwidefsky963ed932006-09-20 15:58:29 +020030#include <linux/init.h>
31#include <linux/err.h>
Arun Sharma600634972011-07-26 16:09:06 -070032#include <linux/atomic.h>
Martin Schwidefsky963ed932006-09-20 15:58:29 +020033#include <asm/uaccess.h>
34
35#include "ap_bus.h"
36#include "zcrypt_api.h"
37#include "zcrypt_error.h"
38#include "zcrypt_cex2a.h"
Holger Dengler5e55a482012-08-28 16:45:36 +020039#include "zcrypt_msgtype50.h"
Martin Schwidefsky963ed932006-09-20 15:58:29 +020040
41#define CEX2A_MIN_MOD_SIZE 1 /* 8 bits */
42#define CEX2A_MAX_MOD_SIZE 256 /* 2048 bits */
Felix Beck8e89b6b2009-12-07 12:51:57 +010043#define CEX3A_MIN_MOD_SIZE CEX2A_MIN_MOD_SIZE
Felix Beck3e309a62011-01-05 12:47:45 +010044#define CEX3A_MAX_MOD_SIZE 512 /* 4096 bits */
Martin Schwidefsky963ed932006-09-20 15:58:29 +020045
46#define CEX2A_SPEED_RATING 970
Felix Beck7a6f5cd2009-12-07 12:51:59 +010047#define CEX3A_SPEED_RATING 900 /* Fixme: Needs finetuning */
Martin Schwidefsky963ed932006-09-20 15:58:29 +020048
49#define CEX2A_MAX_MESSAGE_SIZE 0x390 /* sizeof(struct type50_crb2_msg) */
50#define CEX2A_MAX_RESPONSE_SIZE 0x110 /* max outputdatalength + type80_hdr */
51
Felix Beck3e309a62011-01-05 12:47:45 +010052#define CEX3A_MAX_RESPONSE_SIZE 0x210 /* 512 bit modulus
53 * (max outputdatalength) +
54 * type80_hdr*/
55#define CEX3A_MAX_MESSAGE_SIZE sizeof(struct type50_crb3_msg)
Felix Beck8e89b6b2009-12-07 12:51:57 +010056
Martin Schwidefsky963ed932006-09-20 15:58:29 +020057#define CEX2A_CLEANUP_TIME (15*HZ)
Felix Beck8e89b6b2009-12-07 12:51:57 +010058#define CEX3A_CLEANUP_TIME CEX2A_CLEANUP_TIME
Martin Schwidefsky963ed932006-09-20 15:58:29 +020059
60static struct ap_device_id zcrypt_cex2a_ids[] = {
61 { AP_DEVICE(AP_DEVICE_TYPE_CEX2A) },
Felix Beckffda4f72009-12-07 12:51:56 +010062 { AP_DEVICE(AP_DEVICE_TYPE_CEX3A) },
Martin Schwidefsky963ed932006-09-20 15:58:29 +020063 { /* end of list */ },
64};
65
Martin Schwidefsky963ed932006-09-20 15:58:29 +020066MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_ids);
67MODULE_AUTHOR("IBM Corporation");
Holger Dengler5e55a482012-08-28 16:45:36 +020068MODULE_DESCRIPTION("CEX2A Cryptographic Coprocessor device driver, " \
69 "Copyright IBM Corp. 2001, 2012");
Martin Schwidefsky963ed932006-09-20 15:58:29 +020070MODULE_LICENSE("GPL");
Martin Schwidefsky963ed932006-09-20 15:58:29 +020071
72static int zcrypt_cex2a_probe(struct ap_device *ap_dev);
73static void zcrypt_cex2a_remove(struct ap_device *ap_dev);
Martin Schwidefsky963ed932006-09-20 15:58:29 +020074
75static struct ap_driver zcrypt_cex2a_driver = {
76 .probe = zcrypt_cex2a_probe,
77 .remove = zcrypt_cex2a_remove,
Martin Schwidefsky963ed932006-09-20 15:58:29 +020078 .ids = zcrypt_cex2a_ids,
Ralph Wuerthneraf512ed02007-07-10 11:24:19 +020079 .request_timeout = CEX2A_CLEANUP_TIME,
Martin Schwidefsky963ed932006-09-20 15:58:29 +020080};
81
82/**
Martin Schwidefsky963ed932006-09-20 15:58:29 +020083 * Probe function for CEX2A cards. It always accepts the AP device
84 * since the bus_match already checked the hardware type.
85 * @ap_dev: pointer to the AP device.
86 */
87static int zcrypt_cex2a_probe(struct ap_device *ap_dev)
88{
Felix Beck8e89b6b2009-12-07 12:51:57 +010089 struct zcrypt_device *zdev = NULL;
90 int rc = 0;
Martin Schwidefsky963ed932006-09-20 15:58:29 +020091
Felix Beck8e89b6b2009-12-07 12:51:57 +010092 switch (ap_dev->device_type) {
93 case AP_DEVICE_TYPE_CEX2A:
94 zdev = zcrypt_device_alloc(CEX2A_MAX_RESPONSE_SIZE);
95 if (!zdev)
96 return -ENOMEM;
97 zdev->user_space_type = ZCRYPT_CEX2A;
98 zdev->type_string = "CEX2A";
99 zdev->min_mod_size = CEX2A_MIN_MOD_SIZE;
100 zdev->max_mod_size = CEX2A_MAX_MOD_SIZE;
101 zdev->short_crt = 1;
102 zdev->speed_rating = CEX2A_SPEED_RATING;
Felix Beckc2567f82011-01-05 12:47:47 +0100103 zdev->max_exp_bit_length = CEX2A_MAX_MOD_SIZE;
Felix Beck8e89b6b2009-12-07 12:51:57 +0100104 break;
105 case AP_DEVICE_TYPE_CEX3A:
106 zdev = zcrypt_device_alloc(CEX3A_MAX_RESPONSE_SIZE);
107 if (!zdev)
108 return -ENOMEM;
109 zdev->user_space_type = ZCRYPT_CEX3A;
110 zdev->type_string = "CEX3A";
Felix Beck3e309a62011-01-05 12:47:45 +0100111 zdev->min_mod_size = CEX2A_MIN_MOD_SIZE;
112 zdev->max_mod_size = CEX2A_MAX_MOD_SIZE;
Felix Beckc2567f82011-01-05 12:47:47 +0100113 zdev->max_exp_bit_length = CEX2A_MAX_MOD_SIZE;
Holger Denglerb26bd942012-08-28 16:43:48 +0200114 if (ap_test_bit(&ap_dev->functions, AP_FUNC_MEX4K) &&
115 ap_test_bit(&ap_dev->functions, AP_FUNC_CRT4K)) {
Felix Beck3e309a62011-01-05 12:47:45 +0100116 zdev->max_mod_size = CEX3A_MAX_MOD_SIZE;
Felix Beckc2567f82011-01-05 12:47:47 +0100117 zdev->max_exp_bit_length = CEX3A_MAX_MOD_SIZE;
118 }
Felix Beck8e89b6b2009-12-07 12:51:57 +0100119 zdev->short_crt = 1;
120 zdev->speed_rating = CEX3A_SPEED_RATING;
121 break;
122 }
Holger Dengler5e55a482012-08-28 16:45:36 +0200123 if (!zdev)
124 return -ENODEV;
125 zdev->ops = zcrypt_msgtype_request(MSGTYPE50_NAME,
126 MSGTYPE50_VARIANT_DEFAULT);
127 zdev->ap_dev = ap_dev;
128 zdev->online = 1;
Ingo Tuchschererd6d86c52016-07-25 14:52:28 +0200129 ap_device_init_reply(ap_dev, &zdev->reply);
Holger Dengler5e55a482012-08-28 16:45:36 +0200130 ap_dev->private = zdev;
131 rc = zcrypt_device_register(zdev);
Felix Beck8e89b6b2009-12-07 12:51:57 +0100132 if (rc) {
133 ap_dev->private = NULL;
Holger Dengler5e55a482012-08-28 16:45:36 +0200134 zcrypt_msgtype_release(zdev->ops);
Felix Beck8e89b6b2009-12-07 12:51:57 +0100135 zcrypt_device_free(zdev);
136 }
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200137 return rc;
138}
139
140/**
141 * This is called to remove the extended CEX2A driver information
142 * if an AP device is removed.
143 */
144static void zcrypt_cex2a_remove(struct ap_device *ap_dev)
145{
146 struct zcrypt_device *zdev = ap_dev->private;
Holger Dengler5e55a482012-08-28 16:45:36 +0200147 struct zcrypt_ops *zops = zdev->ops;
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200148
149 zcrypt_device_unregister(zdev);
Holger Dengler5e55a482012-08-28 16:45:36 +0200150 zcrypt_msgtype_release(zops);
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200151}
152
153int __init zcrypt_cex2a_init(void)
154{
155 return ap_driver_register(&zcrypt_cex2a_driver, THIS_MODULE, "cex2a");
156}
157
158void __exit zcrypt_cex2a_exit(void)
159{
160 ap_driver_unregister(&zcrypt_cex2a_driver);
161}
162
Martin Schwidefsky963ed932006-09-20 15:58:29 +0200163module_init(zcrypt_cex2a_init);
164module_exit(zcrypt_cex2a_exit);