qib_srq.c 9.2 KB

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