nx.c 22 KB

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