• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2015 Intel Corporation.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * BSD LICENSE
20  *
21  * Copyright(c) 2015 Intel Corporation.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  *
27  *  - Redistributions of source code must retain the above copyright
28  *    notice, this list of conditions and the following disclaimer.
29  *  - Redistributions in binary form must reproduce the above copyright
30  *    notice, this list of conditions and the following disclaimer in
31  *    the documentation and/or other materials provided with the
32  *    distribution.
33  *  - Neither the name of Intel Corporation nor the names of its
34  *    contributors may be used to endorse or promote products derived
35  *    from this software without specific prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  *
49  */
50 
51 #include <linux/module.h>
52 #include <linux/slab.h>
53 #include <linux/vmalloc.h>
54 #include <linux/mm.h>
55 #include <linux/errno.h>
56 #include <asm/pgtable.h>
57 
58 #include "verbs.h"
59 
60 /**
61  * hfi1_release_mmap_info - free mmap info structure
62  * @ref: a pointer to the kref within struct hfi1_mmap_info
63  */
hfi1_release_mmap_info(struct kref * ref)64 void hfi1_release_mmap_info(struct kref *ref)
65 {
66 	struct hfi1_mmap_info *ip =
67 		container_of(ref, struct hfi1_mmap_info, ref);
68 	struct hfi1_ibdev *dev = to_idev(ip->context->device);
69 
70 	spin_lock_irq(&dev->pending_lock);
71 	list_del(&ip->pending_mmaps);
72 	spin_unlock_irq(&dev->pending_lock);
73 
74 	vfree(ip->obj);
75 	kfree(ip);
76 }
77 
78 /*
79  * open and close keep track of how many times the CQ is mapped,
80  * to avoid releasing it.
81  */
hfi1_vma_open(struct vm_area_struct * vma)82 static void hfi1_vma_open(struct vm_area_struct *vma)
83 {
84 	struct hfi1_mmap_info *ip = vma->vm_private_data;
85 
86 	kref_get(&ip->ref);
87 }
88 
hfi1_vma_close(struct vm_area_struct * vma)89 static void hfi1_vma_close(struct vm_area_struct *vma)
90 {
91 	struct hfi1_mmap_info *ip = vma->vm_private_data;
92 
93 	kref_put(&ip->ref, hfi1_release_mmap_info);
94 }
95 
96 static struct vm_operations_struct hfi1_vm_ops = {
97 	.open =     hfi1_vma_open,
98 	.close =    hfi1_vma_close,
99 };
100 
101 /**
102  * hfi1_mmap - create a new mmap region
103  * @context: the IB user context of the process making the mmap() call
104  * @vma: the VMA to be initialized
105  * Return zero if the mmap is OK. Otherwise, return an errno.
106  */
hfi1_mmap(struct ib_ucontext * context,struct vm_area_struct * vma)107 int hfi1_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
108 {
109 	struct hfi1_ibdev *dev = to_idev(context->device);
110 	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
111 	unsigned long size = vma->vm_end - vma->vm_start;
112 	struct hfi1_mmap_info *ip, *pp;
113 	int ret = -EINVAL;
114 
115 	/*
116 	 * Search the device's list of objects waiting for a mmap call.
117 	 * Normally, this list is very short since a call to create a
118 	 * CQ, QP, or SRQ is soon followed by a call to mmap().
119 	 */
120 	spin_lock_irq(&dev->pending_lock);
121 	list_for_each_entry_safe(ip, pp, &dev->pending_mmaps,
122 				 pending_mmaps) {
123 		/* Only the creator is allowed to mmap the object */
124 		if (context != ip->context || (__u64) offset != ip->offset)
125 			continue;
126 		/* Don't allow a mmap larger than the object. */
127 		if (size > ip->size)
128 			break;
129 
130 		list_del_init(&ip->pending_mmaps);
131 		spin_unlock_irq(&dev->pending_lock);
132 
133 		ret = remap_vmalloc_range(vma, ip->obj, 0);
134 		if (ret)
135 			goto done;
136 		vma->vm_ops = &hfi1_vm_ops;
137 		vma->vm_private_data = ip;
138 		hfi1_vma_open(vma);
139 		goto done;
140 	}
141 	spin_unlock_irq(&dev->pending_lock);
142 done:
143 	return ret;
144 }
145 
146 /*
147  * Allocate information for hfi1_mmap
148  */
hfi1_create_mmap_info(struct hfi1_ibdev * dev,u32 size,struct ib_ucontext * context,void * obj)149 struct hfi1_mmap_info *hfi1_create_mmap_info(struct hfi1_ibdev *dev,
150 					     u32 size,
151 					     struct ib_ucontext *context,
152 					     void *obj) {
153 	struct hfi1_mmap_info *ip;
154 
155 	ip = kmalloc(sizeof(*ip), GFP_KERNEL);
156 	if (!ip)
157 		goto bail;
158 
159 	size = PAGE_ALIGN(size);
160 
161 	spin_lock_irq(&dev->mmap_offset_lock);
162 	if (dev->mmap_offset == 0)
163 		dev->mmap_offset = PAGE_SIZE;
164 	ip->offset = dev->mmap_offset;
165 	dev->mmap_offset += size;
166 	spin_unlock_irq(&dev->mmap_offset_lock);
167 
168 	INIT_LIST_HEAD(&ip->pending_mmaps);
169 	ip->size = size;
170 	ip->context = context;
171 	ip->obj = obj;
172 	kref_init(&ip->ref);
173 
174 bail:
175 	return ip;
176 }
177 
hfi1_update_mmap_info(struct hfi1_ibdev * dev,struct hfi1_mmap_info * ip,u32 size,void * obj)178 void hfi1_update_mmap_info(struct hfi1_ibdev *dev, struct hfi1_mmap_info *ip,
179 			   u32 size, void *obj)
180 {
181 	size = PAGE_ALIGN(size);
182 
183 	spin_lock_irq(&dev->mmap_offset_lock);
184 	if (dev->mmap_offset == 0)
185 		dev->mmap_offset = PAGE_SIZE;
186 	ip->offset = dev->mmap_offset;
187 	dev->mmap_offset += size;
188 	spin_unlock_irq(&dev->mmap_offset_lock);
189 
190 	ip->size = size;
191 	ip->obj = obj;
192 }
193