xref: /openbmc/linux/drivers/infiniband/core/rw.c (revision a163afc8)
1a060b562SChristoph Hellwig /*
2a060b562SChristoph Hellwig  * Copyright (c) 2016 HGST, a Western Digital Company.
3a060b562SChristoph Hellwig  *
4a060b562SChristoph Hellwig  * This program is free software; you can redistribute it and/or modify it
5a060b562SChristoph Hellwig  * under the terms and conditions of the GNU General Public License,
6a060b562SChristoph Hellwig  * version 2, as published by the Free Software Foundation.
7a060b562SChristoph Hellwig  *
8a060b562SChristoph Hellwig  * This program is distributed in the hope it will be useful, but WITHOUT
9a060b562SChristoph Hellwig  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10a060b562SChristoph Hellwig  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11a060b562SChristoph Hellwig  * more details.
12a060b562SChristoph Hellwig  */
13a060b562SChristoph Hellwig #include <linux/moduleparam.h>
14a060b562SChristoph Hellwig #include <linux/slab.h>
1550b7d220SLogan Gunthorpe #include <linux/pci-p2pdma.h>
16a060b562SChristoph Hellwig #include <rdma/mr_pool.h>
17a060b562SChristoph Hellwig #include <rdma/rw.h>
18a060b562SChristoph Hellwig 
19a060b562SChristoph Hellwig enum {
20a060b562SChristoph Hellwig 	RDMA_RW_SINGLE_WR,
21a060b562SChristoph Hellwig 	RDMA_RW_MULTI_WR,
22a060b562SChristoph Hellwig 	RDMA_RW_MR,
230e353e34SChristoph Hellwig 	RDMA_RW_SIG_MR,
24a060b562SChristoph Hellwig };
25a060b562SChristoph Hellwig 
26a060b562SChristoph Hellwig static bool rdma_rw_force_mr;
27a060b562SChristoph Hellwig module_param_named(force_mr, rdma_rw_force_mr, bool, 0);
28a060b562SChristoph Hellwig MODULE_PARM_DESC(force_mr, "Force usage of MRs for RDMA READ/WRITE operations");
29a060b562SChristoph Hellwig 
30a060b562SChristoph Hellwig /*
31a060b562SChristoph Hellwig  * Check if the device might use memory registration.  This is currently only
32a060b562SChristoph Hellwig  * true for iWarp devices. In the future we can hopefully fine tune this based
33a060b562SChristoph Hellwig  * on HCA driver input.
34a060b562SChristoph Hellwig  */
35a060b562SChristoph Hellwig static inline bool rdma_rw_can_use_mr(struct ib_device *dev, u8 port_num)
36a060b562SChristoph Hellwig {
37a060b562SChristoph Hellwig 	if (rdma_protocol_iwarp(dev, port_num))
38a060b562SChristoph Hellwig 		return true;
39a060b562SChristoph Hellwig 	if (unlikely(rdma_rw_force_mr))
40a060b562SChristoph Hellwig 		return true;
41a060b562SChristoph Hellwig 	return false;
42a060b562SChristoph Hellwig }
43a060b562SChristoph Hellwig 
44a060b562SChristoph Hellwig /*
45a060b562SChristoph Hellwig  * Check if the device will use memory registration for this RW operation.
46a060b562SChristoph Hellwig  * We currently always use memory registrations for iWarp RDMA READs, and
47a060b562SChristoph Hellwig  * have a debug option to force usage of MRs.
48a060b562SChristoph Hellwig  *
49a060b562SChristoph Hellwig  * XXX: In the future we can hopefully fine tune this based on HCA driver
50a060b562SChristoph Hellwig  * input.
51a060b562SChristoph Hellwig  */
52a060b562SChristoph Hellwig static inline bool rdma_rw_io_needs_mr(struct ib_device *dev, u8 port_num,
53a060b562SChristoph Hellwig 		enum dma_data_direction dir, int dma_nents)
54a060b562SChristoph Hellwig {
55a060b562SChristoph Hellwig 	if (rdma_protocol_iwarp(dev, port_num) && dir == DMA_FROM_DEVICE)
56a060b562SChristoph Hellwig 		return true;
57a060b562SChristoph Hellwig 	if (unlikely(rdma_rw_force_mr))
58a060b562SChristoph Hellwig 		return true;
59a060b562SChristoph Hellwig 	return false;
60a060b562SChristoph Hellwig }
61a060b562SChristoph Hellwig 
62a060b562SChristoph Hellwig static inline u32 rdma_rw_fr_page_list_len(struct ib_device *dev)
63a060b562SChristoph Hellwig {
64a060b562SChristoph Hellwig 	/* arbitrary limit to avoid allocating gigantic resources */
65a060b562SChristoph Hellwig 	return min_t(u32, dev->attrs.max_fast_reg_page_list_len, 256);
66a060b562SChristoph Hellwig }
67a060b562SChristoph Hellwig 
68eaa74ec7SBart Van Assche /* Caller must have zero-initialized *reg. */
69a060b562SChristoph Hellwig static int rdma_rw_init_one_mr(struct ib_qp *qp, u8 port_num,
70a060b562SChristoph Hellwig 		struct rdma_rw_reg_ctx *reg, struct scatterlist *sg,
71a060b562SChristoph Hellwig 		u32 sg_cnt, u32 offset)
72a060b562SChristoph Hellwig {
73a060b562SChristoph Hellwig 	u32 pages_per_mr = rdma_rw_fr_page_list_len(qp->pd->device);
74a060b562SChristoph Hellwig 	u32 nents = min(sg_cnt, pages_per_mr);
75a060b562SChristoph Hellwig 	int count = 0, ret;
76a060b562SChristoph Hellwig 
77a060b562SChristoph Hellwig 	reg->mr = ib_mr_pool_get(qp, &qp->rdma_mrs);
78a060b562SChristoph Hellwig 	if (!reg->mr)
79a060b562SChristoph Hellwig 		return -EAGAIN;
80a060b562SChristoph Hellwig 
81a060b562SChristoph Hellwig 	if (reg->mr->need_inval) {
82a060b562SChristoph Hellwig 		reg->inv_wr.opcode = IB_WR_LOCAL_INV;
83a060b562SChristoph Hellwig 		reg->inv_wr.ex.invalidate_rkey = reg->mr->lkey;
84a060b562SChristoph Hellwig 		reg->inv_wr.next = &reg->reg_wr.wr;
85a060b562SChristoph Hellwig 		count++;
86a060b562SChristoph Hellwig 	} else {
87a060b562SChristoph Hellwig 		reg->inv_wr.next = NULL;
88a060b562SChristoph Hellwig 	}
89a060b562SChristoph Hellwig 
909aa8b321SBart Van Assche 	ret = ib_map_mr_sg(reg->mr, sg, nents, &offset, PAGE_SIZE);
91c2d7c8ffSDan Carpenter 	if (ret < 0 || ret < nents) {
92a060b562SChristoph Hellwig 		ib_mr_pool_put(qp, &qp->rdma_mrs, reg->mr);
93a060b562SChristoph Hellwig 		return -EINVAL;
94a060b562SChristoph Hellwig 	}
95a060b562SChristoph Hellwig 
96a060b562SChristoph Hellwig 	reg->reg_wr.wr.opcode = IB_WR_REG_MR;
97a060b562SChristoph Hellwig 	reg->reg_wr.mr = reg->mr;
98a060b562SChristoph Hellwig 	reg->reg_wr.access = IB_ACCESS_LOCAL_WRITE;
99a060b562SChristoph Hellwig 	if (rdma_protocol_iwarp(qp->device, port_num))
100a060b562SChristoph Hellwig 		reg->reg_wr.access |= IB_ACCESS_REMOTE_WRITE;
101a060b562SChristoph Hellwig 	count++;
102a060b562SChristoph Hellwig 
103a060b562SChristoph Hellwig 	reg->sge.addr = reg->mr->iova;
104a060b562SChristoph Hellwig 	reg->sge.length = reg->mr->length;
105a060b562SChristoph Hellwig 	return count;
106a060b562SChristoph Hellwig }
107a060b562SChristoph Hellwig 
108a060b562SChristoph Hellwig static int rdma_rw_init_mr_wrs(struct rdma_rw_ctx *ctx, struct ib_qp *qp,
109a060b562SChristoph Hellwig 		u8 port_num, struct scatterlist *sg, u32 sg_cnt, u32 offset,
110a060b562SChristoph Hellwig 		u64 remote_addr, u32 rkey, enum dma_data_direction dir)
111a060b562SChristoph Hellwig {
112eaa74ec7SBart Van Assche 	struct rdma_rw_reg_ctx *prev = NULL;
113a060b562SChristoph Hellwig 	u32 pages_per_mr = rdma_rw_fr_page_list_len(qp->pd->device);
114a060b562SChristoph Hellwig 	int i, j, ret = 0, count = 0;
115a060b562SChristoph Hellwig 
116a060b562SChristoph Hellwig 	ctx->nr_ops = (sg_cnt + pages_per_mr - 1) / pages_per_mr;
117a060b562SChristoph Hellwig 	ctx->reg = kcalloc(ctx->nr_ops, sizeof(*ctx->reg), GFP_KERNEL);
118a060b562SChristoph Hellwig 	if (!ctx->reg) {
119a060b562SChristoph Hellwig 		ret = -ENOMEM;
120a060b562SChristoph Hellwig 		goto out;
121a060b562SChristoph Hellwig 	}
122a060b562SChristoph Hellwig 
123a060b562SChristoph Hellwig 	for (i = 0; i < ctx->nr_ops; i++) {
124a060b562SChristoph Hellwig 		struct rdma_rw_reg_ctx *reg = &ctx->reg[i];
125a060b562SChristoph Hellwig 		u32 nents = min(sg_cnt, pages_per_mr);
126a060b562SChristoph Hellwig 
127a060b562SChristoph Hellwig 		ret = rdma_rw_init_one_mr(qp, port_num, reg, sg, sg_cnt,
128a060b562SChristoph Hellwig 				offset);
129a060b562SChristoph Hellwig 		if (ret < 0)
130a060b562SChristoph Hellwig 			goto out_free;
131a060b562SChristoph Hellwig 		count += ret;
132a060b562SChristoph Hellwig 
133a060b562SChristoph Hellwig 		if (prev) {
134a060b562SChristoph Hellwig 			if (reg->mr->need_inval)
135a060b562SChristoph Hellwig 				prev->wr.wr.next = &reg->inv_wr;
136a060b562SChristoph Hellwig 			else
137a060b562SChristoph Hellwig 				prev->wr.wr.next = &reg->reg_wr.wr;
138a060b562SChristoph Hellwig 		}
139a060b562SChristoph Hellwig 
140a060b562SChristoph Hellwig 		reg->reg_wr.wr.next = &reg->wr.wr;
141a060b562SChristoph Hellwig 
142a060b562SChristoph Hellwig 		reg->wr.wr.sg_list = &reg->sge;
143a060b562SChristoph Hellwig 		reg->wr.wr.num_sge = 1;
144a060b562SChristoph Hellwig 		reg->wr.remote_addr = remote_addr;
145a060b562SChristoph Hellwig 		reg->wr.rkey = rkey;
146a060b562SChristoph Hellwig 		if (dir == DMA_TO_DEVICE) {
147a060b562SChristoph Hellwig 			reg->wr.wr.opcode = IB_WR_RDMA_WRITE;
148a060b562SChristoph Hellwig 		} else if (!rdma_cap_read_inv(qp->device, port_num)) {
149a060b562SChristoph Hellwig 			reg->wr.wr.opcode = IB_WR_RDMA_READ;
150a060b562SChristoph Hellwig 		} else {
151a060b562SChristoph Hellwig 			reg->wr.wr.opcode = IB_WR_RDMA_READ_WITH_INV;
152a060b562SChristoph Hellwig 			reg->wr.wr.ex.invalidate_rkey = reg->mr->lkey;
153a060b562SChristoph Hellwig 		}
154a060b562SChristoph Hellwig 		count++;
155a060b562SChristoph Hellwig 
156a060b562SChristoph Hellwig 		remote_addr += reg->sge.length;
157a060b562SChristoph Hellwig 		sg_cnt -= nents;
158a060b562SChristoph Hellwig 		for (j = 0; j < nents; j++)
159a060b562SChristoph Hellwig 			sg = sg_next(sg);
160eaa74ec7SBart Van Assche 		prev = reg;
161a060b562SChristoph Hellwig 		offset = 0;
162a060b562SChristoph Hellwig 	}
163a060b562SChristoph Hellwig 
164eaa74ec7SBart Van Assche 	if (prev)
165eaa74ec7SBart Van Assche 		prev->wr.wr.next = NULL;
166eaa74ec7SBart Van Assche 
167a060b562SChristoph Hellwig 	ctx->type = RDMA_RW_MR;
168a060b562SChristoph Hellwig 	return count;
169a060b562SChristoph Hellwig 
170a060b562SChristoph Hellwig out_free:
171a060b562SChristoph Hellwig 	while (--i >= 0)
172a060b562SChristoph Hellwig 		ib_mr_pool_put(qp, &qp->rdma_mrs, ctx->reg[i].mr);
173a060b562SChristoph Hellwig 	kfree(ctx->reg);
174a060b562SChristoph Hellwig out:
175a060b562SChristoph Hellwig 	return ret;
176a060b562SChristoph Hellwig }
177a060b562SChristoph Hellwig 
178a060b562SChristoph Hellwig static int rdma_rw_init_map_wrs(struct rdma_rw_ctx *ctx, struct ib_qp *qp,
179a060b562SChristoph Hellwig 		struct scatterlist *sg, u32 sg_cnt, u32 offset,
180a060b562SChristoph Hellwig 		u64 remote_addr, u32 rkey, enum dma_data_direction dir)
181a060b562SChristoph Hellwig {
182632bc3f6SBart Van Assche 	u32 max_sge = dir == DMA_TO_DEVICE ? qp->max_write_sge :
183632bc3f6SBart Van Assche 		      qp->max_read_sge;
184a060b562SChristoph Hellwig 	struct ib_sge *sge;
185a060b562SChristoph Hellwig 	u32 total_len = 0, i, j;
186a060b562SChristoph Hellwig 
187a060b562SChristoph Hellwig 	ctx->nr_ops = DIV_ROUND_UP(sg_cnt, max_sge);
188a060b562SChristoph Hellwig 
189a060b562SChristoph Hellwig 	ctx->map.sges = sge = kcalloc(sg_cnt, sizeof(*sge), GFP_KERNEL);
190a060b562SChristoph Hellwig 	if (!ctx->map.sges)
191a060b562SChristoph Hellwig 		goto out;
192a060b562SChristoph Hellwig 
193a060b562SChristoph Hellwig 	ctx->map.wrs = kcalloc(ctx->nr_ops, sizeof(*ctx->map.wrs), GFP_KERNEL);
194a060b562SChristoph Hellwig 	if (!ctx->map.wrs)
195a060b562SChristoph Hellwig 		goto out_free_sges;
196a060b562SChristoph Hellwig 
197a060b562SChristoph Hellwig 	for (i = 0; i < ctx->nr_ops; i++) {
198a060b562SChristoph Hellwig 		struct ib_rdma_wr *rdma_wr = &ctx->map.wrs[i];
199a060b562SChristoph Hellwig 		u32 nr_sge = min(sg_cnt, max_sge);
200a060b562SChristoph Hellwig 
201a060b562SChristoph Hellwig 		if (dir == DMA_TO_DEVICE)
202a060b562SChristoph Hellwig 			rdma_wr->wr.opcode = IB_WR_RDMA_WRITE;
203a060b562SChristoph Hellwig 		else
204a060b562SChristoph Hellwig 			rdma_wr->wr.opcode = IB_WR_RDMA_READ;
205a060b562SChristoph Hellwig 		rdma_wr->remote_addr = remote_addr + total_len;
206a060b562SChristoph Hellwig 		rdma_wr->rkey = rkey;
207eaa74ec7SBart Van Assche 		rdma_wr->wr.num_sge = nr_sge;
208a060b562SChristoph Hellwig 		rdma_wr->wr.sg_list = sge;
209a060b562SChristoph Hellwig 
210a060b562SChristoph Hellwig 		for (j = 0; j < nr_sge; j++, sg = sg_next(sg)) {
211a163afc8SBart Van Assche 			sge->addr = sg_dma_address(sg) + offset;
212a163afc8SBart Van Assche 			sge->length = sg_dma_len(sg) - offset;
213a060b562SChristoph Hellwig 			sge->lkey = qp->pd->local_dma_lkey;
214a060b562SChristoph Hellwig 
215a060b562SChristoph Hellwig 			total_len += sge->length;
216a060b562SChristoph Hellwig 			sge++;
217a060b562SChristoph Hellwig 			sg_cnt--;
218a060b562SChristoph Hellwig 			offset = 0;
219a060b562SChristoph Hellwig 		}
220a060b562SChristoph Hellwig 
221eaa74ec7SBart Van Assche 		rdma_wr->wr.next = i + 1 < ctx->nr_ops ?
222eaa74ec7SBart Van Assche 			&ctx->map.wrs[i + 1].wr : NULL;
223a060b562SChristoph Hellwig 	}
224a060b562SChristoph Hellwig 
225a060b562SChristoph Hellwig 	ctx->type = RDMA_RW_MULTI_WR;
226a060b562SChristoph Hellwig 	return ctx->nr_ops;
227a060b562SChristoph Hellwig 
228a060b562SChristoph Hellwig out_free_sges:
229a060b562SChristoph Hellwig 	kfree(ctx->map.sges);
230a060b562SChristoph Hellwig out:
231a060b562SChristoph Hellwig 	return -ENOMEM;
232a060b562SChristoph Hellwig }
233a060b562SChristoph Hellwig 
234a060b562SChristoph Hellwig static int rdma_rw_init_single_wr(struct rdma_rw_ctx *ctx, struct ib_qp *qp,
235a060b562SChristoph Hellwig 		struct scatterlist *sg, u32 offset, u64 remote_addr, u32 rkey,
236a060b562SChristoph Hellwig 		enum dma_data_direction dir)
237a060b562SChristoph Hellwig {
238a060b562SChristoph Hellwig 	struct ib_rdma_wr *rdma_wr = &ctx->single.wr;
239a060b562SChristoph Hellwig 
240a060b562SChristoph Hellwig 	ctx->nr_ops = 1;
241a060b562SChristoph Hellwig 
242a060b562SChristoph Hellwig 	ctx->single.sge.lkey = qp->pd->local_dma_lkey;
243a163afc8SBart Van Assche 	ctx->single.sge.addr = sg_dma_address(sg) + offset;
244a163afc8SBart Van Assche 	ctx->single.sge.length = sg_dma_len(sg) - offset;
245a060b562SChristoph Hellwig 
246a060b562SChristoph Hellwig 	memset(rdma_wr, 0, sizeof(*rdma_wr));
247a060b562SChristoph Hellwig 	if (dir == DMA_TO_DEVICE)
248a060b562SChristoph Hellwig 		rdma_wr->wr.opcode = IB_WR_RDMA_WRITE;
249a060b562SChristoph Hellwig 	else
250a060b562SChristoph Hellwig 		rdma_wr->wr.opcode = IB_WR_RDMA_READ;
251a060b562SChristoph Hellwig 	rdma_wr->wr.sg_list = &ctx->single.sge;
252a060b562SChristoph Hellwig 	rdma_wr->wr.num_sge = 1;
253a060b562SChristoph Hellwig 	rdma_wr->remote_addr = remote_addr;
254a060b562SChristoph Hellwig 	rdma_wr->rkey = rkey;
255a060b562SChristoph Hellwig 
256a060b562SChristoph Hellwig 	ctx->type = RDMA_RW_SINGLE_WR;
257a060b562SChristoph Hellwig 	return 1;
258a060b562SChristoph Hellwig }
259a060b562SChristoph Hellwig 
260a060b562SChristoph Hellwig /**
261a060b562SChristoph Hellwig  * rdma_rw_ctx_init - initialize a RDMA READ/WRITE context
262a060b562SChristoph Hellwig  * @ctx:	context to initialize
263a060b562SChristoph Hellwig  * @qp:		queue pair to operate on
264a060b562SChristoph Hellwig  * @port_num:	port num to which the connection is bound
265a060b562SChristoph Hellwig  * @sg:		scatterlist to READ/WRITE from/to
266a060b562SChristoph Hellwig  * @sg_cnt:	number of entries in @sg
267a060b562SChristoph Hellwig  * @sg_offset:	current byte offset into @sg
268a060b562SChristoph Hellwig  * @remote_addr:remote address to read/write (relative to @rkey)
269a060b562SChristoph Hellwig  * @rkey:	remote key to operate on
270a060b562SChristoph Hellwig  * @dir:	%DMA_TO_DEVICE for RDMA WRITE, %DMA_FROM_DEVICE for RDMA READ
271a060b562SChristoph Hellwig  *
272a060b562SChristoph Hellwig  * Returns the number of WQEs that will be needed on the workqueue if
273a060b562SChristoph Hellwig  * successful, or a negative error code.
274a060b562SChristoph Hellwig  */
275a060b562SChristoph Hellwig int rdma_rw_ctx_init(struct rdma_rw_ctx *ctx, struct ib_qp *qp, u8 port_num,
276a060b562SChristoph Hellwig 		struct scatterlist *sg, u32 sg_cnt, u32 sg_offset,
277a060b562SChristoph Hellwig 		u64 remote_addr, u32 rkey, enum dma_data_direction dir)
278a060b562SChristoph Hellwig {
279a060b562SChristoph Hellwig 	struct ib_device *dev = qp->pd->device;
280a060b562SChristoph Hellwig 	int ret;
281a060b562SChristoph Hellwig 
28250b7d220SLogan Gunthorpe 	if (is_pci_p2pdma_page(sg_page(sg)))
28350b7d220SLogan Gunthorpe 		ret = pci_p2pdma_map_sg(dev->dma_device, sg, sg_cnt, dir);
28450b7d220SLogan Gunthorpe 	else
285a060b562SChristoph Hellwig 		ret = ib_dma_map_sg(dev, sg, sg_cnt, dir);
28650b7d220SLogan Gunthorpe 
287a060b562SChristoph Hellwig 	if (!ret)
288a060b562SChristoph Hellwig 		return -ENOMEM;
289a060b562SChristoph Hellwig 	sg_cnt = ret;
290a060b562SChristoph Hellwig 
291a060b562SChristoph Hellwig 	/*
292a060b562SChristoph Hellwig 	 * Skip to the S/G entry that sg_offset falls into:
293a060b562SChristoph Hellwig 	 */
294a060b562SChristoph Hellwig 	for (;;) {
295a163afc8SBart Van Assche 		u32 len = sg_dma_len(sg);
296a060b562SChristoph Hellwig 
297a060b562SChristoph Hellwig 		if (sg_offset < len)
298a060b562SChristoph Hellwig 			break;
299a060b562SChristoph Hellwig 
300a060b562SChristoph Hellwig 		sg = sg_next(sg);
301a060b562SChristoph Hellwig 		sg_offset -= len;
302a060b562SChristoph Hellwig 		sg_cnt--;
303a060b562SChristoph Hellwig 	}
304a060b562SChristoph Hellwig 
305a060b562SChristoph Hellwig 	ret = -EIO;
306a060b562SChristoph Hellwig 	if (WARN_ON_ONCE(sg_cnt == 0))
307a060b562SChristoph Hellwig 		goto out_unmap_sg;
308a060b562SChristoph Hellwig 
309a060b562SChristoph Hellwig 	if (rdma_rw_io_needs_mr(qp->device, port_num, dir, sg_cnt)) {
310a060b562SChristoph Hellwig 		ret = rdma_rw_init_mr_wrs(ctx, qp, port_num, sg, sg_cnt,
311a060b562SChristoph Hellwig 				sg_offset, remote_addr, rkey, dir);
312a060b562SChristoph Hellwig 	} else if (sg_cnt > 1) {
313a060b562SChristoph Hellwig 		ret = rdma_rw_init_map_wrs(ctx, qp, sg, sg_cnt, sg_offset,
314a060b562SChristoph Hellwig 				remote_addr, rkey, dir);
315a060b562SChristoph Hellwig 	} else {
316a060b562SChristoph Hellwig 		ret = rdma_rw_init_single_wr(ctx, qp, sg, sg_offset,
317a060b562SChristoph Hellwig 				remote_addr, rkey, dir);
318a060b562SChristoph Hellwig 	}
319a060b562SChristoph Hellwig 
320a060b562SChristoph Hellwig 	if (ret < 0)
321a060b562SChristoph Hellwig 		goto out_unmap_sg;
322a060b562SChristoph Hellwig 	return ret;
323a060b562SChristoph Hellwig 
324a060b562SChristoph Hellwig out_unmap_sg:
325a060b562SChristoph Hellwig 	ib_dma_unmap_sg(dev, sg, sg_cnt, dir);
326a060b562SChristoph Hellwig 	return ret;
327a060b562SChristoph Hellwig }
328a060b562SChristoph Hellwig EXPORT_SYMBOL(rdma_rw_ctx_init);
329a060b562SChristoph Hellwig 
3300e353e34SChristoph Hellwig /**
331222c7b1fSBart Van Assche  * rdma_rw_ctx_signature_init - initialize a RW context with signature offload
3320e353e34SChristoph Hellwig  * @ctx:	context to initialize
3330e353e34SChristoph Hellwig  * @qp:		queue pair to operate on
3340e353e34SChristoph Hellwig  * @port_num:	port num to which the connection is bound
3350e353e34SChristoph Hellwig  * @sg:		scatterlist to READ/WRITE from/to
3360e353e34SChristoph Hellwig  * @sg_cnt:	number of entries in @sg
3370e353e34SChristoph Hellwig  * @prot_sg:	scatterlist to READ/WRITE protection information from/to
3380e353e34SChristoph Hellwig  * @prot_sg_cnt: number of entries in @prot_sg
3390e353e34SChristoph Hellwig  * @sig_attrs:	signature offloading algorithms
3400e353e34SChristoph Hellwig  * @remote_addr:remote address to read/write (relative to @rkey)
3410e353e34SChristoph Hellwig  * @rkey:	remote key to operate on
3420e353e34SChristoph Hellwig  * @dir:	%DMA_TO_DEVICE for RDMA WRITE, %DMA_FROM_DEVICE for RDMA READ
3430e353e34SChristoph Hellwig  *
3440e353e34SChristoph Hellwig  * Returns the number of WQEs that will be needed on the workqueue if
3450e353e34SChristoph Hellwig  * successful, or a negative error code.
3460e353e34SChristoph Hellwig  */
3470e353e34SChristoph Hellwig int rdma_rw_ctx_signature_init(struct rdma_rw_ctx *ctx, struct ib_qp *qp,
3480e353e34SChristoph Hellwig 		u8 port_num, struct scatterlist *sg, u32 sg_cnt,
3490e353e34SChristoph Hellwig 		struct scatterlist *prot_sg, u32 prot_sg_cnt,
3500e353e34SChristoph Hellwig 		struct ib_sig_attrs *sig_attrs,
3510e353e34SChristoph Hellwig 		u64 remote_addr, u32 rkey, enum dma_data_direction dir)
3520e353e34SChristoph Hellwig {
3530e353e34SChristoph Hellwig 	struct ib_device *dev = qp->pd->device;
3540e353e34SChristoph Hellwig 	u32 pages_per_mr = rdma_rw_fr_page_list_len(qp->pd->device);
3550e353e34SChristoph Hellwig 	struct ib_rdma_wr *rdma_wr;
3560e353e34SChristoph Hellwig 	struct ib_send_wr *prev_wr = NULL;
3570e353e34SChristoph Hellwig 	int count = 0, ret;
3580e353e34SChristoph Hellwig 
3590e353e34SChristoph Hellwig 	if (sg_cnt > pages_per_mr || prot_sg_cnt > pages_per_mr) {
3600e353e34SChristoph Hellwig 		pr_err("SG count too large\n");
3610e353e34SChristoph Hellwig 		return -EINVAL;
3620e353e34SChristoph Hellwig 	}
3630e353e34SChristoph Hellwig 
3640e353e34SChristoph Hellwig 	ret = ib_dma_map_sg(dev, sg, sg_cnt, dir);
3650e353e34SChristoph Hellwig 	if (!ret)
3660e353e34SChristoph Hellwig 		return -ENOMEM;
3670e353e34SChristoph Hellwig 	sg_cnt = ret;
3680e353e34SChristoph Hellwig 
3690e353e34SChristoph Hellwig 	ret = ib_dma_map_sg(dev, prot_sg, prot_sg_cnt, dir);
3700e353e34SChristoph Hellwig 	if (!ret) {
3710e353e34SChristoph Hellwig 		ret = -ENOMEM;
3720e353e34SChristoph Hellwig 		goto out_unmap_sg;
3730e353e34SChristoph Hellwig 	}
3740e353e34SChristoph Hellwig 	prot_sg_cnt = ret;
3750e353e34SChristoph Hellwig 
3760e353e34SChristoph Hellwig 	ctx->type = RDMA_RW_SIG_MR;
3770e353e34SChristoph Hellwig 	ctx->nr_ops = 1;
3780e353e34SChristoph Hellwig 	ctx->sig = kcalloc(1, sizeof(*ctx->sig), GFP_KERNEL);
3790e353e34SChristoph Hellwig 	if (!ctx->sig) {
3800e353e34SChristoph Hellwig 		ret = -ENOMEM;
3810e353e34SChristoph Hellwig 		goto out_unmap_prot_sg;
3820e353e34SChristoph Hellwig 	}
3830e353e34SChristoph Hellwig 
3840e353e34SChristoph Hellwig 	ret = rdma_rw_init_one_mr(qp, port_num, &ctx->sig->data, sg, sg_cnt, 0);
3850e353e34SChristoph Hellwig 	if (ret < 0)
3860e353e34SChristoph Hellwig 		goto out_free_ctx;
3870e353e34SChristoph Hellwig 	count += ret;
3880e353e34SChristoph Hellwig 	prev_wr = &ctx->sig->data.reg_wr.wr;
3890e353e34SChristoph Hellwig 
3900e353e34SChristoph Hellwig 	ret = rdma_rw_init_one_mr(qp, port_num, &ctx->sig->prot,
3910e353e34SChristoph Hellwig 				  prot_sg, prot_sg_cnt, 0);
3920e353e34SChristoph Hellwig 	if (ret < 0)
3930e353e34SChristoph Hellwig 		goto out_destroy_data_mr;
3940e353e34SChristoph Hellwig 	count += ret;
3950e353e34SChristoph Hellwig 
3960e353e34SChristoph Hellwig 	if (ctx->sig->prot.inv_wr.next)
3970e353e34SChristoph Hellwig 		prev_wr->next = &ctx->sig->prot.inv_wr;
3980e353e34SChristoph Hellwig 	else
3990e353e34SChristoph Hellwig 		prev_wr->next = &ctx->sig->prot.reg_wr.wr;
4000e353e34SChristoph Hellwig 	prev_wr = &ctx->sig->prot.reg_wr.wr;
4010e353e34SChristoph Hellwig 
4020e353e34SChristoph Hellwig 	ctx->sig->sig_mr = ib_mr_pool_get(qp, &qp->sig_mrs);
4030e353e34SChristoph Hellwig 	if (!ctx->sig->sig_mr) {
4040e353e34SChristoph Hellwig 		ret = -EAGAIN;
4050e353e34SChristoph Hellwig 		goto out_destroy_prot_mr;
4060e353e34SChristoph Hellwig 	}
4070e353e34SChristoph Hellwig 
4080e353e34SChristoph Hellwig 	if (ctx->sig->sig_mr->need_inval) {
4090e353e34SChristoph Hellwig 		memset(&ctx->sig->sig_inv_wr, 0, sizeof(ctx->sig->sig_inv_wr));
4100e353e34SChristoph Hellwig 
4110e353e34SChristoph Hellwig 		ctx->sig->sig_inv_wr.opcode = IB_WR_LOCAL_INV;
4120e353e34SChristoph Hellwig 		ctx->sig->sig_inv_wr.ex.invalidate_rkey = ctx->sig->sig_mr->rkey;
4130e353e34SChristoph Hellwig 
4140e353e34SChristoph Hellwig 		prev_wr->next = &ctx->sig->sig_inv_wr;
4150e353e34SChristoph Hellwig 		prev_wr = &ctx->sig->sig_inv_wr;
4160e353e34SChristoph Hellwig 	}
4170e353e34SChristoph Hellwig 
4180e353e34SChristoph Hellwig 	ctx->sig->sig_wr.wr.opcode = IB_WR_REG_SIG_MR;
4190e353e34SChristoph Hellwig 	ctx->sig->sig_wr.wr.wr_cqe = NULL;
4200e353e34SChristoph Hellwig 	ctx->sig->sig_wr.wr.sg_list = &ctx->sig->data.sge;
4210e353e34SChristoph Hellwig 	ctx->sig->sig_wr.wr.num_sge = 1;
4220e353e34SChristoph Hellwig 	ctx->sig->sig_wr.access_flags = IB_ACCESS_LOCAL_WRITE;
4230e353e34SChristoph Hellwig 	ctx->sig->sig_wr.sig_attrs = sig_attrs;
4240e353e34SChristoph Hellwig 	ctx->sig->sig_wr.sig_mr = ctx->sig->sig_mr;
4250e353e34SChristoph Hellwig 	if (prot_sg_cnt)
4260e353e34SChristoph Hellwig 		ctx->sig->sig_wr.prot = &ctx->sig->prot.sge;
4270e353e34SChristoph Hellwig 	prev_wr->next = &ctx->sig->sig_wr.wr;
4280e353e34SChristoph Hellwig 	prev_wr = &ctx->sig->sig_wr.wr;
4290e353e34SChristoph Hellwig 	count++;
4300e353e34SChristoph Hellwig 
4310e353e34SChristoph Hellwig 	ctx->sig->sig_sge.addr = 0;
4320e353e34SChristoph Hellwig 	ctx->sig->sig_sge.length = ctx->sig->data.sge.length;
4330e353e34SChristoph Hellwig 	if (sig_attrs->wire.sig_type != IB_SIG_TYPE_NONE)
4340e353e34SChristoph Hellwig 		ctx->sig->sig_sge.length += ctx->sig->prot.sge.length;
4350e353e34SChristoph Hellwig 
4360e353e34SChristoph Hellwig 	rdma_wr = &ctx->sig->data.wr;
4370e353e34SChristoph Hellwig 	rdma_wr->wr.sg_list = &ctx->sig->sig_sge;
4380e353e34SChristoph Hellwig 	rdma_wr->wr.num_sge = 1;
4390e353e34SChristoph Hellwig 	rdma_wr->remote_addr = remote_addr;
4400e353e34SChristoph Hellwig 	rdma_wr->rkey = rkey;
4410e353e34SChristoph Hellwig 	if (dir == DMA_TO_DEVICE)
4420e353e34SChristoph Hellwig 		rdma_wr->wr.opcode = IB_WR_RDMA_WRITE;
4430e353e34SChristoph Hellwig 	else
4440e353e34SChristoph Hellwig 		rdma_wr->wr.opcode = IB_WR_RDMA_READ;
4450e353e34SChristoph Hellwig 	prev_wr->next = &rdma_wr->wr;
4460e353e34SChristoph Hellwig 	prev_wr = &rdma_wr->wr;
4470e353e34SChristoph Hellwig 	count++;
4480e353e34SChristoph Hellwig 
4490e353e34SChristoph Hellwig 	return count;
4500e353e34SChristoph Hellwig 
4510e353e34SChristoph Hellwig out_destroy_prot_mr:
4520e353e34SChristoph Hellwig 	if (prot_sg_cnt)
4530e353e34SChristoph Hellwig 		ib_mr_pool_put(qp, &qp->rdma_mrs, ctx->sig->prot.mr);
4540e353e34SChristoph Hellwig out_destroy_data_mr:
4550e353e34SChristoph Hellwig 	ib_mr_pool_put(qp, &qp->rdma_mrs, ctx->sig->data.mr);
4560e353e34SChristoph Hellwig out_free_ctx:
4570e353e34SChristoph Hellwig 	kfree(ctx->sig);
4580e353e34SChristoph Hellwig out_unmap_prot_sg:
4590e353e34SChristoph Hellwig 	ib_dma_unmap_sg(dev, prot_sg, prot_sg_cnt, dir);
4600e353e34SChristoph Hellwig out_unmap_sg:
4610e353e34SChristoph Hellwig 	ib_dma_unmap_sg(dev, sg, sg_cnt, dir);
4620e353e34SChristoph Hellwig 	return ret;
4630e353e34SChristoph Hellwig }
4640e353e34SChristoph Hellwig EXPORT_SYMBOL(rdma_rw_ctx_signature_init);
4650e353e34SChristoph Hellwig 
466a060b562SChristoph Hellwig /*
467a060b562SChristoph Hellwig  * Now that we are going to post the WRs we can update the lkey and need_inval
468a060b562SChristoph Hellwig  * state on the MRs.  If we were doing this at init time, we would get double
469a060b562SChristoph Hellwig  * or missing invalidations if a context was initialized but not actually
470a060b562SChristoph Hellwig  * posted.
471a060b562SChristoph Hellwig  */
472a060b562SChristoph Hellwig static void rdma_rw_update_lkey(struct rdma_rw_reg_ctx *reg, bool need_inval)
473a060b562SChristoph Hellwig {
474a060b562SChristoph Hellwig 	reg->mr->need_inval = need_inval;
475a060b562SChristoph Hellwig 	ib_update_fast_reg_key(reg->mr, ib_inc_rkey(reg->mr->lkey));
476a060b562SChristoph Hellwig 	reg->reg_wr.key = reg->mr->lkey;
477a060b562SChristoph Hellwig 	reg->sge.lkey = reg->mr->lkey;
478a060b562SChristoph Hellwig }
479a060b562SChristoph Hellwig 
480a060b562SChristoph Hellwig /**
481a060b562SChristoph Hellwig  * rdma_rw_ctx_wrs - return chain of WRs for a RDMA READ or WRITE operation
482a060b562SChristoph Hellwig  * @ctx:	context to operate on
483a060b562SChristoph Hellwig  * @qp:		queue pair to operate on
484a060b562SChristoph Hellwig  * @port_num:	port num to which the connection is bound
485a060b562SChristoph Hellwig  * @cqe:	completion queue entry for the last WR
486a060b562SChristoph Hellwig  * @chain_wr:	WR to append to the posted chain
487a060b562SChristoph Hellwig  *
488a060b562SChristoph Hellwig  * Return the WR chain for the set of RDMA READ/WRITE operations described by
489a060b562SChristoph Hellwig  * @ctx, as well as any memory registration operations needed.  If @chain_wr
490a060b562SChristoph Hellwig  * is non-NULL the WR it points to will be appended to the chain of WRs posted.
491a060b562SChristoph Hellwig  * If @chain_wr is not set @cqe must be set so that the caller gets a
492a060b562SChristoph Hellwig  * completion notification.
493a060b562SChristoph Hellwig  */
494a060b562SChristoph Hellwig struct ib_send_wr *rdma_rw_ctx_wrs(struct rdma_rw_ctx *ctx, struct ib_qp *qp,
495a060b562SChristoph Hellwig 		u8 port_num, struct ib_cqe *cqe, struct ib_send_wr *chain_wr)
496a060b562SChristoph Hellwig {
497a060b562SChristoph Hellwig 	struct ib_send_wr *first_wr, *last_wr;
498a060b562SChristoph Hellwig 	int i;
499a060b562SChristoph Hellwig 
500a060b562SChristoph Hellwig 	switch (ctx->type) {
5010e353e34SChristoph Hellwig 	case RDMA_RW_SIG_MR:
5020e353e34SChristoph Hellwig 		rdma_rw_update_lkey(&ctx->sig->data, true);
5030e353e34SChristoph Hellwig 		if (ctx->sig->prot.mr)
5040e353e34SChristoph Hellwig 			rdma_rw_update_lkey(&ctx->sig->prot, true);
5050e353e34SChristoph Hellwig 
5060e353e34SChristoph Hellwig 		ctx->sig->sig_mr->need_inval = true;
5070e353e34SChristoph Hellwig 		ib_update_fast_reg_key(ctx->sig->sig_mr,
5080e353e34SChristoph Hellwig 			ib_inc_rkey(ctx->sig->sig_mr->lkey));
5090e353e34SChristoph Hellwig 		ctx->sig->sig_sge.lkey = ctx->sig->sig_mr->lkey;
5100e353e34SChristoph Hellwig 
5110e353e34SChristoph Hellwig 		if (ctx->sig->data.inv_wr.next)
5120e353e34SChristoph Hellwig 			first_wr = &ctx->sig->data.inv_wr;
5130e353e34SChristoph Hellwig 		else
5140e353e34SChristoph Hellwig 			first_wr = &ctx->sig->data.reg_wr.wr;
5150e353e34SChristoph Hellwig 		last_wr = &ctx->sig->data.wr.wr;
5160e353e34SChristoph Hellwig 		break;
517a060b562SChristoph Hellwig 	case RDMA_RW_MR:
518a060b562SChristoph Hellwig 		for (i = 0; i < ctx->nr_ops; i++) {
519a060b562SChristoph Hellwig 			rdma_rw_update_lkey(&ctx->reg[i],
520a060b562SChristoph Hellwig 				ctx->reg[i].wr.wr.opcode !=
521a060b562SChristoph Hellwig 					IB_WR_RDMA_READ_WITH_INV);
522a060b562SChristoph Hellwig 		}
523a060b562SChristoph Hellwig 
524a060b562SChristoph Hellwig 		if (ctx->reg[0].inv_wr.next)
525a060b562SChristoph Hellwig 			first_wr = &ctx->reg[0].inv_wr;
526a060b562SChristoph Hellwig 		else
527a060b562SChristoph Hellwig 			first_wr = &ctx->reg[0].reg_wr.wr;
528a060b562SChristoph Hellwig 		last_wr = &ctx->reg[ctx->nr_ops - 1].wr.wr;
529a060b562SChristoph Hellwig 		break;
530a060b562SChristoph Hellwig 	case RDMA_RW_MULTI_WR:
531a060b562SChristoph Hellwig 		first_wr = &ctx->map.wrs[0].wr;
532a060b562SChristoph Hellwig 		last_wr = &ctx->map.wrs[ctx->nr_ops - 1].wr;
533a060b562SChristoph Hellwig 		break;
534a060b562SChristoph Hellwig 	case RDMA_RW_SINGLE_WR:
535a060b562SChristoph Hellwig 		first_wr = &ctx->single.wr.wr;
536a060b562SChristoph Hellwig 		last_wr = &ctx->single.wr.wr;
537a060b562SChristoph Hellwig 		break;
538a060b562SChristoph Hellwig 	default:
539a060b562SChristoph Hellwig 		BUG();
540a060b562SChristoph Hellwig 	}
541a060b562SChristoph Hellwig 
542a060b562SChristoph Hellwig 	if (chain_wr) {
543a060b562SChristoph Hellwig 		last_wr->next = chain_wr;
544a060b562SChristoph Hellwig 	} else {
545a060b562SChristoph Hellwig 		last_wr->wr_cqe = cqe;
546a060b562SChristoph Hellwig 		last_wr->send_flags |= IB_SEND_SIGNALED;
547a060b562SChristoph Hellwig 	}
548a060b562SChristoph Hellwig 
549a060b562SChristoph Hellwig 	return first_wr;
550a060b562SChristoph Hellwig }
551a060b562SChristoph Hellwig EXPORT_SYMBOL(rdma_rw_ctx_wrs);
552a060b562SChristoph Hellwig 
553a060b562SChristoph Hellwig /**
554a060b562SChristoph Hellwig  * rdma_rw_ctx_post - post a RDMA READ or RDMA WRITE operation
555a060b562SChristoph Hellwig  * @ctx:	context to operate on
556a060b562SChristoph Hellwig  * @qp:		queue pair to operate on
557a060b562SChristoph Hellwig  * @port_num:	port num to which the connection is bound
558a060b562SChristoph Hellwig  * @cqe:	completion queue entry for the last WR
559a060b562SChristoph Hellwig  * @chain_wr:	WR to append to the posted chain
560a060b562SChristoph Hellwig  *
561a060b562SChristoph Hellwig  * Post the set of RDMA READ/WRITE operations described by @ctx, as well as
562a060b562SChristoph Hellwig  * any memory registration operations needed.  If @chain_wr is non-NULL the
563a060b562SChristoph Hellwig  * WR it points to will be appended to the chain of WRs posted.  If @chain_wr
564a060b562SChristoph Hellwig  * is not set @cqe must be set so that the caller gets a completion
565a060b562SChristoph Hellwig  * notification.
566a060b562SChristoph Hellwig  */
567a060b562SChristoph Hellwig int rdma_rw_ctx_post(struct rdma_rw_ctx *ctx, struct ib_qp *qp, u8 port_num,
568a060b562SChristoph Hellwig 		struct ib_cqe *cqe, struct ib_send_wr *chain_wr)
569a060b562SChristoph Hellwig {
5701fec77bfSBart Van Assche 	struct ib_send_wr *first_wr;
571a060b562SChristoph Hellwig 
572a060b562SChristoph Hellwig 	first_wr = rdma_rw_ctx_wrs(ctx, qp, port_num, cqe, chain_wr);
5731fec77bfSBart Van Assche 	return ib_post_send(qp, first_wr, NULL);
574a060b562SChristoph Hellwig }
575a060b562SChristoph Hellwig EXPORT_SYMBOL(rdma_rw_ctx_post);
576a060b562SChristoph Hellwig 
577a060b562SChristoph Hellwig /**
578a060b562SChristoph Hellwig  * rdma_rw_ctx_destroy - release all resources allocated by rdma_rw_ctx_init
579a060b562SChristoph Hellwig  * @ctx:	context to release
580a060b562SChristoph Hellwig  * @qp:		queue pair to operate on
581a060b562SChristoph Hellwig  * @port_num:	port num to which the connection is bound
582a060b562SChristoph Hellwig  * @sg:		scatterlist that was used for the READ/WRITE
583a060b562SChristoph Hellwig  * @sg_cnt:	number of entries in @sg
584a060b562SChristoph Hellwig  * @dir:	%DMA_TO_DEVICE for RDMA WRITE, %DMA_FROM_DEVICE for RDMA READ
585a060b562SChristoph Hellwig  */
586a060b562SChristoph Hellwig void rdma_rw_ctx_destroy(struct rdma_rw_ctx *ctx, struct ib_qp *qp, u8 port_num,
587a060b562SChristoph Hellwig 		struct scatterlist *sg, u32 sg_cnt, enum dma_data_direction dir)
588a060b562SChristoph Hellwig {
589a060b562SChristoph Hellwig 	int i;
590a060b562SChristoph Hellwig 
591a060b562SChristoph Hellwig 	switch (ctx->type) {
592a060b562SChristoph Hellwig 	case RDMA_RW_MR:
593a060b562SChristoph Hellwig 		for (i = 0; i < ctx->nr_ops; i++)
594a060b562SChristoph Hellwig 			ib_mr_pool_put(qp, &qp->rdma_mrs, ctx->reg[i].mr);
595a060b562SChristoph Hellwig 		kfree(ctx->reg);
596a060b562SChristoph Hellwig 		break;
597a060b562SChristoph Hellwig 	case RDMA_RW_MULTI_WR:
598a060b562SChristoph Hellwig 		kfree(ctx->map.wrs);
599a060b562SChristoph Hellwig 		kfree(ctx->map.sges);
600a060b562SChristoph Hellwig 		break;
601a060b562SChristoph Hellwig 	case RDMA_RW_SINGLE_WR:
602a060b562SChristoph Hellwig 		break;
603a060b562SChristoph Hellwig 	default:
604a060b562SChristoph Hellwig 		BUG();
605a060b562SChristoph Hellwig 		break;
606a060b562SChristoph Hellwig 	}
607a060b562SChristoph Hellwig 
60850b7d220SLogan Gunthorpe 	/* P2PDMA contexts do not need to be unmapped */
60950b7d220SLogan Gunthorpe 	if (!is_pci_p2pdma_page(sg_page(sg)))
610a060b562SChristoph Hellwig 		ib_dma_unmap_sg(qp->pd->device, sg, sg_cnt, dir);
611a060b562SChristoph Hellwig }
612a060b562SChristoph Hellwig EXPORT_SYMBOL(rdma_rw_ctx_destroy);
613a060b562SChristoph Hellwig 
6140e353e34SChristoph Hellwig /**
6150e353e34SChristoph Hellwig  * rdma_rw_ctx_destroy_signature - release all resources allocated by
6160e353e34SChristoph Hellwig  *	rdma_rw_ctx_init_signature
6170e353e34SChristoph Hellwig  * @ctx:	context to release
6180e353e34SChristoph Hellwig  * @qp:		queue pair to operate on
6190e353e34SChristoph Hellwig  * @port_num:	port num to which the connection is bound
6200e353e34SChristoph Hellwig  * @sg:		scatterlist that was used for the READ/WRITE
6210e353e34SChristoph Hellwig  * @sg_cnt:	number of entries in @sg
6220e353e34SChristoph Hellwig  * @prot_sg:	scatterlist that was used for the READ/WRITE of the PI
6230e353e34SChristoph Hellwig  * @prot_sg_cnt: number of entries in @prot_sg
6240e353e34SChristoph Hellwig  * @dir:	%DMA_TO_DEVICE for RDMA WRITE, %DMA_FROM_DEVICE for RDMA READ
6250e353e34SChristoph Hellwig  */
6260e353e34SChristoph Hellwig void rdma_rw_ctx_destroy_signature(struct rdma_rw_ctx *ctx, struct ib_qp *qp,
6270e353e34SChristoph Hellwig 		u8 port_num, struct scatterlist *sg, u32 sg_cnt,
6280e353e34SChristoph Hellwig 		struct scatterlist *prot_sg, u32 prot_sg_cnt,
6290e353e34SChristoph Hellwig 		enum dma_data_direction dir)
6300e353e34SChristoph Hellwig {
6310e353e34SChristoph Hellwig 	if (WARN_ON_ONCE(ctx->type != RDMA_RW_SIG_MR))
6320e353e34SChristoph Hellwig 		return;
6330e353e34SChristoph Hellwig 
6340e353e34SChristoph Hellwig 	ib_mr_pool_put(qp, &qp->rdma_mrs, ctx->sig->data.mr);
6350e353e34SChristoph Hellwig 	ib_dma_unmap_sg(qp->pd->device, sg, sg_cnt, dir);
6360e353e34SChristoph Hellwig 
6370e353e34SChristoph Hellwig 	if (ctx->sig->prot.mr) {
6380e353e34SChristoph Hellwig 		ib_mr_pool_put(qp, &qp->rdma_mrs, ctx->sig->prot.mr);
6390e353e34SChristoph Hellwig 		ib_dma_unmap_sg(qp->pd->device, prot_sg, prot_sg_cnt, dir);
6400e353e34SChristoph Hellwig 	}
6410e353e34SChristoph Hellwig 
6420e353e34SChristoph Hellwig 	ib_mr_pool_put(qp, &qp->sig_mrs, ctx->sig->sig_mr);
6430e353e34SChristoph Hellwig 	kfree(ctx->sig);
6440e353e34SChristoph Hellwig }
6450e353e34SChristoph Hellwig EXPORT_SYMBOL(rdma_rw_ctx_destroy_signature);
6460e353e34SChristoph Hellwig 
64700628182SChuck Lever /**
64800628182SChuck Lever  * rdma_rw_mr_factor - return number of MRs required for a payload
64900628182SChuck Lever  * @device:	device handling the connection
65000628182SChuck Lever  * @port_num:	port num to which the connection is bound
65100628182SChuck Lever  * @maxpages:	maximum payload pages per rdma_rw_ctx
65200628182SChuck Lever  *
65300628182SChuck Lever  * Returns the number of MRs the device requires to move @maxpayload
65400628182SChuck Lever  * bytes. The returned value is used during transport creation to
65500628182SChuck Lever  * compute max_rdma_ctxts and the size of the transport's Send and
65600628182SChuck Lever  * Send Completion Queues.
65700628182SChuck Lever  */
65800628182SChuck Lever unsigned int rdma_rw_mr_factor(struct ib_device *device, u8 port_num,
65900628182SChuck Lever 			       unsigned int maxpages)
66000628182SChuck Lever {
66100628182SChuck Lever 	unsigned int mr_pages;
66200628182SChuck Lever 
66300628182SChuck Lever 	if (rdma_rw_can_use_mr(device, port_num))
66400628182SChuck Lever 		mr_pages = rdma_rw_fr_page_list_len(device);
66500628182SChuck Lever 	else
66600628182SChuck Lever 		mr_pages = device->attrs.max_sge_rd;
66700628182SChuck Lever 	return DIV_ROUND_UP(maxpages, mr_pages);
66800628182SChuck Lever }
66900628182SChuck Lever EXPORT_SYMBOL(rdma_rw_mr_factor);
67000628182SChuck Lever 
671a060b562SChristoph Hellwig void rdma_rw_init_qp(struct ib_device *dev, struct ib_qp_init_attr *attr)
672a060b562SChristoph Hellwig {
673a060b562SChristoph Hellwig 	u32 factor;
674a060b562SChristoph Hellwig 
675a060b562SChristoph Hellwig 	WARN_ON_ONCE(attr->port_num == 0);
676a060b562SChristoph Hellwig 
677a060b562SChristoph Hellwig 	/*
678a060b562SChristoph Hellwig 	 * Each context needs at least one RDMA READ or WRITE WR.
679a060b562SChristoph Hellwig 	 *
680a060b562SChristoph Hellwig 	 * For some hardware we might need more, eventually we should ask the
681a060b562SChristoph Hellwig 	 * HCA driver for a multiplier here.
682a060b562SChristoph Hellwig 	 */
683a060b562SChristoph Hellwig 	factor = 1;
684a060b562SChristoph Hellwig 
685a060b562SChristoph Hellwig 	/*
686a060b562SChristoph Hellwig 	 * If the devices needs MRs to perform RDMA READ or WRITE operations,
687a060b562SChristoph Hellwig 	 * we'll need two additional MRs for the registrations and the
688a060b562SChristoph Hellwig 	 * invalidation.
689a060b562SChristoph Hellwig 	 */
6900e353e34SChristoph Hellwig 	if (attr->create_flags & IB_QP_CREATE_SIGNATURE_EN)
6910e353e34SChristoph Hellwig 		factor += 6;	/* (inv + reg) * (data + prot + sig) */
6920e353e34SChristoph Hellwig 	else if (rdma_rw_can_use_mr(dev, attr->port_num))
693a060b562SChristoph Hellwig 		factor += 2;	/* inv + reg */
694a060b562SChristoph Hellwig 
695a060b562SChristoph Hellwig 	attr->cap.max_send_wr += factor * attr->cap.max_rdma_ctxs;
696a060b562SChristoph Hellwig 
697a060b562SChristoph Hellwig 	/*
698a060b562SChristoph Hellwig 	 * But maybe we were just too high in the sky and the device doesn't
699a060b562SChristoph Hellwig 	 * even support all we need, and we'll have to live with what we get..
700a060b562SChristoph Hellwig 	 */
701a060b562SChristoph Hellwig 	attr->cap.max_send_wr =
702a060b562SChristoph Hellwig 		min_t(u32, attr->cap.max_send_wr, dev->attrs.max_qp_wr);
703a060b562SChristoph Hellwig }
704a060b562SChristoph Hellwig 
705a060b562SChristoph Hellwig int rdma_rw_init_mrs(struct ib_qp *qp, struct ib_qp_init_attr *attr)
706a060b562SChristoph Hellwig {
707a060b562SChristoph Hellwig 	struct ib_device *dev = qp->pd->device;
7080e353e34SChristoph Hellwig 	u32 nr_mrs = 0, nr_sig_mrs = 0;
709a060b562SChristoph Hellwig 	int ret = 0;
710a060b562SChristoph Hellwig 
7110e353e34SChristoph Hellwig 	if (attr->create_flags & IB_QP_CREATE_SIGNATURE_EN) {
7120e353e34SChristoph Hellwig 		nr_sig_mrs = attr->cap.max_rdma_ctxs;
7130e353e34SChristoph Hellwig 		nr_mrs = attr->cap.max_rdma_ctxs * 2;
7140e353e34SChristoph Hellwig 	} else if (rdma_rw_can_use_mr(dev, attr->port_num)) {
7150e353e34SChristoph Hellwig 		nr_mrs = attr->cap.max_rdma_ctxs;
716a060b562SChristoph Hellwig 	}
717a060b562SChristoph Hellwig 
7180e353e34SChristoph Hellwig 	if (nr_mrs) {
7190e353e34SChristoph Hellwig 		ret = ib_mr_pool_init(qp, &qp->rdma_mrs, nr_mrs,
7200e353e34SChristoph Hellwig 				IB_MR_TYPE_MEM_REG,
7210e353e34SChristoph Hellwig 				rdma_rw_fr_page_list_len(dev));
7220e353e34SChristoph Hellwig 		if (ret) {
7230e353e34SChristoph Hellwig 			pr_err("%s: failed to allocated %d MRs\n",
7240e353e34SChristoph Hellwig 				__func__, nr_mrs);
7250e353e34SChristoph Hellwig 			return ret;
7260e353e34SChristoph Hellwig 		}
7270e353e34SChristoph Hellwig 	}
7280e353e34SChristoph Hellwig 
7290e353e34SChristoph Hellwig 	if (nr_sig_mrs) {
7300e353e34SChristoph Hellwig 		ret = ib_mr_pool_init(qp, &qp->sig_mrs, nr_sig_mrs,
7310e353e34SChristoph Hellwig 				IB_MR_TYPE_SIGNATURE, 2);
7320e353e34SChristoph Hellwig 		if (ret) {
7330e353e34SChristoph Hellwig 			pr_err("%s: failed to allocated %d SIG MRs\n",
7340e353e34SChristoph Hellwig 				__func__, nr_mrs);
7350e353e34SChristoph Hellwig 			goto out_free_rdma_mrs;
7360e353e34SChristoph Hellwig 		}
7370e353e34SChristoph Hellwig 	}
7380e353e34SChristoph Hellwig 
7390e353e34SChristoph Hellwig 	return 0;
7400e353e34SChristoph Hellwig 
7410e353e34SChristoph Hellwig out_free_rdma_mrs:
7420e353e34SChristoph Hellwig 	ib_mr_pool_destroy(qp, &qp->rdma_mrs);
743a060b562SChristoph Hellwig 	return ret;
744a060b562SChristoph Hellwig }
745a060b562SChristoph Hellwig 
746a060b562SChristoph Hellwig void rdma_rw_cleanup_mrs(struct ib_qp *qp)
747a060b562SChristoph Hellwig {
7480e353e34SChristoph Hellwig 	ib_mr_pool_destroy(qp, &qp->sig_mrs);
749a060b562SChristoph Hellwig 	ib_mr_pool_destroy(qp, &qp->rdma_mrs);
750a060b562SChristoph Hellwig }
751