qib_srq.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /*
  2. * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
  3. * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/err.h>
  34. #include <linux/slab.h>
  35. #include <linux/vmalloc.h>
  36. #include "qib_verbs.h"
  37. /**
  38. * qib_post_srq_receive - post a receive on a shared receive queue
  39. * @ibsrq: the SRQ to post the receive on
  40. * @wr: the list of work requests to post
  41. * @bad_wr: A pointer to the first WR to cause a problem is put here
  42. *
  43. * This may be called from interrupt context.
  44. */
  45. int qib_post_srq_receive(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  46. struct ib_recv_wr **bad_wr)
  47. {
  48. struct qib_srq *srq = to_isrq(ibsrq);
  49. struct qib_rwq *wq;
  50. unsigned long flags;
  51. int ret;
  52. for (; wr; wr = wr->next) {
  53. struct qib_rwqe *wqe;
  54. u32 next;
  55. int i;
  56. if ((unsigned) wr->num_sge > srq->rq.max_sge) {
  57. *bad_wr = wr;
  58. ret = -EINVAL;
  59. goto bail;
  60. }
  61. spin_lock_irqsave(&srq->rq.lock, flags);
  62. wq = srq->rq.wq;
  63. next = wq->head + 1;
  64. if (next >= srq->rq.size)
  65. next = 0;
  66. if (next == wq->tail) {
  67. spin_unlock_irqrestore(&srq->rq.lock, flags);
  68. *bad_wr = wr;
  69. ret = -ENOMEM;
  70. goto bail;
  71. }
  72. wqe = get_rwqe_ptr(&srq->rq, wq->head);
  73. wqe->wr_id = wr->wr_id;
  74. wqe->num_sge = wr->num_sge;
  75. for (i = 0; i < wr->num_sge; i++)
  76. wqe->sg_list[i] = wr->sg_list[i];
  77. /* Make sure queue entry is written before the head index. */
  78. smp_wmb();
  79. wq->head = next;
  80. spin_unlock_irqrestore(&srq->rq.lock, flags);
  81. }
  82. ret = 0;
  83. bail:
  84. return ret;
  85. }
  86. /**
  87. * qib_create_srq - create a shared receive queue
  88. * @ibpd: the protection domain of the SRQ to create
  89. * @srq_init_attr: the attributes of the SRQ
  90. * @udata: data from libibverbs when creating a user SRQ
  91. */
  92. struct ib_srq *qib_create_srq(struct ib_pd *ibpd,
  93. struct ib_srq_init_attr *srq_init_attr,
  94. struct ib_udata *udata)
  95. {
  96. struct qib_ibdev *dev = to_idev(ibpd->device);
  97. struct qib_srq *srq;
  98. u32 sz;
  99. struct ib_srq *ret;
  100. if (srq_init_attr->attr.max_sge == 0 ||
  101. srq_init_attr->attr.max_sge > ib_qib_max_srq_sges ||
  102. srq_init_attr->attr.max_wr == 0 ||
  103. srq_init_attr->attr.max_wr > ib_qib_max_srq_wrs) {
  104. ret = ERR_PTR(-EINVAL);
  105. goto done;
  106. }
  107. srq = kmalloc(sizeof(*srq), GFP_KERNEL);
  108. if (!srq) {
  109. ret = ERR_PTR(-ENOMEM);
  110. goto done;
  111. }
  112. /*
  113. * Need to use vmalloc() if we want to support large #s of entries.
  114. */
  115. srq->rq.size = srq_init_attr->attr.max_wr + 1;
  116. srq->rq.max_sge = srq_init_attr->attr.max_sge;
  117. sz = sizeof(struct ib_sge) * srq->rq.max_sge +
  118. sizeof(struct qib_rwqe);
  119. srq->rq.wq = vmalloc_user(sizeof(struct qib_rwq) + srq->rq.size * sz);
  120. if (!srq->rq.wq) {
  121. ret = ERR_PTR(-ENOMEM);
  122. goto bail_srq;
  123. }
  124. /*
  125. * Return the address of the RWQ as the offset to mmap.
  126. * See qib_mmap() for details.
  127. */
  128. if (udata && udata->outlen >= sizeof(__u64)) {
  129. int err;
  130. u32 s = sizeof(struct qib_rwq) + srq->rq.size * sz;
  131. srq->ip =
  132. qib_create_mmap_info(dev, s, ibpd->uobject->context,
  133. srq->rq.wq);
  134. if (!srq->ip) {
  135. ret = ERR_PTR(-ENOMEM);
  136. goto bail_wq;
  137. }
  138. err = ib_copy_to_udata(udata, &srq->ip->offset,
  139. sizeof(srq->ip->offset));
  140. if (err) {
  141. ret = ERR_PTR(err);
  142. goto bail_ip;
  143. }
  144. } else
  145. srq->ip = NULL;
  146. /*
  147. * ib_create_srq() will initialize srq->ibsrq.
  148. */
  149. spin_lock_init(&srq->rq.lock);
  150. srq->rq.wq->head = 0;
  151. srq->rq.wq->tail = 0;
  152. srq->limit = srq_init_attr->attr.srq_limit;
  153. spin_lock(&dev->n_srqs_lock);
  154. if (dev->n_srqs_allocated == ib_qib_max_srqs) {
  155. spin_unlock(&dev->n_srqs_lock);
  156. ret = ERR_PTR(-ENOMEM);
  157. goto bail_ip;
  158. }
  159. dev->n_srqs_allocated++;
  160. spin_unlock(&dev->n_srqs_lock);
  161. if (srq->ip) {
  162. spin_lock_irq(&dev->pending_lock);
  163. list_add(&srq->ip->pending_mmaps, &dev->pending_mmaps);
  164. spin_unlock_irq(&dev->pending_lock);
  165. }
  166. ret = &srq->ibsrq;
  167. goto done;
  168. bail_ip:
  169. kfree(srq->ip);
  170. bail_wq:
  171. vfree(srq->rq.wq);
  172. bail_srq:
  173. kfree(srq);
  174. done:
  175. return ret;
  176. }
  177. /**
  178. * qib_modify_srq - modify a shared receive queue
  179. * @ibsrq: the SRQ to modify
  180. * @attr: the new attributes of the SRQ
  181. * @attr_mask: indicates which attributes to modify
  182. * @udata: user data for libibverbs.so
  183. */
  184. int qib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
  185. enum ib_srq_attr_mask attr_mask,
  186. struct ib_udata *udata)
  187. {
  188. struct qib_srq *srq = to_isrq(ibsrq);
  189. struct qib_rwq *wq;
  190. int ret = 0;
  191. if (attr_mask & IB_SRQ_MAX_WR) {
  192. struct qib_rwq *owq;
  193. struct qib_rwqe *p;
  194. u32 sz, size, n, head, tail;
  195. /* Check that the requested sizes are below the limits. */
  196. if ((attr->max_wr > ib_qib_max_srq_wrs) ||
  197. ((attr_mask & IB_SRQ_LIMIT) ?
  198. attr->srq_limit : srq->limit) > attr->max_wr) {
  199. ret = -EINVAL;
  200. goto bail;
  201. }
  202. sz = sizeof(struct qib_rwqe) +
  203. srq->rq.max_sge * sizeof(struct ib_sge);
  204. size = attr->max_wr + 1;
  205. wq = vmalloc_user(sizeof(struct qib_rwq) + size * sz);
  206. if (!wq) {
  207. ret = -ENOMEM;
  208. goto bail;
  209. }
  210. /* Check that we can write the offset to mmap. */
  211. if (udata && udata->inlen >= sizeof(__u64)) {
  212. __u64 offset_addr;
  213. __u64 offset = 0;
  214. ret = ib_copy_from_udata(&offset_addr, udata,
  215. sizeof(offset_addr));
  216. if (ret)
  217. goto bail_free;
  218. udata->outbuf =
  219. (void __user *) (unsigned long) offset_addr;
  220. ret = ib_copy_to_udata(udata, &offset,
  221. sizeof(offset));
  222. if (ret)
  223. goto bail_free;
  224. }
  225. spin_lock_irq(&srq->rq.lock);
  226. /*
  227. * validate head and tail pointer values and compute
  228. * the number of remaining WQEs.
  229. */
  230. owq = srq->rq.wq;
  231. head = owq->head;
  232. tail = owq->tail;
  233. if (head >= srq->rq.size || tail >= srq->rq.size) {
  234. ret = -EINVAL;
  235. goto bail_unlock;
  236. }
  237. n = head;
  238. if (n < tail)
  239. n += srq->rq.size - tail;
  240. else
  241. n -= tail;
  242. if (size <= n) {
  243. ret = -EINVAL;
  244. goto bail_unlock;
  245. }
  246. n = 0;
  247. p = wq->wq;
  248. while (tail != head) {
  249. struct qib_rwqe *wqe;
  250. int i;
  251. wqe = get_rwqe_ptr(&srq->rq, tail);
  252. p->wr_id = wqe->wr_id;
  253. p->num_sge = wqe->num_sge;
  254. for (i = 0; i < wqe->num_sge; i++)
  255. p->sg_list[i] = wqe->sg_list[i];
  256. n++;
  257. p = (struct qib_rwqe *)((char *) p + sz);
  258. if (++tail >= srq->rq.size)
  259. tail = 0;
  260. }
  261. srq->rq.wq = wq;
  262. srq->rq.size = size;
  263. wq->head = n;
  264. wq->tail = 0;
  265. if (attr_mask & IB_SRQ_LIMIT)
  266. srq->limit = attr->srq_limit;
  267. spin_unlock_irq(&srq->rq.lock);
  268. vfree(owq);
  269. if (srq->ip) {
  270. struct qib_mmap_info *ip = srq->ip;
  271. struct qib_ibdev *dev = to_idev(srq->ibsrq.device);
  272. u32 s = sizeof(struct qib_rwq) + size * sz;
  273. qib_update_mmap_info(dev, ip, s, wq);
  274. /*
  275. * Return the offset to mmap.
  276. * See qib_mmap() for details.
  277. */
  278. if (udata && udata->inlen >= sizeof(__u64)) {
  279. ret = ib_copy_to_udata(udata, &ip->offset,
  280. sizeof(ip->offset));
  281. if (ret)
  282. goto bail;
  283. }
  284. /*
  285. * Put user mapping info onto the pending list
  286. * unless it already is on the list.
  287. */
  288. spin_lock_irq(&dev->pending_lock);
  289. if (list_empty(&ip->pending_mmaps))
  290. list_add(&ip->pending_mmaps,
  291. &dev->pending_mmaps);
  292. spin_unlock_irq(&dev->pending_lock);
  293. }
  294. } else if (attr_mask & IB_SRQ_LIMIT) {
  295. spin_lock_irq(&srq->rq.lock);
  296. if (attr->srq_limit >= srq->rq.size)
  297. ret = -EINVAL;
  298. else
  299. srq->limit = attr->srq_limit;
  300. spin_unlock_irq(&srq->rq.lock);
  301. }
  302. goto bail;
  303. bail_unlock:
  304. spin_unlock_irq(&srq->rq.lock);
  305. bail_free:
  306. vfree(wq);
  307. bail:
  308. return ret;
  309. }
  310. int qib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
  311. {
  312. struct qib_srq *srq = to_isrq(ibsrq);
  313. attr->max_wr = srq->rq.size - 1;
  314. attr->max_sge = srq->rq.max_sge;
  315. attr->srq_limit = srq->limit;
  316. return 0;
  317. }
  318. /**
  319. * qib_destroy_srq - destroy a shared receive queue
  320. * @ibsrq: the SRQ to destroy
  321. */
  322. int qib_destroy_srq(struct ib_srq *ibsrq)
  323. {
  324. struct qib_srq *srq = to_isrq(ibsrq);
  325. struct qib_ibdev *dev = to_idev(ibsrq->device);
  326. spin_lock(&dev->n_srqs_lock);
  327. dev->n_srqs_allocated--;
  328. spin_unlock(&dev->n_srqs_lock);
  329. if (srq->ip)
  330. kref_put(&srq->ip->ref, qib_release_mmap_info);
  331. else
  332. vfree(srq->rq.wq);
  333. kfree(srq);
  334. return 0;
  335. }