forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-01-31 f70575805708cabdedea7498aaa3f710fde4d920
kernel/drivers/crypto/amcc/crypto4xx_core.c
....@@ -1,18 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /**
23 * AMCC SoC PPC4xx Crypto Driver
34 *
45 * Copyright (c) 2008 Applied Micro Circuits Corporation.
56 * All rights reserved. James Hsiao <jhsiao@amcc.com>
6
- *
7
- * This program is free software; you can redistribute it and/or modify
8
- * it under the terms of the GNU General Public License as published by
9
- * the Free Software Foundation; either version 2 of the License, or
10
- * (at your option) any later version.
11
- *
12
- * This program is distributed in the hope that it will be useful,
13
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
- * GNU General Public License for more details.
167 *
178 * This file implements AMCC crypto offload Linux device driver for use with
189 * Linux CryptoAPI.
....@@ -40,9 +31,11 @@
4031 #include <crypto/ctr.h>
4132 #include <crypto/gcm.h>
4233 #include <crypto/sha.h>
34
+#include <crypto/rng.h>
4335 #include <crypto/scatterwalk.h>
4436 #include <crypto/skcipher.h>
4537 #include <crypto/internal/aead.h>
38
+#include <crypto/internal/rng.h>
4639 #include <crypto/internal/skcipher.h>
4740 #include "crypto4xx_reg_def.h"
4841 #include "crypto4xx_core.h"
....@@ -176,7 +169,7 @@
176169 int i;
177170 dev->pdr = dma_alloc_coherent(dev->core_dev->device,
178171 sizeof(struct ce_pd) * PPC4XX_NUM_PD,
179
- &dev->pdr_pa, GFP_ATOMIC);
172
+ &dev->pdr_pa, GFP_KERNEL);
180173 if (!dev->pdr)
181174 return -ENOMEM;
182175
....@@ -189,17 +182,16 @@
189182 dev->pdr_pa);
190183 return -ENOMEM;
191184 }
192
- memset(dev->pdr, 0, sizeof(struct ce_pd) * PPC4XX_NUM_PD);
193185 dev->shadow_sa_pool = dma_alloc_coherent(dev->core_dev->device,
194186 sizeof(union shadow_sa_buf) * PPC4XX_NUM_PD,
195187 &dev->shadow_sa_pool_pa,
196
- GFP_ATOMIC);
188
+ GFP_KERNEL);
197189 if (!dev->shadow_sa_pool)
198190 return -ENOMEM;
199191
200192 dev->shadow_sr_pool = dma_alloc_coherent(dev->core_dev->device,
201193 sizeof(struct sa_state_record) * PPC4XX_NUM_PD,
202
- &dev->shadow_sr_pool_pa, GFP_ATOMIC);
194
+ &dev->shadow_sr_pool_pa, GFP_KERNEL);
203195 if (!dev->shadow_sr_pool)
204196 return -ENOMEM;
205197 for (i = 0; i < PPC4XX_NUM_PD; i++) {
....@@ -283,9 +275,9 @@
283275 */
284276 static u32 crypto4xx_build_gdr(struct crypto4xx_device *dev)
285277 {
286
- dev->gdr = dma_zalloc_coherent(dev->core_dev->device,
287
- sizeof(struct ce_gd) * PPC4XX_NUM_GD,
288
- &dev->gdr_pa, GFP_ATOMIC);
278
+ dev->gdr = dma_alloc_coherent(dev->core_dev->device,
279
+ sizeof(struct ce_gd) * PPC4XX_NUM_GD,
280
+ &dev->gdr_pa, GFP_KERNEL);
289281 if (!dev->gdr)
290282 return -ENOMEM;
291283
....@@ -294,7 +286,8 @@
294286
295287 static inline void crypto4xx_destroy_gdr(struct crypto4xx_device *dev)
296288 {
297
- dma_free_coherent(dev->core_dev->device,
289
+ if (dev->gdr)
290
+ dma_free_coherent(dev->core_dev->device,
298291 sizeof(struct ce_gd) * PPC4XX_NUM_GD,
299292 dev->gdr, dev->gdr_pa);
300293 }
....@@ -362,18 +355,18 @@
362355 {
363356 int i;
364357
365
- /* alloc memory for scatter descriptor ring */
366
- dev->sdr = dma_alloc_coherent(dev->core_dev->device,
367
- sizeof(struct ce_sd) * PPC4XX_NUM_SD,
368
- &dev->sdr_pa, GFP_ATOMIC);
369
- if (!dev->sdr)
370
- return -ENOMEM;
371
-
372358 dev->scatter_buffer_va =
373359 dma_alloc_coherent(dev->core_dev->device,
374360 PPC4XX_SD_BUFFER_SIZE * PPC4XX_NUM_SD,
375
- &dev->scatter_buffer_pa, GFP_ATOMIC);
361
+ &dev->scatter_buffer_pa, GFP_KERNEL);
376362 if (!dev->scatter_buffer_va)
363
+ return -ENOMEM;
364
+
365
+ /* alloc memory for scatter descriptor ring */
366
+ dev->sdr = dma_alloc_coherent(dev->core_dev->device,
367
+ sizeof(struct ce_sd) * PPC4XX_NUM_SD,
368
+ &dev->sdr_pa, GFP_KERNEL);
369
+ if (!dev->sdr)
377370 return -ENOMEM;
378371
379372 for (i = 0; i < PPC4XX_NUM_SD; i++) {
....@@ -529,17 +522,16 @@
529522 {
530523 struct skcipher_request *req;
531524 struct scatterlist *dst;
532
- dma_addr_t addr;
533525
534526 req = skcipher_request_cast(pd_uinfo->async_req);
535527
536
- if (pd_uinfo->using_sd) {
528
+ if (pd_uinfo->sa_va->sa_command_0.bf.scatter) {
537529 crypto4xx_copy_pkt_to_dst(dev, pd, pd_uinfo,
538530 req->cryptlen, req->dst);
539531 } else {
540532 dst = pd_uinfo->dest_va;
541
- addr = dma_map_page(dev->core_dev->device, sg_page(dst),
542
- dst->offset, dst->length, DMA_FROM_DEVICE);
533
+ dma_unmap_page(dev->core_dev->device, pd->dest, dst->length,
534
+ DMA_FROM_DEVICE);
543535 }
544536
545537 if (pd_uinfo->sa_va->sa_command_0.bf.save_iv == SA_SAVE_IV) {
....@@ -564,10 +556,9 @@
564556 struct ahash_request *ahash_req;
565557
566558 ahash_req = ahash_request_cast(pd_uinfo->async_req);
567
- ctx = crypto_tfm_ctx(ahash_req->base.tfm);
559
+ ctx = crypto_ahash_ctx(crypto_ahash_reqtfm(ahash_req));
568560
569
- crypto4xx_copy_digest_to_dst(ahash_req->result, pd_uinfo,
570
- crypto_tfm_ctx(ahash_req->base.tfm));
561
+ crypto4xx_copy_digest_to_dst(ahash_req->result, pd_uinfo, ctx);
571562 crypto4xx_ret_sg_desc(dev, pd_uinfo);
572563
573564 if (pd_uinfo->state & PD_ENTRY_BUSY)
....@@ -587,12 +578,12 @@
587578 u32 icv[AES_BLOCK_SIZE];
588579 int err = 0;
589580
590
- if (pd_uinfo->using_sd) {
581
+ if (pd_uinfo->sa_va->sa_command_0.bf.scatter) {
591582 crypto4xx_copy_pkt_to_dst(dev, pd, pd_uinfo,
592583 pd->pd_ctl_len.bf.pkt_len,
593584 dst);
594585 } else {
595
- __dma_sync_page(sg_page(dst), dst->offset, dst->length,
586
+ dma_unmap_page(dev->core_dev->device, pd->dest, dst->length,
596587 DMA_FROM_DEVICE);
597588 }
598589
....@@ -881,7 +872,6 @@
881872 * we know application give us dst a whole piece of memory
882873 * no need to use scatter ring.
883874 */
884
- pd_uinfo->using_sd = 0;
885875 pd_uinfo->first_sd = 0xffffffff;
886876 sa->sa_command_0.bf.scatter = 0;
887877 pd->dest = (u32)dma_map_page(dev->core_dev->device,
....@@ -895,7 +885,6 @@
895885 u32 sd_idx = fst_sd;
896886 nbytes = datalen;
897887 sa->sa_command_0.bf.scatter = 1;
898
- pd_uinfo->using_sd = 1;
899888 pd_uinfo->first_sd = fst_sd;
900889 sd = crypto4xx_get_sdp(dev, &sd_dma, sd_idx);
901890 pd->dest = sd_dma;
....@@ -959,15 +948,10 @@
959948
960949 if (alg->base.cra_flags & CRYPTO_ALG_NEED_FALLBACK) {
961950 ctx->sw_cipher.cipher =
962
- crypto_alloc_skcipher(alg->base.cra_name, 0,
963
- CRYPTO_ALG_NEED_FALLBACK |
964
- CRYPTO_ALG_ASYNC);
951
+ crypto_alloc_sync_skcipher(alg->base.cra_name, 0,
952
+ CRYPTO_ALG_NEED_FALLBACK);
965953 if (IS_ERR(ctx->sw_cipher.cipher))
966954 return PTR_ERR(ctx->sw_cipher.cipher);
967
-
968
- crypto_skcipher_set_reqsize(sk,
969
- sizeof(struct skcipher_request) + 32 +
970
- crypto_skcipher_reqsize(ctx->sw_cipher.cipher));
971955 }
972956
973957 amcc_alg = container_of(alg, struct crypto4xx_alg, alg.u.cipher);
....@@ -986,7 +970,7 @@
986970
987971 crypto4xx_common_exit(ctx);
988972 if (ctx->sw_cipher.cipher)
989
- crypto_free_skcipher(ctx->sw_cipher.cipher);
973
+ crypto_free_sync_skcipher(ctx->sw_cipher.cipher);
990974 }
991975
992976 static int crypto4xx_aead_init(struct crypto_aead *tfm)
....@@ -1042,6 +1026,10 @@
10421026 rc = crypto_register_ahash(&alg->alg.u.hash);
10431027 break;
10441028
1029
+ case CRYPTO_ALG_TYPE_RNG:
1030
+ rc = crypto_register_rng(&alg->alg.u.rng);
1031
+ break;
1032
+
10451033 default:
10461034 rc = crypto_register_skcipher(&alg->alg.u.cipher);
10471035 break;
....@@ -1069,6 +1057,10 @@
10691057
10701058 case CRYPTO_ALG_TYPE_AEAD:
10711059 crypto_unregister_aead(&alg->alg.u.aead);
1060
+ break;
1061
+
1062
+ case CRYPTO_ALG_TYPE_RNG:
1063
+ crypto_unregister_rng(&alg->alg.u.rng);
10721064 break;
10731065
10741066 default:
....@@ -1129,6 +1121,69 @@
11291121 PPC4XX_TMO_ERR_INT);
11301122 }
11311123
1124
+static int ppc4xx_prng_data_read(struct crypto4xx_device *dev,
1125
+ u8 *data, unsigned int max)
1126
+{
1127
+ unsigned int i, curr = 0;
1128
+ u32 val[2];
1129
+
1130
+ do {
1131
+ /* trigger PRN generation */
1132
+ writel(PPC4XX_PRNG_CTRL_AUTO_EN,
1133
+ dev->ce_base + CRYPTO4XX_PRNG_CTRL);
1134
+
1135
+ for (i = 0; i < 1024; i++) {
1136
+ /* usually 19 iterations are enough */
1137
+ if ((readl(dev->ce_base + CRYPTO4XX_PRNG_STAT) &
1138
+ CRYPTO4XX_PRNG_STAT_BUSY))
1139
+ continue;
1140
+
1141
+ val[0] = readl_be(dev->ce_base + CRYPTO4XX_PRNG_RES_0);
1142
+ val[1] = readl_be(dev->ce_base + CRYPTO4XX_PRNG_RES_1);
1143
+ break;
1144
+ }
1145
+ if (i == 1024)
1146
+ return -ETIMEDOUT;
1147
+
1148
+ if ((max - curr) >= 8) {
1149
+ memcpy(data, &val, 8);
1150
+ data += 8;
1151
+ curr += 8;
1152
+ } else {
1153
+ /* copy only remaining bytes */
1154
+ memcpy(data, &val, max - curr);
1155
+ break;
1156
+ }
1157
+ } while (curr < max);
1158
+
1159
+ return curr;
1160
+}
1161
+
1162
+static int crypto4xx_prng_generate(struct crypto_rng *tfm,
1163
+ const u8 *src, unsigned int slen,
1164
+ u8 *dstn, unsigned int dlen)
1165
+{
1166
+ struct rng_alg *alg = crypto_rng_alg(tfm);
1167
+ struct crypto4xx_alg *amcc_alg;
1168
+ struct crypto4xx_device *dev;
1169
+ int ret;
1170
+
1171
+ amcc_alg = container_of(alg, struct crypto4xx_alg, alg.u.rng);
1172
+ dev = amcc_alg->dev;
1173
+
1174
+ mutex_lock(&dev->core_dev->rng_lock);
1175
+ ret = ppc4xx_prng_data_read(dev, dstn, dlen);
1176
+ mutex_unlock(&dev->core_dev->rng_lock);
1177
+ return ret;
1178
+}
1179
+
1180
+
1181
+static int crypto4xx_prng_seed(struct crypto_rng *tfm, const u8 *seed,
1182
+ unsigned int slen)
1183
+{
1184
+ return 0;
1185
+}
1186
+
11321187 /**
11331188 * Supported Crypto Algorithms
11341189 */
....@@ -1161,7 +1216,7 @@
11611216 .cra_priority = CRYPTO4XX_CRYPTO_PRIORITY,
11621217 .cra_flags = CRYPTO_ALG_ASYNC |
11631218 CRYPTO_ALG_KERN_DRIVER_ONLY,
1164
- .cra_blocksize = AES_BLOCK_SIZE,
1219
+ .cra_blocksize = 1,
11651220 .cra_ctxsize = sizeof(struct crypto4xx_ctx),
11661221 .cra_module = THIS_MODULE,
11671222 },
....@@ -1222,7 +1277,7 @@
12221277 .cra_priority = CRYPTO4XX_CRYPTO_PRIORITY,
12231278 .cra_flags = CRYPTO_ALG_ASYNC |
12241279 CRYPTO_ALG_KERN_DRIVER_ONLY,
1225
- .cra_blocksize = 1,
1280
+ .cra_blocksize = AES_BLOCK_SIZE,
12261281 .cra_ctxsize = sizeof(struct crypto4xx_ctx),
12271282 .cra_module = THIS_MODULE,
12281283 },
....@@ -1298,6 +1353,18 @@
12981353 .cra_module = THIS_MODULE,
12991354 },
13001355 } },
1356
+ { .type = CRYPTO_ALG_TYPE_RNG, .u.rng = {
1357
+ .base = {
1358
+ .cra_name = "stdrng",
1359
+ .cra_driver_name = "crypto4xx_rng",
1360
+ .cra_priority = 300,
1361
+ .cra_ctxsize = 0,
1362
+ .cra_module = THIS_MODULE,
1363
+ },
1364
+ .generate = crypto4xx_prng_generate,
1365
+ .seed = crypto4xx_prng_seed,
1366
+ .seedsize = 0,
1367
+ } },
13011368 };
13021369
13031370 /**
....@@ -1367,18 +1434,18 @@
13671434 core_dev->dev->core_dev = core_dev;
13681435 core_dev->dev->is_revb = is_revb;
13691436 core_dev->device = dev;
1437
+ mutex_init(&core_dev->rng_lock);
13701438 spin_lock_init(&core_dev->lock);
13711439 INIT_LIST_HEAD(&core_dev->dev->alg_list);
13721440 ratelimit_default_init(&core_dev->dev->aead_ratelimit);
1441
+ rc = crypto4xx_build_sdr(core_dev->dev);
1442
+ if (rc)
1443
+ goto err_build_sdr;
13731444 rc = crypto4xx_build_pdr(core_dev->dev);
13741445 if (rc)
1375
- goto err_build_pdr;
1446
+ goto err_build_sdr;
13761447
13771448 rc = crypto4xx_build_gdr(core_dev->dev);
1378
- if (rc)
1379
- goto err_build_pdr;
1380
-
1381
- rc = crypto4xx_build_sdr(core_dev->dev);
13821449 if (rc)
13831450 goto err_build_sdr;
13841451
....@@ -1424,7 +1491,6 @@
14241491 err_build_sdr:
14251492 crypto4xx_destroy_sdr(core_dev->dev);
14261493 crypto4xx_destroy_gdr(core_dev->dev);
1427
-err_build_pdr:
14281494 crypto4xx_destroy_pdr(core_dev->dev);
14291495 kfree(core_dev->dev);
14301496 err_alloc_dev:
....@@ -1446,6 +1512,7 @@
14461512 tasklet_kill(&core_dev->tasklet);
14471513 /* Un-register with Linux CryptoAPI */
14481514 crypto4xx_unregister_alg(core_dev->dev);
1515
+ mutex_destroy(&core_dev->rng_lock);
14491516 /* Free all allocated memory */
14501517 crypto4xx_stop_all(core_dev);
14511518