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