chcr_core.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 LIST_HEAD(uld_ctx_list);
  26. static DEFINE_MUTEX(dev_mutex);
  27. static atomic_t dev_count;
  28. static struct uld_ctx *ctx_rr;
  29. typedef int (*chcr_handler_func)(struct chcr_dev *dev, unsigned char *input);
  30. static int cpl_fw6_pld_handler(struct chcr_dev *dev, unsigned char *input);
  31. static void *chcr_uld_add(const struct cxgb4_lld_info *lld);
  32. static int chcr_uld_state_change(void *handle, enum cxgb4_state state);
  33. static chcr_handler_func work_handlers[NUM_CPL_CMDS] = {
  34. [CPL_FW6_PLD] = cpl_fw6_pld_handler,
  35. };
  36. static struct cxgb4_uld_info chcr_uld_info = {
  37. .name = DRV_MODULE_NAME,
  38. .nrxq = MAX_ULD_QSETS,
  39. .ntxq = MAX_ULD_QSETS,
  40. .rxq_size = 1024,
  41. .add = chcr_uld_add,
  42. .state_change = chcr_uld_state_change,
  43. .rx_handler = chcr_uld_rx_handler,
  44. #ifdef CONFIG_CHELSIO_IPSEC_INLINE
  45. .tx_handler = chcr_uld_tx_handler,
  46. #endif /* CONFIG_CHELSIO_IPSEC_INLINE */
  47. };
  48. struct uld_ctx *assign_chcr_device(void)
  49. {
  50. struct uld_ctx *u_ctx = NULL;
  51. /*
  52. * When multiple devices are present in system select
  53. * device in round-robin fashion for crypto operations
  54. * Although One session must use the same device to
  55. * maintain request-response ordering.
  56. */
  57. mutex_lock(&dev_mutex);
  58. if (!list_empty(&uld_ctx_list)) {
  59. u_ctx = ctx_rr;
  60. if (list_is_last(&ctx_rr->entry, &uld_ctx_list))
  61. ctx_rr = list_first_entry(&uld_ctx_list,
  62. struct uld_ctx,
  63. entry);
  64. else
  65. ctx_rr = list_next_entry(ctx_rr, entry);
  66. }
  67. mutex_unlock(&dev_mutex);
  68. return u_ctx;
  69. }
  70. static int chcr_dev_add(struct uld_ctx *u_ctx)
  71. {
  72. struct chcr_dev *dev;
  73. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  74. if (!dev)
  75. return -ENXIO;
  76. spin_lock_init(&dev->lock_chcr_dev);
  77. u_ctx->dev = dev;
  78. dev->u_ctx = u_ctx;
  79. atomic_inc(&dev_count);
  80. mutex_lock(&dev_mutex);
  81. list_add_tail(&u_ctx->entry, &uld_ctx_list);
  82. if (!ctx_rr)
  83. ctx_rr = u_ctx;
  84. mutex_unlock(&dev_mutex);
  85. return 0;
  86. }
  87. static int chcr_dev_remove(struct uld_ctx *u_ctx)
  88. {
  89. if (ctx_rr == u_ctx) {
  90. if (list_is_last(&ctx_rr->entry, &uld_ctx_list))
  91. ctx_rr = list_first_entry(&uld_ctx_list,
  92. struct uld_ctx,
  93. entry);
  94. else
  95. ctx_rr = list_next_entry(ctx_rr, entry);
  96. }
  97. list_del(&u_ctx->entry);
  98. if (list_empty(&uld_ctx_list))
  99. ctx_rr = NULL;
  100. kfree(u_ctx->dev);
  101. u_ctx->dev = NULL;
  102. atomic_dec(&dev_count);
  103. return 0;
  104. }
  105. static int cpl_fw6_pld_handler(struct chcr_dev *dev,
  106. unsigned char *input)
  107. {
  108. struct crypto_async_request *req;
  109. struct cpl_fw6_pld *fw6_pld;
  110. u32 ack_err_status = 0;
  111. int error_status = 0;
  112. struct adapter *adap = padap(dev);
  113. fw6_pld = (struct cpl_fw6_pld *)input;
  114. req = (struct crypto_async_request *)(uintptr_t)be64_to_cpu(
  115. fw6_pld->data[1]);
  116. ack_err_status =
  117. ntohl(*(__be32 *)((unsigned char *)&fw6_pld->data[0] + 4));
  118. if (ack_err_status) {
  119. if (CHK_MAC_ERR_BIT(ack_err_status) ||
  120. CHK_PAD_ERR_BIT(ack_err_status))
  121. error_status = -EBADMSG;
  122. atomic_inc(&adap->chcr_stats.error);
  123. }
  124. /* call completion callback with failure status */
  125. if (req) {
  126. error_status = chcr_handle_resp(req, input, error_status);
  127. } else {
  128. pr_err("Incorrect request address from the firmware\n");
  129. return -EFAULT;
  130. }
  131. return 0;
  132. }
  133. int chcr_send_wr(struct sk_buff *skb)
  134. {
  135. return cxgb4_crypto_send(skb->dev, skb);
  136. }
  137. static void *chcr_uld_add(const struct cxgb4_lld_info *lld)
  138. {
  139. struct uld_ctx *u_ctx;
  140. /* Create the device and add it in the device list */
  141. if (!(lld->ulp_crypto & ULP_CRYPTO_LOOKASIDE))
  142. return ERR_PTR(-EOPNOTSUPP);
  143. /* Create the device and add it in the device list */
  144. u_ctx = kzalloc(sizeof(*u_ctx), GFP_KERNEL);
  145. if (!u_ctx) {
  146. u_ctx = ERR_PTR(-ENOMEM);
  147. goto out;
  148. }
  149. u_ctx->lldi = *lld;
  150. #ifdef CONFIG_CHELSIO_IPSEC_INLINE
  151. if (lld->crypto & ULP_CRYPTO_IPSEC_INLINE)
  152. chcr_add_xfrmops(lld);
  153. #endif /* CONFIG_CHELSIO_IPSEC_INLINE */
  154. out:
  155. return u_ctx;
  156. }
  157. int chcr_uld_rx_handler(void *handle, const __be64 *rsp,
  158. const struct pkt_gl *pgl)
  159. {
  160. struct uld_ctx *u_ctx = (struct uld_ctx *)handle;
  161. struct chcr_dev *dev = u_ctx->dev;
  162. const struct cpl_fw6_pld *rpl = (struct cpl_fw6_pld *)rsp;
  163. if (rpl->opcode != CPL_FW6_PLD) {
  164. pr_err("Unsupported opcode\n");
  165. return 0;
  166. }
  167. if (!pgl)
  168. work_handlers[rpl->opcode](dev, (unsigned char *)&rsp[1]);
  169. else
  170. work_handlers[rpl->opcode](dev, pgl->va);
  171. return 0;
  172. }
  173. #ifdef CONFIG_CHELSIO_IPSEC_INLINE
  174. int chcr_uld_tx_handler(struct sk_buff *skb, struct net_device *dev)
  175. {
  176. return chcr_ipsec_xmit(skb, dev);
  177. }
  178. #endif /* CONFIG_CHELSIO_IPSEC_INLINE */
  179. static int chcr_uld_state_change(void *handle, enum cxgb4_state state)
  180. {
  181. struct uld_ctx *u_ctx = handle;
  182. int ret = 0;
  183. switch (state) {
  184. case CXGB4_STATE_UP:
  185. if (!u_ctx->dev) {
  186. ret = chcr_dev_add(u_ctx);
  187. if (ret != 0)
  188. return ret;
  189. }
  190. if (atomic_read(&dev_count) == 1)
  191. ret = start_crypto();
  192. break;
  193. case CXGB4_STATE_DETACH:
  194. if (u_ctx->dev) {
  195. mutex_lock(&dev_mutex);
  196. chcr_dev_remove(u_ctx);
  197. mutex_unlock(&dev_mutex);
  198. }
  199. if (!atomic_read(&dev_count))
  200. stop_crypto();
  201. break;
  202. case CXGB4_STATE_START_RECOVERY:
  203. case CXGB4_STATE_DOWN:
  204. default:
  205. break;
  206. }
  207. return ret;
  208. }
  209. static int __init chcr_crypto_init(void)
  210. {
  211. if (cxgb4_register_uld(CXGB4_ULD_CRYPTO, &chcr_uld_info))
  212. pr_err("ULD register fail: No chcr crypto support in cxgb4\n");
  213. return 0;
  214. }
  215. static void __exit chcr_crypto_exit(void)
  216. {
  217. struct uld_ctx *u_ctx, *tmp;
  218. if (atomic_read(&dev_count))
  219. stop_crypto();
  220. /* Remove all devices from list */
  221. mutex_lock(&dev_mutex);
  222. list_for_each_entry_safe(u_ctx, tmp, &uld_ctx_list, entry) {
  223. if (u_ctx->dev)
  224. chcr_dev_remove(u_ctx);
  225. kfree(u_ctx);
  226. }
  227. mutex_unlock(&dev_mutex);
  228. cxgb4_unregister_uld(CXGB4_ULD_CRYPTO);
  229. }
  230. module_init(chcr_crypto_init);
  231. module_exit(chcr_crypto_exit);
  232. MODULE_DESCRIPTION("Crypto Co-processor for Chelsio Terminator cards.");
  233. MODULE_LICENSE("GPL");
  234. MODULE_AUTHOR("Chelsio Communications");
  235. MODULE_VERSION(DRV_VERSION);