blob: c93fa2f7719c32b298eff5fda04035db821f1df0 [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"
Bryan O'Sullivanb55f4f02006-08-25 11:24:33 -070037#include "ipath_kernel.h"
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080038
39/**
40 * ipath_alloc_lkey - allocate an lkey
41 * @rkt: lkey table in which to allocate the lkey
42 * @mr: memory region that this lkey protects
43 *
44 * Returns 1 if successful, otherwise returns 0.
45 */
46
47int ipath_alloc_lkey(struct ipath_lkey_table *rkt, struct ipath_mregion *mr)
48{
49 unsigned long flags;
50 u32 r;
51 u32 n;
52 int ret;
53
54 spin_lock_irqsave(&rkt->lock, flags);
55
56 /* Find the next available LKEY */
57 r = n = rkt->next;
58 for (;;) {
59 if (rkt->table[r] == NULL)
60 break;
61 r = (r + 1) & (rkt->max - 1);
62 if (r == n) {
63 spin_unlock_irqrestore(&rkt->lock, flags);
Bryan O'Sullivan39c0d0b2007-03-15 14:44:52 -070064 ipath_dbg("LKEY table full\n");
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080065 ret = 0;
66 goto bail;
67 }
68 }
69 rkt->next = (r + 1) & (rkt->max - 1);
70 /*
71 * Make sure lkey is never zero which is reserved to indicate an
72 * unrestricted LKEY.
73 */
74 rkt->gen++;
75 mr->lkey = (r << (32 - ib_ipath_lkey_table_size)) |
76 ((((1 << (24 - ib_ipath_lkey_table_size)) - 1) & rkt->gen)
77 << 8);
78 if (mr->lkey == 0) {
79 mr->lkey |= 1 << 8;
80 rkt->gen++;
81 }
82 rkt->table[r] = mr;
83 spin_unlock_irqrestore(&rkt->lock, flags);
84
85 ret = 1;
86
87bail:
88 return ret;
89}
90
91/**
92 * ipath_free_lkey - free an lkey
93 * @rkt: table from which to free the lkey
94 * @lkey: lkey id to free
95 */
96void ipath_free_lkey(struct ipath_lkey_table *rkt, u32 lkey)
97{
98 unsigned long flags;
99 u32 r;
100
101 if (lkey == 0)
102 return;
103 r = lkey >> (32 - ib_ipath_lkey_table_size);
104 spin_lock_irqsave(&rkt->lock, flags);
105 rkt->table[r] = NULL;
106 spin_unlock_irqrestore(&rkt->lock, flags);
107}
108
109/**
110 * ipath_lkey_ok - check IB SGE for validity and initialize
111 * @rkt: table containing lkey to check SGE against
112 * @isge: outgoing internal SGE
113 * @sge: SGE to check
114 * @acc: access flags
115 *
116 * Return 1 if valid and successful, otherwise returns 0.
117 *
118 * Check the IB SGE for validity and initialize our internal version
119 * of it.
120 */
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -0700121int ipath_lkey_ok(struct ipath_qp *qp, struct ipath_sge *isge,
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800122 struct ib_sge *sge, int acc)
123{
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -0700124 struct ipath_lkey_table *rkt = &to_idev(qp->ibqp.device)->lk_table;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800125 struct ipath_mregion *mr;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700126 unsigned n, m;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800127 size_t off;
128 int ret;
129
130 /*
131 * We use LKEY == zero to mean a physical kmalloc() address.
132 * This is a bit of a hack since we rely on dma_map_single()
133 * being reversible by calling bus_to_virt().
134 */
135 if (sge->lkey == 0) {
136 isge->mr = NULL;
Ralph Campbellf2cbb662006-12-12 14:28:28 -0800137 isge->vaddr = (void *) sge->addr;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800138 isge->length = sge->length;
139 isge->sge_length = sge->length;
140 ret = 1;
141 goto bail;
142 }
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800143 mr = rkt->table[(sge->lkey >> (32 - ib_ipath_lkey_table_size))];
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -0700144 if (unlikely(mr == NULL || mr->lkey != sge->lkey ||
145 qp->ibqp.pd != mr->pd)) {
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800146 ret = 0;
147 goto bail;
148 }
149
150 off = sge->addr - mr->user_base;
151 if (unlikely(sge->addr < mr->user_base ||
152 off + sge->length > mr->length ||
153 (mr->access_flags & acc) != acc)) {
154 ret = 0;
155 goto bail;
156 }
157
158 off += mr->offset;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700159 m = 0;
160 n = 0;
161 while (off >= mr->map[m]->segs[n].length) {
162 off -= mr->map[m]->segs[n].length;
163 n++;
164 if (n >= IPATH_SEGSZ) {
165 m++;
166 n = 0;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800167 }
168 }
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700169 isge->mr = mr;
170 isge->vaddr = mr->map[m]->segs[n].vaddr + off;
171 isge->length = mr->map[m]->segs[n].length - off;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800172 isge->sge_length = sge->length;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700173 isge->m = m;
174 isge->n = n;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800175
176 ret = 1;
177
178bail:
179 return ret;
180}
181
182/**
183 * ipath_rkey_ok - check the IB virtual address, length, and RKEY
184 * @dev: infiniband device
185 * @ss: SGE state
186 * @len: length of data
187 * @vaddr: virtual address to place data
188 * @rkey: rkey to check
189 * @acc: access flags
190 *
191 * Return 1 if successful, otherwise 0.
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800192 */
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -0700193int ipath_rkey_ok(struct ipath_qp *qp, struct ipath_sge_state *ss,
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800194 u32 len, u64 vaddr, u32 rkey, int acc)
195{
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -0700196 struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800197 struct ipath_lkey_table *rkt = &dev->lk_table;
198 struct ipath_sge *sge = &ss->sge;
199 struct ipath_mregion *mr;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700200 unsigned n, m;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800201 size_t off;
202 int ret;
203
Ralph Campbellc9f79bd2006-07-17 18:19:54 -0700204 /*
Ralph Campbellf2cbb662006-12-12 14:28:28 -0800205 * We use RKEY == zero for kernel virtual addresses
206 * (see ipath_get_dma_mr and ipath_dma.c).
Ralph Campbellc9f79bd2006-07-17 18:19:54 -0700207 */
208 if (rkey == 0) {
209 sge->mr = NULL;
Ralph Campbellf2cbb662006-12-12 14:28:28 -0800210 sge->vaddr = (void *) vaddr;
Ralph Campbellc9f79bd2006-07-17 18:19:54 -0700211 sge->length = len;
212 sge->sge_length = len;
213 ss->sg_list = NULL;
214 ss->num_sge = 1;
215 ret = 1;
216 goto bail;
217 }
218
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800219 mr = rkt->table[(rkey >> (32 - ib_ipath_lkey_table_size))];
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -0700220 if (unlikely(mr == NULL || mr->lkey != rkey ||
221 qp->ibqp.pd != mr->pd)) {
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800222 ret = 0;
223 goto bail;
224 }
225
226 off = vaddr - mr->iova;
227 if (unlikely(vaddr < mr->iova || off + len > mr->length ||
228 (mr->access_flags & acc) == 0)) {
229 ret = 0;
230 goto bail;
231 }
232
233 off += mr->offset;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700234 m = 0;
235 n = 0;
236 while (off >= mr->map[m]->segs[n].length) {
237 off -= mr->map[m]->segs[n].length;
238 n++;
239 if (n >= IPATH_SEGSZ) {
240 m++;
241 n = 0;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800242 }
243 }
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700244 sge->mr = mr;
245 sge->vaddr = mr->map[m]->segs[n].vaddr + off;
246 sge->length = mr->map[m]->segs[n].length - off;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800247 sge->sge_length = len;
Bryan O'Sullivan12eef412006-07-01 04:36:10 -0700248 sge->m = m;
249 sge->n = n;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800250 ss->sg_list = NULL;
251 ss->num_sge = 1;
252
253 ret = 1;
254
255bail:
256 return ret;
257}