162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * CAAM Error Reporting
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright 2009-2011 Freescale Semiconductor, Inc.
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include "compat.h"
962306a36Sopenharmony_ci#include "regs.h"
1062306a36Sopenharmony_ci#include "desc.h"
1162306a36Sopenharmony_ci#include "error.h"
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#ifdef DEBUG
1462306a36Sopenharmony_ci#include <linux/highmem.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_civoid caam_dump_sg(const char *prefix_str, int prefix_type,
1762306a36Sopenharmony_ci		  int rowsize, int groupsize, struct scatterlist *sg,
1862306a36Sopenharmony_ci		  size_t tlen, bool ascii)
1962306a36Sopenharmony_ci{
2062306a36Sopenharmony_ci	struct scatterlist *it;
2162306a36Sopenharmony_ci	void *it_page;
2262306a36Sopenharmony_ci	size_t len;
2362306a36Sopenharmony_ci	void *buf;
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci	for (it = sg; it && tlen > 0 ; it = sg_next(it)) {
2662306a36Sopenharmony_ci		/*
2762306a36Sopenharmony_ci		 * make sure the scatterlist's page
2862306a36Sopenharmony_ci		 * has a valid virtual memory mapping
2962306a36Sopenharmony_ci		 */
3062306a36Sopenharmony_ci		it_page = kmap_atomic(sg_page(it));
3162306a36Sopenharmony_ci		if (unlikely(!it_page)) {
3262306a36Sopenharmony_ci			pr_err("caam_dump_sg: kmap failed\n");
3362306a36Sopenharmony_ci			return;
3462306a36Sopenharmony_ci		}
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci		buf = it_page + it->offset;
3762306a36Sopenharmony_ci		len = min_t(size_t, tlen, it->length);
3862306a36Sopenharmony_ci		print_hex_dump_debug(prefix_str, prefix_type, rowsize,
3962306a36Sopenharmony_ci				     groupsize, buf, len, ascii);
4062306a36Sopenharmony_ci		tlen -= len;
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci		kunmap_atomic(it_page);
4362306a36Sopenharmony_ci	}
4462306a36Sopenharmony_ci}
4562306a36Sopenharmony_ci#else
4662306a36Sopenharmony_civoid caam_dump_sg(const char *prefix_str, int prefix_type,
4762306a36Sopenharmony_ci		  int rowsize, int groupsize, struct scatterlist *sg,
4862306a36Sopenharmony_ci		  size_t tlen, bool ascii)
4962306a36Sopenharmony_ci{}
5062306a36Sopenharmony_ci#endif /* DEBUG */
5162306a36Sopenharmony_ciEXPORT_SYMBOL(caam_dump_sg);
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_cibool caam_little_end;
5462306a36Sopenharmony_ciEXPORT_SYMBOL(caam_little_end);
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_cibool caam_imx;
5762306a36Sopenharmony_ciEXPORT_SYMBOL(caam_imx);
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_cisize_t caam_ptr_sz;
6062306a36Sopenharmony_ciEXPORT_SYMBOL(caam_ptr_sz);
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_cistatic const struct {
6362306a36Sopenharmony_ci	u8 value;
6462306a36Sopenharmony_ci	const char *error_text;
6562306a36Sopenharmony_ci} desc_error_list[] = {
6662306a36Sopenharmony_ci	{ 0x00, "No error." },
6762306a36Sopenharmony_ci	{ 0x01, "SGT Length Error. The descriptor is trying to read more data than is contained in the SGT table." },
6862306a36Sopenharmony_ci	{ 0x02, "SGT Null Entry Error." },
6962306a36Sopenharmony_ci	{ 0x03, "Job Ring Control Error. There is a bad value in the Job Ring Control register." },
7062306a36Sopenharmony_ci	{ 0x04, "Invalid Descriptor Command. The Descriptor Command field is invalid." },
7162306a36Sopenharmony_ci	{ 0x05, "Reserved." },
7262306a36Sopenharmony_ci	{ 0x06, "Invalid KEY Command" },
7362306a36Sopenharmony_ci	{ 0x07, "Invalid LOAD Command" },
7462306a36Sopenharmony_ci	{ 0x08, "Invalid STORE Command" },
7562306a36Sopenharmony_ci	{ 0x09, "Invalid OPERATION Command" },
7662306a36Sopenharmony_ci	{ 0x0A, "Invalid FIFO LOAD Command" },
7762306a36Sopenharmony_ci	{ 0x0B, "Invalid FIFO STORE Command" },
7862306a36Sopenharmony_ci	{ 0x0C, "Invalid MOVE/MOVE_LEN Command" },
7962306a36Sopenharmony_ci	{ 0x0D, "Invalid JUMP Command. A nonlocal JUMP Command is invalid because the target is not a Job Header Command, or the jump is from a Trusted Descriptor to a Job Descriptor, or because the target Descriptor contains a Shared Descriptor." },
8062306a36Sopenharmony_ci	{ 0x0E, "Invalid MATH Command" },
8162306a36Sopenharmony_ci	{ 0x0F, "Invalid SIGNATURE Command" },
8262306a36Sopenharmony_ci	{ 0x10, "Invalid Sequence Command. A SEQ IN PTR OR SEQ OUT PTR Command is invalid or a SEQ KEY, SEQ LOAD, SEQ FIFO LOAD, or SEQ FIFO STORE decremented the input or output sequence length below 0. This error may result if a built-in PROTOCOL Command has encountered a malformed PDU." },
8362306a36Sopenharmony_ci	{ 0x11, "Skip data type invalid. The type must be 0xE or 0xF."},
8462306a36Sopenharmony_ci	{ 0x12, "Shared Descriptor Header Error" },
8562306a36Sopenharmony_ci	{ 0x13, "Header Error. Invalid length or parity, or certain other problems." },
8662306a36Sopenharmony_ci	{ 0x14, "Burster Error. Burster has gotten to an illegal state" },
8762306a36Sopenharmony_ci	{ 0x15, "Context Register Length Error. The descriptor is trying to read or write past the end of the Context Register. A SEQ LOAD or SEQ STORE with the VLF bit set was executed with too large a length in the variable length register (VSOL for SEQ STORE or VSIL for SEQ LOAD)." },
8862306a36Sopenharmony_ci	{ 0x16, "DMA Error" },
8962306a36Sopenharmony_ci	{ 0x17, "Reserved." },
9062306a36Sopenharmony_ci	{ 0x1A, "Job failed due to JR reset" },
9162306a36Sopenharmony_ci	{ 0x1B, "Job failed due to Fail Mode" },
9262306a36Sopenharmony_ci	{ 0x1C, "DECO Watchdog timer timeout error" },
9362306a36Sopenharmony_ci	{ 0x1D, "DECO tried to copy a key from another DECO but the other DECO's Key Registers were locked" },
9462306a36Sopenharmony_ci	{ 0x1E, "DECO attempted to copy data from a DECO that had an unmasked Descriptor error" },
9562306a36Sopenharmony_ci	{ 0x1F, "LIODN error. DECO was trying to share from itself or from another DECO but the two Non-SEQ LIODN values didn't match or the 'shared from' DECO's Descriptor required that the SEQ LIODNs be the same and they aren't." },
9662306a36Sopenharmony_ci	{ 0x20, "DECO has completed a reset initiated via the DRR register" },
9762306a36Sopenharmony_ci	{ 0x21, "Nonce error. When using EKT (CCM) key encryption option in the FIFO STORE Command, the Nonce counter reached its maximum value and this encryption mode can no longer be used." },
9862306a36Sopenharmony_ci	{ 0x22, "Meta data is too large (> 511 bytes) for TLS decap (input frame; block ciphers) and IPsec decap (output frame, when doing the next header byte update) and DCRC (output frame)." },
9962306a36Sopenharmony_ci	{ 0x23, "Read Input Frame error" },
10062306a36Sopenharmony_ci	{ 0x24, "JDKEK, TDKEK or TDSK not loaded error" },
10162306a36Sopenharmony_ci	{ 0x80, "DNR (do not run) error" },
10262306a36Sopenharmony_ci	{ 0x81, "undefined protocol command" },
10362306a36Sopenharmony_ci	{ 0x82, "invalid setting in PDB" },
10462306a36Sopenharmony_ci	{ 0x83, "Anti-replay LATE error" },
10562306a36Sopenharmony_ci	{ 0x84, "Anti-replay REPLAY error" },
10662306a36Sopenharmony_ci	{ 0x85, "Sequence number overflow" },
10762306a36Sopenharmony_ci	{ 0x86, "Sigver invalid signature" },
10862306a36Sopenharmony_ci	{ 0x87, "DSA Sign Illegal test descriptor" },
10962306a36Sopenharmony_ci	{ 0x88, "Protocol Format Error - A protocol has seen an error in the format of data received. When running RSA, this means that formatting with random padding was used, and did not follow the form: 0x00, 0x02, 8-to-N bytes of non-zero pad, 0x00, F data." },
11062306a36Sopenharmony_ci	{ 0x89, "Protocol Size Error - A protocol has seen an error in size. When running RSA, pdb size N < (size of F) when no formatting is used; or pdb size N < (F + 11) when formatting is used." },
11162306a36Sopenharmony_ci	{ 0xC1, "Blob Command error: Undefined mode" },
11262306a36Sopenharmony_ci	{ 0xC2, "Blob Command error: Secure Memory Blob mode error" },
11362306a36Sopenharmony_ci	{ 0xC4, "Blob Command error: Black Blob key or input size error" },
11462306a36Sopenharmony_ci	{ 0xC5, "Blob Command error: Invalid key destination" },
11562306a36Sopenharmony_ci	{ 0xC8, "Blob Command error: Trusted/Secure mode error" },
11662306a36Sopenharmony_ci	{ 0xF0, "IPsec TTL or hop limit field either came in as 0, or was decremented to 0" },
11762306a36Sopenharmony_ci	{ 0xF1, "3GPP HFN matches or exceeds the Threshold" },
11862306a36Sopenharmony_ci};
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_cistatic const struct {
12162306a36Sopenharmony_ci	u8 value;
12262306a36Sopenharmony_ci	const char *error_text;
12362306a36Sopenharmony_ci} qi_error_list[] = {
12462306a36Sopenharmony_ci	{ 0x00, "No error" },
12562306a36Sopenharmony_ci	{ 0x1F, "Job terminated by FQ or ICID flush" },
12662306a36Sopenharmony_ci	{ 0x20, "FD format error"},
12762306a36Sopenharmony_ci	{ 0x21, "FD command format error"},
12862306a36Sopenharmony_ci	{ 0x23, "FL format error"},
12962306a36Sopenharmony_ci	{ 0x25, "CRJD specified in FD, but not enabled in FLC"},
13062306a36Sopenharmony_ci	{ 0x30, "Max. buffer size too small"},
13162306a36Sopenharmony_ci	{ 0x31, "DHR exceeds max. buffer size (allocate mode, S/G format)"},
13262306a36Sopenharmony_ci	{ 0x32, "SGT exceeds max. buffer size (allocate mode, S/G format"},
13362306a36Sopenharmony_ci	{ 0x33, "Size over/underflow (allocate mode)"},
13462306a36Sopenharmony_ci	{ 0x34, "Size over/underflow (reuse mode)"},
13562306a36Sopenharmony_ci	{ 0x35, "Length exceeds max. short length (allocate mode, S/G/ format)"},
13662306a36Sopenharmony_ci	{ 0x36, "Memory footprint exceeds max. value (allocate mode, S/G/ format)"},
13762306a36Sopenharmony_ci	{ 0x41, "SBC frame format not supported (allocate mode)"},
13862306a36Sopenharmony_ci	{ 0x42, "Pool 0 invalid / pool 1 size < pool 0 size (allocate mode)"},
13962306a36Sopenharmony_ci	{ 0x43, "Annotation output enabled but ASAR = 0 (allocate mode)"},
14062306a36Sopenharmony_ci	{ 0x44, "Unsupported or reserved frame format or SGHR = 1 (reuse mode)"},
14162306a36Sopenharmony_ci	{ 0x45, "DHR correction underflow (reuse mode, single buffer format)"},
14262306a36Sopenharmony_ci	{ 0x46, "Annotation length exceeds offset (reuse mode)"},
14362306a36Sopenharmony_ci	{ 0x48, "Annotation output enabled but ASA limited by ASAR (reuse mode)"},
14462306a36Sopenharmony_ci	{ 0x49, "Data offset correction exceeds input frame data length (reuse mode)"},
14562306a36Sopenharmony_ci	{ 0x4B, "Annotation output enabled but ASA cannot be expanded (frame list)"},
14662306a36Sopenharmony_ci	{ 0x51, "Unsupported IF reuse mode"},
14762306a36Sopenharmony_ci	{ 0x52, "Unsupported FL use mode"},
14862306a36Sopenharmony_ci	{ 0x53, "Unsupported RJD use mode"},
14962306a36Sopenharmony_ci	{ 0x54, "Unsupported inline descriptor use mode"},
15062306a36Sopenharmony_ci	{ 0xC0, "Table buffer pool 0 depletion"},
15162306a36Sopenharmony_ci	{ 0xC1, "Table buffer pool 1 depletion"},
15262306a36Sopenharmony_ci	{ 0xC2, "Data buffer pool 0 depletion, no OF allocated"},
15362306a36Sopenharmony_ci	{ 0xC3, "Data buffer pool 1 depletion, no OF allocated"},
15462306a36Sopenharmony_ci	{ 0xC4, "Data buffer pool 0 depletion, partial OF allocated"},
15562306a36Sopenharmony_ci	{ 0xC5, "Data buffer pool 1 depletion, partial OF allocated"},
15662306a36Sopenharmony_ci	{ 0xD0, "FLC read error"},
15762306a36Sopenharmony_ci	{ 0xD1, "FL read error"},
15862306a36Sopenharmony_ci	{ 0xD2, "FL write error"},
15962306a36Sopenharmony_ci	{ 0xD3, "OF SGT write error"},
16062306a36Sopenharmony_ci	{ 0xD4, "PTA read error"},
16162306a36Sopenharmony_ci	{ 0xD5, "PTA write error"},
16262306a36Sopenharmony_ci	{ 0xD6, "OF SGT F-bit write error"},
16362306a36Sopenharmony_ci	{ 0xD7, "ASA write error"},
16462306a36Sopenharmony_ci	{ 0xE1, "FLC[ICR]=0 ICID error"},
16562306a36Sopenharmony_ci	{ 0xE2, "FLC[ICR]=1 ICID error"},
16662306a36Sopenharmony_ci	{ 0xE4, "source of ICID flush not trusted (BDI = 0)"},
16762306a36Sopenharmony_ci};
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_cistatic const char * const cha_id_list[] = {
17062306a36Sopenharmony_ci	"",
17162306a36Sopenharmony_ci	"AES",
17262306a36Sopenharmony_ci	"DES",
17362306a36Sopenharmony_ci	"ARC4",
17462306a36Sopenharmony_ci	"MDHA",
17562306a36Sopenharmony_ci	"RNG",
17662306a36Sopenharmony_ci	"SNOW f8",
17762306a36Sopenharmony_ci	"Kasumi f8/9",
17862306a36Sopenharmony_ci	"PKHA",
17962306a36Sopenharmony_ci	"CRCA",
18062306a36Sopenharmony_ci	"SNOW f9",
18162306a36Sopenharmony_ci	"ZUCE",
18262306a36Sopenharmony_ci	"ZUCA",
18362306a36Sopenharmony_ci};
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_cistatic const char * const err_id_list[] = {
18662306a36Sopenharmony_ci	"No error.",
18762306a36Sopenharmony_ci	"Mode error.",
18862306a36Sopenharmony_ci	"Data size error.",
18962306a36Sopenharmony_ci	"Key size error.",
19062306a36Sopenharmony_ci	"PKHA A memory size error.",
19162306a36Sopenharmony_ci	"PKHA B memory size error.",
19262306a36Sopenharmony_ci	"Data arrived out of sequence error.",
19362306a36Sopenharmony_ci	"PKHA divide-by-zero error.",
19462306a36Sopenharmony_ci	"PKHA modulus even error.",
19562306a36Sopenharmony_ci	"DES key parity error.",
19662306a36Sopenharmony_ci	"ICV check failed.",
19762306a36Sopenharmony_ci	"Hardware error.",
19862306a36Sopenharmony_ci	"Unsupported CCM AAD size.",
19962306a36Sopenharmony_ci	"Class 1 CHA is not reset",
20062306a36Sopenharmony_ci	"Invalid CHA combination was selected",
20162306a36Sopenharmony_ci	"Invalid CHA selected.",
20262306a36Sopenharmony_ci};
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_cistatic const char * const rng_err_id_list[] = {
20562306a36Sopenharmony_ci	"",
20662306a36Sopenharmony_ci	"",
20762306a36Sopenharmony_ci	"",
20862306a36Sopenharmony_ci	"Instantiate",
20962306a36Sopenharmony_ci	"Not instantiated",
21062306a36Sopenharmony_ci	"Test instantiate",
21162306a36Sopenharmony_ci	"Prediction resistance",
21262306a36Sopenharmony_ci	"Prediction resistance and test request",
21362306a36Sopenharmony_ci	"Uninstantiate",
21462306a36Sopenharmony_ci	"Secure key generation",
21562306a36Sopenharmony_ci	"",
21662306a36Sopenharmony_ci	"Hardware error",
21762306a36Sopenharmony_ci	"Continuous check"
21862306a36Sopenharmony_ci};
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_cistatic int report_ccb_status(struct device *jrdev, const u32 status,
22162306a36Sopenharmony_ci			     const char *error)
22262306a36Sopenharmony_ci{
22362306a36Sopenharmony_ci	u8 cha_id = (status & JRSTA_CCBERR_CHAID_MASK) >>
22462306a36Sopenharmony_ci		    JRSTA_CCBERR_CHAID_SHIFT;
22562306a36Sopenharmony_ci	u8 err_id = status & JRSTA_CCBERR_ERRID_MASK;
22662306a36Sopenharmony_ci	u8 idx = (status & JRSTA_DECOERR_INDEX_MASK) >>
22762306a36Sopenharmony_ci		  JRSTA_DECOERR_INDEX_SHIFT;
22862306a36Sopenharmony_ci	char *idx_str;
22962306a36Sopenharmony_ci	const char *cha_str = "unidentified cha_id value 0x";
23062306a36Sopenharmony_ci	char cha_err_code[3] = { 0 };
23162306a36Sopenharmony_ci	const char *err_str = "unidentified err_id value 0x";
23262306a36Sopenharmony_ci	char err_err_code[3] = { 0 };
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci	if (status & JRSTA_DECOERR_JUMP)
23562306a36Sopenharmony_ci		idx_str = "jump tgt desc idx";
23662306a36Sopenharmony_ci	else
23762306a36Sopenharmony_ci		idx_str = "desc idx";
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	if (cha_id < ARRAY_SIZE(cha_id_list))
24062306a36Sopenharmony_ci		cha_str = cha_id_list[cha_id];
24162306a36Sopenharmony_ci	else
24262306a36Sopenharmony_ci		snprintf(cha_err_code, sizeof(cha_err_code), "%02x", cha_id);
24362306a36Sopenharmony_ci
24462306a36Sopenharmony_ci	if ((cha_id << JRSTA_CCBERR_CHAID_SHIFT) == JRSTA_CCBERR_CHAID_RNG &&
24562306a36Sopenharmony_ci	    err_id < ARRAY_SIZE(rng_err_id_list) &&
24662306a36Sopenharmony_ci	    strlen(rng_err_id_list[err_id])) {
24762306a36Sopenharmony_ci		/* RNG-only error */
24862306a36Sopenharmony_ci		err_str = rng_err_id_list[err_id];
24962306a36Sopenharmony_ci	} else {
25062306a36Sopenharmony_ci		err_str = err_id_list[err_id];
25162306a36Sopenharmony_ci	}
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci	/*
25462306a36Sopenharmony_ci	 * CCB ICV check failures are part of normal operation life;
25562306a36Sopenharmony_ci	 * we leave the upper layers to do what they want with them.
25662306a36Sopenharmony_ci	 */
25762306a36Sopenharmony_ci	if (err_id == JRSTA_CCBERR_ERRID_ICVCHK)
25862306a36Sopenharmony_ci		return -EBADMSG;
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci	dev_err_ratelimited(jrdev, "%08x: %s: %s %d: %s%s: %s%s\n", status,
26162306a36Sopenharmony_ci			    error, idx_str, idx, cha_str, cha_err_code,
26262306a36Sopenharmony_ci			    err_str, err_err_code);
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci	return -EINVAL;
26562306a36Sopenharmony_ci}
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_cistatic int report_jump_status(struct device *jrdev, const u32 status,
26862306a36Sopenharmony_ci			      const char *error)
26962306a36Sopenharmony_ci{
27062306a36Sopenharmony_ci	dev_err(jrdev, "%08x: %s: %s() not implemented\n",
27162306a36Sopenharmony_ci		status, error, __func__);
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci	return -EINVAL;
27462306a36Sopenharmony_ci}
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_cistatic int report_deco_status(struct device *jrdev, const u32 status,
27762306a36Sopenharmony_ci			      const char *error)
27862306a36Sopenharmony_ci{
27962306a36Sopenharmony_ci	u8 err_id = status & JRSTA_DECOERR_ERROR_MASK;
28062306a36Sopenharmony_ci	u8 idx = (status & JRSTA_DECOERR_INDEX_MASK) >>
28162306a36Sopenharmony_ci		  JRSTA_DECOERR_INDEX_SHIFT;
28262306a36Sopenharmony_ci	char *idx_str;
28362306a36Sopenharmony_ci	const char *err_str = "unidentified error value 0x";
28462306a36Sopenharmony_ci	char err_err_code[3] = { 0 };
28562306a36Sopenharmony_ci	int i;
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci	if (status & JRSTA_DECOERR_JUMP)
28862306a36Sopenharmony_ci		idx_str = "jump tgt desc idx";
28962306a36Sopenharmony_ci	else
29062306a36Sopenharmony_ci		idx_str = "desc idx";
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(desc_error_list); i++)
29362306a36Sopenharmony_ci		if (desc_error_list[i].value == err_id)
29462306a36Sopenharmony_ci			break;
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ci	if (i != ARRAY_SIZE(desc_error_list) && desc_error_list[i].error_text)
29762306a36Sopenharmony_ci		err_str = desc_error_list[i].error_text;
29862306a36Sopenharmony_ci	else
29962306a36Sopenharmony_ci		snprintf(err_err_code, sizeof(err_err_code), "%02x", err_id);
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ci	dev_err(jrdev, "%08x: %s: %s %d: %s%s\n",
30262306a36Sopenharmony_ci		status, error, idx_str, idx, err_str, err_err_code);
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci	return -EINVAL;
30562306a36Sopenharmony_ci}
30662306a36Sopenharmony_ci
30762306a36Sopenharmony_cistatic int report_qi_status(struct device *qidev, const u32 status,
30862306a36Sopenharmony_ci			    const char *error)
30962306a36Sopenharmony_ci{
31062306a36Sopenharmony_ci	u8 err_id = status & JRSTA_QIERR_ERROR_MASK;
31162306a36Sopenharmony_ci	const char *err_str = "unidentified error value 0x";
31262306a36Sopenharmony_ci	char err_err_code[3] = { 0 };
31362306a36Sopenharmony_ci	int i;
31462306a36Sopenharmony_ci
31562306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(qi_error_list); i++)
31662306a36Sopenharmony_ci		if (qi_error_list[i].value == err_id)
31762306a36Sopenharmony_ci			break;
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ci	if (i != ARRAY_SIZE(qi_error_list) && qi_error_list[i].error_text)
32062306a36Sopenharmony_ci		err_str = qi_error_list[i].error_text;
32162306a36Sopenharmony_ci	else
32262306a36Sopenharmony_ci		snprintf(err_err_code, sizeof(err_err_code), "%02x", err_id);
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ci	dev_err(qidev, "%08x: %s: %s%s\n",
32562306a36Sopenharmony_ci		status, error, err_str, err_err_code);
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci	return -EINVAL;
32862306a36Sopenharmony_ci}
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_cistatic int report_jr_status(struct device *jrdev, const u32 status,
33162306a36Sopenharmony_ci			    const char *error)
33262306a36Sopenharmony_ci{
33362306a36Sopenharmony_ci	dev_err(jrdev, "%08x: %s: %s() not implemented\n",
33462306a36Sopenharmony_ci		status, error, __func__);
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci	return -EINVAL;
33762306a36Sopenharmony_ci}
33862306a36Sopenharmony_ci
33962306a36Sopenharmony_cistatic int report_cond_code_status(struct device *jrdev, const u32 status,
34062306a36Sopenharmony_ci				   const char *error)
34162306a36Sopenharmony_ci{
34262306a36Sopenharmony_ci	dev_err(jrdev, "%08x: %s: %s() not implemented\n",
34362306a36Sopenharmony_ci		status, error, __func__);
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_ci	return -EINVAL;
34662306a36Sopenharmony_ci}
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ciint caam_strstatus(struct device *jrdev, u32 status, bool qi_v2)
34962306a36Sopenharmony_ci{
35062306a36Sopenharmony_ci	static const struct stat_src {
35162306a36Sopenharmony_ci		int (*report_ssed)(struct device *jrdev, const u32 status,
35262306a36Sopenharmony_ci				   const char *error);
35362306a36Sopenharmony_ci		const char *error;
35462306a36Sopenharmony_ci	} status_src[16] = {
35562306a36Sopenharmony_ci		{ NULL, "No error" },
35662306a36Sopenharmony_ci		{ NULL, NULL },
35762306a36Sopenharmony_ci		{ report_ccb_status, "CCB" },
35862306a36Sopenharmony_ci		{ report_jump_status, "Jump" },
35962306a36Sopenharmony_ci		{ report_deco_status, "DECO" },
36062306a36Sopenharmony_ci		{ report_qi_status, "Queue Manager Interface" },
36162306a36Sopenharmony_ci		{ report_jr_status, "Job Ring" },
36262306a36Sopenharmony_ci		{ report_cond_code_status, "Condition Code" },
36362306a36Sopenharmony_ci		{ NULL, NULL },
36462306a36Sopenharmony_ci		{ NULL, NULL },
36562306a36Sopenharmony_ci		{ NULL, NULL },
36662306a36Sopenharmony_ci		{ NULL, NULL },
36762306a36Sopenharmony_ci		{ NULL, NULL },
36862306a36Sopenharmony_ci		{ NULL, NULL },
36962306a36Sopenharmony_ci		{ NULL, NULL },
37062306a36Sopenharmony_ci		{ NULL, NULL },
37162306a36Sopenharmony_ci	};
37262306a36Sopenharmony_ci	u32 ssrc = status >> JRSTA_SSRC_SHIFT;
37362306a36Sopenharmony_ci	const char *error = status_src[ssrc].error;
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_ci	/*
37662306a36Sopenharmony_ci	 * If there is an error handling function, call it to report the error.
37762306a36Sopenharmony_ci	 * Otherwise print the error source name.
37862306a36Sopenharmony_ci	 */
37962306a36Sopenharmony_ci	if (status_src[ssrc].report_ssed)
38062306a36Sopenharmony_ci		return status_src[ssrc].report_ssed(jrdev, status, error);
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ci	if (error)
38362306a36Sopenharmony_ci		dev_err(jrdev, "%d: %s\n", ssrc, error);
38462306a36Sopenharmony_ci	else
38562306a36Sopenharmony_ci		dev_err(jrdev, "%d: unknown error source\n", ssrc);
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_ci	return -EINVAL;
38862306a36Sopenharmony_ci}
38962306a36Sopenharmony_ciEXPORT_SYMBOL(caam_strstatus);
39062306a36Sopenharmony_ci
39162306a36Sopenharmony_ciMODULE_LICENSE("GPL");
39262306a36Sopenharmony_ciMODULE_DESCRIPTION("FSL CAAM error reporting");
39362306a36Sopenharmony_ciMODULE_AUTHOR("Freescale Semiconductor");
394