srq.c 9.4 KB

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