ipath_srq.c 8.7 KB

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