hns_roce_cmd.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (c) 2016 Hisilicon Limited.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/dmapool.h>
  33. #include <linux/platform_device.h>
  34. #include "hns_roce_common.h"
  35. #include "hns_roce_device.h"
  36. #include "hns_roce_cmd.h"
  37. #define CMD_POLL_TOKEN 0xffff
  38. #define CMD_MAX_NUM 32
  39. #define CMD_TOKEN_MASK 0x1f
  40. static int hns_roce_cmd_mbox_post_hw(struct hns_roce_dev *hr_dev, u64 in_param,
  41. u64 out_param, u32 in_modifier,
  42. u8 op_modifier, u16 op, u16 token,
  43. int event)
  44. {
  45. struct hns_roce_cmdq *cmd = &hr_dev->cmd;
  46. int ret;
  47. mutex_lock(&cmd->hcr_mutex);
  48. ret = hr_dev->hw->post_mbox(hr_dev, in_param, out_param, in_modifier,
  49. op_modifier, op, token, event);
  50. mutex_unlock(&cmd->hcr_mutex);
  51. return ret;
  52. }
  53. /* this should be called with "poll_sem" */
  54. static int __hns_roce_cmd_mbox_poll(struct hns_roce_dev *hr_dev, u64 in_param,
  55. u64 out_param, unsigned long in_modifier,
  56. u8 op_modifier, u16 op,
  57. unsigned long timeout)
  58. {
  59. struct device *dev = hr_dev->dev;
  60. int ret;
  61. ret = hns_roce_cmd_mbox_post_hw(hr_dev, in_param, out_param,
  62. in_modifier, op_modifier, op,
  63. CMD_POLL_TOKEN, 0);
  64. if (ret) {
  65. dev_err(dev, "[cmd_poll]hns_roce_cmd_mbox_post_hw failed\n");
  66. return ret;
  67. }
  68. return hr_dev->hw->chk_mbox(hr_dev, timeout);
  69. }
  70. static int hns_roce_cmd_mbox_poll(struct hns_roce_dev *hr_dev, u64 in_param,
  71. u64 out_param, unsigned long in_modifier,
  72. u8 op_modifier, u16 op, unsigned long timeout)
  73. {
  74. int ret;
  75. down(&hr_dev->cmd.poll_sem);
  76. ret = __hns_roce_cmd_mbox_poll(hr_dev, in_param, out_param, in_modifier,
  77. op_modifier, op, timeout);
  78. up(&hr_dev->cmd.poll_sem);
  79. return ret;
  80. }
  81. void hns_roce_cmd_event(struct hns_roce_dev *hr_dev, u16 token, u8 status,
  82. u64 out_param)
  83. {
  84. struct hns_roce_cmd_context
  85. *context = &hr_dev->cmd.context[token & hr_dev->cmd.token_mask];
  86. if (token != context->token)
  87. return;
  88. context->result = (status == HNS_ROCE_CMD_SUCCESS) ? 0 : (-EIO);
  89. context->out_param = out_param;
  90. complete(&context->done);
  91. }
  92. EXPORT_SYMBOL_GPL(hns_roce_cmd_event);
  93. /* this should be called with "use_events" */
  94. static int __hns_roce_cmd_mbox_wait(struct hns_roce_dev *hr_dev, u64 in_param,
  95. u64 out_param, unsigned long in_modifier,
  96. u8 op_modifier, u16 op,
  97. unsigned long timeout)
  98. {
  99. struct hns_roce_cmdq *cmd = &hr_dev->cmd;
  100. struct hns_roce_cmd_context *context;
  101. struct device *dev = hr_dev->dev;
  102. int ret;
  103. spin_lock(&cmd->context_lock);
  104. WARN_ON(cmd->free_head < 0);
  105. context = &cmd->context[cmd->free_head];
  106. context->token += cmd->token_mask + 1;
  107. cmd->free_head = context->next;
  108. spin_unlock(&cmd->context_lock);
  109. init_completion(&context->done);
  110. ret = hns_roce_cmd_mbox_post_hw(hr_dev, in_param, out_param,
  111. in_modifier, op_modifier, op,
  112. context->token, 1);
  113. if (ret)
  114. goto out;
  115. /*
  116. * It is timeout when wait_for_completion_timeout return 0
  117. * The return value is the time limit set in advance
  118. * how many seconds showing
  119. */
  120. if (!wait_for_completion_timeout(&context->done,
  121. msecs_to_jiffies(timeout))) {
  122. dev_err(dev, "[cmd]wait_for_completion_timeout timeout\n");
  123. ret = -EBUSY;
  124. goto out;
  125. }
  126. ret = context->result;
  127. if (ret) {
  128. dev_err(dev, "[cmd]event mod cmd process error!err=%d\n", ret);
  129. goto out;
  130. }
  131. out:
  132. spin_lock(&cmd->context_lock);
  133. context->next = cmd->free_head;
  134. cmd->free_head = context - cmd->context;
  135. spin_unlock(&cmd->context_lock);
  136. return ret;
  137. }
  138. static int hns_roce_cmd_mbox_wait(struct hns_roce_dev *hr_dev, u64 in_param,
  139. u64 out_param, unsigned long in_modifier,
  140. u8 op_modifier, u16 op, unsigned long timeout)
  141. {
  142. int ret = 0;
  143. down(&hr_dev->cmd.event_sem);
  144. ret = __hns_roce_cmd_mbox_wait(hr_dev, in_param, out_param,
  145. in_modifier, op_modifier, op, timeout);
  146. up(&hr_dev->cmd.event_sem);
  147. return ret;
  148. }
  149. int hns_roce_cmd_mbox(struct hns_roce_dev *hr_dev, u64 in_param, u64 out_param,
  150. unsigned long in_modifier, u8 op_modifier, u16 op,
  151. unsigned long timeout)
  152. {
  153. if (hr_dev->is_reset)
  154. return 0;
  155. if (hr_dev->cmd.use_events)
  156. return hns_roce_cmd_mbox_wait(hr_dev, in_param, out_param,
  157. in_modifier, op_modifier, op,
  158. timeout);
  159. else
  160. return hns_roce_cmd_mbox_poll(hr_dev, in_param, out_param,
  161. in_modifier, op_modifier, op,
  162. timeout);
  163. }
  164. EXPORT_SYMBOL_GPL(hns_roce_cmd_mbox);
  165. int hns_roce_cmd_init(struct hns_roce_dev *hr_dev)
  166. {
  167. struct device *dev = hr_dev->dev;
  168. mutex_init(&hr_dev->cmd.hcr_mutex);
  169. sema_init(&hr_dev->cmd.poll_sem, 1);
  170. hr_dev->cmd.use_events = 0;
  171. hr_dev->cmd.toggle = 1;
  172. hr_dev->cmd.max_cmds = CMD_MAX_NUM;
  173. hr_dev->cmd.pool = dma_pool_create("hns_roce_cmd", dev,
  174. HNS_ROCE_MAILBOX_SIZE,
  175. HNS_ROCE_MAILBOX_SIZE, 0);
  176. if (!hr_dev->cmd.pool)
  177. return -ENOMEM;
  178. return 0;
  179. }
  180. void hns_roce_cmd_cleanup(struct hns_roce_dev *hr_dev)
  181. {
  182. dma_pool_destroy(hr_dev->cmd.pool);
  183. }
  184. int hns_roce_cmd_use_events(struct hns_roce_dev *hr_dev)
  185. {
  186. struct hns_roce_cmdq *hr_cmd = &hr_dev->cmd;
  187. int i;
  188. hr_cmd->context = kmalloc_array(hr_cmd->max_cmds,
  189. sizeof(*hr_cmd->context),
  190. GFP_KERNEL);
  191. if (!hr_cmd->context)
  192. return -ENOMEM;
  193. for (i = 0; i < hr_cmd->max_cmds; ++i) {
  194. hr_cmd->context[i].token = i;
  195. hr_cmd->context[i].next = i + 1;
  196. }
  197. hr_cmd->context[hr_cmd->max_cmds - 1].next = -1;
  198. hr_cmd->free_head = 0;
  199. sema_init(&hr_cmd->event_sem, hr_cmd->max_cmds);
  200. spin_lock_init(&hr_cmd->context_lock);
  201. hr_cmd->token_mask = CMD_TOKEN_MASK;
  202. hr_cmd->use_events = 1;
  203. down(&hr_cmd->poll_sem);
  204. return 0;
  205. }
  206. void hns_roce_cmd_use_polling(struct hns_roce_dev *hr_dev)
  207. {
  208. struct hns_roce_cmdq *hr_cmd = &hr_dev->cmd;
  209. int i;
  210. hr_cmd->use_events = 0;
  211. for (i = 0; i < hr_cmd->max_cmds; ++i)
  212. down(&hr_cmd->event_sem);
  213. kfree(hr_cmd->context);
  214. up(&hr_cmd->poll_sem);
  215. }
  216. struct hns_roce_cmd_mailbox
  217. *hns_roce_alloc_cmd_mailbox(struct hns_roce_dev *hr_dev)
  218. {
  219. struct hns_roce_cmd_mailbox *mailbox;
  220. mailbox = kmalloc(sizeof(*mailbox), GFP_KERNEL);
  221. if (!mailbox)
  222. return ERR_PTR(-ENOMEM);
  223. mailbox->buf = dma_pool_alloc(hr_dev->cmd.pool, GFP_KERNEL,
  224. &mailbox->dma);
  225. if (!mailbox->buf) {
  226. kfree(mailbox);
  227. return ERR_PTR(-ENOMEM);
  228. }
  229. return mailbox;
  230. }
  231. EXPORT_SYMBOL_GPL(hns_roce_alloc_cmd_mailbox);
  232. void hns_roce_free_cmd_mailbox(struct hns_roce_dev *hr_dev,
  233. struct hns_roce_cmd_mailbox *mailbox)
  234. {
  235. if (!mailbox)
  236. return;
  237. dma_pool_free(hr_dev->cmd.pool, mailbox->buf, mailbox->dma);
  238. kfree(mailbox);
  239. }
  240. EXPORT_SYMBOL_GPL(hns_roce_free_cmd_mailbox);