srq.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Copyright(c) 2016 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/err.h>
  48. #include <linux/slab.h>
  49. #include <linux/vmalloc.h>
  50. #include "srq.h"
  51. #include "vt.h"
  52. /**
  53. * rvt_driver_srq_init - init srq resources on a per driver basis
  54. * @rdi: rvt dev structure
  55. *
  56. * Do any initialization needed when a driver registers with rdmavt.
  57. */
  58. void rvt_driver_srq_init(struct rvt_dev_info *rdi)
  59. {
  60. spin_lock_init(&rdi->n_srqs_lock);
  61. rdi->n_srqs_allocated = 0;
  62. }
  63. /**
  64. * rvt_create_srq - create a shared receive queue
  65. * @ibpd: the protection domain of the SRQ to create
  66. * @srq_init_attr: the attributes of the SRQ
  67. * @udata: data from libibverbs when creating a user SRQ
  68. *
  69. * Return: Allocated srq object
  70. */
  71. struct ib_srq *rvt_create_srq(struct ib_pd *ibpd,
  72. struct ib_srq_init_attr *srq_init_attr,
  73. struct ib_udata *udata)
  74. {
  75. struct rvt_dev_info *dev = ib_to_rvt(ibpd->device);
  76. struct rvt_srq *srq;
  77. u32 sz;
  78. struct ib_srq *ret;
  79. if (srq_init_attr->srq_type != IB_SRQT_BASIC)
  80. return ERR_PTR(-EOPNOTSUPP);
  81. if (srq_init_attr->attr.max_sge == 0 ||
  82. srq_init_attr->attr.max_sge > dev->dparms.props.max_srq_sge ||
  83. srq_init_attr->attr.max_wr == 0 ||
  84. srq_init_attr->attr.max_wr > dev->dparms.props.max_srq_wr)
  85. return ERR_PTR(-EINVAL);
  86. srq = kzalloc_node(sizeof(*srq), GFP_KERNEL, dev->dparms.node);
  87. if (!srq)
  88. return ERR_PTR(-ENOMEM);
  89. /*
  90. * Need to use vmalloc() if we want to support large #s of entries.
  91. */
  92. srq->rq.size = srq_init_attr->attr.max_wr + 1;
  93. srq->rq.max_sge = srq_init_attr->attr.max_sge;
  94. sz = sizeof(struct ib_sge) * srq->rq.max_sge +
  95. sizeof(struct rvt_rwqe);
  96. srq->rq.wq = udata ?
  97. vmalloc_user(sizeof(struct rvt_rwq) + srq->rq.size * sz) :
  98. vzalloc_node(sizeof(struct rvt_rwq) + srq->rq.size * sz,
  99. dev->dparms.node);
  100. if (!srq->rq.wq) {
  101. ret = ERR_PTR(-ENOMEM);
  102. goto bail_srq;
  103. }
  104. /*
  105. * Return the address of the RWQ as the offset to mmap.
  106. * See rvt_mmap() for details.
  107. */
  108. if (udata && udata->outlen >= sizeof(__u64)) {
  109. int err;
  110. u32 s = sizeof(struct rvt_rwq) + srq->rq.size * sz;
  111. srq->ip =
  112. rvt_create_mmap_info(dev, s, ibpd->uobject->context,
  113. srq->rq.wq);
  114. if (!srq->ip) {
  115. ret = ERR_PTR(-ENOMEM);
  116. goto bail_wq;
  117. }
  118. err = ib_copy_to_udata(udata, &srq->ip->offset,
  119. sizeof(srq->ip->offset));
  120. if (err) {
  121. ret = ERR_PTR(err);
  122. goto bail_ip;
  123. }
  124. }
  125. /*
  126. * ib_create_srq() will initialize srq->ibsrq.
  127. */
  128. spin_lock_init(&srq->rq.lock);
  129. srq->limit = srq_init_attr->attr.srq_limit;
  130. spin_lock(&dev->n_srqs_lock);
  131. if (dev->n_srqs_allocated == dev->dparms.props.max_srq) {
  132. spin_unlock(&dev->n_srqs_lock);
  133. ret = ERR_PTR(-ENOMEM);
  134. goto bail_ip;
  135. }
  136. dev->n_srqs_allocated++;
  137. spin_unlock(&dev->n_srqs_lock);
  138. if (srq->ip) {
  139. spin_lock_irq(&dev->pending_lock);
  140. list_add(&srq->ip->pending_mmaps, &dev->pending_mmaps);
  141. spin_unlock_irq(&dev->pending_lock);
  142. }
  143. return &srq->ibsrq;
  144. bail_ip:
  145. kfree(srq->ip);
  146. bail_wq:
  147. vfree(srq->rq.wq);
  148. bail_srq:
  149. kfree(srq);
  150. return ret;
  151. }
  152. /**
  153. * rvt_modify_srq - modify a shared receive queue
  154. * @ibsrq: the SRQ to modify
  155. * @attr: the new attributes of the SRQ
  156. * @attr_mask: indicates which attributes to modify
  157. * @udata: user data for libibverbs.so
  158. *
  159. * Return: 0 on success
  160. */
  161. int rvt_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
  162. enum ib_srq_attr_mask attr_mask,
  163. struct ib_udata *udata)
  164. {
  165. struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
  166. struct rvt_dev_info *dev = ib_to_rvt(ibsrq->device);
  167. struct rvt_rwq *wq;
  168. int ret = 0;
  169. if (attr_mask & IB_SRQ_MAX_WR) {
  170. struct rvt_rwq *owq;
  171. struct rvt_rwqe *p;
  172. u32 sz, size, n, head, tail;
  173. /* Check that the requested sizes are below the limits. */
  174. if ((attr->max_wr > dev->dparms.props.max_srq_wr) ||
  175. ((attr_mask & IB_SRQ_LIMIT) ?
  176. attr->srq_limit : srq->limit) > attr->max_wr)
  177. return -EINVAL;
  178. sz = sizeof(struct rvt_rwqe) +
  179. srq->rq.max_sge * sizeof(struct ib_sge);
  180. size = attr->max_wr + 1;
  181. wq = udata ?
  182. vmalloc_user(sizeof(struct rvt_rwq) + size * sz) :
  183. vzalloc_node(sizeof(struct rvt_rwq) + size * sz,
  184. dev->dparms.node);
  185. if (!wq)
  186. return -ENOMEM;
  187. /* Check that we can write the offset to mmap. */
  188. if (udata && udata->inlen >= sizeof(__u64)) {
  189. __u64 offset_addr;
  190. __u64 offset = 0;
  191. ret = ib_copy_from_udata(&offset_addr, udata,
  192. sizeof(offset_addr));
  193. if (ret)
  194. goto bail_free;
  195. udata->outbuf = (void __user *)
  196. (unsigned long)offset_addr;
  197. ret = ib_copy_to_udata(udata, &offset,
  198. sizeof(offset));
  199. if (ret)
  200. goto bail_free;
  201. }
  202. spin_lock_irq(&srq->rq.lock);
  203. /*
  204. * validate head and tail pointer values and compute
  205. * the number of remaining WQEs.
  206. */
  207. owq = srq->rq.wq;
  208. head = owq->head;
  209. tail = owq->tail;
  210. if (head >= srq->rq.size || tail >= srq->rq.size) {
  211. ret = -EINVAL;
  212. goto bail_unlock;
  213. }
  214. n = head;
  215. if (n < tail)
  216. n += srq->rq.size - tail;
  217. else
  218. n -= tail;
  219. if (size <= n) {
  220. ret = -EINVAL;
  221. goto bail_unlock;
  222. }
  223. n = 0;
  224. p = wq->wq;
  225. while (tail != head) {
  226. struct rvt_rwqe *wqe;
  227. int i;
  228. wqe = rvt_get_rwqe_ptr(&srq->rq, tail);
  229. p->wr_id = wqe->wr_id;
  230. p->num_sge = wqe->num_sge;
  231. for (i = 0; i < wqe->num_sge; i++)
  232. p->sg_list[i] = wqe->sg_list[i];
  233. n++;
  234. p = (struct rvt_rwqe *)((char *)p + sz);
  235. if (++tail >= srq->rq.size)
  236. tail = 0;
  237. }
  238. srq->rq.wq = wq;
  239. srq->rq.size = size;
  240. wq->head = n;
  241. wq->tail = 0;
  242. if (attr_mask & IB_SRQ_LIMIT)
  243. srq->limit = attr->srq_limit;
  244. spin_unlock_irq(&srq->rq.lock);
  245. vfree(owq);
  246. if (srq->ip) {
  247. struct rvt_mmap_info *ip = srq->ip;
  248. struct rvt_dev_info *dev = ib_to_rvt(srq->ibsrq.device);
  249. u32 s = sizeof(struct rvt_rwq) + size * sz;
  250. rvt_update_mmap_info(dev, ip, s, wq);
  251. /*
  252. * Return the offset to mmap.
  253. * See rvt_mmap() for details.
  254. */
  255. if (udata && udata->inlen >= sizeof(__u64)) {
  256. ret = ib_copy_to_udata(udata, &ip->offset,
  257. sizeof(ip->offset));
  258. if (ret)
  259. return ret;
  260. }
  261. /*
  262. * Put user mapping info onto the pending list
  263. * unless it already is on the list.
  264. */
  265. spin_lock_irq(&dev->pending_lock);
  266. if (list_empty(&ip->pending_mmaps))
  267. list_add(&ip->pending_mmaps,
  268. &dev->pending_mmaps);
  269. spin_unlock_irq(&dev->pending_lock);
  270. }
  271. } else if (attr_mask & IB_SRQ_LIMIT) {
  272. spin_lock_irq(&srq->rq.lock);
  273. if (attr->srq_limit >= srq->rq.size)
  274. ret = -EINVAL;
  275. else
  276. srq->limit = attr->srq_limit;
  277. spin_unlock_irq(&srq->rq.lock);
  278. }
  279. return ret;
  280. bail_unlock:
  281. spin_unlock_irq(&srq->rq.lock);
  282. bail_free:
  283. vfree(wq);
  284. return ret;
  285. }
  286. /** rvt_query_srq - query srq data
  287. * @ibsrq: srq to query
  288. * @attr: return info in attr
  289. *
  290. * Return: always 0
  291. */
  292. int rvt_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
  293. {
  294. struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
  295. attr->max_wr = srq->rq.size - 1;
  296. attr->max_sge = srq->rq.max_sge;
  297. attr->srq_limit = srq->limit;
  298. return 0;
  299. }
  300. /**
  301. * rvt_destroy_srq - destory an srq
  302. * @ibsrq: srq object to destroy
  303. *
  304. * Return always 0
  305. */
  306. int rvt_destroy_srq(struct ib_srq *ibsrq)
  307. {
  308. struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
  309. struct rvt_dev_info *dev = ib_to_rvt(ibsrq->device);
  310. spin_lock(&dev->n_srqs_lock);
  311. dev->n_srqs_allocated--;
  312. spin_unlock(&dev->n_srqs_lock);
  313. if (srq->ip)
  314. kref_put(&srq->ip->ref, rvt_release_mmap_info);
  315. else
  316. vfree(srq->rq.wq);
  317. kfree(srq);
  318. return 0;
  319. }