]> git.ipfire.org Git - people/ms/linux.git/blame - include/linux/scatterlist.h
Importing "grsecurity-3.1-3.19.2-201503201903.patch"
[people/ms/linux.git] / include / linux / scatterlist.h
CommitLineData
1da177e4
LT
1#ifndef _LINUX_SCATTERLIST_H
2#define _LINUX_SCATTERLIST_H
3
63d9c273 4#include <linux/sched.h>
187f1882
PG
5#include <linux/string.h>
6#include <linux/bug.h>
7#include <linux/mm.h>
8
85cdffcd 9#include <asm/types.h>
d32311fe 10#include <asm/scatterlist.h>
18dabf47
JA
11#include <asm/io.h>
12
0db9299f
JA
13struct sg_table {
14 struct scatterlist *sgl; /* the list */
15 unsigned int nents; /* number of mapped entries */
16 unsigned int orig_nents; /* original size of list */
17};
18
18dabf47
JA
19/*
20 * Notes on SG table design.
21 *
22 * Architectures must provide an unsigned long page_link field in the
23 * scatterlist struct. We use that to place the page pointer AND encode
24 * information about the sg table as well. The two lower bits are reserved
25 * for this information.
26 *
27 * If bit 0 is set, then the page_link contains a pointer to the next sg
28 * table list. Otherwise the next entry is at sg + 1.
29 *
30 * If bit 1 is set, then this sg entry is the last element in a list.
31 *
32 * See sg_next().
33 *
34 */
1da177e4 35
d6ec0842
JA
36#define SG_MAGIC 0x87654321
37
645a8d94
TH
38/*
39 * We overload the LSB of the page pointer to indicate whether it's
40 * a valid sg entry, or whether it points to the start of a new scatterlist.
41 * Those low bits are there for everyone! (thanks mason :-)
42 */
43#define sg_is_chain(sg) ((sg)->page_link & 0x01)
44#define sg_is_last(sg) ((sg)->page_link & 0x02)
45#define sg_chain_ptr(sg) \
46 ((struct scatterlist *) ((sg)->page_link & ~0x03))
47
82f66fbe 48/**
642f1490
JA
49 * sg_assign_page - Assign a given page to an SG entry
50 * @sg: SG entry
51 * @page: The page
82f66fbe
JA
52 *
53 * Description:
642f1490
JA
54 * Assign page to sg entry. Also see sg_set_page(), the most commonly used
55 * variant.
82f66fbe
JA
56 *
57 **/
642f1490 58static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
82f66fbe 59{
18dabf47
JA
60 unsigned long page_link = sg->page_link & 0x3;
61
de26103d
JA
62 /*
63 * In order for the low bit stealing approach to work, pages
64 * must be aligned at a 32-bit boundary as a minimum.
65 */
66 BUG_ON((unsigned long) page & 0x03);
d6ec0842
JA
67#ifdef CONFIG_DEBUG_SG
68 BUG_ON(sg->sg_magic != SG_MAGIC);
645a8d94 69 BUG_ON(sg_is_chain(sg));
d6ec0842 70#endif
18dabf47 71 sg->page_link = page_link | (unsigned long) page;
82f66fbe
JA
72}
73
642f1490
JA
74/**
75 * sg_set_page - Set sg entry to point at given page
76 * @sg: SG entry
77 * @page: The page
78 * @len: Length of data
79 * @offset: Offset into page
80 *
81 * Description:
82 * Use this function to set an sg entry pointing at a page, never assign
83 * the page directly. We encode sg table information in the lower bits
84 * of the page pointer. See sg_page() for looking up the page belonging
85 * to an sg entry.
86 *
87 **/
88static inline void sg_set_page(struct scatterlist *sg, struct page *page,
89 unsigned int len, unsigned int offset)
90{
91 sg_assign_page(sg, page);
92 sg->offset = offset;
93 sg->length = len;
94}
95
645a8d94
TH
96static inline struct page *sg_page(struct scatterlist *sg)
97{
98#ifdef CONFIG_DEBUG_SG
99 BUG_ON(sg->sg_magic != SG_MAGIC);
100 BUG_ON(sg_is_chain(sg));
101#endif
102 return (struct page *)((sg)->page_link & ~0x3);
103}
82f66fbe 104
18dabf47
JA
105/**
106 * sg_set_buf - Set sg entry to point at given data
107 * @sg: SG entry
108 * @buf: Data
109 * @buflen: Data length
110 *
111 **/
03fd9cee 112static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
d32311fe
HX
113 unsigned int buflen)
114{
ac4e97ab
RR
115#ifdef CONFIG_DEBUG_SG
116 BUG_ON(!virt_addr_valid(buf));
63d9c273
MT
117#endif
118#ifdef CONFIG_GRKERNSEC_KSTACKOVERFLOW
119 if (object_starts_on_stack(buf)) {
120 void *adjbuf = buf - current->stack + current->lowmem_stack;
121 sg_set_page(sg, virt_to_page(adjbuf), buflen, offset_in_page(adjbuf));
122 } else
ac4e97ab 123#endif
642f1490 124 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
1da177e4
LT
125}
126
96b418c9
JA
127/*
128 * Loop over each sg element, following the pointer to a new list if necessary
129 */
130#define for_each_sg(sglist, sg, nr, __i) \
131 for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
132
70eb8040
JA
133/**
134 * sg_chain - Chain two sglists together
135 * @prv: First scatterlist
136 * @prv_nents: Number of entries in prv
137 * @sgl: Second scatterlist
138 *
18dabf47
JA
139 * Description:
140 * Links @prv@ and @sgl@ together, to form a longer scatterlist.
70eb8040 141 *
18dabf47 142 **/
70eb8040
JA
143static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
144 struct scatterlist *sgl)
145{
308c09f1 146#ifndef CONFIG_ARCH_HAS_SG_CHAIN
70eb8040
JA
147 BUG();
148#endif
645a8d94
TH
149
150 /*
151 * offset and length are unused for chain entry. Clear them.
152 */
b801a1e7
RR
153 prv[prv_nents - 1].offset = 0;
154 prv[prv_nents - 1].length = 0;
645a8d94 155
73fd546a
JA
156 /*
157 * Set lowest bit to indicate a link pointer, and make sure to clear
158 * the termination bit if it happens to be set.
159 */
160 prv[prv_nents - 1].page_link = ((unsigned long) sgl | 0x01) & ~0x02;
70eb8040
JA
161}
162
82f66fbe
JA
163/**
164 * sg_mark_end - Mark the end of the scatterlist
c46f2334 165 * @sg: SG entryScatterlist
82f66fbe
JA
166 *
167 * Description:
c46f2334
JA
168 * Marks the passed in sg entry as the termination point for the sg
169 * table. A call to sg_next() on this entry will return NULL.
82f66fbe
JA
170 *
171 **/
c46f2334 172static inline void sg_mark_end(struct scatterlist *sg)
82f66fbe 173{
c46f2334
JA
174#ifdef CONFIG_DEBUG_SG
175 BUG_ON(sg->sg_magic != SG_MAGIC);
176#endif
177 /*
178 * Set termination bit, clear potential chain bit
179 */
18dabf47 180 sg->page_link |= 0x02;
c46f2334 181 sg->page_link &= ~0x01;
82f66fbe
JA
182}
183
c8164d89
PB
184/**
185 * sg_unmark_end - Undo setting the end of the scatterlist
186 * @sg: SG entryScatterlist
187 *
188 * Description:
189 * Removes the termination marker from the given entry of the scatterlist.
190 *
191 **/
192static inline void sg_unmark_end(struct scatterlist *sg)
193{
194#ifdef CONFIG_DEBUG_SG
195 BUG_ON(sg->sg_magic != SG_MAGIC);
196#endif
197 sg->page_link &= ~0x02;
198}
199
82f66fbe
JA
200/**
201 * sg_phys - Return physical address of an sg entry
202 * @sg: SG entry
203 *
204 * Description:
205 * This calls page_to_phys() on the page in this sg entry, and adds the
206 * sg offset. The caller must know that it is legal to call page_to_phys()
207 * on the sg page.
208 *
209 **/
85cdffcd 210static inline dma_addr_t sg_phys(struct scatterlist *sg)
82f66fbe
JA
211{
212 return page_to_phys(sg_page(sg)) + sg->offset;
213}
214
215/**
216 * sg_virt - Return virtual address of an sg entry
18dabf47 217 * @sg: SG entry
82f66fbe
JA
218 *
219 * Description:
220 * This calls page_address() on the page in this sg entry, and adds the
221 * sg offset. The caller must know that the sg page has a valid virtual
222 * mapping.
223 *
224 **/
225static inline void *sg_virt(struct scatterlist *sg)
226{
227 return page_address(sg_page(sg)) + sg->offset;
228}
229
2e484610 230int sg_nents(struct scatterlist *sg);
0db9299f
JA
231struct scatterlist *sg_next(struct scatterlist *);
232struct scatterlist *sg_last(struct scatterlist *s, unsigned int);
233void sg_init_table(struct scatterlist *, unsigned int);
234void sg_init_one(struct scatterlist *, const void *, unsigned int);
235
236typedef struct scatterlist *(sg_alloc_fn)(unsigned int, gfp_t);
237typedef void (sg_free_fn)(struct scatterlist *, unsigned int);
238
c53c6d6a 239void __sg_free_table(struct sg_table *, unsigned int, bool, sg_free_fn *);
0db9299f 240void sg_free_table(struct sg_table *);
c53c6d6a
CH
241int __sg_alloc_table(struct sg_table *, unsigned int, unsigned int,
242 struct scatterlist *, gfp_t, sg_alloc_fn *);
0db9299f 243int sg_alloc_table(struct sg_table *, unsigned int, gfp_t);
efc42bc9
TS
244int sg_alloc_table_from_pages(struct sg_table *sgt,
245 struct page **pages, unsigned int n_pages,
246 unsigned long offset, unsigned long size,
247 gfp_t gfp_mask);
0db9299f 248
b1adaf65
FT
249size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
250 void *buf, size_t buflen);
251size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
252 void *buf, size_t buflen);
253
df642cea
AM
254size_t sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents,
255 void *buf, size_t buflen, off_t skip);
256size_t sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents,
257 void *buf, size_t buflen, off_t skip);
258
0db9299f
JA
259/*
260 * Maximum number of entries that will be allocated in one piece, if
261 * a list larger than this is required then chaining will be utilized.
262 */
263#define SG_MAX_SINGLE_ALLOC (PAGE_SIZE / sizeof(struct scatterlist))
264
a321e91b
ID
265/*
266 * sg page iterator
267 *
268 * Iterates over sg entries page-by-page. On each successful iteration,
2db76d7c
ID
269 * you can call sg_page_iter_page(@piter) and sg_page_iter_dma_address(@piter)
270 * to get the current page and its dma address. @piter->sg will point to the
271 * sg holding this page and @piter->sg_pgoffset to the page's page offset
272 * within the sg. The iteration will stop either when a maximum number of sg
273 * entries was reached or a terminating sg (sg_last(sg) == true) was reached.
a321e91b
ID
274 */
275struct sg_page_iter {
a321e91b
ID
276 struct scatterlist *sg; /* sg holding the page */
277 unsigned int sg_pgoffset; /* page offset within the sg */
278
279 /* these are internal states, keep away */
280 unsigned int __nents; /* remaining sg entries */
281 int __pg_advance; /* nr pages to advance at the
282 * next step */
283};
284
285bool __sg_page_iter_next(struct sg_page_iter *piter);
286void __sg_page_iter_start(struct sg_page_iter *piter,
287 struct scatterlist *sglist, unsigned int nents,
288 unsigned long pgoffset);
2db76d7c
ID
289/**
290 * sg_page_iter_page - get the current page held by the page iterator
291 * @piter: page iterator holding the page
292 */
293static inline struct page *sg_page_iter_page(struct sg_page_iter *piter)
294{
295 return nth_page(sg_page(piter->sg), piter->sg_pgoffset);
296}
297
298/**
299 * sg_page_iter_dma_address - get the dma address of the current page held by
300 * the page iterator.
301 * @piter: page iterator holding the page
302 */
303static inline dma_addr_t sg_page_iter_dma_address(struct sg_page_iter *piter)
304{
305 return sg_dma_address(piter->sg) + (piter->sg_pgoffset << PAGE_SHIFT);
306}
a321e91b
ID
307
308/**
309 * for_each_sg_page - iterate over the pages of the given sg list
310 * @sglist: sglist to iterate over
311 * @piter: page iterator to hold current page, sg, sg_pgoffset
312 * @nents: maximum number of sg entries to iterate over
313 * @pgoffset: starting page offset
314 */
315#define for_each_sg_page(sglist, piter, nents, pgoffset) \
316 for (__sg_page_iter_start((piter), (sglist), (nents), (pgoffset)); \
317 __sg_page_iter_next(piter);)
137d3edb
TH
318
319/*
320 * Mapping sg iterator
321 *
322 * Iterates over sg entries mapping page-by-page. On each successful
323 * iteration, @miter->page points to the mapped page and
324 * @miter->length bytes of data can be accessed at @miter->addr. As
325 * long as an interation is enclosed between start and stop, the user
326 * is free to choose control structure and when to stop.
327 *
328 * @miter->consumed is set to @miter->length on each iteration. It
329 * can be adjusted if the user can't consume all the bytes in one go.
330 * Also, a stopped iteration can be resumed by calling next on it.
331 * This is useful when iteration needs to release all resources and
332 * continue later (e.g. at the next interrupt).
333 */
334
335#define SG_MITER_ATOMIC (1 << 0) /* use kmap_atomic */
6de7e356
SAS
336#define SG_MITER_TO_SG (1 << 1) /* flush back to phys on unmap */
337#define SG_MITER_FROM_SG (1 << 2) /* nop */
137d3edb
TH
338
339struct sg_mapping_iter {
340 /* the following three fields can be accessed directly */
341 struct page *page; /* currently mapped page */
342 void *addr; /* pointer to the mapped area */
343 size_t length; /* length of the mapped area */
344 size_t consumed; /* number of consumed bytes */
4225fc85 345 struct sg_page_iter piter; /* page iterator */
137d3edb
TH
346
347 /* these are internal states, keep away */
4225fc85
ID
348 unsigned int __offset; /* offset within page */
349 unsigned int __remaining; /* remaining bytes on page */
137d3edb
TH
350 unsigned int __flags;
351};
352
353void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
354 unsigned int nents, unsigned int flags);
0d6077f8 355bool sg_miter_skip(struct sg_mapping_iter *miter, off_t offset);
137d3edb
TH
356bool sg_miter_next(struct sg_mapping_iter *miter);
357void sg_miter_stop(struct sg_mapping_iter *miter);
358
1da177e4 359#endif /* _LINUX_SCATTERLIST_H */