qat_crypto.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. This file is provided under a dual BSD/GPLv2 license. When using or
  3. redistributing this file, you may do so under either license.
  4. GPL LICENSE SUMMARY
  5. Copyright(c) 2014 Intel Corporation.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License as
  8. published by the Free Software Foundation.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. Contact Information:
  14. qat-linux@intel.com
  15. BSD LICENSE
  16. Copyright(c) 2014 Intel Corporation.
  17. Redistribution and use in source and binary forms, with or without
  18. modification, are permitted provided that the following conditions
  19. are met:
  20. * Redistributions of source code must retain the above copyright
  21. notice, this list of conditions and the following disclaimer.
  22. * Redistributions in binary form must reproduce the above copyright
  23. notice, this list of conditions and the following disclaimer in
  24. the documentation and/or other materials provided with the
  25. distribution.
  26. * Neither the name of Intel Corporation nor the names of its
  27. contributors may be used to endorse or promote products derived
  28. from this software without specific prior written permission.
  29. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  30. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  31. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  32. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  33. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  34. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  35. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  36. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  37. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  39. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40. */
  41. #include <linux/module.h>
  42. #include <linux/slab.h>
  43. #include "adf_accel_devices.h"
  44. #include "adf_common_drv.h"
  45. #include "adf_transport.h"
  46. #include "adf_cfg.h"
  47. #include "adf_cfg_strings.h"
  48. #include "qat_crypto.h"
  49. #include "icp_qat_fw.h"
  50. #define SEC ADF_KERNEL_SEC
  51. static struct service_hndl qat_crypto;
  52. void qat_crypto_put_instance(struct qat_crypto_instance *inst)
  53. {
  54. atomic_dec(&inst->refctr);
  55. adf_dev_put(inst->accel_dev);
  56. }
  57. static int qat_crypto_free_instances(struct adf_accel_dev *accel_dev)
  58. {
  59. struct qat_crypto_instance *inst;
  60. struct list_head *list_ptr, *tmp;
  61. int i;
  62. list_for_each_safe(list_ptr, tmp, &accel_dev->crypto_list) {
  63. inst = list_entry(list_ptr, struct qat_crypto_instance, list);
  64. for (i = 0; i < atomic_read(&inst->refctr); i++)
  65. qat_crypto_put_instance(inst);
  66. if (inst->sym_tx)
  67. adf_remove_ring(inst->sym_tx);
  68. if (inst->sym_rx)
  69. adf_remove_ring(inst->sym_rx);
  70. if (inst->pke_tx)
  71. adf_remove_ring(inst->pke_tx);
  72. if (inst->pke_rx)
  73. adf_remove_ring(inst->pke_rx);
  74. list_del(list_ptr);
  75. kfree(inst);
  76. }
  77. return 0;
  78. }
  79. struct qat_crypto_instance *qat_crypto_get_instance_node(int node)
  80. {
  81. struct adf_accel_dev *accel_dev = NULL;
  82. struct qat_crypto_instance *inst = NULL;
  83. struct list_head *itr;
  84. unsigned long best = ~0;
  85. list_for_each(itr, adf_devmgr_get_head()) {
  86. struct adf_accel_dev *tmp_dev;
  87. unsigned long ctr;
  88. tmp_dev = list_entry(itr, struct adf_accel_dev, list);
  89. if ((node == dev_to_node(&GET_DEV(tmp_dev)) ||
  90. dev_to_node(&GET_DEV(tmp_dev)) < 0) &&
  91. adf_dev_started(tmp_dev) &&
  92. !list_empty(&tmp_dev->crypto_list)) {
  93. ctr = atomic_read(&tmp_dev->ref_count);
  94. if (best > ctr) {
  95. accel_dev = tmp_dev;
  96. best = ctr;
  97. }
  98. }
  99. }
  100. if (!accel_dev)
  101. pr_info("QAT: Could not find a device on node %d\n", node);
  102. /* Get any started device */
  103. list_for_each(itr, adf_devmgr_get_head()) {
  104. struct adf_accel_dev *tmp_dev;
  105. tmp_dev = list_entry(itr, struct adf_accel_dev, list);
  106. if (adf_dev_started(tmp_dev) &&
  107. !list_empty(&tmp_dev->crypto_list)) {
  108. accel_dev = tmp_dev;
  109. break;
  110. }
  111. }
  112. if (!accel_dev)
  113. return NULL;
  114. best = ~0;
  115. list_for_each(itr, &accel_dev->crypto_list) {
  116. struct qat_crypto_instance *tmp_inst;
  117. unsigned long ctr;
  118. tmp_inst = list_entry(itr, struct qat_crypto_instance, list);
  119. ctr = atomic_read(&tmp_inst->refctr);
  120. if (best > ctr) {
  121. inst = tmp_inst;
  122. best = ctr;
  123. }
  124. }
  125. if (inst) {
  126. if (adf_dev_get(accel_dev)) {
  127. dev_err(&GET_DEV(accel_dev), "Could not increment dev refctr\n");
  128. return NULL;
  129. }
  130. atomic_inc(&inst->refctr);
  131. }
  132. return inst;
  133. }
  134. static int qat_crypto_create_instances(struct adf_accel_dev *accel_dev)
  135. {
  136. int i;
  137. unsigned long bank;
  138. unsigned long num_inst, num_msg_sym, num_msg_asym;
  139. int msg_size;
  140. struct qat_crypto_instance *inst;
  141. char key[ADF_CFG_MAX_KEY_LEN_IN_BYTES];
  142. char val[ADF_CFG_MAX_VAL_LEN_IN_BYTES];
  143. INIT_LIST_HEAD(&accel_dev->crypto_list);
  144. strlcpy(key, ADF_NUM_CY, sizeof(key));
  145. if (adf_cfg_get_param_value(accel_dev, SEC, key, val))
  146. return -EFAULT;
  147. if (kstrtoul(val, 0, &num_inst))
  148. return -EFAULT;
  149. for (i = 0; i < num_inst; i++) {
  150. inst = kzalloc_node(sizeof(*inst), GFP_KERNEL,
  151. dev_to_node(&GET_DEV(accel_dev)));
  152. if (!inst)
  153. goto err;
  154. list_add_tail(&inst->list, &accel_dev->crypto_list);
  155. inst->id = i;
  156. atomic_set(&inst->refctr, 0);
  157. inst->accel_dev = accel_dev;
  158. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_BANK_NUM, i);
  159. if (adf_cfg_get_param_value(accel_dev, SEC, key, val))
  160. goto err;
  161. if (kstrtoul(val, 10, &bank))
  162. goto err;
  163. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_SIZE, i);
  164. if (adf_cfg_get_param_value(accel_dev, SEC, key, val))
  165. goto err;
  166. if (kstrtoul(val, 10, &num_msg_sym))
  167. goto err;
  168. num_msg_sym = num_msg_sym >> 1;
  169. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_SIZE, i);
  170. if (adf_cfg_get_param_value(accel_dev, SEC, key, val))
  171. goto err;
  172. if (kstrtoul(val, 10, &num_msg_asym))
  173. goto err;
  174. num_msg_asym = num_msg_asym >> 1;
  175. msg_size = ICP_QAT_FW_REQ_DEFAULT_SZ;
  176. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_TX, i);
  177. if (adf_create_ring(accel_dev, SEC, bank, num_msg_sym,
  178. msg_size, key, NULL, 0, &inst->sym_tx))
  179. goto err;
  180. msg_size = msg_size >> 1;
  181. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_TX, i);
  182. if (adf_create_ring(accel_dev, SEC, bank, num_msg_asym,
  183. msg_size, key, NULL, 0, &inst->pke_tx))
  184. goto err;
  185. msg_size = ICP_QAT_FW_RESP_DEFAULT_SZ;
  186. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_RX, i);
  187. if (adf_create_ring(accel_dev, SEC, bank, num_msg_sym,
  188. msg_size, key, qat_alg_callback, 0,
  189. &inst->sym_rx))
  190. goto err;
  191. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_RX, i);
  192. if (adf_create_ring(accel_dev, SEC, bank, num_msg_asym,
  193. msg_size, key, qat_alg_asym_callback, 0,
  194. &inst->pke_rx))
  195. goto err;
  196. }
  197. return 0;
  198. err:
  199. qat_crypto_free_instances(accel_dev);
  200. return -ENOMEM;
  201. }
  202. static int qat_crypto_init(struct adf_accel_dev *accel_dev)
  203. {
  204. if (qat_crypto_create_instances(accel_dev))
  205. return -EFAULT;
  206. return 0;
  207. }
  208. static int qat_crypto_shutdown(struct adf_accel_dev *accel_dev)
  209. {
  210. return qat_crypto_free_instances(accel_dev);
  211. }
  212. static int qat_crypto_event_handler(struct adf_accel_dev *accel_dev,
  213. enum adf_event event)
  214. {
  215. int ret;
  216. switch (event) {
  217. case ADF_EVENT_INIT:
  218. ret = qat_crypto_init(accel_dev);
  219. break;
  220. case ADF_EVENT_SHUTDOWN:
  221. ret = qat_crypto_shutdown(accel_dev);
  222. break;
  223. case ADF_EVENT_RESTARTING:
  224. case ADF_EVENT_RESTARTED:
  225. case ADF_EVENT_START:
  226. case ADF_EVENT_STOP:
  227. default:
  228. ret = 0;
  229. }
  230. return ret;
  231. }
  232. int qat_crypto_register(void)
  233. {
  234. memset(&qat_crypto, 0, sizeof(qat_crypto));
  235. qat_crypto.event_hld = qat_crypto_event_handler;
  236. qat_crypto.name = "qat_crypto";
  237. return adf_service_register(&qat_crypto);
  238. }
  239. int qat_crypto_unregister(void)
  240. {
  241. return adf_service_unregister(&qat_crypto);
  242. }