zcrypt_cex4.c 21 KB

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