123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466 |
- #include <linux/err.h>
- #include <linux/delay.h>
- #include <crypto/engine.h>
- #include <crypto/internal/hash.h>
- #include "internal.h"
- #define CRYPTO_ENGINE_MAX_QLEN 10
- static void crypto_pump_requests(struct crypto_engine *engine,
- bool in_kthread)
- {
- struct crypto_async_request *async_req, *backlog;
- struct ahash_request *hreq;
- struct ablkcipher_request *breq;
- unsigned long flags;
- bool was_busy = false;
- int ret, rtype;
- spin_lock_irqsave(&engine->queue_lock, flags);
-
- if (engine->cur_req)
- goto out;
-
- if (engine->idling) {
- kthread_queue_work(&engine->kworker, &engine->pump_requests);
- goto out;
- }
-
- if (!crypto_queue_len(&engine->queue) || !engine->running) {
- if (!engine->busy)
- goto out;
-
- if (!in_kthread) {
- kthread_queue_work(&engine->kworker,
- &engine->pump_requests);
- goto out;
- }
- engine->busy = false;
- engine->idling = true;
- spin_unlock_irqrestore(&engine->queue_lock, flags);
- if (engine->unprepare_crypt_hardware &&
- engine->unprepare_crypt_hardware(engine))
- pr_err("failed to unprepare crypt hardware\n");
- spin_lock_irqsave(&engine->queue_lock, flags);
- engine->idling = false;
- goto out;
- }
-
- backlog = crypto_get_backlog(&engine->queue);
- async_req = crypto_dequeue_request(&engine->queue);
- if (!async_req)
- goto out;
- engine->cur_req = async_req;
- if (backlog)
- backlog->complete(backlog, -EINPROGRESS);
- if (engine->busy)
- was_busy = true;
- else
- engine->busy = true;
- spin_unlock_irqrestore(&engine->queue_lock, flags);
- rtype = crypto_tfm_alg_type(engine->cur_req->tfm);
-
- if (!was_busy && engine->prepare_crypt_hardware) {
- ret = engine->prepare_crypt_hardware(engine);
- if (ret) {
- pr_err("failed to prepare crypt hardware\n");
- goto req_err;
- }
- }
- switch (rtype) {
- case CRYPTO_ALG_TYPE_AHASH:
- hreq = ahash_request_cast(engine->cur_req);
- if (engine->prepare_hash_request) {
- ret = engine->prepare_hash_request(engine, hreq);
- if (ret) {
- pr_err("failed to prepare request: %d\n", ret);
- goto req_err;
- }
- engine->cur_req_prepared = true;
- }
- ret = engine->hash_one_request(engine, hreq);
- if (ret) {
- pr_err("failed to hash one request from queue\n");
- goto req_err;
- }
- return;
- case CRYPTO_ALG_TYPE_ABLKCIPHER:
- breq = ablkcipher_request_cast(engine->cur_req);
- if (engine->prepare_cipher_request) {
- ret = engine->prepare_cipher_request(engine, breq);
- if (ret) {
- pr_err("failed to prepare request: %d\n", ret);
- goto req_err;
- }
- engine->cur_req_prepared = true;
- }
- ret = engine->cipher_one_request(engine, breq);
- if (ret) {
- pr_err("failed to cipher one request from queue\n");
- goto req_err;
- }
- return;
- default:
- pr_err("failed to prepare request of unknown type\n");
- return;
- }
- req_err:
- switch (rtype) {
- case CRYPTO_ALG_TYPE_AHASH:
- hreq = ahash_request_cast(engine->cur_req);
- crypto_finalize_hash_request(engine, hreq, ret);
- break;
- case CRYPTO_ALG_TYPE_ABLKCIPHER:
- breq = ablkcipher_request_cast(engine->cur_req);
- crypto_finalize_cipher_request(engine, breq, ret);
- break;
- }
- return;
- out:
- spin_unlock_irqrestore(&engine->queue_lock, flags);
- }
- static void crypto_pump_work(struct kthread_work *work)
- {
- struct crypto_engine *engine =
- container_of(work, struct crypto_engine, pump_requests);
- crypto_pump_requests(engine, true);
- }
- int crypto_transfer_cipher_request(struct crypto_engine *engine,
- struct ablkcipher_request *req,
- bool need_pump)
- {
- unsigned long flags;
- int ret;
- spin_lock_irqsave(&engine->queue_lock, flags);
- if (!engine->running) {
- spin_unlock_irqrestore(&engine->queue_lock, flags);
- return -ESHUTDOWN;
- }
- ret = ablkcipher_enqueue_request(&engine->queue, req);
- if (!engine->busy && need_pump)
- kthread_queue_work(&engine->kworker, &engine->pump_requests);
- spin_unlock_irqrestore(&engine->queue_lock, flags);
- return ret;
- }
- EXPORT_SYMBOL_GPL(crypto_transfer_cipher_request);
- int crypto_transfer_cipher_request_to_engine(struct crypto_engine *engine,
- struct ablkcipher_request *req)
- {
- return crypto_transfer_cipher_request(engine, req, true);
- }
- EXPORT_SYMBOL_GPL(crypto_transfer_cipher_request_to_engine);
- int crypto_transfer_hash_request(struct crypto_engine *engine,
- struct ahash_request *req, bool need_pump)
- {
- unsigned long flags;
- int ret;
- spin_lock_irqsave(&engine->queue_lock, flags);
- if (!engine->running) {
- spin_unlock_irqrestore(&engine->queue_lock, flags);
- return -ESHUTDOWN;
- }
- ret = ahash_enqueue_request(&engine->queue, req);
- if (!engine->busy && need_pump)
- kthread_queue_work(&engine->kworker, &engine->pump_requests);
- spin_unlock_irqrestore(&engine->queue_lock, flags);
- return ret;
- }
- EXPORT_SYMBOL_GPL(crypto_transfer_hash_request);
- int crypto_transfer_hash_request_to_engine(struct crypto_engine *engine,
- struct ahash_request *req)
- {
- return crypto_transfer_hash_request(engine, req, true);
- }
- EXPORT_SYMBOL_GPL(crypto_transfer_hash_request_to_engine);
- void crypto_finalize_cipher_request(struct crypto_engine *engine,
- struct ablkcipher_request *req, int err)
- {
- unsigned long flags;
- bool finalize_cur_req = false;
- int ret;
- spin_lock_irqsave(&engine->queue_lock, flags);
- if (engine->cur_req == &req->base)
- finalize_cur_req = true;
- spin_unlock_irqrestore(&engine->queue_lock, flags);
- if (finalize_cur_req) {
- if (engine->cur_req_prepared &&
- engine->unprepare_cipher_request) {
- ret = engine->unprepare_cipher_request(engine, req);
- if (ret)
- pr_err("failed to unprepare request\n");
- }
- spin_lock_irqsave(&engine->queue_lock, flags);
- engine->cur_req = NULL;
- engine->cur_req_prepared = false;
- spin_unlock_irqrestore(&engine->queue_lock, flags);
- }
- req->base.complete(&req->base, err);
- kthread_queue_work(&engine->kworker, &engine->pump_requests);
- }
- EXPORT_SYMBOL_GPL(crypto_finalize_cipher_request);
- void crypto_finalize_hash_request(struct crypto_engine *engine,
- struct ahash_request *req, int err)
- {
- unsigned long flags;
- bool finalize_cur_req = false;
- int ret;
- spin_lock_irqsave(&engine->queue_lock, flags);
- if (engine->cur_req == &req->base)
- finalize_cur_req = true;
- spin_unlock_irqrestore(&engine->queue_lock, flags);
- if (finalize_cur_req) {
- if (engine->cur_req_prepared &&
- engine->unprepare_hash_request) {
- ret = engine->unprepare_hash_request(engine, req);
- if (ret)
- pr_err("failed to unprepare request\n");
- }
- spin_lock_irqsave(&engine->queue_lock, flags);
- engine->cur_req = NULL;
- engine->cur_req_prepared = false;
- spin_unlock_irqrestore(&engine->queue_lock, flags);
- }
- req->base.complete(&req->base, err);
- kthread_queue_work(&engine->kworker, &engine->pump_requests);
- }
- EXPORT_SYMBOL_GPL(crypto_finalize_hash_request);
- int crypto_engine_start(struct crypto_engine *engine)
- {
- unsigned long flags;
- spin_lock_irqsave(&engine->queue_lock, flags);
- if (engine->running || engine->busy) {
- spin_unlock_irqrestore(&engine->queue_lock, flags);
- return -EBUSY;
- }
- engine->running = true;
- spin_unlock_irqrestore(&engine->queue_lock, flags);
- kthread_queue_work(&engine->kworker, &engine->pump_requests);
- return 0;
- }
- EXPORT_SYMBOL_GPL(crypto_engine_start);
- int crypto_engine_stop(struct crypto_engine *engine)
- {
- unsigned long flags;
- unsigned int limit = 500;
- int ret = 0;
- spin_lock_irqsave(&engine->queue_lock, flags);
-
- while ((crypto_queue_len(&engine->queue) || engine->busy) && limit--) {
- spin_unlock_irqrestore(&engine->queue_lock, flags);
- msleep(20);
- spin_lock_irqsave(&engine->queue_lock, flags);
- }
- if (crypto_queue_len(&engine->queue) || engine->busy)
- ret = -EBUSY;
- else
- engine->running = false;
- spin_unlock_irqrestore(&engine->queue_lock, flags);
- if (ret)
- pr_warn("could not stop engine\n");
- return ret;
- }
- EXPORT_SYMBOL_GPL(crypto_engine_stop);
- struct crypto_engine *crypto_engine_alloc_init(struct device *dev, bool rt)
- {
- struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
- struct crypto_engine *engine;
- if (!dev)
- return NULL;
- engine = devm_kzalloc(dev, sizeof(*engine), GFP_KERNEL);
- if (!engine)
- return NULL;
- engine->rt = rt;
- engine->running = false;
- engine->busy = false;
- engine->idling = false;
- engine->cur_req_prepared = false;
- engine->priv_data = dev;
- snprintf(engine->name, sizeof(engine->name),
- "%s-engine", dev_name(dev));
- crypto_init_queue(&engine->queue, CRYPTO_ENGINE_MAX_QLEN);
- spin_lock_init(&engine->queue_lock);
- kthread_init_worker(&engine->kworker);
- engine->kworker_task = kthread_run(kthread_worker_fn,
- &engine->kworker, "%s",
- engine->name);
- if (IS_ERR(engine->kworker_task)) {
- dev_err(dev, "failed to create crypto request pump task\n");
- return NULL;
- }
- kthread_init_work(&engine->pump_requests, crypto_pump_work);
- if (engine->rt) {
- dev_info(dev, "will run requests pump with realtime priority\n");
- sched_setscheduler(engine->kworker_task, SCHED_FIFO, ¶m);
- }
- return engine;
- }
- EXPORT_SYMBOL_GPL(crypto_engine_alloc_init);
- int crypto_engine_exit(struct crypto_engine *engine)
- {
- int ret;
- ret = crypto_engine_stop(engine);
- if (ret)
- return ret;
- kthread_flush_worker(&engine->kworker);
- kthread_stop(engine->kworker_task);
- return 0;
- }
- EXPORT_SYMBOL_GPL(crypto_engine_exit);
- MODULE_LICENSE("GPL");
- MODULE_DESCRIPTION("Crypto hardware engine framework");
|