blob: a82157db46895c5f770e68139076b949042cede9 [file] [log] [blame]
Ralph Campbell373d9912006-09-22 15:22:26 -07001/*
2 * Copyright (c) 2006 QLogic, Inc. 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
Ralph Campbell373d9912006-09-22 15:22:26 -070033#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 */
45void ipath_release_mmap_info(struct kref *ref)
46{
47 struct ipath_mmap_info *ip =
48 container_of(ref, struct ipath_mmap_info, ref);
49
50 vfree(ip->obj);
51 kfree(ip);
52}
53
54/*
55 * open and close keep track of how many times the CQ is mapped,
56 * to avoid releasing it.
57 */
58static void ipath_vma_open(struct vm_area_struct *vma)
59{
60 struct ipath_mmap_info *ip = vma->vm_private_data;
61
62 kref_get(&ip->ref);
63 ip->mmap_cnt++;
64}
65
66static void ipath_vma_close(struct vm_area_struct *vma)
67{
68 struct ipath_mmap_info *ip = vma->vm_private_data;
69
70 ip->mmap_cnt--;
71 kref_put(&ip->ref, ipath_release_mmap_info);
72}
73
74static struct vm_operations_struct ipath_vm_ops = {
75 .open = ipath_vma_open,
76 .close = ipath_vma_close,
77};
78
79/**
80 * ipath_mmap - create a new mmap region
81 * @context: the IB user context of the process making the mmap() call
82 * @vma: the VMA to be initialized
83 * Return zero if the mmap is OK. Otherwise, return an errno.
84 */
85int ipath_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
86{
87 struct ipath_ibdev *dev = to_idev(context->device);
88 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
89 unsigned long size = vma->vm_end - vma->vm_start;
90 struct ipath_mmap_info *ip, **pp;
91 int ret = -EINVAL;
92
93 /*
94 * Search the device's list of objects waiting for a mmap call.
95 * Normally, this list is very short since a call to create a
96 * CQ, QP, or SRQ is soon followed by a call to mmap().
97 */
98 spin_lock_irq(&dev->pending_lock);
99 for (pp = &dev->pending_mmaps; (ip = *pp); pp = &ip->next) {
100 /* Only the creator is allowed to mmap the object */
101 if (context != ip->context || (void *) offset != ip->obj)
102 continue;
103 /* Don't allow a mmap larger than the object. */
104 if (size > ip->size)
105 break;
106
107 *pp = ip->next;
108 spin_unlock_irq(&dev->pending_lock);
109
110 ret = remap_vmalloc_range(vma, ip->obj, 0);
111 if (ret)
112 goto done;
113 vma->vm_ops = &ipath_vm_ops;
114 vma->vm_private_data = ip;
115 ipath_vma_open(vma);
116 goto done;
117 }
118 spin_unlock_irq(&dev->pending_lock);
119done:
120 return ret;
121}