blob: 4472e2001e794e169dc75a31b528496db963ad82 [file] [log] [blame]
Dan Streetmaned70b472015-05-07 13:49:21 -04001/*
2 * Cryptographic API for the NX-842 hardware compression.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * Copyright (C) IBM Corporation, 2011-2015
15 *
Dan Streetmand31581a2015-07-22 14:26:36 -040016 * Designer of the Power data compression engine:
17 * Bulent Abali <abali@us.ibm.com>
18 *
Dan Streetmaned70b472015-05-07 13:49:21 -040019 * Original Authors: Robert Jennings <rcj@linux.vnet.ibm.com>
20 * Seth Jennings <sjenning@linux.vnet.ibm.com>
21 *
22 * Rewrite: Dan Streetman <ddstreet@ieee.org>
23 *
24 * This is an interface to the NX-842 compression hardware in PowerPC
25 * processors. Most of the complexity of this drvier is due to the fact that
26 * the NX-842 compression hardware requires the input and output data buffers
27 * to be specifically aligned, to be a specific multiple in length, and within
28 * specific minimum and maximum lengths. Those restrictions, provided by the
29 * nx-842 driver via nx842_constraints, mean this driver must use bounce
30 * buffers and headers to correct misaligned in or out buffers, and to split
31 * input buffers that are too large.
32 *
33 * This driver will fall back to software decompression if the hardware
34 * decompression fails, so this driver's decompression should never fail as
35 * long as the provided compressed buffer is valid. Any compressed buffer
36 * created by this driver will have a header (except ones where the input
37 * perfectly matches the constraints); so users of this driver cannot simply
38 * pass a compressed buffer created by this driver over to the 842 software
39 * decompression library. Instead, users must use this driver to decompress;
40 * if the hardware fails or is unavailable, the compressed buffer will be
41 * parsed and the header removed, and the raw 842 buffer(s) passed to the 842
42 * software decompression library.
43 *
44 * This does not fall back to software compression, however, since the caller
45 * of this function is specifically requesting hardware compression; if the
46 * hardware compression fails, the caller can fall back to software
47 * compression, and the raw 842 compressed buffer that the software compressor
48 * creates can be passed to this driver for hardware decompression; any
49 * buffer without our specific header magic is assumed to be a raw 842 buffer
50 * and passed directly to the hardware. Note that the software compression
51 * library will produce a compressed buffer that is incompatible with the
52 * hardware decompressor if the original input buffer length is not a multiple
53 * of 8; if such a compressed buffer is passed to this driver for
54 * decompression, the hardware will reject it and this driver will then pass
55 * it over to the software library for decompression.
56 */
57
58#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
59
60#include <linux/init.h>
61#include <linux/module.h>
62#include <linux/crypto.h>
63#include <linux/vmalloc.h>
Dan Streetmaned70b472015-05-07 13:49:21 -040064#include <linux/sw842.h>
65#include <linux/ratelimit.h>
Herbert Xu23ad69a2015-07-08 21:40:39 +080066#include <linux/spinlock.h>
Dan Streetmaned70b472015-05-07 13:49:21 -040067
Dan Streetman32be6d32015-06-12 10:58:46 -040068#include "nx-842.h"
69
Dan Streetmaned70b472015-05-07 13:49:21 -040070/* The first 5 bits of this magic are 0x1f, which is an invalid 842 5-bit
71 * template (see lib/842/842.h), so this magic number will never appear at
72 * the start of a raw 842 compressed buffer. That is important, as any buffer
73 * passed to us without this magic is assumed to be a raw 842 compressed
74 * buffer, and passed directly to the hardware to decompress.
75 */
76#define NX842_CRYPTO_MAGIC (0xf842)
77#define NX842_CRYPTO_GROUP_MAX (0x20)
78#define NX842_CRYPTO_HEADER_SIZE(g) \
79 (sizeof(struct nx842_crypto_header) + \
80 sizeof(struct nx842_crypto_header_group) * (g))
81#define NX842_CRYPTO_HEADER_MAX_SIZE \
82 NX842_CRYPTO_HEADER_SIZE(NX842_CRYPTO_GROUP_MAX)
83
84/* bounce buffer size */
85#define BOUNCE_BUFFER_ORDER (2)
86#define BOUNCE_BUFFER_SIZE \
87 ((unsigned int)(PAGE_SIZE << BOUNCE_BUFFER_ORDER))
88
89/* try longer on comp because we can fallback to sw decomp if hw is busy */
90#define COMP_BUSY_TIMEOUT (250) /* ms */
91#define DECOMP_BUSY_TIMEOUT (50) /* ms */
92
93struct nx842_crypto_header_group {
94 __be16 padding; /* unused bytes at start of group */
95 __be32 compressed_length; /* compressed bytes in group */
96 __be32 uncompressed_length; /* bytes after decompression */
97} __packed;
98
99struct nx842_crypto_header {
100 __be16 magic; /* NX842_CRYPTO_MAGIC */
101 __be16 ignore; /* decompressed end bytes to ignore */
102 u8 groups; /* total groups in this header */
103 struct nx842_crypto_header_group group[];
104} __packed;
105
106struct nx842_crypto_param {
107 u8 *in;
108 unsigned int iremain;
109 u8 *out;
110 unsigned int oremain;
111 unsigned int ototal;
112};
113
114static int update_param(struct nx842_crypto_param *p,
115 unsigned int slen, unsigned int dlen)
116{
117 if (p->iremain < slen)
118 return -EOVERFLOW;
119 if (p->oremain < dlen)
120 return -ENOSPC;
121
122 p->in += slen;
123 p->iremain -= slen;
124 p->out += dlen;
125 p->oremain -= dlen;
126 p->ototal += dlen;
127
128 return 0;
129}
130
131struct nx842_crypto_ctx {
Herbert Xu23ad69a2015-07-08 21:40:39 +0800132 spinlock_t lock;
133
Dan Streetmaned70b472015-05-07 13:49:21 -0400134 u8 *wmem;
135 u8 *sbounce, *dbounce;
136
137 struct nx842_crypto_header header;
138 struct nx842_crypto_header_group group[NX842_CRYPTO_GROUP_MAX];
139};
140
141static int nx842_crypto_init(struct crypto_tfm *tfm)
142{
143 struct nx842_crypto_ctx *ctx = crypto_tfm_ctx(tfm);
144
Herbert Xu23ad69a2015-07-08 21:40:39 +0800145 spin_lock_init(&ctx->lock);
Dan Streetman2c6f6ea2015-06-12 10:58:47 -0400146 ctx->wmem = kmalloc(nx842_workmem_size(), GFP_KERNEL);
Dan Streetmaned70b472015-05-07 13:49:21 -0400147 ctx->sbounce = (u8 *)__get_free_pages(GFP_KERNEL, BOUNCE_BUFFER_ORDER);
148 ctx->dbounce = (u8 *)__get_free_pages(GFP_KERNEL, BOUNCE_BUFFER_ORDER);
149 if (!ctx->wmem || !ctx->sbounce || !ctx->dbounce) {
150 kfree(ctx->wmem);
151 free_page((unsigned long)ctx->sbounce);
152 free_page((unsigned long)ctx->dbounce);
153 return -ENOMEM;
154 }
155
156 return 0;
157}
158
159static void nx842_crypto_exit(struct crypto_tfm *tfm)
160{
161 struct nx842_crypto_ctx *ctx = crypto_tfm_ctx(tfm);
162
163 kfree(ctx->wmem);
164 free_page((unsigned long)ctx->sbounce);
165 free_page((unsigned long)ctx->dbounce);
166}
167
Dan Streetmand31581a2015-07-22 14:26:36 -0400168static void check_constraints(struct nx842_constraints *c)
Dan Streetmaned70b472015-05-07 13:49:21 -0400169{
Dan Streetmaned70b472015-05-07 13:49:21 -0400170 /* limit maximum, to always have enough bounce buffer to decompress */
Dan Streetmand31581a2015-07-22 14:26:36 -0400171 if (c->maximum > BOUNCE_BUFFER_SIZE)
Dan Streetmaned70b472015-05-07 13:49:21 -0400172 c->maximum = BOUNCE_BUFFER_SIZE;
Dan Streetmaned70b472015-05-07 13:49:21 -0400173}
174
175static int nx842_crypto_add_header(struct nx842_crypto_header *hdr, u8 *buf)
176{
177 int s = NX842_CRYPTO_HEADER_SIZE(hdr->groups);
178
179 /* compress should have added space for header */
180 if (s > be16_to_cpu(hdr->group[0].padding)) {
181 pr_err("Internal error: no space for header\n");
182 return -EINVAL;
183 }
184
185 memcpy(buf, hdr, s);
186
187 print_hex_dump_debug("header ", DUMP_PREFIX_OFFSET, 16, 1, buf, s, 0);
188
189 return 0;
190}
191
192static int compress(struct nx842_crypto_ctx *ctx,
193 struct nx842_crypto_param *p,
194 struct nx842_crypto_header_group *g,
195 struct nx842_constraints *c,
196 u16 *ignore,
197 unsigned int hdrsize)
198{
199 unsigned int slen = p->iremain, dlen = p->oremain, tmplen;
200 unsigned int adj_slen = slen;
201 u8 *src = p->in, *dst = p->out;
202 int ret, dskip = 0;
203 ktime_t timeout;
204
205 if (p->iremain == 0)
206 return -EOVERFLOW;
207
208 if (p->oremain == 0 || hdrsize + c->minimum > dlen)
209 return -ENOSPC;
210
211 if (slen % c->multiple)
212 adj_slen = round_up(slen, c->multiple);
213 if (slen < c->minimum)
214 adj_slen = c->minimum;
215 if (slen > c->maximum)
216 adj_slen = slen = c->maximum;
217 if (adj_slen > slen || (u64)src % c->alignment) {
218 adj_slen = min(adj_slen, BOUNCE_BUFFER_SIZE);
219 slen = min(slen, BOUNCE_BUFFER_SIZE);
220 if (adj_slen > slen)
221 memset(ctx->sbounce + slen, 0, adj_slen - slen);
222 memcpy(ctx->sbounce, src, slen);
223 src = ctx->sbounce;
224 slen = adj_slen;
225 pr_debug("using comp sbounce buffer, len %x\n", slen);
226 }
227
228 dst += hdrsize;
229 dlen -= hdrsize;
230
231 if ((u64)dst % c->alignment) {
232 dskip = (int)(PTR_ALIGN(dst, c->alignment) - dst);
233 dst += dskip;
234 dlen -= dskip;
235 }
236 if (dlen % c->multiple)
237 dlen = round_down(dlen, c->multiple);
238 if (dlen < c->minimum) {
239nospc:
240 dst = ctx->dbounce;
241 dlen = min(p->oremain, BOUNCE_BUFFER_SIZE);
242 dlen = round_down(dlen, c->multiple);
243 dskip = 0;
244 pr_debug("using comp dbounce buffer, len %x\n", dlen);
245 }
246 if (dlen > c->maximum)
247 dlen = c->maximum;
248
249 tmplen = dlen;
250 timeout = ktime_add_ms(ktime_get(), COMP_BUSY_TIMEOUT);
251 do {
252 dlen = tmplen; /* reset dlen, if we're retrying */
Dan Streetmand31581a2015-07-22 14:26:36 -0400253 ret = nx842_platform_driver()->compress(src, slen,
254 dst, &dlen,
255 ctx->wmem);
Dan Streetmaned70b472015-05-07 13:49:21 -0400256 /* possibly we should reduce the slen here, instead of
257 * retrying with the dbounce buffer?
258 */
259 if (ret == -ENOSPC && dst != ctx->dbounce)
260 goto nospc;
261 } while (ret == -EBUSY && ktime_before(ktime_get(), timeout));
262 if (ret)
263 return ret;
264
265 dskip += hdrsize;
266
267 if (dst == ctx->dbounce)
268 memcpy(p->out + dskip, dst, dlen);
269
270 g->padding = cpu_to_be16(dskip);
271 g->compressed_length = cpu_to_be32(dlen);
272 g->uncompressed_length = cpu_to_be32(slen);
273
274 if (p->iremain < slen) {
275 *ignore = slen - p->iremain;
276 slen = p->iremain;
277 }
278
279 pr_debug("compress slen %x ignore %x dlen %x padding %x\n",
280 slen, *ignore, dlen, dskip);
281
282 return update_param(p, slen, dskip + dlen);
283}
284
285static int nx842_crypto_compress(struct crypto_tfm *tfm,
286 const u8 *src, unsigned int slen,
287 u8 *dst, unsigned int *dlen)
288{
289 struct nx842_crypto_ctx *ctx = crypto_tfm_ctx(tfm);
290 struct nx842_crypto_header *hdr = &ctx->header;
291 struct nx842_crypto_param p;
Dan Streetmand31581a2015-07-22 14:26:36 -0400292 struct nx842_constraints c = *nx842_platform_driver()->constraints;
Dan Streetmaned70b472015-05-07 13:49:21 -0400293 unsigned int groups, hdrsize, h;
294 int ret, n;
295 bool add_header;
296 u16 ignore = 0;
297
Dan Streetmand31581a2015-07-22 14:26:36 -0400298 check_constraints(&c);
299
Dan Streetmaned70b472015-05-07 13:49:21 -0400300 p.in = (u8 *)src;
301 p.iremain = slen;
302 p.out = dst;
303 p.oremain = *dlen;
304 p.ototal = 0;
305
306 *dlen = 0;
307
Dan Streetmaned70b472015-05-07 13:49:21 -0400308 groups = min_t(unsigned int, NX842_CRYPTO_GROUP_MAX,
309 DIV_ROUND_UP(p.iremain, c.maximum));
310 hdrsize = NX842_CRYPTO_HEADER_SIZE(groups);
311
Herbert Xu23ad69a2015-07-08 21:40:39 +0800312 spin_lock_bh(&ctx->lock);
313
Dan Streetmaned70b472015-05-07 13:49:21 -0400314 /* skip adding header if the buffers meet all constraints */
315 add_header = (p.iremain % c.multiple ||
316 p.iremain < c.minimum ||
317 p.iremain > c.maximum ||
318 (u64)p.in % c.alignment ||
319 p.oremain % c.multiple ||
320 p.oremain < c.minimum ||
321 p.oremain > c.maximum ||
322 (u64)p.out % c.alignment);
323
324 hdr->magic = cpu_to_be16(NX842_CRYPTO_MAGIC);
325 hdr->groups = 0;
326 hdr->ignore = 0;
327
328 while (p.iremain > 0) {
329 n = hdr->groups++;
Herbert Xu23ad69a2015-07-08 21:40:39 +0800330 ret = -ENOSPC;
Dan Streetmaned70b472015-05-07 13:49:21 -0400331 if (hdr->groups > NX842_CRYPTO_GROUP_MAX)
Herbert Xu23ad69a2015-07-08 21:40:39 +0800332 goto unlock;
Dan Streetmaned70b472015-05-07 13:49:21 -0400333
334 /* header goes before first group */
335 h = !n && add_header ? hdrsize : 0;
336
337 if (ignore)
338 pr_warn("interal error, ignore is set %x\n", ignore);
339
340 ret = compress(ctx, &p, &hdr->group[n], &c, &ignore, h);
341 if (ret)
Herbert Xu23ad69a2015-07-08 21:40:39 +0800342 goto unlock;
Dan Streetmaned70b472015-05-07 13:49:21 -0400343 }
344
345 if (!add_header && hdr->groups > 1) {
346 pr_err("Internal error: No header but multiple groups\n");
Herbert Xu23ad69a2015-07-08 21:40:39 +0800347 ret = -EINVAL;
348 goto unlock;
Dan Streetmaned70b472015-05-07 13:49:21 -0400349 }
350
351 /* ignore indicates the input stream needed to be padded */
352 hdr->ignore = cpu_to_be16(ignore);
353 if (ignore)
354 pr_debug("marked %d bytes as ignore\n", ignore);
355
356 if (add_header)
357 ret = nx842_crypto_add_header(hdr, dst);
358 if (ret)
Herbert Xu23ad69a2015-07-08 21:40:39 +0800359 goto unlock;
Dan Streetmaned70b472015-05-07 13:49:21 -0400360
361 *dlen = p.ototal;
362
363 pr_debug("compress total slen %x dlen %x\n", slen, *dlen);
364
Herbert Xu23ad69a2015-07-08 21:40:39 +0800365unlock:
366 spin_unlock_bh(&ctx->lock);
367 return ret;
Dan Streetmaned70b472015-05-07 13:49:21 -0400368}
369
370static int decompress(struct nx842_crypto_ctx *ctx,
371 struct nx842_crypto_param *p,
372 struct nx842_crypto_header_group *g,
373 struct nx842_constraints *c,
Dan Streetmand31581a2015-07-22 14:26:36 -0400374 u16 ignore)
Dan Streetmaned70b472015-05-07 13:49:21 -0400375{
376 unsigned int slen = be32_to_cpu(g->compressed_length);
377 unsigned int required_len = be32_to_cpu(g->uncompressed_length);
378 unsigned int dlen = p->oremain, tmplen;
379 unsigned int adj_slen = slen;
380 u8 *src = p->in, *dst = p->out;
381 u16 padding = be16_to_cpu(g->padding);
382 int ret, spadding = 0, dpadding = 0;
383 ktime_t timeout;
384
385 if (!slen || !required_len)
386 return -EINVAL;
387
388 if (p->iremain <= 0 || padding + slen > p->iremain)
389 return -EOVERFLOW;
390
391 if (p->oremain <= 0 || required_len - ignore > p->oremain)
392 return -ENOSPC;
393
394 src += padding;
395
Dan Streetmaned70b472015-05-07 13:49:21 -0400396 if (slen % c->multiple)
397 adj_slen = round_up(slen, c->multiple);
398 if (slen < c->minimum)
399 adj_slen = c->minimum;
400 if (slen > c->maximum)
401 goto usesw;
402 if (slen < adj_slen || (u64)src % c->alignment) {
403 /* we can append padding bytes because the 842 format defines
404 * an "end" template (see lib/842/842_decompress.c) and will
405 * ignore any bytes following it.
406 */
407 if (slen < adj_slen)
408 memset(ctx->sbounce + slen, 0, adj_slen - slen);
409 memcpy(ctx->sbounce, src, slen);
410 src = ctx->sbounce;
411 spadding = adj_slen - slen;
412 slen = adj_slen;
413 pr_debug("using decomp sbounce buffer, len %x\n", slen);
414 }
415
416 if (dlen % c->multiple)
417 dlen = round_down(dlen, c->multiple);
418 if (dlen < required_len || (u64)dst % c->alignment) {
419 dst = ctx->dbounce;
420 dlen = min(required_len, BOUNCE_BUFFER_SIZE);
421 pr_debug("using decomp dbounce buffer, len %x\n", dlen);
422 }
423 if (dlen < c->minimum)
424 goto usesw;
425 if (dlen > c->maximum)
426 dlen = c->maximum;
427
428 tmplen = dlen;
429 timeout = ktime_add_ms(ktime_get(), DECOMP_BUSY_TIMEOUT);
430 do {
431 dlen = tmplen; /* reset dlen, if we're retrying */
Dan Streetmand31581a2015-07-22 14:26:36 -0400432 ret = nx842_platform_driver()->decompress(src, slen,
433 dst, &dlen,
434 ctx->wmem);
Dan Streetmaned70b472015-05-07 13:49:21 -0400435 } while (ret == -EBUSY && ktime_before(ktime_get(), timeout));
436 if (ret) {
437usesw:
438 /* reset everything, sw doesn't have constraints */
439 src = p->in + padding;
440 slen = be32_to_cpu(g->compressed_length);
441 spadding = 0;
442 dst = p->out;
443 dlen = p->oremain;
444 dpadding = 0;
445 if (dlen < required_len) { /* have ignore bytes */
446 dst = ctx->dbounce;
447 dlen = BOUNCE_BUFFER_SIZE;
448 }
449 pr_info_ratelimited("using software 842 decompression\n");
450 ret = sw842_decompress(src, slen, dst, &dlen);
451 }
452 if (ret)
453 return ret;
454
455 slen -= spadding;
456
457 dlen -= ignore;
458 if (ignore)
459 pr_debug("ignoring last %x bytes\n", ignore);
460
461 if (dst == ctx->dbounce)
462 memcpy(p->out, dst, dlen);
463
464 pr_debug("decompress slen %x padding %x dlen %x ignore %x\n",
465 slen, padding, dlen, ignore);
466
467 return update_param(p, slen + padding, dlen);
468}
469
470static int nx842_crypto_decompress(struct crypto_tfm *tfm,
471 const u8 *src, unsigned int slen,
472 u8 *dst, unsigned int *dlen)
473{
474 struct nx842_crypto_ctx *ctx = crypto_tfm_ctx(tfm);
475 struct nx842_crypto_header *hdr;
476 struct nx842_crypto_param p;
Dan Streetmand31581a2015-07-22 14:26:36 -0400477 struct nx842_constraints c = *nx842_platform_driver()->constraints;
Dan Streetmaned70b472015-05-07 13:49:21 -0400478 int n, ret, hdr_len;
479 u16 ignore = 0;
Dan Streetmand31581a2015-07-22 14:26:36 -0400480
481 check_constraints(&c);
Dan Streetmaned70b472015-05-07 13:49:21 -0400482
Dan Streetmaned70b472015-05-07 13:49:21 -0400483 p.in = (u8 *)src;
484 p.iremain = slen;
485 p.out = dst;
486 p.oremain = *dlen;
487 p.ototal = 0;
488
489 *dlen = 0;
490
Dan Streetmaned70b472015-05-07 13:49:21 -0400491 hdr = (struct nx842_crypto_header *)src;
492
Herbert Xu23ad69a2015-07-08 21:40:39 +0800493 spin_lock_bh(&ctx->lock);
494
Dan Streetmaned70b472015-05-07 13:49:21 -0400495 /* If it doesn't start with our header magic number, assume it's a raw
496 * 842 compressed buffer and pass it directly to the hardware driver
497 */
498 if (be16_to_cpu(hdr->magic) != NX842_CRYPTO_MAGIC) {
499 struct nx842_crypto_header_group g = {
500 .padding = 0,
501 .compressed_length = cpu_to_be32(p.iremain),
502 .uncompressed_length = cpu_to_be32(p.oremain),
503 };
504
Dan Streetmand31581a2015-07-22 14:26:36 -0400505 ret = decompress(ctx, &p, &g, &c, 0);
Dan Streetmaned70b472015-05-07 13:49:21 -0400506 if (ret)
Herbert Xu23ad69a2015-07-08 21:40:39 +0800507 goto unlock;
Dan Streetmaned70b472015-05-07 13:49:21 -0400508
Dan Streetman20fc3112015-07-22 14:26:35 -0400509 goto success;
Dan Streetmaned70b472015-05-07 13:49:21 -0400510 }
511
512 if (!hdr->groups) {
513 pr_err("header has no groups\n");
Herbert Xu23ad69a2015-07-08 21:40:39 +0800514 ret = -EINVAL;
515 goto unlock;
Dan Streetmaned70b472015-05-07 13:49:21 -0400516 }
517 if (hdr->groups > NX842_CRYPTO_GROUP_MAX) {
518 pr_err("header has too many groups %x, max %x\n",
519 hdr->groups, NX842_CRYPTO_GROUP_MAX);
Herbert Xu23ad69a2015-07-08 21:40:39 +0800520 ret = -EINVAL;
521 goto unlock;
Dan Streetmaned70b472015-05-07 13:49:21 -0400522 }
523
524 hdr_len = NX842_CRYPTO_HEADER_SIZE(hdr->groups);
Herbert Xu23ad69a2015-07-08 21:40:39 +0800525 if (hdr_len > slen) {
526 ret = -EOVERFLOW;
527 goto unlock;
528 }
Dan Streetmaned70b472015-05-07 13:49:21 -0400529
530 memcpy(&ctx->header, src, hdr_len);
531 hdr = &ctx->header;
532
533 for (n = 0; n < hdr->groups; n++) {
534 /* ignore applies to last group */
535 if (n + 1 == hdr->groups)
536 ignore = be16_to_cpu(hdr->ignore);
537
Dan Streetmand31581a2015-07-22 14:26:36 -0400538 ret = decompress(ctx, &p, &hdr->group[n], &c, ignore);
Dan Streetmaned70b472015-05-07 13:49:21 -0400539 if (ret)
Herbert Xu23ad69a2015-07-08 21:40:39 +0800540 goto unlock;
Dan Streetmaned70b472015-05-07 13:49:21 -0400541 }
542
Dan Streetman20fc3112015-07-22 14:26:35 -0400543success:
Dan Streetmaned70b472015-05-07 13:49:21 -0400544 *dlen = p.ototal;
545
546 pr_debug("decompress total slen %x dlen %x\n", slen, *dlen);
547
Herbert Xu23ad69a2015-07-08 21:40:39 +0800548 ret = 0;
549
550unlock:
551 spin_unlock_bh(&ctx->lock);
552
553 return ret;
Dan Streetmaned70b472015-05-07 13:49:21 -0400554}
555
556static struct crypto_alg alg = {
557 .cra_name = "842",
558 .cra_driver_name = "842-nx",
559 .cra_priority = 300,
560 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
561 .cra_ctxsize = sizeof(struct nx842_crypto_ctx),
562 .cra_module = THIS_MODULE,
563 .cra_init = nx842_crypto_init,
564 .cra_exit = nx842_crypto_exit,
565 .cra_u = { .compress = {
566 .coa_compress = nx842_crypto_compress,
567 .coa_decompress = nx842_crypto_decompress } }
568};
569
570static int __init nx842_crypto_mod_init(void)
571{
Dan Streetmand31581a2015-07-22 14:26:36 -0400572 request_module("nx-compress-powernv");
573 request_module("nx-compress-pseries");
574
575 /* we prevent loading/registering if there's no platform driver,
576 * and we get the platform module that set it so it won't unload,
577 * so we don't need to check if it's set in any of our functions
578 */
579 if (!nx842_platform_driver_get()) {
580 pr_err("no nx842 platform driver found.\n");
581 return -ENODEV;
582 }
583
Dan Streetmaned70b472015-05-07 13:49:21 -0400584 return crypto_register_alg(&alg);
585}
586module_init(nx842_crypto_mod_init);
587
588static void __exit nx842_crypto_mod_exit(void)
589{
590 crypto_unregister_alg(&alg);
Dan Streetmand31581a2015-07-22 14:26:36 -0400591
592 nx842_platform_driver_put();
Dan Streetmaned70b472015-05-07 13:49:21 -0400593}
594module_exit(nx842_crypto_mod_exit);
595
596MODULE_LICENSE("GPL");
Dan Streetmand31581a2015-07-22 14:26:36 -0400597MODULE_DESCRIPTION("IBM PowerPC Nest (NX) 842 Hardware Compression Driver");
Dan Streetmaned70b472015-05-07 13:49:21 -0400598MODULE_ALIAS_CRYPTO("842");
599MODULE_ALIAS_CRYPTO("842-nx");
600MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");