blob: 5ae8761f9dd2e8b3ed5dc86430ac9617c251aab9 [file] [log] [blame]
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -08001/*
2 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
3 *
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:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
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.
22 *
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
30 * SOFTWARE.
31 */
32
33#include <asm/io.h>
34
35#include "ipath_verbs.h"
36
37/**
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
41 *
42 * Returns 1 if successful, otherwise returns 0.
43 */
44
45int ipath_alloc_lkey(struct ipath_lkey_table *rkt, struct ipath_mregion *mr)
46{
47 unsigned long flags;
48 u32 r;
49 u32 n;
50 int ret;
51
52 spin_lock_irqsave(&rkt->lock, flags);
53
54 /* Find the next available LKEY */
55 r = n = rkt->next;
56 for (;;) {
57 if (rkt->table[r] == NULL)
58 break;
59 r = (r + 1) & (rkt->max - 1);
60 if (r == n) {
61 spin_unlock_irqrestore(&rkt->lock, flags);
62 _VERBS_INFO("LKEY table full\n");
63 ret = 0;
64 goto bail;
65 }
66 }
67 rkt->next = (r + 1) & (rkt->max - 1);
68 /*
69 * Make sure lkey is never zero which is reserved to indicate an
70 * unrestricted LKEY.
71 */
72 rkt->gen++;
73 mr->lkey = (r << (32 - ib_ipath_lkey_table_size)) |
74 ((((1 << (24 - ib_ipath_lkey_table_size)) - 1) & rkt->gen)
75 << 8);
76 if (mr->lkey == 0) {
77 mr->lkey |= 1 << 8;
78 rkt->gen++;
79 }
80 rkt->table[r] = mr;
81 spin_unlock_irqrestore(&rkt->lock, flags);
82
83 ret = 1;
84
85bail:
86 return ret;
87}
88
89/**
90 * ipath_free_lkey - free an lkey
91 * @rkt: table from which to free the lkey
92 * @lkey: lkey id to free
93 */
94void ipath_free_lkey(struct ipath_lkey_table *rkt, u32 lkey)
95{
96 unsigned long flags;
97 u32 r;
98
99 if (lkey == 0)
100 return;
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);
105}
106
107/**
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
111 * @sge: SGE to check
112 * @acc: access flags
113 *
114 * Return 1 if valid and successful, otherwise returns 0.
115 *
116 * Check the IB SGE for validity and initialize our internal version
117 * of it.
118 */
119int ipath_lkey_ok(struct ipath_lkey_table *rkt, struct ipath_sge *isge,
120 struct ib_sge *sge, int acc)
121{
122 struct ipath_mregion *mr;
123 size_t off;
124 int ret;
125
126 /*
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().
130 */
131 if (sge->lkey == 0) {
132 isge->mr = NULL;
133 isge->vaddr = bus_to_virt(sge->addr);
134 isge->length = sge->length;
135 isge->sge_length = sge->length;
136 ret = 1;
137 goto bail;
138 }
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800139 mr = rkt->table[(sge->lkey >> (32 - ib_ipath_lkey_table_size))];
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800140 if (unlikely(mr == NULL || mr->lkey != sge->lkey)) {
141 ret = 0;
142 goto bail;
143 }
144
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)) {
149 ret = 0;
150 goto bail;
151 }
152
153 off += mr->offset;
154 isge->mr = mr;
155 isge->m = 0;
156 isge->n = 0;
157 while (off >= mr->map[isge->m]->segs[isge->n].length) {
158 off -= mr->map[isge->m]->segs[isge->n].length;
159 isge->n++;
160 if (isge->n >= IPATH_SEGSZ) {
161 isge->m++;
162 isge->n = 0;
163 }
164 }
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;
168
169 ret = 1;
170
171bail:
172 return ret;
173}
174
175/**
176 * ipath_rkey_ok - check the IB virtual address, length, and RKEY
177 * @dev: infiniband device
178 * @ss: SGE state
179 * @len: length of data
180 * @vaddr: virtual address to place data
181 * @rkey: rkey to check
182 * @acc: access flags
183 *
184 * Return 1 if successful, otherwise 0.
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800185 */
186int ipath_rkey_ok(struct ipath_ibdev *dev, struct ipath_sge_state *ss,
187 u32 len, u64 vaddr, u32 rkey, int acc)
188{
189 struct ipath_lkey_table *rkt = &dev->lk_table;
190 struct ipath_sge *sge = &ss->sge;
191 struct ipath_mregion *mr;
192 size_t off;
193 int ret;
194
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800195 mr = rkt->table[(rkey >> (32 - ib_ipath_lkey_table_size))];
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800196 if (unlikely(mr == NULL || mr->lkey != rkey)) {
197 ret = 0;
198 goto bail;
199 }
200
201 off = vaddr - mr->iova;
202 if (unlikely(vaddr < mr->iova || off + len > mr->length ||
203 (mr->access_flags & acc) == 0)) {
204 ret = 0;
205 goto bail;
206 }
207
208 off += mr->offset;
209 sge->mr = mr;
210 sge->m = 0;
211 sge->n = 0;
212 while (off >= mr->map[sge->m]->segs[sge->n].length) {
213 off -= mr->map[sge->m]->segs[sge->n].length;
214 sge->n++;
215 if (sge->n >= IPATH_SEGSZ) {
216 sge->m++;
217 sge->n = 0;
218 }
219 }
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;
223 ss->sg_list = NULL;
224 ss->num_sge = 1;
225
226 ret = 1;
227
228bail:
229 return ret;
230}