blob: 2c3c93572c17262c21dffe891a18ff9f9e5b5223 [file] [log] [blame]
Ralph Campbellf9315512010-05-23 21:44:54 -07001/*
2 * Copyright (c) 2006, 2007, 2009 QLogic Corporation. All rights reserved.
3 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
34#include "qib.h"
35
36/**
37 * qib_alloc_lkey - allocate an lkey
Ralph Campbellf9315512010-05-23 21:44:54 -070038 * @mr: memory region that this lkey protects
Mike Marciniszyn6a826492012-06-27 18:33:12 -040039 * @dma_region: 0->normal key, 1->restricted DMA key
Ralph Campbellf9315512010-05-23 21:44:54 -070040 *
Mike Marciniszyn6a826492012-06-27 18:33:12 -040041 * Returns 0 if successful, otherwise returns -errno.
42 *
Mike Marciniszyn8aac4cc2012-06-27 18:33:19 -040043 * Increments mr reference count as required.
Mike Marciniszyn6a826492012-06-27 18:33:12 -040044 *
45 * Sets the lkey field mr for non-dma regions.
46 *
Ralph Campbellf9315512010-05-23 21:44:54 -070047 */
48
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -080049int qib_alloc_lkey(struct rvt_mregion *mr, int dma_region)
Ralph Campbellf9315512010-05-23 21:44:54 -070050{
51 unsigned long flags;
52 u32 r;
53 u32 n;
Mike Marciniszyn6a826492012-06-27 18:33:12 -040054 int ret = 0;
55 struct qib_ibdev *dev = to_idev(mr->pd->device);
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -080056 struct rvt_lkey_table *rkt = &dev->lk_table;
Ralph Campbellf9315512010-05-23 21:44:54 -070057
58 spin_lock_irqsave(&rkt->lock, flags);
59
Mike Marciniszyn6a826492012-06-27 18:33:12 -040060 /* special case for dma_mr lkey == 0 */
61 if (dma_region) {
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -080062 struct rvt_mregion *tmr;
Mike Marciniszyn8aac4cc2012-06-27 18:33:19 -040063
Mike Marciniszynf3bdf342013-05-17 12:40:32 +000064 tmr = rcu_access_pointer(dev->dma_mr);
Mike Marciniszyn8aac4cc2012-06-27 18:33:19 -040065 if (!tmr) {
Mike Marciniszyn6a826492012-06-27 18:33:12 -040066 qib_get_mr(mr);
Mike Marciniszyn8aac4cc2012-06-27 18:33:19 -040067 rcu_assign_pointer(dev->dma_mr, mr);
Mike Marciniszyn6a826492012-06-27 18:33:12 -040068 mr->lkey_published = 1;
69 }
70 goto success;
71 }
72
Ralph Campbellf9315512010-05-23 21:44:54 -070073 /* Find the next available LKEY */
74 r = rkt->next;
75 n = r;
76 for (;;) {
77 if (rkt->table[r] == NULL)
78 break;
79 r = (r + 1) & (rkt->max - 1);
Mike Marciniszyn6a826492012-06-27 18:33:12 -040080 if (r == n)
Ralph Campbellf9315512010-05-23 21:44:54 -070081 goto bail;
Ralph Campbellf9315512010-05-23 21:44:54 -070082 }
83 rkt->next = (r + 1) & (rkt->max - 1);
84 /*
85 * Make sure lkey is never zero which is reserved to indicate an
86 * unrestricted LKEY.
87 */
88 rkt->gen++;
Mike Marciniszynd6f1c172015-07-21 08:36:07 -040089 /*
90 * bits are capped in qib_verbs.c to insure enough bits
91 * for generation number
92 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -080093 mr->lkey = (r << (32 - ib_rvt_lkey_table_size)) |
94 ((((1 << (24 - ib_rvt_lkey_table_size)) - 1) & rkt->gen)
Ralph Campbellf9315512010-05-23 21:44:54 -070095 << 8);
96 if (mr->lkey == 0) {
97 mr->lkey |= 1 << 8;
98 rkt->gen++;
99 }
Mike Marciniszyn6a826492012-06-27 18:33:12 -0400100 qib_get_mr(mr);
Mike Marciniszyn8aac4cc2012-06-27 18:33:19 -0400101 rcu_assign_pointer(rkt->table[r], mr);
Mike Marciniszyn6a826492012-06-27 18:33:12 -0400102 mr->lkey_published = 1;
103success:
Ralph Campbellf9315512010-05-23 21:44:54 -0700104 spin_unlock_irqrestore(&rkt->lock, flags);
Mike Marciniszyn6a826492012-06-27 18:33:12 -0400105out:
Ralph Campbellf9315512010-05-23 21:44:54 -0700106 return ret;
Mike Marciniszyn6a826492012-06-27 18:33:12 -0400107bail:
108 spin_unlock_irqrestore(&rkt->lock, flags);
109 ret = -ENOMEM;
110 goto out;
Ralph Campbellf9315512010-05-23 21:44:54 -0700111}
112
113/**
114 * qib_free_lkey - free an lkey
Mike Marciniszyn6a826492012-06-27 18:33:12 -0400115 * @mr: mr to free from tables
Ralph Campbellf9315512010-05-23 21:44:54 -0700116 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800117void qib_free_lkey(struct rvt_mregion *mr)
Ralph Campbellf9315512010-05-23 21:44:54 -0700118{
119 unsigned long flags;
120 u32 lkey = mr->lkey;
121 u32 r;
Mike Marciniszyn6a826492012-06-27 18:33:12 -0400122 struct qib_ibdev *dev = to_idev(mr->pd->device);
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800123 struct rvt_lkey_table *rkt = &dev->lk_table;
Mike Marciniszyn6a826492012-06-27 18:33:12 -0400124
125 spin_lock_irqsave(&rkt->lock, flags);
126 if (!mr->lkey_published)
127 goto out;
Mike Marciniszyn8aac4cc2012-06-27 18:33:19 -0400128 if (lkey == 0)
Andreea-Cristina Bernat590c3fe2015-01-16 10:19:59 -0500129 RCU_INIT_POINTER(dev->dma_mr, NULL);
Mike Marciniszyn8aac4cc2012-06-27 18:33:19 -0400130 else {
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800131 r = lkey >> (32 - ib_rvt_lkey_table_size);
Andreea-Cristina Bernat590c3fe2015-01-16 10:19:59 -0500132 RCU_INIT_POINTER(rkt->table[r], NULL);
Ralph Campbellf9315512010-05-23 21:44:54 -0700133 }
Mike Marciniszyn8aac4cc2012-06-27 18:33:19 -0400134 qib_put_mr(mr);
135 mr->lkey_published = 0;
Mike Marciniszyn6a826492012-06-27 18:33:12 -0400136out:
Mike Marciniszyn8aac4cc2012-06-27 18:33:19 -0400137 spin_unlock_irqrestore(&rkt->lock, flags);
Ralph Campbellf9315512010-05-23 21:44:54 -0700138}
139
140/**
Ralph Campbellf9315512010-05-23 21:44:54 -0700141 * qib_rkey_ok - check the IB virtual address, length, and RKEY
Mike Marciniszyn8aac4cc2012-06-27 18:33:19 -0400142 * @qp: qp for validation
143 * @sge: SGE state
Ralph Campbellf9315512010-05-23 21:44:54 -0700144 * @len: length of data
145 * @vaddr: virtual address to place data
146 * @rkey: rkey to check
147 * @acc: access flags
148 *
149 * Return 1 if successful, otherwise 0.
Mike Marciniszyn8aac4cc2012-06-27 18:33:19 -0400150 *
151 * increments the reference count upon success
Ralph Campbellf9315512010-05-23 21:44:54 -0700152 */
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800153int qib_rkey_ok(struct rvt_qp *qp, struct rvt_sge *sge,
Ralph Campbellf9315512010-05-23 21:44:54 -0700154 u32 len, u64 vaddr, u32 rkey, int acc)
155{
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800156 struct rvt_lkey_table *rkt = &to_idev(qp->ibqp.device)->lk_table;
157 struct rvt_mregion *mr;
Ralph Campbellf9315512010-05-23 21:44:54 -0700158 unsigned n, m;
159 size_t off;
Ralph Campbellf9315512010-05-23 21:44:54 -0700160
161 /*
162 * We use RKEY == zero for kernel virtual addresses
163 * (see qib_get_dma_mr and qib_dma.c).
164 */
Mike Marciniszyn8aac4cc2012-06-27 18:33:19 -0400165 rcu_read_lock();
Ralph Campbellf9315512010-05-23 21:44:54 -0700166 if (rkey == 0) {
Dennis Dalessandrof44728d2016-01-22 12:44:44 -0800167 struct rvt_pd *pd = ibpd_to_rvtpd(qp->ibqp.pd);
Ralph Campbellf9315512010-05-23 21:44:54 -0700168 struct qib_ibdev *dev = to_idev(pd->ibpd.device);
169
170 if (pd->user)
171 goto bail;
Mike Marciniszyn8aac4cc2012-06-27 18:33:19 -0400172 mr = rcu_dereference(dev->dma_mr);
173 if (!mr)
Ralph Campbellf9315512010-05-23 21:44:54 -0700174 goto bail;
Mike Marciniszyn8aac4cc2012-06-27 18:33:19 -0400175 if (unlikely(!atomic_inc_not_zero(&mr->refcount)))
176 goto bail;
177 rcu_read_unlock();
Mike Marciniszyn4db62d42011-01-10 17:42:23 -0800178
Mike Marciniszyn8aac4cc2012-06-27 18:33:19 -0400179 sge->mr = mr;
Ralph Campbellf9315512010-05-23 21:44:54 -0700180 sge->vaddr = (void *) vaddr;
181 sge->length = len;
182 sge->sge_length = len;
183 sge->m = 0;
184 sge->n = 0;
185 goto ok;
186 }
187
Mike Marciniszyn8aac4cc2012-06-27 18:33:19 -0400188 mr = rcu_dereference(
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800189 rkt->table[(rkey >> (32 - ib_rvt_lkey_table_size))]);
Mike Marciniszyn8aac4cc2012-06-27 18:33:19 -0400190 if (unlikely(!mr || mr->lkey != rkey || qp->ibqp.pd != mr->pd))
Ralph Campbellf9315512010-05-23 21:44:54 -0700191 goto bail;
192
193 off = vaddr - mr->iova;
194 if (unlikely(vaddr < mr->iova || off + len > mr->length ||
195 (mr->access_flags & acc) == 0))
Mike Marciniszyn4db62d42011-01-10 17:42:23 -0800196 goto bail;
Mike Marciniszyn8aac4cc2012-06-27 18:33:19 -0400197 if (unlikely(!atomic_inc_not_zero(&mr->refcount)))
198 goto bail;
199 rcu_read_unlock();
Ralph Campbellf9315512010-05-23 21:44:54 -0700200
201 off += mr->offset;
Mike Marciniszyn2a600f12011-01-10 17:42:22 -0800202 if (mr->page_shift) {
203 /*
204 page sizes are uniform power of 2 so no loop is necessary
205 entries_spanned_by_off is the number of times the loop below
206 would have executed.
207 */
208 size_t entries_spanned_by_off;
209
210 entries_spanned_by_off = off >> mr->page_shift;
211 off -= (entries_spanned_by_off << mr->page_shift);
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800212 m = entries_spanned_by_off / RVT_SEGSZ;
213 n = entries_spanned_by_off % RVT_SEGSZ;
Mike Marciniszyn2a600f12011-01-10 17:42:22 -0800214 } else {
215 m = 0;
216 n = 0;
217 while (off >= mr->map[m]->segs[n].length) {
218 off -= mr->map[m]->segs[n].length;
219 n++;
Dennis Dalessandro7c2e11f2016-01-22 12:45:59 -0800220 if (n >= RVT_SEGSZ) {
Mike Marciniszyn2a600f12011-01-10 17:42:22 -0800221 m++;
222 n = 0;
223 }
Ralph Campbellf9315512010-05-23 21:44:54 -0700224 }
225 }
Ralph Campbellf9315512010-05-23 21:44:54 -0700226 sge->mr = mr;
227 sge->vaddr = mr->map[m]->segs[n].vaddr + off;
228 sge->length = mr->map[m]->segs[n].length - off;
229 sge->sge_length = len;
230 sge->m = m;
231 sge->n = n;
232ok:
Mike Marciniszyn4db62d42011-01-10 17:42:23 -0800233 return 1;
Ralph Campbellf9315512010-05-23 21:44:54 -0700234bail:
Mike Marciniszyn8aac4cc2012-06-27 18:33:19 -0400235 rcu_read_unlock();
Mike Marciniszyn4db62d42011-01-10 17:42:23 -0800236 return 0;
Ralph Campbellf9315512010-05-23 21:44:54 -0700237}
238