fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / infiniband / hw / ipath / ipath_user_pages.c
1 /*
2  * Copyright (c) 2006 QLogic, Inc. All rights reserved.
3  * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33
34 #include <linux/mm.h>
35 #include <linux/device.h>
36 #include <linux/vs_memory.h>
37
38 #include "ipath_kernel.h"
39
40 static void __ipath_release_user_pages(struct page **p, size_t num_pages,
41                                    int dirty)
42 {
43         size_t i;
44
45         for (i = 0; i < num_pages; i++) {
46                 ipath_cdbg(MM, "%lu/%lu put_page %p\n", (unsigned long) i,
47                            (unsigned long) num_pages, p[i]);
48                 if (dirty)
49                         set_page_dirty_lock(p[i]);
50                 put_page(p[i]);
51         }
52 }
53
54 /* call with current->mm->mmap_sem held */
55 static int __get_user_pages(unsigned long start_page, size_t num_pages,
56                         struct page **p, struct vm_area_struct **vma)
57 {
58         unsigned long lock_limit;
59         size_t got;
60         int ret;
61
62         lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur >>
63                 PAGE_SHIFT;
64
65         if (num_pages > lock_limit ||
66                 !vx_vmlocked_avail(current->mm, num_pages)) {
67                 ret = -ENOMEM;
68                 goto bail;
69         }
70
71         ipath_cdbg(VERBOSE, "pin %lx pages from vaddr %lx\n",
72                    (unsigned long) num_pages, start_page);
73
74         for (got = 0; got < num_pages; got += ret) {
75                 ret = get_user_pages(current, current->mm,
76                                      start_page + got * PAGE_SIZE,
77                                      num_pages - got, 1, 1,
78                                      p + got, vma);
79                 if (ret < 0)
80                         goto bail_release;
81         }
82
83         vx_vmlocked_add(current->mm, num_pages);
84
85         ret = 0;
86         goto bail;
87
88 bail_release:
89         __ipath_release_user_pages(p, got, 0);
90 bail:
91         return ret;
92 }
93
94 /**
95  * ipath_map_page - a safety wrapper around pci_map_page()
96  *
97  * A dma_addr of all 0's is interpreted by the chip as "disabled".
98  * Unfortunately, it can also be a valid dma_addr returned on some
99  * architectures.
100  *
101  * The powerpc iommu assigns dma_addrs in ascending order, so we don't
102  * have to bother with retries or mapping a dummy page to insure we
103  * don't just get the same mapping again.
104  *
105  * I'm sure we won't be so lucky with other iommu's, so FIXME.
106  */
107 dma_addr_t ipath_map_page(struct pci_dev *hwdev, struct page *page,
108         unsigned long offset, size_t size, int direction)
109 {
110         dma_addr_t phys;
111
112         phys = pci_map_page(hwdev, page, offset, size, direction);
113
114         if (phys == 0) {
115                 pci_unmap_page(hwdev, phys, size, direction);
116                 phys = pci_map_page(hwdev, page, offset, size, direction);
117                 /*
118                  * FIXME: If we get 0 again, we should keep this page,
119                  * map another, then free the 0 page.
120                  */
121         }
122
123         return phys;
124 }
125
126 /**
127  * ipath_map_single - a safety wrapper around pci_map_single()
128  *
129  * Same idea as ipath_map_page().
130  */
131 dma_addr_t ipath_map_single(struct pci_dev *hwdev, void *ptr, size_t size,
132         int direction)
133 {
134         dma_addr_t phys;
135
136         phys = pci_map_single(hwdev, ptr, size, direction);
137
138         if (phys == 0) {
139                 pci_unmap_single(hwdev, phys, size, direction);
140                 phys = pci_map_single(hwdev, ptr, size, direction);
141                 /*
142                  * FIXME: If we get 0 again, we should keep this page,
143                  * map another, then free the 0 page.
144                  */
145         }
146
147         return phys;
148 }
149
150 /**
151  * ipath_get_user_pages - lock user pages into memory
152  * @start_page: the start page
153  * @num_pages: the number of pages
154  * @p: the output page structures
155  *
156  * This function takes a given start page (page aligned user virtual
157  * address) and pins it and the following specified number of pages.  For
158  * now, num_pages is always 1, but that will probably change at some point
159  * (because caller is doing expected sends on a single virtually contiguous
160  * buffer, so we can do all pages at once).
161  */
162 int ipath_get_user_pages(unsigned long start_page, size_t num_pages,
163                          struct page **p)
164 {
165         int ret;
166
167         down_write(&current->mm->mmap_sem);
168
169         ret = __get_user_pages(start_page, num_pages, p, NULL);
170
171         up_write(&current->mm->mmap_sem);
172
173         return ret;
174 }
175
176 /**
177  * ipath_get_user_pages_nocopy - lock a single page for I/O and mark shared
178  * @start_page: the page to lock
179  * @p: the output page structure
180  *
181  * This is similar to ipath_get_user_pages, but it's always one page, and we
182  * mark the page as locked for I/O, and shared.  This is used for the user
183  * process page that contains the destination address for the rcvhdrq tail
184  * update, so we need to have the vma. If we don't do this, the page can be
185  * taken away from us on fork, even if the child never touches it, and then
186  * the user process never sees the tail register updates.
187  */
188 int ipath_get_user_pages_nocopy(unsigned long page, struct page **p)
189 {
190         struct vm_area_struct *vma;
191         int ret;
192
193         down_write(&current->mm->mmap_sem);
194
195         ret = __get_user_pages(page, 1, p, &vma);
196
197         up_write(&current->mm->mmap_sem);
198
199         return ret;
200 }
201
202 void ipath_release_user_pages(struct page **p, size_t num_pages)
203 {
204         down_write(&current->mm->mmap_sem);
205
206         __ipath_release_user_pages(p, num_pages, 1);
207
208         vx_vmlocked_sub(current->mm, num_pages);
209
210         up_write(&current->mm->mmap_sem);
211 }
212
213 struct ipath_user_pages_work {
214         struct work_struct work;
215         struct mm_struct *mm;
216         unsigned long num_pages;
217 };
218
219 static void user_pages_account(struct work_struct *_work)
220 {
221         struct ipath_user_pages_work *work =
222                 container_of(_work, struct ipath_user_pages_work, work);
223
224         down_write(&work->mm->mmap_sem);
225         vx_vmlocked_sub(work->mm, work->num_pages);
226         up_write(&work->mm->mmap_sem);
227         mmput(work->mm);
228         kfree(work);
229 }
230
231 void ipath_release_user_pages_on_close(struct page **p, size_t num_pages)
232 {
233         struct ipath_user_pages_work *work;
234         struct mm_struct *mm;
235
236         __ipath_release_user_pages(p, num_pages, 1);
237
238         mm = get_task_mm(current);
239         if (!mm)
240                 goto bail;
241
242         work = kmalloc(sizeof(*work), GFP_KERNEL);
243         if (!work)
244                 goto bail_mm;
245
246         goto bail;
247
248         INIT_WORK(&work->work, user_pages_account);
249         work->mm = mm;
250         work->num_pages = num_pages;
251
252 bail_mm:
253         mmput(mm);
254 bail:
255         return;
256 }