chcr_core.c 5.4 KB

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