snet_ctrl.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SolidRun DPU driver for control plane
  4. *
  5. * Copyright (C) 2022-2023 SolidRun
  6. *
  7. * Author: Alvaro Karsz <alvaro.karsz@solid-run.com>
  8. *
  9. */
  10. #include <linux/iopoll.h>
  11. #include "snet_vdpa.h"
  12. enum snet_ctrl_opcodes {
  13. SNET_CTRL_OP_DESTROY = 1,
  14. SNET_CTRL_OP_READ_VQ_STATE,
  15. SNET_CTRL_OP_SUSPEND,
  16. SNET_CTRL_OP_RESUME,
  17. };
  18. #define SNET_CTRL_TIMEOUT 2000000
  19. #define SNET_CTRL_DATA_SIZE_MASK 0x0000FFFF
  20. #define SNET_CTRL_IN_PROCESS_MASK 0x00010000
  21. #define SNET_CTRL_CHUNK_RDY_MASK 0x00020000
  22. #define SNET_CTRL_ERROR_MASK 0x0FFC0000
  23. #define SNET_VAL_TO_ERR(val) (-(((val) & SNET_CTRL_ERROR_MASK) >> 18))
  24. #define SNET_EMPTY_CTRL(val) (((val) & SNET_CTRL_ERROR_MASK) || \
  25. !((val) & SNET_CTRL_IN_PROCESS_MASK))
  26. #define SNET_DATA_READY(val) ((val) & (SNET_CTRL_ERROR_MASK | SNET_CTRL_CHUNK_RDY_MASK))
  27. /* Control register used to read data from the DPU */
  28. struct snet_ctrl_reg_ctrl {
  29. /* Chunk size in 4B words */
  30. u16 data_size;
  31. /* We are in the middle of a command */
  32. u16 in_process:1;
  33. /* A data chunk is ready and can be consumed */
  34. u16 chunk_ready:1;
  35. /* Error code */
  36. u16 error:10;
  37. /* Saved for future usage */
  38. u16 rsvd:4;
  39. };
  40. /* Opcode register */
  41. struct snet_ctrl_reg_op {
  42. u16 opcode;
  43. /* Only if VQ index is relevant for the command */
  44. u16 vq_idx;
  45. };
  46. struct snet_ctrl_regs {
  47. struct snet_ctrl_reg_op op;
  48. struct snet_ctrl_reg_ctrl ctrl;
  49. u32 rsvd;
  50. u32 data[];
  51. };
  52. static struct snet_ctrl_regs __iomem *snet_get_ctrl(struct snet *snet)
  53. {
  54. return snet->bar + snet->psnet->cfg.ctrl_off;
  55. }
  56. static int snet_wait_for_empty_ctrl(struct snet_ctrl_regs __iomem *regs)
  57. {
  58. u32 val;
  59. return readx_poll_timeout(ioread32, &regs->ctrl, val, SNET_EMPTY_CTRL(val), 10,
  60. SNET_CTRL_TIMEOUT);
  61. }
  62. static int snet_wait_for_empty_op(struct snet_ctrl_regs __iomem *regs)
  63. {
  64. u32 val;
  65. return readx_poll_timeout(ioread32, &regs->op, val, !val, 10, SNET_CTRL_TIMEOUT);
  66. }
  67. static int snet_wait_for_data(struct snet_ctrl_regs __iomem *regs)
  68. {
  69. u32 val;
  70. return readx_poll_timeout(ioread32, &regs->ctrl, val, SNET_DATA_READY(val), 10,
  71. SNET_CTRL_TIMEOUT);
  72. }
  73. static u32 snet_read32_word(struct snet_ctrl_regs __iomem *ctrl_regs, u16 word_idx)
  74. {
  75. return ioread32(&ctrl_regs->data[word_idx]);
  76. }
  77. static u32 snet_read_ctrl(struct snet_ctrl_regs __iomem *ctrl_regs)
  78. {
  79. return ioread32(&ctrl_regs->ctrl);
  80. }
  81. static void snet_write_ctrl(struct snet_ctrl_regs __iomem *ctrl_regs, u32 val)
  82. {
  83. iowrite32(val, &ctrl_regs->ctrl);
  84. }
  85. static void snet_write_op(struct snet_ctrl_regs __iomem *ctrl_regs, u32 val)
  86. {
  87. iowrite32(val, &ctrl_regs->op);
  88. }
  89. static int snet_wait_for_dpu_completion(struct snet_ctrl_regs __iomem *ctrl_regs)
  90. {
  91. /* Wait until the DPU finishes completely.
  92. * It will clear the opcode register.
  93. */
  94. return snet_wait_for_empty_op(ctrl_regs);
  95. }
  96. /* Reading ctrl from the DPU:
  97. * buf_size must be 4B aligned
  98. *
  99. * Steps:
  100. *
  101. * (1) Verify that the DPU is not in the middle of another operation by
  102. * reading the in_process and error bits in the control register.
  103. * (2) Write the request opcode and the VQ idx in the opcode register
  104. * and write the buffer size in the control register.
  105. * (3) Start readind chunks of data, chunk_ready bit indicates that a
  106. * data chunk is available, we signal that we read the data by clearing the bit.
  107. * (4) Detect that the transfer is completed when the in_process bit
  108. * in the control register is cleared or when the an error appears.
  109. */
  110. static int snet_ctrl_read_from_dpu(struct snet *snet, u16 opcode, u16 vq_idx, void *buffer,
  111. u32 buf_size)
  112. {
  113. struct pci_dev *pdev = snet->pdev;
  114. struct snet_ctrl_regs __iomem *regs = snet_get_ctrl(snet);
  115. u32 *bfr_ptr = (u32 *)buffer;
  116. u32 val;
  117. u16 buf_words;
  118. int ret;
  119. u16 words, i, tot_words = 0;
  120. /* Supported for config 2+ */
  121. if (!SNET_CFG_VER(snet, 2))
  122. return -EOPNOTSUPP;
  123. if (!IS_ALIGNED(buf_size, 4))
  124. return -EINVAL;
  125. mutex_lock(&snet->ctrl_lock);
  126. buf_words = buf_size / 4;
  127. /* Make sure control register is empty */
  128. ret = snet_wait_for_empty_ctrl(regs);
  129. if (ret) {
  130. SNET_WARN(pdev, "Timeout waiting for previous control data to be consumed\n");
  131. goto exit;
  132. }
  133. /* We need to write the buffer size in the control register, and the opcode + vq index in
  134. * the opcode register.
  135. * We use a spinlock to serialize the writes.
  136. */
  137. spin_lock(&snet->ctrl_spinlock);
  138. snet_write_ctrl(regs, buf_words);
  139. snet_write_op(regs, opcode | (vq_idx << 16));
  140. spin_unlock(&snet->ctrl_spinlock);
  141. while (buf_words != tot_words) {
  142. ret = snet_wait_for_data(regs);
  143. if (ret) {
  144. SNET_WARN(pdev, "Timeout waiting for control data\n");
  145. goto exit;
  146. }
  147. val = snet_read_ctrl(regs);
  148. /* Error? */
  149. if (val & SNET_CTRL_ERROR_MASK) {
  150. ret = SNET_VAL_TO_ERR(val);
  151. SNET_WARN(pdev, "Error while reading control data from DPU, err %d\n", ret);
  152. goto exit;
  153. }
  154. words = min_t(u16, val & SNET_CTRL_DATA_SIZE_MASK, buf_words - tot_words);
  155. for (i = 0; i < words; i++) {
  156. *bfr_ptr = snet_read32_word(regs, i);
  157. bfr_ptr++;
  158. }
  159. tot_words += words;
  160. /* Is the job completed? */
  161. if (!(val & SNET_CTRL_IN_PROCESS_MASK))
  162. break;
  163. /* Clear the chunk ready bit and continue */
  164. val &= ~SNET_CTRL_CHUNK_RDY_MASK;
  165. snet_write_ctrl(regs, val);
  166. }
  167. ret = snet_wait_for_dpu_completion(regs);
  168. if (ret)
  169. SNET_WARN(pdev, "Timeout waiting for the DPU to complete a control command\n");
  170. exit:
  171. mutex_unlock(&snet->ctrl_lock);
  172. return ret;
  173. }
  174. /* Send a control message to the DPU using the old mechanism
  175. * used with config version 1.
  176. */
  177. static int snet_send_ctrl_msg_old(struct snet *snet, u32 opcode)
  178. {
  179. struct pci_dev *pdev = snet->pdev;
  180. struct snet_ctrl_regs __iomem *regs = snet_get_ctrl(snet);
  181. int ret;
  182. mutex_lock(&snet->ctrl_lock);
  183. /* Old mechanism uses just 1 register, the opcode register.
  184. * Make sure that the opcode register is empty, and that the DPU isn't
  185. * processing an old message.
  186. */
  187. ret = snet_wait_for_empty_op(regs);
  188. if (ret) {
  189. SNET_WARN(pdev, "Timeout waiting for previous control message to be ACKed\n");
  190. goto exit;
  191. }
  192. /* Write the message */
  193. snet_write_op(regs, opcode);
  194. /* DPU ACKs the message by clearing the opcode register */
  195. ret = snet_wait_for_empty_op(regs);
  196. if (ret)
  197. SNET_WARN(pdev, "Timeout waiting for a control message to be ACKed\n");
  198. exit:
  199. mutex_unlock(&snet->ctrl_lock);
  200. return ret;
  201. }
  202. /* Send a control message to the DPU.
  203. * A control message is a message without payload.
  204. */
  205. static int snet_send_ctrl_msg(struct snet *snet, u16 opcode, u16 vq_idx)
  206. {
  207. struct pci_dev *pdev = snet->pdev;
  208. struct snet_ctrl_regs __iomem *regs = snet_get_ctrl(snet);
  209. u32 val;
  210. int ret;
  211. /* If config version is not 2+, use the old mechanism */
  212. if (!SNET_CFG_VER(snet, 2))
  213. return snet_send_ctrl_msg_old(snet, opcode);
  214. mutex_lock(&snet->ctrl_lock);
  215. /* Make sure control register is empty */
  216. ret = snet_wait_for_empty_ctrl(regs);
  217. if (ret) {
  218. SNET_WARN(pdev, "Timeout waiting for previous control data to be consumed\n");
  219. goto exit;
  220. }
  221. /* We need to clear the control register and write the opcode + vq index in the opcode
  222. * register.
  223. * We use a spinlock to serialize the writes.
  224. */
  225. spin_lock(&snet->ctrl_spinlock);
  226. snet_write_ctrl(regs, 0);
  227. snet_write_op(regs, opcode | (vq_idx << 16));
  228. spin_unlock(&snet->ctrl_spinlock);
  229. /* The DPU ACKs control messages by setting the chunk ready bit
  230. * without data.
  231. */
  232. ret = snet_wait_for_data(regs);
  233. if (ret) {
  234. SNET_WARN(pdev, "Timeout waiting for control message to be ACKed\n");
  235. goto exit;
  236. }
  237. /* Check for errors */
  238. val = snet_read_ctrl(regs);
  239. ret = SNET_VAL_TO_ERR(val);
  240. /* Clear the chunk ready bit */
  241. val &= ~SNET_CTRL_CHUNK_RDY_MASK;
  242. snet_write_ctrl(regs, val);
  243. ret = snet_wait_for_dpu_completion(regs);
  244. if (ret)
  245. SNET_WARN(pdev, "Timeout waiting for DPU to complete a control command, err %d\n",
  246. ret);
  247. exit:
  248. mutex_unlock(&snet->ctrl_lock);
  249. return ret;
  250. }
  251. void snet_ctrl_clear(struct snet *snet)
  252. {
  253. struct snet_ctrl_regs __iomem *regs = snet_get_ctrl(snet);
  254. snet_write_op(regs, 0);
  255. }
  256. int snet_destroy_dev(struct snet *snet)
  257. {
  258. return snet_send_ctrl_msg(snet, SNET_CTRL_OP_DESTROY, 0);
  259. }
  260. int snet_read_vq_state(struct snet *snet, u16 idx, struct vdpa_vq_state *state)
  261. {
  262. return snet_ctrl_read_from_dpu(snet, SNET_CTRL_OP_READ_VQ_STATE, idx, state,
  263. sizeof(*state));
  264. }
  265. int snet_suspend_dev(struct snet *snet)
  266. {
  267. return snet_send_ctrl_msg(snet, SNET_CTRL_OP_SUSPEND, 0);
  268. }
  269. int snet_resume_dev(struct snet *snet)
  270. {
  271. return snet_send_ctrl_msg(snet, SNET_CTRL_OP_RESUME, 0);
  272. }