ipath_srq.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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_ibdev *dev = to_idev(ibpd->device);
  121. struct ipath_srq *srq;
  122. u32 sz;
  123. struct ib_srq *ret;
  124. if (dev->n_srqs_allocated == ib_ipath_max_srqs) {
  125. ret = ERR_PTR(-ENOMEM);
  126. goto bail;
  127. }
  128. if (srq_init_attr->attr.max_wr == 0) {
  129. ret = ERR_PTR(-EINVAL);
  130. goto bail;
  131. }
  132. if ((srq_init_attr->attr.max_sge > ib_ipath_max_srq_sges) ||
  133. (srq_init_attr->attr.max_wr > ib_ipath_max_srq_wrs)) {
  134. ret = ERR_PTR(-EINVAL);
  135. goto bail;
  136. }
  137. srq = kmalloc(sizeof(*srq), GFP_KERNEL);
  138. if (!srq) {
  139. ret = ERR_PTR(-ENOMEM);
  140. goto bail;
  141. }
  142. /*
  143. * Need to use vmalloc() if we want to support large #s of entries.
  144. */
  145. srq->rq.size = srq_init_attr->attr.max_wr + 1;
  146. sz = sizeof(struct ipath_sge) * srq_init_attr->attr.max_sge +
  147. sizeof(struct ipath_rwqe);
  148. srq->rq.wq = vmalloc(srq->rq.size * sz);
  149. if (!srq->rq.wq) {
  150. kfree(srq);
  151. ret = ERR_PTR(-ENOMEM);
  152. goto bail;
  153. }
  154. /*
  155. * ib_create_srq() will initialize srq->ibsrq.
  156. */
  157. spin_lock_init(&srq->rq.lock);
  158. srq->rq.head = 0;
  159. srq->rq.tail = 0;
  160. srq->rq.max_sge = srq_init_attr->attr.max_sge;
  161. srq->limit = srq_init_attr->attr.srq_limit;
  162. ret = &srq->ibsrq;
  163. dev->n_srqs_allocated++;
  164. bail:
  165. return ret;
  166. }
  167. /**
  168. * ipath_modify_srq - modify a shared receive queue
  169. * @ibsrq: the SRQ to modify
  170. * @attr: the new attributes of the SRQ
  171. * @attr_mask: indicates which attributes to modify
  172. * @udata: user data for ipathverbs.so
  173. */
  174. int ipath_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
  175. enum ib_srq_attr_mask attr_mask,
  176. struct ib_udata *udata)
  177. {
  178. struct ipath_srq *srq = to_isrq(ibsrq);
  179. unsigned long flags;
  180. int ret;
  181. if (attr_mask & IB_SRQ_MAX_WR)
  182. if ((attr->max_wr > ib_ipath_max_srq_wrs) ||
  183. (attr->max_sge > srq->rq.max_sge)) {
  184. ret = -EINVAL;
  185. goto bail;
  186. }
  187. if (attr_mask & IB_SRQ_LIMIT)
  188. if (attr->srq_limit >= srq->rq.size) {
  189. ret = -EINVAL;
  190. goto bail;
  191. }
  192. if (attr_mask & IB_SRQ_MAX_WR) {
  193. struct ipath_rwqe *wq, *p;
  194. u32 sz, size, n;
  195. sz = sizeof(struct ipath_rwqe) +
  196. attr->max_sge * sizeof(struct ipath_sge);
  197. size = attr->max_wr + 1;
  198. wq = vmalloc(size * sz);
  199. if (!wq) {
  200. ret = -ENOMEM;
  201. goto bail;
  202. }
  203. spin_lock_irqsave(&srq->rq.lock, flags);
  204. if (srq->rq.head < srq->rq.tail)
  205. n = srq->rq.size + srq->rq.head - srq->rq.tail;
  206. else
  207. n = srq->rq.head - srq->rq.tail;
  208. if (size <= n || size <= srq->limit) {
  209. spin_unlock_irqrestore(&srq->rq.lock, flags);
  210. vfree(wq);
  211. ret = -EINVAL;
  212. goto bail;
  213. }
  214. n = 0;
  215. p = wq;
  216. while (srq->rq.tail != srq->rq.head) {
  217. struct ipath_rwqe *wqe;
  218. int i;
  219. wqe = get_rwqe_ptr(&srq->rq, srq->rq.tail);
  220. p->wr_id = wqe->wr_id;
  221. p->length = wqe->length;
  222. p->num_sge = wqe->num_sge;
  223. for (i = 0; i < wqe->num_sge; i++)
  224. p->sg_list[i] = wqe->sg_list[i];
  225. n++;
  226. p = (struct ipath_rwqe *)((char *) p + sz);
  227. if (++srq->rq.tail >= srq->rq.size)
  228. srq->rq.tail = 0;
  229. }
  230. vfree(srq->rq.wq);
  231. srq->rq.wq = wq;
  232. srq->rq.size = size;
  233. srq->rq.head = n;
  234. srq->rq.tail = 0;
  235. srq->rq.max_sge = attr->max_sge;
  236. spin_unlock_irqrestore(&srq->rq.lock, flags);
  237. }
  238. if (attr_mask & IB_SRQ_LIMIT) {
  239. spin_lock_irqsave(&srq->rq.lock, flags);
  240. srq->limit = attr->srq_limit;
  241. spin_unlock_irqrestore(&srq->rq.lock, flags);
  242. }
  243. ret = 0;
  244. bail:
  245. return ret;
  246. }
  247. int ipath_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
  248. {
  249. struct ipath_srq *srq = to_isrq(ibsrq);
  250. attr->max_wr = srq->rq.size - 1;
  251. attr->max_sge = srq->rq.max_sge;
  252. attr->srq_limit = srq->limit;
  253. return 0;
  254. }
  255. /**
  256. * ipath_destroy_srq - destroy a shared receive queue
  257. * @ibsrq: the SRQ to destroy
  258. */
  259. int ipath_destroy_srq(struct ib_srq *ibsrq)
  260. {
  261. struct ipath_srq *srq = to_isrq(ibsrq);
  262. struct ipath_ibdev *dev = to_idev(ibsrq->device);
  263. dev->n_srqs_allocated--;
  264. vfree(srq->rq.wq);
  265. kfree(srq);
  266. return 0;
  267. }