nx.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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/aead.h>
  22. #include <crypto/internal/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/scatterlist.h>
  32. #include <linux/device.h>
  33. #include <linux/of.h>
  34. #include <linux/types.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,
  192. struct nx_sg *end,
  193. unsigned int delta,
  194. unsigned int *nbytes)
  195. {
  196. long int oplen;
  197. long int data_back;
  198. unsigned int is_delta = delta;
  199. while (delta && end > sg) {
  200. struct nx_sg *last = end - 1;
  201. if (last->len > delta) {
  202. last->len -= delta;
  203. delta = 0;
  204. } else {
  205. end--;
  206. delta -= last->len;
  207. }
  208. }
  209. /* There are cases where we need to crop list in order to make it
  210. * a block size multiple, but we also need to align data. In order to
  211. * that we need to calculate how much we need to put back to be
  212. * processed
  213. */
  214. oplen = (sg - end) * sizeof(struct nx_sg);
  215. if (is_delta) {
  216. data_back = (abs(oplen) / AES_BLOCK_SIZE) * sg->len;
  217. data_back = *nbytes - (data_back & ~(AES_BLOCK_SIZE - 1));
  218. *nbytes -= data_back;
  219. }
  220. return oplen;
  221. }
  222. /**
  223. * nx_build_sg_lists - walk the input scatterlists and build arrays of NX
  224. * scatterlists based on them.
  225. *
  226. * @nx_ctx: NX crypto context for the lists we're building
  227. * @desc: the block cipher descriptor for the operation
  228. * @dst: destination scatterlist
  229. * @src: source scatterlist
  230. * @nbytes: length of data described in the scatterlists
  231. * @offset: number of bytes to fast-forward past at the beginning of
  232. * scatterlists.
  233. * @iv: destination for the iv data, if the algorithm requires it
  234. *
  235. * This is common code shared by all the AES algorithms. It uses the block
  236. * cipher walk routines to traverse input and output scatterlists, building
  237. * corresponding NX scatterlists
  238. */
  239. int nx_build_sg_lists(struct nx_crypto_ctx *nx_ctx,
  240. struct blkcipher_desc *desc,
  241. struct scatterlist *dst,
  242. struct scatterlist *src,
  243. unsigned int *nbytes,
  244. unsigned int offset,
  245. u8 *iv)
  246. {
  247. unsigned int delta = 0;
  248. unsigned int total = *nbytes;
  249. struct nx_sg *nx_insg = nx_ctx->in_sg;
  250. struct nx_sg *nx_outsg = nx_ctx->out_sg;
  251. unsigned int max_sg_len;
  252. max_sg_len = min_t(u64, nx_ctx->ap->sglen,
  253. nx_driver.of.max_sg_len/sizeof(struct nx_sg));
  254. max_sg_len = min_t(u64, max_sg_len,
  255. nx_ctx->ap->databytelen/NX_PAGE_SIZE);
  256. if (iv)
  257. memcpy(iv, desc->info, AES_BLOCK_SIZE);
  258. *nbytes = min_t(u64, *nbytes, nx_ctx->ap->databytelen);
  259. nx_outsg = nx_walk_and_build(nx_outsg, max_sg_len, dst,
  260. offset, nbytes);
  261. nx_insg = nx_walk_and_build(nx_insg, max_sg_len, src,
  262. offset, nbytes);
  263. if (*nbytes < total)
  264. delta = *nbytes - (*nbytes & ~(AES_BLOCK_SIZE - 1));
  265. /* these lengths should be negative, which will indicate to phyp that
  266. * the input and output parameters are scatterlists, not linear
  267. * buffers */
  268. nx_ctx->op.inlen = trim_sg_list(nx_ctx->in_sg, nx_insg, delta, nbytes);
  269. nx_ctx->op.outlen = trim_sg_list(nx_ctx->out_sg, nx_outsg, delta, nbytes);
  270. return 0;
  271. }
  272. /**
  273. * nx_ctx_init - initialize an nx_ctx's vio_pfo_op struct
  274. *
  275. * @nx_ctx: the nx context to initialize
  276. * @function: the function code for the op
  277. */
  278. void nx_ctx_init(struct nx_crypto_ctx *nx_ctx, unsigned int function)
  279. {
  280. spin_lock_init(&nx_ctx->lock);
  281. memset(nx_ctx->kmem, 0, nx_ctx->kmem_len);
  282. nx_ctx->csbcpb->csb.valid |= NX_CSB_VALID_BIT;
  283. nx_ctx->op.flags = function;
  284. nx_ctx->op.csbcpb = __pa(nx_ctx->csbcpb);
  285. nx_ctx->op.in = __pa(nx_ctx->in_sg);
  286. nx_ctx->op.out = __pa(nx_ctx->out_sg);
  287. if (nx_ctx->csbcpb_aead) {
  288. nx_ctx->csbcpb_aead->csb.valid |= NX_CSB_VALID_BIT;
  289. nx_ctx->op_aead.flags = function;
  290. nx_ctx->op_aead.csbcpb = __pa(nx_ctx->csbcpb_aead);
  291. nx_ctx->op_aead.in = __pa(nx_ctx->in_sg);
  292. nx_ctx->op_aead.out = __pa(nx_ctx->out_sg);
  293. }
  294. }
  295. static void nx_of_update_status(struct device *dev,
  296. struct property *p,
  297. struct nx_of *props)
  298. {
  299. if (!strncmp(p->value, "okay", p->length)) {
  300. props->status = NX_WAITING;
  301. props->flags |= NX_OF_FLAG_STATUS_SET;
  302. } else {
  303. dev_info(dev, "%s: status '%s' is not 'okay'\n", __func__,
  304. (char *)p->value);
  305. }
  306. }
  307. static void nx_of_update_sglen(struct device *dev,
  308. struct property *p,
  309. struct nx_of *props)
  310. {
  311. if (p->length != sizeof(props->max_sg_len)) {
  312. dev_err(dev, "%s: unexpected format for "
  313. "ibm,max-sg-len property\n", __func__);
  314. dev_dbg(dev, "%s: ibm,max-sg-len is %d bytes "
  315. "long, expected %zd bytes\n", __func__,
  316. p->length, sizeof(props->max_sg_len));
  317. return;
  318. }
  319. props->max_sg_len = *(u32 *)p->value;
  320. props->flags |= NX_OF_FLAG_MAXSGLEN_SET;
  321. }
  322. static void nx_of_update_msc(struct device *dev,
  323. struct property *p,
  324. struct nx_of *props)
  325. {
  326. struct msc_triplet *trip;
  327. struct max_sync_cop *msc;
  328. unsigned int bytes_so_far, i, lenp;
  329. msc = (struct max_sync_cop *)p->value;
  330. lenp = p->length;
  331. /* You can't tell if the data read in for this property is sane by its
  332. * size alone. This is because there are sizes embedded in the data
  333. * structure. The best we can do is check lengths as we parse and bail
  334. * as soon as a length error is detected. */
  335. bytes_so_far = 0;
  336. while ((bytes_so_far + sizeof(struct max_sync_cop)) <= lenp) {
  337. bytes_so_far += sizeof(struct max_sync_cop);
  338. trip = msc->trip;
  339. for (i = 0;
  340. ((bytes_so_far + sizeof(struct msc_triplet)) <= lenp) &&
  341. i < msc->triplets;
  342. i++) {
  343. if (msc->fc >= NX_MAX_FC || msc->mode >= NX_MAX_MODE) {
  344. dev_err(dev, "unknown function code/mode "
  345. "combo: %d/%d (ignored)\n", msc->fc,
  346. msc->mode);
  347. goto next_loop;
  348. }
  349. if (!trip->sglen || trip->databytelen < NX_PAGE_SIZE) {
  350. dev_warn(dev, "bogus sglen/databytelen: "
  351. "%u/%u (ignored)\n", trip->sglen,
  352. trip->databytelen);
  353. goto next_loop;
  354. }
  355. switch (trip->keybitlen) {
  356. case 128:
  357. case 160:
  358. props->ap[msc->fc][msc->mode][0].databytelen =
  359. trip->databytelen;
  360. props->ap[msc->fc][msc->mode][0].sglen =
  361. trip->sglen;
  362. break;
  363. case 192:
  364. props->ap[msc->fc][msc->mode][1].databytelen =
  365. trip->databytelen;
  366. props->ap[msc->fc][msc->mode][1].sglen =
  367. trip->sglen;
  368. break;
  369. case 256:
  370. if (msc->fc == NX_FC_AES) {
  371. props->ap[msc->fc][msc->mode][2].
  372. databytelen = trip->databytelen;
  373. props->ap[msc->fc][msc->mode][2].sglen =
  374. trip->sglen;
  375. } else if (msc->fc == NX_FC_AES_HMAC ||
  376. msc->fc == NX_FC_SHA) {
  377. props->ap[msc->fc][msc->mode][1].
  378. databytelen = trip->databytelen;
  379. props->ap[msc->fc][msc->mode][1].sglen =
  380. trip->sglen;
  381. } else {
  382. dev_warn(dev, "unknown function "
  383. "code/key bit len combo"
  384. ": (%u/256)\n", msc->fc);
  385. }
  386. break;
  387. case 512:
  388. props->ap[msc->fc][msc->mode][2].databytelen =
  389. trip->databytelen;
  390. props->ap[msc->fc][msc->mode][2].sglen =
  391. trip->sglen;
  392. break;
  393. default:
  394. dev_warn(dev, "unknown function code/key bit "
  395. "len combo: (%u/%u)\n", msc->fc,
  396. trip->keybitlen);
  397. break;
  398. }
  399. next_loop:
  400. bytes_so_far += sizeof(struct msc_triplet);
  401. trip++;
  402. }
  403. msc = (struct max_sync_cop *)trip;
  404. }
  405. props->flags |= NX_OF_FLAG_MAXSYNCCOP_SET;
  406. }
  407. /**
  408. * nx_of_init - read openFirmware values from the device tree
  409. *
  410. * @dev: device handle
  411. * @props: pointer to struct to hold the properties values
  412. *
  413. * Called once at driver probe time, this function will read out the
  414. * openFirmware properties we use at runtime. If all the OF properties are
  415. * acceptable, when we exit this function props->flags will indicate that
  416. * we're ready to register our crypto algorithms.
  417. */
  418. static void nx_of_init(struct device *dev, struct nx_of *props)
  419. {
  420. struct device_node *base_node = dev->of_node;
  421. struct property *p;
  422. p = of_find_property(base_node, "status", NULL);
  423. if (!p)
  424. dev_info(dev, "%s: property 'status' not found\n", __func__);
  425. else
  426. nx_of_update_status(dev, p, props);
  427. p = of_find_property(base_node, "ibm,max-sg-len", NULL);
  428. if (!p)
  429. dev_info(dev, "%s: property 'ibm,max-sg-len' not found\n",
  430. __func__);
  431. else
  432. nx_of_update_sglen(dev, p, props);
  433. p = of_find_property(base_node, "ibm,max-sync-cop", NULL);
  434. if (!p)
  435. dev_info(dev, "%s: property 'ibm,max-sync-cop' not found\n",
  436. __func__);
  437. else
  438. nx_of_update_msc(dev, p, props);
  439. }
  440. static bool nx_check_prop(struct device *dev, u32 fc, u32 mode, int slot)
  441. {
  442. struct alg_props *props = &nx_driver.of.ap[fc][mode][slot];
  443. if (!props->sglen || props->databytelen < NX_PAGE_SIZE) {
  444. if (dev)
  445. dev_warn(dev, "bogus sglen/databytelen for %u/%u/%u: "
  446. "%u/%u (ignored)\n", fc, mode, slot,
  447. props->sglen, props->databytelen);
  448. return false;
  449. }
  450. return true;
  451. }
  452. static bool nx_check_props(struct device *dev, u32 fc, u32 mode)
  453. {
  454. int i;
  455. for (i = 0; i < 3; i++)
  456. if (!nx_check_prop(dev, fc, mode, i))
  457. return false;
  458. return true;
  459. }
  460. static int nx_register_alg(struct crypto_alg *alg, u32 fc, u32 mode)
  461. {
  462. return nx_check_props(&nx_driver.viodev->dev, fc, mode) ?
  463. crypto_register_alg(alg) : 0;
  464. }
  465. static int nx_register_aead(struct aead_alg *alg, u32 fc, u32 mode)
  466. {
  467. return nx_check_props(&nx_driver.viodev->dev, fc, mode) ?
  468. crypto_register_aead(alg) : 0;
  469. }
  470. static int nx_register_shash(struct shash_alg *alg, u32 fc, u32 mode, int slot)
  471. {
  472. return (slot >= 0 ? nx_check_prop(&nx_driver.viodev->dev,
  473. fc, mode, slot) :
  474. nx_check_props(&nx_driver.viodev->dev, fc, mode)) ?
  475. crypto_register_shash(alg) : 0;
  476. }
  477. static void nx_unregister_alg(struct crypto_alg *alg, u32 fc, u32 mode)
  478. {
  479. if (nx_check_props(NULL, fc, mode))
  480. crypto_unregister_alg(alg);
  481. }
  482. static void nx_unregister_aead(struct aead_alg *alg, u32 fc, u32 mode)
  483. {
  484. if (nx_check_props(NULL, fc, mode))
  485. crypto_unregister_aead(alg);
  486. }
  487. static void nx_unregister_shash(struct shash_alg *alg, u32 fc, u32 mode,
  488. int slot)
  489. {
  490. if (slot >= 0 ? nx_check_prop(NULL, fc, mode, slot) :
  491. nx_check_props(NULL, fc, mode))
  492. crypto_unregister_shash(alg);
  493. }
  494. /**
  495. * nx_register_algs - register algorithms with the crypto API
  496. *
  497. * Called from nx_probe()
  498. *
  499. * If all OF properties are in an acceptable state, the driver flags will
  500. * indicate that we're ready and we'll create our debugfs files and register
  501. * out crypto algorithms.
  502. */
  503. static int nx_register_algs(void)
  504. {
  505. int rc = -1;
  506. if (nx_driver.of.flags != NX_OF_FLAG_MASK_READY)
  507. goto out;
  508. memset(&nx_driver.stats, 0, sizeof(struct nx_stats));
  509. rc = NX_DEBUGFS_INIT(&nx_driver);
  510. if (rc)
  511. goto out;
  512. nx_driver.of.status = NX_OKAY;
  513. rc = nx_register_alg(&nx_ecb_aes_alg, NX_FC_AES, NX_MODE_AES_ECB);
  514. if (rc)
  515. goto out;
  516. rc = nx_register_alg(&nx_cbc_aes_alg, NX_FC_AES, NX_MODE_AES_CBC);
  517. if (rc)
  518. goto out_unreg_ecb;
  519. rc = nx_register_alg(&nx_ctr3686_aes_alg, NX_FC_AES, NX_MODE_AES_CTR);
  520. if (rc)
  521. goto out_unreg_cbc;
  522. rc = nx_register_aead(&nx_gcm_aes_alg, NX_FC_AES, NX_MODE_AES_GCM);
  523. if (rc)
  524. goto out_unreg_ctr3686;
  525. rc = nx_register_aead(&nx_gcm4106_aes_alg, NX_FC_AES, NX_MODE_AES_GCM);
  526. if (rc)
  527. goto out_unreg_gcm;
  528. rc = nx_register_aead(&nx_ccm_aes_alg, NX_FC_AES, NX_MODE_AES_CCM);
  529. if (rc)
  530. goto out_unreg_gcm4106;
  531. rc = nx_register_aead(&nx_ccm4309_aes_alg, NX_FC_AES, NX_MODE_AES_CCM);
  532. if (rc)
  533. goto out_unreg_ccm;
  534. rc = nx_register_shash(&nx_shash_sha256_alg, NX_FC_SHA, NX_MODE_SHA,
  535. NX_PROPS_SHA256);
  536. if (rc)
  537. goto out_unreg_ccm4309;
  538. rc = nx_register_shash(&nx_shash_sha512_alg, NX_FC_SHA, NX_MODE_SHA,
  539. NX_PROPS_SHA512);
  540. if (rc)
  541. goto out_unreg_s256;
  542. rc = nx_register_shash(&nx_shash_aes_xcbc_alg,
  543. NX_FC_AES, NX_MODE_AES_XCBC_MAC, -1);
  544. if (rc)
  545. goto out_unreg_s512;
  546. goto out;
  547. out_unreg_s512:
  548. nx_unregister_shash(&nx_shash_sha512_alg, NX_FC_SHA, NX_MODE_SHA,
  549. NX_PROPS_SHA512);
  550. out_unreg_s256:
  551. nx_unregister_shash(&nx_shash_sha256_alg, NX_FC_SHA, NX_MODE_SHA,
  552. NX_PROPS_SHA256);
  553. out_unreg_ccm4309:
  554. nx_unregister_aead(&nx_ccm4309_aes_alg, NX_FC_AES, NX_MODE_AES_CCM);
  555. out_unreg_ccm:
  556. nx_unregister_aead(&nx_ccm_aes_alg, NX_FC_AES, NX_MODE_AES_CCM);
  557. out_unreg_gcm4106:
  558. nx_unregister_aead(&nx_gcm4106_aes_alg, NX_FC_AES, NX_MODE_AES_GCM);
  559. out_unreg_gcm:
  560. nx_unregister_aead(&nx_gcm_aes_alg, NX_FC_AES, NX_MODE_AES_GCM);
  561. out_unreg_ctr3686:
  562. nx_unregister_alg(&nx_ctr3686_aes_alg, NX_FC_AES, NX_MODE_AES_CTR);
  563. out_unreg_cbc:
  564. nx_unregister_alg(&nx_cbc_aes_alg, NX_FC_AES, NX_MODE_AES_CBC);
  565. out_unreg_ecb:
  566. nx_unregister_alg(&nx_ecb_aes_alg, NX_FC_AES, NX_MODE_AES_ECB);
  567. out:
  568. return rc;
  569. }
  570. /**
  571. * nx_crypto_ctx_init - create and initialize a crypto api context
  572. *
  573. * @nx_ctx: the crypto api context
  574. * @fc: function code for the context
  575. * @mode: the function code specific mode for this context
  576. */
  577. static int nx_crypto_ctx_init(struct nx_crypto_ctx *nx_ctx, u32 fc, u32 mode)
  578. {
  579. if (nx_driver.of.status != NX_OKAY) {
  580. pr_err("Attempt to initialize NX crypto context while device "
  581. "is not available!\n");
  582. return -ENODEV;
  583. }
  584. /* we need an extra page for csbcpb_aead for these modes */
  585. if (mode == NX_MODE_AES_GCM || mode == NX_MODE_AES_CCM)
  586. nx_ctx->kmem_len = (5 * NX_PAGE_SIZE) +
  587. sizeof(struct nx_csbcpb);
  588. else
  589. nx_ctx->kmem_len = (4 * NX_PAGE_SIZE) +
  590. sizeof(struct nx_csbcpb);
  591. nx_ctx->kmem = kmalloc(nx_ctx->kmem_len, GFP_KERNEL);
  592. if (!nx_ctx->kmem)
  593. return -ENOMEM;
  594. /* the csbcpb and scatterlists must be 4K aligned pages */
  595. nx_ctx->csbcpb = (struct nx_csbcpb *)(round_up((u64)nx_ctx->kmem,
  596. (u64)NX_PAGE_SIZE));
  597. nx_ctx->in_sg = (struct nx_sg *)((u8 *)nx_ctx->csbcpb + NX_PAGE_SIZE);
  598. nx_ctx->out_sg = (struct nx_sg *)((u8 *)nx_ctx->in_sg + NX_PAGE_SIZE);
  599. if (mode == NX_MODE_AES_GCM || mode == NX_MODE_AES_CCM)
  600. nx_ctx->csbcpb_aead =
  601. (struct nx_csbcpb *)((u8 *)nx_ctx->out_sg +
  602. NX_PAGE_SIZE);
  603. /* give each context a pointer to global stats and their OF
  604. * properties */
  605. nx_ctx->stats = &nx_driver.stats;
  606. memcpy(nx_ctx->props, nx_driver.of.ap[fc][mode],
  607. sizeof(struct alg_props) * 3);
  608. return 0;
  609. }
  610. /* entry points from the crypto tfm initializers */
  611. int nx_crypto_ctx_aes_ccm_init(struct crypto_aead *tfm)
  612. {
  613. crypto_aead_set_reqsize(tfm, sizeof(struct nx_ccm_rctx));
  614. return nx_crypto_ctx_init(crypto_aead_ctx(tfm), NX_FC_AES,
  615. NX_MODE_AES_CCM);
  616. }
  617. int nx_crypto_ctx_aes_gcm_init(struct crypto_aead *tfm)
  618. {
  619. crypto_aead_set_reqsize(tfm, sizeof(struct nx_gcm_rctx));
  620. return nx_crypto_ctx_init(crypto_aead_ctx(tfm), NX_FC_AES,
  621. NX_MODE_AES_GCM);
  622. }
  623. int nx_crypto_ctx_aes_ctr_init(struct crypto_tfm *tfm)
  624. {
  625. return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
  626. NX_MODE_AES_CTR);
  627. }
  628. int nx_crypto_ctx_aes_cbc_init(struct crypto_tfm *tfm)
  629. {
  630. return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
  631. NX_MODE_AES_CBC);
  632. }
  633. int nx_crypto_ctx_aes_ecb_init(struct crypto_tfm *tfm)
  634. {
  635. return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
  636. NX_MODE_AES_ECB);
  637. }
  638. int nx_crypto_ctx_sha_init(struct crypto_tfm *tfm)
  639. {
  640. return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_SHA, NX_MODE_SHA);
  641. }
  642. int nx_crypto_ctx_aes_xcbc_init(struct crypto_tfm *tfm)
  643. {
  644. return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
  645. NX_MODE_AES_XCBC_MAC);
  646. }
  647. /**
  648. * nx_crypto_ctx_exit - destroy a crypto api context
  649. *
  650. * @tfm: the crypto transform pointer for the context
  651. *
  652. * As crypto API contexts are destroyed, this exit hook is called to free the
  653. * memory associated with it.
  654. */
  655. void nx_crypto_ctx_exit(struct crypto_tfm *tfm)
  656. {
  657. struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
  658. kzfree(nx_ctx->kmem);
  659. nx_ctx->csbcpb = NULL;
  660. nx_ctx->csbcpb_aead = NULL;
  661. nx_ctx->in_sg = NULL;
  662. nx_ctx->out_sg = NULL;
  663. }
  664. void nx_crypto_ctx_aead_exit(struct crypto_aead *tfm)
  665. {
  666. struct nx_crypto_ctx *nx_ctx = crypto_aead_ctx(tfm);
  667. kzfree(nx_ctx->kmem);
  668. }
  669. static int nx_probe(struct vio_dev *viodev, const struct vio_device_id *id)
  670. {
  671. dev_dbg(&viodev->dev, "driver probed: %s resource id: 0x%x\n",
  672. viodev->name, viodev->resource_id);
  673. if (nx_driver.viodev) {
  674. dev_err(&viodev->dev, "%s: Attempt to register more than one "
  675. "instance of the hardware\n", __func__);
  676. return -EINVAL;
  677. }
  678. nx_driver.viodev = viodev;
  679. nx_of_init(&viodev->dev, &nx_driver.of);
  680. return nx_register_algs();
  681. }
  682. static int nx_remove(struct vio_dev *viodev)
  683. {
  684. dev_dbg(&viodev->dev, "entering nx_remove for UA 0x%x\n",
  685. viodev->unit_address);
  686. if (nx_driver.of.status == NX_OKAY) {
  687. NX_DEBUGFS_FINI(&nx_driver);
  688. nx_unregister_shash(&nx_shash_aes_xcbc_alg,
  689. NX_FC_AES, NX_MODE_AES_XCBC_MAC, -1);
  690. nx_unregister_shash(&nx_shash_sha512_alg,
  691. NX_FC_SHA, NX_MODE_SHA, NX_PROPS_SHA256);
  692. nx_unregister_shash(&nx_shash_sha256_alg,
  693. NX_FC_SHA, NX_MODE_SHA, NX_PROPS_SHA512);
  694. nx_unregister_aead(&nx_ccm4309_aes_alg,
  695. NX_FC_AES, NX_MODE_AES_CCM);
  696. nx_unregister_aead(&nx_ccm_aes_alg, NX_FC_AES, NX_MODE_AES_CCM);
  697. nx_unregister_aead(&nx_gcm4106_aes_alg,
  698. NX_FC_AES, NX_MODE_AES_GCM);
  699. nx_unregister_aead(&nx_gcm_aes_alg,
  700. NX_FC_AES, NX_MODE_AES_GCM);
  701. nx_unregister_alg(&nx_ctr3686_aes_alg,
  702. NX_FC_AES, NX_MODE_AES_CTR);
  703. nx_unregister_alg(&nx_cbc_aes_alg, NX_FC_AES, NX_MODE_AES_CBC);
  704. nx_unregister_alg(&nx_ecb_aes_alg, NX_FC_AES, NX_MODE_AES_ECB);
  705. }
  706. return 0;
  707. }
  708. /* module wide initialization/cleanup */
  709. static int __init nx_init(void)
  710. {
  711. return vio_register_driver(&nx_driver.viodriver);
  712. }
  713. static void __exit nx_fini(void)
  714. {
  715. vio_unregister_driver(&nx_driver.viodriver);
  716. }
  717. static struct vio_device_id nx_crypto_driver_ids[] = {
  718. { "ibm,sym-encryption-v1", "ibm,sym-encryption" },
  719. { "", "" }
  720. };
  721. MODULE_DEVICE_TABLE(vio, nx_crypto_driver_ids);
  722. /* driver state structure */
  723. struct nx_crypto_driver nx_driver = {
  724. .viodriver = {
  725. .id_table = nx_crypto_driver_ids,
  726. .probe = nx_probe,
  727. .remove = nx_remove,
  728. .name = NX_NAME,
  729. },
  730. };
  731. module_init(nx_init);
  732. module_exit(nx_fini);
  733. MODULE_AUTHOR("Kent Yoder <yoder1@us.ibm.com>");
  734. MODULE_DESCRIPTION(NX_STRING);
  735. MODULE_LICENSE("GPL");
  736. MODULE_VERSION(NX_VERSION);