engine.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/aead.h>
  20. #include <crypto/akcipher.h>
  21. #include <crypto/hash.h>
  22. #include <crypto/skcipher.h>
  23. #define ENGINE_NAME_LEN 30
  24. /*
  25. * struct crypto_engine - crypto hardware engine
  26. * @name: the engine name
  27. * @idling: the engine is entering idle state
  28. * @busy: request pump is busy
  29. * @running: the engine is on working
  30. * @cur_req_prepared: current request is prepared
  31. * @list: link with the global crypto engine list
  32. * @queue_lock: spinlock to syncronise access to request queue
  33. * @queue: the crypto queue of the engine
  34. * @rt: whether this queue is set to run as a realtime task
  35. * @prepare_crypt_hardware: a request will soon arrive from the queue
  36. * so the subsystem requests the driver to prepare the hardware
  37. * by issuing this call
  38. * @unprepare_crypt_hardware: there are currently no more requests on the
  39. * queue so the subsystem notifies the driver that it may relax the
  40. * hardware by issuing this call
  41. * @kworker: kthread worker struct for request pump
  42. * @pump_requests: work struct for scheduling work to the request pump
  43. * @priv_data: the engine private data
  44. * @cur_req: the current request which is on processing
  45. */
  46. struct crypto_engine {
  47. char name[ENGINE_NAME_LEN];
  48. bool idling;
  49. bool busy;
  50. bool running;
  51. bool cur_req_prepared;
  52. struct list_head list;
  53. spinlock_t queue_lock;
  54. struct crypto_queue queue;
  55. struct device *dev;
  56. bool rt;
  57. int (*prepare_crypt_hardware)(struct crypto_engine *engine);
  58. int (*unprepare_crypt_hardware)(struct crypto_engine *engine);
  59. struct kthread_worker *kworker;
  60. struct kthread_work pump_requests;
  61. void *priv_data;
  62. struct crypto_async_request *cur_req;
  63. };
  64. /*
  65. * struct crypto_engine_op - crypto hardware engine operations
  66. * @prepare__request: do some prepare if need before handle the current request
  67. * @unprepare_request: undo any work done by prepare_request()
  68. * @do_one_request: do encryption for current request
  69. */
  70. struct crypto_engine_op {
  71. int (*prepare_request)(struct crypto_engine *engine,
  72. void *areq);
  73. int (*unprepare_request)(struct crypto_engine *engine,
  74. void *areq);
  75. int (*do_one_request)(struct crypto_engine *engine,
  76. void *areq);
  77. };
  78. struct crypto_engine_ctx {
  79. struct crypto_engine_op op;
  80. };
  81. int crypto_transfer_ablkcipher_request_to_engine(struct crypto_engine *engine,
  82. struct ablkcipher_request *req);
  83. int crypto_transfer_aead_request_to_engine(struct crypto_engine *engine,
  84. struct aead_request *req);
  85. int crypto_transfer_akcipher_request_to_engine(struct crypto_engine *engine,
  86. struct akcipher_request *req);
  87. int crypto_transfer_hash_request_to_engine(struct crypto_engine *engine,
  88. struct ahash_request *req);
  89. int crypto_transfer_skcipher_request_to_engine(struct crypto_engine *engine,
  90. struct skcipher_request *req);
  91. void crypto_finalize_ablkcipher_request(struct crypto_engine *engine,
  92. struct ablkcipher_request *req, int err);
  93. void crypto_finalize_aead_request(struct crypto_engine *engine,
  94. struct aead_request *req, int err);
  95. void crypto_finalize_akcipher_request(struct crypto_engine *engine,
  96. struct akcipher_request *req, int err);
  97. void crypto_finalize_hash_request(struct crypto_engine *engine,
  98. struct ahash_request *req, int err);
  99. void crypto_finalize_skcipher_request(struct crypto_engine *engine,
  100. struct skcipher_request *req, int err);
  101. int crypto_engine_start(struct crypto_engine *engine);
  102. int crypto_engine_stop(struct crypto_engine *engine);
  103. struct crypto_engine *crypto_engine_alloc_init(struct device *dev, bool rt);
  104. int crypto_engine_exit(struct crypto_engine *engine);
  105. #endif /* _CRYPTO_ENGINE_H */