1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */ 2*4882a593Smuzhiyun /* 3*4882a593Smuzhiyun * Crypto engine API 4*4882a593Smuzhiyun * 5*4882a593Smuzhiyun * Copyright (c) 2016 Baolin Wang <baolin.wang@linaro.org> 6*4882a593Smuzhiyun */ 7*4882a593Smuzhiyun #ifndef _CRYPTO_ENGINE_H 8*4882a593Smuzhiyun #define _CRYPTO_ENGINE_H 9*4882a593Smuzhiyun 10*4882a593Smuzhiyun #include <linux/crypto.h> 11*4882a593Smuzhiyun #include <linux/list.h> 12*4882a593Smuzhiyun #include <linux/kernel.h> 13*4882a593Smuzhiyun #include <linux/kthread.h> 14*4882a593Smuzhiyun #include <crypto/algapi.h> 15*4882a593Smuzhiyun #include <crypto/aead.h> 16*4882a593Smuzhiyun #include <crypto/akcipher.h> 17*4882a593Smuzhiyun #include <crypto/hash.h> 18*4882a593Smuzhiyun #include <crypto/skcipher.h> 19*4882a593Smuzhiyun 20*4882a593Smuzhiyun #define ENGINE_NAME_LEN 30 21*4882a593Smuzhiyun /* 22*4882a593Smuzhiyun * struct crypto_engine - crypto hardware engine 23*4882a593Smuzhiyun * @name: the engine name 24*4882a593Smuzhiyun * @idling: the engine is entering idle state 25*4882a593Smuzhiyun * @busy: request pump is busy 26*4882a593Smuzhiyun * @running: the engine is on working 27*4882a593Smuzhiyun * @retry_support: indication that the hardware allows re-execution 28*4882a593Smuzhiyun * of a failed backlog request 29*4882a593Smuzhiyun * crypto-engine, in head position to keep order 30*4882a593Smuzhiyun * @list: link with the global crypto engine list 31*4882a593Smuzhiyun * @queue_lock: spinlock to syncronise access to request queue 32*4882a593Smuzhiyun * @queue: the crypto queue of the engine 33*4882a593Smuzhiyun * @rt: whether this queue is set to run as a realtime task 34*4882a593Smuzhiyun * @prepare_crypt_hardware: a request will soon arrive from the queue 35*4882a593Smuzhiyun * so the subsystem requests the driver to prepare the hardware 36*4882a593Smuzhiyun * by issuing this call 37*4882a593Smuzhiyun * @unprepare_crypt_hardware: there are currently no more requests on the 38*4882a593Smuzhiyun * queue so the subsystem notifies the driver that it may relax the 39*4882a593Smuzhiyun * hardware by issuing this call 40*4882a593Smuzhiyun * @do_batch_requests: execute a batch of requests. Depends on multiple 41*4882a593Smuzhiyun * requests support. 42*4882a593Smuzhiyun * @kworker: kthread worker struct for request pump 43*4882a593Smuzhiyun * @pump_requests: work struct for scheduling work to the request pump 44*4882a593Smuzhiyun * @priv_data: the engine private data 45*4882a593Smuzhiyun * @cur_req: the current request which is on processing 46*4882a593Smuzhiyun */ 47*4882a593Smuzhiyun struct crypto_engine { 48*4882a593Smuzhiyun char name[ENGINE_NAME_LEN]; 49*4882a593Smuzhiyun bool idling; 50*4882a593Smuzhiyun bool busy; 51*4882a593Smuzhiyun bool running; 52*4882a593Smuzhiyun 53*4882a593Smuzhiyun bool retry_support; 54*4882a593Smuzhiyun 55*4882a593Smuzhiyun struct list_head list; 56*4882a593Smuzhiyun spinlock_t queue_lock; 57*4882a593Smuzhiyun struct crypto_queue queue; 58*4882a593Smuzhiyun struct device *dev; 59*4882a593Smuzhiyun 60*4882a593Smuzhiyun bool rt; 61*4882a593Smuzhiyun 62*4882a593Smuzhiyun int (*prepare_crypt_hardware)(struct crypto_engine *engine); 63*4882a593Smuzhiyun int (*unprepare_crypt_hardware)(struct crypto_engine *engine); 64*4882a593Smuzhiyun int (*do_batch_requests)(struct crypto_engine *engine); 65*4882a593Smuzhiyun 66*4882a593Smuzhiyun 67*4882a593Smuzhiyun struct kthread_worker *kworker; 68*4882a593Smuzhiyun struct kthread_work pump_requests; 69*4882a593Smuzhiyun 70*4882a593Smuzhiyun void *priv_data; 71*4882a593Smuzhiyun struct crypto_async_request *cur_req; 72*4882a593Smuzhiyun }; 73*4882a593Smuzhiyun 74*4882a593Smuzhiyun /* 75*4882a593Smuzhiyun * struct crypto_engine_op - crypto hardware engine operations 76*4882a593Smuzhiyun * @prepare__request: do some prepare if need before handle the current request 77*4882a593Smuzhiyun * @unprepare_request: undo any work done by prepare_request() 78*4882a593Smuzhiyun * @do_one_request: do encryption for current request 79*4882a593Smuzhiyun */ 80*4882a593Smuzhiyun struct crypto_engine_op { 81*4882a593Smuzhiyun int (*prepare_request)(struct crypto_engine *engine, 82*4882a593Smuzhiyun void *areq); 83*4882a593Smuzhiyun int (*unprepare_request)(struct crypto_engine *engine, 84*4882a593Smuzhiyun void *areq); 85*4882a593Smuzhiyun int (*do_one_request)(struct crypto_engine *engine, 86*4882a593Smuzhiyun void *areq); 87*4882a593Smuzhiyun }; 88*4882a593Smuzhiyun 89*4882a593Smuzhiyun struct crypto_engine_ctx { 90*4882a593Smuzhiyun struct crypto_engine_op op; 91*4882a593Smuzhiyun }; 92*4882a593Smuzhiyun 93*4882a593Smuzhiyun int crypto_transfer_aead_request_to_engine(struct crypto_engine *engine, 94*4882a593Smuzhiyun struct aead_request *req); 95*4882a593Smuzhiyun int crypto_transfer_akcipher_request_to_engine(struct crypto_engine *engine, 96*4882a593Smuzhiyun struct akcipher_request *req); 97*4882a593Smuzhiyun int crypto_transfer_hash_request_to_engine(struct crypto_engine *engine, 98*4882a593Smuzhiyun struct ahash_request *req); 99*4882a593Smuzhiyun int crypto_transfer_skcipher_request_to_engine(struct crypto_engine *engine, 100*4882a593Smuzhiyun struct skcipher_request *req); 101*4882a593Smuzhiyun void crypto_finalize_aead_request(struct crypto_engine *engine, 102*4882a593Smuzhiyun struct aead_request *req, int err); 103*4882a593Smuzhiyun void crypto_finalize_akcipher_request(struct crypto_engine *engine, 104*4882a593Smuzhiyun struct akcipher_request *req, int err); 105*4882a593Smuzhiyun void crypto_finalize_hash_request(struct crypto_engine *engine, 106*4882a593Smuzhiyun struct ahash_request *req, int err); 107*4882a593Smuzhiyun void crypto_finalize_skcipher_request(struct crypto_engine *engine, 108*4882a593Smuzhiyun struct skcipher_request *req, int err); 109*4882a593Smuzhiyun int crypto_engine_start(struct crypto_engine *engine); 110*4882a593Smuzhiyun int crypto_engine_stop(struct crypto_engine *engine); 111*4882a593Smuzhiyun struct crypto_engine *crypto_engine_alloc_init(struct device *dev, bool rt); 112*4882a593Smuzhiyun struct crypto_engine *crypto_engine_alloc_init_and_set(struct device *dev, 113*4882a593Smuzhiyun bool retry_support, 114*4882a593Smuzhiyun int (*cbk_do_batch)(struct crypto_engine *engine), 115*4882a593Smuzhiyun bool rt, int qlen); 116*4882a593Smuzhiyun int crypto_engine_exit(struct crypto_engine *engine); 117*4882a593Smuzhiyun 118*4882a593Smuzhiyun #endif /* _CRYPTO_ENGINE_H */ 119