2 * Copyright (c) 2006, 2007, 2009 QLogic Corporation. All rights reserved.
3 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
37 * qib_alloc_lkey - allocate an lkey
38 * @rkt: lkey table in which to allocate the lkey
39 * @mr: memory region that this lkey protects
41 * Returns 1 if successful, otherwise returns 0.
44 int qib_alloc_lkey(struct qib_lkey_table
*rkt
, struct qib_mregion
*mr
)
51 spin_lock_irqsave(&rkt
->lock
, flags
);
53 /* Find the next available LKEY */
57 if (rkt
->table
[r
] == NULL
)
59 r
= (r
+ 1) & (rkt
->max
- 1);
61 spin_unlock_irqrestore(&rkt
->lock
, flags
);
66 rkt
->next
= (r
+ 1) & (rkt
->max
- 1);
68 * Make sure lkey is never zero which is reserved to indicate an
72 mr
->lkey
= (r
<< (32 - ib_qib_lkey_table_size
)) |
73 ((((1 << (24 - ib_qib_lkey_table_size
)) - 1) & rkt
->gen
)
80 spin_unlock_irqrestore(&rkt
->lock
, flags
);
89 * qib_free_lkey - free an lkey
90 * @rkt: table from which to free the lkey
91 * @lkey: lkey id to free
93 int qib_free_lkey(struct qib_ibdev
*dev
, struct qib_mregion
*mr
)
100 spin_lock_irqsave(&dev
->lk_table
.lock
, flags
);
102 if (dev
->dma_mr
&& dev
->dma_mr
== mr
) {
103 ret
= atomic_read(&dev
->dma_mr
->refcount
);
109 r
= lkey
>> (32 - ib_qib_lkey_table_size
);
110 ret
= atomic_read(&dev
->lk_table
.table
[r
]->refcount
);
112 dev
->lk_table
.table
[r
] = NULL
;
114 spin_unlock_irqrestore(&dev
->lk_table
.lock
, flags
);
122 * qib_lkey_ok - check IB SGE for validity and initialize
123 * @rkt: table containing lkey to check SGE against
124 * @isge: outgoing internal SGE
128 * Return 1 if valid and successful, otherwise returns 0.
130 * Check the IB SGE for validity and initialize our internal version
133 int qib_lkey_ok(struct qib_lkey_table
*rkt
, struct qib_pd
*pd
,
134 struct qib_sge
*isge
, struct ib_sge
*sge
, int acc
)
136 struct qib_mregion
*mr
;
142 * We use LKEY == zero for kernel virtual addresses
143 * (see qib_get_dma_mr and qib_dma.c).
145 spin_lock_irqsave(&rkt
->lock
, flags
);
146 if (sge
->lkey
== 0) {
147 struct qib_ibdev
*dev
= to_idev(pd
->ibpd
.device
);
153 atomic_inc(&dev
->dma_mr
->refcount
);
154 spin_unlock_irqrestore(&rkt
->lock
, flags
);
156 isge
->mr
= dev
->dma_mr
;
157 isge
->vaddr
= (void *) sge
->addr
;
158 isge
->length
= sge
->length
;
159 isge
->sge_length
= sge
->length
;
164 mr
= rkt
->table
[(sge
->lkey
>> (32 - ib_qib_lkey_table_size
))];
165 if (unlikely(mr
== NULL
|| mr
->lkey
!= sge
->lkey
||
166 mr
->pd
!= &pd
->ibpd
))
169 off
= sge
->addr
- mr
->user_base
;
170 if (unlikely(sge
->addr
< mr
->user_base
||
171 off
+ sge
->length
> mr
->length
||
172 (mr
->access_flags
& acc
) != acc
))
174 atomic_inc(&mr
->refcount
);
175 spin_unlock_irqrestore(&rkt
->lock
, flags
);
178 if (mr
->page_shift
) {
180 page sizes are uniform power of 2 so no loop is necessary
181 entries_spanned_by_off is the number of times the loop below
184 size_t entries_spanned_by_off
;
186 entries_spanned_by_off
= off
>> mr
->page_shift
;
187 off
-= (entries_spanned_by_off
<< mr
->page_shift
);
188 m
= entries_spanned_by_off
/QIB_SEGSZ
;
189 n
= entries_spanned_by_off
%QIB_SEGSZ
;
193 while (off
>= mr
->map
[m
]->segs
[n
].length
) {
194 off
-= mr
->map
[m
]->segs
[n
].length
;
196 if (n
>= QIB_SEGSZ
) {
203 isge
->vaddr
= mr
->map
[m
]->segs
[n
].vaddr
+ off
;
204 isge
->length
= mr
->map
[m
]->segs
[n
].length
- off
;
205 isge
->sge_length
= sge
->length
;
211 spin_unlock_irqrestore(&rkt
->lock
, flags
);
216 * qib_rkey_ok - check the IB virtual address, length, and RKEY
217 * @dev: infiniband device
219 * @len: length of data
220 * @vaddr: virtual address to place data
221 * @rkey: rkey to check
224 * Return 1 if successful, otherwise 0.
226 int qib_rkey_ok(struct qib_qp
*qp
, struct qib_sge
*sge
,
227 u32 len
, u64 vaddr
, u32 rkey
, int acc
)
229 struct qib_lkey_table
*rkt
= &to_idev(qp
->ibqp
.device
)->lk_table
;
230 struct qib_mregion
*mr
;
236 * We use RKEY == zero for kernel virtual addresses
237 * (see qib_get_dma_mr and qib_dma.c).
239 spin_lock_irqsave(&rkt
->lock
, flags
);
241 struct qib_pd
*pd
= to_ipd(qp
->ibqp
.pd
);
242 struct qib_ibdev
*dev
= to_idev(pd
->ibpd
.device
);
248 atomic_inc(&dev
->dma_mr
->refcount
);
249 spin_unlock_irqrestore(&rkt
->lock
, flags
);
251 sge
->mr
= dev
->dma_mr
;
252 sge
->vaddr
= (void *) vaddr
;
254 sge
->sge_length
= len
;
260 mr
= rkt
->table
[(rkey
>> (32 - ib_qib_lkey_table_size
))];
261 if (unlikely(mr
== NULL
|| mr
->lkey
!= rkey
|| qp
->ibqp
.pd
!= mr
->pd
))
264 off
= vaddr
- mr
->iova
;
265 if (unlikely(vaddr
< mr
->iova
|| off
+ len
> mr
->length
||
266 (mr
->access_flags
& acc
) == 0))
268 atomic_inc(&mr
->refcount
);
269 spin_unlock_irqrestore(&rkt
->lock
, flags
);
272 if (mr
->page_shift
) {
274 page sizes are uniform power of 2 so no loop is necessary
275 entries_spanned_by_off is the number of times the loop below
278 size_t entries_spanned_by_off
;
280 entries_spanned_by_off
= off
>> mr
->page_shift
;
281 off
-= (entries_spanned_by_off
<< mr
->page_shift
);
282 m
= entries_spanned_by_off
/QIB_SEGSZ
;
283 n
= entries_spanned_by_off
%QIB_SEGSZ
;
287 while (off
>= mr
->map
[m
]->segs
[n
].length
) {
288 off
-= mr
->map
[m
]->segs
[n
].length
;
290 if (n
>= QIB_SEGSZ
) {
297 sge
->vaddr
= mr
->map
[m
]->segs
[n
].vaddr
+ off
;
298 sge
->length
= mr
->map
[m
]->segs
[n
].length
- off
;
299 sge
->sge_length
= len
;
305 spin_unlock_irqrestore(&rkt
->lock
, flags
);
310 * Initialize the memory region specified by the work reqeust.
312 int qib_fast_reg_mr(struct qib_qp
*qp
, struct ib_send_wr
*wr
)
314 struct qib_lkey_table
*rkt
= &to_idev(qp
->ibqp
.device
)->lk_table
;
315 struct qib_pd
*pd
= to_ipd(qp
->ibqp
.pd
);
316 struct qib_mregion
*mr
;
317 u32 rkey
= wr
->wr
.fast_reg
.rkey
;
324 spin_lock_irqsave(&rkt
->lock
, flags
);
325 if (pd
->user
|| rkey
== 0)
328 mr
= rkt
->table
[(rkey
>> (32 - ib_qib_lkey_table_size
))];
329 if (unlikely(mr
== NULL
|| qp
->ibqp
.pd
!= mr
->pd
))
332 if (wr
->wr
.fast_reg
.page_list_len
> mr
->max_segs
)
335 ps
= 1UL << wr
->wr
.fast_reg
.page_shift
;
336 if (wr
->wr
.fast_reg
.length
> ps
* wr
->wr
.fast_reg
.page_list_len
)
339 mr
->user_base
= wr
->wr
.fast_reg
.iova_start
;
340 mr
->iova
= wr
->wr
.fast_reg
.iova_start
;
342 mr
->length
= wr
->wr
.fast_reg
.length
;
343 mr
->access_flags
= wr
->wr
.fast_reg
.access_flags
;
344 page_list
= wr
->wr
.fast_reg
.page_list
->page_list
;
347 for (i
= 0; i
< wr
->wr
.fast_reg
.page_list_len
; i
++) {
348 mr
->map
[m
]->segs
[n
].vaddr
= (void *) page_list
[i
];
349 mr
->map
[m
]->segs
[n
].length
= ps
;
350 if (++n
== QIB_SEGSZ
) {
358 spin_unlock_irqrestore(&rkt
->lock
, flags
);