nx.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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/pSeries_reconfig.h>
  36. #include <asm/hvcall.h>
  37. #include <asm/vio.h>
  38. #include "nx_csbcpb.h"
  39. #include "nx.h"
  40. /**
  41. * nx_hcall_sync - make an H_COP_OP hcall for the passed in op structure
  42. *
  43. * @nx_ctx: the crypto context handle
  44. * @op: PFO operation struct to pass in
  45. * @may_sleep: flag indicating the request can sleep
  46. *
  47. * Make the hcall, retrying while the hardware is busy. If we cannot yield
  48. * the thread, limit the number of retries to 10 here.
  49. */
  50. int nx_hcall_sync(struct nx_crypto_ctx *nx_ctx,
  51. struct vio_pfo_op *op,
  52. u32 may_sleep)
  53. {
  54. int rc, retries = 10;
  55. struct vio_dev *viodev = nx_driver.viodev;
  56. atomic_inc(&(nx_ctx->stats->sync_ops));
  57. do {
  58. rc = vio_h_cop_sync(viodev, op);
  59. } while ((rc == -EBUSY && !may_sleep && retries--) ||
  60. (rc == -EBUSY && may_sleep && cond_resched()));
  61. if (rc) {
  62. dev_dbg(&viodev->dev, "vio_h_cop_sync failed: rc: %d "
  63. "hcall rc: %ld\n", rc, op->hcall_err);
  64. atomic_inc(&(nx_ctx->stats->errors));
  65. atomic_set(&(nx_ctx->stats->last_error), op->hcall_err);
  66. atomic_set(&(nx_ctx->stats->last_error_pid), current->pid);
  67. }
  68. return rc;
  69. }
  70. /**
  71. * nx_build_sg_list - build an NX scatter list describing a single buffer
  72. *
  73. * @sg_head: pointer to the first scatter list element to build
  74. * @start_addr: pointer to the linear buffer
  75. * @len: length of the data at @start_addr
  76. * @sgmax: the largest number of scatter list elements we're allowed to create
  77. *
  78. * This function will start writing nx_sg elements at @sg_head and keep
  79. * writing them until all of the data from @start_addr is described or
  80. * until sgmax elements have been written. Scatter list elements will be
  81. * created such that none of the elements describes a buffer that crosses a 4K
  82. * boundary.
  83. */
  84. struct nx_sg *nx_build_sg_list(struct nx_sg *sg_head,
  85. u8 *start_addr,
  86. unsigned int len,
  87. u32 sgmax)
  88. {
  89. unsigned int sg_len = 0;
  90. struct nx_sg *sg;
  91. u64 sg_addr = (u64)start_addr;
  92. u64 end_addr;
  93. /* determine the start and end for this address range - slightly
  94. * different if this is in VMALLOC_REGION */
  95. if (is_vmalloc_addr(start_addr))
  96. sg_addr = page_to_phys(vmalloc_to_page(start_addr))
  97. + offset_in_page(sg_addr);
  98. else
  99. sg_addr = __pa(sg_addr);
  100. end_addr = sg_addr + len;
  101. /* each iteration will write one struct nx_sg element and add the
  102. * length of data described by that element to sg_len. Once @len bytes
  103. * have been described (or @sgmax elements have been written), the
  104. * loop ends. min_t is used to ensure @end_addr falls on the same page
  105. * as sg_addr, if not, we need to create another nx_sg element for the
  106. * data on the next page */
  107. for (sg = sg_head; sg_len < len; sg++) {
  108. sg->addr = sg_addr;
  109. sg_addr = min_t(u64, NX_PAGE_NUM(sg_addr + NX_PAGE_SIZE), end_addr);
  110. sg->len = sg_addr - sg->addr;
  111. sg_len += sg->len;
  112. if ((sg - sg_head) == sgmax) {
  113. pr_err("nx: scatter/gather list overflow, pid: %d\n",
  114. current->pid);
  115. return NULL;
  116. }
  117. }
  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 = scatterwalk_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) {
  152. n = scatterwalk_clamp(&walk, len);
  153. if (!n) {
  154. scatterwalk_start(&walk, sg_next(walk.sg));
  155. n = scatterwalk_clamp(&walk, len);
  156. }
  157. dst = scatterwalk_map(&walk);
  158. nx_sg = nx_build_sg_list(nx_sg, dst, n, sglen);
  159. len -= n;
  160. scatterwalk_unmap(dst);
  161. scatterwalk_advance(&walk, n);
  162. scatterwalk_done(&walk, SCATTERWALK_FROM_SG, len);
  163. }
  164. /* return the moved destination pointer */
  165. return nx_sg;
  166. }
  167. /**
  168. * nx_build_sg_lists - walk the input scatterlists and build arrays of NX
  169. * scatterlists based on them.
  170. *
  171. * @nx_ctx: NX crypto context for the lists we're building
  172. * @desc: the block cipher descriptor for the operation
  173. * @dst: destination scatterlist
  174. * @src: source scatterlist
  175. * @nbytes: length of data described in the scatterlists
  176. * @iv: destination for the iv data, if the algorithm requires it
  177. *
  178. * This is common code shared by all the AES algorithms. It uses the block
  179. * cipher walk routines to traverse input and output scatterlists, building
  180. * corresponding NX scatterlists
  181. */
  182. int nx_build_sg_lists(struct nx_crypto_ctx *nx_ctx,
  183. struct blkcipher_desc *desc,
  184. struct scatterlist *dst,
  185. struct scatterlist *src,
  186. unsigned int nbytes,
  187. u8 *iv)
  188. {
  189. struct nx_sg *nx_insg = nx_ctx->in_sg;
  190. struct nx_sg *nx_outsg = nx_ctx->out_sg;
  191. struct blkcipher_walk walk;
  192. int rc;
  193. blkcipher_walk_init(&walk, dst, src, nbytes);
  194. rc = blkcipher_walk_virt_block(desc, &walk, AES_BLOCK_SIZE);
  195. if (rc)
  196. goto out;
  197. if (iv)
  198. memcpy(iv, walk.iv, AES_BLOCK_SIZE);
  199. while (walk.nbytes) {
  200. nx_insg = nx_build_sg_list(nx_insg, walk.src.virt.addr,
  201. walk.nbytes, nx_ctx->ap->sglen);
  202. nx_outsg = nx_build_sg_list(nx_outsg, walk.dst.virt.addr,
  203. walk.nbytes, nx_ctx->ap->sglen);
  204. rc = blkcipher_walk_done(desc, &walk, 0);
  205. if (rc)
  206. break;
  207. }
  208. if (walk.nbytes) {
  209. nx_insg = nx_build_sg_list(nx_insg, walk.src.virt.addr,
  210. walk.nbytes, nx_ctx->ap->sglen);
  211. nx_outsg = nx_build_sg_list(nx_outsg, walk.dst.virt.addr,
  212. walk.nbytes, nx_ctx->ap->sglen);
  213. rc = 0;
  214. }
  215. /* these lengths should be negative, which will indicate to phyp that
  216. * the input and output parameters are scatterlists, not linear
  217. * buffers */
  218. nx_ctx->op.inlen = (nx_ctx->in_sg - nx_insg) * sizeof(struct nx_sg);
  219. nx_ctx->op.outlen = (nx_ctx->out_sg - nx_outsg) * sizeof(struct nx_sg);
  220. out:
  221. return rc;
  222. }
  223. /**
  224. * nx_ctx_init - initialize an nx_ctx's vio_pfo_op struct
  225. *
  226. * @nx_ctx: the nx context to initialize
  227. * @function: the function code for the op
  228. */
  229. void nx_ctx_init(struct nx_crypto_ctx *nx_ctx, unsigned int function)
  230. {
  231. memset(nx_ctx->kmem, 0, nx_ctx->kmem_len);
  232. nx_ctx->csbcpb->csb.valid |= NX_CSB_VALID_BIT;
  233. nx_ctx->op.flags = function;
  234. nx_ctx->op.csbcpb = __pa(nx_ctx->csbcpb);
  235. nx_ctx->op.in = __pa(nx_ctx->in_sg);
  236. nx_ctx->op.out = __pa(nx_ctx->out_sg);
  237. if (nx_ctx->csbcpb_aead) {
  238. nx_ctx->csbcpb_aead->csb.valid |= NX_CSB_VALID_BIT;
  239. nx_ctx->op_aead.flags = function;
  240. nx_ctx->op_aead.csbcpb = __pa(nx_ctx->csbcpb_aead);
  241. nx_ctx->op_aead.in = __pa(nx_ctx->in_sg);
  242. nx_ctx->op_aead.out = __pa(nx_ctx->out_sg);
  243. }
  244. }
  245. static void nx_of_update_status(struct device *dev,
  246. struct property *p,
  247. struct nx_of *props)
  248. {
  249. if (!strncmp(p->value, "okay", p->length)) {
  250. props->status = NX_WAITING;
  251. props->flags |= NX_OF_FLAG_STATUS_SET;
  252. } else {
  253. dev_info(dev, "%s: status '%s' is not 'okay'\n", __func__,
  254. (char *)p->value);
  255. }
  256. }
  257. static void nx_of_update_sglen(struct device *dev,
  258. struct property *p,
  259. struct nx_of *props)
  260. {
  261. if (p->length != sizeof(props->max_sg_len)) {
  262. dev_err(dev, "%s: unexpected format for "
  263. "ibm,max-sg-len property\n", __func__);
  264. dev_dbg(dev, "%s: ibm,max-sg-len is %d bytes "
  265. "long, expected %zd bytes\n", __func__,
  266. p->length, sizeof(props->max_sg_len));
  267. return;
  268. }
  269. props->max_sg_len = *(u32 *)p->value;
  270. props->flags |= NX_OF_FLAG_MAXSGLEN_SET;
  271. }
  272. static void nx_of_update_msc(struct device *dev,
  273. struct property *p,
  274. struct nx_of *props)
  275. {
  276. struct msc_triplet *trip;
  277. struct max_sync_cop *msc;
  278. unsigned int bytes_so_far, i, lenp;
  279. msc = (struct max_sync_cop *)p->value;
  280. lenp = p->length;
  281. /* You can't tell if the data read in for this property is sane by its
  282. * size alone. This is because there are sizes embedded in the data
  283. * structure. The best we can do is check lengths as we parse and bail
  284. * as soon as a length error is detected. */
  285. bytes_so_far = 0;
  286. while ((bytes_so_far + sizeof(struct max_sync_cop)) <= lenp) {
  287. bytes_so_far += sizeof(struct max_sync_cop);
  288. trip = msc->trip;
  289. for (i = 0;
  290. ((bytes_so_far + sizeof(struct msc_triplet)) <= lenp) &&
  291. i < msc->triplets;
  292. i++) {
  293. if (msc->fc > NX_MAX_FC || msc->mode > NX_MAX_MODE) {
  294. dev_err(dev, "unknown function code/mode "
  295. "combo: %d/%d (ignored)\n", msc->fc,
  296. msc->mode);
  297. goto next_loop;
  298. }
  299. switch (trip->keybitlen) {
  300. case 128:
  301. case 160:
  302. props->ap[msc->fc][msc->mode][0].databytelen =
  303. trip->databytelen;
  304. props->ap[msc->fc][msc->mode][0].sglen =
  305. trip->sglen;
  306. break;
  307. case 192:
  308. props->ap[msc->fc][msc->mode][1].databytelen =
  309. trip->databytelen;
  310. props->ap[msc->fc][msc->mode][1].sglen =
  311. trip->sglen;
  312. break;
  313. case 256:
  314. if (msc->fc == NX_FC_AES) {
  315. props->ap[msc->fc][msc->mode][2].
  316. databytelen = trip->databytelen;
  317. props->ap[msc->fc][msc->mode][2].sglen =
  318. trip->sglen;
  319. } else if (msc->fc == NX_FC_AES_HMAC ||
  320. msc->fc == NX_FC_SHA) {
  321. props->ap[msc->fc][msc->mode][1].
  322. databytelen = trip->databytelen;
  323. props->ap[msc->fc][msc->mode][1].sglen =
  324. trip->sglen;
  325. } else {
  326. dev_warn(dev, "unknown function "
  327. "code/key bit len combo"
  328. ": (%u/256)\n", msc->fc);
  329. }
  330. break;
  331. case 512:
  332. props->ap[msc->fc][msc->mode][2].databytelen =
  333. trip->databytelen;
  334. props->ap[msc->fc][msc->mode][2].sglen =
  335. trip->sglen;
  336. break;
  337. default:
  338. dev_warn(dev, "unknown function code/key bit "
  339. "len combo: (%u/%u)\n", msc->fc,
  340. trip->keybitlen);
  341. break;
  342. }
  343. next_loop:
  344. bytes_so_far += sizeof(struct msc_triplet);
  345. trip++;
  346. }
  347. msc = (struct max_sync_cop *)trip;
  348. }
  349. props->flags |= NX_OF_FLAG_MAXSYNCCOP_SET;
  350. }
  351. /**
  352. * nx_of_init - read openFirmware values from the device tree
  353. *
  354. * @dev: device handle
  355. * @props: pointer to struct to hold the properties values
  356. *
  357. * Called once at driver probe time, this function will read out the
  358. * openFirmware properties we use at runtime. If all the OF properties are
  359. * acceptable, when we exit this function props->flags will indicate that
  360. * we're ready to register our crypto algorithms.
  361. */
  362. static void nx_of_init(struct device *dev, struct nx_of *props)
  363. {
  364. struct device_node *base_node = dev->of_node;
  365. struct property *p;
  366. p = of_find_property(base_node, "status", NULL);
  367. if (!p)
  368. dev_info(dev, "%s: property 'status' not found\n", __func__);
  369. else
  370. nx_of_update_status(dev, p, props);
  371. p = of_find_property(base_node, "ibm,max-sg-len", NULL);
  372. if (!p)
  373. dev_info(dev, "%s: property 'ibm,max-sg-len' not found\n",
  374. __func__);
  375. else
  376. nx_of_update_sglen(dev, p, props);
  377. p = of_find_property(base_node, "ibm,max-sync-cop", NULL);
  378. if (!p)
  379. dev_info(dev, "%s: property 'ibm,max-sync-cop' not found\n",
  380. __func__);
  381. else
  382. nx_of_update_msc(dev, p, props);
  383. }
  384. /**
  385. * nx_register_algs - register algorithms with the crypto API
  386. *
  387. * Called from nx_probe()
  388. *
  389. * If all OF properties are in an acceptable state, the driver flags will
  390. * indicate that we're ready and we'll create our debugfs files and register
  391. * out crypto algorithms.
  392. */
  393. static int nx_register_algs(void)
  394. {
  395. int rc = -1;
  396. if (nx_driver.of.flags != NX_OF_FLAG_MASK_READY)
  397. goto out;
  398. memset(&nx_driver.stats, 0, sizeof(struct nx_stats));
  399. rc = NX_DEBUGFS_INIT(&nx_driver);
  400. if (rc)
  401. goto out;
  402. rc = crypto_register_alg(&nx_ecb_aes_alg);
  403. if (rc)
  404. goto out;
  405. rc = crypto_register_alg(&nx_cbc_aes_alg);
  406. if (rc)
  407. goto out_unreg_ecb;
  408. rc = crypto_register_alg(&nx_ctr_aes_alg);
  409. if (rc)
  410. goto out_unreg_cbc;
  411. rc = crypto_register_alg(&nx_ctr3686_aes_alg);
  412. if (rc)
  413. goto out_unreg_ctr;
  414. rc = crypto_register_alg(&nx_gcm_aes_alg);
  415. if (rc)
  416. goto out_unreg_ctr3686;
  417. rc = crypto_register_alg(&nx_gcm4106_aes_alg);
  418. if (rc)
  419. goto out_unreg_gcm;
  420. rc = crypto_register_alg(&nx_ccm_aes_alg);
  421. if (rc)
  422. goto out_unreg_gcm4106;
  423. rc = crypto_register_alg(&nx_ccm4309_aes_alg);
  424. if (rc)
  425. goto out_unreg_ccm;
  426. rc = crypto_register_shash(&nx_shash_sha256_alg);
  427. if (rc)
  428. goto out_unreg_ccm4309;
  429. rc = crypto_register_shash(&nx_shash_sha512_alg);
  430. if (rc)
  431. goto out_unreg_s256;
  432. rc = crypto_register_shash(&nx_shash_aes_xcbc_alg);
  433. if (rc)
  434. goto out_unreg_s512;
  435. nx_driver.of.status = NX_OKAY;
  436. goto out;
  437. out_unreg_s512:
  438. crypto_unregister_shash(&nx_shash_sha512_alg);
  439. out_unreg_s256:
  440. crypto_unregister_shash(&nx_shash_sha256_alg);
  441. out_unreg_ccm4309:
  442. crypto_unregister_alg(&nx_ccm4309_aes_alg);
  443. out_unreg_ccm:
  444. crypto_unregister_alg(&nx_ccm_aes_alg);
  445. out_unreg_gcm4106:
  446. crypto_unregister_alg(&nx_gcm4106_aes_alg);
  447. out_unreg_gcm:
  448. crypto_unregister_alg(&nx_gcm_aes_alg);
  449. out_unreg_ctr3686:
  450. crypto_unregister_alg(&nx_ctr3686_aes_alg);
  451. out_unreg_ctr:
  452. crypto_unregister_alg(&nx_ctr_aes_alg);
  453. out_unreg_cbc:
  454. crypto_unregister_alg(&nx_cbc_aes_alg);
  455. out_unreg_ecb:
  456. crypto_unregister_alg(&nx_ecb_aes_alg);
  457. out:
  458. return rc;
  459. }
  460. /**
  461. * nx_crypto_ctx_init - create and initialize a crypto api context
  462. *
  463. * @nx_ctx: the crypto api context
  464. * @fc: function code for the context
  465. * @mode: the function code specific mode for this context
  466. */
  467. static int nx_crypto_ctx_init(struct nx_crypto_ctx *nx_ctx, u32 fc, u32 mode)
  468. {
  469. if (nx_driver.of.status != NX_OKAY) {
  470. pr_err("Attempt to initialize NX crypto context while device "
  471. "is not available!\n");
  472. return -ENODEV;
  473. }
  474. /* we need an extra page for csbcpb_aead for these modes */
  475. if (mode == NX_MODE_AES_GCM || mode == NX_MODE_AES_CCM)
  476. nx_ctx->kmem_len = (4 * NX_PAGE_SIZE) +
  477. sizeof(struct nx_csbcpb);
  478. else
  479. nx_ctx->kmem_len = (3 * NX_PAGE_SIZE) +
  480. sizeof(struct nx_csbcpb);
  481. nx_ctx->kmem = kmalloc(nx_ctx->kmem_len, GFP_KERNEL);
  482. if (!nx_ctx->kmem)
  483. return -ENOMEM;
  484. /* the csbcpb and scatterlists must be 4K aligned pages */
  485. nx_ctx->csbcpb = (struct nx_csbcpb *)(round_up((u64)nx_ctx->kmem,
  486. (u64)NX_PAGE_SIZE));
  487. nx_ctx->in_sg = (struct nx_sg *)((u8 *)nx_ctx->csbcpb + NX_PAGE_SIZE);
  488. nx_ctx->out_sg = (struct nx_sg *)((u8 *)nx_ctx->in_sg + NX_PAGE_SIZE);
  489. if (mode == NX_MODE_AES_GCM || mode == NX_MODE_AES_CCM)
  490. nx_ctx->csbcpb_aead =
  491. (struct nx_csbcpb *)((u8 *)nx_ctx->out_sg +
  492. NX_PAGE_SIZE);
  493. /* give each context a pointer to global stats and their OF
  494. * properties */
  495. nx_ctx->stats = &nx_driver.stats;
  496. memcpy(nx_ctx->props, nx_driver.of.ap[fc][mode],
  497. sizeof(struct alg_props) * 3);
  498. return 0;
  499. }
  500. /* entry points from the crypto tfm initializers */
  501. int nx_crypto_ctx_aes_ccm_init(struct crypto_tfm *tfm)
  502. {
  503. return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
  504. NX_MODE_AES_CCM);
  505. }
  506. int nx_crypto_ctx_aes_gcm_init(struct crypto_tfm *tfm)
  507. {
  508. return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
  509. NX_MODE_AES_GCM);
  510. }
  511. int nx_crypto_ctx_aes_ctr_init(struct crypto_tfm *tfm)
  512. {
  513. return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
  514. NX_MODE_AES_CTR);
  515. }
  516. int nx_crypto_ctx_aes_cbc_init(struct crypto_tfm *tfm)
  517. {
  518. return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
  519. NX_MODE_AES_CBC);
  520. }
  521. int nx_crypto_ctx_aes_ecb_init(struct crypto_tfm *tfm)
  522. {
  523. return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
  524. NX_MODE_AES_ECB);
  525. }
  526. int nx_crypto_ctx_sha_init(struct crypto_tfm *tfm)
  527. {
  528. return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_SHA, NX_MODE_SHA);
  529. }
  530. int nx_crypto_ctx_aes_xcbc_init(struct crypto_tfm *tfm)
  531. {
  532. return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
  533. NX_MODE_AES_XCBC_MAC);
  534. }
  535. /**
  536. * nx_crypto_ctx_exit - destroy a crypto api context
  537. *
  538. * @tfm: the crypto transform pointer for the context
  539. *
  540. * As crypto API contexts are destroyed, this exit hook is called to free the
  541. * memory associated with it.
  542. */
  543. void nx_crypto_ctx_exit(struct crypto_tfm *tfm)
  544. {
  545. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
  546. kzfree(nx_ctx->kmem);
  547. nx_ctx->csbcpb = NULL;
  548. nx_ctx->csbcpb_aead = NULL;
  549. nx_ctx->in_sg = NULL;
  550. nx_ctx->out_sg = NULL;
  551. }
  552. static int __devinit nx_probe(struct vio_dev *viodev,
  553. const struct vio_device_id *id)
  554. {
  555. dev_dbg(&viodev->dev, "driver probed: %s resource id: 0x%x\n",
  556. viodev->name, viodev->resource_id);
  557. if (nx_driver.viodev) {
  558. dev_err(&viodev->dev, "%s: Attempt to register more than one "
  559. "instance of the hardware\n", __func__);
  560. return -EINVAL;
  561. }
  562. nx_driver.viodev = viodev;
  563. nx_of_init(&viodev->dev, &nx_driver.of);
  564. return nx_register_algs();
  565. }
  566. static int __devexit nx_remove(struct vio_dev *viodev)
  567. {
  568. dev_dbg(&viodev->dev, "entering nx_remove for UA 0x%x\n",
  569. viodev->unit_address);
  570. if (nx_driver.of.status == NX_OKAY) {
  571. NX_DEBUGFS_FINI(&nx_driver);
  572. crypto_unregister_alg(&nx_ccm_aes_alg);
  573. crypto_unregister_alg(&nx_ccm4309_aes_alg);
  574. crypto_unregister_alg(&nx_gcm_aes_alg);
  575. crypto_unregister_alg(&nx_gcm4106_aes_alg);
  576. crypto_unregister_alg(&nx_ctr_aes_alg);
  577. crypto_unregister_alg(&nx_ctr3686_aes_alg);
  578. crypto_unregister_alg(&nx_cbc_aes_alg);
  579. crypto_unregister_alg(&nx_ecb_aes_alg);
  580. crypto_unregister_shash(&nx_shash_sha256_alg);
  581. crypto_unregister_shash(&nx_shash_sha512_alg);
  582. crypto_unregister_shash(&nx_shash_aes_xcbc_alg);
  583. }
  584. return 0;
  585. }
  586. /* module wide initialization/cleanup */
  587. static int __init nx_init(void)
  588. {
  589. return vio_register_driver(&nx_driver.viodriver);
  590. }
  591. static void __exit nx_fini(void)
  592. {
  593. vio_unregister_driver(&nx_driver.viodriver);
  594. }
  595. static struct vio_device_id nx_crypto_driver_ids[] __devinitdata = {
  596. { "ibm,sym-encryption-v1", "ibm,sym-encryption" },
  597. { "", "" }
  598. };
  599. MODULE_DEVICE_TABLE(vio, nx_crypto_driver_ids);
  600. /* driver state structure */
  601. struct nx_crypto_driver nx_driver = {
  602. .viodriver = {
  603. .id_table = nx_crypto_driver_ids,
  604. .probe = nx_probe,
  605. .remove = nx_remove,
  606. .name = NX_NAME,
  607. },
  608. };
  609. module_init(nx_init);
  610. module_exit(nx_fini);
  611. MODULE_AUTHOR("Kent Yoder <yoder1@us.ibm.com>");
  612. MODULE_DESCRIPTION(NX_STRING);
  613. MODULE_LICENSE("GPL");
  614. MODULE_VERSION(NX_VERSION);