zcrypt_cex4.c 22 KB

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