2 * Cryptographic API for the NX-842 hardware compression.
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.
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.
14 * Copyright (C) IBM Corporation, 2011-2015
16 * Original Authors: Robert Jennings <rcj@linux.vnet.ibm.com>
17 * Seth Jennings <sjenning@linux.vnet.ibm.com>
19 * Rewrite: Dan Streetman <ddstreet@ieee.org>
21 * This is an interface to the NX-842 compression hardware in PowerPC
22 * processors. Most of the complexity of this drvier is due to the fact that
23 * the NX-842 compression hardware requires the input and output data buffers
24 * to be specifically aligned, to be a specific multiple in length, and within
25 * specific minimum and maximum lengths. Those restrictions, provided by the
26 * nx-842 driver via nx842_constraints, mean this driver must use bounce
27 * buffers and headers to correct misaligned in or out buffers, and to split
28 * input buffers that are too large.
30 * This driver will fall back to software decompression if the hardware
31 * decompression fails, so this driver's decompression should never fail as
32 * long as the provided compressed buffer is valid. Any compressed buffer
33 * created by this driver will have a header (except ones where the input
34 * perfectly matches the constraints); so users of this driver cannot simply
35 * pass a compressed buffer created by this driver over to the 842 software
36 * decompression library. Instead, users must use this driver to decompress;
37 * if the hardware fails or is unavailable, the compressed buffer will be
38 * parsed and the header removed, and the raw 842 buffer(s) passed to the 842
39 * software decompression library.
41 * This does not fall back to software compression, however, since the caller
42 * of this function is specifically requesting hardware compression; if the
43 * hardware compression fails, the caller can fall back to software
44 * compression, and the raw 842 compressed buffer that the software compressor
45 * creates can be passed to this driver for hardware decompression; any
46 * buffer without our specific header magic is assumed to be a raw 842 buffer
47 * and passed directly to the hardware. Note that the software compression
48 * library will produce a compressed buffer that is incompatible with the
49 * hardware decompressor if the original input buffer length is not a multiple
50 * of 8; if such a compressed buffer is passed to this driver for
51 * decompression, the hardware will reject it and this driver will then pass
52 * it over to the software library for decompression.
55 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
57 #include <linux/init.h>
58 #include <linux/module.h>
59 #include <linux/crypto.h>
60 #include <linux/vmalloc.h>
61 #include <linux/sw842.h>
62 #include <linux/ratelimit.h>
66 /* The first 5 bits of this magic are 0x1f, which is an invalid 842 5-bit
67 * template (see lib/842/842.h), so this magic number will never appear at
68 * the start of a raw 842 compressed buffer. That is important, as any buffer
69 * passed to us without this magic is assumed to be a raw 842 compressed
70 * buffer, and passed directly to the hardware to decompress.
72 #define NX842_CRYPTO_MAGIC (0xf842)
73 #define NX842_CRYPTO_GROUP_MAX (0x20)
74 #define NX842_CRYPTO_HEADER_SIZE(g) \
75 (sizeof(struct nx842_crypto_header) + \
76 sizeof(struct nx842_crypto_header_group) * (g))
77 #define NX842_CRYPTO_HEADER_MAX_SIZE \
78 NX842_CRYPTO_HEADER_SIZE(NX842_CRYPTO_GROUP_MAX)
80 /* bounce buffer size */
81 #define BOUNCE_BUFFER_ORDER (2)
82 #define BOUNCE_BUFFER_SIZE \
83 ((unsigned int)(PAGE_SIZE << BOUNCE_BUFFER_ORDER))
85 /* try longer on comp because we can fallback to sw decomp if hw is busy */
86 #define COMP_BUSY_TIMEOUT (250) /* ms */
87 #define DECOMP_BUSY_TIMEOUT (50) /* ms */
89 struct nx842_crypto_header_group
{
90 __be16 padding
; /* unused bytes at start of group */
91 __be32 compressed_length
; /* compressed bytes in group */
92 __be32 uncompressed_length
; /* bytes after decompression */
95 struct nx842_crypto_header
{
96 __be16 magic
; /* NX842_CRYPTO_MAGIC */
97 __be16 ignore
; /* decompressed end bytes to ignore */
98 u8 groups
; /* total groups in this header */
99 struct nx842_crypto_header_group group
[];
102 struct nx842_crypto_param
{
104 unsigned int iremain
;
106 unsigned int oremain
;
110 static int update_param(struct nx842_crypto_param
*p
,
111 unsigned int slen
, unsigned int dlen
)
113 if (p
->iremain
< slen
)
115 if (p
->oremain
< dlen
)
127 struct nx842_crypto_ctx
{
129 u8
*sbounce
, *dbounce
;
131 struct nx842_crypto_header header
;
132 struct nx842_crypto_header_group group
[NX842_CRYPTO_GROUP_MAX
];
135 static int nx842_crypto_init(struct crypto_tfm
*tfm
)
137 struct nx842_crypto_ctx
*ctx
= crypto_tfm_ctx(tfm
);
139 ctx
->wmem
= kmalloc(nx842_workmem_size(), GFP_KERNEL
);
140 ctx
->sbounce
= (u8
*)__get_free_pages(GFP_KERNEL
, BOUNCE_BUFFER_ORDER
);
141 ctx
->dbounce
= (u8
*)__get_free_pages(GFP_KERNEL
, BOUNCE_BUFFER_ORDER
);
142 if (!ctx
->wmem
|| !ctx
->sbounce
|| !ctx
->dbounce
) {
144 free_page((unsigned long)ctx
->sbounce
);
145 free_page((unsigned long)ctx
->dbounce
);
152 static void nx842_crypto_exit(struct crypto_tfm
*tfm
)
154 struct nx842_crypto_ctx
*ctx
= crypto_tfm_ctx(tfm
);
157 free_page((unsigned long)ctx
->sbounce
);
158 free_page((unsigned long)ctx
->dbounce
);
161 static int read_constraints(struct nx842_constraints
*c
)
165 ret
= nx842_constraints(c
);
167 pr_err_ratelimited("could not get nx842 constraints : %d\n",
172 /* limit maximum, to always have enough bounce buffer to decompress */
173 if (c
->maximum
> BOUNCE_BUFFER_SIZE
) {
174 c
->maximum
= BOUNCE_BUFFER_SIZE
;
175 pr_info_once("limiting nx842 maximum to %x\n", c
->maximum
);
181 static int nx842_crypto_add_header(struct nx842_crypto_header
*hdr
, u8
*buf
)
183 int s
= NX842_CRYPTO_HEADER_SIZE(hdr
->groups
);
185 /* compress should have added space for header */
186 if (s
> be16_to_cpu(hdr
->group
[0].padding
)) {
187 pr_err("Internal error: no space for header\n");
193 print_hex_dump_debug("header ", DUMP_PREFIX_OFFSET
, 16, 1, buf
, s
, 0);
198 static int compress(struct nx842_crypto_ctx
*ctx
,
199 struct nx842_crypto_param
*p
,
200 struct nx842_crypto_header_group
*g
,
201 struct nx842_constraints
*c
,
203 unsigned int hdrsize
)
205 unsigned int slen
= p
->iremain
, dlen
= p
->oremain
, tmplen
;
206 unsigned int adj_slen
= slen
;
207 u8
*src
= p
->in
, *dst
= p
->out
;
214 if (p
->oremain
== 0 || hdrsize
+ c
->minimum
> dlen
)
217 if (slen
% c
->multiple
)
218 adj_slen
= round_up(slen
, c
->multiple
);
219 if (slen
< c
->minimum
)
220 adj_slen
= c
->minimum
;
221 if (slen
> c
->maximum
)
222 adj_slen
= slen
= c
->maximum
;
223 if (adj_slen
> slen
|| (u64
)src
% c
->alignment
) {
224 adj_slen
= min(adj_slen
, BOUNCE_BUFFER_SIZE
);
225 slen
= min(slen
, BOUNCE_BUFFER_SIZE
);
227 memset(ctx
->sbounce
+ slen
, 0, adj_slen
- slen
);
228 memcpy(ctx
->sbounce
, src
, slen
);
231 pr_debug("using comp sbounce buffer, len %x\n", slen
);
237 if ((u64
)dst
% c
->alignment
) {
238 dskip
= (int)(PTR_ALIGN(dst
, c
->alignment
) - dst
);
242 if (dlen
% c
->multiple
)
243 dlen
= round_down(dlen
, c
->multiple
);
244 if (dlen
< c
->minimum
) {
247 dlen
= min(p
->oremain
, BOUNCE_BUFFER_SIZE
);
248 dlen
= round_down(dlen
, c
->multiple
);
250 pr_debug("using comp dbounce buffer, len %x\n", dlen
);
252 if (dlen
> c
->maximum
)
256 timeout
= ktime_add_ms(ktime_get(), COMP_BUSY_TIMEOUT
);
258 dlen
= tmplen
; /* reset dlen, if we're retrying */
259 ret
= nx842_compress(src
, slen
, dst
, &dlen
, ctx
->wmem
);
260 /* possibly we should reduce the slen here, instead of
261 * retrying with the dbounce buffer?
263 if (ret
== -ENOSPC
&& dst
!= ctx
->dbounce
)
265 } while (ret
== -EBUSY
&& ktime_before(ktime_get(), timeout
));
271 if (dst
== ctx
->dbounce
)
272 memcpy(p
->out
+ dskip
, dst
, dlen
);
274 g
->padding
= cpu_to_be16(dskip
);
275 g
->compressed_length
= cpu_to_be32(dlen
);
276 g
->uncompressed_length
= cpu_to_be32(slen
);
278 if (p
->iremain
< slen
) {
279 *ignore
= slen
- p
->iremain
;
283 pr_debug("compress slen %x ignore %x dlen %x padding %x\n",
284 slen
, *ignore
, dlen
, dskip
);
286 return update_param(p
, slen
, dskip
+ dlen
);
289 static int nx842_crypto_compress(struct crypto_tfm
*tfm
,
290 const u8
*src
, unsigned int slen
,
291 u8
*dst
, unsigned int *dlen
)
293 struct nx842_crypto_ctx
*ctx
= crypto_tfm_ctx(tfm
);
294 struct nx842_crypto_header
*hdr
= &ctx
->header
;
295 struct nx842_crypto_param p
;
296 struct nx842_constraints c
;
297 unsigned int groups
, hdrsize
, h
;
310 ret
= read_constraints(&c
);
314 groups
= min_t(unsigned int, NX842_CRYPTO_GROUP_MAX
,
315 DIV_ROUND_UP(p
.iremain
, c
.maximum
));
316 hdrsize
= NX842_CRYPTO_HEADER_SIZE(groups
);
318 /* skip adding header if the buffers meet all constraints */
319 add_header
= (p
.iremain
% c
.multiple
||
320 p
.iremain
< c
.minimum
||
321 p
.iremain
> c
.maximum
||
322 (u64
)p
.in
% c
.alignment
||
323 p
.oremain
% c
.multiple
||
324 p
.oremain
< c
.minimum
||
325 p
.oremain
> c
.maximum
||
326 (u64
)p
.out
% c
.alignment
);
328 hdr
->magic
= cpu_to_be16(NX842_CRYPTO_MAGIC
);
332 while (p
.iremain
> 0) {
334 if (hdr
->groups
> NX842_CRYPTO_GROUP_MAX
)
337 /* header goes before first group */
338 h
= !n
&& add_header
? hdrsize
: 0;
341 pr_warn("interal error, ignore is set %x\n", ignore
);
343 ret
= compress(ctx
, &p
, &hdr
->group
[n
], &c
, &ignore
, h
);
348 if (!add_header
&& hdr
->groups
> 1) {
349 pr_err("Internal error: No header but multiple groups\n");
353 /* ignore indicates the input stream needed to be padded */
354 hdr
->ignore
= cpu_to_be16(ignore
);
356 pr_debug("marked %d bytes as ignore\n", ignore
);
359 ret
= nx842_crypto_add_header(hdr
, dst
);
365 pr_debug("compress total slen %x dlen %x\n", slen
, *dlen
);
370 static 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
,
377 unsigned int slen
= be32_to_cpu(g
->compressed_length
);
378 unsigned int required_len
= be32_to_cpu(g
->uncompressed_length
);
379 unsigned int dlen
= p
->oremain
, tmplen
;
380 unsigned int adj_slen
= slen
;
381 u8
*src
= p
->in
, *dst
= p
->out
;
382 u16 padding
= be16_to_cpu(g
->padding
);
383 int ret
, spadding
= 0, dpadding
= 0;
386 if (!slen
|| !required_len
)
389 if (p
->iremain
<= 0 || padding
+ slen
> p
->iremain
)
392 if (p
->oremain
<= 0 || required_len
- ignore
> p
->oremain
)
400 if (slen
% c
->multiple
)
401 adj_slen
= round_up(slen
, c
->multiple
);
402 if (slen
< c
->minimum
)
403 adj_slen
= c
->minimum
;
404 if (slen
> c
->maximum
)
406 if (slen
< adj_slen
|| (u64
)src
% c
->alignment
) {
407 /* we can append padding bytes because the 842 format defines
408 * an "end" template (see lib/842/842_decompress.c) and will
409 * ignore any bytes following it.
412 memset(ctx
->sbounce
+ slen
, 0, adj_slen
- slen
);
413 memcpy(ctx
->sbounce
, src
, slen
);
415 spadding
= adj_slen
- slen
;
417 pr_debug("using decomp sbounce buffer, len %x\n", slen
);
420 if (dlen
% c
->multiple
)
421 dlen
= round_down(dlen
, c
->multiple
);
422 if (dlen
< required_len
|| (u64
)dst
% c
->alignment
) {
424 dlen
= min(required_len
, BOUNCE_BUFFER_SIZE
);
425 pr_debug("using decomp dbounce buffer, len %x\n", dlen
);
427 if (dlen
< c
->minimum
)
429 if (dlen
> c
->maximum
)
433 timeout
= ktime_add_ms(ktime_get(), DECOMP_BUSY_TIMEOUT
);
435 dlen
= tmplen
; /* reset dlen, if we're retrying */
436 ret
= nx842_decompress(src
, slen
, dst
, &dlen
, ctx
->wmem
);
437 } while (ret
== -EBUSY
&& ktime_before(ktime_get(), timeout
));
440 /* reset everything, sw doesn't have constraints */
441 src
= p
->in
+ padding
;
442 slen
= be32_to_cpu(g
->compressed_length
);
447 if (dlen
< required_len
) { /* have ignore bytes */
449 dlen
= BOUNCE_BUFFER_SIZE
;
451 pr_info_ratelimited("using software 842 decompression\n");
452 ret
= sw842_decompress(src
, slen
, dst
, &dlen
);
461 pr_debug("ignoring last %x bytes\n", ignore
);
463 if (dst
== ctx
->dbounce
)
464 memcpy(p
->out
, dst
, dlen
);
466 pr_debug("decompress slen %x padding %x dlen %x ignore %x\n",
467 slen
, padding
, dlen
, ignore
);
469 return update_param(p
, slen
+ padding
, dlen
);
472 static int nx842_crypto_decompress(struct crypto_tfm
*tfm
,
473 const u8
*src
, unsigned int slen
,
474 u8
*dst
, unsigned int *dlen
)
476 struct nx842_crypto_ctx
*ctx
= crypto_tfm_ctx(tfm
);
477 struct nx842_crypto_header
*hdr
;
478 struct nx842_crypto_param p
;
479 struct nx842_constraints c
;
492 if (read_constraints(&c
))
495 hdr
= (struct nx842_crypto_header
*)src
;
497 /* If it doesn't start with our header magic number, assume it's a raw
498 * 842 compressed buffer and pass it directly to the hardware driver
500 if (be16_to_cpu(hdr
->magic
) != NX842_CRYPTO_MAGIC
) {
501 struct nx842_crypto_header_group g
= {
503 .compressed_length
= cpu_to_be32(p
.iremain
),
504 .uncompressed_length
= cpu_to_be32(p
.oremain
),
507 ret
= decompress(ctx
, &p
, &g
, &c
, 0, usehw
);
517 pr_err("header has no groups\n");
520 if (hdr
->groups
> NX842_CRYPTO_GROUP_MAX
) {
521 pr_err("header has too many groups %x, max %x\n",
522 hdr
->groups
, NX842_CRYPTO_GROUP_MAX
);
526 hdr_len
= NX842_CRYPTO_HEADER_SIZE(hdr
->groups
);
530 memcpy(&ctx
->header
, src
, hdr_len
);
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
);
538 ret
= decompress(ctx
, &p
, &hdr
->group
[n
], &c
, ignore
, usehw
);
545 pr_debug("decompress total slen %x dlen %x\n", slen
, *dlen
);
550 static struct crypto_alg alg
= {
552 .cra_driver_name
= "842-nx",
554 .cra_flags
= CRYPTO_ALG_TYPE_COMPRESS
,
555 .cra_ctxsize
= sizeof(struct nx842_crypto_ctx
),
556 .cra_module
= THIS_MODULE
,
557 .cra_init
= nx842_crypto_init
,
558 .cra_exit
= nx842_crypto_exit
,
559 .cra_u
= { .compress
= {
560 .coa_compress
= nx842_crypto_compress
,
561 .coa_decompress
= nx842_crypto_decompress
} }
564 static int __init
nx842_crypto_mod_init(void)
566 return crypto_register_alg(&alg
);
568 module_init(nx842_crypto_mod_init
);
570 static void __exit
nx842_crypto_mod_exit(void)
572 crypto_unregister_alg(&alg
);
574 module_exit(nx842_crypto_mod_exit
);
576 MODULE_LICENSE("GPL");
577 MODULE_DESCRIPTION("IBM PowerPC Nest (NX) 842 Hardware Compression Interface");
578 MODULE_ALIAS_CRYPTO("842");
579 MODULE_ALIAS_CRYPTO("842-nx");
580 MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");