ipath_srq.c 9.2 KB

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