// SPDX-License-Identifier: GPL-2.0-or-later /* * Cryptographic API for the NX-842 hardware compression. * * Copyright (C) IBM Corporation, 2011-2015 * * Designer of the Power data compression engine: * Bulent Abali <abali@us.ibm.com> * * Original Authors: Robert Jennings <rcj@linux.vnet.ibm.com> * Seth Jennings <sjenning@linux.vnet.ibm.com> * * Rewrite: Dan Streetman <ddstreet@ieee.org> * * This is an interface to the NX-842 compression hardware in PowerPC * processors. Most of the complexity of this drvier is due to the fact that * the NX-842 compression hardware requires the input and output data buffers * to be specifically aligned, to be a specific multiple in length, and within * specific minimum and maximum lengths. Those restrictions, provided by the * nx-842 driver via nx842_constraints, mean this driver must use bounce * buffers and headers to correct misaligned in or out buffers, and to split * input buffers that are too large. * * This driver will fall back to software decompression if the hardware * decompression fails, so this driver's decompression should never fail as * long as the provided compressed buffer is valid. Any compressed buffer * created by this driver will have a header (except ones where the input * perfectly matches the constraints); so users of this driver cannot simply * pass a compressed buffer created by this driver over to the 842 software * decompression library. Instead, users must use this driver to decompress; * if the hardware fails or is unavailable, the compressed buffer will be * parsed and the header removed, and the raw 842 buffer(s) passed to the 842 * software decompression library. * * This does not fall back to software compression, however, since the caller * of this function is specifically requesting hardware compression; if the * hardware compression fails, the caller can fall back to software * compression, and the raw 842 compressed buffer that the software compressor * creates can be passed to this driver for hardware decompression; any * buffer without our specific header magic is assumed to be a raw 842 buffer * and passed directly to the hardware. Note that the software compression * library will produce a compressed buffer that is incompatible with the * hardware decompressor if the original input buffer length is not a multiple * of 8; if such a compressed buffer is passed to this driver for * decompression, the hardware will reject it and this driver will then pass * it over to the software library for decompression.
*/
/* The first 5 bits of this magic are 0x1f, which is an invalid 842 5-bit * template (see lib/842/842.h), so this magic number will never appear at * the start of a raw 842 compressed buffer. That is important, as any buffer * passed to us without this magic is assumed to be a raw 842 compressed * buffer, and passed directly to the hardware to decompress.
*/ #define NX842_CRYPTO_MAGIC (0xf842) #define NX842_CRYPTO_HEADER_SIZE(g) \
(sizeof(struct nx842_crypto_header) + \ sizeof(struct nx842_crypto_header_group) * (g)) #define NX842_CRYPTO_HEADER_MAX_SIZE \
NX842_CRYPTO_HEADER_SIZE(NX842_CRYPTO_GROUP_MAX)
/* try longer on comp because we can fallback to sw decomp if hw is busy */ #define COMP_BUSY_TIMEOUT (250) /* ms */ #define DECOMP_BUSY_TIMEOUT (50) /* ms */
staticvoid check_constraints(struct nx842_constraints *c)
{ /* limit maximum, to always have enough bounce buffer to decompress */ if (c->maximum > BOUNCE_BUFFER_SIZE)
c->maximum = BOUNCE_BUFFER_SIZE;
}
staticint nx842_crypto_add_header(struct nx842_crypto_header *hdr, u8 *buf)
{ int s = NX842_CRYPTO_HEADER_SIZE(hdr->groups);
/* compress should have added space for header */ if (s > be16_to_cpu(hdr->group[0].padding)) {
pr_err("Internal error: no space for header\n"); return -EINVAL;
}
while (p.iremain > 0) {
n = hdr->groups++;
ret = -ENOSPC; if (hdr->groups > NX842_CRYPTO_GROUP_MAX) goto unlock;
/* header goes before first group */
h = !n && add_header ? hdrsize : 0;
if (ignore)
pr_warn("internal error, ignore is set %x\n", ignore);
ret = compress(ctx, &p, &hdr->group[n], &c, &ignore, h); if (ret) goto unlock;
}
if (!add_header && hdr->groups > 1) {
pr_err("Internal error: No header but multiple groups\n");
ret = -EINVAL; goto unlock;
}
/* ignore indicates the input stream needed to be padded */
hdr->ignore = cpu_to_be16(ignore); if (ignore)
pr_debug("marked %d bytes as ignore\n", ignore);
if (add_header)
ret = nx842_crypto_add_header(hdr, dst); if (ret) goto unlock;
*dlen = p.ototal;
pr_debug("compress total slen %x dlen %x\n", slen, *dlen);
if (slen % c->multiple)
adj_slen = round_up(slen, c->multiple); if (slen < c->minimum)
adj_slen = c->minimum; if (slen > c->maximum) goto usesw; if (slen < adj_slen || (u64)src % c->alignment) { /* we can append padding bytes because the 842 format defines * an "end" template (see lib/842/842_decompress.c) and will * ignore any bytes following it.
*/ if (slen < adj_slen)
memset(ctx->sbounce + slen, 0, adj_slen - slen);
memcpy(ctx->sbounce, src, slen);
src = ctx->sbounce;
spadding = adj_slen - slen;
slen = adj_slen;
pr_debug("using decomp sbounce buffer, len %x\n", slen);
}
if (dlen % c->multiple)
dlen = round_down(dlen, c->multiple); if (dlen < required_len || (u64)dst % c->alignment) {
dst = ctx->dbounce;
dlen = min(required_len, BOUNCE_BUFFER_SIZE);
pr_debug("using decomp dbounce buffer, len %x\n", dlen);
} if (dlen < c->minimum) goto usesw; if (dlen > c->maximum)
dlen = c->maximum;
tmplen = dlen;
timeout = ktime_add_ms(ktime_get(), DECOMP_BUSY_TIMEOUT); do {
dlen = tmplen; /* reset dlen, if we're retrying */
ret = ctx->driver->decompress(src, slen, dst, &dlen, ctx->wmem);
} while (ret == -EBUSY && ktime_before(ktime_get(), timeout)); if (ret) {
usesw: /* reset everything, sw doesn't have constraints */
src = p->in + padding;
slen = be32_to_cpu(g->compressed_length);
spadding = 0;
dst = p->out;
dlen = p->oremain; if (dlen < required_len) { /* have ignore bytes */
dst = ctx->dbounce;
dlen = BOUNCE_BUFFER_SIZE;
}
pr_info_ratelimited("using software 842 decompression\n");
ret = sw842_decompress(src, slen, dst, &dlen);
} if (ret) return ret;
slen -= spadding;
dlen -= ignore; if (ignore)
pr_debug("ignoring last %x bytes\n", ignore);
if (dst == ctx->dbounce)
memcpy(p->out, dst, dlen);
/* If it doesn't start with our header magic number, assume it's a raw * 842 compressed buffer and pass it directly to the hardware driver
*/ if (be16_to_cpu(hdr->magic) != NX842_CRYPTO_MAGIC) { struct nx842_crypto_header_group g = {
.padding = 0,
.compressed_length = cpu_to_be32(p.iremain),
.uncompressed_length = cpu_to_be32(p.oremain),
};
ret = decompress(ctx, &p, &g, &c, 0); if (ret) goto unlock;
goto success;
}
if (!hdr->groups) {
pr_err("header has no groups\n");
ret = -EINVAL; goto unlock;
} if (hdr->groups > NX842_CRYPTO_GROUP_MAX) {
pr_err("header has too many groups %x, max %x\n",
hdr->groups, NX842_CRYPTO_GROUP_MAX);
ret = -EINVAL; goto unlock;
}
hdr_len = NX842_CRYPTO_HEADER_SIZE(hdr->groups); if (hdr_len > slen) {
ret = -EOVERFLOW; goto unlock;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.