Linux 2.6.21
[linux/fpc-iii.git] / drivers / infiniband / hw / ipath / ipath_mr.c
blob8cc8598d6c6968cbd2a957ec4ccd2c3d41b4ea92
1 /*
2 * Copyright (c) 2006 QLogic, Inc. All rights reserved.
3 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
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.
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.
34 #include <rdma/ib_pack.h>
35 #include <rdma/ib_smi.h>
37 #include "ipath_verbs.h"
39 /* Fast memory region */
40 struct ipath_fmr {
41 struct ib_fmr ibfmr;
42 u8 page_shift;
43 struct ipath_mregion mr; /* must be last */
46 static inline struct ipath_fmr *to_ifmr(struct ib_fmr *ibfmr)
48 return container_of(ibfmr, struct ipath_fmr, ibfmr);
51 /**
52 * ipath_get_dma_mr - get a DMA memory region
53 * @pd: protection domain for this memory region
54 * @acc: access flags
56 * Returns the memory region on success, otherwise returns an errno.
57 * Note that all DMA addresses should be created via the
58 * struct ib_dma_mapping_ops functions (see ipath_dma.c).
60 struct ib_mr *ipath_get_dma_mr(struct ib_pd *pd, int acc)
62 struct ipath_mr *mr;
63 struct ib_mr *ret;
65 mr = kzalloc(sizeof *mr, GFP_KERNEL);
66 if (!mr) {
67 ret = ERR_PTR(-ENOMEM);
68 goto bail;
71 mr->mr.access_flags = acc;
72 ret = &mr->ibmr;
74 bail:
75 return ret;
78 static struct ipath_mr *alloc_mr(int count,
79 struct ipath_lkey_table *lk_table)
81 struct ipath_mr *mr;
82 int m, i = 0;
84 /* Allocate struct plus pointers to first level page tables. */
85 m = (count + IPATH_SEGSZ - 1) / IPATH_SEGSZ;
86 mr = kmalloc(sizeof *mr + m * sizeof mr->mr.map[0], GFP_KERNEL);
87 if (!mr)
88 goto done;
90 /* Allocate first level page tables. */
91 for (; i < m; i++) {
92 mr->mr.map[i] = kmalloc(sizeof *mr->mr.map[0], GFP_KERNEL);
93 if (!mr->mr.map[i])
94 goto bail;
96 mr->mr.mapsz = m;
99 * ib_reg_phys_mr() will initialize mr->ibmr except for
100 * lkey and rkey.
102 if (!ipath_alloc_lkey(lk_table, &mr->mr))
103 goto bail;
104 mr->ibmr.rkey = mr->ibmr.lkey = mr->mr.lkey;
106 goto done;
108 bail:
109 while (i) {
110 i--;
111 kfree(mr->mr.map[i]);
113 kfree(mr);
114 mr = NULL;
116 done:
117 return mr;
121 * ipath_reg_phys_mr - register a physical memory region
122 * @pd: protection domain for this memory region
123 * @buffer_list: pointer to the list of physical buffers to register
124 * @num_phys_buf: the number of physical buffers to register
125 * @iova_start: the starting address passed over IB which maps to this MR
127 * Returns the memory region on success, otherwise returns an errno.
129 struct ib_mr *ipath_reg_phys_mr(struct ib_pd *pd,
130 struct ib_phys_buf *buffer_list,
131 int num_phys_buf, int acc, u64 *iova_start)
133 struct ipath_mr *mr;
134 int n, m, i;
135 struct ib_mr *ret;
137 mr = alloc_mr(num_phys_buf, &to_idev(pd->device)->lk_table);
138 if (mr == NULL) {
139 ret = ERR_PTR(-ENOMEM);
140 goto bail;
143 mr->mr.pd = pd;
144 mr->mr.user_base = *iova_start;
145 mr->mr.iova = *iova_start;
146 mr->mr.length = 0;
147 mr->mr.offset = 0;
148 mr->mr.access_flags = acc;
149 mr->mr.max_segs = num_phys_buf;
151 m = 0;
152 n = 0;
153 for (i = 0; i < num_phys_buf; i++) {
154 mr->mr.map[m]->segs[n].vaddr = (void *) buffer_list[i].addr;
155 mr->mr.map[m]->segs[n].length = buffer_list[i].size;
156 mr->mr.length += buffer_list[i].size;
157 n++;
158 if (n == IPATH_SEGSZ) {
159 m++;
160 n = 0;
164 ret = &mr->ibmr;
166 bail:
167 return ret;
171 * ipath_reg_user_mr - register a userspace memory region
172 * @pd: protection domain for this memory region
173 * @region: the user memory region
174 * @mr_access_flags: access flags for this memory region
175 * @udata: unused by the InfiniPath driver
177 * Returns the memory region on success, otherwise returns an errno.
179 struct ib_mr *ipath_reg_user_mr(struct ib_pd *pd, struct ib_umem *region,
180 int mr_access_flags, struct ib_udata *udata)
182 struct ipath_mr *mr;
183 struct ib_umem_chunk *chunk;
184 int n, m, i;
185 struct ib_mr *ret;
187 if (region->length == 0) {
188 ret = ERR_PTR(-EINVAL);
189 goto bail;
192 n = 0;
193 list_for_each_entry(chunk, &region->chunk_list, list)
194 n += chunk->nents;
196 mr = alloc_mr(n, &to_idev(pd->device)->lk_table);
197 if (!mr) {
198 ret = ERR_PTR(-ENOMEM);
199 goto bail;
202 mr->mr.pd = pd;
203 mr->mr.user_base = region->user_base;
204 mr->mr.iova = region->virt_base;
205 mr->mr.length = region->length;
206 mr->mr.offset = region->offset;
207 mr->mr.access_flags = mr_access_flags;
208 mr->mr.max_segs = n;
210 m = 0;
211 n = 0;
212 list_for_each_entry(chunk, &region->chunk_list, list) {
213 for (i = 0; i < chunk->nmap; i++) {
214 mr->mr.map[m]->segs[n].vaddr =
215 page_address(chunk->page_list[i].page);
216 mr->mr.map[m]->segs[n].length = region->page_size;
217 n++;
218 if (n == IPATH_SEGSZ) {
219 m++;
220 n = 0;
224 ret = &mr->ibmr;
226 bail:
227 return ret;
231 * ipath_dereg_mr - unregister and free a memory region
232 * @ibmr: the memory region to free
234 * Returns 0 on success.
236 * Note that this is called to free MRs created by ipath_get_dma_mr()
237 * or ipath_reg_user_mr().
239 int ipath_dereg_mr(struct ib_mr *ibmr)
241 struct ipath_mr *mr = to_imr(ibmr);
242 int i;
244 ipath_free_lkey(&to_idev(ibmr->device)->lk_table, ibmr->lkey);
245 i = mr->mr.mapsz;
246 while (i) {
247 i--;
248 kfree(mr->mr.map[i]);
250 kfree(mr);
251 return 0;
255 * ipath_alloc_fmr - allocate a fast memory region
256 * @pd: the protection domain for this memory region
257 * @mr_access_flags: access flags for this memory region
258 * @fmr_attr: fast memory region attributes
260 * Returns the memory region on success, otherwise returns an errno.
262 struct ib_fmr *ipath_alloc_fmr(struct ib_pd *pd, int mr_access_flags,
263 struct ib_fmr_attr *fmr_attr)
265 struct ipath_fmr *fmr;
266 int m, i = 0;
267 struct ib_fmr *ret;
269 /* Allocate struct plus pointers to first level page tables. */
270 m = (fmr_attr->max_pages + IPATH_SEGSZ - 1) / IPATH_SEGSZ;
271 fmr = kmalloc(sizeof *fmr + m * sizeof fmr->mr.map[0], GFP_KERNEL);
272 if (!fmr)
273 goto bail;
275 /* Allocate first level page tables. */
276 for (; i < m; i++) {
277 fmr->mr.map[i] = kmalloc(sizeof *fmr->mr.map[0],
278 GFP_KERNEL);
279 if (!fmr->mr.map[i])
280 goto bail;
282 fmr->mr.mapsz = m;
285 * ib_alloc_fmr() will initialize fmr->ibfmr except for lkey &
286 * rkey.
288 if (!ipath_alloc_lkey(&to_idev(pd->device)->lk_table, &fmr->mr))
289 goto bail;
290 fmr->ibfmr.rkey = fmr->ibfmr.lkey = fmr->mr.lkey;
292 * Resources are allocated but no valid mapping (RKEY can't be
293 * used).
295 fmr->mr.pd = pd;
296 fmr->mr.user_base = 0;
297 fmr->mr.iova = 0;
298 fmr->mr.length = 0;
299 fmr->mr.offset = 0;
300 fmr->mr.access_flags = mr_access_flags;
301 fmr->mr.max_segs = fmr_attr->max_pages;
302 fmr->page_shift = fmr_attr->page_shift;
304 ret = &fmr->ibfmr;
305 goto done;
307 bail:
308 while (i)
309 kfree(fmr->mr.map[--i]);
310 kfree(fmr);
311 ret = ERR_PTR(-ENOMEM);
313 done:
314 return ret;
318 * ipath_map_phys_fmr - set up a fast memory region
319 * @ibmfr: the fast memory region to set up
320 * @page_list: the list of pages to associate with the fast memory region
321 * @list_len: the number of pages to associate with the fast memory region
322 * @iova: the virtual address of the start of the fast memory region
324 * This may be called from interrupt context.
327 int ipath_map_phys_fmr(struct ib_fmr *ibfmr, u64 * page_list,
328 int list_len, u64 iova)
330 struct ipath_fmr *fmr = to_ifmr(ibfmr);
331 struct ipath_lkey_table *rkt;
332 unsigned long flags;
333 int m, n, i;
334 u32 ps;
335 int ret;
337 if (list_len > fmr->mr.max_segs) {
338 ret = -EINVAL;
339 goto bail;
341 rkt = &to_idev(ibfmr->device)->lk_table;
342 spin_lock_irqsave(&rkt->lock, flags);
343 fmr->mr.user_base = iova;
344 fmr->mr.iova = iova;
345 ps = 1 << fmr->page_shift;
346 fmr->mr.length = list_len * ps;
347 m = 0;
348 n = 0;
349 ps = 1 << fmr->page_shift;
350 for (i = 0; i < list_len; i++) {
351 fmr->mr.map[m]->segs[n].vaddr = (void *) page_list[i];
352 fmr->mr.map[m]->segs[n].length = ps;
353 if (++n == IPATH_SEGSZ) {
354 m++;
355 n = 0;
358 spin_unlock_irqrestore(&rkt->lock, flags);
359 ret = 0;
361 bail:
362 return ret;
366 * ipath_unmap_fmr - unmap fast memory regions
367 * @fmr_list: the list of fast memory regions to unmap
369 * Returns 0 on success.
371 int ipath_unmap_fmr(struct list_head *fmr_list)
373 struct ipath_fmr *fmr;
374 struct ipath_lkey_table *rkt;
375 unsigned long flags;
377 list_for_each_entry(fmr, fmr_list, ibfmr.list) {
378 rkt = &to_idev(fmr->ibfmr.device)->lk_table;
379 spin_lock_irqsave(&rkt->lock, flags);
380 fmr->mr.user_base = 0;
381 fmr->mr.iova = 0;
382 fmr->mr.length = 0;
383 spin_unlock_irqrestore(&rkt->lock, flags);
385 return 0;
389 * ipath_dealloc_fmr - deallocate a fast memory region
390 * @ibfmr: the fast memory region to deallocate
392 * Returns 0 on success.
394 int ipath_dealloc_fmr(struct ib_fmr *ibfmr)
396 struct ipath_fmr *fmr = to_ifmr(ibfmr);
397 int i;
399 ipath_free_lkey(&to_idev(ibfmr->device)->lk_table, ibfmr->lkey);
400 i = fmr->mr.mapsz;
401 while (i)
402 kfree(fmr->mr.map[--i]);
403 kfree(fmr);
404 return 0;