GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / infiniband / hw / ipath / ipath_user_pages.c
bloba04aa1f86f020b0b045528155b637b6115efe8c7
1 /*
2 * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved.
3 * Copyright (c) 2003, 2004, 2005, 2006 PathScale, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
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.
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.
34 #include <linux/mm.h>
35 #include <linux/device.h>
36 #include <linux/slab.h>
37 #include <linux/sched.h>
39 #include "ipath_kernel.h"
41 static void __ipath_release_user_pages(struct page **p, size_t num_pages,
42 int dirty)
44 size_t i;
46 for (i = 0; i < num_pages; i++) {
47 ipath_cdbg(MM, "%lu/%lu put_page %p\n", (unsigned long) i,
48 (unsigned long) num_pages, p[i]);
49 if (dirty)
50 set_page_dirty_lock(p[i]);
51 put_page(p[i]);
55 /* call with current->mm->mmap_sem held */
56 static int __get_user_pages(unsigned long start_page, size_t num_pages,
57 struct page **p, struct vm_area_struct **vma)
59 unsigned long lock_limit;
60 size_t got;
61 int ret;
63 lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
65 if (num_pages > lock_limit) {
66 ret = -ENOMEM;
67 goto bail;
70 ipath_cdbg(VERBOSE, "pin %lx pages from vaddr %lx\n",
71 (unsigned long) num_pages, start_page);
73 for (got = 0; got < num_pages; got += ret) {
74 ret = get_user_pages(current, current->mm,
75 start_page + got * PAGE_SIZE,
76 num_pages - got, 1, 1,
77 p + got, vma);
78 if (ret < 0)
79 goto bail_release;
82 current->mm->locked_vm += num_pages;
84 ret = 0;
85 goto bail;
87 bail_release:
88 __ipath_release_user_pages(p, got, 0);
89 bail:
90 return ret;
93 dma_addr_t ipath_map_page(struct pci_dev *hwdev, struct page *page,
94 unsigned long offset, size_t size, int direction)
96 dma_addr_t phys;
98 phys = pci_map_page(hwdev, page, offset, size, direction);
100 if (phys == 0) {
101 pci_unmap_page(hwdev, phys, size, direction);
102 phys = pci_map_page(hwdev, page, offset, size, direction);
105 return phys;
109 * ipath_map_single - a safety wrapper around pci_map_single()
111 * Same idea as ipath_map_page().
113 dma_addr_t ipath_map_single(struct pci_dev *hwdev, void *ptr, size_t size,
114 int direction)
116 dma_addr_t phys;
118 phys = pci_map_single(hwdev, ptr, size, direction);
120 if (phys == 0) {
121 pci_unmap_single(hwdev, phys, size, direction);
122 phys = pci_map_single(hwdev, ptr, size, direction);
125 return phys;
129 * ipath_get_user_pages - lock user pages into memory
130 * @start_page: the start page
131 * @num_pages: the number of pages
132 * @p: the output page structures
134 * This function takes a given start page (page aligned user virtual
135 * address) and pins it and the following specified number of pages. For
136 * now, num_pages is always 1, but that will probably change at some point
137 * (because caller is doing expected sends on a single virtually contiguous
138 * buffer, so we can do all pages at once).
140 int ipath_get_user_pages(unsigned long start_page, size_t num_pages,
141 struct page **p)
143 int ret;
145 down_write(&current->mm->mmap_sem);
147 ret = __get_user_pages(start_page, num_pages, p, NULL);
149 up_write(&current->mm->mmap_sem);
151 return ret;
154 void ipath_release_user_pages(struct page **p, size_t num_pages)
156 down_write(&current->mm->mmap_sem);
158 __ipath_release_user_pages(p, num_pages, 1);
160 current->mm->locked_vm -= num_pages;
162 up_write(&current->mm->mmap_sem);
165 struct ipath_user_pages_work {
166 struct work_struct work;
167 struct mm_struct *mm;
168 unsigned long num_pages;
171 static void user_pages_account(struct work_struct *_work)
173 struct ipath_user_pages_work *work =
174 container_of(_work, struct ipath_user_pages_work, work);
176 down_write(&work->mm->mmap_sem);
177 work->mm->locked_vm -= work->num_pages;
178 up_write(&work->mm->mmap_sem);
179 mmput(work->mm);
180 kfree(work);
183 void ipath_release_user_pages_on_close(struct page **p, size_t num_pages)
185 struct ipath_user_pages_work *work;
186 struct mm_struct *mm;
188 __ipath_release_user_pages(p, num_pages, 1);
190 mm = get_task_mm(current);
191 if (!mm)
192 return;
194 work = kmalloc(sizeof(*work), GFP_KERNEL);
195 if (!work)
196 goto bail_mm;
198 INIT_WORK(&work->work, user_pages_account);
199 work->mm = mm;
200 work->num_pages = num_pages;
202 schedule_work(&work->work);
203 return;
205 bail_mm:
206 mmput(mm);
207 return;