ipath_srq.c 9.1 KB

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