162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci#ifndef __NX_842_H__
462306a36Sopenharmony_ci#define __NX_842_H__
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <crypto/algapi.h>
762306a36Sopenharmony_ci#include <linux/kernel.h>
862306a36Sopenharmony_ci#include <linux/init.h>
962306a36Sopenharmony_ci#include <linux/module.h>
1062306a36Sopenharmony_ci#include <linux/of.h>
1162306a36Sopenharmony_ci#include <linux/slab.h>
1262306a36Sopenharmony_ci#include <linux/io.h>
1362306a36Sopenharmony_ci#include <linux/mm.h>
1462306a36Sopenharmony_ci#include <linux/ratelimit.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/* Restrictions on Data Descriptor List (DDL) and Entry (DDE) buffers
1762306a36Sopenharmony_ci *
1862306a36Sopenharmony_ci * From NX P8 workbook, sec 4.9.1 "842 details"
1962306a36Sopenharmony_ci *   Each DDE buffer is 128 byte aligned
2062306a36Sopenharmony_ci *   Each DDE buffer size is a multiple of 32 bytes (except the last)
2162306a36Sopenharmony_ci *   The last DDE buffer size is a multiple of 8 bytes
2262306a36Sopenharmony_ci */
2362306a36Sopenharmony_ci#define DDE_BUFFER_ALIGN	(128)
2462306a36Sopenharmony_ci#define DDE_BUFFER_SIZE_MULT	(32)
2562306a36Sopenharmony_ci#define DDE_BUFFER_LAST_MULT	(8)
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci/* Arbitrary DDL length limit
2862306a36Sopenharmony_ci * Allows max buffer size of MAX-1 to MAX pages
2962306a36Sopenharmony_ci * (depending on alignment)
3062306a36Sopenharmony_ci */
3162306a36Sopenharmony_ci#define DDL_LEN_MAX		(17)
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci/* CCW 842 CI/FC masks
3462306a36Sopenharmony_ci * NX P8 workbook, section 4.3.1, figure 4-6
3562306a36Sopenharmony_ci * "CI/FC Boundary by NX CT type"
3662306a36Sopenharmony_ci */
3762306a36Sopenharmony_ci#define CCW_CI_842		(0x00003ff8)
3862306a36Sopenharmony_ci#define CCW_FC_842		(0x00000007)
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci/* CCW Function Codes (FC) for 842
4162306a36Sopenharmony_ci * NX P8 workbook, section 4.9, table 4-28
4262306a36Sopenharmony_ci * "Function Code Definitions for 842 Memory Compression"
4362306a36Sopenharmony_ci */
4462306a36Sopenharmony_ci#define CCW_FC_842_COMP_NOCRC	(0)
4562306a36Sopenharmony_ci#define CCW_FC_842_COMP_CRC	(1)
4662306a36Sopenharmony_ci#define CCW_FC_842_DECOMP_NOCRC	(2)
4762306a36Sopenharmony_ci#define CCW_FC_842_DECOMP_CRC	(3)
4862306a36Sopenharmony_ci#define CCW_FC_842_MOVE		(4)
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci/* CSB CC Error Types for 842
5162306a36Sopenharmony_ci * NX P8 workbook, section 4.10.3, table 4-30
5262306a36Sopenharmony_ci * "Reported Error Types Summary Table"
5362306a36Sopenharmony_ci */
5462306a36Sopenharmony_ci/* These are all duplicates of existing codes defined in icswx.h. */
5562306a36Sopenharmony_ci#define CSB_CC_TRANSLATION_DUP1	(80)
5662306a36Sopenharmony_ci#define CSB_CC_TRANSLATION_DUP2	(82)
5762306a36Sopenharmony_ci#define CSB_CC_TRANSLATION_DUP3	(84)
5862306a36Sopenharmony_ci#define CSB_CC_TRANSLATION_DUP4	(86)
5962306a36Sopenharmony_ci#define CSB_CC_TRANSLATION_DUP5	(92)
6062306a36Sopenharmony_ci#define CSB_CC_TRANSLATION_DUP6	(94)
6162306a36Sopenharmony_ci#define CSB_CC_PROTECTION_DUP1	(81)
6262306a36Sopenharmony_ci#define CSB_CC_PROTECTION_DUP2	(83)
6362306a36Sopenharmony_ci#define CSB_CC_PROTECTION_DUP3	(85)
6462306a36Sopenharmony_ci#define CSB_CC_PROTECTION_DUP4	(87)
6562306a36Sopenharmony_ci#define CSB_CC_PROTECTION_DUP5	(93)
6662306a36Sopenharmony_ci#define CSB_CC_PROTECTION_DUP6	(95)
6762306a36Sopenharmony_ci#define CSB_CC_RD_EXTERNAL_DUP1	(89)
6862306a36Sopenharmony_ci#define CSB_CC_RD_EXTERNAL_DUP2	(90)
6962306a36Sopenharmony_ci#define CSB_CC_RD_EXTERNAL_DUP3	(91)
7062306a36Sopenharmony_ci/* These are specific to NX */
7162306a36Sopenharmony_ci/* 842 codes */
7262306a36Sopenharmony_ci#define CSB_CC_TPBC_GT_SPBC	(64) /* no error, but >1 comp ratio */
7362306a36Sopenharmony_ci#define CSB_CC_CRC_MISMATCH	(65) /* decomp crc mismatch */
7462306a36Sopenharmony_ci#define CSB_CC_TEMPL_INVALID	(66) /* decomp invalid template value */
7562306a36Sopenharmony_ci#define CSB_CC_TEMPL_OVERFLOW	(67) /* decomp template shows data after end */
7662306a36Sopenharmony_ci/* sym crypt codes */
7762306a36Sopenharmony_ci#define CSB_CC_DECRYPT_OVERFLOW	(64)
7862306a36Sopenharmony_ci/* asym crypt codes */
7962306a36Sopenharmony_ci#define CSB_CC_MINV_OVERFLOW	(128)
8062306a36Sopenharmony_ci/*
8162306a36Sopenharmony_ci * HW error - Job did not finish in the maximum time allowed.
8262306a36Sopenharmony_ci * Job terminated.
8362306a36Sopenharmony_ci */
8462306a36Sopenharmony_ci#define CSB_CC_HW_EXPIRED_TIMER		(224)
8562306a36Sopenharmony_ci/* These are reserved for hypervisor use */
8662306a36Sopenharmony_ci#define CSB_CC_HYP_RESERVE_START	(240)
8762306a36Sopenharmony_ci#define CSB_CC_HYP_RESERVE_END		(253)
8862306a36Sopenharmony_ci#define CSB_CC_HYP_RESERVE_P9_END	(251)
8962306a36Sopenharmony_ci/* No valid interrupt server (P9 or later). */
9062306a36Sopenharmony_ci#define CSB_CC_HYP_RESERVE_NO_INTR_SERVER	(252)
9162306a36Sopenharmony_ci#define CSB_CC_HYP_NO_HW		(254)
9262306a36Sopenharmony_ci#define CSB_CC_HYP_HANG_ABORTED		(255)
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci/* CCB Completion Modes (CM) for 842
9562306a36Sopenharmony_ci * NX P8 workbook, section 4.3, figure 4-5
9662306a36Sopenharmony_ci * "CRB Details - Normal Cop_Req (CL=00, C=1)"
9762306a36Sopenharmony_ci */
9862306a36Sopenharmony_ci#define CCB_CM_EXTRA_WRITE	(CCB_CM0_ALL_COMPLETIONS & CCB_CM12_STORE)
9962306a36Sopenharmony_ci#define CCB_CM_INTERRUPT	(CCB_CM0_ALL_COMPLETIONS & CCB_CM12_INTERRUPT)
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci#define LEN_ON_SIZE(pa, size)	((size) - ((pa) & ((size) - 1)))
10262306a36Sopenharmony_ci#define LEN_ON_PAGE(pa)		LEN_ON_SIZE(pa, PAGE_SIZE)
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_cistatic inline unsigned long nx842_get_pa(void *addr)
10562306a36Sopenharmony_ci{
10662306a36Sopenharmony_ci	if (!is_vmalloc_addr(addr))
10762306a36Sopenharmony_ci		return __pa(addr);
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	return page_to_phys(vmalloc_to_page(addr)) + offset_in_page(addr);
11062306a36Sopenharmony_ci}
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci/**
11362306a36Sopenharmony_ci * This provides the driver's constraints.  Different nx842 implementations
11462306a36Sopenharmony_ci * may have varying requirements.  The constraints are:
11562306a36Sopenharmony_ci *   @alignment:	All buffers should be aligned to this
11662306a36Sopenharmony_ci *   @multiple:		All buffer lengths should be a multiple of this
11762306a36Sopenharmony_ci *   @minimum:		Buffer lengths must not be less than this amount
11862306a36Sopenharmony_ci *   @maximum:		Buffer lengths must not be more than this amount
11962306a36Sopenharmony_ci *
12062306a36Sopenharmony_ci * The constraints apply to all buffers and lengths, both input and output,
12162306a36Sopenharmony_ci * for both compression and decompression, except for the minimum which
12262306a36Sopenharmony_ci * only applies to compression input and decompression output; the
12362306a36Sopenharmony_ci * compressed data can be less than the minimum constraint.  It can be
12462306a36Sopenharmony_ci * assumed that compressed data will always adhere to the multiple
12562306a36Sopenharmony_ci * constraint.
12662306a36Sopenharmony_ci *
12762306a36Sopenharmony_ci * The driver may succeed even if these constraints are violated;
12862306a36Sopenharmony_ci * however the driver can return failure or suffer reduced performance
12962306a36Sopenharmony_ci * if any constraint is not met.
13062306a36Sopenharmony_ci */
13162306a36Sopenharmony_cistruct nx842_constraints {
13262306a36Sopenharmony_ci	int alignment;
13362306a36Sopenharmony_ci	int multiple;
13462306a36Sopenharmony_ci	int minimum;
13562306a36Sopenharmony_ci	int maximum;
13662306a36Sopenharmony_ci};
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_cistruct nx842_driver {
13962306a36Sopenharmony_ci	char *name;
14062306a36Sopenharmony_ci	struct module *owner;
14162306a36Sopenharmony_ci	size_t workmem_size;
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci	struct nx842_constraints *constraints;
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci	int (*compress)(const unsigned char *in, unsigned int in_len,
14662306a36Sopenharmony_ci			unsigned char *out, unsigned int *out_len,
14762306a36Sopenharmony_ci			void *wrkmem);
14862306a36Sopenharmony_ci	int (*decompress)(const unsigned char *in, unsigned int in_len,
14962306a36Sopenharmony_ci			  unsigned char *out, unsigned int *out_len,
15062306a36Sopenharmony_ci			  void *wrkmem);
15162306a36Sopenharmony_ci};
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_cistruct nx842_crypto_header_group {
15462306a36Sopenharmony_ci	__be16 padding;			/* unused bytes at start of group */
15562306a36Sopenharmony_ci	__be32 compressed_length;	/* compressed bytes in group */
15662306a36Sopenharmony_ci	__be32 uncompressed_length;	/* bytes after decompression */
15762306a36Sopenharmony_ci} __packed;
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_cistruct nx842_crypto_header {
16062306a36Sopenharmony_ci	__be16 magic;		/* NX842_CRYPTO_MAGIC */
16162306a36Sopenharmony_ci	__be16 ignore;		/* decompressed end bytes to ignore */
16262306a36Sopenharmony_ci	u8 groups;		/* total groups in this header */
16362306a36Sopenharmony_ci	struct nx842_crypto_header_group group[];
16462306a36Sopenharmony_ci} __packed;
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci#define NX842_CRYPTO_GROUP_MAX	(0x20)
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_cistruct nx842_crypto_ctx {
16962306a36Sopenharmony_ci	spinlock_t lock;
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci	u8 *wmem;
17262306a36Sopenharmony_ci	u8 *sbounce, *dbounce;
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	struct nx842_crypto_header header;
17562306a36Sopenharmony_ci	struct nx842_crypto_header_group group[NX842_CRYPTO_GROUP_MAX];
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci	struct nx842_driver *driver;
17862306a36Sopenharmony_ci};
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ciint nx842_crypto_init(struct crypto_tfm *tfm, struct nx842_driver *driver);
18162306a36Sopenharmony_civoid nx842_crypto_exit(struct crypto_tfm *tfm);
18262306a36Sopenharmony_ciint nx842_crypto_compress(struct crypto_tfm *tfm,
18362306a36Sopenharmony_ci			  const u8 *src, unsigned int slen,
18462306a36Sopenharmony_ci			  u8 *dst, unsigned int *dlen);
18562306a36Sopenharmony_ciint nx842_crypto_decompress(struct crypto_tfm *tfm,
18662306a36Sopenharmony_ci			    const u8 *src, unsigned int slen,
18762306a36Sopenharmony_ci			    u8 *dst, unsigned int *dlen);
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci#endif /* __NX_842_H__ */
190