Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 1 | /* |
Ralph Wuerthner | 5432114 | 2006-09-20 15:58:36 +0200 | [diff] [blame] | 2 | * zcrypt 2.1.0 |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 3 | * |
Holger Dengler | 5e55a48 | 2012-08-28 16:45:36 +0200 | [diff] [blame] | 4 | * Copyright IBM Corp. 2001, 2012 |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 5 | * 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 Dengler | 5e55a48 | 2012-08-28 16:45:36 +0200 | [diff] [blame] | 11 | * MSGTYPE restruct: Holger Dengler <hd@linux.vnet.ibm.com> |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 12 | * |
| 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 Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 29 | #include <linux/slab.h> |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 30 | #include <linux/init.h> |
| 31 | #include <linux/err.h> |
Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 32 | #include <linux/atomic.h> |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 33 | #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 Dengler | 5e55a48 | 2012-08-28 16:45:36 +0200 | [diff] [blame] | 39 | #include "zcrypt_msgtype50.h" |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 40 | |
| 41 | #define CEX2A_MIN_MOD_SIZE 1 /* 8 bits */ |
| 42 | #define CEX2A_MAX_MOD_SIZE 256 /* 2048 bits */ |
Felix Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 43 | #define CEX3A_MIN_MOD_SIZE CEX2A_MIN_MOD_SIZE |
Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 44 | #define CEX3A_MAX_MOD_SIZE 512 /* 4096 bits */ |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 45 | |
| 46 | #define CEX2A_SPEED_RATING 970 |
Felix Beck | 7a6f5cd | 2009-12-07 12:51:59 +0100 | [diff] [blame] | 47 | #define CEX3A_SPEED_RATING 900 /* Fixme: Needs finetuning */ |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 48 | |
| 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 Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 52 | #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 Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 56 | |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 57 | #define CEX2A_CLEANUP_TIME (15*HZ) |
Felix Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 58 | #define CEX3A_CLEANUP_TIME CEX2A_CLEANUP_TIME |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 59 | |
| 60 | static struct ap_device_id zcrypt_cex2a_ids[] = { |
| 61 | { AP_DEVICE(AP_DEVICE_TYPE_CEX2A) }, |
Felix Beck | ffda4f7 | 2009-12-07 12:51:56 +0100 | [diff] [blame] | 62 | { AP_DEVICE(AP_DEVICE_TYPE_CEX3A) }, |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 63 | { /* end of list */ }, |
| 64 | }; |
| 65 | |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 66 | MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_ids); |
| 67 | MODULE_AUTHOR("IBM Corporation"); |
Holger Dengler | 5e55a48 | 2012-08-28 16:45:36 +0200 | [diff] [blame] | 68 | MODULE_DESCRIPTION("CEX2A Cryptographic Coprocessor device driver, " \ |
| 69 | "Copyright IBM Corp. 2001, 2012"); |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 70 | MODULE_LICENSE("GPL"); |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 71 | |
| 72 | static int zcrypt_cex2a_probe(struct ap_device *ap_dev); |
| 73 | static void zcrypt_cex2a_remove(struct ap_device *ap_dev); |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 74 | |
| 75 | static struct ap_driver zcrypt_cex2a_driver = { |
| 76 | .probe = zcrypt_cex2a_probe, |
| 77 | .remove = zcrypt_cex2a_remove, |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 78 | .ids = zcrypt_cex2a_ids, |
Ralph Wuerthner | af512ed0 | 2007-07-10 11:24:19 +0200 | [diff] [blame] | 79 | .request_timeout = CEX2A_CLEANUP_TIME, |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | /** |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 83 | * 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 | */ |
| 87 | static int zcrypt_cex2a_probe(struct ap_device *ap_dev) |
| 88 | { |
Felix Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 89 | struct zcrypt_device *zdev = NULL; |
| 90 | int rc = 0; |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 91 | |
Felix Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 92 | 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 Beck | c2567f8 | 2011-01-05 12:47:47 +0100 | [diff] [blame] | 103 | zdev->max_exp_bit_length = CEX2A_MAX_MOD_SIZE; |
Felix Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 104 | 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 Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 111 | zdev->min_mod_size = CEX2A_MIN_MOD_SIZE; |
| 112 | zdev->max_mod_size = CEX2A_MAX_MOD_SIZE; |
Felix Beck | c2567f8 | 2011-01-05 12:47:47 +0100 | [diff] [blame] | 113 | zdev->max_exp_bit_length = CEX2A_MAX_MOD_SIZE; |
Holger Dengler | b26bd94 | 2012-08-28 16:43:48 +0200 | [diff] [blame] | 114 | if (ap_test_bit(&ap_dev->functions, AP_FUNC_MEX4K) && |
| 115 | ap_test_bit(&ap_dev->functions, AP_FUNC_CRT4K)) { |
Felix Beck | 3e309a6 | 2011-01-05 12:47:45 +0100 | [diff] [blame] | 116 | zdev->max_mod_size = CEX3A_MAX_MOD_SIZE; |
Felix Beck | c2567f8 | 2011-01-05 12:47:47 +0100 | [diff] [blame] | 117 | zdev->max_exp_bit_length = CEX3A_MAX_MOD_SIZE; |
| 118 | } |
Felix Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 119 | zdev->short_crt = 1; |
| 120 | zdev->speed_rating = CEX3A_SPEED_RATING; |
| 121 | break; |
| 122 | } |
Holger Dengler | 5e55a48 | 2012-08-28 16:45:36 +0200 | [diff] [blame] | 123 | 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; |
| 129 | ap_dev->reply = &zdev->reply; |
| 130 | ap_dev->private = zdev; |
| 131 | rc = zcrypt_device_register(zdev); |
Felix Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 132 | if (rc) { |
| 133 | ap_dev->private = NULL; |
Holger Dengler | 5e55a48 | 2012-08-28 16:45:36 +0200 | [diff] [blame] | 134 | zcrypt_msgtype_release(zdev->ops); |
Felix Beck | 8e89b6b | 2009-12-07 12:51:57 +0100 | [diff] [blame] | 135 | zcrypt_device_free(zdev); |
| 136 | } |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 137 | return rc; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * This is called to remove the extended CEX2A driver information |
| 142 | * if an AP device is removed. |
| 143 | */ |
| 144 | static void zcrypt_cex2a_remove(struct ap_device *ap_dev) |
| 145 | { |
| 146 | struct zcrypt_device *zdev = ap_dev->private; |
Holger Dengler | 5e55a48 | 2012-08-28 16:45:36 +0200 | [diff] [blame] | 147 | struct zcrypt_ops *zops = zdev->ops; |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 148 | |
| 149 | zcrypt_device_unregister(zdev); |
Holger Dengler | 5e55a48 | 2012-08-28 16:45:36 +0200 | [diff] [blame] | 150 | zcrypt_msgtype_release(zops); |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | int __init zcrypt_cex2a_init(void) |
| 154 | { |
| 155 | return ap_driver_register(&zcrypt_cex2a_driver, THIS_MODULE, "cex2a"); |
| 156 | } |
| 157 | |
| 158 | void __exit zcrypt_cex2a_exit(void) |
| 159 | { |
| 160 | ap_driver_unregister(&zcrypt_cex2a_driver); |
| 161 | } |
| 162 | |
Martin Schwidefsky | 963ed93 | 2006-09-20 15:58:29 +0200 | [diff] [blame] | 163 | module_init(zcrypt_cex2a_init); |
| 164 | module_exit(zcrypt_cex2a_exit); |