blob: 4856f7287eae8ed2f6e29d7a55070e54f9f43a5e [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);
Marcelo Cerric8491632013-08-12 18:49:37 -030064 } while (rc == -EBUSY && !may_sleep && retries--);
Kent Yoderae0222b2012-05-14 10:59:38 +000065
66 if (rc) {
67 dev_dbg(&viodev->dev, "vio_h_cop_sync failed: rc: %d "
68 "hcall rc: %ld\n", rc, op->hcall_err);
69 atomic_inc(&(nx_ctx->stats->errors));
70 atomic_set(&(nx_ctx->stats->last_error), op->hcall_err);
71 atomic_set(&(nx_ctx->stats->last_error_pid), current->pid);
72 }
73
74 return rc;
75}
76
77/**
78 * nx_build_sg_list - build an NX scatter list describing a single buffer
79 *
80 * @sg_head: pointer to the first scatter list element to build
81 * @start_addr: pointer to the linear buffer
82 * @len: length of the data at @start_addr
83 * @sgmax: the largest number of scatter list elements we're allowed to create
84 *
85 * This function will start writing nx_sg elements at @sg_head and keep
86 * writing them until all of the data from @start_addr is described or
87 * until sgmax elements have been written. Scatter list elements will be
88 * created such that none of the elements describes a buffer that crosses a 4K
89 * boundary.
90 */
91struct nx_sg *nx_build_sg_list(struct nx_sg *sg_head,
92 u8 *start_addr,
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -020093 unsigned int *len,
Kent Yoderae0222b2012-05-14 10:59:38 +000094 u32 sgmax)
95{
96 unsigned int sg_len = 0;
97 struct nx_sg *sg;
98 u64 sg_addr = (u64)start_addr;
99 u64 end_addr;
100
101 /* determine the start and end for this address range - slightly
102 * different if this is in VMALLOC_REGION */
103 if (is_vmalloc_addr(start_addr))
Michael Ellerman7187daf2012-07-25 21:19:48 +0000104 sg_addr = page_to_phys(vmalloc_to_page(start_addr))
Kent Yoderae0222b2012-05-14 10:59:38 +0000105 + offset_in_page(sg_addr);
106 else
Michael Ellerman7187daf2012-07-25 21:19:48 +0000107 sg_addr = __pa(sg_addr);
Kent Yoderae0222b2012-05-14 10:59:38 +0000108
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200109 end_addr = sg_addr + *len;
Kent Yoderae0222b2012-05-14 10:59:38 +0000110
111 /* each iteration will write one struct nx_sg element and add the
112 * length of data described by that element to sg_len. Once @len bytes
113 * have been described (or @sgmax elements have been written), the
114 * loop ends. min_t is used to ensure @end_addr falls on the same page
115 * as sg_addr, if not, we need to create another nx_sg element for the
Marcelo Cerri2b7c15c2013-08-02 12:09:51 +0000116 * data on the next page.
117 *
118 * Also when using vmalloc'ed data, every time that a system page
119 * boundary is crossed the physical address needs to be re-calculated.
120 */
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200121 for (sg = sg_head; sg_len < *len; sg++) {
Marcelo Cerri2b7c15c2013-08-02 12:09:51 +0000122 u64 next_page;
123
Kent Yoderae0222b2012-05-14 10:59:38 +0000124 sg->addr = sg_addr;
Marcelo Cerri2b7c15c2013-08-02 12:09:51 +0000125 sg_addr = min_t(u64, NX_PAGE_NUM(sg_addr + NX_PAGE_SIZE),
126 end_addr);
127
128 next_page = (sg->addr & PAGE_MASK) + PAGE_SIZE;
129 sg->len = min_t(u64, sg_addr, next_page) - sg->addr;
Kent Yoderae0222b2012-05-14 10:59:38 +0000130 sg_len += sg->len;
131
Marcelo Cerri2b7c15c2013-08-02 12:09:51 +0000132 if (sg_addr >= next_page &&
133 is_vmalloc_addr(start_addr + sg_len)) {
134 sg_addr = page_to_phys(vmalloc_to_page(
135 start_addr + sg_len));
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200136 end_addr = sg_addr + *len - sg_len;
Marcelo Cerri2b7c15c2013-08-02 12:09:51 +0000137 }
138
Kent Yoderae0222b2012-05-14 10:59:38 +0000139 if ((sg - sg_head) == sgmax) {
140 pr_err("nx: scatter/gather list overflow, pid: %d\n",
141 current->pid);
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200142 sg++;
143 break;
Kent Yoderae0222b2012-05-14 10:59:38 +0000144 }
145 }
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200146 *len = sg_len;
Kent Yoderae0222b2012-05-14 10:59:38 +0000147
148 /* return the moved sg_head pointer */
149 return sg;
150}
151
152/**
153 * nx_walk_and_build - walk a linux scatterlist and build an nx scatterlist
154 *
155 * @nx_dst: pointer to the first nx_sg element to write
156 * @sglen: max number of nx_sg entries we're allowed to write
157 * @sg_src: pointer to the source linux scatterlist to walk
158 * @start: number of bytes to fast-forward past at the beginning of @sg_src
159 * @src_len: number of bytes to walk in @sg_src
160 */
161struct nx_sg *nx_walk_and_build(struct nx_sg *nx_dst,
162 unsigned int sglen,
163 struct scatterlist *sg_src,
164 unsigned int start,
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200165 unsigned int *src_len)
Kent Yoderae0222b2012-05-14 10:59:38 +0000166{
167 struct scatter_walk walk;
168 struct nx_sg *nx_sg = nx_dst;
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200169 unsigned int n, offset = 0, len = *src_len;
Kent Yoderae0222b2012-05-14 10:59:38 +0000170 char *dst;
171
172 /* we need to fast forward through @start bytes first */
173 for (;;) {
174 scatterwalk_start(&walk, sg_src);
175
176 if (start < offset + sg_src->length)
177 break;
178
179 offset += sg_src->length;
Cristian Stoica5be4d4c2015-01-20 10:06:16 +0200180 sg_src = sg_next(sg_src);
Kent Yoderae0222b2012-05-14 10:59:38 +0000181 }
182
183 /* start - offset is the number of bytes to advance in the scatterlist
184 * element we're currently looking at */
185 scatterwalk_advance(&walk, start - offset);
186
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200187 while (len && (nx_sg - nx_dst) < sglen) {
Kent Yoderae0222b2012-05-14 10:59:38 +0000188 n = scatterwalk_clamp(&walk, len);
189 if (!n) {
Cristian Stoica5be4d4c2015-01-20 10:06:16 +0200190 /* In cases where we have scatterlist chain sg_next
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200191 * handles with it properly */
Cristian Stoica5be4d4c2015-01-20 10:06:16 +0200192 scatterwalk_start(&walk, sg_next(walk.sg));
Kent Yoderae0222b2012-05-14 10:59:38 +0000193 n = scatterwalk_clamp(&walk, len);
194 }
195 dst = scatterwalk_map(&walk);
196
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200197 nx_sg = nx_build_sg_list(nx_sg, dst, &n, sglen - (nx_sg - nx_dst));
Kent Yoderae0222b2012-05-14 10:59:38 +0000198 len -= n;
199
200 scatterwalk_unmap(dst);
201 scatterwalk_advance(&walk, n);
202 scatterwalk_done(&walk, SCATTERWALK_FROM_SG, len);
203 }
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200204 /* update to_process */
205 *src_len -= len;
Kent Yoderae0222b2012-05-14 10:59:38 +0000206
207 /* return the moved destination pointer */
208 return nx_sg;
209}
210
211/**
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200212 * trim_sg_list - ensures the bound in sg list.
213 * @sg: sg list head
214 * @end: sg lisg end
215 * @delta: is the amount we need to crop in order to bound the list.
216 *
217 */
Leonidas Da Silva Barbosac3365ce2015-04-23 17:40:30 -0300218static long int trim_sg_list(struct nx_sg *sg,
219 struct nx_sg *end,
220 unsigned int delta,
221 unsigned int *nbytes)
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200222{
Leonidas Da Silva Barbosac3365ce2015-04-23 17:40:30 -0300223 long int oplen;
224 long int data_back;
225 unsigned int is_delta = delta;
226
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200227 while (delta && end > sg) {
228 struct nx_sg *last = end - 1;
229
230 if (last->len > delta) {
231 last->len -= delta;
232 delta = 0;
233 } else {
234 end--;
235 delta -= last->len;
236 }
237 }
Leonidas Da Silva Barbosac3365ce2015-04-23 17:40:30 -0300238
239 /* There are cases where we need to crop list in order to make it
240 * a block size multiple, but we also need to align data. In order to
241 * that we need to calculate how much we need to put back to be
242 * processed
243 */
244 oplen = (sg - end) * sizeof(struct nx_sg);
245 if (is_delta) {
246 data_back = (abs(oplen) / AES_BLOCK_SIZE) * sg->len;
247 data_back = *nbytes - (data_back & ~(AES_BLOCK_SIZE - 1));
248 *nbytes -= data_back;
249 }
250
251 return oplen;
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200252}
253
254/**
255 * nx_sha_build_sg_list - walk and build sg list to sha modes
256 * using right bounds and limits.
257 * @nx_ctx: NX crypto context for the lists we're building
258 * @nx_sg: current sg list in or out list
259 * @op_len: current op_len to be used in order to build a sg list
260 * @nbytes: number or bytes to be processed
261 * @offset: buf offset
262 * @mode: SHA256 or SHA512
263 */
264int nx_sha_build_sg_list(struct nx_crypto_ctx *nx_ctx,
265 struct nx_sg *nx_in_outsg,
266 s64 *op_len,
267 unsigned int *nbytes,
268 u8 *offset,
269 u32 mode)
270{
271 unsigned int delta = 0;
272 unsigned int total = *nbytes;
273 struct nx_sg *nx_insg = nx_in_outsg;
274 unsigned int max_sg_len;
275
276 max_sg_len = min_t(u64, nx_ctx->ap->sglen,
277 nx_driver.of.max_sg_len/sizeof(struct nx_sg));
278 max_sg_len = min_t(u64, max_sg_len,
279 nx_ctx->ap->databytelen/NX_PAGE_SIZE);
280
281 *nbytes = min_t(u64, *nbytes, nx_ctx->ap->databytelen);
282 nx_insg = nx_build_sg_list(nx_insg, offset, nbytes, max_sg_len);
283
284 switch (mode) {
285 case NX_DS_SHA256:
286 if (*nbytes < total)
287 delta = *nbytes - (*nbytes & ~(SHA256_BLOCK_SIZE - 1));
288 break;
289 case NX_DS_SHA512:
290 if (*nbytes < total)
291 delta = *nbytes - (*nbytes & ~(SHA512_BLOCK_SIZE - 1));
292 break;
293 default:
294 return -EINVAL;
295 }
296 *op_len = trim_sg_list(nx_in_outsg, nx_insg, delta);
297
298 return 0;
299}
300
301/**
Kent Yoderae0222b2012-05-14 10:59:38 +0000302 * nx_build_sg_lists - walk the input scatterlists and build arrays of NX
303 * scatterlists based on them.
304 *
305 * @nx_ctx: NX crypto context for the lists we're building
306 * @desc: the block cipher descriptor for the operation
307 * @dst: destination scatterlist
308 * @src: source scatterlist
309 * @nbytes: length of data described in the scatterlists
Marcelo Cerria8fc3912013-08-29 11:36:31 -0300310 * @offset: number of bytes to fast-forward past at the beginning of
311 * scatterlists.
Kent Yoderae0222b2012-05-14 10:59:38 +0000312 * @iv: destination for the iv data, if the algorithm requires it
313 *
314 * This is common code shared by all the AES algorithms. It uses the block
315 * cipher walk routines to traverse input and output scatterlists, building
316 * corresponding NX scatterlists
317 */
318int nx_build_sg_lists(struct nx_crypto_ctx *nx_ctx,
319 struct blkcipher_desc *desc,
320 struct scatterlist *dst,
321 struct scatterlist *src,
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200322 unsigned int *nbytes,
Marcelo Cerria8fc3912013-08-29 11:36:31 -0300323 unsigned int offset,
Kent Yoderae0222b2012-05-14 10:59:38 +0000324 u8 *iv)
325{
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200326 unsigned int delta = 0;
327 unsigned int total = *nbytes;
Kent Yoderae0222b2012-05-14 10:59:38 +0000328 struct nx_sg *nx_insg = nx_ctx->in_sg;
329 struct nx_sg *nx_outsg = nx_ctx->out_sg;
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200330 unsigned int max_sg_len;
331
332 max_sg_len = min_t(u64, nx_ctx->ap->sglen,
333 nx_driver.of.max_sg_len/sizeof(struct nx_sg));
334 max_sg_len = min_t(u64, max_sg_len,
335 nx_ctx->ap->databytelen/NX_PAGE_SIZE);
Kent Yoderae0222b2012-05-14 10:59:38 +0000336
337 if (iv)
Kent Yoder1ad936e2013-04-12 17:13:59 +0000338 memcpy(iv, desc->info, AES_BLOCK_SIZE);
Kent Yoderae0222b2012-05-14 10:59:38 +0000339
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200340 *nbytes = min_t(u64, *nbytes, nx_ctx->ap->databytelen);
341
342 nx_outsg = nx_walk_and_build(nx_outsg, max_sg_len, dst,
343 offset, nbytes);
344 nx_insg = nx_walk_and_build(nx_insg, max_sg_len, src,
345 offset, nbytes);
346
347 if (*nbytes < total)
348 delta = *nbytes - (*nbytes & ~(AES_BLOCK_SIZE - 1));
Kent Yoderae0222b2012-05-14 10:59:38 +0000349
350 /* these lengths should be negative, which will indicate to phyp that
351 * the input and output parameters are scatterlists, not linear
352 * buffers */
Leonidas Da Silva Barbosac3365ce2015-04-23 17:40:30 -0300353 nx_ctx->op.inlen = trim_sg_list(nx_ctx->in_sg, nx_insg, delta, nbytes);
354 nx_ctx->op.outlen = trim_sg_list(nx_ctx->out_sg, nx_outsg, delta, nbytes);
Kent Yoder1ad936e2013-04-12 17:13:59 +0000355
356 return 0;
Kent Yoderae0222b2012-05-14 10:59:38 +0000357}
358
359/**
360 * nx_ctx_init - initialize an nx_ctx's vio_pfo_op struct
361 *
362 * @nx_ctx: the nx context to initialize
363 * @function: the function code for the op
364 */
365void nx_ctx_init(struct nx_crypto_ctx *nx_ctx, unsigned int function)
366{
Marcelo Cerric8491632013-08-12 18:49:37 -0300367 spin_lock_init(&nx_ctx->lock);
Kent Yoderae0222b2012-05-14 10:59:38 +0000368 memset(nx_ctx->kmem, 0, nx_ctx->kmem_len);
369 nx_ctx->csbcpb->csb.valid |= NX_CSB_VALID_BIT;
370
371 nx_ctx->op.flags = function;
Michael Ellerman7187daf2012-07-25 21:19:48 +0000372 nx_ctx->op.csbcpb = __pa(nx_ctx->csbcpb);
373 nx_ctx->op.in = __pa(nx_ctx->in_sg);
374 nx_ctx->op.out = __pa(nx_ctx->out_sg);
Kent Yoderae0222b2012-05-14 10:59:38 +0000375
376 if (nx_ctx->csbcpb_aead) {
377 nx_ctx->csbcpb_aead->csb.valid |= NX_CSB_VALID_BIT;
378
379 nx_ctx->op_aead.flags = function;
Michael Ellerman7187daf2012-07-25 21:19:48 +0000380 nx_ctx->op_aead.csbcpb = __pa(nx_ctx->csbcpb_aead);
381 nx_ctx->op_aead.in = __pa(nx_ctx->in_sg);
382 nx_ctx->op_aead.out = __pa(nx_ctx->out_sg);
Kent Yoderae0222b2012-05-14 10:59:38 +0000383 }
384}
385
386static void nx_of_update_status(struct device *dev,
387 struct property *p,
388 struct nx_of *props)
389{
390 if (!strncmp(p->value, "okay", p->length)) {
391 props->status = NX_WAITING;
392 props->flags |= NX_OF_FLAG_STATUS_SET;
393 } else {
394 dev_info(dev, "%s: status '%s' is not 'okay'\n", __func__,
395 (char *)p->value);
396 }
397}
398
399static void nx_of_update_sglen(struct device *dev,
400 struct property *p,
401 struct nx_of *props)
402{
403 if (p->length != sizeof(props->max_sg_len)) {
404 dev_err(dev, "%s: unexpected format for "
405 "ibm,max-sg-len property\n", __func__);
406 dev_dbg(dev, "%s: ibm,max-sg-len is %d bytes "
407 "long, expected %zd bytes\n", __func__,
408 p->length, sizeof(props->max_sg_len));
409 return;
410 }
411
412 props->max_sg_len = *(u32 *)p->value;
413 props->flags |= NX_OF_FLAG_MAXSGLEN_SET;
414}
415
416static void nx_of_update_msc(struct device *dev,
417 struct property *p,
418 struct nx_of *props)
419{
420 struct msc_triplet *trip;
421 struct max_sync_cop *msc;
422 unsigned int bytes_so_far, i, lenp;
423
424 msc = (struct max_sync_cop *)p->value;
425 lenp = p->length;
426
427 /* You can't tell if the data read in for this property is sane by its
428 * size alone. This is because there are sizes embedded in the data
429 * structure. The best we can do is check lengths as we parse and bail
430 * as soon as a length error is detected. */
431 bytes_so_far = 0;
432
433 while ((bytes_so_far + sizeof(struct max_sync_cop)) <= lenp) {
434 bytes_so_far += sizeof(struct max_sync_cop);
435
436 trip = msc->trip;
437
438 for (i = 0;
439 ((bytes_so_far + sizeof(struct msc_triplet)) <= lenp) &&
440 i < msc->triplets;
441 i++) {
442 if (msc->fc > NX_MAX_FC || msc->mode > NX_MAX_MODE) {
443 dev_err(dev, "unknown function code/mode "
444 "combo: %d/%d (ignored)\n", msc->fc,
445 msc->mode);
446 goto next_loop;
447 }
448
449 switch (trip->keybitlen) {
450 case 128:
451 case 160:
452 props->ap[msc->fc][msc->mode][0].databytelen =
453 trip->databytelen;
454 props->ap[msc->fc][msc->mode][0].sglen =
455 trip->sglen;
456 break;
457 case 192:
458 props->ap[msc->fc][msc->mode][1].databytelen =
459 trip->databytelen;
460 props->ap[msc->fc][msc->mode][1].sglen =
461 trip->sglen;
462 break;
463 case 256:
464 if (msc->fc == NX_FC_AES) {
465 props->ap[msc->fc][msc->mode][2].
466 databytelen = trip->databytelen;
467 props->ap[msc->fc][msc->mode][2].sglen =
468 trip->sglen;
469 } else if (msc->fc == NX_FC_AES_HMAC ||
470 msc->fc == NX_FC_SHA) {
471 props->ap[msc->fc][msc->mode][1].
472 databytelen = trip->databytelen;
473 props->ap[msc->fc][msc->mode][1].sglen =
474 trip->sglen;
475 } else {
476 dev_warn(dev, "unknown function "
477 "code/key bit len combo"
478 ": (%u/256)\n", msc->fc);
479 }
480 break;
481 case 512:
482 props->ap[msc->fc][msc->mode][2].databytelen =
483 trip->databytelen;
484 props->ap[msc->fc][msc->mode][2].sglen =
485 trip->sglen;
486 break;
487 default:
488 dev_warn(dev, "unknown function code/key bit "
489 "len combo: (%u/%u)\n", msc->fc,
490 trip->keybitlen);
491 break;
492 }
493next_loop:
494 bytes_so_far += sizeof(struct msc_triplet);
495 trip++;
496 }
497
498 msc = (struct max_sync_cop *)trip;
499 }
500
501 props->flags |= NX_OF_FLAG_MAXSYNCCOP_SET;
502}
503
504/**
505 * nx_of_init - read openFirmware values from the device tree
506 *
507 * @dev: device handle
508 * @props: pointer to struct to hold the properties values
509 *
510 * Called once at driver probe time, this function will read out the
511 * openFirmware properties we use at runtime. If all the OF properties are
512 * acceptable, when we exit this function props->flags will indicate that
513 * we're ready to register our crypto algorithms.
514 */
515static void nx_of_init(struct device *dev, struct nx_of *props)
516{
517 struct device_node *base_node = dev->of_node;
518 struct property *p;
519
520 p = of_find_property(base_node, "status", NULL);
521 if (!p)
522 dev_info(dev, "%s: property 'status' not found\n", __func__);
523 else
524 nx_of_update_status(dev, p, props);
525
526 p = of_find_property(base_node, "ibm,max-sg-len", NULL);
527 if (!p)
528 dev_info(dev, "%s: property 'ibm,max-sg-len' not found\n",
529 __func__);
530 else
531 nx_of_update_sglen(dev, p, props);
532
533 p = of_find_property(base_node, "ibm,max-sync-cop", NULL);
534 if (!p)
535 dev_info(dev, "%s: property 'ibm,max-sync-cop' not found\n",
536 __func__);
537 else
538 nx_of_update_msc(dev, p, props);
539}
540
541/**
542 * nx_register_algs - register algorithms with the crypto API
543 *
544 * Called from nx_probe()
545 *
546 * If all OF properties are in an acceptable state, the driver flags will
547 * indicate that we're ready and we'll create our debugfs files and register
548 * out crypto algorithms.
549 */
550static int nx_register_algs(void)
551{
552 int rc = -1;
553
554 if (nx_driver.of.flags != NX_OF_FLAG_MASK_READY)
555 goto out;
556
557 memset(&nx_driver.stats, 0, sizeof(struct nx_stats));
558
559 rc = NX_DEBUGFS_INIT(&nx_driver);
560 if (rc)
561 goto out;
562
Kent Yoder1ad936e2013-04-12 17:13:59 +0000563 nx_driver.of.status = NX_OKAY;
564
Kent Yoderae0222b2012-05-14 10:59:38 +0000565 rc = crypto_register_alg(&nx_ecb_aes_alg);
566 if (rc)
567 goto out;
568
569 rc = crypto_register_alg(&nx_cbc_aes_alg);
570 if (rc)
571 goto out_unreg_ecb;
572
573 rc = crypto_register_alg(&nx_ctr_aes_alg);
574 if (rc)
575 goto out_unreg_cbc;
576
577 rc = crypto_register_alg(&nx_ctr3686_aes_alg);
578 if (rc)
579 goto out_unreg_ctr;
580
581 rc = crypto_register_alg(&nx_gcm_aes_alg);
582 if (rc)
583 goto out_unreg_ctr3686;
584
585 rc = crypto_register_alg(&nx_gcm4106_aes_alg);
586 if (rc)
587 goto out_unreg_gcm;
588
589 rc = crypto_register_alg(&nx_ccm_aes_alg);
590 if (rc)
591 goto out_unreg_gcm4106;
592
593 rc = crypto_register_alg(&nx_ccm4309_aes_alg);
594 if (rc)
595 goto out_unreg_ccm;
596
597 rc = crypto_register_shash(&nx_shash_sha256_alg);
598 if (rc)
599 goto out_unreg_ccm4309;
600
601 rc = crypto_register_shash(&nx_shash_sha512_alg);
602 if (rc)
603 goto out_unreg_s256;
604
605 rc = crypto_register_shash(&nx_shash_aes_xcbc_alg);
606 if (rc)
607 goto out_unreg_s512;
608
Kent Yoderae0222b2012-05-14 10:59:38 +0000609 goto out;
610
611out_unreg_s512:
612 crypto_unregister_shash(&nx_shash_sha512_alg);
613out_unreg_s256:
614 crypto_unregister_shash(&nx_shash_sha256_alg);
615out_unreg_ccm4309:
616 crypto_unregister_alg(&nx_ccm4309_aes_alg);
617out_unreg_ccm:
618 crypto_unregister_alg(&nx_ccm_aes_alg);
619out_unreg_gcm4106:
620 crypto_unregister_alg(&nx_gcm4106_aes_alg);
621out_unreg_gcm:
622 crypto_unregister_alg(&nx_gcm_aes_alg);
623out_unreg_ctr3686:
624 crypto_unregister_alg(&nx_ctr3686_aes_alg);
625out_unreg_ctr:
626 crypto_unregister_alg(&nx_ctr_aes_alg);
627out_unreg_cbc:
628 crypto_unregister_alg(&nx_cbc_aes_alg);
629out_unreg_ecb:
630 crypto_unregister_alg(&nx_ecb_aes_alg);
631out:
632 return rc;
633}
634
635/**
636 * nx_crypto_ctx_init - create and initialize a crypto api context
637 *
638 * @nx_ctx: the crypto api context
639 * @fc: function code for the context
640 * @mode: the function code specific mode for this context
641 */
642static int nx_crypto_ctx_init(struct nx_crypto_ctx *nx_ctx, u32 fc, u32 mode)
643{
644 if (nx_driver.of.status != NX_OKAY) {
645 pr_err("Attempt to initialize NX crypto context while device "
646 "is not available!\n");
647 return -ENODEV;
648 }
649
650 /* we need an extra page for csbcpb_aead for these modes */
651 if (mode == NX_MODE_AES_GCM || mode == NX_MODE_AES_CCM)
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200652 nx_ctx->kmem_len = (5 * NX_PAGE_SIZE) +
Kent Yoderae0222b2012-05-14 10:59:38 +0000653 sizeof(struct nx_csbcpb);
654 else
Leonidas S. Barbosaf1294302014-10-28 15:50:45 -0200655 nx_ctx->kmem_len = (4 * NX_PAGE_SIZE) +
Kent Yoderae0222b2012-05-14 10:59:38 +0000656 sizeof(struct nx_csbcpb);
657
658 nx_ctx->kmem = kmalloc(nx_ctx->kmem_len, GFP_KERNEL);
659 if (!nx_ctx->kmem)
660 return -ENOMEM;
661
662 /* the csbcpb and scatterlists must be 4K aligned pages */
663 nx_ctx->csbcpb = (struct nx_csbcpb *)(round_up((u64)nx_ctx->kmem,
664 (u64)NX_PAGE_SIZE));
665 nx_ctx->in_sg = (struct nx_sg *)((u8 *)nx_ctx->csbcpb + NX_PAGE_SIZE);
666 nx_ctx->out_sg = (struct nx_sg *)((u8 *)nx_ctx->in_sg + NX_PAGE_SIZE);
667
668 if (mode == NX_MODE_AES_GCM || mode == NX_MODE_AES_CCM)
669 nx_ctx->csbcpb_aead =
670 (struct nx_csbcpb *)((u8 *)nx_ctx->out_sg +
671 NX_PAGE_SIZE);
672
673 /* give each context a pointer to global stats and their OF
674 * properties */
675 nx_ctx->stats = &nx_driver.stats;
676 memcpy(nx_ctx->props, nx_driver.of.ap[fc][mode],
677 sizeof(struct alg_props) * 3);
678
679 return 0;
680}
681
682/* entry points from the crypto tfm initializers */
683int nx_crypto_ctx_aes_ccm_init(struct crypto_tfm *tfm)
684{
685 return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
686 NX_MODE_AES_CCM);
687}
688
689int nx_crypto_ctx_aes_gcm_init(struct crypto_tfm *tfm)
690{
691 return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
692 NX_MODE_AES_GCM);
693}
694
695int nx_crypto_ctx_aes_ctr_init(struct crypto_tfm *tfm)
696{
697 return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
698 NX_MODE_AES_CTR);
699}
700
701int nx_crypto_ctx_aes_cbc_init(struct crypto_tfm *tfm)
702{
703 return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
704 NX_MODE_AES_CBC);
705}
706
707int nx_crypto_ctx_aes_ecb_init(struct crypto_tfm *tfm)
708{
709 return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
710 NX_MODE_AES_ECB);
711}
712
713int nx_crypto_ctx_sha_init(struct crypto_tfm *tfm)
714{
715 return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_SHA, NX_MODE_SHA);
716}
717
718int nx_crypto_ctx_aes_xcbc_init(struct crypto_tfm *tfm)
719{
720 return nx_crypto_ctx_init(crypto_tfm_ctx(tfm), NX_FC_AES,
721 NX_MODE_AES_XCBC_MAC);
722}
723
724/**
725 * nx_crypto_ctx_exit - destroy a crypto api context
726 *
727 * @tfm: the crypto transform pointer for the context
728 *
729 * As crypto API contexts are destroyed, this exit hook is called to free the
730 * memory associated with it.
731 */
732void nx_crypto_ctx_exit(struct crypto_tfm *tfm)
733{
734 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
735
736 kzfree(nx_ctx->kmem);
737 nx_ctx->csbcpb = NULL;
738 nx_ctx->csbcpb_aead = NULL;
739 nx_ctx->in_sg = NULL;
740 nx_ctx->out_sg = NULL;
741}
742
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -0800743static int nx_probe(struct vio_dev *viodev, const struct vio_device_id *id)
Kent Yoderae0222b2012-05-14 10:59:38 +0000744{
745 dev_dbg(&viodev->dev, "driver probed: %s resource id: 0x%x\n",
746 viodev->name, viodev->resource_id);
747
748 if (nx_driver.viodev) {
749 dev_err(&viodev->dev, "%s: Attempt to register more than one "
750 "instance of the hardware\n", __func__);
751 return -EINVAL;
752 }
753
754 nx_driver.viodev = viodev;
755
756 nx_of_init(&viodev->dev, &nx_driver.of);
757
758 return nx_register_algs();
759}
760
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -0800761static int nx_remove(struct vio_dev *viodev)
Kent Yoderae0222b2012-05-14 10:59:38 +0000762{
763 dev_dbg(&viodev->dev, "entering nx_remove for UA 0x%x\n",
764 viodev->unit_address);
765
766 if (nx_driver.of.status == NX_OKAY) {
767 NX_DEBUGFS_FINI(&nx_driver);
768
769 crypto_unregister_alg(&nx_ccm_aes_alg);
770 crypto_unregister_alg(&nx_ccm4309_aes_alg);
771 crypto_unregister_alg(&nx_gcm_aes_alg);
772 crypto_unregister_alg(&nx_gcm4106_aes_alg);
773 crypto_unregister_alg(&nx_ctr_aes_alg);
774 crypto_unregister_alg(&nx_ctr3686_aes_alg);
775 crypto_unregister_alg(&nx_cbc_aes_alg);
776 crypto_unregister_alg(&nx_ecb_aes_alg);
777 crypto_unregister_shash(&nx_shash_sha256_alg);
778 crypto_unregister_shash(&nx_shash_sha512_alg);
779 crypto_unregister_shash(&nx_shash_aes_xcbc_alg);
780 }
781
782 return 0;
783}
784
785
786/* module wide initialization/cleanup */
787static int __init nx_init(void)
788{
789 return vio_register_driver(&nx_driver.viodriver);
790}
791
792static void __exit nx_fini(void)
793{
794 vio_unregister_driver(&nx_driver.viodriver);
795}
796
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -0800797static struct vio_device_id nx_crypto_driver_ids[] = {
Kent Yoderae0222b2012-05-14 10:59:38 +0000798 { "ibm,sym-encryption-v1", "ibm,sym-encryption" },
799 { "", "" }
800};
801MODULE_DEVICE_TABLE(vio, nx_crypto_driver_ids);
802
803/* driver state structure */
804struct nx_crypto_driver nx_driver = {
805 .viodriver = {
806 .id_table = nx_crypto_driver_ids,
807 .probe = nx_probe,
808 .remove = nx_remove,
809 .name = NX_NAME,
810 },
811};
812
813module_init(nx_init);
814module_exit(nx_fini);
815
816MODULE_AUTHOR("Kent Yoder <yoder1@us.ibm.com>");
817MODULE_DESCRIPTION(NX_STRING);
818MODULE_LICENSE("GPL");
819MODULE_VERSION(NX_VERSION);