Skip to content

Commit 93c7f4d

Browse files
montjoieherbertx
authored andcommitted
crypto: sun8i-ce - enable working on big endian
On big endian kernel, the sun8i-ce crypto driver does not works. This patch do the necessary modification to permit it to work on BE kernel (setting descriptor entries as __le32 and adding some cpu_to_le32) Fixes: 06f751b ("crypto: allwinner - Add sun8i-ce Crypto Engine") Signed-off-by: Corentin Labbe <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 660eda8 commit 93c7f4d

File tree

2 files changed

+40
-34
lines changed

2 files changed

+40
-34
lines changed

drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ static int sun8i_ce_cipher(struct skcipher_request *areq)
9090
struct ce_task *cet;
9191
struct scatterlist *sg;
9292
unsigned int todo, len, offset, ivsize;
93+
dma_addr_t addr_iv = 0, addr_key = 0;
9394
void *backup_iv = NULL;
95+
u32 common, sym;
9496
int flow, i;
9597
int nr_sgs = 0;
9698
int nr_sgd = 0;
@@ -115,38 +117,41 @@ static int sun8i_ce_cipher(struct skcipher_request *areq)
115117
cet = chan->tl;
116118
memset(cet, 0, sizeof(struct ce_task));
117119

118-
cet->t_id = flow;
119-
cet->t_common_ctl = ce->variant->alg_cipher[algt->ce_algo_id];
120-
cet->t_common_ctl |= rctx->op_dir | CE_COMM_INT;
121-
cet->t_dlen = areq->cryptlen / 4;
120+
cet->t_id = cpu_to_le32(flow);
121+
common = ce->variant->alg_cipher[algt->ce_algo_id];
122+
common |= rctx->op_dir | CE_COMM_INT;
123+
cet->t_common_ctl = cpu_to_le32(common);
122124
/* CTS and recent CE (H6) need length in bytes, in word otherwise */
123125
if (ce->variant->has_t_dlen_in_bytes)
124-
cet->t_dlen = areq->cryptlen;
126+
cet->t_dlen = cpu_to_le32(areq->cryptlen);
127+
else
128+
cet->t_dlen = cpu_to_le32(areq->cryptlen / 4);
125129

126-
cet->t_sym_ctl = ce->variant->op_mode[algt->ce_blockmode];
130+
sym = ce->variant->op_mode[algt->ce_blockmode];
127131
len = op->keylen;
128132
switch (len) {
129133
case 128 / 8:
130-
cet->t_sym_ctl |= CE_AES_128BITS;
134+
sym |= CE_AES_128BITS;
131135
break;
132136
case 192 / 8:
133-
cet->t_sym_ctl |= CE_AES_192BITS;
137+
sym |= CE_AES_192BITS;
134138
break;
135139
case 256 / 8:
136-
cet->t_sym_ctl |= CE_AES_256BITS;
140+
sym |= CE_AES_256BITS;
137141
break;
138142
}
139143

144+
cet->t_sym_ctl = cpu_to_le32(sym);
140145
cet->t_asym_ctl = 0;
141146

142147
chan->op_mode = ce->variant->op_mode[algt->ce_blockmode];
143148
chan->op_dir = rctx->op_dir;
144149
chan->method = ce->variant->alg_cipher[algt->ce_algo_id];
145150
chan->keylen = op->keylen;
146151

