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 mr = rkt->table[(sge->lkey >> (32 - ib_ipath_lkey_table_size))];
140 if (unlikely(mr == NULL || mr->lkey != sge->lkey)) {
145 off = sge->addr - mr->user_base;
146 if (unlikely(sge->addr < mr->user_base ||
147 off + sge->length > mr->length ||
148 (mr->access_flags & acc) != acc)) {
157 while (off >= mr->map[isge->m]->segs[isge->n].length) {
158 off -= mr->map[isge->m]->segs[isge->n].length;
160 if (isge->n >= IPATH_SEGSZ) {
165 isge->vaddr = mr->map[isge->m]->segs[isge->n].vaddr + off;
166 isge->length = mr->map[isge->m]->segs[isge->n].length - off;
167 isge->sge_length = sge->length;
176 * ipath_rkey_ok - check the IB virtual address, length, and RKEY
177 * @dev: infiniband device
179 * @len: length of data
180 * @vaddr: virtual address to place data
181 * @rkey: rkey to check
184 * Return 1 if successful, otherwise 0.
186 int ipath_rkey_ok(struct ipath_ibdev *dev, struct ipath_sge_state *ss,
187 u32 len, u64 vaddr, u32 rkey, int acc)
189 struct ipath_lkey_table *rkt = &dev->lk_table;
190 struct ipath_sge *sge = &ss->sge;
191 struct ipath_mregion *mr;
195 mr = rkt->table[(rkey >> (32 - ib_ipath_lkey_table_size))];
196 if (unlikely(mr == NULL || mr->lkey != rkey)) {
201 off = vaddr - mr->iova;
202 if (unlikely(vaddr < mr->iova || off + len > mr->length ||
203 (mr->access_flags & acc) == 0)) {
212 while (off >= mr->map[sge->m]->segs[sge->n].length) {
213 off -= mr->map[sge->m]->segs[sge->n].length;
215 if (sge->n >= IPATH_SEGSZ) {
220 sge->vaddr = mr->map[sge->m]->segs[sge->n].vaddr + off;
221 sge->length = mr->map[sge->m]->segs[sge->n].length - off;
222 sge->sge_length = len;