ipath_srq.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. */
  173. int ipath_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
  174. enum ib_srq_attr_mask attr_mask)
  175. {
  176. struct ipath_srq *srq = to_isrq(ibsrq);
  177. unsigned long flags;
  178. int ret;
  179. if (attr_mask & IB_SRQ_MAX_WR)
  180. if ((attr->max_wr > ib_ipath_max_srq_wrs) ||
  181. (attr->max_sge > srq->rq.max_sge)) {
  182. ret = -EINVAL;
  183. goto bail;
  184. }
  185. if (attr_mask & IB_SRQ_LIMIT)
  186. if (attr->srq_limit >= srq->rq.size) {
  187. ret = -EINVAL;
  188. goto bail;
  189. }
  190. if (attr_mask & IB_SRQ_MAX_WR) {
  191. struct ipath_rwqe *wq, *p;
  192. u32 sz, size, n;
  193. sz = sizeof(struct ipath_rwqe) +
  194. attr->max_sge * sizeof(struct ipath_sge);
  195. size = attr->max_wr + 1;
  196. wq = vmalloc(size * sz);
  197. if (!wq) {
  198. ret = -ENOMEM;
  199. goto bail;
  200. }
  201. spin_lock_irqsave(&srq->rq.lock, flags);
  202. if (srq->rq.head < srq->rq.tail)
  203. n = srq->rq.size + srq->rq.head - srq->rq.tail;
  204. else
  205. n = srq->rq.head - srq->rq.tail;
  206. if (size <= n || size <= srq->limit) {
  207. spin_unlock_irqrestore(&srq->rq.lock, flags);
  208. vfree(wq);
  209. ret = -EINVAL;
  210. goto bail;
  211. }
  212. n = 0;
  213. p = wq;
  214. while (srq->rq.tail != srq->rq.head) {
  215. struct ipath_rwqe *wqe;
  216. int i;
  217. wqe = get_rwqe_ptr(&srq->rq, srq->rq.tail);
  218. p->wr_id = wqe->wr_id;
  219. p->length = wqe->length;
  220. p->num_sge = wqe->num_sge;
  221. for (i = 0; i < wqe->num_sge; i++)
  222. p->sg_list[i] = wqe->sg_list[i];
  223. n++;
  224. p = (struct ipath_rwqe *)((char *) p + sz);
  225. if (++srq->rq.tail >= srq->rq.size)
  226. srq->rq.tail = 0;
  227. }
  228. vfree(srq->rq.wq);
  229. srq->rq.wq = wq;
  230. srq->rq.size = size;
  231. srq->rq.head = n;
  232. srq->rq.tail = 0;
  233. srq->rq.max_sge = attr->max_sge;
  234. spin_unlock_irqrestore(&srq->rq.lock, flags);
  235. }
  236. if (attr_mask & IB_SRQ_LIMIT) {
  237. spin_lock_irqsave(&srq->rq.lock, flags);
  238. srq->limit = attr->srq_limit;
  239. spin_unlock_irqrestore(&srq->rq.lock, flags);
  240. }
  241. ret = 0;
  242. bail:
  243. return ret;
  244. }
  245. int ipath_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
  246. {
  247. struct ipath_srq *srq = to_isrq(ibsrq);
  248. attr->max_wr = srq->rq.size - 1;
  249. attr->max_sge = srq->rq.max_sge;
  250. attr->srq_limit = srq->limit;
  251. return 0;
  252. }
  253. /**
  254. * ipath_destroy_srq - destroy a shared receive queue
  255. * @ibsrq: the SRQ to destroy
  256. */
  257. int ipath_destroy_srq(struct ib_srq *ibsrq)
  258. {
  259. struct ipath_srq *srq = to_isrq(ibsrq);
  260. struct ipath_ibdev *dev = to_idev(ibsrq->device);
  261. dev->n_srqs_allocated--;
  262. vfree(srq->rq.wq);
  263. kfree(srq);
  264. return 0;
  265. }