Skip to content

Commit 2f77690

Browse files
chunkeeyherbertx
authored andcommitted
crypto: crypto4xx - simplify sa and state context acquisition
Thanks to the big overhaul of crypto4xx_build_pd(), the request-local sa_in, sa_out and state_record allocation can be simplified. There's no need to setup any dma coherent memory anymore and much of the support code can be removed. Signed-off-by: Christian Lamparter <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 4b5b799 commit 2f77690

File tree

3 files changed

+15
-68
lines changed

3 files changed

+15
-68
lines changed

drivers/crypto/amcc/crypto4xx_alg.c

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,13 @@ static int crypto4xx_setkey_aes(struct crypto_ablkcipher *cipher,
122122
}
123123

124124
/* Create SA */
125-
if (ctx->sa_in_dma_addr || ctx->sa_out_dma_addr)
125+
if (ctx->sa_in || ctx->sa_out)
126126
crypto4xx_free_sa(ctx);
127127

128128
rc = crypto4xx_alloc_sa(ctx, SA_AES128_LEN + (keylen-16) / 4);
129129
if (rc)
130130
return rc;
131131

132-
if (ctx->state_record_dma_addr == 0) {
133-
rc = crypto4xx_alloc_state_record(ctx);
134-
if (rc) {
135-
crypto4xx_free_sa(ctx);
136-
return rc;
137-
}
138-
}
139132
/* Setup SA */
140133
sa = ctx->sa_in;
141134

