Blame view

drivers/crypto/bfin_crc.c 18.5 KB
b8840098b   Sonic Zhang   crypto: bfin_crc ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  /*
   * Cryptographic API.
   *
   * Support Blackfin CRC HW acceleration.
   *
   * Copyright 2012 Analog Devices Inc.
   *
   * Licensed under the GPL-2.
   */
  
  #include <linux/err.h>
  #include <linux/device.h>
  #include <linux/module.h>
  #include <linux/init.h>
  #include <linux/errno.h>
  #include <linux/interrupt.h>
  #include <linux/kernel.h>
  #include <linux/irq.h>
  #include <linux/io.h>
  #include <linux/platform_device.h>
  #include <linux/scatterlist.h>
  #include <linux/dma-mapping.h>
  #include <linux/delay.h>
b8840098b   Sonic Zhang   crypto: bfin_crc ...
24
25
26
27
28
29
  #include <linux/crypto.h>
  #include <linux/cryptohash.h>
  #include <crypto/scatterwalk.h>
  #include <crypto/algapi.h>
  #include <crypto/hash.h>
  #include <crypto/internal/hash.h>
1e0bdad02   Johannes Berg   crypto: bfin_crc ...
30
  #include <asm/unaligned.h>
b8840098b   Sonic Zhang   crypto: bfin_crc ...
31

b8840098b   Sonic Zhang   crypto: bfin_crc ...
32
33
  #include <asm/dma.h>
  #include <asm/portmux.h>
52e6e543f   Sonic Zhang   crypto: bfin_crc ...
34
35
36
  #include <asm/io.h>
  
  #include "bfin_crc.h"
b8840098b   Sonic Zhang   crypto: bfin_crc ...
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  
  #define CRC_CCRYPTO_QUEUE_LENGTH	5
  
  #define DRIVER_NAME "bfin-hmac-crc"
  #define CHKSUM_DIGEST_SIZE      4
  #define CHKSUM_BLOCK_SIZE       1
  
  #define CRC_MAX_DMA_DESC	100
  
  #define CRC_CRYPTO_STATE_UPDATE		1
  #define CRC_CRYPTO_STATE_FINALUPDATE	2
  #define CRC_CRYPTO_STATE_FINISH		3
  
  struct bfin_crypto_crc {
  	struct list_head	list;
  	struct device		*dev;
  	spinlock_t		lock;
  
  	int			irq;
  	int			dma_ch;
  	u32			poly;
52e6e543f   Sonic Zhang   crypto: bfin_crc ...
58
  	struct crc_register	*regs;
b8840098b   Sonic Zhang   crypto: bfin_crc ...
59
60
61
62
63
  
  	struct ahash_request	*req; /* current request in operation */
  	struct dma_desc_array	*sg_cpu; /* virt addr of sg dma descriptors */
  	dma_addr_t		sg_dma; /* phy addr of sg dma descriptors */
  	u8			*sg_mid_buf;
52d77eb17   Sonic Zhang   cryptoo: bfin_crc...
64
  	dma_addr_t		sg_mid_dma; /* phy addr of sg mid buffer */
b8840098b   Sonic Zhang   crypto: bfin_crc ...
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  
  	struct tasklet_struct	done_task;
  	struct crypto_queue	queue; /* waiting requests */
  
  	u8			busy:1; /* crc device in operation flag */
  };
  
  static struct bfin_crypto_crc_list {
  	struct list_head	dev_list;
  	spinlock_t		lock;
  } crc_list;
  
  struct bfin_crypto_crc_reqctx {
  	struct bfin_crypto_crc	*crc;
  
  	unsigned int		total;	/* total request bytes */
  	size_t			sg_buflen; /* bytes for this update */
  	unsigned int		sg_nents;
  	struct scatterlist	*sg; /* sg list head for this update*/
  	struct scatterlist	bufsl[2]; /* chained sg list */
  
  	size_t			bufnext_len;
  	size_t			buflast_len;
  	u8			bufnext[CHKSUM_DIGEST_SIZE]; /* extra bytes for next udpate */
  	u8			buflast[CHKSUM_DIGEST_SIZE]; /* extra bytes from last udpate */
  
  	u8			flag;
  };
  
  struct bfin_crypto_crc_ctx {
  	struct bfin_crypto_crc	*crc;
  	u32			key;
  };
