1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * CAAM Error Reporting
4  *
5  * Copyright 2009-2014 Freescale Semiconductor, Inc.
6  *
7  * Derived from error.c file in linux drivers/crypto/caam
8  */
9 
10 #include <common.h>
11 #include <log.h>
12 #include <malloc.h>
13 #include "desc.h"
14 #include "jr.h"
15 
16 #define CAAM_ERROR_STR_MAX 302
17 
18 #define JRSTA_SSRC_SHIFT            28
19 #define JRSTA_CCBERR_CHAID_MASK     0x00f0
20 #define JRSTA_CCBERR_CHAID_SHIFT    4
21 #define JRSTA_CCBERR_ERRID_MASK     0x000
22 #define JRSTA_CCBERR_CHAID_RNG      (0x05 << JRSTA_CCBERR_CHAID_SHIFT)
23 
24 #define JRSTA_DECOERR_JUMP          0x08000000
25 #define JRSTA_DECOERR_INDEX_SHIFT   8
26 #define JRSTA_DECOERR_INDEX_MASK    0xff00
27 #define JRSTA_DECOERR_ERROR_MASK    0x00ff
28 
29 
30 static const struct {
31 	u8 value;
32 	const char *error_text;
33 } desc_error_list[] = {
34 	{ 0x00, "No error." },
35 	{ 0x01, "SGT Length Error. The descriptor is trying to read" \
36 		" more data than is contained in the SGT table." },
37 	{ 0x02, "SGT Null Entry Error." },
38 	{ 0x03, "Job Ring Control Error. Bad value in Job Ring Control reg." },
39 	{ 0x04, "Invalid Descriptor Command." },
40 	{ 0x05, "Reserved." },
41 	{ 0x06, "Invalid KEY Command" },
42 	{ 0x07, "Invalid LOAD Command" },
43 	{ 0x08, "Invalid STORE Command" },
44 	{ 0x09, "Invalid OPERATION Command" },
45 	{ 0x0A, "Invalid FIFO LOAD Command" },
46 	{ 0x0B, "Invalid FIFO STORE Command" },
47 	{ 0x0C, "Invalid MOVE/MOVE_LEN Command" },
48 	{ 0x0D, "Invalid JUMP Command" },
49 	{ 0x0E, "Invalid MATH Command" },
50 	{ 0x0F, "Invalid SIGNATURE Command" },
51 	{ 0x10, "Invalid Sequence Command" },
52 	{ 0x11, "Skip data type invalid. The type must be 0xE or 0xF."},
53 	{ 0x12, "Shared Descriptor Header Error" },
54 	{ 0x13, "Header Error. Invalid length or parity, or other problems." },
55 	{ 0x14, "Burster Error. Burster has gotten to an illegal state" },
56 	{ 0x15, "Context Register Length Error" },
57 	{ 0x16, "DMA Error" },
58 	{ 0x17, "Reserved." },
59 	{ 0x1A, "Job failed due to JR reset" },
60 	{ 0x1B, "Job failed due to Fail Mode" },
61 	{ 0x1C, "DECO Watchdog timer timeout error" },
62 	{ 0x1D, "DECO tried to copy a key from another DECO but" \
63 		" the other DECO's Key Registers were locked" },
64 	{ 0x1E, "DECO attempted to copy data from a DECO" \
65 		"that had an unmasked Descriptor error" },
66 	{ 0x1F, "LIODN error" },
67 	{ 0x20, "DECO has completed a reset initiated via the DRR register" },
68 	{ 0x21, "Nonce error" },
69 	{ 0x22, "Meta data is too large (> 511 bytes) for TLS decap" },
70 	{ 0x23, "Read Input Frame error" },
71 	{ 0x24, "JDKEK, TDKEK or TDSK not loaded error" },
72 	{ 0x80, "DNR (do not run) error" },
73 	{ 0x81, "undefined protocol command" },
74 	{ 0x82, "invalid setting in PDB" },
75 	{ 0x83, "Anti-replay LATE error" },
76 	{ 0x84, "Anti-replay REPLAY error" },
77 	{ 0x85, "Sequence number overflow" },
78 	{ 0x86, "Sigver invalid signature" },
79 	{ 0x87, "DSA Sign Illegal test descriptor" },
80 	{ 0x88, "Protocol Format Error" },
81 	{ 0x89, "Protocol Size Error" },
82 	{ 0xC1, "Blob Command error: Undefined mode" },
83 	{ 0xC2, "Blob Command error: Secure Memory Blob mode error" },
84 	{ 0xC4, "Blob Command error: Black Blob key or input size error" },
85 	{ 0xC5, "Blob Command error: Invalid key destination" },
86 	{ 0xC8, "Blob Command error: Trusted/Secure mode error" },
87 	{ 0xF0, "IPsec TTL or hop limit field is 0, or was decremented to 0" },
88 	{ 0xF1, "3GPP HFN matches or exceeds the Threshold" },
89 };
90 
91 static const char * const cha_id_list[] = {
92 	"",
93 	"AES",
94 	"DES",
95 	"ARC4",
96 	"MDHA",
97 	"RNG",
98 	"SNOW f8",
99 	"Kasumi f8/9",
100 	"PKHA",
101 	"CRCA",
102 	"SNOW f9",
103 	"ZUCE",
104 	"ZUCA",
105 };
106 
107 static const char * const err_id_list[] = {
108 	"No error.",
109 	"Mode error.",
110 	"Data size error.",
111 	"Key size error.",
112 	"PKHA A memory size error.",
113 	"PKHA B memory size error.",
114 	"Data arrived out of sequence error.",
115 	"PKHA divide-by-zero error.",
116 	"PKHA modulus even error.",
117 	"DES key parity error.",
118 	"ICV check failed.",
119 	"Hardware error.",
120 	"Unsupported CCM AAD size.",
121 	"Class 1 CHA is not reset",
122 	"Invalid CHA combination was selected",
123 	"Invalid CHA selected.",
124 };
125 
126 static const char * const rng_err_id_list[] = {
127 	"",
128 	"",
129 	"",
130 	"Instantiate",
131 	"Not instantiated",
132 	"Test instantiate",
133 	"Prediction resistance",
134 	"Prediction resistance and test request",
135 	"Uninstantiate",
136 	"Secure key generation",
137 };
138 
report_ccb_status(const u32 status,const char * error)139 static void report_ccb_status(const u32 status,
140 			      const char *error)
141 {
142 	u8 cha_id = (status & JRSTA_CCBERR_CHAID_MASK) >>
143 		    JRSTA_CCBERR_CHAID_SHIFT;
144 	u8 err_id = status & JRSTA_CCBERR_ERRID_MASK;
145 	u8 idx = (status & JRSTA_DECOERR_INDEX_MASK) >>
146 		  JRSTA_DECOERR_INDEX_SHIFT;
147 	char *idx_str;
148 	const char *cha_str = "unidentified cha_id value 0x";
149 	char cha_err_code[3] = { 0 };
150 	const char *err_str = "unidentified err_id value 0x";
151 	char err_err_code[3] = { 0 };
152 
153 	if (status & JRSTA_DECOERR_JUMP)
154 		idx_str = "jump tgt desc idx";
155 	else
156 		idx_str = "desc idx";
157 
158 	if (cha_id < ARRAY_SIZE(cha_id_list))
159 		cha_str = cha_id_list[cha_id];
160 	else
161 		snprintf(cha_err_code, sizeof(cha_err_code), "%02x", cha_id);
162 
163 	if ((cha_id << JRSTA_CCBERR_CHAID_SHIFT) == JRSTA_CCBERR_CHAID_RNG &&
164 	    err_id < ARRAY_SIZE(rng_err_id_list) &&
165 	    strlen(rng_err_id_list[err_id])) {
166 		/* RNG-only error */
167 		err_str = rng_err_id_list[err_id];
168 	} else if (err_id < ARRAY_SIZE(err_id_list)) {
169 		err_str = err_id_list[err_id];
170 	} else {
171 		snprintf(err_err_code, sizeof(err_err_code), "%02x", err_id);
172 	}
173 
174 	debug("%08x: %s: %s %d: %s%s: %s%s\n",
175 	       status, error, idx_str, idx,
176 		cha_str, cha_err_code,
177 		err_str, err_err_code);
178 }
179 
report_jump_status(const u32 status,const char * error)180 static void report_jump_status(const u32 status,
181 			       const char *error)
182 {
183 	debug("%08x: %s: %s() not implemented\n",
184 	       status, error, __func__);
185 }
186 
report_deco_status(const u32 status,const char * error)187 static void report_deco_status(const u32 status,
188 			       const char *error)
189 {
190 	u8 err_id = status & JRSTA_DECOERR_ERROR_MASK;
191 	u8 idx = (status & JRSTA_DECOERR_INDEX_MASK) >>
192 		  JRSTA_DECOERR_INDEX_SHIFT;
193 	char *idx_str;
194 	const char *err_str = "unidentified error value 0x";
195 	char err_err_code[3] = { 0 };
196 	int i;
197 
198 	if (status & JRSTA_DECOERR_JUMP)
199 		idx_str = "jump tgt desc idx";
200 	else
201 		idx_str = "desc idx";
202 
203 	for (i = 0; i < ARRAY_SIZE(desc_error_list); i++)
204 		if (desc_error_list[i].value == err_id)
205 			break;
206 
207 	if (i != ARRAY_SIZE(desc_error_list) && desc_error_list[i].error_text)
208 		err_str = desc_error_list[i].error_text;
209 	else
210 		snprintf(err_err_code, sizeof(err_err_code), "%02x", err_id);
211 
212 	debug("%08x: %s: %s %d: %s%s\n",
213 	       status, error, idx_str, idx, err_str, err_err_code);
214 }
215 
report_jr_status(const u32 status,const char * error)216 static void report_jr_status(const u32 status,
217 			     const char *error)
218 {
219 	debug("%08x: %s: %s() not implemented\n",
220 	       status, error, __func__);
221 }
222 
report_cond_code_status(const u32 status,const char * error)223 static void report_cond_code_status(const u32 status,
224 				    const char *error)
225 {
226 	debug("%08x: %s: %s() not implemented\n",
227 	       status, error, __func__);
228 }
229 
caam_jr_strstatus(u32 status)230 void caam_jr_strstatus(u32 status)
231 {
232 	static const struct stat_src {
233 		void (*report_ssed)(const u32 status,
234 				    const char *error);
235 		const char *error;
236 	} status_src[] = {
237 		{ NULL, "No error" },
238 		{ NULL, NULL },
239 		{ report_ccb_status, "CCB" },
240 		{ report_jump_status, "Jump" },
241 		{ report_deco_status, "DECO" },
242 		{ NULL, NULL },
243 		{ report_jr_status, "Job Ring" },
244 		{ report_cond_code_status, "Condition Code" },
245 	};
246 	u32 ssrc = status >> JRSTA_SSRC_SHIFT;
247 	const char *error = status_src[ssrc].error;
248 
249 	/*
250 	 * If there is no further error handling function, just
251 	 * print the error code, error string and exit. Otherwise
252 	 * call the handler function.
253 	 */
254 	if (!status_src[ssrc].report_ssed)
255 		debug("%08x: %s:\n", status, status_src[ssrc].error);
256 	else
257 		status_src[ssrc].report_ssed(status, error);
258 }
259