Linux Audio

Check our new training course

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