engine.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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: thread struct for request pump
  45. * @kworker_task: pointer to task for request pump kworker thread
  46. * @pump_requests: work struct for scheduling work to the request pump
  47. * @priv_data: the engine private data
  48. * @cur_req: the current request which is on processing
  49. */
  50. struct crypto_engine {
  51. char name[ENGINE_NAME_LEN];
  52. bool idling;
  53. bool busy;
  54. bool running;
  55. bool cur_req_prepared;
  56. struct list_head list;
  57. spinlock_t queue_lock;
  58. struct crypto_queue queue;
  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 task_struct *kworker_task;
  76. struct kthread_work pump_requests;
  77. void *priv_data;
  78. struct crypto_async_request *cur_req;
  79. };
  80. int crypto_transfer_cipher_request(struct crypto_engine *engine,
  81. struct ablkcipher_request *req,
  82. bool need_pump);
  83. int crypto_transfer_cipher_request_to_engine(struct crypto_engine *engine,
  84. struct ablkcipher_request *req);
  85. int crypto_transfer_hash_request(struct crypto_engine *engine,
  86. struct ahash_request *req, bool need_pump);
  87. int crypto_transfer_hash_request_to_engine(struct crypto_engine *engine,
  88. struct ahash_request *req);
  89. void crypto_finalize_cipher_request(struct crypto_engine *engine,
  90. struct ablkcipher_request *req, int err);
  91. void crypto_finalize_hash_request(struct crypto_engine *engine,
  92. struct ahash_request *req, int err);
  93. int crypto_engine_start(struct crypto_engine *engine);
  94. int crypto_engine_stop(struct crypto_engine *engine);
  95. struct crypto_engine *crypto_engine_alloc_init(struct device *dev, bool rt);
  96. int crypto_engine_exit(struct crypto_engine *engine);
  97. #endif /* _CRYPTO_ENGINE_H */