ipath_srq.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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_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 = -ENOMEM;
  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. wq->head = next;
  77. spin_unlock_irqrestore(&srq->rq.lock, flags);
  78. }
  79. ret = 0;
  80. bail:
  81. return ret;
  82. }
  83. /**
  84. * ipath_create_srq - create a shared receive queue
  85. * @ibpd: the protection domain of the SRQ to create
  86. * @attr: the attributes of the SRQ
  87. * @udata: not used by the InfiniPath verbs driver
  88. */
  89. struct ib_srq *ipath_create_srq(struct ib_pd *ibpd,
  90. struct ib_srq_init_attr *srq_init_attr,
  91. struct ib_udata *udata)
  92. {
  93. struct ipath_ibdev *dev = to_idev(ibpd->device);
  94. struct ipath_srq *srq;
  95. u32 sz;
  96. struct ib_srq *ret;
  97. if (srq_init_attr->attr.max_wr == 0) {
  98. ret = ERR_PTR(-EINVAL);
  99. goto done;
  100. }
  101. if ((srq_init_attr->attr.max_sge > ib_ipath_max_srq_sges) ||
  102. (srq_init_attr->attr.max_wr > ib_ipath_max_srq_wrs)) {
  103. ret = ERR_PTR(-EINVAL);
  104. goto done;
  105. }
  106. srq = kmalloc(sizeof(*srq), GFP_KERNEL);
  107. if (!srq) {
  108. ret = ERR_PTR(-ENOMEM);
  109. goto done;
  110. }
  111. /*
  112. * Need to use vmalloc() if we want to support large #s of entries.
  113. */
  114. srq->rq.size = srq_init_attr->attr.max_wr + 1;
  115. srq->rq.max_sge = srq_init_attr->attr.max_sge;
  116. sz = sizeof(struct ib_sge) * srq->rq.max_sge +
  117. sizeof(struct ipath_rwqe);
  118. srq->rq.wq = vmalloc_user(sizeof(struct ipath_rwq) + srq->rq.size * sz);
  119. if (!srq->rq.wq) {
  120. ret = ERR_PTR(-ENOMEM);
  121. goto bail_srq;
  122. }
  123. /*
  124. * Return the address of the RWQ as the offset to mmap.
  125. * See ipath_mmap() for details.
  126. */
  127. if (udata && udata->outlen >= sizeof(__u64)) {
  128. struct ipath_mmap_info *ip;
  129. __u64 offset = (__u64) srq->rq.wq;
  130. int err;
  131. err = ib_copy_to_udata(udata, &offset, sizeof(offset));
  132. if (err) {
  133. ret = ERR_PTR(err);
  134. goto bail_wq;
  135. }
  136. /* Allocate info for ipath_mmap(). */
  137. ip = kmalloc(sizeof(*ip), GFP_KERNEL);
  138. if (!ip) {
  139. ret = ERR_PTR(-ENOMEM);
  140. goto bail_wq;
  141. }
  142. srq->ip = ip;
  143. ip->context = ibpd->uobject->context;
  144. ip->obj = srq->rq.wq;
  145. kref_init(&ip->ref);
  146. ip->mmap_cnt = 0;
  147. ip->size = PAGE_ALIGN(sizeof(struct ipath_rwq) +
  148. srq->rq.size * sz);
  149. spin_lock_irq(&dev->pending_lock);
  150. ip->next = dev->pending_mmaps;
  151. dev->pending_mmaps = ip;
  152. spin_unlock_irq(&dev->pending_lock);
  153. } else
  154. srq->ip = NULL;
  155. /*
  156. * ib_create_srq() will initialize srq->ibsrq.
  157. */
  158. spin_lock_init(&srq->rq.lock);
  159. srq->rq.wq->head = 0;
  160. srq->rq.wq->tail = 0;
  161. srq->limit = srq_init_attr->attr.srq_limit;
  162. spin_lock(&dev->n_srqs_lock);
  163. if (dev->n_srqs_allocated == ib_ipath_max_srqs) {
  164. spin_unlock(&dev->n_srqs_lock);
  165. ret = ERR_PTR(-ENOMEM);
  166. goto bail_wq;
  167. }
  168. dev->n_srqs_allocated++;
  169. spin_unlock(&dev->n_srqs_lock);
  170. ret = &srq->ibsrq;
  171. goto done;
  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. int ret = 0;
  192. if (attr_mask & IB_SRQ_MAX_WR) {
  193. struct ipath_rwq *owq;
  194. struct ipath_rwq *wq;
  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. /*
  213. * Return the address of the RWQ as the offset to mmap.
  214. * See ipath_mmap() for details.
  215. */
  216. if (udata && udata->inlen >= sizeof(__u64)) {
  217. __u64 offset_addr;
  218. __u64 offset = (__u64) wq;
  219. ret = ib_copy_from_udata(&offset_addr, udata,
  220. sizeof(offset_addr));
  221. if (ret) {
  222. vfree(wq);
  223. goto bail;
  224. }
  225. udata->outbuf = (void __user *) offset_addr;
  226. ret = ib_copy_to_udata(udata, &offset,
  227. sizeof(offset));
  228. if (ret) {
  229. vfree(wq);
  230. goto bail;
  231. }
  232. }
  233. spin_lock_irq(&srq->rq.lock);
  234. /*
  235. * validate head pointer value and compute
  236. * the number of remaining WQEs.
  237. */
  238. owq = srq->rq.wq;
  239. head = owq->head;
  240. if (head >= srq->rq.size)
  241. head = 0;
  242. tail = owq->tail;
  243. if (tail >= srq->rq.size)
  244. tail = 0;
  245. n = head;
  246. if (n < tail)
  247. n += srq->rq.size - tail;
  248. else
  249. n -= tail;
  250. if (size <= n) {
  251. spin_unlock_irq(&srq->rq.lock);
  252. vfree(wq);
  253. ret = -EINVAL;
  254. goto bail;
  255. }
  256. n = 0;
  257. p = wq->wq;
  258. while (tail != head) {
  259. struct ipath_rwqe *wqe;
  260. int i;
  261. wqe = get_rwqe_ptr(&srq->rq, tail);
  262. p->wr_id = wqe->wr_id;
  263. p->num_sge = wqe->num_sge;
  264. for (i = 0; i < wqe->num_sge; i++)
  265. p->sg_list[i] = wqe->sg_list[i];
  266. n++;
  267. p = (struct ipath_rwqe *)((char *) p + sz);
  268. if (++tail >= srq->rq.size)
  269. tail = 0;
  270. }
  271. srq->rq.wq = wq;
  272. srq->rq.size = size;
  273. wq->head = n;
  274. wq->tail = 0;
  275. if (attr_mask & IB_SRQ_LIMIT)
  276. srq->limit = attr->srq_limit;
  277. spin_unlock_irq(&srq->rq.lock);
  278. vfree(owq);
  279. if (srq->ip) {
  280. struct ipath_mmap_info *ip = srq->ip;
  281. struct ipath_ibdev *dev = to_idev(srq->ibsrq.device);
  282. ip->obj = wq;
  283. ip->size = PAGE_ALIGN(sizeof(struct ipath_rwq) +
  284. size * sz);
  285. spin_lock_irq(&dev->pending_lock);
  286. ip->next = dev->pending_mmaps;
  287. dev->pending_mmaps = ip;
  288. spin_unlock_irq(&dev->pending_lock);
  289. }
  290. } else if (attr_mask & IB_SRQ_LIMIT) {
  291. spin_lock_irq(&srq->rq.lock);
  292. if (attr->srq_limit >= srq->rq.size)
  293. ret = -EINVAL;
  294. else
  295. srq->limit = attr->srq_limit;
  296. spin_unlock_irq(&srq->rq.lock);
  297. }
  298. bail:
  299. return ret;
  300. }
  301. int ipath_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr)
  302. {
  303. struct ipath_srq *srq = to_isrq(ibsrq);
  304. attr->max_wr = srq->rq.size - 1;
  305. attr->max_sge = srq->rq.max_sge;
  306. attr->srq_limit = srq->limit;
  307. return 0;
  308. }
  309. /**
  310. * ipath_destroy_srq - destroy a shared receive queue
  311. * @ibsrq: the SRQ to destroy
  312. */
  313. int ipath_destroy_srq(struct ib_srq *ibsrq)
  314. {
  315. struct ipath_srq *srq = to_isrq(ibsrq);
  316. struct ipath_ibdev *dev = to_idev(ibsrq->device);
  317. spin_lock(&dev->n_srqs_lock);
  318. dev->n_srqs_allocated--;
  319. spin_unlock(&dev->n_srqs_lock);
  320. if (srq->ip)
  321. kref_put(&srq->ip->ref, ipath_release_mmap_info);
  322. else
  323. vfree(srq->rq.wq);
  324. kfree(srq);
  325. return 0;
  326. }