xref: /linux/drivers/infiniband/hw/mlx4/doorbell.c (revision 762f99f4f3cb41a775b5157dd761217beba65873)
1225c7b1fSRoland Dreier /*
2225c7b1fSRoland Dreier  * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
3225c7b1fSRoland Dreier  *
4225c7b1fSRoland Dreier  * This software is available to you under a choice of one of two
5225c7b1fSRoland Dreier  * licenses.  You may choose to be licensed under the terms of the GNU
6225c7b1fSRoland Dreier  * General Public License (GPL) Version 2, available from the file
7225c7b1fSRoland Dreier  * COPYING in the main directory of this source tree, or the
8225c7b1fSRoland Dreier  * OpenIB.org BSD license below:
9225c7b1fSRoland Dreier  *
10225c7b1fSRoland Dreier  *     Redistribution and use in source and binary forms, with or
11225c7b1fSRoland Dreier  *     without modification, are permitted provided that the following
12225c7b1fSRoland Dreier  *     conditions are met:
13225c7b1fSRoland Dreier  *
14225c7b1fSRoland Dreier  *      - Redistributions of source code must retain the above
15225c7b1fSRoland Dreier  *        copyright notice, this list of conditions and the following
16225c7b1fSRoland Dreier  *        disclaimer.
17225c7b1fSRoland Dreier  *
18225c7b1fSRoland Dreier  *      - Redistributions in binary form must reproduce the above
19225c7b1fSRoland Dreier  *        copyright notice, this list of conditions and the following
20225c7b1fSRoland Dreier  *        disclaimer in the documentation and/or other materials
21225c7b1fSRoland Dreier  *        provided with the distribution.
22225c7b1fSRoland Dreier  *
23225c7b1fSRoland Dreier  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24225c7b1fSRoland Dreier  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25225c7b1fSRoland Dreier  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26225c7b1fSRoland Dreier  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27225c7b1fSRoland Dreier  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28225c7b1fSRoland Dreier  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29225c7b1fSRoland Dreier  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30225c7b1fSRoland Dreier  * SOFTWARE.
31225c7b1fSRoland Dreier  */
32225c7b1fSRoland Dreier 
33225c7b1fSRoland Dreier #include <linux/slab.h>
34ff23dfa1SShamir Rabinovitch #include <rdma/uverbs_ioctl.h>
35225c7b1fSRoland Dreier 
36225c7b1fSRoland Dreier #include "mlx4_ib.h"
37225c7b1fSRoland Dreier 
38225c7b1fSRoland Dreier struct mlx4_ib_user_db_page {
39225c7b1fSRoland Dreier 	struct list_head	list;
40225c7b1fSRoland Dreier 	struct ib_umem	       *umem;
41225c7b1fSRoland Dreier 	unsigned long		user_virt;
42225c7b1fSRoland Dreier 	int			refcnt;
43225c7b1fSRoland Dreier };
44225c7b1fSRoland Dreier 
mlx4_ib_db_map_user(struct ib_udata * udata,unsigned long virt,struct mlx4_db * db)45ff23dfa1SShamir Rabinovitch int mlx4_ib_db_map_user(struct ib_udata *udata, unsigned long virt,
466296883cSYevgeny Petrilin 			struct mlx4_db *db)
47225c7b1fSRoland Dreier {
48225c7b1fSRoland Dreier 	struct mlx4_ib_user_db_page *page;
49225c7b1fSRoland Dreier 	int err = 0;
50ff23dfa1SShamir Rabinovitch 	struct mlx4_ib_ucontext *context = rdma_udata_to_drv_context(
51ff23dfa1SShamir Rabinovitch 		udata, struct mlx4_ib_ucontext, ibucontext);
52225c7b1fSRoland Dreier 
53225c7b1fSRoland Dreier 	mutex_lock(&context->db_page_mutex);
54225c7b1fSRoland Dreier 
55225c7b1fSRoland Dreier 	list_for_each_entry(page, &context->db_page_list, list)
56225c7b1fSRoland Dreier 		if (page->user_virt == (virt & PAGE_MASK))
57225c7b1fSRoland Dreier 			goto found;
58225c7b1fSRoland Dreier 
59225c7b1fSRoland Dreier 	page = kmalloc(sizeof *page, GFP_KERNEL);
60225c7b1fSRoland Dreier 	if (!page) {
61225c7b1fSRoland Dreier 		err = -ENOMEM;
62225c7b1fSRoland Dreier 		goto out;
63225c7b1fSRoland Dreier 	}
64225c7b1fSRoland Dreier 
65225c7b1fSRoland Dreier 	page->user_virt = (virt & PAGE_MASK);
66225c7b1fSRoland Dreier 	page->refcnt    = 0;
67c320e527SMoni Shoua 	page->umem = ib_umem_get(context->ibucontext.device, virt & PAGE_MASK,
68c320e527SMoni Shoua 				 PAGE_SIZE, 0);
69225c7b1fSRoland Dreier 	if (IS_ERR(page->umem)) {
70225c7b1fSRoland Dreier 		err = PTR_ERR(page->umem);
71225c7b1fSRoland Dreier 		kfree(page);
72225c7b1fSRoland Dreier 		goto out;
73225c7b1fSRoland Dreier 	}
74225c7b1fSRoland Dreier 
75225c7b1fSRoland Dreier 	list_add(&page->list, &context->db_page_list);
76225c7b1fSRoland Dreier 
77225c7b1fSRoland Dreier found:
78*79fbd3e1SMaor Gottlieb 	db->dma = sg_dma_address(page->umem->sgt_append.sgt.sgl) +
79*79fbd3e1SMaor Gottlieb 		  (virt & ~PAGE_MASK);
80225c7b1fSRoland Dreier 	db->u.user_page = page;
81225c7b1fSRoland Dreier 	++page->refcnt;
82225c7b1fSRoland Dreier 
83225c7b1fSRoland Dreier out:
84225c7b1fSRoland Dreier 	mutex_unlock(&context->db_page_mutex);
85225c7b1fSRoland Dreier 
86225c7b1fSRoland Dreier 	return err;
87225c7b1fSRoland Dreier }
88225c7b1fSRoland Dreier 
mlx4_ib_db_unmap_user(struct mlx4_ib_ucontext * context,struct mlx4_db * db)896296883cSYevgeny Petrilin void mlx4_ib_db_unmap_user(struct mlx4_ib_ucontext *context, struct mlx4_db *db)
90225c7b1fSRoland Dreier {
91225c7b1fSRoland Dreier 	mutex_lock(&context->db_page_mutex);
92225c7b1fSRoland Dreier 
93225c7b1fSRoland Dreier 	if (!--db->u.user_page->refcnt) {
94225c7b1fSRoland Dreier 		list_del(&db->u.user_page->list);
95225c7b1fSRoland Dreier 		ib_umem_release(db->u.user_page->umem);
96225c7b1fSRoland Dreier 		kfree(db->u.user_page);
97225c7b1fSRoland Dreier 	}
98225c7b1fSRoland Dreier 
99225c7b1fSRoland Dreier 	mutex_unlock(&context->db_page_mutex);
100225c7b1fSRoland Dreier }
101