@@ -203,8 +196,8 @@ int crypto4xx_setkey_rfc3686(struct crypto_ablkcipher *cipher,
203196
if (rc)
204197
return rc;
205198

206-
crypto4xx_memcpy_to_le32(ctx->state_record->save_iv,
207-
key + keylen - CTR_RFC3686_NONCE_SIZE, CTR_RFC3686_NONCE_SIZE);
199+
ctx->iv_nonce = cpu_to_le32p((u32 *)&key[keylen -
200+
CTR_RFC3686_NONCE_SIZE]);
208201

209202
return 0;
210203
}
@@ -213,7 +206,7 @@ int crypto4xx_rfc3686_encrypt(struct ablkcipher_request *req)
213206
{
214207
struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
215208
__le32 iv[AES_IV_SIZE / 4] = {
216-
ctx->state_record->save_iv[0],
209+
ctx->iv_nonce,
217210
cpu_to_le32p((u32 *) req->info),
218211
cpu_to_le32p((u32 *) (req->info + 4)),
219212
cpu_to_le32(1) };
@@ -227,7 +220,7 @@ int crypto4xx_rfc3686_decrypt(struct ablkcipher_request *req)
227220
{
228221
struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
229222
__le32 iv[AES_IV_SIZE / 4] = {
230-
ctx->state_record->save_iv[0],
223+
ctx->iv_nonce,
231224
cpu_to_le32p((u32 *) req->info),
232225
cpu_to_le32p((u32 *) (req->info + 4)),
233226
cpu_to_le32(1) };
@@ -254,21 +247,13 @@ static int crypto4xx_hash_alg_init(struct crypto_tfm *tfm,
254247
ctx->dev = my_alg->dev;
255248

256249
/* Create SA */
257-
if (ctx->sa_in_dma_addr || ctx->sa_out_dma_addr)
250+
if (ctx->sa_in || ctx->sa_out)
258251
crypto4xx_free_sa(ctx);
259252

260253
rc = crypto4xx_alloc_sa(ctx, sa_len);
261254
if (rc)
262255
return rc;
263256

264-
if (ctx->state_record_dma_addr == 0) {
265-
crypto4xx_alloc_state_record(ctx);
266-
if (!ctx->state_record_dma_addr) {
267-
crypto4xx_free_sa(ctx);
268-
return -ENOMEM;
269-
}
270-
}
271-
272257
crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
273258
sizeof(struct crypto4xx_ctx));
274259
sa = (struct dynamic_sa_hash160 *)ctx->sa_in;

drivers/crypto/amcc/crypto4xx_core.c

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -130,62 +130,31 @@ static void crypto4xx_hw_init(struct crypto4xx_device *dev)
130130

131131
int crypto4xx_alloc_sa(struct crypto4xx_ctx *ctx, u32 size)
132132
{
133-
ctx->sa_in = dma_alloc_coherent(ctx->dev->core_dev->device, size * 4,
134-
&ctx->sa_in_dma_addr, GFP_ATOMIC);
133+
ctx->sa_in = kzalloc(size * 4, GFP_ATOMIC);
135134
if (ctx->sa_in == NULL)
136135
return -ENOMEM;
137136

138-
ctx->sa_out = dma_alloc_coherent(ctx->dev->core_dev->device, size * 4,
139-
&ctx->sa_out_dma_addr, GFP_ATOMIC);
137+
ctx->sa_out = kzalloc(size * 4, GFP_ATOMIC);
140138
if (ctx->sa_out == NULL) {
141-
dma_free_coherent(ctx->dev->core_dev->device, size * 4,
142-
ctx->sa_in, ctx->sa_in_dma_addr);
139+
kfree(ctx->sa_in);
140+
ctx->sa_in = NULL;
143141
return -ENOMEM;
144142
}
145143

146-
memset(ctx->sa_in, 0, size * 4);
147-
memset(ctx->sa_out, 0, size * 4);
148144
ctx->sa_len = size;
149145

150146
return 0;
151147
}
152148

153149
void crypto4xx_free_sa(struct crypto4xx_ctx *ctx)
154150
{
155-
if (ctx->sa_in != NULL)
156-
dma_free_coherent(ctx->dev->core_dev->device, ctx->sa_len * 4,
157-
ctx->sa_in, ctx->sa_in_dma_addr);
158-
if (ctx->sa_out != NULL)
159-
dma_free_coherent(ctx->dev->core_dev->device, ctx->sa_len * 4,
160-
ctx->sa_out, ctx->sa_out_dma_addr);
161-
162-
ctx->sa_in_dma_addr = 0;
163-
ctx->sa_out_dma_addr = 0;
151+
kfree(ctx->sa_in);
152+
ctx->sa_in = NULL;
153+
kfree(ctx->sa_out);
154+
ctx->sa_out = NULL;
164155
ctx->sa_len = 0;
165156
}
166157

167-
u32 crypto4xx_alloc_state_record(struct crypto4xx_ctx *ctx)
168-
{
169-
ctx->state_record = dma_alloc_coherent(ctx->dev->core_dev->device,
170-
sizeof(struct sa_state_record),
171-
&ctx->state_record_dma_addr, GFP_ATOMIC);
172-
if (!ctx->state_record_dma_addr)
173-
return -ENOMEM;
174-
memset(ctx->state_record, 0, sizeof(struct sa_state_record));
175-
176-
return 0;
177-
}
178-
179-
static void crypto4xx_free_state_record(struct crypto4xx_ctx *ctx)
180-
{
181-
if (ctx->state_record != NULL)
182-
dma_free_coherent(ctx->dev->core_dev->device,
183-
sizeof(struct sa_state_record),
184-
ctx->state_record,
185-
ctx->state_record_dma_addr);
186-
ctx->state_record_dma_addr = 0;
187-
}
188-
189158
/**
190159
* alloc memory for the gather ring
191160
* no need to alloc buf for the ring
@@ -883,8 +852,6 @@ static int crypto4xx_alg_init(struct crypto_tfm *tfm)
883852
ctx->dev = amcc_alg->dev;
884853
ctx->sa_in = NULL;
885854
ctx->sa_out = NULL;
886-
ctx->sa_in_dma_addr = 0;
887-
ctx->sa_out_dma_addr = 0;
888855
ctx->sa_len = 0;
889856

890857
switch (alg->cra_flags & CRYPTO_ALG_TYPE_MASK) {
@@ -905,7 +872,6 @@ static void crypto4xx_alg_exit(struct crypto_tfm *tfm)
905872
struct crypto4xx_ctx *ctx = crypto_tfm_ctx(tfm);
906873

907874
crypto4xx_free_sa(ctx);
908-
crypto4xx_free_state_record(ctx);
909875
}
910876

911877
int crypto4xx_register_alg(struct crypto4xx_device *sec_dev,

drivers/crypto/amcc/crypto4xx_core.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,8 @@ struct crypto4xx_core_device {
122122
struct crypto4xx_ctx {
123123
struct crypto4xx_device *dev;
124124
struct dynamic_sa_ctl *sa_in;
125-
dma_addr_t sa_in_dma_addr;
126125
struct dynamic_sa_ctl *sa_out;
127-
dma_addr_t sa_out_dma_addr;
128-
struct sa_state_record *state_record;
129-
dma_addr_t state_record_dma_addr;
126+
__le32 iv_nonce;
130127
u32 sa_len;
131128
};
132129

@@ -159,7 +156,6 @@ static inline struct crypto4xx_alg *crypto_alg_to_crypto4xx_alg(
159156
int crypto4xx_alloc_sa(struct crypto4xx_ctx *ctx, u32 size);
160157
void crypto4xx_free_sa(struct crypto4xx_ctx *ctx);
161158
void crypto4xx_free_ctx(struct crypto4xx_ctx *ctx);
162-
u32 crypto4xx_alloc_state_record(struct crypto4xx_ctx *ctx);
163159
int crypto4xx_build_pd(struct crypto_async_request *req,
164160
struct crypto4xx_ctx *ctx,
165161
struct scatterlist *src,

0 commit comments

Comments
 (0)