srq.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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(-ENOSYS);
  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 = kmalloc(sizeof(*srq), GFP_KERNEL);
  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 = vmalloc_user(sizeof(struct rvt_rwq) + srq->rq.size * sz);
  97. if (!srq->rq.wq) {
  98. ret = ERR_PTR(-ENOMEM);
  99. goto bail_srq;
  100. }
  101. /*
  102. * Return the address of the RWQ as the offset to mmap.
  103. * See rvt_mmap() for details.
  104. */
  105. if (udata && udata->outlen >= sizeof(__u64)) {
  106. int err;
  107. u32 s = sizeof(struct rvt_rwq) + srq->rq.size * sz;
  108. srq->ip =
  109. rvt_create_mmap_info(dev, s, ibpd->uobject->context,
  110. srq->rq.wq);
  111. if (!srq->ip) {
  112. ret = ERR_PTR(-ENOMEM);
  113. goto bail_wq;
  114. }
  115. err = ib_copy_to_udata(udata, &srq->ip->offset,
  116. sizeof(srq->ip->offset));
  117. if (err) {
  118. ret = ERR_PTR(err);
  119. goto bail_ip;
  120. }
  121. } else {
  122. srq->ip = NULL;
  123. }
  124. /*
  125. * ib_create_srq() will initialize srq->ibsrq.
  126. */
  127. spin_lock_init(&srq->rq.lock);
  128. srq->rq.wq->head = 0;
  129. srq->rq.wq->tail = 0;
  130. srq->limit = srq_init_attr->attr.srq_limit;
  131. spin_lock(&dev->n_srqs_lock);
  132. if (dev->n_srqs_allocated == dev->dparms.props.max_srq) {
  133. spin_unlock(&dev->n_srqs_lock);
  134. ret = ERR_PTR(-ENOMEM);
  135. goto bail_ip;
  136. }
  137. dev->n_srqs_allocated++;
  138. spin_unlock(&dev->n_srqs_lock);
  139. if (srq->ip) {
  140. spin_lock_irq(&dev->pending_lock);
  141. list_add(&srq->ip->pending_mmaps, &dev->pending_mmaps);
  142. spin_unlock_irq(&dev->pending_lock);
  143. }
  144. return &srq->ibsrq;
  145. bail_ip:
  146. kfree(srq->ip);
  147. bail_wq:
  148. vfree(srq->rq.wq);
  149. bail_srq:
  150. kfree(srq);
  151. return ret;
  152. }
  153. /**
  154. * rvt_modify_srq - modify a shared receive queue
  155. * @ibsrq: the SRQ to modify
  156. * @attr: the new attributes of the SRQ
  157. * @attr_mask: indicates which attributes to modify
  158. * @udata: user data for libibverbs.so
  159. *
  160. * Return: 0 on success
  161. */
  162. int rvt_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
  163. enum ib_srq_attr_mask attr_mask,
  164. struct ib_udata *udata)
  165. {
  166. struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
  167. struct rvt_dev_info *dev = ib_to_rvt(ibsrq->device);
  168. struct rvt_rwq *wq;
  169. int ret = 0;
  170. if (attr_mask & IB_SRQ_MAX_WR) {
  171. struct rvt_rwq *owq;
  172. struct rvt_rwqe *p;
  173. u32 sz, size, n, head, tail;
  174. /* Check that the requested sizes are below the limits. */
  175. if ((attr->max_wr > dev->dparms.props.max_srq_wr) ||
  176. ((attr_mask & IB_SRQ_LIMIT) ?
  177. attr->srq_limit : srq->limit) > attr->max_wr)
  178. return -EINVAL;
  179. sz = sizeof(struct rvt_rwqe) +
  180. srq->rq.max_sge * sizeof(struct ib_sge);
  181. size = attr->max_wr + 1;
  182. wq = vmalloc_user(sizeof(struct rvt_rwq) + size * sz);
  183. if (!wq)
  184. return -ENOMEM;
  185. /* Check that we can write the offset to mmap. */
  186. if (udata && udata->inlen >= sizeof(__u64)) {
  187. __u64 offset_addr;
  188. __u64 offset = 0;
  189. ret = ib_copy_from_udata(&offset_addr, udata,
  190. sizeof(offset_addr));
  191. if (ret)
  192. goto bail_free;
  193. udata->outbuf = (void __user *)
  194. (unsigned long)offset_addr;
  195. ret = ib_copy_to_udata(udata, &offset,
  196. sizeof(offset));
  197. if (ret)
  198. goto bail_free;
  199. }
  200. spin_lock_irq(&srq->rq.lock);
  201. /*
  202. * validate head and tail pointer values and compute
  203. * the number of remaining WQEs.
  204. */
  205. owq = srq->rq.wq;
  206. head = owq->head;
  207. tail = owq->tail;
  208. if (head >= srq->rq.size || tail >= srq->rq.size) {
  209. ret = -EINVAL;
  210. goto bail_unlock;
  211. }
  212. n = head;
  213. if (n < tail)
  214. n += srq->rq.size - tail;
  215. else
  216. n -= tail;
  217. if (size <= n) {
  218. ret = -EINVAL;
  219. goto bail_unlock;
  220. }
  221. n = 0;
  222. p = wq->wq;
  223. while (tail != head) {
  224. struct rvt_rwqe *wqe;
  225. int i;
  226. wqe = rvt_get_rwqe_ptr(&srq->rq, tail);
  227. p->wr_id = wqe->wr_id;
  228. p->num_sge = wqe->num_sge;
  229. for (i = 0; i < wqe->num_sge; i++)
  230. p->sg_list[i] = wqe->sg_list[i];
  231. n++;
  232. p = (struct rvt_rwqe *)((char *)p + sz);
  233. if (++tail >= srq->rq.size)
  234. tail = 0;
  235. }
  236. srq->rq.wq = wq;
  237. srq->rq.size = size;
  238. wq->head = n;
  239. wq->tail = 0;
  240. if (attr_mask & IB_SRQ_LIMIT)
  241. srq->limit = attr->srq_limit;
  242. spin_unlock_irq(&srq->rq.lock);
  243. vfree(owq);
  244. if (srq->ip) {
  245. struct rvt_mmap_info *ip = srq->ip;
  246. struct rvt_dev_info *dev = ib_to_rvt(srq->ibsrq.device);
  247. u32 s = sizeof(struct rvt_rwq) + size * sz;
  248. rvt_update_mmap_info(dev, ip, s, wq);
  249. /*
  250. * Return the offset to mmap.
  251. * See rvt_mmap() for details.
  252. */
  253. if (udata && udata->inlen >= sizeof(__u64)) {
  254. ret = ib_copy_to_udata(udata, &ip->offset,
  255. sizeof(ip->offset));
  256. if (ret)
  257. return ret;
  258. }
  259. /*
  260. * Put user mapping info onto the pending list
  261. * unless it already is on the list.
  262. */
  263. spin_lock_irq(&dev->pending_lock);
  264. if (list_empty(&ip->pending_mmaps))
  265. list_add(&ip->pending_mmaps,
  266. &dev->pending_mmaps);
  267. spin_unlock_irq(&dev->pending_lock);
  268. }
  269. } else if (attr_mask & IB_SRQ_LIMIT) {
  270. spin_lock_irq(&srq->rq.lock);
  271. if (attr->srq_limit >= srq->rq.size)
  272. ret = -EINVAL;
  273. else
  274. srq->limit = attr->srq_limit;
  275. spin_unlock_irq(&srq->rq.lock);
  276. }
  277. return ret;
  278. bail_unlock:
  279. spin_unlock_irq(&srq->rq.lock);
  280. bail_free:
  281. vfree(wq);
  282. return ret;
  283. }
  284. /** rvt_query_srq - query srq data
  285. * @ibsrq: srq to query
  286. * @attr: return info in attr
  287. *
  288. * Return: always 0
  289. */
  290. int rvt_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
  291. {
  292. struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
  293. attr->max_wr = srq->rq.size - 1;
  294. attr->max_sge = srq->rq.max_sge;
  295. attr->srq_limit = srq->limit;
  296. return 0;
  297. }
  298. /**
  299. * rvt_destroy_srq - destory an srq
  300. * @ibsrq: srq object to destroy
  301. *
  302. * Return always 0
  303. */
  304. int rvt_destroy_srq(struct ib_srq *ibsrq)
  305. {
  306. struct rvt_srq *srq = ibsrq_to_rvtsrq(ibsrq);
  307. struct rvt_dev_info *dev = ib_to_rvt(ibsrq->device);
  308. spin_lock(&dev->n_srqs_lock);
  309. dev->n_srqs_allocated--;
  310. spin_unlock(&dev->n_srqs_lock);
  311. if (srq->ip)
  312. kref_put(&srq->ip->ref, rvt_release_mmap_info);
  313. else
  314. vfree(srq->rq.wq);
  315. kfree(srq);
  316. return 0;
  317. }