Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Handle async block request by crypto hardware engine.
0004  *
0005  * Copyright (C) 2016 Linaro, Inc.
0006  *
0007  * Author: Baolin Wang <baolin.wang@linaro.org>
0008  */
0009 
0010 #include <linux/err.h>
0011 #include <linux/delay.h>
0012 #include <linux/device.h>
0013 #include <crypto/engine.h>
0014 #include <uapi/linux/sched/types.h>
0015 #include "internal.h"
0016 
0017 #define CRYPTO_ENGINE_MAX_QLEN 10
0018 
0019 /**
0020  * crypto_finalize_request - finalize one request if the request is done
0021  * @engine: the hardware engine
0022  * @req: the request need to be finalized
0023  * @err: error number
0024  */
0025 static void crypto_finalize_request(struct crypto_engine *engine,
0026                     struct crypto_async_request *req, int err)
0027 {
0028     unsigned long flags;
0029     bool finalize_req = false;
0030     int ret;
0031     struct crypto_engine_ctx *enginectx;
0032 
0033     /*
0034      * If hardware cannot enqueue more requests
0035      * and retry mechanism is not supported
0036      * make sure we are completing the current request
0037      */
0038     if (!engine->retry_support) {
0039         spin_lock_irqsave(&engine->queue_lock, flags);
0040         if (engine->cur_req == req) {
0041             finalize_req = true;
0042             engine->cur_req = NULL;
0043         }
0044         spin_unlock_irqrestore(&engine->queue_lock, flags);
0045     }
0046 
0047     if (finalize_req || engine->retry_support) {
0048         enginectx = crypto_tfm_ctx(req->tfm);
0049         if (enginectx->op.prepare_request &&
0050             enginectx->op.unprepare_request) {
0051             ret = enginectx->op.unprepare_request(engine, req);
0052             if (ret)
0053                 dev_err(engine->dev, "failed to unprepare request\n");
0054         }
0055     }
0056     lockdep_assert_in_softirq();
0057     req->complete(req, err);
0058 
0059     kthread_queue_work(engine->kworker, &engine->pump_requests);
0060 }
0061 
0062 /**
0063  * crypto_pump_requests - dequeue one request from engine queue to process
0064  * @engine: the hardware engine
0065  * @in_kthread: true if we are in the context of the request pump thread
0066  *
0067  * This function checks if there is any request in the engine queue that
0068  * needs processing and if so call out to the driver to initialize hardware
0069  * and handle each request.
0070  */
0071 static void crypto_pump_requests(struct crypto_engine *engine,
0072                  bool in_kthread)
0073 {
0074     struct crypto_async_request *async_req, *backlog;
0075     unsigned long flags;
0076     bool was_busy = false;
0077     int ret;
0078     struct crypto_engine_ctx *enginectx;
0079 
0080     spin_lock_irqsave(&engine->queue_lock, flags);
0081 
0082     /* Make sure we are not already running a request */
0083     if (!engine->retry_support && engine->cur_req)
0084         goto out;
0085 
0086     /* If another context is idling then defer */
0087     if (engine->idling) {
0088         kthread_queue_work(engine->kworker, &engine->pump_requests);
0089         goto out;
0090     }
0091 
0092     /* Check if the engine queue is idle */
0093     if (!crypto_queue_len(&engine->queue) || !engine->running) {
0094         if (!engine->busy)
0095             goto out;
0096 
0097         /* Only do teardown in the thread */
0098         if (!in_kthread) {
0099             kthread_queue_work(engine->kworker,
0100                        &engine->pump_requests);
0101             goto out;
0102         }
0103 
0104         engine->busy = false;
0105         engine->idling = true;
0106         spin_unlock_irqrestore(&engine->queue_lock, flags);
0107 
0108         if (engine->unprepare_crypt_hardware &&
0109             engine->unprepare_crypt_hardware(engine))
0110             dev_err(engine->dev, "failed to unprepare crypt hardware\n");
0111 
0112         spin_lock_irqsave(&engine->queue_lock, flags);
0113         engine->idling = false;
0114         goto out;
0115     }
0116 
0117 start_request:
0118     /* Get the fist request from the engine queue to handle */
0119     backlog = crypto_get_backlog(&engine->queue);
0120     async_req = crypto_dequeue_request(&engine->queue);
0121     if (!async_req)
0122         goto out;
0123 
0124     /*
0125      * If hardware doesn't support the retry mechanism,
0126      * keep track of the request we are processing now.
0127      * We'll need it on completion (crypto_finalize_request).
0128      */
0129     if (!engine->retry_support)
0130         engine->cur_req = async_req;
0131 
0132     if (backlog)
0133         backlog->complete(backlog, -EINPROGRESS);
0134 
0135     if (engine->busy)
0136         was_busy = true;
0137     else
0138         engine->busy = true;
0139 
0140     spin_unlock_irqrestore(&engine->queue_lock, flags);
0141 
0142     /* Until here we get the request need to be encrypted successfully */
0143     if (!was_busy && engine->prepare_crypt_hardware) {
0144         ret = engine->prepare_crypt_hardware(engine);
0145         if (ret) {
0146             dev_err(engine->dev, "failed to prepare crypt hardware\n");
0147             goto req_err_2;
0148         }
0149     }
0150 
0151     enginectx = crypto_tfm_ctx(async_req->tfm);
0152 
0153     if (enginectx->op.prepare_request) {
0154         ret = enginectx->op.prepare_request(engine, async_req);
0155         if (ret) {
0156             dev_err(engine->dev, "failed to prepare request: %d\n",
0157                 ret);
0158             goto req_err_2;
0159         }
0160     }
0161     if (!enginectx->op.do_one_request) {
0162         dev_err(engine->dev, "failed to do request\n");
0163         ret = -EINVAL;
0164         goto req_err_1;
0165     }
0166 
0167     ret = enginectx->op.do_one_request(engine, async_req);
0168 
0169     /* Request unsuccessfully executed by hardware */
0170     if (ret < 0) {
0171         /*
0172          * If hardware queue is full (-ENOSPC), requeue request
0173          * regardless of backlog flag.
0174          * Otherwise, unprepare and complete the request.
0175          */
0176         if (!engine->retry_support ||
0177             (ret != -ENOSPC)) {
0178             dev_err(engine->dev,
0179                 "Failed to do one request from queue: %d\n",
0180                 ret);
0181             goto req_err_1;
0182         }
0183         /*
0184          * If retry mechanism is supported,
0185          * unprepare current request and
0186          * enqueue it back into crypto-engine queue.
0187          */
0188         if (enginectx->op.unprepare_request) {
0189             ret = enginectx->op.unprepare_request(engine,
0190                                   async_req);
0191             if (ret)
0192                 dev_err(engine->dev,
0193                     "failed to unprepare request\n");
0194         }
0195         spin_lock_irqsave(&engine->queue_lock, flags);
0196         /*
0197          * If hardware was unable to execute request, enqueue it
0198          * back in front of crypto-engine queue, to keep the order
0199          * of requests.
0200          */
0201         crypto_enqueue_request_head(&engine->queue, async_req);
0202 
0203         kthread_queue_work(engine->kworker, &engine->pump_requests);
0204         goto out;
0205     }
0206 
0207     goto retry;
0208 
0209 req_err_1:
0210     if (enginectx->op.unprepare_request) {
0211         ret = enginectx->op.unprepare_request(engine, async_req);
0212         if (ret)
0213             dev_err(engine->dev, "failed to unprepare request\n");
0214     }
0215 
0216 req_err_2:
0217     async_req->complete(async_req, ret);
0218 
0219 retry:
0220     /* If retry mechanism is supported, send new requests to engine */
0221     if (engine->retry_support) {
0222         spin_lock_irqsave(&engine->queue_lock, flags);
0223         goto start_request;
0224     }
0225     return;
0226 
0227 out:
0228     spin_unlock_irqrestore(&engine->queue_lock, flags);
0229 
0230     /*
0231      * Batch requests is possible only if
0232      * hardware can enqueue multiple requests
0233      */
0234     if (engine->do_batch_requests) {
0235         ret = engine->do_batch_requests(engine);
0236         if (ret)
0237             dev_err(engine->dev, "failed to do batch requests: %d\n",
0238                 ret);
0239     }
0240 
0241     return;
0242 }
0243 
0244 static void crypto_pump_work(struct kthread_work *work)
0245 {
0246     struct crypto_engine *engine =
0247         container_of(work, struct crypto_engine, pump_requests);
0248 
0249     crypto_pump_requests(engine, true);
0250 }
0251 
0252 /**
0253  * crypto_transfer_request - transfer the new request into the engine queue
0254  * @engine: the hardware engine
0255  * @req: the request need to be listed into the engine queue
0256  * @need_pump: indicates whether queue the pump of request to kthread_work
0257  */
0258 static int crypto_transfer_request(struct crypto_engine *engine,
0259                    struct crypto_async_request *req,
0260                    bool need_pump)
0261 {
0262     unsigned long flags;
0263     int ret;
0264 
0265     spin_lock_irqsave(&engine->queue_lock, flags);
0266 
0267     if (!engine->running) {
0268         spin_unlock_irqrestore(&engine->queue_lock, flags);
0269         return -ESHUTDOWN;
0270     }
0271 
0272     ret = crypto_enqueue_request(&engine->queue, req);
0273 
0274     if (!engine->busy && need_pump)
0275         kthread_queue_work(engine->kworker, &engine->pump_requests);
0276 
0277     spin_unlock_irqrestore(&engine->queue_lock, flags);
0278     return ret;
0279 }
0280 
0281 /**
0282  * crypto_transfer_request_to_engine - transfer one request to list
0283  * into the engine queue
0284  * @engine: the hardware engine
0285  * @req: the request need to be listed into the engine queue
0286  */
0287 static int crypto_transfer_request_to_engine(struct crypto_engine *engine,
0288                          struct crypto_async_request *req)
0289 {
0290     return crypto_transfer_request(engine, req, true);
0291 }
0292 
0293 /**
0294  * crypto_transfer_aead_request_to_engine - transfer one aead_request
0295  * to list into the engine queue
0296  * @engine: the hardware engine
0297  * @req: the request need to be listed into the engine queue
0298  */
0299 int crypto_transfer_aead_request_to_engine(struct crypto_engine *engine,
0300                        struct aead_request *req)
0301 {
0302     return crypto_transfer_request_to_engine(engine, &req->base);
0303 }
0304 EXPORT_SYMBOL_GPL(crypto_transfer_aead_request_to_engine);
0305 
0306 /**
0307  * crypto_transfer_akcipher_request_to_engine - transfer one akcipher_request
0308  * to list into the engine queue
0309  * @engine: the hardware engine
0310  * @req: the request need to be listed into the engine queue
0311  */
0312 int crypto_transfer_akcipher_request_to_engine(struct crypto_engine *engine,
0313                            struct akcipher_request *req)
0314 {
0315     return crypto_transfer_request_to_engine(engine, &req->base);
0316 }
0317 EXPORT_SYMBOL_GPL(crypto_transfer_akcipher_request_to_engine);
0318 
0319 /**
0320  * crypto_transfer_hash_request_to_engine - transfer one ahash_request
0321  * to list into the engine queue
0322  * @engine: the hardware engine
0323  * @req: the request need to be listed into the engine queue
0324  */
0325 int crypto_transfer_hash_request_to_engine(struct crypto_engine *engine,
0326                        struct ahash_request *req)
0327 {
0328     return crypto_transfer_request_to_engine(engine, &req->base);
0329 }
0330 EXPORT_SYMBOL_GPL(crypto_transfer_hash_request_to_engine);
0331 
0332 /**
0333  * crypto_transfer_kpp_request_to_engine - transfer one kpp_request to list
0334  * into the engine queue
0335  * @engine: the hardware engine
0336  * @req: the request need to be listed into the engine queue
0337  */
0338 int crypto_transfer_kpp_request_to_engine(struct crypto_engine *engine,
0339                       struct kpp_request *req)
0340 {
0341     return crypto_transfer_request_to_engine(engine, &req->base);
0342 }
0343 EXPORT_SYMBOL_GPL(crypto_transfer_kpp_request_to_engine);
0344 
0345 /**
0346  * crypto_transfer_skcipher_request_to_engine - transfer one skcipher_request
0347  * to list into the engine queue
0348  * @engine: the hardware engine
0349  * @req: the request need to be listed into the engine queue
0350  */
0351 int crypto_transfer_skcipher_request_to_engine(struct crypto_engine *engine,
0352                            struct skcipher_request *req)
0353 {
0354     return crypto_transfer_request_to_engine(engine, &req->base);
0355 }
0356 EXPORT_SYMBOL_GPL(crypto_transfer_skcipher_request_to_engine);
0357 
0358 /**
0359  * crypto_finalize_aead_request - finalize one aead_request if
0360  * the request is done
0361  * @engine: the hardware engine
0362  * @req: the request need to be finalized
0363  * @err: error number
0364  */
0365 void crypto_finalize_aead_request(struct crypto_engine *engine,
0366                   struct aead_request *req, int err)
0367 {
0368     return crypto_finalize_request(engine, &req->base, err);
0369 }
0370 EXPORT_SYMBOL_GPL(crypto_finalize_aead_request);
0371 
0372 /**
0373  * crypto_finalize_akcipher_request - finalize one akcipher_request if
0374  * the request is done
0375  * @engine: the hardware engine
0376  * @req: the request need to be finalized
0377  * @err: error number
0378  */
0379 void crypto_finalize_akcipher_request(struct crypto_engine *engine,
0380                       struct akcipher_request *req, int err)
0381 {
0382     return crypto_finalize_request(engine, &req->base, err);
0383 }
0384 EXPORT_SYMBOL_GPL(crypto_finalize_akcipher_request);
0385 
0386 /**
0387  * crypto_finalize_hash_request - finalize one ahash_request if
0388  * the request is done
0389  * @engine: the hardware engine
0390  * @req: the request need to be finalized
0391  * @err: error number
0392  */
0393 void crypto_finalize_hash_request(struct crypto_engine *engine,
0394                   struct ahash_request *req, int err)
0395 {
0396     return crypto_finalize_request(engine, &req->base, err);
0397 }
0398 EXPORT_SYMBOL_GPL(crypto_finalize_hash_request);
0399 
0400 /**
0401  * crypto_finalize_kpp_request - finalize one kpp_request if the request is done
0402  * @engine: the hardware engine
0403  * @req: the request need to be finalized
0404  * @err: error number
0405  */
0406 void crypto_finalize_kpp_request(struct crypto_engine *engine,
0407                  struct kpp_request *req, int err)
0408 {
0409     return crypto_finalize_request(engine, &req->base, err);
0410 }
0411 EXPORT_SYMBOL_GPL(crypto_finalize_kpp_request);
0412 
0413 /**
0414  * crypto_finalize_skcipher_request - finalize one skcipher_request if
0415  * the request is done
0416  * @engine: the hardware engine
0417  * @req: the request need to be finalized
0418  * @err: error number
0419  */
0420 void crypto_finalize_skcipher_request(struct crypto_engine *engine,
0421                       struct skcipher_request *req, int err)
0422 {
0423     return crypto_finalize_request(engine, &req->base, err);
0424 }
0425 EXPORT_SYMBOL_GPL(crypto_finalize_skcipher_request);
0426 
0427 /**
0428  * crypto_engine_start - start the hardware engine
0429  * @engine: the hardware engine need to be started
0430  *
0431  * Return 0 on success, else on fail.
0432  */
0433 int crypto_engine_start(struct crypto_engine *engine)
0434 {
0435     unsigned long flags;
0436 
0437     spin_lock_irqsave(&engine->queue_lock, flags);
0438 
0439     if (engine->running || engine->busy) {
0440         spin_unlock_irqrestore(&engine->queue_lock, flags);
0441         return -EBUSY;
0442     }
0443 
0444     engine->running = true;
0445     spin_unlock_irqrestore(&engine->queue_lock, flags);
0446 
0447     kthread_queue_work(engine->kworker, &engine->pump_requests);
0448 
0449     return 0;
0450 }
0451 EXPORT_SYMBOL_GPL(crypto_engine_start);
0452 
0453 /**
0454  * crypto_engine_stop - stop the hardware engine
0455  * @engine: the hardware engine need to be stopped
0456  *
0457  * Return 0 on success, else on fail.
0458  */
0459 int crypto_engine_stop(struct crypto_engine *engine)
0460 {
0461     unsigned long flags;
0462     unsigned int limit = 500;
0463     int ret = 0;
0464 
0465     spin_lock_irqsave(&engine->queue_lock, flags);
0466 
0467     /*
0468      * If the engine queue is not empty or the engine is on busy state,
0469      * we need to wait for a while to pump the requests of engine queue.
0470      */
0471     while ((crypto_queue_len(&engine->queue) || engine->busy) && limit--) {
0472         spin_unlock_irqrestore(&engine->queue_lock, flags);
0473         msleep(20);
0474         spin_lock_irqsave(&engine->queue_lock, flags);
0475     }
0476 
0477     if (crypto_queue_len(&engine->queue) || engine->busy)
0478         ret = -EBUSY;
0479     else
0480         engine->running = false;
0481 
0482     spin_unlock_irqrestore(&engine->queue_lock, flags);
0483 
0484     if (ret)
0485         dev_warn(engine->dev, "could not stop engine\n");
0486 
0487     return ret;
0488 }
0489 EXPORT_SYMBOL_GPL(crypto_engine_stop);
0490 
0491 /**
0492  * crypto_engine_alloc_init_and_set - allocate crypto hardware engine structure
0493  * and initialize it by setting the maximum number of entries in the software
0494  * crypto-engine queue.
0495  * @dev: the device attached with one hardware engine
0496  * @retry_support: whether hardware has support for retry mechanism
0497  * @cbk_do_batch: pointer to a callback function to be invoked when executing
0498  *                a batch of requests.
0499  *                This has the form:
0500  *                callback(struct crypto_engine *engine)
0501  *                where:
0502  *                @engine: the crypto engine structure.
0503  * @rt: whether this queue is set to run as a realtime task
0504  * @qlen: maximum size of the crypto-engine queue
0505  *
0506  * This must be called from context that can sleep.
0507  * Return: the crypto engine structure on success, else NULL.
0508  */
0509 struct crypto_engine *crypto_engine_alloc_init_and_set(struct device *dev,
0510                                bool retry_support,
0511                                int (*cbk_do_batch)(struct crypto_engine *engine),
0512                                bool rt, int qlen)
0513 {
0514     struct crypto_engine *engine;
0515 
0516     if (!dev)
0517         return NULL;
0518 
0519     engine = devm_kzalloc(dev, sizeof(*engine), GFP_KERNEL);
0520     if (!engine)
0521         return NULL;
0522 
0523     engine->dev = dev;
0524     engine->rt = rt;
0525     engine->running = false;
0526     engine->busy = false;
0527     engine->idling = false;
0528     engine->retry_support = retry_support;
0529     engine->priv_data = dev;
0530     /*
0531      * Batch requests is possible only if
0532      * hardware has support for retry mechanism.
0533      */
0534     engine->do_batch_requests = retry_support ? cbk_do_batch : NULL;
0535 
0536     snprintf(engine->name, sizeof(engine->name),
0537          "%s-engine", dev_name(dev));
0538 
0539     crypto_init_queue(&engine->queue, qlen);
0540     spin_lock_init(&engine->queue_lock);
0541 
0542     engine->kworker = kthread_create_worker(0, "%s", engine->name);
0543     if (IS_ERR(engine->kworker)) {
0544         dev_err(dev, "failed to create crypto request pump task\n");
0545         return NULL;
0546     }
0547     kthread_init_work(&engine->pump_requests, crypto_pump_work);
0548 
0549     if (engine->rt) {
0550         dev_info(dev, "will run requests pump with realtime priority\n");
0551         sched_set_fifo(engine->kworker->task);
0552     }
0553 
0554     return engine;
0555 }
0556 EXPORT_SYMBOL_GPL(crypto_engine_alloc_init_and_set);
0557 
0558 /**
0559  * crypto_engine_alloc_init - allocate crypto hardware engine structure and
0560  * initialize it.
0561  * @dev: the device attached with one hardware engine
0562  * @rt: whether this queue is set to run as a realtime task
0563  *
0564  * This must be called from context that can sleep.
0565  * Return: the crypto engine structure on success, else NULL.
0566  */
0567 struct crypto_engine *crypto_engine_alloc_init(struct device *dev, bool rt)
0568 {
0569     return crypto_engine_alloc_init_and_set(dev, false, NULL, rt,
0570                         CRYPTO_ENGINE_MAX_QLEN);
0571 }
0572 EXPORT_SYMBOL_GPL(crypto_engine_alloc_init);
0573 
0574 /**
0575  * crypto_engine_exit - free the resources of hardware engine when exit
0576  * @engine: the hardware engine need to be freed
0577  *
0578  * Return 0 for success.
0579  */
0580 int crypto_engine_exit(struct crypto_engine *engine)
0581 {
0582     int ret;
0583 
0584     ret = crypto_engine_stop(engine);
0585     if (ret)
0586         return ret;
0587 
0588     kthread_destroy_worker(engine->kworker);
0589 
0590     return 0;
0591 }
0592 EXPORT_SYMBOL_GPL(crypto_engine_exit);
0593 
0594 MODULE_LICENSE("GPL");
0595 MODULE_DESCRIPTION("Crypto hardware engine framework");