nx.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Routines supporting the Power 7+ Nest Accelerators driver
  4. *
  5. * Copyright (C) 2011-2012 International Business Machines Inc.
  6. *
  7. * Author: Kent Yoder <[email protected]>
  8. */
  9. #include <crypto/internal/aead.h>
  10. #include <crypto/internal/hash.h>
  11. #include <crypto/aes.h>
  12. #include <crypto/sha2.h>
  13. #include <crypto/algapi.h>
  14. #include <crypto/scatterwalk.h>
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/types.h>
  18. #include <linux/mm.h>
  19. #include <linux/scatterlist.h>
  20. #include <linux/device.h>
  21. #include <linux/of.h>
  22. #include <asm/hvcall.h>
  23. #include <asm/vio.h>
  24. #include "nx_csbcpb.h"
  25. #include "nx.h"
  26. /**
  27. * nx_hcall_sync - make an H_COP_OP hcall for the passed in op structure
  28. *
  29. * @nx_ctx: the crypto context handle
  30. * @op: PFO operation struct to pass in
  31. * @may_sleep: flag indicating the request can sleep
  32. *
  33. * Make the hcall, retrying while the hardware is busy. If we cannot yield
  34. * the thread, limit the number of retries to 10 here.
  35. */
  36. int nx_hcall_sync(struct nx_crypto_ctx *nx_ctx,
  37. struct vio_pfo_op *op,
  38. u32 may_sleep)
  39. {
  40. int rc, retries = 10;
  41. struct vio_dev *viodev = nx_driver.viodev;
  42. atomic_inc(&(nx_ctx->stats->sync_ops));
  43. do {
  44. rc = vio_h_cop_sync(viodev, op);
  45. } while (rc == -EBUSY && !may_sleep && retries--);
  46. if (rc) {
  47. dev_dbg(&viodev->dev, "vio_h_cop_sync failed: rc: %d "
  48. "hcall rc: %ld\n", rc, op->hcall_err);
  49. atomic_inc(&(nx_ctx->stats->errors));
  50. atomic_set(&(nx_ctx->stats->last_error), op->hcall_err);
  51. atomic_set(&(nx_ctx->stats->last_error_pid), current->pid);
  52. }
  53. return rc;
  54. }
  55. /**
  56. * nx_build_sg_list - build an NX scatter list describing a single buffer
  57. *
  58. * @sg_head: pointer to the first scatter list element to build
  59. * @start_addr: pointer to the linear buffer
  60. * @len: length of the data at @start_addr
  61. * @sgmax: the largest number of scatter list elements we're allowed to create
  62. *
  63. * This function will start writing nx_sg elements at @sg_head and keep
  64. * writing them until all of the data from @start_addr is described or
  65. * until sgmax elements have been written. Scatter list elements will be
  66. * created such that none of the elements describes a buffer that crosses a 4K
  67. * boundary.
  68. */
  69. struct nx_sg *nx_build_sg_list(struct nx_sg *sg_head,
  70. u8 *start_addr,
  71. unsigned int *len,
  72. u32 sgmax)
  73. {
  74. unsigned int sg_len = 0;
  75. struct nx_sg *sg;
  76. u64 sg_addr = (u64)start_addr;
  77. u64 end_addr;
  78. /* determine the start and end for this address range - slightly
  79. * different if this is in VMALLOC_REGION */
  80. if (is_vmalloc_addr(start_addr))
  81. sg_addr = page_to_phys(vmalloc_to_page(start_addr))
  82. + offset_in_page(sg_addr);
  83. else
  84. sg_addr = __pa(sg_addr);
  85. end_addr = sg_addr + *len;
  86. /* each iteration will write one struct nx_sg element and add the
  87. * length of data described by that element to sg_len. Once @len bytes
  88. * have been described (or @sgmax elements have been written), the
  89. * loop ends. min_t is used to ensure @end_addr falls on the same page
  90. * as sg_addr, if not, we need to create another nx_sg element for the
  91. * data on the next page.
  92. *
  93. * Also when using vmalloc'ed data, every time that a system page
  94. * boundary is crossed the physical address needs to be re-calculated.
  95. */
  96. for (sg = sg_head; sg_len < *len; sg++) {
  97. u64 next_page;
  98. sg->addr = sg_addr;
  99. sg_addr = min_t(u64, NX_PAGE_NUM(sg_addr + NX_PAGE_SIZE),
  100. end_addr);
  101. next_page = (sg->addr & PAGE_MASK) + PAGE_SIZE;
  102. sg->len = min_t(u64, sg_addr, next_page) - sg->addr;
  103. sg_len += sg->len;
  104. if (sg_addr >= next_page &&
  105. is_vmalloc_addr(start_addr + sg_len)) {
  106. sg_addr = page_to_phys(vmalloc_to_page(
  107. start_addr + sg_len));
  108. end_addr = sg_addr + *len - sg_len;
  109. }
  110. if ((sg - sg_head) == sgmax) {
  111. pr_err("nx: scatter/gather list overflow, pid: %d\n",
  112. current->pid);
  113. sg++;
  114. break;
  115. }
  116. }
  117. *len = sg_len;
  118. /* return the moved sg_head pointer */
  119. return sg;
  120. }
  121. /**
  122. * nx_walk_and_build - walk a linux scatterlist and build an nx scatterlist
  123. *
  124. * @nx_dst: pointer to the first nx_sg element to write
  125. * @sglen: max number of nx_sg entries we're allowed to write
  126. * @sg_src: pointer to the source linux scatterlist to walk
  127. * @start: number of bytes to fast-forward past at the beginning of @sg_src
  128. * @src_len: number of bytes to walk in @sg_src
  129. */
  130. struct nx_sg *nx_walk_and_build(struct nx_sg *nx_dst,
  131. unsigned int sglen,
  132. struct scatterlist *sg_src,
  133. unsigned int start,
  134. unsigned int *src_len)
  135. {
  136. struct scatter_walk walk;
  137. struct nx_sg *nx_sg = nx_dst;
  138. unsigned int n, offset = 0, len = *src_len;
  139. char *dst;
  140. /* we need to fast forward through @start bytes first */
  141. for (;;) {
  142. scatterwalk_start(&walk, sg_src);
  143. if (start < offset + sg_src->length)
  144. break;
  145. offset += sg_src->length;
  146. sg_src = sg_next(sg_src);
  147. }
  148. /* start - offset is the number of bytes to advance in the scatterlist
  149. * element we're currently looking at */
  150. scatterwalk_advance(&walk, start - offset);
  151. while (len && (nx_sg - nx_dst) < sglen) {
  152. n = scatterwalk_clamp(&walk, len);
  153. if (!n) {
  154. /* In cases where we have scatterlist chain sg_next
  155. * handles with it properly */
  156. scatterwalk_start(&walk, sg_next(walk.sg));
  157. n = scatterwalk_clamp(&walk, len);
  158. }
  159. dst = scatterwalk_map(&walk);
  160. nx_sg = nx_build_sg_list(nx_sg, dst, &n, sglen - (nx_sg - nx_dst));
  161. len -= n;
  162. scatterwalk_unmap(dst);
  163. scatterwalk_advance(&walk, n);
  164. scatterwalk_done(&walk, SCATTERWALK_FROM_SG, len);
  165. }
  166. /* update to_process */
  167. *src_len -= len;
  168. /* return the moved destination pointer */
  169. return nx_sg;
  170. }
  171. /**
  172. * trim_sg_list - ensures the bound in sg list.
  173. * @sg: sg list head
  174. * @end: sg lisg end
  175. * @delta: is the amount we need to crop in order to bound the list.
  176. * @nbytes: length of data in the scatterlists or data length - whichever
  177. * is greater.
  178. */
  179. static long int trim_sg_list(struct nx_sg *sg,
  180. struct nx_sg *end,
  181. unsigned int delta,
  182. unsigned int *nbytes)
  183. {
  184. long int oplen;
  185. long int data_back;
  186. unsigned int is_delta = delta;
  187. while (delta && end > sg) {
  188. struct nx_sg *last = end - 1;
  189. if (last->len > delta) {
  190. last->len -= delta;
  191. delta = 0;
  192. } else {
  193. end--;
  194. delta -= last->len;
  195. }
  196. }
  197. /* There are cases where we need to crop list in order to make it
  198. * a block size multiple, but we also need to align data. In order to
  199. * that we need to calculate how much we need to put back to be
  200. * processed
  201. */
  202. oplen = (sg - end) * sizeof(struct nx_sg);
  203. if (is_delta) {
  204. data_back = (abs(oplen) / AES_BLOCK_SIZE) * sg->len;
  205. data_back = *nbytes - (data_back & ~(AES_BLOCK_SIZE - 1));
  206. *nbytes -= data_back;
  207. }
  208. return oplen;
  209. }
  210. /**
  211. * nx_build_sg_lists - walk the input scatterlists and build arrays of NX
  212. * scatterlists based on them.
  213. *
  214. * @nx_ctx: NX crypto context for the lists we're building
  215. * @iv: iv data, if the algorithm requires it
  216. * @dst: destination scatterlist
  217. * @src: source scatterlist
  218. * @nbytes: length of data described in the scatterlists
  219. * @offset: number of bytes to fast-forward past at the beginning of
  220. * scatterlists.
  221. * @oiv: destination for the iv data, if the algorithm requires it
  222. *
  223. * This is common code shared by all the AES algorithms. It uses the crypto
  224. * scatterlist walk routines to traverse input and output scatterlists, building
  225. * corresponding NX scatterlists
  226. */
  227. int nx_build_sg_lists(struct nx_crypto_ctx *nx_ctx,
  228. const u8 *iv,
  229. struct scatterlist *dst,
  230. struct scatterlist *src,
  231. unsigned int *nbytes,
  232. unsigned int offset,
  233. u8 *oiv)
  234. {
  235. unsigned int delta = 0;
  236. unsigned int total = *nbytes;
  237. struct nx_sg *nx_insg = nx_ctx->in_sg;
  238. struct nx_sg *nx_outsg = nx_ctx->out_sg;
  239. unsigned int max_sg_len;
  240. max_sg_len = min_t(u64, nx_ctx->ap->sglen,
  241. nx_driver.of.max_sg_len/sizeof(struct nx_sg));
  242. max_sg_len = min_t(u64, max_sg_len,
  243. nx_ctx->ap->databytelen/NX_PAGE_SIZE);
  244. if (oiv)
  245. memcpy(oiv, iv, AES_BLOCK_SIZE);
  246. *nbytes = min_t(u64, *nbytes, nx_ctx->ap->databytelen);
  247. nx_outsg = nx_walk_and_build(nx_outsg, max_sg_len, dst,
  248. offset, nbytes);
  249. nx_insg = nx_walk_and_build(nx_insg, max_sg_len, src,
  250. offset, nbytes);
  251. if (*nbytes < total)
  252. delta = *nbytes - (*nbytes & ~(AES_BLOCK_SIZE - 1));
  253. /* these lengths should be negative, which will indicate to phyp that
  254. * the input and output parameters are scatterlists, not linear
  255. * buffers */
  256. nx_ctx->op.inlen = trim_sg_list(nx_ctx->in_sg, nx_insg, delta, nbytes);
  257. nx_ctx->op.outlen = trim_sg_list(nx_ctx->out_sg, nx_outsg, delta, nbytes);
  258. return 0;
  259. }
  260. /**
  261. * nx_ctx_init - initialize an nx_ctx's vio_pfo_op struct
  262. *
  263. * @nx_ctx: the nx context to initialize
  264. * @function: the function code for the op
  265. */
  266. void nx_ctx_init(struct nx_crypto_ctx *nx_ctx, unsigned int function)
  267. {
  268. spin_lock_init(&nx_ctx->lock);
  269. memset(nx_ctx->kmem, 0, nx_ctx->kmem_len);
  270. nx_ctx->csbcpb->csb.valid |= NX_CSB_VALID_BIT;
  271. nx_ctx->op.flags = function;
  272. nx_ctx->op.csbcpb = __pa(nx_ctx->csbcpb);
  273. nx_ctx->op.in = __pa(nx_ctx->in_sg);
  274. nx_ctx->op.out = __pa(nx_ctx->out_sg);
  275. if (nx_ctx->csbcpb_aead) {
  276. nx_ctx->csbcpb_aead->csb.valid |= NX_CSB_VALID_BIT;
  277. nx_ctx->op_aead.flags = function;
  278. nx_ctx->op_aead.csbcpb = __pa(nx_ctx->csbcpb_aead);
  279. nx_ctx->op_aead.in = __pa(nx_ctx->in_sg);
  280. nx_ctx->op_aead.out = __pa(nx_ctx->out_sg);
  281. }
  282. }
  283. static void nx_of_update_status(struct device *dev,
  284. struct property *p,
  285. struct nx_of *props)
  286. {
  287. if (!strncmp(p->value, "okay", p->length)) {
  288. props->status = NX_WAITING;
  289. props->flags |= NX_OF_FLAG_STATUS_SET;
  290. } else {
  291. dev_info(dev, "%s: status '%s' is not 'okay'\n", __func__,
  292. (char *)p->value);
  293. }
  294. }
  295. static void nx_of_update_sglen(struct device *dev,
  296. struct property *p,
  297. struct nx_of *props)
  298. {
  299. if (p->length != sizeof(props->max_sg_len)) {
  300. dev_err(dev, "%s: unexpected format for "
  301. "ibm,max-sg-len property\n", __func__);
  302. dev_dbg(dev, "%s: ibm,max-sg-len is %d bytes "
  303. "long, expected %zd bytes\n", __func__,
  304. p->length, sizeof(props->max_sg_len));
  305. return;
  306. }
  307. props->max_sg_len = *(u32 *)p->value;
  308. props->flags |= NX_OF_FLAG_MAXSGLEN_SET;
  309. }
  310. static void nx_of_update_msc(struct device *dev,
  311. struct property *p,
  312. struct nx_of *props)
  313. {
  314. struct msc_triplet *trip;
  315. struct max_sync_cop *msc;
  316. unsigned int bytes_so_far, i, lenp;
  317. msc = (struct max_sync_cop *)p->value;
  318. lenp = p->length;
  319. /* You can't tell if the data read in for this property is sane by its
  320. * size alone. This is because there are sizes embedded in the data
  321. * structure. The best we can do is check lengths as we parse and bail
  322. * as soon as a length error is detected. */
  323. bytes_so_far = 0;
  324. while ((bytes_so_far + sizeof(struct max_sync_cop)) <= lenp) {
  325. bytes_so_far += sizeof(struct max_sync_cop);
  326. trip = msc->trip;
  327. for (i = 0;
  328. ((bytes_so_far + sizeof(struct msc_triplet)) <= lenp) &&
  329. i < msc->triplets;
  330. i++) {
  331. if (msc->fc >= NX_MAX_FC || msc->mode >= NX_MAX_MODE) {
  332. dev_err(dev, "unknown function code/mode "
  333. "combo: %d/%d (ignored)\n", msc->fc,
  334. msc->mode);
  335. goto next_loop;
  336. }
  337. if (!trip->sglen || trip->databytelen < NX_PAGE_SIZE) {
  338. dev_warn(dev, "bogus sglen/databytelen: "
  339. "%u/%u (ignored)\n", trip->sglen,
  340. trip->databytelen);
  341. goto next_loop;
  342. }
  343. switch (trip->keybitlen) {
  344. case 128:
  345. case 160:
  346. props->ap[msc->fc][msc->mode][0].databytelen =
  347. trip->databytelen;
  348. props->ap[msc->fc][msc->mode][0].sglen =
  349. trip->sglen;
  350. break;
  351. case 192:
  352. props->ap[msc->fc][msc->mode][1].databytelen =
  353. trip->databytelen;
  354. props->ap[msc->fc][msc->mode][1].sglen =
  355. trip->sglen;
  356. break;
  357. case 256:
  358. if (msc->fc == NX_FC_AES) {
  359. props->ap[msc->fc][msc->mode][2].
  360. databytelen = trip->databytelen;
  361. props->ap[msc->fc][msc->mode][2].sglen =
  362. trip->sglen;
  363. } else if (msc->fc == NX_FC_AES_HMAC ||
  364. msc->fc == NX_FC_SHA) {
  365. props->ap[msc->fc][msc->mode][1].
  366. databytelen = trip->databytelen;
  367. props->ap[msc->fc][msc->mode][1].sglen =
  368. trip->sglen;
  369. } else {
  370. dev_warn(dev, "unknown function "
  371. "code/key bit len combo"
  372. ": (%u/256)\n", msc->fc);
  373. }
  374. break;
  375. case 512:
  376. props->ap[msc->fc][msc->mode][2].databytelen =
  377. trip->databytelen;
  378. props->ap[msc->fc][msc->mode][2].sglen =
  379. trip->sglen;
  380. break;
  381. default:
  382. dev_warn(dev, "unknown function code/key bit "
  383. "len combo: (%u/%u)\n", msc->fc,
  384. trip->keybitlen);
  385. break;
  386. }
  387. next_loop:
  388. bytes_so_far += sizeof(struct msc_triplet);
  389. trip++;
  390. }
  391. msc = (struct max_sync_cop *)trip;
  392. }
  393. props->flags |= NX_OF_FLAG_MAXSYNCCOP_SET;
  394. }
  395. /**
  396. * nx_of_init - read openFirmware values from the device tree
  397. *
  398. * @dev: device handle
  399. * @props: pointer to struct to hold the properties values
  400. *
  401. * Called once at driver probe time, this function will read out the
  402. * openFirmware properties we use at runtime. If all the OF properties are
  403. * acceptable, when we exit this function props->flags will indicate that
  404. * we're ready to register our crypto algorithms.
  405. */
  406. static void nx_of_init(struct device *dev, struct nx_of *props)
  407. {
  408. struct device_node *base_node = dev->of_node;
  409. struct property *p;
  410. p = of_find_property(base_node, "status", NULL);
  411. if (!p)
  412. dev_info(dev, "%s: property 'status' not found\n", __func__);
  413. else
  414. nx_of_update_status(dev, p, props);
  415. p = of_find_property(base_node, "ibm,max-sg-len", NULL);
  416. if (!p)
  417. dev_info(dev, "%s: property 'ibm,max-sg-len' not found\n",
  418. __func__);
  419. else
  420. nx_of_update_sglen(dev, p, props);
  421. p = of_find_property(base_node, "ibm,max-sync-cop", NULL);
  422. if (!p)
  423. dev_info(dev, "%s: property 'ibm,max-sync-cop' not found\n",
  424. __func__);
  425. else
  426. nx_of_update_msc(dev, p, props);
  427. }
  428. static bool nx_check_prop(struct device *dev, u32 fc, u32 mode, int slot)
  429. {
  430. struct alg_props *props = &nx_driver.of.ap[fc][mode][slot];
  431. if (!props->sglen || props->databytelen < NX_PAGE_SIZE) {
  432. if (dev)
  433. dev_warn(dev, "bogus sglen/databytelen for %u/%u/%u: "
  434. "%u/%u (ignored)\n", fc, mode, slot,
  435. props->sglen, props->databytelen);
  436. return false;
  437. }
  438. return true;
  439. }
  440. static bool nx_check_props(struct device *dev, u32 fc, u32 mode)
  441. {
  442. int i;
  443. for (i = 0; i < 3; i++)
  444. if (!nx_check_prop(dev, fc, mode, i))
  445. return false;
  446. return true;
  447. }
  448. static int nx_register_skcipher(struct skcipher_alg *alg, u32 fc, u32 mode)
  449. {
  450. return nx_check_props(&nx_driver.viodev->dev, fc, mode) ?
  451. crypto_register_skcipher(alg) : 0;
  452. }
  453. static int nx_register_aead(struct aead_alg *alg, u32 fc, u32 mode)
  454. {
  455. return nx_check_props(&nx_driver.viodev->dev, fc, mode) ?
  456. crypto_register_aead(alg) : 0;
  457. }
  458. static int nx_register_shash(struct shash_alg *alg, u32 fc, u32 mode, int slot)
  459. {
  460. return (slot >= 0 ? nx_check_prop(&nx_driver.viodev->dev,
  461. fc, mode, slot) :
  462. nx_check_props(&nx_driver.viodev->dev, fc, mode)) ?
  463. crypto_register_shash(alg) : 0;
  464. }
  465. static void nx_unregister_skcipher(struct skcipher_alg *alg, u32 fc, u32 mode)
  466. {
  467. if (nx_check_props(NULL, fc, mode))
  468. crypto_unregister_skcipher(alg);
  469. }
  470. static void nx_unregister_aead(struct aead_alg *alg, u32 fc, u32 mode)
  471. {
  472. if (nx_check_props(NULL, fc, mode))
  473. crypto_unregister_aead(alg);
  474. }
  475. static void nx_unregister_shash(struct shash_alg *alg, u32 fc, u32 mode,
  476. int slot)
  477. {
  478. if (slot >= 0 ? nx_check_prop(NULL, fc, mode, slot) :
  479. nx_check_props(NULL, fc, mode))
  480. crypto_unregister_shash(alg);
  481. }
  482. /**
  483. * nx_register_algs - register algorithms with the crypto API
  484. *
  485. * Called from nx_probe()
  486. *
  487. * If all OF properties are in an acceptable state, the driver flags will
  488. * indicate that we're ready and we'll create our debugfs files and register
  489. * out crypto algorithms.
  490. */
  491. static int nx_register_algs(void)
  492. {
  493. int rc = -1;
  494. if (nx_driver.of.flags != NX_OF_FLAG_MASK_READY)
  495. goto out;
  496. memset(&nx_driver.stats, 0, sizeof(struct nx_stats));
  497. NX_DEBUGFS_INIT(&nx_driver);
  498. nx_driver.of.status = NX_OKAY;
  499. rc = nx_register_skcipher(&nx_ecb_aes_alg, NX_FC_AES, NX_MODE_AES_ECB);
  500. if (rc)
  501. goto out;
  502. rc = nx_register_skcipher(&nx_cbc_aes_alg, NX_FC_AES, NX_MODE_AES_CBC);
  503. if (rc)
  504. goto out_unreg_ecb;
  505. rc = nx_register_skcipher(&nx_ctr3686_aes_alg, NX_FC_AES,
  506. NX_MODE_AES_CTR);
  507. if (rc)
  508. goto out_unreg_cbc;
  509. rc = nx_register_aead(&nx_gcm_aes_alg, NX_FC_AES, NX_MODE_AES_GCM);
  510. if (rc)
  511. goto out_unreg_ctr3686;
  512. rc = nx_register_aead(&nx_gcm4106_aes_alg, NX_FC_AES, NX_MODE_AES_GCM);
  513. if (rc)
  514. goto out_unreg_gcm;
  515. rc = nx_register_aead(&nx_ccm_aes_alg, NX_FC_AES, NX_MODE_AES_CCM);
  516. if (rc)
  517. goto out_unreg_gcm4106;
  518. rc = nx_register_aead(&nx_ccm4309_aes_alg, NX_FC_AES, NX_MODE_AES_CCM);
  519. if (rc)
  520. goto out_unreg_ccm;
  521. rc = nx_register_shash(&nx_shash_sha256_alg, NX_FC_SHA, NX_MODE_SHA,
  522. NX_PROPS_SHA256);
  523. if (rc)
  524. goto out_unreg_ccm4309;
  525. rc = nx_register_shash(&nx_shash_sha512_alg, NX_FC_SHA, NX_MODE_SHA,
  526. NX_PROPS_SHA512);
  527. if (rc)
  528. goto out_unreg_s256;
  529. rc = nx_register_shash(&nx_shash_aes_xcbc_alg,
  530. NX_FC_AES, NX_MODE_AES_XCBC_MAC, -1);
  531. if (rc)
  532. goto out_unreg_s512;
  533. goto out;
  534. out_unreg_s512:
  535. nx_unregister_shash(&nx_shash_sha512_alg, NX_FC_SHA, NX_MODE_SHA,
  536. NX_PROPS_SHA512);
  537. out_unreg_s256:
  538. nx_unregister_shash(&nx_shash_sha256_alg, NX_FC_SHA, NX_MODE_SHA,
  539. NX_PROPS_SHA256);
  540. out_unreg_ccm4309:
  541. nx_unregister_aead(&nx_ccm4309_aes_alg, NX_FC_AES, NX_MODE_AES_CCM);
  542. out_unreg_ccm:
  543. nx_unregister_aead(&nx_ccm_aes_alg, NX_FC_AES, NX_MODE_AES_CCM);
  544. out_unreg_gcm4106:
  545. nx_unregister_aead(&nx_gcm4106_aes_alg, NX_FC_AES, NX_MODE_AES_GCM);
  546. out_unreg_gcm:
  547. nx_unregister_aead(&nx_gcm_aes_alg, NX_FC_AES, NX_MODE_AES_GCM);
  548. out_unreg_ctr3686:
  549. nx_unregister_skcipher(&nx_ctr3686_aes_alg, NX_FC_AES, NX_MODE_AES_CTR);
  550. out_unreg_cbc:
  551. nx_unregister_skcipher(&nx_cbc_aes_alg, NX_FC_AES, NX_MODE_AES_CBC);
  552. out_unreg_ecb:
  553. nx_unregister_skcipher(&nx_ecb_aes_alg, NX_FC_AES, NX_MODE_AES_ECB);
  554. out:
  555. return rc;
  556. }
  557. /**
  558. * nx_crypto_ctx_init - create and initialize a crypto api context
  559. *
  560. * @nx_ctx: the crypto api context
  561. * @fc: function code for the context
  562. * @mode: the function code specific mode for this context
  563. */
  564. static int nx_crypto_ctx_init(struct nx_crypto_ctx *nx_ctx, u32 fc, u32 mode)
  565. {
  566. if (nx_driver.of.status != NX_OKAY) {
  567. pr_err("Attempt to initialize NX crypto context while device "
  568. "is not available!\n");
  569. return -ENODEV;
  570. }
  571. /* we need an extra page for csbcpb_aead for these modes */
  572. if (mode == NX_MODE_AES_GCM || mode == NX_MODE_AES_CCM)
  573. nx_ctx->kmem_len = (5 * NX_PAGE_SIZE) +
  574. sizeof(struct nx_csbcpb);
  575. else
  576. nx_ctx->kmem_len = (4 * NX_PAGE_SIZE) +
  577. sizeof(struct nx_csbcpb);
  578. nx_ctx->kmem = kmalloc(nx_ctx->kmem_len, GFP_KERNEL);
  579. if (!nx_ctx->kmem)
  580. return -ENOMEM;
  581. /* the csbcpb and scatterlists must be 4K aligned pages */
  582. nx_ctx->csbcpb = (struct nx_csbcpb *)(round_up((u64)nx_ctx->kmem,
  583. (u64)NX_PAGE_SIZE));
  584. nx_ctx->in_sg = (struct nx_sg *)((u8 *)nx_ctx->csbcpb + NX_PAGE_SIZE);
  585. nx_ctx->out_sg = (struct nx_sg *)((u8 *)nx_ctx->in_sg + NX_PAGE_SIZE);
  586. if (mode == NX_MODE_AES_GCM || mode == NX_MODE_AES_CCM)
  587. nx_ctx->csbcpb_aead =
  588. (struct nx_csbcpb *)((u8 *)nx_ctx->out_sg +
  589. NX_PAGE_SIZE);
  590. /* give each context a pointer to global stats and their OF
  591. * properties */
  592. nx_ctx->stats = &nx_driver.stats;
  593. memcpy(nx_ctx->props, nx_driver.of.ap[fc][mode],
  594. sizeof(struct alg_props) * 3);
  595. return 0;
  596. }
  597. /* entry points from the crypto tfm initializers */
  598. int nx_crypto_ctx_aes_ccm_init(struct crypto_aead *tfm)
  599. {
  600. crypto_aead_set_reqsize(tfm, sizeof(struct nx_ccm_rctx));
  601. return nx_crypto_ctx_init(crypto_aead_ctx(tfm), NX_FC_AES,
  602. NX_MODE_AES_CCM);
  603. }
  604. int nx_crypto_ctx_aes_gcm_init(struct crypto_aead *tfm)
  605. {
  606. crypto_aead_set_reqsize(tfm, sizeof(struct nx_gcm_rctx));
  607. return nx_crypto_ctx_init(crypto_aead_ctx(tfm), NX_FC_AES,
  608. NX_MODE_AES_GCM);
  609. }
  610. int nx_crypto_ctx_aes_ctr_init(struct crypto_skcipher *tfm)
  611. {
  612. return nx_crypto_ctx_init(crypto_skcipher_ctx(tfm), NX_FC_AES,
  613. NX_MODE_AES_CTR);
  614. }
  615. int nx_crypto_ctx_aes_cbc_init(struct crypto_skcipher *tfm)
  616. {
  617. return nx_crypto_ctx_init(crypto_skcipher_ctx(tfm), NX_FC_AES,
  618. NX_MODE_AES_CBC);
  619. }
  620. int nx_crypto_ctx_aes_ecb_init(struct crypto_skcipher *tfm)
  621. {
  622. return nx_crypto_ctx_init(crypto_skcipher_ctx(tfm), NX_FC_AES,
  623. NX_MODE_AES_ECB);
  624. }
  625. int nx_crypto_ctx_sha_init(struct crypto_tfm *tfm)
  626. {
  627. return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_SHA, NX_MODE_SHA);
  628. }
  629. int nx_crypto_ctx_aes_xcbc_init(struct crypto_tfm *tfm)
  630. {
  631. return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
  632. NX_MODE_AES_XCBC_MAC);
  633. }
  634. /**
  635. * nx_crypto_ctx_exit - destroy a crypto api context
  636. *
  637. * @tfm: the crypto transform pointer for the context
  638. *
  639. * As crypto API contexts are destroyed, this exit hook is called to free the
  640. * memory associated with it.
  641. */
  642. void nx_crypto_ctx_exit(struct crypto_tfm *tfm)
  643. {
  644. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
  645. kfree_sensitive(nx_ctx->kmem);
  646. nx_ctx->csbcpb = NULL;
  647. nx_ctx->csbcpb_aead = NULL;
  648. nx_ctx->in_sg = NULL;
  649. nx_ctx->out_sg = NULL;
  650. }
  651. void nx_crypto_ctx_skcipher_exit(struct crypto_skcipher *tfm)
  652. {
  653. nx_crypto_ctx_exit(crypto_skcipher_ctx(tfm));
  654. }
  655. void nx_crypto_ctx_aead_exit(struct crypto_aead *tfm)
  656. {
  657. struct nx_crypto_ctx *nx_ctx = crypto_aead_ctx(tfm);
  658. kfree_sensitive(nx_ctx->kmem);
  659. }
  660. static int nx_probe(struct vio_dev *viodev, const struct vio_device_id *id)
  661. {
  662. dev_dbg(&viodev->dev, "driver probed: %s resource id: 0x%x\n",
  663. viodev->name, viodev->resource_id);
  664. if (nx_driver.viodev) {
  665. dev_err(&viodev->dev, "%s: Attempt to register more than one "
  666. "instance of the hardware\n", __func__);
  667. return -EINVAL;
  668. }
  669. nx_driver.viodev = viodev;
  670. nx_of_init(&viodev->dev, &nx_driver.of);
  671. return nx_register_algs();
  672. }
  673. static void nx_remove(struct vio_dev *viodev)
  674. {
  675. dev_dbg(&viodev->dev, "entering nx_remove for UA 0x%x\n",
  676. viodev->unit_address);
  677. if (nx_driver.of.status == NX_OKAY) {
  678. NX_DEBUGFS_FINI(&nx_driver);
  679. nx_unregister_shash(&nx_shash_aes_xcbc_alg,
  680. NX_FC_AES, NX_MODE_AES_XCBC_MAC, -1);
  681. nx_unregister_shash(&nx_shash_sha512_alg,
  682. NX_FC_SHA, NX_MODE_SHA, NX_PROPS_SHA256);
  683. nx_unregister_shash(&nx_shash_sha256_alg,
  684. NX_FC_SHA, NX_MODE_SHA, NX_PROPS_SHA512);
  685. nx_unregister_aead(&nx_ccm4309_aes_alg,
  686. NX_FC_AES, NX_MODE_AES_CCM);
  687. nx_unregister_aead(&nx_ccm_aes_alg, NX_FC_AES, NX_MODE_AES_CCM);
  688. nx_unregister_aead(&nx_gcm4106_aes_alg,
  689. NX_FC_AES, NX_MODE_AES_GCM);
  690. nx_unregister_aead(&nx_gcm_aes_alg,
  691. NX_FC_AES, NX_MODE_AES_GCM);
  692. nx_unregister_skcipher(&nx_ctr3686_aes_alg,
  693. NX_FC_AES, NX_MODE_AES_CTR);
  694. nx_unregister_skcipher(&nx_cbc_aes_alg, NX_FC_AES,
  695. NX_MODE_AES_CBC);
  696. nx_unregister_skcipher(&nx_ecb_aes_alg, NX_FC_AES,
  697. NX_MODE_AES_ECB);
  698. }
  699. }
  700. /* module wide initialization/cleanup */
  701. static int __init nx_init(void)
  702. {
  703. return vio_register_driver(&nx_driver.viodriver);
  704. }
  705. static void __exit nx_fini(void)
  706. {
  707. vio_unregister_driver(&nx_driver.viodriver);
  708. }
  709. static const struct vio_device_id nx_crypto_driver_ids[] = {
  710. { "ibm,sym-encryption-v1", "ibm,sym-encryption" },
  711. { "", "" }
  712. };
  713. MODULE_DEVICE_TABLE(vio, nx_crypto_driver_ids);
  714. /* driver state structure */
  715. struct nx_crypto_driver nx_driver = {
  716. .viodriver = {
  717. .id_table = nx_crypto_driver_ids,
  718. .probe = nx_probe,
  719. .remove = nx_remove,
  720. .name = NX_NAME,
  721. },
  722. };
  723. module_init(nx_init);
  724. module_exit(nx_fini);
  725. MODULE_AUTHOR("Kent Yoder <[email protected]>");
  726. MODULE_DESCRIPTION(NX_STRING);
  727. MODULE_LICENSE("GPL");
  728. MODULE_VERSION(NX_VERSION);