blob: a5ca279370aa2a34004cf3ea3f88043985987b10 [file] [log] [blame]
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -08001/*
Bryan O'Sullivan759d5762006-07-01 04:35:49 -07002 * Copyright (c) 2006 QLogic, Inc. All rights reserved.
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -08003 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
4 *
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:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
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.
23 *
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
31 * SOFTWARE.
32 */
33
34#include <asm/io.h>
35
36#include "ipath_verbs.h"
37
38/**
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
42 *
43 * Returns 1 if successful, otherwise returns 0.
44 */
45
46int ipath_alloc_lkey(struct ipath_lkey_table *rkt, struct ipath_mregion *mr)
47{
48 unsigned long flags;
49 u32 r;
50 u32 n;
51 int ret;
52
53 spin_lock_irqsave(&rkt->lock, flags);
54
55 /* Find the next available LKEY */
56 r = n = rkt->next;
57 for (;;) {
58 if (rkt->table[r] == NULL)
59 break;
60 r = (r + 1) & (rkt->max - 1);
61 if (r == n) {
62 spin_unlock_irqrestore(&rkt->lock, flags);
63 _VERBS_INFO("LKEY table full\n");
64 ret = 0;
65 goto bail;
66 }
67 }
68 rkt->next = (r + 1) & (rkt->max - 1);
69 /*
70 * Make sure lkey is never zero which is reserved to indicate an
71 * unrestricted LKEY.
72 */
73 rkt->gen++;
74 mr->lkey = (r << (32 - ib_ipath_lkey_table_size)) |
75 ((((1 << (24 - ib_ipath_lkey_table_size)) - 1) & rkt->gen)
76 << 8);
77 if (mr->lkey == 0) {
78 mr->lkey |= 1 << 8;
79 rkt->gen++;
80 }
81 rkt->table[r] = mr;
82 spin_unlock_irqrestore(&rkt->lock, flags);
83
84 ret = 1;
85
86bail:
87 return ret;
88}
89
90/**
91 * ipath_free_lkey - free an lkey
92 * @rkt: table from which to free the lkey
93 * @lkey: lkey id to free
94 */
95void ipath_free_lkey(struct ipath_lkey_table *rkt, u32 lkey)
96{
97 unsigned long flags;
98 u32 r;
99
100 if (lkey == 0)
101 return;
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);
106}
107
108/**
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
112 * @sge: SGE to check
113 * @acc: access flags
114 *
115 * Return 1 if valid and successful, otherwise returns 0.
116 *
117 * Check the IB SGE for validity and initialize our internal version
118 * of it.
119 */
120int ipath_lkey_ok(struct ipath_lkey_table *rkt, struct ipath_sge *isge,
121 struct ib_sge *sge, int acc)
122{
123 struct ipath_mregion *mr;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700124 unsigned n, m;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800125 size_t off;
126 int ret;
127
128 /*
129 * We use LKEY == zero to mean a physical kmalloc() address.
130 * This is a bit of a hack since we rely on dma_map_single()
131 * being reversible by calling bus_to_virt().
132 */
133 if (sge->lkey == 0) {
134 isge->mr = NULL;
135 isge->vaddr = bus_to_virt(sge->addr);
136 isge->length = sge->length;
137 isge->sge_length = sge->length;
138 ret = 1;
139 goto bail;
140 }
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800141 mr = rkt->table[(sge->lkey >> (32 - ib_ipath_lkey_table_size))];
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800142 if (unlikely(mr == NULL || mr->lkey != sge->lkey)) {
143 ret = 0;
144 goto bail;
145 }
146
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)) {
151 ret = 0;
152 goto bail;
153 }
154
155 off += mr->offset;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700156 m = 0;
157 n = 0;
158 while (off >= mr->map[m]->segs[n].length) {
159 off -= mr->map[m]->segs[n].length;
160 n++;
161 if (n >= IPATH_SEGSZ) {
162 m++;
163 n = 0;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800164 }
165 }
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700166 isge->mr = mr;
167 isge->vaddr = mr->map[m]->segs[n].vaddr + off;
168 isge->length = mr->map[m]->segs[n].length - off;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800169 isge->sge_length = sge->length;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700170 isge->m = m;
171 isge->n = n;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800172
173 ret = 1;
174
175bail:
176 return ret;
177}
178
179/**
180 * ipath_rkey_ok - check the IB virtual address, length, and RKEY
181 * @dev: infiniband device
182 * @ss: SGE state
183 * @len: length of data
184 * @vaddr: virtual address to place data
185 * @rkey: rkey to check
186 * @acc: access flags
187 *
188 * Return 1 if successful, otherwise 0.
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800189 */
190int ipath_rkey_ok(struct ipath_ibdev *dev, struct ipath_sge_state *ss,
191 u32 len, u64 vaddr, u32 rkey, int acc)
192{
193 struct ipath_lkey_table *rkt = &dev->lk_table;
194 struct ipath_sge *sge = &ss->sge;
195 struct ipath_mregion *mr;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700196 unsigned n, m;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800197 size_t off;
198 int ret;
199
Ralph Campbellc9f79bd2006-07-17 18:19:54 -0700200 /*
201 * We use RKEY == zero for physical addresses
202 * (see ipath_get_dma_mr).
203 */
204 if (rkey == 0) {
205 sge->mr = NULL;
206 sge->vaddr = phys_to_virt(vaddr);
207 sge->length = len;
208 sge->sge_length = len;
209 ss->sg_list = NULL;
210 ss->num_sge = 1;
211 ret = 1;
212 goto bail;
213 }
214
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800215 mr = rkt->table[(rkey >> (32 - ib_ipath_lkey_table_size))];
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800216 if (unlikely(mr == NULL || mr->lkey != rkey)) {
217 ret = 0;
218 goto bail;
219 }
220
221 off = vaddr - mr->iova;
222 if (unlikely(vaddr < mr->iova || off + len > mr->length ||
223 (mr->access_flags & acc) == 0)) {
224 ret = 0;
225 goto bail;
226 }
227
228 off += mr->offset;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700229 m = 0;
230 n = 0;
231 while (off >= mr->map[m]->segs[n].length) {
232 off -= mr->map[m]->segs[n].length;
233 n++;
234 if (n >= IPATH_SEGSZ) {
235 m++;
236 n = 0;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800237 }
238 }
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700239 sge->mr = mr;
240 sge->vaddr = mr->map[m]->segs[n].vaddr + off;
241 sge->length = mr->map[m]->segs[n].length - off;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800242 sge->sge_length = len;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700243 sge->m = m;
244 sge->n = n;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800245 ss->sg_list = NULL;
246 ss->num_sge = 1;
247
248 ret = 1;
249
250bail:
251 return ret;
252}