2 * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2007, 2008 Mellanox Technologies. 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 <linux/slab.h>
38 static u32
convert_access(int acc
)
40 return (acc
& IB_ACCESS_REMOTE_ATOMIC
? MLX4_PERM_ATOMIC
: 0) |
41 (acc
& IB_ACCESS_REMOTE_WRITE
? MLX4_PERM_REMOTE_WRITE
: 0) |
42 (acc
& IB_ACCESS_REMOTE_READ
? MLX4_PERM_REMOTE_READ
: 0) |
43 (acc
& IB_ACCESS_LOCAL_WRITE
? MLX4_PERM_LOCAL_WRITE
: 0) |
44 (acc
& IB_ACCESS_MW_BIND
? MLX4_PERM_BIND_MW
: 0) |
48 static enum mlx4_mw_type
to_mlx4_type(enum ib_mw_type type
)
51 case IB_MW_TYPE_1
: return MLX4_MW_TYPE_1
;
52 case IB_MW_TYPE_2
: return MLX4_MW_TYPE_2
;
57 struct ib_mr
*mlx4_ib_get_dma_mr(struct ib_pd
*pd
, int acc
)
59 struct mlx4_ib_mr
*mr
;
62 mr
= kzalloc(sizeof(*mr
), GFP_KERNEL
);
64 return ERR_PTR(-ENOMEM
);
66 err
= mlx4_mr_alloc(to_mdev(pd
->device
)->dev
, to_mpd(pd
)->pdn
, 0,
67 ~0ull, convert_access(acc
), 0, 0, &mr
->mmr
);
71 err
= mlx4_mr_enable(to_mdev(pd
->device
)->dev
, &mr
->mmr
);
75 mr
->ibmr
.rkey
= mr
->ibmr
.lkey
= mr
->mmr
.key
;
81 (void) mlx4_mr_free(to_mdev(pd
->device
)->dev
, &mr
->mmr
);
89 int mlx4_ib_umem_write_mtt(struct mlx4_ib_dev
*dev
, struct mlx4_mtt
*mtt
,
97 struct scatterlist
*sg
;
99 pages
= (u64
*) __get_free_page(GFP_KERNEL
);
105 for_each_sg(umem
->sg_head
.sgl
, sg
, umem
->nmap
, entry
) {
106 len
= sg_dma_len(sg
) >> mtt
->page_shift
;
107 for (k
= 0; k
< len
; ++k
) {
108 pages
[i
++] = sg_dma_address(sg
) +
111 * Be friendly to mlx4_write_mtt() and
112 * pass it chunks of appropriate size.
114 if (i
== PAGE_SIZE
/ sizeof (u64
)) {
115 err
= mlx4_write_mtt(dev
->dev
, mtt
, n
,
126 err
= mlx4_write_mtt(dev
->dev
, mtt
, n
, i
, pages
);
129 free_page((unsigned long) pages
);
133 struct ib_mr
*mlx4_ib_reg_user_mr(struct ib_pd
*pd
, u64 start
, u64 length
,
134 u64 virt_addr
, int access_flags
,
135 struct ib_udata
*udata
)
137 struct mlx4_ib_dev
*dev
= to_mdev(pd
->device
);
138 struct mlx4_ib_mr
*mr
;
143 mr
= kzalloc(sizeof(*mr
), GFP_KERNEL
);
145 return ERR_PTR(-ENOMEM
);
147 /* Force registering the memory as writable. */
148 /* Used for memory re-registeration. HCA protects the access */
149 mr
->umem
= ib_umem_get(pd
->uobject
->context
, start
, length
,
150 access_flags
| IB_ACCESS_LOCAL_WRITE
, 0);
151 if (IS_ERR(mr
->umem
)) {
152 err
= PTR_ERR(mr
->umem
);
156 n
= ib_umem_page_count(mr
->umem
);
157 shift
= ilog2(mr
->umem
->page_size
);
159 err
= mlx4_mr_alloc(dev
->dev
, to_mpd(pd
)->pdn
, virt_addr
, length
,
160 convert_access(access_flags
), n
, shift
, &mr
->mmr
);
164 err
= mlx4_ib_umem_write_mtt(dev
, &mr
->mmr
.mtt
, mr
->umem
);
168 err
= mlx4_mr_enable(dev
->dev
, &mr
->mmr
);
172 mr
->ibmr
.rkey
= mr
->ibmr
.lkey
= mr
->mmr
.key
;
177 (void) mlx4_mr_free(to_mdev(pd
->device
)->dev
, &mr
->mmr
);
180 ib_umem_release(mr
->umem
);
188 int mlx4_ib_rereg_user_mr(struct ib_mr
*mr
, int flags
,
189 u64 start
, u64 length
, u64 virt_addr
,
190 int mr_access_flags
, struct ib_pd
*pd
,
191 struct ib_udata
*udata
)
193 struct mlx4_ib_dev
*dev
= to_mdev(mr
->device
);
194 struct mlx4_ib_mr
*mmr
= to_mmr(mr
);
195 struct mlx4_mpt_entry
*mpt_entry
;
196 struct mlx4_mpt_entry
**pmpt_entry
= &mpt_entry
;
199 /* Since we synchronize this call and mlx4_ib_dereg_mr via uverbs,
200 * we assume that the calls can't run concurrently. Otherwise, a
203 err
= mlx4_mr_hw_get_mpt(dev
->dev
, &mmr
->mmr
, &pmpt_entry
);
208 if (flags
& IB_MR_REREG_PD
) {
209 err
= mlx4_mr_hw_change_pd(dev
->dev
, *pmpt_entry
,
213 goto release_mpt_entry
;
216 if (flags
& IB_MR_REREG_ACCESS
) {
217 err
= mlx4_mr_hw_change_access(dev
->dev
, *pmpt_entry
,
218 convert_access(mr_access_flags
));
221 goto release_mpt_entry
;
224 if (flags
& IB_MR_REREG_TRANS
) {
228 mlx4_mr_rereg_mem_cleanup(dev
->dev
, &mmr
->mmr
);
229 ib_umem_release(mmr
->umem
);
230 mmr
->umem
= ib_umem_get(mr
->uobject
->context
, start
, length
,
232 IB_ACCESS_LOCAL_WRITE
,
234 if (IS_ERR(mmr
->umem
)) {
235 err
= PTR_ERR(mmr
->umem
);
236 /* Prevent mlx4_ib_dereg_mr from free'ing invalid pointer */
238 goto release_mpt_entry
;
240 n
= ib_umem_page_count(mmr
->umem
);
241 shift
= ilog2(mmr
->umem
->page_size
);
243 err
= mlx4_mr_rereg_mem_write(dev
->dev
, &mmr
->mmr
,
244 virt_addr
, length
, n
, shift
,
247 ib_umem_release(mmr
->umem
);
248 goto release_mpt_entry
;
250 mmr
->mmr
.iova
= virt_addr
;
251 mmr
->mmr
.size
= length
;
253 err
= mlx4_ib_umem_write_mtt(dev
, &mmr
->mmr
.mtt
, mmr
->umem
);
255 mlx4_mr_rereg_mem_cleanup(dev
->dev
, &mmr
->mmr
);
256 ib_umem_release(mmr
->umem
);
257 goto release_mpt_entry
;
261 /* If we couldn't transfer the MR to the HCA, just remember to
262 * return a failure. But dereg_mr will free the resources.
264 err
= mlx4_mr_hw_write_mpt(dev
->dev
, &mmr
->mmr
, pmpt_entry
);
265 if (!err
&& flags
& IB_MR_REREG_ACCESS
)
266 mmr
->mmr
.access
= mr_access_flags
;
269 mlx4_mr_hw_put_mpt(dev
->dev
, pmpt_entry
);
275 mlx4_alloc_priv_pages(struct ib_device
*device
,
276 struct mlx4_ib_mr
*mr
,
279 int size
= max_pages
* sizeof(u64
);
283 add_size
= max_t(int, MLX4_MR_PAGES_ALIGN
- ARCH_KMALLOC_MINALIGN
, 0);
285 mr
->pages_alloc
= kzalloc(size
+ add_size
, GFP_KERNEL
);
286 if (!mr
->pages_alloc
)
289 mr
->pages
= PTR_ALIGN(mr
->pages_alloc
, MLX4_MR_PAGES_ALIGN
);
291 mr
->page_map
= dma_map_single(device
->dma_device
, mr
->pages
,
292 size
, DMA_TO_DEVICE
);
294 if (dma_mapping_error(device
->dma_device
, mr
->page_map
)) {
301 kfree(mr
->pages_alloc
);
307 mlx4_free_priv_pages(struct mlx4_ib_mr
*mr
)
310 struct ib_device
*device
= mr
->ibmr
.device
;
311 int size
= mr
->max_pages
* sizeof(u64
);
313 dma_unmap_single(device
->dma_device
, mr
->page_map
,
314 size
, DMA_TO_DEVICE
);
315 kfree(mr
->pages_alloc
);
320 int mlx4_ib_dereg_mr(struct ib_mr
*ibmr
)
322 struct mlx4_ib_mr
*mr
= to_mmr(ibmr
);
325 mlx4_free_priv_pages(mr
);
327 ret
= mlx4_mr_free(to_mdev(ibmr
->device
)->dev
, &mr
->mmr
);
331 ib_umem_release(mr
->umem
);
337 struct ib_mw
*mlx4_ib_alloc_mw(struct ib_pd
*pd
, enum ib_mw_type type
)
339 struct mlx4_ib_dev
*dev
= to_mdev(pd
->device
);
340 struct mlx4_ib_mw
*mw
;
343 mw
= kmalloc(sizeof(*mw
), GFP_KERNEL
);
345 return ERR_PTR(-ENOMEM
);
347 err
= mlx4_mw_alloc(dev
->dev
, to_mpd(pd
)->pdn
,
348 to_mlx4_type(type
), &mw
->mmw
);
352 err
= mlx4_mw_enable(dev
->dev
, &mw
->mmw
);
356 mw
->ibmw
.rkey
= mw
->mmw
.key
;
361 mlx4_mw_free(dev
->dev
, &mw
->mmw
);
369 int mlx4_ib_bind_mw(struct ib_qp
*qp
, struct ib_mw
*mw
,
370 struct ib_mw_bind
*mw_bind
)
372 struct ib_bind_mw_wr wr
;
373 struct ib_send_wr
*bad_wr
;
376 memset(&wr
, 0, sizeof(wr
));
377 wr
.wr
.opcode
= IB_WR_BIND_MW
;
378 wr
.wr
.wr_id
= mw_bind
->wr_id
;
379 wr
.wr
.send_flags
= mw_bind
->send_flags
;
381 wr
.bind_info
= mw_bind
->bind_info
;
382 wr
.rkey
= ib_inc_rkey(mw
->rkey
);
384 ret
= mlx4_ib_post_send(qp
, &wr
.wr
, &bad_wr
);
391 int mlx4_ib_dealloc_mw(struct ib_mw
*ibmw
)
393 struct mlx4_ib_mw
*mw
= to_mmw(ibmw
);
395 mlx4_mw_free(to_mdev(ibmw
->device
)->dev
, &mw
->mmw
);
401 struct ib_mr
*mlx4_ib_alloc_mr(struct ib_pd
*pd
,
402 enum ib_mr_type mr_type
,
405 struct mlx4_ib_dev
*dev
= to_mdev(pd
->device
);
406 struct mlx4_ib_mr
*mr
;
409 if (mr_type
!= IB_MR_TYPE_MEM_REG
||
410 max_num_sg
> MLX4_MAX_FAST_REG_PAGES
)
411 return ERR_PTR(-EINVAL
);
413 mr
= kzalloc(sizeof(*mr
), GFP_KERNEL
);
415 return ERR_PTR(-ENOMEM
);
417 err
= mlx4_mr_alloc(dev
->dev
, to_mpd(pd
)->pdn
, 0, 0, 0,
418 max_num_sg
, 0, &mr
->mmr
);
422 err
= mlx4_alloc_priv_pages(pd
->device
, mr
, max_num_sg
);
426 mr
->max_pages
= max_num_sg
;
428 err
= mlx4_mr_enable(dev
->dev
, &mr
->mmr
);
432 mr
->ibmr
.rkey
= mr
->ibmr
.lkey
= mr
->mmr
.key
;
438 mlx4_free_priv_pages(mr
);
440 (void) mlx4_mr_free(dev
->dev
, &mr
->mmr
);
446 struct ib_fmr
*mlx4_ib_fmr_alloc(struct ib_pd
*pd
, int acc
,
447 struct ib_fmr_attr
*fmr_attr
)
449 struct mlx4_ib_dev
*dev
= to_mdev(pd
->device
);
450 struct mlx4_ib_fmr
*fmr
;
453 fmr
= kmalloc(sizeof *fmr
, GFP_KERNEL
);
455 return ERR_PTR(-ENOMEM
);
457 err
= mlx4_fmr_alloc(dev
->dev
, to_mpd(pd
)->pdn
, convert_access(acc
),
458 fmr_attr
->max_pages
, fmr_attr
->max_maps
,
459 fmr_attr
->page_shift
, &fmr
->mfmr
);
463 err
= mlx4_fmr_enable(to_mdev(pd
->device
)->dev
, &fmr
->mfmr
);
467 fmr
->ibfmr
.rkey
= fmr
->ibfmr
.lkey
= fmr
->mfmr
.mr
.key
;
472 (void) mlx4_mr_free(to_mdev(pd
->device
)->dev
, &fmr
->mfmr
.mr
);
480 int mlx4_ib_map_phys_fmr(struct ib_fmr
*ibfmr
, u64
*page_list
,
481 int npages
, u64 iova
)
483 struct mlx4_ib_fmr
*ifmr
= to_mfmr(ibfmr
);
484 struct mlx4_ib_dev
*dev
= to_mdev(ifmr
->ibfmr
.device
);
486 return mlx4_map_phys_fmr(dev
->dev
, &ifmr
->mfmr
, page_list
, npages
, iova
,
487 &ifmr
->ibfmr
.lkey
, &ifmr
->ibfmr
.rkey
);
490 int mlx4_ib_unmap_fmr(struct list_head
*fmr_list
)
492 struct ib_fmr
*ibfmr
;
494 struct mlx4_dev
*mdev
= NULL
;
496 list_for_each_entry(ibfmr
, fmr_list
, list
) {
497 if (mdev
&& to_mdev(ibfmr
->device
)->dev
!= mdev
)
499 mdev
= to_mdev(ibfmr
->device
)->dev
;
505 list_for_each_entry(ibfmr
, fmr_list
, list
) {
506 struct mlx4_ib_fmr
*ifmr
= to_mfmr(ibfmr
);
508 mlx4_fmr_unmap(mdev
, &ifmr
->mfmr
, &ifmr
->ibfmr
.lkey
, &ifmr
->ibfmr
.rkey
);
512 * Make sure all MPT status updates are visible before issuing
513 * SYNC_TPT firmware command.
517 err
= mlx4_SYNC_TPT(mdev
);
519 pr_warn("SYNC_TPT error %d when "
520 "unmapping FMRs\n", err
);
525 int mlx4_ib_fmr_dealloc(struct ib_fmr
*ibfmr
)
527 struct mlx4_ib_fmr
*ifmr
= to_mfmr(ibfmr
);
528 struct mlx4_ib_dev
*dev
= to_mdev(ibfmr
->device
);
531 err
= mlx4_fmr_free(dev
->dev
, &ifmr
->mfmr
);
539 static int mlx4_set_page(struct ib_mr
*ibmr
, u64 addr
)
541 struct mlx4_ib_mr
*mr
= to_mmr(ibmr
);
543 if (unlikely(mr
->npages
== mr
->max_pages
))
546 mr
->pages
[mr
->npages
++] = cpu_to_be64(addr
| MLX4_MTT_FLAG_PRESENT
);
551 int mlx4_ib_map_mr_sg(struct ib_mr
*ibmr
,
552 struct scatterlist
*sg
,
555 struct mlx4_ib_mr
*mr
= to_mmr(ibmr
);
560 ib_dma_sync_single_for_cpu(ibmr
->device
, mr
->page_map
,
561 sizeof(u64
) * mr
->max_pages
,
564 rc
= ib_sg_to_pages(ibmr
, sg
, sg_nents
, mlx4_set_page
);
566 ib_dma_sync_single_for_device(ibmr
->device
, mr
->page_map
,
567 sizeof(u64
) * mr
->max_pages
,