Linux-libre 3.10.103-gnu
[librecmc/linux-libre.git] / drivers / crypto / caam / error.c
1 /*
2  * CAAM Error Reporting
3  *
4  * Copyright 2009-2011 Freescale Semiconductor, Inc.
5  */
6
7 #include "compat.h"
8 #include "regs.h"
9 #include "intern.h"
10 #include "desc.h"
11 #include "jr.h"
12 #include "error.h"
13
14 #define SPRINTFCAT(str, format, param, max_alloc)               \
15 {                                                               \
16         char *tmp;                                              \
17                                                                 \
18         tmp = kmalloc(sizeof(format) + max_alloc, GFP_ATOMIC);  \
19         if (likely(tmp)) {                                      \
20                 sprintf(tmp, format, param);                    \
21                 strcat(str, tmp);                               \
22                 kfree(tmp);                                     \
23         } else {                                                \
24                 strcat(str, "kmalloc failure in SPRINTFCAT");   \
25         }                                                       \
26 }
27
28 static void report_jump_idx(u32 status, char *outstr)
29 {
30         u8 idx = (status & JRSTA_DECOERR_INDEX_MASK) >>
31                   JRSTA_DECOERR_INDEX_SHIFT;
32
33         if (status & JRSTA_DECOERR_JUMP)
34                 strcat(outstr, "jump tgt desc idx ");
35         else
36                 strcat(outstr, "desc idx ");
37
38         SPRINTFCAT(outstr, "%d: ", idx, sizeof("255"));
39 }
40
41 static void report_ccb_status(u32 status, char *outstr)
42 {
43         static const char * const cha_id_list[] = {
44                 "",
45                 "AES",
46                 "DES",
47                 "ARC4",
48                 "MDHA",
49                 "RNG",
50                 "SNOW f8",
51                 "Kasumi f8/9",
52                 "PKHA",
53                 "CRCA",
54                 "SNOW f9",
55                 "ZUCE",
56                 "ZUCA",
57         };
58         static const char * const err_id_list[] = {
59                 "No error.",
60                 "Mode error.",
61                 "Data size error.",
62                 "Key size error.",
63                 "PKHA A memory size error.",
64                 "PKHA B memory size error.",
65                 "Data arrived out of sequence error.",
66                 "PKHA divide-by-zero error.",
67                 "PKHA modulus even error.",
68                 "DES key parity error.",
69                 "ICV check failed.",
70                 "Hardware error.",
71                 "Unsupported CCM AAD size.",
72                 "Class 1 CHA is not reset",
73                 "Invalid CHA combination was selected",
74                 "Invalid CHA selected.",
75         };
76         static const char * const rng_err_id_list[] = {
77                 "",
78                 "",
79                 "",
80                 "Instantiate",
81                 "Not instantiated",
82                 "Test instantiate",
83                 "Prediction resistance",
84                 "Prediction resistance and test request",
85                 "Uninstantiate",
86                 "Secure key generation",
87         };
88         u8 cha_id = (status & JRSTA_CCBERR_CHAID_MASK) >>
89                     JRSTA_CCBERR_CHAID_SHIFT;
90         u8 err_id = status & JRSTA_CCBERR_ERRID_MASK;
91
92         report_jump_idx(status, outstr);
93
94         if (cha_id < ARRAY_SIZE(cha_id_list)) {
95                 SPRINTFCAT(outstr, "%s: ", cha_id_list[cha_id],
96                            strlen(cha_id_list[cha_id]));
97         } else {
98                 SPRINTFCAT(outstr, "unidentified cha_id value 0x%02x: ",
99                            cha_id, sizeof("ff"));
100         }
101
102         if ((cha_id << JRSTA_CCBERR_CHAID_SHIFT) == JRSTA_CCBERR_CHAID_RNG &&
103             err_id < ARRAY_SIZE(rng_err_id_list) &&
104             strlen(rng_err_id_list[err_id])) {
105                 /* RNG-only error */
106                 SPRINTFCAT(outstr, "%s", rng_err_id_list[err_id],
107                            strlen(rng_err_id_list[err_id]));
108         } else if (err_id < ARRAY_SIZE(err_id_list)) {
109                 SPRINTFCAT(outstr, "%s", err_id_list[err_id],
110                            strlen(err_id_list[err_id]));
111         } else {
112                 SPRINTFCAT(outstr, "unidentified err_id value 0x%02x",
113                            err_id, sizeof("ff"));
114         }
115 }
116
117 static void report_jump_status(u32 status, char *outstr)
118 {
119         SPRINTFCAT(outstr, "%s() not implemented", __func__, sizeof(__func__));
120 }
121
122 static void report_deco_status(u32 status, char *outstr)
123 {
124         static const struct {
125                 u8 value;
126                 char *error_text;
127         } desc_error_list[] = {
128                 { 0x00, "No error." },
129                 { 0x01, "SGT Length Error. The descriptor is trying to read "
130                         "more data than is contained in the SGT table." },
131                 { 0x02, "SGT Null Entry Error." },
132                 { 0x03, "Job Ring Control Error. There is a bad value in the "
133                         "Job Ring Control register." },
134                 { 0x04, "Invalid Descriptor Command. The Descriptor Command "
135                         "field is invalid." },
136                 { 0x05, "Reserved." },
137                 { 0x06, "Invalid KEY Command" },
138                 { 0x07, "Invalid LOAD Command" },
139                 { 0x08, "Invalid STORE Command" },
140                 { 0x09, "Invalid OPERATION Command" },
141                 { 0x0A, "Invalid FIFO LOAD Command" },
142                 { 0x0B, "Invalid FIFO STORE Command" },
143                 { 0x0C, "Invalid MOVE/MOVE_LEN Command" },
144                 { 0x0D, "Invalid JUMP Command. A nonlocal JUMP Command is "
145                         "invalid because the target is not a Job Header "
146                         "Command, or the jump is from a Trusted Descriptor to "
147                         "a Job Descriptor, or because the target Descriptor "
148                         "contains a Shared Descriptor." },
149                 { 0x0E, "Invalid MATH Command" },
150                 { 0x0F, "Invalid SIGNATURE Command" },
151                 { 0x10, "Invalid Sequence Command. A SEQ IN PTR OR SEQ OUT PTR "
152                         "Command is invalid or a SEQ KEY, SEQ LOAD, SEQ FIFO "
153                         "LOAD, or SEQ FIFO STORE decremented the input or "
154                         "output sequence length below 0. This error may result "
155                         "if a built-in PROTOCOL Command has encountered a "
156                         "malformed PDU." },
157                 { 0x11, "Skip data type invalid. The type must be 0xE or 0xF."},
158                 { 0x12, "Shared Descriptor Header Error" },
159                 { 0x13, "Header Error. Invalid length or parity, or certain "
160                         "other problems." },
161                 { 0x14, "Burster Error. Burster has gotten to an illegal "
162                         "state" },
163                 { 0x15, "Context Register Length Error. The descriptor is "
164                         "trying to read or write past the end of the Context "
165                         "Register. A SEQ LOAD or SEQ STORE with the VLF bit "
166                         "set was executed with too large a length in the "
167                         "variable length register (VSOL for SEQ STORE or VSIL "
168                         "for SEQ LOAD)." },
169                 { 0x16, "DMA Error" },
170                 { 0x17, "Reserved." },
171                 { 0x1A, "Job failed due to JR reset" },
172                 { 0x1B, "Job failed due to Fail Mode" },
173                 { 0x1C, "DECO Watchdog timer timeout error" },
174                 { 0x1D, "DECO tried to copy a key from another DECO but the "
175                         "other DECO's Key Registers were locked" },
176                 { 0x1E, "DECO attempted to copy data from a DECO that had an "
177                         "unmasked Descriptor error" },
178                 { 0x1F, "LIODN error. DECO was trying to share from itself or "
179                         "from another DECO but the two Non-SEQ LIODN values "
180                         "didn't match or the 'shared from' DECO's Descriptor "
181                         "required that the SEQ LIODNs be the same and they "
182                         "aren't." },
183                 { 0x20, "DECO has completed a reset initiated via the DRR "
184                         "register" },
185                 { 0x21, "Nonce error. When using EKT (CCM) key encryption "
186                         "option in the FIFO STORE Command, the Nonce counter "
187                         "reached its maximum value and this encryption mode "
188                         "can no longer be used." },
189                 { 0x22, "Meta data is too large (> 511 bytes) for TLS decap "
190                         "(input frame; block ciphers) and IPsec decap (output "
191                         "frame, when doing the next header byte update) and "
192                         "DCRC (output frame)." },
193                 { 0x23, "Read Input Frame error" },
194                 { 0x24, "JDKEK, TDKEK or TDSK not loaded error" },
195                 { 0x80, "DNR (do not run) error" },
196                 { 0x81, "undefined protocol command" },
197                 { 0x82, "invalid setting in PDB" },
198                 { 0x83, "Anti-replay LATE error" },
199                 { 0x84, "Anti-replay REPLAY error" },
200                 { 0x85, "Sequence number overflow" },
201                 { 0x86, "Sigver invalid signature" },
202                 { 0x87, "DSA Sign Illegal test descriptor" },
203                 { 0x88, "Protocol Format Error - A protocol has seen an error "
204                         "in the format of data received. When running RSA, "
205                         "this means that formatting with random padding was "
206                         "used, and did not follow the form: 0x00, 0x02, 8-to-N "
207                         "bytes of non-zero pad, 0x00, F data." },
208                 { 0x89, "Protocol Size Error - A protocol has seen an error in "
209                         "size. When running RSA, pdb size N < (size of F) when "
210                         "no formatting is used; or pdb size N < (F + 11) when "
211                         "formatting is used." },
212                 { 0xC1, "Blob Command error: Undefined mode" },
213                 { 0xC2, "Blob Command error: Secure Memory Blob mode error" },
214                 { 0xC4, "Blob Command error: Black Blob key or input size "
215                         "error" },
216                 { 0xC5, "Blob Command error: Invalid key destination" },
217                 { 0xC8, "Blob Command error: Trusted/Secure mode error" },
218                 { 0xF0, "IPsec TTL or hop limit field either came in as 0, "
219                         "or was decremented to 0" },
220                 { 0xF1, "3GPP HFN matches or exceeds the Threshold" },
221         };
222         u8 desc_error = status & JRSTA_DECOERR_ERROR_MASK;
223         int i;
224
225         report_jump_idx(status, outstr);
226
227         for (i = 0; i < ARRAY_SIZE(desc_error_list); i++)
228                 if (desc_error_list[i].value == desc_error)
229                         break;
230
231         if (i != ARRAY_SIZE(desc_error_list) && desc_error_list[i].error_text) {
232                 SPRINTFCAT(outstr, "%s", desc_error_list[i].error_text,
233                            strlen(desc_error_list[i].error_text));
234         } else {
235                 SPRINTFCAT(outstr, "unidentified error value 0x%02x",
236                            desc_error, sizeof("ff"));
237         }
238 }
239
240 static void report_jr_status(u32 status, char *outstr)
241 {
242         SPRINTFCAT(outstr, "%s() not implemented", __func__, sizeof(__func__));
243 }
244
245 static void report_cond_code_status(u32 status, char *outstr)
246 {
247         SPRINTFCAT(outstr, "%s() not implemented", __func__, sizeof(__func__));
248 }
249
250 char *caam_jr_strstatus(char *outstr, u32 status)
251 {
252         static const struct stat_src {
253                 void (*report_ssed)(u32 status, char *outstr);
254                 char *error;
255         } status_src[] = {
256                 { NULL, "No error" },
257                 { NULL, NULL },
258                 { report_ccb_status, "CCB" },
259                 { report_jump_status, "Jump" },
260                 { report_deco_status, "DECO" },
261                 { NULL, NULL },
262                 { report_jr_status, "Job Ring" },
263                 { report_cond_code_status, "Condition Code" },
264         };
265         u32 ssrc = status >> JRSTA_SSRC_SHIFT;
266
267         sprintf(outstr, "%s: ", status_src[ssrc].error);
268
269         if (status_src[ssrc].report_ssed)
270                 status_src[ssrc].report_ssed(status, outstr);
271
272         return outstr;
273 }
274 EXPORT_SYMBOL(caam_jr_strstatus);