1 /* 2 * Copyright IBM Corp. 2012 3 * Author(s): Holger Dengler <hd@linux.vnet.ibm.com> 4 */ 5 6 #include <linux/module.h> 7 #include <linux/slab.h> 8 #include <linux/init.h> 9 #include <linux/err.h> 10 #include <linux/atomic.h> 11 #include <linux/uaccess.h> 12 #include <linux/mod_devicetable.h> 13 14 #include "ap_bus.h" 15 #include "zcrypt_api.h" 16 #include "zcrypt_msgtype6.h" 17 #include "zcrypt_msgtype50.h" 18 #include "zcrypt_error.h" 19 #include "zcrypt_cex4.h" 20 21 #define CEX4A_MIN_MOD_SIZE 1 /* 8 bits */ 22 #define CEX4A_MAX_MOD_SIZE_2K 256 /* 2048 bits */ 23 #define CEX4A_MAX_MOD_SIZE_4K 512 /* 4096 bits */ 24 25 #define CEX4C_MIN_MOD_SIZE 16 /* 256 bits */ 26 #define CEX4C_MAX_MOD_SIZE 512 /* 4096 bits */ 27 28 #define CEX4A_MAX_MESSAGE_SIZE MSGTYPE50_CRB3_MAX_MSG_SIZE 29 #define CEX4C_MAX_MESSAGE_SIZE MSGTYPE06_MAX_MSG_SIZE 30 31 /* Waiting time for requests to be processed. 32 * Currently there are some types of request which are not deterministic. 33 * But the maximum time limit managed by the stomper code is set to 60sec. 34 * Hence we have to wait at least that time period. 35 */ 36 #define CEX4_CLEANUP_TIME (900*HZ) 37 38 MODULE_AUTHOR("IBM Corporation"); 39 MODULE_DESCRIPTION("CEX4 Cryptographic Card device driver, " \ 40 "Copyright IBM Corp. 2012"); 41 MODULE_LICENSE("GPL"); 42 43 static struct ap_device_id zcrypt_cex4_card_ids[] = { 44 { .dev_type = AP_DEVICE_TYPE_CEX4, 45 .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE }, 46 { .dev_type = AP_DEVICE_TYPE_CEX5, 47 .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE }, 48 { .dev_type = AP_DEVICE_TYPE_CEX6, 49 .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE }, 50 { /* end of list */ }, 51 }; 52 53 MODULE_DEVICE_TABLE(ap, zcrypt_cex4_card_ids); 54 55 static struct ap_device_id zcrypt_cex4_queue_ids[] = { 56 { .dev_type = AP_DEVICE_TYPE_CEX4, 57 .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE }, 58 { .dev_type = AP_DEVICE_TYPE_CEX5, 59 .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE }, 60 { .dev_type = AP_DEVICE_TYPE_CEX6, 61 .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE }, 62 { /* end of list */ }, 63 }; 64 65 MODULE_DEVICE_TABLE(ap, zcrypt_cex4_queue_ids); 66 67 /** 68 * Probe function for CEX4 card device. It always accepts the AP device 69 * since the bus_match already checked the hardware type. 70 * @ap_dev: pointer to the AP device. 71 */ 72 static int zcrypt_cex4_card_probe(struct ap_device *ap_dev) 73 { 74 /* 75 * Normalized speed ratings per crypto adapter 76 * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY 77 */ 78 static const int CEX4A_SPEED_IDX[] = { 79 14, 19, 249, 42, 228, 1458, 0, 0}; 80 static const int CEX5A_SPEED_IDX[] = { 81 8, 9, 20, 18, 66, 458, 0, 0}; 82 static const int CEX6A_SPEED_IDX[] = { 83 6, 9, 20, 17, 65, 438, 0, 0}; 84 85 static const int CEX4C_SPEED_IDX[] = { 86 59, 69, 308, 83, 278, 2204, 209, 40}; 87 static const int CEX5C_SPEED_IDX[] = { 88 24, 31, 50, 37, 90, 479, 27, 10}; 89 static const int CEX6C_SPEED_IDX[] = { 90 16, 20, 32, 27, 77, 455, 23, 9}; 91 92 static const int CEX4P_SPEED_IDX[] = { 93 224, 313, 3560, 359, 605, 2827, 0, 50}; 94 static const int CEX5P_SPEED_IDX[] = { 95 63, 84, 156, 83, 142, 533, 0, 10}; 96 static const int CEX6P_SPEED_IDX[] = { 97 55, 70, 121, 73, 129, 522, 0, 9}; 98 99 struct ap_card *ac = to_ap_card(&ap_dev->device); 100 struct zcrypt_card *zc; 101 int rc = 0; 102 103 zc = zcrypt_card_alloc(); 104 if (!zc) 105 return -ENOMEM; 106 zc->card = ac; 107 ac->private = zc; 108 if (ap_test_bit(&ac->functions, AP_FUNC_ACCEL)) { 109 if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) { 110 zc->type_string = "CEX4A"; 111 zc->user_space_type = ZCRYPT_CEX4; 112 memcpy(zc->speed_rating, CEX4A_SPEED_IDX, 113 sizeof(CEX4A_SPEED_IDX)); 114 } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) { 115 zc->type_string = "CEX5A"; 116 zc->user_space_type = ZCRYPT_CEX5; 117 memcpy(zc->speed_rating, CEX5A_SPEED_IDX, 118 sizeof(CEX5A_SPEED_IDX)); 119 } else { 120 zc->type_string = "CEX6A"; 121 zc->user_space_type = ZCRYPT_CEX6; 122 memcpy(zc->speed_rating, CEX6A_SPEED_IDX, 123 sizeof(CEX6A_SPEED_IDX)); 124 } 125 zc->min_mod_size = CEX4A_MIN_MOD_SIZE; 126 if (ap_test_bit(&ac->functions, AP_FUNC_MEX4K) && 127 ap_test_bit(&ac->functions, AP_FUNC_CRT4K)) { 128 zc->max_mod_size = CEX4A_MAX_MOD_SIZE_4K; 129 zc->max_exp_bit_length = 130 CEX4A_MAX_MOD_SIZE_4K; 131 } else { 132 zc->max_mod_size = CEX4A_MAX_MOD_SIZE_2K; 133 zc->max_exp_bit_length = 134 CEX4A_MAX_MOD_SIZE_2K; 135 } 136 } else if (ap_test_bit(&ac->functions, AP_FUNC_COPRO)) { 137 if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) { 138 zc->type_string = "CEX4C"; 139 /* wrong user space type, must be CEX4 140 * just keep it for cca compatibility 141 */ 142 zc->user_space_type = ZCRYPT_CEX3C; 143 memcpy(zc->speed_rating, CEX4C_SPEED_IDX, 144 sizeof(CEX4C_SPEED_IDX)); 145 } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) { 146 zc->type_string = "CEX5C"; 147 /* wrong user space type, must be CEX5 148 * just keep it for cca compatibility 149 */ 150 zc->user_space_type = ZCRYPT_CEX3C; 151 memcpy(zc->speed_rating, CEX5C_SPEED_IDX, 152 sizeof(CEX5C_SPEED_IDX)); 153 } else { 154 zc->type_string = "CEX6C"; 155 /* wrong user space type, must be CEX6 156 * just keep it for cca compatibility 157 */ 158 zc->user_space_type = ZCRYPT_CEX3C; 159 memcpy(zc->speed_rating, CEX6C_SPEED_IDX, 160 sizeof(CEX6C_SPEED_IDX)); 161 } 162 zc->min_mod_size = CEX4C_MIN_MOD_SIZE; 163 zc->max_mod_size = CEX4C_MAX_MOD_SIZE; 164 zc->max_exp_bit_length = CEX4C_MAX_MOD_SIZE; 165 } else if (ap_test_bit(&ac->functions, AP_FUNC_EP11)) { 166 if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) { 167 zc->type_string = "CEX4P"; 168 zc->user_space_type = ZCRYPT_CEX4; 169 memcpy(zc->speed_rating, CEX4P_SPEED_IDX, 170 sizeof(CEX4P_SPEED_IDX)); 171 } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) { 172 zc->type_string = "CEX5P"; 173 zc->user_space_type = ZCRYPT_CEX5; 174 memcpy(zc->speed_rating, CEX5P_SPEED_IDX, 175 sizeof(CEX5P_SPEED_IDX)); 176 } else { 177 zc->type_string = "CEX6P"; 178 zc->user_space_type = ZCRYPT_CEX6; 179 memcpy(zc->speed_rating, CEX6P_SPEED_IDX, 180 sizeof(CEX6P_SPEED_IDX)); 181 } 182 zc->min_mod_size = CEX4C_MIN_MOD_SIZE; 183 zc->max_mod_size = CEX4C_MAX_MOD_SIZE; 184 zc->max_exp_bit_length = CEX4C_MAX_MOD_SIZE; 185 } else { 186 zcrypt_card_free(zc); 187 return -ENODEV; 188 } 189 zc->online = 1; 190 191 rc = zcrypt_card_register(zc); 192 if (rc) { 193 ac->private = NULL; 194 zcrypt_card_free(zc); 195 } 196 197 return rc; 198 } 199 200 /** 201 * This is called to remove the CEX4 card driver information 202 * if an AP card device is removed. 203 */ 204 static void zcrypt_cex4_card_remove(struct ap_device *ap_dev) 205 { 206 struct zcrypt_card *zc = to_ap_card(&ap_dev->device)->private; 207 208 if (zc) 209 zcrypt_card_unregister(zc); 210 } 211 212 static struct ap_driver zcrypt_cex4_card_driver = { 213 .probe = zcrypt_cex4_card_probe, 214 .remove = zcrypt_cex4_card_remove, 215 .ids = zcrypt_cex4_card_ids, 216 }; 217 218 /** 219 * Probe function for CEX4 queue device. It always accepts the AP device 220 * since the bus_match already checked the hardware type. 221 * @ap_dev: pointer to the AP device. 222 */ 223 static int zcrypt_cex4_queue_probe(struct ap_device *ap_dev) 224 { 225 struct ap_queue *aq = to_ap_queue(&ap_dev->device); 226 struct zcrypt_queue *zq; 227 int rc; 228 229 if (ap_test_bit(&aq->card->functions, AP_FUNC_ACCEL)) { 230 zq = zcrypt_queue_alloc(CEX4A_MAX_MESSAGE_SIZE); 231 if (!zq) 232 return -ENOMEM; 233 zq->ops = zcrypt_msgtype(MSGTYPE50_NAME, 234 MSGTYPE50_VARIANT_DEFAULT); 235 } else if (ap_test_bit(&aq->card->functions, AP_FUNC_COPRO)) { 236 zq = zcrypt_queue_alloc(CEX4C_MAX_MESSAGE_SIZE); 237 if (!zq) 238 return -ENOMEM; 239 zq->ops = zcrypt_msgtype(MSGTYPE06_NAME, 240 MSGTYPE06_VARIANT_DEFAULT); 241 } else if (ap_test_bit(&aq->card->functions, AP_FUNC_EP11)) { 242 zq = zcrypt_queue_alloc(CEX4C_MAX_MESSAGE_SIZE); 243 if (!zq) 244 return -ENOMEM; 245 zq->ops = zcrypt_msgtype(MSGTYPE06_NAME, 246 MSGTYPE06_VARIANT_EP11); 247 } else { 248 return -ENODEV; 249 } 250 zq->queue = aq; 251 zq->online = 1; 252 atomic_set(&zq->load, 0); 253 ap_queue_init_reply(aq, &zq->reply); 254 aq->request_timeout = CEX4_CLEANUP_TIME, 255 aq->private = zq; 256 rc = zcrypt_queue_register(zq); 257 if (rc) { 258 aq->private = NULL; 259 zcrypt_queue_free(zq); 260 } 261 262 return rc; 263 } 264 265 /** 266 * This is called to remove the CEX4 queue driver information 267 * if an AP queue device is removed. 268 */ 269 static void zcrypt_cex4_queue_remove(struct ap_device *ap_dev) 270 { 271 struct ap_queue *aq = to_ap_queue(&ap_dev->device); 272 struct zcrypt_queue *zq = aq->private; 273 274 ap_queue_remove(aq); 275 if (zq) 276 zcrypt_queue_unregister(zq); 277 } 278 279 static struct ap_driver zcrypt_cex4_queue_driver = { 280 .probe = zcrypt_cex4_queue_probe, 281 .remove = zcrypt_cex4_queue_remove, 282 .suspend = ap_queue_suspend, 283 .resume = ap_queue_resume, 284 .ids = zcrypt_cex4_queue_ids, 285 }; 286 287 int __init zcrypt_cex4_init(void) 288 { 289 int rc; 290 291 rc = ap_driver_register(&zcrypt_cex4_card_driver, 292 THIS_MODULE, "cex4card"); 293 if (rc) 294 return rc; 295 296 rc = ap_driver_register(&zcrypt_cex4_queue_driver, 297 THIS_MODULE, "cex4queue"); 298 if (rc) 299 ap_driver_unregister(&zcrypt_cex4_card_driver); 300 301 return rc; 302 } 303 304 void __exit zcrypt_cex4_exit(void) 305 { 306 ap_driver_unregister(&zcrypt_cex4_queue_driver); 307 ap_driver_unregister(&zcrypt_cex4_card_driver); 308 } 309 310 module_init(zcrypt_cex4_init); 311 module_exit(zcrypt_cex4_exit); 312