chcr_core.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * This file is part of the Chelsio T4/T5/T6 Ethernet driver for Linux.
  3. *
  4. * Copyright (C) 2011-2016 Chelsio Communications. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation.
  9. *
  10. * Written and Maintained by:
  11. * Manoj Malviya (manojmalviya@chelsio.com)
  12. * Atul Gupta (atul.gupta@chelsio.com)
  13. * Jitendra Lulla (jlulla@chelsio.com)
  14. * Yeshaswi M R Gowda (yeshaswi@chelsio.com)
  15. * Harsh Jain (harsh@chelsio.com)
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/skbuff.h>
  20. #include <crypto/aes.h>
  21. #include <crypto/hash.h>
  22. #include "t4_msg.h"
  23. #include "chcr_core.h"
  24. #include "cxgb4_uld.h"
  25. static struct chcr_driver_data drv_data;
  26. typedef int (*chcr_handler_func)(struct adapter *adap, unsigned char *input);
  27. static int cpl_fw6_pld_handler(struct adapter *adap, unsigned char *input);
  28. static void *chcr_uld_add(const struct cxgb4_lld_info *lld);
  29. static int chcr_uld_state_change(void *handle, enum cxgb4_state state);
  30. static chcr_handler_func work_handlers[NUM_CPL_CMDS] = {
  31. [CPL_FW6_PLD] = cpl_fw6_pld_handler,
  32. };
  33. static struct cxgb4_uld_info chcr_uld_info = {
  34. .name = DRV_MODULE_NAME,
  35. .nrxq = MAX_ULD_QSETS,
  36. /* Max ntxq will be derived from fw config file*/
  37. .rxq_size = 1024,
  38. .add = chcr_uld_add,
  39. .state_change = chcr_uld_state_change,
  40. .rx_handler = chcr_uld_rx_handler,
  41. };
  42. static void detach_work_fn(struct work_struct *work)
  43. {
  44. struct chcr_dev *dev;
  45. dev = container_of(work, struct chcr_dev, detach_work.work);
  46. if (atomic_read(&dev->inflight)) {
  47. dev->wqretry--;
  48. if (dev->wqretry) {
  49. pr_debug("Request Inflight Count %d\n",
  50. atomic_read(&dev->inflight));
  51. schedule_delayed_work(&dev->detach_work, WQ_DETACH_TM);
  52. } else {
  53. WARN(1, "CHCR:%d request Still Pending\n",
  54. atomic_read(&dev->inflight));
  55. complete(&dev->detach_comp);
  56. }
  57. } else {
  58. complete(&dev->detach_comp);
  59. }
  60. }
  61. struct uld_ctx *assign_chcr_device(void)
  62. {
  63. struct uld_ctx *u_ctx = NULL;
  64. /*
  65. * When multiple devices are present in system select
  66. * device in round-robin fashion for crypto operations
  67. * Although One session must use the same device to
  68. * maintain request-response ordering.
  69. */
  70. mutex_lock(&drv_data.drv_mutex);
  71. if (!list_empty(&drv_data.act_dev)) {
  72. u_ctx = drv_data.last_dev;
  73. if (list_is_last(&drv_data.last_dev->entry, &drv_data.act_dev))
  74. drv_data.last_dev = list_first_entry(&drv_data.act_dev,
  75. struct uld_ctx, entry);
  76. else
  77. drv_data.last_dev =
  78. list_next_entry(drv_data.last_dev, entry);
  79. }
  80. mutex_unlock(&drv_data.drv_mutex);
  81. return u_ctx;
  82. }
  83. static void chcr_dev_add(struct uld_ctx *u_ctx)
  84. {
  85. struct chcr_dev *dev;
  86. dev = &u_ctx->dev;
  87. dev->state = CHCR_ATTACH;
  88. atomic_set(&dev->inflight, 0);
  89. mutex_lock(&drv_data.drv_mutex);
  90. list_move(&u_ctx->entry, &drv_data.act_dev);
  91. if (!drv_data.last_dev)
  92. drv_data.last_dev = u_ctx;
  93. mutex_unlock(&drv_data.drv_mutex);
  94. }
  95. static void chcr_dev_init(struct uld_ctx *u_ctx)
  96. {
  97. struct chcr_dev *dev;
  98. dev = &u_ctx->dev;
  99. spin_lock_init(&dev->lock_chcr_dev);
  100. INIT_DELAYED_WORK(&dev->detach_work, detach_work_fn);
  101. init_completion(&dev->detach_comp);
  102. dev->state = CHCR_INIT;
  103. dev->wqretry = WQ_RETRY;
  104. atomic_inc(&drv_data.dev_count);
  105. atomic_set(&dev->inflight, 0);
  106. mutex_lock(&drv_data.drv_mutex);
  107. list_add_tail(&u_ctx->entry, &drv_data.inact_dev);
  108. mutex_unlock(&drv_data.drv_mutex);
  109. }
  110. static int chcr_dev_move(struct uld_ctx *u_ctx)
  111. {
  112. mutex_lock(&drv_data.drv_mutex);
  113. if (drv_data.last_dev == u_ctx) {
  114. if (list_is_last(&drv_data.last_dev->entry, &drv_data.act_dev))
  115. drv_data.last_dev = list_first_entry(&drv_data.act_dev,
  116. struct uld_ctx, entry);
  117. else
  118. drv_data.last_dev =
  119. list_next_entry(drv_data.last_dev, entry);
  120. }
  121. list_move(&u_ctx->entry, &drv_data.inact_dev);
  122. if (list_empty(&drv_data.act_dev))
  123. drv_data.last_dev = NULL;
  124. atomic_dec(&drv_data.dev_count);
  125. mutex_unlock(&drv_data.drv_mutex);
  126. return 0;
  127. }
  128. static int cpl_fw6_pld_handler(struct adapter *adap,
  129. unsigned char *input)
  130. {
  131. struct crypto_async_request *req;
  132. struct cpl_fw6_pld *fw6_pld;
  133. u32 ack_err_status = 0;
  134. int error_status = 0;
  135. fw6_pld = (struct cpl_fw6_pld *)input;
  136. req = (struct crypto_async_request *)(uintptr_t)be64_to_cpu(
  137. fw6_pld->data[1]);
  138. ack_err_status =
  139. ntohl(*(__be32 *)((unsigned char *)&fw6_pld->data[0] + 4));
  140. if (CHK_MAC_ERR_BIT(ack_err_status) || CHK_PAD_ERR_BIT(ack_err_status))
  141. error_status = -EBADMSG;
  142. /* call completion callback with failure status */
  143. if (req) {
  144. error_status = chcr_handle_resp(req, input, error_status);
  145. } else {
  146. pr_err("Incorrect request address from the firmware\n");
  147. return -EFAULT;
  148. }
  149. if (error_status)
  150. atomic_inc(&adap->chcr_stats.error);
  151. return 0;
  152. }
  153. int chcr_send_wr(struct sk_buff *skb)
  154. {
  155. return cxgb4_crypto_send(skb->dev, skb);
  156. }
  157. static void *chcr_uld_add(const struct cxgb4_lld_info *lld)
  158. {
  159. struct uld_ctx *u_ctx;
  160. /* Create the device and add it in the device list */
  161. pr_info_once("%s\n", DRV_DESC);
  162. if (!(lld->ulp_crypto & ULP_CRYPTO_LOOKASIDE))
  163. return ERR_PTR(-EOPNOTSUPP);
  164. /* Create the device and add it in the device list */
  165. u_ctx = kzalloc(sizeof(*u_ctx), GFP_KERNEL);
  166. if (!u_ctx) {
  167. u_ctx = ERR_PTR(-ENOMEM);
  168. goto out;
  169. }
  170. u_ctx->lldi = *lld;
  171. chcr_dev_init(u_ctx);
  172. out:
  173. return u_ctx;
  174. }
  175. int chcr_uld_rx_handler(void *handle, const __be64 *rsp,
  176. const struct pkt_gl *pgl)
  177. {
  178. struct uld_ctx *u_ctx = (struct uld_ctx *)handle;
  179. struct chcr_dev *dev = &u_ctx->dev;
  180. struct adapter *adap = padap(dev);
  181. const struct cpl_fw6_pld *rpl = (struct cpl_fw6_pld *)rsp;
  182. if (!work_handlers[rpl->opcode]) {
  183. pr_err("Unsupported opcode %d received\n", rpl->opcode);
  184. return 0;
  185. }
  186. if (!pgl)
  187. work_handlers[rpl->opcode](adap, (unsigned char *)&rsp[1]);
  188. else
  189. work_handlers[rpl->opcode](adap, pgl->va);
  190. return 0;
  191. }
  192. static void chcr_detach_device(struct uld_ctx *u_ctx)
  193. {
  194. struct chcr_dev *dev = &u_ctx->dev;
  195. if (dev->state == CHCR_DETACH) {
  196. pr_debug("Detached Event received for already detach device\n");
  197. return;
  198. }
  199. dev->state = CHCR_DETACH;
  200. if (atomic_read(&dev->inflight) != 0) {
  201. schedule_delayed_work(&dev->detach_work, WQ_DETACH_TM);
  202. wait_for_completion(&dev->detach_comp);
  203. }
  204. // Move u_ctx to inactive_dev list
  205. chcr_dev_move(u_ctx);
  206. }
  207. static int chcr_uld_state_change(void *handle, enum cxgb4_state state)
  208. {
  209. struct uld_ctx *u_ctx = handle;
  210. int ret = 0;
  211. switch (state) {
  212. case CXGB4_STATE_UP:
  213. if (u_ctx->dev.state != CHCR_INIT) {
  214. // ALready Initialised.
  215. return 0;
  216. }
  217. chcr_dev_add(u_ctx);
  218. ret = start_crypto();
  219. break;
  220. case CXGB4_STATE_DETACH:
  221. chcr_detach_device(u_ctx);
  222. if (!atomic_read(&drv_data.dev_count))
  223. stop_crypto();
  224. break;
  225. case CXGB4_STATE_START_RECOVERY:
  226. case CXGB4_STATE_DOWN:
  227. default:
  228. break;
  229. }
  230. return ret;
  231. }
  232. static int __init chcr_crypto_init(void)
  233. {
  234. INIT_LIST_HEAD(&drv_data.act_dev);
  235. INIT_LIST_HEAD(&drv_data.inact_dev);
  236. atomic_set(&drv_data.dev_count, 0);
  237. mutex_init(&drv_data.drv_mutex);
  238. drv_data.last_dev = NULL;
  239. cxgb4_register_uld(CXGB4_ULD_CRYPTO, &chcr_uld_info);
  240. return 0;
  241. }
  242. static void __exit chcr_crypto_exit(void)
  243. {
  244. struct uld_ctx *u_ctx, *tmp;
  245. struct adapter *adap;
  246. stop_crypto();
  247. cxgb4_unregister_uld(CXGB4_ULD_CRYPTO);
  248. /* Remove all devices from list */
  249. mutex_lock(&drv_data.drv_mutex);
  250. list_for_each_entry_safe(u_ctx, tmp, &drv_data.act_dev, entry) {
  251. adap = padap(&u_ctx->dev);
  252. memset(&adap->chcr_stats, 0, sizeof(adap->chcr_stats));
  253. list_del(&u_ctx->entry);
  254. kfree(u_ctx);
  255. }
  256. list_for_each_entry_safe(u_ctx, tmp, &drv_data.inact_dev, entry) {
  257. adap = padap(&u_ctx->dev);
  258. memset(&adap->chcr_stats, 0, sizeof(adap->chcr_stats));
  259. list_del(&u_ctx->entry);
  260. kfree(u_ctx);
  261. }
  262. mutex_unlock(&drv_data.drv_mutex);
  263. }
  264. module_init(chcr_crypto_init);
  265. module_exit(chcr_crypto_exit);
  266. MODULE_DESCRIPTION("Crypto Co-processor for Chelsio Terminator cards.");
  267. MODULE_LICENSE("GPL");
  268. MODULE_AUTHOR("Chelsio Communications");