Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / infiniband / hw / ipath / ipath_mr.c
1 /*
2  * Copyright (c) 2006 QLogic, Inc. 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 <rdma/ib_pack.h>
35 #include <rdma/ib_smi.h>
36
37 #include "ipath_verbs.h"
38
39 /**
40  * ipath_get_dma_mr - get a DMA memory region
41  * @pd: protection domain for this memory region
42  * @acc: access flags
43  *
44  * Returns the memory region on success, otherwise returns an errno.
45  */
46 struct ib_mr *ipath_get_dma_mr(struct ib_pd *pd, int acc)
47 {
48         struct ipath_mr *mr;
49         struct ib_mr *ret;
50
51         mr = kzalloc(sizeof *mr, GFP_KERNEL);
52         if (!mr) {
53                 ret = ERR_PTR(-ENOMEM);
54                 goto bail;
55         }
56
57         mr->mr.access_flags = acc;
58         ret = &mr->ibmr;
59
60 bail:
61         return ret;
62 }
63
64 static struct ipath_mr *alloc_mr(int count,
65                                  struct ipath_lkey_table *lk_table)
66 {
67         struct ipath_mr *mr;
68         int m, i = 0;
69
70         /* Allocate struct plus pointers to first level page tables. */
71         m = (count + IPATH_SEGSZ - 1) / IPATH_SEGSZ;
72         mr = kmalloc(sizeof *mr + m * sizeof mr->mr.map[0], GFP_KERNEL);
73         if (!mr)
74                 goto done;
75
76         /* Allocate first level page tables. */
77         for (; i < m; i++) {
78                 mr->mr.map[i] = kmalloc(sizeof *mr->mr.map[0], GFP_KERNEL);
79                 if (!mr->mr.map[i])
80                         goto bail;
81         }
82         mr->mr.mapsz = m;
83
84         /*
85          * ib_reg_phys_mr() will initialize mr->ibmr except for
86          * lkey and rkey.
87          */
88         if (!ipath_alloc_lkey(lk_table, &mr->mr))
89                 goto bail;
90         mr->ibmr.rkey = mr->ibmr.lkey = mr->mr.lkey;
91
92         goto done;
93
94 bail:
95         while (i) {
96                 i--;
97                 kfree(mr->mr.map[i]);
98         }
99         kfree(mr);
100         mr = NULL;
101
102 done:
103         return mr;
104 }
105
106 /**
107  * ipath_reg_phys_mr - register a physical memory region
108  * @pd: protection domain for this memory region
109  * @buffer_list: pointer to the list of physical buffers to register
110  * @num_phys_buf: the number of physical buffers to register
111  * @iova_start: the starting address passed over IB which maps to this MR
112  *
113  * Returns the memory region on success, otherwise returns an errno.
114  */
115 struct ib_mr *ipath_reg_phys_mr(struct ib_pd *pd,
116                                 struct ib_phys_buf *buffer_list,
117                                 int num_phys_buf, int acc, u64 *iova_start)
118 {
119         struct ipath_mr *mr;
120         int n, m, i;
121         struct ib_mr *ret;
122
123         mr = alloc_mr(num_phys_buf, &to_idev(pd->device)->lk_table);
124         if (mr == NULL) {
125                 ret = ERR_PTR(-ENOMEM);
126                 goto bail;
127         }
128
129         mr->mr.user_base = *iova_start;
130         mr->mr.iova = *iova_start;
131         mr->mr.length = 0;
132         mr->mr.offset = 0;
133         mr->mr.access_flags = acc;
134         mr->mr.max_segs = num_phys_buf;
135
136         m = 0;
137         n = 0;
138         for (i = 0; i < num_phys_buf; i++) {
139                 mr->mr.map[m]->segs[n].vaddr =
140                         phys_to_virt(buffer_list[i].addr);
141                 mr->mr.map[m]->segs[n].length = buffer_list[i].size;
142                 mr->mr.length += buffer_list[i].size;
143                 n++;
144                 if (n == IPATH_SEGSZ) {
145                         m++;
146                         n = 0;
147                 }
148         }
149
150         ret = &mr->ibmr;
151
152 bail:
153         return ret;
154 }
155
156 /**
157  * ipath_reg_user_mr - register a userspace memory region
158  * @pd: protection domain for this memory region
159  * @region: the user memory region
160  * @mr_access_flags: access flags for this memory region
161  * @udata: unused by the InfiniPath driver
162  *
163  * Returns the memory region on success, otherwise returns an errno.
164  */
165 struct ib_mr *ipath_reg_user_mr(struct ib_pd *pd, struct ib_umem *region,
166                                 int mr_access_flags, struct ib_udata *udata)
167 {
168         struct ipath_mr *mr;
169         struct ib_umem_chunk *chunk;
170         int n, m, i;
171         struct ib_mr *ret;
172
173         if (region->length == 0) {
174                 ret = ERR_PTR(-EINVAL);
175                 goto bail;
176         }
177
178         n = 0;
179         list_for_each_entry(chunk, &region->chunk_list, list)
180                 n += chunk->nents;
181
182         mr = alloc_mr(n, &to_idev(pd->device)->lk_table);
183         if (!mr) {
184                 ret = ERR_PTR(-ENOMEM);
185                 goto bail;
186         }
187
188         mr->mr.user_base = region->user_base;
189         mr->mr.iova = region->virt_base;
190         mr->mr.length = region->length;
191         mr->mr.offset = region->offset;
192         mr->mr.access_flags = mr_access_flags;
193         mr->mr.max_segs = n;
194
195         m = 0;
196         n = 0;
197         list_for_each_entry(chunk, &region->chunk_list, list) {
198                 for (i = 0; i < chunk->nmap; i++) {
199                         mr->mr.map[m]->segs[n].vaddr =
200                                 page_address(chunk->page_list[i].page);
201                         mr->mr.map[m]->segs[n].length = region->page_size;
202                         n++;
203                         if (n == IPATH_SEGSZ) {
204                                 m++;
205                                 n = 0;
206                         }
207                 }
208         }
209         ret = &mr->ibmr;
210
211 bail:
212         return ret;
213 }
214
215 /**
216  * ipath_dereg_mr - unregister and free a memory region
217  * @ibmr: the memory region to free
218  *
219  * Returns 0 on success.
220  *
221  * Note that this is called to free MRs created by ipath_get_dma_mr()
222  * or ipath_reg_user_mr().
223  */
224 int ipath_dereg_mr(struct ib_mr *ibmr)
225 {
226         struct ipath_mr *mr = to_imr(ibmr);
227         int i;
228
229         ipath_free_lkey(&to_idev(ibmr->device)->lk_table, ibmr->lkey);
230         i = mr->mr.mapsz;
231         while (i) {
232                 i--;
233                 kfree(mr->mr.map[i]);
234         }
235         kfree(mr);
236         return 0;
237 }
238
239 /**
240  * ipath_alloc_fmr - allocate a fast memory region
241  * @pd: the protection domain for this memory region
242  * @mr_access_flags: access flags for this memory region
243  * @fmr_attr: fast memory region attributes
244  *
245  * Returns the memory region on success, otherwise returns an errno.
246  */
247 struct ib_fmr *ipath_alloc_fmr(struct ib_pd *pd, int mr_access_flags,
248                                struct ib_fmr_attr *fmr_attr)
249 {
250         struct ipath_fmr *fmr;
251         int m, i = 0;
252         struct ib_fmr *ret;
253
254         /* Allocate struct plus pointers to first level page tables. */
255         m = (fmr_attr->max_pages + IPATH_SEGSZ - 1) / IPATH_SEGSZ;
256         fmr = kmalloc(sizeof *fmr + m * sizeof fmr->mr.map[0], GFP_KERNEL);
257         if (!fmr)
258                 goto bail;
259
260         /* Allocate first level page tables. */
261         for (; i < m; i++) {
262                 fmr->mr.map[i] = kmalloc(sizeof *fmr->mr.map[0],
263                                          GFP_KERNEL);
264                 if (!fmr->mr.map[i])
265                         goto bail;
266         }
267         fmr->mr.mapsz = m;
268
269         /*
270          * ib_alloc_fmr() will initialize fmr->ibfmr except for lkey &
271          * rkey.
272          */
273         if (!ipath_alloc_lkey(&to_idev(pd->device)->lk_table, &fmr->mr))
274                 goto bail;
275         fmr->ibfmr.rkey = fmr->ibfmr.lkey = fmr->mr.lkey;
276         /*
277          * Resources are allocated but no valid mapping (RKEY can't be
278          * used).
279          */
280         fmr->mr.user_base = 0;
281         fmr->mr.iova = 0;
282         fmr->mr.length = 0;
283         fmr->mr.offset = 0;
284         fmr->mr.access_flags = mr_access_flags;
285         fmr->mr.max_segs = fmr_attr->max_pages;
286         fmr->page_shift = fmr_attr->page_shift;
287
288         ret = &fmr->ibfmr;
289         goto done;
290
291 bail:
292         while (i)
293                 kfree(fmr->mr.map[--i]);
294         kfree(fmr);
295         ret = ERR_PTR(-ENOMEM);
296
297 done:
298         return ret;
299 }
300
301 /**
302  * ipath_map_phys_fmr - set up a fast memory region
303  * @ibmfr: the fast memory region to set up
304  * @page_list: the list of pages to associate with the fast memory region
305  * @list_len: the number of pages to associate with the fast memory region
306  * @iova: the virtual address of the start of the fast memory region
307  *
308  * This may be called from interrupt context.
309  */
310
311 int ipath_map_phys_fmr(struct ib_fmr *ibfmr, u64 * page_list,
312                        int list_len, u64 iova)
313 {
314         struct ipath_fmr *fmr = to_ifmr(ibfmr);
315         struct ipath_lkey_table *rkt;
316         unsigned long flags;
317         int m, n, i;
318         u32 ps;
319         int ret;
320
321         if (list_len > fmr->mr.max_segs) {
322                 ret = -EINVAL;
323                 goto bail;
324         }
325         rkt = &to_idev(ibfmr->device)->lk_table;
326         spin_lock_irqsave(&rkt->lock, flags);
327         fmr->mr.user_base = iova;
328         fmr->mr.iova = iova;
329         ps = 1 << fmr->page_shift;
330         fmr->mr.length = list_len * ps;
331         m = 0;
332         n = 0;
333         ps = 1 << fmr->page_shift;
334         for (i = 0; i < list_len; i++) {
335                 fmr->mr.map[m]->segs[n].vaddr = phys_to_virt(page_list[i]);
336                 fmr->mr.map[m]->segs[n].length = ps;
337                 if (++n == IPATH_SEGSZ) {
338                         m++;
339                         n = 0;
340                 }
341         }
342         spin_unlock_irqrestore(&rkt->lock, flags);
343         ret = 0;
344
345 bail:
346         return ret;
347 }
348
349 /**
350  * ipath_unmap_fmr - unmap fast memory regions
351  * @fmr_list: the list of fast memory regions to unmap
352  *
353  * Returns 0 on success.
354  */
355 int ipath_unmap_fmr(struct list_head *fmr_list)
356 {
357         struct ipath_fmr *fmr;
358         struct ipath_lkey_table *rkt;
359         unsigned long flags;
360
361         list_for_each_entry(fmr, fmr_list, ibfmr.list) {
362                 rkt = &to_idev(fmr->ibfmr.device)->lk_table;
363                 spin_lock_irqsave(&rkt->lock, flags);
364                 fmr->mr.user_base = 0;
365                 fmr->mr.iova = 0;
366                 fmr->mr.length = 0;
367                 spin_unlock_irqrestore(&rkt->lock, flags);
368         }
369         return 0;
370 }
371
372 /**
373  * ipath_dealloc_fmr - deallocate a fast memory region
374  * @ibfmr: the fast memory region to deallocate
375  *
376  * Returns 0 on success.
377  */
378 int ipath_dealloc_fmr(struct ib_fmr *ibfmr)
379 {
380         struct ipath_fmr *fmr = to_ifmr(ibfmr);
381         int i;
382
383         ipath_free_lkey(&to_idev(ibfmr->device)->lk_table, ibfmr->lkey);
384         i = fmr->mr.mapsz;
385         while (i)
386                 kfree(fmr->mr.map[--i]);
387         kfree(fmr);
388         return 0;
389 }