srq.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
  3. * Copyright (c) 2007, 2008 Mellanox Technologies. 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/mlx4/qp.h>
  34. #include <linux/mlx4/srq.h>
  35. #include "mlx4_ib.h"
  36. #include "user.h"
  37. static void *get_wqe(struct mlx4_ib_srq *srq, int n)
  38. {
  39. return mlx4_buf_offset(&srq->buf, n << srq->msrq.wqe_shift);
  40. }
  41. static void mlx4_ib_srq_event(struct mlx4_srq *srq, enum mlx4_event type)
  42. {
  43. struct ib_event event;
  44. struct ib_srq *ibsrq = &to_mibsrq(srq)->ibsrq;
  45. if (ibsrq->event_handler) {
  46. event.device = ibsrq->device;
  47. event.element.srq = ibsrq;
  48. switch (type) {
  49. case MLX4_EVENT_TYPE_SRQ_LIMIT:
  50. event.event = IB_EVENT_SRQ_LIMIT_REACHED;
  51. break;
  52. case MLX4_EVENT_TYPE_SRQ_CATAS_ERROR:
  53. event.event = IB_EVENT_SRQ_ERR;
  54. break;
  55. default:
  56. printk(KERN_WARNING "mlx4_ib: Unexpected event type %d "
  57. "on SRQ %06x\n", type, srq->srqn);
  58. return;
  59. }
  60. ibsrq->event_handler(&event, ibsrq->srq_context);
  61. }
  62. }
  63. struct ib_srq *mlx4_ib_create_srq(struct ib_pd *pd,
  64. struct ib_srq_init_attr *init_attr,
  65. struct ib_udata *udata)
  66. {
  67. struct mlx4_ib_dev *dev = to_mdev(pd->device);
  68. struct mlx4_ib_srq *srq;
  69. struct mlx4_wqe_srq_next_seg *next;
  70. int desc_size;
  71. int buf_size;
  72. int err;
  73. int i;
  74. /* Sanity check SRQ size before proceeding */
  75. if (init_attr->attr.max_wr >= dev->dev->caps.max_srq_wqes ||
  76. init_attr->attr.max_sge > dev->dev->caps.max_srq_sge)
  77. return ERR_PTR(-EINVAL);
  78. srq = kmalloc(sizeof *srq, GFP_KERNEL);
  79. if (!srq)
  80. return ERR_PTR(-ENOMEM);
  81. mutex_init(&srq->mutex);
  82. spin_lock_init(&srq->lock);
  83. srq->msrq.max = roundup_pow_of_two(init_attr->attr.max_wr + 1);
  84. srq->msrq.max_gs = init_attr->attr.max_sge;
  85. desc_size = max(32UL,
  86. roundup_pow_of_two(sizeof (struct mlx4_wqe_srq_next_seg) +
  87. srq->msrq.max_gs *
  88. sizeof (struct mlx4_wqe_data_seg)));
  89. srq->msrq.wqe_shift = ilog2(desc_size);
  90. buf_size = srq->msrq.max * desc_size;
  91. if (pd->uobject) {
  92. struct mlx4_ib_create_srq ucmd;
  93. if (ib_copy_from_udata(&ucmd, udata, sizeof ucmd)) {
  94. err = -EFAULT;
  95. goto err_srq;
  96. }
  97. srq->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr,
  98. buf_size, 0, 0);
  99. if (IS_ERR(srq->umem)) {
  100. err = PTR_ERR(srq->umem);
  101. goto err_srq;
  102. }
  103. err = mlx4_mtt_init(dev->dev, ib_umem_page_count(srq->umem),
  104. ilog2(srq->umem->page_size), &srq->mtt);
  105. if (err)
  106. goto err_buf;
  107. err = mlx4_ib_umem_write_mtt(dev, &srq->mtt, srq->umem);
  108. if (err)
  109. goto err_mtt;
  110. err = mlx4_ib_db_map_user(to_mucontext(pd->uobject->context),
  111. ucmd.db_addr, &srq->db);
  112. if (err)
  113. goto err_mtt;
  114. } else {
  115. err = mlx4_db_alloc(dev->dev, &srq->db, 0);
  116. if (err)
  117. goto err_srq;
  118. *srq->db.db = 0;
  119. if (mlx4_buf_alloc(dev->dev, buf_size, PAGE_SIZE * 2, &srq->buf)) {
  120. err = -ENOMEM;
  121. goto err_db;
  122. }
  123. srq->head = 0;
  124. srq->tail = srq->msrq.max - 1;
  125. srq->wqe_ctr = 0;
  126. for (i = 0; i < srq->msrq.max; ++i) {
  127. next = get_wqe(srq, i);
  128. next->next_wqe_index =
  129. cpu_to_be16((i + 1) & (srq->msrq.max - 1));
  130. }
  131. err = mlx4_mtt_init(dev->dev, srq->buf.npages, srq->buf.page_shift,
  132. &srq->mtt);
  133. if (err)
  134. goto err_buf;
  135. err = mlx4_buf_write_mtt(dev->dev, &srq->mtt, &srq->buf);
  136. if (err)
  137. goto err_mtt;
  138. srq->wrid = kmalloc(srq->msrq.max * sizeof (u64), GFP_KERNEL);
  139. if (!srq->wrid) {
  140. err = -ENOMEM;
  141. goto err_mtt;
  142. }
  143. }
  144. err = mlx4_srq_alloc(dev->dev, to_mpd(pd)->pdn, &srq->mtt,
  145. srq->db.dma, &srq->msrq);
  146. if (err)
  147. goto err_wrid;
  148. srq->msrq.event = mlx4_ib_srq_event;
  149. if (pd->uobject)
  150. if (ib_copy_to_udata(udata, &srq->msrq.srqn, sizeof (__u32))) {
  151. err = -EFAULT;
  152. goto err_wrid;
  153. }
  154. init_attr->attr.max_wr = srq->msrq.max - 1;
  155. return &srq->ibsrq;
  156. err_wrid:
  157. if (pd->uobject)
  158. mlx4_ib_db_unmap_user(to_mucontext(pd->uobject->context), &srq->db);
  159. else
  160. kfree(srq->wrid);
  161. err_mtt:
  162. mlx4_mtt_cleanup(dev->dev, &srq->mtt);
  163. err_buf:
  164. if (pd->uobject)
  165. ib_umem_release(srq->umem);
  166. else
  167. mlx4_buf_free(dev->dev, buf_size, &srq->buf);
  168. err_db:
  169. if (!pd->uobject)
  170. mlx4_db_free(dev->dev, &srq->db);
  171. err_srq:
  172. kfree(srq);
  173. return ERR_PTR(err);
  174. }
  175. int mlx4_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
  176. enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
  177. {
  178. struct mlx4_ib_dev *dev = to_mdev(ibsrq->device);
  179. struct mlx4_ib_srq *srq = to_msrq(ibsrq);
  180. int ret;
  181. /* We don't support resizing SRQs (yet?) */
  182. if (attr_mask & IB_SRQ_MAX_WR)
  183. return -EINVAL;
  184. if (attr_mask & IB_SRQ_LIMIT) {
  185. if (attr->srq_limit >= srq->msrq.max)
  186. return -EINVAL;
  187. mutex_lock(&srq->mutex);
  188. ret = mlx4_srq_arm(dev->dev, &srq->msrq, attr->srq_limit);
  189. mutex_unlock(&srq->mutex);
  190. if (ret)
  191. return ret;
  192. }
  193. return 0;
  194. }
  195. int mlx4_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
  196. {
  197. struct mlx4_ib_dev *dev = to_mdev(ibsrq->device);
  198. struct mlx4_ib_srq *srq = to_msrq(ibsrq);
  199. int ret;
  200. int limit_watermark;
  201. ret = mlx4_srq_query(dev->dev, &srq->msrq, &limit_watermark);
  202. if (ret)
  203. return ret;
  204. srq_attr->srq_limit = limit_watermark;
  205. srq_attr->max_wr = srq->msrq.max - 1;
  206. srq_attr->max_sge = srq->msrq.max_gs;
  207. return 0;
  208. }
  209. int mlx4_ib_destroy_srq(struct ib_srq *srq)
  210. {
  211. struct mlx4_ib_dev *dev = to_mdev(srq->device);
  212. struct mlx4_ib_srq *msrq = to_msrq(srq);
  213. mlx4_srq_free(dev->dev, &msrq->msrq);
  214. mlx4_mtt_cleanup(dev->dev, &msrq->mtt);
  215. if (srq->uobject) {
  216. mlx4_ib_db_unmap_user(to_mucontext(srq->uobject->context), &msrq->db);
  217. ib_umem_release(msrq->umem);
  218. } else {
  219. kfree(msrq->wrid);
  220. mlx4_buf_free(dev->dev, msrq->msrq.max << msrq->msrq.wqe_shift,
  221. &msrq->buf);
  222. mlx4_db_free(dev->dev, &msrq->db);
  223. }
  224. kfree(msrq);
  225. return 0;
  226. }
  227. void mlx4_ib_free_srq_wqe(struct mlx4_ib_srq *srq, int wqe_index)
  228. {
  229. struct mlx4_wqe_srq_next_seg *next;
  230. /* always called with interrupts disabled. */
  231. spin_lock(&srq->lock);
  232. next = get_wqe(srq, srq->tail);
  233. next->next_wqe_index = cpu_to_be16(wqe_index);
  234. srq->tail = wqe_index;
  235. spin_unlock(&srq->lock);
  236. }
  237. int mlx4_ib_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  238. struct ib_recv_wr **bad_wr)
  239. {
  240. struct mlx4_ib_srq *srq = to_msrq(ibsrq);
  241. struct mlx4_wqe_srq_next_seg *next;
  242. struct mlx4_wqe_data_seg *scat;
  243. unsigned long flags;
  244. int err = 0;
  245. int nreq;
  246. int i;
  247. spin_lock_irqsave(&srq->lock, flags);
  248. for (nreq = 0; wr; ++nreq, wr = wr->next) {
  249. if (unlikely(wr->num_sge > srq->msrq.max_gs)) {
  250. err = -EINVAL;
  251. *bad_wr = wr;
  252. break;
  253. }
  254. if (unlikely(srq->head == srq->tail)) {
  255. err = -ENOMEM;
  256. *bad_wr = wr;
  257. break;
  258. }
  259. srq->wrid[srq->head] = wr->wr_id;
  260. next = get_wqe(srq, srq->head);
  261. srq->head = be16_to_cpu(next->next_wqe_index);
  262. scat = (struct mlx4_wqe_data_seg *) (next + 1);
  263. for (i = 0; i < wr->num_sge; ++i) {
  264. scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length);
  265. scat[i].lkey = cpu_to_be32(wr->sg_list[i].lkey);
  266. scat[i].addr = cpu_to_be64(wr->sg_list[i].addr);
  267. }
  268. if (i < srq->msrq.max_gs) {
  269. scat[i].byte_count = 0;
  270. scat[i].lkey = cpu_to_be32(MLX4_INVALID_LKEY);
  271. scat[i].addr = 0;
  272. }
  273. }
  274. if (likely(nreq)) {
  275. srq->wqe_ctr += nreq;
  276. /*
  277. * Make sure that descriptors are written before
  278. * doorbell record.
  279. */
  280. wmb();
  281. *srq->db.db = cpu_to_be32(srq->wqe_ctr);
  282. }
  283. spin_unlock_irqrestore(&srq->lock, flags);
  284. return err;
  285. }