nx.c 24 KB

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