blob: 7384e91c8b32b58280504d9db825df05657555c9 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jamie Ilesce921362011-02-21 16:43:21 +11002/*
3 * Copyright (c) 2010-2011 Picochip Ltd., Jamie Iles
Jamie Ilesce921362011-02-21 16:43:21 +11004 */
Herbert Xu2d78db02015-05-11 17:47:45 +08005#include <crypto/internal/aead.h>
Jamie Ilesce921362011-02-21 16:43:21 +11006#include <crypto/aes.h>
7#include <crypto/algapi.h>
8#include <crypto/authenc.h>
Ard Biesheuvel0157fb22019-08-15 12:01:00 +03009#include <crypto/internal/des.h>
Jamie Ilesce921362011-02-21 16:43:21 +110010#include <crypto/md5.h>
11#include <crypto/sha.h>
12#include <crypto/internal/skcipher.h>
13#include <linux/clk.h>
14#include <linux/crypto.h>
15#include <linux/delay.h>
16#include <linux/dma-mapping.h>
17#include <linux/dmapool.h>
18#include <linux/err.h>
19#include <linux/init.h>
20#include <linux/interrupt.h>
21#include <linux/io.h>
22#include <linux/list.h>
23#include <linux/module.h>
Jamie Iles30343ef2011-08-01 17:25:19 +010024#include <linux/of.h>
Jamie Ilesce921362011-02-21 16:43:21 +110025#include <linux/platform_device.h>
26#include <linux/pm.h>
27#include <linux/rtnetlink.h>
28#include <linux/scatterlist.h>
29#include <linux/sched.h>
Herbert Xu72071fe2015-06-11 11:28:32 +080030#include <linux/sizes.h>
Jamie Ilesce921362011-02-21 16:43:21 +110031#include <linux/slab.h>
32#include <linux/timer.h>
33
34#include "picoxcell_crypto_regs.h"
35
36/*
37 * The threshold for the number of entries in the CMD FIFO available before
38 * the CMD0_CNT interrupt is raised. Increasing this value will reduce the
39 * number of interrupts raised to the CPU.
40 */
41#define CMD0_IRQ_THRESHOLD 1
42
43/*
44 * The timeout period (in jiffies) for a PDU. When the the number of PDUs in
45 * flight is greater than the STAT_IRQ_THRESHOLD or 0 the timer is disabled.
46 * When there are packets in flight but lower than the threshold, we enable
47 * the timer and at expiry, attempt to remove any processed packets from the
48 * queue and if there are still packets left, schedule the timer again.
49 */
50#define PACKET_TIMEOUT 1
51
52/* The priority to register each algorithm with. */
53#define SPACC_CRYPTO_ALG_PRIORITY 10000
54
55#define SPACC_CRYPTO_KASUMI_F8_KEY_LEN 16
56#define SPACC_CRYPTO_IPSEC_CIPHER_PG_SZ 64
57#define SPACC_CRYPTO_IPSEC_HASH_PG_SZ 64
58#define SPACC_CRYPTO_IPSEC_MAX_CTXS 32
59#define SPACC_CRYPTO_IPSEC_FIFO_SZ 32
60#define SPACC_CRYPTO_L2_CIPHER_PG_SZ 64
61#define SPACC_CRYPTO_L2_HASH_PG_SZ 64
62#define SPACC_CRYPTO_L2_MAX_CTXS 128
63#define SPACC_CRYPTO_L2_FIFO_SZ 128
64
65#define MAX_DDT_LEN 16
66
67/* DDT format. This must match the hardware DDT format exactly. */
68struct spacc_ddt {
69 dma_addr_t p;
70 u32 len;
71};
72
73/*
74 * Asynchronous crypto request structure.
75 *
76 * This structure defines a request that is either queued for processing or
77 * being processed.
78 */
79struct spacc_req {
80 struct list_head list;
81 struct spacc_engine *engine;
82 struct crypto_async_request *req;
83 int result;
84 bool is_encrypt;
85 unsigned ctx_id;
86 dma_addr_t src_addr, dst_addr;
87 struct spacc_ddt *src_ddt, *dst_ddt;
88 void (*complete)(struct spacc_req *req);
Herbert Xuc1359492015-07-30 17:53:19 +080089};
Jamie Ilesce921362011-02-21 16:43:21 +110090
Herbert Xuc1359492015-07-30 17:53:19 +080091struct spacc_aead {
92 unsigned long ctrl_default;
93 unsigned long type;
94 struct aead_alg alg;
95 struct spacc_engine *engine;
96 struct list_head entry;
97 int key_offs;
98 int iv_offs;
Jamie Ilesce921362011-02-21 16:43:21 +110099};
100
101struct spacc_engine {
102 void __iomem *regs;
103 struct list_head pending;
104 int next_ctx;
105 spinlock_t hw_lock;
106 int in_flight;
107 struct list_head completed;
108 struct list_head in_progress;
109 struct tasklet_struct complete;
110 unsigned long fifo_sz;
111 void __iomem *cipher_ctx_base;
112 void __iomem *hash_key_base;
113 struct spacc_alg *algs;
114 unsigned num_algs;
115 struct list_head registered_algs;
Herbert Xuc1359492015-07-30 17:53:19 +0800116 struct spacc_aead *aeads;
117 unsigned num_aeads;
118 struct list_head registered_aeads;
Jamie Ilesce921362011-02-21 16:43:21 +1100119 size_t cipher_pg_sz;
120 size_t hash_pg_sz;
121 const char *name;
122 struct clk *clk;
123 struct device *dev;
124 unsigned max_ctxs;
125 struct timer_list packet_timeout;
126 unsigned stat_irq_thresh;
127 struct dma_pool *req_pool;
128};
129
130/* Algorithm type mask. */
131#define SPACC_CRYPTO_ALG_MASK 0x7
132
133/* SPACC definition of a crypto algorithm. */
134struct spacc_alg {
135 unsigned long ctrl_default;
136 unsigned long type;
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100137 struct skcipher_alg alg;
Jamie Ilesce921362011-02-21 16:43:21 +1100138 struct spacc_engine *engine;
139 struct list_head entry;
140 int key_offs;
141 int iv_offs;
142};
143
144/* Generic context structure for any algorithm type. */
145struct spacc_generic_ctx {
146 struct spacc_engine *engine;
147 int flags;
148 int key_offs;
149 int iv_offs;
150};
151
152/* Block cipher context. */
153struct spacc_ablk_ctx {
154 struct spacc_generic_ctx generic;
155 u8 key[AES_MAX_KEY_SIZE];
156 u8 key_len;
157 /*
158 * The fallback cipher. If the operation can't be done in hardware,
159 * fallback to a software version.
160 */
Kees Cook6adfbd62018-09-18 19:10:59 -0700161 struct crypto_sync_skcipher *sw_cipher;
Jamie Ilesce921362011-02-21 16:43:21 +1100162};
163
164/* AEAD cipher context. */
165struct spacc_aead_ctx {
166 struct spacc_generic_ctx generic;
167 u8 cipher_key[AES_MAX_KEY_SIZE];
168 u8 hash_ctx[SPACC_CRYPTO_IPSEC_HASH_PG_SZ];
169 u8 cipher_key_len;
170 u8 hash_key_len;
171 struct crypto_aead *sw_cipher;
Jamie Ilesce921362011-02-21 16:43:21 +1100172};
173
Jamie Iles40bfc142011-03-27 10:48:29 +0800174static int spacc_ablk_submit(struct spacc_req *req);
175
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100176static inline struct spacc_alg *to_spacc_skcipher(struct skcipher_alg *alg)
Jamie Ilesce921362011-02-21 16:43:21 +1100177{
178 return alg ? container_of(alg, struct spacc_alg, alg) : NULL;
179}
180
Herbert Xuc1359492015-07-30 17:53:19 +0800181static inline struct spacc_aead *to_spacc_aead(struct aead_alg *alg)
182{
183 return container_of(alg, struct spacc_aead, alg);
184}
185
Jamie Ilesce921362011-02-21 16:43:21 +1100186static inline int spacc_fifo_cmd_full(struct spacc_engine *engine)
187{
188 u32 fifo_stat = readl(engine->regs + SPA_FIFO_STAT_REG_OFFSET);
189
190 return fifo_stat & SPA_FIFO_CMD_FULL;
191}
192
193/*
194 * Given a cipher context, and a context number, get the base address of the
195 * context page.
196 *
197 * Returns the address of the context page where the key/context may
198 * be written.
199 */
200static inline void __iomem *spacc_ctx_page_addr(struct spacc_generic_ctx *ctx,
201 unsigned indx,
202 bool is_cipher_ctx)
203{
204 return is_cipher_ctx ? ctx->engine->cipher_ctx_base +
205 (indx * ctx->engine->cipher_pg_sz) :
206 ctx->engine->hash_key_base + (indx * ctx->engine->hash_pg_sz);
207}
208
209/* The context pages can only be written with 32-bit accesses. */
210static inline void memcpy_toio32(u32 __iomem *dst, const void *src,
211 unsigned count)
212{
213 const u32 *src32 = (const u32 *) src;
214
215 while (count--)
216 writel(*src32++, dst++);
217}
218
219static void spacc_cipher_write_ctx(struct spacc_generic_ctx *ctx,
220 void __iomem *page_addr, const u8 *key,
221 size_t key_len, const u8 *iv, size_t iv_len)
222{
223 void __iomem *key_ptr = page_addr + ctx->key_offs;
224 void __iomem *iv_ptr = page_addr + ctx->iv_offs;
225
226 memcpy_toio32(key_ptr, key, key_len / 4);
227 memcpy_toio32(iv_ptr, iv, iv_len / 4);
228}
229
230/*
231 * Load a context into the engines context memory.
232 *
233 * Returns the index of the context page where the context was loaded.
234 */
235static unsigned spacc_load_ctx(struct spacc_generic_ctx *ctx,
236 const u8 *ciph_key, size_t ciph_len,
237 const u8 *iv, size_t ivlen, const u8 *hash_key,
238 size_t hash_len)
239{
240 unsigned indx = ctx->engine->next_ctx++;
241 void __iomem *ciph_page_addr, *hash_page_addr;
242
243 ciph_page_addr = spacc_ctx_page_addr(ctx, indx, 1);
244 hash_page_addr = spacc_ctx_page_addr(ctx, indx, 0);
245
246 ctx->engine->next_ctx &= ctx->engine->fifo_sz - 1;
247 spacc_cipher_write_ctx(ctx, ciph_page_addr, ciph_key, ciph_len, iv,
248 ivlen);
249 writel(ciph_len | (indx << SPA_KEY_SZ_CTX_INDEX_OFFSET) |
250 (1 << SPA_KEY_SZ_CIPHER_OFFSET),
251 ctx->engine->regs + SPA_KEY_SZ_REG_OFFSET);
252
253 if (hash_key) {
254 memcpy_toio32(hash_page_addr, hash_key, hash_len / 4);
255 writel(hash_len | (indx << SPA_KEY_SZ_CTX_INDEX_OFFSET),
256 ctx->engine->regs + SPA_KEY_SZ_REG_OFFSET);
257 }
258
259 return indx;
260}
261
Jamie Ilesce921362011-02-21 16:43:21 +1100262static inline void ddt_set(struct spacc_ddt *ddt, dma_addr_t phys, size_t len)
263{
264 ddt->p = phys;
265 ddt->len = len;
266}
267
268/*
269 * Take a crypto request and scatterlists for the data and turn them into DDTs
270 * for passing to the crypto engines. This also DMA maps the data so that the
271 * crypto engines can DMA to/from them.
272 */
273static struct spacc_ddt *spacc_sg_to_ddt(struct spacc_engine *engine,
274 struct scatterlist *payload,
275 unsigned nbytes,
276 enum dma_data_direction dir,
277 dma_addr_t *ddt_phys)
278{
LABBE Corentinf53e38a2015-11-19 13:38:18 +0100279 unsigned mapped_ents;
Jamie Ilesce921362011-02-21 16:43:21 +1100280 struct scatterlist *cur;
281 struct spacc_ddt *ddt;
282 int i;
LABBE Corentinf53e38a2015-11-19 13:38:18 +0100283 int nents;
Jamie Ilesce921362011-02-21 16:43:21 +1100284
LABBE Corentinf051f952015-11-04 21:13:37 +0100285 nents = sg_nents_for_len(payload, nbytes);
286 if (nents < 0) {
287 dev_err(engine->dev, "Invalid numbers of SG.\n");
288 return NULL;
289 }
Jamie Ilesce921362011-02-21 16:43:21 +1100290 mapped_ents = dma_map_sg(engine->dev, payload, nents, dir);
291
292 if (mapped_ents + 1 > MAX_DDT_LEN)
293 goto out;
294
295 ddt = dma_pool_alloc(engine->req_pool, GFP_ATOMIC, ddt_phys);
296 if (!ddt)
297 goto out;
298
299 for_each_sg(payload, cur, mapped_ents, i)
300 ddt_set(&ddt[i], sg_dma_address(cur), sg_dma_len(cur));
301 ddt_set(&ddt[mapped_ents], 0, 0);
302
303 return ddt;
304
305out:
306 dma_unmap_sg(engine->dev, payload, nents, dir);
307 return NULL;
308}
309
Herbert Xuc1359492015-07-30 17:53:19 +0800310static int spacc_aead_make_ddts(struct aead_request *areq)
Jamie Ilesce921362011-02-21 16:43:21 +1100311{
Herbert Xuc1359492015-07-30 17:53:19 +0800312 struct crypto_aead *aead = crypto_aead_reqtfm(areq);
313 struct spacc_req *req = aead_request_ctx(areq);
Jamie Ilesce921362011-02-21 16:43:21 +1100314 struct spacc_engine *engine = req->engine;
315 struct spacc_ddt *src_ddt, *dst_ddt;
Herbert Xu81781e62015-06-11 11:28:34 +0800316 unsigned total;
LABBE Corentinf53e38a2015-11-19 13:38:18 +0100317 int src_nents, dst_nents;
Jamie Ilesce921362011-02-21 16:43:21 +1100318 struct scatterlist *cur;
Herbert Xuc1359492015-07-30 17:53:19 +0800319 int i, dst_ents, src_ents;
320
321 total = areq->assoclen + areq->cryptlen;
322 if (req->is_encrypt)
323 total += crypto_aead_authsize(aead);
324
LABBE Corentinf051f952015-11-04 21:13:37 +0100325 src_nents = sg_nents_for_len(areq->src, total);
326 if (src_nents < 0) {
327 dev_err(engine->dev, "Invalid numbers of src SG.\n");
328 return src_nents;
329 }
Herbert Xuc1359492015-07-30 17:53:19 +0800330 if (src_nents + 1 > MAX_DDT_LEN)
331 return -E2BIG;
332
333 dst_nents = 0;
334 if (areq->src != areq->dst) {
LABBE Corentinf051f952015-11-04 21:13:37 +0100335 dst_nents = sg_nents_for_len(areq->dst, total);
336 if (dst_nents < 0) {
337 dev_err(engine->dev, "Invalid numbers of dst SG.\n");
338 return dst_nents;
339 }
Herbert Xuc1359492015-07-30 17:53:19 +0800340 if (src_nents + 1 > MAX_DDT_LEN)
341 return -E2BIG;
342 }
Jamie Ilesce921362011-02-21 16:43:21 +1100343
344 src_ddt = dma_pool_alloc(engine->req_pool, GFP_ATOMIC, &req->src_addr);
345 if (!src_ddt)
Herbert Xuc1359492015-07-30 17:53:19 +0800346 goto err;
Jamie Ilesce921362011-02-21 16:43:21 +1100347
348 dst_ddt = dma_pool_alloc(engine->req_pool, GFP_ATOMIC, &req->dst_addr);
Herbert Xuc1359492015-07-30 17:53:19 +0800349 if (!dst_ddt)
350 goto err_free_src;
Jamie Ilesce921362011-02-21 16:43:21 +1100351
352 req->src_ddt = src_ddt;
353 req->dst_ddt = dst_ddt;
354
Herbert Xuc1359492015-07-30 17:53:19 +0800355 if (dst_nents) {
356 src_ents = dma_map_sg(engine->dev, areq->src, src_nents,
Jamie Ilesce921362011-02-21 16:43:21 +1100357 DMA_TO_DEVICE);
Herbert Xuc1359492015-07-30 17:53:19 +0800358 if (!src_ents)
359 goto err_free_dst;
360
361 dst_ents = dma_map_sg(engine->dev, areq->dst, dst_nents,
Jamie Ilesce921362011-02-21 16:43:21 +1100362 DMA_FROM_DEVICE);
Herbert Xuc1359492015-07-30 17:53:19 +0800363
364 if (!dst_ents) {
365 dma_unmap_sg(engine->dev, areq->src, src_nents,
366 DMA_TO_DEVICE);
367 goto err_free_dst;
368 }
Jamie Ilesce921362011-02-21 16:43:21 +1100369 } else {
Herbert Xuc1359492015-07-30 17:53:19 +0800370 src_ents = dma_map_sg(engine->dev, areq->src, src_nents,
Jamie Ilesce921362011-02-21 16:43:21 +1100371 DMA_BIDIRECTIONAL);
Herbert Xuc1359492015-07-30 17:53:19 +0800372 if (!src_ents)
373 goto err_free_dst;
374 dst_ents = src_ents;
Jamie Ilesce921362011-02-21 16:43:21 +1100375 }
376
377 /*
Jamie Ilesce921362011-02-21 16:43:21 +1100378 * Now map in the payload for the source and destination and terminate
379 * with the NULL pointers.
380 */
Herbert Xuc1359492015-07-30 17:53:19 +0800381 for_each_sg(areq->src, cur, src_ents, i)
Jamie Ilesce921362011-02-21 16:43:21 +1100382 ddt_set(src_ddt++, sg_dma_address(cur), sg_dma_len(cur));
Jamie Ilesce921362011-02-21 16:43:21 +1100383
Herbert Xuc1359492015-07-30 17:53:19 +0800384 /* For decryption we need to skip the associated data. */
385 total = req->is_encrypt ? 0 : areq->assoclen;
386 for_each_sg(areq->dst, cur, dst_ents, i) {
387 unsigned len = sg_dma_len(cur);
388
389 if (len <= total) {
390 total -= len;
391 continue;
392 }
393
394 ddt_set(dst_ddt++, sg_dma_address(cur) + total, len - total);
395 }
Jamie Ilesce921362011-02-21 16:43:21 +1100396
397 ddt_set(src_ddt, 0, 0);
398 ddt_set(dst_ddt, 0, 0);
399
400 return 0;
Herbert Xuc1359492015-07-30 17:53:19 +0800401
402err_free_dst:
403 dma_pool_free(engine->req_pool, dst_ddt, req->dst_addr);
404err_free_src:
405 dma_pool_free(engine->req_pool, src_ddt, req->src_addr);
406err:
407 return -ENOMEM;
Jamie Ilesce921362011-02-21 16:43:21 +1100408}
409
410static void spacc_aead_free_ddts(struct spacc_req *req)
411{
412 struct aead_request *areq = container_of(req->req, struct aead_request,
413 base);
Herbert Xuc1359492015-07-30 17:53:19 +0800414 struct crypto_aead *aead = crypto_aead_reqtfm(areq);
415 unsigned total = areq->assoclen + areq->cryptlen +
416 (req->is_encrypt ? crypto_aead_authsize(aead) : 0);
417 struct spacc_aead_ctx *aead_ctx = crypto_aead_ctx(aead);
Jamie Ilesce921362011-02-21 16:43:21 +1100418 struct spacc_engine *engine = aead_ctx->generic.engine;
LABBE Corentinf051f952015-11-04 21:13:37 +0100419 int nents = sg_nents_for_len(areq->src, total);
420
421 /* sg_nents_for_len should not fail since it works when mapping sg */
422 if (unlikely(nents < 0)) {
423 dev_err(engine->dev, "Invalid numbers of src SG.\n");
424 return;
425 }
Jamie Ilesce921362011-02-21 16:43:21 +1100426
427 if (areq->src != areq->dst) {
428 dma_unmap_sg(engine->dev, areq->src, nents, DMA_TO_DEVICE);
LABBE Corentinf051f952015-11-04 21:13:37 +0100429 nents = sg_nents_for_len(areq->dst, total);
430 if (unlikely(nents < 0)) {
431 dev_err(engine->dev, "Invalid numbers of dst SG.\n");
432 return;
433 }
434 dma_unmap_sg(engine->dev, areq->dst, nents, DMA_FROM_DEVICE);
Jamie Ilesce921362011-02-21 16:43:21 +1100435 } else
436 dma_unmap_sg(engine->dev, areq->src, nents, DMA_BIDIRECTIONAL);
437
Jamie Ilesce921362011-02-21 16:43:21 +1100438 dma_pool_free(engine->req_pool, req->src_ddt, req->src_addr);
439 dma_pool_free(engine->req_pool, req->dst_ddt, req->dst_addr);
440}
441
442static void spacc_free_ddt(struct spacc_req *req, struct spacc_ddt *ddt,
443 dma_addr_t ddt_addr, struct scatterlist *payload,
444 unsigned nbytes, enum dma_data_direction dir)
445{
LABBE Corentinf051f952015-11-04 21:13:37 +0100446 int nents = sg_nents_for_len(payload, nbytes);
447
448 if (nents < 0) {
449 dev_err(req->engine->dev, "Invalid numbers of SG.\n");
450 return;
451 }
Jamie Ilesce921362011-02-21 16:43:21 +1100452
453 dma_unmap_sg(req->engine->dev, payload, nents, dir);
454 dma_pool_free(req->engine->req_pool, ddt, ddt_addr);
455}
456
Jamie Ilesce921362011-02-21 16:43:21 +1100457static int spacc_aead_setkey(struct crypto_aead *tfm, const u8 *key,
458 unsigned int keylen)
459{
460 struct spacc_aead_ctx *ctx = crypto_aead_ctx(tfm);
Mathias Krauseab827fb32013-10-15 13:49:33 +0200461 struct crypto_authenc_keys keys;
Herbert Xuc1359492015-07-30 17:53:19 +0800462 int err;
463
464 crypto_aead_clear_flags(ctx->sw_cipher, CRYPTO_TFM_REQ_MASK);
465 crypto_aead_set_flags(ctx->sw_cipher, crypto_aead_get_flags(tfm) &
466 CRYPTO_TFM_REQ_MASK);
467 err = crypto_aead_setkey(ctx->sw_cipher, key, keylen);
Herbert Xuc1359492015-07-30 17:53:19 +0800468 if (err)
469 return err;
Jamie Ilesce921362011-02-21 16:43:21 +1100470
Mathias Krauseab827fb32013-10-15 13:49:33 +0200471 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
Jamie Ilesce921362011-02-21 16:43:21 +1100472 goto badkey;
473
Mathias Krauseab827fb32013-10-15 13:49:33 +0200474 if (keys.enckeylen > AES_MAX_KEY_SIZE)
Jamie Ilesce921362011-02-21 16:43:21 +1100475 goto badkey;
476
Mathias Krauseab827fb32013-10-15 13:49:33 +0200477 if (keys.authkeylen > sizeof(ctx->hash_ctx))
Jamie Ilesce921362011-02-21 16:43:21 +1100478 goto badkey;
479
Herbert Xuc1359492015-07-30 17:53:19 +0800480 memcpy(ctx->cipher_key, keys.enckey, keys.enckeylen);
481 ctx->cipher_key_len = keys.enckeylen;
Jamie Ilesce921362011-02-21 16:43:21 +1100482
Mathias Krauseab827fb32013-10-15 13:49:33 +0200483 memcpy(ctx->hash_ctx, keys.authkey, keys.authkeylen);
484 ctx->hash_key_len = keys.authkeylen;
Jamie Ilesce921362011-02-21 16:43:21 +1100485
Tudor-Dan Ambarusa664b4b2018-03-23 12:42:22 +0200486 memzero_explicit(&keys, sizeof(keys));
Jamie Ilesce921362011-02-21 16:43:21 +1100487 return 0;
488
489badkey:
Tudor-Dan Ambarusa664b4b2018-03-23 12:42:22 +0200490 memzero_explicit(&keys, sizeof(keys));
Jamie Ilesce921362011-02-21 16:43:21 +1100491 return -EINVAL;
492}
493
494static int spacc_aead_setauthsize(struct crypto_aead *tfm,
495 unsigned int authsize)
496{
497 struct spacc_aead_ctx *ctx = crypto_tfm_ctx(crypto_aead_tfm(tfm));
498
Herbert Xuc1359492015-07-30 17:53:19 +0800499 return crypto_aead_setauthsize(ctx->sw_cipher, authsize);
Jamie Ilesce921362011-02-21 16:43:21 +1100500}
501
502/*
503 * Check if an AEAD request requires a fallback operation. Some requests can't
504 * be completed in hardware because the hardware may not support certain key
505 * sizes. In these cases we need to complete the request in software.
506 */
Herbert Xuc1359492015-07-30 17:53:19 +0800507static int spacc_aead_need_fallback(struct aead_request *aead_req)
Jamie Ilesce921362011-02-21 16:43:21 +1100508{
Herbert Xuc1359492015-07-30 17:53:19 +0800509 struct crypto_aead *aead = crypto_aead_reqtfm(aead_req);
510 struct aead_alg *alg = crypto_aead_alg(aead);
511 struct spacc_aead *spacc_alg = to_spacc_aead(alg);
512 struct spacc_aead_ctx *ctx = crypto_aead_ctx(aead);
Jamie Ilesce921362011-02-21 16:43:21 +1100513
Jamie Ilesce921362011-02-21 16:43:21 +1100514 /*
515 * If we have a non-supported key-length, then we need to do a
516 * software fallback.
517 */
518 if ((spacc_alg->ctrl_default & SPACC_CRYPTO_ALG_MASK) ==
519 SPA_CTRL_CIPH_ALG_AES &&
520 ctx->cipher_key_len != AES_KEYSIZE_128 &&
521 ctx->cipher_key_len != AES_KEYSIZE_256)
522 return 1;
523
524 return 0;
525}
526
527static int spacc_aead_do_fallback(struct aead_request *req, unsigned alg_type,
528 bool is_encrypt)
529{
530 struct crypto_tfm *old_tfm = crypto_aead_tfm(crypto_aead_reqtfm(req));
531 struct spacc_aead_ctx *ctx = crypto_tfm_ctx(old_tfm);
Herbert Xuc1359492015-07-30 17:53:19 +0800532 struct aead_request *subreq = aead_request_ctx(req);
Jamie Ilesce921362011-02-21 16:43:21 +1100533
Herbert Xuc1359492015-07-30 17:53:19 +0800534 aead_request_set_tfm(subreq, ctx->sw_cipher);
535 aead_request_set_callback(subreq, req->base.flags,
536 req->base.complete, req->base.data);
537 aead_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
538 req->iv);
539 aead_request_set_ad(subreq, req->assoclen);
Jamie Ilesce921362011-02-21 16:43:21 +1100540
Herbert Xuc1359492015-07-30 17:53:19 +0800541 return is_encrypt ? crypto_aead_encrypt(subreq) :
542 crypto_aead_decrypt(subreq);
Jamie Ilesce921362011-02-21 16:43:21 +1100543}
544
545static void spacc_aead_complete(struct spacc_req *req)
546{
547 spacc_aead_free_ddts(req);
548 req->req->complete(req->req, req->result);
549}
550
551static int spacc_aead_submit(struct spacc_req *req)
552{
Jamie Ilesce921362011-02-21 16:43:21 +1100553 struct aead_request *aead_req =
554 container_of(req->req, struct aead_request, base);
Herbert Xuc1359492015-07-30 17:53:19 +0800555 struct crypto_aead *aead = crypto_aead_reqtfm(aead_req);
556 unsigned int authsize = crypto_aead_authsize(aead);
557 struct spacc_aead_ctx *ctx = crypto_aead_ctx(aead);
558 struct aead_alg *alg = crypto_aead_alg(aead);
559 struct spacc_aead *spacc_alg = to_spacc_aead(alg);
560 struct spacc_engine *engine = ctx->generic.engine;
561 u32 ctrl, proc_len, assoc_len;
Jamie Ilesce921362011-02-21 16:43:21 +1100562
563 req->result = -EINPROGRESS;
564 req->ctx_id = spacc_load_ctx(&ctx->generic, ctx->cipher_key,
Herbert Xuc1359492015-07-30 17:53:19 +0800565 ctx->cipher_key_len, aead_req->iv, crypto_aead_ivsize(aead),
Jamie Ilesce921362011-02-21 16:43:21 +1100566 ctx->hash_ctx, ctx->hash_key_len);
567
568 /* Set the source and destination DDT pointers. */
569 writel(req->src_addr, engine->regs + SPA_SRC_PTR_REG_OFFSET);
570 writel(req->dst_addr, engine->regs + SPA_DST_PTR_REG_OFFSET);
571 writel(0, engine->regs + SPA_OFFSET_REG_OFFSET);
572
573 assoc_len = aead_req->assoclen;
574 proc_len = aead_req->cryptlen + assoc_len;
575
576 /*
Jamie Ilesce921362011-02-21 16:43:21 +1100577 * If we are decrypting, we need to take the length of the ICV out of
578 * the processing length.
579 */
580 if (!req->is_encrypt)
Herbert Xuc1359492015-07-30 17:53:19 +0800581 proc_len -= authsize;
Jamie Ilesce921362011-02-21 16:43:21 +1100582
583 writel(proc_len, engine->regs + SPA_PROC_LEN_REG_OFFSET);
584 writel(assoc_len, engine->regs + SPA_AAD_LEN_REG_OFFSET);
Herbert Xuc1359492015-07-30 17:53:19 +0800585 writel(authsize, engine->regs + SPA_ICV_LEN_REG_OFFSET);
Jamie Ilesce921362011-02-21 16:43:21 +1100586 writel(0, engine->regs + SPA_ICV_OFFSET_REG_OFFSET);
587 writel(0, engine->regs + SPA_AUX_INFO_REG_OFFSET);
588
589 ctrl = spacc_alg->ctrl_default | (req->ctx_id << SPA_CTRL_CTX_IDX) |
590 (1 << SPA_CTRL_ICV_APPEND);
591 if (req->is_encrypt)
592 ctrl |= (1 << SPA_CTRL_ENCRYPT_IDX) | (1 << SPA_CTRL_AAD_COPY);
593 else
594 ctrl |= (1 << SPA_CTRL_KEY_EXP);
595
596 mod_timer(&engine->packet_timeout, jiffies + PACKET_TIMEOUT);
597
598 writel(ctrl, engine->regs + SPA_CTRL_REG_OFFSET);
599
600 return -EINPROGRESS;
601}
602
Jamie Iles40bfc142011-03-27 10:48:29 +0800603static int spacc_req_submit(struct spacc_req *req);
604
605static void spacc_push(struct spacc_engine *engine)
606{
607 struct spacc_req *req;
608
609 while (!list_empty(&engine->pending) &&
610 engine->in_flight + 1 <= engine->fifo_sz) {
611
612 ++engine->in_flight;
613 req = list_first_entry(&engine->pending, struct spacc_req,
614 list);
615 list_move_tail(&req->list, &engine->in_progress);
616
617 req->result = spacc_req_submit(req);
618 }
619}
620
Jamie Ilesce921362011-02-21 16:43:21 +1100621/*
622 * Setup an AEAD request for processing. This will configure the engine, load
623 * the context and then start the packet processing.
Jamie Ilesce921362011-02-21 16:43:21 +1100624 */
Herbert Xuc1359492015-07-30 17:53:19 +0800625static int spacc_aead_setup(struct aead_request *req,
Jamie Ilesce921362011-02-21 16:43:21 +1100626 unsigned alg_type, bool is_encrypt)
627{
Herbert Xuc1359492015-07-30 17:53:19 +0800628 struct crypto_aead *aead = crypto_aead_reqtfm(req);
629 struct aead_alg *alg = crypto_aead_alg(aead);
630 struct spacc_engine *engine = to_spacc_aead(alg)->engine;
Jamie Ilesce921362011-02-21 16:43:21 +1100631 struct spacc_req *dev_req = aead_request_ctx(req);
Herbert Xuc1359492015-07-30 17:53:19 +0800632 int err;
Jamie Ilesce921362011-02-21 16:43:21 +1100633 unsigned long flags;
Jamie Ilesce921362011-02-21 16:43:21 +1100634
Jamie Ilesce921362011-02-21 16:43:21 +1100635 dev_req->req = &req->base;
636 dev_req->is_encrypt = is_encrypt;
637 dev_req->result = -EBUSY;
638 dev_req->engine = engine;
639 dev_req->complete = spacc_aead_complete;
640
Herbert Xuc1359492015-07-30 17:53:19 +0800641 if (unlikely(spacc_aead_need_fallback(req) ||
642 ((err = spacc_aead_make_ddts(req)) == -E2BIG)))
Jamie Ilesce921362011-02-21 16:43:21 +1100643 return spacc_aead_do_fallback(req, alg_type, is_encrypt);
644
Herbert Xuc1359492015-07-30 17:53:19 +0800645 if (err)
646 goto out;
Jamie Ilesce921362011-02-21 16:43:21 +1100647
648 err = -EINPROGRESS;
649 spin_lock_irqsave(&engine->hw_lock, flags);
Jamie Iles40bfc142011-03-27 10:48:29 +0800650 if (unlikely(spacc_fifo_cmd_full(engine)) ||
651 engine->in_flight + 1 > engine->fifo_sz) {
Jamie Ilesce921362011-02-21 16:43:21 +1100652 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
653 err = -EBUSY;
654 spin_unlock_irqrestore(&engine->hw_lock, flags);
655 goto out_free_ddts;
656 }
657 list_add_tail(&dev_req->list, &engine->pending);
658 } else {
Jamie Iles40bfc142011-03-27 10:48:29 +0800659 list_add_tail(&dev_req->list, &engine->pending);
660 spacc_push(engine);
Jamie Ilesce921362011-02-21 16:43:21 +1100661 }
662 spin_unlock_irqrestore(&engine->hw_lock, flags);
663
664 goto out;
665
666out_free_ddts:
667 spacc_aead_free_ddts(dev_req);
668out:
669 return err;
670}
671
672static int spacc_aead_encrypt(struct aead_request *req)
673{
674 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Herbert Xuc1359492015-07-30 17:53:19 +0800675 struct spacc_aead *alg = to_spacc_aead(crypto_aead_alg(aead));
Jamie Ilesce921362011-02-21 16:43:21 +1100676
Herbert Xuc1359492015-07-30 17:53:19 +0800677 return spacc_aead_setup(req, alg->type, 1);
Jamie Ilesce921362011-02-21 16:43:21 +1100678}
679
680static int spacc_aead_decrypt(struct aead_request *req)
681{
682 struct crypto_aead *aead = crypto_aead_reqtfm(req);
Herbert Xuc1359492015-07-30 17:53:19 +0800683 struct spacc_aead *alg = to_spacc_aead(crypto_aead_alg(aead));
Jamie Ilesce921362011-02-21 16:43:21 +1100684
Herbert Xuc1359492015-07-30 17:53:19 +0800685 return spacc_aead_setup(req, alg->type, 0);
Jamie Ilesce921362011-02-21 16:43:21 +1100686}
687
688/*
689 * Initialise a new AEAD context. This is responsible for allocating the
690 * fallback cipher and initialising the context.
691 */
Herbert Xuc1359492015-07-30 17:53:19 +0800692static int spacc_aead_cra_init(struct crypto_aead *tfm)
Jamie Ilesce921362011-02-21 16:43:21 +1100693{
Herbert Xuc1359492015-07-30 17:53:19 +0800694 struct spacc_aead_ctx *ctx = crypto_aead_ctx(tfm);
695 struct aead_alg *alg = crypto_aead_alg(tfm);
696 struct spacc_aead *spacc_alg = to_spacc_aead(alg);
Jamie Ilesce921362011-02-21 16:43:21 +1100697 struct spacc_engine *engine = spacc_alg->engine;
698
699 ctx->generic.flags = spacc_alg->type;
700 ctx->generic.engine = engine;
Herbert Xuc1359492015-07-30 17:53:19 +0800701 ctx->sw_cipher = crypto_alloc_aead(alg->base.cra_name, 0,
Jamie Ilesce921362011-02-21 16:43:21 +1100702 CRYPTO_ALG_NEED_FALLBACK);
Herbert Xuc1359492015-07-30 17:53:19 +0800703 if (IS_ERR(ctx->sw_cipher))
704 return PTR_ERR(ctx->sw_cipher);
Jamie Ilesce921362011-02-21 16:43:21 +1100705 ctx->generic.key_offs = spacc_alg->key_offs;
706 ctx->generic.iv_offs = spacc_alg->iv_offs;
707
Herbert Xuc1359492015-07-30 17:53:19 +0800708 crypto_aead_set_reqsize(
709 tfm,
710 max(sizeof(struct spacc_req),
711 sizeof(struct aead_request) +
712 crypto_aead_reqsize(ctx->sw_cipher)));
Jamie Ilesce921362011-02-21 16:43:21 +1100713
714 return 0;
715}
716
717/*
718 * Destructor for an AEAD context. This is called when the transform is freed
719 * and must free the fallback cipher.
720 */
Herbert Xuc1359492015-07-30 17:53:19 +0800721static void spacc_aead_cra_exit(struct crypto_aead *tfm)
Jamie Ilesce921362011-02-21 16:43:21 +1100722{
Herbert Xuc1359492015-07-30 17:53:19 +0800723 struct spacc_aead_ctx *ctx = crypto_aead_ctx(tfm);
Jamie Ilesce921362011-02-21 16:43:21 +1100724
Herbert Xuc1359492015-07-30 17:53:19 +0800725 crypto_free_aead(ctx->sw_cipher);
Jamie Ilesce921362011-02-21 16:43:21 +1100726}
727
728/*
729 * Set the DES key for a block cipher transform. This also performs weak key
730 * checking if the transform has requested it.
731 */
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100732static int spacc_des_setkey(struct crypto_skcipher *cipher, const u8 *key,
Jamie Ilesce921362011-02-21 16:43:21 +1100733 unsigned int len)
734{
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100735 struct spacc_ablk_ctx *ctx = crypto_skcipher_ctx(cipher);
Ard Biesheuvel0157fb22019-08-15 12:01:00 +0300736 int err;
Jamie Ilesce921362011-02-21 16:43:21 +1100737
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100738 err = verify_skcipher_des_key(cipher, key);
Ard Biesheuvel0157fb22019-08-15 12:01:00 +0300739 if (err)
740 return err;
Jamie Ilesce921362011-02-21 16:43:21 +1100741
742 memcpy(ctx->key, key, len);
743 ctx->key_len = len;
744
745 return 0;
746}
747
748/*
Herbert Xuaa113da2019-04-11 16:51:15 +0800749 * Set the 3DES key for a block cipher transform. This also performs weak key
750 * checking if the transform has requested it.
751 */
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100752static int spacc_des3_setkey(struct crypto_skcipher *cipher, const u8 *key,
Herbert Xuaa113da2019-04-11 16:51:15 +0800753 unsigned int len)
754{
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100755 struct spacc_ablk_ctx *ctx = crypto_skcipher_ctx(cipher);
Herbert Xuaa113da2019-04-11 16:51:15 +0800756 int err;
757
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100758 err = verify_skcipher_des3_key(cipher, key);
Ard Biesheuvel0157fb22019-08-15 12:01:00 +0300759 if (err)
Herbert Xuaa113da2019-04-11 16:51:15 +0800760 return err;
Herbert Xuaa113da2019-04-11 16:51:15 +0800761
762 memcpy(ctx->key, key, len);
763 ctx->key_len = len;
764
765 return 0;
766}
767
768/*
Jamie Ilesce921362011-02-21 16:43:21 +1100769 * Set the key for an AES block cipher. Some key lengths are not supported in
770 * hardware so this must also check whether a fallback is needed.
771 */
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100772static int spacc_aes_setkey(struct crypto_skcipher *cipher, const u8 *key,
Jamie Ilesce921362011-02-21 16:43:21 +1100773 unsigned int len)
774{
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100775 struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher);
Jamie Ilesce921362011-02-21 16:43:21 +1100776 struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm);
777 int err = 0;
778
Eric Biggers674f3682019-12-30 21:19:36 -0600779 if (len > AES_MAX_KEY_SIZE)
Jamie Ilesce921362011-02-21 16:43:21 +1100780 return -EINVAL;
Jamie Ilesce921362011-02-21 16:43:21 +1100781
782 /*
783 * IPSec engine only supports 128 and 256 bit AES keys. If we get a
784 * request for any other size (192 bits) then we need to do a software
785 * fallback.
786 */
Herbert Xu1eb60ff2016-06-29 18:04:03 +0800787 if (len != AES_KEYSIZE_128 && len != AES_KEYSIZE_256) {
788 if (!ctx->sw_cipher)
789 return -EINVAL;
790
Jamie Ilesce921362011-02-21 16:43:21 +1100791 /*
792 * Set the fallback transform to use the same request flags as
793 * the hardware transform.
794 */
Kees Cook6adfbd62018-09-18 19:10:59 -0700795 crypto_sync_skcipher_clear_flags(ctx->sw_cipher,
Herbert Xu1eb60ff2016-06-29 18:04:03 +0800796 CRYPTO_TFM_REQ_MASK);
Kees Cook6adfbd62018-09-18 19:10:59 -0700797 crypto_sync_skcipher_set_flags(ctx->sw_cipher,
Herbert Xu1eb60ff2016-06-29 18:04:03 +0800798 cipher->base.crt_flags &
799 CRYPTO_TFM_REQ_MASK);
Jamie Ilesce921362011-02-21 16:43:21 +1100800
Kees Cook6adfbd62018-09-18 19:10:59 -0700801 err = crypto_sync_skcipher_setkey(ctx->sw_cipher, key, len);
Jamie Ilesce921362011-02-21 16:43:21 +1100802 if (err)
803 goto sw_setkey_failed;
Herbert Xu1eb60ff2016-06-29 18:04:03 +0800804 }
Jamie Ilesce921362011-02-21 16:43:21 +1100805
806 memcpy(ctx->key, key, len);
807 ctx->key_len = len;
808
809sw_setkey_failed:
Jamie Ilesce921362011-02-21 16:43:21 +1100810 return err;
811}
812
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100813static int spacc_kasumi_f8_setkey(struct crypto_skcipher *cipher,
Jamie Ilesce921362011-02-21 16:43:21 +1100814 const u8 *key, unsigned int len)
815{
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100816 struct crypto_tfm *tfm = crypto_skcipher_tfm(cipher);
Jamie Ilesce921362011-02-21 16:43:21 +1100817 struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(tfm);
818 int err = 0;
819
820 if (len > AES_MAX_KEY_SIZE) {
Jamie Ilesce921362011-02-21 16:43:21 +1100821 err = -EINVAL;
822 goto out;
823 }
824
825 memcpy(ctx->key, key, len);
826 ctx->key_len = len;
827
828out:
829 return err;
830}
831
832static int spacc_ablk_need_fallback(struct spacc_req *req)
833{
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100834 struct skcipher_request *ablk_req = skcipher_request_cast(req->req);
835 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(ablk_req);
836 struct spacc_alg *spacc_alg = to_spacc_skcipher(crypto_skcipher_alg(tfm));
Jamie Ilesce921362011-02-21 16:43:21 +1100837 struct spacc_ablk_ctx *ctx;
Jamie Ilesce921362011-02-21 16:43:21 +1100838
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100839 ctx = crypto_skcipher_ctx(tfm);
Jamie Ilesce921362011-02-21 16:43:21 +1100840
841 return (spacc_alg->ctrl_default & SPACC_CRYPTO_ALG_MASK) ==
842 SPA_CTRL_CIPH_ALG_AES &&
843 ctx->key_len != AES_KEYSIZE_128 &&
844 ctx->key_len != AES_KEYSIZE_256;
845}
846
847static void spacc_ablk_complete(struct spacc_req *req)
848{
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100849 struct skcipher_request *ablk_req = skcipher_request_cast(req->req);
Jamie Ilesce921362011-02-21 16:43:21 +1100850
851 if (ablk_req->src != ablk_req->dst) {
852 spacc_free_ddt(req, req->src_ddt, req->src_addr, ablk_req->src,
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100853 ablk_req->cryptlen, DMA_TO_DEVICE);
Jamie Ilesce921362011-02-21 16:43:21 +1100854 spacc_free_ddt(req, req->dst_ddt, req->dst_addr, ablk_req->dst,
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100855 ablk_req->cryptlen, DMA_FROM_DEVICE);
Jamie Ilesce921362011-02-21 16:43:21 +1100856 } else
857 spacc_free_ddt(req, req->dst_ddt, req->dst_addr, ablk_req->dst,
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100858 ablk_req->cryptlen, DMA_BIDIRECTIONAL);
Jamie Ilesce921362011-02-21 16:43:21 +1100859
860 req->req->complete(req->req, req->result);
861}
862
863static int spacc_ablk_submit(struct spacc_req *req)
864{
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100865 struct skcipher_request *ablk_req = skcipher_request_cast(req->req);
866 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(ablk_req);
867 struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
868 struct spacc_alg *spacc_alg = to_spacc_skcipher(alg);
869 struct spacc_ablk_ctx *ctx = crypto_skcipher_ctx(tfm);
Jamie Ilesce921362011-02-21 16:43:21 +1100870 struct spacc_engine *engine = ctx->generic.engine;
871 u32 ctrl;
872
873 req->ctx_id = spacc_load_ctx(&ctx->generic, ctx->key,
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100874 ctx->key_len, ablk_req->iv, alg->ivsize,
Jamie Ilesce921362011-02-21 16:43:21 +1100875 NULL, 0);
876
877 writel(req->src_addr, engine->regs + SPA_SRC_PTR_REG_OFFSET);
878 writel(req->dst_addr, engine->regs + SPA_DST_PTR_REG_OFFSET);
879 writel(0, engine->regs + SPA_OFFSET_REG_OFFSET);
880
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100881 writel(ablk_req->cryptlen, engine->regs + SPA_PROC_LEN_REG_OFFSET);
Jamie Ilesce921362011-02-21 16:43:21 +1100882 writel(0, engine->regs + SPA_ICV_OFFSET_REG_OFFSET);
883 writel(0, engine->regs + SPA_AUX_INFO_REG_OFFSET);
884 writel(0, engine->regs + SPA_AAD_LEN_REG_OFFSET);
885
886 ctrl = spacc_alg->ctrl_default | (req->ctx_id << SPA_CTRL_CTX_IDX) |
887 (req->is_encrypt ? (1 << SPA_CTRL_ENCRYPT_IDX) :
888 (1 << SPA_CTRL_KEY_EXP));
889
890 mod_timer(&engine->packet_timeout, jiffies + PACKET_TIMEOUT);
891
892 writel(ctrl, engine->regs + SPA_CTRL_REG_OFFSET);
893
894 return -EINPROGRESS;
895}
896
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100897static int spacc_ablk_do_fallback(struct skcipher_request *req,
Jamie Ilesce921362011-02-21 16:43:21 +1100898 unsigned alg_type, bool is_encrypt)
899{
900 struct crypto_tfm *old_tfm =
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100901 crypto_skcipher_tfm(crypto_skcipher_reqtfm(req));
Jamie Ilesce921362011-02-21 16:43:21 +1100902 struct spacc_ablk_ctx *ctx = crypto_tfm_ctx(old_tfm);
Kees Cook6adfbd62018-09-18 19:10:59 -0700903 SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->sw_cipher);
Jamie Ilesce921362011-02-21 16:43:21 +1100904 int err;
905
Jamie Ilesce921362011-02-21 16:43:21 +1100906 /*
907 * Change the request to use the software fallback transform, and once
908 * the ciphering has completed, put the old transform back into the
909 * request.
910 */
Kees Cook6adfbd62018-09-18 19:10:59 -0700911 skcipher_request_set_sync_tfm(subreq, ctx->sw_cipher);
Herbert Xu1eb60ff2016-06-29 18:04:03 +0800912 skcipher_request_set_callback(subreq, req->base.flags, NULL, NULL);
913 skcipher_request_set_crypt(subreq, req->src, req->dst,
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100914 req->cryptlen, req->iv);
Herbert Xu1eb60ff2016-06-29 18:04:03 +0800915 err = is_encrypt ? crypto_skcipher_encrypt(subreq) :
916 crypto_skcipher_decrypt(subreq);
917 skcipher_request_zero(subreq);
Jamie Ilesce921362011-02-21 16:43:21 +1100918
919 return err;
920}
921
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100922static int spacc_ablk_setup(struct skcipher_request *req, unsigned alg_type,
Jamie Ilesce921362011-02-21 16:43:21 +1100923 bool is_encrypt)
924{
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100925 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
926 struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
927 struct spacc_engine *engine = to_spacc_skcipher(alg)->engine;
928 struct spacc_req *dev_req = skcipher_request_ctx(req);
Jamie Ilesce921362011-02-21 16:43:21 +1100929 unsigned long flags;
930 int err = -ENOMEM;
931
932 dev_req->req = &req->base;
933 dev_req->is_encrypt = is_encrypt;
934 dev_req->engine = engine;
935 dev_req->complete = spacc_ablk_complete;
936 dev_req->result = -EINPROGRESS;
937
938 if (unlikely(spacc_ablk_need_fallback(dev_req)))
939 return spacc_ablk_do_fallback(req, alg_type, is_encrypt);
940
941 /*
942 * Create the DDT's for the engine. If we share the same source and
943 * destination then we can optimize by reusing the DDT's.
944 */
945 if (req->src != req->dst) {
946 dev_req->src_ddt = spacc_sg_to_ddt(engine, req->src,
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100947 req->cryptlen, DMA_TO_DEVICE, &dev_req->src_addr);
Jamie Ilesce921362011-02-21 16:43:21 +1100948 if (!dev_req->src_ddt)
949 goto out;
950
951 dev_req->dst_ddt = spacc_sg_to_ddt(engine, req->dst,
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100952 req->cryptlen, DMA_FROM_DEVICE, &dev_req->dst_addr);
Jamie Ilesce921362011-02-21 16:43:21 +1100953 if (!dev_req->dst_ddt)
954 goto out_free_src;
955 } else {
956 dev_req->dst_ddt = spacc_sg_to_ddt(engine, req->dst,
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100957 req->cryptlen, DMA_BIDIRECTIONAL, &dev_req->dst_addr);
Jamie Ilesce921362011-02-21 16:43:21 +1100958 if (!dev_req->dst_ddt)
959 goto out;
960
961 dev_req->src_ddt = NULL;
962 dev_req->src_addr = dev_req->dst_addr;
963 }
964
965 err = -EINPROGRESS;
966 spin_lock_irqsave(&engine->hw_lock, flags);
967 /*
968 * Check if the engine will accept the operation now. If it won't then
969 * we either stick it on the end of a pending list if we can backlog,
970 * or bailout with an error if not.
971 */
Jamie Iles40bfc142011-03-27 10:48:29 +0800972 if (unlikely(spacc_fifo_cmd_full(engine)) ||
973 engine->in_flight + 1 > engine->fifo_sz) {
Jamie Ilesce921362011-02-21 16:43:21 +1100974 if (!(req->base.flags & CRYPTO_TFM_REQ_MAY_BACKLOG)) {
975 err = -EBUSY;
976 spin_unlock_irqrestore(&engine->hw_lock, flags);
977 goto out_free_ddts;
978 }
979 list_add_tail(&dev_req->list, &engine->pending);
980 } else {
Jamie Iles40bfc142011-03-27 10:48:29 +0800981 list_add_tail(&dev_req->list, &engine->pending);
982 spacc_push(engine);
Jamie Ilesce921362011-02-21 16:43:21 +1100983 }
984 spin_unlock_irqrestore(&engine->hw_lock, flags);
985
986 goto out;
987
988out_free_ddts:
989 spacc_free_ddt(dev_req, dev_req->dst_ddt, dev_req->dst_addr, req->dst,
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100990 req->cryptlen, req->src == req->dst ?
Jamie Ilesce921362011-02-21 16:43:21 +1100991 DMA_BIDIRECTIONAL : DMA_FROM_DEVICE);
992out_free_src:
993 if (req->src != req->dst)
994 spacc_free_ddt(dev_req, dev_req->src_ddt, dev_req->src_addr,
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +0100995 req->src, req->cryptlen, DMA_TO_DEVICE);
Jamie Ilesce921362011-02-21 16:43:21 +1100996out:
997 return err;
998}
999
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001000static int spacc_ablk_init_tfm(struct crypto_skcipher *tfm)
Jamie Ilesce921362011-02-21 16:43:21 +11001001{
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001002 struct spacc_ablk_ctx *ctx = crypto_skcipher_ctx(tfm);
1003 struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
1004 struct spacc_alg *spacc_alg = to_spacc_skcipher(alg);
Jamie Ilesce921362011-02-21 16:43:21 +11001005 struct spacc_engine *engine = spacc_alg->engine;
1006
1007 ctx->generic.flags = spacc_alg->type;
1008 ctx->generic.engine = engine;
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001009 if (alg->base.cra_flags & CRYPTO_ALG_NEED_FALLBACK) {
Kees Cook6adfbd62018-09-18 19:10:59 -07001010 ctx->sw_cipher = crypto_alloc_sync_skcipher(
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001011 alg->base.cra_name, 0, CRYPTO_ALG_NEED_FALLBACK);
Jamie Ilesce921362011-02-21 16:43:21 +11001012 if (IS_ERR(ctx->sw_cipher)) {
1013 dev_warn(engine->dev, "failed to allocate fallback for %s\n",
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001014 alg->base.cra_name);
Herbert Xu1eb60ff2016-06-29 18:04:03 +08001015 return PTR_ERR(ctx->sw_cipher);
Jamie Ilesce921362011-02-21 16:43:21 +11001016 }
1017 }
1018 ctx->generic.key_offs = spacc_alg->key_offs;
1019 ctx->generic.iv_offs = spacc_alg->iv_offs;
1020
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001021 crypto_skcipher_set_reqsize(tfm, sizeof(struct spacc_req));
Jamie Ilesce921362011-02-21 16:43:21 +11001022
1023 return 0;
1024}
1025
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001026static void spacc_ablk_exit_tfm(struct crypto_skcipher *tfm)
Jamie Ilesce921362011-02-21 16:43:21 +11001027{
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001028 struct spacc_ablk_ctx *ctx = crypto_skcipher_ctx(tfm);
Jamie Ilesce921362011-02-21 16:43:21 +11001029
Kees Cook6adfbd62018-09-18 19:10:59 -07001030 crypto_free_sync_skcipher(ctx->sw_cipher);
Jamie Ilesce921362011-02-21 16:43:21 +11001031}
1032
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001033static int spacc_ablk_encrypt(struct skcipher_request *req)
Jamie Ilesce921362011-02-21 16:43:21 +11001034{
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001035 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
1036 struct skcipher_alg *alg = crypto_skcipher_alg(cipher);
1037 struct spacc_alg *spacc_alg = to_spacc_skcipher(alg);
Jamie Ilesce921362011-02-21 16:43:21 +11001038
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001039 return spacc_ablk_setup(req, spacc_alg->type, 1);
Jamie Ilesce921362011-02-21 16:43:21 +11001040}
1041
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001042static int spacc_ablk_decrypt(struct skcipher_request *req)
Jamie Ilesce921362011-02-21 16:43:21 +11001043{
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001044 struct crypto_skcipher *cipher = crypto_skcipher_reqtfm(req);
1045 struct skcipher_alg *alg = crypto_skcipher_alg(cipher);
1046 struct spacc_alg *spacc_alg = to_spacc_skcipher(alg);
Jamie Ilesce921362011-02-21 16:43:21 +11001047
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001048 return spacc_ablk_setup(req, spacc_alg->type, 0);
Jamie Ilesce921362011-02-21 16:43:21 +11001049}
1050
1051static inline int spacc_fifo_stat_empty(struct spacc_engine *engine)
1052{
1053 return readl(engine->regs + SPA_FIFO_STAT_REG_OFFSET) &
1054 SPA_FIFO_STAT_EMPTY;
1055}
1056
1057static void spacc_process_done(struct spacc_engine *engine)
1058{
1059 struct spacc_req *req;
1060 unsigned long flags;
1061
1062 spin_lock_irqsave(&engine->hw_lock, flags);
1063
1064 while (!spacc_fifo_stat_empty(engine)) {
1065 req = list_first_entry(&engine->in_progress, struct spacc_req,
1066 list);
1067 list_move_tail(&req->list, &engine->completed);
Jamie Iles40bfc142011-03-27 10:48:29 +08001068 --engine->in_flight;
Jamie Ilesce921362011-02-21 16:43:21 +11001069
1070 /* POP the status register. */
1071 writel(~0, engine->regs + SPA_STAT_POP_REG_OFFSET);
1072 req->result = (readl(engine->regs + SPA_STATUS_REG_OFFSET) &
1073 SPA_STATUS_RES_CODE_MASK) >> SPA_STATUS_RES_CODE_OFFSET;
1074
1075 /*
1076 * Convert the SPAcc error status into the standard POSIX error
1077 * codes.
1078 */
1079 if (unlikely(req->result)) {
1080 switch (req->result) {
1081 case SPA_STATUS_ICV_FAIL:
1082 req->result = -EBADMSG;
1083 break;
1084
1085 case SPA_STATUS_MEMORY_ERROR:
1086 dev_warn(engine->dev,
1087 "memory error triggered\n");
1088 req->result = -EFAULT;
1089 break;
1090
1091 case SPA_STATUS_BLOCK_ERROR:
1092 dev_warn(engine->dev,
1093 "block error triggered\n");
1094 req->result = -EIO;
1095 break;
1096 }
1097 }
1098 }
1099
1100 tasklet_schedule(&engine->complete);
1101
1102 spin_unlock_irqrestore(&engine->hw_lock, flags);
1103}
1104
1105static irqreturn_t spacc_spacc_irq(int irq, void *dev)
1106{
1107 struct spacc_engine *engine = (struct spacc_engine *)dev;
1108 u32 spacc_irq_stat = readl(engine->regs + SPA_IRQ_STAT_REG_OFFSET);
1109
1110 writel(spacc_irq_stat, engine->regs + SPA_IRQ_STAT_REG_OFFSET);
1111 spacc_process_done(engine);
1112
1113 return IRQ_HANDLED;
1114}
1115
Kees Cookf34d8d52017-10-19 16:43:19 -07001116static void spacc_packet_timeout(struct timer_list *t)
Jamie Ilesce921362011-02-21 16:43:21 +11001117{
Kees Cookf34d8d52017-10-19 16:43:19 -07001118 struct spacc_engine *engine = from_timer(engine, t, packet_timeout);
Jamie Ilesce921362011-02-21 16:43:21 +11001119
1120 spacc_process_done(engine);
1121}
1122
1123static int spacc_req_submit(struct spacc_req *req)
1124{
1125 struct crypto_alg *alg = req->req->tfm->__crt_alg;
1126
1127 if (CRYPTO_ALG_TYPE_AEAD == (CRYPTO_ALG_TYPE_MASK & alg->cra_flags))
1128 return spacc_aead_submit(req);
1129 else
1130 return spacc_ablk_submit(req);
1131}
1132
1133static void spacc_spacc_complete(unsigned long data)
1134{
1135 struct spacc_engine *engine = (struct spacc_engine *)data;
1136 struct spacc_req *req, *tmp;
1137 unsigned long flags;
Jamie Ilesce921362011-02-21 16:43:21 +11001138 LIST_HEAD(completed);
1139
1140 spin_lock_irqsave(&engine->hw_lock, flags);
Jamie Iles40bfc142011-03-27 10:48:29 +08001141
Jamie Ilesce921362011-02-21 16:43:21 +11001142 list_splice_init(&engine->completed, &completed);
Jamie Iles40bfc142011-03-27 10:48:29 +08001143 spacc_push(engine);
Jamie Ilesce921362011-02-21 16:43:21 +11001144 if (engine->in_flight)
1145 mod_timer(&engine->packet_timeout, jiffies + PACKET_TIMEOUT);
1146
1147 spin_unlock_irqrestore(&engine->hw_lock, flags);
Jamie Iles40bfc142011-03-27 10:48:29 +08001148
1149 list_for_each_entry_safe(req, tmp, &completed, list) {
Jamie Iles40bfc142011-03-27 10:48:29 +08001150 list_del(&req->list);
Jamie Ilesb64dc042011-08-02 11:29:06 +01001151 req->complete(req);
Jamie Iles40bfc142011-03-27 10:48:29 +08001152 }
Jamie Ilesce921362011-02-21 16:43:21 +11001153}
1154
1155#ifdef CONFIG_PM
1156static int spacc_suspend(struct device *dev)
1157{
Wolfram Sang8ce31dc2018-04-19 16:05:36 +02001158 struct spacc_engine *engine = dev_get_drvdata(dev);
Jamie Ilesce921362011-02-21 16:43:21 +11001159
1160 /*
1161 * We only support standby mode. All we have to do is gate the clock to
1162 * the spacc. The hardware will preserve state until we turn it back
1163 * on again.
1164 */
1165 clk_disable(engine->clk);
1166
1167 return 0;
1168}
1169
1170static int spacc_resume(struct device *dev)
1171{
Wolfram Sang8ce31dc2018-04-19 16:05:36 +02001172 struct spacc_engine *engine = dev_get_drvdata(dev);
Jamie Ilesce921362011-02-21 16:43:21 +11001173
1174 return clk_enable(engine->clk);
1175}
1176
1177static const struct dev_pm_ops spacc_pm_ops = {
1178 .suspend = spacc_suspend,
1179 .resume = spacc_resume,
1180};
1181#endif /* CONFIG_PM */
1182
1183static inline struct spacc_engine *spacc_dev_to_engine(struct device *dev)
1184{
Kefeng Wang0e4c6102019-04-23 15:49:57 +08001185 return dev ? dev_get_drvdata(dev) : NULL;
Jamie Ilesce921362011-02-21 16:43:21 +11001186}
1187
1188static ssize_t spacc_stat_irq_thresh_show(struct device *dev,
1189 struct device_attribute *attr,
1190 char *buf)
1191{
1192 struct spacc_engine *engine = spacc_dev_to_engine(dev);
1193
1194 return snprintf(buf, PAGE_SIZE, "%u\n", engine->stat_irq_thresh);
1195}
1196
1197static ssize_t spacc_stat_irq_thresh_store(struct device *dev,
1198 struct device_attribute *attr,
1199 const char *buf, size_t len)
1200{
1201 struct spacc_engine *engine = spacc_dev_to_engine(dev);
1202 unsigned long thresh;
1203
Jingoo Han61e2d1a2013-06-01 16:05:57 +09001204 if (kstrtoul(buf, 0, &thresh))
Jamie Ilesce921362011-02-21 16:43:21 +11001205 return -EINVAL;
1206
1207 thresh = clamp(thresh, 1UL, engine->fifo_sz - 1);
1208
1209 engine->stat_irq_thresh = thresh;
1210 writel(engine->stat_irq_thresh << SPA_IRQ_CTRL_STAT_CNT_OFFSET,
1211 engine->regs + SPA_IRQ_CTRL_REG_OFFSET);
1212
1213 return len;
1214}
1215static DEVICE_ATTR(stat_irq_thresh, 0644, spacc_stat_irq_thresh_show,
1216 spacc_stat_irq_thresh_store);
1217
1218static struct spacc_alg ipsec_engine_algs[] = {
1219 {
1220 .ctrl_default = SPA_CTRL_CIPH_ALG_AES | SPA_CTRL_CIPH_MODE_CBC,
1221 .key_offs = 0,
1222 .iv_offs = AES_MAX_KEY_SIZE,
1223 .alg = {
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001224 .base.cra_name = "cbc(aes)",
1225 .base.cra_driver_name = "cbc-aes-picoxcell",
1226 .base.cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1227 .base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
1228 CRYPTO_ALG_ASYNC |
1229 CRYPTO_ALG_NEED_FALLBACK,
1230 .base.cra_blocksize = AES_BLOCK_SIZE,
1231 .base.cra_ctxsize = sizeof(struct spacc_ablk_ctx),
1232 .base.cra_module = THIS_MODULE,
1233
1234 .setkey = spacc_aes_setkey,
1235 .encrypt = spacc_ablk_encrypt,
1236 .decrypt = spacc_ablk_decrypt,
1237 .min_keysize = AES_MIN_KEY_SIZE,
1238 .max_keysize = AES_MAX_KEY_SIZE,
1239 .ivsize = AES_BLOCK_SIZE,
1240 .init = spacc_ablk_init_tfm,
1241 .exit = spacc_ablk_exit_tfm,
Jamie Ilesce921362011-02-21 16:43:21 +11001242 },
1243 },
1244 {
1245 .key_offs = 0,
1246 .iv_offs = AES_MAX_KEY_SIZE,
1247 .ctrl_default = SPA_CTRL_CIPH_ALG_AES | SPA_CTRL_CIPH_MODE_ECB,
1248 .alg = {
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001249 .base.cra_name = "ecb(aes)",
1250 .base.cra_driver_name = "ecb-aes-picoxcell",
1251 .base.cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1252 .base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
1253 CRYPTO_ALG_ASYNC |
1254 CRYPTO_ALG_NEED_FALLBACK,
1255 .base.cra_blocksize = AES_BLOCK_SIZE,
1256 .base.cra_ctxsize = sizeof(struct spacc_ablk_ctx),
1257 .base.cra_module = THIS_MODULE,
1258
1259 .setkey = spacc_aes_setkey,
1260 .encrypt = spacc_ablk_encrypt,
1261 .decrypt = spacc_ablk_decrypt,
1262 .min_keysize = AES_MIN_KEY_SIZE,
1263 .max_keysize = AES_MAX_KEY_SIZE,
1264 .init = spacc_ablk_init_tfm,
1265 .exit = spacc_ablk_exit_tfm,
Jamie Ilesce921362011-02-21 16:43:21 +11001266 },
1267 },
1268 {
1269 .key_offs = DES_BLOCK_SIZE,
1270 .iv_offs = 0,
1271 .ctrl_default = SPA_CTRL_CIPH_ALG_DES | SPA_CTRL_CIPH_MODE_CBC,
1272 .alg = {
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001273 .base.cra_name = "cbc(des)",
1274 .base.cra_driver_name = "cbc-des-picoxcell",
1275 .base.cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1276 .base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
1277 CRYPTO_ALG_ASYNC,
1278 .base.cra_blocksize = DES_BLOCK_SIZE,
1279 .base.cra_ctxsize = sizeof(struct spacc_ablk_ctx),
1280 .base.cra_module = THIS_MODULE,
1281
1282 .setkey = spacc_des_setkey,
1283 .encrypt = spacc_ablk_encrypt,
1284 .decrypt = spacc_ablk_decrypt,
1285 .min_keysize = DES_KEY_SIZE,
1286 .max_keysize = DES_KEY_SIZE,
1287 .ivsize = DES_BLOCK_SIZE,
1288 .init = spacc_ablk_init_tfm,
1289 .exit = spacc_ablk_exit_tfm,
Jamie Ilesce921362011-02-21 16:43:21 +11001290 },
1291 },
1292 {
1293 .key_offs = DES_BLOCK_SIZE,
1294 .iv_offs = 0,
1295 .ctrl_default = SPA_CTRL_CIPH_ALG_DES | SPA_CTRL_CIPH_MODE_ECB,
1296 .alg = {
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001297 .base.cra_name = "ecb(des)",
1298 .base.cra_driver_name = "ecb-des-picoxcell",
1299 .base.cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1300 .base.cra_flags = CRYPTO_ALG_KERN_DRIVER_ONLY |
1301 CRYPTO_ALG_ASYNC,
1302 .base.cra_blocksize = DES_BLOCK_SIZE,
1303 .base.cra_ctxsize = sizeof(struct spacc_ablk_ctx),
1304 .base.cra_module = THIS_MODULE,
1305
1306 .setkey = spacc_des_setkey,
1307 .encrypt = spacc_ablk_encrypt,
1308 .decrypt = spacc_ablk_decrypt,
1309 .min_keysize = DES_KEY_SIZE,
1310 .max_keysize = DES_KEY_SIZE,
1311 .init = spacc_ablk_init_tfm,
1312 .exit = spacc_ablk_exit_tfm,
Jamie Ilesce921362011-02-21 16:43:21 +11001313 },
1314 },
1315 {
1316 .key_offs = DES_BLOCK_SIZE,
1317 .iv_offs = 0,
1318 .ctrl_default = SPA_CTRL_CIPH_ALG_DES | SPA_CTRL_CIPH_MODE_CBC,
1319 .alg = {
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001320 .base.cra_name = "cbc(des3_ede)",
1321 .base.cra_driver_name = "cbc-des3-ede-picoxcell",
1322 .base.cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1323 .base.cra_flags = CRYPTO_ALG_ASYNC |
1324 CRYPTO_ALG_KERN_DRIVER_ONLY,
1325 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
1326 .base.cra_ctxsize = sizeof(struct spacc_ablk_ctx),
1327 .base.cra_module = THIS_MODULE,
1328
1329 .setkey = spacc_des3_setkey,
1330 .encrypt = spacc_ablk_encrypt,
1331 .decrypt = spacc_ablk_decrypt,
1332 .min_keysize = DES3_EDE_KEY_SIZE,
1333 .max_keysize = DES3_EDE_KEY_SIZE,
1334 .ivsize = DES3_EDE_BLOCK_SIZE,
1335 .init = spacc_ablk_init_tfm,
1336 .exit = spacc_ablk_exit_tfm,
Jamie Ilesce921362011-02-21 16:43:21 +11001337 },
1338 },
1339 {
1340 .key_offs = DES_BLOCK_SIZE,
1341 .iv_offs = 0,
1342 .ctrl_default = SPA_CTRL_CIPH_ALG_DES | SPA_CTRL_CIPH_MODE_ECB,
1343 .alg = {
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001344 .base.cra_name = "ecb(des3_ede)",
1345 .base.cra_driver_name = "ecb-des3-ede-picoxcell",
1346 .base.cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1347 .base.cra_flags = CRYPTO_ALG_ASYNC |
1348 CRYPTO_ALG_KERN_DRIVER_ONLY,
1349 .base.cra_blocksize = DES3_EDE_BLOCK_SIZE,
1350 .base.cra_ctxsize = sizeof(struct spacc_ablk_ctx),
1351 .base.cra_module = THIS_MODULE,
1352
1353 .setkey = spacc_des3_setkey,
1354 .encrypt = spacc_ablk_encrypt,
1355 .decrypt = spacc_ablk_decrypt,
1356 .min_keysize = DES3_EDE_KEY_SIZE,
1357 .max_keysize = DES3_EDE_KEY_SIZE,
1358 .init = spacc_ablk_init_tfm,
1359 .exit = spacc_ablk_exit_tfm,
Jamie Ilesce921362011-02-21 16:43:21 +11001360 },
1361 },
Herbert Xuc1359492015-07-30 17:53:19 +08001362};
1363
1364static struct spacc_aead ipsec_engine_aeads[] = {
Jamie Ilesce921362011-02-21 16:43:21 +11001365 {
Herbert Xuc1359492015-07-30 17:53:19 +08001366 .ctrl_default = SPA_CTRL_CIPH_ALG_AES |
1367 SPA_CTRL_CIPH_MODE_CBC |
1368 SPA_CTRL_HASH_ALG_SHA |
1369 SPA_CTRL_HASH_MODE_HMAC,
Jamie Ilesce921362011-02-21 16:43:21 +11001370 .key_offs = 0,
1371 .iv_offs = AES_MAX_KEY_SIZE,
1372 .alg = {
Herbert Xuc1359492015-07-30 17:53:19 +08001373 .base = {
1374 .cra_name = "authenc(hmac(sha1),cbc(aes))",
1375 .cra_driver_name = "authenc-hmac-sha1-"
1376 "cbc-aes-picoxcell",
1377 .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1378 .cra_flags = CRYPTO_ALG_ASYNC |
1379 CRYPTO_ALG_NEED_FALLBACK |
1380 CRYPTO_ALG_KERN_DRIVER_ONLY,
1381 .cra_blocksize = AES_BLOCK_SIZE,
1382 .cra_ctxsize = sizeof(struct spacc_aead_ctx),
1383 .cra_module = THIS_MODULE,
Jamie Ilesce921362011-02-21 16:43:21 +11001384 },
Herbert Xuc1359492015-07-30 17:53:19 +08001385 .setkey = spacc_aead_setkey,
1386 .setauthsize = spacc_aead_setauthsize,
1387 .encrypt = spacc_aead_encrypt,
1388 .decrypt = spacc_aead_decrypt,
1389 .ivsize = AES_BLOCK_SIZE,
1390 .maxauthsize = SHA1_DIGEST_SIZE,
1391 .init = spacc_aead_cra_init,
1392 .exit = spacc_aead_cra_exit,
Jamie Ilesce921362011-02-21 16:43:21 +11001393 },
1394 },
1395 {
Herbert Xuc1359492015-07-30 17:53:19 +08001396 .ctrl_default = SPA_CTRL_CIPH_ALG_AES |
1397 SPA_CTRL_CIPH_MODE_CBC |
Jamie Ilesce921362011-02-21 16:43:21 +11001398 SPA_CTRL_HASH_ALG_SHA256 |
1399 SPA_CTRL_HASH_MODE_HMAC,
1400 .key_offs = 0,
1401 .iv_offs = AES_MAX_KEY_SIZE,
1402 .alg = {
Herbert Xuc1359492015-07-30 17:53:19 +08001403 .base = {
1404 .cra_name = "authenc(hmac(sha256),cbc(aes))",
1405 .cra_driver_name = "authenc-hmac-sha256-"
1406 "cbc-aes-picoxcell",
1407 .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1408 .cra_flags = CRYPTO_ALG_ASYNC |
1409 CRYPTO_ALG_NEED_FALLBACK |
1410 CRYPTO_ALG_KERN_DRIVER_ONLY,
1411 .cra_blocksize = AES_BLOCK_SIZE,
1412 .cra_ctxsize = sizeof(struct spacc_aead_ctx),
1413 .cra_module = THIS_MODULE,
Jamie Ilesce921362011-02-21 16:43:21 +11001414 },
Herbert Xuc1359492015-07-30 17:53:19 +08001415 .setkey = spacc_aead_setkey,
1416 .setauthsize = spacc_aead_setauthsize,
1417 .encrypt = spacc_aead_encrypt,
1418 .decrypt = spacc_aead_decrypt,
1419 .ivsize = AES_BLOCK_SIZE,
1420 .maxauthsize = SHA256_DIGEST_SIZE,
1421 .init = spacc_aead_cra_init,
1422 .exit = spacc_aead_cra_exit,
Jamie Ilesce921362011-02-21 16:43:21 +11001423 },
1424 },
1425 {
1426 .key_offs = 0,
1427 .iv_offs = AES_MAX_KEY_SIZE,
Herbert Xuc1359492015-07-30 17:53:19 +08001428 .ctrl_default = SPA_CTRL_CIPH_ALG_AES |
1429 SPA_CTRL_CIPH_MODE_CBC |
1430 SPA_CTRL_HASH_ALG_MD5 |
1431 SPA_CTRL_HASH_MODE_HMAC,
Jamie Ilesce921362011-02-21 16:43:21 +11001432 .alg = {
Herbert Xuc1359492015-07-30 17:53:19 +08001433 .base = {
1434 .cra_name = "authenc(hmac(md5),cbc(aes))",
1435 .cra_driver_name = "authenc-hmac-md5-"
1436 "cbc-aes-picoxcell",
1437 .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1438 .cra_flags = CRYPTO_ALG_ASYNC |
1439 CRYPTO_ALG_NEED_FALLBACK |
1440 CRYPTO_ALG_KERN_DRIVER_ONLY,
1441 .cra_blocksize = AES_BLOCK_SIZE,
1442 .cra_ctxsize = sizeof(struct spacc_aead_ctx),
1443 .cra_module = THIS_MODULE,
Jamie Ilesce921362011-02-21 16:43:21 +11001444 },
Herbert Xuc1359492015-07-30 17:53:19 +08001445 .setkey = spacc_aead_setkey,
1446 .setauthsize = spacc_aead_setauthsize,
1447 .encrypt = spacc_aead_encrypt,
1448 .decrypt = spacc_aead_decrypt,
1449 .ivsize = AES_BLOCK_SIZE,
1450 .maxauthsize = MD5_DIGEST_SIZE,
1451 .init = spacc_aead_cra_init,
1452 .exit = spacc_aead_cra_exit,
Jamie Ilesce921362011-02-21 16:43:21 +11001453 },
1454 },
1455 {
1456 .key_offs = DES_BLOCK_SIZE,
1457 .iv_offs = 0,
Herbert Xuc1359492015-07-30 17:53:19 +08001458 .ctrl_default = SPA_CTRL_CIPH_ALG_DES |
1459 SPA_CTRL_CIPH_MODE_CBC |
1460 SPA_CTRL_HASH_ALG_SHA |
1461 SPA_CTRL_HASH_MODE_HMAC,
Jamie Ilesce921362011-02-21 16:43:21 +11001462 .alg = {
Herbert Xuc1359492015-07-30 17:53:19 +08001463 .base = {
1464 .cra_name = "authenc(hmac(sha1),cbc(des3_ede))",
1465 .cra_driver_name = "authenc-hmac-sha1-"
1466 "cbc-3des-picoxcell",
1467 .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1468 .cra_flags = CRYPTO_ALG_ASYNC |
1469 CRYPTO_ALG_NEED_FALLBACK |
1470 CRYPTO_ALG_KERN_DRIVER_ONLY,
1471 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1472 .cra_ctxsize = sizeof(struct spacc_aead_ctx),
1473 .cra_module = THIS_MODULE,
Jamie Ilesce921362011-02-21 16:43:21 +11001474 },
Herbert Xuc1359492015-07-30 17:53:19 +08001475 .setkey = spacc_aead_setkey,
1476 .setauthsize = spacc_aead_setauthsize,
1477 .encrypt = spacc_aead_encrypt,
1478 .decrypt = spacc_aead_decrypt,
1479 .ivsize = DES3_EDE_BLOCK_SIZE,
1480 .maxauthsize = SHA1_DIGEST_SIZE,
1481 .init = spacc_aead_cra_init,
1482 .exit = spacc_aead_cra_exit,
Jamie Ilesce921362011-02-21 16:43:21 +11001483 },
1484 },
1485 {
1486 .key_offs = DES_BLOCK_SIZE,
1487 .iv_offs = 0,
Herbert Xuc1359492015-07-30 17:53:19 +08001488 .ctrl_default = SPA_CTRL_CIPH_ALG_AES |
1489 SPA_CTRL_CIPH_MODE_CBC |
Jamie Ilesce921362011-02-21 16:43:21 +11001490 SPA_CTRL_HASH_ALG_SHA256 |
1491 SPA_CTRL_HASH_MODE_HMAC,
1492 .alg = {
Herbert Xuc1359492015-07-30 17:53:19 +08001493 .base = {
1494 .cra_name = "authenc(hmac(sha256),"
1495 "cbc(des3_ede))",
1496 .cra_driver_name = "authenc-hmac-sha256-"
1497 "cbc-3des-picoxcell",
1498 .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1499 .cra_flags = CRYPTO_ALG_ASYNC |
1500 CRYPTO_ALG_NEED_FALLBACK |
1501 CRYPTO_ALG_KERN_DRIVER_ONLY,
1502 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1503 .cra_ctxsize = sizeof(struct spacc_aead_ctx),
1504 .cra_module = THIS_MODULE,
Jamie Ilesce921362011-02-21 16:43:21 +11001505 },
Herbert Xuc1359492015-07-30 17:53:19 +08001506 .setkey = spacc_aead_setkey,
1507 .setauthsize = spacc_aead_setauthsize,
1508 .encrypt = spacc_aead_encrypt,
1509 .decrypt = spacc_aead_decrypt,
1510 .ivsize = DES3_EDE_BLOCK_SIZE,
1511 .maxauthsize = SHA256_DIGEST_SIZE,
1512 .init = spacc_aead_cra_init,
1513 .exit = spacc_aead_cra_exit,
Jamie Ilesce921362011-02-21 16:43:21 +11001514 },
1515 },
1516 {
1517 .key_offs = DES_BLOCK_SIZE,
1518 .iv_offs = 0,
Herbert Xuc1359492015-07-30 17:53:19 +08001519 .ctrl_default = SPA_CTRL_CIPH_ALG_DES |
1520 SPA_CTRL_CIPH_MODE_CBC |
1521 SPA_CTRL_HASH_ALG_MD5 |
1522 SPA_CTRL_HASH_MODE_HMAC,
Jamie Ilesce921362011-02-21 16:43:21 +11001523 .alg = {
Herbert Xuc1359492015-07-30 17:53:19 +08001524 .base = {
1525 .cra_name = "authenc(hmac(md5),cbc(des3_ede))",
1526 .cra_driver_name = "authenc-hmac-md5-"
1527 "cbc-3des-picoxcell",
1528 .cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1529 .cra_flags = CRYPTO_ALG_ASYNC |
1530 CRYPTO_ALG_NEED_FALLBACK |
1531 CRYPTO_ALG_KERN_DRIVER_ONLY,
1532 .cra_blocksize = DES3_EDE_BLOCK_SIZE,
1533 .cra_ctxsize = sizeof(struct spacc_aead_ctx),
1534 .cra_module = THIS_MODULE,
Jamie Ilesce921362011-02-21 16:43:21 +11001535 },
Herbert Xuc1359492015-07-30 17:53:19 +08001536 .setkey = spacc_aead_setkey,
1537 .setauthsize = spacc_aead_setauthsize,
1538 .encrypt = spacc_aead_encrypt,
1539 .decrypt = spacc_aead_decrypt,
1540 .ivsize = DES3_EDE_BLOCK_SIZE,
1541 .maxauthsize = MD5_DIGEST_SIZE,
1542 .init = spacc_aead_cra_init,
1543 .exit = spacc_aead_cra_exit,
Jamie Ilesce921362011-02-21 16:43:21 +11001544 },
1545 },
1546};
1547
1548static struct spacc_alg l2_engine_algs[] = {
1549 {
1550 .key_offs = 0,
1551 .iv_offs = SPACC_CRYPTO_KASUMI_F8_KEY_LEN,
1552 .ctrl_default = SPA_CTRL_CIPH_ALG_KASUMI |
1553 SPA_CTRL_CIPH_MODE_F8,
1554 .alg = {
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001555 .base.cra_name = "f8(kasumi)",
1556 .base.cra_driver_name = "f8-kasumi-picoxcell",
1557 .base.cra_priority = SPACC_CRYPTO_ALG_PRIORITY,
1558 .base.cra_flags = CRYPTO_ALG_ASYNC |
1559 CRYPTO_ALG_KERN_DRIVER_ONLY,
1560 .base.cra_blocksize = 8,
1561 .base.cra_ctxsize = sizeof(struct spacc_ablk_ctx),
1562 .base.cra_module = THIS_MODULE,
1563
1564 .setkey = spacc_kasumi_f8_setkey,
1565 .encrypt = spacc_ablk_encrypt,
1566 .decrypt = spacc_ablk_decrypt,
1567 .min_keysize = 16,
1568 .max_keysize = 16,
1569 .ivsize = 8,
1570 .init = spacc_ablk_init_tfm,
1571 .exit = spacc_ablk_exit_tfm,
Jamie Ilesce921362011-02-21 16:43:21 +11001572 },
1573 },
1574};
1575
Jamie Iles30343ef2011-08-01 17:25:19 +01001576#ifdef CONFIG_OF
1577static const struct of_device_id spacc_of_id_table[] = {
1578 { .compatible = "picochip,spacc-ipsec" },
1579 { .compatible = "picochip,spacc-l2" },
1580 {}
1581};
Luis de Bethencourtc3abc0f2015-08-28 18:44:03 +02001582MODULE_DEVICE_TABLE(of, spacc_of_id_table);
Jamie Iles30343ef2011-08-01 17:25:19 +01001583#endif /* CONFIG_OF */
1584
Chuhong Yuan7f8c36f2019-12-10 00:21:44 +08001585static void spacc_tasklet_kill(void *data)
1586{
1587 tasklet_kill(data);
1588}
1589
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08001590static int spacc_probe(struct platform_device *pdev)
Jamie Ilesce921362011-02-21 16:43:21 +11001591{
Alexey Khoroshilov2d558072018-01-20 00:53:15 +03001592 int i, err, ret;
YueHaibing9a8e0a52019-08-02 21:28:04 +08001593 struct resource *irq;
Javier Martinez Canillas012ef702017-01-02 14:06:59 -03001594 struct device_node *np = pdev->dev.of_node;
Jamie Ilesce921362011-02-21 16:43:21 +11001595 struct spacc_engine *engine = devm_kzalloc(&pdev->dev, sizeof(*engine),
1596 GFP_KERNEL);
1597 if (!engine)
1598 return -ENOMEM;
1599
Javier Martinez Canillas012ef702017-01-02 14:06:59 -03001600 if (of_device_is_compatible(np, "picochip,spacc-ipsec")) {
Jamie Ilesc3f42002011-08-01 17:25:17 +01001601 engine->max_ctxs = SPACC_CRYPTO_IPSEC_MAX_CTXS;
1602 engine->cipher_pg_sz = SPACC_CRYPTO_IPSEC_CIPHER_PG_SZ;
1603 engine->hash_pg_sz = SPACC_CRYPTO_IPSEC_HASH_PG_SZ;
1604 engine->fifo_sz = SPACC_CRYPTO_IPSEC_FIFO_SZ;
1605 engine->algs = ipsec_engine_algs;
1606 engine->num_algs = ARRAY_SIZE(ipsec_engine_algs);
Herbert Xuc1359492015-07-30 17:53:19 +08001607 engine->aeads = ipsec_engine_aeads;
1608 engine->num_aeads = ARRAY_SIZE(ipsec_engine_aeads);
Javier Martinez Canillas012ef702017-01-02 14:06:59 -03001609 } else if (of_device_is_compatible(np, "picochip,spacc-l2")) {
Jamie Ilesc3f42002011-08-01 17:25:17 +01001610 engine->max_ctxs = SPACC_CRYPTO_L2_MAX_CTXS;
1611 engine->cipher_pg_sz = SPACC_CRYPTO_L2_CIPHER_PG_SZ;
1612 engine->hash_pg_sz = SPACC_CRYPTO_L2_HASH_PG_SZ;
1613 engine->fifo_sz = SPACC_CRYPTO_L2_FIFO_SZ;
1614 engine->algs = l2_engine_algs;
1615 engine->num_algs = ARRAY_SIZE(l2_engine_algs);
1616 } else {
1617 return -EINVAL;
1618 }
1619
1620 engine->name = dev_name(&pdev->dev);
Jamie Ilesce921362011-02-21 16:43:21 +11001621
YueHaibing9a8e0a52019-08-02 21:28:04 +08001622 engine->regs = devm_platform_ioremap_resource(pdev, 0);
Jingoo Han32af1e12014-02-12 13:28:59 +09001623 if (IS_ERR(engine->regs))
1624 return PTR_ERR(engine->regs);
1625
Jamie Ilesce921362011-02-21 16:43:21 +11001626 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Jingoo Han32af1e12014-02-12 13:28:59 +09001627 if (!irq) {
Jamie Ilesce921362011-02-21 16:43:21 +11001628 dev_err(&pdev->dev, "no memory/irq resource for engine\n");
1629 return -ENXIO;
1630 }
1631
Chuhong Yuan7f8c36f2019-12-10 00:21:44 +08001632 tasklet_init(&engine->complete, spacc_spacc_complete,
1633 (unsigned long)engine);
1634
1635 ret = devm_add_action(&pdev->dev, spacc_tasklet_kill,
1636 &engine->complete);
1637 if (ret)
1638 return ret;
1639
Jamie Ilesce921362011-02-21 16:43:21 +11001640 if (devm_request_irq(&pdev->dev, irq->start, spacc_spacc_irq, 0,
1641 engine->name, engine)) {
1642 dev_err(engine->dev, "failed to request IRQ\n");
1643 return -EBUSY;
1644 }
1645
1646 engine->dev = &pdev->dev;
1647 engine->cipher_ctx_base = engine->regs + SPA_CIPH_KEY_BASE_REG_OFFSET;
1648 engine->hash_key_base = engine->regs + SPA_HASH_KEY_BASE_REG_OFFSET;
1649
1650 engine->req_pool = dmam_pool_create(engine->name, engine->dev,
1651 MAX_DDT_LEN * sizeof(struct spacc_ddt), 8, SZ_64K);
1652 if (!engine->req_pool)
1653 return -ENOMEM;
1654
1655 spin_lock_init(&engine->hw_lock);
1656
Jamie Iles4efae8c2011-08-01 17:25:18 +01001657 engine->clk = clk_get(&pdev->dev, "ref");
Jamie Ilesce921362011-02-21 16:43:21 +11001658 if (IS_ERR(engine->clk)) {
1659 dev_info(&pdev->dev, "clk unavailable\n");
Jamie Ilesce921362011-02-21 16:43:21 +11001660 return PTR_ERR(engine->clk);
1661 }
1662
Michael van der Westhuizen1bd2cd62015-06-19 15:55:51 +02001663 if (clk_prepare_enable(engine->clk)) {
1664 dev_info(&pdev->dev, "unable to prepare/enable clk\n");
Alexey Khoroshilov2d558072018-01-20 00:53:15 +03001665 ret = -EIO;
1666 goto err_clk_put;
Jamie Ilesce921362011-02-21 16:43:21 +11001667 }
1668
Alexey Khoroshilov2d558072018-01-20 00:53:15 +03001669 ret = device_create_file(&pdev->dev, &dev_attr_stat_irq_thresh);
1670 if (ret)
1671 goto err_clk_disable;
Jamie Ilesce921362011-02-21 16:43:21 +11001672
1673
1674 /*
1675 * Use an IRQ threshold of 50% as a default. This seems to be a
1676 * reasonable trade off of latency against throughput but can be
1677 * changed at runtime.
1678 */
1679 engine->stat_irq_thresh = (engine->fifo_sz / 2);
1680
1681 /*
1682 * Configure the interrupts. We only use the STAT_CNT interrupt as we
1683 * only submit a new packet for processing when we complete another in
1684 * the queue. This minimizes time spent in the interrupt handler.
1685 */
1686 writel(engine->stat_irq_thresh << SPA_IRQ_CTRL_STAT_CNT_OFFSET,
1687 engine->regs + SPA_IRQ_CTRL_REG_OFFSET);
1688 writel(SPA_IRQ_EN_STAT_EN | SPA_IRQ_EN_GLBL_EN,
1689 engine->regs + SPA_IRQ_EN_REG_OFFSET);
1690
Kees Cookf34d8d52017-10-19 16:43:19 -07001691 timer_setup(&engine->packet_timeout, spacc_packet_timeout, 0);
Jamie Ilesce921362011-02-21 16:43:21 +11001692
1693 INIT_LIST_HEAD(&engine->pending);
1694 INIT_LIST_HEAD(&engine->completed);
1695 INIT_LIST_HEAD(&engine->in_progress);
1696 engine->in_flight = 0;
Jamie Ilesce921362011-02-21 16:43:21 +11001697
1698 platform_set_drvdata(pdev, engine);
1699
Alexey Khoroshilov2d558072018-01-20 00:53:15 +03001700 ret = -EINVAL;
Jamie Ilesce921362011-02-21 16:43:21 +11001701 INIT_LIST_HEAD(&engine->registered_algs);
1702 for (i = 0; i < engine->num_algs; ++i) {
1703 engine->algs[i].engine = engine;
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001704 err = crypto_register_skcipher(&engine->algs[i].alg);
Jamie Ilesce921362011-02-21 16:43:21 +11001705 if (!err) {
1706 list_add_tail(&engine->algs[i].entry,
1707 &engine->registered_algs);
1708 ret = 0;
1709 }
1710 if (err)
1711 dev_err(engine->dev, "failed to register alg \"%s\"\n",
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001712 engine->algs[i].alg.base.cra_name);
Jamie Ilesce921362011-02-21 16:43:21 +11001713 else
1714 dev_dbg(engine->dev, "registered alg \"%s\"\n",
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001715 engine->algs[i].alg.base.cra_name);
Jamie Ilesce921362011-02-21 16:43:21 +11001716 }
1717
Herbert Xuc1359492015-07-30 17:53:19 +08001718 INIT_LIST_HEAD(&engine->registered_aeads);
1719 for (i = 0; i < engine->num_aeads; ++i) {
1720 engine->aeads[i].engine = engine;
Herbert Xuc1359492015-07-30 17:53:19 +08001721 err = crypto_register_aead(&engine->aeads[i].alg);
1722 if (!err) {
1723 list_add_tail(&engine->aeads[i].entry,
1724 &engine->registered_aeads);
1725 ret = 0;
1726 }
1727 if (err)
1728 dev_err(engine->dev, "failed to register alg \"%s\"\n",
1729 engine->aeads[i].alg.base.cra_name);
1730 else
1731 dev_dbg(engine->dev, "registered alg \"%s\"\n",
1732 engine->aeads[i].alg.base.cra_name);
1733 }
1734
Alexey Khoroshilov2d558072018-01-20 00:53:15 +03001735 if (!ret)
1736 return 0;
1737
1738 del_timer_sync(&engine->packet_timeout);
1739 device_remove_file(&pdev->dev, &dev_attr_stat_irq_thresh);
1740err_clk_disable:
1741 clk_disable_unprepare(engine->clk);
1742err_clk_put:
1743 clk_put(engine->clk);
1744
Jamie Ilesce921362011-02-21 16:43:21 +11001745 return ret;
1746}
1747
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08001748static int spacc_remove(struct platform_device *pdev)
Jamie Ilesce921362011-02-21 16:43:21 +11001749{
Herbert Xuc1359492015-07-30 17:53:19 +08001750 struct spacc_aead *aead, *an;
Jamie Ilesce921362011-02-21 16:43:21 +11001751 struct spacc_alg *alg, *next;
1752 struct spacc_engine *engine = platform_get_drvdata(pdev);
1753
1754 del_timer_sync(&engine->packet_timeout);
1755 device_remove_file(&pdev->dev, &dev_attr_stat_irq_thresh);
1756
Herbert Xuc1359492015-07-30 17:53:19 +08001757 list_for_each_entry_safe(aead, an, &engine->registered_aeads, entry) {
1758 list_del(&aead->entry);
1759 crypto_unregister_aead(&aead->alg);
1760 }
1761
Jamie Ilesce921362011-02-21 16:43:21 +11001762 list_for_each_entry_safe(alg, next, &engine->registered_algs, entry) {
1763 list_del(&alg->entry);
Ard Biesheuvelb3cde6b2019-11-09 18:09:44 +01001764 crypto_unregister_skcipher(&alg->alg);
Jamie Ilesce921362011-02-21 16:43:21 +11001765 }
1766
Michael van der Westhuizen1bd2cd62015-06-19 15:55:51 +02001767 clk_disable_unprepare(engine->clk);
Jamie Ilesce921362011-02-21 16:43:21 +11001768 clk_put(engine->clk);
1769
1770 return 0;
1771}
1772
Jamie Ilesc3f42002011-08-01 17:25:17 +01001773static struct platform_driver spacc_driver = {
1774 .probe = spacc_probe,
Greg Kroah-Hartman49cfe4d2012-12-21 13:14:09 -08001775 .remove = spacc_remove,
Jamie Ilesce921362011-02-21 16:43:21 +11001776 .driver = {
Jamie Ilesc3f42002011-08-01 17:25:17 +01001777 .name = "picochip,spacc",
Jamie Ilesce921362011-02-21 16:43:21 +11001778#ifdef CONFIG_PM
1779 .pm = &spacc_pm_ops,
1780#endif /* CONFIG_PM */
Sachin Kamat5cec26e2013-03-14 15:46:58 +05301781 .of_match_table = of_match_ptr(spacc_of_id_table),
Jamie Ilesce921362011-02-21 16:43:21 +11001782 },
1783};
1784
Axel Lin741e8c22011-11-26 21:26:19 +08001785module_platform_driver(spacc_driver);
Jamie Ilesce921362011-02-21 16:43:21 +11001786
1787MODULE_LICENSE("GPL");
1788MODULE_AUTHOR("Jamie Iles");