2 * Copyright (c) 2013-2015, Mellanox Technologies. 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 <linux/delay.h>
39 #include <rdma/ib_umem.h>
40 #include <rdma/ib_umem_odp.h>
41 #include <rdma/ib_verbs.h>
45 MAX_PENDING_REG_MR
= 8,
48 #define MLX5_UMR_ALIGN 2048
50 static int clean_mr(struct mlx5_ib_dev
*dev
, struct mlx5_ib_mr
*mr
);
51 static int dereg_mr(struct mlx5_ib_dev
*dev
, struct mlx5_ib_mr
*mr
);
52 static int mr_cache_max_order(struct mlx5_ib_dev
*dev
);
53 static int unreg_umr(struct mlx5_ib_dev
*dev
, struct mlx5_ib_mr
*mr
);
55 static int destroy_mkey(struct mlx5_ib_dev
*dev
, struct mlx5_ib_mr
*mr
)
57 int err
= mlx5_core_destroy_mkey(dev
->mdev
, &mr
->mmkey
);
59 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
60 /* Wait until all page fault handlers using the mr complete. */
61 synchronize_srcu(&dev
->mr_srcu
);
67 static int order2idx(struct mlx5_ib_dev
*dev
, int order
)
69 struct mlx5_mr_cache
*cache
= &dev
->cache
;
71 if (order
< cache
->ent
[0].order
)
74 return order
- cache
->ent
[0].order
;
77 static bool use_umr_mtt_update(struct mlx5_ib_mr
*mr
, u64 start
, u64 length
)
79 return ((u64
)1 << mr
->order
) * MLX5_ADAPTER_PAGE_SIZE
>=
80 length
+ (start
& (MLX5_ADAPTER_PAGE_SIZE
- 1));
83 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
84 static void update_odp_mr(struct mlx5_ib_mr
*mr
)
86 if (mr
->umem
->odp_data
) {
88 * This barrier prevents the compiler from moving the
89 * setting of umem->odp_data->private to point to our
90 * MR, before reg_umr finished, to ensure that the MR
91 * initialization have finished before starting to
92 * handle invalidations.
95 mr
->umem
->odp_data
->private = mr
;
97 * Make sure we will see the new
98 * umem->odp_data->private value in the invalidation
99 * routines, before we can get page faults on the
100 * MR. Page faults can happen once we put the MR in
101 * the tree, below this line. Without the barrier,
102 * there can be a fault handling and an invalidation
103 * before umem->odp_data->private == mr is visible to
104 * the invalidation handler.
111 static void reg_mr_callback(int status
, void *context
)
113 struct mlx5_ib_mr
*mr
= context
;
114 struct mlx5_ib_dev
*dev
= mr
->dev
;
115 struct mlx5_mr_cache
*cache
= &dev
->cache
;
116 int c
= order2idx(dev
, mr
->order
);
117 struct mlx5_cache_ent
*ent
= &cache
->ent
[c
];
120 struct mlx5_mkey_table
*table
= &dev
->mdev
->priv
.mkey_table
;
123 spin_lock_irqsave(&ent
->lock
, flags
);
125 spin_unlock_irqrestore(&ent
->lock
, flags
);
127 mlx5_ib_warn(dev
, "async reg mr failed. status %d\n", status
);
130 mod_timer(&dev
->delay_timer
, jiffies
+ HZ
);
134 mr
->mmkey
.type
= MLX5_MKEY_MR
;
135 spin_lock_irqsave(&dev
->mdev
->priv
.mkey_lock
, flags
);
136 key
= dev
->mdev
->priv
.mkey_key
++;
137 spin_unlock_irqrestore(&dev
->mdev
->priv
.mkey_lock
, flags
);
138 mr
->mmkey
.key
= mlx5_idx_to_mkey(MLX5_GET(create_mkey_out
, mr
->out
, mkey_index
)) | key
;
140 cache
->last_add
= jiffies
;
142 spin_lock_irqsave(&ent
->lock
, flags
);
143 list_add_tail(&mr
->list
, &ent
->head
);
146 spin_unlock_irqrestore(&ent
->lock
, flags
);
148 write_lock_irqsave(&table
->lock
, flags
);
149 err
= radix_tree_insert(&table
->tree
, mlx5_base_mkey(mr
->mmkey
.key
),
152 pr_err("Error inserting to mkey tree. 0x%x\n", -err
);
153 write_unlock_irqrestore(&table
->lock
, flags
);
155 if (!completion_done(&ent
->compl))
156 complete(&ent
->compl);
159 static int add_keys(struct mlx5_ib_dev
*dev
, int c
, int num
)
161 struct mlx5_mr_cache
*cache
= &dev
->cache
;
162 struct mlx5_cache_ent
*ent
= &cache
->ent
[c
];
163 int inlen
= MLX5_ST_SZ_BYTES(create_mkey_in
);
164 struct mlx5_ib_mr
*mr
;
170 in
= kzalloc(inlen
, GFP_KERNEL
);
174 mkc
= MLX5_ADDR_OF(create_mkey_in
, in
, memory_key_mkey_entry
);
175 for (i
= 0; i
< num
; i
++) {
176 if (ent
->pending
>= MAX_PENDING_REG_MR
) {
181 mr
= kzalloc(sizeof(*mr
), GFP_KERNEL
);
186 mr
->order
= ent
->order
;
187 mr
->allocated_from_cache
= 1;
190 MLX5_SET(mkc
, mkc
, free
, 1);
191 MLX5_SET(mkc
, mkc
, umr_en
, 1);
192 MLX5_SET(mkc
, mkc
, access_mode
, ent
->access_mode
);
194 MLX5_SET(mkc
, mkc
, qpn
, 0xffffff);
195 MLX5_SET(mkc
, mkc
, translations_octword_size
, ent
->xlt
);
196 MLX5_SET(mkc
, mkc
, log_page_size
, ent
->page
);
198 spin_lock_irq(&ent
->lock
);
200 spin_unlock_irq(&ent
->lock
);
201 err
= mlx5_core_create_mkey_cb(dev
->mdev
, &mr
->mmkey
,
203 mr
->out
, sizeof(mr
->out
),
204 reg_mr_callback
, mr
);
206 spin_lock_irq(&ent
->lock
);
208 spin_unlock_irq(&ent
->lock
);
209 mlx5_ib_warn(dev
, "create mkey failed %d\n", err
);
219 static void remove_keys(struct mlx5_ib_dev
*dev
, int c
, int num
)
221 struct mlx5_mr_cache
*cache
= &dev
->cache
;
222 struct mlx5_cache_ent
*ent
= &cache
->ent
[c
];
223 struct mlx5_ib_mr
*mr
;
227 for (i
= 0; i
< num
; i
++) {
228 spin_lock_irq(&ent
->lock
);
229 if (list_empty(&ent
->head
)) {
230 spin_unlock_irq(&ent
->lock
);
233 mr
= list_first_entry(&ent
->head
, struct mlx5_ib_mr
, list
);
237 spin_unlock_irq(&ent
->lock
);
238 err
= destroy_mkey(dev
, mr
);
240 mlx5_ib_warn(dev
, "failed destroy mkey\n");
246 static ssize_t
size_write(struct file
*filp
, const char __user
*buf
,
247 size_t count
, loff_t
*pos
)
249 struct mlx5_cache_ent
*ent
= filp
->private_data
;
250 struct mlx5_ib_dev
*dev
= ent
->dev
;
256 if (copy_from_user(lbuf
, buf
, sizeof(lbuf
)))
259 c
= order2idx(dev
, ent
->order
);
260 lbuf
[sizeof(lbuf
) - 1] = 0;
262 if (sscanf(lbuf
, "%u", &var
) != 1)
265 if (var
< ent
->limit
)
268 if (var
> ent
->size
) {
270 err
= add_keys(dev
, c
, var
- ent
->size
);
271 if (err
&& err
!= -EAGAIN
)
274 usleep_range(3000, 5000);
276 } else if (var
< ent
->size
) {
277 remove_keys(dev
, c
, ent
->size
- var
);
283 static ssize_t
size_read(struct file
*filp
, char __user
*buf
, size_t count
,
286 struct mlx5_cache_ent
*ent
= filp
->private_data
;
293 err
= snprintf(lbuf
, sizeof(lbuf
), "%d\n", ent
->size
);
297 if (copy_to_user(buf
, lbuf
, err
))
305 static const struct file_operations size_fops
= {
306 .owner
= THIS_MODULE
,
312 static ssize_t
limit_write(struct file
*filp
, const char __user
*buf
,
313 size_t count
, loff_t
*pos
)
315 struct mlx5_cache_ent
*ent
= filp
->private_data
;
316 struct mlx5_ib_dev
*dev
= ent
->dev
;
322 if (copy_from_user(lbuf
, buf
, sizeof(lbuf
)))
325 c
= order2idx(dev
, ent
->order
);
326 lbuf
[sizeof(lbuf
) - 1] = 0;
328 if (sscanf(lbuf
, "%u", &var
) != 1)
336 if (ent
->cur
< ent
->limit
) {
337 err
= add_keys(dev
, c
, 2 * ent
->limit
- ent
->cur
);
345 static ssize_t
limit_read(struct file
*filp
, char __user
*buf
, size_t count
,
348 struct mlx5_cache_ent
*ent
= filp
->private_data
;
355 err
= snprintf(lbuf
, sizeof(lbuf
), "%d\n", ent
->limit
);
359 if (copy_to_user(buf
, lbuf
, err
))
367 static const struct file_operations limit_fops
= {
368 .owner
= THIS_MODULE
,
370 .write
= limit_write
,
374 static int someone_adding(struct mlx5_mr_cache
*cache
)
378 for (i
= 0; i
< MAX_MR_CACHE_ENTRIES
; i
++) {
379 if (cache
->ent
[i
].cur
< cache
->ent
[i
].limit
)
386 static void __cache_work_func(struct mlx5_cache_ent
*ent
)
388 struct mlx5_ib_dev
*dev
= ent
->dev
;
389 struct mlx5_mr_cache
*cache
= &dev
->cache
;
390 int i
= order2idx(dev
, ent
->order
);
396 ent
= &dev
->cache
.ent
[i
];
397 if (ent
->cur
< 2 * ent
->limit
&& !dev
->fill_delay
) {
398 err
= add_keys(dev
, i
, 1);
399 if (ent
->cur
< 2 * ent
->limit
) {
400 if (err
== -EAGAIN
) {
401 mlx5_ib_dbg(dev
, "returned eagain, order %d\n",
403 queue_delayed_work(cache
->wq
, &ent
->dwork
,
404 msecs_to_jiffies(3));
406 mlx5_ib_warn(dev
, "command failed order %d, err %d\n",
408 queue_delayed_work(cache
->wq
, &ent
->dwork
,
409 msecs_to_jiffies(1000));
411 queue_work(cache
->wq
, &ent
->work
);
414 } else if (ent
->cur
> 2 * ent
->limit
) {
416 * The remove_keys() logic is performed as garbage collection
417 * task. Such task is intended to be run when no other active
418 * processes are running.
420 * The need_resched() will return TRUE if there are user tasks
421 * to be activated in near future.
423 * In such case, we don't execute remove_keys() and postpone
424 * the garbage collection work to try to run in next cycle,
425 * in order to free CPU resources to other tasks.
427 if (!need_resched() && !someone_adding(cache
) &&
428 time_after(jiffies
, cache
->last_add
+ 300 * HZ
)) {
429 remove_keys(dev
, i
, 1);
430 if (ent
->cur
> ent
->limit
)
431 queue_work(cache
->wq
, &ent
->work
);
433 queue_delayed_work(cache
->wq
, &ent
->dwork
, 300 * HZ
);
438 static void delayed_cache_work_func(struct work_struct
*work
)
440 struct mlx5_cache_ent
*ent
;
442 ent
= container_of(work
, struct mlx5_cache_ent
, dwork
.work
);
443 __cache_work_func(ent
);
446 static void cache_work_func(struct work_struct
*work
)
448 struct mlx5_cache_ent
*ent
;
450 ent
= container_of(work
, struct mlx5_cache_ent
, work
);
451 __cache_work_func(ent
);
454 struct mlx5_ib_mr
*mlx5_mr_cache_alloc(struct mlx5_ib_dev
*dev
, int entry
)
456 struct mlx5_mr_cache
*cache
= &dev
->cache
;
457 struct mlx5_cache_ent
*ent
;
458 struct mlx5_ib_mr
*mr
;
461 if (entry
< 0 || entry
>= MAX_MR_CACHE_ENTRIES
) {
462 mlx5_ib_err(dev
, "cache entry %d is out of range\n", entry
);
466 ent
= &cache
->ent
[entry
];
468 spin_lock_irq(&ent
->lock
);
469 if (list_empty(&ent
->head
)) {
470 spin_unlock_irq(&ent
->lock
);
472 err
= add_keys(dev
, entry
, 1);
473 if (err
&& err
!= -EAGAIN
)
476 wait_for_completion(&ent
->compl);
478 mr
= list_first_entry(&ent
->head
, struct mlx5_ib_mr
,
482 spin_unlock_irq(&ent
->lock
);
483 if (ent
->cur
< ent
->limit
)
484 queue_work(cache
->wq
, &ent
->work
);
490 static struct mlx5_ib_mr
*alloc_cached_mr(struct mlx5_ib_dev
*dev
, int order
)
492 struct mlx5_mr_cache
*cache
= &dev
->cache
;
493 struct mlx5_ib_mr
*mr
= NULL
;
494 struct mlx5_cache_ent
*ent
;
495 int last_umr_cache_entry
;
499 c
= order2idx(dev
, order
);
500 last_umr_cache_entry
= order2idx(dev
, mr_cache_max_order(dev
));
501 if (c
< 0 || c
> last_umr_cache_entry
) {
502 mlx5_ib_warn(dev
, "order %d, cache index %d\n", order
, c
);
506 for (i
= c
; i
<= last_umr_cache_entry
; i
++) {
507 ent
= &cache
->ent
[i
];
509 mlx5_ib_dbg(dev
, "order %d, cache index %d\n", ent
->order
, i
);
511 spin_lock_irq(&ent
->lock
);
512 if (!list_empty(&ent
->head
)) {
513 mr
= list_first_entry(&ent
->head
, struct mlx5_ib_mr
,
517 spin_unlock_irq(&ent
->lock
);
518 if (ent
->cur
< ent
->limit
)
519 queue_work(cache
->wq
, &ent
->work
);
522 spin_unlock_irq(&ent
->lock
);
524 queue_work(cache
->wq
, &ent
->work
);
528 cache
->ent
[c
].miss
++;
533 void mlx5_mr_cache_free(struct mlx5_ib_dev
*dev
, struct mlx5_ib_mr
*mr
)
535 struct mlx5_mr_cache
*cache
= &dev
->cache
;
536 struct mlx5_cache_ent
*ent
;
540 c
= order2idx(dev
, mr
->order
);
541 if (c
< 0 || c
>= MAX_MR_CACHE_ENTRIES
) {
542 mlx5_ib_warn(dev
, "order %d, cache index %d\n", mr
->order
, c
);
546 if (unreg_umr(dev
, mr
))
549 ent
= &cache
->ent
[c
];
550 spin_lock_irq(&ent
->lock
);
551 list_add_tail(&mr
->list
, &ent
->head
);
553 if (ent
->cur
> 2 * ent
->limit
)
555 spin_unlock_irq(&ent
->lock
);
558 queue_work(cache
->wq
, &ent
->work
);
561 static void clean_keys(struct mlx5_ib_dev
*dev
, int c
)
563 struct mlx5_mr_cache
*cache
= &dev
->cache
;
564 struct mlx5_cache_ent
*ent
= &cache
->ent
[c
];
565 struct mlx5_ib_mr
*mr
;
568 cancel_delayed_work(&ent
->dwork
);
570 spin_lock_irq(&ent
->lock
);
571 if (list_empty(&ent
->head
)) {
572 spin_unlock_irq(&ent
->lock
);
575 mr
= list_first_entry(&ent
->head
, struct mlx5_ib_mr
, list
);
579 spin_unlock_irq(&ent
->lock
);
580 err
= destroy_mkey(dev
, mr
);
582 mlx5_ib_warn(dev
, "failed destroy mkey\n");
588 static void mlx5_mr_cache_debugfs_cleanup(struct mlx5_ib_dev
*dev
)
590 if (!mlx5_debugfs_root
)
593 debugfs_remove_recursive(dev
->cache
.root
);
594 dev
->cache
.root
= NULL
;
597 static int mlx5_mr_cache_debugfs_init(struct mlx5_ib_dev
*dev
)
599 struct mlx5_mr_cache
*cache
= &dev
->cache
;
600 struct mlx5_cache_ent
*ent
;
603 if (!mlx5_debugfs_root
)
606 cache
->root
= debugfs_create_dir("mr_cache", dev
->mdev
->priv
.dbg_root
);
610 for (i
= 0; i
< MAX_MR_CACHE_ENTRIES
; i
++) {
611 ent
= &cache
->ent
[i
];
612 sprintf(ent
->name
, "%d", ent
->order
);
613 ent
->dir
= debugfs_create_dir(ent
->name
, cache
->root
);
617 ent
->fsize
= debugfs_create_file("size", 0600, ent
->dir
, ent
,
622 ent
->flimit
= debugfs_create_file("limit", 0600, ent
->dir
, ent
,
627 ent
->fcur
= debugfs_create_u32("cur", 0400, ent
->dir
,
632 ent
->fmiss
= debugfs_create_u32("miss", 0600, ent
->dir
,
640 mlx5_mr_cache_debugfs_cleanup(dev
);
645 static void delay_time_func(struct timer_list
*t
)
647 struct mlx5_ib_dev
*dev
= from_timer(dev
, t
, delay_timer
);
652 int mlx5_mr_cache_init(struct mlx5_ib_dev
*dev
)
654 struct mlx5_mr_cache
*cache
= &dev
->cache
;
655 struct mlx5_cache_ent
*ent
;
659 mutex_init(&dev
->slow_path_mutex
);
660 cache
->wq
= alloc_ordered_workqueue("mkey_cache", WQ_MEM_RECLAIM
);
662 mlx5_ib_warn(dev
, "failed to create work queue\n");
666 timer_setup(&dev
->delay_timer
, delay_time_func
, 0);
667 for (i
= 0; i
< MAX_MR_CACHE_ENTRIES
; i
++) {
668 ent
= &cache
->ent
[i
];
669 INIT_LIST_HEAD(&ent
->head
);
670 spin_lock_init(&ent
->lock
);
675 init_completion(&ent
->compl);
676 INIT_WORK(&ent
->work
, cache_work_func
);
677 INIT_DELAYED_WORK(&ent
->dwork
, delayed_cache_work_func
);
678 queue_work(cache
->wq
, &ent
->work
);
680 if (i
> MR_CACHE_LAST_STD_ENTRY
) {
681 mlx5_odp_init_mr_cache_entry(ent
);
685 if (ent
->order
> mr_cache_max_order(dev
))
688 ent
->page
= PAGE_SHIFT
;
689 ent
->xlt
= (1 << ent
->order
) * sizeof(struct mlx5_mtt
) /
690 MLX5_IB_UMR_OCTOWORD
;
691 ent
->access_mode
= MLX5_MKC_ACCESS_MODE_MTT
;
692 if ((dev
->mdev
->profile
->mask
& MLX5_PROF_MASK_MR_CACHE
) &&
693 mlx5_core_is_pf(dev
->mdev
))
694 ent
->limit
= dev
->mdev
->profile
->mr_cache
[i
].limit
;
699 err
= mlx5_mr_cache_debugfs_init(dev
);
701 mlx5_ib_warn(dev
, "cache debugfs failure\n");
704 * We don't want to fail driver if debugfs failed to initialize,
705 * so we are not forwarding error to the user.
711 static void wait_for_async_commands(struct mlx5_ib_dev
*dev
)
713 struct mlx5_mr_cache
*cache
= &dev
->cache
;
714 struct mlx5_cache_ent
*ent
;
719 for (i
= 0; i
< MAX_MR_CACHE_ENTRIES
; i
++) {
720 ent
= &cache
->ent
[i
];
721 for (j
= 0 ; j
< 1000; j
++) {
727 for (i
= 0; i
< MAX_MR_CACHE_ENTRIES
; i
++) {
728 ent
= &cache
->ent
[i
];
729 total
+= ent
->pending
;
733 mlx5_ib_warn(dev
, "aborted while there are %d pending mr requests\n", total
);
735 mlx5_ib_warn(dev
, "done with all pending requests\n");
738 int mlx5_mr_cache_cleanup(struct mlx5_ib_dev
*dev
)
742 dev
->cache
.stopped
= 1;
743 flush_workqueue(dev
->cache
.wq
);
745 mlx5_mr_cache_debugfs_cleanup(dev
);
747 for (i
= 0; i
< MAX_MR_CACHE_ENTRIES
; i
++)
750 destroy_workqueue(dev
->cache
.wq
);
751 wait_for_async_commands(dev
);
752 del_timer_sync(&dev
->delay_timer
);
757 struct ib_mr
*mlx5_ib_get_dma_mr(struct ib_pd
*pd
, int acc
)
759 struct mlx5_ib_dev
*dev
= to_mdev(pd
->device
);
760 int inlen
= MLX5_ST_SZ_BYTES(create_mkey_in
);
761 struct mlx5_core_dev
*mdev
= dev
->mdev
;
762 struct mlx5_ib_mr
*mr
;
767 mr
= kzalloc(sizeof(*mr
), GFP_KERNEL
);
769 return ERR_PTR(-ENOMEM
);
771 in
= kzalloc(inlen
, GFP_KERNEL
);
777 mkc
= MLX5_ADDR_OF(create_mkey_in
, in
, memory_key_mkey_entry
);
779 MLX5_SET(mkc
, mkc
, access_mode
, MLX5_MKC_ACCESS_MODE_PA
);
780 MLX5_SET(mkc
, mkc
, a
, !!(acc
& IB_ACCESS_REMOTE_ATOMIC
));
781 MLX5_SET(mkc
, mkc
, rw
, !!(acc
& IB_ACCESS_REMOTE_WRITE
));
782 MLX5_SET(mkc
, mkc
, rr
, !!(acc
& IB_ACCESS_REMOTE_READ
));
783 MLX5_SET(mkc
, mkc
, lw
, !!(acc
& IB_ACCESS_LOCAL_WRITE
));
784 MLX5_SET(mkc
, mkc
, lr
, 1);
786 MLX5_SET(mkc
, mkc
, length64
, 1);
787 MLX5_SET(mkc
, mkc
, pd
, to_mpd(pd
)->pdn
);
788 MLX5_SET(mkc
, mkc
, qpn
, 0xffffff);
789 MLX5_SET64(mkc
, mkc
, start_addr
, 0);
791 err
= mlx5_core_create_mkey(mdev
, &mr
->mmkey
, in
, inlen
);
796 mr
->mmkey
.type
= MLX5_MKEY_MR
;
797 mr
->ibmr
.lkey
= mr
->mmkey
.key
;
798 mr
->ibmr
.rkey
= mr
->mmkey
.key
;
812 static int get_octo_len(u64 addr
, u64 len
, int page_shift
)
814 u64 page_size
= 1ULL << page_shift
;
818 offset
= addr
& (page_size
- 1);
819 npages
= ALIGN(len
+ offset
, page_size
) >> page_shift
;
820 return (npages
+ 1) / 2;
823 static int mr_cache_max_order(struct mlx5_ib_dev
*dev
)
825 if (MLX5_CAP_GEN(dev
->mdev
, umr_extended_translation_offset
))
826 return MR_CACHE_LAST_STD_ENTRY
+ 2;
827 return MLX5_MAX_UMR_SHIFT
;
830 static int mr_umem_get(struct ib_pd
*pd
, u64 start
, u64 length
,
831 int access_flags
, struct ib_umem
**umem
,
832 int *npages
, int *page_shift
, int *ncont
,
835 struct mlx5_ib_dev
*dev
= to_mdev(pd
->device
);
838 *umem
= ib_umem_get(pd
->uobject
->context
, start
, length
,
840 err
= PTR_ERR_OR_ZERO(*umem
);
842 mlx5_ib_err(dev
, "umem get failed (%d)\n", err
);
846 mlx5_ib_cont_pages(*umem
, start
, MLX5_MKEY_PAGE_SHIFT_MASK
, npages
,
847 page_shift
, ncont
, order
);
849 mlx5_ib_warn(dev
, "avoid zero region\n");
850 ib_umem_release(*umem
);
854 mlx5_ib_dbg(dev
, "npages %d, ncont %d, order %d, page_shift %d\n",
855 *npages
, *ncont
, *order
, *page_shift
);
860 static void mlx5_ib_umr_done(struct ib_cq
*cq
, struct ib_wc
*wc
)
862 struct mlx5_ib_umr_context
*context
=
863 container_of(wc
->wr_cqe
, struct mlx5_ib_umr_context
, cqe
);
865 context
->status
= wc
->status
;
866 complete(&context
->done
);
869 static inline void mlx5_ib_init_umr_context(struct mlx5_ib_umr_context
*context
)
871 context
->cqe
.done
= mlx5_ib_umr_done
;
872 context
->status
= -1;
873 init_completion(&context
->done
);
876 static int mlx5_ib_post_send_wait(struct mlx5_ib_dev
*dev
,
877 struct mlx5_umr_wr
*umrwr
)
879 struct umr_common
*umrc
= &dev
->umrc
;
880 struct ib_send_wr
*bad
;
882 struct mlx5_ib_umr_context umr_context
;
884 mlx5_ib_init_umr_context(&umr_context
);
885 umrwr
->wr
.wr_cqe
= &umr_context
.cqe
;
888 err
= ib_post_send(umrc
->qp
, &umrwr
->wr
, &bad
);
890 mlx5_ib_warn(dev
, "UMR post send failed, err %d\n", err
);
892 wait_for_completion(&umr_context
.done
);
893 if (umr_context
.status
!= IB_WC_SUCCESS
) {
894 mlx5_ib_warn(dev
, "reg umr failed (%u)\n",
903 static struct mlx5_ib_mr
*alloc_mr_from_cache(
904 struct ib_pd
*pd
, struct ib_umem
*umem
,
905 u64 virt_addr
, u64 len
, int npages
,
906 int page_shift
, int order
, int access_flags
)
908 struct mlx5_ib_dev
*dev
= to_mdev(pd
->device
);
909 struct mlx5_ib_mr
*mr
;
913 for (i
= 0; i
< 1; i
++) {
914 mr
= alloc_cached_mr(dev
, order
);
918 err
= add_keys(dev
, order2idx(dev
, order
), 1);
919 if (err
&& err
!= -EAGAIN
) {
920 mlx5_ib_warn(dev
, "add_keys failed, err %d\n", err
);
926 return ERR_PTR(-EAGAIN
);
930 mr
->access_flags
= access_flags
;
931 mr
->desc_size
= sizeof(struct mlx5_mtt
);
932 mr
->mmkey
.iova
= virt_addr
;
933 mr
->mmkey
.size
= len
;
934 mr
->mmkey
.pd
= to_mpd(pd
)->pdn
;
939 static inline int populate_xlt(struct mlx5_ib_mr
*mr
, int idx
, int npages
,
940 void *xlt
, int page_shift
, size_t size
,
943 struct mlx5_ib_dev
*dev
= mr
->dev
;
944 struct ib_umem
*umem
= mr
->umem
;
945 if (flags
& MLX5_IB_UPD_XLT_INDIRECT
) {
946 mlx5_odp_populate_klm(xlt
, idx
, npages
, mr
, flags
);
950 npages
= min_t(size_t, npages
, ib_umem_num_pages(umem
) - idx
);
952 if (!(flags
& MLX5_IB_UPD_XLT_ZAP
)) {
953 __mlx5_ib_populate_pas(dev
, umem
, page_shift
,
955 MLX5_IB_MTT_PRESENT
);
956 /* Clear padding after the pages
957 * brought from the umem.
959 memset(xlt
+ (npages
* sizeof(struct mlx5_mtt
)), 0,
960 size
- npages
* sizeof(struct mlx5_mtt
));
966 #define MLX5_MAX_UMR_CHUNK ((1 << (MLX5_MAX_UMR_SHIFT + 4)) - \
967 MLX5_UMR_MTT_ALIGNMENT)
968 #define MLX5_SPARE_UMR_CHUNK 0x10000
970 int mlx5_ib_update_xlt(struct mlx5_ib_mr
*mr
, u64 idx
, int npages
,
971 int page_shift
, int flags
)
973 struct mlx5_ib_dev
*dev
= mr
->dev
;
974 struct device
*ddev
= dev
->ib_dev
.dev
.parent
;
975 struct mlx5_ib_ucontext
*uctx
= NULL
;
979 struct mlx5_umr_wr wr
;
982 int desc_size
= (flags
& MLX5_IB_UPD_XLT_INDIRECT
)
983 ? sizeof(struct mlx5_klm
)
984 : sizeof(struct mlx5_mtt
);
985 const int page_align
= MLX5_UMR_MTT_ALIGNMENT
/ desc_size
;
986 const int page_mask
= page_align
- 1;
987 size_t pages_mapped
= 0;
988 size_t pages_to_map
= 0;
989 size_t pages_iter
= 0;
992 /* UMR copies MTTs in units of MLX5_UMR_MTT_ALIGNMENT bytes,
993 * so we need to align the offset and length accordingly
995 if (idx
& page_mask
) {
996 npages
+= idx
& page_mask
;
1000 gfp
= flags
& MLX5_IB_UPD_XLT_ATOMIC
? GFP_ATOMIC
: GFP_KERNEL
;
1001 gfp
|= __GFP_ZERO
| __GFP_NOWARN
;
1003 pages_to_map
= ALIGN(npages
, page_align
);
1004 size
= desc_size
* pages_to_map
;
1005 size
= min_t(int, size
, MLX5_MAX_UMR_CHUNK
);
1007 xlt
= (void *)__get_free_pages(gfp
, get_order(size
));
1008 if (!xlt
&& size
> MLX5_SPARE_UMR_CHUNK
) {
1009 mlx5_ib_dbg(dev
, "Failed to allocate %d bytes of order %d. fallback to spare UMR allocation od %d bytes\n",
1010 size
, get_order(size
), MLX5_SPARE_UMR_CHUNK
);
1012 size
= MLX5_SPARE_UMR_CHUNK
;
1013 xlt
= (void *)__get_free_pages(gfp
, get_order(size
));
1017 uctx
= to_mucontext(mr
->ibmr
.pd
->uobject
->context
);
1018 mlx5_ib_warn(dev
, "Using XLT emergency buffer\n");
1020 xlt
= (void *)uctx
->upd_xlt_page
;
1021 mutex_lock(&uctx
->upd_xlt_page_mutex
);
1022 memset(xlt
, 0, size
);
1024 pages_iter
= size
/ desc_size
;
1025 dma
= dma_map_single(ddev
, xlt
, size
, DMA_TO_DEVICE
);
1026 if (dma_mapping_error(ddev
, dma
)) {
1027 mlx5_ib_err(dev
, "unable to map DMA during XLT update.\n");
1033 sg
.lkey
= dev
->umrc
.pd
->local_dma_lkey
;
1035 memset(&wr
, 0, sizeof(wr
));
1036 wr
.wr
.send_flags
= MLX5_IB_SEND_UMR_UPDATE_XLT
;
1037 if (!(flags
& MLX5_IB_UPD_XLT_ENABLE
))
1038 wr
.wr
.send_flags
|= MLX5_IB_SEND_UMR_FAIL_IF_FREE
;
1039 wr
.wr
.sg_list
= &sg
;
1041 wr
.wr
.opcode
= MLX5_IB_WR_UMR
;
1043 wr
.pd
= mr
->ibmr
.pd
;
1044 wr
.mkey
= mr
->mmkey
.key
;
1045 wr
.length
= mr
->mmkey
.size
;
1046 wr
.virt_addr
= mr
->mmkey
.iova
;
1047 wr
.access_flags
= mr
->access_flags
;
1048 wr
.page_shift
= page_shift
;
1050 for (pages_mapped
= 0;
1051 pages_mapped
< pages_to_map
&& !err
;
1052 pages_mapped
+= pages_iter
, idx
+= pages_iter
) {
1053 npages
= min_t(int, pages_iter
, pages_to_map
- pages_mapped
);
1054 dma_sync_single_for_cpu(ddev
, dma
, size
, DMA_TO_DEVICE
);
1055 npages
= populate_xlt(mr
, idx
, npages
, xlt
,
1056 page_shift
, size
, flags
);
1058 dma_sync_single_for_device(ddev
, dma
, size
, DMA_TO_DEVICE
);
1060 sg
.length
= ALIGN(npages
* desc_size
,
1061 MLX5_UMR_MTT_ALIGNMENT
);
1063 if (pages_mapped
+ pages_iter
>= pages_to_map
) {
1064 if (flags
& MLX5_IB_UPD_XLT_ENABLE
)
1066 MLX5_IB_SEND_UMR_ENABLE_MR
|
1067 MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS
|
1068 MLX5_IB_SEND_UMR_UPDATE_TRANSLATION
;
1069 if (flags
& MLX5_IB_UPD_XLT_PD
||
1070 flags
& MLX5_IB_UPD_XLT_ACCESS
)
1072 MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS
;
1073 if (flags
& MLX5_IB_UPD_XLT_ADDR
)
1075 MLX5_IB_SEND_UMR_UPDATE_TRANSLATION
;
1078 wr
.offset
= idx
* desc_size
;
1079 wr
.xlt_size
= sg
.length
;
1081 err
= mlx5_ib_post_send_wait(dev
, &wr
);
1083 dma_unmap_single(ddev
, dma
, size
, DMA_TO_DEVICE
);
1087 mutex_unlock(&uctx
->upd_xlt_page_mutex
);
1089 free_pages((unsigned long)xlt
, get_order(size
));
1095 * If ibmr is NULL it will be allocated by reg_create.
1096 * Else, the given ibmr will be used.
1098 static struct mlx5_ib_mr
*reg_create(struct ib_mr
*ibmr
, struct ib_pd
*pd
,
1099 u64 virt_addr
, u64 length
,
1100 struct ib_umem
*umem
, int npages
,
1101 int page_shift
, int access_flags
,
1104 struct mlx5_ib_dev
*dev
= to_mdev(pd
->device
);
1105 struct mlx5_ib_mr
*mr
;
1111 bool pg_cap
= !!(MLX5_CAP_GEN(dev
->mdev
, pg
));
1113 mr
= ibmr
? to_mmr(ibmr
) : kzalloc(sizeof(*mr
), GFP_KERNEL
);
1115 return ERR_PTR(-ENOMEM
);
1118 mr
->access_flags
= access_flags
;
1120 inlen
= MLX5_ST_SZ_BYTES(create_mkey_in
);
1122 inlen
+= sizeof(*pas
) * roundup(npages
, 2);
1123 in
= kvzalloc(inlen
, GFP_KERNEL
);
1128 pas
= (__be64
*)MLX5_ADDR_OF(create_mkey_in
, in
, klm_pas_mtt
);
1129 if (populate
&& !(access_flags
& IB_ACCESS_ON_DEMAND
))
1130 mlx5_ib_populate_pas(dev
, umem
, page_shift
, pas
,
1131 pg_cap
? MLX5_IB_MTT_PRESENT
: 0);
1133 /* The pg_access bit allows setting the access flags
1134 * in the page list submitted with the command. */
1135 MLX5_SET(create_mkey_in
, in
, pg_access
, !!(pg_cap
));
1137 mkc
= MLX5_ADDR_OF(create_mkey_in
, in
, memory_key_mkey_entry
);
1138 MLX5_SET(mkc
, mkc
, free
, !populate
);
1139 MLX5_SET(mkc
, mkc
, access_mode
, MLX5_MKC_ACCESS_MODE_MTT
);
1140 MLX5_SET(mkc
, mkc
, a
, !!(access_flags
& IB_ACCESS_REMOTE_ATOMIC
));
1141 MLX5_SET(mkc
, mkc
, rw
, !!(access_flags
& IB_ACCESS_REMOTE_WRITE
));
1142 MLX5_SET(mkc
, mkc
, rr
, !!(access_flags
& IB_ACCESS_REMOTE_READ
));
1143 MLX5_SET(mkc
, mkc
, lw
, !!(access_flags
& IB_ACCESS_LOCAL_WRITE
));
1144 MLX5_SET(mkc
, mkc
, lr
, 1);
1145 MLX5_SET(mkc
, mkc
, umr_en
, 1);
1147 MLX5_SET64(mkc
, mkc
, start_addr
, virt_addr
);
1148 MLX5_SET64(mkc
, mkc
, len
, length
);
1149 MLX5_SET(mkc
, mkc
, pd
, to_mpd(pd
)->pdn
);
1150 MLX5_SET(mkc
, mkc
, bsf_octword_size
, 0);
1151 MLX5_SET(mkc
, mkc
, translations_octword_size
,
1152 get_octo_len(virt_addr
, length
, page_shift
));
1153 MLX5_SET(mkc
, mkc
, log_page_size
, page_shift
);
1154 MLX5_SET(mkc
, mkc
, qpn
, 0xffffff);
1156 MLX5_SET(create_mkey_in
, in
, translations_octword_actual_size
,
1157 get_octo_len(virt_addr
, length
, page_shift
));
1160 err
= mlx5_core_create_mkey(dev
->mdev
, &mr
->mmkey
, in
, inlen
);
1162 mlx5_ib_warn(dev
, "create mkey failed\n");
1165 mr
->mmkey
.type
= MLX5_MKEY_MR
;
1166 mr
->desc_size
= sizeof(struct mlx5_mtt
);
1170 mlx5_ib_dbg(dev
, "mkey = 0x%x\n", mr
->mmkey
.key
);
1181 return ERR_PTR(err
);
1184 static void set_mr_fileds(struct mlx5_ib_dev
*dev
, struct mlx5_ib_mr
*mr
,
1185 int npages
, u64 length
, int access_flags
)
1187 mr
->npages
= npages
;
1188 atomic_add(npages
, &dev
->mdev
->priv
.reg_pages
);
1189 mr
->ibmr
.lkey
= mr
->mmkey
.key
;
1190 mr
->ibmr
.rkey
= mr
->mmkey
.key
;
1191 mr
->ibmr
.length
= length
;
1192 mr
->access_flags
= access_flags
;
1195 struct ib_mr
*mlx5_ib_reg_user_mr(struct ib_pd
*pd
, u64 start
, u64 length
,
1196 u64 virt_addr
, int access_flags
,
1197 struct ib_udata
*udata
)
1199 struct mlx5_ib_dev
*dev
= to_mdev(pd
->device
);
1200 struct mlx5_ib_mr
*mr
= NULL
;
1201 struct ib_umem
*umem
;
1207 bool use_umr
= true;
1209 if (!IS_ENABLED(CONFIG_INFINIBAND_USER_MEM
))
1210 return ERR_PTR(-EINVAL
);
1212 mlx5_ib_dbg(dev
, "start 0x%llx, virt_addr 0x%llx, length 0x%llx, access_flags 0x%x\n",
1213 start
, virt_addr
, length
, access_flags
);
1215 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1216 if (!start
&& length
== U64_MAX
) {
1217 if (!(access_flags
& IB_ACCESS_ON_DEMAND
) ||
1218 !(dev
->odp_caps
.general_caps
& IB_ODP_SUPPORT_IMPLICIT
))
1219 return ERR_PTR(-EINVAL
);
1221 mr
= mlx5_ib_alloc_implicit_mr(to_mpd(pd
), access_flags
);
1226 err
= mr_umem_get(pd
, start
, length
, access_flags
, &umem
, &npages
,
1227 &page_shift
, &ncont
, &order
);
1230 return ERR_PTR(err
);
1232 if (order
<= mr_cache_max_order(dev
)) {
1233 mr
= alloc_mr_from_cache(pd
, umem
, virt_addr
, length
, ncont
,
1234 page_shift
, order
, access_flags
);
1235 if (PTR_ERR(mr
) == -EAGAIN
) {
1236 mlx5_ib_dbg(dev
, "cache empty for order %d\n", order
);
1239 } else if (!MLX5_CAP_GEN(dev
->mdev
, umr_extended_translation_offset
)) {
1240 if (access_flags
& IB_ACCESS_ON_DEMAND
) {
1242 pr_err("Got MR registration for ODP MR > 512MB, not supported for Connect-IB\n");
1249 mutex_lock(&dev
->slow_path_mutex
);
1250 mr
= reg_create(NULL
, pd
, virt_addr
, length
, umem
, ncont
,
1251 page_shift
, access_flags
, !use_umr
);
1252 mutex_unlock(&dev
->slow_path_mutex
);
1260 mlx5_ib_dbg(dev
, "mkey 0x%x\n", mr
->mmkey
.key
);
1263 set_mr_fileds(dev
, mr
, npages
, length
, access_flags
);
1265 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1270 int update_xlt_flags
= MLX5_IB_UPD_XLT_ENABLE
;
1272 if (access_flags
& IB_ACCESS_ON_DEMAND
)
1273 update_xlt_flags
|= MLX5_IB_UPD_XLT_ZAP
;
1275 err
= mlx5_ib_update_xlt(mr
, 0, ncont
, page_shift
,
1280 return ERR_PTR(err
);
1287 ib_umem_release(umem
);
1288 return ERR_PTR(err
);
1291 static int unreg_umr(struct mlx5_ib_dev
*dev
, struct mlx5_ib_mr
*mr
)
1293 struct mlx5_core_dev
*mdev
= dev
->mdev
;
1294 struct mlx5_umr_wr umrwr
= {};
1296 if (mdev
->state
== MLX5_DEVICE_STATE_INTERNAL_ERROR
)
1299 umrwr
.wr
.send_flags
= MLX5_IB_SEND_UMR_DISABLE_MR
|
1300 MLX5_IB_SEND_UMR_FAIL_IF_FREE
;
1301 umrwr
.wr
.opcode
= MLX5_IB_WR_UMR
;
1302 umrwr
.mkey
= mr
->mmkey
.key
;
1304 return mlx5_ib_post_send_wait(dev
, &umrwr
);
1307 static int rereg_umr(struct ib_pd
*pd
, struct mlx5_ib_mr
*mr
,
1308 int access_flags
, int flags
)
1310 struct mlx5_ib_dev
*dev
= to_mdev(pd
->device
);
1311 struct mlx5_umr_wr umrwr
= {};
1314 umrwr
.wr
.send_flags
= MLX5_IB_SEND_UMR_FAIL_IF_FREE
;
1316 umrwr
.wr
.opcode
= MLX5_IB_WR_UMR
;
1317 umrwr
.mkey
= mr
->mmkey
.key
;
1319 if (flags
& IB_MR_REREG_PD
|| flags
& IB_MR_REREG_ACCESS
) {
1321 umrwr
.access_flags
= access_flags
;
1322 umrwr
.wr
.send_flags
|= MLX5_IB_SEND_UMR_UPDATE_PD_ACCESS
;
1325 err
= mlx5_ib_post_send_wait(dev
, &umrwr
);
1330 int mlx5_ib_rereg_user_mr(struct ib_mr
*ib_mr
, int flags
, u64 start
,
1331 u64 length
, u64 virt_addr
, int new_access_flags
,
1332 struct ib_pd
*new_pd
, struct ib_udata
*udata
)
1334 struct mlx5_ib_dev
*dev
= to_mdev(ib_mr
->device
);
1335 struct mlx5_ib_mr
*mr
= to_mmr(ib_mr
);
1336 struct ib_pd
*pd
= (flags
& IB_MR_REREG_PD
) ? new_pd
: ib_mr
->pd
;
1337 int access_flags
= flags
& IB_MR_REREG_ACCESS
?
1340 u64 addr
= (flags
& IB_MR_REREG_TRANS
) ? virt_addr
: mr
->umem
->address
;
1341 u64 len
= (flags
& IB_MR_REREG_TRANS
) ? length
: mr
->umem
->length
;
1349 mlx5_ib_dbg(dev
, "start 0x%llx, virt_addr 0x%llx, length 0x%llx, access_flags 0x%x\n",
1350 start
, virt_addr
, length
, access_flags
);
1352 atomic_sub(mr
->npages
, &dev
->mdev
->priv
.reg_pages
);
1354 if (flags
!= IB_MR_REREG_PD
) {
1356 * Replace umem. This needs to be done whether or not UMR is
1359 flags
|= IB_MR_REREG_TRANS
;
1360 ib_umem_release(mr
->umem
);
1361 err
= mr_umem_get(pd
, addr
, len
, access_flags
, &mr
->umem
,
1362 &npages
, &page_shift
, &ncont
, &order
);
1369 if (flags
& IB_MR_REREG_TRANS
&& !use_umr_mtt_update(mr
, addr
, len
)) {
1371 * UMR can't be used - MKey needs to be replaced.
1373 if (mr
->allocated_from_cache
) {
1374 err
= unreg_umr(dev
, mr
);
1376 mlx5_ib_warn(dev
, "Failed to unregister MR\n");
1378 err
= destroy_mkey(dev
, mr
);
1380 mlx5_ib_warn(dev
, "Failed to destroy MKey\n");
1385 mr
= reg_create(ib_mr
, pd
, addr
, len
, mr
->umem
, ncont
,
1386 page_shift
, access_flags
, true);
1391 mr
->allocated_from_cache
= 0;
1398 mr
->access_flags
= access_flags
;
1399 mr
->mmkey
.iova
= addr
;
1400 mr
->mmkey
.size
= len
;
1401 mr
->mmkey
.pd
= to_mpd(pd
)->pdn
;
1403 if (flags
& IB_MR_REREG_TRANS
) {
1404 upd_flags
= MLX5_IB_UPD_XLT_ADDR
;
1405 if (flags
& IB_MR_REREG_PD
)
1406 upd_flags
|= MLX5_IB_UPD_XLT_PD
;
1407 if (flags
& IB_MR_REREG_ACCESS
)
1408 upd_flags
|= MLX5_IB_UPD_XLT_ACCESS
;
1409 err
= mlx5_ib_update_xlt(mr
, 0, npages
, page_shift
,
1412 err
= rereg_umr(pd
, mr
, access_flags
, flags
);
1416 mlx5_ib_warn(dev
, "Failed to rereg UMR\n");
1417 ib_umem_release(mr
->umem
);
1423 set_mr_fileds(dev
, mr
, npages
, len
, access_flags
);
1425 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1432 mlx5_alloc_priv_descs(struct ib_device
*device
,
1433 struct mlx5_ib_mr
*mr
,
1437 int size
= ndescs
* desc_size
;
1441 add_size
= max_t(int, MLX5_UMR_ALIGN
- ARCH_KMALLOC_MINALIGN
, 0);
1443 mr
->descs_alloc
= kzalloc(size
+ add_size
, GFP_KERNEL
);
1444 if (!mr
->descs_alloc
)
1447 mr
->descs
= PTR_ALIGN(mr
->descs_alloc
, MLX5_UMR_ALIGN
);
1449 mr
->desc_map
= dma_map_single(device
->dev
.parent
, mr
->descs
,
1450 size
, DMA_TO_DEVICE
);
1451 if (dma_mapping_error(device
->dev
.parent
, mr
->desc_map
)) {
1458 kfree(mr
->descs_alloc
);
1464 mlx5_free_priv_descs(struct mlx5_ib_mr
*mr
)
1467 struct ib_device
*device
= mr
->ibmr
.device
;
1468 int size
= mr
->max_descs
* mr
->desc_size
;
1470 dma_unmap_single(device
->dev
.parent
, mr
->desc_map
,
1471 size
, DMA_TO_DEVICE
);
1472 kfree(mr
->descs_alloc
);
1477 static int clean_mr(struct mlx5_ib_dev
*dev
, struct mlx5_ib_mr
*mr
)
1479 int allocated_from_cache
= mr
->allocated_from_cache
;
1483 if (mlx5_core_destroy_psv(dev
->mdev
,
1484 mr
->sig
->psv_memory
.psv_idx
))
1485 mlx5_ib_warn(dev
, "failed to destroy mem psv %d\n",
1486 mr
->sig
->psv_memory
.psv_idx
);
1487 if (mlx5_core_destroy_psv(dev
->mdev
,
1488 mr
->sig
->psv_wire
.psv_idx
))
1489 mlx5_ib_warn(dev
, "failed to destroy wire psv %d\n",
1490 mr
->sig
->psv_wire
.psv_idx
);
1495 mlx5_free_priv_descs(mr
);
1497 if (!allocated_from_cache
) {
1498 u32 key
= mr
->mmkey
.key
;
1500 err
= destroy_mkey(dev
, mr
);
1503 mlx5_ib_warn(dev
, "failed to destroy mkey 0x%x (%d)\n",
1508 mlx5_mr_cache_free(dev
, mr
);
1514 static int dereg_mr(struct mlx5_ib_dev
*dev
, struct mlx5_ib_mr
*mr
)
1516 int npages
= mr
->npages
;
1517 struct ib_umem
*umem
= mr
->umem
;
1519 #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
1520 if (umem
&& umem
->odp_data
) {
1521 /* Prevent new page faults from succeeding */
1523 /* Wait for all running page-fault handlers to finish. */
1524 synchronize_srcu(&dev
->mr_srcu
);
1525 /* Destroy all page mappings */
1526 if (umem
->odp_data
->page_list
)
1527 mlx5_ib_invalidate_range(umem
, ib_umem_start(umem
),
1530 mlx5_ib_free_implicit_mr(mr
);
1532 * We kill the umem before the MR for ODP,
1533 * so that there will not be any invalidations in
1534 * flight, looking at the *mr struct.
1536 ib_umem_release(umem
);
1537 atomic_sub(npages
, &dev
->mdev
->priv
.reg_pages
);
1539 /* Avoid double-freeing the umem. */
1547 ib_umem_release(umem
);
1548 atomic_sub(npages
, &dev
->mdev
->priv
.reg_pages
);
1554 int mlx5_ib_dereg_mr(struct ib_mr
*ibmr
)
1556 struct mlx5_ib_dev
*dev
= to_mdev(ibmr
->device
);
1557 struct mlx5_ib_mr
*mr
= to_mmr(ibmr
);
1559 return dereg_mr(dev
, mr
);
1562 struct ib_mr
*mlx5_ib_alloc_mr(struct ib_pd
*pd
,
1563 enum ib_mr_type mr_type
,
1566 struct mlx5_ib_dev
*dev
= to_mdev(pd
->device
);
1567 int inlen
= MLX5_ST_SZ_BYTES(create_mkey_in
);
1568 int ndescs
= ALIGN(max_num_sg
, 4);
1569 struct mlx5_ib_mr
*mr
;
1574 mr
= kzalloc(sizeof(*mr
), GFP_KERNEL
);
1576 return ERR_PTR(-ENOMEM
);
1578 in
= kzalloc(inlen
, GFP_KERNEL
);
1584 mkc
= MLX5_ADDR_OF(create_mkey_in
, in
, memory_key_mkey_entry
);
1585 MLX5_SET(mkc
, mkc
, free
, 1);
1586 MLX5_SET(mkc
, mkc
, translations_octword_size
, ndescs
);
1587 MLX5_SET(mkc
, mkc
, qpn
, 0xffffff);
1588 MLX5_SET(mkc
, mkc
, pd
, to_mpd(pd
)->pdn
);
1590 if (mr_type
== IB_MR_TYPE_MEM_REG
) {
1591 mr
->access_mode
= MLX5_MKC_ACCESS_MODE_MTT
;
1592 MLX5_SET(mkc
, mkc
, log_page_size
, PAGE_SHIFT
);
1593 err
= mlx5_alloc_priv_descs(pd
->device
, mr
,
1594 ndescs
, sizeof(struct mlx5_mtt
));
1598 mr
->desc_size
= sizeof(struct mlx5_mtt
);
1599 mr
->max_descs
= ndescs
;
1600 } else if (mr_type
== IB_MR_TYPE_SG_GAPS
) {
1601 mr
->access_mode
= MLX5_MKC_ACCESS_MODE_KLMS
;
1603 err
= mlx5_alloc_priv_descs(pd
->device
, mr
,
1604 ndescs
, sizeof(struct mlx5_klm
));
1607 mr
->desc_size
= sizeof(struct mlx5_klm
);
1608 mr
->max_descs
= ndescs
;
1609 } else if (mr_type
== IB_MR_TYPE_SIGNATURE
) {
1612 MLX5_SET(mkc
, mkc
, bsf_en
, 1);
1613 MLX5_SET(mkc
, mkc
, bsf_octword_size
, MLX5_MKEY_BSF_OCTO_SIZE
);
1614 mr
->sig
= kzalloc(sizeof(*mr
->sig
), GFP_KERNEL
);
1620 /* create mem & wire PSVs */
1621 err
= mlx5_core_create_psv(dev
->mdev
, to_mpd(pd
)->pdn
,
1626 mr
->access_mode
= MLX5_MKC_ACCESS_MODE_KLMS
;
1627 mr
->sig
->psv_memory
.psv_idx
= psv_index
[0];
1628 mr
->sig
->psv_wire
.psv_idx
= psv_index
[1];
1630 mr
->sig
->sig_status_checked
= true;
1631 mr
->sig
->sig_err_exists
= false;
1632 /* Next UMR, Arm SIGERR */
1633 ++mr
->sig
->sigerr_count
;
1635 mlx5_ib_warn(dev
, "Invalid mr type %d\n", mr_type
);
1640 MLX5_SET(mkc
, mkc
, access_mode
, mr
->access_mode
);
1641 MLX5_SET(mkc
, mkc
, umr_en
, 1);
1643 mr
->ibmr
.device
= pd
->device
;
1644 err
= mlx5_core_create_mkey(dev
->mdev
, &mr
->mmkey
, in
, inlen
);
1646 goto err_destroy_psv
;
1648 mr
->mmkey
.type
= MLX5_MKEY_MR
;
1649 mr
->ibmr
.lkey
= mr
->mmkey
.key
;
1650 mr
->ibmr
.rkey
= mr
->mmkey
.key
;
1658 if (mlx5_core_destroy_psv(dev
->mdev
,
1659 mr
->sig
->psv_memory
.psv_idx
))
1660 mlx5_ib_warn(dev
, "failed to destroy mem psv %d\n",
1661 mr
->sig
->psv_memory
.psv_idx
);
1662 if (mlx5_core_destroy_psv(dev
->mdev
,
1663 mr
->sig
->psv_wire
.psv_idx
))
1664 mlx5_ib_warn(dev
, "failed to destroy wire psv %d\n",
1665 mr
->sig
->psv_wire
.psv_idx
);
1667 mlx5_free_priv_descs(mr
);
1674 return ERR_PTR(err
);
1677 struct ib_mw
*mlx5_ib_alloc_mw(struct ib_pd
*pd
, enum ib_mw_type type
,
1678 struct ib_udata
*udata
)
1680 struct mlx5_ib_dev
*dev
= to_mdev(pd
->device
);
1681 int inlen
= MLX5_ST_SZ_BYTES(create_mkey_in
);
1682 struct mlx5_ib_mw
*mw
= NULL
;
1687 struct mlx5_ib_alloc_mw req
= {};
1690 __u32 response_length
;
1693 err
= ib_copy_from_udata(&req
, udata
, min(udata
->inlen
, sizeof(req
)));
1695 return ERR_PTR(err
);
1697 if (req
.comp_mask
|| req
.reserved1
|| req
.reserved2
)
1698 return ERR_PTR(-EOPNOTSUPP
);
1700 if (udata
->inlen
> sizeof(req
) &&
1701 !ib_is_udata_cleared(udata
, sizeof(req
),
1702 udata
->inlen
- sizeof(req
)))
1703 return ERR_PTR(-EOPNOTSUPP
);
1705 ndescs
= req
.num_klms
? roundup(req
.num_klms
, 4) : roundup(1, 4);
1707 mw
= kzalloc(sizeof(*mw
), GFP_KERNEL
);
1708 in
= kzalloc(inlen
, GFP_KERNEL
);
1714 mkc
= MLX5_ADDR_OF(create_mkey_in
, in
, memory_key_mkey_entry
);
1716 MLX5_SET(mkc
, mkc
, free
, 1);
1717 MLX5_SET(mkc
, mkc
, translations_octword_size
, ndescs
);
1718 MLX5_SET(mkc
, mkc
, pd
, to_mpd(pd
)->pdn
);
1719 MLX5_SET(mkc
, mkc
, umr_en
, 1);
1720 MLX5_SET(mkc
, mkc
, lr
, 1);
1721 MLX5_SET(mkc
, mkc
, access_mode
, MLX5_MKC_ACCESS_MODE_KLMS
);
1722 MLX5_SET(mkc
, mkc
, en_rinval
, !!((type
== IB_MW_TYPE_2
)));
1723 MLX5_SET(mkc
, mkc
, qpn
, 0xffffff);
1725 err
= mlx5_core_create_mkey(dev
->mdev
, &mw
->mmkey
, in
, inlen
);
1729 mw
->mmkey
.type
= MLX5_MKEY_MW
;
1730 mw
->ibmw
.rkey
= mw
->mmkey
.key
;
1731 mw
->ndescs
= ndescs
;
1733 resp
.response_length
= min(offsetof(typeof(resp
), response_length
) +
1734 sizeof(resp
.response_length
), udata
->outlen
);
1735 if (resp
.response_length
) {
1736 err
= ib_copy_to_udata(udata
, &resp
, resp
.response_length
);
1738 mlx5_core_destroy_mkey(dev
->mdev
, &mw
->mmkey
);
1749 return ERR_PTR(err
);
1752 int mlx5_ib_dealloc_mw(struct ib_mw
*mw
)
1754 struct mlx5_ib_mw
*mmw
= to_mmw(mw
);
1757 err
= mlx5_core_destroy_mkey((to_mdev(mw
->device
))->mdev
,
1764 int mlx5_ib_check_mr_status(struct ib_mr
*ibmr
, u32 check_mask
,
1765 struct ib_mr_status
*mr_status
)
1767 struct mlx5_ib_mr
*mmr
= to_mmr(ibmr
);
1770 if (check_mask
& ~IB_MR_CHECK_SIG_STATUS
) {
1771 pr_err("Invalid status check mask\n");
1776 mr_status
->fail_status
= 0;
1777 if (check_mask
& IB_MR_CHECK_SIG_STATUS
) {
1780 pr_err("signature status check requested on a non-signature enabled MR\n");
1784 mmr
->sig
->sig_status_checked
= true;
1785 if (!mmr
->sig
->sig_err_exists
)
1788 if (ibmr
->lkey
== mmr
->sig
->err_item
.key
)
1789 memcpy(&mr_status
->sig_err
, &mmr
->sig
->err_item
,
1790 sizeof(mr_status
->sig_err
));
1792 mr_status
->sig_err
.err_type
= IB_SIG_BAD_GUARD
;
1793 mr_status
->sig_err
.sig_err_offset
= 0;
1794 mr_status
->sig_err
.key
= mmr
->sig
->err_item
.key
;
1797 mmr
->sig
->sig_err_exists
= false;
1798 mr_status
->fail_status
|= IB_MR_CHECK_SIG_STATUS
;
1806 mlx5_ib_sg_to_klms(struct mlx5_ib_mr
*mr
,
1807 struct scatterlist
*sgl
,
1808 unsigned short sg_nents
,
1809 unsigned int *sg_offset_p
)
1811 struct scatterlist
*sg
= sgl
;
1812 struct mlx5_klm
*klms
= mr
->descs
;
1813 unsigned int sg_offset
= sg_offset_p
? *sg_offset_p
: 0;
1814 u32 lkey
= mr
->ibmr
.pd
->local_dma_lkey
;
1817 mr
->ibmr
.iova
= sg_dma_address(sg
) + sg_offset
;
1818 mr
->ibmr
.length
= 0;
1819 mr
->ndescs
= sg_nents
;
1821 for_each_sg(sgl
, sg
, sg_nents
, i
) {
1822 if (unlikely(i
>= mr
->max_descs
))
1824 klms
[i
].va
= cpu_to_be64(sg_dma_address(sg
) + sg_offset
);
1825 klms
[i
].bcount
= cpu_to_be32(sg_dma_len(sg
) - sg_offset
);
1826 klms
[i
].key
= cpu_to_be32(lkey
);
1827 mr
->ibmr
.length
+= sg_dma_len(sg
) - sg_offset
;
1833 *sg_offset_p
= sg_offset
;
1838 static int mlx5_set_page(struct ib_mr
*ibmr
, u64 addr
)
1840 struct mlx5_ib_mr
*mr
= to_mmr(ibmr
);
1843 if (unlikely(mr
->ndescs
== mr
->max_descs
))
1847 descs
[mr
->ndescs
++] = cpu_to_be64(addr
| MLX5_EN_RD
| MLX5_EN_WR
);
1852 int mlx5_ib_map_mr_sg(struct ib_mr
*ibmr
, struct scatterlist
*sg
, int sg_nents
,
1853 unsigned int *sg_offset
)
1855 struct mlx5_ib_mr
*mr
= to_mmr(ibmr
);
1860 ib_dma_sync_single_for_cpu(ibmr
->device
, mr
->desc_map
,
1861 mr
->desc_size
* mr
->max_descs
,
1864 if (mr
->access_mode
== MLX5_MKC_ACCESS_MODE_KLMS
)
1865 n
= mlx5_ib_sg_to_klms(mr
, sg
, sg_nents
, sg_offset
);
1867 n
= ib_sg_to_pages(ibmr
, sg
, sg_nents
, sg_offset
,
1870 ib_dma_sync_single_for_device(ibmr
->device
, mr
->desc_map
,
1871 mr
->desc_size
* mr
->max_descs
,