mem.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 <rdma/ib_umem.h>
  34. #include <rdma/ib_umem_odp.h>
  35. #include "mlx5_ib.h"
  36. #include <linux/jiffies.h>
  37. /* @umem: umem object to scan
  38. * @addr: ib virtual address requested by the user
  39. * @max_page_shift: high limit for page_shift - 0 means no limit
  40. * @count: number of PAGE_SIZE pages covered by umem
  41. * @shift: page shift for the compound pages found in the region
  42. * @ncont: number of compund pages
  43. * @order: log2 of the number of compound pages
  44. */
  45. void mlx5_ib_cont_pages(struct ib_umem *umem, u64 addr,
  46. unsigned long max_page_shift,
  47. int *count, int *shift,
  48. int *ncont, int *order)
  49. {
  50. unsigned long tmp;
  51. unsigned long m;
  52. u64 base = ~0, p = 0;
  53. u64 len, pfn;
  54. int i = 0;
  55. struct scatterlist *sg;
  56. int entry;
  57. addr = addr >> PAGE_SHIFT;
  58. tmp = (unsigned long)addr;
  59. m = find_first_bit(&tmp, BITS_PER_LONG);
  60. if (max_page_shift)
  61. m = min_t(unsigned long, max_page_shift - PAGE_SHIFT, m);
  62. for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
  63. len = sg_dma_len(sg) >> PAGE_SHIFT;
  64. pfn = sg_dma_address(sg) >> PAGE_SHIFT;
  65. if (base + p != pfn) {
  66. /* If either the offset or the new
  67. * base are unaligned update m
  68. */
  69. tmp = (unsigned long)(pfn | p);
  70. if (!IS_ALIGNED(tmp, 1 << m))
  71. m = find_first_bit(&tmp, BITS_PER_LONG);
  72. base = pfn;
  73. p = 0;
  74. }
  75. p += len;
  76. i += len;
  77. }
  78. if (i) {
  79. m = min_t(unsigned long, ilog2(roundup_pow_of_two(i)), m);
  80. if (order)
  81. *order = ilog2(roundup_pow_of_two(i) >> m);
  82. *ncont = DIV_ROUND_UP(i, (1 << m));
  83. } else {
  84. m = 0;
  85. if (order)
  86. *order = 0;
  87. *ncont = 0;
  88. }
  89. *shift = PAGE_SHIFT + m;
  90. *count = i;
  91. }
  92. /*
  93. * Populate the given array with bus addresses from the umem.
  94. *
  95. * dev - mlx5_ib device
  96. * umem - umem to use to fill the pages
  97. * page_shift - determines the page size used in the resulting array
  98. * offset - offset into the umem to start from,
  99. * only implemented for ODP umems
  100. * num_pages - total number of pages to fill
  101. * pas - bus addresses array to fill
  102. * access_flags - access flags to set on all present pages.
  103. use enum mlx5_ib_mtt_access_flags for this.
  104. */
  105. void __mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
  106. int page_shift, size_t offset, size_t num_pages,
  107. __be64 *pas, int access_flags)
  108. {
  109. int shift = page_shift - PAGE_SHIFT;
  110. int mask = (1 << shift) - 1;
  111. int i, k, idx;
  112. u64 cur = 0;
  113. u64 base;
  114. int len;
  115. struct scatterlist *sg;
  116. int entry;
  117. i = 0;
  118. for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
  119. len = sg_dma_len(sg) >> PAGE_SHIFT;
  120. base = sg_dma_address(sg);
  121. /* Skip elements below offset */
  122. if (i + len < offset << shift) {
  123. i += len;
  124. continue;
  125. }
  126. /* Skip pages below offset */
  127. if (i < offset << shift) {
  128. k = (offset << shift) - i;
  129. i = offset << shift;
  130. } else {
  131. k = 0;
  132. }
  133. for (; k < len; k++) {
  134. if (!(i & mask)) {
  135. cur = base + (k << PAGE_SHIFT);
  136. cur |= access_flags;
  137. idx = (i >> shift) - offset;
  138. pas[idx] = cpu_to_be64(cur);
  139. mlx5_ib_dbg(dev, "pas[%d] 0x%llx\n",
  140. i >> shift, be64_to_cpu(pas[idx]));
  141. }
  142. i++;
  143. /* Stop after num_pages reached */
  144. if (i >> shift >= offset + num_pages)
  145. return;
  146. }
  147. }
  148. }
  149. void mlx5_ib_populate_pas(struct mlx5_ib_dev *dev, struct ib_umem *umem,
  150. int page_shift, __be64 *pas, int access_flags)
  151. {
  152. return __mlx5_ib_populate_pas(dev, umem, page_shift, 0,
  153. ib_umem_num_dma_blocks(umem, PAGE_SIZE),
  154. pas, access_flags);
  155. }
  156. int mlx5_ib_get_buf_offset(u64 addr, int page_shift, u32 *offset)
  157. {
  158. u64 page_size;
  159. u64 page_mask;
  160. u64 off_size;
  161. u64 off_mask;
  162. u64 buf_off;
  163. page_size = (u64)1 << page_shift;
  164. page_mask = page_size - 1;
  165. buf_off = addr & page_mask;
  166. off_size = page_size >> 6;
  167. off_mask = off_size - 1;
  168. if (buf_off & off_mask)
  169. return -EINVAL;
  170. *offset = buf_off >> ilog2(off_size);
  171. return 0;
  172. }
  173. #define WR_ID_BF 0xBF
  174. #define WR_ID_END 0xBAD
  175. #define TEST_WC_NUM_WQES 255
  176. #define TEST_WC_POLLING_MAX_TIME_JIFFIES msecs_to_jiffies(100)
  177. static int post_send_nop(struct mlx5_ib_dev *dev, struct ib_qp *ibqp, u64 wr_id,
  178. bool signaled)
  179. {
  180. struct mlx5_ib_qp *qp = to_mqp(ibqp);
  181. struct mlx5_wqe_ctrl_seg *ctrl;
  182. struct mlx5_bf *bf = &qp->bf;
  183. __be32 mmio_wqe[16] = {};
  184. unsigned long flags;
  185. unsigned int idx;
  186. int i;
  187. if (unlikely(dev->mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR))
  188. return -EIO;
  189. spin_lock_irqsave(&qp->sq.lock, flags);
  190. idx = qp->sq.cur_post & (qp->sq.wqe_cnt - 1);
  191. ctrl = mlx5_frag_buf_get_wqe(&qp->sq.fbc, idx);
  192. memset(ctrl, 0, sizeof(struct mlx5_wqe_ctrl_seg));
  193. ctrl->fm_ce_se = signaled ? MLX5_WQE_CTRL_CQ_UPDATE : 0;
  194. ctrl->opmod_idx_opcode =
  195. cpu_to_be32(((u32)(qp->sq.cur_post) << 8) | MLX5_OPCODE_NOP);
  196. ctrl->qpn_ds = cpu_to_be32((sizeof(struct mlx5_wqe_ctrl_seg) / 16) |
  197. (qp->trans_qp.base.mqp.qpn << 8));
  198. qp->sq.wrid[idx] = wr_id;
  199. qp->sq.w_list[idx].opcode = MLX5_OPCODE_NOP;
  200. qp->sq.wqe_head[idx] = qp->sq.head + 1;
  201. qp->sq.cur_post += DIV_ROUND_UP(sizeof(struct mlx5_wqe_ctrl_seg),
  202. MLX5_SEND_WQE_BB);
  203. qp->sq.w_list[idx].next = qp->sq.cur_post;
  204. qp->sq.head++;
  205. memcpy(mmio_wqe, ctrl, sizeof(*ctrl));
  206. ((struct mlx5_wqe_ctrl_seg *)&mmio_wqe)->fm_ce_se |=
  207. MLX5_WQE_CTRL_CQ_UPDATE;
  208. /* Make sure that descriptors are written before
  209. * updating doorbell record and ringing the doorbell
  210. */
  211. wmb();
  212. qp->db.db[MLX5_SND_DBR] = cpu_to_be32(qp->sq.cur_post);
  213. /* Make sure doorbell record is visible to the HCA before
  214. * we hit doorbell
  215. */
  216. wmb();
  217. for (i = 0; i < 8; i++)
  218. mlx5_write64(&mmio_wqe[i * 2],
  219. bf->bfreg->map + bf->offset + i * 8);
  220. bf->offset ^= bf->buf_size;
  221. spin_unlock_irqrestore(&qp->sq.lock, flags);
  222. return 0;
  223. }
  224. static int test_wc_poll_cq_result(struct mlx5_ib_dev *dev, struct ib_cq *cq)
  225. {
  226. int ret;
  227. struct ib_wc wc = {};
  228. unsigned long end = jiffies + TEST_WC_POLLING_MAX_TIME_JIFFIES;
  229. do {
  230. ret = ib_poll_cq(cq, 1, &wc);
  231. if (ret < 0 || wc.status)
  232. return ret < 0 ? ret : -EINVAL;
  233. if (ret)
  234. break;
  235. } while (!time_after(jiffies, end));
  236. if (!ret)
  237. return -ETIMEDOUT;
  238. if (wc.wr_id != WR_ID_BF)
  239. ret = 0;
  240. return ret;
  241. }
  242. static int test_wc_do_send(struct mlx5_ib_dev *dev, struct ib_qp *qp)
  243. {
  244. int err, i;
  245. for (i = 0; i < TEST_WC_NUM_WQES; i++) {
  246. err = post_send_nop(dev, qp, WR_ID_BF, false);
  247. if (err)
  248. return err;
  249. }
  250. return post_send_nop(dev, qp, WR_ID_END, true);
  251. }
  252. int mlx5_ib_test_wc(struct mlx5_ib_dev *dev)
  253. {
  254. struct ib_cq_init_attr cq_attr = { .cqe = TEST_WC_NUM_WQES + 1 };
  255. int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
  256. struct ib_qp_init_attr qp_init_attr = {
  257. .cap = { .max_send_wr = TEST_WC_NUM_WQES },
  258. .qp_type = IB_QPT_UD,
  259. .sq_sig_type = IB_SIGNAL_REQ_WR,
  260. .create_flags = MLX5_IB_QP_CREATE_WC_TEST,
  261. };
  262. struct ib_qp_attr qp_attr = { .port_num = 1 };
  263. struct ib_device *ibdev = &dev->ib_dev;
  264. struct ib_qp *qp;
  265. struct ib_cq *cq;
  266. struct ib_pd *pd;
  267. int ret;
  268. if (!MLX5_CAP_GEN(dev->mdev, bf))
  269. return 0;
  270. if (!dev->mdev->roce.roce_en &&
  271. port_type_cap == MLX5_CAP_PORT_TYPE_ETH) {
  272. if (mlx5_core_is_pf(dev->mdev))
  273. dev->wc_support = arch_can_pci_mmap_wc();
  274. return 0;
  275. }
  276. ret = mlx5_alloc_bfreg(dev->mdev, &dev->wc_bfreg, true, false);
  277. if (ret)
  278. goto print_err;
  279. if (!dev->wc_bfreg.wc)
  280. goto out1;
  281. pd = ib_alloc_pd(ibdev, 0);
  282. if (IS_ERR(pd)) {
  283. ret = PTR_ERR(pd);
  284. goto out1;
  285. }
  286. cq = ib_create_cq(ibdev, NULL, NULL, NULL, &cq_attr);
  287. if (IS_ERR(cq)) {
  288. ret = PTR_ERR(cq);
  289. goto out2;
  290. }
  291. qp_init_attr.recv_cq = cq;
  292. qp_init_attr.send_cq = cq;
  293. qp = ib_create_qp(pd, &qp_init_attr);
  294. if (IS_ERR(qp)) {
  295. ret = PTR_ERR(qp);
  296. goto out3;
  297. }
  298. qp_attr.qp_state = IB_QPS_INIT;
  299. ret = ib_modify_qp(qp, &qp_attr,
  300. IB_QP_STATE | IB_QP_PORT | IB_QP_PKEY_INDEX |
  301. IB_QP_QKEY);
  302. if (ret)
  303. goto out4;
  304. qp_attr.qp_state = IB_QPS_RTR;
  305. ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE);
  306. if (ret)
  307. goto out4;
  308. qp_attr.qp_state = IB_QPS_RTS;
  309. ret = ib_modify_qp(qp, &qp_attr, IB_QP_STATE | IB_QP_SQ_PSN);
  310. if (ret)
  311. goto out4;
  312. ret = test_wc_do_send(dev, qp);
  313. if (ret < 0)
  314. goto out4;
  315. ret = test_wc_poll_cq_result(dev, cq);
  316. if (ret > 0) {
  317. dev->wc_support = true;
  318. ret = 0;
  319. }
  320. out4:
  321. ib_destroy_qp(qp);
  322. out3:
  323. ib_destroy_cq(cq);
  324. out2:
  325. ib_dealloc_pd(pd);
  326. out1:
  327. mlx5_free_bfreg(dev->mdev, &dev->wc_bfreg);
  328. print_err:
  329. if (ret)
  330. mlx5_ib_err(
  331. dev,
  332. "Error %d while trying to test write-combining support\n",
  333. ret);
  334. return ret;
  335. }