hns_roce_cmd.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 STATUS_MASK 0xff
  40. #define CMD_TOKEN_MASK 0x1f
  41. #define GO_BIT_TIMEOUT_MSECS 10000
  42. enum {
  43. HCR_TOKEN_OFFSET = 0x14,
  44. HCR_STATUS_OFFSET = 0x18,
  45. HCR_GO_BIT = 15,
  46. };
  47. static int cmd_pending(struct hns_roce_dev *hr_dev)
  48. {
  49. u32 status = readl(hr_dev->cmd.hcr + HCR_TOKEN_OFFSET);
  50. return (!!(status & (1 << HCR_GO_BIT)));
  51. }
  52. /* this function should be serialized with "hcr_mutex" */
  53. static int __hns_roce_cmd_mbox_post_hw(struct hns_roce_dev *hr_dev,
  54. u64 in_param, u64 out_param,
  55. u32 in_modifier, u8 op_modifier, u16 op,
  56. u16 token, int event)
  57. {
  58. struct hns_roce_cmdq *cmd = &hr_dev->cmd;
  59. struct device *dev = &hr_dev->pdev->dev;
  60. u32 __iomem *hcr = (u32 *)cmd->hcr;
  61. int ret = -EAGAIN;
  62. unsigned long end;
  63. u32 val = 0;
  64. end = msecs_to_jiffies(GO_BIT_TIMEOUT_MSECS) + jiffies;
  65. while (cmd_pending(hr_dev)) {
  66. if (time_after(jiffies, end)) {
  67. dev_dbg(dev, "jiffies=%d end=%d\n", (int)jiffies,
  68. (int)end);
  69. goto out;
  70. }
  71. cond_resched();
  72. }
  73. roce_set_field(val, ROCEE_MB6_ROCEE_MB_CMD_M, ROCEE_MB6_ROCEE_MB_CMD_S,
  74. op);
  75. roce_set_field(val, ROCEE_MB6_ROCEE_MB_CMD_MDF_M,
  76. ROCEE_MB6_ROCEE_MB_CMD_MDF_S, op_modifier);
  77. roce_set_bit(val, ROCEE_MB6_ROCEE_MB_EVENT_S, event);
  78. roce_set_bit(val, ROCEE_MB6_ROCEE_MB_HW_RUN_S, 1);
  79. roce_set_field(val, ROCEE_MB6_ROCEE_MB_TOKEN_M,
  80. ROCEE_MB6_ROCEE_MB_TOKEN_S, token);
  81. __raw_writeq(cpu_to_le64(in_param), hcr + 0);
  82. __raw_writeq(cpu_to_le64(out_param), hcr + 2);
  83. __raw_writel(cpu_to_le32(in_modifier), hcr + 4);
  84. /* Memory barrier */
  85. wmb();
  86. __raw_writel(cpu_to_le32(val), hcr + 5);
  87. mmiowb();
  88. ret = 0;
  89. out:
  90. return ret;
  91. }
  92. static int hns_roce_cmd_mbox_post_hw(struct hns_roce_dev *hr_dev, u64 in_param,
  93. u64 out_param, u32 in_modifier,
  94. u8 op_modifier, u16 op, u16 token,
  95. int event)
  96. {
  97. struct hns_roce_cmdq *cmd = &hr_dev->cmd;
  98. int ret = -EAGAIN;
  99. mutex_lock(&cmd->hcr_mutex);
  100. ret = __hns_roce_cmd_mbox_post_hw(hr_dev, in_param, out_param,
  101. in_modifier, op_modifier, op, token,
  102. event);
  103. mutex_unlock(&cmd->hcr_mutex);
  104. return ret;
  105. }
  106. /* this should be called with "poll_sem" */
  107. static int __hns_roce_cmd_mbox_poll(struct hns_roce_dev *hr_dev, u64 in_param,
  108. u64 out_param, unsigned long in_modifier,
  109. u8 op_modifier, u16 op,
  110. unsigned long timeout)
  111. {
  112. struct device *dev = &hr_dev->pdev->dev;
  113. u8 __iomem *hcr = hr_dev->cmd.hcr;
  114. unsigned long end = 0;
  115. u32 status = 0;
  116. int ret;
  117. ret = hns_roce_cmd_mbox_post_hw(hr_dev, in_param, out_param,
  118. in_modifier, op_modifier, op,
  119. CMD_POLL_TOKEN, 0);
  120. if (ret) {
  121. dev_err(dev, "[cmd_poll]hns_roce_cmd_mbox_post_hw failed\n");
  122. goto out;
  123. }
  124. end = msecs_to_jiffies(timeout) + jiffies;
  125. while (cmd_pending(hr_dev) && time_before(jiffies, end))
  126. cond_resched();
  127. if (cmd_pending(hr_dev)) {
  128. dev_err(dev, "[cmd_poll]hw run cmd TIMEDOUT!\n");
  129. ret = -ETIMEDOUT;
  130. goto out;
  131. }
  132. status = le32_to_cpu((__force __be32)
  133. __raw_readl(hcr + HCR_STATUS_OFFSET));
  134. if ((status & STATUS_MASK) != 0x1) {
  135. dev_err(dev, "mailbox status 0x%x!\n", status);
  136. ret = -EBUSY;
  137. goto out;
  138. }
  139. out:
  140. return ret;
  141. }
  142. static int hns_roce_cmd_mbox_poll(struct hns_roce_dev *hr_dev, u64 in_param,
  143. u64 out_param, unsigned long in_modifier,
  144. u8 op_modifier, u16 op, unsigned long timeout)
  145. {
  146. int ret;
  147. down(&hr_dev->cmd.poll_sem);
  148. ret = __hns_roce_cmd_mbox_poll(hr_dev, in_param, out_param, in_modifier,
  149. op_modifier, op, timeout);
  150. up(&hr_dev->cmd.poll_sem);
  151. return ret;
  152. }
  153. void hns_roce_cmd_event(struct hns_roce_dev *hr_dev, u16 token, u8 status,
  154. u64 out_param)
  155. {
  156. struct hns_roce_cmd_context
  157. *context = &hr_dev->cmd.context[token & hr_dev->cmd.token_mask];
  158. if (token != context->token)
  159. return;
  160. context->result = (status == HNS_ROCE_CMD_SUCCESS) ? 0 : (-EIO);
  161. context->out_param = out_param;
  162. complete(&context->done);
  163. }
  164. /* this should be called with "use_events" */
  165. static int __hns_roce_cmd_mbox_wait(struct hns_roce_dev *hr_dev, u64 in_param,
  166. u64 out_param, unsigned long in_modifier,
  167. u8 op_modifier, u16 op,
  168. unsigned long timeout)
  169. {
  170. struct hns_roce_cmdq *cmd = &hr_dev->cmd;
  171. struct device *dev = &hr_dev->pdev->dev;
  172. struct hns_roce_cmd_context *context;
  173. int ret = 0;
  174. spin_lock(&cmd->context_lock);
  175. WARN_ON(cmd->free_head < 0);
  176. context = &cmd->context[cmd->free_head];
  177. context->token += cmd->token_mask + 1;
  178. cmd->free_head = context->next;
  179. spin_unlock(&cmd->context_lock);
  180. init_completion(&context->done);
  181. ret = hns_roce_cmd_mbox_post_hw(hr_dev, in_param, out_param,
  182. in_modifier, op_modifier, op,
  183. context->token, 1);
  184. if (ret)
  185. goto out;
  186. /*
  187. * It is timeout when wait_for_completion_timeout return 0
  188. * The return value is the time limit set in advance
  189. * how many seconds showing
  190. */
  191. if (!wait_for_completion_timeout(&context->done,
  192. msecs_to_jiffies(timeout))) {
  193. dev_err(dev, "[cmd]wait_for_completion_timeout timeout\n");
  194. ret = -EBUSY;
  195. goto out;
  196. }
  197. ret = context->result;
  198. if (ret) {
  199. dev_err(dev, "[cmd]event mod cmd process error!err=%d\n", ret);
  200. goto out;
  201. }
  202. out:
  203. spin_lock(&cmd->context_lock);
  204. context->next = cmd->free_head;
  205. cmd->free_head = context - cmd->context;
  206. spin_unlock(&cmd->context_lock);
  207. return ret;
  208. }
  209. static int hns_roce_cmd_mbox_wait(struct hns_roce_dev *hr_dev, u64 in_param,
  210. u64 out_param, unsigned long in_modifier,
  211. u8 op_modifier, u16 op, unsigned long timeout)
  212. {
  213. int ret = 0;
  214. down(&hr_dev->cmd.event_sem);
  215. ret = __hns_roce_cmd_mbox_wait(hr_dev, in_param, out_param,
  216. in_modifier, op_modifier, op, timeout);
  217. up(&hr_dev->cmd.event_sem);
  218. return ret;
  219. }
  220. int hns_roce_cmd_mbox(struct hns_roce_dev *hr_dev, u64 in_param, u64 out_param,
  221. unsigned long in_modifier, u8 op_modifier, u16 op,
  222. unsigned long timeout)
  223. {
  224. if (hr_dev->cmd.use_events)
  225. return hns_roce_cmd_mbox_wait(hr_dev, in_param, out_param,
  226. in_modifier, op_modifier, op,
  227. timeout);
  228. else
  229. return hns_roce_cmd_mbox_poll(hr_dev, in_param, out_param,
  230. in_modifier, op_modifier, op,
  231. timeout);
  232. }
  233. int hns_roce_cmd_init(struct hns_roce_dev *hr_dev)
  234. {
  235. struct device *dev = &hr_dev->pdev->dev;
  236. mutex_init(&hr_dev->cmd.hcr_mutex);
  237. sema_init(&hr_dev->cmd.poll_sem, 1);
  238. hr_dev->cmd.use_events = 0;
  239. hr_dev->cmd.toggle = 1;
  240. hr_dev->cmd.max_cmds = CMD_MAX_NUM;
  241. hr_dev->cmd.hcr = hr_dev->reg_base + ROCEE_MB1_REG;
  242. hr_dev->cmd.pool = dma_pool_create("hns_roce_cmd", dev,
  243. HNS_ROCE_MAILBOX_SIZE,
  244. HNS_ROCE_MAILBOX_SIZE, 0);
  245. if (!hr_dev->cmd.pool)
  246. return -ENOMEM;
  247. return 0;
  248. }
  249. void hns_roce_cmd_cleanup(struct hns_roce_dev *hr_dev)
  250. {
  251. dma_pool_destroy(hr_dev->cmd.pool);
  252. }
  253. int hns_roce_cmd_use_events(struct hns_roce_dev *hr_dev)
  254. {
  255. struct hns_roce_cmdq *hr_cmd = &hr_dev->cmd;
  256. int i;
  257. hr_cmd->context = kmalloc(hr_cmd->max_cmds *
  258. sizeof(struct hns_roce_cmd_context),
  259. GFP_KERNEL);
  260. if (!hr_cmd->context)
  261. return -ENOMEM;
  262. for (i = 0; i < hr_cmd->max_cmds; ++i) {
  263. hr_cmd->context[i].token = i;
  264. hr_cmd->context[i].next = i + 1;
  265. }
  266. hr_cmd->context[hr_cmd->max_cmds - 1].next = -1;
  267. hr_cmd->free_head = 0;
  268. sema_init(&hr_cmd->event_sem, hr_cmd->max_cmds);
  269. spin_lock_init(&hr_cmd->context_lock);
  270. hr_cmd->token_mask = CMD_TOKEN_MASK;
  271. hr_cmd->use_events = 1;
  272. down(&hr_cmd->poll_sem);
  273. return 0;
  274. }
  275. void hns_roce_cmd_use_polling(struct hns_roce_dev *hr_dev)
  276. {
  277. struct hns_roce_cmdq *hr_cmd = &hr_dev->cmd;
  278. int i;
  279. hr_cmd->use_events = 0;
  280. for (i = 0; i < hr_cmd->max_cmds; ++i)
  281. down(&hr_cmd->event_sem);
  282. kfree(hr_cmd->context);
  283. up(&hr_cmd->poll_sem);
  284. }
  285. struct hns_roce_cmd_mailbox
  286. *hns_roce_alloc_cmd_mailbox(struct hns_roce_dev *hr_dev)
  287. {
  288. struct hns_roce_cmd_mailbox *mailbox;
  289. mailbox = kmalloc(sizeof(*mailbox), GFP_KERNEL);
  290. if (!mailbox)
  291. return ERR_PTR(-ENOMEM);
  292. mailbox->buf = dma_pool_alloc(hr_dev->cmd.pool, GFP_KERNEL,
  293. &mailbox->dma);
  294. if (!mailbox->buf) {
  295. kfree(mailbox);
  296. return ERR_PTR(-ENOMEM);
  297. }
  298. return mailbox;
  299. }
  300. void hns_roce_free_cmd_mailbox(struct hns_roce_dev *hr_dev,
  301. struct hns_roce_cmd_mailbox *mailbox)
  302. {
  303. if (!mailbox)
  304. return;
  305. dma_pool_free(hr_dev->cmd.pool, mailbox->buf, mailbox->dma);
  306. kfree(mailbox);
  307. }