srq.c 9.2 KB

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