1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2016 HGST, a Western Digital Company.
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>
19 static bool rdma_rw_force_mr
;
20 module_param_named(force_mr
, rdma_rw_force_mr
, bool, 0);
21 MODULE_PARM_DESC(force_mr
, "Force usage of MRs for RDMA READ/WRITE operations");
24 * Report whether memory registration should be used. Memory registration must
25 * be used for iWarp devices because of iWARP-specific limitations. Memory
26 * registration is also enabled if registering memory might yield better
27 * performance than using multiple SGE entries, see rdma_rw_io_needs_mr()
29 static inline bool rdma_rw_can_use_mr(struct ib_device
*dev
, u32 port_num
)
31 if (rdma_protocol_iwarp(dev
, port_num
))
33 if (dev
->attrs
.max_sgl_rd
)
35 if (unlikely(rdma_rw_force_mr
))
41 * Check if the device will use memory registration for this RW operation.
42 * For RDMA READs we must use MRs on iWarp and can optionally use them as an
43 * optimization otherwise. Additionally we have a debug option to force usage
44 * of MRs to help testing this code path.
46 static inline bool rdma_rw_io_needs_mr(struct ib_device
*dev
, u32 port_num
,
47 enum dma_data_direction dir
, int dma_nents
)
49 if (dir
== DMA_FROM_DEVICE
) {
50 if (rdma_protocol_iwarp(dev
, port_num
))
52 if (dev
->attrs
.max_sgl_rd
&& dma_nents
> dev
->attrs
.max_sgl_rd
)
55 if (unlikely(rdma_rw_force_mr
))
60 static inline u32
rdma_rw_fr_page_list_len(struct ib_device
*dev
,
66 max_pages
= dev
->attrs
.max_pi_fast_reg_page_list_len
;
68 max_pages
= dev
->attrs
.max_fast_reg_page_list_len
;
70 /* arbitrary limit to avoid allocating gigantic resources */
71 return min_t(u32
, max_pages
, 256);
74 static inline int rdma_rw_inv_key(struct rdma_rw_reg_ctx
*reg
)
78 if (reg
->mr
->need_inval
) {
79 reg
->inv_wr
.opcode
= IB_WR_LOCAL_INV
;
80 reg
->inv_wr
.ex
.invalidate_rkey
= reg
->mr
->lkey
;
81 reg
->inv_wr
.next
= ®
->reg_wr
.wr
;
84 reg
->inv_wr
.next
= NULL
;
90 /* Caller must have zero-initialized *reg. */
91 static int rdma_rw_init_one_mr(struct ib_qp
*qp
, u32 port_num
,
92 struct rdma_rw_reg_ctx
*reg
, struct scatterlist
*sg
,
93 u32 sg_cnt
, u32 offset
)
95 u32 pages_per_mr
= rdma_rw_fr_page_list_len(qp
->pd
->device
,
97 u32 nents
= min(sg_cnt
, pages_per_mr
);
100 reg
->mr
= ib_mr_pool_get(qp
, &qp
->rdma_mrs
);
104 count
+= rdma_rw_inv_key(reg
);
106 ret
= ib_map_mr_sg(reg
->mr
, sg
, nents
, &offset
, PAGE_SIZE
);
107 if (ret
< 0 || ret
< nents
) {
108 ib_mr_pool_put(qp
, &qp
->rdma_mrs
, reg
->mr
);
112 reg
->reg_wr
.wr
.opcode
= IB_WR_REG_MR
;
113 reg
->reg_wr
.mr
= reg
->mr
;
114 reg
->reg_wr
.access
= IB_ACCESS_LOCAL_WRITE
;
115 if (rdma_protocol_iwarp(qp
->device
, port_num
))
116 reg
->reg_wr
.access
|= IB_ACCESS_REMOTE_WRITE
;
119 reg
->sge
.addr
= reg
->mr
->iova
;
120 reg
->sge
.length
= reg
->mr
->length
;
124 static int rdma_rw_init_mr_wrs(struct rdma_rw_ctx
*ctx
, struct ib_qp
*qp
,
125 u32 port_num
, struct scatterlist
*sg
, u32 sg_cnt
, u32 offset
,
126 u64 remote_addr
, u32 rkey
, enum dma_data_direction dir
)
128 struct rdma_rw_reg_ctx
*prev
= NULL
;
129 u32 pages_per_mr
= rdma_rw_fr_page_list_len(qp
->pd
->device
,
131 int i
, j
, ret
= 0, count
= 0;
133 ctx
->nr_ops
= DIV_ROUND_UP(sg_cnt
, pages_per_mr
);
134 ctx
->reg
= kcalloc(ctx
->nr_ops
, sizeof(*ctx
->reg
), GFP_KERNEL
);
140 for (i
= 0; i
< ctx
->nr_ops
; i
++) {
141 struct rdma_rw_reg_ctx
*reg
= &ctx
->reg
[i
];
142 u32 nents
= min(sg_cnt
, pages_per_mr
);
144 ret
= rdma_rw_init_one_mr(qp
, port_num
, reg
, sg
, sg_cnt
,
151 if (reg
->mr
->need_inval
)
152 prev
->wr
.wr
.next
= ®
->inv_wr
;
154 prev
->wr
.wr
.next
= ®
->reg_wr
.wr
;
157 reg
->reg_wr
.wr
.next
= ®
->wr
.wr
;
159 reg
->wr
.wr
.sg_list
= ®
->sge
;
160 reg
->wr
.wr
.num_sge
= 1;
161 reg
->wr
.remote_addr
= remote_addr
;
163 if (dir
== DMA_TO_DEVICE
) {
164 reg
->wr
.wr
.opcode
= IB_WR_RDMA_WRITE
;
165 } else if (!rdma_cap_read_inv(qp
->device
, port_num
)) {
166 reg
->wr
.wr
.opcode
= IB_WR_RDMA_READ
;
168 reg
->wr
.wr
.opcode
= IB_WR_RDMA_READ_WITH_INV
;
169 reg
->wr
.wr
.ex
.invalidate_rkey
= reg
->mr
->lkey
;
173 remote_addr
+= reg
->sge
.length
;
175 for (j
= 0; j
< nents
; j
++)
182 prev
->wr
.wr
.next
= NULL
;
184 ctx
->type
= RDMA_RW_MR
;
189 ib_mr_pool_put(qp
, &qp
->rdma_mrs
, ctx
->reg
[i
].mr
);
195 static int rdma_rw_init_map_wrs(struct rdma_rw_ctx
*ctx
, struct ib_qp
*qp
,
196 struct scatterlist
*sg
, u32 sg_cnt
, u32 offset
,
197 u64 remote_addr
, u32 rkey
, enum dma_data_direction dir
)
199 u32 max_sge
= dir
== DMA_TO_DEVICE
? qp
->max_write_sge
:
202 u32 total_len
= 0, i
, j
;
204 ctx
->nr_ops
= DIV_ROUND_UP(sg_cnt
, max_sge
);
206 ctx
->map
.sges
= sge
= kcalloc(sg_cnt
, sizeof(*sge
), GFP_KERNEL
);
210 ctx
->map
.wrs
= kcalloc(ctx
->nr_ops
, sizeof(*ctx
->map
.wrs
), GFP_KERNEL
);
214 for (i
= 0; i
< ctx
->nr_ops
; i
++) {
215 struct ib_rdma_wr
*rdma_wr
= &ctx
->map
.wrs
[i
];
216 u32 nr_sge
= min(sg_cnt
, max_sge
);
218 if (dir
== DMA_TO_DEVICE
)
219 rdma_wr
->wr
.opcode
= IB_WR_RDMA_WRITE
;
221 rdma_wr
->wr
.opcode
= IB_WR_RDMA_READ
;
222 rdma_wr
->remote_addr
= remote_addr
+ total_len
;
223 rdma_wr
->rkey
= rkey
;
224 rdma_wr
->wr
.num_sge
= nr_sge
;
225 rdma_wr
->wr
.sg_list
= sge
;
227 for (j
= 0; j
< nr_sge
; j
++, sg
= sg_next(sg
)) {
228 sge
->addr
= sg_dma_address(sg
) + offset
;
229 sge
->length
= sg_dma_len(sg
) - offset
;
230 sge
->lkey
= qp
->pd
->local_dma_lkey
;
232 total_len
+= sge
->length
;
238 rdma_wr
->wr
.next
= i
+ 1 < ctx
->nr_ops
?
239 &ctx
->map
.wrs
[i
+ 1].wr
: NULL
;
242 ctx
->type
= RDMA_RW_MULTI_WR
;
246 kfree(ctx
->map
.sges
);
251 static int rdma_rw_init_single_wr(struct rdma_rw_ctx
*ctx
, struct ib_qp
*qp
,
252 struct scatterlist
*sg
, u32 offset
, u64 remote_addr
, u32 rkey
,
253 enum dma_data_direction dir
)
255 struct ib_rdma_wr
*rdma_wr
= &ctx
->single
.wr
;
259 ctx
->single
.sge
.lkey
= qp
->pd
->local_dma_lkey
;
260 ctx
->single
.sge
.addr
= sg_dma_address(sg
) + offset
;
261 ctx
->single
.sge
.length
= sg_dma_len(sg
) - offset
;
263 memset(rdma_wr
, 0, sizeof(*rdma_wr
));
264 if (dir
== DMA_TO_DEVICE
)
265 rdma_wr
->wr
.opcode
= IB_WR_RDMA_WRITE
;
267 rdma_wr
->wr
.opcode
= IB_WR_RDMA_READ
;
268 rdma_wr
->wr
.sg_list
= &ctx
->single
.sge
;
269 rdma_wr
->wr
.num_sge
= 1;
270 rdma_wr
->remote_addr
= remote_addr
;
271 rdma_wr
->rkey
= rkey
;
273 ctx
->type
= RDMA_RW_SINGLE_WR
;
278 * rdma_rw_ctx_init - initialize a RDMA READ/WRITE context
279 * @ctx: context to initialize
280 * @qp: queue pair to operate on
281 * @port_num: port num to which the connection is bound
282 * @sg: scatterlist to READ/WRITE from/to
283 * @sg_cnt: number of entries in @sg
284 * @sg_offset: current byte offset into @sg
285 * @remote_addr:remote address to read/write (relative to @rkey)
286 * @rkey: remote key to operate on
287 * @dir: %DMA_TO_DEVICE for RDMA WRITE, %DMA_FROM_DEVICE for RDMA READ
289 * Returns the number of WQEs that will be needed on the workqueue if
290 * successful, or a negative error code.
292 int rdma_rw_ctx_init(struct rdma_rw_ctx
*ctx
, struct ib_qp
*qp
, u32 port_num
,
293 struct scatterlist
*sg
, u32 sg_cnt
, u32 sg_offset
,
294 u64 remote_addr
, u32 rkey
, enum dma_data_direction dir
)
296 struct ib_device
*dev
= qp
->pd
->device
;
297 struct sg_table sgt
= {
299 .orig_nents
= sg_cnt
,
303 ret
= ib_dma_map_sgtable_attrs(dev
, &sgt
, dir
, 0);
309 * Skip to the S/G entry that sg_offset falls into:
312 u32 len
= sg_dma_len(sg
);
323 if (WARN_ON_ONCE(sg_cnt
== 0))
326 if (rdma_rw_io_needs_mr(qp
->device
, port_num
, dir
, sg_cnt
)) {
327 ret
= rdma_rw_init_mr_wrs(ctx
, qp
, port_num
, sg
, sg_cnt
,
328 sg_offset
, remote_addr
, rkey
, dir
);
329 } else if (sg_cnt
> 1) {
330 ret
= rdma_rw_init_map_wrs(ctx
, qp
, sg
, sg_cnt
, sg_offset
,
331 remote_addr
, rkey
, dir
);
333 ret
= rdma_rw_init_single_wr(ctx
, qp
, sg
, sg_offset
,
334 remote_addr
, rkey
, dir
);
342 ib_dma_unmap_sgtable_attrs(dev
, &sgt
, dir
, 0);
345 EXPORT_SYMBOL(rdma_rw_ctx_init
);
348 * rdma_rw_ctx_signature_init - initialize a RW context with signature offload
349 * @ctx: context to initialize
350 * @qp: queue pair to operate on
351 * @port_num: port num to which the connection is bound
352 * @sg: scatterlist to READ/WRITE from/to
353 * @sg_cnt: number of entries in @sg
354 * @prot_sg: scatterlist to READ/WRITE protection information from/to
355 * @prot_sg_cnt: number of entries in @prot_sg
356 * @sig_attrs: signature offloading algorithms
357 * @remote_addr:remote address to read/write (relative to @rkey)
358 * @rkey: remote key to operate on
359 * @dir: %DMA_TO_DEVICE for RDMA WRITE, %DMA_FROM_DEVICE for RDMA READ
361 * Returns the number of WQEs that will be needed on the workqueue if
362 * successful, or a negative error code.
364 int rdma_rw_ctx_signature_init(struct rdma_rw_ctx
*ctx
, struct ib_qp
*qp
,
365 u32 port_num
, struct scatterlist
*sg
, u32 sg_cnt
,
366 struct scatterlist
*prot_sg
, u32 prot_sg_cnt
,
367 struct ib_sig_attrs
*sig_attrs
,
368 u64 remote_addr
, u32 rkey
, enum dma_data_direction dir
)
370 struct ib_device
*dev
= qp
->pd
->device
;
371 u32 pages_per_mr
= rdma_rw_fr_page_list_len(qp
->pd
->device
,
373 struct sg_table sgt
= {
375 .orig_nents
= sg_cnt
,
377 struct sg_table prot_sgt
= {
379 .orig_nents
= prot_sg_cnt
,
381 struct ib_rdma_wr
*rdma_wr
;
384 if (sg_cnt
> pages_per_mr
|| prot_sg_cnt
> pages_per_mr
) {
385 pr_err("SG count too large: sg_cnt=%u, prot_sg_cnt=%u, pages_per_mr=%u\n",
386 sg_cnt
, prot_sg_cnt
, pages_per_mr
);
390 ret
= ib_dma_map_sgtable_attrs(dev
, &sgt
, dir
, 0);
395 ret
= ib_dma_map_sgtable_attrs(dev
, &prot_sgt
, dir
, 0);
400 ctx
->type
= RDMA_RW_SIG_MR
;
402 ctx
->reg
= kzalloc(sizeof(*ctx
->reg
), GFP_KERNEL
);
405 goto out_unmap_prot_sg
;
408 ctx
->reg
->mr
= ib_mr_pool_get(qp
, &qp
->sig_mrs
);
414 count
+= rdma_rw_inv_key(ctx
->reg
);
416 memcpy(ctx
->reg
->mr
->sig_attrs
, sig_attrs
, sizeof(struct ib_sig_attrs
));
418 ret
= ib_map_mr_sg_pi(ctx
->reg
->mr
, sg
, sgt
.nents
, NULL
, prot_sg
,
419 prot_sgt
.nents
, NULL
, SZ_4K
);
421 pr_err("failed to map PI sg (%u)\n",
422 sgt
.nents
+ prot_sgt
.nents
);
423 goto out_destroy_sig_mr
;
426 ctx
->reg
->reg_wr
.wr
.opcode
= IB_WR_REG_MR_INTEGRITY
;
427 ctx
->reg
->reg_wr
.wr
.wr_cqe
= NULL
;
428 ctx
->reg
->reg_wr
.wr
.num_sge
= 0;
429 ctx
->reg
->reg_wr
.wr
.send_flags
= 0;
430 ctx
->reg
->reg_wr
.access
= IB_ACCESS_LOCAL_WRITE
;
431 if (rdma_protocol_iwarp(qp
->device
, port_num
))
432 ctx
->reg
->reg_wr
.access
|= IB_ACCESS_REMOTE_WRITE
;
433 ctx
->reg
->reg_wr
.mr
= ctx
->reg
->mr
;
434 ctx
->reg
->reg_wr
.key
= ctx
->reg
->mr
->lkey
;
437 ctx
->reg
->sge
.addr
= ctx
->reg
->mr
->iova
;
438 ctx
->reg
->sge
.length
= ctx
->reg
->mr
->length
;
439 if (sig_attrs
->wire
.sig_type
== IB_SIG_TYPE_NONE
)
440 ctx
->reg
->sge
.length
-= ctx
->reg
->mr
->sig_attrs
->meta_length
;
442 rdma_wr
= &ctx
->reg
->wr
;
443 rdma_wr
->wr
.sg_list
= &ctx
->reg
->sge
;
444 rdma_wr
->wr
.num_sge
= 1;
445 rdma_wr
->remote_addr
= remote_addr
;
446 rdma_wr
->rkey
= rkey
;
447 if (dir
== DMA_TO_DEVICE
)
448 rdma_wr
->wr
.opcode
= IB_WR_RDMA_WRITE
;
450 rdma_wr
->wr
.opcode
= IB_WR_RDMA_READ
;
451 ctx
->reg
->reg_wr
.wr
.next
= &rdma_wr
->wr
;
457 ib_mr_pool_put(qp
, &qp
->sig_mrs
, ctx
->reg
->mr
);
462 ib_dma_unmap_sgtable_attrs(dev
, &prot_sgt
, dir
, 0);
464 ib_dma_unmap_sgtable_attrs(dev
, &sgt
, dir
, 0);
467 EXPORT_SYMBOL(rdma_rw_ctx_signature_init
);
470 * Now that we are going to post the WRs we can update the lkey and need_inval
471 * state on the MRs. If we were doing this at init time, we would get double
472 * or missing invalidations if a context was initialized but not actually
475 static void rdma_rw_update_lkey(struct rdma_rw_reg_ctx
*reg
, bool need_inval
)
477 reg
->mr
->need_inval
= need_inval
;
478 ib_update_fast_reg_key(reg
->mr
, ib_inc_rkey(reg
->mr
->lkey
));
479 reg
->reg_wr
.key
= reg
->mr
->lkey
;
480 reg
->sge
.lkey
= reg
->mr
->lkey
;
484 * rdma_rw_ctx_wrs - return chain of WRs for a RDMA READ or WRITE operation
485 * @ctx: context to operate on
486 * @qp: queue pair to operate on
487 * @port_num: port num to which the connection is bound
488 * @cqe: completion queue entry for the last WR
489 * @chain_wr: WR to append to the posted chain
491 * Return the WR chain for the set of RDMA READ/WRITE operations described by
492 * @ctx, as well as any memory registration operations needed. If @chain_wr
493 * is non-NULL the WR it points to will be appended to the chain of WRs posted.
494 * If @chain_wr is not set @cqe must be set so that the caller gets a
495 * completion notification.
497 struct ib_send_wr
*rdma_rw_ctx_wrs(struct rdma_rw_ctx
*ctx
, struct ib_qp
*qp
,
498 u32 port_num
, struct ib_cqe
*cqe
, struct ib_send_wr
*chain_wr
)
500 struct ib_send_wr
*first_wr
, *last_wr
;
506 for (i
= 0; i
< ctx
->nr_ops
; i
++) {
507 rdma_rw_update_lkey(&ctx
->reg
[i
],
508 ctx
->reg
[i
].wr
.wr
.opcode
!=
509 IB_WR_RDMA_READ_WITH_INV
);
512 if (ctx
->reg
[0].inv_wr
.next
)
513 first_wr
= &ctx
->reg
[0].inv_wr
;
515 first_wr
= &ctx
->reg
[0].reg_wr
.wr
;
516 last_wr
= &ctx
->reg
[ctx
->nr_ops
- 1].wr
.wr
;
518 case RDMA_RW_MULTI_WR
:
519 first_wr
= &ctx
->map
.wrs
[0].wr
;
520 last_wr
= &ctx
->map
.wrs
[ctx
->nr_ops
- 1].wr
;
522 case RDMA_RW_SINGLE_WR
:
523 first_wr
= &ctx
->single
.wr
.wr
;
524 last_wr
= &ctx
->single
.wr
.wr
;
531 last_wr
->next
= chain_wr
;
533 last_wr
->wr_cqe
= cqe
;
534 last_wr
->send_flags
|= IB_SEND_SIGNALED
;
539 EXPORT_SYMBOL(rdma_rw_ctx_wrs
);
542 * rdma_rw_ctx_post - post a RDMA READ or RDMA WRITE operation
543 * @ctx: context to operate on
544 * @qp: queue pair to operate on
545 * @port_num: port num to which the connection is bound
546 * @cqe: completion queue entry for the last WR
547 * @chain_wr: WR to append to the posted chain
549 * Post the set of RDMA READ/WRITE operations described by @ctx, as well as
550 * any memory registration operations needed. If @chain_wr is non-NULL the
551 * WR it points to will be appended to the chain of WRs posted. If @chain_wr
552 * is not set @cqe must be set so that the caller gets a completion
555 int rdma_rw_ctx_post(struct rdma_rw_ctx
*ctx
, struct ib_qp
*qp
, u32 port_num
,
556 struct ib_cqe
*cqe
, struct ib_send_wr
*chain_wr
)
558 struct ib_send_wr
*first_wr
;
560 first_wr
= rdma_rw_ctx_wrs(ctx
, qp
, port_num
, cqe
, chain_wr
);
561 return ib_post_send(qp
, first_wr
, NULL
);
563 EXPORT_SYMBOL(rdma_rw_ctx_post
);
566 * rdma_rw_ctx_destroy - release all resources allocated by rdma_rw_ctx_init
567 * @ctx: context to release
568 * @qp: queue pair to operate on
569 * @port_num: port num to which the connection is bound
570 * @sg: scatterlist that was used for the READ/WRITE
571 * @sg_cnt: number of entries in @sg
572 * @dir: %DMA_TO_DEVICE for RDMA WRITE, %DMA_FROM_DEVICE for RDMA READ
574 void rdma_rw_ctx_destroy(struct rdma_rw_ctx
*ctx
, struct ib_qp
*qp
,
575 u32 port_num
, struct scatterlist
*sg
, u32 sg_cnt
,
576 enum dma_data_direction dir
)
582 for (i
= 0; i
< ctx
->nr_ops
; i
++)
583 ib_mr_pool_put(qp
, &qp
->rdma_mrs
, ctx
->reg
[i
].mr
);
586 case RDMA_RW_MULTI_WR
:
588 kfree(ctx
->map
.sges
);
590 case RDMA_RW_SINGLE_WR
:
597 ib_dma_unmap_sg(qp
->pd
->device
, sg
, sg_cnt
, dir
);
599 EXPORT_SYMBOL(rdma_rw_ctx_destroy
);
602 * rdma_rw_ctx_destroy_signature - release all resources allocated by
603 * rdma_rw_ctx_signature_init
604 * @ctx: context to release
605 * @qp: queue pair to operate on
606 * @port_num: port num to which the connection is bound
607 * @sg: scatterlist that was used for the READ/WRITE
608 * @sg_cnt: number of entries in @sg
609 * @prot_sg: scatterlist that was used for the READ/WRITE of the PI
610 * @prot_sg_cnt: number of entries in @prot_sg
611 * @dir: %DMA_TO_DEVICE for RDMA WRITE, %DMA_FROM_DEVICE for RDMA READ
613 void rdma_rw_ctx_destroy_signature(struct rdma_rw_ctx
*ctx
, struct ib_qp
*qp
,
614 u32 port_num
, struct scatterlist
*sg
, u32 sg_cnt
,
615 struct scatterlist
*prot_sg
, u32 prot_sg_cnt
,
616 enum dma_data_direction dir
)
618 if (WARN_ON_ONCE(ctx
->type
!= RDMA_RW_SIG_MR
))
621 ib_mr_pool_put(qp
, &qp
->sig_mrs
, ctx
->reg
->mr
);
625 ib_dma_unmap_sg(qp
->pd
->device
, prot_sg
, prot_sg_cnt
, dir
);
626 ib_dma_unmap_sg(qp
->pd
->device
, sg
, sg_cnt
, dir
);
628 EXPORT_SYMBOL(rdma_rw_ctx_destroy_signature
);
631 * rdma_rw_mr_factor - return number of MRs required for a payload
632 * @device: device handling the connection
633 * @port_num: port num to which the connection is bound
634 * @maxpages: maximum payload pages per rdma_rw_ctx
636 * Returns the number of MRs the device requires to move @maxpayload
637 * bytes. The returned value is used during transport creation to
638 * compute max_rdma_ctxts and the size of the transport's Send and
639 * Send Completion Queues.
641 unsigned int rdma_rw_mr_factor(struct ib_device
*device
, u32 port_num
,
642 unsigned int maxpages
)
644 unsigned int mr_pages
;
646 if (rdma_rw_can_use_mr(device
, port_num
))
647 mr_pages
= rdma_rw_fr_page_list_len(device
, false);
649 mr_pages
= device
->attrs
.max_sge_rd
;
650 return DIV_ROUND_UP(maxpages
, mr_pages
);
652 EXPORT_SYMBOL(rdma_rw_mr_factor
);
654 void rdma_rw_init_qp(struct ib_device
*dev
, struct ib_qp_init_attr
*attr
)
658 WARN_ON_ONCE(attr
->port_num
== 0);
661 * Each context needs at least one RDMA READ or WRITE WR.
663 * For some hardware we might need more, eventually we should ask the
664 * HCA driver for a multiplier here.
669 * If the device needs MRs to perform RDMA READ or WRITE operations,
670 * we'll need two additional MRs for the registrations and the
673 if (attr
->create_flags
& IB_QP_CREATE_INTEGRITY_EN
||
674 rdma_rw_can_use_mr(dev
, attr
->port_num
))
675 factor
+= 2; /* inv + reg */
677 attr
->cap
.max_send_wr
+= factor
* attr
->cap
.max_rdma_ctxs
;
680 * But maybe we were just too high in the sky and the device doesn't
681 * even support all we need, and we'll have to live with what we get..
683 attr
->cap
.max_send_wr
=
684 min_t(u32
, attr
->cap
.max_send_wr
, dev
->attrs
.max_qp_wr
);
687 int rdma_rw_init_mrs(struct ib_qp
*qp
, struct ib_qp_init_attr
*attr
)
689 struct ib_device
*dev
= qp
->pd
->device
;
690 u32 nr_mrs
= 0, nr_sig_mrs
= 0, max_num_sg
= 0;
693 if (attr
->create_flags
& IB_QP_CREATE_INTEGRITY_EN
) {
694 nr_sig_mrs
= attr
->cap
.max_rdma_ctxs
;
695 nr_mrs
= attr
->cap
.max_rdma_ctxs
;
696 max_num_sg
= rdma_rw_fr_page_list_len(dev
, true);
697 } else if (rdma_rw_can_use_mr(dev
, attr
->port_num
)) {
698 nr_mrs
= attr
->cap
.max_rdma_ctxs
;
699 max_num_sg
= rdma_rw_fr_page_list_len(dev
, false);
703 ret
= ib_mr_pool_init(qp
, &qp
->rdma_mrs
, nr_mrs
,
707 pr_err("%s: failed to allocated %u MRs\n",
714 ret
= ib_mr_pool_init(qp
, &qp
->sig_mrs
, nr_sig_mrs
,
715 IB_MR_TYPE_INTEGRITY
, max_num_sg
, max_num_sg
);
717 pr_err("%s: failed to allocated %u SIG MRs\n",
718 __func__
, nr_sig_mrs
);
719 goto out_free_rdma_mrs
;
726 ib_mr_pool_destroy(qp
, &qp
->rdma_mrs
);
730 void rdma_rw_cleanup_mrs(struct ib_qp
*qp
)
732 ib_mr_pool_destroy(qp
, &qp
->sig_mrs
);
733 ib_mr_pool_destroy(qp
, &qp
->rdma_mrs
);