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