ipath_srq.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/err.h>
  33. #include <linux/vmalloc.h>
  34. #include "ipath_verbs.h"
  35. /**
  36. * ipath_post_srq_receive - post a receive on a shared receive queue
  37. * @ibsrq: the SRQ to post the receive on
  38. * @wr: the list of work requests to post
  39. * @bad_wr: the first WR to cause a problem is put here
  40. *
  41. * This may be called from interrupt context.
  42. */
  43. int ipath_post_srq_receive(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  44. struct ib_recv_wr **bad_wr)
  45. {
  46. struct ipath_srq *srq = to_isrq(ibsrq);
  47. struct ipath_ibdev *dev = to_idev(ibsrq->device);
  48. unsigned long flags;
  49. int ret;
  50. for (; wr; wr = wr->next) {
  51. struct ipath_rwqe *wqe;
  52. u32 next;
  53. int i, j;
  54. if (wr->num_sge > srq->rq.max_sge) {
  55. *bad_wr = wr;
  56. ret = -ENOMEM;
  57. goto bail;
  58. }
  59. spin_lock_irqsave(&srq->rq.lock, flags);
  60. next = srq->rq.head + 1;
  61. if (next >= srq->rq.size)
  62. next = 0;
  63. if (next == srq->rq.tail) {
  64. spin_unlock_irqrestore(&srq->rq.lock, flags);
  65. *bad_wr = wr;
  66. ret = -ENOMEM;
  67. goto bail;
  68. }
  69. wqe = get_rwqe_ptr(&srq->rq, srq->rq.head);
  70. wqe->wr_id = wr->wr_id;
  71. wqe->sg_list[0].mr = NULL;
  72. wqe->sg_list[0].vaddr = NULL;
  73. wqe->sg_list[0].length = 0;
  74. wqe->sg_list[0].sge_length = 0;
  75. wqe->length = 0;
  76. for (i = 0, j = 0; i < wr->num_sge; i++) {
  77. /* Check LKEY */
  78. if (to_ipd(srq->ibsrq.pd)->user &&
  79. wr->sg_list[i].lkey == 0) {
  80. spin_unlock_irqrestore(&srq->rq.lock,
  81. flags);
  82. *bad_wr = wr;
  83. ret = -EINVAL;
  84. goto bail;
  85. }
  86. if (wr->sg_list[i].length == 0)
  87. continue;
  88. if (!ipath_lkey_ok(&dev->lk_table,
  89. &wqe->sg_list[j],
  90. &wr->sg_list[i],
  91. IB_ACCESS_LOCAL_WRITE)) {
  92. spin_unlock_irqrestore(&srq->rq.lock,
  93. flags);
  94. *bad_wr = wr;
  95. ret = -EINVAL;
  96. goto bail;
  97. }
  98. wqe->length += wr->sg_list[i].length;
  99. j++;
  100. }
  101. wqe->num_sge = j;
  102. srq->rq.head = next;
  103. spin_unlock_irqrestore(&srq->rq.lock, flags);
  104. }
  105. ret = 0;
  106. bail:
  107. return ret;
  108. }
  109. /**
  110. * ipath_create_srq - create a shared receive queue
  111. * @ibpd: the protection domain of the SRQ to create
  112. * @attr: the attributes of the SRQ
  113. * @udata: not used by the InfiniPath verbs driver
  114. */
  115. struct ib_srq *ipath_create_srq(struct ib_pd *ibpd,
  116. struct ib_srq_init_attr *srq_init_attr,
  117. struct ib_udata *udata)
  118. {
  119. struct ipath_srq *srq;
  120. u32 sz;
  121. struct ib_srq *ret;
  122. if (srq_init_attr->attr.max_sge < 1) {
  123. ret = ERR_PTR(-EINVAL);
  124. goto bail;
  125. }
  126. srq = kmalloc(sizeof(*srq), GFP_KERNEL);
  127. if (!srq) {
  128. ret = ERR_PTR(-ENOMEM);
  129. goto bail;
  130. }
  131. /*
  132. * Need to use vmalloc() if we want to support large #s of entries.
  133. */
  134. srq->rq.size = srq_init_attr->attr.max_wr + 1;
  135. sz = sizeof(struct ipath_sge) * srq_init_attr->attr.max_sge +
  136. sizeof(struct ipath_rwqe);
  137. srq->rq.wq = vmalloc(srq->rq.size * sz);
  138. if (!srq->rq.wq) {
  139. kfree(srq);
  140. ret = ERR_PTR(-ENOMEM);
  141. goto bail;
  142. }
  143. /*
  144. * ib_create_srq() will initialize srq->ibsrq.
  145. */
  146. spin_lock_init(&srq->rq.lock);
  147. srq->rq.head = 0;
  148. srq->rq.tail = 0;
  149. srq->rq.max_sge = srq_init_attr->attr.max_sge;
  150. srq->limit = srq_init_attr->attr.srq_limit;
  151. ret = &srq->ibsrq;
  152. bail:
  153. return ret;
  154. }
  155. /**
  156. * ipath_modify_srq - modify a shared receive queue
  157. * @ibsrq: the SRQ to modify
  158. * @attr: the new attributes of the SRQ
  159. * @attr_mask: indicates which attributes to modify
  160. */
  161. int ipath_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
  162. enum ib_srq_attr_mask attr_mask)
  163. {
  164. struct ipath_srq *srq = to_isrq(ibsrq);
  165. unsigned long flags;
  166. int ret;
  167. if (attr_mask & IB_SRQ_LIMIT) {
  168. spin_lock_irqsave(&srq->rq.lock, flags);
  169. srq->limit = attr->srq_limit;
  170. spin_unlock_irqrestore(&srq->rq.lock, flags);
  171. }
  172. if (attr_mask & IB_SRQ_MAX_WR) {
  173. u32 size = attr->max_wr + 1;
  174. struct ipath_rwqe *wq, *p;
  175. u32 n;
  176. u32 sz;
  177. if (attr->max_sge < srq->rq.max_sge) {
  178. ret = -EINVAL;
  179. goto bail;
  180. }
  181. sz = sizeof(struct ipath_rwqe) +
  182. attr->max_sge * sizeof(struct ipath_sge);
  183. wq = vmalloc(size * sz);
  184. if (!wq) {
  185. ret = -ENOMEM;
  186. goto bail;
  187. }
  188. spin_lock_irqsave(&srq->rq.lock, flags);
  189. if (srq->rq.head < srq->rq.tail)
  190. n = srq->rq.size + srq->rq.head - srq->rq.tail;
  191. else
  192. n = srq->rq.head - srq->rq.tail;
  193. if (size <= n || size <= srq->limit) {
  194. spin_unlock_irqrestore(&srq->rq.lock, flags);
  195. vfree(wq);
  196. ret = -EINVAL;
  197. goto bail;
  198. }
  199. n = 0;
  200. p = wq;
  201. while (srq->rq.tail != srq->rq.head) {
  202. struct ipath_rwqe *wqe;
  203. int i;
  204. wqe = get_rwqe_ptr(&srq->rq, srq->rq.tail);
  205. p->wr_id = wqe->wr_id;
  206. p->length = wqe->length;
  207. p->num_sge = wqe->num_sge;
  208. for (i = 0; i < wqe->num_sge; i++)
  209. p->sg_list[i] = wqe->sg_list[i];
  210. n++;
  211. p = (struct ipath_rwqe *)((char *) p + sz);
  212. if (++srq->rq.tail >= srq->rq.size)
  213. srq->rq.tail = 0;
  214. }
  215. vfree(srq->rq.wq);
  216. srq->rq.wq = wq;
  217. srq->rq.size = size;
  218. srq->rq.head = n;
  219. srq->rq.tail = 0;
  220. srq->rq.max_sge = attr->max_sge;
  221. spin_unlock_irqrestore(&srq->rq.lock, flags);
  222. }
  223. ret = 0;
  224. bail:
  225. return ret;
  226. }
  227. int ipath_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
  228. {
  229. struct ipath_srq *srq = to_isrq(ibsrq);
  230. attr->max_wr = srq->rq.size - 1;
  231. attr->max_sge = srq->rq.max_sge;
  232. attr->srq_limit = srq->limit;
  233. return 0;
  234. }
  235. /**
  236. * ipath_destroy_srq - destroy a shared receive queue
  237. * @ibsrq: the SRQ to destroy
  238. */
  239. int ipath_destroy_srq(struct ib_srq *ibsrq)
  240. {
  241. struct ipath_srq *srq = to_isrq(ibsrq);
  242. vfree(srq->rq.wq);
  243. kfree(srq);
  244. return 0;
  245. }