rw.c 21 KB

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