Merge branch 'topic/hda-gateway' into topic/hda
[linux-2.6] / drivers / infiniband / hw / ipath / ipath_mmap.c
1 /*
2  * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved.
3  *
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:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
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.
22  *
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
30  * SOFTWARE.
31  */
32
33 #include <linux/module.h>
34 #include <linux/vmalloc.h>
35 #include <linux/mm.h>
36 #include <linux/errno.h>
37 #include <asm/pgtable.h>
38
39 #include "ipath_verbs.h"
40
41 /**
42  * ipath_release_mmap_info - free mmap info structure
43  * @ref: a pointer to the kref within struct ipath_mmap_info
44  */
45 void ipath_release_mmap_info(struct kref *ref)
46 {
47         struct ipath_mmap_info *ip =
48                 container_of(ref, struct ipath_mmap_info, ref);
49         struct ipath_ibdev *dev = to_idev(ip->context->device);
50
51         spin_lock_irq(&dev->pending_lock);
52         list_del(&ip->pending_mmaps);
53         spin_unlock_irq(&dev->pending_lock);
54
55         vfree(ip->obj);
56         kfree(ip);
57 }
58
59 /*
60  * open and close keep track of how many times the CQ is mapped,
61  * to avoid releasing it.
62  */
63 static void ipath_vma_open(struct vm_area_struct *vma)
64 {
65         struct ipath_mmap_info *ip = vma->vm_private_data;
66
67         kref_get(&ip->ref);
68 }
69
70 static void ipath_vma_close(struct vm_area_struct *vma)
71 {
72         struct ipath_mmap_info *ip = vma->vm_private_data;
73
74         kref_put(&ip->ref, ipath_release_mmap_info);
75 }
76
77 static struct vm_operations_struct ipath_vm_ops = {
78         .open =     ipath_vma_open,
79         .close =    ipath_vma_close,
80 };
81
82 /**
83  * ipath_mmap - create a new mmap region
84  * @context: the IB user context of the process making the mmap() call
85  * @vma: the VMA to be initialized
86  * Return zero if the mmap is OK. Otherwise, return an errno.
87  */
88 int ipath_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
89 {
90         struct ipath_ibdev *dev = to_idev(context->device);
91         unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
92         unsigned long size = vma->vm_end - vma->vm_start;
93         struct ipath_mmap_info *ip, *pp;
94         int ret = -EINVAL;
95
96         /*
97          * Search the device's list of objects waiting for a mmap call.
98          * Normally, this list is very short since a call to create a
99          * CQ, QP, or SRQ is soon followed by a call to mmap().
100          */
101         spin_lock_irq(&dev->pending_lock);
102         list_for_each_entry_safe(ip, pp, &dev->pending_mmaps,
103                                  pending_mmaps) {
104                 /* Only the creator is allowed to mmap the object */
105                 if (context != ip->context || (__u64) offset != ip->offset)
106                         continue;
107                 /* Don't allow a mmap larger than the object. */
108                 if (size > ip->size)
109                         break;
110
111                 list_del_init(&ip->pending_mmaps);
112                 spin_unlock_irq(&dev->pending_lock);
113
114                 ret = remap_vmalloc_range(vma, ip->obj, 0);
115                 if (ret)
116                         goto done;
117                 vma->vm_ops = &ipath_vm_ops;
118                 vma->vm_private_data = ip;
119                 ipath_vma_open(vma);
120                 goto done;
121         }
122         spin_unlock_irq(&dev->pending_lock);
123 done:
124         return ret;
125 }
126
127 /*
128  * Allocate information for ipath_mmap
129  */
130 struct ipath_mmap_info *ipath_create_mmap_info(struct ipath_ibdev *dev,
131                                                u32 size,
132                                                struct ib_ucontext *context,
133                                                void *obj) {
134         struct ipath_mmap_info *ip;
135
136         ip = kmalloc(sizeof *ip, GFP_KERNEL);
137         if (!ip)
138                 goto bail;
139
140         size = PAGE_ALIGN(size);
141
142         spin_lock_irq(&dev->mmap_offset_lock);
143         if (dev->mmap_offset == 0)
144                 dev->mmap_offset = PAGE_SIZE;
145         ip->offset = dev->mmap_offset;
146         dev->mmap_offset += size;
147         spin_unlock_irq(&dev->mmap_offset_lock);
148
149         INIT_LIST_HEAD(&ip->pending_mmaps);
150         ip->size = size;
151         ip->context = context;
152         ip->obj = obj;
153         kref_init(&ip->ref);
154
155 bail:
156         return ip;
157 }
158
159 void ipath_update_mmap_info(struct ipath_ibdev *dev,
160                             struct ipath_mmap_info *ip,
161                             u32 size, void *obj) {
162         size = PAGE_ALIGN(size);
163
164         spin_lock_irq(&dev->mmap_offset_lock);
165         if (dev->mmap_offset == 0)
166                 dev->mmap_offset = PAGE_SIZE;
167         ip->offset = dev->mmap_offset;
168         dev->mmap_offset += size;
169         spin_unlock_irq(&dev->mmap_offset_lock);
170
171         ip->size = size;
172         ip->obj = obj;
173 }