srq.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/mlx4/qp.h>
  33. #include <linux/mlx4/srq.h>
  34. #include "mlx4_ib.h"
  35. #include "user.h"
  36. static void *get_wqe(struct mlx4_ib_srq *srq, int n)
  37. {
  38. return mlx4_buf_offset(&srq->buf, n << srq->msrq.wqe_shift);
  39. }
  40. static void mlx4_ib_srq_event(struct mlx4_srq *srq, enum mlx4_event type)
  41. {
  42. struct ib_event event;
  43. struct ib_srq *ibsrq = &to_mibsrq(srq)->ibsrq;
  44. if (ibsrq->event_handler) {
  45. event.device = ibsrq->device;
  46. event.element.srq = ibsrq;
  47. switch (type) {
  48. case MLX4_EVENT_TYPE_SRQ_LIMIT:
  49. event.event = IB_EVENT_SRQ_LIMIT_REACHED;
  50. break;
  51. case MLX4_EVENT_TYPE_SRQ_CATAS_ERROR:
  52. event.event = IB_EVENT_SRQ_ERR;
  53. break;
  54. default:
  55. printk(KERN_WARNING "mlx4_ib: Unexpected event type %d "
  56. "on SRQ %06x\n", type, srq->srqn);
  57. return;
  58. }
  59. ibsrq->event_handler(&event, ibsrq->srq_context);
  60. }
  61. }
  62. struct ib_srq *mlx4_ib_create_srq(struct ib_pd *pd,
  63. struct ib_srq_init_attr *init_attr,
  64. struct ib_udata *udata)
  65. {
  66. struct mlx4_ib_dev *dev = to_mdev(pd->device);
  67. struct mlx4_ib_srq *srq;
  68. struct mlx4_wqe_srq_next_seg *next;
  69. int desc_size;
  70. int buf_size;
  71. int err;
  72. int i;
  73. /* Sanity check SRQ size before proceeding */
  74. if (init_attr->attr.max_wr >= dev->dev->caps.max_srq_wqes ||
  75. init_attr->attr.max_sge > dev->dev->caps.max_srq_sge)
  76. return ERR_PTR(-EINVAL);
  77. srq = kmalloc(sizeof *srq, GFP_KERNEL);
  78. if (!srq)
  79. return ERR_PTR(-ENOMEM);
  80. mutex_init(&srq->mutex);
  81. spin_lock_init(&srq->lock);
  82. srq->msrq.max = roundup_pow_of_two(init_attr->attr.max_wr + 1);
  83. srq->msrq.max_gs = init_attr->attr.max_sge;
  84. desc_size = max(32UL,
  85. roundup_pow_of_two(sizeof (struct mlx4_wqe_srq_next_seg) +
  86. srq->msrq.max_gs *
  87. sizeof (struct mlx4_wqe_data_seg)));
  88. srq->msrq.wqe_shift = ilog2(desc_size);
  89. buf_size = srq->msrq.max * desc_size;
  90. if (pd->uobject) {
  91. struct mlx4_ib_create_srq ucmd;
  92. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
  93. err = -EFAULT;
  94. goto err_srq;
  95. }
  96. srq->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr,
  97. buf_size, 0, 0);
  98. if (IS_ERR(srq->umem)) {
  99. err = PTR_ERR(srq->umem);
  100. goto err_srq;
  101. }
  102. err = mlx4_mtt_init(dev->dev, ib_umem_page_count(srq->umem),
  103. ilog2(srq->umem->page_size), &srq->mtt);
  104. if (err)
  105. goto err_buf;
  106. err = mlx4_ib_umem_write_mtt(dev, &srq->mtt, srq->umem);
  107. if (err)
  108. goto err_mtt;
  109. err = mlx4_ib_db_map_user(to_mucontext(pd->uobject->context),
  110. ucmd.db_addr, &srq->db);
  111. if (err)
  112. goto err_mtt;
  113. } else {
  114. err = mlx4_db_alloc(dev->dev, &srq->db, 0);
  115. if (err)
  116. goto err_srq;
  117. *srq->db.db = 0;
  118. if (mlx4_buf_alloc(dev->dev, buf_size, PAGE_SIZE * 2, &srq->buf)) {
  119. err = -ENOMEM;
  120. goto err_db;
  121. }
  122. srq->head = 0;
  123. srq->tail = srq->msrq.max - 1;
  124. srq->wqe_ctr = 0;
  125. for (i = 0; i < srq->msrq.max; ++i) {
  126. next = get_wqe(srq, i);
  127. next->next_wqe_index =
  128. cpu_to_be16((i + 1) & (srq->msrq.max - 1));
  129. }
  130. err = mlx4_mtt_init(dev->dev, srq->buf.npages, srq->buf.page_shift,
  131. &srq->mtt);
  132. if (err)
  133. goto err_buf;
  134. err = mlx4_buf_write_mtt(dev->dev, &srq->mtt, &srq->buf);
  135. if (err)
  136. goto err_mtt;
  137. srq->wrid = kmalloc(srq->msrq.max * sizeof (u64), GFP_KERNEL);
  138. if (!srq->wrid) {
  139. err = -ENOMEM;
  140. goto err_mtt;
  141. }
  142. }
  143. err = mlx4_srq_alloc(dev->dev, to_mpd(pd)->pdn, &srq->mtt,
  144. srq->db.dma, &srq->msrq);
  145. if (err)
  146. goto err_wrid;
  147. srq->msrq.event = mlx4_ib_srq_event;
  148. if (pd->uobject)
  149. if (ib_copy_to_udata(udata, &srq->msrq.srqn, sizeof (__u32))) {
  150. err = -EFAULT;
  151. goto err_wrid;
  152. }
  153. init_attr->attr.max_wr = srq->msrq.max - 1;
  154. return &srq->ibsrq;
  155. err_wrid:
  156. if (pd->uobject)
  157. mlx4_ib_db_unmap_user(to_mucontext(pd->uobject->context), &srq->db);
  158. else
  159. kfree(srq->wrid);
  160. err_mtt:
  161. mlx4_mtt_cleanup(dev->dev, &srq->mtt);
  162. err_buf:
  163. if (pd->uobject)
  164. ib_umem_release(srq->umem);
  165. else
  166. mlx4_buf_free(dev->dev, buf_size, &srq->buf);
  167. err_db:
  168. if (!pd->uobject)
  169. mlx4_db_free(dev->dev, &srq->db);
  170. err_srq:
  171. kfree(srq);
  172. return ERR_PTR(err);
  173. }
  174. int mlx4_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
  175. enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
  176. {
  177. struct mlx4_ib_dev *dev = to_mdev(ibsrq->device);
  178. struct mlx4_ib_srq *srq = to_msrq(ibsrq);
  179. int ret;
  180. /* We don't support resizing SRQs (yet?) */
  181. if (attr_mask & IB_SRQ_MAX_WR)
  182. return -EINVAL;
  183. if (attr_mask & IB_SRQ_LIMIT) {
  184. if (attr->srq_limit >= srq->msrq.max)
  185. return -EINVAL;
  186. mutex_lock(&srq->mutex);
  187. ret = mlx4_srq_arm(dev->dev, &srq->msrq, attr->srq_limit);
  188. mutex_unlock(&srq->mutex);
  189. if (ret)
  190. return ret;
  191. }
  192. return 0;
  193. }
  194. int mlx4_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
  195. {
  196. struct mlx4_ib_dev *dev = to_mdev(ibsrq->device);
  197. struct mlx4_ib_srq *srq = to_msrq(ibsrq);
  198. int ret;
  199. int limit_watermark;
  200. ret = mlx4_srq_query(dev->dev, &srq->msrq, &limit_watermark);
  201. if (ret)
  202. return ret;
  203. srq_attr->srq_limit = limit_watermark;
  204. srq_attr->max_wr = srq->msrq.max - 1;
  205. srq_attr->max_sge = srq->msrq.max_gs;
  206. return 0;
  207. }
  208. int mlx4_ib_destroy_srq(struct ib_srq *srq)
  209. {
  210. struct mlx4_ib_dev *dev = to_mdev(srq->device);
  211. struct mlx4_ib_srq *msrq = to_msrq(srq);
  212. mlx4_srq_free(dev->dev, &msrq->msrq);
  213. mlx4_mtt_cleanup(dev->dev, &msrq->mtt);
  214. if (srq->uobject) {
  215. mlx4_ib_db_unmap_user(to_mucontext(srq->uobject->context), &msrq->db);
  216. ib_umem_release(msrq->umem);
  217. } else {
  218. kfree(msrq->wrid);
  219. mlx4_buf_free(dev->dev, msrq->msrq.max << msrq->msrq.wqe_shift,
  220. &msrq->buf);
  221. mlx4_db_free(dev->dev, &msrq->db);
  222. }
  223. kfree(msrq);
  224. return 0;
  225. }
  226. void mlx4_ib_free_srq_wqe(struct mlx4_ib_srq *srq, int wqe_index)
  227. {
  228. struct mlx4_wqe_srq_next_seg *next;
  229. /* always called with interrupts disabled. */
  230. spin_lock(&srq->lock);
  231. next = get_wqe(srq, srq->tail);
  232. next->next_wqe_index = cpu_to_be16(wqe_index);
  233. srq->tail = wqe_index;
  234. spin_unlock(&srq->lock);
  235. }
  236. int mlx4_ib_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  237. struct ib_recv_wr **bad_wr)
  238. {
  239. struct mlx4_ib_srq *srq = to_msrq(ibsrq);
  240. struct mlx4_wqe_srq_next_seg *next;
  241. struct mlx4_wqe_data_seg *scat;
  242. unsigned long flags;
  243. int err = 0;
  244. int nreq;
  245. int i;
  246. spin_lock_irqsave(&srq->lock, flags);
  247. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  248. if (unlikely(wr->num_sge > srq->msrq.max_gs)) {
  249. err = -EINVAL;
  250. *bad_wr = wr;
  251. break;
  252. }
  253. if (unlikely(srq->head == srq->tail)) {
  254. err = -ENOMEM;
  255. *bad_wr = wr;
  256. break;
  257. }
  258. srq->wrid[srq->head] = wr->wr_id;
  259. next = get_wqe(srq, srq->head);
  260. srq->head = be16_to_cpu(next->next_wqe_index);
  261. scat = (struct mlx4_wqe_data_seg *) (next + 1);
  262. for (i = 0; i < wr->num_sge; ++i) {
  263. scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length);
  264. scat[i].lkey = cpu_to_be32(wr->sg_list[i].lkey);
  265. scat[i].addr = cpu_to_be64(wr->sg_list[i].addr);
  266. }
  267. if (i < srq->msrq.max_gs) {
  268. scat[i].byte_count = 0;
  269. scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY);
  270. scat[i].addr = 0;
  271. }
  272. }
  273. if (likely(nreq)) {
  274. srq->wqe_ctr += nreq;
  275. /*
  276. * Make sure that descriptors are written before
  277. * doorbell record.
  278. */
  279. wmb();
  280. *srq->db.db = cpu_to_be32(srq->wqe_ctr);
  281. }
  282. spin_unlock_irqrestore(&srq->lock, flags);
  283. return err;
  284. }