forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-06 08f87f769b595151be1afeff53e144f543faa614
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++) {
....@@ -533,7 +526,7 @@
533526
534527 req = skcipher_request_cast(pd_uinfo->async_req);
535528
536
- if (pd_uinfo->using_sd) {
529
+ if (pd_uinfo->sa_va->sa_command_0.bf.scatter) {
537530 crypto4xx_copy_pkt_to_dst(dev, pd, pd_uinfo,
538531 req->cryptlen, req->dst);
539532 } else {
....@@ -587,12 +580,12 @@
587580 u32 icv[AES_BLOCK_SIZE];
588581 int err = 0;
589582
590
- if (pd_uinfo->using_sd) {
583
+ if (pd_uinfo->sa_va->sa_command_0.bf.scatter) {
591584 crypto4xx_copy_pkt_to_dst(dev, pd, pd_uinfo,
592585 pd->pd_ctl_len.bf.pkt_len,
593586 dst);
594587 } else {
595
- __dma_sync_page(sg_page(dst), dst->offset, dst->length,
588
+ dma_unmap_page(dev->core_dev->device, pd->dest, dst->length,
596589 DMA_FROM_DEVICE);
597590 }
598591
....@@ -881,7 +874,6 @@
881874 * we know application give us dst a whole piece of memory
882875 * no need to use scatter ring.
883876 */
884
- pd_uinfo->using_sd = 0;
885877 pd_uinfo->first_sd = 0xffffffff;
886878 sa->sa_command_0.bf.scatter = 0;
887879 pd->dest = (u32)dma_map_page(dev->core_dev->device,
....@@ -895,7 +887,6 @@
895887 u32 sd_idx = fst_sd;
896888 nbytes = datalen;
897889 sa->sa_command_0.bf.scatter = 1;
898
- pd_uinfo->using_sd = 1;
899890 pd_uinfo->first_sd = fst_sd;
900891 sd = crypto4xx_get_sdp(dev, &sd_dma, sd_idx);
901892 pd->dest = sd_dma;
....@@ -959,15 +950,10 @@
959950
960951 if (alg->base.cra_flags & CRYPTO_ALG_NEED_FALLBACK) {
961952 ctx->sw_cipher.cipher =
962
- crypto_alloc_skcipher(alg->base.cra_name, 0,
963
- CRYPTO_ALG_NEED_FALLBACK |
964
- CRYPTO_ALG_ASYNC);
953
+ crypto_alloc_sync_skcipher(alg->base.cra_name, 0,
954
+ CRYPTO_ALG_NEED_FALLBACK);
965955 if (IS_ERR(ctx->sw_cipher.cipher))
966956 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));
971957 }
972958
973959 amcc_alg = container_of(alg, struct crypto4xx_alg, alg.u.cipher);
....@@ -986,7 +972,7 @@
986972
987973 crypto4xx_common_exit(ctx);
988974 if (ctx->sw_cipher.cipher)
989
- crypto_free_skcipher(ctx->sw_cipher.cipher);
975
+ crypto_free_sync_skcipher(ctx->sw_cipher.cipher);
990976 }
991977
992978 static int crypto4xx_aead_init(struct crypto_aead *tfm)
....@@ -1042,6 +1028,10 @@
10421028 rc = crypto_register_ahash(&alg->alg.u.hash);
10431029 break;
10441030
1031
+ case CRYPTO_ALG_TYPE_RNG:
1032
+ rc = crypto_register_rng(&alg->alg.u.rng);
1033
+ break;
1034
+
10451035 default:
10461036 rc = crypto_register_skcipher(&alg->alg.u.cipher);
10471037 break;
....@@ -1069,6 +1059,10 @@
10691059
10701060 case CRYPTO_ALG_TYPE_AEAD:
10711061 crypto_unregister_aead(&alg->alg.u.aead);
1062
+ break;
1063
+
1064
+ case CRYPTO_ALG_TYPE_RNG:
1065
+ crypto_unregister_rng(&alg->alg.u.rng);
10721066 break;
10731067
10741068 default:
....@@ -1129,6 +1123,69 @@
11291123 PPC4XX_TMO_ERR_INT);
11301124 }
11311125
1126
+static int ppc4xx_prng_data_read(struct crypto4xx_device *dev,
1127
+ u8 *data, unsigned int max)
1128
+{
1129
+ unsigned int i, curr = 0;
1130
+ u32 val[2];
1131
+
1132
+ do {
1133
+ /* trigger PRN generation */
1134
+ writel(PPC4XX_PRNG_CTRL_AUTO_EN,
1135
+ dev->ce_base + CRYPTO4XX_PRNG_CTRL);
1136
+
1137
+ for (i = 0; i < 1024; i++) {
1138
+ /* usually 19 iterations are enough */
1139
+ if ((readl(dev->ce_base + CRYPTO4XX_PRNG_STAT) &
1140
+ CRYPTO4XX_PRNG_STAT_BUSY))
1141
+ continue;
1142
+
1143
+ val[0] = readl_be(dev->ce_base + CRYPTO4XX_PRNG_RES_0);
1144
+ val[1] = readl_be(dev->ce_base + CRYPTO4XX_PRNG_RES_1);
1145
+ break;
1146
+ }
1147
+ if (i == 1024)
1148
+ return -ETIMEDOUT;
1149
+
1150
+ if ((max - curr) >= 8) {
1151
+ memcpy(data, &val, 8);
1152
+ data += 8;
1153
+ curr += 8;
1154
+ } else {
1155
+ /* copy only remaining bytes */
1156
+ memcpy(data, &val, max - curr);
1157
+ break;
1158
+ }
1159
+ } while (curr < max);
1160
+
1161
+ return curr;
1162
+}
1163
+
1164
+static int crypto4xx_prng_generate(struct crypto_rng *tfm,
1165
+ const u8 *src, unsigned int slen,
1166
+ u8 *dstn, unsigned int dlen)
1167
+{
1168
+ struct rng_alg *alg = crypto_rng_alg(tfm);
1169
+ struct crypto4xx_alg *amcc_alg;
1170
+ struct crypto4xx_device *dev;
1171
+ int ret;
1172
+
1173
+ amcc_alg = container_of(alg, struct crypto4xx_alg, alg.u.rng);
1174
+ dev = amcc_alg->dev;
1175
+
1176
+ mutex_lock(&dev->core_dev->rng_lock);
1177
+ ret = ppc4xx_prng_data_read(dev, dstn, dlen);
1178
+ mutex_unlock(&dev->core_dev->rng_lock);
1179
+ return ret;
1180
+}
1181
+
1182
+
1183
+static int crypto4xx_prng_seed(struct crypto_rng *tfm, const u8 *seed,
1184
+ unsigned int slen)
1185
+{
1186
+ return 0;
1187
+}
1188
+
11321189 /**
11331190 * Supported Crypto Algorithms
11341191 */
....@@ -1161,7 +1218,7 @@
11611218 .cra_priority = CRYPTO4XX_CRYPTO_PRIORITY,
11621219 .cra_flags = CRYPTO_ALG_ASYNC |
11631220 CRYPTO_ALG_KERN_DRIVER_ONLY,
1164
- .cra_blocksize = AES_BLOCK_SIZE,
1221
+ .cra_blocksize = 1,
11651222 .cra_ctxsize = sizeof(struct crypto4xx_ctx),
11661223 .cra_module = THIS_MODULE,
11671224 },
....@@ -1222,7 +1279,7 @@
12221279 .cra_priority = CRYPTO4XX_CRYPTO_PRIORITY,
12231280 .cra_flags = CRYPTO_ALG_ASYNC |
12241281 CRYPTO_ALG_KERN_DRIVER_ONLY,
1225
- .cra_blocksize = 1,
1282
+ .cra_blocksize = AES_BLOCK_SIZE,
12261283 .cra_ctxsize = sizeof(struct crypto4xx_ctx),
12271284 .cra_module = THIS_MODULE,
12281285 },
....@@ -1298,6 +1355,18 @@
12981355 .cra_module = THIS_MODULE,
12991356 },
13001357 } },
1358
+ { .type = CRYPTO_ALG_TYPE_RNG, .u.rng = {
1359
+ .base = {
1360
+ .cra_name = "stdrng",
1361
+ .cra_driver_name = "crypto4xx_rng",
1362
+ .cra_priority = 300,
1363
+ .cra_ctxsize = 0,
1364
+ .cra_module = THIS_MODULE,
1365
+ },
1366
+ .generate = crypto4xx_prng_generate,
1367
+ .seed = crypto4xx_prng_seed,
1368
+ .seedsize = 0,
1369
+ } },
13011370 };
13021371
13031372 /**
....@@ -1367,18 +1436,18 @@
13671436 core_dev->dev->core_dev = core_dev;
13681437 core_dev->dev->is_revb = is_revb;
13691438 core_dev->device = dev;
1439
+ mutex_init(&core_dev->rng_lock);
13701440 spin_lock_init(&core_dev->lock);
13711441 INIT_LIST_HEAD(&core_dev->dev->alg_list);
13721442 ratelimit_default_init(&core_dev->dev->aead_ratelimit);
1443
+ rc = crypto4xx_build_sdr(core_dev->dev);
1444
+ if (rc)
1445
+ goto err_build_sdr;
13731446 rc = crypto4xx_build_pdr(core_dev->dev);
13741447 if (rc)
1375
- goto err_build_pdr;
1448
+ goto err_build_sdr;
13761449
13771450 rc = crypto4xx_build_gdr(core_dev->dev);
1378
- if (rc)
1379
- goto err_build_pdr;
1380
-
1381
- rc = crypto4xx_build_sdr(core_dev->dev);
13821451 if (rc)
13831452 goto err_build_sdr;
13841453
....@@ -1424,7 +1493,6 @@
14241493 err_build_sdr:
14251494 crypto4xx_destroy_sdr(core_dev->dev);
14261495 crypto4xx_destroy_gdr(core_dev->dev);
1427
-err_build_pdr:
14281496 crypto4xx_destroy_pdr(core_dev->dev);
14291497 kfree(core_dev->dev);
14301498 err_alloc_dev:
....@@ -1446,6 +1514,7 @@
14461514 tasklet_kill(&core_dev->tasklet);
14471515 /* Un-register with Linux CryptoAPI */
14481516 crypto4xx_unregister_alg(core_dev->dev);
1517
+ mutex_destroy(&core_dev->rng_lock);
14491518 /* Free all allocated memory */
14501519 crypto4xx_stop_all(core_dev);
14511520