2 * Copyright (c) 2013, Mellanox Technologies inc. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 #include <linux/kref.h>
35 #include <linux/random.h>
36 #include <linux/debugfs.h>
37 #include <linux/export.h>
38 #include <rdma/ib_umem.h>
45 static __be64
*mr_align(__be64
*ptr
, int align
)
47 unsigned long mask
= align
- 1;
49 return (__be64
*)(((unsigned long)ptr
+ mask
) & ~mask
);
52 static int order2idx(struct mlx5_ib_dev
*dev
, int order
)
54 struct mlx5_mr_cache
*cache
= &dev
->cache
;
56 if (order
< cache
->ent
[0].order
)
59 return order
- cache
->ent
[0].order
;
62 static int add_keys(struct mlx5_ib_dev
*dev
, int c
, int num
)
64 struct device
*ddev
= dev
->ib_dev
.dma_device
;
65 struct mlx5_mr_cache
*cache
= &dev
->cache
;
66 struct mlx5_cache_ent
*ent
= &cache
->ent
[c
];
67 struct mlx5_create_mkey_mbox_in
*in
;
68 struct mlx5_ib_mr
*mr
;
69 int npages
= 1 << ent
->order
;
70 int size
= sizeof(u64
) * npages
;
74 in
= kzalloc(sizeof(*in
), GFP_KERNEL
);
78 for (i
= 0; i
< num
; i
++) {
79 mr
= kzalloc(sizeof(*mr
), GFP_KERNEL
);
84 mr
->order
= ent
->order
;
86 mr
->pas
= kmalloc(size
+ 0x3f, GFP_KERNEL
);
92 mr
->dma
= dma_map_single(ddev
, mr_align(mr
->pas
, 0x40), size
,
94 if (dma_mapping_error(ddev
, mr
->dma
)) {
101 in
->seg
.status
= 1 << 6;
102 in
->seg
.xlt_oct_size
= cpu_to_be32((npages
+ 1) / 2);
103 in
->seg
.qpn_mkey7_0
= cpu_to_be32(0xffffff << 8);
104 in
->seg
.flags
= MLX5_ACCESS_MODE_MTT
| MLX5_PERM_UMR_EN
;
105 in
->seg
.log2_page_size
= 12;
107 err
= mlx5_core_create_mkey(&dev
->mdev
, &mr
->mmr
, in
,
110 mlx5_ib_warn(dev
, "create mkey failed %d\n", err
);
111 dma_unmap_single(ddev
, mr
->dma
, size
, DMA_TO_DEVICE
);
116 cache
->last_add
= jiffies
;
118 spin_lock(&ent
->lock
);
119 list_add_tail(&mr
->list
, &ent
->head
);
122 spin_unlock(&ent
->lock
);
130 static void remove_keys(struct mlx5_ib_dev
*dev
, int c
, int num
)
132 struct device
*ddev
= dev
->ib_dev
.dma_device
;
133 struct mlx5_mr_cache
*cache
= &dev
->cache
;
134 struct mlx5_cache_ent
*ent
= &cache
->ent
[c
];
135 struct mlx5_ib_mr
*mr
;
140 for (i
= 0; i
< num
; i
++) {
141 spin_lock(&ent
->lock
);
142 if (list_empty(&ent
->head
)) {
143 spin_unlock(&ent
->lock
);
146 mr
= list_first_entry(&ent
->head
, struct mlx5_ib_mr
, list
);
150 spin_unlock(&ent
->lock
);
151 err
= mlx5_core_destroy_mkey(&dev
->mdev
, &mr
->mmr
);
153 mlx5_ib_warn(dev
, "failed destroy mkey\n");
155 size
= ALIGN(sizeof(u64
) * (1 << mr
->order
), 0x40);
156 dma_unmap_single(ddev
, mr
->dma
, size
, DMA_TO_DEVICE
);
163 static ssize_t
size_write(struct file
*filp
, const char __user
*buf
,
164 size_t count
, loff_t
*pos
)
166 struct mlx5_cache_ent
*ent
= filp
->private_data
;
167 struct mlx5_ib_dev
*dev
= ent
->dev
;
173 if (copy_from_user(lbuf
, buf
, sizeof(lbuf
)))
176 c
= order2idx(dev
, ent
->order
);
177 lbuf
[sizeof(lbuf
) - 1] = 0;
179 if (sscanf(lbuf
, "%u", &var
) != 1)
182 if (var
< ent
->limit
)
185 if (var
> ent
->size
) {
186 err
= add_keys(dev
, c
, var
- ent
->size
);
189 } else if (var
< ent
->size
) {
190 remove_keys(dev
, c
, ent
->size
- var
);
196 static ssize_t
size_read(struct file
*filp
, char __user
*buf
, size_t count
,
199 struct mlx5_cache_ent
*ent
= filp
->private_data
;
206 err
= snprintf(lbuf
, sizeof(lbuf
), "%d\n", ent
->size
);
210 if (copy_to_user(buf
, lbuf
, err
))
218 static const struct file_operations size_fops
= {
219 .owner
= THIS_MODULE
,
225 static ssize_t
limit_write(struct file
*filp
, const char __user
*buf
,
226 size_t count
, loff_t
*pos
)
228 struct mlx5_cache_ent
*ent
= filp
->private_data
;
229 struct mlx5_ib_dev
*dev
= ent
->dev
;
235 if (copy_from_user(lbuf
, buf
, sizeof(lbuf
)))
238 c
= order2idx(dev
, ent
->order
);
239 lbuf
[sizeof(lbuf
) - 1] = 0;
241 if (sscanf(lbuf
, "%u", &var
) != 1)
249 if (ent
->cur
< ent
->limit
) {
250 err
= add_keys(dev
, c
, 2 * ent
->limit
- ent
->cur
);
258 static ssize_t
limit_read(struct file
*filp
, char __user
*buf
, size_t count
,
261 struct mlx5_cache_ent
*ent
= filp
->private_data
;
268 err
= snprintf(lbuf
, sizeof(lbuf
), "%d\n", ent
->limit
);
272 if (copy_to_user(buf
, lbuf
, err
))
280 static const struct file_operations limit_fops
= {
281 .owner
= THIS_MODULE
,
283 .write
= limit_write
,
287 static int someone_adding(struct mlx5_mr_cache
*cache
)
291 for (i
= 0; i
< MAX_MR_CACHE_ENTRIES
; i
++) {
292 if (cache
->ent
[i
].cur
< cache
->ent
[i
].limit
)
299 static void __cache_work_func(struct mlx5_cache_ent
*ent
)
301 struct mlx5_ib_dev
*dev
= ent
->dev
;
302 struct mlx5_mr_cache
*cache
= &dev
->cache
;
303 int i
= order2idx(dev
, ent
->order
);
308 ent
= &dev
->cache
.ent
[i
];
309 if (ent
->cur
< 2 * ent
->limit
) {
311 if (ent
->cur
< 2 * ent
->limit
)
312 queue_work(cache
->wq
, &ent
->work
);
313 } else if (ent
->cur
> 2 * ent
->limit
) {
314 if (!someone_adding(cache
) &&
315 time_after(jiffies
, cache
->last_add
+ 60 * HZ
)) {
316 remove_keys(dev
, i
, 1);
317 if (ent
->cur
> ent
->limit
)
318 queue_work(cache
->wq
, &ent
->work
);
320 queue_delayed_work(cache
->wq
, &ent
->dwork
, 60 * HZ
);
325 static void delayed_cache_work_func(struct work_struct
*work
)
327 struct mlx5_cache_ent
*ent
;
329 ent
= container_of(work
, struct mlx5_cache_ent
, dwork
.work
);
330 __cache_work_func(ent
);
333 static void cache_work_func(struct work_struct
*work
)
335 struct mlx5_cache_ent
*ent
;
337 ent
= container_of(work
, struct mlx5_cache_ent
, work
);
338 __cache_work_func(ent
);
341 static struct mlx5_ib_mr
*alloc_cached_mr(struct mlx5_ib_dev
*dev
, int order
)
343 struct mlx5_mr_cache
*cache
= &dev
->cache
;
344 struct mlx5_ib_mr
*mr
= NULL
;
345 struct mlx5_cache_ent
*ent
;
349 c
= order2idx(dev
, order
);
350 if (c
< 0 || c
>= MAX_MR_CACHE_ENTRIES
) {
351 mlx5_ib_warn(dev
, "order %d, cache index %d\n", order
, c
);
355 for (i
= c
; i
< MAX_MR_CACHE_ENTRIES
; i
++) {
356 ent
= &cache
->ent
[i
];
358 mlx5_ib_dbg(dev
, "order %d, cache index %d\n", ent
->order
, i
);
360 spin_lock(&ent
->lock
);
361 if (!list_empty(&ent
->head
)) {
362 mr
= list_first_entry(&ent
->head
, struct mlx5_ib_mr
,
366 spin_unlock(&ent
->lock
);
367 if (ent
->cur
< ent
->limit
)
368 queue_work(cache
->wq
, &ent
->work
);
371 spin_unlock(&ent
->lock
);
373 queue_work(cache
->wq
, &ent
->work
);
380 cache
->ent
[c
].miss
++;
385 static void free_cached_mr(struct mlx5_ib_dev
*dev
, struct mlx5_ib_mr
*mr
)
387 struct mlx5_mr_cache
*cache
= &dev
->cache
;
388 struct mlx5_cache_ent
*ent
;
392 c
= order2idx(dev
, mr
->order
);
393 if (c
< 0 || c
>= MAX_MR_CACHE_ENTRIES
) {
394 mlx5_ib_warn(dev
, "order %d, cache index %d\n", mr
->order
, c
);
397 ent
= &cache
->ent
[c
];
398 spin_lock(&ent
->lock
);
399 list_add_tail(&mr
->list
, &ent
->head
);
401 if (ent
->cur
> 2 * ent
->limit
)
403 spin_unlock(&ent
->lock
);
406 queue_work(cache
->wq
, &ent
->work
);
409 static void clean_keys(struct mlx5_ib_dev
*dev
, int c
)
411 struct device
*ddev
= dev
->ib_dev
.dma_device
;
412 struct mlx5_mr_cache
*cache
= &dev
->cache
;
413 struct mlx5_cache_ent
*ent
= &cache
->ent
[c
];
414 struct mlx5_ib_mr
*mr
;
419 spin_lock(&ent
->lock
);
420 if (list_empty(&ent
->head
)) {
421 spin_unlock(&ent
->lock
);
424 mr
= list_first_entry(&ent
->head
, struct mlx5_ib_mr
, list
);
428 spin_unlock(&ent
->lock
);
429 err
= mlx5_core_destroy_mkey(&dev
->mdev
, &mr
->mmr
);
431 mlx5_ib_warn(dev
, "failed destroy mkey\n");
433 size
= ALIGN(sizeof(u64
) * (1 << mr
->order
), 0x40);
434 dma_unmap_single(ddev
, mr
->dma
, size
, DMA_TO_DEVICE
);
441 static int mlx5_mr_cache_debugfs_init(struct mlx5_ib_dev
*dev
)
443 struct mlx5_mr_cache
*cache
= &dev
->cache
;
444 struct mlx5_cache_ent
*ent
;
447 if (!mlx5_debugfs_root
)
450 cache
->root
= debugfs_create_dir("mr_cache", dev
->mdev
.priv
.dbg_root
);
454 for (i
= 0; i
< MAX_MR_CACHE_ENTRIES
; i
++) {
455 ent
= &cache
->ent
[i
];
456 sprintf(ent
->name
, "%d", ent
->order
);
457 ent
->dir
= debugfs_create_dir(ent
->name
, cache
->root
);
461 ent
->fsize
= debugfs_create_file("size", 0600, ent
->dir
, ent
,
466 ent
->flimit
= debugfs_create_file("limit", 0600, ent
->dir
, ent
,
471 ent
->fcur
= debugfs_create_u32("cur", 0400, ent
->dir
,
476 ent
->fmiss
= debugfs_create_u32("miss", 0600, ent
->dir
,
485 static void mlx5_mr_cache_debugfs_cleanup(struct mlx5_ib_dev
*dev
)
487 if (!mlx5_debugfs_root
)
490 debugfs_remove_recursive(dev
->cache
.root
);
493 int mlx5_mr_cache_init(struct mlx5_ib_dev
*dev
)
495 struct mlx5_mr_cache
*cache
= &dev
->cache
;
496 struct mlx5_cache_ent
*ent
;
502 cache
->wq
= create_singlethread_workqueue("mkey_cache");
504 mlx5_ib_warn(dev
, "failed to create work queue\n");
508 for (i
= 0; i
< MAX_MR_CACHE_ENTRIES
; i
++) {
509 INIT_LIST_HEAD(&cache
->ent
[i
].head
);
510 spin_lock_init(&cache
->ent
[i
].lock
);
512 ent
= &cache
->ent
[i
];
513 INIT_LIST_HEAD(&ent
->head
);
514 spin_lock_init(&ent
->lock
);
518 if (dev
->mdev
.profile
->mask
& MLX5_PROF_MASK_MR_CACHE
) {
519 size
= dev
->mdev
.profile
->mr_cache
[i
].size
;
520 limit
= dev
->mdev
.profile
->mr_cache
[i
].limit
;
522 size
= DEF_CACHE_SIZE
;
525 INIT_WORK(&ent
->work
, cache_work_func
);
526 INIT_DELAYED_WORK(&ent
->dwork
, delayed_cache_work_func
);
528 queue_work(cache
->wq
, &ent
->work
);
531 err
= mlx5_mr_cache_debugfs_init(dev
);
533 mlx5_ib_warn(dev
, "cache debugfs failure\n");
538 int mlx5_mr_cache_cleanup(struct mlx5_ib_dev
*dev
)
542 dev
->cache
.stopped
= 1;
543 destroy_workqueue(dev
->cache
.wq
);
545 mlx5_mr_cache_debugfs_cleanup(dev
);
547 for (i
= 0; i
< MAX_MR_CACHE_ENTRIES
; i
++)
553 struct ib_mr
*mlx5_ib_get_dma_mr(struct ib_pd
*pd
, int acc
)
555 struct mlx5_ib_dev
*dev
= to_mdev(pd
->device
);
556 struct mlx5_core_dev
*mdev
= &dev
->mdev
;
557 struct mlx5_create_mkey_mbox_in
*in
;
558 struct mlx5_mkey_seg
*seg
;
559 struct mlx5_ib_mr
*mr
;
562 mr
= kzalloc(sizeof(*mr
), GFP_KERNEL
);
564 return ERR_PTR(-ENOMEM
);
566 in
= kzalloc(sizeof(*in
), GFP_KERNEL
);
573 seg
->flags
= convert_access(acc
) | MLX5_ACCESS_MODE_PA
;
574 seg
->flags_pd
= cpu_to_be32(to_mpd(pd
)->pdn
| MLX5_MKEY_LEN64
);
575 seg
->qpn_mkey7_0
= cpu_to_be32(0xffffff << 8);
578 err
= mlx5_core_create_mkey(mdev
, &mr
->mmr
, in
, sizeof(*in
));
583 mr
->ibmr
.lkey
= mr
->mmr
.key
;
584 mr
->ibmr
.rkey
= mr
->mmr
.key
;
598 static int get_octo_len(u64 addr
, u64 len
, int page_size
)
603 offset
= addr
& (page_size
- 1);
604 npages
= ALIGN(len
+ offset
, page_size
) >> ilog2(page_size
);
605 return (npages
+ 1) / 2;
608 static int use_umr(int order
)
613 static void prep_umr_reg_wqe(struct ib_pd
*pd
, struct ib_send_wr
*wr
,
614 struct ib_sge
*sg
, u64 dma
, int n
, u32 key
,
615 int page_shift
, u64 virt_addr
, u64 len
,
618 struct mlx5_ib_dev
*dev
= to_mdev(pd
->device
);
619 struct ib_mr
*mr
= dev
->umrc
.mr
;
622 sg
->length
= ALIGN(sizeof(u64
) * n
, 64);
633 wr
->opcode
= MLX5_IB_WR_UMR
;
634 wr
->wr
.fast_reg
.page_list_len
= n
;
635 wr
->wr
.fast_reg
.page_shift
= page_shift
;
636 wr
->wr
.fast_reg
.rkey
= key
;
637 wr
->wr
.fast_reg
.iova_start
= virt_addr
;
638 wr
->wr
.fast_reg
.length
= len
;
639 wr
->wr
.fast_reg
.access_flags
= access_flags
;
640 wr
->wr
.fast_reg
.page_list
= (struct ib_fast_reg_page_list
*)pd
;
643 static void prep_umr_unreg_wqe(struct mlx5_ib_dev
*dev
,
644 struct ib_send_wr
*wr
, u32 key
)
646 wr
->send_flags
= MLX5_IB_SEND_UMR_UNREG
;
647 wr
->opcode
= MLX5_IB_WR_UMR
;
648 wr
->wr
.fast_reg
.rkey
= key
;
651 void mlx5_umr_cq_handler(struct ib_cq
*cq
, void *cq_context
)
653 struct mlx5_ib_mr
*mr
;
658 err
= ib_poll_cq(cq
, 1, &wc
);
660 pr_warn("poll cq error %d\n", err
);
666 mr
= (struct mlx5_ib_mr
*)(unsigned long)wc
.wr_id
;
667 mr
->status
= wc
.status
;
670 ib_req_notify_cq(cq
, IB_CQ_NEXT_COMP
);
673 static struct mlx5_ib_mr
*reg_umr(struct ib_pd
*pd
, struct ib_umem
*umem
,
674 u64 virt_addr
, u64 len
, int npages
,
675 int page_shift
, int order
, int access_flags
)
677 struct mlx5_ib_dev
*dev
= to_mdev(pd
->device
);
678 struct umr_common
*umrc
= &dev
->umrc
;
679 struct ib_send_wr wr
, *bad
;
680 struct mlx5_ib_mr
*mr
;
685 for (i
= 0; i
< 10; i
++) {
686 mr
= alloc_cached_mr(dev
, order
);
690 err
= add_keys(dev
, order2idx(dev
, order
), 1);
692 mlx5_ib_warn(dev
, "add_keys failed\n");
698 return ERR_PTR(-EAGAIN
);
700 mlx5_ib_populate_pas(dev
, umem
, page_shift
, mr_align(mr
->pas
, 0x40), 1);
702 memset(&wr
, 0, sizeof(wr
));
703 wr
.wr_id
= (u64
)(unsigned long)mr
;
704 prep_umr_reg_wqe(pd
, &wr
, &sg
, mr
->dma
, npages
, mr
->mmr
.key
, page_shift
, virt_addr
, len
, access_flags
);
706 /* We serialize polls so one process does not kidnap another's
707 * completion. This is not a problem since wr is completed in
711 init_completion(&mr
->done
);
712 err
= ib_post_send(umrc
->qp
, &wr
, &bad
);
714 mlx5_ib_warn(dev
, "post send failed, err %d\n", err
);
718 wait_for_completion(&mr
->done
);
721 if (mr
->status
!= IB_WC_SUCCESS
) {
722 mlx5_ib_warn(dev
, "reg umr failed\n");
730 free_cached_mr(dev
, mr
);
734 static struct mlx5_ib_mr
*reg_create(struct ib_pd
*pd
, u64 virt_addr
,
735 u64 length
, struct ib_umem
*umem
,
736 int npages
, int page_shift
,
739 struct mlx5_ib_dev
*dev
= to_mdev(pd
->device
);
740 struct mlx5_create_mkey_mbox_in
*in
;
741 struct mlx5_ib_mr
*mr
;
745 mr
= kzalloc(sizeof(*mr
), GFP_KERNEL
);
747 return ERR_PTR(-ENOMEM
);
749 inlen
= sizeof(*in
) + sizeof(*in
->pas
) * ((npages
+ 1) / 2) * 2;
750 in
= mlx5_vzalloc(inlen
);
755 mlx5_ib_populate_pas(dev
, umem
, page_shift
, in
->pas
, 0);
757 in
->seg
.flags
= convert_access(access_flags
) |
758 MLX5_ACCESS_MODE_MTT
;
759 in
->seg
.flags_pd
= cpu_to_be32(to_mpd(pd
)->pdn
);
760 in
->seg
.start_addr
= cpu_to_be64(virt_addr
);
761 in
->seg
.len
= cpu_to_be64(length
);
762 in
->seg
.bsfs_octo_size
= 0;
763 in
->seg
.xlt_oct_size
= cpu_to_be32(get_octo_len(virt_addr
, length
, 1 << page_shift
));
764 in
->seg
.log2_page_size
= page_shift
;
765 in
->seg
.qpn_mkey7_0
= cpu_to_be32(0xffffff << 8);
766 in
->xlat_oct_act_size
= cpu_to_be32(get_octo_len(virt_addr
, length
, 1 << page_shift
));
767 err
= mlx5_core_create_mkey(&dev
->mdev
, &mr
->mmr
, in
, inlen
);
769 mlx5_ib_warn(dev
, "create mkey failed\n");
775 mlx5_ib_dbg(dev
, "mkey = 0x%x\n", mr
->mmr
.key
);
788 struct ib_mr
*mlx5_ib_reg_user_mr(struct ib_pd
*pd
, u64 start
, u64 length
,
789 u64 virt_addr
, int access_flags
,
790 struct ib_udata
*udata
)
792 struct mlx5_ib_dev
*dev
= to_mdev(pd
->device
);
793 struct mlx5_ib_mr
*mr
= NULL
;
794 struct ib_umem
*umem
;
801 mlx5_ib_dbg(dev
, "start 0x%llx, virt_addr 0x%llx, length 0x%llx\n",
802 start
, virt_addr
, length
);
803 umem
= ib_umem_get(pd
->uobject
->context
, start
, length
, access_flags
,
806 mlx5_ib_dbg(dev
, "umem get failed\n");
810 mlx5_ib_cont_pages(umem
, start
, &npages
, &page_shift
, &ncont
, &order
);
812 mlx5_ib_warn(dev
, "avoid zero region\n");
817 mlx5_ib_dbg(dev
, "npages %d, ncont %d, order %d, page_shift %d\n",
818 npages
, ncont
, order
, page_shift
);
820 if (use_umr(order
)) {
821 mr
= reg_umr(pd
, umem
, virt_addr
, length
, ncont
, page_shift
,
822 order
, access_flags
);
823 if (PTR_ERR(mr
) == -EAGAIN
) {
824 mlx5_ib_dbg(dev
, "cache empty for order %d", order
);
830 mr
= reg_create(pd
, virt_addr
, length
, umem
, ncont
, page_shift
,
838 mlx5_ib_dbg(dev
, "mkey 0x%x\n", mr
->mmr
.key
);
842 spin_lock(&dev
->mr_lock
);
843 dev
->mdev
.priv
.reg_pages
+= npages
;
844 spin_unlock(&dev
->mr_lock
);
845 mr
->ibmr
.lkey
= mr
->mmr
.key
;
846 mr
->ibmr
.rkey
= mr
->mmr
.key
;
851 ib_umem_release(umem
);
855 static int unreg_umr(struct mlx5_ib_dev
*dev
, struct mlx5_ib_mr
*mr
)
857 struct umr_common
*umrc
= &dev
->umrc
;
858 struct ib_send_wr wr
, *bad
;
861 memset(&wr
, 0, sizeof(wr
));
862 wr
.wr_id
= (u64
)(unsigned long)mr
;
863 prep_umr_unreg_wqe(dev
, &wr
, mr
->mmr
.key
);
866 init_completion(&mr
->done
);
867 err
= ib_post_send(umrc
->qp
, &wr
, &bad
);
870 mlx5_ib_dbg(dev
, "err %d\n", err
);
873 wait_for_completion(&mr
->done
);
875 if (mr
->status
!= IB_WC_SUCCESS
) {
876 mlx5_ib_warn(dev
, "unreg umr failed\n");
886 int mlx5_ib_dereg_mr(struct ib_mr
*ibmr
)
888 struct mlx5_ib_dev
*dev
= to_mdev(ibmr
->device
);
889 struct mlx5_ib_mr
*mr
= to_mmr(ibmr
);
890 struct ib_umem
*umem
= mr
->umem
;
891 int npages
= mr
->npages
;
892 int umred
= mr
->umred
;
896 err
= mlx5_core_destroy_mkey(&dev
->mdev
, &mr
->mmr
);
898 mlx5_ib_warn(dev
, "failed to destroy mkey 0x%x (%d)\n",
903 err
= unreg_umr(dev
, mr
);
905 mlx5_ib_warn(dev
, "failed unregister\n");
908 free_cached_mr(dev
, mr
);
912 ib_umem_release(umem
);
913 spin_lock(&dev
->mr_lock
);
914 dev
->mdev
.priv
.reg_pages
-= npages
;
915 spin_unlock(&dev
->mr_lock
);
924 struct ib_mr
*mlx5_ib_alloc_fast_reg_mr(struct ib_pd
*pd
,
925 int max_page_list_len
)
927 struct mlx5_ib_dev
*dev
= to_mdev(pd
->device
);
928 struct mlx5_create_mkey_mbox_in
*in
;
929 struct mlx5_ib_mr
*mr
;
932 mr
= kzalloc(sizeof(*mr
), GFP_KERNEL
);
934 return ERR_PTR(-ENOMEM
);
936 in
= kzalloc(sizeof(*in
), GFP_KERNEL
);
942 in
->seg
.status
= 1 << 6; /* free */
943 in
->seg
.xlt_oct_size
= cpu_to_be32((max_page_list_len
+ 1) / 2);
944 in
->seg
.qpn_mkey7_0
= cpu_to_be32(0xffffff << 8);
945 in
->seg
.flags
= MLX5_PERM_UMR_EN
| MLX5_ACCESS_MODE_MTT
;
946 in
->seg
.flags_pd
= cpu_to_be32(to_mpd(pd
)->pdn
);
948 * TBD not needed - issue 197292 */
949 in
->seg
.log2_page_size
= PAGE_SHIFT
;
951 err
= mlx5_core_create_mkey(&dev
->mdev
, &mr
->mmr
, in
, sizeof(*in
));
956 mr
->ibmr
.lkey
= mr
->mmr
.key
;
957 mr
->ibmr
.rkey
= mr
->mmr
.key
;
967 struct ib_fast_reg_page_list
*mlx5_ib_alloc_fast_reg_page_list(struct ib_device
*ibdev
,
970 struct mlx5_ib_fast_reg_page_list
*mfrpl
;
971 int size
= page_list_len
* sizeof(u64
);
973 mfrpl
= kmalloc(sizeof(*mfrpl
), GFP_KERNEL
);
975 return ERR_PTR(-ENOMEM
);
977 mfrpl
->ibfrpl
.page_list
= kmalloc(size
, GFP_KERNEL
);
978 if (!mfrpl
->ibfrpl
.page_list
)
981 mfrpl
->mapped_page_list
= dma_alloc_coherent(ibdev
->dma_device
,
984 if (!mfrpl
->mapped_page_list
)
987 WARN_ON(mfrpl
->map
& 0x3f);
989 return &mfrpl
->ibfrpl
;
992 kfree(mfrpl
->ibfrpl
.page_list
);
994 return ERR_PTR(-ENOMEM
);
997 void mlx5_ib_free_fast_reg_page_list(struct ib_fast_reg_page_list
*page_list
)
999 struct mlx5_ib_fast_reg_page_list
*mfrpl
= to_mfrpl(page_list
);
1000 struct mlx5_ib_dev
*dev
= to_mdev(page_list
->device
);
1001 int size
= page_list
->max_page_list_len
* sizeof(u64
);
1003 dma_free_coherent(&dev
->mdev
.pdev
->dev
, size
, mfrpl
->mapped_page_list
,
1005 kfree(mfrpl
->ibfrpl
.page_list
);