crypto: qce - Qualcomm crypto engine driver
[cascardo/linux.git] / drivers / crypto / qce / common.c
1 /*
2  * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 and
6  * only version 2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13
14 #include <linux/err.h>
15 #include <linux/interrupt.h>
16 #include <linux/types.h>
17 #include <crypto/scatterwalk.h>
18 #include <crypto/sha.h>
19
20 #include "cipher.h"
21 #include "common.h"
22 #include "core.h"
23 #include "regs-v5.h"
24 #include "sha.h"
25
26 #define QCE_SECTOR_SIZE         512
27
28 static inline u32 qce_read(struct qce_device *qce, u32 offset)
29 {
30         return readl(qce->base + offset);
31 }
32
33 static inline void qce_write(struct qce_device *qce, u32 offset, u32 val)
34 {
35         writel(val, qce->base + offset);
36 }
37
38 static inline void qce_write_array(struct qce_device *qce, u32 offset,
39                                    const u32 *val, unsigned int len)
40 {
41         int i;
42
43         for (i = 0; i < len; i++)
44                 qce_write(qce, offset + i * sizeof(u32), val[i]);
45 }
46
47 static inline void
48 qce_clear_array(struct qce_device *qce, u32 offset, unsigned int len)
49 {
50         int i;
51
52         for (i = 0; i < len; i++)
53                 qce_write(qce, offset + i * sizeof(u32), 0);
54 }
55
56 static u32 qce_encr_cfg(unsigned long flags, u32 aes_key_size)
57 {
58         u32 cfg = 0;
59
60         if (IS_AES(flags)) {
61                 if (aes_key_size == AES_KEYSIZE_128)
62                         cfg |= ENCR_KEY_SZ_AES128 << ENCR_KEY_SZ_SHIFT;
63                 else if (aes_key_size == AES_KEYSIZE_256)
64                         cfg |= ENCR_KEY_SZ_AES256 << ENCR_KEY_SZ_SHIFT;
65         }
66
67         if (IS_AES(flags))
68                 cfg |= ENCR_ALG_AES << ENCR_ALG_SHIFT;
69         else if (IS_DES(flags) || IS_3DES(flags))
70                 cfg |= ENCR_ALG_DES << ENCR_ALG_SHIFT;
71
72         if (IS_DES(flags))
73                 cfg |= ENCR_KEY_SZ_DES << ENCR_KEY_SZ_SHIFT;
74
75         if (IS_3DES(flags))
76                 cfg |= ENCR_KEY_SZ_3DES << ENCR_KEY_SZ_SHIFT;
77
78         switch (flags & QCE_MODE_MASK) {
79         case QCE_MODE_ECB:
80                 cfg |= ENCR_MODE_ECB << ENCR_MODE_SHIFT;
81                 break;
82         case QCE_MODE_CBC:
83                 cfg |= ENCR_MODE_CBC << ENCR_MODE_SHIFT;
84                 break;
85         case QCE_MODE_CTR:
86                 cfg |= ENCR_MODE_CTR << ENCR_MODE_SHIFT;
87                 break;
88         case QCE_MODE_XTS:
89                 cfg |= ENCR_MODE_XTS << ENCR_MODE_SHIFT;
90                 break;
91         case QCE_MODE_CCM:
92                 cfg |= ENCR_MODE_CCM << ENCR_MODE_SHIFT;
93                 cfg |= LAST_CCM_XFR << LAST_CCM_SHIFT;
94                 break;
95         default:
96                 return ~0;
97         }
98
99         return cfg;
100 }
101
102 static u32 qce_auth_cfg(unsigned long flags, u32 key_size)
103 {
104         u32 cfg = 0;
105
106         if (IS_AES(flags) && (IS_CCM(flags) || IS_CMAC(flags)))
107                 cfg |= AUTH_ALG_AES << AUTH_ALG_SHIFT;
108         else
109                 cfg |= AUTH_ALG_SHA << AUTH_ALG_SHIFT;
110
111         if (IS_CCM(flags) || IS_CMAC(flags)) {
112                 if (key_size == AES_KEYSIZE_128)
113                         cfg |= AUTH_KEY_SZ_AES128 << AUTH_KEY_SIZE_SHIFT;
114                 else if (key_size == AES_KEYSIZE_256)
115                         cfg |= AUTH_KEY_SZ_AES256 << AUTH_KEY_SIZE_SHIFT;
116         }
117
118         if (IS_SHA1(flags) || IS_SHA1_HMAC(flags))
119                 cfg |= AUTH_SIZE_SHA1 << AUTH_SIZE_SHIFT;
120         else if (IS_SHA256(flags) || IS_SHA256_HMAC(flags))
121                 cfg |= AUTH_SIZE_SHA256 << AUTH_SIZE_SHIFT;
122         else if (IS_CMAC(flags))
123                 cfg |= AUTH_SIZE_ENUM_16_BYTES << AUTH_SIZE_SHIFT;
124
125         if (IS_SHA1(flags) || IS_SHA256(flags))
126                 cfg |= AUTH_MODE_HASH << AUTH_MODE_SHIFT;
127         else if (IS_SHA1_HMAC(flags) || IS_SHA256_HMAC(flags) ||
128                  IS_CBC(flags) || IS_CTR(flags))
129                 cfg |= AUTH_MODE_HMAC << AUTH_MODE_SHIFT;
130         else if (IS_AES(flags) && IS_CCM(flags))
131                 cfg |= AUTH_MODE_CCM << AUTH_MODE_SHIFT;
132         else if (IS_AES(flags) && IS_CMAC(flags))
133                 cfg |= AUTH_MODE_CMAC << AUTH_MODE_SHIFT;
134
135         if (IS_SHA(flags) || IS_SHA_HMAC(flags))
136                 cfg |= AUTH_POS_BEFORE << AUTH_POS_SHIFT;
137
138         if (IS_CCM(flags))
139                 cfg |= QCE_MAX_NONCE_WORDS << AUTH_NONCE_NUM_WORDS_SHIFT;
140
141         if (IS_CBC(flags) || IS_CTR(flags) || IS_CCM(flags) ||
142             IS_CMAC(flags))
143                 cfg |= BIT(AUTH_LAST_SHIFT) | BIT(AUTH_FIRST_SHIFT);
144
145         return cfg;
146 }
147
148 static u32 qce_config_reg(struct qce_device *qce, int little)
149 {
150         u32 beats = (qce->burst_size >> 3) - 1;
151         u32 pipe_pair = qce->pipe_pair_id;
152         u32 config;
153
154         config = (beats << REQ_SIZE_SHIFT) & REQ_SIZE_MASK;
155         config |= BIT(MASK_DOUT_INTR_SHIFT) | BIT(MASK_DIN_INTR_SHIFT) |
156                   BIT(MASK_OP_DONE_INTR_SHIFT) | BIT(MASK_ERR_INTR_SHIFT);
157         config |= (pipe_pair << PIPE_SET_SELECT_SHIFT) & PIPE_SET_SELECT_MASK;
158         config &= ~HIGH_SPD_EN_N_SHIFT;
159
160         if (little)
161                 config |= BIT(LITTLE_ENDIAN_MODE_SHIFT);
162
163         return config;
164 }
165
166 void qce_cpu_to_be32p_array(__be32 *dst, const u8 *src, unsigned int len)
167 {
168         __be32 *d = dst;
169         const u8 *s = src;
170         unsigned int n;
171
172         n = len / sizeof(u32);
173         for (; n > 0; n--) {
174                 *d = cpu_to_be32p((const __u32 *) s);
175                 s += sizeof(__u32);
176                 d++;
177         }
178 }
179
180 static void qce_xts_swapiv(__be32 *dst, const u8 *src, unsigned int ivsize)
181 {
182         u8 swap[QCE_AES_IV_LENGTH];
183         u32 i, j;
184
185         if (ivsize > QCE_AES_IV_LENGTH)
186                 return;
187
188         memset(swap, 0, QCE_AES_IV_LENGTH);
189
190         for (i = (QCE_AES_IV_LENGTH - ivsize), j = ivsize - 1;
191              i < QCE_AES_IV_LENGTH; i++, j--)
192                 swap[i] = src[j];
193
194         qce_cpu_to_be32p_array(dst, swap, QCE_AES_IV_LENGTH);
195 }
196
197 static void qce_xtskey(struct qce_device *qce, const u8 *enckey,
198                        unsigned int enckeylen, unsigned int cryptlen)
199 {
200         u32 xtskey[QCE_MAX_CIPHER_KEY_SIZE / sizeof(u32)] = {0};
201         unsigned int xtsklen = enckeylen / (2 * sizeof(u32));
202         unsigned int xtsdusize;
203
204         qce_cpu_to_be32p_array(xtskey, enckey + enckeylen / 2, enckeylen / 2);
205         qce_write_array(qce, REG_ENCR_XTS_KEY0, xtskey, xtsklen);
206
207         /* xts du size 512B */
208         xtsdusize = min_t(u32, QCE_SECTOR_SIZE, cryptlen);
209         qce_write(qce, REG_ENCR_XTS_DU_SIZE, xtsdusize);
210 }
211
212 static void qce_setup_config(struct qce_device *qce)
213 {
214         u32 config;
215
216         /* get big endianness */
217         config = qce_config_reg(qce, 0);
218
219         /* clear status */
220         qce_write(qce, REG_STATUS, 0);
221         qce_write(qce, REG_CONFIG, config);
222 }
223
224 static inline void qce_crypto_go(struct qce_device *qce)
225 {
226         qce_write(qce, REG_GOPROC, BIT(GO_SHIFT) | BIT(RESULTS_DUMP_SHIFT));
227 }
228
229 static int qce_setup_regs_ahash(struct crypto_async_request *async_req,
230                                 u32 totallen, u32 offset)
231 {
232         struct ahash_request *req = ahash_request_cast(async_req);
233         struct crypto_ahash *ahash = __crypto_ahash_cast(async_req->tfm);
234         struct qce_sha_reqctx *rctx = ahash_request_ctx(req);
235         struct qce_alg_template *tmpl = to_ahash_tmpl(async_req->tfm);
236         struct qce_device *qce = tmpl->qce;
237         unsigned int digestsize = crypto_ahash_digestsize(ahash);
238         unsigned int blocksize = crypto_tfm_alg_blocksize(async_req->tfm);
239         __be32 auth[SHA256_DIGEST_SIZE / sizeof(__be32)] = {0};
240         __be32 mackey[QCE_SHA_HMAC_KEY_SIZE / sizeof(__be32)] = {0};
241         u32 auth_cfg = 0, config;
242         unsigned int iv_words;
243
244         /* if not the last, the size has to be on the block boundary */
245         if (!rctx->last_blk && req->nbytes % blocksize)
246                 return -EINVAL;
247
248         qce_setup_config(qce);
249
250         if (IS_CMAC(rctx->flags)) {
251                 qce_write(qce, REG_AUTH_SEG_CFG, 0);
252                 qce_write(qce, REG_ENCR_SEG_CFG, 0);
253                 qce_write(qce, REG_ENCR_SEG_SIZE, 0);
254                 qce_clear_array(qce, REG_AUTH_IV0, 16);
255                 qce_clear_array(qce, REG_AUTH_KEY0, 16);
256                 qce_clear_array(qce, REG_AUTH_BYTECNT0, 4);
257
258                 auth_cfg = qce_auth_cfg(rctx->flags, rctx->authklen);
259         }
260
261         if (IS_SHA_HMAC(rctx->flags) || IS_CMAC(rctx->flags)) {
262                 u32 authkey_words = rctx->authklen / sizeof(u32);
263
264                 qce_cpu_to_be32p_array(mackey, rctx->authkey, rctx->authklen);
265                 qce_write_array(qce, REG_AUTH_KEY0, mackey, authkey_words);
266         }
267
268         if (IS_CMAC(rctx->flags))
269                 goto go_proc;
270
271         if (rctx->first_blk)
272                 memcpy(auth, rctx->digest, digestsize);
273         else
274                 qce_cpu_to_be32p_array(auth, rctx->digest, digestsize);
275
276         iv_words = (IS_SHA1(rctx->flags) || IS_SHA1_HMAC(rctx->flags)) ? 5 : 8;
277         qce_write_array(qce, REG_AUTH_IV0, auth, iv_words);
278
279         if (rctx->first_blk)
280                 qce_clear_array(qce, REG_AUTH_BYTECNT0, 4);
281         else
282                 qce_write_array(qce, REG_AUTH_BYTECNT0, rctx->byte_count, 2);
283
284         auth_cfg = qce_auth_cfg(rctx->flags, 0);
285
286         if (rctx->last_blk)
287                 auth_cfg |= BIT(AUTH_LAST_SHIFT);
288         else
289                 auth_cfg &= ~BIT(AUTH_LAST_SHIFT);
290
291         if (rctx->first_blk)
292                 auth_cfg |= BIT(AUTH_FIRST_SHIFT);
293         else
294                 auth_cfg &= ~BIT(AUTH_FIRST_SHIFT);
295
296 go_proc:
297         qce_write(qce, REG_AUTH_SEG_CFG, auth_cfg);
298         qce_write(qce, REG_AUTH_SEG_SIZE, req->nbytes);
299         qce_write(qce, REG_AUTH_SEG_START, 0);
300         qce_write(qce, REG_ENCR_SEG_CFG, 0);
301         qce_write(qce, REG_SEG_SIZE, req->nbytes);
302
303         /* get little endianness */
304         config = qce_config_reg(qce, 1);
305         qce_write(qce, REG_CONFIG, config);
306
307         qce_crypto_go(qce);
308
309         return 0;
310 }
311
312 static int qce_setup_regs_ablkcipher(struct crypto_async_request *async_req,
313                                      u32 totallen, u32 offset)
314 {
315         struct ablkcipher_request *req = ablkcipher_request_cast(async_req);
316         struct qce_cipher_reqctx *rctx = ablkcipher_request_ctx(req);
317         struct qce_cipher_ctx *ctx = crypto_tfm_ctx(async_req->tfm);
318         struct qce_alg_template *tmpl = to_cipher_tmpl(async_req->tfm);
319         struct qce_device *qce = tmpl->qce;
320         __be32 enckey[QCE_MAX_CIPHER_KEY_SIZE / sizeof(__be32)] = {0};
321         __be32 enciv[QCE_MAX_IV_SIZE / sizeof(__be32)] = {0};
322         unsigned int enckey_words, enciv_words;
323         unsigned int keylen;
324         u32 encr_cfg = 0, auth_cfg = 0, config;
325         unsigned int ivsize = rctx->ivsize;
326         unsigned long flags = rctx->flags;
327
328         qce_setup_config(qce);
329
330         if (IS_XTS(flags))
331                 keylen = ctx->enc_keylen / 2;
332         else
333                 keylen = ctx->enc_keylen;
334
335         qce_cpu_to_be32p_array(enckey, ctx->enc_key, keylen);
336         enckey_words = keylen / sizeof(u32);
337
338         qce_write(qce, REG_AUTH_SEG_CFG, auth_cfg);
339
340         encr_cfg = qce_encr_cfg(flags, keylen);
341
342         if (IS_DES(flags)) {
343                 enciv_words = 2;
344                 enckey_words = 2;
345         } else if (IS_3DES(flags)) {
346                 enciv_words = 2;
347                 enckey_words = 6;
348         } else if (IS_AES(flags)) {
349                 if (IS_XTS(flags))
350                         qce_xtskey(qce, ctx->enc_key, ctx->enc_keylen,
351                                    rctx->cryptlen);
352                 enciv_words = 4;
353         } else {
354                 return -EINVAL;
355         }
356
357         qce_write_array(qce, REG_ENCR_KEY0, enckey, enckey_words);
358
359         if (!IS_ECB(flags)) {
360                 if (IS_XTS(flags))
361                         qce_xts_swapiv(enciv, rctx->iv, ivsize);
362                 else
363                         qce_cpu_to_be32p_array(enciv, rctx->iv, ivsize);
364
365                 qce_write_array(qce, REG_CNTR0_IV0, enciv, enciv_words);
366         }
367
368         if (IS_ENCRYPT(flags))
369                 encr_cfg |= BIT(ENCODE_SHIFT);
370
371         qce_write(qce, REG_ENCR_SEG_CFG, encr_cfg);
372         qce_write(qce, REG_ENCR_SEG_SIZE, rctx->cryptlen);
373         qce_write(qce, REG_ENCR_SEG_START, offset & 0xffff);
374
375         if (IS_CTR(flags)) {
376                 qce_write(qce, REG_CNTR_MASK, ~0);
377                 qce_write(qce, REG_CNTR_MASK0, ~0);
378                 qce_write(qce, REG_CNTR_MASK1, ~0);
379                 qce_write(qce, REG_CNTR_MASK2, ~0);
380         }
381
382         qce_write(qce, REG_SEG_SIZE, totallen);
383
384         /* get little endianness */
385         config = qce_config_reg(qce, 1);
386         qce_write(qce, REG_CONFIG, config);
387
388         qce_crypto_go(qce);
389
390         return 0;
391 }
392
393 int qce_start(struct crypto_async_request *async_req, u32 type, u32 totallen,
394               u32 offset)
395 {
396         switch (type) {
397         case CRYPTO_ALG_TYPE_ABLKCIPHER:
398                 return qce_setup_regs_ablkcipher(async_req, totallen, offset);
399         case CRYPTO_ALG_TYPE_AHASH:
400                 return qce_setup_regs_ahash(async_req, totallen, offset);
401         default:
402                 return -EINVAL;
403         }
404 }
405
406 #define STATUS_ERRORS   \
407                 (BIT(SW_ERR_SHIFT) | BIT(AXI_ERR_SHIFT) | BIT(HSD_ERR_SHIFT))
408
409 int qce_check_status(struct qce_device *qce, u32 *status)
410 {
411         int ret = 0;
412
413         *status = qce_read(qce, REG_STATUS);
414
415         /*
416          * Don't use result dump status. The operation may not be complete.
417          * Instead, use the status we just read from device. In case, we need to
418          * use result_status from result dump the result_status needs to be byte
419          * swapped, since we set the device to little endian.
420          */
421         if (*status & STATUS_ERRORS || !(*status & BIT(OPERATION_DONE_SHIFT)))
422                 ret = -ENXIO;
423
424         return ret;
425 }
426
427 void qce_get_version(struct qce_device *qce, u32 *major, u32 *minor, u32 *step)
428 {
429         u32 val;
430
431         val = qce_read(qce, REG_VERSION);
432         *major = (val & CORE_MAJOR_REV_MASK) >> CORE_MAJOR_REV_SHIFT;
433         *minor = (val & CORE_MINOR_REV_MASK) >> CORE_MINOR_REV_SHIFT;
434         *step = (val & CORE_STEP_REV_MASK) >> CORE_STEP_REV_SHIFT;
435 }