engine.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Crypto engine API
  3. *
  4. * Copyright (c) 2016 Baolin Wang <baolin.wang@linaro.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. */
  12. #ifndef _CRYPTO_ENGINE_H
  13. #define _CRYPTO_ENGINE_H
  14. #include <linux/crypto.h>
  15. #include <linux/list.h>
  16. #include <linux/kernel.h>
  17. #include <linux/kthread.h>
  18. #include <crypto/algapi.h>
  19. #include <crypto/hash.h>
  20. #define ENGINE_NAME_LEN 30
  21. /*
  22. * struct crypto_engine - crypto hardware engine
  23. * @name: the engine name
  24. * @idling: the engine is entering idle state
  25. * @busy: request pump is busy
  26. * @running: the engine is on working
  27. * @cur_req_prepared: current request is prepared
  28. * @list: link with the global crypto engine list
  29. * @queue_lock: spinlock to syncronise access to request queue
  30. * @queue: the crypto queue of the engine
  31. * @rt: whether this queue is set to run as a realtime task
  32. * @prepare_crypt_hardware: a request will soon arrive from the queue
  33. * so the subsystem requests the driver to prepare the hardware
  34. * by issuing this call
  35. * @unprepare_crypt_hardware: there are currently no more requests on the
  36. * queue so the subsystem notifies the driver that it may relax the
  37. * hardware by issuing this call
  38. * @prepare_cipher_request: do some prepare if need before handle the current request
  39. * @unprepare_cipher_request: undo any work done by prepare_cipher_request()
  40. * @cipher_one_request: do encryption for current request
  41. * @prepare_hash_request: do some prepare if need before handle the current request
  42. * @unprepare_hash_request: undo any work done by prepare_hash_request()
  43. * @hash_one_request: do hash for current request
  44. * @kworker: kthread worker struct for request pump
  45. * @pump_requests: work struct for scheduling work to the request pump
  46. * @priv_data: the engine private data
  47. * @cur_req: the current request which is on processing
  48. */
  49. struct crypto_engine {
  50. char name[ENGINE_NAME_LEN];
  51. bool idling;
  52. bool busy;
  53. bool running;
  54. bool cur_req_prepared;
  55. struct list_head list;
  56. spinlock_t queue_lock;
  57. struct crypto_queue queue;
  58. struct device *dev;
  59. bool rt;
  60. int (*prepare_crypt_hardware)(struct crypto_engine *engine);
  61. int (*unprepare_crypt_hardware)(struct crypto_engine *engine);
  62. int (*prepare_cipher_request)(struct crypto_engine *engine,
  63. struct ablkcipher_request *req);
  64. int (*unprepare_cipher_request)(struct crypto_engine *engine,
  65. struct ablkcipher_request *req);
  66. int (*prepare_hash_request)(struct crypto_engine *engine,
  67. struct ahash_request *req);
  68. int (*unprepare_hash_request)(struct crypto_engine *engine,
  69. struct ahash_request *req);
  70. int (*cipher_one_request)(struct crypto_engine *engine,
  71. struct ablkcipher_request *req);
  72. int (*hash_one_request)(struct crypto_engine *engine,
  73. struct ahash_request *req);
  74. struct kthread_worker *kworker;
  75. struct kthread_work pump_requests;
  76. void *priv_data;
  77. struct crypto_async_request *cur_req;
  78. };
  79. int crypto_transfer_cipher_request(struct crypto_engine *engine,
  80. struct ablkcipher_request *req,
  81. bool need_pump);
  82. int crypto_transfer_cipher_request_to_engine(struct crypto_engine *engine,
  83. struct ablkcipher_request *req);
  84. int crypto_transfer_hash_request(struct crypto_engine *engine,
  85. struct ahash_request *req, bool need_pump);
  86. int crypto_transfer_hash_request_to_engine(struct crypto_engine *engine,
  87. struct ahash_request *req);
  88. void crypto_finalize_cipher_request(struct crypto_engine *engine,
  89. struct ablkcipher_request *req, int err);
  90. void crypto_finalize_hash_request(struct crypto_engine *engine,
  91. struct ahash_request *req, int err);
  92. int crypto_engine_start(struct crypto_engine *engine);
  93. int crypto_engine_stop(struct crypto_engine *engine);
  94. struct crypto_engine *crypto_engine_alloc_init(struct device *dev, bool rt);
  95. int crypto_engine_exit(struct crypto_engine *engine);
  96. #endif /* _CRYPTO_ENGINE_H */