qat_crypto.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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_transport_access_macros.h"
  47. #include "adf_cfg.h"
  48. #include "adf_cfg_strings.h"
  49. #include "qat_crypto.h"
  50. #include "icp_qat_fw.h"
  51. #define SEC ADF_KERNEL_SEC
  52. static struct service_hndl qat_crypto;
  53. void qat_crypto_put_instance(struct qat_crypto_instance *inst)
  54. {
  55. atomic_dec(&inst->refctr);
  56. adf_dev_put(inst->accel_dev);
  57. }
  58. static int qat_crypto_free_instances(struct adf_accel_dev *accel_dev)
  59. {
  60. struct qat_crypto_instance *inst, *tmp;
  61. int i;
  62. list_for_each_entry_safe(inst, tmp, &accel_dev->crypto_list, list) {
  63. for (i = 0; i < atomic_read(&inst->refctr); i++)
  64. qat_crypto_put_instance(inst);
  65. if (inst->sym_tx)
  66. adf_remove_ring(inst->sym_tx);
  67. if (inst->sym_rx)
  68. adf_remove_ring(inst->sym_rx);
  69. if (inst->pke_tx)
  70. adf_remove_ring(inst->pke_tx);
  71. if (inst->pke_rx)
  72. adf_remove_ring(inst->pke_rx);
  73. list_del(&inst->list);
  74. kfree(inst);
  75. }
  76. return 0;
  77. }
  78. struct qat_crypto_instance *qat_crypto_get_instance_node(int node)
  79. {
  80. struct adf_accel_dev *accel_dev = NULL, *tmp_dev;
  81. struct qat_crypto_instance *inst = NULL, *tmp_inst;
  82. unsigned long best = ~0;
  83. list_for_each_entry(tmp_dev, adf_devmgr_get_head(), list) {
  84. unsigned long ctr;
  85. if ((node == dev_to_node(&GET_DEV(tmp_dev)) ||
  86. dev_to_node(&GET_DEV(tmp_dev)) < 0) &&
  87. adf_dev_started(tmp_dev) &&
  88. !list_empty(&tmp_dev->crypto_list)) {
  89. ctr = atomic_read(&tmp_dev->ref_count);
  90. if (best > ctr) {
  91. accel_dev = tmp_dev;
  92. best = ctr;
  93. }
  94. }
  95. }
  96. if (!accel_dev) {
  97. pr_info("QAT: Could not find a device on node %d\n", node);
  98. /* Get any started device */
  99. list_for_each_entry(tmp_dev, adf_devmgr_get_head(), list) {
  100. if (adf_dev_started(tmp_dev) &&
  101. !list_empty(&tmp_dev->crypto_list)) {
  102. accel_dev = tmp_dev;
  103. break;
  104. }
  105. }
  106. }
  107. if (!accel_dev)
  108. return NULL;
  109. best = ~0;
  110. list_for_each_entry(tmp_inst, &accel_dev->crypto_list, list) {
  111. unsigned long ctr;
  112. ctr = atomic_read(&tmp_inst->refctr);
  113. if (best > ctr) {
  114. inst = tmp_inst;
  115. best = ctr;
  116. }
  117. }
  118. if (inst) {
  119. if (adf_dev_get(accel_dev)) {
  120. dev_err(&GET_DEV(accel_dev), "Could not increment dev refctr\n");
  121. return NULL;
  122. }
  123. atomic_inc(&inst->refctr);
  124. }
  125. return inst;
  126. }
  127. /**
  128. * qat_crypto_dev_config() - create dev config required to create crypto inst.
  129. *
  130. * @accel_dev: Pointer to acceleration device.
  131. *
  132. * Function creates device configuration required to create crypto instances
  133. *
  134. * Return: 0 on success, error code otherwise.
  135. */
  136. int qat_crypto_dev_config(struct adf_accel_dev *accel_dev)
  137. {
  138. int cpus = num_online_cpus();
  139. int banks = GET_MAX_BANKS(accel_dev);
  140. int instances = min(cpus, banks);
  141. char key[ADF_CFG_MAX_KEY_LEN_IN_BYTES];
  142. int i;
  143. unsigned long val;
  144. if (adf_cfg_section_add(accel_dev, ADF_KERNEL_SEC))
  145. goto err;
  146. if (adf_cfg_section_add(accel_dev, "Accelerator0"))
  147. goto err;
  148. for (i = 0; i < instances; i++) {
  149. val = i;
  150. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_BANK_NUM, i);
  151. if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
  152. key, (void *)&val, ADF_DEC))
  153. goto err;
  154. snprintf(key, sizeof(key), ADF_CY "%d" ADF_ETRMGR_CORE_AFFINITY,
  155. i);
  156. if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
  157. key, (void *)&val, ADF_DEC))
  158. goto err;
  159. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_SIZE, i);
  160. val = 128;
  161. if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
  162. key, (void *)&val, ADF_DEC))
  163. goto err;
  164. val = 512;
  165. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_SIZE, i);
  166. if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
  167. key, (void *)&val, ADF_DEC))
  168. goto err;
  169. val = 0;
  170. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_TX, i);
  171. if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
  172. key, (void *)&val, ADF_DEC))
  173. goto err;
  174. val = 2;
  175. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_TX, i);
  176. if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
  177. key, (void *)&val, ADF_DEC))
  178. goto err;
  179. val = 8;
  180. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_RX, i);
  181. if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
  182. key, (void *)&val, ADF_DEC))
  183. goto err;
  184. val = 10;
  185. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_RX, i);
  186. if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
  187. key, (void *)&val, ADF_DEC))
  188. goto err;
  189. val = ADF_COALESCING_DEF_TIME;
  190. snprintf(key, sizeof(key), ADF_ETRMGR_COALESCE_TIMER_FORMAT, i);
  191. if (adf_cfg_add_key_value_param(accel_dev, "Accelerator0",
  192. key, (void *)&val, ADF_DEC))
  193. goto err;
  194. }
  195. val = i;
  196. if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC,
  197. ADF_NUM_CY, (void *)&val, ADF_DEC))
  198. goto err;
  199. set_bit(ADF_STATUS_CONFIGURED, &accel_dev->status);
  200. return 0;
  201. err:
  202. dev_err(&GET_DEV(accel_dev), "Failed to start QAT accel dev\n");
  203. return -EINVAL;
  204. }
  205. EXPORT_SYMBOL_GPL(qat_crypto_dev_config);
  206. static int qat_crypto_create_instances(struct adf_accel_dev *accel_dev)
  207. {
  208. int i;
  209. unsigned long bank;
  210. unsigned long num_inst, num_msg_sym, num_msg_asym;
  211. int msg_size;
  212. struct qat_crypto_instance *inst;
  213. char key[ADF_CFG_MAX_KEY_LEN_IN_BYTES];
  214. char val[ADF_CFG_MAX_VAL_LEN_IN_BYTES];
  215. INIT_LIST_HEAD(&accel_dev->crypto_list);
  216. strlcpy(key, ADF_NUM_CY, sizeof(key));
  217. if (adf_cfg_get_param_value(accel_dev, SEC, key, val))
  218. return -EFAULT;
  219. if (kstrtoul(val, 0, &num_inst))
  220. return -EFAULT;
  221. for (i = 0; i < num_inst; i++) {
  222. inst = kzalloc_node(sizeof(*inst), GFP_KERNEL,
  223. dev_to_node(&GET_DEV(accel_dev)));
  224. if (!inst)
  225. goto err;
  226. list_add_tail(&inst->list, &accel_dev->crypto_list);
  227. inst->id = i;
  228. atomic_set(&inst->refctr, 0);
  229. inst->accel_dev = accel_dev;
  230. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_BANK_NUM, i);
  231. if (adf_cfg_get_param_value(accel_dev, SEC, key, val))
  232. goto err;
  233. if (kstrtoul(val, 10, &bank))
  234. goto err;
  235. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_SIZE, i);
  236. if (adf_cfg_get_param_value(accel_dev, SEC, key, val))
  237. goto err;
  238. if (kstrtoul(val, 10, &num_msg_sym))
  239. goto err;
  240. num_msg_sym = num_msg_sym >> 1;
  241. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_SIZE, i);
  242. if (adf_cfg_get_param_value(accel_dev, SEC, key, val))
  243. goto err;
  244. if (kstrtoul(val, 10, &num_msg_asym))
  245. goto err;
  246. num_msg_asym = num_msg_asym >> 1;
  247. msg_size = ICP_QAT_FW_REQ_DEFAULT_SZ;
  248. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_TX, i);
  249. if (adf_create_ring(accel_dev, SEC, bank, num_msg_sym,
  250. msg_size, key, NULL, 0, &inst->sym_tx))
  251. goto err;
  252. msg_size = msg_size >> 1;
  253. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_TX, i);
  254. if (adf_create_ring(accel_dev, SEC, bank, num_msg_asym,
  255. msg_size, key, NULL, 0, &inst->pke_tx))
  256. goto err;
  257. msg_size = ICP_QAT_FW_RESP_DEFAULT_SZ;
  258. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_SYM_RX, i);
  259. if (adf_create_ring(accel_dev, SEC, bank, num_msg_sym,
  260. msg_size, key, qat_alg_callback, 0,
  261. &inst->sym_rx))
  262. goto err;
  263. snprintf(key, sizeof(key), ADF_CY "%d" ADF_RING_ASYM_RX, i);
  264. if (adf_create_ring(accel_dev, SEC, bank, num_msg_asym,
  265. msg_size, key, qat_alg_asym_callback, 0,
  266. &inst->pke_rx))
  267. goto err;
  268. }
  269. return 0;
  270. err:
  271. qat_crypto_free_instances(accel_dev);
  272. return -ENOMEM;
  273. }
  274. static int qat_crypto_init(struct adf_accel_dev *accel_dev)
  275. {
  276. if (qat_crypto_create_instances(accel_dev))
  277. return -EFAULT;
  278. return 0;
  279. }
  280. static int qat_crypto_shutdown(struct adf_accel_dev *accel_dev)
  281. {
  282. return qat_crypto_free_instances(accel_dev);
  283. }
  284. static int qat_crypto_event_handler(struct adf_accel_dev *accel_dev,
  285. enum adf_event event)
  286. {
  287. int ret;
  288. switch (event) {
  289. case ADF_EVENT_INIT:
  290. ret = qat_crypto_init(accel_dev);
  291. break;
  292. case ADF_EVENT_SHUTDOWN:
  293. ret = qat_crypto_shutdown(accel_dev);
  294. break;
  295. case ADF_EVENT_RESTARTING:
  296. case ADF_EVENT_RESTARTED:
  297. case ADF_EVENT_START:
  298. case ADF_EVENT_STOP:
  299. default:
  300. ret = 0;
  301. }
  302. return ret;
  303. }
  304. int qat_crypto_register(void)
  305. {
  306. memset(&qat_crypto, 0, sizeof(qat_crypto));
  307. qat_crypto.event_hld = qat_crypto_event_handler;
  308. qat_crypto.name = "qat_crypto";
  309. return adf_service_register(&qat_crypto);
  310. }
  311. int qat_crypto_unregister(void)
  312. {
  313. return adf_service_unregister(&qat_crypto);
  314. }