• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright(c) 2016 Intel Corporation.
3  *
4  * This file is provided under a dual BSD/GPLv2 license.  When using or
5  * redistributing this file, you may do so under either license.
6  *
7  * GPL LICENSE SUMMARY
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of version 2 of the GNU General Public License as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * BSD LICENSE
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  *
24  *  - Redistributions of source code must retain the above copyright
25  *    notice, this list of conditions and the following disclaimer.
26  *  - Redistributions in binary form must reproduce the above copyright
27  *    notice, this list of conditions and the following disclaimer in
28  *    the documentation and/or other materials provided with the
29  *    distribution.
30  *  - Neither the name of Intel Corporation nor the names of its
31  *    contributors may be used to endorse or promote products derived
32  *    from this software without specific prior written permission.
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
38  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  */
47 
48 #include <linux/slab.h>
49 #include <linux/vmalloc.h>
50 #include <rdma/ib_umem.h>
51 #include <rdma/rdma_vt.h>
52 #include "vt.h"
53 #include "mr.h"
54 #include "trace.h"
55 
56 /**
57  * rvt_driver_mr_init - Init MR resources per driver
58  * @rdi: rvt dev struct
59  *
60  * Do any intilization needed when a driver registers with rdmavt.
61  *
62  * Return: 0 on success or errno on failure
63  */
rvt_driver_mr_init(struct rvt_dev_info * rdi)64 int rvt_driver_mr_init(struct rvt_dev_info *rdi)
65 {
66 	unsigned int lkey_table_size = rdi->dparms.lkey_table_size;
67 	unsigned lk_tab_size;
68 	int i;
69 
70 	/*
71 	 * The top hfi1_lkey_table_size bits are used to index the
72 	 * table.  The lower 8 bits can be owned by the user (copied from
73 	 * the LKEY).  The remaining bits act as a generation number or tag.
74 	 */
75 	if (!lkey_table_size)
76 		return -EINVAL;
77 
78 	spin_lock_init(&rdi->lkey_table.lock);
79 
80 	/* ensure generation is at least 4 bits */
81 	if (lkey_table_size > RVT_MAX_LKEY_TABLE_BITS) {
82 		rvt_pr_warn(rdi, "lkey bits %u too large, reduced to %u\n",
83 			    lkey_table_size, RVT_MAX_LKEY_TABLE_BITS);
84 		rdi->dparms.lkey_table_size = RVT_MAX_LKEY_TABLE_BITS;
85 		lkey_table_size = rdi->dparms.lkey_table_size;
86 	}
87 	rdi->lkey_table.max = 1 << lkey_table_size;
88 	rdi->lkey_table.shift = 32 - lkey_table_size;
89 	lk_tab_size = rdi->lkey_table.max * sizeof(*rdi->lkey_table.table);
90 	rdi->lkey_table.table = (struct rvt_mregion __rcu **)
91 			       vmalloc_node(lk_tab_size, rdi->dparms.node);
92 	if (!rdi->lkey_table.table)
93 		return -ENOMEM;
94 
95 	RCU_INIT_POINTER(rdi->dma_mr, NULL);
96 	for (i = 0; i < rdi->lkey_table.max; i++)
97 		RCU_INIT_POINTER(rdi->lkey_table.table[i], NULL);
98 
99 	rdi->dparms.props.max_mr = rdi->lkey_table.max;
100 	rdi->dparms.props.max_fmr = rdi->lkey_table.max;
101 	return 0;
102 }
103 
104 /**
105  *rvt_mr_exit: clean up MR
106  *@rdi: rvt dev structure
107  *
108  * called when drivers have unregistered or perhaps failed to register with us
109  */
rvt_mr_exit(struct rvt_dev_info * rdi)110 void rvt_mr_exit(struct rvt_dev_info *rdi)
111 {
112 	if (rdi->dma_mr)
113 		rvt_pr_err(rdi, "DMA MR not null!\n");
114 
115 	vfree(rdi->lkey_table.table);
116 }
117 
rvt_deinit_mregion(struct rvt_mregion * mr)118 static void rvt_deinit_mregion(struct rvt_mregion *mr)
119 {
120 	int i = mr->mapsz;
121 
122 	mr->mapsz = 0;
123 	while (i)
124 		kfree(mr->map[--i]);
125 	percpu_ref_exit(&mr->refcount);
126 }
127 
__rvt_mregion_complete(struct percpu_ref * ref)128 static void __rvt_mregion_complete(struct percpu_ref *ref)
129 {
130 	struct rvt_mregion *mr = container_of(ref, struct rvt_mregion,
131 					      refcount);
132 
133 	complete(&mr->comp);
134 }
135 
rvt_init_mregion(struct rvt_mregion * mr,struct ib_pd * pd,int count,unsigned int percpu_flags)136 static int rvt_init_mregion(struct rvt_mregion *mr, struct ib_pd *pd,
137 			    int count, unsigned int percpu_flags)
138 {
139 	int m, i = 0;
140 	struct rvt_dev_info *dev = ib_to_rvt(pd->device);
141 
142 	mr->mapsz = 0;
143 	m = (count + RVT_SEGSZ - 1) / RVT_SEGSZ;
144 	for (; i < m; i++) {
145 		mr->map[i] = kzalloc_node(sizeof(*mr->map[0]), GFP_KERNEL,
146 					  dev->dparms.node);
147 		if (!mr->map[i])
148 			goto bail;
149 		mr->mapsz++;
150 	}
151 	init_completion(&mr->comp);
152 	/* count returning the ptr to user */
153 	if (percpu_ref_init(&mr->refcount, &__rvt_mregion_complete,
154 			    percpu_flags, GFP_KERNEL))
155 		goto bail;
156 
157 	atomic_set(&mr->lkey_invalid, 0);
158 	mr->pd = pd;
159 	mr->max_segs = count;
160 	return 0;
161 bail:
162 	rvt_deinit_mregion(mr);
163 	return -ENOMEM;
164 }
165 
166 /**
167  * rvt_alloc_lkey - allocate an lkey
168  * @mr: memory region that this lkey protects
169  * @dma_region: 0->normal key, 1->restricted DMA key
170  *
171  * Returns 0 if successful, otherwise returns -errno.
172  *
173  * Increments mr reference count as required.
174  *
175  * Sets the lkey field mr for non-dma regions.
176  *
177  */
rvt_alloc_lkey(struct rvt_mregion * mr,int dma_region)178 static int rvt_alloc_lkey(struct rvt_mregion *mr, int dma_region)
179 {
180 	unsigned long flags;
181 	u32 r;
182 	u32 n;
183 	int ret = 0;
184 	struct rvt_dev_info *dev = ib_to_rvt(mr->pd->device);
185 	struct rvt_lkey_table *rkt = &dev->lkey_table;
186 
187 	rvt_get_mr(mr);
188 	spin_lock_irqsave(&rkt->lock, flags);
189 
190 	/* special case for dma_mr lkey == 0 */
191 	if (dma_region) {
192 		struct rvt_mregion *tmr;
193 
194 		tmr = rcu_access_pointer(dev->dma_mr);
195 		if (!tmr) {
196 			mr->lkey_published = 1;
197 			/* Insure published written first */
198 			rcu_assign_pointer(dev->dma_mr, mr);
199 			rvt_get_mr(mr);
200 		}
201 		goto success;
202 	}
203 
204 	/* Find the next available LKEY */
205 	r = rkt->next;
206 	n = r;
207 	for (;;) {
208 		if (!rcu_access_pointer(rkt->table[r]))
209 			break;
210 		r = (r + 1) & (rkt->max - 1);
211 		if (r == n)
212 			goto bail;
213 	}
214 	rkt->next = (r + 1) & (rkt->max - 1);
215 	/*
216 	 * Make sure lkey is never zero which is reserved to indicate an
217 	 * unrestricted LKEY.
218 	 */
219 	rkt->gen++;
220 	/*
221 	 * bits are capped to ensure enough bits for generation number
222 	 */
223 	mr->lkey = (r << (32 - dev->dparms.lkey_table_size)) |
224 		((((1 << (24 - dev->dparms.lkey_table_size)) - 1) & rkt->gen)
225 		 << 8);
226 	if (mr->lkey == 0) {
227 		mr->lkey |= 1 << 8;
228 		rkt->gen++;
229 	}
230 	mr->lkey_published = 1;
231 	/* Insure published written first */
232 	rcu_assign_pointer(rkt->table[r], mr);
233 success:
234 	spin_unlock_irqrestore(&rkt->lock, flags);
235 out:
236 	return ret;
237 bail:
238 	rvt_put_mr(mr);
239 	spin_unlock_irqrestore(&rkt->lock, flags);
240 	ret = -ENOMEM;
241 	goto out;
242 }
243 
244 /**
245  * rvt_free_lkey - free an lkey
246  * @mr: mr to free from tables
247  */
rvt_free_lkey(struct rvt_mregion * mr)248 static void rvt_free_lkey(struct rvt_mregion *mr)
249 {
250 	unsigned long flags;
251 	u32 lkey = mr->lkey;
252 	u32 r;
253 	struct rvt_dev_info *dev = ib_to_rvt(mr->pd->device);
254 	struct rvt_lkey_table *rkt = &dev->lkey_table;
255 	int freed = 0;
256 
257 	spin_lock_irqsave(&rkt->lock, flags);
258 	if (!lkey) {
259 		if (mr->lkey_published) {
260 			mr->lkey_published = 0;
261 			/* insure published is written before pointer */
262 			rcu_assign_pointer(dev->dma_mr, NULL);
263 			rvt_put_mr(mr);
264 		}
265 	} else {
266 		if (!mr->lkey_published)
267 			goto out;
268 		r = lkey >> (32 - dev->dparms.lkey_table_size);
269 		mr->lkey_published = 0;
270 		/* insure published is written before pointer */
271 		rcu_assign_pointer(rkt->table[r], NULL);
272 	}
273 	freed++;
274 out:
275 	spin_unlock_irqrestore(&rkt->lock, flags);
276 	if (freed)
277 		percpu_ref_kill(&mr->refcount);
278 }
279 
__rvt_alloc_mr(int count,struct ib_pd * pd)280 static struct rvt_mr *__rvt_alloc_mr(int count, struct ib_pd *pd)
281 {
282 	struct rvt_mr *mr;
283 	int rval = -ENOMEM;
284 	int m;
285 
286 	/* Allocate struct plus pointers to first level page tables. */
287 	m = (count + RVT_SEGSZ - 1) / RVT_SEGSZ;
288 	mr = kzalloc(sizeof(*mr) + m * sizeof(mr->mr.map[0]), GFP_KERNEL);
289 	if (!mr)
290 		goto bail;
291 
292 	rval = rvt_init_mregion(&mr->mr, pd, count, 0);
293 	if (rval)
294 		goto bail;
295 	/*
296 	 * ib_reg_phys_mr() will initialize mr->ibmr except for
297 	 * lkey and rkey.
298 	 */
299 	rval = rvt_alloc_lkey(&mr->mr, 0);
300 	if (rval)
301 		goto bail_mregion;
302 	mr->ibmr.lkey = mr->mr.lkey;
303 	mr->ibmr.rkey = mr->mr.lkey;
304 done:
305 	return mr;
306 
307 bail_mregion:
308 	rvt_deinit_mregion(&mr->mr);
309 bail:
310 	kfree(mr);
311 	mr = ERR_PTR(rval);
312 	goto done;
313 }
314 
__rvt_free_mr(struct rvt_mr * mr)315 static void __rvt_free_mr(struct rvt_mr *mr)
316 {
317 	rvt_free_lkey(&mr->mr);
318 	rvt_deinit_mregion(&mr->mr);
319 	kfree(mr);
320 }
321 
322 /**
323  * rvt_get_dma_mr - get a DMA memory region
324  * @pd: protection domain for this memory region
325  * @acc: access flags
326  *
327  * Return: the memory region on success, otherwise returns an errno.
328  * Note that all DMA addresses should be created via the functions in
329  * struct dma_virt_ops.
330  */
rvt_get_dma_mr(struct ib_pd * pd,int acc)331 struct ib_mr *rvt_get_dma_mr(struct ib_pd *pd, int acc)
332 {
333 	struct rvt_mr *mr;
334 	struct ib_mr *ret;
335 	int rval;
336 
337 	if (ibpd_to_rvtpd(pd)->user)
338 		return ERR_PTR(-EPERM);
339 
340 	mr = kzalloc(sizeof(*mr), GFP_KERNEL);
341 	if (!mr) {
342 		ret = ERR_PTR(-ENOMEM);
343 		goto bail;
344 	}
345 
346 	rval = rvt_init_mregion(&mr->mr, pd, 0, 0);
347 	if (rval) {
348 		ret = ERR_PTR(rval);
349 		goto bail;
350 	}
351 
352 	rval = rvt_alloc_lkey(&mr->mr, 1);
353 	if (rval) {
354 		ret = ERR_PTR(rval);
355 		goto bail_mregion;
356 	}
357 
358 	mr->mr.access_flags = acc;
359 	ret = &mr->ibmr;
360 done:
361 	return ret;
362 
363 bail_mregion:
364 	rvt_deinit_mregion(&mr->mr);
365 bail:
366 	kfree(mr);
367 	goto done;
368 }
369 
370 /**
371  * rvt_reg_user_mr - register a userspace memory region
372  * @pd: protection domain for this memory region
373  * @start: starting userspace address
374  * @length: length of region to register
375  * @mr_access_flags: access flags for this memory region
376  * @udata: unused by the driver
377  *
378  * Return: the memory region on success, otherwise returns an errno.
379  */
rvt_reg_user_mr(struct ib_pd * pd,u64 start,u64 length,u64 virt_addr,int mr_access_flags,struct ib_udata * udata)380 struct ib_mr *rvt_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
381 			      u64 virt_addr, int mr_access_flags,
382 			      struct ib_udata *udata)
383 {
384 	struct rvt_mr *mr;
385 	struct ib_umem *umem;
386 	struct scatterlist *sg;
387 	int n, m, entry;
388 	struct ib_mr *ret;
389 
390 	if (length == 0)
391 		return ERR_PTR(-EINVAL);
392 
393 	umem = ib_umem_get(pd->uobject->context, start, length,
394 			   mr_access_flags, 0);
395 	if (IS_ERR(umem))
396 		return (void *)umem;
397 
398 	n = umem->nmap;
399 
400 	mr = __rvt_alloc_mr(n, pd);
401 	if (IS_ERR(mr)) {
402 		ret = (struct ib_mr *)mr;
403 		goto bail_umem;
404 	}
405 
406 	mr->mr.user_base = start;
407 	mr->mr.iova = virt_addr;
408 	mr->mr.length = length;
409 	mr->mr.offset = ib_umem_offset(umem);
410 	mr->mr.access_flags = mr_access_flags;
411 	mr->umem = umem;
412 
413 	mr->mr.page_shift = umem->page_shift;
414 	m = 0;
415 	n = 0;
416 	for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
417 		void *vaddr;
418 
419 		vaddr = page_address(sg_page(sg));
420 		if (!vaddr) {
421 			ret = ERR_PTR(-EINVAL);
422 			goto bail_inval;
423 		}
424 		mr->mr.map[m]->segs[n].vaddr = vaddr;
425 		mr->mr.map[m]->segs[n].length = BIT(umem->page_shift);
426 		trace_rvt_mr_user_seg(&mr->mr, m, n, vaddr,
427 				      BIT(umem->page_shift));
428 		n++;
429 		if (n == RVT_SEGSZ) {
430 			m++;
431 			n = 0;
432 		}
433 	}
434 	return &mr->ibmr;
435 
436 bail_inval:
437 	__rvt_free_mr(mr);
438 
439 bail_umem:
440 	ib_umem_release(umem);
441 
442 	return ret;
443 }
444 
445 /**
446  * rvt_dereg_clean_qp_cb - callback from iterator
447  * @qp - the qp
448  * @v - the mregion (as u64)
449  *
450  * This routine fields the callback for all QPs and
451  * for QPs in the same PD as the MR will call the
452  * rvt_qp_mr_clean() to potentially cleanup references.
453  */
rvt_dereg_clean_qp_cb(struct rvt_qp * qp,u64 v)454 static void rvt_dereg_clean_qp_cb(struct rvt_qp *qp, u64 v)
455 {
456 	struct rvt_mregion *mr = (struct rvt_mregion *)v;
457 
458 	/* skip PDs that are not ours */
459 	if (mr->pd != qp->ibqp.pd)
460 		return;
461 	rvt_qp_mr_clean(qp, mr->lkey);
462 }
463 
464 /**
465  * rvt_dereg_clean_qps - find QPs for reference cleanup
466  * @mr - the MR that is being deregistered
467  *
468  * This routine iterates RC QPs looking for references
469  * to the lkey noted in mr.
470  */
rvt_dereg_clean_qps(struct rvt_mregion * mr)471 static void rvt_dereg_clean_qps(struct rvt_mregion *mr)
472 {
473 	struct rvt_dev_info *rdi = ib_to_rvt(mr->pd->device);
474 
475 	rvt_qp_iter(rdi, (u64)mr, rvt_dereg_clean_qp_cb);
476 }
477 
478 /**
479  * rvt_check_refs - check references
480  * @mr - the megion
481  * @t - the caller identification
482  *
483  * This routine checks MRs holding a reference during
484  * when being de-registered.
485  *
486  * If the count is non-zero, the code calls a clean routine then
487  * waits for the timeout for the count to zero.
488  */
rvt_check_refs(struct rvt_mregion * mr,const char * t)489 static int rvt_check_refs(struct rvt_mregion *mr, const char *t)
490 {
491 	unsigned long timeout;
492 	struct rvt_dev_info *rdi = ib_to_rvt(mr->pd->device);
493 
494 	if (mr->lkey) {
495 		/* avoid dma mr */
496 		rvt_dereg_clean_qps(mr);
497 		/* @mr was indexed on rcu protected @lkey_table */
498 		synchronize_rcu();
499 	}
500 
501 	timeout = wait_for_completion_timeout(&mr->comp, 5 * HZ);
502 	if (!timeout) {
503 		rvt_pr_err(rdi,
504 			   "%s timeout mr %p pd %p lkey %x refcount %ld\n",
505 			   t, mr, mr->pd, mr->lkey,
506 			   atomic_long_read(&mr->refcount.count));
507 		rvt_get_mr(mr);
508 		return -EBUSY;
509 	}
510 	return 0;
511 }
512 
513 /**
514  * rvt_mr_has_lkey - is MR
515  * @mr - the mregion
516  * @lkey - the lkey
517  */
rvt_mr_has_lkey(struct rvt_mregion * mr,u32 lkey)518 bool rvt_mr_has_lkey(struct rvt_mregion *mr, u32 lkey)
519 {
520 	return mr && lkey == mr->lkey;
521 }
522 
523 /**
524  * rvt_ss_has_lkey - is mr in sge tests
525  * @ss - the sge state
526  * @lkey
527  *
528  * This code tests for an MR in the indicated
529  * sge state.
530  */
rvt_ss_has_lkey(struct rvt_sge_state * ss,u32 lkey)531 bool rvt_ss_has_lkey(struct rvt_sge_state *ss, u32 lkey)
532 {
533 	int i;
534 	bool rval = false;
535 
536 	if (!ss->num_sge)
537 		return rval;
538 	/* first one */
539 	rval = rvt_mr_has_lkey(ss->sge.mr, lkey);
540 	/* any others */
541 	for (i = 0; !rval && i < ss->num_sge - 1; i++)
542 		rval = rvt_mr_has_lkey(ss->sg_list[i].mr, lkey);
543 	return rval;
544 }
545 
546 /**
547  * rvt_dereg_mr - unregister and free a memory region
548  * @ibmr: the memory region to free
549  *
550  *
551  * Note that this is called to free MRs created by rvt_get_dma_mr()
552  * or rvt_reg_user_mr().
553  *
554  * Returns 0 on success.
555  */
rvt_dereg_mr(struct ib_mr * ibmr)556 int rvt_dereg_mr(struct ib_mr *ibmr)
557 {
558 	struct rvt_mr *mr = to_imr(ibmr);
559 	int ret;
560 
561 	rvt_free_lkey(&mr->mr);
562 
563 	rvt_put_mr(&mr->mr); /* will set completion if last */
564 	ret = rvt_check_refs(&mr->mr, __func__);
565 	if (ret)
566 		goto out;
567 	rvt_deinit_mregion(&mr->mr);
568 	if (mr->umem)
569 		ib_umem_release(mr->umem);
570 	kfree(mr);
571 out:
572 	return ret;
573 }
574 
575 /**
576  * rvt_alloc_mr - Allocate a memory region usable with the
577  * @pd: protection domain for this memory region
578  * @mr_type: mem region type
579  * @max_num_sg: Max number of segments allowed
580  *
581  * Return: the memory region on success, otherwise return an errno.
582  */
rvt_alloc_mr(struct ib_pd * pd,enum ib_mr_type mr_type,u32 max_num_sg)583 struct ib_mr *rvt_alloc_mr(struct ib_pd *pd,
584 			   enum ib_mr_type mr_type,
585 			   u32 max_num_sg)
586 {
587 	struct rvt_mr *mr;
588 
589 	if (mr_type != IB_MR_TYPE_MEM_REG)
590 		return ERR_PTR(-EINVAL);
591 
592 	mr = __rvt_alloc_mr(max_num_sg, pd);
593 	if (IS_ERR(mr))
594 		return (struct ib_mr *)mr;
595 
596 	return &mr->ibmr;
597 }
598 
599 /**
600  * rvt_set_page - page assignment function called by ib_sg_to_pages
601  * @ibmr: memory region
602  * @addr: dma address of mapped page
603  *
604  * Return: 0 on success
605  */
rvt_set_page(struct ib_mr * ibmr,u64 addr)606 static int rvt_set_page(struct ib_mr *ibmr, u64 addr)
607 {
608 	struct rvt_mr *mr = to_imr(ibmr);
609 	u32 ps = 1 << mr->mr.page_shift;
610 	u32 mapped_segs = mr->mr.length >> mr->mr.page_shift;
611 	int m, n;
612 
613 	if (unlikely(mapped_segs == mr->mr.max_segs))
614 		return -ENOMEM;
615 
616 	m = mapped_segs / RVT_SEGSZ;
617 	n = mapped_segs % RVT_SEGSZ;
618 	mr->mr.map[m]->segs[n].vaddr = (void *)addr;
619 	mr->mr.map[m]->segs[n].length = ps;
620 	trace_rvt_mr_page_seg(&mr->mr, m, n, (void *)addr, ps);
621 	mr->mr.length += ps;
622 
623 	return 0;
624 }
625 
626 /**
627  * rvt_map_mr_sg - map sg list and set it the memory region
628  * @ibmr: memory region
629  * @sg: dma mapped scatterlist
630  * @sg_nents: number of entries in sg
631  * @sg_offset: offset in bytes into sg
632  *
633  * Overwrite rvt_mr length with mr length calculated by ib_sg_to_pages.
634  *
635  * Return: number of sg elements mapped to the memory region
636  */
rvt_map_mr_sg(struct ib_mr * ibmr,struct scatterlist * sg,int sg_nents,unsigned int * sg_offset)637 int rvt_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg,
638 		  int sg_nents, unsigned int *sg_offset)
639 {
640 	struct rvt_mr *mr = to_imr(ibmr);
641 	int ret;
642 
643 	mr->mr.length = 0;
644 	mr->mr.page_shift = PAGE_SHIFT;
645 	ret = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, rvt_set_page);
646 	mr->mr.user_base = ibmr->iova;
647 	mr->mr.iova = ibmr->iova;
648 	mr->mr.offset = ibmr->iova - (u64)mr->mr.map[0]->segs[0].vaddr;
649 	mr->mr.length = (size_t)ibmr->length;
650 	return ret;
651 }
652 
653 /**
654  * rvt_fast_reg_mr - fast register physical MR
655  * @qp: the queue pair where the work request comes from
656  * @ibmr: the memory region to be registered
657  * @key: updated key for this memory region
658  * @access: access flags for this memory region
659  *
660  * Returns 0 on success.
661  */
rvt_fast_reg_mr(struct rvt_qp * qp,struct ib_mr * ibmr,u32 key,int access)662 int rvt_fast_reg_mr(struct rvt_qp *qp, struct ib_mr *ibmr, u32 key,
663 		    int access)
664 {
665 	struct rvt_mr *mr = to_imr(ibmr);
666 
667 	if (qp->ibqp.pd != mr->mr.pd)
668 		return -EACCES;
669 
670 	/* not applicable to dma MR or user MR */
671 	if (!mr->mr.lkey || mr->umem)
672 		return -EINVAL;
673 
674 	if ((key & 0xFFFFFF00) != (mr->mr.lkey & 0xFFFFFF00))
675 		return -EINVAL;
676 
677 	ibmr->lkey = key;
678 	ibmr->rkey = key;
679 	mr->mr.lkey = key;
680 	mr->mr.access_flags = access;
681 	mr->mr.iova = ibmr->iova;
682 	atomic_set(&mr->mr.lkey_invalid, 0);
683 
684 	return 0;
685 }
686 EXPORT_SYMBOL(rvt_fast_reg_mr);
687 
688 /**
689  * rvt_invalidate_rkey - invalidate an MR rkey
690  * @qp: queue pair associated with the invalidate op
691  * @rkey: rkey to invalidate
692  *
693  * Returns 0 on success.
694  */
rvt_invalidate_rkey(struct rvt_qp * qp,u32 rkey)695 int rvt_invalidate_rkey(struct rvt_qp *qp, u32 rkey)
696 {
697 	struct rvt_dev_info *dev = ib_to_rvt(qp->ibqp.device);
698 	struct rvt_lkey_table *rkt = &dev->lkey_table;
699 	struct rvt_mregion *mr;
700 
701 	if (rkey == 0)
702 		return -EINVAL;
703 
704 	rcu_read_lock();
705 	mr = rcu_dereference(
706 		rkt->table[(rkey >> (32 - dev->dparms.lkey_table_size))]);
707 	if (unlikely(!mr || mr->lkey != rkey || qp->ibqp.pd != mr->pd))
708 		goto bail;
709 
710 	atomic_set(&mr->lkey_invalid, 1);
711 	rcu_read_unlock();
712 	return 0;
713 
714 bail:
715 	rcu_read_unlock();
716 	return -EINVAL;
717 }
718 EXPORT_SYMBOL(rvt_invalidate_rkey);
719 
720 /**
721  * rvt_alloc_fmr - allocate a fast memory region
722  * @pd: the protection domain for this memory region
723  * @mr_access_flags: access flags for this memory region
724  * @fmr_attr: fast memory region attributes
725  *
726  * Return: the memory region on success, otherwise returns an errno.
727  */
rvt_alloc_fmr(struct ib_pd * pd,int mr_access_flags,struct ib_fmr_attr * fmr_attr)728 struct ib_fmr *rvt_alloc_fmr(struct ib_pd *pd, int mr_access_flags,
729 			     struct ib_fmr_attr *fmr_attr)
730 {
731 	struct rvt_fmr *fmr;
732 	int m;
733 	struct ib_fmr *ret;
734 	int rval = -ENOMEM;
735 
736 	/* Allocate struct plus pointers to first level page tables. */
737 	m = (fmr_attr->max_pages + RVT_SEGSZ - 1) / RVT_SEGSZ;
738 	fmr = kzalloc(sizeof(*fmr) + m * sizeof(fmr->mr.map[0]), GFP_KERNEL);
739 	if (!fmr)
740 		goto bail;
741 
742 	rval = rvt_init_mregion(&fmr->mr, pd, fmr_attr->max_pages,
743 				PERCPU_REF_INIT_ATOMIC);
744 	if (rval)
745 		goto bail;
746 
747 	/*
748 	 * ib_alloc_fmr() will initialize fmr->ibfmr except for lkey &
749 	 * rkey.
750 	 */
751 	rval = rvt_alloc_lkey(&fmr->mr, 0);
752 	if (rval)
753 		goto bail_mregion;
754 	fmr->ibfmr.rkey = fmr->mr.lkey;
755 	fmr->ibfmr.lkey = fmr->mr.lkey;
756 	/*
757 	 * Resources are allocated but no valid mapping (RKEY can't be
758 	 * used).
759 	 */
760 	fmr->mr.access_flags = mr_access_flags;
761 	fmr->mr.max_segs = fmr_attr->max_pages;
762 	fmr->mr.page_shift = fmr_attr->page_shift;
763 
764 	ret = &fmr->ibfmr;
765 done:
766 	return ret;
767 
768 bail_mregion:
769 	rvt_deinit_mregion(&fmr->mr);
770 bail:
771 	kfree(fmr);
772 	ret = ERR_PTR(rval);
773 	goto done;
774 }
775 
776 /**
777  * rvt_map_phys_fmr - set up a fast memory region
778  * @ibmfr: the fast memory region to set up
779  * @page_list: the list of pages to associate with the fast memory region
780  * @list_len: the number of pages to associate with the fast memory region
781  * @iova: the virtual address of the start of the fast memory region
782  *
783  * This may be called from interrupt context.
784  *
785  * Return: 0 on success
786  */
787 
rvt_map_phys_fmr(struct ib_fmr * ibfmr,u64 * page_list,int list_len,u64 iova)788 int rvt_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
789 		     int list_len, u64 iova)
790 {
791 	struct rvt_fmr *fmr = to_ifmr(ibfmr);
792 	struct rvt_lkey_table *rkt;
793 	unsigned long flags;
794 	int m, n;
795 	unsigned long i;
796 	u32 ps;
797 	struct rvt_dev_info *rdi = ib_to_rvt(ibfmr->device);
798 
799 	i = atomic_long_read(&fmr->mr.refcount.count);
800 	if (i > 2)
801 		return -EBUSY;
802 
803 	if (list_len > fmr->mr.max_segs)
804 		return -EINVAL;
805 
806 	rkt = &rdi->lkey_table;
807 	spin_lock_irqsave(&rkt->lock, flags);
808 	fmr->mr.user_base = iova;
809 	fmr->mr.iova = iova;
810 	ps = 1 << fmr->mr.page_shift;
811 	fmr->mr.length = list_len * ps;
812 	m = 0;
813 	n = 0;
814 	for (i = 0; i < list_len; i++) {
815 		fmr->mr.map[m]->segs[n].vaddr = (void *)page_list[i];
816 		fmr->mr.map[m]->segs[n].length = ps;
817 		trace_rvt_mr_fmr_seg(&fmr->mr, m, n, (void *)page_list[i], ps);
818 		if (++n == RVT_SEGSZ) {
819 			m++;
820 			n = 0;
821 		}
822 	}
823 	spin_unlock_irqrestore(&rkt->lock, flags);
824 	return 0;
825 }
826 
827 /**
828  * rvt_unmap_fmr - unmap fast memory regions
829  * @fmr_list: the list of fast memory regions to unmap
830  *
831  * Return: 0 on success.
832  */
rvt_unmap_fmr(struct list_head * fmr_list)833 int rvt_unmap_fmr(struct list_head *fmr_list)
834 {
835 	struct rvt_fmr *fmr;
836 	struct rvt_lkey_table *rkt;
837 	unsigned long flags;
838 	struct rvt_dev_info *rdi;
839 
840 	list_for_each_entry(fmr, fmr_list, ibfmr.list) {
841 		rdi = ib_to_rvt(fmr->ibfmr.device);
842 		rkt = &rdi->lkey_table;
843 		spin_lock_irqsave(&rkt->lock, flags);
844 		fmr->mr.user_base = 0;
845 		fmr->mr.iova = 0;
846 		fmr->mr.length = 0;
847 		spin_unlock_irqrestore(&rkt->lock, flags);
848 	}
849 	return 0;
850 }
851 
852 /**
853  * rvt_dealloc_fmr - deallocate a fast memory region
854  * @ibfmr: the fast memory region to deallocate
855  *
856  * Return: 0 on success.
857  */
rvt_dealloc_fmr(struct ib_fmr * ibfmr)858 int rvt_dealloc_fmr(struct ib_fmr *ibfmr)
859 {
860 	struct rvt_fmr *fmr = to_ifmr(ibfmr);
861 	int ret = 0;
862 
863 	rvt_free_lkey(&fmr->mr);
864 	rvt_put_mr(&fmr->mr); /* will set completion if last */
865 	ret = rvt_check_refs(&fmr->mr, __func__);
866 	if (ret)
867 		goto out;
868 	rvt_deinit_mregion(&fmr->mr);
869 	kfree(fmr);
870 out:
871 	return ret;
872 }
873 
874 /**
875  * rvt_sge_adjacent - is isge compressible
876  * @last_sge: last outgoing SGE written
877  * @sge: SGE to check
878  *
879  * If adjacent will update last_sge to add length.
880  *
881  * Return: true if isge is adjacent to last sge
882  */
rvt_sge_adjacent(struct rvt_sge * last_sge,struct ib_sge * sge)883 static inline bool rvt_sge_adjacent(struct rvt_sge *last_sge,
884 				    struct ib_sge *sge)
885 {
886 	if (last_sge && sge->lkey == last_sge->mr->lkey &&
887 	    ((uint64_t)(last_sge->vaddr + last_sge->length) == sge->addr)) {
888 		if (sge->lkey) {
889 			if (unlikely((sge->addr - last_sge->mr->user_base +
890 			      sge->length > last_sge->mr->length)))
891 				return false; /* overrun, caller will catch */
892 		} else {
893 			last_sge->length += sge->length;
894 		}
895 		last_sge->sge_length += sge->length;
896 		trace_rvt_sge_adjacent(last_sge, sge);
897 		return true;
898 	}
899 	return false;
900 }
901 
902 /**
903  * rvt_lkey_ok - check IB SGE for validity and initialize
904  * @rkt: table containing lkey to check SGE against
905  * @pd: protection domain
906  * @isge: outgoing internal SGE
907  * @last_sge: last outgoing SGE written
908  * @sge: SGE to check
909  * @acc: access flags
910  *
911  * Check the IB SGE for validity and initialize our internal version
912  * of it.
913  *
914  * Increments the reference count when a new sge is stored.
915  *
916  * Return: 0 if compressed, 1 if added , otherwise returns -errno.
917  */
rvt_lkey_ok(struct rvt_lkey_table * rkt,struct rvt_pd * pd,struct rvt_sge * isge,struct rvt_sge * last_sge,struct ib_sge * sge,int acc)918 int rvt_lkey_ok(struct rvt_lkey_table *rkt, struct rvt_pd *pd,
919 		struct rvt_sge *isge, struct rvt_sge *last_sge,
920 		struct ib_sge *sge, int acc)
921 {
922 	struct rvt_mregion *mr;
923 	unsigned n, m;
924 	size_t off;
925 
926 	/*
927 	 * We use LKEY == zero for kernel virtual addresses
928 	 * (see rvt_get_dma_mr() and dma_virt_ops).
929 	 */
930 	if (sge->lkey == 0) {
931 		struct rvt_dev_info *dev = ib_to_rvt(pd->ibpd.device);
932 
933 		if (pd->user)
934 			return -EINVAL;
935 		if (rvt_sge_adjacent(last_sge, sge))
936 			return 0;
937 		rcu_read_lock();
938 		mr = rcu_dereference(dev->dma_mr);
939 		if (!mr)
940 			goto bail;
941 		rvt_get_mr(mr);
942 		rcu_read_unlock();
943 
944 		isge->mr = mr;
945 		isge->vaddr = (void *)sge->addr;
946 		isge->length = sge->length;
947 		isge->sge_length = sge->length;
948 		isge->m = 0;
949 		isge->n = 0;
950 		goto ok;
951 	}
952 	if (rvt_sge_adjacent(last_sge, sge))
953 		return 0;
954 	rcu_read_lock();
955 	mr = rcu_dereference(rkt->table[sge->lkey >> rkt->shift]);
956 	if (!mr)
957 		goto bail;
958 	rvt_get_mr(mr);
959 	if (!READ_ONCE(mr->lkey_published))
960 		goto bail_unref;
961 
962 	if (unlikely(atomic_read(&mr->lkey_invalid) ||
963 		     mr->lkey != sge->lkey || mr->pd != &pd->ibpd))
964 		goto bail_unref;
965 
966 	off = sge->addr - mr->user_base;
967 	if (unlikely(sge->addr < mr->user_base ||
968 		     off + sge->length > mr->length ||
969 		     (mr->access_flags & acc) != acc))
970 		goto bail_unref;
971 	rcu_read_unlock();
972 
973 	off += mr->offset;
974 	if (mr->page_shift) {
975 		/*
976 		 * page sizes are uniform power of 2 so no loop is necessary
977 		 * entries_spanned_by_off is the number of times the loop below
978 		 * would have executed.
979 		*/
980 		size_t entries_spanned_by_off;
981 
982 		entries_spanned_by_off = off >> mr->page_shift;
983 		off -= (entries_spanned_by_off << mr->page_shift);
984 		m = entries_spanned_by_off / RVT_SEGSZ;
985 		n = entries_spanned_by_off % RVT_SEGSZ;
986 	} else {
987 		m = 0;
988 		n = 0;
989 		while (off >= mr->map[m]->segs[n].length) {
990 			off -= mr->map[m]->segs[n].length;
991 			n++;
992 			if (n >= RVT_SEGSZ) {
993 				m++;
994 				n = 0;
995 			}
996 		}
997 	}
998 	isge->mr = mr;
999 	isge->vaddr = mr->map[m]->segs[n].vaddr + off;
1000 	isge->length = mr->map[m]->segs[n].length - off;
1001 	isge->sge_length = sge->length;
1002 	isge->m = m;
1003 	isge->n = n;
1004 ok:
1005 	trace_rvt_sge_new(isge, sge);
1006 	return 1;
1007 bail_unref:
1008 	rvt_put_mr(mr);
1009 bail:
1010 	rcu_read_unlock();
1011 	return -EINVAL;
1012 }
1013 EXPORT_SYMBOL(rvt_lkey_ok);
1014 
1015 /**
1016  * rvt_rkey_ok - check the IB virtual address, length, and RKEY
1017  * @qp: qp for validation
1018  * @sge: SGE state
1019  * @len: length of data
1020  * @vaddr: virtual address to place data
1021  * @rkey: rkey to check
1022  * @acc: access flags
1023  *
1024  * Return: 1 if successful, otherwise 0.
1025  *
1026  * increments the reference count upon success
1027  */
rvt_rkey_ok(struct rvt_qp * qp,struct rvt_sge * sge,u32 len,u64 vaddr,u32 rkey,int acc)1028 int rvt_rkey_ok(struct rvt_qp *qp, struct rvt_sge *sge,
1029 		u32 len, u64 vaddr, u32 rkey, int acc)
1030 {
1031 	struct rvt_dev_info *dev = ib_to_rvt(qp->ibqp.device);
1032 	struct rvt_lkey_table *rkt = &dev->lkey_table;
1033 	struct rvt_mregion *mr;
1034 	unsigned n, m;
1035 	size_t off;
1036 
1037 	/*
1038 	 * We use RKEY == zero for kernel virtual addresses
1039 	 * (see rvt_get_dma_mr() and dma_virt_ops).
1040 	 */
1041 	rcu_read_lock();
1042 	if (rkey == 0) {
1043 		struct rvt_pd *pd = ibpd_to_rvtpd(qp->ibqp.pd);
1044 		struct rvt_dev_info *rdi = ib_to_rvt(pd->ibpd.device);
1045 
1046 		if (pd->user)
1047 			goto bail;
1048 		mr = rcu_dereference(rdi->dma_mr);
1049 		if (!mr)
1050 			goto bail;
1051 		rvt_get_mr(mr);
1052 		rcu_read_unlock();
1053 
1054 		sge->mr = mr;
1055 		sge->vaddr = (void *)vaddr;
1056 		sge->length = len;
1057 		sge->sge_length = len;
1058 		sge->m = 0;
1059 		sge->n = 0;
1060 		goto ok;
1061 	}
1062 
1063 	mr = rcu_dereference(rkt->table[rkey >> rkt->shift]);
1064 	if (!mr)
1065 		goto bail;
1066 	rvt_get_mr(mr);
1067 	/* insure mr read is before test */
1068 	if (!READ_ONCE(mr->lkey_published))
1069 		goto bail_unref;
1070 	if (unlikely(atomic_read(&mr->lkey_invalid) ||
1071 		     mr->lkey != rkey || qp->ibqp.pd != mr->pd))
1072 		goto bail_unref;
1073 
1074 	off = vaddr - mr->iova;
1075 	if (unlikely(vaddr < mr->iova || off + len > mr->length ||
1076 		     (mr->access_flags & acc) == 0))
1077 		goto bail_unref;
1078 	rcu_read_unlock();
1079 
1080 	off += mr->offset;
1081 	if (mr->page_shift) {
1082 		/*
1083 		 * page sizes are uniform power of 2 so no loop is necessary
1084 		 * entries_spanned_by_off is the number of times the loop below
1085 		 * would have executed.
1086 		*/
1087 		size_t entries_spanned_by_off;
1088 
1089 		entries_spanned_by_off = off >> mr->page_shift;
1090 		off -= (entries_spanned_by_off << mr->page_shift);
1091 		m = entries_spanned_by_off / RVT_SEGSZ;
1092 		n = entries_spanned_by_off % RVT_SEGSZ;
1093 	} else {
1094 		m = 0;
1095 		n = 0;
1096 		while (off >= mr->map[m]->segs[n].length) {
1097 			off -= mr->map[m]->segs[n].length;
1098 			n++;
1099 			if (n >= RVT_SEGSZ) {
1100 				m++;
1101 				n = 0;
1102 			}
1103 		}
1104 	}
1105 	sge->mr = mr;
1106 	sge->vaddr = mr->map[m]->segs[n].vaddr + off;
1107 	sge->length = mr->map[m]->segs[n].length - off;
1108 	sge->sge_length = len;
1109 	sge->m = m;
1110 	sge->n = n;
1111 ok:
1112 	return 1;
1113 bail_unref:
1114 	rvt_put_mr(mr);
1115 bail:
1116 	rcu_read_unlock();
1117 	return 0;
1118 }
1119 EXPORT_SYMBOL(rvt_rkey_ok);
1120