pvrdma_srq.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (c) 2016-2017 VMware, Inc. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of EITHER the GNU General Public License
  6. * version 2 as published by the Free Software Foundation or the BSD
  7. * 2-Clause License. This program is distributed in the hope that it
  8. * will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED
  9. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  10. * See the GNU General Public License version 2 for more details at
  11. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program available in the file COPYING in the main
  15. * directory of this source tree.
  16. *
  17. * The BSD 2-Clause License
  18. *
  19. * Redistribution and use in source and binary forms, with or
  20. * without modification, are permitted provided that the following
  21. * conditions are met:
  22. *
  23. * - Redistributions of source code must retain the above
  24. * copyright notice, this list of conditions and the following
  25. * disclaimer.
  26. *
  27. * - Redistributions in binary form must reproduce the above
  28. * copyright notice, this list of conditions and the following
  29. * disclaimer in the documentation and/or other materials
  30. * provided with the distribution.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  33. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  34. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  35. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  36. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  37. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  38. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  39. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  40. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  43. * OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. #include <asm/page.h>
  46. #include <linux/io.h>
  47. #include <linux/wait.h>
  48. #include <rdma/ib_addr.h>
  49. #include <rdma/ib_smi.h>
  50. #include <rdma/ib_user_verbs.h>
  51. #include "pvrdma.h"
  52. int pvrdma_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  53. struct ib_recv_wr **bad_wr)
  54. {
  55. /* No support for kernel clients. */
  56. return -EOPNOTSUPP;
  57. }
  58. /**
  59. * pvrdma_query_srq - query shared receive queue
  60. * @ibsrq: the shared receive queue to query
  61. * @srq_attr: attributes to query and return to client
  62. *
  63. * @return: 0 for success, otherwise returns an errno.
  64. */
  65. int pvrdma_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
  66. {
  67. struct pvrdma_dev *dev = to_vdev(ibsrq->device);
  68. struct pvrdma_srq *srq = to_vsrq(ibsrq);
  69. union pvrdma_cmd_req req;
  70. union pvrdma_cmd_resp rsp;
  71. struct pvrdma_cmd_query_srq *cmd = &req.query_srq;
  72. struct pvrdma_cmd_query_srq_resp *resp = &rsp.query_srq_resp;
  73. int ret;
  74. memset(cmd, 0, sizeof(*cmd));
  75. cmd->hdr.cmd = PVRDMA_CMD_QUERY_SRQ;
  76. cmd->srq_handle = srq->srq_handle;
  77. ret = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_QUERY_SRQ_RESP);
  78. if (ret < 0) {
  79. dev_warn(&dev->pdev->dev,
  80. "could not query shared receive queue, error: %d\n",
  81. ret);
  82. return -EINVAL;
  83. }
  84. srq_attr->srq_limit = resp->attrs.srq_limit;
  85. srq_attr->max_wr = resp->attrs.max_wr;
  86. srq_attr->max_sge = resp->attrs.max_sge;
  87. return 0;
  88. }
  89. /**
  90. * pvrdma_create_srq - create shared receive queue
  91. * @pd: protection domain
  92. * @init_attr: shared receive queue attributes
  93. * @udata: user data
  94. *
  95. * @return: the ib_srq pointer on success, otherwise returns an errno.
  96. */
  97. struct ib_srq *pvrdma_create_srq(struct ib_pd *pd,
  98. struct ib_srq_init_attr *init_attr,
  99. struct ib_udata *udata)
  100. {
  101. struct pvrdma_srq *srq = NULL;
  102. struct pvrdma_dev *dev = to_vdev(pd->device);
  103. union pvrdma_cmd_req req;
  104. union pvrdma_cmd_resp rsp;
  105. struct pvrdma_cmd_create_srq *cmd = &req.create_srq;
  106. struct pvrdma_cmd_create_srq_resp *resp = &rsp.create_srq_resp;
  107. struct pvrdma_create_srq_resp srq_resp = {0};
  108. struct pvrdma_create_srq ucmd;
  109. unsigned long flags;
  110. int ret;
  111. if (!(pd->uobject && udata)) {
  112. /* No support for kernel clients. */
  113. dev_warn(&dev->pdev->dev,
  114. "no shared receive queue support for kernel client\n");
  115. return ERR_PTR(-EOPNOTSUPP);
  116. }
  117. if (init_attr->srq_type != IB_SRQT_BASIC) {
  118. dev_warn(&dev->pdev->dev,
  119. "shared receive queue type %d not supported\n",
  120. init_attr->srq_type);
  121. return ERR_PTR(-EINVAL);
  122. }
  123. if (init_attr->attr.max_wr > dev->dsr->caps.max_srq_wr ||
  124. init_attr->attr.max_sge > dev->dsr->caps.max_srq_sge) {
  125. dev_warn(&dev->pdev->dev,
  126. "shared receive queue size invalid\n");
  127. return ERR_PTR(-EINVAL);
  128. }
  129. if (!atomic_add_unless(&dev->num_srqs, 1, dev->dsr->caps.max_srq))
  130. return ERR_PTR(-ENOMEM);
  131. srq = kmalloc(sizeof(*srq), GFP_KERNEL);
  132. if (!srq) {
  133. ret = -ENOMEM;
  134. goto err_srq;
  135. }
  136. spin_lock_init(&srq->lock);
  137. refcount_set(&srq->refcnt, 1);
  138. init_completion(&srq->free);
  139. dev_dbg(&dev->pdev->dev,
  140. "create shared receive queue from user space\n");
  141. if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
  142. ret = -EFAULT;
  143. goto err_srq;
  144. }
  145. srq->umem = ib_umem_get(pd->uobject->context,
  146. ucmd.buf_addr,
  147. ucmd.buf_size, 0, 0);
  148. if (IS_ERR(srq->umem)) {
  149. ret = PTR_ERR(srq->umem);
  150. goto err_srq;
  151. }
  152. srq->npages = ib_umem_page_count(srq->umem);
  153. if (srq->npages < 0 || srq->npages > PVRDMA_PAGE_DIR_MAX_PAGES) {
  154. dev_warn(&dev->pdev->dev,
  155. "overflow pages in shared receive queue\n");
  156. ret = -EINVAL;
  157. goto err_umem;
  158. }
  159. ret = pvrdma_page_dir_init(dev, &srq->pdir, srq->npages, false);
  160. if (ret) {
  161. dev_warn(&dev->pdev->dev,
  162. "could not allocate page directory\n");
  163. goto err_umem;
  164. }
  165. pvrdma_page_dir_insert_umem(&srq->pdir, srq->umem, 0);
  166. memset(cmd, 0, sizeof(*cmd));
  167. cmd->hdr.cmd = PVRDMA_CMD_CREATE_SRQ;
  168. cmd->srq_type = init_attr->srq_type;
  169. cmd->nchunks = srq->npages;
  170. cmd->pd_handle = to_vpd(pd)->pd_handle;
  171. cmd->attrs.max_wr = init_attr->attr.max_wr;
  172. cmd->attrs.max_sge = init_attr->attr.max_sge;
  173. cmd->attrs.srq_limit = init_attr->attr.srq_limit;
  174. cmd->pdir_dma = srq->pdir.dir_dma;
  175. ret = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_CREATE_SRQ_RESP);
  176. if (ret < 0) {
  177. dev_warn(&dev->pdev->dev,
  178. "could not create shared receive queue, error: %d\n",
  179. ret);
  180. goto err_page_dir;
  181. }
  182. srq->srq_handle = resp->srqn;
  183. srq_resp.srqn = resp->srqn;
  184. spin_lock_irqsave(&dev->srq_tbl_lock, flags);
  185. dev->srq_tbl[srq->srq_handle % dev->dsr->caps.max_srq] = srq;
  186. spin_unlock_irqrestore(&dev->srq_tbl_lock, flags);
  187. /* Copy udata back. */
  188. if (ib_copy_to_udata(udata, &srq_resp, sizeof(srq_resp))) {
  189. dev_warn(&dev->pdev->dev, "failed to copy back udata\n");
  190. pvrdma_destroy_srq(&srq->ibsrq);
  191. return ERR_PTR(-EINVAL);
  192. }
  193. return &srq->ibsrq;
  194. err_page_dir:
  195. pvrdma_page_dir_cleanup(dev, &srq->pdir);
  196. err_umem:
  197. ib_umem_release(srq->umem);
  198. err_srq:
  199. kfree(srq);
  200. atomic_dec(&dev->num_srqs);
  201. return ERR_PTR(ret);
  202. }
  203. static void pvrdma_free_srq(struct pvrdma_dev *dev, struct pvrdma_srq *srq)
  204. {
  205. unsigned long flags;
  206. spin_lock_irqsave(&dev->srq_tbl_lock, flags);
  207. dev->srq_tbl[srq->srq_handle] = NULL;
  208. spin_unlock_irqrestore(&dev->srq_tbl_lock, flags);
  209. if (refcount_dec_and_test(&srq->refcnt))
  210. complete(&srq->free);
  211. wait_for_completion(&srq->free);
  212. /* There is no support for kernel clients, so this is safe. */
  213. ib_umem_release(srq->umem);
  214. pvrdma_page_dir_cleanup(dev, &srq->pdir);
  215. kfree(srq);
  216. atomic_dec(&dev->num_srqs);
  217. }
  218. /**
  219. * pvrdma_destroy_srq - destroy shared receive queue
  220. * @srq: the shared receive queue to destroy
  221. *
  222. * @return: 0 for success.
  223. */
  224. int pvrdma_destroy_srq(struct ib_srq *srq)
  225. {
  226. struct pvrdma_srq *vsrq = to_vsrq(srq);
  227. union pvrdma_cmd_req req;
  228. struct pvrdma_cmd_destroy_srq *cmd = &req.destroy_srq;
  229. struct pvrdma_dev *dev = to_vdev(srq->device);
  230. int ret;
  231. memset(cmd, 0, sizeof(*cmd));
  232. cmd->hdr.cmd = PVRDMA_CMD_DESTROY_SRQ;
  233. cmd->srq_handle = vsrq->srq_handle;
  234. ret = pvrdma_cmd_post(dev, &req, NULL, 0);
  235. if (ret < 0)
  236. dev_warn(&dev->pdev->dev,
  237. "destroy shared receive queue failed, error: %d\n",
  238. ret);
  239. pvrdma_free_srq(dev, vsrq);
  240. return 0;
  241. }
  242. /**
  243. * pvrdma_modify_srq - modify shared receive queue attributes
  244. * @ibsrq: the shared receive queue to modify
  245. * @attr: the shared receive queue's new attributes
  246. * @attr_mask: attributes mask
  247. * @udata: user data
  248. *
  249. * @returns 0 on success, otherwise returns an errno.
  250. */
  251. int pvrdma_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
  252. enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
  253. {
  254. struct pvrdma_srq *vsrq = to_vsrq(ibsrq);
  255. union pvrdma_cmd_req req;
  256. struct pvrdma_cmd_modify_srq *cmd = &req.modify_srq;
  257. struct pvrdma_dev *dev = to_vdev(ibsrq->device);
  258. int ret;
  259. /* Only support SRQ limit. */
  260. if (!(attr_mask & IB_SRQ_LIMIT))
  261. return -EINVAL;
  262. memset(cmd, 0, sizeof(*cmd));
  263. cmd->hdr.cmd = PVRDMA_CMD_MODIFY_SRQ;
  264. cmd->srq_handle = vsrq->srq_handle;
  265. cmd->attrs.srq_limit = attr->srq_limit;
  266. cmd->attr_mask = attr_mask;
  267. ret = pvrdma_cmd_post(dev, &req, NULL, 0);
  268. if (ret < 0) {
  269. dev_warn(&dev->pdev->dev,
  270. "could not modify shared receive queue, error: %d\n",
  271. ret);
  272. return -EINVAL;
  273. }
  274. return ret;
  275. }