zcrypt_cex4.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2012, 2022
  4. * Author(s): Holger Dengler <hd@linux.vnet.ibm.com>
  5. */
  6. #include <linux/module.h>
  7. #include <linux/slab.h>
  8. #include <linux/hex.h>
  9. #include <linux/init.h>
  10. #include <linux/err.h>
  11. #include <linux/atomic.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/mod_devicetable.h>
  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. #include "zcrypt_ccamisc.h"
  21. #include "zcrypt_ep11misc.h"
  22. #define CEX4A_MIN_MOD_SIZE 1 /* 8 bits */
  23. #define CEX4A_MAX_MOD_SIZE_2K 256 /* 2048 bits */
  24. #define CEX4A_MAX_MOD_SIZE_4K 512 /* 4096 bits */
  25. #define CEX4C_MIN_MOD_SIZE 16 /* 256 bits */
  26. #define CEX4C_MAX_MOD_SIZE 512 /* 4096 bits */
  27. /* Waiting time for requests to be processed.
  28. * Currently there are some types of request which are not deterministic.
  29. * But the maximum time limit managed by the stomper code is set to 60sec.
  30. * Hence we have to wait at least that time period.
  31. */
  32. #define CEX4_CLEANUP_TIME (900 * HZ)
  33. MODULE_AUTHOR("IBM Corporation");
  34. MODULE_DESCRIPTION("CEX[45678] Cryptographic Card device driver, " \
  35. "Copyright IBM Corp. 2022");
  36. MODULE_LICENSE("GPL");
  37. static struct ap_device_id zcrypt_cex4_card_ids[] = {
  38. { .dev_type = AP_DEVICE_TYPE_CEX4,
  39. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  40. { .dev_type = AP_DEVICE_TYPE_CEX5,
  41. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  42. { .dev_type = AP_DEVICE_TYPE_CEX6,
  43. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  44. { .dev_type = AP_DEVICE_TYPE_CEX7,
  45. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  46. { .dev_type = AP_DEVICE_TYPE_CEX8,
  47. .match_flags = AP_DEVICE_ID_MATCH_CARD_TYPE },
  48. { /* end of list */ },
  49. };
  50. MODULE_DEVICE_TABLE(ap, zcrypt_cex4_card_ids);
  51. static struct ap_device_id zcrypt_cex4_queue_ids[] = {
  52. { .dev_type = AP_DEVICE_TYPE_CEX4,
  53. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  54. { .dev_type = AP_DEVICE_TYPE_CEX5,
  55. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  56. { .dev_type = AP_DEVICE_TYPE_CEX6,
  57. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  58. { .dev_type = AP_DEVICE_TYPE_CEX7,
  59. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  60. { .dev_type = AP_DEVICE_TYPE_CEX8,
  61. .match_flags = AP_DEVICE_ID_MATCH_QUEUE_TYPE },
  62. { /* end of list */ },
  63. };
  64. MODULE_DEVICE_TABLE(ap, zcrypt_cex4_queue_ids);
  65. /*
  66. * CCA card additional device attributes
  67. */
  68. static ssize_t cca_serialnr_show(struct device *dev,
  69. struct device_attribute *attr,
  70. char *buf)
  71. {
  72. struct ap_card *ac = to_ap_card(dev);
  73. struct cca_info ci;
  74. memset(&ci, 0, sizeof(ci));
  75. cca_get_info(ac->id, AUTOSEL_DOM, &ci, 0);
  76. return sysfs_emit(buf, "%s\n", ci.serial);
  77. }
  78. static struct device_attribute dev_attr_cca_serialnr =
  79. __ATTR(serialnr, 0444, cca_serialnr_show, NULL);
  80. static struct attribute *cca_card_attrs[] = {
  81. &dev_attr_cca_serialnr.attr,
  82. NULL,
  83. };
  84. static const struct attribute_group cca_card_attr_grp = {
  85. .attrs = cca_card_attrs,
  86. };
  87. /*
  88. * CCA queue additional device attributes
  89. */
  90. static ssize_t cca_mkvps_show(struct device *dev,
  91. struct device_attribute *attr,
  92. char *buf)
  93. {
  94. static const char * const new_state[] = { "empty", "partial", "full" };
  95. static const char * const cao_state[] = { "invalid", "valid" };
  96. struct zcrypt_queue *zq = dev_get_drvdata(dev);
  97. struct cca_info ci;
  98. int n = 0;
  99. memset(&ci, 0, sizeof(ci));
  100. cca_get_info(AP_QID_CARD(zq->queue->qid),
  101. AP_QID_QUEUE(zq->queue->qid),
  102. &ci, 0);
  103. if (ci.new_aes_mk_state >= '1' && ci.new_aes_mk_state <= '3')
  104. n += sysfs_emit_at(buf, n, "AES NEW: %s 0x%016llx\n",
  105. new_state[ci.new_aes_mk_state - '1'],
  106. ci.new_aes_mkvp);
  107. else
  108. n += sysfs_emit_at(buf, n, "AES NEW: - -\n");
  109. if (ci.cur_aes_mk_state >= '1' && ci.cur_aes_mk_state <= '2')
  110. n += sysfs_emit_at(buf, n, "AES CUR: %s 0x%016llx\n",
  111. cao_state[ci.cur_aes_mk_state - '1'],
  112. ci.cur_aes_mkvp);
  113. else
  114. n += sysfs_emit_at(buf, n, "AES CUR: - -\n");
  115. if (ci.old_aes_mk_state >= '1' && ci.old_aes_mk_state <= '2')
  116. n += sysfs_emit_at(buf, n, "AES OLD: %s 0x%016llx\n",
  117. cao_state[ci.old_aes_mk_state - '1'],
  118. ci.old_aes_mkvp);
  119. else
  120. n += sysfs_emit_at(buf, n, "AES OLD: - -\n");
  121. if (ci.new_apka_mk_state >= '1' && ci.new_apka_mk_state <= '3')
  122. n += sysfs_emit_at(buf, n, "APKA NEW: %s 0x%016llx\n",
  123. new_state[ci.new_apka_mk_state - '1'],
  124. ci.new_apka_mkvp);
  125. else
  126. n += sysfs_emit_at(buf, n, "APKA NEW: - -\n");
  127. if (ci.cur_apka_mk_state >= '1' && ci.cur_apka_mk_state <= '2')
  128. n += sysfs_emit_at(buf, n, "APKA CUR: %s 0x%016llx\n",
  129. cao_state[ci.cur_apka_mk_state - '1'],
  130. ci.cur_apka_mkvp);
  131. else
  132. n += sysfs_emit_at(buf, n, "APKA CUR: - -\n");
  133. if (ci.old_apka_mk_state >= '1' && ci.old_apka_mk_state <= '2')
  134. n += sysfs_emit_at(buf, n, "APKA OLD: %s 0x%016llx\n",
  135. cao_state[ci.old_apka_mk_state - '1'],
  136. ci.old_apka_mkvp);
  137. else
  138. n += sysfs_emit_at(buf, n, "APKA OLD: - -\n");
  139. if (ci.new_asym_mk_state >= '1' && ci.new_asym_mk_state <= '3')
  140. n += sysfs_emit_at(buf, n, "ASYM NEW: %s 0x%016llx%016llx\n",
  141. new_state[ci.new_asym_mk_state - '1'],
  142. *((u64 *)(ci.new_asym_mkvp)),
  143. *((u64 *)(ci.new_asym_mkvp + sizeof(u64))));
  144. else
  145. n += sysfs_emit_at(buf, n, "ASYM NEW: - -\n");
  146. if (ci.cur_asym_mk_state >= '1' && ci.cur_asym_mk_state <= '2')
  147. n += sysfs_emit_at(buf, n, "ASYM CUR: %s 0x%016llx%016llx\n",
  148. cao_state[ci.cur_asym_mk_state - '1'],
  149. *((u64 *)(ci.cur_asym_mkvp)),
  150. *((u64 *)(ci.cur_asym_mkvp + sizeof(u64))));
  151. else
  152. n += sysfs_emit_at(buf, n, "ASYM CUR: - -\n");
  153. if (ci.old_asym_mk_state >= '1' && ci.old_asym_mk_state <= '2')
  154. n += sysfs_emit_at(buf, n, "ASYM OLD: %s 0x%016llx%016llx\n",
  155. cao_state[ci.old_asym_mk_state - '1'],
  156. *((u64 *)(ci.old_asym_mkvp)),
  157. *((u64 *)(ci.old_asym_mkvp + sizeof(u64))));
  158. else
  159. n += sysfs_emit_at(buf, n, "ASYM OLD: - -\n");
  160. return n;
  161. }
  162. static struct device_attribute dev_attr_cca_mkvps =
  163. __ATTR(mkvps, 0444, cca_mkvps_show, NULL);
  164. static struct attribute *cca_queue_attrs[] = {
  165. &dev_attr_cca_mkvps.attr,
  166. NULL,
  167. };
  168. static const struct attribute_group cca_queue_attr_grp = {
  169. .attrs = cca_queue_attrs,
  170. };
  171. /*
  172. * EP11 card additional device attributes
  173. */
  174. static ssize_t ep11_api_ordinalnr_show(struct device *dev,
  175. struct device_attribute *attr,
  176. char *buf)
  177. {
  178. struct ap_card *ac = to_ap_card(dev);
  179. struct ep11_card_info ci;
  180. memset(&ci, 0, sizeof(ci));
  181. ep11_get_card_info(ac->id, &ci, 0);
  182. if (ci.API_ord_nr > 0)
  183. return sysfs_emit(buf, "%u\n", ci.API_ord_nr);
  184. else
  185. return sysfs_emit(buf, "\n");
  186. }
  187. static struct device_attribute dev_attr_ep11_api_ordinalnr =
  188. __ATTR(API_ordinalnr, 0444, ep11_api_ordinalnr_show, NULL);
  189. static ssize_t ep11_fw_version_show(struct device *dev,
  190. struct device_attribute *attr,
  191. char *buf)
  192. {
  193. struct ap_card *ac = to_ap_card(dev);
  194. struct ep11_card_info ci;
  195. memset(&ci, 0, sizeof(ci));
  196. ep11_get_card_info(ac->id, &ci, 0);
  197. if (ci.FW_version > 0)
  198. return sysfs_emit(buf, "%d.%d\n",
  199. (int)(ci.FW_version >> 8),
  200. (int)(ci.FW_version & 0xFF));
  201. else
  202. return sysfs_emit(buf, "\n");
  203. }
  204. static struct device_attribute dev_attr_ep11_fw_version =
  205. __ATTR(FW_version, 0444, ep11_fw_version_show, NULL);
  206. static ssize_t ep11_serialnr_show(struct device *dev,
  207. struct device_attribute *attr,
  208. char *buf)
  209. {
  210. struct ap_card *ac = to_ap_card(dev);
  211. struct ep11_card_info ci;
  212. memset(&ci, 0, sizeof(ci));
  213. ep11_get_card_info(ac->id, &ci, 0);
  214. if (ci.serial[0])
  215. return sysfs_emit(buf, "%16.16s\n", ci.serial);
  216. else
  217. return sysfs_emit(buf, "\n");
  218. }
  219. static struct device_attribute dev_attr_ep11_serialnr =
  220. __ATTR(serialnr, 0444, ep11_serialnr_show, NULL);
  221. static const struct {
  222. int mode_bit;
  223. const char *mode_txt;
  224. } ep11_op_modes[] = {
  225. { 0, "FIPS2009" },
  226. { 1, "BSI2009" },
  227. { 2, "FIPS2011" },
  228. { 3, "BSI2011" },
  229. { 4, "SIGG-IMPORT" },
  230. { 5, "SIGG" },
  231. { 6, "BSICC2017" },
  232. { 7, "FIPS2021" },
  233. { 8, "FIPS2024" },
  234. { 0, NULL }
  235. };
  236. static ssize_t ep11_card_op_modes_show(struct device *dev,
  237. struct device_attribute *attr,
  238. char *buf)
  239. {
  240. struct ap_card *ac = to_ap_card(dev);
  241. struct ep11_card_info ci;
  242. int i, n = 0;
  243. memset(&ci, 0, sizeof(ci));
  244. ep11_get_card_info(ac->id, &ci, 0);
  245. for (i = 0; ep11_op_modes[i].mode_txt; i++) {
  246. if (ci.op_mode & (1ULL << ep11_op_modes[i].mode_bit)) {
  247. if (n > 0)
  248. buf[n++] = ' ';
  249. n += sysfs_emit_at(buf, n, "%s",
  250. ep11_op_modes[i].mode_txt);
  251. }
  252. }
  253. n += sysfs_emit_at(buf, n, "\n");
  254. return n;
  255. }
  256. static struct device_attribute dev_attr_ep11_card_op_modes =
  257. __ATTR(op_modes, 0444, ep11_card_op_modes_show, NULL);
  258. static struct attribute *ep11_card_attrs[] = {
  259. &dev_attr_ep11_api_ordinalnr.attr,
  260. &dev_attr_ep11_fw_version.attr,
  261. &dev_attr_ep11_serialnr.attr,
  262. &dev_attr_ep11_card_op_modes.attr,
  263. NULL,
  264. };
  265. static const struct attribute_group ep11_card_attr_grp = {
  266. .attrs = ep11_card_attrs,
  267. };
  268. /*
  269. * EP11 queue additional device attributes
  270. */
  271. static ssize_t ep11_mkvps_show(struct device *dev,
  272. struct device_attribute *attr,
  273. char *buf)
  274. {
  275. struct zcrypt_queue *zq = dev_get_drvdata(dev);
  276. int n = 0;
  277. struct ep11_domain_info di;
  278. static const char * const cwk_state[] = { "invalid", "valid" };
  279. static const char * const nwk_state[] = { "empty", "uncommitted",
  280. "committed" };
  281. memset(&di, 0, sizeof(di));
  282. if (zq->online)
  283. ep11_get_domain_info(AP_QID_CARD(zq->queue->qid),
  284. AP_QID_QUEUE(zq->queue->qid),
  285. &di, 0);
  286. if (di.cur_wk_state == '0') {
  287. n = sysfs_emit(buf, "WK CUR: %s -\n",
  288. cwk_state[di.cur_wk_state - '0']);
  289. } else if (di.cur_wk_state == '1') {
  290. n = sysfs_emit(buf, "WK CUR: %s 0x",
  291. cwk_state[di.cur_wk_state - '0']);
  292. bin2hex(buf + n, di.cur_wkvp, sizeof(di.cur_wkvp));
  293. n += 2 * sizeof(di.cur_wkvp);
  294. n += sysfs_emit_at(buf, n, "\n");
  295. } else {
  296. n = sysfs_emit(buf, "WK CUR: - -\n");
  297. }
  298. if (di.new_wk_state == '0') {
  299. n += sysfs_emit_at(buf, n, "WK NEW: %s -\n",
  300. nwk_state[di.new_wk_state - '0']);
  301. } else if (di.new_wk_state >= '1' && di.new_wk_state <= '2') {
  302. n += sysfs_emit_at(buf, n, "WK NEW: %s 0x",
  303. nwk_state[di.new_wk_state - '0']);
  304. bin2hex(buf + n, di.new_wkvp, sizeof(di.new_wkvp));
  305. n += 2 * sizeof(di.new_wkvp);
  306. n += sysfs_emit_at(buf, n, "\n");
  307. } else {
  308. n += sysfs_emit_at(buf, n, "WK NEW: - -\n");
  309. }
  310. return n;
  311. }
  312. static struct device_attribute dev_attr_ep11_mkvps =
  313. __ATTR(mkvps, 0444, ep11_mkvps_show, NULL);
  314. static ssize_t ep11_queue_op_modes_show(struct device *dev,
  315. struct device_attribute *attr,
  316. char *buf)
  317. {
  318. struct zcrypt_queue *zq = dev_get_drvdata(dev);
  319. int i, n = 0;
  320. struct ep11_domain_info di;
  321. memset(&di, 0, sizeof(di));
  322. if (zq->online)
  323. ep11_get_domain_info(AP_QID_CARD(zq->queue->qid),
  324. AP_QID_QUEUE(zq->queue->qid),
  325. &di, 0);
  326. for (i = 0; ep11_op_modes[i].mode_txt; i++) {
  327. if (di.op_mode & (1ULL << ep11_op_modes[i].mode_bit)) {
  328. if (n > 0)
  329. buf[n++] = ' ';
  330. n += sysfs_emit_at(buf, n, "%s",
  331. ep11_op_modes[i].mode_txt);
  332. }
  333. }
  334. n += sysfs_emit_at(buf, n, "\n");
  335. return n;
  336. }
  337. static struct device_attribute dev_attr_ep11_queue_op_modes =
  338. __ATTR(op_modes, 0444, ep11_queue_op_modes_show, NULL);
  339. static struct attribute *ep11_queue_attrs[] = {
  340. &dev_attr_ep11_mkvps.attr,
  341. &dev_attr_ep11_queue_op_modes.attr,
  342. NULL,
  343. };
  344. static const struct attribute_group ep11_queue_attr_grp = {
  345. .attrs = ep11_queue_attrs,
  346. };
  347. /*
  348. * Probe function for CEX[45678] card device. It always
  349. * accepts the AP device since the bus_match already checked
  350. * the hardware type.
  351. * @ap_dev: pointer to the AP device.
  352. */
  353. static int zcrypt_cex4_card_probe(struct ap_device *ap_dev)
  354. {
  355. /*
  356. * Normalized speed ratings per crypto adapter
  357. * MEX_1k, MEX_2k, MEX_4k, CRT_1k, CRT_2k, CRT_4k, RNG, SECKEY
  358. */
  359. static const int CEX4A_SPEED_IDX[NUM_OPS] = {
  360. 14, 19, 249, 42, 228, 1458, 0, 0};
  361. static const int CEX5A_SPEED_IDX[NUM_OPS] = {
  362. 8, 9, 20, 18, 66, 458, 0, 0};
  363. static const int CEX6A_SPEED_IDX[NUM_OPS] = {
  364. 6, 9, 20, 17, 65, 438, 0, 0};
  365. static const int CEX7A_SPEED_IDX[NUM_OPS] = {
  366. 6, 8, 17, 15, 54, 362, 0, 0};
  367. static const int CEX8A_SPEED_IDX[NUM_OPS] = {
  368. 6, 8, 17, 15, 54, 362, 0, 0};
  369. static const int CEX4C_SPEED_IDX[NUM_OPS] = {
  370. 59, 69, 308, 83, 278, 2204, 209, 40};
  371. static const int CEX5C_SPEED_IDX[] = {
  372. 24, 31, 50, 37, 90, 479, 27, 10};
  373. static const int CEX6C_SPEED_IDX[NUM_OPS] = {
  374. 16, 20, 32, 27, 77, 455, 24, 9};
  375. static const int CEX7C_SPEED_IDX[NUM_OPS] = {
  376. 14, 16, 26, 23, 64, 376, 23, 8};
  377. static const int CEX8C_SPEED_IDX[NUM_OPS] = {
  378. 14, 16, 26, 23, 64, 376, 23, 8};
  379. static const int CEX4P_SPEED_IDX[NUM_OPS] = {
  380. 0, 0, 0, 0, 0, 0, 0, 50};
  381. static const int CEX5P_SPEED_IDX[NUM_OPS] = {
  382. 0, 0, 0, 0, 0, 0, 0, 10};
  383. static const int CEX6P_SPEED_IDX[NUM_OPS] = {
  384. 0, 0, 0, 0, 0, 0, 0, 9};
  385. static const int CEX7P_SPEED_IDX[NUM_OPS] = {
  386. 0, 0, 0, 0, 0, 0, 0, 8};
  387. static const int CEX8P_SPEED_IDX[NUM_OPS] = {
  388. 0, 0, 0, 0, 0, 0, 0, 8};
  389. struct ap_card *ac = to_ap_card(&ap_dev->device);
  390. struct zcrypt_card *zc;
  391. int rc = 0;
  392. zc = zcrypt_card_alloc();
  393. if (!zc)
  394. return -ENOMEM;
  395. zc->card = ac;
  396. dev_set_drvdata(&ap_dev->device, zc);
  397. if (ac->hwinfo.accel) {
  398. if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
  399. zc->type_string = "CEX4A";
  400. zc->user_space_type = ZCRYPT_CEX4;
  401. zc->speed_rating = CEX4A_SPEED_IDX;
  402. } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) {
  403. zc->type_string = "CEX5A";
  404. zc->user_space_type = ZCRYPT_CEX5;
  405. zc->speed_rating = CEX5A_SPEED_IDX;
  406. } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX6) {
  407. zc->type_string = "CEX6A";
  408. zc->user_space_type = ZCRYPT_CEX6;
  409. zc->speed_rating = CEX6A_SPEED_IDX;
  410. } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX7) {
  411. zc->type_string = "CEX7A";
  412. zc->speed_rating = CEX7A_SPEED_IDX;
  413. /* wrong user space type, just for compatibility
  414. * with the ZCRYPT_STATUS_MASK ioctl.
  415. */
  416. zc->user_space_type = ZCRYPT_CEX6;
  417. } else {
  418. zc->type_string = "CEX8A";
  419. zc->speed_rating = CEX8A_SPEED_IDX;
  420. /* wrong user space type, just for compatibility
  421. * with the ZCRYPT_STATUS_MASK ioctl.
  422. */
  423. zc->user_space_type = ZCRYPT_CEX6;
  424. }
  425. zc->min_mod_size = CEX4A_MIN_MOD_SIZE;
  426. if (ac->hwinfo.mex4k && ac->hwinfo.crt4k) {
  427. zc->max_mod_size = CEX4A_MAX_MOD_SIZE_4K;
  428. zc->max_exp_bit_length =
  429. CEX4A_MAX_MOD_SIZE_4K;
  430. } else {
  431. zc->max_mod_size = CEX4A_MAX_MOD_SIZE_2K;
  432. zc->max_exp_bit_length =
  433. CEX4A_MAX_MOD_SIZE_2K;
  434. }
  435. } else if (ac->hwinfo.cca) {
  436. if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
  437. zc->type_string = "CEX4C";
  438. zc->speed_rating = CEX4C_SPEED_IDX;
  439. /* wrong user space type, must be CEX3C
  440. * just keep it for cca compatibility
  441. */
  442. zc->user_space_type = ZCRYPT_CEX3C;
  443. } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) {
  444. zc->type_string = "CEX5C";
  445. zc->speed_rating = CEX5C_SPEED_IDX;
  446. /* wrong user space type, must be CEX3C
  447. * just keep it for cca compatibility
  448. */
  449. zc->user_space_type = ZCRYPT_CEX3C;
  450. } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX6) {
  451. zc->type_string = "CEX6C";
  452. zc->speed_rating = CEX6C_SPEED_IDX;
  453. /* wrong user space type, must be CEX3C
  454. * just keep it for cca compatibility
  455. */
  456. zc->user_space_type = ZCRYPT_CEX3C;
  457. } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX7) {
  458. zc->type_string = "CEX7C";
  459. zc->speed_rating = CEX7C_SPEED_IDX;
  460. /* wrong user space type, must be CEX3C
  461. * just keep it for cca compatibility
  462. */
  463. zc->user_space_type = ZCRYPT_CEX3C;
  464. } else {
  465. zc->type_string = "CEX8C";
  466. zc->speed_rating = CEX8C_SPEED_IDX;
  467. /* wrong user space type, must be CEX3C
  468. * just keep it for cca compatibility
  469. */
  470. zc->user_space_type = ZCRYPT_CEX3C;
  471. }
  472. zc->min_mod_size = CEX4C_MIN_MOD_SIZE;
  473. zc->max_mod_size = CEX4C_MAX_MOD_SIZE;
  474. zc->max_exp_bit_length = CEX4C_MAX_MOD_SIZE;
  475. } else if (ac->hwinfo.ep11) {
  476. if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX4) {
  477. zc->type_string = "CEX4P";
  478. zc->user_space_type = ZCRYPT_CEX4;
  479. zc->speed_rating = CEX4P_SPEED_IDX;
  480. } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX5) {
  481. zc->type_string = "CEX5P";
  482. zc->user_space_type = ZCRYPT_CEX5;
  483. zc->speed_rating = CEX5P_SPEED_IDX;
  484. } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX6) {
  485. zc->type_string = "CEX6P";
  486. zc->user_space_type = ZCRYPT_CEX6;
  487. zc->speed_rating = CEX6P_SPEED_IDX;
  488. } else if (ac->ap_dev.device_type == AP_DEVICE_TYPE_CEX7) {
  489. zc->type_string = "CEX7P";
  490. zc->speed_rating = CEX7P_SPEED_IDX;
  491. /* wrong user space type, just for compatibility
  492. * with the ZCRYPT_STATUS_MASK ioctl.
  493. */
  494. zc->user_space_type = ZCRYPT_CEX6;
  495. } else {
  496. zc->type_string = "CEX8P";
  497. zc->speed_rating = CEX8P_SPEED_IDX;
  498. /* wrong user space type, just for compatibility
  499. * with the ZCRYPT_STATUS_MASK ioctl.
  500. */
  501. zc->user_space_type = ZCRYPT_CEX6;
  502. }
  503. zc->min_mod_size = CEX4C_MIN_MOD_SIZE;
  504. zc->max_mod_size = CEX4C_MAX_MOD_SIZE;
  505. zc->max_exp_bit_length = CEX4C_MAX_MOD_SIZE;
  506. } else {
  507. zcrypt_card_free(zc);
  508. return -ENODEV;
  509. }
  510. zc->online = 1;
  511. rc = zcrypt_card_register(zc);
  512. if (rc) {
  513. zcrypt_card_free(zc);
  514. return rc;
  515. }
  516. if (ac->hwinfo.cca) {
  517. rc = sysfs_create_group(&ap_dev->device.kobj,
  518. &cca_card_attr_grp);
  519. if (rc) {
  520. zcrypt_card_unregister(zc);
  521. zcrypt_card_free(zc);
  522. }
  523. } else if (ac->hwinfo.ep11) {
  524. rc = sysfs_create_group(&ap_dev->device.kobj,
  525. &ep11_card_attr_grp);
  526. if (rc) {
  527. zcrypt_card_unregister(zc);
  528. zcrypt_card_free(zc);
  529. }
  530. }
  531. return rc;
  532. }
  533. /*
  534. * This is called to remove the CEX[45678] card driver
  535. * information if an AP card device is removed.
  536. */
  537. static void zcrypt_cex4_card_remove(struct ap_device *ap_dev)
  538. {
  539. struct zcrypt_card *zc = dev_get_drvdata(&ap_dev->device);
  540. struct ap_card *ac = to_ap_card(&ap_dev->device);
  541. if (ac->hwinfo.cca)
  542. sysfs_remove_group(&ap_dev->device.kobj, &cca_card_attr_grp);
  543. else if (ac->hwinfo.ep11)
  544. sysfs_remove_group(&ap_dev->device.kobj, &ep11_card_attr_grp);
  545. zcrypt_card_unregister(zc);
  546. }
  547. static struct ap_driver zcrypt_cex4_card_driver = {
  548. .probe = zcrypt_cex4_card_probe,
  549. .remove = zcrypt_cex4_card_remove,
  550. .ids = zcrypt_cex4_card_ids,
  551. .flags = AP_DRIVER_FLAG_DEFAULT,
  552. };
  553. /*
  554. * Probe function for CEX[45678] queue device. It always
  555. * accepts the AP device since the bus_match already checked
  556. * the hardware type.
  557. * @ap_dev: pointer to the AP device.
  558. */
  559. static int zcrypt_cex4_queue_probe(struct ap_device *ap_dev)
  560. {
  561. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  562. struct zcrypt_queue *zq;
  563. int rc;
  564. if (aq->card->hwinfo.accel) {
  565. zq = zcrypt_queue_alloc(aq->card->maxmsgsize);
  566. if (!zq)
  567. return -ENOMEM;
  568. zq->ops = zcrypt_msgtype(MSGTYPE50_NAME,
  569. MSGTYPE50_VARIANT_DEFAULT);
  570. } else if (aq->card->hwinfo.cca) {
  571. zq = zcrypt_queue_alloc(aq->card->maxmsgsize);
  572. if (!zq)
  573. return -ENOMEM;
  574. zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
  575. MSGTYPE06_VARIANT_DEFAULT);
  576. } else if (aq->card->hwinfo.ep11) {
  577. zq = zcrypt_queue_alloc(aq->card->maxmsgsize);
  578. if (!zq)
  579. return -ENOMEM;
  580. zq->ops = zcrypt_msgtype(MSGTYPE06_NAME,
  581. MSGTYPE06_VARIANT_EP11);
  582. } else {
  583. return -ENODEV;
  584. }
  585. zq->queue = aq;
  586. zq->online = 1;
  587. atomic_set(&zq->load, 0);
  588. ap_queue_init_state(aq);
  589. ap_queue_init_reply(aq, &zq->reply);
  590. aq->request_timeout = CEX4_CLEANUP_TIME;
  591. dev_set_drvdata(&ap_dev->device, zq);
  592. rc = zcrypt_queue_register(zq);
  593. if (rc) {
  594. zcrypt_queue_free(zq);
  595. return rc;
  596. }
  597. if (aq->card->hwinfo.cca) {
  598. rc = sysfs_create_group(&ap_dev->device.kobj,
  599. &cca_queue_attr_grp);
  600. if (rc) {
  601. zcrypt_queue_unregister(zq);
  602. zcrypt_queue_free(zq);
  603. }
  604. } else if (aq->card->hwinfo.ep11) {
  605. rc = sysfs_create_group(&ap_dev->device.kobj,
  606. &ep11_queue_attr_grp);
  607. if (rc) {
  608. zcrypt_queue_unregister(zq);
  609. zcrypt_queue_free(zq);
  610. }
  611. }
  612. return rc;
  613. }
  614. /*
  615. * This is called to remove the CEX[45678] queue driver
  616. * information if an AP queue device is removed.
  617. */
  618. static void zcrypt_cex4_queue_remove(struct ap_device *ap_dev)
  619. {
  620. struct zcrypt_queue *zq = dev_get_drvdata(&ap_dev->device);
  621. struct ap_queue *aq = to_ap_queue(&ap_dev->device);
  622. if (aq->card->hwinfo.cca)
  623. sysfs_remove_group(&ap_dev->device.kobj, &cca_queue_attr_grp);
  624. else if (aq->card->hwinfo.ep11)
  625. sysfs_remove_group(&ap_dev->device.kobj, &ep11_queue_attr_grp);
  626. zcrypt_queue_unregister(zq);
  627. }
  628. static struct ap_driver zcrypt_cex4_queue_driver = {
  629. .probe = zcrypt_cex4_queue_probe,
  630. .remove = zcrypt_cex4_queue_remove,
  631. .ids = zcrypt_cex4_queue_ids,
  632. .flags = AP_DRIVER_FLAG_DEFAULT,
  633. };
  634. int __init zcrypt_cex4_init(void)
  635. {
  636. int rc;
  637. rc = ap_driver_register(&zcrypt_cex4_card_driver,
  638. THIS_MODULE, "cex4card");
  639. if (rc)
  640. return rc;
  641. rc = ap_driver_register(&zcrypt_cex4_queue_driver,
  642. THIS_MODULE, "cex4queue");
  643. if (rc)
  644. ap_driver_unregister(&zcrypt_cex4_card_driver);
  645. return rc;
  646. }
  647. void __exit zcrypt_cex4_exit(void)
  648. {
  649. ap_driver_unregister(&zcrypt_cex4_queue_driver);
  650. ap_driver_unregister(&zcrypt_cex4_card_driver);
  651. }
  652. module_init(zcrypt_cex4_init);
  653. module_exit(zcrypt_cex4_exit);