crypto: chelsio - Reset counters on cxgb4 Detach
[linux-2.6-block.git] / drivers / crypto / chelsio / chcr_algo.c
CommitLineData
324429d7
HS
1/*
2 * This file is part of the Chelsio T6 Crypto driver for Linux.
3 *
4 * Copyright (c) 2003-2016 Chelsio Communications, Inc. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 *
34 * Written and Maintained by:
35 * Manoj Malviya (manojmalviya@chelsio.com)
36 * Atul Gupta (atul.gupta@chelsio.com)
37 * Jitendra Lulla (jlulla@chelsio.com)
38 * Yeshaswi M R Gowda (yeshaswi@chelsio.com)
39 * Harsh Jain (harsh@chelsio.com)
40 */
41
42#define pr_fmt(fmt) "chcr:" fmt
43
44#include <linux/kernel.h>
45#include <linux/module.h>
46#include <linux/crypto.h>
47#include <linux/cryptohash.h>
48#include <linux/skbuff.h>
49#include <linux/rtnetlink.h>
50#include <linux/highmem.h>
51#include <linux/scatterlist.h>
52
53#include <crypto/aes.h>
54#include <crypto/algapi.h>
55#include <crypto/hash.h>
8f6acb7f 56#include <crypto/gcm.h>
324429d7 57#include <crypto/sha.h>
2debd332 58#include <crypto/authenc.h>
b8fd1f41
HJ
59#include <crypto/ctr.h>
60#include <crypto/gf128mul.h>
2debd332
HJ
61#include <crypto/internal/aead.h>
62#include <crypto/null.h>
63#include <crypto/internal/skcipher.h>
64#include <crypto/aead.h>
65#include <crypto/scatterwalk.h>
324429d7
HS
66#include <crypto/internal/hash.h>
67
68#include "t4fw_api.h"
69#include "t4_msg.h"
70#include "chcr_core.h"
71#include "chcr_algo.h"
72#include "chcr_crypto.h"
73
2f47d580
HJ
74#define IV AES_BLOCK_SIZE
75
8579e076
CIK
76static unsigned int sgl_ent_len[] = {
77 0, 0, 16, 24, 40, 48, 64, 72, 88,
78 96, 112, 120, 136, 144, 160, 168, 184,
79 192, 208, 216, 232, 240, 256, 264, 280,
80 288, 304, 312, 328, 336, 352, 360, 376
81};
6dad4e8a 82
8579e076
CIK
83static unsigned int dsgl_ent_len[] = {
84 0, 32, 32, 48, 48, 64, 64, 80, 80,
85 112, 112, 128, 128, 144, 144, 160, 160,
86 192, 192, 208, 208, 224, 224, 240, 240,
87 272, 272, 288, 288, 304, 304, 320, 320
88};
6dad4e8a
AG
89
90static u32 round_constant[11] = {
91 0x01000000, 0x02000000, 0x04000000, 0x08000000,
92 0x10000000, 0x20000000, 0x40000000, 0x80000000,
93 0x1B000000, 0x36000000, 0x6C000000
94};
95
96static int chcr_handle_cipher_resp(struct ablkcipher_request *req,
97 unsigned char *input, int err);
98
2debd332
HJ
99static inline struct chcr_aead_ctx *AEAD_CTX(struct chcr_context *ctx)
100{
101 return ctx->crypto_ctx->aeadctx;
102}
103
324429d7
HS
104static inline struct ablk_ctx *ABLK_CTX(struct chcr_context *ctx)
105{
106 return ctx->crypto_ctx->ablkctx;
107}
108
109static inline struct hmac_ctx *HMAC_CTX(struct chcr_context *ctx)
110{
111 return ctx->crypto_ctx->hmacctx;
112}
113
2debd332
HJ
114static inline struct chcr_gcm_ctx *GCM_CTX(struct chcr_aead_ctx *gctx)
115{
116 return gctx->ctx->gcm;
117}
118
119static inline struct chcr_authenc_ctx *AUTHENC_CTX(struct chcr_aead_ctx *gctx)
120{
121 return gctx->ctx->authenc;
122}
123
324429d7
HS
124static inline struct uld_ctx *ULD_CTX(struct chcr_context *ctx)
125{
fef4912b 126 return container_of(ctx->dev, struct uld_ctx, dev);
324429d7
HS
127}
128
129static inline int is_ofld_imm(const struct sk_buff *skb)
130{
2f47d580 131 return (skb->len <= SGE_MAX_WR_LEN);
324429d7
HS
132}
133
5110e655
HJ
134static inline void chcr_init_hctx_per_wr(struct chcr_ahash_req_ctx *reqctx)
135{
136 memset(&reqctx->hctx_wr, 0, sizeof(struct chcr_hctx_per_wr));
137}
138
2f47d580
HJ
139static int sg_nents_xlen(struct scatterlist *sg, unsigned int reqlen,
140 unsigned int entlen,
141 unsigned int skip)
2956f36c
HJ
142{
143 int nents = 0;
144 unsigned int less;
2f47d580 145 unsigned int skip_len = 0;
2956f36c 146
2f47d580
HJ
147 while (sg && skip) {
148 if (sg_dma_len(sg) <= skip) {
149 skip -= sg_dma_len(sg);
150 skip_len = 0;
151 sg = sg_next(sg);
152 } else {
153 skip_len = skip;
154 skip = 0;
155 }
2956f36c
HJ
156 }
157
2f47d580
HJ
158 while (sg && reqlen) {
159 less = min(reqlen, sg_dma_len(sg) - skip_len);
160 nents += DIV_ROUND_UP(less, entlen);
161 reqlen -= less;
162 skip_len = 0;
163 sg = sg_next(sg);
164 }
2956f36c
HJ
165 return nents;
166}
167
6dad4e8a 168static inline int get_aead_subtype(struct crypto_aead *aead)
2f47d580 169{
6dad4e8a
AG
170 struct aead_alg *alg = crypto_aead_alg(aead);
171 struct chcr_alg_template *chcr_crypto_alg =
172 container_of(alg, struct chcr_alg_template, alg.aead);
173 return chcr_crypto_alg->type & CRYPTO_ALG_SUB_TYPE_MASK;
2f47d580 174}
2f47d580 175
6dad4e8a 176void chcr_verify_tag(struct aead_request *req, u8 *input, int *err)
2debd332
HJ
177{
178 u8 temp[SHA512_DIGEST_SIZE];
179 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
180 int authsize = crypto_aead_authsize(tfm);
181 struct cpl_fw6_pld *fw6_pld;
182 int cmp = 0;
183
184 fw6_pld = (struct cpl_fw6_pld *)input;
185 if ((get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106) ||
186 (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_GCM)) {
d600fc8a 187 cmp = crypto_memneq(&fw6_pld->data[2], (fw6_pld + 1), authsize);
2debd332
HJ
188 } else {
189
190 sg_pcopy_to_buffer(req->src, sg_nents(req->src), temp,
191 authsize, req->assoclen +
192 req->cryptlen - authsize);
d600fc8a 193 cmp = crypto_memneq(temp, (fw6_pld + 1), authsize);
2debd332
HJ
194 }
195 if (cmp)
196 *err = -EBADMSG;
197 else
198 *err = 0;
199}
200
fef4912b
HJ
201static int chcr_inc_wrcount(struct chcr_dev *dev)
202{
203 int err = 0;
204
205 spin_lock_bh(&dev->lock_chcr_dev);
206 if (dev->state == CHCR_DETACH)
207 err = 1;
208 else
209 atomic_inc(&dev->inflight);
210
211 spin_unlock_bh(&dev->lock_chcr_dev);
212
213 return err;
214}
215
216static inline void chcr_dec_wrcount(struct chcr_dev *dev)
217{
218 atomic_dec(&dev->inflight);
219}
220
6dad4e8a
AG
221static inline void chcr_handle_aead_resp(struct aead_request *req,
222 unsigned char *input,
223 int err)
224{
225 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
fef4912b
HJ
226 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
227 struct chcr_dev *dev = a_ctx(tfm)->dev;
6dad4e8a 228
4262c98a 229 chcr_aead_common_exit(req);
6dad4e8a
AG
230 if (reqctx->verify == VERIFY_SW) {
231 chcr_verify_tag(req, input, &err);
232 reqctx->verify = VERIFY_HW;
233 }
fef4912b 234 chcr_dec_wrcount(dev);
6dad4e8a
AG
235 req->base.complete(&req->base, err);
236}
237
2f47d580 238static void get_aes_decrypt_key(unsigned char *dec_key,
39f91a34
HJ
239 const unsigned char *key,
240 unsigned int keylength)
241{
242 u32 temp;
243 u32 w_ring[MAX_NK];
244 int i, j, k;
245 u8 nr, nk;
246
247 switch (keylength) {
248 case AES_KEYLENGTH_128BIT:
249 nk = KEYLENGTH_4BYTES;
250 nr = NUMBER_OF_ROUNDS_10;
251 break;
252 case AES_KEYLENGTH_192BIT:
253 nk = KEYLENGTH_6BYTES;
254 nr = NUMBER_OF_ROUNDS_12;
255 break;
256 case AES_KEYLENGTH_256BIT:
257 nk = KEYLENGTH_8BYTES;
258 nr = NUMBER_OF_ROUNDS_14;
259 break;
260 default:
261 return;
262 }
263 for (i = 0; i < nk; i++)
264 w_ring[i] = be32_to_cpu(*(u32 *)&key[4 * i]);
265
266 i = 0;
267 temp = w_ring[nk - 1];
268 while (i + nk < (nr + 1) * 4) {
269 if (!(i % nk)) {
270 /* RotWord(temp) */
271 temp = (temp << 8) | (temp >> 24);
272 temp = aes_ks_subword(temp);
273 temp ^= round_constant[i / nk];
274 } else if (nk == 8 && (i % 4 == 0)) {
275 temp = aes_ks_subword(temp);
276 }
277 w_ring[i % nk] ^= temp;
278 temp = w_ring[i % nk];
279 i++;
280 }
281 i--;
282 for (k = 0, j = i % nk; k < nk; k++) {
283 *((u32 *)dec_key + k) = htonl(w_ring[j]);
284 j--;
285 if (j < 0)
286 j += nk;
287 }
288}
289
e7922729 290static struct crypto_shash *chcr_alloc_shash(unsigned int ds)
324429d7 291{
ec1bca94 292 struct crypto_shash *base_hash = ERR_PTR(-EINVAL);
324429d7
HS
293
294 switch (ds) {
295 case SHA1_DIGEST_SIZE:
e7922729 296 base_hash = crypto_alloc_shash("sha1", 0, 0);
324429d7
HS
297 break;
298 case SHA224_DIGEST_SIZE:
e7922729 299 base_hash = crypto_alloc_shash("sha224", 0, 0);
324429d7
HS
300 break;
301 case SHA256_DIGEST_SIZE:
e7922729 302 base_hash = crypto_alloc_shash("sha256", 0, 0);
324429d7
HS
303 break;
304 case SHA384_DIGEST_SIZE:
e7922729 305 base_hash = crypto_alloc_shash("sha384", 0, 0);
324429d7
HS
306 break;
307 case SHA512_DIGEST_SIZE:
e7922729 308 base_hash = crypto_alloc_shash("sha512", 0, 0);
324429d7
HS
309 break;
310 }
324429d7 311
e7922729 312 return base_hash;
324429d7
HS
313}
314
315static int chcr_compute_partial_hash(struct shash_desc *desc,
316 char *iopad, char *result_hash,
317 int digest_size)
318{
319 struct sha1_state sha1_st;
320 struct sha256_state sha256_st;
321 struct sha512_state sha512_st;
322 int error;
323
324 if (digest_size == SHA1_DIGEST_SIZE) {
325 error = crypto_shash_init(desc) ?:
326 crypto_shash_update(desc, iopad, SHA1_BLOCK_SIZE) ?:
327 crypto_shash_export(desc, (void *)&sha1_st);
328 memcpy(result_hash, sha1_st.state, SHA1_DIGEST_SIZE);
329 } else if (digest_size == SHA224_DIGEST_SIZE) {
330 error = crypto_shash_init(desc) ?:
331 crypto_shash_update(desc, iopad, SHA256_BLOCK_SIZE) ?:
332 crypto_shash_export(desc, (void *)&sha256_st);
333 memcpy(result_hash, sha256_st.state, SHA256_DIGEST_SIZE);
334
335 } else if (digest_size == SHA256_DIGEST_SIZE) {
336 error = crypto_shash_init(desc) ?:
337 crypto_shash_update(desc, iopad, SHA256_BLOCK_SIZE) ?:
338 crypto_shash_export(desc, (void *)&sha256_st);
339 memcpy(result_hash, sha256_st.state, SHA256_DIGEST_SIZE);
340
341 } else if (digest_size == SHA384_DIGEST_SIZE) {
342 error = crypto_shash_init(desc) ?:
343 crypto_shash_update(desc, iopad, SHA512_BLOCK_SIZE) ?:
344 crypto_shash_export(desc, (void *)&sha512_st);
345 memcpy(result_hash, sha512_st.state, SHA512_DIGEST_SIZE);
346
347 } else if (digest_size == SHA512_DIGEST_SIZE) {
348 error = crypto_shash_init(desc) ?:
349 crypto_shash_update(desc, iopad, SHA512_BLOCK_SIZE) ?:
350 crypto_shash_export(desc, (void *)&sha512_st);
351 memcpy(result_hash, sha512_st.state, SHA512_DIGEST_SIZE);
352 } else {
353 error = -EINVAL;
354 pr_err("Unknown digest size %d\n", digest_size);
355 }
356 return error;
357}
358
359static void chcr_change_order(char *buf, int ds)
360{
361 int i;
362
363 if (ds == SHA512_DIGEST_SIZE) {
364 for (i = 0; i < (ds / sizeof(u64)); i++)
365 *((__be64 *)buf + i) =
366 cpu_to_be64(*((u64 *)buf + i));
367 } else {
368 for (i = 0; i < (ds / sizeof(u32)); i++)
369 *((__be32 *)buf + i) =
370 cpu_to_be32(*((u32 *)buf + i));
371 }
372}
373
374static inline int is_hmac(struct crypto_tfm *tfm)
375{
376 struct crypto_alg *alg = tfm->__crt_alg;
377 struct chcr_alg_template *chcr_crypto_alg =
378 container_of(__crypto_ahash_alg(alg), struct chcr_alg_template,
379 alg.hash);
5c86a8ff 380 if (chcr_crypto_alg->type == CRYPTO_ALG_TYPE_HMAC)
324429d7
HS
381 return 1;
382 return 0;
383}
384
2f47d580
HJ
385static inline void dsgl_walk_init(struct dsgl_walk *walk,
386 struct cpl_rx_phys_dsgl *dsgl)
324429d7 387{
2f47d580
HJ
388 walk->dsgl = dsgl;
389 walk->nents = 0;
390 walk->to = (struct phys_sge_pairs *)(dsgl + 1);
391}
392
add92a81
HJ
393static inline void dsgl_walk_end(struct dsgl_walk *walk, unsigned short qid,
394 int pci_chan_id)
2f47d580
HJ
395{
396 struct cpl_rx_phys_dsgl *phys_cpl;
397
398 phys_cpl = walk->dsgl;
324429d7
HS
399
400 phys_cpl->op_to_tid = htonl(CPL_RX_PHYS_DSGL_OPCODE_V(CPL_RX_PHYS_DSGL)
401 | CPL_RX_PHYS_DSGL_ISRDMA_V(0));
2f47d580
HJ
402 phys_cpl->pcirlxorder_to_noofsgentr =
403 htonl(CPL_RX_PHYS_DSGL_PCIRLXORDER_V(0) |
404 CPL_RX_PHYS_DSGL_PCINOSNOOP_V(0) |
405 CPL_RX_PHYS_DSGL_PCITPHNTENB_V(0) |
406 CPL_RX_PHYS_DSGL_PCITPHNT_V(0) |
407 CPL_RX_PHYS_DSGL_DCAID_V(0) |
408 CPL_RX_PHYS_DSGL_NOOFSGENTR_V(walk->nents));
409 phys_cpl->rss_hdr_int.opcode = CPL_RX_PHYS_ADDR;
410 phys_cpl->rss_hdr_int.qid = htons(qid);
411 phys_cpl->rss_hdr_int.hash_val = 0;
add92a81 412 phys_cpl->rss_hdr_int.channel = pci_chan_id;
2f47d580
HJ
413}
414
415static inline void dsgl_walk_add_page(struct dsgl_walk *walk,
416 size_t size,
c4f6d44d 417 dma_addr_t addr)
2f47d580
HJ
418{
419 int j;
420
421 if (!size)
422 return;
423 j = walk->nents;
424 walk->to->len[j % 8] = htons(size);
c4f6d44d 425 walk->to->addr[j % 8] = cpu_to_be64(addr);
2f47d580
HJ
426 j++;
427 if ((j % 8) == 0)
428 walk->to++;
429 walk->nents = j;
430}
431
432static void dsgl_walk_add_sg(struct dsgl_walk *walk,
433 struct scatterlist *sg,
434 unsigned int slen,
435 unsigned int skip)
436{
437 int skip_len = 0;
438 unsigned int left_size = slen, len = 0;
439 unsigned int j = walk->nents;
440 int offset, ent_len;
441
442 if (!slen)
443 return;
444 while (sg && skip) {
445 if (sg_dma_len(sg) <= skip) {
446 skip -= sg_dma_len(sg);
447 skip_len = 0;
448 sg = sg_next(sg);
449 } else {
450 skip_len = skip;
451 skip = 0;
452 }
453 }
454
2956f36c 455 while (left_size && sg) {
2f47d580 456 len = min_t(u32, left_size, sg_dma_len(sg) - skip_len);
2956f36c
HJ
457 offset = 0;
458 while (len) {
2f47d580
HJ
459 ent_len = min_t(u32, len, CHCR_DST_SG_SIZE);
460 walk->to->len[j % 8] = htons(ent_len);
461 walk->to->addr[j % 8] = cpu_to_be64(sg_dma_address(sg) +
462 offset + skip_len);
2956f36c
HJ
463 offset += ent_len;
464 len -= ent_len;
465 j++;
466 if ((j % 8) == 0)
2f47d580 467 walk->to++;
2956f36c 468 }
2f47d580
HJ
469 walk->last_sg = sg;
470 walk->last_sg_len = min_t(u32, left_size, sg_dma_len(sg) -
471 skip_len) + skip_len;
472 left_size -= min_t(u32, left_size, sg_dma_len(sg) - skip_len);
473 skip_len = 0;
2956f36c
HJ
474 sg = sg_next(sg);
475 }
2f47d580
HJ
476 walk->nents = j;
477}
478
479static inline void ulptx_walk_init(struct ulptx_walk *walk,
480 struct ulptx_sgl *ulp)
481{
482 walk->sgl = ulp;
483 walk->nents = 0;
484 walk->pair_idx = 0;
485 walk->pair = ulp->sge;
486 walk->last_sg = NULL;
487 walk->last_sg_len = 0;
488}
489
490static inline void ulptx_walk_end(struct ulptx_walk *walk)
491{
492 walk->sgl->cmd_nsge = htonl(ULPTX_CMD_V(ULP_TX_SC_DSGL) |
493 ULPTX_NSGE_V(walk->nents));
494}
2956f36c 495
2f47d580
HJ
496
497static inline void ulptx_walk_add_page(struct ulptx_walk *walk,
498 size_t size,
c4f6d44d 499 dma_addr_t addr)
2f47d580
HJ
500{
501 if (!size)
502 return;
503
504 if (walk->nents == 0) {
505 walk->sgl->len0 = cpu_to_be32(size);
c4f6d44d 506 walk->sgl->addr0 = cpu_to_be64(addr);
2f47d580 507 } else {
c4f6d44d 508 walk->pair->addr[walk->pair_idx] = cpu_to_be64(addr);
2f47d580
HJ
509 walk->pair->len[walk->pair_idx] = cpu_to_be32(size);
510 walk->pair_idx = !walk->pair_idx;
511 if (!walk->pair_idx)
512 walk->pair++;
513 }
514 walk->nents++;
324429d7
HS
515}
516
2f47d580 517static void ulptx_walk_add_sg(struct ulptx_walk *walk,
adf1ca61 518 struct scatterlist *sg,
2f47d580
HJ
519 unsigned int len,
520 unsigned int skip)
324429d7 521{
2f47d580
HJ
522 int small;
523 int skip_len = 0;
524 unsigned int sgmin;
324429d7 525
2f47d580
HJ
526 if (!len)
527 return;
2f47d580
HJ
528 while (sg && skip) {
529 if (sg_dma_len(sg) <= skip) {
530 skip -= sg_dma_len(sg);
531 skip_len = 0;
532 sg = sg_next(sg);
533 } else {
534 skip_len = skip;
535 skip = 0;
536 }
537 }
8daa32b9
HJ
538 WARN(!sg, "SG should not be null here\n");
539 if (sg && (walk->nents == 0)) {
2f47d580
HJ
540 small = min_t(unsigned int, sg_dma_len(sg) - skip_len, len);
541 sgmin = min_t(unsigned int, small, CHCR_SRC_SG_SIZE);
542 walk->sgl->len0 = cpu_to_be32(sgmin);
543 walk->sgl->addr0 = cpu_to_be64(sg_dma_address(sg) + skip_len);
544 walk->nents++;
545 len -= sgmin;
546 walk->last_sg = sg;
547 walk->last_sg_len = sgmin + skip_len;
548 skip_len += sgmin;
549 if (sg_dma_len(sg) == skip_len) {
550 sg = sg_next(sg);
551 skip_len = 0;
552 }
553 }
554
555 while (sg && len) {
556 small = min(sg_dma_len(sg) - skip_len, len);
557 sgmin = min_t(unsigned int, small, CHCR_SRC_SG_SIZE);
558 walk->pair->len[walk->pair_idx] = cpu_to_be32(sgmin);
559 walk->pair->addr[walk->pair_idx] =
560 cpu_to_be64(sg_dma_address(sg) + skip_len);
561 walk->pair_idx = !walk->pair_idx;
562 walk->nents++;
563 if (!walk->pair_idx)
564 walk->pair++;
565 len -= sgmin;
566 skip_len += sgmin;
567 walk->last_sg = sg;
568 walk->last_sg_len = skip_len;
569 if (sg_dma_len(sg) == skip_len) {
570 sg = sg_next(sg);
571 skip_len = 0;
572 }
324429d7 573 }
324429d7
HS
574}
575
576static inline int get_cryptoalg_subtype(struct crypto_tfm *tfm)
577{
578 struct crypto_alg *alg = tfm->__crt_alg;
579 struct chcr_alg_template *chcr_crypto_alg =
580 container_of(alg, struct chcr_alg_template, alg.crypto);
581
582 return chcr_crypto_alg->type & CRYPTO_ALG_SUB_TYPE_MASK;
583}
584
b8fd1f41
HJ
585static int cxgb4_is_crypto_q_full(struct net_device *dev, unsigned int idx)
586{
587 struct adapter *adap = netdev2adap(dev);
588 struct sge_uld_txq_info *txq_info =
589 adap->sge.uld_txq_info[CXGB4_TX_CRYPTO];
590 struct sge_uld_txq *txq;
591 int ret = 0;
592
593 local_bh_disable();
594 txq = &txq_info->uldtxq[idx];
595 spin_lock(&txq->sendq.lock);
596 if (txq->full)
597 ret = -1;
598 spin_unlock(&txq->sendq.lock);
599 local_bh_enable();
600 return ret;
601}
602
324429d7
HS
603static int generate_copy_rrkey(struct ablk_ctx *ablkctx,
604 struct _key_ctx *key_ctx)
605{
606 if (ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CBC) {
cc1b156d 607 memcpy(key_ctx->key, ablkctx->rrkey, ablkctx->enckey_len);
324429d7
HS
608 } else {
609 memcpy(key_ctx->key,
610 ablkctx->key + (ablkctx->enckey_len >> 1),
611 ablkctx->enckey_len >> 1);
cc1b156d
HJ
612 memcpy(key_ctx->key + (ablkctx->enckey_len >> 1),
613 ablkctx->rrkey, ablkctx->enckey_len >> 1);
324429d7
HS
614 }
615 return 0;
616}
5110e655
HJ
617
618static int chcr_hash_ent_in_wr(struct scatterlist *src,
619 unsigned int minsg,
620 unsigned int space,
621 unsigned int srcskip)
622{
623 int srclen = 0;
624 int srcsg = minsg;
625 int soffset = 0, sless;
626
627 if (sg_dma_len(src) == srcskip) {
628 src = sg_next(src);
629 srcskip = 0;
630 }
631 while (src && space > (sgl_ent_len[srcsg + 1])) {
632 sless = min_t(unsigned int, sg_dma_len(src) - soffset - srcskip,
633 CHCR_SRC_SG_SIZE);
634 srclen += sless;
635 soffset += sless;
636 srcsg++;
637 if (sg_dma_len(src) == (soffset + srcskip)) {
638 src = sg_next(src);
639 soffset = 0;
640 srcskip = 0;
641 }
642 }
643 return srclen;
644}
645
b8fd1f41
HJ
646static int chcr_sg_ent_in_wr(struct scatterlist *src,
647 struct scatterlist *dst,
648 unsigned int minsg,
2f47d580
HJ
649 unsigned int space,
650 unsigned int srcskip,
651 unsigned int dstskip)
b8fd1f41
HJ
652{
653 int srclen = 0, dstlen = 0;
2f47d580 654 int srcsg = minsg, dstsg = minsg;
1d693cf6 655 int offset = 0, soffset = 0, less, sless = 0;
b8fd1f41 656
2f47d580
HJ
657 if (sg_dma_len(src) == srcskip) {
658 src = sg_next(src);
659 srcskip = 0;
660 }
2f47d580
HJ
661 if (sg_dma_len(dst) == dstskip) {
662 dst = sg_next(dst);
663 dstskip = 0;
664 }
665
666 while (src && dst &&
b8fd1f41 667 space > (sgl_ent_len[srcsg + 1] + dsgl_ent_len[dstsg])) {
1d693cf6
HJ
668 sless = min_t(unsigned int, sg_dma_len(src) - srcskip - soffset,
669 CHCR_SRC_SG_SIZE);
670 srclen += sless;
b8fd1f41 671 srcsg++;
2956f36c 672 offset = 0;
b8fd1f41
HJ
673 while (dst && ((dstsg + 1) <= MAX_DSGL_ENT) &&
674 space > (sgl_ent_len[srcsg] + dsgl_ent_len[dstsg + 1])) {
675 if (srclen <= dstlen)
676 break;
2f47d580 677 less = min_t(unsigned int, sg_dma_len(dst) - offset -
db6deea4 678 dstskip, CHCR_DST_SG_SIZE);
2956f36c
HJ
679 dstlen += less;
680 offset += less;
1d693cf6 681 if ((offset + dstskip) == sg_dma_len(dst)) {
2956f36c
HJ
682 dst = sg_next(dst);
683 offset = 0;
684 }
b8fd1f41 685 dstsg++;
2f47d580 686 dstskip = 0;
b8fd1f41 687 }
1d693cf6
HJ
688 soffset += sless;
689 if ((soffset + srcskip) == sg_dma_len(src)) {
690 src = sg_next(src);
691 srcskip = 0;
692 soffset = 0;
693 }
694
b8fd1f41 695 }
b8fd1f41
HJ
696 return min(srclen, dstlen);
697}
698
28874f26 699static int chcr_cipher_fallback(struct crypto_sync_skcipher *cipher,
b8fd1f41
HJ
700 u32 flags,
701 struct scatterlist *src,
702 struct scatterlist *dst,
703 unsigned int nbytes,
704 u8 *iv,
705 unsigned short op_type)
706{
707 int err;
708
28874f26 709 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, cipher);
6faa0f57 710
28874f26 711 skcipher_request_set_sync_tfm(subreq, cipher);
b8fd1f41
HJ
712 skcipher_request_set_callback(subreq, flags, NULL, NULL);
713 skcipher_request_set_crypt(subreq, src, dst,
714 nbytes, iv);
715
716 err = op_type ? crypto_skcipher_decrypt(subreq) :
717 crypto_skcipher_encrypt(subreq);
718 skcipher_request_zero(subreq);
719
720 return err;
324429d7 721
b8fd1f41 722}
324429d7 723static inline void create_wreq(struct chcr_context *ctx,
358961d1 724 struct chcr_wr *chcr_req,
2f47d580
HJ
725 struct crypto_async_request *req,
726 unsigned int imm,
570265bf 727 int hash_sz,
2f47d580 728 unsigned int len16,
2512a624
HJ
729 unsigned int sc_len,
730 unsigned int lcb)
324429d7
HS
731{
732 struct uld_ctx *u_ctx = ULD_CTX(ctx);
72a56ca9 733 int qid = u_ctx->lldi.rxq_ids[ctx->rx_qidx];
324429d7 734
324429d7 735
570265bf 736 chcr_req->wreq.op_to_cctx_size = FILL_WR_OP_CCTX_SIZE;
358961d1 737 chcr_req->wreq.pld_size_hash_size =
570265bf 738 htonl(FW_CRYPTO_LOOKASIDE_WR_HASH_SIZE_V(hash_sz));
358961d1 739 chcr_req->wreq.len16_pkd =
2f47d580 740 htonl(FW_CRYPTO_LOOKASIDE_WR_LEN16_V(DIV_ROUND_UP(len16, 16)));
358961d1
HJ
741 chcr_req->wreq.cookie = cpu_to_be64((uintptr_t)req);
742 chcr_req->wreq.rx_chid_to_rx_q_id =
d5a4dfbd 743 FILL_WR_RX_Q_ID(ctx->tx_chan_id, qid,
570265bf 744 !!lcb, ctx->tx_qidx);
324429d7 745
add92a81 746 chcr_req->ulptx.cmd_dest = FILL_ULPTX_CMD_DEST(ctx->tx_chan_id,
8a13449f 747 qid);
2f47d580
HJ
748 chcr_req->ulptx.len = htonl((DIV_ROUND_UP(len16, 16) -
749 ((sizeof(chcr_req->wreq)) >> 4)));
324429d7 750
2f47d580 751 chcr_req->sc_imm.cmd_more = FILL_CMD_MORE(!imm);
358961d1 752 chcr_req->sc_imm.len = cpu_to_be32(sizeof(struct cpl_tx_sec_pdu) +
2f47d580 753 sizeof(chcr_req->key_ctx) + sc_len);
324429d7
HS
754}
755
756/**
757 * create_cipher_wr - form the WR for cipher operations
758 * @req: cipher req.
759 * @ctx: crypto driver context of the request.
760 * @qid: ingress qid where response of this WR should be received.
761 * @op_type: encryption or decryption
762 */
b8fd1f41 763static struct sk_buff *create_cipher_wr(struct cipher_wr_param *wrparam)
324429d7 764{
b8fd1f41 765 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(wrparam->req);
2f47d580 766 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(tfm));
324429d7 767 struct sk_buff *skb = NULL;
358961d1 768 struct chcr_wr *chcr_req;
324429d7 769 struct cpl_rx_phys_dsgl *phys_cpl;
2f47d580 770 struct ulptx_sgl *ulptx;
b8fd1f41
HJ
771 struct chcr_blkcipher_req_ctx *reqctx =
772 ablkcipher_request_ctx(wrparam->req);
2f47d580 773 unsigned int temp = 0, transhdr_len, dst_size;
b8fd1f41 774 int error;
2956f36c 775 int nents;
2f47d580 776 unsigned int kctx_len;
b8fd1f41
HJ
777 gfp_t flags = wrparam->req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ?
778 GFP_KERNEL : GFP_ATOMIC;
2f47d580 779 struct adapter *adap = padap(c_ctx(tfm)->dev);
324429d7 780
2f47d580
HJ
781 nents = sg_nents_xlen(reqctx->dstsg, wrparam->bytes, CHCR_DST_SG_SIZE,
782 reqctx->dst_ofst);
335bcc4a 783 dst_size = get_space_for_phys_dsgl(nents);
125d01ca 784 kctx_len = roundup(ablkctx->enckey_len, 16);
2f47d580
HJ
785 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
786 nents = sg_nents_xlen(reqctx->srcsg, wrparam->bytes,
787 CHCR_SRC_SG_SIZE, reqctx->src_ofst);
335bcc4a
HJ
788 temp = reqctx->imm ? roundup(wrparam->bytes, 16) :
789 (sgl_len(nents) * 8);
2f47d580 790 transhdr_len += temp;
125d01ca 791 transhdr_len = roundup(transhdr_len, 16);
2f47d580 792 skb = alloc_skb(SGE_MAX_WR_LEN, flags);
b8fd1f41
HJ
793 if (!skb) {
794 error = -ENOMEM;
795 goto err;
796 }
de77b966 797 chcr_req = __skb_put_zero(skb, transhdr_len);
358961d1 798 chcr_req->sec_cpl.op_ivinsrtofst =
d5a4dfbd 799 FILL_SEC_CPL_OP_IVINSR(c_ctx(tfm)->tx_chan_id, 2, 1);
358961d1 800
2f47d580 801 chcr_req->sec_cpl.pldlen = htonl(IV + wrparam->bytes);
358961d1 802 chcr_req->sec_cpl.aadstart_cipherstop_hi =
2f47d580 803 FILL_SEC_CPL_CIPHERSTOP_HI(0, 0, IV + 1, 0);
358961d1
HJ
804
805 chcr_req->sec_cpl.cipherstop_lo_authinsert =
806 FILL_SEC_CPL_AUTHINSERT(0, 0, 0, 0);
b8fd1f41 807 chcr_req->sec_cpl.seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(reqctx->op, 0,
324429d7 808 ablkctx->ciph_mode,
2f47d580 809 0, 0, IV >> 1);
358961d1 810 chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 0,
335bcc4a 811 0, 1, dst_size);
324429d7 812
358961d1 813 chcr_req->key_ctx.ctx_hdr = ablkctx->key_ctx_hdr;
b8fd1f41
HJ
814 if ((reqctx->op == CHCR_DECRYPT_OP) &&
815 (!(get_cryptoalg_subtype(crypto_ablkcipher_tfm(tfm)) ==
816 CRYPTO_ALG_SUB_TYPE_CTR)) &&
817 (!(get_cryptoalg_subtype(crypto_ablkcipher_tfm(tfm)) ==
818 CRYPTO_ALG_SUB_TYPE_CTR_RFC3686))) {
358961d1 819 generate_copy_rrkey(ablkctx, &chcr_req->key_ctx);
324429d7 820 } else {
b8fd1f41
HJ
821 if ((ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CBC) ||
822 (ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CTR)) {
358961d1
HJ
823 memcpy(chcr_req->key_ctx.key, ablkctx->key,
824 ablkctx->enckey_len);
324429d7 825 } else {
358961d1 826 memcpy(chcr_req->key_ctx.key, ablkctx->key +
324429d7
HS
827 (ablkctx->enckey_len >> 1),
828 ablkctx->enckey_len >> 1);
358961d1 829 memcpy(chcr_req->key_ctx.key +
324429d7
HS
830 (ablkctx->enckey_len >> 1),
831 ablkctx->key,
832 ablkctx->enckey_len >> 1);
833 }
834 }
358961d1 835 phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
2f47d580
HJ
836 ulptx = (struct ulptx_sgl *)((u8 *)(phys_cpl + 1) + dst_size);
837 chcr_add_cipher_src_ent(wrparam->req, ulptx, wrparam);
838 chcr_add_cipher_dst_ent(wrparam->req, phys_cpl, wrparam, wrparam->qid);
324429d7 839
ee0863ba 840 atomic_inc(&adap->chcr_stats.cipher_rqst);
335bcc4a
HJ
841 temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + kctx_len + IV
842 + (reqctx->imm ? (wrparam->bytes) : 0);
2f47d580
HJ
843 create_wreq(c_ctx(tfm), chcr_req, &(wrparam->req->base), reqctx->imm, 0,
844 transhdr_len, temp,
2512a624 845 ablkctx->ciph_mode == CHCR_SCMD_CIPHER_MODE_AES_CBC);
5c86a8ff 846 reqctx->skb = skb;
5fb78dba
HJ
847
848 if (reqctx->op && (ablkctx->ciph_mode ==
849 CHCR_SCMD_CIPHER_MODE_AES_CBC))
850 sg_pcopy_to_buffer(wrparam->req->src,
851 sg_nents(wrparam->req->src), wrparam->req->info, 16,
852 reqctx->processed + wrparam->bytes - AES_BLOCK_SIZE);
853
324429d7 854 return skb;
b8fd1f41
HJ
855err:
856 return ERR_PTR(error);
857}
858
859static inline int chcr_keyctx_ck_size(unsigned int keylen)
860{
861 int ck_size = 0;
862
863 if (keylen == AES_KEYSIZE_128)
864 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
865 else if (keylen == AES_KEYSIZE_192)
866 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
867 else if (keylen == AES_KEYSIZE_256)
868 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
869 else
870 ck_size = 0;
871
872 return ck_size;
873}
874static int chcr_cipher_fallback_setkey(struct crypto_ablkcipher *cipher,
875 const u8 *key,
876 unsigned int keylen)
877{
878 struct crypto_tfm *tfm = crypto_ablkcipher_tfm(cipher);
2f47d580 879 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
b8fd1f41
HJ
880 int err = 0;
881
28874f26
KC
882 crypto_sync_skcipher_clear_flags(ablkctx->sw_cipher,
883 CRYPTO_TFM_REQ_MASK);
884 crypto_sync_skcipher_set_flags(ablkctx->sw_cipher,
885 cipher->base.crt_flags & CRYPTO_TFM_REQ_MASK);
886 err = crypto_sync_skcipher_setkey(ablkctx->sw_cipher, key, keylen);
b8fd1f41
HJ
887 tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
888 tfm->crt_flags |=
28874f26 889 crypto_sync_skcipher_get_flags(ablkctx->sw_cipher) &
b8fd1f41
HJ
890 CRYPTO_TFM_RES_MASK;
891 return err;
324429d7
HS
892}
893
b8fd1f41
HJ
894static int chcr_aes_cbc_setkey(struct crypto_ablkcipher *cipher,
895 const u8 *key,
324429d7
HS
896 unsigned int keylen)
897{
2f47d580 898 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
324429d7
HS
899 unsigned int ck_size, context_size;
900 u16 alignment = 0;
b8fd1f41 901 int err;
324429d7 902
b8fd1f41
HJ
903 err = chcr_cipher_fallback_setkey(cipher, key, keylen);
904 if (err)
324429d7 905 goto badkey_err;
b8fd1f41
HJ
906
907 ck_size = chcr_keyctx_ck_size(keylen);
908 alignment = ck_size == CHCR_KEYCTX_CIPHER_KEY_SIZE_192 ? 8 : 0;
cc1b156d
HJ
909 memcpy(ablkctx->key, key, keylen);
910 ablkctx->enckey_len = keylen;
911 get_aes_decrypt_key(ablkctx->rrkey, ablkctx->key, keylen << 3);
324429d7
HS
912 context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD +
913 keylen + alignment) >> 4;
914
915 ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY,
916 0, 0, context_size);
917 ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CBC;
918 return 0;
919badkey_err:
b8fd1f41 920 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
324429d7 921 ablkctx->enckey_len = 0;
b8fd1f41
HJ
922
923 return err;
324429d7
HS
924}
925
b8fd1f41
HJ
926static int chcr_aes_ctr_setkey(struct crypto_ablkcipher *cipher,
927 const u8 *key,
928 unsigned int keylen)
324429d7 929{
2f47d580 930 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
b8fd1f41
HJ
931 unsigned int ck_size, context_size;
932 u16 alignment = 0;
933 int err;
934
935 err = chcr_cipher_fallback_setkey(cipher, key, keylen);
936 if (err)
937 goto badkey_err;
938 ck_size = chcr_keyctx_ck_size(keylen);
939 alignment = (ck_size == CHCR_KEYCTX_CIPHER_KEY_SIZE_192) ? 8 : 0;
940 memcpy(ablkctx->key, key, keylen);
941 ablkctx->enckey_len = keylen;
942 context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD +
943 keylen + alignment) >> 4;
944
945 ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY,
946 0, 0, context_size);
947 ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CTR;
948
949 return 0;
950badkey_err:
951 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
952 ablkctx->enckey_len = 0;
953
954 return err;
955}
956
957static int chcr_aes_rfc3686_setkey(struct crypto_ablkcipher *cipher,
958 const u8 *key,
959 unsigned int keylen)
960{
2f47d580 961 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
b8fd1f41
HJ
962 unsigned int ck_size, context_size;
963 u16 alignment = 0;
964 int err;
965
966 if (keylen < CTR_RFC3686_NONCE_SIZE)
967 return -EINVAL;
968 memcpy(ablkctx->nonce, key + (keylen - CTR_RFC3686_NONCE_SIZE),
969 CTR_RFC3686_NONCE_SIZE);
970
971 keylen -= CTR_RFC3686_NONCE_SIZE;
972 err = chcr_cipher_fallback_setkey(cipher, key, keylen);
973 if (err)
974 goto badkey_err;
975
976 ck_size = chcr_keyctx_ck_size(keylen);
977 alignment = (ck_size == CHCR_KEYCTX_CIPHER_KEY_SIZE_192) ? 8 : 0;
978 memcpy(ablkctx->key, key, keylen);
979 ablkctx->enckey_len = keylen;
980 context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD +
981 keylen + alignment) >> 4;
982
983 ablkctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY,
984 0, 0, context_size);
985 ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_CTR;
986
987 return 0;
988badkey_err:
989 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
990 ablkctx->enckey_len = 0;
991
992 return err;
993}
994static void ctr_add_iv(u8 *dstiv, u8 *srciv, u32 add)
995{
996 unsigned int size = AES_BLOCK_SIZE;
997 __be32 *b = (__be32 *)(dstiv + size);
998 u32 c, prev;
999
1000 memcpy(dstiv, srciv, AES_BLOCK_SIZE);
1001 for (; size >= 4; size -= 4) {
1002 prev = be32_to_cpu(*--b);
1003 c = prev + add;
1004 *b = cpu_to_be32(c);
1005 if (prev < c)
1006 break;
1007 add = 1;
1008 }
1009
1010}
1011
1012static unsigned int adjust_ctr_overflow(u8 *iv, u32 bytes)
1013{
1014 __be32 *b = (__be32 *)(iv + AES_BLOCK_SIZE);
1015 u64 c;
1016 u32 temp = be32_to_cpu(*--b);
1017
1018 temp = ~temp;
1019 c = (u64)temp + 1; // No of block can processed withou overflow
1020 if ((bytes / AES_BLOCK_SIZE) > c)
1021 bytes = c * AES_BLOCK_SIZE;
1022 return bytes;
1023}
1024
209897d5
HJ
1025static int chcr_update_tweak(struct ablkcipher_request *req, u8 *iv,
1026 u32 isfinal)
b8fd1f41
HJ
1027{
1028 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
2f47d580 1029 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(tfm));
b8fd1f41
HJ
1030 struct chcr_blkcipher_req_ctx *reqctx = ablkcipher_request_ctx(req);
1031 struct crypto_cipher *cipher;
1032 int ret, i;
1033 u8 *key;
1034 unsigned int keylen;
de1a00ac
HJ
1035 int round = reqctx->last_req_len / AES_BLOCK_SIZE;
1036 int round8 = round / 8;
b8fd1f41 1037
d3f1d2f7 1038 cipher = ablkctx->aes_generic;
de1a00ac 1039 memcpy(iv, reqctx->iv, AES_BLOCK_SIZE);
b8fd1f41 1040
b8fd1f41
HJ
1041 keylen = ablkctx->enckey_len / 2;
1042 key = ablkctx->key + keylen;
1043 ret = crypto_cipher_setkey(cipher, key, keylen);
1044 if (ret)
d3f1d2f7 1045 goto out;
335bcc4a 1046 crypto_cipher_encrypt_one(cipher, iv, iv);
de1a00ac
HJ
1047 for (i = 0; i < round8; i++)
1048 gf128mul_x8_ble((le128 *)iv, (le128 *)iv);
1049
1050 for (i = 0; i < (round % 8); i++)
b8fd1f41
HJ
1051 gf128mul_x_ble((le128 *)iv, (le128 *)iv);
1052
209897d5
HJ
1053 if (!isfinal)
1054 crypto_cipher_decrypt_one(cipher, iv, iv);
b8fd1f41
HJ
1055out:
1056 return ret;
1057}
1058
1059static int chcr_update_cipher_iv(struct ablkcipher_request *req,
1060 struct cpl_fw6_pld *fw6_pld, u8 *iv)
1061{
1062 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
1063 struct chcr_blkcipher_req_ctx *reqctx = ablkcipher_request_ctx(req);
1064 int subtype = get_cryptoalg_subtype(crypto_ablkcipher_tfm(tfm));
ab677ff4 1065 int ret = 0;
324429d7 1066
b8fd1f41
HJ
1067 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR)
1068 ctr_add_iv(iv, req->info, (reqctx->processed /
1069 AES_BLOCK_SIZE));
1070 else if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_RFC3686)
1071 *(__be32 *)(reqctx->iv + CTR_RFC3686_NONCE_SIZE +
1072 CTR_RFC3686_IV_SIZE) = cpu_to_be32((reqctx->processed /
1073 AES_BLOCK_SIZE) + 1);
1074 else if (subtype == CRYPTO_ALG_SUB_TYPE_XTS)
209897d5 1075 ret = chcr_update_tweak(req, iv, 0);
b8fd1f41
HJ
1076 else if (subtype == CRYPTO_ALG_SUB_TYPE_CBC) {
1077 if (reqctx->op)
5fb78dba
HJ
1078 /*Updated before sending last WR*/
1079 memcpy(iv, req->info, AES_BLOCK_SIZE);
b8fd1f41
HJ
1080 else
1081 memcpy(iv, &fw6_pld->data[2], AES_BLOCK_SIZE);
1082 }
1083
324429d7 1084 return ret;
b8fd1f41 1085
324429d7
HS
1086}
1087
b8fd1f41
HJ
1088/* We need separate function for final iv because in rfc3686 Initial counter
1089 * starts from 1 and buffer size of iv is 8 byte only which remains constant
1090 * for subsequent update requests
1091 */
1092
1093static int chcr_final_cipher_iv(struct ablkcipher_request *req,
1094 struct cpl_fw6_pld *fw6_pld, u8 *iv)
1095{
1096 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
1097 struct chcr_blkcipher_req_ctx *reqctx = ablkcipher_request_ctx(req);
1098 int subtype = get_cryptoalg_subtype(crypto_ablkcipher_tfm(tfm));
1099 int ret = 0;
1100
1101 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR)
1102 ctr_add_iv(iv, req->info, (reqctx->processed /
1103 AES_BLOCK_SIZE));
1104 else if (subtype == CRYPTO_ALG_SUB_TYPE_XTS)
209897d5 1105 ret = chcr_update_tweak(req, iv, 1);
b8fd1f41 1106 else if (subtype == CRYPTO_ALG_SUB_TYPE_CBC) {
5fb78dba
HJ
1107 /*Already updated for Decrypt*/
1108 if (!reqctx->op)
b8fd1f41
HJ
1109 memcpy(iv, &fw6_pld->data[2], AES_BLOCK_SIZE);
1110
1111 }
1112 return ret;
1113
1114}
1115
b8fd1f41
HJ
1116static int chcr_handle_cipher_resp(struct ablkcipher_request *req,
1117 unsigned char *input, int err)
324429d7
HS
1118{
1119 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
2f47d580
HJ
1120 struct uld_ctx *u_ctx = ULD_CTX(c_ctx(tfm));
1121 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(tfm));
324429d7 1122 struct sk_buff *skb;
b8fd1f41
HJ
1123 struct cpl_fw6_pld *fw6_pld = (struct cpl_fw6_pld *)input;
1124 struct chcr_blkcipher_req_ctx *reqctx = ablkcipher_request_ctx(req);
1125 struct cipher_wr_param wrparam;
fef4912b 1126 struct chcr_dev *dev = c_ctx(tfm)->dev;
b8fd1f41
HJ
1127 int bytes;
1128
b8fd1f41 1129 if (err)
2f47d580 1130 goto unmap;
b8fd1f41 1131 if (req->nbytes == reqctx->processed) {
2f47d580
HJ
1132 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev,
1133 req);
b8fd1f41
HJ
1134 err = chcr_final_cipher_iv(req, fw6_pld, req->info);
1135 goto complete;
1136 }
1137
2f47d580 1138 if (!reqctx->imm) {
335bcc4a 1139 bytes = chcr_sg_ent_in_wr(reqctx->srcsg, reqctx->dstsg, 0,
5110e655 1140 CIP_SPACE_LEFT(ablkctx->enckey_len),
2f47d580 1141 reqctx->src_ofst, reqctx->dst_ofst);
db6deea4
HJ
1142 if ((bytes + reqctx->processed) >= req->nbytes)
1143 bytes = req->nbytes - reqctx->processed;
1144 else
125d01ca 1145 bytes = rounddown(bytes, 16);
2f47d580
HJ
1146 } else {
1147 /*CTR mode counter overfloa*/
1148 bytes = req->nbytes - reqctx->processed;
1149 }
b8fd1f41
HJ
1150 err = chcr_update_cipher_iv(req, fw6_pld, reqctx->iv);
1151 if (err)
2f47d580 1152 goto unmap;
b8fd1f41
HJ
1153
1154 if (unlikely(bytes == 0)) {
2f47d580
HJ
1155 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev,
1156 req);
b8fd1f41
HJ
1157 err = chcr_cipher_fallback(ablkctx->sw_cipher,
1158 req->base.flags,
2f47d580
HJ
1159 req->src,
1160 req->dst,
1161 req->nbytes,
1162 req->info,
b8fd1f41
HJ
1163 reqctx->op);
1164 goto complete;
1165 }
1166
1167 if (get_cryptoalg_subtype(crypto_ablkcipher_tfm(tfm)) ==
1168 CRYPTO_ALG_SUB_TYPE_CTR)
1169 bytes = adjust_ctr_overflow(reqctx->iv, bytes);
2f47d580 1170 wrparam.qid = u_ctx->lldi.rxq_ids[c_ctx(tfm)->rx_qidx];
b8fd1f41
HJ
1171 wrparam.req = req;
1172 wrparam.bytes = bytes;
1173 skb = create_cipher_wr(&wrparam);
1174 if (IS_ERR(skb)) {
1175 pr_err("chcr : %s : Failed to form WR. No memory\n", __func__);
1176 err = PTR_ERR(skb);
2f47d580 1177 goto unmap;
b8fd1f41
HJ
1178 }
1179 skb->dev = u_ctx->lldi.ports[0];
2f47d580 1180 set_wr_txq(skb, CPL_PRIORITY_DATA, c_ctx(tfm)->tx_qidx);
b8fd1f41 1181 chcr_send_wr(skb);
2f47d580
HJ
1182 reqctx->last_req_len = bytes;
1183 reqctx->processed += bytes;
b8fd1f41 1184 return 0;
2f47d580
HJ
1185unmap:
1186 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev, req);
b8fd1f41 1187complete:
fef4912b 1188 chcr_dec_wrcount(dev);
b8fd1f41
HJ
1189 req->base.complete(&req->base, err);
1190 return err;
1191}
1192
1193static int process_cipher(struct ablkcipher_request *req,
1194 unsigned short qid,
1195 struct sk_buff **skb,
1196 unsigned short op_type)
1197{
1198 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
1199 unsigned int ivsize = crypto_ablkcipher_ivsize(tfm);
1200 struct chcr_blkcipher_req_ctx *reqctx = ablkcipher_request_ctx(req);
2f47d580 1201 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(tfm));
b8fd1f41 1202 struct cipher_wr_param wrparam;
2956f36c 1203 int bytes, err = -EINVAL;
b8fd1f41 1204
b8fd1f41
HJ
1205 reqctx->processed = 0;
1206 if (!req->info)
1207 goto error;
1208 if ((ablkctx->enckey_len == 0) || (ivsize > AES_BLOCK_SIZE) ||
1209 (req->nbytes == 0) ||
1210 (req->nbytes % crypto_ablkcipher_blocksize(tfm))) {
1211 pr_err("AES: Invalid value of Key Len %d nbytes %d IV Len %d\n",
1212 ablkctx->enckey_len, req->nbytes, ivsize);
1213 goto error;
1214 }
fef4912b
HJ
1215
1216 err = chcr_cipher_dma_map(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev, req);
1217 if (err)
1218 goto error;
2f47d580
HJ
1219 if (req->nbytes < (SGE_MAX_WR_LEN - (sizeof(struct chcr_wr) +
1220 AES_MIN_KEY_SIZE +
1221 sizeof(struct cpl_rx_phys_dsgl) +
1222 /*Min dsgl size*/
1223 32))) {
1224 /* Can be sent as Imm*/
1225 unsigned int dnents = 0, transhdr_len, phys_dsgl, kctx_len;
1226
1227 dnents = sg_nents_xlen(req->dst, req->nbytes,
1228 CHCR_DST_SG_SIZE, 0);
2f47d580 1229 phys_dsgl = get_space_for_phys_dsgl(dnents);
125d01ca 1230 kctx_len = roundup(ablkctx->enckey_len, 16);
2f47d580
HJ
1231 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, phys_dsgl);
1232 reqctx->imm = (transhdr_len + IV + req->nbytes) <=
1233 SGE_MAX_WR_LEN;
1234 bytes = IV + req->nbytes;
1235
1236 } else {
1237 reqctx->imm = 0;
1238 }
1239
1240 if (!reqctx->imm) {
335bcc4a 1241 bytes = chcr_sg_ent_in_wr(req->src, req->dst, 0,
5110e655 1242 CIP_SPACE_LEFT(ablkctx->enckey_len),
2f47d580 1243 0, 0);
db6deea4
HJ
1244 if ((bytes + reqctx->processed) >= req->nbytes)
1245 bytes = req->nbytes - reqctx->processed;
1246 else
125d01ca 1247 bytes = rounddown(bytes, 16);
2f47d580 1248 } else {
b8fd1f41 1249 bytes = req->nbytes;
2f47d580 1250 }
b8fd1f41 1251 if (get_cryptoalg_subtype(crypto_ablkcipher_tfm(tfm)) ==
db6deea4 1252 CRYPTO_ALG_SUB_TYPE_CTR) {
b8fd1f41
HJ
1253 bytes = adjust_ctr_overflow(req->info, bytes);
1254 }
1255 if (get_cryptoalg_subtype(crypto_ablkcipher_tfm(tfm)) ==
1256 CRYPTO_ALG_SUB_TYPE_CTR_RFC3686) {
1257 memcpy(reqctx->iv, ablkctx->nonce, CTR_RFC3686_NONCE_SIZE);
1258 memcpy(reqctx->iv + CTR_RFC3686_NONCE_SIZE, req->info,
1259 CTR_RFC3686_IV_SIZE);
1260
1261 /* initialize counter portion of counter block */
1262 *(__be32 *)(reqctx->iv + CTR_RFC3686_NONCE_SIZE +
1263 CTR_RFC3686_IV_SIZE) = cpu_to_be32(1);
1264
1265 } else {
1266
2f47d580 1267 memcpy(reqctx->iv, req->info, IV);
b8fd1f41
HJ
1268 }
1269 if (unlikely(bytes == 0)) {
2f47d580
HJ
1270 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev,
1271 req);
b8fd1f41
HJ
1272 err = chcr_cipher_fallback(ablkctx->sw_cipher,
1273 req->base.flags,
1274 req->src,
1275 req->dst,
1276 req->nbytes,
7ffb9118 1277 reqctx->iv,
b8fd1f41
HJ
1278 op_type);
1279 goto error;
1280 }
b8fd1f41 1281 reqctx->op = op_type;
2f47d580
HJ
1282 reqctx->srcsg = req->src;
1283 reqctx->dstsg = req->dst;
1284 reqctx->src_ofst = 0;
1285 reqctx->dst_ofst = 0;
b8fd1f41
HJ
1286 wrparam.qid = qid;
1287 wrparam.req = req;
1288 wrparam.bytes = bytes;
1289 *skb = create_cipher_wr(&wrparam);
1290 if (IS_ERR(*skb)) {
1291 err = PTR_ERR(*skb);
2f47d580 1292 goto unmap;
b8fd1f41 1293 }
2f47d580
HJ
1294 reqctx->processed = bytes;
1295 reqctx->last_req_len = bytes;
b8fd1f41
HJ
1296
1297 return 0;
2f47d580
HJ
1298unmap:
1299 chcr_cipher_dma_unmap(&ULD_CTX(c_ctx(tfm))->lldi.pdev->dev, req);
b8fd1f41
HJ
1300error:
1301 return err;
1302}
1303
1304static int chcr_aes_encrypt(struct ablkcipher_request *req)
1305{
1306 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
fef4912b 1307 struct chcr_dev *dev = c_ctx(tfm)->dev;
b8fd1f41 1308 struct sk_buff *skb = NULL;
6faa0f57 1309 int err, isfull = 0;
2f47d580 1310 struct uld_ctx *u_ctx = ULD_CTX(c_ctx(tfm));
324429d7 1311
fef4912b
HJ
1312 err = chcr_inc_wrcount(dev);
1313 if (err)
1314 return -ENXIO;
324429d7 1315 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
2f47d580 1316 c_ctx(tfm)->tx_qidx))) {
6faa0f57 1317 isfull = 1;
fef4912b
HJ
1318 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
1319 err = -ENOSPC;
1320 goto error;
1321 }
324429d7
HS
1322 }
1323
2f47d580
HJ
1324 err = process_cipher(req, u_ctx->lldi.rxq_ids[c_ctx(tfm)->rx_qidx],
1325 &skb, CHCR_ENCRYPT_OP);
b8fd1f41
HJ
1326 if (err || !skb)
1327 return err;
324429d7 1328 skb->dev = u_ctx->lldi.ports[0];
2f47d580 1329 set_wr_txq(skb, CPL_PRIORITY_DATA, c_ctx(tfm)->tx_qidx);
324429d7 1330 chcr_send_wr(skb);
6faa0f57 1331 return isfull ? -EBUSY : -EINPROGRESS;
fef4912b
HJ
1332error:
1333 chcr_dec_wrcount(dev);
1334 return err;
324429d7
HS
1335}
1336
1337static int chcr_aes_decrypt(struct ablkcipher_request *req)
1338{
1339 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(req);
2f47d580 1340 struct uld_ctx *u_ctx = ULD_CTX(c_ctx(tfm));
fef4912b 1341 struct chcr_dev *dev = c_ctx(tfm)->dev;
b8fd1f41 1342 struct sk_buff *skb = NULL;
6faa0f57 1343 int err, isfull = 0;
324429d7 1344
fef4912b
HJ
1345 err = chcr_inc_wrcount(dev);
1346 if (err)
1347 return -ENXIO;
1348
324429d7 1349 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
2f47d580 1350 c_ctx(tfm)->tx_qidx))) {
6faa0f57 1351 isfull = 1;
324429d7 1352 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG))
6faa0f57 1353 return -ENOSPC;
324429d7
HS
1354 }
1355
fc6176a2
CIK
1356 err = process_cipher(req, u_ctx->lldi.rxq_ids[c_ctx(tfm)->rx_qidx],
1357 &skb, CHCR_DECRYPT_OP);
b8fd1f41
HJ
1358 if (err || !skb)
1359 return err;
324429d7 1360 skb->dev = u_ctx->lldi.ports[0];
2f47d580 1361 set_wr_txq(skb, CPL_PRIORITY_DATA, c_ctx(tfm)->tx_qidx);
324429d7 1362 chcr_send_wr(skb);
6faa0f57 1363 return isfull ? -EBUSY : -EINPROGRESS;
324429d7
HS
1364}
1365
1366static int chcr_device_init(struct chcr_context *ctx)
1367{
14c19b17 1368 struct uld_ctx *u_ctx = NULL;
72a56ca9 1369 struct adapter *adap;
324429d7 1370 unsigned int id;
72a56ca9 1371 int txq_perchan, txq_idx, ntxq;
324429d7
HS
1372 int err = 0, rxq_perchan, rxq_idx;
1373
1374 id = smp_processor_id();
1375 if (!ctx->dev) {
14c19b17
HJ
1376 u_ctx = assign_chcr_device();
1377 if (!u_ctx) {
fef4912b 1378 err = -ENXIO;
324429d7
HS
1379 pr_err("chcr device assignment fails\n");
1380 goto out;
1381 }
fef4912b 1382 ctx->dev = &u_ctx->dev;
72a56ca9 1383 adap = padap(ctx->dev);
a1c6fd43 1384 ntxq = u_ctx->lldi.ntxq;
324429d7 1385 rxq_perchan = u_ctx->lldi.nrxq / u_ctx->lldi.nchan;
72a56ca9 1386 txq_perchan = ntxq / u_ctx->lldi.nchan;
324429d7 1387 spin_lock(&ctx->dev->lock_chcr_dev);
add92a81 1388 ctx->tx_chan_id = ctx->dev->tx_channel_id;
ab677ff4 1389 ctx->dev->tx_channel_id = !ctx->dev->tx_channel_id;
324429d7 1390 spin_unlock(&ctx->dev->lock_chcr_dev);
add92a81
HJ
1391 rxq_idx = ctx->tx_chan_id * rxq_perchan;
1392 rxq_idx += id % rxq_perchan;
1393 txq_idx = ctx->tx_chan_id * txq_perchan;
1394 txq_idx += id % txq_perchan;
1395 ctx->rx_qidx = rxq_idx;
1396 ctx->tx_qidx = txq_idx;
1397 /* Channel Id used by SGE to forward packet to Host.
1398 * Same value should be used in cpl_fw6_pld RSS_CH field
1399 * by FW. Driver programs PCI channel ID to be used in fw
1400 * at the time of queue allocation with value "pi->tx_chan"
1401 */
1402 ctx->pci_chan_id = txq_idx / txq_perchan;
324429d7
HS
1403 }
1404out:
1405 return err;
1406}
1407
1408static int chcr_cra_init(struct crypto_tfm *tfm)
1409{
b8fd1f41
HJ
1410 struct crypto_alg *alg = tfm->__crt_alg;
1411 struct chcr_context *ctx = crypto_tfm_ctx(tfm);
1412 struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
1413
28874f26
KC
1414 ablkctx->sw_cipher = crypto_alloc_sync_skcipher(alg->cra_name, 0,
1415 CRYPTO_ALG_NEED_FALLBACK);
b8fd1f41
HJ
1416 if (IS_ERR(ablkctx->sw_cipher)) {
1417 pr_err("failed to allocate fallback for %s\n", alg->cra_name);
1418 return PTR_ERR(ablkctx->sw_cipher);
1419 }
d3f1d2f7
HJ
1420
1421 if (get_cryptoalg_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_XTS) {
1422 /* To update tweak*/
1423 ablkctx->aes_generic = crypto_alloc_cipher("aes-generic", 0, 0);
1424 if (IS_ERR(ablkctx->aes_generic)) {
1425 pr_err("failed to allocate aes cipher for tweak\n");
1426 return PTR_ERR(ablkctx->aes_generic);
1427 }
1428 } else
1429 ablkctx->aes_generic = NULL;
1430
324429d7
HS
1431 tfm->crt_ablkcipher.reqsize = sizeof(struct chcr_blkcipher_req_ctx);
1432 return chcr_device_init(crypto_tfm_ctx(tfm));
1433}
1434
b8fd1f41
HJ
1435static int chcr_rfc3686_init(struct crypto_tfm *tfm)
1436{
1437 struct crypto_alg *alg = tfm->__crt_alg;
1438 struct chcr_context *ctx = crypto_tfm_ctx(tfm);
1439 struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
1440
1441 /*RFC3686 initialises IV counter value to 1, rfc3686(ctr(aes))
1442 * cannot be used as fallback in chcr_handle_cipher_response
1443 */
28874f26
KC
1444 ablkctx->sw_cipher = crypto_alloc_sync_skcipher("ctr(aes)", 0,
1445 CRYPTO_ALG_NEED_FALLBACK);
b8fd1f41
HJ
1446 if (IS_ERR(ablkctx->sw_cipher)) {
1447 pr_err("failed to allocate fallback for %s\n", alg->cra_name);
1448 return PTR_ERR(ablkctx->sw_cipher);
1449 }
1450 tfm->crt_ablkcipher.reqsize = sizeof(struct chcr_blkcipher_req_ctx);
1451 return chcr_device_init(crypto_tfm_ctx(tfm));
1452}
1453
1454
1455static void chcr_cra_exit(struct crypto_tfm *tfm)
1456{
1457 struct chcr_context *ctx = crypto_tfm_ctx(tfm);
1458 struct ablk_ctx *ablkctx = ABLK_CTX(ctx);
1459
28874f26 1460 crypto_free_sync_skcipher(ablkctx->sw_cipher);
d3f1d2f7
HJ
1461 if (ablkctx->aes_generic)
1462 crypto_free_cipher(ablkctx->aes_generic);
b8fd1f41
HJ
1463}
1464
324429d7
HS
1465static int get_alg_config(struct algo_param *params,
1466 unsigned int auth_size)
1467{
1468 switch (auth_size) {
1469 case SHA1_DIGEST_SIZE:
1470 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_160;
1471 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA1;
1472 params->result_size = SHA1_DIGEST_SIZE;
1473 break;
1474 case SHA224_DIGEST_SIZE:
1475 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
1476 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA224;
1477 params->result_size = SHA256_DIGEST_SIZE;
1478 break;
1479 case SHA256_DIGEST_SIZE:
1480 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
1481 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA256;
1482 params->result_size = SHA256_DIGEST_SIZE;
1483 break;
1484 case SHA384_DIGEST_SIZE:
1485 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_512;
1486 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA512_384;
1487 params->result_size = SHA512_DIGEST_SIZE;
1488 break;
1489 case SHA512_DIGEST_SIZE:
1490 params->mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_512;
1491 params->auth_mode = CHCR_SCMD_AUTH_MODE_SHA512_512;
1492 params->result_size = SHA512_DIGEST_SIZE;
1493 break;
1494 default:
1495 pr_err("chcr : ERROR, unsupported digest size\n");
1496 return -EINVAL;
1497 }
1498 return 0;
1499}
1500
e7922729 1501static inline void chcr_free_shash(struct crypto_shash *base_hash)
324429d7 1502{
e7922729 1503 crypto_free_shash(base_hash);
324429d7
HS
1504}
1505
1506/**
358961d1 1507 * create_hash_wr - Create hash work request
324429d7
HS
1508 * @req - Cipher req base
1509 */
358961d1 1510static struct sk_buff *create_hash_wr(struct ahash_request *req,
2debd332 1511 struct hash_wr_param *param)
324429d7
HS
1512{
1513 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
1514 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2f47d580 1515 struct hmac_ctx *hmacctx = HMAC_CTX(h_ctx(tfm));
324429d7 1516 struct sk_buff *skb = NULL;
2f47d580 1517 struct uld_ctx *u_ctx = ULD_CTX(h_ctx(tfm));
358961d1 1518 struct chcr_wr *chcr_req;
2f47d580 1519 struct ulptx_sgl *ulptx;
5110e655
HJ
1520 unsigned int nents = 0, transhdr_len;
1521 unsigned int temp = 0;
358961d1
HJ
1522 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
1523 GFP_ATOMIC;
2f47d580
HJ
1524 struct adapter *adap = padap(h_ctx(tfm)->dev);
1525 int error = 0;
324429d7 1526
5110e655
HJ
1527 transhdr_len = HASH_TRANSHDR_SIZE(param->kctx_len);
1528 req_ctx->hctx_wr.imm = (transhdr_len + param->bfr_len +
1529 param->sg_len) <= SGE_MAX_WR_LEN;
1530 nents = sg_nents_xlen(req_ctx->hctx_wr.srcsg, param->sg_len,
1531 CHCR_SRC_SG_SIZE, req_ctx->hctx_wr.src_ofst);
2f47d580 1532 nents += param->bfr_len ? 1 : 0;
5110e655
HJ
1533 transhdr_len += req_ctx->hctx_wr.imm ? roundup(param->bfr_len +
1534 param->sg_len, 16) : (sgl_len(nents) * 8);
125d01ca 1535 transhdr_len = roundup(transhdr_len, 16);
2f47d580 1536
5110e655 1537 skb = alloc_skb(transhdr_len, flags);
324429d7 1538 if (!skb)
2f47d580 1539 return ERR_PTR(-ENOMEM);
de77b966 1540 chcr_req = __skb_put_zero(skb, transhdr_len);
324429d7 1541
358961d1 1542 chcr_req->sec_cpl.op_ivinsrtofst =
d5a4dfbd 1543 FILL_SEC_CPL_OP_IVINSR(h_ctx(tfm)->tx_chan_id, 2, 0);
358961d1 1544 chcr_req->sec_cpl.pldlen = htonl(param->bfr_len + param->sg_len);
324429d7 1545
358961d1 1546 chcr_req->sec_cpl.aadstart_cipherstop_hi =
324429d7 1547 FILL_SEC_CPL_CIPHERSTOP_HI(0, 0, 0, 0);
358961d1 1548 chcr_req->sec_cpl.cipherstop_lo_authinsert =
324429d7 1549 FILL_SEC_CPL_AUTHINSERT(0, 1, 0, 0);
358961d1 1550 chcr_req->sec_cpl.seqno_numivs =
324429d7 1551 FILL_SEC_CPL_SCMD0_SEQNO(0, 0, 0, param->alg_prm.auth_mode,
358961d1 1552 param->opad_needed, 0);
324429d7 1553
358961d1 1554 chcr_req->sec_cpl.ivgen_hdrlen =
324429d7
HS
1555 FILL_SEC_CPL_IVGEN_HDRLEN(param->last, param->more, 0, 1, 0, 0);
1556
358961d1
HJ
1557 memcpy(chcr_req->key_ctx.key, req_ctx->partial_hash,
1558 param->alg_prm.result_size);
324429d7
HS
1559
1560 if (param->opad_needed)
358961d1
HJ
1561 memcpy(chcr_req->key_ctx.key +
1562 ((param->alg_prm.result_size <= 32) ? 32 :
1563 CHCR_HASH_MAX_DIGEST_SIZE),
324429d7
HS
1564 hmacctx->opad, param->alg_prm.result_size);
1565
358961d1 1566 chcr_req->key_ctx.ctx_hdr = FILL_KEY_CTX_HDR(CHCR_KEYCTX_NO_KEY,
324429d7
HS
1567 param->alg_prm.mk_size, 0,
1568 param->opad_needed,
5110e655 1569 ((param->kctx_len +
358961d1
HJ
1570 sizeof(chcr_req->key_ctx)) >> 4));
1571 chcr_req->sec_cpl.scmd1 = cpu_to_be64((u64)param->scmd1);
5110e655 1572 ulptx = (struct ulptx_sgl *)((u8 *)(chcr_req + 1) + param->kctx_len +
2f47d580
HJ
1573 DUMMY_BYTES);
1574 if (param->bfr_len != 0) {
5110e655
HJ
1575 req_ctx->hctx_wr.dma_addr =
1576 dma_map_single(&u_ctx->lldi.pdev->dev, req_ctx->reqbfr,
1577 param->bfr_len, DMA_TO_DEVICE);
2f47d580 1578 if (dma_mapping_error(&u_ctx->lldi.pdev->dev,
5110e655 1579 req_ctx->hctx_wr. dma_addr)) {
2f47d580
HJ
1580 error = -ENOMEM;
1581 goto err;
1582 }
5110e655 1583 req_ctx->hctx_wr.dma_len = param->bfr_len;
2f47d580 1584 } else {
5110e655 1585 req_ctx->hctx_wr.dma_addr = 0;
2f47d580
HJ
1586 }
1587 chcr_add_hash_src_ent(req, ulptx, param);
1588 /* Request upto max wr size */
5110e655
HJ
1589 temp = param->kctx_len + DUMMY_BYTES + (req_ctx->hctx_wr.imm ?
1590 (param->sg_len + param->bfr_len) : 0);
ee0863ba 1591 atomic_inc(&adap->chcr_stats.digest_rqst);
5110e655
HJ
1592 create_wreq(h_ctx(tfm), chcr_req, &req->base, req_ctx->hctx_wr.imm,
1593 param->hash_size, transhdr_len,
2f47d580 1594 temp, 0);
5110e655 1595 req_ctx->hctx_wr.skb = skb;
324429d7 1596 return skb;
2f47d580
HJ
1597err:
1598 kfree_skb(skb);
1599 return ERR_PTR(error);
324429d7
HS
1600}
1601
1602static int chcr_ahash_update(struct ahash_request *req)
1603{
1604 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
1605 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
324429d7 1606 struct uld_ctx *u_ctx = NULL;
fef4912b 1607 struct chcr_dev *dev = h_ctx(rtfm)->dev;
324429d7
HS
1608 struct sk_buff *skb;
1609 u8 remainder = 0, bs;
1610 unsigned int nbytes = req->nbytes;
1611 struct hash_wr_param params;
6faa0f57 1612 int error, isfull = 0;
324429d7
HS
1613
1614 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
2f47d580 1615 u_ctx = ULD_CTX(h_ctx(rtfm));
324429d7 1616
44fce12a
HJ
1617 if (nbytes + req_ctx->reqlen >= bs) {
1618 remainder = (nbytes + req_ctx->reqlen) % bs;
1619 nbytes = nbytes + req_ctx->reqlen - remainder;
324429d7 1620 } else {
44fce12a
HJ
1621 sg_pcopy_to_buffer(req->src, sg_nents(req->src), req_ctx->reqbfr
1622 + req_ctx->reqlen, nbytes, 0);
1623 req_ctx->reqlen += nbytes;
324429d7
HS
1624 return 0;
1625 }
fef4912b
HJ
1626 error = chcr_inc_wrcount(dev);
1627 if (error)
1628 return -ENXIO;
1629 /* Detach state for CHCR means lldi or padap is freed. Increasing
1630 * inflight count for dev guarantees that lldi and padap is valid
1631 */
1632 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
1633 h_ctx(rtfm)->tx_qidx))) {
1634 isfull = 1;
1635 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
1636 error = -ENOSPC;
1637 goto err;
1638 }
1639 }
1640
5110e655 1641 chcr_init_hctx_per_wr(req_ctx);
2f47d580 1642 error = chcr_hash_dma_map(&u_ctx->lldi.pdev->dev, req);
fef4912b
HJ
1643 if (error) {
1644 error = -ENOMEM;
1645 goto err;
1646 }
5110e655
HJ
1647 get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
1648 params.kctx_len = roundup(params.alg_prm.result_size, 16);
1649 params.sg_len = chcr_hash_ent_in_wr(req->src, !!req_ctx->reqlen,
1650 HASH_SPACE_LEFT(params.kctx_len), 0);
1651 if (params.sg_len > req->nbytes)
1652 params.sg_len = req->nbytes;
1653 params.sg_len = rounddown(params.sg_len + req_ctx->reqlen, bs) -
1654 req_ctx->reqlen;
324429d7
HS
1655 params.opad_needed = 0;
1656 params.more = 1;
1657 params.last = 0;
44fce12a 1658 params.bfr_len = req_ctx->reqlen;
324429d7 1659 params.scmd1 = 0;
5110e655
HJ
1660 req_ctx->hctx_wr.srcsg = req->src;
1661
1662 params.hash_size = params.alg_prm.result_size;
324429d7 1663 req_ctx->data_len += params.sg_len + params.bfr_len;
358961d1 1664 skb = create_hash_wr(req, &params);
2f47d580
HJ
1665 if (IS_ERR(skb)) {
1666 error = PTR_ERR(skb);
1667 goto unmap;
1668 }
324429d7 1669
5110e655 1670 req_ctx->hctx_wr.processed += params.sg_len;
44fce12a 1671 if (remainder) {
44fce12a 1672 /* Swap buffers */
abfa2b37 1673 swap(req_ctx->reqbfr, req_ctx->skbfr);
324429d7 1674 sg_pcopy_to_buffer(req->src, sg_nents(req->src),
44fce12a 1675 req_ctx->reqbfr, remainder, req->nbytes -
324429d7 1676 remainder);
44fce12a
HJ
1677 }
1678 req_ctx->reqlen = remainder;
324429d7 1679 skb->dev = u_ctx->lldi.ports[0];
2f47d580 1680 set_wr_txq(skb, CPL_PRIORITY_DATA, h_ctx(rtfm)->tx_qidx);
324429d7
HS
1681 chcr_send_wr(skb);
1682
6faa0f57 1683 return isfull ? -EBUSY : -EINPROGRESS;
2f47d580
HJ
1684unmap:
1685 chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req);
fef4912b
HJ
1686err:
1687 chcr_dec_wrcount(dev);
2f47d580 1688 return error;
324429d7
HS
1689}
1690
1691static void create_last_hash_block(char *bfr_ptr, unsigned int bs, u64 scmd1)
1692{
1693 memset(bfr_ptr, 0, bs);
1694 *bfr_ptr = 0x80;
1695 if (bs == 64)
1696 *(__be64 *)(bfr_ptr + 56) = cpu_to_be64(scmd1 << 3);
1697 else
1698 *(__be64 *)(bfr_ptr + 120) = cpu_to_be64(scmd1 << 3);
1699}
1700
1701static int chcr_ahash_final(struct ahash_request *req)
1702{
1703 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
1704 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
fef4912b 1705 struct chcr_dev *dev = h_ctx(rtfm)->dev;
324429d7
HS
1706 struct hash_wr_param params;
1707 struct sk_buff *skb;
1708 struct uld_ctx *u_ctx = NULL;
1709 u8 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
fef4912b
HJ
1710 int error = -EINVAL;
1711
1712 error = chcr_inc_wrcount(dev);
1713 if (error)
1714 return -ENXIO;
324429d7 1715
5110e655 1716 chcr_init_hctx_per_wr(req_ctx);
2f47d580 1717 u_ctx = ULD_CTX(h_ctx(rtfm));
324429d7
HS
1718 if (is_hmac(crypto_ahash_tfm(rtfm)))
1719 params.opad_needed = 1;
1720 else
1721 params.opad_needed = 0;
1722 params.sg_len = 0;
5110e655 1723 req_ctx->hctx_wr.isfinal = 1;
324429d7 1724 get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
5110e655
HJ
1725 params.kctx_len = roundup(params.alg_prm.result_size, 16);
1726 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1727 params.opad_needed = 1;
1728 params.kctx_len *= 2;
1729 } else {
1730 params.opad_needed = 0;
1731 }
1732
1733 req_ctx->hctx_wr.result = 1;
44fce12a 1734 params.bfr_len = req_ctx->reqlen;
324429d7 1735 req_ctx->data_len += params.bfr_len + params.sg_len;
5110e655 1736 req_ctx->hctx_wr.srcsg = req->src;
44fce12a
HJ
1737 if (req_ctx->reqlen == 0) {
1738 create_last_hash_block(req_ctx->reqbfr, bs, req_ctx->data_len);
324429d7
HS
1739 params.last = 0;
1740 params.more = 1;
1741 params.scmd1 = 0;
1742 params.bfr_len = bs;
1743
1744 } else {
1745 params.scmd1 = req_ctx->data_len;
1746 params.last = 1;
1747 params.more = 0;
1748 }
5110e655 1749 params.hash_size = crypto_ahash_digestsize(rtfm);
358961d1 1750 skb = create_hash_wr(req, &params);
fef4912b
HJ
1751 if (IS_ERR(skb)) {
1752 error = PTR_ERR(skb);
1753 goto err;
1754 }
5110e655 1755 req_ctx->reqlen = 0;
324429d7 1756 skb->dev = u_ctx->lldi.ports[0];
2f47d580 1757 set_wr_txq(skb, CPL_PRIORITY_DATA, h_ctx(rtfm)->tx_qidx);
324429d7
HS
1758 chcr_send_wr(skb);
1759 return -EINPROGRESS;
fef4912b
HJ
1760err:
1761 chcr_dec_wrcount(dev);
1762 return error;
324429d7
HS
1763}
1764
1765static int chcr_ahash_finup(struct ahash_request *req)
1766{
1767 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
1768 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
fef4912b 1769 struct chcr_dev *dev = h_ctx(rtfm)->dev;
324429d7
HS
1770 struct uld_ctx *u_ctx = NULL;
1771 struct sk_buff *skb;
1772 struct hash_wr_param params;
1773 u8 bs;
6faa0f57 1774 int error, isfull = 0;
324429d7
HS
1775
1776 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
2f47d580 1777 u_ctx = ULD_CTX(h_ctx(rtfm));
fef4912b
HJ
1778 error = chcr_inc_wrcount(dev);
1779 if (error)
1780 return -ENXIO;
324429d7
HS
1781
1782 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
2f47d580 1783 h_ctx(rtfm)->tx_qidx))) {
6faa0f57 1784 isfull = 1;
fef4912b
HJ
1785 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
1786 error = -ENOSPC;
1787 goto err;
1788 }
324429d7 1789 }
5110e655
HJ
1790 chcr_init_hctx_per_wr(req_ctx);
1791 error = chcr_hash_dma_map(&u_ctx->lldi.pdev->dev, req);
fef4912b
HJ
1792 if (error) {
1793 error = -ENOMEM;
1794 goto err;
1795 }
324429d7 1796
5110e655
HJ
1797 get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
1798 params.kctx_len = roundup(params.alg_prm.result_size, 16);
1799 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1800 params.kctx_len *= 2;
324429d7 1801 params.opad_needed = 1;
5110e655 1802 } else {
324429d7 1803 params.opad_needed = 0;
5110e655 1804 }
324429d7 1805
5110e655
HJ
1806 params.sg_len = chcr_hash_ent_in_wr(req->src, !!req_ctx->reqlen,
1807 HASH_SPACE_LEFT(params.kctx_len), 0);
1808 if (params.sg_len < req->nbytes) {
1809 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1810 params.kctx_len /= 2;
1811 params.opad_needed = 0;
1812 }
1813 params.last = 0;
1814 params.more = 1;
1815 params.sg_len = rounddown(params.sg_len + req_ctx->reqlen, bs)
1816 - req_ctx->reqlen;
1817 params.hash_size = params.alg_prm.result_size;
1818 params.scmd1 = 0;
1819 } else {
1820 params.last = 1;
1821 params.more = 0;
1822 params.sg_len = req->nbytes;
1823 params.hash_size = crypto_ahash_digestsize(rtfm);
1824 params.scmd1 = req_ctx->data_len + req_ctx->reqlen +
1825 params.sg_len;
1826 }
44fce12a 1827 params.bfr_len = req_ctx->reqlen;
324429d7 1828 req_ctx->data_len += params.bfr_len + params.sg_len;
5110e655
HJ
1829 req_ctx->hctx_wr.result = 1;
1830 req_ctx->hctx_wr.srcsg = req->src;
44fce12a
HJ
1831 if ((req_ctx->reqlen + req->nbytes) == 0) {
1832 create_last_hash_block(req_ctx->reqbfr, bs, req_ctx->data_len);
324429d7
HS
1833 params.last = 0;
1834 params.more = 1;
1835 params.scmd1 = 0;
1836 params.bfr_len = bs;
324429d7 1837 }
358961d1 1838 skb = create_hash_wr(req, &params);
2f47d580
HJ
1839 if (IS_ERR(skb)) {
1840 error = PTR_ERR(skb);
1841 goto unmap;
1842 }
5110e655
HJ
1843 req_ctx->reqlen = 0;
1844 req_ctx->hctx_wr.processed += params.sg_len;
324429d7 1845 skb->dev = u_ctx->lldi.ports[0];
2f47d580 1846 set_wr_txq(skb, CPL_PRIORITY_DATA, h_ctx(rtfm)->tx_qidx);
324429d7
HS
1847 chcr_send_wr(skb);
1848
6faa0f57 1849 return isfull ? -EBUSY : -EINPROGRESS;
2f47d580
HJ
1850unmap:
1851 chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req);
fef4912b
HJ
1852err:
1853 chcr_dec_wrcount(dev);
2f47d580 1854 return error;
324429d7
HS
1855}
1856
1857static int chcr_ahash_digest(struct ahash_request *req)
1858{
1859 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
1860 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
fef4912b 1861 struct chcr_dev *dev = h_ctx(rtfm)->dev;
324429d7
HS
1862 struct uld_ctx *u_ctx = NULL;
1863 struct sk_buff *skb;
1864 struct hash_wr_param params;
1865 u8 bs;
6faa0f57 1866 int error, isfull = 0;
324429d7
HS
1867
1868 rtfm->init(req);
1869 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
fef4912b
HJ
1870 error = chcr_inc_wrcount(dev);
1871 if (error)
1872 return -ENXIO;
324429d7 1873
2f47d580 1874 u_ctx = ULD_CTX(h_ctx(rtfm));
324429d7 1875 if (unlikely(cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
2f47d580 1876 h_ctx(rtfm)->tx_qidx))) {
6faa0f57 1877 isfull = 1;
fef4912b
HJ
1878 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
1879 error = -ENOSPC;
1880 goto err;
1881 }
324429d7
HS
1882 }
1883
5110e655 1884 chcr_init_hctx_per_wr(req_ctx);
2f47d580 1885 error = chcr_hash_dma_map(&u_ctx->lldi.pdev->dev, req);
fef4912b
HJ
1886 if (error) {
1887 error = -ENOMEM;
1888 goto err;
1889 }
324429d7 1890
324429d7 1891 get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
5110e655
HJ
1892 params.kctx_len = roundup(params.alg_prm.result_size, 16);
1893 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1894 params.kctx_len *= 2;
1895 params.opad_needed = 1;
1896 } else {
1897 params.opad_needed = 0;
1898 }
1899 params.sg_len = chcr_hash_ent_in_wr(req->src, !!req_ctx->reqlen,
1900 HASH_SPACE_LEFT(params.kctx_len), 0);
1901 if (params.sg_len < req->nbytes) {
1902 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1903 params.kctx_len /= 2;
1904 params.opad_needed = 0;
1905 }
1906 params.last = 0;
1907 params.more = 1;
1908 params.scmd1 = 0;
1909 params.sg_len = rounddown(params.sg_len, bs);
1910 params.hash_size = params.alg_prm.result_size;
1911 } else {
1912 params.sg_len = req->nbytes;
1913 params.hash_size = crypto_ahash_digestsize(rtfm);
1914 params.last = 1;
1915 params.more = 0;
1916 params.scmd1 = req->nbytes + req_ctx->data_len;
1917
1918 }
1919 params.bfr_len = 0;
1920 req_ctx->hctx_wr.result = 1;
1921 req_ctx->hctx_wr.srcsg = req->src;
324429d7
HS
1922 req_ctx->data_len += params.bfr_len + params.sg_len;
1923
44fce12a
HJ
1924 if (req->nbytes == 0) {
1925 create_last_hash_block(req_ctx->reqbfr, bs, 0);
324429d7
HS
1926 params.more = 1;
1927 params.bfr_len = bs;
1928 }
1929
358961d1 1930 skb = create_hash_wr(req, &params);
2f47d580
HJ
1931 if (IS_ERR(skb)) {
1932 error = PTR_ERR(skb);
1933 goto unmap;
1934 }
5110e655 1935 req_ctx->hctx_wr.processed += params.sg_len;
324429d7 1936 skb->dev = u_ctx->lldi.ports[0];
2f47d580 1937 set_wr_txq(skb, CPL_PRIORITY_DATA, h_ctx(rtfm)->tx_qidx);
324429d7 1938 chcr_send_wr(skb);
6faa0f57 1939 return isfull ? -EBUSY : -EINPROGRESS;
2f47d580
HJ
1940unmap:
1941 chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req);
fef4912b
HJ
1942err:
1943 chcr_dec_wrcount(dev);
2f47d580 1944 return error;
324429d7
HS
1945}
1946
6f76672b
HJ
1947static int chcr_ahash_continue(struct ahash_request *req)
1948{
1949 struct chcr_ahash_req_ctx *reqctx = ahash_request_ctx(req);
1950 struct chcr_hctx_per_wr *hctx_wr = &reqctx->hctx_wr;
1951 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(req);
1952 struct uld_ctx *u_ctx = NULL;
1953 struct sk_buff *skb;
1954 struct hash_wr_param params;
1955 u8 bs;
1956 int error;
1957
1958 bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
1959 u_ctx = ULD_CTX(h_ctx(rtfm));
6f76672b
HJ
1960 get_alg_config(&params.alg_prm, crypto_ahash_digestsize(rtfm));
1961 params.kctx_len = roundup(params.alg_prm.result_size, 16);
1962 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1963 params.kctx_len *= 2;
1964 params.opad_needed = 1;
1965 } else {
1966 params.opad_needed = 0;
1967 }
1968 params.sg_len = chcr_hash_ent_in_wr(hctx_wr->srcsg, 0,
1969 HASH_SPACE_LEFT(params.kctx_len),
1970 hctx_wr->src_ofst);
1971 if ((params.sg_len + hctx_wr->processed) > req->nbytes)
1972 params.sg_len = req->nbytes - hctx_wr->processed;
1973 if (!hctx_wr->result ||
1974 ((params.sg_len + hctx_wr->processed) < req->nbytes)) {
1975 if (is_hmac(crypto_ahash_tfm(rtfm))) {
1976 params.kctx_len /= 2;
1977 params.opad_needed = 0;
1978 }
1979 params.last = 0;
1980 params.more = 1;
1981 params.sg_len = rounddown(params.sg_len, bs);
1982 params.hash_size = params.alg_prm.result_size;
1983 params.scmd1 = 0;
1984 } else {
1985 params.last = 1;
1986 params.more = 0;
1987 params.hash_size = crypto_ahash_digestsize(rtfm);
1988 params.scmd1 = reqctx->data_len + params.sg_len;
1989 }
1990 params.bfr_len = 0;
1991 reqctx->data_len += params.sg_len;
1992 skb = create_hash_wr(req, &params);
1993 if (IS_ERR(skb)) {
1994 error = PTR_ERR(skb);
1995 goto err;
1996 }
1997 hctx_wr->processed += params.sg_len;
1998 skb->dev = u_ctx->lldi.ports[0];
1999 set_wr_txq(skb, CPL_PRIORITY_DATA, h_ctx(rtfm)->tx_qidx);
2000 chcr_send_wr(skb);
2001 return 0;
2002err:
2003 return error;
2004}
2005
2006static inline void chcr_handle_ahash_resp(struct ahash_request *req,
2007 unsigned char *input,
2008 int err)
2009{
2010 struct chcr_ahash_req_ctx *reqctx = ahash_request_ctx(req);
2011 struct chcr_hctx_per_wr *hctx_wr = &reqctx->hctx_wr;
2012 int digestsize, updated_digestsize;
2013 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
2014 struct uld_ctx *u_ctx = ULD_CTX(h_ctx(tfm));
fef4912b 2015 struct chcr_dev *dev = h_ctx(tfm)->dev;
6f76672b
HJ
2016
2017 if (input == NULL)
2018 goto out;
2019 digestsize = crypto_ahash_digestsize(crypto_ahash_reqtfm(req));
2020 updated_digestsize = digestsize;
2021 if (digestsize == SHA224_DIGEST_SIZE)
2022 updated_digestsize = SHA256_DIGEST_SIZE;
2023 else if (digestsize == SHA384_DIGEST_SIZE)
2024 updated_digestsize = SHA512_DIGEST_SIZE;
2025
2026 if (hctx_wr->dma_addr) {
2027 dma_unmap_single(&u_ctx->lldi.pdev->dev, hctx_wr->dma_addr,
2028 hctx_wr->dma_len, DMA_TO_DEVICE);
2029 hctx_wr->dma_addr = 0;
2030 }
2031 if (hctx_wr->isfinal || ((hctx_wr->processed + reqctx->reqlen) ==
2032 req->nbytes)) {
2033 if (hctx_wr->result == 1) {
2034 hctx_wr->result = 0;
2035 memcpy(req->result, input + sizeof(struct cpl_fw6_pld),
2036 digestsize);
2037 } else {
2038 memcpy(reqctx->partial_hash,
2039 input + sizeof(struct cpl_fw6_pld),
2040 updated_digestsize);
2041
2042 }
2043 goto unmap;
2044 }
2045 memcpy(reqctx->partial_hash, input + sizeof(struct cpl_fw6_pld),
2046 updated_digestsize);
2047
2048 err = chcr_ahash_continue(req);
2049 if (err)
2050 goto unmap;
2051 return;
2052unmap:
2053 if (hctx_wr->is_sg_map)
2054 chcr_hash_dma_unmap(&u_ctx->lldi.pdev->dev, req);
2055
2056
2057out:
fef4912b 2058 chcr_dec_wrcount(dev);
6f76672b
HJ
2059 req->base.complete(&req->base, err);
2060}
2061
2062/*
2063 * chcr_handle_resp - Unmap the DMA buffers associated with the request
2064 * @req: crypto request
2065 */
2066int chcr_handle_resp(struct crypto_async_request *req, unsigned char *input,
2067 int err)
2068{
2069 struct crypto_tfm *tfm = req->tfm;
2070 struct chcr_context *ctx = crypto_tfm_ctx(tfm);
2071 struct adapter *adap = padap(ctx->dev);
2072
2073 switch (tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
2074 case CRYPTO_ALG_TYPE_AEAD:
2075 chcr_handle_aead_resp(aead_request_cast(req), input, err);
2076 break;
2077
2078 case CRYPTO_ALG_TYPE_ABLKCIPHER:
2079 err = chcr_handle_cipher_resp(ablkcipher_request_cast(req),
2080 input, err);
2081 break;
2082
2083 case CRYPTO_ALG_TYPE_AHASH:
2084 chcr_handle_ahash_resp(ahash_request_cast(req), input, err);
2085 }
2086 atomic_inc(&adap->chcr_stats.complete);
2087 return err;
2088}
324429d7
HS
2089static int chcr_ahash_export(struct ahash_request *areq, void *out)
2090{
2091 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2092 struct chcr_ahash_req_ctx *state = out;
2093
44fce12a 2094 state->reqlen = req_ctx->reqlen;
324429d7 2095 state->data_len = req_ctx->data_len;
44fce12a 2096 memcpy(state->bfr1, req_ctx->reqbfr, req_ctx->reqlen);
324429d7
HS
2097 memcpy(state->partial_hash, req_ctx->partial_hash,
2098 CHCR_HASH_MAX_DIGEST_SIZE);
5110e655 2099 chcr_init_hctx_per_wr(state);
fc6176a2 2100 return 0;
324429d7
HS
2101}
2102
2103static int chcr_ahash_import(struct ahash_request *areq, const void *in)
2104{
2105 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2106 struct chcr_ahash_req_ctx *state = (struct chcr_ahash_req_ctx *)in;
2107
44fce12a 2108 req_ctx->reqlen = state->reqlen;
324429d7 2109 req_ctx->data_len = state->data_len;
44fce12a
HJ
2110 req_ctx->reqbfr = req_ctx->bfr1;
2111 req_ctx->skbfr = req_ctx->bfr2;
2112 memcpy(req_ctx->bfr1, state->bfr1, CHCR_HASH_MAX_BLOCK_SIZE_128);
324429d7
HS
2113 memcpy(req_ctx->partial_hash, state->partial_hash,
2114 CHCR_HASH_MAX_DIGEST_SIZE);
5110e655 2115 chcr_init_hctx_per_wr(req_ctx);
324429d7
HS
2116 return 0;
2117}
2118
2119static int chcr_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
2120 unsigned int keylen)
2121{
2f47d580 2122 struct hmac_ctx *hmacctx = HMAC_CTX(h_ctx(tfm));
324429d7
HS
2123 unsigned int digestsize = crypto_ahash_digestsize(tfm);
2124 unsigned int bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
2125 unsigned int i, err = 0, updated_digestsize;
2126
e7922729
HJ
2127 SHASH_DESC_ON_STACK(shash, hmacctx->base_hash);
2128
2129 /* use the key to calculate the ipad and opad. ipad will sent with the
324429d7
HS
2130 * first request's data. opad will be sent with the final hash result
2131 * ipad in hmacctx->ipad and opad in hmacctx->opad location
2132 */
e7922729
HJ
2133 shash->tfm = hmacctx->base_hash;
2134 shash->flags = crypto_shash_get_flags(hmacctx->base_hash);
324429d7 2135 if (keylen > bs) {
e7922729 2136 err = crypto_shash_digest(shash, key, keylen,
324429d7
HS
2137 hmacctx->ipad);
2138 if (err)
2139 goto out;
2140 keylen = digestsize;
2141 } else {
2142 memcpy(hmacctx->ipad, key, keylen);
2143 }
2144 memset(hmacctx->ipad + keylen, 0, bs - keylen);
2145 memcpy(hmacctx->opad, hmacctx->ipad, bs);
2146
2147 for (i = 0; i < bs / sizeof(int); i++) {
2148 *((unsigned int *)(&hmacctx->ipad) + i) ^= IPAD_DATA;
2149 *((unsigned int *)(&hmacctx->opad) + i) ^= OPAD_DATA;
2150 }
2151
2152 updated_digestsize = digestsize;
2153 if (digestsize == SHA224_DIGEST_SIZE)
2154 updated_digestsize = SHA256_DIGEST_SIZE;
2155 else if (digestsize == SHA384_DIGEST_SIZE)
2156 updated_digestsize = SHA512_DIGEST_SIZE;
e7922729 2157 err = chcr_compute_partial_hash(shash, hmacctx->ipad,
324429d7
HS
2158 hmacctx->ipad, digestsize);
2159 if (err)
2160 goto out;
2161 chcr_change_order(hmacctx->ipad, updated_digestsize);
2162
e7922729 2163 err = chcr_compute_partial_hash(shash, hmacctx->opad,
324429d7
HS
2164 hmacctx->opad, digestsize);
2165 if (err)
2166 goto out;
2167 chcr_change_order(hmacctx->opad, updated_digestsize);
2168out:
2169 return err;
2170}
2171
b8fd1f41 2172static int chcr_aes_xts_setkey(struct crypto_ablkcipher *cipher, const u8 *key,
324429d7
HS
2173 unsigned int key_len)
2174{
2f47d580 2175 struct ablk_ctx *ablkctx = ABLK_CTX(c_ctx(cipher));
324429d7 2176 unsigned short context_size = 0;
b8fd1f41 2177 int err;
324429d7 2178
b8fd1f41
HJ
2179 err = chcr_cipher_fallback_setkey(cipher, key, key_len);
2180 if (err)
2181 goto badkey_err;
cc1b156d
HJ
2182
2183 memcpy(ablkctx->key, key, key_len);
2184 ablkctx->enckey_len = key_len;
2185 get_aes_decrypt_key(ablkctx->rrkey, ablkctx->key, key_len << 2);
2186 context_size = (KEY_CONTEXT_HDR_SALT_AND_PAD + key_len) >> 4;
2187 ablkctx->key_ctx_hdr =
2188 FILL_KEY_CTX_HDR((key_len == AES_KEYSIZE_256) ?
2189 CHCR_KEYCTX_CIPHER_KEY_SIZE_128 :
2190 CHCR_KEYCTX_CIPHER_KEY_SIZE_256,
2191 CHCR_KEYCTX_NO_KEY, 1,
2192 0, context_size);
2193 ablkctx->ciph_mode = CHCR_SCMD_CIPHER_MODE_AES_XTS;
2194 return 0;
b8fd1f41
HJ
2195badkey_err:
2196 crypto_ablkcipher_set_flags(cipher, CRYPTO_TFM_RES_BAD_KEY_LEN);
2197 ablkctx->enckey_len = 0;
2198
2199 return err;
324429d7
HS
2200}
2201
2202static int chcr_sha_init(struct ahash_request *areq)
2203{
2204 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2205 struct crypto_ahash *tfm = crypto_ahash_reqtfm(areq);
2206 int digestsize = crypto_ahash_digestsize(tfm);
2207
2208 req_ctx->data_len = 0;
44fce12a
HJ
2209 req_ctx->reqlen = 0;
2210 req_ctx->reqbfr = req_ctx->bfr1;
2211 req_ctx->skbfr = req_ctx->bfr2;
324429d7 2212 copy_hash_init_values(req_ctx->partial_hash, digestsize);
5110e655 2213
324429d7
HS
2214 return 0;
2215}
2216
2217static int chcr_sha_cra_init(struct crypto_tfm *tfm)
2218{
2219 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2220 sizeof(struct chcr_ahash_req_ctx));
2221 return chcr_device_init(crypto_tfm_ctx(tfm));
2222}
2223
2224static int chcr_hmac_init(struct ahash_request *areq)
2225{
2226 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(areq);
2227 struct crypto_ahash *rtfm = crypto_ahash_reqtfm(areq);
2f47d580 2228 struct hmac_ctx *hmacctx = HMAC_CTX(h_ctx(rtfm));
324429d7
HS
2229 unsigned int digestsize = crypto_ahash_digestsize(rtfm);
2230 unsigned int bs = crypto_tfm_alg_blocksize(crypto_ahash_tfm(rtfm));
2231
2232 chcr_sha_init(areq);
2233 req_ctx->data_len = bs;
2234 if (is_hmac(crypto_ahash_tfm(rtfm))) {
2235 if (digestsize == SHA224_DIGEST_SIZE)
2236 memcpy(req_ctx->partial_hash, hmacctx->ipad,
2237 SHA256_DIGEST_SIZE);
2238 else if (digestsize == SHA384_DIGEST_SIZE)
2239 memcpy(req_ctx->partial_hash, hmacctx->ipad,
2240 SHA512_DIGEST_SIZE);
2241 else
2242 memcpy(req_ctx->partial_hash, hmacctx->ipad,
2243 digestsize);
2244 }
2245 return 0;
2246}
2247
2248static int chcr_hmac_cra_init(struct crypto_tfm *tfm)
2249{
2250 struct chcr_context *ctx = crypto_tfm_ctx(tfm);
2251 struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
2252 unsigned int digestsize =
2253 crypto_ahash_digestsize(__crypto_ahash_cast(tfm));
2254
2255 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2256 sizeof(struct chcr_ahash_req_ctx));
e7922729
HJ
2257 hmacctx->base_hash = chcr_alloc_shash(digestsize);
2258 if (IS_ERR(hmacctx->base_hash))
2259 return PTR_ERR(hmacctx->base_hash);
324429d7
HS
2260 return chcr_device_init(crypto_tfm_ctx(tfm));
2261}
2262
324429d7
HS
2263static void chcr_hmac_cra_exit(struct crypto_tfm *tfm)
2264{
2265 struct chcr_context *ctx = crypto_tfm_ctx(tfm);
2266 struct hmac_ctx *hmacctx = HMAC_CTX(ctx);
2267
e7922729
HJ
2268 if (hmacctx->base_hash) {
2269 chcr_free_shash(hmacctx->base_hash);
2270 hmacctx->base_hash = NULL;
324429d7
HS
2271 }
2272}
2273
4262c98a
HJ
2274inline void chcr_aead_common_exit(struct aead_request *req)
2275{
2276 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2277 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2278 struct uld_ctx *u_ctx = ULD_CTX(a_ctx(tfm));
2279
2280 chcr_aead_dma_unmap(&u_ctx->lldi.pdev->dev, req, reqctx->op);
2281}
2282
2283static int chcr_aead_common_init(struct aead_request *req)
2debd332 2284{
2f47d580
HJ
2285 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2286 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2287 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2f47d580 2288 unsigned int authsize = crypto_aead_authsize(tfm);
4262c98a 2289 int error = -EINVAL;
2debd332 2290
2f47d580
HJ
2291 /* validate key size */
2292 if (aeadctx->enckey_len == 0)
2293 goto err;
4262c98a 2294 if (reqctx->op && req->cryptlen < authsize)
2f47d580 2295 goto err;
4262c98a
HJ
2296 if (reqctx->b0_len)
2297 reqctx->scratch_pad = reqctx->iv + IV;
2298 else
2299 reqctx->scratch_pad = NULL;
2300
2f47d580 2301 error = chcr_aead_dma_map(&ULD_CTX(a_ctx(tfm))->lldi.pdev->dev, req,
4262c98a 2302 reqctx->op);
2f47d580
HJ
2303 if (error) {
2304 error = -ENOMEM;
2305 goto err;
2306 }
1f479e4c 2307
2f47d580
HJ
2308 return 0;
2309err:
2310 return error;
2debd332 2311}
2f47d580
HJ
2312
2313static int chcr_aead_need_fallback(struct aead_request *req, int dst_nents,
0e93708d
HJ
2314 int aadmax, int wrlen,
2315 unsigned short op_type)
2316{
2317 unsigned int authsize = crypto_aead_authsize(crypto_aead_reqtfm(req));
2318
2319 if (((req->cryptlen - (op_type ? authsize : 0)) == 0) ||
2f47d580 2320 dst_nents > MAX_DSGL_ENT ||
0e93708d 2321 (req->assoclen > aadmax) ||
2f47d580 2322 (wrlen > SGE_MAX_WR_LEN))
0e93708d
HJ
2323 return 1;
2324 return 0;
2325}
2debd332 2326
0e93708d
HJ
2327static int chcr_aead_fallback(struct aead_request *req, unsigned short op_type)
2328{
2329 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2f47d580 2330 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
0e93708d
HJ
2331 struct aead_request *subreq = aead_request_ctx(req);
2332
2333 aead_request_set_tfm(subreq, aeadctx->sw_cipher);
2334 aead_request_set_callback(subreq, req->base.flags,
2335 req->base.complete, req->base.data);
4262c98a 2336 aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
0e93708d 2337 req->iv);
fc6176a2 2338 aead_request_set_ad(subreq, req->assoclen);
0e93708d
HJ
2339 return op_type ? crypto_aead_decrypt(subreq) :
2340 crypto_aead_encrypt(subreq);
2341}
2debd332
HJ
2342
2343static struct sk_buff *create_authenc_wr(struct aead_request *req,
2344 unsigned short qid,
4262c98a 2345 int size)
2debd332
HJ
2346{
2347 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2f47d580 2348 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2debd332
HJ
2349 struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx);
2350 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2351 struct sk_buff *skb = NULL;
2352 struct chcr_wr *chcr_req;
2353 struct cpl_rx_phys_dsgl *phys_cpl;
2f47d580
HJ
2354 struct ulptx_sgl *ulptx;
2355 unsigned int transhdr_len;
3d64bd67 2356 unsigned int dst_size = 0, temp, subtype = get_aead_subtype(tfm);
1f479e4c 2357 unsigned int kctx_len = 0, dnents, snents;
2debd332 2358 unsigned int authsize = crypto_aead_authsize(tfm);
2f47d580 2359 int error = -EINVAL;
1f479e4c 2360 u8 *ivptr;
2debd332
HJ
2361 int null = 0;
2362 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
2363 GFP_ATOMIC;
2f47d580 2364 struct adapter *adap = padap(a_ctx(tfm)->dev);
2debd332 2365
2f47d580
HJ
2366 if (req->cryptlen == 0)
2367 return NULL;
2debd332 2368
4262c98a
HJ
2369 reqctx->b0_len = 0;
2370 error = chcr_aead_common_init(req);
2371 if (error)
2372 return ERR_PTR(error);
2373
3d64bd67 2374 if (subtype == CRYPTO_ALG_SUB_TYPE_CBC_NULL ||
4262c98a 2375 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) {
2debd332 2376 null = 1;
2debd332 2377 }
1f479e4c
HJ
2378 dnents = sg_nents_xlen(req->dst, req->assoclen + req->cryptlen +
2379 (reqctx->op ? -authsize : authsize), CHCR_DST_SG_SIZE, 0);
5abc8db0 2380 dnents += MIN_AUTH_SG; // For IV
1f479e4c
HJ
2381 snents = sg_nents_xlen(req->src, req->assoclen + req->cryptlen,
2382 CHCR_SRC_SG_SIZE, 0);
2f47d580 2383 dst_size = get_space_for_phys_dsgl(dnents);
2debd332
HJ
2384 kctx_len = (ntohl(KEY_CONTEXT_CTX_LEN_V(aeadctx->key_ctx_hdr)) << 4)
2385 - sizeof(chcr_req->key_ctx);
2386 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
1f479e4c 2387 reqctx->imm = (transhdr_len + req->assoclen + req->cryptlen) <
2f47d580 2388 SGE_MAX_WR_LEN;
1f479e4c
HJ
2389 temp = reqctx->imm ? roundup(req->assoclen + req->cryptlen, 16)
2390 : (sgl_len(snents) * 8);
2f47d580 2391 transhdr_len += temp;
125d01ca 2392 transhdr_len = roundup(transhdr_len, 16);
2f47d580
HJ
2393
2394 if (chcr_aead_need_fallback(req, dnents, T6_MAX_AAD_SIZE,
4262c98a 2395 transhdr_len, reqctx->op)) {
ee0863ba 2396 atomic_inc(&adap->chcr_stats.fallback);
4262c98a
HJ
2397 chcr_aead_common_exit(req);
2398 return ERR_PTR(chcr_aead_fallback(req, reqctx->op));
0e93708d 2399 }
1f479e4c 2400 skb = alloc_skb(transhdr_len, flags);
5fe8c711
HJ
2401 if (!skb) {
2402 error = -ENOMEM;
2debd332 2403 goto err;
5fe8c711 2404 }
2debd332 2405
de77b966 2406 chcr_req = __skb_put_zero(skb, transhdr_len);
2debd332 2407
4262c98a 2408 temp = (reqctx->op == CHCR_ENCRYPT_OP) ? 0 : authsize;
2debd332
HJ
2409
2410 /*
2411 * Input order is AAD,IV and Payload. where IV should be included as
2412 * the part of authdata. All other fields should be filled according
2413 * to the hardware spec
2414 */
2415 chcr_req->sec_cpl.op_ivinsrtofst =
d5a4dfbd 2416 FILL_SEC_CPL_OP_IVINSR(a_ctx(tfm)->tx_chan_id, 2, 1);
1f479e4c 2417 chcr_req->sec_cpl.pldlen = htonl(req->assoclen + IV + req->cryptlen);
2debd332 2418 chcr_req->sec_cpl.aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI(
1f479e4c
HJ
2419 null ? 0 : 1 + IV,
2420 null ? 0 : IV + req->assoclen,
2421 req->assoclen + IV + 1,
2f47d580 2422 (temp & 0x1F0) >> 4);
2debd332 2423 chcr_req->sec_cpl.cipherstop_lo_authinsert = FILL_SEC_CPL_AUTHINSERT(
2f47d580 2424 temp & 0xF,
1f479e4c 2425 null ? 0 : req->assoclen + IV + 1,
2f47d580 2426 temp, temp);
3d64bd67
HJ
2427 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL ||
2428 subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA)
2429 temp = CHCR_SCMD_CIPHER_MODE_AES_CTR;
2430 else
2431 temp = CHCR_SCMD_CIPHER_MODE_AES_CBC;
4262c98a
HJ
2432 chcr_req->sec_cpl.seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(reqctx->op,
2433 (reqctx->op == CHCR_ENCRYPT_OP) ? 1 : 0,
3d64bd67 2434 temp,
2debd332 2435 actx->auth_mode, aeadctx->hmac_ctrl,
2f47d580 2436 IV >> 1);
2debd332 2437 chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1,
2f47d580 2438 0, 0, dst_size);
2debd332
HJ
2439
2440 chcr_req->key_ctx.ctx_hdr = aeadctx->key_ctx_hdr;
4262c98a 2441 if (reqctx->op == CHCR_ENCRYPT_OP ||
3d64bd67
HJ
2442 subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA ||
2443 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL)
2debd332
HJ
2444 memcpy(chcr_req->key_ctx.key, aeadctx->key,
2445 aeadctx->enckey_len);
2446 else
2447 memcpy(chcr_req->key_ctx.key, actx->dec_rrkey,
2448 aeadctx->enckey_len);
2449
125d01ca
HJ
2450 memcpy(chcr_req->key_ctx.key + roundup(aeadctx->enckey_len, 16),
2451 actx->h_iopad, kctx_len - roundup(aeadctx->enckey_len, 16));
1f479e4c
HJ
2452 phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
2453 ivptr = (u8 *)(phys_cpl + 1) + dst_size;
2454 ulptx = (struct ulptx_sgl *)(ivptr + IV);
3d64bd67
HJ
2455 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA ||
2456 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) {
1f479e4c
HJ
2457 memcpy(ivptr, aeadctx->nonce, CTR_RFC3686_NONCE_SIZE);
2458 memcpy(ivptr + CTR_RFC3686_NONCE_SIZE, req->iv,
3d64bd67 2459 CTR_RFC3686_IV_SIZE);
1f479e4c 2460 *(__be32 *)(ivptr + CTR_RFC3686_NONCE_SIZE +
3d64bd67
HJ
2461 CTR_RFC3686_IV_SIZE) = cpu_to_be32(1);
2462 } else {
1f479e4c 2463 memcpy(ivptr, req->iv, IV);
3d64bd67 2464 }
1f479e4c
HJ
2465 chcr_add_aead_dst_ent(req, phys_cpl, qid);
2466 chcr_add_aead_src_ent(req, ulptx);
ee0863ba 2467 atomic_inc(&adap->chcr_stats.cipher_rqst);
1f479e4c
HJ
2468 temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + IV +
2469 kctx_len + (reqctx->imm ? (req->assoclen + req->cryptlen) : 0);
2f47d580
HJ
2470 create_wreq(a_ctx(tfm), chcr_req, &req->base, reqctx->imm, size,
2471 transhdr_len, temp, 0);
2debd332 2472 reqctx->skb = skb;
2debd332
HJ
2473
2474 return skb;
2debd332 2475err:
4262c98a 2476 chcr_aead_common_exit(req);
2f47d580 2477
5fe8c711 2478 return ERR_PTR(error);
2debd332
HJ
2479}
2480
6dad4e8a
AG
2481int chcr_aead_dma_map(struct device *dev,
2482 struct aead_request *req,
2483 unsigned short op_type)
2f47d580
HJ
2484{
2485 int error;
2486 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2487 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2488 unsigned int authsize = crypto_aead_authsize(tfm);
2489 int dst_size;
2490
2491 dst_size = req->assoclen + req->cryptlen + (op_type ?
2492 -authsize : authsize);
2493 if (!req->cryptlen || !dst_size)
2494 return 0;
4262c98a 2495 reqctx->iv_dma = dma_map_single(dev, reqctx->iv, (IV + reqctx->b0_len),
2f47d580
HJ
2496 DMA_BIDIRECTIONAL);
2497 if (dma_mapping_error(dev, reqctx->iv_dma))
2498 return -ENOMEM;
4262c98a
HJ
2499 if (reqctx->b0_len)
2500 reqctx->b0_dma = reqctx->iv_dma + IV;
2501 else
2502 reqctx->b0_dma = 0;
2f47d580
HJ
2503 if (req->src == req->dst) {
2504 error = dma_map_sg(dev, req->src, sg_nents(req->src),
2505 DMA_BIDIRECTIONAL);
2506 if (!error)
2507 goto err;
2508 } else {
2509 error = dma_map_sg(dev, req->src, sg_nents(req->src),
2510 DMA_TO_DEVICE);
2511 if (!error)
2512 goto err;
2513 error = dma_map_sg(dev, req->dst, sg_nents(req->dst),
2514 DMA_FROM_DEVICE);
2515 if (!error) {
2516 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2517 DMA_TO_DEVICE);
2518 goto err;
2519 }
2520 }
2521
2522 return 0;
2523err:
2524 dma_unmap_single(dev, reqctx->iv_dma, IV, DMA_BIDIRECTIONAL);
2525 return -ENOMEM;
2526}
2527
6dad4e8a
AG
2528void chcr_aead_dma_unmap(struct device *dev,
2529 struct aead_request *req,
2530 unsigned short op_type)
2f47d580
HJ
2531{
2532 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2533 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2534 unsigned int authsize = crypto_aead_authsize(tfm);
2535 int dst_size;
2536
2537 dst_size = req->assoclen + req->cryptlen + (op_type ?
2538 -authsize : authsize);
2539 if (!req->cryptlen || !dst_size)
2540 return;
2541
4262c98a 2542 dma_unmap_single(dev, reqctx->iv_dma, (IV + reqctx->b0_len),
2f47d580
HJ
2543 DMA_BIDIRECTIONAL);
2544 if (req->src == req->dst) {
2545 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2546 DMA_BIDIRECTIONAL);
2547 } else {
2548 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2549 DMA_TO_DEVICE);
2550 dma_unmap_sg(dev, req->dst, sg_nents(req->dst),
2551 DMA_FROM_DEVICE);
2552 }
2553}
2554
6dad4e8a 2555void chcr_add_aead_src_ent(struct aead_request *req,
1f479e4c 2556 struct ulptx_sgl *ulptx)
2f47d580
HJ
2557{
2558 struct ulptx_walk ulp_walk;
2559 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2560
2561 if (reqctx->imm) {
2562 u8 *buf = (u8 *)ulptx;
2563
4262c98a 2564 if (reqctx->b0_len) {
2f47d580
HJ
2565 memcpy(buf, reqctx->scratch_pad, reqctx->b0_len);
2566 buf += reqctx->b0_len;
2567 }
2568 sg_pcopy_to_buffer(req->src, sg_nents(req->src),
1f479e4c 2569 buf, req->cryptlen + req->assoclen, 0);
2f47d580
HJ
2570 } else {
2571 ulptx_walk_init(&ulp_walk, ulptx);
4262c98a 2572 if (reqctx->b0_len)
2f47d580 2573 ulptx_walk_add_page(&ulp_walk, reqctx->b0_len,
c4f6d44d 2574 reqctx->b0_dma);
1f479e4c
HJ
2575 ulptx_walk_add_sg(&ulp_walk, req->src, req->cryptlen +
2576 req->assoclen, 0);
2f47d580
HJ
2577 ulptx_walk_end(&ulp_walk);
2578 }
2579}
2580
6dad4e8a
AG
2581void chcr_add_aead_dst_ent(struct aead_request *req,
2582 struct cpl_rx_phys_dsgl *phys_cpl,
6dad4e8a 2583 unsigned short qid)
2f47d580
HJ
2584{
2585 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2586 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2587 struct dsgl_walk dsgl_walk;
2588 unsigned int authsize = crypto_aead_authsize(tfm);
add92a81 2589 struct chcr_context *ctx = a_ctx(tfm);
2f47d580
HJ
2590 u32 temp;
2591
2592 dsgl_walk_init(&dsgl_walk, phys_cpl);
c4f6d44d 2593 dsgl_walk_add_page(&dsgl_walk, IV + reqctx->b0_len, reqctx->iv_dma);
1f479e4c
HJ
2594 temp = req->assoclen + req->cryptlen +
2595 (reqctx->op ? -authsize : authsize);
2596 dsgl_walk_add_sg(&dsgl_walk, req->dst, temp, 0);
add92a81 2597 dsgl_walk_end(&dsgl_walk, qid, ctx->pci_chan_id);
2f47d580
HJ
2598}
2599
6dad4e8a 2600void chcr_add_cipher_src_ent(struct ablkcipher_request *req,
335bcc4a 2601 void *ulptx,
6dad4e8a 2602 struct cipher_wr_param *wrparam)
2f47d580
HJ
2603{
2604 struct ulptx_walk ulp_walk;
2605 struct chcr_blkcipher_req_ctx *reqctx = ablkcipher_request_ctx(req);
335bcc4a 2606 u8 *buf = ulptx;
2f47d580 2607
335bcc4a
HJ
2608 memcpy(buf, reqctx->iv, IV);
2609 buf += IV;
2f47d580 2610 if (reqctx->imm) {
2f47d580
HJ
2611 sg_pcopy_to_buffer(req->src, sg_nents(req->src),
2612 buf, wrparam->bytes, reqctx->processed);
2613 } else {
335bcc4a 2614 ulptx_walk_init(&ulp_walk, (struct ulptx_sgl *)buf);
2f47d580
HJ
2615 ulptx_walk_add_sg(&ulp_walk, reqctx->srcsg, wrparam->bytes,
2616 reqctx->src_ofst);
2617 reqctx->srcsg = ulp_walk.last_sg;
2618 reqctx->src_ofst = ulp_walk.last_sg_len;
2619 ulptx_walk_end(&ulp_walk);
2620 }
2621}
2622
6dad4e8a
AG
2623void chcr_add_cipher_dst_ent(struct ablkcipher_request *req,
2624 struct cpl_rx_phys_dsgl *phys_cpl,
2625 struct cipher_wr_param *wrparam,
2626 unsigned short qid)
2f47d580
HJ
2627{
2628 struct chcr_blkcipher_req_ctx *reqctx = ablkcipher_request_ctx(req);
add92a81
HJ
2629 struct crypto_ablkcipher *tfm = crypto_ablkcipher_reqtfm(wrparam->req);
2630 struct chcr_context *ctx = c_ctx(tfm);
2f47d580
HJ
2631 struct dsgl_walk dsgl_walk;
2632
2633 dsgl_walk_init(&dsgl_walk, phys_cpl);
2f47d580
HJ
2634 dsgl_walk_add_sg(&dsgl_walk, reqctx->dstsg, wrparam->bytes,
2635 reqctx->dst_ofst);
2636 reqctx->dstsg = dsgl_walk.last_sg;
2637 reqctx->dst_ofst = dsgl_walk.last_sg_len;
2638
add92a81 2639 dsgl_walk_end(&dsgl_walk, qid, ctx->pci_chan_id);
2f47d580
HJ
2640}
2641
6dad4e8a
AG
2642void chcr_add_hash_src_ent(struct ahash_request *req,
2643 struct ulptx_sgl *ulptx,
2644 struct hash_wr_param *param)
2f47d580
HJ
2645{
2646 struct ulptx_walk ulp_walk;
2647 struct chcr_ahash_req_ctx *reqctx = ahash_request_ctx(req);
2648
5110e655 2649 if (reqctx->hctx_wr.imm) {
2f47d580
HJ
2650 u8 *buf = (u8 *)ulptx;
2651
2652 if (param->bfr_len) {
2653 memcpy(buf, reqctx->reqbfr, param->bfr_len);
2654 buf += param->bfr_len;
2655 }
5110e655
HJ
2656
2657 sg_pcopy_to_buffer(reqctx->hctx_wr.srcsg,
2658 sg_nents(reqctx->hctx_wr.srcsg), buf,
2659 param->sg_len, 0);
2f47d580
HJ
2660 } else {
2661 ulptx_walk_init(&ulp_walk, ulptx);
2662 if (param->bfr_len)
2663 ulptx_walk_add_page(&ulp_walk, param->bfr_len,
c4f6d44d 2664 reqctx->hctx_wr.dma_addr);
5110e655
HJ
2665 ulptx_walk_add_sg(&ulp_walk, reqctx->hctx_wr.srcsg,
2666 param->sg_len, reqctx->hctx_wr.src_ofst);
2667 reqctx->hctx_wr.srcsg = ulp_walk.last_sg;
2668 reqctx->hctx_wr.src_ofst = ulp_walk.last_sg_len;
db6deea4 2669 ulptx_walk_end(&ulp_walk);
2f47d580
HJ
2670 }
2671}
2672
6dad4e8a
AG
2673int chcr_hash_dma_map(struct device *dev,
2674 struct ahash_request *req)
2f47d580
HJ
2675{
2676 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
2677 int error = 0;
2678
2679 if (!req->nbytes)
2680 return 0;
2681 error = dma_map_sg(dev, req->src, sg_nents(req->src),
2682 DMA_TO_DEVICE);
2683 if (!error)
7814f552 2684 return -ENOMEM;
5110e655 2685 req_ctx->hctx_wr.is_sg_map = 1;
2f47d580
HJ
2686 return 0;
2687}
2688
6dad4e8a
AG
2689void chcr_hash_dma_unmap(struct device *dev,
2690 struct ahash_request *req)
2f47d580
HJ
2691{
2692 struct chcr_ahash_req_ctx *req_ctx = ahash_request_ctx(req);
2693
2694 if (!req->nbytes)
2695 return;
2696
2697 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2698 DMA_TO_DEVICE);
5110e655 2699 req_ctx->hctx_wr.is_sg_map = 0;
2f47d580
HJ
2700
2701}
2702
6dad4e8a
AG
2703int chcr_cipher_dma_map(struct device *dev,
2704 struct ablkcipher_request *req)
2f47d580
HJ
2705{
2706 int error;
2f47d580
HJ
2707
2708 if (req->src == req->dst) {
2709 error = dma_map_sg(dev, req->src, sg_nents(req->src),
2710 DMA_BIDIRECTIONAL);
2711 if (!error)
2712 goto err;
2713 } else {
2714 error = dma_map_sg(dev, req->src, sg_nents(req->src),
2715 DMA_TO_DEVICE);
2716 if (!error)
2717 goto err;
2718 error = dma_map_sg(dev, req->dst, sg_nents(req->dst),
2719 DMA_FROM_DEVICE);
2720 if (!error) {
2721 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2722 DMA_TO_DEVICE);
2723 goto err;
2724 }
2725 }
2726
2727 return 0;
2728err:
2f47d580
HJ
2729 return -ENOMEM;
2730}
6dad4e8a
AG
2731
2732void chcr_cipher_dma_unmap(struct device *dev,
2733 struct ablkcipher_request *req)
2f47d580 2734{
2f47d580
HJ
2735 if (req->src == req->dst) {
2736 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2737 DMA_BIDIRECTIONAL);
2738 } else {
2739 dma_unmap_sg(dev, req->src, sg_nents(req->src),
2740 DMA_TO_DEVICE);
2741 dma_unmap_sg(dev, req->dst, sg_nents(req->dst),
2742 DMA_FROM_DEVICE);
2743 }
2744}
2745
2debd332
HJ
2746static int set_msg_len(u8 *block, unsigned int msglen, int csize)
2747{
2748 __be32 data;
2749
2750 memset(block, 0, csize);
2751 block += csize;
2752
2753 if (csize >= 4)
2754 csize = 4;
2755 else if (msglen > (unsigned int)(1 << (8 * csize)))
2756 return -EOVERFLOW;
2757
2758 data = cpu_to_be32(msglen);
2759 memcpy(block - csize, (u8 *)&data + 4 - csize, csize);
2760
2761 return 0;
2762}
2763
1f479e4c 2764static void generate_b0(struct aead_request *req, u8 *ivptr,
2debd332
HJ
2765 unsigned short op_type)
2766{
2767 unsigned int l, lp, m;
2768 int rc;
2769 struct crypto_aead *aead = crypto_aead_reqtfm(req);
2770 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2771 u8 *b0 = reqctx->scratch_pad;
2772
2773 m = crypto_aead_authsize(aead);
2774
1f479e4c 2775 memcpy(b0, ivptr, 16);
2debd332
HJ
2776
2777 lp = b0[0];
2778 l = lp + 1;
2779
2780 /* set m, bits 3-5 */
2781 *b0 |= (8 * ((m - 2) / 2));
2782
2783 /* set adata, bit 6, if associated data is used */
2784 if (req->assoclen)
2785 *b0 |= 64;
2786 rc = set_msg_len(b0 + 16 - l,
2787 (op_type == CHCR_DECRYPT_OP) ?
2788 req->cryptlen - m : req->cryptlen, l);
2789}
2790
2791static inline int crypto_ccm_check_iv(const u8 *iv)
2792{
2793 /* 2 <= L <= 8, so 1 <= L' <= 7. */
2794 if (iv[0] < 1 || iv[0] > 7)
2795 return -EINVAL;
2796
2797 return 0;
2798}
2799
2800static int ccm_format_packet(struct aead_request *req,
1f479e4c 2801 u8 *ivptr,
2debd332 2802 unsigned int sub_type,
4262c98a
HJ
2803 unsigned short op_type,
2804 unsigned int assoclen)
2debd332
HJ
2805{
2806 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
1f479e4c
HJ
2807 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2808 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2debd332
HJ
2809 int rc = 0;
2810
2debd332 2811 if (sub_type == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309) {
1f479e4c
HJ
2812 ivptr[0] = 3;
2813 memcpy(ivptr + 1, &aeadctx->salt[0], 3);
2814 memcpy(ivptr + 4, req->iv, 8);
2815 memset(ivptr + 12, 0, 4);
2debd332 2816 } else {
1f479e4c 2817 memcpy(ivptr, req->iv, 16);
2debd332 2818 }
4262c98a
HJ
2819 if (assoclen)
2820 *((unsigned short *)(reqctx->scratch_pad + 16)) =
2821 htons(assoclen);
2822
1f479e4c 2823 generate_b0(req, ivptr, op_type);
2debd332 2824 /* zero the ctr value */
1f479e4c 2825 memset(ivptr + 15 - ivptr[0], 0, ivptr[0] + 1);
2debd332
HJ
2826 return rc;
2827}
2828
2829static void fill_sec_cpl_for_aead(struct cpl_tx_sec_pdu *sec_cpl,
2830 unsigned int dst_size,
2831 struct aead_request *req,
2f47d580 2832 unsigned short op_type)
2debd332
HJ
2833{
2834 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2f47d580 2835 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2debd332
HJ
2836 unsigned int cipher_mode = CHCR_SCMD_CIPHER_MODE_AES_CCM;
2837 unsigned int mac_mode = CHCR_SCMD_AUTH_MODE_CBCMAC;
d5a4dfbd 2838 unsigned int c_id = a_ctx(tfm)->tx_chan_id;
2debd332
HJ
2839 unsigned int ccm_xtra;
2840 unsigned char tag_offset = 0, auth_offset = 0;
2debd332
HJ
2841 unsigned int assoclen;
2842
2843 if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309)
2844 assoclen = req->assoclen - 8;
2845 else
2846 assoclen = req->assoclen;
2847 ccm_xtra = CCM_B0_SIZE +
2848 ((assoclen) ? CCM_AAD_FIELD_SIZE : 0);
2849
2850 auth_offset = req->cryptlen ?
1f479e4c 2851 (req->assoclen + IV + 1 + ccm_xtra) : 0;
2debd332
HJ
2852 if (op_type == CHCR_DECRYPT_OP) {
2853 if (crypto_aead_authsize(tfm) != req->cryptlen)
2854 tag_offset = crypto_aead_authsize(tfm);
2855 else
2856 auth_offset = 0;
2857 }
2858
2859
2860 sec_cpl->op_ivinsrtofst = FILL_SEC_CPL_OP_IVINSR(c_id,
1f479e4c 2861 2, 1);
2debd332 2862 sec_cpl->pldlen =
1f479e4c 2863 htonl(req->assoclen + IV + req->cryptlen + ccm_xtra);
2debd332
HJ
2864 /* For CCM there wil be b0 always. So AAD start will be 1 always */
2865 sec_cpl->aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI(
1f479e4c
HJ
2866 1 + IV, IV + assoclen + ccm_xtra,
2867 req->assoclen + IV + 1 + ccm_xtra, 0);
2debd332
HJ
2868
2869 sec_cpl->cipherstop_lo_authinsert = FILL_SEC_CPL_AUTHINSERT(0,
2870 auth_offset, tag_offset,
2871 (op_type == CHCR_ENCRYPT_OP) ? 0 :
2872 crypto_aead_authsize(tfm));
2873 sec_cpl->seqno_numivs = FILL_SEC_CPL_SCMD0_SEQNO(op_type,
2874 (op_type == CHCR_ENCRYPT_OP) ? 0 : 1,
0a7bd30c 2875 cipher_mode, mac_mode,
2f47d580 2876 aeadctx->hmac_ctrl, IV >> 1);
2debd332
HJ
2877
2878 sec_cpl->ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1, 0,
2f47d580 2879 0, dst_size);
2debd332
HJ
2880}
2881
1efb892b
CIK
2882static int aead_ccm_validate_input(unsigned short op_type,
2883 struct aead_request *req,
2884 struct chcr_aead_ctx *aeadctx,
2885 unsigned int sub_type)
2debd332
HJ
2886{
2887 if (sub_type != CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309) {
2888 if (crypto_ccm_check_iv(req->iv)) {
2889 pr_err("CCM: IV check fails\n");
2890 return -EINVAL;
2891 }
2892 } else {
2893 if (req->assoclen != 16 && req->assoclen != 20) {
2894 pr_err("RFC4309: Invalid AAD length %d\n",
2895 req->assoclen);
2896 return -EINVAL;
2897 }
2898 }
2debd332
HJ
2899 return 0;
2900}
2901
2debd332
HJ
2902static struct sk_buff *create_aead_ccm_wr(struct aead_request *req,
2903 unsigned short qid,
4262c98a 2904 int size)
2debd332
HJ
2905{
2906 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2f47d580 2907 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2debd332
HJ
2908 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
2909 struct sk_buff *skb = NULL;
2910 struct chcr_wr *chcr_req;
2911 struct cpl_rx_phys_dsgl *phys_cpl;
2f47d580
HJ
2912 struct ulptx_sgl *ulptx;
2913 unsigned int transhdr_len;
1f479e4c 2914 unsigned int dst_size = 0, kctx_len, dnents, temp, snents;
2f47d580 2915 unsigned int sub_type, assoclen = req->assoclen;
2debd332 2916 unsigned int authsize = crypto_aead_authsize(tfm);
2f47d580 2917 int error = -EINVAL;
1f479e4c 2918 u8 *ivptr;
2debd332
HJ
2919 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
2920 GFP_ATOMIC;
2f47d580 2921 struct adapter *adap = padap(a_ctx(tfm)->dev);
2debd332 2922
2f47d580
HJ
2923 sub_type = get_aead_subtype(tfm);
2924 if (sub_type == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309)
2925 assoclen -= 8;
4262c98a
HJ
2926 reqctx->b0_len = CCM_B0_SIZE + (assoclen ? CCM_AAD_FIELD_SIZE : 0);
2927 error = chcr_aead_common_init(req);
2f47d580
HJ
2928 if (error)
2929 return ERR_PTR(error);
0e93708d 2930
4262c98a 2931 error = aead_ccm_validate_input(reqctx->op, req, aeadctx, sub_type);
5fe8c711 2932 if (error)
2debd332 2933 goto err;
1f479e4c 2934 dnents = sg_nents_xlen(req->dst, req->assoclen + req->cryptlen
4262c98a 2935 + (reqctx->op ? -authsize : authsize),
1f479e4c 2936 CHCR_DST_SG_SIZE, 0);
e1a018e6 2937 dnents += MIN_CCM_SG; // For IV and B0
2f47d580 2938 dst_size = get_space_for_phys_dsgl(dnents);
1f479e4c
HJ
2939 snents = sg_nents_xlen(req->src, req->assoclen + req->cryptlen,
2940 CHCR_SRC_SG_SIZE, 0);
2941 snents += MIN_CCM_SG; //For B0
125d01ca 2942 kctx_len = roundup(aeadctx->enckey_len, 16) * 2;
2debd332 2943 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
1f479e4c 2944 reqctx->imm = (transhdr_len + req->assoclen + req->cryptlen +
2f47d580 2945 reqctx->b0_len) <= SGE_MAX_WR_LEN;
1f479e4c 2946 temp = reqctx->imm ? roundup(req->assoclen + req->cryptlen +
125d01ca 2947 reqctx->b0_len, 16) :
1f479e4c 2948 (sgl_len(snents) * 8);
2f47d580 2949 transhdr_len += temp;
125d01ca 2950 transhdr_len = roundup(transhdr_len, 16);
2f47d580
HJ
2951
2952 if (chcr_aead_need_fallback(req, dnents, T6_MAX_AAD_SIZE -
1f479e4c 2953 reqctx->b0_len, transhdr_len, reqctx->op)) {
ee0863ba 2954 atomic_inc(&adap->chcr_stats.fallback);
4262c98a
HJ
2955 chcr_aead_common_exit(req);
2956 return ERR_PTR(chcr_aead_fallback(req, reqctx->op));
0e93708d 2957 }
1f479e4c 2958 skb = alloc_skb(transhdr_len, flags);
2debd332 2959
5fe8c711
HJ
2960 if (!skb) {
2961 error = -ENOMEM;
2debd332 2962 goto err;
5fe8c711 2963 }
2debd332 2964
1f479e4c 2965 chcr_req = __skb_put_zero(skb, transhdr_len);
2debd332 2966
4262c98a 2967 fill_sec_cpl_for_aead(&chcr_req->sec_cpl, dst_size, req, reqctx->op);
2debd332
HJ
2968
2969 chcr_req->key_ctx.ctx_hdr = aeadctx->key_ctx_hdr;
2970 memcpy(chcr_req->key_ctx.key, aeadctx->key, aeadctx->enckey_len);
125d01ca
HJ
2971 memcpy(chcr_req->key_ctx.key + roundup(aeadctx->enckey_len, 16),
2972 aeadctx->key, aeadctx->enckey_len);
2debd332
HJ
2973
2974 phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
1f479e4c
HJ
2975 ivptr = (u8 *)(phys_cpl + 1) + dst_size;
2976 ulptx = (struct ulptx_sgl *)(ivptr + IV);
2977 error = ccm_format_packet(req, ivptr, sub_type, reqctx->op, assoclen);
5fe8c711 2978 if (error)
2debd332 2979 goto dstmap_fail;
1f479e4c
HJ
2980 chcr_add_aead_dst_ent(req, phys_cpl, qid);
2981 chcr_add_aead_src_ent(req, ulptx);
2debd332 2982
ee0863ba 2983 atomic_inc(&adap->chcr_stats.aead_rqst);
1f479e4c
HJ
2984 temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + IV +
2985 kctx_len + (reqctx->imm ? (req->assoclen + req->cryptlen +
2f47d580
HJ
2986 reqctx->b0_len) : 0);
2987 create_wreq(a_ctx(tfm), chcr_req, &req->base, reqctx->imm, 0,
2988 transhdr_len, temp, 0);
2debd332 2989 reqctx->skb = skb;
2f47d580 2990
2debd332
HJ
2991 return skb;
2992dstmap_fail:
2993 kfree_skb(skb);
2debd332 2994err:
4262c98a 2995 chcr_aead_common_exit(req);
5fe8c711 2996 return ERR_PTR(error);
2debd332
HJ
2997}
2998
2999static struct sk_buff *create_gcm_wr(struct aead_request *req,
3000 unsigned short qid,
4262c98a 3001 int size)
2debd332
HJ
3002{
3003 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2f47d580 3004 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2debd332
HJ
3005 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
3006 struct sk_buff *skb = NULL;
3007 struct chcr_wr *chcr_req;
3008 struct cpl_rx_phys_dsgl *phys_cpl;
2f47d580 3009 struct ulptx_sgl *ulptx;
1f479e4c 3010 unsigned int transhdr_len, dnents = 0, snents;
2f47d580 3011 unsigned int dst_size = 0, temp = 0, kctx_len, assoclen = req->assoclen;
2debd332 3012 unsigned int authsize = crypto_aead_authsize(tfm);
2f47d580 3013 int error = -EINVAL;
1f479e4c 3014 u8 *ivptr;
2debd332
HJ
3015 gfp_t flags = req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP ? GFP_KERNEL :
3016 GFP_ATOMIC;
2f47d580 3017 struct adapter *adap = padap(a_ctx(tfm)->dev);
2debd332 3018
2f47d580
HJ
3019 if (get_aead_subtype(tfm) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106)
3020 assoclen = req->assoclen - 8;
2debd332 3021
4262c98a
HJ
3022 reqctx->b0_len = 0;
3023 error = chcr_aead_common_init(req);
e1a018e6
HJ
3024 if (error)
3025 return ERR_PTR(error);
1f479e4c 3026 dnents = sg_nents_xlen(req->dst, req->assoclen + req->cryptlen +
4262c98a 3027 (reqctx->op ? -authsize : authsize),
1f479e4c
HJ
3028 CHCR_DST_SG_SIZE, 0);
3029 snents = sg_nents_xlen(req->src, req->assoclen + req->cryptlen,
3030 CHCR_SRC_SG_SIZE, 0);
e1a018e6 3031 dnents += MIN_GCM_SG; // For IV
2f47d580 3032 dst_size = get_space_for_phys_dsgl(dnents);
125d01ca 3033 kctx_len = roundup(aeadctx->enckey_len, 16) + AEAD_H_SIZE;
2debd332 3034 transhdr_len = CIPHER_TRANSHDR_SIZE(kctx_len, dst_size);
1f479e4c 3035 reqctx->imm = (transhdr_len + req->assoclen + req->cryptlen) <=
2f47d580 3036 SGE_MAX_WR_LEN;
1f479e4c
HJ
3037 temp = reqctx->imm ? roundup(req->assoclen + req->cryptlen, 16) :
3038 (sgl_len(snents) * 8);
2f47d580 3039 transhdr_len += temp;
125d01ca 3040 transhdr_len = roundup(transhdr_len, 16);
2f47d580 3041 if (chcr_aead_need_fallback(req, dnents, T6_MAX_AAD_SIZE,
4262c98a
HJ
3042 transhdr_len, reqctx->op)) {
3043
ee0863ba 3044 atomic_inc(&adap->chcr_stats.fallback);
4262c98a
HJ
3045 chcr_aead_common_exit(req);
3046 return ERR_PTR(chcr_aead_fallback(req, reqctx->op));
0e93708d 3047 }
1f479e4c 3048 skb = alloc_skb(transhdr_len, flags);
5fe8c711
HJ
3049 if (!skb) {
3050 error = -ENOMEM;
2debd332 3051 goto err;
5fe8c711 3052 }
2debd332 3053
de77b966 3054 chcr_req = __skb_put_zero(skb, transhdr_len);
2debd332 3055
2f47d580 3056 //Offset of tag from end
4262c98a 3057 temp = (reqctx->op == CHCR_ENCRYPT_OP) ? 0 : authsize;
2debd332 3058 chcr_req->sec_cpl.op_ivinsrtofst = FILL_SEC_CPL_OP_IVINSR(
d5a4dfbd 3059 a_ctx(tfm)->tx_chan_id, 2, 1);
0e93708d 3060 chcr_req->sec_cpl.pldlen =
1f479e4c 3061 htonl(req->assoclen + IV + req->cryptlen);
2debd332 3062 chcr_req->sec_cpl.aadstart_cipherstop_hi = FILL_SEC_CPL_CIPHERSTOP_HI(
1f479e4c
HJ
3063 assoclen ? 1 + IV : 0,
3064 assoclen ? IV + assoclen : 0,
3065 req->assoclen + IV + 1, 0);
e1a018e6 3066 chcr_req->sec_cpl.cipherstop_lo_authinsert =
1f479e4c 3067 FILL_SEC_CPL_AUTHINSERT(0, req->assoclen + IV + 1,
2f47d580 3068 temp, temp);
e1a018e6 3069 chcr_req->sec_cpl.seqno_numivs =
4262c98a 3070 FILL_SEC_CPL_SCMD0_SEQNO(reqctx->op, (reqctx->op ==
2debd332
HJ
3071 CHCR_ENCRYPT_OP) ? 1 : 0,
3072 CHCR_SCMD_CIPHER_MODE_AES_GCM,
0a7bd30c 3073 CHCR_SCMD_AUTH_MODE_GHASH,
2f47d580 3074 aeadctx->hmac_ctrl, IV >> 1);
2debd332 3075 chcr_req->sec_cpl.ivgen_hdrlen = FILL_SEC_CPL_IVGEN_HDRLEN(0, 0, 1,
2f47d580 3076 0, 0, dst_size);
2debd332
HJ
3077 chcr_req->key_ctx.ctx_hdr = aeadctx->key_ctx_hdr;
3078 memcpy(chcr_req->key_ctx.key, aeadctx->key, aeadctx->enckey_len);
125d01ca
HJ
3079 memcpy(chcr_req->key_ctx.key + roundup(aeadctx->enckey_len, 16),
3080 GCM_CTX(aeadctx)->ghash_h, AEAD_H_SIZE);
2debd332 3081
1f479e4c
HJ
3082 phys_cpl = (struct cpl_rx_phys_dsgl *)((u8 *)(chcr_req + 1) + kctx_len);
3083 ivptr = (u8 *)(phys_cpl + 1) + dst_size;
2debd332
HJ
3084 /* prepare a 16 byte iv */
3085 /* S A L T | IV | 0x00000001 */
3086 if (get_aead_subtype(tfm) ==
3087 CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106) {
1f479e4c
HJ
3088 memcpy(ivptr, aeadctx->salt, 4);
3089 memcpy(ivptr + 4, req->iv, GCM_RFC4106_IV_SIZE);
2debd332 3090 } else {
1f479e4c 3091 memcpy(ivptr, req->iv, GCM_AES_IV_SIZE);
2debd332 3092 }
1f479e4c 3093 *((unsigned int *)(ivptr + 12)) = htonl(0x01);
2debd332 3094
1f479e4c 3095 ulptx = (struct ulptx_sgl *)(ivptr + 16);
2debd332 3096
1f479e4c
HJ
3097 chcr_add_aead_dst_ent(req, phys_cpl, qid);
3098 chcr_add_aead_src_ent(req, ulptx);
ee0863ba 3099 atomic_inc(&adap->chcr_stats.aead_rqst);
1f479e4c
HJ
3100 temp = sizeof(struct cpl_rx_phys_dsgl) + dst_size + IV +
3101 kctx_len + (reqctx->imm ? (req->assoclen + req->cryptlen) : 0);
2f47d580
HJ
3102 create_wreq(a_ctx(tfm), chcr_req, &req->base, reqctx->imm, size,
3103 transhdr_len, temp, reqctx->verify);
2debd332 3104 reqctx->skb = skb;
2debd332
HJ
3105 return skb;
3106
2debd332 3107err:
4262c98a 3108 chcr_aead_common_exit(req);
5fe8c711 3109 return ERR_PTR(error);
2debd332
HJ
3110}
3111
3112
3113
3114static int chcr_aead_cra_init(struct crypto_aead *tfm)
3115{
2f47d580 3116 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
0e93708d
HJ
3117 struct aead_alg *alg = crypto_aead_alg(tfm);
3118
3119 aeadctx->sw_cipher = crypto_alloc_aead(alg->base.cra_name, 0,
5fe8c711
HJ
3120 CRYPTO_ALG_NEED_FALLBACK |
3121 CRYPTO_ALG_ASYNC);
0e93708d
HJ
3122 if (IS_ERR(aeadctx->sw_cipher))
3123 return PTR_ERR(aeadctx->sw_cipher);
3124 crypto_aead_set_reqsize(tfm, max(sizeof(struct chcr_aead_reqctx),
3125 sizeof(struct aead_request) +
3126 crypto_aead_reqsize(aeadctx->sw_cipher)));
2f47d580 3127 return chcr_device_init(a_ctx(tfm));
2debd332
HJ
3128}
3129
3130static void chcr_aead_cra_exit(struct crypto_aead *tfm)
3131{
2f47d580 3132 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
0e93708d 3133
0e93708d 3134 crypto_free_aead(aeadctx->sw_cipher);
2debd332
HJ
3135}
3136
3137static int chcr_authenc_null_setauthsize(struct crypto_aead *tfm,
3138 unsigned int authsize)
3139{
2f47d580 3140 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2debd332
HJ
3141
3142 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NOP;
3143 aeadctx->mayverify = VERIFY_HW;
0e93708d 3144 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
2debd332
HJ
3145}
3146static int chcr_authenc_setauthsize(struct crypto_aead *tfm,
3147 unsigned int authsize)
3148{
2f47d580 3149 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2debd332
HJ
3150 u32 maxauth = crypto_aead_maxauthsize(tfm);
3151
3152 /*SHA1 authsize in ipsec is 12 instead of 10 i.e maxauthsize / 2 is not
3153 * true for sha1. authsize == 12 condition should be before
3154 * authsize == (maxauth >> 1)
3155 */
3156 if (authsize == ICV_4) {
3157 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1;
3158 aeadctx->mayverify = VERIFY_HW;
3159 } else if (authsize == ICV_6) {
3160 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL2;
3161 aeadctx->mayverify = VERIFY_HW;
3162 } else if (authsize == ICV_10) {
3163 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_TRUNC_RFC4366;
3164 aeadctx->mayverify = VERIFY_HW;
3165 } else if (authsize == ICV_12) {
3166 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
3167 aeadctx->mayverify = VERIFY_HW;
3168 } else if (authsize == ICV_14) {
3169 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3;
3170 aeadctx->mayverify = VERIFY_HW;
3171 } else if (authsize == (maxauth >> 1)) {
3172 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
3173 aeadctx->mayverify = VERIFY_HW;
3174 } else if (authsize == maxauth) {
3175 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3176 aeadctx->mayverify = VERIFY_HW;
3177 } else {
3178 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3179 aeadctx->mayverify = VERIFY_SW;
3180 }
0e93708d 3181 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
2debd332
HJ
3182}
3183
3184
3185static int chcr_gcm_setauthsize(struct crypto_aead *tfm, unsigned int authsize)
3186{
2f47d580 3187 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2debd332
HJ
3188
3189 switch (authsize) {
3190 case ICV_4:
3191 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1;
3192 aeadctx->mayverify = VERIFY_HW;
3193 break;
3194 case ICV_8:
3195 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
3196 aeadctx->mayverify = VERIFY_HW;
3197 break;
3198 case ICV_12:
fc6176a2
CIK
3199 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
3200 aeadctx->mayverify = VERIFY_HW;
2debd332
HJ
3201 break;
3202 case ICV_14:
fc6176a2
CIK
3203 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3;
3204 aeadctx->mayverify = VERIFY_HW;
2debd332
HJ
3205 break;
3206 case ICV_16:
3207 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3208 aeadctx->mayverify = VERIFY_HW;
3209 break;
3210 case ICV_13:
3211 case ICV_15:
3212 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3213 aeadctx->mayverify = VERIFY_SW;
3214 break;
3215 default:
3216
3217 crypto_tfm_set_flags((struct crypto_tfm *) tfm,
3218 CRYPTO_TFM_RES_BAD_KEY_LEN);
3219 return -EINVAL;
3220 }
0e93708d 3221 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
2debd332
HJ
3222}
3223
3224static int chcr_4106_4309_setauthsize(struct crypto_aead *tfm,
3225 unsigned int authsize)
3226{
2f47d580 3227 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2debd332
HJ
3228
3229 switch (authsize) {
3230 case ICV_8:
3231 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
3232 aeadctx->mayverify = VERIFY_HW;
3233 break;
3234 case ICV_12:
3235 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
3236 aeadctx->mayverify = VERIFY_HW;
3237 break;
3238 case ICV_16:
3239 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3240 aeadctx->mayverify = VERIFY_HW;
3241 break;
3242 default:
3243 crypto_tfm_set_flags((struct crypto_tfm *)tfm,
3244 CRYPTO_TFM_RES_BAD_KEY_LEN);
3245 return -EINVAL;
3246 }
0e93708d 3247 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
2debd332
HJ
3248}
3249
3250static int chcr_ccm_setauthsize(struct crypto_aead *tfm,
3251 unsigned int authsize)
3252{
2f47d580 3253 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2debd332
HJ
3254
3255 switch (authsize) {
3256 case ICV_4:
3257 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL1;
3258 aeadctx->mayverify = VERIFY_HW;
3259 break;
3260 case ICV_6:
3261 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL2;
3262 aeadctx->mayverify = VERIFY_HW;
3263 break;
3264 case ICV_8:
3265 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_DIV2;
3266 aeadctx->mayverify = VERIFY_HW;
3267 break;
3268 case ICV_10:
3269 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_TRUNC_RFC4366;
3270 aeadctx->mayverify = VERIFY_HW;
3271 break;
3272 case ICV_12:
3273 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_IPSEC_96BIT;
3274 aeadctx->mayverify = VERIFY_HW;
3275 break;
3276 case ICV_14:
3277 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_PL3;
3278 aeadctx->mayverify = VERIFY_HW;
3279 break;
3280 case ICV_16:
3281 aeadctx->hmac_ctrl = CHCR_SCMD_HMAC_CTRL_NO_TRUNC;
3282 aeadctx->mayverify = VERIFY_HW;
3283 break;
3284 default:
3285 crypto_tfm_set_flags((struct crypto_tfm *)tfm,
3286 CRYPTO_TFM_RES_BAD_KEY_LEN);
3287 return -EINVAL;
3288 }
0e93708d 3289 return crypto_aead_setauthsize(aeadctx->sw_cipher, authsize);
2debd332
HJ
3290}
3291
0e93708d 3292static int chcr_ccm_common_setkey(struct crypto_aead *aead,
2debd332
HJ
3293 const u8 *key,
3294 unsigned int keylen)
3295{
2f47d580 3296 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead));
2debd332
HJ
3297 unsigned char ck_size, mk_size;
3298 int key_ctx_size = 0;
3299
125d01ca 3300 key_ctx_size = sizeof(struct _key_ctx) + roundup(keylen, 16) * 2;
2debd332 3301 if (keylen == AES_KEYSIZE_128) {
2debd332 3302 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
125d01ca 3303 mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_128;
2debd332
HJ
3304 } else if (keylen == AES_KEYSIZE_192) {
3305 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
3306 mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_192;
3307 } else if (keylen == AES_KEYSIZE_256) {
3308 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
3309 mk_size = CHCR_KEYCTX_MAC_KEY_SIZE_256;
3310 } else {
3311 crypto_tfm_set_flags((struct crypto_tfm *)aead,
3312 CRYPTO_TFM_RES_BAD_KEY_LEN);
3313 aeadctx->enckey_len = 0;
3314 return -EINVAL;
3315 }
3316 aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, mk_size, 0, 0,
3317 key_ctx_size >> 4);
0e93708d
HJ
3318 memcpy(aeadctx->key, key, keylen);
3319 aeadctx->enckey_len = keylen;
3320
2debd332
HJ
3321 return 0;
3322}
3323
0e93708d
HJ
3324static int chcr_aead_ccm_setkey(struct crypto_aead *aead,
3325 const u8 *key,
3326 unsigned int keylen)
3327{
2f47d580 3328 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead));
0e93708d
HJ
3329 int error;
3330
3331 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
3332 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(aead) &
3333 CRYPTO_TFM_REQ_MASK);
3334 error = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
3335 crypto_aead_clear_flags(aead, CRYPTO_TFM_RES_MASK);
3336 crypto_aead_set_flags(aead, crypto_aead_get_flags(aeadctx->sw_cipher) &
3337 CRYPTO_TFM_RES_MASK);
3338 if (error)
3339 return error;
3340 return chcr_ccm_common_setkey(aead, key, keylen);
3341}
3342
2debd332
HJ
3343static int chcr_aead_rfc4309_setkey(struct crypto_aead *aead, const u8 *key,
3344 unsigned int keylen)
3345{
2f47d580 3346 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead));
4dbeae42 3347 int error;
2debd332
HJ
3348
3349 if (keylen < 3) {
3350 crypto_tfm_set_flags((struct crypto_tfm *)aead,
3351 CRYPTO_TFM_RES_BAD_KEY_LEN);
3352 aeadctx->enckey_len = 0;
3353 return -EINVAL;
3354 }
4dbeae42
HJ
3355 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
3356 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(aead) &
3357 CRYPTO_TFM_REQ_MASK);
3358 error = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
3359 crypto_aead_clear_flags(aead, CRYPTO_TFM_RES_MASK);
3360 crypto_aead_set_flags(aead, crypto_aead_get_flags(aeadctx->sw_cipher) &
3361 CRYPTO_TFM_RES_MASK);
3362 if (error)
3363 return error;
2debd332
HJ
3364 keylen -= 3;
3365 memcpy(aeadctx->salt, key + keylen, 3);
0e93708d 3366 return chcr_ccm_common_setkey(aead, key, keylen);
2debd332
HJ
3367}
3368
3369static int chcr_gcm_setkey(struct crypto_aead *aead, const u8 *key,
3370 unsigned int keylen)
3371{
2f47d580 3372 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(aead));
2debd332 3373 struct chcr_gcm_ctx *gctx = GCM_CTX(aeadctx);
8356ea51 3374 struct crypto_cipher *cipher;
2debd332
HJ
3375 unsigned int ck_size;
3376 int ret = 0, key_ctx_size = 0;
3377
0e93708d
HJ
3378 aeadctx->enckey_len = 0;
3379 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
3380 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(aead)
3381 & CRYPTO_TFM_REQ_MASK);
3382 ret = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
3383 crypto_aead_clear_flags(aead, CRYPTO_TFM_RES_MASK);
3384 crypto_aead_set_flags(aead, crypto_aead_get_flags(aeadctx->sw_cipher) &
3385 CRYPTO_TFM_RES_MASK);
3386 if (ret)
3387 goto out;
3388
7c2cf1c4
HJ
3389 if (get_aead_subtype(aead) == CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106 &&
3390 keylen > 3) {
2debd332
HJ
3391 keylen -= 4; /* nonce/salt is present in the last 4 bytes */
3392 memcpy(aeadctx->salt, key + keylen, 4);
3393 }
3394 if (keylen == AES_KEYSIZE_128) {
3395 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
3396 } else if (keylen == AES_KEYSIZE_192) {
3397 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
3398 } else if (keylen == AES_KEYSIZE_256) {
3399 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
3400 } else {
3401 crypto_tfm_set_flags((struct crypto_tfm *)aead,
3402 CRYPTO_TFM_RES_BAD_KEY_LEN);
0e93708d 3403 pr_err("GCM: Invalid key length %d\n", keylen);
2debd332
HJ
3404 ret = -EINVAL;
3405 goto out;
3406 }
3407
3408 memcpy(aeadctx->key, key, keylen);
3409 aeadctx->enckey_len = keylen;
125d01ca 3410 key_ctx_size = sizeof(struct _key_ctx) + roundup(keylen, 16) +
2debd332 3411 AEAD_H_SIZE;
125d01ca 3412 aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size,
2debd332
HJ
3413 CHCR_KEYCTX_MAC_KEY_SIZE_128,
3414 0, 0,
3415 key_ctx_size >> 4);
8356ea51
HJ
3416 /* Calculate the H = CIPH(K, 0 repeated 16 times).
3417 * It will go in key context
2debd332 3418 */
8356ea51
HJ
3419 cipher = crypto_alloc_cipher("aes-generic", 0, 0);
3420 if (IS_ERR(cipher)) {
2debd332
HJ
3421 aeadctx->enckey_len = 0;
3422 ret = -ENOMEM;
3423 goto out;
3424 }
8356ea51
HJ
3425
3426 ret = crypto_cipher_setkey(cipher, key, keylen);
2debd332
HJ
3427 if (ret) {
3428 aeadctx->enckey_len = 0;
3429 goto out1;
3430 }
3431 memset(gctx->ghash_h, 0, AEAD_H_SIZE);
8356ea51 3432 crypto_cipher_encrypt_one(cipher, gctx->ghash_h, gctx->ghash_h);
2debd332
HJ
3433
3434out1:
8356ea51 3435 crypto_free_cipher(cipher);
2debd332
HJ
3436out:
3437 return ret;
3438}
3439
3440static int chcr_authenc_setkey(struct crypto_aead *authenc, const u8 *key,
3441 unsigned int keylen)
3442{
2f47d580 3443 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(authenc));
2debd332
HJ
3444 struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx);
3445 /* it contains auth and cipher key both*/
3446 struct crypto_authenc_keys keys;
3d64bd67 3447 unsigned int bs, subtype;
2debd332
HJ
3448 unsigned int max_authsize = crypto_aead_alg(authenc)->maxauthsize;
3449 int err = 0, i, key_ctx_len = 0;
3450 unsigned char ck_size = 0;
3451 unsigned char pad[CHCR_HASH_MAX_BLOCK_SIZE_128] = { 0 };
ec1bca94 3452 struct crypto_shash *base_hash = ERR_PTR(-EINVAL);
2debd332
HJ
3453 struct algo_param param;
3454 int align;
3455 u8 *o_ptr = NULL;
3456
0e93708d
HJ
3457 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
3458 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(authenc)
3459 & CRYPTO_TFM_REQ_MASK);
3460 err = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
3461 crypto_aead_clear_flags(authenc, CRYPTO_TFM_RES_MASK);
3462 crypto_aead_set_flags(authenc, crypto_aead_get_flags(aeadctx->sw_cipher)
3463 & CRYPTO_TFM_RES_MASK);
3464 if (err)
3465 goto out;
3466
2debd332
HJ
3467 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) {
3468 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
3469 goto out;
3470 }
3471
3472 if (get_alg_config(&param, max_authsize)) {
3473 pr_err("chcr : Unsupported digest size\n");
3474 goto out;
3475 }
3d64bd67
HJ
3476 subtype = get_aead_subtype(authenc);
3477 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA ||
3478 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) {
3479 if (keys.enckeylen < CTR_RFC3686_NONCE_SIZE)
3480 goto out;
3481 memcpy(aeadctx->nonce, keys.enckey + (keys.enckeylen
3482 - CTR_RFC3686_NONCE_SIZE), CTR_RFC3686_NONCE_SIZE);
3483 keys.enckeylen -= CTR_RFC3686_NONCE_SIZE;
3484 }
2debd332
HJ
3485 if (keys.enckeylen == AES_KEYSIZE_128) {
3486 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
3487 } else if (keys.enckeylen == AES_KEYSIZE_192) {
3488 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
3489 } else if (keys.enckeylen == AES_KEYSIZE_256) {
3490 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
3491 } else {
3492 pr_err("chcr : Unsupported cipher key\n");
3493 goto out;
3494 }
3495
3496 /* Copy only encryption key. We use authkey to generate h(ipad) and
3497 * h(opad) so authkey is not needed again. authkeylen size have the
3498 * size of the hash digest size.
3499 */
3500 memcpy(aeadctx->key, keys.enckey, keys.enckeylen);
3501 aeadctx->enckey_len = keys.enckeylen;
3d64bd67
HJ
3502 if (subtype == CRYPTO_ALG_SUB_TYPE_CBC_SHA ||
3503 subtype == CRYPTO_ALG_SUB_TYPE_CBC_NULL) {
2debd332 3504
3d64bd67
HJ
3505 get_aes_decrypt_key(actx->dec_rrkey, aeadctx->key,
3506 aeadctx->enckey_len << 3);
3507 }
2debd332
HJ
3508 base_hash = chcr_alloc_shash(max_authsize);
3509 if (IS_ERR(base_hash)) {
3510 pr_err("chcr : Base driver cannot be loaded\n");
0e93708d 3511 aeadctx->enckey_len = 0;
eb526531 3512 memzero_explicit(&keys, sizeof(keys));
0e93708d 3513 return -EINVAL;
324429d7 3514 }
2debd332
HJ
3515 {
3516 SHASH_DESC_ON_STACK(shash, base_hash);
6faa0f57 3517
2debd332
HJ
3518 shash->tfm = base_hash;
3519 shash->flags = crypto_shash_get_flags(base_hash);
3520 bs = crypto_shash_blocksize(base_hash);
3521 align = KEYCTX_ALIGN_PAD(max_authsize);
3522 o_ptr = actx->h_iopad + param.result_size + align;
3523
3524 if (keys.authkeylen > bs) {
3525 err = crypto_shash_digest(shash, keys.authkey,
3526 keys.authkeylen,
3527 o_ptr);
3528 if (err) {
3529 pr_err("chcr : Base driver cannot be loaded\n");
3530 goto out;
3531 }
3532 keys.authkeylen = max_authsize;
3533 } else
3534 memcpy(o_ptr, keys.authkey, keys.authkeylen);
3535
3536 /* Compute the ipad-digest*/
3537 memset(pad + keys.authkeylen, 0, bs - keys.authkeylen);
3538 memcpy(pad, o_ptr, keys.authkeylen);
3539 for (i = 0; i < bs >> 2; i++)
3540 *((unsigned int *)pad + i) ^= IPAD_DATA;
3541
3542 if (chcr_compute_partial_hash(shash, pad, actx->h_iopad,
3543 max_authsize))
3544 goto out;
3545 /* Compute the opad-digest */
3546 memset(pad + keys.authkeylen, 0, bs - keys.authkeylen);
3547 memcpy(pad, o_ptr, keys.authkeylen);
3548 for (i = 0; i < bs >> 2; i++)
3549 *((unsigned int *)pad + i) ^= OPAD_DATA;
3550
3551 if (chcr_compute_partial_hash(shash, pad, o_ptr, max_authsize))
3552 goto out;
3553
3554 /* convert the ipad and opad digest to network order */
3555 chcr_change_order(actx->h_iopad, param.result_size);
3556 chcr_change_order(o_ptr, param.result_size);
3557 key_ctx_len = sizeof(struct _key_ctx) +
125d01ca 3558 roundup(keys.enckeylen, 16) +
2debd332
HJ
3559 (param.result_size + align) * 2;
3560 aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, param.mk_size,
3561 0, 1, key_ctx_len >> 4);
3562 actx->auth_mode = param.auth_mode;
3563 chcr_free_shash(base_hash);
3564
eb526531 3565 memzero_explicit(&keys, sizeof(keys));
2debd332
HJ
3566 return 0;
3567 }
3568out:
3569 aeadctx->enckey_len = 0;
eb526531 3570 memzero_explicit(&keys, sizeof(keys));
ec1bca94 3571 if (!IS_ERR(base_hash))
2debd332
HJ
3572 chcr_free_shash(base_hash);
3573 return -EINVAL;
324429d7
HS
3574}
3575
2debd332
HJ
3576static int chcr_aead_digest_null_setkey(struct crypto_aead *authenc,
3577 const u8 *key, unsigned int keylen)
3578{
2f47d580 3579 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(authenc));
2debd332
HJ
3580 struct chcr_authenc_ctx *actx = AUTHENC_CTX(aeadctx);
3581 struct crypto_authenc_keys keys;
0e93708d 3582 int err;
2debd332 3583 /* it contains auth and cipher key both*/
3d64bd67 3584 unsigned int subtype;
2debd332
HJ
3585 int key_ctx_len = 0;
3586 unsigned char ck_size = 0;
3587
0e93708d
HJ
3588 crypto_aead_clear_flags(aeadctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
3589 crypto_aead_set_flags(aeadctx->sw_cipher, crypto_aead_get_flags(authenc)
3590 & CRYPTO_TFM_REQ_MASK);
3591 err = crypto_aead_setkey(aeadctx->sw_cipher, key, keylen);
3592 crypto_aead_clear_flags(authenc, CRYPTO_TFM_RES_MASK);
3593 crypto_aead_set_flags(authenc, crypto_aead_get_flags(aeadctx->sw_cipher)
3594 & CRYPTO_TFM_RES_MASK);
3595 if (err)
3596 goto out;
3597
2debd332
HJ
3598 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) {
3599 crypto_aead_set_flags(authenc, CRYPTO_TFM_RES_BAD_KEY_LEN);
3600 goto out;
3601 }
3d64bd67
HJ
3602 subtype = get_aead_subtype(authenc);
3603 if (subtype == CRYPTO_ALG_SUB_TYPE_CTR_SHA ||
3604 subtype == CRYPTO_ALG_SUB_TYPE_CTR_NULL) {
3605 if (keys.enckeylen < CTR_RFC3686_NONCE_SIZE)
3606 goto out;
3607 memcpy(aeadctx->nonce, keys.enckey + (keys.enckeylen
3608 - CTR_RFC3686_NONCE_SIZE), CTR_RFC3686_NONCE_SIZE);
3609 keys.enckeylen -= CTR_RFC3686_NONCE_SIZE;
3610 }
2debd332
HJ
3611 if (keys.enckeylen == AES_KEYSIZE_128) {
3612 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_128;
3613 } else if (keys.enckeylen == AES_KEYSIZE_192) {
3614 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_192;
3615 } else if (keys.enckeylen == AES_KEYSIZE_256) {
3616 ck_size = CHCR_KEYCTX_CIPHER_KEY_SIZE_256;
3617 } else {
3d64bd67 3618 pr_err("chcr : Unsupported cipher key %d\n", keys.enckeylen);
2debd332
HJ
3619 goto out;
3620 }
3621 memcpy(aeadctx->key, keys.enckey, keys.enckeylen);
3622 aeadctx->enckey_len = keys.enckeylen;
3d64bd67
HJ
3623 if (subtype == CRYPTO_ALG_SUB_TYPE_CBC_SHA ||
3624 subtype == CRYPTO_ALG_SUB_TYPE_CBC_NULL) {
3625 get_aes_decrypt_key(actx->dec_rrkey, aeadctx->key,
3626 aeadctx->enckey_len << 3);
3627 }
125d01ca 3628 key_ctx_len = sizeof(struct _key_ctx) + roundup(keys.enckeylen, 16);
2debd332
HJ
3629
3630 aeadctx->key_ctx_hdr = FILL_KEY_CTX_HDR(ck_size, CHCR_KEYCTX_NO_KEY, 0,
3631 0, key_ctx_len >> 4);
3632 actx->auth_mode = CHCR_SCMD_AUTH_MODE_NOP;
eb526531 3633 memzero_explicit(&keys, sizeof(keys));
2debd332
HJ
3634 return 0;
3635out:
3636 aeadctx->enckey_len = 0;
eb526531 3637 memzero_explicit(&keys, sizeof(keys));
2debd332
HJ
3638 return -EINVAL;
3639}
6dad4e8a
AG
3640
3641static int chcr_aead_op(struct aead_request *req,
6dad4e8a
AG
3642 int size,
3643 create_wr_t create_wr_fn)
3644{
3645 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
fef4912b 3646 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
6dad4e8a
AG
3647 struct uld_ctx *u_ctx;
3648 struct sk_buff *skb;
6faa0f57 3649 int isfull = 0;
fef4912b 3650 struct chcr_dev *cdev;
6dad4e8a 3651
fef4912b
HJ
3652 cdev = a_ctx(tfm)->dev;
3653 if (!cdev) {
6dad4e8a
AG
3654 pr_err("chcr : %s : No crypto device.\n", __func__);
3655 return -ENXIO;
3656 }
fef4912b
HJ
3657
3658 if (chcr_inc_wrcount(cdev)) {
3659 /* Detach state for CHCR means lldi or padap is freed.
3660 * We cannot increment fallback here.
3661 */
3662 return chcr_aead_fallback(req, reqctx->op);
3663 }
3664
6dad4e8a
AG
3665 u_ctx = ULD_CTX(a_ctx(tfm));
3666 if (cxgb4_is_crypto_q_full(u_ctx->lldi.ports[0],
3667 a_ctx(tfm)->tx_qidx)) {
6faa0f57 3668 isfull = 1;
fef4912b
HJ
3669 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
3670 chcr_dec_wrcount(cdev);
6faa0f57 3671 return -ENOSPC;
fef4912b 3672 }
6dad4e8a
AG
3673 }
3674
3675 /* Form a WR from req */
4262c98a 3676 skb = create_wr_fn(req, u_ctx->lldi.rxq_ids[a_ctx(tfm)->rx_qidx], size);
6dad4e8a 3677
fef4912b
HJ
3678 if (IS_ERR(skb) || !skb) {
3679 chcr_dec_wrcount(cdev);
6dad4e8a 3680 return PTR_ERR(skb);
fef4912b 3681 }
6dad4e8a
AG
3682
3683 skb->dev = u_ctx->lldi.ports[0];
3684 set_wr_txq(skb, CPL_PRIORITY_DATA, a_ctx(tfm)->tx_qidx);
3685 chcr_send_wr(skb);
6faa0f57 3686 return isfull ? -EBUSY : -EINPROGRESS;
6dad4e8a
AG
3687}
3688
2debd332
HJ
3689static int chcr_aead_encrypt(struct aead_request *req)
3690{
3691 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
3692 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
3693
3694 reqctx->verify = VERIFY_HW;
4262c98a 3695 reqctx->op = CHCR_ENCRYPT_OP;
2debd332
HJ
3696
3697 switch (get_aead_subtype(tfm)) {
3d64bd67
HJ
3698 case CRYPTO_ALG_SUB_TYPE_CTR_SHA:
3699 case CRYPTO_ALG_SUB_TYPE_CBC_SHA:
3700 case CRYPTO_ALG_SUB_TYPE_CBC_NULL:
3701 case CRYPTO_ALG_SUB_TYPE_CTR_NULL:
4262c98a 3702 return chcr_aead_op(req, 0, create_authenc_wr);
2debd332
HJ
3703 case CRYPTO_ALG_SUB_TYPE_AEAD_CCM:
3704 case CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309:
4262c98a 3705 return chcr_aead_op(req, 0, create_aead_ccm_wr);
2debd332 3706 default:
4262c98a 3707 return chcr_aead_op(req, 0, create_gcm_wr);
2debd332
HJ
3708 }
3709}
3710
3711static int chcr_aead_decrypt(struct aead_request *req)
3712{
3713 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
2f47d580 3714 struct chcr_aead_ctx *aeadctx = AEAD_CTX(a_ctx(tfm));
2debd332
HJ
3715 struct chcr_aead_reqctx *reqctx = aead_request_ctx(req);
3716 int size;
3717
3718 if (aeadctx->mayverify == VERIFY_SW) {
3719 size = crypto_aead_maxauthsize(tfm);
3720 reqctx->verify = VERIFY_SW;
3721 } else {
3722 size = 0;
3723 reqctx->verify = VERIFY_HW;
3724 }
4262c98a 3725 reqctx->op = CHCR_DECRYPT_OP;
2debd332 3726 switch (get_aead_subtype(tfm)) {
3d64bd67
HJ
3727 case CRYPTO_ALG_SUB_TYPE_CBC_SHA:
3728 case CRYPTO_ALG_SUB_TYPE_CTR_SHA:
3729 case CRYPTO_ALG_SUB_TYPE_CBC_NULL:
3730 case CRYPTO_ALG_SUB_TYPE_CTR_NULL:
4262c98a 3731 return chcr_aead_op(req, size, create_authenc_wr);
2debd332
HJ
3732 case CRYPTO_ALG_SUB_TYPE_AEAD_CCM:
3733 case CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309:
4262c98a 3734 return chcr_aead_op(req, size, create_aead_ccm_wr);
2debd332 3735 default:
4262c98a 3736 return chcr_aead_op(req, size, create_gcm_wr);
2debd332
HJ
3737 }
3738}
3739
324429d7
HS
3740static struct chcr_alg_template driver_algs[] = {
3741 /* AES-CBC */
3742 {
b8fd1f41 3743 .type = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_SUB_TYPE_CBC,
324429d7
HS
3744 .is_registered = 0,
3745 .alg.crypto = {
3746 .cra_name = "cbc(aes)",
2debd332 3747 .cra_driver_name = "cbc-aes-chcr",
324429d7 3748 .cra_blocksize = AES_BLOCK_SIZE,
324429d7 3749 .cra_init = chcr_cra_init,
b8fd1f41 3750 .cra_exit = chcr_cra_exit,
324429d7
HS
3751 .cra_u.ablkcipher = {
3752 .min_keysize = AES_MIN_KEY_SIZE,
3753 .max_keysize = AES_MAX_KEY_SIZE,
3754 .ivsize = AES_BLOCK_SIZE,
3755 .setkey = chcr_aes_cbc_setkey,
3756 .encrypt = chcr_aes_encrypt,
3757 .decrypt = chcr_aes_decrypt,
3758 }
3759 }
3760 },
3761 {
b8fd1f41 3762 .type = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_SUB_TYPE_XTS,
324429d7
HS
3763 .is_registered = 0,
3764 .alg.crypto = {
3765 .cra_name = "xts(aes)",
2debd332 3766 .cra_driver_name = "xts-aes-chcr",
324429d7 3767 .cra_blocksize = AES_BLOCK_SIZE,
324429d7
HS
3768 .cra_init = chcr_cra_init,
3769 .cra_exit = NULL,
b8fd1f41 3770 .cra_u .ablkcipher = {
324429d7
HS
3771 .min_keysize = 2 * AES_MIN_KEY_SIZE,
3772 .max_keysize = 2 * AES_MAX_KEY_SIZE,
3773 .ivsize = AES_BLOCK_SIZE,
3774 .setkey = chcr_aes_xts_setkey,
3775 .encrypt = chcr_aes_encrypt,
3776 .decrypt = chcr_aes_decrypt,
3777 }
3778 }
b8fd1f41
HJ
3779 },
3780 {
3781 .type = CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_SUB_TYPE_CTR,
3782 .is_registered = 0,
3783 .alg.crypto = {
3784 .cra_name = "ctr(aes)",
3785 .cra_driver_name = "ctr-aes-chcr",
3786 .cra_blocksize = 1,
3787 .cra_init = chcr_cra_init,
3788 .cra_exit = chcr_cra_exit,
3789 .cra_u.ablkcipher = {
3790 .min_keysize = AES_MIN_KEY_SIZE,
3791 .max_keysize = AES_MAX_KEY_SIZE,
3792 .ivsize = AES_BLOCK_SIZE,
3793 .setkey = chcr_aes_ctr_setkey,
3794 .encrypt = chcr_aes_encrypt,
3795 .decrypt = chcr_aes_decrypt,
3796 }
3797 }
3798 },
3799 {
3800 .type = CRYPTO_ALG_TYPE_ABLKCIPHER |
3801 CRYPTO_ALG_SUB_TYPE_CTR_RFC3686,
3802 .is_registered = 0,
3803 .alg.crypto = {
3804 .cra_name = "rfc3686(ctr(aes))",
3805 .cra_driver_name = "rfc3686-ctr-aes-chcr",
3806 .cra_blocksize = 1,
3807 .cra_init = chcr_rfc3686_init,
3808 .cra_exit = chcr_cra_exit,
3809 .cra_u.ablkcipher = {
3810 .min_keysize = AES_MIN_KEY_SIZE +
3811 CTR_RFC3686_NONCE_SIZE,
3812 .max_keysize = AES_MAX_KEY_SIZE +
3813 CTR_RFC3686_NONCE_SIZE,
3814 .ivsize = CTR_RFC3686_IV_SIZE,
3815 .setkey = chcr_aes_rfc3686_setkey,
3816 .encrypt = chcr_aes_encrypt,
3817 .decrypt = chcr_aes_decrypt,
3818 .geniv = "seqiv",
3819 }
324429d7
HS
3820 }
3821 },
3822 /* SHA */
3823 {
3824 .type = CRYPTO_ALG_TYPE_AHASH,
3825 .is_registered = 0,
3826 .alg.hash = {
3827 .halg.digestsize = SHA1_DIGEST_SIZE,
3828 .halg.base = {
3829 .cra_name = "sha1",
3830 .cra_driver_name = "sha1-chcr",
3831 .cra_blocksize = SHA1_BLOCK_SIZE,
3832 }
3833 }
3834 },
3835 {
3836 .type = CRYPTO_ALG_TYPE_AHASH,
3837 .is_registered = 0,
3838 .alg.hash = {
3839 .halg.digestsize = SHA256_DIGEST_SIZE,
3840 .halg.base = {
3841 .cra_name = "sha256",
3842 .cra_driver_name = "sha256-chcr",
3843 .cra_blocksize = SHA256_BLOCK_SIZE,
3844 }
3845 }
3846 },
3847 {
3848 .type = CRYPTO_ALG_TYPE_AHASH,
3849 .is_registered = 0,
3850 .alg.hash = {
3851 .halg.digestsize = SHA224_DIGEST_SIZE,
3852 .halg.base = {
3853 .cra_name = "sha224",
3854 .cra_driver_name = "sha224-chcr",
3855 .cra_blocksize = SHA224_BLOCK_SIZE,
3856 }
3857 }
3858 },
3859 {
3860 .type = CRYPTO_ALG_TYPE_AHASH,
3861 .is_registered = 0,
3862 .alg.hash = {
3863 .halg.digestsize = SHA384_DIGEST_SIZE,
3864 .halg.base = {
3865 .cra_name = "sha384",
3866 .cra_driver_name = "sha384-chcr",
3867 .cra_blocksize = SHA384_BLOCK_SIZE,
3868 }
3869 }
3870 },
3871 {
3872 .type = CRYPTO_ALG_TYPE_AHASH,
3873 .is_registered = 0,
3874 .alg.hash = {
3875 .halg.digestsize = SHA512_DIGEST_SIZE,
3876 .halg.base = {
3877 .cra_name = "sha512",
3878 .cra_driver_name = "sha512-chcr",
3879 .cra_blocksize = SHA512_BLOCK_SIZE,
3880 }
3881 }
3882 },
3883 /* HMAC */
3884 {
3885 .type = CRYPTO_ALG_TYPE_HMAC,
3886 .is_registered = 0,
3887 .alg.hash = {
3888 .halg.digestsize = SHA1_DIGEST_SIZE,
3889 .halg.base = {
3890 .cra_name = "hmac(sha1)",
2debd332 3891 .cra_driver_name = "hmac-sha1-chcr",
324429d7
HS
3892 .cra_blocksize = SHA1_BLOCK_SIZE,
3893 }
3894 }
3895 },
3896 {
3897 .type = CRYPTO_ALG_TYPE_HMAC,
3898 .is_registered = 0,
3899 .alg.hash = {
3900 .halg.digestsize = SHA224_DIGEST_SIZE,
3901 .halg.base = {
3902 .cra_name = "hmac(sha224)",
2debd332 3903 .cra_driver_name = "hmac-sha224-chcr",
324429d7
HS
3904 .cra_blocksize = SHA224_BLOCK_SIZE,
3905 }
3906 }
3907 },
3908 {
3909 .type = CRYPTO_ALG_TYPE_HMAC,
3910 .is_registered = 0,
3911 .alg.hash = {
3912 .halg.digestsize = SHA256_DIGEST_SIZE,
3913 .halg.base = {
3914 .cra_name = "hmac(sha256)",
2debd332 3915 .cra_driver_name = "hmac-sha256-chcr",
324429d7
HS
3916 .cra_blocksize = SHA256_BLOCK_SIZE,
3917 }
3918 }
3919 },
3920 {
3921 .type = CRYPTO_ALG_TYPE_HMAC,
3922 .is_registered = 0,
3923 .alg.hash = {
3924 .halg.digestsize = SHA384_DIGEST_SIZE,
3925 .halg.base = {
3926 .cra_name = "hmac(sha384)",
2debd332 3927 .cra_driver_name = "hmac-sha384-chcr",
324429d7
HS
3928 .cra_blocksize = SHA384_BLOCK_SIZE,
3929 }
3930 }
3931 },
3932 {
3933 .type = CRYPTO_ALG_TYPE_HMAC,
3934 .is_registered = 0,
3935 .alg.hash = {
3936 .halg.digestsize = SHA512_DIGEST_SIZE,
3937 .halg.base = {
3938 .cra_name = "hmac(sha512)",
2debd332 3939 .cra_driver_name = "hmac-sha512-chcr",
324429d7
HS
3940 .cra_blocksize = SHA512_BLOCK_SIZE,
3941 }
3942 }
3943 },
2debd332
HJ
3944 /* Add AEAD Algorithms */
3945 {
3946 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_GCM,
3947 .is_registered = 0,
3948 .alg.aead = {
3949 .base = {
3950 .cra_name = "gcm(aes)",
3951 .cra_driver_name = "gcm-aes-chcr",
3952 .cra_blocksize = 1,
e29abda5 3953 .cra_priority = CHCR_AEAD_PRIORITY,
2debd332
HJ
3954 .cra_ctxsize = sizeof(struct chcr_context) +
3955 sizeof(struct chcr_aead_ctx) +
3956 sizeof(struct chcr_gcm_ctx),
3957 },
8f6acb7f 3958 .ivsize = GCM_AES_IV_SIZE,
2debd332
HJ
3959 .maxauthsize = GHASH_DIGEST_SIZE,
3960 .setkey = chcr_gcm_setkey,
3961 .setauthsize = chcr_gcm_setauthsize,
3962 }
3963 },
3964 {
3965 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_RFC4106,
3966 .is_registered = 0,
3967 .alg.aead = {
3968 .base = {
3969 .cra_name = "rfc4106(gcm(aes))",
3970 .cra_driver_name = "rfc4106-gcm-aes-chcr",
3971 .cra_blocksize = 1,
e29abda5 3972 .cra_priority = CHCR_AEAD_PRIORITY + 1,
2debd332
HJ
3973 .cra_ctxsize = sizeof(struct chcr_context) +
3974 sizeof(struct chcr_aead_ctx) +
3975 sizeof(struct chcr_gcm_ctx),
3976
3977 },
8f6acb7f 3978 .ivsize = GCM_RFC4106_IV_SIZE,
2debd332
HJ
3979 .maxauthsize = GHASH_DIGEST_SIZE,
3980 .setkey = chcr_gcm_setkey,
3981 .setauthsize = chcr_4106_4309_setauthsize,
3982 }
3983 },
3984 {
3985 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_CCM,
3986 .is_registered = 0,
3987 .alg.aead = {
3988 .base = {
3989 .cra_name = "ccm(aes)",
3990 .cra_driver_name = "ccm-aes-chcr",
3991 .cra_blocksize = 1,
e29abda5 3992 .cra_priority = CHCR_AEAD_PRIORITY,
2debd332
HJ
3993 .cra_ctxsize = sizeof(struct chcr_context) +
3994 sizeof(struct chcr_aead_ctx),
3995
3996 },
3997 .ivsize = AES_BLOCK_SIZE,
3998 .maxauthsize = GHASH_DIGEST_SIZE,
3999 .setkey = chcr_aead_ccm_setkey,
4000 .setauthsize = chcr_ccm_setauthsize,
4001 }
4002 },
4003 {
4004 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_AEAD_RFC4309,
4005 .is_registered = 0,
4006 .alg.aead = {
4007 .base = {
4008 .cra_name = "rfc4309(ccm(aes))",
4009 .cra_driver_name = "rfc4309-ccm-aes-chcr",
4010 .cra_blocksize = 1,
e29abda5 4011 .cra_priority = CHCR_AEAD_PRIORITY + 1,
2debd332
HJ
4012 .cra_ctxsize = sizeof(struct chcr_context) +
4013 sizeof(struct chcr_aead_ctx),
4014
4015 },
4016 .ivsize = 8,
4017 .maxauthsize = GHASH_DIGEST_SIZE,
4018 .setkey = chcr_aead_rfc4309_setkey,
4019 .setauthsize = chcr_4106_4309_setauthsize,
4020 }
4021 },
4022 {
3d64bd67 4023 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
2debd332
HJ
4024 .is_registered = 0,
4025 .alg.aead = {
4026 .base = {
4027 .cra_name = "authenc(hmac(sha1),cbc(aes))",
4028 .cra_driver_name =
4029 "authenc-hmac-sha1-cbc-aes-chcr",
4030 .cra_blocksize = AES_BLOCK_SIZE,
e29abda5 4031 .cra_priority = CHCR_AEAD_PRIORITY,
2debd332
HJ
4032 .cra_ctxsize = sizeof(struct chcr_context) +
4033 sizeof(struct chcr_aead_ctx) +
4034 sizeof(struct chcr_authenc_ctx),
4035
4036 },
4037 .ivsize = AES_BLOCK_SIZE,
4038 .maxauthsize = SHA1_DIGEST_SIZE,
4039 .setkey = chcr_authenc_setkey,
4040 .setauthsize = chcr_authenc_setauthsize,
4041 }
4042 },
4043 {
3d64bd67 4044 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
2debd332
HJ
4045 .is_registered = 0,
4046 .alg.aead = {
4047 .base = {
4048
4049 .cra_name = "authenc(hmac(sha256),cbc(aes))",
4050 .cra_driver_name =
4051 "authenc-hmac-sha256-cbc-aes-chcr",
4052 .cra_blocksize = AES_BLOCK_SIZE,
e29abda5 4053 .cra_priority = CHCR_AEAD_PRIORITY,
2debd332
HJ
4054 .cra_ctxsize = sizeof(struct chcr_context) +
4055 sizeof(struct chcr_aead_ctx) +
4056 sizeof(struct chcr_authenc_ctx),
4057
4058 },
4059 .ivsize = AES_BLOCK_SIZE,
4060 .maxauthsize = SHA256_DIGEST_SIZE,
4061 .setkey = chcr_authenc_setkey,
4062 .setauthsize = chcr_authenc_setauthsize,
4063 }
4064 },
4065 {
3d64bd67 4066 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
2debd332
HJ
4067 .is_registered = 0,
4068 .alg.aead = {
4069 .base = {
4070 .cra_name = "authenc(hmac(sha224),cbc(aes))",
4071 .cra_driver_name =
4072 "authenc-hmac-sha224-cbc-aes-chcr",
4073 .cra_blocksize = AES_BLOCK_SIZE,
e29abda5 4074 .cra_priority = CHCR_AEAD_PRIORITY,
2debd332
HJ
4075 .cra_ctxsize = sizeof(struct chcr_context) +
4076 sizeof(struct chcr_aead_ctx) +
4077 sizeof(struct chcr_authenc_ctx),
4078 },
4079 .ivsize = AES_BLOCK_SIZE,
4080 .maxauthsize = SHA224_DIGEST_SIZE,
4081 .setkey = chcr_authenc_setkey,
4082 .setauthsize = chcr_authenc_setauthsize,
4083 }
4084 },
4085 {
3d64bd67 4086 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
2debd332
HJ
4087 .is_registered = 0,
4088 .alg.aead = {
4089 .base = {
4090 .cra_name = "authenc(hmac(sha384),cbc(aes))",
4091 .cra_driver_name =
4092 "authenc-hmac-sha384-cbc-aes-chcr",
4093 .cra_blocksize = AES_BLOCK_SIZE,
e29abda5 4094 .cra_priority = CHCR_AEAD_PRIORITY,
2debd332
HJ
4095 .cra_ctxsize = sizeof(struct chcr_context) +
4096 sizeof(struct chcr_aead_ctx) +
4097 sizeof(struct chcr_authenc_ctx),
4098
4099 },
4100 .ivsize = AES_BLOCK_SIZE,
4101 .maxauthsize = SHA384_DIGEST_SIZE,
4102 .setkey = chcr_authenc_setkey,
4103 .setauthsize = chcr_authenc_setauthsize,
4104 }
4105 },
4106 {
3d64bd67 4107 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_SHA,
2debd332
HJ
4108 .is_registered = 0,
4109 .alg.aead = {
4110 .base = {
4111 .cra_name = "authenc(hmac(sha512),cbc(aes))",
4112 .cra_driver_name =
4113 "authenc-hmac-sha512-cbc-aes-chcr",
4114 .cra_blocksize = AES_BLOCK_SIZE,
e29abda5 4115 .cra_priority = CHCR_AEAD_PRIORITY,
2debd332
HJ
4116 .cra_ctxsize = sizeof(struct chcr_context) +
4117 sizeof(struct chcr_aead_ctx) +
4118 sizeof(struct chcr_authenc_ctx),
4119
4120 },
4121 .ivsize = AES_BLOCK_SIZE,
4122 .maxauthsize = SHA512_DIGEST_SIZE,
4123 .setkey = chcr_authenc_setkey,
4124 .setauthsize = chcr_authenc_setauthsize,
4125 }
4126 },
4127 {
3d64bd67 4128 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CBC_NULL,
2debd332
HJ
4129 .is_registered = 0,
4130 .alg.aead = {
4131 .base = {
4132 .cra_name = "authenc(digest_null,cbc(aes))",
4133 .cra_driver_name =
4134 "authenc-digest_null-cbc-aes-chcr",
4135 .cra_blocksize = AES_BLOCK_SIZE,
e29abda5 4136 .cra_priority = CHCR_AEAD_PRIORITY,
2debd332
HJ
4137 .cra_ctxsize = sizeof(struct chcr_context) +
4138 sizeof(struct chcr_aead_ctx) +
4139 sizeof(struct chcr_authenc_ctx),
4140
4141 },
4142 .ivsize = AES_BLOCK_SIZE,
4143 .maxauthsize = 0,
4144 .setkey = chcr_aead_digest_null_setkey,
4145 .setauthsize = chcr_authenc_null_setauthsize,
4146 }
4147 },
3d64bd67
HJ
4148 {
4149 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
4150 .is_registered = 0,
4151 .alg.aead = {
4152 .base = {
4153 .cra_name = "authenc(hmac(sha1),rfc3686(ctr(aes)))",
4154 .cra_driver_name =
4155 "authenc-hmac-sha1-rfc3686-ctr-aes-chcr",
4156 .cra_blocksize = 1,
4157 .cra_priority = CHCR_AEAD_PRIORITY,
4158 .cra_ctxsize = sizeof(struct chcr_context) +
4159 sizeof(struct chcr_aead_ctx) +
4160 sizeof(struct chcr_authenc_ctx),
4161
4162 },
4163 .ivsize = CTR_RFC3686_IV_SIZE,
4164 .maxauthsize = SHA1_DIGEST_SIZE,
4165 .setkey = chcr_authenc_setkey,
4166 .setauthsize = chcr_authenc_setauthsize,
4167 }
4168 },
4169 {
4170 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
4171 .is_registered = 0,
4172 .alg.aead = {
4173 .base = {
4174
4175 .cra_name = "authenc(hmac(sha256),rfc3686(ctr(aes)))",
4176 .cra_driver_name =
4177 "authenc-hmac-sha256-rfc3686-ctr-aes-chcr",
4178 .cra_blocksize = 1,
4179 .cra_priority = CHCR_AEAD_PRIORITY,
4180 .cra_ctxsize = sizeof(struct chcr_context) +
4181 sizeof(struct chcr_aead_ctx) +
4182 sizeof(struct chcr_authenc_ctx),
4183
4184 },
4185 .ivsize = CTR_RFC3686_IV_SIZE,
4186 .maxauthsize = SHA256_DIGEST_SIZE,
4187 .setkey = chcr_authenc_setkey,
4188 .setauthsize = chcr_authenc_setauthsize,
4189 }
4190 },
4191 {
4192 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
4193 .is_registered = 0,
4194 .alg.aead = {
4195 .base = {
4196 .cra_name = "authenc(hmac(sha224),rfc3686(ctr(aes)))",
4197 .cra_driver_name =
4198 "authenc-hmac-sha224-rfc3686-ctr-aes-chcr",
4199 .cra_blocksize = 1,
4200 .cra_priority = CHCR_AEAD_PRIORITY,
4201 .cra_ctxsize = sizeof(struct chcr_context) +
4202 sizeof(struct chcr_aead_ctx) +
4203 sizeof(struct chcr_authenc_ctx),
4204 },
4205 .ivsize = CTR_RFC3686_IV_SIZE,
4206 .maxauthsize = SHA224_DIGEST_SIZE,
4207 .setkey = chcr_authenc_setkey,
4208 .setauthsize = chcr_authenc_setauthsize,
4209 }
4210 },
4211 {
4212 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
4213 .is_registered = 0,
4214 .alg.aead = {
4215 .base = {
4216 .cra_name = "authenc(hmac(sha384),rfc3686(ctr(aes)))",
4217 .cra_driver_name =
4218 "authenc-hmac-sha384-rfc3686-ctr-aes-chcr",
4219 .cra_blocksize = 1,
4220 .cra_priority = CHCR_AEAD_PRIORITY,
4221 .cra_ctxsize = sizeof(struct chcr_context) +
4222 sizeof(struct chcr_aead_ctx) +
4223 sizeof(struct chcr_authenc_ctx),
4224
4225 },
4226 .ivsize = CTR_RFC3686_IV_SIZE,
4227 .maxauthsize = SHA384_DIGEST_SIZE,
4228 .setkey = chcr_authenc_setkey,
4229 .setauthsize = chcr_authenc_setauthsize,
4230 }
4231 },
4232 {
4233 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_SHA,
4234 .is_registered = 0,
4235 .alg.aead = {
4236 .base = {
4237 .cra_name = "authenc(hmac(sha512),rfc3686(ctr(aes)))",
4238 .cra_driver_name =
4239 "authenc-hmac-sha512-rfc3686-ctr-aes-chcr",
4240 .cra_blocksize = 1,
4241 .cra_priority = CHCR_AEAD_PRIORITY,
4242 .cra_ctxsize = sizeof(struct chcr_context) +
4243 sizeof(struct chcr_aead_ctx) +
4244 sizeof(struct chcr_authenc_ctx),
4245
4246 },
4247 .ivsize = CTR_RFC3686_IV_SIZE,
4248 .maxauthsize = SHA512_DIGEST_SIZE,
4249 .setkey = chcr_authenc_setkey,
4250 .setauthsize = chcr_authenc_setauthsize,
4251 }
4252 },
4253 {
4254 .type = CRYPTO_ALG_TYPE_AEAD | CRYPTO_ALG_SUB_TYPE_CTR_NULL,
4255 .is_registered = 0,
4256 .alg.aead = {
4257 .base = {
4258 .cra_name = "authenc(digest_null,rfc3686(ctr(aes)))",
4259 .cra_driver_name =
4260 "authenc-digest_null-rfc3686-ctr-aes-chcr",
4261 .cra_blocksize = 1,
4262 .cra_priority = CHCR_AEAD_PRIORITY,
4263 .cra_ctxsize = sizeof(struct chcr_context) +
4264 sizeof(struct chcr_aead_ctx) +
4265 sizeof(struct chcr_authenc_ctx),
4266
4267 },
4268 .ivsize = CTR_RFC3686_IV_SIZE,
4269 .maxauthsize = 0,
4270 .setkey = chcr_aead_digest_null_setkey,
4271 .setauthsize = chcr_authenc_null_setauthsize,
4272 }
4273 },
324429d7
HS
4274};
4275
4276/*
4277 * chcr_unregister_alg - Deregister crypto algorithms with
4278 * kernel framework.
4279 */
4280static int chcr_unregister_alg(void)
4281{
4282 int i;
4283
4284 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
4285 switch (driver_algs[i].type & CRYPTO_ALG_TYPE_MASK) {
4286 case CRYPTO_ALG_TYPE_ABLKCIPHER:
4287 if (driver_algs[i].is_registered)
4288 crypto_unregister_alg(
4289 &driver_algs[i].alg.crypto);
4290 break;
2debd332
HJ
4291 case CRYPTO_ALG_TYPE_AEAD:
4292 if (driver_algs[i].is_registered)
4293 crypto_unregister_aead(
4294 &driver_algs[i].alg.aead);
4295 break;
324429d7
HS
4296 case CRYPTO_ALG_TYPE_AHASH:
4297 if (driver_algs[i].is_registered)
4298 crypto_unregister_ahash(
4299 &driver_algs[i].alg.hash);
4300 break;
4301 }
4302 driver_algs[i].is_registered = 0;
4303 }
4304 return 0;
4305}
4306
4307#define SZ_AHASH_CTX sizeof(struct chcr_context)
4308#define SZ_AHASH_H_CTX (sizeof(struct chcr_context) + sizeof(struct hmac_ctx))
4309#define SZ_AHASH_REQ_CTX sizeof(struct chcr_ahash_req_ctx)
324429d7
HS
4310
4311/*
4312 * chcr_register_alg - Register crypto algorithms with kernel framework.
4313 */
4314static int chcr_register_alg(void)
4315{
4316 struct crypto_alg ai;
4317 struct ahash_alg *a_hash;
4318 int err = 0, i;
4319 char *name = NULL;
4320
4321 for (i = 0; i < ARRAY_SIZE(driver_algs); i++) {
4322 if (driver_algs[i].is_registered)
4323 continue;
4324 switch (driver_algs[i].type & CRYPTO_ALG_TYPE_MASK) {
4325 case CRYPTO_ALG_TYPE_ABLKCIPHER:
b8fd1f41
HJ
4326 driver_algs[i].alg.crypto.cra_priority =
4327 CHCR_CRA_PRIORITY;
4328 driver_algs[i].alg.crypto.cra_module = THIS_MODULE;
4329 driver_algs[i].alg.crypto.cra_flags =
4330 CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC |
4331 CRYPTO_ALG_NEED_FALLBACK;
4332 driver_algs[i].alg.crypto.cra_ctxsize =
4333 sizeof(struct chcr_context) +
4334 sizeof(struct ablk_ctx);
4335 driver_algs[i].alg.crypto.cra_alignmask = 0;
4336 driver_algs[i].alg.crypto.cra_type =
4337 &crypto_ablkcipher_type;
324429d7
HS
4338 err = crypto_register_alg(&driver_algs[i].alg.crypto);
4339 name = driver_algs[i].alg.crypto.cra_driver_name;
4340 break;
2debd332 4341 case CRYPTO_ALG_TYPE_AEAD:
2debd332 4342 driver_algs[i].alg.aead.base.cra_flags =
3f4a537a 4343 CRYPTO_ALG_ASYNC | CRYPTO_ALG_NEED_FALLBACK;
2debd332
HJ
4344 driver_algs[i].alg.aead.encrypt = chcr_aead_encrypt;
4345 driver_algs[i].alg.aead.decrypt = chcr_aead_decrypt;
4346 driver_algs[i].alg.aead.init = chcr_aead_cra_init;
4347 driver_algs[i].alg.aead.exit = chcr_aead_cra_exit;
4348 driver_algs[i].alg.aead.base.cra_module = THIS_MODULE;
4349 err = crypto_register_aead(&driver_algs[i].alg.aead);
4350 name = driver_algs[i].alg.aead.base.cra_driver_name;
4351 break;
324429d7
HS
4352 case CRYPTO_ALG_TYPE_AHASH:
4353 a_hash = &driver_algs[i].alg.hash;
4354 a_hash->update = chcr_ahash_update;
4355 a_hash->final = chcr_ahash_final;
4356 a_hash->finup = chcr_ahash_finup;
4357 a_hash->digest = chcr_ahash_digest;
4358 a_hash->export = chcr_ahash_export;
4359 a_hash->import = chcr_ahash_import;
4360 a_hash->halg.statesize = SZ_AHASH_REQ_CTX;
4361 a_hash->halg.base.cra_priority = CHCR_CRA_PRIORITY;
4362 a_hash->halg.base.cra_module = THIS_MODULE;
6a38f622 4363 a_hash->halg.base.cra_flags = CRYPTO_ALG_ASYNC;
324429d7
HS
4364 a_hash->halg.base.cra_alignmask = 0;
4365 a_hash->halg.base.cra_exit = NULL;
324429d7
HS
4366
4367 if (driver_algs[i].type == CRYPTO_ALG_TYPE_HMAC) {
4368 a_hash->halg.base.cra_init = chcr_hmac_cra_init;
4369 a_hash->halg.base.cra_exit = chcr_hmac_cra_exit;
4370 a_hash->init = chcr_hmac_init;
4371 a_hash->setkey = chcr_ahash_setkey;
4372 a_hash->halg.base.cra_ctxsize = SZ_AHASH_H_CTX;
4373 } else {
4374 a_hash->init = chcr_sha_init;
4375 a_hash->halg.base.cra_ctxsize = SZ_AHASH_CTX;
4376 a_hash->halg.base.cra_init = chcr_sha_cra_init;
4377 }
4378 err = crypto_register_ahash(&driver_algs[i].alg.hash);
4379 ai = driver_algs[i].alg.hash.halg.base;
4380 name = ai.cra_driver_name;
4381 break;
4382 }
4383 if (err) {
4384 pr_err("chcr : %s : Algorithm registration failed\n",
4385 name);
4386 goto register_err;
4387 } else {
4388 driver_algs[i].is_registered = 1;
4389 }
4390 }
4391 return 0;
4392
4393register_err:
4394 chcr_unregister_alg();
4395 return err;
4396}
4397
4398/*
4399 * start_crypto - Register the crypto algorithms.
4400 * This should called once when the first device comesup. After this
4401 * kernel will start calling driver APIs for crypto operations.
4402 */
4403int start_crypto(void)
4404{
4405 return chcr_register_alg();
4406}
4407
4408/*
4409 * stop_crypto - Deregister all the crypto algorithms with kernel.
4410 * This should be called once when the last device goes down. After this
4411 * kernel will not call the driver API for crypto operations.
4412 */
4413int stop_crypto(void)
4414{
4415 chcr_unregister_alg();
4416 return 0;
4417}