Skip to content

Commit 49c2c08

Browse files
toromanoSTMherbertx
authored andcommitted
crypto: stm32/crc32 - fix ext4 chksum BUG_ON()
Allow use of crc_update without prior call to crc_init. And change (and fix) driver to use CRC device even on unaligned buffers. Fixes: b51dbe9 ("crypto: stm32 - Support for STM32 CRC32 crypto module") Signed-off-by: Nicolas Toromanoff <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 2c959a3 commit 49c2c08

File tree

1 file changed

+48
-50
lines changed

1 file changed

+48
-50
lines changed

drivers/crypto/stm32/stm32-crc32.c

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828

2929
/* Registers values */
3030
#define CRC_CR_RESET BIT(0)
31-
#define CRC_CR_REVERSE (BIT(7) | BIT(6) | BIT(5))
3231
#define CRC_INIT_DEFAULT 0xFFFFFFFF
32+
#define CRC_CR_REV_IN_WORD (BIT(6) | BIT(5))
33+
#define CRC_CR_REV_IN_BYTE BIT(5)
34+
#define CRC_CR_REV_OUT BIT(7)
3335

3436
#define CRC_AUTOSUSPEND_DELAY 50
3537

@@ -38,8 +40,6 @@ struct stm32_crc {
3840
struct device *dev;
3941
void __iomem *regs;
4042
struct clk *clk;
41-
u8 pending_data[sizeof(u32)];
42-
size_t nb_pending_bytes;
4343
};
4444

