pvrdma_srq.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. /**
  53. * pvrdma_query_srq - query shared receive queue
  54. * @ibsrq: the shared receive queue to query
  55. * @srq_attr: attributes to query and return to client
  56. *
  57. * @return: 0 for success, otherwise returns an errno.
  58. */
  59. int pvrdma_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
  60. {
  61. struct pvrdma_dev *dev = to_vdev(ibsrq->device);
  62. struct pvrdma_srq *srq = to_vsrq(ibsrq);
  63. union pvrdma_cmd_req req;
  64. union pvrdma_cmd_resp rsp;
  65. struct pvrdma_cmd_query_srq *cmd = &req.query_srq;
  66. struct pvrdma_cmd_query_srq_resp *resp = &rsp.query_srq_resp;
  67. int ret;
  68. memset(cmd, 0, sizeof(*cmd));
  69. cmd->hdr.cmd = PVRDMA_CMD_QUERY_SRQ;
  70. cmd->srq_handle = srq->srq_handle;
  71. ret = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_QUERY_SRQ_RESP);
  72. if (ret < 0) {
  73. dev_warn(&dev->pdev->dev,
  74. "could not query shared receive queue, error: %d\n",
  75. ret);
  76. return -EINVAL;
  77. }
  78. srq_attr->srq_limit = resp->attrs.srq_limit;
  79. srq_attr->max_wr = resp->attrs.max_wr;
  80. srq_attr->max_sge = resp->attrs.max_sge;
  81. return 0;
  82. }
  83. /**
  84. * pvrdma_create_srq - create shared receive queue
  85. * @pd: protection domain
  86. * @init_attr: shared receive queue attributes
  87. * @udata: user data
  88. *
  89. * @return: the ib_srq pointer on success, otherwise returns an errno.
  90. */
  91. struct ib_srq *pvrdma_create_srq(struct ib_pd *pd,
  92. struct ib_srq_init_attr *init_attr,
  93. struct ib_udata *udata)
  94. {
  95. struct pvrdma_srq *srq = NULL;
  96. struct pvrdma_dev *dev = to_vdev(pd->device);
  97. union pvrdma_cmd_req req;
  98. union pvrdma_cmd_resp rsp;
  99. struct pvrdma_cmd_create_srq *cmd = &req.create_srq;
  100. struct pvrdma_cmd_create_srq_resp *resp = &rsp.create_srq_resp;
  101. struct pvrdma_create_srq_resp srq_resp = {0};
  102. struct pvrdma_create_srq ucmd;
  103. unsigned long flags;
  104. int ret;
  105. if (!(pd->uobject && udata)) {
  106. /* No support for kernel clients. */
  107. dev_warn(&dev->pdev->dev,
  108. "no shared receive queue support for kernel client\n");
  109. return ERR_PTR(-EOPNOTSUPP);
  110. }
  111. if (init_attr->srq_type != IB_SRQT_BASIC) {
  112. dev_warn(&dev->pdev->dev,
  113. "shared receive queue type %d not supported\n",
  114. init_attr->srq_type);
  115. return ERR_PTR(-EINVAL);
  116. }
  117. if (init_attr->attr.max_wr > dev->dsr->caps.max_srq_wr ||
  118. init_attr->attr.max_sge > dev->dsr->caps.max_srq_sge) {
  119. dev_warn(&dev->pdev->dev,
  120. "shared receive queue size invalid\n");
  121. return ERR_PTR(-EINVAL);
  122. }
  123. if (!atomic_add_unless(&dev->num_srqs, 1, dev->dsr->caps.max_srq))
  124. return ERR_PTR(-ENOMEM);
  125. srq = kmalloc(sizeof(*srq), GFP_KERNEL);
  126. if (!srq) {
  127. ret = -ENOMEM;
  128. goto err_srq;
  129. }
  130. spin_lock_init(&srq->lock);
  131. refcount_set(&srq->refcnt, 1);
  132. init_completion(&srq->free);
  133. dev_dbg(&dev->pdev->dev,
  134. "create shared receive queue from user space\n");
  135. if (ib_copy_from_udata(&ucmd, udata, sizeof(ucmd))) {
  136. ret = -EFAULT;
  137. goto err_srq;
  138. }
  139. srq->umem = ib_umem_get(pd->uobject->context,
  140. ucmd.buf_addr,
  141. ucmd.buf_size, 0, 0);
  142. if (IS_ERR(srq->umem)) {
  143. ret = PTR_ERR(srq->umem);
  144. goto err_srq;
  145. }
  146. srq->npages = ib_umem_page_count(srq->umem);
  147. if (srq->npages < 0 || srq->npages > PVRDMA_PAGE_DIR_MAX_PAGES) {
  148. dev_warn(&dev->pdev->dev,
  149. "overflow pages in shared receive queue\n");
  150. ret = -EINVAL;
  151. goto err_umem;
  152. }
  153. ret = pvrdma_page_dir_init(dev, &srq->pdir, srq->npages, false);
  154. if (ret) {
  155. dev_warn(&dev->pdev->dev,
  156. "could not allocate page directory\n");
  157. goto err_umem;
  158. }
  159. pvrdma_page_dir_insert_umem(&srq->pdir, srq->umem, 0);
  160. memset(cmd, 0, sizeof(*cmd));
  161. cmd->hdr.cmd = PVRDMA_CMD_CREATE_SRQ;
  162. cmd->srq_type = init_attr->srq_type;
  163. cmd->nchunks = srq->npages;
  164. cmd->pd_handle = to_vpd(pd)->pd_handle;
  165. cmd->attrs.max_wr = init_attr->attr.max_wr;
  166. cmd->attrs.max_sge = init_attr->attr.max_sge;
  167. cmd->attrs.srq_limit = init_attr->attr.srq_limit;
  168. cmd->pdir_dma = srq->pdir.dir_dma;
  169. ret = pvrdma_cmd_post(dev, &req, &rsp, PVRDMA_CMD_CREATE_SRQ_RESP);
  170. if (ret < 0) {
  171. dev_warn(&dev->pdev->dev,
  172. "could not create shared receive queue, error: %d\n",
  173. ret);
  174. goto err_page_dir;
  175. }
  176. srq->srq_handle = resp->srqn;
  177. srq_resp.srqn = resp->srqn;
  178. spin_lock_irqsave(&dev->srq_tbl_lock, flags);
  179. dev->srq_tbl[srq->srq_handle % dev->dsr->caps.max_srq] = srq;
  180. spin_unlock_irqrestore(&dev->srq_tbl_lock, flags);
  181. /* Copy udata back. */
  182. if (ib_copy_to_udata(udata, &srq_resp, sizeof(srq_resp))) {
  183. dev_warn(&dev->pdev->dev, "failed to copy back udata\n");
  184. pvrdma_destroy_srq(&srq->ibsrq);
  185. return ERR_PTR(-EINVAL);
  186. }
  187. return &srq->ibsrq;
  188. err_page_dir:
  189. pvrdma_page_dir_cleanup(dev, &srq->pdir);
  190. err_umem:
  191. ib_umem_release(srq->umem);
  192. err_srq:
  193. kfree(srq);
  194. atomic_dec(&dev->num_srqs);
  195. return ERR_PTR(ret);
  196. }
  197. static void pvrdma_free_srq(struct pvrdma_dev *dev, struct pvrdma_srq *srq)
  198. {
  199. unsigned long flags;
  200. spin_lock_irqsave(&dev->srq_tbl_lock, flags);
  201. dev->srq_tbl[srq->srq_handle] = NULL;
  202. spin_unlock_irqrestore(&dev->srq_tbl_lock, flags);
  203. if (refcount_dec_and_test(&srq->refcnt))
  204. complete(&srq->free);
  205. wait_for_completion(&srq->free);
  206. /* There is no support for kernel clients, so this is safe. */
  207. ib_umem_release(srq->umem);
  208. pvrdma_page_dir_cleanup(dev, &srq->pdir);
  209. kfree(srq);
  210. atomic_dec(&dev->num_srqs);
  211. }
  212. /**
  213. * pvrdma_destroy_srq - destroy shared receive queue
  214. * @srq: the shared receive queue to destroy
  215. *
  216. * @return: 0 for success.
  217. */
  218. int pvrdma_destroy_srq(struct ib_srq *srq)
  219. {
  220. struct pvrdma_srq *vsrq = to_vsrq(srq);
  221. union pvrdma_cmd_req req;
  222. struct pvrdma_cmd_destroy_srq *cmd = &req.destroy_srq;
  223. struct pvrdma_dev *dev = to_vdev(srq->device);
  224. int ret;
  225. memset(cmd, 0, sizeof(*cmd));
  226. cmd->hdr.cmd = PVRDMA_CMD_DESTROY_SRQ;
  227. cmd->srq_handle = vsrq->srq_handle;
  228. ret = pvrdma_cmd_post(dev, &req, NULL, 0);
  229. if (ret < 0)
  230. dev_warn(&dev->pdev->dev,
  231. "destroy shared receive queue failed, error: %d\n",
  232. ret);
  233. pvrdma_free_srq(dev, vsrq);
  234. return 0;
  235. }
  236. /**
  237. * pvrdma_modify_srq - modify shared receive queue attributes
  238. * @ibsrq: the shared receive queue to modify
  239. * @attr: the shared receive queue's new attributes
  240. * @attr_mask: attributes mask
  241. * @udata: user data
  242. *
  243. * @returns 0 on success, otherwise returns an errno.
  244. */
  245. int pvrdma_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
  246. enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
  247. {
  248. struct pvrdma_srq *vsrq = to_vsrq(ibsrq);
  249. union pvrdma_cmd_req req;
  250. struct pvrdma_cmd_modify_srq *cmd = &req.modify_srq;
  251. struct pvrdma_dev *dev = to_vdev(ibsrq->device);
  252. int ret;
  253. /* Only support SRQ limit. */
  254. if (!(attr_mask & IB_SRQ_LIMIT))
  255. return -EINVAL;
  256. memset(cmd, 0, sizeof(*cmd));
  257. cmd->hdr.cmd = PVRDMA_CMD_MODIFY_SRQ;
  258. cmd->srq_handle = vsrq->srq_handle;
  259. cmd->attrs.srq_limit = attr->srq_limit;
  260. cmd->attr_mask = attr_mask;
  261. ret = pvrdma_cmd_post(dev, &req, NULL, 0);
  262. if (ret < 0) {
  263. dev_warn(&dev->pdev->dev,
  264. "could not modify shared receive queue, error: %d\n",
  265. ret);
  266. return -EINVAL;
  267. }
  268. return ret;
  269. }