1#ifndef _LINUX_SCATTERLIST_H
2#define _LINUX_SCATTERLIST_H
3
4#include <linux/string.h>
5#include <linux/bug.h>
6#include <linux/mm.h>
7
8#include <asm/types.h>
9#include <asm/scatterlist.h>
10#include <asm/io.h>
11
12struct sg_table {
13 struct scatterlist *sgl;
14 unsigned int nents;
15 unsigned int orig_nents;
16};
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35#define SG_MAGIC 0x87654321
36
37
38
39
40
41
42#define sg_is_chain(sg) ((sg)->page_link & 0x01)
43#define sg_is_last(sg) ((sg)->page_link & 0x02)
44#define sg_chain_ptr(sg) \
45 ((struct scatterlist *) ((sg)->page_link & ~0x03))
46
47
48
49
50
51
52
53
54
55
56
57static inline void sg_assign_page(struct scatterlist *sg, struct page *page)
58{
59 unsigned long page_link = sg->page_link & 0x3;
60
61
62
63
64
65 BUG_ON((unsigned long) page & 0x03);
66#ifdef CONFIG_DEBUG_SG
67 BUG_ON(sg->sg_magic != SG_MAGIC);
68 BUG_ON(sg_is_chain(sg));
69#endif
70 sg->page_link = page_link | (unsigned long) page;
71}
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87static inline void sg_set_page(struct scatterlist *sg, struct page *page,
88 unsigned int len, unsigned int offset)
89{
90 sg_assign_page(sg, page);
91 sg->offset = offset;
92 sg->length = len;
93}
94
95static inline struct page *sg_page(struct scatterlist *sg)
96{
97#ifdef CONFIG_DEBUG_SG
98 BUG_ON(sg->sg_magic != SG_MAGIC);
99 BUG_ON(sg_is_chain(sg));
100#endif
101 return (struct page *)((sg)->page_link & ~0x3);
102}
103
104
105
106
107
108
109
110
111static inline void sg_set_buf(struct scatterlist *sg, const void *buf,
112 unsigned int buflen)
113{
114#ifdef CONFIG_DEBUG_SG
115 BUG_ON(!virt_addr_valid(buf));
116#endif
117 sg_set_page(sg, virt_to_page(buf), buflen, offset_in_page(buf));
118}
119
120
121
122
123#define for_each_sg(sglist, sg, nr, __i) \
124 for (__i = 0, sg = (sglist); __i < (nr); __i++, sg = sg_next(sg))
125
126
127
128
129
130
131
132
133
134
135
136static inline void sg_chain(struct scatterlist *prv, unsigned int prv_nents,
137 struct scatterlist *sgl)
138{
139#ifndef CONFIG_ARCH_HAS_SG_CHAIN
140 BUG();
141#endif
142
143
144
145
146 prv[prv_nents - 1].offset = 0;
147 prv[prv_nents - 1].length = 0;
148
149
150
151
152
153 prv[prv_nents - 1].page_link = ((unsigned long) sgl | 0x01) & ~0x02;
154}
155
156
157
158
159
160
161
162
163
164
165static inline void sg_mark_end(struct scatterlist *sg)
166{
167#ifdef CONFIG_DEBUG_SG
168 BUG_ON(sg->sg_magic != SG_MAGIC);
169#endif
170
171
172
173 sg->page_link |= 0x02;
174 sg->page_link &= ~0x01;
175}
176
177
178
179
180
181
182
183
184
185static inline void sg_unmark_end(struct scatterlist *sg)
186{
187#ifdef CONFIG_DEBUG_SG
188 BUG_ON(sg->sg_magic != SG_MAGIC);
189#endif
190 sg->page_link &= ~0x02;
191}
192
193
194
195
196
197
198
199
200
201
202
203static inline dma_addr_t sg_phys(struct scatterlist *sg)
204{
205 return page_to_phys(sg_page(sg)) + sg->offset;
206}
207
208
209
210
211
212
213
214
215
216
217
218static inline void *sg_virt(struct scatterlist *sg)
219{
220 return page_address(sg_page(sg)) + sg->offset;
221}
222
223int sg_nents(struct scatterlist *sg);
224struct scatterlist *sg_next(struct scatterlist *);
225struct scatterlist *sg_last(struct scatterlist *s, unsigned int);
226void sg_init_table(struct scatterlist *, unsigned int);
227void sg_init_one(struct scatterlist *, const void *, unsigned int);
228
229typedef struct scatterlist *(sg_alloc_fn)(unsigned int, gfp_t);
230typedef void (sg_free_fn)(struct scatterlist *, unsigned int);
231
232void __sg_free_table(struct sg_table *, unsigned int, bool, sg_free_fn *);
233void sg_free_table(struct sg_table *);
234int __sg_alloc_table(struct sg_table *, unsigned int, unsigned int,
235 struct scatterlist *, gfp_t, sg_alloc_fn *);
236int sg_alloc_table(struct sg_table *, unsigned int, gfp_t);
237int sg_alloc_table_from_pages(struct sg_table *sgt,
238 struct page **pages, unsigned int n_pages,
239 unsigned long offset, unsigned long size,
240 gfp_t gfp_mask);
241
242size_t sg_copy_from_buffer(struct scatterlist *sgl, unsigned int nents,
243 void *buf, size_t buflen);
244size_t sg_copy_to_buffer(struct scatterlist *sgl, unsigned int nents,
245 void *buf, size_t buflen);
246
247size_t sg_pcopy_from_buffer(struct scatterlist *sgl, unsigned int nents,
248 void *buf, size_t buflen, off_t skip);
249size_t sg_pcopy_to_buffer(struct scatterlist *sgl, unsigned int nents,
250 void *buf, size_t buflen, off_t skip);
251
252
253
254
255
256#define SG_MAX_SINGLE_ALLOC (PAGE_SIZE / sizeof(struct scatterlist))
257
258
259
260
261
262
263
264
265
266
267
268struct sg_page_iter {
269 struct scatterlist *sg;
270 unsigned int sg_pgoffset;
271
272
273 unsigned int __nents;
274 int __pg_advance;
275
276};
277
278bool __sg_page_iter_next(struct sg_page_iter *piter);
279void __sg_page_iter_start(struct sg_page_iter *piter,
280 struct scatterlist *sglist, unsigned int nents,
281 unsigned long pgoffset);
282
283
284
285
286static inline struct page *sg_page_iter_page(struct sg_page_iter *piter)
287{
288 return nth_page(sg_page(piter->sg), piter->sg_pgoffset);
289}
290
291
292
293
294
295
296static inline dma_addr_t sg_page_iter_dma_address(struct sg_page_iter *piter)
297{
298 return sg_dma_address(piter->sg) + (piter->sg_pgoffset << PAGE_SHIFT);
299}
300
301
302
303
304
305
306
307
308#define for_each_sg_page(sglist, piter, nents, pgoffset) \
309 for (__sg_page_iter_start((piter), (sglist), (nents), (pgoffset)); \
310 __sg_page_iter_next(piter);)
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328#define SG_MITER_ATOMIC (1 << 0)
329#define SG_MITER_TO_SG (1 << 1)
330#define SG_MITER_FROM_SG (1 << 2)
331
332struct sg_mapping_iter {
333
334 struct page *page;
335 void *addr;
336 size_t length;
337 size_t consumed;
338 struct sg_page_iter piter;
339
340
341 unsigned int __offset;
342 unsigned int __remaining;
343 unsigned int __flags;
344};
345
346void sg_miter_start(struct sg_mapping_iter *miter, struct scatterlist *sgl,
347 unsigned int nents, unsigned int flags);
348bool sg_miter_skip(struct sg_mapping_iter *miter, off_t offset);
349bool sg_miter_next(struct sg_mapping_iter *miter);
350void sg_miter_stop(struct sg_mapping_iter *miter);
351
352#endif
353