1 /*
2  * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. 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/sched/signal.h>
36 #include <linux/device.h>
37 
38 #include "qib.h"
39 
40 static void __qib_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 		if (dirty)
47 			set_page_dirty_lock(p[i]);
48 		put_page(p[i]);
49 	}
50 }
51 
52 /**
53  * qib_map_page - a safety wrapper around pci_map_page()
54  *
55  * A dma_addr of all 0's is interpreted by the chip as "disabled".
56  * Unfortunately, it can also be a valid dma_addr returned on some
57  * architectures.
58  *
59  * The powerpc iommu assigns dma_addrs in ascending order, so we don't
60  * have to bother with retries or mapping a dummy page to insure we
61  * don't just get the same mapping again.
62  *
63  * I'm sure we won't be so lucky with other iommu's, so FIXME.
64  */
65 int qib_map_page(struct pci_dev *hwdev, struct page *page, dma_addr_t *daddr)
66 {
67 	dma_addr_t phys;
68 
69 	phys = pci_map_page(hwdev, page, 0, PAGE_SIZE, PCI_DMA_FROMDEVICE);
70 	if (pci_dma_mapping_error(hwdev, phys))
71 		return -ENOMEM;
72 
73 	if (!phys) {
74 		pci_unmap_page(hwdev, phys, PAGE_SIZE, PCI_DMA_FROMDEVICE);
75 		phys = pci_map_page(hwdev, page, 0, PAGE_SIZE,
76 				    PCI_DMA_FROMDEVICE);
77 		if (pci_dma_mapping_error(hwdev, phys))
78 			return -ENOMEM;
79 		/*
80 		 * FIXME: If we get 0 again, we should keep this page,
81 		 * map another, then free the 0 page.
82 		 */
83 	}
84 	*daddr = phys;
85 	return 0;
86 }
87 
88 /**
89  * qib_get_user_pages - lock user pages into memory
90  * @start_page: the start page
91  * @num_pages: the number of pages
92  * @p: the output page structures
93  *
94  * This function takes a given start page (page aligned user virtual
95  * address) and pins it and the following specified number of pages.  For
96  * now, num_pages is always 1, but that will probably change at some point
97  * (because caller is doing expected sends on a single virtually contiguous
98  * buffer, so we can do all pages at once).
99  */
100 int qib_get_user_pages(unsigned long start_page, size_t num_pages,
101 		       struct page **p)
102 {
103 	unsigned long locked, lock_limit;
104 	size_t got;
105 	int ret;
106 
107 	lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT;
108 	locked = atomic64_add_return(num_pages, &current->mm->pinned_vm);
109 
110 	if (locked > lock_limit && !capable(CAP_IPC_LOCK)) {
111 		ret = -ENOMEM;
112 		goto bail;
113 	}
114 
115 	down_read(&current->mm->mmap_sem);
116 	for (got = 0; got < num_pages; got += ret) {
117 		ret = get_user_pages_longterm(start_page + got * PAGE_SIZE,
118 					      num_pages - got,
119 					      FOLL_WRITE | FOLL_FORCE,
120 					      p + got, NULL);
121 		if (ret < 0) {
122 			up_read(&current->mm->mmap_sem);
123 			goto bail_release;
124 		}
125 	}
126 	up_read(&current->mm->mmap_sem);
127 
128 	return 0;
129 bail_release:
130 	__qib_release_user_pages(p, got, 0);
131 bail:
132 	atomic64_sub(num_pages, &current->mm->pinned_vm);
133 	return ret;
134 }
135 
136 void qib_release_user_pages(struct page **p, size_t num_pages)
137 {
138 	__qib_release_user_pages(p, num_pages, 1);
139 
140 	/* during close after signal, mm can be NULL */
141 	if (current->mm)
142 		atomic64_sub(num_pages, &current->mm->pinned_vm);
143 }
144