2 * Copyright (c) 2006 QLogic, Inc. 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
36 #include "ipath_verbs.h"
39 * ipath_alloc_lkey - allocate an lkey
40 * @rkt: lkey table in which to allocate the lkey
41 * @mr: memory region that this lkey protects
43 * Returns 1 if successful, otherwise returns 0.
46 int ipath_alloc_lkey(struct ipath_lkey_table *rkt, struct ipath_mregion *mr)
53 spin_lock_irqsave(&rkt->lock, flags);
55 /* Find the next available LKEY */
58 if (rkt->table[r] == NULL)
60 r = (r + 1) & (rkt->max - 1);
62 spin_unlock_irqrestore(&rkt->lock, flags);
63 _VERBS_INFO("LKEY table full\n");
68 rkt->next = (r + 1) & (rkt->max - 1);
70 * Make sure lkey is never zero which is reserved to indicate an
74 mr->lkey = (r << (32 - ib_ipath_lkey_table_size)) |
75 ((((1 << (24 - ib_ipath_lkey_table_size)) - 1) & rkt->gen)
82 spin_unlock_irqrestore(&rkt->lock, flags);
91 * ipath_free_lkey - free an lkey
92 * @rkt: table from which to free the lkey
93 * @lkey: lkey id to free
95 void ipath_free_lkey(struct ipath_lkey_table *rkt, u32 lkey)
102 r = lkey >> (32 - ib_ipath_lkey_table_size);
103 spin_lock_irqsave(&rkt->lock, flags);
104 rkt->table[r] = NULL;
105 spin_unlock_irqrestore(&rkt->lock, flags);
109 * ipath_lkey_ok - check IB SGE for validity and initialize
110 * @rkt: table containing lkey to check SGE against
111 * @isge: outgoing internal SGE
115 * Return 1 if valid and successful, otherwise returns 0.
117 * Check the IB SGE for validity and initialize our internal version
120 int ipath_lkey_ok(struct ipath_lkey_table *rkt, struct ipath_sge *isge,
121 struct ib_sge *sge, int acc)
123 struct ipath_mregion *mr;
128 * We use LKEY == zero to mean a physical kmalloc() address.
129 * This is a bit of a hack since we rely on dma_map_single()
130 * being reversible by calling bus_to_virt().
132 if (sge->lkey == 0) {
134 isge->vaddr = bus_to_virt(sge->addr);
135 isge->length = sge->length;
136 isge->sge_length = sge->length;
140 mr = rkt->table[(sge->lkey >> (32 - ib_ipath_lkey_table_size))];
141 if (unlikely(mr == NULL || mr->lkey != sge->lkey)) {
146 off = sge->addr - mr->user_base;
147 if (unlikely(sge->addr < mr->user_base ||
148 off + sge->length > mr->length ||
149 (mr->access_flags & acc) != acc)) {
158 while (off >= mr->map[isge->m]->segs[isge->n].length) {
159 off -= mr->map[isge->m]->segs[isge->n].length;
161 if (isge->n >= IPATH_SEGSZ) {
166 isge->vaddr = mr->map[isge->m]->segs[isge->n].vaddr + off;
167 isge->length = mr->map[isge->m]->segs[isge->n].length - off;
168 isge->sge_length = sge->length;
177 * ipath_rkey_ok - check the IB virtual address, length, and RKEY
178 * @dev: infiniband device
180 * @len: length of data
181 * @vaddr: virtual address to place data
182 * @rkey: rkey to check
185 * Return 1 if successful, otherwise 0.
187 int ipath_rkey_ok(struct ipath_ibdev *dev, struct ipath_sge_state *ss,
188 u32 len, u64 vaddr, u32 rkey, int acc)
190 struct ipath_lkey_table *rkt = &dev->lk_table;
191 struct ipath_sge *sge = &ss->sge;
192 struct ipath_mregion *mr;
196 mr = rkt->table[(rkey >> (32 - ib_ipath_lkey_table_size))];
197 if (unlikely(mr == NULL || mr->lkey != rkey)) {
202 off = vaddr - mr->iova;
203 if (unlikely(vaddr < mr->iova || off + len > mr->length ||
204 (mr->access_flags & acc) == 0)) {
213 while (off >= mr->map[sge->m]->segs[sge->n].length) {
214 off -= mr->map[sge->m]->segs[sge->n].length;
216 if (sge->n >= IPATH_SEGSZ) {
221 sge->vaddr = mr->map[sge->m]->segs[sge->n].vaddr + off;
222 sge->length = mr->map[sge->m]->segs[sge->n].length - off;
223 sge->sge_length = len;