2 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #include "ipath_verbs.h"
38 * ipath_alloc_lkey - allocate an lkey
39 * @rkt: lkey table in which to allocate the lkey
40 * @mr: memory region that this lkey protects
42 * Returns 1 if successful, otherwise returns 0.
45 int ipath_alloc_lkey(struct ipath_lkey_table
*rkt
, struct ipath_mregion
*mr
)
52 spin_lock_irqsave(&rkt
->lock
, flags
);
54 /* 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
);
62 _VERBS_INFO("LKEY table full\n");
67 rkt
->next
= (r
+ 1) & (rkt
->max
- 1);
69 * Make sure lkey is never zero which is reserved to indicate an
73 mr
->lkey
= (r
<< (32 - ib_ipath_lkey_table_size
)) |
74 ((((1 << (24 - ib_ipath_lkey_table_size
)) - 1) & rkt
->gen
)
81 spin_unlock_irqrestore(&rkt
->lock
, flags
);
90 * ipath_free_lkey - free an lkey
91 * @rkt: table from which to free the lkey
92 * @lkey: lkey id to free
94 void ipath_free_lkey(struct ipath_lkey_table
*rkt
, u32 lkey
)
101 r
= lkey
>> (32 - ib_ipath_lkey_table_size
);
102 spin_lock_irqsave(&rkt
->lock
, flags
);
103 rkt
->table
[r
] = NULL
;
104 spin_unlock_irqrestore(&rkt
->lock
, flags
);
108 * ipath_lkey_ok - check IB SGE for validity and initialize
109 * @rkt: table containing lkey to check SGE against
110 * @isge: outgoing internal SGE
114 * Return 1 if valid and successful, otherwise returns 0.
116 * Check the IB SGE for validity and initialize our internal version
119 int ipath_lkey_ok(struct ipath_lkey_table
*rkt
, struct ipath_sge
*isge
,
120 struct ib_sge
*sge
, int acc
)
122 struct ipath_mregion
*mr
;
127 * We use LKEY == zero to mean a physical kmalloc() address.
128 * This is a bit of a hack since we rely on dma_map_single()
129 * being reversible by calling bus_to_virt().
131 if (sge
->lkey
== 0) {
133 isge
->vaddr
= bus_to_virt(sge
->addr
);
134 isge
->length
= sge
->length
;
135 isge
->sge_length
= sge
->length
;
139 spin_lock(&rkt
->lock
);
140 mr
= rkt
->table
[(sge
->lkey
>> (32 - ib_ipath_lkey_table_size
))];
141 spin_unlock(&rkt
->lock
);
142 if (unlikely(mr
== NULL
|| mr
->lkey
!= sge
->lkey
)) {
147 off
= sge
->addr
- mr
->user_base
;
148 if (unlikely(sge
->addr
< mr
->user_base
||
149 off
+ sge
->length
> mr
->length
||
150 (mr
->access_flags
& acc
) != acc
)) {
159 while (off
>= mr
->map
[isge
->m
]->segs
[isge
->n
].length
) {
160 off
-= mr
->map
[isge
->m
]->segs
[isge
->n
].length
;
162 if (isge
->n
>= IPATH_SEGSZ
) {
167 isge
->vaddr
= mr
->map
[isge
->m
]->segs
[isge
->n
].vaddr
+ off
;
168 isge
->length
= mr
->map
[isge
->m
]->segs
[isge
->n
].length
- off
;
169 isge
->sge_length
= sge
->length
;
178 * ipath_rkey_ok - check the IB virtual address, length, and RKEY
179 * @dev: infiniband device
181 * @len: length of data
182 * @vaddr: virtual address to place data
183 * @rkey: rkey to check
186 * Return 1 if successful, otherwise 0.
188 * The QP r_rq.lock should be held.
190 int ipath_rkey_ok(struct ipath_ibdev
*dev
, struct ipath_sge_state
*ss
,
191 u32 len
, u64 vaddr
, u32 rkey
, int acc
)
193 struct ipath_lkey_table
*rkt
= &dev
->lk_table
;
194 struct ipath_sge
*sge
= &ss
->sge
;
195 struct ipath_mregion
*mr
;
199 spin_lock(&rkt
->lock
);
200 mr
= rkt
->table
[(rkey
>> (32 - ib_ipath_lkey_table_size
))];
201 spin_unlock(&rkt
->lock
);
202 if (unlikely(mr
== NULL
|| mr
->lkey
!= rkey
)) {
207 off
= vaddr
- mr
->iova
;
208 if (unlikely(vaddr
< mr
->iova
|| off
+ len
> mr
->length
||
209 (mr
->access_flags
& acc
) == 0)) {
218 while (off
>= mr
->map
[sge
->m
]->segs
[sge
->n
].length
) {
219 off
-= mr
->map
[sge
->m
]->segs
[sge
->n
].length
;
221 if (sge
->n
>= IPATH_SEGSZ
) {
226 sge
->vaddr
= mr
->map
[sge
->m
]->segs
[sge
->n
].vaddr
+ off
;
227 sge
->length
= mr
->map
[sge
->m
]->segs
[sge
->n
].length
- off
;
228 sge
->sge_length
= len
;