b8840098b   Sonic Zhang   crypto: bfin_crc ...
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  /*
   * get element in scatter list by given index
   */
  static struct scatterlist *sg_get(struct scatterlist *sg_list, unsigned int nents,
  				unsigned int index)
  {
  	struct scatterlist *sg = NULL;
  	int i;
  
  	for_each_sg(sg_list, sg, nents, i)
  		if (i == index)
  			break;
  
  	return sg;
  }
  
  static int bfin_crypto_crc_init_hw(struct bfin_crypto_crc *crc, u32 key)
  {
52e6e543f   Sonic Zhang   crypto: bfin_crc ...
116
117
118
  	writel(0, &crc->regs->datacntrld);
  	writel(MODE_CALC_CRC << OPMODE_OFFSET, &crc->regs->control);
  	writel(key, &crc->regs->curresult);
b8840098b   Sonic Zhang   crypto: bfin_crc ...
119
120
  
  	/* setup CRC interrupts */
52e6e543f   Sonic Zhang   crypto: bfin_crc ...
121
122
  	writel(CMPERRI | DCNTEXPI, &crc->regs->status);
  	writel(CMPERRI | DCNTEXPI, &crc->regs->intrenset);
b8840098b   Sonic Zhang   crypto: bfin_crc ...
123
124
125
126
127
128
129
130
131
132
  
  	return 0;
  }
  
  static int bfin_crypto_crc_init(struct ahash_request *req)
  {
  	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  	struct bfin_crypto_crc_ctx *crc_ctx = crypto_ahash_ctx(tfm);
  	struct bfin_crypto_crc_reqctx *ctx = ahash_request_ctx(req);
  	struct bfin_crypto_crc *crc;
fb1dd7948   Syam Sidhardhan   crypto: bfin_crc ...
133
134
  	dev_dbg(ctx->crc->dev, "crc_init
  ");
b8840098b   Sonic Zhang   crypto: bfin_crc ...
135
136
137
138
139
140
  	spin_lock_bh(&crc_list.lock);
  	list_for_each_entry(crc, &crc_list.dev_list, list) {
  		crc_ctx->crc = crc;
  		break;
  	}
  	spin_unlock_bh(&crc_list.lock);
1f6d79d7e   LABBE Corentin   crypto: bfin_crc ...
141
  	if (sg_nents(req->src) > CRC_MAX_DMA_DESC) {
fb1dd7948   Syam Sidhardhan   crypto: bfin_crc ...
142
143
  		dev_dbg(ctx->crc->dev, "init: requested sg list is too big > %d
  ",
b8840098b   Sonic Zhang   crypto: bfin_crc ...
144
145
146
147
148
149
150
151
152
153
154
155
156
  			CRC_MAX_DMA_DESC);
  		return -EINVAL;
  	}
  
  	ctx->crc = crc;
  	ctx->bufnext_len = 0;
  	ctx->buflast_len = 0;
  	ctx->sg_buflen = 0;
  	ctx->total = 0;
  	ctx->flag = 0;
  
  	/* init crc results */
  	put_unaligned_le32(crc_ctx->key, req->result);
fb1dd7948   Syam Sidhardhan   crypto: bfin_crc ...
157
158
  	dev_dbg(ctx->crc->dev, "init: digest size: %d
  ",
b8840098b   Sonic Zhang   crypto: bfin_crc ...
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  		crypto_ahash_digestsize(tfm));
  
  	return bfin_crypto_crc_init_hw(crc, crc_ctx->key);
  }
  
  static void bfin_crypto_crc_config_dma(struct bfin_crypto_crc *crc)
  {
  	struct scatterlist *sg;
  	struct bfin_crypto_crc_reqctx *ctx = ahash_request_ctx(crc->req);
  	int i = 0, j = 0;
  	unsigned long dma_config;
  	unsigned int dma_count;
  	unsigned int dma_addr;
  	unsigned int mid_dma_count = 0;
  	int dma_mod;
  
  	dma_map_sg(crc->dev, ctx->sg, ctx->sg_nents, DMA_TO_DEVICE);
  
  	for_each_sg(ctx->sg, sg, ctx->sg_nents, j) {
b8840098b   Sonic Zhang   crypto: bfin_crc ...
178
179
180
181
182
183
184
185
186
187
188
189
  		dma_addr = sg_dma_address(sg);
  		/* deduce extra bytes in last sg */
  		if (sg_is_last(sg))
  			dma_count = sg_dma_len(sg) - ctx->bufnext_len;
  		else
  			dma_count = sg_dma_len(sg);
  
  		if (mid_dma_count) {
  			/* Append last middle dma buffer to 4 bytes with first
  			   bytes in current sg buffer. Move addr of current
  			   sg and deduce the length of current sg.
  			 */
52d77eb17   Sonic Zhang   cryptoo: bfin_crc...
190
191
  			memcpy(crc->sg_mid_buf +(i << 2) + mid_dma_count,
  				sg_virt(sg),
b8840098b   Sonic Zhang   crypto: bfin_crc ...
192
193
194
  				CHKSUM_DIGEST_SIZE - mid_dma_count);
  			dma_addr += CHKSUM_DIGEST_SIZE - mid_dma_count;
  			dma_count -= CHKSUM_DIGEST_SIZE - mid_dma_count;
52d77eb17   Sonic Zhang   cryptoo: bfin_crc...
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
  
  			dma_config = DMAFLOW_ARRAY | RESTART | NDSIZE_3 |
  				DMAEN | PSIZE_32 | WDSIZE_32;
  
  			/* setup new dma descriptor for next middle dma */
  			crc->sg_cpu[i].start_addr = crc->sg_mid_dma + (i << 2);
  			crc->sg_cpu[i].cfg = dma_config;
  			crc->sg_cpu[i].x_count = 1;
  			crc->sg_cpu[i].x_modify = CHKSUM_DIGEST_SIZE;
  			dev_dbg(crc->dev, "%d: crc_dma: start_addr:0x%lx, "
  				"cfg:0x%lx, x_count:0x%lx, x_modify:0x%lx
  ",
  				i, crc->sg_cpu[i].start_addr,
  				crc->sg_cpu[i].cfg, crc->sg_cpu[i].x_count,
  				crc->sg_cpu[i].x_modify);
  			i++;
b8840098b   Sonic Zhang   crypto: bfin_crc ...
211
  		}
52d77eb17   Sonic Zhang   cryptoo: bfin_crc...
212
213
  
  		dma_config = DMAFLOW_ARRAY | RESTART | NDSIZE_3 | DMAEN | PSIZE_32;
b8840098b   Sonic Zhang   crypto: bfin_crc ...
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
  		/* chop current sg dma len to multiple of 32 bits */
  		mid_dma_count = dma_count % 4;
  		dma_count &= ~0x3;
  
  		if (dma_addr % 4 == 0) {
  			dma_config |= WDSIZE_32;
  			dma_count >>= 2;
  			dma_mod = 4;
  		} else if (dma_addr % 2 == 0) {
  			dma_config |= WDSIZE_16;
  			dma_count >>= 1;
  			dma_mod = 2;
  		} else {
  			dma_config |= WDSIZE_8;
  			dma_mod = 1;
  		}
  
  		crc->sg_cpu[i].start_addr = dma_addr;
  		crc->sg_cpu[i].cfg = dma_config;
  		crc->sg_cpu[i].x_count = dma_count;
  		crc->sg_cpu[i].x_modify = dma_mod;
  		dev_dbg(crc->dev, "%d: crc_dma: start_addr:0x%lx, "
  			"cfg:0x%lx, x_count:0x%lx, x_modify:0x%lx
  ",
  			i, crc->sg_cpu[i].start_addr,
  			crc->sg_cpu[i].cfg, crc->sg_cpu[i].x_count,
  			crc->sg_cpu[i].x_modify);
  		i++;
  
  		if (mid_dma_count) {
  			/* copy extra bytes to next middle dma buffer */
b8840098b   Sonic Zhang   crypto: bfin_crc ...
245
  			memcpy(crc->sg_mid_buf + (i << 2),
52d77eb17   Sonic Zhang   cryptoo: bfin_crc...
246
  				(u8*)sg_virt(sg) + (dma_count << 2),
b8840098b   Sonic Zhang   crypto: bfin_crc ...
247
  				mid_dma_count);
b8840098b   Sonic Zhang   crypto: bfin_crc ...
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
  		}
  	}
  
  	dma_config = DMAFLOW_ARRAY | RESTART | NDSIZE_3 | DMAEN | PSIZE_32 | WDSIZE_32;
  	/* For final update req, append the buffer for next update as well*/
  	if (ctx->bufnext_len && (ctx->flag == CRC_CRYPTO_STATE_FINALUPDATE ||
  		ctx->flag == CRC_CRYPTO_STATE_FINISH)) {
  		crc->sg_cpu[i].start_addr = dma_map_single(crc->dev, ctx->bufnext,
  						CHKSUM_DIGEST_SIZE, DMA_TO_DEVICE);
  		crc->sg_cpu[i].cfg = dma_config;
  		crc->sg_cpu[i].x_count = 1;
  		crc->sg_cpu[i].x_modify = CHKSUM_DIGEST_SIZE;
  		dev_dbg(crc->dev, "%d: crc_dma: start_addr:0x%lx, "
  			"cfg:0x%lx, x_count:0x%lx, x_modify:0x%lx
  ",
  			i, crc->sg_cpu[i].start_addr,
  			crc->sg_cpu[i].cfg, crc->sg_cpu[i].x_count,
  			crc->sg_cpu[i].x_modify);
  		i++;
  	}
  
  	if (i == 0)
  		return;
b8840098b   Sonic Zhang   crypto: bfin_crc ...
271
272
273
274
275
276
  	/* Set the last descriptor to stop mode */
  	crc->sg_cpu[i - 1].cfg &= ~(DMAFLOW | NDSIZE);
  	crc->sg_cpu[i - 1].cfg |= DI_EN;
  	set_dma_curr_desc_addr(crc->dma_ch, (unsigned long *)crc->sg_dma);
  	set_dma_x_count(crc->dma_ch, 0);
  	set_dma_x_modify(crc->dma_ch, 0);
b8840098b   Sonic Zhang   crypto: bfin_crc ...
277
278
279
280
281
282
283
284
285
286
287
288
289
  	set_dma_config(crc->dma_ch, dma_config);
  }
  
  static int bfin_crypto_crc_handle_queue(struct bfin_crypto_crc *crc,
  				  struct ahash_request *req)
  {
  	struct crypto_async_request *async_req, *backlog;
  	struct bfin_crypto_crc_reqctx *ctx;
  	struct scatterlist *sg;
  	int ret = 0;
  	int nsg, i, j;
  	unsigned int nextlen;
  	unsigned long flags;
52e6e543f   Sonic Zhang   crypto: bfin_crc ...
290
  	u32 reg;
b8840098b   Sonic Zhang   crypto: bfin_crc ...
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
  
  	spin_lock_irqsave(&crc->lock, flags);
  	if (req)
  		ret = ahash_enqueue_request(&crc->queue, req);
  	if (crc->busy) {
  		spin_unlock_irqrestore(&crc->lock, flags);
  		return ret;
  	}
  	backlog = crypto_get_backlog(&crc->queue);
  	async_req = crypto_dequeue_request(&crc->queue);
  	if (async_req)
  		crc->busy = 1;
  	spin_unlock_irqrestore(&crc->lock, flags);
  
  	if (!async_req)
  		return ret;
  
  	if (backlog)
  		backlog->complete(backlog, -EINPROGRESS);
  
  	req = ahash_request_cast(async_req);
  	crc->req = req;
  	ctx = ahash_request_ctx(req);
  	ctx->sg = NULL;
  	ctx->sg_buflen = 0;
  	ctx->sg_nents = 0;
  
  	dev_dbg(crc->dev, "handling new req, flag=%u, nbytes: %d
  ",
  						ctx->flag, req->nbytes);
  
  	if (ctx->flag == CRC_CRYPTO_STATE_FINISH) {
  		if (ctx->bufnext_len == 0) {
  			crc->busy = 0;
  			return 0;
  		}
  
  		/* Pack last crc update buffer to 32bit */
  		memset(ctx->bufnext + ctx->bufnext_len, 0,
  				CHKSUM_DIGEST_SIZE - ctx->bufnext_len);
  	} else {
  		/* Pack small data which is less than 32bit to buffer for next update. */
  		if (ctx->bufnext_len + req->nbytes < CHKSUM_DIGEST_SIZE) {
  			memcpy(ctx->bufnext + ctx->bufnext_len,
  				sg_virt(req->src), req->nbytes);
  			ctx->bufnext_len += req->nbytes;
  			if (ctx->flag == CRC_CRYPTO_STATE_FINALUPDATE &&
  				ctx->bufnext_len) {
  				goto finish_update;
  			} else {
  				crc->busy = 0;
  				return 0;
  			}
  		}
  
  		if (ctx->bufnext_len) {
  			/* Chain in extra bytes of last update */
  			ctx->buflast_len = ctx->bufnext_len;
  			memcpy(ctx->buflast, ctx->bufnext, ctx->buflast_len);
  
  			nsg = ctx->sg_buflen ? 2 : 1;
  			sg_init_table(ctx->bufsl, nsg);
  			sg_set_buf(ctx->bufsl, ctx->buflast, ctx->buflast_len);
  			if (nsg > 1)
c56f6d127   Dan Williams   crypto: replace s...
355
  				sg_chain(ctx->bufsl, nsg, req->src);
b8840098b   Sonic Zhang   crypto: bfin_crc ...
356
357
358
359
360
  			ctx->sg = ctx->bufsl;
  		} else
  			ctx->sg = req->src;
  
  		/* Chop crc buffer size to multiple of 32 bit */
1f6d79d7e   LABBE Corentin   crypto: bfin_crc ...
361
362
  		nsg = sg_nents(ctx->sg);
  		ctx->sg_nents = nsg;
b8840098b   Sonic Zhang   crypto: bfin_crc ...
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
  		ctx->sg_buflen = ctx->buflast_len + req->nbytes;
  		ctx->bufnext_len = ctx->sg_buflen % 4;
  		ctx->sg_buflen &= ~0x3;
  
  		if (ctx->bufnext_len) {
  			/* copy extra bytes to buffer for next update */
  			memset(ctx->bufnext, 0, CHKSUM_DIGEST_SIZE);
  			nextlen = ctx->bufnext_len;
  			for (i = nsg - 1; i >= 0; i--) {
  				sg = sg_get(ctx->sg, nsg, i);
  				j = min(nextlen, sg_dma_len(sg));
  				memcpy(ctx->bufnext + nextlen - j,
  					sg_virt(sg) + sg_dma_len(sg) - j, j);
  				if (j == sg_dma_len(sg))
  					ctx->sg_nents--;
  				nextlen -= j;
  				if (nextlen == 0)
  					break;
  			}
  		}
  	}
  
  finish_update:
  	if (ctx->bufnext_len && (ctx->flag == CRC_CRYPTO_STATE_FINALUPDATE ||
  		ctx->flag == CRC_CRYPTO_STATE_FINISH))
  		ctx->sg_buflen += CHKSUM_DIGEST_SIZE;
  
  	/* set CRC data count before start DMA */
52e6e543f   Sonic Zhang   crypto: bfin_crc ...
391
  	writel(ctx->sg_buflen >> 2, &crc->regs->datacnt);
b8840098b   Sonic Zhang   crypto: bfin_crc ...
392
393
394
395
396
  
  	/* setup and enable CRC DMA */
  	bfin_crypto_crc_config_dma(crc);
  
  	/* finally kick off CRC operation */
52e6e543f   Sonic Zhang   crypto: bfin_crc ...
397
398
  	reg = readl(&crc->regs->control);
  	writel(reg | BLKEN, &crc->regs->control);
b8840098b   Sonic Zhang   crypto: bfin_crc ...
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
  
  	return -EINPROGRESS;
  }
  
  static int bfin_crypto_crc_update(struct ahash_request *req)
  {
  	struct bfin_crypto_crc_reqctx *ctx = ahash_request_ctx(req);
  
  	if (!req->nbytes)
  		return 0;
  
  	dev_dbg(ctx->crc->dev, "crc_update
  ");
  	ctx->total += req->nbytes;
  	ctx->flag = CRC_CRYPTO_STATE_UPDATE;
  
  	return bfin_crypto_crc_handle_queue(ctx->crc, req);
  }
  
  static int bfin_crypto_crc_final(struct ahash_request *req)
  {
  	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  	struct bfin_crypto_crc_ctx *crc_ctx = crypto_ahash_ctx(tfm);
  	struct bfin_crypto_crc_reqctx *ctx = ahash_request_ctx(req);
  
  	dev_dbg(ctx->crc->dev, "crc_final
  ");
  	ctx->flag = CRC_CRYPTO_STATE_FINISH;
  	crc_ctx->key = 0;
  
  	return bfin_crypto_crc_handle_queue(ctx->crc, req);
  }
  
  static int bfin_crypto_crc_finup(struct ahash_request *req)
  {
  	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
  	struct bfin_crypto_crc_ctx *crc_ctx = crypto_ahash_ctx(tfm);
  	struct bfin_crypto_crc_reqctx *ctx = ahash_request_ctx(req);
  
  	dev_dbg(ctx->crc->dev, "crc_finishupdate
  ");
  	ctx->total += req->nbytes;
  	ctx->flag = CRC_CRYPTO_STATE_FINALUPDATE;
  	crc_ctx->key = 0;
  
  	return bfin_crypto_crc_handle_queue(ctx->crc, req);
  }
  
  static int bfin_crypto_crc_digest(struct ahash_request *req)
  {
  	int ret;
  
  	ret = bfin_crypto_crc_init(req);
  	if (ret)
  		return ret;
  
  	return bfin_crypto_crc_finup(req);
  }
  
  static int bfin_crypto_crc_setkey(struct crypto_ahash *tfm, const u8 *key,
  			unsigned int keylen)
  {
  	struct bfin_crypto_crc_ctx *crc_ctx = crypto_ahash_ctx(tfm);
  
  	dev_dbg(crc_ctx->crc->dev, "crc_setkey
  ");
  	if (keylen != CHKSUM_DIGEST_SIZE) {
  		crypto_ahash_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
  		return -EINVAL;
  	}
  
  	crc_ctx->key = get_unaligned_le32(key);
  
  	return 0;
  }
  
  static int bfin_crypto_crc_cra_init(struct crypto_tfm *tfm)
  {
  	struct bfin_crypto_crc_ctx *crc_ctx = crypto_tfm_ctx(tfm);
  
  	crc_ctx->key = 0;
  	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
  				 sizeof(struct bfin_crypto_crc_reqctx));
  
  	return 0;
  }
  
  static void bfin_crypto_crc_cra_exit(struct crypto_tfm *tfm)
  {
  }
  
  static struct ahash_alg algs = {
  	.init		= bfin_crypto_crc_init,
  	.update		= bfin_crypto_crc_update,
  	.final		= bfin_crypto_crc_final,
  	.finup		= bfin_crypto_crc_finup,
  	.digest		= bfin_crypto_crc_digest,
  	.setkey		= bfin_crypto_crc_setkey,
  	.halg.digestsize	= CHKSUM_DIGEST_SIZE,
  	.halg.base	= {
  		.cra_name		= "hmac(crc32)",
  		.cra_driver_name	= DRIVER_NAME,
  		.cra_priority		= 100,
  		.cra_flags		= CRYPTO_ALG_TYPE_AHASH |
  						CRYPTO_ALG_ASYNC,
  		.cra_blocksize		= CHKSUM_BLOCK_SIZE,
  		.cra_ctxsize		= sizeof(struct bfin_crypto_crc_ctx),
  		.cra_alignmask		= 3,
  		.cra_module		= THIS_MODULE,
  		.cra_init		= bfin_crypto_crc_cra_init,
  		.cra_exit		= bfin_crypto_crc_cra_exit,
  	}
  };
  
  static void bfin_crypto_crc_done_task(unsigned long data)
  {
  	struct bfin_crypto_crc *crc = (struct bfin_crypto_crc *)data;
  
  	bfin_crypto_crc_handle_queue(crc, NULL);
  }
  
  static irqreturn_t bfin_crypto_crc_handler(int irq, void *dev_id)
  {
  	struct bfin_crypto_crc *crc = dev_id;
52e6e543f   Sonic Zhang   crypto: bfin_crc ...
523
  	u32 reg;
b8840098b   Sonic Zhang   crypto: bfin_crc ...
524

52e6e543f   Sonic Zhang   crypto: bfin_crc ...
525
526
  	if (readl(&crc->regs->status) & DCNTEXP) {
  		writel(DCNTEXP, &crc->regs->status);
b8840098b   Sonic Zhang   crypto: bfin_crc ...
527
528
  
  		/* prepare results */
52e6e543f   Sonic Zhang   crypto: bfin_crc ...
529
530
  		put_unaligned_le32(readl(&crc->regs->result),
  			crc->req->result);
b8840098b   Sonic Zhang   crypto: bfin_crc ...
531

52e6e543f   Sonic Zhang   crypto: bfin_crc ...
532
533
  		reg = readl(&crc->regs->control);
  		writel(reg & ~BLKEN, &crc->regs->control);
b8840098b   Sonic Zhang   crypto: bfin_crc ...
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
  		crc->busy = 0;
  
  		if (crc->req->base.complete)
  			crc->req->base.complete(&crc->req->base, 0);
  
  		tasklet_schedule(&crc->done_task);
  
  		return IRQ_HANDLED;
  	} else
  		return IRQ_NONE;
  }
  
  #ifdef CONFIG_PM
  /**
   *	bfin_crypto_crc_suspend - suspend crc device
   *	@pdev: device being suspended
   *	@state: requested suspend state
   */
  static int bfin_crypto_crc_suspend(struct platform_device *pdev, pm_message_t state)
  {
  	struct bfin_crypto_crc *crc = platform_get_drvdata(pdev);
  	int i = 100000;
52e6e543f   Sonic Zhang   crypto: bfin_crc ...
556
  	while ((readl(&crc->regs->control) & BLKEN) && --i)
b8840098b   Sonic Zhang   crypto: bfin_crc ...
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
  		cpu_relax();
  
  	if (i == 0)
  		return -EBUSY;
  
  	return 0;
  }
  #else
  # define bfin_crypto_crc_suspend NULL
  #endif
  
  #define bfin_crypto_crc_resume NULL
  
  /**
   *	bfin_crypto_crc_probe - Initialize module
   *
   */
49cfe4db2   Greg Kroah-Hartman   Drivers: crypto: ...
574
  static int bfin_crypto_crc_probe(struct platform_device *pdev)
b8840098b   Sonic Zhang   crypto: bfin_crc ...
575
576
577
578
579
580
  {
  	struct device *dev = &pdev->dev;
  	struct resource *res;
  	struct bfin_crypto_crc *crc;
  	unsigned int timeout = 100000;
  	int ret;
4ea5d9998   Sonic Zhang   crypt: bfin_crc -...
581
  	crc = devm_kzalloc(dev, sizeof(*crc), GFP_KERNEL);
b8840098b   Sonic Zhang   crypto: bfin_crc ...
582
583
584
585
586
587
588
589
590
591
592
593
594
595
  	if (!crc) {
  		dev_err(&pdev->dev, "fail to malloc bfin_crypto_crc
  ");
  		return -ENOMEM;
  	}
  
  	crc->dev = dev;
  
  	INIT_LIST_HEAD(&crc->list);
  	spin_lock_init(&crc->lock);
  	tasklet_init(&crc->done_task, bfin_crypto_crc_done_task, (unsigned long)crc);
  	crypto_init_queue(&crc->queue, CRC_CCRYPTO_QUEUE_LENGTH);
  
  	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4ea5d9998   Sonic Zhang   crypt: bfin_crc -...
596
597
  	crc->regs = devm_ioremap_resource(dev, res);
  	if (IS_ERR((void *)crc->regs)) {
b8840098b   Sonic Zhang   crypto: bfin_crc ...
598
599
  		dev_err(&pdev->dev, "Cannot map CRC IO
  ");
4ea5d9998   Sonic Zhang   crypt: bfin_crc -...
600
  		return PTR_ERR((void *)crc->regs);
b8840098b   Sonic Zhang   crypto: bfin_crc ...
601
602
603
604
605
606
  	}
  
  	crc->irq = platform_get_irq(pdev, 0);
  	if (crc->irq < 0) {
  		dev_err(&pdev->dev, "No CRC DCNTEXP IRQ specified
  ");
4ea5d9998   Sonic Zhang   crypt: bfin_crc -...
607
  		return -ENOENT;
b8840098b   Sonic Zhang   crypto: bfin_crc ...
608
  	}
4ea5d9998   Sonic Zhang   crypt: bfin_crc -...
609
610
  	ret = devm_request_irq(dev, crc->irq, bfin_crypto_crc_handler,
  			IRQF_SHARED, dev_name(dev), crc);
b8840098b   Sonic Zhang   crypto: bfin_crc ...
611
612
613
  	if (ret) {
  		dev_err(&pdev->dev, "Unable to request blackfin crc irq
  ");
4ea5d9998   Sonic Zhang   crypt: bfin_crc -...
614
  		return ret;
b8840098b   Sonic Zhang   crypto: bfin_crc ...
615
616
617
618
619
620
  	}
  
  	res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  	if (res == NULL) {
  		dev_err(&pdev->dev, "No CRC DMA channel specified
  ");
4ea5d9998   Sonic Zhang   crypt: bfin_crc -...
621
  		return -ENOENT;
b8840098b   Sonic Zhang   crypto: bfin_crc ...
622
623
624
625
626
627
628
  	}
  	crc->dma_ch = res->start;
  
  	ret = request_dma(crc->dma_ch, dev_name(dev));
  	if (ret) {
  		dev_err(&pdev->dev, "Unable to attach Blackfin CRC DMA channel
  ");
4ea5d9998   Sonic Zhang   crypt: bfin_crc -...
629
  		return ret;
b8840098b   Sonic Zhang   crypto: bfin_crc ...
630
631
632
633
634
635
636
637
638
639
640
641
  	}
  
  	crc->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, &crc->sg_dma, GFP_KERNEL);
  	if (crc->sg_cpu == NULL) {
  		ret = -ENOMEM;
  		goto out_error_dma;
  	}
  	/*
  	 * need at most CRC_MAX_DMA_DESC sg + CRC_MAX_DMA_DESC middle  +
  	 * 1 last + 1 next dma descriptors
  	 */
  	crc->sg_mid_buf = (u8 *)(crc->sg_cpu + ((CRC_MAX_DMA_DESC + 1) << 1));
52d77eb17   Sonic Zhang   cryptoo: bfin_crc...
642
643
  	crc->sg_mid_dma = crc->sg_dma + sizeof(struct dma_desc_array)
  			* ((CRC_MAX_DMA_DESC + 1) << 1);
b8840098b   Sonic Zhang   crypto: bfin_crc ...
644

52e6e543f   Sonic Zhang   crypto: bfin_crc ...
645
646
647
  	writel(0, &crc->regs->control);
  	crc->poly = (u32)pdev->dev.platform_data;
  	writel(crc->poly, &crc->regs->poly);
b8840098b   Sonic Zhang   crypto: bfin_crc ...
648

52e6e543f   Sonic Zhang   crypto: bfin_crc ...
649
  	while (!(readl(&crc->regs->status) & LUTDONE) && (--timeout) > 0)
b8840098b   Sonic Zhang   crypto: bfin_crc ...
650
651
652
653
654
  		cpu_relax();
  
  	if (timeout == 0)
  		dev_info(&pdev->dev, "init crc poly timeout
  ");
8d390395f   Sonic Zhang   crypto: bfin_crc ...
655
  	platform_set_drvdata(pdev, crc);
b8840098b   Sonic Zhang   crypto: bfin_crc ...
656
657
658
  	spin_lock(&crc_list.lock);
  	list_add(&crc->list, &crc_list.dev_list);
  	spin_unlock(&crc_list.lock);
8d390395f   Sonic Zhang   crypto: bfin_crc ...
659
660
661
662
663
664
665
666
  	if (list_is_singular(&crc_list.dev_list)) {
  		ret = crypto_register_ahash(&algs);
  		if (ret) {
  			dev_err(&pdev->dev,
  				"Can't register crypto ahash device
  ");
  			goto out_error_dma;
  		}
b8840098b   Sonic Zhang   crypto: bfin_crc ...
667
668
669
670
671
672
673
674
675
676
677
  	}
  
  	dev_info(&pdev->dev, "initialized
  ");
  
  	return 0;
  
  out_error_dma:
  	if (crc->sg_cpu)
  		dma_free_coherent(&pdev->dev, PAGE_SIZE, crc->sg_cpu, crc->sg_dma);
  	free_dma(crc->dma_ch);
b8840098b   Sonic Zhang   crypto: bfin_crc ...
678
679
680
681
682
683
684
685
  
  	return ret;
  }
  
  /**
   *	bfin_crypto_crc_remove - Initialize module
   *
   */
49cfe4db2   Greg Kroah-Hartman   Drivers: crypto: ...
686
  static int bfin_crypto_crc_remove(struct platform_device *pdev)
b8840098b   Sonic Zhang   crypto: bfin_crc ...
687
688
689
690
691
692
693
694
695
696
697
698
  {
  	struct bfin_crypto_crc *crc = platform_get_drvdata(pdev);
  
  	if (!crc)
  		return -ENODEV;
  
  	spin_lock(&crc_list.lock);
  	list_del(&crc->list);
  	spin_unlock(&crc_list.lock);
  
  	crypto_unregister_ahash(&algs);
  	tasklet_kill(&crc->done_task);
b8840098b   Sonic Zhang   crypto: bfin_crc ...
699
  	free_dma(crc->dma_ch);
b8840098b   Sonic Zhang   crypto: bfin_crc ...
700
701
702
703
704
705
  
  	return 0;
  }
  
  static struct platform_driver bfin_crypto_crc_driver = {
  	.probe     = bfin_crypto_crc_probe,
49cfe4db2   Greg Kroah-Hartman   Drivers: crypto: ...
706
  	.remove    = bfin_crypto_crc_remove,
b8840098b   Sonic Zhang   crypto: bfin_crc ...
707
708
709
710
  	.suspend   = bfin_crypto_crc_suspend,
  	.resume    = bfin_crypto_crc_resume,
  	.driver    = {
  		.name  = DRIVER_NAME,
b8840098b   Sonic Zhang   crypto: bfin_crc ...
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
  	},
  };
  
  /**
   *	bfin_crypto_crc_mod_init - Initialize module
   *
   *	Checks the module params and registers the platform driver.
   *	Real work is in the platform probe function.
   */
  static int __init bfin_crypto_crc_mod_init(void)
  {
  	int ret;
  
  	pr_info("Blackfin hardware CRC crypto driver
  ");
  
  	INIT_LIST_HEAD(&crc_list.dev_list);
  	spin_lock_init(&crc_list.lock);
  
  	ret = platform_driver_register(&bfin_crypto_crc_driver);
  	if (ret) {
28c29f565   Masanari Iida   crypto: bfin_crc ...
732
733
  		pr_err("unable to register driver
  ");
b8840098b   Sonic Zhang   crypto: bfin_crc ...
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
  		return ret;
  	}
  
  	return 0;
  }
  
  /**
   *	bfin_crypto_crc_mod_exit - Deinitialize module
   */
  static void __exit bfin_crypto_crc_mod_exit(void)
  {
  	platform_driver_unregister(&bfin_crypto_crc_driver);
  }
  
  module_init(bfin_crypto_crc_mod_init);
  module_exit(bfin_crypto_crc_mod_exit);
  
  MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
  MODULE_DESCRIPTION("Blackfin CRC hardware crypto driver");
  MODULE_LICENSE("GPL");