| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Handle async block request by crypto hardware engine. |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * Copyright (C) 2016 Linaro, Inc. |
|---|
| 5 | 6 | * |
|---|
| 6 | 7 | * Author: Baolin Wang <baolin.wang@linaro.org> |
|---|
| 7 | | - * |
|---|
| 8 | | - * This program is free software; you can redistribute it and/or modify it |
|---|
| 9 | | - * under the terms of the GNU General Public License as published by the Free |
|---|
| 10 | | - * Software Foundation; either version 2 of the License, or (at your option) |
|---|
| 11 | | - * any later version. |
|---|
| 12 | | - * |
|---|
| 13 | 8 | */ |
|---|
| 14 | 9 | |
|---|
| 15 | 10 | #include <linux/err.h> |
|---|
| 16 | 11 | #include <linux/delay.h> |
|---|
| 12 | +#include <linux/device.h> |
|---|
| 17 | 13 | #include <crypto/engine.h> |
|---|
| 18 | 14 | #include <uapi/linux/sched/types.h> |
|---|
| 19 | 15 | #include "internal.h" |
|---|
| .. | .. |
|---|
| 27 | 23 | * @err: error number |
|---|
| 28 | 24 | */ |
|---|
| 29 | 25 | static void crypto_finalize_request(struct crypto_engine *engine, |
|---|
| 30 | | - struct crypto_async_request *req, int err) |
|---|
| 26 | + struct crypto_async_request *req, int err) |
|---|
| 31 | 27 | { |
|---|
| 32 | 28 | unsigned long flags; |
|---|
| 33 | | - bool finalize_cur_req = false; |
|---|
| 29 | + bool finalize_req = false; |
|---|
| 34 | 30 | int ret; |
|---|
| 35 | 31 | struct crypto_engine_ctx *enginectx; |
|---|
| 36 | 32 | |
|---|
| 37 | | - spin_lock_irqsave(&engine->queue_lock, flags); |
|---|
| 38 | | - if (engine->cur_req == req) |
|---|
| 39 | | - finalize_cur_req = true; |
|---|
| 40 | | - spin_unlock_irqrestore(&engine->queue_lock, flags); |
|---|
| 33 | + /* |
|---|
| 34 | + * If hardware cannot enqueue more requests |
|---|
| 35 | + * and retry mechanism is not supported |
|---|
| 36 | + * make sure we are completing the current request |
|---|
| 37 | + */ |
|---|
| 38 | + if (!engine->retry_support) { |
|---|
| 39 | + spin_lock_irqsave(&engine->queue_lock, flags); |
|---|
| 40 | + if (engine->cur_req == req) { |
|---|
| 41 | + finalize_req = true; |
|---|
| 42 | + engine->cur_req = NULL; |
|---|
| 43 | + } |
|---|
| 44 | + spin_unlock_irqrestore(&engine->queue_lock, flags); |
|---|
| 45 | + } |
|---|
| 41 | 46 | |
|---|
| 42 | | - if (finalize_cur_req) { |
|---|
| 47 | + if (finalize_req || engine->retry_support) { |
|---|
| 43 | 48 | enginectx = crypto_tfm_ctx(req->tfm); |
|---|
| 44 | | - if (engine->cur_req_prepared && |
|---|
| 49 | + if (enginectx->op.prepare_request && |
|---|
| 45 | 50 | enginectx->op.unprepare_request) { |
|---|
| 46 | 51 | ret = enginectx->op.unprepare_request(engine, req); |
|---|
| 47 | 52 | if (ret) |
|---|
| 48 | 53 | dev_err(engine->dev, "failed to unprepare request\n"); |
|---|
| 49 | 54 | } |
|---|
| 50 | | - spin_lock_irqsave(&engine->queue_lock, flags); |
|---|
| 51 | | - engine->cur_req = NULL; |
|---|
| 52 | | - engine->cur_req_prepared = false; |
|---|
| 53 | | - spin_unlock_irqrestore(&engine->queue_lock, flags); |
|---|
| 54 | 55 | } |
|---|
| 55 | | - |
|---|
| 56 | 56 | req->complete(req, err); |
|---|
| 57 | 57 | |
|---|
| 58 | 58 | kthread_queue_work(engine->kworker, &engine->pump_requests); |
|---|
| .. | .. |
|---|
| 79 | 79 | spin_lock_irqsave(&engine->queue_lock, flags); |
|---|
| 80 | 80 | |
|---|
| 81 | 81 | /* Make sure we are not already running a request */ |
|---|
| 82 | | - if (engine->cur_req) |
|---|
| 82 | + if (!engine->retry_support && engine->cur_req) |
|---|
| 83 | 83 | goto out; |
|---|
| 84 | 84 | |
|---|
| 85 | 85 | /* If another context is idling then defer */ |
|---|
| .. | .. |
|---|
| 113 | 113 | goto out; |
|---|
| 114 | 114 | } |
|---|
| 115 | 115 | |
|---|
| 116 | +start_request: |
|---|
| 116 | 117 | /* Get the fist request from the engine queue to handle */ |
|---|
| 117 | 118 | backlog = crypto_get_backlog(&engine->queue); |
|---|
| 118 | 119 | async_req = crypto_dequeue_request(&engine->queue); |
|---|
| 119 | 120 | if (!async_req) |
|---|
| 120 | 121 | goto out; |
|---|
| 121 | 122 | |
|---|
| 122 | | - engine->cur_req = async_req; |
|---|
| 123 | + /* |
|---|
| 124 | + * If hardware doesn't support the retry mechanism, |
|---|
| 125 | + * keep track of the request we are processing now. |
|---|
| 126 | + * We'll need it on completion (crypto_finalize_request). |
|---|
| 127 | + */ |
|---|
| 128 | + if (!engine->retry_support) |
|---|
| 129 | + engine->cur_req = async_req; |
|---|
| 130 | + |
|---|
| 123 | 131 | if (backlog) |
|---|
| 124 | 132 | backlog->complete(backlog, -EINPROGRESS); |
|---|
| 125 | 133 | |
|---|
| .. | .. |
|---|
| 135 | 143 | ret = engine->prepare_crypt_hardware(engine); |
|---|
| 136 | 144 | if (ret) { |
|---|
| 137 | 145 | dev_err(engine->dev, "failed to prepare crypt hardware\n"); |
|---|
| 138 | | - goto req_err; |
|---|
| 146 | + goto req_err_2; |
|---|
| 139 | 147 | } |
|---|
| 140 | 148 | } |
|---|
| 141 | 149 | |
|---|
| .. | .. |
|---|
| 146 | 154 | if (ret) { |
|---|
| 147 | 155 | dev_err(engine->dev, "failed to prepare request: %d\n", |
|---|
| 148 | 156 | ret); |
|---|
| 149 | | - goto req_err; |
|---|
| 157 | + goto req_err_2; |
|---|
| 150 | 158 | } |
|---|
| 151 | | - engine->cur_req_prepared = true; |
|---|
| 152 | 159 | } |
|---|
| 153 | 160 | if (!enginectx->op.do_one_request) { |
|---|
| 154 | 161 | dev_err(engine->dev, "failed to do request\n"); |
|---|
| 155 | 162 | ret = -EINVAL; |
|---|
| 156 | | - goto req_err; |
|---|
| 163 | + goto req_err_1; |
|---|
| 157 | 164 | } |
|---|
| 158 | | - ret = enginectx->op.do_one_request(engine, async_req); |
|---|
| 159 | | - if (ret) { |
|---|
| 160 | | - dev_err(engine->dev, "Failed to do one request from queue: %d\n", ret); |
|---|
| 161 | | - goto req_err; |
|---|
| 162 | | - } |
|---|
| 163 | | - return; |
|---|
| 164 | 165 | |
|---|
| 165 | | -req_err: |
|---|
| 166 | | - crypto_finalize_request(engine, async_req, ret); |
|---|
| 166 | + ret = enginectx->op.do_one_request(engine, async_req); |
|---|
| 167 | + |
|---|
| 168 | + /* Request unsuccessfully executed by hardware */ |
|---|
| 169 | + if (ret < 0) { |
|---|
| 170 | + /* |
|---|
| 171 | + * If hardware queue is full (-ENOSPC), requeue request |
|---|
| 172 | + * regardless of backlog flag. |
|---|
| 173 | + * Otherwise, unprepare and complete the request. |
|---|
| 174 | + */ |
|---|
| 175 | + if (!engine->retry_support || |
|---|
| 176 | + (ret != -ENOSPC)) { |
|---|
| 177 | + dev_err(engine->dev, |
|---|
| 178 | + "Failed to do one request from queue: %d\n", |
|---|
| 179 | + ret); |
|---|
| 180 | + goto req_err_1; |
|---|
| 181 | + } |
|---|
| 182 | + /* |
|---|
| 183 | + * If retry mechanism is supported, |
|---|
| 184 | + * unprepare current request and |
|---|
| 185 | + * enqueue it back into crypto-engine queue. |
|---|
| 186 | + */ |
|---|
| 187 | + if (enginectx->op.unprepare_request) { |
|---|
| 188 | + ret = enginectx->op.unprepare_request(engine, |
|---|
| 189 | + async_req); |
|---|
| 190 | + if (ret) |
|---|
| 191 | + dev_err(engine->dev, |
|---|
| 192 | + "failed to unprepare request\n"); |
|---|
| 193 | + } |
|---|
| 194 | + spin_lock_irqsave(&engine->queue_lock, flags); |
|---|
| 195 | + /* |
|---|
| 196 | + * If hardware was unable to execute request, enqueue it |
|---|
| 197 | + * back in front of crypto-engine queue, to keep the order |
|---|
| 198 | + * of requests. |
|---|
| 199 | + */ |
|---|
| 200 | + crypto_enqueue_request_head(&engine->queue, async_req); |
|---|
| 201 | + |
|---|
| 202 | + kthread_queue_work(engine->kworker, &engine->pump_requests); |
|---|
| 203 | + goto out; |
|---|
| 204 | + } |
|---|
| 205 | + |
|---|
| 206 | + goto retry; |
|---|
| 207 | + |
|---|
| 208 | +req_err_1: |
|---|
| 209 | + if (enginectx->op.unprepare_request) { |
|---|
| 210 | + ret = enginectx->op.unprepare_request(engine, async_req); |
|---|
| 211 | + if (ret) |
|---|
| 212 | + dev_err(engine->dev, "failed to unprepare request\n"); |
|---|
| 213 | + } |
|---|
| 214 | + |
|---|
| 215 | +req_err_2: |
|---|
| 216 | + async_req->complete(async_req, ret); |
|---|
| 217 | + |
|---|
| 218 | +retry: |
|---|
| 219 | + /* If retry mechanism is supported, send new requests to engine */ |
|---|
| 220 | + if (engine->retry_support) { |
|---|
| 221 | + spin_lock_irqsave(&engine->queue_lock, flags); |
|---|
| 222 | + goto start_request; |
|---|
| 223 | + } |
|---|
| 167 | 224 | return; |
|---|
| 168 | 225 | |
|---|
| 169 | 226 | out: |
|---|
| 170 | 227 | spin_unlock_irqrestore(&engine->queue_lock, flags); |
|---|
| 228 | + |
|---|
| 229 | + /* |
|---|
| 230 | + * Batch requests is possible only if |
|---|
| 231 | + * hardware can enqueue multiple requests |
|---|
| 232 | + */ |
|---|
| 233 | + if (engine->do_batch_requests) { |
|---|
| 234 | + ret = engine->do_batch_requests(engine); |
|---|
| 235 | + if (ret) |
|---|
| 236 | + dev_err(engine->dev, "failed to do batch requests: %d\n", |
|---|
| 237 | + ret); |
|---|
| 238 | + } |
|---|
| 239 | + |
|---|
| 240 | + return; |
|---|
| 171 | 241 | } |
|---|
| 172 | 242 | |
|---|
| 173 | 243 | static void crypto_pump_work(struct kthread_work *work) |
|---|
| .. | .. |
|---|
| 217 | 287 | { |
|---|
| 218 | 288 | return crypto_transfer_request(engine, req, true); |
|---|
| 219 | 289 | } |
|---|
| 220 | | - |
|---|
| 221 | | -/** |
|---|
| 222 | | - * crypto_transfer_ablkcipher_request_to_engine - transfer one ablkcipher_request |
|---|
| 223 | | - * to list into the engine queue |
|---|
| 224 | | - * @engine: the hardware engine |
|---|
| 225 | | - * @req: the request need to be listed into the engine queue |
|---|
| 226 | | - * TODO: Remove this function when skcipher conversion is finished |
|---|
| 227 | | - */ |
|---|
| 228 | | -int crypto_transfer_ablkcipher_request_to_engine(struct crypto_engine *engine, |
|---|
| 229 | | - struct ablkcipher_request *req) |
|---|
| 230 | | -{ |
|---|
| 231 | | - return crypto_transfer_request_to_engine(engine, &req->base); |
|---|
| 232 | | -} |
|---|
| 233 | | -EXPORT_SYMBOL_GPL(crypto_transfer_ablkcipher_request_to_engine); |
|---|
| 234 | 290 | |
|---|
| 235 | 291 | /** |
|---|
| 236 | 292 | * crypto_transfer_aead_request_to_engine - transfer one aead_request |
|---|
| .. | .. |
|---|
| 283 | 339 | return crypto_transfer_request_to_engine(engine, &req->base); |
|---|
| 284 | 340 | } |
|---|
| 285 | 341 | EXPORT_SYMBOL_GPL(crypto_transfer_skcipher_request_to_engine); |
|---|
| 286 | | - |
|---|
| 287 | | -/** |
|---|
| 288 | | - * crypto_finalize_ablkcipher_request - finalize one ablkcipher_request if |
|---|
| 289 | | - * the request is done |
|---|
| 290 | | - * @engine: the hardware engine |
|---|
| 291 | | - * @req: the request need to be finalized |
|---|
| 292 | | - * @err: error number |
|---|
| 293 | | - * TODO: Remove this function when skcipher conversion is finished |
|---|
| 294 | | - */ |
|---|
| 295 | | -void crypto_finalize_ablkcipher_request(struct crypto_engine *engine, |
|---|
| 296 | | - struct ablkcipher_request *req, int err) |
|---|
| 297 | | -{ |
|---|
| 298 | | - return crypto_finalize_request(engine, &req->base, err); |
|---|
| 299 | | -} |
|---|
| 300 | | -EXPORT_SYMBOL_GPL(crypto_finalize_ablkcipher_request); |
|---|
| 301 | 342 | |
|---|
| 302 | 343 | /** |
|---|
| 303 | 344 | * crypto_finalize_aead_request - finalize one aead_request if |
|---|
| .. | .. |
|---|
| 420 | 461 | EXPORT_SYMBOL_GPL(crypto_engine_stop); |
|---|
| 421 | 462 | |
|---|
| 422 | 463 | /** |
|---|
| 423 | | - * crypto_engine_alloc_init - allocate crypto hardware engine structure and |
|---|
| 424 | | - * initialize it. |
|---|
| 464 | + * crypto_engine_alloc_init_and_set - allocate crypto hardware engine structure |
|---|
| 465 | + * and initialize it by setting the maximum number of entries in the software |
|---|
| 466 | + * crypto-engine queue. |
|---|
| 425 | 467 | * @dev: the device attached with one hardware engine |
|---|
| 468 | + * @retry_support: whether hardware has support for retry mechanism |
|---|
| 469 | + * @cbk_do_batch: pointer to a callback function to be invoked when executing |
|---|
| 470 | + * a batch of requests. |
|---|
| 471 | + * This has the form: |
|---|
| 472 | + * callback(struct crypto_engine *engine) |
|---|
| 473 | + * where: |
|---|
| 474 | + * @engine: the crypto engine structure. |
|---|
| 426 | 475 | * @rt: whether this queue is set to run as a realtime task |
|---|
| 476 | + * @qlen: maximum size of the crypto-engine queue |
|---|
| 427 | 477 | * |
|---|
| 428 | 478 | * This must be called from context that can sleep. |
|---|
| 429 | 479 | * Return: the crypto engine structure on success, else NULL. |
|---|
| 430 | 480 | */ |
|---|
| 431 | | -struct crypto_engine *crypto_engine_alloc_init(struct device *dev, bool rt) |
|---|
| 481 | +struct crypto_engine *crypto_engine_alloc_init_and_set(struct device *dev, |
|---|
| 482 | + bool retry_support, |
|---|
| 483 | + int (*cbk_do_batch)(struct crypto_engine *engine), |
|---|
| 484 | + bool rt, int qlen) |
|---|
| 432 | 485 | { |
|---|
| 433 | | - struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 }; |
|---|
| 434 | 486 | struct crypto_engine *engine; |
|---|
| 435 | 487 | |
|---|
| 436 | 488 | if (!dev) |
|---|
| .. | .. |
|---|
| 445 | 497 | engine->running = false; |
|---|
| 446 | 498 | engine->busy = false; |
|---|
| 447 | 499 | engine->idling = false; |
|---|
| 448 | | - engine->cur_req_prepared = false; |
|---|
| 500 | + engine->retry_support = retry_support; |
|---|
| 449 | 501 | engine->priv_data = dev; |
|---|
| 502 | + /* |
|---|
| 503 | + * Batch requests is possible only if |
|---|
| 504 | + * hardware has support for retry mechanism. |
|---|
| 505 | + */ |
|---|
| 506 | + engine->do_batch_requests = retry_support ? cbk_do_batch : NULL; |
|---|
| 507 | + |
|---|
| 450 | 508 | snprintf(engine->name, sizeof(engine->name), |
|---|
| 451 | 509 | "%s-engine", dev_name(dev)); |
|---|
| 452 | 510 | |
|---|
| 453 | | - crypto_init_queue(&engine->queue, CRYPTO_ENGINE_MAX_QLEN); |
|---|
| 511 | + crypto_init_queue(&engine->queue, qlen); |
|---|
| 454 | 512 | spin_lock_init(&engine->queue_lock); |
|---|
| 455 | 513 | |
|---|
| 456 | 514 | engine->kworker = kthread_create_worker(0, "%s", engine->name); |
|---|
| .. | .. |
|---|
| 462 | 520 | |
|---|
| 463 | 521 | if (engine->rt) { |
|---|
| 464 | 522 | dev_info(dev, "will run requests pump with realtime priority\n"); |
|---|
| 465 | | - sched_setscheduler(engine->kworker->task, SCHED_FIFO, ¶m); |
|---|
| 523 | + sched_set_fifo(engine->kworker->task); |
|---|
| 466 | 524 | } |
|---|
| 467 | 525 | |
|---|
| 468 | 526 | return engine; |
|---|
| 469 | 527 | } |
|---|
| 528 | +EXPORT_SYMBOL_GPL(crypto_engine_alloc_init_and_set); |
|---|
| 529 | + |
|---|
| 530 | +/** |
|---|
| 531 | + * crypto_engine_alloc_init - allocate crypto hardware engine structure and |
|---|
| 532 | + * initialize it. |
|---|
| 533 | + * @dev: the device attached with one hardware engine |
|---|
| 534 | + * @rt: whether this queue is set to run as a realtime task |
|---|
| 535 | + * |
|---|
| 536 | + * This must be called from context that can sleep. |
|---|
| 537 | + * Return: the crypto engine structure on success, else NULL. |
|---|
| 538 | + */ |
|---|
| 539 | +struct crypto_engine *crypto_engine_alloc_init(struct device *dev, bool rt) |
|---|
| 540 | +{ |
|---|
| 541 | + return crypto_engine_alloc_init_and_set(dev, false, NULL, rt, |
|---|
| 542 | + CRYPTO_ENGINE_MAX_QLEN); |
|---|
| 543 | +} |
|---|
| 470 | 544 | EXPORT_SYMBOL_GPL(crypto_engine_alloc_init); |
|---|
| 471 | 545 | |
|---|
| 472 | 546 | /** |
|---|