2 * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. 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_umem.h>
35 #include <rdma/ib_smi.h>
39 /* Fast memory region */
42 struct qib_mregion mr
; /* must be last */
45 static inline struct qib_fmr
*to_ifmr(struct ib_fmr
*ibfmr
)
47 return container_of(ibfmr
, struct qib_fmr
, ibfmr
);
50 static int init_qib_mregion(struct qib_mregion
*mr
, struct ib_pd
*pd
,
56 m
= (count
+ QIB_SEGSZ
- 1) / QIB_SEGSZ
;
58 mr
->map
[i
] = kzalloc(sizeof *mr
->map
[0], GFP_KERNEL
);
63 init_completion(&mr
->comp
);
64 /* count returning the ptr to user */
65 atomic_set(&mr
->refcount
, 1);
77 static void deinit_qib_mregion(struct qib_mregion
*mr
)
88 * qib_get_dma_mr - get a DMA memory region
89 * @pd: protection domain for this memory region
92 * Returns the memory region on success, otherwise returns an errno.
93 * Note that all DMA addresses should be created via the
94 * struct ib_dma_mapping_ops functions (see qib_dma.c).
96 struct ib_mr
*qib_get_dma_mr(struct ib_pd
*pd
, int acc
)
98 struct qib_mr
*mr
= NULL
;
102 if (to_ipd(pd
)->user
) {
103 ret
= ERR_PTR(-EPERM
);
107 mr
= kzalloc(sizeof *mr
, GFP_KERNEL
);
109 ret
= ERR_PTR(-ENOMEM
);
113 rval
= init_qib_mregion(&mr
->mr
, pd
, 0);
120 rval
= qib_alloc_lkey(&mr
->mr
, 1);
126 mr
->mr
.access_flags
= acc
;
132 deinit_qib_mregion(&mr
->mr
);
138 static struct qib_mr
*alloc_mr(int count
, struct ib_pd
*pd
)
144 /* Allocate struct plus pointers to first level page tables. */
145 m
= (count
+ QIB_SEGSZ
- 1) / QIB_SEGSZ
;
146 mr
= kzalloc(sizeof *mr
+ m
* sizeof mr
->mr
.map
[0], GFP_KERNEL
);
150 rval
= init_qib_mregion(&mr
->mr
, pd
, count
);
154 * ib_reg_phys_mr() will initialize mr->ibmr except for
157 rval
= qib_alloc_lkey(&mr
->mr
, 0);
160 mr
->ibmr
.lkey
= mr
->mr
.lkey
;
161 mr
->ibmr
.rkey
= mr
->mr
.lkey
;
166 deinit_qib_mregion(&mr
->mr
);
174 * qib_reg_phys_mr - register a physical memory region
175 * @pd: protection domain for this memory region
176 * @buffer_list: pointer to the list of physical buffers to register
177 * @num_phys_buf: the number of physical buffers to register
178 * @iova_start: the starting address passed over IB which maps to this MR
180 * Returns the memory region on success, otherwise returns an errno.
182 struct ib_mr
*qib_reg_phys_mr(struct ib_pd
*pd
,
183 struct ib_phys_buf
*buffer_list
,
184 int num_phys_buf
, int acc
, u64
*iova_start
)
190 mr
= alloc_mr(num_phys_buf
, pd
);
192 ret
= (struct ib_mr
*)mr
;
196 mr
->mr
.user_base
= *iova_start
;
197 mr
->mr
.iova
= *iova_start
;
198 mr
->mr
.access_flags
= acc
;
202 for (i
= 0; i
< num_phys_buf
; i
++) {
203 mr
->mr
.map
[m
]->segs
[n
].vaddr
= (void *) buffer_list
[i
].addr
;
204 mr
->mr
.map
[m
]->segs
[n
].length
= buffer_list
[i
].size
;
205 mr
->mr
.length
+= buffer_list
[i
].size
;
207 if (n
== QIB_SEGSZ
) {
220 * qib_reg_user_mr - register a userspace memory region
221 * @pd: protection domain for this memory region
222 * @start: starting userspace address
223 * @length: length of region to register
224 * @mr_access_flags: access flags for this memory region
225 * @udata: unused by the QLogic_IB driver
227 * Returns the memory region on success, otherwise returns an errno.
229 struct ib_mr
*qib_reg_user_mr(struct ib_pd
*pd
, u64 start
, u64 length
,
230 u64 virt_addr
, int mr_access_flags
,
231 struct ib_udata
*udata
)
234 struct ib_umem
*umem
;
235 struct ib_umem_chunk
*chunk
;
240 ret
= ERR_PTR(-EINVAL
);
244 umem
= ib_umem_get(pd
->uobject
->context
, start
, length
,
247 return (void *) umem
;
250 list_for_each_entry(chunk
, &umem
->chunk_list
, list
)
253 mr
= alloc_mr(n
, pd
);
255 ret
= (struct ib_mr
*)mr
;
256 ib_umem_release(umem
);
260 mr
->mr
.user_base
= start
;
261 mr
->mr
.iova
= virt_addr
;
262 mr
->mr
.length
= length
;
263 mr
->mr
.offset
= umem
->offset
;
264 mr
->mr
.access_flags
= mr_access_flags
;
267 if (is_power_of_2(umem
->page_size
))
268 mr
->mr
.page_shift
= ilog2(umem
->page_size
);
271 list_for_each_entry(chunk
, &umem
->chunk_list
, list
) {
272 for (i
= 0; i
< chunk
->nents
; i
++) {
275 vaddr
= page_address(sg_page(&chunk
->page_list
[i
]));
277 ret
= ERR_PTR(-EINVAL
);
280 mr
->mr
.map
[m
]->segs
[n
].vaddr
= vaddr
;
281 mr
->mr
.map
[m
]->segs
[n
].length
= umem
->page_size
;
283 if (n
== QIB_SEGSZ
) {
296 * qib_dereg_mr - unregister and free a memory region
297 * @ibmr: the memory region to free
299 * Returns 0 on success.
301 * Note that this is called to free MRs created by qib_get_dma_mr()
302 * or qib_reg_user_mr().
304 int qib_dereg_mr(struct ib_mr
*ibmr
)
306 struct qib_mr
*mr
= to_imr(ibmr
);
308 unsigned long timeout
;
310 qib_free_lkey(&mr
->mr
);
312 qib_put_mr(&mr
->mr
); /* will set completion if last */
313 timeout
= wait_for_completion_timeout(&mr
->mr
.comp
,
320 deinit_qib_mregion(&mr
->mr
);
322 ib_umem_release(mr
->umem
);
329 * Allocate a memory region usable with the
330 * IB_WR_FAST_REG_MR send work request.
332 * Return the memory region on success, otherwise return an errno.
334 struct ib_mr
*qib_alloc_fast_reg_mr(struct ib_pd
*pd
, int max_page_list_len
)
338 mr
= alloc_mr(max_page_list_len
, pd
);
340 return (struct ib_mr
*)mr
;
345 struct ib_fast_reg_page_list
*
346 qib_alloc_fast_reg_page_list(struct ib_device
*ibdev
, int page_list_len
)
348 unsigned size
= page_list_len
* sizeof(u64
);
349 struct ib_fast_reg_page_list
*pl
;
351 if (size
> PAGE_SIZE
)
352 return ERR_PTR(-EINVAL
);
354 pl
= kzalloc(sizeof *pl
, GFP_KERNEL
);
356 return ERR_PTR(-ENOMEM
);
358 pl
->page_list
= kzalloc(size
, GFP_KERNEL
);
366 return ERR_PTR(-ENOMEM
);
369 void qib_free_fast_reg_page_list(struct ib_fast_reg_page_list
*pl
)
371 kfree(pl
->page_list
);
376 * qib_alloc_fmr - allocate a fast memory region
377 * @pd: the protection domain for this memory region
378 * @mr_access_flags: access flags for this memory region
379 * @fmr_attr: fast memory region attributes
381 * Returns the memory region on success, otherwise returns an errno.
383 struct ib_fmr
*qib_alloc_fmr(struct ib_pd
*pd
, int mr_access_flags
,
384 struct ib_fmr_attr
*fmr_attr
)
391 /* Allocate struct plus pointers to first level page tables. */
392 m
= (fmr_attr
->max_pages
+ QIB_SEGSZ
- 1) / QIB_SEGSZ
;
393 fmr
= kzalloc(sizeof *fmr
+ m
* sizeof fmr
->mr
.map
[0], GFP_KERNEL
);
397 rval
= init_qib_mregion(&fmr
->mr
, pd
, fmr_attr
->max_pages
);
402 * ib_alloc_fmr() will initialize fmr->ibfmr except for lkey &
405 rval
= qib_alloc_lkey(&fmr
->mr
, 0);
408 fmr
->ibfmr
.rkey
= fmr
->mr
.lkey
;
409 fmr
->ibfmr
.lkey
= fmr
->mr
.lkey
;
411 * Resources are allocated but no valid mapping (RKEY can't be
414 fmr
->mr
.access_flags
= mr_access_flags
;
415 fmr
->mr
.max_segs
= fmr_attr
->max_pages
;
416 fmr
->mr
.page_shift
= fmr_attr
->page_shift
;
423 deinit_qib_mregion(&fmr
->mr
);
431 * qib_map_phys_fmr - set up a fast memory region
432 * @ibmfr: the fast memory region to set up
433 * @page_list: the list of pages to associate with the fast memory region
434 * @list_len: the number of pages to associate with the fast memory region
435 * @iova: the virtual address of the start of the fast memory region
437 * This may be called from interrupt context.
440 int qib_map_phys_fmr(struct ib_fmr
*ibfmr
, u64
*page_list
,
441 int list_len
, u64 iova
)
443 struct qib_fmr
*fmr
= to_ifmr(ibfmr
);
444 struct qib_lkey_table
*rkt
;
450 i
= atomic_read(&fmr
->mr
.refcount
);
454 if (list_len
> fmr
->mr
.max_segs
) {
458 rkt
= &to_idev(ibfmr
->device
)->lk_table
;
459 spin_lock_irqsave(&rkt
->lock
, flags
);
460 fmr
->mr
.user_base
= iova
;
462 ps
= 1 << fmr
->mr
.page_shift
;
463 fmr
->mr
.length
= list_len
* ps
;
466 for (i
= 0; i
< list_len
; i
++) {
467 fmr
->mr
.map
[m
]->segs
[n
].vaddr
= (void *) page_list
[i
];
468 fmr
->mr
.map
[m
]->segs
[n
].length
= ps
;
469 if (++n
== QIB_SEGSZ
) {
474 spin_unlock_irqrestore(&rkt
->lock
, flags
);
482 * qib_unmap_fmr - unmap fast memory regions
483 * @fmr_list: the list of fast memory regions to unmap
485 * Returns 0 on success.
487 int qib_unmap_fmr(struct list_head
*fmr_list
)
490 struct qib_lkey_table
*rkt
;
493 list_for_each_entry(fmr
, fmr_list
, ibfmr
.list
) {
494 rkt
= &to_idev(fmr
->ibfmr
.device
)->lk_table
;
495 spin_lock_irqsave(&rkt
->lock
, flags
);
496 fmr
->mr
.user_base
= 0;
499 spin_unlock_irqrestore(&rkt
->lock
, flags
);
505 * qib_dealloc_fmr - deallocate a fast memory region
506 * @ibfmr: the fast memory region to deallocate
508 * Returns 0 on success.
510 int qib_dealloc_fmr(struct ib_fmr
*ibfmr
)
512 struct qib_fmr
*fmr
= to_ifmr(ibfmr
);
514 unsigned long timeout
;
516 qib_free_lkey(&fmr
->mr
);
517 qib_put_mr(&fmr
->mr
); /* will set completion if last */
518 timeout
= wait_for_completion_timeout(&fmr
->mr
.comp
,
521 qib_get_mr(&fmr
->mr
);
525 deinit_qib_mregion(&fmr
->mr
);
531 void mr_rcu_callback(struct rcu_head
*list
)
533 struct qib_mregion
*mr
= container_of(list
, struct qib_mregion
, list
);