ipath_srq.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (c) 2006 QLogic, Inc. 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/vmalloc.h>
  35. #include "ipath_verbs.h"
  36. /**
  37. * ipath_post_srq_receive - post a receive on a shared receive queue
  38. * @ibsrq: the SRQ to post the receive on
  39. * @wr: the list of work requests to post
  40. * @bad_wr: the first WR to cause a problem is put here
  41. *
  42. * This may be called from interrupt context.
  43. */
  44. int ipath_post_srq_receive(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  45. struct ib_recv_wr **bad_wr)
  46. {
  47. struct ipath_srq *srq = to_isrq(ibsrq);
  48. struct ipath_ibdev *dev = to_idev(ibsrq->device);
  49. unsigned long flags;
  50. int ret;
  51. for (; wr; wr = wr->next) {
  52. struct ipath_rwqe *wqe;
  53. u32 next;
  54. int i, j;
  55. if (wr->num_sge > srq->rq.max_sge) {
  56. *bad_wr = wr;
  57. ret = -ENOMEM;
  58. goto bail;
  59. }
  60. spin_lock_irqsave(&srq->rq.lock, flags);
  61. next = srq->rq.head + 1;
  62. if (next >= srq->rq.size)
  63. next = 0;
  64. if (next == srq->rq.tail) {
  65. spin_unlock_irqrestore(&srq->rq.lock, flags);
  66. *bad_wr = wr;
  67. ret = -ENOMEM;
  68. goto bail;
  69. }
  70. wqe = get_rwqe_ptr(&srq->rq, srq->rq.head);
  71. wqe->wr_id = wr->wr_id;
  72. wqe->sg_list[0].mr = NULL;
  73. wqe->sg_list[0].vaddr = NULL;
  74. wqe->sg_list[0].length = 0;
  75. wqe->sg_list[0].sge_length = 0;
  76. wqe->length = 0;
  77. for (i = 0, j = 0; i < wr->num_sge; i++) {
  78. /* Check LKEY */
  79. if (to_ipd(srq->ibsrq.pd)->user &&
  80. wr->sg_list[i].lkey == 0) {
  81. spin_unlock_irqrestore(&srq->rq.lock,
  82. flags);
  83. *bad_wr = wr;
  84. ret = -EINVAL;
  85. goto bail;
  86. }
  87. if (wr->sg_list[i].length == 0)
  88. continue;
  89. if (!ipath_lkey_ok(&dev->lk_table,
  90. &wqe->sg_list[j],
  91. &wr->sg_list[i],
  92. IB_ACCESS_LOCAL_WRITE)) {
  93. spin_unlock_irqrestore(&srq->rq.lock,
  94. flags);
  95. *bad_wr = wr;
  96. ret = -EINVAL;
  97. goto bail;
  98. }
  99. wqe->length += wr->sg_list[i].length;
  100. j++;
  101. }
  102. wqe->num_sge = j;
  103. srq->rq.head = next;
  104. spin_unlock_irqrestore(&srq->rq.lock, flags);
  105. }
  106. ret = 0;
  107. bail:
  108. return ret;
  109. }
  110. /**
  111. * ipath_create_srq - create a shared receive queue
  112. * @ibpd: the protection domain of the SRQ to create
  113. * @attr: the attributes of the SRQ
  114. * @udata: not used by the InfiniPath verbs driver
  115. */
  116. struct ib_srq *ipath_create_srq(struct ib_pd *ibpd,
  117. struct ib_srq_init_attr *srq_init_attr,
  118. struct ib_udata *udata)
  119. {
  120. struct ipath_srq *srq;
  121. u32 sz;
  122. struct ib_srq *ret;
  123. if (srq_init_attr->attr.max_sge < 1) {
  124. ret = ERR_PTR(-EINVAL);
  125. goto bail;
  126. }
  127. srq = kmalloc(sizeof(*srq), GFP_KERNEL);
  128. if (!srq) {
  129. ret = ERR_PTR(-ENOMEM);
  130. goto bail;
  131. }
  132. /*
  133. * Need to use vmalloc() if we want to support large #s of entries.
  134. */
  135. srq->rq.size = srq_init_attr->attr.max_wr + 1;
  136. sz = sizeof(struct ipath_sge) * srq_init_attr->attr.max_sge +
  137. sizeof(struct ipath_rwqe);
  138. srq->rq.wq = vmalloc(srq->rq.size * sz);
  139. if (!srq->rq.wq) {
  140. kfree(srq);
  141. ret = ERR_PTR(-ENOMEM);
  142. goto bail;
  143. }
  144. /*
  145. * ib_create_srq() will initialize srq->ibsrq.
  146. */
  147. spin_lock_init(&srq->rq.lock);
  148. srq->rq.head = 0;
  149. srq->rq.tail = 0;
  150. srq->rq.max_sge = srq_init_attr->attr.max_sge;
  151. srq->limit = srq_init_attr->attr.srq_limit;
  152. ret = &srq->ibsrq;
  153. bail:
  154. return ret;
  155. }
  156. /**
  157. * ipath_modify_srq - modify a shared receive queue
  158. * @ibsrq: the SRQ to modify
  159. * @attr: the new attributes of the SRQ
  160. * @attr_mask: indicates which attributes to modify
  161. */
  162. int ipath_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
  163. enum ib_srq_attr_mask attr_mask)
  164. {
  165. struct ipath_srq *srq = to_isrq(ibsrq);
  166. unsigned long flags;
  167. int ret;
  168. if (attr_mask & IB_SRQ_LIMIT) {
  169. spin_lock_irqsave(&srq->rq.lock, flags);
  170. srq->limit = attr->srq_limit;
  171. spin_unlock_irqrestore(&srq->rq.lock, flags);
  172. }
  173. if (attr_mask & IB_SRQ_MAX_WR) {
  174. u32 size = attr->max_wr + 1;
  175. struct ipath_rwqe *wq, *p;
  176. u32 n;
  177. u32 sz;
  178. if (attr->max_sge < srq->rq.max_sge) {
  179. ret = -EINVAL;
  180. goto bail;
  181. }
  182. sz = sizeof(struct ipath_rwqe) +
  183. attr->max_sge * sizeof(struct ipath_sge);
  184. wq = vmalloc(size * sz);
  185. if (!wq) {
  186. ret = -ENOMEM;
  187. goto bail;
  188. }
  189. spin_lock_irqsave(&srq->rq.lock, flags);
  190. if (srq->rq.head < srq->rq.tail)
  191. n = srq->rq.size + srq->rq.head - srq->rq.tail;
  192. else
  193. n = srq->rq.head - srq->rq.tail;
  194. if (size <= n || size <= srq->limit) {
  195. spin_unlock_irqrestore(&srq->rq.lock, flags);
  196. vfree(wq);
  197. ret = -EINVAL;
  198. goto bail;
  199. }
  200. n = 0;
  201. p = wq;
  202. while (srq->rq.tail != srq->rq.head) {
  203. struct ipath_rwqe *wqe;
  204. int i;
  205. wqe = get_rwqe_ptr(&srq->rq, srq->rq.tail);
  206. p->wr_id = wqe->wr_id;
  207. p->length = wqe->length;
  208. p->num_sge = wqe->num_sge;
  209. for (i = 0; i < wqe->num_sge; i++)
  210. p->sg_list[i] = wqe->sg_list[i];
  211. n++;
  212. p = (struct ipath_rwqe *)((char *) p + sz);
  213. if (++srq->rq.tail >= srq->rq.size)
  214. srq->rq.tail = 0;
  215. }
  216. vfree(srq->rq.wq);
  217. srq->rq.wq = wq;
  218. srq->rq.size = size;
  219. srq->rq.head = n;
  220. srq->rq.tail = 0;
  221. srq->rq.max_sge = attr->max_sge;
  222. spin_unlock_irqrestore(&srq->rq.lock, flags);
  223. }
  224. ret = 0;
  225. bail:
  226. return ret;
  227. }
  228. int ipath_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
  229. {
  230. struct ipath_srq *srq = to_isrq(ibsrq);
  231. attr->max_wr = srq->rq.size - 1;
  232. attr->max_sge = srq->rq.max_sge;
  233. attr->srq_limit = srq->limit;
  234. return 0;
  235. }
  236. /**
  237. * ipath_destroy_srq - destroy a shared receive queue
  238. * @ibsrq: the SRQ to destroy
  239. */
  240. int ipath_destroy_srq(struct ib_srq *ibsrq)
  241. {
  242. struct ipath_srq *srq = to_isrq(ibsrq);
  243. vfree(srq->rq.wq);
  244. kfree(srq);
  245. return 0;
  246. }