ipath_srq.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Copyright (c) 2006, 2007, 2008 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/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_rwq *wq;
  49. unsigned long flags;
  50. int ret;
  51. for (; wr; wr = wr->next) {
  52. struct ipath_rwqe *wqe;
  53. u32 next;
  54. int i;
  55. if ((unsigned) wr->num_sge > srq->rq.max_sge) {
  56. *bad_wr = wr;
  57. ret = -EINVAL;
  58. goto bail;
  59. }
  60. spin_lock_irqsave(&srq->rq.lock, flags);
  61. wq = srq->rq.wq;
  62. next = wq->head + 1;
  63. if (next >= srq->rq.size)
  64. next = 0;
  65. if (next == wq->tail) {
  66. spin_unlock_irqrestore(&srq->rq.lock, flags);
  67. *bad_wr = wr;
  68. ret = -ENOMEM;
  69. goto bail;
  70. }
  71. wqe = get_rwqe_ptr(&srq->rq, wq->head);
  72. wqe->wr_id = wr->wr_id;
  73. wqe->num_sge = wr->num_sge;
  74. for (i = 0; i < wr->num_sge; i++)
  75. wqe->sg_list[i] = wr->sg_list[i];
  76. /* Make sure queue entry is written before the head index. */
  77. smp_wmb();
  78. wq->head = next;
  79. spin_unlock_irqrestore(&srq->rq.lock, flags);
  80. }
  81. ret = 0;
  82. bail:
  83. return ret;
  84. }
  85. /**
  86. * ipath_create_srq - create a shared receive queue
  87. * @ibpd: the protection domain of the SRQ to create
  88. * @srq_init_attr: the attributes of the SRQ
  89. * @udata: data from libipathverbs when creating a user SRQ
  90. */
  91. struct ib_srq *ipath_create_srq(struct ib_pd *ibpd,
  92. struct ib_srq_init_attr *srq_init_attr,
  93. struct ib_udata *udata)
  94. {
  95. struct ipath_ibdev *dev = to_idev(ibpd->device);
  96. struct ipath_srq *srq;
  97. u32 sz;
  98. struct ib_srq *ret;
  99. if (srq_init_attr->attr.max_wr == 0) {
  100. ret = ERR_PTR(-EINVAL);
  101. goto done;
  102. }
  103. if ((srq_init_attr->attr.max_sge > ib_ipath_max_srq_sges) ||
  104. (srq_init_attr->attr.max_wr > ib_ipath_max_srq_wrs)) {
  105. ret = ERR_PTR(-EINVAL);
  106. goto done;
  107. }
  108. srq = kmalloc(sizeof(*srq), GFP_KERNEL);
  109. if (!srq) {
  110. ret = ERR_PTR(-ENOMEM);
  111. goto done;
  112. }
  113. /*
  114. * Need to use vmalloc() if we want to support large #s of entries.
  115. */
  116. srq->rq.size = srq_init_attr->attr.max_wr + 1;
  117. srq->rq.max_sge = srq_init_attr->attr.max_sge;
  118. sz = sizeof(struct ib_sge) * srq->rq.max_sge +
  119. sizeof(struct ipath_rwqe);
  120. srq->rq.wq = vmalloc_user(sizeof(struct ipath_rwq) + srq->rq.size * sz);
  121. if (!srq->rq.wq) {
  122. ret = ERR_PTR(-ENOMEM);
  123. goto bail_srq;
  124. }
  125. /*
  126. * Return the address of the RWQ as the offset to mmap.
  127. * See ipath_mmap() for details.
  128. */
  129. if (udata && udata->outlen >= sizeof(__u64)) {
  130. int err;
  131. u32 s = sizeof(struct ipath_rwq) + srq->rq.size * sz;
  132. srq->ip =
  133. ipath_create_mmap_info(dev, s,
  134. ibpd->uobject->context,
  135. srq->rq.wq);
  136. if (!srq->ip) {
  137. ret = ERR_PTR(-ENOMEM);
  138. goto bail_wq;
  139. }
  140. err = ib_copy_to_udata(udata, &srq->ip->offset,
  141. sizeof(srq->ip->offset));
  142. if (err) {
  143. ret = ERR_PTR(err);
  144. goto bail_ip;
  145. }
  146. } else
  147. srq->ip = NULL;
  148. /*
  149. * ib_create_srq() will initialize srq->ibsrq.
  150. */
  151. spin_lock_init(&srq->rq.lock);
  152. srq->rq.wq->head = 0;
  153. srq->rq.wq->tail = 0;
  154. srq->limit = srq_init_attr->attr.srq_limit;
  155. spin_lock(&dev->n_srqs_lock);
  156. if (dev->n_srqs_allocated == ib_ipath_max_srqs) {
  157. spin_unlock(&dev->n_srqs_lock);
  158. ret = ERR_PTR(-ENOMEM);
  159. goto bail_ip;
  160. }
  161. dev->n_srqs_allocated++;
  162. spin_unlock(&dev->n_srqs_lock);
  163. if (srq->ip) {
  164. spin_lock_irq(&dev->pending_lock);
  165. list_add(&srq->ip->pending_mmaps, &dev->pending_mmaps);
  166. spin_unlock_irq(&dev->pending_lock);
  167. }
  168. ret = &srq->ibsrq;
  169. goto done;
  170. bail_ip:
  171. kfree(srq->ip);
  172. bail_wq:
  173. vfree(srq->rq.wq);
  174. bail_srq:
  175. kfree(srq);
  176. done:
  177. return ret;
  178. }
  179. /**
  180. * ipath_modify_srq - modify a shared receive queue
  181. * @ibsrq: the SRQ to modify
  182. * @attr: the new attributes of the SRQ
  183. * @attr_mask: indicates which attributes to modify
  184. * @udata: user data for ipathverbs.so
  185. */
  186. int ipath_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
  187. enum ib_srq_attr_mask attr_mask,
  188. struct ib_udata *udata)
  189. {
  190. struct ipath_srq *srq = to_isrq(ibsrq);
  191. struct ipath_rwq *wq;
  192. int ret = 0;
  193. if (attr_mask & IB_SRQ_MAX_WR) {
  194. struct ipath_rwq *owq;
  195. struct ipath_rwqe *p;
  196. u32 sz, size, n, head, tail;
  197. /* Check that the requested sizes are below the limits. */
  198. if ((attr->max_wr > ib_ipath_max_srq_wrs) ||
  199. ((attr_mask & IB_SRQ_LIMIT) ?
  200. attr->srq_limit : srq->limit) > attr->max_wr) {
  201. ret = -EINVAL;
  202. goto bail;
  203. }
  204. sz = sizeof(struct ipath_rwqe) +
  205. srq->rq.max_sge * sizeof(struct ib_sge);
  206. size = attr->max_wr + 1;
  207. wq = vmalloc_user(sizeof(struct ipath_rwq) + size * sz);
  208. if (!wq) {
  209. ret = -ENOMEM;
  210. goto bail;
  211. }
  212. /* Check that we can write the offset to mmap. */
  213. if (udata && udata->inlen >= sizeof(__u64)) {
  214. __u64 offset_addr;
  215. __u64 offset = 0;
  216. ret = ib_copy_from_udata(&offset_addr, udata,
  217. sizeof(offset_addr));
  218. if (ret)
  219. goto bail_free;
  220. udata->outbuf =
  221. (void __user *) (unsigned long) offset_addr;
  222. ret = ib_copy_to_udata(udata, &offset,
  223. sizeof(offset));
  224. if (ret)
  225. goto bail_free;
  226. }
  227. spin_lock_irq(&srq->rq.lock);
  228. /*
  229. * validate head pointer value and compute
  230. * the number of remaining WQEs.
  231. */
  232. owq = srq->rq.wq;
  233. head = owq->head;
  234. if (head >= srq->rq.size)
  235. head = 0;
  236. tail = owq->tail;
  237. if (tail >= srq->rq.size)
  238. tail = 0;
  239. n = head;
  240. if (n < tail)
  241. n += srq->rq.size - tail;
  242. else
  243. n -= tail;
  244. if (size <= n) {
  245. ret = -EINVAL;
  246. goto bail_unlock;
  247. }
  248. n = 0;
  249. p = wq->wq;
  250. while (tail != head) {
  251. struct ipath_rwqe *wqe;
  252. int i;
  253. wqe = get_rwqe_ptr(&srq->rq, tail);
  254. p->wr_id = wqe->wr_id;
  255. p->num_sge = wqe->num_sge;
  256. for (i = 0; i < wqe->num_sge; i++)
  257. p->sg_list[i] = wqe->sg_list[i];
  258. n++;
  259. p = (struct ipath_rwqe *)((char *) p + sz);
  260. if (++tail >= srq->rq.size)
  261. tail = 0;
  262. }
  263. srq->rq.wq = wq;
  264. srq->rq.size = size;
  265. wq->head = n;
  266. wq->tail = 0;
  267. if (attr_mask & IB_SRQ_LIMIT)
  268. srq->limit = attr->srq_limit;
  269. spin_unlock_irq(&srq->rq.lock);
  270. vfree(owq);
  271. if (srq->ip) {
  272. struct ipath_mmap_info *ip = srq->ip;
  273. struct ipath_ibdev *dev = to_idev(srq->ibsrq.device);
  274. u32 s = sizeof(struct ipath_rwq) + size * sz;
  275. ipath_update_mmap_info(dev, ip, s, wq);
  276. /*
  277. * Return the offset to mmap.
  278. * See ipath_mmap() for details.
  279. */
  280. if (udata && udata->inlen >= sizeof(__u64)) {
  281. ret = ib_copy_to_udata(udata, &ip->offset,
  282. sizeof(ip->offset));
  283. if (ret)
  284. goto bail;
  285. }
  286. spin_lock_irq(&dev->pending_lock);
  287. if (list_empty(&ip->pending_mmaps))
  288. list_add(&ip->pending_mmaps,
  289. &dev->pending_mmaps);
  290. spin_unlock_irq(&dev->pending_lock);
  291. }
  292. } else if (attr_mask & IB_SRQ_LIMIT) {
  293. spin_lock_irq(&srq->rq.lock);
  294. if (attr->srq_limit >= srq->rq.size)
  295. ret = -EINVAL;
  296. else
  297. srq->limit = attr->srq_limit;
  298. spin_unlock_irq(&srq->rq.lock);
  299. }
  300. goto bail;
  301. bail_unlock:
  302. spin_unlock_irq(&srq->rq.lock);
  303. bail_free:
  304. vfree(wq);
  305. bail:
  306. return ret;
  307. }
  308. int ipath_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
  309. {
  310. struct ipath_srq *srq = to_isrq(ibsrq);
  311. attr->max_wr = srq->rq.size - 1;
  312. attr->max_sge = srq->rq.max_sge;
  313. attr->srq_limit = srq->limit;
  314. return 0;
  315. }
  316. /**
  317. * ipath_destroy_srq - destroy a shared receive queue
  318. * @ibsrq: the SRQ to destroy
  319. */
  320. int ipath_destroy_srq(struct ib_srq *ibsrq)
  321. {
  322. struct ipath_srq *srq = to_isrq(ibsrq);
  323. struct ipath_ibdev *dev = to_idev(ibsrq->device);
  324. spin_lock(&dev->n_srqs_lock);
  325. dev->n_srqs_allocated--;
  326. spin_unlock(&dev->n_srqs_lock);
  327. if (srq->ip)
  328. kref_put(&srq->ip->ref, ipath_release_mmap_info);
  329. else
  330. vfree(srq->rq.wq);
  331. kfree(srq);
  332. return 0;
  333. }