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
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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
34 #include <rdma/ib_pack.h>
35 #include <rdma/ib_smi.h>
37 #include "ipath_verbs.h"
40 * ipath_get_dma_mr - get a DMA memory region
41 * @pd: protection domain for this memory region
44 * Returns the memory region on success, otherwise returns an errno.
46 struct ib_mr *ipath_get_dma_mr(struct ib_pd *pd, int acc)
51 mr = kzalloc(sizeof *mr, GFP_KERNEL);
53 ret = ERR_PTR(-ENOMEM);
57 mr->mr.access_flags = acc;
64 static struct ipath_mr *alloc_mr(int count,
65 struct ipath_lkey_table *lk_table)
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);
76 /* Allocate first level page tables. */
78 mr->mr.map[i] = kmalloc(sizeof *mr->mr.map[0], GFP_KERNEL);
85 * ib_reg_phys_mr() will initialize mr->ibmr except for
88 if (!ipath_alloc_lkey(lk_table, &mr->mr))
90 mr->ibmr.rkey = mr->ibmr.lkey = mr->mr.lkey;
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
113 * Returns the memory region on success, otherwise returns an errno.
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)
123 mr = alloc_mr(num_phys_buf, &to_idev(pd->device)->lk_table);
125 ret = ERR_PTR(-ENOMEM);
129 mr->mr.user_base = *iova_start;
130 mr->mr.iova = *iova_start;
133 mr->mr.access_flags = acc;
134 mr->mr.max_segs = num_phys_buf;
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;
144 if (n == IPATH_SEGSZ) {
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
163 * Returns the memory region on success, otherwise returns an errno.
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)
169 struct ib_umem_chunk *chunk;
173 if (region->length == 0) {
174 ret = ERR_PTR(-EINVAL);
179 list_for_each_entry(chunk, ®ion->chunk_list, list)
182 mr = alloc_mr(n, &to_idev(pd->device)->lk_table);
184 ret = ERR_PTR(-ENOMEM);
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;
197 list_for_each_entry(chunk, ®ion->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;
203 if (n == IPATH_SEGSZ) {
216 * ipath_dereg_mr - unregister and free a memory region
217 * @ibmr: the memory region to free
219 * Returns 0 on success.
221 * Note that this is called to free MRs created by ipath_get_dma_mr()
222 * or ipath_reg_user_mr().
224 int ipath_dereg_mr(struct ib_mr *ibmr)
226 struct ipath_mr *mr = to_imr(ibmr);
229 ipath_free_lkey(&to_idev(ibmr->device)->lk_table, ibmr->lkey);
233 kfree(mr->mr.map[i]);
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
245 * Returns the memory region on success, otherwise returns an errno.
247 struct ib_fmr *ipath_alloc_fmr(struct ib_pd *pd, int mr_access_flags,
248 struct ib_fmr_attr *fmr_attr)
250 struct ipath_fmr *fmr;
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);
260 /* Allocate first level page tables. */
262 fmr->mr.map[i] = kmalloc(sizeof *fmr->mr.map[0],
270 * ib_alloc_fmr() will initialize fmr->ibfmr except for lkey &
273 if (!ipath_alloc_lkey(&to_idev(pd->device)->lk_table, &fmr->mr))
275 fmr->ibfmr.rkey = fmr->ibfmr.lkey = fmr->mr.lkey;
277 * Resources are allocated but no valid mapping (RKEY can't be
280 fmr->mr.user_base = 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;
293 kfree(fmr->mr.map[--i]);
295 ret = ERR_PTR(-ENOMEM);
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
308 * This may be called from interrupt context.
311 int ipath_map_phys_fmr(struct ib_fmr *ibfmr, u64 * page_list,
312 int list_len, u64 iova)
314 struct ipath_fmr *fmr = to_ifmr(ibfmr);
315 struct ipath_lkey_table *rkt;
321 if (list_len > fmr->mr.max_segs) {
325 rkt = &to_idev(ibfmr->device)->lk_table;
326 spin_lock_irqsave(&rkt->lock, flags);
327 fmr->mr.user_base = iova;
329 ps = 1 << fmr->page_shift;
330 fmr->mr.length = list_len * ps;
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) {
342 spin_unlock_irqrestore(&rkt->lock, flags);
350 * ipath_unmap_fmr - unmap fast memory regions
351 * @fmr_list: the list of fast memory regions to unmap
353 * Returns 0 on success.
355 int ipath_unmap_fmr(struct list_head *fmr_list)
357 struct ipath_fmr *fmr;
358 struct ipath_lkey_table *rkt;
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;
367 spin_unlock_irqrestore(&rkt->lock, flags);
373 * ipath_dealloc_fmr - deallocate a fast memory region
374 * @ibfmr: the fast memory region to deallocate
376 * Returns 0 on success.
378 int ipath_dealloc_fmr(struct ib_fmr *ibfmr)
380 struct ipath_fmr *fmr = to_ifmr(ibfmr);
383 ipath_free_lkey(&to_idev(ibfmr->device)->lk_table, ibfmr->lkey);
386 kfree(fmr->mr.map[--i]);