162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright 2016 Broadcom
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <linux/debugfs.h>
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include "cipher.h"
962306a36Sopenharmony_ci#include "util.h"
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci/* offset of SPU_OFIFO_CTRL register */
1262306a36Sopenharmony_ci#define SPU_OFIFO_CTRL      0x40
1362306a36Sopenharmony_ci#define SPU_FIFO_WATERMARK  0x1FF
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci/**
1662306a36Sopenharmony_ci * spu_sg_at_offset() - Find the scatterlist entry at a given distance from the
1762306a36Sopenharmony_ci * start of a scatterlist.
1862306a36Sopenharmony_ci * @sg:         [in]  Start of a scatterlist
1962306a36Sopenharmony_ci * @skip:       [in]  Distance from the start of the scatterlist, in bytes
2062306a36Sopenharmony_ci * @sge:        [out] Scatterlist entry at skip bytes from start
2162306a36Sopenharmony_ci * @sge_offset: [out] Number of bytes from start of sge buffer to get to
2262306a36Sopenharmony_ci *                    requested distance.
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * Return: 0 if entry found at requested distance
2562306a36Sopenharmony_ci *         < 0 otherwise
2662306a36Sopenharmony_ci */
2762306a36Sopenharmony_ciint spu_sg_at_offset(struct scatterlist *sg, unsigned int skip,
2862306a36Sopenharmony_ci		     struct scatterlist **sge, unsigned int *sge_offset)
2962306a36Sopenharmony_ci{
3062306a36Sopenharmony_ci	/* byte index from start of sg to the end of the previous entry */
3162306a36Sopenharmony_ci	unsigned int index = 0;
3262306a36Sopenharmony_ci	/* byte index from start of sg to the end of the current entry */
3362306a36Sopenharmony_ci	unsigned int next_index;
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci	next_index = sg->length;
3662306a36Sopenharmony_ci	while (next_index <= skip) {
3762306a36Sopenharmony_ci		sg = sg_next(sg);
3862306a36Sopenharmony_ci		index = next_index;
3962306a36Sopenharmony_ci		if (!sg)
4062306a36Sopenharmony_ci			return -EINVAL;
4162306a36Sopenharmony_ci		next_index += sg->length;
4262306a36Sopenharmony_ci	}
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	*sge_offset = skip - index;
4562306a36Sopenharmony_ci	*sge = sg;
4662306a36Sopenharmony_ci	return 0;
4762306a36Sopenharmony_ci}
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci/* Copy len bytes of sg data, starting at offset skip, to a dest buffer */
5062306a36Sopenharmony_civoid sg_copy_part_to_buf(struct scatterlist *src, u8 *dest,
5162306a36Sopenharmony_ci			 unsigned int len, unsigned int skip)
5262306a36Sopenharmony_ci{
5362306a36Sopenharmony_ci	size_t copied;
5462306a36Sopenharmony_ci	unsigned int nents = sg_nents(src);
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	copied = sg_pcopy_to_buffer(src, nents, dest, len, skip);
5762306a36Sopenharmony_ci	if (copied != len) {
5862306a36Sopenharmony_ci		flow_log("%s copied %u bytes of %u requested. ",
5962306a36Sopenharmony_ci			 __func__, (u32)copied, len);
6062306a36Sopenharmony_ci		flow_log("sg with %u entries and skip %u\n", nents, skip);
6162306a36Sopenharmony_ci	}
6262306a36Sopenharmony_ci}
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci/*
6562306a36Sopenharmony_ci * Copy data into a scatterlist starting at a specified offset in the
6662306a36Sopenharmony_ci * scatterlist. Specifically, copy len bytes of data in the buffer src
6762306a36Sopenharmony_ci * into the scatterlist dest, starting skip bytes into the scatterlist.
6862306a36Sopenharmony_ci */
6962306a36Sopenharmony_civoid sg_copy_part_from_buf(struct scatterlist *dest, u8 *src,
7062306a36Sopenharmony_ci			   unsigned int len, unsigned int skip)
7162306a36Sopenharmony_ci{
7262306a36Sopenharmony_ci	size_t copied;
7362306a36Sopenharmony_ci	unsigned int nents = sg_nents(dest);
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	copied = sg_pcopy_from_buffer(dest, nents, src, len, skip);
7662306a36Sopenharmony_ci	if (copied != len) {
7762306a36Sopenharmony_ci		flow_log("%s copied %u bytes of %u requested. ",
7862306a36Sopenharmony_ci			 __func__, (u32)copied, len);
7962306a36Sopenharmony_ci		flow_log("sg with %u entries and skip %u\n", nents, skip);
8062306a36Sopenharmony_ci	}
8162306a36Sopenharmony_ci}
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci/**
8462306a36Sopenharmony_ci * spu_sg_count() - Determine number of elements in scatterlist to provide a
8562306a36Sopenharmony_ci * specified number of bytes.
8662306a36Sopenharmony_ci * @sg_list:  scatterlist to examine
8762306a36Sopenharmony_ci * @skip:     index of starting point
8862306a36Sopenharmony_ci * @nbytes:   consider elements of scatterlist until reaching this number of
8962306a36Sopenharmony_ci *	      bytes
9062306a36Sopenharmony_ci *
9162306a36Sopenharmony_ci * Return: the number of sg entries contributing to nbytes of data
9262306a36Sopenharmony_ci */
9362306a36Sopenharmony_ciint spu_sg_count(struct scatterlist *sg_list, unsigned int skip, int nbytes)
9462306a36Sopenharmony_ci{
9562306a36Sopenharmony_ci	struct scatterlist *sg;
9662306a36Sopenharmony_ci	int sg_nents = 0;
9762306a36Sopenharmony_ci	unsigned int offset;
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci	if (!sg_list)
10062306a36Sopenharmony_ci		return 0;
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci	if (spu_sg_at_offset(sg_list, skip, &sg, &offset) < 0)
10362306a36Sopenharmony_ci		return 0;
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	while (sg && (nbytes > 0)) {
10662306a36Sopenharmony_ci		sg_nents++;
10762306a36Sopenharmony_ci		nbytes -= (sg->length - offset);
10862306a36Sopenharmony_ci		offset = 0;
10962306a36Sopenharmony_ci		sg = sg_next(sg);
11062306a36Sopenharmony_ci	}
11162306a36Sopenharmony_ci	return sg_nents;
11262306a36Sopenharmony_ci}
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci/**
11562306a36Sopenharmony_ci * spu_msg_sg_add() - Copy scatterlist entries from one sg to another, up to a
11662306a36Sopenharmony_ci * given length.
11762306a36Sopenharmony_ci * @to_sg:       scatterlist to copy to
11862306a36Sopenharmony_ci * @from_sg:     scatterlist to copy from
11962306a36Sopenharmony_ci * @from_skip:   number of bytes to skip in from_sg. Non-zero when previous
12062306a36Sopenharmony_ci *		 request included part of the buffer in entry in from_sg.
12162306a36Sopenharmony_ci *		 Assumes from_skip < from_sg->length.
12262306a36Sopenharmony_ci * @from_nents:  number of entries in from_sg
12362306a36Sopenharmony_ci * @length:      number of bytes to copy. may reach this limit before exhausting
12462306a36Sopenharmony_ci *		 from_sg.
12562306a36Sopenharmony_ci *
12662306a36Sopenharmony_ci * Copies the entries themselves, not the data in the entries. Assumes to_sg has
12762306a36Sopenharmony_ci * enough entries. Does not limit the size of an individual buffer in to_sg.
12862306a36Sopenharmony_ci *
12962306a36Sopenharmony_ci * to_sg, from_sg, skip are all updated to end of copy
13062306a36Sopenharmony_ci *
13162306a36Sopenharmony_ci * Return: Number of bytes copied
13262306a36Sopenharmony_ci */
13362306a36Sopenharmony_ciu32 spu_msg_sg_add(struct scatterlist **to_sg,
13462306a36Sopenharmony_ci		   struct scatterlist **from_sg, u32 *from_skip,
13562306a36Sopenharmony_ci		   u8 from_nents, u32 length)
13662306a36Sopenharmony_ci{
13762306a36Sopenharmony_ci	struct scatterlist *sg;	/* an entry in from_sg */
13862306a36Sopenharmony_ci	struct scatterlist *to = *to_sg;
13962306a36Sopenharmony_ci	struct scatterlist *from = *from_sg;
14062306a36Sopenharmony_ci	u32 skip = *from_skip;
14162306a36Sopenharmony_ci	u32 offset;
14262306a36Sopenharmony_ci	int i;
14362306a36Sopenharmony_ci	u32 entry_len = 0;
14462306a36Sopenharmony_ci	u32 frag_len = 0;	/* length of entry added to to_sg */
14562306a36Sopenharmony_ci	u32 copied = 0;		/* number of bytes copied so far */
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci	if (length == 0)
14862306a36Sopenharmony_ci		return 0;
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci	for_each_sg(from, sg, from_nents, i) {
15162306a36Sopenharmony_ci		/* number of bytes in this from entry not yet used */
15262306a36Sopenharmony_ci		entry_len = sg->length - skip;
15362306a36Sopenharmony_ci		frag_len = min(entry_len, length - copied);
15462306a36Sopenharmony_ci		offset = sg->offset + skip;
15562306a36Sopenharmony_ci		if (frag_len)
15662306a36Sopenharmony_ci			sg_set_page(to++, sg_page(sg), frag_len, offset);
15762306a36Sopenharmony_ci		copied += frag_len;
15862306a36Sopenharmony_ci		if (copied == entry_len) {
15962306a36Sopenharmony_ci			/* used up all of from entry */
16062306a36Sopenharmony_ci			skip = 0;	/* start at beginning of next entry */
16162306a36Sopenharmony_ci		}
16262306a36Sopenharmony_ci		if (copied == length)
16362306a36Sopenharmony_ci			break;
16462306a36Sopenharmony_ci	}
16562306a36Sopenharmony_ci	*to_sg = to;
16662306a36Sopenharmony_ci	*from_sg = sg;
16762306a36Sopenharmony_ci	if (frag_len < entry_len)
16862306a36Sopenharmony_ci		*from_skip = skip + frag_len;
16962306a36Sopenharmony_ci	else
17062306a36Sopenharmony_ci		*from_skip = 0;
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci	return copied;
17362306a36Sopenharmony_ci}
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_civoid add_to_ctr(u8 *ctr_pos, unsigned int increment)
17662306a36Sopenharmony_ci{
17762306a36Sopenharmony_ci	__be64 *high_be = (__be64 *)ctr_pos;
17862306a36Sopenharmony_ci	__be64 *low_be = high_be + 1;
17962306a36Sopenharmony_ci	u64 orig_low = __be64_to_cpu(*low_be);
18062306a36Sopenharmony_ci	u64 new_low = orig_low + (u64)increment;
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	*low_be = __cpu_to_be64(new_low);
18362306a36Sopenharmony_ci	if (new_low < orig_low)
18462306a36Sopenharmony_ci		/* there was a carry from the low 8 bytes */
18562306a36Sopenharmony_ci		*high_be = __cpu_to_be64(__be64_to_cpu(*high_be) + 1);
18662306a36Sopenharmony_ci}
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_cistruct sdesc {
18962306a36Sopenharmony_ci	struct shash_desc shash;
19062306a36Sopenharmony_ci	char ctx[];
19162306a36Sopenharmony_ci};
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci/**
19462306a36Sopenharmony_ci * do_shash() - Do a synchronous hash operation in software
19562306a36Sopenharmony_ci * @name:       The name of the hash algorithm
19662306a36Sopenharmony_ci * @result:     Buffer where digest is to be written
19762306a36Sopenharmony_ci * @data1:      First part of data to hash. May be NULL.
19862306a36Sopenharmony_ci * @data1_len:  Length of data1, in bytes
19962306a36Sopenharmony_ci * @data2:      Second part of data to hash. May be NULL.
20062306a36Sopenharmony_ci * @data2_len:  Length of data2, in bytes
20162306a36Sopenharmony_ci * @key:	Key (if keyed hash)
20262306a36Sopenharmony_ci * @key_len:	Length of key, in bytes (or 0 if non-keyed hash)
20362306a36Sopenharmony_ci *
20462306a36Sopenharmony_ci * Note that the crypto API will not select this driver's own transform because
20562306a36Sopenharmony_ci * this driver only registers asynchronous algos.
20662306a36Sopenharmony_ci *
20762306a36Sopenharmony_ci * Return: 0 if hash successfully stored in result
20862306a36Sopenharmony_ci *         < 0 otherwise
20962306a36Sopenharmony_ci */
21062306a36Sopenharmony_ciint do_shash(unsigned char *name, unsigned char *result,
21162306a36Sopenharmony_ci	     const u8 *data1, unsigned int data1_len,
21262306a36Sopenharmony_ci	     const u8 *data2, unsigned int data2_len,
21362306a36Sopenharmony_ci	     const u8 *key, unsigned int key_len)
21462306a36Sopenharmony_ci{
21562306a36Sopenharmony_ci	int rc;
21662306a36Sopenharmony_ci	unsigned int size;
21762306a36Sopenharmony_ci	struct crypto_shash *hash;
21862306a36Sopenharmony_ci	struct sdesc *sdesc;
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_ci	hash = crypto_alloc_shash(name, 0, 0);
22162306a36Sopenharmony_ci	if (IS_ERR(hash)) {
22262306a36Sopenharmony_ci		rc = PTR_ERR(hash);
22362306a36Sopenharmony_ci		pr_err("%s: Crypto %s allocation error %d\n", __func__, name, rc);
22462306a36Sopenharmony_ci		return rc;
22562306a36Sopenharmony_ci	}
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci	size = sizeof(struct shash_desc) + crypto_shash_descsize(hash);
22862306a36Sopenharmony_ci	sdesc = kmalloc(size, GFP_KERNEL);
22962306a36Sopenharmony_ci	if (!sdesc) {
23062306a36Sopenharmony_ci		rc = -ENOMEM;
23162306a36Sopenharmony_ci		goto do_shash_err;
23262306a36Sopenharmony_ci	}
23362306a36Sopenharmony_ci	sdesc->shash.tfm = hash;
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci	if (key_len > 0) {
23662306a36Sopenharmony_ci		rc = crypto_shash_setkey(hash, key, key_len);
23762306a36Sopenharmony_ci		if (rc) {
23862306a36Sopenharmony_ci			pr_err("%s: Could not setkey %s shash\n", __func__, name);
23962306a36Sopenharmony_ci			goto do_shash_err;
24062306a36Sopenharmony_ci		}
24162306a36Sopenharmony_ci	}
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_ci	rc = crypto_shash_init(&sdesc->shash);
24462306a36Sopenharmony_ci	if (rc) {
24562306a36Sopenharmony_ci		pr_err("%s: Could not init %s shash\n", __func__, name);
24662306a36Sopenharmony_ci		goto do_shash_err;
24762306a36Sopenharmony_ci	}
24862306a36Sopenharmony_ci	rc = crypto_shash_update(&sdesc->shash, data1, data1_len);
24962306a36Sopenharmony_ci	if (rc) {
25062306a36Sopenharmony_ci		pr_err("%s: Could not update1\n", __func__);
25162306a36Sopenharmony_ci		goto do_shash_err;
25262306a36Sopenharmony_ci	}
25362306a36Sopenharmony_ci	if (data2 && data2_len) {
25462306a36Sopenharmony_ci		rc = crypto_shash_update(&sdesc->shash, data2, data2_len);
25562306a36Sopenharmony_ci		if (rc) {
25662306a36Sopenharmony_ci			pr_err("%s: Could not update2\n", __func__);
25762306a36Sopenharmony_ci			goto do_shash_err;
25862306a36Sopenharmony_ci		}
25962306a36Sopenharmony_ci	}
26062306a36Sopenharmony_ci	rc = crypto_shash_final(&sdesc->shash, result);
26162306a36Sopenharmony_ci	if (rc)
26262306a36Sopenharmony_ci		pr_err("%s: Could not generate %s hash\n", __func__, name);
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_cido_shash_err:
26562306a36Sopenharmony_ci	crypto_free_shash(hash);
26662306a36Sopenharmony_ci	kfree(sdesc);
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci	return rc;
26962306a36Sopenharmony_ci}
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ci#ifdef DEBUG
27262306a36Sopenharmony_ci/* Dump len bytes of a scatterlist starting at skip bytes into the sg */
27362306a36Sopenharmony_civoid __dump_sg(struct scatterlist *sg, unsigned int skip, unsigned int len)
27462306a36Sopenharmony_ci{
27562306a36Sopenharmony_ci	u8 dbuf[16];
27662306a36Sopenharmony_ci	unsigned int idx = skip;
27762306a36Sopenharmony_ci	unsigned int num_out = 0;	/* number of bytes dumped so far */
27862306a36Sopenharmony_ci	unsigned int count;
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci	if (packet_debug_logging) {
28162306a36Sopenharmony_ci		while (num_out < len) {
28262306a36Sopenharmony_ci			count = (len - num_out > 16) ? 16 : len - num_out;
28362306a36Sopenharmony_ci			sg_copy_part_to_buf(sg, dbuf, count, idx);
28462306a36Sopenharmony_ci			num_out += count;
28562306a36Sopenharmony_ci			print_hex_dump(KERN_ALERT, "  sg: ", DUMP_PREFIX_NONE,
28662306a36Sopenharmony_ci				       4, 1, dbuf, count, false);
28762306a36Sopenharmony_ci			idx += 16;
28862306a36Sopenharmony_ci		}
28962306a36Sopenharmony_ci	}
29062306a36Sopenharmony_ci	if (debug_logging_sleep)
29162306a36Sopenharmony_ci		msleep(debug_logging_sleep);
29262306a36Sopenharmony_ci}
29362306a36Sopenharmony_ci#endif
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci/* Returns the name for a given cipher alg/mode */
29662306a36Sopenharmony_cichar *spu_alg_name(enum spu_cipher_alg alg, enum spu_cipher_mode mode)
29762306a36Sopenharmony_ci{
29862306a36Sopenharmony_ci	switch (alg) {
29962306a36Sopenharmony_ci	case CIPHER_ALG_RC4:
30062306a36Sopenharmony_ci		return "rc4";
30162306a36Sopenharmony_ci	case CIPHER_ALG_AES:
30262306a36Sopenharmony_ci		switch (mode) {
30362306a36Sopenharmony_ci		case CIPHER_MODE_CBC:
30462306a36Sopenharmony_ci			return "cbc(aes)";
30562306a36Sopenharmony_ci		case CIPHER_MODE_ECB:
30662306a36Sopenharmony_ci			return "ecb(aes)";
30762306a36Sopenharmony_ci		case CIPHER_MODE_OFB:
30862306a36Sopenharmony_ci			return "ofb(aes)";
30962306a36Sopenharmony_ci		case CIPHER_MODE_CFB:
31062306a36Sopenharmony_ci			return "cfb(aes)";
31162306a36Sopenharmony_ci		case CIPHER_MODE_CTR:
31262306a36Sopenharmony_ci			return "ctr(aes)";
31362306a36Sopenharmony_ci		case CIPHER_MODE_XTS:
31462306a36Sopenharmony_ci			return "xts(aes)";
31562306a36Sopenharmony_ci		case CIPHER_MODE_GCM:
31662306a36Sopenharmony_ci			return "gcm(aes)";
31762306a36Sopenharmony_ci		default:
31862306a36Sopenharmony_ci			return "aes";
31962306a36Sopenharmony_ci		}
32062306a36Sopenharmony_ci		break;
32162306a36Sopenharmony_ci	case CIPHER_ALG_DES:
32262306a36Sopenharmony_ci		switch (mode) {
32362306a36Sopenharmony_ci		case CIPHER_MODE_CBC:
32462306a36Sopenharmony_ci			return "cbc(des)";
32562306a36Sopenharmony_ci		case CIPHER_MODE_ECB:
32662306a36Sopenharmony_ci			return "ecb(des)";
32762306a36Sopenharmony_ci		case CIPHER_MODE_CTR:
32862306a36Sopenharmony_ci			return "ctr(des)";
32962306a36Sopenharmony_ci		default:
33062306a36Sopenharmony_ci			return "des";
33162306a36Sopenharmony_ci		}
33262306a36Sopenharmony_ci		break;
33362306a36Sopenharmony_ci	case CIPHER_ALG_3DES:
33462306a36Sopenharmony_ci		switch (mode) {
33562306a36Sopenharmony_ci		case CIPHER_MODE_CBC:
33662306a36Sopenharmony_ci			return "cbc(des3_ede)";
33762306a36Sopenharmony_ci		case CIPHER_MODE_ECB:
33862306a36Sopenharmony_ci			return "ecb(des3_ede)";
33962306a36Sopenharmony_ci		case CIPHER_MODE_CTR:
34062306a36Sopenharmony_ci			return "ctr(des3_ede)";
34162306a36Sopenharmony_ci		default:
34262306a36Sopenharmony_ci			return "3des";
34362306a36Sopenharmony_ci		}
34462306a36Sopenharmony_ci		break;
34562306a36Sopenharmony_ci	default:
34662306a36Sopenharmony_ci		return "other";
34762306a36Sopenharmony_ci	}
34862306a36Sopenharmony_ci}
34962306a36Sopenharmony_ci
35062306a36Sopenharmony_cistatic ssize_t spu_debugfs_read(struct file *filp, char __user *ubuf,
35162306a36Sopenharmony_ci				size_t count, loff_t *offp)
35262306a36Sopenharmony_ci{
35362306a36Sopenharmony_ci	struct bcm_device_private *ipriv;
35462306a36Sopenharmony_ci	char *buf;
35562306a36Sopenharmony_ci	ssize_t ret, out_offset, out_count;
35662306a36Sopenharmony_ci	int i;
35762306a36Sopenharmony_ci	u32 fifo_len;
35862306a36Sopenharmony_ci	u32 spu_ofifo_ctrl;
35962306a36Sopenharmony_ci	u32 alg;
36062306a36Sopenharmony_ci	u32 mode;
36162306a36Sopenharmony_ci	u32 op_cnt;
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_ci	out_count = 2048;
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ci	buf = kmalloc(out_count, GFP_KERNEL);
36662306a36Sopenharmony_ci	if (!buf)
36762306a36Sopenharmony_ci		return -ENOMEM;
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_ci	ipriv = filp->private_data;
37062306a36Sopenharmony_ci	out_offset = 0;
37162306a36Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
37262306a36Sopenharmony_ci			       "Number of SPUs.........%u\n",
37362306a36Sopenharmony_ci			       ipriv->spu.num_spu);
37462306a36Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
37562306a36Sopenharmony_ci			       "Current sessions.......%u\n",
37662306a36Sopenharmony_ci			       atomic_read(&ipriv->session_count));
37762306a36Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
37862306a36Sopenharmony_ci			       "Session count..........%u\n",
37962306a36Sopenharmony_ci			       atomic_read(&ipriv->stream_count));
38062306a36Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
38162306a36Sopenharmony_ci			       "Cipher setkey..........%u\n",
38262306a36Sopenharmony_ci			       atomic_read(&ipriv->setkey_cnt[SPU_OP_CIPHER]));
38362306a36Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
38462306a36Sopenharmony_ci			       "Cipher Ops.............%u\n",
38562306a36Sopenharmony_ci			       atomic_read(&ipriv->op_counts[SPU_OP_CIPHER]));
38662306a36Sopenharmony_ci	for (alg = 0; alg < CIPHER_ALG_LAST; alg++) {
38762306a36Sopenharmony_ci		for (mode = 0; mode < CIPHER_MODE_LAST; mode++) {
38862306a36Sopenharmony_ci			op_cnt = atomic_read(&ipriv->cipher_cnt[alg][mode]);
38962306a36Sopenharmony_ci			if (op_cnt) {
39062306a36Sopenharmony_ci				out_offset += scnprintf(buf + out_offset,
39162306a36Sopenharmony_ci						       out_count - out_offset,
39262306a36Sopenharmony_ci			       "  %-13s%11u\n",
39362306a36Sopenharmony_ci			       spu_alg_name(alg, mode), op_cnt);
39462306a36Sopenharmony_ci			}
39562306a36Sopenharmony_ci		}
39662306a36Sopenharmony_ci	}
39762306a36Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
39862306a36Sopenharmony_ci			       "Hash Ops...............%u\n",
39962306a36Sopenharmony_ci			       atomic_read(&ipriv->op_counts[SPU_OP_HASH]));
40062306a36Sopenharmony_ci	for (alg = 0; alg < HASH_ALG_LAST; alg++) {
40162306a36Sopenharmony_ci		op_cnt = atomic_read(&ipriv->hash_cnt[alg]);
40262306a36Sopenharmony_ci		if (op_cnt) {
40362306a36Sopenharmony_ci			out_offset += scnprintf(buf + out_offset,
40462306a36Sopenharmony_ci					       out_count - out_offset,
40562306a36Sopenharmony_ci		       "  %-13s%11u\n",
40662306a36Sopenharmony_ci		       hash_alg_name[alg], op_cnt);
40762306a36Sopenharmony_ci		}
40862306a36Sopenharmony_ci	}
40962306a36Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
41062306a36Sopenharmony_ci			       "HMAC setkey............%u\n",
41162306a36Sopenharmony_ci			       atomic_read(&ipriv->setkey_cnt[SPU_OP_HMAC]));
41262306a36Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
41362306a36Sopenharmony_ci			       "HMAC Ops...............%u\n",
41462306a36Sopenharmony_ci			       atomic_read(&ipriv->op_counts[SPU_OP_HMAC]));
41562306a36Sopenharmony_ci	for (alg = 0; alg < HASH_ALG_LAST; alg++) {
41662306a36Sopenharmony_ci		op_cnt = atomic_read(&ipriv->hmac_cnt[alg]);
41762306a36Sopenharmony_ci		if (op_cnt) {
41862306a36Sopenharmony_ci			out_offset += scnprintf(buf + out_offset,
41962306a36Sopenharmony_ci					       out_count - out_offset,
42062306a36Sopenharmony_ci		       "  %-13s%11u\n",
42162306a36Sopenharmony_ci		       hash_alg_name[alg], op_cnt);
42262306a36Sopenharmony_ci		}
42362306a36Sopenharmony_ci	}
42462306a36Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
42562306a36Sopenharmony_ci			       "AEAD setkey............%u\n",
42662306a36Sopenharmony_ci			       atomic_read(&ipriv->setkey_cnt[SPU_OP_AEAD]));
42762306a36Sopenharmony_ci
42862306a36Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
42962306a36Sopenharmony_ci			       "AEAD Ops...............%u\n",
43062306a36Sopenharmony_ci			       atomic_read(&ipriv->op_counts[SPU_OP_AEAD]));
43162306a36Sopenharmony_ci	for (alg = 0; alg < AEAD_TYPE_LAST; alg++) {
43262306a36Sopenharmony_ci		op_cnt = atomic_read(&ipriv->aead_cnt[alg]);
43362306a36Sopenharmony_ci		if (op_cnt) {
43462306a36Sopenharmony_ci			out_offset += scnprintf(buf + out_offset,
43562306a36Sopenharmony_ci					       out_count - out_offset,
43662306a36Sopenharmony_ci		       "  %-13s%11u\n",
43762306a36Sopenharmony_ci		       aead_alg_name[alg], op_cnt);
43862306a36Sopenharmony_ci		}
43962306a36Sopenharmony_ci	}
44062306a36Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
44162306a36Sopenharmony_ci			       "Bytes of req data......%llu\n",
44262306a36Sopenharmony_ci			       (u64)atomic64_read(&ipriv->bytes_out));
44362306a36Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
44462306a36Sopenharmony_ci			       "Bytes of resp data.....%llu\n",
44562306a36Sopenharmony_ci			       (u64)atomic64_read(&ipriv->bytes_in));
44662306a36Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
44762306a36Sopenharmony_ci			       "Mailbox full...........%u\n",
44862306a36Sopenharmony_ci			       atomic_read(&ipriv->mb_no_spc));
44962306a36Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
45062306a36Sopenharmony_ci			       "Mailbox send failures..%u\n",
45162306a36Sopenharmony_ci			       atomic_read(&ipriv->mb_send_fail));
45262306a36Sopenharmony_ci	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
45362306a36Sopenharmony_ci			       "Check ICV errors.......%u\n",
45462306a36Sopenharmony_ci			       atomic_read(&ipriv->bad_icv));
45562306a36Sopenharmony_ci	if (ipriv->spu.spu_type == SPU_TYPE_SPUM)
45662306a36Sopenharmony_ci		for (i = 0; i < ipriv->spu.num_spu; i++) {
45762306a36Sopenharmony_ci			spu_ofifo_ctrl = ioread32(ipriv->spu.reg_vbase[i] +
45862306a36Sopenharmony_ci						  SPU_OFIFO_CTRL);
45962306a36Sopenharmony_ci			fifo_len = spu_ofifo_ctrl & SPU_FIFO_WATERMARK;
46062306a36Sopenharmony_ci			out_offset += scnprintf(buf + out_offset,
46162306a36Sopenharmony_ci					       out_count - out_offset,
46262306a36Sopenharmony_ci				       "SPU %d output FIFO high water.....%u\n",
46362306a36Sopenharmony_ci				       i, fifo_len);
46462306a36Sopenharmony_ci		}
46562306a36Sopenharmony_ci
46662306a36Sopenharmony_ci	if (out_offset > out_count)
46762306a36Sopenharmony_ci		out_offset = out_count;
46862306a36Sopenharmony_ci
46962306a36Sopenharmony_ci	ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
47062306a36Sopenharmony_ci	kfree(buf);
47162306a36Sopenharmony_ci	return ret;
47262306a36Sopenharmony_ci}
47362306a36Sopenharmony_ci
47462306a36Sopenharmony_cistatic const struct file_operations spu_debugfs_stats = {
47562306a36Sopenharmony_ci	.owner = THIS_MODULE,
47662306a36Sopenharmony_ci	.open = simple_open,
47762306a36Sopenharmony_ci	.read = spu_debugfs_read,
47862306a36Sopenharmony_ci};
47962306a36Sopenharmony_ci
48062306a36Sopenharmony_ci/*
48162306a36Sopenharmony_ci * Create the debug FS directories. If the top-level directory has not yet
48262306a36Sopenharmony_ci * been created, create it now. Create a stats file in this directory for
48362306a36Sopenharmony_ci * a SPU.
48462306a36Sopenharmony_ci */
48562306a36Sopenharmony_civoid spu_setup_debugfs(void)
48662306a36Sopenharmony_ci{
48762306a36Sopenharmony_ci	if (!debugfs_initialized())
48862306a36Sopenharmony_ci		return;
48962306a36Sopenharmony_ci
49062306a36Sopenharmony_ci	if (!iproc_priv.debugfs_dir)
49162306a36Sopenharmony_ci		iproc_priv.debugfs_dir = debugfs_create_dir(KBUILD_MODNAME,
49262306a36Sopenharmony_ci							    NULL);
49362306a36Sopenharmony_ci
49462306a36Sopenharmony_ci	if (!iproc_priv.debugfs_stats)
49562306a36Sopenharmony_ci		/* Create file with permissions S_IRUSR */
49662306a36Sopenharmony_ci		debugfs_create_file("stats", 0400, iproc_priv.debugfs_dir,
49762306a36Sopenharmony_ci				    &iproc_priv, &spu_debugfs_stats);
49862306a36Sopenharmony_ci}
49962306a36Sopenharmony_ci
50062306a36Sopenharmony_civoid spu_free_debugfs(void)
50162306a36Sopenharmony_ci{
50262306a36Sopenharmony_ci	debugfs_remove_recursive(iproc_priv.debugfs_dir);
50362306a36Sopenharmony_ci	iproc_priv.debugfs_dir = NULL;
50462306a36Sopenharmony_ci}
50562306a36Sopenharmony_ci
50662306a36Sopenharmony_ci/**
50762306a36Sopenharmony_ci * format_value_ccm() - Format a value into a buffer, using a specified number
50862306a36Sopenharmony_ci *			of bytes (i.e. maybe writing value X into a 4 byte
50962306a36Sopenharmony_ci *			buffer, or maybe into a 12 byte buffer), as per the
51062306a36Sopenharmony_ci *			SPU CCM spec.
51162306a36Sopenharmony_ci *
51262306a36Sopenharmony_ci * @val:		value to write (up to max of unsigned int)
51362306a36Sopenharmony_ci * @buf:		(pointer to) buffer to write the value
51462306a36Sopenharmony_ci * @len:		number of bytes to use (0 to 255)
51562306a36Sopenharmony_ci *
51662306a36Sopenharmony_ci */
51762306a36Sopenharmony_civoid format_value_ccm(unsigned int val, u8 *buf, u8 len)
51862306a36Sopenharmony_ci{
51962306a36Sopenharmony_ci	int i;
52062306a36Sopenharmony_ci
52162306a36Sopenharmony_ci	/* First clear full output buffer */
52262306a36Sopenharmony_ci	memset(buf, 0, len);
52362306a36Sopenharmony_ci
52462306a36Sopenharmony_ci	/* Then, starting from right side, fill in with data */
52562306a36Sopenharmony_ci	for (i = 0; i < len; i++) {
52662306a36Sopenharmony_ci		buf[len - i - 1] = (val >> (8 * i)) & 0xff;
52762306a36Sopenharmony_ci		if (i >= 3)
52862306a36Sopenharmony_ci			break;  /* Only handle up to 32 bits of 'val' */
52962306a36Sopenharmony_ci	}
53062306a36Sopenharmony_ci}
531