4545
struct stm32_crc_list {
@@ -59,7 +59,6 @@ struct stm32_crc_ctx {
5959

6060
struct stm32_crc_desc_ctx {
6161
u32 partial; /* crc32c: partial in first 4 bytes of that struct */
62-
struct stm32_crc *crc;
6362
};
6463

6564
static int stm32_crc32_cra_init(struct crypto_tfm *tfm)
@@ -99,25 +98,22 @@ static int stm32_crc_init(struct shash_desc *desc)
9998
struct stm32_crc *crc;
10099

101100
spin_lock_bh(&crc_list.lock);
102-
list_for_each_entry(crc, &crc_list.dev_list, list) {
103-
ctx->crc = crc;
104-
break;
105-
}
101+
crc = list_first_entry(&crc_list.dev_list, struct stm32_crc, list);
106102
spin_unlock_bh(&crc_list.lock);
107103

108-
pm_runtime_get_sync(ctx->crc->dev);
104+
pm_runtime_get_sync(crc->dev);
109105

110106
/* Reset, set key, poly and configure in bit reverse mode */
111-
writel_relaxed(bitrev32(mctx->key), ctx->crc->regs + CRC_INIT);
112-
writel_relaxed(bitrev32(mctx->poly), ctx->crc->regs + CRC_POL);
113-
writel_relaxed(CRC_CR_RESET | CRC_CR_REVERSE, ctx->crc->regs + CRC_CR);
107+
writel_relaxed(bitrev32(mctx->key), crc->regs + CRC_INIT);
108+
writel_relaxed(bitrev32(mctx->poly), crc->regs + CRC_POL);
109+
writel_relaxed(CRC_CR_RESET | CRC_CR_REV_IN_WORD | CRC_CR_REV_OUT,
110+
crc->regs + CRC_CR);
114111

115112
/* Store partial result */
116-
ctx->partial = readl_relaxed(ctx->crc->regs + CRC_DR);
117-
ctx->crc->nb_pending_bytes = 0;
113+
ctx->partial = readl_relaxed(crc->regs + CRC_DR);
118114

119-
pm_runtime_mark_last_busy(ctx->crc->dev);
120-
pm_runtime_put_autosuspend(ctx->crc->dev);
115+
pm_runtime_mark_last_busy(crc->dev);
116+
pm_runtime_put_autosuspend(crc->dev);
121117

122118
return 0;
123119
}
@@ -126,54 +122,56 @@ static int stm32_crc_update(struct shash_desc *desc, const u8 *d8,
126122
unsigned int length)
127123
{
128124
struct stm32_crc_desc_ctx *ctx = shash_desc_ctx(desc);
129-
struct stm32_crc *crc = ctx->crc;
130-
u32 *d32;
131-
unsigned int i;
125+
struct stm32_crc_ctx *mctx = crypto_shash_ctx(desc->tfm);
126+
struct stm32_crc *crc;
127+
128+
spin_lock_bh(&crc_list.lock);
129+
crc = list_first_entry(&crc_list.dev_list, struct stm32_crc, list);
130+
spin_unlock_bh(&crc_list.lock);
132131

133132
pm_runtime_get_sync(crc->dev);
134133

135-
if (unlikely(crc->nb_pending_bytes)) {
136-
while (crc->nb_pending_bytes != sizeof(u32) && length) {
137-
/* Fill in pending data */
138-
crc->pending_data[crc->nb_pending_bytes++] = *(d8++);
134+
/*
135+
* Restore previously calculated CRC for this context as init value
136+
* Restore polynomial configuration
137+
* Configure in register for word input data,
138+
* Configure out register in reversed bit mode data.
139+
*/
140+
writel_relaxed(bitrev32(ctx->partial), crc->regs + CRC_INIT);
141+
writel_relaxed(bitrev32(mctx->poly), crc->regs + CRC_POL);
142+
writel_relaxed(CRC_CR_RESET | CRC_CR_REV_IN_WORD | CRC_CR_REV_OUT,
143+
crc->regs + CRC_CR);
144+
145+
if (d8 != PTR_ALIGN(d8, sizeof(u32))) {
146+
/* Configure for byte data */
147+
writel_relaxed(CRC_CR_REV_IN_BYTE | CRC_CR_REV_OUT,
148+
crc->regs + CRC_CR);
149+
while (d8 != PTR_ALIGN(d8, sizeof(u32)) && length) {
150+
writeb_relaxed(*d8++, crc->regs + CRC_DR);
139151
length--;
140152
}
141-
142-
if (crc->nb_pending_bytes == sizeof(u32)) {
143-
/* Process completed pending data */
144-
writel_relaxed(*(u32 *)crc->pending_data,
145-
crc->regs + CRC_DR);
146-
crc->nb_pending_bytes = 0;
147-
}
153+
/* Configure for word data */
154+
writel_relaxed(CRC_CR_REV_IN_WORD | CRC_CR_REV_OUT,
155+
crc->regs + CRC_CR);
148156
}
149157

150-
d32 = (u32 *)d8;
151-
for (i = 0; i < length >> 2; i++)
152-
/* Process 32 bits data */
153-
writel_relaxed(*(d32++), crc->regs + CRC_DR);
158+
for (; length >= sizeof(u32); d8 += sizeof(u32), length -= sizeof(u32))
159+
writel_relaxed(*((u32 *)d8), crc->regs + CRC_DR);
160+
161+
if (length) {
162+
/* Configure for byte data */
163+
writel_relaxed(CRC_CR_REV_IN_BYTE | CRC_CR_REV_OUT,
164+
crc->regs + CRC_CR);
165+
while (length--)
166+
writeb_relaxed(*d8++, crc->regs + CRC_DR);
167+
}
154168

155169
/* Store partial result */
156170
ctx->partial = readl_relaxed(crc->regs + CRC_DR);
157171

158172
pm_runtime_mark_last_busy(crc->dev);
159173
pm_runtime_put_autosuspend(crc->dev);
160174

161-
/* Check for pending data (non 32 bits) */
162-
length &= 3;
163-
if (likely(!length))
164-
return 0;
165-
166-
if ((crc->nb_pending_bytes + length) >= sizeof(u32)) {
167-
/* Shall not happen */
168-
dev_err(crc->dev, "Pending data overflow\n");
169-
return -EINVAL;
170-
}
171-
172-
d8 = (const u8 *)d32;
173-
for (i = 0; i < length; i++)
174-
/* Store pending data */
175-
crc->pending_data[crc->nb_pending_bytes++] = *(d8++);
176-
177175
return 0;
178176
}
179177

0 commit comments

Comments
 (0)