1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * zcrypt 2.1.0 4 * 5 * Copyright IBM Corp. 2001, 2012 6 * Author(s): Robert Burroughs 7 * Eric Rossman (edrossma@us.ibm.com) 8 * 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> 13 */ 14 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> 22 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" 28 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 */ 33 34 #define CEX2A_MAX_MESSAGE_SIZE 0x390 /* sizeof(struct type50_crb2_msg) */ 35 #define CEX2A_MAX_RESPONSE_SIZE 0x110 /* max outputdatalength + type80_hdr */ 36 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) 41 42 #define CEX2A_CLEANUP_TIME (15*HZ) 43 #define CEX3A_CLEANUP_TIME CEX2A_CLEANUP_TIME 44 45 MODULE_AUTHOR("IBM Corporation"); 46 MODULE_DESCRIPTION("CEX2A Cryptographic Coprocessor device driver, " \ 47 "Copyright IBM Corp. 2001, 2012"); 48 MODULE_LICENSE("GPL"); 49 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 */ }, 56 }; 57 58 MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_card_ids); 59 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 */ }, 66 }; 67 68 MODULE_DEVICE_TABLE(ap, zcrypt_cex2a_queue_ids); 69 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. 74 */ 75 static int zcrypt_cex2a_card_probe(struct ap_device *ap_dev) 76 { 77 /* 78 * Normalized speed ratings per crypto adapter 79 * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY 80 */ 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}; 85 86 struct ap_card *ac = to_ap_card(&ap_dev->device); 87 struct zcrypt_card *zc; 88 int rc = 0; 89 90 zc = zcrypt_card_alloc(); 91 if (!zc) 92 return -ENOMEM; 93 zc->card = ac; 94 ac->private = zc; 95 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; 112 } 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; 120 } 121 zc->online = 1; 122 123 rc = zcrypt_card_register(zc); 124 if (rc) { 125 ac->private = NULL; 126 zcrypt_card_free(zc); 127 } 128 129 return rc; 130 } 131 132 /** 133 * This is called to remove the CEX2A card driver information 134 * if an AP card device is removed. 135 */ 136 static void zcrypt_cex2a_card_remove(struct ap_device *ap_dev) 137 { 138 struct zcrypt_card *zc = to_ap_card(&ap_dev->device)->private; 139 140 if (zc) 141 zcrypt_card_unregister(zc); 142 } 143 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, 148 .flags = AP_DRIVER_FLAG_DEFAULT, 149 }; 150 151 /** 152 * Probe function for CEX2A queue devices. It always accepts the AP device 153 * since the bus_match already checked the queue type. 154 * @ap_dev: pointer to the AP device. 155 */ 156 static int zcrypt_cex2a_queue_probe(struct ap_device *ap_dev) 157 { 158 struct ap_queue *aq = to_ap_queue(&ap_dev->device); 159 struct zcrypt_queue *zq = NULL; 160 int rc; 161 162 switch (ap_dev->device_type) { 163 case AP_DEVICE_TYPE_CEX2A: 164 zq = zcrypt_queue_alloc(CEX2A_MAX_RESPONSE_SIZE); 165 if (!zq) 166 return -ENOMEM; 167 break; 168 case AP_DEVICE_TYPE_CEX3A: 169 zq = zcrypt_queue_alloc(CEX3A_MAX_RESPONSE_SIZE); 170 if (!zq) 171 return -ENOMEM; 172 break; 173 } 174 if (!zq) 175 return -ENODEV; 176 zq->ops = zcrypt_msgtype(MSGTYPE50_NAME, MSGTYPE50_VARIANT_DEFAULT); 177 zq->queue = aq; 178 zq->online = 1; 179 atomic_set(&zq->load, 0); 180 ap_queue_init_reply(aq, &zq->reply); 181 aq->request_timeout = CEX2A_CLEANUP_TIME, 182 aq->private = zq; 183 rc = zcrypt_queue_register(zq); 184 if (rc) { 185 aq->private = NULL; 186 zcrypt_queue_free(zq); 187 } 188 189 return rc; 190 } 191 192 /** 193 * This is called to remove the CEX2A queue driver information 194 * if an AP queue device is removed. 195 */ 196 static void zcrypt_cex2a_queue_remove(struct ap_device *ap_dev) 197 { 198 struct ap_queue *aq = to_ap_queue(&ap_dev->device); 199 struct zcrypt_queue *zq = aq->private; 200 201 ap_queue_remove(aq); 202 if (zq) 203 zcrypt_queue_unregister(zq); 204 } 205 206 static struct ap_driver zcrypt_cex2a_queue_driver = { 207 .probe = zcrypt_cex2a_queue_probe, 208 .remove = zcrypt_cex2a_queue_remove, 209 .suspend = ap_queue_suspend, 210 .resume = ap_queue_resume, 211 .ids = zcrypt_cex2a_queue_ids, 212 .flags = AP_DRIVER_FLAG_DEFAULT, 213 }; 214 215 int __init zcrypt_cex2a_init(void) 216 { 217 int rc; 218 219 rc = ap_driver_register(&zcrypt_cex2a_card_driver, 220 THIS_MODULE, "cex2acard"); 221 if (rc) 222 return rc; 223 224 rc = ap_driver_register(&zcrypt_cex2a_queue_driver, 225 THIS_MODULE, "cex2aqueue"); 226 if (rc) 227 ap_driver_unregister(&zcrypt_cex2a_card_driver); 228 229 return rc; 230 } 231 232 void __exit zcrypt_cex2a_exit(void) 233 { 234 ap_driver_unregister(&zcrypt_cex2a_queue_driver); 235 ap_driver_unregister(&zcrypt_cex2a_card_driver); 236 } 237 238 module_init(zcrypt_cex2a_init); 239 module_exit(zcrypt_cex2a_exit); 240