]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/infiniband/hw/ipath/ipath_keys.c
8f32b17a5eed019d304a4b3c4283019274e69170
[net-next-2.6.git] / drivers / infiniband / hw / ipath / ipath_keys.c
1 /*
2  * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved.
3  * 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 #include "ipath_kernel.h"
38
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
47 int 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);
64                         ipath_dbg("LKEY table full\n");
65                         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
87 bail:
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  */
96 void 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  */
121 int ipath_lkey_ok(struct ipath_qp *qp, struct ipath_sge *isge,
122                   struct ib_sge *sge, int acc)
123 {
124         struct ipath_lkey_table *rkt = &to_idev(qp->ibqp.device)->lk_table;
125         struct ipath_mregion *mr;
126         unsigned n, m;
127         size_t off;
128         int ret;
129
130         /*
131          * We use LKEY == zero for kernel virtual addresses
132          * (see ipath_get_dma_mr and ipath_dma.c).
133          */
134         if (sge->lkey == 0) {
135                 struct ipath_pd *pd = to_ipd(qp->ibqp.pd);
136
137                 if (pd->user) {
138                         ret = 0;
139                         goto bail;
140                 }
141                 isge->mr = NULL;
142                 isge->vaddr = (void *) sge->addr;
143                 isge->length = sge->length;
144                 isge->sge_length = sge->length;
145                 ret = 1;
146                 goto bail;
147         }
148         mr = rkt->table[(sge->lkey >> (32 - ib_ipath_lkey_table_size))];
149         if (unlikely(mr == NULL || mr->lkey != sge->lkey ||
150                      qp->ibqp.pd != mr->pd)) {
151                 ret = 0;
152                 goto bail;
153         }
154
155         off = sge->addr - mr->user_base;
156         if (unlikely(sge->addr < mr->user_base ||
157                      off + sge->length > mr->length ||
158                      (mr->access_flags & acc) != acc)) {
159                 ret = 0;
160                 goto bail;
161         }
162
163         off += mr->offset;
164         m = 0;
165         n = 0;
166         while (off >= mr->map[m]->segs[n].length) {
167                 off -= mr->map[m]->segs[n].length;
168                 n++;
169                 if (n >= IPATH_SEGSZ) {
170                         m++;
171                         n = 0;
172                 }
173         }
174         isge->mr = mr;
175         isge->vaddr = mr->map[m]->segs[n].vaddr + off;
176         isge->length = mr->map[m]->segs[n].length - off;
177         isge->sge_length = sge->length;
178         isge->m = m;
179         isge->n = n;
180
181         ret = 1;
182
183 bail:
184         return ret;
185 }
186
187 /**
188  * ipath_rkey_ok - check the IB virtual address, length, and RKEY
189  * @dev: infiniband device
190  * @ss: SGE state
191  * @len: length of data
192  * @vaddr: virtual address to place data
193  * @rkey: rkey to check
194  * @acc: access flags
195  *
196  * Return 1 if successful, otherwise 0.
197  */
198 int ipath_rkey_ok(struct ipath_qp *qp, struct ipath_sge_state *ss,
199                   u32 len, u64 vaddr, u32 rkey, int acc)
200 {
201         struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
202         struct ipath_lkey_table *rkt = &dev->lk_table;
203         struct ipath_sge *sge = &ss->sge;
204         struct ipath_mregion *mr;
205         unsigned n, m;
206         size_t off;
207         int ret;
208
209         /*
210          * We use RKEY == zero for kernel virtual addresses
211          * (see ipath_get_dma_mr and ipath_dma.c).
212          */
213         if (rkey == 0) {
214                 struct ipath_pd *pd = to_ipd(qp->ibqp.pd);
215
216                 if (pd->user) {
217                         ret = 0;
218                         goto bail;
219                 }
220                 sge->mr = NULL;
221                 sge->vaddr = (void *) vaddr;
222                 sge->length = len;
223                 sge->sge_length = len;
224                 ss->sg_list = NULL;
225                 ss->num_sge = 1;
226                 ret = 1;
227                 goto bail;
228         }
229
230         mr = rkt->table[(rkey >> (32 - ib_ipath_lkey_table_size))];
231         if (unlikely(mr == NULL || mr->lkey != rkey ||
232                      qp->ibqp.pd != mr->pd)) {
233                 ret = 0;
234                 goto bail;
235         }
236
237         off = vaddr - mr->iova;
238         if (unlikely(vaddr < mr->iova || off + len > mr->length ||
239                      (mr->access_flags & acc) == 0)) {
240                 ret = 0;
241                 goto bail;
242         }
243
244         off += mr->offset;
245         m = 0;
246         n = 0;
247         while (off >= mr->map[m]->segs[n].length) {
248                 off -= mr->map[m]->segs[n].length;
249                 n++;
250                 if (n >= IPATH_SEGSZ) {
251                         m++;
252                         n = 0;
253                 }
254         }
255         sge->mr = mr;
256         sge->vaddr = mr->map[m]->segs[n].vaddr + off;
257         sge->length = mr->map[m]->segs[n].length - off;
258         sge->sge_length = len;
259         sge->m = m;
260         sge->n = n;
261         ss->sg_list = NULL;
262         ss->num_sge = 1;
263
264         ret = 1;
265
266 bail:
267         return ret;
268 }