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"
39 /* Fast memory region */
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);
52 * ipath_get_dma_mr - get a DMA memory region
53 * @pd: protection domain for this memory region
56 * Returns the memory region on success, otherwise returns an errno.
58 struct ib_mr *ipath_get_dma_mr(struct ib_pd *pd, int acc)
63 mr = kzalloc(sizeof *mr, GFP_KERNEL);
65 ret = ERR_PTR(-ENOMEM);
69 mr->mr.access_flags = acc;
76 static struct ipath_mr *alloc_mr(int count,
77 struct ipath_lkey_table *lk_table)
82 /* Allocate struct plus pointers to first level page tables. */
83 m = (count + IPATH_SEGSZ - 1) / IPATH_SEGSZ;
84 mr = kmalloc(sizeof *mr + m * sizeof mr->mr.map[0], GFP_KERNEL);
88 /* Allocate first level page tables. */
90 mr->mr.map[i] = kmalloc(sizeof *mr->mr.map[0], GFP_KERNEL);
97 * ib_reg_phys_mr() will initialize mr->ibmr except for
100 if (!ipath_alloc_lkey(lk_table, &mr->mr))
102 mr->ibmr.rkey = mr->ibmr.lkey = mr->mr.lkey;
109 kfree(mr->mr.map[i]);
119 * ipath_reg_phys_mr - register a physical memory region
120 * @pd: protection domain for this memory region
121 * @buffer_list: pointer to the list of physical buffers to register
122 * @num_phys_buf: the number of physical buffers to register
123 * @iova_start: the starting address passed over IB which maps to this MR
125 * Returns the memory region on success, otherwise returns an errno.
127 struct ib_mr *ipath_reg_phys_mr(struct ib_pd *pd,
128 struct ib_phys_buf *buffer_list,
129 int num_phys_buf, int acc, u64 *iova_start)
135 mr = alloc_mr(num_phys_buf, &to_idev(pd->device)->lk_table);
137 ret = ERR_PTR(-ENOMEM);
142 mr->mr.user_base = *iova_start;
143 mr->mr.iova = *iova_start;
146 mr->mr.access_flags = acc;
147 mr->mr.max_segs = num_phys_buf;
151 for (i = 0; i < num_phys_buf; i++) {
152 mr->mr.map[m]->segs[n].vaddr =
153 phys_to_virt(buffer_list[i].addr);
154 mr->mr.map[m]->segs[n].length = buffer_list[i].size;
155 mr->mr.length += buffer_list[i].size;
157 if (n == IPATH_SEGSZ) {
170 * ipath_reg_user_mr - register a userspace memory region
171 * @pd: protection domain for this memory region
172 * @region: the user memory region
173 * @mr_access_flags: access flags for this memory region
174 * @udata: unused by the InfiniPath driver
176 * Returns the memory region on success, otherwise returns an errno.
178 struct ib_mr *ipath_reg_user_mr(struct ib_pd *pd, struct ib_umem *region,
179 int mr_access_flags, struct ib_udata *udata)
182 struct ib_umem_chunk *chunk;
186 if (region->length == 0) {
187 ret = ERR_PTR(-EINVAL);
192 list_for_each_entry(chunk, ®ion->chunk_list, list)
195 mr = alloc_mr(n, &to_idev(pd->device)->lk_table);
197 ret = ERR_PTR(-ENOMEM);
202 mr->mr.user_base = region->user_base;
203 mr->mr.iova = region->virt_base;
204 mr->mr.length = region->length;
205 mr->mr.offset = region->offset;
206 mr->mr.access_flags = mr_access_flags;
211 list_for_each_entry(chunk, ®ion->chunk_list, list) {
212 for (i = 0; i < chunk->nmap; i++) {
213 mr->mr.map[m]->segs[n].vaddr =
214 page_address(chunk->page_list[i].page);
215 mr->mr.map[m]->segs[n].length = region->page_size;
217 if (n == IPATH_SEGSZ) {
230 * ipath_dereg_mr - unregister and free a memory region
231 * @ibmr: the memory region to free
233 * Returns 0 on success.
235 * Note that this is called to free MRs created by ipath_get_dma_mr()
236 * or ipath_reg_user_mr().
238 int ipath_dereg_mr(struct ib_mr *ibmr)
240 struct ipath_mr *mr = to_imr(ibmr);
243 ipath_free_lkey(&to_idev(ibmr->device)->lk_table, ibmr->lkey);
247 kfree(mr->mr.map[i]);
254 * ipath_alloc_fmr - allocate a fast memory region
255 * @pd: the protection domain for this memory region
256 * @mr_access_flags: access flags for this memory region
257 * @fmr_attr: fast memory region attributes
259 * Returns the memory region on success, otherwise returns an errno.
261 struct ib_fmr *ipath_alloc_fmr(struct ib_pd *pd, int mr_access_flags,
262 struct ib_fmr_attr *fmr_attr)
264 struct ipath_fmr *fmr;
268 /* Allocate struct plus pointers to first level page tables. */
269 m = (fmr_attr->max_pages + IPATH_SEGSZ - 1) / IPATH_SEGSZ;
270 fmr = kmalloc(sizeof *fmr + m * sizeof fmr->mr.map[0], GFP_KERNEL);
274 /* Allocate first level page tables. */
276 fmr->mr.map[i] = kmalloc(sizeof *fmr->mr.map[0],
284 * ib_alloc_fmr() will initialize fmr->ibfmr except for lkey &
287 if (!ipath_alloc_lkey(&to_idev(pd->device)->lk_table, &fmr->mr))
289 fmr->ibfmr.rkey = fmr->ibfmr.lkey = fmr->mr.lkey;
291 * Resources are allocated but no valid mapping (RKEY can't be
295 fmr->mr.user_base = 0;
299 fmr->mr.access_flags = mr_access_flags;
300 fmr->mr.max_segs = fmr_attr->max_pages;
301 fmr->page_shift = fmr_attr->page_shift;
308 kfree(fmr->mr.map[--i]);
310 ret = ERR_PTR(-ENOMEM);
317 * ipath_map_phys_fmr - set up a fast memory region
318 * @ibmfr: the fast memory region to set up
319 * @page_list: the list of pages to associate with the fast memory region
320 * @list_len: the number of pages to associate with the fast memory region
321 * @iova: the virtual address of the start of the fast memory region
323 * This may be called from interrupt context.
326 int ipath_map_phys_fmr(struct ib_fmr *ibfmr, u64 * page_list,
327 int list_len, u64 iova)
329 struct ipath_fmr *fmr = to_ifmr(ibfmr);
330 struct ipath_lkey_table *rkt;
336 if (list_len > fmr->mr.max_segs) {
340 rkt = &to_idev(ibfmr->device)->lk_table;
341 spin_lock_irqsave(&rkt->lock, flags);
342 fmr->mr.user_base = iova;
344 ps = 1 << fmr->page_shift;
345 fmr->mr.length = list_len * ps;
348 ps = 1 << fmr->page_shift;
349 for (i = 0; i < list_len; i++) {
350 fmr->mr.map[m]->segs[n].vaddr = phys_to_virt(page_list[i]);
351 fmr->mr.map[m]->segs[n].length = ps;
352 if (++n == IPATH_SEGSZ) {
357 spin_unlock_irqrestore(&rkt->lock, flags);
365 * ipath_unmap_fmr - unmap fast memory regions
366 * @fmr_list: the list of fast memory regions to unmap
368 * Returns 0 on success.
370 int ipath_unmap_fmr(struct list_head *fmr_list)
372 struct ipath_fmr *fmr;
373 struct ipath_lkey_table *rkt;
376 list_for_each_entry(fmr, fmr_list, ibfmr.list) {
377 rkt = &to_idev(fmr->ibfmr.device)->lk_table;
378 spin_lock_irqsave(&rkt->lock, flags);
379 fmr->mr.user_base = 0;
382 spin_unlock_irqrestore(&rkt->lock, flags);
388 * ipath_dealloc_fmr - deallocate a fast memory region
389 * @ibfmr: the fast memory region to deallocate
391 * Returns 0 on success.
393 int ipath_dealloc_fmr(struct ib_fmr *ibfmr)
395 struct ipath_fmr *fmr = to_ifmr(ibfmr);
398 ipath_free_lkey(&to_idev(ibfmr->device)->lk_table, ibfmr->lkey);
401 kfree(fmr->mr.map[--i]);