147-
cet->t_key = dma_map_single(ce->dev, op->key, op->keylen,
148-
DMA_TO_DEVICE);
149-
if (dma_mapping_error(ce->dev, cet->t_key)) {
152+
addr_key = dma_map_single(ce->dev, op->key, op->keylen, DMA_TO_DEVICE);
153+
cet->t_key = cpu_to_le32(addr_key);
154+
if (dma_mapping_error(ce->dev, addr_key)) {
150155
dev_err(ce->dev, "Cannot DMA MAP KEY\n");
151156
err = -EFAULT;
152157
goto theend;
@@ -171,9 +176,10 @@ static int sun8i_ce_cipher(struct skcipher_request *areq)
171176
ivsize, 0);
172177
}
173178
memcpy(chan->bounce_iv, areq->iv, ivsize);
174-
cet->t_iv = dma_map_single(ce->dev, chan->bounce_iv,
175-
chan->ivlen, DMA_TO_DEVICE);
176-
if (dma_mapping_error(ce->dev, cet->t_iv)) {
179+
addr_iv = dma_map_single(ce->dev, chan->bounce_iv, chan->ivlen,
180+
DMA_TO_DEVICE);
181+
cet->t_iv = cpu_to_le32(addr_iv);
182+
if (dma_mapping_error(ce->dev, addr_iv)) {
177183
dev_err(ce->dev, "Cannot DMA MAP IV\n");
178184
err = -ENOMEM;
179185
goto theend_iv;
@@ -208,9 +214,9 @@ static int sun8i_ce_cipher(struct skcipher_request *areq)
208214

209215
len = areq->cryptlen;
210216
for_each_sg(areq->src, sg, nr_sgs, i) {
211-
cet->t_src[i].addr = sg_dma_address(sg);
217+
cet->t_src[i].addr = cpu_to_le32(sg_dma_address(sg));
212218
todo = min(len, sg_dma_len(sg));
213-
cet->t_src[i].len = todo / 4;
219+
cet->t_src[i].len = cpu_to_le32(todo / 4);
214220
dev_dbg(ce->dev, "%s total=%u SG(%d %u off=%d) todo=%u\n", __func__,
215221
areq->cryptlen, i, cet->t_src[i].len, sg->offset, todo);
216222
len -= todo;
@@ -223,9 +229,9 @@ static int sun8i_ce_cipher(struct skcipher_request *areq)
223229

224230
len = areq->cryptlen;
225231
for_each_sg(areq->dst, sg, nr_sgd, i) {
226-
cet->t_dst[i].addr = sg_dma_address(sg);
232+
cet->t_dst[i].addr = cpu_to_le32(sg_dma_address(sg));
227233
todo = min(len, sg_dma_len(sg));
228-
cet->t_dst[i].len = todo / 4;
234+
cet->t_dst[i].len = cpu_to_le32(todo / 4);
229235
dev_dbg(ce->dev, "%s total=%u SG(%d %u off=%d) todo=%u\n", __func__,
230236
areq->cryptlen, i, cet->t_dst[i].len, sg->offset, todo);
231237
len -= todo;
@@ -250,8 +256,8 @@ static int sun8i_ce_cipher(struct skcipher_request *areq)
250256

251257
theend_iv:
252258
if (areq->iv && ivsize > 0) {
253-
if (cet->t_iv)
254-
dma_unmap_single(ce->dev, cet->t_iv, chan->ivlen,
259+
if (addr_iv)
260+
dma_unmap_single(ce->dev, addr_iv, chan->ivlen,
255261
DMA_TO_DEVICE);
256262
offset = areq->cryptlen - ivsize;
257263
if (rctx->op_dir & CE_DECRYPTION) {
@@ -265,7 +271,7 @@ static int sun8i_ce_cipher(struct skcipher_request *areq)
265271
}
266272

267273
theend_key:
268-
dma_unmap_single(ce->dev, cet->t_key, op->keylen, DMA_TO_DEVICE);
274+
dma_unmap_single(ce->dev, addr_key, op->keylen, DMA_TO_DEVICE);
269275

270276
theend:
271277
return err;

drivers/crypto/allwinner/sun8i-ce/sun8i-ce.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,27 +103,27 @@ struct ce_variant {
103103
};
104104

105105
struct sginfo {
106-
u32 addr;
107-
u32 len;
106+
__le32 addr;
107+
__le32 len;
108108
} __packed;
109109

110110
/*
111111
* struct ce_task - CE Task descriptor
112112
* The structure of this descriptor could be found in the datasheet
113113
*/
114114
struct ce_task {
115-
u32 t_id;
116-
u32 t_common_ctl;
117-
u32 t_sym_ctl;
118-
u32 t_asym_ctl;
119-
u32 t_key;
120-
u32 t_iv;
121-
u32 t_ctr;
122-
u32 t_dlen;
115+
__le32 t_id;
116+
__le32 t_common_ctl;
117+
__le32 t_sym_ctl;
118+
__le32 t_asym_ctl;
119+
__le32 t_key;
120+
__le32 t_iv;
121+
__le32 t_ctr;
122+
__le32 t_dlen;
123123
struct sginfo t_src[MAX_SG];
124124
struct sginfo t_dst[MAX_SG];
125-
u32 next;
126-
u32 reserved[3];
125+
__le32 next;
126+
__le32 reserved[3];
127127
} __packed __aligned(8);
128128

129129
/*

0 commit comments

Comments
 (0)