srq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. /*
  2. * Copyright (c) 2013-2015, Mellanox Technologies. 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/module.h>
  33. #include <linux/mlx5/qp.h>
  34. #include <linux/mlx5/srq.h>
  35. #include <linux/slab.h>
  36. #include <rdma/ib_umem.h>
  37. #include <rdma/ib_user_verbs.h>
  38. #include "mlx5_ib.h"
  39. #include "user.h"
  40. /* not supported currently */
  41. static int srq_signature;
  42. static void *get_wqe(struct mlx5_ib_srq *srq, int n)
  43. {
  44. return mlx5_buf_offset(&srq->buf, n << srq->msrq.wqe_shift);
  45. }
  46. static void mlx5_ib_srq_event(struct mlx5_core_srq *srq, enum mlx5_event type)
  47. {
  48. struct ib_event event;
  49. struct ib_srq *ibsrq = &to_mibsrq(srq)->ibsrq;
  50. if (ibsrq->event_handler) {
  51. event.device = ibsrq->device;
  52. event.element.srq = ibsrq;
  53. switch (type) {
  54. case MLX5_EVENT_TYPE_SRQ_RQ_LIMIT:
  55. event.event = IB_EVENT_SRQ_LIMIT_REACHED;
  56. break;
  57. case MLX5_EVENT_TYPE_SRQ_CATAS_ERROR:
  58. event.event = IB_EVENT_SRQ_ERR;
  59. break;
  60. default:
  61. pr_warn("mlx5_ib: Unexpected event type %d on SRQ %06x\n",
  62. type, srq->srqn);
  63. return;
  64. }
  65. ibsrq->event_handler(&event, ibsrq->srq_context);
  66. }
  67. }
  68. static int create_srq_user(struct ib_pd *pd, struct mlx5_ib_srq *srq,
  69. struct mlx5_create_srq_mbox_in **in,
  70. struct ib_udata *udata, int buf_size, int *inlen,
  71. int is_xrc)
  72. {
  73. struct mlx5_ib_dev *dev = to_mdev(pd->device);
  74. struct mlx5_ib_create_srq ucmd = {};
  75. size_t ucmdlen;
  76. void *xsrqc;
  77. int err;
  78. int npages;
  79. int page_shift;
  80. int ncont;
  81. u32 offset;
  82. u32 uidx = MLX5_IB_DEFAULT_UIDX;
  83. ucmdlen = min(udata->inlen, sizeof(ucmd));
  84. if (ib_copy_from_udata(&ucmd, udata, ucmdlen)) {
  85. mlx5_ib_dbg(dev, "failed copy udata\n");
  86. return -EFAULT;
  87. }
  88. if (ucmd.reserved0 || ucmd.reserved1)
  89. return -EINVAL;
  90. if (udata->inlen > sizeof(ucmd) &&
  91. !ib_is_udata_cleared(udata, sizeof(ucmd),
  92. udata->inlen - sizeof(ucmd)))
  93. return -EINVAL;
  94. if (is_xrc) {
  95. err = get_srq_user_index(to_mucontext(pd->uobject->context),
  96. &ucmd, udata->inlen, &uidx);
  97. if (err)
  98. return err;
  99. }
  100. srq->wq_sig = !!(ucmd.flags & MLX5_SRQ_FLAG_SIGNATURE);
  101. srq->umem = ib_umem_get(pd->uobject->context, ucmd.buf_addr, buf_size,
  102. 0, 0);
  103. if (IS_ERR(srq->umem)) {
  104. mlx5_ib_dbg(dev, "failed umem get, size %d\n", buf_size);
  105. err = PTR_ERR(srq->umem);
  106. return err;
  107. }
  108. mlx5_ib_cont_pages(srq->umem, ucmd.buf_addr, &npages,
  109. &page_shift, &ncont, NULL);
  110. err = mlx5_ib_get_buf_offset(ucmd.buf_addr, page_shift,
  111. &offset);
  112. if (err) {
  113. mlx5_ib_warn(dev, "bad offset\n");
  114. goto err_umem;
  115. }
  116. *inlen = sizeof(**in) + sizeof(*(*in)->pas) * ncont;
  117. *in = mlx5_vzalloc(*inlen);
  118. if (!(*in)) {
  119. err = -ENOMEM;
  120. goto err_umem;
  121. }
  122. mlx5_ib_populate_pas(dev, srq->umem, page_shift, (*in)->pas, 0);
  123. err = mlx5_ib_db_map_user(to_mucontext(pd->uobject->context),
  124. ucmd.db_addr, &srq->db);
  125. if (err) {
  126. mlx5_ib_dbg(dev, "map doorbell failed\n");
  127. goto err_in;
  128. }
  129. (*in)->ctx.log_pg_sz = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
  130. (*in)->ctx.pgoff_cqn = cpu_to_be32(offset << 26);
  131. if ((MLX5_CAP_GEN(dev->mdev, cqe_version) == MLX5_CQE_VERSION_V1) &&
  132. is_xrc){
  133. xsrqc = MLX5_ADDR_OF(create_xrc_srq_in, *in,
  134. xrc_srq_context_entry);
  135. MLX5_SET(xrc_srqc, xsrqc, user_index, uidx);
  136. }
  137. return 0;
  138. err_in:
  139. kvfree(*in);
  140. err_umem:
  141. ib_umem_release(srq->umem);
  142. return err;
  143. }
  144. static int create_srq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_srq *srq,
  145. struct mlx5_create_srq_mbox_in **in, int buf_size,
  146. int *inlen, int is_xrc)
  147. {
  148. int err;
  149. int i;
  150. struct mlx5_wqe_srq_next_seg *next;
  151. int page_shift;
  152. int npages;
  153. void *xsrqc;
  154. err = mlx5_db_alloc(dev->mdev, &srq->db);
  155. if (err) {
  156. mlx5_ib_warn(dev, "alloc dbell rec failed\n");
  157. return err;
  158. }
  159. if (mlx5_buf_alloc(dev->mdev, buf_size, &srq->buf)) {
  160. mlx5_ib_dbg(dev, "buf alloc failed\n");
  161. err = -ENOMEM;
  162. goto err_db;
  163. }
  164. page_shift = srq->buf.page_shift;
  165. srq->head = 0;
  166. srq->tail = srq->msrq.max - 1;
  167. srq->wqe_ctr = 0;
  168. for (i = 0; i < srq->msrq.max; i++) {
  169. next = get_wqe(srq, i);
  170. next->next_wqe_index =
  171. cpu_to_be16((i + 1) & (srq->msrq.max - 1));
  172. }
  173. npages = DIV_ROUND_UP(srq->buf.npages, 1 << (page_shift - PAGE_SHIFT));
  174. mlx5_ib_dbg(dev, "buf_size %d, page_shift %d, npages %d, calc npages %d\n",
  175. buf_size, page_shift, srq->buf.npages, npages);
  176. *inlen = sizeof(**in) + sizeof(*(*in)->pas) * npages;
  177. *in = mlx5_vzalloc(*inlen);
  178. if (!*in) {
  179. err = -ENOMEM;
  180. goto err_buf;
  181. }
  182. mlx5_fill_page_array(&srq->buf, (*in)->pas);
  183. srq->wrid = kmalloc(srq->msrq.max * sizeof(u64), GFP_KERNEL);
  184. if (!srq->wrid) {
  185. mlx5_ib_dbg(dev, "kmalloc failed %lu\n",
  186. (unsigned long)(srq->msrq.max * sizeof(u64)));
  187. err = -ENOMEM;
  188. goto err_in;
  189. }
  190. srq->wq_sig = !!srq_signature;
  191. (*in)->ctx.log_pg_sz = page_shift - MLX5_ADAPTER_PAGE_SHIFT;
  192. if ((MLX5_CAP_GEN(dev->mdev, cqe_version) == MLX5_CQE_VERSION_V1) &&
  193. is_xrc){
  194. xsrqc = MLX5_ADDR_OF(create_xrc_srq_in, *in,
  195. xrc_srq_context_entry);
  196. /* 0xffffff means we ask to work with cqe version 0 */
  197. MLX5_SET(xrc_srqc, xsrqc, user_index, MLX5_IB_DEFAULT_UIDX);
  198. }
  199. return 0;
  200. err_in:
  201. kvfree(*in);
  202. err_buf:
  203. mlx5_buf_free(dev->mdev, &srq->buf);
  204. err_db:
  205. mlx5_db_free(dev->mdev, &srq->db);
  206. return err;
  207. }
  208. static void destroy_srq_user(struct ib_pd *pd, struct mlx5_ib_srq *srq)
  209. {
  210. mlx5_ib_db_unmap_user(to_mucontext(pd->uobject->context), &srq->db);
  211. ib_umem_release(srq->umem);
  212. }
  213. static void destroy_srq_kernel(struct mlx5_ib_dev *dev, struct mlx5_ib_srq *srq)
  214. {
  215. kfree(srq->wrid);
  216. mlx5_buf_free(dev->mdev, &srq->buf);
  217. mlx5_db_free(dev->mdev, &srq->db);
  218. }
  219. struct ib_srq *mlx5_ib_create_srq(struct ib_pd *pd,
  220. struct ib_srq_init_attr *init_attr,
  221. struct ib_udata *udata)
  222. {
  223. struct mlx5_ib_dev *dev = to_mdev(pd->device);
  224. struct mlx5_ib_srq *srq;
  225. int desc_size;
  226. int buf_size;
  227. int err;
  228. struct mlx5_create_srq_mbox_in *uninitialized_var(in);
  229. int uninitialized_var(inlen);
  230. int is_xrc;
  231. u32 flgs, xrcdn;
  232. __u32 max_srq_wqes = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
  233. /* Sanity check SRQ size before proceeding */
  234. if (init_attr->attr.max_wr >= max_srq_wqes) {
  235. mlx5_ib_dbg(dev, "max_wr %d, cap %d\n",
  236. init_attr->attr.max_wr,
  237. max_srq_wqes);
  238. return ERR_PTR(-EINVAL);
  239. }
  240. srq = kmalloc(sizeof(*srq), GFP_KERNEL);
  241. if (!srq)
  242. return ERR_PTR(-ENOMEM);
  243. mutex_init(&srq->mutex);
  244. spin_lock_init(&srq->lock);
  245. srq->msrq.max = roundup_pow_of_two(init_attr->attr.max_wr + 1);
  246. srq->msrq.max_gs = init_attr->attr.max_sge;
  247. desc_size = sizeof(struct mlx5_wqe_srq_next_seg) +
  248. srq->msrq.max_gs * sizeof(struct mlx5_wqe_data_seg);
  249. desc_size = roundup_pow_of_two(desc_size);
  250. desc_size = max_t(int, 32, desc_size);
  251. srq->msrq.max_avail_gather = (desc_size - sizeof(struct mlx5_wqe_srq_next_seg)) /
  252. sizeof(struct mlx5_wqe_data_seg);
  253. srq->msrq.wqe_shift = ilog2(desc_size);
  254. buf_size = srq->msrq.max * desc_size;
  255. mlx5_ib_dbg(dev, "desc_size 0x%x, req wr 0x%x, srq size 0x%x, max_gs 0x%x, max_avail_gather 0x%x\n",
  256. desc_size, init_attr->attr.max_wr, srq->msrq.max, srq->msrq.max_gs,
  257. srq->msrq.max_avail_gather);
  258. is_xrc = (init_attr->srq_type == IB_SRQT_XRC);
  259. if (pd->uobject)
  260. err = create_srq_user(pd, srq, &in, udata, buf_size, &inlen,
  261. is_xrc);
  262. else
  263. err = create_srq_kernel(dev, srq, &in, buf_size, &inlen,
  264. is_xrc);
  265. if (err) {
  266. mlx5_ib_warn(dev, "create srq %s failed, err %d\n",
  267. pd->uobject ? "user" : "kernel", err);
  268. goto err_srq;
  269. }
  270. in->ctx.state_log_sz = ilog2(srq->msrq.max);
  271. flgs = ((srq->msrq.wqe_shift - 4) | (is_xrc << 5) | (srq->wq_sig << 7)) << 24;
  272. xrcdn = 0;
  273. if (is_xrc) {
  274. xrcdn = to_mxrcd(init_attr->ext.xrc.xrcd)->xrcdn;
  275. in->ctx.pgoff_cqn |= cpu_to_be32(to_mcq(init_attr->ext.xrc.cq)->mcq.cqn);
  276. } else if (init_attr->srq_type == IB_SRQT_BASIC) {
  277. xrcdn = to_mxrcd(dev->devr.x0)->xrcdn;
  278. in->ctx.pgoff_cqn |= cpu_to_be32(to_mcq(dev->devr.c0)->mcq.cqn);
  279. }
  280. in->ctx.flags_xrcd = cpu_to_be32((flgs & 0xFF000000) | (xrcdn & 0xFFFFFF));
  281. in->ctx.pd = cpu_to_be32(to_mpd(pd)->pdn);
  282. in->ctx.db_record = cpu_to_be64(srq->db.dma);
  283. err = mlx5_core_create_srq(dev->mdev, &srq->msrq, in, inlen, is_xrc);
  284. kvfree(in);
  285. if (err) {
  286. mlx5_ib_dbg(dev, "create SRQ failed, err %d\n", err);
  287. goto err_usr_kern_srq;
  288. }
  289. mlx5_ib_dbg(dev, "create SRQ with srqn 0x%x\n", srq->msrq.srqn);
  290. srq->msrq.event = mlx5_ib_srq_event;
  291. srq->ibsrq.ext.xrc.srq_num = srq->msrq.srqn;
  292. if (pd->uobject)
  293. if (ib_copy_to_udata(udata, &srq->msrq.srqn, sizeof(__u32))) {
  294. mlx5_ib_dbg(dev, "copy to user failed\n");
  295. err = -EFAULT;
  296. goto err_core;
  297. }
  298. init_attr->attr.max_wr = srq->msrq.max - 1;
  299. return &srq->ibsrq;
  300. err_core:
  301. mlx5_core_destroy_srq(dev->mdev, &srq->msrq);
  302. err_usr_kern_srq:
  303. if (pd->uobject)
  304. destroy_srq_user(pd, srq);
  305. else
  306. destroy_srq_kernel(dev, srq);
  307. err_srq:
  308. kfree(srq);
  309. return ERR_PTR(err);
  310. }
  311. int mlx5_ib_modify_srq(struct ib_srq *ibsrq, struct ib_srq_attr *attr,
  312. enum ib_srq_attr_mask attr_mask, struct ib_udata *udata)
  313. {
  314. struct mlx5_ib_dev *dev = to_mdev(ibsrq->device);
  315. struct mlx5_ib_srq *srq = to_msrq(ibsrq);
  316. int ret;
  317. /* We don't support resizing SRQs yet */
  318. if (attr_mask & IB_SRQ_MAX_WR)
  319. return -EINVAL;
  320. if (attr_mask & IB_SRQ_LIMIT) {
  321. if (attr->srq_limit >= srq->msrq.max)
  322. return -EINVAL;
  323. mutex_lock(&srq->mutex);
  324. ret = mlx5_core_arm_srq(dev->mdev, &srq->msrq, attr->srq_limit, 1);
  325. mutex_unlock(&srq->mutex);
  326. if (ret)
  327. return ret;
  328. }
  329. return 0;
  330. }
  331. int mlx5_ib_query_srq(struct ib_srq *ibsrq, struct ib_srq_attr *srq_attr)
  332. {
  333. struct mlx5_ib_dev *dev = to_mdev(ibsrq->device);
  334. struct mlx5_ib_srq *srq = to_msrq(ibsrq);
  335. int ret;
  336. struct mlx5_query_srq_mbox_out *out;
  337. out = kzalloc(sizeof(*out), GFP_KERNEL);
  338. if (!out)
  339. return -ENOMEM;
  340. ret = mlx5_core_query_srq(dev->mdev, &srq->msrq, out);
  341. if (ret)
  342. goto out_box;
  343. srq_attr->srq_limit = be16_to_cpu(out->ctx.lwm);
  344. srq_attr->max_wr = srq->msrq.max - 1;
  345. srq_attr->max_sge = srq->msrq.max_gs;
  346. out_box:
  347. kfree(out);
  348. return ret;
  349. }
  350. int mlx5_ib_destroy_srq(struct ib_srq *srq)
  351. {
  352. struct mlx5_ib_dev *dev = to_mdev(srq->device);
  353. struct mlx5_ib_srq *msrq = to_msrq(srq);
  354. mlx5_core_destroy_srq(dev->mdev, &msrq->msrq);
  355. if (srq->uobject) {
  356. mlx5_ib_db_unmap_user(to_mucontext(srq->uobject->context), &msrq->db);
  357. ib_umem_release(msrq->umem);
  358. } else {
  359. destroy_srq_kernel(dev, msrq);
  360. }
  361. kfree(srq);
  362. return 0;
  363. }
  364. void mlx5_ib_free_srq_wqe(struct mlx5_ib_srq *srq, int wqe_index)
  365. {
  366. struct mlx5_wqe_srq_next_seg *next;
  367. /* always called with interrupts disabled. */
  368. spin_lock(&srq->lock);
  369. next = get_wqe(srq, srq->tail);
  370. next->next_wqe_index = cpu_to_be16(wqe_index);
  371. srq->tail = wqe_index;
  372. spin_unlock(&srq->lock);
  373. }
  374. int mlx5_ib_post_srq_recv(struct ib_srq *ibsrq, struct ib_recv_wr *wr,
  375. struct ib_recv_wr **bad_wr)
  376. {
  377. struct mlx5_ib_srq *srq = to_msrq(ibsrq);
  378. struct mlx5_wqe_srq_next_seg *next;
  379. struct mlx5_wqe_data_seg *scat;
  380. unsigned long flags;
  381. int err = 0;
  382. int nreq;
  383. int i;
  384. spin_lock_irqsave(&srq->lock, flags);
  385. for (nreq = 0; wr; nreq++, wr = wr->next) {
  386. if (unlikely(wr->num_sge > srq->msrq.max_gs)) {
  387. err = -EINVAL;
  388. *bad_wr = wr;
  389. break;
  390. }
  391. if (unlikely(srq->head == srq->tail)) {
  392. err = -ENOMEM;
  393. *bad_wr = wr;
  394. break;
  395. }
  396. srq->wrid[srq->head] = wr->wr_id;
  397. next = get_wqe(srq, srq->head);
  398. srq->head = be16_to_cpu(next->next_wqe_index);
  399. scat = (struct mlx5_wqe_data_seg *)(next + 1);
  400. for (i = 0; i < wr->num_sge; i++) {
  401. scat[i].byte_count = cpu_to_be32(wr->sg_list[i].length);
  402. scat[i].lkey = cpu_to_be32(wr->sg_list[i].lkey);
  403. scat[i].addr = cpu_to_be64(wr->sg_list[i].addr);
  404. }
  405. if (i < srq->msrq.max_avail_gather) {
  406. scat[i].byte_count = 0;
  407. scat[i].lkey = cpu_to_be32(MLX5_INVALID_LKEY);
  408. scat[i].addr = 0;
  409. }
  410. }
  411. if (likely(nreq)) {
  412. srq->wqe_ctr += nreq;
  413. /* Make sure that descriptors are written before
  414. * doorbell record.
  415. */
  416. wmb();
  417. *srq->db.db = cpu_to_be32(srq->wqe_ctr);
  418. }
  419. spin_unlock_irqrestore(&srq->lock, flags);
  420. return err;
  421. }