1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/kref.h>
34#include <linux/slab.h>
35#include <rdma/ib_umem.h>
36
37#include "mlx5_ib.h"
38
39struct mlx5_ib_user_db_page {
40 struct list_head list;
41 struct ib_umem *umem;
42 unsigned long user_virt;
43 int refcnt;
44};
45
46int mlx5_ib_db_map_user(struct mlx5_ib_ucontext *context,
47 struct ib_udata *udata, unsigned long virt,
48 struct mlx5_db *db)
49{
50 struct mlx5_ib_user_db_page *page;
51 int err = 0;
52
53 mutex_lock(&context->db_page_mutex);
54
55 list_for_each_entry(page, &context->db_page_list, list)
56 if (page->user_virt == (virt & PAGE_MASK))
57 goto found;
58
59 page = kmalloc(sizeof(*page), GFP_KERNEL);
60 if (!page) {
61 err = -ENOMEM;
62 goto out;
63 }
64
65 page->user_virt = (virt & PAGE_MASK);
66 page->refcnt = 0;
67 page->umem = ib_umem_get(context->ibucontext.device, virt & PAGE_MASK,
68 PAGE_SIZE, 0);
69 if (IS_ERR(page->umem)) {
70 err = PTR_ERR(page->umem);
71 kfree(page);
72 goto out;
73 }
74
75 list_add(&page->list, &context->db_page_list);
76
77found:
78 db->dma = sg_dma_address(page->umem->sg_head.sgl) + (virt & ~PAGE_MASK);
79 db->u.user_page = page;
80 ++page->refcnt;
81
82out:
83 mutex_unlock(&context->db_page_mutex);
84
85 return err;
86}
87
88void mlx5_ib_db_unmap_user(struct mlx5_ib_ucontext *context, struct mlx5_db *db)
89{
90 mutex_lock(&context->db_page_mutex);
91
92 if (!--db->u.user_page->refcnt) {
93 list_del(&db->u.user_page->list);
94 ib_umem_release(db->u.user_page->umem);
95 kfree(db->u.user_page);
96 }
97
98 mutex_unlock(&context->db_page_mutex);
99}
100