1e126ba97SEli Cohen /*
26cf0a15fSSaeed Mahameed  * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3e126ba97SEli Cohen  *
4e126ba97SEli Cohen  * This software is available to you under a choice of one of two
5e126ba97SEli Cohen  * licenses.  You may choose to be licensed under the terms of the GNU
6e126ba97SEli Cohen  * General Public License (GPL) Version 2, available from the file
7e126ba97SEli Cohen  * COPYING in the main directory of this source tree, or the
8e126ba97SEli Cohen  * OpenIB.org BSD license below:
9e126ba97SEli Cohen  *
10e126ba97SEli Cohen  *     Redistribution and use in source and binary forms, with or
11e126ba97SEli Cohen  *     without modification, are permitted provided that the following
12e126ba97SEli Cohen  *     conditions are met:
13e126ba97SEli Cohen  *
14e126ba97SEli Cohen  *      - Redistributions of source code must retain the above
15e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
16e126ba97SEli Cohen  *        disclaimer.
17e126ba97SEli Cohen  *
18e126ba97SEli Cohen  *      - Redistributions in binary form must reproduce the above
19e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
20e126ba97SEli Cohen  *        disclaimer in the documentation and/or other materials
21e126ba97SEli Cohen  *        provided with the distribution.
22e126ba97SEli Cohen  *
23e126ba97SEli Cohen  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24e126ba97SEli Cohen  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25e126ba97SEli Cohen  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26e126ba97SEli Cohen  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27e126ba97SEli Cohen  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28e126ba97SEli Cohen  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29e126ba97SEli Cohen  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30e126ba97SEli Cohen  * SOFTWARE.
31e126ba97SEli Cohen  */
32e126ba97SEli Cohen 
33e126ba97SEli Cohen #include <linux/kref.h>
34e126ba97SEli Cohen #include <linux/slab.h>
35*b6459415SJakub Kicinski #include <linux/sched/mm.h>
36e126ba97SEli Cohen #include <rdma/ib_umem.h>
37e126ba97SEli Cohen 
38e126ba97SEli Cohen #include "mlx5_ib.h"
39e126ba97SEli Cohen 
40e126ba97SEli Cohen struct mlx5_ib_user_db_page {
41e126ba97SEli Cohen 	struct list_head	list;
42e126ba97SEli Cohen 	struct ib_umem	       *umem;
43e126ba97SEli Cohen 	unsigned long		user_virt;
44e126ba97SEli Cohen 	int			refcnt;
45a0ffb4c1SMark Zhang 	struct mm_struct	*mm;
46e126ba97SEli Cohen };
47e126ba97SEli Cohen 
mlx5_ib_db_map_user(struct mlx5_ib_ucontext * context,unsigned long virt,struct mlx5_db * db)480bedd3d0SLang Cheng int mlx5_ib_db_map_user(struct mlx5_ib_ucontext *context, unsigned long virt,
49e126ba97SEli Cohen 			struct mlx5_db *db)
50e126ba97SEli Cohen {
51e126ba97SEli Cohen 	struct mlx5_ib_user_db_page *page;
52e126ba97SEli Cohen 	int err = 0;
53e126ba97SEli Cohen 
54e126ba97SEli Cohen 	mutex_lock(&context->db_page_mutex);
55e126ba97SEli Cohen 
56e126ba97SEli Cohen 	list_for_each_entry(page, &context->db_page_list, list)
57a0ffb4c1SMark Zhang 		if ((current->mm == page->mm) &&
58a0ffb4c1SMark Zhang 		    (page->user_virt == (virt & PAGE_MASK)))
59e126ba97SEli Cohen 			goto found;
60e126ba97SEli Cohen 
61e126ba97SEli Cohen 	page = kmalloc(sizeof(*page), GFP_KERNEL);
62e126ba97SEli Cohen 	if (!page) {
63e126ba97SEli Cohen 		err = -ENOMEM;
64e126ba97SEli Cohen 		goto out;
65e126ba97SEli Cohen 	}
66e126ba97SEli Cohen 
67e126ba97SEli Cohen 	page->user_virt = (virt & PAGE_MASK);
68e126ba97SEli Cohen 	page->refcnt    = 0;
69c320e527SMoni Shoua 	page->umem = ib_umem_get(context->ibucontext.device, virt & PAGE_MASK,
70c320e527SMoni Shoua 				 PAGE_SIZE, 0);
71e126ba97SEli Cohen 	if (IS_ERR(page->umem)) {
72e126ba97SEli Cohen 		err = PTR_ERR(page->umem);
73e126ba97SEli Cohen 		kfree(page);
74e126ba97SEli Cohen 		goto out;
75e126ba97SEli Cohen 	}
76a0ffb4c1SMark Zhang 	mmgrab(current->mm);
77a0ffb4c1SMark Zhang 	page->mm = current->mm;
78e126ba97SEli Cohen 
79e126ba97SEli Cohen 	list_add(&page->list, &context->db_page_list);
80e126ba97SEli Cohen 
81e126ba97SEli Cohen found:
8279fbd3e1SMaor Gottlieb 	db->dma = sg_dma_address(page->umem->sgt_append.sgt.sgl) +
8379fbd3e1SMaor Gottlieb 		  (virt & ~PAGE_MASK);
84e126ba97SEli Cohen 	db->u.user_page = page;
85e126ba97SEli Cohen 	++page->refcnt;
86e126ba97SEli Cohen 
87e126ba97SEli Cohen out:
88e126ba97SEli Cohen 	mutex_unlock(&context->db_page_mutex);
89e126ba97SEli Cohen 
90e126ba97SEli Cohen 	return err;
91e126ba97SEli Cohen }
92e126ba97SEli Cohen 
mlx5_ib_db_unmap_user(struct mlx5_ib_ucontext * context,struct mlx5_db * db)93e126ba97SEli Cohen void mlx5_ib_db_unmap_user(struct mlx5_ib_ucontext *context, struct mlx5_db *db)
94e126ba97SEli Cohen {
95e126ba97SEli Cohen 	mutex_lock(&context->db_page_mutex);
96e126ba97SEli Cohen 
97e126ba97SEli Cohen 	if (!--db->u.user_page->refcnt) {
98e126ba97SEli Cohen 		list_del(&db->u.user_page->list);
99a0ffb4c1SMark Zhang 		mmdrop(db->u.user_page->mm);
100e126ba97SEli Cohen 		ib_umem_release(db->u.user_page->umem);
101e126ba97SEli Cohen 		kfree(db->u.user_page);
102e126ba97SEli Cohen 	}
103e126ba97SEli Cohen 
104e126ba97SEli Cohen 	mutex_unlock(&context->db_page_mutex);
105e126ba97SEli Cohen }
106