srq.c 13 KB

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