rw.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016 HGST, a Western Digital Company.
  4. */
  5. #include <linux/memremap.h>
  6. #include <linux/moduleparam.h>
  7. #include <linux/slab.h>
  8. #include <linux/pci-p2pdma.h>
  9. #include <rdma/mr_pool.h>
  10. #include <rdma/rw.h>
  11. enum {
  12. RDMA_RW_SINGLE_WR,
  13. RDMA_RW_MULTI_WR,
  14. RDMA_RW_MR,
  15. RDMA_RW_SIG_MR,
  16. };
  17. static bool rdma_rw_force_mr;
  18. module_param_named(force_mr, rdma_rw_force_mr, bool, 0);
  19. MODULE_PARM_DESC(force_mr, "Force usage of MRs for RDMA READ/WRITE operations");
  20. /*
  21. * Report whether memory registration should be used. Memory registration must
  22. * be used for iWarp devices because of iWARP-specific limitations. Memory
  23. * registration is also enabled if registering memory might yield better
  24. * performance than using multiple SGE entries, see rdma_rw_io_needs_mr()
  25. */
  26. static inline bool rdma_rw_can_use_mr(struct ib_device *dev, u32 port_num)
  27. {
  28. if (rdma_protocol_iwarp(dev, port_num))
  29. return true;
  30. if (dev->attrs.max_sgl_rd)
  31. return true;
  32. if (unlikely(rdma_rw_force_mr))
  33. return true;
  34. return false;
  35. }
  36. /*
  37. * Check if the device will use memory registration for this RW operation.
  38. * For RDMA READs we must use MRs on iWarp and can optionally use them as an
  39. * optimization otherwise. Additionally we have a debug option to force usage
  40. * of MRs to help testing this code path.
  41. */
  42. static inline bool rdma_rw_io_needs_mr(struct ib_device *dev, u32 port_num,
  43. enum dma_data_direction dir, int dma_nents)
  44. {
  45. if (dir == DMA_FROM_DEVICE) {
  46. if (rdma_protocol_iwarp(dev, port_num))
  47. return true;
  48. if (dev->attrs.max_sgl_rd && dma_nents > dev->attrs.max_sgl_rd)
  49. return true;
  50. }
  51. if (unlikely(rdma_rw_force_mr))
  52. return true;
  53. return false;
  54. }
  55. static inline u32 rdma_rw_fr_page_list_len(struct ib_device *dev,
  56. bool pi_support)
  57. {
  58. u32 max_pages;
  59. if (pi_support)
  60. max_pages = dev->attrs.max_pi_fast_reg_page_list_len;
  61. else
  62. max_pages = dev->attrs.max_fast_reg_page_list_len;
  63. /* arbitrary limit to avoid allocating gigantic resources */
  64. return min_t(u32, max_pages, 256);
  65. }
  66. static inline int rdma_rw_inv_key(struct rdma_rw_reg_ctx *reg)
  67. {
  68. int count = 0;
  69. if (reg->mr->need_inval) {
  70. reg->inv_wr.opcode = IB_WR_LOCAL_INV;
  71. reg->inv_wr.ex.invalidate_rkey = reg->mr->lkey;
  72. reg->inv_wr.next = &reg->reg_wr.wr;
  73. count++;
  74. } else {
  75. reg->inv_wr.next = NULL;
  76. }
  77. return count;
  78. }
  79. /* Caller must have zero-initialized *reg. */
  80. static int rdma_rw_init_one_mr(struct ib_qp *qp, u32 port_num,
  81. struct rdma_rw_reg_ctx *reg, struct scatterlist *sg,
  82. u32 sg_cnt, u32 offset)
  83. {
  84. u32 pages_per_mr = rdma_rw_fr_page_list_len(qp->pd->device,
  85. qp->integrity_en);
  86. u32 nents = min(sg_cnt, pages_per_mr);
  87. int count = 0, ret;
  88. reg->mr = ib_mr_pool_get(qp, &qp->rdma_mrs);
  89. if (!reg->mr)
  90. return -EAGAIN;
  91. count += rdma_rw_inv_key(reg);
  92. ret = ib_map_mr_sg(reg->mr, sg, nents, &offset, PAGE_SIZE);
  93. if (ret < 0 || ret < nents) {
  94. ib_mr_pool_put(qp, &qp->rdma_mrs, reg->mr);
  95. return -EINVAL;
  96. }
  97. reg->reg_wr.wr.opcode = IB_WR_REG_MR;
  98. reg->reg_wr.mr = reg->mr;
  99. reg->reg_wr.access = IB_ACCESS_LOCAL_WRITE;
  100. if (rdma_protocol_iwarp(qp->device, port_num))
  101. reg->reg_wr.access |= IB_ACCESS_REMOTE_WRITE;
  102. count++;
  103. reg->sge.addr = reg->mr->iova;
  104. reg->sge.length = reg->mr->length;
  105. return count;
  106. }
  107. static int rdma_rw_init_mr_wrs(struct rdma_rw_ctx *ctx, struct ib_qp *qp,
  108. u32 port_num, struct scatterlist *sg, u32 sg_cnt, u32 offset,
  109. u64 remote_addr, u32 rkey, enum dma_data_direction dir)
  110. {
  111. struct rdma_rw_reg_ctx *prev = NULL;
  112. u32 pages_per_mr = rdma_rw_fr_page_list_len(qp->pd->device,
  113. qp->integrity_en);
  114. int i, j, ret = 0, count = 0;
  115. ctx->nr_ops = DIV_ROUND_UP(sg_cnt, pages_per_mr);
  116. ctx->reg = kcalloc(ctx->nr_ops, sizeof(*ctx->reg), GFP_KERNEL);
  117. if (!ctx->reg) {
  118. ret = -ENOMEM;
  119. goto out;
  120. }
  121. for (i = 0; i < ctx->nr_ops; i++) {
  122. struct rdma_rw_reg_ctx *reg = &ctx->reg[i];
  123. u32 nents = min(sg_cnt, pages_per_mr);
  124. ret = rdma_rw_init_one_mr(qp, port_num, reg, sg, sg_cnt,
  125. offset);
  126. if (ret < 0)
  127. goto out_free;
  128. count += ret;
  129. if (prev) {
  130. if (reg->mr->need_inval)
  131. prev->wr.wr.next = &reg->inv_wr;
  132. else
  133. prev->wr.wr.next = &reg->reg_wr.wr;
  134. }
  135. reg->reg_wr.wr.next = &reg->wr.wr;
  136. reg->wr.wr.sg_list = &reg->sge;
  137. reg->wr.wr.num_sge = 1;
  138. reg->wr.remote_addr = remote_addr;
  139. reg->wr.rkey = rkey;
  140. if (dir == DMA_TO_DEVICE) {
  141. reg->wr.wr.opcode = IB_WR_RDMA_WRITE;
  142. } else if (!rdma_cap_read_inv(qp->device, port_num)) {
  143. reg->wr.wr.opcode = IB_WR_RDMA_READ;
  144. } else {
  145. reg->wr.wr.opcode = IB_WR_RDMA_READ_WITH_INV;
  146. reg->wr.wr.ex.invalidate_rkey = reg->mr->lkey;
  147. }
  148. count++;
  149. remote_addr += reg->sge.length;
  150. sg_cnt -= nents;
  151. for (j = 0; j < nents; j++)
  152. sg = sg_next(sg);
  153. prev = reg;
  154. offset = 0;
  155. }
  156. if (prev)
  157. prev->wr.wr.next = NULL;
  158. ctx->type = RDMA_RW_MR;
  159. return count;
  160. out_free:
  161. while (--i >= 0)
  162. ib_mr_pool_put(qp, &qp->rdma_mrs, ctx->reg[i].mr);
  163. kfree(ctx->reg);
  164. out:
  165. return ret;
  166. }
  167. static int rdma_rw_init_map_wrs(struct rdma_rw_ctx *ctx, struct ib_qp *qp,
  168. struct scatterlist *sg, u32 sg_cnt, u32 offset,
  169. u64 remote_addr, u32 rkey, enum dma_data_direction dir)
  170. {
  171. u32 max_sge = dir == DMA_TO_DEVICE ? qp->max_write_sge :
  172. qp->max_read_sge;
  173. struct ib_sge *sge;
  174. u32 total_len = 0, i, j;
  175. ctx->nr_ops = DIV_ROUND_UP(sg_cnt, max_sge);
  176. ctx->map.sges = sge = kcalloc(sg_cnt, sizeof(*sge), GFP_KERNEL);
  177. if (!ctx->map.sges)
  178. goto out;
  179. ctx->map.wrs = kcalloc(ctx->nr_ops, sizeof(*ctx->map.wrs), GFP_KERNEL);
  180. if (!ctx->map.wrs)
  181. goto out_free_sges;
  182. for (i = 0; i < ctx->nr_ops; i++) {
  183. struct ib_rdma_wr *rdma_wr = &ctx->map.wrs[i];
  184. u32 nr_sge = min(sg_cnt, max_sge);
  185. if (dir == DMA_TO_DEVICE)
  186. rdma_wr->wr.opcode = IB_WR_RDMA_WRITE;
  187. else
  188. rdma_wr->wr.opcode = IB_WR_RDMA_READ;
  189. rdma_wr->remote_addr = remote_addr + total_len;
  190. rdma_wr->rkey = rkey;
  191. rdma_wr->wr.num_sge = nr_sge;
  192. rdma_wr->wr.sg_list = sge;
  193. for (j = 0; j < nr_sge; j++, sg = sg_next(sg)) {
  194. sge->addr = sg_dma_address(sg) + offset;
  195. sge->length = sg_dma_len(sg) - offset;
  196. sge->lkey = qp->pd->local_dma_lkey;
  197. total_len += sge->length;
  198. sge++;
  199. sg_cnt--;
  200. offset = 0;
  201. }
  202. rdma_wr->wr.next = i + 1 < ctx->nr_ops ?
  203. &ctx->map.wrs[i + 1].wr : NULL;
  204. }
  205. ctx->type = RDMA_RW_MULTI_WR;
  206. return ctx->nr_ops;
  207. out_free_sges:
  208. kfree(ctx->map.sges);
  209. out:
  210. return -ENOMEM;
  211. }
  212. static int rdma_rw_init_single_wr(struct rdma_rw_ctx *ctx, struct ib_qp *qp,
  213. struct scatterlist *sg, u32 offset, u64 remote_addr, u32 rkey,
  214. enum dma_data_direction dir)
  215. {
  216. struct ib_rdma_wr *rdma_wr = &ctx->single.wr;
  217. ctx->nr_ops = 1;
  218. ctx->single.sge.lkey = qp->pd->local_dma_lkey;
  219. ctx->single.sge.addr = sg_dma_address(sg) + offset;
  220. ctx->single.sge.length = sg_dma_len(sg) - offset;
  221. memset(rdma_wr, 0, sizeof(*rdma_wr));
  222. if (dir == DMA_TO_DEVICE)
  223. rdma_wr->wr.opcode = IB_WR_RDMA_WRITE;
  224. else
  225. rdma_wr->wr.opcode = IB_WR_RDMA_READ;
  226. rdma_wr->wr.sg_list = &ctx->single.sge;
  227. rdma_wr->wr.num_sge = 1;
  228. rdma_wr->remote_addr = remote_addr;
  229. rdma_wr->rkey = rkey;
  230. ctx->type = RDMA_RW_SINGLE_WR;
  231. return 1;
  232. }
  233. /**
  234. * rdma_rw_ctx_init - initialize a RDMA READ/WRITE context
  235. * @ctx: context to initialize
  236. * @qp: queue pair to operate on
  237. * @port_num: port num to which the connection is bound
  238. * @sg: scatterlist to READ/WRITE from/to
  239. * @sg_cnt: number of entries in @sg
  240. * @sg_offset: current byte offset into @sg
  241. * @remote_addr:remote address to read/write (relative to @rkey)
  242. * @rkey: remote key to operate on
  243. * @dir: %DMA_TO_DEVICE for RDMA WRITE, %DMA_FROM_DEVICE for RDMA READ
  244. *
  245. * Returns the number of WQEs that will be needed on the workqueue if
  246. * successful, or a negative error code.
  247. */
  248. int rdma_rw_ctx_init(struct rdma_rw_ctx *ctx, struct ib_qp *qp, u32 port_num,
  249. struct scatterlist *sg, u32 sg_cnt, u32 sg_offset,
  250. u64 remote_addr, u32 rkey, enum dma_data_direction dir)
  251. {
  252. struct ib_device *dev = qp->pd->device;
  253. struct sg_table sgt = {
  254. .sgl = sg,
  255. .orig_nents = sg_cnt,
  256. };
  257. int ret;
  258. ret = ib_dma_map_sgtable_attrs(dev, &sgt, dir, 0);
  259. if (ret)
  260. return ret;
  261. sg_cnt = sgt.nents;
  262. /*
  263. * Skip to the S/G entry that sg_offset falls into:
  264. */
  265. for (;;) {
  266. u32 len = sg_dma_len(sg);
  267. if (sg_offset < len)
  268. break;
  269. sg = sg_next(sg);
  270. sg_offset -= len;
  271. sg_cnt--;
  272. }
  273. ret = -EIO;
  274. if (WARN_ON_ONCE(sg_cnt == 0))
  275. goto out_unmap_sg;
  276. if (rdma_rw_io_needs_mr(qp->device, port_num, dir, sg_cnt)) {
  277. ret = rdma_rw_init_mr_wrs(ctx, qp, port_num, sg, sg_cnt,
  278. sg_offset, remote_addr, rkey, dir);
  279. } else if (sg_cnt > 1) {
  280. ret = rdma_rw_init_map_wrs(ctx, qp, sg, sg_cnt, sg_offset,
  281. remote_addr, rkey, dir);
  282. } else {
  283. ret = rdma_rw_init_single_wr(ctx, qp, sg, sg_offset,
  284. remote_addr, rkey, dir);
  285. }
  286. if (ret < 0)
  287. goto out_unmap_sg;
  288. return ret;
  289. out_unmap_sg:
  290. ib_dma_unmap_sgtable_attrs(dev, &sgt, dir, 0);
  291. return ret;
  292. }
  293. EXPORT_SYMBOL(rdma_rw_ctx_init);
  294. /**
  295. * rdma_rw_ctx_signature_init - initialize a RW context with signature offload
  296. * @ctx: context to initialize
  297. * @qp: queue pair to operate on
  298. * @port_num: port num to which the connection is bound
  299. * @sg: scatterlist to READ/WRITE from/to
  300. * @sg_cnt: number of entries in @sg
  301. * @prot_sg: scatterlist to READ/WRITE protection information from/to
  302. * @prot_sg_cnt: number of entries in @prot_sg
  303. * @sig_attrs: signature offloading algorithms
  304. * @remote_addr:remote address to read/write (relative to @rkey)
  305. * @rkey: remote key to operate on
  306. * @dir: %DMA_TO_DEVICE for RDMA WRITE, %DMA_FROM_DEVICE for RDMA READ
  307. *
  308. * Returns the number of WQEs that will be needed on the workqueue if
  309. * successful, or a negative error code.
  310. */
  311. int rdma_rw_ctx_signature_init(struct rdma_rw_ctx *ctx, struct ib_qp *qp,
  312. u32 port_num, struct scatterlist *sg, u32 sg_cnt,
  313. struct scatterlist *prot_sg, u32 prot_sg_cnt,
  314. struct ib_sig_attrs *sig_attrs,
  315. u64 remote_addr, u32 rkey, enum dma_data_direction dir)
  316. {
  317. struct ib_device *dev = qp->pd->device;
  318. u32 pages_per_mr = rdma_rw_fr_page_list_len(qp->pd->device,
  319. qp->integrity_en);
  320. struct sg_table sgt = {
  321. .sgl = sg,
  322. .orig_nents = sg_cnt,
  323. };
  324. struct sg_table prot_sgt = {
  325. .sgl = prot_sg,
  326. .orig_nents = prot_sg_cnt,
  327. };
  328. struct ib_rdma_wr *rdma_wr;
  329. int count = 0, ret;
  330. if (sg_cnt > pages_per_mr || prot_sg_cnt > pages_per_mr) {
  331. pr_err("SG count too large: sg_cnt=%u, prot_sg_cnt=%u, pages_per_mr=%u\n",
  332. sg_cnt, prot_sg_cnt, pages_per_mr);
  333. return -EINVAL;
  334. }
  335. ret = ib_dma_map_sgtable_attrs(dev, &sgt, dir, 0);
  336. if (ret)
  337. return ret;
  338. if (prot_sg_cnt) {
  339. ret = ib_dma_map_sgtable_attrs(dev, &prot_sgt, dir, 0);
  340. if (ret)
  341. goto out_unmap_sg;
  342. }
  343. ctx->type = RDMA_RW_SIG_MR;
  344. ctx->nr_ops = 1;
  345. ctx->reg = kzalloc(sizeof(*ctx->reg), GFP_KERNEL);
  346. if (!ctx->reg) {
  347. ret = -ENOMEM;
  348. goto out_unmap_prot_sg;
  349. }
  350. ctx->reg->mr = ib_mr_pool_get(qp, &qp->sig_mrs);
  351. if (!ctx->reg->mr) {
  352. ret = -EAGAIN;
  353. goto out_free_ctx;
  354. }
  355. count += rdma_rw_inv_key(ctx->reg);
  356. memcpy(ctx->reg->mr->sig_attrs, sig_attrs, sizeof(struct ib_sig_attrs));
  357. ret = ib_map_mr_sg_pi(ctx->reg->mr, sg, sgt.nents, NULL, prot_sg,
  358. prot_sgt.nents, NULL, SZ_4K);
  359. if (unlikely(ret)) {
  360. pr_err("failed to map PI sg (%u)\n",
  361. sgt.nents + prot_sgt.nents);
  362. goto out_destroy_sig_mr;
  363. }
  364. ctx->reg->reg_wr.wr.opcode = IB_WR_REG_MR_INTEGRITY;
  365. ctx->reg->reg_wr.wr.wr_cqe = NULL;
  366. ctx->reg->reg_wr.wr.num_sge = 0;
  367. ctx->reg->reg_wr.wr.send_flags = 0;
  368. ctx->reg->reg_wr.access = IB_ACCESS_LOCAL_WRITE;
  369. if (rdma_protocol_iwarp(qp->device, port_num))
  370. ctx->reg->reg_wr.access |= IB_ACCESS_REMOTE_WRITE;
  371. ctx->reg->reg_wr.mr = ctx->reg->mr;
  372. ctx->reg->reg_wr.key = ctx->reg->mr->lkey;
  373. count++;
  374. ctx->reg->sge.addr = ctx->reg->mr->iova;
  375. ctx->reg->sge.length = ctx->reg->mr->length;
  376. if (sig_attrs->wire.sig_type == IB_SIG_TYPE_NONE)
  377. ctx->reg->sge.length -= ctx->reg->mr->sig_attrs->meta_length;
  378. rdma_wr = &ctx->reg->wr;
  379. rdma_wr->wr.sg_list = &ctx->reg->sge;
  380. rdma_wr->wr.num_sge = 1;
  381. rdma_wr->remote_addr = remote_addr;
  382. rdma_wr->rkey = rkey;
  383. if (dir == DMA_TO_DEVICE)
  384. rdma_wr->wr.opcode = IB_WR_RDMA_WRITE;
  385. else
  386. rdma_wr->wr.opcode = IB_WR_RDMA_READ;
  387. ctx->reg->reg_wr.wr.next = &rdma_wr->wr;
  388. count++;
  389. return count;
  390. out_destroy_sig_mr:
  391. ib_mr_pool_put(qp, &qp->sig_mrs, ctx->reg->mr);
  392. out_free_ctx:
  393. kfree(ctx->reg);
  394. out_unmap_prot_sg:
  395. if (prot_sgt.nents)
  396. ib_dma_unmap_sgtable_attrs(dev, &prot_sgt, dir, 0);
  397. out_unmap_sg:
  398. ib_dma_unmap_sgtable_attrs(dev, &sgt, dir, 0);
  399. return ret;
  400. }
  401. EXPORT_SYMBOL(rdma_rw_ctx_signature_init);
  402. /*
  403. * Now that we are going to post the WRs we can update the lkey and need_inval
  404. * state on the MRs. If we were doing this at init time, we would get double
  405. * or missing invalidations if a context was initialized but not actually
  406. * posted.
  407. */
  408. static void rdma_rw_update_lkey(struct rdma_rw_reg_ctx *reg, bool need_inval)
  409. {
  410. reg->mr->need_inval = need_inval;
  411. ib_update_fast_reg_key(reg->mr, ib_inc_rkey(reg->mr->lkey));
  412. reg->reg_wr.key = reg->mr->lkey;
  413. reg->sge.lkey = reg->mr->lkey;
  414. }
  415. /**
  416. * rdma_rw_ctx_wrs - return chain of WRs for a RDMA READ or WRITE operation
  417. * @ctx: context to operate on
  418. * @qp: queue pair to operate on
  419. * @port_num: port num to which the connection is bound
  420. * @cqe: completion queue entry for the last WR
  421. * @chain_wr: WR to append to the posted chain
  422. *
  423. * Return the WR chain for the set of RDMA READ/WRITE operations described by
  424. * @ctx, as well as any memory registration operations needed. If @chain_wr
  425. * is non-NULL the WR it points to will be appended to the chain of WRs posted.
  426. * If @chain_wr is not set @cqe must be set so that the caller gets a
  427. * completion notification.
  428. */
  429. struct ib_send_wr *rdma_rw_ctx_wrs(struct rdma_rw_ctx *ctx, struct ib_qp *qp,
  430. u32 port_num, struct ib_cqe *cqe, struct ib_send_wr *chain_wr)
  431. {
  432. struct ib_send_wr *first_wr, *last_wr;
  433. int i;
  434. switch (ctx->type) {
  435. case RDMA_RW_SIG_MR:
  436. case RDMA_RW_MR:
  437. for (i = 0; i < ctx->nr_ops; i++) {
  438. rdma_rw_update_lkey(&ctx->reg[i],
  439. ctx->reg[i].wr.wr.opcode !=
  440. IB_WR_RDMA_READ_WITH_INV);
  441. }
  442. if (ctx->reg[0].inv_wr.next)
  443. first_wr = &ctx->reg[0].inv_wr;
  444. else
  445. first_wr = &ctx->reg[0].reg_wr.wr;
  446. last_wr = &ctx->reg[ctx->nr_ops - 1].wr.wr;
  447. break;
  448. case RDMA_RW_MULTI_WR:
  449. first_wr = &ctx->map.wrs[0].wr;
  450. last_wr = &ctx->map.wrs[ctx->nr_ops - 1].wr;
  451. break;
  452. case RDMA_RW_SINGLE_WR:
  453. first_wr = &ctx->single.wr.wr;
  454. last_wr = &ctx->single.wr.wr;
  455. break;
  456. default:
  457. BUG();
  458. }
  459. if (chain_wr) {
  460. last_wr->next = chain_wr;
  461. } else {
  462. last_wr->wr_cqe = cqe;
  463. last_wr->send_flags |= IB_SEND_SIGNALED;
  464. }
  465. return first_wr;
  466. }
  467. EXPORT_SYMBOL(rdma_rw_ctx_wrs);
  468. /**
  469. * rdma_rw_ctx_post - post a RDMA READ or RDMA WRITE operation
  470. * @ctx: context to operate on
  471. * @qp: queue pair to operate on
  472. * @port_num: port num to which the connection is bound
  473. * @cqe: completion queue entry for the last WR
  474. * @chain_wr: WR to append to the posted chain
  475. *
  476. * Post the set of RDMA READ/WRITE operations described by @ctx, as well as
  477. * any memory registration operations needed. If @chain_wr is non-NULL the
  478. * WR it points to will be appended to the chain of WRs posted. If @chain_wr
  479. * is not set @cqe must be set so that the caller gets a completion
  480. * notification.
  481. */
  482. int rdma_rw_ctx_post(struct rdma_rw_ctx *ctx, struct ib_qp *qp, u32 port_num,
  483. struct ib_cqe *cqe, struct ib_send_wr *chain_wr)
  484. {
  485. struct ib_send_wr *first_wr;
  486. first_wr = rdma_rw_ctx_wrs(ctx, qp, port_num, cqe, chain_wr);
  487. return ib_post_send(qp, first_wr, NULL);
  488. }
  489. EXPORT_SYMBOL(rdma_rw_ctx_post);
  490. /**
  491. * rdma_rw_ctx_destroy - release all resources allocated by rdma_rw_ctx_init
  492. * @ctx: context to release
  493. * @qp: queue pair to operate on
  494. * @port_num: port num to which the connection is bound
  495. * @sg: scatterlist that was used for the READ/WRITE
  496. * @sg_cnt: number of entries in @sg
  497. * @dir: %DMA_TO_DEVICE for RDMA WRITE, %DMA_FROM_DEVICE for RDMA READ
  498. */
  499. void rdma_rw_ctx_destroy(struct rdma_rw_ctx *ctx, struct ib_qp *qp,
  500. u32 port_num, struct scatterlist *sg, u32 sg_cnt,
  501. enum dma_data_direction dir)
  502. {
  503. int i;
  504. switch (ctx->type) {
  505. case RDMA_RW_MR:
  506. for (i = 0; i < ctx->nr_ops; i++)
  507. ib_mr_pool_put(qp, &qp->rdma_mrs, ctx->reg[i].mr);
  508. kfree(ctx->reg);
  509. break;
  510. case RDMA_RW_MULTI_WR:
  511. kfree(ctx->map.wrs);
  512. kfree(ctx->map.sges);
  513. break;
  514. case RDMA_RW_SINGLE_WR:
  515. break;
  516. default:
  517. BUG();
  518. break;
  519. }
  520. ib_dma_unmap_sg(qp->pd->device, sg, sg_cnt, dir);
  521. }
  522. EXPORT_SYMBOL(rdma_rw_ctx_destroy);
  523. /**
  524. * rdma_rw_ctx_destroy_signature - release all resources allocated by
  525. * rdma_rw_ctx_signature_init
  526. * @ctx: context to release
  527. * @qp: queue pair to operate on
  528. * @port_num: port num to which the connection is bound
  529. * @sg: scatterlist that was used for the READ/WRITE
  530. * @sg_cnt: number of entries in @sg
  531. * @prot_sg: scatterlist that was used for the READ/WRITE of the PI
  532. * @prot_sg_cnt: number of entries in @prot_sg
  533. * @dir: %DMA_TO_DEVICE for RDMA WRITE, %DMA_FROM_DEVICE for RDMA READ
  534. */
  535. void rdma_rw_ctx_destroy_signature(struct rdma_rw_ctx *ctx, struct ib_qp *qp,
  536. u32 port_num, struct scatterlist *sg, u32 sg_cnt,
  537. struct scatterlist *prot_sg, u32 prot_sg_cnt,
  538. enum dma_data_direction dir)
  539. {
  540. if (WARN_ON_ONCE(ctx->type != RDMA_RW_SIG_MR))
  541. return;
  542. ib_mr_pool_put(qp, &qp->sig_mrs, ctx->reg->mr);
  543. kfree(ctx->reg);
  544. if (prot_sg_cnt)
  545. ib_dma_unmap_sg(qp->pd->device, prot_sg, prot_sg_cnt, dir);
  546. ib_dma_unmap_sg(qp->pd->device, sg, sg_cnt, dir);
  547. }
  548. EXPORT_SYMBOL(rdma_rw_ctx_destroy_signature);
  549. /**
  550. * rdma_rw_mr_factor - return number of MRs required for a payload
  551. * @device: device handling the connection
  552. * @port_num: port num to which the connection is bound
  553. * @maxpages: maximum payload pages per rdma_rw_ctx
  554. *
  555. * Returns the number of MRs the device requires to move @maxpayload
  556. * bytes. The returned value is used during transport creation to
  557. * compute max_rdma_ctxts and the size of the transport's Send and
  558. * Send Completion Queues.
  559. */
  560. unsigned int rdma_rw_mr_factor(struct ib_device *device, u32 port_num,
  561. unsigned int maxpages)
  562. {
  563. unsigned int mr_pages;
  564. if (rdma_rw_can_use_mr(device, port_num))
  565. mr_pages = rdma_rw_fr_page_list_len(device, false);
  566. else
  567. mr_pages = device->attrs.max_sge_rd;
  568. return DIV_ROUND_UP(maxpages, mr_pages);
  569. }
  570. EXPORT_SYMBOL(rdma_rw_mr_factor);
  571. void rdma_rw_init_qp(struct ib_device *dev, struct ib_qp_init_attr *attr)
  572. {
  573. u32 factor;
  574. WARN_ON_ONCE(attr->port_num == 0);
  575. /*
  576. * Each context needs at least one RDMA READ or WRITE WR.
  577. *
  578. * For some hardware we might need more, eventually we should ask the
  579. * HCA driver for a multiplier here.
  580. */
  581. factor = 1;
  582. /*
  583. * If the device needs MRs to perform RDMA READ or WRITE operations,
  584. * we'll need two additional MRs for the registrations and the
  585. * invalidation.
  586. */
  587. if (attr->create_flags & IB_QP_CREATE_INTEGRITY_EN ||
  588. rdma_rw_can_use_mr(dev, attr->port_num))
  589. factor += 2; /* inv + reg */
  590. attr->cap.max_send_wr += factor * attr->cap.max_rdma_ctxs;
  591. /*
  592. * But maybe we were just too high in the sky and the device doesn't
  593. * even support all we need, and we'll have to live with what we get..
  594. */
  595. attr->cap.max_send_wr =
  596. min_t(u32, attr->cap.max_send_wr, dev->attrs.max_qp_wr);
  597. }
  598. int rdma_rw_init_mrs(struct ib_qp *qp, struct ib_qp_init_attr *attr)
  599. {
  600. struct ib_device *dev = qp->pd->device;
  601. u32 nr_mrs = 0, nr_sig_mrs = 0, max_num_sg = 0;
  602. int ret = 0;
  603. if (attr->create_flags & IB_QP_CREATE_INTEGRITY_EN) {
  604. nr_sig_mrs = attr->cap.max_rdma_ctxs;
  605. nr_mrs = attr->cap.max_rdma_ctxs;
  606. max_num_sg = rdma_rw_fr_page_list_len(dev, true);
  607. } else if (rdma_rw_can_use_mr(dev, attr->port_num)) {
  608. nr_mrs = attr->cap.max_rdma_ctxs;
  609. max_num_sg = rdma_rw_fr_page_list_len(dev, false);
  610. }
  611. if (nr_mrs) {
  612. ret = ib_mr_pool_init(qp, &qp->rdma_mrs, nr_mrs,
  613. IB_MR_TYPE_MEM_REG,
  614. max_num_sg, 0);
  615. if (ret) {
  616. pr_err("%s: failed to allocated %u MRs\n",
  617. __func__, nr_mrs);
  618. return ret;
  619. }
  620. }
  621. if (nr_sig_mrs) {
  622. ret = ib_mr_pool_init(qp, &qp->sig_mrs, nr_sig_mrs,
  623. IB_MR_TYPE_INTEGRITY, max_num_sg, max_num_sg);
  624. if (ret) {
  625. pr_err("%s: failed to allocated %u SIG MRs\n",
  626. __func__, nr_sig_mrs);
  627. goto out_free_rdma_mrs;
  628. }
  629. }
  630. return 0;
  631. out_free_rdma_mrs:
  632. ib_mr_pool_destroy(qp, &qp->rdma_mrs);
  633. return ret;
  634. }
  635. void rdma_rw_cleanup_mrs(struct ib_qp *qp)
  636. {
  637. ib_mr_pool_destroy(qp, &qp->sig_mrs);
  638. ib_mr_pool_destroy(qp, &qp->rdma_mrs);
  639. }